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