5 std::vector<FT_Face> fonts;
11 if (FT_Init_FreeType(&library))
12 throw std::runtime_error("FreeType init failed.");
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);
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");
23 fonts.push_back(face);
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");
30 fonts.push_back(face);
34 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, int xpos, int ypos, int r, int g, int b)
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");
44 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
46 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
47 glyph_index = FT_Get_Char_Index(*j, *i);
51 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
52 throw std::runtime_error("Couldn't load glyph");
56 if (glyph_index == 0) {
57 std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
63 FT_Bitmap *bm = &(slot->bitmap);
64 for (y = 0; y < bm->rows; y++) {
66 int dsty = ypos - slot->bitmap_top + y;
67 if (dsty < 0 || dsty > 599) continue;
69 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
70 unsigned char *src = bm->buffer + y * bm->width;
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;
76 *dst = (*dst * (256-*src) + g * *src) >> 8;
78 *dst = (*dst * (256-*src) + b * *src) >> 8;
86 x += slot->advance.x >> 6;