vte/font/console: support basic character attributes
[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
71 struct kmscon_font_factory;
72 struct kmscon_font;
73
74 int kmscon_font_factory_new(struct kmscon_font_factory **out);
75 void kmscon_font_factory_ref(struct kmscon_font_factory *ff);
76 void kmscon_font_factory_unref(struct kmscon_font_factory *ff);
77
78 int kmscon_font_factory_load(struct kmscon_font_factory *ff,
79         struct kmscon_font **out, unsigned int width, unsigned int height);
80
81 void kmscon_font_ref(struct kmscon_font *font);
82 void kmscon_font_unref(struct kmscon_font *font);
83
84 unsigned int kmscon_font_get_height(struct kmscon_font *font);
85 unsigned int kmscon_font_get_width(struct kmscon_font *font);
86 int kmscon_font_draw(struct kmscon_font *font, kmscon_symbol_t ch, float *m,
87                         struct gl_shader *shader);
88
89 /* font attributes */
90
91 enum font_style {
92         FONT_NORMAL,
93         FONT_ITALIC,
94 };
95
96 struct font_attr {
97         const char *name;       /* use NULL for default */
98         unsigned int points;
99         unsigned int dpi;       /* use 0 for default */
100         bool bold;
101         enum font_style style;
102 };
103
104 #define FONT_ATTR(_name, _points, _dpi) &(const struct font_attr){ \
105                 .name = (_name), \
106                 .points = (_points), \
107                 .dpi = (_dpi), \
108                 .bold = false, \
109                 .style = FONT_NORMAL, \
110         }
111
112 struct font_char_attr {
113         uint8_t fr;                     /* foreground red */
114         uint8_t fg;                     /* foreground green */
115         uint8_t fb;                     /* foreground blue */
116         uint8_t br;                     /* background red */
117         uint8_t bg;                     /* background green */
118         uint8_t bb;                     /* background blue */
119         unsigned int bold : 1;          /* bold character */
120         unsigned int underline : 1;     /* underlined character */
121         unsigned int inverse : 1;       /* inverse colors */
122 };
123
124 /* font draw/assemble buffers */
125
126 struct font_buffer {
127         unsigned int width;
128         unsigned int stride;
129         unsigned int height;
130         char *data;
131 };
132
133 int font_buffer_new(struct font_buffer **out, unsigned int width,
134                         unsigned int height);
135 void font_buffer_free(struct font_buffer *buf);
136
137 /* font screens */
138
139 struct font_screen;
140
141 int font_screen_new(struct font_screen **out, struct font_buffer *buf,
142                         const struct font_attr *attr,
143                         struct gl_shader *shader);
144 int font_screen_new_fixed(struct font_screen **out, struct font_buffer *buf,
145                         const struct font_attr *attr,
146                         unsigned int cols, unsigned int rows,
147                         struct gl_shader *shader);
148 void font_screen_free(struct font_screen *screen);
149
150 unsigned int font_screen_columns(struct font_screen *screen);
151 unsigned int font_screen_rows(struct font_screen *screen);
152 unsigned int font_screen_points(struct font_screen *screen);
153 unsigned int font_screen_width(struct font_screen *screen);
154 unsigned int font_screen_height(struct font_screen *screen);
155
156 int font_screen_draw_start(struct font_screen *screen);
157 int font_screen_draw_char(struct font_screen *screen, kmscon_symbol_t ch,
158                                 const struct font_char_attr *attr,
159                                 unsigned int cellx, unsigned int celly,
160                                 unsigned int width, unsigned int height);
161 int font_screen_draw_perform(struct font_screen *screen, float *m);
162
163 #endif /* FONT_FONT_H */