Debian Codegraph @

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

raw
1 // -*- related-file-name: "../include/efont/t1mm.hh" -*-
2
3 /* t1mm.{cc,hh} -- Type 1 multiple master font information
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/t1mm.hh>
20 #include <efont/t1interp.hh>
21 #include <lcdf/error.hh>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 namespace Efont {
26
27 MultipleMasterSpace::MultipleMasterSpace(PermString fn, int na, int nm)
28 : CharstringProgram(1000),
29 _ok(false), _font_name(fn), _naxes(na), _nmasters(nm),
30 _axis_types(na, PermString()), _axis_labels(na, PermString()),
31 _design_vector(0), _norm_design_vector(0), _weight_vector(0)
32 {
33 }
34
35
36 void
37 MultipleMasterSpace::set_master_positions(const Vector<NumVector> &mp)
38 {
39 _master_positions = mp;
40 }
41
42 void
43 MultipleMasterSpace::set_normalize(const Vector<NumVector> &nin,
44 const Vector<NumVector> &nout)
45 {
46 _normalize_in = nin;
47 _normalize_out = nout;
48 }
49
50 void
51 MultipleMasterSpace::set_axis_type(int ax, PermString t)
52 {
53 _axis_types[ax] = t;
54 }
55
56 void
57 MultipleMasterSpace::set_axis_label(int ax, PermString t)
58 {
59 _axis_labels[ax] = t;
60 }
61
62 void
63 MultipleMasterSpace::set_design_vector(const NumVector &v)
64 {
65 _default_design_vector = v;
66 }
67
68 void
69 MultipleMasterSpace::set_weight_vector(const NumVector &v)
70 {
71 _default_weight_vector = v;
72 }
73
74
75 PermString
76 MultipleMasterSpace::axis_abbreviation(PermString atype)
77 {
78 if (atype == "Weight")
79 return "wt";
80 else if (atype == "Width")
81 return "wd";
82 else if (atype == "OpticalSize")
83 return "op";
84 else if (atype == "Style")
85 return "st";
86 else
87 return atype;
88 }
89
90
91 bool
92 MultipleMasterSpace::error(ErrorHandler *errh, const char *s, ...) const
93 {
94 if (errh) {
95 char buf[1024];
96 va_list val;
97 va_start(val, s);
98 assert(strlen(s) < 800);
99 snprintf(buf, sizeof(buf), (s[0] == ' ' ? "%.200s%s" : "%.200s: %s"),
100 _font_name.c_str(), s);
101 errh->xmessage(ErrorHandler::e_error, buf, val);
102 va_end(val);
103 }
104 return false;
105 }
106
107
108 bool
109 MultipleMasterSpace::check(ErrorHandler *errh)
110 {
111 if (_ok)
112 return true;
113
114 if (_nmasters <= 0 || _nmasters > 16)
115 return error(errh, "number of masters must be between 1 and 16");
116 if (_naxes <= 0 || _naxes > 4)
117 return error(errh, "number of axes must be between 1 and 4");
118
119 if (_master_positions.size() != _nmasters)
120 return error(errh, "bad BlendDesignPositions");
121 for (int i = 0; i < _nmasters; i++)
122 if (_master_positions[i].size() != _naxes)
123 return error(errh, "inconsistent BlendDesignPositions");
124
125 if (_normalize_in.size() != _naxes || _normalize_out.size() != _naxes)
126 return error(errh, "bad BlendDesignMap");
127 for (int i = 0; i < _naxes; i++)
128 if (_normalize_in[i].size() != _normalize_out[i].size())
129 return error(errh, "bad BlendDesignMap");
130
131 if (!_axis_types.size())
132 _axis_types.assign(_naxes, PermString());
133 if (_axis_types.size() != _naxes)
134 return error(errh, "bad BlendAxisTypes");
135
136 if (!_axis_labels.size())
137 _axis_labels.assign(_naxes, PermString());
138 if (_axis_labels.size() != _naxes)
139 return error(errh, "bad axis labels");
140
141 if (!_default_design_vector.size())
142 _default_design_vector.assign(_naxes, UNKDOUBLE);
143 if (_default_design_vector.size() != _naxes)
144 return error(errh, "inconsistent design vector");
145
146 if (!_default_weight_vector.size())
147 _default_weight_vector.assign(_nmasters, UNKDOUBLE);
148 if (_default_weight_vector.size() != _nmasters)
149 return error(errh, "inconsistent weight vector");
150
151 _ok = true;
152 return true;
153 }
154
155 bool
156 MultipleMasterSpace::check_intermediate(ErrorHandler *errh)
157 {
158 if (!_ok || _cdv)
159 return true;
160
161 for (int a = 0; a < _naxes; a++)
162 for (int m = 0; m < _nmasters; m++)
163 if (_master_positions[m][a] != 0 && _master_positions[m][a] != 1) {
164 if (errh)
165 errh->warning("%s requires intermediate master conversion programs",
166 _font_name.c_str());
167 return false;
168 }
169
170 return true;
171 }
172
173
174 int
175 MultipleMasterSpace::axis(PermString ax) const
176 {
177 for (int a = 0; a < _naxes; a++)
178 if (_axis_types[a] == ax || _axis_labels[a] == ax)
179 return a;
180 return -1;
181 }
182
183 double
184 MultipleMasterSpace::axis_low(int ax) const
185 {
186 return _normalize_in[ax][0];
187 }
188
189 double
190 MultipleMasterSpace::axis_high(int ax) const
191 {
192 return _normalize_in[ax].back();
193 }
194
195
196 Vector<double>
197 MultipleMasterSpace::empty_design_vector() const
198 {
199 return Vector<double>(_naxes, UNKDOUBLE);
200 }
201
202 bool
203 MultipleMasterSpace::set_design(NumVector &design_vector, int ax, double value,
204 ErrorHandler *errh) const
205 {
206 if (ax < 0 || ax >= _naxes)
207 return error(errh, " has only %d axes", _naxes);
208
209 if (value < axis_low(ax)) {
210 value = axis_low(ax);
211 if (errh)
212 errh->warning("raising %s's %s to %g", _font_name.c_str(),
213 _axis_types[ax].c_str(), value);
214 }
215 if (value > axis_high(ax)) {
216 value = axis_high(ax);
217 if (errh)
218 errh->warning("lowering %s's %s to %g", _font_name.c_str(),
219 _axis_types[ax].c_str(), value);
220 }
221
222 design_vector[ax] = value;
223 return true;
224 }
225
226 bool
227 MultipleMasterSpace::set_design(NumVector &design_vector, PermString ax_name,
228 double val, ErrorHandler *errh) const
229 {
230 int ax = axis(ax_name);
231 if (ax < 0)
232 return error(errh, " has no `%s' axis", ax_name.c_str());
233 else
234 return set_design(design_vector, ax, val, errh);
235 }
236
237
238 bool
239 MultipleMasterSpace::normalize_vector(ErrorHandler *errh) const
240 {
241 NumVector &design = *_design_vector;
242 NumVector &norm_design = *_norm_design_vector;
243
244 for (int a = 0; a < _naxes; a++)
245 if (!KNOWN(design[a])) {
246 if (errh)
247 errh->error("must specify %s's %s coordinate", _font_name.c_str(), _axis_types[a].c_str());
248 return false;
249 }
250
251 // Move to normalized design coordinates.
252 norm_design.assign(_naxes, UNKDOUBLE);
253
254 if (_ndv) {
255 CharstringInterp ai;
256 if (!ai.interpret(this, &_ndv))
257 return error(errh, "%s in NDV program", ai.error_string().c_str());
258
259 } else
260 for (int a = 0; a < _naxes; a++) {
261 double d = design[a];
262 double nd = UNKDOUBLE;
263 const Vector<double> &norm_in = _normalize_in[a];
264 const Vector<double> &norm_out = _normalize_out[a];
265
266 if (d < norm_in[0])
267 nd = norm_out[0];
268 for (int i = 1; i < norm_in.size(); i++)
269 if (d >= norm_in[i-1] && d < norm_in[i]) {
270 nd = norm_out[i-1]
271 + ((d - norm_in[i-1])
272 * (norm_out[i] - norm_out[i-1])
273 / (norm_in[i] - norm_in[i-1]));
274 goto done;
275 }
276 if (d >= norm_in.back())
277 nd = norm_out.back();
278
279 done:
280 norm_design[a] = nd;
281 }
282
283 for (int a = 0; a < _naxes; a++)
284 if (!KNOWN(norm_design[a]))
285 return error(errh, "bad normalization");
286
287 return true;
288 }
289
290
291 bool
292 MultipleMasterSpace::convert_vector(ErrorHandler *errh) const
293 {
294 NumVector &norm_design = *_norm_design_vector;
295 NumVector &weight = *_weight_vector;
296
297 weight.assign(_nmasters, 1);
298
299 if (_cdv) {
300 CharstringInterp ai;
301 if (!ai.interpret(this, &_cdv))
302 return error(errh, "%s in CDV program", ai.error_string().c_str());
303
304 } else
305 for (int a = 0; a < _naxes; a++)
306 for (int m = 0; m < _nmasters; m++) {
307 if (_master_positions[m][a] == 0)
308 weight[m] *= 1 - norm_design[a];
309 else if (_master_positions[m][a] == 1)
310 weight[m] *= norm_design[a];
311 else
312 return error(errh, " requires intermediate master conversion programs");
313 }
314
315 return true;
316 }
317
318
319 bool
320 MultipleMasterSpace::design_to_norm_design(const NumVector &design_in,
321 NumVector &norm_design,
322 ErrorHandler *errh) const
323 {
324 NumVector design(design_in);
325 NumVector weight;
326
327 _design_vector = &design;
328 _norm_design_vector = &norm_design;
329 _weight_vector = &weight;
330 if (!normalize_vector(errh))
331 return false;
332 _design_vector = _norm_design_vector = _weight_vector = 0;
333
334 return true;
335 }
336
337
338 bool
339 MultipleMasterSpace::design_to_weight(const NumVector &design_in, NumVector &weight, ErrorHandler *errh) const
340 {
341 NumVector design(design_in);
342 NumVector norm_design;
343
344 bool dirty = false;
345 for (int i = 0; i < _naxes; i++)
346 if (design[i] != _default_design_vector[i])
347 dirty = true;
348
349 if (dirty) {
350 _design_vector = &design;
351 _norm_design_vector = &norm_design;
352 _weight_vector = &weight;
353 if (!normalize_vector(errh))
354 return false;
355 if (!convert_vector(errh))
356 return false;
357 _design_vector = _norm_design_vector = _weight_vector = 0;
358 } else
359 weight = _default_weight_vector;
360
361 double sum = 0;
362 for (int m = 0; m < _nmasters; m++)
363 sum += weight[m];
364 if (sum < 0.9999 || sum > 1.0001)
365 return error(errh, "bad conversion: weight vector doesn't sum to 1");
366
367 // adjust weight vector to max 4 decimal digits of precision, and make it
368 // sum to exactly 1
369 sum = 0;
370 for (int m = 0; m < _nmasters - 1; m++) {
371 weight[m] = floor(weight[m] * 10000. + 0.5) / 10000.;
372 sum += weight[m];
373 }
374 weight[_nmasters - 1] = 1 - sum;
375
376 return true;
377 }
378
379
380 Vector<double> *
381 MultipleMasterSpace::mm_vector(VectorType t, bool writable) const
382 {
383 if (t == VEC_WEIGHT)
384 return _weight_vector;
385 else if (t == VEC_NORM_DESIGN)
386 return _norm_design_vector;
387 else if (t == VEC_DESIGN && !writable)
388 return _design_vector;
389 else
390 return 0;
391 }
392
393 }