text: add bblit text-renderer
[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 "font.h"
43 #include "unicode.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         unsigned int ascent;
73         unsigned int descent;
74         struct uterm_video_buffer buf;
75 };
76
77 struct kmscon_font {
78         unsigned long ref;
79         const struct kmscon_font_ops *ops;
80         struct kmscon_font_attr attr;
81         unsigned int baseline;
82         void *data;
83 };
84
85 struct kmscon_font_ops {
86         const char *name;
87         int (*init) (struct kmscon_font *out,
88                      const struct kmscon_font_attr *attr);
89         void (*destroy) (struct kmscon_font *font);
90         int (*render) (struct kmscon_font *font, kmscon_symbol_t sym,
91                        const struct kmscon_glyph **out);
92         void (*drop) (struct kmscon_font *font,
93                       const struct kmscon_glyph *glyph);
94 };
95
96 int kmscon_font_register(const struct kmscon_font_ops *ops);
97 void kmscon_font_unregister(const char *name);
98
99 int kmscon_font_find(struct kmscon_font **out,
100                      const struct kmscon_font_attr *attr,
101                      const char *backend);
102 void kmscon_font_ref(struct kmscon_font *font);
103 void kmscon_font_unref(struct kmscon_font *font);
104
105 int kmscon_font_render(struct kmscon_font *font, kmscon_symbol_t sym,
106                        const struct kmscon_glyph **out);
107 void kmscon_font_drop(struct kmscon_font *font,
108                       const struct kmscon_glyph *glyph);
109
110 /* text renderer */
111
112 struct kmscon_text;
113 struct kmscon_text_ops;
114
115 struct kmscon_text {
116         unsigned long ref;
117         const struct kmscon_text_ops *ops;
118         struct kmscon_font *font;
119         struct uterm_screen *screen;
120         uint8_t bg_r;
121         uint8_t bg_g;
122         uint8_t bg_b;
123         unsigned int cols;
124         unsigned int rows;
125         bool rendering;
126         void *data;
127 };
128
129 struct kmscon_text_ops {
130         const char *name;
131         int (*init) (struct kmscon_text *txt);
132         void (*destroy) (struct kmscon_text *txt);
133         void (*new_font) (struct kmscon_text *txt);
134         void (*new_bgcolor) (struct kmscon_text *txt);
135         void (*new_screen) (struct kmscon_text *txt);
136         void (*prepare) (struct kmscon_text *txt);
137         void (*draw) (struct kmscon_text *txt, kmscon_symbol_t ch,
138                       unsigned int posx, unsigned int posy,
139                       const struct font_char_attr *attr);
140         void (*render) (struct kmscon_text *txt);
141 };
142
143 int kmscon_text_register(const struct kmscon_text_ops *ops);
144 void kmscon_text_unregister(const char *name);
145
146 int kmscon_text_new(struct kmscon_text **out, const char *backend);
147 void kmscon_text_ref(struct kmscon_text *txt);
148 void kmscon_text_unref(struct kmscon_text *txt);
149
150 void kmscon_text_set_font(struct kmscon_text *txt,
151                           struct kmscon_font *font);
152 void kmscon_text_set_bgcolor(struct kmscon_text *txt,
153                              uint8_t r, uint8_t g, uint8_t b);
154 void kmscon_text_set_screen(struct kmscon_text *txt,
155                             struct uterm_screen *screen);
156 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
157 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
158
159 void kmscon_text_prepare(struct kmscon_text *txt);
160 void kmscon_text_draw(struct kmscon_text *txt, kmscon_symbol_t ch,
161                       unsigned int posx, unsigned int posy,
162                       const struct font_char_attr *attr);
163 void kmscon_text_render(struct kmscon_text *txt);
164
165 /* modularized backends */
166
167 int kmscon_font_8x16_load(void);
168 void kmscon_font_8x16_unload(void);
169 int kmscon_font_pango_load(void);
170 void kmscon_font_pango_unload(void);
171
172 int kmscon_text_bblit_load(void);
173 void kmscon_text_bblit_unload(void);
174
175 #endif /* KMSCON_TEXT_H */