| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2006 Han The Thanh, <[email protected]> |
| 4 |
Copyright 2006-2010 Taco Hoekwater <[email protected]> |
| 5 |
|
| 6 |
This file is part of LuaTeX. |
| 7 |
|
| 8 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 9 |
the terms of the GNU General Public License as published by the Free |
| 10 |
Software Foundation; either version 2 of the License, or (at your |
| 11 |
option) any later version. |
| 12 |
|
| 13 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 |
License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License along |
| 19 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ptexlib.h" |
| 24 |
|
| 25 |
#define isXdigit(c) (isdigit(c) || ('A' <= (c) && (c) <= 'F')) |
| 26 |
|
| 27 |
/*tex |
| 28 |
|
| 29 |
|UNIC_STRING| is a string allocated by |def_tounicode| while |
| 30 |
|UNI_EXTRA_STRING| is one allocated by |set_glyph_unicode|. |
| 31 |
|
| 32 |
*/ |
| 33 |
|
| 34 |
#define UNI_UNDEF -1 |
| 35 |
#define UNI_STRING -2 |
| 36 |
#define UNI_EXTRA_STRING -3 |
| 37 |
|
| 38 |
static struct avl_table *glyph_unicode_tree = NULL; |
| 39 |
|
| 40 |
static int comp_glyph_unicode_entry(const void *pa, const void *pb, void *p) |
| 41 |
{ |
| 42 |
(void) p; |
| 43 |
return strcmp(((const glyph_unicode_entry *) pa)->name, ((const glyph_unicode_entry *) pb)->name); |
| 44 |
} |
| 45 |
|
| 46 |
static glyph_unicode_entry *new_glyph_unicode_entry(void) |
| 47 |
{ |
| 48 |
glyph_unicode_entry *e; |
| 49 |
e = xtalloc(1, glyph_unicode_entry); |
| 50 |
e->name = NULL; |
| 51 |
e->code = UNI_UNDEF; |
| 52 |
e->unicode_seq = NULL; |
| 53 |
return e; |
| 54 |
} |
| 55 |
|
| 56 |
static void destroy_glyph_unicode_entry(void *pa, void *pb) |
| 57 |
{ |
| 58 |
glyph_unicode_entry *e = (glyph_unicode_entry *) pa; |
| 59 |
(void) pb; |
| 60 |
xfree(e->name); |
| 61 |
if (e->code == UNI_STRING) { |
| 62 |
xfree(e->unicode_seq); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void glyph_unicode_free(void) |
| 67 |
{ |
| 68 |
if (glyph_unicode_tree != NULL) |
| 69 |
avl_destroy(glyph_unicode_tree, destroy_glyph_unicode_entry); |
| 70 |
} |
| 71 |
|
| 72 |
void glyph_unicode_new(void) |
| 73 |
{ |
| 74 |
if (glyph_unicode_tree == NULL) { |
| 75 |
glyph_unicode_tree = avl_create(comp_glyph_unicode_entry, NULL, &avl_xallocator); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void def_tounicode(str_number glyph, str_number unistr) |
| 80 |
{ |
| 81 |
char buf[SMALL_BUF_SIZE], *p, *ph; |
| 82 |
char buf2[SMALL_BUF_SIZE], *q; |
| 83 |
/*tex |0| is invalid, |1| an \UNICODE\ value and |2| a string */ |
| 84 |
int valid_unistr; |
| 85 |
int i, l; |
| 86 |
glyph_unicode_entry *gu, t; |
| 87 |
void **aa; |
| 88 |
p = makecstring(glyph); |
| 89 |
assert(strlen(p) < SMALL_BUF_SIZE); |
| 90 |
strcpy(buf, p); |
| 91 |
free(p); |
| 92 |
p = makecstring(unistr); |
| 93 |
ph = p; |
| 94 |
/*tex Ignore leading spaces. */ |
| 95 |
while (*p == ' ') |
| 96 |
p++; |
| 97 |
l = (int) strlen(p); |
| 98 |
/*tex Ignore traling spaces. */ |
| 99 |
while (l > 0 && p[l - 1] == ' ') |
| 100 |
l--; |
| 101 |
/*tex A \UNICODE\ value is the most common case. */ |
| 102 |
valid_unistr = 1; |
| 103 |
for (i = 0; i < l; i++) { |
| 104 |
/*tex If a space occurs we treat this entry as a string. */ |
| 105 |
if (p[i] == ' ') |
| 106 |
valid_unistr = 2; |
| 107 |
else if (!isXdigit((unsigned char)p[i])) { |
| 108 |
valid_unistr = 0; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
if (l == 0 || valid_unistr == 0 || strlen(buf) == 0 || strcmp(buf, notdef) == 0) { |
| 113 |
formatted_warning("tounicode", "invalid parameter(s): %s -> %s", buf, p); |
| 114 |
return; |
| 115 |
} |
| 116 |
if (glyph_unicode_tree == NULL) { |
| 117 |
glyph_unicode_tree = avl_create(comp_glyph_unicode_entry, NULL, &avl_xallocator); |
| 118 |
} |
| 119 |
t.name = buf; |
| 120 |
/*tex Allow overriding existing entries. */ |
| 121 |
if ((gu = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &t)) != NULL) { |
| 122 |
if (gu->code == UNI_STRING) { |
| 123 |
xfree(gu->unicode_seq); |
| 124 |
} |
| 125 |
} else { |
| 126 |
/*tex Make a new entry. */ |
| 127 |
gu = new_glyph_unicode_entry(); |
| 128 |
gu->name = xstrdup(buf); |
| 129 |
} |
| 130 |
if (valid_unistr == 2) { |
| 131 |
/*tex A string can have space(s) that we ignore by copying |p| to |buf2|. */ |
| 132 |
for (q = buf2; *p != 0; p++) |
| 133 |
if (*p != ' ') |
| 134 |
*q++ = *p; |
| 135 |
*q = 0; |
| 136 |
gu->code = UNI_STRING; |
| 137 |
gu->unicode_seq = xstrdup(buf2); |
| 138 |
} else { |
| 139 |
i = sscanf(p, "%lX", &(gu->code)); |
| 140 |
} |
| 141 |
aa = avl_probe(glyph_unicode_tree, gu); |
| 142 |
assert(aa != NULL); |
| 143 |
free(ph); |
| 144 |
} |
| 145 |
|
| 146 |
static long check_unicode_value(char *s, boolean multiple_value) |
| 147 |
{ |
| 148 |
int l = (int) strlen(s); |
| 149 |
int i; |
| 150 |
/*tex Anything that is not |UNI_UNDEF| will do: */ |
| 151 |
long code = 0; |
| 152 |
if (l == 0) |
| 153 |
return UNI_UNDEF; |
| 154 |
if (multiple_value && l % 4 != 0) |
| 155 |
return UNI_UNDEF; |
| 156 |
if (!multiple_value && !(4 <= l && l <= 6)) |
| 157 |
return UNI_UNDEF; |
| 158 |
for (i = 0; i < l; i++) { |
| 159 |
if (!isXdigit((unsigned char)s[i])) |
| 160 |
return UNI_UNDEF; |
| 161 |
if (multiple_value) { |
| 162 |
if (i % 4 == 3) { |
| 163 |
if (sscanf(s + i - 3, "%4lX", &code) != 1) |
| 164 |
return UNI_UNDEF; |
| 165 |
if (!((0x0000 <= code && code <= 0xD7FF) || |
| 166 |
(0xE000 <= code && code <= 0xFFFF))) |
| 167 |
return UNI_UNDEF; |
| 168 |
} |
| 169 |
} else { /* single value */ |
| 170 |
if (i == l - 1) { |
| 171 |
if (sscanf(s, "%lX", &code) != 1) |
| 172 |
return UNI_UNDEF; |
| 173 |
if (!((0x0000 <= code && code <= 0xD7FF) || |
| 174 |
(0xE000 <= code && code <= 0x10FFFF))) |
| 175 |
return UNI_UNDEF; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
return code; |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
|
| 184 |
This function set proper values to |*gp| based on |s|; in case it returns |
| 185 |
|gp->code == UNI_EXTRA_STRING| then the caller is responsible for freeing |
| 186 |
|gp->unicode_seq| too. |
| 187 |
|
| 188 |
*/ |
| 189 |
|
| 190 |
static void set_glyph_unicode(char *s, glyph_unicode_entry * gp) |
| 191 |
{ |
| 192 |
char buf[SMALL_BUF_SIZE], buf2[SMALL_BUF_SIZE], *p; |
| 193 |
long code; |
| 194 |
boolean last_component; |
| 195 |
glyph_unicode_entry tmp, *ptmp; |
| 196 |
/*tex Skip dummy entries. */ |
| 197 |
if (s == NULL || s == notdef) |
| 198 |
return; |
| 199 |
/*tex Strip everything after the first dot. */ |
| 200 |
p = strchr(s, '.'); |
| 201 |
if (p != NULL) { |
| 202 |
*buf = 0; |
| 203 |
strncat(buf, s, (size_t) (p - s)); |
| 204 |
s = buf; |
| 205 |
} |
| 206 |
if (strlen(s) == 0) |
| 207 |
return; |
| 208 |
/*tex Check for case of multiple components separated by |_|. */ |
| 209 |
p = strchr(s, '_'); |
| 210 |
if (p != NULL) { |
| 211 |
assert(strlen(s) < sizeof(buf)); |
| 212 |
if (s != buf) { |
| 213 |
strcpy(buf, s); |
| 214 |
p = strchr(buf, '_'); |
| 215 |
s = buf; |
| 216 |
} |
| 217 |
*buf2 = 0; |
| 218 |
last_component = false; |
| 219 |
for (;;) { |
| 220 |
*p = 0; |
| 221 |
tmp.code = UNI_UNDEF; |
| 222 |
set_glyph_unicode(s, &tmp); |
| 223 |
switch (tmp.code) { |
| 224 |
case UNI_UNDEF: |
| 225 |
/*tex Not found, do nothing. */ |
| 226 |
break; |
| 227 |
case UNI_STRING: |
| 228 |
/*tex |s| matched an entry with string value in the database. */ |
| 229 |
assert(tmp.unicode_seq != NULL); |
| 230 |
assert(strlen(buf2) + strlen(tmp.unicode_seq) < sizeof(buf2)); |
| 231 |
strcat(buf2, tmp.unicode_seq); |
| 232 |
break; |
| 233 |
case UNI_EXTRA_STRING: |
| 234 |
/*tex |s| is a multiple value of form "uniXXXX" */ |
| 235 |
assert(strlen(buf2) + strlen(tmp.unicode_seq) < sizeof(buf2)); |
| 236 |
strcat(buf2, tmp.unicode_seq); |
| 237 |
xfree(tmp.unicode_seq); |
| 238 |
break; |
| 239 |
default: |
| 240 |
/*tex |
| 241 |
|s| matched an entry with numeric value in the database, or a |
| 242 |
value derived from |uXXXX|. |
| 243 |
*/ |
| 244 |
assert(tmp.code >= 0); |
| 245 |
strcat(buf2, utf16be_str(tmp.code)); |
| 246 |
} |
| 247 |
if (last_component) |
| 248 |
break; |
| 249 |
s = p + 1; |
| 250 |
p = strchr(s, '_'); |
| 251 |
if (p == NULL) { |
| 252 |
p = strend(s); |
| 253 |
last_component = true; |
| 254 |
} |
| 255 |
} |
| 256 |
gp->code = UNI_EXTRA_STRING; |
| 257 |
gp->unicode_seq = xstrdup(buf2); |
| 258 |
return; |
| 259 |
} |
| 260 |
/*tex Lookup glyph name in the database. */ |
| 261 |
tmp.name = s; |
| 262 |
tmp.code = UNI_UNDEF; |
| 263 |
ptmp = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &tmp); |
| 264 |
if (ptmp != NULL) { |
| 265 |
gp->code = ptmp->code; |
| 266 |
gp->unicode_seq = ptmp->unicode_seq; |
| 267 |
return; |
| 268 |
} |
| 269 |
/*tex Check for case of |uniXXXX|, multiple 4-hex-digit values allowed. */ |
| 270 |
if (str_prefix(s, "uni")) { |
| 271 |
p = s + strlen("uni"); |
| 272 |
code = check_unicode_value(p, true); |
| 273 |
if (code != UNI_UNDEF) { |
| 274 |
if (strlen(p) == 4) { |
| 275 |
/*tex Single value: */ |
| 276 |
gp->code = code; |
| 277 |
} else { |
| 278 |
/*tex Multiple value: */ |
| 279 |
gp->code = UNI_EXTRA_STRING; |
| 280 |
gp->unicode_seq = xstrdup(p); |
| 281 |
} |
| 282 |
} |
| 283 |
/*tex Since the last case cannot happen: */ |
| 284 |
return; |
| 285 |
} |
| 286 |
/*tex Check for case of |uXXXX|, a single value up to 6 hex digits. */ |
| 287 |
if (str_prefix(s, "u")) { |
| 288 |
p = s + strlen("u"); |
| 289 |
code = check_unicode_value(p, false); |
| 290 |
if (code != UNI_UNDEF) { |
| 291 |
assert(code >= 0); |
| 292 |
gp->code = code; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* |
| 298 |
static void set_cid_glyph_unicode(long index, glyph_unicode_entry * gp, internal_font_number f) |
| 299 |
{ |
| 300 |
char *s; |
| 301 |
if (font_tounicode(f)) { |
| 302 |
if ((s = get_charinfo_tounicode(char_info(f, (int) index))) != NULL) { |
| 303 |
gp->code = UNI_EXTRA_STRING; |
| 304 |
gp->unicode_seq = xstrdup(s); |
| 305 |
} else { |
| 306 |
// No fall back as we're providing them ourselves. |
| 307 |
} |
| 308 |
} else { |
| 309 |
// Fall back |
| 310 |
gp->code = index; |
| 311 |
} |
| 312 |
} |
| 313 |
*/ |
| 314 |
|
| 315 |
static boolean is_last_byte_valid(int srcCode1, int srcCode2, long code) |
| 316 |
{ |
| 317 |
/*tex |
| 318 |
Followin pdfTeX, when defining ranges of this type, the value of the last byte in the |
| 319 |
string shall be less than or equal to 255 − (srcCode2 − srcCode1). This |
| 320 |
ensures that the last byte of the string shall not be incremented past |
| 321 |
255; otherwise, the result of mapping is undefined. |
| 322 |
*/ |
| 323 |
char *s = strend(utf16be_str(code)) - 2; |
| 324 |
long l = strtol(s, NULL, 16); |
| 325 |
return l < 255 - (srcCode2 - srcCode1); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static int do_write_tounicode(PDF pdf, char **glyph_names, char *name, internal_font_number f, int obey_f) |
| 331 |
{ |
| 332 |
char buf[SMALL_BUF_SIZE], *p, *s; |
| 333 |
static char builtin_suffix[] = "-builtin"; |
| 334 |
short range_size[257]; |
| 335 |
glyph_unicode_entry gtab[257]; |
| 336 |
int objnum; |
| 337 |
int i, j; |
| 338 |
int bfchar_count, bfrange_count, subrange_count; |
| 339 |
/* |
| 340 |
This is weird test: why completely disable this generation when we |
| 341 |
have no tree as a font can have one? So let's check. That way we are |
| 342 |
(sort of) compatible. Anyway, the gen_tounicode is set every time we |
| 343 |
do a font. |
| 344 |
*/ |
| 345 |
/* |
| 346 |
We could have a callback here that gets the font name and id passes so |
| 347 |
that one can do late mappings. |
| 348 |
*/ |
| 349 |
int done = 0; |
| 350 |
if (f) { |
| 351 |
for (i = 0; i < 256; ++i) { |
| 352 |
if ((s = get_charinfo_tounicode(char_info(f,(int)i))) != NULL) { |
| 353 |
done = 1; |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
if (done) { |
| 359 |
/* |
| 360 |
We're fine, some values in the font are set but we combien these with |
| 361 |
shared user set values at the tex end. |
| 362 |
*/ |
| 363 |
glyph_unicode_new(); /* play safe */ |
| 364 |
} else if (glyph_unicode_tree == NULL) { |
| 365 |
/* As before, we quit. */ |
| 366 |
pdf->gen_tounicode = 0; |
| 367 |
return 0; |
| 368 |
} else { |
| 369 |
/* |
| 370 |
We continue and try to assemble a vector, either or not completes with |
| 371 |
shared user defined values (at the tex end). |
| 372 |
*/ |
| 373 |
} |
| 374 |
if (name == NULL) { |
| 375 |
strcpy(buf, "no-name"); |
| 376 |
} else { |
| 377 |
strcpy(buf, name); |
| 378 |
} |
| 379 |
/* |
| 380 |
We never passed the f to write_fontdictionary but when we have a type one fonts we do |
| 381 |
want to honor the |
| 382 |
*/ |
| 383 |
if (f && obey_f) { |
| 384 |
/*tex |
| 385 |
Always. Old code path for raw. |
| 386 |
*/ |
| 387 |
strcat(buf, builtin_suffix); |
| 388 |
} else if ((p = strrchr(buf, '.')) != NULL && strcmp(p, ".enc") == 0) { |
| 389 |
/*tex |
| 390 |
Strip |.enc| from encoding name. |
| 391 |
*/ |
| 392 |
*p = 0; |
| 393 |
} else { |
| 394 |
/* |
| 395 |
The suffix |.enc| is not present so this is a builtin encoding so the |
| 396 |
name becomes e.g. |cmr10-builtin|. |
| 397 |
*/ |
| 398 |
strcat(buf, builtin_suffix); |
| 399 |
} |
| 400 |
/*tex Set gtab: */ |
| 401 |
if (f && obey_f) { |
| 402 |
/* |
| 403 |
This is again the case where we do a raw vector. Maybe needs checking. |
| 404 |
*/ |
| 405 |
int done = 0 ; |
| 406 |
for (i = 0; i < 256; ++i) { |
| 407 |
if ((s = get_charinfo_tounicode(char_info(f,(int)i))) != NULL) { |
| 408 |
gtab[i].code = UNI_EXTRA_STRING; |
| 409 |
gtab[i].unicode_seq = xstrdup(s); |
| 410 |
done = 1; |
| 411 |
} else { |
| 412 |
gtab[i].code = UNI_UNDEF; |
| 413 |
} |
| 414 |
} |
| 415 |
if (! done) { |
| 416 |
return 0; |
| 417 |
} |
| 418 |
} else { |
| 419 |
/* |
| 420 |
We end up here in case of a type one font with a given vector but also |
| 421 |
possible overloads via the tounicode settings. We let the tounicode |
| 422 |
entry win here. |
| 423 |
*/ |
| 424 |
const char *filename = font_name(f); |
| 425 |
unsigned basesize = filename ? (strlen(filename) + 20) : 0; |
| 426 |
for (i = 0; i < 256; ++i) { |
| 427 |
gtab[i].code = UNI_UNDEF; |
| 428 |
if (f) { |
| 429 |
if ((s = get_charinfo_tounicode(char_info(f,(int)i))) != NULL) { |
| 430 |
gtab[i].code = UNI_EXTRA_STRING; |
| 431 |
gtab[i].unicode_seq = xstrdup(s); |
| 432 |
} |
| 433 |
} |
| 434 |
if (glyph_unicode_tree && basesize && gtab[i].code == UNI_UNDEF) { |
| 435 |
glyph_unicode_entry tmp, *ptmp; |
| 436 |
char *s = xmalloc(basesize + (unsigned) strlen(glyph_names[i])); |
| 437 |
/* undocumented as not really tested well */ |
| 438 |
sprintf(s, "%s::%s", filename, glyph_names[i]); /* nice */ |
| 439 |
tmp.name = s; |
| 440 |
tmp.code = UNI_UNDEF; |
| 441 |
ptmp = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &tmp); |
| 442 |
if (ptmp == NULL) { |
| 443 |
sprintf(s, "tfm:%s/%s", filename, glyph_names[i]); /* ugly */ |
| 444 |
tmp.name = s; |
| 445 |
tmp.code = UNI_UNDEF; |
| 446 |
ptmp = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &tmp); |
| 447 |
} |
| 448 |
if (ptmp != NULL) { |
| 449 |
gtab[i].code = ptmp->code; |
| 450 |
gtab[i].unicode_seq = ptmp->unicode_seq; |
| 451 |
} |
| 452 |
xfree(s); |
| 453 |
} |
| 454 |
if (gtab[i].code == UNI_UNDEF) { |
| 455 |
set_glyph_unicode(glyph_names[i], >ab[i]); |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
gtab[256].code = UNI_UNDEF; |
| 460 |
/* */ |
| 461 |
objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 462 |
pdf_begin_obj(pdf, objnum, OBJSTM_NEVER); |
| 463 |
pdf_begin_dict(pdf); |
| 464 |
pdf_dict_add_streaminfo(pdf); |
| 465 |
pdf_end_dict(pdf); |
| 466 |
pdf_begin_stream(pdf); |
| 467 |
pdf_printf(pdf, |
| 468 |
"%%!PS-Adobe-3.0 Resource-CMap\n" |
| 469 |
"%%%%DocumentNeededResources: ProcSet (CIDInit)\n" |
| 470 |
"%%%%IncludeResource: ProcSet (CIDInit)\n" |
| 471 |
"%%%%BeginResource: CMap (TeX-%s-0)\n" |
| 472 |
"%%%%Title: (TeX-%s-0 TeX %s 0)\n" |
| 473 |
"%%%%Version: 1.000\n" |
| 474 |
"%%%%EndComments\n" |
| 475 |
"/CIDInit /ProcSet findresource begin\n" |
| 476 |
"12 dict begin\n" |
| 477 |
"begincmap\n" |
| 478 |
"/CIDSystemInfo\n" |
| 479 |
"<< /Registry (TeX)\n" |
| 480 |
"/Ordering (%s)\n" |
| 481 |
"/Supplement 0\n" |
| 482 |
">> def\n" |
| 483 |
"/CMapName /TeX-%s-0 def\n" |
| 484 |
"/CMapType 2 def\n" |
| 485 |
"1 begincodespacerange\n" |
| 486 |
"<00> <FF>\n" "endcodespacerange\n", |
| 487 |
buf, buf, buf, buf, buf); |
| 488 |
/*tex Set |range_size|: */ |
| 489 |
for (i = 0; i < 256;) { |
| 490 |
if (gtab[i].code == UNI_STRING || gtab[i].code == UNI_EXTRA_STRING) { |
| 491 |
/*tex Single entry: */ |
| 492 |
range_size[i] = 1; |
| 493 |
i++; |
| 494 |
} else if (gtab[i].code == UNI_UNDEF) { |
| 495 |
/*tex No entry: */ |
| 496 |
range_size[i] = 0; |
| 497 |
i++; |
| 498 |
} else { |
| 499 |
/*tex |gtab[i].code >= 0| */ |
| 500 |
j = i; |
| 501 |
while (i < 256 && gtab[i + 1].code >= 0 && gtab[i].code + 1 == gtab[i + 1].code && is_last_byte_valid(j, i, gtab[i].code) |
| 502 |
) |
| 503 |
i++; |
| 504 |
/*tex |
| 505 |
At this point |i| is the last entry of the subrange so we move |i| to |
| 506 |
the next entry. |
| 507 |
*/ |
| 508 |
i++; |
| 509 |
range_size[j] = (short) (i - j); |
| 510 |
} |
| 511 |
} |
| 512 |
/*tex Calculate |bfrange_count| and |bfchar_count|. */ |
| 513 |
bfrange_count = 0; |
| 514 |
bfchar_count = 0; |
| 515 |
for (i = 0; i < 256;) { |
| 516 |
if (range_size[i] == 1) { |
| 517 |
bfchar_count++; |
| 518 |
i++; |
| 519 |
} else if (range_size[i] > 1) { |
| 520 |
bfrange_count++; |
| 521 |
i += range_size[i]; |
| 522 |
} else |
| 523 |
i++; |
| 524 |
} |
| 525 |
/*tex Write out |bfrange|. */ |
| 526 |
i = 0; |
| 527 |
write_bfrange: |
| 528 |
if (bfrange_count > 100) |
| 529 |
subrange_count = 100; |
| 530 |
else |
| 531 |
subrange_count = bfrange_count; |
| 532 |
bfrange_count -= subrange_count; |
| 533 |
if (subrange_count>0) { |
| 534 |
pdf_printf(pdf, "%i beginbfrange\n", subrange_count); |
| 535 |
for (j = 0; j < subrange_count; j++) { |
| 536 |
while (range_size[i] <= 1 && i < 256) |
| 537 |
i++; |
| 538 |
assert(i < 256); |
| 539 |
pdf_printf(pdf, "<%02X> <%02X> <%s>\n", i, i + range_size[i] - 1, utf16be_str(gtab[i].code)); |
| 540 |
i += range_size[i]; |
| 541 |
} |
| 542 |
pdf_printf(pdf, "endbfrange\n"); |
| 543 |
} |
| 544 |
if (bfrange_count > 0) |
| 545 |
goto write_bfrange; |
| 546 |
/*tex Write out |bfchar|. */ |
| 547 |
i = 0; |
| 548 |
write_bfchar: |
| 549 |
if (bfchar_count > 100) |
| 550 |
subrange_count = 100; |
| 551 |
else |
| 552 |
subrange_count = bfchar_count; |
| 553 |
bfchar_count -= subrange_count; |
| 554 |
pdf_printf(pdf, "%i beginbfchar\n", subrange_count); |
| 555 |
for (j = 0; j < subrange_count; j++) { |
| 556 |
while (i < 256) { |
| 557 |
if (range_size[i] > 1) |
| 558 |
i += range_size[i]; |
| 559 |
else if (range_size[i] == 0) |
| 560 |
i++; |
| 561 |
else |
| 562 |
break; |
| 563 |
} |
| 564 |
assert(i < 256 && gtab[i].code != UNI_UNDEF); |
| 565 |
if (gtab[i].code == UNI_STRING || gtab[i].code == UNI_EXTRA_STRING) { |
| 566 |
assert(gtab[i].unicode_seq != NULL); |
| 567 |
pdf_printf(pdf, "<%02X> <%s>\n", i, gtab[i].unicode_seq); |
| 568 |
} else |
| 569 |
pdf_printf(pdf, "<%02X> <%s>\n", i, utf16be_str(gtab[i].code)); |
| 570 |
i++; |
| 571 |
} |
| 572 |
pdf_printf(pdf, "endbfchar\n"); |
| 573 |
if (bfchar_count > 0) |
| 574 |
goto write_bfchar; |
| 575 |
/*tex Free strings allocated by |set_glyph_unicode|. */ |
| 576 |
for (i = 0; i < 256; ++i) { |
| 577 |
if (gtab[i].code == UNI_EXTRA_STRING) |
| 578 |
xfree(gtab[i].unicode_seq); |
| 579 |
} |
| 580 |
pdf_printf(pdf, |
| 581 |
"endcmap\n" |
| 582 |
"CMapName currentdict /CMap defineresource pop\n" |
| 583 |
"end\n" |
| 584 |
"end\n" |
| 585 |
"%%%%EndResource\n" |
| 586 |
"%%%%EOF\n"); |
| 587 |
pdf_end_stream(pdf); |
| 588 |
pdf_end_obj(pdf); |
| 589 |
return objnum; |
| 590 |
} |
| 591 |
|
| 592 |
int write_tounicode(PDF pdf, char **glyph_names, char *name, internal_font_number f) |
| 593 |
{ |
| 594 |
return do_write_tounicode(pdf, glyph_names, name, f, 0); |
| 595 |
} |
| 596 |
|
| 597 |
int write_raw_tounicode(PDF pdf, internal_font_number f, char *name) |
| 598 |
{ |
| 599 |
return do_write_tounicode(pdf, NULL, name, f, 1); |
| 600 |
} |
| 601 |
|
| 602 |
int write_cid_tounicode(PDF pdf, fo_entry * fo, internal_font_number f) |
| 603 |
{ |
| 604 |
static int range_size[65537]; |
| 605 |
static glyph_unicode_entry gtab[65537]; |
| 606 |
int objnum; |
| 607 |
int i, j, k, tu; |
| 608 |
int bfchar_count, bfrange_count, subrange_count; |
| 609 |
char *buf; |
| 610 |
char *s; |
| 611 |
buf = xmalloc((unsigned) (strlen(fo->fd->fontname) + 8)); |
| 612 |
sprintf(buf, "%s-%s", (fo->fd->subset_tag != NULL ? fo->fd->subset_tag : "UCS"), fo->fd->fontname); |
| 613 |
objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 614 |
pdf_begin_obj(pdf, objnum, OBJSTM_NEVER); |
| 615 |
pdf_begin_dict(pdf); |
| 616 |
pdf_dict_add_streaminfo(pdf); |
| 617 |
pdf_end_dict(pdf); |
| 618 |
pdf_begin_stream(pdf); |
| 619 |
pdf_printf(pdf, |
| 620 |
"%%!PS-Adobe-3.0 Resource-CMap\n" |
| 621 |
"%%%%DocumentNeededResources: ProcSet (CIDInit)\n" |
| 622 |
"%%%%IncludeResource: ProcSet (CIDInit)\n" |
| 623 |
"%%%%BeginResource: CMap (TeX-%s-0)\n" |
| 624 |
"%%%%Title: (TeX-%s-0 TeX %s 0)\n" |
| 625 |
"%%%%Version: 1.000\n" |
| 626 |
"%%%%EndComments\n" |
| 627 |
"/CIDInit /ProcSet findresource begin\n" |
| 628 |
"12 dict begin\n" |
| 629 |
"begincmap\n" |
| 630 |
"/CIDSystemInfo\n" |
| 631 |
"<< /Registry (TeX)\n" |
| 632 |
"/Ordering (%s)\n" |
| 633 |
"/Supplement 0\n" |
| 634 |
">> def\n" |
| 635 |
"/CMapName /TeX-Identity-%s def\n" |
| 636 |
"/CMapType 2 def\n" |
| 637 |
"1 begincodespacerange\n" |
| 638 |
"<0000> <FFFF>\n" |
| 639 |
"endcodespacerange\n", |
| 640 |
buf, buf, buf, buf, buf); |
| 641 |
xfree(buf); |
| 642 |
/*tex Set up |gtab|: */ |
| 643 |
for (i = 0; i < 65537; ++i) { |
| 644 |
gtab[i].code = UNI_UNDEF; |
| 645 |
} |
| 646 |
for (k = 1; k <= max_font_id(); k++) { |
| 647 |
if (k == f || -f == pdf_font_num(k)) { |
| 648 |
for (i = font_bc(k); i <= font_ec(k); i++) { |
| 649 |
if (quick_char_exists(k, i) && char_used(k, i)) { |
| 650 |
j = char_index(k, i); |
| 651 |
if (gtab[j].code == UNI_UNDEF) { |
| 652 |
/* |
| 653 |
set_cid_glyph_unicode(i, >ab[j], f); |
| 654 |
*/ |
| 655 |
tu = 0; |
| 656 |
/*tex |
| 657 |
First look in the instance (new). Ff this fails |
| 658 |
us (context) it will go away or become an option. |
| 659 |
*/ |
| 660 |
if (font_tounicode(k)) { |
| 661 |
tu = 1; /*tex no fallback to index */ |
| 662 |
if ((s = get_charinfo_tounicode(char_info(k, (int) i))) != NULL) { |
| 663 |
gtab[j].code = UNI_EXTRA_STRING; |
| 664 |
gtab[j].unicode_seq = xstrdup(s); |
| 665 |
} |
| 666 |
} |
| 667 |
/*tex |
| 668 |
Then look in the parent (was default, so one can |
| 669 |
still be sparse). |
| 670 |
*/ |
| 671 |
if (k != f && gtab[j].code == UNI_UNDEF && font_tounicode(f)) { |
| 672 |
tu = 1; /*tex no fallback to index */ |
| 673 |
if ((s = get_charinfo_tounicode(char_info(f, (int) i))) != NULL) { |
| 674 |
gtab[j].code = UNI_EXTRA_STRING; |
| 675 |
gtab[j].unicode_seq = xstrdup(s); |
| 676 |
} |
| 677 |
} |
| 678 |
if (! tu) { |
| 679 |
/*tex No tonunicode so we map onto index. */ |
| 680 |
gtab[j].code = i; |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
/*tex Set |range_size|: */ |
| 688 |
for (i = 0; i < 65536;) { |
| 689 |
if (gtab[i].code == UNI_STRING || gtab[i].code == UNI_EXTRA_STRING) { |
| 690 |
/*tex Single entry. */ |
| 691 |
range_size[i] = 1; |
| 692 |
i++; |
| 693 |
} else if (gtab[i].code == UNI_UNDEF) { |
| 694 |
/*tex No entry. */ |
| 695 |
range_size[i] = 0; |
| 696 |
i++; |
| 697 |
} else { |
| 698 |
/*tex |gtab[i].code >= 0| */ |
| 699 |
j = i; |
| 700 |
k = i % 256; |
| 701 |
while (i < 65536 && k<255 && gtab[i + 1].code >= 0 && |
| 702 |
gtab[i].code + 1 == gtab[i + 1].code) { |
| 703 |
i++; k++; |
| 704 |
} |
| 705 |
/* tex |
| 706 |
At this point |i| is the last entry of the subrange so we move |
| 707 |
|i| to the next entry |
| 708 |
*/ |
| 709 |
i++; |
| 710 |
range_size[j] = i - j; |
| 711 |
} |
| 712 |
} |
| 713 |
/*tex Calculate |bfrange_count| and |bfchar_count|. */ |
| 714 |
bfrange_count = 0; |
| 715 |
bfchar_count = 0; |
| 716 |
for (i = 0; i < 65536;) { |
| 717 |
if (range_size[i] == 1) { |
| 718 |
bfchar_count++; |
| 719 |
i++; |
| 720 |
} else if (range_size[i] > 1) { |
| 721 |
bfrange_count++; |
| 722 |
i += range_size[i]; |
| 723 |
} else |
| 724 |
i++; |
| 725 |
} |
| 726 |
/*tex Write out |bfrange|. */ |
| 727 |
i = 0; |
| 728 |
write_bfrange: |
| 729 |
if (bfrange_count > 100) |
| 730 |
subrange_count = 100; |
| 731 |
else |
| 732 |
subrange_count = bfrange_count; |
| 733 |
bfrange_count -= subrange_count; |
| 734 |
if (subrange_count>0) { |
| 735 |
pdf_printf(pdf, "%i beginbfrange\n", subrange_count); |
| 736 |
for (j = 0; j < subrange_count; j++) { |
| 737 |
while (range_size[i] <= 1 && i < 65536) |
| 738 |
i++; |
| 739 |
pdf_printf(pdf, "<%04X> <%04X> <%s>\n", i, i + range_size[i] - 1, utf16be_str(gtab[i].code)); |
| 740 |
i += range_size[i]; |
| 741 |
} |
| 742 |
pdf_printf(pdf, "endbfrange\n"); |
| 743 |
} |
| 744 |
if (bfrange_count > 0) |
| 745 |
goto write_bfrange; |
| 746 |
/*tex Write out |bfchar| */ |
| 747 |
i = 0; |
| 748 |
write_bfchar: |
| 749 |
if (bfchar_count > 100) |
| 750 |
subrange_count = 100; |
| 751 |
else |
| 752 |
subrange_count = bfchar_count; |
| 753 |
bfchar_count -= subrange_count; |
| 754 |
pdf_printf(pdf, "%i beginbfchar\n", subrange_count); |
| 755 |
for (j = 0; j < subrange_count; j++) { |
| 756 |
while (i < 65536) { |
| 757 |
if (range_size[i] > 1) |
| 758 |
i += range_size[i]; |
| 759 |
else if (range_size[i] == 0) |
| 760 |
i++; |
| 761 |
else |
| 762 |
/* |range_size[i] == 1| */ |
| 763 |
break; |
| 764 |
} |
| 765 |
assert(i < 65536 && gtab[i].code != UNI_UNDEF); |
| 766 |
if (gtab[i].code == UNI_STRING || gtab[i].code == UNI_EXTRA_STRING) { |
| 767 |
pdf_printf(pdf, "<%04X> <%s>\n", i, gtab[i].unicode_seq); |
| 768 |
} else |
| 769 |
pdf_printf(pdf, "<%04X> <%s>\n", i, utf16be_str(gtab[i].code)); |
| 770 |
i++; |
| 771 |
} |
| 772 |
pdf_printf(pdf, "endbfchar\n"); |
| 773 |
if (bfchar_count > 0) |
| 774 |
goto write_bfchar; |
| 775 |
/*tex Free strings allocated by |set_glyph_unicode|: */ |
| 776 |
for (i = 0; i < 65536; ++i) { |
| 777 |
if (gtab[i].code == UNI_EXTRA_STRING) |
| 778 |
xfree(gtab[i].unicode_seq); |
| 779 |
} |
| 780 |
pdf_printf(pdf, |
| 781 |
"endcmap\n" |
| 782 |
"CMapName currentdict /CMap defineresource pop\n" |
| 783 |
"end\n" |
| 784 |
"end\n" |
| 785 |
"%%%%EndResource\n" |
| 786 |
"%%%%EOF\n"); |
| 787 |
pdf_end_stream(pdf); |
| 788 |
pdf_end_obj(pdf); |
| 789 |
return objnum; |
| 790 |
} |