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