]> git.sesse.net Git - ccbs/blob - bigscreen/fonts.cpp
Lots of changes/fixes to the new translation stuff.
[ccbs] / bigscreen / fonts.cpp
1 #include <vector>
2 #include <stdexcept>
3 #include "fonts.h"
4 #include "resolution.h"
5
6 std::vector<FT_Face> fonts;
7
8 void init_freetype()
9 {
10         FT_Library library;
11         FT_Face face;
12         if (FT_Init_FreeType(&library))
13                 throw std::runtime_error("FreeType init failed.");
14
15         // Georgia
16         if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &face))
17                 throw std::runtime_error("Face opening failed.");
18         fonts.push_back(face);
19
20         // FreeSerif
21         if (FT_New_Face(library, "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 0, &face)) {
22                 std::fprintf(stderr, "Warning: Couldn't open FreeSerif, some glyphs might not be available\n");
23         } else {
24                 fonts.push_back(face);
25         }
26         
27         // Arial Unicode MS
28         if (FT_New_Face(library, "arialuni.ttf", 0, &face)) {
29                 std::fprintf(stderr, "Warning: Couldn't open Arial Unicode MS, some glyphs might not be available\n");
30         } else {
31                 fonts.push_back(face);
32         }
33 }
34
35 // this should really be done somehow else :-)
36 static unsigned screen_width = SCREEN_WIDTH, screen_height = SCREEN_HEIGHT;
37 void set_screen_size(unsigned width, unsigned height)
38 {
39         screen_width = width;
40         screen_height = height;
41 }
42
43 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, int xpos, int ypos, int r, int g, int b)
44 {
45         FT_GlyphSlot slot;
46         int x = 0;
47         
48         for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
49                 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96 * screen_width/LOGICAL_SCREEN_WIDTH, 96 * screen_height/LOGICAL_SCREEN_HEIGHT))
50                         throw std::runtime_error("Couldn't set font size");
51         }
52
53         // whoop :-P
54         xpos = xpos * screen_width / LOGICAL_SCREEN_WIDTH;
55         ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
56
57         for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
58                 int glyph_index;
59                 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
60                         glyph_index = FT_Get_Char_Index(*j, *i);
61                         if (glyph_index == 0)
62                                 continue;
63
64 #if SCREEN_LCD
65                         if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
66 #else
67                         if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
68 #endif
69                                 throw std::runtime_error("Couldn't load glyph");
70                         slot = (*j)->glyph;
71                         break;
72                 }
73                 if (glyph_index == 0) {
74                         std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
75                         continue;
76                 }
77
78                 if (buf != NULL) {
79                         int y;
80                         FT_Bitmap *bm = &(slot->bitmap);
81                         for (y = 0; y < bm->rows; y++) {
82                                 int xx;
83                                 int dsty = ypos - slot->bitmap_top + y;
84                                 if (dsty < 0 || dsty > signed(screen_height-1)) continue;
85
86                                 unsigned char *dst = buf + dsty * screen_width*4 + (x + xpos + slot->bitmap_left)*4;
87 #if SCREEN_LCD
88                                 unsigned char *src = bm->buffer + y * bm->pitch;
89                                 int width = (x + xpos + slot->bitmap_left + bm->width/3 >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width/3;
90 #else
91                                 unsigned char *src = bm->buffer + y * bm->pitch;
92                                 int width = (x + xpos + slot->bitmap_left + bm->width >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width;
93 #endif
94
95 #if SCREEN_LCD
96                                 for (xx = 0; xx < width; xx++) {
97                                         *dst = (*dst * (256-src[2]) + b * src[2]) >> 8;
98                                         ++dst;
99                                         *dst = (*dst * (256-src[1]) + g * src[1]) >> 8;
100                                         ++dst;
101                                         *dst = (*dst * (256-src[0]) + r * src[0]) >> 8;
102                                         ++dst;
103                                         *dst++ = 0;
104
105                                         src += 3;
106                                 }
107 #else
108                                 for (xx = 0; xx < width; xx++) {
109                                         *dst = (*dst * (256-*src) + b * *src) >> 8;
110                                         ++dst;
111                                         *dst = (*dst * (256-*src) + g * *src) >> 8;
112                                         ++dst;
113                                         *dst = (*dst * (256-*src) + r * *src) >> 8;
114                                         ++dst;
115                                         *dst++ = 0;
116                                         ++src;
117                                 }
118 #endif
119                         }
120                 }
121
122                 x += slot->advance.x >> 6;
123         }
124
125         return x * LOGICAL_SCREEN_WIDTH / screen_width;
126 }
127
128 void my_draw_text_deferred(std::vector<TextDefer> &td, const widestring &str, double size, int xpos, int ypos)
129 {
130         TextDefer newtd;
131         newtd.str = str;
132         newtd.size = size;
133         newtd.xpos = xpos;
134         newtd.ypos = ypos;
135         td.push_back(newtd);
136 }
137
138 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> &current, std::vector<TextDefer> &old)
139 {
140         for (unsigned i = 0; i < current.size(); ++i) {
141                 int r, g, b;
142                 if (i < old.size() && current[i].str != old[i].str) {
143                         // changed text
144                         r = 255;
145                         g = 0;
146                         b = 0;
147                 } else {
148                         r = g = b = 255;
149                 }       
150                 
151                 my_draw_text(current[i].str, buf, current[i].size, current[i].xpos, current[i].ypos, r, g, b);
152         }
153 }
154