1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
19 #include "CharTypes.h"
23 class CharCodeToUnicode;
24 struct GfxFontCIDWidths;
26 //------------------------------------------------------------------------
28 //------------------------------------------------------------------------
43 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
47 struct GfxFontCIDWidthExcep {
48 CID first; // this record applies to
49 CID last; // CIDs <first>..<last>
50 double width; // char width
53 struct GfxFontCIDWidthExcepV {
54 CID first; // this record applies to
55 CID last; // CIDs <first>..<last>
56 double height; // char height
57 double vx, vy; // origin position
60 struct GfxFontCIDWidths {
61 double defWidth; // default char width
62 double defHeight; // default char height
63 double defVY; // default origin position
64 GfxFontCIDWidthExcep *exceps; // exceptions
65 int nExceps; // number of valid entries in exceps
66 GfxFontCIDWidthExcepV * // exceptions for vertical font
68 int nExcepsV; // number of valid entries in excepsV
71 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
75 #define fontFixedWidth (1 << 0)
76 #define fontSerif (1 << 1)
77 #define fontSymbolic (1 << 2)
78 #define fontItalic (1 << 6)
79 #define fontBold (1 << 18)
84 // Build a GfxFont object.
85 static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
87 GfxFont(char *tagA, Ref idA, GString *nameA);
91 GBool isOk() { return ok; }
94 GString *getTag() { return tag; }
96 // Get font dictionary ID.
97 Ref *getID() { return &id; }
99 // Does this font match the tag?
100 GBool matches(char *tagA) { return !tag->cmp(tagA); }
102 // Get base font name.
103 GString *getName() { return name; }
106 GfxFontType getType() { return type; }
107 virtual GBool isCIDFont() { return gFalse; }
109 // Get embedded font ID, i.e., a ref for the font file stream.
110 // Returns false if there is no embedded font.
111 GBool getEmbeddedFontID(Ref *embID)
112 { *embID = embFontID; return embFontID.num >= 0; }
114 // Get the PostScript font name for the embedded font. Returns
115 // NULL if there is no embedded font.
116 GString *getEmbeddedFontName() { return embFontName; }
118 // Get the name of the external font file. Returns NULL if there
119 // is no external font file.
120 GString *getExtFontFile() { return extFontFile; }
122 // Get font descriptor flags.
123 GBool isFixedWidth() { return flags & fontFixedWidth; }
124 GBool isSerif() { return flags & fontSerif; }
125 GBool isSymbolic() { return flags & fontSymbolic; }
126 GBool isItalic() { return flags & fontItalic; }
127 GBool isBold() { return flags & fontBold; }
129 // Return the font matrix.
130 double *getFontMatrix() { return fontMat; }
132 // Return the font bounding box.
133 double *getFontBBox() { return fontBBox; }
135 // Return the ascent and descent values.
136 double getAscent() { return ascent; }
137 double getDescent() { return descent; }
139 // Return the writing mode (0=horizontal, 1=vertical).
140 virtual int getWMode() { return 0; }
142 // Read an external or embedded font file into a buffer.
143 char *readExtFontFile(int *len);
144 char *readEmbFontFile(XRef *xref, int *len);
146 // Get the next char from a string <s> of <len> bytes, returning the
147 // char <code>, its Unicode mapping <u>, its displacement vector
148 // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>). <uSize>
149 // is the number of entries available in <u>, and <uLen> is set to
150 // the number actually used. Returns the number of bytes used by
152 virtual int getNextChar(char *s, int len, CharCode *code,
153 Unicode *u, int uSize, int *uLen,
154 double *dx, double *dy, double *ox, double *oy) = 0;
158 void readFontDescriptor(XRef *xref, Dict *fontDict);
159 CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits);
160 void findExtFontFile();
162 GString *tag; // PDF font tag
163 Ref id; // reference (used as unique ID)
164 GString *name; // font name
165 GfxFontType type; // type of font
166 int flags; // font descriptor flags
167 GString *embFontName; // name of embedded font
168 Ref embFontID; // ref to embedded font file stream
169 GString *extFontFile; // external font file name
170 double fontMat[6]; // font matrix (Type 3 only)
171 double fontBBox[4]; // font bounding box (Type 3 only)
172 double missingWidth; // "default" width
173 double ascent; // max height above baseline
174 double descent; // max depth below baseline
178 //------------------------------------------------------------------------
180 //------------------------------------------------------------------------
182 class Gfx8BitFont: public GfxFont {
185 Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
186 GfxFontType typeA, Dict *fontDict);
188 virtual ~Gfx8BitFont();
190 virtual int getNextChar(char *s, int len, CharCode *code,
191 Unicode *u, int uSize, int *uLen,
192 double *dx, double *dy, double *ox, double *oy);
194 // Return the encoding.
195 char **getEncoding() { return enc; }
197 // Return the Unicode map.
198 CharCodeToUnicode *getToUnicode();
200 // Return the character name associated with <code>.
201 char *getCharName(int code) { return code>=256?0:enc[code]; }
203 // Returns true if the PDF font specified an encoding.
204 GBool getHasEncoding() { return hasEncoding; }
206 // Get width of a character or string.
207 double getWidth(Guchar c) { return widths[c]; }
209 // Return the Type 3 CharProc dictionary, or NULL if none.
210 Dict *getCharProcs();
212 // Return the Type 3 CharProc for the character associated with <code>.
213 Object *getCharProc(int code, Object *proc);
215 // Return the Type 3 Resources dictionary, or NULL if none.
216 Dict *getResources();
220 char *enc[256]; // char code --> char name
221 char encFree[256]; // boolean for each char name: if set,
222 // the string is malloc'ed
223 CharCodeToUnicode *ctu; // char code --> Unicode
225 double widths[256]; // character widths
226 Object charProcs; // Type 3 CharProcs dictionary
227 Object resources; // Type 3 Resources dictionary
230 //------------------------------------------------------------------------
232 //------------------------------------------------------------------------
234 class GfxCIDFont: public GfxFont {
237 GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
240 virtual ~GfxCIDFont();
242 virtual GBool isCIDFont() { return gTrue; }
244 virtual int getNextChar(char *s, int len, CharCode *code,
245 Unicode *u, int uSize, int *uLen,
246 double *dx, double *dy, double *ox, double *oy);
248 // Return the writing mode (0=horizontal, 1=vertical).
249 virtual int getWMode();
251 // Return the Unicode map.
252 CharCodeToUnicode *getToUnicode();
254 // Get the collection name (<registry>-<ordering>).
255 GString *getCollection();
257 // Return the CID-to-GID mapping table. These should only be called
258 // if type is fontCIDType2.
259 Gushort *getCIDToGID() { return cidToGID; }
260 int getCIDToGIDLen() { return cidToGIDLen; }
264 CMap *cMap; // char code --> CID
265 CharCodeToUnicode *ctu; // CID --> Unicode
266 GfxFontCIDWidths widths; // character widths
267 Gushort *cidToGID; // CID --> GID mapping (for embedded
272 //------------------------------------------------------------------------
274 //------------------------------------------------------------------------
279 // Build the font dictionary, given the PDF font dictionary.
280 GfxFontDict(XRef *xref, Dict *fontDict);
285 // Get the specified font.
286 GfxFont *lookup(char *tag);
289 int getNumFonts() { return numFonts; }
290 GfxFont *getFont(int i) { return fonts[i]; }
294 GfxFont **fonts; // list of fonts
295 int numFonts; // number of fonts