2 #include <freetype/freetype.h>
8 #define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
11 static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
13 static FT_Library ft_lib;
14 static FT_Face ft_face;
16 static int text_x, text_y;
17 static int text_color[3] = {255, 255, 255};
21 if(FT_Init_FreeType(&ft_lib)) {
22 fprintf(stderr, "Failed to initialize the FreeType library!\n");
26 if(FT_New_Face(ft_lib, FONT_PATH, 0, &ft_face)) {
27 fprintf(stderr, "Failed to load font: %s\n", FONT_PATH);
31 if(FT_Set_Char_Size(ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
32 fprintf(stderr, "Failed to set font size\n");
39 void draw_text(const char *txt, Pixmap *pixmap)
42 pixmap = get_framebuffer_pixmap();
46 text_x += draw_glyph(pixmap, text_x, text_y, *txt);
51 void set_text_position(int x, int y)
58 void set_text_color(int r, int g, int b)
65 static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
67 if(FT_Load_Char(ft_face, c, FT_LOAD_RENDER)) {
71 x += ft_face->glyph->bitmap_left;
72 y -= ft_face->glyph->bitmap_top;
74 FT_Bitmap *ft_bmp = &ft_face->glyph->bitmap;
75 unsigned char *bmp_ptr = ft_bmp->buffer;
76 unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4;
78 Rect clipping_rect = get_clipping_rect();
80 for(int i=0; i<ft_bmp->rows; i++) {
82 if(dest_y >= clipping_rect.y + clipping_rect.height) {
86 if(dest_y >= clipping_rect.y) {
87 for(int j=0; j<ft_bmp->width; j++) {
90 if(dest_x >= clipping_rect.x + clipping_rect.width) {
94 if(bmp_ptr[j] && dest_x >= clipping_rect.x) {
95 int a = (int)bmp_ptr[j];
96 pxm_ptr[4 * j] = (a * text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
97 pxm_ptr[4 * j + 1] = (a * text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
98 pxm_ptr[4 * j + 2] = (a * text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
103 pxm_ptr += 4 * pixmap->get_width();
104 bmp_ptr += ft_bmp->pitch;
107 return ft_face->glyph->advance.x >> 6;