| 1 |
/* |
| 2 |
|
| 3 |
Copyright 1996-2006 Han The Thanh <[email protected]> |
| 4 |
Copyright 2006-2010 Taco Hoekwater <[email protected]> |
| 5 |
|
| 6 |
This file is part of LuaTeX. |
| 7 |
|
| 8 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 9 |
the terms of the GNU General Public License as published by the Free |
| 10 |
Software Foundation; either version 2 of the License, or (at your |
| 11 |
option) any later version. |
| 12 |
|
| 13 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 |
License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License along |
| 19 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ptexlib.h" |
| 24 |
#include "lua/luatex-api.h" |
| 25 |
|
| 26 |
int t1_wide_mode = 0 ; |
| 27 |
|
| 28 |
void write_cid_fontdictionary(PDF pdf, fo_entry * fo, internal_font_number f); |
| 29 |
|
| 30 |
static void create_cid_fontdictionary(PDF pdf, internal_font_number f); |
| 31 |
|
| 32 |
const key_entry font_key[FONT_KEYS_NUM] = { |
| 33 |
{ "Ascent", "Ascender", 1 }, |
| 34 |
{ "CapHeight", "CapHeight", 1 }, |
| 35 |
{ "Descent", "Descender", 1 }, |
| 36 |
{ "ItalicAngle", "ItalicAngle", 1 }, |
| 37 |
{ "StemV", "StdVW", 1 }, |
| 38 |
{ "XHeight", "XHeight", 1 }, |
| 39 |
{ "FontBBox", "FontBBox", 1 }, |
| 40 |
{ "", "", 0 }, |
| 41 |
{ "", "", 0 }, |
| 42 |
{ "", "", 0 }, |
| 43 |
{ "FontName", "FontName", 1 } |
| 44 |
}; |
| 45 |
|
| 46 |
/*tex A tree of font dictionaries: */ |
| 47 |
|
| 48 |
struct avl_table *fo_tree = NULL; |
| 49 |
|
| 50 |
/*tex A tree of font descriptor objects: */ |
| 51 |
|
| 52 |
struct avl_table *fd_tree = NULL; |
| 53 |
|
| 54 |
static int comp_fo_entry(const void *pa, const void *pb, void *p) |
| 55 |
{ |
| 56 |
(void) p; |
| 57 |
return strcmp(((const fo_entry *) pa)->fm->tfm_name, ((const fo_entry *) pb)->fm->tfm_name); |
| 58 |
} |
| 59 |
|
| 60 |
static int comp_fd_entry(const void *pa, const void *pb, void *p) |
| 61 |
{ |
| 62 |
const fd_entry *p1 = (const fd_entry *) pa, *p2 = (const fd_entry *) pb; |
| 63 |
(void) p; |
| 64 |
return strcmp(p1->fm->ff_name, p2->fm->ff_name); |
| 65 |
} |
| 66 |
|
| 67 |
/*tex We initialize data structure for |/Type| |/Font|: */ |
| 68 |
|
| 69 |
static fo_entry *new_fo_entry(void) |
| 70 |
{ |
| 71 |
fo_entry *fo; |
| 72 |
fo = xtalloc(1, fo_entry); |
| 73 |
fo->fo_objnum = 0; |
| 74 |
fo->tex_font = 0; |
| 75 |
fo->fm = NULL; |
| 76 |
fo->fd = NULL; |
| 77 |
fo->fe = NULL; |
| 78 |
fo->cw_objnum = 0; |
| 79 |
fo->first_char = 1; |
| 80 |
fo->last_char = 0; |
| 81 |
fo->tx_tree = NULL; |
| 82 |
fo->tounicode_objnum = 0; |
| 83 |
return fo; |
| 84 |
} |
| 85 |
|
| 86 |
/*tex We initialize data structure for |/Type| |/FontDescriptor|: */ |
| 87 |
|
| 88 |
fd_entry *new_fd_entry(internal_font_number f) |
| 89 |
{ |
| 90 |
fd_entry *fd; |
| 91 |
int i; |
| 92 |
fd = xtalloc(1, fd_entry); |
| 93 |
fd->fd_objnum = 0; |
| 94 |
fd->fontname = NULL; |
| 95 |
fd->subset_tag = NULL; |
| 96 |
fd->ff_found = false; |
| 97 |
fd->ff_objnum = 0; |
| 98 |
fd->all_glyphs = false; |
| 99 |
fd->write_ttf_glyph_names = false; |
| 100 |
for (i = 0; i < FONT_KEYS_NUM; i++) { |
| 101 |
fd->font_dim[i].val = 0; |
| 102 |
fd->font_dim[i].set = false; |
| 103 |
} |
| 104 |
fd->fe = NULL; |
| 105 |
fd->builtin_glyph_names = NULL; |
| 106 |
fd->fm = NULL; |
| 107 |
fd->tx_tree = NULL; |
| 108 |
fd->gl_tree = NULL; |
| 109 |
fd->tex_font = f; |
| 110 |
return fd; |
| 111 |
} |
| 112 |
|
| 113 |
/*tex |
| 114 |
|
| 115 |
Only fallback values of font metrics are taken from the TFM info of |f| by |
| 116 |
|preset_fontmetrics|. During reading of the font file, these values are |
| 117 |
replaced by metrics from the font, if available. |
| 118 |
|
| 119 |
*/ |
| 120 |
|
| 121 |
static void preset_fontmetrics(fd_entry * fd, internal_font_number f) |
| 122 |
{ |
| 123 |
int i; |
| 124 |
fd->font_dim[ITALIC_ANGLE_CODE].val = 0; |
| 125 |
fd->font_dim[ASCENT_CODE].val = |
| 126 |
divide_scaled(char_height(f, 'h'), font_size(f), 3); |
| 127 |
fd->font_dim[CAPHEIGHT_CODE].val = |
| 128 |
divide_scaled(char_height(f, 'H'), font_size(f), 3); |
| 129 |
i = -divide_scaled(char_depth(f, 'y'), font_size(f), 3); |
| 130 |
fd->font_dim[DESCENT_CODE].val = i < 0 ? i : 0; |
| 131 |
fd->font_dim[STEMV_CODE].val = |
| 132 |
divide_scaled(char_width(f, '.') / 3, font_size(f), 3); |
| 133 |
fd->font_dim[XHEIGHT_CODE].val = |
| 134 |
divide_scaled(get_x_height(f), font_size(f), 3); |
| 135 |
fd->font_dim[FONTBBOX1_CODE].val = 0; |
| 136 |
fd->font_dim[FONTBBOX2_CODE].val = fd->font_dim[DESCENT_CODE].val; |
| 137 |
fd->font_dim[FONTBBOX3_CODE].val = |
| 138 |
divide_scaled(get_quad(f), font_size(f), 3); |
| 139 |
fd->font_dim[FONTBBOX4_CODE].val = |
| 140 |
fd->font_dim[CAPHEIGHT_CODE].val > fd->font_dim[ASCENT_CODE].val ? |
| 141 |
fd->font_dim[CAPHEIGHT_CODE].val : fd->font_dim[ASCENT_CODE].val; |
| 142 |
for (i = 0; i < INT_KEYS_NUM; i++) |
| 143 |
fd->font_dim[i].set = true; |
| 144 |
} |
| 145 |
|
| 146 |
static void fix_fontmetrics(fd_entry * fd) |
| 147 |
{ |
| 148 |
int i; |
| 149 |
intparm *p = (intparm *) fd->font_dim; |
| 150 |
/*tex Make sure there is a rectangle. */ |
| 151 |
if (p[FONTBBOX3_CODE].val < p[FONTBBOX1_CODE].val) { |
| 152 |
i = p[FONTBBOX3_CODE].val; |
| 153 |
p[FONTBBOX3_CODE].val = p[FONTBBOX1_CODE].val; |
| 154 |
p[FONTBBOX1_CODE].val = i; |
| 155 |
} else if (p[FONTBBOX3_CODE].val == p[FONTBBOX1_CODE].val) |
| 156 |
p[FONTBBOX3_CODE].val = p[FONTBBOX1_CODE].val + 1; |
| 157 |
if (p[FONTBBOX4_CODE].val < p[FONTBBOX2_CODE].val) { |
| 158 |
i = p[FONTBBOX4_CODE].val; |
| 159 |
p[FONTBBOX4_CODE].val = p[FONTBBOX2_CODE].val; |
| 160 |
p[FONTBBOX2_CODE].val = i; |
| 161 |
} else if (p[FONTBBOX4_CODE].val == p[FONTBBOX2_CODE].val) |
| 162 |
p[FONTBBOX4_CODE].val = p[FONTBBOX2_CODE].val + 1; |
| 163 |
if (!p[ASCENT_CODE].set) { |
| 164 |
p[ASCENT_CODE].val = p[FONTBBOX4_CODE].val; |
| 165 |
p[ASCENT_CODE].set = true; |
| 166 |
} |
| 167 |
if (!p[DESCENT_CODE].set) { |
| 168 |
p[DESCENT_CODE].val = p[FONTBBOX2_CODE].val; |
| 169 |
p[DESCENT_CODE].set = true; |
| 170 |
} |
| 171 |
if (!p[CAPHEIGHT_CODE].set) { |
| 172 |
p[CAPHEIGHT_CODE].val = p[FONTBBOX4_CODE].val; |
| 173 |
p[CAPHEIGHT_CODE].set = true; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void write_fontmetrics(PDF pdf, fd_entry * fd) |
| 178 |
{ |
| 179 |
int i; |
| 180 |
fix_fontmetrics(fd); |
| 181 |
pdf_add_name(pdf, font_key[FONTBBOX1_CODE].pdfname); |
| 182 |
pdf_begin_array(pdf); |
| 183 |
/* |
| 184 |
pdf_check_space; |
| 185 |
pdf_printf(pdf, "%i %i %i %i", |
| 186 |
(int) fd->font_dim[FONTBBOX1_CODE].val, |
| 187 |
(int) fd->font_dim[FONTBBOX2_CODE].val, |
| 188 |
(int) fd->font_dim[FONTBBOX3_CODE].val, |
| 189 |
(int) fd->font_dim[FONTBBOX4_CODE].val); |
| 190 |
*/ |
| 191 |
pdf_add_int(pdf,(int) fd->font_dim[FONTBBOX1_CODE].val); |
| 192 |
pdf_add_int(pdf,(int) fd->font_dim[FONTBBOX2_CODE].val); |
| 193 |
pdf_add_int(pdf,(int) fd->font_dim[FONTBBOX3_CODE].val); |
| 194 |
pdf_add_int(pdf,(int) fd->font_dim[FONTBBOX4_CODE].val); |
| 195 |
/* */ |
| 196 |
pdf_end_array(pdf); |
| 197 |
for (i = 0; i < GEN_KEY_NUM; i++) |
| 198 |
if (fd->font_dim[i].set) |
| 199 |
pdf_dict_add_int(pdf, font_key[i].pdfname, fd->font_dim[i].val); |
| 200 |
} |
| 201 |
|
| 202 |
static void preset_fontname(fo_entry * fo, internal_font_number f) |
| 203 |
{ |
| 204 |
if (fo->fm->ps_name != NULL) { |
| 205 |
/*tex We just fallback. */ |
| 206 |
fo->fd->fontname = xstrdup(fo->fm->ps_name); |
| 207 |
} else if (font_fullname(f) != NULL) { |
| 208 |
fo->fd->fontname = xstrdup(font_fullname(f)); |
| 209 |
} else { |
| 210 |
fo->fd->fontname = xstrdup(fo->fm->tfm_name); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static void pdf_dict_add_fontname(PDF pdf, const char *key, fd_entry * fd) |
| 215 |
{ |
| 216 |
char *s; |
| 217 |
size_t l1 = 0, l2; |
| 218 |
if (fd->subset_tag != NULL) |
| 219 |
l1 = strlen(fd->subset_tag); |
| 220 |
l2 = strlen(fd->fontname); |
| 221 |
s = xmalloc(l1 + l2 + 2); |
| 222 |
if (l1 > 0) |
| 223 |
snprintf(s, l1 + l2 + 2, "%s+%s", fd->subset_tag, fd->fontname); |
| 224 |
else |
| 225 |
snprintf(s, l2 + 1, "%s", fd->fontname); |
| 226 |
pdf_dict_add_name(pdf, key, s); |
| 227 |
xfree(s); |
| 228 |
} |
| 229 |
|
| 230 |
fd_entry *lookup_fd_entry(char *s) |
| 231 |
{ |
| 232 |
fd_entry fd; |
| 233 |
fm_entry fm; |
| 234 |
fm.ff_name = s; |
| 235 |
fd.fm = &fm; |
| 236 |
if (fd_tree == NULL) { |
| 237 |
fd_tree = avl_create(comp_fd_entry, NULL, &avl_xallocator); |
| 238 |
} |
| 239 |
return (fd_entry *) avl_find(fd_tree, &fd); |
| 240 |
} |
| 241 |
|
| 242 |
static fd_entry *lookup_fontdescriptor(fo_entry * fo) |
| 243 |
{ |
| 244 |
return lookup_fd_entry(fo->fm->ff_name); |
| 245 |
} |
| 246 |
|
| 247 |
void register_fd_entry(fd_entry * fd) |
| 248 |
{ |
| 249 |
void **aa; |
| 250 |
if (fd_tree == NULL) { |
| 251 |
fd_tree = avl_create(comp_fd_entry, NULL, &avl_xallocator); |
| 252 |
} |
| 253 |
/*tex The font descriptor is not yet registered: */ |
| 254 |
if (lookup_fd_entry(fd->fm->ff_name) == NULL) { |
| 255 |
/*tex Is this a problem? */ |
| 256 |
} else { |
| 257 |
/*tex The lookup also can create */ |
| 258 |
} |
| 259 |
aa = avl_probe(fd_tree, fd); |
| 260 |
if (aa == NULL) { |
| 261 |
/*tex Is this a problem? */ |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
static void create_fontdescriptor(fo_entry * fo, internal_font_number f) |
| 266 |
{ |
| 267 |
fo->fd = new_fd_entry(f); |
| 268 |
preset_fontname(fo, f); |
| 269 |
preset_fontmetrics(fo->fd, f); |
| 270 |
/*tex An encoding is needed for \TRUETYPE\ writing: */ |
| 271 |
fo->fd->fe = fo->fe; |
| 272 |
/*tex A map entry is needed for \TRUETYPE\ writing: */ |
| 273 |
fo->fd->fm = fo->fm; |
| 274 |
fo->fd->gl_tree = avl_create(comp_string_entry, NULL, &avl_xallocator); |
| 275 |
} |
| 276 |
|
| 277 |
/*tex |
| 278 |
|
| 279 |
For all used characters of \TeX font |f|, get corresponding glyph names from |
| 280 |
external reencoding (.enc) file and collect these in the glyph tree |gl_tree| |
| 281 |
of font descriptor |fd| referenced by font dictionary |fo|. |
| 282 |
|
| 283 |
*/ |
| 284 |
|
| 285 |
static void mark_reenc_glyphs(fo_entry * fo, internal_font_number f) |
| 286 |
{ |
| 287 |
int i; |
| 288 |
char **g; |
| 289 |
void **aa; |
| 290 |
if (is_subsetted(fo->fm)) { |
| 291 |
/*tex mark glyphs from TeX (externally reencoded characters) */ |
| 292 |
g = fo->fe->glyph_names; |
| 293 |
for (i = fo->first_char; i <= fo->last_char; i++) { |
| 294 |
if (pdf_char_marked(f, i) |
| 295 |
&& g[i] != notdef |
| 296 |
&& (char *) avl_find(fo->fd->gl_tree, g[i]) == NULL) { |
| 297 |
aa = avl_probe(fo->fd->gl_tree, xstrdup(g[i])); |
| 298 |
if (aa == NULL) { |
| 299 |
/*tex Is this a problem? */ |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/*tex |
| 307 |
|
| 308 |
Function |mark_chars| has 2 uses: |
| 309 |
|
| 310 |
\startitemize[n] |
| 311 |
\startitem Mark characters as chars on \TeX\ level. \stopitem |
| 312 |
\startitem Mark encoding pairs used by \TeX\ to optimize encoding vector. \stopitem |
| 313 |
\stopitemize |
| 314 |
|
| 315 |
*/ |
| 316 |
|
| 317 |
static struct avl_table *mark_chars(fo_entry * fo, struct avl_table *tx_tree, internal_font_number f) |
| 318 |
{ |
| 319 |
int i, *j; |
| 320 |
void **aa; |
| 321 |
if (tx_tree == NULL) { |
| 322 |
tx_tree = avl_create(comp_int_entry, NULL, &avl_xallocator); |
| 323 |
assert(tx_tree != NULL); |
| 324 |
} |
| 325 |
for (i = fo->first_char; i <= fo->last_char; i++) { |
| 326 |
if (pdf_char_marked(f, i) && (int *) avl_find(tx_tree, &i) == NULL) { |
| 327 |
j = xtalloc(1, int); |
| 328 |
*j = i; |
| 329 |
aa = avl_probe(tx_tree, j); |
| 330 |
if (aa == NULL) { |
| 331 |
/*tex Is this a problem? */ |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
return tx_tree; |
| 336 |
} |
| 337 |
|
| 338 |
static void get_char_range(fo_entry * fo, internal_font_number f) |
| 339 |
{ |
| 340 |
int i; |
| 341 |
assert(fo != NULL); |
| 342 |
/*tex Search for |first_char| and |last_char|. */ |
| 343 |
for (i = font_bc(f); i <= font_ec(f); i++) |
| 344 |
if (pdf_char_marked(f, i)) |
| 345 |
break; |
| 346 |
fo->first_char = i; |
| 347 |
for (i = font_ec(f); i >= font_bc(f); i--) |
| 348 |
if (pdf_char_marked(f, i)) |
| 349 |
break; |
| 350 |
fo->last_char = i; |
| 351 |
if ((fo->first_char > fo->last_char) || !pdf_char_marked(f, fo->first_char)) { |
| 352 |
/*tex No character has been used from this font. */ |
| 353 |
fo->last_char = 0; |
| 354 |
fo->first_char = fo->last_char + 1; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
static int font_has_subset(internal_font_number f) |
| 359 |
{ |
| 360 |
int i, s; |
| 361 |
/*tex Search for |first_char| and |last_char|. */ |
| 362 |
for (i = font_bc(f); i <= font_ec(f); i++) |
| 363 |
if (pdf_char_marked(f, i)) |
| 364 |
break; |
| 365 |
s = i; |
| 366 |
for (i = font_ec(f); i >= font_bc(f); i--) |
| 367 |
if (pdf_char_marked(f, i)) |
| 368 |
break; |
| 369 |
if (s > i) |
| 370 |
return 0; |
| 371 |
else |
| 372 |
return 1; |
| 373 |
} |
| 374 |
|
| 375 |
static void write_charwidth_array(PDF pdf, fo_entry * fo, internal_font_number f) |
| 376 |
{ |
| 377 |
int i, j, *ip, *fip; |
| 378 |
struct avl_traverser t; |
| 379 |
fo->cw_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 380 |
pdf_begin_obj(pdf, fo->cw_objnum, OBJSTM_ALWAYS); |
| 381 |
avl_t_init(&t, fo->tx_tree); |
| 382 |
fip = (int *) avl_t_first(&t, fo->tx_tree); |
| 383 |
pdf_begin_array(pdf); |
| 384 |
for (ip = fip, j = *ip; ip != NULL; ip = (int *) avl_t_next(&t)) { |
| 385 |
if (ip != fip) |
| 386 |
pdf_out(pdf, ' '); |
| 387 |
i = *ip; |
| 388 |
while (j < i - 1) { |
| 389 |
pdf_puts(pdf, "0 "); |
| 390 |
j++; |
| 391 |
} |
| 392 |
j = i; |
| 393 |
pdf_print_charwidth(pdf, f, i); |
| 394 |
} |
| 395 |
pdf_end_array(pdf); |
| 396 |
pdf_end_obj(pdf); |
| 397 |
} |
| 398 |
|
| 399 |
/*tex |
| 400 |
|
| 401 |
Remark: Font objects from embedded PDF files are never registered into |
| 402 |
|fo_tree|; they are individually written out. |
| 403 |
|
| 404 |
*/ |
| 405 |
|
| 406 |
static fo_entry *lookup_fo_entry(char *s) |
| 407 |
{ |
| 408 |
fo_entry fo; |
| 409 |
fm_entry fm; |
| 410 |
fm.tfm_name = s; |
| 411 |
fo.fm = &fm; |
| 412 |
if (fo_tree == NULL) { |
| 413 |
fo_tree = avl_create(comp_fo_entry, NULL, &avl_xallocator); |
| 414 |
} |
| 415 |
return (fo_entry *) avl_find(fo_tree, &fo); |
| 416 |
} |
| 417 |
|
| 418 |
static void register_fo_entry(fo_entry * fo) |
| 419 |
{ |
| 420 |
void **aa; |
| 421 |
if (fo_tree == NULL) { |
| 422 |
fo_tree = avl_create(comp_fo_entry, NULL, &avl_xallocator); |
| 423 |
} |
| 424 |
if (lookup_fo_entry(fo->fm->tfm_name) == NULL) { |
| 425 |
/*tex Is this a problem? */ |
| 426 |
} else { |
| 427 |
/*tex The lookup also can create */ |
| 428 |
} |
| 429 |
aa = avl_probe(fo_tree, fo); |
| 430 |
if (aa == NULL) { |
| 431 |
/*tex Is this a problem? */ |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/*tex |
| 436 |
|
| 437 |
In principle we could replace the pdftex derived ttf.otf inclusion part by using |
| 438 |
the regular code for this and assigning indices and tounicodes to the character |
| 439 |
blobs, but for the moment we keep the current approach. |
| 440 |
|
| 441 |
*/ |
| 442 |
|
| 443 |
static void write_fontfile(PDF pdf, fd_entry * fd) |
| 444 |
{ |
| 445 |
if (is_cidkeyed(fd->fm)) { |
| 446 |
if (is_opentype(fd->fm)) { |
| 447 |
writetype0(pdf, fd); |
| 448 |
} else if (is_truetype(fd->fm)) { |
| 449 |
if (!writetype2(pdf, fd)) { |
| 450 |
writetype0(pdf,fd); |
| 451 |
fd->fm->type |= F_OTF; fd->fm->type ^= F_TRUETYPE; |
| 452 |
} |
| 453 |
} else if (is_type1(fd->fm)) { |
| 454 |
if (t1_wide_mode) { |
| 455 |
writet1(pdf, fd, 1); |
| 456 |
} else { |
| 457 |
writetype1w(pdf, fd); |
| 458 |
} |
| 459 |
} else { |
| 460 |
normal_error("fonts","there is a problem writing the font file (1)"); |
| 461 |
} |
| 462 |
} else { |
| 463 |
if (is_type1(fd->fm)) { |
| 464 |
writet1(pdf, fd, 0); |
| 465 |
} else if (is_truetype(fd->fm)) { |
| 466 |
writettf(pdf, fd); |
| 467 |
} else if (is_opentype(fd->fm)) { |
| 468 |
writeotf(pdf, fd); |
| 469 |
} else { |
| 470 |
normal_error("fonts","there is a problem writing the font file (2)"); |
| 471 |
} |
| 472 |
} |
| 473 |
if (!fd->ff_found) |
| 474 |
return; |
| 475 |
fd->ff_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 476 |
/*tex The font file stream: */ |
| 477 |
pdf_begin_obj(pdf, fd->ff_objnum, OBJSTM_NEVER); |
| 478 |
pdf_begin_dict(pdf); |
| 479 |
if (is_cidkeyed(fd->fm)) { |
| 480 |
/*tex No subtype is used for |TRUETYPE\ based \OPENTYPE\ fonts. */ |
| 481 |
if (is_opentype(fd->fm)) { |
| 482 |
pdf_dict_add_name(pdf, "Subtype", "CIDFontType0C"); |
| 483 |
} else if (is_type1(fd->fm)) { |
| 484 |
if (t1_wide_mode) { |
| 485 |
pdf_dict_add_int(pdf, "Length1", (int) t1_length1); |
| 486 |
pdf_dict_add_int(pdf, "Length2", (int) t1_length2); |
| 487 |
pdf_dict_add_int(pdf, "Length3", (int) t1_length3); |
| 488 |
} else { |
| 489 |
pdf_dict_add_name(pdf, "Subtype", "CIDFontType0C"); |
| 490 |
} |
| 491 |
} |
| 492 |
} else if (is_type1(fd->fm)) { |
| 493 |
pdf_dict_add_int(pdf, "Length1", (int) t1_length1); |
| 494 |
pdf_dict_add_int(pdf, "Length2", (int) t1_length2); |
| 495 |
pdf_dict_add_int(pdf, "Length3", (int) t1_length3); |
| 496 |
} else if (is_truetype(fd->fm)) { |
| 497 |
pdf_dict_add_int(pdf, "Length1", (int) ttf_length); |
| 498 |
} else if (is_opentype(fd->fm)) { |
| 499 |
pdf_dict_add_name(pdf, "Subtype", "Type1C"); |
| 500 |
} else { |
| 501 |
normal_error("fonts","there is a problem writing the font file (3)"); |
| 502 |
} |
| 503 |
pdf_dict_add_streaminfo(pdf); |
| 504 |
pdf_end_dict(pdf); |
| 505 |
pdf_begin_stream(pdf); |
| 506 |
strbuf_flush(pdf, pdf->fb); |
| 507 |
pdf_end_stream(pdf); |
| 508 |
pdf_end_obj(pdf); |
| 509 |
} |
| 510 |
|
| 511 |
int cidset = 0; |
| 512 |
|
| 513 |
static void write_fontdescriptor(PDF pdf, fd_entry * fd) |
| 514 |
{ |
| 515 |
static const int std_flags[] = { |
| 516 |
1 + 2 + (1 << 5), /* Courier */ |
| 517 |
1 + 2 + (1 << 5) + (1 << 18), /* Courier-Bold */ |
| 518 |
1 + 2 + (1 << 5) + (1 << 6), /* Courier-Oblique */ |
| 519 |
1 + 2 + (1 << 5) + (1 << 6) + (1 << 18), /* Courier-BoldOblique */ |
| 520 |
(1 << 5), /* Helvetica */ |
| 521 |
(1 << 5) + (1 << 18), /* Helvetica-Bold */ |
| 522 |
(1 << 5) + (1 << 6), /* Helvetica-Oblique */ |
| 523 |
(1 << 5) + (1 << 6) + (1 << 18), /* Helvetica-BoldOblique */ |
| 524 |
4, /* Symbol */ |
| 525 |
2 + (1 << 5), /* Times-Roman */ |
| 526 |
2 + (1 << 5) + (1 << 18), /* Times-Bold */ |
| 527 |
2 + (1 << 5) + (1 << 6), /* Times-Italic */ |
| 528 |
2 + (1 << 5) + (1 << 6) + (1 << 18), /* Times-BoldItalic */ |
| 529 |
4 /* ZapfDingbats */ |
| 530 |
}; |
| 531 |
char *glyph; |
| 532 |
struct avl_traverser t; |
| 533 |
int fd_flags; |
| 534 |
/*tex Possibly updated by |write_fontfile|: */ |
| 535 |
cidset = 0; |
| 536 |
if (fd->fd_objnum == 0) { |
| 537 |
int n = 0; |
| 538 |
int callback_id = callback_defined(font_descriptor_objnum_provider_callback); |
| 539 |
if (callback_id) { |
| 540 |
run_callback(callback_id, "S->d", fd->fontname, &n); |
| 541 |
} |
| 542 |
if (!n) { |
| 543 |
n = pdf_create_obj(pdf, obj_type_others, 0); |
| 544 |
} |
| 545 |
fd->fd_objnum = n; |
| 546 |
} |
| 547 |
if (is_fontfile(fd->fm) && is_included(fd->fm)) { |
| 548 |
/*tex This will set |fd->ff_found| if font file is found: */ |
| 549 |
write_fontfile(pdf, fd); |
| 550 |
} |
| 551 |
pdf_begin_obj(pdf, fd->fd_objnum, OBJSTM_ALWAYS); |
| 552 |
pdf_begin_dict(pdf); |
| 553 |
pdf_dict_add_name(pdf, "Type", "FontDescriptor"); |
| 554 |
pdf_dict_add_fontname(pdf, "FontName", fd); |
| 555 |
if (fd->fm->fd_flags != FD_FLAGS_NOT_SET_IN_MAPLINE) { |
| 556 |
fd_flags = (int) fd->fm->fd_flags; |
| 557 |
} else if (fd->ff_found) { |
| 558 |
fd_flags = FD_FLAGS_DEFAULT_EMBED; |
| 559 |
} else { |
| 560 |
fd_flags = is_std_t1font(fd->fm) ? std_flags[check_std_t1font(fd->fm->ps_name)] : FD_FLAGS_DEFAULT_NON_EMBED; |
| 561 |
formatted_warning("map file", |
| 562 |
"No flags specified for non-embedded font '%s' (%s), I'm using %i, fix your map entry", |
| 563 |
fd->fm->ps_name != NULL ? fd->fm->ps_name : "No name given", |
| 564 |
fd->fm->tfm_name, fd_flags); |
| 565 |
} |
| 566 |
pdf_dict_add_int(pdf, "Flags", fd_flags); |
| 567 |
write_fontmetrics(pdf, fd); |
| 568 |
if (fd->ff_found) { |
| 569 |
if (is_cidkeyed(fd->fm)) { |
| 570 |
if (is_type1(fd->fm)) { |
| 571 |
if (t1_wide_mode) { |
| 572 |
pdf_dict_add_ref(pdf, "FontFile", (int) fd->ff_objnum); |
| 573 |
} else { |
| 574 |
pdf_dict_add_ref(pdf, "FontFile3", (int) fd->ff_objnum); |
| 575 |
} |
| 576 |
} else if (is_truetype(fd->fm)) { |
| 577 |
pdf_dict_add_ref(pdf, "FontFile2", (int) fd->ff_objnum); |
| 578 |
} else if (is_opentype(fd->fm)) { |
| 579 |
pdf_dict_add_ref(pdf, "FontFile3", (int) fd->ff_objnum); |
| 580 |
} else { |
| 581 |
normal_error("fonts","there is a problem writing the font file (4)"); |
| 582 |
} |
| 583 |
} else { |
| 584 |
if (is_subsetted(fd->fm) && is_type1(fd->fm)) { |
| 585 |
/*tex |/CharSet| is optional; names may appear in any order */ |
| 586 |
if ((! pdf->omit_charset) && (pdf->major_version == 1)) { |
| 587 |
avl_t_init(&t, fd->gl_tree); |
| 588 |
pdf_add_name(pdf, "CharSet"); |
| 589 |
pdf_out(pdf, '('); |
| 590 |
for (glyph = (char *) avl_t_first(&t, fd->gl_tree); glyph != NULL; glyph = (char *) avl_t_next(&t)) { |
| 591 |
pdf_add_name(pdf, glyph); |
| 592 |
} |
| 593 |
pdf_out(pdf, ')'); |
| 594 |
pdf_set_space(pdf); |
| 595 |
} |
| 596 |
} |
| 597 |
if (is_type1(fd->fm)) { |
| 598 |
pdf_dict_add_ref(pdf, "FontFile", (int) fd->ff_objnum); |
| 599 |
} else if (is_truetype(fd->fm)) { |
| 600 |
pdf_dict_add_ref(pdf, "FontFile2", (int) fd->ff_objnum); |
| 601 |
} else if (is_opentype(fd->fm)) { |
| 602 |
pdf_dict_add_ref(pdf, "FontFile3", (int) fd->ff_objnum); |
| 603 |
} else { |
| 604 |
normal_error("fonts","there is a problem writing the font file (5)"); |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
if ((! pdf->omit_cidset) && (pdf->major_version == 1) && (cidset != 0) ) { |
| 609 |
pdf_dict_add_ref(pdf, "CIDSet", cidset); |
| 610 |
} |
| 611 |
/*tex |
| 612 |
Currently we don't export the optional keys for CID fonts like |/Style << |
| 613 |
/Panose <12-byte string> >>| and we probably never will. |
| 614 |
*/ |
| 615 |
pdf_end_dict(pdf); |
| 616 |
pdf_end_obj(pdf); |
| 617 |
} |
| 618 |
|
| 619 |
static void write_fontdescriptors(PDF pdf) |
| 620 |
{ |
| 621 |
fd_entry *fd; |
| 622 |
struct avl_traverser t; |
| 623 |
if (fd_tree == NULL) |
| 624 |
return; |
| 625 |
avl_t_init(&t, fd_tree); |
| 626 |
for (fd = (fd_entry *) avl_t_first(&t, fd_tree); fd != NULL; fd = (fd_entry *) avl_t_next(&t)) |
| 627 |
write_fontdescriptor(pdf, fd); |
| 628 |
} |
| 629 |
|
| 630 |
static void write_fontdictionary(PDF pdf, fo_entry * fo) |
| 631 |
{ |
| 632 |
/*tex Write the |/ToUnicode| entry if needed. */ |
| 633 |
if (pdf->gen_tounicode > 0 && fo->fd != NULL) { |
| 634 |
if (fo->fe != NULL) { |
| 635 |
fo->tounicode_objnum = write_tounicode(pdf, fo->fe->glyph_names, fo->fe->name, fo->tex_font); |
| 636 |
} else if (is_type1(fo->fm)) { |
| 637 |
if (fo->fd->builtin_glyph_names==NULL) { |
| 638 |
normal_error("font", "builtin glyph names is empty"); |
| 639 |
} |
| 640 |
fo->tounicode_objnum = write_tounicode(pdf, fo->fd->builtin_glyph_names, fo->fm->tfm_name, fo->tex_font); |
| 641 |
} |
| 642 |
} |
| 643 |
pdf_begin_obj(pdf, fo->fo_objnum, OBJSTM_ALWAYS); |
| 644 |
pdf_begin_dict(pdf); |
| 645 |
pdf_dict_add_name(pdf, "Type", "Font"); |
| 646 |
if (is_type1(fo->fm)) { |
| 647 |
pdf_dict_add_name(pdf, "Subtype", "Type1"); |
| 648 |
} else if (is_truetype(fo->fm)) { |
| 649 |
pdf_dict_add_name(pdf, "Subtype", "TrueType"); |
| 650 |
} else if (is_opentype(fo->fm)) { |
| 651 |
pdf_dict_add_name(pdf, "Subtype", "Type1"); |
| 652 |
} else { |
| 653 |
normal_error("fonts","there is a problem writing the font file (6)"); |
| 654 |
} |
| 655 |
pdf_dict_add_fontname(pdf, "BaseFont", fo->fd); |
| 656 |
pdf_dict_add_ref(pdf, "FontDescriptor", (int) fo->fd->fd_objnum); |
| 657 |
pdf_dict_add_int(pdf, "FirstChar", (int) fo->first_char); |
| 658 |
pdf_dict_add_int(pdf, "LastChar", (int) fo->last_char); |
| 659 |
pdf_dict_add_ref(pdf, "Widths", (int) fo->cw_objnum); |
| 660 |
if ((is_type1(fo->fm) || is_opentype(fo->fm)) && fo->fe != NULL && fo->fe->fe_objnum != 0) { |
| 661 |
pdf_dict_add_ref(pdf, "Encoding", (int) fo->fe->fe_objnum); |
| 662 |
} |
| 663 |
if (fo->tounicode_objnum != 0) { |
| 664 |
pdf_dict_add_ref(pdf, "ToUnicode", (int) fo->tounicode_objnum); |
| 665 |
} |
| 666 |
if (pdf_font_attr(fo->tex_font) != get_nullstr() && pdf_font_attr(fo->tex_font) != 0) { |
| 667 |
pdf_check_space(pdf); |
| 668 |
pdf_print(pdf, pdf_font_attr(fo->tex_font)); |
| 669 |
pdf_set_space(pdf); |
| 670 |
} |
| 671 |
pdf_end_dict(pdf); |
| 672 |
pdf_end_obj(pdf); |
| 673 |
} |
| 674 |
|
| 675 |
static void write_fontdictionaries(PDF pdf) |
| 676 |
{ |
| 677 |
fo_entry *fo; |
| 678 |
struct avl_traverser t; |
| 679 |
if (fo_tree == NULL) |
| 680 |
return; |
| 681 |
avl_t_init(&t, fo_tree); |
| 682 |
for (fo = (fo_entry *) avl_t_first(&t, fo_tree); fo != NULL; fo = (fo_entry *) avl_t_next(&t)) { |
| 683 |
write_fontdictionary(pdf, fo); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
/*tex |
| 688 |
|
| 689 |
Final flush of all font related stuff by call from \.{Output fonts |
| 690 |
definitions} elsewhere. |
| 691 |
|
| 692 |
*/ |
| 693 |
|
| 694 |
void write_fontstuff(PDF pdf) |
| 695 |
{ |
| 696 |
write_fontdescriptors(pdf); |
| 697 |
write_fontencodings(pdf); |
| 698 |
write_fontdictionaries(pdf); |
| 699 |
} |
| 700 |
|
| 701 |
static void create_fontdictionary(PDF pdf, internal_font_number f) |
| 702 |
{ |
| 703 |
fo_entry *fo = new_fo_entry(); |
| 704 |
fm_entry *fm = font_map(f); |
| 705 |
/*tex set |fo->first_char| and |fo->last_char| from |f| */ |
| 706 |
get_char_range(fo, f); |
| 707 |
if (fo->last_char > 255) |
| 708 |
fo->last_char = 255; |
| 709 |
fo->fm = fm; |
| 710 |
fo->fo_objnum = pdf_font_num(f); |
| 711 |
fo->tex_font = f; |
| 712 |
if (is_reencoded(fo->fm)) { |
| 713 |
/*tex |
| 714 |
At least the map entry tells so but it returns |NULL| if the .enc |
| 715 |
file couldn't be opened. |
| 716 |
*/ |
| 717 |
fo->fe = get_fe_entry(fo->fm->encname); |
| 718 |
if (fo->fe != NULL && (is_type1(fo->fm) || is_opentype(fo->fm))) { |
| 719 |
/*tex We don't end up here for truetype fonts. */ |
| 720 |
if (fo->fe->fe_objnum == 0) { |
| 721 |
/*tex It will be written out */ |
| 722 |
fo->fe->fe_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 723 |
} |
| 724 |
/*tex Mark encoding pairs used by TeX to optimize encoding vector. */ |
| 725 |
fo->fe->tx_tree = mark_chars(fo, fo->fe->tx_tree, f); |
| 726 |
} |
| 727 |
} |
| 728 |
/*tex For |write_charwidth_array|: */ |
| 729 |
fo->tx_tree = mark_chars(fo, fo->tx_tree, f); |
| 730 |
write_charwidth_array(pdf, fo, f); |
| 731 |
if (!is_builtin(fo->fm)) { |
| 732 |
if (is_type1(fo->fm)) { |
| 733 |
if ((fo->fd = lookup_fontdescriptor(fo)) == NULL) { |
| 734 |
create_fontdescriptor(fo, f); |
| 735 |
register_fd_entry(fo->fd); |
| 736 |
} |
| 737 |
} else { |
| 738 |
create_fontdescriptor(fo, f); |
| 739 |
} |
| 740 |
if (fo->fe != NULL) { |
| 741 |
mark_reenc_glyphs(fo, f); |
| 742 |
if (!is_type1(fo->fm)) { |
| 743 |
/*tex Mark reencoded characters as chars on TeX level. */ |
| 744 |
assert(fo->fd->tx_tree == NULL); |
| 745 |
fo->fd->tx_tree = mark_chars(fo, fo->fd->tx_tree, f); |
| 746 |
if (is_truetype(fo->fm)) { |
| 747 |
fo->fd->write_ttf_glyph_names = true; |
| 748 |
} |
| 749 |
} |
| 750 |
} else { |
| 751 |
/*tex Mark non-reencoded characters as chars on TeX level. */ |
| 752 |
fo->fd->tx_tree = mark_chars(fo, fo->fd->tx_tree, f); |
| 753 |
} |
| 754 |
if (!is_type1(fo->fm)) { |
| 755 |
write_fontdescriptor(pdf, fo->fd); |
| 756 |
} |
| 757 |
} else { |
| 758 |
/*tex |
| 759 |
Builtin fonts still need the \type {/Widths} array and \type |
| 760 |
{/FontDescriptor} (to avoid error \quotation {font FOO contains bad |
| 761 |
\type {/BBox}}). |
| 762 |
*/ |
| 763 |
create_fontdescriptor(fo, f); |
| 764 |
write_fontdescriptor(pdf, fo->fd); |
| 765 |
if (!is_std_t1font(fo->fm)) { |
| 766 |
formatted_warning("map file", "font '%s' is not a standard font; I suppose it is available to your PDF viewer then", fo->fm->ps_name); |
| 767 |
} |
| 768 |
} |
| 769 |
if (is_type1(fo->fm)) { |
| 770 |
register_fo_entry(fo); |
| 771 |
} else { |
| 772 |
write_fontdictionary(pdf, fo); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
static int has_ttf_outlines(fm_entry * fm) |
| 777 |
{ |
| 778 |
FILE *f = fopen(fm->ff_name, "rb"); |
| 779 |
if (f != NULL) { |
| 780 |
int ch1 = getc(f); |
| 781 |
int ch2 = getc(f); |
| 782 |
int ch3 = getc(f); |
| 783 |
int ch4 = getc(f); |
| 784 |
fclose(f); |
| 785 |
if (ch1 == 'O' && ch2 == 'T' && ch3 == 'T' && ch4 == 'O') |
| 786 |
return 0; |
| 787 |
return 1; |
| 788 |
} |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
void do_pdf_font(PDF pdf, internal_font_number f) |
| 793 |
{ |
| 794 |
fm_entry *fm; |
| 795 |
/*tex |
| 796 |
This is not 100\% true: CID is actually needed whenever (and only) there |
| 797 |
are more than 256 separate glyphs used. But for now, we just assume the |
| 798 |
user knows what he is doing. In practice this seems to be the case. |
| 799 |
*/ |
| 800 |
if (!font_has_subset(f)) |
| 801 |
return; |
| 802 |
if (font_encodingbytes(f) == 2) { |
| 803 |
/*tex |
| 804 |
Create a virtual font map entry, as this is needed by the rest of the |
| 805 |
font inclusion mechanism. |
| 806 |
*/ |
| 807 |
fm = font_map(f) = new_fm_entry(); |
| 808 |
/*tex Set this to a name or whatever, not a real \TFM\ anyway: */ |
| 809 |
fm->tfm_name = font_name(f); |
| 810 |
/*tex The actual file: */ |
| 811 |
fm->ff_name = font_filename(f); |
| 812 |
/*tex The true (used) name: */ |
| 813 |
if (font_psname(f) != NULL) { |
| 814 |
fm->ps_name = font_psname(f); |
| 815 |
} else { |
| 816 |
fm->ps_name = font_fullname(f); |
| 817 |
} |
| 818 |
/*tex Needed for the CIDSystemInfo: */ |
| 819 |
fm->encname = font_encodingname(f); |
| 820 |
fm->subfont = font_subfont(f); |
| 821 |
fm->slant = font_slant(f); |
| 822 |
set_slantset(fm); |
| 823 |
fm->extend = font_extend(f); |
| 824 |
set_extendset(fm); |
| 825 |
/*tex Flags can perhaps be done better. */ |
| 826 |
fm->fd_flags = 4; |
| 827 |
set_inuse(fm); |
| 828 |
switch (font_format(f)) { |
| 829 |
case opentype_format: |
| 830 |
if (has_ttf_outlines(fm)) { |
| 831 |
set_truetype(fm); |
| 832 |
} else { |
| 833 |
set_opentype(fm); |
| 834 |
} |
| 835 |
break; |
| 836 |
case truetype_format: |
| 837 |
set_truetype(fm); |
| 838 |
break; |
| 839 |
case type1_format: |
| 840 |
set_type1(fm); |
| 841 |
break; |
| 842 |
default: |
| 843 |
formatted_error("font","file format '%s' for '%s' is incompatible with wide characters", |
| 844 |
font_format_name(f), font_name(f)); |
| 845 |
} |
| 846 |
/*tex This makes \quotation {unknown} default to subsetted inclusion. */ |
| 847 |
if (font_embedding(f) != no_embedding) { |
| 848 |
set_included(fm); |
| 849 |
if (font_embedding(f) != full_embedding) { |
| 850 |
set_subsetted(fm); |
| 851 |
} |
| 852 |
} |
| 853 |
set_cidkeyed(fm); |
| 854 |
create_cid_fontdictionary(pdf, f); |
| 855 |
} else { |
| 856 |
/*tex By now |font_map(f)|, if any, should have been set via |pdf_init_font|. */ |
| 857 |
if (font_psname(f) != NULL && strstr(font_psname(f),"none")) |
| 858 |
writet3user(pdf, f); |
| 859 |
else if ((fm = font_map(f)) == NULL || (fm->ps_name == NULL && fm->ff_name == NULL)) |
| 860 |
writet3pk(pdf, f); |
| 861 |
else |
| 862 |
create_fontdictionary(pdf, f); |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
int do_pdf_preroll_font(PDF pdf, internal_font_number f) |
| 867 |
{ |
| 868 |
if (!font_has_subset(f)) { |
| 869 |
return 0; |
| 870 |
} else if (font_encodingbytes(f) != 2 && font_psname(f) != NULL && strstr(font_psname(f),"none")) { |
| 871 |
prerollt3user(pdf, f); |
| 872 |
return 1; |
| 873 |
} else { |
| 874 |
return 0; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
/*tex |
| 879 |
|
| 880 |
The glyph width is included in |glw_entry|, because that width depends on the |
| 881 |
value it has in the font where it is actually typeset from, not the font that |
| 882 |
is the owner of the fd entry. |
| 883 |
|
| 884 |
It is possible that the user messes with the metric width, but handling that |
| 885 |
properly would require access to the |hmtx| table at this point in the |
| 886 |
program. |
| 887 |
|
| 888 |
*/ |
| 889 |
|
| 890 |
static int comp_glw_entry(const void *pa, const void *pb, void *p __attribute__ ((unused))) |
| 891 |
{ |
| 892 |
unsigned short i, j; |
| 893 |
i = (unsigned short) (*(const glw_entry *) pa).id; |
| 894 |
j = (unsigned short) (*(const glw_entry *) pb).id; |
| 895 |
cmp_return(i, j); |
| 896 |
return 0; |
| 897 |
} |
| 898 |
|
| 899 |
static void create_cid_fontdescriptor(fo_entry * fo, internal_font_number f) |
| 900 |
{ |
| 901 |
fo->fd = new_fd_entry(f); |
| 902 |
preset_fontname(fo, f); |
| 903 |
preset_fontmetrics(fo->fd, f); |
| 904 |
/*tex Encoding needed by \TRUETYPE\ writing: */ |
| 905 |
fo->fd->fe = fo->fe; |
| 906 |
/*tex Map entry needed by \TRUETYPE\ writing */ |
| 907 |
fo->fd->fm = fo->fm; |
| 908 |
fo->fd->gl_tree = avl_create(comp_glw_entry, NULL, &avl_xallocator); |
| 909 |
assert(fo->fd->gl_tree != NULL); |
| 910 |
} |
| 911 |
|
| 912 |
/*tex |
| 913 |
|
| 914 |
The values |font_bc()| and |font_ec()| are potentially large character ids, |
| 915 |
but the strings that are written out use CID indexes, and those are limited |
| 916 |
to 16-bit values. |
| 917 |
|
| 918 |
*/ |
| 919 |
|
| 920 |
static void mark_cid_subset_glyphs(fo_entry * fo, internal_font_number f) |
| 921 |
{ |
| 922 |
glw_entry *j; |
| 923 |
void *aa; |
| 924 |
int l = font_size(f); |
| 925 |
int i; |
| 926 |
for (i = font_bc(f); i <= font_ec(f); i++) { |
| 927 |
if (quick_char_exists(f, i) && char_used(f, i)) { |
| 928 |
j = xtalloc(1, glw_entry); |
| 929 |
j->id = (unsigned) char_index(f, i); |
| 930 |
j->wd = divide_scaled_n(char_width(f, i), l, 10000.0); |
| 931 |
if ((glw_entry *) avl_find(fo->fd->gl_tree, j) == NULL) { |
| 932 |
aa = avl_probe(fo->fd->gl_tree, j); |
| 933 |
if (aa == NULL) { |
| 934 |
/*tex Is this a problem? */ |
| 935 |
} |
| 936 |
} else { |
| 937 |
xfree(j); |
| 938 |
} |
| 939 |
} |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
/*tex |
| 944 |
|
| 945 |
It is possible to compress the widths array even better, by using the |
| 946 |
alternate 'range' syntax and possibly even using /DW to set a default value. |
| 947 |
|
| 948 |
There is a some optimization here already: glyphs that are not used do not |
| 949 |
appear in the widths array at all. |
| 950 |
|
| 951 |
We have to make sure that we do not output an (incorrect!) width for a |
| 952 |
character that exists in the font, but is not used in typesetting. An |
| 953 |
enormous negative width is used as sentinel value |
| 954 |
|
| 955 |
*/ |
| 956 |
|
| 957 |
static void write_cid_charwidth_array(PDF pdf, fo_entry * fo) |
| 958 |
{ |
| 959 |
int i, j; |
| 960 |
glw_entry *glyph; |
| 961 |
struct avl_traverser t; |
| 962 |
fo->cw_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 963 |
pdf_begin_obj(pdf, fo->cw_objnum, OBJSTM_ALWAYS); |
| 964 |
avl_t_init(&t, fo->fd->gl_tree); |
| 965 |
glyph = (glw_entry *) avl_t_first(&t, fo->fd->gl_tree); |
| 966 |
i = (int) glyph->id; |
| 967 |
pdf_begin_array(pdf); |
| 968 |
pdf_add_int(pdf, i); |
| 969 |
pdf_begin_array(pdf); |
| 970 |
for (; glyph != NULL; glyph = (glw_entry *) avl_t_next(&t)) { |
| 971 |
j = glyph->wd; |
| 972 |
if (glyph->id > (unsigned) (i + 1)) { |
| 973 |
pdf_end_array(pdf); |
| 974 |
pdf_add_int(pdf, glyph->id); |
| 975 |
pdf_begin_array(pdf); |
| 976 |
j = glyph->wd; |
| 977 |
} |
| 978 |
pdf_check_space(pdf); |
| 979 |
pdf_print_cid_charwidth(pdf, fo->tex_font, j); |
| 980 |
i = (int) glyph->id; |
| 981 |
pdf_set_space(pdf); |
| 982 |
} |
| 983 |
pdf_end_array(pdf); |
| 984 |
pdf_end_array(pdf); |
| 985 |
pdf_end_obj(pdf); |
| 986 |
} |
| 987 |
|
| 988 |
static void destroy_glw_cid_entry(void *pa, void *pb) |
| 989 |
{ |
| 990 |
glw_entry *e = (glw_entry *) pa; |
| 991 |
(void) pb; |
| 992 |
xfree(e); |
| 993 |
} |
| 994 |
|
| 995 |
static void create_cid_fontdictionary(PDF pdf, internal_font_number f) |
| 996 |
{ |
| 997 |
fm_entry *fm = font_map(f); |
| 998 |
fo_entry *fo = new_fo_entry(); |
| 999 |
/*tex set |fo->first_char| and |fo->last_char| from |f| */ |
| 1000 |
get_char_range(fo, f); |
| 1001 |
fo->fm = fm; |
| 1002 |
fo->fo_objnum = pdf_font_num(f); |
| 1003 |
fo->tex_font = f; |
| 1004 |
create_cid_fontdescriptor(fo, f); |
| 1005 |
mark_cid_subset_glyphs(fo, f); |
| 1006 |
if (is_subsetted(fo->fm)) { |
| 1007 |
/*tex |
| 1008 |
This is a bit sneaky. |make_subset_tag()| actually expects the glyph |
| 1009 |
tree to contain strings instead of |glw_entry| items. However, all |
| 1010 |
calculations are done using explicit typecasts, so it works out ok. |
| 1011 |
*/ |
| 1012 |
make_subset_tag(fo->fd); |
| 1013 |
} |
| 1014 |
write_cid_charwidth_array(pdf, fo); |
| 1015 |
write_fontdescriptor(pdf, fo->fd); |
| 1016 |
write_cid_fontdictionary(pdf, fo, f); |
| 1017 |
if (fo->fd) { |
| 1018 |
if (fo->fd->gl_tree) { |
| 1019 |
avl_destroy(fo->fd->gl_tree,destroy_glw_cid_entry); |
| 1020 |
} |
| 1021 |
xfree(fo->fd); |
| 1022 |
} |
| 1023 |
xfree(fo); |
| 1024 |
} |
| 1025 |
|
| 1026 |
void write_cid_fontdictionary(PDF pdf, fo_entry * fo, internal_font_number f) |
| 1027 |
{ |
| 1028 |
int i; |
| 1029 |
fo->tounicode_objnum = write_cid_tounicode(pdf, fo, f); |
| 1030 |
pdf_begin_obj(pdf, fo->fo_objnum, OBJSTM_ALWAYS); |
| 1031 |
pdf_begin_dict(pdf); |
| 1032 |
pdf_dict_add_name(pdf, "Type", "Font"); |
| 1033 |
pdf_dict_add_name(pdf, "Subtype", "Type0"); |
| 1034 |
if (font_identity(f) == vertical_identity) { |
| 1035 |
pdf_dict_add_name(pdf, "Encoding", "Identity-V"); |
| 1036 |
} else { |
| 1037 |
pdf_dict_add_name(pdf, "Encoding", "Identity-H"); |
| 1038 |
} |
| 1039 |
pdf_dict_add_fontname(pdf, "BaseFont", fo->fd); |
| 1040 |
i = pdf_create_obj(pdf, obj_type_others, 0); |
| 1041 |
pdf_add_name(pdf, "DescendantFonts"); |
| 1042 |
pdf_begin_array(pdf); |
| 1043 |
pdf_add_ref(pdf, i); |
| 1044 |
pdf_end_array(pdf); |
| 1045 |
if (fo->tounicode_objnum != 0) |
| 1046 |
pdf_dict_add_ref(pdf, "ToUnicode", (int) fo->tounicode_objnum); |
| 1047 |
pdf_end_dict(pdf); |
| 1048 |
pdf_end_obj(pdf); |
| 1049 |
pdf_begin_obj(pdf, i, OBJSTM_ALWAYS); |
| 1050 |
pdf_begin_dict(pdf); |
| 1051 |
pdf_dict_add_name(pdf, "Type", "Font"); |
| 1052 |
if (is_opentype(fo->fm)) { |
| 1053 |
pdf_dict_add_name(pdf, "Subtype", "CIDFontType0"); |
| 1054 |
} else if (is_type1(fo->fm)) { |
| 1055 |
if (t1_wide_mode) { |
| 1056 |
pdf_dict_add_name(pdf, "Subtype", "CIDFontType0"); |
| 1057 |
// pdf_dict_add_name(pdf, "CIDToGIDMap", "Identity"); |
| 1058 |
} else { |
| 1059 |
pdf_dict_add_name(pdf, "Subtype", "CIDFontType0"); |
| 1060 |
} |
| 1061 |
} else { |
| 1062 |
pdf_dict_add_name(pdf, "Subtype", "CIDFontType2"); |
| 1063 |
pdf_dict_add_name(pdf, "CIDToGIDMap", "Identity"); |
| 1064 |
} |
| 1065 |
pdf_dict_add_fontname(pdf, "BaseFont", fo->fd); |
| 1066 |
pdf_dict_add_ref(pdf, "FontDescriptor", (int) fo->fd->fd_objnum); |
| 1067 |
pdf_dict_add_ref(pdf, "W", (int) fo->cw_objnum); |
| 1068 |
pdf_add_name(pdf, "CIDSystemInfo"); |
| 1069 |
pdf_begin_dict(pdf); |
| 1070 |
pdf_dict_add_string(pdf, "Registry", (font_cidregistry(f) ? font_cidregistry(f) : "Adobe")); |
| 1071 |
pdf_dict_add_string(pdf, "Ordering", (font_cidordering(f) ? font_cidordering(f) : "Identity")); |
| 1072 |
pdf_dict_add_int(pdf, "Supplement", (int) font_cidsupplement(f)); |
| 1073 |
pdf_end_dict(pdf); |
| 1074 |
/*tex |
| 1075 |
I doubt there is anything useful that could be written here so for now we |
| 1076 |
comment this. |
| 1077 |
*/ |
| 1078 |
/* |
| 1079 |
if (pdf_font_attr(fo->tex_font) != get_nullstr()) { |
| 1080 |
pdf_out(pdf, '\n'); |
| 1081 |
pdf_print(pdf_font_attr(fo->tex_font)); |
| 1082 |
pdf_out(pdf, '\n'); |
| 1083 |
} |
| 1084 |
*/ |
| 1085 |
pdf_end_dict(pdf); |
| 1086 |
pdf_end_obj(pdf); |
| 1087 |
} |