Debian Codegraph @

texk/web2c/luatexdir/pdf/pdffont.c

raw
1 /*
2
3 Copyright 2009-2014 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
24 /*tex
25
26 As \LUATEX\ should also act as a back-end driver, it needs to support virtual
27 fonts too. Information about virtual fonts can be found in the source of some
28 \DVI-related programs.
29
30 Whenever we want to write out a character in a font to \PDF\ output, we
31 should check whether the used character is a virtual or real character. The
32 |has_packet| C macro checks for this condition.
33
34 The following code typesets a character to \PDF\ output
35
36 */
37
38 scaled_whd output_one_char(PDF pdf, halfword p)
39 {
40 internal_font_number f = font(p);
41 int c = character(p);
42 int ex_glyph = ex_glyph(p)/1000;
43 /*tex The real width, height and depth of the character: */
44 scaled_whd ci = get_charinfo_whd(f, c);
45 if (!(char_exists(f,c))) {
46 lua_glyph_not_found_callback(f,c);
47 /*tex Not here |char_warning(f,c);| */
48 return ci;
49 }
50 ci.wd = ext_xn_over_d(ci.wd, 1000000 + ex_glyph(p), 1000000);
51 switch (pdf->posstruct->dir) {
52 case dir_TLT:
53 break;
54 case dir_TRT:
55 pos_left(ci.wd);
56 break;
57 case dir_LTL:
58 pos_down(ci.ht);
59 pos_left(ci.wd);
60 break;
61 case dir_RTT:
62 pos_down(ci.ht);
63 pos_left(ci.wd / 2);
64 break;
65 default:
66 formatted_warning("pdf backend","ignoring bad dir %i when outputting a character",pdf->posstruct->dir);
67 }
68 if (has_packet(f, c)) {
69 do_vf_packet(pdf, f, c, ex_glyph);
70 } else {
71 backend_out_node_glyph[BACKEND_INDEX(glyph_node)] (pdf, f, c, ex_glyph);
72 }
73 return ci;
74 }
75
76 /*tex
77
78 Mark |f| as a used font; set |font_used(f)|, |font_size(f)| and
79 |pdf_font_num(f)|.
80
81 */
82
83 static void pdf_use_font(internal_font_number f, int fontnum)
84 {
85 set_font_used(f, true);
86 if ((fontnum > 0) || ((fontnum < 0) && (pdf_font_num(-fontnum) > 0))) {
87 set_pdf_font_num(f, fontnum);
88 } else {
89 normal_error("pdf backend","bad font id");
90 }
91 }
92
93 /*tex
94
95 To set PDF font we need to find out fonts with the same name, because \TeX\
96 can load the same font several times for various sizes. For such fonts we
97 define only one font resource. The array |pdf_font_num| holds the object
98 number of font resource. A negative value of an entry of |pdf_font_num|
99 indicates that the corresponding font shares the font resource with the font.
100
101 */
102
103 #define same(n,f,k) (n(f) != NULL && n(k) != NULL && strcmp(n(f), n(k)) == 0)
104
105 /*tex
106
107 For some \LUA-loaded (for instance \AFM) fonts, it is normal to have a zero
108 cidregistry, and such fonts do not have a fontmap entry yet at this point, so
109 the test should use the other branch.
110
111 */
112
113 static boolean font_shareable(internal_font_number f, internal_font_number k)
114 {
115 if (font_cidregistry(f) == NULL && font_cidregistry(k) == NULL && font_encodingbytes(f) != 2 && font_encodingbytes(k) != 2) {
116 if (font_map(k) != NULL && font_map(f) != NULL && (same(font_name, k, f))) {
117 return 1;
118 }
119 } else if ((same(font_filename, k, f) && same(font_fullname, k, f))) {
120 return 1;
121 }
122 return 0;
123 }
124
125 /*tex
126
127 We will create a font object. We check whether |f| can share the font object
128 with some |k|: we have 2 cases here: |f| and |k| have the same tfm name, so
129 they have been loaded at different sizes, eg 'cmr10' and 'cmr10 at 11pt'.
130
131 We optionally take over slant and extend from map entry, if not already set;
132 this should also be the only place where getfontmap() may be called.
133
134 Beware, \LUATEX\ is different from \PDFTEX\ in dealing with expanded and
135 slanted fonts and expansion and protrusion as it will use the same font but a
136 different transformation which is way more efficient and also cleaner.
137
138 */
139
140 void pdf_init_font(PDF pdf, internal_font_number f)
141 {
142 internal_font_number k;
143 fm_entry *fm;
144 int i, l;
145 if (font_used(f)) {
146 formatted_error("pdf backend","font %i gets initialized twice",(int) f);
147 }
148 fm = getfontmap(font_name(f));
149 if (font_map(f) == NULL && fm != NULL) {
150 font_map(f) = fm;
151 if (is_slantset(fm))
152 font_slant(f) = fm->slant;
153 if (is_extendset(fm))
154 font_extend(f) = fm->extend;
155 }
156 i = pdf->head_tab[obj_type_font];
157 while (i != 0) {
158 k = obj_info(pdf, i);
159 if (font_shareable(f, k)) {
160 if (pdf_font_num(k) < 0)
161 pdf_use_font(f, pdf_font_num(k));
162 else
163 pdf_use_font(f, -k);
164 return;
165 }
166 i = obj_link(pdf, i);
167 }
168 /*tex Create a new font object for |f|: */
169 l = pdf_create_obj(pdf, obj_type_font, f);
170 pdf_use_font(f, l);
171 }
172
173 /*tex
174
175 Set the actual font on PDF page; sets |ff| to the tfm number of the base font
176 sharing the font object with |f|; |ff| is either |f| itself (then it is its
177 own base font), or some font with the same tfm name at different size and/or
178 expansion.
179
180 */
181
182 internal_font_number pdf_set_font(PDF pdf, internal_font_number f)
183 {
184 /*tex For use with |set_ff|: */
185 int ff;
186 if (!font_used(f))
187 pdf_init_font(pdf, f);
188 /*tex Also known as |set_ff(f)|: */
189 ff = pdf_font_num(f) < 0 ? -pdf_font_num(f) : f;
190 addto_page_resources(pdf, obj_type_font, pdf_font_num(ff));
191 return ff;
192 }
193
194 void pdf_include_chars(PDF pdf)
195 {
196 str_number s;
197 unsigned char *k, *j;
198 internal_font_number f;
199 scan_font_ident();
200 f = cur_val;
201 if (f == null_font)
202 normal_error("pdf backend", "invalid font identifier for 'includechars'");
203 /* pdf_check_vf(c); */
204 if (!font_used(f))
205 pdf_init_font(pdf, f);
206 scan_toks(false, true);
207 s = tokens_to_string(def_ref);
208 delete_token_ref(def_ref);
209 j = str_string(s) + str_length(s);
210 for (k = str_string(s); k < j; k++) {
211 pdf_mark_char(f, *k);
212 }
213 flush_str(s);
214 }