1% writetype0.w
2%
3% Copyright 2006-2008 Taco Hoekwater <taco@@luatex.org>
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@ @c
21
22
23#include "ptexlib.h"
24#include "font/writettf.h"
25#include "font/writecff.h"
26
27@ @c
28void writetype0(PDF pdf, fd_entry * fd)
29{
30    int callback_id;
31    int file_opened = 0;
32    long i;
33    dirtab_entry *tab;
34    cff_font *cff;
35
36    dir_tab = NULL;
37    glyph_tab = NULL;
38
39    fd_cur = fd;                /* |fd_cur| is global inside \.{writettf.w} */
40    assert(fd_cur->fm != NULL);
41    assert(is_opentype(fd_cur->fm));
42    assert(is_included(fd_cur->fm));
43
44    ttf_curbyte = 0;
45    ttf_size = 0;
46    cur_file_name =
47        luatex_find_file(fd_cur->fm->ff_name, find_opentype_file_callback);
48    if (cur_file_name == NULL) {
49        luatex_fail("cannot find OpenType font file for reading (%s)",
50                    fd_cur->fm->ff_name);
51    }
52    callback_id = callback_defined(read_opentype_file_callback);
53    if (callback_id > 0) {
54        if (run_callback(callback_id, "S->bSd", cur_file_name,
55                         &file_opened, &ttf_buffer, &ttf_size) &&
56            file_opened && ttf_size > 0) {
57        } else {
58            luatex_fail("cannot open OpenType font file for reading (%s)",
59                        cur_file_name);
60        }
61    } else {
62        if (!otf_open(cur_file_name)) {
63            luatex_fail("cannot open OpenType font file for reading (%s)",
64                        cur_file_name);
65        }
66        ttf_read_file();
67        ttf_close();
68    }
69
70    fd_cur->ff_found = true;
71
72    if (is_subsetted(fd_cur->fm)) {
73        report_start_file(filetype_subset, cur_file_name);
74    } else {
75        report_start_file(filetype_font, cur_file_name);
76    }
77    ttf_read_tabdir();
78    /* read font parameters */
79    if (ttf_name_lookup("head", false) != NULL)
80        ttf_read_head();
81    if (ttf_name_lookup("hhea", false) != NULL)
82        ttf_read_hhea();
83    if (ttf_name_lookup("PCLT", false) != NULL)
84        ttf_read_pclt();
85    if (ttf_name_lookup("post", false) != NULL)
86        ttf_read_post();
87
88    /* copy font file */
89    tab = ttf_seek_tab("CFF ", 0);
90
91    /* TODO the next 0 is a subfont index */
92    cff = read_cff(ttf_buffer + ttf_curbyte, (long) tab->length, 0);
93    if (!is_subsetted(fd_cur->fm)) {
94        /* not subsetted, just do a copy */
95        for (i = (long) tab->length; i > 0; i--)
96            strbuf_putchar(pdf->fb, (unsigned char) ttf_getnum(1));
97    } else {
98        if (cff != NULL) {
99            if (cff_is_cidfont(cff)) {
100                write_cid_cff(pdf, cff, fd_cur);
101#if 0
102                for (i = tab->length; i > 0; i--)
103                    strbuf_putchar(pdf->fb, (unsigned char) ttf_getnum(1));
104#endif
105            } else {
106                write_cff(pdf, cff, fd_cur);
107            }
108        } else {
109            /* not understood, just do a copy */
110            for (i = (long) tab->length; i > 0; i--)
111                strbuf_putchar(pdf->fb, (unsigned char) ttf_getnum(1));
112        }
113    }
114    xfree(dir_tab);
115    xfree(ttf_buffer);
116    if (is_subsetted(fd_cur->fm)) {
117        report_stop_file(filetype_subset);
118    } else {
119        report_stop_file(filetype_font);
120    }
121    cur_file_name = NULL;
122}
123