2 #include <freetype/freetype.h>
9 #define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
12 static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
25 if(!(text = (Text*)sh_malloc(sizeof *text))) {
29 if(FT_Init_FreeType(&text->ft_lib)) {
30 fprintf(stderr, "Failed to initialize the FreeType library!\n");
34 if(FT_New_Face(text->ft_lib, FONT_PATH, 0, &text->ft_face)) {
35 fprintf(stderr, "Failed to load font: %s\n", FONT_PATH);
39 if(FT_Set_Char_Size(text->ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
40 fprintf(stderr, "Failed to set font size\n");
44 set_text_color(255, 255, 255);
54 void draw_text(const char *txt, Pixmap *pixmap)
57 pixmap = get_framebuffer_pixmap();
61 text->text_x += draw_glyph(pixmap, text->text_x, text->text_y, *txt);
66 void set_text_position(int x, int y)
73 void set_text_color(int r, int g, int b)
75 text->text_color[0] = r;
76 text->text_color[1] = g;
77 text->text_color[2] = b;
80 static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
82 if(FT_Load_Char(text->ft_face, c, FT_LOAD_RENDER)) {
86 x += text->ft_face->glyph->bitmap_left;
87 y -= text->ft_face->glyph->bitmap_top;
89 FT_Bitmap *ft_bmp = &text->ft_face->glyph->bitmap;
90 unsigned char *bmp_ptr = ft_bmp->buffer;
91 unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4;
93 Rect clipping_rect = get_clipping_rect();
95 for(int i=0; i<ft_bmp->rows; i++) {
97 if(dest_y >= clipping_rect.y + clipping_rect.height) {
101 if(dest_y >= clipping_rect.y) {
102 for(int j=0; j<ft_bmp->width; j++) {
105 if(dest_x >= clipping_rect.x + clipping_rect.width) {
109 if(bmp_ptr[j] && dest_x >= clipping_rect.x) {
110 int a = (int)bmp_ptr[j];
111 pxm_ptr[4 * j] = (a * text->text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
112 pxm_ptr[4 * j + 1] = (a * text->text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
113 pxm_ptr[4 * j + 2] = (a * text->text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
118 pxm_ptr += 4 * pixmap->get_width();
119 bmp_ptr += ft_bmp->pitch;
122 return text->ft_face->glyph->advance.x >> 6;