5 #include "resolution.h"
7 std::vector<FT_Face> fonts;
13 if (FT_Init_FreeType(&library))
14 throw std::runtime_error("FreeType init failed.");
17 if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &face))
18 throw std::runtime_error("Face opening failed.");
19 fonts.push_back(face);
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");
25 fonts.push_back(face);
29 if (FT_New_Face(library, "arialuni.ttf", 0, &face)) {
30 std::fprintf(stderr, "Warning: Couldn't open Arial Unicode MS, some glyphs might not be available\n");
32 fonts.push_back(face);
36 // this should really be done somehow else :-)
37 static unsigned screen_width = SCREEN_WIDTH, screen_height = SCREEN_HEIGHT;
38 void set_screen_size(unsigned width, unsigned height)
41 screen_height = height;
44 unsigned my_draw_text(const widestring &str, unsigned char *buf, double size, int xpos, int ypos, int r, int g, int b)
49 for (std::vector<FT_Face>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
50 if (FT_Set_Char_Size(*i, 0, unsigned(size * 64.0), 96 * screen_width/LOGICAL_SCREEN_WIDTH, 96 * screen_height/LOGICAL_SCREEN_HEIGHT))
51 throw std::runtime_error("Couldn't set font size");
55 xpos = xpos * screen_width / LOGICAL_SCREEN_WIDTH;
56 ypos = ypos * screen_height / LOGICAL_SCREEN_HEIGHT;
58 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
60 for (std::vector<FT_Face>::const_iterator j = fonts.begin(); j != fonts.end(); ++j) {
61 glyph_index = FT_Get_Char_Index(*j, *i);
66 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_LCD))
68 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
70 throw std::runtime_error("Couldn't load glyph");
74 if (glyph_index == 0) {
75 std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
81 FT_Bitmap *bm = &(slot->bitmap);
82 for (y = 0; y < bm->rows; y++) {
84 int dsty = ypos - slot->bitmap_top + y;
85 if (dsty < 0 || dsty > signed(screen_height-1)) continue;
87 unsigned char *dst = buf + dsty * screen_width*4 + (x + xpos + slot->bitmap_left)*4;
89 unsigned char *src = bm->buffer + y * bm->pitch;
90 int width = (x + xpos + slot->bitmap_left + bm->width/3 >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width/3;
92 unsigned char *src = bm->buffer + y * bm->pitch;
93 int width = (x + xpos + slot->bitmap_left + bm->width >= signed(screen_width)) ? ((screen_width-1) - x - xpos - slot->bitmap_left) : bm->width;
97 for (xx = 0; xx < width; xx++) {
98 *dst = (*dst * (256-src[2]) + b * src[2]) >> 8;
100 *dst = (*dst * (256-src[1]) + g * src[1]) >> 8;
102 *dst = (*dst * (256-src[0]) + r * src[0]) >> 8;
109 for (xx = 0; xx < width; xx++) {
110 *dst = (*dst * (256-*src) + b * *src) >> 8;
112 *dst = (*dst * (256-*src) + g * *src) >> 8;
114 *dst = (*dst * (256-*src) + r * *src) >> 8;
123 x += slot->advance.x >> 6;
126 return x * LOGICAL_SCREEN_WIDTH / screen_width;
129 void my_draw_text_deferred(std::vector<TextDefer> &td, const widestring &str, double size, int xpos, int ypos, int r, int g, int b, int rn, int gn, int bn)
145 void draw_all_deferred_text(unsigned char *buf, std::vector<TextDefer> ¤t, std::vector<TextDefer> &old)
147 for (unsigned i = 0; i < current.size(); ++i) {
149 if (i < old.size() && current[i].str != old[i].str) {
160 my_draw_text(current[i].str, buf, current[i].size, current[i].xpos, current[i].ypos, r, g, b);