| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2021 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 |
* TrueType glyf table is sorted by CID and no CIDToGIDMap is used here. |
| 23 |
* GhostScript can't handle CIDToGIDMap correctly. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include <config.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "system.h" |
| 31 |
#include "numbers.h" |
| 32 |
#include "mem.h" |
| 33 |
#include "error.h" |
| 34 |
#include "dpxconf.h" |
| 35 |
#include "dpxfile.h" |
| 36 |
|
| 37 |
#include "unicode.h" |
| 38 |
|
| 39 |
#include "pdfobj.h" |
| 40 |
/* pseudo unique tag */ |
| 41 |
#include "pdffont.h" |
| 42 |
|
| 43 |
#ifndef PDF_NAME_LEN_MAX |
| 44 |
# define PDF_NAME_LEN_MAX 255 |
| 45 |
#endif |
| 46 |
|
| 47 |
/* TrueType */ |
| 48 |
#include "sfnt.h" |
| 49 |
#include "tt_aux.h" |
| 50 |
#include "tt_glyf.h" |
| 51 |
#include "tt_cmap.h" |
| 52 |
#include "tt_table.h" |
| 53 |
|
| 54 |
#include "tt_gsub.h" |
| 55 |
|
| 56 |
/* CID font */ |
| 57 |
#include "cmap.h" |
| 58 |
#include "type0.h" |
| 59 |
#include "cid.h" |
| 60 |
#include "cidtype2.h" |
| 61 |
|
| 62 |
/* |
| 63 |
* PDF viewer applications use following tables (CIDFontType 2) |
| 64 |
* |
| 65 |
* head, hhea, loca, maxp, glyf, hmtx, fpgm, cvt_, prep |
| 66 |
* |
| 67 |
* - from PDF Ref. v.1.3, 2nd ed. |
| 68 |
* |
| 69 |
* The fpgm, cvt_, and prep tables appears only when TrueType instructions |
| 70 |
* requires them. Those tables must be preserved if they exist. |
| 71 |
* We use must_exist flag to indicate `preserve it if present' |
| 72 |
* and to make sure not to cause an error when it does not exist. |
| 73 |
* |
| 74 |
* post and name table must exist in ordinary TrueType font file, |
| 75 |
* but when a TrueType font is converted to CIDFontType 2 font, those tables |
| 76 |
* are no longer required. |
| 77 |
* |
| 78 |
* The OS/2 table (required for TrueType font for Windows and OS/2) contains |
| 79 |
* liscencing information, but PDF viewers seems not using them. |
| 80 |
* |
| 81 |
* The 'name' table added. See comments in ttf.c. |
| 82 |
*/ |
| 83 |
|
| 84 |
static struct |
| 85 |
{ |
| 86 |
const char *name; |
| 87 |
int must_exist; |
| 88 |
} required_table[] = { |
| 89 |
{"OS/2", 0}, {"head", 1}, {"hhea", 1}, {"loca", 1}, {"maxp", 1}, |
| 90 |
{"name", 1}, {"glyf", 1}, {"hmtx", 1}, {"fpgm", 0}, {"cvt ", 0}, |
| 91 |
{"prep", 0}, {NULL, 0} |
| 92 |
}; |
| 93 |
|
| 94 |
static int |
| 95 |
validate_name (char *fontname, int len) |
| 96 |
{ |
| 97 |
int i, count; |
| 98 |
char *p; |
| 99 |
static const char *badstrlist[] = { |
| 100 |
"-WIN-RKSJ-H", |
| 101 |
"-WINP-RKSJ-H", |
| 102 |
"-WING-RKSJ-H", |
| 103 |
"-90pv-RKSJ-H", |
| 104 |
NULL |
| 105 |
}; |
| 106 |
|
| 107 |
for (count = 0, i = 0; i < len; i++) { |
| 108 |
if (fontname[i] == 0) { |
| 109 |
memmove(fontname + i, fontname + i + 1, len - i); |
| 110 |
count++; |
| 111 |
len--; |
| 112 |
} |
| 113 |
} |
| 114 |
if (count > 0) { |
| 115 |
WARN("Removed %d null character(s) from fontname --> %s", |
| 116 |
count, fontname); |
| 117 |
} |
| 118 |
fontname[len] = '\0'; |
| 119 |
|
| 120 |
/* For some fonts that have bad PS name. ad hoc. remove me. */ |
| 121 |
for (i = 0; badstrlist[i] != NULL; i++) { |
| 122 |
p = strstr(fontname, badstrlist[i]); |
| 123 |
if (p && p > fontname) { |
| 124 |
WARN("Removed string \"%s\" from fontname \"%s\".", |
| 125 |
badstrlist[i], fontname); |
| 126 |
p[0] = '\0'; |
| 127 |
len = (int) (p - fontname); |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (len < 1) { |
| 133 |
WARN("No valid character found in fontname string."); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* We will follow the convension for finding ToUnicode CMap described in PDF |
| 142 |
* Reference 4th ed., page 432. The name of "ToCode" (not limited to Unicode |
| 143 |
* here) CMap is obtained by concatenating the registry, ordering, and the |
| 144 |
* name of encoding. |
| 145 |
* |
| 146 |
* UCSms-UCS4, UCSms-UCS2, UCS4 added... |
| 147 |
*/ |
| 148 |
|
| 149 |
#define WIN_UCS_INDEX_MAX 1 |
| 150 |
#define KNOWN_ENCODINGS_MAX 10 |
| 151 |
static struct |
| 152 |
{ |
| 153 |
unsigned short platform; |
| 154 |
unsigned short encoding; |
| 155 |
const char *pdfnames[5]; |
| 156 |
} known_encodings[] = { |
| 157 |
{TT_WIN, TT_WIN_UCS4, {"UCSms-UCS4", "UCSms-UCS2", "UCS4", "UCS2", NULL}}, |
| 158 |
{TT_WIN, TT_WIN_UNICODE, {"UCSms-UCS4", "UCSms-UCS2", "UCS4", "UCS2", NULL}}, |
| 159 |
{TT_WIN, TT_WIN_SJIS, {"90ms-RKSJ", NULL}}, |
| 160 |
{TT_WIN, TT_WIN_RPC, {"GBK-EUC", NULL}}, |
| 161 |
{TT_WIN, TT_WIN_BIG5, {"ETen-B5", NULL}}, |
| 162 |
{TT_WIN, TT_WIN_WANSUNG, {"KSCms-UHC", NULL}}, |
| 163 |
{TT_MAC, TT_MAC_JAPANESE, {"90pv-RKSJ", NULL}}, |
| 164 |
{TT_MAC, TT_MAC_TRADITIONAL_CHINESE, {"B5pc", NULL}}, |
| 165 |
{TT_MAC, TT_MAC_SIMPLIFIED_CHINESE, {"GBpc-EUC", NULL}}, |
| 166 |
{TT_MAC, TT_MAC_KOREAN, {"KSCpc-EUC", NULL}}, |
| 167 |
{0, 4, {"UCSms-UCS4", "UCSms-UCS2", "UCS4", "UCS2", NULL}}, |
| 168 |
{0, 0, {NULL}} |
| 169 |
}; |
| 170 |
|
| 171 |
static CMap * |
| 172 |
find_tocode_cmap (const char *reg, const char *ord, int select) |
| 173 |
{ |
| 174 |
int cmap_id = -1, i; |
| 175 |
char *cmap_name; |
| 176 |
const char *append; |
| 177 |
|
| 178 |
if (!reg || !ord || |
| 179 |
select < 0 || select > KNOWN_ENCODINGS_MAX) { |
| 180 |
WARN("Character set unknown."); |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
for (i = 0; cmap_id < 0 && i < 5; i++) { |
| 185 |
append = known_encodings[select].pdfnames[i]; |
| 186 |
if (!append) |
| 187 |
break; |
| 188 |
cmap_name = NEW(strlen(reg) + strlen(ord) + strlen(append) + 3, char); |
| 189 |
sprintf(cmap_name, "%s-%s-%s", reg, ord, append); |
| 190 |
cmap_id = CMap_cache_find(cmap_name); |
| 191 |
RELEASE(cmap_name); |
| 192 |
} |
| 193 |
if (cmap_id < 0) { |
| 194 |
WARN("Could not find CID-to-Code mapping for \"%s-%s\".", reg, ord); |
| 195 |
WARN("I tried to load (one of) the following file(s):"); |
| 196 |
for (i = 0; i < 5; i++) { |
| 197 |
append = known_encodings[select].pdfnames[i]; |
| 198 |
if (!append) |
| 199 |
break; |
| 200 |
MESG(" %s-%s-%s", reg, ord, append); |
| 201 |
} |
| 202 |
WARN("Please check if this file exists."); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
return CMap_cache_get(cmap_id); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/* |
| 211 |
* CIDFont glyph metrics: |
| 212 |
* Mostly same as add_CID[HV]Metrics in cidtype0.c. |
| 213 |
*/ |
| 214 |
#define PDFUNIT(v) ((double) (ROUND(1000.0*(v)/(g->emsize), 1))) |
| 215 |
|
| 216 |
static void |
| 217 |
add_TTCIDHMetrics (pdf_obj *fontdict, struct tt_glyphs *g, |
| 218 |
char *used_chars, unsigned char *cidtogidmap, unsigned short last_cid) |
| 219 |
{ |
| 220 |
int cid, start = 0, prev = 0; |
| 221 |
pdf_obj *w_array, *an_array = NULL; |
| 222 |
double dw; |
| 223 |
int empty = 1; |
| 224 |
|
| 225 |
w_array = pdf_new_array(); |
| 226 |
if (g->dw != 0 && g->dw <= g->emsize) { |
| 227 |
dw = PDFUNIT(g->dw); |
| 228 |
} else { |
| 229 |
dw = PDFUNIT(g->gd[0].advw); |
| 230 |
} |
| 231 |
for (cid = 0; cid <= last_cid; cid++) { |
| 232 |
USHORT idx, gid; |
| 233 |
double width; |
| 234 |
|
| 235 |
if (!is_used_char2(used_chars, cid)) |
| 236 |
continue; |
| 237 |
gid = (cidtogidmap) ? ((cidtogidmap[2*cid] << 8)|cidtogidmap[2*cid+1]) : cid; |
| 238 |
idx = tt_get_index(g, gid); |
| 239 |
if (cid != 0 && idx == 0) |
| 240 |
continue; |
| 241 |
width = PDFUNIT((g->gd)[idx].advw); |
| 242 |
if (width == dw) { |
| 243 |
if (an_array) { |
| 244 |
pdf_add_array(w_array, pdf_new_number(start)); |
| 245 |
pdf_add_array(w_array, an_array); |
| 246 |
an_array = NULL; |
| 247 |
empty = 0; |
| 248 |
} |
| 249 |
} else { |
| 250 |
if (cid != prev + 1) { |
| 251 |
if (an_array) { |
| 252 |
pdf_add_array(w_array, pdf_new_number(start)); |
| 253 |
pdf_add_array(w_array, an_array); |
| 254 |
an_array = NULL; |
| 255 |
empty = 0; |
| 256 |
} |
| 257 |
} |
| 258 |
if (an_array == NULL) { |
| 259 |
an_array = pdf_new_array(); |
| 260 |
start = cid; |
| 261 |
} |
| 262 |
pdf_add_array(an_array, pdf_new_number(width)); |
| 263 |
prev = cid; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if (an_array) { |
| 268 |
pdf_add_array(w_array, pdf_new_number(start)); |
| 269 |
pdf_add_array(w_array, an_array); |
| 270 |
empty = 0; |
| 271 |
} |
| 272 |
|
| 273 |
pdf_add_dict(fontdict, pdf_new_name("DW"), pdf_new_number(dw)); |
| 274 |
if (!empty) { |
| 275 |
pdf_add_dict(fontdict, pdf_new_name("W"), pdf_ref_obj(w_array)); |
| 276 |
} |
| 277 |
pdf_release_obj(w_array); |
| 278 |
|
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
static void |
| 283 |
add_TTCIDVMetrics (pdf_obj *fontdict, struct tt_glyphs *g, |
| 284 |
char *used_chars, unsigned short last_cid) |
| 285 |
{ |
| 286 |
pdf_obj *w2_array, *an_array = NULL; |
| 287 |
int cid; |
| 288 |
#if 0 |
| 289 |
int prev = 0, start = 0; |
| 290 |
#endif |
| 291 |
double defaultVertOriginY, defaultAdvanceHeight; |
| 292 |
int empty = 1; |
| 293 |
|
| 294 |
defaultVertOriginY = PDFUNIT(g->default_advh - g->default_tsb); |
| 295 |
defaultAdvanceHeight = PDFUNIT(g->default_advh); |
| 296 |
|
| 297 |
w2_array = pdf_new_array(); |
| 298 |
for (cid = 0; cid <= last_cid; cid++) { |
| 299 |
USHORT idx; |
| 300 |
#if 0 |
| 301 |
USHORT gid; |
| 302 |
#endif |
| 303 |
double vertOriginX, vertOriginY, advanceHeight; |
| 304 |
|
| 305 |
if (!is_used_char2(used_chars, cid)) |
| 306 |
continue; |
| 307 |
#if 0 |
| 308 |
gid = (cidtogidmap) ? ((cidtogidmap[2*cid] << 8)|cidtogidmap[2*cid+1]) : cid; |
| 309 |
#endif |
| 310 |
idx = tt_get_index(g, (USHORT)cid); |
| 311 |
if (cid != 0 && idx == 0) |
| 312 |
continue; |
| 313 |
advanceHeight = PDFUNIT(g->gd[idx].advh); |
| 314 |
vertOriginX = PDFUNIT(0.5*(g->gd[idx].advw)); |
| 315 |
vertOriginY = PDFUNIT(g->gd[idx].tsb + g->gd[idx].ury); |
| 316 |
#if 0 |
| 317 |
/* |
| 318 |
* c [w1_1y v_1x v_1y w1_2y v_2x v_2y ...] |
| 319 |
* Not working... Why? |
| 320 |
* Acrobat Reader: |
| 321 |
* Wrong rendering, interpretation of position vector is wrong. |
| 322 |
* Xpdf and gs: ignores W2? |
| 323 |
*/ |
| 324 |
if (vertOriginY == defaultVertOriginY && |
| 325 |
advanceHeight == defaultAdvanceHeight) { |
| 326 |
if (an_array) { |
| 327 |
pdf_add_array(w2_array, pdf_new_number(start)); |
| 328 |
pdf_add_array(w2_array, an_array); |
| 329 |
an_array = NULL; |
| 330 |
empty = 0; |
| 331 |
} |
| 332 |
} else { |
| 333 |
if (cid != prev + 1 && an_array) { |
| 334 |
pdf_add_array(w2_array, pdf_new_number(start)); |
| 335 |
pdf_add_array(w2_array, an_array); |
| 336 |
an_array = NULL; |
| 337 |
empty = 0; |
| 338 |
} |
| 339 |
if (an_array == NULL) { |
| 340 |
an_array = pdf_new_array(); |
| 341 |
start = cid; |
| 342 |
} |
| 343 |
pdf_add_array(an_array, pdf_new_number(-advanceHeight)); |
| 344 |
pdf_add_array(an_array, pdf_new_number(vertOriginX)); |
| 345 |
pdf_add_array(an_array, pdf_new_number(vertOriginY)); |
| 346 |
prev = cid; |
| 347 |
} |
| 348 |
#else |
| 349 |
/* |
| 350 |
* c_first c_last w1_y v_x v_y |
| 351 |
* This form may hit Acrobat's implementation limit of array element size, |
| 352 |
* 8192. AFPL GhostScript 8.11 stops with rangecheck error with this. |
| 353 |
* Maybe GS's bug? |
| 354 |
*/ |
| 355 |
if (vertOriginY != defaultVertOriginY || |
| 356 |
advanceHeight != defaultAdvanceHeight) { |
| 357 |
pdf_add_array(w2_array, pdf_new_number(cid)); |
| 358 |
pdf_add_array(w2_array, pdf_new_number(cid)); |
| 359 |
pdf_add_array(w2_array, pdf_new_number(-advanceHeight)); |
| 360 |
pdf_add_array(w2_array, pdf_new_number(vertOriginX)); |
| 361 |
pdf_add_array(w2_array, pdf_new_number(vertOriginY)); |
| 362 |
empty = 0; |
| 363 |
} |
| 364 |
#endif |
| 365 |
} |
| 366 |
|
| 367 |
#if 0 |
| 368 |
if (an_array) { |
| 369 |
pdf_add_array(w2_array, pdf_new_number(start)); |
| 370 |
pdf_add_array(w2_array, an_array); |
| 371 |
empty = 0; |
| 372 |
} |
| 373 |
#endif |
| 374 |
|
| 375 |
if (defaultVertOriginY != 880 || defaultAdvanceHeight != 1000) { |
| 376 |
an_array = pdf_new_array(); |
| 377 |
pdf_add_array(an_array, pdf_new_number(defaultVertOriginY)); |
| 378 |
pdf_add_array(an_array, pdf_new_number(-defaultAdvanceHeight)); |
| 379 |
pdf_add_dict(fontdict, pdf_new_name ("DW2"), an_array); |
| 380 |
} |
| 381 |
if (!empty) { |
| 382 |
pdf_add_dict(fontdict, |
| 383 |
pdf_new_name("W2"), |
| 384 |
pdf_ref_obj(w2_array)); |
| 385 |
} |
| 386 |
pdf_release_obj(w2_array); |
| 387 |
|
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* The following routine fixes few problems caused by vendor specific |
| 393 |
* Unicode mappings. |
| 394 |
*/ |
| 395 |
|
| 396 |
#define FIX_CJK_UNIOCDE_SYMBOLS 1 |
| 397 |
|
| 398 |
static unsigned short |
| 399 |
fix_CJK_symbols (unsigned short code) |
| 400 |
{ |
| 401 |
unsigned short alt_code; |
| 402 |
static struct |
| 403 |
{ |
| 404 |
unsigned short alt1; |
| 405 |
unsigned short alt2; |
| 406 |
} CJK_Uni_symbols[] = { |
| 407 |
/* |
| 408 |
* Microsoft/Apple Unicode mapping difference: |
| 409 |
* They are taken from SJIS-Unicode mapping difference but nearly |
| 410 |
* same thing might be applied to Chinese (e.g., Big5) too. |
| 411 |
*/ |
| 412 |
{0x2014, 0x2015}, /* EM DASH <-> HORIZONTAL BAR */ |
| 413 |
{0x2016, 0x2225}, /* DOUBLE VERTICAL LINE <-> PARALLEL TO */ |
| 414 |
{0x203E, 0xFFE3}, /* OVERLINE <-> FULLWIDTH MACRON */ |
| 415 |
{0x2026, 0x22EF}, /* HORIZONTAL ELLIPSIS <-> MIDLINE HORIZONTAL ELLIPSIS */ |
| 416 |
{0x2212, 0xFF0D}, /* MINUS SIGN <-> FULLWIDTH HYPHEN-MINUS */ |
| 417 |
{0x301C, 0xFF5E}, /* WAVE DASH <-> FULLWIDTH TILDE */ |
| 418 |
{0xFFE0, 0x00A2}, /* FULLWIDTH CENT SIGN <-> CENT SIGN */ |
| 419 |
{0xFFE1, 0x00A3}, /* FULLWIDTH POUND SIGN <-> POUND SIGN */ |
| 420 |
{0xFFE2, 0x00AC}, /* FULLWIDTH NOT SIGN <-> NOT SIGN */ |
| 421 |
{0xFFE5, 0x00A5}, /* FULLWIDTH YEN SIGN <-> YEN SIGN */ |
| 422 |
{0xFFFF, 0xFFFF}, /* EOD */ |
| 423 |
}; |
| 424 |
#define NUM_CJK_SYMBOLS (sizeof(CJK_Uni_symbols)/sizeof(CJK_Uni_symbols[0])) |
| 425 |
int i; |
| 426 |
|
| 427 |
alt_code = code; |
| 428 |
for (i = 0; i < NUM_CJK_SYMBOLS; i++) { |
| 429 |
if (CJK_Uni_symbols[i].alt1 == code) { |
| 430 |
alt_code = CJK_Uni_symbols[i].alt2; |
| 431 |
break; |
| 432 |
} else if (CJK_Uni_symbols[i].alt2 == code) { |
| 433 |
alt_code = CJK_Uni_symbols[i].alt1; |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return alt_code; |
| 439 |
} |
| 440 |
|
| 441 |
static int32_t |
| 442 |
cid_to_code (CMap *cmap, CID cid, int unicode_cmap, int32_t *puvs) |
| 443 |
{ |
| 444 |
unsigned char inbuf[2], outbuf[32]; |
| 445 |
int inbytesleft = 2, outbytesleft = 32; |
| 446 |
const unsigned char *p; |
| 447 |
unsigned char *q; |
| 448 |
|
| 449 |
*puvs = -1; |
| 450 |
|
| 451 |
if (!cmap) |
| 452 |
return cid; |
| 453 |
|
| 454 |
inbuf[0] = (cid >> 8) & 0xff; |
| 455 |
inbuf[1] = cid & 0xff; |
| 456 |
p = inbuf; q = outbuf; |
| 457 |
|
| 458 |
CMap_decode_char(cmap, &p, &inbytesleft, &q, &outbytesleft); |
| 459 |
|
| 460 |
if (inbytesleft != 0) |
| 461 |
return -1; |
| 462 |
else if (outbytesleft == 31) |
| 463 |
return (int32_t) outbuf[0]; |
| 464 |
else if (outbytesleft == 30) |
| 465 |
return (int32_t) (outbuf[0] << 8|outbuf[1]); |
| 466 |
else if (outbytesleft == 28 && !unicode_cmap) |
| 467 |
return (int32_t) (outbuf[0] << 24)|(outbuf[1] << 16)|(outbuf[2] << 8)|outbuf[3]; |
| 468 |
else if ((outbytesleft == 28 || outbytesleft == 26 || outbytesleft == 24) |
| 469 |
&& unicode_cmap) { |
| 470 |
/* We assume the output encoding is UTF-16. */ |
| 471 |
int32_t uc, uvs; |
| 472 |
const unsigned char *endptr; |
| 473 |
|
| 474 |
p = outbuf; |
| 475 |
endptr = p + 32 - outbytesleft; |
| 476 |
uc = UC_UTF16BE_decode_char(&p, endptr); |
| 477 |
if (p == endptr) |
| 478 |
return uc; /* single Unicode characters */ |
| 479 |
/* Check following Variation Selectors */ |
| 480 |
uvs = UC_UTF16BE_decode_char(&p, endptr); |
| 481 |
if (p == endptr) { |
| 482 |
if (uvs >= 0xfe00 && uvs <= 0xfe0f) { |
| 483 |
/* Standardized Variation Sequence */ |
| 484 |
*puvs = uvs; |
| 485 |
return uc; |
| 486 |
} else if (uvs >= 0xe0100 && uvs <= 0xe01ef) { |
| 487 |
/* Ideographic Variation Sequence */ |
| 488 |
*puvs = uvs; |
| 489 |
return uc; |
| 490 |
} else if (uvs == 0x3099 || uvs == 0x309a) { |
| 491 |
/* Combining Katakana-Hiragana (Semi-)Voiced Sound Mark */ |
| 492 |
*puvs = uvs; |
| 493 |
return uc; |
| 494 |
} |
| 495 |
} |
| 496 |
WARN("CID=%u mapped to non-single Unicode characters...", cid); |
| 497 |
return -1; |
| 498 |
} |
| 499 |
|
| 500 |
return -1; |
| 501 |
} |
| 502 |
|
| 503 |
static uint16_t |
| 504 |
cid_to_gid (CMap *cmap, CID cid) |
| 505 |
{ |
| 506 |
unsigned char inbuf[2], outbuf[2]; |
| 507 |
int inbytesleft = 2, outbytesleft = 2; |
| 508 |
const unsigned char *p; |
| 509 |
unsigned char *q; |
| 510 |
|
| 511 |
if (!cmap) |
| 512 |
return cid; |
| 513 |
|
| 514 |
inbuf[0] = (cid >> 8) & 0xff; |
| 515 |
inbuf[1] = cid & 0xff; |
| 516 |
p = inbuf; q = outbuf; |
| 517 |
|
| 518 |
CMap_decode_char(cmap, &p, &inbytesleft, &q, &outbytesleft); |
| 519 |
|
| 520 |
if (inbytesleft != 0 || outbytesleft != 0) |
| 521 |
return 0; |
| 522 |
|
| 523 |
return (uint16_t) (outbuf[0] << 8|outbuf[1]); |
| 524 |
} |
| 525 |
|
| 526 |
/* #define NO_GHOSTSCRIPT_BUG 1 */ |
| 527 |
|
| 528 |
int |
| 529 |
CIDFont_type2_dofont (pdf_font *font) |
| 530 |
{ |
| 531 |
pdf_obj *fontfile; |
| 532 |
sfnt *sfont; |
| 533 |
char *h_used_chars, *v_used_chars, *used_chars; |
| 534 |
struct tt_glyphs *glyphs; |
| 535 |
CMap *cmap = NULL; |
| 536 |
tt_cmap *ttcmap = NULL; |
| 537 |
tt_cmap *ttcmap_uvs = NULL; |
| 538 |
ULONG offset = 0; |
| 539 |
CID cid, last_cid; |
| 540 |
unsigned char *cidtogidmap; |
| 541 |
USHORT num_glyphs; |
| 542 |
enum { |
| 543 |
glyph_ordering, |
| 544 |
via_cid_to_code, |
| 545 |
via_cid_to_gid, |
| 546 |
} maptype = via_cid_to_code; |
| 547 |
int i, unicode_cmap = 0; |
| 548 |
FILE *fp = NULL; |
| 549 |
|
| 550 |
if (!font->reference) |
| 551 |
return 0; |
| 552 |
|
| 553 |
pdf_add_dict(font->resource, |
| 554 |
pdf_new_name("FontDescriptor"), pdf_ref_obj(font->descriptor)); |
| 555 |
|
| 556 |
if (font->flags & PDF_FONT_FLAG_BASEFONT) |
| 557 |
return 0; |
| 558 |
|
| 559 |
/* |
| 560 |
* CIDSystemInfo comes here since Supplement can be increased. |
| 561 |
*/ |
| 562 |
{ |
| 563 |
pdf_obj *tmp; |
| 564 |
|
| 565 |
tmp = pdf_new_dict (); |
| 566 |
pdf_add_dict(tmp, |
| 567 |
pdf_new_name("Registry"), |
| 568 |
pdf_new_string(font->cid.csi.registry, strlen(font->cid.csi.registry))); |
| 569 |
pdf_add_dict(tmp, |
| 570 |
pdf_new_name("Ordering"), |
| 571 |
pdf_new_string(font->cid.csi.ordering, strlen(font->cid.csi.ordering))); |
| 572 |
pdf_add_dict(tmp, |
| 573 |
pdf_new_name("Supplement"), |
| 574 |
pdf_new_number(font->cid.csi.supplement)); |
| 575 |
pdf_add_dict(font->resource, pdf_new_name("CIDSystemInfo"), tmp); |
| 576 |
} |
| 577 |
|
| 578 |
/* Quick exit for non-embedded & fixed-pitch font. */ |
| 579 |
if (!font->cid.options.embed && |
| 580 |
(opt_flags_cidfont & CIDFONT_FORCE_FIXEDPITCH)) { |
| 581 |
pdf_add_dict(font->resource, pdf_new_name("DW"), pdf_new_number(1000.0)); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
fp = DPXFOPEN(font->filename, DPX_RES_TYPE_TTFONT); |
| 586 |
if (!fp) { |
| 587 |
fp = DPXFOPEN(font->filename, DPX_RES_TYPE_DFONT); |
| 588 |
if (!fp) { |
| 589 |
WARN("Could not open TTF/dfont file: %s", font->filename); |
| 590 |
return -1; |
| 591 |
} |
| 592 |
sfont = dfont_open(fp, font->index); |
| 593 |
} else { |
| 594 |
sfont = sfnt_open(fp); |
| 595 |
} |
| 596 |
|
| 597 |
if (!sfont) { |
| 598 |
WARN("Could not open TTF file: %s", font->filename); |
| 599 |
DPXFCLOSE(fp); |
| 600 |
return -1; |
| 601 |
} |
| 602 |
|
| 603 |
switch (sfont->type) { |
| 604 |
case SFNT_TYPE_TTC: |
| 605 |
offset = ttc_read_offset(sfont, font->index); |
| 606 |
if (offset == 0) { |
| 607 |
WARN("Invalid TTC index in %s.", font->filename); |
| 608 |
sfnt_close(sfont); |
| 609 |
DPXFCLOSE(fp); |
| 610 |
return -1; |
| 611 |
} |
| 612 |
break; |
| 613 |
case SFNT_TYPE_TRUETYPE: |
| 614 |
if (font->index > 0) { |
| 615 |
WARN("Found TrueType font file while expecting TTC file (%s).", font->filename); |
| 616 |
sfnt_close(sfont); |
| 617 |
DPXFCLOSE(fp); |
| 618 |
return -1; |
| 619 |
} |
| 620 |
offset = 0; |
| 621 |
break; |
| 622 |
case SFNT_TYPE_DFONT: |
| 623 |
offset = sfont->offset; |
| 624 |
break; |
| 625 |
default: |
| 626 |
WARN("Not a TrueType/TTC font (%s)?", font->filename); |
| 627 |
sfnt_close(sfont); |
| 628 |
DPXFCLOSE(fp); |
| 629 |
return -1; |
| 630 |
break; |
| 631 |
} |
| 632 |
|
| 633 |
if (sfnt_read_table_directory(sfont, offset) < 0) { |
| 634 |
WARN("Could not read TrueType table directory (%s).", font->filename); |
| 635 |
sfnt_close(sfont); |
| 636 |
DPXFCLOSE(fp); |
| 637 |
return -1; |
| 638 |
} |
| 639 |
|
| 640 |
/* |
| 641 |
* Adobe-Identity means font's internal glyph ordering here. |
| 642 |
*/ |
| 643 |
if (!strcmp(font->cid.csi.registry, "Adobe") && |
| 644 |
!strcmp(font->cid.csi.ordering, "Identity")) { |
| 645 |
maptype = glyph_ordering; |
| 646 |
ttcmap = NULL; |
| 647 |
cmap = NULL; |
| 648 |
} else { |
| 649 |
if (font->cid.csi.registry && font->cid.csi.ordering) { |
| 650 |
char *cmap_name; |
| 651 |
size_t len; |
| 652 |
int cmap_id; |
| 653 |
|
| 654 |
len = strlen(font->cid.csi.registry) + strlen(font->cid.csi.ordering); |
| 655 |
len += strlen(font->fontname); |
| 656 |
len += 3; |
| 657 |
cmap_name = NEW(len+1, char); |
| 658 |
snprintf(cmap_name, len + 1, "%s-%s-%s", |
| 659 |
font->cid.csi.registry, font->cid.csi.ordering, font->fontname); |
| 660 |
cmap_id = CMap_cache_find(cmap_name); |
| 661 |
RELEASE(cmap_name); |
| 662 |
if (cmap_id >= 0) { |
| 663 |
cmap = CMap_cache_get(cmap_id); |
| 664 |
ttcmap = NULL; |
| 665 |
maptype = via_cid_to_gid; |
| 666 |
} |
| 667 |
} |
| 668 |
if (maptype != via_cid_to_gid) { |
| 669 |
maptype = via_cid_to_code; |
| 670 |
/* |
| 671 |
* This part contains a bug. It may choose SJIS encoding TrueType cmap |
| 672 |
* table for Adobe-GB1. |
| 673 |
*/ |
| 674 |
for (i = 0; i <= KNOWN_ENCODINGS_MAX; i++) { |
| 675 |
ttcmap = tt_cmap_read(sfont, |
| 676 |
known_encodings[i].platform, |
| 677 |
known_encodings[i].encoding); |
| 678 |
if (ttcmap) |
| 679 |
break; |
| 680 |
} |
| 681 |
if (!ttcmap) { |
| 682 |
WARN("No usable TrueType cmap table found for font \"%s\".", font->filename); |
| 683 |
WARN("CID character collection for this font is set to \"%s-%s\"", |
| 684 |
font->cid.csi.registry, font->cid.csi.ordering); |
| 685 |
WARN("Cannot continue without this..."); |
| 686 |
sfnt_close(sfont); |
| 687 |
DPXFCLOSE(fp); |
| 688 |
return -1; |
| 689 |
} else if (i <= WIN_UCS_INDEX_MAX) { |
| 690 |
unicode_cmap = 1; |
| 691 |
/* Unicode Variation Sequences */ |
| 692 |
ttcmap_uvs = tt_cmap_read(sfont, 0, 5); |
| 693 |
} else { |
| 694 |
unicode_cmap = 0; |
| 695 |
} |
| 696 |
|
| 697 |
if (!strcmp(font->cid.csi.ordering, "UCS") && i <= WIN_UCS_INDEX_MAX) { |
| 698 |
cmap = NULL; |
| 699 |
} else { |
| 700 |
cmap = find_tocode_cmap(font->cid.csi.registry, font->cid.csi.ordering, i); |
| 701 |
if (!cmap) { |
| 702 |
sfnt_close(sfont); |
| 703 |
DPXFCLOSE(fp); |
| 704 |
return -1; |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
glyphs = tt_build_init(); |
| 711 |
|
| 712 |
last_cid = 0; |
| 713 |
num_glyphs = 1; /* .notdef */ |
| 714 |
used_chars = h_used_chars = v_used_chars = NULL; |
| 715 |
{ |
| 716 |
int c; |
| 717 |
|
| 718 |
h_used_chars = font->usedchars; |
| 719 |
v_used_chars = font->cid.usedchars_v; |
| 720 |
|
| 721 |
ASSERT(h_used_chars || v_used_chars); |
| 722 |
|
| 723 |
/* |
| 724 |
* Quick check of max CID. |
| 725 |
*/ |
| 726 |
c = 0; |
| 727 |
for (i = 8191; i >= 0; i--) { |
| 728 |
if (h_used_chars && h_used_chars[i] != 0) { |
| 729 |
last_cid = i * 8 + 7; |
| 730 |
c = h_used_chars[i]; |
| 731 |
break; |
| 732 |
} |
| 733 |
} |
| 734 |
for (i = 8191; i >= 0; i--) { |
| 735 |
if (v_used_chars && v_used_chars[i] != 0) { |
| 736 |
if (i * 8 + 7 >= last_cid) { |
| 737 |
c = (i * 8 + 7 > last_cid) ? (v_used_chars[i]) : (c | v_used_chars[i]); |
| 738 |
last_cid = i * 8 + 7; |
| 739 |
break; |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
if (last_cid > 0) { |
| 744 |
for (i = 0; i < 8; i++) { |
| 745 |
if ((c >> i) & 1) |
| 746 |
break; |
| 747 |
last_cid--; |
| 748 |
} |
| 749 |
} |
| 750 |
ASSERT(last_cid < 0xFFFFu); |
| 751 |
} |
| 752 |
|
| 753 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 754 |
cidtogidmap = NULL; |
| 755 |
#else |
| 756 |
cidtogidmap = NEW((last_cid + 1) * 2, unsigned char); |
| 757 |
memset(cidtogidmap, 0, (last_cid + 1) * 2); |
| 758 |
#endif /* !NO_GHOSTSCRIPT_BUG */ |
| 759 |
|
| 760 |
/* |
| 761 |
* Map CIDs to GIDs. |
| 762 |
* Horizontal and vertical used_chars are merged. |
| 763 |
*/ |
| 764 |
|
| 765 |
/* |
| 766 |
* Horizontal |
| 767 |
*/ |
| 768 |
if (h_used_chars) { |
| 769 |
used_chars = h_used_chars; |
| 770 |
for (cid = 1; cid <= last_cid; cid++) { |
| 771 |
int32_t code, uvs = 0; |
| 772 |
uint16_t gid = 0; |
| 773 |
|
| 774 |
if (!is_used_char2(h_used_chars, cid)) |
| 775 |
continue; |
| 776 |
|
| 777 |
switch (maptype) { |
| 778 |
case glyph_ordering: |
| 779 |
gid = cid; |
| 780 |
code = cid; |
| 781 |
break; |
| 782 |
case via_cid_to_gid: |
| 783 |
gid = cid_to_gid(cmap, cid); |
| 784 |
code = cid; |
| 785 |
break; |
| 786 |
case via_cid_to_code: |
| 787 |
code = cid_to_code(cmap, cid, unicode_cmap, &uvs); |
| 788 |
if (code < 0) { |
| 789 |
WARN("Unable to map CID to code: CID=%u", cid); |
| 790 |
} else { |
| 791 |
if (ttcmap_uvs && uvs > 0) { |
| 792 |
gid = tt_cmap_uvs_lookup(ttcmap_uvs, ttcmap, code, uvs); |
| 793 |
} |
| 794 |
if (gid == 0 && uvs >= 0xfe00 && uvs <= 0xfe0f) { |
| 795 |
/* Standardized Variation Sequence */ |
| 796 |
/* Combine CJK compatibility ideograph */ |
| 797 |
int32_t code2 = UC_Combine_CJK_compatibility_ideograph(code, uvs); |
| 798 |
if (code2 > 0) |
| 799 |
gid = tt_cmap_lookup(ttcmap, code2); |
| 800 |
} |
| 801 |
if (gid == 0) { |
| 802 |
gid = tt_cmap_lookup(ttcmap, code); |
| 803 |
if (gid > 0 && uvs > 0) { |
| 804 |
WARN("Ignored Variation Selector: CID=%u mapped to U+%04X U+%04X", cid, code, uvs); |
| 805 |
} |
| 806 |
} |
| 807 |
#ifdef FIX_CJK_UNIOCDE_SYMBOLS |
| 808 |
if (gid == 0 && unicode_cmap && code <= 0xFFFF) { |
| 809 |
int alt_code; |
| 810 |
|
| 811 |
alt_code = fix_CJK_symbols((unsigned short)code); |
| 812 |
if (alt_code != code) { |
| 813 |
gid = tt_cmap_lookup(ttcmap, alt_code); |
| 814 |
if (gid != 0) { |
| 815 |
WARN("Unicode char U+%04x replaced with U+%04x.", code, alt_code); |
| 816 |
} |
| 817 |
} |
| 818 |
} |
| 819 |
#endif /* FIX_CJK_UNIOCDE_SYMBOLS */ |
| 820 |
} |
| 821 |
break; |
| 822 |
} |
| 823 |
if (gid == 0) { |
| 824 |
if (code >= 0) |
| 825 |
WARN("Glyph missing in font. (CID=%u, code=0x%04x)", cid, code); |
| 826 |
} |
| 827 |
|
| 828 |
/* TODO: duplicated glyph */ |
| 829 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 830 |
gid = tt_add_glyph(glyphs, gid, cid); |
| 831 |
#else |
| 832 |
gid = tt_add_glyph(glyphs, gid, num_glyphs); |
| 833 |
cidtogidmap[2*cid ] = gid >> 8; |
| 834 |
cidtogidmap[2*cid+1] = gid & 0xff; |
| 835 |
#endif /* !NO_GHOSTSCRIPT_BUG */ |
| 836 |
|
| 837 |
num_glyphs++; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
/* |
| 842 |
* Vertical |
| 843 |
*/ |
| 844 |
if (v_used_chars) { |
| 845 |
otl_gsub *gsub_list = NULL; |
| 846 |
|
| 847 |
/* |
| 848 |
* Require `vrt2' or `vert'. |
| 849 |
*/ |
| 850 |
if (maptype != via_cid_to_code) { |
| 851 |
gsub_list = NULL; |
| 852 |
} else { |
| 853 |
gsub_list = otl_gsub_new(); |
| 854 |
if (otl_gsub_add_feat(gsub_list, "*", "*", "vrt2", sfont) < 0) { |
| 855 |
if (otl_gsub_add_feat(gsub_list, "*", "*", "vert", sfont) < 0) { |
| 856 |
WARN("GSUB feature vrt2/vert not found."); |
| 857 |
otl_gsub_release(gsub_list); |
| 858 |
gsub_list = NULL; |
| 859 |
} else { |
| 860 |
otl_gsub_select(gsub_list, "*", "*", "vert"); |
| 861 |
} |
| 862 |
} else { |
| 863 |
otl_gsub_select(gsub_list, "*", "*", "vrt2"); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
for (cid = 1; cid <= last_cid; cid++) { |
| 868 |
int32_t code, uvs = 0; |
| 869 |
uint16_t gid = 0; |
| 870 |
|
| 871 |
if (!is_used_char2(v_used_chars, cid)) |
| 872 |
continue; |
| 873 |
|
| 874 |
/* There may be conflict of horizontal and vertical glyphs |
| 875 |
* when font is used with /UCS. However, we simply ignore |
| 876 |
* that... |
| 877 |
*/ |
| 878 |
if (h_used_chars && is_used_char2(h_used_chars, cid)) { |
| 879 |
continue; |
| 880 |
} |
| 881 |
|
| 882 |
switch (maptype) { |
| 883 |
case glyph_ordering: |
| 884 |
gid = cid; |
| 885 |
code = cid; |
| 886 |
break; |
| 887 |
case via_cid_to_gid: |
| 888 |
gid = cid_to_gid(cmap, cid); |
| 889 |
code = cid; |
| 890 |
break; |
| 891 |
case via_cid_to_code: |
| 892 |
code = cid_to_code(cmap, cid, unicode_cmap, &uvs); |
| 893 |
if (code < 0) { |
| 894 |
WARN("Unable to map CID to code: CID=%u", cid); |
| 895 |
} else { |
| 896 |
if (ttcmap_uvs && uvs > 0) { |
| 897 |
gid = tt_cmap_uvs_lookup(ttcmap_uvs, ttcmap, code, uvs); |
| 898 |
} |
| 899 |
if (gid == 0 && uvs >= 0xfe00 && uvs <= 0xfe0f) { |
| 900 |
/* Standardized Variation Sequence */ |
| 901 |
/* Combine CJK compatibility ideograph */ |
| 902 |
int32_t code2 = UC_Combine_CJK_compatibility_ideograph(code, uvs); |
| 903 |
if (code2 > 0) |
| 904 |
gid = tt_cmap_lookup(ttcmap, code2); |
| 905 |
} |
| 906 |
if (gid == 0) { |
| 907 |
gid = tt_cmap_lookup(ttcmap, code); |
| 908 |
if (gid > 0 && uvs > 0) { |
| 909 |
WARN("Ignored Variation Selector: CID=%u mapped to U+%04X U+%04X", cid, code, uvs); |
| 910 |
} |
| 911 |
} |
| 912 |
#ifdef FIX_CJK_UNIOCDE_SYMBOLS |
| 913 |
if (gid == 0 && unicode_cmap && code <= 0xFFFF) { |
| 914 |
int alt_code; |
| 915 |
|
| 916 |
alt_code = fix_CJK_symbols((unsigned short)code); |
| 917 |
if (alt_code != code) { |
| 918 |
gid = tt_cmap_lookup(ttcmap, alt_code); |
| 919 |
if (gid != 0) { |
| 920 |
WARN("Unicode char U+%04x replaced with U+%04x.", code, alt_code); |
| 921 |
} |
| 922 |
} |
| 923 |
} |
| 924 |
#endif /* FIX_CJK_UNIOCDE_SYMBOLS */ |
| 925 |
} |
| 926 |
break; |
| 927 |
} |
| 928 |
if (gid == 0) { |
| 929 |
if (code >= 0) |
| 930 |
WARN("Glyph missing in font. (CID=%u, code=0x%04x)", cid, code); |
| 931 |
} else if (gsub_list) { |
| 932 |
otl_gsub_apply(gsub_list, &gid); |
| 933 |
} |
| 934 |
|
| 935 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 936 |
gid = tt_add_glyph(glyphs, gid, cid); |
| 937 |
#else |
| 938 |
gid = tt_add_glyph(glyphs, gid, num_glyphs); |
| 939 |
cidtogidmap[2*cid ] = gid >> 8; |
| 940 |
cidtogidmap[2*cid+1] = gid & 0xff; |
| 941 |
#endif /* !NO_GHOSTSCRIPT_BUG */ |
| 942 |
|
| 943 |
if (used_chars) /* merge vertical used_chars to horizontal */ |
| 944 |
add_to_used_chars2(used_chars, cid); |
| 945 |
|
| 946 |
num_glyphs++; |
| 947 |
} |
| 948 |
|
| 949 |
if (gsub_list) |
| 950 |
otl_gsub_release(gsub_list); |
| 951 |
|
| 952 |
if (!used_chars) /* We have no horizontal. */ |
| 953 |
used_chars = v_used_chars; |
| 954 |
} |
| 955 |
|
| 956 |
ASSERT(used_chars); |
| 957 |
|
| 958 |
tt_cmap_release(ttcmap); |
| 959 |
tt_cmap_release(ttcmap_uvs); |
| 960 |
|
| 961 |
if (font->cid.options.embed) { |
| 962 |
if (tt_build_tables(sfont, glyphs) < 0) { |
| 963 |
WARN("Could not created FontFile stream."); |
| 964 |
if (cidtogidmap) |
| 965 |
RELEASE(cidtogidmap); |
| 966 |
sfnt_close(sfont); |
| 967 |
DPXFCLOSE(fp); |
| 968 |
return -1; |
| 969 |
} |
| 970 |
if (dpx_conf.verbose_level > 1) |
| 971 |
MESG("[%u glyphs (Max CID: %u)]", glyphs->num_glyphs, last_cid); |
| 972 |
} else { |
| 973 |
if (tt_get_metrics(sfont, glyphs) < 0) { |
| 974 |
WARN("Reading glyph metrics failed..."); |
| 975 |
if (cidtogidmap) |
| 976 |
RELEASE(cidtogidmap); |
| 977 |
sfnt_close(sfont); |
| 978 |
DPXFCLOSE(fp); |
| 979 |
return -1; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
/* |
| 984 |
* DW, W, DW2, and W2 |
| 985 |
*/ |
| 986 |
if (opt_flags_cidfont & CIDFONT_FORCE_FIXEDPITCH) { |
| 987 |
pdf_add_dict(font->resource, pdf_new_name("DW"), pdf_new_number(1000.0)); |
| 988 |
} else { |
| 989 |
add_TTCIDHMetrics(font->resource, glyphs, used_chars, cidtogidmap, last_cid); |
| 990 |
if (font->cid.need_vmetrics) |
| 991 |
add_TTCIDVMetrics(font->resource, glyphs, used_chars, last_cid); |
| 992 |
} |
| 993 |
|
| 994 |
/* CIDSet |
| 995 |
* NOTE: All glyphs including component glyph and dummy glyph must be |
| 996 |
* listed in CIDSet. However, .notdef glyph should be ommitted. |
| 997 |
*/ |
| 998 |
if (pdf_check_version(2, 0) < 0) { |
| 999 |
pdf_obj *cidset; |
| 1000 |
char *cidset_data; |
| 1001 |
|
| 1002 |
cidset_data = NEW(glyphs->last_gid/8 + 1, char); |
| 1003 |
memset(cidset_data, 0, glyphs->last_gid/8 + 1); |
| 1004 |
for (i = 1; i <= glyphs->last_gid; i++) |
| 1005 |
cidset_data[i/8] |= (1 << (7 - i % 8)); |
| 1006 |
cidset = pdf_new_stream(STREAM_COMPRESS); |
| 1007 |
pdf_add_stream(cidset, cidset_data, glyphs->last_gid/8 + 1); |
| 1008 |
RELEASE(cidset_data); |
| 1009 |
pdf_add_dict(font->descriptor, |
| 1010 |
pdf_new_name("CIDSet"), pdf_ref_obj(cidset)); |
| 1011 |
pdf_release_obj(cidset); |
| 1012 |
} |
| 1013 |
|
| 1014 |
tt_build_finish(glyphs); |
| 1015 |
|
| 1016 |
/* Finish here if not embedded. */ |
| 1017 |
if (!font->cid.options.embed) { |
| 1018 |
if (cidtogidmap) |
| 1019 |
RELEASE(cidtogidmap); |
| 1020 |
sfnt_close(sfont); |
| 1021 |
DPXFCLOSE(fp); |
| 1022 |
|
| 1023 |
return 0; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/* Create font file */ |
| 1027 |
for (i = 0; required_table[i].name; i++) { |
| 1028 |
if (sfnt_require_table(sfont, |
| 1029 |
required_table[i].name, |
| 1030 |
required_table[i].must_exist) < 0) { |
| 1031 |
WARN("Some required TrueType table (%s) does not exist.", required_table[i].name); |
| 1032 |
if (cidtogidmap) |
| 1033 |
RELEASE(cidtogidmap); |
| 1034 |
sfnt_close(sfont); |
| 1035 |
DPXFCLOSE(fp); |
| 1036 |
return -1; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
/* |
| 1041 |
* FontFile2 |
| 1042 |
*/ |
| 1043 |
fontfile = sfnt_create_FontFile_stream(sfont); |
| 1044 |
|
| 1045 |
sfnt_close(sfont); |
| 1046 |
DPXFCLOSE(fp); |
| 1047 |
|
| 1048 |
if (!fontfile) { |
| 1049 |
WARN("Could not created FontFile stream for \"%s\".", font->filename); |
| 1050 |
if (cidtogidmap) |
| 1051 |
RELEASE(cidtogidmap); |
| 1052 |
return -1; |
| 1053 |
} |
| 1054 |
if (dpx_conf.verbose_level > 1) { |
| 1055 |
MESG("[%ld bytes]", pdf_stream_length(fontfile)); |
| 1056 |
} |
| 1057 |
|
| 1058 |
pdf_add_dict(font->descriptor, pdf_new_name("FontFile2"), pdf_ref_obj (fontfile)); |
| 1059 |
pdf_release_obj(fontfile); |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* CIDToGIDMap |
| 1063 |
* Adobe's PDF Reference had been describing it as "optional" and |
| 1064 |
* default value as "Identity". However, ISO 32000-1 requires it |
| 1065 |
* for Type 2 CIDFonts with embedded font programs. |
| 1066 |
*/ |
| 1067 |
if (!cidtogidmap) { |
| 1068 |
pdf_add_dict(font->resource, pdf_new_name("CIDToGIDMap"), pdf_new_name("Identity")); |
| 1069 |
} else { |
| 1070 |
pdf_obj *c2gmstream; |
| 1071 |
|
| 1072 |
c2gmstream = pdf_new_stream(STREAM_COMPRESS); |
| 1073 |
pdf_add_stream(c2gmstream, cidtogidmap, (last_cid + 1) * 2); |
| 1074 |
pdf_add_dict(font->resource, pdf_new_name("CIDToGIDMap"), pdf_ref_obj (c2gmstream)); |
| 1075 |
pdf_release_obj(c2gmstream); |
| 1076 |
RELEASE(cidtogidmap); |
| 1077 |
} |
| 1078 |
|
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
|
| 1082 |
int |
| 1083 |
CIDFont_type2_open (pdf_font *font, const char *name, int index, cid_opt *opt) |
| 1084 |
{ |
| 1085 |
int error; |
| 1086 |
char *fontname; |
| 1087 |
sfnt *sfont; |
| 1088 |
ULONG offset = 0; |
| 1089 |
FILE *fp = NULL; |
| 1090 |
|
| 1091 |
ASSERT(font && opt); |
| 1092 |
|
| 1093 |
fp = DPXFOPEN(name, DPX_RES_TYPE_TTFONT); |
| 1094 |
if (!fp) { |
| 1095 |
fp = DPXFOPEN(name, DPX_RES_TYPE_DFONT); |
| 1096 |
if (!fp) return -1; |
| 1097 |
sfont = dfont_open(fp, index); |
| 1098 |
} else { |
| 1099 |
sfont = sfnt_open(fp); |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (!sfont) { |
| 1103 |
DPXFCLOSE(fp); |
| 1104 |
return -1; |
| 1105 |
} |
| 1106 |
|
| 1107 |
switch (sfont->type) { |
| 1108 |
case SFNT_TYPE_TTC: |
| 1109 |
offset = ttc_read_offset(sfont, index); |
| 1110 |
break; |
| 1111 |
case SFNT_TYPE_TRUETYPE: |
| 1112 |
if (index > 0) { |
| 1113 |
WARN("Invalid TTC index (not TTC font): %s", name); |
| 1114 |
sfnt_close(sfont); |
| 1115 |
DPXFCLOSE(fp); |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
offset = 0; |
| 1119 |
break; |
| 1120 |
case SFNT_TYPE_DFONT: |
| 1121 |
offset = sfont->offset; |
| 1122 |
break; |
| 1123 |
default: |
| 1124 |
sfnt_close(sfont); |
| 1125 |
DPXFCLOSE(fp); |
| 1126 |
return -1; |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (sfnt_read_table_directory(sfont, offset) < 0) { |
| 1130 |
WARN("Reading TrueType table directory failed: %s", name); |
| 1131 |
sfnt_close(sfont); |
| 1132 |
DPXFCLOSE(fp); |
| 1133 |
return -1; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/* Ignore TrueType Collection with CFF table. */ |
| 1137 |
if (sfont->type == SFNT_TYPE_TTC && sfnt_find_table_pos(sfont, "CFF ")) { |
| 1138 |
sfnt_close(sfont); |
| 1139 |
DPXFCLOSE(fp); |
| 1140 |
return -1; |
| 1141 |
} |
| 1142 |
|
| 1143 |
{ |
| 1144 |
char *shortname; |
| 1145 |
int namelen; |
| 1146 |
|
| 1147 |
/* MAC-ROMAN-EN-POSTSCRIPT or WIN-UNICODE-EN(US)-POSTSCRIPT */ |
| 1148 |
shortname = NEW(PDF_NAME_LEN_MAX, char); |
| 1149 |
namelen = tt_get_ps_fontname(sfont, shortname, PDF_NAME_LEN_MAX); |
| 1150 |
if (namelen == 0) { |
| 1151 |
memset(shortname, 0, PDF_NAME_LEN_MAX); |
| 1152 |
strncpy(shortname, name, PDF_NAME_LEN_MAX); |
| 1153 |
namelen = strlen(shortname); |
| 1154 |
} |
| 1155 |
error = validate_name(shortname, namelen); /* for SJIS, UTF-16, ... string */ |
| 1156 |
if (error) { |
| 1157 |
RELEASE(shortname); |
| 1158 |
sfnt_close(sfont); |
| 1159 |
DPXFCLOSE(fp); |
| 1160 |
return -1; |
| 1161 |
} |
| 1162 |
/* |
| 1163 |
* Strlen works, after validate_named string. |
| 1164 |
* Mangled name requires more 7 bytes. |
| 1165 |
* Style requires more 11 bytes. |
| 1166 |
*/ |
| 1167 |
fontname = NEW(strlen(shortname)+19, char); |
| 1168 |
strcpy(fontname, shortname); |
| 1169 |
RELEASE(shortname); |
| 1170 |
} |
| 1171 |
|
| 1172 |
if (opt->embed && opt->style != FONT_STYLE_NONE) { |
| 1173 |
WARN("Embedding disabled due to style option for %s.", name); |
| 1174 |
opt->embed = 0; |
| 1175 |
} |
| 1176 |
switch (opt->style) { |
| 1177 |
case FONT_STYLE_BOLD: |
| 1178 |
strcat(fontname, ",Bold"); |
| 1179 |
break; |
| 1180 |
case FONT_STYLE_ITALIC: |
| 1181 |
strcat(fontname, ",Italic"); |
| 1182 |
break; |
| 1183 |
case FONT_STYLE_BOLDITALIC: |
| 1184 |
strcat(fontname, ",BoldItalic"); |
| 1185 |
break; |
| 1186 |
} |
| 1187 |
/* |
| 1188 |
* CIDSystemInfo is determined from CMap or from map record option. |
| 1189 |
*/ |
| 1190 |
font->fontname = fontname; |
| 1191 |
font->subtype = PDF_FONT_FONTTYPE_CIDTYPE2; |
| 1192 |
if (opt->csi.registry && opt->csi.ordering) { |
| 1193 |
font->cid.csi.registry = NEW(strlen(opt->csi.registry)+1, char); |
| 1194 |
strcpy(font->cid.csi.registry, opt->csi.registry); |
| 1195 |
font->cid.csi.ordering = NEW(strlen(opt->csi.ordering)+1, char); |
| 1196 |
strcpy(font->cid.csi.ordering, opt->csi.ordering); |
| 1197 |
font->cid.csi.supplement = opt->csi.supplement; |
| 1198 |
} |
| 1199 |
|
| 1200 |
font->resource = pdf_new_dict(); |
| 1201 |
pdf_add_dict(font->resource, pdf_new_name("Type"), pdf_new_name("Font")); |
| 1202 |
pdf_add_dict(font->resource, pdf_new_name("Subtype"), pdf_new_name("CIDFontType2")); |
| 1203 |
|
| 1204 |
font->descriptor = tt_get_fontdesc(sfont, &(opt->embed), opt->stemv, 0, name); |
| 1205 |
if (!font->descriptor) { |
| 1206 |
WARN("Could not obtain necessary font info: %s", name); |
| 1207 |
sfnt_close(sfont); |
| 1208 |
DPXFCLOSE(fp); |
| 1209 |
return -1; |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (opt->embed) { |
| 1213 |
char *tmp; |
| 1214 |
|
| 1215 |
pdf_font_make_uniqueTag(font->uniqueID); |
| 1216 |
tmp = NEW(strlen(fontname)+8, char); |
| 1217 |
sprintf(tmp, "%s+%s", font->uniqueID, font->fontname); |
| 1218 |
pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(tmp)); |
| 1219 |
pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(tmp)); |
| 1220 |
RELEASE(tmp); |
| 1221 |
} else { |
| 1222 |
pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(font->fontname)); |
| 1223 |
pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(font->fontname)); |
| 1224 |
} |
| 1225 |
|
| 1226 |
sfnt_close(sfont); |
| 1227 |
if (fp) |
| 1228 |
DPXFCLOSE(fp); |
| 1229 |
|
| 1230 |
/* |
| 1231 |
* Don't write fontdict here. |
| 1232 |
* /Supplement in /CIDSystemInfo may change. |
| 1233 |
*/ |
| 1234 |
|
| 1235 |
return 0; |
| 1236 |
} |