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