FreeType 2 Tutorial
Step 2 — managing glyphs

© 2009 David Turner (david@freetype.org)
© 2009 The FreeType Development Team (www.freetype.org)


Introduction

This is the second section of the FreeType 2 tutorial. It describes how to

  • retrieve glyph metrics
  • easily manage glyph images
  • retrieve global metrics (including kerning)
  • render a simple string of text, with kerning
  • render a centered string of text (with kerning)
  • render a transformed string of text (with centering)
  • access metrics in design font units when needed, and how to scale them to device space

1. Glyph metrics

Glyph 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:

width

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

height

This is the height of the glyph image's bounding box. It is independent of the layout direction. Be careful not to confuse it with the ‘height’ field in the FT_Size_Metrics structure.

horiBearingX

For horizontal text layouts, this is the horizontal distance from the current cursor position to the leftmost 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 topmost 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 leftmost 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 topmost 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.

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:

horizontal layout

For vertical text layouts, the baseline is vertical, identical to the vertical axis:

vertical layout

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:

advance

This field is a FT_Vector which holds the transformed advance for the glyph. That is useful when you are 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 similar to linearHoriAdvance but 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. 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:

  • Created a variable, named glyph, of type