| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "system.h" |
| 26 |
|
| 27 |
#include "numbers.h" |
| 28 |
#include "error.h" |
| 29 |
#include "mem.h" |
| 30 |
|
| 31 |
#include "dpxconf.h" |
| 32 |
#include "dpxfile.h" |
| 33 |
#include "dpxutil.h" |
| 34 |
|
| 35 |
#include "pdfobj.h" |
| 36 |
#include "pdfresource.h" |
| 37 |
#include "pdffont.h" |
| 38 |
|
| 39 |
#include "pdfencoding.h" |
| 40 |
#include "unicode.h" |
| 41 |
#include "agl.h" |
| 42 |
|
| 43 |
/* TrueType */ |
| 44 |
#include "sfnt.h" |
| 45 |
#include "tt_cmap.h" |
| 46 |
#include "tt_table.h" |
| 47 |
#include "tt_glyf.h" |
| 48 |
#include "tt_post.h" |
| 49 |
#include "tt_gsub.h" |
| 50 |
#include "tt_aux.h" |
| 51 |
|
| 52 |
#include "truetype.h" |
| 53 |
|
| 54 |
/* Modifying this has no effect :P */ |
| 55 |
#ifdef ENABLE_NOEMBED |
| 56 |
# undef ENABLE_NOEMBED |
| 57 |
#endif |
| 58 |
|
| 59 |
int |
| 60 |
pdf_font_open_truetype (pdf_font *font, const char *ident, int index, int encoding_id, int embedding) |
| 61 |
{ |
| 62 |
pdf_obj *fontdict, *descriptor; |
| 63 |
sfnt *sfont; |
| 64 |
FILE *fp = NULL; |
| 65 |
int length, error = 0; |
| 66 |
|
| 67 |
ASSERT(font); |
| 68 |
ASSERT(ident); |
| 69 |
|
| 70 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_TTFONT); |
| 71 |
if (!fp) { |
| 72 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_DFONT); |
| 73 |
if (!fp) |
| 74 |
return -1; |
| 75 |
sfont = dfont_open(fp, index); |
| 76 |
} else { |
| 77 |
sfont = sfnt_open(fp); |
| 78 |
} |
| 79 |
|
| 80 |
if (!sfont) { |
| 81 |
WARN("Could not open TrueType font: %s", ident); |
| 82 |
if (fp) |
| 83 |
DPXFCLOSE(fp); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
if (sfont->type == SFNT_TYPE_TTC) { |
| 88 |
ULONG offset; |
| 89 |
|
| 90 |
offset = ttc_read_offset(sfont, index); |
| 91 |
if (offset == 0) { |
| 92 |
WARN("Invalid TTC index in %s.", ident); |
| 93 |
error = -1; |
| 94 |
} else { |
| 95 |
error = sfnt_read_table_directory(sfont, offset); |
| 96 |
} |
| 97 |
} else { |
| 98 |
error = sfnt_read_table_directory(sfont, sfont->offset); |
| 99 |
} |
| 100 |
if (error) { |
| 101 |
sfnt_close(sfont); |
| 102 |
if (fp) |
| 103 |
DPXFCLOSE(fp); |
| 104 |
return -1; /* Silently */ |
| 105 |
} |
| 106 |
|
| 107 |
/* Reading fontdict before checking fonttype conflicts with PKFONT |
| 108 |
* because pdf_font_get_resource() always makes a dictionary. |
| 109 |
*/ |
| 110 |
fontdict = pdf_font_get_resource(font); |
| 111 |
descriptor = pdf_font_get_descriptor(font); |
| 112 |
#ifndef ENABLE_NOEMBED |
| 113 |
if (!embedding) { |
| 114 |
WARN("No-embed option not supported for TrueType font: %s", ident); |
| 115 |
embedding = 1; |
| 116 |
font->flags &= ~PDF_FONT_FLAG_NOEMBED; |
| 117 |
} |
| 118 |
#endif /* ENABLE_NOEMBED */ |
| 119 |
|
| 120 |
ASSERT( fontdict && descriptor ); |
| 121 |
|
| 122 |
{ |
| 123 |
char fontname[256]; |
| 124 |
int n; |
| 125 |
pdf_obj *tmp; |
| 126 |
|
| 127 |
memset(fontname, 0, 256); |
| 128 |
length = tt_get_ps_fontname(sfont, fontname, 255); |
| 129 |
if (length < 1) { |
| 130 |
length = MIN(strlen(ident), 255); |
| 131 |
memcpy(fontname, ident, length); |
| 132 |
} |
| 133 |
fontname[length] = '\0'; |
| 134 |
for (n = 0; n < length; n++) { |
| 135 |
if (fontname[n] == 0) { |
| 136 |
memmove(fontname + n, fontname + n + 1, length - n - 1); |
| 137 |
} |
| 138 |
} |
| 139 |
if (strlen(fontname) == 0) { |
| 140 |
WARN("Can't find valid fontname for \"%s\".", ident); |
| 141 |
error = -1; |
| 142 |
} else { |
| 143 |
font->fontname = NEW(strlen(fontname)+1, char); |
| 144 |
strcpy(font->fontname, fontname); |
| 145 |
tmp = tt_get_fontdesc(sfont, &embedding, -1, 1, fontname); |
| 146 |
if (!tmp) { |
| 147 |
WARN("Could not obtain necessary font info: %s", ident); |
| 148 |
error = -1; |
| 149 |
} else { |
| 150 |
ASSERT(pdf_obj_typeof(tmp) == PDF_DICT); |
| 151 |
pdf_merge_dict(descriptor, tmp); |
| 152 |
pdf_release_obj(tmp); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
if (error) { |
| 157 |
sfnt_close(sfont); |
| 158 |
if (fp) |
| 159 |
DPXFCLOSE(fp); |
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
if (!embedding) { |
| 164 |
#ifndef ENABLE_NOEMBED |
| 165 |
WARN("Font file=\"%s\" can't be embedded due to liscence restrictions.", ident); |
| 166 |
error = -1; |
| 167 |
#else |
| 168 |
if (encoding_id >= 0 && !pdf_encoding_is_predefined(encoding_id)) { |
| 169 |
WARN("Custom encoding not allowed for non-embedded TrueType font: %s", ident); |
| 170 |
error = -1; |
| 171 |
} else { |
| 172 |
/* There are basically no guarantee for font substitution |
| 173 |
* can work with "symblic" fonts. At least all glyphs |
| 174 |
* contained in the font must be identified; glyphs covers |
| 175 |
* by this instance of font should contain glyphs only from |
| 176 |
* Adobe Standard Latin Set. We allow non-embedded font |
| 177 |
* only to predefined encodings for this reason. Note that |
| 178 |
* "builtin" encoding means "MacRoman" here. |
| 179 |
*/ |
| 180 |
pdf_obj *tmp; |
| 181 |
int flags; |
| 182 |
|
| 183 |
font->flags |= PDF_FONT_FLAG_NOEMBED; |
| 184 |
tmp = pdf_lookup_dict(descriptor, "Flags"); |
| 185 |
if (tmp && pdf_obj_typeof(tmp) == PDF_NUMBER) { |
| 186 |
flags = (int) pdf_number_value(tmp); |
| 187 |
flags &= (1 << 2); /* clear Symbolic */ |
| 188 |
flags |= (1 << 5); /* set Nonsymbolic */ |
| 189 |
pdf_add_dict(descriptor, pdf_new_name("Flags"), pdf_new_number(flags)); |
| 190 |
} |
| 191 |
} |
| 192 |
#endif /* ENABLE_NOEMBED */ |
| 193 |
} |
| 194 |
|
| 195 |
sfnt_close(sfont); |
| 196 |
if (fp) |
| 197 |
DPXFCLOSE(fp); |
| 198 |
|
| 199 |
if (!error) { |
| 200 |
pdf_add_dict(fontdict, |
| 201 |
pdf_new_name("Type"), pdf_new_name("Font")); |
| 202 |
pdf_add_dict(fontdict, |
| 203 |
pdf_new_name("Subtype"), pdf_new_name("TrueType")); |
| 204 |
font->subtype = PDF_FONT_FONTTYPE_TRUETYPE; |
| 205 |
} |
| 206 |
|
| 207 |
return error; |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 211 |
* The 'name' table should be preserved since it contains copyright |
| 212 |
* information, but it might cause problem when there are invalid |
| 213 |
* table entries (wrongly encoded text which is often the case in |
| 214 |
* CJK fonts). Acrobat does not use 'name' table. Unicode TrueType |
| 215 |
* fonts may have 10K bytes 'name' table... |
| 216 |
* |
| 217 |
* We preserve the 'OS/2' table too, since it contains the license |
| 218 |
* information. PDF applications should use this table to decide |
| 219 |
* whether the font is embedded only for the purpose of preview & |
| 220 |
* printing. Otherwise, we must encrypt the document. Acrobat does |
| 221 |
* not use 'OS/2' table, though... |
| 222 |
*/ |
| 223 |
static struct |
| 224 |
{ |
| 225 |
const char *name; |
| 226 |
int must_exist; |
| 227 |
} required_table[] = { |
| 228 |
{"OS/2", 0}, {"head", 1}, {"hhea", 1}, {"loca", 1}, {"maxp", 1}, |
| 229 |
{"name", 1}, {"glyf", 1}, {"hmtx", 1}, {"fpgm", 0}, {"cvt ", 0}, |
| 230 |
{"prep", 0}, {"cmap", 1}, {NULL, 0} |
| 231 |
}; |
| 232 |
|
| 233 |
static void |
| 234 |
do_widths (pdf_font *font, double *widths) |
| 235 |
{ |
| 236 |
pdf_obj *fontdict; |
| 237 |
pdf_obj *array; |
| 238 |
int code, firstchar, lastchar; |
| 239 |
char *usedchars; |
| 240 |
|
| 241 |
fontdict = font->resource; |
| 242 |
usedchars = font->usedchars; |
| 243 |
|
| 244 |
for (firstchar = 255, lastchar = 0, code = 0; code < 256; code++) { |
| 245 |
if (usedchars[code]) { |
| 246 |
if (code < firstchar) firstchar = code; |
| 247 |
if (code > lastchar) lastchar = code; |
| 248 |
} |
| 249 |
} |
| 250 |
if (firstchar > lastchar) { |
| 251 |
WARN("No glyphs actually used???"); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
pdf_check_tfm_widths(font->ident, widths, firstchar, lastchar, usedchars); |
| 256 |
|
| 257 |
array = pdf_new_array(); |
| 258 |
for (code = firstchar; code <= lastchar; code++) { |
| 259 |
if (usedchars[code]) { |
| 260 |
pdf_add_array(array, pdf_new_number(ROUND(widths[code], 0.1))); |
| 261 |
} else { |
| 262 |
pdf_add_array(array, pdf_new_number(0.0)); |
| 263 |
} |
| 264 |
} |
| 265 |
if (pdf_array_length(array) > 0) { |
| 266 |
pdf_add_dict(fontdict, pdf_new_name("Widths"), pdf_ref_obj(array)); |
| 267 |
} |
| 268 |
pdf_release_obj(array); |
| 269 |
|
| 270 |
pdf_add_dict(fontdict, |
| 271 |
pdf_new_name("FirstChar"), pdf_new_number(firstchar)); |
| 272 |
pdf_add_dict(fontdict, |
| 273 |
pdf_new_name("LastChar"), pdf_new_number(lastchar)); |
| 274 |
|
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
#define PDFUNIT(v) ((double) (ROUND(1000.0*(v)/(glyphs->emsize), 1))) |
| 279 |
|
| 280 |
/* |
| 281 |
* There are several issues in TrueType font support in PDF. |
| 282 |
* How PDF viewers select TrueType cmap table is not so clear. |
| 283 |
* Most reliable way seem to reencode font and sort glyphs as |
| 284 |
* charcode == gid and to use Mac-Roman format 0 subtable. |
| 285 |
* It does not work with encodings that uses full 256 range since |
| 286 |
* GID = 0 is reserved for .notdef, so GID = 256 is not accessible. |
| 287 |
*/ |
| 288 |
static int |
| 289 |
do_builtin_encoding (pdf_font *font, const char *usedchars, sfnt *sfont) |
| 290 |
{ |
| 291 |
struct tt_glyphs *glyphs; |
| 292 |
char *cmap_table; |
| 293 |
tt_cmap *ttcm; |
| 294 |
USHORT gid, idx; |
| 295 |
int code, count; |
| 296 |
double widths[256]; |
| 297 |
USHORT code_to_idx[256] = {0}; |
| 298 |
int cmap_old_format = 0; |
| 299 |
|
| 300 |
ttcm = tt_cmap_read(sfont, TT_MAC, TT_MAC_ROMAN); |
| 301 |
if (!ttcm) { |
| 302 |
WARN("Could not read Mac-Roman TrueType cmap table..."); |
| 303 |
return -1; |
| 304 |
} |
| 305 |
|
| 306 |
if (cmap_old_format) { |
| 307 |
cmap_table = NEW(274, char); |
| 308 |
memset(cmap_table, 0, 274); |
| 309 |
sfnt_put_ushort(cmap_table, 0); /* Version */ |
| 310 |
sfnt_put_ushort(cmap_table+2, 1); /* Number of subtables */ |
| 311 |
sfnt_put_ushort(cmap_table+4, TT_MAC); /* Platform ID */ |
| 312 |
sfnt_put_ushort(cmap_table+6, TT_MAC_ROMAN); /* Encoding ID */ |
| 313 |
sfnt_put_ulong (cmap_table+8, 12); /* Offset */ |
| 314 |
sfnt_put_ushort(cmap_table+12, 0); /* Format */ |
| 315 |
sfnt_put_ushort(cmap_table+14, 262); /* Length */ |
| 316 |
sfnt_put_ushort(cmap_table+16, 0); /* Language */ |
| 317 |
} else { |
| 318 |
cmap_table = NEW(1086, char); /* = 20 + 522 + 544 */ |
| 319 |
memset(cmap_table, 0, 1086); |
| 320 |
sfnt_put_ushort(cmap_table, 0); /* Version */ |
| 321 |
sfnt_put_ushort(cmap_table+2, 2); /* Number of subtables */ |
| 322 |
sfnt_put_ushort(cmap_table+4, TT_MAC); /* Platform ID */ |
| 323 |
sfnt_put_ushort(cmap_table+6, TT_MAC_ROMAN); /* Encoding ID */ |
| 324 |
sfnt_put_ulong (cmap_table+8, 20); /* Offset */ |
| 325 |
sfnt_put_ushort(cmap_table+12, TT_WIN); /* Platform ID */ |
| 326 |
sfnt_put_ushort(cmap_table+14, TT_WIN_SYMBOL);/* Encoding ID */ |
| 327 |
sfnt_put_ulong (cmap_table+16, 542); /* Offset = 20 + 522 */ |
| 328 |
/* (1, 0) subtable */ |
| 329 |
sfnt_put_ushort(cmap_table+20, 6); /* format */ |
| 330 |
sfnt_put_ushort(cmap_table+22, 522); /* length = 10 + 512 */ |
| 331 |
sfnt_put_ushort(cmap_table+24, 0); /* language */ |
| 332 |
sfnt_put_ushort(cmap_table+26, 0); /* firstCode */ |
| 333 |
sfnt_put_ushort(cmap_table+28, 256); /* entryCount */ |
| 334 |
/* (3, 0) subtable */ |
| 335 |
sfnt_put_ushort(cmap_table+542, 4); /* format */ |
| 336 |
sfnt_put_ushort(cmap_table+544, 544); /* length = 16 + 8 * 2 + 512 */ |
| 337 |
sfnt_put_ushort(cmap_table+546, 0); /* language */ |
| 338 |
sfnt_put_ushort(cmap_table+548, 4); /* segCountX2 */ |
| 339 |
sfnt_put_ushort(cmap_table+550, 4); /* searchRange */ |
| 340 |
sfnt_put_ushort(cmap_table+552, 1); /* entrySelector */ |
| 341 |
sfnt_put_ushort(cmap_table+554, 0); /* rangeShift */ |
| 342 |
sfnt_put_ushort(cmap_table+556, 0xf0ff) /* endCode[0] */ |
| 343 |
sfnt_put_ushort(cmap_table+558, 0xffff) /* endCode[1] */ |
| 344 |
sfnt_put_ushort(cmap_table+560, 0); /* reservedPad */ |
| 345 |
sfnt_put_ushort(cmap_table+562, 0xf000); /* startCode[0] */ |
| 346 |
sfnt_put_ushort(cmap_table+564, 0xffff); /* startCode[1] */ |
| 347 |
sfnt_put_short (cmap_table+566, 0); /* idDelta[0] */ |
| 348 |
sfnt_put_short (cmap_table+568, 1); /* idDelta[1] */ |
| 349 |
sfnt_put_ushort(cmap_table+570, 4); /* idRangeOffset[0] */ |
| 350 |
sfnt_put_ushort(cmap_table+572, 0); /* idRangeOffset[1] */ |
| 351 |
} |
| 352 |
|
| 353 |
glyphs = tt_build_init(); |
| 354 |
|
| 355 |
if (dpx_conf.verbose_level > 2) |
| 356 |
MESG("[glyphs:/.notdef"); |
| 357 |
|
| 358 |
count = 1; /* .notdef */ |
| 359 |
for (code = 0; code < 256; code++) { |
| 360 |
if (!usedchars[code]) |
| 361 |
continue; |
| 362 |
|
| 363 |
if (dpx_conf.verbose_level > 2) |
| 364 |
MESG("/.c0x%02x", code); |
| 365 |
|
| 366 |
gid = tt_cmap_lookup(ttcm, code); |
| 367 |
if (gid == 0) { |
| 368 |
WARN("Glyph for character code=0x%02x missing in font font-file=\"%s\".", |
| 369 |
code, font->filename); |
| 370 |
idx = 0; |
| 371 |
} else { |
| 372 |
idx = tt_find_glyph(glyphs, gid); |
| 373 |
if (idx == 0) |
| 374 |
idx = tt_add_glyph(glyphs, (USHORT)gid, (USHORT)count); /* count returned. */ |
| 375 |
} |
| 376 |
code_to_idx[code] = idx; |
| 377 |
if (cmap_old_format) { |
| 378 |
if (idx >= 256) { |
| 379 |
WARN("Font font-file=\"%s\" uses more than 255 glyphs", font->filename); |
| 380 |
WARN(">> Glyph for character code=0x%02x will be inaccessible.", code); |
| 381 |
cmap_table[18 + code] = 0; |
| 382 |
} else { |
| 383 |
cmap_table[18 + code] = (char)(BYTE)idx; |
| 384 |
} |
| 385 |
} else { |
| 386 |
sfnt_put_ushort(cmap_table + 30 + 2 * code, idx); |
| 387 |
sfnt_put_ushort(cmap_table + 574 + 2 * code, idx); |
| 388 |
} |
| 389 |
count++; |
| 390 |
} |
| 391 |
tt_cmap_release(ttcm); |
| 392 |
|
| 393 |
if (dpx_conf.verbose_level > 2) |
| 394 |
MESG("]"); |
| 395 |
|
| 396 |
if (tt_build_tables(sfont, glyphs) < 0) { |
| 397 |
WARN("Packing TrueType font into SFNT failed!"); |
| 398 |
tt_build_finish(glyphs); |
| 399 |
RELEASE(cmap_table); |
| 400 |
return -1; |
| 401 |
} |
| 402 |
|
| 403 |
for (code = 0; code < 256; code++) { |
| 404 |
if (usedchars[code]) { |
| 405 |
idx = tt_get_index(glyphs, code_to_idx[code]); |
| 406 |
widths[code] = PDFUNIT(glyphs->gd[idx].advw); |
| 407 |
} else { |
| 408 |
widths[code] = 0.0; |
| 409 |
} |
| 410 |
} |
| 411 |
do_widths(font, widths); |
| 412 |
|
| 413 |
if (dpx_conf.verbose_level > 1) |
| 414 |
MESG("[%d glyphs]", glyphs->num_glyphs); |
| 415 |
|
| 416 |
tt_build_finish(glyphs); |
| 417 |
|
| 418 |
if (cmap_old_format) { |
| 419 |
sfnt_set_table(sfont, "cmap", cmap_table, 274); |
| 420 |
} else { |
| 421 |
sfnt_set_table(sfont, "cmap", cmap_table, 1086); |
| 422 |
} |
| 423 |
|
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
/* Order of lookup should be |
| 428 |
* post, unicode+otl |
| 429 |
*/ |
| 430 |
struct glyph_mapper |
| 431 |
{ |
| 432 |
tt_cmap *codetogid; |
| 433 |
otl_gsub *gsub; |
| 434 |
sfnt *sfont; |
| 435 |
struct tt_post_table *nametogid; |
| 436 |
}; |
| 437 |
|
| 438 |
|
| 439 |
/* WARNING: This modifies glyphname itself */ |
| 440 |
static int |
| 441 |
agl_decompose_glyphname (char *glyphname, char **nptrs, int size, char **suffix) |
| 442 |
{ |
| 443 |
char *q, *p = glyphname; |
| 444 |
int n; |
| 445 |
|
| 446 |
q = strchr(p, '.'); /* chop every thing after *first* dot */ |
| 447 |
if (!q) |
| 448 |
*suffix = NULL; |
| 449 |
else { |
| 450 |
*q = '\0'; q++; |
| 451 |
*suffix = q; |
| 452 |
} |
| 453 |
|
| 454 |
nptrs[0] = p; |
| 455 |
for (n = 1; p && *p; n++) { |
| 456 |
p = strchr(p, '_'); |
| 457 |
if (!p || p[1] == '\0') |
| 458 |
break; |
| 459 |
if (n >= size) |
| 460 |
ERROR("Uh ah..."); /* _FIXME_ */ |
| 461 |
*p = '\0'; p++; |
| 462 |
nptrs[n] = p; |
| 463 |
} |
| 464 |
|
| 465 |
return n; |
| 466 |
} |
| 467 |
|
| 468 |
static int |
| 469 |
select_gsub (const char *feat, struct glyph_mapper *gm) |
| 470 |
{ |
| 471 |
int idx, error = 0; |
| 472 |
|
| 473 |
if (!feat || *feat == 0 || !gm || !gm->gsub) |
| 474 |
return -1; |
| 475 |
|
| 476 |
/* First treat as is */ |
| 477 |
idx = otl_gsub_select(gm->gsub, "*", "*", feat); |
| 478 |
if (idx >= 0) |
| 479 |
return 0; |
| 480 |
|
| 481 |
if (dpx_conf.verbose_level > 1) |
| 482 |
MESG("\ntrutype>> Try loading OTL GSUB for \"*.*.%s\"...", feat); |
| 483 |
error = otl_gsub_add_feat(gm->gsub, "*", "*", feat, gm->sfont); |
| 484 |
if (!error) { |
| 485 |
idx = otl_gsub_select(gm->gsub, "*", "*", feat); |
| 486 |
return (idx >= 0 ? 0 : -1); |
| 487 |
} |
| 488 |
|
| 489 |
return -1; |
| 490 |
} |
| 491 |
|
| 492 |
static int findparanoiac (const char *glyph_name, USHORT *gid, struct glyph_mapper *gm); |
| 493 |
static int resolve_glyph (const char *glyph_name, USHORT *gid, struct glyph_mapper *gm); |
| 494 |
|
| 495 |
/* Apply GSUB. This is a bit tricky... */ |
| 496 |
static int |
| 497 |
selectglyph (USHORT in, const char *suffix, struct glyph_mapper *gm, USHORT *out) |
| 498 |
{ |
| 499 |
char *s, *q, t[5]; |
| 500 |
const char *r; |
| 501 |
int n, error = 0; |
| 502 |
|
| 503 |
ASSERT(suffix && gm && out); |
| 504 |
ASSERT(suffix && *suffix != 0); |
| 505 |
|
| 506 |
s = NEW(strlen(suffix) + 1, char); |
| 507 |
strcpy(s, suffix); |
| 508 |
|
| 509 |
/* First try converting suffix to feature tag. |
| 510 |
* agl.c currently only knows less ambiguos cases; |
| 511 |
* e.g., 'sc', 'superior', etc. |
| 512 |
*/ |
| 513 |
r = agl_suffix_to_otltag(s); |
| 514 |
if (r) { /* We found feature tag for 'suffix'. */ |
| 515 |
error = select_gsub(r, gm); /* no fallback for this */ |
| 516 |
if (!error) |
| 517 |
error = otl_gsub_apply(gm->gsub, &in); |
| 518 |
} else { /* 'suffix' may represent feature tag. */ |
| 519 |
/* Try loading GSUB only when length of 'suffix' is less |
| 520 |
* than or equal to 4. tt_gsub give a warning otherwise. |
| 521 |
*/ |
| 522 |
if (strlen(s) > 4) |
| 523 |
error = -1; /* Uh */ |
| 524 |
else if (strlen(s) == 4) |
| 525 |
error = select_gsub(s, gm); |
| 526 |
else { /* less than 4. pad ' '. */ |
| 527 |
memset(t, ' ', 4); t[4] = '\0'; |
| 528 |
memcpy(t, s, strlen(s)); |
| 529 |
error = select_gsub(t, gm); |
| 530 |
} |
| 531 |
if (!error) /* 'suffix' represents feature tag. */ |
| 532 |
error = otl_gsub_apply(gm->gsub, &in); |
| 533 |
else { /* other case: alt1, nalt10... (alternates) */ |
| 534 |
for (q = s + strlen(s) - 1; q > s && *q >= '0' && *q <= '9'; q--); |
| 535 |
if (q == s) |
| 536 |
error = -1; |
| 537 |
else { /* starting at 1 */ |
| 538 |
n = atoi(q + 1) - 1; q[1] = '\0'; |
| 539 |
if (strlen(s) > 4) |
| 540 |
error = -1; |
| 541 |
else { /* This may be alternate substitution. */ |
| 542 |
memset(t, ' ', 4); t[4] = '\0'; |
| 543 |
memcpy(t, s, strlen(s)); |
| 544 |
error = select_gsub(s, gm); |
| 545 |
if (!error) |
| 546 |
error = otl_gsub_apply_alt(gm->gsub, (USHORT)n, (USHORT *)&in); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
RELEASE(s); |
| 552 |
|
| 553 |
*out = in; |
| 554 |
return error; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
/* Compose glyphs via ligature substitution. */ |
| 559 |
static int |
| 560 |
composeglyph (USHORT *glyphs, int n_glyphs, |
| 561 |
const char *feat, struct glyph_mapper *gm, USHORT *gid) |
| 562 |
{ |
| 563 |
int error = 0; |
| 564 |
char t[5] = {' ', ' ', ' ', ' ', 0}; |
| 565 |
|
| 566 |
ASSERT(glyphs && n_glyphs > 0 && gm && gid); |
| 567 |
|
| 568 |
if (!feat || feat[0] == '\0') /* meaning "Unknown" */ |
| 569 |
error = select_gsub("(?lig|lig?|?cmp|cmp?|frac|afrc)", gm); |
| 570 |
else { |
| 571 |
if (strlen(feat) > 4) |
| 572 |
error = -1; |
| 573 |
else { |
| 574 |
memcpy(t, feat, strlen(feat)); |
| 575 |
error = select_gsub(t, gm); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if (!error) |
| 580 |
error = otl_gsub_apply_lig(gm->gsub, (USHORT *)glyphs, (USHORT)n_glyphs, |
| 581 |
(USHORT *)gid); |
| 582 |
|
| 583 |
return error; |
| 584 |
} |
| 585 |
|
| 586 |
/* This may be called by findparanoiac(). */ |
| 587 |
static int |
| 588 |
composeuchar (int32_t *unicodes, int n_unicodes, |
| 589 |
const char *feat, struct glyph_mapper *gm, USHORT *gid) |
| 590 |
{ |
| 591 |
USHORT *gids; |
| 592 |
int i, error = 0; |
| 593 |
|
| 594 |
if (!gm->codetogid) |
| 595 |
return -1; |
| 596 |
|
| 597 |
gids = NEW(n_unicodes, USHORT); |
| 598 |
for (i = 0; |
| 599 |
!error && i < n_unicodes; i++) { |
| 600 |
gids[i] = tt_cmap_lookup(gm->codetogid, unicodes[i]); |
| 601 |
error = (gids[i] == 0) ? -1 : 0; |
| 602 |
} |
| 603 |
|
| 604 |
if (!error) |
| 605 |
error = composeglyph(gids, n_unicodes, feat, gm, gid); |
| 606 |
|
| 607 |
RELEASE(gids); |
| 608 |
|
| 609 |
return error; |
| 610 |
} |
| 611 |
|
| 612 |
/* Search 'post' table. */ |
| 613 |
static int |
| 614 |
findposttable (const char *glyph_name, USHORT *gid, struct glyph_mapper *gm) |
| 615 |
{ |
| 616 |
if (!gm->nametogid) |
| 617 |
return -1; |
| 618 |
|
| 619 |
*gid = tt_lookup_post_table(gm->nametogid, glyph_name); |
| 620 |
#if 0 |
| 621 |
if (dpx_conf.verbose_level > 1) |
| 622 |
{ |
| 623 |
if (*gid > 0) |
| 624 |
MESG("%s =post=> 0x%04X\n", glyph_name, *gid); |
| 625 |
} |
| 626 |
#endif |
| 627 |
|
| 628 |
return (*gid == 0 ? -1 : 0); |
| 629 |
} |
| 630 |
|
| 631 |
/* This is wrong. We must care about '.'. */ |
| 632 |
#define is_comp(n) (strchr((n), '_') != NULL) |
| 633 |
|
| 634 |
/* Glyph names are concatinated with '_'. */ |
| 635 |
static int |
| 636 |
findcomposite (const char *glyphname, USHORT *gid, struct glyph_mapper *gm) |
| 637 |
{ |
| 638 |
char *gname, *suffix = NULL; |
| 639 |
USHORT gids[32]; |
| 640 |
char *nptrs[32]; |
| 641 |
int i, n_comp; |
| 642 |
int error = 0; |
| 643 |
|
| 644 |
error = findposttable(glyphname, gid, gm); |
| 645 |
if (!error) |
| 646 |
return 0; |
| 647 |
|
| 648 |
gname = NEW(strlen(glyphname) + 1, char); |
| 649 |
strcpy(gname, glyphname); |
| 650 |
|
| 651 |
memset(gids, 0, 32 * sizeof(USHORT)); |
| 652 |
n_comp = agl_decompose_glyphname(gname, nptrs, 32, &suffix); |
| 653 |
for (error = 0, i = 0; !error && i < n_comp; i++) { |
| 654 |
error = resolve_glyph(nptrs[i], &gids[i], gm); |
| 655 |
if (error) |
| 656 |
WARN("Could not resolve glyph \"%s\" (%dth component of glyph \"%s\").", |
| 657 |
nptrs[i], i, glyphname); |
| 658 |
} |
| 659 |
|
| 660 |
if (!error) { |
| 661 |
if (suffix && |
| 662 |
(!strcmp(suffix, "liga") || !strcmp(suffix, "dlig") || |
| 663 |
!strcmp(suffix, "hlig") || !strcmp(suffix, "frac") || |
| 664 |
!strcmp(suffix, "ccmp") || !strcmp(suffix, "afrc") |
| 665 |
) |
| 666 |
) { |
| 667 |
error = composeglyph(gids, n_comp, suffix, gm, gid); |
| 668 |
} else { /* first try composing glyph */ |
| 669 |
error = composeglyph(gids, n_comp, NULL, gm, gid); |
| 670 |
if (!error && suffix) /* a_b_c.vert */ |
| 671 |
error = selectglyph(*gid, suffix, gm, gid); |
| 672 |
} |
| 673 |
} |
| 674 |
RELEASE(gname); |
| 675 |
|
| 676 |
return error; |
| 677 |
} |
| 678 |
|
| 679 |
/* glyphname should not have suffix here */ |
| 680 |
static int |
| 681 |
findparanoiac (const char *glyphname, USHORT *gid, struct glyph_mapper *gm) |
| 682 |
{ |
| 683 |
agl_name *agln; |
| 684 |
USHORT idx = 0U; |
| 685 |
int error = 0; |
| 686 |
|
| 687 |
agln = agl_lookup_list(glyphname); |
| 688 |
while (agln && idx == 0) { |
| 689 |
if (agln->suffix) { |
| 690 |
error = findparanoiac(agln->name, &idx, gm); |
| 691 |
if (error) |
| 692 |
return error; |
| 693 |
|
| 694 |
error = selectglyph(idx, agln->suffix, gm, &idx); |
| 695 |
if (error) { |
| 696 |
WARN("Variant \"%s\" for glyph \"%s\" might not be found.", |
| 697 |
agln->suffix, agln->name); |
| 698 |
WARN("Using glyph name without suffix instead..."); |
| 699 |
error = 0; /* ignore */ |
| 700 |
} |
| 701 |
} else { |
| 702 |
if (agln->n_components == 1) |
| 703 |
idx = tt_cmap_lookup(gm->codetogid, agln->unicodes[0]); |
| 704 |
else if (agln->n_components > 1) { |
| 705 |
if (dpx_conf.verbose_level >= 0) /* give warning */ |
| 706 |
WARN("Glyph \"%s\" looks like a composite glyph...", |
| 707 |
agln->name); |
| 708 |
error = composeuchar(agln->unicodes, agln->n_components, NULL, gm, &idx); |
| 709 |
if (dpx_conf.verbose_level >= 0) { |
| 710 |
if (error) |
| 711 |
WARN("Not found..."); |
| 712 |
else { |
| 713 |
int _i, _n = 0; |
| 714 |
char *_p, _buf[256]; |
| 715 |
WARN(">> Composite glyph glyph-name=\"%s\" found at glyph-id=\"%u\".", |
| 716 |
agln->name, idx); |
| 717 |
for (_p = _buf, _i = 0; _i < agln->n_components && _n < 245; _i++) { |
| 718 |
_p[_n++] = _i == 0 ? '<' : ' '; |
| 719 |
if (agln->unicodes[_i] >= 0x10000) |
| 720 |
_n += sprintf(_p+_n, "U+%06X", agln->unicodes[_i]); |
| 721 |
else |
| 722 |
_n += sprintf(_p+_n, "U+%04X", agln->unicodes[_i]); |
| 723 |
_p[_n++] = _i == agln->n_components - 1 ? '>' : ','; |
| 724 |
} |
| 725 |
_p[_n++] = '\0'; |
| 726 |
WARN(">> Input Unicode seq.=\"%s\" ==> glyph-id=\"%u\" in font-file=\"_please_try_-v_\".", _buf, idx); |
| 727 |
} |
| 728 |
} |
| 729 |
} else ASSERT(0); /* Boooo */ |
| 730 |
} |
| 731 |
agln = agln->alternate; |
| 732 |
} |
| 733 |
|
| 734 |
*gid = idx; |
| 735 |
return (idx == 0 ? -1 : 0); |
| 736 |
} |
| 737 |
|
| 738 |
static int |
| 739 |
resolve_glyph (const char *glyphname, USHORT *gid, struct glyph_mapper *gm) |
| 740 |
{ |
| 741 |
int error = 0; |
| 742 |
char *name, *suffix = NULL; |
| 743 |
int32_t ucv; |
| 744 |
|
| 745 |
ASSERT(glyphname); |
| 746 |
|
| 747 |
/* |
| 748 |
* First we try glyph name to GID mapping using post table if post table |
| 749 |
* is available. If post table is not available or glyph is not listed |
| 750 |
* in the post table, then we try Unicode if Windows-Unicode TrueType |
| 751 |
* cmap is available. |
| 752 |
*/ |
| 753 |
error = findposttable(glyphname, gid, gm); |
| 754 |
if (!error) |
| 755 |
return 0; |
| 756 |
|
| 757 |
if (!gm->codetogid) |
| 758 |
return -1; |
| 759 |
|
| 760 |
name = agl_chop_suffix(glyphname, &suffix); |
| 761 |
if (!name) /* .notdef, .foo */ |
| 762 |
error = -1; |
| 763 |
else if (agl_name_is_unicode(name)) { |
| 764 |
ucv = agl_name_convert_unicode(name); |
| 765 |
*gid = tt_cmap_lookup(gm->codetogid, ucv); |
| 766 |
error = (*gid == 0) ? -1 : 0; |
| 767 |
} else { |
| 768 |
error = findparanoiac(name, gid, gm); |
| 769 |
} |
| 770 |
if (!error && suffix) { |
| 771 |
error = selectglyph(*gid, suffix, gm, gid); |
| 772 |
if (error) { |
| 773 |
WARN("Variant \"%s\" for glyph \"%s\" might not be found.", |
| 774 |
suffix, name); |
| 775 |
WARN("Using glyph name without suffix instead..."); |
| 776 |
error = 0; /* ignore */ |
| 777 |
} |
| 778 |
} |
| 779 |
if (suffix) |
| 780 |
RELEASE(suffix); |
| 781 |
if (name) |
| 782 |
RELEASE(name); |
| 783 |
|
| 784 |
return error; |
| 785 |
} |
| 786 |
|
| 787 |
/* Things are complicated. We still need to use PostScript |
| 788 |
* glyph names. But OpenType fonts may not have PS name to |
| 789 |
* glyph mapping. We use Unicode plus OTL GSUB for finding |
| 790 |
* glyphs in this case. |
| 791 |
*/ |
| 792 |
static int |
| 793 |
setup_glyph_mapper (struct glyph_mapper *gm, sfnt *sfont) |
| 794 |
{ |
| 795 |
gm->sfont = sfont; |
| 796 |
gm->nametogid = tt_read_post_table(sfont); |
| 797 |
gm->codetogid = tt_cmap_read(sfont, TT_WIN, TT_WIN_UCS4); |
| 798 |
if (!gm->codetogid) |
| 799 |
gm->codetogid = tt_cmap_read(sfont, TT_WIN, TT_WIN_UNICODE); |
| 800 |
|
| 801 |
if (!gm->nametogid && !gm->codetogid) |
| 802 |
return -1; |
| 803 |
|
| 804 |
gm->gsub = otl_gsub_new(); |
| 805 |
|
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
static void |
| 810 |
clean_glyph_mapper (struct glyph_mapper *gm) |
| 811 |
{ |
| 812 |
if (gm->gsub) |
| 813 |
otl_gsub_release(gm->gsub); |
| 814 |
if (gm->codetogid) |
| 815 |
tt_cmap_release (gm->codetogid); |
| 816 |
if (gm->nametogid) |
| 817 |
tt_release_post_table(gm->nametogid); |
| 818 |
|
| 819 |
gm->gsub = NULL; |
| 820 |
gm->codetogid = NULL; |
| 821 |
gm->nametogid = NULL; |
| 822 |
gm->sfont = NULL; |
| 823 |
|
| 824 |
return; |
| 825 |
} |
| 826 |
|
| 827 |
static int |
| 828 |
do_custom_encoding (pdf_font *font, |
| 829 |
char **encoding, const char *usedchars, sfnt *sfont) |
| 830 |
{ |
| 831 |
struct tt_glyphs *glyphs; |
| 832 |
char *cmap_table; |
| 833 |
int code, count; |
| 834 |
double widths[256]; |
| 835 |
struct glyph_mapper gm; |
| 836 |
USHORT idx, gid; |
| 837 |
USHORT code_to_idx[256] = {0}; |
| 838 |
int error = 0; |
| 839 |
int cmap_old_format = 0; |
| 840 |
|
| 841 |
ASSERT(font && encoding && usedchars && sfont); |
| 842 |
|
| 843 |
error = setup_glyph_mapper(&gm, sfont); |
| 844 |
if (error) { |
| 845 |
WARN("No post table nor Unicode cmap found in font: %s", font->filename); |
| 846 |
WARN(">> I can't find glyphs without this!"); |
| 847 |
return -1; |
| 848 |
} |
| 849 |
|
| 850 |
if (cmap_old_format) { |
| 851 |
cmap_table = NEW(274, char); |
| 852 |
memset(cmap_table, 0, 274); |
| 853 |
sfnt_put_ushort(cmap_table, 0); /* Version */ |
| 854 |
sfnt_put_ushort(cmap_table+2, 1); /* Number of subtables */ |
| 855 |
sfnt_put_ushort(cmap_table+4, TT_MAC); /* Platform ID */ |
| 856 |
sfnt_put_ushort(cmap_table+6, TT_MAC_ROMAN); /* Encoding ID */ |
| 857 |
sfnt_put_ulong (cmap_table+8, 12); /* Offset */ |
| 858 |
sfnt_put_ushort(cmap_table+12, 0); /* Format */ |
| 859 |
sfnt_put_ushort(cmap_table+14, 262); /* Length */ |
| 860 |
sfnt_put_ushort(cmap_table+16, 0); /* Language */ |
| 861 |
} else { |
| 862 |
cmap_table = NEW(1086, char); /* = 20 + 522 + 544 */ |
| 863 |
memset(cmap_table, 0, 1086); |
| 864 |
sfnt_put_ushort(cmap_table, 0); /* Version */ |
| 865 |
sfnt_put_ushort(cmap_table+2, 2); /* Number of subtables */ |
| 866 |
sfnt_put_ushort(cmap_table+4, TT_MAC); /* Platform ID */ |
| 867 |
sfnt_put_ushort(cmap_table+6, TT_MAC_ROMAN); /* Encoding ID */ |
| 868 |
sfnt_put_ulong (cmap_table+8, 20); /* Offset */ |
| 869 |
sfnt_put_ushort(cmap_table+12, TT_WIN); /* Platform ID */ |
| 870 |
sfnt_put_ushort(cmap_table+14, TT_WIN_SYMBOL);/* Encoding ID */ |
| 871 |
sfnt_put_ulong (cmap_table+16, 542); /* Offset = 20 + 522 */ |
| 872 |
/* (1, 0) subtable */ |
| 873 |
sfnt_put_ushort(cmap_table+20, 6); /* format */ |
| 874 |
sfnt_put_ushort(cmap_table+22, 522); /* length = 10 + 512 */ |
| 875 |
sfnt_put_ushort(cmap_table+24, 0); /* language */ |
| 876 |
sfnt_put_ushort(cmap_table+26, 0); /* firstCode */ |
| 877 |
sfnt_put_ushort(cmap_table+28, 256); /* entryCount */ |
| 878 |
/* (3, 0) subtable */ |
| 879 |
sfnt_put_ushort(cmap_table+542, 4); /* format */ |
| 880 |
sfnt_put_ushort(cmap_table+544, 544); /* length = 16 + 8 * 2 + 512 */ |
| 881 |
sfnt_put_ushort(cmap_table+546, 0); /* language */ |
| 882 |
sfnt_put_ushort(cmap_table+548, 4); /* segCountX2 */ |
| 883 |
sfnt_put_ushort(cmap_table+550, 4); /* searchRange */ |
| 884 |
sfnt_put_ushort(cmap_table+552, 1); /* entrySelector */ |
| 885 |
sfnt_put_ushort(cmap_table+554, 0); /* rangeShift */ |
| 886 |
sfnt_put_ushort(cmap_table+556, 0xf0ff) /* endCode[0] */ |
| 887 |
sfnt_put_ushort(cmap_table+558, 0xffff) /* endCode[1] */ |
| 888 |
sfnt_put_ushort(cmap_table+560, 0); /* reservedPad */ |
| 889 |
sfnt_put_ushort(cmap_table+562, 0xf000); /* startCode[0] */ |
| 890 |
sfnt_put_ushort(cmap_table+564, 0xffff); /* startCode[1] */ |
| 891 |
sfnt_put_short (cmap_table+566, 0); /* idDelta[0] */ |
| 892 |
sfnt_put_short (cmap_table+568, 1); /* idDelta[1] */ |
| 893 |
sfnt_put_ushort(cmap_table+570, 4); /* idRangeOffset[0] */ |
| 894 |
sfnt_put_ushort(cmap_table+572, 0); /* idRangeOffset[1] */ |
| 895 |
} |
| 896 |
|
| 897 |
glyphs = tt_build_init(); |
| 898 |
|
| 899 |
count = 1; /* +1 for .notdef */ |
| 900 |
for (code = 0; code < 256; code++) { |
| 901 |
if (!usedchars[code]) |
| 902 |
continue; |
| 903 |
|
| 904 |
if (!encoding[code] || !strcmp(encoding[code], ".notdef")) { |
| 905 |
WARN("Character code=\"0x%02X\" mapped to \".notdef\" glyph used in font font-file=\"%s\"", |
| 906 |
code, font->filename); |
| 907 |
WARN(">> Maybe incorrect encoding specified?"); |
| 908 |
idx = 0; |
| 909 |
} else { |
| 910 |
if (is_comp(encoding[code])) |
| 911 |
error = findcomposite(encoding[code], &gid, &gm); |
| 912 |
else |
| 913 |
error = resolve_glyph(encoding[code], &gid, &gm); |
| 914 |
|
| 915 |
/* |
| 916 |
* Older versions of gs had problem with glyphs (other than .notdef) |
| 917 |
* mapped to gid = 0. |
| 918 |
*/ |
| 919 |
if (error) { |
| 920 |
WARN("Glyph \"%s\" not available in font \"%s\".", |
| 921 |
encoding[code], font->filename); |
| 922 |
} else { |
| 923 |
if (dpx_conf.verbose_level > 1) |
| 924 |
MESG("truetype>> Glyph glyph-name=\"%s\" found at glyph-id=\"%u\".\n", encoding[code], gid); |
| 925 |
} |
| 926 |
idx = tt_find_glyph(glyphs, gid); |
| 927 |
if (idx == 0) { |
| 928 |
idx = tt_add_glyph(glyphs, (USHORT)gid, (USHORT)count); /* count returned. */ |
| 929 |
count++; |
| 930 |
} |
| 931 |
} |
| 932 |
code_to_idx[code] = idx; |
| 933 |
if (cmap_old_format) { |
| 934 |
if (idx >= 256) { |
| 935 |
WARN("Font font-file=\"%s\" uses more than 255 glyphs", font->filename); |
| 936 |
WARN(">> Glyph glyph-name=\"%s\" will be inaccessible.", encoding[code]); |
| 937 |
cmap_table[18 + code] = 0; |
| 938 |
} else { |
| 939 |
cmap_table[18 + code] = (char)(BYTE)idx; |
| 940 |
} |
| 941 |
} else { |
| 942 |
sfnt_put_ushort(cmap_table + 30 + 2 * code, idx); |
| 943 |
sfnt_put_ushort(cmap_table + 574 + 2 * code, idx); |
| 944 |
} |
| 945 |
} |
| 946 |
clean_glyph_mapper(&gm); |
| 947 |
|
| 948 |
if (tt_build_tables(sfont, glyphs) < 0) { |
| 949 |
WARN("Packing TrueType font into SFNT file faild..."); /* _FIXME_: wrong message */ |
| 950 |
tt_build_finish(glyphs); |
| 951 |
RELEASE(cmap_table); |
| 952 |
return -1; |
| 953 |
} |
| 954 |
|
| 955 |
for (code = 0; code < 256; code++) { |
| 956 |
if (usedchars[code]) { |
| 957 |
idx = tt_get_index(glyphs, code_to_idx[code]); |
| 958 |
widths[code] = PDFUNIT(glyphs->gd[idx].advw); |
| 959 |
} else { |
| 960 |
widths[code] = 0.0; |
| 961 |
} |
| 962 |
} |
| 963 |
do_widths(font, widths); |
| 964 |
|
| 965 |
if (dpx_conf.verbose_level > 1) |
| 966 |
MESG("[%d glyphs]", glyphs->num_glyphs); |
| 967 |
|
| 968 |
tt_build_finish(glyphs); |
| 969 |
|
| 970 |
if (cmap_old_format) { |
| 971 |
sfnt_set_table(sfont, "cmap", cmap_table, 274); |
| 972 |
} else { |
| 973 |
sfnt_set_table(sfont, "cmap", cmap_table, 1086); |
| 974 |
} |
| 975 |
|
| 976 |
return 0; |
| 977 |
} |
| 978 |
|
| 979 |
int |
| 980 |
pdf_font_load_truetype (pdf_font *font) |
| 981 |
{ |
| 982 |
pdf_obj *descriptor = font->descriptor; |
| 983 |
char *ident = font->filename; |
| 984 |
int encoding_id = font->encoding_id; |
| 985 |
char *usedchars = font->usedchars; |
| 986 |
#ifdef ENABLE_NOEMBED |
| 987 |
int embedding = font->flags & PDF_FONT_FLAG_NOEMBED ? 0 : 1; |
| 988 |
#endif /* ENABLE_NOEMBED */ |
| 989 |
int index = font->index; |
| 990 |
char **enc_vec; |
| 991 |
pdf_obj *fontfile; |
| 992 |
FILE *fp = NULL; |
| 993 |
sfnt *sfont; |
| 994 |
int i, error = 0; |
| 995 |
|
| 996 |
if (!font->reference) |
| 997 |
return 0; |
| 998 |
|
| 999 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_TTFONT); |
| 1000 |
if (!fp) { |
| 1001 |
fp = DPXFOPEN(ident, DPX_RES_TYPE_DFONT); |
| 1002 |
if (!fp) ERROR("Unable to open TrueType/dfont font file: %s", ident); /* Should find *truetype* here */ |
| 1003 |
sfont = dfont_open(fp, index); |
| 1004 |
} else { |
| 1005 |
sfont = sfnt_open(fp); |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (!sfont) { |
| 1009 |
ERROR("Unable to open TrueType/dfont file: %s", ident); |
| 1010 |
if (fp) |
| 1011 |
DPXFCLOSE(fp); |
| 1012 |
return -1; |
| 1013 |
} else if (sfont->type != SFNT_TYPE_TRUETYPE && |
| 1014 |
sfont->type != SFNT_TYPE_TTC && |
| 1015 |
sfont->type != SFNT_TYPE_DFONT) { |
| 1016 |
ERROR("Font \"%s\" not a TrueType/dfont font?", ident); |
| 1017 |
sfnt_close(sfont); |
| 1018 |
if (fp) |
| 1019 |
DPXFCLOSE(fp); |
| 1020 |
return -1; |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (sfont->type == SFNT_TYPE_TTC) { |
| 1024 |
ULONG offset; |
| 1025 |
offset = ttc_read_offset(sfont, index); |
| 1026 |
if (offset == 0) ERROR("Invalid TTC index in %s.", ident); |
| 1027 |
error = sfnt_read_table_directory(sfont, offset); |
| 1028 |
} else { |
| 1029 |
error = sfnt_read_table_directory(sfont, sfont->offset); |
| 1030 |
} |
| 1031 |
|
| 1032 |
if (error) { |
| 1033 |
ERROR("Reading SFND table dir failed for font-file=\"%s\"... Not a TrueType font?", ident); |
| 1034 |
sfnt_close(sfont); |
| 1035 |
if (fp) |
| 1036 |
DPXFCLOSE(fp); |
| 1037 |
return -1; |
| 1038 |
} |
| 1039 |
|
| 1040 |
/* |
| 1041 |
* Create new TrueType cmap table with MacRoman encoding. |
| 1042 |
*/ |
| 1043 |
if (encoding_id < 0) |
| 1044 |
error = do_builtin_encoding(font, usedchars, sfont); |
| 1045 |
else { |
| 1046 |
enc_vec = pdf_encoding_get_encoding(encoding_id); |
| 1047 |
error = do_custom_encoding(font, enc_vec, usedchars, sfont); |
| 1048 |
} |
| 1049 |
if (error) { |
| 1050 |
ERROR("Error occured while creating font subfont for \"%s\"", ident); |
| 1051 |
sfnt_close(sfont); |
| 1052 |
if (fp) |
| 1053 |
DPXFCLOSE(fp); |
| 1054 |
return -1; |
| 1055 |
} |
| 1056 |
|
| 1057 |
#if ENABLE_NOEMBED |
| 1058 |
if (!embedding) { |
| 1059 |
sfnt_close(sfont); |
| 1060 |
if (fp) |
| 1061 |
DPXFCLOSE(fp); |
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
#endif /* ENABLE_NOEMBED */ |
| 1065 |
|
| 1066 |
/* |
| 1067 |
* TODO: post table? |
| 1068 |
*/ |
| 1069 |
|
| 1070 |
for (i = 0; required_table[i].name != NULL; i++) { |
| 1071 |
if (sfnt_require_table(sfont, |
| 1072 |
required_table[i].name, |
| 1073 |
required_table[i].must_exist) < 0) { |
| 1074 |
ERROR("Required TrueType table \"%s\" does not exist in font: %s", |
| 1075 |
required_table[i].name, ident); |
| 1076 |
sfnt_close(sfont); |
| 1077 |
if (fp) |
| 1078 |
DPXFCLOSE(fp); |
| 1079 |
return -1; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* |
| 1084 |
* FontFile2 |
| 1085 |
*/ |
| 1086 |
fontfile = sfnt_create_FontFile_stream(sfont); |
| 1087 |
if (!fontfile) |
| 1088 |
ERROR("Could not created FontFile stream for \"%s\".", ident); |
| 1089 |
|
| 1090 |
sfnt_close(sfont); |
| 1091 |
if (fp) |
| 1092 |
DPXFCLOSE(fp); |
| 1093 |
|
| 1094 |
if (dpx_conf.verbose_level > 1) |
| 1095 |
MESG("[%ld bytes]", pdf_stream_length(fontfile)); |
| 1096 |
|
| 1097 |
pdf_add_dict(descriptor, |
| 1098 |
pdf_new_name("FontFile2"), pdf_ref_obj(fontfile)); /* XXX */ |
| 1099 |
pdf_release_obj(fontfile); |
| 1100 |
|
| 1101 |
return 0; |
| 1102 |
} |