Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / xpdf / GfxFont.cc
blobb8afcce058dfef7768873a2fe686f35f74cb9d03
1 //========================================================================
2 //
3 // GfxFont.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <aconf.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include "gmem.h"
20 #include "Error.h"
21 #include "Object.h"
22 #include "Dict.h"
23 #include "GlobalParams.h"
24 #include "CMap.h"
25 #include "CharCodeToUnicode.h"
26 #include "FontEncodingTables.h"
27 #include "BuiltinFontTables.h"
28 #include "FoFiType1.h"
29 #include "FoFiType1C.h"
30 #include "FoFiTrueType.h"
31 #include "GfxFont.h"
32 #include "FontCache.h"
34 //------------------------------------------------------------------------
36 struct StdFontMapEntry {
37 char *altName;
38 char *properName;
41 // Acrobat 4.0 and earlier substituted Base14-compatible fonts without
42 // providing Widths and a FontDescriptor, so we munge the names into
43 // the proper Base14 names. This table is from implementation note 44
44 // in the PDF 1.4 spec, with some additions based on empirical
45 // evidence.
46 static StdFontMapEntry stdFontMap[] = {
47 { "Arial", "Helvetica" },
48 { "Arial,Bold", "Helvetica-Bold" },
49 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
50 { "Arial,Italic", "Helvetica-Oblique" },
51 { "Arial-Bold", "Helvetica-Bold" },
52 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
53 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
54 { "Arial-BoldMT", "Helvetica-Bold" },
55 { "Arial-Italic", "Helvetica-Oblique" },
56 { "Arial-ItalicMT", "Helvetica-Oblique" },
57 { "ArialMT", "Helvetica" },
58 { "Courier,Bold", "Courier-Bold" },
59 { "Courier,BoldItalic", "Courier-BoldOblique" },
60 { "Courier,Italic", "Courier-Oblique" },
61 { "CourierNew", "Courier" },
62 { "CourierNew,Bold", "Courier-Bold" },
63 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
64 { "CourierNew,Italic", "Courier-Oblique" },
65 { "CourierNew-Bold", "Courier-Bold" },
66 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
67 { "CourierNew-Italic", "Courier-Oblique" },
68 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
69 { "CourierNewPS-BoldMT", "Courier-Bold" },
70 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
71 { "CourierNewPSMT", "Courier" },
72 { "Helvetica,Bold", "Helvetica-Bold" },
73 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
74 { "Helvetica,Italic", "Helvetica-Oblique" },
75 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
76 { "Helvetica-Italic", "Helvetica-Oblique" },
77 { "Symbol,Bold", "Symbol" },
78 { "Symbol,BoldItalic", "Symbol" },
79 { "Symbol,Italic", "Symbol" },
80 { "TimesNewRoman", "Times-Roman" },
81 { "TimesNewRoman,Bold", "Times-Bold" },
82 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
83 { "TimesNewRoman,Italic", "Times-Italic" },
84 { "TimesNewRoman-Bold", "Times-Bold" },
85 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
86 { "TimesNewRoman-Italic", "Times-Italic" },
87 { "TimesNewRomanPS", "Times-Roman" },
88 { "TimesNewRomanPS-Bold", "Times-Bold" },
89 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
90 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
91 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
92 { "TimesNewRomanPS-Italic", "Times-Italic" },
93 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
94 { "TimesNewRomanPSMT", "Times-Roman" },
95 { "TimesNewRomanPSMT,Bold", "Times-Bold" },
96 { "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
97 { "TimesNewRomanPSMT,Italic", "Times-Italic" }
100 //------------------------------------------------------------------------
101 // GfxFont
102 //------------------------------------------------------------------------
104 GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
105 GString *nameA;
106 GfxFont *font;
107 Object obj1;
109 // get base font name
110 nameA = NULL;
111 fontDict->lookup("BaseFont", &obj1);
112 if (obj1.isName()) {
113 nameA = new GString(obj1.getName());
114 } else {
115 char buf[32];
116 sprintf(buf,"Unnamed-%d-%d", idA.gen, idA.num);
117 nameA = new GString(buf);
119 obj1.free();
121 // get font type
122 font = NULL;
123 fontDict->lookup("Subtype", &obj1);
124 if (obj1.isName("Type1") || obj1.isName("MMType1")) {
125 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
126 } else if (obj1.isName("Type1C")) {
127 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
128 } else if (obj1.isName("Type3")) {
129 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
130 } else if (obj1.isName("TrueType")) {
131 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
132 } else if (obj1.isName("Type0")) {
133 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
134 } else {
135 error(-1, "Unknown font type: '%s'",
136 obj1.isName() ? obj1.getName() : "???");
137 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
139 obj1.free();
141 if (font && !font->isOk()) {
142 delete font;
143 font = NULL;
146 return font;
149 GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
150 ok = gFalse;
151 tag = new GString(tagA);
152 id = idA;
153 name = nameA;
154 origName = nameA;
155 embFontName = NULL;
156 extFontFile = NULL;
159 GfxFont::~GfxFont() {
160 delete tag;
161 if (origName && origName != name) {
162 delete origName;
164 if (name) {
165 delete name;
167 if (embFontName) {
168 delete embFontName;
170 if (extFontFile) {
171 delete extFontFile;
175 void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
176 Object obj1, obj2, obj3, obj4;
177 double t;
178 int i;
180 // assume Times-Roman by default (for substitution purposes)
181 flags = fontSerif;
183 embFontID.num = -1;
184 embFontID.gen = -1;
185 missingWidth = 0;
187 if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
189 // get flags
190 if (obj1.dictLookup("Flags", &obj2)->isInt()) {
191 flags = obj2.getInt();
193 obj2.free();
195 // get name
196 obj1.dictLookup("FontName", &obj2);
197 if (obj2.isName()) {
198 embFontName = new GString(obj2.getName());
200 obj2.free();
202 // look for embedded font file
203 if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
204 embFontID = obj2.getRef();
205 if (type != fontType1) {
206 error(-1, "Mismatch between font type and embedded font file");
207 type = fontType1;
210 obj2.free();
211 if (embFontID.num == -1 &&
212 obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
213 embFontID = obj2.getRef();
214 if (type != fontTrueType && type != fontCIDType2) {
215 error(-1, "Mismatch between font type and embedded font file");
216 type = type == fontCIDType0 ? fontCIDType2 : fontTrueType;
219 obj2.free();
220 if (embFontID.num == -1 &&
221 obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
222 if (obj2.fetch(xref, &obj3)->isStream()) {
223 obj3.streamGetDict()->lookup("Subtype", &obj4);
224 if (obj4.isName("Type1")) {
225 embFontID = obj2.getRef();
226 if (type != fontType1) {
227 error(-1, "Mismatch between font type and embedded font file");
228 type = fontType1;
230 } else if (obj4.isName("Type1C")) {
231 embFontID = obj2.getRef();
232 if (type != fontType1 && type != fontType1C) {
233 error(-1, "Mismatch between font type and embedded font file");
235 type = fontType1C;
236 } else if (obj4.isName("TrueType")) {
237 embFontID = obj2.getRef();
238 if (type != fontTrueType) {
239 error(-1, "Mismatch between font type and embedded font file");
240 type = fontTrueType;
242 } else if (obj4.isName("CIDFontType0C")) {
243 embFontID = obj2.getRef();
244 if (type != fontCIDType0) {
245 error(-1, "Mismatch between font type and embedded font file");
247 type = fontCIDType0C;
248 } else {
249 error(-1, "Unknown embedded font type '%s'",
250 obj4.isName() ? obj4.getName() : "???");
252 obj4.free();
254 obj3.free();
256 obj2.free();
258 // look for MissingWidth
259 obj1.dictLookup("MissingWidth", &obj2);
260 if (obj2.isNum()) {
261 missingWidth = obj2.getNum();
263 obj2.free();
265 // get Ascent and Descent
266 obj1.dictLookup("Ascent", &obj2);
267 if (obj2.isNum()) {
268 t = 0.001 * obj2.getNum();
269 // some broken font descriptors set ascent and descent to 0
270 if (t != 0) {
271 ascent = t;
274 obj2.free();
275 obj1.dictLookup("Descent", &obj2);
276 if (obj2.isNum()) {
277 t = 0.001 * obj2.getNum();
278 // some broken font descriptors set ascent and descent to 0
279 if (t != 0) {
280 descent = t;
282 // some broken font descriptors specify a positive descent
283 if (descent > 0) {
284 descent = -descent;
287 obj2.free();
289 // font FontBBox
290 if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
291 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
292 if (obj2.arrayGet(i, &obj3)->isNum()) {
293 fontBBox[i] = 0.001 * obj3.getNum();
295 obj3.free();
298 obj2.free();
301 obj1.free();
304 CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
305 CharCodeToUnicode *ctu) {
306 GString *buf;
307 Object obj1;
308 int c;
310 if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
311 obj1.free();
312 return NULL;
314 buf = new GString();
315 obj1.streamReset();
316 while ((c = obj1.streamGetChar()) != EOF) {
317 buf->append(c);
319 obj1.streamClose();
320 obj1.free();
321 if (ctu) {
322 ctu->mergeCMap(buf, nBits);
323 } else {
324 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
326 delete buf;
327 return ctu;
330 void GfxFont::findExtFontFile() {
331 static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
332 static char *ttExts[] = { ".ttf", NULL };
334 if (name) {
335 if (type == fontType1) {
336 extFontFile = globalParams->findFontFile(name, type1Exts);
337 } else if (type == fontTrueType) {
338 extFontFile = globalParams->findFontFile(name, ttExts);
343 char *GfxFont::readExtFontFile(int *len) {
344 myFILE *f;
345 char *buf;
347 if (!(f = myfopen(extFontFile->getCString(), "rb"))) {
348 error(-1, "External font file '%s' vanished", extFontFile->getCString());
349 return NULL;
351 myfseek(f, 0, SEEK_END);
352 *len = (int)myftell(f);
353 myfseek(f, 0, SEEK_SET);
354 buf = (char *)gmalloc(*len);
355 if ((int)myfread(buf, 1, *len, f) != *len) {
356 error(-1, "Error reading external font file '%s'",
357 extFontFile->getCString());
359 myfclose(f);
360 return buf;
363 char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
364 char *buf;
365 Object obj1, obj2;
366 Stream *str;
367 int c;
368 int size, i;
370 obj1.initRef(embFontID.num, embFontID.gen);
371 obj1.fetch(xref, &obj2);
372 if (!obj2.isStream()) {
373 error(-1, "Embedded font file is not a stream");
374 obj2.free();
375 obj1.free();
376 embFontID.num = -1;
377 return NULL;
379 str = obj2.getStream();
381 buf = NULL;
382 i = size = 0;
383 str->reset();
384 while ((c = str->getChar()) != EOF) {
385 if (i == size) {
386 size += 4096;
387 buf = (char *)grealloc(buf, size);
389 buf[i++] = c;
391 *len = i;
392 str->close();
394 obj2.free();
395 obj1.free();
397 return buf;
400 //------------------------------------------------------------------------
401 // Gfx8BitFont
402 //------------------------------------------------------------------------
404 Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
405 GfxFontType typeA, Dict *fontDict):
406 GfxFont(tagA, idA, nameA)
408 GString *name2;
409 BuiltinFont *builtinFont;
410 char **baseEnc;
411 GBool baseEncFromFontFile;
412 char *buf;
413 int len;
414 FoFiType1 *ffT1;
415 FoFiType1C *ffT1C;
416 int code, code2;
417 char *charName;
418 GBool missing, hex;
419 Unicode toUnicode[256];
420 CharCodeToUnicode *utu, *ctu2;
421 Unicode uBuf[8];
422 double mul;
423 int firstChar, lastChar;
424 Gushort w;
425 Object obj1, obj2, obj3;
426 int n, i, a, b, m;
428 type = typeA;
429 ctu = NULL;
431 // do font name substitution for various aliases of the Base 14 font
432 // names
433 if (name) {
434 name2 = name->copy();
435 i = 0;
436 while (i < name2->getLength()) {
437 if (name2->getChar(i) == ' ') {
438 name2->del(i);
439 } else {
440 ++i;
443 a = 0;
444 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
445 // invariant: stdFontMap[a].altName <= name2 < stdFontMap[b].altName
446 while (b - a > 1) {
447 m = (a + b) / 2;
448 if (name2->cmp(stdFontMap[m].altName) >= 0) {
449 a = m;
450 } else {
451 b = m;
454 if (!name2->cmp(stdFontMap[a].altName)) {
455 name = new GString(stdFontMap[a].properName);
457 delete name2;
460 // is it a built-in font?
461 builtinFont = NULL;
462 if (name) {
463 for (i = 0; i < nBuiltinFonts; ++i) {
464 if (!name->cmp(builtinFonts[i].name)) {
465 builtinFont = &builtinFonts[i];
466 break;
471 // default ascent/descent values
472 if (builtinFont) {
473 ascent = 0.001 * builtinFont->ascent;
474 descent = 0.001 * builtinFont->descent;
475 fontBBox[0] = 0.001 * builtinFont->bbox[0];
476 fontBBox[1] = 0.001 * builtinFont->bbox[1];
477 fontBBox[2] = 0.001 * builtinFont->bbox[2];
478 fontBBox[3] = 0.001 * builtinFont->bbox[3];
479 } else {
480 ascent = 0.95;
481 descent = -0.35;
482 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
485 // get info from font descriptor
486 readFontDescriptor(xref, fontDict);
488 // for non-embedded fonts, don't trust the ascent/descent/bbox
489 // values from the font descriptor
490 if (builtinFont && embFontID.num < 0) {
491 ascent = 0.001 * builtinFont->ascent;
492 descent = 0.001 * builtinFont->descent;
493 fontBBox[0] = 0.001 * builtinFont->bbox[0];
494 fontBBox[1] = 0.001 * builtinFont->bbox[1];
495 fontBBox[2] = 0.001 * builtinFont->bbox[2];
496 fontBBox[3] = 0.001 * builtinFont->bbox[3];
499 // look for an external font file
500 findExtFontFile();
502 // get font matrix
503 fontMat[0] = fontMat[3] = 1;
504 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
505 if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
506 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
507 if (obj1.arrayGet(i, &obj2)->isNum()) {
508 fontMat[i] = obj2.getNum();
510 obj2.free();
513 obj1.free();
515 // get Type 3 bounding box, font definition, and resources
516 if (type == fontType3) {
517 if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
518 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
519 if (obj1.arrayGet(i, &obj2)->isNum()) {
520 fontBBox[i] = obj2.getNum();
522 obj2.free();
525 obj1.free();
526 if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
527 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
528 charProcs.free();
530 if (!fontDict->lookup("Resources", &resources)->isDict()) {
531 resources.free();
535 //----- build the font encoding -----
537 // Encodings start with a base encoding, which can come from
538 // (in order of priority):
539 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
540 // - MacRoman / MacExpert / WinAnsi / Standard
541 // 2. embedded or external font file
542 // 3. default:
543 // - builtin --> builtin encoding
544 // - TrueType --> WinAnsiEncoding
545 // - others --> StandardEncoding
546 // and then add a list of differences (if any) from
547 // FontDict.Encoding.Differences.
549 // check FontDict for base encoding
550 hasEncoding = gFalse;
551 usesMacRomanEnc = gFalse;
552 baseEnc = NULL;
553 baseEncFromFontFile = gFalse;
554 fontDict->lookup("Encoding", &obj1);
555 if (obj1.isDict()) {
556 obj1.dictLookup("BaseEncoding", &obj2);
557 if (obj2.isName("MacRomanEncoding")) {
558 hasEncoding = gTrue;
559 usesMacRomanEnc = gTrue;
560 baseEnc = macRomanEncoding;
561 } else if (obj2.isName("MacExpertEncoding")) {
562 hasEncoding = gTrue;
563 baseEnc = macExpertEncoding;
564 } else if (obj2.isName("WinAnsiEncoding")) {
565 hasEncoding = gTrue;
566 baseEnc = winAnsiEncoding;
568 obj2.free();
569 } else if (obj1.isName("MacRomanEncoding")) {
570 hasEncoding = gTrue;
571 usesMacRomanEnc = gTrue;
572 baseEnc = macRomanEncoding;
573 } else if (obj1.isName("MacExpertEncoding")) {
574 hasEncoding = gTrue;
575 baseEnc = macExpertEncoding;
576 } else if (obj1.isName("WinAnsiEncoding")) {
577 hasEncoding = gTrue;
578 baseEnc = winAnsiEncoding;
581 // check embedded or external font file for base encoding
582 // (only for Type 1 fonts - trying to get an encoding out of a
583 // TrueType font is a losing proposition)
584 ffT1 = NULL;
585 ffT1C = NULL;
586 buf = NULL;
587 if (type == fontType1 && (extFontFile || embFontID.num >= 0)) {
588 if (extFontFile) {
589 ffT1 = FoFiType1::load(extFontFile->getCString());
590 } else {
591 buf = readEmbFontFile(xref, &len);
592 ffT1 = FoFiType1::make(buf, len);
594 if (ffT1) {
595 if (ffT1->getName()) {
596 if (embFontName) {
597 delete embFontName;
599 embFontName = new GString(ffT1->getName());
601 if (!baseEnc) {
602 baseEnc = ffT1->getEncoding();
603 baseEncFromFontFile = gTrue;
606 } else if (type == fontType1C && (extFontFile || embFontID.num >= 0)) {
607 if (extFontFile) {
608 ffT1C = FoFiType1C::load(extFontFile->getCString());
609 } else {
610 buf = readEmbFontFile(xref, &len);
611 ffT1C = FoFiType1C::make(buf, len);
613 if (ffT1C) {
614 if (ffT1C->getName()) {
615 if (embFontName) {
616 delete embFontName;
618 embFontName = new GString(ffT1C->getName());
620 if (!baseEnc) {
621 baseEnc = ffT1C->getEncoding();
622 baseEncFromFontFile = gTrue;
626 if (buf) {
627 gfree(buf);
630 // get default base encoding
631 if (!baseEnc) {
632 if (builtinFont && embFontID.num < 0) {
633 baseEnc = builtinFont->defaultBaseEnc;
634 hasEncoding = gTrue;
635 } else if (type == fontTrueType) {
636 baseEnc = winAnsiEncoding;
637 } else {
638 baseEnc = standardEncoding;
642 // copy the base encoding
643 for (i = 0; i < 256; ++i) {
644 enc[i] = baseEnc[i];
645 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
646 enc[i] = copyString(baseEnc[i]);
650 // some Type 1C font files have empty encodings, which can break the
651 // T1C->T1 conversion (since the 'seac' operator depends on having
652 // the accents in the encoding), so we fill in any gaps from
653 // StandardEncoding
654 if (type == fontType1C && (extFontFile || embFontID.num >= 0) &&
655 baseEncFromFontFile) {
656 for (i = 0; i < 256; ++i) {
657 if (!enc[i] && standardEncoding[i]) {
658 enc[i] = standardEncoding[i];
659 encFree[i] = gFalse;
664 // merge differences into encoding
665 if (obj1.isDict()) {
666 obj1.dictLookup("Differences", &obj2);
667 if (obj2.isArray()) {
668 hasEncoding = gTrue;
669 code = 0;
670 for (i = 0; i < obj2.arrayGetLength(); ++i) {
671 obj2.arrayGet(i, &obj3);
672 if (obj3.isInt()) {
673 code = obj3.getInt();
674 } else if (obj3.isName()) {
675 if (code >= 0 && code < 256) {
676 if (encFree[code]) {
677 gfree(enc[code]);
679 enc[code] = copyString(obj3.getName());
680 encFree[code] = gTrue;
682 ++code;
683 } else {
684 error(-1, "Wrong type in font encoding resource differences (%s)",
685 obj3.getTypeName());
687 obj3.free();
690 obj2.free();
692 obj1.free();
693 if (ffT1) {
694 delete ffT1;
696 if (ffT1C) {
697 delete ffT1C;
700 //----- build the mapping to Unicode -----
702 // pass 1: use the name-to-Unicode mapping table
703 missing = hex = gFalse;
704 for (code = 0; code < 256; ++code) {
705 if ((charName = enc[code])) {
706 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
707 strcmp(charName, ".notdef")) {
708 // if it wasn't in the name-to-Unicode table, check for a
709 // name that looks like 'Axx' or 'xx', where 'A' is any letter
710 // and 'xx' is two hex digits
711 if ((strlen(charName) == 3 &&
712 isalpha(charName[0]) &&
713 isxdigit(charName[1]) && isxdigit(charName[2]) &&
714 ((charName[1] >= 'a' && charName[1] <= 'f') ||
715 (charName[1] >= 'A' && charName[1] <= 'F') ||
716 (charName[2] >= 'a' && charName[2] <= 'f') ||
717 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
718 (strlen(charName) == 2 &&
719 isxdigit(charName[0]) && isxdigit(charName[1]) &&
720 ((charName[0] >= 'a' && charName[0] <= 'f') ||
721 (charName[0] >= 'A' && charName[0] <= 'F') ||
722 (charName[1] >= 'a' && charName[1] <= 'f') ||
723 (charName[1] >= 'A' && charName[1] <= 'F')))) {
724 hex = gTrue;
726 missing = gTrue;
728 } else {
729 toUnicode[code] = 0;
733 // pass 2: try to fill in the missing chars, looking for names of
734 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
735 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
736 // decimal digits
737 if (missing && globalParams->getMapNumericCharNames()) {
738 for (code = 0; code < 256; ++code) {
739 if ((charName = enc[code]) && !toUnicode[code] &&
740 strcmp(charName, ".notdef")) {
741 n = strlen(charName);
742 code2 = -1;
743 if (hex && n == 3 && isalpha(charName[0]) &&
744 isxdigit(charName[1]) && isxdigit(charName[2])) {
745 sscanf(charName+1, "%x", &code2);
746 } else if (hex && n == 2 &&
747 isxdigit(charName[0]) && isxdigit(charName[1])) {
748 sscanf(charName, "%x", &code2);
749 } else if (!hex && n >= 2 && n <= 4 &&
750 isdigit(charName[0]) && isdigit(charName[1])) {
751 code2 = atoi(charName);
752 } else if (n >= 3 && n <= 5 &&
753 isdigit(charName[1]) && isdigit(charName[2])) {
754 code2 = atoi(charName+1);
755 } else if (n >= 4 && n <= 6 &&
756 isdigit(charName[2]) && isdigit(charName[3])) {
757 code2 = atoi(charName+2);
759 if (code2 >= 0 && code2 <= 0xff) {
760 toUnicode[code] = (Unicode)code2;
766 // construct the char code -> Unicode mapping object
767 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
769 // merge in a ToUnicode CMap, if there is one -- this overwrites
770 // existing entries in ctu, i.e., the ToUnicode CMap takes
771 // precedence, but the other encoding info is allowed to fill in any
772 // holes
773 readToUnicodeCMap(fontDict, 8, ctu);
775 // look for a Unicode-to-Unicode mapping
776 if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
777 for (i = 0; i < 256; ++i) {
778 toUnicode[i] = 0;
780 ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
781 for (i = 0; i < 256; ++i) {
782 n = ctu->mapToUnicode((CharCode)i, uBuf, 8);
783 if (n >= 1) {
784 n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
785 if (n >= 1) {
786 ctu2->setMapping((CharCode)i, uBuf, n);
790 utu->decRefCnt();
791 delete ctu;
792 ctu = ctu2;
795 //----- get the character widths -----
797 // initialize all widths
798 for (code = 0; code < 256; ++code) {
799 widths[code] = missingWidth * 0.001;
802 // use widths from font dict, if present
803 fontDict->lookup("FirstChar", &obj1);
804 firstChar = obj1.isInt() ? obj1.getInt() : 0;
805 obj1.free();
806 if (firstChar < 0 || firstChar > 255) {
807 firstChar = 0;
809 fontDict->lookup("LastChar", &obj1);
810 lastChar = obj1.isInt() ? obj1.getInt() : 255;
811 obj1.free();
812 if (lastChar < 0 || lastChar > 255) {
813 lastChar = 255;
815 mul = (type == fontType3) ? fontMat[0] : 0.001;
816 fontDict->lookup("Widths", &obj1);
817 if (obj1.isArray()) {
818 flags |= fontFixedWidth;
819 if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
820 lastChar = firstChar + obj1.arrayGetLength() - 1;
822 for (code = firstChar; code <= lastChar; ++code) {
823 obj1.arrayGet(code - firstChar, &obj2);
824 if (obj2.isNum()) {
825 widths[code] = obj2.getNum() * mul;
826 if (widths[code] != widths[firstChar]) {
827 flags &= ~fontFixedWidth;
830 obj2.free();
833 // use widths from built-in font
834 } else if (builtinFont) {
835 // this is a kludge for broken PDF files that encode char 32
836 // as .notdef
837 if (builtinFont->widths->getWidth("space", &w)) {
838 widths[32] = 0.001 * w;
840 for (code = 0; code < 256; ++code) {
841 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
842 widths[code] = 0.001 * w;
846 // couldn't find widths -- use defaults
847 } else {
848 // this is technically an error -- the Widths entry is required
849 // for all but the Base-14 fonts -- but certain PDF generators
850 // apparently don't include widths for Arial and TimesNewRoman
851 if (isFixedWidth()) {
852 i = 0;
853 } else if (isSerif()) {
854 i = 8;
855 } else {
856 i = 4;
858 if (isBold()) {
859 i += 2;
861 if (isItalic()) {
862 i += 1;
864 builtinFont = builtinFontSubst[i];
865 // this is a kludge for broken PDF files that encode char 32
866 // as .notdef
867 if (builtinFont->widths->getWidth("space", &w)) {
868 widths[32] = 0.001 * w;
870 for (code = 0; code < 256; ++code) {
871 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
872 widths[code] = 0.001 * w;
876 obj1.free();
878 ok = gTrue;
881 Gfx8BitFont::~Gfx8BitFont() {
882 int i;
884 for (i = 0; i < 256; ++i) {
885 if (encFree[i] && enc[i]) {
886 gfree(enc[i]);
889 ctu->decRefCnt();
890 if (charProcs.isDict()) {
891 charProcs.free();
893 if (resources.isDict()) {
894 resources.free();
898 int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
899 Unicode *u, int uSize, int *uLen,
900 double *dx, double *dy, double *ox, double *oy) {
901 CharCode c;
903 *code = c = (CharCode)(*s & 0xff);
904 *uLen = ctu->mapToUnicode(c, u, uSize);
905 *dx = widths[c];
906 *dy = *ox = *oy = 0;
907 return 1;
910 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
911 ctu->incRefCnt();
912 return ctu;
915 Gushort *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
916 Gushort *map;
917 int cmapPlatform, cmapEncoding;
918 int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
919 GBool useMacRoman, useUnicode;
920 char *charName;
921 Unicode u;
922 int code, i, n;
924 map = (Gushort *)gmallocn(256, sizeof(Gushort));
925 for (i = 0; i < 256; ++i) {
926 map[i] = 0;
929 // To match up with the Adobe-defined behaviour, we choose a cmap
930 // like this:
931 // 1. If the PDF font has an encoding:
932 // 1a. If the PDF font specified MacRomanEncoding and the
933 // TrueType font has a Macintosh Roman cmap, use it, and
934 // reverse map the char names through MacRomanEncoding to
935 // get char codes.
936 // 1b. If the TrueType font has a Microsoft Unicode cmap or a
937 // non-Microsoft Unicode cmap, use it, and use the Unicode
938 // indexes, not the char codes.
939 // 1c. If the PDF font is symbolic and the TrueType font has a
940 // Microsoft Symbol cmap, use it, and use char codes
941 // directly (possibly with an offset of 0xf000).
942 // 1d. If the TrueType font has a Macintosh Roman cmap, use it,
943 // as in case 1a.
944 // 2. If the PDF font does not have an encoding or the PDF font is
945 // symbolic:
946 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
947 // and use char codes directly (possibly with an offset of
948 // 0xf000).
949 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
950 // and use char codes directly (possible with an offset of
951 // 0xf000).
952 // 3. If none of these rules apply, use the first cmap and hope for
953 // the best (this shouldn't happen).
954 unicodeCmap = macRomanCmap = msSymbolCmap = -1;
955 for (i = 0; i < ff->getNumCmaps(); ++i) {
956 cmapPlatform = ff->getCmapPlatform(i);
957 cmapEncoding = ff->getCmapEncoding(i);
958 if ((cmapPlatform == 3 && cmapEncoding == 1) ||
959 cmapPlatform == 0) {
960 unicodeCmap = i;
961 } else if (cmapPlatform == 1 && cmapEncoding == 0) {
962 macRomanCmap = i;
963 } else if (cmapPlatform == 3 && cmapEncoding == 0) {
964 msSymbolCmap = i;
967 cmap = 0;
968 useMacRoman = gFalse;
969 useUnicode = gFalse;
970 if (hasEncoding) {
971 if (usesMacRomanEnc && macRomanCmap >= 0) {
972 cmap = macRomanCmap;
973 useMacRoman = gTrue;
974 } else if (unicodeCmap >= 0) {
975 cmap = unicodeCmap;
976 useUnicode = gTrue;
977 } else if ((flags & fontSymbolic) && msSymbolCmap >= 0) {
978 cmap = msSymbolCmap;
979 } else if ((flags & fontSymbolic) && macRomanCmap >= 0) {
980 cmap = macRomanCmap;
981 } else if (macRomanCmap >= 0) {
982 cmap = macRomanCmap;
983 useMacRoman = gTrue;
985 } else {
986 if (macRomanCmap >= 0) {
987 cmap = macRomanCmap;
988 } else if (msSymbolCmap >= 0) {
989 cmap = msSymbolCmap;
993 // reverse map the char names through MacRomanEncoding, then map the
994 // char codes through the cmap
995 if (useMacRoman) {
996 for (i = 0; i < 256; ++i) {
997 if ((charName = enc[i])) {
998 if ((code = globalParams->getMacRomanCharCode(charName))) {
999 map[i] = ff->mapCodeToGID(cmap, code);
1004 // map Unicode through the cmap
1005 } else if (useUnicode) {
1006 for (i = 0; i < 256; ++i) {
1007 if (((charName = enc[i]) &&
1008 (u = globalParams->mapNameToUnicode(charName))) ||
1009 (n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
1010 map[i] = ff->mapCodeToGID(cmap, u);
1014 // map the char codes through the cmap, possibly with an offset of
1015 // 0xf000
1016 } else {
1017 for (i = 0; i < 256; ++i) {
1018 if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
1019 map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
1024 // try the TrueType 'post' table to handle any unmapped characters
1025 for (i = 0; i < 256; ++i) {
1026 if (!map[i] && (charName = enc[i])) {
1027 map[i] = (Gushort)(int)ff->mapNameToGID(charName);
1031 return map;
1034 Dict *Gfx8BitFont::getCharProcs() {
1035 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
1038 Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
1039 if (enc[code] && charProcs.isDict()) {
1040 charProcs.dictLookup(enc[code], proc);
1041 } else {
1042 proc->initNull();
1044 return proc;
1047 Dict *Gfx8BitFont::getResources() {
1048 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1051 //------------------------------------------------------------------------
1052 // GfxCIDFont
1053 //------------------------------------------------------------------------
1055 static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
1056 return ((GfxFontCIDWidthExcep *)w1)->first -
1057 ((GfxFontCIDWidthExcep *)w2)->first;
1060 static int CDECL cmpWidthExcepV(const void *w1, const void *w2) {
1061 return ((GfxFontCIDWidthExcepV *)w1)->first -
1062 ((GfxFontCIDWidthExcepV *)w2)->first;
1065 GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
1066 Dict *fontDict):
1067 GfxFont(tagA, idA, nameA)
1069 Dict *desFontDict;
1070 GString *collection, *cMapName;
1071 Object desFontDictObj;
1072 Object obj1, obj2, obj3, obj4, obj5, obj6;
1073 CharCodeToUnicode *utu;
1074 CharCode c;
1075 Unicode uBuf[8];
1076 int c1, c2;
1077 int excepsSize, i, j, k, n;
1079 ascent = 0.95;
1080 descent = -0.35;
1081 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1082 cMap = NULL;
1083 ctu = NULL;
1084 widths.defWidth = 1.0;
1085 widths.defHeight = -1.0;
1086 widths.defVY = 0.880;
1087 widths.exceps = NULL;
1088 widths.nExceps = 0;
1089 widths.excepsV = NULL;
1090 widths.nExcepsV = 0;
1091 cidToGID = NULL;
1092 cidToGIDLen = 0;
1094 // get the descendant font
1095 if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
1096 error(-1, "Missing DescendantFonts entry in Type 0 font");
1097 obj1.free();
1098 goto err1;
1100 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1101 error(-1, "Bad descendant font in Type 0 font");
1102 goto err3;
1104 obj1.free();
1105 desFontDict = desFontDictObj.getDict();
1107 // font type
1108 if (!desFontDict->lookup("Subtype", &obj1)) {
1109 error(-1, "Missing Subtype entry in Type 0 descendant font");
1110 goto err3;
1112 if (obj1.isName("CIDFontType0")) {
1113 type = fontCIDType0;
1114 } else if (obj1.isName("CIDFontType2")) {
1115 type = fontCIDType2;
1116 } else {
1117 error(-1, "Unknown Type 0 descendant font type '%s'",
1118 obj1.isName() ? obj1.getName() : "???");
1119 goto err3;
1121 obj1.free();
1123 // get info from font descriptor
1124 readFontDescriptor(xref, desFontDict);
1126 // look for an external font file
1127 findExtFontFile();
1129 //----- encoding info -----
1131 // char collection
1132 if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1133 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1134 goto err3;
1136 obj1.dictLookup("Registry", &obj2);
1137 obj1.dictLookup("Ordering", &obj3);
1138 if (!obj2.isString() || !obj3.isString()) {
1139 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1140 goto err4;
1142 collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1143 obj3.free();
1144 obj2.free();
1145 obj1.free();
1147 // look for a ToUnicode CMap
1148 if (!(ctu = readToUnicodeCMap(fontDict, 16, NULL))) {
1150 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
1151 // cidToUnicode files
1152 if (collection->cmp("Adobe-Identity") &&
1153 collection->cmp("Adobe-UCS")) {
1155 // look for a user-supplied .cidToUnicode file
1156 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
1157 error(-1, "Unknown character collection '%s'",
1158 collection->getCString());
1159 // fall-through, assuming the Identity mapping -- this appears
1160 // to match Adobe's behavior
1165 // look for a Unicode-to-Unicode mapping
1166 if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
1167 if (ctu) {
1168 for (c = 0; c < ctu->getLength(); ++c) {
1169 n = ctu->mapToUnicode(c, uBuf, 8);
1170 if (n >= 1) {
1171 n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
1172 if (n >= 1) {
1173 ctu->setMapping(c, uBuf, n);
1177 utu->decRefCnt();
1178 } else {
1179 ctu = utu;
1183 // encoding (i.e., CMap)
1184 //~ need to handle a CMap stream here
1185 //~ also need to deal with the UseCMap entry in the stream dict
1186 if (!fontDict->lookup("Encoding", &obj1)->isName()) {
1187 error(-1, "Missing or invalid Encoding entry in Type 0 font");
1188 delete collection;
1189 goto err3;
1191 cMapName = new GString(obj1.getName());
1192 obj1.free();
1193 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
1194 error(-1, "Unknown CMap '%s' for character collection '%s'",
1195 cMapName->getCString(), collection->getCString());
1196 delete collection;
1197 delete cMapName;
1198 goto err2;
1200 delete collection;
1201 delete cMapName;
1203 // CIDToGIDMap (for embedded TrueType fonts)
1204 if (type == fontCIDType2) {
1205 desFontDict->lookup("CIDToGIDMap", &obj1);
1206 if (obj1.isStream()) {
1207 cidToGIDLen = 0;
1208 i = 64;
1209 cidToGID = (Gushort *)gmallocn(i, sizeof(Gushort));
1210 obj1.streamReset();
1211 while ((c1 = obj1.streamGetChar()) != EOF &&
1212 (c2 = obj1.streamGetChar()) != EOF) {
1213 if (cidToGIDLen == i) {
1214 i *= 2;
1215 cidToGID = (Gushort *)greallocn(cidToGID, i, sizeof(Gushort));
1217 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
1219 } else if (!obj1.isName("Identity") && !obj1.isNull()) {
1220 error(-1, "Invalid CIDToGIDMap entry in CID font");
1222 obj1.free();
1225 //----- character metrics -----
1227 // default char width
1228 if (desFontDict->lookup("DW", &obj1)->isInt()) {
1229 widths.defWidth = obj1.getInt() * 0.001;
1231 obj1.free();
1233 // char width exceptions
1234 if (desFontDict->lookup("W", &obj1)->isArray()) {
1235 excepsSize = 0;
1236 i = 0;
1237 while (i + 1 < obj1.arrayGetLength()) {
1238 obj1.arrayGet(i, &obj2);
1239 obj1.arrayGet(i + 1, &obj3);
1240 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1241 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1242 if (widths.nExceps == excepsSize) {
1243 excepsSize += 16;
1244 widths.exceps = (GfxFontCIDWidthExcep *)
1245 greallocn(widths.exceps,
1246 excepsSize, sizeof(GfxFontCIDWidthExcep));
1248 widths.exceps[widths.nExceps].first = obj2.getInt();
1249 widths.exceps[widths.nExceps].last = obj3.getInt();
1250 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1251 ++widths.nExceps;
1252 } else {
1253 error(-1, "Bad widths array in Type 0 font");
1255 obj4.free();
1256 i += 3;
1257 } else if (obj2.isInt() && obj3.isArray()) {
1258 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1259 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1260 widths.exceps = (GfxFontCIDWidthExcep *)
1261 greallocn(widths.exceps,
1262 excepsSize, sizeof(GfxFontCIDWidthExcep));
1264 j = obj2.getInt();
1265 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1266 if (obj3.arrayGet(k, &obj4)->isNum()) {
1267 widths.exceps[widths.nExceps].first = j;
1268 widths.exceps[widths.nExceps].last = j;
1269 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1270 ++j;
1271 ++widths.nExceps;
1272 } else {
1273 error(-1, "Bad widths array in Type 0 font");
1275 obj4.free();
1277 i += 2;
1278 } else {
1279 error(-1, "Bad widths array in Type 0 font");
1280 ++i;
1282 obj3.free();
1283 obj2.free();
1285 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1286 &cmpWidthExcep);
1288 obj1.free();
1290 // default metrics for vertical font
1291 if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1292 obj1.arrayGetLength() == 2) {
1293 if (obj1.arrayGet(0, &obj2)->isNum()) {
1294 widths.defVY = obj2.getNum() * 0.001;
1296 obj2.free();
1297 if (obj1.arrayGet(1, &obj2)->isNum()) {
1298 widths.defHeight = obj2.getNum() * 0.001;
1300 obj2.free();
1302 obj1.free();
1304 // char metric exceptions for vertical font
1305 if (desFontDict->lookup("W2", &obj1)->isArray()) {
1306 excepsSize = 0;
1307 i = 0;
1308 while (i + 1 < obj1.arrayGetLength()) {
1309 obj1.arrayGet(i, &obj2);
1310 obj1.arrayGet(i+ 1, &obj3);
1311 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1312 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1313 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1314 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1315 if (widths.nExcepsV == excepsSize) {
1316 excepsSize += 16;
1317 widths.excepsV = (GfxFontCIDWidthExcepV *)
1318 greallocn(widths.excepsV,
1319 excepsSize, sizeof(GfxFontCIDWidthExcepV));
1321 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1322 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1323 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1324 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1325 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1326 ++widths.nExcepsV;
1327 } else {
1328 error(-1, "Bad widths (W2) array in Type 0 font");
1330 obj6.free();
1331 obj5.free();
1332 obj4.free();
1333 i += 5;
1334 } else if (obj2.isInt() && obj3.isArray()) {
1335 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1336 excepsSize =
1337 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1338 widths.excepsV = (GfxFontCIDWidthExcepV *)
1339 greallocn(widths.excepsV,
1340 excepsSize, sizeof(GfxFontCIDWidthExcepV));
1342 j = obj2.getInt();
1343 for (k = 0; k < obj3.arrayGetLength(); k += 3) {
1344 if (obj3.arrayGet(k, &obj4)->isNum() &&
1345 obj3.arrayGet(k+1, &obj5)->isNum() &&
1346 obj3.arrayGet(k+2, &obj6)->isNum()) {
1347 widths.excepsV[widths.nExceps].first = j;
1348 widths.excepsV[widths.nExceps].last = j;
1349 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1350 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1351 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1352 ++j;
1353 ++widths.nExcepsV;
1354 } else {
1355 error(-1, "Bad widths (W2) array in Type 0 font");
1357 obj6.free();
1358 obj5.free();
1359 obj4.free();
1361 i += 2;
1362 } else {
1363 error(-1, "Bad widths (W2) array in Type 0 font");
1364 ++i;
1366 obj3.free();
1367 obj2.free();
1369 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1370 &cmpWidthExcepV);
1372 obj1.free();
1374 desFontDictObj.free();
1375 ok = gTrue;
1376 return;
1378 err4:
1379 obj3.free();
1380 obj2.free();
1381 err3:
1382 obj1.free();
1383 err2:
1384 desFontDictObj.free();
1385 err1:;
1388 GfxCIDFont::~GfxCIDFont() {
1389 if (cMap) {
1390 cMap->decRefCnt();
1392 if (ctu) {
1393 ctu->decRefCnt();
1395 gfree(widths.exceps);
1396 gfree(widths.excepsV);
1397 if (cidToGID) {
1398 gfree(cidToGID);
1402 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1403 Unicode *u, int uSize, int *uLen,
1404 double *dx, double *dy, double *ox, double *oy) {
1405 CID cid;
1406 double w, h, vx, vy;
1407 int n, a, b, m;
1409 if (!cMap) {
1410 *code = 0;
1411 *uLen = 0;
1412 *dx = *dy = 0;
1413 return 1;
1416 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1417 if (ctu) {
1418 *uLen = ctu->mapToUnicode(cid, u, uSize);
1419 } else {
1420 *uLen = 0;
1423 // horizontal
1424 if (cMap->getWMode() == 0) {
1425 w = widths.defWidth;
1426 h = vx = vy = 0;
1427 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1428 a = 0;
1429 b = widths.nExceps;
1430 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1431 while (b - a > 1) {
1432 m = (a + b) / 2;
1433 if (widths.exceps[m].first <= cid) {
1434 a = m;
1435 } else {
1436 b = m;
1439 if (cid <= widths.exceps[a].last) {
1440 w = widths.exceps[a].width;
1444 // vertical
1445 } else {
1446 w = 0;
1447 h = widths.defHeight;
1448 vx = widths.defWidth / 2;
1449 vy = widths.defVY;
1450 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1451 a = 0;
1452 b = widths.nExcepsV;
1453 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1454 while (b - a > 1) {
1455 m = (a + b) / 2;
1456 if (widths.excepsV[m].last <= cid) {
1457 a = m;
1458 } else {
1459 b = m;
1462 if (cid <= widths.excepsV[a].last) {
1463 h = widths.excepsV[a].height;
1464 vx = widths.excepsV[a].vx;
1465 vy = widths.excepsV[a].vy;
1470 *dx = w;
1471 *dy = h;
1472 *ox = vx;
1473 *oy = vy;
1475 return n;
1478 int GfxCIDFont::getWMode() {
1479 return cMap ? cMap->getWMode() : 0;
1482 CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1483 if (ctu) {
1484 ctu->incRefCnt();
1486 return ctu;
1489 GString *GfxCIDFont::getCollection() {
1490 return cMap ? cMap->getCollection() : (GString *)NULL;
1493 //------------------------------------------------------------------------
1494 // GfxFontDict
1495 //------------------------------------------------------------------------
1497 GfxFontDict::GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict) {
1498 int i;
1499 Object obj1;
1500 Ref r;
1502 numFonts = fontDict->getLength();
1503 fonts = (Entry *)gmallocn(numFonts, sizeof(Entry));
1504 for (i = 0; i < numFonts; ++i) {
1505 fontDict->getValNF(i, &obj1);
1506 fonts[i].tag = new GString(fontDict->getKey(i));
1507 if (obj1.isRef()) {
1508 r = obj1.getRef();
1509 } else {
1510 // no indirect reference for this font, so invent a unique one
1511 // (legal generation numbers are five digits, so any 6-digit
1512 // number would be safe)
1513 r.num = i;
1514 if (fontDictRef) {
1515 r.gen = 100000 + fontDictRef->num;
1516 } else {
1517 r.gen = 999999;
1520 fonts[i].gfxFont = gfxFontCache->getGfxFont(xref, fontDict->getKey(i), r);
1521 obj1.free();
1525 GfxFontDict::~GfxFontDict() {
1526 int i;
1528 for (i = 0; i < numFonts; ++i) {
1529 gfxFontCache->release(fonts[i].gfxFont);
1530 delete fonts[i].tag;
1532 gfree(fonts);
1535 GfxFont *GfxFontDict::lookup(char *tag) {
1536 int i;
1538 for (i = 0; i < numFonts; ++i) {
1539 if (fonts[i].tag->cmp(tag) == 0) {
1540 return fonts[i].gfxFont;
1543 return NULL;