| 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 |
* A large part of codes are brought from ttfdump-0.5.5. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <config.h> |
| 29 |
|
| 30 |
#include "xdvips.h" /* The copyright notice in that file is included too! */ |
| 31 |
#include "protos.h" |
| 32 |
|
| 33 |
#include "system.h" |
| 34 |
#include "mem.h" |
| 35 |
#include "error.h" |
| 36 |
|
| 37 |
#include "sfnt.h" |
| 38 |
|
| 39 |
|
| 40 |
/* Sorry for placing this here. |
| 41 |
* We need to rewrite TrueType font support code... |
| 42 |
*/ |
| 43 |
#include "cmap.h" |
| 44 |
#include "cmap_write.h" |
| 45 |
|
| 46 |
#include "tt_aux.h" |
| 47 |
#include "tt_post.h" |
| 48 |
|
| 49 |
#include "unicode.h" |
| 50 |
|
| 51 |
#include "dpxfile.h" |
| 52 |
|
| 53 |
/* Hash */ |
| 54 |
#include "dpxutil.h" |
| 55 |
|
| 56 |
#include "tt_cmap.h" |
| 57 |
|
| 58 |
/* format 0: byte encoding table */ |
| 59 |
struct cmap0 |
| 60 |
{ |
| 61 |
BYTE glyphIndexArray[256]; |
| 62 |
}; |
| 63 |
|
| 64 |
static struct cmap0 * |
| 65 |
read_cmap0 (sfnt *sfont, ULONG len) |
| 66 |
{ |
| 67 |
struct cmap0 *map; |
| 68 |
int i; |
| 69 |
|
| 70 |
if (len < 256) |
| 71 |
ERROR("invalid cmap subtable"); |
| 72 |
|
| 73 |
map = NEW(1, struct cmap0); |
| 74 |
|
| 75 |
for (i = 0; i < 256; i++) |
| 76 |
map->glyphIndexArray[i] = sfnt_get_byte(sfont); |
| 77 |
|
| 78 |
return map; |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
release_cmap0(struct cmap0 *map) |
| 83 |
{ |
| 84 |
if (map) |
| 85 |
RELEASE(map); |
| 86 |
} |
| 87 |
|
| 88 |
static USHORT |
| 89 |
lookup_cmap0 (struct cmap0 *map, USHORT cc) |
| 90 |
{ |
| 91 |
return ((cc > 255) ? 0 : map->glyphIndexArray[cc]); |
| 92 |
} |
| 93 |
|
| 94 |
/* format 2: high-byte mapping through table */ |
| 95 |
struct SubHeader |
| 96 |
{ |
| 97 |
USHORT firstCode; |
| 98 |
USHORT entryCount; |
| 99 |
SHORT idDelta; |
| 100 |
USHORT idRangeOffset; |
| 101 |
}; |
| 102 |
|
| 103 |
struct cmap2 |
| 104 |
{ |
| 105 |
USHORT subHeaderKeys[256]; |
| 106 |
struct SubHeader *subHeaders; |
| 107 |
USHORT *glyphIndexArray; |
| 108 |
}; |
| 109 |
|
| 110 |
static struct cmap2 * |
| 111 |
read_cmap2 (sfnt *sfont, ULONG len) |
| 112 |
{ |
| 113 |
struct cmap2 *map; |
| 114 |
USHORT i, n; |
| 115 |
|
| 116 |
if (len < 512) |
| 117 |
ERROR("invalid cmap subtable"); |
| 118 |
|
| 119 |
map = NEW(1, struct cmap2); |
| 120 |
|
| 121 |
for (i = 0; i < 256; i++) |
| 122 |
map->subHeaderKeys[i] = sfnt_get_ushort(sfont); |
| 123 |
|
| 124 |
for (n = 0, i = 0; i < 256; i++) { |
| 125 |
map->subHeaderKeys[i] /= 8; |
| 126 |
if (n < map->subHeaderKeys[i]) |
| 127 |
n = map->subHeaderKeys[i]; |
| 128 |
} |
| 129 |
n += 1; /* the number of subHeaders is one plus the max of subHeaderKeys */ |
| 130 |
|
| 131 |
map->subHeaders = NEW(n, struct SubHeader); |
| 132 |
for (i = 0; i < n; i++) { |
| 133 |
map->subHeaders[i].firstCode = sfnt_get_ushort(sfont); |
| 134 |
map->subHeaders[i].entryCount = sfnt_get_ushort(sfont); |
| 135 |
map->subHeaders[i].idDelta = sfnt_get_short(sfont); |
| 136 |
map->subHeaders[i].idRangeOffset = sfnt_get_ushort(sfont); |
| 137 |
|
| 138 |
/* It makes things easier to let the offset starts from |
| 139 |
* the beginning of glyphIndexArray. |
| 140 |
*/ |
| 141 |
if (map->subHeaders[i].idRangeOffset != 0) |
| 142 |
map->subHeaders[i].idRangeOffset -= (2 + (n - i - 1) * 8); |
| 143 |
} |
| 144 |
|
| 145 |
/* Caculate the length of glyphIndexArray, this is ugly, |
| 146 |
* there should be a better way to get this information. |
| 147 |
*/ |
| 148 |
n = (USHORT) (len - 518 - n * 8) / 2; |
| 149 |
|
| 150 |
map->glyphIndexArray = NEW(n, USHORT); |
| 151 |
for (i = 0; i < n; i++) |
| 152 |
map->glyphIndexArray[i] = sfnt_get_ushort(sfont); |
| 153 |
|
| 154 |
return map; |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
release_cmap2 (struct cmap2 *map) |
| 159 |
{ |
| 160 |
if (map) { |
| 161 |
if (map->subHeaders) |
| 162 |
RELEASE(map->subHeaders); |
| 163 |
if (map->glyphIndexArray) |
| 164 |
RELEASE(map->glyphIndexArray); |
| 165 |
RELEASE(map); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static USHORT |
| 170 |
lookup_cmap2 (struct cmap2 *map, USHORT cc) |
| 171 |
{ |
| 172 |
USHORT idx = 0; |
| 173 |
SHORT idDelta; |
| 174 |
USHORT firstCode, entryCount, idRangeOffset; |
| 175 |
int hi, lo; |
| 176 |
USHORT i; |
| 177 |
|
| 178 |
hi = (cc >> 8) & 0xff; |
| 179 |
lo = cc & 0xff; |
| 180 |
|
| 181 |
/* select which subHeader to use */ |
| 182 |
i = map->subHeaderKeys[hi]; |
| 183 |
|
| 184 |
firstCode = map->subHeaders[i].firstCode; |
| 185 |
entryCount = map->subHeaders[i].entryCount; |
| 186 |
idDelta = map->subHeaders[i].idDelta; |
| 187 |
idRangeOffset = map->subHeaders[i].idRangeOffset / 2; |
| 188 |
|
| 189 |
if (lo >= firstCode && |
| 190 |
lo < firstCode + entryCount) { |
| 191 |
idRangeOffset += lo - firstCode; |
| 192 |
idx = map->glyphIndexArray[idRangeOffset]; |
| 193 |
if (idx != 0) |
| 194 |
idx = (idx + idDelta) & 0xffff; |
| 195 |
} |
| 196 |
|
| 197 |
return idx; |
| 198 |
} |
| 199 |
|
| 200 |
/* |
| 201 |
* format 4: segment mapping to delta values |
| 202 |
* - Microsoft standard character to glyph index mapping table |
| 203 |
*/ |
| 204 |
struct cmap4 |
| 205 |
{ |
| 206 |
USHORT segCountX2; |
| 207 |
USHORT searchRange; |
| 208 |
USHORT entrySelector; |
| 209 |
USHORT rangeShift; |
| 210 |
USHORT *endCount; |
| 211 |
USHORT reservedPad; |
| 212 |
USHORT *startCount; |
| 213 |
USHORT *idDelta; |
| 214 |
USHORT *idRangeOffset; |
| 215 |
USHORT *glyphIndexArray; |
| 216 |
}; |
| 217 |
|
| 218 |
static struct cmap4 * |
| 219 |
read_cmap4(sfnt *sfont, ULONG len) |
| 220 |
{ |
| 221 |
struct cmap4 *map; |
| 222 |
USHORT i, n, segCount; |
| 223 |
|
| 224 |
if (len < 8) |
| 225 |
ERROR("invalid cmap subtable"); |
| 226 |
|
| 227 |
map = NEW(1, struct cmap4); |
| 228 |
|
| 229 |
map->segCountX2 = segCount = sfnt_get_ushort(sfont); |
| 230 |
map->searchRange = sfnt_get_ushort(sfont); |
| 231 |
map->entrySelector = sfnt_get_ushort(sfont); |
| 232 |
map->rangeShift = sfnt_get_ushort(sfont); |
| 233 |
|
| 234 |
segCount /= 2; |
| 235 |
|
| 236 |
map->endCount = NEW(segCount, USHORT); |
| 237 |
for (i = 0; i < segCount; i++) |
| 238 |
map->endCount[i] = sfnt_get_ushort(sfont); |
| 239 |
|
| 240 |
map->reservedPad = sfnt_get_ushort(sfont); |
| 241 |
|
| 242 |
map->startCount = NEW(segCount, USHORT); |
| 243 |
for (i = 0; i < segCount; i++) |
| 244 |
map->startCount[i] = sfnt_get_ushort(sfont); |
| 245 |
|
| 246 |
map->idDelta = NEW(segCount, USHORT); |
| 247 |
for (i = 0; i < segCount; i++) |
| 248 |
map->idDelta[i] = sfnt_get_ushort(sfont); |
| 249 |
|
| 250 |
map->idRangeOffset = NEW(segCount, USHORT); |
| 251 |
for (i = 0; i < segCount; i++) |
| 252 |
map->idRangeOffset[i] = sfnt_get_ushort(sfont); |
| 253 |
|
| 254 |
n = (len - 16 - 8 * segCount) / 2; |
| 255 |
if (n == 0) |
| 256 |
map->glyphIndexArray = NULL; |
| 257 |
else { |
| 258 |
map->glyphIndexArray = NEW(n, USHORT); |
| 259 |
for (i = 0; i < n; i++) |
| 260 |
map->glyphIndexArray[i] = sfnt_get_ushort(sfont); |
| 261 |
} |
| 262 |
|
| 263 |
return map; |
| 264 |
} |
| 265 |
|
| 266 |
static void |
| 267 |
release_cmap4 (struct cmap4 *map) |
| 268 |
{ |
| 269 |
if (map) { |
| 270 |
if (map->endCount) RELEASE(map->endCount); |
| 271 |
if (map->startCount) RELEASE(map->startCount); |
| 272 |
if (map->idDelta) RELEASE(map->idDelta); |
| 273 |
if (map->idRangeOffset) RELEASE(map->idRangeOffset); |
| 274 |
if (map->glyphIndexArray) RELEASE(map->glyphIndexArray); |
| 275 |
RELEASE(map); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
static USHORT |
| 280 |
lookup_cmap4 (struct cmap4 *map, USHORT cc) |
| 281 |
{ |
| 282 |
USHORT gid = 0; |
| 283 |
USHORT i, j, segCount; |
| 284 |
|
| 285 |
/* |
| 286 |
* Segments are sorted in order of increasing endCode values. |
| 287 |
* Last segment maps 0xffff to gid 0 (?) |
| 288 |
*/ |
| 289 |
i = segCount = map->segCountX2 / 2; |
| 290 |
while (i-- > 0 && cc <= map->endCount[i]) { |
| 291 |
if (cc >= map->startCount[i]) { |
| 292 |
if (map->idRangeOffset[i] == 0) { |
| 293 |
gid = (cc + map->idDelta[i]) & 0xffff; |
| 294 |
} else if (cc == 0xffff && map->idRangeOffset[i] == 0xffff) { |
| 295 |
/* this is for protection against some old broken fonts... */ |
| 296 |
gid = 0; |
| 297 |
} else { |
| 298 |
j = map->idRangeOffset[i] - (segCount - i) * 2; |
| 299 |
j = (cc - map->startCount[i]) + (j / 2); |
| 300 |
gid = map->glyphIndexArray[j]; |
| 301 |
if (gid != 0) |
| 302 |
gid = (gid + map->idDelta[i]) & 0xffff; |
| 303 |
} |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return gid; |
| 309 |
} |
| 310 |
|
| 311 |
/* format 6: trimmed table mapping */ |
| 312 |
struct cmap6 |
| 313 |
{ |
| 314 |
USHORT firstCode; |
| 315 |
USHORT entryCount; |
| 316 |
USHORT *glyphIndexArray; |
| 317 |
}; |
| 318 |
|
| 319 |
static struct cmap6 * |
| 320 |
read_cmap6 (sfnt *sfont, ULONG len) |
| 321 |
{ |
| 322 |
struct cmap6 *map; |
| 323 |
USHORT i; |
| 324 |
|
| 325 |
if (len < 4) |
| 326 |
ERROR("invalid cmap subtable"); |
| 327 |
|
| 328 |
map = NEW(1, struct cmap6); |
| 329 |
map->firstCode = sfnt_get_ushort(sfont); |
| 330 |
map->entryCount = sfnt_get_ushort(sfont); |
| 331 |
map->glyphIndexArray = NEW(map->entryCount, USHORT); |
| 332 |
|
| 333 |
for (i = 0; i < map->entryCount; i++) |
| 334 |
map->glyphIndexArray[i] = sfnt_get_ushort(sfont); |
| 335 |
|
| 336 |
return map; |
| 337 |
} |
| 338 |
|
| 339 |
static void |
| 340 |
release_cmap6 (struct cmap6 *map) |
| 341 |
{ |
| 342 |
if (map) { |
| 343 |
if (map->glyphIndexArray) |
| 344 |
RELEASE(map->glyphIndexArray); |
| 345 |
RELEASE(map); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
static USHORT |
| 350 |
lookup_cmap6 (struct cmap6 *map, USHORT cc) |
| 351 |
{ |
| 352 |
USHORT idx; |
| 353 |
|
| 354 |
idx = cc - map->firstCode; |
| 355 |
if (idx < map->entryCount) |
| 356 |
return map->glyphIndexArray[idx]; |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
/* Format 8 and 10 not supported... |
| 361 |
* |
| 362 |
* format 8: mixed 16-bit and 32-bit coverage |
| 363 |
* format 10: trimmed array |
| 364 |
*/ |
| 365 |
|
| 366 |
/* |
| 367 |
* format 12: segmented coverage |
| 368 |
* |
| 369 |
* startGlyphID is 32-bit long, however, GlyphID is still 16-bit long ! |
| 370 |
*/ |
| 371 |
|
| 372 |
struct charGroup |
| 373 |
{ |
| 374 |
ULONG startCharCode; |
| 375 |
ULONG endCharCode; |
| 376 |
ULONG startGlyphID; |
| 377 |
}; |
| 378 |
|
| 379 |
struct cmap12 |
| 380 |
{ |
| 381 |
ULONG nGroups; |
| 382 |
struct charGroup *groups; |
| 383 |
}; |
| 384 |
|
| 385 |
/* ULONG length */ |
| 386 |
static struct cmap12 * |
| 387 |
read_cmap12 (sfnt *sfont, ULONG len) |
| 388 |
{ |
| 389 |
struct cmap12 *map; |
| 390 |
ULONG i; |
| 391 |
|
| 392 |
if (len < 4) |
| 393 |
ERROR("invalid cmap subtable"); |
| 394 |
|
| 395 |
map = NEW(1, struct cmap12); |
| 396 |
map->nGroups = sfnt_get_ulong(sfont); |
| 397 |
map->groups = NEW(map->nGroups, struct charGroup); |
| 398 |
|
| 399 |
for (i = 0; i < map->nGroups; i++) { |
| 400 |
map->groups[i].startCharCode = sfnt_get_ulong(sfont); |
| 401 |
map->groups[i].endCharCode = sfnt_get_ulong(sfont); |
| 402 |
map->groups[i].startGlyphID = sfnt_get_ulong(sfont); |
| 403 |
} |
| 404 |
|
| 405 |
return map; |
| 406 |
} |
| 407 |
|
| 408 |
static void |
| 409 |
release_cmap12 (struct cmap12 *map) |
| 410 |
{ |
| 411 |
if (map) { |
| 412 |
if (map->groups) |
| 413 |
RELEASE(map->groups); |
| 414 |
RELEASE(map); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
static USHORT |
| 419 |
lookup_cmap12 (struct cmap12 *map, ULONG cccc) |
| 420 |
{ |
| 421 |
USHORT gid = 0; |
| 422 |
int i; |
| 423 |
|
| 424 |
i = map->nGroups; |
| 425 |
while (i-- >= 0 && |
| 426 |
cccc <= map->groups[i].endCharCode) { |
| 427 |
if ( i < 0 ) break; |
| 428 |
if (cccc >= map->groups[i].startCharCode) { |
| 429 |
gid = (USHORT) ((cccc - |
| 430 |
map->groups[i].startCharCode + |
| 431 |
map->groups[i].startGlyphID) & 0xffff); |
| 432 |
break; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
return gid; |
| 437 |
} |
| 438 |
|
| 439 |
/* read cmap */ |
| 440 |
tt_cmap * |
| 441 |
tt_cmap_read (sfnt *sfont, USHORT platform, USHORT encoding) |
| 442 |
{ |
| 443 |
tt_cmap *cmap = NULL; |
| 444 |
ULONG offset, length = 0; |
| 445 |
USHORT p_id, e_id; |
| 446 |
USHORT i, n_subtabs; |
| 447 |
|
| 448 |
ASSERT(sfont); |
| 449 |
|
| 450 |
offset = sfnt_locate_table(sfont, "cmap"); |
| 451 |
(void) sfnt_get_ushort(sfont); |
| 452 |
n_subtabs = sfnt_get_ushort(sfont); |
| 453 |
|
| 454 |
for (i = 0; i < n_subtabs; i++) { |
| 455 |
p_id = sfnt_get_ushort(sfont); |
| 456 |
e_id = sfnt_get_ushort(sfont); |
| 457 |
if (p_id != platform || e_id != encoding) |
| 458 |
sfnt_get_ulong(sfont); |
| 459 |
else { |
| 460 |
offset += sfnt_get_ulong(sfont); |
| 461 |
break; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
if (i == n_subtabs) |
| 466 |
return NULL; |
| 467 |
|
| 468 |
cmap = NEW(1, tt_cmap); |
| 469 |
cmap->map = NULL; |
| 470 |
cmap->platform = platform; |
| 471 |
cmap->encoding = encoding; |
| 472 |
|
| 473 |
sfnt_seek_set(sfont, offset); |
| 474 |
cmap->format = sfnt_get_ushort(sfont); |
| 475 |
/* Length and version (language) is ULONG for |
| 476 |
* format 8, 10, 12 ! |
| 477 |
*/ |
| 478 |
if (cmap->format <= 6) { |
| 479 |
length = sfnt_get_ushort(sfont); |
| 480 |
cmap->language = sfnt_get_ushort(sfont); /* language (Mac) */ |
| 481 |
} else { |
| 482 |
if (sfnt_get_ushort(sfont) != 0) { /* reverved - 0 */ |
| 483 |
WARN("Unrecognized cmap subtable format."); |
| 484 |
tt_cmap_release(cmap); |
| 485 |
return NULL; |
| 486 |
} else { |
| 487 |
length = sfnt_get_ulong(sfont); |
| 488 |
cmap->language = sfnt_get_ulong(sfont); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
switch(cmap->format) { |
| 493 |
case 0: |
| 494 |
cmap->map = read_cmap0(sfont, length); |
| 495 |
break; |
| 496 |
case 2: |
| 497 |
cmap->map = read_cmap2(sfont, length); |
| 498 |
break; |
| 499 |
case 4: |
| 500 |
cmap->map = read_cmap4(sfont, length); |
| 501 |
break; |
| 502 |
case 6: |
| 503 |
cmap->map = read_cmap6(sfont, length); |
| 504 |
break; |
| 505 |
case 12: |
| 506 |
/* WARN("UCS-4 TrueType cmap table..."); */ |
| 507 |
cmap->map = read_cmap12(sfont, length); |
| 508 |
break; |
| 509 |
default: |
| 510 |
WARN("Unrecognized OpenType/TrueType cmap format."); |
| 511 |
tt_cmap_release(cmap); |
| 512 |
return NULL; |
| 513 |
} |
| 514 |
|
| 515 |
if (!cmap->map) { |
| 516 |
tt_cmap_release(cmap); |
| 517 |
cmap = NULL; |
| 518 |
} |
| 519 |
|
| 520 |
return cmap; |
| 521 |
} |
| 522 |
|
| 523 |
void |
| 524 |
tt_cmap_release (tt_cmap *cmap) |
| 525 |
{ |
| 526 |
|
| 527 |
if (cmap) { |
| 528 |
if (cmap->map) { |
| 529 |
switch(cmap->format) { |
| 530 |
case 0: |
| 531 |
release_cmap0(cmap->map); |
| 532 |
break; |
| 533 |
case 2: |
| 534 |
release_cmap2(cmap->map); |
| 535 |
break; |
| 536 |
case 4: |
| 537 |
release_cmap4(cmap->map); |
| 538 |
break; |
| 539 |
case 6: |
| 540 |
release_cmap6(cmap->map); |
| 541 |
break; |
| 542 |
case 12: |
| 543 |
release_cmap12(cmap->map); |
| 544 |
break; |
| 545 |
default: |
| 546 |
ERROR("Unrecognized OpenType/TrueType cmap format."); |
| 547 |
} |
| 548 |
} |
| 549 |
RELEASE(cmap); |
| 550 |
} |
| 551 |
|
| 552 |
return; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
USHORT |
| 557 |
tt_cmap_lookup (tt_cmap *cmap, long cc) |
| 558 |
{ |
| 559 |
USHORT gid = 0; |
| 560 |
|
| 561 |
ASSERT(cmap); |
| 562 |
|
| 563 |
if (cc > 0xffffL && cmap->format < 12) { |
| 564 |
WARN("Four bytes charcode not supported in OpenType/TrueType cmap format 0...6."); |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
switch (cmap->format) { |
| 569 |
case 0: |
| 570 |
gid = lookup_cmap0(cmap->map, (USHORT) cc); |
| 571 |
break; |
| 572 |
case 2: |
| 573 |
gid = lookup_cmap2(cmap->map, (USHORT) cc); |
| 574 |
break; |
| 575 |
case 4: |
| 576 |
gid = lookup_cmap4(cmap->map, (USHORT) cc); |
| 577 |
break; |
| 578 |
case 6: |
| 579 |
gid = lookup_cmap6(cmap->map, (USHORT) cc); |
| 580 |
break; |
| 581 |
case 12: |
| 582 |
gid = lookup_cmap12(cmap->map, (ULONG) cc); |
| 583 |
break; |
| 584 |
default: |
| 585 |
ERROR("Unrecognized OpenType/TrueType cmap subtable format"); |
| 586 |
break; |
| 587 |
} |
| 588 |
|
| 589 |
return gid; |
| 590 |
} |
| 591 |
|
| 592 |
/* Sorry for placing this here. |
| 593 |
* We need to rewrite TrueType font support code... |
| 594 |
*/ |
| 595 |
|
| 596 |
#define WBUF_SIZE 1024 |
| 597 |
static unsigned char wbuf[WBUF_SIZE]; |
| 598 |
|
| 599 |
static unsigned char srange_min[2] = {0x00, 0x00}; |
| 600 |
static unsigned char srange_max[2] = {0xff, 0xff}; |
| 601 |
static unsigned char lrange_min[4] = {0x00, 0x00, 0x00, 0x00}; |
| 602 |
static unsigned char lrange_max[4] = {0x7f, 0xff, 0xff, 0xff}; |
| 603 |
|
| 604 |
static void |
| 605 |
load_cmap4 (struct cmap4 *map, |
| 606 |
unsigned char *GIDToCIDMap, CMap *cmap) |
| 607 |
{ |
| 608 |
USHORT c0, c1, gid, cid; |
| 609 |
USHORT j, d, segCount; |
| 610 |
USHORT ch; |
| 611 |
long i; |
| 612 |
|
| 613 |
segCount = map->segCountX2 / 2; |
| 614 |
for (i = segCount - 1; i >= 0 ; i--) { |
| 615 |
c0 = map->startCount[i]; |
| 616 |
c1 = map->endCount[i]; |
| 617 |
d = map->idRangeOffset[i] / 2 - (segCount - i); |
| 618 |
for (j = 0; j <= c1 - c0; j++) { |
| 619 |
ch = c0 + j; |
| 620 |
if (map->idRangeOffset[i] == 0) { |
| 621 |
gid = (ch + map->idDelta[i]) & 0xffff; |
| 622 |
} else if (c0 == 0xffff && c1 == 0xffff && map->idRangeOffset[i] == 0xffff) { |
| 623 |
/* this is for protection against some old broken fonts... */ |
| 624 |
gid = 0; |
| 625 |
} else { |
| 626 |
gid = (map->glyphIndexArray[j+d] + |
| 627 |
map->idDelta[i]) & 0xffff; |
| 628 |
} |
| 629 |
if (gid != 0 && gid != 0xffff) { |
| 630 |
if (GIDToCIDMap) { |
| 631 |
cid = ((GIDToCIDMap[2*gid] << 8)|GIDToCIDMap[2*gid+1]); |
| 632 |
if (cid == 0) |
| 633 |
WARN("GID %u does not have corresponding CID %u.", |
| 634 |
gid, cid); |
| 635 |
} else { |
| 636 |
cid = gid; |
| 637 |
} |
| 638 |
wbuf[0] = 0; |
| 639 |
wbuf[1] = 0; |
| 640 |
wbuf[2] = (ch >> 8) & 0xff; |
| 641 |
wbuf[3] = ch & 0xff; |
| 642 |
CMap_add_cidchar(cmap, wbuf, 4, cid); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
static void |
| 651 |
load_cmap12 (struct cmap12 *map, |
| 652 |
unsigned char *GIDToCIDMap, CMap *cmap) |
| 653 |
{ |
| 654 |
ULONG i, ch; /* LONG ? */ |
| 655 |
USHORT gid, cid; |
| 656 |
|
| 657 |
for (i = 0; i < map->nGroups; i++) { |
| 658 |
for (ch = map->groups[i].startCharCode; |
| 659 |
ch <= map->groups[i].endCharCode; |
| 660 |
ch++) { |
| 661 |
long d = ch - map->groups[i].startCharCode; |
| 662 |
gid = (USHORT) ((map->groups[i].startGlyphID + d) & 0xffff); |
| 663 |
if (GIDToCIDMap) { |
| 664 |
cid = ((GIDToCIDMap[2*gid] << 8)|GIDToCIDMap[2*gid+1]); |
| 665 |
if (cid == 0) |
| 666 |
WARN("GID %u does not have corresponding CID %u.", gid, cid); |
| 667 |
} else { |
| 668 |
cid = gid; |
| 669 |
} |
| 670 |
wbuf[0] = (ch >> 24) & 0xff; |
| 671 |
wbuf[1] = (ch >> 16) & 0xff; |
| 672 |
wbuf[2] = (ch >> 8) & 0xff; |
| 673 |
wbuf[3] = ch & 0xff; |
| 674 |
CMap_add_cidchar(cmap, wbuf, 4, cid); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
/* OpenType CIDFont: |
| 682 |
* |
| 683 |
* We don't use GID for them. OpenType cmap table is for |
| 684 |
* charcode to GID mapping rather than to-CID mapping. |
| 685 |
*/ |
| 686 |
#include "cidsysinfo.h" |
| 687 |
|
| 688 |
#include "tt_table.h" |
| 689 |
#include "cff_types.h" |
| 690 |
#include "cff_dict.h" |
| 691 |
#include "cff.h" |
| 692 |
|
| 693 |
static int |
| 694 |
handle_CIDFont (sfnt *sfont, |
| 695 |
unsigned char **GIDToCIDMap, CIDSysInfo *csi) |
| 696 |
{ |
| 697 |
cff_font *cffont; |
| 698 |
long offset, i; |
| 699 |
card16 num_glyphs, gid; |
| 700 |
cff_charsets *charset; |
| 701 |
unsigned char *map; |
| 702 |
struct tt_maxp_table *maxp; |
| 703 |
|
| 704 |
ASSERT(csi); |
| 705 |
|
| 706 |
offset = sfnt_find_table_pos(sfont, "CFF "); |
| 707 |
if (offset == 0) { |
| 708 |
csi->registry = NULL; |
| 709 |
csi->ordering = NULL; |
| 710 |
*GIDToCIDMap = NULL; |
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
maxp = tt_read_maxp_table(sfont); |
| 715 |
num_glyphs = (card16) maxp->numGlyphs; |
| 716 |
RELEASE(maxp); |
| 717 |
if (num_glyphs < 1) |
| 718 |
ERROR("No glyph contained in this font..."); |
| 719 |
|
| 720 |
cffont = cff_open(sfont->stream, offset, 0); |
| 721 |
if (!cffont) |
| 722 |
ERROR("Could not open CFF font..."); |
| 723 |
|
| 724 |
|
| 725 |
if (!(cffont->flag & FONTTYPE_CIDFONT)) { |
| 726 |
cff_close(cffont); |
| 727 |
csi->registry = NULL; |
| 728 |
csi->ordering = NULL; |
| 729 |
*GIDToCIDMap = NULL; |
| 730 |
return 0; |
| 731 |
} |
| 732 |
|
| 733 |
if (!cff_dict_known(cffont->topdict, "ROS")) { |
| 734 |
ERROR("No CIDSystemInfo???"); |
| 735 |
} else { |
| 736 |
card16 reg, ord; |
| 737 |
|
| 738 |
reg = (card16) cff_dict_get(cffont->topdict, "ROS", 0); |
| 739 |
ord = (card16) cff_dict_get(cffont->topdict, "ROS", 1); |
| 740 |
|
| 741 |
csi->registry = cff_get_string(cffont, reg); |
| 742 |
csi->ordering = cff_get_string(cffont, ord); |
| 743 |
csi->supplement = (int) cff_dict_get(cffont->topdict, "ROS", 2); |
| 744 |
} |
| 745 |
|
| 746 |
cff_read_charsets(cffont); |
| 747 |
charset = cffont->charsets; |
| 748 |
if (!charset) { |
| 749 |
ERROR("No CFF charset data???"); |
| 750 |
} |
| 751 |
|
| 752 |
map = NEW(num_glyphs * 2, unsigned char); |
| 753 |
memset(map, 0, num_glyphs * 2); |
| 754 |
switch (charset->format) { |
| 755 |
case 0: |
| 756 |
{ |
| 757 |
s_SID *cids; /* CID... */ |
| 758 |
|
| 759 |
cids = charset->data.glyphs; |
| 760 |
for (gid = 1, i = 0; |
| 761 |
i < charset->num_entries; i++) { |
| 762 |
map[2*gid ] = (cids[i] >> 8) & 0xff; |
| 763 |
map[2*gid+1] = cids[i] & 0xff; |
| 764 |
gid++; |
| 765 |
} |
| 766 |
} |
| 767 |
break; |
| 768 |
case 1: |
| 769 |
{ |
| 770 |
cff_range1 *ranges; |
| 771 |
card16 cid, count; |
| 772 |
|
| 773 |
ranges = charset->data.range1; |
| 774 |
for (gid = 1, i = 0; |
| 775 |
i < charset->num_entries; i++) { |
| 776 |
cid = ranges[i].first; |
| 777 |
count = ranges[i].n_left + 1; /* card8 */ |
| 778 |
while (count-- > 0 && |
| 779 |
gid <= num_glyphs) { |
| 780 |
map[2*gid ] = (cid >> 8) & 0xff; |
| 781 |
map[2*gid + 1] = cid & 0xff; |
| 782 |
gid++; cid++; |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
break; |
| 787 |
case 2: |
| 788 |
{ |
| 789 |
cff_range2 *ranges; |
| 790 |
card16 cid, count; |
| 791 |
|
| 792 |
ranges = charset->data.range2; |
| 793 |
if (charset->num_entries == 1 && |
| 794 |
ranges[0].first == 1) { |
| 795 |
/* "Complete" CIDFont */ |
| 796 |
RELEASE(map); map = NULL; |
| 797 |
} else { |
| 798 |
/* Not trivial mapping */ |
| 799 |
for (gid = 1, i = 0; |
| 800 |
i < charset->num_entries; i++) { |
| 801 |
cid = ranges[i].first; |
| 802 |
count = ranges[i].n_left + 1; |
| 803 |
while (count-- > 0 && |
| 804 |
gid <= num_glyphs) { |
| 805 |
map[gid] = (cid >> 8) & 0xff; |
| 806 |
map[gid] = cid & 0xff; |
| 807 |
gid++; cid++; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
} |
| 812 |
} |
| 813 |
break; |
| 814 |
default: |
| 815 |
RELEASE(map); map = NULL; |
| 816 |
ERROR("Unknown CFF charset format...: %d", charset->format); |
| 817 |
break; |
| 818 |
} |
| 819 |
cff_close(cffont); |
| 820 |
|
| 821 |
*GIDToCIDMap = map; |
| 822 |
return 1; |
| 823 |
} |
| 824 |
|
| 825 |
static int is_PUA_or_presentation (unsigned int uni) |
| 826 |
{ |
| 827 |
return ((uni >= 0xE000 && uni <= 0xF8FF) || (uni >= 0xFB00 && uni <= 0xFB4F) || |
| 828 |
(uni >= 0xF0000 && uni <= 0xFFFFD) || (uni >= 0x100000 && uni <= 0x10FFFD)); |
| 829 |
} |
| 830 |
|
| 831 |
static char* |
| 832 |
sfnt_get_glyphname(struct tt_post_table *post, cff_font *cffont, USHORT gid) |
| 833 |
{ |
| 834 |
char* name = NULL; |
| 835 |
|
| 836 |
if (post) |
| 837 |
name = tt_get_glyphname(post, gid); |
| 838 |
|
| 839 |
if (!name && cffont) |
| 840 |
name = cff_get_glyphname(cffont, gid); |
| 841 |
|
| 842 |
return name; |
| 843 |
} |
| 844 |
|
| 845 |
/* |
| 846 |
* Substituted glyphs: |
| 847 |
* |
| 848 |
* Mapping information stored in cmap_add. |
| 849 |
*/ |
| 850 |
|
| 851 |
static cff_font * |
| 852 |
prepare_CIDFont_from_sfnt(sfnt* sfont) |
| 853 |
{ |
| 854 |
cff_font *cffont; |
| 855 |
unsigned long offset = 0; |
| 856 |
|
| 857 |
if (sfont->type != SFNT_TYPE_POSTSCRIPT || |
| 858 |
sfnt_read_table_directory(sfont, 0) < 0 || |
| 859 |
(offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) { |
| 860 |
return NULL; |
| 861 |
} |
| 862 |
|
| 863 |
cffont = cff_open(sfont->stream, offset, 0); |
| 864 |
if (!cffont) |
| 865 |
return NULL; |
| 866 |
|
| 867 |
cff_read_charsets(cffont); |
| 868 |
return cffont; |
| 869 |
} |
| 870 |
|
| 871 |
static USHORT |
| 872 |
add_to_cmap_if_used (CMap *cmap, |
| 873 |
cff_font *cffont, |
| 874 |
UsedMapElem *used_chars, |
| 875 |
USHORT gid, |
| 876 |
ULONG ch) |
| 877 |
{ |
| 878 |
USHORT count = 0; |
| 879 |
USHORT cid = cffont ? cff_charsets_lookup_inverse(cffont, gid) : gid; |
| 880 |
if (IS_USED_CHAR(used_chars, cid)) { |
| 881 |
int len; |
| 882 |
unsigned char *p = wbuf + 2; |
| 883 |
|
| 884 |
count++; |
| 885 |
|
| 886 |
wbuf[0] = (cid >> 8) & 0xff; |
| 887 |
wbuf[1] = (cid & 0xff); |
| 888 |
len = UC_sput_UTF16BE((long) ch, &p, wbuf + WBUF_SIZE); |
| 889 |
CMap_add_bfchar(cmap, wbuf, 2, wbuf + 2, len); |
| 890 |
|
| 891 |
/* Skip PUA characters and alphabetic presentation forms, allowing |
| 892 |
* handle_subst_glyphs() as it might find better mapping. Fixes the |
| 893 |
* mapping of ligatures encoded in PUA in fonts like Linux Libertine |
| 894 |
* and old Adobe fonts. |
| 895 |
*/ |
| 896 |
if (!is_PUA_or_presentation(ch)) { |
| 897 |
/* Avoid duplicate entry |
| 898 |
* There are problem when two Unicode code is mapped to |
| 899 |
* single glyph... |
| 900 |
*/ |
| 901 |
REMOVE_FROM_USED_CHARS(used_chars, cid); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
return count; |
| 906 |
} |
| 907 |
|
| 908 |
static USHORT |
| 909 |
create_ToUnicode_cmap4 (CMap *cmap, |
| 910 |
struct cmap4 *map, |
| 911 |
UsedMapElem *used_chars, |
| 912 |
cff_font *cffont) |
| 913 |
{ |
| 914 |
USHORT count = 0, segCount = map->segCountX2 / 2; |
| 915 |
USHORT i, j; |
| 916 |
|
| 917 |
for (i = 0; i < segCount; i++) { |
| 918 |
USHORT c0 = map->startCount[i]; |
| 919 |
USHORT c1 = map->endCount[i]; |
| 920 |
USHORT d = map->idRangeOffset[i] / 2 - (segCount - i); |
| 921 |
for (j = 0; j <= c1 - c0; j++) { |
| 922 |
USHORT ch = c0 + j; |
| 923 |
USHORT gid; |
| 924 |
|
| 925 |
if (map->idRangeOffset[i] == 0) { |
| 926 |
gid = (ch + map->idDelta[i]) & 0xffff; |
| 927 |
} else if (c0 == 0xffff && c1 == 0xffff && map->idRangeOffset[i] == 0xffff) { |
| 928 |
/* this is for protection against some old broken fonts... */ |
| 929 |
gid = 0; |
| 930 |
} else { |
| 931 |
gid = (map->glyphIndexArray[j + d] + map->idDelta[i]) & 0xffff; |
| 932 |
} |
| 933 |
|
| 934 |
count += add_to_cmap_if_used(cmap, cffont, used_chars, gid, ch); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
return count; |
| 939 |
} |
| 940 |
|
| 941 |
static USHORT |
| 942 |
create_ToUnicode_cmap12 (CMap *cmap, |
| 943 |
struct cmap12 *map, |
| 944 |
UsedMapElem *used_chars, |
| 945 |
cff_font *cffont) |
| 946 |
{ |
| 947 |
ULONG i, ch, count = 0; |
| 948 |
|
| 949 |
for (i = 0; i < map->nGroups; i++) { |
| 950 |
for (ch = map->groups[i].startCharCode; |
| 951 |
ch <= map->groups[i].endCharCode; ch++) { |
| 952 |
long d = ch - map->groups[i].startCharCode; |
| 953 |
USHORT gid = (USHORT) ((map->groups[i].startGlyphID + d) & 0xffff); |
| 954 |
count += add_to_cmap_if_used(cmap, cffont, used_chars, gid, ch); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
return count; |
| 959 |
} |
| 960 |
|
| 961 |
static int |
| 962 |
create_ToUnicode_cmap (tt_cmap *ttcmap, |
| 963 |
const char *cmap_name, |
| 964 |
const char *cmap_ext, |
| 965 |
CIDSysInfo *csi, |
| 966 |
CMap *cmap_add, |
| 967 |
const UsedMapElem *used_chars, |
| 968 |
sfnt *sfont, |
| 969 |
CMap *code_to_cid_cmap, |
| 970 |
const char *cmap_path, |
| 971 |
luacharmap *luamap) |
| 972 |
{ |
| 973 |
int retval = 0; |
| 974 |
CMap *cmap; |
| 975 |
USHORT count = 0; |
| 976 |
cff_font *cffont = prepare_CIDFont_from_sfnt(sfont); |
| 977 |
char is_cidfont = cffont && (cffont->flag & FONTTYPE_CIDFONT); |
| 978 |
|
| 979 |
cmap = CMap_new(); |
| 980 |
CMap_set_name (cmap, cmap_name); |
| 981 |
CMap_set_wmode(cmap, 0); |
| 982 |
CMap_set_type (cmap, CMAP_TYPE_TO_UNICODE); |
| 983 |
CMap_set_CIDSysInfo(cmap, csi); |
| 984 |
CMap_add_codespacerange(cmap, srange_min, srange_max, 2); |
| 985 |
|
| 986 |
if (luamap) { |
| 987 |
USHORT i; |
| 988 |
luamaptype *map, *current; |
| 989 |
luacharmap *p; |
| 990 |
for (i = 0; i < USED_CHARS_BUF_SIZE; i++) { |
| 991 |
int j; |
| 992 |
|
| 993 |
if (used_chars[i] == 0) |
| 994 |
continue; |
| 995 |
|
| 996 |
for (j = 0; j < BITS_PER_USED_ELEM; j++) { |
| 997 |
USHORT cid = BITS_PER_USED_ELEM * i + j; |
| 998 |
int ch; |
| 999 |
|
| 1000 |
if (!IS_USED_CHAR(used_chars, cid)) |
| 1001 |
continue; |
| 1002 |
ch = cid; |
| 1003 |
current = NULL; |
| 1004 |
p = luamap; |
| 1005 |
while ( p) { |
| 1006 |
map = LuaMap_cache_get(p->luamap_idx); |
| 1007 |
HASH_FIND_INT(map, &ch, current); /* s: output pointer */ |
| 1008 |
if (current) |
| 1009 |
break; |
| 1010 |
p = p->next; |
| 1011 |
} |
| 1012 |
if (current) { |
| 1013 |
if (current->tu_count > 0) { |
| 1014 |
unsigned char* p = wbuf + 2; |
| 1015 |
int k; |
| 1016 |
long len = 0; |
| 1017 |
for (k = 0; k < current->tu_count; ++k) { |
| 1018 |
len += UC_sput_UTF16BE(current->tounicode[k], &p, wbuf + WBUF_SIZE); |
| 1019 |
} |
| 1020 |
wbuf[0] = (cid >> 8) & 0xff; |
| 1021 |
wbuf[1] = cid & 0xff; |
| 1022 |
CMap_add_bfchar(cmap, wbuf, 2, wbuf + 2, len); |
| 1023 |
} |
| 1024 |
count++; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} else if (code_to_cid_cmap && cffont && is_cidfont) { |
| 1029 |
USHORT i; |
| 1030 |
for (i = 0; i < USED_CHARS_BUF_SIZE; i++) { |
| 1031 |
int j; |
| 1032 |
|
| 1033 |
if (used_chars[i] == 0) |
| 1034 |
continue; |
| 1035 |
|
| 1036 |
for (j = 0; j < BITS_PER_USED_ELEM; j++) { |
| 1037 |
USHORT cid = BITS_PER_USED_ELEM * i + j; |
| 1038 |
int ch; |
| 1039 |
|
| 1040 |
if (!IS_USED_CHAR(used_chars, cid)) |
| 1041 |
continue; |
| 1042 |
|
| 1043 |
ch = CMap_reverse_decode(code_to_cid_cmap, cid); |
| 1044 |
if (ch >= 0) { |
| 1045 |
long len; |
| 1046 |
unsigned char *p = wbuf + 2; |
| 1047 |
wbuf[0] = (cid >> 8) & 0xff; |
| 1048 |
wbuf[1] = cid & 0xff; |
| 1049 |
len = UC_sput_UTF16BE((long)ch, &p, wbuf + WBUF_SIZE); |
| 1050 |
CMap_add_bfchar(cmap, wbuf, 2, wbuf + 2, len); |
| 1051 |
count++; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
} else { |
| 1056 |
static UsedMapElem used_chars_copy[USED_CHARS_BUF_SIZE]; |
| 1057 |
memcpy(used_chars_copy, used_chars, sizeof(used_chars_copy)); |
| 1058 |
|
| 1059 |
/* For create_ToUnicode_cmap{4,12}(), cffont is for GID -> CID lookup, |
| 1060 |
* so it is only needed for CID fonts. */ |
| 1061 |
switch (ttcmap->format) { |
| 1062 |
case 4: |
| 1063 |
count = create_ToUnicode_cmap4(cmap, ttcmap->map, used_chars_copy, |
| 1064 |
is_cidfont ? cffont : NULL); |
| 1065 |
break; |
| 1066 |
case 12: |
| 1067 |
count = create_ToUnicode_cmap12(cmap, ttcmap->map, used_chars_copy, |
| 1068 |
is_cidfont ? cffont : NULL); |
| 1069 |
break; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (count > 0) { |
| 1074 |
retval = CMap_create_file(cmap_path, cmap_name, cmap_ext,cmap, 0); |
| 1075 |
} |
| 1076 |
CMap_release(cmap); |
| 1077 |
|
| 1078 |
if (cffont) |
| 1079 |
cff_close(cffont); |
| 1080 |
|
| 1081 |
return retval; |
| 1082 |
} |
| 1083 |
|
| 1084 |
typedef struct { |
| 1085 |
short platform; |
| 1086 |
short encoding; |
| 1087 |
} cmap_plat_enc_rec; |
| 1088 |
|
| 1089 |
static cmap_plat_enc_rec cmap_plat_encs[] = { |
| 1090 |
{ 3, 10 }, |
| 1091 |
{ 0, 3 }, |
| 1092 |
{ 0, 0 }, |
| 1093 |
{ 3, 1 }, |
| 1094 |
{ 0, 1 } |
| 1095 |
}; |
| 1096 |
|
| 1097 |
int |
| 1098 |
otf_create_ToUnicode_stream (sfnt *sfont, |
| 1099 |
const char *font_name, |
| 1100 |
CMap *code_to_cid_cmap, |
| 1101 |
CIDSysInfo *csi, |
| 1102 |
const UsedMapElem *used_chars, |
| 1103 |
const char *path, |
| 1104 |
const char *cmap_name, |
| 1105 |
const char *cmap_ext, |
| 1106 |
void *luamap) |
| 1107 |
{ |
| 1108 |
CMap *cmap_add; |
| 1109 |
int cmap_add_id; |
| 1110 |
tt_cmap *ttcmap; |
| 1111 |
int i, cmap_type, cmap_ret = 0; |
| 1112 |
luacharmap *lmap; |
| 1113 |
|
| 1114 |
if (!font_name || !used_chars || !csi) |
| 1115 |
return 0; |
| 1116 |
|
| 1117 |
|
| 1118 |
cmap_add = NULL; |
| 1119 |
|
| 1120 |
CMap_set_silent(1); /* many warnings without this... */ |
| 1121 |
lmap = (luacharmap *)luamap; |
| 1122 |
if (luamap && (lmap->luamap_idx >= 0)) { |
| 1123 |
ttcmap = NULL; |
| 1124 |
cmap_ret = create_ToUnicode_cmap(ttcmap, cmap_name, cmap_ext, csi, cmap_add, used_chars, |
| 1125 |
sfont, code_to_cid_cmap, path, lmap); |
| 1126 |
} |
| 1127 |
else { |
| 1128 |
for (i = 0; i < sizeof(cmap_plat_encs) / sizeof(cmap_plat_enc_rec); ++i) { |
| 1129 |
ttcmap = tt_cmap_read(sfont, cmap_plat_encs[i].platform, cmap_plat_encs[i].encoding); |
| 1130 |
if (!ttcmap) |
| 1131 |
continue; |
| 1132 |
|
| 1133 |
if (ttcmap->format == 4 || ttcmap->format == 12) { |
| 1134 |
cmap_ret = create_ToUnicode_cmap(ttcmap, cmap_name, cmap_ext, csi, cmap_add, used_chars, |
| 1135 |
sfont, code_to_cid_cmap, path, NULL); |
| 1136 |
break; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
tt_cmap_release(ttcmap); |
| 1140 |
} |
| 1141 |
if (cmap_ret == 0) |
| 1142 |
WARN("Unable to read OpenType/TrueType Unicode cmap table."); |
| 1143 |
CMap_set_silent(0); |
| 1144 |
|
| 1145 |
return 1; |
| 1146 |
} |
| 1147 |
|
| 1148 |
/* Must be smaller than (WBUF_SIZE-2)/8 */ |
| 1149 |
#define MAX_UNICODES 16 |
| 1150 |
|
| 1151 |
struct gent |
| 1152 |
{ |
| 1153 |
USHORT gid; |
| 1154 |
long ucv; /* assigned PUA unicode */ |
| 1155 |
|
| 1156 |
int num_unicodes; |
| 1157 |
long unicodes[MAX_UNICODES]; |
| 1158 |
}; |
| 1159 |
|
| 1160 |
static void |
| 1161 |
create_cmaps (CMap *cmap, CMap *tounicode, |
| 1162 |
struct ht_table *unencoded, unsigned char *GIDToCIDMap) |
| 1163 |
{ |
| 1164 |
struct ht_iter iter; |
| 1165 |
|
| 1166 |
ASSERT(cmap && unencoded); |
| 1167 |
|
| 1168 |
if (ht_set_iter(unencoded, &iter) < 0) |
| 1169 |
return; |
| 1170 |
|
| 1171 |
CMap_set_silent(1); /* many warnings without this... */ |
| 1172 |
|
| 1173 |
do { |
| 1174 |
struct gent *glyph; |
| 1175 |
unsigned char *ucv; |
| 1176 |
int i, len; |
| 1177 |
unsigned char *p, *endptr; |
| 1178 |
CID cid; |
| 1179 |
|
| 1180 |
glyph = (struct gent *) ht_iter_getval(&iter); |
| 1181 |
ucv = (unsigned char *) ht_iter_getkey(&iter, &len); |
| 1182 |
|
| 1183 |
if (GIDToCIDMap) { |
| 1184 |
cid = ((GIDToCIDMap[2 * glyph->gid] << 8)|GIDToCIDMap[2 * glyph->gid + 1]); |
| 1185 |
if (cid == 0) |
| 1186 |
WARN("Glyph gid=%u does not have corresponding CID.", glyph->gid); |
| 1187 |
} else { |
| 1188 |
cid = glyph->gid; |
| 1189 |
} |
| 1190 |
|
| 1191 |
CMap_add_cidchar(cmap, ucv, 4, cid); |
| 1192 |
|
| 1193 |
if (tounicode) { |
| 1194 |
wbuf[0] = (cid >> 8) & 0xff; |
| 1195 |
wbuf[1] = cid & 0xff; |
| 1196 |
p = wbuf + 2; |
| 1197 |
endptr = wbuf + WBUF_SIZE; |
| 1198 |
len = 0; |
| 1199 |
for (i = 0; i < glyph->num_unicodes; i++) { |
| 1200 |
len += UC_sput_UTF16BE(glyph->unicodes[i], &p, endptr); |
| 1201 |
} |
| 1202 |
CMap_add_bfchar(tounicode, wbuf, 2, wbuf + 2, len); |
| 1203 |
} |
| 1204 |
} while (ht_iter_next(&iter) >= 0); |
| 1205 |
|
| 1206 |
CMap_set_silent(0); |
| 1207 |
|
| 1208 |
ht_clear_iter(&iter); |
| 1209 |
} |
| 1210 |
|
| 1211 |
static void |
| 1212 |
add_glyph (struct ht_table *unencoded, |
| 1213 |
USHORT gid, long ucv, int num_unicodes, long *unicodes) |
| 1214 |
{ |
| 1215 |
struct gent *glyph; |
| 1216 |
int i; |
| 1217 |
|
| 1218 |
ASSERT(unencoded); |
| 1219 |
|
| 1220 |
if (gid == 0 || num_unicodes < 1) { |
| 1221 |
return; |
| 1222 |
} |
| 1223 |
|
| 1224 |
wbuf[0] = (ucv >> 24) & 0xff; |
| 1225 |
wbuf[1] = (ucv >> 16) & 0xff; |
| 1226 |
wbuf[2] = (ucv >> 8) & 0xff; |
| 1227 |
wbuf[3] = ucv & 0xff; |
| 1228 |
|
| 1229 |
glyph = NEW(1, struct gent); |
| 1230 |
glyph->gid = gid; |
| 1231 |
glyph->num_unicodes = num_unicodes; |
| 1232 |
for (i = 0; |
| 1233 |
i < num_unicodes && i < MAX_UNICODES; i++) { |
| 1234 |
glyph->unicodes[i] = unicodes[i]; |
| 1235 |
} |
| 1236 |
|
| 1237 |
ht_append_table(unencoded, wbuf, 4, glyph); |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* This seriously affects speed... */ |
| 1241 |
static struct gent * |
| 1242 |
find_glyph (struct ht_table *unencoded, long ucv) |
| 1243 |
{ |
| 1244 |
ASSERT(unencoded); |
| 1245 |
|
| 1246 |
wbuf[0] = (ucv >> 24) & 0xff; |
| 1247 |
wbuf[1] = (ucv >> 16) & 0xff; |
| 1248 |
wbuf[2] = (ucv >> 8) & 0xff; |
| 1249 |
wbuf[3] = ucv & 0xff; |
| 1250 |
|
| 1251 |
return (struct gent *) ht_lookup_table(unencoded, wbuf, 4); |
| 1252 |
} |
| 1253 |
|
| 1254 |
static CMap* |
| 1255 |
load_base_CMap (const char *cmap_name, int wmode, |
| 1256 |
CIDSysInfo *csi, unsigned char *GIDToCIDMap, |
| 1257 |
tt_cmap *ttcmap) |
| 1258 |
{ |
| 1259 |
CMap *cmap; |
| 1260 |
|
| 1261 |
cmap = CMap_new(); |
| 1262 |
CMap_set_name (cmap, cmap_name); |
| 1263 |
CMap_set_type (cmap, CMAP_TYPE_CODE_TO_CID); |
| 1264 |
CMap_set_wmode(cmap, wmode); |
| 1265 |
CMap_add_codespacerange(cmap, lrange_min, lrange_max, 4); |
| 1266 |
|
| 1267 |
if (csi) { /* CID */ |
| 1268 |
CMap_set_CIDSysInfo(cmap, csi); |
| 1269 |
} else { |
| 1270 |
CMap_set_CIDSysInfo(cmap, &CSI_IDENTITY); |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (ttcmap->format == 12) { |
| 1274 |
load_cmap12(ttcmap->map, GIDToCIDMap, cmap); |
| 1275 |
} else if (ttcmap->format == 4) { |
| 1276 |
load_cmap4(ttcmap->map, GIDToCIDMap, cmap); |
| 1277 |
} |
| 1278 |
|
| 1279 |
return cmap; |
| 1280 |
} |
| 1281 |
|
| 1282 |
static void CDECL |
| 1283 |
hval_free (void *hval) |
| 1284 |
{ |
| 1285 |
RELEASE(hval); |
| 1286 |
} |
| 1287 |
|
| 1288 |
CMap* |
| 1289 |
otf_load_Unicode_CMap (const char *map_name, sfnt *sfont, |
| 1290 |
const char *otl_tags, int wmode) |
| 1291 |
{ |
| 1292 |
int tounicode_id = -1, is_cidfont = 0; |
| 1293 |
char *base_name = NULL, *cmap_name = NULL; |
| 1294 |
char *tounicode_name = NULL; |
| 1295 |
FILE *fp = NULL; |
| 1296 |
tt_cmap *ttcmap; |
| 1297 |
CMap *cmap, *base, *tounicode = NULL; |
| 1298 |
CIDSysInfo csi = {NULL, NULL, 0}; |
| 1299 |
unsigned char *GIDToCIDMap = NULL; |
| 1300 |
|
| 1301 |
if (!map_name) |
| 1302 |
return NULL; |
| 1303 |
|
| 1304 |
cmap = NULL; |
| 1305 |
|
| 1306 |
base_name = NEW(strlen(map_name)+strlen("-UCS4-H")+5, char); |
| 1307 |
if (wmode) |
| 1308 |
sprintf(base_name, "%s,%03d-UCS4-V", map_name, 0); |
| 1309 |
else { |
| 1310 |
sprintf(base_name, "%s,%03d-UCS4-H", map_name, 0); |
| 1311 |
} |
| 1312 |
|
| 1313 |
if (otl_tags) { |
| 1314 |
cmap_name = NEW(strlen(map_name)+strlen(otl_tags)+strlen("-UCS4-H")+6, char); |
| 1315 |
if (wmode) |
| 1316 |
sprintf(cmap_name, "%s,%03d,%s-UCS4-V", map_name, 0, otl_tags); |
| 1317 |
else |
| 1318 |
sprintf(cmap_name, "%s,%03d,%s-UCS4-H", map_name, 0, otl_tags); |
| 1319 |
} |
| 1320 |
else { |
| 1321 |
cmap_name = NEW(strlen(base_name)+1, char); |
| 1322 |
strcpy(cmap_name, base_name); |
| 1323 |
} |
| 1324 |
|
| 1325 |
if (sfont->type == SFNT_TYPE_POSTSCRIPT) { |
| 1326 |
is_cidfont = handle_CIDFont(sfont, &GIDToCIDMap, &csi); |
| 1327 |
} else { |
| 1328 |
is_cidfont = 0; |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (is_cidfont) { |
| 1332 |
tounicode_name = NULL; |
| 1333 |
} else { |
| 1334 |
tounicode_name = NEW(strlen(map_name)+strlen("-UTF16")+5, char); |
| 1335 |
sprintf(tounicode_name, "%s,%03d-UTF16", map_name, 0); |
| 1336 |
} |
| 1337 |
|
| 1338 |
ttcmap = tt_cmap_read(sfont, 3, 10); /* Microsoft UCS4 */ |
| 1339 |
if (!ttcmap) { |
| 1340 |
ttcmap = tt_cmap_read(sfont, 3, 1); /* Microsoft UCS2 */ |
| 1341 |
if (!ttcmap) { |
| 1342 |
ttcmap = tt_cmap_read(sfont, 0, 3); /* Unicode 2.0 or later */ |
| 1343 |
if (!ttcmap) { |
| 1344 |
ttcmap = tt_cmap_read(sfont, 3, 0); /* Windows Symbol encoding */ |
| 1345 |
if (!ttcmap) { |
| 1346 |
ttcmap = tt_cmap_read(sfont, 1, 0); /* Mac encoding */ |
| 1347 |
if (!ttcmap) { |
| 1348 |
RELEASE(base_name); |
| 1349 |
return NULL; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
} |
| 1353 |
} |
| 1354 |
} |
| 1355 |
cmap = load_base_CMap(base_name, wmode, |
| 1356 |
(is_cidfont ? &csi : NULL), |
| 1357 |
GIDToCIDMap, ttcmap); |
| 1358 |
if (cmap == NULL) |
| 1359 |
ERROR("Failed to read OpenType/TrueType cmap table."); |
| 1360 |
|
| 1361 |
if (!otl_tags) { |
| 1362 |
RELEASE(cmap_name); |
| 1363 |
RELEASE(base_name); |
| 1364 |
if (GIDToCIDMap) |
| 1365 |
RELEASE(GIDToCIDMap); |
| 1366 |
if (tounicode_name) |
| 1367 |
RELEASE(tounicode_name); |
| 1368 |
if (is_cidfont) { |
| 1369 |
if (csi.registry) |
| 1370 |
RELEASE(csi.registry); |
| 1371 |
if (csi.ordering) |
| 1372 |
RELEASE(csi.ordering); |
| 1373 |
} |
| 1374 |
tt_cmap_release(ttcmap); |
| 1375 |
|
| 1376 |
return cmap; |
| 1377 |
} |
| 1378 |
|
| 1379 |
base = cmap; |
| 1380 |
|
| 1381 |
cmap = CMap_new(); |
| 1382 |
CMap_set_name (cmap, cmap_name); |
| 1383 |
CMap_set_type (cmap, CMAP_TYPE_CODE_TO_CID); |
| 1384 |
CMap_set_wmode(cmap, wmode); |
| 1385 |
/* CMap_add_codespacerange(cmap, lrange_min, lrange_max, 4); */ |
| 1386 |
CMap_set_usecmap(cmap, base); |
| 1387 |
CMap_add_cidchar(cmap, lrange_max, 4, 0); /* FIXME */ |
| 1388 |
|
| 1389 |
if (is_cidfont) { |
| 1390 |
CMap_set_CIDSysInfo(cmap, &csi); |
| 1391 |
if (csi.registry) |
| 1392 |
RELEASE(csi.registry); |
| 1393 |
if (csi.ordering) |
| 1394 |
RELEASE(csi.ordering); |
| 1395 |
} else { |
| 1396 |
CMap_set_CIDSysInfo(cmap, &CSI_IDENTITY); |
| 1397 |
} |
| 1398 |
|
| 1399 |
if (!is_cidfont && tounicode_id < 0) /* New */ |
| 1400 |
CMap_release(tounicode); |
| 1401 |
|
| 1402 |
tt_cmap_release(ttcmap); |
| 1403 |
|
| 1404 |
if (GIDToCIDMap) |
| 1405 |
RELEASE(GIDToCIDMap); |
| 1406 |
if (base_name) |
| 1407 |
RELEASE(base_name); |
| 1408 |
if (cmap_name) |
| 1409 |
RELEASE(cmap_name); |
| 1410 |
if (tounicode_name) |
| 1411 |
RELEASE(tounicode_name); |
| 1412 |
|
| 1413 |
return cmap; |
| 1414 |
} |
| 1415 |
|
| 1416 |
extern CMap* ttf_load_Native_CMap (const char *map_name, sfnt *sfont) |
| 1417 |
{ |
| 1418 |
char *base_name = NULL, *cmap_name = NULL; |
| 1419 |
FILE *fp = NULL; |
| 1420 |
tt_cmap *ttcmap; |
| 1421 |
CMap *cmap; |
| 1422 |
|
| 1423 |
if (!map_name) |
| 1424 |
return NULL; |
| 1425 |
|
| 1426 |
cmap = NULL; |
| 1427 |
|
| 1428 |
base_name = NEW(strlen(map_name)+strlen("-UCS4-H")+5, char); |
| 1429 |
sprintf(base_name, "%s,%03d-UCS4-V", map_name, 0); |
| 1430 |
|
| 1431 |
ttcmap = tt_cmap_read(sfont, 3, 0); /* Windows Symbol encoding */ |
| 1432 |
if (!ttcmap) { |
| 1433 |
ttcmap = tt_cmap_read(sfont, 1, 0); /* Mac encoding */ |
| 1434 |
if (!ttcmap) { |
| 1435 |
ERROR("Unable to read TrueType Native cmap table."); |
| 1436 |
} |
| 1437 |
} |
| 1438 |
cmap = load_base_CMap(base_name, 0,NULL, NULL, ttcmap); |
| 1439 |
if (cmap == NULL) |
| 1440 |
ERROR("Failed to read TrueType cmap table."); |
| 1441 |
|
| 1442 |
RELEASE(cmap_name); |
| 1443 |
RELEASE(base_name); |
| 1444 |
tt_cmap_release(ttcmap); |
| 1445 |
|
| 1446 |
return cmap; |
| 1447 |
} |