range checking.
[swftools.git] / pdf2swf / xpdf / GfxFont.h
1 //========================================================================
2 //
3 // GfxFont.h
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef GFXFONT_H
10 #define GFXFONT_H
11
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
15
16 #include "gtypes.h"
17 #include "GString.h"
18 #include "Object.h"
19 #include "CharTypes.h"
20
21 class Dict;
22 class CMap;
23 class CharCodeToUnicode;
24 struct GfxFontCIDWidths;
25
26 //------------------------------------------------------------------------
27 // GfxFontType
28 //------------------------------------------------------------------------
29
30 enum GfxFontType {
31   //----- Gfx8BitFont
32   fontUnknownType,
33   fontType1,
34   fontType1C,
35   fontType3,
36   fontTrueType,
37   //----- GfxCIDFont
38   fontCIDType0,
39   fontCIDType0C,
40   fontCIDType2
41 };
42
43 //------------------------------------------------------------------------
44 // GfxFontCIDWidths
45 //------------------------------------------------------------------------
46
47 struct GfxFontCIDWidthExcep {
48   CID first;                    // this record applies to
49   CID last;                     //   CIDs <first>..<last>
50   double width;                 // char width
51 };
52
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
58 };
59
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
67     excepsV;
68   int nExcepsV;                 // number of valid entries in excepsV
69 };
70
71 //------------------------------------------------------------------------
72 // GfxFont
73 //------------------------------------------------------------------------
74
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)
80
81 class GfxFont {
82 public:
83
84   // Build a GfxFont object.
85   static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
86
87   GfxFont(char *tagA, Ref idA, GString *nameA);
88
89   virtual ~GfxFont();
90
91   GBool isOk() { return ok; }
92
93   // Get font tag.
94   GString *getTag() { return tag; }
95
96   // Get font dictionary ID.
97   Ref *getID() { return &id; }
98
99   // Does this font match the tag?
100   GBool matches(char *tagA) { return !tag->cmp(tagA); }
101
102   // Get base font name.
103   GString *getName() { return name; }
104
105   // Get font type.
106   GfxFontType getType() { return type; }
107   virtual GBool isCIDFont() { return gFalse; }
108
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; }
113
114   // Get the PostScript font name for the embedded font.  Returns
115   // NULL if there is no embedded font.
116   GString *getEmbeddedFontName() { return embFontName; }
117
118   // Get the name of the external font file.  Returns NULL if there
119   // is no external font file.
120   GString *getExtFontFile() { return extFontFile; }
121
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; }
128
129   // Return the font matrix.
130   double *getFontMatrix() { return fontMat; }
131
132   // Return the font bounding box.
133   double *getFontBBox() { return fontBBox; }
134
135   // Return the ascent and descent values.
136   double getAscent() { return ascent; }
137   double getDescent() { return descent; }
138
139   // Return the writing mode (0=horizontal, 1=vertical).
140   virtual int getWMode() { return 0; }
141
142   // Read an external or embedded font file into a buffer.
143   char *readExtFontFile(int *len);
144   char *readEmbFontFile(XRef *xref, int *len);
145
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
151   // the char code.
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;
155
156 protected:
157
158   void readFontDescriptor(XRef *xref, Dict *fontDict);
159   CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits);
160   void findExtFontFile();
161
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
175   GBool ok;
176 };
177
178 //------------------------------------------------------------------------
179 // Gfx8BitFont
180 //------------------------------------------------------------------------
181
182 class Gfx8BitFont: public GfxFont {
183 public:
184
185   Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
186               GfxFontType typeA, Dict *fontDict);
187
188   virtual ~Gfx8BitFont();
189
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);
193
194   // Return the encoding.
195   char **getEncoding() { return enc; }
196
197   // Return the Unicode map.
198   CharCodeToUnicode *getToUnicode();
199
200   // Return the character name associated with <code>.
201   char *getCharName(int code) { return code>=256?0:enc[code]; }
202
203   // Returns true if the PDF font specified an encoding.
204   GBool getHasEncoding() { return hasEncoding; }
205
206   // Get width of a character or string.
207   double getWidth(Guchar c) { return widths[c]; }
208
209   // Return the Type 3 CharProc dictionary, or NULL if none.
210   Dict *getCharProcs();
211
212   // Return the Type 3 CharProc for the character associated with <code>.
213   Object *getCharProc(int code, Object *proc);
214
215   // Return the Type 3 Resources dictionary, or NULL if none.
216   Dict *getResources();
217
218 private:
219
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
224   GBool hasEncoding;
225   double widths[256];           // character widths
226   Object charProcs;             // Type 3 CharProcs dictionary
227   Object resources;             // Type 3 Resources dictionary
228 };
229
230 //------------------------------------------------------------------------
231 // GfxCIDFont
232 //------------------------------------------------------------------------
233
234 class GfxCIDFont: public GfxFont {
235 public:
236
237   GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
238              Dict *fontDict);
239
240   virtual ~GfxCIDFont();
241
242   virtual GBool isCIDFont() { return gTrue; }
243
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);
247
248   // Return the writing mode (0=horizontal, 1=vertical).
249   virtual int getWMode();
250
251   // Return the Unicode map.
252   CharCodeToUnicode *getToUnicode();
253
254   // Get the collection name (<registry>-<ordering>).
255   GString *getCollection();
256
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; }
261
262 private:
263
264   CMap *cMap;                   // char code --> CID
265   CharCodeToUnicode *ctu;       // CID --> Unicode
266   GfxFontCIDWidths widths;      // character widths
267   Gushort *cidToGID;            // CID --> GID mapping (for embedded
268                                 //   TrueType fonts)
269   int cidToGIDLen;
270 };
271
272 //------------------------------------------------------------------------
273 // GfxFontDict
274 //------------------------------------------------------------------------
275
276 class GfxFontDict {
277 public:
278
279   // Build the font dictionary, given the PDF font dictionary.
280   GfxFontDict(XRef *xref, Dict *fontDict);
281
282   // Destructor.
283   ~GfxFontDict();
284
285   // Get the specified font.
286   GfxFont *lookup(char *tag);
287
288   // Iterative access.
289   int getNumFonts() { return numFonts; }
290   GfxFont *getFont(int i) { return fonts[i]; }
291
292 private:
293
294   GfxFont **fonts;              // list of fonts
295   int numFonts;                 // number of fonts
296 };
297
298 #endif