GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_font.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2024 Cyril Hrubis <metan@ucw.cz>
4 */
5
12#ifndef TEXT_GP_FONT_H
13#define TEXT_GP_FONT_H
14
15#include <stddef.h>
16#include <stdint.h>
17
18#define GP_FONT_NAME_MAX 64
19
33typedef struct gp_glyph {
35 uint8_t width;
36
38 uint8_t height;
39
41 int8_t bearing_x;
42
44 int8_t bearing_y;
45
51 uint8_t advance_x;
52
59 uint8_t bitmap[];
61
73
99
100#define GP_FONT_STYLE(x) ((x) & GP_FONT_STYLE_MASK)
101
102typedef uint32_t gp_glyph_offset;
103#define GP_NOGLYPH UINT32_MAX
104
108typedef struct gp_glyphs {
114 void *glyphs;
115
122 gp_glyph_offset *offsets;
129 gp_glyph_offset offset;
130
136 uint32_t min_glyph;
142 uint32_t max_glyph;
144
145typedef struct gp_font_face gp_font_face;
146
153typedef struct gp_font_face_ops {
162 gp_glyph *(*glyph_load)(const gp_font_face *self, uint32_t ch);
168 void (*font_free)(gp_font_face *self);
170
178 char family_name[GP_FONT_NAME_MAX];
179
181 uint8_t style;
182
189
191 uint16_t ascent;
192
194 uint16_t descent;
195
203
208
213
218
223 void *priv;
224
232};
233
256
265typedef struct gp_font_family {
267 const char *family_name;
269 uint32_t ucode_blocks;
271 const gp_font_face *const fonts[];
273
285static inline unsigned int gp_font_height(const gp_font_face *font)
286{
287 return font->ascent + font->descent;
288}
289
301static inline unsigned int gp_font_ascent(const gp_font_face *font)
302{
303 return font->ascent;
304}
305
317static inline unsigned int gp_font_descent(const gp_font_face *font)
318{
319 return font->descent;
320}
321
331static inline unsigned int gp_font_max_width(const gp_font_face *font)
332{
333 return font->max_glyph_width;
334}
335
345static inline unsigned int gp_font_max_advance_x(const gp_font_face *font)
346{
347 return font->max_glyph_advance;
348}
349
359static inline unsigned int gp_font_avg_advance_x(const gp_font_face *font)
360{
361 if (font->avg_glyph_advance)
362 return font->avg_glyph_advance;
363
364 /* For monospace bitmap fonts the avg == max */
365 return font->max_glyph_advance;
366}
367
375static inline const char *gp_font_family_name(const gp_font_face *font)
376{
377 return font->family_name;
378}
379
391
400gp_glyph *gp_glyph_get(const gp_font_face *font, uint32_t ch);
401
414gp_font_face *gp_font_face_load(const char *path, uint32_t width, uint32_t height);
415
427gp_font_face *gp_font_face_fc_load(const char *name, uint32_t width, uint32_t height);
428
437
438#endif /* TEXT_GP_FONT_H */
static unsigned int gp_font_max_advance_x(const gp_font_face *font)
Returns maximal glyph advance.
Definition gp_font.h:345
gp_font_style
Font style bitflags.
Definition gp_font.h:80
@ GP_FONT_REGULAR
Regular font.
Definition gp_font.h:82
@ GP_FONT_FALLBACK
A fallback flag.
Definition gp_font.h:97
@ GP_FONT_ITALIC
Italic font.
Definition gp_font.h:88
@ GP_FONT_STYLE_MASK
Font style mask.
Definition gp_font.h:90
@ GP_FONT_BOLD
Bold font.
Definition gp_font.h:86
@ GP_FONT_MONO
Monospace font.
Definition gp_font.h:84
gp_glyph * gp_glyph_get(const gp_font_face *font, uint32_t ch)
Looks up a glyph in a font.
gp_font_bitmap_format
Glyph bitmap data format.
Definition gp_font.h:67
@ GP_FONT_BITMAP_8BPP
8 bit per pixel.
Definition gp_font.h:71
@ GP_FONT_BITMAP_1BPP
1 bit per pixel.
Definition gp_font.h:69
gp_font_face * gp_font_face_fc_load(const char *name, uint32_t width, uint32_t height)
Uses fontconfig to lookup font file.
static const char * gp_font_family_name(const gp_font_face *font)
Returns font family name.
Definition gp_font.h:375
gp_font_face * gp_font_face_load(const char *path, uint32_t width, uint32_t height)
Load a font face from a file.
static unsigned int gp_font_avg_advance_x(const gp_font_face *font)
Returns average glyph advance.
Definition gp_font.h:359
static unsigned int gp_font_descent(const gp_font_face *font)
Returns font ascent.
Definition gp_font.h:317
void gp_font_face_free(gp_font_face *self)
Frees the font face memory.
const char * gp_font_style_name(gp_font_style style)
Returns font style name.
static unsigned int gp_font_height(const gp_font_face *font)
Returns font height.
Definition gp_font.h:285
static unsigned int gp_font_max_width(const gp_font_face *font)
Returns maximal glyph width.
Definition gp_font.h:331
gp_font_ucode_block
Unicode block ids.
Definition gp_font.h:240
@ GP_UCODE_LATIN_SUP
Latin suplement 0xa0 - 0xff.
Definition gp_font.h:244
@ GP_UCODE_HIRAGANA
Hiragan 0x3041 - 0x3096.
Definition gp_font.h:252
@ GP_UCODE_GREEK
Greek 0x384 - 0x3ce.
Definition gp_font.h:248
@ GP_UCODE_KATAKANA
Katakana 0x30a0 - 0x30aff.
Definition gp_font.h:254
@ GP_UCODE_LATIN_BASIC
ASCII block 0x20 - 0x7f.
Definition gp_font.h:242
@ GP_UCODE_LATIN_EXT_A
Latin extended A 0x100 - 0x17e.
Definition gp_font.h:246
@ GP_UCODE_CYRILIC
Cyrilic 0x340 - 0x45f.
Definition gp_font.h:250
static unsigned int gp_font_ascent(const gp_font_face *font)
Returns font ascent.
Definition gp_font.h:301
Font loader callback.
Definition gp_font.h:153
void(* font_free)(gp_font_face *self)
Callback to free a font.
Definition gp_font.h:168
A font face.
Definition gp_font.h:176
uint16_t avg_glyph_advance
An average glyph advance.
Definition gp_font.h:212
uint16_t max_glyph_advance
Maximal glyph advance.
Definition gp_font.h:207
gp_glyphs glyphs[]
Glyph tables.
Definition gp_font.h:231
uint16_t ascent
Maximal height of font glyph from baseline to the top.
Definition gp_font.h:191
const gp_font_face_ops * ops
On demand loader used for non-ASCII unicode glyphs.
Definition gp_font.h:222
char family_name[64]
Font family name - eg. Sans, Serif ...
Definition gp_font.h:178
uint16_t max_glyph_width
Maximal width of font glyph.
Definition gp_font.h:202
uint8_t glyph_tables
A number of glyph tables in this font.
Definition gp_font.h:188
uint8_t style
A gp_font_style flags.
Definition gp_font.h:181
gp_font_bitmap_format glyph_bitmap_format
Bitmap format for all glyphs.
Definition gp_font.h:217
uint16_t descent
Maximal length of font glyph from baseline to the bottom.
Definition gp_font.h:194
A font family.
Definition gp_font.h:265
const char * family_name
A font family name.
Definition gp_font.h:267
const gp_font_face *const fonts[]
Definition gp_font.h:271
uint32_t ucode_blocks
A bitmask of unicode blocks included in the font faces.
Definition gp_font.h:269
A data describing single Glyph.
Definition gp_font.h:33
uint8_t bitmap[]
A character bitmap.
Definition gp_font.h:59
int8_t bearing_y
An Y offset from baseline to the top of the bitmap.
Definition gp_font.h:44
uint8_t height
Bitmap heigth in pixels.
Definition gp_font.h:38
uint8_t width
Bitmap width in pixels.
Definition gp_font.h:35
int8_t bearing_x
An X offset to be applied before we start drawing.
Definition gp_font.h:41
uint8_t advance_x
An offset to be applied after drawing.
Definition gp_font.h:51
An unicode glyphs block.
Definition gp_font.h:108
gp_glyph_offset offset
An offset to the glyph data.
Definition gp_font.h:129
uint32_t max_glyph
Last character in glyphs table.
Definition gp_font.h:142
uint32_t min_glyph
First character in glyphs table.
Definition gp_font.h:136
gp_glyph_offset * offsets
Offsets to the glyph data.
Definition gp_font.h:122
void * glyphs
Pointer to glyph bitmap buffer.
Definition gp_font.h:114