Refactor font rendering a tiny bit.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 19 Feb 2012 16:44:04 +0000 (17:44 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 19 Feb 2012 16:44:04 +0000 (17:44 +0100)
bigscreen/fonts.cpp

index 8cee589..4b0f3c8 100644 (file)
@@ -60,6 +60,17 @@ void init_freetype()
        }
 }
 
+void get_glyph(const std::vector<FT_Face> &fonts, wchar_t ch, FT_Face *face, int *glyph_index)
+{
+       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)
@@ -70,7 +81,6 @@ void set_screen_size(unsigned width, unsigned height)
 
 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, const std::string &theme_element, int xpos, int ypos)
 {
-       FT_GlyphSlot slot;
        int x = 0;
 
        int r = atoi(get_theme_config(theme_element, "red").c_str());
@@ -97,27 +107,23 @@ unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, co
        ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
 
        for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
-               int glyph_index = 0;
-               for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
-                       glyph_index = FT_Get_Char_Index(*j, *i);
-                       if (glyph_index == 0)
-                               continue;
-
-                       if (use_lcd) {
-                               if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
-                                       throw std::runtime_error("Couldn't load glyph");
-                       } else {
-                               if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
-                                       throw std::runtime_error("Couldn't load glyph");
-                       }
-                       slot = (*j)->glyph;
-                       break;
-               }
+               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;
                }
 
+               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");
+               }
+
+               FT_GlyphSlot slot = face->glyph;
                if (buf != NULL) {
                        int y;
                        FT_Bitmap *bm = &(slot->bitmap);