| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2006-2012 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
#include "font/writettf.h" |
| 24 |
#include "font/writecff.h" |
| 25 |
#include "lua/luatex-api.h" |
| 26 |
|
| 27 |
#include "font/sfnt.h" |
| 28 |
#include "font/tt_glyf.h" |
| 29 |
|
| 30 |
/*tex |
| 31 |
|
| 32 |
Forward declarations |
| 33 |
|
| 34 |
*/ |
| 35 |
|
| 36 |
boolean make_tt_subset(PDF pdf, fd_entry * fd, unsigned char *buff, int buflen); |
| 37 |
|
| 38 |
unsigned long cidtogid_obj = 0; |
| 39 |
|
| 40 |
/*tex |
| 41 |
|
| 42 |
Low-level helpers (a weird place): |
| 43 |
|
| 44 |
*/ |
| 45 |
|
| 46 |
#define test_loc(l) \ |
| 47 |
if ((f->loc + l) > f->buflen) { \ |
| 48 |
normal_error("type 2","the file ended prematurely"); \ |
| 49 |
} |
| 50 |
|
| 51 |
BYTE get_unsigned_byte(sfnt * f) |
| 52 |
{ |
| 53 |
test_loc(1); |
| 54 |
return (BYTE) f->buffer[(f->loc++)]; |
| 55 |
} |
| 56 |
|
| 57 |
ICHAR get_signed_byte(sfnt * f) |
| 58 |
{ |
| 59 |
test_loc(1); |
| 60 |
return (ICHAR) f->buffer[(f->loc++)]; |
| 61 |
} |
| 62 |
|
| 63 |
USHORT get_unsigned_pair(sfnt * f) |
| 64 |
{ |
| 65 |
USHORT l; |
| 66 |
test_loc(2); |
| 67 |
l = f->buffer[(f->loc++)]; |
| 68 |
l = (USHORT) (l * 0x100 + f->buffer[(f->loc++)]); |
| 69 |
return l; |
| 70 |
} |
| 71 |
|
| 72 |
SHORT get_signed_pair(sfnt * f) |
| 73 |
{ |
| 74 |
long l; |
| 75 |
test_loc(2); |
| 76 |
l = f->buffer[(f->loc++)]; |
| 77 |
if (l > 0x80) |
| 78 |
l -= 0x100; |
| 79 |
l = l * 0x100 + f->buffer[(f->loc++)]; |
| 80 |
return (SHORT) l; |
| 81 |
} |
| 82 |
|
| 83 |
ULONG get_unsigned_quad(sfnt * f) |
| 84 |
{ |
| 85 |
ULONG l; |
| 86 |
test_loc(4); |
| 87 |
l = f->buffer[(f->loc++)]; |
| 88 |
l = l * 0x100 + f->buffer[(f->loc++)]; |
| 89 |
l = l * 0x100 + f->buffer[(f->loc++)]; |
| 90 |
l = l * 0x100 + f->buffer[(f->loc++)]; |
| 91 |
return l; |
| 92 |
} |
| 93 |
|
| 94 |
int do_sfnt_read(unsigned char *dest, int len, sfnt * f) |
| 95 |
{ |
| 96 |
int i; |
| 97 |
test_loc(len); |
| 98 |
for (i = 0; i < len; i++) { |
| 99 |
*(dest + i) = f->buffer[f->loc + i]; |
| 100 |
} |
| 101 |
f->loc += len; |
| 102 |
return len; |
| 103 |
} |
| 104 |
|
| 105 |
/*tex |
| 106 |
|
| 107 |
The main function: |
| 108 |
|
| 109 |
*/ |
| 110 |
|
| 111 |
boolean writetype2(PDF pdf, fd_entry * fd) |
| 112 |
{ |
| 113 |
int callback_id; |
| 114 |
int file_opened = 0; |
| 115 |
boolean ret; |
| 116 |
glyph_tab = NULL; |
| 117 |
/*tex |fd_cur| is global inside |writettf.c| */ |
| 118 |
fd_cur = fd; |
| 119 |
assert(fd_cur->fm != NULL); |
| 120 |
assert(is_truetype(fd_cur->fm)); |
| 121 |
assert(is_included(fd_cur->fm)); |
| 122 |
ttf_curbyte = 0; |
| 123 |
ttf_size = 0; |
| 124 |
cur_file_name = |
| 125 |
luatex_find_file(fd_cur->fm->ff_name, find_opentype_file_callback); |
| 126 |
if (cur_file_name == NULL) { |
| 127 |
formatted_error("type 2","cannot find file '%s'", fd_cur->fm->ff_name); |
| 128 |
} |
| 129 |
callback_id = callback_defined(read_opentype_file_callback); |
| 130 |
if (callback_id > 0) { |
| 131 |
if (run_callback(callback_id, "S->bSd", cur_file_name, |
| 132 |
&file_opened, &ttf_buffer, &ttf_size) && |
| 133 |
file_opened && ttf_size > 0) { |
| 134 |
} else { |
| 135 |
formatted_error("type 2","cannot find file '%s'", cur_file_name); |
| 136 |
} |
| 137 |
} else { |
| 138 |
if (!otf_open(cur_file_name)) { |
| 139 |
formatted_error("type 2","cannot find file '%s'", cur_file_name); |
| 140 |
} |
| 141 |
ttf_read_file(); |
| 142 |
ttf_close(); |
| 143 |
} |
| 144 |
fd_cur->ff_found = true; |
| 145 |
if (is_subsetted(fd_cur->fm)) |
| 146 |
report_start_file(filetype_subset,cur_file_name); |
| 147 |
else |
| 148 |
report_start_file(filetype_font,cur_file_name); |
| 149 |
/*tex Here is the real work done: */ |
| 150 |
ret = make_tt_subset(pdf, fd, ttf_buffer, ttf_size); |
| 151 |
xfree(ttf_buffer); |
| 152 |
if (is_subsetted(fd_cur->fm)) |
| 153 |
report_stop_file(filetype_subset); |
| 154 |
else |
| 155 |
report_stop_file(filetype_font); |
| 156 |
cur_file_name = NULL; |
| 157 |
return ret; |
| 158 |
} |
| 159 |
|
| 160 |
/*tex |
| 161 |
|
| 162 |
The \PDF\ viewer applications use following tables for CIDFontType 2: |head|, |
| 163 |
|hhea|, |loca|, |maxp|, |glyf|, |hmtx|, |fpgm|, |cvt| and |prep|. According |
| 164 |
to PDF Ref. v.1.3, 2nd ed. The |fpgm\, |cvt| and |prep| tables appears only |
| 165 |
when TrueType instructions requires them. Those tables must be preserved if |
| 166 |
they exist. We use |must_exist| flag to indicate `preserve it if present' and |
| 167 |
to make sure not to cause an error when it does not exist. The |post\ and |
| 168 |
|name| tables must exist in ordinary TrueType font file, but when a TrueType |
| 169 |
font is converted to CIDFontType 2 font, those tables are no longer required. |
| 170 |
The |OS/2| table (required for TrueType font for Windows and OS/2) contains |
| 171 |
liscencing information, but PDF viewers seems not using them. The |name| |
| 172 |
table as been added added too (see comments in |writettf.c|. |
| 173 |
|
| 174 |
*/ |
| 175 |
|
| 176 |
static struct { |
| 177 |
const char *name; |
| 178 |
int must_exist; |
| 179 |
} |
| 180 |
|
| 181 |
required_table[] = { |
| 182 |
{ "OS/2", 0 }, |
| 183 |
{ "cmap", 0 }, |
| 184 |
{ "head", 1 }, |
| 185 |
{ "hhea", 1 }, |
| 186 |
{ "loca", 1 }, |
| 187 |
{ "maxp", 0 }, |
| 188 |
{ "name", 1 }, |
| 189 |
{ "glyf", 1 }, |
| 190 |
{ "hmtx", 1 }, |
| 191 |
{ "fpgm", 0 }, |
| 192 |
{ "cvt ", 0 }, |
| 193 |
{ "prep", 0 }, |
| 194 |
{ NULL, 0 } |
| 195 |
}; |
| 196 |
|
| 197 |
unsigned long ttc_read_offset(sfnt * sfont, int ttc_idx, fd_entry * fd) |
| 198 |
{ |
| 199 |
unsigned long offset = 0; |
| 200 |
unsigned long num_dirs = 0; |
| 201 |
/*tex Skip the |ULONG| version tag: */ |
| 202 |
sfnt_seek_set(sfont, 4); |
| 203 |
/* version = */ (void)sfnt_get_ulong(sfont); |
| 204 |
num_dirs = sfnt_get_ulong(sfont); |
| 205 |
if (ttc_idx < 0 || ttc_idx > (int) (num_dirs - 1)) { |
| 206 |
formatted_error("type 2", |
| 207 |
"invalid TTC index number %i (0..%i), using index 0 for font %s", |
| 208 |
ttc_idx,(int) (num_dirs - 1),(fd->fm->ps_name ? fd->fm->ps_name : "")); |
| 209 |
return 0 ; |
| 210 |
} |
| 211 |
sfnt_seek_set(sfont, 12 + ttc_idx * 4); |
| 212 |
offset = sfnt_get_ulong(sfont); |
| 213 |
return offset; |
| 214 |
} |
| 215 |
|
| 216 |
/*tex |
| 217 |
|
| 218 |
Creating the subset. |
| 219 |
*/ |
| 220 |
|
| 221 |
extern int cidset; |
| 222 |
|
| 223 |
boolean make_tt_subset(PDF pdf, fd_entry * fd, unsigned char *buff, int buflen) |
| 224 |
{ |
| 225 |
|
| 226 |
long i, cid; |
| 227 |
unsigned int last_cid = 0; |
| 228 |
glw_entry *found; |
| 229 |
struct avl_traverser t; |
| 230 |
unsigned char *cidtogidmap; |
| 231 |
unsigned short num_glyphs, gid; |
| 232 |
struct tt_glyphs *glyphs; |
| 233 |
char *used_chars = NULL; |
| 234 |
sfnt *sfont; |
| 235 |
pdf_obj *fontfile; |
| 236 |
int error = 0; |
| 237 |
cidtogidmap = NULL; |
| 238 |
sfont = sfnt_open(buff, buflen); |
| 239 |
if (sfont->type == SFNT_TYPE_TTC) { |
| 240 |
i = fd->fm->subfont > 0 ? (fd->fm->subfont - 1) : ff_get_ttc_index(fd->fm->ff_name, fd->fm->ps_name); |
| 241 |
error = sfnt_read_table_directory(sfont, ttc_read_offset(sfont, (int) i, fd)); |
| 242 |
} else { |
| 243 |
error = sfnt_read_table_directory(sfont, 0); |
| 244 |
} |
| 245 |
if (error < 0) { |
| 246 |
normal_error("type 2","parsing the TTF directory fails"); |
| 247 |
} |
| 248 |
if (sfont->type == SFNT_TYPE_TTC && sfnt_find_table_pos(sfont, "CFF ")) { |
| 249 |
sfnt_close(sfont); |
| 250 |
return false; |
| 251 |
} |
| 252 |
if (is_subsetted(fd->fm)) { |
| 253 |
/*tex Rebuild the glyph tables and create a fresh cidmap :*/ |
| 254 |
glyphs = tt_build_init(); |
| 255 |
last_cid = 0; |
| 256 |
avl_t_init(&t, fd->gl_tree); |
| 257 |
for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree); |
| 258 |
found != NULL; found = (glw_entry *) avl_t_next(&t)) { |
| 259 |
if (found->id > last_cid) |
| 260 |
last_cid = found->id; |
| 261 |
} |
| 262 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 263 |
cidtogidmap = NULL; |
| 264 |
#else |
| 265 |
cidtogidmap = xmalloc(((last_cid + 1) * 2) * sizeof(unsigned char)); |
| 266 |
memset(cidtogidmap, 0, (last_cid + 1) * 2); |
| 267 |
#endif |
| 268 |
/*tex Fill |used_chars|: */ |
| 269 |
used_chars = xmalloc((last_cid + 1) * sizeof(char)); |
| 270 |
memset(used_chars, 0, (last_cid + 1)); |
| 271 |
avl_t_init(&t, fd->gl_tree); |
| 272 |
for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree); |
| 273 |
found != NULL; found = (glw_entry *) avl_t_next(&t)) { |
| 274 |
used_chars[found->id] = 1; |
| 275 |
} |
| 276 |
/*tex Map CIDs to GIDs, with |.notdef| in slot zero: */ |
| 277 |
num_glyphs = 1; |
| 278 |
for (cid = 1; cid <= (long) last_cid; cid++) { |
| 279 |
if (used_chars[cid] == 0) |
| 280 |
continue; |
| 281 |
gid = (short unsigned) cid; |
| 282 |
#ifndef NO_GHOSTSCRIPT_BUG |
| 283 |
gid = tt_add_glyph(glyphs, (USHORT) gid, (USHORT) cid); |
| 284 |
#else |
| 285 |
gid = tt_add_glyph(glyphs, (USHORT) gid, (USHORT) num_glyphs); |
| 286 |
cidtogidmap[2 * cid] = gid >> 8; |
| 287 |
cidtogidmap[2 * cid + 1] = gid & 0xff; |
| 288 |
#endif |
| 289 |
num_glyphs++; |
| 290 |
} |
| 291 |
|
| 292 |
if (num_glyphs == 1) { |
| 293 |
normal_error("type 2","there are no glyphs in the subset"); |
| 294 |
} |
| 295 |
if (tt_build_tables(sfont, glyphs, fd) < 0) { |
| 296 |
normal_error("type 2","the TTF buffer can't be parsed"); |
| 297 |
} |
| 298 |
tt_build_finish(glyphs); |
| 299 |
} |
| 300 |
/*tex Create the font file: */ |
| 301 |
for (i = 0; required_table[i].name; i++) { |
| 302 |
if (sfnt_require_table(sfont,required_table[i].name, required_table[i].must_exist) < 0) { |
| 303 |
normal_error("type 2","some required TrueType table does not exist"); |
| 304 |
} |
| 305 |
} |
| 306 |
fontfile = sfnt_create_FontFile_stream(sfont); |
| 307 |
/*tex The |cidgidmap|: */ |
| 308 |
if (cidtogidmap != NULL) { |
| 309 |
/* never seen when ndef NO_GHOSTSCRIPT_BUG */ |
| 310 |
cidtogid_obj = (unsigned long) pdf_create_obj(pdf, obj_type_others, 0); |
| 311 |
pdf_begin_obj(pdf, (int) cidtogid_obj, OBJSTM_NEVER); |
| 312 |
pdf_begin_dict(pdf); |
| 313 |
pdf_dict_add_int(pdf, "Length", ((last_cid + 1) * 2)); |
| 314 |
pdf_end_dict(pdf); |
| 315 |
pdf_begin_stream(pdf); |
| 316 |
pdf_room(pdf, (int) ((last_cid + 1) * 2)); |
| 317 |
for (i = 0; i < ((int) (last_cid + 1) * 2); i++) { |
| 318 |
pdf_quick_out(pdf, cidtogidmap[i]); |
| 319 |
} |
| 320 |
pdf_end_stream(pdf); |
| 321 |
pdf_end_obj(pdf); |
| 322 |
} |
| 323 |
/*tex The tff subset: */ |
| 324 |
for (i = 0; i < (int) (fontfile->length); i++) |
| 325 |
strbuf_putchar(pdf->fb, fontfile->data[i]); |
| 326 |
pdf_release_obj(fontfile); |
| 327 |
/*tex |
| 328 |
|CIDSet| is a table of bits indexed by cid, bytes with high order bit |
| 329 |
first, each (set) bit is a (present) CID. |
| 330 |
*/ |
| 331 |
if (is_subsetted(fd->fm)) { |
| 332 |
if ((! pdf->omit_cidset) && (pdf->major_version == 1)) { |
| 333 |
cidset = pdf_create_obj(pdf, obj_type_others, 0); |
| 334 |
if (cidset != 0) { |
| 335 |
size_t l = (last_cid / 8) + 1; |
| 336 |
char *stream = xmalloc(l); |
| 337 |
memset(stream, 0, l); |
| 338 |
stream[0] |= 1 << 7; /*tex Force |.notdef| into the map. */ |
| 339 |
for (cid = 1; cid <= (long) last_cid; cid++) { |
| 340 |
if (used_chars[cid]) { |
| 341 |
stream[(cid / 8)] |= (1 << (7 - (cid % 8))); |
| 342 |
} |
| 343 |
} |
| 344 |
pdf_begin_obj(pdf, cidset, OBJSTM_NEVER); |
| 345 |
pdf_begin_dict(pdf); |
| 346 |
pdf_dict_add_streaminfo(pdf); |
| 347 |
pdf_end_dict(pdf); |
| 348 |
pdf_begin_stream(pdf); |
| 349 |
pdf_out_block(pdf, stream, l); |
| 350 |
pdf_end_stream(pdf); |
| 351 |
pdf_end_obj(pdf); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
xfree(used_chars); |
| 356 |
sfnt_close(sfont); |
| 357 |
return true; |
| 358 |
} |