]> git.sesse.net Git - ccbs/blob - bigscreen/fonts.cpp
Make my_draw_text() clip horizontally.
[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         fonts.push_back(face);
18
19         // FreeSerif
20         if (FT_New_Face(library, "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 0, &face)) {
21                 std::fprintf(stderr, "Warning: Couldn't open FreeSerif, some glyphs might not be available\n");
22         } else {
23                 fonts.push_back(face);
24         }
25         
26         // Arial Unicode MS
27         if (FT_New_Face(library, "arialuni.ttf", 0, &face)) {
28                 std::fprintf(stderr, "Warning: Couldn't open Arial Unicode MS, some glyphs might not be available\n");
29         } else {
30                 fonts.push_back(face);
31         }
32 }
33
34 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, int xpos, int ypos, int r, int g, int b)
35 {
36         FT_GlyphSlot slot;
37         int x = 0;
38         
39         for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
40                 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96, 96))
41                         throw std::runtime_error("Couldn't set font size");
42         }
43
44         for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
45                 int glyph_index;
46                 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
47                         glyph_index = FT_Get_Char_Index(*j, *i);
48                         if (glyph_index == 0)
49                                 continue;
50
51                         if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
52                                 throw std::runtime_error("Couldn't load glyph");
53                         slot = (*j)->glyph;
54                         break;
55                 }
56                 if (glyph_index == 0) {
57                         std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
58                         continue;
59                 }
60
61                 if (buf != NULL) {
62                         int y;
63                         FT_Bitmap *bm = &(slot->bitmap);
64                         for (y = 0; y < bm->rows; y++) {
65                                 int xx;
66                                 int dsty = ypos - slot->bitmap_top + y;
67                                 if (dsty < 0 || dsty > 599) continue;
68
69                                 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
70                                 unsigned char *src = bm->buffer + y * bm->width;
71
72                                 int width = (x + xpos + slot->bitmap_left + bm->width >= 800) ? (799 - x - xpos - slot->bitmap_left) : bm->width;
73                                 for (xx = 0; xx < width; xx++) {
74                                         *dst = (*dst * (256-*src) + r * *src) >> 8;
75                                         *dst++;
76                                         *dst = (*dst * (256-*src) + g * *src) >> 8;
77                                         *dst++;
78                                         *dst = (*dst * (256-*src) + b * *src) >> 8;
79                                         *dst++;
80                                         *dst++ = 0;
81                                         src++;
82                                 }
83                         }
84                 }
85
86                 x += slot->advance.x >> 6;
87         }
88
89         return x;
90 }