upgrade to xpdf 1.01.
[swftools.git] / pdf2swf / xpdf / GfxFont.h
index 0435d90..c67ac29 100644 (file)
@@ -2,7 +2,7 @@
 //
 // GfxFont.h
 //
-// Copyright 1996 Derek B. Noonburg
+// Copyright 1996-2002 Glyph & Cog, LLC
 //
 //========================================================================
 
 #include "gtypes.h"
 #include "GString.h"
 #include "Object.h"
-#include "FontEncoding.h"
+#include "CharTypes.h"
 
 class Dict;
-struct BuiltinFont;
+class CMap;
+class CharCodeToUnicode;
+struct GfxFontCIDWidths;
 
 //------------------------------------------------------------------------
-// GfxFontCharSet16
+// GfxFontType
 //------------------------------------------------------------------------
 
-enum GfxFontCharSet16 {
-  font16AdobeJapan12,          // Adobe-Japan1-2
-  font16AdobeGB12,             // Adobe-GB1-2 (Chinese)
-  font16AdobeCNS13             // Adobe-CNS1-3 (Chinese)
-};
-
-//------------------------------------------------------------------------
-// GfxFontEncoding16
-//------------------------------------------------------------------------
-
-struct GfxFontEncoding16 {
-  int wMode;                   // writing mode (0=horizontal, 1=vertical)
-  Guchar codeLen[256];         // length of codes, in bytes, indexed by
-                               //   first byte of code
-  Gushort map1[256];           // one-byte code mapping:
-                               //   map1[code] --> 16-bit char selector
-  Gushort *map2;               // two-byte code mapping
-                               //   map2[2*i]   --> first code in range
-                               //   map2[2*i+1] --> 16-bit char selector
-                               //                   for map2[2*i]
-  int map2Len;                 // length of map2 array (divided by 2)
+enum GfxFontType {
+  //----- Gfx8BitFont
+  fontUnknownType,
+  fontType1,
+  fontType1C,
+  fontType3,
+  fontTrueType,
+  //----- GfxCIDFont
+  fontCIDType0,
+  fontCIDType0C,
+  fontCIDType2
 };
 
 //------------------------------------------------------------------------
-// GfxFontWidths16
+// GfxFontCIDWidths
 //------------------------------------------------------------------------
 
-struct GfxFontWidthExcep {
-  int first;                   // this record applies to
-  int last;                    //   chars <first>..<last>
+struct GfxFontCIDWidthExcep {
+  CID first;                   // this record applies to
+  CID last;                    //   CIDs <first>..<last>
   double width;                        // char width
 };
 
-struct GfxFontWidthExcepV {
-  int first;                   // this record applies to
-  int last;                    //   chars <first>..<last>
+struct GfxFontCIDWidthExcepV {
+  CID first;                   // this record applies to
+  CID last;                    //   CIDs <first>..<last>
   double height;               // char height
   double vx, vy;               // origin position
 };
 
-struct GfxFontWidths16 {
+struct GfxFontCIDWidths {
   double defWidth;             // default char width
   double defHeight;            // default char height
   double defVY;                        // default origin position
-  GfxFontWidthExcep *exceps;   // exceptions
-  int numExceps;               // number of valid entries in exceps
-  GfxFontWidthExcepV *excepsV; // exceptions for vertical font
-  int numExcepsV;              // number of valid entries in excepsV
+  GfxFontCIDWidthExcep *exceps;        // exceptions
+  int nExceps;                 // number of valid entries in exceps
+  GfxFontCIDWidthExcepV *      // exceptions for vertical font
+    excepsV;
+  int nExcepsV;                        // number of valid entries in excepsV
 };
 
 //------------------------------------------------------------------------
@@ -85,41 +78,33 @@ struct GfxFontWidths16 {
 #define fontItalic     (1 << 6)
 #define fontBold       (1 << 18)
 
-enum GfxFontType {
-  fontUnknownType,
-  fontType1,
-  fontType1C,
-  fontType3,
-  fontTrueType,
-  fontType0
-};
-
 class GfxFont {
 public:
 
-  // Constructor.
-  GfxFont(char *tag1, Ref id1, Dict *fontDict);
+  // Build a GfxFont object.
+  static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
 
-  // Destructor.
-  ~GfxFont();
+  GfxFont(char *tagA, Ref idA, GString *nameA);
+
+  virtual ~GfxFont();
+
+  GBool isOk() { return ok; }
 
   // Get font tag.
   GString *getTag() { return tag; }
 
   // Get font dictionary ID.
-  Ref getID() { return id; }
+  Ref *getID() { return &id; }
 
   // Does this font match the tag?
-  GBool matches(char *tag1) { return !tag->cmp(tag1); }
+  GBool matches(char *tagA) { return !tag->cmp(tagA); }
 
   // Get base font name.
   GString *getName() { return name; }
 
   // Get font type.
   GfxFontType getType() { return type; }
-
-  // Does this font use 16-bit characters?
-  GBool is16Bit() { return is16; }
+  virtual GBool isCIDFont() { return gFalse; }
 
   // Get embedded font ID, i.e., a ref for the font file stream.
   // Returns false if there is no embedded font.
@@ -128,8 +113,7 @@ public:
 
   // Get the PostScript font name for the embedded font.  Returns
   // NULL if there is no embedded font.
-  char *getEmbeddedFontName()
-    { return embFontName ? embFontName->getCString() : (char *)NULL; }
+  GString *getEmbeddedFontName() { return embFontName; }
 
   // Get the name of the external font file.  Returns NULL if there
   // is no external font file.
@@ -142,73 +126,147 @@ public:
   GBool isItalic() { return flags & fontItalic; }
   GBool isBold() { return flags & fontBold; }
 
-  // Get width of a character or string.
-  double getWidth(Guchar c) { return widths[c]; }
-  double getWidth(GString *s);
+  // Return the font matrix.
+  double *getFontMatrix() { return fontMat; }
+
+  // Return the font bounding box.
+  double *getFontBBox() { return fontBBox; }
+
+  // Return the ascent and descent values.
+  double getAscent() { return ascent; }
+  double getDescent() { return descent; }
+
+  // Return the writing mode (0=horizontal, 1=vertical).
+  virtual int getWMode() { return 0; }
+
+  // Read an external or embedded font file into a buffer.
+  char *readExtFontFile(int *len);
+  char *readEmbFontFile(XRef *xref, int *len);
+
+  // Get the next char from a string <s> of <len> bytes, returning the
+  // char <code>, its Unicode mapping <u>, its displacement vector
+  // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>).  <uSize>
+  // is the number of entries available in <u>, and <uLen> is set to
+  // the number actually used.  Returns the number of bytes used by
+  // the char code.
+  virtual int getNextChar(char *s, int len, CharCode *code,
+                         Unicode *u, int uSize, int *uLen,
+                         double *dx, double *dy, double *ox, double *oy) = 0;
+
+protected:
+
+  void readFontDescriptor(XRef *xref, Dict *fontDict);
+  CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits);
+  void findExtFontFile();
+
+  GString *tag;                        // PDF font tag
+  Ref id;                      // reference (used as unique ID)
+  GString *name;               // font name
+  GfxFontType type;            // type of font
+  int flags;                   // font descriptor flags
+  GString *embFontName;                // name of embedded font
+  Ref embFontID;               // ref to embedded font file stream
+  GString *extFontFile;                // external font file name
+  double fontMat[6];           // font matrix (Type 3 only)
+  double fontBBox[4];          // font bounding box (Type 3 only)
+  double missingWidth;         // "default" width
+  double ascent;               // max height above baseline
+  double descent;              // max depth below baseline
+  GBool ok;
+};
+
+//------------------------------------------------------------------------
+// Gfx8BitFont
+//------------------------------------------------------------------------
 
-  // Get character metrics for 16-bit font.
-  double getWidth16(int c);
-  double getHeight16(int c);
-  double getOriginX16(int c);
-  double getOriginY16(int c);
+class Gfx8BitFont: public GfxFont {
+public:
+
+  Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
+             GfxFontType typeA, Dict *fontDict);
+
+  virtual ~Gfx8BitFont();
+
+  virtual int getNextChar(char *s, int len, CharCode *code,
+                         Unicode *u, int uSize, int *uLen,
+                         double *dx, double *dy, double *ox, double *oy);
 
   // Return the encoding.
-  FontEncoding *getEncoding() { return encoding; }
+  char **getEncoding() { return enc; }
+
+  // Return the Unicode map.
+  CharCodeToUnicode *getToUnicode();
 
   // Return the character name associated with <code>.
-  char *getCharName(int code) { return encoding->getCharName(code); }
+  char *getCharName(int code) { return enc[code]; }
 
-  // Return the code associated with <name>.
-  int getCharCode(char *charName) { return encoding->getCharCode(charName); }
+  // Returns true if the PDF font specified an encoding.
+  GBool getHasEncoding() { return hasEncoding; }
+
+  // Get width of a character or string.
+  double getWidth(Guchar c) { return widths[c]; }
+
+  // Return the Type 3 CharProc dictionary, or NULL if none.
+  Dict *getCharProcs();
 
   // Return the Type 3 CharProc for the character associated with <code>.
   Object *getCharProc(int code, Object *proc);
 
-  // Return the 16-bit character set and encoding.
-  GfxFontCharSet16 getCharSet16() { return enc16.charSet; }
-  GfxFontEncoding16 *getEncoding16() { return enc16.enc; }
+  // Return the Type 3 Resources dictionary, or NULL if none.
+  Dict *getResources();
 
-  // Get the writing mode (0=horizontal, 1=vertical).
-  int getWMode16() { return enc16.enc->wMode; }
+private:
 
-  // Return the font matrix.
-  double *getFontMatrix() { return fontMat; }
+  char *enc[256];              // char code --> char name
+  char encFree[256];           // boolean for each char name: if set,
+                               //   the string is malloc'ed
+  CharCodeToUnicode *ctu;      // char code --> Unicode
+  GBool hasEncoding;
+  double widths[256];          // character widths
+  Object charProcs;            // Type 3 CharProcs dictionary
+  Object resources;            // Type 3 Resources dictionary
+};
 
-  // Read an external or embedded font file into a buffer.
-  char *readExtFontFile(int *len);
-  char *readEmbFontFile(int *len);
+//------------------------------------------------------------------------
+// GfxCIDFont
+//------------------------------------------------------------------------
 
-private:
+class GfxCIDFont: public GfxFont {
+public:
 
-  void getEncAndWidths(Dict *fontDict, BuiltinFont *builtinFont,
-                      int missingWidth);
-  void findExtFontFile();
-  void makeWidths(Dict *fontDict, FontEncoding *builtinEncoding,
-                 Gushort *builtinWidths, int missingWidth);
-  void getType0EncAndWidths(Dict *fontDict);
+  GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
+            Dict *fontDict);
 
-  GString *tag;                        // PDF font tag
-  Ref id;                      // reference (used as unique ID)
-  GString *name;               // font name
-  int flags;                   // font descriptor flags
-  GfxFontType type;            // type of font
-  GBool is16;                  // set if font uses 16-bit chars
-  GString *embFontName;                // name of embedded font
-  Ref embFontID;               // ref to embedded font file stream
-  GString *extFontFile;                // external font file name
-  Object charProcs;            // Type3 CharProcs dictionary
-  double fontMat[6];           // font matrix
-  union {
-    FontEncoding *encoding;    // 8-bit font encoding
-    struct {
-      GfxFontCharSet16 charSet;        // 16-bit character set
-      GfxFontEncoding16 *enc;  // 16-bit encoding (CMap)
-    } enc16;
-  };
-  union {
-    double widths[256];                // width of each char for 8-bit font
-    GfxFontWidths16 widths16;  // char widths for 16-bit font
-  };
+  virtual ~GfxCIDFont();
+
+  virtual GBool isCIDFont() { return gTrue; }
+
+  virtual int getNextChar(char *s, int len, CharCode *code,
+                         Unicode *u, int uSize, int *uLen,
+                         double *dx, double *dy, double *ox, double *oy);
+
+  // Return the writing mode (0=horizontal, 1=vertical).
+  virtual int getWMode();
+
+  // Return the Unicode map.
+  CharCodeToUnicode *getToUnicode();
+
+  // Get the collection name (<registry>-<ordering>).
+  GString *getCollection();
+
+  // Return the CID-to-GID mapping table.  These should only be called
+  // if type is fontCIDType2.
+  Gushort *getCIDToGID() { return cidToGID; }
+  int getCIDToGIDLen() { return cidToGIDLen; }
+
+private:
+
+  CMap *cMap;                  // char code --> CID
+  CharCodeToUnicode *ctu;      // CID --> Unicode
+  GfxFontCIDWidths widths;     // character widths
+  Gushort *cidToGID;           // CID --> GID mapping (for embedded
+                               //   TrueType fonts)
+  int cidToGIDLen;
 };
 
 //------------------------------------------------------------------------
@@ -219,7 +277,7 @@ class GfxFontDict {
 public:
 
   // Build the font dictionary, given the PDF font dictionary.
-  GfxFontDict(Dict *fontDict);
+  GfxFontDict(XRef *xref, Dict *fontDict);
 
   // Destructor.
   ~GfxFontDict();