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

Ken Resander kresander at yahoo.com
Thu Apr 11 04:55:58 PDT 2013


Many thanks Bill for the very useful information.

I will use it to check that family, style and weight are available for a choice. Would have liked to include size (in pts) too, but it (psz) was always returned as 0 by:

pfd = pango_font_face_describe(face);
pstyle = pango_font_description_get_style( pfd ); //ok
pwt = pango_font_description_get_weight( pfd ); //ok
psz = pango_font_description_get_size( pfd ); // 0 - not ok

Your dump_stuff function also did not return any size-list information.
What might be the cause?

Ken
 





________________________________
 From: Bill Spitzak <spitzak at gmail.com>
To: Ken Resander <kresander at yahoo.com>; Cairo Mailing List <cairo at cairographics.org> 
Sent: Wednesday, 10 April 2013, 18:10
Subject: Re: [cairo] How to check if a font exists in Cairo and Pango?
 

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
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20130411/471501bd/attachment.html>


More information about the cairo mailing list