upgrade to xpdf 3.00.
[swftools.git] / pdf2swf / xpdf / GfxFont.cc
blobed9f076d99105ace3aea48dfcf490ee031c115d6
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"
33 //------------------------------------------------------------------------
35 struct StdFontMapEntry {
36 char *altName;
37 char *properName;
40 // Acrobat 4.0 and earlier substituted Base14-compatible fonts without
41 // providing Widths and a FontDescriptor, so we munge the names into
42 // the proper Base14 names. This table is from implementation note 44
43 // in the PDF 1.4 spec, with some additions based on empirical
44 // evidence.
45 static StdFontMapEntry stdFontMap[] = {
46 { "Arial", "Helvetica" },
47 { "Arial,Bold", "Helvetica-Bold" },
48 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
49 { "Arial,Italic", "Helvetica-Oblique" },
50 { "Arial-Bold", "Helvetica-Bold" },
51 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
52 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
53 { "Arial-BoldMT", "Helvetica-Bold" },
54 { "Arial-Italic", "Helvetica-Oblique" },
55 { "Arial-ItalicMT", "Helvetica-Oblique" },
56 { "ArialMT", "Helvetica" },
57 { "Courier,Bold", "Courier-Bold" },
58 { "Courier,Italic", "Courier-Oblique" },
59 { "Courier,BoldItalic", "Courier-BoldOblique" },
60 { "CourierNew", "Courier" },
61 { "CourierNew,Bold", "Courier-Bold" },
62 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
63 { "CourierNew,Italic", "Courier-Oblique" },
64 { "CourierNew-Bold", "Courier-Bold" },
65 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
66 { "CourierNew-Italic", "Courier-Oblique" },
67 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
68 { "CourierNewPS-BoldMT", "Courier-Bold" },
69 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
70 { "CourierNewPSMT", "Courier" },
71 { "Helvetica,Bold", "Helvetica-Bold" },
72 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
73 { "Helvetica,Italic", "Helvetica-Oblique" },
74 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
75 { "Helvetica-Italic", "Helvetica-Oblique" },
76 { "Symbol,Bold", "Symbol" },
77 { "Symbol,BoldItalic", "Symbol" },
78 { "Symbol,Italic", "Symbol" },
79 { "TimesNewRoman", "Times-Roman" },
80 { "TimesNewRoman,Bold", "Times-Bold" },
81 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
82 { "TimesNewRoman,Italic", "Times-Italic" },
83 { "TimesNewRoman-Bold", "Times-Bold" },
84 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
85 { "TimesNewRoman-Italic", "Times-Italic" },
86 { "TimesNewRomanPS", "Times-Roman" },
87 { "TimesNewRomanPS-Bold", "Times-Bold" },
88 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
89 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
90 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
91 { "TimesNewRomanPS-Italic", "Times-Italic" },
92 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
93 { "TimesNewRomanPSMT", "Times-Roman" },
94 { "TimesNewRomanPSMT,Bold", "Times-Bold" },
95 { "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
96 { "TimesNewRomanPSMT,Italic", "Times-Italic" }
99 //------------------------------------------------------------------------
100 // GfxFont
101 //------------------------------------------------------------------------
103 GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
104 GString *nameA;
105 GfxFont *font;
106 Object obj1;
108 // get base font name
109 nameA = NULL;
110 fontDict->lookup("BaseFont", &obj1);
111 if (obj1.isName()) {
112 nameA = new GString(obj1.getName());
114 obj1.free();
116 // get font type
117 font = NULL;
118 fontDict->lookup("Subtype", &obj1);
119 if (obj1.isName("Type1") || obj1.isName("MMType1")) {
120 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
121 } else if (obj1.isName("Type1C")) {
122 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
123 } else if (obj1.isName("Type3")) {
124 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
125 } else if (obj1.isName("TrueType")) {
126 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
127 } else if (obj1.isName("Type0")) {
128 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
129 } else {
130 error(-1, "Unknown font type: '%s'",
131 obj1.isName() ? obj1.getName() : "???");
132 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
134 obj1.free();
136 return font;
139 GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
140 ok = gFalse;
141 tag = new GString(tagA);
142 id = idA;
143 name = nameA;
144 origName = nameA;
145 embFontName = NULL;
146 extFontFile = NULL;
149 GfxFont::~GfxFont() {
150 delete tag;
151 if (origName && origName != name) {
152 delete origName;
154 if (name) {
155 delete name;
157 if (embFontName) {
158 delete embFontName;
160 if (extFontFile) {
161 delete extFontFile;
165 void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
166 Object obj1, obj2, obj3, obj4;
167 double t;
168 int i;
170 // assume Times-Roman by default (for substitution purposes)
171 flags = fontSerif;
173 embFontID.num = -1;
174 embFontID.gen = -1;
175 missingWidth = 0;
177 if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
179 // get flags
180 if (obj1.dictLookup("Flags", &obj2)->isInt()) {
181 flags = obj2.getInt();
183 obj2.free();
185 // get name
186 obj1.dictLookup("FontName", &obj2);
187 if (obj2.isName()) {
188 embFontName = new GString(obj2.getName());
190 obj2.free();
192 // look for embedded font file
193 if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
194 if (type == fontType1) {
195 embFontID = obj2.getRef();
196 } else {
197 error(-1, "Mismatch between font type and embedded font file");
200 obj2.free();
201 if (embFontID.num == -1 &&
202 obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
203 if (type == fontTrueType || type == fontCIDType2) {
204 embFontID = obj2.getRef();
205 } else {
206 error(-1, "Mismatch between font type and embedded font file");
209 obj2.free();
210 if (embFontID.num == -1 &&
211 obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
212 if (obj2.fetch(xref, &obj3)->isStream()) {
213 obj3.streamGetDict()->lookup("Subtype", &obj4);
214 if (obj4.isName("Type1")) {
215 if (type == fontType1) {
216 embFontID = obj2.getRef();
217 } else {
218 error(-1, "Mismatch between font type and embedded font file");
220 } else if (obj4.isName("Type1C")) {
221 if (type == fontType1) {
222 type = fontType1C;
223 embFontID = obj2.getRef();
224 } else if (type == fontType1C) {
225 embFontID = obj2.getRef();
226 } else {
227 error(-1, "Mismatch between font type and embedded font file");
229 } else if (obj4.isName("TrueType")) {
230 if (type == fontTrueType) {
231 embFontID = obj2.getRef();
232 } else {
233 error(-1, "Mismatch between font type and embedded font file");
235 } else if (obj4.isName("CIDFontType0C")) {
236 if (type == fontCIDType0) {
237 type = fontCIDType0C;
238 embFontID = obj2.getRef();
239 } else {
240 error(-1, "Mismatch between font type and embedded font file");
242 } else {
243 error(-1, "Unknown embedded font type '%s'",
244 obj4.isName() ? obj4.getName() : "???");
246 obj4.free();
248 obj3.free();
250 obj2.free();
252 // look for MissingWidth
253 obj1.dictLookup("MissingWidth", &obj2);
254 if (obj2.isNum()) {
255 missingWidth = obj2.getNum();
257 obj2.free();
259 // get Ascent and Descent
260 obj1.dictLookup("Ascent", &obj2);
261 if (obj2.isNum()) {
262 t = 0.001 * obj2.getNum();
263 // some broken font descriptors set ascent and descent to 0
264 if (t != 0) {
265 ascent = t;
268 obj2.free();
269 obj1.dictLookup("Descent", &obj2);
270 if (obj2.isNum()) {
271 t = 0.001 * obj2.getNum();
272 // some broken font descriptors set ascent and descent to 0
273 if (t != 0) {
274 descent = t;
276 // some broken font descriptors specify a positive descent
277 if (descent > 0) {
278 descent = -descent;
281 obj2.free();
283 // font FontBBox
284 if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
285 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
286 if (obj2.arrayGet(i, &obj3)->isNum()) {
287 fontBBox[i] = 0.001 * obj3.getNum();
289 obj3.free();
292 obj2.free();
295 obj1.free();
298 CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
299 CharCodeToUnicode *ctu) {
300 GString *buf;
301 Object obj1;
302 int c;
304 if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
305 obj1.free();
306 return NULL;
308 buf = new GString();
309 obj1.streamReset();
310 while ((c = obj1.streamGetChar()) != EOF) {
311 buf->append(c);
313 obj1.streamClose();
314 obj1.free();
315 if (ctu) {
316 ctu->mergeCMap(buf, nBits);
317 } else {
318 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
320 delete buf;
321 return ctu;
324 void GfxFont::findExtFontFile() {
325 static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
326 static char *ttExts[] = { ".ttf", NULL };
328 if (name) {
329 if (type == fontType1) {
330 extFontFile = globalParams->findFontFile(name, type1Exts);
331 } else if (type == fontTrueType) {
332 extFontFile = globalParams->findFontFile(name, ttExts);
337 char *GfxFont::readExtFontFile(int *len) {
338 FILE *f;
339 char *buf;
341 if (!(f = fopen(extFontFile->getCString(), "rb"))) {
342 error(-1, "External font file '%s' vanished", extFontFile->getCString());
343 return NULL;
345 fseek(f, 0, SEEK_END);
346 *len = (int)ftell(f);
347 fseek(f, 0, SEEK_SET);
348 buf = (char *)gmalloc(*len);
349 if ((int)fread(buf, 1, *len, f) != *len) {
350 error(-1, "Error reading external font file '%s'",
351 extFontFile->getCString());
353 fclose(f);
354 return buf;
357 char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
358 char *buf;
359 Object obj1, obj2;
360 Stream *str;
361 int c;
362 int size, i;
364 obj1.initRef(embFontID.num, embFontID.gen);
365 obj1.fetch(xref, &obj2);
366 if (!obj2.isStream()) {
367 error(-1, "Embedded font file is not a stream");
368 obj2.free();
369 obj1.free();
370 embFontID.num = -1;
371 return NULL;
373 str = obj2.getStream();
375 buf = NULL;
376 i = size = 0;
377 str->reset();
378 while ((c = str->getChar()) != EOF) {
379 if (i == size) {
380 size += 4096;
381 buf = (char *)grealloc(buf, size);
383 buf[i++] = c;
385 *len = i;
386 str->close();
388 obj2.free();
389 obj1.free();
391 return buf;
394 //------------------------------------------------------------------------
395 // Gfx8BitFont
396 //------------------------------------------------------------------------
398 Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
399 GfxFontType typeA, Dict *fontDict):
400 GfxFont(tagA, idA, nameA)
402 BuiltinFont *builtinFont;
403 char **baseEnc;
404 GBool baseEncFromFontFile;
405 char *buf;
406 int len;
407 FoFiType1 *ffT1;
408 FoFiType1C *ffT1C;
409 int code, code2;
410 char *charName;
411 GBool missing, hex;
412 Unicode toUnicode[256];
413 CharCodeToUnicode *utu, *ctu2;
414 Unicode uBuf[8];
415 double mul;
416 int firstChar, lastChar;
417 Gushort w;
418 Object obj1, obj2, obj3;
419 int n, i, a, b, m;
421 type = typeA;
422 ctu = NULL;
424 // do font name substitution for various aliases of the Base 14 font
425 // names
426 if (name) {
427 a = 0;
428 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
429 // invariant: stdFontMap[a].altName <= name < stdFontMap[b].altName
430 while (b - a > 1) {
431 m = (a + b) / 2;
432 if (name->cmp(stdFontMap[m].altName) >= 0) {
433 a = m;
434 } else {
435 b = m;
438 if (!name->cmp(stdFontMap[a].altName)) {
439 name = new GString(stdFontMap[a].properName);
443 // is it a built-in font?
444 builtinFont = NULL;
445 if (name) {
446 for (i = 0; i < nBuiltinFonts; ++i) {
447 if (!name->cmp(builtinFonts[i].name)) {
448 builtinFont = &builtinFonts[i];
449 break;
454 // default ascent/descent values
455 if (builtinFont) {
456 ascent = 0.001 * builtinFont->ascent;
457 descent = 0.001 * builtinFont->descent;
458 fontBBox[0] = 0.001 * builtinFont->bbox[0];
459 fontBBox[1] = 0.001 * builtinFont->bbox[1];
460 fontBBox[2] = 0.001 * builtinFont->bbox[2];
461 fontBBox[3] = 0.001 * builtinFont->bbox[3];
462 } else {
463 ascent = 0.95;
464 descent = -0.35;
465 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
468 // get info from font descriptor
469 readFontDescriptor(xref, fontDict);
471 // look for an external font file
472 findExtFontFile();
474 // get font matrix
475 fontMat[0] = fontMat[3] = 1;
476 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
477 if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
478 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
479 if (obj1.arrayGet(i, &obj2)->isNum()) {
480 fontMat[i] = obj2.getNum();
482 obj2.free();
485 obj1.free();
487 // get Type 3 bounding box, font definition, and resources
488 if (type == fontType3) {
489 if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
490 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
491 if (obj1.arrayGet(i, &obj2)->isNum()) {
492 fontBBox[i] = obj2.getNum();
494 obj2.free();
497 obj1.free();
498 if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
499 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
500 charProcs.free();
502 if (!fontDict->lookup("Resources", &resources)->isDict()) {
503 resources.free();
507 //----- build the font encoding -----
509 // Encodings start with a base encoding, which can come from
510 // (in order of priority):
511 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
512 // - MacRoman / MacExpert / WinAnsi / Standard
513 // 2. embedded or external font file
514 // 3. default:
515 // - builtin --> builtin encoding
516 // - TrueType --> MacRomanEncoding
517 // - others --> StandardEncoding
518 // and then add a list of differences (if any) from
519 // FontDict.Encoding.Differences.
521 // check FontDict for base encoding
522 hasEncoding = gFalse;
523 usesMacRomanEnc = gFalse;
524 baseEnc = NULL;
525 baseEncFromFontFile = gFalse;
526 fontDict->lookup("Encoding", &obj1);
527 if (obj1.isDict()) {
528 obj1.dictLookup("BaseEncoding", &obj2);
529 if (obj2.isName("MacRomanEncoding")) {
530 hasEncoding = gTrue;
531 usesMacRomanEnc = gTrue;
532 baseEnc = macRomanEncoding;
533 } else if (obj2.isName("MacExpertEncoding")) {
534 hasEncoding = gTrue;
535 baseEnc = macExpertEncoding;
536 } else if (obj2.isName("WinAnsiEncoding")) {
537 hasEncoding = gTrue;
538 baseEnc = winAnsiEncoding;
539 } else if (obj2.isName("StandardEncoding")) {
540 hasEncoding = gTrue;
541 baseEnc = standardEncoding;
543 obj2.free();
544 } else if (obj1.isName("MacRomanEncoding")) {
545 hasEncoding = gTrue;
546 usesMacRomanEnc = gTrue;
547 baseEnc = macRomanEncoding;
548 } else if (obj1.isName("MacExpertEncoding")) {
549 hasEncoding = gTrue;
550 baseEnc = macExpertEncoding;
551 } else if (obj1.isName("WinAnsiEncoding")) {
552 hasEncoding = gTrue;
553 baseEnc = winAnsiEncoding;
554 } else if (obj1.isName("StandardEncoding")) {
555 hasEncoding = gTrue;
556 baseEnc = standardEncoding;
559 // check embedded or external font file for base encoding
560 // (only for Type 1 fonts - trying to get an encoding out of a
561 // TrueType font is a losing proposition)
562 ffT1 = NULL;
563 ffT1C = NULL;
564 buf = NULL;
565 if (type == fontType1 && (extFontFile || embFontID.num >= 0)) {
566 if (extFontFile) {
567 ffT1 = FoFiType1::load(extFontFile->getCString());
568 } else {
569 buf = readEmbFontFile(xref, &len);
570 ffT1 = FoFiType1::make(buf, len);
572 if (ffT1) {
573 if (ffT1->getName()) {
574 if (embFontName) {
575 delete embFontName;
577 embFontName = new GString(ffT1->getName());
579 if (!baseEnc) {
580 baseEnc = ffT1->getEncoding();
581 baseEncFromFontFile = gTrue;
584 } else if (type == fontType1C && (extFontFile || embFontID.num >= 0)) {
585 if (extFontFile) {
586 ffT1C = FoFiType1C::load(extFontFile->getCString());
587 } else {
588 buf = readEmbFontFile(xref, &len);
589 ffT1C = FoFiType1C::make(buf, len);
591 if (ffT1C) {
592 if (ffT1C->getName()) {
593 if (embFontName) {
594 delete embFontName;
596 embFontName = new GString(ffT1C->getName());
598 if (!baseEnc) {
599 baseEnc = ffT1C->getEncoding();
600 baseEncFromFontFile = gTrue;
604 if (buf) {
605 gfree(buf);
608 // get default base encoding
609 if (!baseEnc) {
610 if (builtinFont) {
611 baseEnc = builtinFont->defaultBaseEnc;
612 hasEncoding = gTrue;
613 } else if (type == fontTrueType) {
614 baseEnc = winAnsiEncoding;
615 } else {
616 baseEnc = standardEncoding;
620 // copy the base encoding
621 for (i = 0; i < 256; ++i) {
622 enc[i] = baseEnc[i];
623 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
624 enc[i] = copyString(baseEnc[i]);
628 // some Type 1C font files have empty encodings, which can break the
629 // T1C->T1 conversion (since the 'seac' operator depends on having
630 // the accents in the encoding), so we fill in any gaps from
631 // StandardEncoding
632 if (type == fontType1C && (extFontFile || embFontID.num >= 0) &&
633 baseEncFromFontFile) {
634 for (i = 0; i < 256; ++i) {
635 if (!enc[i] && standardEncoding[i]) {
636 enc[i] = standardEncoding[i];
637 encFree[i] = gFalse;
642 // merge differences into encoding
643 if (obj1.isDict()) {
644 obj1.dictLookup("Differences", &obj2);
645 if (obj2.isArray()) {
646 hasEncoding = gTrue;
647 code = 0;
648 for (i = 0; i < obj2.arrayGetLength(); ++i) {
649 obj2.arrayGet(i, &obj3);
650 if (obj3.isInt()) {
651 code = obj3.getInt();
652 } else if (obj3.isName()) {
653 if (code >= 0 && code < 256) {
654 if (encFree[code]) {
655 gfree(enc[code]);
657 enc[code] = copyString(obj3.getName());
658 encFree[code] = gTrue;
660 ++code;
661 } else {
662 error(-1, "Wrong type in font encoding resource differences (%s)",
663 obj3.getTypeName());
665 obj3.free();
668 obj2.free();
670 obj1.free();
671 if (ffT1) {
672 delete ffT1;
674 if (ffT1C) {
675 delete ffT1C;
678 //----- build the mapping to Unicode -----
680 // pass 1: use the name-to-Unicode mapping table
681 missing = hex = gFalse;
682 for (code = 0; code < 256; ++code) {
683 if ((charName = enc[code])) {
684 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
685 strcmp(charName, ".notdef")) {
686 // if it wasn't in the name-to-Unicode table, check for a
687 // name that looks like 'Axx' or 'xx', where 'A' is any letter
688 // and 'xx' is two hex digits
689 if ((strlen(charName) == 3 &&
690 isalpha(charName[0]) &&
691 isxdigit(charName[1]) && isxdigit(charName[2]) &&
692 ((charName[1] >= 'a' && charName[1] <= 'f') ||
693 (charName[1] >= 'A' && charName[1] <= 'F') ||
694 (charName[2] >= 'a' && charName[2] <= 'f') ||
695 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
696 (strlen(charName) == 2 &&
697 isxdigit(charName[0]) && isxdigit(charName[1]) &&
698 ((charName[0] >= 'a' && charName[0] <= 'f') ||
699 (charName[0] >= 'A' && charName[0] <= 'F') ||
700 (charName[1] >= 'a' && charName[1] <= 'f') ||
701 (charName[1] >= 'A' && charName[1] <= 'F')))) {
702 hex = gTrue;
704 missing = gTrue;
706 } else {
707 toUnicode[code] = 0;
711 // pass 2: try to fill in the missing chars, looking for names of
712 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
713 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
714 // decimal digits
715 if (missing && globalParams->getMapNumericCharNames()) {
716 for (code = 0; code < 256; ++code) {
717 if ((charName = enc[code]) && !toUnicode[code] &&
718 strcmp(charName, ".notdef")) {
719 n = strlen(charName);
720 code2 = -1;
721 if (hex && n == 3 && isalpha(charName[0]) &&
722 isxdigit(charName[1]) && isxdigit(charName[2])) {
723 sscanf(charName+1, "%x", &code2);
724 } else if (hex && n == 2 &&
725 isxdigit(charName[0]) && isxdigit(charName[1])) {
726 sscanf(charName, "%x", &code2);
727 } else if (!hex && n >= 2 && n <= 4 &&
728 isdigit(charName[0]) && isdigit(charName[1])) {
729 code2 = atoi(charName);
730 } else if (n >= 3 && n <= 5 &&
731 isdigit(charName[1]) && isdigit(charName[2])) {
732 code2 = atoi(charName+1);
733 } else if (n >= 4 && n <= 6 &&
734 isdigit(charName[2]) && isdigit(charName[3])) {
735 code2 = atoi(charName+2);
737 if (code2 >= 0 && code2 <= 0xff) {
738 toUnicode[code] = (Unicode)code2;
744 // construct the char code -> Unicode mapping object
745 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
747 // merge in a ToUnicode CMap, if there is one -- this overwrites
748 // existing entries in ctu, i.e., the ToUnicode CMap takes
749 // precedence, but the other encoding info is allowed to fill in any
750 // holes
751 readToUnicodeCMap(fontDict, 8, ctu);
753 // look for a Unicode-to-Unicode mapping
754 if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
755 for (i = 0; i < 256; ++i) {
756 toUnicode[i] = 0;
758 ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
759 for (i = 0; i < 256; ++i) {
760 n = ctu->mapToUnicode((CharCode)i, uBuf, 8);
761 if (n >= 1) {
762 n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
763 if (n >= 1) {
764 ctu2->setMapping((CharCode)i, uBuf, n);
768 utu->decRefCnt();
769 delete ctu;
770 ctu = ctu2;
773 //----- get the character widths -----
775 // initialize all widths
776 for (code = 0; code < 256; ++code) {
777 widths[code] = missingWidth * 0.001;
780 // use widths from font dict, if present
781 fontDict->lookup("FirstChar", &obj1);
782 firstChar = obj1.isInt() ? obj1.getInt() : 0;
783 obj1.free();
784 if (firstChar < 0 || firstChar > 255) {
785 firstChar = 0;
787 fontDict->lookup("LastChar", &obj1);
788 lastChar = obj1.isInt() ? obj1.getInt() : 255;
789 obj1.free();
790 if (lastChar < 0 || lastChar > 255) {
791 lastChar = 255;
793 mul = (type == fontType3) ? fontMat[0] : 0.001;
794 fontDict->lookup("Widths", &obj1);
795 if (obj1.isArray()) {
796 flags |= fontFixedWidth;
797 if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
798 lastChar = firstChar + obj1.arrayGetLength() - 1;
800 for (code = firstChar; code <= lastChar; ++code) {
801 obj1.arrayGet(code - firstChar, &obj2);
802 if (obj2.isNum()) {
803 widths[code] = obj2.getNum() * mul;
804 if (widths[code] != widths[firstChar]) {
805 flags &= ~fontFixedWidth;
808 obj2.free();
811 // use widths from built-in font
812 } else if (builtinFont) {
813 // this is a kludge for broken PDF files that encode char 32
814 // as .notdef
815 if (builtinFont->widths->getWidth("space", &w)) {
816 widths[32] = 0.001 * w;
818 for (code = 0; code < 256; ++code) {
819 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
820 widths[code] = 0.001 * w;
824 // couldn't find widths -- use defaults
825 } else {
826 // this is technically an error -- the Widths entry is required
827 // for all but the Base-14 fonts -- but certain PDF generators
828 // apparently don't include widths for Arial and TimesNewRoman
829 if (isFixedWidth()) {
830 i = 0;
831 } else if (isSerif()) {
832 i = 8;
833 } else {
834 i = 4;
836 if (isBold()) {
837 i += 2;
839 if (isItalic()) {
840 i += 1;
842 builtinFont = builtinFontSubst[i];
843 // this is a kludge for broken PDF files that encode char 32
844 // as .notdef
845 if (builtinFont->widths->getWidth("space", &w)) {
846 widths[32] = 0.001 * w;
848 for (code = 0; code < 256; ++code) {
849 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
850 widths[code] = 0.001 * w;
854 obj1.free();
856 ok = gTrue;
859 Gfx8BitFont::~Gfx8BitFont() {
860 int i;
862 for (i = 0; i < 256; ++i) {
863 if (encFree[i] && enc[i]) {
864 gfree(enc[i]);
867 ctu->decRefCnt();
868 if (charProcs.isDict()) {
869 charProcs.free();
871 if (resources.isDict()) {
872 resources.free();
876 int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
877 Unicode *u, int uSize, int *uLen,
878 double *dx, double *dy, double *ox, double *oy) {
879 CharCode c;
881 *code = c = (CharCode)(*s & 0xff);
882 *uLen = ctu->mapToUnicode(c, u, uSize);
883 *dx = widths[c];
884 *dy = *ox = *oy = 0;
885 return 1;
888 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
889 ctu->incRefCnt();
890 return ctu;
893 Gushort *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
894 Gushort *map;
895 int cmapPlatform, cmapEncoding;
896 int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
897 GBool useMacRoman, useUnicode;
898 char *charName;
899 Unicode u;
900 int code, i, n;
902 map = (Gushort *)gmalloc(256 * sizeof(Gushort));
903 for (i = 0; i < 256; ++i) {
904 map[i] = 0;
907 // To match up with the Adobe-defined behaviour, we choose a cmap
908 // like this:
909 // 1. If the PDF font has an encoding:
910 // 1a. If the PDF font specified MacRomanEncoding and the
911 // TrueType font has a Macintosh Roman cmap, use it, and
912 // reverse map the char names through MacRomanEncoding to
913 // get char codes.
914 // 1b. If the TrueType font has a Microsoft Unicode cmap or a
915 // non-Microsoft Unicode cmap, use it, and use the Unicode
916 // indexes, not the char codes.
917 // 1c. If the PDF font is symbolic and the TrueType font has a
918 // Microsoft Symbol cmap, use it, and use char codes
919 // directly (possibly with an offset of 0xf000).
920 // 1d. If the TrueType font has a Macintosh Roman cmap, use it,
921 // as in case 1a.
922 // 2. If the PDF font does not have an encoding:
923 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
924 // and use char codes directly (possibly with an offset of
925 // 0xf000).
926 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
927 // and use char codes directly (possible with an offset of
928 // 0xf000).
929 // 3. If none of these rules apply, use the first cmap and hope for
930 // the best (this shouldn't happen).
931 unicodeCmap = macRomanCmap = msSymbolCmap = -1;
932 for (i = 0; i < ff->getNumCmaps(); ++i) {
933 cmapPlatform = ff->getCmapPlatform(i);
934 cmapEncoding = ff->getCmapEncoding(i);
935 if ((cmapPlatform == 3 && cmapEncoding == 1) ||
936 cmapPlatform == 0) {
937 unicodeCmap = i;
938 } else if (cmapPlatform == 1 && cmapEncoding == 0) {
939 macRomanCmap = i;
940 } else if (cmapPlatform == 3 && cmapEncoding == 0) {
941 msSymbolCmap = i;
944 cmap = 0;
945 useMacRoman = gFalse;
946 useUnicode = gFalse;
947 if (hasEncoding) {
948 if (usesMacRomanEnc && macRomanCmap >= 0) {
949 cmap = macRomanCmap;
950 useMacRoman = gTrue;
951 } else if (unicodeCmap >= 0) {
952 cmap = unicodeCmap;
953 useUnicode = gTrue;
954 } else if ((flags & fontSymbolic) && msSymbolCmap >= 0) {
955 cmap = msSymbolCmap;
956 } else if (macRomanCmap >= 0) {
957 cmap = macRomanCmap;
958 useMacRoman = gTrue;
960 } else {
961 if (macRomanCmap >= 0) {
962 cmap = macRomanCmap;
963 } else if (msSymbolCmap >= 0) {
964 cmap = msSymbolCmap;
968 // reverse map the char names through MacRomanEncoding, then map the
969 // char codes through the cmap
970 if (useMacRoman) {
971 for (i = 0; i < 256; ++i) {
972 if ((charName = enc[i])) {
973 if ((code = globalParams->getMacRomanCharCode(charName))) {
974 map[i] = ff->mapCodeToGID(cmap, code);
979 // map Unicode through the cmap
980 } else if (useUnicode) {
981 for (i = 0; i < 256; ++i) {
982 if ((n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
983 map[i] = ff->mapCodeToGID(cmap, u);
987 // map the char codes through the cmap, possibly with an offset of
988 // 0xf000
989 } else {
990 for (i = 0; i < 256; ++i) {
991 if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
992 map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
997 // try the TrueType 'post' table to handle any unmapped characters
998 for (i = 0; i < 256; ++i) {
999 if (!map[i] && (charName = enc[i])) {
1000 map[i] = (Gushort)(int)ff->mapNameToGID(charName);
1004 return map;
1007 Dict *Gfx8BitFont::getCharProcs() {
1008 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
1011 Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
1012 if (enc[code] && charProcs.isDict()) {
1013 charProcs.dictLookup(enc[code], proc);
1014 } else {
1015 proc->initNull();
1017 return proc;
1020 Dict *Gfx8BitFont::getResources() {
1021 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1024 //------------------------------------------------------------------------
1025 // GfxCIDFont
1026 //------------------------------------------------------------------------
1028 static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
1029 return ((GfxFontCIDWidthExcep *)w1)->first -
1030 ((GfxFontCIDWidthExcep *)w2)->first;
1033 static int CDECL cmpWidthExcepV(const void *w1, const void *w2) {
1034 return ((GfxFontCIDWidthExcepV *)w1)->first -
1035 ((GfxFontCIDWidthExcepV *)w2)->first;
1038 GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
1039 Dict *fontDict):
1040 GfxFont(tagA, idA, nameA)
1042 Dict *desFontDict;
1043 GString *collection, *cMapName;
1044 Object desFontDictObj;
1045 Object obj1, obj2, obj3, obj4, obj5, obj6;
1046 int c1, c2;
1047 int excepsSize, i, j, k;
1049 ascent = 0.95;
1050 descent = -0.35;
1051 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1052 cMap = NULL;
1053 ctu = NULL;
1054 widths.defWidth = 1.0;
1055 widths.defHeight = -1.0;
1056 widths.defVY = 0.880;
1057 widths.exceps = NULL;
1058 widths.nExceps = 0;
1059 widths.excepsV = NULL;
1060 widths.nExcepsV = 0;
1061 cidToGID = NULL;
1062 cidToGIDLen = 0;
1064 // get the descendant font
1065 if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
1066 error(-1, "Missing DescendantFonts entry in Type 0 font");
1067 obj1.free();
1068 goto err1;
1070 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1071 error(-1, "Bad descendant font in Type 0 font");
1072 goto err3;
1074 obj1.free();
1075 desFontDict = desFontDictObj.getDict();
1077 // font type
1078 if (!desFontDict->lookup("Subtype", &obj1)) {
1079 error(-1, "Missing Subtype entry in Type 0 descendant font");
1080 goto err3;
1082 if (obj1.isName("CIDFontType0")) {
1083 type = fontCIDType0;
1084 } else if (obj1.isName("CIDFontType2")) {
1085 type = fontCIDType2;
1086 } else {
1087 error(-1, "Unknown Type 0 descendant font type '%s'",
1088 obj1.isName() ? obj1.getName() : "???");
1089 goto err3;
1091 obj1.free();
1093 // get info from font descriptor
1094 readFontDescriptor(xref, desFontDict);
1096 // look for an external font file
1097 findExtFontFile();
1099 //----- encoding info -----
1101 // char collection
1102 if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1103 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1104 goto err3;
1106 obj1.dictLookup("Registry", &obj2);
1107 obj1.dictLookup("Ordering", &obj3);
1108 if (!obj2.isString() || !obj3.isString()) {
1109 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1110 goto err4;
1112 collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1113 obj3.free();
1114 obj2.free();
1115 obj1.free();
1117 // look for a ToUnicode CMap
1118 if (!(ctu = readToUnicodeCMap(fontDict, 16, NULL))) {
1120 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
1121 // cidToUnicode files
1122 if (collection->cmp("Adobe-Identity") &&
1123 collection->cmp("Adobe-UCS")) {
1125 // look for a user-supplied .cidToUnicode file
1126 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
1127 error(-1, "Unknown character collection '%s'",
1128 collection->getCString());
1129 delete collection;
1130 goto err2;
1135 // encoding (i.e., CMap)
1136 //~ need to handle a CMap stream here
1137 //~ also need to deal with the UseCMap entry in the stream dict
1138 if (!fontDict->lookup("Encoding", &obj1)->isName()) {
1139 error(-1, "Missing or invalid Encoding entry in Type 0 font");
1140 delete collection;
1141 goto err3;
1143 cMapName = new GString(obj1.getName());
1144 obj1.free();
1145 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
1146 error(-1, "Unknown CMap '%s' for character collection '%s'",
1147 cMapName->getCString(), collection->getCString());
1148 delete collection;
1149 delete cMapName;
1150 goto err2;
1152 delete collection;
1153 delete cMapName;
1155 // CIDToGIDMap (for embedded TrueType fonts)
1156 if (type == fontCIDType2) {
1157 desFontDict->lookup("CIDToGIDMap", &obj1);
1158 if (obj1.isStream()) {
1159 cidToGIDLen = 0;
1160 i = 64;
1161 cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
1162 obj1.streamReset();
1163 while ((c1 = obj1.streamGetChar()) != EOF &&
1164 (c2 = obj1.streamGetChar()) != EOF) {
1165 if (cidToGIDLen == i) {
1166 i *= 2;
1167 cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
1169 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
1171 } else if (!obj1.isName("Identity") && !obj1.isNull()) {
1172 error(-1, "Invalid CIDToGIDMap entry in CID font");
1174 obj1.free();
1177 //----- character metrics -----
1179 // default char width
1180 if (desFontDict->lookup("DW", &obj1)->isInt()) {
1181 widths.defWidth = obj1.getInt() * 0.001;
1183 obj1.free();
1185 // char width exceptions
1186 if (desFontDict->lookup("W", &obj1)->isArray()) {
1187 excepsSize = 0;
1188 i = 0;
1189 while (i + 1 < obj1.arrayGetLength()) {
1190 obj1.arrayGet(i, &obj2);
1191 obj1.arrayGet(i + 1, &obj3);
1192 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1193 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1194 if (widths.nExceps == excepsSize) {
1195 excepsSize += 16;
1196 widths.exceps = (GfxFontCIDWidthExcep *)
1197 grealloc(widths.exceps,
1198 excepsSize * sizeof(GfxFontCIDWidthExcep));
1200 widths.exceps[widths.nExceps].first = obj2.getInt();
1201 widths.exceps[widths.nExceps].last = obj3.getInt();
1202 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1203 ++widths.nExceps;
1204 } else {
1205 error(-1, "Bad widths array in Type 0 font");
1207 obj4.free();
1208 i += 3;
1209 } else if (obj2.isInt() && obj3.isArray()) {
1210 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1211 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1212 widths.exceps = (GfxFontCIDWidthExcep *)
1213 grealloc(widths.exceps,
1214 excepsSize * sizeof(GfxFontCIDWidthExcep));
1216 j = obj2.getInt();
1217 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1218 if (obj3.arrayGet(k, &obj4)->isNum()) {
1219 widths.exceps[widths.nExceps].first = j;
1220 widths.exceps[widths.nExceps].last = j;
1221 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1222 ++j;
1223 ++widths.nExceps;
1224 } else {
1225 error(-1, "Bad widths array in Type 0 font");
1227 obj4.free();
1229 i += 2;
1230 } else {
1231 error(-1, "Bad widths array in Type 0 font");
1232 ++i;
1234 obj3.free();
1235 obj2.free();
1237 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1238 &cmpWidthExcep);
1240 obj1.free();
1242 // default metrics for vertical font
1243 if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1244 obj1.arrayGetLength() == 2) {
1245 if (obj1.arrayGet(0, &obj2)->isNum()) {
1246 widths.defVY = obj2.getNum() * 0.001;
1248 obj2.free();
1249 if (obj1.arrayGet(1, &obj2)->isNum()) {
1250 widths.defHeight = obj2.getNum() * 0.001;
1252 obj2.free();
1254 obj1.free();
1256 // char metric exceptions for vertical font
1257 if (desFontDict->lookup("W2", &obj1)->isArray()) {
1258 excepsSize = 0;
1259 i = 0;
1260 while (i + 1 < obj1.arrayGetLength()) {
1261 obj1.arrayGet(i, &obj2);
1262 obj1.arrayGet(i+ 1, &obj3);
1263 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1264 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1265 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1266 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1267 if (widths.nExcepsV == excepsSize) {
1268 excepsSize += 16;
1269 widths.excepsV = (GfxFontCIDWidthExcepV *)
1270 grealloc(widths.excepsV,
1271 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1273 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1274 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1275 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1276 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1277 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1278 ++widths.nExcepsV;
1279 } else {
1280 error(-1, "Bad widths (W2) array in Type 0 font");
1282 obj6.free();
1283 obj5.free();
1284 obj4.free();
1285 i += 5;
1286 } else if (obj2.isInt() && obj3.isArray()) {
1287 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1288 excepsSize =
1289 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1290 widths.excepsV = (GfxFontCIDWidthExcepV *)
1291 grealloc(widths.excepsV,
1292 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1294 j = obj2.getInt();
1295 for (k = 0; k < obj3.arrayGetLength(); k += 3) {
1296 if (obj3.arrayGet(k, &obj4)->isNum() &&
1297 obj3.arrayGet(k+1, &obj5)->isNum() &&
1298 obj3.arrayGet(k+2, &obj6)->isNum()) {
1299 widths.excepsV[widths.nExceps].first = j;
1300 widths.excepsV[widths.nExceps].last = j;
1301 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1302 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1303 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1304 ++j;
1305 ++widths.nExcepsV;
1306 } else {
1307 error(-1, "Bad widths (W2) array in Type 0 font");
1309 obj6.free();
1310 obj5.free();
1311 obj4.free();
1313 i += 2;
1314 } else {
1315 error(-1, "Bad widths (W2) array in Type 0 font");
1316 ++i;
1318 obj3.free();
1319 obj2.free();
1321 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1322 &cmpWidthExcepV);
1324 obj1.free();
1326 desFontDictObj.free();
1327 ok = gTrue;
1328 return;
1330 err4:
1331 obj3.free();
1332 obj2.free();
1333 err3:
1334 obj1.free();
1335 err2:
1336 desFontDictObj.free();
1337 err1:;
1340 GfxCIDFont::~GfxCIDFont() {
1341 if (cMap) {
1342 cMap->decRefCnt();
1344 if (ctu) {
1345 ctu->decRefCnt();
1347 gfree(widths.exceps);
1348 gfree(widths.excepsV);
1349 if (cidToGID) {
1350 gfree(cidToGID);
1354 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1355 Unicode *u, int uSize, int *uLen,
1356 double *dx, double *dy, double *ox, double *oy) {
1357 CID cid;
1358 double w, h, vx, vy;
1359 int n, a, b, m;
1361 if (!cMap) {
1362 *code = 0;
1363 *uLen = 0;
1364 *dx = *dy = 0;
1365 return 1;
1368 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1369 if (ctu) {
1370 *uLen = ctu->mapToUnicode(cid, u, uSize);
1371 } else {
1372 *uLen = 0;
1375 // horizontal
1376 if (cMap->getWMode() == 0) {
1377 w = widths.defWidth;
1378 h = vx = vy = 0;
1379 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1380 a = 0;
1381 b = widths.nExceps;
1382 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1383 while (b - a > 1) {
1384 m = (a + b) / 2;
1385 if (widths.exceps[m].first <= cid) {
1386 a = m;
1387 } else {
1388 b = m;
1391 if (cid <= widths.exceps[a].last) {
1392 w = widths.exceps[a].width;
1396 // vertical
1397 } else {
1398 w = 0;
1399 h = widths.defHeight;
1400 vx = widths.defWidth / 2;
1401 vy = widths.defVY;
1402 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1403 a = 0;
1404 b = widths.nExcepsV;
1405 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1406 while (b - a > 1) {
1407 m = (a + b) / 2;
1408 if (widths.excepsV[m].last <= cid) {
1409 a = m;
1410 } else {
1411 b = m;
1414 if (cid <= widths.excepsV[a].last) {
1415 h = widths.excepsV[a].height;
1416 vx = widths.excepsV[a].vx;
1417 vy = widths.excepsV[a].vy;
1422 *dx = w;
1423 *dy = h;
1424 *ox = vx;
1425 *oy = vy;
1427 return n;
1430 int GfxCIDFont::getWMode() {
1431 return cMap ? cMap->getWMode() : 0;
1434 CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1435 if (ctu) {
1436 ctu->incRefCnt();
1438 return ctu;
1441 GString *GfxCIDFont::getCollection() {
1442 return cMap ? cMap->getCollection() : (GString *)NULL;
1445 //------------------------------------------------------------------------
1446 // GfxFontDict
1447 //------------------------------------------------------------------------
1449 GfxFontDict::GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict) {
1450 int i;
1451 Object obj1, obj2;
1452 Ref r;
1454 numFonts = fontDict->getLength();
1455 fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
1456 for (i = 0; i < numFonts; ++i) {
1457 fontDict->getValNF(i, &obj1);
1458 obj1.fetch(xref, &obj2);
1459 if (obj2.isDict()) {
1460 if (obj1.isRef()) {
1461 r = obj1.getRef();
1462 } else {
1463 // no indirect reference for this font, so invent a unique one
1464 // (legal generation numbers are five digits, so any 6-digit
1465 // number would be safe)
1466 r.num = i;
1467 if (fontDictRef) {
1468 r.gen = 100000 + fontDictRef->num;
1469 } else {
1470 r.gen = 999999;
1473 fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1474 r, obj2.getDict());
1475 if (fonts[i] && !fonts[i]->isOk()) {
1476 delete fonts[i];
1477 fonts[i] = NULL;
1479 } else {
1480 error(-1, "font resource is not a dictionary");
1481 fonts[i] = NULL;
1483 obj1.free();
1484 obj2.free();
1488 GfxFontDict::~GfxFontDict() {
1489 int i;
1491 for (i = 0; i < numFonts; ++i) {
1492 if (fonts[i]) {
1493 delete fonts[i];
1496 gfree(fonts);
1499 GfxFont *GfxFontDict::lookup(char *tag) {
1500 int i;
1502 for (i = 0; i < numFonts; ++i) {
1503 if (fonts[i] && fonts[i]->matches(tag)) {
1504 return fonts[i];
1507 return NULL;