1 //======================================================================== 2 // 3 // GfxFont.h 4 // 5 // Copyright 1996-2003 Glyph & Cog, LLC 6 // 7 //======================================================================== 8 9 #ifndef GFXFONT_H 10 #define GFXFONT_H 11 12 #include <aconf.h> 13 14 #ifdef USE_GCC_PRAGMAS 15 #pragma interface 16 #endif 17 18 #include "gtypes.h" 19 #include "GString.h" 20 #include "Object.h" 21 #include "CharTypes.h" 22 23 class Dict; 24 class CMap; 25 class CharCodeToUnicode; 26 class FoFiTrueType; 27 struct GfxFontCIDWidths; 28 29 //------------------------------------------------------------------------ 30 // GfxFontType 31 //------------------------------------------------------------------------ 32 33 enum GfxFontType { 34 //----- Gfx8BitFont 35 fontUnknownType, 36 fontType1, 37 fontType1C, 38 fontType1COT, 39 fontType3, 40 fontTrueType, 41 fontTrueTypeOT, 42 //----- GfxCIDFont 43 fontCIDType0, 44 fontCIDType0C, 45 fontCIDType0COT, 46 fontCIDType2, 47 fontCIDType2OT 48 }; 49 50 //------------------------------------------------------------------------ 51 // GfxFontCIDWidths 52 //------------------------------------------------------------------------ 53 54 struct GfxFontCIDWidthExcep { 55 CID first; // this record applies to 56 CID last; // CIDs <first>..<last> 57 double width; // char width 58 }; 59 60 struct GfxFontCIDWidthExcepV { 61 CID first; // this record applies to 62 CID last; // CIDs <first>..<last> 63 double height; // char height 64 double vx, vy; // origin position 65 }; 66 67 struct GfxFontCIDWidths { 68 double defWidth; // default char width 69 double defHeight; // default char height 70 double defVY; // default origin position 71 GfxFontCIDWidthExcep *exceps; // exceptions 72 int nExceps; // number of valid entries in exceps 73 GfxFontCIDWidthExcepV * // exceptions for vertical font 74 excepsV; 75 int nExcepsV; // number of valid entries in excepsV 76 }; 77 78 //------------------------------------------------------------------------ 79 // GfxFont 80 //------------------------------------------------------------------------ 81 82 #define fontFixedWidth (1 << 0) 83 #define fontSerif (1 << 1) 84 #define fontSymbolic (1 << 2) 85 #define fontItalic (1 << 6) 86 #define fontBold (1 << 18) 87 88 class GfxFont { 89 public: 90 91 // Build a GfxFont object. 92 static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict); 93 94 GfxFont(char *tagA, Ref idA, GString *nameA); 95 96 virtual ~GfxFont(); 97 isOk()98 GBool isOk() { return ok; } 99 100 // Get font tag. getTag()101 GString *getTag() { return tag; } 102 103 // Get font dictionary ID. getID()104 Ref *getID() { return &id; } 105 106 // Does this font match the tag? matches(char * tagA)107 GBool matches(char *tagA) { return !tag->cmp(tagA); } 108 109 // Get base font name. getName()110 GString *getName() { return name; } 111 112 // Get the original font name (ignornig any munging that might have 113 // been done to map to a canonical Base-14 font name). getOrigName()114 GString *getOrigName() { return origName; } 115 116 // Get font type. getType()117 GfxFontType getType() { return type; } isCIDFont()118 virtual GBool isCIDFont() { return gFalse; } 119 120 // Get embedded font ID, i.e., a ref for the font file stream. 121 // Returns false if there is no embedded font. getEmbeddedFontID(Ref * embID)122 GBool getEmbeddedFontID(Ref *embID) 123 { *embID = embFontID; return embFontID.num >= 0; } 124 125 // Get the PostScript font name for the embedded font. Returns 126 // NULL if there is no embedded font. getEmbeddedFontName()127 GString *getEmbeddedFontName() { return embFontName; } 128 129 // Get the name of the external font file. Returns NULL if there 130 // is no external font file. getExtFontFile()131 GString *getExtFontFile() { return extFontFile; } 132 133 // Get font descriptor flags. getFlags()134 int getFlags() { return flags; } isFixedWidth()135 GBool isFixedWidth() { return flags & fontFixedWidth; } isSerif()136 GBool isSerif() { return flags & fontSerif; } isSymbolic()137 GBool isSymbolic() { return flags & fontSymbolic; } isItalic()138 GBool isItalic() { return flags & fontItalic; } isBold()139 GBool isBold() { return flags & fontBold; } 140 141 // Return the font matrix. getFontMatrix()142 double *getFontMatrix() { return fontMat; } 143 144 // Return the font bounding box. getFontBBox()145 double *getFontBBox() { return fontBBox; } 146 147 // Return the ascent and descent values. getAscent()148 double getAscent() { return ascent; } getDescent()149 double getDescent() { return descent; } 150 151 // Return the writing mode (0=horizontal, 1=vertical). getWMode()152 virtual int getWMode() { return 0; } 153 154 // Read an external or embedded font file into a buffer. 155 char *readExtFontFile(int *len); 156 char *readEmbFontFile(XRef *xref, int *len); 157 158 // Get the next char from a string <s> of <len> bytes, returning the 159 // char <code>, its Unicode mapping <u>, its displacement vector 160 // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>). <uSize> 161 // is the number of entries available in <u>, and <uLen> is set to 162 // the number actually used. Returns the number of bytes used by 163 // the char code. 164 virtual int getNextChar(char *s, int len, CharCode *code, 165 Unicode *u, int uSize, int *uLen, 166 double *dx, double *dy, double *ox, double *oy) = 0; 167 virtual CharCodeToUnicode* getCTU() = 0; 168 169 protected: 170 171 void readFontDescriptor(XRef *xref, Dict *fontDict); 172 CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits, 173 CharCodeToUnicode *ctu); 174 void findExtFontFile(); 175 176 GString *tag; // PDF font tag 177 Ref id; // reference (used as unique ID) 178 GString *name; // font name 179 GString *origName; // original font name 180 GfxFontType type; // type of font 181 int flags; // font descriptor flags 182 GString *embFontName; // name of embedded font 183 Ref embFontID; // ref to embedded font file stream 184 GString *extFontFile; // external font file name 185 double fontMat[6]; // font matrix (Type 3 only) 186 double fontBBox[4]; // font bounding box (Type 3 only) 187 double missingWidth; // "default" width 188 double ascent; // max height above baseline 189 double descent; // max depth below baseline 190 GBool ok; 191 }; 192 193 //------------------------------------------------------------------------ 194 // Gfx8BitFont 195 //------------------------------------------------------------------------ 196 197 class Gfx8BitFont: public GfxFont { 198 public: 199 200 Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA, 201 GfxFontType typeA, Dict *fontDict); 202 203 virtual ~Gfx8BitFont(); 204 205 virtual int getNextChar(char *s, int len, CharCode *code, 206 Unicode *u, int uSize, int *uLen, 207 double *dx, double *dy, double *ox, double *oy); 208 virtual CharCodeToUnicode* getCTU(); 209 210 // Return the encoding. getEncoding()211 char **getEncoding() { return enc; } 212 213 // Return the Unicode map. 214 CharCodeToUnicode *getToUnicode(); 215 216 // Return the character name associated with <code>. getCharName(int code)217 char *getCharName(int code) { return code>=256?0:enc[code]; } 218 219 // Returns true if the PDF font specified an encoding. getHasEncoding()220 GBool getHasEncoding() { return hasEncoding; } 221 222 // Returns true if the PDF font specified MacRomanEncoding. getUsesMacRomanEnc()223 GBool getUsesMacRomanEnc() { return usesMacRomanEnc; } 224 225 // Get width of a character. getWidth(Guchar c)226 double getWidth(Guchar c) { return widths[c]; } 227 228 // Return a char code-to-GID mapping for the provided font file. 229 // (This is only useful for TrueType fonts.) 230 Gushort *getCodeToGIDMap(FoFiTrueType *ff); 231 232 // Return the Type 3 CharProc dictionary, or NULL if none. 233 Dict *getCharProcs(); 234 235 // Return the Type 3 CharProc for the character associated with <code>. 236 Object *getCharProc(int code, Object *proc); 237 238 // Return the Type 3 Resources dictionary, or NULL if none. 239 Dict *getResources(); 240 241 private: 242 243 char *enc[256]; // char code --> char name 244 char encFree[256]; // boolean for each char name: if set, 245 // the string is malloc'ed 246 CharCodeToUnicode *ctu; // char code --> Unicode 247 GBool hasEncoding; 248 GBool usesMacRomanEnc; 249 double widths[256]; // character widths 250 Object charProcs; // Type 3 CharProcs dictionary 251 Object resources; // Type 3 Resources dictionary 252 }; 253 254 //------------------------------------------------------------------------ 255 // GfxCIDFont 256 //------------------------------------------------------------------------ 257 258 class GfxCIDFont: public GfxFont { 259 public: 260 261 GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA, 262 Dict *fontDict); 263 264 virtual ~GfxCIDFont(); 265 isCIDFont()266 virtual GBool isCIDFont() { return gTrue; } 267 268 virtual int getNextChar(char *s, int len, CharCode *code, 269 Unicode *u, int uSize, int *uLen, 270 double *dx, double *dy, double *ox, double *oy); 271 virtual CharCodeToUnicode* getCTU(); 272 273 // Return the writing mode (0=horizontal, 1=vertical). 274 virtual int getWMode(); 275 276 // Return the Unicode map. 277 CharCodeToUnicode *getToUnicode(); 278 279 // Get the collection name (<registry>-<ordering>). 280 GString *getCollection(); 281 282 // Return the CID-to-GID mapping table. These should only be called 283 // if type is fontCIDType2. getCIDToGID()284 Gushort *getCIDToGID() { return cidToGID; } getCIDToGIDLen()285 int getCIDToGIDLen() { return cidToGIDLen; } 286 287 private: 288 289 CMap *cMap; // char code --> CID 290 CharCodeToUnicode *ctu; // CID --> Unicode 291 GfxFontCIDWidths widths; // character widths 292 Gushort *cidToGID; // CID --> GID mapping (for embedded 293 // TrueType fonts) 294 int cidToGIDLen; 295 }; 296 297 //------------------------------------------------------------------------ 298 // GfxFontDict 299 //------------------------------------------------------------------------ 300 301 class GfxFontDict { 302 public: 303 304 // Build the font dictionary, given the PDF font dictionary. 305 GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict); 306 307 // Destructor. 308 ~GfxFontDict(); 309 310 // Get the specified font. 311 GfxFont *lookup(char *tag); 312 313 // Iterative access. getNumFonts()314 int getNumFonts() { return numFonts; } getFont(int i)315 GfxFont *getFont(int i) { return fonts[i]; } 316 317 private: 318 319 GfxFont **fonts; // list of fonts 320 int numFonts; // number of fonts 321 }; 322 323 #endif 324