[cairo] text measuring speed

Keith Packard keithp at keithp.com
Sat Jun 11 14:31:00 PDT 2005

On Sat, 2005-06-11 at 14:27 -0400, Jon Smirl wrote:

> If I need a glyph transformed into a non-rectangular region who does
> the transform Cairo or do you ask the back end to generate a
> transformed glyph? Or does the answer vary with the backend?

The current architecture has the font backend generating the glyph
images for pixel-based devices in device space given the transformations
created by the application.  This is necessary for these pixel-based
devices as glyph hinting occurs in pixel space and requires additional
data from the font file (either descriptive or instructed hints) along
with a significant amount of hinting code.  We could get the outlines
from the backend and rasterize them with cairo, but we don't.  Right
now, the FreeType rasterizer is probably more efficient for tiny glyph
images than the cairo rasterizer.

There are three coordinate spaces within cairo:

     1. Device space: pixel coordinates for pixel devices, points for
        other devices.
     2. User space: Defaults to identity, changed with 'translate,
        rotate, scale' operators.  Used to construct a suitable
        coordinate space for the application data.
     3. Font space: Allows fonts to be scaled independently of user
        space so you can create a '12 point' font and still use whatever
        coordinate space the application wants to position the glyphs.
        This also allows for shearing to construct artificial oblique
        glyphs or even rotation of the glyphs within the user space.

As with all of the cairo API, the glyph extents functions return values
in user space.  Right now, the backend font functions provide metrics in
font space, so a transformation is needed to convert the font-space
bounding box to a user-space bounding box.

All layout is done above the cairo layer by Pango or other layout
libraries.  This is far more complex than can be described by mere
kerning information as can be seen by the size of the implementation of
these various layout systems.

One problem with the bounding boxes is that they are defined to be
upright rectangles in their respective coordinate spaces.  Take a
bounding box and run it through an affine transform and you get a
diamond in the new coordinate system.  Construct a bounding box to
contain that diamond and note that it will be larger than the diamond.
Thus, we must always keep bounding boxes in the original glyph
coordinate system to make sure it is as close to the glyph bounds as

In FreeType, there are two separate transformations applied to each
glyph.  The first is a simple scaling transform which adjusts the X and
Y dimensions to fit the desired pixel dimensions.  After this scaling
operation, FreeType applies whatever hinting algorithm is specified to
adjust the outlines themselves.

Once the outlines are hinted at the desired pixel size, a separate
transformation matrix can be applied to the outline to rotate, shear or
even rescale the outline.

Xft uses this separate matrix to scale glyphs by 3 in the X dimension
when doing sub-pixel decimated text drawing.  It uses this post-hinting
matrix so that the glyph hints operate in full pixel space rather than
sub-pixel space.  

Cairo takes the two transformation matrices (font-space -> user-space
and user-space -> device-space) and multiplies them together.  From this
combined transformation matrix, it separates the scaling portion and the
reshaping portion and hands these to FreeType, the scaling being applied
pre-hinting and the reshaping (shear, mirror, rotate) afterwards.  This
ensures that the glyph appearance on the screen is correctly hinted at
the specified pixel size independently of how that pixel size was
arrived at as a combination of the two transformations applied by the

Because hints affect glyphs metrics, cairo must use post-hinted metrics
for measuring text, this means using the metrics of the glyphs after the
hinting and scaling operation.  It can not, however, use pure
device-space metrics as those will have the reshaping transformation
applied and hence be no longer aligned with the nominal font-space
baseline of the glyphs.

However, the separation of the combined transformation matrices is not
exposed above the font layer, and so it cannot store metrics in the
post-hinted space either.  Instead, it takes the post-hinted metrics and
scales them by the inverse of the scaling portion of the combined
transformation.  This provides pure font-space metrics which can be
converted to an arbitrary user space by the simple application of the
font-space -> user-space transformation matrix.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050611/d2084cb2/attachment.pgp

More information about the cairo mailing list