Debian Codegraph @

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

raw
1 /* myfont.cc -- general multiple- to single-master conversion
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 "myfont.hh"
18 #include <efont/t1item.hh>
19 #include <efont/t1interp.hh>
20 #include "t1rewrit.hh"
21 #include <efont/t1mm.hh>
22 #include <lcdf/error.hh>
23 #include <lcdf/straccum.hh>
24 #include <string.h>
25 #include <stdio.h>
26 #include <math.h>
27 using namespace Efont;
28
29 MyFont::MyFont(Type1Reader &reader)
30 : Type1Font(reader)
31 {
32 }
33
34 MyFont::~MyFont()
35 {
36 }
37
38 void
39 MyFont::kill_def(Type1Definition *t1d, int whichd)
40 {
41 if (!t1d)
42 return;
43
44 if (whichd < 0)
45 for (whichd = dFont; whichd < dLast; whichd = (Dict)(whichd + 1))
46 if (dict(whichd, t1d->name()) == t1d)
47 break;
48 if (whichd < 0 || whichd >= dLast || dict(whichd, t1d->name()) != t1d)
49 return;
50
51 int icount = nitems();
52 for (int i = first_dict_item(whichd); i < icount; i++)
53 if (item(i) == t1d) {
54 StringAccum sa;
55 sa << '%';
56 t1d->gen(sa);
57 PermString name = t1d->name();
58 Type1CopyItem *t1ci = new Type1CopyItem(sa.take_string());
59 set_item(i, t1ci);
60 set_dict(whichd, name, 0);
61 return;
62 }
63
64 assert(0);
65 }
66
67 bool
68 MyFont::set_design_vector(MultipleMasterSpace *mmspace, const Vector<double> &design, ErrorHandler *errh)
69 {
70 Type1Definition *t1d = dict("DesignVector");
71 if (t1d) {
72 t1d->set_numvec(design);
73 kill_def(t1d, dFont);
74 }
75
76 t1d = dict("NormDesignVector");
77 if (t1d) {
78 NumVector norm_design;
79 if (mmspace->design_to_norm_design(design, norm_design))
80 t1d->set_numvec(norm_design);
81 kill_def(t1d, dFont);
82 }
83
84 if (!mmspace->design_to_weight(design, _weight_vector, errh))
85 return false;
86
87 // Need to check for case when all design coordinates are unspecified. The
88 // font file contains a default WeightVector, but possibly NOT a default
89 // DesignVector; we don't want to generate a FontName like
90 // `MyriadMM_-9.79797979e97_-9.79797979e97_' because the DesignVector
91 // components are unknown.
92 if (!KNOWN(design[0])) {
93 errh->error("must specify %s%,s %s coordinate", font_name().c_str(),
94 mmspace->axis_type(0).c_str());
95 return false;
96 }
97
98 t1d = dict("WeightVector");
99 if (t1d) {
100 t1d->set_numvec(_weight_vector);
101 kill_def(t1d, dFont);
102 }
103
104 int naxes = design.size();
105 _nmasters = _weight_vector.size();
106
107 PermString name;
108 t1d = dict("FontName");
109 if (t1d && t1d->value_name(name)) {
110 StringAccum sa(name);
111 for (int a = 0; a < naxes; a++)
112 sa << '_' << design[a];
113 // Multiple masters require an underscore AFTER the font name
114 sa << '_';
115 t1d->set_name(sa.c_str());
116 uncache_defs(); // remove cached font name
117 }
118
119 // add a FullName too
120 String full_name;
121 t1d = fi_dict("FullName");
122 if (t1d && t1d->value_string(full_name)) {
123 StringAccum sa(full_name);
124 for (int a = 0; a < naxes; a++) {
125 sa << (a ? ' ' : '_') << design[a];
126 PermString label = mmspace->axis_abbreviation(a);
127 if (label)
128 sa << ' ' << label;
129 }
130 t1d->set_string(sa.c_str());
131 }
132
133 // save UniqueID, then kill its definition
134 int uniqueid;
135 t1d = dict("UniqueID");
136 bool have_uniqueid = (t1d && t1d->value_int(uniqueid));
137 kill_def(t1d, dFont);
138
139 // prepare XUID
140 t1d = dict("XUID");
141 NumVector xuid;
142 if (!t1d || !t1d->value_numvec(xuid)) {
143 if (have_uniqueid) {
144 t1d = ensure(dFont, "XUID");
145 xuid.clear();
146 xuid.push_back(1);
147 xuid.push_back(uniqueid);
148 } else if (t1d) {
149 kill_def(t1d, dFont);
150 t1d = 0;
151 }
152 }
153 if (t1d) {
154 // Append design vector values to the XUID to prevent cache pollution.
155 for (int a = 0; a < naxes; a++)
156 xuid.push_back((int)(design[a] * 100));
157 t1d->set_numvec(xuid);
158 }
159
160 return true;
161 }
162
163 void
164 MyFont::interpolate_dict_int(PermString name, Dict the_dict, ErrorHandler *errh)
165 {
166 Type1Definition *def = dict(the_dict, name);
167 Type1Definition *blend_def = dict(the_dict + dBlend, name);
168 NumVector blend;
169
170 if (def && blend_def && blend_def->value_numvec(blend)) {
171 int n = _nmasters;
172 double val = 0;
173 for (int m = 0; m < n; m++)
174 val += blend[m] * _weight_vector[m];
175 int ival = (int)floor(val + 0.50001);
176 if (fabs(val - ival) >= 0.001)
177 errh->warning("interpolated %s should be an integer (it is %g)", name.c_str(), val);
178 def->set_num(ival);
179 kill_def(blend_def, the_dict + dBlend);
180 }
181 }
182
183 void
184 MyFont::interpolate_dict_num(PermString name, Dict the_dict, bool force_integer)
185 {
186 Type1Definition *def = dict(the_dict, name);
187 Type1Definition *blend_def = dict(the_dict + dBlend, name);
188 NumVector blend;
189
190 if (def && blend_def && blend_def->value_numvec(blend)) {
191 int n = _nmasters;
192 double val = 0;
193 for (int m = 0; m < n; m++)
194 val += blend[m] * _weight_vector[m];
195 if (force_integer)
196 val = floor(val + 0.50001);
197 def->set_num(val);
198 kill_def(blend_def, the_dict + dBlend);
199 } else if (def && !blend_def && force_integer) {
200 double val;
201 if (def->value_num(val))
202 def->set_num(floor(val + 0.50001));
203 }
204 }
205
206 void
207 MyFont::interpolate_dict_numvec(PermString name, Dict the_dict, int round_mode, bool executable)
208 {
209 Type1Definition *def = dict(the_dict, name);
210 Type1Definition *blend_def = dict(the_dict + dBlend, name);
211 Vector<NumVector> blend;
212
213 if (def && blend_def && blend_def->value_numvec_vec(blend)) {
214 int n = blend.size();
215 NumVector val;
216 for (int i = 0; i < n; i++) {
217 double d = 0;
218 for (int m = 0; m < _nmasters; m++)
219 d += blend[i][m] * _weight_vector[m];
220 if (round_mode == 2 && i < 2)
221 d = floor(d - 0.50001);
222 else if (round_mode)
223 d = floor(d + 0.50001);
224 val.push_back(d);
225 }
226 def->set_numvec(val, executable);
227 kill_def(blend_def, the_dict + dBlend);
228 }
229 }
230
231 void
232 MyFont::interpolate_dicts(bool force_integer, ErrorHandler *errh)
233 {
234 // Unfortunately, some programs (acroread) expect the FontBBox to consist
235 // of integers. Round its elements away from zero (this is what the
236 // Acrobat distiller seems to do).
237 interpolate_dict_numvec("FontBBox", dFont, 2, true);
238
239 interpolate_dict_numvec("BlueValues", dPrivate, force_integer);
240 interpolate_dict_numvec("OtherBlues", dPrivate, force_integer);
241 interpolate_dict_numvec("FamilyBlues", dPrivate, force_integer);
242 interpolate_dict_numvec("FamilyOtherBlues", dPrivate, force_integer);
243 interpolate_dict_numvec("StdHW", dPrivate);
244 interpolate_dict_numvec("StdVW", dPrivate);
245 interpolate_dict_numvec("StemSnapH", dPrivate);
246 interpolate_dict_numvec("StemSnapV", dPrivate);
247
248 interpolate_dict_num("BlueScale", dPrivate);
249 interpolate_dict_num("BlueShift", dPrivate, force_integer);
250 interpolate_dict_int("BlueFuzz", dPrivate, errh);
251
252 {
253 Type1Definition *def = p_dict("ForceBold");
254 Type1Definition *blend_def = bp_dict("ForceBold");
255 Type1Definition *thresh = p_dict("ForceBoldThreshold");
256 Vector<PermString> namevec;
257 double thresh_val;
258 if (def && blend_def && thresh && blend_def->value_namevec(namevec)
259 && thresh->value_num(thresh_val) && namevec.size() == _nmasters) {
260 double v = 0;
261 for (int m = 0; m < _nmasters; m++)
262 if (namevec[m] == "true")
263 v += _weight_vector[m];
264 def->set_code(v >= thresh_val ? "true" : "false");
265 kill_def(blend_def, dBlendPrivate);
266 }
267 }
268
269 interpolate_dict_num("UnderlinePosition", dFontInfo);
270 interpolate_dict_num("UnderlineThickness", dFontInfo);
271 interpolate_dict_num("ItalicAngle", dFontInfo);
272
273 if (Type1Definition *def = bp_dict("BuildCharArray"))
274 kill_def(def, dBlendPrivate);
275
276 for (DictHashMap::const_iterator i = dict_begin(dBlend); i; i++) {
277 PermString name = i.key();
278 if (i.value() && name != "Private" && name != "FontInfo"
279 && name != "ConvertDesignVector" && name != "NormalizeDesignVector")
280 errh->warning("didn%,t interpolate %s in Blend", name.c_str());
281 }
282
283 for (DictHashMap::const_iterator i = dict_begin(dBlendPrivate); i; i++)
284 if (i.value() && i.key() != "Erode")
285 errh->warning("didn%,t interpolate %s in BlendPrivate", i.key().c_str());
286
287 kill_def(p_dict("NDV"), dPrivate);
288 kill_def(p_dict("CDV"), dPrivate);
289 kill_def(p_dict("UniqueID"), dPrivate);
290 kill_def(fi_dict("BlendDesignPositions"), dFontInfo);
291 kill_def(fi_dict("BlendDesignMap"), dFontInfo);
292 kill_def(fi_dict("BlendAxisTypes"), dFontInfo);
293 }
294
295 void
296 MyFont::interpolate_charstrings(int precision, ErrorHandler *errh)
297 {
298 Type1MMRemover remover(this, _weight_vector, precision, errh);
299 remover.run();
300 }