applied diff between xpdf-1.01-orig and xpdf-1.01-swftools.
[swftools.git] / pdf2swf / xpdf / GfxFont.h
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   fontType3,
39   fontTrueType,
40   //----- GfxCIDFont
41   fontCIDType0,
42   fontCIDType0C,
43   fontCIDType2
44 };
45
46 //------------------------------------------------------------------------
47 // GfxFontCIDWidths
48 //------------------------------------------------------------------------
49
50 struct GfxFontCIDWidthExcep {
51   CID first;                    // this record applies to
52   CID last;                     //   CIDs <first>..<last>
53   double width;                 // char width
54 };
55
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
61 };
62
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
70     excepsV;
71   int nExcepsV;                 // number of valid entries in excepsV
72 };
73
74 //------------------------------------------------------------------------
75 // GfxFont
76 //------------------------------------------------------------------------
77
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)
83
84 class GfxFont {
85 public:
86
87   // Build a GfxFont object.
88   static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
89
90   GfxFont(char *tagA, Ref idA, GString *nameA);
91
92   virtual ~GfxFont();
93
94   GBool isOk() { return ok; }
95
96   // Get font tag.
97   GString *getTag() { return tag; }
98
99   // Get font dictionary ID.
100   Ref *getID() { return &id; }
101
102   // Does this font match the tag?
103   GBool matches(char *tagA) { return !tag->cmp(tagA); }
104
105   // Get base font name.
106   GString *getName() { return name; }
107
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; }
111
112   // Get font type.
113   GfxFontType getType() { return type; }
114   virtual GBool isCIDFont() { return gFalse; }
115
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; }
120
121   // Get the PostScript font name for the embedded font.  Returns
122   // NULL if there is no embedded font.
123   GString *getEmbeddedFontName() { return embFontName; }
124
125   // Get the name of the external font file.  Returns NULL if there
126   // is no external font file.
127   GString *getExtFontFile() { return extFontFile; }
128
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; }
135
136   // Return the font matrix.
137   double *getFontMatrix() { return fontMat; }
138
139   // Return the font bounding box.
140   double *getFontBBox() { return fontBBox; }
141
142   // Return the ascent and descent values.
143   double getAscent() { return ascent; }
144   double getDescent() { return descent; }
145
146   // Return the writing mode (0=horizontal, 1=vertical).
147   virtual int getWMode() { return 0; }
148
149   // Read an external or embedded font file into a buffer.
150   char *readExtFontFile(int *len);
151   char *readEmbFontFile(XRef *xref, int *len);
152
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
158   // the char code.
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;
162
163 protected:
164
165   void readFontDescriptor(XRef *xref, Dict *fontDict);
166   CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits,
167                                        CharCodeToUnicode *ctu);
168   void findExtFontFile();
169
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
184   GBool ok;
185 };
186
187 //------------------------------------------------------------------------
188 // Gfx8BitFont
189 //------------------------------------------------------------------------
190
191 class Gfx8BitFont: public GfxFont {
192 public:
193
194   Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
195               GfxFontType typeA, Dict *fontDict);
196
197   virtual ~Gfx8BitFont();
198
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);
202
203   // Return the encoding.
204   char **getEncoding() { return enc; }
205
206   // Return the Unicode map.
207   CharCodeToUnicode *getToUnicode();
208
209   // Return the character name associated with <code>.
210   char *getCharName(int code) { return code>=256?0:enc[code]; }
211
212   // Returns true if the PDF font specified an encoding.
213   GBool getHasEncoding() { return hasEncoding; }
214
215   // Returns true if the PDF font specified MacRomanEncoding.
216   GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
217
218   // Get width of a character.
219   double getWidth(Guchar c) { return widths[c]; }
220
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);
224
225   // Return the Type 3 CharProc dictionary, or NULL if none.
226   Dict *getCharProcs();
227
228   // Return the Type 3 CharProc for the character associated with <code>.
229   Object *getCharProc(int code, Object *proc);
230
231   // Return the Type 3 Resources dictionary, or NULL if none.
232   Dict *getResources();
233
234 private:
235
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
240   GBool hasEncoding;
241   GBool usesMacRomanEnc;
242   double widths[256];           // character widths
243   Object charProcs;             // Type 3 CharProcs dictionary
244   Object resources;             // Type 3 Resources dictionary
245 };
246
247 //------------------------------------------------------------------------
248 // GfxCIDFont
249 //------------------------------------------------------------------------
250
251 class GfxCIDFont: public GfxFont {
252 public:
253
254   GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
255              Dict *fontDict);
256
257   virtual ~GfxCIDFont();
258
259   virtual GBool isCIDFont() { return gTrue; }
260
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);
264
265   // Return the writing mode (0=horizontal, 1=vertical).
266   virtual int getWMode();
267
268   // Return the Unicode map.
269   CharCodeToUnicode *getToUnicode();
270
271   // Get the collection name (<registry>-<ordering>).
272   GString *getCollection();
273
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; }
278
279 private:
280
281   CMap *cMap;                   // char code --> CID
282   CharCodeToUnicode *ctu;       // CID --> Unicode
283   GfxFontCIDWidths widths;      // character widths
284   Gushort *cidToGID;            // CID --> GID mapping (for embedded
285                                 //   TrueType fonts)
286   int cidToGIDLen;
287 };
288
289 //------------------------------------------------------------------------
290 // GfxFontDict
291 //------------------------------------------------------------------------
292
293 class GfxFontDict {
294 public:
295
296   // Build the font dictionary, given the PDF font dictionary.
297   GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
298
299   // Destructor.
300   ~GfxFontDict();
301
302   // Get the specified font.
303   GfxFont *lookup(char *tag);
304
305   // Iterative access.
306   int getNumFonts() { return numFonts; }
307   GfxFont *getFont(int i) { return fonts[i]; }
308
309 private:
310
311   GfxFont **fonts;              // list of fonts
312   int numFonts;                 // number of fonts
313 };
314
315 #endif