1 | //======================================================================== |
2 | // |
3 | // FoFiType1C.h |
4 | // |
5 | // Copyright 1999-2003 Glyph & Cog, LLC |
6 | // |
7 | //======================================================================== |
8 | |
9 | //======================================================================== |
10 | // |
11 | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | // |
13 | // All changes made under the Poppler project to this file are licensed |
14 | // under GPL version 2 or later |
15 | // |
16 | // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de> |
17 | // Copyright (C) 2012 Thomas Freitag <Thomas.Freitag@alfa.de> |
18 | // Copyright (C) 2018-2020 Albert Astals Cid <aacid@kde.org> |
19 | // Copyright (C) 2022 Oliver Sander <oliver.sander@tu-dresden.de> |
20 | // |
21 | // To see a description of the changes please see the Changelog file that |
22 | // came with your tarball or type make ChangeLog if you are building from git |
23 | // |
24 | //======================================================================== |
25 | |
26 | #ifndef FOFITYPE1C_H |
27 | #define FOFITYPE1C_H |
28 | |
29 | #include "FoFiBase.h" |
30 | |
31 | #include "poppler_private_export.h" |
32 | |
33 | #include <set> |
34 | |
35 | class GooString; |
36 | |
37 | //------------------------------------------------------------------------ |
38 | |
39 | struct Type1CIndex |
40 | { |
41 | int pos; // absolute position in file |
42 | int len; // length (number of entries) |
43 | int offSize; // offset size |
44 | int startPos; // position of start of index data - 1 |
45 | int endPos; // position one byte past end of the index |
46 | }; |
47 | |
48 | struct Type1CIndexVal |
49 | { |
50 | int pos; // absolute position in file |
51 | int len; // length, in bytes |
52 | }; |
53 | |
54 | struct Type1CTopDict |
55 | { |
56 | int firstOp; |
57 | |
58 | int versionSID; |
59 | int noticeSID; |
60 | int copyrightSID; |
61 | int fullNameSID; |
62 | int familyNameSID; |
63 | int weightSID; |
64 | int isFixedPitch; |
65 | double italicAngle; |
66 | double underlinePosition; |
67 | double underlineThickness; |
68 | int paintType; |
69 | int charstringType; |
70 | double fontMatrix[6]; |
71 | bool hasFontMatrix; // CID fonts are allowed to put their |
72 | // FontMatrix in the FD instead of the |
73 | // top dict |
74 | int uniqueID; |
75 | double fontBBox[4]; |
76 | double strokeWidth; |
77 | int charsetOffset; |
78 | int encodingOffset; |
79 | int charStringsOffset; |
80 | int privateSize; |
81 | int privateOffset; |
82 | |
83 | // CIDFont entries |
84 | int registrySID; |
85 | int orderingSID; |
86 | int supplement; |
87 | int fdArrayOffset; |
88 | int fdSelectOffset; |
89 | }; |
90 | |
91 | #define type1CMaxBlueValues 14 |
92 | #define type1CMaxOtherBlues 10 |
93 | #define type1CMaxStemSnap 12 |
94 | |
95 | struct Type1CPrivateDict |
96 | { |
97 | double fontMatrix[6]; |
98 | bool hasFontMatrix; |
99 | int blueValues[type1CMaxBlueValues]; |
100 | int nBlueValues; |
101 | int otherBlues[type1CMaxOtherBlues]; |
102 | int nOtherBlues; |
103 | int familyBlues[type1CMaxBlueValues]; |
104 | int nFamilyBlues; |
105 | int familyOtherBlues[type1CMaxOtherBlues]; |
106 | int nFamilyOtherBlues; |
107 | double blueScale; |
108 | int blueShift; |
109 | int blueFuzz; |
110 | double stdHW; |
111 | bool hasStdHW; |
112 | double stdVW; |
113 | bool hasStdVW; |
114 | double stemSnapH[type1CMaxStemSnap]; |
115 | int nStemSnapH; |
116 | double stemSnapV[type1CMaxStemSnap]; |
117 | int nStemSnapV; |
118 | bool forceBold; |
119 | bool hasForceBold; |
120 | double forceBoldThreshold; |
121 | int languageGroup; |
122 | double expansionFactor; |
123 | int initialRandomSeed; |
124 | int subrsOffset; |
125 | double defaultWidthX; |
126 | bool defaultWidthXFP; |
127 | double nominalWidthX; |
128 | bool nominalWidthXFP; |
129 | }; |
130 | |
131 | struct Type1COp |
132 | { |
133 | bool isNum = true; // true -> number, false -> operator |
134 | bool isFP = false; // true -> floating point number, false -> int |
135 | union { |
136 | double num = 0; // if num is true |
137 | int op; // if num is false |
138 | }; |
139 | }; |
140 | |
141 | struct Type1CEexecBuf |
142 | { |
143 | FoFiOutputFunc outputFunc; |
144 | void *outputStream; |
145 | bool ascii; // ASCII encoding? |
146 | unsigned short r1; // eexec encryption key |
147 | int line; // number of eexec chars left on current line |
148 | }; |
149 | |
150 | //------------------------------------------------------------------------ |
151 | // FoFiType1C |
152 | //------------------------------------------------------------------------ |
153 | |
154 | class POPPLER_PRIVATE_EXPORT FoFiType1C : public FoFiBase |
155 | { |
156 | public: |
157 | // Create a FoFiType1C object from a memory buffer. |
158 | static FoFiType1C *make(const unsigned char *fileA, int lenA); |
159 | |
160 | // Create a FoFiType1C object from a file on disk. |
161 | static FoFiType1C *load(const char *fileName); |
162 | |
163 | ~FoFiType1C() override; |
164 | |
165 | // Return the font name. |
166 | const char *getName() const; |
167 | |
168 | // Return the encoding, as an array of 256 names (any of which may |
169 | // be NULL). This is only useful with 8-bit fonts. |
170 | char **getEncoding() const; |
171 | |
172 | // Get the glyph names. |
173 | int getNumGlyphs() const { return nGlyphs; } |
174 | GooString *getGlyphName(int gid) const; |
175 | |
176 | // Return the mapping from CIDs to GIDs, and return the number of |
177 | // CIDs in *<nCIDs>. This is only useful for CID fonts. |
178 | int *getCIDToGIDMap(int *nCIDs) const; |
179 | |
180 | // Return the font matrix as an array of six numbers. |
181 | void getFontMatrix(double *mat) const; |
182 | |
183 | // Convert to a Type 1 font, suitable for embedding in a PostScript |
184 | // file. This is only useful with 8-bit fonts. If <newEncoding> is |
185 | // not NULL, it will be used in place of the encoding in the Type 1C |
186 | // font. If <ascii> is true the eexec section will be hex-encoded, |
187 | // otherwise it will be left as binary data. If <psName> is non-NULL, |
188 | // it will be used as the PostScript font name. |
189 | void convertToType1(const char *psName, const char **newEncoding, bool ascii, FoFiOutputFunc outputFunc, void *outputStream); |
190 | |
191 | // Convert to a Type 0 CIDFont, suitable for embedding in a |
192 | // PostScript file. <psName> will be used as the PostScript font |
193 | // name. There are three cases for the CID-to-GID mapping: |
194 | // (1) if <codeMap> is non-NULL, then it is the CID-to-GID mapping |
195 | // (2) if <codeMap> is NULL and this is a CID CFF font, then the |
196 | // font's internal CID-to-GID mapping is used |
197 | // (3) is <codeMap> is NULL and this is an 8-bit CFF font, then |
198 | // the identity CID-to-GID mapping is used |
199 | void convertToCIDType0(const char *psName, const int *codeMap, int nCodes, FoFiOutputFunc outputFunc, void *outputStream); |
200 | |
201 | // Convert to a Type 0 (but non-CID) composite font, suitable for |
202 | // embedding in a PostScript file. <psName> will be used as the |
203 | // PostScript font name. There are three cases for the CID-to-GID |
204 | // mapping: |
205 | // (1) if <codeMap> is non-NULL, then it is the CID-to-GID mapping |
206 | // (2) if <codeMap> is NULL and this is a CID CFF font, then the |
207 | // font's internal CID-to-GID mapping is used |
208 | // (3) is <codeMap> is NULL and this is an 8-bit CFF font, then |
209 | // the identity CID-to-GID mapping is used |
210 | void convertToType0(const char *psName, const int *codeMap, int nCodes, FoFiOutputFunc outputFunc, void *outputStream); |
211 | |
212 | private: |
213 | FoFiType1C(const unsigned char *fileA, int lenA, bool freeFileDataA); |
214 | void eexecCvtGlyph(Type1CEexecBuf *eb, const char *glyphName, int offset, int nBytes, const Type1CIndex *subrIdx, const Type1CPrivateDict *pDict); |
215 | void cvtGlyph(int offset, int nBytes, GooString *charBuf, const Type1CIndex *subrIdx, const Type1CPrivateDict *pDict, bool top, std::set<int> &offsetBeingParsed); |
216 | void cvtGlyphWidth(bool useOp, GooString *charBuf, const Type1CPrivateDict *pDict); |
217 | void cvtNum(double x, bool isFP, GooString *charBuf) const; |
218 | void eexecWrite(Type1CEexecBuf *eb, const char *s) const; |
219 | void eexecWriteCharstring(Type1CEexecBuf *eb, const unsigned char *s, int n) const; |
220 | void writePSString(const char *s, FoFiOutputFunc outputFunc, void *outputStream) const; |
221 | bool parse(); |
222 | void readTopDict(); |
223 | void readFD(int offset, int length, Type1CPrivateDict *pDict); |
224 | void readPrivateDict(int offset, int length, Type1CPrivateDict *pDict); |
225 | void readFDSelect(); |
226 | void buildEncoding(); |
227 | bool readCharset(); |
228 | int getOp(int pos, bool charstring, bool *ok); |
229 | int getDeltaIntArray(int *arr, int maxLen) const; |
230 | int getDeltaFPArray(double *arr, int maxLen) const; |
231 | void getIndex(int pos, Type1CIndex *idx, bool *ok) const; |
232 | void getIndexVal(const Type1CIndex *idx, int i, Type1CIndexVal *val, bool *ok) const; |
233 | char *getString(int sid, char *buf, bool *ok) const; |
234 | |
235 | GooString *name; |
236 | char **encoding; |
237 | |
238 | Type1CIndex nameIdx; |
239 | Type1CIndex topDictIdx; |
240 | Type1CIndex stringIdx; |
241 | Type1CIndex gsubrIdx; |
242 | Type1CIndex charStringsIdx; |
243 | |
244 | Type1CTopDict topDict; |
245 | Type1CPrivateDict *privateDicts; |
246 | |
247 | int nGlyphs; |
248 | int nFDs; |
249 | unsigned char *fdSelect; |
250 | const unsigned short *charset; |
251 | unsigned short charsetLength; |
252 | int gsubrBias; |
253 | |
254 | bool parsedOk; |
255 | |
256 | Type1COp ops[49]; // operands and operator |
257 | int nOps; // number of operands |
258 | int nHints; // number of hints for the current glyph |
259 | bool firstOp; // true if we haven't hit the first op yet |
260 | bool openPath; // true if there is an unclosed path |
261 | }; |
262 | |
263 | #endif |
264 | |