text: font: Simplify font-system and add freetype2 renderer
[platform/upstream/kmscon.git] / src / text.h
1 /*
2  * kmscon - Text Renderer
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Text Renderer
28  * The Text-Renderer subsystem provides a simple way to draw text into a
29  * framebuffer. The system is modular and several different backends are
30  * available that can be used.
31  * The system is split into:
32  *  - Font renderer: The font renderer allows selecting fonts and rendering
33  *    single glyphs into memory-buffers
34  *  - Text renderer: The text renderer uses the font renderer to draw a whole
35  *    console into a framebuffer.
36  */
37
38 #ifndef KMSCON_TEXT_H
39 #define KMSCON_TEXT_H
40
41 #include <stdlib.h>
42 #include "font.h"
43 #include "unicode.h"
44 #include "uterm.h"
45
46 /* fonts */
47
48 struct kmscon_font_attr;
49 struct kmscon_glyph;
50 struct kmscon_font;
51 struct kmscon_font_ops;
52
53 #define KMSCON_FONT_MAX_NAME 128
54 #define KMSCON_FONT_DEFAULT_NAME "monospace"
55 #define KMSCON_FONT_DEFAULT_PPI 72
56
57 struct kmscon_font_attr {
58         char name[KMSCON_FONT_MAX_NAME];
59         unsigned int ppi;
60         unsigned int points;
61         bool bold;
62         bool italic;
63         unsigned int height;
64         unsigned int width;
65 };
66
67 void kmscon_font_attr_normalize(struct kmscon_font_attr *attr);
68 bool kmscon_font_attr_match(const struct kmscon_font_attr *a1,
69                             const struct kmscon_font_attr *a2);
70
71 struct kmscon_glyph {
72         struct uterm_video_buffer buf;
73         void *data;
74 };
75
76 struct kmscon_font {
77         unsigned long ref;
78         const struct kmscon_font_ops *ops;
79         struct kmscon_font_attr attr;
80         unsigned int baseline;
81         void *data;
82 };
83
84 struct kmscon_font_ops {
85         const char *name;
86         int (*init) (struct kmscon_font *out,
87                      const struct kmscon_font_attr *attr);
88         void (*destroy) (struct kmscon_font *font);
89         int (*render) (struct kmscon_font *font, kmscon_symbol_t sym,
90                        const struct kmscon_glyph **out);
91         int (*render_empty) (struct kmscon_font *font,
92                              const struct kmscon_glyph **out);
93         int (*render_inval) (struct kmscon_font *font,
94                              const struct kmscon_glyph **out);
95 };
96
97 int kmscon_font_register(const struct kmscon_font_ops *ops);
98 void kmscon_font_unregister(const char *name);
99
100 int kmscon_font_find(struct kmscon_font **out,
101                      const struct kmscon_font_attr *attr,
102                      const char *backend);
103 void kmscon_font_ref(struct kmscon_font *font);
104 void kmscon_font_unref(struct kmscon_font *font);
105
106 int kmscon_font_render(struct kmscon_font *font, kmscon_symbol_t sym,
107                        const struct kmscon_glyph **out);
108 int kmscon_font_render_empty(struct kmscon_font *font,
109                              const struct kmscon_glyph **out);
110 int kmscon_font_render_inval(struct kmscon_font *font,
111                              const struct kmscon_glyph **out);
112
113 /* text renderer */
114
115 struct kmscon_text;
116 struct kmscon_text_ops;
117
118 struct kmscon_text {
119         unsigned long ref;
120         const struct kmscon_text_ops *ops;
121         struct kmscon_font *font;
122         struct uterm_screen *screen;
123         uint8_t bg_r;
124         uint8_t bg_g;
125         uint8_t bg_b;
126         unsigned int cols;
127         unsigned int rows;
128         bool rendering;
129         void *data;
130 };
131
132 struct kmscon_text_ops {
133         const char *name;
134         int (*init) (struct kmscon_text *txt);
135         void (*destroy) (struct kmscon_text *txt);
136         void (*new_font) (struct kmscon_text *txt);
137         void (*new_bgcolor) (struct kmscon_text *txt);
138         void (*new_screen) (struct kmscon_text *txt);
139         void (*prepare) (struct kmscon_text *txt);
140         void (*draw) (struct kmscon_text *txt, kmscon_symbol_t ch,
141                       unsigned int posx, unsigned int posy,
142                       const struct font_char_attr *attr);
143         void (*render) (struct kmscon_text *txt);
144 };
145
146 int kmscon_text_register(const struct kmscon_text_ops *ops);
147 void kmscon_text_unregister(const char *name);
148
149 int kmscon_text_new(struct kmscon_text **out, const char *backend);
150 void kmscon_text_ref(struct kmscon_text *txt);
151 void kmscon_text_unref(struct kmscon_text *txt);
152
153 void kmscon_text_set_font(struct kmscon_text *txt,
154                           struct kmscon_font *font);
155 void kmscon_text_set_bgcolor(struct kmscon_text *txt,
156                              uint8_t r, uint8_t g, uint8_t b);
157 void kmscon_text_set_screen(struct kmscon_text *txt,
158                             struct uterm_screen *screen);
159 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
160 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
161
162 void kmscon_text_prepare(struct kmscon_text *txt);
163 void kmscon_text_draw(struct kmscon_text *txt, kmscon_symbol_t ch,
164                       unsigned int posx, unsigned int posy,
165                       const struct font_char_attr *attr);
166 void kmscon_text_render(struct kmscon_text *txt);
167
168 /* modularized backends */
169
170 #ifdef KMSCON_HAVE_8X16
171
172 int kmscon_font_8x16_load(void);
173 void kmscon_font_8x16_unload(void);
174
175 #else
176
177 static inline int kmscon_font_8x16_load(void)
178 {
179         return -EOPNOTSUPP;
180 }
181
182 static inline void kmscon_font_8x16_unload(void)
183 {
184 }
185
186 #endif
187
188 #ifdef KMSCON_HAVE_FREETYPE2
189
190 int kmscon_font_freetype2_load(void);
191 void kmscon_font_freetype2_unload(void);
192
193 #else
194
195 static inline int kmscon_font_freetype2_load(void)
196 {
197         return -EOPNOTSUPP;
198 }
199
200 static inline void kmscon_font_freetype2_unload(void)
201 {
202 }
203
204 #endif
205
206 #ifdef KMSCON_HAVE_PANGO
207
208 int kmscon_font_pango_load(void);
209 void kmscon_font_pango_unload(void);
210
211 #else
212
213 static inline int kmscon_font_pango_load(void)
214 {
215         return -EOPNOTSUPP;
216 }
217
218 static inline void kmscon_font_pango_unload(void)
219 {
220 }
221
222 #endif
223
224 int kmscon_text_bblit_load(void);
225 void kmscon_text_bblit_unload(void);
226
227 #endif /* KMSCON_TEXT_H */