]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/GfxFont.h
kill traces of ltk, incorporate new sources
[evince.git] / pdf / 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 #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 struct GfxFontCIDWidths;
27
28 //------------------------------------------------------------------------
29 // GfxFontType
30 //------------------------------------------------------------------------
31
32 enum GfxFontType {
33   //----- Gfx8BitFont
34   fontUnknownType,
35   fontType1,
36   fontType1C,
37   fontType3,
38   fontTrueType,
39   //----- GfxCIDFont
40   fontCIDType0,
41   fontCIDType0C,
42   fontCIDType2
43 };
44
45 //------------------------------------------------------------------------
46 // GfxFontCIDWidths
47 //------------------------------------------------------------------------
48
49 struct GfxFontCIDWidthExcep {
50   CID first;                    // this record applies to
51   CID last;                     //   CIDs <first>..<last>
52   double width;                 // char width
53 };
54
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
60 };
61
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
69     excepsV;
70   int nExcepsV;                 // number of valid entries in excepsV
71 };
72
73 //------------------------------------------------------------------------
74 // GfxFont
75 //------------------------------------------------------------------------
76
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)
82
83 class GfxFont {
84 public:
85
86   // Build a GfxFont object.
87   static GfxFont *makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict);
88
89   GfxFont(char *tagA, Ref idA, GString *nameA);
90
91   virtual ~GfxFont();
92
93   GBool isOk() { return ok; }
94
95   // Get font tag.
96   GString *getTag() { return tag; }
97
98   // Get font dictionary ID.
99   Ref *getID() { return &id; }
100
101   // Does this font match the tag?
102   GBool matches(char *tagA) { return !tag->cmp(tagA); }
103
104   // Get base font name.
105   GString *getName() { return name; }
106
107   // Get font type.
108   GfxFontType getType() { return type; }
109   virtual GBool isCIDFont() { return gFalse; }
110
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; }
115
116   // Get the PostScript font name for the embedded font.  Returns
117   // NULL if there is no embedded font.
118   GString *getEmbeddedFontName() { return embFontName; }
119
120   // Get the name of the external font file.  Returns NULL if there
121   // is no external font file.
122   GString *getExtFontFile() { return extFontFile; }
123
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; }
130
131   // Return the font matrix.
132   double *getFontMatrix() { return fontMat; }
133
134   // Return the font bounding box.
135   double *getFontBBox() { return fontBBox; }
136
137   // Return the ascent and descent values.
138   double getAscent() { return ascent; }
139   double getDescent() { return descent; }
140
141   // Return the writing mode (0=horizontal, 1=vertical).
142   virtual int getWMode() { return 0; }
143
144   // Read an external or embedded font file into a buffer.
145   char *readExtFontFile(int *len);
146   char *readEmbFontFile(XRef *xref, int *len);
147
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
153   // the char code.
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;
157
158 protected:
159
160   void readFontDescriptor(XRef *xref, Dict *fontDict);
161   CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits);
162   void findExtFontFile();
163
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
177   GBool ok;
178 };
179
180 //------------------------------------------------------------------------
181 // Gfx8BitFont
182 //------------------------------------------------------------------------
183
184 class Gfx8BitFont: public GfxFont {
185 public:
186
187   Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
188               GfxFontType typeA, Dict *fontDict);
189
190   virtual ~Gfx8BitFont();
191
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);
195
196   // Return the encoding.
197   char **getEncoding() { return enc; }
198
199   // Return the Unicode map.
200   CharCodeToUnicode *getToUnicode();
201
202   // Return the character name associated with <code>.
203   char *getCharName(int code) { return enc[code]; }
204
205   // Returns true if the PDF font specified an encoding.
206   GBool getHasEncoding() { return hasEncoding; }
207
208   // Get width of a character or string.
209   double getWidth(Guchar c) { return widths[c]; }
210
211   // Return the Type 3 CharProc dictionary, or NULL if none.
212   Dict *getCharProcs();
213
214   // Return the Type 3 CharProc for the character associated with <code>.
215   Object *getCharProc(int code, Object *proc);
216
217   // Return the Type 3 Resources dictionary, or NULL if none.
218   Dict *getResources();
219
220 private:
221
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
226   GBool hasEncoding;
227   double widths[256];           // character widths
228   Object charProcs;             // Type 3 CharProcs dictionary
229   Object resources;             // Type 3 Resources dictionary
230 };
231
232 //------------------------------------------------------------------------
233 // GfxCIDFont
234 //------------------------------------------------------------------------
235
236 class GfxCIDFont: public GfxFont {
237 public:
238
239   GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
240              Dict *fontDict);
241
242   virtual ~GfxCIDFont();
243
244   virtual GBool isCIDFont() { return gTrue; }
245
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);
249
250   // Return the writing mode (0=horizontal, 1=vertical).
251   virtual int getWMode();
252
253   // Return the Unicode map.
254   CharCodeToUnicode *getToUnicode();
255
256   // Get the collection name (<registry>-<ordering>).
257   GString *getCollection();
258
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; }
263
264 private:
265
266   CMap *cMap;                   // char code --> CID
267   CharCodeToUnicode *ctu;       // CID --> Unicode
268   GfxFontCIDWidths widths;      // character widths
269   Gushort *cidToGID;            // CID --> GID mapping (for embedded
270                                 //   TrueType fonts)
271   int cidToGIDLen;
272 };
273
274 //------------------------------------------------------------------------
275 // GfxFontDict
276 //------------------------------------------------------------------------
277
278 class GfxFontDict {
279 public:
280
281   // Build the font dictionary, given the PDF font dictionary.
282   GfxFontDict(XRef *xref, Dict *fontDict);
283
284   // Destructor.
285   ~GfxFontDict();
286
287   // Get the specified font.
288   GfxFont *lookup(char *tag);
289
290   // Iterative access.
291   int getNumFonts() { return numFonts; }
292   GfxFont *getFont(int i) { return fonts[i]; }
293
294 private:
295
296   GfxFont **fonts;              // list of fonts
297   int numFonts;                 // number of fonts
298 };
299
300 #endif