Back to index

tetex-bin  3.0
GfxFont.h
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // GfxFont.h
00004 //
00005 // Copyright 1996-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef GFXFONT_H
00010 #define GFXFONT_H
00011 
00012 #include <aconf.h>
00013 
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017 
00018 #include "gtypes.h"
00019 #include "GString.h"
00020 #include "Object.h"
00021 #include "CharTypes.h"
00022 
00023 class Dict;
00024 class CMap;
00025 class CharCodeToUnicode;
00026 class FoFiTrueType;
00027 struct GfxFontCIDWidths;
00028 
00029 //------------------------------------------------------------------------
00030 // GfxFontType
00031 //------------------------------------------------------------------------
00032 
00033 enum GfxFontType {
00034   //----- Gfx8BitFont
00035   fontUnknownType,
00036   fontType1,
00037   fontType1C,
00038   fontType3,
00039   fontTrueType,
00040   //----- GfxCIDFont
00041   fontCIDType0,
00042   fontCIDType0C,
00043   fontCIDType2
00044 };
00045 
00046 //------------------------------------------------------------------------
00047 // GfxFontCIDWidths
00048 //------------------------------------------------------------------------
00049 
00050 struct GfxFontCIDWidthExcep {
00051   CID first;                // this record applies to
00052   CID last;                 //   CIDs <first>..<last>
00053   double width;                    // char width
00054 };
00055 
00056 struct GfxFontCIDWidthExcepV {
00057   CID first;                // this record applies to
00058   CID last;                 //   CIDs <first>..<last>
00059   double height;            // char height
00060   double vx, vy;            // origin position
00061 };
00062 
00063 struct GfxFontCIDWidths {
00064   double defWidth;          // default char width
00065   double defHeight;         // default char height
00066   double defVY;                    // default origin position
00067   GfxFontCIDWidthExcep *exceps;    // exceptions
00068   int nExceps;                     // number of valid entries in exceps
00069   GfxFontCIDWidthExcepV *   // exceptions for vertical font
00070     excepsV;
00071   int nExcepsV;                    // number of valid entries in excepsV
00072 };
00073 
00074 //------------------------------------------------------------------------
00075 // GfxFont
00076 //------------------------------------------------------------------------
00077 
00078 #define fontFixedWidth (1 << 0)
00079 #define fontSerif      (1 << 1)
00080 #define fontSymbolic   (1 << 2)
00081 #define fontItalic     (1 << 6)
00082 #define fontBold       (1 << 18)
00083 
00084 class GfxFont {
00085 public:
00086 
00087   // Build a GfxFont object.
00088   static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
00089 
00090   GfxFont(char *tagA, Ref idA, GString *nameA);
00091 
00092   virtual ~GfxFont();
00093 
00094   GBool isOk() { return ok; }
00095 
00096   // Get font tag.
00097   GString *getTag() { return tag; }
00098 
00099   // Get font dictionary ID.
00100   Ref *getID() { return &id; }
00101 
00102   // Does this font match the tag?
00103   GBool matches(char *tagA) { return !tag->cmp(tagA); }
00104 
00105   // Get base font name.
00106   GString *getName() { return name; }
00107 
00108   // Get the original font name (ignornig any munging that might have
00109   // been done to map to a canonical Base-14 font name).
00110   GString *getOrigName() { return origName; }
00111 
00112   // Get font type.
00113   GfxFontType getType() { return type; }
00114   virtual GBool isCIDFont() { return gFalse; }
00115 
00116   // Get embedded font ID, i.e., a ref for the font file stream.
00117   // Returns false if there is no embedded font.
00118   GBool getEmbeddedFontID(Ref *embID)
00119     { *embID = embFontID; return embFontID.num >= 0; }
00120 
00121   // Get the PostScript font name for the embedded font.  Returns
00122   // NULL if there is no embedded font.
00123   GString *getEmbeddedFontName() { return embFontName; }
00124 
00125   // Get the name of the external font file.  Returns NULL if there
00126   // is no external font file.
00127   GString *getExtFontFile() { return extFontFile; }
00128 
00129   // Get font descriptor flags.
00130   GBool isFixedWidth() { return flags & fontFixedWidth; }
00131   GBool isSerif() { return flags & fontSerif; }
00132   GBool isSymbolic() { return flags & fontSymbolic; }
00133   GBool isItalic() { return flags & fontItalic; }
00134   GBool isBold() { return flags & fontBold; }
00135 
00136   // Return the font matrix.
00137   double *getFontMatrix() { return fontMat; }
00138 
00139   // Return the font bounding box.
00140   double *getFontBBox() { return fontBBox; }
00141 
00142   // Return the ascent and descent values.
00143   double getAscent() { return ascent; }
00144   double getDescent() { return descent; }
00145 
00146   // Return the writing mode (0=horizontal, 1=vertical).
00147   virtual int getWMode() { return 0; }
00148 
00149   // Read an external or embedded font file into a buffer.
00150   char *readExtFontFile(int *len);
00151   char *readEmbFontFile(XRef *xref, int *len);
00152 
00153   // Get the next char from a string <s> of <len> bytes, returning the
00154   // char <code>, its Unicode mapping <u>, its displacement vector
00155   // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>).  <uSize>
00156   // is the number of entries available in <u>, and <uLen> is set to
00157   // the number actually used.  Returns the number of bytes used by
00158   // the char code.
00159   virtual int getNextChar(char *s, int len, CharCode *code,
00160                        Unicode *u, int uSize, int *uLen,
00161                        double *dx, double *dy, double *ox, double *oy) = 0;
00162 
00163 protected:
00164 
00165   void readFontDescriptor(XRef *xref, Dict *fontDict);
00166   CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits,
00167                                    CharCodeToUnicode *ctu);
00168   void findExtFontFile();
00169 
00170   GString *tag;                    // PDF font tag
00171   Ref id;                   // reference (used as unique ID)
00172   GString *name;            // font name
00173   GString *origName;        // original font name
00174   GfxFontType type;         // type of font
00175   int flags;                // font descriptor flags
00176   GString *embFontName;            // name of embedded font
00177   Ref embFontID;            // ref to embedded font file stream
00178   GString *extFontFile;            // external font file name
00179   double fontMat[6];        // font matrix (Type 3 only)
00180   double fontBBox[4];              // font bounding box (Type 3 only)
00181   double missingWidth;             // "default" width
00182   double ascent;            // max height above baseline
00183   double descent;           // max depth below baseline
00184   GBool ok;
00185 };
00186 
00187 //------------------------------------------------------------------------
00188 // Gfx8BitFont
00189 //------------------------------------------------------------------------
00190 
00191 class Gfx8BitFont: public GfxFont {
00192 public:
00193 
00194   Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
00195              GfxFontType typeA, Dict *fontDict);
00196 
00197   virtual ~Gfx8BitFont();
00198 
00199   virtual int getNextChar(char *s, int len, CharCode *code,
00200                        Unicode *u, int uSize, int *uLen,
00201                        double *dx, double *dy, double *ox, double *oy);
00202 
00203   // Return the encoding.
00204   char **getEncoding() { return enc; }
00205 
00206   // Return the Unicode map.
00207   CharCodeToUnicode *getToUnicode();
00208 
00209   // Return the character name associated with <code>.
00210   char *getCharName(int code) { return enc[code]; }
00211 
00212   // Returns true if the PDF font specified an encoding.
00213   GBool getHasEncoding() { return hasEncoding; }
00214 
00215   // Returns true if the PDF font specified MacRomanEncoding.
00216   GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
00217 
00218   // Get width of a character.
00219   double getWidth(Guchar c) { return widths[c]; }
00220 
00221   // Return a char code-to-GID mapping for the provided font file.
00222   // (This is only useful for TrueType fonts.)
00223   Gushort *getCodeToGIDMap(FoFiTrueType *ff);
00224 
00225   // Return the Type 3 CharProc dictionary, or NULL if none.
00226   Dict *getCharProcs();
00227 
00228   // Return the Type 3 CharProc for the character associated with <code>.
00229   Object *getCharProc(int code, Object *proc);
00230 
00231   // Return the Type 3 Resources dictionary, or NULL if none.
00232   Dict *getResources();
00233 
00234 private:
00235 
00236   char *enc[256];           // char code --> char name
00237   char encFree[256];        // boolean for each char name: if set,
00238                             //   the string is malloc'ed
00239   CharCodeToUnicode *ctu;   // char code --> Unicode
00240   GBool hasEncoding;
00241   GBool usesMacRomanEnc;
00242   double widths[256];              // character widths
00243   Object charProcs;         // Type 3 CharProcs dictionary
00244   Object resources;         // Type 3 Resources dictionary
00245 };
00246 
00247 //------------------------------------------------------------------------
00248 // GfxCIDFont
00249 //------------------------------------------------------------------------
00250 
00251 class GfxCIDFont: public GfxFont {
00252 public:
00253 
00254   GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
00255             Dict *fontDict);
00256 
00257   virtual ~GfxCIDFont();
00258 
00259   virtual GBool isCIDFont() { return gTrue; }
00260 
00261   virtual int getNextChar(char *s, int len, CharCode *code,
00262                        Unicode *u, int uSize, int *uLen,
00263                        double *dx, double *dy, double *ox, double *oy);
00264 
00265   // Return the writing mode (0=horizontal, 1=vertical).
00266   virtual int getWMode();
00267 
00268   // Return the Unicode map.
00269   CharCodeToUnicode *getToUnicode();
00270 
00271   // Get the collection name (<registry>-<ordering>).
00272   GString *getCollection();
00273 
00274   // Return the CID-to-GID mapping table.  These should only be called
00275   // if type is fontCIDType2.
00276   Gushort *getCIDToGID() { return cidToGID; }
00277   int getCIDToGIDLen() { return cidToGIDLen; }
00278 
00279 private:
00280 
00281   CMap *cMap;               // char code --> CID
00282   CharCodeToUnicode *ctu;   // CID --> Unicode
00283   GfxFontCIDWidths widths;  // character widths
00284   Gushort *cidToGID;        // CID --> GID mapping (for embedded
00285                             //   TrueType fonts)
00286   int cidToGIDLen;
00287 };
00288 
00289 //------------------------------------------------------------------------
00290 // GfxFontDict
00291 //------------------------------------------------------------------------
00292 
00293 class GfxFontDict {
00294 public:
00295 
00296   // Build the font dictionary, given the PDF font dictionary.
00297   GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
00298 
00299   // Destructor.
00300   ~GfxFontDict();
00301 
00302   // Get the specified font.
00303   GfxFont *lookup(char *tag);
00304 
00305   // Iterative access.
00306   int getNumFonts() { return numFonts; }
00307   GfxFont *getFont(int i) { return fonts[i]; }
00308 
00309 private:
00310 
00311   GfxFont **fonts;          // list of fonts
00312   int numFonts;                    // number of fonts
00313 };
00314 
00315 #endif