Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/mmpfb/main.cc

raw
1 /* main.cc -- driver for mmpfb program
2 *
3 * Copyright (c) 1997-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/t1mm.hh>
20 #include "myfont.hh"
21 #include "t1rewrit.hh"
22 #include "t1minimize.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 #define WEIGHT_OPT 300
40 #define WIDTH_OPT 301
41 #define OPSIZE_OPT 302
42 #define STYLE_OPT 303
43 #define N1_OPT 304
44 #define N2_OPT 305
45 #define N3_OPT 306
46 #define N4_OPT 307
47 #define VERSION_OPT 308
48 #define AMCP_INFO_OPT 309
49 #define HELP_OPT 310
50 #define PFA_OPT 311
51 #define PFB_OPT 312
52 #define OUTPUT_OPT 313
53 #define QUIET_OPT 314
54 #define PRECISION_OPT 315
55 #define SUBRS_OPT 316
56 #define MINIMIZE_OPT 317
57
58 const Clp_Option options[] = {
59 { "1", '1', N1_OPT, Clp_ValDouble, 0 },
60 { "2", '2', N2_OPT, Clp_ValDouble, 0 },
61 { "3", '3', N3_OPT, Clp_ValDouble, 0 },
62 { "4", '4', N4_OPT, Clp_ValDouble, 0 },
63 { "amcp-info", 0, AMCP_INFO_OPT, 0, 0 },
64 { "help", 'h', HELP_OPT, 0, 0 },
65 { "minimize", 'm', MINIMIZE_OPT, 0, Clp_Negate },
66 { "optical-size", 'O', OPSIZE_OPT, Clp_ValDouble, 0 },
67 { "output", 'o', OUTPUT_OPT, Clp_ValString, 0 },
68 { "pfa", 'a', PFA_OPT, 0, 0 },
69 { "pfb", 'b', PFB_OPT, 0, 0 },
70 { "precision", 'p', PRECISION_OPT, Clp_ValUnsigned, 0 },
71 { "quiet", 'q', QUIET_OPT, 0, Clp_Negate },
72 { "style", 0, STYLE_OPT, Clp_ValDouble, 0 },
73 { "subrs", 0, SUBRS_OPT, Clp_ValInt, Clp_Negate },
74 { "version", 'v', VERSION_OPT, 0, 0 },
75 { "wd", 0, WIDTH_OPT, Clp_ValDouble, 0 },
76 { "weight", 'w', WEIGHT_OPT, Clp_ValDouble, 0 },
77 { "width", 'W', WIDTH_OPT, Clp_ValDouble, 0 },
78 { "wt", 0, WEIGHT_OPT, Clp_ValDouble, 0 },
79 };
80
81 using namespace Efont;
82
83 static const char *program_name;
84 static ErrorHandler *errh;
85 static MyFont *font;
86 static MultipleMasterSpace *mmspace;
87
88 static Vector<PermString> ax_names;
89 static Vector<int> ax_nums;
90 static Vector<double> values;
91
92
93 void
94 usage_error(const char *error_message, ...)
95 {
96 va_list val;
97 va_start(val, error_message);
98 if (!error_message)
99 errh->message("Usage: %s [OPTION]... FONT", program_name);
100 else
101 errh->xmessage(ErrorHandler::e_error, error_message, val);
102 errh->message("Type %s --help for more information.", program_name);
103 exit(1);
104 va_end(val);
105 }
106
107 void
108 usage()
109 {
110 FileErrorHandler uerrh(stdout);
111 uerrh.message("\
112 %<Mmpfb%> creates a single-master PostScript Type 1 font by interpolating a\n\
113 multiple master font at a point you specify. The resulting font does not\n\
114 contain multiple master extensions. It is written to the standard output.\n\
115 \n\
116 Usage: %s [OPTION]... FONT\n\
117 \n\
118 FONT is either the name of a PFA or PFB multiple master font file, or a\n\
119 PostScript font name. In the second case, mmpfb will find the actual outline\n\
120 file using the PSRESOURCEPATH environment variable.\n\
121 \n\
122 General options:\n\
123 --amcp-info Print AMCP info, if necessary, and exit.\n\
124 -a, --pfa Output PFA font.\n\
125 -b, --pfb Output PFB font. This is the default.\n\
126 -o, --output=FILE Write output to FILE.\n\
127 -p, --precision=N Set precision to N (larger means more precise).\n\
128 --subrs=N Limit output font to at most N subroutines.\n\
129 --no-minimize Do not replace original font%,s PostScript code.\n\
130 -h, --help Print this message and exit.\n\
131 -q, --quiet Do not generate any error messages.\n\
132 -v, --version Print version number and exit.\n\
133 \n\
134 Interpolation settings:\n\
135 -w, --weight=N Set weight to N.\n\
136 -W, --width=N Set width to N.\n\
137 -O, --optical-size=N Set optical size to N.\n\
138 --style=N Set style axis to N.\n\
139 --1=N, --2=N, --3=N, --4=N Set first (second, third, fourth) axis to N.\n\
140 \n\
141 Report bugs to <[email protected]>.\n", program_name);
142 }
143
144
145 static void
146 set_design(PermString a, double v)
147 {
148 ax_names.push_back(a);
149 ax_nums.push_back(-1);
150 values.push_back(v);
151 }
152
153 static void
154 set_design(int a, double v)
155 {
156 ax_names.push_back(PermString());
157 ax_nums.push_back(a);
158 values.push_back(v);
159 }
160
161 void
162 do_file(const char *filename, PsresDatabase *psres)
163 {
164 FILE *f;
165 if (strcmp(filename, "-") == 0) {
166 f = stdin;
167 filename = "<stdin>";
168 #if defined(_MSDOS) || defined(_WIN32)
169 _setmode(_fileno(f), _O_BINARY);
170 #endif
171 } else
172 f = fopen(filename, "rb");
173
174 if (!f) {
175 // check for PostScript or instance name
176 Filename fn = psres->filename_value("FontOutline", filename);
177 const char *underscore = strchr(filename, '_');
178 if (!fn && underscore) {
179 fn = psres->filename_value
180 ("FontOutline", PermString(filename, underscore - filename));
181 int i = 0;
182 while (underscore[0] == '_' && underscore[1]) {
183 double x = strtod(underscore + 1, const_cast<char **>(&underscore));
184 set_design(i, x);
185 i++;
186 }
187 }
188 f = fn.open_read();
189 }
190
191 if (!f)
192 errh->fatal("%s: %s", filename, strerror(errno));
193
194 Type1Reader *reader;
195 int c = getc(f);
196 ungetc(c, f);
197 if (c == EOF)
198 errh->fatal("%s: empty file", filename);
199 if (c == 128)
200 reader = new Type1PFBReader(f);
201 else
202 reader = new Type1PFAReader(f);
203
204 font = new MyFont(*reader);
205 delete reader;
206 if (!font->ok())
207 errh->fatal("%s: invalid font", filename);
208 else if (font->nglyphs() == 0)
209 errh->fatal("%s: font contains no characters", filename);
210
211 mmspace = font->create_mmspace(errh);
212 if (!mmspace)
213 errh->fatal("%s: not a multiple master font", filename);
214
215 font->undo_synthetic();
216 }
217
218
219 static void
220 print_conversion_program(FILE *f, const Type1Charstring &cs, PermString name)
221 {
222 if (cs) {
223 const unsigned char *data = cs.data();
224 for (int i = 0; i < cs.length(); ) {
225 int l = cs.length() - i;
226 if (l > 32)
227 l = 32;
228 fprintf(f, "%s <", name.c_str());
229 for (int j = 0; j < l; j++)
230 fprintf(f, "%02X", data[j]);
231 fprintf(f, ">\n");
232 data += l;
233 i += l;
234 }
235 }
236 }
237
238
239 static void
240 print_amcp_info(MultipleMasterSpace *mmspace, FILE *f)
241 {
242 const Type1Charstring &ndv = mmspace->ndv();
243 const Type1Charstring &cdv = mmspace->cdv();
244 if (!ndv && !cdv)
245 fprintf(stderr, "%s does not have conversion programs.\n",
246 mmspace->font_name().c_str());
247 else {
248 fprintf(f, "StartConversionPrograms %d %d\n", ndv.length(),
249 cdv.length());
250 print_conversion_program(f, ndv, "NDV");
251 print_conversion_program(f, cdv, "CDV");
252 fprintf(f, "EndConversionPrograms\n");
253 }
254 }
255
256
257 int
258 main(int argc, char *argv[])
259 {
260 PsresDatabase *psres = new PsresDatabase;
261 psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false);
262
263 Clp_Parser *clp =
264 Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options);
265 program_name = Clp_ProgramName(clp);
266
267 bool write_pfb = true;
268 bool amcp_info = false;
269 bool minimize = true;
270 int precision = 5;
271 int subr_count = -1;
272 FILE *outfile = 0;
273 ::errh =
274 ErrorHandler::static_initialize(new FileErrorHandler(stderr, String(program_name) + ": "));
275
276 while (1) {
277 int opt = Clp_Next(clp);
278 switch (opt) {
279
280 case WEIGHT_OPT:
281 set_design("Weight", clp->val.d);
282 break;
283
284 case WIDTH_OPT:
285 set_design("Width", clp->val.d);
286 break;
287
288 case OPSIZE_OPT:
289 set_design("OpticalSize", clp->val.d);
290 break;
291
292 case STYLE_OPT:
293 set_design("Style", clp->val.d);
294 break;
295
296 case N1_OPT:
297 case N2_OPT:
298 case N3_OPT:
299 case N4_OPT:
300 set_design(opt - N1_OPT, clp->val.d);
301 break;
302
303 case AMCP_INFO_OPT:
304 amcp_info = true;
305 break;
306
307 case PFA_OPT:
308 write_pfb = false;
309 break;
310
311 case PFB_OPT:
312 write_pfb = true;
313 break;
314
315 case PRECISION_OPT:
316 if (clp->val.i > 107) {
317 errh->warning("precision lowered to 107");
318 precision = 107;
319 } else if (clp->val.i < 1) {
320 errh->warning("precision raised to 1");
321 precision = 1;
322 } else
323 precision = clp->val.i;
324 break;
325
326 case SUBRS_OPT:
327 if (clp->negated)
328 subr_count = -1;
329 else if (clp->val.i <= 0)
330 errh->warning("subr count too small");
331 else
332 subr_count = clp->val.i;
333 break;
334
335 case MINIMIZE_OPT:
336 minimize = !clp->negated;
337 break;
338
339 case QUIET_OPT:
340 if (clp->negated)
341 errh = ErrorHandler::default_handler();
342 else
343 errh = new SilentErrorHandler;
344 break;
345
346 case OUTPUT_OPT:
347 if (outfile) errh->fatal("output file already specified");
348 if (strcmp(clp->vstr, "-") == 0)
349 outfile = stdout;
350 else {
351 outfile = fopen(clp->vstr, "wb");
352 if (!outfile)
353 errh->fatal("%s: %s", clp->vstr, strerror(errno));
354 }
355 break;
356
357 case VERSION_OPT:
358 printf("mmpfb (LCDF typetools) %s\n", VERSION);
359 printf("Copyright (C) 1997-2023 Eddie Kohler\n\
360 This is free software; see the source for copying conditions.\n\
361 There is NO warranty, not even for merchantability or fitness for a\n\
362 particular purpose.\n");
363 exit(0);
364 break;
365
366 case HELP_OPT:
367 usage();
368 exit(0);
369 break;
370
371 case Clp_NotOption:
372 do_file(clp->vstr, psres);
373 break;
374
375 case Clp_Done:
376 if (!font)
377 usage_error("missing font argument");
378 goto done;
379
380 case Clp_BadOption:
381 usage_error(0);
382 break;
383
384 default:
385 break;
386
387 }
388 }
389
390 done:
391 if (outfile == 0)
392 outfile = stdout;
393
394 if (amcp_info) {
395 print_amcp_info(mmspace, outfile);
396 exit(0);
397 }
398
399 Vector<double> design = mmspace->empty_design_vector();
400 for (int i = 0; i < values.size(); i++)
401 if (ax_names[i])
402 mmspace->set_design(design, ax_names[i], values[i], errh);
403 else
404 mmspace->set_design(design, ax_nums[i], values[i], errh);
405
406 Vector<double> default_design = mmspace->default_design_vector();
407 for (int i = 0; i < mmspace->naxes(); i++)
408 if (!KNOWN(design[i]) && KNOWN(default_design[i])) {
409 errh->warning("using default value %g for %s%,s %s", default_design[i],
410 font->font_name().c_str(), mmspace->axis_type(i).c_str());
411 design[i] = default_design[i];
412 }
413
414 if (!font->set_design_vector(mmspace, design, errh))
415 exit(1);
416
417 font->interpolate_dicts(minimize, errh);
418 font->interpolate_charstrings(precision, errh);
419
420 if (subr_count >= 0) {
421 Type1SubrRemover sr(font, errh);
422 sr.run(subr_count);
423 }
424
425 font->fill_in_subrs();
426
427 Type1Font *t1font;
428 if (minimize) {
429 t1font = ::minimize(font);
430 delete font;
431 } else
432 t1font = font;
433
434 { // Add an identifying comment.
435 #if HAVE_CTIME
436 time_t cur_time = time(0);
437 char *time_str = ctime(&cur_time);
438 int time_len = strlen(time_str) - 1;
439 size_t bufsz = strlen(VERSION) + time_len + 100;
440 char* buf = new char[bufsz];
441 snprintf(buf, bufsz, "%%%% Interpolated by mmpfb-%s on %.*s.", VERSION,
442 time_len, time_str);
443 #else
444 char* buf = new char[strlen(VERSION) + 100];
445 snprintf(buf, strlen(VERSION) + 100, "%%%% Interpolated by mmpfb-%s.", VERSION);
446 #endif
447
448 t1font->add_header_comment(buf);
449 t1font->add_header_comment("%% Mmpfb is free software. See <http://www.lcdf.org/type/>.");
450 delete[] buf;
451 }
452
453 if (write_pfb) {
454 #if defined(_MSDOS) || defined(_WIN32)
455 _setmode(_fileno(outfile), _O_BINARY);
456 #endif
457 Type1PFBWriter w(outfile);
458 t1font->write(w);
459 } else {
460 Type1PFAWriter w(outfile);
461 t1font->write(w);
462 }
463
464 return 0;
465 }