Search code examples
utf-8fontspango

Pango select multiples fonts


I have three fonts i want to use in my software with pango:

  • Font1: latin, Cryllic characters
  • Font2: Korean characters
  • Font3: Japanese characters

Pango render the text correctly but i want select a font

There any way to indicate this preference pango font?

I use: linux and pango 1.29


Solution

  • The simplest way is to use PangoMarkup to set the fonts you want:

    //  See documentation for Pango markup for details
    
    char *pszMarkup = "<span face=\"{font family name goes here}\">"
                      "{text requiring font goes here}"
                      "</span>";  //  Split for clarity
    char            *pszText;   // Pointer for text without markup tags
    PangoAttrList   *pAttr;     // Attribute list - will be populated with tag info
    
    pango_parse_markup (pszMarkup, -1, 0, &attr_list, &pszText, NULL, NULL);
    

    You now have a buffer of regular text and an attribute list. If you want to set these up by hand (without going through the parser), you will need one PangoAttribute per instance of the font and set PangoAttribute.start_index and PangoAttribute.end_index by hand.

    However you get them, you now give them to a PangoLayout:

    //  pWidget is the windowed widget in which the text is displayed:
    
    PangoContext *pCtxt = gtk_widget_get_pango_context (pWidget);
    PangoLayout  *pLayout = pango_layout_new (pCtxt);
    
    pango_layout_set_attributes(pLayout, pAttr);
    pango_layout_set_text (pLayout, pszText, -1);
    

    That's it. Use pango_cairo_show_layout (cr, pLayout) to display the results. The setup only needs changing when the content changes - it maintains the values across draw signals.