[cairo] Newbie questions on fonts

Baz brian.ewins at gmail.com
Sun Oct 14 16:47:28 PDT 2007

On 12/10/2007, Fred Clare <fred at ucar.edu> wrote:
> I have just started using cairo.  I have followed the thread
> in the cairo archives of late December 2006 on "Problems
> compiling Cairo..doesn't detect fontconfig/freetype
> installation" and, in particular, I installed into a
> private directory (say /home/myhome/cairo) on a Mac OS X machine:
>    freetype-2.3.5
>    fontconfig-2.4.2
>    cairo-1.4.10

Its a bit easier on the mac if you just use macports. That would have
installed all this in /opt/local, and the fonts in
/opt/local/etc/fonts. I presume fink works well too, installing things
in /sw. You should at least look at the macports builds, any patches
they apply are going to be of interest to you. The portfile for
freetype, for instance:
... shows that they ship it patched; although in this case the patch
was to better support Japanese, and you might not need it.

> I have put /home/myhome/cairo/lib on my DYLD_LIBRARY_PATH and
> /home/myhome/cairo/lib/pkgconfig on my PKG_CONFIG_PATH as well as
> putting /home/myhome/cairo/bin on my PATH.

... while I do bang on a bit about macports, it is perfectly
reasonable to build things yourself like this, don't let me put you

> Some questions:
>    1.)  Just what are the "toy" fonts?  What font database is
>         being used for those?  When I execute:

Its not toy fonts, but a toy api. To do 'real' work you're going to
want to use a better font selection mechanism, and a better way of
converting text to glyphs. The toy api lets you get text output across
all platforms, but its limited in what it can do and so is really only
appropriate for test programs and examples. One of the limitations is
that you can have multiple font backends compiled, but the toy api
will choose one for you.

BTW for brevity below I'm going to say 'atsui toy api' where I mean
'toy api with the atsui font backend enabled', and freetype toy api
for where I mean 'toy api with the freetype font backend enabled and
the atsui font backend disabled'... save a few electrons...

>           cairo_select_font_face (cr, "serif",
>           cairo_show_text (cr, "Text");
>         I do not get an Italic font, but just the normal font.

This is one of the limitations of the atsui toy api. It sounds like
you enabled that in your build (you don't say what flags you used?).
However there are other ways this could have gone wrong.

You ask what fonts those are: it depends which backend the toy api
uses, the choices of font mappings vary slightly. Theres support in
(most? all?) backends for the css generic family names "serif",
"sans", "mono" mapping to fonts; with the freetype toy api the font
name can also be a font family name, while with the atsui toy api
we're limited to using the actual font name.

On any platform, the toy api will not normally report  a failure to
select a font, but will try to use a fallback font of some kind -
which is another explanation for what you saw.

>    2.)  Installing fontconfig creates an /etc/fonts directory (I wish
>         the documentation of fontconfig was specific about that) and

That's configurable at build time, with --sysconfdir.

>         lists some font directories in the fonts.conf file in
>         that directory.  There are some fonts in /Library/fonts
>         that I would like to access and the directory
>         /Library/fonts is not listed in fonts.conf.  The directive
>         in fonts.conf says not to edit that file but put local
>         changes in a 'local.conf' file which I did by inserting:
>            <dir>/Library/Fonts</dir>
>         in an xml file created using the fonts.conf file as a template.
>         I was hoping that this would then give me access to the
>         fonts in /Library/Fonts using the font name in a
>         cairo_select_font_face call with the font name as the second
>         argument.  But a subsequent call to cairo_show_text
>         just gives me the default serif font.

Its hard to tell if this problem is because you have enabled atsui, or
have a misconfigured freetype, or something else. I'd suggest
narrowing down the causes - install freetype + fontconfig using
macports, and only then build your own cairo with no atsui. That way
most of the setup will be in a known working state. Next, run the
cairo tests (make test; open test/index.html) to see if your copy of
cairo works before trying your own programs.

Also the behaviour here can vary somewhat with your choice of font -
it would help to begin with if you try to select one of the Bitstream
Vera family; opening a font that freetype has problems with - eg
selecting italic from a .dfont font - just going to confuse things.
Try switching between 'normal' faces of two fonts first.

>         Should the above work?  Is my assumption correct that cairo
>         is looking at the files in /etc/fonts to find its fonts?
>         Since the above did not work, I put the line
>            <dir>/Library/Fonts</dir>

The macports version has:

but yes this should work.

>         in fonts.conf, but that did not work either.
>    3.)  Since the above did not work, I tried accessing the fonts
>         directly using the calls:
>             error = FT_Init_FreeType( &library );
>             error = FT_New_Face( library,
>                       "/Library/Fonts/ArialHBBold.ttf", 0, &face );
>             font_face = cairo_ft_font_face_create_for_ft_face(face,0);
>             cairo_set_font_face(cr, font_face);
>         I did not get any error returns, but a show_text call just
>         produces rectangles.

Don't know about this one. Again, it might be best to get a working
freetype/fontconfig setup before we try to diagnose your cairo issues.

> Any help will be appreciated.

No problem - hope this is useful.


> Fred Clare
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo

More information about the cairo mailing list