tsm: screen: move filenames to tsm_screen.[ch]
[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 KMSCON_HAVE_UNIFONT
179
180 int kmscon_font_unifont_load(void);
181 void kmscon_font_unifont_unload(void);
182
183 #else
184
185 static inline int kmscon_font_unifont_load(void)
186 {
187         return -EOPNOTSUPP;
188 }
189
190 static inline void kmscon_font_unifont_unload(void)
191 {
192 }
193
194 #endif
195
196
197 #ifdef KMSCON_HAVE_8X16
198
199 int kmscon_font_8x16_load(void);
200 void kmscon_font_8x16_unload(void);
201
202 #else
203
204 static inline int kmscon_font_8x16_load(void)
205 {
206         return -EOPNOTSUPP;
207 }
208
209 static inline void kmscon_font_8x16_unload(void)
210 {
211 }
212
213 #endif
214
215 #ifdef KMSCON_HAVE_FREETYPE2
216
217 int kmscon_font_freetype2_load(void);
218 void kmscon_font_freetype2_unload(void);
219
220 #else
221
222 static inline int kmscon_font_freetype2_load(void)
223 {
224         return -EOPNOTSUPP;
225 }
226
227 static inline void kmscon_font_freetype2_unload(void)
228 {
229 }
230
231 #endif
232
233 #ifdef KMSCON_HAVE_PANGO
234
235 int kmscon_font_pango_load(void);
236 void kmscon_font_pango_unload(void);
237
238 #else
239
240 static inline int kmscon_font_pango_load(void)
241 {
242         return -EOPNOTSUPP;
243 }
244
245 static inline void kmscon_font_pango_unload(void)
246 {
247 }
248
249 #endif
250
251 #ifdef KMSCON_HAVE_BBLIT
252
253 int kmscon_text_bblit_load(void);
254 void kmscon_text_bblit_unload(void);
255
256 #else
257
258 static inline int kmscon_text_bblit_load(void)
259 {
260         return -EOPNOTSUPP;
261 }
262
263 static inline void kmscon_text_bblit_unload(void)
264 {
265 }
266
267 #endif
268
269 #ifdef KMSCON_HAVE_BBULK
270
271 int kmscon_text_bbulk_load(void);
272 void kmscon_text_bbulk_unload(void);
273
274 #else
275
276 static inline int kmscon_text_bbulk_load(void)
277 {
278         return -EOPNOTSUPP;
279 }
280
281 static inline void kmscon_text_bbulk_unload(void)
282 {
283 }
284
285 #endif
286
287 #ifdef KMSCON_HAVE_GLES2
288
289 int kmscon_text_gltex_load(void);
290 void kmscon_text_gltex_unload(void);
291
292 #else
293
294 static inline int kmscon_text_gltex_load(void)
295 {
296         return -EOPNOTSUPP;
297 }
298
299 static inline void kmscon_text_gltex_unload(void)
300 {
301 }
302
303 #endif
304
305 #endif /* KMSCON_TEXT_H */