summaryrefslogtreecommitdiff
path: root/Build/source/texk/lcdf-typetools/lcdf-typetools-src/libefont/t1cs.cc
blob: f4060e607b913b62c5c584cfafc2264bbfaba059 (plain)
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
// -*- related-file-name: "../include/efont/t1cs.hh" -*-

/* t1cs.{cc,hh} -- Type 1/2 charstrings
 *
 * Copyright (c) 1998-2019 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/t1cs.hh>
#include <efont/t1interp.hh>
#include <string.h>
namespace Efont {

const char * const Charstring::command_names[] = {
    "error", "hstem", "UNKNOWN_2", "vstem", "vmoveto",
    "rlineto", "hlineto", "vlineto", "rrcurveto", "closepath",

    "callsubr", "return", "escape", "hsbw", "endchar",
    "UNKNOWN_15", "blend", "UNKNOWN_17", "hstemhm", "hintmask",

    "cntrmask", "rmoveto", "hmoveto", "vstemhm", "rcurveline",
    "rlinecurve", "vvcurveto", "hhcurveto", "shortint", "callgsubr",

    "vhcurveto", "hvcurveto", "dotsection", "vstem3", "hstem3",
    "and", "or", "not", "seac", "sbw",

    "store", "abs", "add", "sub", "div",
    "load", "neg", "eq", "callothersubr", "pop",

    "drop", "UNKNOWN_12_19", "put", "get", "ifelse",
    "random", "mul", "UNKNOWN_12_25", "sqrt", "dup",

    "exch", "index", "roll", "UNKNOWN_12_31", "UNKNOWN_12_32",
    "setcurrentpoint", "hflex", "flex", "hflex1", "flex1"
};

const char * const Charstring::standard_encoding[256] = {
    /* 00x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 01x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 02x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 03x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 04x */ "space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
    "ampersand", "quoteright",
    /* 05x */ "parenleft", "parenright", "asterisk", "plus", "comma", "hyphen",
    "period", "slash",
    /* 06x */ "zero", "one", "two", "three", "four", "five", "six", "seven",
    /* 07x */ "eight", "nine", "colon", "semicolon", "less", "equal",
    "greater", "question",
    /* 10x */ "at", "A", "B", "C", "D", "E", "F", "G",
    /* 11x */ "H", "I", "J", "K", "L", "M", "N", "O",
    /* 12x */ "P", "Q", "R", "S", "T", "U", "V", "W",
    /* 13x */ "X", "Y", "Z", "bracketleft", "backslash", "bracketright",
    "asciicircum", "underscore",
    /* 14x */ "quoteleft", "a", "b", "c", "d", "e", "f", "g",
    /* 15x */ "h", "i", "j", "k", "l", "m", "n", "o",
    /* 16x */ "p", "q", "r", "s", "t", "u", "v", "w",
    /* 17x */ "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", 0,
    /* 20x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 21x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 22x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 23x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 24x */ 0, "exclamdown", "cent", "sterling", "fraction", "yen",
    "florin", "section",
    /* 25x */ "currency", "quotesingle", "quotedblleft", "guillemotleft",
    "guilsinglleft", "guilsinglright", "fi", "fl",
    /* 26x */ 0, "endash", "dagger", "daggerdbl", "periodcentered", 0,
    "paragraph", "bullet",
    /* 27x */ "quotesinglbase", "quotedblbase", "quotedblright",
    "guillemotright", "ellipsis", "perthousand", 0, "questiondown",
    /* 30x */ 0, "grave", "acute", "circumflex", "tilde", "macron",
    "breve", "dotaccent",
    /* 31x */ "dieresis", 0, "ring", "cedilla", 0, "hungarumlaut",
    "ogonek", "caron",
    /* 32x */ "emdash", 0, 0, 0, 0, 0, 0, 0,
    /* 33x */ 0, 0, 0, 0, 0, 0, 0, 0,
    /* 34x */ 0, "AE", 0, "ordfeminine", 0, 0, 0, 0,
    /* 35x */ "Lslash", "Oslash", "OE", "ordmasculine", 0, 0, 0, 0,
    /* 36x */ 0, "ae", 0, 0, 0, "dotlessi", 0, 0,
    /* 37x */ "lslash", "oslash", "oe", "germandbls", 0, 0, 0, 0,
};


Charstring::~Charstring()
{
}

String
Charstring::command_name(int cmd)
{
    if (cmd >= 0 && cmd <= cLastCommand)
        return command_names[cmd];
    else if (cmd < cEscapeDelta + 256)
        return String("COMMAND_12_") + String(cmd - cEscapeDelta);
    else
        return String("<INVALID>");
}


Type1Charstring::Type1Charstring(int lenIV, const String &s)
    : Charstring(), _key(-1)
{
    if (lenIV < 0)              // no charstring encryption
        _s = s;
    else if (lenIV < s.length()) {
        const unsigned char *d = reinterpret_cast<const unsigned char*>(s.data());
        _key = t1R_cs;
        for (int i = 0; i < lenIV; i++, d++)
            _key = ((*d + _key) * (uint32_t) t1C1 + t1C2) & 0xFFFF;
        _s = s.substring(lenIV);
    }
}

void
Type1Charstring::prepend(const Type1Charstring &t1cs)
{
    if (_key >= 0)
        decrypt();
    if (t1cs._key >= 0)
        t1cs.decrypt();
    _s = t1cs._s + _s;
}

void
Type1Charstring::decrypt() const
{
    if (_key >= 0) {
        int r = _key;
        uint8_t *d = reinterpret_cast<uint8_t *>(_s.mutable_data());
        for (int i = 0; i < _s.length(); i++, d++) {
            uint8_t encrypted = *d;
            *d = encrypted ^ (r >> 8);
            r = ((encrypted + r) * (uint32_t) t1C1 + t1C2) & 0xFFFF;
        }
        _key = -1;
    }
}

bool
Type1Charstring::process(CharstringInterp &interp) const
{
    const uint8_t *data = Type1Charstring::data();
    int left = _s.length();

    while (left > 0) {
        bool more;
        int ahead;

        if (*data >= 32 && *data <= 246) {              // push small number
            more = interp.number(data[0] - 139);
            ahead = 1;

        } else if (*data < 32) {                        // a command
            if (*data == cEscape) {
                if (left < 2)
                    goto runoff_error;
                more = interp.type1_command(cEscapeDelta + data[1]);
                ahead = 2;
            } else if (*data == cShortint) { // short integer
                if (left < 3)
                    goto runoff_error;
                int16_t val = (data[1] << 8) | data[2];
                more = interp.number(val);
                ahead = 3;
            } else {
                more = interp.type1_command(data[0]);
                ahead = 1;
            }

        } else if (*data >= 247 && *data <= 250) {      // push medium number
            if (left < 2)
                goto runoff_error;
            int val = + ((data[0] - 247) << 8) + 108 + data[1];
            more = interp.number(val);
            ahead = 2;

        } else if (*data >= 251 && *data <= 254) {      // push negative medium number
            if (left < 2)
                goto runoff_error;
            int val = - ((data[0] - 251) << 8) - 108 - data[1];
            more = interp.number(val);
            ahead = 2;

        } else {                                        // 255: push huge number
            if (left < 5)
                goto runoff_error;
            int32_t val = ((uint32_t) data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
            more = interp.number(val);
            ahead = 5;
        }

        if (!more)
            return interp.error() == CharstringInterp::errOK;

        data += ahead;
        left -= ahead;
    }

  runoff_error:
    interp.error(CharstringInterp::errRunoff);
    return false;
}

int
Type1Charstring::first_caret_after(int pos) const
{
    const uint8_t *data = Type1Charstring::data();
    const uint8_t *edata = data + (pos < length() ? pos : length());

    while (data < edata) {
        if (*data >= 32 && *data <= 246)        // push small number
            data++;
        else if (*data < 32) {                  // a command
            if (*data == cEscape)
                data += 2;
            else if (*data == cShortint)
                data += 3;
            else
                data++;
        } else if (*data >= 247 && *data <= 254)        // push medium number
            data += 2;
        else                                    // 255: push huge number
            data += 5;
    }

    const uint8_t *odata = Type1Charstring::data();
    return (data > odata + length() ? length() : data - odata);
}

void
Type1Charstring::assign_substring(int pos, int len, const String &cs)
{
    if (_key >= 0)
        decrypt();
    if (pos < 0 || len < 0 || pos + len >= _s.length())
        /* do nothing */;
    else if (cs.length() == len) {
        char *d = _s.mutable_data();
        memcpy(d + pos, cs.data(), cs.length());
    } else if (cs.length() <= len) {
        char *d = _s.mutable_data();
        memcpy(d + pos, cs.data(), cs.length());
        memmove(d + pos + cs.length(), d + pos + len, _s.length() - pos - len);
        _s = _s.substring(0, cs.length() - len);
    } else
        _s = _s.substring(0, pos) + cs + _s.substring(pos + len);
}


bool
Type2Charstring::process(CharstringInterp &interp) const
{
    const uint8_t *data = Type2Charstring::data();
    int left = _s.length();

    while (left > 0) {
        bool more;
        int ahead;

        if (*data >= 32 && *data <= 246) {              // push small number
            more = interp.number(data[0] - 139);
            ahead = 1;

        } else if (*data < 32) {                        // a command
            if (*data == cEscape) {
                if (left < 2)
                    goto runoff_error;
                more = interp.type2_command(cEscapeDelta + data[1], 0, 0);
                ahead = 2;
            } else if (*data == cShortint) { // short integer
                if (left < 3)
                    goto runoff_error;
                int16_t val = (data[1] << 8) | data[2];
                more = interp.number(val);
                ahead = 3;
            } else if (*data == cHintmask || *data == cCntrmask) {
                int left_ptr = left - 1;
                more = interp.type2_command(data[0], data + 1, &left_ptr);
                ahead = 1 + (left - 1) - left_ptr;
            } else {
                more = interp.type2_command(data[0], 0, 0);
                ahead = 1;
            }

        } else if (*data >= 247 && *data <= 250) {      // push medium number
            if (left < 2)
                goto runoff_error;
            int val = + ((data[0] - 247) << 8) + 108 + data[1];
            more = interp.number(val);
            ahead = 2;

        } else if (*data >= 251 && *data <= 254) {      // push negative medium number
            if (left < 2)
                goto runoff_error;
            int val = - ((data[0] - 251) << 8) - 108 - data[1];
            more = interp.number(val);
            ahead = 2;

        } else {                                        // 255: push huge number
            if (left < 5)
                goto runoff_error;
            int32_t val = (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
            more = interp.number(val / 65536.);
            ahead = 5;
        }

        if (!more)
            return interp.error() == CharstringInterp::errOK;

        data += ahead;
        left -= ahead;
    }

  runoff_error:
    interp.error(CharstringInterp::errRunoff);
    return false;
}


CharstringProgram::CharstringProgram(unsigned units_per_em)
    : _parent_program(false),
      _units_per_em(units_per_em ? units_per_em : 1000) {
}

const CharstringProgram *
CharstringProgram::child_program(int) const
{
    return this;
}

void
CharstringProgram::font_matrix(double matrix[6]) const
{
    matrix[0] = matrix[3] = 0.001;
    matrix[1] = matrix[2] = matrix[4] = matrix[5] = 0;
}

void
CharstringProgram::glyph_names(Vector<PermString> &gnames) const
{
    int n = nglyphs();
    gnames.resize(n);
    for (int i = 0; i < n; i++)
        gnames[i] = glyph_name(i);
}

Vector<double> *
CharstringProgram::mm_vector(VectorType, bool) const
{
    return 0;
}

double
CharstringProgram::global_width_x(bool) const
{
    return UNKDOUBLE;
}

}