]> git.sesse.net Git - ccbs/blob - bigscreen/fonts.cpp
bbc4c1a0d4305e5133c7626d8ace838435080439
[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                         }
57                         faces[font_list[i]] = face;
58                 }
59         }
60 }
61
62 // this should really be done somehow else :-)
63 static unsigned screen_width, screen_height;
64 void set_screen_size(unsigned width, unsigned height)
65 {
66         screen_width = width;
67         screen_height = height;
68 }
69
70 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, const std::string &theme_element, int xpos, int ypos)
71 {
72         FT_GlyphSlot slot;
73         int x = 0;
74
75         int r = atoi(get_theme_config(theme_element, "red").c_str());
76         int g = atoi(get_theme_config(theme_element, "green").c_str());
77         int b = atoi(get_theme_config(theme_element, "blue").c_str());
78         bool use_lcd = atoi(get_theme_config("screen", "lcd").c_str());
79
80         // Find font faces.
81         std::vector<FT_Face> fonts;
82         std::vector<std::string> font_list = split(get_theme_config(theme_element, "font"), ';');
83         for (unsigned i = 0; i < font_list.size(); ++i) {
84                 if (faces.count(font_list[i])) {
85                         fonts.push_back(faces[font_list[i]]);
86                 }
87         }
88         
89         for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
90                 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96 * screen_width/LOGICAL_SCREEN_WIDTH, 96 * screen_height/LOGICAL_SCREEN_HEIGHT))
91                         throw std::runtime_error("Couldn't set font size");
92         }
93
94         // whoop :-P
95         xpos = xpos * screen_width / LOGICAL_SCREEN_WIDTH;
96         ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
97
98         for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
99                 int glyph_index;
100                 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
101                         glyph_index = FT_Get_Char_Index(*j, *i);
102                         if (glyph_index == 0)
103                                 continue;
104
105                         if (use_lcd) {
106                                 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
107                                         throw std::runtime_error("Couldn't load glyph");
108                         } else {
109                                 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
110                                         throw std::runtime_error("Couldn't load glyph");
111                         }
112                         slot = (*j)->glyph;
113                         break;
114                 }
115                 if (glyph_index == 0) {
116                         std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
117                         continue;
118                 }
119
120                 if (buf != NULL) {
121                         int y;
122                         FT_Bitmap *bm = &(slot->bitmap);
123                         for (y = 0; y < bm->rows; y++) {
124                                 int xx;
125                                 int dsty = ypos - slot->bitmap_top + y;
126                                 if (dsty < 0 || dsty > signed(screen_height-1)) continue;
127
128                                 unsigned char *dst = buf + dsty * screen_width*4 + (x + xpos + slot->bitmap_left)*4;
129                                 unsigned char *src = bm->buffer + y * bm->pitch;
130                                 if (use_lcd) {
131                                         int width = (x + xpos + slot->bitmap_left + bm->width/3 >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width/3;
132                                         for (xx = 0; xx < width; xx++) {
133                                                 *dst = (*dst * (256-src[2]) + b * src[2]) >> 8;
134                                                 ++dst;
135                                                 *dst = (*dst * (256-src[1]) + g * src[1]) >> 8;
136                                                 ++dst;
137                                                 *dst = (*dst * (256-src[0]) + r * src[0]) >> 8;
138                                                 ++dst;
139                                                 *dst++ = 0;
140
141                                                 src += 3;
142                                         }
143                                 } else {
144                                         int width = (x + xpos + slot->bitmap_left + bm->width >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width;
145                                         for (xx = 0; xx < width; xx++) {
146                                                 *dst = (*dst * (256-*src) + b * *src) >> 8;
147                                                 ++dst;
148                                                 *dst = (*dst * (256-*src) + g * *src) >> 8;
149                                                 ++dst;
150                                                 *dst = (*dst * (256-*src) + r * *src) >> 8;
151                                                 ++dst;
152                                                 *dst++ = 0;
153                                                 ++src;
154                                         }
155                                 }
156                         }
157                 }
158
159                 x += slot->advance.x >> 6;
160         }
161
162         return x * LOGICAL_SCREEN_WIDTH / screen_width;
163 }
164
165 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)
166 {
167         TextDefer newtd;
168         newtd.str = str;
169         newtd.size = size;
170         newtd.xpos = xpos;
171         newtd.ypos = ypos;
172         newtd.theme_element = theme_element;
173         newtd.fresh_theme_element = fresh_theme_element;
174         td.push_back(newtd);
175 }
176
177 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> &current, std::vector<TextDefer> &old)
178 {
179         for (unsigned i = 0; i < current.size(); ++i) {
180                 std::string theme_element;
181                 if (i < old.size() && current[i].str != old[i].str) {
182                         // changed text
183                         theme_element = current[i].fresh_theme_element;
184                 } else {
185                         theme_element = current[i].theme_element;
186                 }       
187                 
188                 my_draw_text(current[i].str, buf, current[i].size, theme_element, current[i].xpos, current[i].ypos);
189         }
190 }
191