filters

GfxFont.h

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