Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GfxFont.h

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

Generated on Sat Nov 5 16:18:14 2005 for OPIE by  doxygen 1.4.2