gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
face.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <stdlib.h>
7#include <string.h>
8
9#include <utils/gp_vec.h>
10
11#include "resolve.h"
12#include "face.h"
13
14/*
15 * The ASCII table is indexed directly by gfxprim, so it always spans the whole
16 * 0x20 - 0x7f range. Everything above that goes into a second table.
17 */
18#define ASCII_MIN 0x20
19#define ASCII_MAX 0x7f
20
21/*
22 * A glyph is five bytes of metrics followed by the bitmap, rows byte aligned,
23 * the same layout as struct gp_glyph.
24 */
25static int emit_glyph(uint8_t **data, const struct gpf_glyph *glyph,
26 gp_glyph_offset *off)
27{
28 unsigned int row_bytes = (glyph->width + 7) / 8;
29 size_t need = 5 + (size_t)row_bytes * glyph->height;
30 size_t pos = gp_vec_len(*data);
31 unsigned int x, y;
32 uint8_t *dst;
33
34 /* the compiled in fonts align glyphs on four bytes, do the same */
35 need = (need + 3) & ~(size_t)3;
36
37 dst = gp_vec_expand(*data, need);
38 if (!dst)
39 return 1;
40
41 *data = dst;
42 dst += pos;
43
44 memset(dst, 0, need);
45
46 dst[0] = glyph->width;
47 dst[1] = glyph->height;
48 dst[2] = glyph->bearing_x;
49 dst[3] = glyph->bearing_y;
50 dst[4] = glyph->advance;
51
52 for (y = 0; y < glyph->height; y++) {
53 for (x = 0; x < glyph->width; x++) {
54 if (gpf_glyph_pixel(glyph, x, y))
55 dst[5 + y * row_bytes + x/8] |= 0x80 >> (x%8);
56 }
57 }
58
59 *off = pos;
60
61 return 0;
62}
63
64static int build_table(struct gpf_font *font, enum gpf_variant_id id,
65 gp_glyphs *table, uint32_t min, uint32_t max,
66 uint8_t **data, gp_font_face *face,
67 gp_glyph_offset blank)
68{
69 uint32_t code;
70 size_t cnt = max - min + 1;
71
72 table->offsets = malloc(cnt * sizeof(gp_glyph_offset));
73 if (!table->offsets)
74 return 1;
75
76 table->min_glyph = min;
77 table->max_glyph = max;
78
79 for (code = min; code <= max; code++) {
80 struct gpf_resolved res;
81 int width;
82
83 /*
84 * A missing glyph points at the blank rather than at
85 * GP_NOGLYPH: gp_glyph_get() falls back to '?' and returns
86 * NULL when that is missing too, and gfxprim's text metric
87 * dereferences the result without checking. A font being
88 * built has no ASCII to fall back on.
89 */
90 table->offsets[code - min] = blank;
91
92 if (gpf_resolve(font, id, code, &res))
93 continue;
94
95 if (emit_glyph(data, &res.glyph, &table->offsets[code - min])) {
97 return 1;
98 }
99
100 width = res.glyph.bearing_x + (int)res.glyph.width;
101
102 if (width > face->max_glyph_width)
103 face->max_glyph_width = width;
104
105 if (res.glyph.advance > face->max_glyph_advance)
106 face->max_glyph_advance = res.glyph.advance;
107
108 gpf_resolved_clear(&res);
109 }
110
111 return 0;
112}
113
114gp_font_face *gpf_face_build(struct gpf_font *font, enum gpf_variant_id id)
115{
116 struct gpf_family_meta *m = &font->meta;
117 gp_font_face *face;
118 uint8_t *data;
119 uint32_t *codes;
120 uint32_t high_max = 0;
121 unsigned int tables = 1, i;
122 size_t j;
123 struct gpf_glyph blank = {};
124 gp_glyph_offset blank_off;
125
126 codes = gpf_font_codes(font);
127 if (!codes)
128 return NULL;
129
130 for (j = 0; j < gp_vec_len(codes); j++) {
131 if (codes[j] > ASCII_MAX)
132 high_max = codes[j];
133 }
134
135 gp_vec_free(codes);
136
137 if (high_max)
138 tables = 2;
139
140 face = calloc(1, sizeof(gp_font_face) + tables * sizeof(gp_glyphs));
141 if (!face)
142 return NULL;
143
144 data = gp_vec_new(0, 1);
145 if (!data) {
146 free(face);
147 return NULL;
148 }
149
150 snprintf(face->family_name, sizeof(face->family_name), "%s", m->family);
151
152 face->style = (id == GPF_MONO || id == GPF_BOLD_MONO) ? GP_FONT_MONO : GP_FONT_REGULAR;
153
154 if (id == GPF_BOLD || id == GPF_BOLD_MONO)
155 face->style |= GP_FONT_BOLD;
156
157 face->glyph_tables = tables;
158 face->ascent = m->ascent;
159 face->descent = m->descent;
160 face->glyph_bitmap_format = GP_FONT_BITMAP_1BPP;
161
162 /*
163 * The typographic metrics, which is what the format carries them for:
164 * gp_font_face has had them since the em, x-height and cap height
165 * landed upstream. The em is the size the font is meant to be read as
166 * — what a layout gets for 1em — so it is the family file's em and not
167 * the pixel size.
168 */
169 face->em = m->em;
170 face->x_height = m->x_height;
171 face->cap_height = m->cap_height;
172 face->ch_width = m->ch_width;
173 face->line_gap = m->line_gap;
174 face->underline_pos = m->underline_pos;
175 face->underline_thickness = m->underline_thickness;
176 face->strike_pos = m->strike_pos;
177 face->strike_thickness = m->strike_thickness;
178 face->overline_pos = m->overline_pos;
179 face->overline_thickness = m->overline_thickness;
180
181 /* one blank glyph, shared by every codepoint the font has no glyph for */
182 if (emit_glyph(&data, &blank, &blank_off))
183 goto err;
184
185 if (build_table(font, id, &face->glyphs[0], ASCII_MIN, ASCII_MAX,
186 &data, face, blank_off))
187 goto err;
188
189 if (tables > 1) {
190 if (build_table(font, id, &face->glyphs[1], ASCII_MAX + 1,
191 high_max, &data, face, blank_off))
192 goto err;
193 }
194
195 /*
196 * The glyph data may have been reallocated while it was filled in, so
197 * the table pointers are set once it is complete.
198 */
199 for (i = 0; i < tables; i++)
200 face->glyphs[i].glyphs = data;
201
202 face->priv = data;
203
204 return face;
205err:
206 for (i = 0; i < tables; i++)
207 free(face->glyphs[i].offsets);
208
209 gp_vec_free(data);
210 free(face);
211
212 return NULL;
213}
214
215void gpf_face_free(gp_font_face *self)
216{
217 unsigned int i;
218
219 if (!self)
220 return;
221
222 for (i = 0; i < self->glyph_tables; i++)
223 free(self->glyphs[i].offsets);
224
225 gp_vec_free(self->priv);
226 free(self);
227}
Compiles a gfxprim font face from an in-memory gpf font.
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
gpf_variant_id
Font face variant.
Definition font.h:34
@ GPF_BOLD_MONO
Bold monospace glyphs.
Definition font.h:40
@ GPF_MONO
Monospace glyphs, the base variant.
Definition font.h:36
@ GPF_BOLD
Bold glyphs.
Definition font.h:42
uint32_t * gpf_font_codes(struct gpf_font *self)
Returns every codepoint the font mentions, in any variant.
Definition font.c:317
The glyph variant lattice.
void gpf_resolved_clear(struct gpf_resolved *self)
Clears the resolved glyph.
Definition resolve.c:30
int gpf_resolve(struct gpf_font *font, enum gpf_variant_id id, uint32_t code, struct gpf_resolved *res)
Resolves a glyph variant metric and ink.
Definition resolve.c:521
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 ch_width
The advance of the digit zero.
Definition font.h:252
int overline_pos
Overline position.
Definition font.h:263
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_family_meta meta
Font meta data.
Definition font.h:276
A glyph.
Definition font.h:80
unsigned int height
Glyph ink height.
Definition font.h:137
unsigned int width
Glyph ink width.
Definition font.h:133
int bearing_x
Glyph bearing x.
Definition font.h:122
A resolved glyph and where it was drawn from.
Definition resolve.h:36
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
Definition resolve.h:42