16#include <utils/gp_utf.h>
17#include <utils/gp_vec.h>
18#include <utils/gp_vec_str.h>
23#define LINE_MAX_LEN 1024
25int gpf_code_printable(uint32_t code)
27 if (code < 0x20 || code == 0x7f)
31 if (code >= 0x80 && code < 0xa0)
42static void write_glyph_header(FILE *f,
const struct gpf_glyph *glyph)
46 fprintf(f,
"glyph U+%04X", glyph->
code);
48 if (gpf_code_printable(glyph->
code)) {
49 buf[gp_to_utf8(glyph->
code, buf)] = 0;
50 fprintf(f,
" '%s'", buf);
56static void write_ink(FILE *f,
const struct gpf_glyph *glyph)
60 int rows = top - (bottom < 0 ? bottom : 0);
64 int cols, len, origin, advance, x, y, i;
77 for (y = 0; y < rows; y++) {
78 fputs(y == top - 1 ?
" _|" :
" |", f);
80 for (x = 0; x < cols; x++) {
85 if (ix >= 0 && iy >= 0)
88 fputs(set ?
"##" :
"..", f);
95 advance = glyph->
advance - left;
97 len = 2 * (right > glyph->
advance ? right - left : advance) + 1;
101 for (i = 0; i < len; i++)
102 fputc(i == 2 * origin || i == 2 * advance ?
'^' :
'-', f);
123 write_glyph_header(f, glyph);
125 if (numbers.has_advance)
126 fprintf(f,
"advance %i\n", numbers.advance);
128 if (numbers.has_shift) {
129 fprintf(f,
"shift %i,%i\n",
130 numbers.shift_x, numbers.shift_y);
137 write_glyph_header(f, glyph);
139 switch (glyph->
kind) {
145 fprintf(f,
"advance %i\n", glyph->
advance);
147 fprintf(f,
"shift %i,%i\n",
148 glyph->
shift_x, glyph->shift_y);
152 fprintf(f,
"compose U+%04X U+%04X at %i,%i\n",
159 const char *dir,
char *err,
size_t err_len)
168 f = fopen(path,
"w");
170 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
182 fprintf(f,
"%-9s %s\n",
"spacing",
186 fprintf(f,
"%-9s %s\n",
"derive",
"embolden");
189 fprintf(f,
"%-9s %i\n",
"advance", variant->
advance);
196 write_glyph(f, font, variant->
id, &variant->
glyphs[i]);
199 if (ftell(f) == pos + 1) {
202 if (ftruncate(fileno(f), pos))
205 fseek(f, pos, SEEK_SET);
210 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
217static int write_family(
struct gpf_font *font,
const char *dir,
218 char *err,
size_t err_len)
225 snprintf(path,
sizeof(path),
"%s/family", dir);
227 f = fopen(path,
"w");
229 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
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);
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);
247 fprintf(f,
"%-14s %i %i\n",
"underline",
249 fprintf(f,
"%-14s %i %i\n",
"strikethrough",
251 fprintf(f,
"%-14s %i %i\n",
"overline",
258 fprintf(f,
"%-14s U+%04X\n",
"default_glyph", m->
default_glyph);
261 fprintf(f,
"%-14s %s\n",
"license", m->
license);
266 for (i = 0; i < gp_vec_len(font->
authors); i++) {
267 fprintf(f,
"%-14s %s %s <%s>\n",
"author",
276 if (gp_vec_len(font->
blocks))
279 for (i = 0; i < gp_vec_len(font->
blocks); i++) {
280 fprintf(f,
"%-14s U+%04X U+%04X %s\n",
"block",
286 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
293int gpf_font_write(
struct gpf_font *self,
const char *dir,
294 char *err,
size_t err_len)
298 if (mkdir(dir, 0755) && errno != EEXIST) {
299 snprintf(err, err_len,
"%s: %s", dir, strerror(errno));
303 if (write_family(self, dir, err, err_len))
310 if (write_variant(self, self->
variants[i], dir, err, err_len))
326static int perr(
struct parser *p,
const char *fmt, ...)
328 char msg[GPF_ERR_MAX];
332 vsnprintf(msg,
sizeof(msg), fmt, va);
335 snprintf(p->err, p->err_len,
"%s:%u: %s", p->path, p->line, msg);
340static char *skip_space(
char *str)
342 while (*str ==
' ' || *str ==
'\t')
354static void strip_line(
char *line)
356 size_t len = strlen(line);
357 char *start = skip_space(line);
360 while (len && (line[len-1] ==
'\n' || line[len-1] ==
'\r' ||
361 line[len-1] ==
' ' || line[len-1] ==
'\t'))
364 if (*start ==
'|' || *start ==
'_' || *start ==
'^')
367 for (i = line; *i; i++) {
371 if (i == line || i[-1] ==
' ' || i[-1] ==
'\t') {
373 while (i > line && (i[-1] ==
' ' || i[-1] ==
'\t'))
380static int line_is_comment(
const char *line)
382 const char *str = skip_space((
char *)line);
390static char *split_key(
char *line,
char **key)
394 *key = skip_space(line);
397 while (*val && *val !=
' ' && *val !=
'\t')
403 return skip_space(val);
406static int parse_int(
struct parser *p,
const char *str,
int *val)
412 return perr(p,
"expected a number");
415 res = strtol(str, &end, 10);
418 return perr(p,
"invalid number '%s'", str);
425static int parse_two_ints(
struct parser *p,
char *str,
int *a,
int *b)
429 while (*second && *second !=
' ' && *second !=
'\t')
433 return perr(p,
"expected two numbers");
437 if (parse_int(p, str, a))
440 return parse_int(p, skip_space(second), b);
443static int parse_code(
struct parser *p,
const char *str, uint32_t *code)
448 if (str[0] !=
'U' || str[1] !=
'+')
449 return perr(p,
"expected U+XXXX, got '%s'", str);
452 res = strtoul(str + 2, &end, 16);
454 if (errno || (*end && *end !=
' ' && *end !=
'\t'))
455 return perr(p,
"invalid codepoint '%s'", str);
468 unsigned int first_line;
472static int block_add(
struct block *self,
const char *line)
474 char *str = strdup(line);
480 self->lines = gp_vec_new(0,
sizeof(
char *));
487 if (!GP_VEC_APPEND(self->lines, str)) {
495static void block_free(
struct block *self)
499 for (i = 0; i < gp_vec_len(self->lines); i++)
500 free(self->lines[i]);
502 gp_vec_free(self->lines);
506static int row_ink_start(
struct parser *p,
const char *line,
int *start)
508 const char *bar = strchr(line,
'|');
511 return perr(p,
"ink row without a '|'");
513 *start = bar - line + 1;
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)
524 int start = 0, cols = 0, first, last, left, top;
529 return perr(p,
"ink without a ruler");
532 return perr(p,
"ink without a '_' baseline mark");
534 if (row_ink_start(p, rows[0], &start))
537 cols = (int)strlen(rows[0]) - start - 1;
539 if (cols < 0 || cols % 2)
540 return perr(p,
"malformed ink row");
544 first = strchr(ruler,
'^') - ruler;
545 last = strrchr(ruler,
'^') - ruler;
547 if ((first - start) % 2 || (last - first) % 2)
548 return perr(p,
"ruler is not aligned with the ink");
550 left = -(first - start) / 2;
553 bits = calloc(1, (
size_t)cols * rows_cnt + 1);
555 return perr(p,
"out of memory");
557 for (y = 0; y < rows_cnt; y++) {
560 if (row_ink_start(p, rows[y], &rstart)) {
565 if (rstart != start || (
int)strlen(rows[y]) != start + 2 * cols + 1) {
567 return perr(p,
"ink rows are not aligned");
570 if (rows[y][start + 2 * cols] !=
'|') {
572 return perr(p,
"ink row without a closing '|'");
575 for (x = 0; x < cols; x++) {
576 char a = rows[y][start + 2 * x];
577 char b = rows[y][start + 2 * x + 1];
579 if (a != b || (a !=
'#' && a !=
'.')) {
581 return perr(p,
"expected '##' or '..' in an ink row");
584 bits[y * cols + x] = (a ==
'#');
591 return perr(p,
"out of memory");
597 glyph->
advance = (last - first) / 2;
601 return perr(p,
"out of memory");
609static int parse_glyph_block(
struct parser *parser,
struct gpf_variant *variant,
613 struct parser bp = *parser;
614 struct parser *p = &bp;
623 p->line = block->first_line;
625 for (i = 0; i < gp_vec_len(block->lines); i++) {
626 char *line = block->lines[i];
627 char *start = skip_space(line);
629 if (*start ==
'|' || *start ==
'_') {
631 return perr(p,
"ink too tall");
636 rows[rows_cnt++] = line;
641 if (*start ==
'^' || *start ==
'-') {
643 return perr(p,
"more than one ruler");
652 if (rows_cnt && has_numbers)
653 return perr(p,
"a glyph block mixes ink with numbers");
656 return parse_ink(p, variant, block, rows_cnt, rows, ruler, marker);
659 return perr(p,
"a ruler without ink");
663 return perr(p,
"out of memory");
667 for (i = 0; i < gp_vec_len(block->lines); i++) {
670 p->line = block->first_line + i + 1;
672 val = split_key(block->lines[i], &key);
674 if (!strcmp(key,
"advance")) {
675 if (parse_int(p, val, &glyph->
advance))
681 if (!strcmp(key,
"shift")) {
682 char *comma = strchr(val,
',');
685 return perr(p,
"shift is 'dx,dy'");
689 if (parse_int(p, val, &glyph->
shift_x))
692 if (parse_int(p, skip_space(comma), &glyph->shift_y))
699 if (!strcmp(key,
"compose")) {
704 accent = strchr(val,
' ');
706 return perr(p,
"compose needs a base and an accent");
709 if (parse_code(p, val, &glyph->
base))
714 at = strstr(
accent,
" at ");
716 return perr(p,
"compose needs an 'at dx,dy'");
726 char *comma = strchr(at,
',');
729 return perr(p,
"compose offset is 'dx,dy'");
733 if (parse_int(p, at, &glyph->
dx))
736 if (parse_int(p, skip_space(comma), &glyph->dy))
743 return perr(p,
"unknown key '%s' in a glyph block", key);
749static int append_comment(
char **dst,
const char *line)
754 str = gp_vec_str_new();
761 str = gp_vec_str_append(str, line);
767 str = gp_vec_str_append(str,
"\n");
780static int parse_author(
struct parser *p,
struct gpf_font *font,
char *val)
782 char *name, *email, *end;
785 name = strpbrk(val,
" \t");
787 return perr(p,
"author needs years, a name and an <email>");
790 name = skip_space(name);
792 email = strrchr(name,
'<');
793 end = name + strlen(name) - 1;
795 if (!email || *end !=
'>')
796 return perr(p,
"author needs an <email> at the end");
801 end = name + strlen(name);
802 while (end > name && (end[-1] ==
' ' || end[-1] ==
'\t'))
807 return perr(p,
"%s", msg);
810 return perr(p,
"out of memory");
815static int read_family(
struct gpf_font *font,
const char *dir,
816 char *err,
size_t err_len)
820 char line[LINE_MAX_LEN];
821 struct parser p = {.path = path, .err = err, .err_len = err_len};
825 snprintf(path,
sizeof(path),
"%s/family", dir);
827 f = fopen(path,
"r");
829 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
833 while (fgets(line,
sizeof(line), f)) {
838 if (!seen_key && line_is_comment(line)) {
839 char *nl = strchr(line,
'\n');
844 if (append_comment(&font->
comments, line))
852 if (!*skip_space(line))
857 val = split_key(line, &key);
859 if (!strcmp(key,
"family")) {
864 if (!strcmp(key,
"license")) {
869 if (!strcmp(key,
"size")) {
870 if (parse_int(&p, val, &m->
size))
875 if (!strcmp(key,
"ascent")) {
876 if (parse_int(&p, val, &m->
ascent))
881 if (!strcmp(key,
"descent")) {
882 if (parse_int(&p, val, &m->
descent))
887 if (!strcmp(key,
"line_gap")) {
888 if (parse_int(&p, val, &m->
line_gap))
893 if (!strcmp(key,
"em")) {
894 if (parse_int(&p, val, &m->
em))
899 if (!strcmp(key,
"x_height")) {
900 if (parse_int(&p, val, &m->
x_height))
905 if (!strcmp(key,
"cap_height")) {
911 if (!strcmp(key,
"ch_width")) {
912 if (parse_int(&p, val, &m->
ch_width))
917 if (!strcmp(key,
"underline")) {
924 if (!strcmp(key,
"strikethrough")) {
931 if (!strcmp(key,
"overline")) {
938 if (!strcmp(key,
"block")) {
942 second = strchr(val,
' ');
944 perr(&p,
"block needs a range");
949 second = skip_space(second);
951 name = strchr(second,
' ');
956 if (parse_code(&p, val, &min))
959 if (parse_code(&p, second, &max))
962 if (gpf_font_block_add(font, min, max,
963 name ? skip_space(name) :
"")) {
964 perr(&p,
"out of memory");
971 if (!strcmp(key,
"default_glyph")) {
977 if (!strcmp(key,
"author")) {
978 if (parse_author(&p, font, val))
983 perr(&p,
"unknown key '%s'", key);
996 const char *dir,
char *err,
size_t err_len)
999 char line[LINE_MAX_LEN];
1000 struct parser p = {.path = path, .err = err, .err_len = err_len};
1002 struct block block = {0};
1004 int in_glyph = 0, seen_key = 0;
1006 snprintf(path,
sizeof(path),
"%s/%s", dir, gpf_variant_name(
id));
1008 f = fopen(path,
"r");
1010 if (errno == ENOENT)
1013 snprintf(err, err_len,
"%s: %s", path, strerror(errno));
1017 variant = gpf_font_variant(font,
id, 1);
1020 snprintf(err, err_len,
"out of memory");
1024 while (fgets(line,
sizeof(line), f)) {
1025 char *key, *val, *start;
1029 if (!seen_key && !in_glyph && line_is_comment(line)) {
1030 char *nl = strchr(line,
'\n');
1035 if (append_comment(&variant->
comments, line))
1042 start = skip_space(line);
1046 if (parse_glyph_block(&p, variant, &block))
1055 if (!strncmp(start,
"glyph ", 6)) {
1057 if (parse_glyph_block(&p, variant, &block))
1064 block.first_line = p.line;
1066 if (parse_code(&p, skip_space(start + 6), &block.code))
1073 if (block_add(&block, line)) {
1074 perr(&p,
"out of memory");
1082 val = split_key(line, &key);
1084 if (!strcmp(key,
"variant")) {
1087 if (gpf_variant_by_name(val, &got)) {
1088 perr(&p,
"unknown variant '%s'", val);
1093 perr(&p,
"variant '%s' in a file named '%s'",
1094 val, gpf_variant_name(
id));
1101 if (!strcmp(key,
"parent")) {
1104 if (gpf_variant_by_name(val, &got)) {
1105 perr(&p,
"unknown parent '%s'", val);
1109 if (got != gpf_variant_parent(
id)) {
1110 perr(&p,
"'%s' cannot be a parent of '%s'",
1111 val, gpf_variant_name(
id));
1118 if (!strcmp(key,
"spacing")) {
1119 if (!strcmp(val,
"mono"))
1121 else if (!strcmp(val,
"proportional"))
1124 perr(&p,
"unknown spacing '%s'", val);
1130 if (!strcmp(key,
"derive")) {
1131 if (strcmp(val,
"embolden")) {
1132 perr(&p,
"unknown derive '%s'", val);
1139 if (!strcmp(key,
"advance")) {
1140 if (parse_int(&p, val, &variant->
advance))
1145 perr(&p,
"unknown key '%s'", key);
1150 if (parse_glyph_block(&p, variant, &block))
1164struct gpf_font *gpf_font_read(
const char *dir,
char *err,
size_t err_len)
1170 snprintf(err, err_len,
"out of memory");
1174 if (read_family(font, dir, err, err_len))
1178 if (read_variant(font, i, dir, err, err_len))
const char * gpf_variant_name(enum gpf_variant_id id)
Returns the name of a variant.
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.
void gpf_font_free(struct gpf_font *self)
Frees a font with all its variants and glyphs.
enum gpf_variant_id gpf_variant_parent(enum gpf_variant_id id)
Returns the variant a variant derives its missing glyphs from.
const char * gpf_author_invalid(const char *years, const char *name, const char *email)
Checks the fields of an author.
static size_t gpf_variant_glyphs(const struct gpf_variant *self)
Returns the number of glyph entries in a variant.
gpf_variant_id
Font face variant.
@ GPF_NO_PARENT
What gpf_variant_parent() returns for the base variant.
@ GPF_VARIANTS
Number of font variants.
struct gpf_font * gpf_font_new(void)
Allocates an empty font.
@ GPF_GLYPH_INK
Glyph with ink.
@ GPF_GLYPH_COMPOSE
Glyph is composed from base + accent.
@ GPF_GLYPH_METRICS
Glyph with metrics.
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.
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.
The glyph variant lattice.
gpf_entry_form
The smallest form an entry can be written in on save.
@ GPF_ENTRY_METRICS
The ink is the parent's, only numbers differ.
@ GPF_ENTRY_INK
The ink differs, it has to be saved.
@ GPF_ENTRY_NONE
Identical to what the variant would derive, no entry needed.
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.
char name[64]
The block name.
uint32_t max
The last codepoint of the block, inclusive.
uint32_t min
The first codepoint of the block.
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.
struct gpf_block_decl * blocks
Declared unicode blocks.
char * comments
Font comments.
uint8_t kind
enum gpf_glyph_kind
int bearing_y
Glyph bearing y.
uint8_t has_advance
The entry carries an advance.
int shift_x
How far the inherited ink is moved, positive right and up.
int dx
Accent offset, positive right and up, compose only.
uint8_t has_shift
The entry moves the inherited ink, GPF_GLYPH_METRICS only.
uint32_t base
Base glyph, GPF_GLYPH_COMPOSE only.
uint32_t accent
Accent to be combined with the base, GPF_GLYPH_COMPOSE only.
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.
int advance
Advance for all glyphs.
int derive_embolden
Glyphs not drawn here are emboldened from the parent.
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.
char * comments
Comments above the first glyph block.