| 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 |
/* |
| 25 |
* References: |
| 26 |
* |
| 27 |
* PostScript Language Reference Manual, 3rd. ed. (Adobe Systems Inc.) |
| 28 |
* 5.11.4 CMap Dictionaries |
| 29 |
* 5.11.5 FMapType 9 Composite Fonts |
| 30 |
* Building CMap Files for CID-Keyed Fonts, Adobe Technical Note #5099 |
| 31 |
* CID-Keyed Font Technology Overview, Adobe Technical Note #5092 |
| 32 |
* Adobe CMap and CIDFont Files Specification, Adobe Technical Specification #5014 |
| 33 |
* |
| 34 |
* Undefined Character Handling: |
| 35 |
* PLRM 3rd. ed., sec. 5.11.5., "Handling Undefined Characters" |
| 36 |
* |
| 37 |
* TODO: |
| 38 |
* Only cid(range|char) allowed for CODE_TO_CID and bf(range|char) for CID_TO_CODE ? |
| 39 |
*/ |
| 40 |
|
| 41 |
#include <config.h> |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
#include "system.h" |
| 46 |
#include "mem.h" |
| 47 |
#include "error.h" |
| 48 |
#include "dpxutil.h" |
| 49 |
|
| 50 |
#include "cmap_p.h" |
| 51 |
#include "cmap.h" |
| 52 |
|
| 53 |
static int __silent = 0; |
| 54 |
|
| 55 |
void |
| 56 |
CMap_set_silent (int value) |
| 57 |
{ |
| 58 |
__silent = value ? 1 : 0; |
| 59 |
} |
| 60 |
|
| 61 |
/* Private funcs. */ |
| 62 |
static int bytes_consumed (CMap *cmap, const unsigned char *instr, long inbytes); |
| 63 |
static void handle_undefined (CMap *cmap, |
| 64 |
const unsigned char **inbuf, long *inbytesleft, |
| 65 |
unsigned char **outbuf, long *outbytesleft); |
| 66 |
|
| 67 |
static int check_range (CMap *cmap, |
| 68 |
const unsigned char *srclo, const unsigned char *srchi, int srcdim, |
| 69 |
const unsigned char *dst, int dstdim); |
| 70 |
|
| 71 |
static unsigned char *get_mem (CMap *cmap, int size); |
| 72 |
static mapDef *mapDef_new (void); |
| 73 |
static void mapDef_release (mapDef *t); |
| 74 |
static int locate_tbl (mapDef **cur, const unsigned char *code, int dim); |
| 75 |
|
| 76 |
CMap * |
| 77 |
CMap_new (void) |
| 78 |
{ |
| 79 |
CMap *cmap; |
| 80 |
|
| 81 |
cmap = NEW(1, struct CMap); |
| 82 |
cmap->name = NULL; |
| 83 |
cmap->type = CMAP_TYPE_CODE_TO_CID; |
| 84 |
cmap->wmode = 0; |
| 85 |
cmap->useCMap = NULL; |
| 86 |
cmap->CSI = NULL; |
| 87 |
|
| 88 |
cmap->profile.minBytesIn = 2; |
| 89 |
cmap->profile.maxBytesIn = 2; |
| 90 |
cmap->profile.minBytesOut = 2; |
| 91 |
cmap->profile.maxBytesOut = 2; |
| 92 |
|
| 93 |
cmap->flags = 0; |
| 94 |
|
| 95 |
cmap->codespace.num = 0; |
| 96 |
cmap->codespace.max = 10; |
| 97 |
cmap->codespace.ranges = NEW(10, struct rangeDef); |
| 98 |
|
| 99 |
cmap->mapTbl = NULL; |
| 100 |
|
| 101 |
cmap->mapData = NEW(1, struct mapData); |
| 102 |
cmap->mapData->prev = NULL; |
| 103 |
cmap->mapData->pos = 0; |
| 104 |
cmap->mapData->data = NEW(MEM_ALLOC_SIZE, unsigned char); |
| 105 |
|
| 106 |
cmap->reverseMap = NEW(65536, int); |
| 107 |
memset(cmap->reverseMap, 0, 65536 * sizeof(int)); |
| 108 |
|
| 109 |
return cmap; |
| 110 |
} |
| 111 |
|
| 112 |
void |
| 113 |
CMap_release (CMap *cmap) |
| 114 |
{ |
| 115 |
if (!cmap) |
| 116 |
return; |
| 117 |
|
| 118 |
if (cmap->name) |
| 119 |
RELEASE(cmap->name); |
| 120 |
if (cmap->CSI) { |
| 121 |
if (cmap->CSI->registry) RELEASE(cmap->CSI->registry); |
| 122 |
if (cmap->CSI->ordering) RELEASE(cmap->CSI->ordering); |
| 123 |
RELEASE(cmap->CSI); |
| 124 |
} |
| 125 |
if (cmap->codespace.ranges) |
| 126 |
RELEASE(cmap->codespace.ranges); |
| 127 |
if (cmap->mapTbl) |
| 128 |
mapDef_release(cmap->mapTbl); |
| 129 |
{ |
| 130 |
mapData *map = cmap->mapData; |
| 131 |
while (map != NULL) { |
| 132 |
mapData *prev = map->prev; |
| 133 |
if (map->data != NULL) |
| 134 |
RELEASE(map->data); |
| 135 |
RELEASE(map); |
| 136 |
map = prev; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (cmap->reverseMap) |
| 141 |
RELEASE(cmap->reverseMap); |
| 142 |
|
| 143 |
RELEASE(cmap); |
| 144 |
} |
| 145 |
|
| 146 |
int |
| 147 |
CMap_is_Identity (CMap *cmap) |
| 148 |
{ |
| 149 |
ASSERT(cmap); |
| 150 |
if (!strcmp(cmap->name, "Identity-H") || !strcmp(cmap->name, "Identity-V")) |
| 151 |
return 1; |
| 152 |
else |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
#if 0 |
| 157 |
/* NOT IMPLEMENTED YET */ |
| 158 |
int |
| 159 |
CMap_is_realy_Identity(CMap *cmap) |
| 160 |
{ |
| 161 |
return 0; |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
int |
| 166 |
CMap_is_valid (CMap *cmap) |
| 167 |
{ |
| 168 |
/* Quick check */ |
| 169 |
if (!cmap || !(cmap->name) || cmap->type < CMAP_TYPE_IDENTITY || |
| 170 |
cmap->type > CMAP_TYPE_CID_TO_CODE || cmap->codespace.num < 1 || |
| 171 |
(cmap->type != CMAP_TYPE_IDENTITY && !cmap->mapTbl)) |
| 172 |
return 0; |
| 173 |
|
| 174 |
if (cmap->useCMap) { |
| 175 |
CIDSysInfo *csi1, *csi2; |
| 176 |
csi1 = CMap_get_CIDSysInfo(cmap); |
| 177 |
csi2 = CMap_get_CIDSysInfo(cmap->useCMap); |
| 178 |
if (strcmp(csi1->registry, csi2->registry) || |
| 179 |
strcmp(csi1->ordering, csi2->ordering)) { |
| 180 |
WARN("CIDSystemInfo mismatched %s <--> %s", |
| 181 |
CMap_get_name(cmap), CMap_get_name(cmap->useCMap)); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
int |
| 190 |
CMap_get_profile (CMap *cmap, int type) |
| 191 |
{ |
| 192 |
int value = 0; |
| 193 |
|
| 194 |
ASSERT(cmap); |
| 195 |
switch (type) { |
| 196 |
case CMAP_PROF_TYPE_INBYTES_MIN: |
| 197 |
value = cmap->profile.minBytesIn; |
| 198 |
break; |
| 199 |
case CMAP_PROF_TYPE_INBYTES_MAX: |
| 200 |
value = cmap->profile.maxBytesIn; |
| 201 |
break; |
| 202 |
case CMAP_PROF_TYPE_OUTBYTES_MIN: |
| 203 |
value = cmap->profile.maxBytesOut; |
| 204 |
break; |
| 205 |
case CMAP_PROF_TYPE_OUTBYTES_MAX: |
| 206 |
value = cmap->profile.maxBytesOut; |
| 207 |
break; |
| 208 |
default: |
| 209 |
ERROR("%s: Unrecognized profile type %d.", CMAP_DEBUG_STR, type); |
| 210 |
} |
| 211 |
|
| 212 |
return value; |
| 213 |
} |
| 214 |
|
| 215 |
/* |
| 216 |
* Put notdef chars for codes not declared in notdef(range|char) |
| 217 |
*/ |
| 218 |
static void |
| 219 |
handle_undefined (CMap *cmap, |
| 220 |
const unsigned char **inbuf, long *inbytesleft, |
| 221 |
unsigned char **outbuf, long *outbytesleft) |
| 222 |
{ |
| 223 |
long len = 0; |
| 224 |
|
| 225 |
if (*outbytesleft < 2) |
| 226 |
ERROR("%s: Buffer overflow.", CMAP_DEBUG_STR); |
| 227 |
|
| 228 |
switch (cmap->type) { |
| 229 |
case CMAP_TYPE_CODE_TO_CID: |
| 230 |
memcpy(*outbuf, CID_NOTDEF_CHAR, 2); |
| 231 |
break; |
| 232 |
case CMAP_TYPE_TO_UNICODE: |
| 233 |
memcpy(*outbuf, UCS_NOTDEF_CHAR, 2); |
| 234 |
break; |
| 235 |
default: |
| 236 |
WARN("Cannot handle undefined mapping for this type of CMap mapping: %d", cmap->type); |
| 237 |
WARN("<0000> is used for .notdef char."); |
| 238 |
memset(*outbuf, 0, 2); |
| 239 |
} |
| 240 |
*outbuf += 2; |
| 241 |
*outbytesleft -= 2; |
| 242 |
|
| 243 |
len = bytes_consumed(cmap, *inbuf, *inbytesleft); |
| 244 |
|
| 245 |
*inbuf += len; |
| 246 |
*inbytesleft -= len; |
| 247 |
} |
| 248 |
|
| 249 |
void |
| 250 |
CMap_decode_char (CMap *cmap, |
| 251 |
const unsigned char **inbuf, long *inbytesleft, |
| 252 |
unsigned char **outbuf, long *outbytesleft) |
| 253 |
{ |
| 254 |
mapDef *t; |
| 255 |
const unsigned char *p, *save; |
| 256 |
unsigned char c = 0; |
| 257 |
long count = 0; |
| 258 |
|
| 259 |
p = save = *inbuf; |
| 260 |
/* |
| 261 |
* First handle some special cases: |
| 262 |
*/ |
| 263 |
if (cmap->type == CMAP_TYPE_IDENTITY) { |
| 264 |
if ((*inbytesleft) % 2) |
| 265 |
ERROR("%s: Invalid/truncated input string.", CMAP_DEBUG_STR); |
| 266 |
if (*outbytesleft < 2) |
| 267 |
ERROR("%s: Buffer overflow.", CMAP_DEBUG_STR); |
| 268 |
memcpy(*outbuf, *inbuf, 2); |
| 269 |
*inbuf += 2; |
| 270 |
*outbuf += 2; |
| 271 |
*outbytesleft -= 2; |
| 272 |
*inbytesleft -= 2; |
| 273 |
return; |
| 274 |
} else if (!cmap->mapTbl) { |
| 275 |
if (cmap->useCMap) { |
| 276 |
CMap_decode_char(cmap->useCMap, inbuf, inbytesleft, outbuf, outbytesleft); |
| 277 |
return; |
| 278 |
} else { |
| 279 |
/* no mapping available in this CMap */ |
| 280 |
WARN("No mapping available for this character."); |
| 281 |
handle_undefined(cmap, inbuf, inbytesleft, outbuf, outbytesleft); |
| 282 |
return; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
ASSERT(cmap->mapTbl); |
| 287 |
t = cmap->mapTbl; |
| 288 |
while (count < *inbytesleft) { |
| 289 |
c = *p++; |
| 290 |
count++; |
| 291 |
if (LOOKUP_END(t[c].flag)) |
| 292 |
break; |
| 293 |
t = t[c].next; |
| 294 |
} |
| 295 |
if (LOOKUP_CONTINUE(t[c].flag)) /* need more bytes */ |
| 296 |
ERROR("%s: Premature end of input string.", CMAP_DEBUG_STR); |
| 297 |
else if (!MAP_DEFINED(t[c].flag)) { |
| 298 |
if (cmap->useCMap) { |
| 299 |
CMap_decode_char(cmap->useCMap, inbuf, inbytesleft, outbuf, outbytesleft); |
| 300 |
return; |
| 301 |
} else { |
| 302 |
/* no mapping available in this CMap */ |
| 303 |
WARN("No character mapping available."); |
| 304 |
MESG(" CMap name: %s\n", CMap_get_name(cmap)); |
| 305 |
MESG(" input str: "); |
| 306 |
MESG("<"); |
| 307 |
while (save < p) { |
| 308 |
MESG("%02x", *save); |
| 309 |
save++; |
| 310 |
} |
| 311 |
MESG(">\n"); |
| 312 |
/* |
| 313 |
* We know partial match found up to `count' bytes, |
| 314 |
* but we will not use this information for the sake of simplicity. |
| 315 |
*/ |
| 316 |
handle_undefined(cmap, inbuf, inbytesleft, outbuf, outbytesleft); |
| 317 |
return; |
| 318 |
} |
| 319 |
} else { |
| 320 |
switch (MAP_TYPE(t[c].flag)) { |
| 321 |
case MAP_IS_NOTDEF: |
| 322 |
WARN("Character mapped to .notdef found."); |
| 323 |
/* continue */ |
| 324 |
case MAP_IS_CID: case MAP_IS_CODE: |
| 325 |
if (*outbytesleft >= t[c].len) |
| 326 |
memcpy(*outbuf, t[c].code, t[c].len); |
| 327 |
else |
| 328 |
ERROR("%s: Buffer overflow.", CMAP_DEBUG_STR); |
| 329 |
*outbuf += t[c].len; |
| 330 |
*outbytesleft -= t[c].len; |
| 331 |
break; |
| 332 |
case MAP_IS_NAME: |
| 333 |
ERROR("%s: CharName mapping not supported.", CMAP_DEBUG_STR); |
| 334 |
break; |
| 335 |
default: |
| 336 |
ERROR("%s: Unknown mapping type.", CMAP_DEBUG_STR); |
| 337 |
} |
| 338 |
if (inbytesleft) |
| 339 |
*inbytesleft -= count; |
| 340 |
*inbuf = p; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
* For convenience, it does not do decoding to CIDs. |
| 346 |
*/ |
| 347 |
long |
| 348 |
CMap_decode (CMap *cmap, |
| 349 |
const unsigned char **inbuf, long *inbytesleft, |
| 350 |
unsigned char **outbuf, long *outbytesleft) |
| 351 |
{ |
| 352 |
int count; |
| 353 |
|
| 354 |
ASSERT(cmap && inbuf && outbuf); |
| 355 |
ASSERT(inbytesleft && outbytesleft); |
| 356 |
for (count = 0;*inbytesleft > 0 && *outbytesleft > 0; count++) |
| 357 |
CMap_decode_char(cmap, inbuf, inbytesleft, outbuf, outbytesleft); |
| 358 |
|
| 359 |
return count; |
| 360 |
} |
| 361 |
|
| 362 |
int |
| 363 |
CMap_reverse_decode(CMap *cmap, CID cid) { |
| 364 |
int ch = cmap->reverseMap ? cmap->reverseMap[cid] : -1; |
| 365 |
if (ch == 0 && cmap->useCMap) |
| 366 |
return CMap_reverse_decode(cmap->useCMap, cid); |
| 367 |
return ch; |
| 368 |
} |
| 369 |
|
| 370 |
char * |
| 371 |
CMap_get_name (CMap *cmap) |
| 372 |
{ |
| 373 |
ASSERT(cmap); |
| 374 |
return cmap->name; |
| 375 |
} |
| 376 |
|
| 377 |
int |
| 378 |
CMap_get_type (CMap *cmap) |
| 379 |
{ |
| 380 |
ASSERT(cmap); |
| 381 |
return cmap->type; |
| 382 |
} |
| 383 |
|
| 384 |
int |
| 385 |
CMap_get_wmode (CMap *cmap) |
| 386 |
{ |
| 387 |
ASSERT(cmap); |
| 388 |
return cmap->wmode; |
| 389 |
} |
| 390 |
|
| 391 |
CIDSysInfo * |
| 392 |
CMap_get_CIDSysInfo (CMap *cmap) |
| 393 |
{ |
| 394 |
ASSERT(cmap); |
| 395 |
return cmap->CSI; |
| 396 |
} |
| 397 |
|
| 398 |
void |
| 399 |
CMap_set_name (CMap *cmap, const char *name) |
| 400 |
{ |
| 401 |
ASSERT(cmap); |
| 402 |
if (cmap->name) |
| 403 |
RELEASE(cmap->name); |
| 404 |
cmap->name = NEW(strlen(name)+1, char); |
| 405 |
strcpy(cmap->name, name); |
| 406 |
} |
| 407 |
|
| 408 |
void |
| 409 |
CMap_set_type (CMap *cmap, int type) |
| 410 |
{ |
| 411 |
ASSERT(cmap); |
| 412 |
cmap->type = type; |
| 413 |
} |
| 414 |
|
| 415 |
void |
| 416 |
CMap_set_wmode (CMap *cmap, int wmode) |
| 417 |
{ |
| 418 |
ASSERT(cmap); |
| 419 |
cmap->wmode = wmode; |
| 420 |
} |
| 421 |
|
| 422 |
void |
| 423 |
CMap_set_CIDSysInfo (CMap *cmap, const CIDSysInfo *csi) |
| 424 |
{ |
| 425 |
ASSERT(cmap); |
| 426 |
|
| 427 |
if (cmap->CSI) { |
| 428 |
if (cmap->CSI->registry) |
| 429 |
RELEASE(cmap->CSI->registry); |
| 430 |
if (cmap->CSI->ordering) |
| 431 |
RELEASE(cmap->CSI->ordering); |
| 432 |
RELEASE(cmap->CSI); |
| 433 |
} |
| 434 |
|
| 435 |
if (csi && csi->registry && csi->ordering) { |
| 436 |
cmap->CSI = NEW(1, CIDSysInfo); |
| 437 |
cmap->CSI->registry = NEW(strlen(csi->registry)+1, char); |
| 438 |
strcpy(cmap->CSI->registry, csi->registry); |
| 439 |
cmap->CSI->ordering = NEW(strlen(csi->ordering)+1, char); |
| 440 |
strcpy(cmap->CSI->ordering, csi->ordering); |
| 441 |
cmap->CSI->supplement = csi->supplement; |
| 442 |
} else { |
| 443 |
WARN("Invalid CIDSystemInfo."); |
| 444 |
cmap->CSI = NULL; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
* Can have muliple entry ? |
| 450 |
*/ |
| 451 |
void |
| 452 |
CMap_set_usecmap (CMap *cmap, CMap *ucmap) |
| 453 |
{ |
| 454 |
int i; |
| 455 |
|
| 456 |
ASSERT(cmap); |
| 457 |
ASSERT(ucmap); /* Maybe if (!ucmap) ERROR() is better for this. */ |
| 458 |
|
| 459 |
if (cmap == ucmap) |
| 460 |
ERROR("%s: Identical CMap object cannot be used for usecmap CMap: 0x%p=0x%p", |
| 461 |
CMAP_DEBUG_STR, cmap, ucmap); |
| 462 |
|
| 463 |
/* Check if ucmap have neccesary information. */ |
| 464 |
if (!CMap_is_valid(ucmap)) |
| 465 |
ERROR("%s: Invalid CMap.", CMAP_DEBUG_STR); |
| 466 |
|
| 467 |
/* |
| 468 |
* CMapName of cmap can be undefined when usecmap is executed in CMap parsing. |
| 469 |
* And it is also possible CSI is not defined at that time. |
| 470 |
*/ |
| 471 |
if (cmap->name && strcmp(cmap->name, ucmap->name) == 0) |
| 472 |
ERROR("%s: CMap refering itself not allowed: CMap %s --> %s", |
| 473 |
CMAP_DEBUG_STR, cmap->name, ucmap->name); |
| 474 |
|
| 475 |
if (cmap->CSI && cmap->CSI->registry && cmap->CSI->ordering) { |
| 476 |
if (strcmp(cmap->CSI->registry, ucmap->CSI->registry) || |
| 477 |
strcmp(cmap->CSI->ordering, ucmap->CSI->ordering)) |
| 478 |
ERROR("%s: CMap %s required by %s have different CSI.", |
| 479 |
CMAP_DEBUG_STR, CMap_get_name(cmap), CMap_get_name(ucmap)); |
| 480 |
} |
| 481 |
|
| 482 |
/* We must copy codespaceranges. */ |
| 483 |
for (i = 0; i < ucmap->codespace.num; i++) { |
| 484 |
rangeDef *csr = ucmap->codespace.ranges + i; |
| 485 |
CMap_add_codespacerange(cmap, csr->codeLo, csr->codeHi, csr->dim); |
| 486 |
} |
| 487 |
|
| 488 |
cmap->useCMap = ucmap; |
| 489 |
} |
| 490 |
|
| 491 |
/* Test the validity of character c. */ |
| 492 |
int |
| 493 |
CMap_match_codespace (CMap *cmap, const unsigned char *c, int dim) |
| 494 |
{ |
| 495 |
int i, pos; |
| 496 |
|
| 497 |
ASSERT(cmap); |
| 498 |
for (i = 0; i < cmap->codespace.num; i++) { |
| 499 |
rangeDef *csr = cmap->codespace.ranges + i; |
| 500 |
if (csr->dim != dim) |
| 501 |
continue; |
| 502 |
for (pos = 0; pos < dim; pos++) { |
| 503 |
if (c[pos] > csr->codeHi[pos] || c[pos] < csr->codeLo[pos]) |
| 504 |
break; |
| 505 |
} |
| 506 |
if (pos == dim) |
| 507 |
return 0; /* Valid */ |
| 508 |
} |
| 509 |
|
| 510 |
return -1; /* Invalid */ |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
* No overlapping codespace ranges are allowed, otherwise mapping is ambiguous. |
| 515 |
*/ |
| 516 |
int |
| 517 |
CMap_add_codespacerange (CMap *cmap, |
| 518 |
const unsigned char *codelo, const unsigned char *codehi, int dim) |
| 519 |
{ |
| 520 |
rangeDef *csr = NULL; |
| 521 |
int i; |
| 522 |
|
| 523 |
ASSERT(cmap && dim > 0); |
| 524 |
|
| 525 |
for (i = 0; i < cmap->codespace.num; i++) { |
| 526 |
int j, overlap = 1; |
| 527 |
csr = cmap->codespace.ranges + i; |
| 528 |
for (j = 0; j < MIN(csr->dim, dim) && overlap; j++) { |
| 529 |
if ((codelo[j] >= csr->codeLo[j] && codelo[j] <= csr->codeHi[j]) || |
| 530 |
(codehi[j] >= csr->codeLo[j] && codehi[j] <= csr->codeHi[j])) |
| 531 |
overlap = 1; |
| 532 |
else |
| 533 |
overlap = 0; |
| 534 |
} |
| 535 |
if (overlap) { |
| 536 |
WARN("Overlapping codespace found. (ingored)"); |
| 537 |
return -1; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
if (dim < cmap->profile.minBytesIn) |
| 542 |
cmap->profile.minBytesIn = dim; |
| 543 |
if (dim > cmap->profile.maxBytesIn) |
| 544 |
cmap->profile.maxBytesIn = dim; |
| 545 |
|
| 546 |
if (cmap->codespace.num + 1 > cmap->codespace.max) { |
| 547 |
cmap->codespace.max += 10; |
| 548 |
cmap->codespace.ranges = RENEW(cmap->codespace.ranges, cmap->codespace.max, struct rangeDef); |
| 549 |
} |
| 550 |
|
| 551 |
csr = cmap->codespace.ranges + cmap->codespace.num; |
| 552 |
csr->dim = dim; |
| 553 |
csr->codeHi = get_mem(cmap, dim); |
| 554 |
csr->codeLo = get_mem(cmap, dim); |
| 555 |
memcpy(csr->codeHi, codehi, dim); |
| 556 |
memcpy(csr->codeLo, codelo, dim); |
| 557 |
|
| 558 |
(cmap->codespace.num)++; |
| 559 |
|
| 560 |
return 0; |
| 561 |
} |
| 562 |
|
| 563 |
int |
| 564 |
CMap_add_notdefchar (CMap *cmap, const unsigned char *src, int srcdim, CID dst) |
| 565 |
{ |
| 566 |
return CMap_add_notdefrange(cmap, src, src, srcdim, dst); |
| 567 |
} |
| 568 |
|
| 569 |
int |
| 570 |
CMap_add_notdefrange (CMap *cmap, |
| 571 |
const unsigned char *srclo, const unsigned char *srchi, int srcdim, CID dst) |
| 572 |
{ |
| 573 |
int c; |
| 574 |
mapDef *cur; |
| 575 |
|
| 576 |
ASSERT(cmap); |
| 577 |
/* dst not used here */ |
| 578 |
/* FIXME */ |
| 579 |
if (check_range(cmap, srclo, srchi, srcdim, (const unsigned char *)&dst, 2) < 0) |
| 580 |
return -1; |
| 581 |
|
| 582 |
if (cmap->mapTbl == NULL ) |
| 583 |
cmap->mapTbl = mapDef_new(); |
| 584 |
|
| 585 |
cur = cmap->mapTbl; |
| 586 |
if (locate_tbl(&cur, srclo, srcdim) < 0) |
| 587 |
return -1; |
| 588 |
|
| 589 |
for (c = srclo[srcdim-1]; c <= srchi[srcdim-1]; c++) { |
| 590 |
if (MAP_DEFINED(cur[c].flag)) { |
| 591 |
if (!__silent) |
| 592 |
WARN("Trying to redefine already defined code mapping. (ignored)"); |
| 593 |
} else { |
| 594 |
cur[c].flag = (MAP_LOOKUP_END|MAP_IS_NOTDEF); |
| 595 |
cur[c].code = get_mem(cmap, 2); |
| 596 |
cur[c].len = 2; |
| 597 |
cur[c].code[0] = dst >> 8; |
| 598 |
cur[c].code[1] = dst & 0xff; |
| 599 |
} |
| 600 |
/* Do not do dst++ for notdefrange */ |
| 601 |
} |
| 602 |
|
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
int |
| 607 |
CMap_add_bfchar (CMap *cmap, |
| 608 |
const unsigned char *src, int srcdim, |
| 609 |
const unsigned char *dst, int dstdim) |
| 610 |
{ |
| 611 |
return CMap_add_bfrange(cmap, src, src, srcdim, dst, dstdim); |
| 612 |
} |
| 613 |
|
| 614 |
int |
| 615 |
CMap_add_bfrange (CMap *cmap, |
| 616 |
const unsigned char *srclo, const unsigned char *srchi, int srcdim, |
| 617 |
const unsigned char *base, int dstdim) |
| 618 |
{ |
| 619 |
int c, last_byte, i; |
| 620 |
mapDef *cur; |
| 621 |
|
| 622 |
ASSERT(cmap); |
| 623 |
if (check_range(cmap, srclo, srchi, srcdim, base, dstdim) < 0) |
| 624 |
return -1; |
| 625 |
|
| 626 |
if (cmap->mapTbl == NULL) |
| 627 |
cmap->mapTbl = mapDef_new(); |
| 628 |
|
| 629 |
cur = cmap->mapTbl; |
| 630 |
if (locate_tbl(&cur, srclo, srcdim) < 0) |
| 631 |
return -1; |
| 632 |
|
| 633 |
for (c = srclo[srcdim-1]; c <= srchi[srcdim-1]; c++) { |
| 634 |
/* According to 5014.CIDFont_Spec.pdf (p.52), |
| 635 |
* Code mappings (unlike codespace ranges) may overlap, |
| 636 |
* but succeeding maps superceded preceding maps. |
| 637 |
* (reported and patched by Luo Jie on 2007/12/2) |
| 638 |
*/ |
| 639 |
if (!MAP_DEFINED(cur[c].flag) || cur[c].len < dstdim) { |
| 640 |
cur[c].flag = (MAP_LOOKUP_END|MAP_IS_CODE); |
| 641 |
cur[c].code = get_mem(cmap, dstdim); |
| 642 |
} |
| 643 |
/* |
| 644 |
* We assume restriction to code ranges also applied here. |
| 645 |
* Addition <00FF> + 1 is undefined. |
| 646 |
* |
| 647 |
* Changed on 2004-03-20: |
| 648 |
* |
| 649 |
* Should be treated as <0100> in Acrobat's "ToUnicode" CMap. |
| 650 |
*/ |
| 651 |
cur[c].len = dstdim; |
| 652 |
memcpy(cur[c].code, base, dstdim); |
| 653 |
|
| 654 |
last_byte = c - srclo[srcdim-1] + base[dstdim-1]; |
| 655 |
cur[c].code[dstdim-1] = (last_byte & 0xFF); |
| 656 |
for (i = dstdim - 2; i >= 0 && last_byte > 255; i--) { |
| 657 |
last_byte = cur[c].code[i] + 1; |
| 658 |
cur[c].code[i] = (last_byte & 0xFF); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
int |
| 666 |
CMap_add_cidchar (CMap *cmap, const unsigned char *src, int srcdim, CID dst) |
| 667 |
{ |
| 668 |
return CMap_add_cidrange(cmap, src, src, srcdim, dst); |
| 669 |
} |
| 670 |
|
| 671 |
int |
| 672 |
CMap_add_cidrange (CMap *cmap, |
| 673 |
const unsigned char *srclo, const unsigned char *srchi, int srcdim, CID base) |
| 674 |
{ |
| 675 |
int i, c, v; |
| 676 |
mapDef *cur; |
| 677 |
|
| 678 |
ASSERT(cmap); |
| 679 |
/* base not used here */ |
| 680 |
if (check_range(cmap, srclo, srchi, srcdim, (const unsigned char *)&base, 2) < 0) /* FIXME */ |
| 681 |
return -1; |
| 682 |
|
| 683 |
if (cmap->mapTbl == NULL ) |
| 684 |
cmap->mapTbl = mapDef_new(); |
| 685 |
|
| 686 |
cur = cmap->mapTbl; |
| 687 |
if (locate_tbl(&cur, srclo, srcdim) < 0) |
| 688 |
return -1; |
| 689 |
|
| 690 |
for (v = 0, i = 0; i < srcdim - 1; i++) |
| 691 |
v = (v << 8) + srclo[i]; |
| 692 |
|
| 693 |
cmap->reverseMap[base] = v; |
| 694 |
|
| 695 |
for (c = srclo[srcdim-1]; c <= srchi[srcdim-1]; c++) { |
| 696 |
if (cur[c].flag != 0) { |
| 697 |
if (!__silent) |
| 698 |
WARN("Trying to redefine already defined CID mapping. (ignored)"); |
| 699 |
} else { |
| 700 |
cur[c].flag = (MAP_LOOKUP_END|MAP_IS_CID); |
| 701 |
cur[c].len = 2; |
| 702 |
cur[c].code = get_mem(cmap, 2); |
| 703 |
cur[c].code[0] = base >> 8; |
| 704 |
cur[c].code[1] = base & 0xff; |
| 705 |
|
| 706 |
cmap->reverseMap[base] = (v << 8) + c; |
| 707 |
} |
| 708 |
if (base >= CID_MAX) |
| 709 |
WARN("CID number too large."); |
| 710 |
base++; |
| 711 |
} |
| 712 |
|
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
static void |
| 717 |
mapDef_release (mapDef *t) |
| 718 |
{ |
| 719 |
int c; |
| 720 |
|
| 721 |
ASSERT(t); |
| 722 |
for (c = 0; c < 256; c++) { |
| 723 |
if (LOOKUP_CONTINUE(t[c].flag)) |
| 724 |
mapDef_release(t[c].next); |
| 725 |
} |
| 726 |
RELEASE(t); |
| 727 |
} |
| 728 |
|
| 729 |
static mapDef * |
| 730 |
mapDef_new (void) |
| 731 |
{ |
| 732 |
mapDef *t; |
| 733 |
int c; |
| 734 |
|
| 735 |
t = NEW(256, mapDef); |
| 736 |
for (c=0; c<256; c++) { |
| 737 |
t[c].flag = (MAP_LOOKUP_END|MAP_IS_UNDEF); |
| 738 |
t[c].code = NULL; |
| 739 |
t[c].next = NULL; |
| 740 |
} |
| 741 |
|
| 742 |
return t; |
| 743 |
} |
| 744 |
|
| 745 |
static unsigned char * |
| 746 |
get_mem (CMap *cmap, int size) |
| 747 |
{ |
| 748 |
mapData *map; |
| 749 |
unsigned char *p; |
| 750 |
|
| 751 |
ASSERT(cmap && cmap->mapData && size >= 0); |
| 752 |
map = cmap->mapData; |
| 753 |
if (map->pos + size >= MEM_ALLOC_SIZE) { |
| 754 |
mapData *prev = map; |
| 755 |
map = NEW(1, struct mapData); |
| 756 |
map->data = NEW(MEM_ALLOC_SIZE, unsigned char); |
| 757 |
map->prev = prev; |
| 758 |
map->pos = 0; |
| 759 |
cmap->mapData = map; |
| 760 |
} |
| 761 |
p = map->data + map->pos; |
| 762 |
map->pos += size; |
| 763 |
|
| 764 |
return p; |
| 765 |
} |
| 766 |
|
| 767 |
static int |
| 768 |
locate_tbl (mapDef **cur, const unsigned char *code, int dim) |
| 769 |
{ |
| 770 |
int i, c; |
| 771 |
|
| 772 |
ASSERT(cur && *cur); |
| 773 |
for (i = 0; i < dim-1; i++) { |
| 774 |
c = code[i]; |
| 775 |
if (MAP_DEFINED((*cur)[c].flag)) { |
| 776 |
WARN("Ambiguous CMap entry."); |
| 777 |
return -1; |
| 778 |
} |
| 779 |
if ((*cur)[c].next == NULL) /* create new node */ |
| 780 |
(*cur)[c].next = mapDef_new(); |
| 781 |
(*cur)[c].flag |= MAP_LOOKUP_CONTINUE; |
| 782 |
*cur = (*cur)[c].next; |
| 783 |
} |
| 784 |
|
| 785 |
return 0; |
| 786 |
} |
| 787 |
|
| 788 |
/* |
| 789 |
* Guess how many bytes consumed as a `single' character: |
| 790 |
* Substring of length bytesconsumed bytes of input string is interpreted as |
| 791 |
* a `single' character by CMap_decode(). |
| 792 |
*/ |
| 793 |
static int |
| 794 |
bytes_consumed (CMap *cmap, const unsigned char *instr, long inbytes) |
| 795 |
{ |
| 796 |
int i, pos, longest = 0, bytesconsumed; |
| 797 |
|
| 798 |
ASSERT(cmap); |
| 799 |
for (i = 0; i < cmap->codespace.num; i++) { |
| 800 |
rangeDef *csr = cmap->codespace.ranges + i; |
| 801 |
for (pos = 0; pos < MIN(csr->dim, inbytes); pos++) { |
| 802 |
if (instr[pos] > csr->codeHi[pos] || instr[pos] < csr->codeLo[pos]) |
| 803 |
break; |
| 804 |
} |
| 805 |
if (pos == csr->dim) /* part of instr is totally valid in this codespace. */ |
| 806 |
return csr->dim; |
| 807 |
if (pos > longest) |
| 808 |
longest = pos; |
| 809 |
} |
| 810 |
|
| 811 |
if (i == cmap->codespace.num) /* No matching at all */ |
| 812 |
bytesconsumed = cmap->profile.minBytesIn; |
| 813 |
else { |
| 814 |
bytesconsumed = cmap->profile.maxBytesIn; |
| 815 |
for (i = 0; i< cmap->codespace.num; i++) { |
| 816 |
rangeDef *csr = cmap->codespace.ranges + i; |
| 817 |
if (csr->dim > longest && csr->dim < bytesconsumed) |
| 818 |
bytesconsumed = csr->dim; |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
return bytesconsumed; |
| 823 |
} |
| 824 |
|
| 825 |
static int |
| 826 |
check_range (CMap *cmap, |
| 827 |
const unsigned char *srclo, const unsigned char *srchi, int srcdim, |
| 828 |
const unsigned char *dst, int dstdim) |
| 829 |
{ |
| 830 |
if ((srcdim < 1 || dstdim < 1) || |
| 831 |
(!srclo || !srchi || !dst) || |
| 832 |
memcmp(srclo, srchi, srcdim - 1) || |
| 833 |
srclo[srcdim-1] > srchi[srcdim-1]) { |
| 834 |
WARN("Invalid CMap mapping entry. (ignored)"); |
| 835 |
return -1; |
| 836 |
} |
| 837 |
|
| 838 |
if (CMap_match_codespace(cmap, srclo, srcdim) < 0 || |
| 839 |
CMap_match_codespace(cmap, srchi, srcdim) < 0) { |
| 840 |
WARN("Invalid CMap mapping entry. (ignored)"); |
| 841 |
return -1; |
| 842 |
} |
| 843 |
|
| 844 |
if (srcdim < cmap->profile.minBytesIn) |
| 845 |
cmap->profile.minBytesIn = srcdim; |
| 846 |
if (srcdim > cmap->profile.maxBytesIn) |
| 847 |
cmap->profile.maxBytesIn = srcdim; |
| 848 |
if (dstdim < cmap->profile.minBytesOut) |
| 849 |
cmap->profile.minBytesOut = dstdim; |
| 850 |
if (dstdim > cmap->profile.maxBytesOut) |
| 851 |
cmap->profile.maxBytesOut = dstdim; |
| 852 |
|
| 853 |
return 0; |
| 854 |
} |