File: t1mm.cc

package info (click to toggle)
lcdf-typetools 2.105~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,100 kB
  • ctags: 4,798
  • sloc: cpp: 35,107; ansic: 1,861; sh: 1,254; makefile: 269
file content (393 lines) | stat: -rw-r--r-- 10,751 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// -*- related-file-name: "../include/efont/t1mm.hh" -*-

/* t1mm.{cc,hh} -- Type 1 multiple master font information
 *
 * Copyright (c) 1998-2014 Eddie Kohler
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <efont/t1mm.hh>
#include <efont/t1interp.hh>
#include <lcdf/error.hh>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
namespace Efont {

MultipleMasterSpace::MultipleMasterSpace(PermString fn, int na, int nm)
    : CharstringProgram(1000),
      _ok(false), _font_name(fn), _naxes(na), _nmasters(nm),
      _axis_types(na, PermString()), _axis_labels(na, PermString()),
      _design_vector(0), _norm_design_vector(0), _weight_vector(0)
{
}


void
MultipleMasterSpace::set_master_positions(const Vector<NumVector> &mp)
{
    _master_positions = mp;
}

void
MultipleMasterSpace::set_normalize(const Vector<NumVector> &nin,
                            const Vector<NumVector> &nout)
{
    _normalize_in = nin;
    _normalize_out = nout;
}

void
MultipleMasterSpace::set_axis_type(int ax, PermString t)
{
    _axis_types[ax] = t;
}

void
MultipleMasterSpace::set_axis_label(int ax, PermString t)
{
    _axis_labels[ax] = t;
}

void
MultipleMasterSpace::set_design_vector(const NumVector &v)
{
    _default_design_vector = v;
}

void
MultipleMasterSpace::set_weight_vector(const NumVector &v)
{
    _default_weight_vector = v;
}


PermString
MultipleMasterSpace::axis_abbreviation(PermString atype)
{
    if (atype == "Weight")
        return "wt";
    else if (atype == "Width")
        return "wd";
    else if (atype == "OpticalSize")
        return "op";
    else if (atype == "Style")
        return "st";
    else
        return atype;
}


bool
MultipleMasterSpace::error(ErrorHandler *errh, const char *s, ...) const
{
    if (errh) {
        char buf[1024];
        va_list val;
        va_start(val, s);
        assert(strlen(s) < 800);
        sprintf(buf, (s[0] == ' ' ? "%.200s%s" : "%.200s: %s"),
                _font_name.c_str(), s);
        errh->vxmessage(ErrorHandler::e_error, buf, val);
        va_end(val);
    }
    return false;
}


bool
MultipleMasterSpace::check(ErrorHandler *errh)
{
    if (_ok)
        return true;

    if (_nmasters <= 0 || _nmasters > 16)
        return error(errh, "number of masters must be between 1 and 16");
    if (_naxes <= 0 || _naxes > 4)
        return error(errh, "number of axes must be between 1 and 4");

    if (_master_positions.size() != _nmasters)
        return error(errh, "bad BlendDesignPositions");
    for (int i = 0; i < _nmasters; i++)
        if (_master_positions[i].size() != _naxes)
            return error(errh, "inconsistent BlendDesignPositions");

    if (_normalize_in.size() != _naxes || _normalize_out.size() != _naxes)
        return error(errh, "bad BlendDesignMap");
    for (int i = 0; i < _naxes; i++)
        if (_normalize_in[i].size() != _normalize_out[i].size())
            return error(errh, "bad BlendDesignMap");

    if (!_axis_types.size())
        _axis_types.assign(_naxes, PermString());
    if (_axis_types.size() != _naxes)
        return error(errh, "bad BlendAxisTypes");

    if (!_axis_labels.size())
        _axis_labels.assign(_naxes, PermString());
    if (_axis_labels.size() != _naxes)
        return error(errh, "bad axis labels");

    if (!_default_design_vector.size())
        _default_design_vector.assign(_naxes, UNKDOUBLE);
    if (_default_design_vector.size() != _naxes)
        return error(errh, "inconsistent design vector");

    if (!_default_weight_vector.size())
        _default_weight_vector.assign(_nmasters, UNKDOUBLE);
    if (_default_weight_vector.size() != _nmasters)
        return error(errh, "inconsistent weight vector");

    _ok = true;
    return true;
}

bool
MultipleMasterSpace::check_intermediate(ErrorHandler *errh)
{
    if (!_ok || _cdv)
        return true;

    for (int a = 0; a < _naxes; a++)
        for (int m = 0; m < _nmasters; m++)
            if (_master_positions[m][a] != 0 && _master_positions[m][a] != 1) {
                if (errh)
                    errh->warning("%s requires intermediate master conversion programs",
                                  _font_name.c_str());
                return false;
            }

    return true;
}


int
MultipleMasterSpace::axis(PermString ax) const
{
    for (int a = 0; a < _naxes; a++)
        if (_axis_types[a] == ax || _axis_labels[a] == ax)
            return a;
    return -1;
}

double
MultipleMasterSpace::axis_low(int ax) const
{
    return _normalize_in[ax][0];
}

double
MultipleMasterSpace::axis_high(int ax) const
{
    return _normalize_in[ax].back();
}


Vector<double>
MultipleMasterSpace::empty_design_vector() const
{
    return Vector<double>(_naxes, UNKDOUBLE);
}

bool
MultipleMasterSpace::set_design(NumVector &design_vector, int ax, double value,
                          ErrorHandler *errh) const
{
    if (ax < 0 || ax >= _naxes)
        return error(errh, " has only %d axes", _naxes);

    if (value < axis_low(ax)) {
        value = axis_low(ax);
        if (errh)
            errh->warning("raising %s's %s to %g", _font_name.c_str(),
                          _axis_types[ax].c_str(), value);
    }
    if (value > axis_high(ax)) {
        value = axis_high(ax);
        if (errh)
            errh->warning("lowering %s's %s to %g", _font_name.c_str(),
                          _axis_types[ax].c_str(), value);
    }

    design_vector[ax] = value;
    return true;
}

bool
MultipleMasterSpace::set_design(NumVector &design_vector, PermString ax_name,
                          double val, ErrorHandler *errh) const
{
    int ax = axis(ax_name);
    if (ax < 0)
        return error(errh, " has no `%s' axis", ax_name.c_str());
    else
        return set_design(design_vector, ax, val, errh);
}


bool
MultipleMasterSpace::normalize_vector(ErrorHandler *errh) const
{
    NumVector &design = *_design_vector;
    NumVector &norm_design = *_norm_design_vector;

    for (int a = 0; a < _naxes; a++)
        if (!KNOWN(design[a])) {
            if (errh)
                errh->error("must specify %s's %s coordinate", _font_name.c_str(), _axis_types[a].c_str());
            return false;
        }

    // Move to normalized design coordinates.
    norm_design.assign(_naxes, UNKDOUBLE);

    if (_ndv) {
        CharstringInterp ai;
        if (!ai.interpret(this, &_ndv))
            return error(errh, "%s in NDV program", ai.error_string().c_str());

    } else
        for (int a = 0; a < _naxes; a++) {
            double d = design[a];
            double nd = UNKDOUBLE;
            const Vector<double> &norm_in = _normalize_in[a];
            const Vector<double> &norm_out = _normalize_out[a];

            if (d < norm_in[0])
                nd = norm_out[0];
            for (int i = 1; i < norm_in.size(); i++)
                if (d >= norm_in[i-1] && d < norm_in[i]) {
                    nd = norm_out[i-1]
                        + ((d - norm_in[i-1])
                           * (norm_out[i] - norm_out[i-1])
                           / (norm_in[i] - norm_in[i-1]));
                    goto done;
                }
            if (d >= norm_in.back())
                nd = norm_out.back();

          done:
            norm_design[a] = nd;
        }

    for (int a = 0; a < _naxes; a++)
        if (!KNOWN(norm_design[a]))
            return error(errh, "bad normalization");

    return true;
}


bool
MultipleMasterSpace::convert_vector(ErrorHandler *errh) const
{
    NumVector &norm_design = *_norm_design_vector;
    NumVector &weight = *_weight_vector;

    weight.assign(_nmasters, 1);

    if (_cdv) {
        CharstringInterp ai;
        if (!ai.interpret(this, &_cdv))
            return error(errh, "%s in CDV program", ai.error_string().c_str());

    } else
        for (int a = 0; a < _naxes; a++)
            for (int m = 0; m < _nmasters; m++) {
                if (_master_positions[m][a] == 0)
                    weight[m] *= 1 - norm_design[a];
                else if (_master_positions[m][a] == 1)
                    weight[m] *= norm_design[a];
                else
                    return error(errh, " requires intermediate master conversion programs");
            }

    return true;
}


bool
MultipleMasterSpace::design_to_norm_design(const NumVector &design_in,
                                           NumVector &norm_design,
                                           ErrorHandler *errh) const
{
    NumVector design(design_in);
    NumVector weight;

    _design_vector = &design;
    _norm_design_vector = &norm_design;
    _weight_vector = &weight;
    if (!normalize_vector(errh))
        return false;
    _design_vector = _norm_design_vector = _weight_vector = 0;

    return true;
}


bool
MultipleMasterSpace::design_to_weight(const NumVector &design_in, NumVector &weight, ErrorHandler *errh) const
{
    NumVector design(design_in);
    NumVector norm_design;

    bool dirty = false;
    for (int i = 0; i < _naxes; i++)
        if (design[i] != _default_design_vector[i])
            dirty = true;

    if (dirty) {
        _design_vector = &design;
        _norm_design_vector = &norm_design;
        _weight_vector = &weight;
        if (!normalize_vector(errh))
            return false;
        if (!convert_vector(errh))
            return false;
        _design_vector = _norm_design_vector = _weight_vector = 0;
    } else
        weight = _default_weight_vector;

    double sum = 0;
    for (int m = 0; m < _nmasters; m++)
        sum += weight[m];
    if (sum < 0.9999 || sum > 1.0001)
        return error(errh, "bad conversion: weight vector doesn't sum to 1");

    // adjust weight vector to max 4 decimal digits of precision, and make it
    // sum to exactly 1
    sum = 0;
    for (int m = 0; m < _nmasters - 1; m++) {
        weight[m] = floor(weight[m] * 10000. + 0.5) / 10000.;
        sum += weight[m];
    }
    weight[_nmasters - 1] = 1 - sum;

    return true;
}


Vector<double> *
MultipleMasterSpace::mm_vector(VectorType t, bool writable) const
{
    if (t == VEC_WEIGHT)
        return _weight_vector;
    else if (t == VEC_NORM_DESIGN)
        return _norm_design_vector;
    else if (t == VEC_DESIGN && !writable)
        return _design_vector;
    else
        return 0;
}

}