gpforge
An advanced bitmap font editor
Loading...
Searching...
No Matches
lint.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
4 */
5
6#include <stdarg.h>
7#include <stdio.h>
8#include <string.h>
9
10#include <utils/gp_vec.h>
11
12#include "resolve.h"
13#include "lint.h"
14
15struct sink {
16 /* a gp_vec, or NULL when only the first finding is wanted */
17 struct gpf_lint_finding **vec;
18 struct gpf_lint_finding *first;
19 int found;
20};
21
22static void emit(struct sink *sink, enum gpf_variant_id id, uint32_t code,
23 enum gpf_lint_severity sev, const char *fmt, ...)
24 __attribute__((format(printf, 5, 6)));
25
26static void emit(struct sink *sink, enum gpf_variant_id id, uint32_t code,
27 enum gpf_lint_severity sev, const char *fmt, ...)
28{
29 struct gpf_lint_finding finding = {.id = id, .code = code, .sev = sev};
30 va_list va;
31
32 if (sink->found && !sink->vec)
33 return;
34
35 va_start(va, fmt);
36 vsnprintf(finding.msg, sizeof(finding.msg), fmt, va);
37 va_end(va);
38
39 sink->found = 1;
40
41 if (sink->first && !sink->first->msg[0])
42 *sink->first = finding;
43
44 if (sink->vec)
45 GP_VEC_APPEND(*sink->vec, finding);
46}
47
48/*
49 * Box drawing, block elements and geometric shapes are cell art: they are
50 * drawn to fill the cell and to touch the neighbouring one, which is the whole
51 * point of them. Geometry rules do not apply.
52 */
53static int cell_art(uint32_t code)
54{
55 return code >= 0x2500 && code <= 0x25ff;
56}
57
58static const char x_letters[] = "acemnorsuvwxz";
59static const char cap_letters[] = "BDEFHIKLMNPRT";
60
61static int in_set(const char *set, uint32_t code)
62{
63 /* strchr() finds the terminator, and U+0000 is not a letter */
64 if (!code || code >= 0x80)
65 return 0;
66
67 return !!strchr(set, (int)code);
68}
69
70/*
71 * A glyph whose ink is the same as its parent's has not been emboldened: the
72 * automatic embolden would have widened it, so this one was drawn by hand and
73 * the hand stopped too early.
74 */
75static int same_ink(const struct gpf_glyph *a, const struct gpf_glyph *b)
76{
77 if (a->width != b->width || a->height != b->height)
78 return 0;
79
80 if (a->bearing_x != b->bearing_x || a->bearing_y != b->bearing_y)
81 return 0;
82
83 if (!a->width)
84 return 1;
85
86 return !memcmp(a->bits, b->bits, (size_t)a->width * a->height);
87}
88
89static void check_bold(struct gpf_font *font, enum gpf_variant_id id,
90 uint32_t code, const struct gpf_glyph *glyph,
91 struct sink *sink)
92{
93 enum gpf_variant_id parent = gpf_variant_parent(id);
94 struct gpf_resolved res;
95
96 if (parent == GPF_NO_PARENT || !glyph->width)
97 return;
98
99 if (gpf_resolve(font, parent, code, &res))
100 return;
101
102 if (same_ink(glyph, &res.glyph)) {
103 emit(sink, id, code, GPF_LINT_WARN, "no wider than %s",
104 gpf_variant_name(parent));
105 }
106
107 gpf_resolved_clear(&res);
108}
109
110static void check_compose(struct gpf_font *font, enum gpf_variant_id id,
111 uint32_t code, const struct gpf_glyph *entry,
112 struct sink *sink)
113{
114 struct gpf_resolved res;
115 uint32_t missing = 0;
116
117 if (gpf_resolve(font, id, entry->base, &res))
118 missing = entry->base;
119 else
120 gpf_resolved_clear(&res);
121
122 if (!missing) {
123 if (gpf_resolve(font, id, entry->accent, &res))
124 missing = entry->accent;
125 else
126 gpf_resolved_clear(&res);
127 }
128
129 if (missing) {
130 emit(sink, id, code, GPF_LINT_ERROR,
131 "composed from U+%04X, which the font has not got", missing);
132 }
133}
134
135/*
136 * Everything that can be said about one glyph in one variant. The editor
137 * calls this for the glyph it is showing, the report calls it for all of them.
138 */
139static void check_glyph(struct gpf_font *font, enum gpf_variant_id id,
140 uint32_t code, struct sink *sink)
141{
142 struct gpf_family_meta *m = &font->meta;
143 struct gpf_variant *variant = font->variants[id];
144 struct gpf_glyph *entry;
145 struct gpf_resolved res;
146 int right, bottom;
147
148 if (!variant)
149 return;
150
151 entry = gpf_variant_glyph(variant, code);
152
153 if (entry && entry->kind == GPF_GLYPH_COMPOSE)
154 check_compose(font, id, code, entry, sink);
155
156 if (gpf_resolve(font, id, code, &res)) {
157 /*
158 * A variant that has an entry for a glyph it cannot resolve is
159 * a glyph the base variant lost: the ink lives in the base and
160 * the overlay is all that is left.
161 */
162 if (entry && entry->kind == GPF_GLYPH_METRICS) {
163 emit(sink, id, code, GPF_LINT_ERROR,
164 "metrics for a glyph %s has not got",
165 gpf_variant_name(GPF_MONO));
166 }
167
168 return;
169 }
170
171 right = res.glyph.bearing_x + (int)res.glyph.width;
172 bottom = res.glyph.bearing_y - (int)res.glyph.height + 1;
173
174 if (res.glyph.width && res.glyph.advance <= 0) {
175 emit(sink, id, code, GPF_LINT_ERROR,
176 "ink with an advance of %i", res.glyph.advance);
177 }
178
179 if (variant->spacing_mono && variant->advance &&
180 res.glyph.advance != variant->advance) {
181 emit(sink, id, code, GPF_LINT_ERROR,
182 "advance %i, the variant's is %i", res.glyph.advance,
183 variant->advance);
184 }
185
186 if (res.glyph.width && !cell_art(code)) {
187 enum gpf_lint_severity sev = variant->spacing_mono
189
190 if (res.glyph.bearing_x < 0) {
191 emit(sink, id, code, sev, "ink starts %i left of the origin",
192 -res.glyph.bearing_x);
193 }
194
195 if (right > res.glyph.advance) {
196 emit(sink, id, code, sev,
197 "ink %i wide in a cell of %i", right,
198 res.glyph.advance);
199 }
200
201 if (res.glyph.bearing_y > m->ascent) {
202 emit(sink, id, code, GPF_LINT_WARN, "ink %i above the ascent",
203 res.glyph.bearing_y - m->ascent);
204 }
205
206 if (bottom < -m->descent) {
207 emit(sink, id, code, GPF_LINT_WARN, "ink %i below the descent",
208 -m->descent - bottom);
209 }
210 }
211
212 if (res.glyph.width && m->x_height && in_set(x_letters, code) &&
213 res.glyph.bearing_y != m->x_height) {
214 emit(sink, id, code, GPF_LINT_WARN, "x-height %i, the font's is %i",
215 res.glyph.bearing_y, m->x_height);
216 }
217
218 if (res.glyph.width && m->cap_height && in_set(cap_letters, code) &&
219 res.glyph.bearing_y != m->cap_height) {
220 emit(sink, id, code, GPF_LINT_WARN,
221 "cap height %i, the font's is %i", res.glyph.bearing_y,
222 m->cap_height);
223 }
224
225 if (entry && entry->kind == GPF_GLYPH_INK &&
226 (id == GPF_BOLD || id == GPF_BOLD_MONO))
227 check_bold(font, id, code, &res.glyph, sink);
228
229 gpf_resolved_clear(&res);
230}
231
232int gpf_lint_glyph(struct gpf_font *font, enum gpf_variant_id id, uint32_t code,
233 struct gpf_lint_finding *out)
234{
235 struct sink sink = {.first = out};
236
237 memset(out, 0, sizeof(*out));
238
239 check_glyph(font, id, code, &sink);
240
241 return !sink.found;
242}
243
244struct gpf_lint_finding *gpf_lint(struct gpf_font *font)
245{
246 struct gpf_lint_finding *vec = gp_vec_new(0, sizeof(struct gpf_lint_finding));
247 struct sink sink = {.vec = &vec};
248 uint32_t *codes = gpf_font_codes(font);
249 unsigned int i;
250 size_t j;
251
252 if (!vec || !codes) {
253 gp_vec_free(codes);
254 return vec;
255 }
256
257 for (i = 0; i < GPF_VARIANTS; i++) {
258 for (j = 0; j < gp_vec_len(codes); j++) {
259 sink.found = 0;
260 check_glyph(font, i, codes[j], &sink);
261 }
262 }
263
264 gp_vec_free(codes);
265
266 return vec;
267}
268
269void gpf_lint_count(struct gpf_font *font, unsigned int *errors,
270 unsigned int *warnings)
271{
272 struct gpf_lint_finding *findings = gpf_lint(font);
273 size_t i;
274
275 *errors = 0;
276 *warnings = 0;
277
278 for (i = 0; findings && i < gp_vec_len(findings); i++) {
279 if (findings[i].sev == GPF_LINT_ERROR)
280 (*errors)++;
281 else
282 (*warnings)++;
283 }
284
285 gp_vec_free(findings);
286}
287
289{
290 switch (sev) {
291 case GPF_LINT_ERROR:
292 return "error";
293 case GPF_LINT_WARN:
294 return "warn";
295 }
296
297 return "?";
298}
gpf_variant_id
Font face variant.
Definition font.h:34
@ GPF_BOLD_MONO
Bold monospace glyphs.
Definition font.h:40
@ GPF_NO_PARENT
What gpf_variant_parent() returns for the base variant.
Definition font.h:46
@ GPF_VARIANTS
Number of font variants.
Definition font.h:44
@ GPF_MONO
Monospace glyphs, the base variant.
Definition font.h:36
@ GPF_BOLD
Bold glyphs.
Definition font.h:42
@ 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
The lint checks.
const char * gpf_lint_severity_name(enum gpf_lint_severity sev)
Returns severity name.
Definition lint.c:288
struct gpf_lint_finding * gpf_lint(struct gpf_font *font)
Runs linter on a font.
Definition lint.c:244
gpf_lint_severity
Linter severity.
Definition lint.h:21
@ GPF_LINT_WARN
Probably a slip. May be deliberate.
Definition lint.h:25
@ GPF_LINT_ERROR
The font renders wrong.
Definition lint.h:23
The glyph variant lattice.
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 cap_height
Capital letter height.
Definition font.h:250
int x_height
The x-height.
Definition font.h:248
int ascent
Font ascent.
Definition font.h:239
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 * bits
Glyph ink.
Definition font.h:145
int bearing_y
Glyph bearing y.
Definition font.h:128
unsigned int height
Glyph ink height.
Definition font.h:137
unsigned int width
Glyph ink width.
Definition font.h:133
int bearing_x
Glyph bearing x.
Definition font.h:122
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
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