+ // Itemize the text into parts that will be shaped and rendered separately.
+ PangoContext *context = pango_context_new();
+ PangoFontMap *fontmap = pango_ft2_font_map_new();
+ pango_context_set_font_map(context, fontmap);
+ PangoAttrList *attrs = pango_attr_list_new();
+ std::string utf8str = str.to_utf8();
+ GList *items = pango_itemize(context, utf8str.data(), 0, utf8str.size(), attrs, NULL);
+ for (GList *item_it = items; item_it != NULL; item_it = item_it->next) {
+ PangoItem *item = (PangoItem *)(item_it->data);
+ PangoAnalysis *analysis = &item->analysis;
+
+ // Try to shape and render it in each of the fonts in turn.
+ for (std::vector<FT_Face>::const_iterator face_it = fonts.begin(); face_it != fonts.end(); ++face_it) {
+ // The buffer seemingly needs to be created anew for each try.
+ hb_buffer_t *hbuf = hb_buffer_create();
+ hb_buffer_set_unicode_funcs(hbuf, hb_icu_get_unicode_funcs());
+ hb_direction_t dir = PANGO_GRAVITY_IS_VERTICAL(analysis->gravity) ? HB_DIRECTION_TTB : HB_DIRECTION_LTR;
+ if (analysis->level % 2)
+ dir = HB_DIRECTION_REVERSE(dir);
+ if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity))
+ dir = HB_DIRECTION_REVERSE(dir);
+
+ hb_buffer_set_direction(hbuf, dir);
+ hb_buffer_set_script(hbuf, hb_glib_script_to_script((GUnicodeScript)analysis->script));
+ hb_buffer_set_language(hbuf, hb_language_from_string(pango_language_to_string(analysis->language), -1));
+ hb_buffer_add_utf8(hbuf, utf8str.data(), utf8str.size(), item->offset, item->length);
+
+ // Font-specifics.
+ FT_Face face = *face_it;
+ hb_font_t *font = hb_ft_font_create(face, NULL);
+ hb_shape(font, hbuf, NULL, 0);
+ unsigned glyph_count;
+ hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(hbuf, &glyph_count);
+ hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(hbuf, &glyph_count);