aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/freetype/ftglyph.inc
blob: 0d4acc99556a3cda2ae404c3b01c9a26618332cf (plain) (blame)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
(***************************************************************************)
(*                                                                         *)
(*  ftglyph.h                                                              *)
(*                                                                         *)
(*    FreeType convenience functions to handle glyphs (specification).     *)
(*                                                                         *)
(*  Copyright 1996-2001, 2002, 2003, 2006 by                               *)
(*  David Turner, Robert Wilhelm, and Werner Lemberg.                      *)
(*                                                                         *)
(*  This file is part of the FreeType project, and may only be used,       *)
(*  modified, and distributed under the terms of the FreeType project      *)
(*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     *)
(*  this file you indicate that you have read the license and              *)
(*  understand and accept it fully.                                        *)
(*                                                                         *)
(***************************************************************************)
(***************************************************************************)
(* Pascal port by the UltraStar Deluxe Team                                *)
(***************************************************************************)


  (*************************************************************************)
  (*                                                                       *)
  (* This file contains the definition of several convenience functions    *)
  (* that can be used by client applications to easily retrieve glyph      *)
  (* bitmaps and outlines from a given face.                               *)
  (*                                                                       *)
  (* These functions should be optional if you are writing a font server   *)
  (* or text layout engine on top of FreeType.  However, they are pretty   *)
  (* handy for many other simple uses of the library.                      *)
  (*                                                                       *)
  (*************************************************************************)

  (*************************************************************************)
  (*                                                                       *)
  (* <Section>                                                             *)
  (*    glyph_management                                                   *)
  (*                                                                       *)
  (* <Title>                                                               *)
  (*    Glyph Management                                                   *)
  (*                                                                       *)
  (* <Abstract>                                                            *)
  (*    Generic interface to manage individual glyph data.                 *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    This section contains definitions used to manage glyph data        *)
  (*    through generic FT_Glyph objects.  Each of them can contain a      *)
  (*    bitmap, a vector outline, or even images in other formats.         *)
  (*                                                                       *)
  (*************************************************************************)

{$IFDEF TYPE_DECL}
  
  (* forward declaration to a private type *)
  PFT_Glyph_Class = Pointer;

  
  (*************************************************************************)
  (*                                                                       *)
  (* <Type>                                                                *)
  (*    FT_Glyph                                                           *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    Handle to an object used to model generic glyph images.  It is a   *)
  (*    pointer to the @FT_GlyphRec structure and can contain a glyph      *)
  (*    bitmap or pointer.                                                 *)
  (*                                                                       *)
  (* <Note>                                                                *)
  (*    Glyph objects are not owned by the library.  You must thus release *)
  (*    them manually (through @FT_Done_Glyph) _before_ calling            *)
  (*    @FT_Done_FreeType.                                                 *)
  (*                                                                       *)
  FT_Glyph = ^FT_GlyphRec;
  
  (*************************************************************************)
  (*                                                                       *)
  (* <Struct>                                                              *)
  (*    FT_GlyphRec                                                        *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    The root glyph structure contains a given glyph image plus its     *)
  (*    advance width in 16.16 fixed float format.                         *)
  (*                                                                       *)
  (* <Fields>                                                              *)
  (*    library :: A handle to the FreeType library object.                *)
  (*                                                                       *)
  (*    clazz   :: A pointer to the glyph's class.  Private.               *)
  (*                                                                       *)
  (*    format  :: The format of the glyph's image.                        *)
  (*                                                                       *)
  (*    advance :: A 16.16 vector that gives the glyph's advance width.    *)
  (*                                                                       *)
  FT_GlyphRec = record
    library_: FT_Library;
    clazz:    PFT_Glyph_Class;
    format:   FT_Glyph_Format;
    advance:  FT_Vector;
  end;


  (*************************************************************************)
  (*                                                                       *)
  (* <Type>                                                                *)
  (*    FT_BitmapGlyph                                                     *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    A handle to an object used to model a bitmap glyph image.  This is *)
  (*    a sub-class of @FT_Glyph, and a pointer to @FT_BitmapGlyphRec.     *)
  (*                                                                       *)
  FT_BitmapGlyph = ^FT_BitmapGlyphRec;

  (*************************************************************************)
  (*                                                                       *)
  (* <Struct>                                                              *)
  (*    FT_BitmapGlyphRec                                                  *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    A structure used for bitmap glyph images.  This really is a        *)
  (*    `sub-class' of `FT_GlyphRec'.                                      *)
  (*                                                                       *)
  (* <Fields>                                                              *)
  (*    root   :: The root FT_Glyph fields.                                *)
  (*                                                                       *)
  (*    left   :: The left-side bearing, i.e., the horizontal distance     *)
  (*              from the current pen position to the left border of the  *)
  (*              glyph bitmap.                                            *)
  (*                                                                       *)
  (*    top    :: The top-side bearing, i.e., the vertical distance from   *)
  (*              the current pen position to the top border of the glyph  *)
  (*              bitmap.  This distance is positive for upwards-y!        *)
  (*                                                                       *)
  (*    bitmap :: A descriptor for the bitmap.                             *)
  (*                                                                       *)
  (* <Note>                                                                *)
  (*    You can typecast FT_Glyph to FT_BitmapGlyph if you have            *)
  (*    glyph->format == FT_GLYPH_FORMAT_BITMAP.  This lets you access     *)
  (*    the bitmap's contents easily.                                      *)
  (*                                                                       *)
  (*    The corresponding pixel buffer is always owned by the BitmapGlyph  *)
  (*    and is thus created and destroyed with it.                         *)
  (*                                                                       *)
  FT_BitmapGlyphRec = record
    root:   FT_GlyphRec;
    left:   FT_Int;
    top:    FT_Int;
    bitmap: FT_Bitmap;
  end;


  (*************************************************************************)
  (*                                                                       *)
  (* <Type>                                                                *)
  (*    FT_OutlineGlyph                                                    *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    A handle to an object used to model an outline glyph image.  This  *)
  (*    is a sub-class of @FT_Glyph, and a pointer to @FT_OutlineGlyphRec. *)
  (*                                                                       *)
  FT_OutlineGlyph = ^FT_OutlineGlyphRec;


  (*************************************************************************)
  (*                                                                       *)
  (* <Struct>                                                              *)
  (*    FT_OutlineGlyphRec                                                 *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    A structure used for outline (vectorial) glyph images.  This       *)
  (*    really is a `sub-class' of `FT_GlyphRec'.                          *)
  (*                                                                       *)
  (* <Fields>                                                              *)
  (*    root    :: The root FT_Glyph fields.                               *)
  (*                                                                       *)
  (*    outline :: A descriptor for the outline.                           *)
  (*                                                                       *)
  (* <Note>                                                                *)
  (*    You can typecast FT_Glyph to FT_OutlineGlyph if you have           *)
  (*    glyph->format == FT_GLYPH_FORMAT_OUTLINE.  This lets you access    *)
  (*    the outline's content easily.                                      *)
  (*                                                                       *)
  (*    As the outline is extracted from a glyph slot, its coordinates are *)
  (*    expressed normally in 26.6 pixels, unless the flag                 *)
  (*    FT_LOAD_NO_SCALE was used in FT_Load_Glyph() or FT_Load_Char().    *)
  (*                                                                       *)
  (*    The outline's tables are always owned by the object and are        *)
  (*    destroyed with it.                                                 *)
  (*                                                                       *)
  FT_OutlineGlyphRec = record
    root:    FT_GlyphRec;
    outline: FT_Outline;
  end;

{$ELSE TYPE_DECL}

  (*************************************************************************)
  (*                                                                       *)
  (* <Function>                                                            *)
  (*    FT_Get_Glyph                                                       *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    A function used to extract a glyph image from a slot.              *)
  (*                                                                       *)
  (* <Input>                                                               *)
  (*    slot   :: A handle to the source glyph slot.                       *)
  (*                                                                       *)
  (* <Output>                                                              *)
  (*    aglyph :: A handle to the glyph object.                            *)
  (*                                                                       *)
  (* <Return>                                                              *)
  (*    FreeType error code.  0 means success.                             *)
  (*                                                                       *)
  function FT_Get_Glyph(
             slot:   FT_GlyphSlot;
             out aglyph: FT_Glyph ): FT_Error;
    cdecl; external ft_lib name 'FT_Get_Glyph';

  (*************************************************************************)
  (*                                                                       *)
  (* <Function>                                                            *)
  (*    FT_Glyph_Copy                                                      *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    A function used to copy a glyph image.  Note that the created      *)
  (*    @FT_Glyph object must be released with @FT_Done_Glyph.             *)
  (*                                                                       *)
  (* <Input>                                                               *)
  (*    source :: A handle to the source glyph object.                     *)
  (*                                                                       *)
  (* <Output>                                                              *)
  (*    target :: A handle to the target glyph object.  0~in case of       *)
  (*              error.                                                   *)
  (*                                                                       *)
  (* <Return>                                                              *)
  (*    FreeType error code.  0~means success.                             *)
  (*                                                                       *)
  function FT_Glyph_Copy(source: FT_Glyph;
                 var target: FT_Glyph ): FT_Error;
    cdecl; external ft_lib name 'FT_Glyph_Copy';

{$ENDIF TYPE_DECL}
{$IFDEF TYPE_DECL}

  (*************************************************************************)
  (*                                                                       *)
  (* <Enum>                                                                *)
  (*    FT_Glyph_BBox_Mode                                                 *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    The mode how the values of @FT_Glyph_Get_CBox are returned.        *)
  (*                                                                       *)
  (* <Values>                                                              *)
  (*    FT_GLYPH_BBOX_UNSCALED ::                                          *)
  (*      Return unscaled font units.                                      *)
  (*                                                                       *)
  (*    FT_GLYPH_BBOX_SUBPIXELS ::                                         *)
  (*      Return unfitted 26.6 coordinates.                                *)
  (*                                                                       *)
  (*    FT_GLYPH_BBOX_GRIDFIT ::                                           *)
  (*      Return grid-fitted 26.6 coordinates.                             *)
  (*                                                                       *)
  (*    FT_GLYPH_BBOX_TRUNCATE ::                                          *)
  (*      Return coordinates in integer pixels.                            *)
  (*                                                                       *)
  (*    FT_GLYPH_BBOX_PIXELS ::                                            *)
  (*      Return grid-fitted pixel coordinates.                            *)
  (*                                                                       *)
  FT_Glyph_BBox_Mode = cint;
{$ELSE TYPE_DECL}
const
  FT_GLYPH_BBOX_UNSCALED  = 0;
  FT_GLYPH_BBOX_SUBPIXELS = 0;
  FT_GLYPH_BBOX_GRIDFIT   = 1;
  FT_GLYPH_BBOX_TRUNCATE  = 2;
  FT_GLYPH_BBOX_PIXELS    = 3;


  (*************************************************************************)
  (*                                                                       *)
  (* <Function>                                                            *)
  (*    FT_Glyph_Get_CBox                                                  *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    Return a glyph's `control box'.  The control box encloses all the  *)
  (*    outline's points, including Bzier control points.  Though it      *)
  (*    coincides with the exact bounding box for most glyphs, it can be   *)
  (*    slightly larger in some situations (like when rotating an outline  *)
  (*    which contains Bzier outside arcs).                               *)
  (*                                                                       *)
  (*    Computing the control box is very fast, while getting the bounding *)
  (*    box can take much more time as it needs to walk over all segments  *)
  (*    and arcs in the outline.  To get the latter, you can use the       *)
  (*    `ftbbox' component which is dedicated to this single task.         *)
  (*                                                                       *)
  (* <Input>                                                               *)
  (*    glyph :: A handle to the source glyph object.                      *)
  (*                                                                       *)
  (*    mode  :: The mode which indicates how to interpret the returned    *)
  (*             bounding box values.                                      *)
  (*                                                                       *)
  (* <Output>                                                              *)
  (*    acbox :: The glyph coordinate bounding box.  Coordinates are       *)
  (*             expressed in 1/64th of pixels if it is grid-fitted.       *)
  (*                                                                       *)
  (* <Note>                                                                *)
  (*    Coordinates are relative to the glyph origin, using the Y-upwards  *)
  (*    convention.                                                        *)
  (*                                                                       *)
  (*    If the glyph has been loaded with @FT_LOAD_NO_SCALE, `bbox_mode'   *)
  (*    must be set to @FT_GLYPH_BBOX_UNSCALED to get unscaled font        *)
  (*    units in 26.6 pixel format.  The value @FT_GLYPH_BBOX_SUBPIXELS    *)
  (*    is another name for this constant.                                 *)
  (*                                                                       *)
  (*    Note that the maximum coordinates are exclusive, which means that  *)
  (*    one can compute the width and height of the glyph image (be it in  *)
  (*    integer or 26.6 pixels) as:                                        *)
  (*                                                                       *)
  (*    {                                                                  *)
  (*      width  = bbox.xMax - bbox.xMin;                                  *)
  (*      height = bbox.yMax - bbox.yMin;                                  *)
  (*    }                                                                  *)
  (*                                                                       *)
  (*    Note also that for 26.6 coordinates, if `bbox_mode' is set to      *)
  (*    @FT_GLYPH_BBOX_GRIDFIT, the coordinates will also be grid-fitted,  *)
  (*    which corresponds to:                                              *)
  (*                                                                       *)
  (*    {                                                                  *)
  (*      bbox.xMin = FLOOR(bbox.xMin);                                    *)
  (*      bbox.yMin = FLOOR(bbox.yMin);                                    *)
  (*      bbox.xMax = CEILING(bbox.xMax);                                  *)
  (*      bbox.yMax = CEILING(bbox.yMax);                                  *)
  (*    }                                                                  *)
  (*                                                                       *)
  (*    To get the bbox in pixel coordinates, set `bbox_mode' to           *)
  (*    @FT_GLYPH_BBOX_TRUNCATE.                                           *)
  (*                                                                       *)
  (*    To get the bbox in grid-fitted pixel coordinates, set `bbox_mode'  *)
  (*    to @FT_GLYPH_BBOX_PIXELS.                                          *)
  (*                                                                       *)
  procedure FT_Glyph_Get_CBox( glyph: FT_Glyph;
                     bbox_mode: FT_UInt;
                     out acbox: FT_BBox );
    cdecl; external ft_lib name 'FT_Glyph_Get_CBox';


  (*************************************************************************)
  (*                                                                       *)
  (* <Function>                                                            *)
  (*    FT_Glyph_To_Bitmap                                                 *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    Converts a given glyph object to a bitmap glyph object.            *)
  (*                                                                       *)
  (* <InOut>                                                               *)
  (*    the_glyph   :: A pointer to a handle to the target glyph.          *)
  (*                                                                       *)
  (* <Input>                                                               *)
  (*    render_mode :: An enumeration that describe how the data is        *)
  (*                   rendered.                                           *)
  (*                                                                       *)
  (*    origin      :: A pointer to a vector used to translate the glyph   *)
  (*                   image before rendering.  Can be 0 (if no            *)
  (*                   translation).  The origin is expressed in           *)
  (*                   26.6 pixels.                                        *)
  (*                                                                       *)
  (*    destroy     :: A boolean that indicates that the original glyph    *)
  (*                   image should be destroyed by this function.  It is  *)
  (*                   never destroyed in case of error.                   *)
  (*                                                                       *)
  (* <Return>                                                              *)
  (*    FreeType error code.  0 means success.                             *)
  (*                                                                       *)
  (* <Note>                                                                *)
  (*    The glyph image is translated with the `origin' vector before      *)
  (*    rendering.                                                         *)
  (*                                                                       *)
  (*    The first parameter is a pointer to a FT_Glyph handle, that will   *)
  (*    be replaced by this function.  Typically, you would use (omitting  *)
  (*    error handling):                                                   *)
  (*                                                                       *)
  (*                                                                       *)
  (*      {                                                                *)
  (*        FT_Glyph        glyph;                                         *)
  (*        FT_BitmapGlyph  glyph_bitmap;                                  *)
  (*                                                                       *)
  (*                                                                       *)
  (*        // load glyph                                                  *)
  (*        error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAUT );     *)
  (*                                                                       *)
  (*        // extract glyph image                                         *)
  (*        error = FT_Get_Glyph( face->glyph, &glyph );                   *)
  (*                                                                       *)
  (*        // convert to a bitmap (default render mode + destroy old)     *)
  (*        if ( glyph->format != FT_GLYPH_FORMAT_BITMAP )                 *)
  (*        {                                                              *)
  (*          error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_DEFAULT,  *)
  (*                                      0, 1 );                          *)
  (*          if ( error ) // glyph unchanged                              *)
  (*            ...                                                        *)
  (*        }                                                              *)
  (*                                                                       *)
  (*        // access bitmap content by typecasting                        *)
  (*        glyph_bitmap = (FT_BitmapGlyph)glyph;                          *)
  (*                                                                       *)
  (*        // do funny stuff with it, like blitting/drawing               *)
  (*        ...                                                            *)
  (*                                                                       *)
  (*        // discard glyph image (bitmap or not)                         *)
  (*        FT_Done_Glyph( glyph );                                        *)
  (*      }                                                                *)
  (*                                                                       *)
  (*                                                                       *)
  (*    This function does nothing if the glyph format isn't scalable.     *)
  (*                                                                       *)
  function FT_Glyph_To_Bitmap(var the_glyph: FT_Glyph;
                      render_mode: FT_Render_Mode;
                      origin:      PFT_Vector;
                      destroy:     FT_Bool ): FT_Error;
    cdecl; external ft_lib name 'FT_Glyph_To_Bitmap';

    
  (*************************************************************************)
  (*                                                                       *)
  (* <Function>                                                            *)
  (*    FT_Done_Glyph                                                      *)
  (*                                                                       *)
  (* <Description>                                                         *)
  (*    Destroys a given glyph.                                            *)
  (*                                                                       *)
  (* <Input>                                                               *)
  (*    glyph :: A handle to the target glyph object.                      *)
  (*                                                                       *)
  procedure FT_Done_Glyph( glyph: FT_Glyph );
    cdecl; external ft_lib name 'FT_Done_Glyph';

{$ENDIF TYPE_DECL}