4 #include "resolution.h"
6 std::vector<FT_Face> fonts;
12 if (FT_Init_FreeType(&library))
13 throw std::runtime_error("FreeType init failed.");
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);
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");
24 fonts.push_back(face);
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");
31 fonts.push_back(face);
35 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, int xpos, int ypos, int r, int g, int b)
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, 96))
42 throw std::runtime_error("Couldn't set font size");
45 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
47 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
48 glyph_index = FT_Get_Char_Index(*j, *i);
52 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
53 throw std::runtime_error("Couldn't load glyph");
57 if (glyph_index == 0) {
58 std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
64 FT_Bitmap *bm = &(slot->bitmap);
65 for (y = 0; y < bm->rows; y++) {
67 int dsty = ypos - slot->bitmap_top + y;
68 if (dsty < 0 || dsty > (SCREEN_HEIGHT-1)) continue;
70 unsigned char *dst = buf + dsty * SCREEN_WIDTH*4 + (x + xpos + slot->bitmap_left)*4;
71 unsigned char *src = bm->buffer + y * bm->width;
73 int width = (x + xpos + slot->bitmap_left + bm->width >= SCREEN_WIDTH) ? ((SCREEN_WIDTH-1) - x - xpos - slot->bitmap_left) : bm->width;
74 for (xx = 0; xx < width; xx++) {
75 *dst = (*dst * (256-*src) + b * *src) >> 8;
77 *dst = (*dst * (256-*src) + g * *src) >> 8;
79 *dst = (*dst * (256-*src) + r * *src) >> 8;
87 x += slot->advance.x >> 6;
93 void my_draw_text_deferred(std::vector<TextDefer> &td, const widestring &str, double size, int xpos, int ypos)
103 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> ¤t, std::vector<TextDefer> &old)
105 for (unsigned i = 0; i < current.size(); ++i) {
107 if (i < old.size() && current[i].str != old[i].str) {
116 my_draw_text(current[i].str, buf, current[i].size, current[i].xpos, current[i].ypos, r, g, b);