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