From: Steinar H. Gunderson Date: Sun, 19 Feb 2012 16:44:04 +0000 (+0100) Subject: Refactor font rendering a tiny bit. X-Git-Url: https://git.sesse.net/?p=ccbs;a=commitdiff_plain;h=cb9749b300678e08c05bed4288ac200a13811b70 Refactor font rendering a tiny bit. --- diff --git a/bigscreen/fonts.cpp b/bigscreen/fonts.cpp index 8cee589..4b0f3c8 100644 --- a/bigscreen/fonts.cpp +++ b/bigscreen/fonts.cpp @@ -60,6 +60,17 @@ void init_freetype() } } +void get_glyph(const std::vector &fonts, wchar_t ch, FT_Face *face, int *glyph_index) +{ + for (std::vector::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::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);