Debian Codegraph @

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

raw
1 /* main.cc -- driver for mmafm 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/findmet.hh>
18 #include <efont/psres.hh>
19 #include <lcdf/slurper.hh>
20 #include <efont/afm.hh>
21 #include <efont/afmw.hh>
22 #include <efont/amfm.hh>
23 #include <lcdf/error.hh>
24 #include <lcdf/clp.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <math.h>
29 #ifdef HAVE_CTIME
30 # include <time.h>
31 #endif
32
33 #define WEIGHT_OPT 300
34 #define WIDTH_OPT 301
35 #define OPSIZE_OPT 302
36 #define STYLE_OPT 303
37 #define N1_OPT 304
38 #define N2_OPT 305
39 #define N3_OPT 306
40 #define N4_OPT 307
41 #define VERSION_OPT 308
42 #define HELP_OPT 309
43 #define OUTPUT_OPT 310
44 #define PRECISION_OPT 311
45 #define KERN_PREC_OPT 312
46
47 const Clp_Option options[] = {
48 { "1", '1', N1_OPT, Clp_ValDouble, 0 },
49 { "2", '2', N2_OPT, Clp_ValDouble, 0 },
50 { "3", '3', N3_OPT, Clp_ValDouble, 0 },
51 { "4", '4', N4_OPT, Clp_ValDouble, 0 },
52 { "weight", 'w', WEIGHT_OPT, Clp_ValDouble, 0 },
53 { "width", 'W', WIDTH_OPT, Clp_ValDouble, 0 },
54 { "optical-size", 'O', OPSIZE_OPT, Clp_ValDouble, 0 },
55 { "style", 0, STYLE_OPT, Clp_ValDouble, 0 },
56 { "wt", 0, WEIGHT_OPT, Clp_ValDouble, 0 },
57 { "wd", 0, WIDTH_OPT, Clp_ValDouble, 0 },
58 { "min-kern", 'k', KERN_PREC_OPT, Clp_ValDouble, 0 },
59 { "minimum-kern", 'k', KERN_PREC_OPT, Clp_ValDouble, 0 },
60 { "kern-precision", 'k', KERN_PREC_OPT, Clp_ValDouble, 0 },
61 { "output", 'o', OUTPUT_OPT, Clp_ValString, 0 },
62 { "precision", 'p', PRECISION_OPT, Clp_ValInt, 0 },
63 { "version", 'v', VERSION_OPT, 0, 0 },
64 { "help", 'h', HELP_OPT, 0, 0 },
65 };
66
67 using namespace Efont;
68
69
70 static const char *program_name;
71 static ErrorHandler *errh;
72 static AmfmMetrics *amfm;
73
74 static Vector<PermString> ax_names;
75 static Vector<int> ax_nums;
76 static Vector<double> values;
77
78 static void
79 set_design(PermString a, double v)
80 {
81 ax_names.push_back(a);
82 ax_nums.push_back(-1);
83 values.push_back(v);
84 }
85
86 static void
87 set_design(int a, double v)
88 {
89 ax_names.push_back(PermString());
90 ax_nums.push_back(a);
91 values.push_back(v);
92 }
93
94 static void
95 set_amfm(AmfmMetrics *a)
96 {
97 if (a) {
98 if (amfm) errh->fatal("already read one AMFM file");
99 amfm = a;
100 }
101 }
102
103
104 // apply precision
105 static inline void
106 pround(double &v, double multiplier, double divider)
107 {
108 if (KNOWN(v))
109 v = floor(v * multiplier + 0.5) * divider;
110 }
111
112 static void
113 apply_precision(Metrics *m, int precision)
114 {
115 if (precision < 0)
116 return;
117
118 double multiplier = 1, divider = 1;
119 for (int i = 0; i < precision; i++)
120 multiplier *= 10, divider /= 10;
121
122 for (int i = 0; i < m->nfd(); i++)
123 pround(m->fd(i), multiplier, divider);
124
125 for (int i = 0; i < m->nglyphs(); i++) {
126 pround(m->wd(i), multiplier, divider);
127 pround(m->lf(i), multiplier, divider);
128 pround(m->bt(i), multiplier, divider);
129 pround(m->rt(i), multiplier, divider);
130 pround(m->tp(i), multiplier, divider);
131 }
132
133 for (int i = 0; i < m->nkv(); i++)
134 pround(m->kv(i), multiplier, divider);
135 }
136
137 static void
138 apply_kern_precision(Metrics *m, double kern_precision)
139 {
140 if (kern_precision <= 0)
141 return;
142
143 for (int i = 0; i < m->nkv(); i++)
144 if (fabs(m->kv(i)) < kern_precision)
145 m->kv(i) = 0;
146 }
147
148
149 static void
150 read_file(const char *fn, MetricsFinder *finder)
151 {
152 Filename filename;
153 FILE *file;
154
155 if (strcmp(fn, "-") == 0) {
156 filename = Filename("<stdin>");
157 file = stdin;
158 } else {
159 filename = Filename(fn);
160 file = filename.open_read();
161 }
162 int save_errno = errno;
163
164 if (!file) {
165 // look for a font by name
166 AmfmMetrics *new_amfm = finder->find_amfm(fn, errh);
167 if (new_amfm) {
168 set_amfm(new_amfm);
169 return;
170 }
171 if (finder->find_metrics(fn, errh))
172 return;
173
174 // check for instance name. don't use InstanceMetricsFinder.
175 const char *underscore = strchr(fn, '_');
176 if (underscore)
177 new_amfm = finder->find_amfm(PermString(fn, underscore - fn), errh);
178 if (!new_amfm)
179 errh->fatal("%s: %s", fn, strerror(save_errno));
180 set_amfm(new_amfm);
181
182 int i = 0;
183 while (underscore[0] == '_' && underscore[1]) {
184 double x = strtod(underscore + 1, const_cast<char **>(&underscore));
185 set_design(i, x);
186 i++;
187 }
188 return;
189 }
190
191 Slurper slurper(filename, file);
192 bool is_afm = false;
193 if (file != stdin) {
194 char *first_line = slurper.peek_line();
195 if (first_line)
196 is_afm = strncmp(first_line, "StartFontMetrics", 16) == 0;
197 }
198
199 if (is_afm) {
200 Metrics *afm = AfmReader::read(slurper, errh);
201 if (afm)
202 finder->record(afm);
203 } else
204 set_amfm(AmfmReader::read(slurper, finder, errh));
205 }
206
207
208 static void
209 usage_error(const char *error_message, ...)
210 {
211 va_list val;
212 va_start(val, error_message);
213 if (!error_message)
214 errh->message("Usage: %s [OPTION | FONT]...", program_name);
215 else
216 errh->xmessage(ErrorHandler::e_error, error_message, val);
217 errh->message("Type %s --help for more information.", program_name);
218 exit(1);
219 va_end(val);
220 }
221
222 static void
223 usage()
224 {
225 FileErrorHandler uerrh(stdout);
226 uerrh.message("\
227 %<Mmafm%> creates an AFM font metrics file for a multiple master font by\n\
228 interpolating at a point you specify and writes it to the standard output.\n\
229 \n\
230 Usage: %s [OPTION | FONT]...\n\
231 \n\
232 Each FONT is either an AFM or AMFM file name, or the font name of a multiple\n\
233 master font. In the second case, mmafm will find the actual AMFM file using\n\
234 the PSRESOURCEPATH environment variable.\n\
235 \n\
236 General options:\n\
237 -o, --output=FILE Write output to FILE.\n\
238 -h, --help Print this message and exit.\n\
239 -v, --version Print version number and warranty and exit.\n\
240 \n\
241 Interpolation settings:\n\
242 -w, --weight=N Set weight to N.\n\
243 -W, --width=N Set width to N.\n\
244 -O, --optical-size=N Set optical size to N.\n\
245 --style=N Set style axis to N.\n\
246 --1=N, --2=N, --3=N, --4=N Set first (second, third, fourth) axis to N.\n\
247 -p, --precision=N Allow N digits of fraction (default 3).\n\
248 -k, --min-kern=N Remove kerns smaller than N (default 2).\n\
249 \n\
250 Report bugs to <[email protected]>.\n", program_name);
251 }
252
253
254 int
255 main(int argc, char *argv[])
256 {
257 MetricsFinder *finder = new CacheMetricsFinder;
258
259 PsresDatabase *psres = new PsresDatabase;
260 psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false);
261 PsresMetricsFinder *psres_finder = new PsresMetricsFinder(psres);
262 finder->add_finder(psres_finder);
263
264 Clp_Parser *clp =
265 Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options);
266 program_name = Clp_ProgramName(clp);
267
268 errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr, String(program_name) + ": "));
269
270 FILE *output_file = 0;
271 int precision = 3;
272 double kern_precision = 2.0;
273 while (1) {
274 int opt = Clp_Next(clp);
275 switch (opt) {
276
277 case WEIGHT_OPT:
278 set_design("Weight", clp->val.d);
279 break;
280
281 case WIDTH_OPT:
282 set_design("Width", clp->val.d);
283 break;
284
285 case OPSIZE_OPT:
286 set_design("OpticalSize", clp->val.d);
287 break;
288
289 case STYLE_OPT:
290 set_design("Style", clp->val.d);
291 break;
292
293 case N1_OPT:
294 case N2_OPT:
295 case N3_OPT:
296 case N4_OPT:
297 set_design(opt - N1_OPT, clp->val.d);
298 break;
299
300 case PRECISION_OPT:
301 precision = clp->val.i;
302 break;
303
304 case KERN_PREC_OPT:
305 kern_precision = clp->val.d;
306 break;
307
308 case OUTPUT_OPT:
309 if (output_file) errh->fatal("output file already specified");
310 if (strcmp(clp->vstr, "-") == 0)
311 output_file = stdout;
312 else {
313 output_file = fopen(clp->vstr, "wb");
314 if (!output_file)
315 errh->fatal("%s: %s", clp->vstr, strerror(errno));
316 }
317 break;
318
319 case HELP_OPT:
320 usage();
321 exit(0);
322 break;
323
324 case VERSION_OPT:
325 printf("mmafm (LCDF typetools) %s\n", VERSION);
326 printf("Copyright (C) 1997-2023 Eddie Kohler\n\
327 This is free software; see the source for copying conditions.\n\
328 There is NO warranty, not even for merchantability or fitness for a\n\
329 particular purpose.\n");
330 exit(0);
331 break;
332
333 case Clp_NotOption:
334 read_file(clp->vstr, finder);
335 break;
336
337 case Clp_Done:
338 goto done;
339
340 case Clp_BadOption:
341 usage_error(0);
342 break;
343
344 }
345 }
346
347 done:
348 if (!amfm) usage_error("missing font argument");
349
350 MultipleMasterSpace *mmspace = amfm->mmspace();
351 #if MMAFM_RUN_MMPFB
352 if (!mmspace->check_intermediate()) {
353 size_t bufsz = amfm->font_name().length() + 30;
354 char *buf = new char[bufsz];
355 snprintf(buf, bufsz, "mmpfb -q --amcp-info '%s'", amfm->font_name().c_str());
356
357 FILE *f = popen(buf, "r");
358 if (f) {
359 Filename fake("<mmpfb output>");
360 Slurper slurpy(fake, f);
361 AmfmReader::add_amcp_file(slurpy, amfm, errh);
362 pclose(f);
363 }
364
365 delete[] buf;
366 }
367 #endif
368
369 Vector<double> design = mmspace->default_design_vector();
370 for (int i = 0; i < values.size(); i++)
371 if (ax_names[i])
372 mmspace->set_design(design, ax_names[i], values[i], errh);
373 else
374 mmspace->set_design(design, ax_nums[i], values[i], errh);
375
376 Vector<double> weight;
377 if (!mmspace->design_to_weight(design, weight, errh)) {
378 if (!mmspace->check_intermediate()) {
379 errh->message("(I can%,t interpolate font programs with intermediate masters on my own.");
380 #if MMAFM_RUN_MMPFB
381 errh->message("I tried to run %<mmpfb --amcp-info %s%>, but it didn't work.", amfm->font_name().c_str());
382 errh->message("Maybe your PSRESOURCEPATH environment variable is not set?");
383 #endif
384 errh->fatal("See the manual page for more information.)");
385 } else
386 errh->fatal("can%,t create weight vector");
387 }
388
389 // Need to check for case when all design coordinates are unspecified. The
390 // AMFM file contains a default WeightVector, but often NOT a default
391 // DesignVector; we don't want to generate a file with a FontName like
392 // `MyriadMM_-9.79797979e97_-9.79797979e97_' because the DesignVector
393 // components are unknown.
394 if (!KNOWN(design[0]))
395 errh->fatal("must specify %s%,s %s coordinate", amfm->font_name().c_str(),
396 mmspace->axis_type(0).c_str());
397
398 Metrics *m = amfm->interpolate(design, weight, errh);
399 if (m) {
400
401 // Add a comment identifying this as interpolated by mmafm
402 if (MetricsXt *xt = m->find_xt("AFM")) {
403 AfmMetricsXt *afm_xt = (AfmMetricsXt *)xt;
404
405 #if HAVE_CTIME
406 time_t cur_time = time(0);
407 char *time_str = ctime(&cur_time);
408 int time_len = strlen(time_str) - 1;
409 size_t bufsz = strlen(VERSION) + time_len + 100;
410 char* buf = new char[bufsz];
411 snprintf(buf, bufsz, "Interpolated by mmafm-%s on %.*s.", VERSION,
412 time_len, time_str);
413 #else
414 char* buf = new char[strlen(VERSION) + 100];
415 snprintf(buf, strlen(VERSION) + 100, "Interpolated by mmafm-%s.", VERSION);
416 #endif
417
418 afm_xt->opening_comments.push_back(buf);
419 afm_xt->opening_comments.push_back("Mmafm is free software. See <http://www.lcdf.org/type/>.");
420 delete[] buf;
421 }
422
423 // round numbers if necessary
424 if (precision >= 0)
425 apply_precision(m, precision);
426 if (kern_precision > 0)
427 apply_kern_precision(m, kern_precision);
428
429 // write the output file
430 if (!output_file)
431 output_file = stdout;
432 AfmWriter::write(m, output_file);
433
434 return 0;
435 } else
436 return 1;
437 }