gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
gpforge-cli.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5/*
6 * The command line half of gpforge: BDF import and export, the C export, and
7 * a canonical dump used to compare an imported font against a font loaded
8 * back from a directory.
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/stat.h>
15
16#include "font.h"
17#include "resolve.h"
18#include "lint.h"
19#include "format.h"
20#include "format_bdf.h"
21#include "format_c.h"
22
23
24static int is_dir(const char *path)
25{
26 struct stat st;
27
28 if (stat(path, &st))
29 return 0;
30
31 return S_ISDIR(st.st_mode);
32}
33
34static struct gpf_font *load(const char *path, char *err, size_t err_len)
35{
36 if (is_dir(path))
37 return gpf_font_read(path, err, err_len);
38
39 return gpf_bdf_read(path, err, err_len);
40}
41
42static void dump_glyph(const struct gpf_glyph *glyph)
43{
44 unsigned int x, y;
45
46 printf("U+%04X ", glyph->code);
47
48 switch (glyph->kind) {
49 case GPF_GLYPH_INK:
50 printf("ink adv %i brg %i,%i size %ux%u\n",
51 glyph->advance, glyph->bearing_x, glyph->bearing_y,
52 glyph->width, glyph->height);
53
54 for (y = 0; y < glyph->height; y++) {
55 printf("\t");
56 for (x = 0; x < glyph->width; x++)
57 putchar(gpf_glyph_pixel(glyph, x, y) ? '#' : '.');
58 putchar('\n');
59 }
60 break;
62 printf("metrics");
63 if (glyph->has_advance)
64 printf(" adv %i", glyph->advance);
65 if (glyph->has_shift)
66 printf(" shift %i,%i", glyph->shift_x, glyph->shift_y);
67 putchar('\n');
68 break;
70 printf("compose U+%04X U+%04X at %i,%i\n",
71 glyph->base, glyph->accent, glyph->dx, glyph->dy);
72 break;
73 }
74}
75
76static void dump(struct gpf_font *font)
77{
78 struct gpf_family_meta *m = &font->meta;
79 unsigned int i;
80 size_t j;
81
82 printf("family %s\n", m->family);
83 printf("size %i ascent %i descent %i line_gap %i\n",
84 m->size, m->ascent, m->descent, m->line_gap);
85 printf("em %i x_height %i cap_height %i ch_width %i\n",
86 m->em, m->x_height, m->cap_height, m->ch_width);
87 printf("underline %i %i strikethrough %i %i overline %i %i\n",
91 printf("default_glyph U+%04X\n", m->default_glyph);
92 printf("license %s\n", m->license);
93
94 for (j = 0; j < gp_vec_len(font->authors); j++) {
95 printf("author %s %s <%s>\n", font->authors[j].years,
96 font->authors[j].name, font->authors[j].email);
97 }
98
99 for (i = 0; i < GPF_VARIANTS; i++) {
100 struct gpf_variant *variant = font->variants[i];
101
102 if (!variant)
103 continue;
104
105 printf("variant %s spacing %s derive %s advance %i glyphs %zu\n",
106 gpf_variant_name(variant->id),
107 variant->spacing_mono ? "mono" : "proportional",
108 variant->derive_embolden ? "embolden" : "none",
109 variant->advance, gpf_variant_glyphs(variant));
110
111 for (j = 0; j < gpf_variant_glyphs(variant); j++)
112 dump_glyph(&variant->glyphs[j]);
113 }
114}
115
116/*
117 * Dumps a resolved face, which is what a consumer of the font would see.
118 */
119static int face(struct gpf_font *font, const char *name)
120{
121 enum gpf_variant_id id;
122 uint32_t *vec;
123 size_t i;
124
125 if (gpf_variant_by_name(name, &id)) {
126 fprintf(stderr, "unknown variant '%s'\n", name);
127 return 1;
128 }
129
130 vec = gpf_font_codes(font);
131 if (!vec)
132 return 1;
133
134 for (i = 0; i < gp_vec_len(vec); i++) {
135 struct gpf_resolved res;
136 unsigned int x, y;
137
138 if (gpf_resolve(font, id, vec[i], &res))
139 continue;
140
141 printf("U+%04X %s adv %i brg %i,%i size %ux%u\n",
142 vec[i], gpf_provenance_name(res.prov),
143 res.glyph.advance, res.glyph.bearing_x,
144 res.glyph.bearing_y, res.glyph.width, res.glyph.height);
145
146 for (y = 0; y < res.glyph.height; y++) {
147 printf("\t");
148 for (x = 0; x < res.glyph.width; x++)
149 putchar(gpf_glyph_pixel(&res.glyph, x, y) ? '#' : '.');
150 putchar('\n');
151 }
152
153 gpf_resolved_clear(&res);
154 }
155
156 gp_vec_free(vec);
157
158 return 0;
159}
160
161static void usage(const char *name)
162{
163 fprintf(stderr,
164 "usage: %s import <font.bdf> <dir>\n"
165 " %s write <dir> <out-dir>\n"
166 " %s dump <font.bdf|dir>\n"
167 " %s face <font.bdf|dir> <variant>\n"
168 " %s lint <font.bdf|dir>\n"
169 " %s c <font.bdf|dir> <font_id> <family-name>\n"
170 " %s bdf <font.bdf|dir> <variant>\n",
171 name, name, name, name, name, name, name);
172}
173
174int main(int argc, char *argv[])
175{
176 char err[GPF_ERR_MAX] = "";
177 struct gpf_font *font;
178
179 if (argc < 3) {
180 usage(argv[0]);
181 return 1;
182 }
183
184 if (!strcmp(argv[1], "dump")) {
185 font = load(argv[2], err, sizeof(err));
186 if (!font) {
187 fprintf(stderr, "%s\n", err);
188 return 1;
189 }
190
191 dump(font);
192 gpf_font_free(font);
193
194 return 0;
195 }
196
197 if (!strcmp(argv[1], "c")) {
198 if (argc < 5) {
199 usage(argv[0]);
200 return 1;
201 }
202
203 font = load(argv[2], err, sizeof(err));
204 if (!font) {
205 fprintf(stderr, "%s\n", err);
206 return 1;
207 }
208
209 if (gpf_export_c(font, argv[3], argv[4], stdout, err, sizeof(err))) {
210 fprintf(stderr, "%s\n", err);
211 gpf_font_free(font);
212 return 1;
213 }
214
215 gpf_font_free(font);
216
217 return 0;
218 }
219
220 if (!strcmp(argv[1], "bdf")) {
221 enum gpf_variant_id id;
222
223 if (argc < 4) {
224 usage(argv[0]);
225 return 1;
226 }
227
228 if (gpf_variant_by_name(argv[3], &id)) {
229 fprintf(stderr, "unknown variant '%s'\n", argv[3]);
230 return 1;
231 }
232
233 font = load(argv[2], err, sizeof(err));
234 if (!font) {
235 fprintf(stderr, "%s\n", err);
236 return 1;
237 }
238
239 if (gpf_bdf_write(font, id, stdout, err, sizeof(err))) {
240 fprintf(stderr, "%s\n", err);
241 gpf_font_free(font);
242 return 1;
243 }
244
245 gpf_font_free(font);
246
247 return 0;
248 }
249
250 if (!strcmp(argv[1], "lint")) {
251 struct gpf_lint_finding *findings;
252 unsigned int errors = 0, warns = 0;
253 size_t i;
254
255 font = load(argv[2], err, sizeof(err));
256 if (!font) {
257 fprintf(stderr, "%s\n", err);
258 return 1;
259 }
260
261 findings = gpf_lint(font);
262
263 for (i = 0; findings && i < gp_vec_len(findings); i++) {
264 struct gpf_lint_finding *f = &findings[i];
265
266 if (f->sev == GPF_LINT_ERROR)
267 errors++;
268 else
269 warns++;
270
271 printf("%-10s U+%04X %-5s %s\n",
272 gpf_variant_name(f->id), f->code,
273 gpf_lint_severity_name(f->sev), f->msg);
274 }
275
276 printf("%u errors, %u warnings\n", errors, warns);
277
278 gp_vec_free(findings);
279 gpf_font_free(font);
280
281 return errors ? 1 : 0;
282 }
283
284 if (!strcmp(argv[1], "face")) {
285 int ret;
286
287 if (argc < 4) {
288 usage(argv[0]);
289 return 1;
290 }
291
292 font = load(argv[2], err, sizeof(err));
293 if (!font) {
294 fprintf(stderr, "%s\n", err);
295 return 1;
296 }
297
298 ret = face(font, argv[3]);
299
300 gpf_font_free(font);
301
302 return ret;
303 }
304
305 if (!strcmp(argv[1], "import") || !strcmp(argv[1], "write")) {
306 if (argc < 4) {
307 usage(argv[0]);
308 return 1;
309 }
310
311 font = load(argv[2], err, sizeof(err));
312 if (!font) {
313 fprintf(stderr, "%s\n", err);
314 return 1;
315 }
316
317 if (gpf_font_write(font, argv[3], err, sizeof(err))) {
318 fprintf(stderr, "%s\n", err);
319 gpf_font_free(font);
320 return 1;
321 }
322
323 gpf_font_free(font);
324
325 return 0;
326 }
327
328 usage(argv[0]);
329
330 return 1;
331}
The in-memory font model.
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
int gpf_variant_by_name(const char *name, enum gpf_variant_id *id)
Looks up a variant by its on-disk name.
Definition font.c:31
void gpf_font_free(struct gpf_font *self)
Frees a font with all its variants and glyphs.
Definition font.c:159
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_VARIANTS
Number of font variants.
Definition font.h:44
uint32_t * gpf_font_codes(struct gpf_font *self)
Returns every codepoint the font mentions, in any variant.
Definition font.c:317
@ 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
Native font format.
BDF import, and BDF export.
The gfxprim C font export.
The lint checks.
struct gpf_lint_finding * gpf_lint(struct gpf_font *font)
Runs linter on a font.
Definition lint.c:244
@ GPF_LINT_ERROR
The font renders wrong.
Definition lint.h:23
The glyph variant lattice.
const char * gpf_provenance_name(enum gpf_provenance prov)
Returns provenance name.
Definition resolve.c:25
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
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
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
A glyph.
Definition font.h:80
uint8_t kind
enum gpf_glyph_kind
Definition font.h:88
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
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 linter finding.
Definition lint.h:29
uint32_t code
A unicode code point the finding is for.
Definition lint.h:33
enum gpf_lint_severity sev
Severity.
Definition lint.h:35
enum gpf_variant_id id
Variant of the glyph the finding is for.
Definition lint.h:31
char msg[72]
Linter message.
Definition lint.h:37
A resolved glyph and where it was drawn from.
Definition resolve.h:36
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