Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/libefont/cff.cc

raw
1 // -*- related-file-name: "../include/efont/cff.hh" -*-
2
3 /* cff.{cc,hh} -- Compact Font Format fonts
4 *
5 * Copyright (c) 1998-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/cff.hh>
20 #include <lcdf/error.hh>
21 #include <efont/t1item.hh>
22 #include <lcdf/straccum.hh>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <ctype.h>
28 #include <efont/t1unparser.hh>
29
30 #ifndef static_assert
31 #define static_assert(c, msg) switch ((int) (c)) case 0: case (c):
32 #endif
33
34 namespace Efont {
35
36 const char * const Cff::operator_names[] = {
37 "version", "Notice", "FullName", "FamilyName",
38 "Weight", "FontBBox", "BlueValues", "OtherBlues",
39 "FamilyBlues", "FamilyOtherBlues", "StdHW", "StdVW",
40 "UNKNOWN_12", "UniqueID", "XUID", "charset",
41 "Encoding", "CharStrings", "Private", "Subrs",
42 "defaultWidthX", "nominalWidthX", "UNKNOWN_22", "UNKNOWN_23",
43 "UNKNOWN_24", "UNKNOWN_25", "UNKNOWN_26", "UNKNOWN_27",
44 "UNKNOWN_28", "UNKNOWN_29", "UNKNOWN_30", "UNKNOWN_31",
45 "Copyright", "isFixedPitch", "ItalicAngle", "UnderlinePosition",
46 "UnderlineThickness", "PaintType", "CharstringType", "FontMatrix",
47 "StrokeWidth", "BlueScale", "BlueShift", "BlueFuzz",
48 "StemSnapH", "StemSnapV", "ForceBold", "UNKNOWN_12_15",
49 "UNKNOWN_12_16", "LanguageGroup", "ExpansionFactor", "initialRandomSeed",
50 "SyntheticBase", "PostScript", "BaseFontName", "BaseFontBlend",
51 "UNKNOWN_12_24", "UNKNOWN_12_25", "UNKNOWN_12_26", "UNKNOWN_12_27",
52 "UNKNOWN_12_28", "UNKNOWN_12_29", "ROS", "CIDFontVersion",
53 "CIDFontRevision", "CIDFontType", "CIDCount", "UIDBase",
54 "FDArray", "FDSelect", "FontName"
55 };
56
57 const int Cff::operator_types[] = {
58 tSID, tSID, tSID, tSID, // version, Notice, FullName, FamilyName
59 tSID, tArray4, tP+tArray, tP+tArray, // Weight, FontBBox, BlueValues, OtherBlues
60 tP+tArray, tP+tArray, tP+tNumber, tP+tNumber, // FamBlues, FamOthBlues, StdHW, StdVW
61 tNone, tNumber, tArray, tOffset, // escape, UniqueID, XUID, charset
62 tOffset, tOffset, tPrivateType, tP+tLocalOffset, // Encoding, CharStrings, Private, Subrs
63 tP+tNumber, tP+tNumber, tNone, tNone, // defaultWX, nominalWX, 22, 23
64 tNone, tNone, tNone, tNone, // 24, 25, 26, 27
65 tNone, tNone, tNone, tNone, // 28, 29, 30, 31
66 tSID, tBoolean, tNumber, tNumber, // Copyright, isFixedPitch, ItalicAngle, UnderlinePosition
67 tNumber, tNumber, tNumber, tArray6, // UnderlineThickness, PaintType, CharstringType, FontMatrix
68 tNumber, tP+tNumber, tP+tNumber, tP+tNumber, // StrokeWidth, BlueScale, BlueShift, BlueFuzz
69 tP+tArray, tP+tArray, tP+tBoolean, tNone, // StemSnapH, StemSnapV, ForceBold, 12 15
70 tNone, tP+tNumber, tP+tNumber, tP+tNumber, // 12 16, LanguageGroup, ExpansionFactor, initialRandomSeed
71 tNumber, tSID, tSID, tArray, // SyntheticBase, PostScript, BaseFontName, BaseFontBlend
72 tNone, tNone, tNone, tNone, // 12 24, 12 25, 12 26, 12 27
73 tNone, tNone, tArray, tNumber, // 12 28, 12 29, ROS, CIDFontVersion
74 tNumber, tNumber, tNumber, tNumber, // CIDFontRevision, CIDFontType, CIDCount, UIDBase
75 tOffset, tOffset, tSID // FDArray, FDSelect, FontName
76 };
77
78 static PermString::Initializer initializer;
79 static const char * const standard_strings[] = {
80 // Automatically generated from Appendix A of the CFF specification; do
81 // not edit. Size should be 391.
82 ".notdef", "space", "exclam", "quotedbl", "numbersign", "dollar",
83 "percent", "ampersand", "quoteright", "parenleft", "parenright",
84 "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one",
85 "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon",
86 "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C",
87 "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
88 "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash",
89 "bracketright", "asciicircum", "underscore", "quoteleft", "a", "b", "c",
90 "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
91 "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright",
92 "asciitilde", "exclamdown", "cent", "sterling", "fraction", "yen",
93 "florin", "section", "currency", "quotesingle", "quotedblleft",
94 "guillemotleft", "guilsinglleft", "guilsinglright", "fi", "fl", "endash",
95 "dagger", "daggerdbl", "periodcentered", "paragraph", "bullet",
96 "quotesinglbase", "quotedblbase", "quotedblright", "guillemotright",
97 "ellipsis", "perthousand", "questiondown", "grave", "acute", "circumflex",
98 "tilde", "macron", "breve", "dotaccent", "dieresis", "ring", "cedilla",
99 "hungarumlaut", "ogonek", "caron", "emdash", "AE", "ordfeminine", "Lslash",
100 "Oslash", "OE", "ordmasculine", "ae", "dotlessi", "lslash", "oslash", "oe",
101 "germandbls", "onesuperior", "logicalnot", "mu", "trademark", "Eth",
102 "onehalf", "plusminus", "Thorn", "onequarter", "divide", "brokenbar",
103 "degree", "thorn", "threequarters", "twosuperior", "registered", "minus",
104 "eth", "multiply", "threesuperior", "copyright", "Aacute", "Acircumflex",
105 "Adieresis", "Agrave", "Aring", "Atilde", "Ccedilla", "Eacute",
106 "Ecircumflex", "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis",
107 "Igrave", "Ntilde", "Oacute", "Ocircumflex", "Odieresis", "Ograve",
108 "Otilde", "Scaron", "Uacute", "Ucircumflex", "Udieresis", "Ugrave",
109 "Yacute", "Ydieresis", "Zcaron", "aacute", "acircumflex", "adieresis",
110 "agrave", "aring", "atilde", "ccedilla", "eacute", "ecircumflex",
111 "edieresis", "egrave", "iacute", "icircumflex", "idieresis", "igrave",
112 "ntilde", "oacute", "ocircumflex", "odieresis", "ograve", "otilde",
113 "scaron", "uacute", "ucircumflex", "udieresis", "ugrave", "yacute",
114 "ydieresis", "zcaron", "exclamsmall", "Hungarumlautsmall",
115 "dollaroldstyle", "dollarsuperior", "ampersandsmall", "Acutesmall",
116 "parenleftsuperior", "parenrightsuperior", "twodotenleader",
117 "onedotenleader", "zerooldstyle", "oneoldstyle", "twooldstyle",
118 "threeoldstyle", "fouroldstyle", "fiveoldstyle", "sixoldstyle",
119 "sevenoldstyle", "eightoldstyle", "nineoldstyle", "commasuperior",
120 "threequartersemdash", "periodsuperior", "questionsmall", "asuperior",
121 "bsuperior", "centsuperior", "dsuperior", "esuperior", "isuperior",
122 "lsuperior", "msuperior", "nsuperior", "osuperior", "rsuperior",
123 "ssuperior", "tsuperior", "ff", "ffi", "ffl", "parenleftinferior",
124 "parenrightinferior", "Circumflexsmall", "hyphensuperior", "Gravesmall",
125 "Asmall", "Bsmall", "Csmall", "Dsmall", "Esmall", "Fsmall", "Gsmall",
126 "Hsmall", "Ismall", "Jsmall", "Ksmall", "Lsmall", "Msmall", "Nsmall",
127 "Osmall", "Psmall", "Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall",
128 "Vsmall", "Wsmall", "Xsmall", "Ysmall", "Zsmall", "colonmonetary",
129 "onefitted", "rupiah", "Tildesmall", "exclamdownsmall", "centoldstyle",
130 "Lslashsmall", "Scaronsmall", "Zcaronsmall", "Dieresissmall", "Brevesmall",
131 "Caronsmall", "Dotaccentsmall", "Macronsmall", "figuredash",
132 "hypheninferior", "Ogoneksmall", "Ringsmall", "Cedillasmall",
133 "questiondownsmall", "oneeighth", "threeeighths", "fiveeighths",
134 "seveneighths", "onethird", "twothirds", "zerosuperior", "foursuperior",
135 "fivesuperior", "sixsuperior", "sevensuperior", "eightsuperior",
136 "ninesuperior", "zeroinferior", "oneinferior", "twoinferior",
137 "threeinferior", "fourinferior", "fiveinferior", "sixinferior",
138 "seveninferior", "eightinferior", "nineinferior", "centinferior",
139 "dollarinferior", "periodinferior", "commainferior", "Agravesmall",
140 "Aacutesmall", "Acircumflexsmall", "Atildesmall", "Adieresissmall",
141 "Aringsmall", "AEsmall", "Ccedillasmall", "Egravesmall", "Eacutesmall",
142 "Ecircumflexsmall", "Edieresissmall", "Igravesmall", "Iacutesmall",
143 "Icircumflexsmall", "Idieresissmall", "Ethsmall", "Ntildesmall",
144 "Ogravesmall", "Oacutesmall", "Ocircumflexsmall", "Otildesmall",
145 "Odieresissmall", "OEsmall", "Oslashsmall", "Ugravesmall", "Uacutesmall",
146 "Ucircumflexsmall", "Udieresissmall", "Yacutesmall", "Thornsmall",
147 "Ydieresissmall", "001.000", "001.001", "001.002", "001.003", "Black",
148 "Bold", "Book", "Light", "Medium", "Regular", "Roman", "Semibold"
149 };
150 static PermString standard_permstrings[Cff::NSTANDARD_STRINGS];
151 static HashMap<PermString, int> standard_permstrings_map(-1);
152
153 static const int standard_encoding[] = {
154 // Automatically generated from Appendix B of the CFF specification; do
155 // not edit. Size should be 256.
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 1, 2, 3, 4, 5, 6, 7, 8,
160 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
161 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
162 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
163 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
164 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
165 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
166 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
167 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
168 89, 90, 91, 92, 93, 94, 95, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 96, 97, 98, 99, 100, 101, 102, 103, 104,
173 105, 106, 107, 108, 109, 110, 0, 111, 112, 113,
174 114, 0, 115, 116, 117, 118, 119, 120, 121, 122,
175 0, 123, 0, 124, 125, 126, 127, 128, 129, 130,
176 131, 0, 132, 133, 0, 134, 135, 136, 137, 0,
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178 0, 0, 0, 0, 0, 138, 0, 139, 0, 0,
179 0, 0, 140, 141, 142, 143, 0, 0, 0, 0,
180 0, 144, 0, 0, 0, 145, 0, 0, 146, 147,
181 148, 149, 0, 0, 0, 0
182 };
183
184 static const int expert_encoding[] = {
185 // Automatically generated from Appendix B of the CFF specification; do
186 // not edit. Size should be 256.
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190 0, 0, 1, 229, 230, 0, 231, 232, 233, 234,
191 235, 236, 237, 238, 13, 14, 15, 99, 239, 240,
192 241, 242, 243, 244, 245, 246, 247, 248, 27, 28,
193 249, 250, 251, 252, 0, 253, 254, 255, 256, 257,
194 0, 0, 0, 258, 0, 0, 259, 260, 261, 262,
195 0, 0, 263, 264, 265, 0, 266, 109, 110, 267,
196 268, 269, 0, 270, 271, 272, 273, 274, 275, 276,
197 277, 278, 279, 280, 281, 282, 283, 284, 285, 286,
198 287, 288, 289, 290, 291, 292, 293, 294, 295, 296,
199 297, 298, 299, 300, 301, 302, 303, 0, 0, 0,
200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203 0, 304, 305, 306, 0, 0, 307, 308, 309, 310,
204 311, 0, 312, 0, 0, 313, 0, 0, 314, 315,
205 0, 0, 316, 317, 318, 0, 0, 0, 158, 155,
206 163, 319, 320, 321, 322, 323, 324, 325, 0, 0,
207 326, 150, 164, 169, 327, 328, 329, 330, 331, 332,
208 333, 334, 335, 336, 337, 338, 339, 340, 341, 342,
209 343, 344, 345, 346, 347, 348, 349, 350, 351, 352,
210 353, 354, 355, 356, 357, 358, 359, 360, 361, 362,
211 363, 364, 365, 366, 367, 368, 369, 370, 371, 372,
212 373, 374, 375, 376, 377, 378
213 };
214
215 static const int iso_adobe_charset[] = {
216 // Automatically generated from Appendix C of the CFF specification; do
217 // not edit.
218 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
219 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
220 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
221 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
222 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
223 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
224 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
225 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
226 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
227 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
228 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
229 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
230 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
231 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
232 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
233 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
234 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
235 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
236 180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
237 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
238 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
239 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
240 220, 221, 222, 223, 224, 225, 226, 227, 228
241 };
242
243 static const int expert_charset[] = {
244 // Automatically generated from Appendix C of the CFF specification; do
245 // not edit.
246 0, 1, 229, 230, 231, 232, 233, 234, 235, 236,
247 237, 238, 13, 14, 15, 99, 239, 240, 241, 242,
248 243, 244, 245, 246, 247, 248, 27, 28, 249, 250,
249 251, 252, 253, 254, 255, 256, 257, 258, 259, 260,
250 261, 262, 263, 264, 265, 266, 109, 110, 267, 268,
251 269, 270, 271, 272, 273, 274, 275, 276, 277, 278,
252 279, 280, 281, 282, 283, 284, 285, 286, 287, 288,
253 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
254 299, 300, 301, 302, 303, 304, 305, 306, 307, 308,
255 309, 310, 311, 312, 313, 314, 315, 316, 317, 318,
256 158, 155, 163, 319, 320, 321, 322, 323, 324, 325,
257 326, 150, 164, 169, 327, 328, 329, 330, 331, 332,
258 333, 334, 335, 336, 337, 338, 339, 340, 341, 342,
259 343, 344, 345, 346, 347, 348, 349, 350, 351, 352,
260 353, 354, 355, 356, 357, 358, 359, 360, 361, 362,
261 363, 364, 365, 366, 367, 368, 369, 370, 371, 372,
262 373, 374, 375, 376, 377, 378
263 };
264
265 static const int expert_subset_charset[] = {
266 // Automatically generated from Appendix C of the CFF specification; do
267 // not edit.
268 0, 1, 231, 232, 235, 236, 237, 238, 13, 14,
269 15, 99, 239, 240, 241, 242, 243, 244, 245, 246,
270 247, 248, 27, 28, 249, 250, 251, 253, 254, 255,
271 256, 257, 258, 259, 260, 261, 262, 263, 264, 265,
272 266, 109, 110, 267, 268, 269, 270, 272, 300, 301,
273 302, 305, 314, 315, 158, 155, 163, 320, 321, 322,
274 323, 324, 325, 326, 150, 164, 169, 327, 328, 329,
275 330, 331, 332, 333, 334, 335, 336, 337, 338, 339,
276 340, 341, 342, 343, 344, 345, 346
277 };
278
279 static const uint8_t default_dict_cff_data[] = {
280 // CFF header
281 1, 0, // format major and minor version
282 4, // header size
283 4, // absolute offset size
284
285 // Name INDEX
286 0, 1, // one element in index
287 1, // offset size
288 1, 14, // offset array
289 '%', 'D', 'E', 'F', 'A', 'U', 'L', 'T', 'D', 'I', 'C', 'T', '%',
290
291 // Top DICT INDEX
292 0, 1, // one element in index
293 1, // offset size
294 1, 92, // offset array
295 // and the DICT
296 139, 12, 1, // isFixedPitch false
297 139, 12, 2, // ItalicAngle 0
298 39, 12, 3, // UnderlinePosition -100
299 189, 12, 4, // UnderlineThickness 50
300 139, 12, 5, // PaintType 0
301 141, 12, 6, // CharstringType 2
302 30, 0x0A, 0x00, 0x1F, 139, 139, 30, 0x0A, 0x00, 0x1F, 139, 139, 12, 7,
303 // FontMatrix 0.001 0 0 0.001 0 0
304 139, 139, 139, 139, 5, // FontBBox 0 0 0 0
305 139, 12, 8, // StrokeWidth 0
306 139, 15, // charset 0
307 139, 16, // Encoding 0
308 139, 12, 31, // CIDFontVersion 0
309 139, 12, 32, // CIDFontRevision 0
310 139, 12, 33, // CIDFontType 0
311 28, 34, 16, 12, 34, // CIDCount 8720
312 30, 0x0A, 0x03, 0x96, 0x25, 0xFF, 12, 9, // BlueScale 0.039625
313 146, 12, 10, // BlueShift 7
314 140, 12, 11, // BlueFuzz 1
315 139, 12, 14, // ForceBold false
316 139, 12, 17, // LanguageGroup 0
317 30, 0x0A, 0x06, 0xFF, 12, 18, // ExpansionFactor 0.06
318 139, 12, 19, // initialRandomSeed 0
319 139, 20, // defaultWidthX 0
320 139, 21, // nominalWidthX 0
321
322 // String INDEX
323 0, 0,
324
325 // Gsubr INDEX
326 0, 0
327 };
328
329 static const Cff::Dict&
330 default_dict()
331 {
332 static Cff cff(String::make_stable((const char*) default_dict_cff_data, sizeof(default_dict_cff_data)), 0, ErrorHandler::default_handler());
333 return static_cast<Cff::Font*>(cff.font())->top_dict();
334 }
335
336
337 #define POS_GT(pos1, pos2) ((unsigned)(pos1) > (unsigned)(pos2))
338
339
340 Cff::Cff(const String& s, unsigned units_per_em, ErrorHandler* errh)
341 : _data_string(s), _data(reinterpret_cast<const uint8_t *>(_data_string.data())), _len(_data_string.length()),
342 _strings_map(-2), _units_per_em(units_per_em)
343 {
344 static_assert((sizeof(standard_strings) / sizeof(standard_strings[0])) == NSTANDARD_STRINGS,
345 "NSTANDARD_STRINGS defined incorrectly");
346 static_assert((sizeof(standard_encoding) / sizeof(standard_encoding[0])) == 256,
347 "standard_encoding has wrong size");
348 static_assert((sizeof(expert_encoding) / sizeof(expert_encoding[0])) == 256,
349 "expert_encoding has wrong size");
350 _error = parse_header(errh ? errh : ErrorHandler::silent_handler());
351 }
352
353 Cff::~Cff()
354 {
355 for (int i = 0; i < _gsubrs_cs.size(); i++)
356 delete _gsubrs_cs[i];
357 for (int i = 0; i < _fonts.size(); ++i)
358 delete _fonts[i];
359 }
360
361 /*
362 * Parsing the file header
363 */
364
365 int
366 Cff::parse_header(ErrorHandler *errh)
367 {
368 if (_gsubrs_index.error() >= 0) // already done
369 return 0;
370
371 // parse header
372 if (_len == 0)
373 return errh->error("not a PostScript-flavored OpenType font"), -EFAULT;
374 if (_len < HEADER_SIZE)
375 return errh->error("CFF file corrupted (too small)"), -EFAULT;
376 if (_data[0] != 1) // major version number
377 return errh->error("bad major version number %d", _data[0]), -ERANGE;
378 int hdrSize = _data[2], offSize = _data[3];
379 if (hdrSize < 4 || hdrSize > _len || offSize < 1 || offSize > 4)
380 return errh->error("corrupted file header"), -EINVAL;
381 int name_index_pos = hdrSize;
382
383 // parse name INDEX
384 IndexIterator niter(_data, name_index_pos, _len, errh, "Name INDEX");
385 if (niter.error() < 0)
386 return niter.error();
387 _name_index.clear();
388 for (; niter; niter++) {
389 const uint8_t *d0 = niter[0];
390 const uint8_t *d1 = niter[1];
391 if (d0 == d1 || d0[0] == 0)
392 _name_index.push_back(PermString());
393 else
394 _name_index.push_back(PermString(reinterpret_cast<const char *>(d0), d1 - d0));
395 }
396 int top_dict_index_pos = niter.index_end() - _data;
397
398 // check top DICT INDEX
399 _top_dict_index = IndexIterator(_data, top_dict_index_pos, _len, errh, "Top DICT INDEX");
400 if (_top_dict_index.error() < 0)
401 return _top_dict_index.error();
402 else if (_top_dict_index.nitems() != nfonts())
403 return errh->error("invalid font: Top DICT INDEX has %d elements, but there are %d fonts", _top_dict_index.nitems(), nfonts()), -EINVAL;
404 int string_index_pos = _top_dict_index.index_end() - _data;
405
406 // check strings INDEX
407 _strings_index = IndexIterator(_data, string_index_pos, _len, errh, "Strings INDEX");
408 if (_strings_index.error() < 0)
409 return _strings_index.error();
410 else if (NSTANDARD_STRINGS + _strings_index.nitems() - 1 > MAX_SID)
411 return errh->error("too many strings defined in font"), -EINVAL;
412 _strings.assign(_strings_index.nitems(), PermString());
413 int global_subr_index_pos = _strings_index.index_end() - _data;
414
415 // check gsubr INDEX
416 _gsubrs_index = IndexIterator(_data, global_subr_index_pos, _len, errh, "Gsubrs INDEX");
417 if (_gsubrs_index.error() < 0)
418 return _gsubrs_index.error();
419 _gsubrs_cs.assign(ngsubrs(), 0);
420
421 return 0;
422 }
423
424 int
425 Cff::sid(PermString s)
426 {
427 if (!s) // XXX?
428 return -1;
429
430 // check standard strings
431 if (standard_permstrings_map["a"] < 0)
432 for (int i = 0; i < NSTANDARD_STRINGS; i++) {
433 if (!standard_permstrings[i])
434 standard_permstrings[i] = PermString(standard_strings[i]);
435 standard_permstrings_map.insert(standard_permstrings[i], i);
436 }
437 int sid = standard_permstrings_map[s];
438 if (sid >= 0)
439 return sid;
440
441 // check user strings
442 sid = _strings_map[s];
443 if (sid >= -1)
444 return sid;
445
446 for (int i = 0; i < _strings.size(); i++)
447 if (!_strings[i] && s.length() == _strings_index[i+1] - _strings_index[i] && memcmp(s.c_str(), _strings_index[i], s.length()) == 0) {
448 _strings[i] = s;
449 _strings_map.insert(s, i + NSTANDARD_STRINGS);
450 return i + NSTANDARD_STRINGS;
451 }
452
453 _strings_map.insert(s, -1);
454 return -1;
455 }
456
457 String
458 Cff::sid_string(int sid) const
459 {
460 if (sid < 0)
461 return String();
462 else if (sid < NSTANDARD_STRINGS)
463 return String(sid_permstring(sid));
464 else {
465 sid -= NSTANDARD_STRINGS;
466 if (sid >= _strings.size())
467 return String();
468 else if (_strings[sid])
469 return String(_strings[sid]);
470 else
471 return String(reinterpret_cast<const char *>(_strings_index[sid]), _strings_index[sid + 1] - _strings_index[sid]);
472 }
473 }
474
475 PermString
476 Cff::sid_permstring(int sid) const
477 {
478 if (sid < 0)
479 return PermString();
480 else if (sid < NSTANDARD_STRINGS) {
481 if (!standard_permstrings[sid])
482 standard_permstrings[sid] = PermString(standard_strings[sid]);
483 return standard_permstrings[sid];
484 } else {
485 sid -= NSTANDARD_STRINGS;
486 if (sid >= _strings.size())
487 return PermString();
488 else if (_strings[sid])
489 return _strings[sid];
490 else {
491 PermString s = PermString(reinterpret_cast<const char *>(_strings_index[sid]), _strings_index[sid + 1] - _strings_index[sid]);
492 _strings[sid] = s;
493 _strings_map.insert(s, sid + NSTANDARD_STRINGS);
494 return s;
495 }
496 }
497 }
498
499 Cff::FontParent *
500 Cff::font(PermString font_name, ErrorHandler *errh)
501 {
502 if (!errh)
503 errh = ErrorHandler::silent_handler();
504
505 if (!ok())
506 return errh->error("invalid CFF"), (FontParent *) 0;
507
508 // search for a font named 'font_name'
509 int findex;
510 for (findex = 0; findex < _name_index.size(); ++findex) {
511 if (_name_index[findex]
512 && (!font_name || font_name == _name_index[findex]))
513 break;
514 }
515 if (findex >= _name_index.size()) {
516 if (!font_name)
517 errh->error("no fonts in CFF");
518 else
519 errh->error("font %<%s%> not found", font_name.c_str());
520 return 0;
521 }
522
523 // return font
524 for (int i = 0; i < _fonts.size(); ++i)
525 if (_fonts[i]->_font_index == findex)
526 return _fonts[i];
527
528 int td_offset = _top_dict_index[findex] - _data;
529 int td_length = _top_dict_index[findex + 1] - _top_dict_index[findex];
530 Dict top_dict(this, td_offset, td_length, errh, "Top DICT");
531 if (!top_dict.ok())
532 return 0;
533
534 Cff::FontParent* fp;
535 if (top_dict.has_first(oROS))
536 fp = new Cff::CIDFont(this, _name_index[findex], top_dict, errh);
537 else
538 fp = new Cff::Font(this, _name_index[findex], top_dict, errh);
539 fp->_font_index = findex;
540 _fonts.push_back(fp);
541 return fp;
542 }
543
544 static inline int
545 subr_bias(int charstring_type, int nsubrs)
546 {
547 if (charstring_type == 1)
548 return 0;
549 else if (nsubrs < 1240)
550 return 107;
551 else if (nsubrs < 33900)
552 return 1131;
553 else
554 return 32768;
555 }
556
557 Charstring *
558 Cff::gsubr(int i)
559 {
560 i += subr_bias(2, ngsubrs());
561 if (i < 0 || i >= ngsubrs())
562 return 0;
563 if (!_gsubrs_cs[i]) {
564 const uint8_t *s1 = _gsubrs_index[i];
565 int slen = _gsubrs_index[i + 1] - s1;
566 String cs = data_string().substring(s1 - data(), slen);
567 if (slen == 0)
568 return 0;
569 else
570 _gsubrs_cs[i] = new Type2Charstring(cs);
571 }
572 return _gsubrs_cs[i];
573 }
574
575
576 /*****
577 * Cff::Charset
578 **/
579
580 Cff::Charset::Charset(const Cff *cff, int pos, int nglyphs, int max_sid, ErrorHandler *errh)
581 {
582 assign(cff, pos, nglyphs, max_sid, errh);
583 }
584
585 void
586 Cff::Charset::assign(const Cff *cff, int pos, int nglyphs, int max_sid, ErrorHandler *errh)
587 {
588 if (!errh)
589 errh = ErrorHandler::silent_handler();
590
591 _sids.reserve(nglyphs);
592
593 if (pos == 0)
594 assign(iso_adobe_charset, sizeof(iso_adobe_charset) / sizeof(int), nglyphs);
595 else if (pos == 1)
596 assign(expert_charset, sizeof(expert_charset) / sizeof(int), nglyphs);
597 else if (pos == 2)
598 assign(expert_subset_charset, sizeof(expert_subset_charset) / sizeof(int), nglyphs);
599 else
600 _error = parse(cff, pos, nglyphs, max_sid, errh);
601
602 if (_error >= 0)
603 for (int g = 0; g < _sids.size(); g++) {
604 if (_gids[_sids[g]] >= 0) {
605 errh->error("glyph %<%s%> in charset twice", cff->sid_permstring(_sids[g]).c_str());
606 _error = -EEXIST;
607 }
608 _gids[_sids[g]] = g;
609 }
610 }
611
612 void
613 Cff::Charset::assign(const int *data, int size, int nglyphs)
614 {
615 if (size < nglyphs)
616 size = nglyphs;
617 _sids.resize(size);
618 memcpy(&_sids[0], data, sizeof(const int) * size);
619 _gids.resize(data[size-1] + 1, -1);
620 _error = 0;
621 }
622
623 int
624 Cff::Charset::parse(const Cff *cff, int pos, int nglyphs, int max_sid, ErrorHandler *errh)
625 {
626 const uint8_t *data = cff->data();
627 int len = cff->length();
628
629 if (pos + 1 > len)
630 return errh->error("charset position out of range"), -EFAULT;
631
632 _sids.push_back(0);
633 int actual_max_sid = 0;
634
635 int format = data[pos];
636 if (format == 0) {
637 if (pos + 1 + (nglyphs - 1) * 2 > len)
638 return errh->error("charset [format 0] out of range"), -EFAULT;
639 const uint8_t *p = data + pos + 1;
640 for (; _sids.size() < nglyphs; p += 2) {
641 int sid = (p[0] << 8) | p[1];
642 if (sid > actual_max_sid)
643 actual_max_sid = sid;
644 _sids.push_back(sid);
645 }
646
647 } else if (format == 1) {
648 const uint8_t *p = data + pos + 1;
649 for (; _sids.size() < nglyphs; p += 3) {
650 if (p + 3 > data + len)
651 return errh->error("charset [format 1] out of range"), -EFAULT;
652 int sid = (p[0] << 8) | p[1];
653 int n = p[2];
654 if (sid + n > actual_max_sid)
655 actual_max_sid = sid + n;
656 for (int i = 0; i <= n; i++)
657 _sids.push_back(sid + i);
658 }
659
660 } else if (format == 2) {
661 const uint8_t *p = data + pos + 1;
662 for (; _sids.size() < nglyphs; p += 4) {
663 if (p + 4 > data + len)
664 return errh->error("charset [format 2] out of range"), -EFAULT;
665 int sid = (p[0] << 8) | p[1];
666 int n = (p[2] << 8) | p[3];
667 if (sid + n > actual_max_sid)
668 actual_max_sid = sid + n;
669 for (int i = 0; i <= n; i++)
670 _sids.push_back(sid + i);
671 }
672
673 } else
674 return errh->error("unknown charset format %d", format), -EINVAL;
675
676 if (max_sid >= 0 && actual_max_sid > max_sid)
677 return errh->error("charset [format %d] uses bad SID %d", format, actual_max_sid), -EINVAL;
678 _sids.resize(nglyphs);
679 _gids.resize(actual_max_sid + 1, -1);
680 return 0;
681 }
682
683
684 /*****
685 * Cff::FDSelect
686 **/
687
688 void
689 Cff::FDSelect::assign(const Cff *cff, int pos, int nglyphs, ErrorHandler *errh)
690 {
691 if (!errh)
692 errh = ErrorHandler::silent_handler();
693 if (_my_fds)
694 delete[] _fds;
695 _fds = 0;
696 _my_fds = false;
697 _nglyphs = nglyphs;
698 _error = parse(cff, pos, nglyphs, errh);
699 }
700
701 Cff::FDSelect::~FDSelect()
702 {
703 if (_my_fds)
704 delete[] _fds;
705 }
706
707 int
708 Cff::FDSelect::parse(const Cff *cff, int pos, int nglyphs, ErrorHandler *errh)
709 {
710 const uint8_t *data = cff->data();
711 int len = cff->length();
712
713 if (pos + 1 > len)
714 return errh->error("FDSelect position out of range"), -EFAULT;
715
716 int format = data[pos];
717 if (format == 0) {
718 if (pos + 1 + nglyphs > len)
719 return errh->error("FDSelect [format 0] out of range"), -EFAULT;
720 _fds = data + pos + 1;
721 _my_fds = false;
722 return 0;
723
724 } else if (format == 3) {
725 int nranges = (data[pos+1] << 8) | data[pos+2];
726 if (pos + 3 + 3*nranges + 2 > len)
727 return errh->error("FDSelect [format 3] out of range"), -EFAULT;
728
729 const uint8_t *p = data + pos + 3;
730 int last_glyph = (p[3*nranges] << 8) | p[3*nranges + 1];
731 if (p[0] || p[1] || last_glyph != nglyphs)
732 return errh->error("FDSelect [format 3] bad values"), -EINVAL;
733
734 _fds = new uint8_t[nglyphs];
735 _my_fds = true;
736 int curglyph = 0;
737 for (; curglyph < nglyphs; p += 3) {
738 int nextglyph = (p[3] << 8) | p[4];
739 if (nextglyph > nglyphs || nextglyph < curglyph)
740 return errh->error("FDSelect [format 3] sorting error"), -EINVAL;
741 memset(const_cast<uint8_t *>(_fds + curglyph), p[2], nextglyph - curglyph);
742 curglyph = nextglyph;
743 }
744 return 0;
745
746 } else
747 return errh->error("unknown charset format %d", format), -EINVAL;
748 }
749
750
751 /*****
752 * Cff::IndexIterator
753 **/
754
755 Cff::IndexIterator::IndexIterator(const uint8_t *data, int pos, int len, ErrorHandler *errh, const char *index_name)
756 : _contents(0), _offset(0), _last_offset(0)
757 {
758 if (!errh)
759 errh = ErrorHandler::silent_handler();
760
761 // check header
762 int nitems = 0;
763 if (POS_GT(pos + 2, len)) {
764 errh->error("%s: position out of range", index_name);
765 _offsize = -EFAULT;
766 } else if (data[pos] == 0 && data[pos + 1] == 0) {
767 _contents = data + pos + 2;
768 _offsize = 0;
769 } else if (POS_GT(pos + 3, len)) {
770 errh->error("%s: position out of range", index_name);
771 _offsize = -EFAULT;
772 } else if ((_offsize = data[pos + 2]), (_offsize < 1 || _offsize > 4)) {
773 errh->error("%s: offset size %d out of range", index_name, _offsize);
774 _offsize = -EINVAL;
775 } else {
776 nitems = (data[pos] << 8) | data[pos + 1];
777 if (POS_GT(pos + 3 + (nitems + 1) * _offsize, len)) {
778 errh->error("%s: data out of range", index_name);
779 _offsize = -EFAULT;
780 } else {
781 _offset = data + pos + 3;
782 _last_offset = _offset + nitems * _offsize;
783 _contents = _last_offset + _offsize - 1;
784 }
785 }
786
787 // check items in offset array
788 uint32_t max_doff_allowed = len - (pos + 2 + (nitems + 1) * _offsize);
789 uint32_t last_doff = 1;
790 for (const uint8_t *o = _offset; o <= _last_offset && _offsize > 0; o += _offsize) {
791 uint32_t doff = offset_at(o);
792 if (doff > max_doff_allowed) {
793 errh->error("%s: element out of range", index_name);
794 _offsize = -EFAULT;
795 } else if (doff < last_doff) {
796 errh->error("%s: garbled elements", index_name);
797 break;
798 }
799 last_doff = doff;
800 }
801 }
802
803 const uint8_t *
804 Cff::IndexIterator::index_end() const
805 {
806 if (_offsize <= 0)
807 return _contents;
808 else
809 return _contents + offset_at(_last_offset);
810 }
811
812 int
813 Cff::IndexIterator::nitems() const
814 {
815 if (_offsize <= 0)
816 return 0;
817 else
818 return (_last_offset - _offset) / _offsize;
819 }
820
821
822
823 /*****
824 * Cff::Dict
825 **/
826
827 Cff::Dict::Dict()
828 : _cff(0), _pos(0), _error(-ENOENT)
829 {
830 }
831
832 Cff::Dict::Dict(Cff *cff, int pos, int dict_len, ErrorHandler *errh, const char *dict_name)
833 {
834 assign(cff, pos, dict_len, errh, dict_name);
835 }
836
837 int
838 Cff::Dict::assign(Cff *cff, int pos, int dict_len, ErrorHandler *errh, const char *dict_name)
839 {
840 _cff = cff;
841 _pos = pos;
842 _operators.clear();
843 _pointers.clear();
844 _operands.clear();
845
846 if (!errh)
847 errh = ErrorHandler::silent_handler();
848
849 const uint8_t *data = cff->data() + pos;
850 const uint8_t *end_data = data + dict_len;
851
852 _pointers.push_back(0);
853 while (data < end_data)
854 switch (data[0]) {
855
856 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
857 case 8: case 9: case 10: case 11: case 13: case 14: case 15:
858 case 16: case 17: case 18: case 19: case 20: case 21:
859 _operators.push_back(data[0]);
860 _pointers.push_back(_operands.size());
861 data++;
862 break;
863
864 case 22: case 23: case 24: case 25: case 26: case 27: case 31:
865 case 255: // reserved
866 errh->error("%s: reserved operator %d", dict_name, data[0]);
867 return (_error = -ERANGE);
868
869 case 12:
870 if (data + 1 >= end_data)
871 goto runoff;
872 _operators.push_back(32 + data[1]);
873 _pointers.push_back(_operands.size());
874 data += 2;
875 break;
876
877 case 28: {
878 if (data + 2 >= end_data)
879 goto runoff;
880 int16_t val = (data[1] << 8) | data[2];
881 _operands.push_back(val);
882 data += 3;
883 break;
884 }
885
886 case 29: {
887 if (data + 4 >= end_data)
888 goto runoff;
889 int32_t val = (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
890 _operands.push_back(val);
891 data += 5;
892 break;
893 }
894
895 case 30: {
896 char buf[1024];
897 int pos = 0;
898 if (data + 1 >= end_data)
899 goto runoff;
900 for (data++; data < end_data && pos < 1020; data++) {
901 int d = *data;
902 for (int i = 0; i < 2; i++, d <<= 4) {
903 int digit = (d >> 4) & 0xF;
904 switch (digit) {
905 case 10:
906 buf[pos++] = '.';
907 break;
908 case 11:
909 buf[pos++] = 'E';
910 break;
911 case 12:
912 buf[pos++] = 'E';
913 buf[pos++] = '-';
914 break;
915 case 13:
916 errh->error("%s: bad digit in real number", dict_name);
917 goto invalid;
918 case 14:
919 buf[pos++] = '-';
920 break;
921 case 15:
922 goto found;
923 default:
924 buf[pos++] = digit + '0';
925 break;
926 }
927 }
928 }
929 // number not found
930 goto runoff;
931 found:
932 char *endptr;
933 buf[pos] = '\0';
934 _operands.push_back(strtod(buf, &endptr));
935 if (*endptr) {
936 errh->error("%s: real number syntax error", dict_name);
937 goto invalid;
938 }
939 data++;
940 break;
941 }
942
943 case 247: case 248: case 249: case 250: {
944 if (data + 1 >= end_data)
945 goto runoff;
946 int val = ((data[0] - 247) << 8) + data[1] + 108;
947 _operands.push_back(val);
948 data += 2;
949 break;
950 }
951
952 case 251: case 252: case 253: case 254: {
953 if (data + 1 >= end_data)
954 goto runoff;
955 int val = -((data[0] - 251) << 8) - data[1] - 108;
956 _operands.push_back(val);
957 data += 2;
958 break;
959 }
960
961 default:
962 _operands.push_back(data[0] - 139);
963 data++;
964 break;
965
966 }
967
968 // not closed by an operator?
969 if (_pointers.back() != _operands.size()) {
970 errh->error("%s: not closed by an operator", dict_name);
971 goto invalid;
972 }
973
974 return (_error = 0);
975
976 runoff:
977 errh->error("%s: runoff end of DICT", dict_name);
978 return (_error = -EFAULT);
979
980 invalid:
981 return (_error = -EINVAL);
982 }
983
984 int
985 Cff::Dict::check(bool is_private, ErrorHandler *errh, const char *dict_name) const
986 {
987 if (!errh)
988 errh = ErrorHandler::silent_handler();
989 int before_nerrors = errh->nerrors();
990
991 // keep track of operator reuse
992 Vector<int> operators_used;
993
994 for (int i = 0; i < _operators.size(); i++) {
995 int arity = _pointers[i+1] - _pointers[i];
996 double num = (arity == 0 ? 0 : _operands[_pointers[i]]);
997 double truncnum = floor(num);
998 int op = _operators[i];
999 int type = (op > oLastOperator ? tNone : operator_types[op]);
1000
1001 // check reuse
1002 if (op >= operators_used.size())
1003 operators_used.resize(op + 1, 0);
1004 if (operators_used[op] && (type & tTypeMask) != tNone)
1005 errh->error("%s: operator %<%s%> specified twice", dict_name, operator_names[op]);
1006 operators_used[op]++;
1007
1008 // check data
1009 switch (type & tTypeMask) {
1010
1011 case tNone:
1012 if (op >= 32)
1013 errh->warning("%s: unknown operator %<12 %d%>", dict_name, op - 32);
1014 else
1015 errh->warning("%s: unknown operator %<%d%>", dict_name, op);
1016 continue;
1017
1018 case tSID:
1019 if (arity != 1 || num != truncnum || num < 0 || num > _cff->max_sid())
1020 goto bad_data;
1021 break;
1022
1023 case tFontNumber:
1024 if (arity != 1 || num != truncnum || num < 0 || num >= _cff->nfonts())
1025 goto bad_data;
1026 break;
1027
1028 case tBoolean:
1029 if (arity != 1)
1030 goto bad_data;
1031 else if (num != 0 && num != 1)
1032 errh->warning("%s: data for Boolean operator %<%s%> not 0 or 1", dict_name, operator_names[op]);
1033 break;
1034
1035 case tNumber:
1036 if (arity != 1)
1037 goto bad_data;
1038 break;
1039
1040 case tOffset:
1041 if (arity != 1 || num != truncnum || num < 0 || num >= _cff->length())
1042 goto bad_data;
1043 break;
1044
1045 case tLocalOffset:
1046 if (arity != 1 || num != truncnum || _pos + num < 0 || _pos + num >= _cff->length())
1047 goto bad_data;
1048 break;
1049
1050 case tPrivateType: {
1051 if (arity != 2 || num != truncnum || num < 0)
1052 goto bad_data;
1053 double off = _operands[_pointers[i] + 1];
1054 if (off < 0 || off + num > _cff->length())
1055 goto bad_data;
1056 break;
1057 }
1058
1059 case tArray2: case tArray3: case tArray4:
1060 case tArray5: case tArray6:
1061 if (arity != (type & tTypeMask) - tArray2 + 2)
1062 goto bad_data;
1063 break;
1064
1065 case tArray:
1066 break;
1067
1068 }
1069
1070 // check dict location
1071 if (((type & tPrivate) != 0) != is_private)
1072 errh->warning("%s: operator %<%s%> in wrong DICT", dict_name, operator_names[op]);
1073
1074 continue;
1075
1076 bad_data:
1077 errh->error("%s: bad data for operator %<%s%>", dict_name, operator_names[op]);
1078 }
1079
1080 return (errh->nerrors() != before_nerrors ? -1 : 0);
1081 }
1082
1083 bool
1084 Cff::Dict::has(DictOperator op) const
1085 {
1086 for (int i = 0; i < _operators.size(); i++)
1087 if (_operators[i] == op)
1088 return true;
1089 return false;
1090 }
1091
1092 bool
1093 Cff::Dict::xvalue(DictOperator op, Vector<double> &out) const
1094 {
1095 out.clear();
1096 for (int i = 0; i < _operators.size(); i++)
1097 if (_operators[i] == op) {
1098 for (int j = _pointers[i]; j < _pointers[i+1]; j++)
1099 out.push_back(_operands[j]);
1100 return true;
1101 }
1102 return false;
1103 }
1104
1105 bool
1106 Cff::Dict::xvalue(DictOperator op, int *val) const
1107 {
1108 for (int i = 0; i < _operators.size(); i++)
1109 if (_operators[i] == op && _pointers[i] + 1 == _pointers[i+1]) {
1110 *val = (int) _operands[_pointers[i]];
1111 return true;
1112 }
1113 return false;
1114 }
1115
1116 bool
1117 Cff::Dict::xvalue(DictOperator op, double *val) const
1118 {
1119 for (int i = 0; i < _operators.size(); i++)
1120 if (_operators[i] == op && _pointers[i] + 1 == _pointers[i+1]) {
1121 *val = _operands[_pointers[i]];
1122 return true;
1123 }
1124 return false;
1125 }
1126
1127 bool
1128 Cff::Dict::value(DictOperator op, Vector<double> &out) const
1129 {
1130 return xvalue(op, out) || default_dict().xvalue(op, out);
1131 }
1132
1133 bool
1134 Cff::Dict::value(DictOperator op, int *val) const
1135 {
1136 return xvalue(op, val) || default_dict().xvalue(op, val);
1137 }
1138
1139 bool
1140 Cff::Dict::value(DictOperator op, double *val) const
1141 {
1142 return xvalue(op, val) || default_dict().xvalue(op, val);
1143 }
1144
1145 void
1146 Cff::Dict::unparse(ErrorHandler *errh, const char *dict_name) const
1147 {
1148 StringAccum sa;
1149 for (int i = 0; i < _operators.size(); i++) {
1150 sa.clear();
1151 if (_pointers[i] + 1 == _pointers[i+1])
1152 sa << _operands[_pointers[i]];
1153 else {
1154 sa << "[";
1155 for (int j = _pointers[i]; j < _pointers[i+1]; j++)
1156 sa << _operands[j] << ' ';
1157 sa.pop_back();
1158 sa << "]";
1159 }
1160 errh->message("%s: %s %s", dict_name, operator_names[_operators[i]], sa.c_str());
1161 }
1162 }
1163
1164
1165 /*****
1166 * CffFontParent
1167 **/
1168
1169 static int
1170 handle_private(Cff *cff, const Cff::Dict &top_dict, Cff::Dict &private_dict,
1171 double &default_width_x, double &nominal_width_x,
1172 Cff::IndexIterator &subrs_index, Vector<Charstring *> &subrs_cs,
1173 ErrorHandler *errh)
1174 {
1175 Vector<double> private_info;
1176 top_dict.value(Cff::oPrivate, private_info);
1177 int private_offset = (int) private_info[1];
1178 private_dict.assign(cff, private_offset, (int) private_info[0], errh, "Private DICT");
1179 if (private_dict.error() < 0)
1180 return private_dict.error();
1181 else if (private_dict.check(true, errh, "Private DICT") < 0)
1182 return -EINVAL;
1183 //private_dict.unparse(errh, "Private DICT");
1184
1185 private_dict.value(Cff::oDefaultWidthX, &default_width_x);
1186 private_dict.value(Cff::oNominalWidthX, &nominal_width_x);
1187 if (private_dict.has(Cff::oSubrs)) {
1188 int subrs_offset = 0;
1189 private_dict.value(Cff::oSubrs, &subrs_offset);
1190 subrs_index = Cff::IndexIterator(cff->data(), private_offset + subrs_offset, cff->length(), errh, "Subrs INDEX");
1191 if (subrs_index.error() < 0)
1192 return subrs_index.error();
1193 }
1194 subrs_cs.assign(subrs_index.nitems(), 0);
1195 return 0;
1196 }
1197
1198
1199 Cff::FontParent::FontParent(Cff* cff)
1200 : CharstringProgram(cff->units_per_em()), _cff(cff), _error(-1)
1201 {
1202 }
1203
1204 Charstring *
1205 Cff::FontParent::charstring(const IndexIterator &iiter, int which) const
1206 {
1207 const uint8_t *s1 = iiter[which];
1208 int slen = iiter[which + 1] - s1;
1209 String cs = _cff->data_string().substring(s1 - _cff->data(), slen);
1210 if (slen == 0)
1211 return 0;
1212 else if (_charstring_type == 1)
1213 return new Type1Charstring(cs);
1214 else
1215 return new Type2Charstring(cs);
1216 }
1217
1218 Charstring *
1219 Cff::FontParent::gsubr(int i) const
1220 {
1221 return _cff->gsubr(i);
1222 }
1223
1224 int
1225 Cff::FontParent::gsubr_bias() const
1226 {
1227 return Efont::subr_bias(2, ngsubrs_x());
1228 }
1229
1230
1231 /*****
1232 * CffFont
1233 **/
1234
1235 Cff::Font::Font(Cff *cff, PermString font_name, const Dict &top_dict, ErrorHandler *errh)
1236 : ChildFont(cff, 0, 2, top_dict, errh), _font_name(font_name),
1237 _t1encoding(0)
1238 {
1239 assert(!_top_dict.has_first(oROS));
1240 if (_error < 0)
1241 return;
1242
1243 // extract CharStrings
1244 // must use xvalue because we could be creating the default dict!
1245 int charstrings_offset = 0;
1246 _top_dict.xvalue(oCharStrings, &charstrings_offset);
1247 _charstrings_index = Cff::IndexIterator(cff->data(), charstrings_offset, cff->length(), errh, "CharStrings INDEX");
1248 if (_charstrings_index.error() < 0) {
1249 _error = _charstrings_index.error();
1250 return;
1251 }
1252 _charstrings_cs.assign(_charstrings_index.nitems(), 0);
1253
1254 int charset = 0;
1255 _top_dict.xvalue(oCharset, &charset);
1256 _charset.assign(cff, charset, _charstrings_index.nitems(), cff->max_sid(), errh);
1257 if (_charset.error() < 0) {
1258 _error = _charset.error();
1259 return;
1260 }
1261
1262 int Encoding = 0;
1263 _top_dict.xvalue(oEncoding, &Encoding);
1264 if (parse_encoding(Encoding, errh) < 0)
1265 return;
1266
1267 // success!
1268 _error = 0;
1269 }
1270
1271 Cff::Font::~Font()
1272 {
1273 for (int i = 0; i < _charstrings_cs.size(); i++)
1274 delete _charstrings_cs[i];
1275 delete _t1encoding;
1276 }
1277
1278 int
1279 Cff::Font::parse_encoding(int pos, ErrorHandler *errh)
1280 {
1281 _encoding_pos = pos;
1282 for (int i = 0; i < 256; i++)
1283 _encoding[i] = 0;
1284
1285 // check for standard encodings
1286 if (pos == 0)
1287 return assign_standard_encoding(standard_encoding);
1288 else if (pos == 1)
1289 return assign_standard_encoding(expert_encoding);
1290
1291 // otherwise, a custom encoding
1292 const uint8_t *data = _cff->data();
1293 int len = _cff->length();
1294 if (pos + 1 > len)
1295 return errh->error("Encoding position out of range"), -EFAULT;
1296 bool supplemented = (data[pos] & 0x80) != 0;
1297 int format = (data[pos] & 0x7F);
1298
1299 int retval = 0;
1300 int endpos, g = 1;
1301 if (format == 0) {
1302 endpos = pos + 2 + data[pos + 1];
1303 if (endpos > len)
1304 return errh->error("Encoding[0] out of range"), -EFAULT;
1305 const uint8_t *p = data + pos + 2;
1306 int n = data[pos + 1];
1307 for (; g <= n; g++, p++) {
1308 int e = p[0];
1309 if (_encoding[e])
1310 retval = 1;
1311 _encoding[e] = g;
1312 }
1313
1314 } else if (format == 1) {
1315 endpos = pos + 2 + data[pos + 1] * 2;
1316 if (endpos > len)
1317 return errh->error("Encoding[1] out of range"), -EFAULT;
1318 const uint8_t *p = data + pos + 2;
1319 int n = data[pos + 1];
1320 for (int i = 0; i < n; i++, p += 2) {
1321 int first = p[0];
1322 int nLeft = p[1];
1323 if (first + nLeft > 255)
1324 return errh->error("Encoding[1] has out-of-range component"), -EINVAL;
1325 for (int e = first; e <= first + nLeft; e++) {
1326 if (_encoding[e])
1327 retval = 1;
1328 _encoding[e] = g++;
1329 }
1330 }
1331
1332 } else {
1333 return errh->error("unknown Encoding format %d", format), -EINVAL;
1334 }
1335
1336 if (g > _charset.nglyphs()) {
1337 return errh->error("Encoding glyph %d out of range", g), -EINVAL;
1338 }
1339
1340 // check supplements
1341 if (supplemented) {
1342 if (endpos + data[endpos] * 3 > len)
1343 return -EINVAL;
1344 const uint8_t *p = data + endpos + 1;
1345 int n = data[endpos];
1346 for (int i = 0; i < n; i++, p += 3) {
1347 int e = p[0];
1348 int s = (p[1] << 8) | p[2];
1349 int g = _charset.sid_to_gid(s);
1350 if (_encoding[e])
1351 retval = 1;
1352 if (g < 0 || g >= _charset.nglyphs())
1353 return errh->error("Encoding glyph %d out of range", g), -EINVAL;
1354 _encoding[e] = g;
1355 }
1356 }
1357
1358 // successfully done
1359 return retval;
1360 }
1361
1362 int
1363 Cff::Font::assign_standard_encoding(const int *standard_encoding)
1364 {
1365 for (int i = 0; i < 256; i++)
1366 _encoding[i] = _charset.sid_to_gid(standard_encoding[i]);
1367 return 0;
1368 }
1369
1370 void
1371 Cff::Font::font_matrix(double matrix[6]) const
1372 {
1373 Vector<double> t1d_matrix;
1374 if (dict_value(oFontMatrix, t1d_matrix) && t1d_matrix.size() == 6)
1375 memcpy(&matrix[0], &t1d_matrix[0], sizeof(double) * 6);
1376 else {
1377 matrix[0] = matrix[3] = 0.001;
1378 matrix[1] = matrix[2] = matrix[4] = matrix[5] = 0;
1379 }
1380 }
1381
1382 PermString
1383 Cff::Font::glyph_name(int gid) const
1384 {
1385 if (gid >= 0 && gid < nglyphs())
1386 return _cff->sid_permstring(_charset.gid_to_sid(gid));
1387 else
1388 return PermString();
1389 }
1390
1391 void
1392 Cff::Font::glyph_names(Vector<PermString> &gnames) const
1393 {
1394 gnames.resize(nglyphs());
1395 for (int i = 0; i < nglyphs(); i++)
1396 gnames[i] = _cff->sid_permstring(_charset.gid_to_sid(i));
1397 }
1398
1399 Charstring *
1400 Cff::Font::glyph(int gid) const
1401 {
1402 if (gid < 0 || gid >= nglyphs())
1403 return 0;
1404 if (!_charstrings_cs[gid])
1405 _charstrings_cs[gid] = charstring(_charstrings_index, gid);
1406 return _charstrings_cs[gid];
1407 }
1408
1409 Charstring *
1410 Cff::Font::glyph(PermString name) const
1411 {
1412 int gid = _charset.sid_to_gid(_cff->sid(name));
1413 if (gid < 0)
1414 return 0;
1415 if (!_charstrings_cs[gid])
1416 _charstrings_cs[gid] = charstring(_charstrings_index, gid);
1417 return _charstrings_cs[gid];
1418 }
1419
1420 int
1421 Cff::Font::glyphid(PermString name) const
1422 {
1423 return _charset.sid_to_gid(_cff->sid(name));
1424 }
1425
1426 Type1Encoding *
1427 Cff::Font::type1_encoding() const
1428 {
1429 if (_encoding_pos == 0)
1430 return Type1Encoding::standard_encoding();
1431 if (!_t1encoding)
1432 _t1encoding = type1_encoding_copy();
1433 return _t1encoding;
1434 }
1435
1436 Type1Encoding *
1437 Cff::Font::type1_encoding_copy() const
1438 {
1439 if (_encoding_pos == 0)
1440 return Type1Encoding::standard_encoding();
1441 Type1Encoding *e = new Type1Encoding;
1442 for (int i = 0; i < 256; i++)
1443 if (_encoding[i])
1444 e->put(i, _cff->sid_permstring(_charset.gid_to_sid(_encoding[i])));
1445 return e;
1446 }
1447
1448 bool
1449 Cff::Font::dict_has(DictOperator op) const
1450 {
1451 return dict_of(op).has(op);
1452 }
1453
1454 String
1455 Cff::Font::dict_string(DictOperator op) const
1456 {
1457 Vector<double> vec;
1458 dict_of(op).value(op, vec);
1459 if (vec.size() == 1 && vec[0] >= 0 && vec[0] <= _cff->max_sid())
1460 return _cff->sid_string((int) vec[0]);
1461 else
1462 return String();
1463 }
1464
1465
1466 /*****
1467 * Cff::CIDFont
1468 **/
1469
1470 Cff::CIDFont::CIDFont(Cff *cff, PermString font_name, const Dict &top_dict, ErrorHandler *errh)
1471 : FontParent(cff), _font_name(font_name), _top_dict(top_dict)
1472 {
1473 assert(_top_dict.has_first(oROS));
1474
1475 // parse top DICT
1476 _error = -EINVAL;
1477 if (_top_dict.check(false, errh, "Top DICT") < 0)
1478 return;
1479 else if (!_top_dict.has(oCharStrings)) {
1480 errh->error("font has no CharStrings dictionary");
1481 return;
1482 }
1483 //_top_dict.unparse(errh, "Top DICT");
1484
1485 // extract offsets and information from TOP DICT
1486 _top_dict.value(oCharstringType, &_charstring_type);
1487 if (_charstring_type != 1 && _charstring_type != 2) {
1488 errh->error("unknown CharString type %d", _charstring_type);
1489 return;
1490 }
1491
1492 int charstrings_offset = 0;
1493 _top_dict.value(oCharStrings, &charstrings_offset);
1494 _charstrings_index = Cff::IndexIterator(cff->data(), charstrings_offset, cff->length(), errh, "CharStrings INDEX");
1495 if (_charstrings_index.error() < 0) {
1496 _error = _charstrings_index.error();
1497 return;
1498 }
1499 _charstrings_cs.assign(_charstrings_index.nitems(), 0);
1500
1501 int charset = 0;
1502 _top_dict.value(oCharset, &charset);
1503 _charset.assign(cff, charset, _charstrings_index.nitems(), -1, errh);
1504 if (_charset.error() < 0) {
1505 _error = _charset.error();
1506 return;
1507 }
1508
1509 // extract information about child fonts
1510 int fdarray_offset = 0;
1511 if (!_top_dict.value(oFDArray, &fdarray_offset)) {
1512 errh->error("CID-keyed font missing FDArray");
1513 return;
1514 }
1515 IndexIterator fdarray_index(cff->data(), fdarray_offset, cff->length(), errh, "FDArray INDEX");
1516 for (; fdarray_index; fdarray_index++) {
1517 Dict d(cff, fdarray_index[0] - cff->data(), fdarray_index[1] - fdarray_index[0], errh, "Top DICT");
1518 if (!d.ok() || d.check(false, errh, "Top DICT") < 0) {
1519 _error = d.error();
1520 return;
1521 }
1522 _child_fonts.push_back(new ChildFont(cff, this, _charstring_type, d, errh));
1523 if (!_child_fonts.back()->ok())
1524 return;
1525 }
1526
1527 int fdselect_offset = 0;
1528 if (!_top_dict.value(oFDSelect, &fdselect_offset)) {
1529 errh->error("CID-keyed font missing FDSelect");
1530 return;
1531 }
1532 _fdselect.assign(cff, fdselect_offset, _charstrings_cs.size(), errh);
1533 if (_fdselect.error() < 0)
1534 return;
1535
1536 // success!
1537 _error = 0;
1538 set_parent_program(true);
1539 }
1540
1541 Cff::CIDFont::~CIDFont()
1542 {
1543 for (int i = 0; i < _charstrings_cs.size(); i++)
1544 delete _charstrings_cs[i];
1545 for (int i = 0; i < _child_fonts.size(); i++)
1546 delete _child_fonts[i];
1547 }
1548
1549 void
1550 Cff::CIDFont::font_matrix(double matrix[6]) const
1551 {
1552 // XXX
1553 matrix[0] = matrix[3] = 0.001;
1554 matrix[1] = matrix[2] = matrix[4] = matrix[5] = 0;
1555 }
1556
1557 const CharstringProgram *
1558 Cff::CIDFont::child_program(int gid) const
1559 {
1560 int fd = _fdselect.gid_to_fd(gid);
1561 if (fd >= 0 && fd < _child_fonts.size())
1562 return _child_fonts.at_u(fd);
1563 else
1564 return 0;
1565 }
1566
1567 PermString
1568 Cff::CIDFont::glyph_name(int gid) const
1569 {
1570 if (gid >= 0 && gid < nglyphs())
1571 return permprintf("#%d", _charset.gid_to_sid(gid));
1572 else
1573 return PermString();
1574 }
1575
1576 void
1577 Cff::CIDFont::glyph_names(Vector<PermString> &gnames) const
1578 {
1579 gnames.resize(nglyphs());
1580 for (int i = 0; i < nglyphs(); i++)
1581 gnames[i] = permprintf("#%d", _charset.gid_to_sid(i));
1582 }
1583
1584 Charstring *
1585 Cff::CIDFont::glyph(int gid) const
1586 {
1587 if (gid < 0 || gid >= nglyphs())
1588 return 0;
1589 if (!_charstrings_cs[gid])
1590 _charstrings_cs[gid] = charstring(_charstrings_index, gid);
1591 return _charstrings_cs[gid];
1592 }
1593
1594 int
1595 Cff::CIDFont::glyphid(PermString name) const
1596 {
1597 if (name.length() <= 1 || name[0] != '#' || !isdigit((unsigned char) name[1]))
1598 return -1;
1599 char *endptr;
1600 long cid = strtol(name.c_str() + 1, &endptr, 10);
1601 if (*endptr != 0)
1602 return -1;
1603 return _charset.sid_to_gid(cid);
1604 }
1605
1606 Charstring *
1607 Cff::CIDFont::glyph(PermString name) const
1608 {
1609 return CIDFont::glyph(CIDFont::glyphid(name));
1610 }
1611
1612
1613 /*****
1614 * ChildFont
1615 **/
1616
1617 Cff::ChildFont::ChildFont(Cff *cff, Cff::CIDFont *parent, int charstring_type, const Dict &top_dict, ErrorHandler *errh)
1618 : FontParent(cff), _parent(parent), _top_dict(top_dict)
1619 {
1620 if (!errh)
1621 errh = ErrorHandler::silent_handler();
1622
1623 if (!cff->ok() || !_top_dict.ok()) {
1624 errh->error("invalid CFF");
1625 _error = -EINVAL;
1626 return;
1627 }
1628
1629 // extract offsets and information from TOP DICT
1630 _charstring_type = charstring_type;
1631 _top_dict.value(oCharstringType, &_charstring_type);
1632 if (_charstring_type != 1 && _charstring_type != 2) {
1633 errh->error("unknown CharString type %d", _charstring_type);
1634 return;
1635 }
1636
1637 // extract information from Private DICT
1638 if (_top_dict.has(oPrivate)
1639 && (_error = handle_private(cff, _top_dict, _private_dict, _default_width_x, _nominal_width_x, _subrs_index, _subrs_cs, errh)) < 0)
1640 return;
1641
1642 // success!
1643 _error = 0;
1644 }
1645
1646 Cff::ChildFont::~ChildFont()
1647 {
1648 for (int i = 0; i < _subrs_cs.size(); i++)
1649 delete _subrs_cs[i];
1650 }
1651
1652 Charstring *
1653 Cff::ChildFont::charstring(const IndexIterator &iiter, int which) const
1654 {
1655 const uint8_t *s1 = iiter[which];
1656 int slen = iiter[which + 1] - s1;
1657 String cs = _cff->data_string().substring(s1 - _cff->data(), slen);
1658 if (slen == 0)
1659 return 0;
1660 else if (_charstring_type == 1)
1661 return new Type1Charstring(cs);
1662 else
1663 return new Type2Charstring(cs);
1664 }
1665
1666 Charstring *
1667 Cff::ChildFont::subr(int i) const
1668 {
1669 i += Efont::subr_bias(_charstring_type, nsubrs_x());
1670 if (i < 0 || i >= nsubrs_x())
1671 return 0;
1672 if (!_subrs_cs[i])
1673 _subrs_cs[i] = charstring(_subrs_index, i);
1674 return _subrs_cs[i];
1675 }
1676
1677 int
1678 Cff::ChildFont::subr_bias() const
1679 {
1680 return Efont::subr_bias(_charstring_type, nsubrs_x());
1681 }
1682
1683 double
1684 Cff::ChildFont::global_width_x(bool is_nominal) const
1685 {
1686 return (is_nominal ? _nominal_width_x : _default_width_x);
1687 }
1688
1689 }