]> git.sesse.net Git - ccbs/blob - bigscreen/fonts.cpp
Add column headings and fix widths.
[ccbs] / bigscreen / fonts.cpp
1 #include <vector>
2 #include <stdexcept>
3 #include "fonts.h"
4
5 std::vector<FT_Face> fonts;
6
7 void init_freetype()
8 {
9         FT_Library library;
10         FT_Face face;
11         if (FT_Init_FreeType(&library))
12                 throw std::runtime_error("FreeType init failed.");
13
14         // Georgia
15         if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &face))
16                 throw std::runtime_error("Face opening failed.");
17         if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
18                 throw std::runtime_error("Size set 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                 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
26                         throw std::runtime_error("Size set failed.");
27                 fonts.push_back(face);
28         }
29         
30         // Arial Unicode MS
31         if (FT_New_Face(library, "arialuni.ttf", 0, &face)) {
32                 std::fprintf(stderr, "Warning: Couldn't open Arial Unicode MS, some glyphs might not be available\n");
33         } else {
34                 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
35                         throw std::runtime_error("Size set failed.");
36                 fonts.push_back(face);
37         }
38 }
39
40 unsigned my_draw_text(const widestring &str, unsigned char *buf, int xpos, int ypos, bool real_render, int r, int g, int b)
41 {
42         FT_GlyphSlot slot;
43         int x = 0;
44
45         for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
46                 int glyph_index;
47                 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
48                         glyph_index = FT_Get_Char_Index(*j, *i);
49                         if (glyph_index == 0)
50                                 continue;
51
52                         if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
53                                 throw std::runtime_error("Couldn't load glyph");
54                         slot = (*j)->glyph;
55                         break;
56                 }
57                 if (glyph_index == 0) {
58                         std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
59                         continue;
60                 }
61
62                 if (real_render) {
63                         int y;
64                         FT_Bitmap *bm = &(slot->bitmap);
65                         for (y = 0; y < bm->rows; y++) {
66                                 int xx;
67                                 int dsty = ypos - slot->bitmap_top + y;
68                                 if (dsty < 0 || dsty > 599) continue;
69
70                                 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
71                                 unsigned char *src = bm->buffer + y * bm->width;
72                                 for (xx = 0; xx < bm->width; xx++) {
73                                         *dst = (*dst * (256-*src) + r * *src) >> 8;
74                                         *dst++;
75                                         *dst = (*dst * (256-*src) + g * *src) >> 8;
76                                         *dst++;
77                                         *dst = (*dst * (256-*src) + b * *src) >> 8;
78                                         *dst++;
79                                         *dst++ = 0;
80                                         src++;
81                                 }
82                         }
83                 }
84
85                 x += slot->advance.x >> 6;
86         }
87
88         return x;
89 }