Debian Codegraph @

libs/xpdf/xpdf-src/fofi/FoFiType1C.cc

raw
1 //========================================================================
2 //
3 // FoFiType1C.cc
4 //
5 // Copyright 1999-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <math.h>
14 #include "gmem.h"
15 #include "gmempp.h"
16 #include "GString.h"
17 #include "GHash.h"
18 #include "FoFiEncodings.h"
19 #include "FoFiType1C.h"
20
21 //------------------------------------------------------------------------
22
23 static char hexChars[17] = "0123456789ABCDEF";
24
25 #define type1cSubrRecursionLimit 20
26
27 //------------------------------------------------------------------------
28
29 GBool Type1COp::isZero() {
30 switch (kind) {
31 case type1COpInteger: return intgr == 0;
32 case type1COpFloat: return flt == 0;
33 case type1COpRational: return rat.num == 0;
34 default: return gFalse; // shouldn't happen
35 }
36 }
37
38 GBool Type1COp::isNegative() {
39 switch (kind) {
40 case type1COpInteger:
41 return intgr < 0;
42 case type1COpFloat:
43 return flt < 0;
44 case type1COpRational:
45 return (rat.num < 0) != (rat.den < 0);
46 default:
47 // shouldn't happen
48 return gFalse;
49 }
50 }
51
52 int Type1COp::toInt() {
53 switch (kind) {
54 case type1COpInteger:
55 return intgr;
56 case type1COpFloat:
57 if (flt < -2e9 || flt > 2e9) {
58 return 0;
59 }
60 return (int)flt;
61 case type1COpRational:
62 if (rat.den == 0) {
63 return 0;
64 }
65 return rat.num / rat.den;
66 default:
67 // shouldn't happen
68 return 0;
69 }
70 }
71
72 double Type1COp::toFloat() {
73 switch (kind) {
74 case type1COpInteger:
75 return (double)intgr;
76 case type1COpFloat:
77 return flt;
78 case type1COpRational:
79 if (rat.den == 0) {
80 return 0;
81 }
82 return (double)rat.num / (double)rat.den;
83 default:
84 // shouldn't happen
85 return 0.0;
86 }
87 }
88
89 //------------------------------------------------------------------------
90 // FoFiType1C
91 //------------------------------------------------------------------------
92
93 FoFiType1C *FoFiType1C::make(char *fileA, int lenA) {
94 FoFiType1C *ff;
95
96 ff = new FoFiType1C(fileA, lenA, gFalse);
97 if (!ff->parse()) {
98 delete ff;
99 return NULL;
100 }
101 return ff;
102 }
103
104 FoFiType1C *FoFiType1C::load(char *fileName) {
105 FoFiType1C *ff;
106 char *fileA;
107 int lenA;
108
109 if (!(fileA = FoFiBase::readFile(fileName, &lenA))) {
110 return NULL;
111 }
112 ff = new FoFiType1C(fileA, lenA, gTrue);
113 if (!ff->parse()) {
114 delete ff;
115 return NULL;
116 }
117 return ff;
118 }
119
120 FoFiType1C::FoFiType1C(char *fileA, int lenA, GBool freeFileDataA):
121 FoFiBase(fileA, lenA, freeFileDataA)
122 {
123 name = NULL;
124 encoding = NULL;
125 privateDicts = NULL;
126 fdSelect = NULL;
127 charset = NULL;
128 }
129
130 FoFiType1C::~FoFiType1C() {
131 int i;
132
133 if (name) {
134 delete name;
135 }
136 if (encoding &&
137 encoding != (char **)fofiType1StandardEncoding &&
138 encoding != (char **)fofiType1ExpertEncoding) {
139 for (i = 0; i < 256; ++i) {
140 gfree(encoding[i]);
141 }
142 gfree(encoding);
143 }
144 if (privateDicts) {
145 gfree(privateDicts);
146 }
147 if (fdSelect) {
148 gfree(fdSelect);
149 }
150 if (charset &&
151 charset != fofiType1CISOAdobeCharset &&
152 charset != fofiType1CExpertCharset &&
153 charset != fofiType1CExpertSubsetCharset) {
154 gfree(charset);
155 }
156 }
157
158 char *FoFiType1C::getName() {
159 return name ? name->getCString() : (char *)NULL;
160 }
161
162 char **FoFiType1C::getEncoding() {
163 return encoding;
164 }
165
166 GString *FoFiType1C::getGlyphName(int gid) {
167 char buf[256];
168 GBool ok;
169
170 ok = gTrue;
171 getString(charset[gid], buf, &ok);
172 if (!ok) {
173 return NULL;
174 }
175 return new GString(buf);
176 }
177
178 GHash *FoFiType1C::getNameToGIDMap() {
179 GHash *map;
180 char glyphName[256];
181 GBool ok;
182 int gid;
183
184 map = new GHash(gTrue);
185 for (gid = 0; gid < nGlyphs; ++gid) {
186 ok = gTrue;
187 getString(charset[gid], glyphName, &ok);
188 if (ok) {
189 map->add(new GString(glyphName), gid);
190 }
191 }
192 return map;
193 }
194
195 int *FoFiType1C::getCIDToGIDMap(int *nCIDs) {
196 int *map;
197 int n, i;
198
199 // a CID font's top dict has ROS as the first operator
200 if (topDict.firstOp != 0x0c1e) {
201 *nCIDs = 0;
202 return NULL;
203 }
204
205 // in a CID font, the charset data is the GID-to-CID mapping, so all
206 // we have to do is reverse it
207 n = 0;
208 for (i = 0; i < nGlyphs; ++i) {
209 if (charset[i] > n) {
210 n = charset[i];
211 }
212 }
213 ++n;
214 map = (int *)gmallocn(n, sizeof(int));
215 memset(map, 0, n * sizeof(int));
216 for (i = 0; i < nGlyphs; ++i) {
217 map[charset[i]] = i;
218 }
219 *nCIDs = n;
220 return map;
221 }
222
223 void FoFiType1C::getFontMatrix(double *mat) {
224 int i;
225
226 if (topDict.firstOp == 0x0c1e && privateDicts[0].hasFontMatrix) {
227 if (topDict.hasFontMatrix) {
228 mat[0] = topDict.fontMatrix[0] * privateDicts[0].fontMatrix[0] +
229 topDict.fontMatrix[1] * privateDicts[0].fontMatrix[2];
230 mat[1] = topDict.fontMatrix[0] * privateDicts[0].fontMatrix[1] +
231 topDict.fontMatrix[1] * privateDicts[0].fontMatrix[3];
232 mat[2] = topDict.fontMatrix[2] * privateDicts[0].fontMatrix[0] +
233 topDict.fontMatrix[3] * privateDicts[0].fontMatrix[2];
234 mat[3] = topDict.fontMatrix[2] * privateDicts[0].fontMatrix[1] +
235 topDict.fontMatrix[3] * privateDicts[0].fontMatrix[3];
236 mat[4] = topDict.fontMatrix[4] * privateDicts[0].fontMatrix[0] +
237 topDict.fontMatrix[5] * privateDicts[0].fontMatrix[2];
238 mat[5] = topDict.fontMatrix[4] * privateDicts[0].fontMatrix[1] +
239 topDict.fontMatrix[5] * privateDicts[0].fontMatrix[3];
240 } else {
241 for (i = 0; i < 6; ++i) {
242 mat[i] = privateDicts[0].fontMatrix[i];
243 }
244 }
245 } else {
246 for (i = 0; i < 6; ++i) {
247 mat[i] = topDict.fontMatrix[i];
248 }
249 }
250 }
251
252 void FoFiType1C::convertToType1(char *psName, const char **newEncoding,
253 GBool ascii, FoFiOutputFunc outputFunc,
254 void *outputStream) {
255 int psNameLen;
256 Type1CEexecBuf eb;
257 Type1CIndex subrIdx;
258 Type1CIndexVal val;
259 GString *buf;
260 char buf2[256];
261 const char **enc;
262 GBool ok;
263 int i;
264
265 if (psName) {
266 psNameLen = (int)strlen(psName);
267 } else {
268 psName = name->getCString();
269 psNameLen = name->getLength();
270 }
271
272 // write header and font dictionary, up to encoding
273 ok = gTrue;
274 (*outputFunc)(outputStream, "%!FontType1-1.0: ", 17);
275 (*outputFunc)(outputStream, psName, psNameLen);
276 if (topDict.versionSID != 0) {
277 getString(topDict.versionSID, buf2, &ok);
278 (*outputFunc)(outputStream, buf2, (int)strlen(buf2));
279 }
280 (*outputFunc)(outputStream, "\n", 1);
281 // the dictionary needs room for 12 entries: the following 9, plus
282 // Private and CharStrings (in the eexec section) and FID (which is
283 // added by definefont)
284 (*outputFunc)(outputStream, "12 dict begin\n", 14);
285 (*outputFunc)(outputStream, "/FontInfo 10 dict dup begin\n", 28);
286 if (topDict.versionSID != 0) {
287 (*outputFunc)(outputStream, "/version ", 9);
288 writePSString(buf2, outputFunc, outputStream);
289 (*outputFunc)(outputStream, " readonly def\n", 14);
290 }
291 if (topDict.noticeSID != 0) {
292 getString(topDict.noticeSID, buf2, &ok);
293 (*outputFunc)(outputStream, "/Notice ", 8);
294 writePSString(buf2, outputFunc, outputStream);
295 (*outputFunc)(outputStream, " readonly def\n", 14);
296 }
297 if (topDict.copyrightSID != 0) {
298 getString(topDict.copyrightSID, buf2, &ok);
299 (*outputFunc)(outputStream, "/Copyright ", 11);
300 writePSString(buf2, outputFunc, outputStream);
301 (*outputFunc)(outputStream, " readonly def\n", 14);
302 }
303 if (topDict.fullNameSID != 0) {
304 getString(topDict.fullNameSID, buf2, &ok);
305 (*outputFunc)(outputStream, "/FullName ", 10);
306 writePSString(buf2, outputFunc, outputStream);
307 (*outputFunc)(outputStream, " readonly def\n", 14);
308 }
309 if (topDict.familyNameSID != 0) {
310 getString(topDict.familyNameSID, buf2, &ok);
311 (*outputFunc)(outputStream, "/FamilyName ", 12);
312 writePSString(buf2, outputFunc, outputStream);
313 (*outputFunc)(outputStream, " readonly def\n", 14);
314 }
315 if (topDict.weightSID != 0) {
316 getString(topDict.weightSID, buf2, &ok);
317 (*outputFunc)(outputStream, "/Weight ", 8);
318 writePSString(buf2, outputFunc, outputStream);
319 (*outputFunc)(outputStream, " readonly def\n", 14);
320 }
321 if (topDict.isFixedPitch) {
322 (*outputFunc)(outputStream, "/isFixedPitch true def\n", 23);
323 } else {
324 (*outputFunc)(outputStream, "/isFixedPitch false def\n", 24);
325 }
326 buf = GString::format("/ItalicAngle {0:.4g} def\n", topDict.italicAngle);
327 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
328 delete buf;
329 buf = GString::format("/UnderlinePosition {0:.4g} def\n",
330 topDict.underlinePosition);
331 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
332 delete buf;
333 buf = GString::format("/UnderlineThickness {0:.4g} def\n",
334 topDict.underlineThickness);
335 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
336 delete buf;
337 (*outputFunc)(outputStream, "end readonly def\n", 17);
338 (*outputFunc)(outputStream, "/FontName /", 11);
339 (*outputFunc)(outputStream, psName, psNameLen);
340 (*outputFunc)(outputStream, " def\n", 5);
341 buf = GString::format("/PaintType {0:d} def\n", topDict.paintType);
342 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
343 delete buf;
344 (*outputFunc)(outputStream, "/FontType 1 def\n", 16);
345 buf = GString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] readonly def\n",
346 topDict.fontMatrix[0], topDict.fontMatrix[1],
347 topDict.fontMatrix[2], topDict.fontMatrix[3],
348 topDict.fontMatrix[4], topDict.fontMatrix[5]);
349 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
350 delete buf;
351 buf = GString::format("/FontBBox [{0:.4g} {1:.4g} {2:.4g} {3:.4g}] readonly def\n",
352 topDict.fontBBox[0], topDict.fontBBox[1],
353 topDict.fontBBox[2], topDict.fontBBox[3]);
354 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
355 delete buf;
356 buf = GString::format("/StrokeWidth {0:.4g} def\n", topDict.strokeWidth);
357 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
358 delete buf;
359 if (topDict.uniqueID != 0) {
360 buf = GString::format("/UniqueID {0:d} def\n", topDict.uniqueID);
361 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
362 delete buf;
363 }
364
365 // write the encoding
366 (*outputFunc)(outputStream, "/Encoding ", 10);
367 if (!newEncoding && encoding == (char **)fofiType1StandardEncoding) {
368 (*outputFunc)(outputStream, "StandardEncoding def\n", 21);
369 } else {
370 (*outputFunc)(outputStream, "256 array\n", 10);
371 (*outputFunc)(outputStream,
372 "0 1 255 {1 index exch /.notdef put} for\n", 40);
373 enc = newEncoding ? newEncoding : (const char **)encoding;
374 for (i = 0; i < 256; ++i) {
375 if (enc[i]) {
376 buf = GString::format("dup {0:d} /{1:s} put\n", i, enc[i]);
377 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
378 delete buf;
379 }
380 }
381 (*outputFunc)(outputStream, "readonly def\n", 13);
382 }
383 (*outputFunc)(outputStream, "currentdict end\n", 16);
384
385 // start the binary section
386 (*outputFunc)(outputStream, "currentfile eexec\n", 18);
387 eb.outputFunc = outputFunc;
388 eb.outputStream = outputStream;
389 eb.ascii = ascii;
390 eb.r1 = 55665;
391 eb.line = 0;
392
393 // write the private dictionary
394 eexecWrite(&eb, "\x83\xca\x73\xd5");
395 eexecWrite(&eb, "dup /Private 32 dict dup begin\n");
396 eexecWrite(&eb, "/RD {string currentfile exch readstring pop}"
397 " executeonly def\n");
398 eexecWrite(&eb, "/ND {noaccess def} executeonly def\n");
399 eexecWrite(&eb, "/NP {noaccess put} executeonly def\n");
400 eexecWrite(&eb, "/MinFeature {16 16} def\n");
401 eexecWrite(&eb, "/password 5839 def\n");
402 if (privateDicts[0].nBlueValues) {
403 eexecWrite(&eb, "/BlueValues [");
404 // number of entries must be even
405 int n = privateDicts[0].nBlueValues & ~1;
406 for (i = 0; i < n; ++i) {
407 buf = GString::format("{0:s}{1:d}",
408 i > 0 ? " " : "", privateDicts[0].blueValues[i]);
409 eexecWrite(&eb, buf->getCString());
410 delete buf;
411 }
412 eexecWrite(&eb, "] def\n");
413 }
414 if (privateDicts[0].nOtherBlues) {
415 eexecWrite(&eb, "/OtherBlues [");
416 // number of entries must be even
417 int n = privateDicts[0].nOtherBlues & ~1;
418 for (i = 0; i < n; ++i) {
419 buf = GString::format("{0:s}{1:d}",
420 i > 0 ? " " : "", privateDicts[0].otherBlues[i]);
421 eexecWrite(&eb, buf->getCString());
422 delete buf;
423 }
424 eexecWrite(&eb, "] def\n");
425 }
426 if (privateDicts[0].nFamilyBlues) {
427 eexecWrite(&eb, "/FamilyBlues [");
428 // number of entries must be even
429 int n = privateDicts[0].nFamilyBlues & ~1;
430 for (i = 0; i < n; ++i) {
431 buf = GString::format("{0:s}{1:d}",
432 i > 0 ? " " : "", privateDicts[0].familyBlues[i]);
433 eexecWrite(&eb, buf->getCString());
434 delete buf;
435 }
436 eexecWrite(&eb, "] def\n");
437 }
438 if (privateDicts[0].nFamilyOtherBlues) {
439 eexecWrite(&eb, "/FamilyOtherBlues [");
440 // number of entries must be even
441 int n = privateDicts[0].nFamilyOtherBlues & ~1;
442 for (i = 0; i < n; ++i) {
443 buf = GString::format("{0:s}{1:d}", i > 0 ? " " : "",
444 privateDicts[0].familyOtherBlues[i]);
445 eexecWrite(&eb, buf->getCString());
446 delete buf;
447 }
448 eexecWrite(&eb, "] def\n");
449 }
450 if (privateDicts[0].blueScale != 0.039625) {
451 buf = GString::format("/BlueScale {0:.4g} def\n",
452 privateDicts[0].blueScale);
453 eexecWrite(&eb, buf->getCString());
454 delete buf;
455 }
456 if (privateDicts[0].blueShift != 7) {
457 buf = GString::format("/BlueShift {0:d} def\n", privateDicts[0].blueShift);
458 eexecWrite(&eb, buf->getCString());
459 delete buf;
460 }
461 if (privateDicts[0].blueFuzz != 1) {
462 buf = GString::format("/BlueFuzz {0:d} def\n", privateDicts[0].blueFuzz);
463 eexecWrite(&eb, buf->getCString());
464 delete buf;
465 }
466 if (privateDicts[0].hasStdHW) {
467 buf = GString::format("/StdHW [{0:.4g}] def\n", privateDicts[0].stdHW);
468 eexecWrite(&eb, buf->getCString());
469 delete buf;
470 }
471 if (privateDicts[0].hasStdVW) {
472 buf = GString::format("/StdVW [{0:.4g}] def\n", privateDicts[0].stdVW);
473 eexecWrite(&eb, buf->getCString());
474 delete buf;
475 }
476 if (privateDicts[0].nStemSnapH) {
477 // the StemSnapH array should be unique values in ascending order --
478 // if not, just skip it
479 for (i = 1; i < privateDicts[0].nStemSnapH; ++i) {
480 if (privateDicts[0].stemSnapH[i-1] >= privateDicts[0].stemSnapH[i]) {
481 break;
482 }
483 }
484 if (i == privateDicts[0].nStemSnapH) {
485 eexecWrite(&eb, "/StemSnapH [");
486 for (i = 0; i < privateDicts[0].nStemSnapH; ++i) {
487 buf = GString::format("{0:s}{1:.4g}",
488 i > 0 ? " " : "", privateDicts[0].stemSnapH[i]);
489 eexecWrite(&eb, buf->getCString());
490 delete buf;
491 }
492 eexecWrite(&eb, "] def\n");
493 }
494 }
495 if (privateDicts[0].nStemSnapV) {
496 // the StemSnapV array should be unique values in ascending order --
497 // if not, just skip it
498 for (i = 1; i < privateDicts[0].nStemSnapV; ++i) {
499 if (privateDicts[0].stemSnapV[i-1] >= privateDicts[0].stemSnapV[i]) {
500 break;
501 }
502 }
503 if (i == privateDicts[0].nStemSnapV) {
504 eexecWrite(&eb, "/StemSnapV [");
505 for (i = 0; i < privateDicts[0].nStemSnapV; ++i) {
506 buf = GString::format("{0:s}{1:.4g}",
507 i > 0 ? " " : "", privateDicts[0].stemSnapV[i]);
508 eexecWrite(&eb, buf->getCString());
509 delete buf;
510 }
511 eexecWrite(&eb, "] def\n");
512 }
513 }
514 if (privateDicts[0].hasForceBold) {
515 buf = GString::format("/ForceBold {0:s} def\n",
516 privateDicts[0].forceBold ? "true" : "false");
517 eexecWrite(&eb, buf->getCString());
518 delete buf;
519 }
520 if (privateDicts[0].forceBoldThreshold != 0) {
521 buf = GString::format("/ForceBoldThreshold {0:.4g} def\n",
522 privateDicts[0].forceBoldThreshold);
523 eexecWrite(&eb, buf->getCString());
524 delete buf;
525 }
526 if (privateDicts[0].languageGroup != 0) {
527 buf = GString::format("/LanguageGroup {0:d} def\n",
528 privateDicts[0].languageGroup);
529 eexecWrite(&eb, buf->getCString());
530 delete buf;
531 }
532 if (privateDicts[0].expansionFactor != 0.06) {
533 buf = GString::format("/ExpansionFactor {0:.4g} def\n",
534 privateDicts[0].expansionFactor);
535 eexecWrite(&eb, buf->getCString());
536 delete buf;
537 }
538
539 // set up subroutines
540 ok = gTrue;
541 getIndex(privateDicts[0].subrsOffset, &subrIdx, &ok);
542 if (!ok) {
543 subrIdx.pos = -1;
544 }
545
546 // write the CharStrings
547 buf = GString::format("2 index /CharStrings {0:d} dict dup begin\n",
548 nGlyphs);
549 eexecWrite(&eb, buf->getCString());
550 delete buf;
551 for (i = 0; i < nGlyphs; ++i) {
552 ok = gTrue;
553 getIndexVal(&charStringsIdx, i, &val, &ok);
554 if (ok) {
555 getString(charset[i], buf2, &ok);
556 if (ok) {
557 eexecCvtGlyph(&eb, buf2, val.pos, val.len, &subrIdx, &privateDicts[0]);
558 }
559 }
560 }
561 eexecWrite(&eb, "end\n");
562 eexecWrite(&eb, "end\n");
563 eexecWrite(&eb, "readonly put\n");
564 eexecWrite(&eb, "noaccess put\n");
565 eexecWrite(&eb, "dup /FontName get exch definefont pop\n");
566 eexecWrite(&eb, "mark currentfile closefile\n");
567
568 // trailer
569 if (ascii && eb.line > 0) {
570 (*outputFunc)(outputStream, "\n", 1);
571 }
572 for (i = 0; i < 8; ++i) {
573 (*outputFunc)(outputStream, "0000000000000000000000000000000000000000000000000000000000000000\n", 65);
574 }
575 (*outputFunc)(outputStream, "cleartomark\n", 12);
576 }
577
578 void FoFiType1C::convertToCIDType0(char *psName, int *codeMap, int nCodes,
579 FoFiOutputFunc outputFunc,
580 void *outputStream) {
581 int *cidMap;
582 GString *charStrings;
583 int *charStringOffsets;
584 Type1CIndex subrIdx;
585 Type1CIndexVal val;
586 int nCIDs, gdBytes;
587 GString *buf;
588 char buf2[256];
589 GBool ok;
590 int gid, offset, n, i, j, k;
591
592 // compute the CID count and build the CID-to-GID mapping
593 if (codeMap) {
594 nCIDs = nCodes;
595 cidMap = (int *)gmallocn(nCIDs, sizeof(int));
596 for (i = 0; i < nCodes; ++i) {
597 if (codeMap[i] >= 0 && codeMap[i] < nGlyphs) {
598 cidMap[i] = codeMap[i];
599 } else {
600 cidMap[i] = -1;
601 }
602 }
603 } else if (topDict.firstOp == 0x0c1e) {
604 nCIDs = 0;
605 for (i = 0; i < nGlyphs; ++i) {
606 if (charset[i] >= nCIDs) {
607 nCIDs = charset[i] + 1;
608 }
609 }
610 cidMap = (int *)gmallocn(nCIDs, sizeof(int));
611 for (i = 0; i < nCIDs; ++i) {
612 cidMap[i] = -1;
613 }
614 for (i = 0; i < nGlyphs; ++i) {
615 cidMap[charset[i]] = i;
616 }
617 } else {
618 nCIDs = nGlyphs;
619 cidMap = (int *)gmallocn(nCIDs, sizeof(int));
620 for (i = 0; i < nCIDs; ++i) {
621 cidMap[i] = i;
622 }
623 }
624
625 // build the charstrings
626 charStrings = new GString();
627 charStringOffsets = (int *)gmallocn(nCIDs + 1, sizeof(int));
628 for (i = 0; i < nCIDs; ++i) {
629 charStringOffsets[i] = charStrings->getLength();
630 if ((gid = cidMap[i]) >= 0) {
631 ok = gTrue;
632 getIndexVal(&charStringsIdx, gid, &val, &ok);
633 if (ok) {
634 getIndex(privateDicts[fdSelect ? fdSelect[gid] : 0].subrsOffset,
635 &subrIdx, &ok);
636 if (!ok) {
637 subrIdx.pos = -1;
638 }
639 cvtGlyph(val.pos, val.len, charStrings,
640 &subrIdx, &privateDicts[fdSelect ? fdSelect[gid] : 0],
641 gTrue, 0);
642 }
643 }
644 }
645 charStringOffsets[nCIDs] = charStrings->getLength();
646
647 // compute gdBytes = number of bytes needed for charstring offsets
648 // (offset size needs to account for the charstring offset table,
649 // with a worst case of five bytes per entry, plus the charstrings
650 // themselves)
651 i = (nCIDs + 1) * 5 + charStrings->getLength();
652 if (i < 0x100) {
653 gdBytes = 1;
654 } else if (i < 0x10000) {
655 gdBytes = 2;
656 } else if (i < 0x1000000) {
657 gdBytes = 3;
658 } else {
659 gdBytes = 4;
660 }
661
662 // begin the font dictionary
663 (*outputFunc)(outputStream, "/CIDInit /ProcSet findresource begin\n", 37);
664 (*outputFunc)(outputStream, "20 dict begin\n", 14);
665 (*outputFunc)(outputStream, "/CIDFontName /", 14);
666 (*outputFunc)(outputStream, psName, (int)strlen(psName));
667 (*outputFunc)(outputStream, " def\n", 5);
668 (*outputFunc)(outputStream, "/CIDFontType 0 def\n", 19);
669 (*outputFunc)(outputStream, "/CIDSystemInfo 3 dict dup begin\n", 32);
670 if (topDict.registrySID > 0 && topDict.orderingSID > 0) {
671 ok = gTrue;
672 getString(topDict.registrySID, buf2, &ok);
673 if (ok) {
674 (*outputFunc)(outputStream, " /Registry (", 13);
675 (*outputFunc)(outputStream, buf2, (int)strlen(buf2));
676 (*outputFunc)(outputStream, ") def\n", 6);
677 }
678 ok = gTrue;
679 getString(topDict.orderingSID, buf2, &ok);
680 if (ok) {
681 (*outputFunc)(outputStream, " /Ordering (", 13);
682 (*outputFunc)(outputStream, buf2, (int)strlen(buf2));
683 (*outputFunc)(outputStream, ") def\n", 6);
684 }
685 } else {
686 (*outputFunc)(outputStream, " /Registry (Adobe) def\n", 24);
687 (*outputFunc)(outputStream, " /Ordering (Identity) def\n", 27);
688 }
689 buf = GString::format(" /Supplement {0:d} def\n", topDict.supplement);
690 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
691 delete buf;
692 (*outputFunc)(outputStream, "end def\n", 8);
693 if (topDict.hasFontMatrix) {
694 buf = GString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
695 topDict.fontMatrix[0], topDict.fontMatrix[1],
696 topDict.fontMatrix[2], topDict.fontMatrix[3],
697 topDict.fontMatrix[4], topDict.fontMatrix[5]);
698 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
699 delete buf;
700 } else if (privateDicts[0].hasFontMatrix) {
701 (*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
702 } else {
703 (*outputFunc)(outputStream,
704 "/FontMatrix [0.001 0 0 0.001 0 0] def\n", 38);
705 }
706 buf = GString::format("/FontBBox [{0:.4g} {1:.4g} {2:.4g} {3:.4g}] def\n",
707 topDict.fontBBox[0], topDict.fontBBox[1],
708 topDict.fontBBox[2], topDict.fontBBox[3]);
709 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
710 delete buf;
711 (*outputFunc)(outputStream, "/FontInfo 1 dict dup begin\n", 27);
712 (*outputFunc)(outputStream, " /FSType 8 def\n", 16);
713 (*outputFunc)(outputStream, "end def\n", 8);
714
715 // CIDFont-specific entries
716 buf = GString::format("/CIDCount {0:d} def\n", nCIDs);
717 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
718 delete buf;
719 (*outputFunc)(outputStream, "/FDBytes 1 def\n", 15);
720 buf = GString::format("/GDBytes {0:d} def\n", gdBytes);
721 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
722 delete buf;
723 (*outputFunc)(outputStream, "/CIDMapOffset 0 def\n", 20);
724 if (topDict.paintType != 0) {
725 buf = GString::format("/PaintType {0:d} def\n", topDict.paintType);
726 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
727 delete buf;
728 buf = GString::format("/StrokeWidth {0:.4g} def\n", topDict.strokeWidth);
729 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
730 delete buf;
731 }
732
733 // FDArray entry
734 buf = GString::format("/FDArray {0:d} array\n", nFDs);
735 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
736 delete buf;
737 for (i = 0; i < nFDs; ++i) {
738 buf = GString::format("dup {0:d} 10 dict begin\n", i);
739 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
740 delete buf;
741 (*outputFunc)(outputStream, "/FontType 1 def\n", 16);
742 if (privateDicts[i].hasFontMatrix) {
743 buf = GString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
744 privateDicts[i].fontMatrix[0],
745 privateDicts[i].fontMatrix[1],
746 privateDicts[i].fontMatrix[2],
747 privateDicts[i].fontMatrix[3],
748 privateDicts[i].fontMatrix[4],
749 privateDicts[i].fontMatrix[5]);
750 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
751 delete buf;
752 } else {
753 (*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
754 }
755 buf = GString::format("/PaintType {0:d} def\n", topDict.paintType);
756 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
757 delete buf;
758 (*outputFunc)(outputStream, "/Private 32 dict begin\n", 23);
759 if (privateDicts[i].nBlueValues) {
760 (*outputFunc)(outputStream, "/BlueValues [", 13);
761 for (j = 0; j < privateDicts[i].nBlueValues; ++j) {
762 buf = GString::format("{0:s}{1:d}",
763 j > 0 ? " " : "", privateDicts[i].blueValues[j]);
764 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
765 delete buf;
766 }
767 (*outputFunc)(outputStream, "] def\n", 6);
768 }
769 if (privateDicts[i].nOtherBlues) {
770 (*outputFunc)(outputStream, "/OtherBlues [", 13);
771 for (j = 0; j < privateDicts[i].nOtherBlues; ++j) {
772 buf = GString::format("{0:s}{1:d}",
773 j > 0 ? " " : "", privateDicts[i].otherBlues[j]);
774 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
775 delete buf;
776 }
777 (*outputFunc)(outputStream, "] def\n", 6);
778 }
779 if (privateDicts[i].nFamilyBlues) {
780 (*outputFunc)(outputStream, "/FamilyBlues [", 14);
781 for (j = 0; j < privateDicts[i].nFamilyBlues; ++j) {
782 buf = GString::format("{0:s}{1:d}",
783 j > 0 ? " " : "",
784 privateDicts[i].familyBlues[j]);
785 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
786 delete buf;
787 }
788 (*outputFunc)(outputStream, "] def\n", 6);
789 }
790 if (privateDicts[i].nFamilyOtherBlues) {
791 (*outputFunc)(outputStream, "/FamilyOtherBlues [", 19);
792 for (j = 0; j < privateDicts[i].nFamilyOtherBlues; ++j) {
793 buf = GString::format("{0:s}{1:d}", j > 0 ? " " : "",
794 privateDicts[i].familyOtherBlues[j]);
795 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
796 delete buf;
797 }
798 (*outputFunc)(outputStream, "] def\n", 6);
799 }
800 if (privateDicts[i].blueScale != 0.039625) {
801 buf = GString::format("/BlueScale {0:.4g} def\n",
802 privateDicts[i].blueScale);
803 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
804 delete buf;
805 }
806 if (privateDicts[i].blueShift != 7) {
807 buf = GString::format("/BlueShift {0:d} def\n",
808 privateDicts[i].blueShift);
809 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
810 delete buf;
811 }
812 if (privateDicts[i].blueFuzz != 1) {
813 buf = GString::format("/BlueFuzz {0:d} def\n", privateDicts[i].blueFuzz);
814 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
815 delete buf;
816 }
817 if (privateDicts[i].hasStdHW) {
818 buf = GString::format("/StdHW [{0:.4g}] def\n", privateDicts[i].stdHW);
819 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
820 delete buf;
821 }
822 if (privateDicts[i].hasStdVW) {
823 buf = GString::format("/StdVW [{0:.4g}] def\n", privateDicts[i].stdVW);
824 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
825 delete buf;
826 }
827 if (privateDicts[i].nStemSnapH) {
828 // the StemSnapH array should be unique values in ascending order --
829 // if not, just skip it
830 for (j = 1; j < privateDicts[i].nStemSnapH; ++j) {
831 if (privateDicts[i].stemSnapH[j-1] >= privateDicts[i].stemSnapH[j]) {
832 break;
833 }
834 }
835 if (j == privateDicts[i].nStemSnapH) {
836 (*outputFunc)(outputStream, "/StemSnapH [", 12);
837 for (j = 0; j < privateDicts[i].nStemSnapH; ++j) {
838 buf = GString::format("{0:s}{1:.4g}",
839 j > 0 ? " " : "", privateDicts[i].stemSnapH[j]);
840 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
841 delete buf;
842 }
843 (*outputFunc)(outputStream, "] def\n", 6);
844 }
845 }
846 if (privateDicts[i].nStemSnapV) {
847 // the StemSnapV array should be unique values in ascending order --
848 // if not, just skip it
849 for (j = 1; j < privateDicts[i].nStemSnapV; ++j) {
850 if (privateDicts[i].stemSnapV[j-1] >= privateDicts[i].stemSnapV[j]) {
851 break;
852 }
853 }
854 if (j == privateDicts[i].nStemSnapV) {
855 (*outputFunc)(outputStream, "/StemSnapV [", 12);
856 for (j = 0; j < privateDicts[i].nStemSnapV; ++j) {
857 buf = GString::format("{0:s}{1:.4g}",
858 j > 0 ? " " : "", privateDicts[i].stemSnapV[j]);
859 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
860 delete buf;
861 }
862 (*outputFunc)(outputStream, "] def\n", 6);
863 }
864 }
865 if (privateDicts[i].hasForceBold) {
866 buf = GString::format("/ForceBold {0:s} def\n",
867 privateDicts[i].forceBold ? "true" : "false");
868 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
869 delete buf;
870 }
871 if (privateDicts[i].forceBoldThreshold != 0) {
872 buf = GString::format("/ForceBoldThreshold {0:.4g} def\n",
873 privateDicts[i].forceBoldThreshold);
874 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
875 delete buf;
876 }
877 if (privateDicts[i].languageGroup != 0) {
878 buf = GString::format("/LanguageGroup {0:d} def\n",
879 privateDicts[i].languageGroup);
880 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
881 delete buf;
882 }
883 if (privateDicts[i].expansionFactor != 0.06) {
884 buf = GString::format("/ExpansionFactor {0:.4g} def\n",
885 privateDicts[i].expansionFactor);
886 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
887 delete buf;
888 }
889 (*outputFunc)(outputStream, "currentdict end def\n", 20);
890 (*outputFunc)(outputStream, "currentdict end put\n", 20);
891 }
892 (*outputFunc)(outputStream, "def\n", 4);
893
894 // start the binary section
895 offset = (nCIDs + 1) * (1 + gdBytes);
896 buf = GString::format("(Hex) {0:d} StartData\n",
897 offset + charStrings->getLength());
898 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
899 delete buf;
900
901 // write the charstring offset (CIDMap) table
902 for (i = 0; i <= nCIDs; i += 6) {
903 for (j = 0; j < 6 && i+j <= nCIDs; ++j) {
904 if (i+j < nCIDs && cidMap[i+j] >= 0 && fdSelect) {
905 buf2[0] = (char)fdSelect[cidMap[i+j]];
906 } else {
907 buf2[0] = (char)0;
908 }
909 n = offset + charStringOffsets[i+j];
910 for (k = gdBytes; k >= 1; --k) {
911 buf2[k] = (char)(n & 0xff);
912 n >>= 8;
913 }
914 for (k = 0; k <= gdBytes; ++k) {
915 buf = GString::format("{0:02x}", buf2[k] & 0xff);
916 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
917 delete buf;
918 }
919 }
920 (*outputFunc)(outputStream, "\n", 1);
921 }
922
923 // write the charstring data
924 n = charStrings->getLength();
925 for (i = 0; i < n; i += 32) {
926 for (j = 0; j < 32 && i+j < n; ++j) {
927 buf = GString::format("{0:02x}", charStrings->getChar(i+j) & 0xff);
928 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
929 delete buf;
930 }
931 if (i + 32 >= n) {
932 (*outputFunc)(outputStream, ">", 1);
933 }
934 (*outputFunc)(outputStream, "\n", 1);
935 }
936
937 gfree(charStringOffsets);
938 delete charStrings;
939 gfree(cidMap);
940 }
941
942 void FoFiType1C::convertToType0(char *psName, int *codeMap, int nCodes,
943 FoFiOutputFunc outputFunc,
944 void *outputStream) {
945 int *cidMap;
946 Type1CIndex subrIdx;
947 Type1CIndexVal val;
948 int nCIDs;
949 GString *buf;
950 Type1CEexecBuf eb;
951 GBool ok;
952 int fd, i, j, k;
953
954 // compute the CID count and build the CID-to-GID mapping
955 if (codeMap) {
956 nCIDs = nCodes;
957 cidMap = (int *)gmallocn(nCIDs, sizeof(int));
958 for (i = 0; i < nCodes; ++i) {
959 if (codeMap[i] >= 0 && codeMap[i] < nGlyphs) {
960 cidMap[i] = codeMap[i];
961 } else {
962 cidMap[i] = -1;
963 }
964 }
965 } else if (topDict.firstOp == 0x0c1e) {
966 nCIDs = 0;
967 for (i = 0; i < nGlyphs; ++i) {
968 if (charset[i] >= nCIDs) {
969 nCIDs = charset[i] + 1;
970 }
971 }
972 cidMap = (int *)gmallocn(nCIDs, sizeof(int));
973 for (i = 0; i < nCIDs; ++i) {
974 cidMap[i] = -1;
975 }
976 for (i = 0; i < nGlyphs; ++i) {
977 cidMap[charset[i]] = i;
978 }
979 } else {
980 nCIDs = nGlyphs;
981 cidMap = (int *)gmallocn(nCIDs, sizeof(int));
982 for (i = 0; i < nCIDs; ++i) {
983 cidMap[i] = i;
984 }
985 }
986
987 // write the descendant Type 1 fonts
988 for (i = 0; i < nCIDs; i += 256) {
989
990 //~ this assumes that all CIDs in this block have the same FD --
991 //~ to handle multiple FDs correctly, need to somehow divide the
992 //~ font up by FD; as a kludge we ignore CID 0, which is .notdef
993 fd = 0;
994 // if fdSelect is NULL, we have an 8-bit font, so just leave fd=0
995 if (fdSelect) {
996 for (j = i==0 ? 1 : 0; j < 256 && i+j < nCIDs; ++j) {
997 if (cidMap[i+j] >= 0) {
998 fd = fdSelect[cidMap[i+j]];
999 break;
1000 }
1001 }
1002 }
1003 if (fd < 0 || fd >= nFDs) {
1004 // this will only happen in a broken/damaged font
1005 fd = 0;
1006 }
1007
1008 // font dictionary (unencrypted section)
1009 (*outputFunc)(outputStream, "16 dict begin\n", 14);
1010 (*outputFunc)(outputStream, "/FontName /", 11);
1011 (*outputFunc)(outputStream, psName, (int)strlen(psName));
1012 buf = GString::format("_{0:02x} def\n", i >> 8);
1013 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1014 delete buf;
1015 (*outputFunc)(outputStream, "/FontType 1 def\n", 16);
1016 if (privateDicts[fd].hasFontMatrix) {
1017 buf = GString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
1018 privateDicts[fd].fontMatrix[0],
1019 privateDicts[fd].fontMatrix[1],
1020 privateDicts[fd].fontMatrix[2],
1021 privateDicts[fd].fontMatrix[3],
1022 privateDicts[fd].fontMatrix[4],
1023 privateDicts[fd].fontMatrix[5]);
1024 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1025 delete buf;
1026 } else if (topDict.hasFontMatrix) {
1027 (*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
1028 } else {
1029 (*outputFunc)(outputStream,
1030 "/FontMatrix [0.001 0 0 0.001 0 0] def\n", 38);
1031 }
1032 buf = GString::format("/FontBBox [{0:.4g} {1:.4g} {2:.4g} {3:.4g}] def\n",
1033 topDict.fontBBox[0], topDict.fontBBox[1],
1034 topDict.fontBBox[2], topDict.fontBBox[3]);
1035 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1036 delete buf;
1037 buf = GString::format("/PaintType {0:d} def\n", topDict.paintType);
1038 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1039 delete buf;
1040 if (topDict.paintType != 0) {
1041 buf = GString::format("/StrokeWidth {0:.4g} def\n", topDict.strokeWidth);
1042 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1043 delete buf;
1044 }
1045 (*outputFunc)(outputStream, "/Encoding 256 array\n", 20);
1046 for (j = 0; j < 256 && i+j < nCIDs; ++j) {
1047 buf = GString::format("dup {0:d} /c{1:02x} put\n", j, j);
1048 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1049 delete buf;
1050 }
1051 if (j < 256) {
1052 buf = GString::format("{0:d} 1 255 {{ 1 index exch /.notdef put }} for\n",
1053 j);
1054 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1055 delete buf;
1056 }
1057 (*outputFunc)(outputStream, "readonly def\n", 13);
1058 (*outputFunc)(outputStream, "currentdict end\n", 16);
1059
1060 // start the binary section
1061 (*outputFunc)(outputStream, "currentfile eexec\n", 18);
1062 eb.outputFunc = outputFunc;
1063 eb.outputStream = outputStream;
1064 eb.ascii = gTrue;
1065 eb.r1 = 55665;
1066 eb.line = 0;
1067
1068 // start the private dictionary
1069 eexecWrite(&eb, "\x83\xca\x73\xd5");
1070 eexecWrite(&eb, "dup /Private 32 dict dup begin\n");
1071 eexecWrite(&eb, "/RD {string currentfile exch readstring pop}"
1072 " executeonly def\n");
1073 eexecWrite(&eb, "/ND {noaccess def} executeonly def\n");
1074 eexecWrite(&eb, "/NP {noaccess put} executeonly def\n");
1075 eexecWrite(&eb, "/MinFeature {16 16} def\n");
1076 eexecWrite(&eb, "/password 5839 def\n");
1077 if (privateDicts[fd].nBlueValues) {
1078 eexecWrite(&eb, "/BlueValues [");
1079 for (k = 0; k < privateDicts[fd].nBlueValues; ++k) {
1080 buf = GString::format("{0:s}{1:d}",
1081 k > 0 ? " " : "",
1082 privateDicts[fd].blueValues[k]);
1083 eexecWrite(&eb, buf->getCString());
1084 delete buf;
1085 }
1086 eexecWrite(&eb, "] def\n");
1087 }
1088 if (privateDicts[fd].nOtherBlues) {
1089 eexecWrite(&eb, "/OtherBlues [");
1090 for (k = 0; k < privateDicts[fd].nOtherBlues; ++k) {
1091 buf = GString::format("{0:s}{1:d}",
1092 k > 0 ? " " : "",
1093 privateDicts[fd].otherBlues[k]);
1094 eexecWrite(&eb, buf->getCString());
1095 delete buf;
1096 }
1097 eexecWrite(&eb, "] def\n");
1098 }
1099 if (privateDicts[fd].nFamilyBlues) {
1100 eexecWrite(&eb, "/FamilyBlues [");
1101 for (k = 0; k < privateDicts[fd].nFamilyBlues; ++k) {
1102 buf = GString::format("{0:s}{1:d}", k > 0 ? " " : "",
1103 privateDicts[fd].familyBlues[k]);
1104 eexecWrite(&eb, buf->getCString());
1105 delete buf;
1106 }
1107 eexecWrite(&eb, "] def\n");
1108 }
1109 if (privateDicts[fd].nFamilyOtherBlues) {
1110 eexecWrite(&eb, "/FamilyOtherBlues [");
1111 for (k = 0; k < privateDicts[fd].nFamilyOtherBlues; ++k) {
1112 buf = GString::format("{0:s}{1:d}", k > 0 ? " " : "",
1113 privateDicts[fd].familyOtherBlues[k]);
1114 eexecWrite(&eb, buf->getCString());
1115 delete buf;
1116 }
1117 eexecWrite(&eb, "] def\n");
1118 }
1119 if (privateDicts[fd].blueScale != 0.039625) {
1120 buf = GString::format("/BlueScale {0:.4g} def\n",
1121 privateDicts[fd].blueScale);
1122 eexecWrite(&eb, buf->getCString());
1123 delete buf;
1124 }
1125 if (privateDicts[fd].blueShift != 7) {
1126 buf = GString::format("/BlueShift {0:d} def\n",
1127 privateDicts[fd].blueShift);
1128 eexecWrite(&eb, buf->getCString());
1129 delete buf;
1130 }
1131 if (privateDicts[fd].blueFuzz != 1) {
1132 buf = GString::format("/BlueFuzz {0:d} def\n",
1133 privateDicts[fd].blueFuzz);
1134 eexecWrite(&eb, buf->getCString());
1135 delete buf;
1136 }
1137 if (privateDicts[fd].hasStdHW) {
1138 buf = GString::format("/StdHW [{0:.4g}] def\n", privateDicts[fd].stdHW);
1139 eexecWrite(&eb, buf->getCString());
1140 delete buf;
1141 }
1142 if (privateDicts[fd].hasStdVW) {
1143 buf = GString::format("/StdVW [{0:.4g}] def\n", privateDicts[fd].stdVW);
1144 eexecWrite(&eb, buf->getCString());
1145 delete buf;
1146 }
1147 if (privateDicts[fd].nStemSnapH) {
1148 // the StemSnapH array should be unique values in ascending order --
1149 // if not, just skip it
1150 for (k = 1; k < privateDicts[fd].nStemSnapH; ++k) {
1151 if (privateDicts[fd].stemSnapH[k-1] >= privateDicts[fd].stemSnapH[k]) {
1152 break;
1153 }
1154 }
1155 if (k == privateDicts[fd].nStemSnapH) {
1156 eexecWrite(&eb, "/StemSnapH [");
1157 for (k = 0; k < privateDicts[fd].nStemSnapH; ++k) {
1158 buf = GString::format("{0:s}{1:.4g}",
1159 k > 0 ? " " : "",
1160 privateDicts[fd].stemSnapH[k]);
1161 eexecWrite(&eb, buf->getCString());
1162 delete buf;
1163 }
1164 eexecWrite(&eb, "] def\n");
1165 }
1166 }
1167 if (privateDicts[fd].nStemSnapV) {
1168 // the StemSnapV array should be unique values in ascending order --
1169 // if not, just skip it
1170 for (k = 1; k < privateDicts[fd].nStemSnapV; ++k) {
1171 if (privateDicts[fd].stemSnapV[k-1] >= privateDicts[fd].stemSnapV[k]) {
1172 break;
1173 }
1174 }
1175 if (k == privateDicts[fd].nStemSnapV) {
1176 eexecWrite(&eb, "/StemSnapV [");
1177 for (k = 0; k < privateDicts[fd].nStemSnapV; ++k) {
1178 buf = GString::format("{0:s}{1:.4g}",
1179 k > 0 ? " " : "",
1180 privateDicts[fd].stemSnapV[k]);
1181 eexecWrite(&eb, buf->getCString());
1182 delete buf;
1183 }
1184 eexecWrite(&eb, "] def\n");
1185 }
1186 }
1187 if (privateDicts[fd].hasForceBold) {
1188 buf = GString::format("/ForceBold {0:s} def\n",
1189 privateDicts[fd].forceBold ? "true" : "false");
1190 eexecWrite(&eb, buf->getCString());
1191 delete buf;
1192 }
1193 if (privateDicts[fd].forceBoldThreshold != 0) {
1194 buf = GString::format("/ForceBoldThreshold {0:.4g} def\n",
1195 privateDicts[fd].forceBoldThreshold);
1196 eexecWrite(&eb, buf->getCString());
1197 delete buf;
1198 }
1199 if (privateDicts[fd].languageGroup != 0) {
1200 buf = GString::format("/LanguageGroup {0:d} def\n",
1201 privateDicts[fd].languageGroup);
1202 eexecWrite(&eb, buf->getCString());
1203 delete buf;
1204 }
1205 if (privateDicts[fd].expansionFactor != 0.06) {
1206 buf = GString::format("/ExpansionFactor {0:.4g} def\n",
1207 privateDicts[fd].expansionFactor);
1208 eexecWrite(&eb, buf->getCString());
1209 delete buf;
1210 }
1211
1212 // set up the subroutines
1213 ok = gTrue;
1214 getIndex(privateDicts[fd].subrsOffset, &subrIdx, &ok);
1215 if (!ok) {
1216 subrIdx.pos = -1;
1217 }
1218
1219 // start the CharStrings
1220 eexecWrite(&eb, "2 index /CharStrings 256 dict dup begin\n");
1221
1222 // write the .notdef CharString
1223 ok = gTrue;
1224 getIndexVal(&charStringsIdx, 0, &val, &ok);
1225 if (ok) {
1226 eexecCvtGlyph(&eb, ".notdef", val.pos, val.len,
1227 &subrIdx, &privateDicts[fd]);
1228 }
1229
1230 // write the CharStrings
1231 for (j = 0; j < 256 && i+j < nCIDs; ++j) {
1232 if (cidMap[i+j] >= 0) {
1233 ok = gTrue;
1234 getIndexVal(&charStringsIdx, cidMap[i+j], &val, &ok);
1235 if (ok) {
1236 buf = GString::format("c{0:02x}", j);
1237 eexecCvtGlyph(&eb, buf->getCString(), val.pos, val.len,
1238 &subrIdx, &privateDicts[fd]);
1239 delete buf;
1240 }
1241 }
1242 }
1243 eexecWrite(&eb, "end\n");
1244 eexecWrite(&eb, "end\n");
1245 eexecWrite(&eb, "readonly put\n");
1246 eexecWrite(&eb, "noaccess put\n");
1247 eexecWrite(&eb, "dup /FontName get exch definefont pop\n");
1248 eexecWrite(&eb, "mark currentfile closefile\n");
1249
1250 // trailer
1251 if (eb.line > 0) {
1252 (*outputFunc)(outputStream, "\n", 1);
1253 }
1254 for (j = 0; j < 8; ++j) {
1255 (*outputFunc)(outputStream, "0000000000000000000000000000000000000000000000000000000000000000\n", 65);
1256 }
1257 (*outputFunc)(outputStream, "cleartomark\n", 12);
1258 }
1259
1260 // write the Type 0 parent font
1261 (*outputFunc)(outputStream, "16 dict begin\n", 14);
1262 (*outputFunc)(outputStream, "/FontName /", 11);
1263 (*outputFunc)(outputStream, psName, (int)strlen(psName));
1264 (*outputFunc)(outputStream, " def\n", 5);
1265 (*outputFunc)(outputStream, "/FontType 0 def\n", 16);
1266 if (topDict.hasFontMatrix) {
1267 buf = GString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
1268 topDict.fontMatrix[0], topDict.fontMatrix[1],
1269 topDict.fontMatrix[2], topDict.fontMatrix[3],
1270 topDict.fontMatrix[4], topDict.fontMatrix[5]);
1271 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1272 delete buf;
1273 } else {
1274 (*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
1275 }
1276 (*outputFunc)(outputStream, "/FMapType 2 def\n", 16);
1277 (*outputFunc)(outputStream, "/Encoding [\n", 12);
1278 for (i = 0; i < nCIDs; i += 256) {
1279 buf = GString::format("{0:d}\n", i >> 8);
1280 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1281 delete buf;
1282 }
1283 (*outputFunc)(outputStream, "] def\n", 6);
1284 (*outputFunc)(outputStream, "/FDepVector [\n", 14);
1285 for (i = 0; i < nCIDs; i += 256) {
1286 (*outputFunc)(outputStream, "/", 1);
1287 (*outputFunc)(outputStream, psName, (int)strlen(psName));
1288 buf = GString::format("_{0:02x} findfont\n", i >> 8);
1289 (*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1290 delete buf;
1291 }
1292 (*outputFunc)(outputStream, "] def\n", 6);
1293 (*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
1294
1295 gfree(cidMap);
1296 }
1297
1298 void FoFiType1C::eexecCvtGlyph(Type1CEexecBuf *eb, const char *glyphName,
1299 int offset, int nBytes,
1300 Type1CIndex *subrIdx,
1301 Type1CPrivateDict *pDict) {
1302 GString *buf;
1303 GString *charBuf;
1304
1305 // generate the charstring
1306 charBuf = new GString();
1307 cvtGlyph(offset, nBytes, charBuf, subrIdx, pDict, gTrue, 0);
1308
1309 buf = GString::format("/{0:s} {1:d} RD ", glyphName, charBuf->getLength());
1310 eexecWrite(eb, buf->getCString());
1311 delete buf;
1312 eexecWriteCharstring(eb, (Guchar *)charBuf->getCString(),
1313 charBuf->getLength());
1314 eexecWrite(eb, " ND\n");
1315
1316 delete charBuf;
1317 }
1318
1319 void FoFiType1C::cvtGlyph(int offset, int nBytes, GString *charBuf,
1320 Type1CIndex *subrIdx, Type1CPrivateDict *pDict,
1321 GBool top, int recursion) {
1322 Type1CIndexVal val;
1323 Type1COp zero, tmp;
1324 GBool ok, dInt;
1325 double d, dx, dy;
1326 Gushort r2;
1327 Guchar byte;
1328 int pos, subrBias, start, num, den, i, k;
1329
1330 if (recursion > type1cSubrRecursionLimit) {
1331 //~ error(-1, "Recursive loop in Type1C glyph");
1332 return;
1333 }
1334
1335 start = charBuf->getLength();
1336 if (top) {
1337 charBuf->append((char)73);
1338 charBuf->append((char)58);
1339 charBuf->append((char)147);
1340 charBuf->append((char)134);
1341 nOps = 0;
1342 nHints = 0;
1343 firstOp = gTrue;
1344 openPath = gFalse;
1345 }
1346
1347 zero.kind = type1COpInteger;
1348 zero.intgr = 0;
1349
1350 pos = offset;
1351 while (pos < offset + nBytes) {
1352 ok = gTrue;
1353 pos = getOp(pos, gTrue, &ok);
1354 if (!ok) {
1355 break;
1356 }
1357 if (ops[nOps - 1].kind == type1COpOperator) {
1358 --nOps; // drop the operator
1359 switch (ops[nOps].op) {
1360 case 0x0001: // hstem
1361 if (firstOp) {
1362 cvtGlyphWidth(nOps & 1, charBuf, pDict);
1363 firstOp = gFalse;
1364 }
1365 if (nOps & 1) {
1366 //~ error(-1, "Wrong number of args (%d) to Type 2 hstem", nOps);
1367 }
1368 d = 0;
1369 dInt = gTrue;
1370 for (k = 0; k+1 < nOps; k += 2) {
1371 // convert Type 2 edge hints (-20 or -21) to Type 1 ghost hints
1372 if (ops[k+1].isNegative()) {
1373 d += ops[k].toFloat() + ops[k+1].toFloat();
1374 dInt &= ops[k].kind == type1COpInteger;
1375 dInt &= ops[k+1].kind == type1COpInteger;
1376 if (dInt) {
1377 tmp.kind = type1COpInteger;
1378 tmp.intgr = (int)(d + 0.5);
1379 } else {
1380 tmp.kind = type1COpFloat;
1381 tmp.flt = d;
1382 }
1383 cvtNum(tmp, charBuf);
1384 if (ops[k+1].kind == type1COpInteger) {
1385 tmp.kind = type1COpInteger;
1386 tmp.intgr = -ops[k+1].intgr;
1387 } else if (ops[k+1].kind == type1COpRational) {
1388 tmp.kind = type1COpRational;
1389 tmp.rat.num = -ops[k+1].rat.num;
1390 tmp.rat.den = ops[k+1].rat.den;
1391 } else {
1392 tmp.kind = type1COpFloat;
1393 tmp.flt = -ops[k+1].toFloat();
1394 }
1395 cvtNum(tmp, charBuf);
1396 } else {
1397 d += ops[k].toFloat();
1398 dInt &= ops[k].kind == type1COpInteger;
1399 if (dInt) {
1400 tmp.kind = type1COpInteger;
1401 tmp.intgr = (int)(d + 0.5);
1402 } else {
1403 tmp.kind = type1COpFloat;
1404 tmp.flt = d;
1405 }
1406 cvtNum(tmp, charBuf);
1407 cvtNum(ops[k+1], charBuf);
1408 d += ops[k+1].toFloat();
1409 dInt &= ops[k+1].kind == type1COpInteger;
1410 }
1411 charBuf->append((char)1);
1412 }
1413 nHints += nOps / 2;
1414 nOps = 0;
1415 break;
1416 case 0x0003: // vstem
1417 if (firstOp) {
1418 cvtGlyphWidth(nOps & 1, charBuf, pDict);
1419 firstOp = gFalse;
1420 }
1421 if (nOps & 1) {
1422 //~ error(-1, "Wrong number of args (%d) to Type 2 vstem", nOps);
1423 }
1424 d = 0;
1425 dInt = gTrue;
1426 for (k = 0; k+1 < nOps; k += 2) {
1427 // convert Type 2 edge hints (-20 or -21) to Type 1 ghost hints
1428 if (ops[k+1].isNegative()) {
1429 d += ops[k].toFloat() + ops[k+1].toFloat();
1430 dInt &= ops[k].kind == type1COpInteger;
1431 dInt &= ops[k+1].kind == type1COpInteger;
1432 if (dInt) {
1433 tmp.kind = type1COpInteger;
1434 tmp.intgr = (int)(d + 0.5);
1435 } else {
1436 tmp.kind = type1COpFloat;
1437 tmp.flt = d;
1438 }
1439 cvtNum(tmp, charBuf);
1440 if (ops[k+1].kind == type1COpInteger) {
1441 tmp.kind = type1COpInteger;
1442 tmp.intgr = -ops[k+1].intgr;
1443 } else if (ops[k+1].kind == type1COpRational) {
1444 tmp.kind = type1COpRational;
1445 tmp.rat.num = -ops[k+1].rat.num;
1446 tmp.rat.den = ops[k+1].rat.den;
1447 } else {
1448 tmp.kind = type1COpFloat;
1449 tmp.flt = -ops[k+1].toFloat();
1450 }
1451 cvtNum(tmp, charBuf);
1452 } else {
1453 d += ops[k].toFloat();
1454 dInt &= ops[k].kind == type1COpInteger;
1455 if (dInt) {
1456 tmp.kind = type1COpInteger;
1457 tmp.intgr = (int)(d + 0.5);
1458 } else {
1459 tmp.kind = type1COpFloat;
1460 tmp.flt = d;
1461 }
1462 cvtNum(tmp, charBuf);
1463 cvtNum(ops[k+1], charBuf);
1464 d += ops[k+1].toFloat();
1465 dInt &= ops[k+1].kind == type1COpInteger;
1466 }
1467 charBuf->append((char)3);
1468 }
1469 nHints += nOps / 2;
1470 nOps = 0;
1471 break;
1472 case 0x0004: // vmoveto
1473 if (firstOp) {
1474 cvtGlyphWidth(nOps == 2, charBuf, pDict);
1475 firstOp = gFalse;
1476 }
1477 if (openPath) {
1478 charBuf->append((char)9);
1479 openPath = gFalse;
1480 }
1481 if (nOps != 1) {
1482 //~ error(-1, "Wrong number of args (%d) to Type 2 vmoveto", nOps);
1483 }
1484 cvtNum(ops[0], charBuf);
1485 charBuf->append((char)4);
1486 nOps = 0;
1487 break;
1488 case 0x0005: // rlineto
1489 if (nOps < 2 || nOps % 2 != 0) {
1490 //~ error(-1, "Wrong number of args (%d) to Type 2 rlineto", nOps);
1491 }
1492 for (k = 0; k+1 < nOps; k += 2) {
1493 cvtNum(ops[k], charBuf);
1494 cvtNum(ops[k+1], charBuf);
1495 charBuf->append((char)5);
1496 }
1497 nOps = 0;
1498 openPath = gTrue;
1499 break;
1500 case 0x0006: // hlineto
1501 if (nOps < 1) {
1502 //~ error(-1, "Wrong number of args (%d) to Type 2 hlineto", nOps);
1503 }
1504 for (k = 0; k < nOps; ++k) {
1505 cvtNum(ops[k], charBuf);
1506 charBuf->append((char)((k & 1) ? 7 : 6));
1507 }
1508 nOps = 0;
1509 openPath = gTrue;
1510 break;
1511 case 0x0007: // vlineto
1512 if (nOps < 1) {
1513 //~ error(-1, "Wrong number of args (%d) to Type 2 vlineto", nOps);
1514 }
1515 for (k = 0; k < nOps; ++k) {
1516 cvtNum(ops[k], charBuf);
1517 charBuf->append((char)((k & 1) ? 6 : 7));
1518 }
1519 nOps = 0;
1520 openPath = gTrue;
1521 break;
1522 case 0x0008: // rrcurveto
1523 if (nOps < 6 || nOps % 6 != 0) {
1524 //~ error(-1, "Wrong number of args (%d) to Type 2 rrcurveto", nOps);
1525 }
1526 for (k = 0; k+5 < nOps; k += 6) {
1527 cvtNum(ops[k], charBuf);
1528 cvtNum(ops[k+1], charBuf);
1529 cvtNum(ops[k+2], charBuf);
1530 cvtNum(ops[k+3], charBuf);
1531 cvtNum(ops[k+4], charBuf);
1532 cvtNum(ops[k+5], charBuf);
1533 charBuf->append((char)8);
1534 }
1535 nOps = 0;
1536 openPath = gTrue;
1537 break;
1538 case 0x000a: // callsubr
1539 if (nOps >= 1) {
1540 subrBias = (subrIdx->len < 1240)
1541 ? 107 : (subrIdx->len < 33900) ? 1131 : 32768;
1542 k = subrBias + ops[nOps - 1].toInt();
1543 --nOps;
1544 ok = gTrue;
1545 getIndexVal(subrIdx, k, &val, &ok);
1546 if (ok) {
1547 cvtGlyph(val.pos, val.len, charBuf, subrIdx, pDict, gFalse,
1548 recursion + 1);
1549 }
1550 } else {
1551 //~ error(-1, "Too few args to Type 2 callsubr");
1552 }
1553 // don't clear the stack
1554 break;
1555 case 0x000b: // return
1556 // don't clear the stack
1557 break;
1558 case 0x000e: // endchar / seac
1559 if (firstOp) {
1560 cvtGlyphWidth(nOps == 1 || nOps == 5, charBuf, pDict);
1561 firstOp = gFalse;
1562 }
1563 if (openPath) {
1564 charBuf->append((char)9);
1565 openPath = gFalse;
1566 }
1567 if (nOps == 4) {
1568 cvtNum(zero, charBuf);
1569 cvtNum(ops[0], charBuf);
1570 cvtNum(ops[1], charBuf);
1571 cvtNum(ops[2], charBuf);
1572 cvtNum(ops[3], charBuf);
1573 charBuf->append((char)12)->append((char)6);
1574 } else if (nOps == 0) {
1575 charBuf->append((char)14);
1576 } else {
1577 //~ error(-1, "Wrong number of args (%d) to Type 2 endchar", nOps);
1578 }
1579 nOps = 0;
1580 break;
1581 case 0x000f: // (obsolete)
1582 // this op is ignored, but we need the glyph width
1583 if (firstOp) {
1584 cvtGlyphWidth(nOps > 0, charBuf, pDict);
1585 firstOp = gFalse;
1586 }
1587 nOps = 0;
1588 break;
1589 case 0x0010: // blend
1590 //~ error(-1, "Unimplemented Type 2 charstring op: %d", file[i]);
1591 nOps = 0;
1592 break;
1593 case 0x0012: // hstemhm
1594 // ignored
1595 if (firstOp) {
1596 cvtGlyphWidth(nOps & 1, charBuf, pDict);
1597 firstOp = gFalse;
1598 }
1599 if (nOps & 1) {
1600 //~ error(-1, "Wrong number of args (%d) to Type 2 hstemhm", nOps);
1601 }
1602 nHints += nOps / 2;
1603 nOps = 0;
1604 break;
1605 case 0x0013: // hintmask
1606 // ignored
1607 if (firstOp) {
1608 cvtGlyphWidth(nOps & 1, charBuf, pDict);
1609 firstOp = gFalse;
1610 }
1611 if (nOps > 0) {
1612 if (nOps & 1) {
1613 //~ error(-1, "Wrong number of args (%d) to Type 2 hintmask/vstemhm",
1614 //~ nOps);
1615 }
1616 nHints += nOps / 2;
1617 }
1618 pos += (nHints + 7) >> 3;
1619 nOps = 0;
1620 break;
1621 case 0x0014: // cntrmask
1622 // ignored
1623 if (firstOp) {
1624 cvtGlyphWidth(nOps & 1, charBuf, pDict);
1625 firstOp = gFalse;
1626 }
1627 if (nOps > 0) {
1628 if (nOps & 1) {
1629 //~ error(-1, "Wrong number of args (%d) to Type 2 cntrmask/vstemhm",
1630 //~ nOps);
1631 }
1632 nHints += nOps / 2;
1633 }
1634 pos += (nHints + 7) >> 3;
1635 nOps = 0;
1636 break;
1637 case 0x0015: // rmoveto
1638 if (firstOp) {
1639 cvtGlyphWidth(nOps == 3, charBuf, pDict);
1640 firstOp = gFalse;
1641 }
1642 if (openPath) {
1643 charBuf->append((char)9);
1644 openPath = gFalse;
1645 }
1646 if (nOps != 2) {
1647 //~ error(-1, "Wrong number of args (%d) to Type 2 rmoveto", nOps);
1648 }
1649 cvtNum(ops[0], charBuf);
1650 cvtNum(ops[1], charBuf);
1651 charBuf->append((char)21);
1652 nOps = 0;
1653 break;
1654 case 0x0016: // hmoveto
1655 if (firstOp) {
1656 cvtGlyphWidth(nOps == 2, charBuf, pDict);
1657 firstOp = gFalse;
1658 }
1659 if (openPath) {
1660 charBuf->append((char)9);
1661 openPath = gFalse;
1662 }
1663 if (nOps != 1) {
1664 //~ error(-1, "Wrong number of args (%d) to Type 2 hmoveto", nOps);
1665 }
1666 cvtNum(ops[0], charBuf);
1667 charBuf->append((char)22);
1668 nOps = 0;
1669 break;
1670 case 0x0017: // vstemhm
1671 // ignored
1672 if (firstOp) {
1673 cvtGlyphWidth(nOps & 1, charBuf, pDict);
1674 firstOp = gFalse;
1675 }
1676 if (nOps & 1) {
1677 //~ error(-1, "Wrong number of args (%d) to Type 2 vstemhm", nOps);
1678 }
1679 nHints += nOps / 2;
1680 nOps = 0;
1681 break;
1682 case 0x0018: // rcurveline
1683 if (nOps < 8 || (nOps - 2) % 6 != 0) {
1684 //~ error(-1, "Wrong number of args (%d) to Type 2 rcurveline", nOps);
1685 }
1686 for (k = 0; k <= nOps - 8; k += 6) {
1687 cvtNum(ops[k], charBuf);
1688 cvtNum(ops[k+1], charBuf);
1689 cvtNum(ops[k+2], charBuf);
1690 cvtNum(ops[k+3], charBuf);
1691 cvtNum(ops[k+4], charBuf);
1692 cvtNum(ops[k+5], charBuf);
1693 charBuf->append((char)8);
1694 }
1695 if (k+1 < nOps) {
1696 cvtNum(ops[k], charBuf);
1697 cvtNum(ops[k+1], charBuf);
1698 charBuf->append((char)5);
1699 }
1700 nOps = 0;
1701 openPath = gTrue;
1702 break;
1703 case 0x0019: // rlinecurve
1704 if (nOps < 8 || (nOps - 6) % 2 != 0) {
1705 //~ error(-1, "Wrong number of args (%d) to Type 2 rlinecurve", nOps);
1706 }
1707 for (k = 0; k <= nOps - 8; k += 2) {
1708 cvtNum(ops[k], charBuf);
1709 cvtNum(ops[k+1], charBuf);
1710 charBuf->append((char)5);
1711 }
1712 if (k+5 < nOps) {
1713 cvtNum(ops[k], charBuf);
1714 cvtNum(ops[k+1], charBuf);
1715 cvtNum(ops[k+2], charBuf);
1716 cvtNum(ops[k+3], charBuf);
1717 cvtNum(ops[k+4], charBuf);
1718 cvtNum(ops[k+5], charBuf);
1719 charBuf->append((char)8);
1720 }
1721 nOps = 0;
1722 openPath = gTrue;
1723 break;
1724 case 0x001a: // vvcurveto
1725 if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1726 //~ error(-1, "Wrong number of args (%d) to Type 2 vvcurveto", nOps);
1727 }
1728 if (nOps % 2 == 1 && nOps >= 5) {
1729 cvtNum(ops[0], charBuf);
1730 cvtNum(ops[1], charBuf);
1731 cvtNum(ops[2], charBuf);
1732 cvtNum(ops[3], charBuf);
1733 cvtNum(zero, charBuf);
1734 cvtNum(ops[4], charBuf);
1735 charBuf->append((char)8);
1736 k = 5;
1737 } else {
1738 k = 0;
1739 }
1740 for (; k+3 < nOps; k += 4) {
1741 cvtNum(zero, charBuf);
1742 cvtNum(ops[k], charBuf);
1743 cvtNum(ops[k+1], charBuf);
1744 cvtNum(ops[k+2], charBuf);
1745 cvtNum(zero, charBuf);
1746 cvtNum(ops[k+3], charBuf);
1747 charBuf->append((char)8);
1748 }
1749 nOps = 0;
1750 openPath = gTrue;
1751 break;
1752 case 0x001b: // hhcurveto
1753 if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1754 //~ error(-1, "Wrong number of args (%d) to Type 2 hhcurveto", nOps);
1755 }
1756 if (nOps % 2 == 1 && nOps >= 5) {
1757 cvtNum(ops[1], charBuf);
1758 cvtNum(ops[0], charBuf);
1759 cvtNum(ops[2], charBuf);
1760 cvtNum(ops[3], charBuf);
1761 cvtNum(ops[4], charBuf);
1762 cvtNum(zero, charBuf);
1763 charBuf->append((char)8);
1764 k = 5;
1765 } else {
1766 k = 0;
1767 }
1768 for (; k+3 < nOps; k += 4) {
1769 cvtNum(ops[k], charBuf);
1770 cvtNum(zero, charBuf);
1771 cvtNum(ops[k+1], charBuf);
1772 cvtNum(ops[k+2], charBuf);
1773 cvtNum(ops[k+3], charBuf);
1774 cvtNum(zero, charBuf);
1775 charBuf->append((char)8);
1776 }
1777 nOps = 0;
1778 openPath = gTrue;
1779 break;
1780 case 0x001d: // callgsubr
1781 if (nOps >= 1) {
1782 k = gsubrBias + ops[nOps - 1].toInt();
1783 --nOps;
1784 ok = gTrue;
1785 getIndexVal(&gsubrIdx, k, &val, &ok);
1786 if (ok) {
1787 cvtGlyph(val.pos, val.len, charBuf, subrIdx, pDict, gFalse,
1788 recursion + 1);
1789 }
1790 } else {
1791 //~ error(-1, "Too few args to Type 2 callgsubr");
1792 }
1793 // don't clear the stack
1794 break;
1795 case 0x001e: // vhcurveto
1796 if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1797 //~ error(-1, "Wrong number of args (%d) to Type 2 vhcurveto", nOps);
1798 }
1799 for (k = 0; k+3 < nOps && k != nOps-5; k += 4) {
1800 if (k % 8 == 0) {
1801 cvtNum(ops[k], charBuf);
1802 cvtNum(ops[k+1], charBuf);
1803 cvtNum(ops[k+2], charBuf);
1804 cvtNum(ops[k+3], charBuf);
1805 charBuf->append((char)30);
1806 } else {
1807 cvtNum(ops[k], charBuf);
1808 cvtNum(ops[k+1], charBuf);
1809 cvtNum(ops[k+2], charBuf);
1810 cvtNum(ops[k+3], charBuf);
1811 charBuf->append((char)31);
1812 }
1813 }
1814 if (k == nOps-5) {
1815 if (k % 8 == 0) {
1816 cvtNum(zero, charBuf);
1817 cvtNum(ops[k], charBuf);
1818 cvtNum(ops[k+1], charBuf);
1819 cvtNum(ops[k+2], charBuf);
1820 cvtNum(ops[k+3], charBuf);
1821 cvtNum(ops[k+4], charBuf);
1822 } else {
1823 cvtNum(ops[k], charBuf);
1824 cvtNum(zero, charBuf);
1825 cvtNum(ops[k+1], charBuf);
1826 cvtNum(ops[k+2], charBuf);
1827 cvtNum(ops[k+4], charBuf);
1828 cvtNum(ops[k+3], charBuf);
1829 }
1830 charBuf->append((char)8);
1831 }
1832 nOps = 0;
1833 openPath = gTrue;
1834 break;
1835 case 0x001f: // hvcurveto
1836 if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1837 //~ error(-1, "Wrong number of args (%d) to Type 2 hvcurveto", nOps);
1838 }
1839 for (k = 0; k+3 < nOps && k != nOps-5; k += 4) {
1840 if (k % 8 == 0) {
1841 cvtNum(ops[k], charBuf);
1842 cvtNum(ops[k+1], charBuf);
1843 cvtNum(ops[k+2], charBuf);
1844 cvtNum(ops[k+3], charBuf);
1845 charBuf->append((char)31);
1846 } else {
1847 cvtNum(ops[k], charBuf);
1848 cvtNum(ops[k+1], charBuf);
1849 cvtNum(ops[k+2], charBuf);
1850 cvtNum(ops[k+3], charBuf);
1851 charBuf->append((char)30);
1852 }
1853 }
1854 if (k == nOps-5) {
1855 if (k % 8 == 0) {
1856 cvtNum(ops[k], charBuf);
1857 cvtNum(zero, charBuf);
1858 cvtNum(ops[k+1], charBuf);
1859 cvtNum(ops[k+2], charBuf);
1860 cvtNum(ops[k+4], charBuf);
1861 cvtNum(ops[k+3], charBuf);
1862 } else {
1863 cvtNum(zero, charBuf);
1864 cvtNum(ops[k], charBuf);
1865 cvtNum(ops[k+1], charBuf);
1866 cvtNum(ops[k+2], charBuf);
1867 cvtNum(ops[k+3], charBuf);
1868 cvtNum(ops[k+4], charBuf);
1869 }
1870 charBuf->append((char)8);
1871 }
1872 nOps = 0;
1873 openPath = gTrue;
1874 break;
1875 case 0x0c00: // dotsection (should be Type 1 only?)
1876 // ignored
1877 nOps = 0;
1878 break;
1879 case 0x0c0c: // div
1880 if (nOps >= 2) {
1881 num = ops[nOps-2].toInt();
1882 den = ops[nOps-1].toInt();
1883 --nOps;
1884 ops[nOps-1].kind = type1COpRational;
1885 ops[nOps-1].rat.num = num;
1886 ops[nOps-1].rat.den = den;
1887 } else {
1888 //~ error(-1, "Wrong number of args (%d) to Type 2 div", nOps);
1889 }
1890 break;
1891 case 0x0c03: // and
1892 case 0x0c04: // or
1893 case 0x0c05: // not
1894 case 0x0c08: // store
1895 case 0x0c09: // abs
1896 case 0x0c0a: // add
1897 case 0x0c0b: // sub
1898 case 0x0c0d: // load
1899 case 0x0c0e: // neg
1900 case 0x0c0f: // eq
1901 case 0x0c12: // drop
1902 case 0x0c14: // put
1903 case 0x0c15: // get
1904 case 0x0c16: // ifelse
1905 case 0x0c17: // random
1906 case 0x0c18: // mul
1907 case 0x0c1a: // sqrt
1908 case 0x0c1b: // dup
1909 case 0x0c1c: // exch
1910 case 0x0c1d: // index
1911 case 0x0c1e: // roll
1912 //~ error(-1, "Unimplemented Type 2 charstring op: 12.%d", file[i+1]);
1913 nOps = 0;
1914 break;
1915 case 0x0c22: // hflex
1916 if (nOps == 7) {
1917 cvtNum(ops[0], charBuf);
1918 cvtNum(zero, charBuf);
1919 cvtNum(ops[1], charBuf);
1920 cvtNum(ops[2], charBuf);
1921 cvtNum(ops[3], charBuf);
1922 cvtNum(zero, charBuf);
1923 charBuf->append((char)8);
1924 cvtNum(ops[4], charBuf);
1925 cvtNum(zero, charBuf);
1926 cvtNum(ops[5], charBuf);
1927 if (ops[2].kind == type1COpInteger) {
1928 tmp.kind = type1COpInteger;
1929 tmp.intgr = -ops[2].intgr;
1930 } else if (ops[2].kind == type1COpRational) {
1931 tmp.kind = type1COpRational;
1932 tmp.rat.num = -ops[2].rat.num;
1933 tmp.rat.den = ops[2].rat.den;
1934 } else {
1935 tmp.kind = type1COpFloat;
1936 tmp.flt = -ops[2].toFloat();
1937 }
1938 cvtNum(tmp, charBuf);
1939 cvtNum(ops[6], charBuf);
1940 cvtNum(zero, charBuf);
1941 charBuf->append((char)8);
1942 } else {
1943 //~ error(-1, "Wrong number of args (%d) to Type 2 hflex", nOps);
1944 }
1945 nOps = 0;
1946 openPath = gTrue;
1947 break;
1948 case 0x0c23: // flex
1949 if (nOps == 13) {
1950 cvtNum(ops[0], charBuf);
1951 cvtNum(ops[1], charBuf);
1952 cvtNum(ops[2], charBuf);
1953 cvtNum(ops[3], charBuf);
1954 cvtNum(ops[4], charBuf);
1955 cvtNum(ops[5], charBuf);
1956 charBuf->append((char)8);
1957 cvtNum(ops[6], charBuf);
1958 cvtNum(ops[7], charBuf);
1959 cvtNum(ops[8], charBuf);
1960 cvtNum(ops[9], charBuf);
1961 cvtNum(ops[10], charBuf);
1962 cvtNum(ops[11], charBuf);
1963 charBuf->append((char)8);
1964 } else {
1965 //~ error(-1, "Wrong number of args (%d) to Type 2 flex", nOps);
1966 }
1967 nOps = 0;
1968 openPath = gTrue;
1969 break;
1970 case 0x0c24: // hflex1
1971 if (nOps == 9) {
1972 cvtNum(ops[0], charBuf);
1973 cvtNum(ops[1], charBuf);
1974 cvtNum(ops[2], charBuf);
1975 cvtNum(ops[3], charBuf);
1976 cvtNum(ops[4], charBuf);
1977 cvtNum(zero, charBuf);
1978 charBuf->append((char)8);
1979 cvtNum(ops[5], charBuf);
1980 cvtNum(zero, charBuf);
1981 cvtNum(ops[6], charBuf);
1982 cvtNum(ops[7], charBuf);
1983 cvtNum(ops[8], charBuf);
1984 if (ops[1].kind == type1COpInteger &&
1985 ops[3].kind == type1COpInteger &&
1986 ops[7].kind == type1COpInteger) {
1987 tmp.kind = type1COpInteger;
1988 tmp.intgr = -(ops[1].intgr + ops[3].intgr + ops[7].intgr);
1989 } else {
1990 tmp.kind = type1COpFloat;
1991 tmp.flt = -(ops[1].toFloat() + ops[3].toFloat() + ops[7].toFloat());
1992 }
1993 cvtNum(tmp, charBuf);
1994 charBuf->append((char)8);
1995 } else {
1996 //~ error(-1, "Wrong number of args (%d) to Type 2 hflex1", nOps);
1997 }
1998 nOps = 0;
1999 openPath = gTrue;
2000 break;
2001 case 0x0c25: // flex1
2002 if (nOps == 11) {
2003 cvtNum(ops[0], charBuf);
2004 cvtNum(ops[1], charBuf);
2005 cvtNum(ops[2], charBuf);
2006 cvtNum(ops[3], charBuf);
2007 cvtNum(ops[4], charBuf);
2008 cvtNum(ops[5], charBuf);
2009 charBuf->append((char)8);
2010 cvtNum(ops[6], charBuf);
2011 cvtNum(ops[7], charBuf);
2012 cvtNum(ops[8], charBuf);
2013 cvtNum(ops[9], charBuf);
2014 dx = ops[0].toFloat() + ops[2].toFloat() + ops[4].toFloat()
2015 + ops[6].toFloat() + ops[8].toFloat();
2016 dy = ops[1].toFloat() + ops[3].toFloat() + ops[5].toFloat()
2017 + ops[7].toFloat() + ops[9].toFloat();
2018 if (fabs(dx) > fabs(dy)) {
2019 cvtNum(ops[10], charBuf);
2020 if (ops[1].kind == type1COpInteger &&
2021 ops[3].kind == type1COpInteger &&
2022 ops[5].kind == type1COpInteger &&
2023 ops[7].kind == type1COpInteger &&
2024 ops[9].kind == type1COpInteger) {
2025 tmp.kind = type1COpInteger;
2026 tmp.intgr = -(int)dy;
2027 } else {
2028 tmp.kind = type1COpFloat;
2029 tmp.flt = -dy;
2030 }
2031 cvtNum(tmp, charBuf);
2032 } else {
2033 if (ops[0].kind == type1COpInteger &&
2034 ops[2].kind == type1COpInteger &&
2035 ops[4].kind == type1COpInteger &&
2036 ops[6].kind == type1COpInteger &&
2037 ops[8].kind == type1COpInteger) {
2038 tmp.kind = type1COpInteger;
2039 tmp.intgr = -(int)dx;
2040 } else {
2041 tmp.kind = type1COpFloat;
2042 tmp.flt = -dx;
2043 }
2044 cvtNum(tmp, charBuf);
2045 cvtNum(ops[10], charBuf);
2046 }
2047 charBuf->append((char)8);
2048 } else {
2049 //~ error(-1, "Wrong number of args (%d) to Type 2 flex1", nOps);
2050 }
2051 nOps = 0;
2052 openPath = gTrue;
2053 break;
2054 default:
2055 //~ error(-1, "Illegal Type 2 charstring op: %04x",
2056 //~ ops[nOps].op);
2057 nOps = 0;
2058 break;
2059 }
2060 }
2061 }
2062
2063 // charstring encryption
2064 if (top) {
2065 r2 = 4330;
2066 for (i = start; i < charBuf->getLength(); ++i) {
2067 byte = (Guchar)(charBuf->getChar(i) ^ (r2 >> 8));
2068 charBuf->setChar(i, byte);
2069 r2 = (Gushort)((byte + r2) * 52845 + 22719);
2070 }
2071 }
2072 }
2073
2074 void FoFiType1C::cvtGlyphWidth(GBool useOp, GString *charBuf,
2075 Type1CPrivateDict *pDict) {
2076 Type1COp zero, w;
2077 int i;
2078
2079 if (useOp) {
2080 if (pDict->nominalWidthXInt & (ops[0].kind == type1COpInteger)) {
2081 w.kind = type1COpInteger;
2082 w.intgr = (int)(pDict->nominalWidthX + ops[0].intgr);
2083 } else {
2084 w.kind = type1COpFloat;
2085 w.flt = pDict->nominalWidthX + ops[0].toFloat();
2086 }
2087 for (i = 1; i < nOps; ++i) {
2088 ops[i-1] = ops[i];
2089 }
2090 --nOps;
2091 } else {
2092 if (pDict->defaultWidthXInt) {
2093 w.kind = type1COpInteger;
2094 w.intgr = (int)pDict->defaultWidthX;
2095 } else {
2096 w.kind = type1COpFloat;
2097 w.flt = pDict->defaultWidthX;
2098 }
2099 }
2100 zero.kind = type1COpInteger;
2101 zero.intgr = 0;
2102 cvtNum(zero, charBuf);
2103 cvtNum(w, charBuf);
2104 charBuf->append((char)13);
2105 }
2106
2107 void FoFiType1C::cvtNum(Type1COp op, GString *charBuf) {
2108 Type1COp tmp;
2109 Guchar buf[12];
2110 int y, n;
2111
2112 n = 0;
2113 switch (op.kind) {
2114 case type1COpInteger:
2115 y = op.intgr;
2116 if (y >= -107 && y <= 107) {
2117 buf[0] = (Guchar)(y + 139);
2118 n = 1;
2119 } else if (y > 107 && y <= 1131) {
2120 y -= 108;
2121 buf[0] = (Guchar)((y >> 8) + 247);
2122 buf[1] = (Guchar)(y & 0xff);
2123 n = 2;
2124 } else if (y < -107 && y >= -1131) {
2125 y = -y - 108;
2126 buf[0] = (Guchar)((y >> 8) + 251);
2127 buf[1] = (Guchar)(y & 0xff);
2128 n = 2;
2129 } else {
2130 buf[0] = 255;
2131 buf[1] = (Guchar)(y >> 24);
2132 buf[2] = (Guchar)(y >> 16);
2133 buf[3] = (Guchar)(y >> 8);
2134 buf[4] = (Guchar)y;
2135 n = 5;
2136 }
2137 break;
2138 case type1COpFloat:
2139 if (op.flt >= -32768 && op.flt < 32768) {
2140 y = (int)(op.flt * 256.0);
2141 buf[0] = 255;
2142 buf[1] = (Guchar)(y >> 24);
2143 buf[2] = (Guchar)(y >> 16);
2144 buf[3] = (Guchar)(y >> 8);
2145 buf[4] = (Guchar)y;
2146 buf[5] = 255;
2147 buf[6] = 0;
2148 buf[7] = 0;
2149 buf[8] = 1;
2150 buf[9] = 0;
2151 buf[10] = 12;
2152 buf[11] = 12;
2153 n = 12;
2154 } else {
2155 //~ error(-1, "Type 2 fixed point constant out of range");
2156 }
2157 break;
2158 case type1COpRational:
2159 tmp.kind = type1COpInteger;
2160 tmp.intgr = op.rat.num;
2161 cvtNum(tmp, charBuf);
2162 tmp.intgr = op.rat.den;
2163 cvtNum(tmp, charBuf);
2164 buf[0] = 0x0c;
2165 buf[1] = 0x0c;
2166 n = 2;
2167 break;
2168 default: // shouldn't happen
2169 break;
2170 }
2171 charBuf->append((char *)buf, n);
2172 }
2173
2174 void FoFiType1C::eexecWrite(Type1CEexecBuf *eb, const char *s) {
2175 Guchar *p;
2176 Guchar x;
2177
2178 for (p = (Guchar *)s; *p; ++p) {
2179 x = *p ^ (Guchar)(eb->r1 >> 8);
2180 eb->r1 = (Gushort)((x + eb->r1) * 52845 + 22719);
2181 if (eb->ascii) {
2182 (*eb->outputFunc)(eb->outputStream, &hexChars[x >> 4], 1);
2183 (*eb->outputFunc)(eb->outputStream, &hexChars[x & 0x0f], 1);
2184 eb->line += 2;
2185 if (eb->line == 64) {
2186 (*eb->outputFunc)(eb->outputStream, "\n", 1);
2187 eb->line = 0;
2188 }
2189 } else {
2190 (*eb->outputFunc)(eb->outputStream, (char *)&x, 1);
2191 }
2192 }
2193 }
2194
2195 void FoFiType1C::eexecWriteCharstring(Type1CEexecBuf *eb,
2196 Guchar *s, int n) {
2197 Guchar x;
2198 int i;
2199
2200 // eexec encryption
2201 for (i = 0; i < n; ++i) {
2202 x = s[i] ^ (Guchar)(eb->r1 >> 8);
2203 eb->r1 = (Gushort)((x + eb->r1) * 52845 + 22719);
2204 if (eb->ascii) {
2205 (*eb->outputFunc)(eb->outputStream, &hexChars[x >> 4], 1);
2206 (*eb->outputFunc)(eb->outputStream, &hexChars[x & 0x0f], 1);
2207 eb->line += 2;
2208 if (eb->line == 64) {
2209 (*eb->outputFunc)(eb->outputStream, "\n", 1);
2210 eb->line = 0;
2211 }
2212 } else {
2213 (*eb->outputFunc)(eb->outputStream, (char *)&x, 1);
2214 }
2215 }
2216 }
2217
2218 void FoFiType1C::writePSString(char *s, FoFiOutputFunc outputFunc,
2219 void *outputStream) {
2220 char buf[80];
2221 char *p;
2222 int i, c;
2223
2224 i = 0;
2225 buf[i++] = '(';
2226 for (p = s; *p; ++p) {
2227 c = *p & 0xff;
2228 if (c == '(' || c == ')' || c == '\\') {
2229 buf[i++] = '\\';
2230 buf[i++] = (char)c;
2231 } else if (c < 0x20 || c >= 0x80) {
2232 buf[i++] = '\\';
2233 buf[i++] = (char)('0' + ((c >> 6) & 7));
2234 buf[i++] = (char)('0' + ((c >> 3) & 7));
2235 buf[i++] = (char)('0' + (c & 7));
2236 } else {
2237 buf[i++] = (char)c;
2238 }
2239 if (i >= 64) {
2240 buf[i++] = '\\';
2241 buf[i++] = '\n';
2242 (*outputFunc)(outputStream, buf, i);
2243 i = 0;
2244 }
2245 }
2246 buf[i++] = ')';
2247 (*outputFunc)(outputStream, buf, i);
2248 }
2249
2250 void FoFiType1C::convertToOpenType(FoFiOutputFunc outputFunc,
2251 void *outputStream,
2252 int nWidths, Gushort *widths,
2253 Guchar *cmapTable, int cmapTableLen) {
2254 Guchar os2Table[96], headTable[54], hheaTable[36], maxpTable[6];
2255 Guchar nameTable[26], postTable[32];
2256 Guchar *hmtxTable;
2257 static const char *tableTag[9] = {
2258 "CFF ",
2259 "OS/2",
2260 "cmap",
2261 "head",
2262 "hhea",
2263 "hmtx",
2264 "maxp",
2265 "name",
2266 "post"
2267 };
2268 Guchar *tableData[9];
2269 int tableLength[9];
2270 Guchar header[12 + 9*16];
2271 double mat[6];
2272 Gushort maxWidth;
2273 Guint checksum, fileChecksum;
2274 int unitsPerEm, xMin, yMin, xMax, yMax, ascent, descent, offset, i;
2275
2276 xMin = (int)(topDict.fontBBox[0] + 0.5);
2277 yMin = (int)(topDict.fontBBox[1] + 0.5);
2278 xMax = (int)(topDict.fontBBox[2] + 0.5);
2279 yMax = (int)(topDict.fontBBox[3] + 0.5);
2280 ascent = yMax;
2281 descent = -yMin;
2282
2283 //--- CFF_ table
2284 tableData[0] = file;
2285 tableLength[0] = len;
2286
2287 //--- OS/2 table
2288 os2Table[ 0] = 0; // version
2289 os2Table[ 1] = 4;
2290 os2Table[ 2] = 0; // xAvgCharWidth
2291 os2Table[ 3] = 1;
2292 os2Table[ 4] = 0x01; // usWeightClass
2293 os2Table[ 5] = 0x90;
2294 os2Table[ 6] = 0; // usWidthClass
2295 os2Table[ 7] = 5;
2296 os2Table[ 8] = 0; // fsType
2297 os2Table[ 9] = 0;
2298 os2Table[10] = 0; // ySubscriptXSize
2299 os2Table[11] = 0;
2300 os2Table[12] = 0; // ySubscriptYSize
2301 os2Table[13] = 0;
2302 os2Table[14] = 0; // ySubscriptXOffset
2303 os2Table[15] = 0;
2304 os2Table[16] = 0; // ySubscriptYOffset
2305 os2Table[17] = 0;
2306 os2Table[18] = 0; // ySuperscriptXSize
2307 os2Table[19] = 0;
2308 os2Table[20] = 0; // ySuperscriptYSize
2309 os2Table[21] = 0;
2310 os2Table[22] = 0; // ySuperscriptXOffset
2311 os2Table[23] = 0;
2312 os2Table[24] = 0; // ySuperscriptYOffset
2313 os2Table[25] = 0;
2314 os2Table[26] = 0; // yStrikeoutSize
2315 os2Table[27] = 0;
2316 os2Table[28] = 0; // yStrikeoutPosition
2317 os2Table[29] = 0;
2318 os2Table[30] = 0; // sFamilyClass
2319 os2Table[31] = 0;
2320 os2Table[32] = 0; // panose
2321 os2Table[33] = 0;
2322 os2Table[34] = 0;
2323 os2Table[35] = 0;
2324 os2Table[36] = 0;
2325 os2Table[37] = 0;
2326 os2Table[38] = 0;
2327 os2Table[39] = 0;
2328 os2Table[40] = 0;
2329 os2Table[41] = 0;
2330 os2Table[42] = 0; // ulUnicodeRange1
2331 os2Table[43] = 0;
2332 os2Table[44] = 0;
2333 os2Table[45] = 0;
2334 os2Table[46] = 0; // ulUnicodeRange2
2335 os2Table[47] = 0;
2336 os2Table[48] = 0;
2337 os2Table[49] = 0;
2338 os2Table[50] = 0; // ulUnicodeRange3
2339 os2Table[51] = 0;
2340 os2Table[52] = 0;
2341 os2Table[53] = 0;
2342 os2Table[54] = 0; // ulUnicodeRange4
2343 os2Table[55] = 0;
2344 os2Table[56] = 0;
2345 os2Table[57] = 0;
2346 os2Table[58] = 0; // achVendID
2347 os2Table[59] = 0;
2348 os2Table[60] = 0;
2349 os2Table[61] = 0;
2350 os2Table[62] = 0; // fsSelection
2351 os2Table[63] = 0;
2352 os2Table[64] = 0; // usFirstCharIndex
2353 os2Table[65] = 0;
2354 os2Table[66] = 0; // usLastCharIndex
2355 os2Table[67] = 0;
2356 os2Table[68] = 0; // sTypoAscender
2357 os2Table[69] = 0;
2358 os2Table[70] = 0; // sTypoDescender
2359 os2Table[71] = 0;
2360 os2Table[72] = 0; // sTypoLineGap
2361 os2Table[73] = 0;
2362 os2Table[74] = (Guchar)(ascent >> 8); // usWinAscent
2363 os2Table[75] = (Guchar)ascent;
2364 os2Table[76] = (Guchar)(descent >> 8); // usWinDescent
2365 os2Table[77] = (Guchar)descent;
2366 os2Table[78] = 0; // ulCodePageRange1
2367 os2Table[79] = 0;
2368 os2Table[80] = 0;
2369 os2Table[81] = 1;
2370 os2Table[82] = 0; // ulCodePageRange2
2371 os2Table[83] = 0;
2372 os2Table[84] = 0;
2373 os2Table[85] = 0;
2374 os2Table[86] = 0; // sxHeight
2375 os2Table[87] = 0;
2376 os2Table[88] = 0; // sCapHeight
2377 os2Table[89] = 0;
2378 os2Table[90] = 0; // usDefaultChar
2379 os2Table[91] = 0;
2380 os2Table[92] = 0; // usBreakChar
2381 os2Table[93] = 0;
2382 os2Table[94] = 0; // usMaxContext
2383 os2Table[95] = 0;
2384 tableData[1] = os2Table;
2385 tableLength[1] = 96;
2386
2387 //--- cmap table
2388 tableData[2] = cmapTable;
2389 tableLength[2] = cmapTableLen;
2390
2391 //--- head table
2392 getFontMatrix(mat);
2393 if (mat[0] == 0) {
2394 unitsPerEm = 1000;
2395 } else {
2396 unitsPerEm = (int)(1 / mat[0] + 0.5);
2397 }
2398 headTable[ 0] = 0x00; // version
2399 headTable[ 1] = 0x01;
2400 headTable[ 2] = 0x00;
2401 headTable[ 3] = 0x00;
2402 headTable[ 4] = 0x00; // revision
2403 headTable[ 5] = 0x01; // (needs to be non-zero)
2404 headTable[ 6] = 0x00;
2405 headTable[ 7] = 0x01;
2406 headTable[ 8] = 0x00; // checksumAdjustment
2407 headTable[ 9] = 0x00; // (set later)
2408 headTable[10] = 0x00;
2409 headTable[11] = 0x00;
2410 headTable[12] = 0x5f; // magicNumber
2411 headTable[13] = 0x0f;
2412 headTable[14] = 0x3c;
2413 headTable[15] = 0xf5;
2414 headTable[16] = 0x00; // flags
2415 headTable[17] = 0x03;
2416 headTable[18] = (Guchar)(unitsPerEm >> 8); // unitsPerEm
2417 headTable[19] = (Guchar)unitsPerEm;
2418 headTable[20] = 0x00; // created
2419 headTable[21] = 0x00;
2420 headTable[22] = 0x00;
2421 headTable[23] = 0x00;
2422 headTable[24] = 0x00;
2423 headTable[25] = 0x00;
2424 headTable[26] = 0x00;
2425 headTable[27] = 0x00;
2426 headTable[28] = 0x00; // modified
2427 headTable[29] = 0x00;
2428 headTable[30] = 0x00;
2429 headTable[31] = 0x00;
2430 headTable[32] = 0x00;
2431 headTable[33] = 0x00;
2432 headTable[34] = 0x00;
2433 headTable[35] = 0x00;
2434 headTable[36] = (Guchar)(xMin >> 8); // xMin
2435 headTable[37] = (Guchar)xMin;
2436 headTable[38] = (Guchar)(yMin >> 8); // yMin
2437 headTable[39] = (Guchar)yMin;
2438 headTable[40] = (Guchar)(xMax >> 8); // xMax
2439 headTable[41] = (Guchar)xMax;
2440 headTable[42] = (Guchar)(yMax >> 8); // yMax
2441 headTable[43] = (Guchar)yMax;
2442 headTable[44] = 0; // macStyle
2443 headTable[45] = 0;
2444 headTable[46] = 0; // lowestRecPPEM
2445 headTable[47] = 3;
2446 headTable[48] = 0; // fontDirectionHint
2447 headTable[49] = 2; // (deprecated)
2448 headTable[50] = 0; // indexToLocFormat
2449 headTable[51] = 0; // (n/a to CFF fonts)
2450 headTable[52] = 0; // glyphDataFormat
2451 headTable[53] = 0; // (n/a to CFF fonts)
2452 tableData[3] = headTable;
2453 tableLength[3] = 54;
2454
2455 //--- hhea table
2456 maxWidth = 0;
2457 for (i = 0; i < nWidths; ++i) {
2458 if (widths[i] > maxWidth) {
2459 maxWidth = widths[i];
2460 }
2461 }
2462 hheaTable[ 0] = 0x00; // version
2463 hheaTable[ 1] = 0x01;
2464 hheaTable[ 2] = 0x00;
2465 hheaTable[ 3] = 0x00;
2466 hheaTable[ 4] = (Guchar)(yMax >> 8); // ascender
2467 hheaTable[ 5] = (Guchar)yMax;
2468 hheaTable[ 6] = (Guchar)(yMin >> 8); // descender
2469 hheaTable[ 7] = (Guchar)yMin;
2470 hheaTable[ 8] = 0; // lineGap
2471 hheaTable[ 9] = 0;
2472 hheaTable[10] = (Guchar)(maxWidth >> 8); // advanceWidthMax
2473 hheaTable[11] = (Guchar)maxWidth;
2474 hheaTable[12] = 0; // minLeftSideBearing
2475 hheaTable[13] = 0;
2476 hheaTable[14] = 0; // minRightSideBearing
2477 hheaTable[15] = 0;
2478 hheaTable[16] = (Guchar)(maxWidth >> 8); // xMaxExtent
2479 hheaTable[17] = (Guchar)maxWidth;
2480 hheaTable[18] = 0; // caretSlopeRise
2481 hheaTable[19] = 1;
2482 hheaTable[20] = 0; // caretSlopeRun
2483 hheaTable[21] = 0;
2484 hheaTable[22] = 0; // caretOffset
2485 hheaTable[23] = 0;
2486 hheaTable[24] = 0; // reserved
2487 hheaTable[25] = 0;
2488 hheaTable[26] = 0; // reserved
2489 hheaTable[27] = 0;
2490 hheaTable[28] = 0; // reserved
2491 hheaTable[29] = 0;
2492 hheaTable[30] = 0; // reserved
2493 hheaTable[31] = 0;
2494 hheaTable[32] = 0; // metricDataFormat
2495 hheaTable[33] = 0;
2496 hheaTable[34] = (Guchar)(nWidths >> 8); // numberOfHMetrics
2497 hheaTable[35] = (Guchar)nWidths;
2498 tableData[4] = hheaTable;
2499 tableLength[4] = 36;
2500
2501 //--- hmtx table
2502 //~ this currently sets LSB to 0 for all glyphs
2503 hmtxTable = (Guchar *)gmallocn(nWidths, 4);
2504 for (i = 0; i < nWidths; ++i) {
2505 hmtxTable[4*i ] = (Guchar)(widths[i] >> 8);
2506 hmtxTable[4*i+1] = (Guchar)widths[i];
2507 hmtxTable[4*i+2] = 0;
2508 hmtxTable[4*i+3] = 0;
2509 }
2510 tableData[5] = hmtxTable;
2511 tableLength[5] = 4 * nWidths;
2512
2513 //--- maxp table
2514 maxpTable[0] = 0x00; // version = 0.5
2515 maxpTable[1] = 0x00;
2516 maxpTable[2] = 0x50;
2517 maxpTable[3] = 0x00;
2518 maxpTable[4] = (Guchar)(nGlyphs >> 8); // numGlyphs
2519 maxpTable[5] = (Guchar)nGlyphs;
2520 tableData[6] = maxpTable;
2521 tableLength[6] = 6;
2522
2523 //--- name table
2524 nameTable[ 0] = 0x00; // format
2525 nameTable[ 1] = 0x00;
2526 nameTable[ 2] = 0x00; // count
2527 nameTable[ 3] = 0x01;
2528 nameTable[ 4] = 0x00; // stringOffset
2529 nameTable[ 5] = 0x12;
2530 nameTable[ 6] = 0x00; // platformID
2531 nameTable[ 7] = 0x00;
2532 nameTable[ 8] = 0x00; // encodingID
2533 nameTable[ 9] = 0x03;
2534 nameTable[10] = 0x00; // languageID
2535 nameTable[11] = 0x00;
2536 nameTable[12] = 0x00; // nameID
2537 nameTable[13] = 0x00;
2538 nameTable[14] = 0x00; // length
2539 nameTable[15] = 0x08;
2540 nameTable[16] = 0x00; // offset
2541 nameTable[17] = 0x00;
2542 nameTable[18] = 0x00; // string data
2543 nameTable[19] = (Guchar)'n';
2544 nameTable[20] = 0x00;
2545 nameTable[21] = (Guchar)'o';
2546 nameTable[22] = 0x00;
2547 nameTable[23] = (Guchar)'n';
2548 nameTable[24] = 0x00;
2549 nameTable[25] = (Guchar)'e';
2550 tableData[7] = nameTable;
2551 tableLength[7] = 26;
2552
2553 //--- post table
2554 postTable[ 0] = 0x00; // version = 3.0
2555 postTable[ 1] = 0x03;
2556 postTable[ 2] = 0x00;
2557 postTable[ 3] = 0x00;
2558 postTable[ 4] = 0x00; // italicAngle
2559 postTable[ 5] = 0x00;
2560 postTable[ 6] = 0x00;
2561 postTable[ 7] = 0x00;
2562 postTable[ 8] = 0x00; // underlinePosition
2563 postTable[ 9] = 0x00;
2564 postTable[10] = 0x00; // underlineThickness
2565 postTable[11] = 0x00;
2566 postTable[12] = 0x00; // isFixedPitch
2567 postTable[13] = 0x00;
2568 postTable[14] = 0x00;
2569 postTable[15] = 0x00;
2570 postTable[16] = 0x00; // minMemType42
2571 postTable[17] = 0x00;
2572 postTable[18] = 0x00;
2573 postTable[19] = 0x00;
2574 postTable[20] = 0x00; // maxMemType42
2575 postTable[21] = 0x00;
2576 postTable[22] = 0x00;
2577 postTable[23] = 0x00;
2578 postTable[24] = 0x00; // minMemType1
2579 postTable[25] = 0x00;
2580 postTable[26] = 0x00;
2581 postTable[27] = 0x00;
2582 postTable[28] = 0x00; // maxMemType1
2583 postTable[29] = 0x00;
2584 postTable[30] = 0x00;
2585 postTable[31] = 0x00;
2586 tableData[8] = postTable;
2587 tableLength[8] = 32;
2588
2589 //--- header and table directory
2590 header[ 0] = 'O'; // sfnt version
2591 header[ 1] = 'T';
2592 header[ 2] = 'T';
2593 header[ 3] = 'O';
2594 header[ 4] = 0x00; // numTables
2595 header[ 5] = 0x09;
2596 header[ 6] = 0x00; // searchRange
2597 header[ 7] = 0x80;
2598 header[ 8] = 0x00; // entrySelector
2599 header[ 9] = 0x03;
2600 header[10] = 0x00; // rangeShift
2601 header[11] = 0x10;
2602 offset = 12 + 9*16;
2603 fileChecksum = 0;
2604 for (i = 0; i < 9; ++i) {
2605 header[12 + i*16 + 0] = tableTag[i][0];
2606 header[12 + i*16 + 1] = tableTag[i][1];
2607 header[12 + i*16 + 2] = tableTag[i][2];
2608 header[12 + i*16 + 3] = tableTag[i][3];
2609 checksum = computeOpenTypeTableChecksum(tableData[i], tableLength[i]);
2610 fileChecksum += checksum;
2611 header[12 + i*16 + 4] = (Guchar)(checksum >> 24);
2612 header[12 + i*16 + 5] = (Guchar)(checksum >> 16);
2613 header[12 + i*16 + 6] = (Guchar)(checksum >> 8);
2614 header[12 + i*16 + 7] = (Guchar)checksum;
2615 header[12 + i*16 + 8] = (Guchar)(offset >> 24);
2616 header[12 + i*16 + 9] = (Guchar)(offset >> 16);
2617 header[12 + i*16 + 10] = (Guchar)(offset >> 8);
2618 header[12 + i*16 + 11] = (Guchar)offset;
2619 header[12 + i*16 + 12] = (Guchar)(tableLength[i] >> 24);
2620 header[12 + i*16 + 13] = (Guchar)(tableLength[i] >> 16);
2621 header[12 + i*16 + 14] = (Guchar)(tableLength[i] >> 8);
2622 header[12 + i*16 + 15] = (Guchar)tableLength[i];
2623 offset += tableLength[i];
2624 if (tableLength[i] & 3) {
2625 offset += 4 - (tableLength[i] & 3);
2626 }
2627 }
2628
2629 //--- file checksum
2630 fileChecksum += computeOpenTypeTableChecksum(header, 12 + 9*16);
2631 fileChecksum = 0xb1b0afba - fileChecksum;
2632 headTable[ 8] = (Guchar)(fileChecksum >> 24);
2633 headTable[ 9] = (Guchar)(fileChecksum >> 16);
2634 headTable[10] = (Guchar)(fileChecksum >> 8);
2635 headTable[11] = (Guchar)fileChecksum;
2636
2637 //--- write the OpenType font
2638 (*outputFunc)(outputStream, (char *)header, 12 + 9*16);
2639 for (i = 0; i < 9; ++i) {
2640 (*outputFunc)(outputStream, (char *)tableData[i], tableLength[i]);
2641 if (tableLength[i] & 3) {
2642 (*outputFunc)(outputStream, "\0\0\0", 4 - (tableLength[i] & 3));
2643 }
2644 }
2645
2646 gfree(hmtxTable);
2647 }
2648
2649 Guint FoFiType1C::computeOpenTypeTableChecksum(Guchar *data, int length) {
2650 Guint checksum, word;
2651 int i;
2652
2653 checksum = 0;
2654 for (i = 0; i+3 < length; i += 4) {
2655 word = ((data[i ] & 0xff) << 24) +
2656 ((data[i+1] & 0xff) << 16) +
2657 ((data[i+2] & 0xff) << 8) +
2658 (data[i+3] & 0xff);
2659 checksum += word;
2660 }
2661 if (length & 3) {
2662 word = 0;
2663 i = length & ~3;
2664 switch (length & 3) {
2665 case 3:
2666 word |= (data[i+2] & 0xff) << 8;
2667 case 2:
2668 word |= (data[i+1] & 0xff) << 16;
2669 case 1:
2670 word |= (data[i ] & 0xff) << 24;
2671 break;
2672 }
2673 checksum += word;
2674 }
2675 return checksum;
2676 }
2677
2678 GBool FoFiType1C::parse() {
2679 Type1CIndex fdIdx;
2680 Type1CIndexVal val;
2681 int i;
2682
2683 parsedOk = gTrue;
2684
2685 // some tools embed Type 1C fonts with an extra whitespace char at
2686 // the beginning
2687 if (len > 0 && file[0] != '\x01') {
2688 ++file;
2689 --len;
2690 }
2691
2692 // find the indexes
2693 getIndex(getU8(2, &parsedOk), &nameIdx, &parsedOk);
2694 getIndex(nameIdx.endPos, &topDictIdx, &parsedOk);
2695 getIndex(topDictIdx.endPos, &stringIdx, &parsedOk);
2696 getIndex(stringIdx.endPos, &gsubrIdx, &parsedOk);
2697 if (!parsedOk) {
2698 return gFalse;
2699 }
2700 gsubrBias = (gsubrIdx.len < 1240) ? 107
2701 : (gsubrIdx.len < 33900) ? 1131 : 32768;
2702
2703 // read the first font name
2704 getIndexVal(&nameIdx, 0, &val, &parsedOk);
2705 if (!parsedOk) {
2706 return gFalse;
2707 }
2708 name = new GString((char *)&file[val.pos], val.len);
2709
2710 // read the top dict for the first font
2711 readTopDict();
2712
2713 // for CID fonts: read the FDArray dicts and private dicts
2714 if (topDict.firstOp == 0x0c1e) {
2715 if (topDict.fdArrayOffset == 0) {
2716 nFDs = 1;
2717 privateDicts = (Type1CPrivateDict *)gmalloc(sizeof(Type1CPrivateDict));
2718 readPrivateDict(0, 0, &privateDicts[0]);
2719 } else {
2720 getIndex(topDict.fdArrayOffset, &fdIdx, &parsedOk);
2721 if (!parsedOk) {
2722 return gFalse;
2723 }
2724 nFDs = fdIdx.len;
2725 if (nFDs < 1) {
2726 parsedOk = gFalse;
2727 return gFalse;
2728 }
2729 privateDicts = (Type1CPrivateDict *)
2730 gmallocn(nFDs, sizeof(Type1CPrivateDict));
2731 for (i = 0; i < nFDs; ++i) {
2732 getIndexVal(&fdIdx, i, &val, &parsedOk);
2733 if (!parsedOk) {
2734 return gFalse;
2735 }
2736 readFD(val.pos, val.len, &privateDicts[i]);
2737 }
2738 }
2739
2740 // for 8-bit fonts: read the private dict
2741 } else {
2742 nFDs = 1;
2743 privateDicts = (Type1CPrivateDict *)gmalloc(sizeof(Type1CPrivateDict));
2744 readPrivateDict(topDict.privateOffset, topDict.privateSize,
2745 &privateDicts[0]);
2746 }
2747
2748 // check for parse errors in the private dict(s)
2749 if (!parsedOk) {
2750 return gFalse;
2751 }
2752
2753 // get the charstrings index
2754 if (topDict.charStringsOffset <= 0) {
2755 parsedOk = gFalse;
2756 return gFalse;
2757 }
2758 getIndex(topDict.charStringsOffset, &charStringsIdx, &parsedOk);
2759 if (!parsedOk) {
2760 return gFalse;
2761 }
2762 nGlyphs = charStringsIdx.len;
2763
2764 // for CID fonts: read the FDSelect table
2765 if (topDict.firstOp == 0x0c1e) {
2766 readFDSelect();
2767 if (!parsedOk) {
2768 return gFalse;
2769 }
2770 }
2771
2772 // read the charset
2773 if (!readCharset()) {
2774 parsedOk = gFalse;
2775 return gFalse;
2776 }
2777
2778 // for 8-bit fonts: build the encoding
2779 if (topDict.firstOp != 0x0c14 && topDict.firstOp != 0x0c1e) {
2780 buildEncoding();
2781 if (!parsedOk) {
2782 return gFalse;
2783 }
2784 }
2785
2786 return parsedOk;
2787 }
2788
2789 void FoFiType1C::readTopDict() {
2790 Type1CIndexVal topDictPtr;
2791 int pos;
2792
2793 topDict.firstOp = -1;
2794 topDict.versionSID = 0;
2795 topDict.noticeSID = 0;
2796 topDict.copyrightSID = 0;
2797 topDict.fullNameSID = 0;
2798 topDict.familyNameSID = 0;
2799 topDict.weightSID = 0;
2800 topDict.isFixedPitch = 0;
2801 topDict.italicAngle = 0;
2802 topDict.underlinePosition = -100;
2803 topDict.underlineThickness = 50;
2804 topDict.paintType = 0;
2805 topDict.charstringType = 2;
2806 topDict.fontMatrix[0] = 0.001;
2807 topDict.fontMatrix[1] = 0;
2808 topDict.fontMatrix[2] = 0;
2809 topDict.fontMatrix[3] = 0.001;
2810 topDict.fontMatrix[4] = 0;
2811 topDict.fontMatrix[5] = 0;
2812 topDict.hasFontMatrix = gFalse;
2813 topDict.uniqueID = 0;
2814 topDict.fontBBox[0] = 0;
2815 topDict.fontBBox[1] = 0;
2816 topDict.fontBBox[2] = 0;
2817 topDict.fontBBox[3] = 0;
2818 topDict.strokeWidth = 0;
2819 topDict.charsetOffset = 0;
2820 topDict.encodingOffset = 0;
2821 topDict.charStringsOffset = 0;
2822 topDict.privateSize = 0;
2823 topDict.privateOffset = 0;
2824 topDict.registrySID = 0;
2825 topDict.orderingSID = 0;
2826 topDict.supplement = 0;
2827 topDict.fdArrayOffset = 0;
2828 topDict.fdSelectOffset = 0;
2829
2830 getIndexVal(&topDictIdx, 0, &topDictPtr, &parsedOk);
2831 pos = topDictPtr.pos;
2832 nOps = 0;
2833 while (pos < topDictPtr.pos + topDictPtr.len) {
2834 pos = getOp(pos, gFalse, &parsedOk);
2835 if (!parsedOk) {
2836 break;
2837 }
2838 if (ops[nOps - 1].kind == type1COpOperator) {
2839 --nOps; // drop the operator
2840 if (topDict.firstOp < 0) {
2841 topDict.firstOp = ops[nOps].op;
2842 }
2843 switch (ops[nOps].op) {
2844 case 0x0000: topDict.versionSID = ops[0].toInt(); break;
2845 case 0x0001: topDict.noticeSID = ops[0].toInt(); break;
2846 case 0x0c00: topDict.copyrightSID = ops[0].toInt(); break;
2847 case 0x0002: topDict.fullNameSID = ops[0].toInt(); break;
2848 case 0x0003: topDict.familyNameSID = ops[0].toInt(); break;
2849 case 0x0004: topDict.weightSID = ops[0].toInt(); break;
2850 case 0x0c01: topDict.isFixedPitch = ops[0].toInt(); break;
2851 case 0x0c02: topDict.italicAngle = ops[0].toFloat(); break;
2852 case 0x0c03: topDict.underlinePosition = ops[0].toFloat(); break;
2853 case 0x0c04: topDict.underlineThickness = ops[0].toFloat(); break;
2854 case 0x0c05: topDict.paintType = ops[0].toInt(); break;
2855 case 0x0c06: topDict.charstringType = ops[0].toInt(); break;
2856 case 0x0c07: topDict.fontMatrix[0] = ops[0].toFloat();
2857 topDict.fontMatrix[1] = ops[1].toFloat();
2858 topDict.fontMatrix[2] = ops[2].toFloat();
2859 topDict.fontMatrix[3] = ops[3].toFloat();
2860 topDict.fontMatrix[4] = ops[4].toFloat();
2861 topDict.fontMatrix[5] = ops[5].toFloat();
2862 topDict.hasFontMatrix = gTrue;
2863 break;
2864 case 0x000d: topDict.uniqueID = ops[0].toInt(); break;
2865 case 0x0005: topDict.fontBBox[0] = ops[0].toFloat();
2866 topDict.fontBBox[1] = ops[1].toFloat();
2867 topDict.fontBBox[2] = ops[2].toFloat();
2868 topDict.fontBBox[3] = ops[3].toFloat();
2869 break;
2870 case 0x0c08: topDict.strokeWidth = ops[0].toFloat(); break;
2871 case 0x000f: topDict.charsetOffset = ops[0].toInt(); break;
2872 case 0x0010: topDict.encodingOffset = ops[0].toInt(); break;
2873 case 0x0011: topDict.charStringsOffset = ops[0].toInt(); break;
2874 case 0x0012: topDict.privateSize = ops[0].toInt();
2875 topDict.privateOffset = ops[1].toInt();
2876 break;
2877 case 0x0c1e: topDict.registrySID = ops[0].toInt();
2878 topDict.orderingSID = ops[1].toInt();
2879 topDict.supplement = ops[2].toInt();
2880 break;
2881 case 0x0c24: topDict.fdArrayOffset = ops[0].toInt(); break;
2882 case 0x0c25: topDict.fdSelectOffset = ops[0].toInt(); break;
2883 }
2884 nOps = 0;
2885 }
2886 }
2887 }
2888
2889 // Read a CID font dict (FD) - this pulls out the private dict
2890 // pointer, and reads the private dict. It also pulls the FontMatrix
2891 // (if any) out of the FD.
2892 void FoFiType1C::readFD(int offset, int length, Type1CPrivateDict *pDict) {
2893 int pos, pSize, pOffset;
2894 double fontMatrix[6];
2895 GBool hasFontMatrix;
2896
2897 hasFontMatrix = gFalse;
2898 fontMatrix[0] = fontMatrix[1] = fontMatrix[2] = 0; // make gcc happy
2899 fontMatrix[3] = fontMatrix[4] = fontMatrix[5] = 0;
2900 pSize = pOffset = 0;
2901 pos = offset;
2902 nOps = 0;
2903 while (pos < offset + length) {
2904 pos = getOp(pos, gFalse, &parsedOk);
2905 if (!parsedOk) {
2906 return;
2907 }
2908 if (ops[nOps - 1].kind == type1COpOperator) {
2909 if (ops[nOps - 1].op == 0x0012) {
2910 if (nOps < 3) {
2911 parsedOk = gFalse;
2912 return;
2913 }
2914 pSize = ops[0].toInt();
2915 pOffset = ops[1].toInt();
2916 break;
2917 } else if (ops[nOps - 1].op == 0x0c07) {
2918 fontMatrix[0] = ops[0].toFloat();
2919 fontMatrix[1] = ops[1].toFloat();
2920 fontMatrix[2] = ops[2].toFloat();
2921 fontMatrix[3] = ops[3].toFloat();
2922 fontMatrix[4] = ops[4].toFloat();
2923 fontMatrix[5] = ops[5].toFloat();
2924 hasFontMatrix = gTrue;
2925 }
2926 nOps = 0;
2927 }
2928 }
2929 readPrivateDict(pOffset, pSize, pDict);
2930 if (hasFontMatrix) {
2931 pDict->fontMatrix[0] = fontMatrix[0];
2932 pDict->fontMatrix[1] = fontMatrix[1];
2933 pDict->fontMatrix[2] = fontMatrix[2];
2934 pDict->fontMatrix[3] = fontMatrix[3];
2935 pDict->fontMatrix[4] = fontMatrix[4];
2936 pDict->fontMatrix[5] = fontMatrix[5];
2937 pDict->hasFontMatrix = gTrue;
2938 }
2939 }
2940
2941 void FoFiType1C::readPrivateDict(int offset, int length,
2942 Type1CPrivateDict *pDict) {
2943 int pos;
2944
2945 pDict->hasFontMatrix = gFalse;
2946 pDict->nBlueValues = 0;
2947 pDict->nOtherBlues = 0;
2948 pDict->nFamilyBlues = 0;
2949 pDict->nFamilyOtherBlues = 0;
2950 pDict->blueScale = 0.039625;
2951 pDict->blueShift = 7;
2952 pDict->blueFuzz = 1;
2953 pDict->hasStdHW = gFalse;
2954 pDict->hasStdVW = gFalse;
2955 pDict->nStemSnapH = 0;
2956 pDict->nStemSnapV = 0;
2957 pDict->hasForceBold = gFalse;
2958 pDict->forceBoldThreshold = 0;
2959 pDict->languageGroup = 0;
2960 pDict->expansionFactor = 0.06;
2961 pDict->initialRandomSeed = 0;
2962 pDict->subrsOffset = 0;
2963 pDict->defaultWidthX = 0;
2964 pDict->defaultWidthXInt = gTrue;
2965 pDict->nominalWidthX = 0;
2966 pDict->nominalWidthXInt = gTrue;
2967
2968 // no dictionary
2969 if (offset == 0 || length == 0) {
2970 return;
2971 }
2972
2973 pos = offset;
2974 nOps = 0;
2975 while (pos < offset + length) {
2976 pos = getOp(pos, gFalse, &parsedOk);
2977 if (!parsedOk) {
2978 break;
2979 }
2980 if (ops[nOps - 1].kind == type1COpOperator) {
2981 --nOps; // drop the operator
2982 switch (ops[nOps].op) {
2983 case 0x0006:
2984 pDict->nBlueValues = getDeltaIntArray(pDict->blueValues,
2985 type1CMaxBlueValues);
2986 break;
2987 case 0x0007:
2988 pDict->nOtherBlues = getDeltaIntArray(pDict->otherBlues,
2989 type1CMaxOtherBlues);
2990 break;
2991 case 0x0008:
2992 pDict->nFamilyBlues = getDeltaIntArray(pDict->familyBlues,
2993 type1CMaxBlueValues);
2994 break;
2995 case 0x0009:
2996 pDict->nFamilyOtherBlues = getDeltaIntArray(pDict->familyOtherBlues,
2997 type1CMaxOtherBlues);
2998 break;
2999 case 0x0c09:
3000 pDict->blueScale = ops[0].toFloat();
3001 break;
3002 case 0x0c0a:
3003 pDict->blueShift = ops[0].toInt();
3004 break;
3005 case 0x0c0b:
3006 pDict->blueFuzz = ops[0].toInt();
3007 break;
3008 case 0x000a:
3009 pDict->stdHW = ops[0].toFloat();
3010 pDict->hasStdHW = gTrue;
3011 break;
3012 case 0x000b:
3013 pDict->stdVW = ops[0].toFloat();
3014 pDict->hasStdVW = gTrue;
3015 break;
3016 case 0x0c0c:
3017 pDict->nStemSnapH = getDeltaFPArray(pDict->stemSnapH,
3018 type1CMaxStemSnap);
3019 break;
3020 case 0x0c0d:
3021 pDict->nStemSnapV = getDeltaFPArray(pDict->stemSnapV,
3022 type1CMaxStemSnap);
3023 break;
3024 case 0x0c0e:
3025 pDict->forceBold = !ops[0].isZero();
3026 pDict->hasForceBold = gTrue;
3027 break;
3028 case 0x0c0f:
3029 pDict->forceBoldThreshold = ops[0].toFloat();
3030 break;
3031 case 0x0c11:
3032 pDict->languageGroup = ops[0].toInt();
3033 break;
3034 case 0x0c12:
3035 pDict->expansionFactor = ops[0].toFloat();
3036 break;
3037 case 0x0c13:
3038 pDict->initialRandomSeed = ops[0].toInt();
3039 break;
3040 case 0x0013:
3041 pDict->subrsOffset = offset + ops[0].toInt();
3042 break;
3043 case 0x0014:
3044 pDict->defaultWidthX = ops[0].toFloat();
3045 pDict->defaultWidthXInt = ops[0].kind == type1COpInteger;
3046 break;
3047 case 0x0015:
3048 pDict->nominalWidthX = ops[0].toFloat();
3049 pDict->nominalWidthXInt = ops[0].kind == type1COpInteger;
3050 break;
3051 }
3052 nOps = 0;
3053 }
3054 }
3055 }
3056
3057 void FoFiType1C::readFDSelect() {
3058 int fdSelectFmt, pos, nRanges, gid0, gid1, fd, i, j;
3059
3060 fdSelect = (Guchar *)gmalloc(nGlyphs);
3061 for (i = 0; i < nGlyphs; ++i) {
3062 fdSelect[i] = 0;
3063 }
3064 if (topDict.fdSelectOffset != 0) {
3065 pos = topDict.fdSelectOffset;
3066 fdSelectFmt = getU8(pos++, &parsedOk);
3067 if (!parsedOk) {
3068 return;
3069 }
3070 if (fdSelectFmt == 0) {
3071 if (!checkRegion(pos, nGlyphs)) {
3072 parsedOk = gFalse;
3073 return;
3074 }
3075 for (gid0 = 0; gid0 < nGlyphs; ++gid0) {
3076 if (file[pos + gid0] >= nFDs) {
3077 //~ error(-1, "Bad FDSelect table in CID font");
3078 parsedOk = gFalse;
3079 return;
3080 }
3081 fdSelect[gid0] = file[pos + gid0];
3082 }
3083 } else if (fdSelectFmt == 3) {
3084 nRanges = getU16BE(pos, &parsedOk);
3085 pos += 2;
3086 gid0 = getU16BE(pos, &parsedOk);
3087 pos += 2;
3088 for (i = 1; i <= nRanges; ++i) {
3089 fd = getU8(pos++, &parsedOk);
3090 gid1 = getU16BE(pos, &parsedOk);
3091 if (!parsedOk) {
3092 return;
3093 }
3094 pos += 2;
3095 if (gid0 > gid1 || gid1 > nGlyphs || fd >= nFDs) {
3096 //~ error(-1, "Bad FDSelect table in CID font");
3097 parsedOk = gFalse;
3098 return;
3099 }
3100 for (j = gid0; j < gid1; ++j) {
3101 fdSelect[j] = (Guchar)fd;
3102 }
3103 gid0 = gid1;
3104 }
3105 } else {
3106 //~ error(-1, "Unknown FDSelect table format in CID font");
3107 }
3108 }
3109 }
3110
3111 void FoFiType1C::buildEncoding() {
3112 char buf[256];
3113 int nCodes, nRanges, encFormat;
3114 int pos, c, sid, nLeft, nSups, i, j;
3115
3116 if (topDict.encodingOffset == 0) {
3117 encoding = (char **)fofiType1StandardEncoding;
3118
3119 } else if (topDict.encodingOffset == 1) {
3120 encoding = (char **)fofiType1ExpertEncoding;
3121
3122 } else {
3123 encoding = (char **)gmallocn(256, sizeof(char *));
3124 for (i = 0; i < 256; ++i) {
3125 encoding[i] = NULL;
3126 }
3127 pos = topDict.encodingOffset;
3128 encFormat = getU8(pos++, &parsedOk);
3129 if (!parsedOk) {
3130 return;
3131 }
3132 if ((encFormat & 0x7f) == 0) {
3133 nCodes = 1 + getU8(pos++, &parsedOk);
3134 if (!parsedOk) {
3135 return;
3136 }
3137 if (nCodes > nGlyphs) {
3138 nCodes = nGlyphs;
3139 }
3140 for (i = 1; i < nCodes; ++i) {
3141 c = getU8(pos++, &parsedOk);
3142 if (!parsedOk) {
3143 return;
3144 }
3145 if (encoding[c]) {
3146 gfree(encoding[c]);
3147 }
3148 encoding[c] = copyString(getString(charset[i], buf, &parsedOk));
3149 }
3150 } else if ((encFormat & 0x7f) == 1) {
3151 nRanges = getU8(pos++, &parsedOk);
3152 if (!parsedOk) {
3153 return;
3154 }
3155 nCodes = 1;
3156 for (i = 0; i < nRanges; ++i) {
3157 c = getU8(pos++, &parsedOk);
3158 nLeft = getU8(pos++, &parsedOk);
3159 if (!parsedOk) {
3160 return;
3161 }
3162 for (j = 0; j <= nLeft && nCodes < nGlyphs; ++j) {
3163 if (c < 256) {
3164 if (encoding[c]) {
3165 gfree(encoding[c]);
3166 }
3167 encoding[c] = copyString(getString(charset[nCodes], buf,
3168 &parsedOk));
3169 }
3170 ++nCodes;
3171 ++c;
3172 }
3173 }
3174 }
3175 if (encFormat & 0x80) {
3176 nSups = getU8(pos++, &parsedOk);
3177 if (!parsedOk) {
3178 return;
3179 }
3180 for (i = 0; i < nSups; ++i) {
3181 c = getU8(pos++, &parsedOk);;
3182 if (!parsedOk) {
3183 return;;
3184 }
3185 sid = getU16BE(pos, &parsedOk);
3186 pos += 2;
3187 if (!parsedOk) {
3188 return;
3189 }
3190 if (encoding[c]) {
3191 gfree(encoding[c]);
3192 }
3193 encoding[c] = copyString(getString(sid, buf, &parsedOk));
3194 }
3195 }
3196 }
3197 }
3198
3199 GBool FoFiType1C::readCharset() {
3200 int charsetFormat, c, pos;
3201 int nLeft, i, j;
3202
3203 if (topDict.charsetOffset == 0) {
3204 charset = fofiType1CISOAdobeCharset;
3205 if (nGlyphs > 229) {
3206 nGlyphs = 229;
3207 }
3208 } else if (topDict.charsetOffset == 1) {
3209 charset = fofiType1CExpertCharset;
3210 if (nGlyphs > 166) {
3211 nGlyphs = 166;
3212 }
3213 } else if (topDict.charsetOffset == 2) {
3214 charset = fofiType1CExpertSubsetCharset;
3215 if (nGlyphs > 87) {
3216 nGlyphs = 87;
3217 }
3218 } else {
3219 charset = (Gushort *)gmallocn(nGlyphs, sizeof(Gushort));
3220 for (i = 0; i < nGlyphs; ++i) {
3221 charset[i] = 0;
3222 }
3223 pos = topDict.charsetOffset;
3224 charsetFormat = getU8(pos++, &parsedOk);
3225 if (charsetFormat == 0) {
3226 for (i = 1; i < nGlyphs; ++i) {
3227 charset[i] = (Gushort)getU16BE(pos, &parsedOk);
3228 pos += 2;
3229 if (!parsedOk) {
3230 break;
3231 }
3232 }
3233 } else if (charsetFormat == 1) {
3234 i = 1;
3235 while (i < nGlyphs) {
3236 c = getU16BE(pos, &parsedOk);
3237 pos += 2;
3238 nLeft = getU8(pos++, &parsedOk);
3239 if (!parsedOk) {
3240 break;
3241 }
3242 for (j = 0; j <= nLeft && i < nGlyphs; ++j) {
3243 charset[i++] = (Gushort)c++;
3244 }
3245 }
3246 } else if (charsetFormat == 2) {
3247 i = 1;
3248 while (i < nGlyphs) {
3249 c = getU16BE(pos, &parsedOk);
3250 pos += 2;
3251 nLeft = getU16BE(pos, &parsedOk);
3252 pos += 2;
3253 if (!parsedOk) {
3254 break;
3255 }
3256 for (j = 0; j <= nLeft && i < nGlyphs; ++j) {
3257 charset[i++] = (Gushort)c++;
3258 }
3259 }
3260 }
3261 if (!parsedOk) {
3262 gfree(charset);
3263 charset = NULL;
3264 return gFalse;
3265 }
3266 }
3267 return gTrue;
3268 }
3269
3270 int FoFiType1C::getOp(int pos, GBool charstring, GBool *ok) {
3271 static char nybChars[16] = "0123456789.ee -";
3272 Type1COp op;
3273 char buf[65];
3274 int b0, b1, nyb0, nyb1, x, i;
3275
3276 b0 = getU8(pos++, ok);
3277
3278 if (b0 == 28) {
3279 x = getU8(pos++, ok);
3280 x = (x << 8) | getU8(pos++, ok);
3281 if (x & 0x8000) {
3282 x |= ~0xffff;
3283 }
3284 op.kind = type1COpInteger;
3285 op.intgr = x;
3286
3287 } else if (!charstring && b0 == 29) {
3288 x = getU8(pos++, ok);
3289 x = (x << 8) | getU8(pos++, ok);
3290 x = (x << 8) | getU8(pos++, ok);
3291 x = (x << 8) | getU8(pos++, ok);
3292 if (x & 0x80000000) {
3293 x |= ~0xffffffff;
3294 }
3295 op.kind = type1COpInteger;
3296 op.intgr = x;
3297
3298 } else if (!charstring && b0 == 30) {
3299 i = 0;
3300 do {
3301 b1 = getU8(pos++, ok);
3302 nyb0 = b1 >> 4;
3303 nyb1 = b1 & 0x0f;
3304 if (nyb0 == 0xf) {
3305 break;
3306 }
3307 buf[i++] = nybChars[nyb0];
3308 if (i == 64) {
3309 break;
3310 }
3311 if (nyb0 == 0xc) {
3312 buf[i++] = '-';
3313 }
3314 if (i == 64) {
3315 break;
3316 }
3317 if (nyb1 == 0xf) {
3318 break;
3319 }
3320 buf[i++] = nybChars[nyb1];
3321 if (i == 64) {
3322 break;
3323 }
3324 if (nyb1 == 0xc) {
3325 buf[i++] = '-';
3326 }
3327 } while (i < 64);
3328 buf[i] = '\0';
3329 op.kind = type1COpFloat;
3330 op.flt = atof(buf);
3331
3332 } else if (b0 >= 32 && b0 <= 246) {
3333 op.kind = type1COpInteger;
3334 op.intgr = b0 - 139;
3335
3336 } else if (b0 >= 247 && b0 <= 250) {
3337 op.kind = type1COpInteger;
3338 op.intgr = ((b0 - 247) << 8) + getU8(pos++, ok) + 108;
3339
3340 } else if (b0 >= 251 && b0 <= 254) {
3341 op.kind = type1COpInteger;
3342 op.intgr = -((b0 - 251) << 8) - getU8(pos++, ok) - 108;
3343
3344 } else if (charstring && b0 == 255) {
3345 x = getU8(pos++, ok);
3346 x = (x << 8) | getU8(pos++, ok);
3347 x = (x << 8) | getU8(pos++, ok);
3348 x = (x << 8) | getU8(pos++, ok);
3349 if (x & 0x80000000) {
3350 x |= ~0xffffffff;
3351 }
3352 op.kind = type1COpFloat;
3353 op.flt = (double)x / 65536.0;
3354
3355 } else if (b0 == 12) {
3356 op.kind = type1COpOperator;
3357 op.op = 0x0c00 + getU8(pos++, ok);
3358
3359 } else {
3360 op.kind = type1COpOperator;
3361 op.op = b0;
3362 }
3363
3364 if (nOps < 49) {
3365 ops[nOps++] = op;
3366 }
3367
3368 return pos;
3369 }
3370
3371 // Convert the delta-encoded ops array to an array of ints.
3372 int FoFiType1C::getDeltaIntArray(int *arr, int maxLen) {
3373 int x;
3374 int n, i;
3375
3376 if ((n = nOps) > maxLen) {
3377 n = maxLen;
3378 }
3379 x = 0;
3380 for (i = 0; i < n; ++i) {
3381 x += ops[i].toInt();
3382 arr[i] = x;
3383 }
3384 return n;
3385 }
3386
3387 // Convert the delta-encoded ops array to an array of doubles.
3388 int FoFiType1C::getDeltaFPArray(double *arr, int maxLen) {
3389 double x;
3390 int n, i;
3391
3392 if ((n = nOps) > maxLen) {
3393 n = maxLen;
3394 }
3395 x = 0;
3396 for (i = 0; i < n; ++i) {
3397 x += ops[i].toFloat();
3398 arr[i] = x;
3399 }
3400 return n;
3401 }
3402
3403 void FoFiType1C::getIndex(int pos, Type1CIndex *idx, GBool *ok) {
3404 idx->pos = pos;
3405 idx->len = getU16BE(pos, ok);
3406 if (idx->len == 0) {
3407 // empty indexes are legal and contain just the length field
3408 idx->offSize = 0;
3409 idx->startPos = idx->endPos = pos + 2;
3410 } else {
3411 idx->offSize = getU8(pos + 2, ok);
3412 if (idx->offSize < 1 || idx->offSize > 4) {
3413 *ok = gFalse;
3414 }
3415 idx->startPos = pos + 3 + (idx->len + 1) * idx->offSize - 1;
3416 if (idx->startPos < 0 || idx->startPos >= len) {
3417 *ok = gFalse;
3418 }
3419 idx->endPos = idx->startPos + getUVarBE(pos + 3 + idx->len * idx->offSize,
3420 idx->offSize, ok);
3421 if (idx->endPos < idx->startPos || idx->endPos > len) {
3422 *ok = gFalse;
3423 }
3424 }
3425 }
3426
3427 void FoFiType1C::getIndexVal(Type1CIndex *idx, int i,
3428 Type1CIndexVal *val, GBool *ok) {
3429 int pos0, pos1;
3430
3431 if (i < 0 || i >= idx->len) {
3432 val->pos = 0;
3433 val->len = 0;
3434 *ok = gFalse;
3435 return;
3436 }
3437 pos0 = idx->startPos + getUVarBE(idx->pos + 3 + i * idx->offSize,
3438 idx->offSize, ok);
3439 pos1 = idx->startPos + getUVarBE(idx->pos + 3 + (i + 1) * idx->offSize,
3440 idx->offSize, ok);
3441 if (pos0 < idx->startPos || pos0 > idx->endPos ||
3442 pos1 <= idx->startPos || pos1 > idx->endPos ||
3443 pos1 < pos0) {
3444 *ok = gFalse;
3445 }
3446 val->pos = pos0;
3447 val->len = pos1 - pos0;
3448 }
3449
3450 char *FoFiType1C::getString(int sid, char *buf, GBool *ok) {
3451 Type1CIndexVal val;
3452 int n;
3453
3454 if (sid < 0) {
3455 buf[0] = '\0';
3456 } else if (sid < 391) {
3457 strcpy(buf, fofiType1CStdStrings[sid]);
3458 } else {
3459 sid -= 391;
3460 getIndexVal(&stringIdx, sid, &val, ok);
3461 if (*ok) {
3462 if ((n = val.len) > 255) {
3463 n = 255;
3464 }
3465 strncpy(buf, (char *)&file[val.pos], n);
3466 buf[n] = '\0';
3467 } else {
3468 buf[0] = '\0';
3469 }
3470 }
3471 return buf;
3472 }