[cairo] Using Pango+Cairo to layout non-standard / international text

robertk54 at aol.com robertk54 at aol.com
Fri Apr 11 08:07:18 PDT 2008


>I have also made a demonstration of different orientations while
>figuring it out for myself, available from here:

>  http://iki.fi/zuh/pango-rotated.c

Thanks, this is great.  I looked at it and will reference it when I 
start using international fonts.  I am currently playing with english 
fonts and still can't get vertical text working.  Below is the current 
code I am using, I have moved things around a number of times to no 
avail.  This is part of my Pango-Cairo prototype code which is 
currently running on Windows.  The software plays a video file (YUV), 
converts each frame to ARGB, overlays text, then renders to a display 
window.  I can update the text overlay parameters on the fly and the 
rendered image updates in real time.  The text is entered as a UTF8 
string but it is actually just a plain text string with the extra 
markup added as shown .  .  .

"The <i>quick</i> <span foreground="brown">brown</span> fox <span 
rise="10000">j</span><span rise="20000">u</span><span 
rise="30000">m</span><span rise="20000">p</span><span 
rise="10000">s</span> over the <u>lazy</u> dog."

The font is selected from a pull down menu I populate by using 
Pango+Cairo to enumerate the available families / faces . . .

<<
   pPangoFontMap = pango_cairo_font_map_get_default ();
    pango_font_map_list_families (pPangoFontMap, &pPangoFontFamily, 
&n_families);
   for (i=0; i<n_families; i++)
   {
       pango_font_family_list_faces (pPangoFontFamily[i], 
&pPangoFontFace, &n_faces);
>>

I select the gravity value with a radio button.

My initial goal is to render the text with the characters facing the 
normal vertical direction no matter which orientation I select.  To see 
the string horizontallay I select auto or south.  To see the string 
vertically I select one of the east or west.  With the current code, 
selecting east or west simply rotates the text +/- 90 degrees.  The 
characters rotate with the string and do not remain vertical.

Selecting west the tops of the characters point west and the bottoms 
point east.  With the current code the characters are mirror images of 
the original.  'The quick' is at the north and 'lazy dog' is in the 
south.  All the characters are flipped (mirror images) not simply the 
text as in 'kciuq ehT'.  'The ' is rendered as hollow boxes, all other 
characters are rendered in the proper font / format / etc.  The current 
font selcted is 'Papyrus Normal' but I get the same results other fonts.

Selecting east the tops of the characters point west and the bottoms 
point east.  With the current code the characters are not mirror images 
and can be read as normal.  'The quick' is at the north and 'lazy dog' 
is in the south.  'The ' is rendered as hollow boxes, all other 
characters are rendered in the proper font / format / etc.  The current 
font selcted is 'Papyrus Normal' but I get the same results other fonts.

Selecting south or auto the characters and text are rendered normally.  
'The ' as well as all other characters are rendered in the proper font 
/ format / etc.  The current font selcted is 'Papyrus Normal' but I get 
the same results other fonts.

Selecting north the tops of the characters point south and the bottoms 
point north.  With the current code the characters are mirror images of 
the original.  'The quick' is at the east and 'lazy dog' is in the 
west.  All the characters are flipped (mirror images) not simply the 
text as in 'kciuq ehT'.  'The ' is rendered as hollow boxes, all other 
characters are rendered in the proper font / format / etc.  The current 
font selcted is 'Papyrus Normal' but I get the same results other fonts.

<<
      cairo_save (m_pCairo);
      // Get the layout's current attribute list
      pPangoAttrList = pango_layout_get_attributes (m_pPangoLayout);
      // 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_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_move_to (m_pCairo, m_dXPos+m_dXPosStart, 
m_dYPos+m_dYPosStart);
      switch (gravity)
      {
      case  PANGO_GRAVITY_SOUTH:
      case  PANGO_GRAVITY_AUTO:
         rotation = 0 / (180.0 / G_PI);
         break;
      case  PANGO_GRAVITY_EAST:
         rotation = 90 / (180.0 / G_PI);
         break;
      case  PANGO_GRAVITY_NORTH:
         rotation = 180 / (180.0 / G_PI);
         break;
      case  PANGO_GRAVITY_WEST:
         rotation = 270 / (180.0 / G_PI);
         break;
      }
      cairo_rotate (m_pCairo, rotation);
      // Get the layout's current context
      pPangoContext = pango_layout_get_context (m_pPangoLayout);
      // Try to implement vertical text using context gravity
      pango_context_set_base_gravity (pPangoContext, gravity);
       pango_context_set_gravity_hint (pPangoContext, 
PANGO_GRAVITY_HINT_STRONG);
      // Let the pango layout know of the context changes above.
      pango_layout_context_changed (m_pPangoLayout);
      pango_cairo_show_layout (m_pCairo, m_pPangoLayout);
      pango_attr_list_unref (pPangoAttrList);
      cairo_restore (m_pCairo);
>>


More information about the cairo mailing list