| 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 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include <math.h> |
| 29 |
|
| 30 |
#include "system.h" |
| 31 |
#include "mem.h" |
| 32 |
#include "error.h" |
| 33 |
#include "dpxconf.h" |
| 34 |
#include "dpxfile.h" |
| 35 |
|
| 36 |
#include "numbers.h" |
| 37 |
|
| 38 |
#include "pdfobj.h" |
| 39 |
#include "pdffont.h" |
| 40 |
|
| 41 |
#include "pdfencoding.h" |
| 42 |
#include "unicode.h" |
| 43 |
|
| 44 |
#include "dpxutil.h" |
| 45 |
|
| 46 |
#include "pst_obj.h" |
| 47 |
#include "pst.h" |
| 48 |
|
| 49 |
#include "cff_limits.h" |
| 50 |
#include "cff_types.h" |
| 51 |
#include "cff_dict.h" |
| 52 |
#include "cff.h" |
| 53 |
|
| 54 |
#include "t1_load.h" |
| 55 |
#include "t1_char.h" |
| 56 |
|
| 57 |
#include "type1.h" |
| 58 |
|
| 59 |
#define FONT_FLAG_FIXEDPITCH (1 << 0) /* Fixed-width font */ |
| 60 |
#define FONT_FLAG_SERIF (1 << 1) /* Serif font */ |
| 61 |
#define FONT_FLAG_SYMBOLIC (1 << 2) /* Symbolic font */ |
| 62 |
#define FONT_FLAG_SCRIPT (1 << 3) /* Script font */ |
| 63 |
#define FONT_FLAG_STANDARD (1 << 5) /* Adobe Standard Character Set */ |
| 64 |
#define FONT_FLAG_ITALIC (1 << 6) /* Italic */ |
| 65 |
#define FONT_FLAG_ALLCAP (1 << 16) /* All-cap font */ |
| 66 |
#define FONT_FLAG_SMALLCAP (1 << 17) /* Small-cap font */ |
| 67 |
#define FONT_FLAG_FORCEBOLD (1 << 18) /* Force bold at small text sizes */ |
| 68 |
|
| 69 |
static int |
| 70 |
is_basefont (const char *name) |
| 71 |
{ |
| 72 |
static const char *basefonts[] = { |
| 73 |
"Courier", "Courier-Bold", "Courier-Oblique", |
| 74 |
"Courier-BoldOblique", "Helvetica", "Helvetica-Bold", |
| 75 |
"Helvetica-Oblique", "Helvetica-BoldOblique", "Symbol", |
| 76 |
"Times-Roman", "Times-Bold", "Times-Italic", |
| 77 |
"Times-BoldItalic", "ZapfDingbats" |
| 78 |
}; |
| 79 |
int i; |
| 80 |
|
| 81 |
for (i = 0; i < 14; i++) { |
| 82 |
if (!strcmp(name, basefonts[i])) |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
int |
| 90 |
pdf_font_open_type1 (pdf_font *font, const char *ident, int index, int encoding_id, int embedding) |
| 91 |
{ |
| 92 |
FILE *fp; |
| 93 |
char fontname[PDF_NAME_LEN_MAX+1]; |
| 94 |
|
| 95 |
ASSERT(font); |
| 96 |
ASSERT(ident); |
| 97 |
|
| 98 |
if (index != 0) { |
| 99 |
WARN("Ignoring non-zero font index: %s", ident); |
| 100 |
} |
| 101 |
|
| 102 |
if (is_basefont(ident)) { |
| 103 |
font->fontname = NEW(strlen(ident)+1, char); |
| 104 |
strcpy(font->fontname, ident); |
| 105 |
font->subtype = PDF_FONT_FONTTYPE_TYPE1; |
| 106 |
font->flags |= PDF_FONT_FLAG_NOEMBED; |
| 107 |
font->flags |= PDF_FONT_FLAG_BASEFONT; |
| 108 |
} else { |
| 109 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_T1FONT); |
| 110 |
if (!fp) |
| 111 |
return -1; |
| 112 |
|
| 113 |
if (!embedding) { |
| 114 |
WARN("Ignoring no-embed option for Type1 font: %s", ident); |
| 115 |
font->flags &= ~PDF_FONT_FLAG_NOEMBED; |
| 116 |
} |
| 117 |
memset(fontname, 0, PDF_NAME_LEN_MAX+1); |
| 118 |
if (!is_pfb(fp) || t1_get_fontname(fp, fontname) < 0) { |
| 119 |
ERROR("Failed to read Type 1 font \"%s\".", ident); |
| 120 |
} |
| 121 |
DPXFCLOSE(fp); |
| 122 |
|
| 123 |
font->fontname = NEW(strlen(fontname)+1, char); |
| 124 |
strcpy(font->fontname, fontname); |
| 125 |
font->subtype = PDF_FONT_FONTTYPE_TYPE1; |
| 126 |
} |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
get_font_attr (pdf_font *font, cff_font *cffont) |
| 133 |
{ |
| 134 |
char *fontname; |
| 135 |
pdf_obj *descriptor; |
| 136 |
double capheight, ascent, descent; |
| 137 |
double italicangle, stemv; |
| 138 |
double defaultwidth, nominalwidth; |
| 139 |
int flags = 0, gid, i; |
| 140 |
static const char *L_c[] = { |
| 141 |
"H", "P", "Pi", "Rho", NULL |
| 142 |
}; |
| 143 |
static const char *L_d[] = { |
| 144 |
"p", "q", "mu", "eta", NULL |
| 145 |
}; |
| 146 |
static const char *L_a[] = { |
| 147 |
"b", "h", "lambda", NULL |
| 148 |
}; |
| 149 |
t1_ginfo gm; |
| 150 |
|
| 151 |
defaultwidth = 500.0; |
| 152 |
nominalwidth = 0.0; |
| 153 |
|
| 154 |
/* |
| 155 |
* CapHeight, Ascent, and Descent is meaningfull only for Latin/Greek/Cyrillic. |
| 156 |
* The BlueValues and OtherBlues also have those information. |
| 157 |
*/ |
| 158 |
if (cff_dict_known(cffont->topdict, "FontBBox")) { |
| 159 |
/* Default values */ |
| 160 |
capheight = ascent = cff_dict_get(cffont->topdict, "FontBBox", 3); |
| 161 |
descent = cff_dict_get(cffont->topdict, "FontBBox", 1); |
| 162 |
} else { |
| 163 |
capheight = 680.0; |
| 164 |
ascent = 690.0; |
| 165 |
descent = -190.0; |
| 166 |
} |
| 167 |
if (cff_dict_known(cffont->private[0], "StdVW")) { |
| 168 |
stemv = cff_dict_get(cffont->private[0], "StdVW", 0); |
| 169 |
} else { |
| 170 |
/* |
| 171 |
* We may use the following values for StemV: |
| 172 |
* Thin - ExtraLight: <= 50 |
| 173 |
* Light: 71 |
| 174 |
* Regular(Normal): 88 |
| 175 |
* Medium: 109 |
| 176 |
* SemiBold(DemiBold): 135 |
| 177 |
* Bold - Heavy: >= 166 |
| 178 |
*/ |
| 179 |
stemv = 88.0; |
| 180 |
} |
| 181 |
if (cff_dict_known(cffont->topdict, "ItalicAngle")) { |
| 182 |
italicangle = cff_dict_get(cffont->topdict, "ItalicAngle", 0); |
| 183 |
if (italicangle != 0.0) |
| 184 |
flags |= FONT_FLAG_ITALIC; |
| 185 |
} else { |
| 186 |
italicangle = 0.0; |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
* Use "space", "H", "p", and "b" for various values. |
| 191 |
* Those characters should not "seac". (no accent) |
| 192 |
*/ |
| 193 |
gid = cff_glyph_lookup(cffont, "space"); |
| 194 |
if (gid >= 0 && gid < cffont->cstrings->count) { |
| 195 |
t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1, |
| 196 |
cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid], |
| 197 |
cffont->subrs[0], &gm); |
| 198 |
defaultwidth = gm.wx; |
| 199 |
} |
| 200 |
|
| 201 |
for (i = 0; L_c[i] != NULL; i++) { |
| 202 |
gid = cff_glyph_lookup(cffont, L_c[i]); |
| 203 |
if (gid >= 0 && gid < cffont->cstrings->count) { |
| 204 |
t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1, |
| 205 |
cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid], |
| 206 |
cffont->subrs[0], &gm); |
| 207 |
capheight = gm.bbox.ury; |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
for (i = 0; L_d[i] != NULL; i++) { |
| 213 |
gid = cff_glyph_lookup(cffont, L_d[i]); |
| 214 |
if (gid >= 0 && gid < cffont->cstrings->count) { |
| 215 |
t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1, |
| 216 |
cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid], |
| 217 |
cffont->subrs[0], &gm); |
| 218 |
descent = gm.bbox.lly; |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
for (i = 0; L_a[i] != NULL; i++) { |
| 224 |
gid = cff_glyph_lookup(cffont, L_a[i]); |
| 225 |
if (gid >= 0 && gid < cffont->cstrings->count) { |
| 226 |
t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1, |
| 227 |
cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid], |
| 228 |
cffont->subrs[0], &gm); |
| 229 |
ascent = gm.bbox.ury; |
| 230 |
break; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if (defaultwidth != 0.0) { |
| 235 |
cff_dict_add(cffont->private[0], "defaultWidthX", 1); |
| 236 |
cff_dict_set(cffont->private[0], "defaultWidthX", 0, defaultwidth); |
| 237 |
} |
| 238 |
if (nominalwidth != 0.0) { |
| 239 |
cff_dict_add(cffont->private[0], "nominalWidthX", 1); |
| 240 |
cff_dict_set(cffont->private[0], "nominalWidthX", 0, nominalwidth); |
| 241 |
} |
| 242 |
if (cff_dict_known(cffont->private[0], "ForceBold") && |
| 243 |
cff_dict_get(cffont->private[0], "ForceBold", 0)) { |
| 244 |
flags |= FONT_FLAG_FORCEBOLD; |
| 245 |
} |
| 246 |
if (cff_dict_known(cffont->private[0], "IsFixedPitch") && |
| 247 |
cff_dict_get(cffont->private[0], "IsFixedPitch", 0)) { |
| 248 |
flags |= FONT_FLAG_FIXEDPITCH; |
| 249 |
} |
| 250 |
|
| 251 |
fontname = font->fontname; |
| 252 |
descriptor = pdf_font_get_descriptor(font); |
| 253 |
|
| 254 |
if (fontname && !strstr(fontname, "Sans")) { |
| 255 |
flags |= FONT_FLAG_SERIF; |
| 256 |
} |
| 257 |
if (fontname && strstr(fontname, "Caps")) { |
| 258 |
flags |= FONT_FLAG_SMALLCAP; |
| 259 |
} |
| 260 |
flags |= FONT_FLAG_SYMBOLIC; /* FIXME */ |
| 261 |
|
| 262 |
pdf_add_dict(descriptor, |
| 263 |
pdf_new_name("CapHeight"), pdf_new_number(capheight)); |
| 264 |
pdf_add_dict(descriptor, |
| 265 |
pdf_new_name("Ascent"), pdf_new_number(ascent)); |
| 266 |
pdf_add_dict(descriptor, |
| 267 |
pdf_new_name("Descent"), pdf_new_number(descent)); |
| 268 |
pdf_add_dict(descriptor, |
| 269 |
pdf_new_name("ItalicAngle"), pdf_new_number(italicangle)); |
| 270 |
pdf_add_dict(descriptor, |
| 271 |
pdf_new_name("StemV"), pdf_new_number(stemv)); |
| 272 |
pdf_add_dict(descriptor, |
| 273 |
pdf_new_name("Flags"), pdf_new_number(flags)); |
| 274 |
} |
| 275 |
|
| 276 |
static void |
| 277 |
add_metrics (pdf_font *font, cff_font *cffont, char **enc_vec, double *widths, int num_glyphs) |
| 278 |
{ |
| 279 |
pdf_obj *fontdict, *descriptor; |
| 280 |
pdf_obj *array; |
| 281 |
int code, firstchar, lastchar; |
| 282 |
double val; |
| 283 |
int i; |
| 284 |
char *usedchars; |
| 285 |
double scaling = 1.0; |
| 286 |
double norm_widths[256]; |
| 287 |
|
| 288 |
fontdict = pdf_font_get_resource (font); |
| 289 |
descriptor = pdf_font_get_descriptor(font); |
| 290 |
usedchars = font->usedchars; |
| 291 |
|
| 292 |
/* |
| 293 |
* The original FontBBox of the font is preserved, instead |
| 294 |
* of replacing it with tight bounding box calculated from |
| 295 |
* charstrings, to prevent Acrobat 4 from greeking text as |
| 296 |
* much as possible. |
| 297 |
*/ |
| 298 |
if (!cff_dict_known(cffont->topdict, "FontBBox")) { |
| 299 |
ERROR("No FontBBox?"); |
| 300 |
} |
| 301 |
|
| 302 |
/* The widhts array in the font dictionary must be given relative |
| 303 |
* to the default scaling of 1000:1, not relative to the scaling |
| 304 |
* given by the font matrix. |
| 305 |
*/ |
| 306 |
if (cff_dict_known(cffont->topdict, "FontMatrix")) { |
| 307 |
scaling = 1000*cff_dict_get(cffont->topdict, "FontMatrix", 0); |
| 308 |
} else { |
| 309 |
scaling = 1; |
| 310 |
} |
| 311 |
|
| 312 |
array = pdf_new_array(); |
| 313 |
for (i = 0; i < 4; i++) { |
| 314 |
val = cff_dict_get(cffont->topdict, "FontBBox", i); |
| 315 |
pdf_add_array(array, pdf_new_number(ROUND(val, 1.0))); |
| 316 |
} |
| 317 |
pdf_add_dict(descriptor, pdf_new_name("FontBBox"), pdf_link_obj(array)); |
| 318 |
pdf_release_obj(array); |
| 319 |
|
| 320 |
array = pdf_new_array(); |
| 321 |
if (num_glyphs <= 1) { /* This must be an error. */ |
| 322 |
firstchar = lastchar = 0; |
| 323 |
pdf_add_array(array, pdf_new_number(0.0)); |
| 324 |
} else { |
| 325 |
for (firstchar = 255, lastchar = 0, code = 0; code < 256; code++) { |
| 326 |
if (usedchars[code]) { |
| 327 |
if (code < firstchar) firstchar = code; |
| 328 |
if (code > lastchar) lastchar = code; |
| 329 |
norm_widths[code] = scaling * widths[cff_glyph_lookup(cffont, enc_vec[code])]; |
| 330 |
} |
| 331 |
} |
| 332 |
if (firstchar > lastchar) { |
| 333 |
WARN("No glyphs actually used???"); |
| 334 |
pdf_release_obj(array); |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
pdf_check_tfm_widths(font->ident, norm_widths, firstchar, lastchar, usedchars); |
| 339 |
|
| 340 |
for (code = firstchar; code <= lastchar; code++) { |
| 341 |
if (usedchars[code]) { |
| 342 |
pdf_add_array(array, pdf_new_number(ROUND(norm_widths[code], 0.1))); |
| 343 |
} else { |
| 344 |
pdf_add_array(array, pdf_new_number(0.0)); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if (pdf_array_length(array) > 0) { |
| 350 |
pdf_add_dict(fontdict, pdf_new_name("Widths"), pdf_ref_obj(array)); |
| 351 |
} |
| 352 |
pdf_release_obj(array); |
| 353 |
|
| 354 |
pdf_add_dict(fontdict, pdf_new_name("FirstChar"), pdf_new_number(firstchar)); |
| 355 |
pdf_add_dict(fontdict, pdf_new_name("LastChar"), pdf_new_number(lastchar)); |
| 356 |
|
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
static int |
| 362 |
#if defined(LIBDPX) |
| 363 |
write_fontfile (pdf_font *font, cff_font *cffont) |
| 364 |
#else |
| 365 |
write_fontfile (pdf_font *font, cff_font *cffont, pdf_obj *pdfcharset) |
| 366 |
#endif /* LIBDPX */ |
| 367 |
{ |
| 368 |
pdf_obj *descriptor; |
| 369 |
pdf_obj *fontfile, *stream_dict; |
| 370 |
cff_index *topdict; |
| 371 |
int private_size, stream_data_len, charstring_len; |
| 372 |
int topdict_offset, offset; |
| 373 |
#define WBUF_SIZE 1024 |
| 374 |
card8 *stream_data_ptr, wbuf[WBUF_SIZE]; |
| 375 |
|
| 376 |
descriptor = pdf_font_get_descriptor(font); |
| 377 |
|
| 378 |
topdict = cff_new_index(1); |
| 379 |
/* |
| 380 |
* Force existence of Encoding. |
| 381 |
*/ |
| 382 |
if (!cff_dict_known(cffont->topdict, "CharStrings")) |
| 383 |
cff_dict_add(cffont->topdict, "CharStrings", 1); |
| 384 |
if (!cff_dict_known(cffont->topdict, "charset")) |
| 385 |
cff_dict_add(cffont->topdict, "charset", 1); |
| 386 |
if (!cff_dict_known(cffont->topdict, "Encoding")) |
| 387 |
cff_dict_add(cffont->topdict, "Encoding", 1); |
| 388 |
private_size = cff_dict_pack((cffont->private)[0], wbuf, WBUF_SIZE); |
| 389 |
/* Private dict is required (but may have size 0) */ |
| 390 |
if (!cff_dict_known(cffont->topdict, "Private")) |
| 391 |
cff_dict_add(cffont->topdict, "Private", 2); |
| 392 |
topdict->offset[1] = cff_dict_pack(cffont->topdict, wbuf, WBUF_SIZE) + 1; |
| 393 |
|
| 394 |
/* |
| 395 |
* Estimate total size of fontfile. |
| 396 |
*/ |
| 397 |
charstring_len = cff_index_size(cffont->cstrings); |
| 398 |
|
| 399 |
stream_data_len = 4; /* header size */ |
| 400 |
stream_data_len += cff_index_size(cffont->name); |
| 401 |
stream_data_len += cff_index_size(topdict); |
| 402 |
stream_data_len += cff_index_size(cffont->string); |
| 403 |
stream_data_len += cff_index_size(cffont->gsubr); |
| 404 |
/* We are using format 1 for Encoding and format 0 for charset. |
| 405 |
* TODO: Should implement cff_xxx_size(). |
| 406 |
*/ |
| 407 |
stream_data_len += 2 + (cffont->encoding->num_entries)*2 + 1 + (cffont->encoding->num_supps)*3; |
| 408 |
stream_data_len += 1 + (cffont->charsets->num_entries)*2; |
| 409 |
stream_data_len += charstring_len; |
| 410 |
stream_data_len += private_size; |
| 411 |
|
| 412 |
/* |
| 413 |
* Now we create FontFile data. |
| 414 |
*/ |
| 415 |
stream_data_ptr = NEW(stream_data_len, card8); |
| 416 |
/* |
| 417 |
* Data Layout order as described in CFF spec., sec 2 "Data Layout". |
| 418 |
*/ |
| 419 |
offset = 0; |
| 420 |
/* Header */ |
| 421 |
offset += cff_put_header(cffont, |
| 422 |
stream_data_ptr + offset, stream_data_len - offset); |
| 423 |
/* Name */ |
| 424 |
offset += cff_pack_index(cffont->name, |
| 425 |
stream_data_ptr + offset, stream_data_len - offset); |
| 426 |
/* Top DICT */ |
| 427 |
topdict_offset = offset; |
| 428 |
offset += cff_index_size(topdict); |
| 429 |
/* Strings */ |
| 430 |
offset += cff_pack_index(cffont->string, |
| 431 |
stream_data_ptr + offset, stream_data_len - offset); |
| 432 |
/* Global Subrs */ |
| 433 |
offset += cff_pack_index(cffont->gsubr, |
| 434 |
stream_data_ptr + offset, stream_data_len - offset); |
| 435 |
/* Encoding */ |
| 436 |
/* TODO: don't write Encoding entry if the font is always used |
| 437 |
* with PDF Encoding information. Applies to type1c.c as well. |
| 438 |
*/ |
| 439 |
cff_dict_set(cffont->topdict, "Encoding", 0, offset); |
| 440 |
offset += cff_pack_encoding(cffont, |
| 441 |
stream_data_ptr + offset, stream_data_len - offset); |
| 442 |
/* charset */ |
| 443 |
cff_dict_set(cffont->topdict, "charset", 0, offset); |
| 444 |
offset += cff_pack_charsets(cffont, |
| 445 |
stream_data_ptr + offset, stream_data_len - offset); |
| 446 |
/* CharStrings */ |
| 447 |
cff_dict_set(cffont->topdict, "CharStrings", 0, offset); |
| 448 |
offset += cff_pack_index(cffont->cstrings, |
| 449 |
stream_data_ptr + offset, charstring_len); |
| 450 |
/* Private */ |
| 451 |
if ((cffont->private)[0] && private_size > 0) { |
| 452 |
private_size = cff_dict_pack(cffont->private[0], |
| 453 |
stream_data_ptr + offset, private_size); |
| 454 |
cff_dict_set(cffont->topdict, "Private", 1, offset); |
| 455 |
cff_dict_set(cffont->topdict, "Private", 0, private_size); |
| 456 |
} |
| 457 |
offset += private_size; |
| 458 |
|
| 459 |
/* Finally Top DICT */ |
| 460 |
topdict->data = NEW(topdict->offset[1] - 1, card8); |
| 461 |
cff_dict_pack (cffont->topdict, topdict->data, topdict->offset[1] - 1); |
| 462 |
cff_pack_index(topdict, |
| 463 |
stream_data_ptr + topdict_offset, cff_index_size(topdict)); |
| 464 |
cff_release_index(topdict); |
| 465 |
|
| 466 |
/* Copyright and Trademark Notice ommited. */ |
| 467 |
|
| 468 |
/* Flush Font File */ |
| 469 |
fontfile = pdf_new_stream(STREAM_COMPRESS); |
| 470 |
stream_dict = pdf_stream_dict(fontfile); |
| 471 |
pdf_add_dict(descriptor, |
| 472 |
pdf_new_name("FontFile3"), pdf_ref_obj (fontfile)); |
| 473 |
pdf_add_dict(stream_dict, |
| 474 |
pdf_new_name("Subtype"), pdf_new_name("Type1C")); |
| 475 |
pdf_add_stream (fontfile, (void *) stream_data_ptr, offset); |
| 476 |
pdf_release_obj(fontfile); |
| 477 |
#if !defined(LIBDPX) |
| 478 |
if (pdf_check_version(2, 0) < 0) { |
| 479 |
pdf_add_dict(descriptor, |
| 480 |
pdf_new_name("CharSet"), |
| 481 |
pdf_new_string(pdf_stream_dataptr(pdfcharset), |
| 482 |
pdf_stream_length(pdfcharset))); |
| 483 |
} |
| 484 |
#endif /* !LIBDPX */ |
| 485 |
RELEASE(stream_data_ptr); |
| 486 |
|
| 487 |
return offset; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
int |
| 492 |
pdf_font_load_type1 (pdf_font *font) |
| 493 |
{ |
| 494 |
pdf_obj *fontdict; |
| 495 |
#if !defined(LIBDPX) |
| 496 |
pdf_obj *pdfcharset; /* Actually string object */ |
| 497 |
#endif /* !LIBDPX */ |
| 498 |
int encoding_id; |
| 499 |
char *usedchars, *ident; |
| 500 |
char *fontname, *uniqueTag; |
| 501 |
char *fullname; /* With pseudo unique tag */ |
| 502 |
cff_font *cffont; |
| 503 |
cff_charsets *charset; |
| 504 |
char **enc_vec; |
| 505 |
double defaultwidth, nominalwidth; |
| 506 |
double *widths; |
| 507 |
card16 *GIDMap, num_glyphs = 0; |
| 508 |
FILE *fp; |
| 509 |
int offset; |
| 510 |
int code; |
| 511 |
|
| 512 |
ASSERT(font); |
| 513 |
|
| 514 |
if (!font->reference) |
| 515 |
return 0; |
| 516 |
|
| 517 |
fontdict = pdf_font_get_resource(font); |
| 518 |
encoding_id = font->encoding_id; |
| 519 |
usedchars = font->usedchars; |
| 520 |
ident = font->filename; |
| 521 |
fontname = font->fontname; |
| 522 |
uniqueTag = pdf_font_get_uniqueTag(font); |
| 523 |
|
| 524 |
ASSERT(usedchars); |
| 525 |
ASSERT(ident); |
| 526 |
ASSERT(fontname); |
| 527 |
|
| 528 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_T1FONT); |
| 529 |
if (!fp) { |
| 530 |
ERROR("Type1: Could not open Type1 font: %s", ident); |
| 531 |
} |
| 532 |
|
| 533 |
GIDMap = NULL; |
| 534 |
num_glyphs = 0; |
| 535 |
|
| 536 |
if (encoding_id >= 0) { |
| 537 |
enc_vec = NULL; |
| 538 |
} else { |
| 539 |
enc_vec = NEW(256, char *); |
| 540 |
for (code = 0; code <= 0xff; code++) { |
| 541 |
enc_vec[code] = NULL; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
cffont = t1_load_font(enc_vec, 0, fp); |
| 546 |
if (!cffont) { |
| 547 |
ERROR("Could not load Type 1 font: %s", ident); |
| 548 |
} |
| 549 |
DPXFCLOSE(fp); |
| 550 |
|
| 551 |
fullname = NEW(strlen(fontname) + 8, char); |
| 552 |
sprintf(fullname, "%6s+%s", uniqueTag, fontname); |
| 553 |
|
| 554 |
/* |
| 555 |
* Encoding related things. |
| 556 |
*/ |
| 557 |
if (encoding_id >= 0) { |
| 558 |
enc_vec = pdf_encoding_get_encoding(encoding_id); |
| 559 |
} else { |
| 560 |
pdf_obj *tounicode; |
| 561 |
|
| 562 |
/* |
| 563 |
* Create enc_vec and ToUnicode CMap for built-in encoding. |
| 564 |
*/ |
| 565 |
if (!pdf_lookup_dict(fontdict, "ToUnicode")) { |
| 566 |
tounicode = pdf_create_ToUnicode_CMap(fullname, |
| 567 |
enc_vec, usedchars); |
| 568 |
if (tounicode) { |
| 569 |
pdf_add_dict(fontdict, |
| 570 |
pdf_new_name("ToUnicode"), |
| 571 |
pdf_ref_obj (tounicode)); |
| 572 |
pdf_release_obj(tounicode); |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
cff_set_name(cffont, fullname); |
| 578 |
RELEASE(fullname); |
| 579 |
|
| 580 |
/* defaultWidthX, CapHeight, etc. */ |
| 581 |
get_font_attr(font, cffont); |
| 582 |
if (cff_dict_known(cffont->private[0], "defaultWidthX")) { |
| 583 |
defaultwidth = cff_dict_get(cffont->private[0], "defaultWidthX", 0); |
| 584 |
} else { |
| 585 |
defaultwidth = 0.0; |
| 586 |
} |
| 587 |
if (cff_dict_known(cffont->private[0], "nominalWidthX")) { |
| 588 |
nominalwidth = cff_dict_get(cffont->private[0], "nominalWidthX", 0); |
| 589 |
} else { |
| 590 |
nominalwidth = 0.0; |
| 591 |
} |
| 592 |
|
| 593 |
/* Create CFF encoding, charset, sort glyphs */ |
| 594 |
#define MAX_GLYPHS 1024 |
| 595 |
GIDMap = NEW(MAX_GLYPHS, card16); |
| 596 |
#if !defined(LIBDPX) |
| 597 |
pdfcharset = pdf_new_stream(0); |
| 598 |
#endif /* !LIBDPX */ |
| 599 |
{ |
| 600 |
int prev, duplicate; |
| 601 |
int gid; |
| 602 |
char *glyph; |
| 603 |
s_SID sid; |
| 604 |
|
| 605 |
cffont->encoding = NEW(1, cff_encoding); |
| 606 |
cffont->encoding->format = 1; |
| 607 |
cffont->encoding->num_entries = 0; |
| 608 |
cffont->encoding->data.range1 = NEW(256, cff_range1); |
| 609 |
cffont->encoding->num_supps = 0; |
| 610 |
cffont->encoding->supp = NEW(256, cff_map); |
| 611 |
|
| 612 |
charset = NEW(1, cff_charsets); |
| 613 |
charset->format = 0; |
| 614 |
charset->num_entries = 0; |
| 615 |
charset->data.glyphs = NEW(MAX_GLYPHS, s_SID); |
| 616 |
|
| 617 |
gid = cff_glyph_lookup(cffont, ".notdef"); |
| 618 |
if (gid < 0) |
| 619 |
ERROR("Type 1 font with no \".notdef\" glyph???"); |
| 620 |
GIDMap[0] = (card16) gid; |
| 621 |
if (dpx_conf.verbose_level > 2) |
| 622 |
MESG("[glyphs:/.notdef"); |
| 623 |
num_glyphs = 1; |
| 624 |
for (prev = -2, code = 0; code <= 0xff; code++) { |
| 625 |
glyph = enc_vec[code]; |
| 626 |
|
| 627 |
if (!usedchars[code]) |
| 628 |
continue; |
| 629 |
if (glyph && !strcmp(glyph, ".notdef")) { |
| 630 |
WARN("Character mapped to .notdef used in font: %s", |
| 631 |
fontname); |
| 632 |
usedchars[code] = 0; |
| 633 |
continue; |
| 634 |
} |
| 635 |
|
| 636 |
gid = cff_glyph_lookup(cffont, glyph); |
| 637 |
if (gid < 1 || gid >= cffont->cstrings->count) { |
| 638 |
WARN("Glyph \"%s\" missing in font \"%s\".", glyph, fontname); |
| 639 |
usedchars[code] = 0; |
| 640 |
continue; |
| 641 |
} |
| 642 |
|
| 643 |
for (duplicate = 0; duplicate < code; duplicate++) { |
| 644 |
if (usedchars[duplicate] && |
| 645 |
enc_vec[duplicate] && !strcmp(enc_vec[duplicate], glyph)) |
| 646 |
break; |
| 647 |
} |
| 648 |
|
| 649 |
sid = cff_add_string(cffont, glyph, 1); /* FIXME */ |
| 650 |
if (duplicate < code) { /* found duplicates */ |
| 651 |
cffont->encoding->supp[cffont->encoding->num_supps].code = duplicate; |
| 652 |
cffont->encoding->supp[cffont->encoding->num_supps].glyph = sid; |
| 653 |
cffont->encoding->num_supps += 1; |
| 654 |
} else { |
| 655 |
GIDMap[num_glyphs] = (card16) gid; |
| 656 |
charset->data.glyphs[charset->num_entries] = sid; |
| 657 |
charset->num_entries += 1; |
| 658 |
if (code != prev + 1) { |
| 659 |
cffont->encoding->num_entries += 1; |
| 660 |
cffont->encoding->data.range1[cffont->encoding->num_entries-1].first = code; |
| 661 |
cffont->encoding->data.range1[cffont->encoding->num_entries-1].n_left = 0; |
| 662 |
} else { |
| 663 |
cffont->encoding->data.range1[cffont->encoding->num_entries-1].n_left += 1; |
| 664 |
} |
| 665 |
prev = code; |
| 666 |
num_glyphs++; |
| 667 |
|
| 668 |
if (dpx_conf.verbose_level > 2) { |
| 669 |
MESG("/%s", glyph); |
| 670 |
} |
| 671 |
#if !defined(LIBDPX) |
| 672 |
/* CharSet is actually string object. */ |
| 673 |
{ |
| 674 |
pdf_add_stream(pdfcharset, "/", 1); |
| 675 |
pdf_add_stream(pdfcharset, glyph, strlen(glyph)); |
| 676 |
} |
| 677 |
#endif /* !LIBDPX */ |
| 678 |
} |
| 679 |
} |
| 680 |
if (cffont->encoding->num_supps > 0) { |
| 681 |
cffont->encoding->format |= 0x80; |
| 682 |
} else { |
| 683 |
RELEASE(cffont->encoding->supp); /* FIXME */ |
| 684 |
cffont->encoding->supp = NULL; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
widths = NEW(cffont->cstrings->count, double); |
| 689 |
/* |
| 690 |
* No more string will be added. |
| 691 |
* The Type 1 seac operator may add another glyph but the glyph name of |
| 692 |
* those glyphs are contained in standard string. The String Index will |
| 693 |
* not be modified after here. |
| 694 |
* BUT: We cannot update the String Index yet because then we wouldn't be |
| 695 |
* able to find the GIDs of the base and accent characters (unless they |
| 696 |
* have been used already). |
| 697 |
*/ |
| 698 |
|
| 699 |
{ |
| 700 |
cff_index *cstring; |
| 701 |
t1_ginfo gm; |
| 702 |
card16 gid, gid_orig; |
| 703 |
int dstlen_max, srclen; |
| 704 |
card8 *srcptr, *dstptr; |
| 705 |
|
| 706 |
offset = dstlen_max = 0L; |
| 707 |
cstring = cff_new_index(cffont->cstrings->count); |
| 708 |
cstring->data = NULL; |
| 709 |
cstring->offset[0] = 1; |
| 710 |
|
| 711 |
/* The num_glyphs increases if "seac" operators are used. */ |
| 712 |
for (gid = 0; gid < num_glyphs; gid++) { |
| 713 |
if (offset + CS_STR_LEN_MAX >= dstlen_max) { |
| 714 |
dstlen_max += CS_STR_LEN_MAX * 2; |
| 715 |
cstring->data = RENEW(cstring->data, dstlen_max, card8); |
| 716 |
} |
| 717 |
gid_orig = GIDMap[gid]; |
| 718 |
|
| 719 |
dstptr = cstring->data + cstring->offset[gid] - 1; |
| 720 |
srcptr = cffont->cstrings->data + cffont->cstrings->offset[gid_orig] - 1; |
| 721 |
srclen = cffont->cstrings->offset[gid_orig + 1] - cffont->cstrings->offset[gid_orig]; |
| 722 |
|
| 723 |
offset += t1char_convert_charstring(dstptr, CS_STR_LEN_MAX, |
| 724 |
srcptr, srclen, |
| 725 |
cffont->subrs[0], defaultwidth, nominalwidth, &gm); |
| 726 |
cstring->offset[gid + 1] = offset + 1; |
| 727 |
if (gm.use_seac) { |
| 728 |
int bchar_gid, achar_gid, i; |
| 729 |
const char *bchar_name, *achar_name; |
| 730 |
|
| 731 |
/* |
| 732 |
* NOTE: |
| 733 |
* 1. seac.achar and seac.bchar must be contained in the CFF standard string. |
| 734 |
* 2. Those characters need not to be encoded. |
| 735 |
* 3. num_glyphs == charsets->num_entries + 1. |
| 736 |
*/ |
| 737 |
achar_name = t1_get_standard_glyph(gm.seac.achar); |
| 738 |
achar_gid = cff_glyph_lookup(cffont, achar_name); |
| 739 |
bchar_name = t1_get_standard_glyph(gm.seac.bchar); |
| 740 |
bchar_gid = cff_glyph_lookup(cffont, bchar_name); |
| 741 |
if (achar_gid < 0) { |
| 742 |
WARN("Accent char \"%s\" not found. Invalid use of \"seac\" operator.", |
| 743 |
achar_name); |
| 744 |
continue; |
| 745 |
} |
| 746 |
if (bchar_gid < 0) { |
| 747 |
WARN("Base char \"%s\" not found. Invalid use of \"seac\" operator.", |
| 748 |
bchar_name); |
| 749 |
continue; |
| 750 |
} |
| 751 |
|
| 752 |
for (i = 0; i < num_glyphs; i++) { |
| 753 |
if (GIDMap[i] == achar_gid) |
| 754 |
break; |
| 755 |
} |
| 756 |
if (i == num_glyphs) { |
| 757 |
if (dpx_conf.verbose_level > 2) |
| 758 |
MESG("/%s", achar_name); |
| 759 |
GIDMap[num_glyphs++] = achar_gid; |
| 760 |
charset->data.glyphs[charset->num_entries] = cff_get_seac_sid(cffont, achar_name); |
| 761 |
charset->num_entries += 1; |
| 762 |
#if !defined(LIBDPX) |
| 763 |
/* CharSet is actually string object. */ |
| 764 |
{ |
| 765 |
pdf_add_stream(pdfcharset, "/", 1); |
| 766 |
pdf_add_stream(pdfcharset, achar_name, strlen(achar_name)); |
| 767 |
} |
| 768 |
#endif /* !LIBDPX */ |
| 769 |
} |
| 770 |
|
| 771 |
for (i = 0; i < num_glyphs; i++) { |
| 772 |
if (GIDMap[i] == bchar_gid) |
| 773 |
break; |
| 774 |
} |
| 775 |
if (i == num_glyphs) { |
| 776 |
if (dpx_conf.verbose_level > 2) |
| 777 |
MESG("/%s", bchar_name); |
| 778 |
GIDMap[num_glyphs++] = bchar_gid; |
| 779 |
charset->data.glyphs[charset->num_entries] = cff_get_seac_sid(cffont, bchar_name); |
| 780 |
charset->num_entries += 1; |
| 781 |
#if !defined(LIBDPX) |
| 782 |
/* CharSet is actually string object. */ |
| 783 |
{ |
| 784 |
pdf_add_stream(pdfcharset, "/", 1); |
| 785 |
pdf_add_stream(pdfcharset, bchar_name, strlen(bchar_name)); |
| 786 |
} |
| 787 |
#endif /* !LIBDPX */ |
| 788 |
} |
| 789 |
} |
| 790 |
widths[gid] = gm.wx; |
| 791 |
} |
| 792 |
cstring->count = num_glyphs; |
| 793 |
|
| 794 |
cff_release_index(cffont->subrs[0]); |
| 795 |
cffont->subrs[0] = NULL; |
| 796 |
RELEASE(cffont->subrs); |
| 797 |
cffont->subrs = NULL; |
| 798 |
|
| 799 |
cff_release_index(cffont->cstrings); |
| 800 |
cffont->cstrings = cstring; |
| 801 |
|
| 802 |
cff_release_charsets(cffont->charsets); |
| 803 |
cffont->charsets = charset; |
| 804 |
} |
| 805 |
if (dpx_conf.verbose_level > 2) |
| 806 |
MESG("]"); |
| 807 |
|
| 808 |
/* Now we can update the String Index */ |
| 809 |
cff_dict_update (cffont->topdict, cffont); |
| 810 |
cff_dict_update (cffont->private[0], cffont); |
| 811 |
cff_update_string(cffont); |
| 812 |
|
| 813 |
add_metrics(font, cffont, enc_vec, widths, num_glyphs); |
| 814 |
|
| 815 |
#if defined(LIBDPX) |
| 816 |
offset = write_fontfile(font, cffont); |
| 817 |
#else |
| 818 |
offset = write_fontfile(font, cffont, pdfcharset); |
| 819 |
#endif /* LIBDPX */ |
| 820 |
if (dpx_conf.verbose_level > 1) |
| 821 |
MESG("[%u glyphs][%ld bytes]", num_glyphs, offset); |
| 822 |
|
| 823 |
#if !defined(LIBDPX) |
| 824 |
pdf_release_obj(pdfcharset); |
| 825 |
#endif /* LIBDPX */ |
| 826 |
|
| 827 |
cff_close(cffont); |
| 828 |
|
| 829 |
/* Cleanup */ |
| 830 |
if (encoding_id < 0 && enc_vec) { |
| 831 |
for (code = 0; code < 256; code++) { |
| 832 |
if (enc_vec[code]) |
| 833 |
RELEASE(enc_vec[code]); |
| 834 |
enc_vec[code] = NULL; |
| 835 |
} |
| 836 |
RELEASE(enc_vec); |
| 837 |
} |
| 838 |
if (widths) |
| 839 |
RELEASE(widths); |
| 840 |
if (GIDMap) |
| 841 |
RELEASE(GIDMap); |
| 842 |
|
| 843 |
return 0; |
| 844 |
} |