Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfglyph.c

raw
1 /*
2
3 Copyright 2009-2011 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 "pdf/pdfpage.h"
24
25 /*tex
26
27 The |e_tj| step must be 3 because movements in |[]TJ| are in fontsize/$10^3$
28 units.
29
30 */
31
32 #define e_tj 3
33
34 /*tex
35
36 Use exactly this formula also for calculating the width array values. The units patch below is
37 meant for opentype cff fonts that don't use the recommended 1000 units per em and was tested
38 for a few fonts by HH and MS (2023-11-01). We explicitly check for the deviation from 1000
39 in order to be compatible and only touch the few fonts that are sensitive for this. The patch
40 makes sure that copying works (in spite of the accessibility hype viewers still have to gamble
41 on what a space is; so much for standards) and also has the side effect of a bit more compact
42 character streams. It's one of these cases where one wonders if it was really worth spending
43 that much time on (because after all, it worked ok in luametatex).
44
45 */
46
47 static int64_t pdf_char_width(pdfstructure * p, internal_font_number f, int i)
48 {
49 if (font_encodingbytes(f) == 2 && font_format(f) == opentype_format && font_units_per_em(f) != 1000) {
50 /* So we only end up here for the very few fonts that deviate from 1000. */
51 return i64round(((double) font_units_per_em(f) / 1000.0) * (double) char_width(f, i) / font_size(f) * ten_pow[e_tj + p->cw.e]);
52 } else {
53 return i64round((double) char_width(f, i) / font_size(f) * ten_pow[e_tj + p->cw.e]);
54 }
55 }
56
57 void pdf_print_cid_charwidth(PDF pdf, int f, int w)
58 {
59 if (font_encodingbytes(f) == 2 && font_format(f) == opentype_format && font_units_per_em(f) != 1000) {
60 /* So we only end up here for the very few fonts that deviate from 1000. */
61 pdf_printf(pdf, "%0.1f",(double) w * font_units_per_em(f) / 10000.0);
62 } else {
63 if (w < 0) {
64 pdf_out(pdf, '-');
65 w = -w;
66 }
67 pdf_printf(pdf, "%i", (w / 10));
68 if ((w % 10) != 0) {
69 pdf_printf(pdf, ".%i", (w % 10));
70 }
71 }
72 }
73
74 void pdf_print_charwidth(PDF pdf, internal_font_number f, int i)
75 {
76 pdffloat cw;
77 pdfstructure *p = pdf->pstruct;
78 cw.m = pdf_char_width(p, f, i);
79 cw.e = p->cw.e;
80 print_pdffloat(pdf, cw);
81 }
82
83 static void setup_fontparameters(PDF pdf, internal_font_number f, int ex_glyph)
84 {
85 float slant, extend, squeeze, expand, scale = 1.0;
86 float u = 1.0;
87 pdfstructure *p = pdf->pstruct;
88 if ((font_format(f) == opentype_format || (font_format(f) == type1_format && font_encodingbytes(f) == 2)) && font_units_per_em(f) > 0) {
89 u = font_units_per_em(f) / 1000.0;
90 }
91 pdf->f_cur = f;
92 p->f_pdf = pdf_set_font(pdf, f);
93 p->fs.m = i64round(font_size(f) / u / by_one_bp * ten_pow[p->fs.e]);
94 slant = font_slant(f) / 1000.0;
95 extend = font_extend(f) / 1000.0;
96 squeeze = font_squeeze(f) / 1000.0;
97 expand = 1.0 + (ex_glyph/1) / 1000.0;
98 /*tex The |-1| makes less corrections inside |[]TJ|: */
99 p->tj_delta.e = p->cw.e - 1;
100 /*tex There is no need to be more precise than \TEX\ (1sp). */
101 while (p->tj_delta.e > 0 && (double) font_size(f) / ten_pow[p->tj_delta.e + e_tj] < 0.5) {
102 /*tex This happens for very tiny fonts. */
103 p->tj_delta.e--;
104 }
105 p->tm[0].m = i64round(scale * expand * extend * ten_pow[p->tm[0].e]);
106 p->tm[2].m = i64round(slant * ten_pow[p->tm[2].e]);
107 p->tm[3].m = i64round(scale * squeeze * ten_pow[p->tm[3].e]);
108 p->k2 = ten_pow[e_tj + p->cw.e] * scale / (ten_pow[p->pdf.h.e] * pdf2double(p->fs) * pdf2double(p->tm[0]));
109 /*tex We keep track of the state of ex. */
110 p->cur_ex = ex_glyph ;
111 p->need_width = font_width(f);
112 p->need_mode = font_mode(f);
113 }
114
115 static void set_font(PDF pdf)
116 {
117 pdfstructure *p = pdf->pstruct;
118
119 if (p->need_width != 0) {
120 pdf_printf(pdf, "%0.3f w\n",((7227.0/7200.0)/1000.0) * p->need_width );
121 p->done_width = 1;
122 } else if (p->done_width) {
123 pdf_puts(pdf, "0 w\n");
124 p->done_width = 0;
125 }
126 if (p->need_mode != 0) {
127 pdf_printf(pdf, "%d Tr\n", (int) p->need_mode);
128 p->done_mode = 1;
129 } else if (p->done_mode) {
130 pdf_puts(pdf, "0 Tr\n");
131 p->done_mode = 0;
132 }
133 pdf_printf(pdf, "/F%d", (int) p->f_pdf);
134 pdf_print_resname_prefix(pdf);
135 pdf_out(pdf, ' ');
136 print_pdffloat(pdf, p->fs);
137 pdf_puts(pdf, " Tf\n");
138 p->f_pdf_cur = p->f_pdf;
139 p->fs_cur.m = p->fs.m;
140 p->need_tf = false;
141 /*tex Always follow |Tf| by |Tm|: */
142 p->need_tm = true;
143 }
144
145 static void set_textmatrix(PDF pdf, scaledpos pos)
146 {
147 boolean move;
148 pdfstructure *p = pdf->pstruct;
149 if (!is_textmode(p))
150 normal_error("pdf backend","text mode expected in set_textmatrix");
151 move = calc_pdfpos(p, pos);
152 if (p->need_tm || move) {
153 print_pdf_matrix(pdf, p->tm);
154 pdf_puts(pdf, " Tm ");
155 /*tex |Tm| replaces */
156 p->pdf.h.m = p->pdf_bt_pos.h.m + p->tm[4].m;
157 p->pdf.v.m = p->pdf_bt_pos.v.m + p->tm[5].m;
158 p->need_tm = false;
159 }
160 p->tm0_cur.m = p->tm[0].m;
161 }
162
163 /*tex
164
165 Print out a character to PDF buffer; the character will be printed in octal
166 form in the following cases: chars <= 32, backslash (92), left parenthesis
167 (40), and right parenthesis (41).
168
169 */
170
171 static void pdf_print_char(PDF pdf, int c)
172 {
173 if (c > 255) {
174 return;
175 } else if (c <= 32 || c == '\\' || c == '(' || c == ')' || c > 127) {
176 pdf_room(pdf, 4);
177 pdf_quick_out(pdf, '\\');
178 pdf_quick_out(pdf, (unsigned char) ('0' + ((c >> 6) & 0x3)));
179 pdf_quick_out(pdf, (unsigned char) ('0' + ((c >> 3) & 0x7)));
180 pdf_quick_out(pdf, (unsigned char) ('0' + (c & 0x7)));
181 } else
182 pdf_out(pdf, c);
183 }
184
185 static void pdf_print_wide_char(PDF pdf, int c)
186 {
187 char hex[5];
188 snprintf(hex, 5, "%04X", c);
189 pdf_out_block(pdf, (const char *) hex, 4);
190 }
191
192 static void begin_charmode(PDF pdf, internal_font_number f, pdfstructure * p)
193 {
194 if (!is_chararraymode(p))
195 normal_error("pdf backend","char array mode expected in begin_char_mode");
196 if (font_encodingbytes(f) == 2) {
197 p->ishex = 1;
198 pdf_out(pdf, '<');
199 } else {
200 p->ishex = 0;
201 pdf_out(pdf, '(');
202 }
203 p->mode = PMODE_CHAR;
204 }
205
206 void end_charmode(PDF pdf)
207 {
208 pdfstructure *p = pdf->pstruct;
209 if (!is_charmode(p))
210 normal_error("pdf backend","char mode expected in end_char_mode");
211 if (p->ishex == 1) {
212 p->ishex = 0;
213 pdf_out(pdf, '>');
214 } else {
215 pdf_out(pdf, ')');
216 }
217 p->mode = PMODE_CHARARRAY;
218 }
219
220 static void begin_chararray(PDF pdf)
221 {
222 pdfstructure *p = pdf->pstruct;
223 if (!is_textmode(p))
224 normal_error("pdf backend","text mode expected in begin_char_array");
225 p->pdf_tj_pos = p->pdf;
226 p->cw.m = 0;
227 pdf_out(pdf, '[');
228 p->mode = PMODE_CHARARRAY;
229 }
230
231 void end_chararray(PDF pdf)
232 {
233 pdfstructure *p = pdf->pstruct;
234 if (!is_chararraymode(p))
235 normal_error("pdf backend","char array mode expected in end_char_array");
236 pdf_puts(pdf, "]TJ\n");
237 p->pdf = p->pdf_tj_pos;
238 p->mode = PMODE_TEXT;
239 }
240
241 /*tex
242
243 We need to adapt the tm when a font changes. A change can be a change in id
244 (frontend) or pdf reference (backend, as we share font resources). At such a
245 change we also need to adapt to the slant and extend. Initially we also need
246 to take the exfactor of a glyph into account. When the font is unchanged, we
247 still need to check each glyph for a change in exfactor. We store the current
248 one on the state record so that we can minimize testing.
249
250 */
251
252 void pdf_place_glyph(PDF pdf, internal_font_number f, int c, int ex)
253 {
254 boolean move;
255 pdfstructure *p = pdf->pstruct;
256 scaledpos pos = pdf->posstruct->pos;
257 /*tex
258
259 This is already done:
260
261 \startyping
262 if (!char_exists(f, c)) {
263 return;
264 }
265 \stoptyping
266
267 */
268 if (font_writingmode(f) == vertical_writingmode) {
269 if (p->wmode != WMODE_V) {
270 p->wmode = WMODE_V;
271 p->need_tm = true;
272 }
273 } else {
274 if (p->wmode != WMODE_H) {
275 p->wmode = WMODE_H;
276 p->need_tm = true;
277 }
278 }
279 if (p->need_tf || f != pdf->f_cur || p->f_pdf != p->f_pdf_cur || p->fs.m != p->fs_cur.m || is_pagemode(p)) {
280 pdf_goto_textmode(pdf);
281 setup_fontparameters(pdf, f, ex);
282 set_font(pdf);
283 } else if (p->tm0_cur.m != p->tm[0].m || p->cur_ex != ex) {
284 setup_fontparameters(pdf, f, ex);
285 p->need_tm = true;
286 }
287 /*tex All movements within text or chararray or char mode: */
288 move = calc_pdfpos(p, pos);
289 if (move || p->need_tm) {
290 if (p->need_tm
291 || (p->wmode == WMODE_H && (p->pdf_bt_pos.v.m + p->tm[5].m) != p->pdf.v.m)
292 || (p->wmode == WMODE_V && (p->pdf_bt_pos.h.m + p->tm[4].m) != p->pdf.h.m)
293 || abs(p->tj_delta.m) >= 1000000) {
294 pdf_goto_textmode(pdf);
295 set_textmatrix(pdf, pos);
296 begin_chararray(pdf);
297 /*tex For fine adjustment: */
298 move = calc_pdfpos(p, pos);
299 }
300 if (move) {
301 if (is_charmode(p))
302 end_charmode(pdf);
303 print_pdffloat(pdf, p->tj_delta);
304 p->cw.m -= p->tj_delta.m * ten_pow[p->cw.e - p->tj_delta.e];
305 }
306 }
307 /*tex Glyph output: */
308 if (is_chararraymode(p))
309 begin_charmode(pdf, f, p);
310 else if (!is_charmode(p))
311 normal_error("pdf backend","char (array) mode expected in place_glyph");
312 pdf_mark_char(f, c);
313 if (font_encodingbytes(f) == 2)
314 pdf_print_wide_char(pdf, char_index(f, c));
315 else
316 pdf_print_char(pdf, c);
317 /*tex Also known as |adv_char_width()|: */
318 p->cw.m += pdf_char_width(p, p->f_pdf, c);
319 }