7 #include "resolution.h"
10 extern std::map<std::string, std::string> config;
11 std::map<std::string, FT_Face> faces;
13 std::vector<std::string> split(const std::string &str, char delim)
15 std::vector<std::string> ret;
17 for (unsigned i = 0; i < str.size(); ++i) {
18 if (str[i] == delim) {
34 if (FT_Init_FreeType(&library))
35 throw std::runtime_error("FreeType init failed.");
38 for (std::map<std::string, std::string>::const_iterator it = config.begin();
41 const std::string key = it->first;
42 if (key.size() <= 5 || key.substr(key.size() - 5) != ".font") {
46 std::vector<std::string> font_list = split(it->second, ';');
47 for (unsigned i = 0; i < font_list.size(); ++i) {
48 if (faces.count(font_list[i])) {
54 if (FT_New_Face(library, font_list[i].c_str(), 0, &face)) {
55 fprintf(stderr, "Warning: Couldn't open '%s', some glyphs might not be available\n", font_list[i].c_str());
57 faces[font_list[i]] = face;
63 void get_glyph(const std::vector<FT_Face> &fonts, wchar_t ch, FT_Face *face, int *glyph_index)
66 for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
67 *glyph_index = FT_Get_Char_Index(*i, ch);
68 if (*glyph_index == 0)
75 // this should really be done somehow else :-)
76 static unsigned screen_width, screen_height;
77 void set_screen_size(unsigned width, unsigned height)
80 screen_height = height;
83 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, const std::string &theme_element, int xpos, int ypos)
87 int r = atoi(get_theme_config(theme_element, "red").c_str());
88 int g = atoi(get_theme_config(theme_element, "green").c_str());
89 int b = atoi(get_theme_config(theme_element, "blue").c_str());
90 bool use_lcd = atoi(get_theme_config("screen", "lcd").c_str());
93 std::vector<FT_Face> fonts;
94 std::vector<std::string> font_list = split(get_theme_config(theme_element, "font"), ';');
95 for (unsigned i = 0; i < font_list.size(); ++i) {
96 if (faces.count(font_list[i])) {
97 fonts.push_back(faces[font_list[i]]);
101 for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
102 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96 * screen_width/LOGICAL_SCREEN_WIDTH, 96 * screen_height/LOGICAL_SCREEN_HEIGHT))
103 throw std::runtime_error("Couldn't set font size");
107 xpos = xpos * screen_width / LOGICAL_SCREEN_WIDTH;
108 ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
110 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
113 get_glyph(fonts, *i, &face, &glyph_index);
114 if (glyph_index == 0) {
115 std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%04x, ignoring\n", *i);
120 if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
121 throw std::runtime_error("Couldn't load glyph");
123 if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
124 throw std::runtime_error("Couldn't load glyph");
127 FT_GlyphSlot slot = face->glyph;
130 FT_Bitmap *bm = &(slot->bitmap);
131 for (y = 0; y < bm->rows; y++) {
133 int dsty = ypos - slot->bitmap_top + y;
134 if (dsty < 0 || dsty > signed(screen_height-1)) continue;
136 unsigned char *dst = buf + dsty * screen_width*4 + (x + xpos + slot->bitmap_left)*4;
137 unsigned char *src = bm->buffer + y * bm->pitch;
139 int width = (x + xpos + slot->bitmap_left + bm->width/3 >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width/3;
140 for (xx = 0; xx < width; xx++) {
141 *dst = (*dst * (256-src[2]) + b * src[2]) >> 8;
143 *dst = (*dst * (256-src[1]) + g * src[1]) >> 8;
145 *dst = (*dst * (256-src[0]) + r * src[0]) >> 8;
152 int width = (x + xpos + slot->bitmap_left + bm->width >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width;
153 for (xx = 0; xx < width; xx++) {
154 *dst = (*dst * (256-*src) + b * *src) >> 8;
156 *dst = (*dst * (256-*src) + g * *src) >> 8;
158 *dst = (*dst * (256-*src) + r * *src) >> 8;
167 x += slot->advance.x >> 6;
170 return x * LOGICAL_SCREEN_WIDTH / screen_width;
173 void my_draw_text_deferred(std::vector<TextDefer> &td, const widestring &str, double size, const std::string &theme_element, const std::string &fresh_theme_element, int xpos, int ypos)
180 newtd.theme_element = theme_element;
181 newtd.fresh_theme_element = fresh_theme_element;
185 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> ¤t, std::vector<TextDefer> &old)
187 for (unsigned i = 0; i < current.size(); ++i) {
188 std::string theme_element;
189 if (i < old.size() && current[i].str != old[i].str) {
191 theme_element = current[i].fresh_theme_element;
193 theme_element = current[i].theme_element;
196 my_draw_text(current[i].str, buf, current[i].size, theme_element, current[i].xpos, current[i].ypos);