1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
14 #ifdef USE_GCC_PRAGMAS
21 #include "CharTypes.h"
25 class CharCodeToUnicode;
26 struct GfxFontCIDWidths;
28 //------------------------------------------------------------------------
30 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
47 //------------------------------------------------------------------------
49 struct GfxFontCIDWidthExcep {
50 CID first; // this record applies to
51 CID last; // CIDs <first>..<last>
52 double width; // char width
55 struct GfxFontCIDWidthExcepV {
56 CID first; // this record applies to
57 CID last; // CIDs <first>..<last>
58 double height; // char height
59 double vx, vy; // origin position
62 struct GfxFontCIDWidths {
63 double defWidth; // default char width
64 double defHeight; // default char height
65 double defVY; // default origin position
66 GfxFontCIDWidthExcep *exceps; // exceptions
67 int nExceps; // number of valid entries in exceps
68 GfxFontCIDWidthExcepV * // exceptions for vertical font
70 int nExcepsV; // number of valid entries in excepsV
73 //------------------------------------------------------------------------
75 //------------------------------------------------------------------------
77 #define fontFixedWidth (1 << 0)
78 #define fontSerif (1 << 1)
79 #define fontSymbolic (1 << 2)
80 #define fontItalic (1 << 6)
81 #define fontBold (1 << 18)
86 // Build a GfxFont object.
87 static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
89 GfxFont(char *tagA, Ref idA, GString *nameA);
93 GBool isOk() { return ok; }
96 GString *getTag() { return tag; }
98 // Get font dictionary ID.
99 Ref *getID() { return &id; }
101 // Does this font match the tag?
102 GBool matches(char *tagA) { return !tag->cmp(tagA); }
104 // Get base font name.
105 GString *getName() { return name; }
108 GfxFontType getType() { return type; }
109 virtual GBool isCIDFont() { return gFalse; }
111 // Get embedded font ID, i.e., a ref for the font file stream.
112 // Returns false if there is no embedded font.
113 GBool getEmbeddedFontID(Ref *embID)
114 { *embID = embFontID; return embFontID.num >= 0; }
116 // Get the PostScript font name for the embedded font. Returns
117 // NULL if there is no embedded font.
118 GString *getEmbeddedFontName() { return embFontName; }
120 // Get the name of the external font file. Returns NULL if there
121 // is no external font file.
122 GString *getExtFontFile() { return extFontFile; }
124 // Get font descriptor flags.
125 GBool isFixedWidth() { return flags & fontFixedWidth; }
126 GBool isSerif() { return flags & fontSerif; }
127 GBool isSymbolic() { return flags & fontSymbolic; }
128 GBool isItalic() { return flags & fontItalic; }
129 GBool isBold() { return flags & fontBold; }
131 // Return the font matrix.
132 double *getFontMatrix() { return fontMat; }
134 // Return the font bounding box.
135 double *getFontBBox() { return fontBBox; }
137 // Return the ascent and descent values.
138 double getAscent() { return ascent; }
139 double getDescent() { return descent; }
141 // Return the writing mode (0=horizontal, 1=vertical).
142 virtual int getWMode() { return 0; }
144 // Read an external or embedded font file into a buffer.
145 char *readExtFontFile(int *len);
146 char *readEmbFontFile(XRef *xref, int *len);
148 // Get the next char from a string <s> of <len> bytes, returning the
149 // char <code>, its Unicode mapping <u>, its displacement vector
150 // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>). <uSize>
151 // is the number of entries available in <u>, and <uLen> is set to
152 // the number actually used. Returns the number of bytes used by
154 virtual int getNextChar(char *s, int len, CharCode *code,
155 Unicode *u, int uSize, int *uLen,
156 double *dx, double *dy, double *ox, double *oy) = 0;
160 void readFontDescriptor(XRef *xref, Dict *fontDict);
161 CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits);
162 void findExtFontFile();
164 GString *tag; // PDF font tag
165 Ref id; // reference (used as unique ID)
166 GString *name; // font name
167 GfxFontType type; // type of font
168 int flags; // font descriptor flags
169 GString *embFontName; // name of embedded font
170 Ref embFontID; // ref to embedded font file stream
171 GString *extFontFile; // external font file name
172 double fontMat[6]; // font matrix (Type 3 only)
173 double fontBBox[4]; // font bounding box (Type 3 only)
174 double missingWidth; // "default" width
175 double ascent; // max height above baseline
176 double descent; // max depth below baseline
180 //------------------------------------------------------------------------
182 //------------------------------------------------------------------------
184 class Gfx8BitFont: public GfxFont {
187 Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
188 GfxFontType typeA, Dict *fontDict);
190 virtual ~Gfx8BitFont();
192 virtual int getNextChar(char *s, int len, CharCode *code,
193 Unicode *u, int uSize, int *uLen,
194 double *dx, double *dy, double *ox, double *oy);
196 // Return the encoding.
197 char **getEncoding() { return enc; }
199 // Return the Unicode map.
200 CharCodeToUnicode *getToUnicode();
202 // Return the character name associated with <code>.
203 char *getCharName(int code) { return enc[code]; }
205 // Returns true if the PDF font specified an encoding.
206 GBool getHasEncoding() { return hasEncoding; }
208 // Get width of a character or string.
209 double getWidth(Guchar c) { return widths[c]; }
211 // Return the Type 3 CharProc dictionary, or NULL if none.
212 Dict *getCharProcs();
214 // Return the Type 3 CharProc for the character associated with <code>.
215 Object *getCharProc(int code, Object *proc);
217 // Return the Type 3 Resources dictionary, or NULL if none.
218 Dict *getResources();
222 char *enc[256]; // char code --> char name
223 char encFree[256]; // boolean for each char name: if set,
224 // the string is malloc'ed
225 CharCodeToUnicode *ctu; // char code --> Unicode
227 double widths[256]; // character widths
228 Object charProcs; // Type 3 CharProcs dictionary
229 Object resources; // Type 3 Resources dictionary
232 //------------------------------------------------------------------------
234 //------------------------------------------------------------------------
236 class GfxCIDFont: public GfxFont {
239 GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
242 virtual ~GfxCIDFont();
244 virtual GBool isCIDFont() { return gTrue; }
246 virtual int getNextChar(char *s, int len, CharCode *code,
247 Unicode *u, int uSize, int *uLen,
248 double *dx, double *dy, double *ox, double *oy);
250 // Return the writing mode (0=horizontal, 1=vertical).
251 virtual int getWMode();
253 // Return the Unicode map.
254 CharCodeToUnicode *getToUnicode();
256 // Get the collection name (<registry>-<ordering>).
257 GString *getCollection();
259 // Return the CID-to-GID mapping table. These should only be called
260 // if type is fontCIDType2.
261 Gushort *getCIDToGID() { return cidToGID; }
262 int getCIDToGIDLen() { return cidToGIDLen; }
266 CMap *cMap; // char code --> CID
267 CharCodeToUnicode *ctu; // CID --> Unicode
268 GfxFontCIDWidths widths; // character widths
269 Gushort *cidToGID; // CID --> GID mapping (for embedded
274 //------------------------------------------------------------------------
276 //------------------------------------------------------------------------
281 // Build the font dictionary, given the PDF font dictionary.
282 GfxFontDict(XRef *xref, Dict *fontDict);
287 // Get the specified font.
288 GfxFont *lookup(char *tag);
291 int getNumFonts() { return numFonts; }
292 GfxFont *getFont(int i) { return fonts[i]; }
296 GfxFont **fonts; // list of fonts
297 int numFonts; // number of fonts