summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/pdf/pdffont.w
blob: 4a7319db8a3fdaf5a4c6040cf1d445fcc41a9f8c (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
% pdffont.w
%
% Copyright 2009-2014 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/>.

\def\pdfTeX{pdf\TeX}

@ @c


#include "ptexlib.h"

@ As \pdfTeX{} should also act as a back-end driver, it needs to support virtual
fonts too. Information about virtual fonts can be found in the source of some
\.{DVI}-related programs.

Whenever we want to write out a character in a font to PDF output, we
should check whether the used character is a virtual or real character.
The |has_packet()| C macro checks for this condition.

@ The following code typesets a character to PDF output

@c
scaled_whd output_one_char(PDF pdf, halfword p)
{
    scaled_whd ci;              /* the real width, height and depth of the character */
    internal_font_number f = font(p);
    int c = character(p);
    int ex_glyph = ex_glyph(p)/1000;
    ci = get_charinfo_whd(f, c);
    if (!(char_exists(f,c))) {
        char_warning(f,c);
        return ci;
    }
    //ci.wd = round_xn_over_d(ci.wd, 1000 + ex_glyph, 1000);
    ci.wd = ext_xn_over_d(ci.wd, 1000000 + ex_glyph(p), 1000000);
    switch (pdf->posstruct->dir) {
    case dir_TLT:
        break;
    case dir_TRT:
        pos_left(ci.wd);
        break;
    case dir_LTL:
        pos_down(ci.ht);
        pos_left(ci.wd);
        break;
    case dir_RTT:
        pos_down(ci.ht);
        pos_left(ci.wd / 2);
        break;
    default:
        assert(0);
    }
    if (has_packet(f, c)) {
        do_vf_packet(pdf, f, c, ex_glyph);
    } else
        backend_out[glyph_node] (pdf, f, c, ex_glyph);  /* |pdf_place_glyph(pdf, f, c, ex_glyph);| */
    return ci;
}

@ Mark |f| as a used font; set |font_used(f)|, |font_size(f)| and |pdf_font_num(f)|
@c
static void pdf_use_font(internal_font_number f, int fontnum)
{
    set_font_used(f, true);
    assert((fontnum > 0) || ((fontnum < 0) && (pdf_font_num(-fontnum) > 0)));
    set_pdf_font_num(f, fontnum);
}

@ To set PDF font we need to find out fonts with the same name, because \TeX\
can load the same font several times for various sizes. For such fonts we
define only one font resource. The array |pdf_font_num| holds the object
number of font resource. A negative value of an entry of |pdf_font_num|
indicates that the corresponding font shares the font resource with the font

@c
#define same(n,f,k) (n(f) != NULL && n(k) != NULL && strcmp(n(f), n(k)) == 0)

static boolean font_shareable(internal_font_number f, internal_font_number k)
{
    int ret = 0;
    /* For some lua-loaded (for instance AFM) fonts, it is normal to have
       a zero cidregistry,  and such fonts do not have a fontmap entry yet
       at this point, so the test should use the other branch  */
    if (font_cidregistry(f) == NULL && font_cidregistry(k) == NULL &&
        font_encodingbytes(f) != 2 && font_encodingbytes(k) != 2) {
        if (font_map(k) != NULL &&
            font_map(f) != NULL && (same(font_name, k, f))) {
            ret = 1;
        }
    } else {
        if ((same(font_filename, k, f) && same(font_fullname, k, f))) {
            ret = 1;
        }
#ifdef DEBUG
        printf("\nfont_shareable(%d:%s:%s,%d:%s:%s): => %d\n",
               f, font_filename(f), font_fullname(f),
               k, font_filename(k), font_fullname(k), ret);
#endif
    }
    return ret;
}

@ create a font object
@c
void pdf_init_font(PDF pdf, internal_font_number f)
{
    internal_font_number k/*, b*/;
    fm_entry *fm;
    int i, l;
    assert(!font_used(f));

    ///* if |f| is auto expanded then ensure the base font is initialized */
    //
    //    if (font_auto_expand(f) && ((b = pdf_font_blink(f)) != null_font)) {
    //    if (!font_used(b))
    //        pdf_init_font(pdf, b);
    //    set_font_map(f, font_map(b));
    //    /* propagate slant and extend from unexpanded base font */
    //    set_font_slant(f, font_slant(b));
    //    set_font_extend(f, font_extend(b));
    //}
    /* check whether |f| can share the font object with some |k|: we have 2 cases
       here: 1) |f| and |k| have the same tfm name (so they have been loaded at
       different sizes, eg 'cmr10' and 'cmr10 at 11pt'); 2) |f| has been auto
       expanded from |k|
     */

    /* take over slant and extend from map entry, if not already set;
       this should also be the only place where getfontmap() may be called. */

    fm = getfontmap(font_name(f));
    if (font_map(f) == NULL && fm != NULL) {
        font_map(f) = fm;
        if (is_slantset(fm))
            font_slant(f) = fm->slant;
        if (is_extendset(fm))
            font_extend(f) = fm->extend;
    }
    i = pdf->head_tab[obj_type_font];
    while (i != 0) {
        k = obj_info(pdf, i);
        if (font_shareable(f, k)) {
            assert(pdf_font_num(k) != 0);
            if (pdf_font_num(k) < 0)
                pdf_use_font(f, pdf_font_num(k));
            else
                pdf_use_font(f, -k);
            return;
        }
        i = obj_link(pdf, i);
    }
    /* create a new font object for |f| */
    l = pdf_create_obj(pdf, obj_type_font, f);
    pdf_use_font(f, l);
}

@ set the actual font on PDF page
@c
internal_font_number pdf_set_font(PDF pdf, internal_font_number f)
{
    int ff;                     /* for use with |set_ff| */
    if (!font_used(f))
        pdf_init_font(pdf, f);
    /*
       set |ff| to the tfm number of the base font sharing the font object with |f|;
       |ff| is either |f| itself (then it is its own base font),
       or some font with the same tfm name at different size and/or expansion.
     */
    ff = pdf_font_num(f) < 0 ? -pdf_font_num(f) : f;    /* aka |set_ff(f)| */
    assert(pdf_font_num(ff) > 0);       /* base font object number */
    addto_page_resources(pdf, obj_type_font, pdf_font_num(ff));
    return ff;
}

@ @c
void pdf_include_chars(PDF pdf)
{
    str_number s;
    unsigned char *k, *j;       /* running index */
    internal_font_number f;
    scan_font_ident();
    f = cur_val;
    if (f == null_font)
        normal_error("pdf backend", "invalid font identifier for 'includechars'");
    pdf_check_vf(cur_val);
    if (!font_used(f))
        pdf_init_font(pdf, f);
    scan_pdf_ext_toks();
    s = tokens_to_string(def_ref);
    delete_token_ref(def_ref);
    j = str_string(s) + str_length(s);
    for (k = str_string(s); k < j; k++) {
        pdf_mark_char(f, *k);
    }
    flush_str(s);
}