1
//========================================================================
5
// Copyright 1999-2003 Glyph & Cog, LLC
7
//========================================================================
9
//========================================================================
11
// Modified under the Poppler project - http://poppler.freedesktop.org
13
// All changes made under the Poppler project to this file are licensed
14
// under GPL version 2 or later
16
// Copyright (C) 2009, 2010 Albert Astals Cid <aacid@kde.org>
18
// To see a description of the changes please see the Changelog file that
19
// came with your tarball or type make ChangeLog if you are building from git
21
//========================================================================
25
#ifdef USE_GCC_PRAGMAS
26
#pragma implementation
33
#include "goo/gstrtod.h"
34
#include "goo/GooString.h"
35
#include "FoFiEncodings.h"
36
#include "FoFiType1C.h"
38
//------------------------------------------------------------------------
40
static char hexChars[17] = "0123456789ABCDEF";
42
//------------------------------------------------------------------------
44
//------------------------------------------------------------------------
46
FoFiType1C *FoFiType1C::make(char *fileA, int lenA) {
49
ff = new FoFiType1C(fileA, lenA, gFalse);
57
FoFiType1C *FoFiType1C::load(char *fileName) {
62
if (!(fileA = FoFiBase::readFile(fileName, &lenA))) {
65
ff = new FoFiType1C(fileA, lenA, gTrue);
73
FoFiType1C::FoFiType1C(char *fileA, int lenA, GBool freeFileDataA):
74
FoFiBase(fileA, lenA, freeFileDataA)
83
FoFiType1C::~FoFiType1C() {
90
encoding != fofiType1StandardEncoding &&
91
encoding != fofiType1ExpertEncoding) {
92
for (i = 0; i < 256; ++i) {
104
charset != fofiType1CISOAdobeCharset &&
105
charset != fofiType1CExpertCharset &&
106
charset != fofiType1CExpertSubsetCharset) {
111
char *FoFiType1C::getName() {
112
return name ? name->getCString() : (char *)NULL;
115
char **FoFiType1C::getEncoding() {
119
Gushort *FoFiType1C::getCIDToGIDMap(int *nCIDs) {
123
// a CID font's top dict has ROS as the first operator
124
if (topDict.firstOp != 0x0c1e) {
129
// in a CID font, the charset data is the GID-to-CID mapping, so all
130
// we have to do is reverse it
132
for (i = 0; i < nGlyphs; ++i) {
133
if (charset[i] > n) {
138
map = (Gushort *)gmallocn(n, sizeof(Gushort));
139
memset(map, 0, n * sizeof(Gushort));
140
for (i = 0; i < nGlyphs; ++i) {
147
void FoFiType1C::convertToType1(char *psName, char **newEncoding, GBool ascii,
148
FoFiOutputFunc outputFunc,
149
void *outputStream) {
161
psNameLen = strlen(psName);
163
psName = name->getCString();
164
psNameLen = name->getLength();
167
// write header and font dictionary, up to encoding
169
(*outputFunc)(outputStream, "%!FontType1-1.0: ", 17);
170
(*outputFunc)(outputStream, psName, psNameLen);
171
if (topDict.versionSID != 0) {
172
getString(topDict.versionSID, buf2, &ok);
173
(*outputFunc)(outputStream, buf2, strlen(buf2));
175
(*outputFunc)(outputStream, "\n", 1);
176
// the dictionary needs room for 12 entries: the following 9, plus
177
// Private and CharStrings (in the eexec section) and FID (which is
178
// added by definefont)
179
(*outputFunc)(outputStream, "12 dict begin\n", 14);
180
(*outputFunc)(outputStream, "/FontInfo 10 dict dup begin\n", 28);
181
if (topDict.versionSID != 0) {
182
(*outputFunc)(outputStream, "/version (", 10);
183
(*outputFunc)(outputStream, buf2, strlen(buf2));
184
(*outputFunc)(outputStream, ") readonly def\n", 15);
186
if (topDict.noticeSID != 0) {
187
getString(topDict.noticeSID, buf2, &ok);
188
(*outputFunc)(outputStream, "/Notice (", 9);
189
(*outputFunc)(outputStream, buf2, strlen(buf2));
190
(*outputFunc)(outputStream, ") readonly def\n", 15);
192
if (topDict.copyrightSID != 0) {
193
getString(topDict.copyrightSID, buf2, &ok);
194
(*outputFunc)(outputStream, "/Copyright (", 12);
195
(*outputFunc)(outputStream, buf2, strlen(buf2));
196
(*outputFunc)(outputStream, ") readonly def\n", 15);
198
if (topDict.fullNameSID != 0) {
199
getString(topDict.fullNameSID, buf2, &ok);
200
(*outputFunc)(outputStream, "/FullName (", 11);
201
(*outputFunc)(outputStream, buf2, strlen(buf2));
202
(*outputFunc)(outputStream, ") readonly def\n", 15);
204
if (topDict.familyNameSID != 0) {
205
getString(topDict.familyNameSID, buf2, &ok);
206
(*outputFunc)(outputStream, "/FamilyName (", 13);
207
(*outputFunc)(outputStream, buf2, strlen(buf2));
208
(*outputFunc)(outputStream, ") readonly def\n", 15);
210
if (topDict.weightSID != 0) {
211
getString(topDict.weightSID, buf2, &ok);
212
(*outputFunc)(outputStream, "/Weight (", 9);
213
(*outputFunc)(outputStream, buf2, strlen(buf2));
214
(*outputFunc)(outputStream, ") readonly def\n", 15);
216
if (topDict.isFixedPitch) {
217
(*outputFunc)(outputStream, "/isFixedPitch true def\n", 23);
219
(*outputFunc)(outputStream, "/isFixedPitch false def\n", 24);
221
buf = GooString::format("/ItalicAngle {0:.4g} def\n", topDict.italicAngle);
222
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
224
buf = GooString::format("/UnderlinePosition {0:.4g} def\n",
225
topDict.underlinePosition);
226
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
228
buf = GooString::format("/UnderlineThickness {0:.4g} def\n",
229
topDict.underlineThickness);
230
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
232
(*outputFunc)(outputStream, "end readonly def\n", 17);
233
(*outputFunc)(outputStream, "/FontName /", 11);
234
(*outputFunc)(outputStream, psName, psNameLen);
235
(*outputFunc)(outputStream, " def\n", 5);
236
buf = GooString::format("/PaintType {0:d} def\n", topDict.paintType);
237
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
239
(*outputFunc)(outputStream, "/FontType 1 def\n", 16);
240
buf = GooString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] readonly def\n",
241
topDict.fontMatrix[0], topDict.fontMatrix[1],
242
topDict.fontMatrix[2], topDict.fontMatrix[3],
243
topDict.fontMatrix[4], topDict.fontMatrix[5]);
244
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
246
buf = GooString::format("/FontBBox [{0:.4g} {1:.4g} {2:.4g} {3:.4g}] readonly def\n",
247
topDict.fontBBox[0], topDict.fontBBox[1],
248
topDict.fontBBox[2], topDict.fontBBox[3]);
249
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
251
buf = GooString::format("/StrokeWidth {0:.4g} def\n", topDict.strokeWidth);
252
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
254
if (topDict.uniqueID != 0) {
255
buf = GooString::format("/UniqueID {0:d} def\n", topDict.uniqueID);
256
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
260
// write the encoding
261
(*outputFunc)(outputStream, "/Encoding ", 10);
262
if (!newEncoding && encoding == fofiType1StandardEncoding) {
263
(*outputFunc)(outputStream, "StandardEncoding def\n", 21);
265
(*outputFunc)(outputStream, "256 array\n", 10);
266
(*outputFunc)(outputStream,
267
"0 1 255 {1 index exch /.notdef put} for\n", 40);
268
enc = newEncoding ? newEncoding : encoding;
269
for (i = 0; i < 256; ++i) {
271
buf = GooString::format("dup {0:d} /{1:s} put\n", i, enc[i]);
272
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
276
(*outputFunc)(outputStream, "readonly def\n", 13);
278
(*outputFunc)(outputStream, "currentdict end\n", 16);
280
// start the binary section
281
(*outputFunc)(outputStream, "currentfile eexec\n", 18);
282
eb.outputFunc = outputFunc;
283
eb.outputStream = outputStream;
288
// write the private dictionary
289
eexecWrite(&eb, "\x83\xca\x73\xd5");
290
eexecWrite(&eb, "dup /Private 32 dict dup begin\n");
291
eexecWrite(&eb, "/RD {string currentfile exch readstring pop}"
292
" executeonly def\n");
293
eexecWrite(&eb, "/ND {noaccess def} executeonly def\n");
294
eexecWrite(&eb, "/NP {noaccess put} executeonly def\n");
295
eexecWrite(&eb, "/MinFeature {16 16} def\n");
296
eexecWrite(&eb, "/password 5839 def\n");
297
if (privateDicts[0].nBlueValues) {
298
eexecWrite(&eb, "/BlueValues [");
299
for (i = 0; i < privateDicts[0].nBlueValues; ++i) {
300
buf = GooString::format("{0:s}{1:d}",
301
i > 0 ? " " : "", privateDicts[0].blueValues[i]);
302
eexecWrite(&eb, buf->getCString());
305
eexecWrite(&eb, "] def\n");
307
if (privateDicts[0].nOtherBlues) {
308
eexecWrite(&eb, "/OtherBlues [");
309
for (i = 0; i < privateDicts[0].nOtherBlues; ++i) {
310
buf = GooString::format("{0:s}{1:d}",
311
i > 0 ? " " : "", privateDicts[0].otherBlues[i]);
312
eexecWrite(&eb, buf->getCString());
315
eexecWrite(&eb, "] def\n");
317
if (privateDicts[0].nFamilyBlues) {
318
eexecWrite(&eb, "/FamilyBlues [");
319
for (i = 0; i < privateDicts[0].nFamilyBlues; ++i) {
320
buf = GooString::format("{0:s}{1:d}",
321
i > 0 ? " " : "", privateDicts[0].familyBlues[i]);
322
eexecWrite(&eb, buf->getCString());
325
eexecWrite(&eb, "] def\n");
327
if (privateDicts[0].nFamilyOtherBlues) {
328
eexecWrite(&eb, "/FamilyOtherBlues [");
329
for (i = 0; i < privateDicts[0].nFamilyOtherBlues; ++i) {
330
buf = GooString::format("{0:s}{1:d}", i > 0 ? " " : "",
331
privateDicts[0].familyOtherBlues[i]);
332
eexecWrite(&eb, buf->getCString());
335
eexecWrite(&eb, "] def\n");
337
if (privateDicts[0].blueScale != 0.039625) {
338
buf = GooString::format("/BlueScale {0:.4g} def\n",
339
privateDicts[0].blueScale);
340
eexecWrite(&eb, buf->getCString());
343
if (privateDicts[0].blueShift != 7) {
344
buf = GooString::format("/BlueShift {0:d} def\n", privateDicts[0].blueShift);
345
eexecWrite(&eb, buf->getCString());
348
if (privateDicts[0].blueFuzz != 1) {
349
buf = GooString::format("/BlueFuzz {0:d} def\n", privateDicts[0].blueFuzz);
350
eexecWrite(&eb, buf->getCString());
353
if (privateDicts[0].hasStdHW) {
354
buf = GooString::format("/StdHW [{0:.4g}] def\n", privateDicts[0].stdHW);
355
eexecWrite(&eb, buf->getCString());
358
if (privateDicts[0].hasStdVW) {
359
buf = GooString::format("/StdVW [{0:.4g}] def\n", privateDicts[0].stdVW);
360
eexecWrite(&eb, buf->getCString());
363
if (privateDicts[0].nStemSnapH) {
364
eexecWrite(&eb, "/StemSnapH [");
365
for (i = 0; i < privateDicts[0].nStemSnapH; ++i) {
366
buf = GooString::format("{0:s}{1:.4g}",
367
i > 0 ? " " : "", privateDicts[0].stemSnapH[i]);
368
eexecWrite(&eb, buf->getCString());
371
eexecWrite(&eb, "] def\n");
373
if (privateDicts[0].nStemSnapV) {
374
eexecWrite(&eb, "/StemSnapV [");
375
for (i = 0; i < privateDicts[0].nStemSnapV; ++i) {
376
buf = GooString::format("{0:s}{1:.4g}",
377
i > 0 ? " " : "", privateDicts[0].stemSnapV[i]);
378
eexecWrite(&eb, buf->getCString());
381
eexecWrite(&eb, "] def\n");
383
if (privateDicts[0].hasForceBold) {
384
buf = GooString::format("/ForceBold {0:s} def\n",
385
privateDicts[0].forceBold ? "true" : "false");
386
eexecWrite(&eb, buf->getCString());
389
if (privateDicts[0].forceBoldThreshold != 0) {
390
buf = GooString::format("/ForceBoldThreshold {0:.4g} def\n",
391
privateDicts[0].forceBoldThreshold);
392
eexecWrite(&eb, buf->getCString());
395
if (privateDicts[0].languageGroup != 0) {
396
buf = GooString::format("/LanguageGroup {0:d} def\n",
397
privateDicts[0].languageGroup);
398
eexecWrite(&eb, buf->getCString());
401
if (privateDicts[0].expansionFactor != 0.06) {
402
buf = GooString::format("/ExpansionFactor {0:.4g} def\n",
403
privateDicts[0].expansionFactor);
404
eexecWrite(&eb, buf->getCString());
408
// set up subroutines
410
getIndex(privateDicts[0].subrsOffset, &subrIdx, &ok);
415
// write the CharStrings
416
buf = GooString::format("2 index /CharStrings {0:d} dict dup begin\n",
418
eexecWrite(&eb, buf->getCString());
420
for (i = 0; i < nGlyphs; ++i) {
422
getIndexVal(&charStringsIdx, i, &val, &ok);
424
getString(charset[i], buf2, &ok);
426
eexecCvtGlyph(&eb, buf2, val.pos, val.len, &subrIdx, &privateDicts[0]);
430
eexecWrite(&eb, "end\n");
431
eexecWrite(&eb, "end\n");
432
eexecWrite(&eb, "readonly put\n");
433
eexecWrite(&eb, "noaccess put\n");
434
eexecWrite(&eb, "dup /FontName get exch definefont pop\n");
435
eexecWrite(&eb, "mark currentfile closefile\n");
438
if (ascii && eb.line > 0) {
439
(*outputFunc)(outputStream, "\n", 1);
441
for (i = 0; i < 8; ++i) {
442
(*outputFunc)(outputStream, "0000000000000000000000000000000000000000000000000000000000000000\n", 65);
444
(*outputFunc)(outputStream, "cleartomark\n", 12);
447
void FoFiType1C::convertToCIDType0(char *psName,
448
FoFiOutputFunc outputFunc,
449
void *outputStream) {
451
GooString *charStrings;
452
int *charStringOffsets;
459
int gid, offset, n, i, j, k;
461
// compute the CID count and build the CID-to-GID mapping
463
for (i = 0; i < nGlyphs; ++i) {
464
if (charset[i] >= nCIDs) {
465
nCIDs = charset[i] + 1;
468
cidMap = (int *)gmallocn(nCIDs, sizeof(int));
469
for (i = 0; i < nCIDs; ++i) {
472
for (i = 0; i < nGlyphs; ++i) {
473
cidMap[charset[i]] = i;
476
// build the charstrings
477
charStrings = new GooString();
478
charStringOffsets = (int *)gmallocn(nCIDs + 1, sizeof(int));
479
for (i = 0; i < nCIDs; ++i) {
480
charStringOffsets[i] = charStrings->getLength();
481
if ((gid = cidMap[i]) >= 0) {
483
getIndexVal(&charStringsIdx, gid, &val, &ok);
485
getIndex(privateDicts[fdSelect[gid]].subrsOffset, &subrIdx, &ok);
489
cvtGlyph(val.pos, val.len, charStrings,
490
&subrIdx, &privateDicts[fdSelect[gid]], gTrue);
494
charStringOffsets[nCIDs] = charStrings->getLength();
496
// compute gdBytes = number of bytes needed for charstring offsets
497
// (offset size needs to account for the charstring offset table,
498
// with a worst case of five bytes per entry, plus the charstrings
500
i = (nCIDs + 1) * 5 + charStrings->getLength();
503
} else if (i < 0x10000) {
505
} else if (i < 0x1000000) {
511
// begin the font dictionary
512
(*outputFunc)(outputStream, "/CIDInit /ProcSet findresource begin\n", 37);
513
(*outputFunc)(outputStream, "20 dict begin\n", 14);
514
(*outputFunc)(outputStream, "/CIDFontName /", 14);
515
(*outputFunc)(outputStream, psName, strlen(psName));
516
(*outputFunc)(outputStream, " def\n", 5);
517
(*outputFunc)(outputStream, "/CIDFontType 0 def\n", 19);
518
(*outputFunc)(outputStream, "/CIDSystemInfo 3 dict dup begin\n", 32);
519
if (topDict.registrySID > 0 && topDict.orderingSID > 0) {
521
getString(topDict.registrySID, buf2, &ok);
523
(*outputFunc)(outputStream, " /Registry (", 13);
524
(*outputFunc)(outputStream, buf2, strlen(buf2));
525
(*outputFunc)(outputStream, ") def\n", 6);
528
getString(topDict.orderingSID, buf2, &ok);
530
(*outputFunc)(outputStream, " /Ordering (", 13);
531
(*outputFunc)(outputStream, buf2, strlen(buf2));
532
(*outputFunc)(outputStream, ") def\n", 6);
535
(*outputFunc)(outputStream, " /Registry (Adobe) def\n", 24);
536
(*outputFunc)(outputStream, " /Ordering (Identity) def\n", 27);
538
buf = GooString::format(" /Supplement {0:d} def\n", topDict.supplement);
539
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
541
(*outputFunc)(outputStream, "end def\n", 8);
542
if (topDict.hasFontMatrix) {
543
buf = GooString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
544
topDict.fontMatrix[0], topDict.fontMatrix[1],
545
topDict.fontMatrix[2], topDict.fontMatrix[3],
546
topDict.fontMatrix[4], topDict.fontMatrix[5]);
547
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
549
} else if (privateDicts[0].hasFontMatrix) {
550
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
552
(*outputFunc)(outputStream,
553
"/FontMatrix [0.001 0 0 0.001 0 0] def\n", 38);
555
buf = GooString::format("/FontBBox [{0:.4g} {1:.4g} {2:.4g} {3:.4g}] def\n",
556
topDict.fontBBox[0], topDict.fontBBox[1],
557
topDict.fontBBox[2], topDict.fontBBox[3]);
558
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
560
(*outputFunc)(outputStream, "/FontInfo 1 dict dup begin\n", 27);
561
(*outputFunc)(outputStream, " /FSType 8 def\n", 16);
562
(*outputFunc)(outputStream, "end def\n", 8);
564
// CIDFont-specific entries
565
buf = GooString::format("/CIDCount {0:d} def\n", nCIDs);
566
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
568
(*outputFunc)(outputStream, "/FDBytes 1 def\n", 15);
569
buf = GooString::format("/GDBytes {0:d} def\n", gdBytes);
570
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
572
(*outputFunc)(outputStream, "/CIDMapOffset 0 def\n", 20);
573
if (topDict.paintType != 0) {
574
buf = GooString::format("/PaintType {0:d} def\n", topDict.paintType);
575
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
577
buf = GooString::format("/StrokeWidth {0:.4g} def\n", topDict.strokeWidth);
578
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
583
buf = GooString::format("/FDArray {0:d} array\n", nFDs);
584
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
586
for (i = 0; i < nFDs; ++i) {
587
buf = GooString::format("dup {0:d} 10 dict begin\n", i);
588
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
590
(*outputFunc)(outputStream, "/FontType 1 def\n", 16);
591
if (privateDicts[i].hasFontMatrix) {
592
buf = GooString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
593
privateDicts[i].fontMatrix[0],
594
privateDicts[i].fontMatrix[1],
595
privateDicts[i].fontMatrix[2],
596
privateDicts[i].fontMatrix[3],
597
privateDicts[i].fontMatrix[4],
598
privateDicts[i].fontMatrix[5]);
599
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
602
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
604
buf = GooString::format("/PaintType {0:d} def\n", topDict.paintType);
605
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
607
(*outputFunc)(outputStream, "/Private 32 dict begin\n", 23);
608
if (privateDicts[i].nBlueValues) {
609
(*outputFunc)(outputStream, "/BlueValues [", 13);
610
for (j = 0; j < privateDicts[i].nBlueValues; ++j) {
611
buf = GooString::format("{0:s}{1:d}",
612
j > 0 ? " " : "", privateDicts[i].blueValues[j]);
613
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
616
(*outputFunc)(outputStream, "] def\n", 6);
618
if (privateDicts[i].nOtherBlues) {
619
(*outputFunc)(outputStream, "/OtherBlues [", 13);
620
for (j = 0; j < privateDicts[i].nOtherBlues; ++j) {
621
buf = GooString::format("{0:s}{1:d}",
622
j > 0 ? " " : "", privateDicts[i].otherBlues[j]);
623
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
626
(*outputFunc)(outputStream, "] def\n", 6);
628
if (privateDicts[i].nFamilyBlues) {
629
(*outputFunc)(outputStream, "/FamilyBlues [", 14);
630
for (j = 0; j < privateDicts[i].nFamilyBlues; ++j) {
631
buf = GooString::format("{0:s}{1:d}",
633
privateDicts[i].familyBlues[j]);
634
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
637
(*outputFunc)(outputStream, "] def\n", 6);
639
if (privateDicts[i].nFamilyOtherBlues) {
640
(*outputFunc)(outputStream, "/FamilyOtherBlues [", 19);
641
for (j = 0; j < privateDicts[i].nFamilyOtherBlues; ++j) {
642
buf = GooString::format("{0:s}{1:d}", j > 0 ? " " : "",
643
privateDicts[i].familyOtherBlues[j]);
644
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
647
(*outputFunc)(outputStream, "] def\n", 6);
649
if (privateDicts[i].blueScale != 0.039625) {
650
buf = GooString::format("/BlueScale {0:.4g} def\n",
651
privateDicts[i].blueScale);
652
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
655
if (privateDicts[i].blueShift != 7) {
656
buf = GooString::format("/BlueShift {0:d} def\n",
657
privateDicts[i].blueShift);
658
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
661
if (privateDicts[i].blueFuzz != 1) {
662
buf = GooString::format("/BlueFuzz {0:d} def\n", privateDicts[i].blueFuzz);
663
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
666
if (privateDicts[i].hasStdHW) {
667
buf = GooString::format("/StdHW [{0:.4g}] def\n", privateDicts[i].stdHW);
668
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
671
if (privateDicts[i].hasStdVW) {
672
buf = GooString::format("/StdVW [{0:.4g}] def\n", privateDicts[i].stdVW);
673
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
676
if (privateDicts[i].nStemSnapH) {
677
(*outputFunc)(outputStream, "/StemSnapH [", 12);
678
for (j = 0; j < privateDicts[i].nStemSnapH; ++j) {
679
buf = GooString::format("{0:s}{1:.4g}",
680
j > 0 ? " " : "", privateDicts[i].stemSnapH[j]);
681
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
684
(*outputFunc)(outputStream, "] def\n", 6);
686
if (privateDicts[i].nStemSnapV) {
687
(*outputFunc)(outputStream, "/StemSnapV [", 12);
688
for (j = 0; j < privateDicts[i].nStemSnapV; ++j) {
689
buf = GooString::format("{0:s}{1:.4g}",
690
j > 0 ? " " : "", privateDicts[i].stemSnapV[j]);
691
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
694
(*outputFunc)(outputStream, "] def\n", 6);
696
if (privateDicts[i].hasForceBold) {
697
buf = GooString::format("/ForceBold {0:s} def\n",
698
privateDicts[i].forceBold ? "true" : "false");
699
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
702
if (privateDicts[i].forceBoldThreshold != 0) {
703
buf = GooString::format("/ForceBoldThreshold {0:.4g} def\n",
704
privateDicts[i].forceBoldThreshold);
705
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
708
if (privateDicts[i].languageGroup != 0) {
709
buf = GooString::format("/LanguageGroup {0:d} def\n",
710
privateDicts[i].languageGroup);
711
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
714
if (privateDicts[i].expansionFactor != 0.06) {
715
buf = GooString::format("/ExpansionFactor {0:.4g} def\n",
716
privateDicts[i].expansionFactor);
717
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
720
(*outputFunc)(outputStream, "currentdict end def\n", 20);
721
(*outputFunc)(outputStream, "currentdict end put\n", 20);
723
(*outputFunc)(outputStream, "def\n", 4);
725
// start the binary section
726
offset = (nCIDs + 1) * (1 + gdBytes);
727
buf = GooString::format("(Hex) {0:d} StartData\n",
728
offset + charStrings->getLength());
729
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
732
// write the charstring offset (CIDMap) table
733
for (i = 0; i <= nCIDs; i += 6) {
734
for (j = 0; j < 6 && i+j <= nCIDs; ++j) {
735
if (i+j < nCIDs && cidMap[i+j] >= 0) {
736
buf2[0] = (char)fdSelect[cidMap[i+j]];
740
n = offset + charStringOffsets[i+j];
741
for (k = gdBytes; k >= 1; --k) {
742
buf2[k] = (char)(n & 0xff);
745
for (k = 0; k <= gdBytes; ++k) {
746
buf = GooString::format("{0:02x}", buf2[k] & 0xff);
747
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
751
(*outputFunc)(outputStream, "\n", 1);
754
// write the charstring data
755
n = charStrings->getLength();
756
for (i = 0; i < n; i += 32) {
757
for (j = 0; j < 32 && i+j < n; ++j) {
758
buf = GooString::format("{0:02x}", charStrings->getChar(i+j) & 0xff);
759
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
763
(*outputFunc)(outputStream, ">", 1);
765
(*outputFunc)(outputStream, "\n", 1);
768
gfree(charStringOffsets);
773
void FoFiType1C::convertToType0(char *psName,
774
FoFiOutputFunc outputFunc,
775
void *outputStream) {
785
// compute the CID count and build the CID-to-GID mapping
787
for (i = 0; i < nGlyphs; ++i) {
788
if (charset[i] >= nCIDs) {
789
nCIDs = charset[i] + 1;
792
cidMap = (int *)gmallocn(nCIDs, sizeof(int));
793
for (i = 0; i < nCIDs; ++i) {
796
for (i = 0; i < nGlyphs; ++i) {
797
cidMap[charset[i]] = i;
800
// write the descendant Type 1 fonts
801
for (i = 0; i < nCIDs; i += 256) {
803
//~ this assumes that all CIDs in this block have the same FD --
804
//~ to handle multiple FDs correctly, need to somehow divide the
805
//~ font up by FD; as a kludge we ignore CID 0, which is .notdef
807
if (fdSelect != NULL) {
808
for (j = i==0 ? 1 : 0; j < 256 && i+j < nCIDs; ++j) {
809
if (cidMap[i+j] >= 0) {
810
fd = fdSelect[cidMap[i+j]];
816
// font dictionary (unencrypted section)
817
(*outputFunc)(outputStream, "16 dict begin\n", 14);
818
(*outputFunc)(outputStream, "/FontName /", 11);
819
(*outputFunc)(outputStream, psName, strlen(psName));
820
buf = GooString::format("_{0:02x} def\n", i >> 8);
821
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
823
(*outputFunc)(outputStream, "/FontType 1 def\n", 16);
824
if (privateDicts[fd].hasFontMatrix) {
825
buf = GooString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
826
privateDicts[fd].fontMatrix[0],
827
privateDicts[fd].fontMatrix[1],
828
privateDicts[fd].fontMatrix[2],
829
privateDicts[fd].fontMatrix[3],
830
privateDicts[fd].fontMatrix[4],
831
privateDicts[fd].fontMatrix[5]);
832
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
834
} else if (topDict.hasFontMatrix) {
835
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
837
(*outputFunc)(outputStream,
838
"/FontMatrix [0.001 0 0 0.001 0 0] def\n", 38);
840
buf = GooString::format("/FontBBox [{0:.4g} {1:.4g} {2:.4g} {3:.4g}] def\n",
841
topDict.fontBBox[0], topDict.fontBBox[1],
842
topDict.fontBBox[2], topDict.fontBBox[3]);
843
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
845
buf = GooString::format("/PaintType {0:d} def\n", topDict.paintType);
846
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
848
if (topDict.paintType != 0) {
849
buf = GooString::format("/StrokeWidth {0:.4g} def\n", topDict.strokeWidth);
850
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
853
(*outputFunc)(outputStream, "/Encoding 256 array\n", 20);
854
for (j = 0; j < 256 && i+j < nCIDs; ++j) {
855
buf = GooString::format("dup {0:d} /c{1:02x} put\n", j, j);
856
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
860
buf = GooString::format("{0:d} 1 255 {{ 1 index exch /.notdef put }} for\n",
862
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
865
(*outputFunc)(outputStream, "readonly def\n", 13);
866
(*outputFunc)(outputStream, "currentdict end\n", 16);
868
// start the binary section
869
(*outputFunc)(outputStream, "currentfile eexec\n", 18);
870
eb.outputFunc = outputFunc;
871
eb.outputStream = outputStream;
876
// start the private dictionary
877
eexecWrite(&eb, "\x83\xca\x73\xd5");
878
eexecWrite(&eb, "dup /Private 32 dict dup begin\n");
879
eexecWrite(&eb, "/RD {string currentfile exch readstring pop}"
880
" executeonly def\n");
881
eexecWrite(&eb, "/ND {noaccess def} executeonly def\n");
882
eexecWrite(&eb, "/NP {noaccess put} executeonly def\n");
883
eexecWrite(&eb, "/MinFeature {16 16} def\n");
884
eexecWrite(&eb, "/password 5839 def\n");
885
if (privateDicts[fd].nBlueValues) {
886
eexecWrite(&eb, "/BlueValues [");
887
for (k = 0; k < privateDicts[fd].nBlueValues; ++k) {
888
buf = GooString::format("{0:s}{1:d}",
890
privateDicts[fd].blueValues[k]);
891
eexecWrite(&eb, buf->getCString());
894
eexecWrite(&eb, "] def\n");
896
if (privateDicts[fd].nOtherBlues) {
897
eexecWrite(&eb, "/OtherBlues [");
898
for (k = 0; k < privateDicts[fd].nOtherBlues; ++k) {
899
buf = GooString::format("{0:s}{1:d}",
901
privateDicts[fd].otherBlues[k]);
902
eexecWrite(&eb, buf->getCString());
905
eexecWrite(&eb, "] def\n");
907
if (privateDicts[fd].nFamilyBlues) {
908
eexecWrite(&eb, "/FamilyBlues [");
909
for (k = 0; k < privateDicts[fd].nFamilyBlues; ++k) {
910
buf = GooString::format("{0:s}{1:d}", k > 0 ? " " : "",
911
privateDicts[fd].familyBlues[k]);
912
eexecWrite(&eb, buf->getCString());
915
eexecWrite(&eb, "] def\n");
917
if (privateDicts[fd].nFamilyOtherBlues) {
918
eexecWrite(&eb, "/FamilyOtherBlues [");
919
for (k = 0; k < privateDicts[fd].nFamilyOtherBlues; ++k) {
920
buf = GooString::format("{0:s}{1:d}", k > 0 ? " " : "",
921
privateDicts[fd].familyOtherBlues[k]);
922
eexecWrite(&eb, buf->getCString());
925
eexecWrite(&eb, "] def\n");
927
if (privateDicts[fd].blueScale != 0.039625) {
928
buf = GooString::format("/BlueScale {0:.4g} def\n",
929
privateDicts[fd].blueScale);
930
eexecWrite(&eb, buf->getCString());
933
if (privateDicts[fd].blueShift != 7) {
934
buf = GooString::format("/BlueShift {0:d} def\n",
935
privateDicts[fd].blueShift);
936
eexecWrite(&eb, buf->getCString());
939
if (privateDicts[fd].blueFuzz != 1) {
940
buf = GooString::format("/BlueFuzz {0:d} def\n",
941
privateDicts[fd].blueFuzz);
942
eexecWrite(&eb, buf->getCString());
945
if (privateDicts[fd].hasStdHW) {
946
buf = GooString::format("/StdHW [{0:.4g}] def\n", privateDicts[fd].stdHW);
947
eexecWrite(&eb, buf->getCString());
950
if (privateDicts[fd].hasStdVW) {
951
buf = GooString::format("/StdVW [{0:.4g}] def\n", privateDicts[fd].stdVW);
952
eexecWrite(&eb, buf->getCString());
955
if (privateDicts[fd].nStemSnapH) {
956
eexecWrite(&eb, "/StemSnapH [");
957
for (k = 0; k < privateDicts[fd].nStemSnapH; ++k) {
958
buf = GooString::format("{0:s}{1:.4g}",
959
k > 0 ? " " : "", privateDicts[fd].stemSnapH[k]);
960
eexecWrite(&eb, buf->getCString());
963
eexecWrite(&eb, "] def\n");
965
if (privateDicts[fd].nStemSnapV) {
966
eexecWrite(&eb, "/StemSnapV [");
967
for (k = 0; k < privateDicts[fd].nStemSnapV; ++k) {
968
buf = GooString::format("{0:s}{1:.4g}",
969
k > 0 ? " " : "", privateDicts[fd].stemSnapV[k]);
970
eexecWrite(&eb, buf->getCString());
973
eexecWrite(&eb, "] def\n");
975
if (privateDicts[fd].hasForceBold) {
976
buf = GooString::format("/ForceBold {0:s} def\n",
977
privateDicts[fd].forceBold ? "true" : "false");
978
eexecWrite(&eb, buf->getCString());
981
if (privateDicts[fd].forceBoldThreshold != 0) {
982
buf = GooString::format("/ForceBoldThreshold {0:.4g} def\n",
983
privateDicts[fd].forceBoldThreshold);
984
eexecWrite(&eb, buf->getCString());
987
if (privateDicts[fd].languageGroup != 0) {
988
buf = GooString::format("/LanguageGroup {0:d} def\n",
989
privateDicts[fd].languageGroup);
990
eexecWrite(&eb, buf->getCString());
993
if (privateDicts[fd].expansionFactor != 0.06) {
994
buf = GooString::format("/ExpansionFactor {0:.4g} def\n",
995
privateDicts[fd].expansionFactor);
996
eexecWrite(&eb, buf->getCString());
1000
// set up the subroutines
1002
getIndex(privateDicts[fd].subrsOffset, &subrIdx, &ok);
1007
// start the CharStrings
1008
eexecWrite(&eb, "2 index /CharStrings 256 dict dup begin\n");
1010
// write the .notdef CharString
1012
getIndexVal(&charStringsIdx, 0, &val, &ok);
1014
eexecCvtGlyph(&eb, ".notdef", val.pos, val.len,
1015
&subrIdx, &privateDicts[fd]);
1018
// write the CharStrings
1019
for (j = 0; j < 256 && i+j < nCIDs; ++j) {
1020
if (cidMap[i+j] >= 0) {
1022
getIndexVal(&charStringsIdx, cidMap[i+j], &val, &ok);
1024
buf = GooString::format("c{0:02x}", j);
1025
eexecCvtGlyph(&eb, buf->getCString(), val.pos, val.len,
1026
&subrIdx, &privateDicts[fd]);
1031
eexecWrite(&eb, "end\n");
1032
eexecWrite(&eb, "end\n");
1033
eexecWrite(&eb, "readonly put\n");
1034
eexecWrite(&eb, "noaccess put\n");
1035
eexecWrite(&eb, "dup /FontName get exch definefont pop\n");
1036
eexecWrite(&eb, "mark currentfile closefile\n");
1040
(*outputFunc)(outputStream, "\n", 1);
1042
for (j = 0; j < 8; ++j) {
1043
(*outputFunc)(outputStream, "0000000000000000000000000000000000000000000000000000000000000000\n", 65);
1045
(*outputFunc)(outputStream, "cleartomark\n", 12);
1048
// write the Type 0 parent font
1049
(*outputFunc)(outputStream, "16 dict begin\n", 14);
1050
(*outputFunc)(outputStream, "/FontName /", 11);
1051
(*outputFunc)(outputStream, psName, strlen(psName));
1052
(*outputFunc)(outputStream, " def\n", 5);
1053
(*outputFunc)(outputStream, "/FontType 0 def\n", 16);
1054
if (topDict.hasFontMatrix) {
1055
buf = GooString::format("/FontMatrix [{0:.8g} {1:.8g} {2:.8g} {3:.8g} {4:.8g} {5:.8g}] def\n",
1056
topDict.fontMatrix[0], topDict.fontMatrix[1],
1057
topDict.fontMatrix[2], topDict.fontMatrix[3],
1058
topDict.fontMatrix[4], topDict.fontMatrix[5]);
1059
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1062
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
1064
(*outputFunc)(outputStream, "/FMapType 2 def\n", 16);
1065
(*outputFunc)(outputStream, "/Encoding [\n", 12);
1066
for (i = 0; i < nCIDs; i += 256) {
1067
buf = GooString::format("{0:d}\n", i >> 8);
1068
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1071
(*outputFunc)(outputStream, "] def\n", 6);
1072
(*outputFunc)(outputStream, "/FDepVector [\n", 14);
1073
for (i = 0; i < nCIDs; i += 256) {
1074
(*outputFunc)(outputStream, "/", 1);
1075
(*outputFunc)(outputStream, psName, strlen(psName));
1076
buf = GooString::format("_{0:02x} findfont\n", i >> 8);
1077
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
1080
(*outputFunc)(outputStream, "] def\n", 6);
1081
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
1086
void FoFiType1C::eexecCvtGlyph(Type1CEexecBuf *eb, char *glyphName,
1087
int offset, int nBytes,
1088
Type1CIndex *subrIdx,
1089
Type1CPrivateDict *pDict) {
1093
// generate the charstring
1094
charBuf = new GooString();
1095
cvtGlyph(offset, nBytes, charBuf, subrIdx, pDict, gTrue);
1097
buf = GooString::format("/{0:s} {1:d} RD ", glyphName, charBuf->getLength());
1098
eexecWrite(eb, buf->getCString());
1100
eexecWriteCharstring(eb, (Guchar *)charBuf->getCString(),
1101
charBuf->getLength());
1102
eexecWrite(eb, " ND\n");
1107
void FoFiType1C::cvtGlyph(int offset, int nBytes, GooString *charBuf,
1108
Type1CIndex *subrIdx, Type1CPrivateDict *pDict,
1115
int pos, subrBias, start, i, k;
1117
start = charBuf->getLength();
1119
charBuf->append((char)73);
1120
charBuf->append((char)58);
1121
charBuf->append((char)147);
1122
charBuf->append((char)134);
1130
while (pos < offset + nBytes) {
1132
pos = getOp(pos, gTrue, &ok);
1136
if (!ops[nOps - 1].isNum) {
1137
--nOps; // drop the operator
1138
switch (ops[nOps].op) {
1139
case 0x0001: // hstem
1141
cvtGlyphWidth(nOps & 1, charBuf, pDict);
1145
//~ error(-1, "Wrong number of args (%d) to Type 2 hstem", nOps);
1149
for (k = 0; k < nOps; k += 2) {
1150
// convert Type 2 edge hints (-20 or -21) to Type 1 ghost hints
1151
if (ops[k+1].num < 0) {
1152
d += ops[k].num + ops[k+1].num;
1153
dFP |= ops[k].isFP | ops[k+1].isFP;
1154
cvtNum(d, dFP, charBuf);
1155
cvtNum(-ops[k+1].num, ops[k+1].isFP, charBuf);
1159
cvtNum(d, dFP, charBuf);
1160
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1162
dFP |= ops[k+1].isFP;
1164
charBuf->append((char)1);
1169
case 0x0003: // vstem
1171
cvtGlyphWidth(nOps & 1, charBuf, pDict);
1175
//~ error(-1, "Wrong number of args (%d) to Type 2 vstem", nOps);
1179
for (k = 0; k < nOps; k += 2) {
1180
// convert Type 2 edge hints (-20 or -21) to Type 1 ghost hints
1181
if (ops[k+1].num < 0) {
1182
d += ops[k].num + ops[k+1].num;
1183
dFP |= ops[k].isFP | ops[k+1].isFP;
1184
cvtNum(d, dFP, charBuf);
1185
cvtNum(-ops[k+1].num, ops[k+1].isFP, charBuf);
1189
cvtNum(d, dFP, charBuf);
1190
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1192
dFP |= ops[k+1].isFP;
1194
charBuf->append((char)3);
1199
case 0x0004: // vmoveto
1201
cvtGlyphWidth(nOps == 2, charBuf, pDict);
1205
charBuf->append((char)9);
1209
//~ error(-1, "Wrong number of args (%d) to Type 2 vmoveto", nOps);
1211
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1212
charBuf->append((char)4);
1215
case 0x0005: // rlineto
1216
if (nOps < 2 || nOps % 2 != 0) {
1217
//~ error(-1, "Wrong number of args (%d) to Type 2 rlineto", nOps);
1219
for (k = 0; k < nOps; k += 2) {
1220
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1221
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1222
charBuf->append((char)5);
1227
case 0x0006: // hlineto
1229
//~ error(-1, "Wrong number of args (%d) to Type 2 hlineto", nOps);
1231
for (k = 0; k < nOps; ++k) {
1232
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1233
charBuf->append((char)((k & 1) ? 7 : 6));
1238
case 0x0007: // vlineto
1240
//~ error(-1, "Wrong number of args (%d) to Type 2 vlineto", nOps);
1242
for (k = 0; k < nOps; ++k) {
1243
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1244
charBuf->append((char)((k & 1) ? 6 : 7));
1249
case 0x0008: // rrcurveto
1250
if (nOps < 6 || nOps % 6 != 0) {
1251
//~ error(-1, "Wrong number of args (%d) to Type 2 rrcurveto", nOps);
1253
for (k = 0; k < nOps; k += 6) {
1254
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1255
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1256
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1257
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1258
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1259
cvtNum(ops[k+5].num, ops[k+5].isFP, charBuf);
1260
charBuf->append((char)8);
1265
case 0x000a: // callsubr
1267
subrBias = (subrIdx->len < 1240)
1268
? 107 : (subrIdx->len < 33900) ? 1131 : 32768;
1269
k = subrBias + (int)ops[nOps - 1].num;
1272
getIndexVal(subrIdx, k, &val, &ok);
1274
cvtGlyph(val.pos, val.len, charBuf, subrIdx, pDict, gFalse);
1277
//~ error(-1, "Too few args to Type 2 callsubr");
1279
// don't clear the stack
1281
case 0x000b: // return
1282
// don't clear the stack
1284
case 0x000e: // endchar / seac
1286
cvtGlyphWidth(nOps == 1 || nOps == 5, charBuf, pDict);
1290
charBuf->append((char)9);
1294
cvtNum(0, gFalse, charBuf);
1295
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1296
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1297
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1298
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1299
charBuf->append((char)12)->append((char)6);
1300
} else if (nOps == 0) {
1301
charBuf->append((char)14);
1303
//~ error(-1, "Wrong number of args (%d) to Type 2 endchar", nOps);
1307
case 0x000f: // (obsolete)
1308
// this op is ignored, but we need the glyph width
1310
cvtGlyphWidth(nOps > 0, charBuf, pDict);
1315
case 0x0010: // blend
1316
//~ error(-1, "Unimplemented Type 2 charstring op: %d", file[i]);
1319
case 0x0012: // hstemhm
1322
cvtGlyphWidth(nOps & 1, charBuf, pDict);
1326
//~ error(-1, "Wrong number of args (%d) to Type 2 hstemhm", nOps);
1331
case 0x0013: // hintmask
1334
cvtGlyphWidth(nOps & 1, charBuf, pDict);
1339
//~ error(-1, "Wrong number of args (%d) to Type 2 hintmask/vstemhm",
1344
pos += (nHints + 7) >> 3;
1347
case 0x0014: // cntrmask
1350
cvtGlyphWidth(nOps & 1, charBuf, pDict);
1355
//~ error(-1, "Wrong number of args (%d) to Type 2 cntrmask/vstemhm",
1360
pos += (nHints + 7) >> 3;
1363
case 0x0015: // rmoveto
1365
cvtGlyphWidth(nOps == 3, charBuf, pDict);
1369
charBuf->append((char)9);
1373
//~ error(-1, "Wrong number of args (%d) to Type 2 rmoveto", nOps);
1375
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1376
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1377
charBuf->append((char)21);
1380
case 0x0016: // hmoveto
1382
cvtGlyphWidth(nOps == 2, charBuf, pDict);
1386
charBuf->append((char)9);
1390
//~ error(-1, "Wrong number of args (%d) to Type 2 hmoveto", nOps);
1392
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1393
charBuf->append((char)22);
1396
case 0x0017: // vstemhm
1399
cvtGlyphWidth(nOps & 1, charBuf, pDict);
1403
//~ error(-1, "Wrong number of args (%d) to Type 2 vstemhm", nOps);
1408
case 0x0018: // rcurveline
1409
if (nOps < 8 || (nOps - 2) % 6 != 0) {
1410
//~ error(-1, "Wrong number of args (%d) to Type 2 rcurveline", nOps);
1412
for (k = 0; k < nOps - 2; k += 6) {
1413
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1414
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1415
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1416
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1417
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1418
cvtNum(ops[k+5].num, ops[k+5].isFP, charBuf);
1419
charBuf->append((char)8);
1421
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1422
cvtNum(ops[k+1].num, ops[k].isFP, charBuf);
1423
charBuf->append((char)5);
1427
case 0x0019: // rlinecurve
1428
if (nOps < 8 || (nOps - 6) % 2 != 0) {
1429
//~ error(-1, "Wrong number of args (%d) to Type 2 rlinecurve", nOps);
1431
for (k = 0; k < nOps - 6; k += 2) {
1432
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1433
cvtNum(ops[k+1].num, ops[k].isFP, charBuf);
1434
charBuf->append((char)5);
1436
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1437
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1438
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1439
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1440
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1441
cvtNum(ops[k+5].num, ops[k+5].isFP, charBuf);
1442
charBuf->append((char)8);
1446
case 0x001a: // vvcurveto
1447
if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1448
//~ error(-1, "Wrong number of args (%d) to Type 2 vvcurveto", nOps);
1450
if (nOps % 2 == 1) {
1451
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1452
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1453
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1454
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1455
cvtNum(0, gFalse, charBuf);
1456
cvtNum(ops[4].num, ops[4].isFP, charBuf);
1457
charBuf->append((char)8);
1462
for (; k < nOps; k += 4) {
1463
cvtNum(0, gFalse, charBuf);
1464
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1465
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1466
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1467
cvtNum(0, gFalse, charBuf);
1468
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1469
charBuf->append((char)8);
1474
case 0x001b: // hhcurveto
1475
if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1476
//~ error(-1, "Wrong number of args (%d) to Type 2 hhcurveto", nOps);
1478
if (nOps % 2 == 1) {
1479
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1480
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1481
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1482
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1483
cvtNum(ops[4].num, ops[4].isFP, charBuf);
1484
cvtNum(0, gFalse, charBuf);
1485
charBuf->append((char)8);
1490
for (; k < nOps; k += 4) {
1491
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1492
cvtNum(0, gFalse, charBuf);
1493
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1494
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1495
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1496
cvtNum(0, gFalse, charBuf);
1497
charBuf->append((char)8);
1502
case 0x001d: // callgsubr
1504
k = gsubrBias + (int)ops[nOps - 1].num;
1507
getIndexVal(&gsubrIdx, k, &val, &ok);
1509
cvtGlyph(val.pos, val.len, charBuf, subrIdx, pDict, gFalse);
1512
//~ error(-1, "Too few args to Type 2 callgsubr");
1514
// don't clear the stack
1516
case 0x001e: // vhcurveto
1517
if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1518
//~ error(-1, "Wrong number of args (%d) to Type 2 vhcurveto", nOps);
1520
for (k = 0; k < nOps && k != nOps-5; k += 4) {
1522
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1523
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1524
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1525
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1526
charBuf->append((char)30);
1528
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1529
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1530
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1531
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1532
charBuf->append((char)31);
1537
cvtNum(0, gFalse, charBuf);
1538
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1539
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1540
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1541
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1542
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1544
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1545
cvtNum(0, gFalse, charBuf);
1546
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1547
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1548
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1549
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1551
charBuf->append((char)8);
1556
case 0x001f: // hvcurveto
1557
if (nOps < 4 || !(nOps % 4 == 0 || (nOps-1) % 4 == 0)) {
1558
//~ error(-1, "Wrong number of args (%d) to Type 2 hvcurveto", nOps);
1560
for (k = 0; k < nOps && k != nOps-5; k += 4) {
1562
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1563
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1564
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1565
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1566
charBuf->append((char)31);
1568
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1569
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1570
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1571
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1572
charBuf->append((char)30);
1577
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1578
cvtNum(0, gFalse, charBuf);
1579
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1580
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1581
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1582
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1584
cvtNum(0, gFalse, charBuf);
1585
cvtNum(ops[k].num, ops[k].isFP, charBuf);
1586
cvtNum(ops[k+1].num, ops[k+1].isFP, charBuf);
1587
cvtNum(ops[k+2].num, ops[k+2].isFP, charBuf);
1588
cvtNum(ops[k+3].num, ops[k+3].isFP, charBuf);
1589
cvtNum(ops[k+4].num, ops[k+4].isFP, charBuf);
1591
charBuf->append((char)8);
1596
case 0x0c00: // dotsection (should be Type 1 only?)
1603
case 0x0c08: // store
1608
case 0x0c0d: // load
1611
case 0x0c12: // drop
1614
case 0x0c16: // ifelse
1615
case 0x0c17: // random
1617
case 0x0c1a: // sqrt
1619
case 0x0c1c: // exch
1620
case 0x0c1d: // index
1621
case 0x0c1e: // roll
1622
//~ error(-1, "Unimplemented Type 2 charstring op: 12.%d", file[i+1]);
1625
case 0x0c22: // hflex
1627
//~ error(-1, "Wrong number of args (%d) to Type 2 hflex", nOps);
1629
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1630
cvtNum(0, gFalse, charBuf);
1631
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1632
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1633
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1634
cvtNum(0, gFalse, charBuf);
1635
charBuf->append((char)8);
1636
cvtNum(ops[4].num, ops[4].isFP, charBuf);
1637
cvtNum(0, gFalse, charBuf);
1638
cvtNum(ops[5].num, ops[5].isFP, charBuf);
1639
cvtNum(-ops[2].num, ops[2].isFP, charBuf);
1640
cvtNum(ops[6].num, ops[6].isFP, charBuf);
1641
cvtNum(0, gFalse, charBuf);
1642
charBuf->append((char)8);
1646
case 0x0c23: // flex
1648
//~ error(-1, "Wrong number of args (%d) to Type 2 flex", nOps);
1650
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1651
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1652
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1653
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1654
cvtNum(ops[4].num, ops[4].isFP, charBuf);
1655
cvtNum(ops[5].num, ops[5].isFP, charBuf);
1656
charBuf->append((char)8);
1657
cvtNum(ops[6].num, ops[6].isFP, charBuf);
1658
cvtNum(ops[7].num, ops[7].isFP, charBuf);
1659
cvtNum(ops[8].num, ops[8].isFP, charBuf);
1660
cvtNum(ops[9].num, ops[9].isFP, charBuf);
1661
cvtNum(ops[10].num, ops[10].isFP, charBuf);
1662
cvtNum(ops[11].num, ops[11].isFP, charBuf);
1663
charBuf->append((char)8);
1667
case 0x0c24: // hflex1
1669
//~ error(-1, "Wrong number of args (%d) to Type 2 hflex1", nOps);
1671
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1672
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1673
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1674
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1675
cvtNum(ops[4].num, ops[4].isFP, charBuf);
1676
cvtNum(0, gFalse, charBuf);
1677
charBuf->append((char)8);
1678
cvtNum(ops[5].num, ops[5].isFP, charBuf);
1679
cvtNum(0, gFalse, charBuf);
1680
cvtNum(ops[6].num, ops[6].isFP, charBuf);
1681
cvtNum(ops[7].num, ops[7].isFP, charBuf);
1682
cvtNum(ops[8].num, ops[8].isFP, charBuf);
1683
cvtNum(-(ops[1].num + ops[3].num + ops[7].num),
1684
ops[1].isFP | ops[3].isFP | ops[7].isFP, charBuf);
1685
charBuf->append((char)8);
1689
case 0x0c25: // flex1
1691
//~ error(-1, "Wrong number of args (%d) to Type 2 flex1", nOps);
1693
cvtNum(ops[0].num, ops[0].isFP, charBuf);
1694
cvtNum(ops[1].num, ops[1].isFP, charBuf);
1695
cvtNum(ops[2].num, ops[2].isFP, charBuf);
1696
cvtNum(ops[3].num, ops[3].isFP, charBuf);
1697
cvtNum(ops[4].num, ops[4].isFP, charBuf);
1698
cvtNum(ops[5].num, ops[5].isFP, charBuf);
1699
charBuf->append((char)8);
1700
cvtNum(ops[6].num, ops[6].isFP, charBuf);
1701
cvtNum(ops[7].num, ops[7].isFP, charBuf);
1702
cvtNum(ops[8].num, ops[8].isFP, charBuf);
1703
cvtNum(ops[9].num, ops[9].isFP, charBuf);
1704
dx = ops[0].num + ops[2].num + ops[4].num + ops[6].num + ops[8].num;
1705
dy = ops[1].num + ops[3].num + ops[5].num + ops[7].num + ops[9].num;
1706
if (fabs(dx) > fabs(dy)) {
1707
cvtNum(ops[10].num, ops[10].isFP, charBuf);
1708
cvtNum(-dy, ops[1].isFP | ops[3].isFP | ops[5].isFP |
1709
ops[7].isFP | ops[9].isFP, charBuf);
1711
cvtNum(-dx, ops[0].isFP | ops[2].isFP | ops[4].isFP |
1712
ops[6].isFP | ops[8].isFP, charBuf);
1713
cvtNum(ops[10].num, ops[10].isFP, charBuf);
1715
charBuf->append((char)8);
1720
//~ error(-1, "Illegal Type 2 charstring op: %04x",
1728
// charstring encryption
1731
for (i = start; i < charBuf->getLength(); ++i) {
1732
byte = charBuf->getChar(i) ^ (r2 >> 8);
1733
charBuf->setChar(i, byte);
1734
r2 = (byte + r2) * 52845 + 22719;
1739
void FoFiType1C::cvtGlyphWidth(GBool useOp, GooString *charBuf,
1740
Type1CPrivateDict *pDict) {
1746
w = pDict->nominalWidthX + ops[0].num;
1747
wFP = pDict->nominalWidthXFP | ops[0].isFP;
1748
for (i = 1; i < nOps; ++i) {
1753
w = pDict->defaultWidthX;
1754
wFP = pDict->defaultWidthXFP;
1756
cvtNum(0, gFalse, charBuf);
1757
cvtNum(w, wFP, charBuf);
1758
charBuf->append((char)13);
1761
void FoFiType1C::cvtNum(double x, GBool isFP, GooString *charBuf) {
1767
if (x >= -32768 && x < 32768) {
1768
y = (int)(x * 256.0);
1770
buf[1] = (Guchar)(y >> 24);
1771
buf[2] = (Guchar)(y >> 16);
1772
buf[3] = (Guchar)(y >> 8);
1783
//~ error(-1, "Type 2 fixed point constant out of range");
1787
if (y >= -107 && y <= 107) {
1788
buf[0] = (Guchar)(y + 139);
1790
} else if (y > 107 && y <= 1131) {
1792
buf[0] = (Guchar)((y >> 8) + 247);
1793
buf[1] = (Guchar)(y & 0xff);
1795
} else if (y < -107 && y >= -1131) {
1797
buf[0] = (Guchar)((y >> 8) + 251);
1798
buf[1] = (Guchar)(y & 0xff);
1802
buf[1] = (Guchar)(y >> 24);
1803
buf[2] = (Guchar)(y >> 16);
1804
buf[3] = (Guchar)(y >> 8);
1809
charBuf->append((char *)buf, n);
1812
void FoFiType1C::eexecWrite(Type1CEexecBuf *eb, char *s) {
1816
for (p = (Guchar *)s; *p; ++p) {
1817
x = *p ^ (eb->r1 >> 8);
1818
eb->r1 = (x + eb->r1) * 52845 + 22719;
1820
(*eb->outputFunc)(eb->outputStream, &hexChars[x >> 4], 1);
1821
(*eb->outputFunc)(eb->outputStream, &hexChars[x & 0x0f], 1);
1823
if (eb->line == 64) {
1824
(*eb->outputFunc)(eb->outputStream, "\n", 1);
1828
(*eb->outputFunc)(eb->outputStream, (char *)&x, 1);
1833
void FoFiType1C::eexecWriteCharstring(Type1CEexecBuf *eb,
1839
for (i = 0; i < n; ++i) {
1840
x = s[i] ^ (eb->r1 >> 8);
1841
eb->r1 = (x + eb->r1) * 52845 + 22719;
1843
(*eb->outputFunc)(eb->outputStream, &hexChars[x >> 4], 1);
1844
(*eb->outputFunc)(eb->outputStream, &hexChars[x & 0x0f], 1);
1846
if (eb->line == 64) {
1847
(*eb->outputFunc)(eb->outputStream, "\n", 1);
1851
(*eb->outputFunc)(eb->outputStream, (char *)&x, 1);
1856
GBool FoFiType1C::parse() {
1863
// some tools embed Type 1C fonts with an extra whitespace char at
1865
if (len > 0 && file[0] != '\x01') {
1871
getIndex(getU8(2, &parsedOk), &nameIdx, &parsedOk);
1872
getIndex(nameIdx.endPos, &topDictIdx, &parsedOk);
1873
getIndex(topDictIdx.endPos, &stringIdx, &parsedOk);
1874
getIndex(stringIdx.endPos, &gsubrIdx, &parsedOk);
1878
gsubrBias = (gsubrIdx.len < 1240) ? 107
1879
: (gsubrIdx.len < 33900) ? 1131 : 32768;
1881
// read the first font name
1882
getIndexVal(&nameIdx, 0, &val, &parsedOk);
1886
name = new GooString((char *)&file[val.pos], val.len);
1888
// read the top dict for the first font
1891
// for CID fonts: read the FDArray dicts and private dicts
1892
if (topDict.firstOp == 0x0c1e) {
1893
if (topDict.fdArrayOffset == 0) {
1895
privateDicts = (Type1CPrivateDict *)gmalloc(sizeof(Type1CPrivateDict));
1896
readPrivateDict(0, 0, &privateDicts[0]);
1898
getIndex(topDict.fdArrayOffset, &fdIdx, &parsedOk);
1903
privateDicts = (Type1CPrivateDict *)
1904
gmallocn(nFDs, sizeof(Type1CPrivateDict));
1905
for (i = 0; i < nFDs; ++i) {
1906
getIndexVal(&fdIdx, i, &val, &parsedOk);
1910
readFD(val.pos, val.len, &privateDicts[i]);
1914
// for 8-bit fonts: read the private dict
1916
privateDicts = (Type1CPrivateDict *)gmalloc(sizeof(Type1CPrivateDict));
1917
readPrivateDict(topDict.privateOffset, topDict.privateSize,
1921
// check for parse errors in the private dict(s)
1926
// get the charstrings index
1927
if (topDict.charStringsOffset <= 0) {
1931
getIndex(topDict.charStringsOffset, &charStringsIdx, &parsedOk);
1935
nGlyphs = charStringsIdx.len;
1937
// for CID fonts: read the FDSelect table
1938
if (topDict.firstOp == 0x0c1e) {
1946
if (!readCharset()) {
1951
// for 8-bit fonts: build the encoding
1952
if (topDict.firstOp != 0x0c14 && topDict.firstOp != 0x0c1e) {
1962
void FoFiType1C::readTopDict() {
1963
Type1CIndexVal topDictPtr;
1966
topDict.firstOp = -1;
1967
topDict.versionSID = 0;
1968
topDict.noticeSID = 0;
1969
topDict.copyrightSID = 0;
1970
topDict.fullNameSID = 0;
1971
topDict.familyNameSID = 0;
1972
topDict.weightSID = 0;
1973
topDict.isFixedPitch = 0;
1974
topDict.italicAngle = 0;
1975
topDict.underlinePosition = -100;
1976
topDict.underlineThickness = 50;
1977
topDict.paintType = 0;
1978
topDict.charstringType = 2;
1979
topDict.fontMatrix[0] = 0.001;
1980
topDict.fontMatrix[1] = 0;
1981
topDict.fontMatrix[2] = 0;
1982
topDict.fontMatrix[3] = 0.001;
1983
topDict.fontMatrix[4] = 0;
1984
topDict.fontMatrix[5] = 0;
1985
topDict.hasFontMatrix = gFalse;
1986
topDict.uniqueID = 0;
1987
topDict.fontBBox[0] = 0;
1988
topDict.fontBBox[1] = 0;
1989
topDict.fontBBox[2] = 0;
1990
topDict.fontBBox[3] = 0;
1991
topDict.strokeWidth = 0;
1992
topDict.charsetOffset = 0;
1993
topDict.encodingOffset = 0;
1994
topDict.charStringsOffset = 0;
1995
topDict.privateSize = 0;
1996
topDict.privateOffset = 0;
1997
topDict.registrySID = 0;
1998
topDict.orderingSID = 0;
1999
topDict.supplement = 0;
2000
topDict.fdArrayOffset = 0;
2001
topDict.fdSelectOffset = 0;
2003
getIndexVal(&topDictIdx, 0, &topDictPtr, &parsedOk);
2004
pos = topDictPtr.pos;
2006
while (pos < topDictPtr.pos + topDictPtr.len) {
2007
pos = getOp(pos, gFalse, &parsedOk);
2011
if (!ops[nOps - 1].isNum) {
2012
--nOps; // drop the operator
2013
if (topDict.firstOp < 0) {
2014
topDict.firstOp = ops[nOps].op;
2016
switch (ops[nOps].op) {
2017
case 0x0000: topDict.versionSID = (int)ops[0].num; break;
2018
case 0x0001: topDict.noticeSID = (int)ops[0].num; break;
2019
case 0x0c00: topDict.copyrightSID = (int)ops[0].num; break;
2020
case 0x0002: topDict.fullNameSID = (int)ops[0].num; break;
2021
case 0x0003: topDict.familyNameSID = (int)ops[0].num; break;
2022
case 0x0004: topDict.weightSID = (int)ops[0].num; break;
2023
case 0x0c01: topDict.isFixedPitch = (int)ops[0].num; break;
2024
case 0x0c02: topDict.italicAngle = ops[0].num; break;
2025
case 0x0c03: topDict.underlinePosition = ops[0].num; break;
2026
case 0x0c04: topDict.underlineThickness = ops[0].num; break;
2027
case 0x0c05: topDict.paintType = (int)ops[0].num; break;
2028
case 0x0c06: topDict.charstringType = (int)ops[0].num; break;
2029
case 0x0c07: topDict.fontMatrix[0] = ops[0].num;
2030
topDict.fontMatrix[1] = ops[1].num;
2031
topDict.fontMatrix[2] = ops[2].num;
2032
topDict.fontMatrix[3] = ops[3].num;
2033
topDict.fontMatrix[4] = ops[4].num;
2034
topDict.fontMatrix[5] = ops[5].num;
2035
topDict.hasFontMatrix = gTrue; break;
2036
case 0x000d: topDict.uniqueID = (int)ops[0].num; break;
2037
case 0x0005: topDict.fontBBox[0] = ops[0].num;
2038
topDict.fontBBox[1] = ops[1].num;
2039
topDict.fontBBox[2] = ops[2].num;
2040
topDict.fontBBox[3] = ops[3].num; break;
2041
case 0x0c08: topDict.strokeWidth = ops[0].num; break;
2042
case 0x000f: topDict.charsetOffset = (int)ops[0].num; break;
2043
case 0x0010: topDict.encodingOffset = (int)ops[0].num; break;
2044
case 0x0011: topDict.charStringsOffset = (int)ops[0].num; break;
2045
case 0x0012: topDict.privateSize = (int)ops[0].num;
2046
topDict.privateOffset = (int)ops[1].num; break;
2047
case 0x0c1e: topDict.registrySID = (int)ops[0].num;
2048
topDict.orderingSID = (int)ops[1].num;
2049
topDict.supplement = (int)ops[2].num; break;
2050
case 0x0c24: topDict.fdArrayOffset = (int)ops[0].num; break;
2051
case 0x0c25: topDict.fdSelectOffset = (int)ops[0].num; break;
2058
// Read a CID font dict (FD) - this pulls out the private dict
2059
// pointer, and reads the private dict. It also pulls the FontMatrix
2060
// (if any) out of the FD.
2061
void FoFiType1C::readFD(int offset, int length, Type1CPrivateDict *pDict) {
2062
int pos, pSize, pOffset;
2063
double fontMatrix[6] = {0};
2064
GBool hasFontMatrix;
2066
hasFontMatrix = gFalse;
2067
pSize = pOffset = 0;
2070
while (pos < offset + length) {
2071
pos = getOp(pos, gFalse, &parsedOk);
2075
if (!ops[nOps - 1].isNum) {
2076
if (ops[nOps - 1].op == 0x0012) {
2081
pSize = (int)ops[0].num;
2082
pOffset = (int)ops[1].num;
2084
} else if (ops[nOps - 1].op == 0x0c07) {
2085
fontMatrix[0] = ops[0].num;
2086
fontMatrix[1] = ops[1].num;
2087
fontMatrix[2] = ops[2].num;
2088
fontMatrix[3] = ops[3].num;
2089
fontMatrix[4] = ops[4].num;
2090
fontMatrix[5] = ops[5].num;
2091
hasFontMatrix = gTrue;
2096
readPrivateDict(pOffset, pSize, pDict);
2097
if (hasFontMatrix) {
2098
pDict->fontMatrix[0] = fontMatrix[0];
2099
pDict->fontMatrix[1] = fontMatrix[1];
2100
pDict->fontMatrix[2] = fontMatrix[2];
2101
pDict->fontMatrix[3] = fontMatrix[3];
2102
pDict->fontMatrix[4] = fontMatrix[4];
2103
pDict->fontMatrix[5] = fontMatrix[5];
2104
pDict->hasFontMatrix = gTrue;
2108
void FoFiType1C::readPrivateDict(int offset, int length,
2109
Type1CPrivateDict *pDict) {
2112
pDict->hasFontMatrix = gFalse;
2113
pDict->nBlueValues = 0;
2114
pDict->nOtherBlues = 0;
2115
pDict->nFamilyBlues = 0;
2116
pDict->nFamilyOtherBlues = 0;
2117
pDict->blueScale = 0.039625;
2118
pDict->blueShift = 7;
2119
pDict->blueFuzz = 1;
2120
pDict->hasStdHW = gFalse;
2121
pDict->hasStdVW = gFalse;
2122
pDict->nStemSnapH = 0;
2123
pDict->nStemSnapV = 0;
2124
pDict->hasForceBold = gFalse;
2125
pDict->forceBoldThreshold = 0;
2126
pDict->languageGroup = 0;
2127
pDict->expansionFactor = 0.06;
2128
pDict->initialRandomSeed = 0;
2129
pDict->subrsOffset = 0;
2130
pDict->defaultWidthX = 0;
2131
pDict->defaultWidthXFP = gFalse;
2132
pDict->nominalWidthX = 0;
2133
pDict->nominalWidthXFP = gFalse;
2136
if (offset == 0 || length == 0) {
2142
while (pos < offset + length) {
2143
pos = getOp(pos, gFalse, &parsedOk);
2147
if (!ops[nOps - 1].isNum) {
2148
--nOps; // drop the operator
2149
switch (ops[nOps].op) {
2151
pDict->nBlueValues = getDeltaIntArray(pDict->blueValues,
2152
type1CMaxBlueValues);
2155
pDict->nOtherBlues = getDeltaIntArray(pDict->otherBlues,
2156
type1CMaxOtherBlues);
2159
pDict->nFamilyBlues = getDeltaIntArray(pDict->familyBlues,
2160
type1CMaxBlueValues);
2163
pDict->nFamilyOtherBlues = getDeltaIntArray(pDict->familyOtherBlues,
2164
type1CMaxOtherBlues);
2167
pDict->blueScale = ops[0].num;
2170
pDict->blueShift = (int)ops[0].num;
2173
pDict->blueFuzz = (int)ops[0].num;
2176
pDict->stdHW = ops[0].num;
2177
pDict->hasStdHW = gTrue;
2180
pDict->stdVW = ops[0].num;
2181
pDict->hasStdVW = gTrue;
2184
pDict->nStemSnapH = getDeltaFPArray(pDict->stemSnapH,
2188
pDict->nStemSnapV = getDeltaFPArray(pDict->stemSnapV,
2192
pDict->forceBold = ops[0].num != 0;
2193
pDict->hasForceBold = gTrue;
2196
pDict->forceBoldThreshold = ops[0].num;
2199
pDict->languageGroup = (int)ops[0].num;
2202
pDict->expansionFactor = ops[0].num;
2205
pDict->initialRandomSeed = (int)ops[0].num;
2208
pDict->subrsOffset = offset + (int)ops[0].num;
2211
pDict->defaultWidthX = ops[0].num;
2212
pDict->defaultWidthXFP = ops[0].isFP;
2215
pDict->nominalWidthX = ops[0].num;
2216
pDict->nominalWidthXFP = ops[0].isFP;
2224
void FoFiType1C::readFDSelect() {
2225
int fdSelectFmt, pos, nRanges, gid0, gid1, fd, i, j;
2227
fdSelect = (Guchar *)gmalloc(nGlyphs);
2228
if (topDict.fdSelectOffset == 0) {
2229
for (i = 0; i < nGlyphs; ++i) {
2233
pos = topDict.fdSelectOffset;
2234
fdSelectFmt = getU8(pos++, &parsedOk);
2238
if (fdSelectFmt == 0) {
2239
if (!checkRegion(pos, nGlyphs)) {
2243
memcpy(fdSelect, file + pos, nGlyphs);
2244
} else if (fdSelectFmt == 3) {
2245
nRanges = getU16BE(pos, &parsedOk);
2247
gid0 = getU16BE(pos, &parsedOk);
2249
for (i = 1; i <= nRanges; ++i) {
2250
fd = getU8(pos++, &parsedOk);
2251
gid1 = getU16BE(pos, &parsedOk);
2256
if (gid0 > gid1 || gid1 > nGlyphs) {
2257
//~ error(-1, "Bad FDSelect table in CID font");
2261
for (j = gid0; j < gid1; ++j) {
2267
//~ error(-1, "Unknown FDSelect table format in CID font");
2268
for (i = 0; i < nGlyphs; ++i) {
2275
void FoFiType1C::buildEncoding() {
2277
int nCodes, nRanges, encFormat;
2278
int pos, c, sid, nLeft, nSups, i, j;
2280
if (topDict.encodingOffset == 0) {
2281
encoding = fofiType1StandardEncoding;
2283
} else if (topDict.encodingOffset == 1) {
2284
encoding = fofiType1ExpertEncoding;
2287
encoding = (char **)gmallocn(256, sizeof(char *));
2288
for (i = 0; i < 256; ++i) {
2291
pos = topDict.encodingOffset;
2292
encFormat = getU8(pos++, &parsedOk);
2296
if ((encFormat & 0x7f) == 0) {
2297
nCodes = 1 + getU8(pos++, &parsedOk);
2301
if (nCodes > nGlyphs) {
2304
for (i = 1; i < nCodes; ++i) {
2305
c = getU8(pos++, &parsedOk);
2312
encoding[c] = copyString(getString(charset[i], buf, &parsedOk));
2314
} else if ((encFormat & 0x7f) == 1) {
2315
nRanges = getU8(pos++, &parsedOk);
2320
for (i = 0; i < nRanges; ++i) {
2321
c = getU8(pos++, &parsedOk);
2322
nLeft = getU8(pos++, &parsedOk);
2326
for (j = 0; j <= nLeft && nCodes < nGlyphs; ++j) {
2331
encoding[c] = copyString(getString(charset[nCodes], buf,
2339
if (encFormat & 0x80) {
2340
nSups = getU8(pos++, &parsedOk);
2344
for (i = 0; i < nSups; ++i) {
2345
c = getU8(pos++, &parsedOk);;
2349
sid = getU16BE(pos, &parsedOk);
2357
encoding[c] = copyString(getString(sid, buf, &parsedOk));
2363
GBool FoFiType1C::readCharset() {
2364
int charsetFormat, c, pos;
2367
if (topDict.charsetOffset == 0) {
2368
charset = fofiType1CISOAdobeCharset;
2369
} else if (topDict.charsetOffset == 1) {
2370
charset = fofiType1CExpertCharset;
2371
} else if (topDict.charsetOffset == 2) {
2372
charset = fofiType1CExpertSubsetCharset;
2374
charset = (Gushort *)gmallocn(nGlyphs, sizeof(Gushort));
2375
for (i = 0; i < nGlyphs; ++i) {
2378
pos = topDict.charsetOffset;
2379
charsetFormat = getU8(pos++, &parsedOk);
2380
if (charsetFormat == 0) {
2381
for (i = 1; i < nGlyphs; ++i) {
2382
charset[i] = (Gushort)getU16BE(pos, &parsedOk);
2388
} else if (charsetFormat == 1) {
2390
while (i < nGlyphs) {
2391
c = getU16BE(pos, &parsedOk);
2393
nLeft = getU8(pos++, &parsedOk);
2397
for (j = 0; j <= nLeft && i < nGlyphs; ++j) {
2398
charset[i++] = (Gushort)c++;
2401
} else if (charsetFormat == 2) {
2403
while (i < nGlyphs) {
2404
c = getU16BE(pos, &parsedOk);
2406
nLeft = getU16BE(pos, &parsedOk);
2411
for (j = 0; j <= nLeft && i < nGlyphs; ++j) {
2412
charset[i++] = (Gushort)c++;
2425
int FoFiType1C::getOp(int pos, GBool charstring, GBool *ok) {
2426
static char nybChars[16] = "0123456789.ee -";
2429
int b0, b1, nyb0, nyb1, x, i;
2431
b0 = getU8(pos++, ok);
2436
x = getU8(pos++, ok);
2437
x = (x << 8) | getU8(pos++, ok);
2443
} else if (!charstring && b0 == 29) {
2444
x = getU8(pos++, ok);
2445
x = (x << 8) | getU8(pos++, ok);
2446
x = (x << 8) | getU8(pos++, ok);
2447
x = (x << 8) | getU8(pos++, ok);
2448
if (x & 0x80000000) {
2453
} else if (!charstring && b0 == 30) {
2456
b1 = getU8(pos++, ok);
2462
buf[i++] = nybChars[nyb0];
2475
buf[i++] = nybChars[nyb1];
2484
op.num = gatof(buf);
2487
} else if (b0 >= 32 && b0 <= 246) {
2490
} else if (b0 >= 247 && b0 <= 250) {
2491
op.num = ((b0 - 247) << 8) + getU8(pos++, ok) + 108;
2493
} else if (b0 >= 251 && b0 <= 254) {
2494
op.num = -((b0 - 251) << 8) - getU8(pos++, ok) - 108;
2496
} else if (charstring && b0 == 255) {
2497
x = getU8(pos++, ok);
2498
x = (x << 8) | getU8(pos++, ok);
2499
x = (x << 8) | getU8(pos++, ok);
2500
x = (x << 8) | getU8(pos++, ok);
2501
if (x & 0x80000000) {
2504
op.num = (double)x / 65536.0;
2507
} else if (b0 == 12) {
2509
op.op = 0x0c00 + getU8(pos++, ok);
2523
// Convert the delta-encoded ops array to an array of ints.
2524
int FoFiType1C::getDeltaIntArray(int *arr, int maxLen) {
2528
if ((n = nOps) > maxLen) {
2532
for (i = 0; i < n; ++i) {
2533
x += (int)ops[i].num;
2539
// Convert the delta-encoded ops array to an array of doubles.
2540
int FoFiType1C::getDeltaFPArray(double *arr, int maxLen) {
2544
if ((n = nOps) > maxLen) {
2548
for (i = 0; i < n; ++i) {
2555
void FoFiType1C::getIndex(int pos, Type1CIndex *idx, GBool *ok) {
2557
idx->len = getU16BE(pos, ok);
2558
if (idx->len == 0) {
2559
// empty indexes are legal and contain just the length field
2561
idx->startPos = idx->endPos = pos + 2;
2563
idx->offSize = getU8(pos + 2, ok);
2564
if (idx->offSize < 1 || idx->offSize > 4) {
2567
idx->startPos = pos + 3 + (idx->len + 1) * idx->offSize - 1;
2568
if (idx->startPos < 0 || idx->startPos >= len) {
2571
idx->endPos = idx->startPos + getUVarBE(pos + 3 + idx->len * idx->offSize,
2573
if (idx->endPos < idx->startPos || idx->endPos > len) {
2579
void FoFiType1C::getIndexVal(Type1CIndex *idx, int i,
2580
Type1CIndexVal *val, GBool *ok) {
2583
if (i < 0 || i >= idx->len) {
2587
pos0 = idx->startPos + getUVarBE(idx->pos + 3 + i * idx->offSize,
2589
pos1 = idx->startPos + getUVarBE(idx->pos + 3 + (i + 1) * idx->offSize,
2591
if (pos0 < idx->startPos || pos0 > idx->endPos ||
2592
pos1 <= idx->startPos || pos1 > idx->endPos ||
2597
val->len = pos1 - pos0;
2600
char *FoFiType1C::getString(int sid, char *buf, GBool *ok) {
2605
strcpy(buf, fofiType1CStdStrings[sid]);
2608
getIndexVal(&stringIdx, sid, &val, ok);
2610
if ((n = val.len) > 255) {
2613
strncpy(buf, (char *)&file[val.pos], n);