[cairo] Newbie questions on fonts

Fred Clare fred at ucar.edu
Mon Oct 15 19:10:18 PDT 2007


Hi Björn,

Thanks for your response.

On Oct 14, 2007, at 7:09 PM, BJörn Lindqvist wrote:

> Hello!
>
> On 10/12/07, Fred Clare <fred at ucar.edu> wrote:
>>    freetype-2.3.5
>>    fontconfig-2.4.2
>>    cairo-1.4.10
>>
>> 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.
>>
>> Some questions:
>>
>>    1.)  Just what are the "toy" fonts?  What font database is
>>         being used for those?  When I execute:
>
> I think this thread might provide some clues:
>
> http://lists.freedesktop.org/archives/cairo/2007-August/011133.html

Very good.  I think I understand this now.
>
> The font database comes from the platforms font database. On OS X that
> probably is ATSUI even if you have FreeType installed because Cairo
> prefers that. You can of course force the issue using:
>
> ./configure --enable-freetype=yes --enable-atsui=no

This is what I  did since I am trying to develop an application that
I want to port to several systems and I want to understand what an
implementation from scratch takes, and I do not want any Mac specific
stuff in there.  Here is the relevant output from my configure run:

   the following font backends:
     FreeType:      yes
    Win32:         no (requires a Win32 platform)
    ATSUI:         no (disabled, use --enable-atsui to enable)


>
>>           cairo_select_font_face (cr, "serif",
>>                    CAIRO_FONT_SLANT_ITALIC,  
>> CAIRO_FONT_WEIGHT_NORMAL);
>>           cairo_show_text (cr, "Text");
>>
>>         I do not get an Italic font, but just the normal font.
>
> Same here. It seems like cairo manages to pick a face without italic
> glyphs for serif. Try another family like monospace or sans.

"sans" does give me the Italic and oblique styles.

>
>>         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.
>
> Have you updated the fontconfigs font cache?

No, I was not even aware of such.

> fc-cache -fv will at
> least tell you which directories fontconfig looks in.

It looks like the above command does do the caching.  It reports
that it cashed 97 fonts in my /Library/Fonts directory.  It is helpful
to see that the /Library/Fonts directory is being recognized.  I just
can't seem to access the fonts there.

>
>>         Should the above work?  Is my assumption correct that cairo
>>         is looking at the files in /etc/fonts to find its fonts?
>
> Cairo doesn't look for fonts at all. It lets the font backend handle
> that.

OK, got it.

>
>>    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);
>
> This code works fine for me (on Ubuntu Linux). My uneducated guess is
> that your cairo library uses ATSUI and not FreeType. You should also
> of course check so that the FT functions doesn't throw any error..

I have done this - no errors.

> Maybe there is also some encoding issue involved? You often get
> "rectangles" when that happens.

How would one pursue looking for an encoding problem?  As you can
see I am quite a novice when it comes to this.

Can you see anything obviously wrong with my complete code?:

#include <math.h>
#include <stdio.h>
#include <cairo.h>
#include <cairo-ps.h>
#include <cairo-ft.h>

#define FILENAME "text_test.ps"

int main (int argc, char *argv[]) {

   FT_Library library;
   FT_Face    face;
   int        error;

   cairo_font_face_t *font_face;

   cairo_surface_t *surface;
   cairo_t *cr;

   FILE *file;
   file = fopen (FILENAME, "w");
   if (file == NULL) {
     fprintf (stderr, "Failed to open file %s for writing.\n",  
FILENAME);
     return 1;
   }

   error = FT_Init_FreeType( &library );
   if ( error )
     {
       printf("Error initializing FreeType library\n");
       return 1;
     }

   error = FT_New_Face( library,
                        "/Library/Fonts/ArialHBBold.ttf",
                        0,
                        &face );
   if ( error == FT_Err_Unknown_File_Format )
   {
       printf("The font file could be opened and read, \n but it  
appears that its font format is unsupported\n");
       return 1;
   }
   else if ( error )
   {
       printf("Font file could not be opened or otherwise is broken. 
\n");
       return 1;
   }

   font_face = cairo_ft_font_face_create_for_ft_face(face,0);

   surface = cairo_ps_surface_create (FILENAME, 600, 600);
   cr = cairo_create (surface);
   cairo_surface_destroy(surface);

   cairo_set_font_face(cr, font_face);
   cairo_set_font_size (cr, 30);
   cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);

   cairo_move_to (cr, 200, 100);
   cairo_show_text (cr, "Magnetic");

   cairo_stroke(cr);
   cairo_show_page(cr);
   cairo_destroy(cr);
   fclose(file);

   return 0;
}

Thanks much.

Fred Clare

>
>
> -- 
> mvh Björn



More information about the cairo mailing list