upgrade to xpdf-3.00.
[swftools.git] / pdf2swf / xpdf / FontFile.h
1 //========================================================================
2 //
3 // FontFile.h
4 //
5 // Copyright 1999-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef FONTFILE_H
10 #define FONTFILE_H
11
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
15
16 #include <stdio.h>
17 #include "gtypes.h"
18 #include "GString.h"
19 #include "CharTypes.h"
20
21 class CharCodeToUnicode;
22
23 //------------------------------------------------------------------------
24 // FontFile
25 //------------------------------------------------------------------------
26
27 class FontFile {
28 public:
29
30   FontFile();
31   virtual ~FontFile();
32
33   // Returns the font name, as specified internally by the font file.
34   // Returns NULL if no name is available.
35   virtual char *getName() = 0;
36
37   // Returns the custom font encoding, or NULL if the encoding is not
38   // available.
39   virtual char **getEncoding() = 0;
40 };
41
42 //------------------------------------------------------------------------
43 // Type1FontFile
44 //------------------------------------------------------------------------
45
46 class Type1FontFile: public FontFile {
47 public:
48
49   Type1FontFile(char *file, int len);
50   virtual ~Type1FontFile();
51   virtual char *getName() { return name; }
52   virtual char **getEncoding() { return encoding; }
53
54 private:
55
56   char *name;
57   char **encoding;
58 };
59
60 //------------------------------------------------------------------------
61 // Type1CFontFile
62 //------------------------------------------------------------------------
63
64 struct Type1CTopDict;
65 struct Type1CPrivateDict;
66
67 class Type1CFontFile: public FontFile {
68 public:
69
70   Type1CFontFile(char *fileA, int lenA);
71   virtual ~Type1CFontFile();
72
73   virtual char *getName();
74   virtual char **getEncoding();
75
76   // Convert to a Type 1 font, suitable for embedding in a PostScript
77   // file.  The name will be used as the PostScript font name.
78   void convertToType1(FILE *outA);
79
80   // Convert to a Type 0 CIDFont, suitable for embedding in a
81   // PostScript file.  The name will be used as the PostScript font
82   // name.
83   void convertToCIDType0(char *psName, FILE *outA);
84
85   // Convert to a Type 0 (but non-CID) composite font, suitable for
86   // embedding in a PostScript file.  The name will be used as the
87   // PostScript font name.
88   void convertToType0(char *psName, FILE *outA);
89
90 private:
91
92   void readNameAndEncoding();
93   void readTopDict(Type1CTopDict *dict);
94   void readPrivateDict(Type1CPrivateDict *privateDict,
95                        int offset, int size);
96   Gushort *readCharset(int charset, int nGlyphs);
97   void eexecWrite(char *s);
98   void eexecCvtGlyph(char *glyphName, Guchar *s, int n);
99   void cvtGlyph(Guchar *s, int n);
100   void cvtGlyphWidth(GBool useOp);
101   void eexecDumpNum(double x, GBool fpA);
102   void eexecDumpOp1(int opA);
103   void eexecDumpOp2(int opA);
104   void eexecWriteCharstring(Guchar *s, int n);
105   void getDeltaInt(char *buf, char *key, double *opA, int n);
106   void getDeltaReal(char *buf, char *key, double *opA, int n);
107   int getIndexLen(Guchar *indexPtr);
108   Guchar *getIndexValPtr(Guchar *indexPtr, int i);
109   Guchar *getIndexEnd(Guchar *indexPtr);
110   Guint getWord(Guchar *ptr, int size);
111   double getNum(Guchar **ptr, GBool *fp);
112   char *getString(int sid, char *buf);
113
114   char *file;
115   int len;
116
117   GString *name;
118   char **encoding;
119
120   int topOffSize;
121   Guchar *topDictIdxPtr;
122   Guchar *stringIdxPtr;
123   Guchar *gsubrIdxPtr;
124
125   FILE *out;
126   double op[48];                // operands
127   GBool fp[48];                 // true if operand is fixed point
128   int nOps;                     // number of operands
129   double defaultWidthX;         // default glyph width
130   double nominalWidthX;         // nominal glyph width
131   GBool defaultWidthXFP;        // true if defaultWidthX is fixed point
132   GBool nominalWidthXFP;        // true if nominalWidthX is fixed point
133   Gushort r1;                   // eexec encryption key
134   GString *charBuf;             // charstring output buffer
135   int line;                     // number of eexec chars on current line
136 };
137
138 //------------------------------------------------------------------------
139 // TrueTypeFontFile
140 //------------------------------------------------------------------------
141
142 struct TTFontTableHdr;
143
144 class TrueTypeFontFile: public FontFile {
145 public:
146
147   TrueTypeFontFile(char *fileA, int lenA);
148   ~TrueTypeFontFile();
149
150   // This always returns NULL, since it's probably better to trust the
151   // font name in the PDF file rather than the one in the TrueType
152   // font file.
153   virtual char *getName();
154
155   virtual char **getEncoding();
156
157   // Convert to a Type 42 font, suitable for embedding in a PostScript
158   // file.  The name will be used as the PostScript font name (so we
159   // don't need to depend on the 'name' table in the font).  The
160   // encoding is needed because the PDF Font object can modify the
161   // encoding.
162   void convertToType42(char *name, char **encodingA,
163                        CharCodeToUnicode *toUnicode,
164                        GBool pdfFontHasEncoding, FILE *out);
165
166   // Convert to a Type 2 CIDFont, suitable for embedding in a
167   // PostScript file.  The name will be used as the PostScript font
168   // name (so we don't need to depend on the 'name' table in the
169   // font).
170   void convertToCIDType2(char *name, Gushort *cidMap,
171                          int nCIDs, FILE *out);
172
173   // Convert to a Type 0 (but non-CID) composite font, suitable for
174   // embedding in a PostScript file.  The name will be used as the
175   // PostScript font name (so we don't need to depend on the 'name'
176   // table in the font).
177   void convertToType0(char *name, Gushort *cidMap,
178                       int nCIDs, FILE *out);
179
180   // Write a TTF file, filling in any missing tables that are required
181   // by the TrueType spec.  If the font already has all the required
182   // tables, it will be written unmodified.
183   void writeTTF(FILE *out);
184
185 private:
186
187   char *file;
188   int len;
189
190   char **encoding;
191
192   TTFontTableHdr *tableHdrs;
193   int nTables;
194   int bbox[4];
195   int locaFmt;
196   int nGlyphs;
197
198   int getByte(int pos);
199   int getChar(int pos);
200   int getUShort(int pos);
201   int getShort(int pos);
202   Guint getULong(int pos);
203   double getFixed(int pos);
204   int seekTable(char *tag);
205   int seekTableIdx(char *tag);
206   void cvtEncoding(char **encodingA, FILE *out);
207   void cvtCharStrings(char **encodingA, CharCodeToUnicode *toUnicode,
208                       GBool pdfFontHasEncoding, FILE *out);
209   int getCmapEntry(int cmapFmt, int pos, int code);
210   void cvtSfnts(FILE *out, GString *name);
211   void dumpString(char *s, int length, FILE *out);
212   Guint computeTableChecksum(char *data, int length);
213 };
214
215 #endif