23#include "GlobalParams.h"
25#include "CharCodeToUnicode.h"
26#include "FontEncodingTables.h"
27#include "BuiltinFontTables.h"
33struct StdFontMapEntry {
35 const char *properName;
38static StdFontMapEntry stdFontMap[] = {
39 { "Arial", "Helvetica" },
40 { "Arial,Bold", "Helvetica-Bold" },
41 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
42 { "Arial,Italic", "Helvetica-Oblique" },
43 { "Arial-Bold", "Helvetica-Bold" },
44 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
45 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
46 { "Arial-BoldMT", "Helvetica-Bold" },
47 { "Arial-Italic", "Helvetica-Oblique" },
48 { "Arial-ItalicMT", "Helvetica-Oblique" },
49 { "ArialMT", "Helvetica" },
50 { "Courier,Bold", "Courier-Bold" },
51 { "Courier,Italic", "Courier-Oblique" },
52 { "Courier,BoldItalic", "Courier-BoldOblique" },
53 { "CourierNew", "Courier" },
54 { "CourierNew,Bold", "Courier-Bold" },
55 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
56 { "CourierNew,Italic", "Courier-Oblique" },
57 { "CourierNew-Bold", "Courier-Bold" },
58 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
59 { "CourierNew-Italic", "Courier-Oblique" },
60 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
61 { "CourierNewPS-BoldMT", "Courier-Bold" },
62 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
63 { "CourierNewPSMT", "Courier" },
64 { "Helvetica,Bold", "Helvetica-Bold" },
65 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
66 { "Helvetica,Italic", "Helvetica-Oblique" },
67 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
68 { "Helvetica-Italic", "Helvetica-Oblique" },
69 { "TimesNewRoman", "Times-Roman" },
70 { "TimesNewRoman,Bold", "Times-Bold" },
71 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
72 { "TimesNewRoman,Italic", "Times-Italic" },
73 { "TimesNewRoman-Bold", "Times-Bold" },
74 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
75 { "TimesNewRoman-Italic", "Times-Italic" },
76 { "TimesNewRomanPS", "Times-Roman" },
77 { "TimesNewRomanPS-Bold", "Times-Bold" },
78 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
79 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
80 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
81 { "TimesNewRomanPS-Italic", "Times-Italic" },
82 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
83 { "TimesNewRomanPSMT", "Times-Roman" }
90GfxFont *GfxFont::makeFont(XRef *xref, const char *tagA, Ref idA, Dict *fontDict) {
97 fontDict->lookup( "BaseFont", &obj1);
99 nameA = new GString(obj1.getName());
105 fontDict->lookup( "Subtype", &obj1);
106 if (obj1.isName( "Type1") || obj1.isName( "MMType1")) {
107 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
108 } else if (obj1.isName( "Type1C")) {
109 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
110 } else if (obj1.isName( "Type3")) {
111 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
112 } else if (obj1.isName( "TrueType")) {
113 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
114 } else if (obj1.isName( "Type0")) {
115 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
117 error(-1, "Unknown font type: '%s'",
118 obj1.isName() ? obj1.getName() : "???");
119 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
126GfxFont::GfxFont( const char *tagA, Ref idA, GString *nameA) {
128 tag = new GString(tagA);
148void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
149 Object obj1, obj2, obj3, obj4;
160 if (fontDict->lookup( "FontDescriptor", &obj1)->isDict()) {
163 if (obj1.dictLookup( "Flags", &obj2)->isInt()) {
164 flags = obj2.getInt();
169 obj1.dictLookup( "FontName", &obj2);
171 embFontName = new GString(obj2.getName());
176 if (obj1.dictLookupNF( "FontFile", &obj2)->isRef()) {
177 if (type == fontType1) {
178 embFontID = obj2.getRef();
180 error(-1, "Mismatch between font type and embedded font file");
184 if (embFontID.num == -1 &&
185 obj1.dictLookupNF( "FontFile2", &obj2)->isRef()) {
186 if (type == fontTrueType || type == fontCIDType2) {
187 embFontID = obj2.getRef();
189 error(-1, "Mismatch between font type and embedded font file");
193 if (embFontID.num == -1 &&
194 obj1.dictLookupNF( "FontFile3", &obj2)->isRef()) {
195 if (obj2.fetch(xref, &obj3)->isStream()) {
196 obj3.streamGetDict()->lookup( "Subtype", &obj4);
197 if (obj4.isName( "Type1")) {
198 if (type == fontType1) {
199 embFontID = obj2.getRef();
201 error(-1, "Mismatch between font type and embedded font file");
203 } else if (obj4.isName( "Type1C")) {
204 if (type == fontType1) {
206 embFontID = obj2.getRef();
207 } else if (type == fontType1C) {
208 embFontID = obj2.getRef();
210 error(-1, "Mismatch between font type and embedded font file");
212 } else if (obj4.isName( "TrueType")) {
213 if (type == fontTrueType) {
214 embFontID = obj2.getRef();
216 error(-1, "Mismatch between font type and embedded font file");
218 } else if (obj4.isName( "CIDFontType0C")) {
219 if (type == fontCIDType0) {
220 type = fontCIDType0C;
221 embFontID = obj2.getRef();
223 error(-1, "Mismatch between font type and embedded font file");
226 error(-1, "Unknown embedded font type '%s'",
227 obj4.isName() ? obj4.getName() : "???");
236 obj1.dictLookup( "MissingWidth", &obj2);
238 missingWidth = obj2.getNum();
243 obj1.dictLookup( "Ascent", &obj2);
245 t = 0.001 * obj2.getNum();
252 obj1.dictLookup( "Descent", &obj2);
254 t = 0.001 * obj2.getNum();
263 if (obj1.dictLookup( "FontBBox", &obj2)->isArray()) {
264 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
265 if (obj2.arrayGet(i, &obj3)->isNum()) {
266 fontBBox[i] = 0.001 * obj3.getNum();
277CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits) {
278 CharCodeToUnicode *ctu;
283 if (!fontDict->lookup( "ToUnicode", &obj1)->isStream()) {
289 while ((c = obj1.streamGetChar()) != EOF) {
294 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
299void GfxFont::findExtFontFile() {
301 if (type == fontType1) {
302 extFontFile = globalParams->findFontFile(name, ".pfa", ".pfb");
303 } else if (type == fontTrueType) {
304 extFontFile = globalParams->findFontFile(name, ".ttf", NULL);
309char *GfxFont::readExtFontFile( int *len) {
313 if (!(f = fopen(extFontFile->getCString(), "rb"))) {
314 error(-1, "External font file '%s' vanished", extFontFile->getCString());
317 fseek(f, 0, SEEK_END);
318 *len = (int)ftell(f);
319 fseek(f, 0, SEEK_SET);
320 buf = ( char *)gmalloc(*len);
321 if (( int)fread(buf, 1, *len, f) != *len) {
322 error(-1, "Error reading external font file '%s'", extFontFile);
328char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
335 obj1.initRef(embFontID.num, embFontID.gen);
336 obj1.fetch(xref, &obj2);
337 if (!obj2.isStream()) {
338 error(-1, "Embedded font file is not a stream");
344 str = obj2.getStream();
349 while ((c = str->getChar()) != EOF) {
352 buf = ( char *)grealloc(buf, size);
369Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GString *nameA,
370 GfxFontType typeA, Dict *fontDict):
371 GfxFont(tagA, idA, nameA)
373 BuiltinFont *builtinFont;
375 GBool baseEncFromFontFile;
382 Unicode toUnicode[256];
384 int firstChar, lastChar;
386 Object obj1, obj2, obj3;
398 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
402 if (name->cmp(stdFontMap[m].altName) >= 0) {
408 if (!name->cmp(stdFontMap[a].altName)) {
410 name = new GString(stdFontMap[a].properName);
417 for (i = 0; i < nBuiltinFonts; ++i) {
418 if (!name->cmp(builtinFonts[i].name)) {
419 builtinFont = &builtinFonts[i];
427 ascent = 0.001 * builtinFont->ascent;
428 descent = 0.001 * builtinFont->descent;
429 fontBBox[0] = 0.001 * builtinFont->bbox[0];
430 fontBBox[1] = 0.001 * builtinFont->bbox[1];
431 fontBBox[2] = 0.001 * builtinFont->bbox[2];
432 fontBBox[3] = 0.001 * builtinFont->bbox[3];
436 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
440 readFontDescriptor(xref, fontDict);
446 fontMat[0] = fontMat[3] = 1;
447 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
448 if (fontDict->lookup( "FontMatrix", &obj1)->isArray()) {
449 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
450 if (obj1.arrayGet(i, &obj2)->isNum()) {
451 fontMat[i] = obj2.getNum();
459 if (type == fontType3) {
460 if (fontDict->lookup( "FontBBox", &obj1)->isArray()) {
461 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
462 if (obj1.arrayGet(i, &obj2)->isNum()) {
463 fontBBox[i] = obj2.getNum();
469 if (!fontDict->lookup( "CharProcs", &charProcs)->isDict()) {
470 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
473 if (!fontDict->lookup( "Resources", &resources)->isDict()) {
493 hasEncoding = gFalse;
495 baseEncFromFontFile = gFalse;
496 fontDict->lookup( "Encoding", &obj1);
498 obj1.dictLookup( "BaseEncoding", &obj2);
499 if (obj2.isName( "MacRomanEncoding")) {
501 baseEnc = ( char **)macRomanEncoding;
502 } else if (obj2.isName( "MacExpertEncoding")) {
504 baseEnc = ( char **)macExpertEncoding;
505 } else if (obj2.isName( "WinAnsiEncoding")) {
507 baseEnc = ( char **)winAnsiEncoding;
508 } else if (obj2.isName( "StandardEncoding")) {
510 baseEnc = ( char **)standardEncoding;
513 } else if (obj1.isName( "MacRomanEncoding")) {
515 baseEnc = ( char **)macRomanEncoding;
516 } else if (obj1.isName( "MacExpertEncoding")) {
518 baseEnc = ( char **)macExpertEncoding;
519 } else if (obj1.isName( "WinAnsiEncoding")) {
521 baseEnc = ( char **)winAnsiEncoding;
522 } else if (obj1.isName( "StandardEncoding")) {
524 baseEnc = ( char **)standardEncoding;
532 if ((type == fontType1 || type == fontType1C) &&
533 (extFontFile || embFontID.num >= 0)) {
535 buf = readExtFontFile(&len);
537 buf = readEmbFontFile(xref, &len);
540 if (type == fontType1C && !strncmp(buf, "%!", 2)) {
545 if (type == fontType1) {
546 fontFile = new Type1FontFile(buf, len);
548 fontFile = new Type1CFontFile(buf, len);
550 if (fontFile->getName()) {
554 embFontName = new GString(fontFile->getName());
557 baseEnc = fontFile->getEncoding();
558 baseEncFromFontFile = gTrue;
567 baseEnc = ( char **)builtinFont->defaultBaseEnc;
569 } else if (type == fontTrueType) {
570 baseEnc = ( char **)winAnsiEncoding;
572 baseEnc = ( char **)standardEncoding;
577 for (i = 0; i < 256; ++i) {
579 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
580 enc[i] = copyString(baseEnc[i]);
586 obj1.dictLookup( "Differences", &obj2);
587 if (obj2.isArray()) {
590 for (i = 0; i < obj2.arrayGetLength(); ++i) {
591 obj2.arrayGet(i, &obj3);
593 code = obj3.getInt();
594 } else if (obj3.isName()) {
599 enc[code] = copyString(obj3.getName());
600 encFree[code] = gTrue;
604 error(-1, "Wrong type in font encoding resource differences (%s)",
620 if (!(ctu = readToUnicodeCMap(fontDict, 8))) {
625 missing = hex = gFalse;
626 for (code = 0; code < 256; ++code) {
627 if ((charName = enc[code])) {
628 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
629 strcmp(charName, ".notdef")) {
633 if ((strlen(charName) == 3 &&
634 isalpha(charName[0]) &&
635 isxdigit(charName[1]) && isxdigit(charName[2]) &&
636 ((charName[1] >= 'a' && charName[1] <= 'f') ||
637 (charName[1] >= 'A' && charName[1] <= 'F') ||
638 (charName[2] >= 'a' && charName[2] <= 'f') ||
639 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
640 (strlen(charName) == 2 &&
641 isxdigit(charName[0]) && isxdigit(charName[1]) &&
642 ((charName[0] >= 'a' && charName[0] <= 'f') ||
643 (charName[0] >= 'A' && charName[0] <= 'F') ||
644 (charName[1] >= 'a' && charName[1] <= 'f') ||
645 (charName[1] >= 'A' && charName[1] <= 'F')))) {
659 if (missing && globalParams->getMapNumericCharNames()) {
660 for (code = 0; code < 256; ++code) {
661 if ((charName = enc[code]) && !toUnicode[code] &&
662 strcmp(charName, ".notdef")) {
663 n = strlen(charName);
665 if (hex && n == 3 && isalpha(charName[0]) &&
666 isxdigit(charName[1]) && isxdigit(charName[2])) {
668 sscanf(charName+1, "%x", &tmp);
670 } else if (hex && n == 2 &&
671 isxdigit(charName[0]) && isxdigit(charName[1])) {
673 sscanf(charName, "%x", &tmp);
675 } else if (!hex && n >= 2 && n <= 4 &&
676 isdigit(charName[0]) && isdigit(charName[1])) {
677 code2 = atoi(charName);
678 } else if (n >= 3 && n <= 5 &&
679 isdigit(charName[1]) && isdigit(charName[2])) {
680 code2 = atoi(charName+1);
681 } else if (n >= 4 && n <= 6 &&
682 isdigit(charName[2]) && isdigit(charName[3])) {
683 code2 = atoi(charName+2);
685 if (code2 >= 0 && code2 <= 0xff) {
686 toUnicode[code] = (Unicode)code2;
692 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
698 for (code = 0; code < 256; ++code) {
699 widths[code] = missingWidth * 0.001;
703 fontDict->lookup( "FirstChar", &obj1);
704 firstChar = obj1.isInt() ? obj1.getInt() : 0;
706 fontDict->lookup( "LastChar", &obj1);
707 lastChar = obj1.isInt() ? obj1.getInt() : 255;
709 mul = (type == fontType3) ? fontMat[0] : 0.001;
710 fontDict->lookup( "Widths", &obj1);
711 if (obj1.isArray()) {
712 flags |= fontFixedWidth;
713 for (code = firstChar; code <= lastChar; ++code) {
714 obj1.arrayGet(code - firstChar, &obj2);
716 widths[code] = obj2.getNum() * mul;
717 if (widths[code] != widths[firstChar]) {
718 flags &= ~fontFixedWidth;
725 } else if (builtinFont) {
728 if (builtinFont->widths->getWidth( "space", &w)) {
729 widths[32] = 0.001 * w;
731 for (code = 0; code < 256; ++code) {
732 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
733 widths[code] = 0.001 * w;
742 if (isFixedWidth()) {
744 } else if (isSerif()) {
755 builtinFont = builtinFontSubst[i];
758 if (builtinFont->widths->getWidth( "space", &w)) {
759 widths[32] = 0.001 * w;
761 for (code = 0; code < 256; ++code) {
762 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
763 widths[code] = 0.001 * w;
772Gfx8BitFont::~Gfx8BitFont() {
775 for (i = 0; i < 256; ++i) {
776 if (encFree[i] && enc[i]) {
781 if (charProcs.isDict()) {
784 if (resources.isDict()) {
789int Gfx8BitFont::getNextChar( char *s, int , CharCode *code,
790 Unicode *u, int uSize, int *uLen,
791 double *dx, double *dy, double *ox, double *oy) {
794 *code = c = (CharCode)(*s & 0xff);
795 *uLen = ctu->mapToUnicode(c, u, uSize);
801CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
806Dict *Gfx8BitFont::getCharProcs() {
807 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
810Object *Gfx8BitFont::getCharProc( int code, Object *proc) {
811 if (charProcs.isDict()) {
812 charProcs.dictLookup(enc[code], proc);
819Dict *Gfx8BitFont::getResources() {
820 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
827static int cmpWidthExcep( const void *w1, const void *w2) {
828 return ((GfxFontCIDWidthExcep *)w1)->first -
829 ((GfxFontCIDWidthExcep *)w2)->first;
832static int cmpWidthExcepV( const void *w1, const void *w2) {
833 return ((GfxFontCIDWidthExcepV *)w1)->first -
834 ((GfxFontCIDWidthExcepV *)w2)->first;
837GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GString *nameA,
839 GfxFont(tagA, idA, nameA)
842 GString *collection, *cMapName;
843 Object desFontDictObj;
844 Object obj1, obj2, obj3, obj4, obj5, obj6;
846 int excepsSize, i, j, k;
850 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
853 widths.defWidth = 1.0;
854 widths.defHeight = -1.0;
855 widths.defVY = 0.880;
856 widths.exceps = NULL;
858 widths.excepsV = NULL;
864 if (!fontDict->lookup( "DescendantFonts", &obj1)->isArray()) {
865 error(-1, "Missing DescendantFonts entry in Type 0 font");
869 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
870 error(-1, "Bad descendant font in Type 0 font");
874 desFontDict = desFontDictObj.getDict();
877 if (!desFontDict->lookup( "Subtype", &obj1)) {
878 error(-1, "Missing Subtype entry in Type 0 descendant font");
881 if (obj1.isName( "CIDFontType0")) {
883 } else if (obj1.isName( "CIDFontType2")) {
886 error(-1, "Unknown Type 0 descendant font type '%s'",
887 obj1.isName() ? obj1.getName() : "???");
893 readFontDescriptor(xref, desFontDict);
901 if (!desFontDict->lookup( "CIDSystemInfo", &obj1)->isDict()) {
902 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
905 obj1.dictLookup( "Registry", &obj2);
906 obj1.dictLookup( "Ordering", &obj3);
907 if (!obj2.isString() || !obj3.isString()) {
908 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
911 collection = obj2.getString()->copy()->append( '-')->append(obj3.getString());
917 if (!(ctu = readToUnicodeCMap(fontDict, 16))) {
921 if (collection->cmp( "Adobe-Identity") &&
922 collection->cmp( "Adobe-UCS")) {
925 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
926 error(-1, "Unknown character collection '%s'",
927 collection->getCString());
937 if (!fontDict->lookup( "Encoding", &obj1)->isName()) {
938 error(-1, "Missing or invalid Encoding entry in Type 0 font");
942 cMapName = new GString(obj1.getName());
944 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
945 error(-1, "Unknown CMap '%s' for character collection '%s'",
946 cMapName->getCString(), collection->getCString());
955 if (type == fontCIDType2) {
956 fontDict->lookup( "CIDToGIDMap", &obj1);
957 if (obj1.isStream()) {
960 cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
962 while ((c1 = obj1.streamGetChar()) != EOF &&
963 (c2 = obj1.streamGetChar()) != EOF) {
964 if (cidToGIDLen == i) {
966 cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
968 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
970 } else if (!obj1.isName( "Identity") && !obj1.isNull()) {
971 error(-1, "Invalid CIDToGIDMap entry in CID font");
979 if (desFontDict->lookup( "DW", &obj1)->isInt()) {
980 widths.defWidth = obj1.getInt() * 0.001;
985 if (desFontDict->lookup( "W", &obj1)->isArray()) {
988 while (i + 1 < obj1.arrayGetLength()) {
989 obj1.arrayGet(i, &obj2);
990 obj1.arrayGet(i + 1, &obj3);
991 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
992 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
993 if (widths.nExceps == excepsSize) {
995 widths.exceps = (GfxFontCIDWidthExcep *)
996 grealloc(widths.exceps,
997 excepsSize * sizeof(GfxFontCIDWidthExcep));
999 widths.exceps[widths.nExceps].first = obj2.getInt();
1000 widths.exceps[widths.nExceps].last = obj3.getInt();
1001 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1004 error(-1, "Bad widths array in Type 0 font");
1008 } else if (obj2.isInt() && obj3.isArray()) {
1009 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1010 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1011 widths.exceps = (GfxFontCIDWidthExcep *)
1012 grealloc(widths.exceps,
1013 excepsSize * sizeof(GfxFontCIDWidthExcep));
1016 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1017 if (obj3.arrayGet(k, &obj4)->isNum()) {
1018 widths.exceps[widths.nExceps].first = j;
1019 widths.exceps[widths.nExceps].last = j;
1020 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1024 error(-1, "Bad widths array in Type 0 font");
1030 error(-1, "Bad widths array in Type 0 font");
1036 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1042 if (desFontDict->lookup( "DW2", &obj1)->isArray() &&
1043 obj1.arrayGetLength() == 2) {
1044 if (obj1.arrayGet(0, &obj2)->isNum()) {
1045 widths.defVY = obj1.getNum() * 0.001;
1048 if (obj1.arrayGet(1, &obj2)->isNum()) {
1049 widths.defHeight = obj1.getNum() * 0.001;
1056 if (desFontDict->lookup( "W2", &obj1)->isArray()) {
1059 while (i + 1 < obj1.arrayGetLength()) {
1060 obj1.arrayGet(0, &obj2);
1061 obj2.arrayGet(0, &obj3);
1062 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1063 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1064 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1065 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1066 if (widths.nExcepsV == excepsSize) {
1068 widths.excepsV = (GfxFontCIDWidthExcepV *)
1069 grealloc(widths.excepsV,
1070 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1072 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1073 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1074 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1075 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1076 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1079 error(-1, "Bad widths (W2) array in Type 0 font");
1085 } else if (obj2.isInt() && obj3.isArray()) {
1086 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1088 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1089 widths.excepsV = (GfxFontCIDWidthExcepV *)
1090 grealloc(widths.excepsV,
1091 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1094 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1095 if (obj3.arrayGet(k, &obj4)->isNum() &&
1096 obj3.arrayGet(k, &obj5)->isNum() &&
1097 obj3.arrayGet(k, &obj6)->isNum()) {
1098 widths.excepsV[widths.nExceps].first = j;
1099 widths.excepsV[widths.nExceps].last = j;
1100 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1101 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1102 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1106 error(-1, "Bad widths (W2) array in Type 0 font");
1114 error(-1, "Bad widths (W2) array in Type 0 font");
1120 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1125 desFontDictObj.free();
1135 desFontDictObj.free();
1139GfxCIDFont::~GfxCIDFont() {
1146 gfree(widths.exceps);
1147 gfree(widths.excepsV);
1153int GfxCIDFont::getNextChar( char *s, int len, CharCode *code,
1154 Unicode *u, int uSize, int *uLen,
1155 double *dx, double *dy, double *ox, double *oy) {
1157 double w, h, vx, vy;
1167 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1169 *uLen = ctu->mapToUnicode(cid, u, uSize);
1175 if (cMap->getWMode() == 0) {
1176 w = widths.defWidth;
1178 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1184 if (widths.exceps[m].first <= cid) {
1190 if (cid <= widths.exceps[a].last) {
1191 w = widths.exceps[a].width;
1198 h = widths.defHeight;
1199 vx = widths.defWidth / 2;
1201 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1203 b = widths.nExcepsV;
1207 if (widths.excepsV[m].last <= cid) {
1213 if (cid <= widths.excepsV[a].last) {
1214 h = widths.excepsV[a].height;
1215 vx = widths.excepsV[a].vx;
1216 vy = widths.excepsV[a].vy;
1229int GfxCIDFont::getWMode() {
1230 return cMap ? cMap->getWMode() : 0;
1233CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1238GString *GfxCIDFont::getCollection() {
1239 return cMap ? cMap->getCollection() : (GString *)NULL;
1246GfxFontDict::GfxFontDict(XRef *xref, Dict *fontDict) {
1251 numFonts = fontDict->getLength();
1252 fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
1253 for (i = 0; i < numFonts; ++i) {
1254 fontDict->getValNF(i, &obj1);
1255 obj1.fetch(xref, &obj2);
1256 if (obj2.isDict()) {
1266 fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1268 if (fonts[i] && !fonts[i]->isOk()) {
1273 error(-1, "font resource is not a dictionary");
1281GfxFontDict::~GfxFontDict() {
1284 for (i = 0; i < numFonts; ++i) {
1292GfxFont *GfxFontDict::lookup( char *tag) {
1295 for (i = 0; i < numFonts; ++i) {
1296 if (fonts[i] && fonts[i]->matches(tag)) {
|