Merge branch 'github/master'
[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 <errno.h>
42 #include <stdlib.h>
43 #include "tsm_screen.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,
90                        uint32_t id, const uint32_t *ch, size_t len,
91                        const struct kmscon_glyph **out);
92         int (*render_empty) (struct kmscon_font *font,
93                              const struct kmscon_glyph **out);
94         int (*render_inval) (struct kmscon_font *font,
95                              const struct kmscon_glyph **out);
96 };
97
98 int kmscon_font_register(const struct kmscon_font_ops *ops);
99 void kmscon_font_unregister(const char *name);
100
101 int kmscon_font_find(struct kmscon_font **out,
102                      const struct kmscon_font_attr *attr,
103                      const char *backend);
104 void kmscon_font_ref(struct kmscon_font *font);
105 void kmscon_font_unref(struct kmscon_font *font);
106
107 int kmscon_font_render(struct kmscon_font *font,
108                        uint32_t id, const uint32_t *ch, size_t len,
109                        const struct kmscon_glyph **out);
110 int kmscon_font_render_empty(struct kmscon_font *font,
111                              const struct kmscon_glyph **out);
112 int kmscon_font_render_inval(struct kmscon_font *font,
113                              const struct kmscon_glyph **out);
114
115 /* text renderer */
116
117 struct kmscon_text;
118 struct kmscon_text_ops;
119
120 struct kmscon_text {
121         unsigned long ref;
122         const struct kmscon_text_ops *ops;
123         void *data;
124
125         struct kmscon_font *font;
126         struct uterm_screen *screen;
127         unsigned int cols;
128         unsigned int rows;
129         bool rendering;
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         int (*set) (struct kmscon_text *txt);
137         void (*unset) (struct kmscon_text *txt);
138         int (*prepare) (struct kmscon_text *txt);
139         int (*draw) (struct kmscon_text *txt,
140                      uint32_t id, const uint32_t *ch, size_t len,
141                      unsigned int posx, unsigned int posy,
142                      const struct tsm_screen_attr *attr);
143         int (*render) (struct kmscon_text *txt);
144         void (*abort) (struct kmscon_text *txt);
145 };
146
147 int kmscon_text_register(const struct kmscon_text_ops *ops);
148 void kmscon_text_unregister(const char *name);
149
150 int kmscon_text_new(struct kmscon_text **out, const char *backend);
151 void kmscon_text_ref(struct kmscon_text *txt);
152 void kmscon_text_unref(struct kmscon_text *txt);
153
154 int kmscon_text_set(struct kmscon_text *txt,
155                     struct kmscon_font *font,
156                     struct uterm_screen *screen);
157 void kmscon_text_unset(struct kmscon_text *txt);
158 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
159 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
160
161 int kmscon_text_prepare(struct kmscon_text *txt);
162 int kmscon_text_draw(struct kmscon_text *txt,
163                      uint32_t id, const uint32_t *ch, size_t len,
164                      unsigned int posx, unsigned int posy,
165                      const struct tsm_screen_attr *attr);
166 int kmscon_text_render(struct kmscon_text *txt);
167 void kmscon_text_abort(struct kmscon_text *txt);
168
169 int kmscon_text_prepare_cb(struct tsm_screen *con, void *data);
170 int kmscon_text_draw_cb(struct tsm_screen *con,
171                         uint32_t id, const uint32_t *ch, size_t len,
172                         unsigned int posx, unsigned int posy,
173                         const struct tsm_screen_attr *attr, void *data);
174 int kmscon_text_render_cb(struct tsm_screen *con, void *data);
175
176 /* modularized backends */
177
178 #ifdef BUILD_ENABLE_FONT_8X16
179
180 int kmscon_font_8x16_load(void);
181 void kmscon_font_8x16_unload(void);
182
183 #else
184
185 static inline int kmscon_font_8x16_load(void)
186 {
187         return -EOPNOTSUPP;
188 }
189
190 static inline void kmscon_font_8x16_unload(void)
191 {
192 }
193
194 #endif
195
196 #ifdef BUILD_ENABLE_FONT_UNIFONT
197
198 int kmscon_font_unifont_load(void);
199 void kmscon_font_unifont_unload(void);
200
201 #else
202
203 static inline int kmscon_font_unifont_load(void)
204 {
205         return -EOPNOTSUPP;
206 }
207
208 static inline void kmscon_font_unifont_unload(void)
209 {
210 }
211
212 #endif
213
214 #ifdef BUILD_ENABLE_FONT_FREETYPE2
215
216 int kmscon_font_freetype2_load(void);
217 void kmscon_font_freetype2_unload(void);
218
219 #else
220
221 static inline int kmscon_font_freetype2_load(void)
222 {
223         return -EOPNOTSUPP;
224 }
225
226 static inline void kmscon_font_freetype2_unload(void)
227 {
228 }
229
230 #endif
231
232 #ifdef BUILD_ENABLE_FONT_PANGO
233
234 int kmscon_font_pango_load(void);
235 void kmscon_font_pango_unload(void);
236
237 #else
238
239 static inline int kmscon_font_pango_load(void)
240 {
241         return -EOPNOTSUPP;
242 }
243
244 static inline void kmscon_font_pango_unload(void)
245 {
246 }
247
248 #endif
249
250 #ifdef BUILD_ENABLE_RENDERER_BBLIT
251
252 int kmscon_text_bblit_load(void);
253 void kmscon_text_bblit_unload(void);
254
255 #else
256
257 static inline int kmscon_text_bblit_load(void)
258 {
259         return -EOPNOTSUPP;
260 }
261
262 static inline void kmscon_text_bblit_unload(void)
263 {
264 }
265
266 #endif
267
268 #ifdef BUILD_ENABLE_RENDERER_BBULK
269
270 int kmscon_text_bbulk_load(void);
271 void kmscon_text_bbulk_unload(void);
272
273 #else
274
275 static inline int kmscon_text_bbulk_load(void)
276 {
277         return -EOPNOTSUPP;
278 }
279
280 static inline void kmscon_text_bbulk_unload(void)
281 {
282 }
283
284 #endif
285
286 #ifdef BUILD_ENABLE_RENDERER_GLTEX
287
288 int kmscon_text_gltex_load(void);
289 void kmscon_text_gltex_unload(void);
290
291 #else
292
293 static inline int kmscon_text_gltex_load(void)
294 {
295         return -EOPNOTSUPP;
296 }
297
298 static inline void kmscon_text_gltex_unload(void)
299 {
300 }
301
302 #endif
303
304 static inline void kmscon_font_load_all(void)
305 {
306         kmscon_font_8x16_load();
307         kmscon_font_unifont_load();
308         kmscon_font_pango_load();
309         kmscon_font_freetype2_load();
310 }
311
312 static inline void kmscon_font_unload_all(void)
313 {
314         kmscon_font_freetype2_unload();
315         kmscon_font_pango_unload();
316         kmscon_font_unifont_unload();
317         kmscon_font_8x16_unload();
318 }
319
320 static inline void kmscon_text_load_all(void)
321 {
322         kmscon_text_bbulk_load();
323         kmscon_text_bblit_load();
324         kmscon_text_gltex_load();
325 }
326
327 static inline void kmscon_text_unload_all(void)
328 {
329         kmscon_text_gltex_unload();
330         kmscon_text_bblit_unload();
331         kmscon_text_bbulk_unload();
332 }
333
334 #endif /* KMSCON_TEXT_H */