gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
format.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#include <errno.h>
11#include <stdarg.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15
16#include <utils/gp_utf.h>
17#include <utils/gp_vec.h>
18#include <utils/gp_vec_str.h>
19
20#include "resolve.h"
21#include "format.h"
22
23#define LINE_MAX_LEN 1024
24
25int gpf_code_printable(uint32_t code)
26{
27 if (code < 0x20 || code == 0x7f)
28 return 0;
29
30 /* C1 controls */
31 if (code >= 0x80 && code < 0xa0)
32 return 0;
33
34 if (code > 0x10ffff)
35 return 0;
36
37 return 1;
38}
39
40/* ------------------------------------------------------------------ write */
41
42static void write_glyph_header(FILE *f, const struct gpf_glyph *glyph)
43{
44 char buf[5];
45
46 fprintf(f, "glyph U+%04X", glyph->code);
47
48 if (gpf_code_printable(glyph->code)) {
49 buf[gp_to_utf8(glyph->code, buf)] = 0;
50 fprintf(f, " '%s'", buf);
51 }
52
53 fputc('\n', f);
54}
55
56static void write_ink(FILE *f, const struct gpf_glyph *glyph)
57{
58 int top = glyph->bearing_y > 1 ? glyph->bearing_y : 1;
59 int bottom = glyph->bearing_y - (int)glyph->height;
60 int rows = top - (bottom < 0 ? bottom : 0);
61 int left = glyph->bearing_x < 0 ? glyph->bearing_x : 0;
62 int right = glyph->bearing_x + (int)glyph->width;
63 int ink_top = top - glyph->bearing_y;
64 int cols, len, origin, advance, x, y, i;
65
66 /*
67 * An empty glyph still has to show its cell, so it's drawn as a
68 * single blank row spanning the advance.
69 */
70 if (!glyph->width)
71 right = left + (glyph->advance > 0 ? glyph->advance : 1);
72
73 cols = right - left;
74 if (cols < 1)
75 cols = 1;
76
77 for (y = 0; y < rows; y++) {
78 fputs(y == top - 1 ? " _|" : " |", f);
79
80 for (x = 0; x < cols; x++) {
81 int ix = left + x - glyph->bearing_x;
82 int iy = y - ink_top;
83 int set = 0;
84
85 if (ix >= 0 && iy >= 0)
86 set = gpf_glyph_pixel(glyph, ix, iy);
87
88 fputs(set ? "##" : "..", f);
89 }
90
91 fputs("|\n", f);
92 }
93
94 origin = -left;
95 advance = glyph->advance - left;
96
97 len = 2 * (right > glyph->advance ? right - left : advance) + 1;
98
99 fputs(" ", f);
100
101 for (i = 0; i < len; i++)
102 fputc(i == 2 * origin || i == 2 * advance ? '^' : '-', f);
103
104 fputc('\n', f);
105}
106
107/*
108 * An overlay entry is written in the smallest form that is true: an entry
109 * that only moves the ink or changes the advance is numbers, and one that
110 * matches what the variant would derive is not written at all. Editing a
111 * glyph materializes it as ink, so without this every advance change would
112 * store a duplicate of the parent's ink.
113 */
114static void write_glyph(FILE *f, struct gpf_font *font, enum gpf_variant_id id,
115 const struct gpf_glyph *glyph)
116{
117 struct gpf_glyph numbers;
118
119 switch (gpf_entry_form(font, id, glyph, &numbers)) {
120 case GPF_ENTRY_NONE:
121 return;
123 write_glyph_header(f, glyph);
124
125 if (numbers.has_advance)
126 fprintf(f, "advance %i\n", numbers.advance);
127
128 if (numbers.has_shift) {
129 fprintf(f, "shift %i,%i\n",
130 numbers.shift_x, numbers.shift_y);
131 }
132 return;
133 case GPF_ENTRY_INK:
134 break;
135 }
136
137 write_glyph_header(f, glyph);
138
139 switch (glyph->kind) {
140 case GPF_GLYPH_INK:
141 write_ink(f, glyph);
142 break;
144 if (glyph->has_advance)
145 fprintf(f, "advance %i\n", glyph->advance);
146 if (glyph->has_shift) {
147 fprintf(f, "shift %i,%i\n",
148 glyph->shift_x, glyph->shift_y);
149 }
150 break;
152 fprintf(f, "compose U+%04X U+%04X at %i,%i\n",
153 glyph->base, glyph->accent, glyph->dx, glyph->dy);
154 break;
155 }
156}
157
158static int write_variant(struct gpf_font *font, struct gpf_variant *variant,
159 const char *dir, char *err, size_t err_len)
160{
161 char path[4096];
162 FILE *f;
163 size_t i;
164 enum gpf_variant_id parent = gpf_variant_parent(variant->id);
165
166 snprintf(path, sizeof(path), "%s/%s", dir, gpf_variant_name(variant->id));
167
168 f = fopen(path, "w");
169 if (!f) {
170 snprintf(err, err_len, "%s: %s", path, strerror(errno));
171 return 1;
172 }
173
174 if (variant->comments)
175 fputs(variant->comments, f);
176
177 fprintf(f, "%-9s %s\n", "variant", gpf_variant_name(variant->id));
178
179 if (parent != GPF_NO_PARENT)
180 fprintf(f, "%-9s %s\n", "parent", gpf_variant_name(parent));
181
182 fprintf(f, "%-9s %s\n", "spacing",
183 variant->spacing_mono ? "mono" : "proportional");
184
185 if (variant->derive_embolden)
186 fprintf(f, "%-9s %s\n", "derive", "embolden");
187
188 if (variant->advance)
189 fprintf(f, "%-9s %i\n", "advance", variant->advance);
190
191 for (i = 0; i < gpf_variant_glyphs(variant); i++) {
192 long pos = ftell(f);
193
194 fputc('\n', f);
195
196 write_glyph(f, font, variant->id, &variant->glyphs[i]);
197
198 /* an entry that says nothing is not written, blank line and all */
199 if (ftell(f) == pos + 1) {
200 fflush(f);
201
202 if (ftruncate(fileno(f), pos))
203 break;
204
205 fseek(f, pos, SEEK_SET);
206 }
207 }
208
209 if (fclose(f)) {
210 snprintf(err, err_len, "%s: %s", path, strerror(errno));
211 return 1;
212 }
213
214 return 0;
215}
216
217static int write_family(struct gpf_font *font, const char *dir,
218 char *err, size_t err_len)
219{
220 struct gpf_family_meta *m = &font->meta;
221 char path[4096];
222 FILE *f;
223 size_t i;
224
225 snprintf(path, sizeof(path), "%s/family", dir);
226
227 f = fopen(path, "w");
228 if (!f) {
229 snprintf(err, err_len, "%s: %s", path, strerror(errno));
230 return 1;
231 }
232
233 if (font->comments)
234 fputs(font->comments, f);
235
236 fprintf(f, "%-14s %s\n", "family", m->family);
237 fprintf(f, "%-14s %i\n", "size", m->size);
238 fprintf(f, "%-14s %i\n", "ascent", m->ascent);
239 fprintf(f, "%-14s %i\n", "descent", m->descent);
240 fprintf(f, "%-14s %i\n", "line_gap", m->line_gap);
241 fputc('\n', f);
242 fprintf(f, "%-14s %i\n", "em", m->em);
243 fprintf(f, "%-14s %i\n", "x_height", m->x_height);
244 fprintf(f, "%-14s %i\n", "cap_height", m->cap_height);
245 fprintf(f, "%-14s %i\n", "ch_width", m->ch_width);
246 fputc('\n', f);
247 fprintf(f, "%-14s %i %i\n", "underline",
249 fprintf(f, "%-14s %i %i\n", "strikethrough",
251 fprintf(f, "%-14s %i %i\n", "overline",
253
254 if (m->default_glyph || m->license[0])
255 fputc('\n', f);
256
257 if (m->default_glyph)
258 fprintf(f, "%-14s U+%04X\n", "default_glyph", m->default_glyph);
259
260 if (m->license[0])
261 fprintf(f, "%-14s %s\n", "license", m->license);
262
263 if (gp_vec_len(font->authors))
264 fputc('\n', f);
265
266 for (i = 0; i < gp_vec_len(font->authors); i++) {
267 fprintf(f, "%-14s %s %s <%s>\n", "author",
268 font->authors[i].years, font->authors[i].name,
269 font->authors[i].email);
270 }
271
272 /*
273 * The blocks the font means to cover. A block that has glyphs needs
274 * no line here, this is for the ones that are still empty.
275 */
276 if (gp_vec_len(font->blocks))
277 fputc('\n', f);
278
279 for (i = 0; i < gp_vec_len(font->blocks); i++) {
280 fprintf(f, "%-14s U+%04X U+%04X %s\n", "block",
281 font->blocks[i].min, font->blocks[i].max,
282 font->blocks[i].name);
283 }
284
285 if (fclose(f)) {
286 snprintf(err, err_len, "%s: %s", path, strerror(errno));
287 return 1;
288 }
289
290 return 0;
291}
292
293int gpf_font_write(struct gpf_font *self, const char *dir,
294 char *err, size_t err_len)
295{
296 unsigned int i;
297
298 if (mkdir(dir, 0755) && errno != EEXIST) {
299 snprintf(err, err_len, "%s: %s", dir, strerror(errno));
300 return 1;
301 }
302
303 if (write_family(self, dir, err, err_len))
304 return 1;
305
306 for (i = 0; i < GPF_VARIANTS; i++) {
307 if (!self->variants[i])
308 continue;
309
310 if (write_variant(self, self->variants[i], dir, err, err_len))
311 return 1;
312 }
313
314 return 0;
315}
316
317/* ------------------------------------------------------------------- read */
318
319struct parser {
320 const char *path;
321 unsigned int line;
322 char *err;
323 size_t err_len;
324};
325
326static int perr(struct parser *p, const char *fmt, ...)
327{
328 char msg[GPF_ERR_MAX];
329 va_list va;
330
331 va_start(va, fmt);
332 vsnprintf(msg, sizeof(msg), fmt, va);
333 va_end(va);
334
335 snprintf(p->err, p->err_len, "%s:%u: %s", p->path, p->line, msg);
336
337 return 1;
338}
339
340static char *skip_space(char *str)
341{
342 while (*str == ' ' || *str == '\t')
343 str++;
344
345 return str;
346}
347
348/*
349 * Strips the trailing newline and whitespace and cuts a comment off. A '#'
350 * only starts a comment at the start of a line or after a whitespace, which
351 * keeps it out of a glyph annotation, and ink rows are left alone since
352 * '#' is also the ink.
353 */
354static void strip_line(char *line)
355{
356 size_t len = strlen(line);
357 char *start = skip_space(line);
358 char *i;
359
360 while (len && (line[len-1] == '\n' || line[len-1] == '\r' ||
361 line[len-1] == ' ' || line[len-1] == '\t'))
362 line[--len] = 0;
363
364 if (*start == '|' || *start == '_' || *start == '^')
365 return;
366
367 for (i = line; *i; i++) {
368 if (*i != '#')
369 continue;
370
371 if (i == line || i[-1] == ' ' || i[-1] == '\t') {
372 *i = 0;
373 while (i > line && (i[-1] == ' ' || i[-1] == '\t'))
374 *--i = 0;
375 return;
376 }
377 }
378}
379
380static int line_is_comment(const char *line)
381{
382 const char *str = skip_space((char *)line);
383
384 return *str == '#';
385}
386
387/*
388 * Splits a header line into a key and the rest.
389 */
390static char *split_key(char *line, char **key)
391{
392 char *val;
393
394 *key = skip_space(line);
395
396 val = *key;
397 while (*val && *val != ' ' && *val != '\t')
398 val++;
399
400 if (*val)
401 *val++ = 0;
402
403 return skip_space(val);
404}
405
406static int parse_int(struct parser *p, const char *str, int *val)
407{
408 char *end;
409 long res;
410
411 if (!*str)
412 return perr(p, "expected a number");
413
414 errno = 0;
415 res = strtol(str, &end, 10);
416
417 if (errno || *end)
418 return perr(p, "invalid number '%s'", str);
419
420 *val = res;
421
422 return 0;
423}
424
425static int parse_two_ints(struct parser *p, char *str, int *a, int *b)
426{
427 char *second = str;
428
429 while (*second && *second != ' ' && *second != '\t')
430 second++;
431
432 if (!*second)
433 return perr(p, "expected two numbers");
434
435 *second++ = 0;
436
437 if (parse_int(p, str, a))
438 return 1;
439
440 return parse_int(p, skip_space(second), b);
441}
442
443static int parse_code(struct parser *p, const char *str, uint32_t *code)
444{
445 char *end;
446 unsigned long res;
447
448 if (str[0] != 'U' || str[1] != '+')
449 return perr(p, "expected U+XXXX, got '%s'", str);
450
451 errno = 0;
452 res = strtoul(str + 2, &end, 16);
453
454 if (errno || (*end && *end != ' ' && *end != '\t'))
455 return perr(p, "invalid codepoint '%s'", str);
456
457 *code = res;
458
459 return 0;
460}
461
462/*
463 * A growable vector of the lines of a single glyph block.
464 */
465struct block {
466 /* a gp_vec of the lines of a single glyph block */
467 char **lines;
468 unsigned int first_line;
469 uint32_t code;
470};
471
472static int block_add(struct block *self, const char *line)
473{
474 char *str = strdup(line);
475
476 if (!str)
477 return 1;
478
479 if (!self->lines) {
480 self->lines = gp_vec_new(0, sizeof(char *));
481 if (!self->lines) {
482 free(str);
483 return 1;
484 }
485 }
486
487 if (!GP_VEC_APPEND(self->lines, str)) {
488 free(str);
489 return 1;
490 }
491
492 return 0;
493}
494
495static void block_free(struct block *self)
496{
497 size_t i;
498
499 for (i = 0; i < gp_vec_len(self->lines); i++)
500 free(self->lines[i]);
501
502 gp_vec_free(self->lines);
503 self->lines = NULL;
504}
505
506static int row_ink_start(struct parser *p, const char *line, int *start)
507{
508 const char *bar = strchr(line, '|');
509
510 if (!bar)
511 return perr(p, "ink row without a '|'");
512
513 *start = bar - line + 1;
514
515 return 0;
516}
517
518static int parse_ink(struct parser *p, struct gpf_variant *variant,
519 struct block *block, size_t rows_cnt,
520 char **rows, const char *ruler, int marker)
521{
522 struct gpf_glyph *glyph;
523 uint8_t *bits;
524 int start = 0, cols = 0, first, last, left, top;
525 size_t y;
526 int x;
527
528 if (!ruler)
529 return perr(p, "ink without a ruler");
530
531 if (marker < 0)
532 return perr(p, "ink without a '_' baseline mark");
533
534 if (row_ink_start(p, rows[0], &start))
535 return 1;
536
537 cols = (int)strlen(rows[0]) - start - 1;
538
539 if (cols < 0 || cols % 2)
540 return perr(p, "malformed ink row");
541
542 cols /= 2;
543
544 first = strchr(ruler, '^') - ruler;
545 last = strrchr(ruler, '^') - ruler;
546
547 if ((first - start) % 2 || (last - first) % 2)
548 return perr(p, "ruler is not aligned with the ink");
549
550 left = -(first - start) / 2;
551 top = marker + 1;
552
553 bits = calloc(1, (size_t)cols * rows_cnt + 1);
554 if (!bits)
555 return perr(p, "out of memory");
556
557 for (y = 0; y < rows_cnt; y++) {
558 int rstart;
559
560 if (row_ink_start(p, rows[y], &rstart)) {
561 free(bits);
562 return 1;
563 }
564
565 if (rstart != start || (int)strlen(rows[y]) != start + 2 * cols + 1) {
566 free(bits);
567 return perr(p, "ink rows are not aligned");
568 }
569
570 if (rows[y][start + 2 * cols] != '|') {
571 free(bits);
572 return perr(p, "ink row without a closing '|'");
573 }
574
575 for (x = 0; x < cols; x++) {
576 char a = rows[y][start + 2 * x];
577 char b = rows[y][start + 2 * x + 1];
578
579 if (a != b || (a != '#' && a != '.')) {
580 free(bits);
581 return perr(p, "expected '##' or '..' in an ink row");
582 }
583
584 bits[y * cols + x] = (a == '#');
585 }
586 }
587
588 glyph = gpf_variant_glyph_add(variant, block->code);
589 if (!glyph) {
590 free(bits);
591 return perr(p, "out of memory");
592 }
593
594 glyph->kind = GPF_GLYPH_INK;
595 glyph->has_advance = 1;
596 glyph->has_bearing = 1;
597 glyph->advance = (last - first) / 2;
598
599 if (gpf_glyph_set_bits(glyph, bits, cols, rows_cnt, left, top)) {
600 free(bits);
601 return perr(p, "out of memory");
602 }
603
604 free(bits);
605
606 return 0;
607}
608
609static int parse_glyph_block(struct parser *parser, struct gpf_variant *variant,
610 struct block *block)
611{
612 /* the block is parsed after it has been read, so keep our own line */
613 struct parser bp = *parser;
614 struct parser *p = &bp;
615 char *rows[256];
616 size_t rows_cnt = 0;
617 char *ruler = NULL;
618 int marker = -1;
619 struct gpf_glyph *glyph;
620 size_t i;
621 int has_numbers = 0;
622
623 p->line = block->first_line;
624
625 for (i = 0; i < gp_vec_len(block->lines); i++) {
626 char *line = block->lines[i];
627 char *start = skip_space(line);
628
629 if (*start == '|' || *start == '_') {
630 if (rows_cnt >= 256)
631 return perr(p, "ink too tall");
632
633 if (*start == '_')
634 marker = rows_cnt;
635
636 rows[rows_cnt++] = line;
637 continue;
638 }
639
640 /* a ruler starts with dashes when the origin is inside it */
641 if (*start == '^' || *start == '-') {
642 if (ruler)
643 return perr(p, "more than one ruler");
644
645 ruler = line;
646 continue;
647 }
648
649 has_numbers = 1;
650 }
651
652 if (rows_cnt && has_numbers)
653 return perr(p, "a glyph block mixes ink with numbers");
654
655 if (rows_cnt)
656 return parse_ink(p, variant, block, rows_cnt, rows, ruler, marker);
657
658 if (ruler)
659 return perr(p, "a ruler without ink");
660
661 glyph = gpf_variant_glyph_add(variant, block->code);
662 if (!glyph)
663 return perr(p, "out of memory");
664
665 glyph->kind = GPF_GLYPH_METRICS;
666
667 for (i = 0; i < gp_vec_len(block->lines); i++) {
668 char *key, *val;
669
670 p->line = block->first_line + i + 1;
671
672 val = split_key(block->lines[i], &key);
673
674 if (!strcmp(key, "advance")) {
675 if (parse_int(p, val, &glyph->advance))
676 return 1;
677 glyph->has_advance = 1;
678 continue;
679 }
680
681 if (!strcmp(key, "shift")) {
682 char *comma = strchr(val, ',');
683
684 if (!comma)
685 return perr(p, "shift is 'dx,dy'");
686
687 *comma++ = 0;
688
689 if (parse_int(p, val, &glyph->shift_x))
690 return 1;
691
692 if (parse_int(p, skip_space(comma), &glyph->shift_y))
693 return 1;
694
695 glyph->has_shift = 1;
696 continue;
697 }
698
699 if (!strcmp(key, "compose")) {
700 char *accent, *at;
701
702 glyph->kind = GPF_GLYPH_COMPOSE;
703
704 accent = strchr(val, ' ');
705 if (!accent)
706 return perr(p, "compose needs a base and an accent");
707 *accent++ = 0;
708
709 if (parse_code(p, val, &glyph->base))
710 return 1;
711
712 accent = skip_space(accent);
713
714 at = strstr(accent, " at ");
715 if (!at)
716 return perr(p, "compose needs an 'at dx,dy'");
717 *at = 0;
718 at += 4;
719
720 if (parse_code(p, accent, &glyph->accent))
721 return 1;
722
723 at = skip_space(at);
724
725 {
726 char *comma = strchr(at, ',');
727
728 if (!comma)
729 return perr(p, "compose offset is 'dx,dy'");
730
731 *comma++ = 0;
732
733 if (parse_int(p, at, &glyph->dx))
734 return 1;
735
736 if (parse_int(p, skip_space(comma), &glyph->dy))
737 return 1;
738 }
739
740 continue;
741 }
742
743 return perr(p, "unknown key '%s' in a glyph block", key);
744 }
745
746 return 0;
747}
748
749static int append_comment(char **dst, const char *line)
750{
751 char *str = *dst;
752
753 if (!str) {
754 str = gp_vec_str_new();
755 if (!str)
756 return 1;
757
758 *dst = str;
759 }
760
761 str = gp_vec_str_append(str, line);
762 if (!str)
763 return 1;
764
765 *dst = str;
766
767 str = gp_vec_str_append(str, "\n");
768 if (!str)
769 return 1;
770
771 *dst = str;
772
773 return 0;
774}
775
776/*
777 * An author line is `years name <email>`: the years are the first field, the
778 * email the last, and the name is everything in between, spaces and all.
779 */
780static int parse_author(struct parser *p, struct gpf_font *font, char *val)
781{
782 char *name, *email, *end;
783 const char *msg;
784
785 name = strpbrk(val, " \t");
786 if (!name)
787 return perr(p, "author needs years, a name and an <email>");
788
789 *name++ = 0;
790 name = skip_space(name);
791
792 email = strrchr(name, '<');
793 end = name + strlen(name) - 1;
794
795 if (!email || *end != '>')
796 return perr(p, "author needs an <email> at the end");
797
798 *email++ = 0;
799 *end = 0;
800
801 end = name + strlen(name);
802 while (end > name && (end[-1] == ' ' || end[-1] == '\t'))
803 *--end = 0;
804
805 msg = gpf_author_invalid(val, name, email);
806 if (msg)
807 return perr(p, "%s", msg);
808
809 if (gpf_authors_add(&font->authors, val, name, email))
810 return perr(p, "out of memory");
811
812 return 0;
813}
814
815static int read_family(struct gpf_font *font, const char *dir,
816 char *err, size_t err_len)
817{
818 struct gpf_family_meta *m = &font->meta;
819 char path[4096];
820 char line[LINE_MAX_LEN];
821 struct parser p = {.path = path, .err = err, .err_len = err_len};
822 FILE *f;
823 int seen_key = 0;
824
825 snprintf(path, sizeof(path), "%s/family", dir);
826
827 f = fopen(path, "r");
828 if (!f) {
829 snprintf(err, err_len, "%s: %s", path, strerror(errno));
830 return 1;
831 }
832
833 while (fgets(line, sizeof(line), f)) {
834 char *key, *val;
835
836 p.line++;
837
838 if (!seen_key && line_is_comment(line)) {
839 char *nl = strchr(line, '\n');
840
841 if (nl)
842 *nl = 0;
843
844 if (append_comment(&font->comments, line))
845 goto err;
846
847 continue;
848 }
849
850 strip_line(line);
851
852 if (!*skip_space(line))
853 continue;
854
855 seen_key = 1;
856
857 val = split_key(line, &key);
858
859 if (!strcmp(key, "family")) {
860 snprintf(m->family, sizeof(m->family), "%s", val);
861 continue;
862 }
863
864 if (!strcmp(key, "license")) {
865 snprintf(m->license, sizeof(m->license), "%s", val);
866 continue;
867 }
868
869 if (!strcmp(key, "size")) {
870 if (parse_int(&p, val, &m->size))
871 goto err;
872 continue;
873 }
874
875 if (!strcmp(key, "ascent")) {
876 if (parse_int(&p, val, &m->ascent))
877 goto err;
878 continue;
879 }
880
881 if (!strcmp(key, "descent")) {
882 if (parse_int(&p, val, &m->descent))
883 goto err;
884 continue;
885 }
886
887 if (!strcmp(key, "line_gap")) {
888 if (parse_int(&p, val, &m->line_gap))
889 goto err;
890 continue;
891 }
892
893 if (!strcmp(key, "em")) {
894 if (parse_int(&p, val, &m->em))
895 goto err;
896 continue;
897 }
898
899 if (!strcmp(key, "x_height")) {
900 if (parse_int(&p, val, &m->x_height))
901 goto err;
902 continue;
903 }
904
905 if (!strcmp(key, "cap_height")) {
906 if (parse_int(&p, val, &m->cap_height))
907 goto err;
908 continue;
909 }
910
911 if (!strcmp(key, "ch_width")) {
912 if (parse_int(&p, val, &m->ch_width))
913 goto err;
914 continue;
915 }
916
917 if (!strcmp(key, "underline")) {
918 if (parse_two_ints(&p, val, &m->underline_pos,
920 goto err;
921 continue;
922 }
923
924 if (!strcmp(key, "strikethrough")) {
925 if (parse_two_ints(&p, val, &m->strike_pos,
926 &m->strike_thickness))
927 goto err;
928 continue;
929 }
930
931 if (!strcmp(key, "overline")) {
932 if (parse_two_ints(&p, val, &m->overline_pos,
934 goto err;
935 continue;
936 }
937
938 if (!strcmp(key, "block")) {
939 uint32_t min, max;
940 char *second, *name;
941
942 second = strchr(val, ' ');
943 if (!second) {
944 perr(&p, "block needs a range");
945 goto err;
946 }
947
948 *second++ = 0;
949 second = skip_space(second);
950
951 name = strchr(second, ' ');
952
953 if (name)
954 *name++ = 0;
955
956 if (parse_code(&p, val, &min))
957 goto err;
958
959 if (parse_code(&p, second, &max))
960 goto err;
961
962 if (gpf_font_block_add(font, min, max,
963 name ? skip_space(name) : "")) {
964 perr(&p, "out of memory");
965 goto err;
966 }
967
968 continue;
969 }
970
971 if (!strcmp(key, "default_glyph")) {
972 if (parse_code(&p, val, &m->default_glyph))
973 goto err;
974 continue;
975 }
976
977 if (!strcmp(key, "author")) {
978 if (parse_author(&p, font, val))
979 goto err;
980 continue;
981 }
982
983 perr(&p, "unknown key '%s'", key);
984 goto err;
985 }
986
987 fclose(f);
988
989 return 0;
990err:
991 fclose(f);
992 return 1;
993}
994
995static int read_variant(struct gpf_font *font, enum gpf_variant_id id,
996 const char *dir, char *err, size_t err_len)
997{
998 char path[4096];
999 char line[LINE_MAX_LEN];
1000 struct parser p = {.path = path, .err = err, .err_len = err_len};
1001 struct gpf_variant *variant;
1002 struct block block = {0};
1003 FILE *f;
1004 int in_glyph = 0, seen_key = 0;
1005
1006 snprintf(path, sizeof(path), "%s/%s", dir, gpf_variant_name(id));
1007
1008 f = fopen(path, "r");
1009 if (!f) {
1010 if (errno == ENOENT)
1011 return 0;
1012
1013 snprintf(err, err_len, "%s: %s", path, strerror(errno));
1014 return 1;
1015 }
1016
1017 variant = gpf_font_variant(font, id, 1);
1018 if (!variant) {
1019 fclose(f);
1020 snprintf(err, err_len, "out of memory");
1021 return 1;
1022 }
1023
1024 while (fgets(line, sizeof(line), f)) {
1025 char *key, *val, *start;
1026
1027 p.line++;
1028
1029 if (!seen_key && !in_glyph && line_is_comment(line)) {
1030 char *nl = strchr(line, '\n');
1031
1032 if (nl)
1033 *nl = 0;
1034
1035 if (append_comment(&variant->comments, line))
1036 goto err;
1037
1038 continue;
1039 }
1040
1041 strip_line(line);
1042 start = skip_space(line);
1043
1044 if (!*start) {
1045 if (in_glyph) {
1046 if (parse_glyph_block(&p, variant, &block))
1047 goto err;
1048
1049 block_free(&block);
1050 in_glyph = 0;
1051 }
1052 continue;
1053 }
1054
1055 if (!strncmp(start, "glyph ", 6)) {
1056 if (in_glyph) {
1057 if (parse_glyph_block(&p, variant, &block))
1058 goto err;
1059
1060 block_free(&block);
1061 }
1062
1063 in_glyph = 1;
1064 block.first_line = p.line;
1065
1066 if (parse_code(&p, skip_space(start + 6), &block.code))
1067 goto err;
1068
1069 continue;
1070 }
1071
1072 if (in_glyph) {
1073 if (block_add(&block, line)) {
1074 perr(&p, "out of memory");
1075 goto err;
1076 }
1077 continue;
1078 }
1079
1080 seen_key = 1;
1081
1082 val = split_key(line, &key);
1083
1084 if (!strcmp(key, "variant")) {
1085 enum gpf_variant_id got;
1086
1087 if (gpf_variant_by_name(val, &got)) {
1088 perr(&p, "unknown variant '%s'", val);
1089 goto err;
1090 }
1091
1092 if (got != id) {
1093 perr(&p, "variant '%s' in a file named '%s'",
1094 val, gpf_variant_name(id));
1095 goto err;
1096 }
1097
1098 continue;
1099 }
1100
1101 if (!strcmp(key, "parent")) {
1102 enum gpf_variant_id got;
1103
1104 if (gpf_variant_by_name(val, &got)) {
1105 perr(&p, "unknown parent '%s'", val);
1106 goto err;
1107 }
1108
1109 if (got != gpf_variant_parent(id)) {
1110 perr(&p, "'%s' cannot be a parent of '%s'",
1111 val, gpf_variant_name(id));
1112 goto err;
1113 }
1114
1115 continue;
1116 }
1117
1118 if (!strcmp(key, "spacing")) {
1119 if (!strcmp(val, "mono"))
1120 variant->spacing_mono = 1;
1121 else if (!strcmp(val, "proportional"))
1122 variant->spacing_mono = 0;
1123 else {
1124 perr(&p, "unknown spacing '%s'", val);
1125 goto err;
1126 }
1127 continue;
1128 }
1129
1130 if (!strcmp(key, "derive")) {
1131 if (strcmp(val, "embolden")) {
1132 perr(&p, "unknown derive '%s'", val);
1133 goto err;
1134 }
1135 variant->derive_embolden = 1;
1136 continue;
1137 }
1138
1139 if (!strcmp(key, "advance")) {
1140 if (parse_int(&p, val, &variant->advance))
1141 goto err;
1142 continue;
1143 }
1144
1145 perr(&p, "unknown key '%s'", key);
1146 goto err;
1147 }
1148
1149 if (in_glyph) {
1150 if (parse_glyph_block(&p, variant, &block))
1151 goto err;
1152 }
1153
1154 block_free(&block);
1155 fclose(f);
1156
1157 return 0;
1158err:
1159 block_free(&block);
1160 fclose(f);
1161 return 1;
1162}
1163
1164struct gpf_font *gpf_font_read(const char *dir, char *err, size_t err_len)
1165{
1166 struct gpf_font *font = gpf_font_new();
1167 unsigned int i;
1168
1169 if (!font) {
1170 snprintf(err, err_len, "out of memory");
1171 return NULL;
1172 }
1173
1174 if (read_family(font, dir, err, err_len))
1175 goto err;
1176
1177 for (i = 0; i < GPF_VARIANTS; i++) {
1178 if (read_variant(font, i, dir, err, err_len))
1179 goto err;
1180 }
1181
1182 return font;
1183err:
1184 gpf_font_free(font);
1185 return NULL;
1186}
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_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
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_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
struct gpf_font * gpf_font_new(void)
Allocates an empty font.
Definition font.c:58
@ 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
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
Native font format.
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
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
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
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