| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2006-2010 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
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 |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
#include "lua/luatex-api.h" |
| 24 |
#include "font/writecff.h" |
| 25 |
|
| 26 |
extern int cidset; |
| 27 |
|
| 28 |
#define get_offset(s,n) get_unsigned(s, (n)) |
| 29 |
#define get_card8(a) (card8)(a->stream[a->offset++]) |
| 30 |
#define get_card16(a) (card16)(get_unsigned(a,2)) |
| 31 |
#define get_card32(a) (get_unsigned(a,4)) |
| 32 |
|
| 33 |
#undef b0 |
| 34 |
#undef b1 |
| 35 |
#undef b2 |
| 36 |
#undef b3 |
| 37 |
|
| 38 |
#define WORK_BUFFER_SIZE 1024 |
| 39 |
|
| 40 |
static char work_buffer[WORK_BUFFER_SIZE]; |
| 41 |
|
| 42 |
static unsigned long get_unsigned(cff_font * cff, int n) |
| 43 |
{ |
| 44 |
unsigned long v = 0; |
| 45 |
while (n-- > 0) |
| 46 |
v = v * 256 + get_card8(cff); |
| 47 |
return v; |
| 48 |
} |
| 49 |
|
| 50 |
const char *const cff_stdstr[CFF_STDSTR_MAX] = { |
| 51 |
".notdef", "space", "exclam", "quotedbl", "numbersign", |
| 52 |
"dollar", "percent", "ampersand", "quoteright", "parenleft", |
| 53 |
"parenright", "asterisk", "plus", "comma", "hyphen", |
| 54 |
"period", "slash", "zero", "one", "two", |
| 55 |
"three", "four", "five", "six", "seven", |
| 56 |
"eight", "nine", "colon", "semicolon", "less", |
| 57 |
"equal", "greater", "question", "at", "A", |
| 58 |
"B", "C", "D", "E", "F", |
| 59 |
"G", "H", "I", "J", "K", |
| 60 |
"L", "M", "N", "O", "P", |
| 61 |
"Q", "R", "S", "T", "U", |
| 62 |
"V", "W", "X", "Y", "Z", |
| 63 |
"bracketleft", "backslash", "bracketright", "asciicircum", "underscore", |
| 64 |
"quoteleft", "a", "b", "c", "d", |
| 65 |
"e", "f", "g", "h", "i", |
| 66 |
"j", "k", "l", "m", "n", |
| 67 |
"o", "p", "q", "r", "s", |
| 68 |
"t", "u", "v", "w", "x", |
| 69 |
"y", "z", "braceleft", "bar", "braceright", |
| 70 |
"asciitilde", "exclamdown", "cent", "sterling", "fraction", |
| 71 |
"yen", "florin", "section", "currency", "quotesingle", |
| 72 |
"quotedblleft", "guillemotleft", "guilsinglleft", "guilsinglright", "fi", |
| 73 |
"fl", "endash", "dagger", "daggerdbl", "periodcentered", |
| 74 |
"paragraph", "bullet", "quotesinglbase", "quotedblbase", "quotedblright", |
| 75 |
"guillemotright", "ellipsis", "perthousand", "questiondown", "grave", |
| 76 |
"acute", "circumflex", "tilde", "macron", "breve", |
| 77 |
"dotaccent", "dieresis", "ring", "cedilla", "hungarumlaut", |
| 78 |
"ogonek", "caron", "emdash", "AE", "ordfeminine", |
| 79 |
"Lslash", "Oslash", "OE", "ordmasculine", "ae", |
| 80 |
"dotlessi", "lslash", "oslash", "oe", "germandbls", |
| 81 |
"onesuperior", "logicalnot", "mu", "trademark", "Eth", |
| 82 |
"onehalf", "plusminus", "Thorn", "onequarter", "divide", |
| 83 |
"brokenbar", "degree", "thorn", "threequarters", "twosuperior", |
| 84 |
"registered", "minus", "eth", "multiply", "threesuperior", |
| 85 |
"copyright", "Aacute", "Acircumflex", "Adieresis", "Agrave", |
| 86 |
"Aring", "Atilde", "Ccedilla", "Eacute", "Ecircumflex", |
| 87 |
"Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", |
| 88 |
"Igrave", "Ntilde", "Oacute", "Ocircumflex", "Odieresis", |
| 89 |
"Ograve", "Otilde", "Scaron", "Uacute", "Ucircumflex", |
| 90 |
"Udieresis", "Ugrave", "Yacute", "Ydieresis", "Zcaron", |
| 91 |
"aacute", "acircumflex", "adieresis", "agrave", "aring", |
| 92 |
"atilde", "ccedilla", "eacute", "ecircumflex", "edieresis", |
| 93 |
"egrave", "iacute", "icircumflex", "idieresis", "igrave", |
| 94 |
"ntilde", "oacute", "ocircumflex", "odieresis", "ograve", |
| 95 |
"otilde", "scaron", "uacute", "ucircumflex", "udieresis", |
| 96 |
"ugrave", "yacute", "ydieresis", "zcaron", "exclamsmall", |
| 97 |
"Hungarumlautsmall", "dollaroldstyle", "dollarsuperior", "ampersandsmall", |
| 98 |
"Acutesmall", |
| 99 |
"parenleftsuperior", "parenrightsuperior", "twodotenleader", |
| 100 |
"onedotenleader", "zerooldstyle", |
| 101 |
"oneoldstyle", "twooldstyle", "threeoldstyle", "fouroldstyle", |
| 102 |
"fiveoldstyle", |
| 103 |
"sixoldstyle", "sevenoldstyle", "eightoldstyle", "nineoldstyle", |
| 104 |
"commasuperior", |
| 105 |
"threequartersemdash", "periodsuperior", "questionsmall", "asuperior", |
| 106 |
"bsuperior", |
| 107 |
"centsuperior", "dsuperior", "esuperior", "isuperior", "lsuperior", |
| 108 |
"msuperior", "nsuperior", "osuperior", "rsuperior", "ssuperior", |
| 109 |
"tsuperior", "ff", "ffi", "ffl", "parenleftinferior", |
| 110 |
"parenrightinferior", "Circumflexsmall", "hyphensuperior", "Gravesmall", |
| 111 |
"Asmall", |
| 112 |
"Bsmall", "Csmall", "Dsmall", "Esmall", "Fsmall", |
| 113 |
"Gsmall", "Hsmall", "Ismall", "Jsmall", "Ksmall", |
| 114 |
"Lsmall", "Msmall", "Nsmall", "Osmall", "Psmall", |
| 115 |
"Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall", |
| 116 |
"Vsmall", "Wsmall", "Xsmall", "Ysmall", "Zsmall", |
| 117 |
"colonmonetary", "onefitted", "rupiah", "Tildesmall", "exclamdownsmall", |
| 118 |
"centoldstyle", "Lslashsmall", "Scaronsmall", "Zcaronsmall", |
| 119 |
"Dieresissmall", |
| 120 |
"Brevesmall", "Caronsmall", "Dotaccentsmall", "Macronsmall", "figuredash", |
| 121 |
"hypheninferior", "Ogoneksmall", "Ringsmall", "Cedillasmall", |
| 122 |
"questiondownsmall", |
| 123 |
"oneeighth", "threeeighths", "fiveeighths", "seveneighths", "onethird", |
| 124 |
"twothirds", "zerosuperior", "foursuperior", "fivesuperior", "sixsuperior", |
| 125 |
"sevensuperior", "eightsuperior", "ninesuperior", "zeroinferior", |
| 126 |
"oneinferior", |
| 127 |
"twoinferior", "threeinferior", "fourinferior", "fiveinferior", |
| 128 |
"sixinferior", |
| 129 |
"seveninferior", "eightinferior", "nineinferior", "centinferior", |
| 130 |
"dollarinferior", |
| 131 |
"periodinferior", "commainferior", "Agravesmall", "Aacutesmall", |
| 132 |
"Acircumflexsmall", |
| 133 |
"Atildesmall", "Adieresissmall", "Aringsmall", "AEsmall", "Ccedillasmall", |
| 134 |
"Egravesmall", "Eacutesmall", "Ecircumflexsmall", "Edieresissmall", |
| 135 |
"Igravesmall", |
| 136 |
"Iacutesmall", "Icircumflexsmall", "Idieresissmall", "Ethsmall", |
| 137 |
"Ntildesmall", |
| 138 |
"Ogravesmall", "Oacutesmall", "Ocircumflexsmall", "Otildesmall", |
| 139 |
"Odieresissmall", |
| 140 |
"OEsmall", "Oslashsmall", "Ugravesmall", "Uacutesmall", "Ucircumflexsmall", |
| 141 |
"Udieresissmall", "Yacutesmall", "Thornsmall", "Ydieresissmall", |
| 142 |
"001.000", "001.001", "001.002", "001.003", |
| 143 |
"Black", "Bold", "Book", "Light", "Medium", "Regular", "Roman", "Semibold" |
| 144 |
}; |
| 145 |
|
| 146 |
/*tex Only read the header part and forget about the body */ |
| 147 |
|
| 148 |
cff_index *cff_get_index_header(cff_font * cff) |
| 149 |
{ |
| 150 |
cff_index *idx; |
| 151 |
card16 i, count; |
| 152 |
idx = xcalloc(1, sizeof(cff_index)); |
| 153 |
if (cff->header_major == 2) { |
| 154 |
idx->count = count = get_card32(cff); |
| 155 |
} else { |
| 156 |
idx->count = count = get_card16(cff); |
| 157 |
} |
| 158 |
if (count > 0) { |
| 159 |
idx->offsize = get_card8(cff); |
| 160 |
if (idx->offsize < 1 || idx->offsize > 4) |
| 161 |
normal_error("cff","invalid offsize data (1)"); |
| 162 |
idx->offset = xmalloc((unsigned) (((unsigned) count + 1) * sizeof(l_offset))); |
| 163 |
for (i = 0; i <count + 1 ; i++) { |
| 164 |
(idx->offset)[i] = get_offset(cff, idx->offsize); |
| 165 |
if (i == USHRT_MAX) |
| 166 |
break; |
| 167 |
} |
| 168 |
if (idx->offset[0] != 1) |
| 169 |
normal_error("cff","invalid index data"); |
| 170 |
idx->data = NULL; |
| 171 |
} else { |
| 172 |
idx->offsize = 0; |
| 173 |
idx->offset = NULL; |
| 174 |
idx->data = NULL; |
| 175 |
} |
| 176 |
return idx; |
| 177 |
} |
| 178 |
|
| 179 |
cff_index *cff_get_index(cff_font * cff) |
| 180 |
{ |
| 181 |
cff_index *idx; |
| 182 |
card16 i, count; |
| 183 |
size_t length; |
| 184 |
idx = xcalloc(1, sizeof(cff_index)); |
| 185 |
idx->count = count = get_card16(cff); |
| 186 |
if (count > 0) { |
| 187 |
idx->offsize = get_card8(cff); |
| 188 |
if (idx->offsize < 1 || idx->offsize > 4) |
| 189 |
normal_error("cff","invalid offsize data (2)"); |
| 190 |
idx->offset = xmalloc((unsigned) (((unsigned) count + 1) * sizeof(l_offset))); |
| 191 |
for (i = 0; i < count + 1; i++) { |
| 192 |
idx->offset[i] = get_offset(cff, idx->offsize); |
| 193 |
} |
| 194 |
if (idx->offset[0] != 1) |
| 195 |
normal_error("cff","invalid index offset data"); |
| 196 |
length = (size_t) (idx->offset[count] - idx->offset[0]); |
| 197 |
idx->data = xmalloc((unsigned) length * sizeof(card8)); |
| 198 |
memcpy(idx->data, &cff->stream[cff->offset], length); |
| 199 |
cff->offset += length; |
| 200 |
} else { |
| 201 |
idx->offsize = 0; |
| 202 |
idx->offset = NULL; |
| 203 |
idx->data = NULL; |
| 204 |
} |
| 205 |
return idx; |
| 206 |
} |
| 207 |
|
| 208 |
static cff_index *cff_empty_index(cff_font * cff) |
| 209 |
{ |
| 210 |
cff_index *idx; |
| 211 |
idx = xcalloc(1, sizeof(cff_index)); |
| 212 |
idx->count = 0; |
| 213 |
idx->offsize = 0; |
| 214 |
idx->offset = NULL; |
| 215 |
idx->data = NULL; |
| 216 |
return idx; |
| 217 |
} |
| 218 |
|
| 219 |
static cff_index *cff_get_index2(cff_font * cff) |
| 220 |
{ |
| 221 |
/*tex We fake a dict array. */ |
| 222 |
cff_index *idx; |
| 223 |
size_t length; |
| 224 |
idx = xcalloc(1, sizeof(cff_index)); |
| 225 |
length = (size_t) cff->header_offsize; |
| 226 |
idx->offsize = 2; |
| 227 |
idx->count = 1; |
| 228 |
idx->offset = xmalloc((unsigned) (((unsigned) 2) * sizeof(l_offset))); |
| 229 |
idx->offset[0] = 1; |
| 230 |
idx->offset[1] = length + 1; |
| 231 |
idx->data = xmalloc((unsigned) length * sizeof(card8)); |
| 232 |
memcpy(idx->data, &cff->stream[cff->offset], length ); |
| 233 |
cff->offset += length ; |
| 234 |
return idx; |
| 235 |
} |
| 236 |
|
| 237 |
long cff_pack_index(cff_index * idx, card8 * dest, long destlen) |
| 238 |
{ |
| 239 |
long len = 0; |
| 240 |
unsigned long datalen; |
| 241 |
card16 i; |
| 242 |
if (idx->count < 1) { |
| 243 |
if (destlen < 2) |
| 244 |
normal_error("cff","not enough space available"); |
| 245 |
memset(dest, 0, 2); |
| 246 |
return 2; |
| 247 |
} |
| 248 |
len = cff_index_size(idx); |
| 249 |
datalen = idx->offset[idx->count] - 1; |
| 250 |
if (destlen < len) |
| 251 |
normal_error("cff","not enough space available"); |
| 252 |
*(dest++) = (card8) ((idx->count >> 8) & 0xff); |
| 253 |
*(dest++) = (card8) (idx->count & 0xff); |
| 254 |
if (datalen < 0xffUL) { |
| 255 |
idx->offsize = 1; |
| 256 |
*(dest++) = 1; |
| 257 |
for (i = 0; i <= idx->count; i++) { |
| 258 |
*(dest++) = (card8) (idx->offset[i] & 0xff); |
| 259 |
} |
| 260 |
} else if (datalen < 0xffffUL) { |
| 261 |
idx->offsize = 2; |
| 262 |
*(dest++) = 2; |
| 263 |
for (i = 0; i <= idx->count; i++) { |
| 264 |
*(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff); |
| 265 |
*(dest++) = (card8) (idx->offset[i] & 0xff); |
| 266 |
} |
| 267 |
} else if (datalen < 0xffffffUL) { |
| 268 |
idx->offsize = 3; |
| 269 |
*(dest++) = 3; |
| 270 |
for (i = 0; i <= idx->count; i++) { |
| 271 |
*(dest++) = (card8) ((idx->offset[i] >> 16) & 0xff); |
| 272 |
*(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff); |
| 273 |
*(dest++) = (card8) (idx->offset[i] & 0xff); |
| 274 |
} |
| 275 |
} else { |
| 276 |
idx->offsize = 4; |
| 277 |
*(dest++) = 4; |
| 278 |
for (i = 0; i <= idx->count; i++) { |
| 279 |
*(dest++) = (card8) ((idx->offset[i] >> 24) & 0xff); |
| 280 |
*(dest++) = (card8) ((idx->offset[i] >> 16) & 0xff); |
| 281 |
*(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff); |
| 282 |
*(dest++) = (card8) (idx->offset[i] & 0xff); |
| 283 |
} |
| 284 |
} |
| 285 |
memmove(dest, idx->data, idx->offset[idx->count] - 1); |
| 286 |
return len; |
| 287 |
} |
| 288 |
|
| 289 |
long cff_index_size(cff_index * idx) |
| 290 |
{ |
| 291 |
if (idx->count > 0) { |
| 292 |
l_offset datalen; |
| 293 |
datalen = idx->offset[idx->count] - 1; |
| 294 |
if (datalen < 0xffUL) { |
| 295 |
idx->offsize = 1; |
| 296 |
} else if (datalen < 0xffffUL) { |
| 297 |
idx->offsize = 2; |
| 298 |
} else if (datalen < 0xffffffUL) { |
| 299 |
idx->offsize = 3; |
| 300 |
} else { |
| 301 |
idx->offsize = 4; |
| 302 |
} |
| 303 |
return (3 + (idx->offsize) * (idx->count + 1) + (long) datalen); |
| 304 |
} else { |
| 305 |
return 2; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
cff_index *cff_new_index(card16 count) |
| 310 |
{ |
| 311 |
cff_index *idx; |
| 312 |
idx = xcalloc(1, sizeof(cff_index)); |
| 313 |
idx->count = count; |
| 314 |
idx->offsize = 0; |
| 315 |
if (count > 0) { |
| 316 |
idx->offset = xcalloc((unsigned) (count + 1), sizeof(l_offset)); |
| 317 |
(idx->offset)[0] = 1; |
| 318 |
} else { |
| 319 |
idx->offset = NULL; |
| 320 |
} |
| 321 |
idx->data = NULL; |
| 322 |
return idx; |
| 323 |
} |
| 324 |
|
| 325 |
void cff_release_index(cff_index * idx) |
| 326 |
{ |
| 327 |
if (idx) { |
| 328 |
xfree(idx->data); |
| 329 |
xfree(idx->offset); |
| 330 |
xfree(idx); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
void cff_release_dict(cff_dict * dict) |
| 335 |
{ |
| 336 |
if (dict) { |
| 337 |
if (dict->entries) { |
| 338 |
int i; |
| 339 |
for (i = 0; i < dict->count; i++) { |
| 340 |
xfree((dict->entries)[i].values); |
| 341 |
} |
| 342 |
xfree(dict->entries); |
| 343 |
} |
| 344 |
xfree(dict); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
void cff_release_encoding(cff_encoding * encoding) |
| 349 |
{ |
| 350 |
if (encoding) { |
| 351 |
switch (encoding->format & (~0x80)) { |
| 352 |
case 0: |
| 353 |
xfree(encoding->data.codes); |
| 354 |
break; |
| 355 |
case 1: |
| 356 |
xfree(encoding->data.range1); |
| 357 |
break; |
| 358 |
default: |
| 359 |
normal_error("cff","unknown encoding format"); |
| 360 |
} |
| 361 |
if (encoding->format & 0x80) |
| 362 |
xfree(encoding->supp); |
| 363 |
xfree(encoding); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
void cff_release_charsets(cff_charsets * charset) |
| 368 |
{ |
| 369 |
if (charset) { |
| 370 |
switch (charset->format) { |
| 371 |
case 0: |
| 372 |
xfree(charset->data.glyphs); |
| 373 |
break; |
| 374 |
case 1: |
| 375 |
xfree(charset->data.range1); |
| 376 |
break; |
| 377 |
case 2: |
| 378 |
xfree(charset->data.range2); |
| 379 |
break; |
| 380 |
default: |
| 381 |
break; |
| 382 |
} |
| 383 |
xfree(charset); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
void cff_release_fdselect(cff_fdselect * fdselect) |
| 388 |
{ |
| 389 |
if (fdselect) { |
| 390 |
if (fdselect->format == 0) { |
| 391 |
xfree(fdselect->data.fds); |
| 392 |
} else if (fdselect->format == 3) { |
| 393 |
xfree(fdselect->data.ranges); |
| 394 |
} |
| 395 |
xfree(fdselect); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
void cff_close(cff_font * cff) |
| 400 |
{ |
| 401 |
card16 i; |
| 402 |
if (cff) { |
| 403 |
xfree(cff->fontname); |
| 404 |
if (cff->name) |
| 405 |
cff_release_index(cff->name); |
| 406 |
if (cff->topdict) |
| 407 |
cff_release_dict(cff->topdict); |
| 408 |
if (cff->string) |
| 409 |
cff_release_index(cff->string); |
| 410 |
if (cff->gsubr) |
| 411 |
cff_release_index(cff->gsubr); |
| 412 |
if (cff->encoding) |
| 413 |
cff_release_encoding(cff->encoding); |
| 414 |
if (cff->charsets) |
| 415 |
cff_release_charsets(cff->charsets); |
| 416 |
if (cff->fdselect) |
| 417 |
cff_release_fdselect(cff->fdselect); |
| 418 |
if (cff->cstrings) |
| 419 |
cff_release_index(cff->cstrings); |
| 420 |
if (cff->fdarray) { |
| 421 |
for (i = 0; i < cff->num_fds; i++) { |
| 422 |
if (cff->fdarray[i]) |
| 423 |
cff_release_dict(cff->fdarray[i]); |
| 424 |
} |
| 425 |
xfree(cff->fdarray); |
| 426 |
} |
| 427 |
if (cff->private) { |
| 428 |
for (i = 0; i < cff->num_fds; i++) { |
| 429 |
if (cff->private[i]) |
| 430 |
cff_release_dict(cff->private[i]); |
| 431 |
} |
| 432 |
xfree(cff->private); |
| 433 |
} |
| 434 |
if (cff->subrs) { |
| 435 |
for (i = 0; i < cff->num_fds; i++) { |
| 436 |
if (cff->subrs[i]) |
| 437 |
cff_release_index(cff->subrs[i]); |
| 438 |
} |
| 439 |
xfree(cff->subrs); |
| 440 |
} |
| 441 |
if (cff->_string) |
| 442 |
cff_release_index(cff->_string); |
| 443 |
xfree(cff); |
| 444 |
} |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
char *cff_get_name(cff_font * cff) |
| 449 |
{ |
| 450 |
char *fontname; |
| 451 |
l_offset len; |
| 452 |
cff_index *idx; |
| 453 |
idx = cff->name; |
| 454 |
len = idx->offset[cff->index + 1] - idx->offset[cff->index]; |
| 455 |
fontname = xmalloc((unsigned) (len + 1) * sizeof(char)); |
| 456 |
memcpy(fontname, idx->data + idx->offset[cff->index] - 1, len); |
| 457 |
fontname[len] = '\0'; |
| 458 |
return fontname; |
| 459 |
} |
| 460 |
|
| 461 |
long cff_set_name(cff_font * cff, char *name) |
| 462 |
{ |
| 463 |
cff_index *idx; |
| 464 |
if (strlen(name) > 127) |
| 465 |
normal_error("cff","FontName string length too large"); |
| 466 |
if (cff->name) |
| 467 |
cff_release_index(cff->name); |
| 468 |
cff->name = idx = xcalloc(1, sizeof(cff_index)); |
| 469 |
idx->count = 1; |
| 470 |
idx->offsize = 1; |
| 471 |
idx->offset = xmalloc(2 * sizeof(l_offset)); |
| 472 |
(idx->offset)[0] = 1; |
| 473 |
(idx->offset)[1] = strlen(name) + 1; |
| 474 |
idx->data = xmalloc((unsigned) strlen(name) * sizeof(card8)); |
| 475 |
/*tex No trailing |\0| */ |
| 476 |
memmove(idx->data, name, strlen(name)); |
| 477 |
return (long) (5 + strlen(name)); |
| 478 |
} |
| 479 |
|
| 480 |
long cff_put_header(cff_font * cff, card8 * dest, long destlen) |
| 481 |
{ |
| 482 |
if (destlen < 4) |
| 483 |
normal_error("cff","not enough space available"); |
| 484 |
/*tex cff->header_major */ |
| 485 |
*(dest++) = 1; |
| 486 |
*(dest++) = cff->header_minor; |
| 487 |
*(dest++) = 4; |
| 488 |
/*tex |
| 489 |
Additional data in between header and Name INDEX is ignored. We will set |
| 490 |
all offset (0) to a four-byte integer. |
| 491 |
*/ |
| 492 |
*(dest++) = 4; |
| 493 |
cff->header_offsize = 4; |
| 494 |
return 4; |
| 495 |
} |
| 496 |
|
| 497 |
#define CFF_PARSE_OK 0 |
| 498 |
#define CFF_CFF_ERROR_PARSE_CFF_ERROR -1 |
| 499 |
#define CFF_CFF_ERROR_STACK_OVERFLOW -2 |
| 500 |
#define CFF_CFF_ERROR_STACK_UNDERFLOW -3 |
| 501 |
#define CFF_CFF_ERROR_STACK_RANGECHECK -4 |
| 502 |
|
| 503 |
#define DICT_ENTRY_MAX 16 |
| 504 |
|
| 505 |
cff_dict *cff_new_dict(void) |
| 506 |
{ |
| 507 |
cff_dict *dict; |
| 508 |
dict = xcalloc(1, sizeof(cff_dict)); |
| 509 |
dict->max = DICT_ENTRY_MAX; |
| 510 |
dict->count = 0; |
| 511 |
dict->entries = xcalloc((unsigned) dict->max, sizeof(cff_dict_entry)); |
| 512 |
return dict; |
| 513 |
} |
| 514 |
|
| 515 |
/*tex |
| 516 |
|
| 517 |
Operand stack: only numbers are stored (as double). Operand types are: |
| 518 |
|
| 519 |
\startitemize |
| 520 |
\startitem number: double (integer or real) \stopitem |
| 521 |
\startitem boolean: stored as a number \stopitem |
| 522 |
\startitem SID: stored as a number \stopitem |
| 523 |
\startitem array: array of numbers \stopitem |
| 524 |
\startitem delta: array of numbers \stopitem |
| 525 |
\stopitemize |
| 526 |
|
| 527 |
*/ |
| 528 |
|
| 529 |
#define CFF_DICT_STACK_LIMIT 64 |
| 530 |
static int stack_top = 0; |
| 531 |
static double arg_stack[CFF_DICT_STACK_LIMIT]; |
| 532 |
|
| 533 |
/* The CFF DICT encoding: */ |
| 534 |
|
| 535 |
#define CFF_LAST_DICT_OP1 26 |
| 536 |
#define CFF_LAST_DICT_OP2 39 |
| 537 |
#define CFF_LAST_DICT_OP (CFF_LAST_DICT_OP1 + CFF_LAST_DICT_OP2) |
| 538 |
|
| 539 |
static struct { |
| 540 |
const char *opname; |
| 541 |
int argtype; |
| 542 |
} dict_operator[CFF_LAST_DICT_OP] = { |
| 543 |
{ "version", CFF_TYPE_SID }, |
| 544 |
{ "Notice", CFF_TYPE_SID }, |
| 545 |
{ "FullName", CFF_TYPE_SID }, |
| 546 |
{ "FamilyName", CFF_TYPE_SID }, |
| 547 |
{ "Weight", CFF_TYPE_SID }, |
| 548 |
{ "FontBBox", CFF_TYPE_ARRAY }, |
| 549 |
{ "BlueValues", CFF_TYPE_DELTA }, |
| 550 |
{ "OtherBlues", CFF_TYPE_DELTA }, |
| 551 |
{ "FamilyBlues", CFF_TYPE_DELTA }, |
| 552 |
{ "FamilyOtherBlues", CFF_TYPE_DELTA }, |
| 553 |
{ "StdHW", CFF_TYPE_NUMBER }, |
| 554 |
{ "StdVW", CFF_TYPE_NUMBER }, |
| 555 |
{ NULL, -1 }, |
| 556 |
{ "UniqueID", CFF_TYPE_NUMBER }, |
| 557 |
{ "XUID", CFF_TYPE_ARRAY }, |
| 558 |
{ "charset", CFF_TYPE_OFFSET }, |
| 559 |
{ "Encoding", CFF_TYPE_OFFSET }, |
| 560 |
{ "CharStrings", CFF_TYPE_OFFSET }, |
| 561 |
{ "Private", CFF_TYPE_SZOFF }, |
| 562 |
{ "Subrs", CFF_TYPE_OFFSET }, |
| 563 |
{ "defaultWidthX", CFF_TYPE_NUMBER }, |
| 564 |
{ "nominalWidthX", CFF_TYPE_NUMBER }, |
| 565 |
{ NULL, -1 }, |
| 566 |
{ NULL, -1 }, |
| 567 |
/*tex two CFF2 instructions */ |
| 568 |
{ "vstore", CFF_TYPE_OFFSET }, |
| 569 |
{ "maxstack", CFF_TYPE_NUMBER }, |
| 570 |
/*tex Here we start with operator 2 of 12. */ |
| 571 |
{ "Copyright", CFF_TYPE_SID }, |
| 572 |
{ "IsFixedPitch", CFF_TYPE_BOOLEAN }, |
| 573 |
{ "ItalicAngle", CFF_TYPE_NUMBER }, |
| 574 |
{ "UnderlinePosition", CFF_TYPE_NUMBER }, |
| 575 |
{ "UnderlineThickness", CFF_TYPE_NUMBER }, |
| 576 |
{ "PaintType", CFF_TYPE_NUMBER }, |
| 577 |
{ "CharstringType", CFF_TYPE_NUMBER }, |
| 578 |
{ "FontMatrix", CFF_TYPE_ARRAY }, |
| 579 |
{ "StrokeWidth", CFF_TYPE_NUMBER }, |
| 580 |
{ "BlueScale", CFF_TYPE_NUMBER }, |
| 581 |
{ "BlueShift", CFF_TYPE_NUMBER }, |
| 582 |
{ "BlueFuzz", CFF_TYPE_NUMBER }, |
| 583 |
{ "StemSnapH", CFF_TYPE_DELTA }, |
| 584 |
{ "StemSnapV", CFF_TYPE_DELTA }, |
| 585 |
{ "ForceBold", CFF_TYPE_BOOLEAN }, |
| 586 |
{ NULL, -1 }, |
| 587 |
{ NULL, -1 }, |
| 588 |
{ "LanguageGroup", CFF_TYPE_NUMBER }, |
| 589 |
{ "ExpansionFactor", CFF_TYPE_NUMBER }, |
| 590 |
{ "InitialRandomSeed", CFF_TYPE_NUMBER }, |
| 591 |
{ "SyntheticBase", CFF_TYPE_NUMBER }, |
| 592 |
{ "PostScript", CFF_TYPE_SID }, |
| 593 |
{ "BaseFontName", CFF_TYPE_SID }, |
| 594 |
{ "BaseFontBlend", CFF_TYPE_DELTA }, |
| 595 |
{ NULL, -1 }, |
| 596 |
{ NULL, -1 }, |
| 597 |
{ NULL, -1 }, |
| 598 |
{ NULL, -1 }, |
| 599 |
{ NULL, -1 }, |
| 600 |
{ NULL, -1 }, |
| 601 |
{ "ROS", CFF_TYPE_ROS }, |
| 602 |
{ "CIDFontVersion", CFF_TYPE_NUMBER }, |
| 603 |
{ "CIDFontRevision", CFF_TYPE_NUMBER }, |
| 604 |
{ "CIDFontType", CFF_TYPE_NUMBER }, |
| 605 |
{ "CIDCount", CFF_TYPE_NUMBER }, |
| 606 |
{ "UIDBase", CFF_TYPE_NUMBER }, |
| 607 |
{ "FDArray", CFF_TYPE_OFFSET }, |
| 608 |
{ "FDSelect", CFF_TYPE_OFFSET }, |
| 609 |
{ "FontName", CFF_TYPE_SID } |
| 610 |
}; |
| 611 |
|
| 612 |
/*tex Parse DICT data */ |
| 613 |
|
| 614 |
static double get_integer(card8 ** data, card8 * endptr, int *status) |
| 615 |
{ |
| 616 |
long result = 0; |
| 617 |
card8 b0, b1, b2; |
| 618 |
|
| 619 |
b0 = *(*data)++; |
| 620 |
if (b0 == 28 && *data < endptr - 2) { |
| 621 |
/*tex shortint */ |
| 622 |
b1 = *(*data)++; |
| 623 |
b2 = *(*data)++; |
| 624 |
result = b1 * 256 + b2; |
| 625 |
if (result > 0x7fffL) |
| 626 |
result -= 0x10000L; |
| 627 |
} else if (b0 == 29 && *data < endptr - 4) { |
| 628 |
/*tex longint */ |
| 629 |
int i; |
| 630 |
result = *(*data)++; |
| 631 |
if (result > 0x7f) |
| 632 |
result -= 0x100; |
| 633 |
for (i = 0; i < 3; i++) { |
| 634 |
result = result * 256 + (**data); |
| 635 |
*data += 1; |
| 636 |
} |
| 637 |
} else if (b0 >= 32 && b0 <= 246) { |
| 638 |
/*tex int (1) */ |
| 639 |
result = b0 - 139; |
| 640 |
} else if (b0 >= 247 && b0 <= 250) { |
| 641 |
/*tex int (2) */ |
| 642 |
b1 = *(*data)++; |
| 643 |
result = (b0 - 247) * 256 + b1 + 108; |
| 644 |
} else if (b0 >= 251 && b0 <= 254) { |
| 645 |
b1 = *(*data)++; |
| 646 |
result = -(b0 - 251) * 256 - b1 - 108; |
| 647 |
} else { |
| 648 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 649 |
} |
| 650 |
return (double) result; |
| 651 |
} |
| 652 |
|
| 653 |
/*tex Simply uses |strtod|: */ |
| 654 |
|
| 655 |
static double get_real(card8 ** data, card8 * endptr, int *status) |
| 656 |
{ |
| 657 |
double result = 0.0; |
| 658 |
int nibble = 0, pos = 0; |
| 659 |
int len = 0, fail = 0; |
| 660 |
|
| 661 |
if (**data != 30 || *data >= endptr - 1) { |
| 662 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 663 |
return 0.0; |
| 664 |
} |
| 665 |
/*tex Skip the first byte (30): */ |
| 666 |
*data += 1; |
| 667 |
pos = 0; |
| 668 |
while ((!fail) && len < WORK_BUFFER_SIZE - 2 && *data < endptr) { |
| 669 |
/*tex Get a nibble. */ |
| 670 |
if (pos % 2) { |
| 671 |
nibble = **data & 0x0f; |
| 672 |
*data += 1; |
| 673 |
} else { |
| 674 |
nibble = (**data >> 4) & 0x0f; |
| 675 |
} |
| 676 |
if (nibble >= 0x00 && nibble <= 0x09) { |
| 677 |
work_buffer[len++] = (char) (nibble + '0'); |
| 678 |
} else if (nibble == 0x0a) { /* . */ |
| 679 |
work_buffer[len++] = '.'; |
| 680 |
} else if (nibble == 0x0b || nibble == 0x0c) { |
| 681 |
/*tex E, E- */ |
| 682 |
work_buffer[len++] = 'e'; |
| 683 |
if (nibble == 0x0c) |
| 684 |
work_buffer[len++] = '-'; |
| 685 |
} else if (nibble == 0x0e) { |
| 686 |
/*tex the minus */ |
| 687 |
work_buffer[len++] = '-'; |
| 688 |
} else if (nibble == 0x0d) { |
| 689 |
/*tex do nothing */ |
| 690 |
} else if (nibble == 0x0f) { |
| 691 |
/*tex we're done */ |
| 692 |
work_buffer[len++] = '\0'; |
| 693 |
if (((pos % 2) == 0) && (**data != 0xff)) { |
| 694 |
fail = 1; |
| 695 |
} |
| 696 |
break; |
| 697 |
} else { |
| 698 |
/*tex invalid */ |
| 699 |
fail = 1; |
| 700 |
} |
| 701 |
pos++; |
| 702 |
} |
| 703 |
/*tex the returned values */ |
| 704 |
if (fail || nibble != 0x0f) { |
| 705 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 706 |
} else { |
| 707 |
char *s; |
| 708 |
errno=0; |
| 709 |
result = strtod(work_buffer, &s); |
| 710 |
if ((result==0.0 && work_buffer==s) || errno) { |
| 711 |
/*tex Conversion is not possible. */ |
| 712 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 713 |
} |
| 714 |
} |
| 715 |
return result; |
| 716 |
} |
| 717 |
|
| 718 |
/*tex Operators */ |
| 719 |
|
| 720 |
static void add_dict(cff_dict * dict, card8 ** data, card8 * endptr, int *status) |
| 721 |
{ |
| 722 |
int id, argtype, t; |
| 723 |
id = **data; |
| 724 |
if (id == 0x0c) { |
| 725 |
*data += 1; |
| 726 |
if (*data >= endptr || |
| 727 |
(id = **data + CFF_LAST_DICT_OP1) >= CFF_LAST_DICT_OP) { |
| 728 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 729 |
return; |
| 730 |
} |
| 731 |
} else if (id >= CFF_LAST_DICT_OP1) { |
| 732 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 733 |
return; |
| 734 |
} |
| 735 |
argtype = dict_operator[id].argtype; |
| 736 |
if (dict_operator[id].opname == NULL || argtype < 0) { |
| 737 |
*status = CFF_CFF_ERROR_PARSE_CFF_ERROR; |
| 738 |
return; |
| 739 |
} |
| 740 |
if (dict->count >= dict->max) { |
| 741 |
dict->max += DICT_ENTRY_MAX; |
| 742 |
/*tex Not zeroed! */ |
| 743 |
dict->entries = xrealloc(dict->entries, (unsigned) ((unsigned) dict->max * sizeof(cff_dict_entry))); |
| 744 |
} |
| 745 |
(dict->entries)[dict->count].id = id; |
| 746 |
(dict->entries)[dict->count].key = dict_operator[id].opname; |
| 747 |
if (argtype == CFF_TYPE_NUMBER || |
| 748 |
argtype == CFF_TYPE_BOOLEAN || |
| 749 |
argtype == CFF_TYPE_SID || argtype == CFF_TYPE_OFFSET) { |
| 750 |
/*tex Check for underflow here, as exactly one operand is expected. */ |
| 751 |
if (stack_top < 1) { |
| 752 |
*status = CFF_CFF_ERROR_STACK_UNDERFLOW; |
| 753 |
return; |
| 754 |
} |
| 755 |
stack_top--; |
| 756 |
(dict->entries)[dict->count].count = 1; |
| 757 |
(dict->entries)[dict->count].values = xcalloc(1, sizeof(double)); |
| 758 |
(dict->entries)[dict->count].values[0] = arg_stack[stack_top]; |
| 759 |
dict->count += 1; |
| 760 |
} else { |
| 761 |
/*tex |
| 762 |
Just ignore operator if there were no operands provided. Don't treat |
| 763 |
this as underflow, e.g. |StemSnapV| in |TemporaLGCUni-Italic.otf|. |
| 764 |
*/ |
| 765 |
if ((t = stack_top) > 0) { |
| 766 |
(dict->entries)[dict->count].count = stack_top; |
| 767 |
(dict->entries)[dict->count].values = |
| 768 |
xmalloc((unsigned) ((unsigned) stack_top * sizeof(double))); |
| 769 |
while (stack_top > 0) { |
| 770 |
stack_top--; |
| 771 |
(dict->entries)[dict->count].values[stack_top] = |
| 772 |
arg_stack[stack_top]; |
| 773 |
} |
| 774 |
if (t > 3 && strcmp(dict_operator[id].opname, "FontMatrix") == 0) { |
| 775 |
(dict->entries)[dict->count].values[0] = 0.001; |
| 776 |
(dict->entries)[dict->count].values[3] = 0.001; |
| 777 |
} |
| 778 |
dict->count += 1; |
| 779 |
} |
| 780 |
} |
| 781 |
*data += 1; |
| 782 |
return; |
| 783 |
} |
| 784 |
|
| 785 |
/*tex |
| 786 |
|
| 787 |
All operands are treated as number or array of numbers. |
| 788 |
|
| 789 |
\startitemize |
| 790 |
\startitem |Private|: two numbers, size and offset \stopitem |
| 791 |
\startitem |ROS|: hree numbers, SID, SID, and a number \stopitem |
| 792 |
\stopitemize |
| 793 |
|
| 794 |
*/ |
| 795 |
|
| 796 |
cff_dict *cff_dict_unpack(card8 * data, card8 * endptr) |
| 797 |
{ |
| 798 |
cff_dict *dict; |
| 799 |
int status = CFF_PARSE_OK; |
| 800 |
stack_top = 0; |
| 801 |
dict = cff_new_dict(); |
| 802 |
while (data < endptr && status == CFF_PARSE_OK) { |
| 803 |
if (*data < CFF_LAST_DICT_OP1) { |
| 804 |
/*tex Some operator. */ |
| 805 |
add_dict(dict, &data, endptr, &status); |
| 806 |
} else if (*data == 30) { |
| 807 |
/*tex First byte of a sequence (variable). */ |
| 808 |
if (stack_top < CFF_DICT_STACK_LIMIT) { |
| 809 |
arg_stack[stack_top] = get_real(&data, endptr, &status); |
| 810 |
stack_top++; |
| 811 |
} else { |
| 812 |
status = CFF_CFF_ERROR_STACK_OVERFLOW; |
| 813 |
} |
| 814 |
} else if (*data == 255 || (*data >= CFF_LAST_DICT_OP1 && *data <= 27)) { |
| 815 |
/*tex Reserved. */ |
| 816 |
data++; |
| 817 |
} else { |
| 818 |
/*tex Everything else is an integer. */ |
| 819 |
if (stack_top < CFF_DICT_STACK_LIMIT) { |
| 820 |
arg_stack[stack_top] = get_integer(&data, endptr, &status); |
| 821 |
stack_top++; |
| 822 |
} else { |
| 823 |
status = CFF_CFF_ERROR_STACK_OVERFLOW; |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
if (status != CFF_PARSE_OK) { |
| 828 |
formatted_error("cff","parsing DICT failed (error=%d)", status); |
| 829 |
} else if (stack_top != 0) { |
| 830 |
normal_warning("cff","garbage in DICT data"); |
| 831 |
stack_top = 0; |
| 832 |
} |
| 833 |
return dict; |
| 834 |
} |
| 835 |
|
| 836 |
int cff_dict_known(cff_dict * dict, const char *key) |
| 837 |
{ |
| 838 |
int i; |
| 839 |
for (i = 0; i < dict->count; i++) { |
| 840 |
if (key && strcmp(key, (dict->entries)[i].key) == 0 |
| 841 |
&& (dict->entries)[i].count > 0) |
| 842 |
return 1; |
| 843 |
} |
| 844 |
return 0; |
| 845 |
} |
| 846 |
|
| 847 |
double cff_dict_get(cff_dict * dict, const char *key, int idx) |
| 848 |
{ |
| 849 |
double value = 0.0; |
| 850 |
int i; |
| 851 |
assert(key && dict); |
| 852 |
for (i = 0; i < dict->count; i++) { |
| 853 |
if (strcmp(key, (dict->entries)[i].key) == 0) { |
| 854 |
if ((dict->entries)[i].count > idx) |
| 855 |
value = (dict->entries)[i].values[idx]; |
| 856 |
else |
| 857 |
normal_error("cff","invalid index number"); |
| 858 |
break; |
| 859 |
} |
| 860 |
} |
| 861 |
if (i == dict->count) |
| 862 |
formatted_error("cff","DICT entry '%s' not found", key); |
| 863 |
return value; |
| 864 |
} |
| 865 |
|
| 866 |
card8 cff_fdselect_lookup(cff_font * cff, card16 gid) |
| 867 |
{ |
| 868 |
card8 fd = 0xff; |
| 869 |
cff_fdselect *fdsel; |
| 870 |
if (cff->fdselect == NULL) |
| 871 |
normal_error("cff","FDSelect not available"); |
| 872 |
fdsel = cff->fdselect; |
| 873 |
if (gid >= cff->num_glyphs) |
| 874 |
normal_error("cff","invalid glyph index"); |
| 875 |
switch (fdsel->format) { |
| 876 |
case 0: |
| 877 |
fd = fdsel->data.fds[gid]; |
| 878 |
break; |
| 879 |
case 3: |
| 880 |
{ |
| 881 |
if (gid == 0) { |
| 882 |
fd = (fdsel->data).ranges[0].fd; |
| 883 |
} else { |
| 884 |
card16 i; |
| 885 |
for (i = 1; i < (fdsel->num_entries); i++) { |
| 886 |
if (gid < (fdsel->data).ranges[i].first) |
| 887 |
break; |
| 888 |
} |
| 889 |
fd = (fdsel->data).ranges[i - 1].fd; |
| 890 |
} |
| 891 |
} |
| 892 |
break; |
| 893 |
default: |
| 894 |
normal_error("cff","invalid FDSelect format"); |
| 895 |
break; |
| 896 |
} |
| 897 |
if (fd >= cff->num_fds) |
| 898 |
normal_error("cff","invalid Font DICT index"); |
| 899 |
return fd; |
| 900 |
} |
| 901 |
|
| 902 |
long cff_read_subrs(cff_font * cff) |
| 903 |
{ |
| 904 |
long len = 0; |
| 905 |
long offset; |
| 906 |
int i; |
| 907 |
if ((cff->flag & FONTTYPE_CIDFONT) && cff->fdselect == NULL) { |
| 908 |
cff_read_fdselect(cff); |
| 909 |
} |
| 910 |
if ((cff->flag & FONTTYPE_CIDFONT) && cff->fdarray == NULL) { |
| 911 |
cff_read_fdarray(cff); |
| 912 |
} |
| 913 |
if (cff->private == NULL) |
| 914 |
cff_read_private(cff); |
| 915 |
if (cff->gsubr == NULL) { |
| 916 |
cff->offset = cff->gsubr_offset; |
| 917 |
cff->gsubr = cff_get_index(cff); |
| 918 |
} |
| 919 |
cff->subrs = xcalloc(cff->num_fds, sizeof(cff_index *)); |
| 920 |
if (cff->flag & FONTTYPE_CIDFONT) { |
| 921 |
for (i = 0; i < cff->num_fds; i++) { |
| 922 |
if (cff->private[i] == NULL || |
| 923 |
!cff_dict_known(cff->private[i], "Subrs")) { |
| 924 |
(cff->subrs)[i] = NULL; |
| 925 |
} else { |
| 926 |
offset = (long) cff_dict_get(cff->fdarray[i], "Private", 1); |
| 927 |
offset += (long) cff_dict_get(cff->private[i], "Subrs", 0); |
| 928 |
cff->offset = (l_offset) offset; |
| 929 |
(cff->subrs)[i] = cff_get_index(cff); |
| 930 |
len += cff_index_size((cff->subrs)[i]); |
| 931 |
} |
| 932 |
} |
| 933 |
} else if (cff->private[0] == NULL || !cff_dict_known(cff->private[0], "Subrs")) { |
| 934 |
(cff->subrs)[0] = NULL; |
| 935 |
} else { |
| 936 |
offset = (long) cff_dict_get(cff->topdict, "Private", 1); |
| 937 |
offset += (long) cff_dict_get(cff->private[0], "Subrs", 0); |
| 938 |
cff->offset = (l_offset) offset; |
| 939 |
(cff->subrs)[0] = cff_get_index(cff); |
| 940 |
len += cff_index_size((cff->subrs)[0]); |
| 941 |
} |
| 942 |
return len; |
| 943 |
} |
| 944 |
|
| 945 |
long cff_read_fdarray(cff_font * cff) |
| 946 |
{ |
| 947 |
long len = 0; |
| 948 |
cff_index *idx; |
| 949 |
long offset, size; |
| 950 |
card16 i; |
| 951 |
if (cff->topdict == NULL) |
| 952 |
normal_error("cff","top DICT not found"); |
| 953 |
if (!(cff->flag & FONTTYPE_CIDFONT)) |
| 954 |
return 0; |
| 955 |
offset = (long) cff_dict_get(cff->topdict, "FDArray", 0); |
| 956 |
cff->offset = (l_offset) offset; |
| 957 |
idx = cff_get_index(cff); |
| 958 |
cff->num_fds = (card8) idx->count; |
| 959 |
cff->fdarray = xmalloc((unsigned) (idx->count * sizeof(cff_dict *))); |
| 960 |
for (i = 0; i < idx->count; i++) { |
| 961 |
card8 *data = idx->data + (idx->offset)[i] - 1; |
| 962 |
size = (long) ((idx->offset)[i + 1] - (idx->offset)[i]); |
| 963 |
if (size > 0) { |
| 964 |
(cff->fdarray)[i] = cff_dict_unpack(data, data + size); |
| 965 |
} else { |
| 966 |
(cff->fdarray)[i] = NULL; |
| 967 |
} |
| 968 |
} |
| 969 |
len = cff_index_size(idx); |
| 970 |
cff_release_index(idx); |
| 971 |
return len; |
| 972 |
} |
| 973 |
|
| 974 |
long cff_read_private(cff_font * cff) |
| 975 |
{ |
| 976 |
long len = 0; |
| 977 |
card8 *data; |
| 978 |
long offset, size; |
| 979 |
if (cff->flag & FONTTYPE_CIDFONT) { |
| 980 |
int i; |
| 981 |
if (cff->fdarray == NULL) |
| 982 |
cff_read_fdarray(cff); |
| 983 |
cff->private = xmalloc((unsigned) (cff->num_fds * sizeof(cff_dict *))); |
| 984 |
for (i = 0; i < cff->num_fds; i++) { |
| 985 |
if (cff->fdarray[i] != NULL && |
| 986 |
cff_dict_known(cff->fdarray[i], "Private") && |
| 987 |
(size = (long) cff_dict_get(cff->fdarray[i], "Private", 0)) > 0) { |
| 988 |
offset = (long) cff_dict_get(cff->fdarray[i], "Private", 1); |
| 989 |
cff->offset = (l_offset) offset; |
| 990 |
data = xmalloc((unsigned) size * sizeof(card8)); |
| 991 |
memcpy(data, &cff->stream[cff->offset], (size_t) size); |
| 992 |
cff->offset = (l_offset) size; |
| 993 |
(cff->private)[i] = cff_dict_unpack(data, data + size); |
| 994 |
xfree(data); |
| 995 |
len += size; |
| 996 |
} else { |
| 997 |
(cff->private)[i] = NULL; |
| 998 |
} |
| 999 |
} |
| 1000 |
} else { |
| 1001 |
cff->num_fds = 1; |
| 1002 |
cff->private = xmalloc(sizeof(cff_dict *)); |
| 1003 |
if (cff_dict_known(cff->topdict, "Private") && |
| 1004 |
(size = (long) cff_dict_get(cff->topdict, "Private", 0)) > 0) { |
| 1005 |
offset = (long) cff_dict_get(cff->topdict, "Private", 1); |
| 1006 |
cff->offset = (l_offset) offset; |
| 1007 |
data = xmalloc((unsigned) size * sizeof(card8)); |
| 1008 |
memcpy(data, &cff->stream[cff->offset], (size_t) size); |
| 1009 |
cff->offset = (l_offset) size; |
| 1010 |
cff->private[0] = cff_dict_unpack(data, data + size); |
| 1011 |
xfree(data); |
| 1012 |
len += size; |
| 1013 |
} else { |
| 1014 |
(cff->private)[0] = NULL; |
| 1015 |
len = 0; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
return len; |
| 1019 |
} |
| 1020 |
|
| 1021 |
cff_font *read_cff(unsigned char *buf, long buflength, int n) |
| 1022 |
{ |
| 1023 |
cff_font *cff; |
| 1024 |
cff_index *idx; |
| 1025 |
long offset; |
| 1026 |
cff = xcalloc(1, sizeof(cff_font)); |
| 1027 |
cff->stream = buf; |
| 1028 |
cff->stream_size = (l_offset) buflength; |
| 1029 |
cff->index = n; |
| 1030 |
cff->header_major = get_card8(cff); |
| 1031 |
cff->header_minor = get_card8(cff); |
| 1032 |
cff->header_hdr_size = get_card8(cff); |
| 1033 |
if (cff->header_major == 2) { |
| 1034 |
/*tex We have only one top dictionary. */ |
| 1035 |
cff->header_offsize = get_card16(cff); |
| 1036 |
} else { |
| 1037 |
cff->header_offsize = get_card8(cff); |
| 1038 |
if (cff->header_offsize < 1 || cff->header_offsize > 4) { |
| 1039 |
normal_warning("cff","invalid offsize data (4)"); |
| 1040 |
cff_close(cff); |
| 1041 |
return NULL; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
if (cff->header_major > 2) { |
| 1045 |
formatted_warning("cff","major version %u not supported", cff->header_major); |
| 1046 |
cff_close(cff); |
| 1047 |
return NULL; |
| 1048 |
} |
| 1049 |
cff->offset = cff->header_hdr_size; |
| 1050 |
/*tex The name index. */ |
| 1051 |
if (cff->header_major == 2) { |
| 1052 |
cff->name = cff_empty_index(cff); |
| 1053 |
} else { |
| 1054 |
idx = cff_get_index(cff); |
| 1055 |
if (n > idx->count - 1) { |
| 1056 |
normal_warning("cff","invalid fontset index number"); |
| 1057 |
cff_close(cff); |
| 1058 |
return NULL; |
| 1059 |
} |
| 1060 |
cff->name = idx; |
| 1061 |
cff->fontname = cff_get_name(cff); |
| 1062 |
} |
| 1063 |
/*tex The top dict index. */ |
| 1064 |
if (cff->header_major == 2) { |
| 1065 |
/*tex we fake an index (just one entry) */ |
| 1066 |
idx = cff_get_index2(cff); |
| 1067 |
} else { |
| 1068 |
idx = cff_get_index(cff); |
| 1069 |
} |
| 1070 |
if (n > idx->count - 1) { |
| 1071 |
normal_warning("cff","top DICT not exist"); |
| 1072 |
cff_close(cff); |
| 1073 |
return NULL; |
| 1074 |
} |
| 1075 |
cff->topdict = cff_dict_unpack(idx->data + idx->offset[n] - 1, idx->data + idx->offset[n + 1] - 1); |
| 1076 |
if (!cff->topdict) { |
| 1077 |
normal_warning("cff","parsing top DICT data failed"); |
| 1078 |
cff_close(cff); |
| 1079 |
return NULL; |
| 1080 |
} |
| 1081 |
cff_release_index(idx); |
| 1082 |
if (cff_dict_known(cff->topdict, "CharstringType") && |
| 1083 |
cff_dict_get(cff->topdict, "CharstringType", 0) != 2) { |
| 1084 |
normal_warning("cff","only type 2 charstrings supported"); |
| 1085 |
cff_close(cff); |
| 1086 |
return NULL; |
| 1087 |
} |
| 1088 |
if (cff_dict_known(cff->topdict, "SyntheticBase")) { |
| 1089 |
normal_warning("cff","synthetic font not supported"); |
| 1090 |
cff_close(cff); |
| 1091 |
return NULL; |
| 1092 |
} |
| 1093 |
/*tex The string index. */ |
| 1094 |
if (cff->header_major == 2) { |
| 1095 |
/*tex do nothing */ |
| 1096 |
} else { |
| 1097 |
cff->string = cff_get_index(cff); |
| 1098 |
} |
| 1099 |
/*tex The offset to subroutines. */ |
| 1100 |
cff->gsubr_offset = cff->offset; |
| 1101 |
/*tex The number of glyphs. */ |
| 1102 |
offset = (long) cff_dict_get(cff->topdict, "CharStrings", 0); |
| 1103 |
cff->offset = (l_offset) offset; |
| 1104 |
cff->num_glyphs = get_card16(cff); |
| 1105 |
/*tex Check for font type. */ |
| 1106 |
if (cff_dict_known(cff->topdict, "ROS")) { |
| 1107 |
cff->flag |= FONTTYPE_CIDFONT; |
| 1108 |
} else { |
| 1109 |
cff->flag |= FONTTYPE_FONT; |
| 1110 |
} |
| 1111 |
/*tex Check for the encoding. */ |
| 1112 |
if (cff_dict_known(cff->topdict, "Encoding")) { |
| 1113 |
offset = (long) cff_dict_get(cff->topdict, "Encoding", 0); |
| 1114 |
if (offset == 0) { /* predefined */ |
| 1115 |
cff->flag |= ENCODING_STANDARD; |
| 1116 |
} else if (offset == 1) { |
| 1117 |
cff->flag |= ENCODING_EXPERT; |
| 1118 |
} |
| 1119 |
} else { |
| 1120 |
cff->flag |= ENCODING_STANDARD; |
| 1121 |
} |
| 1122 |
cff->offset = cff->gsubr_offset; |
| 1123 |
return cff; |
| 1124 |
} |
| 1125 |
|
| 1126 |
/*tex Write CFF data for an \OPENTYPE\ font. We need to pack dictionary data. */ |
| 1127 |
|
| 1128 |
static long pack_integer(card8 * dest, long destlen, long value) |
| 1129 |
{ |
| 1130 |
long len = 0; |
| 1131 |
if (value >= -107 && value <= 107) { |
| 1132 |
if (destlen < 1) |
| 1133 |
normal_error("cff","buffer overflow (1)"); |
| 1134 |
dest[0] = (card8) ((value + 139) & 0xff); |
| 1135 |
len = 1; |
| 1136 |
} else if (value >= 108 && value <= 1131) { |
| 1137 |
if (destlen < 2) |
| 1138 |
normal_error("cff","buffer overflow (2)"); |
| 1139 |
value = (long) 0xf700u + value - 108; |
| 1140 |
dest[0] = (card8) ((value >> 8) & 0xff); |
| 1141 |
dest[1] = (card8) (value & 0xff); |
| 1142 |
len = 2; |
| 1143 |
} else if (value >= -1131 && value <= -108) { |
| 1144 |
if (destlen < 2) |
| 1145 |
normal_error("cff","buffer overflow (3)"); |
| 1146 |
value = (long) 0xfb00u - value - 108; |
| 1147 |
dest[0] = (card8) ((value >> 8) & 0xff); |
| 1148 |
dest[1] = (card8) (value & 0xff); |
| 1149 |
len = 2; |
| 1150 |
} else if (value >= -32768 && value <= 32767) { |
| 1151 |
/*tex shortint */ |
| 1152 |
if (destlen < 3) |
| 1153 |
normal_error("cff","buffer overflow (4)"); |
| 1154 |
dest[0] = 28; |
| 1155 |
dest[1] = (card8) ((value >> 8) & 0xff); |
| 1156 |
dest[2] = (card8) (value & 0xff); |
| 1157 |
len = 3; |
| 1158 |
} else { |
| 1159 |
/*tex longint */ |
| 1160 |
if (destlen < 5) |
| 1161 |
normal_error("cff","buffer overflow (5)"); |
| 1162 |
dest[0] = 29; |
| 1163 |
dest[1] = (card8) ((value >> 24) & 0xff); |
| 1164 |
dest[2] = (card8) ((value >> 16) & 0xff); |
| 1165 |
dest[3] = (card8) ((value >> 8) & 0xff); |
| 1166 |
dest[4] = (card8) (value & 0xff); |
| 1167 |
len = 5; |
| 1168 |
} |
| 1169 |
return len; |
| 1170 |
} |
| 1171 |
|
| 1172 |
static long pack_real(card8 * dest, long destlen, double value) |
| 1173 |
{ |
| 1174 |
long e; |
| 1175 |
int i = 0, pos = 2; |
| 1176 |
int res; |
| 1177 |
char local_work_buffer[WORK_BUFFER_SIZE]; |
| 1178 |
#define CFF_REAL_MAX_LEN 17 |
| 1179 |
if (destlen < 2) |
| 1180 |
normal_error("cff","buffer overflow (6)"); |
| 1181 |
dest[0] = 30; |
| 1182 |
if (value == 0.0) { |
| 1183 |
dest[1] = 0x0f; |
| 1184 |
return 2; |
| 1185 |
} |
| 1186 |
if (value < 0.0) { |
| 1187 |
dest[1] = 0xe0; |
| 1188 |
value *= -1.0; |
| 1189 |
pos++; |
| 1190 |
} |
| 1191 |
e = 0; |
| 1192 |
if (value >= 10.0) { |
| 1193 |
while (value >= 10.0) { |
| 1194 |
value /= 10.0; |
| 1195 |
e++; |
| 1196 |
} |
| 1197 |
} else if (value < 1.0) { |
| 1198 |
while (value < 1.0) { |
| 1199 |
value *= 10.0; |
| 1200 |
e--; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
res = sprintf(local_work_buffer, "%1.14g", value); |
| 1204 |
if ( (dest>((card8*)(work_buffer))) && (dest-((card8*)work_buffer))<(res+1)) { |
| 1205 |
normal_warning("cff","invalid real value to pack. Continuing, but the font looks wrong."); |
| 1206 |
} |
| 1207 |
if (res<0) |
| 1208 |
normal_error("cff","invalid conversion"); |
| 1209 |
if (res>CFF_REAL_MAX_LEN) |
| 1210 |
res=CFF_REAL_MAX_LEN; |
| 1211 |
for (i = 0; i < res; i++) { |
| 1212 |
unsigned char ch = 0; |
| 1213 |
if (local_work_buffer[i] == '\0') { |
| 1214 |
/*tex In fact |res| should prevent this. */ |
| 1215 |
break; |
| 1216 |
} else if (local_work_buffer[i] == '.') { |
| 1217 |
ch = 0x0a; |
| 1218 |
} else if (local_work_buffer[i] >= '0' && local_work_buffer[i] <= '9') { |
| 1219 |
ch = (unsigned char) (local_work_buffer[i] - '0'); |
| 1220 |
} else { |
| 1221 |
normal_error("cff","invalid character"); |
| 1222 |
} |
| 1223 |
if (destlen < pos / 2 + 1) |
| 1224 |
normal_error("cff","buffer overflow (7)"); |
| 1225 |
|
| 1226 |
if (pos % 2) { |
| 1227 |
dest[pos / 2] = (card8) (dest[pos / 2] + ch); |
| 1228 |
} else { |
| 1229 |
dest[pos / 2] = (card8) (ch << 4); |
| 1230 |
} |
| 1231 |
pos++; |
| 1232 |
} |
| 1233 |
if (e > 0) { |
| 1234 |
if (pos % 2) { |
| 1235 |
dest[pos / 2] = (card8) (dest[pos / 2] + 0x0b); |
| 1236 |
} else { |
| 1237 |
if (destlen < pos / 2 + 1) |
| 1238 |
normal_error("cff","buffer overflow (8)"); |
| 1239 |
dest[pos / 2] = (card8) (0xb0); |
| 1240 |
} |
| 1241 |
pos++; |
| 1242 |
} else if (e < 0) { |
| 1243 |
if (pos % 2) { |
| 1244 |
dest[pos / 2] = (card8) (dest[pos / 2] + 0x0c); |
| 1245 |
} else { |
| 1246 |
if (destlen < pos / 2 + 1) |
| 1247 |
normal_error("cff","buffer overflow (9)"); |
| 1248 |
dest[pos / 2] = (card8) (0xc0); |
| 1249 |
} |
| 1250 |
e *= -1; |
| 1251 |
pos++; |
| 1252 |
} |
| 1253 |
if (e != 0) { |
| 1254 |
sprintf(local_work_buffer, "%ld", e); |
| 1255 |
for (i = 0; i < CFF_REAL_MAX_LEN; i++) { |
| 1256 |
unsigned char ch = 0; |
| 1257 |
if (local_work_buffer[i] == '\0') { |
| 1258 |
break; |
| 1259 |
} else if (local_work_buffer[i] == '.') { |
| 1260 |
ch = 0x0a; |
| 1261 |
} else if (local_work_buffer[i] >= '0' && local_work_buffer[i] <= '9') { |
| 1262 |
ch = (unsigned char) (local_work_buffer[i] - '0'); |
| 1263 |
} else { |
| 1264 |
normal_error("cff","invalid character"); |
| 1265 |
} |
| 1266 |
if (destlen < pos / 2 + 1) |
| 1267 |
normal_error("cff","buffer overflow (10)"); |
| 1268 |
if (pos % 2) { |
| 1269 |
dest[pos / 2] = (card8) (dest[pos / 2] + ch); |
| 1270 |
} else { |
| 1271 |
dest[pos / 2] = (card8) (ch << 4); |
| 1272 |
} |
| 1273 |
pos++; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
if (pos % 2) { |
| 1277 |
dest[pos / 2] = (card8) (dest[pos / 2] + 0x0f); |
| 1278 |
pos++; |
| 1279 |
} else { |
| 1280 |
if (destlen < pos / 2 + 1) |
| 1281 |
normal_error("cff","buffer overflow (11)"); |
| 1282 |
dest[pos / 2] = (card8) (0xff); |
| 1283 |
pos += 2; |
| 1284 |
} |
| 1285 |
return pos / 2; |
| 1286 |
} |
| 1287 |
|
| 1288 |
static long cff_dict_put_number(double value, card8 * dest, long destlen, int type) |
| 1289 |
{ |
| 1290 |
long len = 0; |
| 1291 |
double nearint; |
| 1292 |
nearint = floor(value + 0.5); |
| 1293 |
if (type == CFF_TYPE_OFFSET) { |
| 1294 |
long lvalue; |
| 1295 |
lvalue = (long) value; |
| 1296 |
if (destlen < 5) |
| 1297 |
normal_error("cff","buffer overflow (12)"); |
| 1298 |
dest[0] = 29; |
| 1299 |
dest[1] = (card8) ((lvalue >> 24) & 0xff); |
| 1300 |
dest[2] = (card8) ((lvalue >> 16) & 0xff); |
| 1301 |
dest[3] = (card8) ((lvalue >> 8) & 0xff); |
| 1302 |
dest[4] = (card8) (lvalue & 0xff); |
| 1303 |
len = 5; |
| 1304 |
} else if (value > CFF_INT_MAX || value < CFF_INT_MIN || (fabs(value - nearint) > 1.0e-5)) { |
| 1305 |
/*tex A real */ |
| 1306 |
len = pack_real(dest, destlen, value); |
| 1307 |
} else { |
| 1308 |
/*tex An integer */ |
| 1309 |
len = pack_integer(dest, destlen, (long) nearint); |
| 1310 |
} |
| 1311 |
return len; |
| 1312 |
} |
| 1313 |
|
| 1314 |
static long put_dict_entry(cff_dict_entry * de, card8 * dest, long destlen) |
| 1315 |
{ |
| 1316 |
long len = 0; |
| 1317 |
int i, type, id; |
| 1318 |
if (de->count > 0) { |
| 1319 |
id = de->id; |
| 1320 |
if (dict_operator[id].argtype == CFF_TYPE_OFFSET || |
| 1321 |
dict_operator[id].argtype == CFF_TYPE_SZOFF) { |
| 1322 |
type = CFF_TYPE_OFFSET; |
| 1323 |
} else { |
| 1324 |
type = CFF_TYPE_NUMBER; |
| 1325 |
} |
| 1326 |
for (i = 0; i < de->count; i++) { |
| 1327 |
len += cff_dict_put_number(de->values[i], dest + len, destlen - len, type); |
| 1328 |
} |
| 1329 |
if (id >= 0 && id < CFF_LAST_DICT_OP1) { |
| 1330 |
if (len + 1 > destlen) |
| 1331 |
normal_error("cff","buffer overflow (13)"); |
| 1332 |
dest[len++] = (card8) id; |
| 1333 |
} else if (id >= 0 && id < CFF_LAST_DICT_OP) { |
| 1334 |
if (len + 2 > destlen) |
| 1335 |
normal_error("cff","buffer overflow (14)"); |
| 1336 |
dest[len++] = 12; |
| 1337 |
dest[len++] = (card8) (id - CFF_LAST_DICT_OP1); |
| 1338 |
} else { |
| 1339 |
normal_error("cff","invalid DICT operator ID"); |
| 1340 |
} |
| 1341 |
} |
| 1342 |
return len; |
| 1343 |
} |
| 1344 |
|
| 1345 |
long cff_dict_pack(cff_dict * dict, card8 * dest, long destlen) |
| 1346 |
{ |
| 1347 |
long len = 0; |
| 1348 |
int i; |
| 1349 |
for (i = 0; i < dict->count; i++) { |
| 1350 |
if (!strcmp(dict->entries[i].key, "ROS")) { |
| 1351 |
len += put_dict_entry(&dict->entries[i], dest, destlen); |
| 1352 |
break; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
for (i = 0; i < dict->count; i++) { |
| 1356 |
if (strcmp(dict->entries[i].key, "ROS")) { |
| 1357 |
len += put_dict_entry(&dict->entries[i], dest + len, destlen - len); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
return len; |
| 1361 |
} |
| 1362 |
|
| 1363 |
void cff_dict_add(cff_dict * dict, const char *key, int count) |
| 1364 |
{ |
| 1365 |
int id, i; |
| 1366 |
for (id = 0; id < CFF_LAST_DICT_OP; id++) { |
| 1367 |
if (key && dict_operator[id].opname && |
| 1368 |
strcmp(dict_operator[id].opname, key) == 0) |
| 1369 |
break; |
| 1370 |
} |
| 1371 |
if (id == CFF_LAST_DICT_OP) |
| 1372 |
normal_error("cff","unknown DICT operator"); |
| 1373 |
for (i = 0; i < dict->count; i++) { |
| 1374 |
if ((dict->entries)[i].id == id) { |
| 1375 |
if ((dict->entries)[i].count != count) |
| 1376 |
normal_error("cff","inconsistent DICT argument number"); |
| 1377 |
return; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
if (dict->count + 1 >= dict->max) { |
| 1381 |
dict->max += 8; |
| 1382 |
dict->entries = |
| 1383 |
xrealloc(dict->entries, (unsigned) ((unsigned) dict->max * sizeof(cff_dict_entry))); |
| 1384 |
} |
| 1385 |
(dict->entries)[dict->count].id = id; |
| 1386 |
(dict->entries)[dict->count].key = dict_operator[id].opname; |
| 1387 |
(dict->entries)[dict->count].count = count; |
| 1388 |
if (count > 0) { |
| 1389 |
(dict->entries)[dict->count].values = xcalloc((unsigned) count, sizeof(double)); |
| 1390 |
} else { |
| 1391 |
(dict->entries)[dict->count].values = NULL; |
| 1392 |
} |
| 1393 |
dict->count += 1; |
| 1394 |
return; |
| 1395 |
} |
| 1396 |
|
| 1397 |
void cff_dict_remove(cff_dict * dict, const char *key) |
| 1398 |
{ |
| 1399 |
int i; |
| 1400 |
for (i = 0; i < dict->count; i++) { |
| 1401 |
if (key && strcmp(key, (dict->entries)[i].key) == 0) { |
| 1402 |
(dict->entries)[i].count = 0; |
| 1403 |
xfree((dict->entries)[i].values); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
void cff_dict_set(cff_dict * dict, const char *key, int idx, double value) |
| 1409 |
{ |
| 1410 |
int i; |
| 1411 |
for (i = 0; i < dict->count; i++) { |
| 1412 |
if (strcmp(key, (dict->entries)[i].key) == 0) { |
| 1413 |
if ((dict->entries)[i].count > idx) |
| 1414 |
(dict->entries)[i].values[idx] = value; |
| 1415 |
else |
| 1416 |
normal_error("cff","invalid index number"); |
| 1417 |
break; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
if (i == dict->count) |
| 1421 |
formatted_error("cff","DICT entry '%s' not found", key); |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
/*tex Strings */ |
| 1426 |
|
| 1427 |
char *cff_get_string(cff_font * cff, s_SID id) |
| 1428 |
{ |
| 1429 |
char *result = NULL; |
| 1430 |
size_t len; |
| 1431 |
if (id < CFF_STDSTR_MAX) { |
| 1432 |
len = strlen(cff_stdstr[id]); |
| 1433 |
result = xmalloc((unsigned) (len + 1) * sizeof(char)); |
| 1434 |
memcpy(result, cff_stdstr[id], len); |
| 1435 |
result[len] = '\0'; |
| 1436 |
} else if (cff && cff->string) { |
| 1437 |
cff_index *strings = cff->string; |
| 1438 |
id = (s_SID) (id - CFF_STDSTR_MAX); |
| 1439 |
if (id < strings->count) { |
| 1440 |
len = (strings->offset)[id + 1] - (strings->offset)[id]; |
| 1441 |
result = xmalloc((unsigned) (len + 1) * sizeof(char)); |
| 1442 |
memmove(result, strings->data + (strings->offset)[id] - 1, len); |
| 1443 |
result[len] = '\0'; |
| 1444 |
} |
| 1445 |
} |
| 1446 |
return result; |
| 1447 |
} |
| 1448 |
|
| 1449 |
long cff_get_sid(cff_font * cff, const char *str) |
| 1450 |
{ |
| 1451 |
card16 i; |
| 1452 |
if (!cff || !str) |
| 1453 |
return -1; |
| 1454 |
/*tex We search the string index first. */ |
| 1455 |
if (cff && cff->string) { |
| 1456 |
cff_index *idx = cff->string; |
| 1457 |
for (i = 0; i < idx->count; i++) { |
| 1458 |
if (strlen(str) == (idx->offset)[i + 1] - (idx->offset)[i] && |
| 1459 |
!memcmp(str, (idx->data) + (idx->offset)[i] - 1, strlen(str))) |
| 1460 |
return (i + CFF_STDSTR_MAX); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
for (i = 0; i < CFF_STDSTR_MAX; i++) { |
| 1464 |
if (!strcmp(str, cff_stdstr[i])) |
| 1465 |
return i; |
| 1466 |
} |
| 1467 |
return -1; |
| 1468 |
} |
| 1469 |
|
| 1470 |
void cff_update_string(cff_font * cff) |
| 1471 |
{ |
| 1472 |
if (cff == NULL) |
| 1473 |
normal_error("cff","CFF font not opened"); |
| 1474 |
if (cff->string) |
| 1475 |
cff_release_index(cff->string); |
| 1476 |
cff->string = cff->_string; |
| 1477 |
cff->_string = NULL; |
| 1478 |
} |
| 1479 |
|
| 1480 |
s_SID cff_add_string(cff_font * cff, const char *str) |
| 1481 |
{ |
| 1482 |
card16 idx; |
| 1483 |
cff_index *strings; |
| 1484 |
l_offset offset, size; |
| 1485 |
if (cff == NULL) { |
| 1486 |
normal_error("cff","CFF font not opened"); |
| 1487 |
} |
| 1488 |
if (cff->_string == NULL) { |
| 1489 |
cff->_string = cff_new_index(0); |
| 1490 |
} |
| 1491 |
strings = cff->_string; |
| 1492 |
for (idx = 0; idx < strings->count; idx++) { |
| 1493 |
size = strings->offset[idx + 1] - strings->offset[idx]; |
| 1494 |
offset = strings->offset[idx]; |
| 1495 |
if (size == strlen(str) && !memcmp(strings->data + offset - 1, str, strlen(str))) { |
| 1496 |
return (s_SID) (idx + CFF_STDSTR_MAX); |
| 1497 |
} |
| 1498 |
} |
| 1499 |
for (idx = 0; idx < CFF_STDSTR_MAX; idx++) { |
| 1500 |
if (cff_stdstr[idx] && !strcmp(cff_stdstr[idx], str)) { |
| 1501 |
return idx; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
offset = (strings->count > 0) ? strings->offset[strings->count] : 1; |
| 1505 |
strings->offset = xrealloc(strings->offset, (unsigned) (((unsigned) strings->count + 2) * sizeof(l_offset))); |
| 1506 |
if (strings->count == 0) |
| 1507 |
strings->offset[0] = 1; |
| 1508 |
idx = strings->count; |
| 1509 |
strings->count = (card16) (strings->count + 1); |
| 1510 |
strings->offset[strings->count] = offset + strlen(str); |
| 1511 |
strings->data = xrealloc(strings->data, (unsigned) ((offset + strlen(str) - 1) * sizeof(card8))); |
| 1512 |
memcpy(strings->data + offset - 1, str, strlen(str)); |
| 1513 |
return (s_SID) (idx + CFF_STDSTR_MAX); |
| 1514 |
} |
| 1515 |
|
| 1516 |
void cff_dict_update(cff_dict * dict, cff_font * cff) |
| 1517 |
{ |
| 1518 |
int i; |
| 1519 |
for (i = 0; i < dict->count; i++) { |
| 1520 |
if ((dict->entries)[i].count > 0) { |
| 1521 |
char *str; |
| 1522 |
int id; |
| 1523 |
id = (dict->entries)[i].id; |
| 1524 |
if (dict_operator[id].argtype == CFF_TYPE_SID) { |
| 1525 |
str = cff_get_string(cff, (s_SID) (dict->entries)[i].values[0]); |
| 1526 |
if (str != NULL) { |
| 1527 |
(dict->entries)[i].values[0] = cff_add_string(cff, str); |
| 1528 |
xfree(str); |
| 1529 |
} |
| 1530 |
} else if (dict_operator[id].argtype == CFF_TYPE_ROS) { |
| 1531 |
str = cff_get_string(cff, (s_SID) (dict->entries)[i].values[0]); |
| 1532 |
if (str != NULL) { |
| 1533 |
(dict->entries)[i].values[0] = cff_add_string(cff, str); |
| 1534 |
xfree(str); |
| 1535 |
} |
| 1536 |
str = cff_get_string(cff, (s_SID) (dict->entries)[i].values[1]); |
| 1537 |
if (str != NULL) { |
| 1538 |
(dict->entries)[i].values[1] = cff_add_string(cff, str); |
| 1539 |
xfree(str); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
} |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
/*tex The charsets. */ |
| 1547 |
|
| 1548 |
long cff_read_charsets(cff_font * cff) |
| 1549 |
{ |
| 1550 |
cff_charsets *charset; |
| 1551 |
long offset, length; |
| 1552 |
card16 count, i; |
| 1553 |
if (cff->topdict == NULL) |
| 1554 |
normal_error("cff","top DICT not available"); |
| 1555 |
if (!cff_dict_known(cff->topdict, "charset")) { |
| 1556 |
cff->flag |= CHARSETS_ISOADOBE; |
| 1557 |
cff->charsets = NULL; |
| 1558 |
return 0; |
| 1559 |
} |
| 1560 |
offset = (long) cff_dict_get(cff->topdict, "charset", 0); |
| 1561 |
if (offset == 0) { |
| 1562 |
/*tex predefined */ |
| 1563 |
cff->flag |= CHARSETS_ISOADOBE; |
| 1564 |
cff->charsets = NULL; |
| 1565 |
return 0; |
| 1566 |
} else if (offset == 1) { |
| 1567 |
cff->flag |= CHARSETS_EXPERT; |
| 1568 |
cff->charsets = NULL; |
| 1569 |
return 0; |
| 1570 |
} else if (offset == 2) { |
| 1571 |
cff->flag |= CHARSETS_EXPSUB; |
| 1572 |
cff->charsets = NULL; |
| 1573 |
return 0; |
| 1574 |
} |
| 1575 |
cff->offset = (l_offset) offset; |
| 1576 |
cff->charsets = charset = xcalloc(1, sizeof(cff_charsets)); |
| 1577 |
charset->format = get_card8(cff); |
| 1578 |
charset->num_entries = 0; |
| 1579 |
count = (card16) (cff->num_glyphs - 1); |
| 1580 |
length = 1; |
| 1581 |
/*tex Not well documented. */ |
| 1582 |
switch (charset->format) { |
| 1583 |
case 0: |
| 1584 |
charset->num_entries = (card16) (cff->num_glyphs - 1); /* no .notdef */ |
| 1585 |
charset->data.glyphs = |
| 1586 |
xmalloc((unsigned) (charset->num_entries * sizeof(s_SID))); |
| 1587 |
length += (charset->num_entries) * 2; |
| 1588 |
for (i = 0; i < (charset->num_entries); i++) { |
| 1589 |
charset->data.glyphs[i] = get_card16(cff); |
| 1590 |
} |
| 1591 |
count = 0; |
| 1592 |
break; |
| 1593 |
case 1: |
| 1594 |
{ |
| 1595 |
cff_range1 *ranges = NULL; |
| 1596 |
while (count > 0 && charset->num_entries < cff->num_glyphs) { |
| 1597 |
ranges = |
| 1598 |
xrealloc(ranges, |
| 1599 |
(unsigned) (((unsigned) charset->num_entries + |
| 1600 |
1) * sizeof(cff_range1))); |
| 1601 |
ranges[charset->num_entries].first = get_card16(cff); |
| 1602 |
ranges[charset->num_entries].n_left = get_card8(cff); |
| 1603 |
count = (card16) (count - ranges[charset->num_entries].n_left + 1); /* no-overrap */ |
| 1604 |
charset->num_entries++; |
| 1605 |
charset->data.range1 = ranges; |
| 1606 |
} |
| 1607 |
length += (charset->num_entries) * 3; |
| 1608 |
} |
| 1609 |
break; |
| 1610 |
case 2: |
| 1611 |
{ |
| 1612 |
cff_range2 *ranges = NULL; |
| 1613 |
while (count > 0 && charset->num_entries < cff->num_glyphs) { |
| 1614 |
ranges = |
| 1615 |
xrealloc(ranges, |
| 1616 |
(unsigned) (((unsigned) charset->num_entries + |
| 1617 |
1) * sizeof(cff_range2))); |
| 1618 |
ranges[charset->num_entries].first = get_card16(cff); |
| 1619 |
ranges[charset->num_entries].n_left = get_card16(cff); |
| 1620 |
count = (card16) (count - (ranges[charset->num_entries].n_left + 1)); /* non-overrapping */ |
| 1621 |
charset->num_entries++; |
| 1622 |
} |
| 1623 |
charset->data.range2 = ranges; |
| 1624 |
length += (charset->num_entries) * 4; |
| 1625 |
} |
| 1626 |
break; |
| 1627 |
default: |
| 1628 |
xfree(charset); |
| 1629 |
normal_error("cff","unknown charset format"); |
| 1630 |
break; |
| 1631 |
} |
| 1632 |
if (count > 0) { |
| 1633 |
normal_warning("cff","charset data possibly broken (too many glyphs)"); |
| 1634 |
} |
| 1635 |
return length; |
| 1636 |
} |
| 1637 |
|
| 1638 |
long cff_pack_charsets(cff_font * cff, card8 * dest, long destlen) |
| 1639 |
{ |
| 1640 |
long len = 0; |
| 1641 |
card16 i; |
| 1642 |
cff_charsets *charset; |
| 1643 |
if (cff->flag & HAVE_STANDARD_CHARSETS || cff->charsets == NULL) |
| 1644 |
return 0; |
| 1645 |
if (destlen < 1) |
| 1646 |
normal_error("cff","buffer overflow (15)"); |
| 1647 |
charset = cff->charsets; |
| 1648 |
dest[len++] = charset->format; |
| 1649 |
switch (charset->format) { |
| 1650 |
case 0: |
| 1651 |
if (destlen < len + (charset->num_entries) * 2) |
| 1652 |
normal_error("cff","buffer overflow (16)"); |
| 1653 |
for (i = 0; i < (charset->num_entries); i++) { |
| 1654 |
s_SID sid = (charset->data).glyphs[i]; /* or CID */ |
| 1655 |
dest[len++] = (card8) ((sid >> 8) & 0xff); |
| 1656 |
dest[len++] = (card8) (sid & 0xff); |
| 1657 |
} |
| 1658 |
break; |
| 1659 |
case 1: |
| 1660 |
{ |
| 1661 |
if (destlen < len + (charset->num_entries) * 3) |
| 1662 |
normal_error("cff","buffer overflow (17)"); |
| 1663 |
for (i = 0; i < (charset->num_entries); i++) { |
| 1664 |
dest[len++] = (card8) (((charset->data).range1[i].first >> 8) & 0xff); |
| 1665 |
dest[len++] = (card8) ((charset->data).range1[i].first & 0xff); |
| 1666 |
dest[len++] = (card8) ((charset->data).range1[i].n_left); |
| 1667 |
} |
| 1668 |
} |
| 1669 |
break; |
| 1670 |
case 2: |
| 1671 |
{ |
| 1672 |
if (destlen < len + (charset->num_entries) * 4) |
| 1673 |
normal_error("cff","buffer overflow (18)"); |
| 1674 |
for (i = 0; i < (charset->num_entries); i++) { |
| 1675 |
dest[len++] = (card8) (((charset->data).range2[i].first >> 8) & 0xff); |
| 1676 |
dest[len++] = (card8) ((charset->data).range2[i].first & 0xff); |
| 1677 |
dest[len++] = (card8) (((charset->data).range2[i].n_left >> 8) & 0xff); |
| 1678 |
dest[len++] = (card8) ((charset->data).range2[i].n_left & 0xff); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
break; |
| 1682 |
default: |
| 1683 |
normal_error("cff","unknown charset format"); |
| 1684 |
break; |
| 1685 |
} |
| 1686 |
return len; |
| 1687 |
} |
| 1688 |
|
| 1689 |
/*tex |
| 1690 |
|
| 1691 |
Here we decode and encode Type 2 charstring. All local/global subroutine |
| 1692 |
calls in a given charstring is replace by the content of subroutine |
| 1693 |
charstrings. We do this because some PostScript RIP may have problems with |
| 1694 |
sparse subroutine array. Workaround for this is to re-order subroutine array |
| 1695 |
so that no gap appears in the subroutine array, or put dummy charstrings that |
| 1696 |
contains only `return' in the gap. However, re-ordering of subroutine is |
| 1697 |
rather difficult for Type 2 charstrings due to the bias which depends on the |
| 1698 |
total number of subroutines. Replacing callgsubr/callsubr calls with the |
| 1699 |
content of the corresponding subroutine charstring may be more efficient than |
| 1700 |
putting dummy subroutines in the case of subsetted font. Adobe distiller |
| 1701 |
seems doing same thing. |
| 1702 |
|
| 1703 |
And also note that subroutine numbers within subroutines can depend on the |
| 1704 |
content of operand stack as follows: |
| 1705 |
|
| 1706 |
\startyping |
| 1707 |
\.{ ... l m callsubr << subr \#(m+bias): n add callsubr >> ...} |
| 1708 |
\stoptyping |
| 1709 |
|
| 1710 |
I've not implemented the `random' operator which generates a pseudo-random |
| 1711 |
number in the range (0, 1] and push them into argument stack. How |
| 1712 |
pseudo-random sequences are generated is not documented in the Type 2 |
| 1713 |
charstring spec. |
| 1714 |
|
| 1715 |
*/ |
| 1716 |
|
| 1717 |
#define CS_TYPE2_DEBUG_STR "Type2 Charstring Parser" |
| 1718 |
#define CS_TYPE2_DEBUG 5 |
| 1719 |
|
| 1720 |
#define CS_BUFFER_CFF_ERROR -3 |
| 1721 |
#define CS_STACK_CFF_ERROR -2 |
| 1722 |
#define CS_PARSE_CFF_ERROR -1 |
| 1723 |
#define CS_PARSE_OK 0 |
| 1724 |
#define CS_PARSE_END 1 |
| 1725 |
#define CS_SUBR_RETURN 2 |
| 1726 |
#define CS_CHAR_END 3 |
| 1727 |
|
| 1728 |
static int status = CS_PARSE_CFF_ERROR; |
| 1729 |
|
| 1730 |
#define DST_NEED(a,b) {if ((a) < (b)) { status = CS_BUFFER_CFF_ERROR ; return ; }} |
| 1731 |
#define SRC_NEED(a,b) {if ((a) < (b)) { status = CS_PARSE_CFF_ERROR ; return ; }} |
| 1732 |
#define NEED(a,b) {if ((a) < (b)) { status = CS_STACK_CFF_ERROR ; return ; }} |
| 1733 |
|
| 1734 |
/*tex The hintmask and cntrmask need the number of stem zones. */ |
| 1735 |
|
| 1736 |
static int num_stems = 0; |
| 1737 |
static int phase = 0; |
| 1738 |
|
| 1739 |
/*tex Subroutine nesting. |
| 1740 |
*/ |
| 1741 |
static int cs2_nest = 0; |
| 1742 |
|
| 1743 |
/*tex The advance width. */ |
| 1744 |
|
| 1745 |
static int have_width = 0; |
| 1746 |
static double width = 0.0; |
| 1747 |
|
| 1748 |
/*tex |
| 1749 |
|
| 1750 |
Standard Encoding Accented Characters: Optional four arguments for endchar. |
| 1751 |
See, CFF spec., p.35. This is obsolete feature and is no longer supported. |
| 1752 |
|
| 1753 |
\starttyping |
| 1754 |
static double seac[4] = { 0.0, 0.0, 0.0, 0.0 }; // gone |
| 1755 |
\stoptyping |
| 1756 |
|
| 1757 |
*/ |
| 1758 |
|
| 1759 |
/*tex Operand stack and Transient array */ |
| 1760 |
|
| 1761 |
static int cs2_stack_top = 0; |
| 1762 |
static double cs2_arg_stack[CS_ARG_STACK_MAX]; |
| 1763 |
static double trn_array[CS_TRANS_ARRAY_MAX]; |
| 1764 |
|
| 1765 |
/*tex |
| 1766 |
|
| 1767 |
Type 2 CharString encoding, first the 1 byte operators: |
| 1768 |
|
| 1769 |
*/ |
| 1770 |
|
| 1771 |
/* RESERVED 0 */ |
| 1772 |
#define cs_hstem 1 |
| 1773 |
/* RESERVED 2 */ |
| 1774 |
#define cs_vstem 3 |
| 1775 |
#define cs_vmoveto 4 |
| 1776 |
#define cs_rlineto 5 |
| 1777 |
#define cs_hlineto 6 |
| 1778 |
#define cs_vlineto 7 |
| 1779 |
#define cs_rrcurveto 8 |
| 1780 |
/* cs_closepath 9 */ |
| 1781 |
#define cs_callsubr 10 |
| 1782 |
#define cs_return 11 |
| 1783 |
#define cs_escape 12 |
| 1784 |
/* cs_hsbw 13 */ |
| 1785 |
#define cs_endchar 14 |
| 1786 |
#define cs_setvsindex 15 |
| 1787 |
#define cs_blend 16 |
| 1788 |
/* RESERVED 17 */ |
| 1789 |
#define cs_hstemhm 18 |
| 1790 |
#define cs_hintmask 19 |
| 1791 |
#define cs_cntrmask 20 |
| 1792 |
#define cs_rmoveto 21 |
| 1793 |
#define cs_hmoveto 22 |
| 1794 |
#define cs_vstemhm 23 |
| 1795 |
#define cs_rcurveline 24 |
| 1796 |
#define cs_rlinecurve 25 |
| 1797 |
#define cs_vvcurveto 26 |
| 1798 |
#define cs_hhcurveto 27 |
| 1799 |
/* SHORTINT 28 */ |
| 1800 |
#define cs_callgsubr 29 |
| 1801 |
#define cs_vhcurveto 30 |
| 1802 |
#define cs_hvcurveto 31 |
| 1803 |
|
| 1804 |
/*tex |
| 1805 |
|
| 1806 |
Next the two byte CharString operators: |
| 1807 |
|
| 1808 |
*/ |
| 1809 |
|
| 1810 |
#define cs_dotsection 0 |
| 1811 |
/* cs_vstem3 1 */ |
| 1812 |
/* cs_hstem3 2 */ |
| 1813 |
#define cs_and 3 |
| 1814 |
#define cs_or 4 |
| 1815 |
#define cs_not 5 |
| 1816 |
/* cs_seac 6 */ |
| 1817 |
/* cs_sbw 7 */ |
| 1818 |
/* RESERVED 8 */ |
| 1819 |
#define cs_abs 9 |
| 1820 |
#define cs_add 10 |
| 1821 |
#define cs_sub 11 |
| 1822 |
#define cs_div 12 |
| 1823 |
/* RESERVED 13 */ |
| 1824 |
#define cs_neg 14 |
| 1825 |
#define cs_eq 15 |
| 1826 |
/* cs_callothersubr 16 */ |
| 1827 |
/* cs_pop| 17 */ |
| 1828 |
#define cs_drop 18 |
| 1829 |
/* RESERVED 19 */ |
| 1830 |
#define cs_put 20 |
| 1831 |
#define cs_get 21 |
| 1832 |
#define cs_ifelse 22 |
| 1833 |
#define cs_random 23 |
| 1834 |
#define cs_mul 24 |
| 1835 |
/* RESERVED 25 */ |
| 1836 |
#define cs_sqrt 26 |
| 1837 |
#define cs_dup 27 |
| 1838 |
#define cs_exch 28 |
| 1839 |
#define cs_index 29 |
| 1840 |
#define cs_roll 30 |
| 1841 |
/* cs_setcurrentpoint 31 */ |
| 1842 |
/* RESERVED 32 */ |
| 1843 |
/* RESERVED 33 */ |
| 1844 |
#define cs_hflex 34 |
| 1845 |
#define cs_flex 35 |
| 1846 |
#define cs_hflex1 36 |
| 1847 |
#define cs_flex1 37 |
| 1848 |
|
| 1849 |
/*tex |clear_stack| put all operands sotred in operand stack to dest. */ |
| 1850 |
|
| 1851 |
static void clear_stack(card8 ** dest, card8 * limit) |
| 1852 |
{ |
| 1853 |
int i; |
| 1854 |
for (i = 0; i < cs2_stack_top; i++) { |
| 1855 |
double value; |
| 1856 |
long ivalue; |
| 1857 |
value = cs2_arg_stack[i]; |
| 1858 |
/*tex The nearest integer value. */ |
| 1859 |
ivalue = (long) floor(value + 0.5); |
| 1860 |
if (value >= 0x8000L || value <= (-0x8000L - 1)) { |
| 1861 |
/*tex |
| 1862 |
This number cannot be represented as a single operand. We must |
| 1863 |
use |a b mul ...| or |a c div| to represent large values. |
| 1864 |
*/ |
| 1865 |
normal_error("cff","argument value too large (this is bug)"); |
| 1866 |
} else if (fabs(value - (double) ivalue) > 3.0e-5) { |
| 1867 |
/*tex A 16.16-bit signed fixed value */ |
| 1868 |
DST_NEED(limit, *dest + 5); |
| 1869 |
*(*dest)++ = 255; |
| 1870 |
/*tex The mantissa. */ |
| 1871 |
ivalue = (long) floor(value); |
| 1872 |
*(*dest)++ = (card8) ((ivalue >> 8) & 0xff); |
| 1873 |
*(*dest)++ = (card8) (ivalue & 0xff); |
| 1874 |
/*tex The fraction. */ |
| 1875 |
ivalue = (long) ((value - (double) ivalue) * 0x10000l); |
| 1876 |
*(*dest)++ = (card8) ((ivalue >> 8) & 0xff); |
| 1877 |
*(*dest)++ = (card8) (ivalue & 0xff); |
| 1878 |
/*tex Everything else is integer. */ |
| 1879 |
} else if (ivalue >= -107 && ivalue <= 107) { |
| 1880 |
DST_NEED(limit, *dest + 1); |
| 1881 |
*(*dest)++ = (card8) (ivalue + 139); |
| 1882 |
} else if (ivalue >= 108 && ivalue <= 1131) { |
| 1883 |
DST_NEED(limit, *dest + 2); |
| 1884 |
ivalue = (long) 0xf700u + ivalue - 108; |
| 1885 |
*(*dest)++ = (card8) ((ivalue >> 8) & 0xff); |
| 1886 |
*(*dest)++ = (card8) (ivalue & 0xff); |
| 1887 |
} else if (ivalue >= -1131 && ivalue <= -108) { |
| 1888 |
DST_NEED(limit, *dest + 2); |
| 1889 |
ivalue = (long) 0xfb00u - ivalue - 108; |
| 1890 |
*(*dest)++ = (card8) ((ivalue >> 8) & 0xff); |
| 1891 |
*(*dest)++ = (card8) (ivalue & 0xff); |
| 1892 |
} else if (ivalue >= -32768 && ivalue <= 32767) { |
| 1893 |
/*tex A shortint. */ |
| 1894 |
DST_NEED(limit, *dest + 3); |
| 1895 |
*(*dest)++ = 28; |
| 1896 |
*(*dest)++ = (card8) ((ivalue >> 8) & 0xff); |
| 1897 |
*(*dest)++ = (card8) ((ivalue) & 0xff); |
| 1898 |
} else { |
| 1899 |
normal_error("cff","unexpected error"); |
| 1900 |
} |
| 1901 |
} |
| 1902 |
/*tex Clear the stack. */ |
| 1903 |
cs2_stack_top = 0; |
| 1904 |
return; |
| 1905 |
} |
| 1906 |
|
| 1907 |
/*tex |
| 1908 |
Single byte operators: Path construction, Operator for finishing a path, Hint |
| 1909 |
operators. Phases: |
| 1910 |
|
| 1911 |
\starttabulate |
| 1912 |
\NC \type{0} \NC inital state \NC \NR |
| 1913 |
\NC \type{1} \NC hint declaration, first stack-clearing operator appeared \NC \NR |
| 1914 |
\NC \type{2} \NC in path construction \NC \NR |
| 1915 |
\stoptabulate |
| 1916 |
|
| 1917 |
*/ |
| 1918 |
|
| 1919 |
static void do_operator1(card8 ** dest, card8 * limit, card8 ** data, card8 * endptr) |
| 1920 |
{ |
| 1921 |
card8 op = **data; |
| 1922 |
|
| 1923 |
*data += 1; |
| 1924 |
|
| 1925 |
switch (op) { |
| 1926 |
case cs_hstemhm: |
| 1927 |
case cs_vstemhm: |
| 1928 |
/*tex A charstring may have a hintmask if the above operator has been seen. */ |
| 1929 |
case cs_hstem: |
| 1930 |
case cs_vstem: |
| 1931 |
if (phase == 0 && (cs2_stack_top % 2)) { |
| 1932 |
have_width = 1; |
| 1933 |
width = cs2_arg_stack[0]; |
| 1934 |
} |
| 1935 |
num_stems += cs2_stack_top / 2; |
| 1936 |
clear_stack(dest, limit); |
| 1937 |
DST_NEED(limit, *dest + 1); |
| 1938 |
*(*dest)++ = op; |
| 1939 |
phase = 1; |
| 1940 |
break; |
| 1941 |
case cs_hintmask: |
| 1942 |
case cs_cntrmask: |
| 1943 |
if (phase < 2) { |
| 1944 |
if (phase == 0 && (cs2_stack_top % 2)) { |
| 1945 |
have_width = 1; |
| 1946 |
width = cs2_arg_stack[0]; |
| 1947 |
} |
| 1948 |
num_stems += cs2_stack_top / 2; |
| 1949 |
} |
| 1950 |
clear_stack(dest, limit); |
| 1951 |
DST_NEED(limit, *dest + 1); |
| 1952 |
*(*dest)++ = op; |
| 1953 |
if (num_stems > 0) { |
| 1954 |
int masklen = (num_stems + 7) / 8; |
| 1955 |
DST_NEED(limit, *dest + masklen); |
| 1956 |
SRC_NEED(endptr, *data + masklen); |
| 1957 |
memmove(*dest, *data, (size_t) masklen); |
| 1958 |
*data += masklen; |
| 1959 |
*dest += masklen; |
| 1960 |
} |
| 1961 |
phase = 2; |
| 1962 |
break; |
| 1963 |
case cs_rmoveto: |
| 1964 |
if (phase == 0 && (cs2_stack_top % 2)) { |
| 1965 |
have_width = 1; |
| 1966 |
width = cs2_arg_stack[0]; |
| 1967 |
} |
| 1968 |
clear_stack(dest, limit); |
| 1969 |
DST_NEED(limit, *dest + 1); |
| 1970 |
*(*dest)++ = op; |
| 1971 |
phase = 2; |
| 1972 |
break; |
| 1973 |
case cs_hmoveto: |
| 1974 |
case cs_vmoveto: |
| 1975 |
if (phase == 0 && (cs2_stack_top % 2) == 0) { |
| 1976 |
have_width = 1; |
| 1977 |
width = cs2_arg_stack[0]; |
| 1978 |
} |
| 1979 |
clear_stack(dest, limit); |
| 1980 |
DST_NEED(limit, *dest + 1); |
| 1981 |
*(*dest)++ = op; |
| 1982 |
phase = 2; |
| 1983 |
break; |
| 1984 |
case cs_endchar: |
| 1985 |
if (cs2_stack_top == 1) { |
| 1986 |
have_width = 1; |
| 1987 |
width = cs2_arg_stack[0]; |
| 1988 |
clear_stack(dest, limit); |
| 1989 |
} else if (cs2_stack_top == 4 || cs2_stack_top == 5) { |
| 1990 |
normal_warning("cff","'seac' character deprecated in type 2 charstring"); |
| 1991 |
status = CS_PARSE_CFF_ERROR; |
| 1992 |
return; |
| 1993 |
} else if (cs2_stack_top > 0) { |
| 1994 |
normal_warning("cff","operand stack not empty"); |
| 1995 |
} |
| 1996 |
DST_NEED(limit, *dest + 1); |
| 1997 |
*(*dest)++ = op; |
| 1998 |
status = CS_CHAR_END; |
| 1999 |
break; |
| 2000 |
/*tex The above operators are candidate for first stack clearing operator. */ |
| 2001 |
case cs_setvsindex: |
| 2002 |
/* |
| 2003 |
vsindex = cs2_arg_stack[cs2_stack_top-1]; |
| 2004 |
cs2_stack_top -= 1; |
| 2005 |
*/ |
| 2006 |
normal_warning("cff2","unsupported setvindex operator"); |
| 2007 |
status = CS_PARSE_CFF_ERROR; |
| 2008 |
break; |
| 2009 |
case cs_blend: |
| 2010 |
/* |
| 2011 |
blends = cs2_arg_stack[cs2_stack_top-1]; |
| 2012 |
cs2_stack_top -= 1; |
| 2013 |
cs2_stack_top -= blends * regions ; |
| 2014 |
*/ |
| 2015 |
normal_warning("cff2","unsupported blend operator"); |
| 2016 |
status = CS_PARSE_CFF_ERROR; |
| 2017 |
break; |
| 2018 |
case cs_rlineto: |
| 2019 |
case cs_hlineto: |
| 2020 |
case cs_vlineto: |
| 2021 |
case cs_rrcurveto: |
| 2022 |
case cs_rcurveline: |
| 2023 |
case cs_rlinecurve: |
| 2024 |
case cs_vvcurveto: |
| 2025 |
case cs_hhcurveto: |
| 2026 |
case cs_vhcurveto: |
| 2027 |
case cs_hvcurveto: |
| 2028 |
if (phase < 2) { |
| 2029 |
normal_warning("cff","broken type 2 charstring"); |
| 2030 |
status = CS_PARSE_CFF_ERROR; |
| 2031 |
return; |
| 2032 |
} |
| 2033 |
clear_stack(dest, limit); |
| 2034 |
DST_NEED(limit, *dest + 1); |
| 2035 |
*(*dest)++ = op; |
| 2036 |
break; |
| 2037 |
/*tex All the operotors above are stack clearing. */ |
| 2038 |
case cs_return: |
| 2039 |
normal_error("cff","unexpected return"); |
| 2040 |
case cs_callgsubr: |
| 2041 |
normal_error("cff","unexpected callgsubr"); |
| 2042 |
case cs_callsubr: |
| 2043 |
normal_error("cff","unexpected callsubr"); |
| 2044 |
break; |
| 2045 |
default: |
| 2046 |
formatted_warning("cff","%s: unknown charstring operator: 0x%02x", CS_TYPE2_DEBUG_STR, op); |
| 2047 |
status = CS_PARSE_CFF_ERROR; |
| 2048 |
break; |
| 2049 |
} |
| 2050 |
return; |
| 2051 |
} |
| 2052 |
|
| 2053 |
/*tex |
| 2054 |
|
| 2055 |
Double byte operators: Flex, arithmetic, conditional, and storage operators. |
| 2056 |
The following operators are not supported: random but How random ? |
| 2057 |
|
| 2058 |
*/ |
| 2059 |
|
| 2060 |
static void do_operator2(card8 ** dest, card8 * limit, card8 ** data, card8 * endptr) |
| 2061 |
{ |
| 2062 |
card8 op; |
| 2063 |
*data += 1; |
| 2064 |
SRC_NEED(endptr, *data + 1); |
| 2065 |
op = **data; |
| 2066 |
*data += 1; |
| 2067 |
switch (op) { |
| 2068 |
case cs_dotsection: |
| 2069 |
normal_warning("cff","Operator 'dotsection' deprecated in type 2 charstring"); |
| 2070 |
status = CS_PARSE_CFF_ERROR; |
| 2071 |
return; |
| 2072 |
break; |
| 2073 |
case cs_hflex: |
| 2074 |
case cs_flex: |
| 2075 |
case cs_hflex1: |
| 2076 |
case cs_flex1: |
| 2077 |
if (phase < 2) { |
| 2078 |
formatted_warning("cff","%s: broken type 2 charstring", CS_TYPE2_DEBUG_STR); |
| 2079 |
status = CS_PARSE_CFF_ERROR; |
| 2080 |
return; |
| 2081 |
} |
| 2082 |
clear_stack(dest, limit); |
| 2083 |
DST_NEED(limit, *dest + 2); |
| 2084 |
*(*dest)++ = cs_escape; |
| 2085 |
*(*dest)++ = op; |
| 2086 |
break; |
| 2087 |
/*tex All operators above are stack clearing. */ |
| 2088 |
case cs_and: |
| 2089 |
NEED(cs2_stack_top, 2); |
| 2090 |
cs2_stack_top--; |
| 2091 |
if (cs2_arg_stack[cs2_stack_top] && cs2_arg_stack[cs2_stack_top - 1]) { |
| 2092 |
cs2_arg_stack[cs2_stack_top - 1] = 1.0; |
| 2093 |
} else { |
| 2094 |
cs2_arg_stack[cs2_stack_top - 1] = 0.0; |
| 2095 |
} |
| 2096 |
break; |
| 2097 |
case cs_or: |
| 2098 |
NEED(cs2_stack_top, 2); |
| 2099 |
cs2_stack_top--; |
| 2100 |
if (cs2_arg_stack[cs2_stack_top] || cs2_arg_stack[cs2_stack_top - 1]) { |
| 2101 |
cs2_arg_stack[cs2_stack_top - 1] = 1.0; |
| 2102 |
} else { |
| 2103 |
cs2_arg_stack[cs2_stack_top - 1] = 0.0; |
| 2104 |
} |
| 2105 |
break; |
| 2106 |
case cs_not: |
| 2107 |
NEED(cs2_stack_top, 1); |
| 2108 |
if (cs2_arg_stack[cs2_stack_top - 1]) { |
| 2109 |
cs2_arg_stack[cs2_stack_top - 1] = 0.0; |
| 2110 |
} else { |
| 2111 |
cs2_arg_stack[cs2_stack_top - 1] = 1.0; |
| 2112 |
} |
| 2113 |
break; |
| 2114 |
case cs_abs: |
| 2115 |
NEED(cs2_stack_top, 1); |
| 2116 |
cs2_arg_stack[cs2_stack_top - 1] = |
| 2117 |
fabs(cs2_arg_stack[cs2_stack_top - 1]); |
| 2118 |
break; |
| 2119 |
case cs_add: |
| 2120 |
NEED(cs2_stack_top, 2); |
| 2121 |
cs2_arg_stack[cs2_stack_top - 2] += cs2_arg_stack[cs2_stack_top - 1]; |
| 2122 |
cs2_stack_top--; |
| 2123 |
break; |
| 2124 |
case cs_sub: |
| 2125 |
NEED(cs2_stack_top, 2); |
| 2126 |
cs2_arg_stack[cs2_stack_top - 2] -= cs2_arg_stack[cs2_stack_top - 1]; |
| 2127 |
cs2_stack_top--; |
| 2128 |
break; |
| 2129 |
case cs_div: |
| 2130 |
NEED(cs2_stack_top, 2); |
| 2131 |
cs2_arg_stack[cs2_stack_top - 2] /= cs2_arg_stack[cs2_stack_top - 1]; |
| 2132 |
cs2_stack_top--; |
| 2133 |
break; |
| 2134 |
case cs_neg: |
| 2135 |
NEED(cs2_stack_top, 1); |
| 2136 |
cs2_arg_stack[cs2_stack_top - 1] *= -1.0; |
| 2137 |
break; |
| 2138 |
case cs_eq: |
| 2139 |
NEED(cs2_stack_top, 2); |
| 2140 |
cs2_stack_top--; |
| 2141 |
if (cs2_arg_stack[cs2_stack_top] == cs2_arg_stack[cs2_stack_top - 1]) { |
| 2142 |
cs2_arg_stack[cs2_stack_top - 1] = 1.0; |
| 2143 |
} else { |
| 2144 |
cs2_arg_stack[cs2_stack_top - 1] = 0.0; |
| 2145 |
} |
| 2146 |
break; |
| 2147 |
case cs_drop: |
| 2148 |
NEED(cs2_stack_top, 1); |
| 2149 |
cs2_stack_top--; |
| 2150 |
break; |
| 2151 |
case cs_put: |
| 2152 |
NEED(cs2_stack_top, 2); |
| 2153 |
{ |
| 2154 |
int idx = (int) cs2_arg_stack[--cs2_stack_top]; |
| 2155 |
NEED(CS_TRANS_ARRAY_MAX, idx); |
| 2156 |
trn_array[idx] = cs2_arg_stack[--cs2_stack_top]; |
| 2157 |
} |
| 2158 |
break; |
| 2159 |
case cs_get: |
| 2160 |
NEED(cs2_stack_top, 1); |
| 2161 |
{ |
| 2162 |
int idx = (int) cs2_arg_stack[cs2_stack_top - 1]; |
| 2163 |
NEED(CS_TRANS_ARRAY_MAX, idx); |
| 2164 |
cs2_arg_stack[cs2_stack_top - 1] = trn_array[idx]; |
| 2165 |
} |
| 2166 |
break; |
| 2167 |
case cs_ifelse: |
| 2168 |
NEED(cs2_stack_top, 4); |
| 2169 |
cs2_stack_top -= 3; |
| 2170 |
if (cs2_arg_stack[cs2_stack_top + 1] > cs2_arg_stack[cs2_stack_top + 2]) { |
| 2171 |
cs2_arg_stack[cs2_stack_top - 1] = cs2_arg_stack[cs2_stack_top]; |
| 2172 |
} |
| 2173 |
break; |
| 2174 |
case cs_mul: |
| 2175 |
NEED(cs2_stack_top, 2); |
| 2176 |
cs2_arg_stack[cs2_stack_top - 2] = |
| 2177 |
cs2_arg_stack[cs2_stack_top - 2] * cs2_arg_stack[cs2_stack_top - 1]; |
| 2178 |
cs2_stack_top--; |
| 2179 |
break; |
| 2180 |
case cs_sqrt: |
| 2181 |
NEED(cs2_stack_top, 1); |
| 2182 |
cs2_arg_stack[cs2_stack_top - 1] = |
| 2183 |
sqrt(cs2_arg_stack[cs2_stack_top - 1]); |
| 2184 |
break; |
| 2185 |
case cs_dup: |
| 2186 |
NEED(cs2_stack_top, 1); |
| 2187 |
NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1); |
| 2188 |
cs2_arg_stack[cs2_stack_top] = cs2_arg_stack[cs2_stack_top - 1]; |
| 2189 |
cs2_stack_top++; |
| 2190 |
break; |
| 2191 |
case cs_exch: |
| 2192 |
NEED(cs2_stack_top, 2); |
| 2193 |
{ |
| 2194 |
double save = cs2_arg_stack[cs2_stack_top - 2]; |
| 2195 |
cs2_arg_stack[cs2_stack_top - 2] = cs2_arg_stack[cs2_stack_top - 1]; |
| 2196 |
cs2_arg_stack[cs2_stack_top - 1] = save; |
| 2197 |
} |
| 2198 |
break; |
| 2199 |
case cs_index: |
| 2200 |
NEED(cs2_stack_top, 2); |
| 2201 |
{ |
| 2202 |
int idx = (int) cs2_arg_stack[cs2_stack_top - 1]; |
| 2203 |
if (idx < 0) { |
| 2204 |
cs2_arg_stack[cs2_stack_top - 1] = |
| 2205 |
cs2_arg_stack[cs2_stack_top - 2]; |
| 2206 |
} else { |
| 2207 |
NEED(cs2_stack_top, idx + 2); |
| 2208 |
cs2_arg_stack[cs2_stack_top - 1] = |
| 2209 |
cs2_arg_stack[cs2_stack_top - idx - 2]; |
| 2210 |
} |
| 2211 |
} |
| 2212 |
break; |
| 2213 |
case cs_roll: |
| 2214 |
NEED(cs2_stack_top, 2); |
| 2215 |
{ |
| 2216 |
int N, J; |
| 2217 |
J = (int) cs2_arg_stack[--cs2_stack_top]; |
| 2218 |
N = (int) cs2_arg_stack[--cs2_stack_top]; |
| 2219 |
NEED(cs2_stack_top, N); |
| 2220 |
if (J > 0) { |
| 2221 |
J = J % N; |
| 2222 |
while (J-- > 0) { |
| 2223 |
double save = cs2_arg_stack[cs2_stack_top - 1]; |
| 2224 |
int i = cs2_stack_top - 1; |
| 2225 |
while (i > cs2_stack_top - N) { |
| 2226 |
cs2_arg_stack[i] = cs2_arg_stack[i - 1]; |
| 2227 |
i--; |
| 2228 |
} |
| 2229 |
cs2_arg_stack[i] = save; |
| 2230 |
} |
| 2231 |
} else { |
| 2232 |
J = (-J) % N; |
| 2233 |
while (J-- > 0) { |
| 2234 |
double save = cs2_arg_stack[cs2_stack_top - N]; |
| 2235 |
int i = cs2_stack_top - N; |
| 2236 |
while (i < cs2_stack_top - 1) { |
| 2237 |
cs2_arg_stack[i] = cs2_arg_stack[i + 1]; |
| 2238 |
i++; |
| 2239 |
} |
| 2240 |
cs2_arg_stack[i] = save; |
| 2241 |
} |
| 2242 |
} |
| 2243 |
} |
| 2244 |
break; |
| 2245 |
case cs_random: |
| 2246 |
formatted_warning("cff","%s: Charstring operator 'random' found.", CS_TYPE2_DEBUG_STR); |
| 2247 |
NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1); |
| 2248 |
cs2_arg_stack[cs2_stack_top++] = 1.0; |
| 2249 |
break; |
| 2250 |
default: |
| 2251 |
formatted_warning("cff","%s: unknown charstring operator: 0x0c%02x", CS_TYPE2_DEBUG_STR, op); |
| 2252 |
status = CS_PARSE_CFF_ERROR; |
| 2253 |
break; |
| 2254 |
} |
| 2255 |
return; |
| 2256 |
} |
| 2257 |
|
| 2258 |
/*tex integer: exactly the same as the DICT encoding (except 29) */ |
| 2259 |
|
| 2260 |
static void cs2_get_integer(card8 ** data, card8 * endptr) |
| 2261 |
{ |
| 2262 |
long result = 0; |
| 2263 |
card8 b0 = **data, b1, b2; |
| 2264 |
*data += 1; |
| 2265 |
if (b0 == 28) { |
| 2266 |
/*tex shortint */ |
| 2267 |
SRC_NEED(endptr, *data + 2); |
| 2268 |
b1 = **data; |
| 2269 |
b2 = *(*data + 1); |
| 2270 |
result = b1 * 256 + b2; |
| 2271 |
if (result > 0x7fff) |
| 2272 |
result -= 0x10000L; |
| 2273 |
*data += 2; |
| 2274 |
} else if (b0 >= 32 && b0 <= 246) { |
| 2275 |
/*tex int (1) */ |
| 2276 |
result = b0 - 139; |
| 2277 |
} else if (b0 >= 247 && b0 <= 250) { |
| 2278 |
/*tex int (2) */ |
| 2279 |
SRC_NEED(endptr, *data + 1); |
| 2280 |
b1 = **data; |
| 2281 |
result = (b0 - 247) * 256 + b1 + 108; |
| 2282 |
*data += 1; |
| 2283 |
} else if (b0 >= 251 && b0 <= 254) { |
| 2284 |
SRC_NEED(endptr, *data + 1); |
| 2285 |
b1 = **data; |
| 2286 |
result = -(b0 - 251) * 256 - b1 - 108; |
| 2287 |
*data += 1; |
| 2288 |
} else { |
| 2289 |
status = CS_PARSE_CFF_ERROR; |
| 2290 |
return; |
| 2291 |
} |
| 2292 |
NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1); |
| 2293 |
cs2_arg_stack[cs2_stack_top++] = (double) result; |
| 2294 |
return; |
| 2295 |
} |
| 2296 |
|
| 2297 |
/*tex Signed 16.16-bits fixed number for Type 2 charstring encoding. */ |
| 2298 |
|
| 2299 |
static void get_fixed(card8 ** data, card8 * endptr) |
| 2300 |
{ |
| 2301 |
long ivalue; |
| 2302 |
double rvalue; |
| 2303 |
*data += 1; |
| 2304 |
SRC_NEED(endptr, *data + 4); |
| 2305 |
ivalue = *(*data) * 0x100 + *(*data + 1); |
| 2306 |
rvalue = (double) ((ivalue > 0x7fffL) ? (ivalue - 0x10000L) : ivalue); |
| 2307 |
ivalue = *(*data + 2) * 0x100 + *(*data + 3); |
| 2308 |
rvalue += ((double) ivalue) / 0x10000L; |
| 2309 |
NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1); |
| 2310 |
cs2_arg_stack[cs2_stack_top++] = rvalue; |
| 2311 |
*data += 4; |
| 2312 |
return; |
| 2313 |
} |
| 2314 |
|
| 2315 |
/*tex |
| 2316 |
|
| 2317 |
Subroutines: the bias for subroutine number is introduced in type 2 |
| 2318 |
charstrings. |
| 2319 |
|
| 2320 |
\starttabulate |
| 2321 |
\NC \type {subr} \NC set to a pointer to the subroutine charstring \NC \NR |
| 2322 |
\NC \type {len} \NC set to the length of subroutine charstring \NC \NR |
| 2323 |
\NC \type {subr_idx} \NC CFF INDEX data that contains subroutines \NC \NR |
| 2324 |
\NC \type {id} \NC biased subroutine number \NC \NR |
| 2325 |
\stoptabulate |
| 2326 |
|
| 2327 |
*/ |
| 2328 |
|
| 2329 |
static void get_subr(card8 ** subr, long *len, cff_index * subr_idx, long id) |
| 2330 |
{ |
| 2331 |
card16 count; |
| 2332 |
if (subr_idx == NULL) |
| 2333 |
formatted_error("cff","%s: subroutine called but no subroutine found",CS_TYPE2_DEBUG_STR); |
| 2334 |
count = subr_idx->count; |
| 2335 |
/*tex addi the bias number */ |
| 2336 |
if (count < 1240) { |
| 2337 |
id += 107; |
| 2338 |
} else if (count < 33900) { |
| 2339 |
id += 1131; |
| 2340 |
} else { |
| 2341 |
id += 32768; |
| 2342 |
} |
| 2343 |
if (id > count) |
| 2344 |
formatted_error("cff","%s: invalid subroutine index: %ld (max=%u)", CS_TYPE2_DEBUG_STR, id, count); |
| 2345 |
*len = (long) ((subr_idx->offset)[id + 1] - (subr_idx->offset)[id]); |
| 2346 |
*subr = subr_idx->data + (subr_idx->offset)[id] - 1; |
| 2347 |
return; |
| 2348 |
} |
| 2349 |
|
| 2350 |
/*tex |
| 2351 |
|
| 2352 |
The Type 2 interpretation of a number encoded in five-bytes (those with an |
| 2353 |
initial byte value of 255) differs from how it is interpreted in the Type 1 |
| 2354 |
format. |
| 2355 |
|
| 2356 |
*/ |
| 2357 |
|
| 2358 |
static void do_charstring(card8 ** dest, card8 * limit, card8 ** data, card8 * endptr, |
| 2359 |
cff_index * gsubr_idx, cff_index * subr_idx, int cff2) |
| 2360 |
{ |
| 2361 |
card8 b0 = 0, *subr; |
| 2362 |
long len; |
| 2363 |
if (cs2_nest > CS_SUBR_NEST_MAX) |
| 2364 |
formatted_error("cff","%s: subroutine nested too deeply", CS_TYPE2_DEBUG_STR); |
| 2365 |
cs2_nest++; |
| 2366 |
while (*data < endptr && status == CS_PARSE_OK) { |
| 2367 |
b0 = **data; |
| 2368 |
if (b0 == 255) { |
| 2369 |
/*tex A 16-bit.16-bit fixed signed number. */ |
| 2370 |
get_fixed(data, endptr); |
| 2371 |
} else if (b0 == cs_return) { |
| 2372 |
status = CS_SUBR_RETURN; |
| 2373 |
} else if (b0 == cs_callgsubr) { |
| 2374 |
if (cs2_stack_top < 1) { |
| 2375 |
status = CS_STACK_CFF_ERROR; |
| 2376 |
} else { |
| 2377 |
cs2_stack_top--; |
| 2378 |
get_subr(&subr, &len, gsubr_idx, (long) cs2_arg_stack[cs2_stack_top]); |
| 2379 |
if (*dest + len > limit) |
| 2380 |
formatted_error("cff","%s: possible buffer overflow (1)", CS_TYPE2_DEBUG_STR); |
| 2381 |
do_charstring(dest, limit, &subr, subr + len, gsubr_idx, subr_idx, cff2); |
| 2382 |
*data += 1; |
| 2383 |
} |
| 2384 |
} else if (b0 == cs_callsubr) { |
| 2385 |
if (cs2_stack_top < 1) { |
| 2386 |
status = CS_STACK_CFF_ERROR; |
| 2387 |
} else { |
| 2388 |
cs2_stack_top--; |
| 2389 |
get_subr(&subr, &len, subr_idx, (long) cs2_arg_stack[cs2_stack_top]); |
| 2390 |
if (limit < *dest + len) |
| 2391 |
formatted_error("cff","%s: possible buffer overflow (2)", CS_TYPE2_DEBUG_STR); |
| 2392 |
do_charstring(dest, limit, &subr, subr + len, gsubr_idx, subr_idx, cff2); |
| 2393 |
*data += 1; |
| 2394 |
} |
| 2395 |
} else if (b0 == cs_escape) { |
| 2396 |
do_operator2(dest, limit, data, endptr); |
| 2397 |
} else if (b0 < 32 && b0 != 28) { |
| 2398 |
do_operator1(dest, limit, data, endptr); |
| 2399 |
} else if ((b0 <= 22 && b0 >= 27) || b0 == 31) { |
| 2400 |
status = CS_PARSE_CFF_ERROR; |
| 2401 |
} else { |
| 2402 |
cs2_get_integer(data, endptr); |
| 2403 |
} |
| 2404 |
} |
| 2405 |
if (cff2) { |
| 2406 |
DST_NEED(limit, *dest + 1); |
| 2407 |
++endptr; |
| 2408 |
*(*dest)++ = cs_endchar; |
| 2409 |
} else if (status == CS_SUBR_RETURN) { |
| 2410 |
status = CS_PARSE_OK; |
| 2411 |
} else if (status == CS_CHAR_END && *data < endptr) { |
| 2412 |
formatted_warning("cff","%s: garbage after endchar", CS_TYPE2_DEBUG_STR); |
| 2413 |
} else if (status < CS_PARSE_OK) { |
| 2414 |
formatted_error("cff","%s: parsing charstring failed: (status=%d, stack=%d)", CS_TYPE2_DEBUG_STR, status, cs2_stack_top); |
| 2415 |
} |
| 2416 |
cs2_nest--; |
| 2417 |
return; |
| 2418 |
} |
| 2419 |
|
| 2420 |
static void cs_parse_init(void) |
| 2421 |
{ |
| 2422 |
status = CS_PARSE_OK; |
| 2423 |
cs2_nest = 0; |
| 2424 |
phase = 0; |
| 2425 |
num_stems = 0; |
| 2426 |
cs2_stack_top = 0; |
| 2427 |
} |
| 2428 |
|
| 2429 |
/*tex Not just copying \unknown */ |
| 2430 |
|
| 2431 |
static long cs_copy_charstring(card8 * dst, long dstlen, card8 * src, long srclen, cff_index * gsubr, |
| 2432 |
cff_index * subr, double default_width, double nominal_width, cs_ginfo * ginfo, int cff2) |
| 2433 |
{ |
| 2434 |
card8 *save = dst; |
| 2435 |
|
| 2436 |
cs_parse_init(); |
| 2437 |
|
| 2438 |
width = 0.0; |
| 2439 |
have_width = 0; |
| 2440 |
|
| 2441 |
/* expand call(g)subrs */ |
| 2442 |
do_charstring(&dst, dst + dstlen, &src, src + srclen, gsubr, subr, cff2); |
| 2443 |
|
| 2444 |
if (ginfo) { |
| 2445 |
ginfo->flags = 0; /* not used */ |
| 2446 |
if (have_width) { |
| 2447 |
ginfo->wx = nominal_width + width; |
| 2448 |
} else { |
| 2449 |
ginfo->wx = default_width; |
| 2450 |
} |
| 2451 |
} |
| 2452 |
|
| 2453 |
return (long) (dst - save); |
| 2454 |
} |
| 2455 |
|
| 2456 |
/*tex CID-Keyed font specific. */ |
| 2457 |
|
| 2458 |
long cff_read_fdselect(cff_font * cff) |
| 2459 |
{ |
| 2460 |
cff_fdselect *fdsel; |
| 2461 |
long offset, length; |
| 2462 |
card16 i; |
| 2463 |
if (cff->topdict == NULL) |
| 2464 |
normal_error("cff","top DICT not available"); |
| 2465 |
if (!(cff->flag & FONTTYPE_CIDFONT)) |
| 2466 |
return 0; |
| 2467 |
offset = (long) cff_dict_get(cff->topdict, "FDSelect", 0); |
| 2468 |
cff->offset = (l_offset) offset; |
| 2469 |
cff->fdselect = fdsel = xcalloc(1, sizeof(cff_fdselect)); |
| 2470 |
fdsel->format = get_card8(cff); |
| 2471 |
length = 1; |
| 2472 |
switch (fdsel->format) { |
| 2473 |
case 0: |
| 2474 |
fdsel->num_entries = cff->num_glyphs; |
| 2475 |
(fdsel->data).fds = xmalloc(fdsel->num_entries * sizeof(card8)); |
| 2476 |
for (i = 0; i < (fdsel->num_entries); i++) { |
| 2477 |
(fdsel->data).fds[i] = get_card8(cff); |
| 2478 |
} |
| 2479 |
length += fdsel->num_entries; |
| 2480 |
break; |
| 2481 |
case 3: |
| 2482 |
{ |
| 2483 |
cff_range3 *ranges; |
| 2484 |
fdsel->num_entries = get_card16(cff); |
| 2485 |
fdsel->data.ranges = ranges = |
| 2486 |
xcalloc(fdsel->num_entries, sizeof(cff_range3)); |
| 2487 |
for (i = 0; i < (fdsel->num_entries); i++) { |
| 2488 |
ranges[i].first = get_card16(cff); |
| 2489 |
ranges[i].fd = get_card8(cff); |
| 2490 |
} |
| 2491 |
if (ranges[0].first != 0) |
| 2492 |
normal_error("cff","range not starting with 0"); |
| 2493 |
if (cff->num_glyphs != get_card16(cff)) |
| 2494 |
normal_error("cff","sentinel value mismatched with number of glyphs"); |
| 2495 |
length += (fdsel->num_entries) * 3 + 4; |
| 2496 |
} |
| 2497 |
break; |
| 2498 |
default: |
| 2499 |
xfree(fdsel); |
| 2500 |
normal_error("cff","unknown FDSelect format"); |
| 2501 |
break; |
| 2502 |
} |
| 2503 |
return length; |
| 2504 |
} |
| 2505 |
|
| 2506 |
long cff_pack_fdselect(cff_font * cff, card8 * dest, long destlen) |
| 2507 |
{ |
| 2508 |
cff_fdselect *fdsel; |
| 2509 |
long len = 0; |
| 2510 |
card16 i; |
| 2511 |
if (cff->fdselect == NULL) |
| 2512 |
return 0; |
| 2513 |
if (destlen < 1) |
| 2514 |
normal_error("cff","buffer overflow (23)"); |
| 2515 |
fdsel = cff->fdselect; |
| 2516 |
dest[len++] = fdsel->format; |
| 2517 |
switch (fdsel->format) { |
| 2518 |
case 0: |
| 2519 |
if (fdsel->num_entries != cff->num_glyphs) |
| 2520 |
normal_error("cff","invalid data"); |
| 2521 |
if (destlen < len + fdsel->num_entries) |
| 2522 |
normal_error("cff","buffer overflow (24)"); |
| 2523 |
for (i = 0; i < fdsel->num_entries; i++) { |
| 2524 |
dest[len++] = (fdsel->data).fds[i]; |
| 2525 |
} |
| 2526 |
break; |
| 2527 |
case 3: |
| 2528 |
{ |
| 2529 |
if (destlen < len + 2) |
| 2530 |
normal_error("cff","buffer overflow (25)"); |
| 2531 |
len += 2; |
| 2532 |
for (i = 0; i < (fdsel->num_entries); i++) { |
| 2533 |
if (destlen < len + 3) |
| 2534 |
normal_error("cff","buffer overflow (26)"); |
| 2535 |
dest[len++] = |
| 2536 |
(card8) (((fdsel->data).ranges[i].first >> 8) & 0xff); |
| 2537 |
dest[len++] = (card8) ((fdsel->data).ranges[i].first & 0xff); |
| 2538 |
dest[len++] = (card8) ((fdsel->data).ranges[i].fd); |
| 2539 |
} |
| 2540 |
if (destlen < len + 2) |
| 2541 |
normal_error("cff","buffer overflow (27)"); |
| 2542 |
dest[len++] = (card8) ((cff->num_glyphs >> 8) & 0xff); |
| 2543 |
dest[len++] = (card8) (cff->num_glyphs & 0xff); |
| 2544 |
dest[1] = (card8) (((len / 3 - 1) >> 8) & 0xff); |
| 2545 |
dest[2] = (card8) ((len / 3 - 1) & 0xff); |
| 2546 |
} |
| 2547 |
break; |
| 2548 |
default: |
| 2549 |
normal_error("cff","unknown FDSelect format"); |
| 2550 |
break; |
| 2551 |
} |
| 2552 |
return len; |
| 2553 |
} |
| 2554 |
|
| 2555 |
/*tex Create an instance of embeddable font. */ |
| 2556 |
|
| 2557 |
static void write_fontfile(PDF pdf, cff_font * cffont, char *fullname) |
| 2558 |
{ |
| 2559 |
cff_index *topdict, *fdarray, *private; |
| 2560 |
unsigned char *dest; |
| 2561 |
long destlen = 0, i, size; |
| 2562 |
long offset, topdict_offset, fdarray_offset; |
| 2563 |
topdict = cff_new_index(1); |
| 2564 |
fdarray = cff_new_index(cffont->num_fds); |
| 2565 |
private = cff_new_index(cffont->num_fds); |
| 2566 |
cff_dict_remove(cffont->topdict, "UniqueID"); |
| 2567 |
cff_dict_remove(cffont->topdict, "XUID"); |
| 2568 |
/*tex A bad font may have this: */ |
| 2569 |
cff_dict_remove(cffont->topdict, "Private"); |
| 2570 |
/*tex A bad font may have this: */ |
| 2571 |
cff_dict_remove(cffont->topdict, "Encoding"); |
| 2572 |
/*tex This is CFF2 specific: */ |
| 2573 |
cff_dict_remove(cffont->topdict, "vstore"); |
| 2574 |
/*tex This is CFF2 specific: */ |
| 2575 |
cff_dict_remove(cffont->topdict, "maxstack"); |
| 2576 |
topdict->offset[1] = (l_offset) cff_dict_pack(cffont->topdict, (card8 *) work_buffer, WORK_BUFFER_SIZE) + 1; |
| 2577 |
for (i = 0; i < cffont->num_fds; i++) { |
| 2578 |
size = 0; |
| 2579 |
if (cffont->private && cffont->private[i]) { |
| 2580 |
size = cff_dict_pack(cffont->private[i], (card8 *) work_buffer, WORK_BUFFER_SIZE); |
| 2581 |
if (size < 1) { |
| 2582 |
/*tex |Private| contains only |Subr|: */ |
| 2583 |
cff_dict_remove(cffont->fdarray[i], "Private"); |
| 2584 |
} |
| 2585 |
} |
| 2586 |
(private->offset)[i + 1] = (unsigned long) ((private->offset)[i] + (unsigned) size); |
| 2587 |
(fdarray->offset)[i + 1] = (unsigned long) ((fdarray->offset)[i] |
| 2588 |
+ (unsigned) cff_dict_pack(cffont->fdarray[i], (card8 *) work_buffer, WORK_BUFFER_SIZE)); |
| 2589 |
} |
| 2590 |
/*tex The header size: */ |
| 2591 |
destlen = 4; |
| 2592 |
destlen += cff_set_name(cffont, fullname); |
| 2593 |
destlen += cff_index_size(topdict); |
| 2594 |
destlen += cff_index_size(cffont->string); |
| 2595 |
destlen += cff_index_size(cffont->gsubr); |
| 2596 |
/*tex |charset| format 0 */ |
| 2597 |
destlen += (cffont->charsets->num_entries) * 2 + 1; |
| 2598 |
/*tex |fdselect| format 3 */ |
| 2599 |
destlen += (cffont->fdselect->num_entries) * 3 + 5; |
| 2600 |
destlen += cff_index_size(cffont->cstrings); |
| 2601 |
destlen += cff_index_size(fdarray); |
| 2602 |
/* |Private| is not indexed */ |
| 2603 |
destlen = (long) (destlen + (long) private->offset[private->count] - 1); |
| 2604 |
dest = xcalloc((unsigned) destlen, sizeof(card8)); |
| 2605 |
offset = 0; |
| 2606 |
/*tex |Header| */ |
| 2607 |
offset += cff_put_header(cffont, dest + offset, destlen - offset); |
| 2608 |
/*tex |Name| */ |
| 2609 |
offset += cff_pack_index(cffont->name, dest + offset, destlen - offset); |
| 2610 |
/*tex |Top DICT| */ |
| 2611 |
topdict_offset = offset; |
| 2612 |
offset += cff_index_size(topdict); |
| 2613 |
/*tex |Strings| */ |
| 2614 |
offset += cff_pack_index(cffont->string, dest + offset, destlen - offset); |
| 2615 |
/*tex |Global Subrs| */ |
| 2616 |
offset += cff_pack_index(cffont->gsubr, dest + offset, destlen - offset); |
| 2617 |
/*tex |charset| */ |
| 2618 |
cff_dict_set(cffont->topdict, "charset", 0, (double) offset); |
| 2619 |
offset += cff_pack_charsets(cffont, dest + offset, destlen - offset); |
| 2620 |
/*tex |FDSelect| */ |
| 2621 |
cff_dict_set(cffont->topdict, "FDSelect", 0, (double) offset); |
| 2622 |
offset += cff_pack_fdselect(cffont, dest + offset, destlen - offset); |
| 2623 |
/*tex |CharStrings| */ |
| 2624 |
cff_dict_set(cffont->topdict, "CharStrings", 0, (double) offset); |
| 2625 |
offset += cff_pack_index(cffont->cstrings, dest + offset, cff_index_size(cffont->cstrings)); |
| 2626 |
cff_release_index(cffont->cstrings); |
| 2627 |
/*tex |Charstring|s can consume a lot of memory. */ |
| 2628 |
cffont->cstrings = NULL; |
| 2629 |
/*tex |FDArray| and |Private| */ |
| 2630 |
cff_dict_set(cffont->topdict, "FDArray", 0, (double) offset); |
| 2631 |
fdarray_offset = offset; |
| 2632 |
offset += cff_index_size(fdarray); |
| 2633 |
fdarray->data = xcalloc((unsigned) (fdarray->offset[fdarray->count] - 1), sizeof(card8)); |
| 2634 |
for (i = 0; i < cffont->num_fds; i++) { |
| 2635 |
size = (long) (private->offset[i + 1] - private->offset[i]); |
| 2636 |
if (cffont->private[i] && size > 0) { |
| 2637 |
cff_dict_pack(cffont->private[i], dest + offset, size); |
| 2638 |
cff_dict_set(cffont->fdarray[i], "Private", 0, (double) size); |
| 2639 |
cff_dict_set(cffont->fdarray[i], "Private", 1, (double) offset); |
| 2640 |
} |
| 2641 |
cff_dict_pack(cffont->fdarray[i], fdarray->data + (fdarray->offset)[i] - 1, (long) (fdarray->offset[fdarray->count] - 1)); |
| 2642 |
offset += size; |
| 2643 |
} |
| 2644 |
cff_pack_index(fdarray, dest + fdarray_offset, cff_index_size(fdarray)); |
| 2645 |
cff_release_index(fdarray); |
| 2646 |
cff_release_index(private); |
| 2647 |
/*tex Finally the |Top DICT| */ |
| 2648 |
topdict->data = xcalloc((unsigned) (topdict->offset[topdict->count] - 1), sizeof(card8)); |
| 2649 |
cff_dict_pack(cffont->topdict, topdict->data, (long) (topdict->offset[topdict->count] - 1)); |
| 2650 |
cff_pack_index(topdict, dest + topdict_offset, cff_index_size(topdict)); |
| 2651 |
cff_release_index(topdict); |
| 2652 |
for (i = 0; i < offset; i++) { |
| 2653 |
strbuf_putchar(pdf->fb, dest[i]); |
| 2654 |
} |
| 2655 |
xfree(dest); |
| 2656 |
return; |
| 2657 |
} |
| 2658 |
|
| 2659 |
void write_cff(PDF pdf, cff_font * cffont, fd_entry * fd) |
| 2660 |
{ |
| 2661 |
cff_index *charstrings, *cs_idx; |
| 2662 |
long charstring_len, max_len; |
| 2663 |
long size, offset = 0; |
| 2664 |
card8 *data; |
| 2665 |
card16 num_glyphs, cs_count1, code, gid, last_cid; |
| 2666 |
double nominal_width, default_width; |
| 2667 |
char *fontname; |
| 2668 |
char *fullname; |
| 2669 |
glw_entry *glyph, *found; |
| 2670 |
struct avl_traverser t; |
| 2671 |
cffont->_string = NULL; |
| 2672 |
fontname = xcalloc((unsigned) (1 + strlen(fd->fontname)), 1); |
| 2673 |
sprintf(fontname, "%s", fd->fontname); |
| 2674 |
fullname = xcalloc((unsigned) (8 + strlen(fd->fontname)), 1); |
| 2675 |
sprintf(fullname, "%s+%s", fd->subset_tag, fd->fontname); |
| 2676 |
/*tex Finish parsing the CFF. */ |
| 2677 |
cff_read_private(cffont); |
| 2678 |
cff_read_subrs(cffont); |
| 2679 |
/*tex The |Width|s. */ |
| 2680 |
if (cffont->private[0] && cff_dict_known(cffont->private[0], "defaultWidthX")) { |
| 2681 |
default_width = (double) cff_dict_get(cffont->private[0], "defaultWidthX", 0); |
| 2682 |
} else { |
| 2683 |
default_width = CFF_DEFAULTWIDTHX_DEFAULT; |
| 2684 |
} |
| 2685 |
if (cffont->private[0] && cff_dict_known(cffont->private[0], "nominalWidthX")) { |
| 2686 |
nominal_width = (double) cff_dict_get(cffont->private[0], "nominalWidthX", 0); |
| 2687 |
} else { |
| 2688 |
nominal_width = CFF_NOMINALWIDTHX_DEFAULT; |
| 2689 |
} |
| 2690 |
num_glyphs = 0; |
| 2691 |
last_cid = 0; |
| 2692 |
glyph = xtalloc(1, glw_entry); |
| 2693 |
/*tex insert |notdef| */ |
| 2694 |
glyph->id = 0; |
| 2695 |
if (avl_find(fd->gl_tree, glyph) == NULL) { |
| 2696 |
avl_insert(fd->gl_tree, glyph); |
| 2697 |
glyph = xtalloc(1, glw_entry); |
| 2698 |
} |
| 2699 |
avl_t_init(&t, fd->gl_tree); |
| 2700 |
for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree); |
| 2701 |
found != NULL; found = (glw_entry *) avl_t_next(&t)) { |
| 2702 |
if (found->id > last_cid) |
| 2703 |
last_cid = (card16) found->id; |
| 2704 |
num_glyphs++; |
| 2705 |
} |
| 2706 |
{ |
| 2707 |
cff_fdselect *fdselect; |
| 2708 |
fdselect = xcalloc(1, sizeof(cff_fdselect)); |
| 2709 |
fdselect->format = 3; |
| 2710 |
fdselect->num_entries = 1; |
| 2711 |
fdselect->data.ranges = xcalloc(1, sizeof(cff_range3)); |
| 2712 |
fdselect->data.ranges[0].first = 0; |
| 2713 |
fdselect->data.ranges[0].fd = 0; |
| 2714 |
cffont->fdselect = fdselect; |
| 2715 |
} |
| 2716 |
{ |
| 2717 |
cff_charsets *charset; |
| 2718 |
charset = xcalloc(1, sizeof(cff_charsets)); |
| 2719 |
charset->format = 0; |
| 2720 |
charset->num_entries = (card16) (num_glyphs - 1); |
| 2721 |
charset->data.glyphs = xcalloc(num_glyphs, sizeof(s_SID)); |
| 2722 |
gid = 0; |
| 2723 |
avl_t_init(&t, fd->gl_tree); |
| 2724 |
for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree); |
| 2725 |
found != NULL; found = (glw_entry *) avl_t_next(&t)) { |
| 2726 |
if (found->id != 0) { |
| 2727 |
charset->data.glyphs[gid] = (s_SID) found->id; |
| 2728 |
gid++; |
| 2729 |
} |
| 2730 |
} |
| 2731 |
cffont->charsets = charset; |
| 2732 |
if (cffont->header_major == 2) { |
| 2733 |
cff_dict_add(cffont->topdict, "charset", 1); |
| 2734 |
} |
| 2735 |
} |
| 2736 |
cff_dict_add(cffont->topdict, "CIDCount", 1); |
| 2737 |
cff_dict_set(cffont->topdict, "CIDCount", 0, last_cid + 1); |
| 2738 |
if (cffont->header_major == 2) { |
| 2739 |
cff_dict_add(cffont->topdict, "FullName", 1); |
| 2740 |
cff_dict_set(cffont->topdict, "FullName", 0, (double) cff_add_string(cffont, fontname)); |
| 2741 |
cff_dict_add(cffont->topdict, "FontBBox", 4); |
| 2742 |
cff_dict_set(cffont->topdict, "FontBBox", 0, fd->font_dim[FONTBBOX1_CODE].val); |
| 2743 |
cff_dict_set(cffont->topdict, "FontBBox", 1, fd->font_dim[FONTBBOX2_CODE].val); |
| 2744 |
cff_dict_set(cffont->topdict, "FontBBox", 2, fd->font_dim[FONTBBOX3_CODE].val); |
| 2745 |
cff_dict_set(cffont->topdict, "FontBBox", 3, fd->font_dim[FONTBBOX4_CODE].val); |
| 2746 |
} |
| 2747 |
cffont->fdarray = xcalloc(1, sizeof(cff_dict *)); |
| 2748 |
cffont->fdarray[0] = cff_new_dict(); |
| 2749 |
cff_dict_add(cffont->fdarray[0], "FontName", 1); |
| 2750 |
/*tex fix: skip XXXXXX+ */ |
| 2751 |
cff_dict_set(cffont->fdarray[0], "FontName", 0, (double) cff_add_string(cffont, fullname)); |
| 2752 |
cff_dict_add(cffont->fdarray[0], "Private", 2); |
| 2753 |
cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0); |
| 2754 |
cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0); |
| 2755 |
/*tex |FDArray| index offset, not known yet */ |
| 2756 |
cff_dict_add(cffont->topdict, "FDArray", 1); |
| 2757 |
cff_dict_set(cffont->topdict, "FDArray", 0, 0.0); |
| 2758 |
/*tex |FDSelect| offset, not known yet */ |
| 2759 |
cff_dict_add(cffont->topdict, "FDSelect", 1); |
| 2760 |
cff_dict_set(cffont->topdict, "FDSelect", 0, 0.0); |
| 2761 |
cff_dict_remove(cffont->topdict, "UniqueID"); |
| 2762 |
cff_dict_remove(cffont->topdict, "XUID"); |
| 2763 |
cff_dict_remove(cffont->topdict, "Private"); |
| 2764 |
cff_dict_remove(cffont->topdict, "Encoding"); |
| 2765 |
cffont->offset = (l_offset) cff_dict_get(cffont->topdict, "CharStrings", 0); |
| 2766 |
cs_idx = cff_get_index_header(cffont); |
| 2767 |
offset = (long) cffont->offset; |
| 2768 |
cs_count1 = cs_idx->count; |
| 2769 |
if (cs_count1 < 2) { |
| 2770 |
normal_error("cff","no valid charstring data found"); |
| 2771 |
} |
| 2772 |
/*tex Build the new charstrings entry. */ |
| 2773 |
charstrings = cff_new_index((card16) (cs_count1==USHRT_MAX?cs_count1: cs_count1 + 1)); |
| 2774 |
max_len = 2 * CS_STR_LEN_MAX; |
| 2775 |
charstrings->data = xcalloc((unsigned) max_len, sizeof(card8)); |
| 2776 |
charstring_len = 0; |
| 2777 |
gid = 0; |
| 2778 |
data = xcalloc(CS_STR_LEN_MAX, sizeof(card8)); |
| 2779 |
{ |
| 2780 |
int i; |
| 2781 |
int tex_font = fd->tex_font; |
| 2782 |
int streamprovider = 0; |
| 2783 |
int callback_id = 0 ; |
| 2784 |
if ((tex_font > 0) && (font_streamprovider(tex_font) == 1)) { |
| 2785 |
streamprovider = font_streamprovider(tex_font); |
| 2786 |
callback_id = callback_defined(glyph_stream_provider_callback); |
| 2787 |
} |
| 2788 |
for (i = 0; i < cs_count1; i++) { |
| 2789 |
code = (card16) i; |
| 2790 |
glyph->id = code; |
| 2791 |
if ((avl_find(fd->gl_tree,glyph) != NULL)) { |
| 2792 |
/*tex This code is the same as below, apart from small details */ |
| 2793 |
if (callback_id > 0) { |
| 2794 |
lstring * result; |
| 2795 |
run_callback(callback_id, "ddd->L", tex_font, i, streamprovider, &result); /* this call can be sped up */ |
| 2796 |
size = (size_t) result->l ; |
| 2797 |
if (size > 0) { |
| 2798 |
if (charstring_len + CS_STR_LEN_MAX >= max_len) { |
| 2799 |
max_len = (long)(charstring_len + 2 * CS_STR_LEN_MAX); |
| 2800 |
charstrings->data = xrealloc(charstrings->data, (unsigned)((unsigned)max_len*sizeof(card8))); |
| 2801 |
} |
| 2802 |
(charstrings->offset)[gid] = (unsigned)(charstring_len + 1); |
| 2803 |
cffont->offset = (l_offset)((unsigned)offset + (cs_idx->offset)[code] - 1); |
| 2804 |
memcpy(charstrings->data+charstring_len,(const char *) result->s,(size_t) size); |
| 2805 |
charstring_len += size; |
| 2806 |
xfree(result); |
| 2807 |
} |
| 2808 |
} else { |
| 2809 |
size = (long)(cs_idx->offset[code+1] - cs_idx->offset[code]); |
| 2810 |
if (size > CS_STR_LEN_MAX) { |
| 2811 |
formatted_error("cff","charstring too long: gid=%u, %ld bytes", code, size); |
| 2812 |
} |
| 2813 |
if (charstring_len + CS_STR_LEN_MAX >= max_len) { |
| 2814 |
max_len = (long)(charstring_len + 2 * CS_STR_LEN_MAX); |
| 2815 |
charstrings->data = xrealloc(charstrings->data, (unsigned)((unsigned)max_len*sizeof(card8))); |
| 2816 |
} |
| 2817 |
(charstrings->offset)[gid] = (unsigned)(charstring_len + 1); |
| 2818 |
cffont->offset = (l_offset)((unsigned)offset + (cs_idx->offset)[code] - 1); |
| 2819 |
memcpy(data,&cffont->stream[cffont->offset],(size_t)size); |
| 2820 |
charstring_len += cs_copy_charstring( |
| 2821 |
charstrings->data + charstring_len, |
| 2822 |
max_len - charstring_len, |
| 2823 |
data, size, |
| 2824 |
cffont->gsubr, (cffont->subrs)[0], |
| 2825 |
default_width, nominal_width, NULL, |
| 2826 |
cffont->header_major == 2 |
| 2827 |
); |
| 2828 |
} |
| 2829 |
gid++; |
| 2830 |
} |
| 2831 |
} |
| 2832 |
} |
| 2833 |
/*tex |
| 2834 |
The |CIDSet| is a table of bits indexed by cid, bytes with high order bit |
| 2835 |
first, each (set) bit is a (present) CID. |
| 2836 |
*/ |
| 2837 |
if ((! pdf->omit_cidset) && (pdf->major_version == 1)) { |
| 2838 |
int cid; |
| 2839 |
cidset = pdf_create_obj(pdf, obj_type_others, 0); |
| 2840 |
if (cidset != 0) { |
| 2841 |
size_t l = (last_cid/8)+1; |
| 2842 |
char *stream = xmalloc(l); |
| 2843 |
memset(stream, 0, l); |
| 2844 |
for (cid = 1; cid <= (long) last_cid; cid++) { |
| 2845 |
glyph->id = cid; |
| 2846 |
if (avl_find(fd->gl_tree,glyph) != NULL) { |
| 2847 |
stream[(cid / 8)] |= (1 << (7 - (cid % 8))); |
| 2848 |
} |
| 2849 |
} |
| 2850 |
pdf_begin_obj(pdf, cidset, OBJSTM_NEVER); |
| 2851 |
pdf_begin_dict(pdf); |
| 2852 |
pdf_dict_add_streaminfo(pdf); |
| 2853 |
pdf_end_dict(pdf); |
| 2854 |
pdf_begin_stream(pdf); |
| 2855 |
pdf_out_block(pdf, stream, l); |
| 2856 |
pdf_end_stream(pdf); |
| 2857 |
pdf_end_obj(pdf); |
| 2858 |
} |
| 2859 |
} |
| 2860 |
/*tex |
| 2861 |
This happens if the internal metrics do not agree with the actual disk |
| 2862 |
font. |
| 2863 |
*/ |
| 2864 |
if (gid < num_glyphs) { |
| 2865 |
formatted_warning("cff","embedded subset is smaller than expected: %d instead of %d glyphs", gid, num_glyphs); |
| 2866 |
num_glyphs = gid; |
| 2867 |
} |
| 2868 |
xfree(data); |
| 2869 |
cff_release_index(cs_idx); |
| 2870 |
(charstrings->offset)[num_glyphs] = (l_offset) (charstring_len + 1); |
| 2871 |
charstrings->count = num_glyphs; |
| 2872 |
cffont->num_glyphs = num_glyphs; |
| 2873 |
cffont->cstrings = charstrings; |
| 2874 |
/*tex |
| 2875 |
We don't use subroutines at all. |
| 2876 |
*/ |
| 2877 |
if (cffont->gsubr) |
| 2878 |
cff_release_index(cffont->gsubr); |
| 2879 |
cffont->gsubr = cff_new_index(0); |
| 2880 |
if (cffont->subrs && cffont->subrs[0]) |
| 2881 |
cff_release_index(cffont->subrs[0]); |
| 2882 |
cffont->subrs[0] = NULL; |
| 2883 |
if (cffont->private && (cffont->private)[0]) { |
| 2884 |
cff_dict_remove((cffont->private)[0], "Subrs"); /* no Subrs */ |
| 2885 |
} |
| 2886 |
cff_dict_update(cffont->topdict, cffont); |
| 2887 |
cff_add_string(cffont, "Adobe"); |
| 2888 |
cff_add_string(cffont, "Identity"); |
| 2889 |
if (cffont->header_major == 2) { |
| 2890 |
/*tex A crash. */ |
| 2891 |
} else if (cffont->private && (cffont->private)[0]) { |
| 2892 |
cff_dict_update(cffont->private[0], cffont); |
| 2893 |
} |
| 2894 |
cff_update_string(cffont); |
| 2895 |
/* CFF code need to be rewritten */ |
| 2896 |
cff_dict_add(cffont->topdict, "ROS", 3); |
| 2897 |
cff_dict_set(cffont->topdict, "ROS", 0, (double) cff_get_sid(cffont, "Adobe")); |
| 2898 |
cff_dict_set(cffont->topdict, "ROS", 1, (double) cff_get_sid(cffont, "Identity")); |
| 2899 |
cff_dict_set(cffont->topdict, "ROS", 2, 0.0); |
| 2900 |
write_fontfile(pdf, cffont, fullname); |
| 2901 |
xfree(fontname); |
| 2902 |
xfree(fullname); |
| 2903 |
cff_close(cffont); |
| 2904 |
} |
| 2905 |
|
| 2906 |
#define is_cidfont(a) ((a)->flag & FONTTYPE_CIDFONT) |
| 2907 |
#define CID_MAX 65535 |
| 2908 |
|
| 2909 |
void write_cid_cff(PDF pdf, cff_font * cffont, fd_entry * fd) |
| 2910 |
{ |
| 2911 |
cff_index *charstrings, *cs_idx; |
| 2912 |
long charstring_len, max_len; |
| 2913 |
long size, offset = 0; |
| 2914 |
int tex_font = fd->tex_font; |
| 2915 |
int streamprovider = 0; |
| 2916 |
int callback_id = 0 ; |
| 2917 |
card8 *data; |
| 2918 |
card16 num_glyphs, cs_count1, gid, last_cid; |
| 2919 |
int fdsel, prev_fd, cid_count, cid ; |
| 2920 |
char *fullname; |
| 2921 |
glw_entry *glyph; |
| 2922 |
unsigned char *CIDToGIDMap = NULL; |
| 2923 |
cff_fdselect *fdselect = NULL; |
| 2924 |
cff_charsets *charset = NULL; |
| 2925 |
if (!is_cidfont(cffont)) { |
| 2926 |
normal_error("cff","invalid CIDfont"); |
| 2927 |
return; |
| 2928 |
} |
| 2929 |
if ((tex_font > 0) && (font_streamprovider(tex_font) == 1)) { |
| 2930 |
streamprovider = font_streamprovider(tex_font); |
| 2931 |
callback_id = callback_defined(glyph_stream_provider_callback); |
| 2932 |
} |
| 2933 |
fullname = xcalloc((unsigned) (8 + strlen(fd->fontname)), 1); |
| 2934 |
sprintf(fullname, "%s+%s", fd->subset_tag, fd->fontname); |
| 2935 |
/*tex Finish parsing the CFF. */ |
| 2936 |
if (cff_dict_known(cffont->topdict, "CIDCount")) { |
| 2937 |
cid_count = (card16) cff_dict_get(cffont->topdict, "CIDCount", 0); |
| 2938 |
} else { |
| 2939 |
cid_count = CFF_CIDCOUNT_DEFAULT; |
| 2940 |
} |
| 2941 |
if (cffont->header_major == 2) { |
| 2942 |
/*tex hm */ |
| 2943 |
} else { |
| 2944 |
cff_read_charsets(cffont); |
| 2945 |
} |
| 2946 |
CIDToGIDMap = xmalloc((unsigned) ((2 * (unsigned) cid_count) * sizeof(unsigned char))); |
| 2947 |
memset(CIDToGIDMap, 0, (size_t) (2 * cid_count)); |
| 2948 |
glyph = xtalloc(1, glw_entry); |
| 2949 |
/*tex insert |notdef| */ |
| 2950 |
glyph->id = 0; |
| 2951 |
if (avl_find(fd->gl_tree, glyph) == NULL) { |
| 2952 |
avl_insert(fd->gl_tree, glyph); |
| 2953 |
glyph = xtalloc(1, glw_entry); |
| 2954 |
} |
| 2955 |
last_cid = 0; |
| 2956 |
num_glyphs = 0; |
| 2957 |
for (cid = 0; cid <= CID_MAX; cid++) { |
| 2958 |
glyph->id = (unsigned) cid; |
| 2959 |
if (avl_find(fd->gl_tree, glyph) != NULL) { |
| 2960 |
gid = (card16) cid; |
| 2961 |
CIDToGIDMap[2 * cid] = (unsigned char) ((gid >> 8) & 0xff); |
| 2962 |
CIDToGIDMap[2 * cid + 1] = (unsigned char) (gid & 0xff); |
| 2963 |
last_cid = (card16) cid; |
| 2964 |
num_glyphs++; |
| 2965 |
} |
| 2966 |
} |
| 2967 |
if (cffont->header_major == 2) { |
| 2968 |
/*tex hm */ |
| 2969 |
} else if (last_cid >= cffont->num_glyphs) { |
| 2970 |
formatted_error("cff font","bad glyph index %i",last_cid); |
| 2971 |
} |
| 2972 |
/*tex |
| 2973 |
The |CIDSet| table is a table of bits indexed by cid, bytes with high |
| 2974 |
order bit first, each (set) bit is a (present) CID. |
| 2975 |
*/ |
| 2976 |
if ((! pdf->omit_cidset) && (pdf->major_version == 1)) { |
| 2977 |
cidset = pdf_create_obj(pdf, obj_type_others, 0); |
| 2978 |
if (cidset != 0) { |
| 2979 |
size_t l = (last_cid / 8) + 1; |
| 2980 |
char *stream = xmalloc(l); |
| 2981 |
memset(stream, 0, l); |
| 2982 |
stream[0] |= 1 << 7; /*tex Force |.notdef| into the map. */ |
| 2983 |
for (cid = 1; cid <= (long) last_cid; cid++) { |
| 2984 |
if (CIDToGIDMap[2 * cid] || CIDToGIDMap[2 * cid + 1]) { |
| 2985 |
stream[(cid / 8)] |= (1 << (7 - (cid % 8))); |
| 2986 |
} |
| 2987 |
} |
| 2988 |
pdf_begin_obj(pdf, cidset, OBJSTM_NEVER); |
| 2989 |
pdf_begin_dict(pdf); |
| 2990 |
pdf_dict_add_streaminfo(pdf); |
| 2991 |
pdf_end_dict(pdf); |
| 2992 |
pdf_begin_stream(pdf); |
| 2993 |
pdf_out_block(pdf, stream, l); |
| 2994 |
pdf_end_stream(pdf); |
| 2995 |
pdf_end_obj(pdf); |
| 2996 |
xfree(stream); |
| 2997 |
} |
| 2998 |
} |
| 2999 |
cff_read_fdselect(cffont); |
| 3000 |
cff_read_fdarray(cffont); |
| 3001 |
cff_read_private(cffont); |
| 3002 |
cff_read_subrs(cffont); |
| 3003 |
cffont->offset = (l_offset) cff_dict_get(cffont->topdict, "CharStrings", 0); |
| 3004 |
cs_idx = cff_get_index_header(cffont); |
| 3005 |
offset = (long) cffont->offset; |
| 3006 |
cs_count1 = cs_idx->count; |
| 3007 |
if (cs_count1 < 2) { |
| 3008 |
normal_error("cff","no valid charstring data found"); |
| 3009 |
} |
| 3010 |
charset = xcalloc(1, sizeof(cff_charsets)); |
| 3011 |
charset->format = 0; |
| 3012 |
charset->num_entries = 0; |
| 3013 |
charset->data.glyphs = xcalloc(num_glyphs, sizeof(s_SID)); |
| 3014 |
fdselect = xcalloc(1, sizeof(cff_fdselect)); |
| 3015 |
fdselect->format = 3; |
| 3016 |
fdselect->num_entries = 0; |
| 3017 |
fdselect->data.ranges = xcalloc(num_glyphs, sizeof(cff_range3)); |
| 3018 |
charstrings = cff_new_index((card16) (cs_count1==USHRT_MAX?cs_count1: cs_count1 + 1)); |
| 3019 |
max_len = 2 * CS_STR_LEN_MAX; |
| 3020 |
charstrings->data = xcalloc((unsigned) max_len, sizeof(card8)); |
| 3021 |
charstring_len = 0; |
| 3022 |
prev_fd = -1; |
| 3023 |
gid = 0; |
| 3024 |
data = xcalloc(CS_STR_LEN_MAX, sizeof(card8)); |
| 3025 |
for (cid = 0; cid <= last_cid; cid++) { |
| 3026 |
unsigned short gid_org; |
| 3027 |
glyph->id = (unsigned) cid; |
| 3028 |
if (avl_find(fd->gl_tree, glyph) == NULL) |
| 3029 |
continue; |
| 3030 |
gid_org = (short unsigned) ((CIDToGIDMap[2 * cid] << 8) | (CIDToGIDMap[2 * cid + 1])); |
| 3031 |
fdsel = cff_fdselect_lookup(cffont, gid_org); |
| 3032 |
if (callback_id > 0) { |
| 3033 |
/*tex The next blob is not yet tested \unknown\ I need a font. */ |
| 3034 |
lstring * result; |
| 3035 |
run_callback(callback_id, "ddd->L", tex_font, gid_org, streamprovider, &result); |
| 3036 |
size = (size_t) result->l ; |
| 3037 |
if (size > 0) { |
| 3038 |
if (charstring_len + CS_STR_LEN_MAX >= max_len) { |
| 3039 |
max_len = (long)(charstring_len + 2 * CS_STR_LEN_MAX); |
| 3040 |
charstrings->data = xrealloc(charstrings->data, (unsigned)((unsigned)max_len*sizeof(card8))); |
| 3041 |
} |
| 3042 |
(charstrings->offset)[gid] = (unsigned)(charstring_len + 1); |
| 3043 |
cffont->offset = (l_offset)((unsigned)offset + (cs_idx->offset)[gid_org] - 1); |
| 3044 |
memcpy(charstrings->data+charstring_len,(const char *) result->s,(size_t)size); |
| 3045 |
charstring_len += size; |
| 3046 |
xfree(result); |
| 3047 |
} |
| 3048 |
} else { |
| 3049 |
size = (long) (cs_idx->offset[gid_org + 1] - cs_idx->offset[gid_org]); |
| 3050 |
if (size > CS_STR_LEN_MAX) { |
| 3051 |
formatted_error("cff","charstring too long: gid=%u, %ld bytes", cid, size); |
| 3052 |
} |
| 3053 |
if (charstring_len + CS_STR_LEN_MAX >= max_len) { |
| 3054 |
max_len = charstring_len + 2 * CS_STR_LEN_MAX; |
| 3055 |
charstrings->data = xrealloc(charstrings->data, (unsigned) ((unsigned) max_len * sizeof(card8))); |
| 3056 |
} |
| 3057 |
(charstrings->offset)[gid] = (l_offset) (charstring_len + 1); |
| 3058 |
cffont->offset = (l_offset) ((unsigned) offset + (cs_idx->offset)[gid_org] - 1); |
| 3059 |
memcpy(data, &cffont->stream[cffont->offset], (size_t) size); |
| 3060 |
charstring_len += cs_copy_charstring( |
| 3061 |
charstrings->data + charstring_len, |
| 3062 |
max_len - charstring_len, |
| 3063 |
data, size, |
| 3064 |
cffont->gsubr, (cffont->subrs)[fdsel], |
| 3065 |
0, 0, NULL, |
| 3066 |
cffont->header_major == 2 |
| 3067 |
); |
| 3068 |
} |
| 3069 |
if (cid > 0 && gid_org > 0) { |
| 3070 |
charset->data.glyphs[charset->num_entries] = (s_SID) cid; |
| 3071 |
charset->num_entries++; |
| 3072 |
} |
| 3073 |
if (fdsel != prev_fd) { |
| 3074 |
fdselect->data.ranges[fdselect->num_entries].first = gid; |
| 3075 |
fdselect->data.ranges[fdselect->num_entries].fd = (card8) fdsel; |
| 3076 |
fdselect->num_entries++; |
| 3077 |
prev_fd = fdsel; |
| 3078 |
} |
| 3079 |
gid++; |
| 3080 |
} |
| 3081 |
if (gid != num_glyphs) |
| 3082 |
formatted_error("cff","unexpected error: %i != %i", gid, num_glyphs); |
| 3083 |
xfree(data); |
| 3084 |
cff_release_index(cs_idx); |
| 3085 |
xfree(CIDToGIDMap); |
| 3086 |
(charstrings->offset)[num_glyphs] = (l_offset) (charstring_len + 1); |
| 3087 |
charstrings->count = num_glyphs; |
| 3088 |
cffont->num_glyphs = num_glyphs; |
| 3089 |
cffont->cstrings = charstrings; |
| 3090 |
cff_release_charsets(cffont->charsets); |
| 3091 |
cffont->charsets = charset; |
| 3092 |
cff_release_fdselect(cffont->fdselect); |
| 3093 |
cffont->fdselect = fdselect; |
| 3094 |
/*tex |
| 3095 |
We don't use subroutines at all. |
| 3096 |
*/ |
| 3097 |
if (cffont->gsubr) |
| 3098 |
cff_release_index(cffont->gsubr); |
| 3099 |
cffont->gsubr = cff_new_index(0); |
| 3100 |
for (fdsel = 0; fdsel < cffont->num_fds; fdsel++) { |
| 3101 |
if (cffont->subrs && cffont->subrs[fdsel]) { |
| 3102 |
cff_release_index(cffont->subrs[fdsel]); |
| 3103 |
cffont->subrs[fdsel] = NULL; |
| 3104 |
} |
| 3105 |
if (cffont->private && (cffont->private)[fdsel]) { |
| 3106 |
cff_dict_remove((cffont->private)[fdsel], "Subrs"); /* no Subrs */ |
| 3107 |
} |
| 3108 |
} |
| 3109 |
write_fontfile(pdf, cffont, fullname); |
| 3110 |
xfree(fullname); |
| 3111 |
cff_close(cffont); |
| 3112 |
} |
| 3113 |
|
| 3114 |
/*tex |
| 3115 |
|
| 3116 |
Here is a sneaky trick: fontforge knows how to convert Type1 to CFF, so I |
| 3117 |
have defined a utility function in luafflib.c that does exactly that. If it |
| 3118 |
works out ok, I will clean up this code. |
| 3119 |
|
| 3120 |
*/ |
| 3121 |
|
| 3122 |
void writetype1w(PDF pdf, fd_entry * fd) |
| 3123 |
{ |
| 3124 |
cff_font *cff; |
| 3125 |
int i; |
| 3126 |
FILE *fp; |
| 3127 |
ff_entry *ff; |
| 3128 |
unsigned char *tfm_buffer = NULL; |
| 3129 |
int tfm_size = 0; |
| 3130 |
ff = check_ff_exist(fd->fm->ff_name, 0); |
| 3131 |
fp = fopen(ff->ff_path, "rb"); |
| 3132 |
cur_file_name = ff->ff_path; |
| 3133 |
if (!fp) { |
| 3134 |
formatted_error("cff","could not open Type1 font: %s", cur_file_name); |
| 3135 |
} |
| 3136 |
fclose(fp); |
| 3137 |
if (is_subsetted(fd->fm)) { |
| 3138 |
report_start_file(filetype_subset,cur_file_name); |
| 3139 |
} else { |
| 3140 |
report_start_file(filetype_font,cur_file_name); |
| 3141 |
} |
| 3142 |
(void) ff_createcff(ff->ff_path, &tfm_buffer, &tfm_size); |
| 3143 |
if (tfm_size > 0) { |
| 3144 |
cff = read_cff(tfm_buffer, tfm_size, 0); |
| 3145 |
if (cff != NULL) { |
| 3146 |
write_cff(pdf, cff, fd); |
| 3147 |
} else { |
| 3148 |
for (i = 0; i < tfm_size; i++) |
| 3149 |
strbuf_putchar(pdf->fb, tfm_buffer[i]); |
| 3150 |
} |
| 3151 |
fd->ff_found = 1; |
| 3152 |
} else { |
| 3153 |
formatted_error("cff","could not understand Type1 font: %s",cur_file_name); |
| 3154 |
} |
| 3155 |
if (is_subsetted(fd->fm)) { |
| 3156 |
report_stop_file(filetype_subset); |
| 3157 |
} else { |
| 3158 |
report_stop_file(filetype_font); |
| 3159 |
} |
| 3160 |
cur_file_name = NULL; |
| 3161 |
} |