]> git.sesse.net Git - ccbs/blob - bigscreen/fonts.cpp
Refactor font rendering a tiny bit.
[ccbs] / bigscreen / fonts.cpp
1 #include <cstdio>
2 #include <vector>
3 #include <stdexcept>
4 #include <map>
5 #include <string>
6 #include "fonts.h"
7 #include "resolution.h"
8 #include "theme.h"
9
10 extern std::map<std::string, std::string> config;
11 std::map<std::string, FT_Face> faces;
12
13 std::vector<std::string> split(const std::string &str, char delim)
14 {
15         std::vector<std::string> ret;
16         std::string s;
17         for (unsigned i = 0; i < str.size(); ++i) {
18                 if (str[i] == delim) {
19                         ret.push_back(s);
20                         s.clear();
21                 } else {
22                         s.push_back(str[i]);
23                 }
24         }
25         if (!s.empty()) {
26                 ret.push_back(s);
27         }
28         return ret;
29 }
30
31 void init_freetype()
32 {
33         FT_Library library;
34         if (FT_Init_FreeType(&library))
35                 throw std::runtime_error("FreeType init failed.");
36
37         // Preload all fonts.
38         for (std::map<std::string, std::string>::const_iterator it = config.begin();
39              it != config.end();
40              ++it) {
41                 const std::string key = it->first;
42                 if (key.size() <= 5 || key.substr(key.size() - 5) != ".font") {
43                         continue;
44                 }
45
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])) {
49                                 // Already preloaded.
50                                 continue;
51                         }
52
53                         FT_Face face;
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());
56                         } else {
57                                 faces[font_list[i]] = face;
58                         }
59                 }
60         }
61 }
62
63 void get_glyph(const std::vector<FT_Face> &fonts, wchar_t ch, FT_Face *face, int *glyph_index)
64 {
65         for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
66                 *glyph_index = FT_Get_Char_Index(*i, ch);
67                 if (*glyph_index == 0)
68                         continue;
69                 *face = *i;
70                 return;
71         }
72 }
73
74 // this should really be done somehow else :-)
75 static unsigned screen_width, screen_height;
76 void set_screen_size(unsigned width, unsigned height)
77 {
78         screen_width = width;
79         screen_height = height;
80 }
81
82 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, const std::string &theme_element, int xpos, int ypos)
83 {
84         int x = 0;
85
86         int r = atoi(get_theme_config(theme_element, "red").c_str());
87         int g = atoi(get_theme_config(theme_element, "green").c_str());
88         int b = atoi(get_theme_config(theme_element, "blue").c_str());
89         bool use_lcd = atoi(get_theme_config("screen", "lcd").c_str());
90
91         // Find font faces.
92         std::vector<FT_Face> fonts;
93         std::vector<std::string> font_list = split(get_theme_config(theme_element, "font"), ';');
94         for (unsigned i = 0; i < font_list.size(); ++i) {
95                 if (faces.count(font_list[i])) {
96                         fonts.push_back(faces[font_list[i]]);
97                 }
98         }
99         
100         for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
101                 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96 * screen_width/LOGICAL_SCREEN_WIDTH, 96 * screen_height/LOGICAL_SCREEN_HEIGHT))
102                         throw std::runtime_error("Couldn't set font size");
103         }
104
105         // whoop :-P
106         xpos = xpos * screen_width / LOGICAL_SCREEN_WIDTH;
107         ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
108
109         for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
110                 FT_Face face;
111                 int glyph_index;
112                 get_glyph(fonts, *i, &face, &glyph_index);
113                 if (glyph_index == 0) {
114                         std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%04x, ignoring\n", *i);
115                         continue;
116                 }
117
118                 if (use_lcd) {
119                         if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
120                                 throw std::runtime_error("Couldn't load glyph");
121                 } else {
122                         if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
123                                 throw std::runtime_error("Couldn't load glyph");
124                 }
125
126                 FT_GlyphSlot slot = face->glyph;
127                 if (buf != NULL) {
128                         int y;
129                         FT_Bitmap *bm = &(slot->bitmap);
130                         for (y = 0; y < bm->rows; y++) {
131                                 int xx;
132                                 int dsty = ypos - slot->bitmap_top + y;
133                                 if (dsty < 0 || dsty > signed(screen_height-1)) continue;
134
135                                 unsigned char *dst = buf + dsty * screen_width*4 + (x + xpos + slot->bitmap_left)*4;
136                                 unsigned char *src = bm->buffer + y * bm->pitch;
137                                 if (use_lcd) {
138                                         int width = (x + xpos + slot->bitmap_left + bm->width/3 >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width/3;
139                                         for (xx = 0; xx < width; xx++) {
140                                                 *dst = (*dst * (256-src[2]) + b * src[2]) >> 8;
141                                                 ++dst;
142                                                 *dst = (*dst * (256-src[1]) + g * src[1]) >> 8;
143                                                 ++dst;
144                                                 *dst = (*dst * (256-src[0]) + r * src[0]) >> 8;
145                                                 ++dst;
146                                                 *dst++ = 0;
147
148                                                 src += 3;
149                                         }
150                                 } else {
151                                         int width = (x + xpos + slot->bitmap_left + bm->width >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width;
152                                         for (xx = 0; xx < width; xx++) {
153                                                 *dst = (*dst * (256-*src) + b * *src) >> 8;
154                                                 ++dst;
155                                                 *dst = (*dst * (256-*src) + g * *src) >> 8;
156                                                 ++dst;
157                                                 *dst = (*dst * (256-*src) + r * *src) >> 8;
158                                                 ++dst;
159                                                 *dst++ = 0;
160                                                 ++src;
161                                         }
162                                 }
163                         }
164                 }
165
166                 x += slot->advance.x >> 6;
167         }
168
169         return x * LOGICAL_SCREEN_WIDTH / screen_width;
170 }
171
172 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)
173 {
174         TextDefer newtd;
175         newtd.str = str;
176         newtd.size = size;
177         newtd.xpos = xpos;
178         newtd.ypos = ypos;
179         newtd.theme_element = theme_element;
180         newtd.fresh_theme_element = fresh_theme_element;
181         td.push_back(newtd);
182 }
183
184 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> &current, std::vector<TextDefer> &old)
185 {
186         for (unsigned i = 0; i < current.size(); ++i) {
187                 std::string theme_element;
188                 if (i < old.size() && current[i].str != old[i].str) {
189                         // changed text
190                         theme_element = current[i].fresh_theme_element;
191                 } else {
192                         theme_element = current[i].theme_element;
193                 }       
194                 
195                 my_draw_text(current[i].str, buf, current[i].size, theme_element, current[i].xpos, current[i].ypos);
196         }
197 }
198