text: font: add unifont backend
[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 "unicode.h"
44 #include "uterm.h"
45
46 /* chars */
47
48 /* TODO: rename to kmscon_char_attr */
49 struct font_char_attr {
50         uint8_t fr;                     /* foreground red */
51         uint8_t fg;                     /* foreground green */
52         uint8_t fb;                     /* foreground blue */
53         uint8_t br;                     /* background red */
54         uint8_t bg;                     /* background green */
55         uint8_t bb;                     /* background blue */
56         unsigned int bold : 1;          /* bold character */
57         unsigned int underline : 1;     /* underlined character */
58         unsigned int inverse : 1;       /* inverse colors */
59         unsigned int protect : 1;       /* cannot be erased */
60 };
61
62 /* fonts */
63
64 struct kmscon_font_attr;
65 struct kmscon_glyph;
66 struct kmscon_font;
67 struct kmscon_font_ops;
68
69 #define KMSCON_FONT_MAX_NAME 128
70 #define KMSCON_FONT_DEFAULT_NAME "monospace"
71 #define KMSCON_FONT_DEFAULT_PPI 72
72
73 struct kmscon_font_attr {
74         char name[KMSCON_FONT_MAX_NAME];
75         unsigned int ppi;
76         unsigned int points;
77         bool bold;
78         bool italic;
79         unsigned int height;
80         unsigned int width;
81 };
82
83 void kmscon_font_attr_normalize(struct kmscon_font_attr *attr);
84 bool kmscon_font_attr_match(const struct kmscon_font_attr *a1,
85                             const struct kmscon_font_attr *a2);
86
87 struct kmscon_glyph {
88         struct uterm_video_buffer buf;
89         void *data;
90 };
91
92 struct kmscon_font {
93         unsigned long ref;
94         const struct kmscon_font_ops *ops;
95         struct kmscon_font_attr attr;
96         unsigned int baseline;
97         void *data;
98 };
99
100 struct kmscon_font_ops {
101         const char *name;
102         int (*init) (struct kmscon_font *out,
103                      const struct kmscon_font_attr *attr);
104         void (*destroy) (struct kmscon_font *font);
105         int (*render) (struct kmscon_font *font, kmscon_symbol_t sym,
106                        const struct kmscon_glyph **out);
107         int (*render_empty) (struct kmscon_font *font,
108                              const struct kmscon_glyph **out);
109         int (*render_inval) (struct kmscon_font *font,
110                              const struct kmscon_glyph **out);
111 };
112
113 int kmscon_font_register(const struct kmscon_font_ops *ops);
114 void kmscon_font_unregister(const char *name);
115
116 int kmscon_font_find(struct kmscon_font **out,
117                      const struct kmscon_font_attr *attr,
118                      const char *backend);
119 void kmscon_font_ref(struct kmscon_font *font);
120 void kmscon_font_unref(struct kmscon_font *font);
121
122 int kmscon_font_render(struct kmscon_font *font, kmscon_symbol_t sym,
123                        const struct kmscon_glyph **out);
124 int kmscon_font_render_empty(struct kmscon_font *font,
125                              const struct kmscon_glyph **out);
126 int kmscon_font_render_inval(struct kmscon_font *font,
127                              const struct kmscon_glyph **out);
128
129 /* text renderer */
130
131 struct kmscon_text;
132 struct kmscon_text_ops;
133
134 struct kmscon_text {
135         unsigned long ref;
136         const struct kmscon_text_ops *ops;
137         void *data;
138
139         struct kmscon_font *font;
140         struct uterm_screen *screen;
141         unsigned int cols;
142         unsigned int rows;
143         bool rendering;
144 };
145
146 struct kmscon_text_ops {
147         const char *name;
148         int (*init) (struct kmscon_text *txt);
149         void (*destroy) (struct kmscon_text *txt);
150         int (*set) (struct kmscon_text *txt);
151         void (*unset) (struct kmscon_text *txt);
152         int (*prepare) (struct kmscon_text *txt);
153         int (*draw) (struct kmscon_text *txt, kmscon_symbol_t ch,
154                      unsigned int posx, unsigned int posy,
155                      const struct font_char_attr *attr);
156         int (*render) (struct kmscon_text *txt);
157         void (*abort) (struct kmscon_text *txt);
158 };
159
160 int kmscon_text_register(const struct kmscon_text_ops *ops);
161 void kmscon_text_unregister(const char *name);
162
163 int kmscon_text_new(struct kmscon_text **out, const char *backend);
164 void kmscon_text_ref(struct kmscon_text *txt);
165 void kmscon_text_unref(struct kmscon_text *txt);
166
167 int kmscon_text_set(struct kmscon_text *txt,
168                     struct kmscon_font *font,
169                     struct uterm_screen *screen);
170 void kmscon_text_unset(struct kmscon_text *txt);
171 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
172 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
173
174 int kmscon_text_prepare(struct kmscon_text *txt);
175 int kmscon_text_draw(struct kmscon_text *txt, kmscon_symbol_t ch,
176                       unsigned int posx, unsigned int posy,
177                       const struct font_char_attr *attr);
178 int kmscon_text_render(struct kmscon_text *txt);
179 void kmscon_text_abort(struct kmscon_text *txt);
180
181 /* modularized backends */
182
183 #ifdef KMSCON_HAVE_UNIFONT
184
185 int kmscon_font_unifont_load(void);
186 void kmscon_font_unifont_unload(void);
187
188 #else
189
190 static inline int kmscon_font_unifont_load(void)
191 {
192         return -EOPNOTSUPP;
193 }
194
195 static inline void kmscon_font_unifont_unload(void)
196 {
197 }
198
199 #endif
200
201
202 #ifdef KMSCON_HAVE_8X16
203
204 int kmscon_font_8x16_load(void);
205 void kmscon_font_8x16_unload(void);
206
207 #else
208
209 static inline int kmscon_font_8x16_load(void)
210 {
211         return -EOPNOTSUPP;
212 }
213
214 static inline void kmscon_font_8x16_unload(void)
215 {
216 }
217
218 #endif
219
220 #ifdef KMSCON_HAVE_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 KMSCON_HAVE_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 KMSCON_HAVE_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 KMSCON_HAVE_GLES2
275
276 int kmscon_text_gltex_load(void);
277 void kmscon_text_gltex_unload(void);
278
279 #else
280
281 static inline int kmscon_text_gltex_load(void)
282 {
283         return -EOPNOTSUPP;
284 }
285
286 static inline void kmscon_text_gltex_unload(void)
287 {
288 }
289
290 #endif
291
292 #endif /* KMSCON_TEXT_H */