cfreetypetext-renderingpango

What is a simple example of using Pango to render text into an image using a FreeType font?


I want to render text using Pango, but I can't find example code that does not use Cairo. Can someone give me a simple example of Pango usage with a FreeType backend? This is how I think it should roughly work, but as you can see in the code, there are some things I don't understand yet.

// context and font stuff
PangoFontMap *fontmap = pango_ft2_font_map_new();
// do I have to initialize the fontmap?
PangoContext *context = pango_font_map_create_context(fontmap);
PangoFontDescription *desc = pango_font_description_new();
pango_font_description_set_style(desc, PANGO_STYLE_NORMAL); // not bold or italic
pango_font_description_set_size(desc, 12); // 12pt font
PangoFont *font = pango_context_load_font(context, desc);

// target bitmap
FT_Bitmap bitmap;
// how do I say how big the bitmap should be? write it into the struct?
FT_Bitmap_New(&bitmap);

// text
char *text = "Hello World";
PangoAttrList *attrs = pango_attr_list_new();
GList *items = pango_itemize(context, text, 0, strlen(text), attrs, NULL);
// do I have to initialize this?
PangoGlyphString *glyphs = pango_glyph_string_new();
pango_shape(text, strlen(text),
      /* PangoAnalysis here – where do I get that? */, glyphs);

pango_ft2_render(&bitmap, font, PangoGlyphString *glyphs,
      0, 0/*or does this have to be height-1?*/);

Solution

  • I'd take a look at the source code of pango-view, the "toy" app included in pango to demonstrate its capabilities. It can render in all pango modes