| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2021 by Jin-Hwan Cho, Matthias Franz, and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include "system.h" |
| 30 |
#include "error.h" |
| 31 |
#include "mem.h" |
| 32 |
|
| 33 |
#include "dpxconf.h" |
| 34 |
#include "dpxcrypt.h" |
| 35 |
#include "dpxfile.h" |
| 36 |
#include "dpxutil.h" |
| 37 |
|
| 38 |
#include "pdfobj.h" |
| 39 |
|
| 40 |
#include "agl.h" |
| 41 |
#include "pdfencoding.h" |
| 42 |
#include "cmap.h" |
| 43 |
#include "unicode.h" |
| 44 |
|
| 45 |
#include "type1.h" |
| 46 |
#include "type1c.h" |
| 47 |
#include "truetype.h" |
| 48 |
|
| 49 |
#include "pkfont.h" |
| 50 |
|
| 51 |
#include "cidtype0.h" |
| 52 |
#include "type0.h" |
| 53 |
#include "tt_cmap.h" |
| 54 |
|
| 55 |
#include "dvipdfmx.h" |
| 56 |
|
| 57 |
#include "pdffont.h" |
| 58 |
|
| 59 |
#define MREC_HAS_TOUNICODE(m) ((m) && (m)->opt.tounicode) |
| 60 |
|
| 61 |
void |
| 62 |
pdf_font_set_dpi (int font_dpi) |
| 63 |
{ |
| 64 |
PKFont_set_dpi(font_dpi); |
| 65 |
} |
| 66 |
|
| 67 |
static union { |
| 68 |
char p[sizeof(int)]; |
| 69 |
int* i; |
| 70 |
} unique_tag_count; |
| 71 |
|
| 72 |
/* This function used to be implemented as |
| 73 |
* |
| 74 |
* for (i = 0; i < 6; i++) { |
| 75 |
* ch = rand() % 26; |
| 76 |
* tag[i] = ch + 'A'; |
| 77 |
* } |
| 78 |
* tag[6] = '\0'; |
| 79 |
* |
| 80 |
* but this meant that the tag would change on every run, producing a |
| 81 |
* non-deterministic PDF file. You could work around this by setting |
| 82 |
* `SOURCE_DATE_EPOCH` in the environment (since the current time is used to |
| 83 |
* seed `rand`), but that requires extra effort. Instead, we use an MD5 hash of |
| 84 |
* the input (dvi) filename, the output (pdf) filename, and a counter that |
| 85 |
* increments on each call to this function. This produces a deterministic tag |
| 86 |
* for each document, provided that the input filename, the output filename, and |
| 87 |
* the order/number of fonts remains the same. |
| 88 |
* |
| 89 |
* Why do we need this function in the first place? Well, since we are |
| 90 |
* subsetting the fonts, this means that the "LM Roman 10" font in one document |
| 91 |
* will not be the same as the "LM Roman 10" font in another document. This can |
| 92 |
* cause problems when older/buggy PDF processors merge or embed multiple |
| 93 |
* documents, since it's invalid to have two fonts with the same name and |
| 94 |
* neither font is a strict subset/superset of the other. |
| 95 |
* |
| 96 |
* pdfTeX and LuaTeX solve this by hashing over the subsetting hash table, but |
| 97 |
* this only works there since they only generate the PDF font name _after_ |
| 98 |
* creating the subset. (x)dvipdfmx generates the PDF font name as (almost) the |
| 99 |
* very first step when including a font, so we couldn't use this method without |
| 100 |
* extensive refactoring. |
| 101 |
* |
| 102 |
* The pdfTeX and LuaTeX methods guarantee that multiple incompatible subsets |
| 103 |
* will never have the same name (barring hash collisions), and the prior `rand` |
| 104 |
* method had the same guarantee (barring an _extremely_ unlikely RNG |
| 105 |
* collision). This new method isn't quite as good since if the input and output |
| 106 |
* are both pipes, then both filenames will be `NULL` and the tag will only |
| 107 |
* depend on the counter. But I think that most PDF processors these days will |
| 108 |
* properly check for font name collisions, so this is probably good enough. |
| 109 |
*/ |
| 110 |
void |
| 111 |
pdf_font_make_uniqueTag (char *tag) |
| 112 |
{ |
| 113 |
MD5_CONTEXT state; |
| 114 |
unsigned char digest[16]; |
| 115 |
int i, ch; |
| 116 |
|
| 117 |
unique_tag_count.i++; |
| 118 |
|
| 119 |
MD5_init(&state); |
| 120 |
if (dvi_filename) |
| 121 |
MD5_write(&state, dvi_filename, strlen(dvi_filename)); |
| 122 |
if (pdf_filename) |
| 123 |
MD5_write(&state, pdf_filename, strlen(pdf_filename)); |
| 124 |
MD5_write(&state, unique_tag_count.p, sizeof(unique_tag_count)); |
| 125 |
MD5_final(digest, &state); |
| 126 |
|
| 127 |
for (i = 0; i < 6; i++) { |
| 128 |
ch = digest[i] % 26; |
| 129 |
tag[i] = ch + 'A'; |
| 130 |
} |
| 131 |
tag[6] = '\0'; |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
init_CIDSysInfo (CIDSysInfo *csi) { |
| 136 |
csi->registry = NULL; |
| 137 |
csi->ordering = NULL; |
| 138 |
csi->supplement = 0; |
| 139 |
} |
| 140 |
|
| 141 |
static void |
| 142 |
pdf_init_font_struct (pdf_font *font) |
| 143 |
{ |
| 144 |
ASSERT(font); |
| 145 |
|
| 146 |
font->ident = NULL; |
| 147 |
font->filename = NULL; |
| 148 |
font->subtype = -1; |
| 149 |
font->font_id = -1; |
| 150 |
font->fontname = NULL; |
| 151 |
memset(font->uniqueID, 0, 7); |
| 152 |
font->index = 0; |
| 153 |
|
| 154 |
font->encoding_id = -1; |
| 155 |
|
| 156 |
font->reference = NULL; |
| 157 |
font->resource = NULL; |
| 158 |
font->descriptor = NULL; |
| 159 |
|
| 160 |
font->point_size = 0; |
| 161 |
font->design_size = 0; |
| 162 |
|
| 163 |
font->usedchars = NULL; |
| 164 |
font->flags = 0; |
| 165 |
|
| 166 |
font->type0.descendant = -1; |
| 167 |
font->type0.wmode = 0; |
| 168 |
|
| 169 |
init_CIDSysInfo(&font->cid.csi); |
| 170 |
font->cid.need_vmetrics = 0; |
| 171 |
font->cid.usedchars_v = NULL; |
| 172 |
font->cid.options.embed = 0; |
| 173 |
font->cid.options.style = FONT_STYLE_NONE; |
| 174 |
font->cid.options.stemv = 0; |
| 175 |
init_CIDSysInfo(&font->cid.options.csi); |
| 176 |
|
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
pdf_flush_font (pdf_font *font) |
| 182 |
{ |
| 183 |
char *fontname, *uniqueTag; |
| 184 |
|
| 185 |
if (!font) |
| 186 |
return; |
| 187 |
if ((font->flags & PDF_FONT_FLAG_IS_ALIAS) || (font->flags & PDF_FONT_FLAG_IS_REENCODE)) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
if (font->resource && font->reference) { |
| 192 |
switch (font->subtype) { |
| 193 |
case PDF_FONT_FONTTYPE_TYPE3: |
| 194 |
case PDF_FONT_FONTTYPE_TYPE0: |
| 195 |
break; |
| 196 |
case PDF_FONT_FONTTYPE_CIDTYPE0: |
| 197 |
case PDF_FONT_FONTTYPE_CIDTYPE2: |
| 198 |
break; |
| 199 |
default: |
| 200 |
if (font->flags & PDF_FONT_FLAG_NOEMBED) { |
| 201 |
pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(font->fontname)); |
| 202 |
if (font->descriptor) { |
| 203 |
pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(font->fontname)); |
| 204 |
} |
| 205 |
} else { |
| 206 |
ASSERT(font->fontname); |
| 207 |
fontname = NEW(7+strlen(font->fontname)+1, char); |
| 208 |
uniqueTag = pdf_font_get_uniqueTag(font); |
| 209 |
sprintf(fontname, "%6s+%s", uniqueTag, font->fontname); |
| 210 |
pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(fontname)); |
| 211 |
if (font->descriptor) { |
| 212 |
pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(fontname)); |
| 213 |
} |
| 214 |
RELEASE(fontname); |
| 215 |
} |
| 216 |
if (font->descriptor) { |
| 217 |
pdf_add_dict(font->resource, pdf_new_name("FontDescriptor"), pdf_ref_obj(font->descriptor)); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (font->resource) |
| 223 |
pdf_release_obj(font->resource); |
| 224 |
if (font->descriptor) |
| 225 |
pdf_release_obj(font->descriptor); |
| 226 |
if (font->reference) |
| 227 |
pdf_release_obj(font->reference); |
| 228 |
|
| 229 |
font->reference = NULL; |
| 230 |
font->resource = NULL; |
| 231 |
font->descriptor = NULL; |
| 232 |
|
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
pdf_clean_font_struct (pdf_font *font) |
| 238 |
{ |
| 239 |
if (!font) |
| 240 |
return; |
| 241 |
|
| 242 |
if (font->resource) |
| 243 |
WARN("font \"%s\" not properly released?", font->ident); |
| 244 |
|
| 245 |
if (font->ident) |
| 246 |
RELEASE(font->ident); |
| 247 |
if (font->filename) |
| 248 |
RELEASE(font->filename); |
| 249 |
if (font->fontname) |
| 250 |
RELEASE(font->fontname); |
| 251 |
if (font->usedchars) { |
| 252 |
if (!(font->flags & PDF_FONT_FLAG_USEDCHAR_SHARED)) |
| 253 |
RELEASE(font->usedchars); |
| 254 |
} |
| 255 |
if (font->cid.csi.registry) |
| 256 |
RELEASE(font->cid.csi.registry); |
| 257 |
if (font->cid.csi.ordering) |
| 258 |
RELEASE(font->cid.csi.ordering); |
| 259 |
if (font->cid.options.csi.registry) |
| 260 |
RELEASE(font->cid.options.csi.registry); |
| 261 |
if (font->cid.options.csi.ordering) |
| 262 |
RELEASE(font->cid.options.csi.ordering); |
| 263 |
if (font->cid.usedchars_v) |
| 264 |
RELEASE(font->cid.usedchars_v); |
| 265 |
|
| 266 |
font->ident = NULL; |
| 267 |
font->filename = NULL; |
| 268 |
font->fontname = NULL; |
| 269 |
font->usedchars = NULL; |
| 270 |
font->cid.csi.registry = NULL; |
| 271 |
font->cid.csi.ordering = NULL; |
| 272 |
font->cid.options.csi.registry = NULL; |
| 273 |
font->cid.options.csi.ordering = NULL; |
| 274 |
font->cid.usedchars_v = NULL; |
| 275 |
|
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
#define CACHE_ALLOC_SIZE 16u |
| 280 |
|
| 281 |
static struct { |
| 282 |
int count; |
| 283 |
int capacity; |
| 284 |
pdf_font *fonts; |
| 285 |
} font_cache = { |
| 286 |
0, 0, NULL |
| 287 |
}; |
| 288 |
|
| 289 |
void |
| 290 |
pdf_init_fonts (void) |
| 291 |
{ |
| 292 |
ASSERT(font_cache.fonts == NULL); |
| 293 |
|
| 294 |
agl_init_map(); |
| 295 |
|
| 296 |
CMap_cache_init(); |
| 297 |
pdf_init_encodings(); |
| 298 |
|
| 299 |
font_cache.count = 0; |
| 300 |
font_cache.capacity = CACHE_ALLOC_SIZE; |
| 301 |
font_cache.fonts = NEW(font_cache.capacity, pdf_font); |
| 302 |
|
| 303 |
{ |
| 304 |
time_t current_time; |
| 305 |
|
| 306 |
current_time = dpx_util_get_unique_time_if_given(); |
| 307 |
if (current_time == INVALID_EPOCH_VALUE) |
| 308 |
current_time = time(NULL); |
| 309 |
srand(current_time); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
#define CHECK_ID(n) do {\ |
| 314 |
if ((n) < 0 || (n) >= font_cache.count) {\ |
| 315 |
ERROR("Invalid font ID: %d", (n));\ |
| 316 |
}\ |
| 317 |
} while (0) |
| 318 |
|
| 319 |
static pdf_font * |
| 320 |
GET_FONT (int font_id) |
| 321 |
{ |
| 322 |
pdf_font *font = NULL; |
| 323 |
|
| 324 |
if (font_id >= 0 && font_id < font_cache.count) { |
| 325 |
font = &font_cache.fonts[font_id]; |
| 326 |
if (font->flags & PDF_FONT_FLAG_IS_ALIAS) { |
| 327 |
font = &font_cache.fonts[font->font_id]; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return font; |
| 332 |
} |
| 333 |
|
| 334 |
pdf_font * |
| 335 |
pdf_get_font_data (int font_id) |
| 336 |
{ |
| 337 |
CHECK_ID(font_id); |
| 338 |
|
| 339 |
return &font_cache.fonts[font_id]; |
| 340 |
} |
| 341 |
|
| 342 |
char * |
| 343 |
pdf_get_font_ident (int font_id) |
| 344 |
{ |
| 345 |
pdf_font *font; |
| 346 |
|
| 347 |
CHECK_ID(font_id); |
| 348 |
|
| 349 |
font = &font_cache.fonts[font_id]; |
| 350 |
|
| 351 |
return font->ident; |
| 352 |
} |
| 353 |
|
| 354 |
pdf_obj * |
| 355 |
pdf_get_font_reference (int font_id) |
| 356 |
{ |
| 357 |
pdf_font *font; |
| 358 |
|
| 359 |
CHECK_ID(font_id); |
| 360 |
|
| 361 |
font = GET_FONT(font_id); |
| 362 |
if (font->flags & PDF_FONT_FLAG_IS_REENCODE) { |
| 363 |
font = GET_FONT(font->font_id); |
| 364 |
} |
| 365 |
if (!font->reference) { |
| 366 |
font->reference = pdf_ref_obj(pdf_font_get_resource(font)); |
| 367 |
} |
| 368 |
if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) { |
| 369 |
if (!pdf_lookup_dict(font->resource, "DescendantFonts")) { |
| 370 |
pdf_obj *array; |
| 371 |
|
| 372 |
array = pdf_new_array(); |
| 373 |
pdf_add_array(array, pdf_get_font_reference(font->type0.descendant)); |
| 374 |
pdf_add_dict(font->resource, pdf_new_name("DescendantFonts"), array); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return pdf_link_obj(font->reference); |
| 379 |
} |
| 380 |
|
| 381 |
pdf_obj * |
| 382 |
pdf_get_font_resource (int font_id) |
| 383 |
{ |
| 384 |
pdf_font *font; |
| 385 |
|
| 386 |
CHECK_ID(font_id); |
| 387 |
|
| 388 |
font = GET_FONT(font_id); |
| 389 |
if (font->flags & PDF_FONT_FLAG_IS_REENCODE) { |
| 390 |
font = GET_FONT(font->font_id); |
| 391 |
} |
| 392 |
if (!font->resource) |
| 393 |
font->resource = pdf_new_dict(); |
| 394 |
|
| 395 |
return font->resource; /* FIXME */ |
| 396 |
} |
| 397 |
|
| 398 |
char * |
| 399 |
pdf_get_font_usedchars (int font_id) |
| 400 |
{ |
| 401 |
pdf_font *font; |
| 402 |
|
| 403 |
CHECK_ID(font_id); |
| 404 |
|
| 405 |
font = GET_FONT(font_id); |
| 406 |
if (font->flags & PDF_FONT_FLAG_IS_REENCODE) { |
| 407 |
font = GET_FONT(font->font_id); |
| 408 |
} |
| 409 |
if (font->subtype != PDF_FONT_FONTTYPE_TYPE0) { |
| 410 |
if (!font->usedchars) { |
| 411 |
font->usedchars = NEW(256, char); |
| 412 |
memset(font->usedchars, 0, 256 * sizeof(char)); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
return font->usedchars; |
| 417 |
} |
| 418 |
|
| 419 |
int |
| 420 |
pdf_get_font_wmode (int font_id) |
| 421 |
{ |
| 422 |
pdf_font *font; |
| 423 |
|
| 424 |
CHECK_ID(font_id); |
| 425 |
|
| 426 |
font = GET_FONT(font_id); |
| 427 |
if (font->flags & PDF_FONT_FLAG_IS_REENCODE) { |
| 428 |
font = GET_FONT(font->font_id); |
| 429 |
} |
| 430 |
if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) { |
| 431 |
return font->type0.wmode; |
| 432 |
} else { |
| 433 |
return 0; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
int |
| 438 |
pdf_get_font_subtype (int font_id) |
| 439 |
{ |
| 440 |
pdf_font *font; |
| 441 |
|
| 442 |
CHECK_ID(font_id); |
| 443 |
|
| 444 |
font = GET_FONT(font_id); |
| 445 |
if (font->flags & PDF_FONT_FLAG_IS_REENCODE) { |
| 446 |
font = GET_FONT(font->font_id); |
| 447 |
} |
| 448 |
|
| 449 |
return font->subtype; |
| 450 |
} |
| 451 |
|
| 452 |
int |
| 453 |
pdf_get_font_encoding (int font_id) |
| 454 |
{ |
| 455 |
pdf_font *font; |
| 456 |
|
| 457 |
CHECK_ID(font_id); |
| 458 |
|
| 459 |
font = GET_FONT(font_id); |
| 460 |
|
| 461 |
return font->encoding_id; |
| 462 |
} |
| 463 |
|
| 464 |
int |
| 465 |
pdf_font_resource_name (int font_id, char *buff) |
| 466 |
{ |
| 467 |
int len; |
| 468 |
pdf_font *font; |
| 469 |
|
| 470 |
CHECK_ID(font_id); |
| 471 |
|
| 472 |
font = &font_cache.fonts[font_id]; |
| 473 |
if (font->flags & PDF_FONT_FLAG_IS_ALIAS) { |
| 474 |
font_id = font->font_id; |
| 475 |
} |
| 476 |
font = GET_FONT(font_id); |
| 477 |
if (font->flags & PDF_FONT_FLAG_IS_REENCODE) { |
| 478 |
font_id = font->font_id; |
| 479 |
} |
| 480 |
len = sprintf(buff, "F%d", font_id); |
| 481 |
|
| 482 |
return len; |
| 483 |
} |
| 484 |
|
| 485 |
/* The rule for ToUnicode creation is: |
| 486 |
* |
| 487 |
* If "tounicode" option is specified in fontmap, use that. |
| 488 |
* If there is ToUnicode CMap with same name as TFM, use that. |
| 489 |
* If no "tounicode" option is used and no ToUnicode CMap with |
| 490 |
* same name as TFM is found, create ToUnicode CMap from glyph |
| 491 |
* names and AGL file. |
| 492 |
*/ |
| 493 |
static int |
| 494 |
try_load_ToUnicode_CMap (pdf_font *font) |
| 495 |
{ |
| 496 |
pdf_obj *tounicode; |
| 497 |
const char *cmap_name = NULL; |
| 498 |
fontmap_rec *mrec = NULL; /* Be sure fontmap is still alive here */ |
| 499 |
|
| 500 |
ASSERT(font); |
| 501 |
|
| 502 |
/* We are using different encoding for Type0 font. |
| 503 |
* This feature is unavailable for them. |
| 504 |
*/ |
| 505 |
if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) |
| 506 |
return 0; |
| 507 |
|
| 508 |
ASSERT(font->ident); |
| 509 |
|
| 510 |
mrec = pdf_lookup_fontmap_record(font->ident); |
| 511 |
if (MREC_HAS_TOUNICODE(mrec)) { |
| 512 |
cmap_name = mrec->opt.tounicode; |
| 513 |
} else { |
| 514 |
cmap_name = font->ident; |
| 515 |
} |
| 516 |
tounicode = pdf_load_ToUnicode_stream(cmap_name); |
| 517 |
|
| 518 |
if (!tounicode) { |
| 519 |
if (MREC_HAS_TOUNICODE(mrec)) { |
| 520 |
WARN("Failed to read ToUnicode mapping \"%s\"...", mrec->opt.tounicode); |
| 521 |
} |
| 522 |
} else { |
| 523 |
if (pdf_obj_typeof(tounicode) != PDF_STREAM) { |
| 524 |
ERROR("Object returned by pdf_load_ToUnicode_stream() not stream object! (This must be bug)"); |
| 525 |
} else if (pdf_stream_length(tounicode) > 0) { |
| 526 |
pdf_obj *fontdict = pdf_font_get_resource(font); |
| 527 |
|
| 528 |
pdf_add_dict(fontdict, |
| 529 |
pdf_new_name("ToUnicode"), |
| 530 |
pdf_ref_obj (tounicode)); /* _FIXME_ */ |
| 531 |
if (dpx_conf.verbose_level > 0) |
| 532 |
MESG("pdf_font>> ToUnicode CMap \"%s\" attached to font id=\"%s\".\n", |
| 533 |
cmap_name, font->ident); |
| 534 |
} |
| 535 |
pdf_release_obj(tounicode); |
| 536 |
} |
| 537 |
|
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
void |
| 542 |
pdf_close_fonts (void) |
| 543 |
{ |
| 544 |
int font_id; |
| 545 |
|
| 546 |
for (font_id = 0; font_id < font_cache.count; font_id++) { |
| 547 |
pdf_font *font; |
| 548 |
|
| 549 |
font = &font_cache.fonts[font_id]; |
| 550 |
if ((font->flags & PDF_FONT_FLAG_IS_ALIAS) || |
| 551 |
(font->flags & PDF_FONT_FLAG_IS_REENCODE) || |
| 552 |
!font->reference) { |
| 553 |
continue; |
| 554 |
} |
| 555 |
if (font->subtype == PDF_FONT_FONTTYPE_CIDTYPE0 || |
| 556 |
font->subtype == PDF_FONT_FONTTYPE_CIDTYPE2) { |
| 557 |
continue; |
| 558 |
} |
| 559 |
|
| 560 |
if (dpx_conf.verbose_level > 0) { |
| 561 |
if (font->subtype != PDF_FONT_FONTTYPE_TYPE0) { |
| 562 |
MESG("(%s", font->filename); |
| 563 |
if (dpx_conf.verbose_level > 2 && |
| 564 |
!(font->flags & PDF_FONT_FLAG_NOEMBED)) { |
| 565 |
MESG("[%s+%s]", pdf_font_get_uniqueTag(font), font->fontname); |
| 566 |
} else if (dpx_conf.verbose_level > 1) { |
| 567 |
MESG("[%s]", font->fontname); |
| 568 |
} |
| 569 |
if (dpx_conf.verbose_level > 1) { |
| 570 |
if (font->encoding_id >= 0) { |
| 571 |
MESG("[%s]", pdf_encoding_get_name(font->encoding_id)); |
| 572 |
} else { |
| 573 |
MESG("[built-in]"); |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
/* Must come before load_xxx */ |
| 580 |
try_load_ToUnicode_CMap(font); |
| 581 |
|
| 582 |
switch (font->subtype) { |
| 583 |
case PDF_FONT_FONTTYPE_TYPE1: |
| 584 |
if (dpx_conf.verbose_level > 0) |
| 585 |
MESG("[Type1]"); |
| 586 |
if (!(font->flags & PDF_FONT_FLAG_BASEFONT)) |
| 587 |
pdf_font_load_type1(font); |
| 588 |
break; |
| 589 |
case PDF_FONT_FONTTYPE_TYPE1C: |
| 590 |
if (dpx_conf.verbose_level > 0) |
| 591 |
MESG("[Type1C]"); |
| 592 |
pdf_font_load_type1c(font); |
| 593 |
break; |
| 594 |
case PDF_FONT_FONTTYPE_TRUETYPE: |
| 595 |
if (dpx_conf.verbose_level > 0) |
| 596 |
MESG("[TrueType]"); |
| 597 |
pdf_font_load_truetype(font); |
| 598 |
break; |
| 599 |
case PDF_FONT_FONTTYPE_TYPE3: |
| 600 |
if (dpx_conf.verbose_level > 0) |
| 601 |
MESG("[Type3/PK]"); |
| 602 |
pdf_font_load_pkfont (font); |
| 603 |
break; |
| 604 |
case PDF_FONT_FONTTYPE_TYPE0: |
| 605 |
if (dpx_conf.verbose_level > 0) |
| 606 |
MESG("[Type0]"); |
| 607 |
pdf_font_load_type0(font); |
| 608 |
break; |
| 609 |
} |
| 610 |
|
| 611 |
if (font->encoding_id >= 0) { |
| 612 |
if (font->subtype != PDF_FONT_FONTTYPE_TYPE0) { |
| 613 |
pdf_encoding_add_usedchars(font->encoding_id, font->usedchars); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
if (dpx_conf.verbose_level > 0) { |
| 618 |
if (font->subtype != PDF_FONT_FONTTYPE_TYPE0) { |
| 619 |
MESG(")"); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
pdf_encoding_complete(); |
| 625 |
/* Order is important... */ |
| 626 |
for (font_id = 0; font_id < font_cache.count; font_id++) { |
| 627 |
pdf_font *font = &font_cache.fonts[font_id]; |
| 628 |
|
| 629 |
if ((font->flags & PDF_FONT_FLAG_IS_ALIAS) || |
| 630 |
(font->flags & PDF_FONT_FLAG_IS_REENCODE) || |
| 631 |
!font->reference) { |
| 632 |
continue; |
| 633 |
} |
| 634 |
|
| 635 |
switch (font->subtype) { |
| 636 |
case PDF_FONT_FONTTYPE_CIDTYPE0: |
| 637 |
case PDF_FONT_FONTTYPE_CIDTYPE2: |
| 638 |
pdf_font_load_cidfont(font); |
| 639 |
break; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
for (font_id = 0; font_id < font_cache.count; font_id++) { |
| 644 |
pdf_font *font; |
| 645 |
|
| 646 |
font = &font_cache.fonts[font_id]; |
| 647 |
|
| 648 |
if ((font->flags & PDF_FONT_FLAG_IS_ALIAS) || |
| 649 |
(font->flags & PDF_FONT_FLAG_IS_REENCODE) || |
| 650 |
!font->reference) { |
| 651 |
pdf_flush_font(font); |
| 652 |
pdf_clean_font_struct(font); |
| 653 |
continue; |
| 654 |
} |
| 655 |
|
| 656 |
if (font->encoding_id >= 0 && |
| 657 |
font->subtype != PDF_FONT_FONTTYPE_TYPE0 && |
| 658 |
font->subtype != PDF_FONT_FONTTYPE_CIDTYPE0 && |
| 659 |
font->subtype != PDF_FONT_FONTTYPE_CIDTYPE2) { |
| 660 |
pdf_obj *enc_obj = pdf_get_encoding_obj(font->encoding_id); |
| 661 |
pdf_obj *tounicode; |
| 662 |
|
| 663 |
/* Predefined encodings (and those simplified to them) are embedded |
| 664 |
* as direct objects, but this is purely a matter of taste. |
| 665 |
*/ |
| 666 |
if (enc_obj) { |
| 667 |
if (font->subtype == PDF_FONT_FONTTYPE_TRUETYPE) { |
| 668 |
if (pdf_encoding_is_predefined(font->encoding_id) && PDF_OBJ_NAMETYPE(enc_obj)) { |
| 669 |
pdf_add_dict(font->resource, |
| 670 |
pdf_new_name("Encoding"), pdf_link_obj(enc_obj)); |
| 671 |
} |
| 672 |
} else { |
| 673 |
pdf_add_dict(font->resource, |
| 674 |
pdf_new_name("Encoding"), |
| 675 |
PDF_OBJ_NAMETYPE(enc_obj) ? pdf_link_obj(enc_obj) : pdf_ref_obj(enc_obj)); |
| 676 |
} |
| 677 |
} |
| 678 |
/* For built-in encoding, each font loader create ToUnicode CMap. */ |
| 679 |
if (!pdf_lookup_dict(font->resource, "ToUnicode")) { |
| 680 |
tounicode = pdf_encoding_get_tounicode(font->encoding_id); |
| 681 |
if (tounicode) { |
| 682 |
pdf_add_dict(font->resource, |
| 683 |
pdf_new_name("ToUnicode"), pdf_ref_obj(tounicode)); |
| 684 |
} |
| 685 |
} |
| 686 |
} else if (font->subtype == PDF_FONT_FONTTYPE_TRUETYPE) { |
| 687 |
/* encoding_id < 0 means MacRoman here (but not really) |
| 688 |
* We use MacRoman as "default" encoding. */ |
| 689 |
pdf_add_dict(font->resource, |
| 690 |
pdf_new_name("Encoding"), |
| 691 |
pdf_new_name("MacRomanEncoding")); |
| 692 |
} |
| 693 |
pdf_flush_font(font); |
| 694 |
pdf_clean_font_struct(font); |
| 695 |
} |
| 696 |
|
| 697 |
RELEASE(font_cache.fonts); |
| 698 |
font_cache.fonts = NULL; |
| 699 |
font_cache.count = 0; |
| 700 |
font_cache.capacity = 0; |
| 701 |
|
| 702 |
CMap_cache_close(); |
| 703 |
pdf_close_encodings(); |
| 704 |
|
| 705 |
agl_close_map (); /* After encoding */ |
| 706 |
|
| 707 |
return; |
| 708 |
} |
| 709 |
|
| 710 |
int |
| 711 |
pdf_font_findresource (const char *ident, double scale) |
| 712 |
{ |
| 713 |
int font_id, found = 0; |
| 714 |
|
| 715 |
for (font_id = 0; font_id < font_cache.count; font_id++) { |
| 716 |
pdf_font *font; |
| 717 |
|
| 718 |
font = &font_cache.fonts[font_id]; |
| 719 |
switch (font->subtype) { |
| 720 |
case PDF_FONT_FONTTYPE_TYPE1: |
| 721 |
case PDF_FONT_FONTTYPE_TYPE1C: |
| 722 |
case PDF_FONT_FONTTYPE_TRUETYPE: |
| 723 |
case PDF_FONT_FONTTYPE_TYPE0: |
| 724 |
if (!strcmp(ident, font->ident)) { |
| 725 |
found = 1; |
| 726 |
} |
| 727 |
break; |
| 728 |
case PDF_FONT_FONTTYPE_TYPE3: |
| 729 |
/* There shouldn't be any encoding specified for PK font. |
| 730 |
* It must be always font's build-in encoding. |
| 731 |
* |
| 732 |
* TODO: a PK font with two encodings makes no sense. Change? |
| 733 |
*/ |
| 734 |
if (!strcmp(ident, font->ident) && scale == font->point_size) { |
| 735 |
found = 1; |
| 736 |
} |
| 737 |
break; |
| 738 |
} |
| 739 |
|
| 740 |
if (found) { |
| 741 |
if (dpx_conf.verbose_level > 0) { |
| 742 |
MESG("\npdf_font>> Font \"%s\" (enc_id=%d) found at id=%d.\n", font->ident, font->encoding_id, font_id); |
| 743 |
} |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
return found ? font_id : -1; |
| 749 |
} |
| 750 |
|
| 751 |
static int |
| 752 |
create_font_alias (const char *ident, int font_id) |
| 753 |
{ |
| 754 |
int this_id; |
| 755 |
pdf_font *font, *src; |
| 756 |
|
| 757 |
if (font_id < 0 || font_id >= font_cache.count) |
| 758 |
return -1; |
| 759 |
|
| 760 |
src = &font_cache.fonts[font_id]; |
| 761 |
|
| 762 |
this_id = font_cache.count; |
| 763 |
if (font_cache.count >= font_cache.capacity) { |
| 764 |
font_cache.capacity += CACHE_ALLOC_SIZE; |
| 765 |
font_cache.fonts = RENEW(font_cache.fonts, font_cache.capacity, pdf_font); |
| 766 |
} |
| 767 |
font = &font_cache.fonts[this_id]; |
| 768 |
pdf_init_font_struct(font); |
| 769 |
|
| 770 |
font->ident = NEW(strlen(ident) + 1, char); |
| 771 |
strcpy(font->ident, ident); |
| 772 |
font->font_id = font_id; |
| 773 |
font->subtype = src->subtype; |
| 774 |
font->encoding_id = src->encoding_id; |
| 775 |
|
| 776 |
font->flags |= PDF_FONT_FLAG_IS_ALIAS; |
| 777 |
|
| 778 |
font_cache.count++; |
| 779 |
|
| 780 |
return this_id; |
| 781 |
} |
| 782 |
|
| 783 |
static int |
| 784 |
create_font_reencoded (const char *ident, int font_id, int cmap_id) |
| 785 |
{ |
| 786 |
int this_id; |
| 787 |
pdf_font *font; |
| 788 |
|
| 789 |
if (font_cache.count + 1 >= font_cache.capacity) { |
| 790 |
font_cache.capacity += CACHE_ALLOC_SIZE; |
| 791 |
font_cache.fonts = RENEW(font_cache.fonts, font_cache.capacity, pdf_font); |
| 792 |
} |
| 793 |
|
| 794 |
this_id = font_cache.count; |
| 795 |
font = &font_cache.fonts[this_id]; |
| 796 |
pdf_init_font_struct(font); |
| 797 |
font->ident = NEW(strlen(ident) + 1, char); |
| 798 |
strcpy(font->ident, ident); |
| 799 |
font->font_id = font_id; |
| 800 |
font->subtype = PDF_FONT_FONTTYPE_TYPE0; |
| 801 |
font->encoding_id = cmap_id; |
| 802 |
font->flags |= PDF_FONT_FLAG_IS_REENCODE; |
| 803 |
font->flags |= PDF_FONT_FLAG_USEDCHAR_SHARED; |
| 804 |
font_cache.count++; |
| 805 |
|
| 806 |
return this_id; |
| 807 |
} |
| 808 |
|
| 809 |
int |
| 810 |
pdf_font_load_font (const char *ident, double font_scale, const fontmap_rec *mrec) |
| 811 |
{ |
| 812 |
int font_id = -1; |
| 813 |
pdf_font *font; |
| 814 |
int encoding_id = -1, cmap_id = -1; |
| 815 |
const char *fontname; |
| 816 |
|
| 817 |
/* |
| 818 |
* Get appropriate info from map file. (PK fonts at two different |
| 819 |
* point sizes would be looked up twice unecessarily.) |
| 820 |
*/ |
| 821 |
fontname = mrec ? mrec->font_name : ident; |
| 822 |
/* XeTeX specific... |
| 823 |
* First try loading GID-to-CID mapping from CFF CID-keyed OpenType font. |
| 824 |
* There was a serious bug in xdv support... It was implemented with the wrong |
| 825 |
* assumption that CID always equals to GID. |
| 826 |
* TODO: There is a possibility that GID-to-CID mapping is not one-to-one. |
| 827 |
* Use internal glyph ordering rather than map GID to CIDs. |
| 828 |
*/ |
| 829 |
if (mrec && mrec->opt.use_glyph_encoding) { |
| 830 |
int wmode = 0; |
| 831 |
/* Should be always Identity-H or Identity-V for XeTeX output. */ |
| 832 |
if (mrec->enc_name) { |
| 833 |
if (!strcmp(mrec->enc_name, "Identity-V")) |
| 834 |
wmode = 1; |
| 835 |
else if (!strcmp(mrec->enc_name, "Identity-H")) |
| 836 |
wmode = 0; |
| 837 |
else { |
| 838 |
WARN("Unexpected encoding specified for xdv: %s", mrec->enc_name); |
| 839 |
} |
| 840 |
/* cmap_id < 0 is returned if ... |
| 841 |
* Font is not a CFF font |
| 842 |
* GID to CID mapping is identity mapping |
| 843 |
* |
| 844 |
* TODO: fontmap record still has Identity CMap assigned but actually different CMap |
| 845 |
* can be attached to the font here. Should we fix mrec->enc_name here? |
| 846 |
*/ |
| 847 |
cmap_id = otf_try_load_GID_to_CID_map(mrec->font_name, mrec->opt.index, wmode); |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
if (cmap_id < 0 && mrec && mrec->enc_name) { |
| 852 |
if (!strcmp(mrec->enc_name, "unicode")) { |
| 853 |
cmap_id = otf_load_Unicode_CMap(mrec->font_name, |
| 854 |
mrec->opt.index, mrec->opt.otl_tags, |
| 855 |
((mrec->opt.flags & FONTMAP_OPT_VERT) ? 1 : 0)); |
| 856 |
if (cmap_id < 0) { |
| 857 |
cmap_id = t1_load_UnicodeCMap(mrec->font_name, mrec->opt.otl_tags, |
| 858 |
((mrec->opt.flags & FONTMAP_OPT_VERT) ? 1 : 0)); |
| 859 |
} |
| 860 |
} else if (!strstr(mrec->enc_name, ".enc") || strstr(mrec->enc_name, ".cmap")) { |
| 861 |
cmap_id = CMap_cache_find(mrec->enc_name); |
| 862 |
} |
| 863 |
if (cmap_id < 0) { |
| 864 |
encoding_id = pdf_encoding_findresource(mrec->enc_name); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
if (mrec && mrec->enc_name) { |
| 869 |
if (cmap_id < 0 && encoding_id < 0) { |
| 870 |
WARN("Could not read encoding file: %s", mrec->enc_name); |
| 871 |
return -1; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
if (mrec && cmap_id >= 0) { |
| 876 |
/* Composite font */ |
| 877 |
CMap *cmap; |
| 878 |
CIDSysInfo *csi; |
| 879 |
int wmode, cid_id; |
| 880 |
|
| 881 |
cmap = CMap_cache_get(cmap_id); |
| 882 |
csi = CMap_is_Identity(cmap) ? NULL : CMap_get_CIDSysInfo(cmap); |
| 883 |
wmode = CMap_get_wmode(cmap); |
| 884 |
|
| 885 |
cid_id = pdf_font_cidfont_lookup_cache(font_cache.fonts, font_cache.count, mrec->font_name, csi, &mrec->opt); |
| 886 |
if (cid_id >= 0) { |
| 887 |
for (font_id = 0; font_id < font_cache.count; font_id++) { |
| 888 |
font = &font_cache.fonts[font_id]; |
| 889 |
if (font->subtype != PDF_FONT_FONTTYPE_TYPE0) |
| 890 |
continue; |
| 891 |
if (font->type0.wmode == wmode && font->type0.descendant == cid_id) { |
| 892 |
break; |
| 893 |
} |
| 894 |
} |
| 895 |
if (font_id >= 0 && font_id < font_cache.count) { |
| 896 |
font = &font_cache.fonts[font_id]; |
| 897 |
if (font->encoding_id == cmap_id) { |
| 898 |
if (dpx_conf.verbose_level > 0) { |
| 899 |
MESG("\npdf_font>> Type0 font \"%s\" cmap_id=<%s,%d> found at font_id=<%s,%d>.\n", |
| 900 |
mrec->font_name, mrec->enc_name, cmap_id, pdf_get_font_ident(font_id), font_id); |
| 901 |
} |
| 902 |
if (!strcmp(ident, font->ident)) { |
| 903 |
return font_id; |
| 904 |
} else { |
| 905 |
return create_font_alias(ident, font_id); |
| 906 |
} |
| 907 |
} else { |
| 908 |
if (dpx_conf.verbose_level > 0) { |
| 909 |
MESG("\npdf_font>> Type0 font \"%s\" cmap_id=<%s,%d> found at font_id=<%s,%d>. (re-encoded)\n", |
| 910 |
mrec->font_name, mrec->enc_name, cmap_id, pdf_get_font_ident(font_id), font_id); |
| 911 |
} |
| 912 |
return create_font_reencoded(ident, font_id, cmap_id); |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
/* plus one for CIDFont */ |
| 918 |
if (font_cache.count + 1 >= font_cache.capacity) { |
| 919 |
font_cache.capacity += CACHE_ALLOC_SIZE; |
| 920 |
font_cache.fonts = RENEW(font_cache.fonts, font_cache.capacity, pdf_font); |
| 921 |
} |
| 922 |
|
| 923 |
if (cid_id < 0) { |
| 924 |
pdf_font *cidfont; |
| 925 |
|
| 926 |
cid_id = font_cache.count; |
| 927 |
cidfont = &font_cache.fonts[cid_id]; |
| 928 |
pdf_init_font_struct(cidfont); |
| 929 |
if (pdf_font_open_cidfont(cidfont, fontname, csi, &mrec->opt) < 0) { |
| 930 |
pdf_clean_font_struct(cidfont); |
| 931 |
return -1; |
| 932 |
} |
| 933 |
font_cache.count++; |
| 934 |
} |
| 935 |
font_id = font_cache.count; |
| 936 |
font = &font_cache.fonts[font_id]; |
| 937 |
pdf_init_font_struct(font); |
| 938 |
if (pdf_font_open_type0(font, cid_id, wmode) < 0) { |
| 939 |
pdf_clean_font_struct(font); |
| 940 |
return -1; |
| 941 |
} |
| 942 |
font->ident = NEW(strlen(ident) + 1, char); |
| 943 |
strcpy(font->ident, ident); |
| 944 |
font->subtype = PDF_FONT_FONTTYPE_TYPE0; |
| 945 |
font->encoding_id = cmap_id; |
| 946 |
|
| 947 |
font_cache.count++; |
| 948 |
|
| 949 |
if (dpx_conf.verbose_level > 0) { |
| 950 |
MESG("\n"); |
| 951 |
MESG("pdf_font>> Type0 font \"%s\" ", fontname); |
| 952 |
MESG("cmap_id=<%s,%d> ", mrec->enc_name, font->encoding_id); |
| 953 |
MESG("font_id=<%s,%d>.", ident, font_id); |
| 954 |
MESG("\n"); |
| 955 |
} |
| 956 |
} else { |
| 957 |
/* Simple Font - always embed. */ |
| 958 |
for (font_id = 0; font_id < font_cache.count; font_id++) { |
| 959 |
font = &font_cache.fonts[font_id]; |
| 960 |
if (font->flags & PDF_FONT_FLAG_IS_ALIAS) |
| 961 |
continue; |
| 962 |
switch (font->subtype) { |
| 963 |
case PDF_FONT_FONTTYPE_TYPE1: |
| 964 |
case PDF_FONT_FONTTYPE_TYPE1C: |
| 965 |
case PDF_FONT_FONTTYPE_TRUETYPE: |
| 966 |
/* fontname here is font file name. |
| 967 |
* We must compare both font file name and encoding |
| 968 |
* |
| 969 |
* TODO: Embed a font only once if it is used |
| 970 |
* with two different encodings |
| 971 |
*/ |
| 972 |
if (!strcmp(fontname, font->filename) && encoding_id == font->encoding_id && |
| 973 |
(!mrec || mrec->opt.index == font->index)) { |
| 974 |
if (dpx_conf.verbose_level > 0) { |
| 975 |
MESG("\npdf_font>> Simple font \"%s\" (enc_id=%d) found at id=%d.\n", fontname, encoding_id, font_id); |
| 976 |
} |
| 977 |
if (!strcmp(ident, font->ident)) { |
| 978 |
return font_id; |
| 979 |
} else { |
| 980 |
return create_font_alias(ident, font_id); |
| 981 |
} |
| 982 |
} |
| 983 |
break; |
| 984 |
case PDF_FONT_FONTTYPE_TYPE3: |
| 985 |
/* There shouldn't be any encoding specified for PK font. |
| 986 |
* It must be always font's build-in encoding. |
| 987 |
* |
| 988 |
* TODO: a PK font with two encodings makes no sense. Change? |
| 989 |
*/ |
| 990 |
if (!strcmp(fontname, font->filename) && font_scale == font->point_size) { |
| 991 |
if (dpx_conf.verbose_level > 0) { |
| 992 |
MESG("\npdf_font>> Simple font \"%s\" (enc_id=%d) found at id=%d.\n", fontname, encoding_id, font_id); |
| 993 |
} |
| 994 |
if (!strcmp(ident, font->ident)) { |
| 995 |
return font_id; |
| 996 |
} else { |
| 997 |
return create_font_alias(ident, font_id); |
| 998 |
} |
| 999 |
} |
| 1000 |
break; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
font_id = font_cache.count; |
| 1005 |
if (font_cache.count >= font_cache.capacity) { |
| 1006 |
font_cache.capacity += CACHE_ALLOC_SIZE; |
| 1007 |
font_cache.fonts = RENEW(font_cache.fonts, font_cache.capacity, pdf_font); |
| 1008 |
} |
| 1009 |
|
| 1010 |
font = &font_cache.fonts[font_id]; |
| 1011 |
pdf_init_font_struct(font); |
| 1012 |
|
| 1013 |
font->ident = NEW(strlen(ident) + 1, char); |
| 1014 |
strcpy(font->ident, ident); |
| 1015 |
font->encoding_id = encoding_id; |
| 1016 |
font->filename = NEW(strlen(fontname) + 1, char); |
| 1017 |
font->point_size = font_scale; |
| 1018 |
strcpy(font->filename, fontname); |
| 1019 |
font->index = (mrec && mrec->opt.index) ? mrec->opt.index : 0; |
| 1020 |
font->flags |= (mrec && (mrec->opt.flags & FONTMAP_OPT_NOEMBED)) ? PDF_FONT_FLAG_NOEMBED : 0; |
| 1021 |
if (pdf_font_open_type1(font, font->filename, font->index, font->encoding_id, (font->flags & PDF_FONT_FLAG_NOEMBED) ? 0 : 1) >= 0) { |
| 1022 |
font->subtype = PDF_FONT_FONTTYPE_TYPE1; |
| 1023 |
} else if (pdf_font_open_type1c(font, font->filename, font->index, font->encoding_id, (font->flags & PDF_FONT_FLAG_NOEMBED) ? 0 : 1) >= 0) { |
| 1024 |
font->subtype = PDF_FONT_FONTTYPE_TYPE1C; |
| 1025 |
} else if (pdf_font_open_truetype(font, font->filename, font->index, font->encoding_id, (font->flags & PDF_FONT_FLAG_NOEMBED) ? 0 : 1) >= 0) { |
| 1026 |
font->subtype = PDF_FONT_FONTTYPE_TRUETYPE; |
| 1027 |
} else if (pdf_font_open_pkfont(font, font->filename, font->index, font->encoding_id, (font->flags & PDF_FONT_FLAG_NOEMBED) ? 0 : 1, font->point_size) >= 0) { |
| 1028 |
font->subtype = PDF_FONT_FONTTYPE_TYPE3; |
| 1029 |
} else { |
| 1030 |
pdf_clean_font_struct(font); |
| 1031 |
return -1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
font_cache.count++; |
| 1035 |
|
| 1036 |
if (dpx_conf.verbose_level > 0) { |
| 1037 |
MESG("\n"); |
| 1038 |
MESG("pdf_font>> Simple font \"%s\" ", fontname); |
| 1039 |
MESG("enc_id=<%s,%d> ", (mrec && mrec->enc_name) ? mrec->enc_name : "builtin", font->encoding_id); |
| 1040 |
MESG("font_id=<%s,%d>.", ident, font_id); |
| 1041 |
MESG("\n"); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
return font_id; |
| 1046 |
} |
| 1047 |
|
| 1048 |
pdf_obj * |
| 1049 |
pdf_font_get_resource (pdf_font *font) |
| 1050 |
{ |
| 1051 |
ASSERT(font); |
| 1052 |
|
| 1053 |
if (!font->resource) { |
| 1054 |
font->resource = pdf_new_dict(); |
| 1055 |
pdf_add_dict(font->resource, pdf_new_name("Type"), pdf_new_name("Font")); |
| 1056 |
switch (font->subtype) { |
| 1057 |
case PDF_FONT_FONTTYPE_TYPE1: |
| 1058 |
case PDF_FONT_FONTTYPE_TYPE1C: |
| 1059 |
pdf_add_dict(font->resource, pdf_new_name("Subtype"), pdf_new_name("Type1")); |
| 1060 |
break; |
| 1061 |
case PDF_FONT_FONTTYPE_TYPE3: |
| 1062 |
pdf_add_dict(font->resource, pdf_new_name("Subtype"), pdf_new_name("Type3")); |
| 1063 |
break; |
| 1064 |
case PDF_FONT_FONTTYPE_TRUETYPE: |
| 1065 |
pdf_add_dict(font->resource, pdf_new_name("Subtype"), pdf_new_name("TrueType")); |
| 1066 |
break; |
| 1067 |
default: |
| 1068 |
break; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
return font->resource; |
| 1073 |
} |
| 1074 |
|
| 1075 |
pdf_obj * |
| 1076 |
pdf_font_get_descriptor (pdf_font *font) |
| 1077 |
{ |
| 1078 |
ASSERT(font); |
| 1079 |
|
| 1080 |
if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) { |
| 1081 |
return NULL; |
| 1082 |
} else if (!font->descriptor) { |
| 1083 |
font->descriptor = pdf_new_dict(); |
| 1084 |
pdf_add_dict(font->descriptor, pdf_new_name("Type"), pdf_new_name("FontDescriptor")); |
| 1085 |
} |
| 1086 |
|
| 1087 |
return font->descriptor; |
| 1088 |
} |
| 1089 |
|
| 1090 |
char * |
| 1091 |
pdf_font_get_uniqueTag (pdf_font *font) |
| 1092 |
{ |
| 1093 |
ASSERT(font); |
| 1094 |
|
| 1095 |
if (font->uniqueID[0] == '\0') { |
| 1096 |
pdf_font_make_uniqueTag(font->uniqueID); |
| 1097 |
} |
| 1098 |
|
| 1099 |
return font->uniqueID; |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* |
| 1103 |
* Please don't use TFM widths for the /Widths |
| 1104 |
* entry of the font dictionary. |
| 1105 |
* |
| 1106 |
* PDF 32000-1:2008 (p.255) |
| 1107 |
* |
| 1108 |
* These widths shall be consistent with the |
| 1109 |
* actual widths given in the font program. |
| 1110 |
*/ |
| 1111 |
|
| 1112 |
#include "tfm.h" |
| 1113 |
|
| 1114 |
int |
| 1115 |
pdf_check_tfm_widths (const char *ident, double *widths, int firstchar, int lastchar, const char *usedchars) |
| 1116 |
{ |
| 1117 |
int error = 0; |
| 1118 |
int tfm_id, code, count = 0; |
| 1119 |
double sum = 0.0; |
| 1120 |
double tolerance = 1.0; |
| 1121 |
int override = 0; /* Don't set to 1! */ |
| 1122 |
|
| 1123 |
tfm_id = tfm_open(ident, 0); |
| 1124 |
if (tfm_id < 0) |
| 1125 |
return 0; |
| 1126 |
for (code = firstchar; code <= lastchar; code++) { |
| 1127 |
if (usedchars[code]) { |
| 1128 |
double width, diff; |
| 1129 |
|
| 1130 |
width = 1000. * tfm_get_width(tfm_id, code); |
| 1131 |
diff = widths[code] - width; |
| 1132 |
diff = diff < 0 ? -diff : diff; |
| 1133 |
if (override) { |
| 1134 |
widths[code] = width; |
| 1135 |
} else if (diff > tolerance) { |
| 1136 |
if (dpx_conf.verbose_level > 0) { |
| 1137 |
WARN("Intolerable difference in glyph width: font=%s, char=%d", ident, code); |
| 1138 |
WARN("font: %g vs. tfm: %g", widths[code], width); |
| 1139 |
} |
| 1140 |
sum += diff; |
| 1141 |
} |
| 1142 |
count++; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
error = sum > 0.5 * count * tolerance ? -1 : 0; |
| 1147 |
|
| 1148 |
return override ? 0 : error; |
| 1149 |
} |