12#include <utils/gp_vec.h>
17#define LINE_MAX_LEN 1024
20#define BDF_NAME_MAX (GPF_NAME_MAX / 2)
23 struct gpf_font *font;
24 struct gpf_variant *variant;
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];
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,
57static uint32_t map_code(
const struct bdf_state *state, uint32_t code)
59 if (!state->latin2 || code < 0xa0 || code > 0xff)
62 return iso8859_2_to_unicode[code - 0xa0];
65static char *skip_space(
char *str)
67 while (*str ==
' ' || *str ==
'\t')
73static void strip_eol(
char *line)
75 size_t len = strlen(line);
77 while (len && (line[len-1] ==
'\n' || line[len-1] ==
'\r' ||
78 line[len-1] ==
' ' || line[len-1] ==
'\t'))
85static char *split_kw(
char *line,
char **kw)
89 *kw = skip_space(line);
92 while (*val && *val !=
' ' && *val !=
'\t')
98 return skip_space(val);
104static void unquote(
char *val,
char *dst,
size_t dst_len)
111 snprintf(dst, dst_len,
"%s", val);
115 if (len && dst[len-1] ==
'"')
119static int hex_digit(
char c)
121 if (c >=
'0' && c <=
'9')
124 if (c >=
'a' && c <=
'f')
127 if (c >=
'A' && c <=
'F')
137static int read_glyph(
struct bdf_state *state, FILE *f,
138 char *err,
size_t err_len)
140 char line[LINE_MAX_LEN];
143 int w = 0, h = 0, xoff = 0, yoff = 0;
145 uint8_t *bits = NULL;
146 unsigned int row = 0;
149 while (fgets(line,
sizeof(line), f)) {
153 val = split_kw(line, &kw);
155 if (!strcmp(kw,
"ENCODING")) {
156 code = strtol(val, NULL, 10);
160 if (!strcmp(kw,
"DWIDTH")) {
161 advance = strtol(val, NULL, 10);
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);
174 if (!strcmp(kw,
"BITMAP")) {
176 snprintf(err, err_len,
"BITMAP without a BBX");
180 if (w < 0 || h < 0 || w > 255 || h > 255) {
181 snprintf(err, err_len,
"BBX out of range");
185 bits = calloc(1, (
size_t)w * h + 1);
187 snprintf(err, err_len,
"out of memory");
194 if (!strcmp(kw,
"ENDCHAR"))
201 if (row < (
unsigned int)h) {
202 unsigned int bytes = ((
unsigned int)w + 7) / 8;
205 for (i = 0; i < bytes; i++) {
206 int hi = hex_digit(kw[2 * i]);
207 int lo = hex_digit(kw[2 * i + 1]);
210 if (hi < 0 || lo < 0) {
212 snprintf(err, err_len,
213 "malformed bitmap row '%s'", kw);
219 for (x = 0; x < 8; x++) {
220 unsigned int px = 8 * i + x;
222 if (px >= (
unsigned int)w)
225 bits[row * w + px] = !!(b & (0x80 >> x));
241 snprintf(err, err_len,
"out of memory");
252 snprintf(err, err_len,
"out of memory");
269static void measure_size(
struct bdf_state *state)
273 int vals[2] = {m->
size, 0};
276 if (state->size_pt && state->res_y)
277 vals[1] = (state->size_pt * state->res_y + 36) / 72;
279 for (i = 0; i < 2; i++) {
280 if (vals[i] < m->
ascent || vals[i] > box)
305 variant->
advance = max_advance;
333struct gpf_font *gpf_bdf_read(
const char *path,
char *err,
size_t err_len)
335 char line[LINE_MAX_LEN];
336 struct bdf_state state = {0};
340 f = fopen(path,
"r");
342 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
349 snprintf(err, err_len,
"out of memory");
353 state.spacing_mono = 1;
355 while (fgets(line,
sizeof(line), f)) {
359 val = split_kw(line, &kw);
361 if (!strcmp(kw,
"FAMILY_NAME")) {
362 unquote(val, state.family_name,
sizeof(state.family_name));
366 if (!strcmp(kw,
"WEIGHT_NAME")) {
367 unquote(val, state.weight_name,
sizeof(state.weight_name));
371 if (!strcmp(kw,
"CHARSET_REGISTRY")) {
372 unquote(val, state.registry,
sizeof(state.registry));
376 if (!strcmp(kw,
"CHARSET_ENCODING")) {
377 unquote(val, state.encoding,
sizeof(state.encoding));
381 if (!strcmp(kw,
"SPACING")) {
384 unquote(val, spacing,
sizeof(spacing));
385 state.spacing_mono = (spacing[0] !=
'P' && spacing[0] !=
'p');
389 if (!strcmp(kw,
"PIXEL_SIZE")) {
390 state.font->
meta.
size = strtol(val, NULL, 10);
394 if (!strcmp(kw,
"SIZE")) {
397 state.size_pt = strtol(val, &end, 10);
398 strtol(end, &end, 10);
399 state.res_y = strtol(end, NULL, 10);
403 if (!strcmp(kw,
"FONT_ASCENT")) {
404 state.font->
meta.
ascent = strtol(val, NULL, 10);
408 if (!strcmp(kw,
"FONT_DESCENT")) {
413 if (!strcmp(kw,
"DEFAULT_CHAR")) {
414 long code = strtol(val, NULL, 10);
421 if (!strcmp(kw,
"STARTCHAR")) {
422 if (!state.variant) {
430 state.latin2 = !strcasecmp(state.registry,
"iso8859") &&
431 state.encoding[0] ==
'2';
436 state.variant = gpf_font_variant(state.font,
id, 1);
437 if (!state.variant) {
438 snprintf(err, err_len,
"out of memory");
445 if (read_glyph(&state, f, err, err_len))
454 if (!state.variant) {
455 snprintf(err, err_len,
"%s: no glyphs", path);
456 gpf_font_free(state.font);
461 sizeof(state.font->
meta.
family),
"%s", state.family_name);
463 if (state.weight_name[0] && strcmp(state.weight_name,
"Medium") &&
464 len > 0 && len < (
int)
sizeof(state.font->
meta.
family) - 1) {
470 measure(state.font, state.variant);
471 measure_size(&state);
476 gpf_font_free(state.font);
493#define BDF_PROP_MAX (GPF_NAME_MAX + 32)
501static void add_prop(
char props[][BDF_PROP_MAX],
unsigned int *cnt,
502 const char *fmt, ...)
506 if (*cnt >= BDF_PROPS)
510 vsnprintf(props[(*cnt)++], BDF_PROP_MAX, fmt, ap);
515static void xlfd_field(
char *dst,
size_t len,
const char *str)
519 snprintf(dst, len,
"%s", str);
521 for (i = 0; dst[i]; i++) {
530 int min_x, max_x, min_y, max_y;
538 const uint32_t *codes,
struct bdf_extent *self)
549 for (i = 0; i < gp_vec_len(codes); i++) {
557 y = res.glyph.bearing_y - (int)res.glyph.height;
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);
566 self->advance = res.glyph.advance;
567 else if (res.glyph.advance != self->advance)
570 self->advances += res.glyph.advance;
582static void write_glyph(FILE *f, uint32_t code,
const struct gpf_glyph *
glyph,
585 unsigned int row_bytes = (
glyph->
width + 7) / 8;
586 unsigned int x, y, i;
589 fprintf(f,
"STARTCHAR uni%04X\n", code);
591 fprintf(f,
"STARTCHAR u%06X\n", code);
593 fprintf(f,
"ENCODING %u\n", code);
594 fprintf(f,
"SWIDTH %i 0\n",
glyph->
advance * 1000 / size);
598 fprintf(f,
"BITMAP\n");
601 for (i = 0; i < row_bytes; i++) {
602 unsigned int bits = 0;
604 for (x = 0; x < 8; x++) {
612 fprintf(f,
"%02X", bits);
618 fprintf(f,
"ENDCHAR\n");
622 char *err,
size_t err_len)
629 const char *weight = bold ?
"Bold" :
"Medium";
631 char props[BDF_PROPS][BDF_PROP_MAX];
633 struct bdf_extent ext = {};
634 unsigned int cnt = 0, i;
639 codes = gpf_font_codes(font);
641 snprintf(err, err_len,
"out of memory");
645 measure_face(font,
id, codes, &ext);
648 snprintf(err, err_len,
"the %s variant has no glyphs",
649 gpf_variant_name(
id));
659 spacing = (mono && ext.fixed) ?
"M" :
"P";
664 avg = (10 * ext.advances + ext.cnt / 2) / ext.cnt;
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);
685 xlfd_field(family,
sizeof(family), m->
family);
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);
692 fprintf(f,
"COMMENT SPDX-License-Identifier: %s\n", m->
license);
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);
697 fprintf(f,
"SIZE %i %i %i\n", size, BDF_DPI, BDF_DPI);
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);
702 fprintf(f,
"STARTPROPERTIES %u\n", cnt);
704 for (i = 0; i < cnt; i++)
705 fprintf(f,
"%s\n", props[i]);
707 fprintf(f,
"ENDPROPERTIES\n");
709 fprintf(f,
"CHARS %u\n", ext.cnt);
711 for (j = 0; j < gp_vec_len(codes); j++) {
717 write_glyph(f, codes[j], &res.
glyph, size);
722 fprintf(f,
"ENDFONT\n");
static int gpf_glyph_pixel(const struct gpf_glyph *self, unsigned int x, unsigned int y)
Returns a glyph ink pixel.
struct gpf_glyph * gpf_variant_glyph_add(struct gpf_variant *self, uint32_t code)
Adds a glyph to a variant.
#define GPF_NAME_MAX
Size of the name buffers, including the terminating zero.
static size_t gpf_variant_glyphs(const struct gpf_variant *self)
Returns the number of glyph entries in a variant.
#define GPF_MAX(a, b)
Returns the larger of two values, evaluates them twice.
gpf_variant_id
Font face variant.
@ GPF_BOLD_MONO
Bold monospace glyphs.
@ GPF_MONO
Monospace glyphs, the base variant.
@ GPF_REGULAR
Proportional glyphs, "regular" on disk.
struct gpf_font * gpf_font_new(void)
Allocates an empty font.
@ GPF_GLYPH_INK
Glyph with ink.
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.
#define GPF_MIN(a, b)
Returns the smaller of two values, evaluates them twice.
The glyph variant lattice.
gpf_measure
The family metrics that are measured off the glyphs.
@ GPF_MEASURE_CH_WIDTH
The advance of the '0' glyph.
@ GPF_MEASURE_CAP_HEIGHT
The top of the 'H' ink above the baseline.
@ GPF_MEASURE_X_HEIGHT
The top of the 'x' ink above the baseline.
void gpf_resolved_clear(struct gpf_resolved *self)
Clears the resolved glyph.
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.
struct gpf_variant * variants[GPF_VARIANTS]
Variants indexed by gpf_variant_id, NULL when missing.
struct gpf_family_meta meta
Font meta data.
uint8_t kind
enum gpf_glyph_kind
int bearing_y
Glyph bearing y.
uint8_t has_advance
The entry carries an advance.
uint8_t has_bearing
The entry carries both bearings, see has_advance.
unsigned int height
Glyph ink height.
unsigned int width
Glyph ink width.
int advance
Glyph advance.
int bearing_x
Glyph bearing x.
uint32_t code
Unicode code point.
A resolved glyph and where it was drawn from.
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
int advance
Advance for all glyphs.
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.
int spacing_mono
Every glyph has the same advance.
struct gpf_glyph * glyphs
Glyphs.