gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
13#ifndef GPFORGE_FONT_H
14#define GPFORGE_FONT_H
15
16#include <stdint.h>
17#include <stddef.h>
18
19#include <utils/gp_vec.h>
20
22#define GPF_NAME_MAX 64
24#define GPF_YEARS_MAX 32
25
27#define GPF_MIN(a, b) ((a) < (b) ? (a) : (b))
29#define GPF_MAX(a, b) ((a) > (b) ? (a) : (b))
30
48
76
80struct gpf_glyph {
84 uint32_t code;
88 uint8_t kind;
89
97 uint8_t has_advance;
99 uint8_t has_bearing;
101 uint8_t has_shift;
102
108 uint8_t modified;
109
129
133 unsigned int width;
137 unsigned int height;
145 uint8_t *bits;
146
150 uint32_t base;
154 uint32_t accent;
156 int dx, dy;
157
164 int shift_x, shift_y;
165};
166
197
208 uint32_t min;
210 uint32_t max;
213};
214
226
270
274struct gpf_font {
277
283 char *comments;
284
292
299
302};
303
310const char *gpf_variant_name(enum gpf_variant_id id);
311
319int gpf_variant_by_name(const char *name, enum gpf_variant_id *id);
320
330
339struct gpf_font *gpf_font_new(void);
340
346void gpf_font_free(struct gpf_font *self);
347
361void gpf_family_meta_guess(struct gpf_family_meta *meta, const char *family,
362 int size, int ascent, int descent, int advance);
363
374struct gpf_font *gpf_font_create(const struct gpf_family_meta *meta,
375 int advance);
376
387int gpf_family_meta_same(const struct gpf_family_meta *a,
388 const struct gpf_family_meta *b);
389
402struct gpf_variant *gpf_font_variant(struct gpf_font *self,
403 enum gpf_variant_id id, int create);
404
411static inline size_t gpf_variant_glyphs(const struct gpf_variant *self)
412{
413 return gp_vec_len(self->glyphs);
414}
415
423uint32_t *gpf_font_codes(struct gpf_font *self);
424
432void gpf_font_clear_modified(struct gpf_font *self);
433
445void gpf_font_ink_extent(struct gpf_font *self, int *top, int *bottom);
446
458int gpf_font_block_add(struct gpf_font *self, uint32_t min, uint32_t max,
459 const char *name);
460
470void gpf_font_block_del(struct gpf_font *self, uint32_t min);
471
480unsigned int gpf_font_block_clear(struct gpf_font *self, uint32_t min,
481 uint32_t max);
482
491unsigned int gpf_font_range_glyphs(struct gpf_font *self, uint32_t min,
492 uint32_t max);
493
501int gpf_font_block_declared(struct gpf_font *self, uint32_t min);
502
515const char *gpf_author_invalid(const char *years, const char *name,
516 const char *email);
517
528int gpf_authors_add(struct gpf_author **authors, const char *years,
529 const char *name, const char *email);
530
537void gpf_authors_del(struct gpf_author **authors, size_t idx);
538
545struct gpf_author *gpf_authors_dup(const struct gpf_author *authors);
546
554int gpf_authors_same(const struct gpf_author *a, const struct gpf_author *b);
555
556
565struct gpf_glyph *gpf_variant_glyph(struct gpf_variant *self, uint32_t code);
566
578struct gpf_glyph *gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code);
579
588void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code);
589
595void gpf_glyph_clear(struct gpf_glyph *self);
596
611int gpf_glyph_set_bits(struct gpf_glyph *self, const uint8_t *bits,
612 unsigned int w, unsigned int h,
613 int bearing_x, int bearing_y);
614
623static inline int gpf_glyph_pixel(const struct gpf_glyph *self,
624 unsigned int x, unsigned int y)
625{
626 if (x >= self->width || y >= self->height)
627 return 0;
628
629 return self->bits[y * self->width + x];
630}
631
632#endif /* GPFORGE_FONT_H */
const char * gpf_variant_name(enum gpf_variant_id id)
Returns the name of a variant.
Definition font.c:23
static int gpf_glyph_pixel(const struct gpf_glyph *self, unsigned int x, unsigned int y)
Returns a glyph ink pixel.
Definition font.h:623
struct gpf_author * gpf_authors_dup(const struct gpf_author *authors)
Copies a gp_vec of authors.
Definition font.c:458
void gpf_glyph_clear(struct gpf_glyph *self)
Frees the ink and zeroes the glyph, keeping only the code.
Definition font.c:576
void gpf_family_meta_guess(struct gpf_family_meta *meta, const char *family, int size, int ascent, int descent, int advance)
Fills in metadata for a font that is only being started.
Definition font.c:72
int gpf_family_meta_same(const struct gpf_family_meta *a, const struct gpf_family_meta *b)
Compares two sets of font metadata.
Definition font.c:122
int gpf_variant_by_name(const char *name, enum gpf_variant_id *id)
Looks up a variant by its on-disk name.
Definition font.c:31
struct gpf_glyph * gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code)
Adds a glyph to a variant.
Definition font.c:242
void gpf_font_free(struct gpf_font *self)
Frees a font with all its variants and glyphs.
Definition font.c:159
enum gpf_variant_id gpf_variant_parent(enum gpf_variant_id id)
Returns the variant a variant derives its missing glyphs from.
Definition font.c:45
const char * gpf_author_invalid(const char *years, const char *name, const char *email)
Checks the fields of an author.
Definition font.c:397
unsigned int gpf_font_block_clear(struct gpf_font *self, uint32_t min, uint32_t max)
Deletes every glyph in a range, in all variants.
Definition font.c:550
unsigned int gpf_font_range_glyphs(struct gpf_font *self, uint32_t min, uint32_t max)
Counts glyph entries in a range, in all variants.
Definition font.c:527
#define GPF_YEARS_MAX
Size of the author years buffer, including the terminating zero.
Definition font.h:24
#define GPF_NAME_MAX
Size of the name buffers, including the terminating zero.
Definition font.h:22
void gpf_font_clear_modified(struct gpf_font *self)
Clears the modified flag of all glyphs in all variants.
Definition font.c:269
void gpf_authors_del(struct gpf_author **authors, size_t idx)
Removes an author from a gp_vec of authors.
Definition font.c:450
int gpf_font_block_declared(struct gpf_font *self, uint32_t min)
Checks whether a block is declared.
Definition font.c:352
void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code)
Removes a glyph from a variant.
Definition font.c:489
int gpf_authors_same(const struct gpf_author *a, const struct gpf_author *b)
Compares two gp_vecs of authors, NULL being the same as empty.
Definition font.c:472
struct gpf_variant * gpf_font_variant(struct gpf_font *self, enum gpf_variant_id id, int create)
Returns a font variant.
Definition font.c:175
static size_t gpf_variant_glyphs(const struct gpf_variant *self)
Returns the number of glyph entries in a variant.
Definition font.h:411
gpf_variant_id
Font face variant.
Definition font.h:34
@ GPF_BOLD_MONO
Bold monospace glyphs.
Definition font.h:40
@ GPF_NO_PARENT
What gpf_variant_parent() returns for the base variant.
Definition font.h:46
@ GPF_VARIANTS
Number of font variants.
Definition font.h:44
@ GPF_MONO
Monospace glyphs, the base variant.
Definition font.h:36
@ GPF_BOLD
Bold glyphs.
Definition font.h:42
@ GPF_REGULAR
Proportional glyphs, "regular" on disk.
Definition font.h:38
struct gpf_font * gpf_font_new(void)
Allocates an empty font.
Definition font.c:58
struct gpf_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
Definition font.c:231
uint32_t * gpf_font_codes(struct gpf_font *self)
Returns every codepoint the font mentions, in any variant.
Definition font.c:317
int gpf_font_block_add(struct gpf_font *self, uint32_t min, uint32_t max, const char *name)
Declares a unicode block.
Definition font.c:364
gpf_glyph_kind
A glyph entry type.
Definition font.h:55
@ GPF_GLYPH_INK
Glyph with ink.
Definition font.h:62
@ GPF_GLYPH_COMPOSE
Glyph is composed from base + accent.
Definition font.h:74
@ GPF_GLYPH_METRICS
Glyph with metrics.
Definition font.h:68
void gpf_font_ink_extent(struct gpf_font *self, int *top, int *bottom)
Returns how far the ink of the whole font reaches.
Definition font.c:285
int gpf_authors_add(struct gpf_author **authors, const char *years, const char *name, const char *email)
Appends an author to a gp_vec of authors.
Definition font.c:426
int gpf_glyph_set_bits(struct gpf_glyph *self, const uint8_t *bits, unsigned int w, unsigned int h, int bearing_x, int bearing_y)
Stores the glyph ink.
Definition font.c:585
struct gpf_font * gpf_font_create(const struct gpf_family_meta *meta, int advance)
Creates a new font.
Definition font.c:100
void gpf_font_block_del(struct gpf_font *self, uint32_t min)
Removes a block declaration.
Definition font.c:563
A font author, one author line of the family file.
Definition font.h:218
char years[32]
Copyright years, e.g. "2024", "2019-2024" or "2019,2023".
Definition font.h:220
char email[64]
The author's email, without the angle brackets.
Definition font.h:224
char name[64]
The author's name.
Definition font.h:222
A declared unicode block.
Definition font.h:206
char name[64]
The block name.
Definition font.h:212
uint32_t max
The last codepoint of the block, inclusive.
Definition font.h:210
uint32_t min
The first codepoint of the block.
Definition font.h:208
Font family meta data.
Definition font.h:230
int descent
Font descent.
Definition font.h:241
int line_gap
Gap between two lines of text.
Definition font.h:243
int overline_thickness
Overline thickness.
Definition font.h:265
int size
Font size.
Definition font.h:237
int ch_width
The advance of the digit zero.
Definition font.h:252
int overline_pos
Overline position.
Definition font.h:263
uint32_t default_glyph
The unicode code point of the default glyph.
Definition font.h:268
char license[64]
SPDX license name.
Definition font.h:234
int cap_height
Capital letter height.
Definition font.h:250
int x_height
The x-height.
Definition font.h:248
int strike_thickness
Strikethrough thickness.
Definition font.h:261
char family[64]
Font family name.
Definition font.h:232
int em
Em size.
Definition font.h:246
int ascent
Font ascent.
Definition font.h:239
int strike_pos
Strikethrough position.
Definition font.h:259
int underline_thickness
Underline thickness.
Definition font.h:257
int underline_pos
Underline position.
Definition font.h:255
A font family.
Definition font.h:274
struct gpf_author * authors
Font authors.
Definition font.h:298
struct gpf_variant * variants[GPF_VARIANTS]
Variants indexed by gpf_variant_id, NULL when missing.
Definition font.h:301
struct gpf_family_meta meta
Font meta data.
Definition font.h:276
struct gpf_block_decl * blocks
Declared unicode blocks.
Definition font.h:291
char * comments
Font comments.
Definition font.h:283
A glyph.
Definition font.h:80
uint8_t kind
enum gpf_glyph_kind
Definition font.h:88
uint8_t * bits
Glyph ink.
Definition font.h:145
int bearing_y
Glyph bearing y.
Definition font.h:128
uint8_t has_advance
The entry carries an advance.
Definition font.h:97
int shift_x
How far the inherited ink is moved, positive right and up.
Definition font.h:164
int dx
Accent offset, positive right and up, compose only.
Definition font.h:156
uint8_t has_shift
The entry moves the inherited ink, GPF_GLYPH_METRICS only.
Definition font.h:101
uint32_t base
Base glyph, GPF_GLYPH_COMPOSE only.
Definition font.h:150
uint32_t accent
Accent to be combined with the base, GPF_GLYPH_COMPOSE only.
Definition font.h:154
uint8_t has_bearing
The entry carries both bearings, see has_advance.
Definition font.h:99
unsigned int height
Glyph ink height.
Definition font.h:137
unsigned int width
Glyph ink width.
Definition font.h:133
int advance
Glyph advance.
Definition font.h:116
uint8_t modified
Glyph was modified flag.
Definition font.h:108
int bearing_x
Glyph bearing x.
Definition font.h:122
uint32_t code
Unicode code point.
Definition font.h:84
A font variant.
Definition font.h:170
int advance
Advance for all glyphs.
Definition font.h:182
int derive_embolden
Glyphs not drawn here are emboldened from the parent.
Definition font.h:176
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.
Definition font.h:172
int spacing_mono
Every glyph has the same advance.
Definition font.h:174
struct gpf_glyph * glyphs
Glyphs.
Definition font.h:195
char * comments
Comments above the first glyph block.
Definition font.h:188