sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
ftglyph.h
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * ftglyph.h
4 *
5 * FreeType convenience functions to handle glyphs (specification).
6 *
7 * Copyright (C) 1996-2025 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
9 *
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
15 *
16 */
17
18
19 /**************************************************************************
20 *
21 * This file contains the definition of several convenience functions that
22 * can be used by client applications to easily retrieve glyph bitmaps and
23 * outlines from a given face.
24 *
25 * These functions should be optional if you are writing a font server or
26 * text layout engine on top of FreeType. However, they are pretty handy
27 * for many other simple uses of the library.
28 *
29 */
30
31
36#ifndef FTGLYPH_H_
37#define FTGLYPH_H_
38
39
40#include <freetype/freetype.h>
41
42#ifdef FREETYPE_H
43#error "freetype.h of FreeType 1 has been loaded!"
44#error "Please fix the directory search order for header files"
45#error "so that freetype.h of FreeType 2 is found first."
46#endif
47
48
49FT_BEGIN_HEADER
50
51
52 /**************************************************************************
53 *
54 * @section:
55 * glyph_management
56 *
57 * @title:
58 * Glyph Management
59 *
60 * @abstract:
61 * Generic interface to manage individual glyph data.
62 *
63 * @description:
64 * This section contains definitions used to manage glyph data through
65 * generic @FT_Glyph objects. Each of them can contain a bitmap,
66 * a vector outline, or even images in other formats. These objects are
67 * detached from @FT_Face, contrary to @FT_GlyphSlot.
68 *
69 */
70
71
72 /* forward declaration to a private type */
73 typedef struct FT_Glyph_Class_ FT_Glyph_Class;
74
75
76 /**************************************************************************
77 *
78 * @type:
79 * FT_Glyph
80 *
81 * @description:
82 * Handle to an object used to model generic glyph images. It is a
83 * pointer to the @FT_GlyphRec structure and can contain a glyph bitmap
84 * or pointer.
85 *
86 * @note:
87 * Glyph objects are not owned by the library. You must thus release
88 * them manually (through @FT_Done_Glyph) _before_ calling
89 * @FT_Done_FreeType.
90 */
91 typedef struct FT_GlyphRec_* FT_Glyph;
92
93
94 /**************************************************************************
95 *
96 * @struct:
97 * FT_GlyphRec
98 *
99 * @description:
100 * The root glyph structure contains a given glyph image plus its advance
101 * width in 16.16 fixed-point format.
102 *
103 * @fields:
104 * library ::
105 * A handle to the FreeType library object.
106 *
107 * clazz ::
108 * A pointer to the glyph's class. Private.
109 *
110 * format ::
111 * The format of the glyph's image.
112 *
113 * advance ::
114 * A 16.16 vector that gives the glyph's advance width.
115 */
116 typedef struct FT_GlyphRec_
117 {
118 FT_Library library;
119 const FT_Glyph_Class* clazz;
120 FT_Glyph_Format format;
121 FT_Vector advance;
122
123 } FT_GlyphRec;
124
125
126 /**************************************************************************
127 *
128 * @type:
129 * FT_BitmapGlyph
130 *
131 * @description:
132 * A handle to an object used to model a bitmap glyph image. This is a
133 * 'sub-class' of @FT_Glyph, and a pointer to @FT_BitmapGlyphRec.
134 */
135 typedef struct FT_BitmapGlyphRec_* FT_BitmapGlyph;
136
137
138 /**************************************************************************
139 *
140 * @struct:
141 * FT_BitmapGlyphRec
142 *
143 * @description:
144 * A structure used for bitmap glyph images. This really is a
145 * 'sub-class' of @FT_GlyphRec.
146 *
147 * @fields:
148 * root ::
149 * The root fields of @FT_Glyph.
150 *
151 * left ::
152 * The left-side bearing, i.e., the horizontal distance from the
153 * current pen position to the left border of the glyph bitmap.
154 *
155 * top ::
156 * The top-side bearing, i.e., the vertical distance from the current
157 * pen position to the top border of the glyph bitmap. This distance
158 * is positive for upwards~y!
159 *
160 * bitmap ::
161 * A descriptor for the bitmap.
162 *
163 * @note:
164 * You can typecast an @FT_Glyph to @FT_BitmapGlyph if you have
165 * `glyph->format == FT_GLYPH_FORMAT_BITMAP`. This lets you access the
166 * bitmap's contents easily.
167 *
168 * The corresponding pixel buffer is always owned by @FT_BitmapGlyph and
169 * is thus created and destroyed with it.
170 */
171 typedef struct FT_BitmapGlyphRec_
172 {
173 FT_GlyphRec root;
174 FT_Int left;
175 FT_Int top;
176 FT_Bitmap bitmap;
177
178 } FT_BitmapGlyphRec;
179
180
181 /**************************************************************************
182 *
183 * @type:
184 * FT_OutlineGlyph
185 *
186 * @description:
187 * A handle to an object used to model an outline glyph image. This is a
188 * 'sub-class' of @FT_Glyph, and a pointer to @FT_OutlineGlyphRec.
189 */
190 typedef struct FT_OutlineGlyphRec_* FT_OutlineGlyph;
191
192
193 /**************************************************************************
194 *
195 * @struct:
196 * FT_OutlineGlyphRec
197 *
198 * @description:
199 * A structure used for outline (vectorial) glyph images. This really is
200 * a 'sub-class' of @FT_GlyphRec.
201 *
202 * @fields:
203 * root ::
204 * The root @FT_Glyph fields.
205 *
206 * outline ::
207 * A descriptor for the outline.
208 *
209 * @note:
210 * You can typecast an @FT_Glyph to @FT_OutlineGlyph if you have
211 * `glyph->format == FT_GLYPH_FORMAT_OUTLINE`. This lets you access the
212 * outline's content easily.
213 *
214 * As the outline is extracted from a glyph slot, its coordinates are
215 * expressed normally in 26.6 pixels, unless the flag @FT_LOAD_NO_SCALE
216 * was used in @FT_Load_Glyph or @FT_Load_Char.
217 *
218 * The outline's tables are always owned by the object and are destroyed
219 * with it.
220 */
221 typedef struct FT_OutlineGlyphRec_
222 {
223 FT_GlyphRec root;
224 FT_Outline outline;
225
226 } FT_OutlineGlyphRec;
227
228
229 /**************************************************************************
230 *
231 * @type:
232 * FT_SvgGlyph
233 *
234 * @description:
235 * A handle to an object used to model an SVG glyph. This is a
236 * 'sub-class' of @FT_Glyph, and a pointer to @FT_SvgGlyphRec.
237 *
238 * @since:
239 * 2.12
240 */
241 typedef struct FT_SvgGlyphRec_* FT_SvgGlyph;
242
243
244 /**************************************************************************
245 *
246 * @struct:
247 * FT_SvgGlyphRec
248 *
249 * @description:
250 * A structure used for OT-SVG glyphs. This is a 'sub-class' of
251 * @FT_GlyphRec.
252 *
253 * @fields:
254 * root ::
255 * The root @FT_GlyphRec fields.
256 *
257 * svg_document ::
258 * A pointer to the SVG document.
259 *
260 * svg_document_length ::
261 * The length of `svg_document`.
262 *
263 * glyph_index ::
264 * The index of the glyph to be rendered.
265 *
266 * metrics ::
267 * A metrics object storing the size information.
268 *
269 * units_per_EM ::
270 * The size of the EM square.
271 *
272 * start_glyph_id ::
273 * The first glyph ID in the glyph range covered by this document.
274 *
275 * end_glyph_id ::
276 * The last glyph ID in the glyph range covered by this document.
277 *
278 * transform ::
279 * A 2x2 transformation matrix to apply to the glyph while rendering
280 * it.
281 *
282 * delta ::
283 * Translation to apply to the glyph while rendering.
284 *
285 * @note:
286 * The Glyph Management API requires @FT_Glyph or its 'sub-class' to have
287 * all the information needed to completely define the glyph's rendering.
288 * Outline-based glyphs can directly apply transformations to the outline
289 * but this is not possible for an SVG document that hasn't been parsed.
290 * Therefore, the transformation is stored along with the document. In
291 * the absence of a 'ViewBox' or 'Width'/'Height' attribute, the size of
292 * the ViewPort should be assumed to be 'units_per_EM'.
293 */
294 typedef struct FT_SvgGlyphRec_
295 {
296 FT_GlyphRec root;
297
298 FT_Byte* svg_document;
299 FT_ULong svg_document_length;
300
301 FT_UInt glyph_index;
302
303 FT_Size_Metrics metrics;
304 FT_UShort units_per_EM;
305
306 FT_UShort start_glyph_id;
307 FT_UShort end_glyph_id;
308
309 FT_Matrix transform;
310 FT_Vector delta;
311
312 } FT_SvgGlyphRec;
313
314
315 /**************************************************************************
316 *
317 * @function:
318 * FT_New_Glyph
319 *
320 * @description:
321 * A function used to create a new empty glyph image. Note that the
322 * created @FT_Glyph object must be released with @FT_Done_Glyph.
323 *
324 * @input:
325 * library ::
326 * A handle to the FreeType library object.
327 *
328 * format ::
329 * The format of the glyph's image.
330 *
331 * @output:
332 * aglyph ::
333 * A handle to the glyph object.
334 *
335 * @return:
336 * FreeType error code. 0~means success.
337 *
338 * @since:
339 * 2.10
340 */
341 FT_EXPORT( FT_Error )
342 FT_New_Glyph( FT_Library library,
343 FT_Glyph_Format format,
344 FT_Glyph *aglyph );
345
346
347 /**************************************************************************
348 *
349 * @function:
350 * FT_Get_Glyph
351 *
352 * @description:
353 * A function used to extract a glyph image from a slot. Note that the
354 * created @FT_Glyph object must be released with @FT_Done_Glyph.
355 *
356 * @input:
357 * slot ::
358 * A handle to the source glyph slot.
359 *
360 * @output:
361 * aglyph ::
362 * A handle to the glyph object. `NULL` in case of error.
363 *
364 * @return:
365 * FreeType error code. 0~means success.
366 *
367 * @note:
368 * Because `*aglyph->advance.x` and `*aglyph->advance.y` are 16.16
369 * fixed-point numbers, `slot->advance.x` and `slot->advance.y` (which
370 * are in 26.6 fixed-point format) must be in the range ]-32768;32768[.
371 */
372 FT_EXPORT( FT_Error )
373 FT_Get_Glyph( FT_GlyphSlot slot,
374 FT_Glyph *aglyph );
375
376
377 /**************************************************************************
378 *
379 * @function:
380 * FT_Glyph_Copy
381 *
382 * @description:
383 * A function used to copy a glyph image. Note that the created
384 * @FT_Glyph object must be released with @FT_Done_Glyph.
385 *
386 * @input:
387 * source ::
388 * A handle to the source glyph object.
389 *
390 * @output:
391 * target ::
392 * A handle to the target glyph object. `NULL` in case of error.
393 *
394 * @return:
395 * FreeType error code. 0~means success.
396 */
397 FT_EXPORT( FT_Error )
398 FT_Glyph_Copy( FT_Glyph source,
399 FT_Glyph *target );
400
401
402 /**************************************************************************
403 *
404 * @function:
405 * FT_Glyph_Transform
406 *
407 * @description:
408 * Transform a glyph image if its format is scalable.
409 *
410 * @inout:
411 * glyph ::
412 * A handle to the target glyph object.
413 *
414 * @input:
415 * matrix ::
416 * A pointer to a 2x2 matrix to apply.
417 *
418 * delta ::
419 * A pointer to a 2d vector to apply. Coordinates are expressed in
420 * 1/64 of a pixel.
421 *
422 * @return:
423 * FreeType error code (if not 0, the glyph format is not scalable).
424 *
425 * @note:
426 * The 2x2 transformation matrix is also applied to the glyph's advance
427 * vector.
428 */
429 FT_EXPORT( FT_Error )
430 FT_Glyph_Transform( FT_Glyph glyph,
431 const FT_Matrix* matrix,
432 const FT_Vector* delta );
433
434
435 /**************************************************************************
436 *
437 * @enum:
438 * FT_Glyph_BBox_Mode
439 *
440 * @description:
441 * The mode how the values of @FT_Glyph_Get_CBox are returned.
442 *
443 * @values:
444 * FT_GLYPH_BBOX_UNSCALED ::
445 * Return unscaled font units.
446 *
447 * FT_GLYPH_BBOX_SUBPIXELS ::
448 * Return unfitted 26.6 coordinates.
449 *
450 * FT_GLYPH_BBOX_GRIDFIT ::
451 * Return grid-fitted 26.6 coordinates.
452 *
453 * FT_GLYPH_BBOX_TRUNCATE ::
454 * Return coordinates in integer pixels.
455 *
456 * FT_GLYPH_BBOX_PIXELS ::
457 * Return grid-fitted pixel coordinates.
458 */
459 typedef enum FT_Glyph_BBox_Mode_
460 {
461 FT_GLYPH_BBOX_UNSCALED = 0,
462 FT_GLYPH_BBOX_SUBPIXELS = 0,
463 FT_GLYPH_BBOX_GRIDFIT = 1,
464 FT_GLYPH_BBOX_TRUNCATE = 2,
465 FT_GLYPH_BBOX_PIXELS = 3
466
467 } FT_Glyph_BBox_Mode;
468
469
470 /* these constants are deprecated; use the corresponding */
471 /* `FT_Glyph_BBox_Mode` values instead */
472#define ft_glyph_bbox_unscaled FT_GLYPH_BBOX_UNSCALED
473#define ft_glyph_bbox_subpixels FT_GLYPH_BBOX_SUBPIXELS
474#define ft_glyph_bbox_gridfit FT_GLYPH_BBOX_GRIDFIT
475#define ft_glyph_bbox_truncate FT_GLYPH_BBOX_TRUNCATE
476#define ft_glyph_bbox_pixels FT_GLYPH_BBOX_PIXELS
477
478
479 /**************************************************************************
480 *
481 * @function:
482 * FT_Glyph_Get_CBox
483 *
484 * @description:
485 * Return a glyph's 'control box'. The control box encloses all the
486 * outline's points, including Bezier control points. Though it
487 * coincides with the exact bounding box for most glyphs, it can be
488 * slightly larger in some situations (like when rotating an outline that
489 * contains Bezier outside arcs).
490 *
491 * Computing the control box is very fast, while getting the bounding box
492 * can take much more time as it needs to walk over all segments and arcs
493 * in the outline. To get the latter, you can use the 'ftbbox'
494 * component, which is dedicated to this single task.
495 *
496 * @input:
497 * glyph ::
498 * A handle to the source glyph object.
499 *
500 * mode ::
501 * The mode that indicates how to interpret the returned bounding box
502 * values.
503 *
504 * @output:
505 * acbox ::
506 * The glyph coordinate bounding box. Coordinates are expressed in
507 * 1/64 of pixels if it is grid-fitted.
508 *
509 * @note:
510 * Coordinates are relative to the glyph origin, using the y~upwards
511 * convention.
512 *
513 * If the glyph has been loaded with @FT_LOAD_NO_SCALE, `bbox_mode` must
514 * be set to @FT_GLYPH_BBOX_UNSCALED to get unscaled font units in 26.6
515 * pixel format. The value @FT_GLYPH_BBOX_SUBPIXELS is another name for
516 * this constant.
517 *
518 * If the font is tricky and the glyph has been loaded with
519 * @FT_LOAD_NO_SCALE, the resulting CBox is meaningless. To get
520 * reasonable values for the CBox it is necessary to load the glyph at a
521 * large ppem value (so that the hinting instructions can properly shift
522 * and scale the subglyphs), then extracting the CBox, which can be
523 * eventually converted back to font units.
524 *
525 * Note that the maximum coordinates are exclusive, which means that one
526 * can compute the width and height of the glyph image (be it in integer
527 * or 26.6 pixels) as:
528 *
529 * ```
530 * width = bbox.xMax - bbox.xMin;
531 * height = bbox.yMax - bbox.yMin;
532 * ```
533 *
534 * Note also that for 26.6 coordinates, if `bbox_mode` is set to
535 * @FT_GLYPH_BBOX_GRIDFIT, the coordinates will also be grid-fitted,
536 * which corresponds to:
537 *
538 * ```
539 * bbox.xMin = FLOOR(bbox.xMin);
540 * bbox.yMin = FLOOR(bbox.yMin);
541 * bbox.xMax = CEILING(bbox.xMax);
542 * bbox.yMax = CEILING(bbox.yMax);
543 * ```
544 *
545 * To get the bbox in pixel coordinates, set `bbox_mode` to
546 * @FT_GLYPH_BBOX_TRUNCATE.
547 *
548 * To get the bbox in grid-fitted pixel coordinates, set `bbox_mode` to
549 * @FT_GLYPH_BBOX_PIXELS.
550 */
551 FT_EXPORT( void )
552 FT_Glyph_Get_CBox( FT_Glyph glyph,
553 FT_UInt bbox_mode,
554 FT_BBox *acbox );
555
556
557 /**************************************************************************
558 *
559 * @function:
560 * FT_Glyph_To_Bitmap
561 *
562 * @description:
563 * Convert a given glyph object to a bitmap glyph object.
564 *
565 * @inout:
566 * the_glyph ::
567 * A pointer to a handle to the target glyph.
568 *
569 * @input:
570 * render_mode ::
571 * An enumeration that describes how the data is rendered.
572 *
573 * origin ::
574 * A pointer to a vector used to translate the glyph image before
575 * rendering. Can be~0 (if no translation). The origin is expressed
576 * in 26.6 pixels.
577 *
578 * destroy ::
579 * A boolean that indicates that the original glyph image should be
580 * destroyed by this function. It is never destroyed in case of error.
581 *
582 * @return:
583 * FreeType error code. 0~means success.
584 *
585 * @note:
586 * This function does nothing if the glyph format isn't scalable.
587 *
588 * The glyph image is translated with the `origin` vector before
589 * rendering.
590 *
591 * The first parameter is a pointer to an @FT_Glyph handle that will be
592 * _replaced_ by this function (with newly allocated data). Typically,
593 * you would do something like the following (omitting error handling).
594 *
595 * ```
596 * FT_Glyph glyph;
597 * FT_BitmapGlyph glyph_bitmap;
598 *
599 *
600 * // load glyph
601 * error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAULT );
602 *
603 * // extract glyph image
604 * error = FT_Get_Glyph( face->glyph, &glyph );
605 *
606 * // convert to a bitmap (default render mode + destroying old)
607 * if ( glyph->format != FT_GLYPH_FORMAT_BITMAP )
608 * {
609 * error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL,
610 * 0, 1 );
611 * if ( error ) // `glyph' unchanged
612 * ...
613 * }
614 *
615 * // access bitmap content by typecasting
616 * glyph_bitmap = (FT_BitmapGlyph)glyph;
617 *
618 * // do funny stuff with it, like blitting/drawing
619 * ...
620 *
621 * // discard glyph image (bitmap or not)
622 * FT_Done_Glyph( glyph );
623 * ```
624 *
625 * Here is another example, again without error handling.
626 *
627 * ```
628 * FT_Glyph glyphs[MAX_GLYPHS]
629 *
630 *
631 * ...
632 *
633 * for ( idx = 0; i < MAX_GLYPHS; i++ )
634 * error = FT_Load_Glyph( face, idx, FT_LOAD_DEFAULT ) ||
635 * FT_Get_Glyph ( face->glyph, &glyphs[idx] );
636 *
637 * ...
638 *
639 * for ( idx = 0; i < MAX_GLYPHS; i++ )
640 * {
641 * FT_Glyph bitmap = glyphs[idx];
642 *
643 *
644 * ...
645 *
646 * // after this call, `bitmap' no longer points into
647 * // the `glyphs' array (and the old value isn't destroyed)
648 * FT_Glyph_To_Bitmap( &bitmap, FT_RENDER_MODE_MONO, 0, 0 );
649 *
650 * ...
651 *
652 * FT_Done_Glyph( bitmap );
653 * }
654 *
655 * ...
656 *
657 * for ( idx = 0; i < MAX_GLYPHS; i++ )
658 * FT_Done_Glyph( glyphs[idx] );
659 * ```
660 */
661 FT_EXPORT( FT_Error )
662 FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
663 FT_Render_Mode render_mode,
664 const FT_Vector* origin,
665 FT_Bool destroy );
666
667
668 /**************************************************************************
669 *
670 * @function:
671 * FT_Done_Glyph
672 *
673 * @description:
674 * Destroy a given glyph.
675 *
676 * @input:
677 * glyph ::
678 * A handle to the target glyph object. Can be `NULL`.
679 */
680 FT_EXPORT( void )
681 FT_Done_Glyph( FT_Glyph glyph );
682
683 /* */
684
685
686 /* other helpful functions */
687
688 /**************************************************************************
689 *
690 * @section:
691 * computations
692 *
693 */
694
695
696 /**************************************************************************
697 *
698 * @function:
699 * FT_Matrix_Multiply
700 *
701 * @description:
702 * Perform the matrix operation `b = a*b`.
703 *
704 * @input:
705 * a ::
706 * A pointer to matrix `a`.
707 *
708 * @inout:
709 * b ::
710 * A pointer to matrix `b`.
711 *
712 * @note:
713 * The result is undefined if either `a` or `b` is zero.
714 *
715 * Since the function uses wrap-around arithmetic, results become
716 * meaningless if the arguments are very large.
717 */
718 FT_EXPORT( void )
719 FT_Matrix_Multiply( const FT_Matrix* a,
720 FT_Matrix* b );
721
722
723 /**************************************************************************
724 *
725 * @function:
726 * FT_Matrix_Invert
727 *
728 * @description:
729 * Invert a 2x2 matrix. Return an error if it can't be inverted.
730 *
731 * @inout:
732 * matrix ::
733 * A pointer to the target matrix. Remains untouched in case of error.
734 *
735 * @return:
736 * FreeType error code. 0~means success.
737 */
738 FT_EXPORT( FT_Error )
739 FT_Matrix_Invert( FT_Matrix* matrix );
740
741 /* */
742
743
744FT_END_HEADER
745
746#endif /* FTGLYPH_H_ */
747
748
749/* END */
750
751
752/* Local Variables: */
753/* coding: utf-8 */
754/* End: */