gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
resolve.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 "embolden.h"
10#include "compose.h"
11#include "edit.h"
12#include "resolve.h"
13
14/* a compose block may reference a composed glyph, but not forever */
15#define GPF_COMPOSE_DEPTH_MAX 8
16
17static const char *provenance_names[] = {
18 [GPF_PROV_STORED] = "stored",
19 [GPF_PROV_COMPOSED] = "composed",
20 [GPF_PROV_INHERITED] = "inherited",
21 [GPF_PROV_EMBOLDENED] = "emboldened",
22 [GPF_PROV_BOLD_MONO] = "bold-mono",
23};
24
25const char *gpf_provenance_name(enum gpf_provenance prov)
26{
27 return provenance_names[prov];
28}
29
30void gpf_resolved_clear(struct gpf_resolved *self)
31{
32 free(self->glyph.bits);
33 memset(self, 0, sizeof(*self));
34}
35
36static int glyph_copy(struct gpf_glyph *dst, const struct gpf_glyph *src)
37{
38 free(dst->bits);
39 *dst = *src;
40 dst->bits = NULL;
41
42 if (!src->width || !src->height)
43 return 0;
44
45 dst->bits = malloc((size_t)src->width * src->height);
46 if (!dst->bits)
47 return 1;
48
49 memcpy(dst->bits, src->bits, (size_t)src->width * src->height);
50
51 return 0;
52}
53
54/*
55 * Takes the ink over, the metrics stay readable in src.
56 */
57static void glyph_move(struct gpf_glyph *dst, struct gpf_glyph *src)
58{
59 free(dst->bits);
60 *dst = *src;
61 src->bits = NULL;
62}
63
64/*
65 * A numbers block overrides the metrics it carries and nothing else. The
66 * shift moves the ink wherever the parent has it.
67 */
68static void apply_metrics(struct gpf_glyph *glyph, const struct gpf_glyph *entry)
69{
70 if (!entry)
71 return;
72
73 if (entry->has_advance)
74 glyph->advance = entry->advance;
75
76 if (entry->has_shift) {
77 glyph->bearing_x += entry->shift_x;
78 glyph->bearing_y += entry->shift_y;
79 }
80}
81
82static struct gpf_glyph *entry_get(struct gpf_font *font,
83 enum gpf_variant_id id, uint32_t code)
84{
85 struct gpf_variant *variant = font->variants[id];
86
87 if (!variant)
88 return NULL;
89
90 return gpf_variant_glyph(variant, code);
91}
92
93/*
94 * An entry that carries ink, either drawn or composed.
95 */
96static int entry_has_ink(const struct gpf_glyph *entry)
97{
98 return entry && entry->kind != GPF_GLYPH_METRICS;
99}
100
101static int resolve(struct gpf_font *font, enum gpf_variant_id id,
102 uint32_t code, struct gpf_resolved *res, int depth);
103
104static int materialize(struct gpf_font *font, enum gpf_variant_id id,
105 const struct gpf_glyph *entry, struct gpf_resolved *res,
106 int depth)
107{
108 struct gpf_resolved base = {0}, accent = {0};
109 int ret;
110
111 res->ink_from = id;
112
113 if (entry->kind == GPF_GLYPH_INK) {
114 res->prov = GPF_PROV_STORED;
115 return glyph_copy(&res->glyph, entry);
116 }
117
118 if (depth >= GPF_COMPOSE_DEPTH_MAX)
119 return 1;
120
121 if (resolve(font, id, entry->base, &base, depth + 1))
122 return 1;
123
124 if (resolve(font, id, entry->accent, &accent, depth + 1)) {
125 gpf_resolved_clear(&base);
126 return 1;
127 }
128
129 ret = gpf_compose(&res->glyph, &base.glyph, &accent.glyph,
130 entry->dx, entry->dy);
131
132 gpf_resolved_clear(&base);
133 gpf_resolved_clear(&accent);
134
135 res->prov = GPF_PROV_COMPOSED;
136 res->glyph.code = entry->code;
137
138 return ret;
139}
140
141/*
142 * The base variant, the ink is either drawn or composed here. A numbers
143 * block has no parent to inherit ink from, which is how an empty glyph such
144 * as a space is stored.
145 */
146static int resolve_mono(struct gpf_font *font, uint32_t code,
147 struct gpf_resolved *res, int depth)
148{
149 struct gpf_glyph *entry = entry_get(font, GPF_MONO, code);
150
151 if (!entry)
152 return 1;
153
154 if (entry_has_ink(entry))
155 return materialize(font, GPF_MONO, entry, res, depth);
156
157 res->glyph.code = code;
158 res->glyph.kind = GPF_GLYPH_INK;
159 res->glyph.has_advance = 1;
160 res->glyph.has_bearing = 1;
161 res->glyph.advance = entry->advance;
162 res->prov = GPF_PROV_STORED;
163 res->ink_from = GPF_MONO;
164
165 return 0;
166}
167
168/*
169 * The composition that defines a glyph, taken from this variant or inherited
170 * from a parent. A composition is inherited as a definition, not as ink: a
171 * variant that redraws the base redraws everything composed from it, which is
172 * the whole reason for storing accented glyphs this way. An ink entry anywhere
173 * on the way up overrides the definition, that is what drawing one means.
174 */
175struct gpf_glyph *gpf_compose_def(struct gpf_font *font,
176 enum gpf_variant_id id, uint32_t code)
177{
178 while (id != GPF_NO_PARENT) {
179 struct gpf_glyph *entry = entry_get(font, id, code);
180
181 if (entry && entry->kind == GPF_GLYPH_COMPOSE)
182 return entry;
183
184 if (entry_has_ink(entry))
185 return NULL;
186
187 id = gpf_variant_parent(id);
188 }
189
190 return NULL;
191}
192
193static int resolve_regular(struct gpf_font *font, uint32_t code,
194 struct gpf_resolved *res, int depth)
195{
196 struct gpf_glyph *entry = entry_get(font, GPF_REGULAR, code);
197 struct gpf_glyph *def;
198
199 if (entry_has_ink(entry))
200 return materialize(font, GPF_REGULAR, entry, res, depth);
201
202 def = gpf_compose_def(font, GPF_MONO, code);
203
204 if (def && !materialize(font, GPF_REGULAR, def, res, depth)) {
205 apply_metrics(&res->glyph, entry);
206 return 0;
207 }
208
209 if (resolve_mono(font, code, res, depth))
210 return 1;
211
213 res->ink_from = GPF_MONO;
214
215 apply_metrics(&res->glyph, entry);
216
217 return 0;
218}
219
220/* the cell of the monospace family, zero when there is none to keep */
221static int mono_cell(struct gpf_font *font)
222{
223 struct gpf_variant *mono = font->variants[GPF_MONO];
224
225 if (!mono || !mono->spacing_mono)
226 return 0;
227
228 return mono->advance;
229}
230
231static int resolve_bold_mono(struct gpf_font *font, uint32_t code,
232 struct gpf_resolved *res, int depth)
233{
234 struct gpf_glyph *entry = entry_get(font, GPF_BOLD_MONO, code);
235 struct gpf_glyph *def;
236
237 if (entry_has_ink(entry))
238 return materialize(font, GPF_BOLD_MONO, entry, res, depth);
239
240 /* the parts are emboldened on the way out of this variant */
241 def = gpf_compose_def(font, GPF_MONO, code);
242
243 if (def && !materialize(font, GPF_BOLD_MONO, def, res, depth)) {
244 apply_metrics(&res->glyph, entry);
245 return 0;
246 }
247
248 if (resolve_mono(font, code, res, depth))
249 return 1;
250
251 /* bold-mono keeps the cell the monospace family shares */
252 if (gpf_embolden(&res->glyph, mono_cell(font))) {
253 gpf_resolved_clear(res);
254 return 1;
255 }
256
258 res->ink_from = GPF_MONO;
259
260 apply_metrics(&res->glyph, entry);
261
262 return 0;
263}
264
265static int ink_eq(const struct gpf_glyph *a, const struct gpf_glyph *b)
266{
267 if (a->width != b->width || a->height != b->height)
268 return 0;
269
270 if (!a->width || !a->height)
271 return 1;
272
273 return !memcmp(a->bits, b->bits, (size_t)a->width * a->height);
274}
275
276/*
277 * Whether regular's ink differs from mono's, not whether regular has an ink
278 * entry. Editing materializes a glyph as ink and saving writes the same ink
279 * back as a numbers block, so the two have to resolve bold the same way.
280 */
281static int regular_overrides_ink(const struct gpf_glyph *regular_entry,
282 const struct gpf_glyph *mono)
283{
284 if (!entry_has_ink(regular_entry))
285 return 0;
286
287 if (!mono)
288 return 1;
289
290 return !ink_eq(regular_entry, mono);
291}
292
293/*
294 * The three step rule. The choice is made per glyph, which is what stops a
295 * hand fix having to be drawn twice.
296 */
297static int resolve_bold(struct gpf_font *font, uint32_t code,
298 struct gpf_resolved *res, int depth)
299{
300 struct gpf_glyph *entry = entry_get(font, GPF_BOLD, code);
301 struct gpf_glyph *regular_entry;
302 struct gpf_resolved regular = {0}, mono = {0};
303 int has_mono, widen = 0;
304
305 /* 1. hand drawn in bold */
306 if (entry_has_ink(entry))
307 return materialize(font, GPF_BOLD, entry, res, depth);
308
309 /*
310 * A composition is built here out of this variant's parts, so its
311 * metrics are its base's and the step 2 and 3 rules below do not
312 * apply to it.
313 */
314 regular_entry = gpf_compose_def(font, GPF_REGULAR, code);
315
316 if (regular_entry &&
317 !materialize(font, GPF_BOLD, regular_entry, res, depth)) {
318 apply_metrics(&res->glyph, entry);
319 return 0;
320 }
321
322 if (resolve_regular(font, code, &regular, depth))
323 return 1;
324
325 has_mono = !resolve_mono(font, code, &mono, depth);
326
327 regular_entry = entry_get(font, GPF_REGULAR, code);
328
329 if (regular_overrides_ink(regular_entry,
330 has_mono ? &mono.glyph : NULL)) {
331 /* 2. regular overrides the ink, embolden the shape it should */
332 glyph_move(&res->glyph, &regular.glyph);
333
334 /* proportional, so the pixel goes on the advance below */
335 if (gpf_embolden(&res->glyph, 0)) {
336 gpf_resolved_clear(&mono);
338 return 1;
339 }
340
342 res->ink_from = GPF_REGULAR;
343 widen = 1;
344 } else {
345 /* 3. whatever bold-mono has, hand fix or embolden */
346 struct gpf_resolved bold_mono = {0};
347
348 if (resolve_bold_mono(font, code, &bold_mono, depth)) {
349 gpf_resolved_clear(&mono);
350 gpf_resolved_clear(&regular);
351 return 1;
352 }
353
354 glyph_move(&res->glyph, &bold_mono.glyph);
355
356 res->prov = bold_mono.prov == GPF_PROV_EMBOLDENED ?
358 res->ink_from = GPF_BOLD_MONO;
359
360 /*
361 * Bold-mono ink was made to fit the monospace cell, which is
362 * room only as long as regular keeps the advance mono has.
363 */
364 widen = has_mono && regular.glyph.advance != mono.glyph.advance;
365
366 gpf_resolved_clear(&bold_mono);
367 }
368
369 /* the metrics always come from regular */
370 res->glyph.advance = regular.glyph.advance;
371 res->glyph.bearing_x = regular.glyph.bearing_x;
372 res->glyph.bearing_y = regular.glyph.bearing_y;
373
374 /*
375 * A proportional advance is fitted to regular's ink, so bold keeps
376 * the gap regular leaves on the right and the advance grows by what
377 * the bold ink is wider. A hand drawn glyph was drawn to its own
378 * advance and does not get here.
379 */
380 if (widen && res->glyph.width)
381 res->glyph.advance += (int)res->glyph.width - (int)regular.glyph.width;
382
383 apply_metrics(&res->glyph, entry);
384
385 gpf_resolved_clear(&mono);
386 gpf_resolved_clear(&regular);
387
388 return 0;
389}
390
391static int resolve(struct gpf_font *font, enum gpf_variant_id id,
392 uint32_t code, struct gpf_resolved *res, int depth)
393{
394 switch (id) {
395 case GPF_MONO:
396 return resolve_mono(font, code, res, depth);
397 case GPF_REGULAR:
398 return resolve_regular(font, code, res, depth);
399 case GPF_BOLD_MONO:
400 return resolve_bold_mono(font, code, res, depth);
401 case GPF_BOLD:
402 return resolve_bold(font, code, res, depth);
403 default:
404 return 1;
405 }
406}
407
408/*
409 * Neutralizing the entry makes every resolution path treat it as "no ink
410 * here", which is exactly what the entry is an override of.
411 */
413 uint32_t code, struct gpf_resolved *res)
414{
415 struct gpf_variant *variant = font->variants[id];
416 struct gpf_glyph *entry = variant ? gpf_variant_glyph(variant, code) : NULL;
417 struct gpf_glyph saved;
418 int ret;
419
420 if (!entry)
421 return gpf_resolve(font, id, code, res);
422
423 saved = *entry;
424
425 entry->kind = GPF_GLYPH_METRICS;
426 entry->has_advance = 0;
427 entry->has_shift = 0;
428
429 ret = gpf_resolve(font, id, code, res);
430
431 *entry = saved;
432
433 return ret;
434}
435
437 enum gpf_variant_id id,
438 const struct gpf_glyph *glyph,
439 struct gpf_glyph *out)
440{
441 struct gpf_resolved base;
442 enum gpf_entry_form form = GPF_ENTRY_INK;
443 struct gpf_glyph *numbers = out;
444
445 memset(out, 0, sizeof(*out));
446 out->code = glyph->code;
447 out->kind = GPF_GLYPH_METRICS;
448
449 if (glyph->kind != GPF_GLYPH_INK)
450 return GPF_ENTRY_INK;
451
453 return GPF_ENTRY_INK;
454
455 if (gpf_resolve_derived(font, id, glyph->code, &base))
456 return GPF_ENTRY_INK;
457
458 if (glyph->width != base.glyph.width || glyph->height != base.glyph.height)
459 goto out;
460
461 if (glyph->width &&
462 memcmp(glyph->bits, base.glyph.bits,
463 (size_t)glyph->width * glyph->height))
464 goto out;
465
466 /* the same ink, so only the numbers can differ */
467 form = GPF_ENTRY_METRICS;
468
469 if (glyph->bearing_x != base.glyph.bearing_x ||
470 glyph->bearing_y != base.glyph.bearing_y) {
471 numbers->has_shift = 1;
472 numbers->shift_x = glyph->bearing_x - base.glyph.bearing_x;
473 numbers->shift_y = glyph->bearing_y - base.glyph.bearing_y;
474 }
475
476 if (glyph->advance != base.glyph.advance) {
477 numbers->has_advance = 1;
478 numbers->advance = glyph->advance;
479 }
480
481 if (!numbers->has_shift && !numbers->has_advance)
482 form = GPF_ENTRY_NONE;
483out:
484 gpf_resolved_clear(&base);
485
486 return form;
487}
488
489void gpf_font_simplify(struct gpf_font *font)
490{
491 unsigned int i;
492 size_t j;
493
494 /* from the base outwards, a variant derives through its parent */
495 for (i = 0; i < GPF_VARIANTS; i++) {
496 struct gpf_variant *variant = font->variants[i];
497
498 if (!variant || gpf_variant_parent(i) == GPF_NO_PARENT)
499 continue;
500
501 for (j = gpf_variant_glyphs(variant); j > 0; j--) {
502 struct gpf_glyph *glyph = &variant->glyphs[j-1];
503 struct gpf_glyph numbers;
504
505 switch (gpf_entry_form(font, i, glyph, &numbers)) {
506 case GPF_ENTRY_NONE:
507 gpf_variant_glyph_del(variant, glyph->code);
508 break;
510 free(glyph->bits);
511 numbers.code = glyph->code;
512 *glyph = numbers;
513 break;
514 case GPF_ENTRY_INK:
515 break;
516 }
517 }
518 }
519}
520
521int gpf_resolve(struct gpf_font *font, enum gpf_variant_id id, uint32_t code,
522 struct gpf_resolved *res)
523{
524 memset(res, 0, sizeof(*res));
525
526 if (resolve(font, id, code, res, 0)) {
527 gpf_resolved_clear(res);
528 return 1;
529 }
530
531 res->glyph.code = code;
532
533 return 0;
534}
535
536int gpf_measure(struct gpf_font *font, enum gpf_variant_id id,
537 enum gpf_measure what, int *val)
538{
539 static const uint32_t codes[] = {
540 [GPF_MEASURE_X_HEIGHT] = 'x',
542 [GPF_MEASURE_CH_WIDTH] = '0',
543 };
544 struct gpf_resolved res;
545 int ret = 0;
546
547 if (gpf_resolve(font, id, codes[what], &res))
548 return 1;
549
550 switch (what) {
553 if (res.glyph.height)
554 *val = res.glyph.bearing_y;
555 else
556 ret = 1;
557 break;
559 *val = res.glyph.advance;
560 break;
561 }
562
563 gpf_resolved_clear(&res);
564
565 return ret;
566}
Accented letters: the decomposition table and accent composition.
Glyph editing.
Generates bold letters.
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
void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code)
Removes a glyph from a variant.
Definition font.c:489
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_glyph * gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
Looks up a glyph in a variant.
Definition font.c:231
@ 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
The glyph variant lattice.
gpf_entry_form
The smallest form an entry can be written in on save.
Definition resolve.h:101
@ GPF_ENTRY_METRICS
The ink is the parent's, only numbers differ.
Definition resolve.h:105
@ GPF_ENTRY_INK
The ink differs, it has to be saved.
Definition resolve.h:107
@ GPF_ENTRY_NONE
Identical to what the variant would derive, no entry needed.
Definition resolve.h:103
int gpf_resolve_derived(struct gpf_font *font, enum gpf_variant_id id, uint32_t code, struct gpf_resolved *res)
Resolves a derived glyph variant metric and ink.
Definition resolve.c:412
gpf_measure
The family metrics that are measured off the glyphs.
Definition resolve.h:154
@ GPF_MEASURE_CH_WIDTH
The advance of the '0' glyph.
Definition resolve.h:160
@ GPF_MEASURE_CAP_HEIGHT
The top of the 'H' ink above the baseline.
Definition resolve.h:158
@ GPF_MEASURE_X_HEIGHT
The top of the 'x' ink above the baseline.
Definition resolve.h:156
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
gpf_provenance
Glyph provenance.
Definition resolve.h:20
@ GPF_PROV_EMBOLDENED
The parent's ink, emboldened.
Definition resolve.h:28
@ GPF_PROV_COMPOSED
Built from a base glyph and an accent.
Definition resolve.h:24
@ GPF_PROV_BOLD_MONO
Bold taking a hand fixed glyph from bold-mono.
Definition resolve.h:30
@ GPF_PROV_INHERITED
The parent's ink, the metrics may still be overridden.
Definition resolve.h:26
@ GPF_PROV_STORED
The ink is stored in the variant itself.
Definition resolve.h:22
A font family.
Definition font.h:274
struct gpf_variant * variants[GPF_VARIANTS]
Variants indexed by gpf_variant_id, NULL when missing.
Definition font.h:301
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
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
int bearing_x
Glyph bearing x.
Definition font.h:122
uint32_t code
Unicode code point.
Definition font.h:84
A resolved glyph and where it was drawn from.
Definition resolve.h:36
enum gpf_variant_id ink_from
The variant the ink came from.
Definition resolve.h:46
enum gpf_provenance prov
Where the glyph came from.
Definition resolve.h:44
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
Definition resolve.h:42
A font variant.
Definition font.h:170
int advance
Advance for all glyphs.
Definition font.h:182
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