]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/FontFile.h
Imported Xpdf 2.03 and fixed build.
[evince.git] / pdf / xpdf / FontFile.h
1 //========================================================================
2 //
3 // FontFile.h
4 //
5 // Copyright 1999-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef FONTFILE_H
10 #define FONTFILE_H
11
12 #include <aconf.h>
13
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17
18 #include <stdio.h>
19 #include "gtypes.h"
20 #include "GString.h"
21 #include "CharTypes.h"
22
23 class GHash;
24 class CharCodeToUnicode;
25
26 //------------------------------------------------------------------------
27
28 typedef void (*FontFileOutputFunc)(void *stream, char *data, int len);
29
30 //------------------------------------------------------------------------
31 // FontFile
32 //------------------------------------------------------------------------
33
34 class FontFile {
35 public:
36
37   FontFile();
38   virtual ~FontFile();
39
40   // Returns the font name, as specified internally by the font file.
41   // Returns NULL if no name is available.
42   virtual char *getName() = 0;
43
44   // Returns the custom font encoding, or NULL if the encoding is not
45   // available.
46   virtual char **getEncoding() = 0;
47 };
48
49 //------------------------------------------------------------------------
50 // Type1FontFile
51 //------------------------------------------------------------------------
52
53 class Type1FontFile: public FontFile {
54 public:
55
56   Type1FontFile(char *file, int len);
57   virtual ~Type1FontFile();
58   virtual char *getName() { return name; }
59   virtual char **getEncoding() { return encoding; }
60
61 private:
62
63   char *name;
64   char **encoding;
65 };
66
67 //------------------------------------------------------------------------
68 // Type1CFontFile
69 //------------------------------------------------------------------------
70
71 struct Type1CTopDict;
72 struct Type1CPrivateDict;
73
74 class Type1CFontFile: public FontFile {
75 public:
76
77   Type1CFontFile(char *fileA, int lenA);
78   virtual ~Type1CFontFile();
79   GBool isOk() { return ok; }
80
81   virtual char *getName();
82   virtual char **getEncoding();
83
84   // Convert to a Type 1 font, suitable for embedding in a PostScript
85   // file.  The name will be used as the PostScript font name.
86   void convertToType1(FontFileOutputFunc outputFuncA, void *outputStreamA);
87
88   // Convert to a Type 0 CIDFont, suitable for embedding in a
89   // PostScript file.  The name will be used as the PostScript font
90   // name.
91   void convertToCIDType0(char *psName,
92                          FontFileOutputFunc outputFuncA, void *outputStreamA);
93
94   // Convert to a Type 0 (but non-CID) composite font, suitable for
95   // embedding in a PostScript file.  The name will be used as the
96   // PostScript font name.
97   void convertToType0(char *psName,
98                       FontFileOutputFunc outputFuncA, void *outputStreamA);
99
100 private:
101
102   void readEncoding();
103   void readTopDict(Type1CTopDict *dict);
104   void readPrivateDict(Type1CPrivateDict *privateDict,
105                        int offset, int size);
106   Gushort *readCharset(int charset, int nGlyphs);
107   void eexecWrite(char *s);
108   void eexecCvtGlyph(char *glyphName, int pos, int n);
109   void cvtGlyph(int pos, int n, GBool top);
110   void cvtGlyphWidth(GBool useOp);
111   void eexecDumpNum(double x, GBool fpA);
112   void eexecDumpOp1(int opA);
113   void eexecDumpOp2(int opA);
114   void eexecWriteCharstring(Guchar *s, int n);
115   void getDeltaInt(char *buf, char *key, double *opA, int n);
116   void getDeltaReal(char *buf, char *key, double *opA, int n);
117   int getIndexLen(int indexPos);
118   int getIndexValPos(int indexPos, int i, int *valLen);
119   int getIndexEnd(int indexPos);
120   Guint getWord(int pos, int size);
121   double getNum(int *pos, GBool *fp);
122   char *getString(int sid, char *buf);
123
124   Guchar *file;
125   int len;
126
127   GString *name;
128   char **encoding;
129
130   int topDictIdxPos;
131   int stringIdxPos;
132   int gsubrIdxPos;
133   int subrIdxPos;
134   int gsubrBias;
135   int subrBias;
136
137   FontFileOutputFunc outputFunc;
138   void *outputStream;
139   double op[48];                // operands
140   GBool fp[48];                 // true if operand is fixed point
141   int nOps;                     // number of operands
142   int nHints;                   // number of hints for the current glyph
143   GBool firstOp;                // true if we haven't hit the first op yet
144   double defaultWidthX;         // default glyph width
145   double nominalWidthX;         // nominal glyph width
146   GBool defaultWidthXFP;        // true if defaultWidthX is fixed point
147   GBool nominalWidthXFP;        // true if nominalWidthX is fixed point
148   Gushort r1;                   // eexec encryption key
149   GString *charBuf;             // charstring output buffer
150   int line;                     // number of eexec chars on current line
151
152   GBool ok;
153 };
154
155 //------------------------------------------------------------------------
156 // TrueTypeFontFile
157 //------------------------------------------------------------------------
158
159 struct TTFontTableHdr;
160 struct TTFontCmap;
161
162 class TrueTypeFontFile: public FontFile {
163 public:
164
165   TrueTypeFontFile(char *fileA, int lenA);
166   ~TrueTypeFontFile();
167
168   // This always returns NULL, since it's probably better to trust the
169   // font name in the PDF file rather than the one in the TrueType
170   // font file.
171   virtual char *getName();
172
173   virtual char **getEncoding();
174
175   // Return the number of cmaps defined by this font.
176   int getNumCmaps();
177
178   // Return the platform ID of the <i>th cmap.
179   int getCmapPlatform(int i);
180
181   // Return the encoding ID of the <i>th cmap.
182   int getCmapEncoding(int i);
183
184   // Return the index of the cmap for <platform>, <encoding>.  Returns
185   // -1 if there is no corresponding cmap.
186   int findCmap(int platform, int enc);
187
188   // Return the GID corresponding to <c> according to the <i>th cmap.
189   Gushort mapCodeToGID(int i, int c);
190
191   // Return a name-to-GID mapping, constructed from the font's post
192   // table.  Returns NULL if there is no post table.
193   GHash *getNameToGID();
194
195   // Convert to a Type 42 font, suitable for embedding in a PostScript
196   // file.  The name will be used as the PostScript font name (so we
197   // don't need to depend on the 'name' table in the font).  The
198   // encoding is needed because the PDF Font object can modify the
199   // encoding.
200   void convertToType42(char *name, char **encodingA,
201                        GBool pdfFontHasEncoding,
202                        Gushort *codeToGID,
203                        FontFileOutputFunc outputFunc, void *outputStream);
204
205   // Convert to a Type 2 CIDFont, suitable for embedding in a
206   // PostScript file.  The name will be used as the PostScript font
207   // name (so we don't need to depend on the 'name' table in the
208   // font).
209   void convertToCIDType2(char *name, Gushort *cidMap, int nCIDs,
210                          FontFileOutputFunc outputFunc, void *outputStream);
211
212   // Convert to a Type 0 (but non-CID) composite font, suitable for
213   // embedding in a PostScript file.  The name will be used as the
214   // PostScript font name (so we don't need to depend on the 'name'
215   // table in the font).
216   void convertToType0(char *name, Gushort *cidMap, int nCIDs,
217                       FontFileOutputFunc outputFunc, void *outputStream);
218
219   // Write a TTF file, filling in any missing tables that are required
220   // by the TrueType spec.  If the font already has all the required
221   // tables, it will be written unmodified.
222   void writeTTF(FILE *out);
223
224 private:
225
226   char *file;
227   int len;
228
229   char **encoding;
230
231   TTFontTableHdr *tableHdrs;
232   int nTables;
233   int bbox[4];
234   int locaFmt;
235   int nGlyphs;
236   GBool mungedCmapSize;
237   TTFontCmap *cmaps;
238   int nCmaps;
239
240   int getByte(int pos);
241   int getChar(int pos);
242   int getUShort(int pos);
243   int getShort(int pos);
244   Guint getULong(int pos);
245   double getFixed(int pos);
246   int seekTable(char *tag);
247   int seekTableIdx(char *tag);
248   void cvtEncoding(char **encodingA, GBool pdfFontHasEncoding,
249                    FontFileOutputFunc outputFunc, void *outputStream);
250   void cvtCharStrings(char **encodingA, GBool pdfFontHasEncoding,
251                       Gushort *codeToGID,
252                       FontFileOutputFunc outputFunc, void *outputStream);
253   int getCmapEntry(int cmapFmt, int pos, int code);
254   void cvtSfnts(FontFileOutputFunc outputFunc, void *outputStream,
255                 GString *name);
256   void dumpString(char *s, int length,
257                   FontFileOutputFunc outputFunc, void *outputStream);
258   Guint computeTableChecksum(char *data, int length);
259 };
260
261 #endif