build: make bblit backend optional
[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         struct kmscon_font *font;
137         struct uterm_screen *screen;
138         uint8_t bg_r;
139         uint8_t bg_g;
140         uint8_t bg_b;
141         unsigned int cols;
142         unsigned int rows;
143         bool rendering;
144         void *data;
145 };
146
147 struct kmscon_text_ops {
148         const char *name;
149         int (*init) (struct kmscon_text *txt);
150         void (*destroy) (struct kmscon_text *txt);
151         void (*new_font) (struct kmscon_text *txt);
152         void (*new_bgcolor) (struct kmscon_text *txt);
153         void (*new_screen) (struct kmscon_text *txt);
154         void (*prepare) (struct kmscon_text *txt);
155         void (*draw) (struct kmscon_text *txt, kmscon_symbol_t ch,
156                       unsigned int posx, unsigned int posy,
157                       const struct font_char_attr *attr);
158         void (*render) (struct kmscon_text *txt);
159 };
160
161 int kmscon_text_register(const struct kmscon_text_ops *ops);
162 void kmscon_text_unregister(const char *name);
163
164 int kmscon_text_new(struct kmscon_text **out, const char *backend);
165 void kmscon_text_ref(struct kmscon_text *txt);
166 void kmscon_text_unref(struct kmscon_text *txt);
167
168 void kmscon_text_set_font(struct kmscon_text *txt,
169                           struct kmscon_font *font);
170 void kmscon_text_set_bgcolor(struct kmscon_text *txt,
171                              uint8_t r, uint8_t g, uint8_t b);
172 void kmscon_text_set_screen(struct kmscon_text *txt,
173                             struct uterm_screen *screen);
174 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
175 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
176
177 void kmscon_text_prepare(struct kmscon_text *txt);
178 void kmscon_text_draw(struct kmscon_text *txt, kmscon_symbol_t ch,
179                       unsigned int posx, unsigned int posy,
180                       const struct font_char_attr *attr);
181 void kmscon_text_render(struct kmscon_text *txt);
182
183 /* modularized backends */
184
185 #ifdef KMSCON_HAVE_8X16
186
187 int kmscon_font_8x16_load(void);
188 void kmscon_font_8x16_unload(void);
189
190 #else
191
192 static inline int kmscon_font_8x16_load(void)
193 {
194         return -EOPNOTSUPP;
195 }
196
197 static inline void kmscon_font_8x16_unload(void)
198 {
199 }
200
201 #endif
202
203 #ifdef KMSCON_HAVE_FREETYPE2
204
205 int kmscon_font_freetype2_load(void);
206 void kmscon_font_freetype2_unload(void);
207
208 #else
209
210 static inline int kmscon_font_freetype2_load(void)
211 {
212         return -EOPNOTSUPP;
213 }
214
215 static inline void kmscon_font_freetype2_unload(void)
216 {
217 }
218
219 #endif
220
221 #ifdef KMSCON_HAVE_PANGO
222
223 int kmscon_font_pango_load(void);
224 void kmscon_font_pango_unload(void);
225
226 #else
227
228 static inline int kmscon_font_pango_load(void)
229 {
230         return -EOPNOTSUPP;
231 }
232
233 static inline void kmscon_font_pango_unload(void)
234 {
235 }
236
237 #endif
238
239 #ifdef KMSCON_HAVE_BBLIT
240
241 int kmscon_text_bblit_load(void);
242 void kmscon_text_bblit_unload(void);
243
244 #else
245
246 static inline int kmscon_text_bblit_load(void)
247 {
248         return -EOPNOTSUPP;
249 }
250
251 static inline void kmscon_text_bblit_unload(void)
252 {
253 }
254
255 #endif
256
257 #endif /* KMSCON_TEXT_H */