IntroductionThis is the second section of the FreeType 2 tutorial. It describes how to
1. Glyph metricsGlyph metrics are, as their name suggests, certain distances associated with 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 (Latin, Cyrillic, Arabic, Hebrew, etc.), and those used to layout the glyph in vertical text layouts (Chinese, Japanese, Korean, etc.). Note that only a few font formats provide vertical metrics. You can test whether a given face object contains them by using the macro FT_HAS_VERTICAL, 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, whose type is FT_Glyph_Metrics. We will discuss this in more detail below; for now, we only note that it contains the following fields:
NOTE: As not all fonts do contain vertical metrics, the values of vertBearingX, vertBearingY and vertAdvance should not be considered reliable when FT_HAS_VERTICAL is false. 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, identical to the vertical axis: ![]() The metrics found in face->glyph->metrics are normally 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 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 through face->glyph->xxx, where xxx is one of the following fields:
2. Managing glyph imagesThe 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 situations, 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 FT_GLYPH_H header file, as in:
#include FT_GLYPH_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; /* a handle to the glyph image */
...
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NORMAL );
if ( error ) { ... }
error = FT_Get_Glyph( face->glyph, &glyph );
if ( error ) { ... }
As you see, we have:
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 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. The glyph object contains exactly one glyph image and a 2D vector representing the glyph's advance in 16.16 fixed-point coordinates. The latter can be accessed directly as glyph->advance Note that unlike other FreeType objects, the library doesn't keep a list of all allocated glyph objects. This means you have to destroy them yourself instead of relying on FT_Done_FreeType doing all the clean-up. b. Transforming & copying the glyph imageIf 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 is 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 pixel format */
delta.y = 50 * 64;
FT_Glyph_Transform( glyph, 0, &delta );
/* transform glyph2 (horizontal shear) */
matrix.xx = 0x10000L;
matrix.xy = 0.12 * 0x10000L;
matrix.yx = 0;
matrix.yy = 0x10000L;
FT_Glyph_Transform( glyph2, &matrix, 0 );
Note that the 2×2 transform matrix is always applied to the 16.16 advance vector in the glyph; you thus don't need to recompute it. c. Measuring the glyph imageYou can also retrieve the control (bounding) box of any glyph image (scalable or not) through the FT_Glyph_Get_CBox function, as in:
FT_BBox bbox;
...
FT_Glyph_Get_CBox( glyph, bbox_mode, &bbox );
Coordinates are relative to the glyph origin (0,0), using the y upwards convention. This function takes a special argument, the bbox mode, to indicate how box coordinates are expressed. If the glyph has been loaded with FT_LOAD_NO_SCALE, bbox_mode must be set to FT_GLYPH_BBOX_UNSCALED to get unscaled font units in 26.6 pixel format. The value FT_GLYPH_BBOX_SUBPIXELS is another name for this constant. Note that the 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 used as 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 )
To get the bbox in integer pixel coordinates, set bbox_mode to FT_GLYPH_BBOX_TRUNCATE. Finally, to get the bounding box in grid-fitted pixel coordinates, set bbox_mode to FT_GLYPH_BBOX_PIXELS. d. Converting the glyph image to a bitmapYou 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. It is in charge of converting any glyph object into a bitmap, as in:
FT_Vector origin;
origin.x = 32; /* 1/2 pixel in 26.6 format */
origin.y = 0;
error = FT_Glyph_To_Bitmap(
&glyph,
render_mode,
&origin,
1 ); /* destroy original image == true */
Some notes:
The new glyph object always contains a bitmap (if 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 (see FT_BitmapGlyphRec):
3. Global glyph metricsUnlike glyph metrics, global metrics 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. a. Design global metricsFor scalable formats, all global metrics are expressed in font units in order to be later scaled to the 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 which returns true when appropriate. In this case, you can access the global design metrics as:
Notice how, unfortunately, the values of the ascender and the descender are not reliable (due to various discrepancies in font formats). b. Scaled global metricsEach 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 or 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.
Note that the face->size->metrics structure contains other fields that are used to scale design coordinates to device space. They are described in the last chapter. c. KerningKerning 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, and not all kerning formats are supported by FreeType; in particular, for TrueType fonts, the API can only access kerning via the ‘kern’ table; OpenType kerning via the ‘GPOS’ table is not supported. You need a higher-level library like Pango or ICU to handle that. Sometimes, the font file is associated with an additional file that contains various glyph metrics, including kerning, but no glyph images. A good example is 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 a 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 content is entirely font format specific. FreeType 2 allows you to retrieve the kerning information for 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 you see, the function takes a handle to a face object, the indices of the left and right glyph 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 is a enumeration that indicates how the kerning distances are expressed in the target vector. The default value is FT_KERNING_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_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_UNSCALED is used to return the design kerning distance, expressed in font units. You can later scale it to the device space using the computations explained in the last chapter of this section. Note that the ‘left’ and ‘right’ positions correspond to the visual order of the glyphs in the string of text. This is important for bidirectional text, or simply when writing right-to-left text. 4. Simple text rendering: kerning + centeringIn order to show off what we just learned, we will now demonstrate 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. a. Kerning supportAdding 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 )
{
FT_Vector delta;
FT_Get_Kerning( face, previous, glyph_index,
FT_KERNING_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;
}
We are done. Some notes:
b. CenteringOur code begins to become interesting but it is still a bit too simple for normal use. For example, the position of the pen is determined before we do the rendering; normally, you would rather layout the text and measure it before computing its final position (centering, etc.) or perform things like word-wrapping. Let us now 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 will 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;
... 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 )
{
FT_Vector delta;
FT_Get_Kerning( face, previous, glyph_index,
FT_KERNING_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 without rendering */
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++;
}
This is a very slight 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;
FT_BBox glyph_bbox;
/* initialize 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_Glyph_Get_CBox( glyphs[n], ft_glyph_bbox_pixels,
&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 > 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 are expressed in integer pixels and 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;
string_height = string_bbox.yMax - string_bbox.yMin;
/* 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, 0 );
if ( !error )
{
FT_BitmapGlyph bit = (FT_BitmapGlyph)image;
my_draw_bitmap( bit->bitmap,
bit->left,
my_target_height - bit->top );
FT_Done_Glyph( image );
}
}
Some remarks:
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: transformation + centering + kerningWe 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 glyphsWe 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 also translate each glyph image directly after it is loaded to its position on the baseline at load time. As we will see, this 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;
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:
void compute_string_bbox( FT_BBox *abbox )
{
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_Glyph_Get_CBox( glyphs[n], ft_glyph_bbox_pixels,
&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 > 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;
}
Now take a closer look: The compute_string_bbox function 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 ...
... set up "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 sequenceHowever, directly transforming the glyphs in our sequence is not a good idea if we want to reuse them in order to draw the text string with various angles or transformations. It is better to perform the affine transformation just before the glyph is rendered, as in the following code:
FT_Vector start;
FT_Matrix matrix;
FT_Glyph image;
FT_Vector pen;
FT_BBox bbox;
/* 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 ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
pen = start;
for ( n = 0; n < num_glyphs; n++ )
{
/* create a copy of the original glyph */
error = FT_Glyph_Copy( glyphs[n].image, &image );
if ( error ) continue;
/* transform copy (this will also translate it to the */
/* correct position */
FT_Glyph_Transform( image, &matrix, &pen );
/* 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( bit->bitmap,
bit->left,
my_target_height - bit->top );
/* increment pen position -- */
/* we don't have access to a slot structure, */
/* so we have to use advances from glyph structure */
/* (which are in 16.16 fixed float format) */
pen.x += image.advance.x >> 10;
pen.y += image.advance.y >> 10;
FT_Done_Glyph( image );
}
}
There are a few changes compared to the original version of this code:
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 needs. For example, let us assume that our text string is ‘FreeType&rsquo'. 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 themScalable font formats usually store a single vectorial image, called an outline, for each glyph 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 the device space, and how to read glyph outlines and metrics in font units. This is important for a number of things:
a. Scaling distances to device spaceDesign coordinates are scaled to the 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 macro, which returns true when appropriate. When you call the function FT_Set_Pixel_Sizes, you are specifying the value of pixel_size_x and pixel_size_y FreeType shall use. The library will 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 and the corresponding 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:
You can scale a distance expressed in font units to 26.6 pixel format 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:
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 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 chapter 3 of this section. ConclusionThis is the end of the second section 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 here if you want. The demo programs in the ‘ft2demos’ bundle (especially ‘ftview’) are a kind of reference implementation, and are a good resource to turn to for answers. They also show how to use additional features, such as the glyph stroker and cache. 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). |
Last update: 10-Apr-2013