summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/font/writetype2.c
blob: 49193579b7e12859a2125ac2221653be7d54ac92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*

Copyright 2006-2012 Taco Hoekwater <taco@luatex.org>

This file is part of LuaTeX.

LuaTeX is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.

LuaTeX is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU General Public License along
with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

*/

#include "ptexlib.h"
#include "font/writettf.h"
#include "font/writecff.h"
#include "lua/luatex-api.h"

#include "font/sfnt.h"
#include "font/tt_glyf.h"

/*tex

    Forward declarations

*/

boolean make_tt_subset(PDF pdf, fd_entry * fd, unsigned char *buff, int buflen);

unsigned long cidtogid_obj = 0;

/*tex

    Low-level helpers (a weird place):

*/

#define test_loc(l) \
    if ((f->loc + l) > f->buflen) { \
        normal_error("type 2","the file ended prematurely"); \
    }

BYTE get_unsigned_byte(sfnt * f)
{
    test_loc(1);
    return (BYTE) f->buffer[(f->loc++)];
}

ICHAR get_signed_byte(sfnt * f)
{
    test_loc(1);
    return (ICHAR) f->buffer[(f->loc++)];
}

USHORT get_unsigned_pair(sfnt * f)
{
    USHORT l;
    test_loc(2);
    l = f->buffer[(f->loc++)];
    l = (USHORT) (l * 0x100 + f->buffer[(f->loc++)]);
    return l;
}

SHORT get_signed_pair(sfnt * f)
{
    long l;
    test_loc(2);
    l = f->buffer[(f->loc++)];
    if (l > 0x80)
        l -= 0x100;
    l = l * 0x100 + f->buffer[(f->loc++)];
    return (SHORT) l;
}

ULONG get_unsigned_quad(sfnt * f)
{
    ULONG l;
    test_loc(4);
    l = f->buffer[(f->loc++)];
    l = l * 0x100 + f->buffer[(f->loc++)];
    l = l * 0x100 + f->buffer[(f->loc++)];
    l = l * 0x100 + f->buffer[(f->loc++)];
    return l;
}

int do_sfnt_read(unsigned char *dest, int len, sfnt * f)
{
    int i;
    test_loc(len);
    for (i = 0; i < len; i++) {
        *(dest + i) = f->buffer[f->loc + i];
    }
    f->loc += len;
    return len;
}

/*tex

    The main function:

*/

boolean writetype2(PDF pdf, fd_entry * fd)
{
    int callback_id;
    int file_opened = 0;
    boolean ret;
    glyph_tab = NULL;
    /*tex |fd_cur| is global inside |writettf.c| */
    fd_cur = fd;
    assert(fd_cur->fm != NULL);
    assert(is_truetype(fd_cur->fm));
    assert(is_included(fd_cur->fm));
    ttf_curbyte = 0;
    ttf_size = 0;
    cur_file_name =
        luatex_find_file(fd_cur->fm->ff_name, find_opentype_file_callback);
    if (cur_file_name == NULL) {
        formatted_error("type 2","cannot find file '%s'", fd_cur->fm->ff_name);
    }
    callback_id = callback_defined(read_opentype_file_callback);
    if (callback_id > 0) {
        if (run_callback(callback_id, "S->bSd", cur_file_name,
                         &file_opened, &ttf_buffer, &ttf_size) &&
            file_opened && ttf_size > 0) {
        } else {
            formatted_error("type 2","cannot find file '%s'", cur_file_name);
        }
    } else {
        if (!otf_open(cur_file_name)) {
            formatted_error("type 2","cannot find file '%s'", cur_file_name);
        }
        ttf_read_file();
        ttf_close();
    }
    fd_cur->ff_found = true;
    if (is_subsetted(fd_cur->fm))
        report_start_file(filetype_subset,cur_file_name);
     else
        report_start_file(filetype_font,cur_file_name);
    /*tex Here is the real work done: */
    ret = make_tt_subset(pdf, fd, ttf_buffer, ttf_size);
    xfree(ttf_buffer);
    if (is_subsetted(fd_cur->fm))
        report_stop_file(filetype_subset);
     else
        report_stop_file(filetype_font);
    cur_file_name = NULL;
    return ret;
}

/*tex

    The \PDF\ viewer applications use following tables for CIDFontType 2: |head|,
    |hhea|, |loca|, |maxp|, |glyf|, |hmtx|, |fpgm|, |cvt| and |prep|. According
    to PDF Ref. v.1.3, 2nd ed. The |fpgm\, |cvt| and |prep| tables appears only
    when TrueType instructions requires them. Those tables must be preserved if
    they exist. We use |must_exist| flag to indicate `preserve it if present' and
    to make sure not to cause an error when it does not exist. The |post\ and
    |name| tables must exist in ordinary TrueType font file, but when a TrueType
    font is converted to CIDFontType 2 font, those tables are no longer required.
    The |OS/2| table (required for TrueType font for Windows and OS/2) contains
    liscencing information, but PDF viewers seems not using them. The |name|
    table as been added added too (see comments in |writettf.c|.

*/

static struct {
    const char *name;
    int must_exist;
}

required_table[] = {
    { "OS/2", 0 },
    { "cmap", 0 },
    { "head", 1 },
    { "hhea", 1 },
    { "loca", 1 },
    { "maxp", 0 },
    { "name", 1 },
    { "glyf", 1 },
    { "hmtx", 1 },
    { "fpgm", 0 },
    { "cvt ", 0 },
    { "prep", 0 },
    { NULL,   0 }
};

unsigned long ttc_read_offset(sfnt * sfont, int ttc_idx, fd_entry * fd)
{
    unsigned long offset = 0;
    unsigned long num_dirs = 0;
    /*tex Skip the |ULONG| version tag: */
    sfnt_seek_set(sfont, 4);
    /* version = */ (void)sfnt_get_ulong(sfont);
    num_dirs = sfnt_get_ulong(sfont);
    if (ttc_idx < 0 || ttc_idx > (int) (num_dirs - 1)) {
        formatted_error("type 2",
            "invalid TTC index number %i (0..%i), using index 0 for font %s",
            ttc_idx,(int) (num_dirs - 1),(fd->fm->ps_name ? fd->fm->ps_name : ""));
        return 0 ;
    }
    sfnt_seek_set(sfont, 12 + ttc_idx * 4);
    offset = sfnt_get_ulong(sfont);
    return offset;
}

/*tex

    Creating the subset.
*/

extern int cidset;

boolean make_tt_subset(PDF pdf, fd_entry * fd, unsigned char *buff, int buflen)
{

    long i, cid;
    unsigned int last_cid = 0;
    glw_entry *found;
    struct avl_traverser t;
    unsigned char *cidtogidmap;
    unsigned short num_glyphs, gid;
    struct tt_glyphs *glyphs;
    char *used_chars = NULL;
    sfnt *sfont;
    pdf_obj *fontfile;
    int error = 0;
    cidtogidmap = NULL;
    sfont = sfnt_open(buff, buflen);
    if (sfont->type == SFNT_TYPE_TTC) {
        i = ff_get_ttc_index(fd->fm->ff_name, fd->fm->ps_name);
        error = sfnt_read_table_directory(sfont, ttc_read_offset(sfont, (int) i, fd));
    } else {
        error = sfnt_read_table_directory(sfont, 0);
    }
    if (error < 0) {
        normal_error("type 2","parsing the TTF directory fails");
    }
    if (sfont->type == SFNT_TYPE_TTC && sfnt_find_table_pos(sfont, "CFF ")) {
        sfnt_close(sfont);
        return false;
    }
    if (is_subsetted(fd->fm)) {
        /*tex Rebuild the glyph tables and create a fresh cidmap :*/
        glyphs = tt_build_init();
        last_cid = 0;
        avl_t_init(&t, fd->gl_tree);
        for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree);
             found != NULL; found = (glw_entry *) avl_t_next(&t)) {
            if (found->id > last_cid)
                last_cid = found->id;
        }
#ifndef NO_GHOSTSCRIPT_BUG
        cidtogidmap = NULL;
#else
        cidtogidmap = xmalloc(((last_cid + 1) * 2) * sizeof(unsigned char));
        memset(cidtogidmap, 0, (last_cid + 1) * 2);
#endif
        /*tex Fill |used_chars|: */
        used_chars = xmalloc((last_cid + 1) * sizeof(char));
        memset(used_chars, 0, (last_cid + 1));
        avl_t_init(&t, fd->gl_tree);
        for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree);
             found != NULL; found = (glw_entry *) avl_t_next(&t)) {
            used_chars[found->id] = 1;
        }
        /*tex Map CIDs to GIDs, with |.notdef| in slot zero: */
        num_glyphs = 1;
        for (cid = 1; cid <= (long) last_cid; cid++) {
            if (used_chars[cid] == 0)
                continue;
            gid = (short unsigned) cid;
#ifndef NO_GHOSTSCRIPT_BUG
            gid = tt_add_glyph(glyphs, (USHORT) gid, (USHORT) cid);
#else
            gid = tt_add_glyph(glyphs, (USHORT) gid, (USHORT) num_glyphs);
            cidtogidmap[2 * cid] = gid >> 8;
            cidtogidmap[2 * cid + 1] = gid & 0xff;
#endif
            num_glyphs++;
        }

        if (num_glyphs == 1) {
            normal_error("type 2","there are no glyphs in the subset");
        }
        if (tt_build_tables(sfont, glyphs, fd) < 0) {
            normal_error("type 2","the TTF buffer can't be parsed");
        }
        tt_build_finish(glyphs);
    }
    /*tex Create the font file: */
    for (i = 0; required_table[i].name; i++) {
        if (sfnt_require_table(sfont,required_table[i].name, required_table[i].must_exist) < 0) {
            normal_error("type 2","some required TrueType table does not exist");
        }
    }
    fontfile = sfnt_create_FontFile_stream(sfont);
    /*tex The |cidgidmap|: */
    if (cidtogidmap != NULL) {
        cidtogid_obj = (unsigned long) pdf_create_obj(pdf, obj_type_others, 0);
        pdf_begin_obj(pdf, (int) cidtogid_obj, OBJSTM_NEVER);
        pdf_begin_dict(pdf);
        pdf_dict_add_int(pdf, "Length", ((last_cid + 1) * 2));
        pdf_end_dict(pdf);
        pdf_begin_stream(pdf);
        pdf_room(pdf, (int) ((last_cid + 1) * 2));
        for (i = 0; i < ((int) (last_cid + 1) * 2); i++) {
            pdf_quick_out(pdf, cidtogidmap[i]);
        }
        pdf_end_stream(pdf);
        pdf_end_obj(pdf);
    }
    /*tex The tff subset: */
    for (i = 0; i < (int) (fontfile->length); i++)
        strbuf_putchar(pdf->fb, fontfile->data[i]);
    pdf_release_obj(fontfile);
    /*tex
        |CIDSet| is a table of bits indexed by cid, bytes with high order bit
        first, each (set) bit is a (present) CID.
    */
    if (is_subsetted(fd->fm)) {
        if ((! pdf->omit_cidset) && (pdf->major_version == 1)) {
            cidset = pdf_create_obj(pdf, obj_type_others, 0);
            if (cidset != 0) {
                size_t l = (last_cid / 8) + 1;
                char *stream = xmalloc(l);
                memset(stream, 0, l);
                stream[0] |= 1 << 7; /*tex Force |.notdef| into the map. */
                for (cid = 1; cid <= (long) last_cid; cid++) {
                    if (used_chars[cid]) {
                        stream[(cid / 8)] |= (1 << (7 - (cid % 8)));
                    }
                }
                pdf_begin_obj(pdf, cidset, OBJSTM_NEVER);
                pdf_begin_dict(pdf);
                pdf_dict_add_streaminfo(pdf);
                pdf_end_dict(pdf);
                pdf_begin_stream(pdf);
                pdf_out_block(pdf, stream, l);
                pdf_end_stream(pdf);
                pdf_end_obj(pdf);
            }
        }
    }
    xfree(used_chars);
    sfnt_close(sfont);
    return true;
}