gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
format_bdf.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 <stdarg.h>
9#include <string.h>
10#include <errno.h>
11
12#include <utils/gp_vec.h>
13
14#include "resolve.h"
15#include "format_bdf.h"
16
17#define LINE_MAX_LEN 1024
18
19/* Half of GPF_NAME_MAX, the two are joined into the family name */
20#define BDF_NAME_MAX (GPF_NAME_MAX / 2)
21
22struct bdf_state {
23 struct gpf_font *font;
24 struct gpf_variant *variant;
25
26 char family_name[BDF_NAME_MAX];
27 char weight_name[BDF_NAME_MAX];
28 char registry[BDF_NAME_MAX];
29 char encoding[BDF_NAME_MAX];
30 int spacing_mono;
31 int latin2;
32 /* SIZE says the point size and the resolution it is meant at */
33 int size_pt;
34 int res_y;
35};
36
37/*
38 * The corpus has each font twice, once encoded in ISO10646 and once in
39 * iso8859-2, and the encoding of the latter is not unicode. The table maps
40 * the high half onto unicode.
41 */
42static const uint16_t iso8859_2_to_unicode[] = {
43 0x00a0, 0x0104, 0x02d8, 0x0141, 0x00a4, 0x013d, 0x015a, 0x00a7,
44 0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b,
45 0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7,
46 0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c,
47 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7,
48 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e,
49 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7,
50 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df,
51 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7,
52 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f,
53 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7,
54 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9,
55};
56
57static uint32_t map_code(const struct bdf_state *state, uint32_t code)
58{
59 if (!state->latin2 || code < 0xa0 || code > 0xff)
60 return code;
61
62 return iso8859_2_to_unicode[code - 0xa0];
63}
64
65static char *skip_space(char *str)
66{
67 while (*str == ' ' || *str == '\t')
68 str++;
69
70 return str;
71}
72
73static void strip_eol(char *line)
74{
75 size_t len = strlen(line);
76
77 while (len && (line[len-1] == '\n' || line[len-1] == '\r' ||
78 line[len-1] == ' ' || line[len-1] == '\t'))
79 line[--len] = 0;
80}
81
82/*
83 * Splits a BDF line into a keyword and the rest.
84 */
85static char *split_kw(char *line, char **kw)
86{
87 char *val;
88
89 *kw = skip_space(line);
90
91 val = *kw;
92 while (*val && *val != ' ' && *val != '\t')
93 val++;
94
95 if (*val)
96 *val++ = 0;
97
98 return skip_space(val);
99}
100
101/*
102 * BDF property values are quoted strings, the quotes are not part of them.
103 */
104static void unquote(char *val, char *dst, size_t dst_len)
105{
106 size_t len;
107
108 if (*val == '"')
109 val++;
110
111 snprintf(dst, dst_len, "%s", val);
112
113 len = strlen(dst);
114
115 if (len && dst[len-1] == '"')
116 dst[len-1] = 0;
117}
118
119static int hex_digit(char c)
120{
121 if (c >= '0' && c <= '9')
122 return c - '0';
123
124 if (c >= 'a' && c <= 'f')
125 return c - 'a' + 10;
126
127 if (c >= 'A' && c <= 'F')
128 return c - 'A' + 10;
129
130 return -1;
131}
132
133/*
134 * Reads a single STARTCHAR block. The glyph is added to the variant unless
135 * it has no encoding.
136 */
137static int read_glyph(struct bdf_state *state, FILE *f,
138 char *err, size_t err_len)
139{
140 char line[LINE_MAX_LEN];
141 long code = -1;
142 int advance = 0;
143 int w = 0, h = 0, xoff = 0, yoff = 0;
144 int have_bbx = 0;
145 uint8_t *bits = NULL;
146 unsigned int row = 0;
147 struct gpf_glyph *glyph;
148
149 while (fgets(line, sizeof(line), f)) {
150 char *kw, *val;
151
152 strip_eol(line);
153 val = split_kw(line, &kw);
154
155 if (!strcmp(kw, "ENCODING")) {
156 code = strtol(val, NULL, 10);
157 continue;
158 }
159
160 if (!strcmp(kw, "DWIDTH")) {
161 advance = strtol(val, NULL, 10);
162 continue;
163 }
164
165 if (!strcmp(kw, "BBX")) {
166 if (sscanf(val, "%i %i %i %i", &w, &h, &xoff, &yoff) != 4) {
167 snprintf(err, err_len, "malformed BBX '%s'", val);
168 return 1;
169 }
170 have_bbx = 1;
171 continue;
172 }
173
174 if (!strcmp(kw, "BITMAP")) {
175 if (!have_bbx) {
176 snprintf(err, err_len, "BITMAP without a BBX");
177 return 1;
178 }
179
180 if (w < 0 || h < 0 || w > 255 || h > 255) {
181 snprintf(err, err_len, "BBX out of range");
182 return 1;
183 }
184
185 bits = calloc(1, (size_t)w * h + 1);
186 if (!bits) {
187 snprintf(err, err_len, "out of memory");
188 return 1;
189 }
190
191 continue;
192 }
193
194 if (!strcmp(kw, "ENDCHAR"))
195 break;
196
197 if (!bits)
198 continue;
199
200 /* a bitmap row, two hex digits per byte */
201 if (row < (unsigned int)h) {
202 unsigned int bytes = ((unsigned int)w + 7) / 8;
203 unsigned int i;
204
205 for (i = 0; i < bytes; i++) {
206 int hi = hex_digit(kw[2 * i]);
207 int lo = hex_digit(kw[2 * i + 1]);
208 unsigned int b, x;
209
210 if (hi < 0 || lo < 0) {
211 free(bits);
212 snprintf(err, err_len,
213 "malformed bitmap row '%s'", kw);
214 return 1;
215 }
216
217 b = 16 * hi + lo;
218
219 for (x = 0; x < 8; x++) {
220 unsigned int px = 8 * i + x;
221
222 if (px >= (unsigned int)w)
223 break;
224
225 bits[row * w + px] = !!(b & (0x80 >> x));
226 }
227 }
228 }
229
230 row++;
231 }
232
233 if (code < 0 || !bits) {
234 free(bits);
235 return 0;
236 }
237
238 glyph = gpf_variant_glyph_add(state->variant, map_code(state, code));
239 if (!glyph) {
240 free(bits);
241 snprintf(err, err_len, "out of memory");
242 return 1;
243 }
244
245 glyph->kind = GPF_GLYPH_INK;
246 glyph->has_advance = 1;
247 glyph->has_bearing = 1;
248 glyph->advance = advance;
249
250 if (gpf_glyph_set_bits(glyph, bits, w, h, xoff, h + yoff)) {
251 free(bits);
252 snprintf(err, err_len, "out of memory");
253 return 1;
254 }
255
256 free(bits);
257
258 return 0;
259}
260
261/*
262 * The design square, which BDF says twice and gets wrong at least once per
263 * file: HaxorSquare-8x8 kept the PIXEL_SIZE of the twelve pixel font it was
264 * edited from, HaxorNarrow-16 kept a SIZE of 13, and HaxorNarrow-15 has no
265 * PIXEL_SIZE at all. So take the first one that is not absurd — a design
266 * square is at least the ascent and at most the whole box — and fall back to
267 * the box.
268 */
269static void measure_size(struct bdf_state *state)
270{
271 struct gpf_family_meta *m = &state->font->meta;
272 int box = m->ascent + m->descent;
273 int vals[2] = {m->size, 0};
274 int i;
275
276 if (state->size_pt && state->res_y)
277 vals[1] = (state->size_pt * state->res_y + 36) / 72;
278
279 for (i = 0; i < 2; i++) {
280 if (vals[i] < m->ascent || vals[i] > box)
281 continue;
282
283 m->size = vals[i];
284 return;
285 }
286
287 m->size = box;
288}
289
290/*
291 * The metadata BDF does not have is measured off the glyphs.
292 */
293static void measure(struct gpf_font *font, struct gpf_variant *variant)
294{
295 struct gpf_family_meta *m = &font->meta;
296 size_t i;
297 int max_advance = 0;
298
299 for (i = 0; i < gpf_variant_glyphs(variant); i++) {
300 if (variant->glyphs[i].advance > max_advance)
301 max_advance = variant->glyphs[i].advance;
302 }
303
304 if (variant->spacing_mono)
305 variant->advance = max_advance;
306
307 /*
308 * The em is the size the font is meant to be read as, which is a
309 * decision and not something a BDF knows: the box is a starting point
310 * and the family file is where it is then set. A six pixel font meant
311 * to be shown at twice the size has an em of twelve.
312 */
313 m->em = m->ascent + m->descent;
314
315 gpf_measure(font, variant->id, GPF_MEASURE_X_HEIGHT, &m->x_height);
316 gpf_measure(font, variant->id, GPF_MEASURE_CAP_HEIGHT, &m->cap_height);
317 gpf_measure(font, variant->id, GPF_MEASURE_CH_WIDTH, &m->ch_width);
318
319 m->underline_pos = m->descent > 2 ? -2 : -1;
320 m->underline_thickness = 1;
321
322 m->strike_pos = m->x_height / 2;
323 m->strike_thickness = 1;
324
325 /*
326 * Over the letters and clear of the ascent, which the accents reach —
327 * an overline drawn on them is no overline.
328 */
329 m->overline_pos = m->ascent + 1;
330 m->overline_thickness = 1;
331}
332
333struct gpf_font *gpf_bdf_read(const char *path, char *err, size_t err_len)
334{
335 char line[LINE_MAX_LEN];
336 struct bdf_state state = {0};
337 FILE *f;
338 int len;
339
340 f = fopen(path, "r");
341 if (!f) {
342 snprintf(err, err_len, "%s: %s", path, strerror(errno));
343 return NULL;
344 }
345
346 state.font = gpf_font_new();
347 if (!state.font) {
348 fclose(f);
349 snprintf(err, err_len, "out of memory");
350 return NULL;
351 }
352
353 state.spacing_mono = 1;
354
355 while (fgets(line, sizeof(line), f)) {
356 char *kw, *val;
357
358 strip_eol(line);
359 val = split_kw(line, &kw);
360
361 if (!strcmp(kw, "FAMILY_NAME")) {
362 unquote(val, state.family_name, sizeof(state.family_name));
363 continue;
364 }
365
366 if (!strcmp(kw, "WEIGHT_NAME")) {
367 unquote(val, state.weight_name, sizeof(state.weight_name));
368 continue;
369 }
370
371 if (!strcmp(kw, "CHARSET_REGISTRY")) {
372 unquote(val, state.registry, sizeof(state.registry));
373 continue;
374 }
375
376 if (!strcmp(kw, "CHARSET_ENCODING")) {
377 unquote(val, state.encoding, sizeof(state.encoding));
378 continue;
379 }
380
381 if (!strcmp(kw, "SPACING")) {
382 char spacing[8];
383
384 unquote(val, spacing, sizeof(spacing));
385 state.spacing_mono = (spacing[0] != 'P' && spacing[0] != 'p');
386 continue;
387 }
388
389 if (!strcmp(kw, "PIXEL_SIZE")) {
390 state.font->meta.size = strtol(val, NULL, 10);
391 continue;
392 }
393
394 if (!strcmp(kw, "SIZE")) {
395 char *end;
396
397 state.size_pt = strtol(val, &end, 10);
398 strtol(end, &end, 10);
399 state.res_y = strtol(end, NULL, 10);
400 continue;
401 }
402
403 if (!strcmp(kw, "FONT_ASCENT")) {
404 state.font->meta.ascent = strtol(val, NULL, 10);
405 continue;
406 }
407
408 if (!strcmp(kw, "FONT_DESCENT")) {
409 state.font->meta.descent = strtol(val, NULL, 10);
410 continue;
411 }
412
413 if (!strcmp(kw, "DEFAULT_CHAR")) {
414 long code = strtol(val, NULL, 10);
415
416 if (code > 0)
417 state.font->meta.default_glyph = code;
418 continue;
419 }
420
421 if (!strcmp(kw, "STARTCHAR")) {
422 if (!state.variant) {
423 enum gpf_variant_id id;
424
425 /*
426 * The two properties arrive in either order,
427 * so the encoding is decided once, when the
428 * first glyph shows up.
429 */
430 state.latin2 = !strcasecmp(state.registry, "iso8859") &&
431 state.encoding[0] == '2';
432
433
434 id = state.spacing_mono ? GPF_MONO : GPF_REGULAR;
435
436 state.variant = gpf_font_variant(state.font, id, 1);
437 if (!state.variant) {
438 snprintf(err, err_len, "out of memory");
439 goto err;
440 }
441
442 state.variant->spacing_mono = state.spacing_mono;
443 }
444
445 if (read_glyph(&state, f, err, err_len))
446 goto err;
447
448 continue;
449 }
450 }
451
452 fclose(f);
453
454 if (!state.variant) {
455 snprintf(err, err_len, "%s: no glyphs", path);
456 gpf_font_free(state.font);
457 return NULL;
458 }
459
460 len = snprintf(state.font->meta.family,
461 sizeof(state.font->meta.family), "%s", state.family_name);
462
463 if (state.weight_name[0] && strcmp(state.weight_name, "Medium") &&
464 len > 0 && len < (int)sizeof(state.font->meta.family) - 1) {
465 snprintf(state.font->meta.family + len,
466 sizeof(state.font->meta.family) - len, " %s",
467 state.weight_name);
468 }
469
470 measure(state.font, state.variant);
471 measure_size(&state);
472
473 return state.font;
474err:
475 fclose(f);
476 gpf_font_free(state.font);
477 return NULL;
478}
479
480/*
481 * BDF export. One file is one face, so the caller says which of the four
482 * variants it wants and the lattice resolves it: exporting `bold` writes the
483 * emboldened glyphs out as ink, since a BDF has nowhere to say that a glyph is
484 * derived. What comes back through the importer is a
485 * font with one variant, which is the shape a BDF can hold.
486 *
487 * A bitmap font has no physical size of its own, so the numbers that pretend
488 * it does are picked to agree with each other: at 72 dpi a pixel is a point,
489 * and PIXEL_SIZE, POINT_SIZE and SIZE all say the same thing.
490 */
491#define BDF_DPI 72
492
493#define BDF_PROP_MAX (GPF_NAME_MAX + 32)
494#define BDF_PROPS 24
495
496/*
497 * STARTPROPERTIES says how many property lines follow, so they are collected
498 * and counted rather than printed as they come: a count that disagrees with
499 * the lines is a file no reader will take.
500 */
501static void add_prop(char props[][BDF_PROP_MAX], unsigned int *cnt,
502 const char *fmt, ...)
503{
504 va_list ap;
505
506 if (*cnt >= BDF_PROPS)
507 return;
508
509 va_start(ap, fmt);
510 vsnprintf(props[(*cnt)++], BDF_PROP_MAX, fmt, ap);
511 va_end(ap);
512}
513
514/* '-' separates the XLFD fields, so it cannot be inside one */
515static void xlfd_field(char *dst, size_t len, const char *str)
516{
517 size_t i;
518
519 snprintf(dst, len, "%s", str);
520
521 for (i = 0; dst[i]; i++) {
522 if (dst[i] == '-')
523 dst[i] = ' ';
524 }
525}
526
527/* what the header has to know before a glyph can be written */
528struct bdf_extent {
529 unsigned int cnt;
530 int min_x, max_x, min_y, max_y;
531 long advances;
532 /* SPACING "M" is a promise that every glyph has the same advance */
533 int fixed;
534 int advance;
535};
536
537static void measure_face(struct gpf_font *font, enum gpf_variant_id id,
538 const uint32_t *codes, struct bdf_extent *self)
539{
540 size_t i;
541
542 /* the line box is part of the font box, whatever the ink does */
543 self->fixed = 1;
544 self->min_x = 0;
545 self->max_x = 0;
546 self->min_y = -font->meta.descent;
547 self->max_y = font->meta.ascent;
548
549 for (i = 0; i < gp_vec_len(codes); i++) {
550 struct gpf_resolved res;
551 int x = 0, y = 0;
552
553 if (gpf_resolve(font, id, codes[i], &res))
554 continue;
555
556 x = res.glyph.bearing_x;
557 y = res.glyph.bearing_y - (int)res.glyph.height;
558
559 self->min_x = GPF_MIN(self->min_x, x);
560 self->max_x = GPF_MAX(self->max_x, x + (int)res.glyph.width);
561 self->max_x = GPF_MAX(self->max_x, res.glyph.advance);
562 self->min_y = GPF_MIN(self->min_y, y);
563 self->max_y = GPF_MAX(self->max_y, res.glyph.bearing_y);
564
565 if (!self->cnt)
566 self->advance = res.glyph.advance;
567 else if (res.glyph.advance != self->advance)
568 self->fixed = 0;
569
570 self->advances += res.glyph.advance;
571 self->cnt++;
572
573 gpf_resolved_clear(&res);
574 }
575}
576
577/*
578 * BBX is the ink box against the baseline, so the y offset is the bottom row
579 * and not the bearing. An empty glyph is `BBX 0 0 0 0` and no bitmap rows,
580 * which is what gbdfed writes for a space and what the reader trims back to.
581 */
582static void write_glyph(FILE *f, uint32_t code, const struct gpf_glyph *glyph,
583 int size)
584{
585 unsigned int row_bytes = (glyph->width + 7) / 8;
586 unsigned int x, y, i;
587
588 if (code < 0x10000)
589 fprintf(f, "STARTCHAR uni%04X\n", code);
590 else
591 fprintf(f, "STARTCHAR u%06X\n", code);
592
593 fprintf(f, "ENCODING %u\n", code);
594 fprintf(f, "SWIDTH %i 0\n", glyph->advance * 1000 / size);
595 fprintf(f, "DWIDTH %i 0\n", glyph->advance);
596 fprintf(f, "BBX %u %u %i %i\n", glyph->width, glyph->height,
598 fprintf(f, "BITMAP\n");
599
600 for (y = 0; y < glyph->height; y++) {
601 for (i = 0; i < row_bytes; i++) {
602 unsigned int bits = 0;
603
604 for (x = 0; x < 8; x++) {
605 if (8 * i + x >= glyph->width)
606 break;
607
608 if (gpf_glyph_pixel(glyph, 8 * i + x, y))
609 bits |= 0x80 >> x;
610 }
611
612 fprintf(f, "%02X", bits);
613 }
614
615 fprintf(f, "\n");
616 }
617
618 fprintf(f, "ENDCHAR\n");
619}
620
621int gpf_bdf_write(struct gpf_font *font, enum gpf_variant_id id, FILE *f,
622 char *err, size_t err_len)
623{
624 struct gpf_family_meta *m = &font->meta;
625 struct gpf_variant *variant = font->variants[id];
626 int mono = variant ? variant->spacing_mono
627 : (id == GPF_MONO || id == GPF_BOLD_MONO);
628 int bold = (id == GPF_BOLD || id == GPF_BOLD_MONO);
629 const char *weight = bold ? "Bold" : "Medium";
630 const char *spacing;
631 char props[BDF_PROPS][BDF_PROP_MAX];
632 char family[GPF_NAME_MAX];
633 struct bdf_extent ext = {};
634 unsigned int cnt = 0, i;
635 uint32_t *codes;
636 int size, avg;
637 size_t j;
638
639 codes = gpf_font_codes(font);
640 if (!codes) {
641 snprintf(err, err_len, "out of memory");
642 return 1;
643 }
644
645 measure_face(font, id, codes, &ext);
646
647 if (!ext.cnt) {
648 snprintf(err, err_len, "the %s variant has no glyphs",
649 gpf_variant_name(id));
650 gp_vec_free(codes);
651 return 1;
652 }
653
654 /*
655 * A monospace variant whose advances are not all the one advance is a
656 * lint error, not something to say "M" about: the file describes what
657 * it holds.
658 */
659 spacing = (mono && ext.fixed) ? "M" : "P";
660
661 size = m->size > 0 ? m->size : m->ascent + m->descent;
662
663 /* AVERAGE_WIDTH is in tenths of a pixel */
664 avg = (10 * ext.advances + ext.cnt / 2) / ext.cnt;
665
666 add_prop(props, &cnt, "FOUNDRY \"gpforge\"");
667 add_prop(props, &cnt, "FAMILY_NAME \"%s\"", m->family);
668 add_prop(props, &cnt, "WEIGHT_NAME \"%s\"", weight);
669 add_prop(props, &cnt, "SLANT \"R\"");
670 add_prop(props, &cnt, "SETWIDTH_NAME \"Normal\"");
671 add_prop(props, &cnt, "SPACING \"%s\"", spacing);
672 add_prop(props, &cnt, "CHARSET_REGISTRY \"ISO10646\"");
673 add_prop(props, &cnt, "CHARSET_ENCODING \"1\"");
674 add_prop(props, &cnt, "PIXEL_SIZE %i", size);
675 add_prop(props, &cnt, "POINT_SIZE %i", 10 * size);
676 add_prop(props, &cnt, "RESOLUTION_X %i", BDF_DPI);
677 add_prop(props, &cnt, "RESOLUTION_Y %i", BDF_DPI);
678 add_prop(props, &cnt, "FONT_ASCENT %i", m->ascent);
679 add_prop(props, &cnt, "FONT_DESCENT %i", m->descent);
680 add_prop(props, &cnt, "AVERAGE_WIDTH %i", avg);
681
682 if (m->default_glyph)
683 add_prop(props, &cnt, "DEFAULT_CHAR %u", m->default_glyph);
684
685 xlfd_field(family, sizeof(family), m->family);
686
687 fprintf(f, "STARTFONT 2.1\n");
688 fprintf(f, "COMMENT Generated by gpforge, the %s variant of %s.\n",
689 gpf_variant_name(id), m->family);
690
691 if (m->license[0])
692 fprintf(f, "COMMENT SPDX-License-Identifier: %s\n", m->license);
693
694 fprintf(f, "FONT -gpforge-%s-%s-R-Normal--%i-%i-%i-%i-%s-%i-ISO10646-1\n",
695 family, weight, size, 10 * size, BDF_DPI, BDF_DPI, spacing, avg);
696
697 fprintf(f, "SIZE %i %i %i\n", size, BDF_DPI, BDF_DPI);
698
699 fprintf(f, "FONTBOUNDINGBOX %i %i %i %i\n", ext.max_x - ext.min_x,
700 ext.max_y - ext.min_y, ext.min_x, ext.min_y);
701
702 fprintf(f, "STARTPROPERTIES %u\n", cnt);
703
704 for (i = 0; i < cnt; i++)
705 fprintf(f, "%s\n", props[i]);
706
707 fprintf(f, "ENDPROPERTIES\n");
708
709 fprintf(f, "CHARS %u\n", ext.cnt);
710
711 for (j = 0; j < gp_vec_len(codes); j++) {
712 struct gpf_resolved res;
713
714 if (gpf_resolve(font, id, codes[j], &res))
715 continue;
716
717 write_glyph(f, codes[j], &res.glyph, size);
718
719 gpf_resolved_clear(&res);
720 }
721
722 fprintf(f, "ENDFONT\n");
723
724 gp_vec_free(codes);
725
726 return 0;
727}
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
#define GPF_NAME_MAX
Size of the name buffers, including the terminating zero.
Definition font.h:22
static size_t gpf_variant_glyphs(const struct gpf_variant *self)
Returns the number of glyph entries in a variant.
Definition font.h:411
#define GPF_MAX(a, b)
Returns the larger of two values, evaluates them twice.
Definition font.h:29
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
@ 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
@ GPF_GLYPH_INK
Glyph with ink.
Definition font.h:62
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
#define GPF_MIN(a, b)
Returns the smaller of two values, evaluates them twice.
Definition font.h:27
BDF import, and BDF export.
The glyph variant lattice.
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
Font family meta data.
Definition font.h:230
int descent
Font descent.
Definition font.h:241
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_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
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
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
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