console/font: draw bg only if needed
[platform/upstream/kmscon.git] / src / font.h
1 /*
2  * kmscon - Font Management
3  *
4  * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5  * Copyright (c) 2011 University of Tuebingen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /*
28  * Font Management
29  * The output of a console is a fixed-size table. That is, it consists of many
30  * cells where each character is printed either into a single cell or spread
31  * across multiple cells. However, there will never be multiple characters in a
32  * single cell so cell indexes are the smallest position information.
33  * The classic console uses one character per cell. Newer consoles may allow
34  * widened characters, though. Common are characters that are double-width and
35  * characters that are double-width+double-height.
36  * If you mix many different widths/heights then this might get very
37  * memory-consuming as we need to have one loaded font for each size to get
38  * decent results. Therefore, avoid widths/heights other than the ones
39  * mentioned.
40  *
41  * Therefore, this layer does not provide the classic font APIs, instead it
42  * offers a font_screen object which represents the whole screen. You specify
43  * the x/y coordinates of your framebuffer/target and the font plus point-size
44  * that you want to use. This layer automatically computes the pixel size and
45  * resulting row/column counts.
46  * For reversed logic you can also specify row/column counts and the API
47  * calculates the required font-point-size.
48  * In both situations you never have to deal with font related details! The only
49  * thing you need to know is the row/column count of the resulting table and
50  * all the characters in the table.
51  *
52  * When drawing a screen you need to tell the font layer where to draw the
53  * characters. For performance reasons this is split into several tasks:
54  *   1: Start a drawing operation. This resets the screen and prepares the font
55  *      for drawing. It clears all previous entries.
56  *   2: Add each character you want to draw to the font_screen object with its
57  *      cell position and cell width. The width is probably always 1/1 but for
58  *      multi-cell characters you can specify other widths/heights.
59  *   3: Perform the drawing operation. This instructs the font-layer to actually
60  *      draw all the added characters to your surface.
61  * You need to perform all 3 steps for every frame you render.
62  */
63
64 #ifndef FONT_FONT_H
65 #define FONT_FONT_H
66
67 #include <stdlib.h>
68 #include "gl.h"
69 #include "unicode.h"
70 #include "uterm.h"
71
72 struct kmscon_font_factory;
73 struct kmscon_font;
74
75 int kmscon_font_factory_new(struct kmscon_font_factory **out);
76 void kmscon_font_factory_ref(struct kmscon_font_factory *ff);
77 void kmscon_font_factory_unref(struct kmscon_font_factory *ff);
78
79 int kmscon_font_factory_load(struct kmscon_font_factory *ff,
80         struct kmscon_font **out, unsigned int width, unsigned int height);
81
82 void kmscon_font_ref(struct kmscon_font *font);
83 void kmscon_font_unref(struct kmscon_font *font);
84
85 unsigned int kmscon_font_get_height(struct kmscon_font *font);
86 unsigned int kmscon_font_get_width(struct kmscon_font *font);
87 int kmscon_font_draw(struct kmscon_font *font, kmscon_symbol_t ch, float *m,
88                         struct gl_shader *shader);
89
90 /* font attributes */
91
92 enum font_style {
93         FONT_NORMAL,
94         FONT_ITALIC,
95 };
96
97 struct font_attr {
98         const char *name;       /* use NULL for default */
99         unsigned int points;
100         unsigned int dpi;       /* use 0 for default */
101         bool bold;
102         enum font_style style;
103 };
104
105 #define FONT_ATTR(_name, _points, _dpi) &(const struct font_attr){ \
106                 .name = (_name), \
107                 .points = (_points), \
108                 .dpi = (_dpi), \
109                 .bold = false, \
110                 .style = FONT_NORMAL, \
111         }
112
113 struct font_char_attr {
114         uint8_t fr;                     /* foreground red */
115         uint8_t fg;                     /* foreground green */
116         uint8_t fb;                     /* foreground blue */
117         uint8_t br;                     /* background red */
118         uint8_t bg;                     /* background green */
119         uint8_t bb;                     /* background blue */
120         unsigned int bold : 1;          /* bold character */
121         unsigned int underline : 1;     /* underlined character */
122         unsigned int inverse : 1;       /* inverse colors */
123         unsigned int protect : 1;       /* cannot be erased */
124 };
125
126 /* font draw/assemble buffers */
127
128 struct font_buffer {
129         unsigned int width;
130         unsigned int stride;
131         unsigned int height;
132         char *data;
133 };
134
135 int font_buffer_new(struct font_buffer **out, unsigned int width,
136                         unsigned int height);
137 void font_buffer_free(struct font_buffer *buf);
138
139 /* font screens */
140
141 struct font_screen;
142
143 int font_screen_new(struct font_screen **out, struct font_buffer *buf,
144                         const struct font_attr *attr,
145                         struct uterm_screen *scr, struct gl_shader *shader);
146 int font_screen_new_fixed(struct font_screen **out, struct font_buffer *buf,
147                         const struct font_attr *attr,
148                         unsigned int cols, unsigned int rows,
149                         struct uterm_screen *scr, struct gl_shader *shader);
150 void font_screen_free(struct font_screen *screen);
151
152 unsigned int font_screen_columns(struct font_screen *screen);
153 unsigned int font_screen_rows(struct font_screen *screen);
154 unsigned int font_screen_points(struct font_screen *screen);
155 unsigned int font_screen_width(struct font_screen *screen);
156 unsigned int font_screen_height(struct font_screen *screen);
157
158 int font_screen_draw_start(struct font_screen *screen);
159 int font_screen_draw_char(struct font_screen *screen, kmscon_symbol_t ch,
160                                 const struct font_char_attr *attr,
161                                 unsigned int cellx, unsigned int celly,
162                                 unsigned int width, unsigned int height,
163                                 bool draw_bg);
164 int font_screen_draw_perform(struct font_screen *screen, float *m);
165
166 #endif /* FONT_FONT_H */