| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 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 |
/* |
| 24 |
* CFF/OpenType Font support: |
| 25 |
* |
| 26 |
* Adobe Technical Note #5176, "The Compact Font Format Specfication" |
| 27 |
* |
| 28 |
* NOTE: |
| 29 |
* |
| 30 |
* Many CFF/OpenType does not have meaningful/correct CFF encoding. |
| 31 |
* Encoding should be expilicitly supplied in the fontmap. |
| 32 |
* |
| 33 |
*/ |
| 34 |
|
| 35 |
#ifdef HAVE_CONFIG_H |
| 36 |
#include <config.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <string.h> |
| 40 |
|
| 41 |
#include "system.h" |
| 42 |
#include "mem.h" |
| 43 |
#include "error.h" |
| 44 |
#include "dpxconf.h" |
| 45 |
#include "dpxfile.h" |
| 46 |
|
| 47 |
#include "pdfobj.h" |
| 48 |
#include "pdffont.h" |
| 49 |
|
| 50 |
#include "pdfencoding.h" |
| 51 |
#include "unicode.h" |
| 52 |
|
| 53 |
/* Font info. from OpenType tables */ |
| 54 |
#include "sfnt.h" |
| 55 |
#include "tt_aux.h" |
| 56 |
|
| 57 |
#include "cff_types.h" |
| 58 |
#include "cff_limits.h" |
| 59 |
#include "cff.h" |
| 60 |
#include "cff_dict.h" |
| 61 |
#include "cs_type2.h" |
| 62 |
|
| 63 |
#include "type1c.h" |
| 64 |
|
| 65 |
int |
| 66 |
pdf_font_open_type1c (pdf_font *font, const char *ident, int index, int encoding_id, int embedding) |
| 67 |
{ |
| 68 |
char *fontname; |
| 69 |
FILE *fp = NULL; |
| 70 |
sfnt *sfont; |
| 71 |
cff_font *cffont; |
| 72 |
pdf_obj *descriptor, *tmp; |
| 73 |
ULONG offset = 0; |
| 74 |
|
| 75 |
ASSERT(font); |
| 76 |
ASSERT(ident); |
| 77 |
|
| 78 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_OTFONT); |
| 79 |
if (!fp) |
| 80 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_TTFONT); |
| 81 |
if (!fp) |
| 82 |
return -1; |
| 83 |
|
| 84 |
sfont = sfnt_open(fp); |
| 85 |
if (!sfont) { |
| 86 |
if (fp) |
| 87 |
DPXFCLOSE(fp); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
if (sfont->type == SFNT_TYPE_TTC) { |
| 92 |
offset = ttc_read_offset(sfont, index); |
| 93 |
} |
| 94 |
|
| 95 |
if ((sfont->type != SFNT_TYPE_TTC && sfont->type != SFNT_TYPE_POSTSCRIPT) || |
| 96 |
sfnt_read_table_directory(sfont, offset) < 0 || |
| 97 |
(offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) { |
| 98 |
sfnt_close(sfont); |
| 99 |
if (fp) |
| 100 |
DPXFCLOSE(fp); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
cffont = cff_open(sfont->stream, offset, 0); |
| 105 |
if (!cffont) { |
| 106 |
WARN("Could not read CFF font data: %s", ident); |
| 107 |
sfnt_close(sfont); |
| 108 |
if (fp) |
| 109 |
DPXFCLOSE(fp); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
if (cffont->flag & FONTTYPE_CIDFONT) { |
| 114 |
cff_close (cffont); |
| 115 |
sfnt_close(sfont); |
| 116 |
if (fp) |
| 117 |
DPXFCLOSE(fp); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
fontname = cff_get_name(cffont); |
| 122 |
if (!fontname) { |
| 123 |
WARN("No valid FontName found in CFF/OpenType font: %s", ident); |
| 124 |
cff_close (cffont); |
| 125 |
sfnt_close(sfont); |
| 126 |
if (fp) |
| 127 |
DPXFCLOSE(fp); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
font->fontname = fontname; |
| 131 |
|
| 132 |
cff_close(cffont); |
| 133 |
|
| 134 |
if (!embedding) { |
| 135 |
WARN("Ignoring no-embed option for Type1C font: %s", ident); |
| 136 |
embedding = 1; |
| 137 |
font->flags &= ~PDF_FONT_FLAG_NOEMBED; |
| 138 |
} |
| 139 |
/* |
| 140 |
* Font like AdobePiStd does not have meaningful built-in encoding. |
| 141 |
* Some software generate CFF/OpenType font with incorrect encoding. |
| 142 |
*/ |
| 143 |
if (encoding_id < 0) { |
| 144 |
WARN("Built-in encoding used for CFF/OpenType font."); |
| 145 |
WARN("CFF font in OpenType font sometimes have strange built-in encoding."); |
| 146 |
WARN("If you find text is not encoded properly in the generated PDF file,"); |
| 147 |
WARN("please specify appropriate \".enc\" file in your fontmap."); |
| 148 |
} |
| 149 |
font->subtype = PDF_FONT_FONTTYPE_TYPE1C; |
| 150 |
|
| 151 |
descriptor = pdf_font_get_descriptor(font); |
| 152 |
/* |
| 153 |
* Create font descriptor from OpenType tables. |
| 154 |
* We can also use CFF TOP DICT/Private DICT for this. |
| 155 |
*/ |
| 156 |
tmp = tt_get_fontdesc(sfont, &embedding, -1, 1, fontname); |
| 157 |
if (!tmp) { |
| 158 |
ERROR("Could not obtain neccesary font info from OpenType table."); |
| 159 |
return -1; |
| 160 |
} |
| 161 |
pdf_merge_dict (descriptor, tmp); /* copy */ |
| 162 |
pdf_release_obj(tmp); |
| 163 |
if (!embedding) { /* tt_get_fontdesc may have changed this */ |
| 164 |
WARN("Font embedding disallowed for \"%s\"", ident); |
| 165 |
sfnt_close(sfont); |
| 166 |
if (fp) |
| 167 |
DPXFCLOSE(fp); |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
sfnt_close(sfont); |
| 172 |
if (fp) |
| 173 |
DPXFCLOSE(fp); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static void |
| 179 |
add_SimpleMetrics (pdf_font *font, cff_font *cffont, double *widths, card16 num_glyphs) |
| 180 |
{ |
| 181 |
pdf_obj *fontdict; |
| 182 |
int code, firstchar, lastchar; |
| 183 |
char *usedchars; |
| 184 |
pdf_obj *array; |
| 185 |
double scaling = 1.0; |
| 186 |
|
| 187 |
fontdict = pdf_font_get_resource(font); |
| 188 |
usedchars = font->usedchars; |
| 189 |
|
| 190 |
/* The widhts array in the font dictionary must be given relative |
| 191 |
* to the default scaling of 1000:1, not relative to the scaling |
| 192 |
* given by the font matrix. |
| 193 |
*/ |
| 194 |
if (cff_dict_known(cffont->topdict, "FontMatrix")) { |
| 195 |
scaling = 1000.0 * cff_dict_get(cffont->topdict, "FontMatrix", 0); |
| 196 |
} else { |
| 197 |
scaling = 1.0; |
| 198 |
} |
| 199 |
|
| 200 |
array = pdf_new_array(); |
| 201 |
if (num_glyphs <= 1) { |
| 202 |
/* This should be error. */ |
| 203 |
firstchar = lastchar = 0; |
| 204 |
pdf_add_array(array, pdf_new_number(0.0)); |
| 205 |
} else { |
| 206 |
firstchar = 255; lastchar = 0; |
| 207 |
for (code = 0; code < 256; code++) { |
| 208 |
if (usedchars[code]) { |
| 209 |
if (code < firstchar) firstchar = code; |
| 210 |
if (code > lastchar) lastchar = code; |
| 211 |
widths[code] *= scaling; |
| 212 |
} |
| 213 |
} |
| 214 |
if (firstchar > lastchar) { |
| 215 |
ERROR("No glyphs used at all!"); |
| 216 |
return; |
| 217 |
} |
| 218 |
pdf_check_tfm_widths(font->ident, widths, firstchar, lastchar, usedchars); |
| 219 |
|
| 220 |
for (code = firstchar; code <= lastchar; code++) { |
| 221 |
if (usedchars[code]) { |
| 222 |
pdf_add_array(array, pdf_new_number(ROUND(widths[code], 0.1))); |
| 223 |
} else { |
| 224 |
pdf_add_array(array, pdf_new_number(0.0)); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if (pdf_array_length(array) > 0) { |
| 230 |
pdf_add_dict(fontdict, pdf_new_name("Widths"), pdf_ref_obj(array)); |
| 231 |
} |
| 232 |
pdf_release_obj(array); |
| 233 |
|
| 234 |
pdf_add_dict(fontdict, pdf_new_name("FirstChar"), pdf_new_number(firstchar)); |
| 235 |
pdf_add_dict(fontdict, pdf_new_name("LastChar"), pdf_new_number(lastchar)); |
| 236 |
|
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
int |
| 241 |
pdf_font_load_type1c (pdf_font *font) |
| 242 |
{ |
| 243 |
pdf_obj *fontdict, *descriptor; |
| 244 |
pdf_obj *pdfcharset; /* Actually string object */ |
| 245 |
char *usedchars; |
| 246 |
char *fontname, *uniqueTag, *ident, *fullname; |
| 247 |
FILE *fp = NULL; |
| 248 |
int encoding_id; |
| 249 |
pdf_obj *fontfile, *stream_dict; |
| 250 |
char **enc_vec; |
| 251 |
sfnt *sfont; |
| 252 |
cff_font *cffont; |
| 253 |
cff_index *charstrings, *topdict, *cs_idx; |
| 254 |
cff_charsets *charset = NULL; |
| 255 |
cff_encoding *encoding = NULL; |
| 256 |
int topdict_offset, private_size; |
| 257 |
int charstring_len, max_len; |
| 258 |
int size, offset = 0; |
| 259 |
int stream_data_len = 0; |
| 260 |
card8 *stream_data_ptr, *data; |
| 261 |
card16 num_glyphs, cs_count, code; |
| 262 |
cs_ginfo ginfo; |
| 263 |
double nominal_width, default_width, notdef_width; |
| 264 |
double widths[256]; |
| 265 |
|
| 266 |
ASSERT(font); |
| 267 |
|
| 268 |
if (!font->reference) |
| 269 |
return 0; |
| 270 |
|
| 271 |
if (font->flags & PDF_FONT_FLAG_NOEMBED) { |
| 272 |
ERROR("Only embedded font supported for CFF/OpenType font."); |
| 273 |
} |
| 274 |
|
| 275 |
usedchars = font->usedchars; |
| 276 |
fontname = font->fontname; |
| 277 |
ident = font->filename; |
| 278 |
uniqueTag = pdf_font_get_uniqueTag (font); |
| 279 |
|
| 280 |
ASSERT(usedchars); |
| 281 |
ASSERT(fontname); |
| 282 |
ASSERT(ident); |
| 283 |
|
| 284 |
fontdict = pdf_font_get_resource (font); |
| 285 |
descriptor = pdf_font_get_descriptor(font); |
| 286 |
encoding_id = font->encoding_id; |
| 287 |
|
| 288 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_OTFONT); |
| 289 |
if (!fp) { |
| 290 |
ERROR("Could not open OpenType font: %s", ident); |
| 291 |
} |
| 292 |
|
| 293 |
sfont = sfnt_open(fp); |
| 294 |
if (!sfont) { |
| 295 |
ERROR("Could not open OpenType font: %s", ident); |
| 296 |
} |
| 297 |
if (sfnt_read_table_directory(sfont, 0) < 0) { |
| 298 |
ERROR("Could not read OpenType table directory: %s", ident); |
| 299 |
} |
| 300 |
if (sfont->type != SFNT_TYPE_POSTSCRIPT || |
| 301 |
(offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) { |
| 302 |
ERROR("Not a CFF/OpenType font (or variable font?) (11)?"); |
| 303 |
} |
| 304 |
|
| 305 |
cffont = cff_open(fp, offset, 0); |
| 306 |
if (!cffont) { |
| 307 |
ERROR("Could not open CFF font."); |
| 308 |
} |
| 309 |
if (cffont->flag & FONTTYPE_CIDFONT) { |
| 310 |
ERROR("This is CIDFont..."); |
| 311 |
} |
| 312 |
|
| 313 |
fullname = NEW(strlen(fontname) + 8, char); |
| 314 |
sprintf(fullname, "%6s+%s", uniqueTag, fontname); |
| 315 |
|
| 316 |
/* Offsets from DICTs */ |
| 317 |
cff_read_charsets(cffont); |
| 318 |
if (encoding_id < 0) |
| 319 |
cff_read_encoding(cffont); |
| 320 |
cff_read_private(cffont); |
| 321 |
cff_read_subrs (cffont); |
| 322 |
|
| 323 |
/* FIXME */ |
| 324 |
cffont->_string = cff_new_index(0); |
| 325 |
|
| 326 |
/* New Charsets data */ |
| 327 |
charset = NEW(1, cff_charsets); |
| 328 |
charset->format = 0; |
| 329 |
charset->num_entries = 0; |
| 330 |
charset->data.glyphs = NEW(256, s_SID); |
| 331 |
|
| 332 |
/* |
| 333 |
* Encoding related things. |
| 334 |
*/ |
| 335 |
enc_vec = NULL; |
| 336 |
if (encoding_id >= 0) { |
| 337 |
enc_vec = pdf_encoding_get_encoding(encoding_id); |
| 338 |
} else { |
| 339 |
pdf_obj *tounicode; |
| 340 |
|
| 341 |
/* |
| 342 |
* Create enc_vec and ToUnicode CMap for built-in encoding. |
| 343 |
*/ |
| 344 |
enc_vec = NEW(256, char *); |
| 345 |
for (code = 0; code < 256; code++) { |
| 346 |
if (usedchars[code]) { |
| 347 |
card16 gid; |
| 348 |
|
| 349 |
gid = cff_encoding_lookup(cffont, code); |
| 350 |
enc_vec[code] = cff_get_string(cffont, cff_charsets_lookup_inverse(cffont, gid)); |
| 351 |
} else { |
| 352 |
enc_vec[code] = NULL; |
| 353 |
} |
| 354 |
} |
| 355 |
if (!pdf_lookup_dict(fontdict, "ToUnicode")) { |
| 356 |
tounicode = pdf_create_ToUnicode_CMap(fullname, enc_vec, usedchars); |
| 357 |
if (tounicode) { |
| 358 |
pdf_add_dict(fontdict, |
| 359 |
pdf_new_name("ToUnicode"), |
| 360 |
pdf_ref_obj (tounicode)); |
| 361 |
pdf_release_obj(tounicode); |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* New Encoding data: |
| 368 |
* |
| 369 |
* We should not use format 0 here. |
| 370 |
* The number of encoded glyphs (num_entries) is limited to 255 in format 0, |
| 371 |
* and hence it causes problem for encodings that uses full 256 code-points. |
| 372 |
* As we always sort glyphs by encoding, we can avoid this problem simply |
| 373 |
* by using format 1; Using full range result in a single range, 0 255. |
| 374 |
* |
| 375 |
* Creating actual encoding date is delayed to eliminate character codes to |
| 376 |
* be mapped to .notdef and to handle multiply-encoded glyphs. |
| 377 |
*/ |
| 378 |
encoding = NEW(1, cff_encoding); |
| 379 |
encoding->format = 1; |
| 380 |
encoding->num_entries = 0; |
| 381 |
encoding->data.range1 = NEW(255, cff_range1); |
| 382 |
encoding->num_supps = 0; |
| 383 |
encoding->supp = NEW(255, cff_map); |
| 384 |
|
| 385 |
/* |
| 386 |
* Charastrings. |
| 387 |
*/ |
| 388 |
offset = cff_dict_get(cffont->topdict, "CharStrings", 0); |
| 389 |
cff_seek_set(cffont, offset); |
| 390 |
cs_idx = cff_get_index_header(cffont); |
| 391 |
|
| 392 |
/* Offset is now absolute offset ... fixme */ |
| 393 |
offset = cff_tell(cffont); |
| 394 |
cs_count = cs_idx->count; |
| 395 |
if (cs_count < 2) { |
| 396 |
ERROR("No valid charstring data found."); |
| 397 |
} |
| 398 |
|
| 399 |
/* New CharStrings INDEX */ |
| 400 |
charstrings = cff_new_index(257); /* 256 + 1 for ".notdef" glyph */ |
| 401 |
max_len = 2 * CS_STR_LEN_MAX; |
| 402 |
charstrings->data = NEW(max_len, card8); |
| 403 |
charstring_len = 0; |
| 404 |
|
| 405 |
/* |
| 406 |
* Information from OpenType table is rough estimate. Replace with accurate value. |
| 407 |
*/ |
| 408 |
if (cffont->private[0] && |
| 409 |
cff_dict_known(cffont->private[0], "StdVW")) { |
| 410 |
double stemv; |
| 411 |
|
| 412 |
stemv = cff_dict_get(cffont->private[0], "StdVW", 0); |
| 413 |
pdf_add_dict(descriptor, pdf_new_name("StemV"), pdf_new_number(stemv)); |
| 414 |
} |
| 415 |
|
| 416 |
/* |
| 417 |
* Widths |
| 418 |
*/ |
| 419 |
if (cffont->private[0] && |
| 420 |
cff_dict_known(cffont->private[0], "defaultWidthX")) { |
| 421 |
default_width = (double) cff_dict_get(cffont->private[0], "defaultWidthX", 0); |
| 422 |
} else { |
| 423 |
default_width = CFF_DEFAULTWIDTHX_DEFAULT; |
| 424 |
} |
| 425 |
if (cffont->private[0] && |
| 426 |
cff_dict_known(cffont->private[0], "nominalWidthX")) { |
| 427 |
nominal_width = (double) cff_dict_get(cffont->private[0], "nominalWidthX", 0); |
| 428 |
} else { |
| 429 |
nominal_width = CFF_NOMINALWIDTHX_DEFAULT; |
| 430 |
} |
| 431 |
|
| 432 |
data = NEW(CS_STR_LEN_MAX, card8); |
| 433 |
|
| 434 |
/* First we add .notdef glyph. |
| 435 |
* All Type 1 font requires .notdef glyph to be present. |
| 436 |
*/ |
| 437 |
if (dpx_conf.verbose_level > 2) { |
| 438 |
MESG("[glyphs:/.notdef"); |
| 439 |
} |
| 440 |
size = cs_idx->offset[1] - cs_idx->offset[0]; |
| 441 |
if (size > CS_STR_LEN_MAX) { |
| 442 |
ERROR("Charstring too long: gid=%u, %ld bytes", 0, size); |
| 443 |
} |
| 444 |
charstrings->offset[0] = charstring_len + 1; |
| 445 |
cff_seek(cffont, offset + cs_idx->offset[0] - 1); |
| 446 |
cff_read_data(data, size, cffont); |
| 447 |
charstring_len += cs_copy_charstring(charstrings->data + charstring_len, |
| 448 |
max_len - charstring_len, |
| 449 |
data, size, |
| 450 |
cffont->gsubr, cffont->subrs[0], |
| 451 |
default_width, nominal_width, &ginfo); |
| 452 |
notdef_width = ginfo.wx; |
| 453 |
|
| 454 |
/* |
| 455 |
* Subset font |
| 456 |
*/ |
| 457 |
num_glyphs = 1; |
| 458 |
pdfcharset = pdf_new_stream(0); |
| 459 |
for (code = 0; code < 256; code++) { |
| 460 |
card16 gid, j; |
| 461 |
s_SID sid_orig, sid; |
| 462 |
|
| 463 |
widths[code] = notdef_width; |
| 464 |
|
| 465 |
if (!usedchars[code] || !enc_vec[code] || |
| 466 |
!strcmp(enc_vec[code], ".notdef")) |
| 467 |
continue; |
| 468 |
|
| 469 |
/* |
| 470 |
* FIXME: |
| 471 |
* cff_get_sid() obtain SID from original String INDEX. |
| 472 |
* It should be cff_string_get_sid(string, ...). |
| 473 |
* cff_add_string(cff, ...) -> cff_string_add(string, ...). |
| 474 |
*/ |
| 475 |
sid_orig = cff_get_sid (cffont, enc_vec[code]); |
| 476 |
sid = sid_orig < CFF_STDSTR_MAX ? |
| 477 |
sid_orig : cff_add_string(cffont, enc_vec[code], 0); |
| 478 |
/* |
| 479 |
* We use "unique = 0" because duplicate strings are impossible |
| 480 |
* at this stage unless the original font already had duplicates. |
| 481 |
*/ |
| 482 |
|
| 483 |
/* |
| 484 |
* Check if multiply-encoded glyph. |
| 485 |
*/ |
| 486 |
for (j = 0; j < charset->num_entries; j++) { |
| 487 |
if (sid == charset->data.glyphs[j]) { |
| 488 |
/* Already have this glyph. */ |
| 489 |
encoding->supp[encoding->num_supps].code = code; |
| 490 |
encoding->supp[encoding->num_supps].glyph = sid; |
| 491 |
usedchars[code] = 0; /* Used but multiply-encoded. */ |
| 492 |
encoding->num_supps += 1; |
| 493 |
break; |
| 494 |
} |
| 495 |
} |
| 496 |
if (j < charset->num_entries) { |
| 497 |
continue; /* Prevent duplication. */ |
| 498 |
} |
| 499 |
|
| 500 |
/* This is new encoding entry. */ |
| 501 |
gid = cff_charsets_lookup(cffont, sid_orig); /* FIXME */ |
| 502 |
if (gid == 0) { |
| 503 |
WARN("Glyph \"%s\" missing in font \"%s\".", enc_vec[code], fontname); |
| 504 |
WARN("Maybe incorrect encoding specified."); |
| 505 |
usedchars[code] = 0; /* Set unused for writing correct encoding */ |
| 506 |
continue; |
| 507 |
} |
| 508 |
pdf_add_stream(pdfcharset, "/", 1); |
| 509 |
pdf_add_stream(pdfcharset, enc_vec[code], strlen(enc_vec[code])); |
| 510 |
|
| 511 |
if (dpx_conf.verbose_level > 2) { |
| 512 |
MESG("/%s", enc_vec[code]); |
| 513 |
} |
| 514 |
|
| 515 |
size = cs_idx->offset[gid+1] - cs_idx->offset[gid]; |
| 516 |
if (size > CS_STR_LEN_MAX) { |
| 517 |
ERROR("Charstring too long: gid=%u, %ld bytes", gid, size); |
| 518 |
} |
| 519 |
|
| 520 |
if (charstring_len + CS_STR_LEN_MAX >= max_len) { |
| 521 |
max_len = charstring_len + 2 * CS_STR_LEN_MAX; |
| 522 |
charstrings->data = RENEW(charstrings->data, max_len, card8); |
| 523 |
} |
| 524 |
charstrings->offset[num_glyphs] = charstring_len + 1; |
| 525 |
cff_seek(cffont, offset + cs_idx->offset[gid] - 1); |
| 526 |
cff_read_data(data, size, cffont); |
| 527 |
charstring_len += cs_copy_charstring(charstrings->data + charstring_len, |
| 528 |
max_len - charstring_len, |
| 529 |
data, size, |
| 530 |
cffont->gsubr, cffont->subrs[0], |
| 531 |
default_width, nominal_width, &ginfo); |
| 532 |
widths[code] = ginfo.wx; |
| 533 |
charset->data.glyphs[charset->num_entries] = sid; |
| 534 |
charset->num_entries += 1; |
| 535 |
num_glyphs++; |
| 536 |
} |
| 537 |
if (dpx_conf.verbose_level > 2) { |
| 538 |
MESG("]"); |
| 539 |
} |
| 540 |
RELEASE(data); |
| 541 |
|
| 542 |
/* |
| 543 |
* Now we create encoding data. |
| 544 |
*/ |
| 545 |
if (encoding->num_supps > 0) |
| 546 |
encoding->format |= 0x80; /* Have supplemantary data. */ |
| 547 |
else { |
| 548 |
RELEASE(encoding->supp); /* FIXME */ |
| 549 |
} |
| 550 |
for (code = 0; code < 256; code++) { |
| 551 |
if (!usedchars[code] || |
| 552 |
!enc_vec[code] || !strcmp(enc_vec[code], ".notdef")) |
| 553 |
continue; |
| 554 |
encoding->data.range1[encoding->num_entries].first = code; |
| 555 |
encoding->data.range1[encoding->num_entries].n_left = 0; |
| 556 |
code++; |
| 557 |
while (code < 256 && usedchars[code] && |
| 558 |
enc_vec[code] && strcmp(enc_vec[code], ".notdef")) { |
| 559 |
encoding->data.range1[encoding->num_entries].n_left += 1; |
| 560 |
code++; |
| 561 |
} |
| 562 |
encoding->num_entries += 1; |
| 563 |
/* The above while() loop stopped at unused char or code == 256. */ |
| 564 |
} |
| 565 |
|
| 566 |
/* cleanup */ |
| 567 |
if (encoding_id < 0 && enc_vec) { |
| 568 |
for (code = 0; code < 256; code++) { |
| 569 |
if (enc_vec[code]) { |
| 570 |
RELEASE(enc_vec[code]); |
| 571 |
} |
| 572 |
} |
| 573 |
RELEASE(enc_vec); |
| 574 |
} |
| 575 |
|
| 576 |
cff_release_index(cs_idx); |
| 577 |
|
| 578 |
charstrings->offset[num_glyphs] = charstring_len + 1; |
| 579 |
charstrings->count = num_glyphs; |
| 580 |
charstring_len = cff_index_size(charstrings); |
| 581 |
cffont->num_glyphs = num_glyphs; |
| 582 |
|
| 583 |
/* |
| 584 |
* Discard old one, set new data. |
| 585 |
*/ |
| 586 |
if (cffont->charsets) |
| 587 |
cff_release_charsets(cffont->charsets); |
| 588 |
cffont->charsets = charset; |
| 589 |
if (cffont->encoding) |
| 590 |
cff_release_encoding(cffont->encoding); |
| 591 |
cffont->encoding = encoding; |
| 592 |
/* |
| 593 |
* We don't use subroutines at all. |
| 594 |
*/ |
| 595 |
if (cffont->gsubr) |
| 596 |
cff_release_index(cffont->gsubr); |
| 597 |
cffont->gsubr = cff_new_index(0); |
| 598 |
if (cffont->subrs[0]) |
| 599 |
cff_release_index(cffont->subrs[0]); |
| 600 |
cffont->subrs[0] = NULL; |
| 601 |
|
| 602 |
/* |
| 603 |
* Flag must be reset since cff_pack_encoding(charset) does not write |
| 604 |
* encoding(charset) if HAVE_STANDARD_ENCODING(CHARSET) is set. We are |
| 605 |
* re-encoding font. |
| 606 |
*/ |
| 607 |
cffont->flag = FONTTYPE_FONT; |
| 608 |
|
| 609 |
/* |
| 610 |
* FIXME: |
| 611 |
* Update String INDEX to delete unused strings. |
| 612 |
*/ |
| 613 |
cff_dict_update(cffont->topdict, cffont); |
| 614 |
if (cffont->private[0]) |
| 615 |
cff_dict_update(cffont->private[0], cffont); |
| 616 |
cff_update_string(cffont); |
| 617 |
|
| 618 |
/* |
| 619 |
* Calculate sizes of Top DICT and Private DICT. |
| 620 |
* All offset values in DICT are set to long (32-bit) integer |
| 621 |
* in cff_dict_pack(), those values are updated later. |
| 622 |
*/ |
| 623 |
topdict = cff_new_index(1); |
| 624 |
|
| 625 |
cff_dict_remove(cffont->topdict, "UniqueID"); |
| 626 |
cff_dict_remove(cffont->topdict, "XUID"); |
| 627 |
|
| 628 |
/* |
| 629 |
* Force existence of Encoding. |
| 630 |
*/ |
| 631 |
if (!cff_dict_known(cffont->topdict, "Encoding")) |
| 632 |
cff_dict_add(cffont->topdict, "Encoding", 1); |
| 633 |
topdict->offset[1] = cff_dict_pack(cffont->topdict, |
| 634 |
(card8 *) work_buffer, |
| 635 |
WORK_BUFFER_SIZE) + 1; |
| 636 |
private_size = 0; |
| 637 |
if (cffont->private[0]) { |
| 638 |
cff_dict_remove(cffont->private[0], "Subrs"); /* no Subrs */ |
| 639 |
private_size = cff_dict_pack(cffont->private[0], |
| 640 |
(card8 *) work_buffer, WORK_BUFFER_SIZE); |
| 641 |
} |
| 642 |
|
| 643 |
/* |
| 644 |
* Estimate total size of fontfile. |
| 645 |
*/ |
| 646 |
stream_data_len = 4; /* header size */ |
| 647 |
|
| 648 |
stream_data_len += cff_set_name(cffont, fullname); |
| 649 |
RELEASE(fullname); |
| 650 |
|
| 651 |
stream_data_len += cff_index_size(topdict); |
| 652 |
stream_data_len += cff_index_size(cffont->string); |
| 653 |
stream_data_len += cff_index_size(cffont->gsubr); |
| 654 |
|
| 655 |
/* We are using format 1 for Encoding and format 0 for charset. |
| 656 |
* TODO: Should implement cff_xxx_size(). |
| 657 |
*/ |
| 658 |
stream_data_len += 2 + (encoding->num_entries)*2 + 1 + (encoding->num_supps)*3; |
| 659 |
stream_data_len += 1 + (charset->num_entries)*2; |
| 660 |
stream_data_len += charstring_len; |
| 661 |
stream_data_len += private_size; |
| 662 |
|
| 663 |
/* |
| 664 |
* Now we create FontFile data. |
| 665 |
*/ |
| 666 |
stream_data_ptr = NEW(stream_data_len, card8); |
| 667 |
|
| 668 |
/* |
| 669 |
* Data Layout order as described in CFF spec., sec 2 "Data Layout". |
| 670 |
*/ |
| 671 |
offset = 0; |
| 672 |
/* Header */ |
| 673 |
offset += cff_put_header(cffont, stream_data_ptr + offset, stream_data_len - offset); |
| 674 |
/* Name */ |
| 675 |
offset += cff_pack_index(cffont->name, stream_data_ptr + offset, stream_data_len - offset); |
| 676 |
/* Top DICT */ |
| 677 |
topdict_offset = offset; |
| 678 |
offset += cff_index_size(topdict); |
| 679 |
/* Strings */ |
| 680 |
offset += cff_pack_index(cffont->string, |
| 681 |
stream_data_ptr + offset, stream_data_len - offset); |
| 682 |
/* Global Subrs */ |
| 683 |
offset += cff_pack_index(cffont->gsubr, |
| 684 |
stream_data_ptr + offset, stream_data_len - offset); |
| 685 |
/* Encoding */ |
| 686 |
cff_dict_set(cffont->topdict, "Encoding", 0, offset); |
| 687 |
offset += cff_pack_encoding(cffont, |
| 688 |
stream_data_ptr + offset, stream_data_len - offset); |
| 689 |
/* charset */ |
| 690 |
cff_dict_set(cffont->topdict, "charset", 0, offset); |
| 691 |
offset += cff_pack_charsets(cffont, |
| 692 |
stream_data_ptr + offset, stream_data_len - offset); |
| 693 |
/* CharStrings */ |
| 694 |
cff_dict_set(cffont->topdict, "CharStrings", 0, offset); |
| 695 |
offset += cff_pack_index(charstrings, |
| 696 |
stream_data_ptr + offset, charstring_len); |
| 697 |
cff_release_index(charstrings); |
| 698 |
/* Private */ |
| 699 |
cff_dict_set(cffont->topdict, "Private", 1, offset); |
| 700 |
if (cffont->private[0] && private_size > 0) |
| 701 |
private_size = cff_dict_pack(cffont->private[0], |
| 702 |
stream_data_ptr + offset, private_size); |
| 703 |
cff_dict_set(cffont->topdict, "Private", 0, private_size); |
| 704 |
offset += private_size; |
| 705 |
|
| 706 |
/* Finally Top DICT */ |
| 707 |
topdict->data = NEW(topdict->offset[1] - 1, card8); |
| 708 |
cff_dict_pack (cffont->topdict, topdict->data, topdict->offset[1] - 1); |
| 709 |
cff_pack_index(topdict, |
| 710 |
stream_data_ptr + topdict_offset, cff_index_size(topdict)); |
| 711 |
cff_release_index(topdict); |
| 712 |
|
| 713 |
/* Copyright and Trademark Notice ommited. */ |
| 714 |
|
| 715 |
/* Handle Widths in fontdict. */ |
| 716 |
add_SimpleMetrics(font, cffont, widths, num_glyphs); |
| 717 |
|
| 718 |
/* Close font */ |
| 719 |
cff_close (cffont); |
| 720 |
sfnt_close(sfont); |
| 721 |
|
| 722 |
if (fp) |
| 723 |
DPXFCLOSE(fp); |
| 724 |
|
| 725 |
if (dpx_conf.verbose_level > 1) { |
| 726 |
MESG("[%u/%u glyphs][%ld bytes]", num_glyphs, cs_count, offset); |
| 727 |
} |
| 728 |
|
| 729 |
/* |
| 730 |
* CharSet |
| 731 |
*/ |
| 732 |
if (pdf_check_version(2, 0) < 0) { |
| 733 |
pdf_add_dict(descriptor, |
| 734 |
pdf_new_name("CharSet"), |
| 735 |
pdf_new_string(pdf_stream_dataptr(pdfcharset), |
| 736 |
pdf_stream_length(pdfcharset))); |
| 737 |
} |
| 738 |
pdf_release_obj(pdfcharset); |
| 739 |
/* |
| 740 |
* Write PDF FontFile data. |
| 741 |
*/ |
| 742 |
fontfile = pdf_new_stream(STREAM_COMPRESS); |
| 743 |
stream_dict = pdf_stream_dict(fontfile); |
| 744 |
pdf_add_dict(descriptor, |
| 745 |
pdf_new_name("FontFile3"), pdf_ref_obj (fontfile)); |
| 746 |
pdf_add_dict(stream_dict, |
| 747 |
pdf_new_name("Subtype"), pdf_new_name("Type1C")); |
| 748 |
pdf_add_stream (fontfile, (void *) stream_data_ptr, offset); |
| 749 |
pdf_release_obj(fontfile); |
| 750 |
|
| 751 |
RELEASE(stream_data_ptr); |
| 752 |
|
| 753 |
return 0; |
| 754 |
} |