Debian Codegraph @

texk/web2c/luatexdir/lua/lfontlib.c

raw
1 /* lfontlib.c
2
3 Copyright 2006-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 #include "ptexlib.h"
22 #include "lua/luatex-api.h"
23
24 #define TIMERS 0
25
26 #if TIMERS
27 # include <sys/time.h>
28 #endif
29
30 static int get_fontid(void)
31 {
32 if (font_tables == NULL || font_tables[0] == NULL) {
33 create_null_font();
34 }
35 return new_font();
36 }
37
38 static int font_read_tfm(lua_State * L)
39 {
40 if (lua_type(L, 1) == LUA_TSTRING) {
41 const char *cnom = lua_tostring(L, 1);
42 if (lua_type(L, 2) == LUA_TNUMBER) {
43 scaled s = (int) lua_roundnumber(L, 2);
44 if (strlen(cnom)) {
45 internal_font_number f = get_fontid();
46 if (read_tfm_info(f, cnom, s)) {
47 int k = font_to_lua(L, f, 0);
48 delete_font(f);
49 return k;
50 } else {
51 delete_font(f);
52 luaL_error(L, "font loading failed");
53 }
54 } else {
55 luaL_error(L, "expected tfm name as first argument");
56 }
57 } else {
58 luaL_error(L, "expected an integer size as second argument");
59 }
60 } else {
61 luaL_error(L, "expected tfm name as first argument");
62 }
63 return 2; /* not reached */
64 }
65
66
67 static int font_read_vf(lua_State * L)
68 {
69 if (lua_type(L, 1) == LUA_TSTRING) {
70 const char *cnom = lua_tostring(L, 1);
71 if (strlen(cnom)) {
72 if (lua_type(L, 2) == LUA_TNUMBER) {
73 int i = lua_roundnumber(L, 2);
74 return make_vf_table(L, cnom, (scaled) i);
75 } else {
76 luaL_error(L, "expected an integer size as second argument");
77 return 2;
78 }
79 }
80 }
81 luaL_error(L, "expected vf name as first argument");
82 return 2; /* not reached */
83 }
84
85 static int tex_current_font(lua_State * L)
86 {
87 int i = luaL_optinteger(L, 1, 0);
88 if (i > 0) {
89 if (is_valid_font(i)) {
90 zset_cur_font(i);
91 return 0;
92 } else {
93 luaL_error(L, "expected a valid font id");
94 return 2; /* not reached */
95 }
96 } else {
97 lua_pushinteger(L, get_cur_font());
98 return 1;
99 }
100 }
101
102 static int tex_max_font(lua_State * L)
103 {
104 lua_pushinteger(L, max_font_id());
105 return 1;
106 }
107
108
109 static int tex_each_font_next(lua_State * L)
110 {
111 int m = lua_tointeger(L, 1);
112 int i = lua_tointeger(L, 2);
113 i++;
114 while (i <= m && !is_valid_font(i))
115 i++;
116 if (i > m) {
117 lua_pushnil(L);
118 return 1;
119 } else {
120 lua_pushinteger(L, i);
121 if (!font_to_lua(L, i, 1))
122 lua_pushnil(L);
123 return 2;
124 }
125 }
126
127 static int tex_each_font(lua_State * L)
128 {
129 lua_pushcclosure(L, tex_each_font_next, 0);
130 lua_pushinteger(L, max_font_id());
131 lua_pushinteger(L, 0);
132 return 3;
133 }
134
135 static int frozenfont(lua_State * L)
136 {
137 int i = luaL_checkinteger(L, 1);
138 if (i) {
139 if (is_valid_font(i)) {
140 if (font_touched(i) || font_used(i)) {
141 lua_pushboolean(L, 1);
142 } else {
143 lua_pushboolean(L, 0);
144 }
145 } else {
146 lua_pushnil(L);
147 }
148 return 1;
149 } else {
150 luaL_error(L, "expected an integer argument");
151 }
152 return 0; /* not reached */
153 }
154
155
156 static int setfont(lua_State * L)
157 {
158 int t = lua_gettop(L);
159 int i = luaL_checkinteger(L,1);
160 if (i) {
161 luaL_checktype(L, t, LUA_TTABLE);
162 if (is_valid_font(i)) {
163 if (! (font_touched(i) || font_used(i))) {
164 font_from_lua(L, i);
165 } else {
166 luaL_error(L, "that font has been accessed already, changing it is forbidden");
167 }
168 } else {
169 luaL_error(L, "that integer id is not a valid font");
170 }
171 }
172 return 0;
173 }
174
175 static int addcharacters(lua_State * L)
176 {
177 int t = lua_gettop(L);
178 int i = luaL_checkinteger(L,1);
179 if (i) {
180 luaL_checktype(L, t, LUA_TTABLE);
181 if (is_valid_font(i)) {
182 characters_from_lua(L, i);
183 } else {
184 luaL_error(L, "that integer id is not a valid font");
185 }
186 }
187 return 0;
188 }
189
190 static int settounicode(lua_State * L)
191 {
192 int font = luaL_checkinteger(L,1);
193 if (font && is_valid_font(font)) {
194 int unicode = luaL_checkinteger(L,2);
195 if (quick_char_exists(font,unicode)) {
196 charinfo *co = char_info(font, unicode);
197 if (co) {
198 char *tounicode = lua_type(L,3) == LUA_TSTRING ? (char *) lua_tostring(L,3) : NULL;
199 set_charinfo_tounicode(co, tounicode);
200 glyph_unicode_new(); /* signals that we need to check it */
201 set_font_tounicode(font, 1);
202 }
203 }
204 } else {
205 luaL_error(L, "that integer id is not a valid font");
206 }
207 return 0;
208 }
209
210 static int setexpansion(lua_State * L)
211 {
212 int f = luaL_checkinteger(L,1);
213 if (f) {
214 if (is_valid_font(f)) {
215 int fstretch = luaL_checkinteger(L,2);
216 int fshrink = luaL_checkinteger(L,3);
217 int fstep = luaL_checkinteger(L,4);
218 set_expand_params(f, fstretch, fshrink, fstep);
219 } else {
220 luaL_error(L, "that integer id is not a valid font");
221 }
222 }
223 return 0;
224 }
225
226 /* font.define(id,table) */
227 /* font.define(table) */
228
229 static int deffont(lua_State * L)
230 {
231 int i = 0;
232 int t = lua_gettop(L);
233 if (t == 2) {
234 i = lua_tointeger(L,1);
235 if ((i <= 0) || ! is_valid_font(i)) {
236 lua_pop(L, 1); /* pop the broken table */
237 luaL_error(L, "font creation failed, invalid id passed");
238 }
239 } else if (t == 1) {
240 i = get_fontid();
241 } else {
242 luaL_error(L, "font creation failed, no table passed");
243 return 0;
244 }
245 luaL_checktype(L, -1, LUA_TTABLE);
246 if (font_from_lua(L, i)) {
247 lua_pushinteger(L, i);
248 return 1;
249 } else {
250 lua_pop(L, 1); /* pop the broken table */
251 delete_font(i);
252 luaL_error(L, "font creation failed, error in table");
253 }
254 return 0; /* not reached */
255 }
256
257 /* this returns the expected (!) next fontid. */
258 /* first arg true will keep the id */
259
260 static int nextfontid(lua_State * L)
261 {
262 int b = ((lua_gettop(L) == 1) && lua_toboolean(L,1));
263 int i = get_fontid();
264 lua_pushinteger(L, i);
265 if (b == 0) {
266 delete_font(i);
267 }
268 return 1;
269 }
270
271 static int getfont(lua_State * L)
272 {
273 int i = luaL_checkinteger(L, -1);
274 if (i && is_valid_font(i) && font_to_lua(L, i, 1))
275 return 1;
276 lua_pushnil(L);
277 return 1;
278 }
279
280 static int getcopy(lua_State * L)
281 {
282 int i = luaL_checkinteger(L, -1);
283 if (i && is_valid_font(i) && font_to_lua(L, i, 0))
284 return 1;
285 lua_pushnil(L);
286 return 1;
287 }
288
289 static int getparameters(lua_State * L)
290 {
291 int i = luaL_checkinteger(L, -1);
292 if (i && is_valid_font(i)) {
293 return font_parameters_to_lua(L,i);
294 }
295 return 0;
296 }
297
298 static int getfontid(lua_State * L)
299 {
300 if (lua_type(L, 1) == LUA_TSTRING) {
301 size_t ff;
302 const char *s = lua_tolstring(L, 1, &ff);
303 int cs = string_lookup(s, ff);
304 int f;
305 if (cs == undefined_control_sequence || cs == undefined_cs_cmd || eq_type(cs) != set_font_cmd) {
306 lua_pushstring(L, "not a valid font csname");
307 f = -1;
308 } else {
309 f = equiv(cs);
310 }
311 lua_pushinteger(L, f);
312 } else {
313 luaL_error(L, "expected font csname string as argument");
314 }
315 return 1;
316 }
317
318 static const struct luaL_Reg fontlib[] = {
319 {"read_tfm", font_read_tfm},
320 {"read_vf", font_read_vf},
321 {"current", tex_current_font},
322 {"max", tex_max_font},
323 {"each", tex_each_font},
324 {"getfont", getfont},
325 {"getcopy", getcopy},
326 {"getparameters", getparameters},
327 {"setfont", setfont},
328 {"addcharacters", addcharacters},
329 {"settounicode", settounicode},
330 {"setexpansion", setexpansion},
331 {"define", deffont},
332 {"nextid", nextfontid},
333 {"id", getfontid},
334 {"frozen", frozenfont},
335 {NULL, NULL} /* sentinel */
336 };
337
338 int luaopen_font(lua_State * L)
339 {
340 luaL_openlib(L, "font", fontlib, 0);
341 make_table(L, "fonts", "tex.fonts", "getfont", "setfont");
342 return 1;
343 }
344
345 /**********************************************************************/
346 /* "vf" library: Lua functions within virtual fonts */
347
348 static int l_vf_char(lua_State * L)
349 {
350 int k, w;
351 vf_struct *vsp = static_pdf->vfstruct;
352 packet_stack_record *mat_p;
353 internal_font_number lf = vsp->lf;
354 int ex_glyph = vsp->ex_glyph/1000;
355 if (!vsp->vflua)
356 normal_error("vf", "vf.char() outside virtual font");
357 k = luaL_checkinteger(L, 1);
358 if (!char_exists(lf, k)) {
359 char_warning(lf, k);
360 } else {
361 if (has_packet(lf, k))
362 do_vf_packet(static_pdf, lf, k, ex_glyph);
363 else
364 backend_out_node_glyph[BACKEND_INDEX(glyph_node)] (static_pdf, lf, k, ex_glyph);
365 }
366 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
367 w = char_width(lf, k);
368 if (ex_glyph != 0)
369 w = round_xn_over_d(w, 1000 + ex_glyph, 1000);
370 mat_p->pos.h += w;
371 synch_pos_with_cur(static_pdf->posstruct, vsp->refpos, mat_p->pos);
372 return 0;
373 }
374
375 static int l_vf_down(lua_State * L)
376 {
377 scaled i;
378 vf_struct *vsp = static_pdf->vfstruct;
379 packet_stack_record *mat_p;
380 if (!vsp->vflua)
381 normal_error("vf", "vf.down() outside virtual font");
382 i = (scaled) luaL_checkinteger(L, 1);
383 i = store_scaled_f(i, vsp->fs_f);
384 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
385 mat_p->pos.v += i;
386 synch_pos_with_cur(static_pdf->posstruct, vsp->refpos, mat_p->pos);
387 return 0;
388 }
389
390 static int l_vf_fontid(lua_State * L)
391 {
392 vf_struct *vsp = static_pdf->vfstruct;
393 if (!vsp->vflua)
394 normal_error("vf", "vf.fontid() outside virtual font");
395 vsp->lf = (int) luaL_checkinteger(L, 1);
396 return 0;
397 }
398
399 static int l_vf_image(lua_State * L)
400 {
401 int k;
402 vf_struct *vsp = static_pdf->vfstruct;
403 if (!vsp->vflua)
404 normal_error("vf", "vf.image() outside virtual font");
405 k = (int) luaL_checkinteger(L, 1);
406 vf_out_image(static_pdf, k);
407 return 0;
408 }
409
410 static int l_vf_node(lua_State * L)
411 {
412 int k;
413 vf_struct *vsp = static_pdf->vfstruct;
414 if (!vsp->vflua)
415 normal_error("vf", "vf.node() outside virtual font");
416 k = (int) luaL_checkinteger(L, 1);
417 hlist_out(static_pdf, (halfword) k, 0);
418 return 0;
419 }
420
421 static int l_vf_nop(lua_State * L)
422 {
423 vf_struct *vsp = static_pdf->vfstruct;
424 if (!vsp->vflua)
425 normal_error("vf", "vf.nop() outside virtual font");
426 return 0;
427 }
428
429 static int l_vf_pop(lua_State * L)
430 {
431 vf_struct *vsp = static_pdf->vfstruct;
432 packet_stack_record *mat_p;
433 if (!vsp->vflua)
434 normal_error("vf", "vf.pop() outside virtual font");
435 if (vsp->packet_stack_level == vsp->packet_stack_minlevel)
436 normal_error("vf", "packet_stack_level underflow");
437 vsp->packet_stack_level--;
438 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
439 synch_pos_with_cur(static_pdf->posstruct, vsp->refpos, mat_p->pos);
440 return 0;
441 }
442
443 static int l_vf_push(lua_State * L)
444 {
445 vf_struct *vsp = static_pdf->vfstruct;
446 packet_stack_record *mat_p;
447 if (!vsp->vflua)
448 normal_error("vf", "vf.push() outside virtual font");
449 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
450 vsp->packet_stack_level++;
451 if (vsp->packet_stack_level == packet_stack_size)
452 normal_error("vf", "packet_stack_level overflow");
453 vsp->packet_stack[vsp->packet_stack_level] = *mat_p;
454 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
455 return 0;
456 }
457
458 static int l_vf_right(lua_State * L)
459 {
460 scaled i;
461 vf_struct *vsp = static_pdf->vfstruct;
462 int ex_glyph = vsp->ex_glyph/1000;
463 packet_stack_record *mat_p;
464 if (!vsp->vflua)
465 normal_error("vf", "vf.right() outside virtual font");
466 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
467 i = (scaled) luaL_checkinteger(L, 1);
468 if (ex_glyph != 0 && i != 0) /* new, experiment */
469 i = round_xn_over_d(i, 1000 + ex_glyph, 1000);
470 i = store_scaled_f(i, vsp->fs_f);
471 mat_p->pos.h += i;
472 synch_pos_with_cur(static_pdf->posstruct, vsp->refpos, mat_p->pos);
473 return 0;
474 }
475
476 static int l_vf_rule(lua_State * L)
477 {
478 scaledpos size;
479 vf_struct *vsp = static_pdf->vfstruct;
480 int ex_glyph = vsp->ex_glyph/1000;
481 packet_stack_record *mat_p;
482 if (!vsp->vflua)
483 normal_error("vf", "vf.rule() outside virtual font");
484 size.h = (scaled) luaL_checkinteger(L, 1);
485 size.v = (scaled) luaL_checkinteger(L, 2);
486 if (ex_glyph != 0 && size.h > 0) /* new, experiment */
487 size.h = round_xn_over_d(size.h, 1000 + ex_glyph, 1000);
488 size.h = store_scaled_f(size.h, vsp->fs_f);
489 size.v = store_scaled_f(size.v, vsp->fs_f);
490 if (size.h > 0 && size.v > 0)
491 backend_out_node_rule[BACKEND_INDEX(rule_node)](static_pdf, 0, size,0); /* the 0s are unused */
492 mat_p = &(vsp->packet_stack[vsp->packet_stack_level]);
493 mat_p->pos.h += size.h;
494 synch_pos_with_cur(static_pdf->posstruct, vsp->refpos, mat_p->pos);
495 return 0;
496 }
497
498 static int l_vf_special(lua_State * L)
499 {
500 const_lstring st;
501 int texstr;
502 vf_struct *vsp = static_pdf->vfstruct;
503 if (!vsp->vflua)
504 normal_error("vf", "vf.special() outside virtual font");
505 st.s = lua_tolstring(L, 1, &(st.l));
506 texstr = maketexlstring(st.s, st.l);
507 pdf_literal(static_pdf, texstr, scan_special, false);
508 flush_str(texstr);
509 return 0;
510 }
511
512 static int l_vf_pdf(lua_State * L)
513 {
514 vf_struct *vsp = static_pdf->vfstruct;
515 if (!vsp->vflua)
516 normal_error("vf", "vf.special() outside virtual font");
517 luapdfprint(L);
518 pdf_out(static_pdf, '\n');
519 return 0;
520 }
521
522 static const struct luaL_Reg vflib[] = {
523 {"char", l_vf_char},
524 {"down", l_vf_down},
525 /* {"font", l_vf_font}, */
526 {"fontid", l_vf_fontid},
527 {"image", l_vf_image},
528 /* {"lua", l_vf_lua}, */
529 {"node", l_vf_node},
530 {"nop", l_vf_nop},
531 {"pop", l_vf_pop},
532 {"push", l_vf_push},
533 {"right", l_vf_right},
534 {"rule", l_vf_rule},
535 /* {"scale", l_vf_scale}, */
536 /* {"slot", l_vf_slot}, */
537 {"special", l_vf_special},
538 {"pdf", l_vf_pdf},
539 {NULL, NULL} /* sentinel */
540 };
541
542 int luaopen_vf(lua_State * L)
543 {
544 luaL_openlib(L, "vf", vflib, 0);
545 return 1;
546 }