Initial revision
[swftools.git] / pdf2swf / xpdf / GfxFont.h
1 //========================================================================
2 //
3 // GfxFont.h
4 //
5 // Copyright 1996 Derek B. Noonburg
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 "FontEncoding.h"
20
21 class Dict;
22 struct BuiltinFont;
23
24 //------------------------------------------------------------------------
25 // GfxFontCharSet16
26 //------------------------------------------------------------------------
27
28 enum GfxFontCharSet16 {
29   font16AdobeJapan12,           // Adobe-Japan1-2
30   font16AdobeGB12,              // Adobe-GB1-2 (Chinese)
31   font16AdobeCNS13              // Adobe-CNS1-3 (Chinese)
32 };
33
34 //------------------------------------------------------------------------
35 // GfxFontEncoding16
36 //------------------------------------------------------------------------
37
38 struct GfxFontEncoding16 {
39   int wMode;                    // writing mode (0=horizontal, 1=vertical)
40   Guchar codeLen[256];          // length of codes, in bytes, indexed by
41                                 //   first byte of code
42   Gushort map1[256];            // one-byte code mapping:
43                                 //   map1[code] --> 16-bit char selector
44   Gushort *map2;                // two-byte code mapping
45                                 //   map2[2*i]   --> first code in range
46                                 //   map2[2*i+1] --> 16-bit char selector
47                                 //                   for map2[2*i]
48   int map2Len;                  // length of map2 array (divided by 2)
49 };
50
51 //------------------------------------------------------------------------
52 // GfxFontWidths16
53 //------------------------------------------------------------------------
54
55 struct GfxFontWidthExcep {
56   int first;                    // this record applies to
57   int last;                     //   chars <first>..<last>
58   double width;                 // char width
59 };
60
61 struct GfxFontWidthExcepV {
62   int first;                    // this record applies to
63   int last;                     //   chars <first>..<last>
64   double height;                // char height
65   double vx, vy;                // origin position
66 };
67
68 struct GfxFontWidths16 {
69   double defWidth;              // default char width
70   double defHeight;             // default char height
71   double defVY;                 // default origin position
72   GfxFontWidthExcep *exceps;    // exceptions
73   int numExceps;                // number of valid entries in exceps
74   GfxFontWidthExcepV *excepsV;  // exceptions for vertical font
75   int numExcepsV;               // number of valid entries in excepsV
76 };
77
78 //------------------------------------------------------------------------
79 // GfxFont
80 //------------------------------------------------------------------------
81
82 #define fontFixedWidth (1 << 0)
83 #define fontSerif      (1 << 1)
84 #define fontSymbolic   (1 << 2)
85 #define fontItalic     (1 << 6)
86 #define fontBold       (1 << 18)
87
88 enum GfxFontType {
89   fontUnknownType,
90   fontType1,
91   fontType1C,
92   fontType3,
93   fontTrueType,
94   fontType0
95 };
96
97 class GfxFont {
98 public:
99
100   // Constructor.
101   GfxFont(char *tag1, Ref id1, Dict *fontDict);
102
103   // Destructor.
104   ~GfxFont();
105
106   // Get font tag.
107   GString *getTag() { return tag; }
108
109   // Get font dictionary ID.
110   Ref getID() { return id; }
111
112   // Does this font match the tag?
113   GBool matches(char *tag1) { return !tag->cmp(tag1); }
114
115   // Get base font name.
116   GString *getName() { return name; }
117
118   // Get font type.
119   GfxFontType getType() { return type; }
120
121   // Does this font use 16-bit characters?
122   GBool is16Bit() { return is16; }
123
124   // Get embedded font ID, i.e., a ref for the font file stream.
125   // Returns false if there is no embedded font.
126   GBool getEmbeddedFontID(Ref *embID)
127     { *embID = embFontID; return embFontID.num >= 0; }
128
129   // Get the PostScript font name for the embedded font.  Returns
130   // NULL if there is no embedded font.
131   char *getEmbeddedFontName()
132     { return embFontName ? embFontName->getCString() : (char *)NULL; }
133
134   // Get the name of the external font file.  Returns NULL if there
135   // is no external font file.
136   GString *getExtFontFile() { return extFontFile; }
137
138   // Get font descriptor flags.
139   GBool isFixedWidth() { return flags & fontFixedWidth; }
140   GBool isSerif() { return flags & fontSerif; }
141   GBool isSymbolic() { return flags & fontSymbolic; }
142   GBool isItalic() { return flags & fontItalic; }
143   GBool isBold() { return flags & fontBold; }
144
145   // Get width of a character or string.
146   double getWidth(Guchar c) { return widths[c]; }
147   double getWidth(GString *s);
148
149   // Get character metrics for 16-bit font.
150   double getWidth16(int c);
151   double getHeight16(int c);
152   double getOriginX16(int c);
153   double getOriginY16(int c);
154
155   // Return the encoding.
156   FontEncoding *getEncoding() { return encoding; }
157
158   // Return the character name associated with <code>.
159   char *getCharName(int code) { return encoding->getCharName(code); }
160
161   // Return the code associated with <name>.
162   int getCharCode(char *charName) { return encoding->getCharCode(charName); }
163
164   // Return the Type 3 CharProc for the character associated with <code>.
165   Object *getCharProc(int code, Object *proc);
166
167   // Return the 16-bit character set and encoding.
168   GfxFontCharSet16 getCharSet16() { return enc16.charSet; }
169   GfxFontEncoding16 *getEncoding16() { return enc16.enc; }
170
171   // Get the writing mode (0=horizontal, 1=vertical).
172   int getWMode16() { return enc16.enc->wMode; }
173
174   // Return the font matrix.
175   double *getFontMatrix() { return fontMat; }
176
177   // Read an external or embedded font file into a buffer.
178   char *readExtFontFile(int *len);
179   char *readEmbFontFile(int *len);
180
181 private:
182
183   void getEncAndWidths(Dict *fontDict, BuiltinFont *builtinFont,
184                        int missingWidth);
185   void findExtFontFile();
186   void makeWidths(Dict *fontDict, FontEncoding *builtinEncoding,
187                   Gushort *builtinWidths, int missingWidth);
188   void getType0EncAndWidths(Dict *fontDict);
189
190   GString *tag;                 // PDF font tag
191   Ref id;                       // reference (used as unique ID)
192   GString *name;                // font name
193   int flags;                    // font descriptor flags
194   GfxFontType type;             // type of font
195   GBool is16;                   // set if font uses 16-bit chars
196   GString *embFontName;         // name of embedded font
197   Ref embFontID;                // ref to embedded font file stream
198   GString *extFontFile;         // external font file name
199   Object charProcs;             // Type3 CharProcs dictionary
200   double fontMat[6];            // font matrix
201   union {
202     FontEncoding *encoding;     // 8-bit font encoding
203     struct {
204       GfxFontCharSet16 charSet; // 16-bit character set
205       GfxFontEncoding16 *enc;   // 16-bit encoding (CMap)
206     } enc16;
207   };
208   union {
209     double widths[256];         // width of each char for 8-bit font
210     GfxFontWidths16 widths16;   // char widths for 16-bit font
211   };
212 };
213
214 //------------------------------------------------------------------------
215 // GfxFontDict
216 //------------------------------------------------------------------------
217
218 class GfxFontDict {
219 public:
220
221   // Build the font dictionary, given the PDF font dictionary.
222   GfxFontDict(Dict *fontDict);
223
224   // Destructor.
225   ~GfxFontDict();
226
227   // Get the specified font.
228   GfxFont *lookup(char *tag);
229
230   // Iterative access.
231   int getNumFonts() { return numFonts; }
232   GfxFont *getFont(int i) { return fonts[i]; }
233
234 private:
235
236   GfxFont **fonts;              // list of fonts
237   int numFonts;                 // number of fonts
238 };
239
240 #endif