| 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 |
*/ |
| 35 |
|
| 36 |
#ifdef HAVE_CONFIG_H |
| 37 |
#include <config.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <string.h> |
| 41 |
|
| 42 |
#include "system.h" |
| 43 |
#include "mem.h" |
| 44 |
#include "error.h" |
| 45 |
#include "dpxutil.h" |
| 46 |
|
| 47 |
#include "pdfobj.h" |
| 48 |
#include "pdfresource.h" |
| 49 |
|
| 50 |
#include "cmap_p.h" |
| 51 |
#include "cmap.h" |
| 52 |
|
| 53 |
#include "cmap_write.h" |
| 54 |
|
| 55 |
struct sbuf { |
| 56 |
char *buf; |
| 57 |
char *curptr; |
| 58 |
char *limptr; |
| 59 |
}; |
| 60 |
|
| 61 |
static int write_map (mapDef *mtab, int count, |
| 62 |
unsigned char *codestr, int depth, |
| 63 |
struct sbuf *wbuf, pdf_obj *stream); |
| 64 |
#if 0 |
| 65 |
/* Not completed yet */ |
| 66 |
|
| 67 |
/* used_slot... |
| 68 |
* This is for collecting only used CIDs. |
| 69 |
* add_inverse_map never supports code-to-code of |
| 70 |
* cid-to-code mapping. |
| 71 |
*/ |
| 72 |
static int add_inverse_map (CMap *icmap, mapDef *mtab, |
| 73 |
unsigned char *codestr, int depth, |
| 74 |
unsigned char *used_slot); |
| 75 |
static int add_map (CMap *cmap, mapDef *mtab, |
| 76 |
unsigned char *codestr, int depth); |
| 77 |
static CMap *invert_cmap (CMap *cmap, unsigned char *used_slot); |
| 78 |
static CMap *flatten_cmap (CMap *cmap); |
| 79 |
#endif /* 0 */ |
| 80 |
|
| 81 |
static int |
| 82 |
block_count (mapDef *mtab, int c) |
| 83 |
{ |
| 84 |
int count = 0, n; |
| 85 |
|
| 86 |
n = mtab[c].len - 1; |
| 87 |
c += 1; |
| 88 |
for (; c < 256; c++) { |
| 89 |
if (LOOKUP_CONTINUE(mtab[c].flag) || |
| 90 |
!MAP_DEFINED(mtab[c].flag) || |
| 91 |
(MAP_TYPE(mtab[c].flag) != MAP_IS_CID && |
| 92 |
MAP_TYPE(mtab[c].flag) != MAP_IS_CODE) || |
| 93 |
mtab[c-1].len != mtab[c].len) |
| 94 |
break; |
| 95 |
else if (!memcmp(mtab[c-1].code, mtab[c].code, n) && |
| 96 |
mtab[c-1].code[n] < 255 && |
| 97 |
mtab[c-1].code[n] + 1 == mtab[c].code[n]) |
| 98 |
count++; |
| 99 |
else { |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return count; |
| 105 |
} |
| 106 |
|
| 107 |
static int |
| 108 |
sputx (unsigned char c, char **s, char *end) |
| 109 |
{ |
| 110 |
char hi = (c >> 4), lo = c & 0x0f; |
| 111 |
|
| 112 |
if (*s + 2 > end) |
| 113 |
ERROR("Buffer overflow."); |
| 114 |
**s = (hi < 10) ? hi + '0' : hi + '7'; |
| 115 |
*(*s+1) = (lo < 10) ? lo + '0' : lo + '7'; |
| 116 |
*s += 2; |
| 117 |
|
| 118 |
return 2; |
| 119 |
} |
| 120 |
|
| 121 |
static int |
| 122 |
write_map (mapDef *mtab, int count, |
| 123 |
unsigned char *codestr, int depth, |
| 124 |
struct sbuf *wbuf, pdf_obj *stream) |
| 125 |
{ |
| 126 |
int c, i, block_length; |
| 127 |
mapDef *mtab1; |
| 128 |
/* Must be greater than 1 */ |
| 129 |
#define BLOCK_LEN_MIN 2 |
| 130 |
struct { |
| 131 |
int start, count; |
| 132 |
} blocks[256/BLOCK_LEN_MIN+1]; |
| 133 |
int num_blocks = 0; |
| 134 |
|
| 135 |
for (c = 0; c < 256; c++) { |
| 136 |
codestr[depth] = (unsigned char) (c & 0xff); |
| 137 |
if (LOOKUP_CONTINUE(mtab[c].flag)) { |
| 138 |
mtab1 = mtab[c].next; |
| 139 |
count = write_map(mtab1, count, |
| 140 |
codestr, depth + 1, wbuf, stream); |
| 141 |
} else { |
| 142 |
if (MAP_DEFINED(mtab[c].flag)) { |
| 143 |
switch (MAP_TYPE(mtab[c].flag)) { |
| 144 |
case MAP_IS_CID: case MAP_IS_CODE: |
| 145 |
block_length = block_count(mtab, c); |
| 146 |
if (block_length >= BLOCK_LEN_MIN) { |
| 147 |
blocks[num_blocks].start = c; |
| 148 |
blocks[num_blocks].count = block_length; |
| 149 |
num_blocks++; |
| 150 |
c += block_length; |
| 151 |
} else { |
| 152 |
*(wbuf->curptr)++ = '<'; |
| 153 |
for (i = 0; i <= depth; i++) |
| 154 |
sputx(codestr[i], &(wbuf->curptr), wbuf->limptr); |
| 155 |
*(wbuf->curptr)++ = '>'; |
| 156 |
*(wbuf->curptr)++ = ' '; |
| 157 |
*(wbuf->curptr)++ = '<'; |
| 158 |
for (i = 0; i < mtab[c].len; i++) |
| 159 |
sputx(mtab[c].code[i], &(wbuf->curptr), wbuf->limptr); |
| 160 |
*(wbuf->curptr)++ = '>'; |
| 161 |
*(wbuf->curptr)++ = '\n'; |
| 162 |
count++; |
| 163 |
} |
| 164 |
break; |
| 165 |
case MAP_IS_NAME: |
| 166 |
ERROR("%s: Unexpected error...", CMAP_DEBUG_STR); |
| 167 |
break; |
| 168 |
case MAP_IS_NOTDEF: |
| 169 |
break; |
| 170 |
default: |
| 171 |
ERROR("%s: Unknown mapping type: %d", |
| 172 |
CMAP_DEBUG_STR, MAP_TYPE(mtab[c].flag)); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/* Flush if necessary */ |
| 178 |
if (count >= 100 || |
| 179 |
wbuf->curptr >= wbuf->limptr ) { |
| 180 |
char fmt_buf[32]; |
| 181 |
if (count > 100) |
| 182 |
ERROR("Unexpected error....: %d", count); |
| 183 |
sprintf(fmt_buf, "%d beginbfchar\n", count); |
| 184 |
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 185 |
pdf_add_stream(stream, |
| 186 |
wbuf->buf, (int) (wbuf->curptr - wbuf->buf)); |
| 187 |
wbuf->curptr = wbuf->buf; |
| 188 |
pdf_add_stream(stream, |
| 189 |
"endbfchar\n", strlen("endbfchar\n")); |
| 190 |
count = 0; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if (num_blocks > 0) { |
| 195 |
char fmt_buf[32]; |
| 196 |
|
| 197 |
if (count > 0) { |
| 198 |
sprintf(fmt_buf, "%d beginbfchar\n", count); |
| 199 |
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 200 |
pdf_add_stream(stream, |
| 201 |
wbuf->buf, (int) (wbuf->curptr - wbuf->buf)); |
| 202 |
wbuf->curptr = wbuf->buf; |
| 203 |
pdf_add_stream(stream, |
| 204 |
"endbfchar\n", strlen("endbfchar\n")); |
| 205 |
count = 0; |
| 206 |
} |
| 207 |
sprintf(fmt_buf, "%d beginbfrange\n", num_blocks); |
| 208 |
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 209 |
for (i = 0; i < num_blocks; i++) { |
| 210 |
int j; |
| 211 |
|
| 212 |
c = blocks[i].start; |
| 213 |
*(wbuf->curptr)++ = '<'; |
| 214 |
for (j = 0; j < depth; j++) |
| 215 |
sputx(codestr[j], &(wbuf->curptr), wbuf->limptr); |
| 216 |
sputx((unsigned char)c, &(wbuf->curptr), wbuf->limptr); |
| 217 |
*(wbuf->curptr)++ = '>'; |
| 218 |
*(wbuf->curptr)++ = ' '; |
| 219 |
*(wbuf->curptr)++ = '<'; |
| 220 |
for (j = 0; j < depth; j++) |
| 221 |
sputx(codestr[j], &(wbuf->curptr), wbuf->limptr); |
| 222 |
sputx((unsigned char)(c + blocks[i].count), &(wbuf->curptr), wbuf->limptr); |
| 223 |
*(wbuf->curptr)++ = '>'; |
| 224 |
*(wbuf->curptr)++ = ' '; |
| 225 |
*(wbuf->curptr)++ = '<'; |
| 226 |
for (j = 0; j < mtab[c].len; j++) |
| 227 |
sputx(mtab[c].code[j], &(wbuf->curptr), wbuf->limptr); |
| 228 |
*(wbuf->curptr)++ = '>'; |
| 229 |
*(wbuf->curptr)++ = '\n'; |
| 230 |
} |
| 231 |
pdf_add_stream(stream, |
| 232 |
wbuf->buf, (int) (wbuf->curptr - wbuf->buf)); |
| 233 |
wbuf->curptr = wbuf->buf; |
| 234 |
pdf_add_stream(stream, |
| 235 |
"endbfrange\n", strlen("endbfrange\n")); |
| 236 |
} |
| 237 |
|
| 238 |
return count; |
| 239 |
} |
| 240 |
|
| 241 |
/* duplicated from pdfobj.c */ |
| 242 |
static void |
| 243 |
write_string (char **outptr, char *endptr, const char *string_data) |
| 244 |
{ |
| 245 |
char *p; |
| 246 |
const char *s; |
| 247 |
int i, length; |
| 248 |
|
| 249 |
s = string_data; |
| 250 |
length = string_data ? strlen(string_data) : 0; |
| 251 |
p = *outptr; |
| 252 |
|
| 253 |
*p++ = '('; |
| 254 |
for (i = 0; i < length; i++) { |
| 255 |
unsigned char ch; |
| 256 |
|
| 257 |
ch = s[i]; |
| 258 |
if (ch < 32 || ch > 126) { |
| 259 |
p += sprintf(p, "\\%03o", ch); |
| 260 |
} else { |
| 261 |
switch (ch) { |
| 262 |
case '(': case ')': case '\\': |
| 263 |
*p++ = '\\'; |
| 264 |
*p++ = ch; |
| 265 |
break; |
| 266 |
default: |
| 267 |
*p++ = ch; |
| 268 |
break; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
*p++ = ')'; |
| 273 |
|
| 274 |
*outptr = p; |
| 275 |
} |
| 276 |
|
| 277 |
static void |
| 278 |
write_name (char **outptr, char *endptr, const char *name_data) |
| 279 |
{ |
| 280 |
char *p; |
| 281 |
const char *s; |
| 282 |
int i, length; |
| 283 |
|
| 284 |
s = name_data; |
| 285 |
length = name_data ? strlen(name_data) : 0; |
| 286 |
p = *outptr; |
| 287 |
#ifndef is_delim |
| 288 |
/* Avoid '{' and '}' for PostScript compatibility? */ |
| 289 |
#define is_delim(c) ((c) == '(' || (c) == ')' || \ |
| 290 |
(c) == '/' || \ |
| 291 |
(c) == '<' || (c) == '>' || \ |
| 292 |
(c) == '[' || (c) == ']' || \ |
| 293 |
(c) == '{' || (c) == '}' || \ |
| 294 |
(c) == '%') |
| 295 |
#endif |
| 296 |
*p++ = '/'; |
| 297 |
for (i = 0; i < length; i++) { |
| 298 |
if (s[i] < '!' || s[i] > '~' || s[i] == '#' || is_delim(s[i])) { |
| 299 |
/* ^ "space" is here. */ |
| 300 |
*p++ = '#'; |
| 301 |
sputx(s[i], &p, endptr); |
| 302 |
} else { |
| 303 |
*p++ = s[i]; |
| 304 |
} |
| 305 |
} |
| 306 |
*outptr = p; |
| 307 |
} |
| 308 |
|
| 309 |
#define CMAP_BEGIN "\ |
| 310 |
/CIDInit /ProcSet findresource begin\n\ |
| 311 |
12 dict begin\n\ |
| 312 |
begincmap\n\ |
| 313 |
" |
| 314 |
|
| 315 |
#define CMAP_END "\ |
| 316 |
endcmap\n\ |
| 317 |
CMapName currentdict /CMap defineresource pop\n\ |
| 318 |
end\n\ |
| 319 |
end\n\ |
| 320 |
" |
| 321 |
|
| 322 |
pdf_obj * |
| 323 |
CMap_create_stream (CMap *cmap) |
| 324 |
{ |
| 325 |
pdf_obj *stream; |
| 326 |
pdf_obj *stream_dict; |
| 327 |
CIDSysInfo *csi; |
| 328 |
struct sbuf wbuf; |
| 329 |
struct rangeDef *ranges; |
| 330 |
unsigned char *codestr; |
| 331 |
int i, j, count = 0; |
| 332 |
|
| 333 |
if (!cmap || !CMap_is_valid(cmap)) { |
| 334 |
WARN("Invalid CMap"); |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
if (cmap->type == CMAP_TYPE_IDENTITY) |
| 339 |
return NULL; |
| 340 |
|
| 341 |
stream = pdf_new_stream(STREAM_COMPRESS); |
| 342 |
stream_dict = pdf_stream_dict(stream); |
| 343 |
|
| 344 |
csi = CMap_get_CIDSysInfo(cmap); |
| 345 |
if (!csi) { |
| 346 |
csi = (cmap->type != CMAP_TYPE_TO_UNICODE) ? |
| 347 |
&CSI_IDENTITY : &CSI_UNICODE; |
| 348 |
} |
| 349 |
|
| 350 |
if (cmap->type != CMAP_TYPE_TO_UNICODE) { |
| 351 |
pdf_obj *csi_dict; |
| 352 |
|
| 353 |
csi_dict = pdf_new_dict(); |
| 354 |
pdf_add_dict(csi_dict, |
| 355 |
pdf_new_name("Registry"), |
| 356 |
pdf_new_string(csi->registry, strlen(csi->registry))); |
| 357 |
pdf_add_dict(csi_dict, |
| 358 |
pdf_new_name("Ordering"), |
| 359 |
pdf_new_string(csi->ordering, strlen(csi->ordering))); |
| 360 |
pdf_add_dict(csi_dict, |
| 361 |
pdf_new_name("Supplement"), |
| 362 |
pdf_new_number(csi->supplement)); |
| 363 |
|
| 364 |
pdf_add_dict(stream_dict, |
| 365 |
pdf_new_name("Type"), |
| 366 |
pdf_new_name("CMap")); |
| 367 |
pdf_add_dict(stream_dict, |
| 368 |
pdf_new_name("CMapName"), |
| 369 |
pdf_new_name(cmap->name)); |
| 370 |
pdf_add_dict(stream_dict, |
| 371 |
pdf_new_name("CIDSystemInfo"), csi_dict); |
| 372 |
if (cmap->wmode != 0) |
| 373 |
pdf_add_dict(stream_dict, |
| 374 |
pdf_new_name("WMode"), |
| 375 |
pdf_new_number(cmap->wmode)); |
| 376 |
} |
| 377 |
|
| 378 |
/* TODO: |
| 379 |
* Predefined CMaps need not to be embedded. |
| 380 |
*/ |
| 381 |
if (cmap->useCMap) { |
| 382 |
ERROR("UseCMap found (not supported yet)..."); |
| 383 |
if (CMap_is_Identity(cmap->useCMap)) { /* not sure */ |
| 384 |
if (CMap_get_wmode(cmap) == 1) { |
| 385 |
pdf_add_dict(stream_dict, |
| 386 |
pdf_new_name("UseCMap"), |
| 387 |
pdf_new_name("Identity-V")); |
| 388 |
} else { |
| 389 |
pdf_add_dict(stream_dict, |
| 390 |
pdf_new_name("UseCMap"), |
| 391 |
pdf_new_name("Identity-H")); |
| 392 |
} |
| 393 |
} else { |
| 394 |
int res_id; |
| 395 |
pdf_obj *ucmap_ref; |
| 396 |
|
| 397 |
res_id = pdf_findresource("CMap", CMap_get_name(cmap->useCMap)); |
| 398 |
if (res_id >= 0) { |
| 399 |
ucmap_ref = pdf_get_resource_reference(res_id); |
| 400 |
} else { |
| 401 |
pdf_obj *ucmap_obj; |
| 402 |
|
| 403 |
ucmap_obj = CMap_create_stream(cmap->useCMap); |
| 404 |
if (!ucmap_obj) { |
| 405 |
ERROR("Uh ah. I cannot continue..."); |
| 406 |
} |
| 407 |
|
| 408 |
res_id = pdf_defineresource("CMap", |
| 409 |
CMap_get_name(cmap->useCMap), |
| 410 |
ucmap_obj, PDF_RES_FLUSH_IMMEDIATE); |
| 411 |
ucmap_ref = pdf_get_resource_reference(res_id); |
| 412 |
} |
| 413 |
pdf_add_dict(stream_dict, pdf_new_name("UseCMap"), ucmap_ref); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
#define WBUF_SIZE 40960 |
| 418 |
wbuf.buf = NEW(WBUF_SIZE, char); |
| 419 |
codestr = NEW(cmap->profile.maxBytesIn, unsigned char); |
| 420 |
memset(codestr, 0, cmap->profile.maxBytesIn); |
| 421 |
|
| 422 |
wbuf.curptr = wbuf.buf; |
| 423 |
wbuf.limptr = wbuf.buf + WBUF_SIZE - |
| 424 |
2 * (cmap->profile.maxBytesIn + cmap->profile.maxBytesOut) + 16; |
| 425 |
|
| 426 |
/* Start CMap */ |
| 427 |
pdf_add_stream(stream, (const void *) CMAP_BEGIN, strlen(CMAP_BEGIN)); |
| 428 |
|
| 429 |
wbuf.curptr += sprintf(wbuf.curptr, "/CMapName "); |
| 430 |
write_name(&wbuf.curptr, wbuf.limptr, cmap->name); |
| 431 |
wbuf.curptr += sprintf(wbuf.curptr, " def\n"); |
| 432 |
wbuf.curptr += sprintf(wbuf.curptr, "/CMapType %d def\n" , cmap->type); |
| 433 |
if (cmap->wmode != 0 && |
| 434 |
cmap->type != CMAP_TYPE_TO_UNICODE) |
| 435 |
wbuf.curptr += sprintf(wbuf.curptr, "/WMode %d def\n", cmap->wmode); |
| 436 |
|
| 437 |
#define CMAP_CSI_FMT "/CIDSystemInfo <<\n\ |
| 438 |
/Registry (%s)\n\ |
| 439 |
/Ordering (%s)\n\ |
| 440 |
/Supplement %d\n\ |
| 441 |
>> def\n" |
| 442 |
wbuf.curptr += sprintf(wbuf.curptr, "/CIDSystemInfo <<\n"); |
| 443 |
wbuf.curptr += sprintf(wbuf.curptr, " /Registry "); |
| 444 |
write_string(&wbuf.curptr, wbuf.limptr, csi->registry); |
| 445 |
wbuf.curptr += sprintf(wbuf.curptr, "\n"); |
| 446 |
wbuf.curptr += sprintf(wbuf.curptr, " /Ordering "); |
| 447 |
write_string(&wbuf.curptr, wbuf.limptr, csi->ordering); |
| 448 |
wbuf.curptr += sprintf(wbuf.curptr, "\n"); |
| 449 |
wbuf.curptr += sprintf(wbuf.curptr, " /Supplement %d\n>> def\n", |
| 450 |
csi->supplement); |
| 451 |
pdf_add_stream(stream, wbuf.buf, (int)(wbuf.curptr - wbuf.buf)); |
| 452 |
wbuf.curptr = wbuf.buf; |
| 453 |
|
| 454 |
/* codespacerange */ |
| 455 |
ranges = cmap->codespace.ranges; |
| 456 |
wbuf.curptr += sprintf(wbuf.curptr, |
| 457 |
"%d begincodespacerange\n", cmap->codespace.num); |
| 458 |
for (i = 0; i < cmap->codespace.num; i++) { |
| 459 |
*(wbuf.curptr)++ = '<'; |
| 460 |
for (j = 0; j < ranges[i].dim; j++) { |
| 461 |
sputx(ranges[i].codeLo[j], &(wbuf.curptr), wbuf.limptr); |
| 462 |
} |
| 463 |
*(wbuf.curptr)++ = '>'; |
| 464 |
*(wbuf.curptr)++ = ' '; |
| 465 |
*(wbuf.curptr)++ = '<'; |
| 466 |
for (j = 0; j < ranges[i].dim; j++) { |
| 467 |
sputx(ranges[i].codeHi[j], &(wbuf.curptr), wbuf.limptr); |
| 468 |
} |
| 469 |
*(wbuf.curptr)++ = '>'; |
| 470 |
*(wbuf.curptr)++ = '\n'; |
| 471 |
} |
| 472 |
pdf_add_stream(stream, wbuf.buf, (int)(wbuf.curptr - wbuf.buf)); |
| 473 |
wbuf.curptr = wbuf.buf; |
| 474 |
pdf_add_stream(stream, |
| 475 |
"endcodespacerange\n", strlen("endcodespacerange\n")); |
| 476 |
|
| 477 |
/* CMap body */ |
| 478 |
if (cmap->mapTbl) { |
| 479 |
count = write_map(cmap->mapTbl, |
| 480 |
0, codestr, 0, &wbuf, stream); /* Top node */ |
| 481 |
if (count > 0) { /* Flush */ |
| 482 |
char fmt_buf[32]; |
| 483 |
if (count > 100) |
| 484 |
ERROR("Unexpected error....: %d", count); |
| 485 |
sprintf(fmt_buf, "%d beginbfchar\n", count); |
| 486 |
pdf_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 487 |
pdf_add_stream(stream, |
| 488 |
wbuf.buf, (int) (wbuf.curptr - wbuf.buf)); |
| 489 |
pdf_add_stream(stream, |
| 490 |
"endbfchar\n", strlen("endbfchar\n")); |
| 491 |
count = 0; |
| 492 |
wbuf.curptr = wbuf.buf; |
| 493 |
} |
| 494 |
} |
| 495 |
/* End CMap */ |
| 496 |
pdf_add_stream(stream, CMAP_END, strlen(CMAP_END)); |
| 497 |
|
| 498 |
RELEASE(codestr); |
| 499 |
RELEASE(wbuf.buf); |
| 500 |
|
| 501 |
return stream; |
| 502 |
} |
| 503 |
|
| 504 |
#if 0 |
| 505 |
/* Not completed yet */ |
| 506 |
|
| 507 |
static int |
| 508 |
add_inverse_map (CMap *icmap, mapDef *mtab, |
| 509 |
unsigned char *codestr, int depth, |
| 510 |
unsigned char *used_slot) |
| 511 |
{ |
| 512 |
CID cid; |
| 513 |
int c; |
| 514 |
mapDef *mtab1; |
| 515 |
|
| 516 |
for (c = 0; c < 256; c++) { |
| 517 |
codestr[depth] = (unsigned char) (c & 0xff); |
| 518 |
if (LOOKUP_CONTINUE(mtab[c].flag)) { |
| 519 |
mtab1 = mtab[c].next; |
| 520 |
add_inverse_map(icmap, mtab1, codestr, depth + 1, used_slot); |
| 521 |
} else { |
| 522 |
if (MAP_DEFINED(mtab[c].flag)) { |
| 523 |
switch (MAP_TYPE(mtab[c].flag)) { |
| 524 |
/* We should restrict it to to-CID mapping. |
| 525 |
* However... |
| 526 |
*/ |
| 527 |
case MAP_IS_CID: case MAP_IS_CODE: |
| 528 |
if (mtab[c].len == 2) { |
| 529 |
cid = (mtab[c].code[0] << 8)|mtab[c].code[1]; |
| 530 |
#ifndef is_used_char2 |
| 531 |
#define is_used_char2(b,c) (((b)[(c)/8]) & (1 << (7-((c)%8)))) |
| 532 |
#endif |
| 533 |
if (!used_slot || |
| 534 |
is_used_char2(used_slot, cid)) { |
| 535 |
CMap_add_bfchar(icmap, |
| 536 |
mtab[c].code, mtab[c].len, |
| 537 |
codestr, depth + 1); |
| 538 |
} |
| 539 |
} |
| 540 |
break; |
| 541 |
case MAP_IS_NAME: |
| 542 |
ERROR("%s: Unexpected error...", CMAP_DEBUG_STR); |
| 543 |
break; |
| 544 |
case MAP_IS_NOTDEF: |
| 545 |
break; |
| 546 |
default: |
| 547 |
ERROR("%s: Unknown mapping type: %d", |
| 548 |
CMAP_DEBUG_STR, MAP_TYPE(mtab[c].flag)); |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
static int |
| 558 |
add_map (CMap *cmap, mapDef *mtab, |
| 559 |
unsigned char *codestr, int depth) |
| 560 |
{ |
| 561 |
int c; |
| 562 |
mapDef *mtab1; |
| 563 |
|
| 564 |
for (c = 0; c < 256; c++) { |
| 565 |
codestr[depth] = (unsigned char) (c & 0xff); |
| 566 |
if (LOOKUP_CONTINUE(mtab[c].flag)) { |
| 567 |
mtab1 = mtab[c].next; |
| 568 |
add_map(cmap, mtab1, codestr, depth + 1); |
| 569 |
} else { |
| 570 |
if (MAP_DEFINED(mtab[c].flag)) { |
| 571 |
switch (MAP_TYPE(mtab[c].flag)) { |
| 572 |
case MAP_IS_CID: case MAP_IS_CODE: |
| 573 |
CMap_add_bfchar(cmap, |
| 574 |
codestr, depth + 1, |
| 575 |
mtab[c].code, mtab[c].len); |
| 576 |
break; |
| 577 |
case MAP_IS_NAME: |
| 578 |
ERROR("%s: Unexpected error...", CMAP_DEBUG_STR); |
| 579 |
break; |
| 580 |
case MAP_IS_NOTDEF: |
| 581 |
break; |
| 582 |
default: |
| 583 |
ERROR("%s: Unknown mapping type: %d", |
| 584 |
CMAP_DEBUG_STR, MAP_TYPE(mtab[c].flag)); |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
static CMap * |
| 595 |
invert_cmap (CMap *cmap, unsigned char *used_slot) |
| 596 |
{ |
| 597 |
CMap *icmap; |
| 598 |
unsigned char *codestr; |
| 599 |
unsigned char range_min[2] = {0x00, 0x00}; /* CID */ |
| 600 |
unsigned char range_max[2] = {0xff, 0xfe}; /* CID */ |
| 601 |
|
| 602 |
cmap = flatten_cmap(cmap); |
| 603 |
ASSERT(cmap); |
| 604 |
|
| 605 |
icmap = CMap_new(); |
| 606 |
CMap_set_type(icmap, CMAP_TYPE_CID_TO_CODE); |
| 607 |
CMap_add_codespacerange(icmap, range_min, range_max, 2); |
| 608 |
|
| 609 |
if (cmap->mapTbl) { |
| 610 |
codestr = NEW(cmap->profile.maxBytesIn, unsigned char); |
| 611 |
memset(codestr, 0, cmap->profile.maxBytesIn); |
| 612 |
add_inverse_map(icmap, cmap->mapTbl, |
| 613 |
codestr, 0, used_slot); /* top node */ |
| 614 |
RELEASE(codestr); |
| 615 |
} |
| 616 |
|
| 617 |
CMap_release(cmap); |
| 618 |
|
| 619 |
return icmap; |
| 620 |
} |
| 621 |
|
| 622 |
static CMap * |
| 623 |
flatten_cmap (CMap *cmap) |
| 624 |
{ |
| 625 |
CMap *fcmap; |
| 626 |
rangeDef *range; |
| 627 |
int i; |
| 628 |
|
| 629 |
ASSERT(cmap); |
| 630 |
|
| 631 |
fcmap = CMap_new(); |
| 632 |
|
| 633 |
for (i = 0; i < cmap->codespace.num; i++) { |
| 634 |
range = &(cmap->codespace.ranges[i]); |
| 635 |
CMap_add_codespacerange(fcmap, range->codeLo, range->codeHi, range->dim); |
| 636 |
} |
| 637 |
CMap_set_CIDSysInfo(fcmap, cmap->CSI); |
| 638 |
CMap_set_type (fcmap, cmap->type ); |
| 639 |
CMap_set_wmode(fcmap, cmap->wmode); |
| 640 |
|
| 641 |
fcmap->flags = cmap->flags; |
| 642 |
fcmap->version = cmap->version; |
| 643 |
|
| 644 |
while (cmap) { |
| 645 |
if (cmap->mapTbl) { |
| 646 |
unsigned char *codestr; |
| 647 |
|
| 648 |
codestr = NEW(cmap->profile.maxBytesIn, unsigned char); |
| 649 |
memset(codestr, 0, cmap->profile.maxBytesIn); |
| 650 |
add_map(fcmap, cmap->mapTbl, codestr, 0); /* top node */ |
| 651 |
RELEASE(codestr); |
| 652 |
} |
| 653 |
cmap = cmap->useCMap; |
| 654 |
} |
| 655 |
|
| 656 |
return fcmap; |
| 657 |
} |
| 658 |
|
| 659 |
pdf_obj * |
| 660 |
CMap_ToCode_stream (CMap *cmap, const char *cmap_name, |
| 661 |
CIDSysInfo *csi, int cmap_type, |
| 662 |
unsigned char *used_slot, int flags) |
| 663 |
{ |
| 664 |
pdf_obj *stream = NULL; |
| 665 |
CMap *icmap; |
| 666 |
|
| 667 |
ASSERT(cmap && cmap_name); |
| 668 |
|
| 669 |
if (cmap->type != |
| 670 |
CMAP_TYPE_CODE_TO_CID) |
| 671 |
return NULL; |
| 672 |
|
| 673 |
icmap = invert_cmap(cmap, used_slot); |
| 674 |
if (icmap) { |
| 675 |
CMap_set_name(icmap, cmap_name); |
| 676 |
if (csi) |
| 677 |
CMap_set_CIDSysInfo(icmap, csi); |
| 678 |
else { |
| 679 |
CMap_set_CIDSysInfo(icmap, &(CSI_IDENTITY)); |
| 680 |
} |
| 681 |
CMap_set_type(icmap, cmap_type); |
| 682 |
|
| 683 |
stream = CMap_create_stream(icmap, flags); |
| 684 |
|
| 685 |
CMap_release(icmap); |
| 686 |
} |
| 687 |
|
| 688 |
return stream; |
| 689 |
} |
| 690 |
#endif /* 0 */ |