]> www.fi.muni.cz Git - evince.git/blobdiff - pdf/xpdf/GfxFont.cc
Add a few more toolbar items.
[evince.git] / pdf / xpdf / GfxFont.cc
index d1148cf212efc270471e9ec3a456435c2f34ca15..ed9f076d99105ace3aea48dfcf490ee031c115d6 100644 (file)
 //
 // GfxFont.cc
 //
-// Copyright 1996 Derek B. Noonburg
+// Copyright 1996-2003 Glyph & Cog, LLC
 //
 //========================================================================
 
-#ifdef __GNUC__
+#include <aconf.h>
+
+#ifdef USE_GCC_PRAGMAS
 #pragma implementation
 #endif
 
-#include <stdlib.h>
-#include <stddef.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
-#include "GString.h"
 #include "gmem.h"
-#include "gfile.h"
-#include "config.h"
+#include "Error.h"
 #include "Object.h"
-#include "Array.h"
 #include "Dict.h"
-#include "Error.h"
-#include "Params.h"
+#include "GlobalParams.h"
+#include "CMap.h"
+#include "CharCodeToUnicode.h"
+#include "FontEncodingTables.h"
+#include "BuiltinFontTables.h"
+#include "FoFiType1.h"
+#include "FoFiType1C.h"
+#include "FoFiTrueType.h"
 #include "GfxFont.h"
 
-#include "FontInfo.h"
-#if JAPANESE_SUPPORT
-#include "CMapInfo.h"
-#endif
-
 //------------------------------------------------------------------------
 
-static int CDECL cmpWidthExcep(const void *w1, const void *w2);
-
-//------------------------------------------------------------------------
+struct StdFontMapEntry {
+  char *altName;
+  char *properName;
+};
 
-static Gushort *defCharWidths[12] = {
-  courierWidths,
-  courierObliqueWidths,
-  courierBoldWidths,
-  courierBoldObliqueWidths,
-  helveticaWidths,
-  helveticaObliqueWidths,
-  helveticaBoldWidths,
-  helveticaBoldObliqueWidths,
-  timesRomanWidths,
-  timesItalicWidths,
-  timesBoldWidths,
-  timesBoldItalicWidths
+// Acrobat 4.0 and earlier substituted Base14-compatible fonts without
+// providing Widths and a FontDescriptor, so we munge the names into
+// the proper Base14 names.  This table is from implementation note 44
+// in the PDF 1.4 spec, with some additions based on empirical
+// evidence.
+static StdFontMapEntry stdFontMap[] = {
+  { "Arial",                        "Helvetica" },
+  { "Arial,Bold",                   "Helvetica-Bold" },
+  { "Arial,BoldItalic",             "Helvetica-BoldOblique" },
+  { "Arial,Italic",                 "Helvetica-Oblique" },
+  { "Arial-Bold",                   "Helvetica-Bold" },
+  { "Arial-BoldItalic",             "Helvetica-BoldOblique" },
+  { "Arial-BoldItalicMT",           "Helvetica-BoldOblique" },
+  { "Arial-BoldMT",                 "Helvetica-Bold" },
+  { "Arial-Italic",                 "Helvetica-Oblique" },
+  { "Arial-ItalicMT",               "Helvetica-Oblique" },
+  { "ArialMT",                      "Helvetica" },
+  { "Courier,Bold",                 "Courier-Bold" },
+  { "Courier,Italic",               "Courier-Oblique" },
+  { "Courier,BoldItalic",           "Courier-BoldOblique" },
+  { "CourierNew",                   "Courier" },
+  { "CourierNew,Bold",              "Courier-Bold" },
+  { "CourierNew,BoldItalic",        "Courier-BoldOblique" },
+  { "CourierNew,Italic",            "Courier-Oblique" },
+  { "CourierNew-Bold",              "Courier-Bold" },
+  { "CourierNew-BoldItalic",        "Courier-BoldOblique" },
+  { "CourierNew-Italic",            "Courier-Oblique" },
+  { "CourierNewPS-BoldItalicMT",    "Courier-BoldOblique" },
+  { "CourierNewPS-BoldMT",          "Courier-Bold" },
+  { "CourierNewPS-ItalicMT",        "Courier-Oblique" },
+  { "CourierNewPSMT",               "Courier" },
+  { "Helvetica,Bold",               "Helvetica-Bold" },
+  { "Helvetica,BoldItalic",         "Helvetica-BoldOblique" },
+  { "Helvetica,Italic",             "Helvetica-Oblique" },
+  { "Helvetica-BoldItalic",         "Helvetica-BoldOblique" },
+  { "Helvetica-Italic",             "Helvetica-Oblique" },
+  { "Symbol,Bold",                  "Symbol" },
+  { "Symbol,BoldItalic",            "Symbol" },
+  { "Symbol,Italic",                "Symbol" },
+  { "TimesNewRoman",                "Times-Roman" },
+  { "TimesNewRoman,Bold",           "Times-Bold" },
+  { "TimesNewRoman,BoldItalic",     "Times-BoldItalic" },
+  { "TimesNewRoman,Italic",         "Times-Italic" },
+  { "TimesNewRoman-Bold",           "Times-Bold" },
+  { "TimesNewRoman-BoldItalic",     "Times-BoldItalic" },
+  { "TimesNewRoman-Italic",         "Times-Italic" },
+  { "TimesNewRomanPS",              "Times-Roman" },
+  { "TimesNewRomanPS-Bold",         "Times-Bold" },
+  { "TimesNewRomanPS-BoldItalic",   "Times-BoldItalic" },
+  { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
+  { "TimesNewRomanPS-BoldMT",       "Times-Bold" },
+  { "TimesNewRomanPS-Italic",       "Times-Italic" },
+  { "TimesNewRomanPS-ItalicMT",     "Times-Italic" },
+  { "TimesNewRomanPSMT",            "Times-Roman" },
+  { "TimesNewRomanPSMT,Bold",       "Times-Bold" },
+  { "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
+  { "TimesNewRomanPSMT,Italic",     "Times-Italic" }
 };
 
 //------------------------------------------------------------------------
-// GfxFontEncoding
+// GfxFont
 //------------------------------------------------------------------------
 
-inline int GfxFontEncoding::hash(char *name) {
-  int h;
+GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
+  GString *nameA;
+  GfxFont *font;
+  Object obj1;
 
-  h = name[0];
-  if (name[1])
-    h = h * 61 + name[1];
-  return h % gfxFontEncHashSize;
-}
+  // get base font name
+  nameA = NULL;
+  fontDict->lookup("BaseFont", &obj1);
+  if (obj1.isName()) {
+    nameA = new GString(obj1.getName());
+  }
+  obj1.free();
 
-GfxFontEncoding::GfxFontEncoding() {
-  int i;
+  // get font type
+  font = NULL;
+  fontDict->lookup("Subtype", &obj1);
+  if (obj1.isName("Type1") || obj1.isName("MMType1")) {
+    font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
+  } else if (obj1.isName("Type1C")) {
+    font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
+  } else if (obj1.isName("Type3")) {
+    font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
+  } else if (obj1.isName("TrueType")) {
+    font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
+  } else if (obj1.isName("Type0")) {
+    font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
+  } else {
+    error(-1, "Unknown font type: '%s'",
+         obj1.isName() ? obj1.getName() : "???");
+    font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
+  }
+  obj1.free();
 
-  encoding = (char **)gmalloc(256 * sizeof(char *));
-  freeEnc = gTrue;
-  for (i = 0; i < 256; ++i)
-    encoding[i] = NULL;
-  for (i = 0; i < gfxFontEncHashSize; ++i)
-    hashTab[i] = -1;
+  return font;
 }
 
-GfxFontEncoding::GfxFontEncoding(char **encoding1, int encSize) {
-  int i;
+GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
+  ok = gFalse;
+  tag = new GString(tagA);
+  id = idA;
+  name = nameA;
+  origName = nameA;
+  embFontName = NULL;
+  extFontFile = NULL;
+}
 
-  encoding = encoding1;
-  freeEnc = gFalse;
-  for (i = 0; i < gfxFontEncHashSize; ++i)
-    hashTab[i] = -1;
-  for (i = 0; i < encSize; ++i) {
-    if (encoding[i])
-      addChar1(i, encoding[i]);
+GfxFont::~GfxFont() {
+  delete tag;
+  if (origName && origName != name) {
+    delete origName;
+  }
+  if (name) {
+    delete name;
+  }
+  if (embFontName) {
+    delete embFontName;
+  }
+  if (extFontFile) {
+    delete extFontFile;
   }
 }
 
-void GfxFontEncoding::addChar(int code, char *name) {
-  int h, i;
+void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
+  Object obj1, obj2, obj3, obj4;
+  double t;
+  int i;
+
+  // assume Times-Roman by default (for substitution purposes)
+  flags = fontSerif;
 
-  // replace character associated with code
-  if (encoding[code]) {
-    h = hash(encoding[code]);
-    for (i = 0; i < gfxFontEncHashSize; ++i) {
-      if (hashTab[h] == code) {
-       hashTab[h] = -2;
-       break;
+  embFontID.num = -1;
+  embFontID.gen = -1;
+  missingWidth = 0;
+
+  if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
+
+    // get flags
+    if (obj1.dictLookup("Flags", &obj2)->isInt()) {
+      flags = obj2.getInt();
+    }
+    obj2.free();
+
+    // get name
+    obj1.dictLookup("FontName", &obj2);
+    if (obj2.isName()) {
+      embFontName = new GString(obj2.getName());
+    }
+    obj2.free();
+
+    // look for embedded font file
+    if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
+      if (type == fontType1) {
+       embFontID = obj2.getRef();
+      } else {
+       error(-1, "Mismatch between font type and embedded font file");
       }
-      if (++h == gfxFontEncHashSize)
-       h = 0;
     }
-    gfree(encoding[code]);
-  }
+    obj2.free();
+    if (embFontID.num == -1 &&
+       obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
+      if (type == fontTrueType || type == fontCIDType2) {
+       embFontID = obj2.getRef();
+      } else {
+       error(-1, "Mismatch between font type and embedded font file");
+      }
+    }
+    obj2.free();
+    if (embFontID.num == -1 &&
+       obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
+      if (obj2.fetch(xref, &obj3)->isStream()) {
+       obj3.streamGetDict()->lookup("Subtype", &obj4);
+       if (obj4.isName("Type1")) {
+         if (type == fontType1) {
+           embFontID = obj2.getRef();
+         } else {
+           error(-1, "Mismatch between font type and embedded font file");
+         }
+       } else if (obj4.isName("Type1C")) {
+         if (type == fontType1) {
+           type = fontType1C;
+           embFontID = obj2.getRef();
+         } else if (type == fontType1C) {
+           embFontID = obj2.getRef();
+         } else {
+           error(-1, "Mismatch between font type and embedded font file");
+         }
+       } else if (obj4.isName("TrueType")) {
+         if (type == fontTrueType) {
+           embFontID = obj2.getRef();
+         } else {
+           error(-1, "Mismatch between font type and embedded font file");
+         }
+       } else if (obj4.isName("CIDFontType0C")) {
+         if (type == fontCIDType0) {
+           type = fontCIDType0C;
+           embFontID = obj2.getRef();
+         } else {
+           error(-1, "Mismatch between font type and embedded font file");
+         }
+       } else {
+         error(-1, "Unknown embedded font type '%s'",
+               obj4.isName() ? obj4.getName() : "???");
+       }
+       obj4.free();
+      }
+      obj3.free();
+    }
+    obj2.free();
+
+    // look for MissingWidth
+    obj1.dictLookup("MissingWidth", &obj2);
+    if (obj2.isNum()) {
+      missingWidth = obj2.getNum();
+    }
+    obj2.free();
+
+    // get Ascent and Descent
+    obj1.dictLookup("Ascent", &obj2);
+    if (obj2.isNum()) {
+      t = 0.001 * obj2.getNum();
+      // some broken font descriptors set ascent and descent to 0
+      if (t != 0) {
+       ascent = t;
+      }
+    }
+    obj2.free();
+    obj1.dictLookup("Descent", &obj2);
+    if (obj2.isNum()) {
+      t = 0.001 * obj2.getNum();
+      // some broken font descriptors set ascent and descent to 0
+      if (t != 0) {
+       descent = t;
+      }
+      // some broken font descriptors specify a positive descent
+      if (descent > 0) {
+       descent = -descent;
+      }
+    }
+    obj2.free();
 
-  // associate name with code
-  encoding[code] = name;
+    // font FontBBox
+    if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
+      for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
+       if (obj2.arrayGet(i, &obj3)->isNum()) {
+         fontBBox[i] = 0.001 * obj3.getNum();
+       }
+       obj3.free();
+      }
+    }
+    obj2.free();
 
-  // insert name in hash table
-  addChar1(code, name);
+  }
+  obj1.free();
 }
 
-void GfxFontEncoding::addChar1(int code, char *name) {
-  int h, i, code2;
-
-  // insert name in hash table
-  h = hash(name); 
-  for (i = 0; i < gfxFontEncHashSize; ++i) {
-    code2 = hashTab[h];
-    if (code2 < 0) {
-      hashTab[h] = code;
-      break;
-    } else if (encoding[code2] && !strcmp(encoding[code2], name)) {
-      // keep the highest code for each char -- this is needed because
-      // X won't display chars with codes < 32
-      if (code > code2)
-       hashTab[h] = code;
-      break;
-    }
-    if (++h == gfxFontEncHashSize)
-      h = 0;
+CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
+                                             CharCodeToUnicode *ctu) {
+  GString *buf;
+  Object obj1;
+  int c;
+
+  if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
+    obj1.free();
+    return NULL;
   }
+  buf = new GString();
+  obj1.streamReset();
+  while ((c = obj1.streamGetChar()) != EOF) {
+    buf->append(c);
+  }
+  obj1.streamClose();
+  obj1.free();
+  if (ctu) {
+    ctu->mergeCMap(buf, nBits);
+  } else {
+    ctu = CharCodeToUnicode::parseCMap(buf, nBits);
+  }
+  delete buf;
+  return ctu;
 }
 
-GfxFontEncoding::~GfxFontEncoding() {
-  int i;
+void GfxFont::findExtFontFile() {
+  static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
+  static char *ttExts[] = { ".ttf", NULL };
 
-  if (freeEnc) {
-    for (i = 0; i < 256; ++i) {
-      if (encoding[i])
-       gfree(encoding[i]);
+  if (name) {
+    if (type == fontType1) {
+      extFontFile = globalParams->findFontFile(name, type1Exts);
+    } else if (type == fontTrueType) {
+      extFontFile = globalParams->findFontFile(name, ttExts);
     }
-    gfree(encoding);
   }
 }
 
-int GfxFontEncoding::getCharCode(char *name) {
-  int h, i, code;
+char *GfxFont::readExtFontFile(int *len) {
+  FILE *f;
+  char *buf;
 
-  h = hash(name);
-  for (i = 0; i < gfxFontEncHashSize; ++i) {
-    code = hashTab[h];
-    if (code == -1 ||
-       (code > 0 && encoding[code] && !strcmp(encoding[code], name)))
-      return code;
-    if (++h >= gfxFontEncHashSize)
-      h = 0;
+  if (!(f = fopen(extFontFile->getCString(), "rb"))) {
+    error(-1, "External font file '%s' vanished", extFontFile->getCString());
+    return NULL;
   }
-  return -1;
+  fseek(f, 0, SEEK_END);
+  *len = (int)ftell(f);
+  fseek(f, 0, SEEK_SET);
+  buf = (char *)gmalloc(*len);
+  if ((int)fread(buf, 1, *len, f) != *len) {
+    error(-1, "Error reading external font file '%s'",
+         extFontFile->getCString());
+  }
+  fclose(f);
+  return buf;
+}
+
+char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
+  char *buf;
+  Object obj1, obj2;
+  Stream *str;
+  int c;
+  int size, i;
+
+  obj1.initRef(embFontID.num, embFontID.gen);
+  obj1.fetch(xref, &obj2);
+  if (!obj2.isStream()) {
+    error(-1, "Embedded font file is not a stream");
+    obj2.free();
+    obj1.free();
+    embFontID.num = -1;
+    return NULL;
+  }
+  str = obj2.getStream();
+
+  buf = NULL;
+  i = size = 0;
+  str->reset();
+  while ((c = str->getChar()) != EOF) {
+    if (i == size) {
+      size += 4096;
+      buf = (char *)grealloc(buf, size);
+    }
+    buf[i++] = c;
+  }
+  *len = i;
+  str->close();
+
+  obj2.free();
+  obj1.free();
+
+  return buf;
 }
 
 //------------------------------------------------------------------------
-// GfxFont
+// Gfx8BitFont
 //------------------------------------------------------------------------
 
-GfxFont::GfxFont(char *tag1, Ref id1, Dict *fontDict) {
+Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
+                        GfxFontType typeA, Dict *fontDict):
+  GfxFont(tagA, idA, nameA)
+{
   BuiltinFont *builtinFont;
-  char buf[256];
+  char **baseEnc;
+  GBool baseEncFromFontFile;
+  char *buf;
+  int len;
+  FoFiType1 *ffT1;
+  FoFiType1C *ffT1C;
+  int code, code2;
+  char *charName;
+  GBool missing, hex;
+  Unicode toUnicode[256];
+  CharCodeToUnicode *utu, *ctu2;
+  Unicode uBuf[8];
+  double mul;
+  int firstChar, lastChar;
+  Gushort w;
   Object obj1, obj2, obj3;
-  char *p1, *p2;
-  int i;
+  int n, i, a, b, m;
 
-  // get font tag and ID
-  tag = new GString(tag1);
-  id = id1;
+  type = typeA;
+  ctu = NULL;
 
-  // get base font name
-  name = NULL;
-  fontDict->lookup("BaseFont", &obj1);
-  if (obj1.isName())
-    name = new GString(obj1.getName());
-  obj1.free();
+  // do font name substitution for various aliases of the Base 14 font
+  // names
+  if (name) {
+    a = 0;
+    b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
+    // invariant: stdFontMap[a].altName <= name < stdFontMap[b].altName
+    while (b - a > 1) {
+      m = (a + b) / 2;
+      if (name->cmp(stdFontMap[m].altName) >= 0) {
+       a = m;
+      } else {
+       b = m;
+      }
+    }
+    if (!name->cmp(stdFontMap[a].altName)) {
+      name = new GString(stdFontMap[a].properName);
+    }
+  }
 
   // is it a built-in font?
   builtinFont = NULL;
   if (name) {
-    for (i = 0; i < numBuiltinFonts; ++i) {
-      if (!strcmp(builtinFonts[i].name, name->getCString())) {
+    for (i = 0; i < nBuiltinFonts; ++i) {
+      if (!name->cmp(builtinFonts[i].name)) {
        builtinFont = &builtinFonts[i];
        break;
       }
     }
   }
 
-  // get font type
-  type = fontUnknownType;
-  fontDict->lookup("Subtype", &obj1);
-  if (obj1.isName("Type1"))
-    type = fontType1;
-  else if (obj1.isName("Type3"))
-    type = fontType3;
-  else if (obj1.isName("TrueType"))
-    type = fontTrueType;
-  else if (obj1.isName("Type0"))
-    type = fontType0;
-  obj1.free();
-  is16 = gFalse;
+  // default ascent/descent values
+  if (builtinFont) {
+    ascent = 0.001 * builtinFont->ascent;
+    descent = 0.001 * builtinFont->descent;
+    fontBBox[0] = 0.001 * builtinFont->bbox[0];
+    fontBBox[1] = 0.001 * builtinFont->bbox[1];
+    fontBBox[2] = 0.001 * builtinFont->bbox[2];
+    fontBBox[3] = 0.001 * builtinFont->bbox[3];
+  } else {
+    ascent = 0.95;
+    descent = -0.35;
+    fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
+  }
 
   // get info from font descriptor
-  // for flags: assume Times-Roman (or TimesNewRoman), but
-  // explicitly check for Arial and CourierNew -- certain PDF
-  // generators apparently don't include FontDescriptors for Arial,
-  // TimesNewRoman, and CourierNew
-  flags = fontSerif;   // assume Times-Roman by default
-  if (type == fontTrueType && !name->cmp("Arial"))
-    flags = 0;
-  else if (type == fontTrueType && !name->cmp("CourierNew"))
-    flags = fontFixedWidth;
-  embFontID.num = -1;
-  embFontID.gen = -1;
-  embFontName = NULL;
-  extFontFile = NULL;
-  fontDict->lookup("FontDescriptor", &obj1);
-  if (obj1.isDict()) {
+  readFontDescriptor(xref, fontDict);
 
-    // flags
-    obj1.dictLookup("Flags", &obj2);
-    if (obj2.isInt())
-      flags = obj2.getInt();
-    obj2.free();
-
-    // embedded Type 1 font file and font name
-    if (type == fontType1) {
-      obj1.dictLookupNF("FontFile", &obj2);
-      if (obj2.isRef()) {
-       embFontID = obj2.getRef();
-
-       // get font name from the font file itself since font subsets
-       // sometimes use the 'AAAAAA+foo' name and sometimes use just 'foo'
-       obj2.fetch(&obj3);
-       if (obj3.isStream()) {
-         obj3.streamReset();
-         for (i = 0; i < 64; ++i) {
-           obj3.streamGetLine(buf, sizeof(buf));
-           if (!strncmp(buf, "/FontName", 9)) {
-             if ((p1 = strchr(buf+9, '/'))) {
-               ++p1;
-               for (p2 = p1; *p2 && !isspace(*p2); ++p2) ;
-               embFontName = new GString(p1, p2 - p1);
-             }
-             break;
-           }
-         }
-       }
-       obj3.free();
-       obj2.free();
-
-       // couldn't find font name so just use the one in the PDF font
-       // descriptor
-       if (!embFontName) {
-         obj1.dictLookup("FontName", &obj2);
-         if (obj2.isName())
-           embFontName = new GString(obj2.getName());
-       }
-      }
-      obj2.free();
-
-    // embedded TrueType font file
-    } else if (type == fontTrueType) {
-      obj1.dictLookupNF("FontFile2", &obj2);
-      if (obj2.isRef())
-       embFontID = obj2.getRef();
-      obj2.free();
-    }
-  }
-  obj1.free();
+  // look for an external font file
+  findExtFontFile();
 
   // get font matrix
   fontMat[0] = fontMat[3] = 1;
   fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
   if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
     for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
-      if (obj1.arrayGet(i, &obj2)->isNum())
+      if (obj1.arrayGet(i, &obj2)->isNum()) {
        fontMat[i] = obj2.getNum();
+      }
       obj2.free();
     }
   }
   obj1.free();
 
-  // get encoding and character widths
-  if (type == fontType0) {
-    getType0EncAndWidths(fontDict);
-  } else if (builtinFont) {
-    makeEncoding(fontDict, builtinFont->encoding);
-    makeWidths(fontDict, builtinFont->encoding, builtinFont->widths);
-  } else {
-    makeEncoding(fontDict, NULL);
-    makeWidths(fontDict, NULL, NULL);
+  // get Type 3 bounding box, font definition, and resources
+  if (type == fontType3) {
+    if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
+      for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
+       if (obj1.arrayGet(i, &obj2)->isNum()) {
+         fontBBox[i] = obj2.getNum();
+       }
+       obj2.free();
+      }
+    }
+    obj1.free();
+    if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
+      error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
+      charProcs.free();
+    }
+    if (!fontDict->lookup("Resources", &resources)->isDict()) {
+      resources.free();
+    }
   }
-}
-
-GfxFont::~GfxFont() {
-  delete tag;
-  if (name)
-    delete name;
-  if (!is16 && encoding)
-    delete encoding;
-  if (embFontName)
-    delete embFontName;
-  if (extFontFile)
-    delete extFontFile;
-  if (is16)
-    gfree(widths16.exceps);
-}
-
-double GfxFont::getWidth(GString *s) {
-  double w;
-  int i;
 
-  w = 0;
-  for (i = 0; i < s->getLength(); ++i)
-    w += widths[s->getChar(i) & 0xff];
-  return w;
-}
+  //----- build the font encoding -----
+
+  // Encodings start with a base encoding, which can come from
+  // (in order of priority):
+  //   1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
+  //        - MacRoman / MacExpert / WinAnsi / Standard
+  //   2. embedded or external font file
+  //   3. default:
+  //        - builtin --> builtin encoding
+  //        - TrueType --> MacRomanEncoding
+  //        - others --> StandardEncoding
+  // and then add a list of differences (if any) from
+  // FontDict.Encoding.Differences.
+
+  // check FontDict for base encoding
+  hasEncoding = gFalse;
+  usesMacRomanEnc = gFalse;
+  baseEnc = NULL;
+  baseEncFromFontFile = gFalse;
+  fontDict->lookup("Encoding", &obj1);
+  if (obj1.isDict()) {
+    obj1.dictLookup("BaseEncoding", &obj2);
+    if (obj2.isName("MacRomanEncoding")) {
+      hasEncoding = gTrue;
+      usesMacRomanEnc = gTrue;
+      baseEnc = macRomanEncoding;
+    } else if (obj2.isName("MacExpertEncoding")) {
+      hasEncoding = gTrue;
+      baseEnc = macExpertEncoding;
+    } else if (obj2.isName("WinAnsiEncoding")) {
+      hasEncoding = gTrue;
+      baseEnc = winAnsiEncoding;
+    } else if (obj2.isName("StandardEncoding")) {
+      hasEncoding = gTrue;
+      baseEnc = standardEncoding;
+    }
+    obj2.free();
+  } else if (obj1.isName("MacRomanEncoding")) {
+    hasEncoding = gTrue;
+    usesMacRomanEnc = gTrue;
+    baseEnc = macRomanEncoding;
+  } else if (obj1.isName("MacExpertEncoding")) {
+    hasEncoding = gTrue;
+    baseEnc = macExpertEncoding;
+  } else if (obj1.isName("WinAnsiEncoding")) {
+    hasEncoding = gTrue;
+    baseEnc = winAnsiEncoding;
+  } else if (obj1.isName("StandardEncoding")) {
+    hasEncoding = gTrue;
+    baseEnc = standardEncoding;
+  }
 
-double GfxFont::getWidth16(int c) {
-  double w;
-  int a, b, m;
-
-  w = widths16.defWidth;
-  a = -1;
-  b = widths16.numExceps;
-  // invariant: widths16.exceps[a].last < c < widths16.exceps[b].first
-  while (b - a > 1) {
-    m = (a + b) / 2;
-    if (widths16.exceps[m].last < c) {
-      a = m;
-    } else if (c < widths16.exceps[m].first) {
-      b = m;
+  // check embedded or external font file for base encoding
+  // (only for Type 1 fonts - trying to get an encoding out of a
+  // TrueType font is a losing proposition)
+  ffT1 = NULL;
+  ffT1C = NULL;
+  buf = NULL;
+  if (type == fontType1 && (extFontFile || embFontID.num >= 0)) {
+    if (extFontFile) {
+      ffT1 = FoFiType1::load(extFontFile->getCString());
+    } else {
+      buf = readEmbFontFile(xref, &len);
+      ffT1 = FoFiType1::make(buf, len);
+    }
+    if (ffT1) {
+      if (ffT1->getName()) {
+       if (embFontName) {
+         delete embFontName;
+       }
+       embFontName = new GString(ffT1->getName());
+      }
+      if (!baseEnc) {
+       baseEnc = ffT1->getEncoding();
+       baseEncFromFontFile = gTrue;
+      }
+    }
+  } else if (type == fontType1C && (extFontFile || embFontID.num >= 0)) {
+    if (extFontFile) {
+      ffT1C = FoFiType1C::load(extFontFile->getCString());
     } else {
-      w = widths16.exceps[m].width;
-      break;
+      buf = readEmbFontFile(xref, &len);
+      ffT1C = FoFiType1C::make(buf, len);
+    }
+    if (ffT1C) {
+      if (ffT1C->getName()) {
+       if (embFontName) {
+         delete embFontName;
+       }
+       embFontName = new GString(ffT1C->getName());
+      }
+      if (!baseEnc) {
+       baseEnc = ffT1C->getEncoding();
+       baseEncFromFontFile = gTrue;
+      }
     }
   }
-  return w;
-}
-
-double GfxFont::getWidth16(GString *s) {
-  double w;
-  int c;
-  int i;
-
-  w = 0;
-  for (i = 0; i < s->getLength(); i += 2) {
-    c = (s->getChar(i) << 8) + s->getChar(i+1);
-    w += getWidth16(c);
+  if (buf) {
+    gfree(buf);
   }
-  return w;
-}
 
-void GfxFont::makeEncoding(Dict *fontDict, GfxFontEncoding *builtinEncoding) {
-  GfxFontEncoding *baseEnc;
-  Object obj1, obj2, obj3;
-  char *charName;
-  int code, i;
+  // get default base encoding
+  if (!baseEnc) {
+    if (builtinFont) {
+      baseEnc = builtinFont->defaultBaseEnc;
+      hasEncoding = gTrue;
+    } else if (type == fontTrueType) {
+      baseEnc = winAnsiEncoding;
+    } else {
+      baseEnc = standardEncoding;
+    }
+  }
 
-  // start with empty encoding
-  encoding = new GfxFontEncoding();
+  // copy the base encoding
+  for (i = 0; i < 256; ++i) {
+    enc[i] = baseEnc[i];
+    if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
+      enc[i] = copyString(baseEnc[i]);
+    }
+  }
 
-  // get encoding from font dict
-  fontDict->lookup("Encoding", &obj1);
+  // some Type 1C font files have empty encodings, which can break the
+  // T1C->T1 conversion (since the 'seac' operator depends on having
+  // the accents in the encoding), so we fill in any gaps from
+  // StandardEncoding
+  if (type == fontType1C && (extFontFile || embFontID.num >= 0) &&
+      baseEncFromFontFile) {
+    for (i = 0; i < 256; ++i) {
+      if (!enc[i] && standardEncoding[i]) {
+       enc[i] = standardEncoding[i];
+       encFree[i] = gFalse;
+      }
+    }
+  }
 
-  // encoding specified by dictionary
+  // merge differences into encoding
   if (obj1.isDict()) {
-    obj1.dictLookup("BaseEncoding", &obj2);
-    baseEnc = makeEncoding1(obj2, fontDict, builtinEncoding);
-    obj2.free();
     obj1.dictLookup("Differences", &obj2);
     if (obj2.isArray()) {
+      hasEncoding = gTrue;
       code = 0;
       for (i = 0; i < obj2.arrayGetLength(); ++i) {
        obj2.arrayGet(i, &obj3);
        if (obj3.isInt()) {
          code = obj3.getInt();
        } else if (obj3.isName()) {
-         if (code < 256)
-           encoding->addChar(code, copyString(obj3.getName()));
+         if (code >= 0 && code < 256) {
+           if (encFree[code]) {
+             gfree(enc[code]);
+           }
+           enc[code] = copyString(obj3.getName());
+           encFree[code] = gTrue;
+         }
          ++code;
        } else {
          error(-1, "Wrong type in font encoding resource differences (%s)",
@@ -397,402 +666,816 @@ void GfxFont::makeEncoding(Dict *fontDict, GfxFontEncoding *builtinEncoding) {
       }
     }
     obj2.free();
-
-  // encoding specified by name or null
-  } else {
-    baseEnc = makeEncoding1(obj1, fontDict, builtinEncoding);
   }
-
-  // free the font dict encoding
   obj1.free();
+  if (ffT1) {
+    delete ffT1;
+  }
+  if (ffT1C) {
+    delete ffT1C;
+  }
+
+  //----- build the mapping to Unicode -----
 
-  // merge base encoding and differences;
+  // pass 1: use the name-to-Unicode mapping table
+  missing = hex = gFalse;
   for (code = 0; code < 256; ++code) {
-    if (!encoding->getCharName(code)) {
-      if ((charName = baseEnc->getCharName(code)))
-       encoding->addChar(code, copyString(charName));
+    if ((charName = enc[code])) {
+      if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
+         strcmp(charName, ".notdef")) {
+       // if it wasn't in the name-to-Unicode table, check for a
+       // name that looks like 'Axx' or 'xx', where 'A' is any letter
+       // and 'xx' is two hex digits
+       if ((strlen(charName) == 3 &&
+            isalpha(charName[0]) &&
+            isxdigit(charName[1]) && isxdigit(charName[2]) &&
+            ((charName[1] >= 'a' && charName[1] <= 'f') ||
+             (charName[1] >= 'A' && charName[1] <= 'F') ||
+             (charName[2] >= 'a' && charName[2] <= 'f') ||
+             (charName[2] >= 'A' && charName[2] <= 'F'))) ||
+           (strlen(charName) == 2 &&
+            isxdigit(charName[0]) && isxdigit(charName[1]) &&
+            ((charName[0] >= 'a' && charName[0] <= 'f') ||
+             (charName[0] >= 'A' && charName[0] <= 'F') ||
+             (charName[1] >= 'a' && charName[1] <= 'f') ||
+             (charName[1] >= 'A' && charName[1] <= 'F')))) {
+         hex = gTrue;
+       }
+       missing = gTrue;
+      }
+    } else {
+      toUnicode[code] = 0;
     }
   }
-}
 
-GfxFontEncoding *GfxFont::makeEncoding1(Object obj, Dict *fontDict,
-                                       GfxFontEncoding *builtinEncoding) {
-  GfxFontEncoding *enc;
-  GBool haveEncoding;
-  Object obj1, obj2;
-  char **path;
-  FILE *f;
-  FileStream *str;
+  // pass 2: try to fill in the missing chars, looking for names of
+  // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
+  // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
+  // decimal digits
+  if (missing && globalParams->getMapNumericCharNames()) {
+    for (code = 0; code < 256; ++code) {
+      if ((charName = enc[code]) && !toUnicode[code] &&
+         strcmp(charName, ".notdef")) {
+       n = strlen(charName);
+       code2 = -1;
+       if (hex && n == 3 && isalpha(charName[0]) &&
+           isxdigit(charName[1]) && isxdigit(charName[2])) {
+         sscanf(charName+1, "%x", &code2);
+       } else if (hex && n == 2 &&
+                  isxdigit(charName[0]) && isxdigit(charName[1])) {
+         sscanf(charName, "%x", &code2);
+       } else if (!hex && n >= 2 && n <= 4 &&
+                  isdigit(charName[0]) && isdigit(charName[1])) {
+         code2 = atoi(charName);
+       } else if (n >= 3 && n <= 5 &&
+                  isdigit(charName[1]) && isdigit(charName[2])) {
+         code2 = atoi(charName+1);
+       } else if (n >= 4 && n <= 6 &&
+                  isdigit(charName[2]) && isdigit(charName[3])) {
+         code2 = atoi(charName+2);
+       }
+       if (code2 >= 0 && code2 <= 0xff) {
+         toUnicode[code] = (Unicode)code2;
+       }
+      }
+    }
+  }
 
-  // MacRoman, WinAnsi, or Standard encoding
-  if (obj.isName("MacRomanEncoding")) {
-    enc = &macRomanEncoding;
-  } else if (obj.isName("WinAnsiEncoding")) {
-    enc = &winAnsiEncoding;
-  } else if (obj.isName("StandardEncoding")) {
-    enc = &standardEncoding;
+  // construct the char code -> Unicode mapping object
+  ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
 
-  // use the built-in font encoding if possible
-  } else if (builtinEncoding) {
-    enc = builtinEncoding;
+  // merge in a ToUnicode CMap, if there is one -- this overwrites
+  // existing entries in ctu, i.e., the ToUnicode CMap takes
+  // precedence, but the other encoding info is allowed to fill in any
+  // holes
+  readToUnicodeCMap(fontDict, 8, ctu);
 
-  // check font type
-  } else {
+  // look for a Unicode-to-Unicode mapping
+  if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
+    for (i = 0; i < 256; ++i) {
+      toUnicode[i] = 0;
+    }
+    ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
+    for (i = 0; i < 256; ++i) {
+      n = ctu->mapToUnicode((CharCode)i, uBuf, 8);
+      if (n >= 1) {
+       n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
+       if (n >= 1) {
+         ctu2->setMapping((CharCode)i, uBuf, n);
+       }
+      }
+    }
+    utu->decRefCnt();
+    delete ctu;
+    ctu = ctu2;
+  }
 
-    // Type 1 font: try to get encoding from font file
-    if (type == fontType1) {
+  //----- get the character widths -----
 
-      // default to using standard encoding
-      enc = &standardEncoding;
-
-      // is there an external font file?
-      haveEncoding = gFalse;
-      if (name) {
-       for (path = fontPath; *path; ++path) {
-         extFontFile = appendToPath(new GString(*path), name->getCString());
-         f = fopen(extFontFile->getCString(), "rb");
-         if (!f) {
-           extFontFile->append(".pfb");
-           f = fopen(extFontFile->getCString(), "rb");
-         }
-         if (!f) {
-           extFontFile->del(extFontFile->getLength() - 4, 4);
-           extFontFile->append(".pfa");
-           f = fopen(extFontFile->getCString(), "rb");
-         }
-         if (f) {
-           obj1.initNull();
-           str = new FileStream(f, 0, -1, &obj1);
-           getType1Encoding(str);
-           delete str;
-           fclose(f);
-           haveEncoding = gTrue;
-           break;
-         }
-         delete extFontFile;
-         extFontFile = NULL;
+  // initialize all widths
+  for (code = 0; code < 256; ++code) {
+    widths[code] = missingWidth * 0.001;
+  }
+
+  // use widths from font dict, if present
+  fontDict->lookup("FirstChar", &obj1);
+  firstChar = obj1.isInt() ? obj1.getInt() : 0;
+  obj1.free();
+  if (firstChar < 0 || firstChar > 255) {
+    firstChar = 0;
+  }
+  fontDict->lookup("LastChar", &obj1);
+  lastChar = obj1.isInt() ? obj1.getInt() : 255;
+  obj1.free();
+  if (lastChar < 0 || lastChar > 255) {
+    lastChar = 255;
+  }
+  mul = (type == fontType3) ? fontMat[0] : 0.001;
+  fontDict->lookup("Widths", &obj1);
+  if (obj1.isArray()) {
+    flags |= fontFixedWidth;
+    if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
+      lastChar = firstChar + obj1.arrayGetLength() - 1;
+    }
+    for (code = firstChar; code <= lastChar; ++code) {
+      obj1.arrayGet(code - firstChar, &obj2);
+      if (obj2.isNum()) {
+       widths[code] = obj2.getNum() * mul;
+       if (widths[code] != widths[firstChar]) {
+         flags &= ~fontFixedWidth;
        }
       }
+      obj2.free();
+    }
 
-      // is there an embedded font file?
-      // (this has to be checked after the external font because
-      // XOutputDev needs the encoding from the external font)
-      if (!haveEncoding && embFontID.num >= 0) {
-       obj1.initRef(embFontID.num, embFontID.gen);
-       obj1.fetch(&obj2);
-       if (obj2.isStream())
-         getType1Encoding(obj2.getStream());
-       obj2.free();
-       obj1.free();
+  // use widths from built-in font
+  } else if (builtinFont) {
+    // this is a kludge for broken PDF files that encode char 32
+    // as .notdef
+    if (builtinFont->widths->getWidth("space", &w)) {
+      widths[32] = 0.001 * w;
+    }
+    for (code = 0; code < 256; ++code) {
+      if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
+       widths[code] = 0.001 * w;
       }
+    }
 
-    // TrueType font: use Mac encoding
-    } else if (type == fontTrueType) {
-      enc = &macRomanEncoding;
-
-    // not Type 1 or TrueType: just use the standard encoding
+  // couldn't find widths -- use defaults 
+  } else {
+    // this is technically an error -- the Widths entry is required
+    // for all but the Base-14 fonts -- but certain PDF generators
+    // apparently don't include widths for Arial and TimesNewRoman
+    if (isFixedWidth()) {
+      i = 0;
+    } else if (isSerif()) {
+      i = 8;
     } else {
-      enc = &standardEncoding;
+      i = 4;
+    }
+    if (isBold()) {
+      i += 2;
+    }
+    if (isItalic()) {
+      i += 1;
+    }
+    builtinFont = builtinFontSubst[i];
+    // this is a kludge for broken PDF files that encode char 32
+    // as .notdef
+    if (builtinFont->widths->getWidth("space", &w)) {
+      widths[32] = 0.001 * w;
+    }
+    for (code = 0; code < 256; ++code) {
+      if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
+       widths[code] = 0.001 * w;
+      }
     }
   }
+  obj1.free();
 
-  return enc;
+  ok = gTrue;
 }
 
-void GfxFont::getType1Encoding(Stream *str) {
-  char buf[256];
-  char *p;
-  GBool found;
-  int code, i;
+Gfx8BitFont::~Gfx8BitFont() {
+  int i;
 
-  // look for encoding in font file
-  str->reset();
-  found = gFalse;
-  for (i = 0; i < 100; ++i) {
-    if (!str->getLine(buf, sizeof(buf)))
-      break;
-    if (!strncmp(buf, "/Encoding StandardEncoding def", 30))
-      break;
-    if (!strncmp(buf, "/Encoding 256 array", 19)) {
-      found = gTrue;
-      break;
-    }
-  }
-
-  // found the encoding, grab it
-  if (found) {
-    for (i = 0; i < 300; ++i) {
-      if (!str->getLine(buf, sizeof(buf)))
-       break;
-      p = strtok(buf, " \t");
-      if (p && !strcmp(p, "dup")) {
-       if ((p = strtok(NULL, " \t"))) {
-         code = atoi(p);
-         if ((p = strtok(NULL, " \t"))) {
-           if (p[0] == '/')
-             encoding->addChar(code, copyString(p+1));
-         }
-       }
-      }
+  for (i = 0; i < 256; ++i) {
+    if (encFree[i] && enc[i]) {
+      gfree(enc[i]);
     }
-    //~ look for getinterval/putinterval junk
+  }
+  ctu->decRefCnt();
+  if (charProcs.isDict()) {
+    charProcs.free();
+  }
+  if (resources.isDict()) {
+    resources.free();
   }
 }
 
-void GfxFont::makeWidths(Dict *fontDict, GfxFontEncoding *builtinEncoding,
-                        Gushort *builtinWidths) {
-  Object obj1, obj2;
-  int firstChar, lastChar;
-  int code, code2;
+int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
+                            Unicode *u, int uSize, int *uLen,
+                            double *dx, double *dy, double *ox, double *oy) {
+  CharCode c;
+
+  *code = c = (CharCode)(*s & 0xff);
+  *uLen = ctu->mapToUnicode(c, u, uSize);
+  *dx = widths[c];
+  *dy = *ox = *oy = 0;
+  return 1;
+}
+
+CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
+  ctu->incRefCnt();
+  return ctu;
+}
+
+Gushort *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
+  Gushort *map;
+  int cmapPlatform, cmapEncoding;
+  int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
+  GBool useMacRoman, useUnicode;
   char *charName;
-  Gushort *defWidths;
-  int index;
-  double mult;
+  Unicode u;
+  int code, i, n;
+
+  map = (Gushort *)gmalloc(256 * sizeof(Gushort));
+  for (i = 0; i < 256; ++i) {
+    map[i] = 0;
+  }
 
-  // initialize all widths to zero
-  for (code = 0; code < 256; ++code)
-    widths[code] = 0;
+  // To match up with the Adobe-defined behaviour, we choose a cmap
+  // like this:
+  // 1. If the PDF font has an encoding:
+  //    1a. If the PDF font specified MacRomanEncoding and the
+  //        TrueType font has a Macintosh Roman cmap, use it, and
+  //        reverse map the char names through MacRomanEncoding to
+  //        get char codes.
+  //    1b. If the TrueType font has a Microsoft Unicode cmap or a
+  //        non-Microsoft Unicode cmap, use it, and use the Unicode
+  //        indexes, not the char codes.
+  //    1c. If the PDF font is symbolic and the TrueType font has a
+  //        Microsoft Symbol cmap, use it, and use char codes
+  //        directly (possibly with an offset of 0xf000).
+  //    1d. If the TrueType font has a Macintosh Roman cmap, use it,
+  //        as in case 1a.
+  // 2. If the PDF font does not have an encoding:
+  //    2a. If the TrueType font has a Macintosh Roman cmap, use it,
+  //        and use char codes directly (possibly with an offset of
+  //        0xf000).
+  //    2b. If the TrueType font has a Microsoft Symbol cmap, use it,
+  //        and use char codes directly (possible with an offset of
+  //        0xf000).
+  // 3. If none of these rules apply, use the first cmap and hope for
+  //    the best (this shouldn't happen).
+  unicodeCmap = macRomanCmap = msSymbolCmap = -1;
+  for (i = 0; i < ff->getNumCmaps(); ++i) {
+    cmapPlatform = ff->getCmapPlatform(i);
+    cmapEncoding = ff->getCmapEncoding(i);
+    if ((cmapPlatform == 3 && cmapEncoding == 1) ||
+       cmapPlatform == 0) {
+      unicodeCmap = i;
+    } else if (cmapPlatform == 1 && cmapEncoding == 0) {
+      macRomanCmap = i;
+    } else if (cmapPlatform == 3 && cmapEncoding == 0) {
+      msSymbolCmap = i;
+    }
+  }
+  cmap = 0;
+  useMacRoman = gFalse;
+  useUnicode = gFalse;
+  if (hasEncoding) {
+    if (usesMacRomanEnc && macRomanCmap >= 0) {
+      cmap = macRomanCmap;
+      useMacRoman = gTrue;
+    } else if (unicodeCmap >= 0) {
+      cmap = unicodeCmap;
+      useUnicode = gTrue;
+    } else if ((flags & fontSymbolic) && msSymbolCmap >= 0) {
+      cmap = msSymbolCmap;
+    } else if (macRomanCmap >= 0) {
+      cmap = macRomanCmap;
+      useMacRoman = gTrue;
+    }
+  } else {
+    if (macRomanCmap >= 0) {
+      cmap = macRomanCmap;
+    } else if (msSymbolCmap >= 0) {
+      cmap = msSymbolCmap;
+    }
+  }
 
-  // use widths from built-in font
-  if (builtinEncoding) {
-    code2 = 0; // to make gcc happy
-    for (code = 0; code < 256; ++code) {
-      if ((charName = encoding->getCharName(code)) &&
-         (code2 = builtinEncoding->getCharCode(charName)) >= 0)
-       widths[code] = builtinWidths[code2] * 0.001;
+  // reverse map the char names through MacRomanEncoding, then map the
+  // char codes through the cmap
+  if (useMacRoman) {
+    for (i = 0; i < 256; ++i) {
+      if ((charName = enc[i])) {
+       if ((code = globalParams->getMacRomanCharCode(charName))) {
+         map[i] = ff->mapCodeToGID(cmap, code);
+       }
+      }
     }
 
-  // get widths from font dict
-  } else {
-    fontDict->lookup("FirstChar", &obj1);
-    firstChar = obj1.isInt() ? obj1.getInt() : 0;
-    obj1.free();
-    fontDict->lookup("LastChar", &obj1);
-    lastChar = obj1.isInt() ? obj1.getInt() : 255;
-    obj1.free();
-    if (type == fontType3)
-      mult = fontMat[0];
-    else
-      mult = 0.001;
-    fontDict->lookup("Widths", &obj1);
-    if (obj1.isArray()) {
-      for (code = firstChar; code <= lastChar; ++code) {
-       obj1.arrayGet(code - firstChar, &obj2);
-       if (obj2.isNum())
-         widths[code] = obj2.getNum() * mult;
-       obj2.free();
+  // map Unicode through the cmap
+  } else if (useUnicode) {
+    for (i = 0; i < 256; ++i) {
+      if ((n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
+       map[i] = ff->mapCodeToGID(cmap, u);
       }
-    } else {
+    }
 
-      // couldn't find widths -- use defaults 
-#if 0
-      //~ certain PDF generators apparently don't include widths
-      //~ for Arial and TimesNewRoman -- and this error message
-      //~ is a nuisance
-      error(-1, "No character widths resource for non-builtin font");
-#endif
-      if (isFixedWidth())
-       index = 0;
-      else if (isSerif())
-       index = 8;
-      else
-       index = 4;
-      if (isBold())
-       index += 2;
-      if (isItalic())
-       index += 1;
-      defWidths = defCharWidths[index];
-      code2 = 0; // to make gcc happy
-      for (code = 0; code < 256; ++code) {
-       if ((charName = encoding->getCharName(code)) &&
-           (code2 = standardEncoding.getCharCode(charName)) >= 0)
-         widths[code] = defWidths[code2] * 0.001;
+  // map the char codes through the cmap, possibly with an offset of
+  // 0xf000
+  } else {
+    for (i = 0; i < 256; ++i) {
+      if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
+       map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
       }
     }
-    obj1.free();
   }
+
+  // try the TrueType 'post' table to handle any unmapped characters
+  for (i = 0; i < 256; ++i) {
+    if (!map[i] && (charName = enc[i])) {
+      map[i] = (Gushort)(int)ff->mapNameToGID(charName);
+    }
+  }
+
+  return map;
 }
 
-void GfxFont::getType0EncAndWidths(Dict *fontDict) {
-  Object obj1, obj2, obj3, obj4, obj5, obj6;
-  int excepsSize;
-  int i, j, k, n;
+Dict *Gfx8BitFont::getCharProcs() {
+  return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
+}
 
-  fontDict->lookup("DescendantFonts", &obj1);
-  if (!obj1.isArray() || obj1.arrayGetLength() != 1) {
-    error(-1, "Bad DescendantFonts entry for Type 0 font");
+Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
+  if (enc[code] && charProcs.isDict()) {
+    charProcs.dictLookup(enc[code], proc);
+  } else {
+    proc->initNull();
+  }
+  return proc;
+}
+
+Dict *Gfx8BitFont::getResources() {
+  return resources.isDict() ? resources.getDict() : (Dict *)NULL;
+}
+
+//------------------------------------------------------------------------
+// GfxCIDFont
+//------------------------------------------------------------------------
+
+static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
+  return ((GfxFontCIDWidthExcep *)w1)->first -
+         ((GfxFontCIDWidthExcep *)w2)->first;
+}
+
+static int CDECL cmpWidthExcepV(const void *w1, const void *w2) {
+  return ((GfxFontCIDWidthExcepV *)w1)->first -
+         ((GfxFontCIDWidthExcepV *)w2)->first;
+}
+
+GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
+                      Dict *fontDict):
+  GfxFont(tagA, idA, nameA)
+{
+  Dict *desFontDict;
+  GString *collection, *cMapName;
+  Object desFontDictObj;
+  Object obj1, obj2, obj3, obj4, obj5, obj6;
+  int c1, c2;
+  int excepsSize, i, j, k;
+
+  ascent = 0.95;
+  descent = -0.35;
+  fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
+  cMap = NULL;
+  ctu = NULL;
+  widths.defWidth = 1.0;
+  widths.defHeight = -1.0;
+  widths.defVY = 0.880;
+  widths.exceps = NULL;
+  widths.nExceps = 0;
+  widths.excepsV = NULL;
+  widths.nExcepsV = 0;
+  cidToGID = NULL;
+  cidToGIDLen = 0;
+
+  // get the descendant font
+  if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
+    error(-1, "Missing DescendantFonts entry in Type 0 font");
+    obj1.free();
     goto err1;
   }
-  obj1.arrayGet(0, &obj2);
-  if (!obj2.isDict("Font")) {
-    error(-1, "Bad descendant font of Type 0 font");
-    goto err2;
+  if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
+    error(-1, "Bad descendant font in Type 0 font");
+    goto err3;
   }
+  obj1.free();
+  desFontDict = desFontDictObj.getDict();
 
-  obj2.dictLookup("CIDSystemInfo", &obj3);
-  if (!obj3.isDict()) {
-    error(-1, "Bad CIDSystemInfo in Type 0 font descendant");
+  // font type
+  if (!desFontDict->lookup("Subtype", &obj1)) {
+    error(-1, "Missing Subtype entry in Type 0 descendant font");
     goto err3;
   }
-  obj3.dictLookup("Registry", &obj4);
-  obj3.dictLookup("Ordering", &obj5);
-  if (obj4.isString() && obj5.isString()) {
-    if (obj4.getString()->cmp("Adobe") == 0 &&
-       obj5.getString()->cmp("Japan1") == 0) {
-#if JAPANESE_SUPPORT
-      is16 = gTrue;
-      enc16.charSet = font16AdobeJapan12;
-#else
-      error(-1, "Xpdf was compiled without Japanese font support");
-      goto err4;
-#endif
-    } else {
-      error(-1, "Uknown Type 0 character set: %s-%s",
-           obj4.getString()->getCString(), obj5.getString()->getCString());
-      goto err4;
-    }
+  if (obj1.isName("CIDFontType0")) {
+    type = fontCIDType0;
+  } else if (obj1.isName("CIDFontType2")) {
+    type = fontCIDType2;
   } else {
-    error(-1, "Unknown Type 0 character set");
+    error(-1, "Unknown Type 0 descendant font type '%s'",
+         obj1.isName() ? obj1.getName() : "???");
+    goto err3;
+  }
+  obj1.free();
+
+  // get info from font descriptor
+  readFontDescriptor(xref, desFontDict);
+
+  // look for an external font file
+  findExtFontFile();
+
+  //----- encoding info -----
+
+  // char collection
+  if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
+    error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
+    goto err3;
+  }
+  obj1.dictLookup("Registry", &obj2);
+  obj1.dictLookup("Ordering", &obj3);
+  if (!obj2.isString() || !obj3.isString()) {
+    error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
     goto err4;
   }
-  obj5.free();
-  obj4.free();
+  collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
   obj3.free();
+  obj2.free();
+  obj1.free();
 
-  obj2.dictLookup("DW", &obj3);
-  if (obj3.isInt())
-    widths16.defWidth = obj3.getInt() * 0.001;
-  else
-    widths16.defWidth = 1.0;
-  obj3.free();
+  // look for a ToUnicode CMap
+  if (!(ctu = readToUnicodeCMap(fontDict, 16, NULL))) {
+
+    // the "Adobe-Identity" and "Adobe-UCS" collections don't have
+    // cidToUnicode files
+    if (collection->cmp("Adobe-Identity") &&
+       collection->cmp("Adobe-UCS")) {
+
+      // look for a user-supplied .cidToUnicode file
+      if (!(ctu = globalParams->getCIDToUnicode(collection))) {
+       error(-1, "Unknown character collection '%s'",
+             collection->getCString());
+       delete collection;
+       goto err2;
+      }
+    }
+  }
+
+  // encoding (i.e., CMap)
+  //~ need to handle a CMap stream here
+  //~ also need to deal with the UseCMap entry in the stream dict
+  if (!fontDict->lookup("Encoding", &obj1)->isName()) {
+    error(-1, "Missing or invalid Encoding entry in Type 0 font");
+    delete collection;
+    goto err3;
+  }
+  cMapName = new GString(obj1.getName());
+  obj1.free();
+  if (!(cMap = globalParams->getCMap(collection, cMapName))) {
+    error(-1, "Unknown CMap '%s' for character collection '%s'",
+         cMapName->getCString(), collection->getCString());
+    delete collection;
+    delete cMapName;
+    goto err2;
+  }
+  delete collection;
+  delete cMapName;
+
+  // CIDToGIDMap (for embedded TrueType fonts)
+  if (type == fontCIDType2) {
+    desFontDict->lookup("CIDToGIDMap", &obj1);
+    if (obj1.isStream()) {
+      cidToGIDLen = 0;
+      i = 64;
+      cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
+      obj1.streamReset();
+      while ((c1 = obj1.streamGetChar()) != EOF &&
+            (c2 = obj1.streamGetChar()) != EOF) {
+       if (cidToGIDLen == i) {
+         i *= 2;
+         cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
+       }
+       cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
+      }
+    } else if (!obj1.isName("Identity") && !obj1.isNull()) {
+      error(-1, "Invalid CIDToGIDMap entry in CID font");
+    }
+    obj1.free();
+  }
 
-  widths16.exceps = NULL;
-  widths16.numExceps = 0;
-  obj2.dictLookup("W", &obj3);
-  if (obj3.isArray()) {
+  //----- character metrics -----
+
+  // default char width
+  if (desFontDict->lookup("DW", &obj1)->isInt()) {
+    widths.defWidth = obj1.getInt() * 0.001;
+  }
+  obj1.free();
+
+  // char width exceptions
+  if (desFontDict->lookup("W", &obj1)->isArray()) {
     excepsSize = 0;
-    k = 0;
     i = 0;
-    while (i+1 < obj3.arrayGetLength()) {
-      obj3.arrayGet(i, &obj4);
-      obj3.arrayGet(i+1, &obj5);
-      if (obj4.isInt() && obj5.isInt()) {
-       obj3.arrayGet(i+2, &obj6);
-       if (!obj6.isNum()) {
+    while (i + 1 < obj1.arrayGetLength()) {
+      obj1.arrayGet(i, &obj2);
+      obj1.arrayGet(i + 1, &obj3);
+      if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
+       if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
+         if (widths.nExceps == excepsSize) {
+           excepsSize += 16;
+           widths.exceps = (GfxFontCIDWidthExcep *)
+             grealloc(widths.exceps,
+                      excepsSize * sizeof(GfxFontCIDWidthExcep));
+         }
+         widths.exceps[widths.nExceps].first = obj2.getInt();
+         widths.exceps[widths.nExceps].last = obj3.getInt();
+         widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
+         ++widths.nExceps;
+       } else {
          error(-1, "Bad widths array in Type 0 font");
-         obj6.free();
-         obj5.free();
-         obj4.free();
-         break;
        }
-       if (k == excepsSize) {
-         excepsSize += 16;
-         widths16.exceps = (GfxFontWidthExcep *)
-                       grealloc(widths16.exceps,
-                                excepsSize * sizeof(GfxFontWidthExcep));
-       }
-       widths16.exceps[k].first = obj4.getInt();
-       widths16.exceps[k].last = obj5.getInt();
-       widths16.exceps[k].width = obj6.getNum() * 0.001;
-       obj6.free();
-       ++k;
+       obj4.free();
        i += 3;
-      } else if (obj4.isInt() && obj5.isArray()) {
-       if (k + obj5.arrayGetLength() >= excepsSize) {
-         excepsSize = (k + obj5.arrayGetLength() + 15) & ~15;
-         widths16.exceps = (GfxFontWidthExcep *)
-                       grealloc(widths16.exceps,
-                                excepsSize * sizeof(GfxFontWidthExcep));
+      } else if (obj2.isInt() && obj3.isArray()) {
+       if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
+         excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
+         widths.exceps = (GfxFontCIDWidthExcep *)
+           grealloc(widths.exceps,
+                    excepsSize * sizeof(GfxFontCIDWidthExcep));
        }
-       n = obj4.getInt();
-       for (j = 0; j < obj5.arrayGetLength(); ++j) {
-         obj5.arrayGet(j, &obj6);
-         if (!obj6.isNum()) {
+       j = obj2.getInt();
+       for (k = 0; k < obj3.arrayGetLength(); ++k) {
+         if (obj3.arrayGet(k, &obj4)->isNum()) {
+           widths.exceps[widths.nExceps].first = j;
+           widths.exceps[widths.nExceps].last = j;
+           widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
+           ++j;
+           ++widths.nExceps;
+         } else {
            error(-1, "Bad widths array in Type 0 font");
-           obj6.free();
-           break;
          }
-         widths16.exceps[k].first = widths16.exceps[k].last = n++;
-         widths16.exceps[k].width = obj6.getNum() * 0.001;
-         obj6.free();
-         ++k;
+         obj4.free();
        }
        i += 2;
       } else {
        error(-1, "Bad widths array in Type 0 font");
-       obj6.free();
-       obj5.free();
-       obj4.free();
-       break;
+       ++i;
       }
-      obj5.free();
-      obj4.free();
+      obj3.free();
+      obj2.free();
     }
-    widths16.numExceps = k;
-    if (k > 0)
-      qsort(widths16.exceps, k, sizeof(GfxFontWidthExcep), &cmpWidthExcep);
+    qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
+         &cmpWidthExcep);
   }
-  obj3.free();
-
-  obj2.free();
   obj1.free();
 
-  fontDict->lookup("Encoding", &obj1);
-  if (!obj1.isName()) {
-    error(-1, "Bad encoding for Type 0 font");
-    goto err1;
-  }
-#if JAPANESE_SUPPORT
-  if (enc16.charSet == font16AdobeJapan12) {
-    for (i = 0; gfxFontEnc16Tab[i].name; ++i) {
-      if (!strcmp(obj1.getName(), gfxFontEnc16Tab[i].name))
-       break;
+  // default metrics for vertical font
+  if (desFontDict->lookup("DW2", &obj1)->isArray() &&
+      obj1.arrayGetLength() == 2) {
+    if (obj1.arrayGet(0, &obj2)->isNum()) {
+      widths.defVY = obj2.getNum() * 0.001;
     }
-    if (!gfxFontEnc16Tab[i].name) {
-      error(-1, "Unknown encoding '%s' for Adobe-Japan1-2 font",
-           obj1.getName());
-      goto err1;
+    obj2.free();
+    if (obj1.arrayGet(1, &obj2)->isNum()) {
+      widths.defHeight = obj2.getNum() * 0.001;
     }
-    enc16.enc = gfxFontEnc16Tab[i].enc;
+    obj2.free();
+  }
+  obj1.free();
+
+  // char metric exceptions for vertical font
+  if (desFontDict->lookup("W2", &obj1)->isArray()) {
+    excepsSize = 0;
+    i = 0;
+    while (i + 1 < obj1.arrayGetLength()) {
+      obj1.arrayGet(i, &obj2);
+      obj1.arrayGet(i+ 1, &obj3);
+      if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
+       if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
+           obj1.arrayGet(i + 3, &obj5)->isNum() &&
+           obj1.arrayGet(i + 4, &obj6)->isNum()) {
+         if (widths.nExcepsV == excepsSize) {
+           excepsSize += 16;
+           widths.excepsV = (GfxFontCIDWidthExcepV *)
+             grealloc(widths.excepsV,
+                      excepsSize * sizeof(GfxFontCIDWidthExcepV));
+         }
+         widths.excepsV[widths.nExcepsV].first = obj2.getInt();
+         widths.excepsV[widths.nExcepsV].last = obj3.getInt();
+         widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
+         widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
+         widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
+         ++widths.nExcepsV;
+       } else {
+         error(-1, "Bad widths (W2) array in Type 0 font");
+       }
+       obj6.free();
+       obj5.free();
+       obj4.free();
+       i += 5;
+      } else if (obj2.isInt() && obj3.isArray()) {
+       if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
+         excepsSize =
+           (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
+         widths.excepsV = (GfxFontCIDWidthExcepV *)
+           grealloc(widths.excepsV,
+                    excepsSize * sizeof(GfxFontCIDWidthExcepV));
+       }
+       j = obj2.getInt();
+       for (k = 0; k < obj3.arrayGetLength(); k += 3) {
+         if (obj3.arrayGet(k, &obj4)->isNum() &&
+             obj3.arrayGet(k+1, &obj5)->isNum() &&
+             obj3.arrayGet(k+2, &obj6)->isNum()) {
+           widths.excepsV[widths.nExceps].first = j;
+           widths.excepsV[widths.nExceps].last = j;
+           widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
+           widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
+           widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
+           ++j;
+           ++widths.nExcepsV;
+         } else {
+           error(-1, "Bad widths (W2) array in Type 0 font");
+         }
+         obj6.free();
+         obj5.free();
+         obj4.free();
+       }
+       i += 2;
+      } else {
+       error(-1, "Bad widths (W2) array in Type 0 font");
+       ++i;
+      }
+      obj3.free();
+      obj2.free();
+    }
+    qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
+         &cmpWidthExcepV);
   }
-#endif
   obj1.free();
 
+  desFontDictObj.free();
+  ok = gTrue;
   return;
 
  err4:
-  obj5.free();
-  obj4.free();
- err3:
   obj3.free();
- err2:
   obj2.free();
- err1:
+ err3:
   obj1.free();
-  makeEncoding(fontDict, NULL);
-  makeWidths(fontDict, NULL, NULL);
+ err2:
+  desFontDictObj.free();
+ err1:;
 }
 
-static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
-  return ((GfxFontWidthExcep *)w1)->first - ((GfxFontWidthExcep *)w2)->first;
+GfxCIDFont::~GfxCIDFont() {
+  if (cMap) {
+    cMap->decRefCnt();
+  }
+  if (ctu) {
+    ctu->decRefCnt();
+  }
+  gfree(widths.exceps);
+  gfree(widths.excepsV);
+  if (cidToGID) {
+    gfree(cidToGID);
+  }
+}
+
+int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
+                           Unicode *u, int uSize, int *uLen,
+                           double *dx, double *dy, double *ox, double *oy) {
+  CID cid;
+  double w, h, vx, vy;
+  int n, a, b, m;
+
+  if (!cMap) {
+    *code = 0;
+    *uLen = 0;
+    *dx = *dy = 0;
+    return 1;
+  }
+
+  *code = (CharCode)(cid = cMap->getCID(s, len, &n));
+  if (ctu) {
+    *uLen = ctu->mapToUnicode(cid, u, uSize);
+  } else {
+    *uLen = 0;
+  }
+
+  // horizontal
+  if (cMap->getWMode() == 0) {
+    w = widths.defWidth;
+    h = vx = vy = 0;
+    if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
+      a = 0;
+      b = widths.nExceps;
+      // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
+      while (b - a > 1) {
+       m = (a + b) / 2;
+       if (widths.exceps[m].first <= cid) {
+         a = m;
+       } else {
+         b = m;
+       }
+      }
+      if (cid <= widths.exceps[a].last) {
+       w = widths.exceps[a].width;
+      }
+    }
+
+  // vertical
+  } else {
+    w = 0;
+    h = widths.defHeight;
+    vx = widths.defWidth / 2;
+    vy = widths.defVY;
+    if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
+      a = 0;
+      b = widths.nExcepsV;
+      // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
+      while (b - a > 1) {
+       m = (a + b) / 2;
+       if (widths.excepsV[m].last <= cid) {
+         a = m;
+       } else {
+         b = m;
+       }
+      }
+      if (cid <= widths.excepsV[a].last) {
+       h = widths.excepsV[a].height;
+       vx = widths.excepsV[a].vx;
+       vy = widths.excepsV[a].vy;
+      }
+    }
+  }
+
+  *dx = w;
+  *dy = h;
+  *ox = vx;
+  *oy = vy;
+
+  return n;
+}
+
+int GfxCIDFont::getWMode() {
+  return cMap ? cMap->getWMode() : 0;
+}
+
+CharCodeToUnicode *GfxCIDFont::getToUnicode() {
+  if (ctu) {
+    ctu->incRefCnt();
+  }
+  return ctu;
+}
+
+GString *GfxCIDFont::getCollection() {
+  return cMap ? cMap->getCollection() : (GString *)NULL;
 }
 
 //------------------------------------------------------------------------
 // GfxFontDict
 //------------------------------------------------------------------------
 
-GfxFontDict::GfxFontDict(Dict *fontDict) {
+GfxFontDict::GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict) {
   int i;
   Object obj1, obj2;
+  Ref r;
 
   numFonts = fontDict->getLength();
   fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
   for (i = 0; i < numFonts; ++i) {
     fontDict->getValNF(i, &obj1);
-    obj1.fetch(&obj2);
-    if (obj1.isRef() && obj2.isDict("Font")) {
-      fonts[i] = new GfxFont(fontDict->getKey(i), obj1.getRef(),
-                            obj2.getDict());
+    obj1.fetch(xref, &obj2);
+    if (obj2.isDict()) {
+      if (obj1.isRef()) {
+       r = obj1.getRef();
+      } else {
+       // no indirect reference for this font, so invent a unique one
+       // (legal generation numbers are five digits, so any 6-digit
+       // number would be safe)
+       r.num = i;
+       if (fontDictRef) {
+         r.gen = 100000 + fontDictRef->num;
+       } else {
+         r.gen = 999999;
+       }
+      }
+      fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
+                                  r, obj2.getDict());
+      if (fonts[i] && !fonts[i]->isOk()) {
+       delete fonts[i];
+       fonts[i] = NULL;
+      }
     } else {
       error(-1, "font resource is not a dictionary");
       fonts[i] = NULL;
@@ -805,8 +1488,11 @@ GfxFontDict::GfxFontDict(Dict *fontDict) {
 GfxFontDict::~GfxFontDict() {
   int i;
 
-  for (i = 0; i < numFonts; ++i)
-    delete fonts[i];
+  for (i = 0; i < numFonts; ++i) {
+    if (fonts[i]) {
+      delete fonts[i];
+    }
+  }
   gfree(fonts);
 }
 
@@ -814,8 +1500,9 @@ GfxFont *GfxFontDict::lookup(char *tag) {
   int i;
 
   for (i = 0; i < numFonts; ++i) {
-    if (fonts[i]->matches(tag))
+    if (fonts[i] && fonts[i]->matches(tag)) {
       return fonts[i];
+    }
   }
   return NULL;
 }