[cairo] Pango Cairo Rendering Text with Misc Fonts

robertk54 at aol.com robertk54 at aol.com
Tue May 20 07:50:32 PDT 2008

Based on my results, either I am doing something wrong or the default 
pango-cairo fonts being used are the cairo toy fonts.  Specifiying any 
available font results in only a small subset of rendered font faces.  
Given that I am using (forced to based on environment) an older release 
(pango-cairo 1.14) should I be able to programatically select freetype 
fonts vs. cairo toy fonts?

-----Original Message-----
From: robertk54 at aol.com
To: cairo at cairographics.org
Sent: Wed, 14 May 2008 11:35 am
Subject: Pango Cairo Rendering Text with Misc Fonts

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, 
FOURCIF_WIDTH*0.21, 0); 
  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, 
  printf ("%s :: [", pango_font_family_get_name (pPangoFontFamily[i])); 
  for (j=0; j<n_faces; j++) 
  printf ("%s", pango_font_face_get_face_name (pPangoFontFace[j])); 
  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