Make a "logical resolution" (yuck!) of 800x600 for all the screens, and make fonts...
[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 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, int xpos, int ypos, int r, int g, int b)
36 {
37         FT_GlyphSlot slot;
38         int x = 0;
39         
40         for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
41                 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96 * SCREEN_WIDTH/LOGICAL_SCREEN_WIDTH, 96 * SCREEN_HEIGHT/LOGICAL_SCREEN_HEIGHT))
42                         throw std::runtime_error("Couldn't set font size");
43         }
44
45         // whoop :-P
46         xpos = xpos * SCREEN_WIDTH / LOGICAL_SCREEN_WIDTH;
47         ypos = ypos * SCREEN_HEIGHT / LOGICAL_SCREEN_HEIGHT;
48
49         for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
50                 int glyph_index;
51                 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
52                         glyph_index = FT_Get_Char_Index(*j, *i);
53                         if (glyph_index == 0)
54                                 continue;
55
56                         if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
57                                 throw std::runtime_error("Couldn't load glyph");
58                         slot = (*j)->glyph;
59                         break;
60                 }
61                 if (glyph_index == 0) {
62                         std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
63                         continue;
64                 }
65
66                 if (buf != NULL) {
67                         int y;
68                         FT_Bitmap *bm = &(slot->bitmap);
69                         for (y = 0; y < bm->rows; y++) {
70                                 int xx;
71                                 int dsty = ypos - slot->bitmap_top + y;
72                                 if (dsty < 0 || dsty > (SCREEN_HEIGHT-1)) continue;
73
74                                 unsigned char *dst = buf + dsty * SCREEN_WIDTH*4 + (x + xpos + slot->bitmap_left)*4;
75                                 unsigned char *src = bm->buffer + y * bm->width;
76
77                                 int width = (x + xpos + slot->bitmap_left + bm->width >= SCREEN_WIDTH) ? ((SCREEN_WIDTH-1) - x - xpos - slot->bitmap_left) : bm->width;
78                                 for (xx = 0; xx < width; xx++) {
79                                         *dst = (*dst * (256-*src) + b * *src) >> 8;
80                                         *dst++;
81                                         *dst = (*dst * (256-*src) + g * *src) >> 8;
82                                         *dst++;
83                                         *dst = (*dst * (256-*src) + r * *src) >> 8;
84                                         *dst++;
85                                         *dst++ = 0;
86                                         src++;
87                                 }
88                         }
89                 }
90
91                 x += slot->advance.x >> 6;
92         }
93
94         return x * LOGICAL_SCREEN_WIDTH / SCREEN_WIDTH;
95 }
96
97 void my_draw_text_deferred(std::vector<TextDefer> &td, const widestring &str, double size, int xpos, int ypos)
98 {
99         TextDefer newtd;
100         newtd.str = str;
101         newtd.size = size;
102         newtd.xpos = xpos;
103         newtd.ypos = ypos;
104         td.push_back(newtd);
105 }
106
107 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> &current, std::vector<TextDefer> &old)
108 {
109         for (unsigned i = 0; i < current.size(); ++i) {
110                 int r, g, b;
111                 if (i < old.size() && current[i].str != old[i].str) {
112                         // changed text
113                         r = 255;
114                         g = 0;
115                         b = 0;
116                 } else {
117                         r = g = b = 255;
118                 }       
119                 
120                 my_draw_text(current[i].str, buf, current[i].size, current[i].xpos, current[i].ypos, r, g, b);
121         }
122 }
123