2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
23 #include <freetype/freetype.h>
30 #define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
33 static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
46 if(!(text = (Text*)sh_malloc(sizeof *text))) {
50 if(FT_Init_FreeType(&text->ft_lib)) {
51 fprintf(stderr, "Failed to initialize the FreeType library!\n");
55 if(FT_New_Face(text->ft_lib, FONT_PATH, 0, &text->ft_face)) {
56 fprintf(stderr, "Failed to load font: %s\n", FONT_PATH);
60 if(FT_Set_Char_Size(text->ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
61 fprintf(stderr, "Failed to set font size\n");
65 set_text_color(255, 255, 255);
75 void draw_text(const char *txt, Pixmap *pixmap)
78 pixmap = get_framebuffer_pixmap();
82 text->text_x += draw_glyph(pixmap, text->text_x, text->text_y, *txt);
87 void set_text_position(int x, int y)
94 void set_text_color(int r, int g, int b)
96 text->text_color[0] = r;
97 text->text_color[1] = g;
98 text->text_color[2] = b;
101 static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
103 if(FT_Load_Char(text->ft_face, c, FT_LOAD_RENDER)) {
107 x += text->ft_face->glyph->bitmap_left;
108 y -= text->ft_face->glyph->bitmap_top;
110 FT_Bitmap *ft_bmp = &text->ft_face->glyph->bitmap;
111 unsigned char *bmp_ptr = ft_bmp->buffer;
112 unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4;
114 Rect clipping_rect = get_clipping_rect();
116 for(int i=0; i<ft_bmp->rows; i++) {
118 if(dest_y >= clipping_rect.y + clipping_rect.height) {
122 if(dest_y >= clipping_rect.y) {
123 for(int j=0; j<ft_bmp->width; j++) {
126 if(dest_x >= clipping_rect.x + clipping_rect.width) {
130 if(bmp_ptr[j] && dest_x >= clipping_rect.x) {
131 int a = (int)bmp_ptr[j];
132 pxm_ptr[4 * j] = (a * text->text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
133 pxm_ptr[4 * j + 1] = (a * text->text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
134 pxm_ptr[4 * j + 2] = (a * text->text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
139 pxm_ptr += 4 * pixmap->get_width();
140 bmp_ptr += ft_bmp->pitch;
143 return text->ft_face->glyph->advance.x >> 6;