Debian Codegraph @

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

raw
1 // -*- related-file-name: "../include/efont/t1rw.hh" -*-
2
3 /* t1rw.{cc,hh} -- Type 1 font reading and writing
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/t1rw.hh>
20 #include <efont/t1cs.hh>
21 #include <lcdf/straccum.hh>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <string.h>
25 namespace Efont {
26
27 unsigned char Type1Reader::xvalue_store[257];
28 unsigned char *Type1Reader::xvalue = &Type1Reader::xvalue_store[1];
29
30 void
31 Type1Reader::static_initialize()
32 {
33 // rely on static data being initialized to 0
34 if (!xvalue[(unsigned char) 'A']) {
35 xvalue[(unsigned char) '0'] = 0;
36 xvalue[(unsigned char) '1'] = 1;
37 xvalue[(unsigned char) '2'] = 2;
38 xvalue[(unsigned char) '3'] = 3;
39 xvalue[(unsigned char) '4'] = 4;
40 xvalue[(unsigned char) '5'] = 5;
41 xvalue[(unsigned char) '6'] = 6;
42 xvalue[(unsigned char) '7'] = 7;
43 xvalue[(unsigned char) '8'] = 8;
44 xvalue[(unsigned char) '9'] = 9;
45 xvalue[(unsigned char) 'A'] = 10;
46 xvalue[(unsigned char) 'B'] = 11;
47 xvalue[(unsigned char) 'C'] = 12;
48 xvalue[(unsigned char) 'D'] = 13;
49 xvalue[(unsigned char) 'E'] = 14;
50 xvalue[(unsigned char) 'F'] = 15;
51 xvalue[(unsigned char) 'a'] = 10;
52 xvalue[(unsigned char) 'b'] = 11;
53 xvalue[(unsigned char) 'c'] = 12;
54 xvalue[(unsigned char) 'd'] = 13;
55 xvalue[(unsigned char) 'e'] = 14;
56 xvalue[(unsigned char) 'f'] = 15;
57 }
58 }
59
60
61 Type1Reader::Type1Reader()
62 : _data(new unsigned char[DATA_SIZE]), _len(0), _pos(0),
63 _ungot(-1), _eexec(false)
64 {
65 static_initialize();
66 }
67
68 Type1Reader::~Type1Reader()
69 {
70 delete[] _data;
71 }
72
73
74 void
75 Type1Reader::set_charstring_definer(PermString x)
76 {
77 _charstring_definer = x;
78 }
79
80 void
81 Type1Reader::switch_eexec(bool on, unsigned char *data, int len)
82 {
83 if (on) {
84 if (_pos < len + 3) {
85 unsigned char *new_data = new unsigned char[len + 3 + DATA_SIZE];
86 assert(_len <= DATA_SIZE);
87 memcpy(new_data + len + 3, _data + _pos, _len - _pos);
88 _len = len + 3 + _len - _pos;
89 _pos = len + 3;
90 delete[] _data;
91 _data = new_data;
92 }
93 int original_pos = _pos;
94 // don't forget _ungot!!
95 if (_ungot >= 0)
96 _data[--_pos] = _ungot, _ungot = -1;
97 if (_crlf == 0 || _crlf == 2)
98 _data[--_pos] = '\n';
99 if (_crlf == 1 || _crlf == 2)
100 _data[--_pos] = '\r';
101 memcpy(_data + _pos - len, data, len);
102 _pos -= len;
103 start_eexec(original_pos - _pos);
104 }
105 _eexec = on;
106 }
107
108
109 int
110 Type1Reader::more_data()
111 {
112 _pos = 0;
113 _len = more_data(_data, DATA_SIZE);
114 if (_len < 0)
115 return -1;
116 else
117 return _data[_pos++];
118 }
119
120
121 inline int
122 Type1Reader::get_base()
123 {
124 if (_pos >= _len)
125 return more_data();
126 else
127 return _data[_pos++];
128 }
129
130
131 inline int
132 Type1Reader::eexec(int c)
133 {
134 unsigned char answer = (unsigned char)(c ^ (_r >> 8));
135 _r = (((unsigned char)c + _r) * (uint32_t) t1C1 + t1C2) & 0xFFFF;
136 return answer;
137 }
138
139
140 int
141 Type1Reader::ascii_eexec_get()
142 {
143 int d1 = get_base();
144 while (isspace(d1))
145 d1 = get_base();
146
147 int d2 = get_base();
148 while (isspace(d2))
149 d2 = get_base();
150 if (d2 < 0)
151 return -1;
152
153 return eexec((xvalue[d1] << 4) | (xvalue[d2]));
154 }
155
156
157 inline int
158 Type1Reader::get()
159 {
160 if (!_eexec)
161 return get_base();
162
163 else if (_binary_eexec) {
164 int c = get_base();
165 return c < 0 ? c : eexec(c);
166
167 } else
168 return ascii_eexec_get();
169 }
170
171
172 void
173 Type1Reader::start_eexec(int initial_ascii)
174 {
175 /* God damn this _ungot thing!! It makes sense not to check it on every
176 char fetch, since it can only be set at the end of next_line; therefore,
177 only the first get() into a user-visible function might need to check
178 _ungot. The problem is I forgot start_eexec() was such a function! */
179 int c = _ungot < 0 ? get_base() : _ungot;
180 initial_ascii--;
181 _ungot = -1;
182
183 /* Strictly speaking, I should look for whitespace even in binary sections
184 of PFB fonts; but it turns out some PFBs would be unreadable with that
185 pedantic rule. */
186 while (isspace(c) && (initial_ascii >= 0 || !preserve_whitespace()))
187 c = get_base(), initial_ascii--;
188
189 /* Differentiate between ASCII eexec and binary eexec. */
190 int rand_bytes[4];
191 _binary_eexec = false;
192 for (int i = 0; i < 4; i++) {
193 if (i)
194 c = get_base();
195 rand_bytes[i] = c;
196 if (!isxdigit(c))
197 _binary_eexec = true;
198 }
199
200 _r = t1R_ee;
201 if (_binary_eexec)
202 for (int i = 0; i < 4; i++)
203 eexec(rand_bytes[i]);
204 else {
205 for (int i = 0; i < 2; i++) {
206 c = xvalue[rand_bytes[i*2]] * 16 + xvalue[rand_bytes[i*2+1]];
207 eexec(c);
208 }
209 ascii_eexec_get();
210 ascii_eexec_get();
211 }
212 }
213
214
215 bool
216 Type1Reader::test_charstring(StringAccum &str)
217 {
218 /* PERFORMANCE NOTE: This function is definitely a bottleneck. Making it
219 more efficient cut time by about 40%. */
220
221 if (!_charstring_definer)
222 return false;
223 if (_charstring_len >= 0)
224 return str.length() <= _charstring_start + _charstring_len;
225
226 str.append('\0'); // protect against running off end of string
227 char *s = str.data();
228 char *start;
229 while (*s == ' ')
230 s++;
231
232 if (s[0] == '/')
233 s++;
234 else if (s[0] == 'd' && s[1] == 'u' && s[2] == 'p' && isspace((unsigned char) s[3])) {
235 s += 4;
236 // 17.Jan.2000 -- some fonts have extra space here.
237 while (isspace((unsigned char) *s))
238 s++;
239 } else
240 goto not_charstring;
241
242 // Force one literal space rather than isspace().
243 // Why? Consistency: we force 1 literal space around _charstring_definer.
244 while (*s != ' ' && *s)
245 s++;
246 if (*s++ != ' ' || !isdigit((unsigned char) *s))
247 goto not_charstring;
248 start = s;
249 s++;
250 while (*s != ' ' && *s)
251 s++;
252 if (strncmp(s, _charstring_definer.c_str(), _charstring_definer.length()) != 0)
253 goto not_charstring;
254
255 _charstring_len = strtol(start, 0, 10);
256 _charstring_start = (s - str.data()) + _charstring_definer.length();
257 str.pop_back();
258 return str.length() <= _charstring_start + _charstring_len;
259
260 not_charstring:
261 str.pop_back();
262 return false;
263 }
264
265
266 /* PERFORMANCE NOTE: All kinds of speedup tricks tried with eexec -- buffering
267 (doing eexec processing at more_data time), etc., and all this stuff -- and
268 found to have essentially zero effect for PFB fonts. Now, it might be
269 effective for PFAs; but who cares? I hate PFAs. */
270
271 bool
272 Type1Reader::next_line(StringAccum &s)
273 {
274 if (_len < 0)
275 return false;
276
277 // Can't be a charstring if incoming accumulator has nonzero length.
278 _charstring_start = 0;
279 _charstring_len = (s.length() > 0 ? 0 : -1);
280
281 int first_char = _ungot < 0 ? get() : _ungot;
282 _ungot = -1;
283
284 for (int c = first_char; c >= 0; c = get())
285 switch (c) {
286
287 case '\n':
288 if (test_charstring(s))
289 goto normal;
290 else {
291 _crlf = 0;
292 goto done;
293 }
294
295 case '\r':
296 // check for \r\n (counts as only one line ending)
297 if (test_charstring(s))
298 goto normal;
299 c = get();
300 if (c != '\n' || preserve_whitespace())
301 _ungot = c, _crlf = 1;
302 else
303 _crlf = 2;
304 goto done;
305
306 normal:
307 default:
308 s.append((char)c);
309 break;
310
311 }
312
313 done:
314 return true;
315 }
316
317
318 int
319 Type1Reader::get_data(unsigned char *data, int len)
320 {
321 if (_len < 0)
322 return -1;
323 if (len <= 0)
324 return 0;
325
326 int pos = 0;
327 if (_ungot >= 0) {
328 *data++ = _ungot;
329 pos++;
330 _ungot = -1;
331 }
332
333 for (; pos < len; pos++) {
334 int c = get();
335 if (c < 0)
336 break;
337 *data++ = c;
338 }
339
340 return pos;
341 }
342
343
344 /*****
345 * Type1PFAReader
346 **/
347
348 Type1PFAReader::Type1PFAReader(FILE *f)
349 : _f(f)
350 {
351 }
352
353 int
354 Type1PFAReader::more_data(unsigned char *data, int len)
355 {
356 size_t size = fread(data, 1, len, _f);
357 return size ? (int)size : -1;
358 }
359
360
361 /*****
362 * Type1PFBReader
363 **/
364
365 Type1PFBReader::Type1PFBReader(FILE *f)
366 : _f(f), _binary(false), _left(0)
367 {
368 }
369
370 int
371 Type1PFBReader::more_data(unsigned char *data, int len)
372 {
373 while (_left == 0) {
374 int c = getc(_f);
375 if (c != 128)
376 return -1;
377
378 c = getc(_f);
379 if (c == 3 || c < 1 || c > 3)
380 return -1;
381 _binary = (c == 2);
382
383 _left = getc(_f);
384 _left |= getc(_f) << 8;
385 _left |= getc(_f) << 16;
386 _left |= getc(_f) << 24;
387 }
388
389 if (_left < 0)
390 return -1;
391
392 if (len > _left)
393 len = _left;
394 _left -= len;
395
396 return fread(data, 1, len, _f);
397 }
398
399 bool
400 Type1PFBReader::preserve_whitespace() const
401 {
402 return _binary;
403 }
404
405
406 /*****
407 * Type1SubsetReader
408 **/
409
410 Type1SubsetReader::Type1SubsetReader(Type1Reader *r, int left)
411 : _reader(r), _left(left)
412 {
413 }
414
415 int
416 Type1SubsetReader::more_data(unsigned char *data, int len)
417 {
418 if (_left <= 0)
419 return -1;
420 if (len > _left)
421 len = _left;
422 int how_much = _reader->get_data(data, len);
423 if (how_much > 0)
424 _left -= how_much;
425 return how_much;
426 }
427
428 bool
429 Type1SubsetReader::preserve_whitespace() const
430 {
431 return _reader->preserve_whitespace();
432 }
433
434
435 /*****
436 * Writer
437 **/
438
439 Type1Writer::Type1Writer()
440 : _buf(new unsigned char[BufSize]), _pos(0),
441 _eexec(false), _eexec_start(-1), _eexec_end(-1), _lenIV(4)
442 {
443 }
444
445
446 Type1Writer::~Type1Writer()
447 {
448 assert(!_pos);
449 delete[] _buf;
450 }
451
452
453 inline unsigned char
454 Type1Writer::eexec(int p)
455 {
456 unsigned char c = ((unsigned char)p ^ (_r >> 8)) & 0xFF;
457 _r = ((c + _r) * (uint32_t) t1C1 + t1C2) & 0xFFFF;
458 return c;
459 }
460
461
462 void
463 Type1Writer::flush()
464 {
465 local_flush();
466 }
467
468
469 /* PERFORMANCE NOTE: Doing eexec processing during flush -- which streamlines
470 code for the print() methods -- seems to save some time (4-5%). It also
471 makes write performance more consistent. But I have mixed feelings. */
472
473 void
474 Type1Writer::local_flush()
475 {
476 if (_eexec_start >= 0 && _eexec_end < 0)
477 _eexec_end = _pos;
478 for (int p = _eexec_start; p < _eexec_end; p++)
479 _buf[p] = eexec(_buf[p]);
480 print0(_buf, _pos);
481 _pos = 0;
482 _eexec_start = _eexec ? 0 : -1;
483 _eexec_end = -1;
484 }
485
486
487 void
488 Type1Writer::print(const char *s, int n)
489 {
490 while (n > 0) {
491 if (_pos >= BufSize)
492 local_flush();
493 int copy = BufSize - _pos;
494 if (copy > n) copy = n;
495 memcpy(_buf + _pos, s, copy);
496 _pos += copy;
497 s += copy;
498 n -= copy;
499 }
500 }
501
502
503 Type1Writer &
504 Type1Writer::operator<<(int x)
505 {
506 char str[128];
507 size_t len = snprintf(str, sizeof(str), "%d", x);
508 print(str, len);
509 return *this;
510 }
511
512
513 Type1Writer &
514 Type1Writer::operator<<(double x)
515 {
516 char str[256];
517 size_t len = snprintf(str, sizeof(str), "%g", x);
518 print(str, len);
519 return *this;
520 }
521
522
523 void
524 Type1Writer::switch_eexec(bool on)
525 {
526 _eexec = on;
527 if (_eexec) {
528 _eexec_start = _pos;
529 _r = t1R_ee;
530 print("SUCK", 4);
531 } else
532 _eexec_end = _pos;
533 }
534
535
536 /*****
537 * Type1PFAWriter
538 **/
539
540 Type1PFAWriter::Type1PFAWriter(FILE *f)
541 : _f(f), _hex_line(0)
542 {
543 }
544
545 Type1PFAWriter::~Type1PFAWriter()
546 {
547 flush();
548 }
549
550
551 void
552 Type1PFAWriter::switch_eexec(bool on)
553 {
554 flush();
555 _hex_line = 0;
556 Type1Writer::switch_eexec(on);
557 }
558
559 void
560 Type1PFAWriter::print0(const unsigned char *c, int l)
561 {
562 if (eexecing()) {
563 const char *hex = "0123456789ABCDEF";
564 for (; l; c++, l--) {
565 putc(hex[*c / 16], _f);
566 putc(hex[*c % 16], _f);
567 if (++_hex_line == 39) {
568 putc('\n', _f);
569 _hex_line = 0;
570 }
571 }
572 } else {
573 ssize_t result = fwrite(c, 1, l, _f);
574 (void) result;
575 }
576 }
577
578
579 /*****
580 * Type1PFBWriter
581 **/
582
583 Type1PFBWriter::Type1PFBWriter(FILE *f)
584 : _f(f), _binary(false)
585 {
586 }
587
588 Type1PFBWriter::~Type1PFBWriter()
589 {
590 flush();
591 fputc(128, _f);
592 fputc(3, _f);
593 }
594
595 void
596 Type1PFBWriter::flush()
597 {
598 Type1Writer::flush();
599 if (_save.length()) {
600 fputc(128, _f);
601 fputc(_binary ? 2 : 1, _f);
602 long l = _save.length();
603 fputc(l & 0xFF, _f);
604 fputc((l >> 8) & 0xFF, _f);
605 fputc((l >> 16) & 0xFF, _f);
606 fputc((l >> 24) & 0xFF, _f);
607 ssize_t result = fwrite(_save.data(), 1, _save.length(), _f);
608 (void) result;
609 _save.clear();
610 }
611 }
612
613 void
614 Type1PFBWriter::switch_eexec(bool on)
615 {
616 flush();
617 Type1Writer::switch_eexec(on);
618 _binary = on;
619 }
620
621 void
622 Type1PFBWriter::print0(const unsigned char *c, int l)
623 {
624 char *m = _save.extend(l);
625 memcpy(m, c, l);
626 }
627
628 }