| 1 |
/* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki. |
| 2 |
|
| 3 |
Copyright (C) 2016 by VTeX Ltd (www.vtex.lt), |
| 4 |
the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis. |
| 5 |
|
| 6 |
Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and |
| 7 |
Shunsaku Hirata, the dvipdfmx project team. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include "system.h" |
| 30 |
#include "mem.h" |
| 31 |
#include "error.h" |
| 32 |
#include "mfileio.h" |
| 33 |
|
| 34 |
#include "cff_limits.h" |
| 35 |
#include "cff_types.h" |
| 36 |
#include "cff_stdstr.h" |
| 37 |
#include "cff_dict.h" |
| 38 |
|
| 39 |
#include "cff.h" |
| 40 |
|
| 41 |
#define CFF_DEBUG 5 |
| 42 |
#define CFF_DEBUG_STR "CFF" |
| 43 |
|
| 44 |
static unsigned long get_unsigned (FILE *stream, int n) |
| 45 |
{ |
| 46 |
unsigned long v = 0; |
| 47 |
|
| 48 |
while (n-- > 0) |
| 49 |
v = v*0x100u + get_unsigned_byte(stream); |
| 50 |
|
| 51 |
return v; |
| 52 |
} |
| 53 |
|
| 54 |
#define get_offset(s, n) get_unsigned((s), (n)) |
| 55 |
|
| 56 |
/* |
| 57 |
* Read Header, Name INDEX, Top DICT INDEX, and String INDEX. |
| 58 |
*/ |
| 59 |
cff_font *cff_open(FILE *stream, long offset, int n) |
| 60 |
{ |
| 61 |
cff_font *cff; |
| 62 |
cff_index *idx; |
| 63 |
|
| 64 |
cff = NEW(1, cff_font); |
| 65 |
|
| 66 |
cff->fontname = NULL; |
| 67 |
cff->index = n; |
| 68 |
cff->stream = stream; |
| 69 |
cff->offset = offset; |
| 70 |
cff->filter = 0; /* not used */ |
| 71 |
cff->flag = 0; |
| 72 |
|
| 73 |
cff->name = NULL; |
| 74 |
cff->topdict = NULL; |
| 75 |
cff->gsubr = NULL; |
| 76 |
cff->encoding = NULL; |
| 77 |
cff->charsets = NULL; |
| 78 |
cff->fdselect = NULL; |
| 79 |
cff->cstrings = NULL; |
| 80 |
cff->fdarray = NULL; |
| 81 |
cff->privat = NULL; |
| 82 |
cff->subrs = NULL; |
| 83 |
|
| 84 |
cff->num_glyphs = 0; |
| 85 |
cff->num_fds = 0; |
| 86 |
cff->string = NULL; |
| 87 |
cff->_string = NULL; |
| 88 |
|
| 89 |
cff_seek_set(cff, 0); |
| 90 |
cff->header.major = get_unsigned_byte(cff->stream); |
| 91 |
cff->header.minor = get_unsigned_byte(cff->stream); |
| 92 |
cff->header.hdr_size = get_unsigned_byte(cff->stream); |
| 93 |
cff->header.offsize = get_unsigned_byte(cff->stream); |
| 94 |
if (cff->header.offsize < 1 || |
| 95 |
cff->header.offsize > 4) |
| 96 |
ERROR("invalid offsize data"); |
| 97 |
|
| 98 |
if (cff->header.major > 1 || |
| 99 |
cff->header.minor > 0) { |
| 100 |
WARN("%s: CFF version %u.%u not supported.", |
| 101 |
CFF_DEBUG_STR, cff->header.major, cff->header.minor); |
| 102 |
cff_close(cff); |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
cff_seek_set(cff, (cff->header).hdr_size); |
| 107 |
|
| 108 |
/* Name INDEX */ |
| 109 |
idx = cff_get_index(cff); |
| 110 |
if (n > idx->count - 1) { |
| 111 |
WARN("%s: Invalid CFF fontset index number.", CFF_DEBUG_STR); |
| 112 |
cff_close(cff); |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
cff->name = idx; |
| 117 |
|
| 118 |
cff->fontname = cff_get_name(cff); |
| 119 |
|
| 120 |
/* Top DICT INDEX */ |
| 121 |
idx = cff_get_index(cff); |
| 122 |
if (n > idx->count - 1) |
| 123 |
ERROR("CFF Top DICT not exist..."); |
| 124 |
cff->topdict = cff_dict_unpack(idx->data + idx->offset[n] - 1, |
| 125 |
idx->data + idx->offset[n + 1] - 1); |
| 126 |
if (!cff->topdict) |
| 127 |
ERROR("Parsing CFF Top DICT data failed..."); |
| 128 |
cff_release_index(idx); |
| 129 |
|
| 130 |
if (cff_dict_known(cff->topdict, "CharstringType") && |
| 131 |
cff_dict_get(cff->topdict, "CharstringType", 0) != 2) { |
| 132 |
WARN("Only Type 2 Charstrings supported..."); |
| 133 |
cff_close(cff); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
if (cff_dict_known(cff->topdict, "SyntheticBase")) { |
| 138 |
WARN("CFF Synthetic font not supported."); |
| 139 |
cff_close(cff); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
/* String INDEX */ |
| 144 |
cff->string = cff_get_index(cff); |
| 145 |
|
| 146 |
/* offset to GSubr */ |
| 147 |
cff->gsubr_offset = tell_position(cff->stream) - offset; |
| 148 |
|
| 149 |
/* Number of glyphs */ |
| 150 |
offset = (long) cff_dict_get(cff->topdict, "CharStrings", 0); |
| 151 |
cff_seek_set(cff, offset); |
| 152 |
cff->num_glyphs = get_unsigned_pair(cff->stream); |
| 153 |
|
| 154 |
/* Check for font type */ |
| 155 |
if (cff_dict_known(cff->topdict, "ROS")) { |
| 156 |
cff->flag |= FONTTYPE_CIDFONT; |
| 157 |
} else { |
| 158 |
cff->flag |= FONTTYPE_FONT; |
| 159 |
} |
| 160 |
|
| 161 |
/* Check for encoding */ |
| 162 |
if (cff_dict_known(cff->topdict, "Encoding")) { |
| 163 |
offset = (long) cff_dict_get(cff->topdict, "Encoding", 0); |
| 164 |
if (offset == 0) { /* predefined */ |
| 165 |
cff->flag |= ENCODING_STANDARD; |
| 166 |
} else if (offset == 1) { |
| 167 |
cff->flag |= ENCODING_EXPERT; |
| 168 |
} |
| 169 |
} else { |
| 170 |
cff->flag |= ENCODING_STANDARD; |
| 171 |
} |
| 172 |
|
| 173 |
/* Check for charset */ |
| 174 |
if (cff_dict_known(cff->topdict, "charset")) { |
| 175 |
offset = (long) cff_dict_get(cff->topdict, "charset", 0); |
| 176 |
if (offset == 0) { /* predefined */ |
| 177 |
cff->flag |= CHARSETS_ISOADOBE; |
| 178 |
} else if (offset == 1) { |
| 179 |
cff->flag |= CHARSETS_EXPERT; |
| 180 |
} else if (offset == 2) { |
| 181 |
cff->flag |= CHARSETS_EXPSUB; |
| 182 |
} |
| 183 |
} else { |
| 184 |
cff->flag |= CHARSETS_ISOADOBE; |
| 185 |
} |
| 186 |
|
| 187 |
cff_seek_set(cff, cff->gsubr_offset); /* seek back to GSubr */ |
| 188 |
|
| 189 |
return cff; |
| 190 |
} |
| 191 |
|
| 192 |
void |
| 193 |
cff_close (cff_font *cff) |
| 194 |
{ |
| 195 |
card16 i; |
| 196 |
|
| 197 |
if (cff) { |
| 198 |
if (cff->fontname) RELEASE(cff->fontname); |
| 199 |
if (cff->name) cff_release_index(cff->name); |
| 200 |
if (cff->topdict) cff_release_dict(cff->topdict); |
| 201 |
if (cff->string) cff_release_index(cff->string); |
| 202 |
if (cff->gsubr) cff_release_index(cff->gsubr); |
| 203 |
if (cff->encoding) cff_release_encoding(cff->encoding); |
| 204 |
if (cff->charsets) cff_release_charsets(cff->charsets); |
| 205 |
if (cff->fdselect) cff_release_fdselect(cff->fdselect); |
| 206 |
if (cff->cstrings) cff_release_index(cff->cstrings); |
| 207 |
if (cff->fdarray) { |
| 208 |
for (i=0;i<cff->num_fds;i++) { |
| 209 |
if (cff->fdarray[i]) cff_release_dict(cff->fdarray[i]); |
| 210 |
} |
| 211 |
RELEASE(cff->fdarray); |
| 212 |
} |
| 213 |
if (cff->privat) { |
| 214 |
for (i=0;i<cff->num_fds;i++) { |
| 215 |
if (cff->privat[i]) cff_release_dict(cff->privat[i]); |
| 216 |
} |
| 217 |
RELEASE(cff->privat); |
| 218 |
} |
| 219 |
if (cff->subrs) { |
| 220 |
for (i=0;i<cff->num_fds;i++) { |
| 221 |
if (cff->subrs[i]) cff_release_index(cff->subrs[i]); |
| 222 |
} |
| 223 |
RELEASE(cff->subrs); |
| 224 |
} |
| 225 |
if (cff->_string) |
| 226 |
cff_release_index(cff->_string); |
| 227 |
|
| 228 |
RELEASE(cff); |
| 229 |
} |
| 230 |
|
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
char * |
| 235 |
cff_get_name (cff_font *cff) |
| 236 |
{ |
| 237 |
char *fontname; |
| 238 |
l_offset len; |
| 239 |
cff_index *idx; |
| 240 |
|
| 241 |
idx = cff->name; |
| 242 |
len = idx->offset[cff->index + 1] - idx->offset[cff->index]; |
| 243 |
fontname = NEW(len + 1, char); |
| 244 |
memcpy(fontname, idx->data + idx->offset[cff->index] - 1, len); |
| 245 |
fontname[len] = '\0'; |
| 246 |
|
| 247 |
return fontname; |
| 248 |
} |
| 249 |
|
| 250 |
long |
| 251 |
cff_set_name (cff_font *cff, char *name) |
| 252 |
{ |
| 253 |
cff_index *idx; |
| 254 |
|
| 255 |
if (strlen(name) > 127) |
| 256 |
ERROR("FontName string length too large..."); |
| 257 |
|
| 258 |
if (cff->name) |
| 259 |
cff_release_index(cff->name); |
| 260 |
|
| 261 |
cff->name = idx = NEW(1, cff_index); |
| 262 |
idx->count = 1; |
| 263 |
idx->offsize = 1; |
| 264 |
idx->offset = NEW(2, l_offset); |
| 265 |
(idx->offset)[0] = 1; |
| 266 |
(idx->offset)[1] = strlen(name) + 1; |
| 267 |
idx->data = NEW(strlen(name), card8); |
| 268 |
memmove(idx->data, name, strlen(name)); /* no trailing '\0' */ |
| 269 |
|
| 270 |
return 5 + strlen(name); |
| 271 |
} |
| 272 |
|
| 273 |
long |
| 274 |
cff_put_header (cff_font *cff, card8 *dest, long destlen) |
| 275 |
{ |
| 276 |
if (destlen < 4) |
| 277 |
ERROR("Not enough space available..."); |
| 278 |
|
| 279 |
*(dest++) = cff->header.major; |
| 280 |
*(dest++) = cff->header.minor; |
| 281 |
*(dest++) = 4; /* Additional data in between header and |
| 282 |
* Name INDEX ignored. |
| 283 |
*/ |
| 284 |
/* We will set all offset (0) to four-byte integer. */ |
| 285 |
*(dest++) = 4; |
| 286 |
cff->header.offsize = 4; |
| 287 |
|
| 288 |
return 4; |
| 289 |
} |
| 290 |
|
| 291 |
/* Only read header part but not body */ |
| 292 |
cff_index * |
| 293 |
cff_get_index_header (cff_font *cff) |
| 294 |
{ |
| 295 |
cff_index *idx; |
| 296 |
card16 i, count; |
| 297 |
|
| 298 |
idx = NEW(1, cff_index); |
| 299 |
|
| 300 |
idx->count = count = get_unsigned_pair(cff->stream); |
| 301 |
if (count > 0) { |
| 302 |
idx->offsize = get_unsigned_byte(cff->stream); |
| 303 |
if (idx->offsize < 1 || idx->offsize > 4) |
| 304 |
ERROR("invalid offsize data"); |
| 305 |
|
| 306 |
idx->offset = NEW(count+1, l_offset); |
| 307 |
for (i=0;i<count;i++) { |
| 308 |
(idx->offset)[i] = get_offset(cff->stream, idx->offsize); |
| 309 |
} |
| 310 |
if (count == 0xFFFF) |
| 311 |
cff_seek(cff, cff_tell(cff) + idx->offsize); |
| 312 |
else |
| 313 |
(idx->offset)[i] = get_offset(cff->stream, idx->offsize); |
| 314 |
|
| 315 |
if (idx->offset[0] != 1) |
| 316 |
ERROR("cff_get_index(): invalid index data"); |
| 317 |
|
| 318 |
idx->data = NULL; |
| 319 |
} else { |
| 320 |
idx->offsize = 0; |
| 321 |
idx->offset = NULL; |
| 322 |
idx->data = NULL; |
| 323 |
} |
| 324 |
|
| 325 |
return idx; |
| 326 |
} |
| 327 |
|
| 328 |
cff_index * |
| 329 |
cff_get_index (cff_font *cff) |
| 330 |
{ |
| 331 |
cff_index *idx; |
| 332 |
card16 i, count; |
| 333 |
long length, nb_read, offset; |
| 334 |
|
| 335 |
idx = NEW(1, cff_index); |
| 336 |
|
| 337 |
idx->count = count = get_unsigned_pair(cff->stream); |
| 338 |
if (count > 0) { |
| 339 |
idx->offsize = get_unsigned_byte(cff->stream); |
| 340 |
if (idx->offsize < 1 || idx->offsize > 4) |
| 341 |
ERROR("invalid offsize data"); |
| 342 |
|
| 343 |
idx->offset = NEW(count + 1, l_offset); |
| 344 |
for (i = 0 ; i < count + 1; i++) { |
| 345 |
idx->offset[i] = get_offset(cff->stream, idx->offsize); |
| 346 |
} |
| 347 |
|
| 348 |
if (idx->offset[0] != 1) |
| 349 |
ERROR("Invalid CFF Index offset data"); |
| 350 |
|
| 351 |
length = idx->offset[count] - idx->offset[0]; |
| 352 |
|
| 353 |
idx->data = NEW(length, card8); |
| 354 |
offset = 0; |
| 355 |
while (length > 0) { |
| 356 |
nb_read = cff_read_data(idx->data + offset, length, cff); |
| 357 |
offset += nb_read; |
| 358 |
length -= nb_read; |
| 359 |
} |
| 360 |
} else { |
| 361 |
idx->offsize = 0; |
| 362 |
idx->offset = NULL; |
| 363 |
idx->data = NULL; |
| 364 |
} |
| 365 |
|
| 366 |
return idx; |
| 367 |
} |
| 368 |
|
| 369 |
long |
| 370 |
cff_pack_index (cff_index *idx, card8 *dest, long destlen) |
| 371 |
{ |
| 372 |
long len = 0; |
| 373 |
long datalen; |
| 374 |
card16 i; |
| 375 |
|
| 376 |
if (idx->count < 1) { |
| 377 |
if (destlen < 2) |
| 378 |
ERROR("Not enough space available..."); |
| 379 |
memset(dest, 0, 2); |
| 380 |
return 2; |
| 381 |
} |
| 382 |
|
| 383 |
len = cff_index_size(idx); |
| 384 |
datalen = idx->offset[idx->count] - 1; |
| 385 |
|
| 386 |
if (destlen < len) |
| 387 |
ERROR("Not enough space available..."); |
| 388 |
|
| 389 |
*(dest++) = (idx->count >> 8) & 0xff; |
| 390 |
*(dest++) = idx->count & 0xff; |
| 391 |
|
| 392 |
if (datalen < 0xffUL) { |
| 393 |
idx->offsize = 1; |
| 394 |
*(dest++) = 1; |
| 395 |
for (i = 0; i <= idx->count; i++) { |
| 396 |
*(dest++) = (card8) (idx->offset[i] & 0xff); |
| 397 |
} |
| 398 |
} else if (datalen < 0xffffUL) { |
| 399 |
idx->offsize = 2; |
| 400 |
*(dest++) = 2; |
| 401 |
for (i = 0; i <= idx->count; i++) { |
| 402 |
*(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff); |
| 403 |
*(dest++) = (card8) ( idx->offset[i] & 0xff); |
| 404 |
} |
| 405 |
} else if (datalen < 0xffffffUL) { |
| 406 |
idx->offsize = 3; |
| 407 |
*(dest++) = 3; |
| 408 |
for (i = 0; i <= idx->count; i++) { |
| 409 |
*(dest++) = (card8)((idx->offset[i] >> 16) & 0xff); |
| 410 |
*(dest++) = (card8)((idx->offset[i] >> 8) & 0xff); |
| 411 |
*(dest++) = (card8)(idx->offset[i] & 0xff); |
| 412 |
} |
| 413 |
} else { |
| 414 |
idx->offsize = 4; |
| 415 |
*(dest++) = 4; |
| 416 |
for (i = 0; i <= idx->count; i++) { |
| 417 |
*(dest++) = (card8)((idx->offset[i] >> 24) & 0xff); |
| 418 |
*(dest++) = (card8)((idx->offset[i] >> 16) & 0xff); |
| 419 |
*(dest++) = (card8)((idx->offset[i] >> 8) & 0xff); |
| 420 |
*(dest++) = (card8)(idx->offset[i] & 0xff); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
memmove(dest, idx->data, idx->offset[idx->count] - 1); |
| 425 |
|
| 426 |
return len; |
| 427 |
} |
| 428 |
|
| 429 |
long |
| 430 |
cff_index_size (cff_index *idx) |
| 431 |
{ |
| 432 |
if (idx->count > 0) { |
| 433 |
l_offset datalen; |
| 434 |
|
| 435 |
datalen = idx->offset[idx->count] - 1; |
| 436 |
if (datalen < 0xffUL) { |
| 437 |
idx->offsize = 1; |
| 438 |
} else if (datalen < 0xffffUL) { |
| 439 |
idx->offsize = 2; |
| 440 |
} else if (datalen < 0xffffffUL) { |
| 441 |
idx->offsize = 3; |
| 442 |
} else { |
| 443 |
idx->offsize = 4; |
| 444 |
} |
| 445 |
return (3 + (idx->offsize)*(idx->count + 1) + datalen); |
| 446 |
} else { |
| 447 |
return 2; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
cff_index *cff_new_index (card16 count) |
| 453 |
{ |
| 454 |
cff_index *idx; |
| 455 |
|
| 456 |
idx = NEW(1, cff_index); |
| 457 |
idx->count = count; |
| 458 |
idx->offsize = 0; |
| 459 |
|
| 460 |
if (count > 0) { |
| 461 |
idx->offset = NEW(count + 1, l_offset); |
| 462 |
(idx->offset)[0] = 1; |
| 463 |
} else { |
| 464 |
idx->offset = NULL; |
| 465 |
} |
| 466 |
idx->data = NULL; |
| 467 |
|
| 468 |
return idx; |
| 469 |
} |
| 470 |
|
| 471 |
void cff_release_index (cff_index *idx) |
| 472 |
{ |
| 473 |
if (idx) { |
| 474 |
if (idx->data) { |
| 475 |
RELEASE(idx->data); |
| 476 |
} |
| 477 |
if (idx->offset) { |
| 478 |
RELEASE(idx->offset); |
| 479 |
} |
| 480 |
RELEASE(idx); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/* Strings */ |
| 485 |
char *cff_get_string (cff_font *cff, s_SID id) |
| 486 |
{ |
| 487 |
char *result = NULL; |
| 488 |
long len; |
| 489 |
|
| 490 |
if (id < CFF_STDSTR_MAX) { |
| 491 |
len = strlen(cff_stdstr[id]); |
| 492 |
result = NEW(len+1, char); |
| 493 |
memcpy(result, cff_stdstr[id], len); |
| 494 |
result[len] = '\0'; |
| 495 |
} else if (cff && cff->string) { |
| 496 |
cff_index *strings = cff->string; |
| 497 |
id -= CFF_STDSTR_MAX; |
| 498 |
if (id < strings->count) { |
| 499 |
len = (strings->offset)[id+1] - (strings->offset)[id]; |
| 500 |
result = NEW(len + 1, char); |
| 501 |
memmove(result, strings->data + (strings->offset)[id] - 1, len); |
| 502 |
result[len] = '\0'; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
return result; |
| 507 |
} |
| 508 |
|
| 509 |
long cff_get_sid (cff_font *cff, const char *str) |
| 510 |
{ |
| 511 |
card16 i; |
| 512 |
|
| 513 |
if (!cff || !str) |
| 514 |
return -1; |
| 515 |
|
| 516 |
/* I search String INDEX first. */ |
| 517 |
if (cff && cff->string) { |
| 518 |
cff_index *idx = cff->string; |
| 519 |
for (i = 0; i < idx->count; i++) { |
| 520 |
if (strlen(str) == (idx->offset)[i+1] - (idx->offset)[i] && |
| 521 |
!memcmp(str, (idx->data)+(idx->offset)[i]-1, strlen(str))) |
| 522 |
return (i + CFF_STDSTR_MAX); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
for (i = 0; i < CFF_STDSTR_MAX; i++) { |
| 527 |
if (!strcmp(str, cff_stdstr[i])) |
| 528 |
return i; |
| 529 |
} |
| 530 |
|
| 531 |
return -1; |
| 532 |
} |
| 533 |
|
| 534 |
long cff_get_seac_sid (cff_font *cff, const char *str) |
| 535 |
{ |
| 536 |
card16 i; |
| 537 |
|
| 538 |
if (!cff || !str) |
| 539 |
return -1; |
| 540 |
|
| 541 |
for (i = 0; i < CFF_STDSTR_MAX; i++) { |
| 542 |
if (!strcmp(str, cff_stdstr[i])) |
| 543 |
return i; |
| 544 |
} |
| 545 |
|
| 546 |
return -1; |
| 547 |
} |
| 548 |
|
| 549 |
int cff_match_string (cff_font *cff, const char *str, s_SID sid) |
| 550 |
{ |
| 551 |
card16 i; |
| 552 |
|
| 553 |
if (sid < CFF_STDSTR_MAX) { |
| 554 |
return ((!strcmp(str, cff_stdstr[sid])) ? 1 : 0); |
| 555 |
} else { |
| 556 |
i = sid - CFF_STDSTR_MAX; |
| 557 |
if (cff == NULL || cff->string == NULL || i >= cff->string->count) |
| 558 |
ERROR("Invalid SID"); |
| 559 |
if (strlen(str) == (cff->string->offset)[i+1] - (cff->string->offset)[i]) |
| 560 |
return (!memcmp(str, |
| 561 |
(cff->string->data)+(cff->string->offset)[i]-1, |
| 562 |
strlen(str))) ? 1 : 0; |
| 563 |
} |
| 564 |
|
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
void cff_update_string (cff_font *cff) |
| 569 |
{ |
| 570 |
if (cff == NULL) |
| 571 |
ERROR("CFF font not opened."); |
| 572 |
|
| 573 |
if (cff->string) |
| 574 |
cff_release_index(cff->string); |
| 575 |
cff->string = cff->_string; |
| 576 |
cff->_string = NULL; |
| 577 |
} |
| 578 |
|
| 579 |
s_SID cff_add_string (cff_font *cff, const char *str, int unique) |
| 580 |
/* Setting unique == 1 eliminates redundant or predefined strings. */ |
| 581 |
{ |
| 582 |
card16 idx; |
| 583 |
cff_index *strings; |
| 584 |
l_offset offset, size; |
| 585 |
long len = strlen(str); |
| 586 |
|
| 587 |
if (cff == NULL) |
| 588 |
ERROR("CFF font not opened."); |
| 589 |
|
| 590 |
if (cff->_string == NULL) |
| 591 |
cff->_string = cff_new_index(0); |
| 592 |
strings = cff->_string; |
| 593 |
|
| 594 |
if (unique) { |
| 595 |
/* TODO: do binary search to speed things up */ |
| 596 |
for (idx = 0; idx < CFF_STDSTR_MAX; idx++) { |
| 597 |
if (cff_stdstr[idx] && !strcmp(cff_stdstr[idx], str)) |
| 598 |
return idx; |
| 599 |
} |
| 600 |
for (idx = 0; idx < strings->count; idx++) { |
| 601 |
size = strings->offset[idx+1] - strings->offset[idx]; |
| 602 |
offset = strings->offset[idx]; |
| 603 |
if (size == len && !memcmp(strings->data+offset-1, str, len)) |
| 604 |
return (idx + CFF_STDSTR_MAX); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
offset = (strings->count > 0) ? strings->offset[strings->count] : 1; |
| 609 |
strings->offset = RENEW(strings->offset, strings->count+2, l_offset); |
| 610 |
if (strings->count == 0) |
| 611 |
strings->offset[0] = 1; |
| 612 |
idx = strings->count; |
| 613 |
strings->count += 1; |
| 614 |
strings->offset[strings->count] = offset + len; |
| 615 |
strings->data = RENEW(strings->data, offset+len-1, card8); |
| 616 |
memcpy(strings->data+offset-1, str, len); |
| 617 |
|
| 618 |
return (idx + CFF_STDSTR_MAX); |
| 619 |
} |
| 620 |
|
| 621 |
/* |
| 622 |
* Encoding and Charset |
| 623 |
* |
| 624 |
* Encoding and Charset arrays always begin with GID = 1. |
| 625 |
*/ |
| 626 |
long cff_read_encoding (cff_font *cff) |
| 627 |
{ |
| 628 |
cff_encoding *encoding; |
| 629 |
long offset, length; |
| 630 |
card8 i; |
| 631 |
|
| 632 |
if (cff->topdict == NULL) { |
| 633 |
ERROR("Top DICT data not found"); |
| 634 |
} |
| 635 |
|
| 636 |
if (!cff_dict_known(cff->topdict, "Encoding")) { |
| 637 |
cff->flag |= ENCODING_STANDARD; |
| 638 |
cff->encoding = NULL; |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
offset = (long) cff_dict_get(cff->topdict, "Encoding", 0); |
| 643 |
if (offset == 0) { /* predefined */ |
| 644 |
cff->flag |= ENCODING_STANDARD; |
| 645 |
cff->encoding = NULL; |
| 646 |
return 0; |
| 647 |
} else if (offset == 1) { |
| 648 |
cff->flag |= ENCODING_EXPERT; |
| 649 |
cff->encoding = NULL; |
| 650 |
return 0; |
| 651 |
} |
| 652 |
|
| 653 |
cff_seek_set(cff, offset); |
| 654 |
cff->encoding = encoding = NEW(1, cff_encoding); |
| 655 |
encoding->format = get_unsigned_byte(cff->stream); |
| 656 |
length = 1; |
| 657 |
|
| 658 |
switch (encoding->format & (~0x80)) { |
| 659 |
case 0: |
| 660 |
encoding->num_entries = get_unsigned_byte(cff->stream); |
| 661 |
(encoding->data).codes = NEW(encoding->num_entries, card8); |
| 662 |
for (i=0;i<(encoding->num_entries);i++) { |
| 663 |
(encoding->data).codes[i] = get_unsigned_byte(cff->stream); |
| 664 |
} |
| 665 |
length += encoding->num_entries + 1; |
| 666 |
break; |
| 667 |
case 1: |
| 668 |
{ |
| 669 |
cff_range1 *ranges; |
| 670 |
encoding->num_entries = get_unsigned_byte(cff->stream); |
| 671 |
encoding->data.range1 = ranges |
| 672 |
= NEW(encoding->num_entries, cff_range1); |
| 673 |
for (i=0;i<(encoding->num_entries);i++) { |
| 674 |
ranges[i].first = get_unsigned_byte(cff->stream); |
| 675 |
ranges[i].n_left = get_unsigned_byte(cff->stream); |
| 676 |
} |
| 677 |
length += (encoding->num_entries) * 2 + 1; |
| 678 |
} |
| 679 |
break; |
| 680 |
default: |
| 681 |
RELEASE(encoding); |
| 682 |
ERROR("Unknown Encoding format"); |
| 683 |
break; |
| 684 |
} |
| 685 |
|
| 686 |
/* Supplementary data */ |
| 687 |
if ((encoding->format) & 0x80) { |
| 688 |
cff_map *map; |
| 689 |
encoding->num_supps = get_unsigned_byte(cff->stream); |
| 690 |
encoding->supp = map = NEW(encoding->num_supps, cff_map); |
| 691 |
for (i=0;i<(encoding->num_supps);i++) { |
| 692 |
map[i].code = get_unsigned_byte(cff->stream); |
| 693 |
map[i].glyph = get_unsigned_pair(cff->stream); /* SID */ |
| 694 |
} |
| 695 |
length += (encoding->num_supps) * 3 + 1; |
| 696 |
} else { |
| 697 |
encoding->num_supps = 0; |
| 698 |
encoding->supp = NULL; |
| 699 |
} |
| 700 |
|
| 701 |
return length; |
| 702 |
} |
| 703 |
|
| 704 |
long cff_pack_encoding (cff_font *cff, card8 *dest, long destlen) |
| 705 |
{ |
| 706 |
long len = 0; |
| 707 |
cff_encoding *encoding; |
| 708 |
card16 i; |
| 709 |
|
| 710 |
if (cff->flag & HAVE_STANDARD_ENCODING || cff->encoding == NULL) |
| 711 |
return 0; |
| 712 |
|
| 713 |
if (destlen < 2) |
| 714 |
ERROR("in cff_pack_encoding(): Buffer overflow"); |
| 715 |
|
| 716 |
encoding = cff->encoding; |
| 717 |
|
| 718 |
dest[len++] = encoding->format; |
| 719 |
dest[len++] = encoding->num_entries; |
| 720 |
switch (encoding->format & (~0x80)) { |
| 721 |
case 0: |
| 722 |
if (destlen < len + encoding->num_entries) |
| 723 |
ERROR("in cff_pack_encoding(): Buffer overflow"); |
| 724 |
for (i=0;i<(encoding->num_entries);i++) { |
| 725 |
dest[len++] = (encoding->data).codes[i]; |
| 726 |
} |
| 727 |
break; |
| 728 |
case 1: |
| 729 |
{ |
| 730 |
if (destlen < len + (encoding->num_entries)*2) |
| 731 |
ERROR("in cff_pack_encoding(): Buffer overflow"); |
| 732 |
for (i=0;i<(encoding->num_entries);i++) { |
| 733 |
dest[len++] = (encoding->data).range1[i].first & 0xff; |
| 734 |
dest[len++] = (encoding->data).range1[i].n_left; |
| 735 |
} |
| 736 |
} |
| 737 |
break; |
| 738 |
default: |
| 739 |
ERROR("Unknown Encoding format"); |
| 740 |
break; |
| 741 |
} |
| 742 |
|
| 743 |
if ((encoding->format) & 0x80) { |
| 744 |
if (destlen < len + (encoding->num_supps)*3 + 1) |
| 745 |
ERROR("in cff_pack_encoding(): Buffer overflow"); |
| 746 |
dest[len++] = encoding->num_supps; |
| 747 |
for (i=0;i<(encoding->num_supps);i++) { |
| 748 |
dest[len++] = (encoding->supp)[i].code; |
| 749 |
dest[len++] = ((encoding->supp)[i].glyph >> 8) & 0xff; |
| 750 |
dest[len++] = (encoding->supp)[i].glyph & 0xff; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
return len; |
| 755 |
} |
| 756 |
|
| 757 |
/* input: code, output: glyph index */ |
| 758 |
card16 cff_encoding_lookup (cff_font *cff, card8 code) |
| 759 |
{ |
| 760 |
card16 gid = 0; |
| 761 |
cff_encoding *encoding; |
| 762 |
card16 i; |
| 763 |
|
| 764 |
if (cff->flag & (ENCODING_STANDARD|ENCODING_EXPERT)) { |
| 765 |
ERROR("Predefined CFF encoding not supported yet"); |
| 766 |
} else if (cff->encoding == NULL) { |
| 767 |
ERROR("Encoding data not available"); |
| 768 |
} |
| 769 |
|
| 770 |
encoding = cff->encoding; |
| 771 |
|
| 772 |
gid = 0; |
| 773 |
switch (encoding->format & (~0x80)) { |
| 774 |
case 0: |
| 775 |
for (i = 0; i < encoding->num_entries; i++) { |
| 776 |
if (code == (encoding->data).codes[i]) { |
| 777 |
gid = i + 1; |
| 778 |
break; |
| 779 |
} |
| 780 |
} |
| 781 |
break; |
| 782 |
case 1: |
| 783 |
for (i = 0; i < encoding->num_entries; i++) { |
| 784 |
if (code >= (encoding->data).range1[i].first && |
| 785 |
code <= (encoding->data).range1[i].first + (encoding->data).range1[i].n_left) { |
| 786 |
gid += code - (encoding->data).range1[i].first + 1; |
| 787 |
break; |
| 788 |
} |
| 789 |
gid += (encoding->data).range1[i].n_left + 1; |
| 790 |
} |
| 791 |
if (i == encoding->num_entries) |
| 792 |
gid = 0; |
| 793 |
break; |
| 794 |
default: |
| 795 |
ERROR("Unknown Encoding format."); |
| 796 |
} |
| 797 |
|
| 798 |
/* Supplementary data */ |
| 799 |
if (gid == 0 && ((encoding->format) & 0x80)) { |
| 800 |
cff_map *map; |
| 801 |
if (!encoding->supp) |
| 802 |
ERROR("No CFF supplementary encoding data read."); |
| 803 |
map = encoding->supp; |
| 804 |
for (i=0;i<(encoding->num_supps);i++) { |
| 805 |
if (code == map[i].code) { |
| 806 |
gid = cff_charsets_lookup(cff, map[i].glyph); |
| 807 |
break; |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
return gid; |
| 813 |
} |
| 814 |
|
| 815 |
void cff_release_encoding (cff_encoding *encoding) |
| 816 |
{ |
| 817 |
if (encoding) { |
| 818 |
switch (encoding->format & (~0x80)) { |
| 819 |
case 0: |
| 820 |
if (encoding->data.codes) |
| 821 |
RELEASE(encoding->data.codes); |
| 822 |
break; |
| 823 |
case 1: |
| 824 |
if (encoding->data.range1) |
| 825 |
RELEASE(encoding->data.range1); |
| 826 |
break; |
| 827 |
default: |
| 828 |
ERROR("Unknown Encoding format."); |
| 829 |
} |
| 830 |
if (encoding->format & 0x80) { |
| 831 |
if (encoding->supp) |
| 832 |
RELEASE(encoding->supp); |
| 833 |
} |
| 834 |
RELEASE(encoding); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
long cff_read_charsets (cff_font *cff) |
| 839 |
{ |
| 840 |
cff_charsets *charset; |
| 841 |
long offset, length; |
| 842 |
card16 count, i; |
| 843 |
|
| 844 |
if (cff->topdict == NULL) |
| 845 |
ERROR("Top DICT not available"); |
| 846 |
|
| 847 |
if (!cff_dict_known(cff->topdict, "charset")) { |
| 848 |
cff->flag |= CHARSETS_ISOADOBE; |
| 849 |
cff->charsets = NULL; |
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
offset = (long) cff_dict_get(cff->topdict, "charset", 0); |
| 854 |
|
| 855 |
if (offset == 0) { /* predefined */ |
| 856 |
cff->flag |= CHARSETS_ISOADOBE; |
| 857 |
cff->charsets = NULL; |
| 858 |
return 0; |
| 859 |
} else if (offset == 1) { |
| 860 |
cff->flag |= CHARSETS_EXPERT; |
| 861 |
cff->charsets = NULL; |
| 862 |
return 0; |
| 863 |
} else if (offset == 2) { |
| 864 |
cff->flag |= CHARSETS_EXPSUB; |
| 865 |
cff->charsets = NULL; |
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
cff_seek_set(cff, offset); |
| 870 |
cff->charsets = charset = NEW(1, cff_charsets); |
| 871 |
charset->format = get_unsigned_byte(cff->stream); |
| 872 |
charset->num_entries = 0; |
| 873 |
|
| 874 |
count = cff->num_glyphs - 1; |
| 875 |
length = 1; |
| 876 |
|
| 877 |
/* Not sure. Not well documented. */ |
| 878 |
switch (charset->format) { |
| 879 |
case 0: |
| 880 |
charset->num_entries = cff->num_glyphs - 1; /* no .notdef */ |
| 881 |
charset->data.glyphs = NEW(charset->num_entries, s_SID); |
| 882 |
length += (charset->num_entries) * 2; |
| 883 |
for (i=0;i<(charset->num_entries);i++) { |
| 884 |
charset->data.glyphs[i] = get_unsigned_pair(cff->stream); |
| 885 |
} |
| 886 |
count = 0; |
| 887 |
break; |
| 888 |
case 1: |
| 889 |
{ |
| 890 |
cff_range1 *ranges = NULL; |
| 891 |
while (count > 0 && charset->num_entries < cff->num_glyphs) { |
| 892 |
ranges = RENEW(ranges, charset->num_entries + 1, cff_range1); |
| 893 |
ranges[charset->num_entries].first = get_unsigned_pair(cff->stream); |
| 894 |
ranges[charset->num_entries].n_left = get_unsigned_byte(cff->stream); |
| 895 |
count -= ranges[charset->num_entries].n_left + 1; /* no-overrap */ |
| 896 |
charset->num_entries += 1; |
| 897 |
charset->data.range1 = ranges; |
| 898 |
} |
| 899 |
length += (charset->num_entries) * 3; |
| 900 |
} |
| 901 |
break; |
| 902 |
case 2: |
| 903 |
{ |
| 904 |
cff_range2 *ranges = NULL; |
| 905 |
while (count > 0 && charset->num_entries < cff->num_glyphs) { |
| 906 |
ranges = RENEW(ranges, charset->num_entries + 1, cff_range2); |
| 907 |
ranges[charset->num_entries].first = get_unsigned_pair(cff->stream); |
| 908 |
ranges[charset->num_entries].n_left = get_unsigned_pair(cff->stream); |
| 909 |
count -= ranges[charset->num_entries].n_left + 1; /* non-overrapping */ |
| 910 |
charset->num_entries += 1; |
| 911 |
} |
| 912 |
charset->data.range2 = ranges; |
| 913 |
length += (charset->num_entries) * 4; |
| 914 |
} |
| 915 |
break; |
| 916 |
default: |
| 917 |
RELEASE(charset); |
| 918 |
ERROR("Unknown Charset format"); |
| 919 |
break; |
| 920 |
} |
| 921 |
|
| 922 |
if (count > 0) |
| 923 |
ERROR("Charset data possibly broken"); |
| 924 |
|
| 925 |
return length; |
| 926 |
} |
| 927 |
|
| 928 |
long cff_pack_charsets (cff_font *cff, card8 *dest, long destlen) |
| 929 |
{ |
| 930 |
long len = 0; |
| 931 |
card16 i; |
| 932 |
cff_charsets *charset; |
| 933 |
|
| 934 |
if (cff->flag & HAVE_STANDARD_CHARSETS || cff->charsets == NULL) |
| 935 |
return 0; |
| 936 |
|
| 937 |
if (destlen < 1) |
| 938 |
ERROR("in cff_pack_charsets(): Buffer overflow"); |
| 939 |
|
| 940 |
charset = cff->charsets; |
| 941 |
|
| 942 |
dest[len++] = charset->format; |
| 943 |
switch (charset->format) { |
| 944 |
case 0: |
| 945 |
if (destlen < len + (charset->num_entries)*2) |
| 946 |
ERROR("in cff_pack_charsets(): Buffer overflow"); |
| 947 |
for (i=0;i<(charset->num_entries);i++) { |
| 948 |
s_SID sid = (charset->data).glyphs[i]; /* or CID */ |
| 949 |
dest[len++] = (sid >> 8) & 0xff; |
| 950 |
dest[len++] = sid & 0xff; |
| 951 |
} |
| 952 |
break; |
| 953 |
case 1: |
| 954 |
{ |
| 955 |
if (destlen < len + (charset->num_entries)*3) |
| 956 |
ERROR("in cff_pack_charsets(): Buffer overflow"); |
| 957 |
for (i=0;i<(charset->num_entries);i++) { |
| 958 |
dest[len++] = ((charset->data).range1[i].first >> 8) & 0xff; |
| 959 |
dest[len++] = (charset->data).range1[i].first & 0xff; |
| 960 |
dest[len++] = (charset->data).range1[i].n_left; |
| 961 |
} |
| 962 |
} |
| 963 |
break; |
| 964 |
case 2: |
| 965 |
{ |
| 966 |
if (destlen < len + (charset->num_entries)*4) |
| 967 |
ERROR("in cff_pack_charsets(): Buffer overflow"); |
| 968 |
for (i=0;i<(charset->num_entries);i++) { |
| 969 |
dest[len++] = ((charset->data).range2[i].first >> 8) & 0xff; |
| 970 |
dest[len++] = (charset->data).range2[i].first & 0xff; |
| 971 |
dest[len++] = ((charset->data).range2[i].n_left >> 8) & 0xff; |
| 972 |
dest[len++] = (charset->data).range2[i].n_left & 0xff; |
| 973 |
} |
| 974 |
} |
| 975 |
break; |
| 976 |
default: |
| 977 |
ERROR("Unknown Charset format"); |
| 978 |
break; |
| 979 |
} |
| 980 |
|
| 981 |
return len; |
| 982 |
} |
| 983 |
|
| 984 |
char* cff_get_glyphname (cff_font *cff, card16 gid) |
| 985 |
{ |
| 986 |
s_SID sid; |
| 987 |
|
| 988 |
sid = cff_charsets_lookup_inverse(cff, gid); |
| 989 |
return cff_get_string(cff, sid); |
| 990 |
} |
| 991 |
|
| 992 |
card16 cff_glyph_lookup (cff_font *cff, const char *glyph) |
| 993 |
{ |
| 994 |
card16 gid; |
| 995 |
cff_charsets *charset; |
| 996 |
card16 i, n; |
| 997 |
|
| 998 |
if (cff->flag & (CHARSETS_ISOADOBE|CHARSETS_EXPERT|CHARSETS_EXPSUB)) { |
| 999 |
ERROR("Predefined CFF charsets not supported yet"); |
| 1000 |
} else if (cff->charsets == NULL) { |
| 1001 |
ERROR("Charsets data not available"); |
| 1002 |
} |
| 1003 |
|
| 1004 |
/* .notdef always have glyph index 0 */ |
| 1005 |
if (!glyph || !strcmp(glyph, ".notdef")) { |
| 1006 |
return 0; |
| 1007 |
} |
| 1008 |
|
| 1009 |
charset = cff->charsets; |
| 1010 |
|
| 1011 |
gid = 0; |
| 1012 |
switch (charset->format) { |
| 1013 |
case 0: |
| 1014 |
for (i = 0; i < charset->num_entries; i++) { |
| 1015 |
gid++; |
| 1016 |
if (cff_match_string(cff, glyph, charset->data.glyphs[i])) { |
| 1017 |
return gid; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
break; |
| 1021 |
case 1: |
| 1022 |
for (i = 0; i < charset->num_entries; i++) { |
| 1023 |
for (n = 0; |
| 1024 |
n <= charset->data.range1[i].n_left; n++) { |
| 1025 |
gid++; |
| 1026 |
if (cff_match_string(cff, glyph, |
| 1027 |
(s_SID)(charset->data.range1[i].first + n))) { |
| 1028 |
return gid; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
break; |
| 1033 |
case 2: |
| 1034 |
for (i = 0; i <charset->num_entries; i++) { |
| 1035 |
for (n = 0; |
| 1036 |
n <= charset->data.range2[i].n_left; n++) { |
| 1037 |
gid++; |
| 1038 |
if (cff_match_string(cff, glyph, |
| 1039 |
(s_SID)(charset->data.range2[i].first + n))) { |
| 1040 |
return gid; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
break; |
| 1045 |
default: |
| 1046 |
ERROR("Unknown Charset format"); |
| 1047 |
} |
| 1048 |
|
| 1049 |
return 0; /* not found, returns .notdef */ |
| 1050 |
} |
| 1051 |
|
| 1052 |
/* Input : SID or CID (16-bit unsigned int) |
| 1053 |
* Output: glyph index |
| 1054 |
*/ |
| 1055 |
card16 |
| 1056 |
cff_charsets_lookup (cff_font *cff, card16 cid) |
| 1057 |
{ |
| 1058 |
if (cff->flag & (CHARSETS_ISOADOBE|CHARSETS_EXPERT|CHARSETS_EXPSUB)) { |
| 1059 |
ERROR("Predefined CFF charsets not supported yet"); |
| 1060 |
} else if (cff->charsets == NULL) { |
| 1061 |
ERROR("Charsets data not available"); |
| 1062 |
} |
| 1063 |
|
| 1064 |
return cff_charsets_lookup_gid(cff->charsets, cid); |
| 1065 |
} |
| 1066 |
|
| 1067 |
card16 cff_charsets_lookup_gid (cff_charsets *charset, card16 cid) |
| 1068 |
{ |
| 1069 |
card16 gid = 0; |
| 1070 |
card16 i; |
| 1071 |
|
| 1072 |
if (cid == 0) { |
| 1073 |
return 0; /* GID 0 (.notdef) */ |
| 1074 |
} |
| 1075 |
|
| 1076 |
switch (charset->format) { |
| 1077 |
case 0: |
| 1078 |
for (i = 0; i <charset->num_entries; i++) { |
| 1079 |
if (cid == charset->data.glyphs[i]) { |
| 1080 |
gid = i + 1; |
| 1081 |
return gid; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
break; |
| 1085 |
case 1: |
| 1086 |
for (i = 0; i < charset->num_entries; i++) { |
| 1087 |
if (cid >= charset->data.range1[i].first && |
| 1088 |
cid <= charset->data.range1[i].first + charset->data.range1[i].n_left) { |
| 1089 |
gid += cid - charset->data.range1[i].first + 1; |
| 1090 |
return gid; |
| 1091 |
} |
| 1092 |
gid += charset->data.range1[i].n_left + 1; |
| 1093 |
} |
| 1094 |
break; |
| 1095 |
case 2: |
| 1096 |
for (i = 0; i < charset->num_entries; i++) { |
| 1097 |
if (cid >= charset->data.range2[i].first && |
| 1098 |
cid <= charset->data.range2[i].first + charset->data.range2[i].n_left) { |
| 1099 |
gid += cid - charset->data.range2[i].first + 1; |
| 1100 |
return gid; |
| 1101 |
} |
| 1102 |
gid += charset->data.range2[i].n_left + 1; |
| 1103 |
} |
| 1104 |
break; |
| 1105 |
default: |
| 1106 |
ERROR("Unknown Charset format"); |
| 1107 |
} |
| 1108 |
|
| 1109 |
return 0; /* not found */ |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* Input : GID |
| 1113 |
* Output: SID/CID (card16) |
| 1114 |
*/ |
| 1115 |
card16 |
| 1116 |
cff_charsets_lookup_inverse (cff_font *cff, card16 gid) |
| 1117 |
{ |
| 1118 |
if (cff->flag & (CHARSETS_ISOADOBE|CHARSETS_EXPERT|CHARSETS_EXPSUB)) { |
| 1119 |
ERROR("Predefined CFF charsets not supported yet"); |
| 1120 |
} else if (cff->charsets == NULL) { |
| 1121 |
ERROR("Charsets data not available"); |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (gid == 0) { |
| 1125 |
return 0; /* .notdef */ |
| 1126 |
} |
| 1127 |
|
| 1128 |
return cff_charsets_lookup_cid(cff->charsets, gid); |
| 1129 |
} |
| 1130 |
|
| 1131 |
card16 |
| 1132 |
cff_charsets_lookup_cid(cff_charsets *charset, card16 gid) |
| 1133 |
{ |
| 1134 |
card16 sid = 0; |
| 1135 |
card16 i; |
| 1136 |
|
| 1137 |
switch (charset->format) { |
| 1138 |
case 0: |
| 1139 |
if (gid - 1 >= charset->num_entries) |
| 1140 |
ERROR("Invalid GID."); |
| 1141 |
sid = charset->data.glyphs[gid - 1]; |
| 1142 |
break; |
| 1143 |
case 1: |
| 1144 |
for (i = 0; i < charset->num_entries; i++) { |
| 1145 |
if (gid <= charset->data.range1[i].n_left + 1) { |
| 1146 |
sid = gid + charset->data.range1[i].first - 1; |
| 1147 |
break; |
| 1148 |
} |
| 1149 |
gid -= charset->data.range1[i].n_left + 1; |
| 1150 |
} |
| 1151 |
if (i == charset->num_entries) |
| 1152 |
ERROR("Invalid GID"); |
| 1153 |
break; |
| 1154 |
case 2: |
| 1155 |
for (i = 0; i < charset->num_entries; i++) { |
| 1156 |
if (gid <= charset->data.range2[i].n_left + 1) { |
| 1157 |
sid = gid + charset->data.range2[i].first - 1; |
| 1158 |
break; |
| 1159 |
} |
| 1160 |
gid -= charset->data.range2[i].n_left + 1; |
| 1161 |
} |
| 1162 |
if (i == charset->num_entries) |
| 1163 |
ERROR("Invalid GID"); |
| 1164 |
break; |
| 1165 |
default: |
| 1166 |
ERROR("Unknown Charset format"); |
| 1167 |
} |
| 1168 |
|
| 1169 |
return sid; |
| 1170 |
} |
| 1171 |
|
| 1172 |
void |
| 1173 |
cff_release_charsets (cff_charsets *charset) |
| 1174 |
{ |
| 1175 |
if (charset) { |
| 1176 |
switch (charset->format) { |
| 1177 |
case 0: |
| 1178 |
if (charset->data.glyphs) |
| 1179 |
RELEASE(charset->data.glyphs); |
| 1180 |
break; |
| 1181 |
case 1: |
| 1182 |
if (charset->data.range1) |
| 1183 |
RELEASE(charset->data.range1); |
| 1184 |
break; |
| 1185 |
case 2: |
| 1186 |
if (charset->data.range2) |
| 1187 |
RELEASE(charset->data.range2); |
| 1188 |
break; |
| 1189 |
default: |
| 1190 |
break; |
| 1191 |
} |
| 1192 |
RELEASE(charset); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
/* CID-Keyed font specific */ |
| 1197 |
long cff_read_fdselect (cff_font *cff) |
| 1198 |
{ |
| 1199 |
cff_fdselect *fdsel; |
| 1200 |
long offset, length; |
| 1201 |
card16 i; |
| 1202 |
|
| 1203 |
if (cff->topdict == NULL) |
| 1204 |
ERROR("Top DICT not available"); |
| 1205 |
|
| 1206 |
if (!(cff->flag & FONTTYPE_CIDFONT)) |
| 1207 |
return 0; |
| 1208 |
|
| 1209 |
offset = (long) cff_dict_get(cff->topdict, "FDSelect", 0); |
| 1210 |
cff_seek_set(cff, offset); |
| 1211 |
cff->fdselect = fdsel = NEW(1, cff_fdselect); |
| 1212 |
fdsel->format = get_unsigned_byte(cff->stream); |
| 1213 |
|
| 1214 |
length = 1; |
| 1215 |
|
| 1216 |
switch (fdsel->format) { |
| 1217 |
case 0: |
| 1218 |
fdsel->num_entries = cff->num_glyphs; |
| 1219 |
(fdsel->data).fds = NEW(fdsel->num_entries, card8); |
| 1220 |
for (i=0;i<(fdsel->num_entries);i++) { |
| 1221 |
(fdsel->data).fds[i] = get_unsigned_byte(cff->stream); |
| 1222 |
} |
| 1223 |
length += fdsel->num_entries; |
| 1224 |
break; |
| 1225 |
case 3: |
| 1226 |
{ |
| 1227 |
cff_range3 *ranges; |
| 1228 |
fdsel->num_entries = get_unsigned_pair(cff->stream); |
| 1229 |
fdsel->data.ranges = ranges = NEW(fdsel->num_entries, cff_range3); |
| 1230 |
for (i=0;i<(fdsel->num_entries);i++) { |
| 1231 |
ranges[i].first = get_unsigned_pair(cff->stream); |
| 1232 |
ranges[i].fd = get_unsigned_byte(cff->stream); |
| 1233 |
} |
| 1234 |
if (ranges[0].first != 0) |
| 1235 |
ERROR("Range not starting with 0."); |
| 1236 |
if (cff->num_glyphs != get_unsigned_pair(cff->stream)) |
| 1237 |
ERROR("Sentinel value mismatched with number of glyphs."); |
| 1238 |
length += (fdsel->num_entries) * 3 + 4; |
| 1239 |
} |
| 1240 |
break; |
| 1241 |
default: |
| 1242 |
RELEASE(fdsel); |
| 1243 |
ERROR("Unknown FDSelect format."); |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
|
| 1247 |
return length; |
| 1248 |
} |
| 1249 |
|
| 1250 |
long cff_pack_fdselect (cff_font *cff, card8 *dest, long destlen) |
| 1251 |
{ |
| 1252 |
cff_fdselect *fdsel; |
| 1253 |
long len = 0; |
| 1254 |
card16 i; |
| 1255 |
|
| 1256 |
if (cff->fdselect == NULL) |
| 1257 |
return 0; |
| 1258 |
|
| 1259 |
if (destlen < 1) |
| 1260 |
ERROR("in cff_pack_fdselect(): Buffur overflow"); |
| 1261 |
|
| 1262 |
fdsel = cff->fdselect; |
| 1263 |
|
| 1264 |
dest[len++] = fdsel->format; |
| 1265 |
switch (fdsel->format) { |
| 1266 |
case 0: |
| 1267 |
if (fdsel->num_entries != cff->num_glyphs) |
| 1268 |
ERROR("in cff_pack_fdselect(): Invalid data"); |
| 1269 |
if (destlen < len + fdsel->num_entries) |
| 1270 |
ERROR("in cff_pack_fdselect(): Buffer overflow"); |
| 1271 |
for (i=0;i<fdsel->num_entries;i++) { |
| 1272 |
dest[len++] = (fdsel->data).fds[i]; |
| 1273 |
} |
| 1274 |
break; |
| 1275 |
case 3: |
| 1276 |
{ |
| 1277 |
if (destlen < len + 2) |
| 1278 |
ERROR("in cff_pack_fdselect(): Buffer overflow"); |
| 1279 |
len += 2; |
| 1280 |
for (i=0;i<(fdsel->num_entries);i++) { |
| 1281 |
if (destlen < len + 3) |
| 1282 |
ERROR("in cff_pack_fdselect(): Buffer overflow"); |
| 1283 |
dest[len++] = ((fdsel->data).ranges[i].first >> 8) & 0xff; |
| 1284 |
dest[len++] = (fdsel->data).ranges[i].first & 0xff; |
| 1285 |
dest[len++] = (fdsel->data).ranges[i].fd; |
| 1286 |
} |
| 1287 |
if (destlen < len + 2) |
| 1288 |
ERROR("in cff_pack_fdselect(): Buffer overflow"); |
| 1289 |
dest[len++] = (cff->num_glyphs >> 8) & 0xff; |
| 1290 |
dest[len++] = cff->num_glyphs & 0xff; |
| 1291 |
dest[1] = ((len/3 - 1) >> 8) & 0xff; |
| 1292 |
dest[2] = (len/3 - 1) & 0xff; |
| 1293 |
} |
| 1294 |
break; |
| 1295 |
default: |
| 1296 |
ERROR("Unknown FDSelect format."); |
| 1297 |
break; |
| 1298 |
} |
| 1299 |
|
| 1300 |
return len; |
| 1301 |
} |
| 1302 |
|
| 1303 |
void cff_release_fdselect (cff_fdselect *fdselect) |
| 1304 |
{ |
| 1305 |
if (fdselect) { |
| 1306 |
if (fdselect->format == 0) { |
| 1307 |
if (fdselect->data.fds) RELEASE(fdselect->data.fds); |
| 1308 |
} else if (fdselect->format == 3) { |
| 1309 |
if (fdselect->data.ranges) RELEASE(fdselect->data.ranges); |
| 1310 |
} |
| 1311 |
RELEASE(fdselect); |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
card8 cff_fdselect_lookup (cff_font *cff, card16 gid) |
| 1316 |
{ |
| 1317 |
card8 fd = 0xff; |
| 1318 |
cff_fdselect *fdsel; |
| 1319 |
|
| 1320 |
if (cff->fdselect == NULL) |
| 1321 |
ERROR("in cff_fdselect_lookup(): FDSelect not available"); |
| 1322 |
|
| 1323 |
fdsel = cff->fdselect; |
| 1324 |
|
| 1325 |
if (gid >= cff->num_glyphs) |
| 1326 |
ERROR("in cff_fdselect_lookup(): Invalid glyph index"); |
| 1327 |
|
| 1328 |
switch (fdsel->format) { |
| 1329 |
case 0: |
| 1330 |
fd = fdsel->data.fds[gid]; |
| 1331 |
break; |
| 1332 |
case 3: |
| 1333 |
{ |
| 1334 |
if (gid == 0) { |
| 1335 |
fd = (fdsel->data).ranges[0].fd; |
| 1336 |
} else { |
| 1337 |
card16 i; |
| 1338 |
for (i=1;i<(fdsel->num_entries);i++) { |
| 1339 |
if (gid < (fdsel->data).ranges[i].first) |
| 1340 |
break; |
| 1341 |
} |
| 1342 |
fd = (fdsel->data).ranges[i-1].fd; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
break; |
| 1346 |
default: |
| 1347 |
ERROR("in cff_fdselect_lookup(): Invalid FDSelect format"); |
| 1348 |
break; |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (fd >= cff->num_fds) |
| 1352 |
ERROR("in cff_fdselect_lookup(): Invalid Font DICT index"); |
| 1353 |
|
| 1354 |
return fd; |
| 1355 |
} |
| 1356 |
|
| 1357 |
long cff_read_subrs (cff_font *cff) |
| 1358 |
{ |
| 1359 |
long len = 0; |
| 1360 |
long offset; |
| 1361 |
int i; |
| 1362 |
|
| 1363 |
if ((cff->flag & FONTTYPE_CIDFONT) && cff->fdarray == NULL) { |
| 1364 |
cff_read_fdarray(cff); |
| 1365 |
} |
| 1366 |
|
| 1367 |
if (cff->privat == NULL) |
| 1368 |
cff_read_private(cff); |
| 1369 |
|
| 1370 |
if (cff->gsubr == NULL) { |
| 1371 |
cff_seek_set(cff, cff->gsubr_offset); |
| 1372 |
cff->gsubr = cff_get_index(cff); |
| 1373 |
} |
| 1374 |
|
| 1375 |
cff->subrs = NEW(cff->num_fds, cff_index *); |
| 1376 |
if (cff->flag & FONTTYPE_CIDFONT) { |
| 1377 |
for (i=0;i<cff->num_fds;i++) { |
| 1378 |
if (cff->privat[i] == NULL || |
| 1379 |
!cff_dict_known(cff->privat[i], "Subrs")) { |
| 1380 |
(cff->subrs)[i] = NULL; |
| 1381 |
} else { |
| 1382 |
offset = (long) cff_dict_get(cff->fdarray[i], "Private", 1); |
| 1383 |
offset += (long) cff_dict_get(cff->privat[i], "Subrs", 0); |
| 1384 |
cff_seek_set(cff, offset); |
| 1385 |
(cff->subrs)[i] = cff_get_index(cff); |
| 1386 |
len += cff_index_size((cff->subrs)[i]); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
} else { |
| 1390 |
if (cff->privat[0] == NULL || |
| 1391 |
!cff_dict_known(cff->privat[0], "Subrs")) { |
| 1392 |
(cff->subrs)[0] = NULL; |
| 1393 |
} else { |
| 1394 |
offset = (long) cff_dict_get(cff->topdict, "Private", 1); |
| 1395 |
offset += (long) cff_dict_get(cff->privat[0], "Subrs", 0); |
| 1396 |
cff_seek_set(cff, offset); |
| 1397 |
(cff->subrs)[0] = cff_get_index(cff); |
| 1398 |
len += cff_index_size((cff->subrs)[0]); |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
return len; |
| 1403 |
} |
| 1404 |
|
| 1405 |
long cff_read_fdarray (cff_font *cff) |
| 1406 |
{ |
| 1407 |
long len = 0; |
| 1408 |
cff_index *idx; |
| 1409 |
long offset, size; |
| 1410 |
card16 i; |
| 1411 |
|
| 1412 |
if (cff->topdict == NULL) |
| 1413 |
ERROR("in cff_read_fdarray(): Top DICT not found"); |
| 1414 |
|
| 1415 |
if (!(cff->flag & FONTTYPE_CIDFONT)) |
| 1416 |
return 0; |
| 1417 |
|
| 1418 |
/* must exist */ |
| 1419 |
offset = (long) cff_dict_get(cff->topdict, "FDArray", 0); |
| 1420 |
cff_seek_set(cff, offset); |
| 1421 |
idx = cff_get_index(cff); |
| 1422 |
cff->num_fds = (card8)idx->count; |
| 1423 |
cff->fdarray = NEW(idx->count, cff_dict *); |
| 1424 |
for (i=0;i<idx->count;i++) { |
| 1425 |
card8 *data = idx->data + (idx->offset)[i] - 1; |
| 1426 |
size = (idx->offset)[i+1] - (idx->offset)[i]; |
| 1427 |
if (size > 0) { |
| 1428 |
(cff->fdarray)[i] = cff_dict_unpack(data, data+size); |
| 1429 |
} else { |
| 1430 |
(cff->fdarray)[i] = NULL; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
len = cff_index_size(idx); |
| 1434 |
cff_release_index(idx); |
| 1435 |
|
| 1436 |
return len; |
| 1437 |
} |
| 1438 |
|
| 1439 |
long cff_read_private (cff_font *cff) |
| 1440 |
{ |
| 1441 |
long len = 0; |
| 1442 |
card8 *data; |
| 1443 |
long offset, size; |
| 1444 |
|
| 1445 |
if (cff->flag & FONTTYPE_CIDFONT) { |
| 1446 |
int i; |
| 1447 |
|
| 1448 |
if (cff->fdarray == NULL) |
| 1449 |
cff_read_fdarray(cff); |
| 1450 |
|
| 1451 |
cff->privat = NEW(cff->num_fds, cff_dict *); |
| 1452 |
for (i=0;i<cff->num_fds;i++) { |
| 1453 |
if (cff->fdarray[i] != NULL && |
| 1454 |
cff_dict_known(cff->fdarray[i], "Private") && |
| 1455 |
(size = (long) cff_dict_get(cff->fdarray[i], "Private", 0)) |
| 1456 |
> 0) { |
| 1457 |
offset = (long) cff_dict_get(cff->fdarray[i], "Private", 1); |
| 1458 |
cff_seek_set(cff, offset); |
| 1459 |
data = NEW(size, card8); |
| 1460 |
if (cff_read_data(data, size, cff) != size) |
| 1461 |
ERROR("reading file failed"); |
| 1462 |
(cff->privat)[i] = cff_dict_unpack(data, data+size); |
| 1463 |
RELEASE(data); |
| 1464 |
len += size; |
| 1465 |
} else { |
| 1466 |
(cff->privat)[i] = NULL; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
} else { |
| 1470 |
cff->num_fds = 1; |
| 1471 |
cff->privat = NEW(1, cff_dict *); |
| 1472 |
if (cff_dict_known(cff->topdict, "Private") && |
| 1473 |
(size = (long) cff_dict_get(cff->topdict, "Private", 0)) > 0) { |
| 1474 |
offset = (long) cff_dict_get(cff->topdict, "Private", 1); |
| 1475 |
cff_seek_set(cff, offset); |
| 1476 |
data = NEW(size, card8); |
| 1477 |
if (cff_read_data(data, size, cff) != size) |
| 1478 |
ERROR("reading file failed"); |
| 1479 |
cff->privat[0] = cff_dict_unpack(data, data+size); |
| 1480 |
RELEASE(data); |
| 1481 |
len += size; |
| 1482 |
} else { |
| 1483 |
(cff->privat)[0] = NULL; |
| 1484 |
len = 0; |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
return len; |
| 1489 |
} |