Shape text using Pango and HarfBuzz; gives us nice ligatures and exotic scripts.
[ccbs] / bigscreen / fonts.cpp
index 1a2c104..b9a02c5 100644 (file)
@@ -3,6 +3,13 @@
 #include <stdexcept>
 #include <map>
 #include <string>
+#include <harfbuzz/hb.h>
+#include <harfbuzz/hb-ft.h>
+#include <harfbuzz/hb-glib.h>
+#include <harfbuzz/hb-icu.h>
+#include <pango/pango-context.h>
+#include <pango/pango-fontmap.h>
+#include <pango/pangoft2.h>
 #include "fonts.h"
 #include "resolution.h"
 #include "theme.h"
@@ -60,18 +67,6 @@ void init_freetype()
        }
 }
 
-void get_glyph(const std::vector<FT_Face> &fonts, wchar_t ch, FT_Face *face, int *glyph_index)
-{
-       *glyph_index = 0;
-       for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
-               *glyph_index = FT_Get_Char_Index(*i, ch);
-               if (*glyph_index == 0)
-                       continue;
-               *face = *i;
-               return;
-       }
-}
-
 // this should really be done somehow else :-)
 static unsigned screen_width, screen_height;
 void set_screen_size(unsigned width, unsigned height)
@@ -146,30 +141,89 @@ unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, co
        xpos = xpos * screen_width / LOGICAL_SCREEN_WIDTH;
        ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
 
-       for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
-               FT_Face face;
-               int glyph_index;
-               get_glyph(fonts, *i, &face, &glyph_index);
-               if (glyph_index == 0) {
-                       std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%04x, 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;
 
-               if (use_lcd) {
-                       if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
-                               throw std::runtime_error("Couldn't load glyph");
-               } else {
-                       if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
-                               throw std::runtime_error("Couldn't load glyph");
-               }
+               // 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);
 
-               if (buf != NULL) {
-                       render_glyph(face->glyph, xpos, ypos, buf, r, g, b, use_lcd);
-               }
+                       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);
+
+                       bool found_all = true;
+                       for (unsigned i = 0; i < glyph_count; ++i) {
+                               if (glyph_info[i].codepoint == 0) {
+                                       found_all = false;
+                               }
+                               break;
+                       }
+
+                       if (!found_all) {
+                               std::string utf8itemstr(&utf8str[item->offset], &utf8str[item->offset + item->length]);
+                               if (face_it + 1 == fonts.end()) {
+                                       std::fprintf(stderr, "Warning: Could not find any font to render item '%s', rendering in last font anyway\n", utf8itemstr.c_str());
+                               } else {
+                                       hb_font_destroy(font);
+                                       hb_buffer_destroy(hbuf);
+                                       continue;
+                               }
+                       }
 
-               xpos += face->glyph->advance.x / 64;
-               ypos -= face->glyph->advance.y / 64;
+                       for (unsigned i = 0; i < glyph_count; ++i) {
+                               if (use_lcd) {
+                                       if (FT_Load_Glyph(face, glyph_info[i].codepoint, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
+                                               throw std::runtime_error("Couldn't load glyph");
+                               } else {
+                                       if (FT_Load_Glyph(face, glyph_info[i].codepoint, FT_LOAD_RENDER))
+                                               throw std::runtime_error("Couldn't load glyph");
+                               }
+
+                               int glyphx = xpos + glyph_pos[i].x_offset / 64;
+                               int glyphy = ypos - glyph_pos[i].y_offset / 64;
+
+                               if (buf != NULL) {
+                                       render_glyph(face->glyph, glyphx, glyphy, buf, r, g, b, use_lcd);
+                               }
+
+                               xpos += glyph_pos[i].x_advance / 64;
+                               ypos -= glyph_pos[i].y_advance / 64;
+                       }
+                       hb_font_destroy(font);
+                       hb_buffer_destroy(hbuf);
+                       break;
+               }
+               pango_item_free(item);
        }
+       g_list_free(items);
+       pango_attr_list_unref(attrs);
+       g_object_unref(fontmap);
+       g_object_unref(context);
 
        return (xpos - start_xpos) * LOGICAL_SCREEN_WIDTH / screen_width;
 }