1 /****************************************************************************
5 * FreeType convenience functions to handle glyphs (specification).
7 * Copyright (C) 1996-2023 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
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.
19 /**************************************************************************
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.
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.
36 #include <freetype/freetype.h>
39 #error "freetype.h of FreeType 1 has been loaded!"
40 #error "Please fix the directory search order for header files"
41 #error "so that freetype.h of FreeType 2 is found first."
48 /**************************************************************************
57 * Generic interface to manage individual glyph data.
60 * This section contains definitions used to manage glyph data through
61 * generic @FT_Glyph objects. Each of them can contain a bitmap,
62 * a vector outline, or even images in other formats. These objects are
63 * detached from @FT_Face, contrary to @FT_GlyphSlot.
68 /* forward declaration to a private type */
69 typedef struct FT_Glyph_Class_ FT_Glyph_Class;
72 /**************************************************************************
78 * Handle to an object used to model generic glyph images. It is a
79 * pointer to the @FT_GlyphRec structure and can contain a glyph bitmap
83 * Glyph objects are not owned by the library. You must thus release
84 * them manually (through @FT_Done_Glyph) _before_ calling
87 typedef struct FT_GlyphRec_* FT_Glyph;
90 /**************************************************************************
96 * The root glyph structure contains a given glyph image plus its advance
97 * width in 16.16 fixed-point format.
101 * A handle to the FreeType library object.
104 * A pointer to the glyph's class. Private.
107 * The format of the glyph's image.
110 * A 16.16 vector that gives the glyph's advance width.
112 typedef struct FT_GlyphRec_
115 const FT_Glyph_Class* clazz;
116 FT_Glyph_Format format;
122 /**************************************************************************
128 * A handle to an object used to model a bitmap glyph image. This is a
129 * 'sub-class' of @FT_Glyph, and a pointer to @FT_BitmapGlyphRec.
131 typedef struct FT_BitmapGlyphRec_* FT_BitmapGlyph;
134 /**************************************************************************
140 * A structure used for bitmap glyph images. This really is a
141 * 'sub-class' of @FT_GlyphRec.
145 * The root fields of @FT_Glyph.
148 * The left-side bearing, i.e., the horizontal distance from the
149 * current pen position to the left border of the glyph bitmap.
152 * The top-side bearing, i.e., the vertical distance from the current
153 * pen position to the top border of the glyph bitmap. This distance
154 * is positive for upwards~y!
157 * A descriptor for the bitmap.
160 * You can typecast an @FT_Glyph to @FT_BitmapGlyph if you have
161 * `glyph->format == FT_GLYPH_FORMAT_BITMAP`. This lets you access the
162 * bitmap's contents easily.
164 * The corresponding pixel buffer is always owned by @FT_BitmapGlyph and
165 * is thus created and destroyed with it.
167 typedef struct FT_BitmapGlyphRec_
177 /**************************************************************************
183 * A handle to an object used to model an outline glyph image. This is a
184 * 'sub-class' of @FT_Glyph, and a pointer to @FT_OutlineGlyphRec.
186 typedef struct FT_OutlineGlyphRec_* FT_OutlineGlyph;
189 /**************************************************************************
195 * A structure used for outline (vectorial) glyph images. This really is
196 * a 'sub-class' of @FT_GlyphRec.
200 * The root @FT_Glyph fields.
203 * A descriptor for the outline.
206 * You can typecast an @FT_Glyph to @FT_OutlineGlyph if you have
207 * `glyph->format == FT_GLYPH_FORMAT_OUTLINE`. This lets you access the
208 * outline's content easily.
210 * As the outline is extracted from a glyph slot, its coordinates are
211 * expressed normally in 26.6 pixels, unless the flag @FT_LOAD_NO_SCALE
212 * was used in @FT_Load_Glyph or @FT_Load_Char.
214 * The outline's tables are always owned by the object and are destroyed
217 typedef struct FT_OutlineGlyphRec_
222 } FT_OutlineGlyphRec;
225 /**************************************************************************
231 * A handle to an object used to model an SVG glyph. This is a
232 * 'sub-class' of @FT_Glyph, and a pointer to @FT_SvgGlyphRec.
237 typedef struct FT_SvgGlyphRec_* FT_SvgGlyph;
240 /**************************************************************************
246 * A structure used for OT-SVG glyphs. This is a 'sub-class' of
251 * The root @FT_GlyphRec fields.
254 * A pointer to the SVG document.
256 * svg_document_length ::
257 * The length of `svg_document`.
260 * The index of the glyph to be rendered.
263 * A metrics object storing the size information.
266 * The size of the EM square.
269 * The first glyph ID in the glyph range covered by this document.
272 * The last glyph ID in the glyph range covered by this document.
275 * A 2x2 transformation matrix to apply to the glyph while rendering
279 * Translation to apply to the glyph while rendering.
282 * The Glyph Management API requires @FT_Glyph or its 'sub-class' to have
283 * all the information needed to completely define the glyph's rendering.
284 * Outline-based glyphs can directly apply transformations to the outline
285 * but this is not possible for an SVG document that hasn't been parsed.
286 * Therefore, the transformation is stored along with the document. In
287 * the absence of a 'ViewBox' or 'Width'/'Height' attribute, the size of
288 * the ViewPort should be assumed to be 'units_per_EM'.
290 typedef struct FT_SvgGlyphRec_
294 FT_Byte* svg_document;
295 FT_ULong svg_document_length;
299 FT_Size_Metrics metrics;
300 FT_UShort units_per_EM;
302 FT_UShort start_glyph_id;
303 FT_UShort end_glyph_id;
311 /**************************************************************************
317 * A function used to create a new empty glyph image. Note that the
318 * created @FT_Glyph object must be released with @FT_Done_Glyph.
322 * A handle to the FreeType library object.
325 * The format of the glyph's image.
329 * A handle to the glyph object.
332 * FreeType error code. 0~means success.
337 FT_EXPORT( FT_Error )
338 FT_New_Glyph( FT_Library library,
339 FT_Glyph_Format format,
343 /**************************************************************************
349 * A function used to extract a glyph image from a slot. Note that the
350 * created @FT_Glyph object must be released with @FT_Done_Glyph.
354 * A handle to the source glyph slot.
358 * A handle to the glyph object. `NULL` in case of error.
361 * FreeType error code. 0~means success.
364 * Because `*aglyph->advance.x` and `*aglyph->advance.y` are 16.16
365 * fixed-point numbers, `slot->advance.x` and `slot->advance.y` (which
366 * are in 26.6 fixed-point format) must be in the range ]-32768;32768[.
368 FT_EXPORT( FT_Error )
369 FT_Get_Glyph( FT_GlyphSlot slot,
373 /**************************************************************************
379 * A function used to copy a glyph image. Note that the created
380 * @FT_Glyph object must be released with @FT_Done_Glyph.
384 * A handle to the source glyph object.
388 * A handle to the target glyph object. `NULL` in case of error.
391 * FreeType error code. 0~means success.
393 FT_EXPORT( FT_Error )
394 FT_Glyph_Copy( FT_Glyph source,
398 /**************************************************************************
404 * Transform a glyph image if its format is scalable.
408 * A handle to the target glyph object.
412 * A pointer to a 2x2 matrix to apply.
415 * A pointer to a 2d vector to apply. Coordinates are expressed in
419 * FreeType error code (if not 0, the glyph format is not scalable).
422 * The 2x2 transformation matrix is also applied to the glyph's advance
425 FT_EXPORT( FT_Error )
426 FT_Glyph_Transform( FT_Glyph glyph,
427 const FT_Matrix* matrix,
428 const FT_Vector* delta );
431 /**************************************************************************
437 * The mode how the values of @FT_Glyph_Get_CBox are returned.
440 * FT_GLYPH_BBOX_UNSCALED ::
441 * Return unscaled font units.
443 * FT_GLYPH_BBOX_SUBPIXELS ::
444 * Return unfitted 26.6 coordinates.
446 * FT_GLYPH_BBOX_GRIDFIT ::
447 * Return grid-fitted 26.6 coordinates.
449 * FT_GLYPH_BBOX_TRUNCATE ::
450 * Return coordinates in integer pixels.
452 * FT_GLYPH_BBOX_PIXELS ::
453 * Return grid-fitted pixel coordinates.
455 typedef enum FT_Glyph_BBox_Mode_
457 FT_GLYPH_BBOX_UNSCALED = 0,
458 FT_GLYPH_BBOX_SUBPIXELS = 0,
459 FT_GLYPH_BBOX_GRIDFIT = 1,
460 FT_GLYPH_BBOX_TRUNCATE = 2,
461 FT_GLYPH_BBOX_PIXELS = 3
463 } FT_Glyph_BBox_Mode;
466 /* these constants are deprecated; use the corresponding */
467 /* `FT_Glyph_BBox_Mode` values instead */
468 #define ft_glyph_bbox_unscaled FT_GLYPH_BBOX_UNSCALED
469 #define ft_glyph_bbox_subpixels FT_GLYPH_BBOX_SUBPIXELS
470 #define ft_glyph_bbox_gridfit FT_GLYPH_BBOX_GRIDFIT
471 #define ft_glyph_bbox_truncate FT_GLYPH_BBOX_TRUNCATE
472 #define ft_glyph_bbox_pixels FT_GLYPH_BBOX_PIXELS
475 /**************************************************************************
481 * Return a glyph's 'control box'. The control box encloses all the
482 * outline's points, including Bezier control points. Though it
483 * coincides with the exact bounding box for most glyphs, it can be
484 * slightly larger in some situations (like when rotating an outline that
485 * contains Bezier outside arcs).
487 * Computing the control box is very fast, while getting the bounding box
488 * can take much more time as it needs to walk over all segments and arcs
489 * in the outline. To get the latter, you can use the 'ftbbox'
490 * component, which is dedicated to this single task.
494 * A handle to the source glyph object.
497 * The mode that indicates how to interpret the returned bounding box
502 * The glyph coordinate bounding box. Coordinates are expressed in
503 * 1/64 of pixels if it is grid-fitted.
506 * Coordinates are relative to the glyph origin, using the y~upwards
509 * If the glyph has been loaded with @FT_LOAD_NO_SCALE, `bbox_mode` must
510 * be set to @FT_GLYPH_BBOX_UNSCALED to get unscaled font units in 26.6
511 * pixel format. The value @FT_GLYPH_BBOX_SUBPIXELS is another name for
514 * If the font is tricky and the glyph has been loaded with
515 * @FT_LOAD_NO_SCALE, the resulting CBox is meaningless. To get
516 * reasonable values for the CBox it is necessary to load the glyph at a
517 * large ppem value (so that the hinting instructions can properly shift
518 * and scale the subglyphs), then extracting the CBox, which can be
519 * eventually converted back to font units.
521 * Note that the maximum coordinates are exclusive, which means that one
522 * can compute the width and height of the glyph image (be it in integer
523 * or 26.6 pixels) as:
526 * width = bbox.xMax - bbox.xMin;
527 * height = bbox.yMax - bbox.yMin;
530 * Note also that for 26.6 coordinates, if `bbox_mode` is set to
531 * @FT_GLYPH_BBOX_GRIDFIT, the coordinates will also be grid-fitted,
532 * which corresponds to:
535 * bbox.xMin = FLOOR(bbox.xMin);
536 * bbox.yMin = FLOOR(bbox.yMin);
537 * bbox.xMax = CEILING(bbox.xMax);
538 * bbox.yMax = CEILING(bbox.yMax);
541 * To get the bbox in pixel coordinates, set `bbox_mode` to
542 * @FT_GLYPH_BBOX_TRUNCATE.
544 * To get the bbox in grid-fitted pixel coordinates, set `bbox_mode` to
545 * @FT_GLYPH_BBOX_PIXELS.
548 FT_Glyph_Get_CBox( FT_Glyph glyph,
553 /**************************************************************************
559 * Convert a given glyph object to a bitmap glyph object.
563 * A pointer to a handle to the target glyph.
567 * An enumeration that describes how the data is rendered.
570 * A pointer to a vector used to translate the glyph image before
571 * rendering. Can be~0 (if no translation). The origin is expressed
575 * A boolean that indicates that the original glyph image should be
576 * destroyed by this function. It is never destroyed in case of error.
579 * FreeType error code. 0~means success.
582 * This function does nothing if the glyph format isn't scalable.
584 * The glyph image is translated with the `origin` vector before
587 * The first parameter is a pointer to an @FT_Glyph handle that will be
588 * _replaced_ by this function (with newly allocated data). Typically,
589 * you would do something like the following (omitting error handling).
593 * FT_BitmapGlyph glyph_bitmap;
597 * error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAULT );
599 * // extract glyph image
600 * error = FT_Get_Glyph( face->glyph, &glyph );
602 * // convert to a bitmap (default render mode + destroying old)
603 * if ( glyph->format != FT_GLYPH_FORMAT_BITMAP )
605 * error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL,
607 * if ( error ) // `glyph' unchanged
611 * // access bitmap content by typecasting
612 * glyph_bitmap = (FT_BitmapGlyph)glyph;
614 * // do funny stuff with it, like blitting/drawing
617 * // discard glyph image (bitmap or not)
618 * FT_Done_Glyph( glyph );
621 * Here is another example, again without error handling.
624 * FT_Glyph glyphs[MAX_GLYPHS]
629 * for ( idx = 0; i < MAX_GLYPHS; i++ )
630 * error = FT_Load_Glyph( face, idx, FT_LOAD_DEFAULT ) ||
631 * FT_Get_Glyph ( face->glyph, &glyphs[idx] );
635 * for ( idx = 0; i < MAX_GLYPHS; i++ )
637 * FT_Glyph bitmap = glyphs[idx];
642 * // after this call, `bitmap' no longer points into
643 * // the `glyphs' array (and the old value isn't destroyed)
644 * FT_Glyph_To_Bitmap( &bitmap, FT_RENDER_MODE_MONO, 0, 0 );
648 * FT_Done_Glyph( bitmap );
653 * for ( idx = 0; i < MAX_GLYPHS; i++ )
654 * FT_Done_Glyph( glyphs[idx] );
657 FT_EXPORT( FT_Error )
658 FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
659 FT_Render_Mode render_mode,
660 const FT_Vector* origin,
664 /**************************************************************************
670 * Destroy a given glyph.
674 * A handle to the target glyph object. Can be `NULL`.
677 FT_Done_Glyph( FT_Glyph glyph );
682 /* other helpful functions */
684 /**************************************************************************
692 /**************************************************************************
698 * Perform the matrix operation `b = a*b`.
702 * A pointer to matrix `a`.
706 * A pointer to matrix `b`.
709 * The result is undefined if either `a` or `b` is zero.
711 * Since the function uses wrap-around arithmetic, results become
712 * meaningless if the arguments are very large.
715 FT_Matrix_Multiply( const FT_Matrix* a,
719 /**************************************************************************
725 * Invert a 2x2 matrix. Return an error if it can't be inverted.
729 * A pointer to the target matrix. Remains untouched in case of error.
732 * FreeType error code. 0~means success.
734 FT_EXPORT( FT_Error )
735 FT_Matrix_Invert( FT_Matrix* matrix );
742 #endif /* FTGLYPH_H_ */
748 /* Local Variables: */