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