text: font: add .finalize callback
[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         unsigned int width;
74         void *data;
75 };
76
77 struct kmscon_font {
78         unsigned long ref;
79         struct shl_register_record *record;
80         const struct kmscon_font_ops *ops;
81         struct kmscon_font_attr attr;
82         unsigned int baseline;
83         void *data;
84 };
85
86 struct kmscon_font_ops {
87         const char *name;
88         int (*init) (struct kmscon_font *out,
89                      const struct kmscon_font_attr *attr);
90         void (*destroy) (struct kmscon_font *font);
91         int (*render) (struct kmscon_font *font,
92                        uint32_t id, const uint32_t *ch, size_t len,
93                        const struct kmscon_glyph **out);
94         int (*render_empty) (struct kmscon_font *font,
95                              const struct kmscon_glyph **out);
96         int (*render_inval) (struct kmscon_font *font,
97                              const struct kmscon_glyph **out);
98         void (*finalize) (void);
99 };
100
101 int kmscon_font_register(const struct kmscon_font_ops *ops);
102 void kmscon_font_unregister(const char *name);
103
104 int kmscon_font_find(struct kmscon_font **out,
105                      const struct kmscon_font_attr *attr,
106                      const char *backend);
107 void kmscon_font_ref(struct kmscon_font *font);
108 void kmscon_font_unref(struct kmscon_font *font);
109
110 int kmscon_font_render(struct kmscon_font *font,
111                        uint32_t id, const uint32_t *ch, size_t len,
112                        const struct kmscon_glyph **out);
113 int kmscon_font_render_empty(struct kmscon_font *font,
114                              const struct kmscon_glyph **out);
115 int kmscon_font_render_inval(struct kmscon_font *font,
116                              const struct kmscon_glyph **out);
117
118 /* text renderer */
119
120 struct kmscon_text;
121 struct kmscon_text_ops;
122
123 struct kmscon_text {
124         unsigned long ref;
125         struct shl_register_record *record;
126         const struct kmscon_text_ops *ops;
127         void *data;
128
129         struct kmscon_font *font;
130         struct kmscon_font *bold_font;
131         struct uterm_display *disp;
132         unsigned int cols;
133         unsigned int rows;
134         bool rendering;
135 };
136
137 struct kmscon_text_ops {
138         const char *name;
139         int (*init) (struct kmscon_text *txt);
140         void (*destroy) (struct kmscon_text *txt);
141         int (*set) (struct kmscon_text *txt);
142         void (*unset) (struct kmscon_text *txt);
143         int (*prepare) (struct kmscon_text *txt);
144         int (*draw) (struct kmscon_text *txt,
145                      uint32_t id, const uint32_t *ch, size_t len,
146                      unsigned int width,
147                      unsigned int posx, unsigned int posy,
148                      const struct tsm_screen_attr *attr);
149         int (*render) (struct kmscon_text *txt);
150         void (*abort) (struct kmscon_text *txt);
151 };
152
153 int kmscon_text_register(const struct kmscon_text_ops *ops);
154 void kmscon_text_unregister(const char *name);
155
156 int kmscon_text_new(struct kmscon_text **out, const char *backend);
157 void kmscon_text_ref(struct kmscon_text *txt);
158 void kmscon_text_unref(struct kmscon_text *txt);
159
160 int kmscon_text_set(struct kmscon_text *txt,
161                     struct kmscon_font *font,
162                     struct kmscon_font *bold_font,
163                     struct uterm_display *disp);
164 void kmscon_text_unset(struct kmscon_text *txt);
165 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
166 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
167
168 int kmscon_text_prepare(struct kmscon_text *txt);
169 int kmscon_text_draw(struct kmscon_text *txt,
170                      uint32_t id, const uint32_t *ch, size_t len,
171                      unsigned int width,
172                      unsigned int posx, unsigned int posy,
173                      const struct tsm_screen_attr *attr);
174 int kmscon_text_render(struct kmscon_text *txt);
175 void kmscon_text_abort(struct kmscon_text *txt);
176
177 int kmscon_text_prepare_cb(struct tsm_screen *con, void *data);
178 int kmscon_text_draw_cb(struct tsm_screen *con,
179                         uint32_t id, const uint32_t *ch, size_t len,
180                         unsigned int width,
181                         unsigned int posx, unsigned int posy,
182                         const struct tsm_screen_attr *attr, void *data);
183 int kmscon_text_render_cb(struct tsm_screen *con, void *data);
184
185 /* modularized backends */
186
187 extern struct kmscon_font_ops kmscon_font_8x16_ops;
188 extern struct kmscon_font_ops kmscon_font_unifont_ops;
189 extern struct kmscon_font_ops kmscon_font_freetype2_ops;
190 extern struct kmscon_font_ops kmscon_font_pango_ops;
191
192 #ifdef BUILD_ENABLE_RENDERER_BBLIT
193
194 int kmscon_text_bblit_load(void);
195 void kmscon_text_bblit_unload(void);
196
197 #else
198
199 static inline int kmscon_text_bblit_load(void)
200 {
201         return -EOPNOTSUPP;
202 }
203
204 static inline void kmscon_text_bblit_unload(void)
205 {
206 }
207
208 #endif
209
210 #ifdef BUILD_ENABLE_RENDERER_BBULK
211
212 int kmscon_text_bbulk_load(void);
213 void kmscon_text_bbulk_unload(void);
214
215 #else
216
217 static inline int kmscon_text_bbulk_load(void)
218 {
219         return -EOPNOTSUPP;
220 }
221
222 static inline void kmscon_text_bbulk_unload(void)
223 {
224 }
225
226 #endif
227
228 #ifdef BUILD_ENABLE_RENDERER_GLTEX
229
230 int kmscon_text_gltex_load(void);
231 void kmscon_text_gltex_unload(void);
232
233 #else
234
235 static inline int kmscon_text_gltex_load(void)
236 {
237         return -EOPNOTSUPP;
238 }
239
240 static inline void kmscon_text_gltex_unload(void)
241 {
242 }
243
244 #endif
245
246 static inline void kmscon_text_load_all(void)
247 {
248         kmscon_text_bbulk_load();
249         kmscon_text_bblit_load();
250         kmscon_text_gltex_load();
251 }
252
253 static inline void kmscon_text_unload_all(void)
254 {
255         kmscon_text_gltex_unload();
256         kmscon_text_bblit_unload();
257         kmscon_text_bbulk_unload();
258 }
259
260 #endif /* KMSCON_TEXT_H */