[cairo] Font extents mysteries

Tim Teulings rael at edge.ping.de
Thu May 10 11:46:24 PDT 2007


Hello!

>> Am I doing something wrong? Is there an error in my code or am I
>> basing by code on wrong assumptions? 
> 
> The reason you see this behavior is that you are measuring with a
> default font options object, but rendering to a surface that indeed has
> non-default options (in this case, metrics hinting on).  Instead you can
> use what cairo_surface_get_font_options() returns.

Thank you for your fast help! Your suggestion sounded reasonable so I
added a

        cairo_surface_get_font_options(surface,options);

after the creation of the cairo_font_options_t during
cairo_scaled_font_t initialization. So the code in question is now:

        cairo_select_font_face(cairo,name.c_str(),
                               CAIRO_FONT_SLANT_NORMAL,
                               CAIRO_FONT_WEIGHT_NORMAL);

        cairo_font_options_t *options;

        [Matrix initialisation code...]

        options=cairo_font_options_create();
        cairo_surface_get_font_options(surface,options);
        fontScaled=cairo_scaled_font_create(cairo_get_font_face(cairo),
                                                           &scaleMatrix,
                                                         &transformMatrix,
                                                           options);

        cairo_font_options_destroy(options);			

This however did not help. The effect is still the same. So some more
background:

The surface and cairo object I use during initialization of scaled_font
are not identical to that I use for later drawing. That made your hint
so reasonable :-)

The framework creates an hidden X11 window during startup and a
corresponding surface cairo_xlib_surface_create. The cairo_t object for
this surface is used for initial scaled font creation (and that is the
reason why I use scaled fonts: to cache all possible styles of some
always used default fix and proportional font). Later on I draw into
other X11 windows (surfaces also created by cairo_xlib_surface_create)
and offscreen bitmaps (using
cairo_surface_create_similar(globalSurface,CAIRO_CONTENT_COLOR,width,height)
on the global surface for the global, hidden X11 window). The
corresponding cairo_t objects and then later used for drawing text as
described. I however added some debugging code to proof that the
font_options object returned by cairo_surface_get_font_options have the
same values for the global surface and the later drawing surfaces!

Antialias 3
Subpixels 1
Hintstyle 4
Hintmetrics 2

(Removing the font_options initialization code indeed shows, that the
font options values are than different as assumed).

So this cannot (at least after I added the missing initialization you
mentioned) be the cause and in fact by box drawing code still show
differences and the same given words.

> But you really don't need to go that far (unless you are doing something
> special).  All you need to do is to use cairo_text_extents() right after
> cairo_select_font_face().  No need to deal with scaled fonts explicitly.

As told above I create scaled font objects to cache frequently used
font, font_style, font_size combinations.

> Hope it helps and you are having fun with cairo!

The library is of high quality, the performance increase in the 1.4
version is stunning and the response time for help request is
astonishing :-)

I'm currently fixing some last missing problems and then plan to switch
my few applications for the Nokia 800 to use cairo as drawing layer -
dropping existing X11 code (besides using the Gtk theming engine for
most of the drawing however :-/). The freedom of having its own GUI
library :-)

-- 
Gruß...
       Tim



More information about the cairo mailing list