added license GPL
[winnie] / src / text.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
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.
10
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.
15
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/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #include <ft2build.h>
23 #include <freetype/freetype.h>
24
25 #include "gfx.h"
26 #include "shalloc.h"
27 #include "text.h"
28
29 #define DPI 72
30 #define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
31 #define FONT_SIZE 16
32
33 static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
34
35 struct Text {
36         FT_Library ft_lib;
37         FT_Face ft_face;
38         int text_x, text_y;
39         int text_color[3];
40 };
41
42 static Text *text;
43
44 bool init_text()
45 {
46         if(!(text = (Text*)sh_malloc(sizeof *text))) {
47                 return false;
48         }
49
50         if(FT_Init_FreeType(&text->ft_lib)) {
51                 fprintf(stderr, "Failed to initialize the FreeType library!\n");
52                 return false;
53         }
54
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);
57                 return false;
58         }
59
60         if(FT_Set_Char_Size(text->ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
61                 fprintf(stderr, "Failed to set font size\n");
62                 return false;
63         }
64
65         set_text_color(255, 255, 255);
66
67         return true;
68 }
69
70 void destroy_text()
71 {
72         sh_free(text);
73 }
74
75 void draw_text(const char *txt, Pixmap *pixmap)
76 {
77         if(!pixmap) {
78                 pixmap = get_framebuffer_pixmap();
79         }
80
81         while(*txt != 0) {
82                 text->text_x += draw_glyph(pixmap, text->text_x, text->text_y, *txt);
83                 txt++;
84         }
85 }
86
87 void set_text_position(int x, int y)
88 {
89         text->text_x = x;
90         text->text_y = y;
91
92 }
93
94 void set_text_color(int r, int g, int b)
95 {
96         text->text_color[0] = r;
97         text->text_color[1] = g;
98         text->text_color[2] = b;
99 }
100
101 static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
102 {
103         if(FT_Load_Char(text->ft_face, c, FT_LOAD_RENDER)) {
104                 return 0;
105         }
106
107         x += text->ft_face->glyph->bitmap_left;
108         y -= text->ft_face->glyph->bitmap_top;
109
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;
113
114         Rect clipping_rect = get_clipping_rect();
115
116         for(int i=0; i<ft_bmp->rows; i++) {
117                 int dest_y = i + y;
118                 if(dest_y >= clipping_rect.y + clipping_rect.height) {
119                         break;
120                 }
121
122                 if(dest_y >= clipping_rect.y) {
123                         for(int j=0; j<ft_bmp->width; j++) {
124                                 int dest_x = j + x;
125
126                                 if(dest_x >= clipping_rect.x + clipping_rect.width) {
127                                         break;
128                                 }
129
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;
135                                 }
136                         }
137                 }
138
139                 pxm_ptr += 4 * pixmap->get_width();
140                 bmp_ptr += ft_bmp->pitch;
141         }
142
143         return text->ft_face->glyph->advance.x >> 6;
144 }