Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/libefont/amfm.cc

raw
1 // -*- related-file-name: "../include/efont/amfm.hh" -*-
2
3 /* amfm.{cc,hh} -- Adobe Multiple-Master Font Metrics
4 *
5 * Copyright (c) 1998-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/amfm.hh>
20 #include <efont/afm.hh>
21 #include <efont/afmparse.hh>
22 #include <lcdf/error.hh>
23 #include <efont/findmet.hh>
24 #include <efont/t1cs.hh>
25 #include <lcdf/straccum.hh>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <assert.h>
30 #include <string.h>
31 namespace Efont {
32
33 AmfmMetrics::AmfmMetrics(MetricsFinder *finder)
34 : _finder(finder),
35 _fdv(fdLast, UNKDOUBLE),
36 _nmasters(-1), _naxes(-1), _masters(0), _mmspace(0),
37 _primary_fonts(0), _sanity_afm(0), _uses(0)
38 {
39 }
40
41 AmfmMetrics::~AmfmMetrics()
42 {
43 assert(_uses == 0);
44 for (int m = 0; m < _nmasters; m++)
45 if (_masters[m].afm)
46 _masters[m].afm->unuse();
47 delete[] _masters;
48 delete _mmspace;
49 while (_primary_fonts) {
50 AmfmPrimaryFont *pf = _primary_fonts;
51 _primary_fonts = _primary_fonts->next;
52 delete pf;
53 }
54 }
55
56
57 bool
58 AmfmMetrics::sanity(ErrorHandler *errh) const
59 {
60 if (!_mmspace) {
61 errh->error("AMFM sanity: no multiple master interpolation information");
62 return false;
63 }
64
65 bool ok = true;
66 for (int m = 0; m < _nmasters; m++)
67 if (!_masters[m].font_name
68 || _masters[m].weight_vector.size() != _nmasters) {
69 errh->error("AMFM sanity: no information for master %d", m);
70 ok = false;
71 }
72
73 if (!_mmspace->check(errh))
74 ok = false;
75
76 return ok;
77 }
78
79
80 int
81 AmfmMetrics::primary_label_value(int ax, PermString label) const
82 {
83 assert(ax >= 0 && ax < _naxes);
84 for (AmfmPrimaryFont *pf = _primary_fonts; pf; pf = pf->next) {
85 if (pf->labels[ax] == label)
86 return pf->design_vector[ax];
87 }
88 return -1;
89 }
90
91
92 inline static bool
93 strcompat(PermString a, PermString b)
94 {
95 return !a || !b || a == b;
96 }
97
98 Metrics *
99 AmfmMetrics::master(int m, ErrorHandler *errh)
100 {
101 AmfmMaster &master = _masters[m];
102
103 if (!master.loaded) {
104 master.loaded = true;
105 DirectoryMetricsFinder directory_finder(_directory);
106 _finder->add_finder(&directory_finder);
107 Metrics *afm = _finder->find_metrics(master.font_name);
108
109 if (!afm) {
110 if (errh)
111 errh->error("%s: can%,t find AFM file for master %<%s%>",
112 _font_name.c_str(), master.font_name.c_str());
113
114 } else if (!strcompat(afm->font_name(), master.font_name)
115 || !strcompat(afm->family(), master.family)
116 || !strcompat(afm->full_name(), master.full_name)
117 || !strcompat(afm->version(), master.version)) {
118 if (errh)
119 errh->error("%s: AFM for master %<%s%> doesn%,t match AMFM",
120 _font_name.c_str(), master.font_name.c_str());
121
122 } else if (!_sanity_afm) {
123 master.afm = afm;
124 _sanity_afm = afm;
125 afm->use();
126
127 } else {
128 PairProgram *sanity_pairp = _sanity_afm->pair_program();
129 PairProgram *pairp = afm->pair_program();
130 char buf[1024];
131 buf[0] = 0;
132
133 if (afm->nglyphs() != _sanity_afm->nglyphs())
134 snprintf(buf, sizeof(buf), "glyph count (%d vs. %d)", afm->nglyphs(), _sanity_afm->nglyphs());
135 else if (afm->nfd() != _sanity_afm->nfd())
136 snprintf(buf, sizeof(buf), "fd count (%d vs. %d)", afm->nfd(), _sanity_afm->nfd());
137 else if (afm->nkv() != _sanity_afm->nkv())
138 snprintf(buf, sizeof(buf), "kv count (%d vs. %d)", afm->nkv(), _sanity_afm->nkv());
139 else if (pairp->op_count() != sanity_pairp->op_count())
140 snprintf(buf, sizeof(buf), "pair op count (%d vs. %d)", pairp->op_count(), sanity_pairp->op_count());
141
142 if (!buf[0]) {
143 master.afm = afm;
144 afm->use();
145 } else if (errh)
146 errh->error("%s: AFM for master %<%s%> failed sanity checks (%s)", _font_name.c_str(), master.font_name.c_str(), buf);
147 }
148 }
149
150 return master.afm;
151 }
152
153
154 AmfmPrimaryFont *
155 AmfmMetrics::find_primary_font(const Vector<double> &design_vector) const
156 {
157 assert(design_vector.size() == _naxes);
158 for (AmfmPrimaryFont *pf = _primary_fonts; pf; pf = pf->next) {
159
160 for (int a = 0; a < _naxes; a++)
161 if ((int)design_vector[a] != pf->design_vector[a])
162 goto loser;
163 return pf;
164
165 loser: ;
166 }
167 return 0;
168 }
169
170
171 Metrics *
172 AmfmMetrics::interpolate(const Vector<double> &design_vector,
173 const Vector<double> &weight_vector,
174 ErrorHandler *errh)
175 {
176 assert(design_vector.size() == _naxes);
177 assert(weight_vector.size() == _nmasters);
178
179 // FIXME: check masters for correspondence.
180
181 /* 0.
182 * Make sure all necessary AFMs have been loaded. */
183 int m;
184 for (m = 0; m < _nmasters; m++)
185 if (weight_vector[m])
186 if (!master(m, errh))
187 return 0;
188
189 /* 1.
190 * Use the design vector to generate new FontName and FullName. */
191
192 AmfmPrimaryFont *pf = find_primary_font(design_vector);
193 // The primary font is useless to us if it doesn't have axis labels.
194 if (pf && !pf->labels.size())
195 pf = 0;
196
197 StringAccum font_name_sa, full_name_sa;
198 font_name_sa << _font_name;
199 full_name_sa << _full_name;
200 for (int a = 0; a < _naxes; a++) {
201 double dv = design_vector[a];
202 font_name_sa << '_' << dv;
203 full_name_sa << (a == 0 ? '_' : ' ') << dv;
204
205 PermString label;
206 if (pf)
207 label = pf->labels[a];
208 if (!label)
209 label = _mmspace->axis_abbreviation(a);
210 if (label)
211 full_name_sa << ' ' << label;
212 }
213 // Multiple master fonts require an underscore AFTER the font name too
214 font_name_sa << '_';
215
216 /* 2.
217 * Set up the new AFM with the special constructor. */
218
219 // Find the first master with a non-zero component.
220 for (m = 0; m < _nmasters && weight_vector[m] == 0; m++)
221 ;
222 Metrics *afm = new Metrics(font_name_sa.c_str(), full_name_sa.c_str(), *_masters[m].afm);
223 if (MetricsXt *xt = _masters[m].afm->find_xt("AFM")) {
224 AfmMetricsXt *new_xt = new AfmMetricsXt((AfmMetricsXt &)*xt);
225 afm->add_xt(new_xt);
226 }
227
228 /* 2.
229 * Interpolate the old AFM data into the new. */
230
231 afm->interpolate_dimens(*_masters[m].afm, weight_vector[m], false);
232
233 for (m++; m < _nmasters; m++)
234 if (weight_vector[m])
235 afm->interpolate_dimens(*_masters[m].afm, weight_vector[m], true);
236
237 return afm;
238 }
239
240
241 /*****
242 * AmfmReader
243 **/
244
245 AmfmReader::AmfmReader(AfmParser &afmp, AmfmMetrics *amfm, ErrorHandler *errh)
246 : _amfm(amfm), _l(afmp), _mmspace(amfm->_mmspace)
247 {
248 _errh = errh ? errh : ErrorHandler::silent_handler();
249 }
250
251 AmfmMetrics *
252 AmfmReader::read(Slurper &slurper, MetricsFinder *finder, ErrorHandler *errh)
253 {
254 AfmParser parser(slurper);
255 if (!parser.ok()) return 0;
256 AmfmMetrics *amfm = new AmfmMetrics(finder);
257 AmfmReader reader(parser, amfm, errh);
258 if (!reader.read()) {
259 delete amfm;
260 return 0;
261 } else
262 return amfm;
263 }
264
265 AmfmMetrics *
266 AmfmReader::read(const Filename &fn, MetricsFinder *finder, ErrorHandler *errh)
267 {
268 Slurper slurper(fn);
269 return read(slurper, finder, errh);
270 }
271
272 void
273 AmfmReader::add_amcp_file(Slurper &slurper, AmfmMetrics *amfm, ErrorHandler *errh)
274 {
275 AfmParser parser(slurper);
276 if (!parser.ok()) return;
277 AmfmReader reader(parser, amfm, errh);
278 reader.read_amcp_file();
279 }
280
281
282 void
283 AmfmReader::lwarning(const char *format, ...) const
284 {
285 va_list val;
286 va_start(val, format);
287 _errh->xmessage(_l.landmark(), ErrorHandler::e_warning, format, val);
288 va_end(val);
289 }
290
291 void
292 AmfmReader::lerror(const char *format, ...) const
293 {
294 va_list val;
295 va_start(val, format);
296 _errh->xmessage(_l.landmark(), ErrorHandler::e_error, format, val);
297 va_end(val);
298 }
299
300 void
301 AmfmReader::no_match_warning(const char *context) const
302 {
303 // keyword() will fail (and a warning won't get printed) only if the string
304 // is all whitespace, which the spec allows
305 PermString keyword = _l.keyword();
306 if (!keyword) return;
307 if (_l.key_matched()) {
308 lwarning(context ? "bad %<%s%> command in %s:"
309 : "bad %<%s%> command:", keyword.c_str(), context);
310 lwarning("field %d %s", _l.fail_field(), _l.message().c_str());
311 } else
312 lwarning(context ? "unknown command %<%s%> in %s"
313 : "unknown command %<%s%>", keyword.c_str(), context);
314 _l.clear_message();
315 }
316
317
318 void
319 AmfmReader::check_mmspace()
320 {
321 if (!_mmspace && _amfm->_naxes >= 0 && _amfm->_nmasters >= 0
322 && _amfm->_font_name) {
323 _mmspace = _amfm->_mmspace =
324 new MultipleMasterSpace(_amfm->_font_name, _amfm->_naxes, _amfm->_nmasters);
325 }
326 }
327
328
329 bool
330 AmfmReader::read()
331 {
332 assert(_amfm != 0);
333 _mmspace = _amfm->_mmspace;
334
335 AfmParser &l = _l;
336 _amfm->_directory = l.filename().directory();
337
338 // First, read all opening comments into an array so we can print them out
339 // later.
340 PermString comment;
341 while (l.next_line()) {
342 if (l.isall("Comment %+s", &comment))
343 _amfm->_opening_comments.push_back(comment);
344 else if (l.isall("StartMasterFontMetrics %g", (double *)0))
345 ;
346 else {
347 l.save_line();
348 break;
349 }
350 }
351
352 int master = 0, axis = 0;
353
354 while (l.next_line())
355 switch (l[0]) {
356
357 case 'A':
358 if (l.isall("Ascender %g", &fd( fdAscender )))
359 break;
360 if (l.isall("Axes %d", &_amfm->_naxes)) {
361 check_mmspace();
362 break;
363 }
364 goto invalid;
365
366 case 'B':
367 if (l.is("BlendDesignPositions")) {
368 read_positions();
369 break;
370 }
371 if (l.is("BlendDesignMap")) {
372 read_normalize();
373 break;
374 }
375 if (l.is("BlendAxisTypes")) {
376 read_axis_types();
377 break;
378 }
379 goto invalid;
380
381 case 'C':
382 if (l.isall("CapHeight %g", &fd( fdCapHeight )))
383 break;
384 if (l.is("Comment"))
385 break;
386 goto invalid;
387
388 case 'D':
389 if (l.isall("Descender %g", &fd( fdDescender )))
390 break;
391 goto invalid;
392
393 case 'E':
394 if (l.isall("EncodingScheme %+s", &_amfm->_encoding_scheme))
395 break;
396 if (l.isall("EndMasterFontMetrics"))
397 goto done;
398 goto invalid;
399
400 case 'F':
401 if (l.isall("FontName %+s", &_amfm->_font_name)) {
402 check_mmspace();
403 break;
404 }
405 if (l.isall("FullName %+s", &_amfm->_full_name))
406 break;
407 if (l.isall("FamilyName %+s", &_amfm->_family))
408 break;
409 if (l.isall("FontBBox %g %g %g %g",
410 &fd( fdFontBBllx ), &fd( fdFontBBlly ),
411 &fd( fdFontBBurx ), &fd( fdFontBBury )))
412 break;
413 goto invalid;
414
415 case 'I':
416 if (l.isall("IsFixedPitch %b", (bool *)0))
417 break;
418 if (l.isall("ItalicAngle %g", &fd( fdItalicAngle )))
419 break;
420 goto invalid;
421
422 case 'M':
423 if (l.isall("Masters %d", &_amfm->_nmasters)) {
424 check_mmspace();
425 break;
426 }
427 goto invalid;
428
429 case 'N':
430 if (l.isall("Notice %+s", &_amfm->_notice))
431 break;
432 goto invalid;
433
434 case 'S':
435 if (l.isall("StartAxis")) {
436 read_axis(axis++);
437 break;
438 }
439 if (l.isall("StartMaster")) {
440 read_master(master++);
441 break;
442 }
443 if (l.isall("StartPrimaryFonts %d", (int *)0)) {
444 read_primary_fonts();
445 break;
446 }
447 if (l.isall("StartConversionPrograms %d %d", (int *)0, (int *)0)) {
448 read_conversion_programs();
449 break;
450 }
451 if (l.isall("StartMasterFontMetrics %g", (double *)0))
452 break;
453 goto invalid;
454
455 case 'U':
456 if (l.isall("UnderlinePosition %g", &fd( fdUnderlinePosition )))
457 break;
458 else if (l.isall("UnderlineThickness %g", &fd( fdUnderlineThickness )))
459 break;
460 goto invalid;
461
462 case 'V':
463 if (l.isall("Version %+s", &_amfm->_version))
464 break;
465 goto invalid;
466
467 case 'W':
468 if (l.isall("Weight %+s", &_amfm->_weight))
469 break;
470 if (l.is("WeightVector")) {
471 Vector<double> wv;
472 if (!read_simple_array(wv) || !_mmspace)
473 lerror("bad WeightVector");
474 else
475 _mmspace->set_weight_vector(wv);
476 break;
477 }
478 goto invalid;
479
480 case 'X':
481 if (l.isall("XHeight %g", &fd( fdXHeight )))
482 break;
483 goto invalid;
484
485 default:
486 invalid:
487 no_match_warning();
488
489 }
490
491 done:
492 if (!_mmspace) {
493 _errh->error("%<%s%> is not an AMFM file", String(_l.landmark().file()).c_str());
494 return false;
495 }
496
497 LandmarkErrorHandler pin_errh(_errh, _l.landmark());
498 if (!_amfm->sanity(&pin_errh)) {
499 _errh->lerror(_l.landmark().whole_file(),
500 "bad AMFM file (missing or inconsistent information)");
501 return false;
502 }
503
504 if (!_mmspace->check_intermediate() && _l.filename().directory()) {
505 String name = l.filename().base() + ".amcp";
506 Slurper slurp(_l.filename().from_directory(name));
507 add_amcp_file(slurp, _amfm, _errh);
508 }
509
510 return true;
511 }
512
513
514 void
515 AmfmReader::read_amcp_file()
516 {
517 int lines_read = 0;
518
519 while (_l.next_line()) {
520 lines_read++;
521 switch (_l[0]) {
522
523 case 'C':
524 if (_l.is("Comment"))
525 break;
526 goto invalid;
527
528 case 'S':
529 if (_l.isall("StartConversionPrograms %d %d", (int *)0, (int *)0)) {
530 read_conversion_programs();
531 break;
532 }
533 goto invalid;
534
535 default:
536 invalid:
537 no_match_warning("AMCP file");
538
539 }
540 }
541
542 if (_mmspace && !_mmspace->ndv() && !_mmspace->cdv() && lines_read)
543 lwarning("no conversion programs in .amcp file");
544 }
545
546
547 bool
548 AmfmReader::read_simple_array(Vector<double> &vec) const
549 {
550 if (!_l.is("[")) return false;
551
552 vec.clear();
553 double d;
554 while (_l.is("%g", &d))
555 vec.push_back(d);
556
557 return _l.is("]");
558 }
559
560
561 void
562 AmfmReader::read_positions() const
563 {
564 if (nmasters() < 2 || naxes() < 1) return;
565 Vector<NumVector> positions;
566 if (!_l.is("[") || !_mmspace) goto error;
567
568 for (int i = 0; i < nmasters(); i++) {
569 positions.push_back(NumVector());
570 if (!read_simple_array(positions.back()))
571 goto error;
572 }
573
574 if (!_l.is("]")) goto error;
575 _mmspace->set_master_positions(positions);
576 return;
577
578 error:
579 lerror("bad BlendDesignPositions");
580 }
581
582
583 void
584 AmfmReader::read_normalize() const
585 {
586 if (naxes() < 1) return;
587 Vector<NumVector> normalize_in, normalize_out;
588 if (!_l.is("[") || !_mmspace) goto error;
589
590 for (int a = 0; a < naxes(); a++) {
591 if (!_l.is("[")) goto error;
592 normalize_in.push_back(NumVector());
593 normalize_out.push_back(NumVector());
594 double v1, v2;
595 while (_l.is("[-%g %g-]", &v1, &v2)) {
596 normalize_in[a].push_back(v1);
597 normalize_out[a].push_back(v2);
598 }
599 if (!_l.is("]")) goto error;
600 }
601
602 if (!_l.is("]")) goto error;
603 _mmspace->set_normalize(normalize_in, normalize_out);
604 return;
605
606 error:
607 lerror("bad BlendDesignPositions");
608 }
609
610
611 void
612 AmfmReader::read_axis_types() const
613 {
614 PermString s;
615 int ax = 0;
616 Vector<PermString> types;
617 if (naxes() < 1) return;
618 if (!_l.is("[") || !_mmspace) goto error;
619 _mmspace->check();
620
621 while (_l.is("/%/s", &s))
622 _mmspace->set_axis_type(ax++, s);
623
624 if (!_l.is("]")) goto error;
625 return;
626
627 error:
628 lerror("bad BlendAxisTypes");
629 }
630
631
632 void
633 AmfmReader::read_axis(int ax) const
634 {
635 bool ok = _mmspace && ax < naxes();
636 if (!ok)
637 lerror("bad axis number %d", ax);
638 else
639 _mmspace->check();
640
641 PermString s;
642 while (_l.next_line())
643 switch (_l[0]) {
644
645 case 'A':
646 if (_l.is("AxisType %+s", &s)) {
647 if (ok)
648 _mmspace->set_axis_type(ax, s);
649 break;
650 }
651 if (_l.is("AxisLabel %+s", &s)) {
652 if (ok)
653 _mmspace->set_axis_label(ax, s);
654 break;
655 }
656 goto invalid;
657
658 case 'C':
659 if (_l.is("Comment"))
660 break;
661 goto invalid;
662
663 case 'E':
664 if (_l.isall("EndAxis"))
665 goto endaxis;
666 goto invalid;
667
668 default:
669 invalid:
670 no_match_warning();
671
672 }
673
674 endaxis: ;
675 }
676
677
678 void
679 AmfmReader::read_master(int m) const
680 {
681 AmfmMaster *amfmm;
682 AmfmMaster dummy;
683 if (m >= nmasters()) {
684 lerror("too many masters");
685 amfmm = &dummy;
686 } else {
687 if (!_amfm->_masters)
688 _amfm->_masters = new AmfmMaster[ nmasters() ];
689 amfmm = &_amfm->_masters[m];
690 }
691
692 while (_l.next_line())
693 // Grok the whole line. Are we on a character metric data line?
694 switch (_l[0]) {
695
696 case 'C':
697 if (_l.is("Comment"))
698 break;
699 goto invalid;
700
701 case 'E':
702 if (_l.isall("EndMaster"))
703 goto endmaster;
704 goto invalid;
705
706 case 'F':
707 if (_l.isall("FontName %+s", &amfmm->font_name))
708 break;
709 if (_l.isall("FullName %+s", &amfmm->full_name))
710 break;
711 if (_l.isall("FamilyName %+s", &amfmm->family))
712 break;
713 goto invalid;
714
715 case 'V':
716 if (_l.isall("Version %+s", &amfmm->version))
717 break;
718 goto invalid;
719
720 case 'W':
721 if (_l.is("WeightVector")) {
722 if (!(read_simple_array(amfmm->weight_vector) &&
723 amfmm->weight_vector.size() == nmasters())) {
724 lerror("bad WeightVector");
725 amfmm->weight_vector.clear();
726 }
727 break;
728 }
729 goto invalid;
730
731 default:
732 invalid:
733 no_match_warning();
734
735 }
736
737 endmaster: ;
738 }
739
740
741 void
742 AmfmReader::read_one_primary_font() const
743 {
744 AmfmPrimaryFont *pf = new AmfmPrimaryFont;
745 pf->design_vector.resize(naxes());
746 pf->labels.resize(naxes());
747
748 while (_l.left()) {
749
750 if (_l.is("PC")) {
751 for (int a = 0; a < naxes(); a++)
752 if (!_l.is("%d", &pf->design_vector[a]))
753 goto error;
754 } else if (_l.is("PL")) {
755 for (int a = 0; a < naxes(); a++)
756 if (!_l.is("(-%/s-)", &pf->labels[a]))
757 goto error;
758 } else if (_l.is("PN %(", &pf->name))
759 ;
760 else
761 no_match_warning("primary font");
762
763 _l.is(";"); // get rid of any possible semicolon
764 }
765
766 pf->next = _amfm->_primary_fonts;
767 _amfm->_primary_fonts = pf;
768 return;
769
770 error:
771 delete pf;
772 }
773
774
775 void
776 AmfmReader::read_primary_fonts() const
777 {
778 while (_l.next_line())
779 switch (_l[0]) {
780
781 case 'C':
782 if (_l.is("Comment"))
783 break;
784 goto invalid;
785
786 case 'E':
787 if (_l.isall("EndPrimaryFonts"))
788 goto end_primary_fonts;
789 goto invalid;
790
791 case 'P':
792 if (_l[1] == 'C' && isspace(_l[2])) {
793 read_one_primary_font();
794 break;
795 }
796 goto invalid;
797
798 default:
799 invalid:
800 no_match_warning();
801
802 }
803
804 end_primary_fonts: ;
805 }
806
807
808 void
809 AmfmReader::read_conversion_programs() const
810 {
811 String ndv, cdv, s;
812
813 while (_l.next_line())
814 switch (_l[0]) {
815
816 case 'C':
817 if (_l.isall("CDV %<", &s)) {
818 cdv += s;
819 break;
820 }
821 goto invalid;
822
823 case 'E':
824 if (_l.isall("EndConversionPrograms"))
825 goto end_conversion_programs;
826 goto invalid;
827
828 case 'N':
829 if (_l.isall("NDV %<", &s)) {
830 ndv += s;
831 break;
832 }
833 goto invalid;
834
835 default:
836 invalid:
837 no_match_warning();
838 break;
839
840 }
841
842 end_conversion_programs:
843 if (_mmspace) {
844 _mmspace->set_ndv(Type1Charstring(ndv));
845 _mmspace->set_cdv(Type1Charstring(cdv));
846 }
847 }
848
849 }