[cairo] How to check if a font exists in Cairo and Pango?

Bill Spitzak spitzak at gmail.com
Wed Apr 10 10:10:50 PDT 2013


This code dumps the list of Pango fonts, and I believe the same strings 
are usable as toy fonts:


#include "cairot.h"
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <math.h>
#include <stdio.h>

// PangoLayout* layout = pango_cairo_create_layout(cairo_t);

void dump_stuff(PangoLayout* layout) {
   PangoContext* context = pango_layout_get_context(layout);
   PangoFontFamily** families; int n;
   pango_context_list_families(context, &families, &n);
   printf("%d families:\n", n);
   for (int i=0; i < n; i++) {
     PangoFontFamily* family = families[i];
     if (!PANGO_IS_FONT_FAMILY(family)) {
       printf("a %s\n", G_OBJECT_TYPE_NAME(family));
       continue;
     }
     printf("%s\n", pango_font_family_get_name(family));
     PangoFontFace** faces; int m;
     pango_font_family_list_faces(family, &faces, &m);
     for (int j = 0; j < m; j++) {
       PangoFontFace* face = faces[j];
       printf("  %s", pango_font_face_get_face_name(face));
       int* sizes; int nsizes;
       pango_font_face_list_sizes(face, &sizes, &nsizes);
       if (sizes) {
	for (int k = 0; k < nsizes; ++k) printf(" %d", sizes[k]);
	g_free(sizes);
       }
       if (pango_font_face_is_synthesized(face)) printf(" *");
       printf("\n");
     }
     g_free(faces);
   }
   g_free(families);
}


On 04/10/2013 09:01 AM, Ken Resander wrote:
> My application should check if a font exists and if it does not try the
> next font preference obtained from the user, a bit like processing a
> font-choice list in CSS.
>
> Cairo:
> const char * str = "nosuchfont";
> cairo_font_face_t * ff =
>   cairo_toy_font_face_create ( str ,
>                                CAIRO_FONT_SLANT_NORMAL ,
>                                CAIRO_FONT_WEIGHT_NORMAL ) ;
> const char * toyfamilyis = cairo_toy_font_face_get_family ( ff );
>
> The toyfamilyis was returned as 'nosuchfont'. The manual mentions that
> toy_font_face_create and the cairo_font_select_face do not report back
> whether the font exists or not. A pity. If it returned NULL for a
> non-existent choice then my program could loop through the user preferences.
>
> I also tried pango:
> PangoFontDescription * pfd = pango_font_description_from_string ( str );
> const char * familyis = pango_font_description_get_family(pfd);
>
> This time familyis was also returned as 'nosuchfont'.
>
> I have read somewhere that pango can tell if a font exists.
>
> How can I make it return this information?
>
> Ken
>
>



More information about the cairo mailing list