1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
14 #ifdef USE_GCC_PRAGMAS
21 #include "CharTypes.h"
25 class CharCodeToUnicode;
27 struct GfxFontCIDWidths;
29 //------------------------------------------------------------------------
31 //------------------------------------------------------------------------
46 //------------------------------------------------------------------------
48 //------------------------------------------------------------------------
50 struct GfxFontCIDWidthExcep {
51 CID first; // this record applies to
52 CID last; // CIDs <first>..<last>
53 double width; // char width
56 struct GfxFontCIDWidthExcepV {
57 CID first; // this record applies to
58 CID last; // CIDs <first>..<last>
59 double height; // char height
60 double vx, vy; // origin position
63 struct GfxFontCIDWidths {
64 double defWidth; // default char width
65 double defHeight; // default char height
66 double defVY; // default origin position
67 GfxFontCIDWidthExcep *exceps; // exceptions
68 int nExceps; // number of valid entries in exceps
69 GfxFontCIDWidthExcepV * // exceptions for vertical font
71 int nExcepsV; // number of valid entries in excepsV
74 //------------------------------------------------------------------------
76 //------------------------------------------------------------------------
78 #define fontFixedWidth (1 << 0)
79 #define fontSerif (1 << 1)
80 #define fontSymbolic (1 << 2)
81 #define fontItalic (1 << 6)
82 #define fontBold (1 << 18)
87 // Build a GfxFont object.
88 static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
90 GfxFont(char *tagA, Ref idA, GString *nameA);
94 GBool isOk() { return ok; }
97 GString *getTag() { return tag; }
99 // Get font dictionary ID.
100 Ref *getID() { return &id; }
102 // Does this font match the tag?
103 GBool matches(char *tagA) { return !tag->cmp(tagA); }
105 // Get base font name.
106 GString *getName() { return name; }
108 // Get the original font name (ignornig any munging that might have
109 // been done to map to a canonical Base-14 font name).
110 GString *getOrigName() { return origName; }
113 GfxFontType getType() { return type; }
114 virtual GBool isCIDFont() { return gFalse; }
116 // Get embedded font ID, i.e., a ref for the font file stream.
117 // Returns false if there is no embedded font.
118 GBool getEmbeddedFontID(Ref *embID)
119 { *embID = embFontID; return embFontID.num >= 0; }
121 // Get the PostScript font name for the embedded font. Returns
122 // NULL if there is no embedded font.
123 GString *getEmbeddedFontName() { return embFontName; }
125 // Get the name of the external font file. Returns NULL if there
126 // is no external font file.
127 GString *getExtFontFile() { return extFontFile; }
129 // Get font descriptor flags.
130 GBool isFixedWidth() { return flags & fontFixedWidth; }
131 GBool isSerif() { return flags & fontSerif; }
132 GBool isSymbolic() { return flags & fontSymbolic; }
133 GBool isItalic() { return flags & fontItalic; }
134 GBool isBold() { return flags & fontBold; }
136 // Return the font matrix.
137 double *getFontMatrix() { return fontMat; }
139 // Return the font bounding box.
140 double *getFontBBox() { return fontBBox; }
142 // Return the ascent and descent values.
143 double getAscent() { return ascent; }
144 double getDescent() { return descent; }
146 // Return the writing mode (0=horizontal, 1=vertical).
147 virtual int getWMode() { return 0; }
149 // Read an external or embedded font file into a buffer.
150 char *readExtFontFile(int *len);
151 char *readEmbFontFile(XRef *xref, int *len);
153 // Get the next char from a string <s> of <len> bytes, returning the
154 // char <code>, its Unicode mapping <u>, its displacement vector
155 // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>). <uSize>
156 // is the number of entries available in <u>, and <uLen> is set to
157 // the number actually used. Returns the number of bytes used by
159 virtual int getNextChar(char *s, int len, CharCode *code,
160 Unicode *u, int uSize, int *uLen,
161 double *dx, double *dy, double *ox, double *oy) = 0;
165 void readFontDescriptor(XRef *xref, Dict *fontDict);
166 CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits,
167 CharCodeToUnicode *ctu);
168 void findExtFontFile();
170 GString *tag; // PDF font tag
171 Ref id; // reference (used as unique ID)
172 GString *name; // font name
173 GString *origName; // original font name
174 GfxFontType type; // type of font
175 int flags; // font descriptor flags
176 GString *embFontName; // name of embedded font
177 Ref embFontID; // ref to embedded font file stream
178 GString *extFontFile; // external font file name
179 double fontMat[6]; // font matrix (Type 3 only)
180 double fontBBox[4]; // font bounding box (Type 3 only)
181 double missingWidth; // "default" width
182 double ascent; // max height above baseline
183 double descent; // max depth below baseline
187 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
191 class Gfx8BitFont: public GfxFont {
194 Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
195 GfxFontType typeA, Dict *fontDict);
197 virtual ~Gfx8BitFont();
199 virtual int getNextChar(char *s, int len, CharCode *code,
200 Unicode *u, int uSize, int *uLen,
201 double *dx, double *dy, double *ox, double *oy);
203 // Return the encoding.
204 char **getEncoding() { return enc; }
206 // Return the Unicode map.
207 CharCodeToUnicode *getToUnicode();
209 // Return the character name associated with <code>.
210 char *getCharName(int code) { return code>=256?0:enc[code]; }
212 // Returns true if the PDF font specified an encoding.
213 GBool getHasEncoding() { return hasEncoding; }
215 // Returns true if the PDF font specified MacRomanEncoding.
216 GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
218 // Get width of a character.
219 double getWidth(Guchar c) { return widths[c]; }
221 // Return a char code-to-GID mapping for the provided font file.
222 // (This is only useful for TrueType fonts.)
223 Gushort *getCodeToGIDMap(FoFiTrueType *ff);
225 // Return the Type 3 CharProc dictionary, or NULL if none.
226 Dict *getCharProcs();
228 // Return the Type 3 CharProc for the character associated with <code>.
229 Object *getCharProc(int code, Object *proc);
231 // Return the Type 3 Resources dictionary, or NULL if none.
232 Dict *getResources();
236 char *enc[256]; // char code --> char name
237 char encFree[256]; // boolean for each char name: if set,
238 // the string is malloc'ed
239 CharCodeToUnicode *ctu; // char code --> Unicode
241 GBool usesMacRomanEnc;
242 double widths[256]; // character widths
243 Object charProcs; // Type 3 CharProcs dictionary
244 Object resources; // Type 3 Resources dictionary
247 //------------------------------------------------------------------------
249 //------------------------------------------------------------------------
251 class GfxCIDFont: public GfxFont {
254 GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
257 virtual ~GfxCIDFont();
259 virtual GBool isCIDFont() { return gTrue; }
261 virtual int getNextChar(char *s, int len, CharCode *code,
262 Unicode *u, int uSize, int *uLen,
263 double *dx, double *dy, double *ox, double *oy);
265 // Return the writing mode (0=horizontal, 1=vertical).
266 virtual int getWMode();
268 // Return the Unicode map.
269 CharCodeToUnicode *getToUnicode();
271 // Get the collection name (<registry>-<ordering>).
272 GString *getCollection();
274 // Return the CID-to-GID mapping table. These should only be called
275 // if type is fontCIDType2.
276 Gushort *getCIDToGID() { return cidToGID; }
277 int getCIDToGIDLen() { return cidToGIDLen; }
281 CMap *cMap; // char code --> CID
282 CharCodeToUnicode *ctu; // CID --> Unicode
283 GfxFontCIDWidths widths; // character widths
284 Gushort *cidToGID; // CID --> GID mapping (for embedded
289 //------------------------------------------------------------------------
291 //------------------------------------------------------------------------
296 // Build the font dictionary, given the PDF font dictionary.
297 GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
302 // Get the specified font.
303 GfxFont *lookup(char *tag);
306 int getNumFonts() { return numFonts; }
307 GfxFont *getFont(int i) { return fonts[i]; }
311 GfxFont **fonts; // list of fonts
312 int numFonts; // number of fonts