Debian Codegraph @

texk/web2c/luatexdir/font/writet3.c

raw
1 /*
2
3 Copyright 1996-2006 Han The Thanh <[email protected]>
4 Copyright 2006-2011 Taco Hoekwater <[email protected]>
5
6 This file is part of LuaTeX.
7
8 LuaTeX is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 #include "ptexlib.h"
24 #include <kpathsea/tex-glyph.h>
25 #include <kpathsea/magstep.h>
26 #include <string.h>
27
28 #define T3_BUF_SIZE 1024
29
30 typedef char t3_line_entry;
31 define_array(t3_line);
32
33 FILE *t3_file;
34
35 static int t3_char_procs[256];
36 static double t3_char_widths[256];
37 static int t3_glyph_num;
38 static double t3_font_scale;
39 static int t3_b0, t3_b1, t3_b2, t3_b3;
40
41 /*tex Not static because used elsewhere. */
42
43 unsigned char *t3_buffer = NULL;
44 int t3_size = 0;
45 int t3_curbyte = 0;
46
47 #define t3_check_eof() \
48 if (t3_eof()) \
49 normal_error("type 3","unexpected end of file");
50
51 static void update_bbox(int llx, int lly, int urx, int ury, boolean is_first_glyph)
52 {
53 if (is_first_glyph) {
54 t3_b0 = llx;
55 t3_b1 = lly;
56 t3_b2 = urx;
57 t3_b3 = ury;
58 } else {
59 if (llx < t3_b0)
60 t3_b0 = llx;
61 if (lly < t3_b1)
62 t3_b1 = lly;
63 if (urx > t3_b2)
64 t3_b2 = urx;
65 if (ury > t3_b3)
66 t3_b3 = ury;
67 }
68 }
69
70 /*tex Fixed precision 3 (+5 in |pdfgen.c|)*/
71
72 #define get_pk_font_scale(pdf,f,scale_factor) \
73 divide_scaled(scale_factor, divide_scaled(font_size(f),one_hundred_bp,pk_decimal_digits(pdf,2)), 0)
74
75 #define pk_char_width(pdf,f,w,scale_factor) \
76 divide_scaled(divide_scaled(w,font_size(f),pk_decimal_digits(pdf,4)), get_pk_font_scale(pdf,f,scale_factor), 0)
77
78 static boolean writepk(PDF pdf, internal_font_number f)
79 {
80 kpse_glyph_file_type font_ret;
81 int llx, lly, urx, ury;
82 int cw, rw, i, j;
83 pdffloat pf;
84 halfword *row;
85 char *name;
86 chardesc cd;
87 boolean is_null_glyph, check_preamble;
88 int dpi = 0;
89 int newdpi = 0;
90 int callback_id = 0;
91 int file_opened = 0;
92 xfree(t3_buffer);
93 t3_curbyte = 0;
94 t3_size = 0;
95 callback_id = callback_defined(find_pk_file_callback);
96 if (pdf->pk_fixed_dpi) {
97 newdpi = pdf->pk_resolution;
98 } else {
99 newdpi = dpi;
100 }
101 if (callback_id > 0) {
102 /*tex |<base>.dpi/<fontname>.<tdpi>pk| */
103 dpi = round((float) pdf->pk_resolution * (((float) font_size(f)) / (float) font_dsize(f)));
104 cur_file_name = font_name(f);
105 run_callback(callback_id, "Sd->S", cur_file_name, (int) newdpi, &name);
106 if (name == NULL || strlen(name) == 0) {
107 formatted_warning("type 3","font %s at %i not found", cur_file_name, (int) dpi);
108 return false;
109 }
110 } else {
111 dpi = (int) kpse_magstep_fix(
112 (unsigned) round ((float)pdf->pk_resolution * ((float)font_size(f)/(float)font_dsize(f))),
113 (unsigned) pdf->pk_resolution, NULL
114 );
115 cur_file_name = font_name(f);
116 name = kpse_find_pk(cur_file_name, (unsigned) dpi, &font_ret);
117 if (name == NULL || !FILESTRCASEEQ(cur_file_name, font_ret.name)
118 || !kpse_bitmap_tolerance((float) font_ret.dpi, (float) dpi)) {
119 formatted_error("type 3","font %s at %i not found", cur_file_name, (int) dpi);
120 return false;
121 }
122 }
123 callback_id = callback_defined(read_pk_file_callback);
124 if (callback_id > 0) {
125 if (!(run_callback(callback_id, "S->bSd", name, &file_opened, &t3_buffer, &t3_size) && file_opened && t3_size > 0)) {
126 formatted_warning("type 3","font %s at %i not found", cur_file_name, (int) dpi);
127 cur_file_name = NULL;
128 return false;
129 }
130 } else {
131 t3_file = xfopen(name, FOPEN_RBIN_MODE);
132 recorder_record_input(name);
133 t3_read_file();
134 t3_close();
135 }
136 report_start_file(filetype_font,(char *) name);
137 cd.rastersize = 256;
138 cd.raster = xtalloc((unsigned long) cd.rastersize, halfword);
139 check_preamble = true;
140 if (dpi==0) {
141 normal_error("type 3","invalid dpi value 0");
142 } else if (newdpi==0) {
143 newdpi = dpi;
144 }
145 while (readchar(check_preamble, &cd) != 0) {
146 check_preamble = false;
147 if (!pdf_char_marked(f, cd.charcode))
148 continue;
149 t3_char_widths[cd.charcode] = (double) pk_char_width(pdf, f, get_charwidth(f, cd.charcode), pdf->pk_scale_factor);
150 if (cd.cwidth < 1 || cd.cheight < 1) {
151 cd.xescape = cd.cwidth = round(t3_char_widths[cd.charcode] / 100.0);
152 cd.cheight = 1;
153 cd.xoff = 0;
154 cd.yoff = 0;
155 is_null_glyph = true;
156 } else
157 is_null_glyph = false;
158 llx = -cd.xoff;
159 lly = cd.yoff - cd.cheight + 1;
160 urx = cd.cwidth + llx + 1;
161 ury = cd.cheight + lly;
162 update_bbox(llx, lly, urx, ury, t3_glyph_num == 0);
163 t3_glyph_num++;
164 t3_char_procs[cd.charcode] = pdf_create_obj(pdf, obj_type_others, 0);
165 pdf_begin_obj(pdf, t3_char_procs[cd.charcode], OBJSTM_NEVER);
166 pdf_begin_dict(pdf);
167 pdf_dict_add_streaminfo(pdf);
168 pdf_end_dict(pdf);
169 pdf_begin_stream(pdf);
170 setpdffloat(pf, (int64_t) t3_char_widths[cd.charcode], 2);
171 print_pdffloat(pdf, pf);
172 pdf_printf(pdf, " 0 %i %i %i %i d1\n", (int) llx, (int) lly, (int) urx, (int) ury);
173 if (is_null_glyph)
174 goto end_stream;
175 pdf_printf(pdf, "q\n%i 0 0 %i %i %i cm\nBI\n",
176 (int) (dpi * cd.cwidth / newdpi),
177 (int) (dpi * cd.cheight / newdpi),
178 (int) llx, (int) lly);
179 pdf_printf(pdf, "/W %i\n/H %i\n", (int) cd.cwidth, (int) cd.cheight);
180 pdf_puts(pdf, "/IM true\n/BPC 1\n/D [1 0]\nID ");
181 cw = (cd.cwidth + 7) / 8;
182 rw = (cd.cwidth + 15) / 16;
183 row = cd.raster;
184 for (i = 0; i < cd.cheight; i++) {
185 for (j = 0; j < rw - 1; j++) {
186 pdf_out(pdf, (unsigned char) (*row / 256));
187 pdf_out(pdf, (unsigned char) (*row % 256));
188 row++;
189 }
190 pdf_out(pdf, (unsigned char) (*row / 256));
191 if (2 * rw == cw)
192 pdf_out(pdf, (unsigned char) (*row % 256));
193 row++;
194 }
195 pdf_puts(pdf, "\nEI\nQ\n");
196 end_stream:
197 pdf_end_stream(pdf);
198 pdf_end_obj(pdf);
199 }
200 xfree(cd.raster);
201 cur_file_name = NULL;
202 return true;
203 }
204
205 /*tex
206
207 Mode |0| is traditional pk with map file lookup, while mode |1| listens to
208 the callback. The callback gets a font id and character number and returns an
209 object number of a stream object plus the width that ends up in the widths
210 array. The stream can of course refer to another xform but it has to start
211 with the right sequence for being a character. As there are different
212 situations this is more flexible that hardcoding all kind of variants that
213 will never suit all possible purposes.
214
215 The next ones are a bit strange. We could as well have had one callback that
216 just generates the whole type 3 but I think that the assumption is that
217 \LUATEX\ should do the work. So, instead we have one callback that is invoked
218 with several intentions as indicated by the first argument.
219
220 */
221
222 static void writet3(PDF pdf, internal_font_number f, int callback_id)
223 {
224 int i;
225 char s[32];
226 int wptr, eptr, cptr;
227 int first_char, last_char;
228 int pk_font_scale;
229 int tounicode_objnum = 0;
230 int bc = font_bc(f);
231 int ec = font_ec(f);
232 if (bc > 255) {
233 bc = 255;
234 }
235 if (ec > 255) {
236 formatted_warning("font", "discarding characters above 255 in type 3 font '%s' in write", font_name(f));
237 ec = 255;
238 }
239 pdffloat pf;
240 boolean is_notdef;
241 t3_glyph_num = 0;
242 for (i = 0; i < 256; i++) {
243 t3_char_procs[i] = 0;
244 t3_char_widths[i] = 0;
245 }
246 xfree(t3_buffer);
247 t3_curbyte = 0;
248 t3_size = 0;
249 for (i = bc; i <= ec; i++) {
250 if (pdf_char_marked(f, i)) {
251 break;
252 }
253 }
254 first_char = i;
255 for (i = ec; i > first_char; i--) {
256 if (pdf_char_marked(f, i)) {
257 break;
258 }
259 }
260 last_char = i;
261 if (callback_id > 0) {
262 /*tex We assume a user font. */
263 for (i = first_char; i <= last_char; i++) {
264 if (pdf_char_marked(f, i)) {
265 int r = 0;
266 double w = 0;
267 /*tex
268 We pass |2|, the font id, the character index, and we expect
269 back a width in basepoints, an object number of a stream, and
270 an objectnumber of an xobject used where zero means nothing is
271 used.
272 */
273 run_callback(callback_id, "ddd->df", 2, f, i, &r, &w);
274 if (r > 0) {
275 t3_char_procs[i] = r;
276 t3_char_widths[i] = w;
277 }
278 }
279 }
280 t3_font_scale = 0.001;
281 t3_b0 = 0;
282 t3_b1 = 0;
283 t3_b2 = 0;
284 t3_b3 = 0;
285 } else if (writepk(pdf, f)) {
286 /*tex We assume a pk font. */
287 } else {
288 /*tex We just quit. */
289 return;
290 }
291 if (pdf->gen_tounicode > 0) {
292 tounicode_objnum = write_raw_tounicode(pdf, f, font_fullname(f));
293 }
294 /*tex We create a |Type3| font dictionary: */
295 pdf_begin_obj(pdf, pdf_font_num(f), OBJSTM_ALWAYS);
296 pdf_begin_dict(pdf);
297 pdf_dict_add_name(pdf, "Type", "Font");
298 pdf_dict_add_name(pdf, "Subtype", "Type3");
299 snprintf(s, 31, "F%i", (int) f);
300 pdf_dict_add_name(pdf, "Name", s);
301 if (pdf_font_attr(f) != get_nullstr() && pdf_font_attr(f) != 0) {
302 pdf_out(pdf, '\n');
303 pdf_print(pdf, pdf_font_attr(f));
304 pdf_out(pdf, '\n');
305 }
306 pdf_add_name(pdf, font_key[FONTBBOX1_CODE].pdfname);
307 pdf_begin_array(pdf);
308 pdf_add_int(pdf, (int) t3_b0);
309 pdf_add_int(pdf, (int) t3_b1);
310 pdf_add_int(pdf, (int) t3_b2);
311 pdf_add_int(pdf, (int) t3_b3);
312 pdf_end_array(pdf);
313 pdf_add_name(pdf, "Resources");
314 /*tex
315 This is not mandate but we just set them all. They are ignored anyway. In
316 PDF version 2 they are even obsolete but then, unknown entries are again
317 ignored.
318 */
319 pdf_begin_dict(pdf);
320 pdf_add_name(pdf, "ProcSet");
321 pdf_begin_array(pdf);
322 pdf_add_name(pdf, "PDF");
323 pdf_add_name(pdf, "ImageB");
324 if (callback_id) {
325 pdf_add_name(pdf, "ImageC");
326 pdf_add_name(pdf, "ImageI");
327 pdf_add_name(pdf, "Text");
328 }
329 pdf_end_array(pdf);
330 /*tex
331 A previous version had two calls that dealt with adding font resources and
332 xform references and a separate one for the font matrix but we now delegate
333 some stupid work to the caller and have all in one: we expect a matrix and
334 a resource string.
335 */
336 if (callback_id > 0) {
337 char *str = NULL;
338 double s = t3_font_scale;
339 run_callback(callback_id, "dd->fR", 3, f, &s, &str);
340 t3_font_scale = s;
341 if (str != NULL) {
342 pdf_out(pdf, ' ');
343 pdf_puts(pdf, str);
344 free(str);
345 }
346 pdf_end_dict(pdf); /*tex The resources. */
347 pdf_add_name(pdf, "FontMatrix");
348 pdf_begin_array(pdf);
349 pdf_printf(pdf, "%g 0 0 %g 0 0", t3_font_scale, t3_font_scale);
350 pdf_end_array(pdf);
351 } else {
352 pdf_end_dict(pdf); /*tex The resources. */
353 pk_font_scale = get_pk_font_scale(pdf,f,pdf->pk_scale_factor);
354 pdf_add_name(pdf, "FontMatrix");
355 pdf_begin_array(pdf);
356 setpdffloat(pf, (int64_t) pk_font_scale, 5);
357 print_pdffloat(pdf, pf);
358 pdf_puts(pdf, " 0 0 ");
359 print_pdffloat(pdf, pf);
360 pdf_puts(pdf, " 0 0");
361 pdf_end_array(pdf);
362 }
363 /*tex Some preparations. */
364 pdf_dict_add_int(pdf, "FirstChar", first_char);
365 pdf_dict_add_int(pdf, "LastChar", last_char);
366 wptr = pdf_create_obj(pdf, obj_type_others, 0);
367 eptr = pdf_create_obj(pdf, obj_type_others, 0);
368 cptr = pdf_create_obj(pdf, obj_type_others, 0);
369 pdf_dict_add_ref(pdf, "Widths", (int) wptr);
370 pdf_dict_add_ref(pdf, "Encoding", (int) eptr);
371 pdf_dict_add_ref(pdf, "CharProcs", (int) cptr);
372 if (tounicode_objnum) {
373 pdf_dict_add_ref(pdf, "ToUnicode", tounicode_objnum);
374 }
375 pdf_end_dict(pdf);
376 pdf_end_obj(pdf);
377 /*tex The |Widths| array: */
378 pdf_begin_obj(pdf, wptr, OBJSTM_ALWAYS);
379 pdf_begin_array(pdf);
380 if (callback_id) {
381 for (i = first_char; i <= last_char; i++) {
382 pdf_printf(pdf, " %g", t3_char_widths[i]);
383 }
384 } else {
385 for (i = first_char; i <= last_char; i++) {
386 setpdffloat(pf, (int64_t) t3_char_widths[i], 2);
387 print_pdffloat(pdf, pf);
388 pdf_out(pdf, ' ');
389 }
390 }
391 pdf_end_array(pdf);
392 pdf_end_obj(pdf);
393 /*tex The |Encoding| dictionary: */
394 pdf_begin_obj(pdf, eptr, OBJSTM_ALWAYS);
395 pdf_begin_dict(pdf);
396 pdf_dict_add_name(pdf, "Type", "Encoding");
397 pdf_add_name(pdf, "Differences");
398 pdf_begin_array(pdf);
399 pdf_add_int(pdf, first_char);
400 if (t3_char_procs[first_char] == 0) {
401 pdf_add_name(pdf, notdef);
402 is_notdef = true;
403 } else {
404 snprintf(s, 31, "a%i", first_char);
405 pdf_add_name(pdf, s);
406 is_notdef = false;
407 }
408 for (i = first_char + 1; i <= last_char; i++) {
409 if (t3_char_procs[i] == 0) {
410 if (!is_notdef) {
411 pdf_add_int(pdf, i);
412 pdf_add_name(pdf, notdef);
413 is_notdef = true;
414 }
415 } else {
416 if (is_notdef) {
417 pdf_add_int(pdf, i);
418 is_notdef = false;
419 }
420 snprintf(s, 31, "a%i", i);
421 pdf_add_name(pdf, s);
422 }
423 }
424 pdf_end_array(pdf);
425 pdf_end_dict(pdf);
426 pdf_end_obj(pdf);
427 /*tex The |CharProcs| dictionary: */
428 pdf_begin_obj(pdf, cptr, OBJSTM_ALWAYS);
429 pdf_begin_dict(pdf);
430 for (i = first_char; i <= last_char; i++) {
431 if (t3_char_procs[i] != 0) {
432 snprintf(s, 31, "a%i", (int) i);
433 pdf_dict_add_ref(pdf, s, (int) t3_char_procs[i]);
434 }
435 }
436 pdf_end_dict(pdf);
437 pdf_end_obj(pdf);
438 report_stop_file(filetype_font);
439 cur_file_name = NULL;
440 }
441
442 void writet3pk(PDF pdf, internal_font_number f)
443 {
444 writet3(pdf, f, 0);
445 }
446
447 void prerollt3user(PDF pdf, internal_font_number f)
448 {
449 int callback_id = callback_defined(provide_charproc_data_callback);
450 if (callback_id > 0) {
451 int bc = font_bc(f);
452 int ec = font_ec(f);
453 if (bc > 255) {
454 bc = 255;
455 }
456 if (ec > 255) {
457 formatted_warning("font", "discarding characters above 255 in type 3 font '%s' in preroll", font_name(f));
458 ec = 255;
459 }
460 int i;
461 for (i = bc; i <= ec; i++) {
462 if (pdf_char_marked(f, i)) {
463 /*tex We pass |true|, the font id and the character index. */
464 run_callback(callback_id, "ddd->", 1, f, i);
465 }
466 }
467 }
468 }
469
470 void writet3user(PDF pdf, internal_font_number f)
471 {
472 int callback_id = callback_defined(provide_charproc_data_callback);
473 if (callback_id) {
474 writet3(pdf, f, callback_id);
475 }
476 }