[cairo] Pango Cairo Rendering Text with Misc Fonts

robertk54 at aol.com robertk54 at aol.com
Wed May 14 08:35:33 PDT 2008

I switched to a Linux prototype and have a simple test app which 
renders text on top of an ARGB surface and saves to a .png file.  I am 
still having some issues with rendering text when selecting 
international fonts.  It's almost as if Cairo is using the toy fonts 
but some fonts render good.  The version of Pango I am currently using 
is older and does not support Gravity so I am not trying to render 
vertical text.

Fonts I tried, all render in english and are not very different from 
each other so I do not think they are all good . . .

Century Schoolbook L
Drugulin CLM
East Syriac Adiabene
Frank Ruehl CLM
Kochi Mincho
Lohit Punjabi
Lohit Tamil
Miriam Mono CLM
Mukti Narrow
Serto Batnan
Serto Jerusalem Outline

Most of my current test app . . .

#include <unistd.h>
#include <stdio.h>

#include <pango/pangocairo.h>

#define FOURCIF_WIDTH   704
#define FOURCIF_HEIGHT  476

#if !defined(CAIRO_HAS_FT_FONT)

static cairo_t *m_pCairo=NULL;
static cairo_surface_t *m_pSurface=NULL;
static cairo_surface_t *m_pSurfaceSample=NULL;static double 
static double m_dYPosStart=0.0;
static double m_dXPos=0.0;
static double m_dYPos=0.0;
static char *m_pUtf8=NULL;
static char *m_pColor=NULL;
static char *m_pFont=NULL;
static PangoLayout *m_pPangoLayout=NULL;
static PangoFontMap *m_pPangoFontMap=NULL;
static double m_size=12.0;
static double m_r=1.0, m_g=0.0, m_b=0.0, m_a=1.0;
static double m_dTransparency = 0.0;
static int m_iNumFonts;

void ListFonts (void);
void ParseParameters (int argc, char *argv[]);
void SetFont ( const char *font);
void SetRGB (unsigned char r, unsigned char g, unsigned char b, 
unsigned char a);
void HTML40ToRGB(char *HTML40, unsigned char *R, unsigned char *G, 
unsigned char *B);
void AddText (const char *utf8);

int main(int argc, char *argv[])
   // Default initial color blue
   unsigned char R = 0;
   unsigned char G = 0;
   unsigned char B = 255;
   unsigned char A = 255;
   double dNorm;

    m_pSurface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 
   m_pCairo = cairo_create (m_pSurface);
   cairo_set_source_rgba (m_pCairo, R, G, B, A);
   cairo_paint (m_pCairo);
    m_pSurfaceSample = cairo_image_surface_create_from_png 
    cairo_set_source_surface (m_pCairo, m_pSurfaceSample, 
   cairo_paint (m_pCairo);
   m_pPangoFontMap = pango_cairo_font_map_get_default ();
   m_pPangoLayout = pango_cairo_create_layout (m_pCairo);

   ParseParameters (argc, argv);

   dNorm = m_dTransparency;
   dNorm = (dNorm < 0.0) ? 0.0 : dNorm;
   dNorm = (dNorm > 1.0) ? 1.0 : dNorm;
   A = (unsigned char) ((1.0 - dNorm) * 255.0) & 0xff;

   HTML40ToRGB (m_pColor, &R, &G, &B);
   SetRGB (R, G, B, A);
   SetFont (m_pFont);

   AddText (m_pUtf8);

   g_object_unref (m_pPangoLayout);
   // Write the ARGB surface to a .PNG file so we can view it.
   cairo_surface_write_to_png (m_pSurface, "utf8test-pango.png");
   return 0;

void ListFonts ()
   PangoFontMap     *pPangoFontMap;
   PangoFontFamily **pPangoFontFamily;
   PangoFontFace   **pPangoFontFace;
   int n_faces;
   int n_families;
   int i;
   int j;

   m_iNumFonts = 0;
   pPangoFontMap = pango_cairo_font_map_get_default ();
    pango_font_map_list_families (pPangoFontMap, &pPangoFontFamily, 
    // Determine the font string and insert into (alpabetically sorted) 
combo list.
   for (i=0; i<n_families; i++)
       pango_font_family_list_faces (pPangoFontFamily[i], 
&pPangoFontFace, &n_faces);
       printf ("%s :: [", pango_font_family_get_name 
      for (j=0; j<n_faces; j++)
          printf ("%s", pango_font_face_get_face_name 
         if (j+1 < n_faces) printf (", ");
      printf ("]\n");
   g_free (pPangoFontFace);
   g_free (pPangoFontFamily);

void AddText (const char *utf8)
   PangoAttrList* pPangoAttrList;
   gchar *text;
   // Get the layout's current attribute list
   pPangoAttrList = pango_layout_get_attributes (m_pPangoLayout);
    // Would be nice to extend the markup language for dynamic 
    // These would need to be processed and stripped here, prior to the 
pango_parse_markup call.
   // Update the attribute list from the markup text.
   pango_parse_markup (utf8, -1, 0, &pPangoAttrList, &text, NULL, NULL);
   pango_layout_set_attributes (m_pPangoLayout, pPangoAttrList);
   pango_attr_list_unref (pPangoAttrList);
   pango_layout_set_text (m_pPangoLayout, text, -1);
   // Set the color and position of the cairo context.
   cairo_set_source_rgba (m_pCairo, m_r, m_g, m_b, m_a);
   cairo_save (m_pCairo);
   cairo_move_to (m_pCairo, m_dXPos+m_dXPosStart, m_dYPos+m_dYPosStart);
   // Let the pango layout know of the context changes above.
   pango_layout_context_changed (m_pPangoLayout);
   pango_cairo_update_layout (m_pCairo, m_pPangoLayout);
   pango_cairo_show_layout (m_pCairo, m_pPangoLayout);
   cairo_restore (m_pCairo);

void SetFont (const char *font)
   PangoFontDescription* pPangoFontDescription=NULL;
   double dSize;

   pPangoFontDescription = pango_font_description_from_string (font);
    dSize = m_size * cairo_image_surface_get_height (m_pSurface) / 100.0 
    pango_font_description_set_absolute_size (pPangoFontDescription, 
    pango_layout_set_font_description (m_pPangoLayout, 
    printf ("%s : %s : %d : %d\n", font, 
pango_font_description_to_string (pPangoFontDescription), (int) m_size, 
(int) dSize);
   pango_font_description_free (pPangoFontDescription);
   pPangoFontDescription = NULL;

More information about the cairo mailing list