9#include <utils/gp_utf.h>
10#include <utils/gp_vec.h>
11#include <text/gp_font.h>
32static const struct range ranges[] = {
33 {0x0020, 0x007f, NULL,
"GP_UCODE_LATIN_BASIC"},
34 {0x00a1, 0x017e,
"latin_ext",
"GP_UCODE_LATIN_SUP | GP_UCODE_LATIN_EXT_A"},
35 {0x0384, 0x03ce,
"greek",
"GP_UCODE_GREEK"},
36 {0x0400, 0x045f,
"cyrilic",
"GP_UCODE_CYRILIC"},
37 {0x2018, 0x2037,
"punctuation",
"GP_UCODE_PUNCTUATION"},
38 {0x2070, 0x208e,
"subsuper",
"GP_UCODE_SUB_SUPER"},
39 {0x2190, 0x21ff,
"arrows",
"GP_UCODE_ARROWS"},
40 {0x2500, 0x257f,
"box",
"GP_UCODE_BOX"},
41 {0x3041, 0x3096,
"hiragana",
"GP_UCODE_HIRAGANA"},
42 {0x30a0, 0x30ff,
"katakana",
"GP_UCODE_KATAKANA"},
53 return "font_regular";
55 return "font_bold_mono";
62 const struct range *range,
const char *what)
65 snprintf(buf, len,
"%s_%s_%s", var_id(
id), range->suffix, what);
67 snprintf(buf, len,
"%s_%s", var_id(
id), what);
75static size_t glyph_size(
const struct gpf_glyph *glyph)
77 size_t rows = ((size_t)glyph->
width + 7) / 8;
79 return (5 + rows * glyph->
height + 3) & ~(size_t)3;
86static uint8_t *glyph_record(
const struct gpf_glyph *glyph)
88 unsigned int row_bytes = (glyph->
width + 7) / 8;
89 uint8_t *rec = calloc(1, glyph_size(glyph));
96 rec[0] = glyph->
width;
102 for (y = 0; y < glyph->
height; y++) {
103 for (x = 0; x < row_bytes; x++) {
104 for (i = 0; i < 8; i++) {
105 if (8 * x + i >= glyph->
width)
109 rec[pos] |= 0x80 >> i;
124 unsigned int variants;
127static void emit_ref(FILE *f,
const struct glyph_ref *ref)
130 const char *sep =
"";
133 utf[gp_to_utf8(ref->code, utf)] = 0;
135 if (ref->code < 0x7f)
136 fprintf(f,
"\t/* '%s'", utf);
138 fprintf(f,
"\t/* 0x%0x '%s'", ref->code, utf);
141 if (!(ref->variants & (1u <<
id)))
144 fprintf(f,
"%s %s", sep, gpf_variant_name(
id));
155static void emit_record(FILE *f,
const struct glyph_ref *refs,
156 const uint8_t *rec,
size_t size)
160 for (i = 0; i < gp_vec_len(refs); i++)
161 emit_ref(f, &refs[i]);
164 fprintf(f,
"\t%2i, %2i, %2i, %2i, %2i,\n", rec[0], rec[1],
165 (int8_t)rec[2], (int8_t)rec[3], rec[4]);
170 for (pos = 5; pos < size; pos++)
171 fprintf(f,
"0x%02x,%s", rec[pos], pos + 1 < size ?
" " :
"\n");
175 const struct range *range)
179 for (code = range->min; code <= range->max; code++) {
202 struct glyph_ref **refs;
210static ssize_t find_record(
const struct glyph_store *store,
211 const uint8_t *rec,
size_t size)
213 size_t i, cnt = gp_vec_len(store->starts);
215 for (i = 0; i < cnt; i++) {
216 size_t start = store->starts[i];
217 size_t end = i + 1 < cnt ? store->starts[i + 1] : gp_vec_len(store->data);
219 if (end - start == size && !memcmp(store->data + start, rec, size))
232static gp_glyph_offset store_record(
struct glyph_store *store,
234 const uint8_t *rec,
size_t size)
236 struct glyph_ref ref = {.code = code, .variants = 1u <<
id};
237 ssize_t i = find_record(store, rec, size);
238 size_t len = gp_vec_len(store->data), j;
239 struct glyph_ref *refs;
243 refs = store->refs[i];
245 for (j = 0; j < gp_vec_len(refs); j++) {
246 if (refs[j].code == code) {
247 refs[j].variants |= ref.variants;
248 return store->starts[i];
252 if (!GP_VEC_APPEND(store->refs[i], ref))
255 return store->starts[i];
258 refs = gp_vec_new(0,
sizeof(
struct glyph_ref));
262 if (!GP_VEC_APPEND(refs, ref) || !GP_VEC_APPEND(store->refs, refs)) {
267 if (!GP_VEC_APPEND(store->starts, len))
270 tmp = gp_vec_expand(store->data, size);
276 memcpy(store->data + len, rec, size);
281static void emit_glyphs(FILE *f,
const struct glyph_store *store)
283 size_t i, cnt = gp_vec_len(store->starts);
285 fprintf(f,
"static uint8_t %s_glyphs[] = {\n", var_id(
GPF_MONO));
287 for (i = 0; i < cnt; i++) {
288 size_t start = store->starts[i];
289 size_t end = i + 1 < cnt ? store->starts[i + 1] : gp_vec_len(store->data);
291 emit_record(f, store->refs[i], store->data + start, end - start);
294 fprintf(f,
"};\n\n");
302static int variant_offsets(
struct gpf_font *font,
304 int *tables, gp_glyph_offset **offsets)
309 for (i = 0; ranges[i].max; i++) {
310 size_t cnt = ranges[i].max - ranges[i].min + 1;
312 tables[i] = range_has_glyphs(font,
id, &ranges[i]);
316 offsets[i] = malloc(cnt *
sizeof(gp_glyph_offset));
320 for (code = ranges[i].min; code <= ranges[i].max; code++) {
321 gp_glyph_offset *off = &offsets[i][code - ranges[i].min];
331 size = glyph_size(&res.glyph);
332 rec = glyph_record(&res.glyph);
338 *off = store_record(store,
id, code, rec, size);
341 if (*off == GP_NOGLYPH)
355 gp_glyph_offset *offsets[][16],
358 size_t size = (ranges[i].max - ranges[i].min + 1) *
sizeof(gp_glyph_offset);
361 for (prev = 0; prev < id; prev++) {
362 if (tables[prev][i] && !memcmp(offsets[prev][i], offsets[
id][i], size))
370 const struct range *range,
371 const gp_glyph_offset *offsets)
373 size_t cnt = range->max - range->min + 1, i;
376 sym(name,
sizeof(name),
id, range,
"offsets");
378 fprintf(f,
"static gp_glyph_offset %s[] = {", name);
380 for (i = 0; i < cnt; i++) {
384 if (offsets[i] == GP_NOGLYPH)
385 fprintf(f,
"GP_NOGLYPH,");
387 fprintf(f,
" 0x%04x,", offsets[i]);
393 fprintf(f,
"\n};\n\n");
397 const char *font_id,
const int *tables,
404 unsigned int max_width = 0, max_advance = 0, cnt = 0, i;
410 for (i = 0; ranges[i].max; i++) {
416 for (code = ranges[i].min; code <= ranges[i].max; code++) {
425 if (width > max_width)
428 if ((
unsigned int)res.glyph.advance > max_advance)
429 max_advance = res.glyph.advance;
435 fprintf(f,
"static struct gp_font_face %s = {\n", var_id(
id));
438 fprintf(f,
"\t.family_name = \"%s\",\n", font_id);
440 fprintf(f,
"\t.family_name = \"%s_%s\",\n", font_id, var_id(
id) + 5);
442 fprintf(f,
"\t.style = %s%s,\n", mono ?
"GP_FONT_MONO" :
"GP_FONT_REGULAR",
443 bold ?
" | GP_FONT_BOLD" :
"");
445 fprintf(f,
"\t.ascent = %i,\n", m->
ascent);
446 fprintf(f,
"\t.descent = %i,\n", m->
descent);
447 fprintf(f,
"\t.max_glyph_width = %u,\n", max_width);
448 fprintf(f,
"\t.max_glyph_advance = %u,\n", max_advance);
451 fprintf(f,
"\t.em = %i,\n", m->
em);
453 fprintf(f,
"\t.x_height = %i,\n", m->
x_height);
455 fprintf(f,
"\t.cap_height = %i,\n", m->
cap_height);
457 fprintf(f,
"\t.ch_width = %i,\n", m->
ch_width);
459 fprintf(f,
"\t.line_gap = %i,\n", m->
line_gap);
465 fprintf(f,
"\t.strike_pos = %i,\n", m->
strike_pos);
473 fprintf(f,
"\t.glyph_bitmap_format = GP_FONT_BITMAP_1BPP,\n");
474 fprintf(f,
"\t.glyph_tables = %u,\n", cnt);
475 fprintf(f,
"\t.glyphs = {\n");
477 for (i = 0; ranges[i].max; i++) {
481 fprintf(f,
"\t\t{\n");
483 fprintf(f,
"\t\t\t.glyphs = %s_glyphs,\n", var_id(
GPF_MONO));
485 sym(name,
sizeof(name), owners[i], &ranges[i],
"offsets");
486 fprintf(f,
"\t\t\t.offsets = %s,\n", name);
488 fprintf(f,
"\t\t\t.min_glyph = 0x%04x,\n", ranges[i].min);
489 fprintf(f,
"\t\t\t.max_glyph = 0x%04x,\n", ranges[i].max);
490 fprintf(f,
"\t\t},\n");
493 fprintf(f,
"\t}\n};\n\n");
511static void count_dropped(
struct gpf_font *font,
unsigned int *kept,
512 unsigned int *dropped)
514 uint32_t *codes = gpf_font_codes(font);
521 for (i = 0; i < gp_vec_len(codes); i++) {
524 for (j = 0; ranges[j].max; j++) {
525 if (codes[i] >= ranges[j].min && codes[i] <= ranges[j].max)
538int gpf_export_c(
struct gpf_font *font,
const char *font_id,
const char *name,
539 FILE *f,
char *err,
size_t err_len)
544 struct glyph_store store = {
545 .data = gp_vec_new(0, 1),
546 .starts = gp_vec_new(0,
sizeof(
size_t)),
547 .refs = gp_vec_new(0,
sizeof(
struct glyph_ref *)),
549 char blocks[512] =
"";
550 unsigned int kept = 0, dropped = 0;
552 int faces = 0, ret = 1;
555 snprintf(err, err_len,
"the font has no mono variant to export");
559 if (!store.data || !store.starts || !store.refs)
563 fprintf(f,
"// SPDX-License-Identifier: %s\n", font->
meta.
license);
567 for (i = 0; i < gp_vec_len(font->
authors); i++) {
568 fprintf(f,
" * Copyright (C) %s %s <%s>\n",
576 fprintf(f,
"/* Generated file, do not touch */\n\n");
577 fprintf(f,
"#include <text/gp_font.h>\n\n");
580 if (!face_wanted(font,
id))
583 if (variant_offsets(font,
id, &store, tables[
id], offsets[
id]))
587 emit_glyphs(f, &store);
590 for (i = 0; ranges[i].max; i++) {
594 owners[id][i] = offsets_owner(tables, offsets,
id, i);
596 if (owners[
id][i] ==
id)
597 emit_offsets(f,
id, &ranges[i], offsets[
id][i]);
602 if (!face_wanted(font,
id))
605 emit_face(f, font,
id, font_id, tables[
id], owners[
id]);
611 for (i = 0; ranges[i].max; i++) {
616 strncat(blocks,
" | ",
sizeof(blocks) - strlen(blocks) - 1);
618 strncat(blocks, ranges[i].ucode,
sizeof(blocks) - strlen(blocks) - 1);
621 fprintf(f,
"const gp_font_family __attribute__((visibility (\"hidden\")))"
622 " font_family_%s = {\n", font_id);
623 fprintf(f,
"\t.family_name = \"%s\",\n", name);
624 fprintf(f,
"\t.ucode_blocks = %s,\n", blocks);
625 fprintf(f,
"\t.fonts = {\n");
628 if (!face_wanted(font,
id))
631 fprintf(f,
"\t\t&%s,\n", var_id(
id));
634 fprintf(f,
"\t\tNULL\n\t}\n};\n");
636 count_dropped(font, &kept, &dropped);
638 fprintf(stderr,
"%s: %u glyphs, %u faces", name, kept, faces);
641 fprintf(stderr,
", %u outside the exported ranges dropped",
645 fprintf(stderr,
"\n");
650 snprintf(err, err_len,
"out of memory");
653 for (i = 0; ranges[i].max; i++)
654 free(offsets[
id][i]);
657 for (i = 0; store.refs && i < gp_vec_len(store.refs); i++)
658 gp_vec_free(store.refs[i]);
660 gp_vec_free(store.refs);
661 gp_vec_free(store.starts);
662 gp_vec_free(store.data);
static int gpf_glyph_pixel(const struct gpf_glyph *self, unsigned int x, unsigned int y)
Returns a glyph ink pixel.
gpf_variant_id
Font face variant.
@ GPF_BOLD_MONO
Bold monospace glyphs.
@ GPF_VARIANTS
Number of font variants.
@ GPF_MONO
Monospace glyphs, the base variant.
@ GPF_REGULAR
Proportional glyphs, "regular" on disk.
The glyph variant lattice.
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.
char years[32]
Copyright years, e.g. "2024", "2019-2024" or "2019,2023".
char email[64]
The author's email, without the angle brackets.
char name[64]
The author's name.
struct gpf_author * authors
Font authors.
struct gpf_variant * variants[GPF_VARIANTS]
Variants indexed by gpf_variant_id, NULL when missing.
struct gpf_family_meta meta
Font meta data.
int bearing_y
Glyph bearing y.
unsigned int height
Glyph ink height.
unsigned int width
Glyph ink width.
int advance
Glyph advance.
int bearing_x
Glyph bearing x.
A resolved glyph and where it was drawn from.
struct gpf_glyph glyph
Freshly allocated glyph with copied/generated ink.
enum gpf_variant_id id
Font variant e.g. GPF_REGULAR, GPF_MONO, etc.