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