[cairo] text measuring speed

Bill Spitzak spitzak at d2.com
Wed Jun 15 09:51:47 PDT 2005

It seems to me that the way FreeType does the hinting is perhaps 
desirable and Cairo should copy it.

It would be very useful to know that changing the CTM would result in 
the exact same image, only transformed. Thus the CTM should not affect 
the font hinting, because the result is not a scaled image.

Therefore, why not have only the font transformation matrix affect the 

The only disadvantage I can think of is that programs would have to set 
the CTM to approximately the default in order to get correct hinting. 
However I feel this is how most programs operate anyway and this is thus 
not a huge problem.

The advantages are:

A text-formatting program wanting to produce a reduced-size version of 
it's output currently has to get all the font metric information at the 
actual output size, and then reuse that information to lay out the text 
in the smaller page, otherwise the word wrap and justification will 
change. This requires two cairo_t's, which can get real annoying if 
Pango or some other intermediate library has not been set up to do this. 
With this change the same cairo_t can be used to measure and print.

The CTM is completely irrelevant to font metric information and 
therefore it can be cached and returned with no transformation, 
resulting in (apparently) a great deal of speed up.

The font transform could be limited in scope to avoid questionable 
metrics informations. Translation is already disallowed, with this 
scheme that would be more consistent. Vertical skew could also be 
disallowed, so that rotation must be done using the CTM, eliminating a 
lot of mysteries in what the font metric information contains and also 
eliminating the growing bounding box problem. I could even see removing 
horizontal skew, though that is probably not a good idea.

Keith Packard wrote:

> 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
> user.
> 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.

More information about the cairo mailing list