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>
31 #define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
34 static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
47 if(!(text = (Text*)sh_malloc(sizeof *text))) {
51 get_subsys()->text_offset = (int)((char*)text - (char*)get_pool());
53 if(FT_Init_FreeType(&text->ft_lib)) {
54 fprintf(stderr, "Failed to initialize the FreeType library!\n");
58 if(FT_New_Face(text->ft_lib, FONT_PATH, 0, &text->ft_face)) {
59 fprintf(stderr, "Failed to load font: %s\n", FONT_PATH);
63 if(FT_Set_Char_Size(text->ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
64 fprintf(stderr, "Failed to set font size\n");
68 set_text_color(255, 255, 255);
78 void draw_text(const char *txt, Pixmap *pixmap)
81 pixmap = get_framebuffer_pixmap();
85 text->text_x += draw_glyph(pixmap, text->text_x, text->text_y, *txt);
90 void set_text_position(int x, int y)
97 void set_text_color(int r, int g, int b)
99 text->text_color[0] = r;
100 text->text_color[1] = g;
101 text->text_color[2] = b;
104 static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
106 if(FT_Load_Char(text->ft_face, c, FT_LOAD_RENDER)) {
110 x += text->ft_face->glyph->bitmap_left;
111 y -= text->ft_face->glyph->bitmap_top;
113 FT_Bitmap *ft_bmp = &text->ft_face->glyph->bitmap;
114 unsigned char *bmp_ptr = ft_bmp->buffer;
115 unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4;
117 Rect clipping_rect = get_clipping_rect();
119 for(int i=0; i<ft_bmp->rows; i++) {
121 if(dest_y >= clipping_rect.y + clipping_rect.height) {
125 if(dest_y >= clipping_rect.y) {
126 for(int j=0; j<ft_bmp->width; j++) {
129 if(dest_x >= clipping_rect.x + clipping_rect.width) {
133 if(bmp_ptr[j] && dest_x >= clipping_rect.x) {
134 int a = (int)bmp_ptr[j];
135 pxm_ptr[4 * j] = (a * text->text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
136 pxm_ptr[4 * j + 1] = (a * text->text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
137 pxm_ptr[4 * j + 2] = (a * text->text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
142 pxm_ptr += 4 * pixmap->get_width();
143 bmp_ptr += ft_bmp->pitch;
146 return text->ft_face->glyph->advance.x >> 6;