From 0b7b23edfde49948af83037d774d9edeb5f5b247 Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Fri, 13 Oct 2000 07:08:44 +0000 Subject: [PATCH] complete revision --- docs/tutorial/step1.html | 140 +- docs/tutorial/step2.html | 2691 ++++++++++++++++++++------------------ 2 files changed, 1519 insertions(+), 1312 deletions(-) diff --git a/docs/tutorial/step1.html b/docs/tutorial/step1.html index 2468f1da8..fc66b124c 100644 --- a/docs/tutorial/step1.html +++ b/docs/tutorial/step1.html @@ -39,7 +39,7 @@

This is the first section of the FreeType 2 tutorial. It will teach you to do the following:

- + - +

@@ -343,13 +343,13 @@ cousins), it automatically creates a new size object for the returned face. This size object is directly accessible as face->size.

- -

NOTA BENE: A single face object can deal with one or more size - objects at a time; however, this is something that few programmers - really need to do. We have thus have decided to simplify the API for - the most common use (i.e. one size per face), while keeping this feature - available through additional functions.

- + +

A single face object can deal with one or more size objects at a + time; however, this is something that few programmers really need to do. + We have thus have decided to simplify the API for the most common use + (i.e. one size per face), while keeping this feature available through + additional functions.

+

When a new face object is created, its size object defaults to the character size of 10 pixels (both horizontally and vertically) for scalable formats. For fixed-sizes formats, the size is more or less @@ -374,7 +374,7 @@

@@ -414,7 +414,7 @@ 16 ); /* pixel_height */ -

This example will set the character pixel sizes to 16x16 pixels. +

This example will set the character pixel sizes to 16x16 pixels. As previously, a value of 0 for one of the dimensions means same as the other.

@@ -452,7 +452,7 @@ Unicode character codes if it finds one. Otherwise, it tries to find support for Latin-1, then ASCII.

-

We will describe later how to look for specific charmaps in a face. +

We will describe later how to look for specific charmaps in a face. For now, we will assume that the face contains at least a Unicode charmap that was selected during FT_New_Face(). To convert a Unicode character code to a font glyph index, we use @@ -479,13 +479,13 @@

Once you have a glyph index, you can load the corresponding glyph image. The latter can be stored in various formats within the font - file. For fixed-size formats like FNT or PCF, each image is a bitmap. + file. For fixed-size formats like FNT or PCF, each image is a bitmap. Scalable formats like TrueType or Type 1 use vectorial shapes, named outlines to describe each glyph. Some formats may have - even more exotic ways of representing glyph (e.g. MetaFont). + even more exotic ways of representing glyph (e.g. MetaFont). Fortunately, FreeType 2 is flexible enough to support any kind of glyph format through a simple API.

- +

The glyph image is always stored in a special object called a glyph slot. As its name suggests, a glyph slot is a container that is able to hold one glyph image at a time, be it a @@ -497,7 +497,7 @@

-    error = FT_Load_Glyph( 
+    error = FT_Load_Glyph(
               face,          /* handle to face object */
               glyph_index,   /* glyph index           */
               load_flags );  /* load flags, see below */
@@ -506,10 +506,10 @@

The load_flags value is a set of bit flags used to indicate some special operations. The default value FT_LOAD_DEFAULT is 0.

- +

This function will try to load the corresponding glyph image from the face. Basically, this means that

- +
  • If a bitmap is found for the corresponding glyph and pixel @@ -524,7 +524,7 @@ for certain formats like TrueType and Type 1.

- +

The field glyph->format describes the format used to store the glyph image in the slot. If it is not ft_glyph_format_bitmap, it is possible to immedialy convert @@ -536,7 +536,7 @@ face->glyph, /* glyph slot */ render_mode ); /* render mode */ - +

The parameter render_mode specifies how to render the glyph image. Set it ft_render_mode_normal to render a high-quality anti-aliased (256 gray levels) bitmap. You can @@ -547,11 +547,11 @@ through glyph->bitmap (a simple bitmap descriptor), and position it with glyph->bitmap_left and glyph->bitmap_top.

- +

Note that bitmap_left is the horizontal distance from the current pen position to the left-most border of the glyph bitmap, while bitmap_top is the vertical distance from the pen - position (on the baseline) to the top-most border of the glyph bitmap. + position (on the baseline) to the top-most border of the glyph bitmap. It is positive to indicate an upwards distance.

The second part of the tutorial will describe the contents of a @@ -572,23 +572,23 @@

There are two ways to select a different charmap with FreeType 2. The easiest is if the encoding you need already has a corresponding enumeration defined in - <freetype/freetype.h>, as ft_encoding_big5. + <freetype/freetype.h>, as ft_encoding_big5. In this case, you can simply call FT_Select_CharMap() as in

- +
     error = FT_Select_CharMap(
               face,                 /* target face object */
               ft_encoding_big5 );   /* encoding           */
- +

Another way is to manually parse the list of charmaps for the face, this is accessible through the fields num_charmaps and charmaps (notice the final 's') of the face object. As you could expect, the first is the number of charmaps in the face, while the second is a table of pointers to the charmaps embedded in the face.

- +

Each charmap has a few visible fields used to describe it more precisely. Mainly, one will look at charmap->platform_id and charmap->encoding_id which define a pair of values that can @@ -605,7 +605,7 @@ list. Bear in mind that some encodings correspond to several values pairs (yes, it's a real mess, but blame Apple and Microsoft on such stupidity). Here some code to do it:

- +
     FT_CharMap  found = 0;
@@ -642,9 +642,9 @@
       

It is possible to specify an affine transformation to be applied to glyph images when they are loaded. Of course, this will only work for scalable (vectorial) font formats.

- +

To do that, simply call FT_Set_Transform(), as in

- +
     error = FT_Set_Transform(
@@ -652,22 +652,22 @@
               &matrix,    /* pointer to 2x2 matrix */
               &delta );   /* pointer to 2d vector  */
- +

This function will set the current transformation for a given face object. Its second parameter is a pointer to a FT_Matrix structure that describes a 2x2 affine matrix. The third parameter is a pointer to a FT_Vector structure that describes a simple 2d vector that is used to translate the glyph image after the 2x2 transformation.

- +

Note that the matrix pointer can be set to NULL, in which case the identity transformation will be used. Coefficients of the matrix are otherwise in 16.16 fixed float units.

- +

The vector pointer can also be set to NULL in which case a delta vector of (0,0) will be used. The vector coordinates are expressed in 1/64th of a pixel (also known as 26.6 fixed floats).

- +

The transformation is applied to every glyph that is loaded through FT_Load_Glyph() and is completely independent of any hinting process. This means that you won't get the same @@ -682,7 +682,7 @@ compute a new character pixel size, then the other one to call FT_Set_Transform(). This is explained in details in a later section of this tutorial.

- +

Note also that loading a glyph bitmap with a non-identity transformation will produce an error.

@@ -695,7 +695,7 @@

We will now present you with a very simple example used to render a string of 8-bit Latin-1 text, assuming a face that contains a Unicode charmap

- +

The idea is to create a loop that will, on each iteration, load one glyph image, convert it to an anti-aliased bitmap, draw it on the target surface, then increment the current pen position.

@@ -706,7 +706,7 @@

The following code performs our simple text rendering with the functions previously described.

- +
     FT_GlyphSlot  slot = face->glyph;  /* a small shortcut */
@@ -716,37 +716,37 @@
     .. initialize library ..
     .. create face object ..
     .. set character size ..
-       
+
     pen_x = 300;
     pen_y = 200;
-       
+
     for ( n = 0; n < num_chars; n++ )
     {
       FT_UInt  glyph_index;
-         
+
 
       /* retrieve glyph index from character code */
       glyph_index = FT_Get_Char_Index( face, text[n] );
-         
+
       /* load glyph image into the slot (erase previous one) */
       error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
       if ( error ) continue;  /* ignore errors */
-         
+
       /* convert to an anti-aliased bitmap */
       error = FT_Render_Glyph( face->glyph, ft_render_mode_normal );
       if ( error ) continue;
-         
+
       /* now, draw to our target surface */
       my_draw_bitmap( &slot->bitmap,
                       pen_x + slot->bitmap_left,
                       pen_y - slot->bitmap_top );
-                         
+
       /* increment pen position */
       pen_x += slot->advance.x >> 6;
       pen_y += slot->advance.y >> 6;   /* not useful for now */
     }
-
- +
+

This code needs a few explanations:

    @@ -778,13 +778,13 @@ to pen_y instead of adding it.
- +

b. refined code

- +

The following code is a refined version of the example above. It uses features and functions of FreeType 2 that have not yet been introduced, and which will be explained below.

- +
     FT_GlyphSlot  slot = face->glyph;  /* a small shortcut */
@@ -795,29 +795,29 @@
     .. initialize library ..
     .. create face object ..
     .. set character size ..
-       
+
     pen_x = 300;
     pen_y = 200;
-       
+
     for ( n = 0; n < num_chars; n++ )
     {
       /* load glyph image into the slot (erase previous one) */
       error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
       if ( error ) continue;  /* ignore errors */
-         
+
       /* now, draw to our target surface */
       my_draw_bitmap( &slot->bitmap,
                       pen_x + slot->bitmap_left,
                       pen_y - slot->bitmap_top );
-                         
+
       /* increment pen position */
       pen_x += slot->advance.x >> 6;
     }
-
+

We have reduced the size of our code, but it does exactly the same thing.

- +
  • We use the function FT_Load_Char() instead of @@ -828,20 +828,20 @@

  • We do not use FT_LOAD_DEFAULT for the loading mode but the bit flag FT_LOAD_RENDER. It indicates that the glyph - image must be immediately converted to an anti-aliased bitmap. + image must be immediately converted to an anti-aliased bitmap. This is of course a shortcut that avoids calling FT_Render_Glyph() explicitly but is strictly equivalent.

    - +

    Note that you can also specify that you want a monochrome bitmap instead by using the additional FT_LOAD_MONOCHROME load flag.

  • -
- + +

c. more advanced rendering

- -

We now render transformed text (for example through a rotation). + +

We now render transformed text (for example through a rotation). To do that we use FT_Set_Transform():

@@ -862,30 +862,30 @@ matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L ); matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); - + /* the pen position in 26.6 cartesian space coordinates */ pen.x = 300 * 64; pen.y = ( my_target_height - 200 ) * 64; - + for ( n = 0; n < num_chars; n++ ) { /* set transformation */ FT_Set_Transform( face, &matrix, &pen ); - + /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, text[n], FT_LOAD_RENDER ); if ( error ) continue; /* ignore errors */ - + /* now, draw to our target surface (convert position) */ my_draw_bitmap( &slot->bitmap, slot->bitmap_left, my_target_height - slot->bitmap_top ); - + /* increment pen position */ pen.x += slot->advance.x; pen.y += slot->advance.y; }
- +

Notes:

@@ -916,7 +916,7 @@ is not rounded this time. - +

It is important to note that, while this example is a bit more complex than the previous one, it is strictly equivalent for the case where the transformation is the identity. Hence it can be used as a @@ -934,11 +934,11 @@

In this first section, you have learned the basics of FreeType 2, as well as sufficient knowledge how to render rotated text.

- +

The next part will dive into more details of the API in order to let you access glyph metrics and images directly, as well as how to deal with scaling, hinting, kerning, etc.

- +

The third part will discuss issues like modules, caching, and a few other advanced topics like how to use multiple size objects with a single face.

diff --git a/docs/tutorial/step2.html b/docs/tutorial/step2.html index d8cc1505c..242546389 100644 --- a/docs/tutorial/step2.html +++ b/docs/tutorial/step2.html @@ -1,4 +1,5 @@ - +

- FreeType 2.0 Tutorial
- Step 2 - managing glyphs + FreeType 2.0 Tutorial
+ Step 2 -- managing glyphs

- © 2000 David Turner + © 2000 David Turner (david@freetype.org)
- © 2000 The FreeType Development Team + © 2000 The FreeType Development Team (www.freetype.org)

- +

@@ -36,9 +37,9 @@ Introduction -

This is the second section of the FreeType 2 tutorial. It will teach - you the following:

- +

This is the second section of the FreeType 2 tutorial. It will + teach you the following:

+
  • how to retrieve glyph metrics
  • how to easily manage glyph images
  • @@ -46,1351 +47,1557 @@
  • how to render a simple string of text, with kerning
  • how to render a centered string of text (with kerning)
  • how to render a transformed string of text (with centering)
  • -
  • finally, how to access metrics in design font units when needed, - and how to scale them to device space.
  • +
  • finally, how to access metrics in design font units if needed, + and how to scale them to device space
- -
- + +
+

- 1. Glyph metrics: + 1. Glyph metrics

-

Glyph metrics are, as their name suggests, certain distances associated - to each glyph in order to describe how to use it to layout text.

+

Glyph metrics are, as their name suggests, certain distances + associated to each glyph in order to describe how to use it to layout + text.

-

There are usually two sets of metrics for a single glyph: those used to - layout the glyph in horizontal text layouts (like latin, cyrillic, - arabic, hebrew, etc..), and those used to layout the glyph in vertical - text layouts (like some layouts of Chinese, Japanese, Korean, and - others..).

+

There are usually two sets of metrics for a single glyph: those used + to layout the glyph in horizontal text layouts (like Latin, Cyrillic, + Arabic, Hebrew, etc.), and those used to layout the glyph in vertical + text layouts (like some layouts of Chinese, Japanese, Korean, and + others).

+ +

Note that only a few font formats provide vertical metrics. You can + test wether a given face object contains them by using the macro + FT_HAS_VERTICAL(face), which is true if has vertical + metrics.

-

Note that only a few font formats provide vertical metrics. You can - test wether a given face object contains them by using the macro - FT_HAS_VERTICAL(face), which is true when appropriate.

-

Individual glyph metrics can be accessed by first loading the glyph - in a face's glyph slot, then accessing them through the - face->glyph->metrics structure. This will be detailed - later, for now, we'll see that it contains the following fields:

- -
- width - -

This is the width of the glyph image's bounding box. It is independent - of layout direction.

-
- height - -

This is the height of the glyph image's bounding box. It is independent - of layout direction.

-
- horiBearingX - -

For horizontal text layouts, this is the horizontal distance from - the current cursor position to the left-most border of the glyph image's - bounding box.

-
- horiBearingY - -

For horizontal text layouts, this is the vertical distance from - the current cursor position (on the baseline) to the top-most border of - the glyph image's bounding box.

-
- horiAdvance - -

For horizontal text layouts, this is the horizontal distance - used to increment the pen position when the glyph is drawn as part of - a string of text.

-
- vertBearingX - -

For vertical text layouts, this is the horizontal distance from - the current cursor position to the left-most border of the glyph image's - bounding box.

-
- vertBearingY - -

For vertical text layouts, this is the vertical distance from - the current cursor position (on the baseline) to the top-most border of - the glyph image's bounding box.

-
- vertAdvance - -

For vertical text layouts, this is the vertical distance - used to increment the pen position when the glyph is drawn as part of - a string of text.

-
+ in a face's glyph slot, then using the face->glyph->metrics + structure. This will be described later; for now, we observe that it + contains the following fields:

-

NOTA BENE: As all fonts do not contain vertical - metrics, the values of vertBearingX, vertBearingY - and vertAdvance should not be considered reliable when - FT_HAS_VERTICAL(face) is false.

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ width + + This is the width of the glyph image's bounding box. It is + independent of layout direction. +
+ height + + This is the height of the glyph image's bounding box. It is + independent of layout direction. +
+ horiBearingX + + For horizontal text layouts, this is the horizontal + distance from the current cursor position to the left-most border of + the glyph image's bounding box. +
+ horiBearingY + + For horizontal text layouts, this is the vertical distance + from the current cursor position (on the baseline) to the top-most + border of the glyph image's bounding box. +
+ horiAdvance + + For horizontal text layouts, this is the horizontal + distance used to increment the pen position when the glyph is drawn + as part of a string of text. +
+ vertBearingX + + For vertical text layouts, this is the horizontal distance + from the current cursor position to the left-most border of the + glyph image's bounding box. +
+ vertBearingY + + For vertical text layouts, this is the vertical distance + from the current cursor position (on the baseline) to the top-most + border of the glyph image's bounding box. +
+ vertAdvance + + For vertical text layouts, this is the vertical distance + used to increment the pen position when the glyph is drawn as part + of a string of text. +
+
+ +

As not all fonts do contain vertical metrics, the values of + vertBearingX, vertBearingY, and vertAdvance + should not be considered reliable if FT_HAS_VERTICAL(face) is + false.

+ +

The following graphics illustrate the metrics more clearly. First, + for horizontal metrics, where the baseline is the horizontal axis:

+ +
+ horizontal metrics layout +
-

The following graphics illustrate the metrics more clearly. First, for - horizontal metrics, where the baseline is the horizontal axis :

- -
-

For vertical text layouts, the baseline is vertical and is the - vertical axis:

- -
+ vertical axis:

+
+ vertical metrics layout +

The metrics found in face->glyph->metrics are normally - expressed in 26.6 pixels (i.e 1/64th of pixels), unless you use - the FT_LOAD_NO_SCALE flag when calling - FT_Load_Glyph or FT_Load_Char. In this case, - the metrics will be expressed in original font units.

- -

The glyph slot object has also a few other interesting fields - that will ease a developer's work. You can access them though - face->glyph->??? :

+ expressed in 26.6 pixel format (i.e 1/64th of pixels), unless you use + the FT_LOAD_NO_SCALE flag when calling FT_Load_Glyph() + or FT_Load_Char(). In that case, the metrics will be expressed + in original font units.

-
- advance - -

This field is a FT_Vector which holds the transformed - advance for the glyph. That's useful when you're using a transform - through FT_Set_Transform, as shown in the rotated text - example of section I. Other than that, its value is - by default (metrics.horiAdvance,0), unless you specify - FT_LOAD_VERTICAL when loading the glyph image; - it will then be (0,metrics.vertAdvance)

-
- linearHoriAdvance - -

- This field contains the linearly-scaled value of the glyph's horizontal - advance width. Indeed, the value of metrics.horiAdvance that is - returned in the glyph slot is normally rounded to integer pixel - coordinates (i.e., it will be a multiple of 64) by the font driver used - to load the glyph image. linearHoriAdvance is a 16.16 fixed float - number that gives the value of the original glyph advance width in - 1/65536th of pixels. It can be use to perform pseudo device-independent - text layouts.

-
- linearVertAdvance - -

This is the same thing as linearHoriAdvance for the - glyph's vertical advance height. Its value is only reliable if the font - face contains vertical metrics.

-
- +

The glyph slot object has a few other interesting fields that will + ease a developer's work. You can access them through + face->glyph->???:

+ +
+ + + + + + + + + + + + + +
+ advance + + This field is an FT_Vector which holds the transformed + advance value for the glyph. This is useful if you are using a + transformation through FT_Set_Transform(), as shown in the + rotated text example of the previous part. Other than that, its + value is by default (metrics.horiAdvance,0), unless you specify + FT_LOAD_VERTICAL when loading the glyph image; it will then + be (0,metrics.vertAdvance). +
+ linearHoriAdvance + + This field contains the linearly-scaled value of the glyph's + horizontal advance width. Indeed, the value of + metrics.horiAdvance that is returned in the glyph slot is + normally rounded to integer pixel coordinates (i.e., it will be a + multiple of 64) by the font driver used to load the glyph + image. linearHoriAdvance is a 16.16 fixed float number + that gives the value of the original glyph advance width in + 1/65536th of pixels. It can be use to perform pseudo + device-independent text layouts. +
+ linearVertAdvance + + This is the same thing as linearHoriAdvance for the glyph's + vertical advance height. Its value is only reliable if the font + face contains vertical metrics. +
+
+ +
+ +

+ 2. Managing glyph images +

+ +

The glyph image that is loaded in a glyph slot can be converted into + a bitmap, either by using FT_LOAD_RENDER when loading it, or by + calling FT_Render_Glyph() afterwards. Each time you load a new + glyph image, the previous one is erased from the glyph slot.

+ +

There are times, however, where you may need to extract this image + from the glyph slot, in order to cache it within your application, and + even perform additional transformations and measures on it before + converting it to a bitmap.

+ +

The FreeType 2 API has a specific extension which is capable of + dealing with glyph images in a flexible and generic way. To use it, you + first need to include the ftglyph.h header file:

+ + +
+    #include <freetype/ftglyph.h>
+
+ +

We will now explain how to use the functions defined in this + file.

+ +

+ a. Extracting the glyph image +

+ +

You can extract a single glyph image very easily. Here some code + that shows how to do it.

+ + +
+    FT_Glyph  glyph;    /* handle to glyph image */
 
 
-  
+ ... + error = FT_Load_Glyph( face, glyph, FT_LOAD_NORMAL ); + if ( error ) { .... } -

- 2. Managing glyph images: -

+ error = FT_Get_Glyph( face->glyph, &glyph ); + if ( error ) { .... }
+
-

The glyph image that is loaded in a glyph slot can be converted into - a bitmap, either by using FT_LOAD_RENDER when loading it, or - by calling FT_Render_Glyph. Each time you load a new glyph - image, the previous one is erased from the glyph slot.

- -

There are times however where you may need to extract this image from - the glyph slot, in order to cache it within your application, and - even perform additional transforms and measures on it before converting - it to a bitmap. -

+

As can be seen, we have

-

The FreeType 2 API has a specific extension which is capable of dealing - with glyph images in a flexible and generic way. To use it, you first need - to include the "ftglyph.h" header file, as in:

- -

-      #include <freetype/ftglyph.h>
-  
+
    +
  • + created a variable, named glyph, of type + FT_Glyph. This is a handle (pointer) to an individual + glyph image, +
  • +
  • + loaded the glyph image normally in the face's glyph slot. We did + not use FT_LOAD_RENDER because we want to grab a scalable + glyph image, in order to transform it later, +
  • +
  • + copied the glyph image from the slot into a new FT_Glyph + object, by calling FT_Get_Glyph(). This function returns + an error code and sets glyph. +
  • +
-

We will now explain how to use the functions defined in this file:

- -

a. Extracting the glyph image:

+

It is important to note that the extracted glyph is in the same + format as the original one that is still in the slot. For example, if + we are loading a glyph from a TrueType font file, the glyph image will + really be a scalable vector outline.

-

You can extract a single glyph image very easily. Here's some code - that shows how to do it:

+

You can access the field glyph->format if you want to know + exactly how the glyph is modeled and stored. A new glyph object can + be destroyed with a call to FT_Done_Glyph.

-

-       FT_Glyph    glyph;    // handle to glyph image
-       
-       ....
-       error = FT_Load_Glyph( face, glyph, FT_LOAD_NORMAL );
-       if (error) { .... }
-       
-       error = FT_Get_Glyph( face->glyph, &glyph );
-       if (error) { .... }
-  
+

The glyph object contains exactly one glyph image and a 2d vector + representing the glyph's advance in 16.16 fixed float coordinates. + The latter can be accessed directly as glyph->advance.

-

As you see, we have:

- -
    -
  • - Created a variable, named glyph, of type FT_Glyph. - This is a handle (pointer) to an individual glyph image. -

  • - -
  • - Loaded the glyph image normally in the face's glyph slot. We did not - use FT_LOAD_RENDER because we want to grab a scalable glyph - image, in order to later transform it. -

  • +

    Note that unlike other FreeType objects, the library doesn't + keep a list of all allocated glyph objects. This means you will need + to destroy them yourself, instead of relying on + FT_Done_FreeType() doing all the clean-up.

    -
  • - Copy the glyph image from the slot into a new FT_Glyph object, - by calling FT_Get_Glyph. This function returns an error - code and sets glyph. -

  • -
- -

It is important to note that the extracted glyph is in the same format - than the original one that is still in the slot. For example, if we're - loading a glyph from a TrueType font file, the glyph image will really - be a scalable vector outline.

+

+ b. Transforming & copying the glyph image +

-

You can access the field glyph->format if you want to - know exactly how the glyph is modeled and stored. A new glyph object can - be destroyed with a call to FT_Done_Glyph.

+

If the glyph image is scalable (i.e., if glyph->format is + not equal to ft_glyph_format_bitmap), it is possible to + transform the image anytime by a call to + FT_Glyph_Transform().

-

The glyph object contains exactly one glyph image and a 2D vector - representing the glyph's advance in 16.16 fixed float coordinates. - The latter can be accessed directly as glyph->advance -

+

You can also copy a single glyph image with + FT_Glyph_Copy(). Here some example code:

-

Note that unlike - other FreeType objects, the library doesn't keeps a list of all - allocated glyph objects. This means you'll need to destroy them - yourself, instead of relying on FT_Done_FreeType doing - all the clean-up.

- -

b. Transforming & copying the glyph image

+ +
+    FT_Glyph   glyph, glyph2;
+    FT_Matrix  matrix;
+    FT_Vector  delta;
 
-  

If the glyph image is scalable (i.e. if glyph->format is not - equal to ft_glyph_format_bitmap), it is possible to transform - the image anytime by a call to FT_Glyph_Transform.

- -

You can also copy a single glyph image with FT_Glyph_Copy. - Here's some example code:

- -

-    FT_Glyph  glyph, glyph2;
-    FT_Matrix matrix;
-    FT_Vector delta;
-    
-    ......
+
+    ...
     .. load glyph image in "glyph" ..
-    
-    // copy glyph to glyph2
-    //
-    error = FT_Glyph_Copy( glyph, &glyph2 );
-    if (error) { ... could not copy (out of memory) }
-    
-    // translate "glyph"
-    //    
-    delta.x = -100 * 64;   // coordinates are in 26.6 pixels
-    delta.y =  50  * 64;
-    
-    FT_Glyph_Transform( glyph, 0, &delta );
-    
-    // transform glyph2 (horizontal shear)
-    //
-    matrix.xx = 0x10000;
+
+    /* copy glyph to glyph2 */
+    error = FT_Glyph_Copy( glyph, &glyph2 );
+    if ( error ) { ... could not copy (out of memory) }
+
+    /* translate "glyph" */
+    delta.x = -100 * 64;   /* coordinates are in 26.6 pixels */
+    delta.y =   50 * 64;
+
+    FT_Glyph_Transform( glyph, 0, &delta );
+
+    /* transform glyph2 (horizontal shear) */
+    matrix.xx = 0x10000L;
     matrix.xy = 0;
-    matrix.yx = 0.12 * 0x10000;
-    matrix.yy = 0x10000;
-    
-    FT_Glyph_Transform( glyph2, &matrix, 0 );
-  
+ matrix.yx = 0.12 * 0x10000L; + matrix.yy = 0x10000L; -

Note that the 2x2 transform matrix is always applied to the 16.16 - advance vector in the glyph, you thus don't need to recompute it..

+ FT_Glyph_Transform( glyph2, &lmatrix, 0 );
+
-

c. Measuring the glyph image

- -

You can also retrieve the control (bounding) box of any glyph image - (scalable or not), through the FT_Glyph_Get_CBox function, - as in: -

+

Note that the 2x2 transform matrix is always applied to the 16.16 + advance vector in the glyph; you thus don't need to recompute it.

-

-     FT_BBox   bbox;
-     ...
-     FT_Glyph_BBox(  glyph, bbox_mode, &bbox );
-  
+

+ c. Measuring the glyph image +

-

Coordinates are relative to the glyph origin, i.e. (0,0), using the - Y_upwards convention. This function takes a special argument, the - "bbox mode", that is a set of bit flags used to indicate how - box coordinates are expressed. If ft_glyph_bbox_subpixels - is set in the bbox mode, the coordinates are returned in 26.6 pixels - (i.e. 1/64th of pixels). Otherwise, they're in integer pixels.

+

You can also retrieve the control (bounding) box of any glyph image + (scalable or not), using the FT_Glyph_Get_CBox function:

-

Note that the box's maximum coordinates are exclusive, which means - that you can always compute the width and height of the glyph image, - be in in integer or 26.6 pixels with:

- -
     
-     width  = bbox.xMax - bbox.xMin;
-     height = bbox.yMax - bbox.yMin;
-  
- -

Note also that for 26.6 coordinates, if - ft_glyph_bbox_gridfit is set in the bbox mode, - 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)
-  
- -

The default value for the bbox mode, which is 0, corresponds to - ft_glyph_bbox_pixels (i.e. integer pixel coordinates).

+ +
+    FT_BBox  bbox;
 
 
-  

d. Converting the glyph image to a bitmap

- -

You may need to convert the glyph object to a bitmap once you have - convienently cached or transformed it. This can be done easily with - the FT_Glyph_To_Bitmap function. It is chared of - converting any glyph object into a bitmap, as in:

- -

+    ...
+    FT_Glyph_Get_CBox( glyph, bbox_mode, &bbox );
+ + +

Coordinates are relative to the glyph origin, i.e. (0,0), using the + Y upwards convention. This function takes a special argument, + the bbox mode, to indicate how box coordinates are expressed. + If bbox_mode is set to ft_glyph_bbox_subpixels, the + coordinates are returned in 26.6 pixels (i.e. 1/64th of pixels). + +

Note that the box's maximum coordinates are exclusive, which means + that you can always compute the width and height of the glyph image, + be it in integer or 26.6 pixels with

+ + +
+    width  = bbox.xMax - bbox.xMin;
+    height = bbox.yMax - bbox.yMin;
+
+ +

Note also that for 26.6 coordinates, if + ft_glyph_bbox_gridfit is set in bbox_mode, 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)
+
+ +

The default value for the bbox mode is + ft_glyph_bbox_pixels (i.e. integer, grid-fitted pixel + coordinates). Please check the API reference for + FT_Glyph_Get_CBox() other possible values

+ +

+ d. Converting the glyph image to a bitmap +

+ +

You may need to convert the glyph object to a bitmap once you have + conveniently cached or transformed it. This can be done easily with + the FT_Glyph_To_Bitmap() function:

+ + +
     FT_Vector  origin;
-    
-    origin.x = 32;   /* 1/2 pixel in 26.26 format */
+
+
+    origin.x = 32;   /* 1/2 pixel in 26.26 format */
     origin.y = 0;
-    
-    error = FT_Glyph_To_Bitmap( &glyph,
+
+    error = FT_Glyph_To_Bitmap( &glyph,
                                 render_mode,
-                                &origin,
-                                1 );        // destroy original image == true
-  
+ &origin, + 1 ); /* destroy orig. image == true */
+
-

We will know details this function's parameters:

- -
    -
  • - the first parameter is the address of the source glyph's handle. - When the function is called, it reads its to access the source - glyph object. After the call, the handle will point to a - new glyph object that contains the rendered bitmap. -

  • - -
  • - the second parameter is a standard render mode, that is used to specify - what kind of bitmap we want. It can be ft_render_mode_default - for an 8-bit anti-aliased pixmap, or ft_render_mode_mono for - a 1-bit monochrome bitmap. -

  • - -
  • - the third parameter is a pointer to a 2D vector that is used to - translate the source glyph image before the conversion. Note that - the source image will be translated back to its original position - (and will thus be left unchanged) after the call. If you do not need - to translate the source glyph before rendering, set this pointer to 0. -

  • - -
  • - the last parameter is a boolean that indicates wether the source - glyph object should be destroyed by the function. By default, the - original glyph object is never destroyed, even if its handle is - lost (it's up to client applications to keep it). -

  • -
- -

The new glyph object always contain a bitmap (when no error is returned), - and you must typecast its handle to the - FT_BitmapGlyph type in order to access its content. - This type is a sort of "subclass" of FT_Glyph that contains - additional fields:

- -
- left - -

Just like the bitmap_left field of a glyph slot, this is the - horizontal distance from the glyph origin (0,0) to the left-most pixel - of the glyph bitmap. It is expressed in integer pixels.

-
- top - -

Just like the bitmap_top field of a glyph slot, this is the - vertical distance from the glyph origin (0,0) to the top-most pixel - of the glyph bitmap (more exactly, to the pixel just above the bitmap). - This distance is expressed in integer pixels, and is positive for upwards - Y.

-
- bitmap - -

This is a bitmap descriptor for the glyph object, just like the - bitmap field in a glyph slot.

-
- -
-

- 3. Global glyph metrics: -

- -

Unlike glyph metrics, global ones are used to describe distances - and features of a whole font face. They can be expressed either in - 26.6 pixels or in design "font units" for scalable formats.

+

Some details on this function's parameters:

-

- a. Design Global Metrics: -

+
    +
  • + The first parameter is the address of the source glyph's + handle. When the function is called, it reads it to access + the source glyph object. After the call, the handle will point to + a new glyph object that contains the rendered bitmap. +
  • +
  • + The second parameter is a standard render mode that is used to + specify what kind of bitmap we want. It can be + ft_render_mode_default for an 8-bit anti-aliased pixmap, + or ft_render_mode_mono for a 1-bit monochrome bitmap. +
  • +
  • + The third parameter is a pointer to a 2d vector that is used to + translate the source glyph image before the conversion. Note that + the source image will be translated back to its original position + (and will thus be left unchanged) after the call. If you do not + need to translate the source glyph before rendering, set this + pointer to 0. +
  • +
  • + The last parameter is a Boolean to indicate whether the source + glyph object should be destroyed by the function. By default, the + original glyph object is never destroyed, even if its handle is + lost (it's up to client applications to keep it). +
  • +
-

For scalable formats, all global metrics are expressed in font units - in order to be later scaled to device space, according to the rules - described in the last chapter of this section of the tutorial. You - can access them directly as simple fields of a FT_Face - handle.

- -

However, you need to check that the font face's format is scalable - before using them. One can do it by using the macro - FT_IS_SCALABLE(face) which returns true when - appropriate.

+

The new glyph object always contains a bitmap (when no error is + returned), and you must typecast its handle to the + FT_BitmapGlyph type in order to access its contents. This + type is a sort of subclass of FT_Glyph that contains + additional fields:

-

In this case, you can access the global design metrics as:

+
+ + + + + + + + + + + + + +
+ left + + Just like the bitmap_left field of a glyph slot, this is + the horizontal distance from the glyph origin (0,0) to the + left-most pixel of the glyph bitmap. It is expressed in integer + pixels. +
+ top + + Just like the bitmap_top field of a glyph slot, this is + the vertical distance from the glyph origin (0,0) to the top-most + pixel of the glyph bitmap (more exactly, to the pixel just above + the bitmap). This distance is expressed in integer pixels, and is + positive for upwards Y. +
+ bitmap + + This is a bitmap descriptor for the glyph object, just like the + bitmap field in a glyph slot. +
+
-
- units_per_EM - -

This is the size of the EM square for the font face. It is used by scalable - formats to scale design coordinates to device pixels, as described by the - last chapter of this section. Its value usually is 2048 (for TrueType) - or 1000 (for Type1), but others are possible too. It is set to 1 for - fixed-size formats like FNT/FON/PCF/BDF.

-
- global_bbox - -

The global bounding box is defined as the largest rectangle that can - enclose all the glyphs in a font face. It is defined for horizontal - layouts only.

-
- ascender - -

The ascender is the vertical distance from the horizontal baseline to - the highest "character" coordinate in a font face. Unfortunately, font - formats define the ascender differently. For some, it represents - the ascent of all capital latin characters, without accents, for others - it's the ascent of the highest accented character, and finally, other - formats define it as being equal to global_bbox.yMax.

-
- descender - -

The descender is the vertical distance from the horizontal baseline to - the lowest "character" coordinate in a font face. Unfortunately, font - formats define the descender differently. For some, it represents - the descent of all capital latin characters, without accents, for others - it's the ascent of the lowest accented character, and finally, other - formats define it as being equal to global_bbox.yMin. - This field is usually negative

-
- text_height - -

This field is simply used to compute a default line spacing (i.e. the - baseline-to-baseline distance) when writing text with this font. Note that - it usually is larger than the sum of the ascender and descender taken in - absolute value. There is also no guarantee that no glyphs can extend - above or below subsequent baselines when using this distance.

-
- max_advance_width - -

This field gives the maximum horizontal cursor advance for all glyphs - in the font. It can be used to quickly compute the maximum advance width - of a string of text. It doesn't correspond to the maximum glyph image - width !!

-
- max_advance_height - -

Same as max_advance_width but for vertical text layout. It is - only available in fonts providing vertical glyph metrics.

-
- underline_position - -

When displaying or rendering underlined text, this value corresponds to - the vertical position, relative to the baseline, of the underline bar. It - noramlly is negative (as it's below the baseline).

-
- underline_thickness - -

When displaying or rendering underlined text, this value corresponds to - the vertical thickness of the underline.

-
- -

Notice how, unfortunately, the values of the ascender and the descender - are not reliable (due to various discrepancies in font formats).

- -

- b. Scaled Global Metrics: -

+
-

Each size object also contains a scaled versions of some of the global - metrics described above. They can be accessed directly through the - face->size->metrics structure.

- -

Note that these values correspond to scaled versions of the design - global metrics, with no rounding/grid-fitting performed.. - They are also completely independent of any hinting process. In other - words, don't rely on them to get exact metrics at the pixel level. - They're expressed in 26.6 pixels.

+

+ 3. Global glyph metrics +

-
- ascender - -

This is the scaled version of the original design ascender.

-
- descender - -

This is the scaled version of the original design descender.

-
- height - -

This is the scaled version of the original design text height. - That probably is the only field you should really use in this structure.

-
- max_advance - -

Thi is the scaled version of the original design max advance.

-
+

Unlike glyph metrics, global ones are used to describe distances and + features of a whole font face. They can be expressed either in 26.6 + pixel format or in design font units for scalable formats.

-

Note that the face->size->metrics structure contains other - fields that are used to scale design coordinates to device space. They're - described, in the last chapter.

+

+ a. Design global metrics +

-

- c. Kerning: -

- -

Kerning is the process of adjusting the position of two subsequent - glyph images in a string of text, in order to improve the general - appearance of text. Basically, it means that when the glyph for an - "A" is followed by the glyph for a "V", the space between them can - be slightly reduced to avoid extra "diagonal whitespace".

- -

Note that in theory, kerning can happen both in the horizontal and - vertical direction between two glyphs; however, it only happens in - the horizontal direction in nearly all cases except really extreme - ones.

+

For scalable formats, all global metrics are expressed in font + units in order to be later scaled to device space, according to the + rules described in the last chapter of this part of the tutorial. You + can access them directly as simple fields of a FT_Face + handle.

-

Note all font formats contain kerning information. Instead, they sometimes - rely on an additional file that contains various glyph metrics, including - kerning, but no glyph images. A good example would be the Type 1 format, - where glyph images are stored in a file with extension ".pfa" or ".pfb", - and where kerning metrics can be found in an additional file with extension - ".afm" or ".pfm".

- -

FreeType 2 allows you to deal with this, by providing the - FT_Attach_File and FT_Attach_Stream APIs. - Both functions are used to load additional metrics into a face object, - by reading them from an additional format-specific file. For example, - you could open a Type 1 font by doing the following:

- -

-      error = FT_New_Face( library, "/usr/shared/fonts/cour.pfb", 0, &face );
-      if (error) { ... }
-      
-      error = FT_Attach_File( face, "/usr/shared/fonts/cour.afm" );
-      if (error) { .. could not read kerning and additional metrics .. }
-  
+

However, you need to check that the font face's format is scalable + before using them. One can do it by using the macro + FT_IS_SCALABLE(face) which returns true if we have a + scalable format.

-

Note that FT_Attach_Stream is similar to - FT_Attach_File except that it doesn't take a C string - to name the extra file, but a FT_Stream handle. Also, - reading a metrics file is in no way, mandatory.

+

In this case, you can access the global design metrics as

-

Finally, the file attachment APIs are very generic and can be used to - load any kind of extra information for a given face. The nature of the - additional content is entirely font format specific.

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ units_per_EM + + This is the size of the EM square for the font face. It is used + by scalable formats to scale design coordinates to device pixels, + as described by the last chapter of this part. Its value usually + is 2048 (for TrueType) or 1000 (for Type 1), but + other values are possible too. It is set to 1 for fixed-size + formats like FNT/FON/PCF/BDF. +
+ global_bbox + + The global bounding box is defined as the largest rectangle that + can enclose all the glyphs in a font face. It is defined for + horizontal layouts only. This is not necessarily the smallest + bounding box which is possible. +
+ ascender + + The ascender is the vertical distance from the horizontal baseline + to the height of the highest character in a font face. + Unfortunately, font formats define the ascender + differently. For some, it represents the ascent of all + capital Latin characters, without accents, for others it is the + ascent of the highest accented character, and finally, other + formats define it as being equal to global_bbox.yMax. +
+ descender + + The descender is the vertical distance from the horizontal + baseline to the depth of the lowest character in a font face. + Unfortunately, font formats define the descender + differently. For some, it represents the descent of all + capital Latin characters, without accents, for others it is the + ascent of the lowest accented character, and finally, other + formats define it as being equal to global_bbox.yMin. + This field is usually negative. +
+ text_height + + This field is used to compute a default line spacing (i.e. the + baseline-to-baseline distance) when writing text with this font. + Note that it usually is larger than the sum of the ascender and + descender taken as absolute values. There is also no guarantee + that no glyphs can extend above or below subsequent baselines when + using this distance. +
+ max_advance_width + + This field gives the maximum horizontal cursor advance for all + glyphs in the font. It can be used to quickly compute the maximum + advance width of a string of text. It doesn't correspond to + the maximum glyph image width! +
+ max_advance_height + + Same as max_advance_width but for vertical text layout. + It is only available in fonts providing vertical glyph metrics. +
+ underline_position + + When displaying or rendering underlined text, this value + corresponds to the vertical position, relative to the baseline, of + the underline bar. It normally is negative (as it is below the + baseline). +
+ underline_thickness + + When displaying or rendering underlined text, this value + corresponds to the vertical thickness of the underline. +
+
-

FreeType 2 allows you to retrieve the kerning information between - two glyphs through the FT_Get_Kerning function, whose - interface looks like:

- -

-     FT_Vector  kerning;
-     ...
-     error = FT_Get_Kerning( face,                  // handle to face object
-                             left,                  // left glyph index
-                             right,                 // right glyph index
-                             kerning_mode,          // kerning mode
-                             &kerning );            // target vector
-  
+

Notice how, unfortunately, the values of the ascender and the + descender are not reliable (due to various discrepancies in font + formats).

-

As you see, the function takes a handle to a face object, the indices - of the left and right glyphs for which the kerning value is desired, - as well as an integer, called the "kerning mode", and a pointer to - a destination vector that receives the corresponding distances.

- -

The kerning mode is very similar to the "bbox mode" described in a - previous chapter. It's a enumeration that indicates how the - kerning distances are expressed in the target vector.

- -

The default value is ft_kerning_mode_default which - has value 0. It corresponds to kerning distances expressed in 26.6 - grid-fitted pixels (which means that the values are multiples of 64). - For scalable formats, this means that the design kerning distance is - scaled then rounded.

- -

The value ft_kerning_mode_unfitted corresponds to kerning - distances expressed in 26.6 unfitted pixels (i.e. that do not correspond - to integer coordinates). It's the design kerning distance that is simply - scaled without rounding.

- -

Finally, the value ft_kerning_mode_unscaled is used to - return the design kerning distance, expressed in font units. You can - later scale it to device space using the computations explained in the - last chapter of this section.

+

+ b. Scaled global metrics +

-

Note that the "left" and "right" positions correspond to the visual - order of the glyphs in the string of text. This is important for +

Each size object also contains a scaled versions of some of the + global metrics described above. They can be accessed directly through + the face->size->metrics structure.

- bi-directional text, or simply when writing right-to-left text..

- -
+

Note that these values correspond to scaled versions of the design + global metrics, with no rounding/grid-fitting performed. They + are also completely independent of any hinting process. In other + words, don't rely on them to get exact metrics at the pixel level. + They are expressed in 26.6 pixel format.

-

- 4. Simple text rendering: kerning + centering: -

+
+ + + + + + + + + + + + + + + + + +
+ ascender + + This is the scaled version of the original design ascender. +
+ descender + + This is the scaled version of the original design descender. +
+ height + + The scaled version of the original design text height. This is + probably the only field you should really use in this structure. +
+ max_advance + + This is the scaled version of the original design max advance. +
+
-

In order to show off what we just learned, we will now show how to modify - the example code that was provided in section I to render a string of text, - and enhance it to support kerning and delayed rendering.

+

Note that the face->size->metrics structure contains other + fields that are used to scale design coordinates to device space. + They are described below.

-

- a. Kerning support: -

- -

Adding support for kerning to our code is trivial, as long as we consider - that we're still dealing with a left-to-right script like Latin. We - simply need to retrieve the kerning distance between two glyphs in order - to alter the pen position appropriately. The code looks like:

- -
-      FT_GlyphSlot  slot = face->glyph;  // a small shortcut
-      FT_UInt       glyph_index;
-      FT_Bool       use_kerning;
-      FT_UInt       previous;
-      int           pen_x, pen_y, n;
+      

+ c. Kerning +

- .. initialise library .. - .. create face object .. - .. set character size .. - - pen_x = 300; - pen_y = 200; - - use_kerning = FT_HAS_KERNING(face); - previous = 0; - - for ( n = 0; n < num_chars; n++ ) +

Kerning is the process of adjusting the position of two subsequent + glyph images in a string of text, in order to improve the general + appearance of text. Basically, it means that when the glyph for an + "A" is followed by the glyph for a "V", the space between them can be + slightly reduced to avoid extra "diagonal whitespace".

+ +

Note that in theory, kerning can happen both in the horizontal and + vertical direction between two glyphs; however, it only happens in the + horizontal direction in nearly all cases except really extreme + ones.

+ +

Not all font formats contain kerning information. Instead, they + sometimes rely on an additional file that contains various glyph + metrics, including kerning, but no glyph images. A good example would + be the Type 1 format, where glyph images are stored in a file + with extension .pfa or .pfb, and where kerning + metrics can be found in an additional file with extension + .afm or .pfm.

+ +

FreeType 2 allows you to deal with this by providing the + FT_Attach_File() and FT_Attach_Stream() APIs. Both + functions are used to load additional metrics into a face object, by + reading them from an additional format-specific file. For example, + you could open a Type 1 font by doing the following:

+ + +
+    error = FT_New_Face( library,
+                         "/usr/shared/fonts/cour.pfb",
+                         0,
+                         &face );
+    if ( error ) { ... }
+
+    error = FT_Attach_File( face, "/usr/shared/fonts/cour.afm" );
+    if ( error ) { .. could not read kerning and additional metrics .. }
+
+ +

Note that FT_Attach_Stream() is similar to + FT_Attach_File() except that it doesn't take a C string + to name the extra file, but a FT_Stream handle. Also, + reading a metrics file is in no way mandatory.

+ +

Finally, the file attachment APIs are very generic and can be used + to load any kind of extra information for a given face. The nature of + the additional data is entirely font format specific.

+ +

FreeType 2 allows you to retrieve the kerning information + between two glyphs through the FT_Get_Kerning() function, + whose interface looks like

+ + +
+    FT_Vector  kerning;
+
+
+    ...
+    error = FT_Get_Kerning(
+              face,              /* handle to face object */
+              left,              /* left glyph index      */
+              right,             /* right glyph index     */
+              kerning_mode,      /* kerning mode          */
+              &kerning );        /* target vector         */
+
+ +

As can be seen, the function takes a handle to a face object, the + indices of the left and right glyphs for which the kerning value is + desired, as well as an integer, called the kerning mode, and + a pointer to a destination vector that receives the corresponding + distances.

+ +

The kerning mode is very similar to the bbox mode + described in a previous part. It is an enumeration value that + indicates how the kerning distances are expressed in the target + vector.

+ +

The default value ft_kerning_mode_default (which has + value 0) corresponds to kerning distances expressed in 26.6 + grid-fitted pixels (which means that the values are multiples + of 64). For scalable formats, this means that the design kerning + distance is scaled, then rounded.

+ +

The value ft_kerning_mode_unfitted corresponds to kerning + distances expressed in 26.6 unfitted pixels (i.e. that do not + correspond to integer coordinates). It is the design kerning distance + that is scaled without rounding.

+ +

Finally, the value ft_kerning_mode_unscaled is used to + return the design kerning distance, expressed in font units. You can + later scale it to device space using the computations explained in the + last chapter of this part.

+ +

Note that the "left" and "right" positions correspond to the + visual order of the glyphs in the string of text. This is + important for bi-directional text, or simply when writing + right-to-left text.

+ +
+ +

+ 4. Simple text rendering: kerning + centering +

+ +

In order to show off what we have just learned, we will now show how + to modify the example code that was provided in the first part to render + a string of text, and enhance it to support kerning and delayed + rendering.

+ +

+ a. Kerning support +

+ +

Adding support for kerning to our code is trivial, as long as we + consider that we are still dealing with a left-to-right script like + Latin. We simply need to retrieve the kerning distance between two + glyphs in order to alter the pen position appropriately. The code + looks like

+ + +
+    FT_GlyphSlot  slot = face->glyph;  /* a small shortcut */
+    FT_UInt       glyph_index;
+    FT_Bool       use_kerning;
+    FT_UInt       previous;
+    int           pen_x, pen_y, n;
+
+
+    .. initialize library ..
+    .. create face object ..
+    .. set character size ..
+
+    pen_x = 300;
+    pen_y = 200;
+
+    use_kerning = FT_HAS_KERNING( face );
+    previous    = 0;
+
+    for ( n = 0; n < num_chars; n++ )
+    {
+      /* convert character code to glyph index */
+      glyph_index = FT_Get_Char_Index( face, text[n] );
+
+      /* retrieve kerning distance and move pen position */
+      if ( use_kerning && previous && glyph_index )
       {
-        // convert character code to glyph index
-        glyph_index = FT_Get_Char_Index( face, text[n] );
-        
-        // retrieve kerning distance and move pen position
-        if ( use_kerning && previous && glyph_index )
-        {
-          FT_Vector  delta;
-          
-          FT_Get_Kerning( face, previous, glyph_index,
-                          ft_kerning_mode_default, &delta );
-                          
-          pen_x += delta.x >> 6;
-        }
-      
-        // load glyph image into the slot (erase previous one)
-        error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER );
-        if (error) continue;  // ignore errors
-        
-        // now, draw to our target surface
-        my_draw_bitmap( &slot->bitmap,
-                        pen_x + slot->bitmap_left,
-                        pen_y - slot->bitmap_top );
-                        
-        // increment pen position
-        pen_x += slot->advance.x >> 6;
-        
-        // record current glyph index
-        previous = glyph_index
+        FT_Vector  delta;
+
+
+        FT_Get_Kerning( face, previous, glyph_index,
+                        ft_kerning_mode_default, &delta );
+
+        pen_x += delta.x >> 6;
       }
-   
-

That's it. You'll notice that:

- -
    -
  • - As kerning is determined from glyph indices, we need to explicitely - convert our character code into a glyph index, then later call - FT_Load_Glyph instead of FT_Load_Char. No big - deal, if you ask me :-) -

  • - -
  • - We use a boolean named use_kerning which is set with the - result of the macro FT_HAS_KERNING(face). It's - certainly faster not to call FT_Get_Kerning when we know - that the font face does not contain kerning information. -

  • - -
  • - We move the position of the pen before a new glyph is drawn. -

  • - -
  • - We did initialize the variable previous with the value 0, - which always correspond to the "missing glyph" (also called - .notdef in the Postscript world). There is never any - kerning distance associated with this glyph. -

  • - -
  • - We do not check the error code returned by FT_get_Kerning. - This is because the function always set the content of delta - to (0,0) when an error occurs. -

  • -
+ /* load glyph image into the slot (erase previous one) */ + error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER ); + if ( error ) continue; /* ignore errors */ -

As you see, this is not terribly complex :-)

+ /* now, draw to our target surface */ + my_draw_bitmap( &slot->bitmap, + pen_x + slot->bitmap_left, + pen_y - slot->bitmap_top ); -

- b. Centering: -

+ /* increment pen position */ + pen_x += slot->advance.x >> 6; -

Our code begins to become interesting but it's still a bit too simple - for normal uses. For example, the position of the pen is determined - before we do the rendering when in a normal situation, you would want + /* record current glyph index */ + previous = glyph_index; + }

+
+ +

We are done. Notice that

+ +
    +
  • + As kerning is determined from glyph indices, we need to + explicitly convert our character code into a glyph index, then + later call FT_Load_Glyph() instead of + FT_Load_Char(). +
  • +
  • + We use a Boolean named use_kerning which is set with the + result of the macro FT_HAS_KERNING(face). It is + certainly faster not to call FT_Get_Kerning() if we + know that the font face does not contain kerning information. +
  • +
  • + We move the position of the pen before a new glyph is + drawn. +
  • +
  • + We did initialize the variable previous with the + value 0, which always correspond to the missing + glyph (also called .notdef in the PostScript world). + There is never any kerning distance associated with this glyph. +
  • +
  • + We do not check the error code returned by + FT_Get_Kerning(). This is because the function always + set the content of delta to (0,0) when an error occurs. +
  • +
+ +

+ b. Centering +

+ +

Our code becomes more interesting but it is still a bit too simple + for normal uses. For example, the position of the pen is determined + before we do the rendering if in a real-life situation; you would want to layout the text and measure it before computing its final position (e.g. centering) or perform things like word-wrapping.

- -

We're thus now going to decompose our text rendering function into two - distinct but successive parts: the first one will position individual - glyph images on the baseline, while the second one will render the - glyphs. As we'll see, this has many advantages.

- -

We will thus start by storing individual glyph images, as well as their - position on the baseline. This can be done with code like:

- -
-      FT_GlyphSlot  slot = face->glyph;  // a small shortcut
-      FT_UInt       glyph_index;
-      FT_Bool       use_kerning;
-      FT_UInt       previous;
-      int           pen_x, pen_y, n;
-      
-      FT_Glyph      glyphs[ MAX_GLYPHS ];   // glyph image
-      FT_Vector     pos   [ MAX_GLYPHS ];   // glyph position
-      FT_UInt       num_glyphs;
 
-      .. initialise library ..
-      .. create face object ..
-      .. set character size ..
-      
-      pen_x = 0;   /* start at (0,0) !! */
-      pen_y = 0;
-      
-      num_glyphs  = 0;
-      use_kerning = FT_HAS_KERNING(face);
-      previous    = 0;
-      
-      for ( n = 0; n < num_chars; n++ )
+      

As a consequence we are now going to decompose our text rendering + function into two distinct but successive parts: The first one will + position individual glyph images on the baseline, while the second one + will render the glyphs. As will be shown, this has many + advantages.

+ +

We start by storing individual glyph images, as well as their + position on the baseline. This can be done with code like

+ + +
+    FT_GlyphSlot  slot = face->glyph;  /* a small shortcut */
+    FT_UInt       glyph_index;
+    FT_Bool       use_kerning;
+    FT_UInt       previous;
+    int           pen_x, pen_y, n;
+
+    FT_Glyph      glyphs[MAX_GLYPHS];  /* glyph image    */
+    FT_Vector     pos   [MAX_GLYPHS];  /* glyph position */
+    FT_UInt       num_glyphs;
+
+
+    .. initialize library ..
+    .. create face object ..
+    .. set character size ..
+
+    pen_x = 0;   /* start at (0,0)! */
+    pen_y = 0;
+
+    num_glyphs  = 0;
+    use_kerning = FT_HAS_KERNING( face );
+    previous    = 0;
+
+    for ( n = 0; n < num_chars; n++ )
+    {
+      /* convert character code to glyph index */
+      glyph_index = FT_Get_Char_Index( face, text[n] );
+
+      /* retrieve kerning distance and move pen position */
+      if ( use_kerning && previous && glyph_index )
       {
-        // convert character code to glyph index
-        glyph_index = FT_Get_Char_Index( face, text[n] );
-        
-        // retrieve kerning distance and move pen position
-        if ( use_kerning && previous && glyph_index )
-        {
-          FT_Vector  delta;
-          
-          FT_Get_Kerning( face, previous, glyph_index,
-                          ft_kerning_mode_default, &delta );
-                          
-          pen_x += delta.x >> 6;
-        }
-      
-        // store current pen position
-        pos[ num_glyphs ].x = pen_x;
-        pos[ num_glyphs ].y = pen_y;
-      
-        // load glyph image into the slot. DO NOT RENDER IT !!
-        error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
-        if (error) continue;  // ignore errors, jump to next glyph
-        
-        // extract glyph image and store it in our table
-        error = FT_Get_Glyph( face->glyph, & glyphs[num_glyphs] );
-        if (error) continue;  // ignore errors, jump to next glyph
-        
-        // increment pen position
-        pen_x += slot->advance.x >> 6;
-        
-        // record current glyph index
-        previous = glyph_index
-        
-        // increment number of glyphs
-        num_glyphs++;
+        FT_Vector  delta;
+
+
+        FT_Get_Kerning( face, previous, glyph_index,
+                        ft_kerning_mode_default, &delta );
+
+        pen_x += delta.x >> 6;
       }
-   
- -

As you see, this is a very simple variation of our previous code + + /* store current pen position */ + pos[num_glyphs].x = pen_x; + pos[num_glyphs].y = pen_y; + + /* load glyph image into the slot. DO NOT RENDER IT! */ + error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT ); + if ( error ) continue; /* ignore errors, jump to next glyph */ + + /* extract glyph image and store it in our table */ + error = FT_Get_Glyph( face->glyph, &glyphs[num_glyphs] ); + if ( error ) continue; /* ignore errors, jump to next glyph */ + + /* increment pen position */ + pen_x += slot->advance.x >> 6; + + /* record current glyph index */ + previous = glyph_index; + + /* increment number of glyphs */ + num_glyphs++; + }

+
+ +

As you see, this is a very simple variation of our previous code where we extract each glyph image from the slot, and store it, along with the corresponding position, in our tables.

- -

Note also that "pen_x" contains the total advance for the string of - text. We can now compute the bounding box of the text string with - a simple function like:

- - -
-      void   compute_string_bbox( FT_BBox  *abbox )
-      {
-        FT_BBox  bbox;
-        
-        // initialise string bbox to "empty" values
-        bbox.xMin = bbox.yMin =  32000;
-        bbox.xMax = bbox.yMax = -32000;
-        
-        // for each glyph image, compute its bounding box, translate it,
-        // and grow the string bbox
-        for ( n = 0; n < num_glyphs; n++ )
-        {
-          FT_BBox   glyph_bbox;
 
-          FT_Glyph_Get_CBox( glyphs[n], &glyph_bbox );
+      

Note also that pen_x contains the total advance for the + string of text. We can now compute the bounding box of the text + string with a simple function like

- glyph_bbox.xMin += pos[n].x; - glyph_bbox.xMax += pos[n].x; - glyph_bbox.yMin += pos[n].y; - glyph_bbox.yMax += pos[n].y; + +
+    void  compute_string_bbox( FT_BBox*  abbox )
+    {
+      FT_BBox  bbox;
 
-          if (glyph_bbox.xMin < bbox.xMin)
-            bbox.xMin = glyph_bbox.xMin;
 
-          if (glyph_bbox.yMin < bbox.yMin)
-            bbox.yMin = glyph_bbox.yMin;
+      /* initialize string bbox to "empty" values */
+      bbox.xMin = bbox.yMin =  32000;
+      bbox.xMax = bbox.yMax = -32000;
 
-          if (glyph_bbox.xMax > bbox.xMax)
-            bbox.xMax = glyph_bbox.xMax;
-
-          if (glyph_bbox.yMax &gy; bbox.yMax)
-            bbox.yMax = glyph_bbox.yMax;
-        }
-        
-        // check that we really grew the string bbox
-        if ( bbox.xMin > bbox.xMax )
-        {
-          bbox.xMin = 0;
-          bbox.yMin = 0;
-          bbox.xMax = 0;
-          bbox.yMax = 0;
-        }
-        
-        // return string bbox
-        *abbox = bbox;
-      }
-   
- -

The resulting bounding box dimensions can then be used to compute the - final pen position before rendering the string as in:

- -
-      // compute string dimensions in integer pixels
-      string_width  = (string_bbox.xMax - string_bbox.xMin)/64;
-      string_height = (string_bbox.yMax - string_bbox.yMin)/64;
-   
-      // compute start pen position in 26.6 cartesian pixels
-      start_x = (( my_target_width  - string_width )/2)*64;
-      start_y = (( my_target_height - string_height)/2)*64;
-      
+      /* for each glyph image, compute its bounding box, */
+      /* translateit, and grow the string bbox           */
       for ( n = 0; n < num_glyphs; n++ )
       {
-        FT_Glyph  image;
-        FT_Vector pen;
-        
-        image = glyphs[n];
-        
-        pen.x = start_x + pos[n].x;
-        pen.y = start_y + pos[n].y;
-        
-        error = FT_Glyph_To_Bitmap( &image, ft_render_mode_normal,
-                                    &pen.x, 0 );
-        if (!error)
-        {
-          FT_BitmapGlyph  bit = (FT_BitmapGlyph)image;
-          
-          my_draw_bitmap( bitmap->bitmap,
-                          bitmap->left,
-                          my_target_height - bitmap->top );
-                          
-          FT_Done_Glyph( image );
-        }
+        FT_BBox  glyph_bbox;
+
+
+        FT_Glyph_Get_CBox( glyphs[n], &glyph_bbox );
+
+        glyph_bbox.xMin += pos[n].x;
+        glyph_bbox.xMax += pos[n].x;
+        glyph_bbox.yMin += pos[n].y;
+        glyph_bbox.yMax += pos[n].y;
+
+        if ( glyph_bbox.xMin < bbox.xMin )
+          bbox.xMin = glyph_bbox.xMin;
+
+        if ( glyph_bbox.yMin < bbox.yMin )
+          bbox.yMin = glyph_bbox.yMin;
+
+        if ( glyph_bbox.xMax > bbox.xMax )
+          bbox.xMax = glyph_bbox.xMax;
+
+        if ( glyph_bbox.yMax &gy; bbox.yMax )
+          bbox.yMax = glyph_bbox.yMax;
       }
-   
- -

You'll take note that:

- -
    -
  • - The pen position is expressed in the cartesian space (i.e. Y upwards). -

  • - -
  • - We call FT_Glyph_To_Bitmap with the destroy - parameter set to 0 (false), in order to avoid destroying the original - glyph image. The new glyph bitmap is accessed through image - after the call and is typecasted to a FT_BitmapGlyph. -

  • - -
  • - We use translation when calling FT_Glyph_To_Bitmap. This - ensures that the left and top fields - of the bitmap glyph object are already set to the correct pixel - coordinates in the cartesian space. -

  • - -
  • - Of course, we still need to convert pixel coordinates from cartesian - to device space before rendering, hence the my_target_height - - bitmap->top in the call to my_draw_bitmap. -

  • - -
- -

The same loop can be used to render the string anywhere on our display - surface, without the need to reload our glyph images each time.. We - could also decide to implement word wrapping, and only draw

-
-

- 5. Advanced text rendering: transform + centering + kerning: -

- -

We are now going to modify our code in order to be able to easily - transform the rendered string, for example to rotate it. We will - start by performing a few minor improvements:

- -

a. packing & translating glyphs:

- -

We'll start by packing the information related to a single glyph image - into a single structure, instead of parallel arrays. We thus define the - following structure type:

- -
-     typedef struct TGlyph_
-     {
-       FT_UInt    index;    // glyph index
-       FT_Vector  pos;      // glyph origin on the baseline
-       FT_Glyph   image;    // glyph image
-     
-     } TGlyph, *PGlyph;
-  
- -

We will also translate each glyph image directly after it is loaded - to its position on the baseline at load time. As we'll see, this - as several advantages. Our glyph sequence loader thus becomes:

- -
-      FT_GlyphSlot  slot = face->glyph;  // a small shortcut
-      FT_UInt       glyph_index;
-      FT_Bool       use_kerning;
-      FT_UInt       previous;
-      int           pen_x, pen_y, n;
-      
-      TGlyph        glyphs[ MAX_GLYPHS ];   // glyphs table
-      PGlyph        glyph;                  // current glyph in table
-      FT_UInt       num_glyphs;
-
-      .. initialise library ..
-      .. create face object ..
-      .. set character size ..
-      
-      pen_x = 0;   /* start at (0,0) !! */
-      pen_y = 0;
-      
-      num_glyphs  = 0;
-      use_kerning = FT_HAS_KERNING(face);
-      previous    = 0;
-
-      glyph = glyphs;      
-      for ( n = 0; n < num_chars; n++ )
+      /* check that we really grew the string bbox */
+      if ( bbox.xMin > bbox.xMax )
       {
-        glyph->index = FT_Get_Char_Index( face, text[n] );
-        
-        if ( use_kerning && previous && glyph->index )
-        {
-          FT_Vector  delta;
-          
-          FT_Get_Kerning( face, previous, glyph->index,
-                          ft_kerning_mode_default, &delta );
-                          
-          pen_x += delta.x >> 6;
-        }
-      
-        // store current pen position
-        glyph->pos.x = pen_x;
-        glyph->pos.y = pen_y;
-      
-        error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
-        if (error) continue;
-        
-        error = FT_Get_Glyph( face->glyph, &glyph->image );
-        if (error) continue;
-        
-        // translate the glyph image now..
-        FT_Glyph_Transform( glyph->image, 0, &glyph->pos );
-        
-        pen_x   += slot->advance.x >> 6;
-        previous = glyph->index
-        
-        // increment number of glyphs
-        glyph++;
+        bbox.xMin = 0;
+        bbox.yMin = 0;
+        bbox.xMax = 0;
+        bbox.yMax = 0;
       }
-      // count number of glyphs loaded..
-      num_glyphs = glyph - glyphs;
-   
-

Note that translating glyphs now has several advantages. The first - one, is that we don't need to translate the glyph bbox when we compute - the string's bounding box. The code becomes:

+ /* return string bbox */ + *abbox = bbox; + }
+
-
-      void   compute_string_bbox( FT_BBox  *abbox )
+      

The resulting bounding box dimensions can then be used to compute + the final pen position before rendering the string as in:

+ + +
+    /* compute string dimensions in integer pixels */
+    string_width  = ( string_bbox.xMax - string_bbox.xMin ) / 64;
+    string_height = ( string_bbox.yMax - string_bbox.yMin ) / 64;
+
+    /* compute start pen position in 26.6 cartesian pixels */
+    start_x = ( ( my_target_width  - string_width  ) / 2 ) * 64;
+    start_y = ( ( my_target_height - string_height ) / 2 ) * 64;
+
+    for ( n = 0; n < num_glyphs; n++ )
+    {
+      FT_Glyph  image;
+      FT_Vector pen;
+
+
+      image = glyphs[n];
+
+      pen.x = start_x + pos[n].x;
+      pen.y = start_y + pos[n].y;
+
+      error = FT_Glyph_To_Bitmap( &image, ft_render_mode_normal,
+                                  &pen.x, 0 );
+      if ( !error )
       {
-        FT_BBox  bbox;
-        
-        bbox.xMin = bbox.yMin =  32000;
-        bbox.xMax = bbox.yMax = -32000;
-        
-        for ( n = 0; n < num_glyphs; n++ )
-        {
-          FT_BBox   glyph_bbox;
+        FT_BitmapGlyph  bit = (FT_BitmapGlyph)image;
 
-          FT_Glyph_Get_CBox( glyphs[n], &glyph_bbox );
 
-          if (glyph_bbox.xMin < bbox.xMin)
-            bbox.xMin = glyph_bbox.xMin;
+        my_draw_bitmap( bitmap->bitmap,
+                        bitmap->left,
+                        my_target_height - bitmap->top );
 
-          if (glyph_bbox.yMin < bbox.yMin)
-            bbox.yMin = glyph_bbox.yMin;
-
-          if (glyph_bbox.xMax > bbox.xMax)
-            bbox.xMax = glyph_bbox.xMax;
-
-          if (glyph_bbox.yMax &gy; bbox.yMax)
-            bbox.yMax = glyph_bbox.yMax;
-        }
-        
-        if ( bbox.xMin > bbox.xMax )
-        {
-          bbox.xMin = 0;
-          bbox.yMin = 0;
-          bbox.xMax = 0;
-          bbox.yMax = 0;
-        }
-        
-        *abbox = bbox;
+        FT_Done_Glyph( image );
       }
-   
+ }
+
-

Now take a closer look, the compute_string_bbox can now - compute the bounding box of a transformed glyph string. For example, - we can do something like:

- -

-      FT_BBox    bbox;
-      FT_Matrix  matrix;
-      FT_Vector  delta;
-   
-      ... load glyph sequence
-      
-      ... setup "matrix" and "delta"
-      
-      // transform glyphs
-      for ( n = 0; n < num_glyphs; n++ )
-        FT_Glyph_Transform( glyphs[n].image, &matrix, &delta );
-      
-      // compute bounding box of transformed glyphs
-      compute_string_bbox( &bbox );
-   
+

Some remarks:

+ +
    +
  • + The pen position is expressed in the cartesian space (i.e. + Y upwards). +
  • +
  • + We call FT_Glyph_To_Bitmap() with the destroy + parameter set to 0 (false), in order to avoid destroying the + original glyph image. The new glyph bitmap is accessed through + image after the call and is typecasted to a + FT_BitmapGlyph. +
  • +
  • + We use translation when calling FT_Glyph_To_Bitmap(). + This ensures that the left and top fields of the + bitmap glyph object are already set to the correct pixel + coordinates in the cartesian space. +
  • +
  • + Of course, we still need to convert pixel coordinates from + cartesian to device space before rendering, hence the + my_target_height - bitmap->top in the call to + my_draw_bitmap(). +
  • +
+ +

The same loop can be used to render the string anywhere on our + display surface, without the need to reload our glyph images each + time.

+ +
+ +

+ 5. Advanced text rendering: transformation + centering + kerning +

+ +

We are now going to modify our code in order to be able to easily + transform the rendered string, for example to rotate it. We will start + by performing a few minor improvements:

+ +

+ a. packing & translating glyphs +

+ +

We start by packing the information related to a single glyph image + into a single structure instead of parallel arrays. We thus define + the following structure type:

+ + +
+    typedef struct  TGlyph_
+    {
+      FT_UInt    index;    /* glyph index                  */
+      FT_Vector  pos;      /* glyph origin on the baseline */
+      FT_Glyph   image;    /* glyph image                  */
+
+     } TGlyph, *PGlyph;
+
+ +

We will also translate each glyph image directly after it is loaded + to its position on the baseline at load time, which has several + advantages. Our glyph sequence loader thus becomes:

+ + +
+    FT_GlyphSlot  slot = face->glyph;  /* a small shortcut */
+    FT_UInt       glyph_index;
+    FT_Bool       use_kerning;
+    FT_UInt       previous;
+    int           pen_x, pen_y, n;
+
+    TGlyph        glyphs[MAX_GLYPHS];  /* glyphs table           */
+    PGlyph        glyph;               /* current glyph in table */
+    FT_UInt       num_glyphs;
+
+
+    .. initialize library ..
+    .. create face object ..
+    .. set character size ..
+
+    pen_x = 0;   /* start at (0,0)! */
+    pen_y = 0;
+
+    num_glyphs  = 0;
+    use_kerning = FT_HAS_KERNING( face );
+    previous    = 0;
+
+    glyph = glyphs;
+    for ( n = 0; n < num_chars; n++ )
+    {
+      glyph->index = FT_Get_Char_Index( face, text[n] );
+
+      if ( use_kerning && previous && glyph->index )
+      {
+        FT_Vector  delta;
+
+
+        FT_Get_Kerning( face, previous, glyph->index,
+                        ft_kerning_mode_default, &delta );
+
+        pen_x += delta.x >> 6;
+      }
+
+      /* store current pen position */
+      glyph->pos.x = pen_x;
+      glyph->pos.y = pen_y;
+
+      error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
+      if ( error ) continue;
+
+      error = FT_Get_Glyph( face->glyph, &glyph->image );
+      if ( error ) continue;
+
+      /* translate the glyph image now */
+      FT_Glyph_Transform( glyph->image, 0, &glyph->pos );
+
+      pen_x   += slot->advance.x >> 6;
+      previous = glyph->index
+
+      /* increment number of glyphs */
+      glyph++;
+    }
+
+    /* count number of glyphs loaded */
+    num_glyphs = glyph - glyphs;
+
+ +

Translating glyphs now has several advantages, as mentioned + earlier. The first one is that we don't need to translate the glyph + bounding box when we compute the string's bounding box. The code + becomes:

+ + +
+    void  compute_string_bbox( FT_BBox*  abbox )
+    {
+      FT_BBox  bbox;
+
+
+      bbox.xMin = bbox.yMin =  32000;
+      bbox.xMax = bbox.yMax = -32000;
 
-  

- b. Rendering a transformed glyph sequence: -

- -

However, directly transforming the glyphs in our sequence is not an idea - if we want to re-use them in order to draw the text string with various - angles or transforms. It's better to perform the affine transformation - just before the glyph is rendered, as in the following code:

- -
-      FT_Vector  start;
-      FT_Matrix  transform;
-   
-      // get bbox of original glyph sequence
-      compute_string_bbox( &string_bbox );
-   
-      // compute string dimensions in integer pixels
-      string_width  = (string_bbox.xMax - string_bbox.xMin)/64;
-      string_height = (string_bbox.yMax - string_bbox.yMin)/64;
-   
-      // set up start position in 26.6 cartesian space
-      start.x = (( my_target_width  - string_width )/2)*64;
-      start.y = (( my_target_height - string_height)/2)*64;
-      
-      // set up transform (a rotation here)
-      matrix.xx = (FT_Fixed)( cos(angle)*0x10000);
-      matrix.xy = (FT_Fixed)(-sin(angle)*0x10000);
-      matrix.yx = (FT_Fixed)( sin(angle)*0x10000);
-      matrix.yy = (FT_Fixed)( cos(angle)*0x10000);
-      
       for ( n = 0; n < num_glyphs; n++ )
       {
-        FT_Glyph  image;
-        FT_Vector pen;
-        FT_BBox   bbox;
-        
-        // create a copy of the original glyph
-        error = FT_Glyph_Copy( glyphs[n].image, &image );
-        if (error) continue;
+        FT_BBox  glyph_bbox;
 
-        // transform copy (this will also translate it to the correct
-        // position
-        FT_Glyph_Transform( image, &matrix, &start );
-        
-        // check bounding box, if the transformed glyph image
-        // is not in our target surface, we can avoid rendering it
-        FT_Glyph_Get_CBox( image, ft_glyph_bbox_pixels, &bbox );
-        if ( bbox.xMax <= 0 || bbox.xMin >= my_target_width  ||
-             bbox.yMax <= 0 || bbox.yMin >= my_target_height )
-          continue;
-        
-        // convert glyph image to bitmap (destroy the glyph copy !!)
-        //
-        error = FT_Glyph_To_Bitmap( &image,
-                                    ft_render_mode_normal,
-                                    0,      // no additional translation
-                                    1 );    // destroy copy in "image"
-        if (!error)
-        {
-          FT_BitmapGlyph  bit = (FT_BitmapGlyph)image;
-          
-          my_draw_bitmap( bitmap->bitmap,
-                          bitmap->left,
-                          my_target_height - bitmap->top );
-                          
-          FT_Done_Glyph( image );
-        }
+
+        FT_Glyph_Get_CBox( glyphs[n], &glyph_bbox );
+
+        if ( glyph_bbox.xMin < bbox.xMin )
+          bbox.xMin = glyph_bbox.xMin;
+
+        if ( glyph_bbox.yMin < bbox.yMin )
+          bbox.yMin = glyph_bbox.yMin;
+
+        if ( glyph_bbox.xMax > bbox.xMax )
+          bbox.xMax = glyph_bbox.xMax;
+
+        if ( glyph_bbox.yMax &gy; bbox.yMax )
+          bbox.yMax = glyph_bbox.yMax;
       }
-   
-

You'll notice a few changes compared to the original version of this + if ( bbox.xMin > bbox.xMax ) + { + bbox.xMin = 0; + bbox.yMin = 0; + bbox.xMax = 0; + bbox.yMax = 0; + } + + *abbox = bbox; + }

+
+ +

compute_string_bbox() can now compute the bounding box of + a transformed glyph string. For example, we can do something + like

+ + +
+    FT_BBox    bbox;
+    FT_Matrix  matrix;
+    FT_Vector  delta;
+
+
+    ... load glyph sequence
+
+    ... setup "matrix" and "delta"
+
+    /* transform glyphs */
+    for ( n = 0; n < num_glyphs; n++ )
+      FT_Glyph_Transform( glyphs[n].image, &matrix, &delta );
+
+    /* compute bounding box of transformed glyphs */
+    compute_string_bbox( &bbox );
+
+ +

+ b. Rendering a transformed glyph sequence +

+ +

However, directly transforming the glyphs in our sequence is not a + useful idea if we want to re-use them in order to draw the text string + with various angles or transforms. It is better to perform the affine + transformation just before the glyph is rendered, as in the following code:

- -
    -
  • - We keep the original glyph images untouched, by transforming a - copy. -

  • - -
  • - We perform clipping computations, in order to avoid rendering & - drawing glyphs that are not within our target surface -

  • - -
  • - We always destroy the copy when calling FT_Glyph_To_Bitmap - in order to get rid of the transformed scalable image. Note that - the image is destroyed even when the function returns an error - code (which is why FT_Done_Glyph is only called within - the compound statement. -

  • - -
  • - The translation of the glyph sequence to the start pen position is - integrated in the call to FT_Glyph_Transform intead of - FT_Glyph_To_Bitmap. -

  • -
-

It's possible to call this function several times to render the string - width different angles, or even change the way "start" is computed in - order to move it to different place.

+ +
+    FT_Vector  start;
+    FT_Matrix  transform;
 
-  

This code is the basis of the FreeType 2 demonstration program - named"ftstring.c". It could be easily extended to perform - advanced text layout or word-wrapping in the first part, without - changing the second one.

- -

Note however that a normal implementation would use a glyph cache in - order to reduce memory needs. For example, let's assume that our text - string is "FreeType". We would store three identical glyph images in - our table for the letter "e", which isn't optimal (especially when you - consider longer lines of text, or even whole pages..). -

-
- -

- 6. Accessing metrics in design font units, and scaling them: -

+ /* get bbox of original glyph sequence */ + compute_string_bbox( &string_bbox ); -

Scalable font formats usually store a single vectorial image, called - an "outline", for each in a face. Each outline is defined in an abstract - grid called the "design space", with coordinates expressed in nominal - "font units". When a glyph image is loaded, the font driver usually - scales the outline to device space according to the current character - pixel size found in a FT_Size object. The driver may also - modify the scaled outline in order to significantly improve its - appearance on a pixel-based surface (a process known as "hinting" - or "grid-fitting").

- -

This chapter describes how design coordinates are scaled to device - space, and how to read glyph outlines and metrics in font units. This - is important for a number of things:

- -
    -
  • - In order to perform "true" WYSIWYG text layout -

  • - -
  • - In order to access font content for conversion or analysis purposes -

  • -
+ /* compute string dimensions in integer pixels */ + string_width = ( string_bbox.xMax - string_bbox.xMin ) / 64; + string_height = ( string_bbox.yMax - string_bbox.yMin ) / 64; -

a.Scaling distances to device space:

- -

Design coordinates are scaled to device space using a simple scaling - transform, whose coefficients are computed with the help of the - character pixel size:

- -

-     device_x = design_x * x_scale
-     device_y = design_y * y_scale
+    /* set up start position in 26.6 cartesian space */
+    start.x = ( ( my_target_width  - string_width  ) / 2 ) * 64;
+    start.y = ( ( my_target_height - string_height ) / 2 ) * 64;
 
-     x_scale  = pixel_size_x / EM_size
-     y_scale  = pixel_size_y / EM_size
-  
+ /* set up transformation (a rotation here) */ + matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); + matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L ); + matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); + matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); -

Here, the value EM_size is font-specific and correspond - to the size of an abstract square of the design space (called the "EM"), - which is used by font designers to create glyph images. It is thus - expressed in font units. It is also accessible directly for scalable - font formats as face->units_per_EM. You should - check that a font face contains scalable glyph images by using the - FT_IS_SCALABLE(face) macro, which returns true when - appropriate.

+ for ( n = 0; n < num_glyphs; n++ ) + { + FT_Glyph image; + FT_Vector pen; + FT_BBox bbox; -

When you call the function FT_Set_Pixel_Sizes, you're - specifying the value of pixel_size_x and pixel_size_y - you want to use to FreeType, which will immediately compute the values - of x_scale and y_scale.

-

When you call the function FT_Set_Char_Size, you're - specifying the character size in physical "points", which is used, - along with the device's resolutions, to compute the character pixel - size, then the scaling factors.

+ /* create a copy of the original glyph */ + error = FT_Glyph_Copy( glyphs[n].image, &image ); + if ( error ) continue; -

Note that after calling any of these two functions, you can access - the values of the character pixel size and scaling factors as fields - of the face->size->metrics structure. These fields are:

- -
- -
- x_ppem - -

Which stands for "X Pixels Per EM", this is the size in integer pixels - of the EM square, which also is the horizontal character pixel size, - called pixel_size_x in the above example.

-
- y_ppem - -

Which stands for "Y Pixels Per EM", this is the size in integer pixels - of the EM square, which also is the vertical character pixel size, - called pixel_size_y in the above example.

-
- x_scale - -

This is a 16.16 fixed float scale that is used to directly - scale horizontal distances from design space to 1/64th of device pixels. -

-
- y_scale - -

This is a 16.16 fixed float scale that is used to directly scale - vertical distances from design space to 1/64th of device pixels.

-
-
+ /* transform copy (this will also translate it to the */ + /* correct position */ + FT_Glyph_Transform( image, &matrix, &start ); -

Basically, this means that you can scale a distance expressed in - font units to 26.6 pixels directly with the help of the FT_MulFix - function, as in:

- -

-      // convert design distances to 1/64th of pixels
-      //
-      pixels_x = FT_MulFix( design_x, face->size->metrics.x_scale );
-      pixels_y = FT_MulFix( design_y, face->size->metrics.y_scale );
-  
- -

However, you can also scale the value directly with more accuracy - by using doubles and the equations:

- -

-      FT_Size_Metrics*  metrics = &face->size->metrics;    // shortcut
-      double            pixels_x, pixels_y;
-      double            em_size, x_scale, y_scale;
+      /* check bounding box -- if the transformed glyph image */
+      /* is not in our target surface, we can avoid rendering */
+      FT_Glyph_Get_CBox( image, ft_glyph_bbox_pixels, &bbox );
+      if ( bbox.xMax <= 0 || bbox.xMin >= my_target_width  ||
+           bbox.yMax <= 0 || bbox.yMin >= my_target_height )
+        continue;
 
-      // compute floating point scale factors
-      //
-      em_size = 1.0 * face->units_per_EM;
-      x_scale = metrics->x_ppem / em_size;
-      y_scale = metrics->y_ppem / em_size;
-      
-      // convert design distances to floating point pixels
-      //
-      pixels_x = design_x * x_scale;
-      pixels_y = design_y * y_scale;
-  
- -

- b. Accessing design metrics (glyph & global): -

- -

You can access glyph metrics in font units simply by specifying the - FT_LOAD_NO_SCALE bit flag in FT_Load_Glyph - or FT_Load_Char. The metrics returned in - face->glyph->metrics will all be in font units.

- -

You can access unscaled kerning data using the - ft_kerning_mode_unscaled mode

+ /* convert glyph image to bitmap (destroy the glyph */ + /* copy!) */ + error = FT_Glyph_To_Bitmap( + &image, + ft_render_mode_normal, + 0, /* no additional translation */ + 1 ); /* destroy copy in "image" */ + if ( !error ) + { + FT_BitmapGlyph bit = (FT_BitmapGlyph)image; -

Finally, a few global metrics are available directly in font units - as fields of the FT_Face handle, as described in chapter 3 - of this section.

- -
- -

- Conclusion -

- -

This is the end of the second section of the FreeType 2 tutorial, - you're now able to access glyph metrics, manage glyph images, and - render text much more intelligently (kerning, measuring, transforming - & caching).

- -

You have now sufficient knowledge to build a pretty decent text service - on top of FreeType 2, and you could possibly stop there if you want.

- -

The next section will deal with FreeType 2 internals (like modules, - vector outlines, font drivers, renderers), as well as a few font format - specific issues (mainly, how to access certain TrueType or Type 1 tables). -

+ + my_draw_bitmap( bitmap->bitmap, + bitmap->left, + my_target_height - bitmap->top ); + + FT_Done_Glyph( image ); + } + }
+
+ +

There are a few changes compared to the previous version of this + code:

+ +
    +
  • + We keep the original glyph images untouched, by transforming a + copy. +
  • +
  • + We perform clipping computations in order to avoid rendering & + drawing glyphs that are not within our target surface. +
  • +
  • + We always destroy the copy when calling + FT_Glyph_To_Bitmap() in order to get rid of the + transformed scalable image. Note that the image is destroyed even + when the function returns an error code (which is why + FT_Done_Glyph() is only called within the compound + statement). +
  • +
  • + The translation of the glyph sequence to the start pen position is + integrated in the call to FT_Glyph_Transform() intead of + FT_Glyph_To_Bitmap(). +
  • +
+ +

It is possible to call this function several times to render the + string width different angles, or even change the way start + is computed in order to move it to different place.

+ +

This code is the basis of the FreeType 2 demonstration program + named ftstring.c. It could be easily extended to perform + advanced text layout or word-wrapping in the first part, without + changing the second one.

+ +

Note however that a normal implementation would use a glyph cache + in order to reduce memory consumption. For example, let us assume + that our text string to render is "FreeType". We would store three + identical glyph images in our table for the letter "e", which isn't + optimal (especially when you consider longer lines of text, or even + whole pages).

+ +
+ +

+ 6. Accessing metrics in design font units, and scaling them +

+ +

Scalable font formats usually store a single vectorial image, called + an outline, for each in a face. Each outline is defined in an + abstract grid called the design space, with coordinates + expressed in nominal font units. When a glyph image is loaded, + the font driver usually scales the outline to device space according to + the current character pixel size found in a FT_Size object. + The driver may also modify the scaled outline in order to significantly + improve its appearance on a pixel-based surface (a process known as + hinting or grid-fitting).

+ +

This section describes how design coordinates are scaled to device + space, and how to read glyph outlines and metrics in font units. This + is important for a number of things:

+ +
    +
  • +

    to perform "true" WYSIWYG text layout.

    +
  • +
  • +

    to access font data for conversion or analysis purposes

    +
  • +
+ +

+ a. Scaling distances to device space +

+ +

Design coordinates are scaled to device space using a simple + scaling transformation whose coefficients are computed with the help + of the character pixel size:

+ + +
+    device_x = design_x * x_scale
+    device_y = design_y * y_scale
+
+    x_scale  = pixel_size_x / EM_size
+    y_scale  = pixel_size_y / EM_size
+
+ +

Here, the value EM_size is font-specific and corresponds + to the size of an abstract square of the design space (called the + "EM"), which is used by font designers to create glyph images. It is + thus expressed in font units. It is also accessible directly for + scalable font formats as face->units_per_EM. You should + check that a font face contains scalable glyph images by using the + FT_IS_SCALABLE(face) macro, which returns true when the font + is scalable.

+ +

When you call the function FT_Set_Pixel_Sizes(), you are + specifying the value of pixel_size_x and + pixel_size_y; FreeType will then immediately compute the + values of x_scale and y_scale.

+ +

When you call the function FT_Set_Char_Size(), you are + specifying the character size in physical "points", which is used, + along with the device's resolutions, to compute the character pixel + size, then the scaling factors.

+ +

Note that after calling any of these two functions, you can access + the values of the character pixel size and scaling factors as fields + of the face->size->metrics structure. These fields are:

+ +
+ + + + + + + + + + + + + + + + + +
+ x_ppem + + Which stands for "X Pixels Per EM"; this is the size in integer + pixels of the EM square, which also is the horizontal + character pixel size, called pixel_size_x in the + above example. +
+ y_ppem + + Which stands for "Y Pixels Per EM"; this is the size in integer + pixels of the EM square, which also is the vertical character + pixel size, called pixel_size_y in the above + example. +
+ x_scale + + This is a 16.16 fixed float scale that is used to directly scale + horizontal distances from design space to 1/64th of device pixels. +
+ y_scale + + This is a 16.16 fixed float scale that is used to directly scale + vertical distances from design space to 1/64th of device pixels. +
+
+ +

Basically, this means that you can scale a distance expressed in + font units to 26.6 pixels directly with the help of the + FT_MulFix() function, as in:

+ + +
+    /* convert design distances to 1/64th of pixels */
+    pixels_x = FT_MulFix( design_x, face->size->metrics.x_scale );
+    pixels_y = FT_MulFix( design_y, face->size->metrics.y_scale );
+
+ +

However, you can also scale the value directly with more accuracy + by using doubles and the equations:

+ + +
+    FT_Size_Metrics*  metrics = &face->size->metrics;  /* shortcut */
+    double            pixels_x, pixels_y;
+    double            em_size, x_scale, y_scale;
+
+
+    /* compute floating point scale factors */
+    em_size = 1.0 * face->units_per_EM;
+    x_scale = metrics->x_ppem / em_size;
+    y_scale = metrics->y_ppem / em_size;
+
+    /* convert design distances to floating point pixels */
+    pixels_x = design_x * x_scale;
+    pixels_y = design_y * y_scale;
+
+ +

+ b. Accessing design metrics (glyph & global) +

+ +

You can access glyph metrics in font units simply by specifying the + FT_LOAD_NO_SCALE bit flag in FT_Load_Glyph() or + FT_Load_Char(). The metrics returned in + face->glyph->metrics will then all be in font units.

+ +

You can access unscaled kerning data using the + ft_kerning_mode_unscaled mode.

+ +

Finally, a few global metrics are available directly in font units + as fields of the FT_Face handle, as described in + section 3 of this tutorial part.

+ +
+ +

+ Conclusion +

+ +

This is the end of the second part of the FreeType 2 tutorial; + you are now able to access glyph metrics, manage glyph images, and + render text much more intelligently (kerning, measuring, transforming + & caching).

+ +

You have now sufficient knowledge to build a pretty decent text + service on top of FreeType 2, and you could possibly stop there if + you want.

+ +

The next section will deal with FreeType 2 internals (like + modules, vector outlines, font drivers, renderers), as well as a few + font format specific issues (mainly, how to access certain TrueType or + Type 1 tables).