gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
font.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <ctype.h>
10
11#include <utils/gp_vec.h>
12#include <utils/gp_vec_str.h>
13
14#include "font.h"
15
16static const char *variant_names[GPF_VARIANTS] = {
17 [GPF_MONO] = "mono",
18 [GPF_REGULAR] = "regular",
19 [GPF_BOLD_MONO] = "bold-mono",
20 [GPF_BOLD] = "bold",
21};
22
23const char *gpf_variant_name(enum gpf_variant_id id)
24{
25 if (id >= GPF_VARIANTS)
26 return NULL;
27
28 return variant_names[id];
29}
30
31int gpf_variant_by_name(const char *name, enum gpf_variant_id *id)
32{
33 unsigned int i;
34
35 for (i = 0; i < GPF_VARIANTS; i++) {
36 if (!strcmp(name, variant_names[i])) {
37 *id = i;
38 return 0;
39 }
40 }
41
42 return 1;
43}
44
45enum gpf_variant_id gpf_variant_parent(enum gpf_variant_id id)
46{
47 switch (id) {
48 case GPF_REGULAR:
49 case GPF_BOLD_MONO:
50 return GPF_MONO;
51 case GPF_BOLD:
52 return GPF_REGULAR;
53 default:
54 return GPF_NO_PARENT;
55 }
56}
57
59{
60 struct gpf_font *font = calloc(1, sizeof(struct gpf_font));
61
62 if (!font)
63 return NULL;
64
65 font->meta.underline_thickness = 1;
66 font->meta.strike_thickness = 1;
67 font->meta.overline_thickness = 1;
68
69 return font;
70}
71
72void gpf_family_meta_guess(struct gpf_family_meta *meta, const char *family,
73 int size, int ascent, int descent, int advance)
74{
75 memset(meta, 0, sizeof(*meta));
76
77 snprintf(meta->family, sizeof(meta->family), "%s",
78 family && *family ? family : "New");
79
80 meta->size = size;
81 meta->ascent = ascent;
82 meta->descent = descent;
83
84 /* where the importer starts it, at the box */
85 meta->em = ascent + descent;
86 meta->x_height = ascent / 2;
87 meta->cap_height = (3 * ascent) / 4;
88 meta->ch_width = advance;
89
90 meta->underline_pos = descent > 2 ? -2 : -1;
94 meta->overline_pos = ascent + 1;
96
97 meta->default_glyph = '?';
98}
99
101 int advance)
102{
103 struct gpf_font *font = gpf_font_new();
104 struct gpf_variant *mono;
105
106 if (!font)
107 return NULL;
108
109 font->meta = *meta;
110
111 mono = gpf_font_variant(font, GPF_MONO, 1);
112 if (!mono) {
113 gpf_font_free(font);
114 return NULL;
115 }
116
117 mono->advance = advance;
118
119 return font;
120}
121
123 const struct gpf_family_meta *b)
124{
125 return !strcmp(a->family, b->family) &&
126 !strcmp(a->license, b->license) &&
127 a->size == b->size &&
128 a->ascent == b->ascent &&
129 a->descent == b->descent &&
130 a->line_gap == b->line_gap &&
131 a->em == b->em &&
132 a->x_height == b->x_height &&
133 a->cap_height == b->cap_height &&
134 a->ch_width == b->ch_width &&
135 a->underline_pos == b->underline_pos &&
137 a->strike_pos == b->strike_pos &&
139 a->overline_pos == b->overline_pos &&
142}
143
144static void variant_free(struct gpf_variant *self)
145{
146 size_t i;
147
148 if (!self)
149 return;
150
151 for (i = 0; i < gp_vec_len(self->glyphs); i++)
152 free(self->glyphs[i].bits);
153
154 gp_vec_free(self->glyphs);
155 gp_vec_free(self->comments);
156 free(self);
157}
158
159void gpf_font_free(struct gpf_font *self)
160{
161 unsigned int i;
162
163 if (!self)
164 return;
165
166 for (i = 0; i < GPF_VARIANTS; i++)
167 variant_free(self->variants[i]);
168
169 gp_vec_free(self->comments);
170 gp_vec_free(self->blocks);
171 gp_vec_free(self->authors);
172 free(self);
173}
174
176 enum gpf_variant_id id, int create)
177{
178 struct gpf_variant *variant;
179
180 if (id >= GPF_VARIANTS)
181 return NULL;
182
183 if (self->variants[id] || !create)
184 return self->variants[id];
185
186 variant = calloc(1, sizeof(struct gpf_variant));
187 if (!variant)
188 return NULL;
189
190 variant->glyphs = gp_vec_new(0, sizeof(struct gpf_glyph));
191 if (!variant->glyphs) {
192 free(variant);
193 return NULL;
194 }
195
196 variant->id = id;
197 variant->spacing_mono = (id == GPF_MONO || id == GPF_BOLD_MONO);
198 variant->derive_embolden = (id == GPF_BOLD_MONO || id == GPF_BOLD);
199
200 self->variants[id] = variant;
201
202 return variant;
203}
204
205/*
206 * Returns the index of code, or the index it would be inserted at.
207 */
208static size_t glyph_lookup(struct gpf_variant *self, uint32_t code, int *found)
209{
210 size_t l = 0, r = gp_vec_len(self->glyphs);
211
212 *found = 0;
213
214 while (l < r) {
215 size_t m = l + (r - l) / 2;
216
217 if (self->glyphs[m].code == code) {
218 *found = 1;
219 return m;
220 }
221
222 if (self->glyphs[m].code < code)
223 l = m + 1;
224 else
225 r = m;
226 }
227
228 return l;
229}
230
231struct gpf_glyph *gpf_variant_glyph(struct gpf_variant *self, uint32_t code)
232{
233 int found;
234 size_t idx = glyph_lookup(self, code, &found);
235
236 if (!found)
237 return NULL;
238
239 return &self->glyphs[idx];
240}
241
242struct gpf_glyph *gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code)
243{
244 struct gpf_glyph *glyphs;
245 int found;
246 size_t idx = glyph_lookup(self, code, &found);
247
248 if (found)
249 return &self->glyphs[idx];
250
251 /* the gap gp_vec_ins() opens is zeroed for us */
252 glyphs = gp_vec_ins(self->glyphs, idx, 1);
253 if (!glyphs)
254 return NULL;
255
256 self->glyphs = glyphs;
257 self->glyphs[idx].code = code;
258
259 return &self->glyphs[idx];
260}
261
262static int code_cmp(const void *a, const void *b)
263{
264 uint32_t ca = *(const uint32_t *)a, cb = *(const uint32_t *)b;
265
266 return ca < cb ? -1 : ca > cb;
267}
268
270{
271 unsigned int i;
272 size_t j;
273
274 for (i = 0; i < GPF_VARIANTS; i++) {
275 struct gpf_variant *variant = self->variants[i];
276
277 if (!variant)
278 continue;
279
280 for (j = 0; j < gpf_variant_glyphs(variant); j++)
281 variant->glyphs[j].modified = 0;
282 }
283}
284
285void gpf_font_ink_extent(struct gpf_font *self, int *top, int *bottom)
286{
287 unsigned int i;
288 size_t j;
289
290 *top = self->meta.ascent;
291 *bottom = -self->meta.descent;
292
293 for (i = 0; i < GPF_VARIANTS; i++) {
294 struct gpf_variant *variant = self->variants[i];
295
296 if (!variant)
297 continue;
298
299 for (j = 0; j < gpf_variant_glyphs(variant); j++) {
300 struct gpf_glyph *glyph = &variant->glyphs[j];
301 int low;
302
303 if (!glyph->height)
304 continue;
305
306 low = glyph->bearing_y - (int)glyph->height + 1;
307
308 if (glyph->bearing_y > *top)
309 *top = glyph->bearing_y;
310
311 if (low < *bottom)
312 *bottom = low;
313 }
314 }
315}
316
317uint32_t *gpf_font_codes(struct gpf_font *self)
318{
319 uint32_t *vec = gp_vec_new(0, sizeof(uint32_t));
320 unsigned int i;
321 size_t j, cnt;
322
323 if (!vec)
324 return NULL;
325
326 for (i = 0; i < GPF_VARIANTS; i++) {
327 struct gpf_variant *variant = self->variants[i];
328
329 if (!variant)
330 continue;
331
332 for (j = 0; j < gpf_variant_glyphs(variant); j++) {
333 if (!GP_VEC_APPEND(vec, variant->glyphs[j].code)) {
334 gp_vec_free(vec);
335 return NULL;
336 }
337 }
338 }
339
340 cnt = gp_vec_len(vec);
341
342 qsort(vec, cnt, sizeof(uint32_t), code_cmp);
343
344 for (j = cnt; j > 1; j--) {
345 if (vec[j-1] == vec[j-2])
346 vec = gp_vec_del(vec, j-1, 1);
347 }
348
349 return vec;
350}
351
352int gpf_font_block_declared(struct gpf_font *self, uint32_t min)
353{
354 size_t i;
355
356 for (i = 0; i < gp_vec_len(self->blocks); i++) {
357 if (self->blocks[i].min == min)
358 return 1;
359 }
360
361 return 0;
362}
363
364int gpf_font_block_add(struct gpf_font *self, uint32_t min, uint32_t max,
365 const char *name)
366{
367 struct gpf_block_decl decl = {.min = min, .max = max};
368 struct gpf_block_decl *blocks;
369 size_t i;
370
371 if (gpf_font_block_declared(self, min))
372 return 0;
373
374 snprintf(decl.name, sizeof(decl.name), "%s", name ? name : "");
375
376 if (!self->blocks) {
377 self->blocks = gp_vec_new(0, sizeof(struct gpf_block_decl));
378 if (!self->blocks)
379 return 1;
380 }
381
382 for (i = 0; i < gp_vec_len(self->blocks); i++) {
383 if (self->blocks[i].min > min)
384 break;
385 }
386
387 blocks = gp_vec_ins(self->blocks, i, 1);
388 if (!blocks)
389 return 1;
390
391 self->blocks = blocks;
392 self->blocks[i] = decl;
393
394 return 0;
395}
396
397const char *gpf_author_invalid(const char *years, const char *name,
398 const char *email)
399{
400 struct gpf_author author;
401 size_t i;
402
403 if (!isdigit((unsigned char)years[0]))
404 return "the years do not start with a year";
405
406 for (i = 0; years[i]; i++) {
407 if (!isdigit((unsigned char)years[i]) && years[i] != '-' &&
408 years[i] != ',')
409 return "the years are not years";
410 }
411
412 if (!name[0])
413 return "the author needs a name";
414
415 if (!email[0] || strpbrk(email, " \t<>") || !strchr(email, '@'))
416 return "the email is not an email";
417
418 if (strlen(years) >= sizeof(author.years) ||
419 strlen(name) >= sizeof(author.name) ||
420 strlen(email) >= sizeof(author.email))
421 return "the author is too long";
422
423 return NULL;
424}
425
426int gpf_authors_add(struct gpf_author **authors, const char *years,
427 const char *name, const char *email)
428{
429 struct gpf_author author;
430
432 return 1;
433
434 strcpy(author.years, years);
435 strcpy(author.name, name);
436 strcpy(author.email, email);
437
438 if (!*authors) {
439 *authors = gp_vec_new(0, sizeof(struct gpf_author));
440 if (!*authors)
441 return 1;
442 }
443
444 if (!GP_VEC_APPEND(*authors, author))
445 return 1;
446
447 return 0;
448}
449
450void gpf_authors_del(struct gpf_author **authors, size_t idx)
451{
452 if (idx >= gp_vec_len(*authors))
453 return;
454
455 *authors = gp_vec_del(*authors, idx, 1);
456}
457
458struct gpf_author *gpf_authors_dup(const struct gpf_author *authors)
459{
460 size_t len = gp_vec_len(authors);
461 struct gpf_author *dup = gp_vec_new(len, sizeof(struct gpf_author));
462
463 if (!dup)
464 return NULL;
465
466 if (len)
467 memcpy(dup, authors, len * sizeof(struct gpf_author));
468
469 return dup;
470}
471
472int gpf_authors_same(const struct gpf_author *a, const struct gpf_author *b)
473{
474 size_t i;
475
476 if (gp_vec_len(a) != gp_vec_len(b))
477 return 0;
478
479 for (i = 0; i < gp_vec_len(a); i++) {
480 if (strcmp(a[i].years, b[i].years) ||
481 strcmp(a[i].name, b[i].name) ||
482 strcmp(a[i].email, b[i].email))
483 return 0;
484 }
485
486 return 1;
487}
488
489void gpf_variant_glyph_del(struct gpf_variant *self, uint32_t code)
490{
491 int found;
492 size_t idx = glyph_lookup(self, code, &found);
493
494 if (!found)
495 return;
496
497 free(self->glyphs[idx].bits);
498
499 self->glyphs = gp_vec_del(self->glyphs, idx, 1);
500}
501
502static unsigned int variant_clear(struct gpf_variant *self,
503 uint32_t min, uint32_t max)
504{
505 size_t i, first = 0, cnt = 0;
506
507 for (i = 0; i < gp_vec_len(self->glyphs); i++) {
508 if (self->glyphs[i].code < min)
509 continue;
510
511 if (self->glyphs[i].code > max)
512 break;
513
514 if (!cnt)
515 first = i;
516
517 free(self->glyphs[i].bits);
518 cnt++;
519 }
520
521 if (cnt)
522 self->glyphs = gp_vec_del(self->glyphs, first, cnt);
523
524 return cnt;
525}
526
527unsigned int gpf_font_range_glyphs(struct gpf_font *self, uint32_t min,
528 uint32_t max)
529{
530 unsigned int i, cnt = 0;
531 size_t j;
532
533 for (i = 0; i < GPF_VARIANTS; i++) {
534 struct gpf_variant *variant = self->variants[i];
535
536 if (!variant)
537 continue;
538
539 for (j = 0; j < gpf_variant_glyphs(variant); j++) {
540 uint32_t code = variant->glyphs[j].code;
541
542 if (code >= min && code <= max)
543 cnt++;
544 }
545 }
546
547 return cnt;
548}
549
550unsigned int gpf_font_block_clear(struct gpf_font *self, uint32_t min,
551 uint32_t max)
552{
553 unsigned int i, cnt = 0;
554
555 for (i = 0; i < GPF_VARIANTS; i++) {
556 if (self->variants[i])
557 cnt += variant_clear(self->variants[i], min, max);
558 }
559
560 return cnt;
561}
562
563void gpf_font_block_del(struct gpf_font *self, uint32_t min)
564{
565 size_t i;
566
567 for (i = 0; i < gp_vec_len(self->blocks); i++) {
568 if (self->blocks[i].min != min)
569 continue;
570
571 self->blocks = gp_vec_del(self->blocks, i, 1);
572 return;
573 }
574}
575
576void gpf_glyph_clear(struct gpf_glyph *self)
577{
578 uint32_t code = self->code;
579
580 free(self->bits);
581 memset(self, 0, sizeof(struct gpf_glyph));
582 self->code = code;
583}
584
585int gpf_glyph_set_bits(struct gpf_glyph *self, const uint8_t *bits,
586 unsigned int w, unsigned int h,
587 int bearing_x, int bearing_y)
588{
589 unsigned int x, y;
590 int min_x = w, max_x = -1, min_y = h, max_y = -1;
591 unsigned int nw, nh;
592 uint8_t *nbits;
593
594 for (y = 0; y < h; y++) {
595 for (x = 0; x < w; x++) {
596 if (!bits[y * w + x])
597 continue;
598
599 if ((int)x < min_x)
600 min_x = x;
601 if ((int)x > max_x)
602 max_x = x;
603 if ((int)y < min_y)
604 min_y = y;
605 if ((int)y > max_y)
606 max_y = y;
607 }
608 }
609
610 free(self->bits);
611 self->bits = NULL;
612
613 /* An empty glyph, a space, carries no ink and no bearings */
614 if (max_x < 0) {
615 self->width = 0;
616 self->height = 0;
617 self->bearing_x = 0;
618 self->bearing_y = 0;
619 return 0;
620 }
621
622 nw = max_x - min_x + 1;
623 nh = max_y - min_y + 1;
624
625 nbits = malloc(nw * nh);
626 if (!nbits)
627 return 1;
628
629 for (y = 0; y < nh; y++) {
630 for (x = 0; x < nw; x++)
631 nbits[y * nw + x] = !!bits[(y + min_y) * w + x + min_x];
632 }
633
634 self->bits = nbits;
635 self->width = nw;
636 self->height = nh;
637 self->bearing_x = bearing_x + (int)min_x;
638 self->bearing_y = bearing_y - (int)min_y;
639
640 return 0;
641}
The in-memory font model.
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
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
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
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
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 * bits
Glyph ink.
Definition font.h:145
int bearing_y
Glyph bearing y.
Definition font.h:128
unsigned int height
Glyph ink height.
Definition font.h:137
unsigned int width
Glyph ink width.
Definition font.h:133
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