| 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 |
* TrueType glyf table is sorted by CID and no CIDToGIDMap is used here. |
| 26 |
* GhostScript can't handle CIDToGIDMap correctly. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include "system.h" |
| 32 |
#include "numbers.h" |
| 33 |
#include "mem.h" |
| 34 |
#include "error.h" |
| 35 |
|
| 36 |
#include "xdvips.h" |
| 37 |
#include "protos.h" |
| 38 |
|
| 39 |
/* TrueType */ |
| 40 |
#include "sfnt.h" |
| 41 |
#include "tt_aux.h" |
| 42 |
#include "tt_glyf.h" |
| 43 |
#include "tt_table.h" |
| 44 |
|
| 45 |
/* CID font */ |
| 46 |
#include "cidtype2.h" |
| 47 |
#include "cmap.h" |
| 48 |
#include "tt_cmap.h" |
| 49 |
|
| 50 |
static int verbose = 0; |
| 51 |
|
| 52 |
/* |
| 53 |
* PDF viewer applications use following tables (CIDFontType 2) |
| 54 |
* |
| 55 |
* head, hhea, loca, maxp, glyf, hmtx, fpgm, cvt_, prep |
| 56 |
* |
| 57 |
* - from PDF Ref. v.1.3, 2nd ed. |
| 58 |
* |
| 59 |
* The fpgm, cvt_, and prep tables appears only when TrueType instructions |
| 60 |
* requires them. Those tables must be preserved if they exist. |
| 61 |
* We use must_exist flag to indicate `preserve it if present' |
| 62 |
* and to make sure not to cause an error when it does not exist. |
| 63 |
* |
| 64 |
* post and name table must exist in ordinary TrueType font file, |
| 65 |
* but when a TrueType font is converted to CIDFontType 2 font, those tables |
| 66 |
* are no longer required. |
| 67 |
* |
| 68 |
* The OS/2 table (required for TrueType font for Windows and OS/2) contains |
| 69 |
* liscencing information, but PDF viewers seems not using them. |
| 70 |
* |
| 71 |
* The 'name' table added. See comments in ttf.c. |
| 72 |
*/ |
| 73 |
|
| 74 |
static struct |
| 75 |
{ |
| 76 |
const char *name; |
| 77 |
int must_exist; |
| 78 |
} required_table[] = { |
| 79 |
{"OS/2", 0}, {"head", 1}, {"hhea", 1}, {"loca", 1}, {"maxp", 1}, |
| 80 |
{"name", 1}, {"glyf", 1}, {"hmtx", 1}, {"fpgm", 0}, {"cvt ", 0}, |
| 81 |
{"prep", 0}, {"vhea", 0}, {"vmtx", 0}, {NULL, 0} |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
/* |
| 87 |
* The following routine fixes few problems caused by vendor specific |
| 88 |
* Unicode mappings. |
| 89 |
*/ |
| 90 |
|
| 91 |
#define FIX_CJK_UNIOCDE_SYMBOLS 1 |
| 92 |
|
| 93 |
static unsigned short |
| 94 |
fix_CJK_symbols (unsigned short code) |
| 95 |
{ |
| 96 |
unsigned short alt_code; |
| 97 |
static struct |
| 98 |
{ |
| 99 |
unsigned short alt1; |
| 100 |
unsigned short alt2; |
| 101 |
} CJK_Uni_symbols[] = { |
| 102 |
/* |
| 103 |
* Microsoft/Apple Unicode mapping difference: |
| 104 |
* They are taken from SJIS-Unicode mapping difference but nearly |
| 105 |
* same thing might be applied to Chinese (e.g., Big5) too. |
| 106 |
*/ |
| 107 |
{0x2014, 0x2015}, /* EM DASH <-> HORIZONTAL BAR */ |
| 108 |
{0x2016, 0x2225}, /* DOUBLE VERTICAL LINE <-> PARALLEL TO */ |
| 109 |
{0x203E, 0xFFE3}, /* OVERLINE <-> FULLWIDTH MACRON */ |
| 110 |
{0x2026, 0x22EF}, /* HORIZONTAL ELLIPSIS <-> MIDLINE HORIZONTAL ELLIPSIS */ |
| 111 |
{0x2212, 0xFF0D}, /* MINUS SIGN <-> FULLWIDTH HYPHEN-MINUS */ |
| 112 |
{0x301C, 0xFF5E}, /* WAVE DASH <-> FULLWIDTH TILDE */ |
| 113 |
{0xFFE0, 0x00A2}, /* FULLWIDTH CENT SIGN <-> CENT SIGN */ |
| 114 |
{0xFFE1, 0x00A3}, /* FULLWIDTH POUND SIGN <-> POUND SIGN */ |
| 115 |
{0xFFE2, 0x00AC}, /* FULLWIDTH NOT SIGN <-> NOT SIGN */ |
| 116 |
{0xFFFF, 0xFFFF}, /* EOD */ |
| 117 |
}; |
| 118 |
#define NUM_CJK_SYMBOLS (sizeof(CJK_Uni_symbols)/sizeof(CJK_Uni_symbols[0])) |
| 119 |
int i; |
| 120 |
|
| 121 |
alt_code = code; |
| 122 |
for (i = 0; i < NUM_CJK_SYMBOLS; i++) { |
| 123 |
if (CJK_Uni_symbols[i].alt1 == code) { |
| 124 |
alt_code = CJK_Uni_symbols[i].alt2; |
| 125 |
break; |
| 126 |
} else if (CJK_Uni_symbols[i].alt2 == code) { |
| 127 |
alt_code = CJK_Uni_symbols[i].alt1; |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return alt_code; |
| 133 |
} |
| 134 |
|
| 135 |
static long |
| 136 |
cid_to_code (CMap *cmap, CID cid) |
| 137 |
{ |
| 138 |
unsigned char inbuf[2], outbuf[32]; |
| 139 |
long inbytesleft = 2, outbytesleft = 32; |
| 140 |
const unsigned char *p; |
| 141 |
unsigned char *q; |
| 142 |
|
| 143 |
if (!cmap) |
| 144 |
return cid; |
| 145 |
|
| 146 |
inbuf[0] = (cid >> 8) & 0xff; |
| 147 |
inbuf[1] = cid & 0xff; |
| 148 |
p = inbuf; q = outbuf; |
| 149 |
|
| 150 |
CMap_decode_char(cmap, &p, &inbytesleft, &q, &outbytesleft); |
| 151 |
|
| 152 |
if (inbytesleft != 0) |
| 153 |
return 0; |
| 154 |
else if (outbytesleft == 31) |
| 155 |
return (long) outbuf[0]; |
| 156 |
else if (outbytesleft == 30) |
| 157 |
return (long) (outbuf[0] << 8|outbuf[1]); |
| 158 |
else if (outbytesleft == 28) { /* We assume the output encoding is UTF-16. */ |
| 159 |
CID hi, lo; |
| 160 |
hi = outbuf[0] << 8|outbuf[1]; |
| 161 |
lo = outbuf[2] << 8|outbuf[3]; |
| 162 |
if (hi >= 0xd800 && hi <= 0xdbff && lo >= 0xdc00 && lo <= 0xdfff) |
| 163 |
return (long) ((hi - 0xd800) * 0x400 + 0x10000 + lo - 0xdc00); |
| 164 |
else |
| 165 |
return (long) (hi << 16|lo); |
| 166 |
} |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
/* #define NO_GHOSTSCRIPT_BUG 1 */ |
| 172 |
|
| 173 |
void |
| 174 |
CIDFont_type2_dofont (const char *PSName, sfnt *sfont, int index, UsedMapElem *used_glyphs, long *maxcid) |
| 175 |
{ |
| 176 |
UsedMapElem *h_used_chars, *v_used_chars, *used_chars; |
| 177 |
struct tt_glyphs *glyphs; |
| 178 |
CMap *cmap = NULL; |
| 179 |
tt_cmap *ttcmap = NULL; |
| 180 |
unsigned long offset = 0; |
| 181 |
CID cid, last_cid; |
| 182 |
unsigned char *cidtogidmap; |
| 183 |
USHORT num_glyphs; |
| 184 |
int i, glyph_ordering = 0, unicode_cmap = 0; |
| 185 |
|
| 186 |
|
| 187 |
ASSERT(sfont); |
| 188 |
|
| 189 |
switch (sfont->type) { |
| 190 |
case SFNT_TYPE_TTC: |
| 191 |
offset = ttc_read_offset(sfont, index); |
| 192 |
if (offset == 0) |
| 193 |
ERROR("Invalid TTC index in %s.", PSName); |
| 194 |
break; |
| 195 |
case SFNT_TYPE_TRUETYPE: |
| 196 |
if (index > 0) |
| 197 |
ERROR("Found TrueType font file while expecting TTC file (%s).", PSName); |
| 198 |
offset = 0; |
| 199 |
break; |
| 200 |
case SFNT_TYPE_DFONT: |
| 201 |
offset = sfont->offset; |
| 202 |
break; |
| 203 |
default: |
| 204 |
ERROR("Not a TrueType/TTC font (%s)?", PSName); |
| 205 |
break; |
| 206 |
} |
| 207 |
|
| 208 |
if (sfnt_read_table_directory(sfont, offset) < 0) |
| 209 |
ERROR("Could not read TrueType table directory (%s).", PSName); |
| 210 |
|
| 211 |
glyph_ordering = 1; |
| 212 |
ttcmap = NULL; |
| 213 |
cmap = NULL; |
| 214 |
|
| 215 |
glyphs = tt_build_init(); |
| 216 |
|
| 217 |
last_cid = 0; |
| 218 |
num_glyphs = 1; /* .notdef */ |
| 219 |
used_chars = h_used_chars = v_used_chars = NULL; |
| 220 |
{ |
| 221 |
int c; |
| 222 |
|
| 223 |
h_used_chars = used_glyphs; |
| 224 |
|
| 225 |
/* |
| 226 |
* Quick check of max CID. |
| 227 |
*/ |
| 228 |
c = 0; |
| 229 |
for (i = USED_CHARS_BUF_SIZE - 1; i >= 0; i--) { |
| 230 |
if (h_used_chars && h_used_chars[i] != 0) { |
| 231 |
last_cid = i * BITS_PER_USED_ELEM + BITS_PER_USED_ELEM - 1; |
| 232 |
c = h_used_chars[i]; |
| 233 |
break; |
| 234 |
} |
| 235 |
if (v_used_chars && v_used_chars[i] != 0) { |
| 236 |
last_cid = i * BITS_PER_USED_ELEM + BITS_PER_USED_ELEM - 1; |
| 237 |
c = v_used_chars[i]; |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
if (last_cid > 0) { |
| 242 |
for (i = 0; i < BITS_PER_USED_ELEM; i++) { |
| 243 |
if ((c >> i) & 1) |
| 244 |
break; |
| 245 |
last_cid--; |
| 246 |
} |
| 247 |
} |
| 248 |
if (last_cid >= 0xFFFFu) { |
| 249 |
ERROR("CID count > 65535"); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 254 |
cidtogidmap = NULL; |
| 255 |
#else |
| 256 |
cidtogidmap = NEW((last_cid + 1) * 2, unsigned char); |
| 257 |
memset(cidtogidmap, 0, (last_cid + 1) * 2); |
| 258 |
#endif /* !NO_GHOSTSCRIPT_BUG */ |
| 259 |
|
| 260 |
/* |
| 261 |
* Map CIDs to GIDs. |
| 262 |
* Horizontal and vertical used_chars are merged. |
| 263 |
*/ |
| 264 |
|
| 265 |
/* |
| 266 |
* Horizontal |
| 267 |
*/ |
| 268 |
if (h_used_chars) { |
| 269 |
used_chars = h_used_chars; |
| 270 |
for (cid = 1; cid <= last_cid; cid++) { |
| 271 |
long code; |
| 272 |
unsigned short gid; |
| 273 |
|
| 274 |
if (!IS_USED_CHAR(h_used_chars, cid)) |
| 275 |
continue; |
| 276 |
|
| 277 |
if (glyph_ordering) { |
| 278 |
gid = cid; |
| 279 |
code = cid; |
| 280 |
} else { |
| 281 |
code = cid_to_code(cmap, cid); |
| 282 |
gid = tt_cmap_lookup(ttcmap, code); |
| 283 |
#ifdef FIX_CJK_UNIOCDE_SYMBOLS |
| 284 |
if (gid == 0 && unicode_cmap) { |
| 285 |
long alt_code; |
| 286 |
|
| 287 |
alt_code = fix_CJK_symbols((unsigned short)code); |
| 288 |
if (alt_code != code) { |
| 289 |
gid = tt_cmap_lookup(ttcmap, alt_code); |
| 290 |
if (gid != 0) { |
| 291 |
WARN("Unicode char U+%04x replaced with U+%04x.", |
| 292 |
code, alt_code); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
#endif /* FIX_CJK_UNIOCDE_SYMBOLS */ |
| 297 |
} |
| 298 |
|
| 299 |
if (gid == 0) { |
| 300 |
WARN("Glyph missing in font. (CID=%u, code=0x%04x)", cid, code); |
| 301 |
} |
| 302 |
|
| 303 |
/* TODO: duplicated glyph */ |
| 304 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 305 |
gid = tt_add_glyph(glyphs, gid, cid); |
| 306 |
#else |
| 307 |
gid = tt_add_glyph(glyphs, gid, num_glyphs); |
| 308 |
cidtogidmap[2*cid ] = gid >> 8; |
| 309 |
cidtogidmap[2*cid+1] = gid & 0xff; |
| 310 |
#endif /* !NO_GHOSTSCRIPT_BUG */ |
| 311 |
|
| 312 |
num_glyphs++; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/* |
| 317 |
* Vertical |
| 318 |
*/ |
| 319 |
if (v_used_chars) { |
| 320 |
|
| 321 |
/* |
| 322 |
* Require `vrt2' or `vert'. |
| 323 |
*/ |
| 324 |
|
| 325 |
for (cid = 1; cid <= last_cid; cid++) { |
| 326 |
long code; |
| 327 |
unsigned short gid; |
| 328 |
|
| 329 |
if (!IS_USED_CHAR(v_used_chars, cid)) |
| 330 |
continue; |
| 331 |
|
| 332 |
/* There may be conflict of horizontal and vertical glyphs |
| 333 |
* when font is used with /UCS. However, we simply ignore |
| 334 |
* that... |
| 335 |
*/ |
| 336 |
if (h_used_chars && IS_USED_CHAR(h_used_chars, cid)) { |
| 337 |
continue; |
| 338 |
} |
| 339 |
|
| 340 |
if (glyph_ordering) { |
| 341 |
gid = cid; |
| 342 |
code = cid; |
| 343 |
} else { |
| 344 |
code = cid_to_code(cmap, cid); |
| 345 |
gid = tt_cmap_lookup(ttcmap, code); |
| 346 |
#ifdef FIX_CJK_UNIOCDE_SYMBOLS |
| 347 |
if (gid == 0 && unicode_cmap) { |
| 348 |
long alt_code; |
| 349 |
|
| 350 |
alt_code = fix_CJK_symbols((unsigned short)code); |
| 351 |
if (alt_code != code) { |
| 352 |
gid = tt_cmap_lookup(ttcmap, alt_code); |
| 353 |
if (gid != 0) { |
| 354 |
WARN("Unicode char U+%04x replaced with U+%04x.", |
| 355 |
code, alt_code); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
#endif /* FIX_CJK_UNIOCDE_SYMBOLS */ |
| 360 |
} |
| 361 |
if (gid == 0) { |
| 362 |
WARN("Glyph missing in font. (CID=%u, code=0x%04x)", cid, code); |
| 363 |
} |
| 364 |
|
| 365 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 366 |
gid = tt_add_glyph(glyphs, gid, cid); |
| 367 |
#else |
| 368 |
gid = tt_add_glyph(glyphs, gid, num_glyphs); |
| 369 |
cidtogidmap[2*cid ] = gid >> 8; |
| 370 |
cidtogidmap[2*cid+1] = gid & 0xff; |
| 371 |
#endif /* !NO_GHOSTSCRIPT_BUG */ |
| 372 |
|
| 373 |
if (used_chars) /* merge vertical used_chars to horizontal */ |
| 374 |
ADD_TO_USED_CHARS(used_chars, cid); |
| 375 |
|
| 376 |
num_glyphs++; |
| 377 |
} |
| 378 |
|
| 379 |
if (!used_chars) /* We have no horizontal. */ |
| 380 |
used_chars = v_used_chars; |
| 381 |
} |
| 382 |
|
| 383 |
if (!used_chars) |
| 384 |
ERROR("Unexpected error."); |
| 385 |
|
| 386 |
tt_cmap_release(ttcmap); |
| 387 |
|
| 388 |
if (tt_build_tables(sfont, glyphs) < 0) |
| 389 |
ERROR("Could not created FontFile stream."); |
| 390 |
if (verbose > 1) |
| 391 |
MESG("[%u glyphs (Max CID: %u)]", glyphs->num_glyphs, last_cid); |
| 392 |
|
| 393 |
tt_build_finish(glyphs); |
| 394 |
|
| 395 |
*maxcid = last_cid; |
| 396 |
|
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
void |
| 401 |
CIDFont_type2_checktables(sfnt *sfont) |
| 402 |
{ |
| 403 |
int i; |
| 404 |
|
| 405 |
for (i = 0; required_table[i].name; i++) { |
| 406 |
if (sfnt_require_table(sfont, |
| 407 |
required_table[i].name, |
| 408 |
required_table[i].must_exist) < 0) { |
| 409 |
ERROR("Some required TrueType table (%s) does not exist.", required_table[i].name); |
| 410 |
} |
| 411 |
} |
| 412 |
} |