GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_font.h
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#ifndef TEXT_GP_FONT_H
7#define TEXT_GP_FONT_H
8
9#include <stddef.h>
10#include <stdint.h>
11
12#define GP_FONT_NAME_MAX 64
13
14/*
15 * Data describing single Glyph.
16 *
17 * Note that glyph do not necessarily correspond to one character (for example
18 * ligature is a glyph but corresponds to at least two characters).
19 *
20 * The glyphs are rendered to horizontal baseline, vertical rendering is not
21 * supported.
22 *
23 * The structure could contain glyphs of different BPP and information about
24 * the bitmap format is stored in the font structure. The bitmap lines are byte
25 * aligned.
26 */
27typedef struct gp_glyph {
28 /*
29 * Bitmap width in pixels.
30 */
31 uint8_t width;
32
33 /*
34 * Bitmap heigth in pixels.
35 */
36 uint8_t height;
37
38 /*
39 * X offset to be applied before we start drawing.
40 */
41 int8_t bearing_x;
42
43 /*
44 * Y offset from baseline to the top of the bitmap.
45 */
46 int8_t bearing_y;
47
48 /*
49 * Offset to be applied after drawing, defines
50 * basepoint for next glyph.
51 */
52 uint8_t advance_x;
53
54 /*
55 * Character bitmap, byte aligned bitmap.
56 */
57 uint8_t bitmap[];
58} gp_glyph;
59
60/*
61 * Glyph bitmap data format.
62 *
63 * The bitmap is byte aligned and for 1BPP the number of bytes per row is
64 * rounted to bytes.
65 *
66 */
67typedef enum gp_font_bitmap_format {
68 GP_FONT_BITMAP_1BPP,
69 GP_FONT_BITMAP_8BPP,
70} gp_font_bitmap_format;
71
72/*
73 * Font style bitflags.
74 */
75typedef enum gp_font_style {
76 GP_FONT_REGULAR = 0x00,
77 GP_FONT_MONO = 0x01,
78 GP_FONT_BOLD = 0x02,
79 GP_FONT_ITALIC = 0x04,
80 GP_FONT_STYLE_MASK = 0x0f,
81 /*
82 * If passed make sure we return something
83 * possibly the default compiled-in font
84 */
85 GP_FONT_FALLBACK = 0x10,
86} gp_font_style;
87
88#define GP_FONT_STYLE(x) ((x) & GP_FONT_STYLE_MASK)
89
90typedef uint32_t gp_glyph_offset;
91#define GP_NOGLYPH UINT32_MAX
92
93typedef struct gp_glyphs {
94 /* Pointer to glyph bitmap buffer */
95 void *glyphs;
96
97 /*
98 * Offsets to the glyph data.
99 *
100 * If offsets is NULL offset is the same for all glyphs and saved in
101 * offset instead.
102 */
103 gp_glyph_offset *offsets;
104 gp_glyph_offset offset;
105
106 /*
107 * First and last character in glyphs table
108 *
109 * For table 0 which is ASCII this is set to 0x20 and 0x7f
110 */
111 uint32_t min_glyph;
112 uint32_t max_glyph;
113} gp_glyphs;
114
115typedef struct gp_font_face gp_font_face;
116
120typedef struct gp_font_face_ops {
121 gp_glyph *(*glyph_load)(const gp_font_face *self, uint32_t ch);
122 void (*font_free)(gp_font_face *self);
124
125struct gp_font_face {
126 /* Font family name - eg. Sans, Serif ... */
127 char family_name[GP_FONT_NAME_MAX];
128
129 /* Font style flags */
130 uint8_t style;
131
132 /* Size of the glyphs array */
133 uint8_t glyph_tables;
134
135 /* Maximal height of font glyph from baseline to the top. */
136 uint16_t ascend;
137
138 /* Maximal length of font glyph from baseline to the bottom. */
139 uint16_t descend;
140
141 /*
142 * Maximal width of font glyph.
143 *
144 * (basically max from glyph->width + glyph->bearing_x)
145 */
146 uint16_t max_glyph_width;
147
148 /*
149 * Maximal glyph advance.
150 */
151 uint16_t max_glyph_advance;
152
153 /*
154 * Average glyph advance.
155 */
156 uint16_t avg_glyph_advance;
157
158 /*
159 * Bitmap format for all glyphs
160 */
161 gp_font_bitmap_format glyph_bitmap_format;
162
163 /*
164 * "Lazy" loader used for non-ascii unicode glyphs
165 */
166 const gp_font_face_ops *ops;
167 void *priv;
168
169 /* Glyphs tables
170 *
171 * NULL terminated array of glyph tables sorted by the max_glyph, i.e.
172 * the ASCII which ends at 0x7f should be first.
173 */
174 gp_glyphs glyphs[];
175};
176
177/*
178 * All fonts must include LATIN_BASIC and it has to be the first table
179 * in the font.
180 */
181typedef enum gp_font_ucode_block {
182 /* 0x20 - 0x7f */
183 GP_UCODE_LATIN_BASIC = 0x0001,
184 /* 0xa0 - 0xff */
185 GP_UCODE_LATIN_SUP = 0x0002,
186 /* 0x100 - 0x17e */
187 GP_UCODE_LATIN_EXT_A = 0x0004,
188 /* 0x384 - 0x3ce */
189 GP_UCODE_GREEK = 0x0008,
190 /* 0x340 - 0x45f */
191 GP_UCODE_CYRILIC = 0x0010,
192 /* 0x3041 - 0x3096 */
193 GP_UCODE_HIRAGANA= 0x0020,
194 /* 0x30a0 - 0x30aff */
195 GP_UCODE_KATAKANA = 0x0030,
196} gp_font_ucode_block;
197
198/*
199 * Font family is a group of fonts of the same family and size but different
200 * style i.e. monospace, bold, italic...
201 *
202 * The fonts array is NULL terminated.
203 */
204typedef struct gp_font_family {
205 const char *family_name;
206 /* Bitmask of unicode blocks included in the font */
207 uint32_t ucode_blocks;
208 const gp_font_face *const fonts[];
209} gp_font_family;
210
211/*
212 * Returns font height eg. ascend + descend
213 */
214static inline unsigned int gp_font_height(const gp_font_face *font)
215{
216 return font->ascend + font->descend;
217}
218
219static inline unsigned int gp_font_ascend(const gp_font_face *font)
220{
221 return font->ascend;
222}
223
224static inline unsigned int gp_font_descend(const gp_font_face *font)
225{
226 return font->descend;
227}
228
229static inline unsigned int gp_font_max_width(const gp_font_face *font)
230{
231 return font->max_glyph_width;
232}
233
234static inline unsigned int gp_font_max_advance_x(const gp_font_face *font)
235{
236 return font->max_glyph_advance;
237}
238
239static inline unsigned int gp_font_avg_advance_x(const gp_font_face *font)
240{
241 if (font->avg_glyph_advance)
242 return font->avg_glyph_advance;
243
244 /* For monospace bitmap fonts the avg == max */
245 return font->max_glyph_advance;
246}
247
248static inline const char *gp_font_family_name(const gp_font_face *font)
249{
250 return font->family_name;
251}
252
253const char *gp_font_style_name(uint8_t style);
254
255/*
256 * Returns glyph mapping
257 */
258gp_glyph *gp_get_glyph(const gp_font_face *font, uint32_t ch);
259
260/* Loads font face from file */
261gp_font_face *gp_font_face_load(const char *path, uint32_t width, uint32_t height);
262
263/* Uses fontconfig to lookup font file */
264gp_font_face *gp_font_face_fc_load(const char *name, uint32_t width, uint32_t height);
265
266/* Free the font face memory */
267void gp_font_face_free(gp_font_face *self);
268
269#endif /* TEXT_GP_FONT_H */