This file is indexed.

/usr/src/castle-game-engine-4.1.1/fonts/windows/castlewinfontconvert.pas is in castle-game-engine-src 4.1.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
  Copyright 2002-2013 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Convert fonts available on Windows to TBitmapFont and TOutlineFont.

  This unit heavily depends on GetGlpyhOutline WinAPI function.
  This function is our "core" of converting Windows fonts to
  TBitmapFont or TOutlineFont. Unfortunately, this makes this unit
  Windows-only forever.
  In the future I plan to write some similiar unit, but portable.
  Probably using FreeType2 library.
}

unit CastleWinFontConvert;

interface

uses Windows, CastleBitmapFonts, CastleOutlineFonts;

{ Create our font from a Windows font handle.
  Remeber to free resulting font later by FreeAndNilFont.
  @groupBegin }
function Font2BitmapFont(WinFont: HFont): TBitmapFont;
function Font2OutlineFont(WinFont: HFont): TOutlineFont;
{ @groupEnd }

{ Free and nil Font instance, freeing also all characters by FreeMem.
  Use this only on fonts with characters created by Font2BitmapFont /
  Font2OutlineFont.
  @groupBegin }
procedure FreeAndNilFont(var Font: TBitmapFont); overload;
procedure FreeAndNilFont(var Font: TOutlineFont); overload;
{ @groupEnd }

implementation

uses SysUtils, CastleUtils, CastleGenericLists;

{ Note about "underline" and "strikeout": it does not matter whether
  Window font was created as underline or strikeout (e.g. what values for
  fdwUnderline/StrikeOut were set when calling CreateFont).
  Fonts converted by these functions are NEVER underlined or strikeout.

  On the other hand, "bold" (actually "weight") and "italic" matter.
  That is, fonts created by this functions reflect the weight and italic settings
  of the font. This works regardless if there are separate TTF files for
  bold/italic or versions not (in the latter case,
  I assume that Windows automatically synthesizes bold/italic versions).

  This is true both for Font2BitmapChar and Font2OutlineChar.
  This is an effect of how Windows.GetGlyphOutline works,
  although it doesn't seem documented anywhere.
}

const
  IdentityMat2:Mat2= { identity matrix }
    (eM11:(fract:0; value:1); eM12:(fract:0; value:0);    { 1.0  0.0 }
     eM21:(fract:0; value:0); eM22:(fract:0; value:1));   { 0.0  1.0 }

{ Create a bitmap font character
  from a currently selected Windows font on device context DC. }
function Font2BitmapChar_HDc(dc: HDC; c: char): PBitmapChar;
var
  GlyphData: PByteArray;
  GlyphMetrics: TGlyphMetrics;
  GlyphDataSize: Dword;
  j: Cardinal;
  RowByteLength: Cardinal;
begin
  GlyphData := nil;
  try
    { get GlyphDataSize }
    GlyphDataSize := GetGlyphOutline(dc, Ord(c), GGO_BITMAP, GlyphMetrics, 0,
      nil, IdentityMat2);
    OSCheck( GlyphDataSize <> GDI_ERROR, 'GlyphDataSize');

    if GlyphDataSize = 0 then
    begin
      { Assert((gmBlackBoxX = 1) and (gmBlackBoxY = 1)) works under Win2000 Prof,
        Assert((gmBlackBoxX = 0) and (gmBlackBoxY = 0)) works under Win98.
        So I just ignore values of gmBlackBoxX and gmBlackBoxY -- it seems
        that they have no guaranteed value when GlyphDataSize = 0. }
      Result := GetMem(SizeOf(TBitmapCharInfo));

      Result^.Info.Alignment := 4;
      Result^.Info.XOrig := -GlyphMetrics.gmptGlyphOrigin.x;
      Result^.Info.YOrig := -GlyphMetrics.gmptGlyphOrigin.y;
      Result^.Info.XMove := GlyphMetrics.gmCellIncX;
      Result^.Info.YMove := GlyphMetrics.gmCellIncY;
      Result^.Info.Width := 0;
      Result^.Info.Height := 0;
    end else
    begin
      GlyphData := GetMem(GlyphDataSize);
      Check( GetGlyphOutline(dc, Ord(c), GGO_BITMAP, GlyphMetrics, GlyphDataSize,
        GlyphData, IdentityMat2) <> GDI_ERROR, 'GetGlyphOutline failed');

       { alignment w zwroconej data jest zawsze 4 }
       RowByteLength := BitmapCharRowByteLength(GlyphMetrics.gmBlackBoxX, 4);

       { Under Win98 this assertion was always true.
           Assert(GlyphDataSize = linelen * gmBlackBoxY);
         Under Win2000 Prof this is no longer true -- it seems that GlyphDataSize
         is sometimes excessively large, e.g. I have here gmBlackBoxX = 12
         (so linelen = 4), gmBlackBoxY = 19, so GlyphDataSize should be 19*4.
         But it is 80 = 20*4.

         So I'm correcting here GlyphDataSize to something smaller. This
         leads to no harm because GetMem(Pointer(GlyphData), GlyphDataSize);
         is already done. So I already allocated memory. I will just look at
         this memory area as though it would be smaller than it is. }
      Assert(GlyphDataSize >= RowByteLength * GlyphMetrics.gmBlackBoxY);
      GlyphDataSize := RowByteLength * GlyphMetrics.gmBlackBoxY;

      Result := GetMem(SizeOf(TBitmapCharInfo) + GlyphDataSize*SizeOf(Byte));

      Result^.Info.Alignment := 4;
      Result^.Info.XOrig := -GlyphMetrics.gmptGlyphOrigin.x;
      Result^.Info.YOrig := Integer(GlyphMetrics.gmBlackBoxY) - GlyphMetrics.gmptGlyphOrigin.y;
      Result^.Info.XMove := GlyphMetrics.gmCellIncX;
      Result^.Info.YMove := GlyphMetrics.gmCellIncY;
      Result^.Info.Width:=  GlyphMetrics.gmBlackBoxX;
      Result^.Info.Height := GlyphMetrics.gmBlackBoxY;

      { copy GlyphData do Result^.Data line by line.
        We must replace line order - TBitmapFont wants lines bottom -> top,
        while GlyphData has lines top -> bottom. }
      for j := 0 to GlyphMetrics.gmBlackBoxY-1 do
        Move(GlyphData^[(Result^.Info.Height - j - 1) * RowByteLength],
             Result^.Data[j * RowByteLength],
             RowByteLength);
    end;
  finally
    FreeMemNiling(Pointer(GlyphData));
  end;
end;

type
  TOutlineCharItemList = specialize TGenericStructList<TOutlineCharItem>;

{ Create an outline font character
  from a currently selected Windows font on device context DC. }
function Font2OutlineChar_HDc(dc: HDC; c: char): POutlineChar;
var
  GlyphMetrics: TGlyphMetrics;
  GlyphDataSize: Dword;
  Buffer : Pointer;
  BufferEnd, PolygonEnd : PtrUInt;
  PolHeader : PTTPolygonHeader;
  PolCurve : PTTPolyCurve;
  i, j: integer;
type
  TArray_PointFX = packed array [0..High(Word)] of TPointFX;
  PArray_PointFX = ^TArray_PointFX;
var
  PointsFX: PArray_PointFX;
  ResultItems: TOutlineCharItemList;
  ResultInfo: TOutlineCharInfo;
  lastPunkt: record x, y: Single end;
  Dlug: Cardinal;

  procedure ResultItemsAdd(Kind: TPolygonKind; Count: Cardinal{ = 0}); overload;
  { use only with Kind <> pkPoint }
  var
    Item: POutlineCharItem;
  begin
    Item := ResultItems.Add;
    Assert(Kind <> pkPoint);
    Item^.Kind := Kind;
    Item^.Count := Count;
  end;

  procedure ResultItemsAdd(Kind: TPolygonKind {Count: Cardinal = 0 }); overload;
  begin
    ResultItemsAdd(Kind, 0);
  end;

  procedure ResultItemsAdd(x, y: Single); overload;
  var
    Item: POutlineCharItem;
  begin
    Item := ResultItems.Add;
    Item^.Kind := pkPoint;
    Item^.x := x;
    Item^.y := y;
  end;

  function ToFloat(const Val: TFixed): Extended;
  begin
    result := Val.value + (Val.fract / Dword(High(Val.fract))+1 );
  end;

begin
  Result := nil; { <- only to avoid Delphi warning }

  ResultItems := TOutlineCharItemList.Create;
  try
    Buffer := nil;
    try
      { get Buffer }
      GlyphDataSize := GetGlyphOutline(dc, Ord(c), GGO_NATIVE, GlyphMetrics, 0, nil,
        IdentityMat2);
      OSCheck( GlyphDataSize <> GDI_ERROR, 'GetGlyphOutline');
      Buffer := GetMem(GlyphDataSize);
      Check( GetGlyphOutline(dc, Ord(c), GGO_NATIVE, GlyphMetrics, GlyphDataSize,
        Buffer, identityMat2) <> GDI_ERROR, 'GetGlyphOutline');

      { convert GlyphMetrics to ResultInfo (ResultInfo.Polygons/ItemsCount will be
        calculated later) }
      ResultInfo.MoveX := GlyphMetrics.gmCellIncX;
      ResultInfo.MoveY := GlyphMetrics.gmCellIncY;
      ResultInfo.Height := GlyphMetrics.gmptGlyphOrigin.y + Integer(GlyphMetrics.gmBlackBoxY);

      { wskazniki na BufferEnd lub dalej wskazuja ZA Bufferem }
      BufferEnd := PtrUInt(Buffer)+GlyphDataSize;

      { calculate ResultItems. Only "Count" fields are left not initialized. }
      PolHeader := Buffer; { pierwszy PolHeader }
      while PtrUInt(PolHeader) < BufferEnd do
      begin
        { czytaj PolHeader }
        ResultItemsAdd(pkNewPolygon);
        PolygonEnd := PtrUInt(PointerAdd(PolHeader, PolHeader^.cb));
        lastPunkt.x := ToFloat(PolHeader^.pfxStart.x);
        lastPunkt.y := ToFloat(PolHeader^.pfxStart.y);

        { czytaj PolCurves }
        PolCurve := PointerAdd(PolHeader, SizeOf(TTPolygonHeader)); { pierwszy PolCurve }
        while PtrUInt(PolCurve) < PolygonEnd do
        begin
          case PolCurve^.wType of
            TT_PRIM_LINE : ResultItemsAdd(pkLines);
            TT_PRIM_QSPLINE : ResultItemsAdd(pkBezier);
            else raise Exception.Create('UNKNOWN PolCurve^.wType !!!!');
          end;
          ResultItemsAdd(lastPunkt.x, lastPunkt.y);
          PointsFX := @PolCurve^.apfx[0];
          for i := 0 to PolCurve^.cpfx-1 do
          begin
            lastPunkt.x := ToFloat(PointsFX^[i].x);
            lastPunkt.y := ToFloat(PointsFX^[i].y);
            ResultItemsAdd(lastPunkt.x, lastPunkt.y);
          end;
          { nastepny PolCurve: }
          PolCurve := PointerAdd(PolCurve, SizeOf(TTPolyCurve) +
            (PolCurve^.cpfx-1)*SizeOf(TPointFX));
        end;

        { zakoncz ten polygon }
        with PolHeader^.pfxStart do ResultItemsAdd(ToFloat(x), ToFloat(y));

        { nastepny PolHeader: }
        PolHeader := Pointer(PolCurve);
      end;
    finally FreeMemNiling(Buffer) end;

    { calculate "Count" fields for items with Kind <> pkPoint in ResultItems }
    for i := 0 to ResultItems.Count - 1 do
      case ResultItems.L[i].Kind of
        pkNewPolygon:
          begin
            dlug := 0;
            for j := i+1 to ResultItems.Count - 1 do
              case ResultItems.L[j].Kind of
                pkLines, pkBezier : Inc(dlug);
                pkNewPolygon : break;
              end;
            ResultItems.L[i].Count := dlug;
          end;
        pkLines, pkBezier:
          begin
            dlug := 0;
            for j := i+1 to ResultItems.Count - 1 do
              if ResultItems.L[j].Kind = pkPoint then Inc(dlug) else break;
            ResultItems.L[i].Count := dlug;
          end;
      end;

    { calculate Result^.Info.PolygonsCount/ItemsCount }
    ResultInfo.ItemsCount := ResultItems.Count;
    ResultInfo.PolygonsCount := 0;
    for i := 0 to ResultItems.Count - 1 do
      if ResultItems.L[i].Kind = pkNewPolygon then Inc(ResultInfo.PolygonsCount);

    { get mem for Result and fill Result^ with calculated data }
    Result := GetMem(SizeOf(TOutlineCharInfo) +
      ResultInfo.ItemsCount*SizeOf(TOutlineCharItem));
    try
      Result^.Info := ResultInfo;
      Move(ResultItems.L[0], Result^.Items,
        ResultInfo.ItemsCount*SizeOf(TOutlineCharItem));
    except FreeMem(Result); raise end;

  finally ResultItems.Free end;
end;

{ Font2XxxFont_HDc ----------------------------------------------- }

function Font2BitmapFont_HDc(dc: HDC): TBitmapFont;
var
  c: char;
begin
  Result := TBitmapFont.Create;
  try
    for c := Low(char) to High(char) do
      Result.Data[c] := Font2BitmapChar_HDc(dc, c);
  except FreeAndNilFont(Result); raise end;
end;

function Font2OutlineFont_HDc(dc: HDC): TOutlineFont;
var
  c: char;
begin
  Result := TOutlineFont.Create;
  try
    for c := Low(char) to High(char) do
      Result.Data[c] := Font2OutlineChar_HDc(dc, c);
  except FreeAndNilFont(Result); raise end;
end;

{ versions without _HDc ------------------------------------------- }

function Font2BitmapFont(WinFont: HFont): TBitmapFont;
var
  dc: HDc;
  PreviousObject: HGdiObj;
begin
  dc := GetDC(0);
  Check(dc <> 0, 'GetDC(0) failed');
  try
    PreviousObject := SelectObject(dc, WinFont);
    try
      Result := Font2BitmapFont_HDc(dc);
    finally SelectObject(dc, PreviousObject) end;
  finally ReleaseDC(0, dc) end;
end;

function Font2OutlineFont(WinFont: HFont): TOutlineFont;
var
  dc: HDc;
  PreviousObject: HGdiObj;
begin
  dc := GetDC(0);
  Check(dc <> 0, 'GetDC(0) failed');
  try
    PreviousObject := SelectObject(dc, WinFont);
    try
      Result := Font2OutlineFont_HDc(dc);
    finally SelectObject(dc, PreviousObject) end;
  finally ReleaseDC(0, dc) end;
end;

{ FreeAndNilFont ------------------------------------------------------------- }

procedure FreeAndNilFont(var Font: TBitmapFont);
var
  c: char;
begin
  for c := Low(char) to High(char) do FreeMem(Pointer(Font.Data[c]));
  FreeAndNil(Font);
end;

procedure FreeAndNilFont(var Font: TOutlineFont);
var
  c: char;
begin
  for c := Low(char) to High(char) do FreeMem(Pointer(Font.Data[c]));
  FreeAndNil(Font);
end;

end.