console: rename font_char_attr to kmscon_console_attr
[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 "console.h"
44 #include "unicode.h"
45 #include "uterm.h"
46
47 /* fonts */
48
49 struct kmscon_font_attr;
50 struct kmscon_glyph;
51 struct kmscon_font;
52 struct kmscon_font_ops;
53
54 #define KMSCON_FONT_MAX_NAME 128
55 #define KMSCON_FONT_DEFAULT_NAME "monospace"
56 #define KMSCON_FONT_DEFAULT_PPI 72
57
58 struct kmscon_font_attr {
59         char name[KMSCON_FONT_MAX_NAME];
60         unsigned int ppi;
61         unsigned int points;
62         bool bold;
63         bool italic;
64         unsigned int height;
65         unsigned int width;
66 };
67
68 void kmscon_font_attr_normalize(struct kmscon_font_attr *attr);
69 bool kmscon_font_attr_match(const struct kmscon_font_attr *a1,
70                             const struct kmscon_font_attr *a2);
71
72 struct kmscon_glyph {
73         struct uterm_video_buffer buf;
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, kmscon_symbol_t sym,
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, kmscon_symbol_t sym,
108                        const struct kmscon_glyph **out);
109 int kmscon_font_render_empty(struct kmscon_font *font,
110                              const struct kmscon_glyph **out);
111 int kmscon_font_render_inval(struct kmscon_font *font,
112                              const struct kmscon_glyph **out);
113
114 /* text renderer */
115
116 struct kmscon_text;
117 struct kmscon_text_ops;
118
119 struct kmscon_text {
120         unsigned long ref;
121         const struct kmscon_text_ops *ops;
122         void *data;
123
124         struct kmscon_font *font;
125         struct uterm_screen *screen;
126         unsigned int cols;
127         unsigned int rows;
128         bool rendering;
129 };
130
131 struct kmscon_text_ops {
132         const char *name;
133         int (*init) (struct kmscon_text *txt);
134         void (*destroy) (struct kmscon_text *txt);
135         int (*set) (struct kmscon_text *txt);
136         void (*unset) (struct kmscon_text *txt);
137         int (*prepare) (struct kmscon_text *txt);
138         int (*draw) (struct kmscon_text *txt, kmscon_symbol_t ch,
139                      unsigned int posx, unsigned int posy,
140                      const struct kmscon_console_attr *attr);
141         int (*render) (struct kmscon_text *txt);
142         void (*abort) (struct kmscon_text *txt);
143 };
144
145 int kmscon_text_register(const struct kmscon_text_ops *ops);
146 void kmscon_text_unregister(const char *name);
147
148 int kmscon_text_new(struct kmscon_text **out, const char *backend);
149 void kmscon_text_ref(struct kmscon_text *txt);
150 void kmscon_text_unref(struct kmscon_text *txt);
151
152 int kmscon_text_set(struct kmscon_text *txt,
153                     struct kmscon_font *font,
154                     struct uterm_screen *screen);
155 void kmscon_text_unset(struct kmscon_text *txt);
156 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
157 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
158
159 int kmscon_text_prepare(struct kmscon_text *txt);
160 int kmscon_text_draw(struct kmscon_text *txt, kmscon_symbol_t ch,
161                       unsigned int posx, unsigned int posy,
162                       const struct kmscon_console_attr *attr);
163 int kmscon_text_render(struct kmscon_text *txt);
164 void kmscon_text_abort(struct kmscon_text *txt);
165
166 /* modularized backends */
167
168 #ifdef KMSCON_HAVE_UNIFONT
169
170 int kmscon_font_unifont_load(void);
171 void kmscon_font_unifont_unload(void);
172
173 #else
174
175 static inline int kmscon_font_unifont_load(void)
176 {
177         return -EOPNOTSUPP;
178 }
179
180 static inline void kmscon_font_unifont_unload(void)
181 {
182 }
183
184 #endif
185
186
187 #ifdef KMSCON_HAVE_8X16
188
189 int kmscon_font_8x16_load(void);
190 void kmscon_font_8x16_unload(void);
191
192 #else
193
194 static inline int kmscon_font_8x16_load(void)
195 {
196         return -EOPNOTSUPP;
197 }
198
199 static inline void kmscon_font_8x16_unload(void)
200 {
201 }
202
203 #endif
204
205 #ifdef KMSCON_HAVE_FREETYPE2
206
207 int kmscon_font_freetype2_load(void);
208 void kmscon_font_freetype2_unload(void);
209
210 #else
211
212 static inline int kmscon_font_freetype2_load(void)
213 {
214         return -EOPNOTSUPP;
215 }
216
217 static inline void kmscon_font_freetype2_unload(void)
218 {
219 }
220
221 #endif
222
223 #ifdef KMSCON_HAVE_PANGO
224
225 int kmscon_font_pango_load(void);
226 void kmscon_font_pango_unload(void);
227
228 #else
229
230 static inline int kmscon_font_pango_load(void)
231 {
232         return -EOPNOTSUPP;
233 }
234
235 static inline void kmscon_font_pango_unload(void)
236 {
237 }
238
239 #endif
240
241 #ifdef KMSCON_HAVE_BBLIT
242
243 int kmscon_text_bblit_load(void);
244 void kmscon_text_bblit_unload(void);
245
246 #else
247
248 static inline int kmscon_text_bblit_load(void)
249 {
250         return -EOPNOTSUPP;
251 }
252
253 static inline void kmscon_text_bblit_unload(void)
254 {
255 }
256
257 #endif
258
259 #ifdef KMSCON_HAVE_BBULK
260
261 int kmscon_text_bbulk_load(void);
262 void kmscon_text_bbulk_unload(void);
263
264 #else
265
266 static inline int kmscon_text_bbulk_load(void)
267 {
268         return -EOPNOTSUPP;
269 }
270
271 static inline void kmscon_text_bbulk_unload(void)
272 {
273 }
274
275 #endif
276
277 #ifdef KMSCON_HAVE_GLES2
278
279 int kmscon_text_gltex_load(void);
280 void kmscon_text_gltex_unload(void);
281
282 #else
283
284 static inline int kmscon_text_gltex_load(void)
285 {
286         return -EOPNOTSUPP;
287 }
288
289 static inline void kmscon_text_gltex_unload(void)
290 {
291 }
292
293 #endif
294
295 #endif /* KMSCON_TEXT_H */