- if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
- throw std::runtime_error("Couldn't load glyph");
- slot = (*j)->glyph;
- break;
- }
- if (glyph_index == 0) {
- std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
- continue;
- }
+ // 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);