[cairo] Pango+Cario : proper font rendering under Win32

Timothée Lecomte timothee.lecomte at ens.fr
Thu Jan 5 04:09:41 PST 2006


Dear Pango/Cairo enthusiast,

I have written a terminal to gnuplot (http://www.gnuplot.info) which 
draws thanks to cairo, and handles text via pango... and I have some 
questions about font rendering.

I use a generic cairo "image" surface, and create my text layouts with 
pango_cairo_create_layout(cairo_t*) or 
pango_cairo_font_map_get_default() plus other calls.
I choose my font with :

desc = pango_font_description_new ();
pango_font_description_set_family (desc, (const char*) plot->fontname);
pango_font_description_set_size (desc, (int) (plot->fontsize*PANGO_SCALE) );

where plot->fontname is a char* containing the desired font name, 
usually Sans, Verdana or other usual fonts.

Under Linux, the result is good, the fonts are properly antialiased.
However, under MS-Windows (with binary snapshots of 
cairo1.0.2/pango1.10.1 distributed at 
ftp://ftp.gtk.org/pub/gtk/v2.8/win32/), the same code gives uglier 
results, with black text "over-antialiased" with a subpixel method.

Here are screenshots :
http://tipote.free.fr/wxt15.png (linux, various fonts, result satisfying)
http://tipote.free.fr/wxt_windows4.png (windows, using font "Sans", size 15)

I have tried to modify manually the way antialiasing is set. I use :

    PangoFontMap* fontmap = pango_cairo_font_map_get_default();
    PangoContext* context = 
pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP(fontmap));

    desc = pango_font_description_new ();
    pango_font_description_set_family (desc, (const char*) plot->fontname);
    pango_font_description_set_size (desc, (int) 
(plot->fontsize*PANGO_SCALE) );
    pango_context_set_font_description (context, desc);
    pango_font_description_free (desc);
   
    cairo_font_options_t* options = cairo_font_options_create();
    cairo_font_options_set_antialias(options,CAIRO_ANTIALIAS_GRAY);
    cairo_font_options_set_hint_style(options,CAIRO_HINT_STYLE_FULL);
    cairo_font_options_set_hint_metrics(options,CAIRO_HINT_METRICS_ON);
    cairo_font_options_set_subpixel_order(options,CAIRO_SUBPIXEL_ORDER_BGR);
    pango_cairo_context_set_font_options(context, options);
   
    layout = pango_layout_new (context);
(...)

With this code, my settings (cairo_font_options_set_...) are not taken 
into account unless I hardcode the font in the file with, for example :
    pango_font_description_set_family (desc, "Sans");
I think it is a bug...

If I use a description_from_string, the settings are taken into account 
properly :
    desc = pango_font_description_from_string ("Sans 20");
    pango_context_set_font_description (context, desc);
    pango_font_description_free (desc);

So that's a first point : how to make the font options taken into 
account with pango_font_description_set_family (desc, (const char*) 
plot->fontname)) ???
(note that plot->fontname contains exactly "Sans" or other font names, 
so the problem is not in it)


Then, even when hardcoding the fontfamily and trying to tweak the font 
options, I did not manage to get perfect results.
With CAIRO_ANTIALIAS_GRAY, I observed that there's no antialiasing below 
font size = 14, for "Sans".
With CAIRO_ANTIALIAS_SUBPIXEL, I get the same unsatisfying results as 
reported above and illustrated in the screenshot.
Other settings (hint_style and hint_metrics) don't give noticeable 
changes to my eyes.

So here's the second point : how to get correct antialiasing, such as 
gray antialiasing even at low font sizes, or subpixel antialiasing but 
more accurate compared to the general appearance of fonts under Windows ?

To give you a whole idea, here is a screenshot of the whole screen, 
where you can compare the fonts drawn under cairo/pango with 
subpixel_antialiasing, hint_style=full, and fonts in Thunderbird, which 
are also drawn with subpixel antialiasing, but really more readable :

http://tipote.free.fr/wxt_windows5.png

I hope some of you may be able to give me advice.

Thanks for your consideration and time, and thanks for the great work 
already done on pango and cairo !

Timothée Lecomte



More information about the cairo mailing list