Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/t1lint/t1lint.cc

raw
1 /* t1lint.cc -- driver for checking Type 1 fonts for validity
2 *
3 * Copyright (c) 1999-2023 Eddie Kohler
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version. This program is distributed in the hope that it will be
9 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include <efont/psres.hh>
18 #include <efont/t1rw.hh>
19 #include <efont/t1font.hh>
20 #include <efont/t1item.hh>
21 #include <efont/t1mm.hh>
22 #include "cscheck.hh"
23 #include <lcdf/clp.h>
24 #include <lcdf/error.hh>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #ifdef HAVE_CTIME
32 # include <time.h>
33 #endif
34 #if defined(_MSDOS) || defined(_WIN32)
35 # include <fcntl.h>
36 # include <io.h>
37 #endif
38
39 using namespace Efont;
40
41 #define VERSION_OPT 301
42 #define HELP_OPT 302
43 #define QUIET_OPT 303
44
45 const Clp_Option options[] = {
46 { "help", 'h', HELP_OPT, 0, 0 },
47 { "quiet", 'q', QUIET_OPT, 0, Clp_Negate },
48 { "version", 0, VERSION_OPT, 0, 0 },
49 };
50
51
52 static const char *program_name;
53
54
55 void
56 usage_error(ErrorHandler *errh, const char *error_message, ...)
57 {
58 va_list val;
59 va_start(val, error_message);
60 if (!error_message)
61 errh->message("Usage: %s [OPTION]... FONT", program_name);
62 else
63 errh->xmessage(ErrorHandler::e_error, error_message, val);
64 errh->message("Type %s --help for more information.", program_name);
65 exit(1);
66 va_end(val);
67 }
68
69 void
70 usage()
71 {
72 FileErrorHandler uerrh(stdout);
73 uerrh.message("\
74 %<T1lint%> checks a PostScript Type 1 font for correctness. Any errors or\n\
75 divergences from the specification are reported to standard error. Nothing is\n\
76 printed for correct fonts. The command exits with status 0 if there are no\n\
77 errors, and status 1 otherwise.\n\
78 \n\
79 Usage: %s [OPTION]... FONT [...]\n\
80 \n\
81 Each FONT is the name of a PFA or PFB font file.\n\
82 \n\
83 Options:\n\
84 -h, --help Print this message and exit.\n\
85 -q, --quiet Do not report errors to standard error.\n\
86 --version Print version number and exit.\n\
87 \n\
88 Report bugs to <[email protected]>.\n", program_name);
89 }
90
91
92 /*****
93 * CHECKS
94 **/
95
96 static bool
97 get_num_array(Type1Font *font, int dictionary, const char *name,
98 Vector<double> &v, ErrorHandler *errh, bool mandatory = false)
99 {
100 if (Type1Definition *d = font->dict(dictionary, name)) {
101 if (d->value_numvec(v))
102 return true;
103 errh->error("%s not an array of numbers", name);
104 v.clear();
105 } else if (mandatory)
106 errh->error("%s not defined", name);
107 return false;
108 }
109
110 static bool
111 get_integer(Type1Font *font, int dictionary, const char *name,
112 int &v, ErrorHandler *errh, bool mandatory = false)
113 {
114 Type1Definition *d = font->dict(dictionary, name);
115 double scratch;
116 if (d && d->value_int(v))
117 return true;
118 else if (d && d->value_num(scratch)) {
119 errh->warning("%s not an integer", name);
120 v = (int)scratch;
121 return true;
122 } else if (d)
123 errh->error("%s not a number", name);
124 else if (mandatory)
125 errh->error("%s not defined", name);
126 return false;
127 }
128
129 // BLUES
130
131 static void
132 check_blue_array(Vector<double> &blues, const char *name, double BlueScale,
133 ErrorHandler *errh)
134 {
135 if (blues.size() % 2 != 0) {
136 errh->error("%s has an odd number of entries", name);
137 blues.push_back(blues.back());
138 }
139
140 for (int i = 0; i < blues.size(); i++)
141 if (blues[i] != (double)((int)blues[i])) {
142 errh->warning("some %s entries are not integers", name);
143 break;
144 }
145
146 double max_diff = 1 / BlueScale;
147 for (int i = 0; i < blues.size(); i += 2) {
148 if (blues[i] > blues[i+1])
149 errh->error("%s zone %d in the wrong order", name, i/2);
150 else if (blues[i+1] - blues[i] >= max_diff)
151 errh->error("%s zone %d too large in relation to BlueScale (size %g, max %g [%g])", name, i/2, blues[i+1] - blues[i], max_diff, BlueScale);
152 }
153 }
154
155 static void
156 check_blue_overlap(Vector<double> &bl1, const char *name1,
157 Vector<double> &bl2, const char *name2, int BlueFuzz,
158 ErrorHandler *errh)
159 {
160 int min_fuzz = 2*BlueFuzz + 1;
161 for (int i = 2; i < bl1.size(); i += 2) {
162 int thresh = (&bl1 == &bl2 ? i : bl2.size());
163 for (int j = 0; j < thresh; j += 2) {
164 if ((bl2[j] >= bl1[i] && bl2[j] <= bl1[i+1])
165 || (bl2[j+1] >= bl1[i] && bl2[j+1] <= bl1[i+1]))
166 errh->error("%s zone %d and %s zone %d overlap",
167 name1, i/2, name2, j/2);
168 else if ((bl2[j] >= bl1[i+1] && bl2[j] < bl1[i+1]+min_fuzz)
169 || (bl2[j+1] <= bl1[i] && bl2[j+1]+min_fuzz > bl1[i]))
170 errh->error("%s zone %d and %s zone %d overlap within BlueFuzz",
171 name1, i/2, name2, j/2);
172 }
173 }
174 }
175
176 static void
177 check_blues(Type1Font *font, ErrorHandler *errh)
178 {
179 Type1Definition *d;
180
181 // BlueScale
182 double BlueScale;
183 bool ok = false;
184 if ((d = font->p_dict("BlueScale"))) {
185 if (!d->value_num(BlueScale))
186 errh->error("BlueScale not a real number");
187 else if (BlueScale <= 0)
188 errh->error("BlueScale less than or equal to 0");
189 else {
190 if (BlueScale > 0.5)
191 errh->error("suspiciously large BlueScale (%g)", BlueScale);
192 ok = true;
193 }
194 }
195 if (!ok) BlueScale = 0.039625;
196
197 // BlueShift
198 int BlueShift = -1;
199 if (get_integer(font, Type1Font::dP, "BlueShift", BlueShift, errh)
200 && BlueShift < 0)
201 errh->error("BlueShift less than 0");
202 if (BlueShift < 0)
203 BlueShift = 7;
204
205 // BlueFuzz
206 int BlueFuzz = -1;
207 if (get_integer(font, Type1Font::dP, "BlueFuzz", BlueFuzz, errh)
208 && BlueFuzz < 0)
209 errh->error("BlueFuzz less than 0");
210 if (BlueFuzz < 0)
211 BlueFuzz = 1;
212 else if (BlueFuzz > 10)
213 errh->warning("suspiciously large BlueFuzz (%d)", BlueFuzz);
214
215 // BlueValues
216 Vector<double> BlueValues, OtherBlues;
217 get_num_array(font, Type1Font::dP, "BlueValues", BlueValues, errh, true);
218 get_num_array(font, Type1Font::dP, "OtherBlues", OtherBlues, errh);
219
220 check_blue_array(BlueValues, "BlueValues", BlueScale, errh);
221 if (BlueValues.size() > 14)
222 errh->error("too many zones in BlueValues (max 7)");
223 check_blue_array(OtherBlues, "OtherBlues", BlueScale, errh);
224 if (OtherBlues.size() > 10)
225 errh->error("too many zones in OtherBlues (max 5)");
226
227 check_blue_overlap(BlueValues, "BlueValues", BlueValues, "BlueValues", BlueFuzz, errh);
228 check_blue_overlap(OtherBlues, "OtherBlues", OtherBlues, "OtherBlues", BlueFuzz, errh);
229 check_blue_overlap(BlueValues, "BlueValues", OtherBlues, "OtherBlues", BlueFuzz, errh);
230
231 // FamilyBlues
232 Vector<double> FamilyBlues, FamilyOtherBlues;
233 get_num_array(font, Type1Font::dP, "FamilyBlues", FamilyBlues, errh);
234 get_num_array(font, Type1Font::dP, "FamilyOtherBlues", FamilyOtherBlues, errh);
235
236 check_blue_array(FamilyBlues, "FamilyBlues", BlueScale, errh);
237 if (FamilyBlues.size() > 14)
238 errh->error("too many zones in FamilyBlues (max 7)");
239 check_blue_array(FamilyOtherBlues, "FamilyOtherBlues", BlueScale, errh);
240 if (FamilyOtherBlues.size() > 10)
241 errh->error("too many zones in FamilyOtherBlues (max 5)");
242
243 check_blue_overlap(FamilyBlues, "FamilyBlues", FamilyBlues, "FamilyBlues", BlueFuzz, errh);
244 check_blue_overlap(FamilyOtherBlues, "FamilyOtherBlues", FamilyOtherBlues, "FamilyOtherBlues", BlueFuzz, errh);
245 check_blue_overlap(FamilyBlues, "FamilyBlues", FamilyOtherBlues, "FamilyOtherBlues", BlueFuzz, errh);
246 }
247
248
249 // STEMS
250
251 void
252 check_stem_snap(Vector<double> &stem_snap, double main, bool is_v,
253 ErrorHandler *errh)
254 {
255 const char *name = (is_v ? "V" : "H");
256 if (stem_snap.size() > 12)
257 errh->error("StemSnap%s has more than 12 entries", name);
258 for (int i = 0; i < stem_snap.size() - 1; i++)
259 if (stem_snap[i] >= stem_snap[i+1]) {
260 errh->error("StemSnap%s is not sorted in increasing order", name);
261 break;
262 }
263 for (int i = 0; i < stem_snap.size(); i++)
264 if (stem_snap[i] == main)
265 return;
266 if (main >= 0)
267 errh->warning("Std%sW not in StemSnap%s array", name, name);
268 }
269
270 void
271 check_stems(Type1Font *font, ErrorHandler *errh)
272 {
273 Vector<double> StdHW, StdVW, StemSnapH, StemSnapV;
274 if (get_num_array(font, Type1Font::dP, "StdHW", StdHW, errh)) {
275 if (StdHW.size() != 1)
276 errh->error("StdHW has %d entries (exactly one required)", StdHW.size());
277 if (StdHW.size() > 0 && StdHW[0] <= 0)
278 errh->error("StdHW entry less than or equal to 0");
279 }
280 if (get_num_array(font, Type1Font::dP, "StdVW", StdVW, errh)) {
281 if (StdVW.size() != 1)
282 errh->error("StdVW has %d entries (exactly one required)", StdVW.size());
283 if (StdVW.size() > 0 && StdVW[0] <= 0)
284 errh->error("StdVW entry less than or equal to 0");
285 }
286
287 if (get_num_array(font, Type1Font::dP, "StemSnapH", StemSnapH, errh))
288 check_stem_snap(StemSnapH, StdHW.size() ? StdHW[0] : -1000, false, errh);
289 if (get_num_array(font, Type1Font::dP, "StemSnapV", StemSnapV, errh))
290 check_stem_snap(StemSnapV, StdVW.size() ? StdVW[0] : -1000, true, errh);
291 }
292
293
294 // MAIN
295
296 static void
297 do_file(const char *filename, PsresDatabase *psres, ErrorHandler *errh,
298 ErrorHandler *err_errh)
299 {
300 FILE *f;
301 if (strcmp(filename, "-") == 0) {
302 f = stdin;
303 filename = "<stdin>";
304 #if defined(_MSDOS) || defined(_WIN32)
305 _setmode(_fileno(f), _O_BINARY);
306 #endif
307 } else
308 f = fopen(filename, "rb");
309
310 if (!f) {
311 // check for PostScript name
312 Filename fn = psres->filename_value("FontOutline", filename);
313 f = fn.open_read();
314 }
315
316 if (!f)
317 err_errh->fatal("%s: %s", filename, strerror(errno));
318
319 Type1Reader *reader;
320 int c = getc(f);
321 ungetc(c, f);
322 if (c == EOF)
323 err_errh->fatal("%s: empty file", filename);
324 if (c == 128)
325 reader = new Type1PFBReader(f);
326 else
327 reader = new Type1PFAReader(f);
328
329 Type1Font *font = new Type1Font(*reader);
330
331 if (font) {
332 LandmarkErrorHandler cerrh(errh, filename);
333
334 // check UniqueID values
335 int UniqueID = -1, UniqueID2 = -1;
336 if (get_integer(font, Type1Font::dF, "UniqueID", UniqueID, errh)
337 && (UniqueID < 0 || UniqueID > 0xFFFFFF))
338 cerrh.error("UniqueID not in the range 0-16777215");
339 if (get_integer(font, Type1Font::dP, "UniqueID", UniqueID2, errh)
340 && (UniqueID2 < 0 || UniqueID2 > 0xFFFFFF))
341 cerrh.error("Private UniqueID not in the range 0-16777215");
342 if (UniqueID >= 0 && UniqueID2 >= 0 && UniqueID != UniqueID2)
343 cerrh.error("Private UniqueID does not equal font UniqueID");
344
345 // check XUID values
346 Vector<double> XUID;
347 if (get_num_array(font, Type1Font::dF, "XUID", XUID, errh)) {
348 if (XUID.size() == 0)
349 cerrh.error("empty XUID");
350 for (int i = 0; i < XUID.size(); i++) {
351 if (floor(XUID[i]) != XUID[i] || XUID[i] < 0 || XUID[i] > 0xFFFFFF) {
352 cerrh.error("element of XUID not an integer in the range 0-16777215");
353 break;
354 }
355 }
356 }
357
358 check_blues(font, &cerrh);
359 check_stems(font, &cerrh);
360
361 MultipleMasterSpace *mmspace = font->create_mmspace(&cerrh);
362 Vector<double> weight_vector;
363 if (mmspace)
364 weight_vector = mmspace->default_weight_vector();
365 int gc = font->nglyphs();
366 CharstringChecker cc(weight_vector);
367
368 for (int i = 0; i < gc; i++) {
369 ContextErrorHandler derrh
370 (&cerrh, "While interpreting %<%s%>:", font->glyph_name(i).c_str());
371 cc.check(font->glyph_context(i), &derrh);
372 }
373
374 int ns = font->nsubrs();
375 CharstringSubrChecker csc(weight_vector);
376 for (int i = 0; i < ns; ++i)
377 if (Type1Charstring *cs = font->subr(i)) {
378 ContextErrorHandler derrh(&cerrh, "While interpreting subr %d:", i);
379 CharstringContext cctx(font, cs);
380 csc.check(cctx, &derrh);
381 }
382 }
383
384 delete font;
385 delete reader;
386 }
387
388 int
389 main(int argc, char *argv[])
390 {
391 PsresDatabase *psres = new PsresDatabase;
392 psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false);
393
394 Clp_Parser *clp =
395 Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options);
396 program_name = Clp_ProgramName(clp);
397
398 ErrorHandler *err_errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr));
399 ErrorHandler *out_errh = new FileErrorHandler(stdout);
400 ErrorHandler *errh = out_errh;
401 int nfiles = 0;
402
403 while (1) {
404 int opt = Clp_Next(clp);
405 switch (opt) {
406
407 case QUIET_OPT:
408 if (clp->negated)
409 errh = out_errh;
410 else
411 errh = new SilentErrorHandler;
412 break;
413
414 case VERSION_OPT:
415 printf("t1lint (LCDF typetools) %s\n", VERSION);
416 printf("Copyright (C) 1999-2023 Eddie Kohler\n\
417 This is free software; see the source for copying conditions.\n\
418 There is NO warranty, not even for merchantability or fitness for a\n\
419 particular purpose.\n");
420 exit(0);
421 break;
422
423 case HELP_OPT:
424 usage();
425 exit(0);
426 break;
427
428 case Clp_NotOption:
429 do_file(clp->vstr, psres, errh, err_errh);
430 nfiles++;
431 break;
432
433 case Clp_Done:
434 goto done;
435
436 case Clp_BadOption:
437 usage_error(err_errh, 0);
438 break;
439
440 default:
441 break;
442
443 }
444 }
445
446 done:
447 if (nfiles == 0)
448 do_file("-", psres, errh, err_errh);
449
450 return (errh->nerrors() == 0 ? 0 : 1);
451 }