Debian Codegraph @

texk/web2c/mplibdir/lmplib.c

raw
1 /* lmplib.c
2
3 Copyright 2012 Taco Hoekwater <[email protected]>
4
5 This file is part of the MetaPost tarball, but belongs to LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 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 Lesser General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20 */
21 #include "mpconfig.h"
22 #include <w2c/config.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <math.h>
29
30 #ifndef pdfTeX
31 # include <lua.h>
32 # include <lauxlib.h>
33 # include <lualib.h>
34 #else
35 # include "lua.h"
36 # include "lauxlib.h"
37 # include "lualib.h"
38 #define luaL_reg luaL_Reg
39 #define lua_objlen lua_rawlen
40 #endif
41
42 #ifndef luaL_reg
43 #define luaL_reg luaL_Reg
44 #endif
45
46 #ifndef lua_objlen
47 #define lua_objlen lua_rawlen
48 #endif
49
50 #include "mplib.h"
51 #include "mplibps.h"
52 #include "mplibsvg.h"
53 #include "mplibpng.h"
54
55 int luaopen_mplib(lua_State * L);
56
57 /*tex
58
59 We need a few metatable identifiers in order to access the metatables for the
60 main object and result userdata.
61
62 */
63
64 #define MPLIB_METATABLE "MPlib.meta"
65 #define MPLIB_FIG_METATABLE "MPlib.fig"
66 #define MPLIB_GR_METATABLE "MPlib.gr"
67
68 #define is_mp(L,b) (MP *)luaL_checkudata(L,b,MPLIB_METATABLE)
69 #define is_fig(L,b) (struct mp_edge_object **)luaL_checkudata(L,b,MPLIB_FIG_METATABLE)
70 #define is_gr_object(L,b) (struct mp_graphic_object **)luaL_checkudata(L,b,MPLIB_GR_METATABLE)
71
72 /*tex
73
74 We pre-hash the \LUA\ strings which is much faster. The approach is similar to the one
75 used at the \TEX\ end.
76
77 */
78
79 #define mplib_init_S(a) do { \
80 lua_pushliteral(L,#a); \
81 mplib_##a##_ptr = lua_tostring(L,-1); \
82 mplib_##a##_index = luaL_ref (L,LUA_REGISTRYINDEX); \
83 } while (0)
84
85 #define mplib_push_S(a) do { \
86 lua_rawgeti(L,LUA_REGISTRYINDEX,mplib_##a##_index); \
87 } while (0)
88
89 #define mplib_is_S(a,i) \
90 (mplib_##a##_ptr==lua_tostring(L,i))
91
92 #define mplib_make_S(a) \
93 static int mplib_##a##_index = 0; \
94 static const char *mplib_##a##_ptr = NULL
95
96 /*tex In the next array entry 0 is not used */
97
98 static int mplib_type_Ses[mp_special_code + 1] = { 0 };
99
100 mplib_make_S(term);
101 mplib_make_S(error);
102 mplib_make_S(log);
103 mplib_make_S(fig);
104 mplib_make_S(status);
105
106 mplib_make_S(memory);
107 mplib_make_S(hash);
108 mplib_make_S(params);
109 mplib_make_S(open);
110 mplib_make_S(cycle);
111
112 mplib_make_S(offset);
113 mplib_make_S(dashes);
114
115 mplib_make_S(fill);
116 mplib_make_S(outline);
117 mplib_make_S(text);
118 mplib_make_S(special);
119 mplib_make_S(start_bounds);
120 mplib_make_S(stop_bounds);
121 mplib_make_S(start_clip);
122 mplib_make_S(stop_clip);
123
124 mplib_make_S(left_type);
125 mplib_make_S(right_type);
126 mplib_make_S(x_coord);
127 mplib_make_S(y_coord);
128 mplib_make_S(left_x);
129 mplib_make_S(left_y);
130 mplib_make_S(right_x);
131 mplib_make_S(right_y);
132 mplib_make_S(left_tension);
133 mplib_make_S(right_tension);
134 mplib_make_S(left_curl);
135 mplib_make_S(right_curl);
136 mplib_make_S(direction_x);
137 mplib_make_S(direction_y);
138
139 mplib_make_S(color);
140 mplib_make_S(dash);
141 mplib_make_S(depth);
142 mplib_make_S(dsize);
143 mplib_make_S(font);
144 mplib_make_S(height);
145 mplib_make_S(htap);
146 mplib_make_S(linecap);
147 mplib_make_S(linejoin);
148 mplib_make_S(miterlimit);
149 mplib_make_S(path);
150 mplib_make_S(pen);
151 mplib_make_S(postscript);
152 mplib_make_S(prescript);
153 mplib_make_S(transform);
154 mplib_make_S(type);
155 mplib_make_S(width);
156 mplib_make_S(elliptical);
157
158 static void mplib_init_Ses(lua_State * L)
159 {
160 mplib_init_S(term);
161 mplib_init_S(error);
162 mplib_init_S(log);
163 mplib_init_S(fig);
164 mplib_init_S(status);
165
166 mplib_init_S(memory);
167 mplib_init_S(hash);
168 mplib_init_S(params);
169 mplib_init_S(open);
170 mplib_init_S(cycle);
171
172 mplib_init_S(offset);
173 mplib_init_S(dashes);
174
175 mplib_init_S(fill);
176 mplib_init_S(outline);
177 mplib_init_S(text);
178 mplib_init_S(start_bounds);
179 mplib_init_S(stop_bounds);
180 mplib_init_S(start_clip);
181 mplib_init_S(stop_clip);
182 mplib_init_S(special);
183
184 mplib_type_Ses[mp_fill_code] = mplib_fill_index;
185 mplib_type_Ses[mp_stroked_code] = mplib_outline_index;
186 mplib_type_Ses[mp_text_code] = mplib_text_index;
187 mplib_type_Ses[mp_start_bounds_code] = mplib_start_bounds_index;
188 mplib_type_Ses[mp_stop_bounds_code] = mplib_stop_bounds_index;
189 mplib_type_Ses[mp_start_clip_code] = mplib_start_clip_index;
190 mplib_type_Ses[mp_stop_clip_code] = mplib_stop_clip_index;
191 mplib_type_Ses[mp_special_code] = mplib_special_index;
192
193 mplib_init_S(left_type);
194 mplib_init_S(right_type);
195 mplib_init_S(x_coord);
196 mplib_init_S(y_coord);
197 mplib_init_S(left_x);
198 mplib_init_S(left_y);
199 mplib_init_S(right_x);
200 mplib_init_S(right_y);
201 mplib_init_S(left_tension);
202 mplib_init_S(right_tension);
203 mplib_init_S(left_curl);
204 mplib_init_S(right_curl);
205 mplib_init_S(direction_x);
206 mplib_init_S(direction_y);
207
208 mplib_init_S(color);
209 mplib_init_S(dash);
210 mplib_init_S(depth);
211 mplib_init_S(dsize);
212 mplib_init_S(font);
213 mplib_init_S(height);
214 mplib_init_S(htap);
215 mplib_init_S(linecap);
216 mplib_init_S(linejoin);
217 mplib_init_S(miterlimit);
218 mplib_init_S(path);
219 mplib_init_S(pen);
220 mplib_init_S(postscript);
221 mplib_init_S(prescript);
222 mplib_init_S(transform);
223 mplib_init_S(type);
224 mplib_init_S(width);
225 mplib_init_S(elliptical);
226 }
227
228 /*tex
229
230 Here are some enumeration arrays to map MPlib enums to \LUA\ strings. If needed
231 we can also predefine keys here, as we do with nodes.
232
233 */
234
235 static const char *math_options[] =
236 { "scaled", "double", "binary", "decimal", NULL };
237
238 static const char *interaction_options[] =
239 { "unknown", "batch", "nonstop", "scroll", "errorstop", NULL };
240
241 static const char *mplib_filetype_names[] =
242 { "term", "error", "mp", "log", "ps", "png", "mem", "tfm", "map", "pfb", "enc", NULL };
243
244 static const char *knot_type_enum[] =
245 { "endpoint", "explicit", "given", "curl", "open", "end_cycle" };
246
247 static const char *fill_fields[] =
248 { "type", "path", "htap", "pen", "color", "linejoin", "miterlimit",
249 "prescript", "postscript", NULL };
250
251 static const char *stroked_fields[] =
252 { "type", "path", "pen", "color", "linejoin", "miterlimit", "linecap",
253 "dash", "prescript", "postscript", NULL };
254
255 static const char *text_fields[] =
256 { "type", "text", "dsize", "font", "color", "width", "height", "depth",
257 "transform", "prescript", "postscript", NULL };
258
259 static const char *special_fields[] =
260 { "type", "prescript", NULL };
261
262 static const char *start_bounds_fields[] =
263 { "type", "path", NULL };
264
265 static const char *start_clip_fields[] =
266 { "type", "path", NULL };
267
268 static const char *stop_bounds_fields[] =
269 { "type", NULL };
270
271 static const char *stop_clip_fields[] =
272 { "type", NULL };
273
274 static const char *no_fields[] =
275 { NULL };
276
277 /*tex
278
279 The list of supported MPlib options (not all make sense).
280
281 */
282
283 typedef enum {
284 P_ERROR_LINE,
285 P_MAX_LINE,
286 P_RANDOM_SEED,
287 P_MATH_MODE,
288 P_INTERACTION,
289 P_INI_VERSION,
290 P_MEM_NAME,
291 P_JOB_NAME,
292 P_FIND_FILE,
293 P_RUN_SCRIPT,
294 P_MAKE_TEXT,
295 P_SCRIPT_ERROR,
296 P_EXTENSIONS,
297 P_UTF8_MODE,
298 P__SENTINEL
299 } mplib_parm_idx;
300
301 typedef struct {
302 /*tex parameter name */
303 const char *name;
304 /*tex parameter index */
305 mplib_parm_idx idx;
306 } mplib_parm_struct;
307
308 static mplib_parm_struct mplib_parms[] = {
309 {"error_line", P_ERROR_LINE },
310 {"print_line", P_MAX_LINE },
311 {"random_seed", P_RANDOM_SEED },
312 {"interaction", P_INTERACTION },
313 {"job_name", P_JOB_NAME },
314 {"find_file", P_FIND_FILE },
315 {"run_script", P_RUN_SCRIPT },
316 {"make_text", P_MAKE_TEXT },
317 {"script_error", P_SCRIPT_ERROR },
318 {"extensions", P_EXTENSIONS },
319 {"math_mode", P_MATH_MODE },
320 {"utf8_mode", P_UTF8_MODE },
321 {NULL, P__SENTINEL }
322 };
323
324 /*tex
325
326 We start by defining the needed callback routines for the library.
327
328 */
329
330 static char *mplib_find_file(MP mp, const char *fname, const char *fmode, int ftype)
331 {
332 lua_State *L = (lua_State *)mp_userdata(mp);
333 lua_checkstack(L, 4);
334 lua_getfield(L, LUA_REGISTRYINDEX, "mplib.file_finder");
335 if (lua_isfunction(L, -1)) {
336 char *s = NULL;
337 const char *x = NULL;
338 lua_pushstring(L, fname);
339 lua_pushstring(L, fmode);
340 if (ftype >= mp_filetype_text) {
341 lua_pushinteger(L, (ftype - mp_filetype_text));
342 } else {
343 lua_pushstring(L, mplib_filetype_names[ftype]);
344 }
345 if (lua_pcall(L, 3, 1, 0) != 0) {
346 fprintf(stdout, "Error in mp.find_file: %s\n", lua_tostring(L, -1));
347 return NULL;
348 }
349 x = lua_tostring(L, -1);
350 if (x != NULL)
351 s = strdup(x);
352 /*tex pop the string */
353 lua_pop(L, 1);
354 return s;
355 } else {
356 lua_pop(L, 1);
357 }
358 if (fmode[0] != 'r' || (!access(fname, R_OK)) || ftype) {
359 return strdup(fname);
360 }
361 return NULL;
362 }
363
364 static int mplib_find_file_function(lua_State * L)
365 {
366 if (!(lua_isfunction(L, -1) || lua_isnil(L, -1))) {
367 /*tex An error. */
368 return 1;
369 }
370 lua_pushstring(L, "mplib.file_finder");
371 lua_pushvalue(L, -2);
372 lua_rawset(L, LUA_REGISTRYINDEX);
373 return 0;
374 }
375
376 static void mplib_warning(const char *str)
377 {
378 fprintf(stdout,"mplib warning: %s\n",str);
379 }
380
381 static void mplib_script_error(MP mp, const char *str)
382 {
383 lua_State *L = (lua_State *)mp_userdata(mp);
384 lua_checkstack(L, 1);
385 lua_getfield(L, LUA_REGISTRYINDEX, "mplib.script_error");
386 if (lua_isfunction(L, -1)) {
387 lua_pushstring(L, str);
388 /*tex We assume that the function is okay. */
389 lua_pcall(L, 1, 0, 0);
390 } else {
391 mplib_warning(str);
392 lua_pop(L, 1);
393 }
394 }
395
396 static int mplib_script_error_function(lua_State * L)
397 {
398 if (!(lua_isfunction(L, -1) || lua_isnil(L, -1))) {
399 /*tex An error. */
400 return 1;
401 }
402 lua_pushstring(L, "mplib.script_error");
403 lua_pushvalue(L, -2);
404 lua_rawset(L, LUA_REGISTRYINDEX);
405 return 0;
406 }
407
408 static char *mplib_run_script(MP mp, const char *str, size_t len)
409 {
410 lua_State *L = (lua_State *)mp_userdata(mp);
411 lua_checkstack(L, 1);
412 lua_getfield(L, LUA_REGISTRYINDEX, "mplib.run_script");
413 if (lua_isfunction(L, -1)) {
414 char *s = NULL;
415 const char *x = NULL;
416 lua_pushlstring(L, str, len);
417 if (lua_pcall(L, 1, 1, 0) != 0) {
418 fprintf(stdout,"mplib warning: error in script: %s\n",lua_tostring(L, -1));
419 return NULL;
420 }
421 x = lua_tostring(L, -1);
422 if (x != NULL)
423 s = strdup(x);
424 /*tex Pop the string. */
425 lua_pop(L, 1);
426 return s;
427 } else {
428 lua_pop(L, 1);
429 }
430 return NULL;
431 }
432
433 static int mplib_run_script_function(lua_State * L)
434 {
435 if (!(lua_isfunction(L, -1) || lua_isnil(L, -1))) {
436 return 1; /* error */
437 }
438 lua_pushstring(L, "mplib.run_script");
439 lua_pushvalue(L, -2);
440 lua_rawset(L, LUA_REGISTRYINDEX);
441 return 0;
442 }
443
444 static char *mplib_make_text(MP mp, const char *str, size_t len, int mode)
445 {
446 lua_State *L = (lua_State *)mp_userdata(mp);
447 lua_checkstack(L, 1);
448 lua_getfield(L, LUA_REGISTRYINDEX, "mplib.make_text");
449 if (lua_isfunction(L, -1)) {
450 char *s = NULL;
451 const char *x = NULL;
452 lua_pushlstring(L, str, len);
453 lua_pushinteger(L, mode);
454 if (lua_pcall(L, 2, 1, 0) != 0) {
455 mplib_script_error(mp, lua_tostring(L, -1));
456 return NULL;
457 }
458 x = lua_tostring(L, -1);
459 if (x != NULL)
460 s = strdup(x);
461 /*tex Pop the string. */
462 lua_pop(L, 1);
463 return s;
464 } else {
465 lua_pop(L, 1);
466 }
467 return NULL;
468 }
469
470 static int mplib_make_text_function(lua_State * L)
471 {
472 if (!(lua_isfunction(L, -1) || lua_isnil(L, -1))) {
473 /*tex An error. */
474 return 1;
475 }
476 lua_pushstring(L, "mplib.make_text");
477 lua_pushvalue(L, -2);
478 lua_rawset(L, LUA_REGISTRYINDEX);
479 return 0;
480 }
481
482 static int mplib_get_numeric(lua_State * L)
483 {
484 MP *mp = is_mp(L, 1);
485 if (*mp != NULL) {
486 size_t l;
487 const char *s = lua_tolstring(L, 2, &l);
488 if (s != NULL) {
489 lua_pushnumber(L, mp_get_numeric_value(*mp,s,l));
490 return 1;
491 }
492 }
493 lua_pushnumber(L,0);
494 return 1;
495 }
496
497 static int mplib_get_boolean(lua_State * L)
498 {
499 MP *mp = is_mp(L, 1);
500 if (*mp != NULL) {
501 size_t l;
502 const char *s = lua_tolstring(L, 2, &l);
503 if (s != NULL) {
504 lua_pushboolean(L, mp_get_boolean_value(*mp,s,l));
505 return 1;
506 }
507 }
508 lua_pushboolean(L,0);
509 return 1;
510 }
511
512 static int mplib_get_string(lua_State * L)
513 {
514 MP *mp = is_mp(L, 1);
515 if (*mp != NULL) {
516 size_t l;
517 const char *s = lua_tolstring(L, 2, &l);
518 if (s != NULL) {
519 char *r = mp_get_string_value(*mp,s,l) ;
520 if (r != NULL) {
521 lua_pushstring(L, r);
522 return 1;
523 }
524 }
525 }
526 lua_pushstring(L,"");
527 return 1;
528 }
529
530 #define xfree(A) if ((A)!=NULL) { free((A)); A = NULL; }
531
532 static int mplib_new(lua_State * L)
533 {
534 MP *mp_ptr;
535 mp_ptr = lua_newuserdata(L, sizeof(MP *));
536 if (mp_ptr) {
537 int i;
538 struct MP_options *options = mp_options();
539 options->userdata = (void *) L;
540 /*tex Required: */
541 options->noninteractive = 1;
542 options->extensions = 0 ;
543 options->find_file = mplib_find_file;
544 options->run_script = mplib_run_script;
545 options->make_text = mplib_make_text;
546 /* options->script_error = mplib_script_error; */
547 options->print_found_names = 1;
548 options->ini_version = 1;
549 options->utf8_mode = 0;
550 if (lua_type(L, 1) == LUA_TTABLE) {
551 for (i = 0; mplib_parms[i].name != NULL; i++) {
552 lua_getfield(L, 1, mplib_parms[i].name);
553 if (lua_isnil(L, -1)) {
554 lua_pop(L, 1);
555 continue;
556 }
557 switch (mplib_parms[i].idx) {
558 case P_ERROR_LINE:
559 options->error_line = (int)lua_tointeger(L, -1);
560 if (options->error_line < 60)
561 options->error_line = 60;
562 if (options->error_line > 250)
563 options->error_line = 250;
564 options->half_error_line = (options->error_line/2)+10;
565 break;
566 case P_MAX_LINE:
567 options->max_print_line = (int)lua_tointeger(L, -1);
568 if (options->max_print_line < 60)
569 options->max_print_line = 60;
570 break;
571 case P_RANDOM_SEED:
572 options->random_seed = (int)lua_tointeger(L, -1);
573 break;
574 case P_INTERACTION:
575 options->interaction = luaL_checkoption(L, -1, "errorstopmode", interaction_options);
576 break;
577 case P_MATH_MODE:
578 options->math_mode = luaL_checkoption(L, -1, "scaled", math_options);
579 break;
580 case P_JOB_NAME:
581 options->job_name = strdup(lua_tostring(L, -1));
582 break;
583 case P_FIND_FILE:
584 if (mplib_find_file_function(L)) {
585 mplib_warning("function expected for 'find_file'");
586 }
587 break;
588 case P_RUN_SCRIPT:
589 if (mplib_run_script_function(L)) {
590 mplib_warning("function expected for 'run_script'");
591 }
592 break;
593 case P_MAKE_TEXT:
594 if (mplib_make_text_function(L)) {
595 mplib_warning("function expected for 'make_text'");
596 }
597 break;
598 case P_SCRIPT_ERROR:
599 if (mplib_script_error_function(L)) {
600 mplib_warning("function expected for 'script_error'");
601 }
602 break;
603 case P_EXTENSIONS:
604 options->extensions = (int)lua_tointeger(L, -1);
605 break;
606 case P_UTF8_MODE:
607 options->utf8_mode = (int)lua_toboolean(L, -1);
608 break;
609 default:
610 break;
611 }
612 lua_pop(L, 1);
613 }
614 }
615 *mp_ptr = mp_initialize(options);
616 xfree(options->command_line);
617 xfree(options->mem_name);
618 free(options);
619 if (*mp_ptr) {
620 luaL_getmetatable(L, MPLIB_METATABLE);
621 lua_setmetatable(L, -2);
622 return 1;
623 }
624 }
625 lua_pushnil(L);
626 return 1;
627 }
628
629 static int mplib_collect(lua_State * L)
630 {
631 MP *mp_ptr = is_mp(L, 1);
632 if (*mp_ptr != NULL) {
633 (void)mp_finish(*mp_ptr);
634 *mp_ptr = NULL;
635 }
636 return 0;
637 }
638
639 static int mplib_tostring(lua_State * L)
640 {
641 MP *mp_ptr = is_mp(L, 1);
642 if (*mp_ptr != NULL) {
643 (void) lua_pushfstring(L, "<MP %p>", *mp_ptr);
644 return 1;
645 }
646 return 0;
647 }
648
649 static int mplib_wrapresults(lua_State * L, mp_run_data *res, int status)
650 {
651 lua_checkstack(L, 5);
652 lua_newtable(L);
653 if (res->term_out.used != 0) {
654 mplib_push_S(term);
655 lua_pushlstring(L, res->term_out.data, res->term_out.used);
656 lua_rawset(L,-3);
657 }
658 if (res->error_out.used != 0) {
659 mplib_push_S(error);
660 lua_pushlstring(L, res->error_out.data, res->error_out.used);
661 lua_rawset(L,-3);
662 }
663 if (res->log_out.used != 0) {
664 mplib_push_S(log);
665 lua_pushlstring(L, res->log_out.data, res->log_out.used);
666 lua_rawset(L,-3);
667 }
668 if (res->edges != NULL) {
669 struct mp_edge_object **v;
670 struct mp_edge_object *p = res->edges;
671 int i = 1;
672 mplib_push_S(fig);
673 lua_newtable(L);
674 while (p != NULL) {
675 v = lua_newuserdata(L, sizeof(struct mp_edge_object *));
676 *v = p;
677 luaL_getmetatable(L, MPLIB_FIG_METATABLE);
678 lua_setmetatable(L, -2);
679 lua_rawseti(L, -2, i);
680 i++;
681 p = p->next;
682 }
683 lua_rawset(L,-3);
684 res->edges = NULL;
685 }
686 mplib_push_S(status);
687 lua_pushinteger(L, status);
688 lua_rawset(L,-3);
689 return 1;
690 }
691
692 static int mplib_execute(lua_State * L)
693 {
694 MP *mp_ptr;
695 if (lua_gettop(L)!=2) {
696 lua_pushnil(L);
697 return 1;
698 }
699 mp_ptr = is_mp(L, 1);
700 if (*mp_ptr != NULL && lua_isstring(L, 2)) {
701 size_t l;
702 char *s = xstrdup(lua_tolstring(L, 2, &l));
703 int h = mp_execute(*mp_ptr, s, l);
704 mp_run_data *res = mp_rundata(*mp_ptr);
705 free(s);
706 return mplib_wrapresults(L, res, h);
707 } else {
708 lua_pushnil(L);
709 }
710 return 1;
711 }
712
713 static int mplib_finish(lua_State * L)
714 {
715 MP *mp_ptr = is_mp(L, 1);
716 if (*mp_ptr != NULL) {
717 int i;
718 int h = mp_execute(*mp_ptr,NULL,0);
719 mp_run_data *res = mp_rundata(*mp_ptr);
720 i = mplib_wrapresults(L, res, h);
721 (void)mp_finish(*mp_ptr);
722 *mp_ptr = NULL;
723 return i;
724 } else {
725 lua_pushnil(L);
726 }
727 return 1;
728 }
729
730 static int mplib_char_dimension(lua_State * L, int t)
731 {
732 MP *mp_ptr = is_mp(L, 1);
733 if (*mp_ptr != NULL) {
734 char *fname = xstrdup(luaL_checkstring(L,2));
735 int charnum = (int)luaL_checkinteger(L,3);
736 if (charnum<0 || charnum>255) {
737 lua_pushnumber(L, (lua_Number)0);
738 } else {
739 lua_pushnumber(L,(lua_Number)mp_get_char_dimension(*mp_ptr,fname,charnum,t));
740 }
741 free(fname);
742 } else {
743 lua_pushnumber(L, (lua_Number)0);
744 }
745 return 1;
746 }
747
748 static int mplib_charwidth(lua_State * L)
749 {
750 return mplib_char_dimension(L, 'w');
751 }
752
753 static int mplib_chardepth(lua_State * L)
754 {
755 return mplib_char_dimension(L, 'd');
756 }
757
758 static int mplib_charheight(lua_State * L)
759 {
760 return mplib_char_dimension(L, 'h');
761 }
762
763 static int mplib_version(lua_State * L)
764 {
765 char *s = mp_metapost_version();
766 lua_pushstring(L, s);
767 free(s);
768 return 1;
769 }
770
771 static int mplib_statistics(lua_State * L)
772 {
773 MP *mp_ptr = is_mp(L, 1);
774 if (*mp_ptr != NULL) {
775 lua_newtable(L);
776 mplib_push_S(memory);
777 lua_pushinteger(L, mp_memory_usage(*mp_ptr));
778 lua_rawset(L,-3);
779 mplib_push_S(hash);
780 lua_pushinteger(L, mp_hash_usage(*mp_ptr));
781 lua_rawset(L,-3);
782 mplib_push_S(params);
783 lua_pushinteger(L, mp_param_usage(*mp_ptr));
784 lua_rawset(L,-3);
785 mplib_push_S(open);
786 lua_pushinteger(L, mp_open_usage(*mp_ptr));
787 lua_rawset(L,-3);
788 } else {
789 lua_pushnil(L);
790 }
791 return 1;
792 }
793
794 static int set_direction (lua_State * L, MP mp, mp_knot p) {
795 double direction_x = 0, direction_y = 0;
796 direction_x = (double)lua_tonumber(L,-1);
797 lua_pop(L,1);
798 mplib_push_S(direction_y);
799 lua_rawget(L,-2);
800 if (!lua_isnumber(L,-1)) {
801 return 0;
802 }
803 direction_y = (double)lua_tonumber(L,-1);
804 lua_pop(L,1);
805 if (!mp_set_knot_direction(mp, p, direction_x, direction_y)) {
806 return 0;
807 }
808 return 1;
809 }
810
811 static int set_left_curl (lua_State * L, MP mp, mp_knot p) {
812 double curl = (double)lua_tonumber(L,-1);
813 lua_pop(L,1);
814 if (!mp_set_knot_left_curl(mp, p, curl)) {
815 return 0;
816 }
817 return 1;
818 }
819
820 static int set_left_tension (lua_State * L, MP mp, mp_knot p) {
821 double tension = (double)lua_tonumber(L,-1);
822 lua_pop(L,1);
823 if (!mp_set_knot_left_tension(mp, p, tension)) {
824 return 0;
825 }
826 return 1;
827 }
828
829 static int set_left_control (lua_State * L, MP mp, mp_knot p) {
830 double x, y;
831 x = (double)lua_tonumber(L,-1);
832 lua_pop(L,1);
833 mplib_push_S(left_y);
834 lua_rawget(L,-2);
835 if (!lua_isnumber(L,-1)) {
836 return 0;
837 }
838 y = (double)lua_tonumber(L,-1);
839 lua_pop(L,1);
840 if (!mp_set_knot_left_control(mp, p, x, y)) {
841 return 0;
842 }
843 return 1;
844 }
845
846 static int set_right_curl (lua_State * L, MP mp, mp_knot p) {
847 double curl = (double)lua_tonumber(L,-1);
848 lua_pop(L,1);
849 if (!mp_set_knot_right_curl(mp, p, curl)) {
850 return 0;
851 }
852 return 1;
853 }
854
855 static int set_right_tension (lua_State * L, MP mp, mp_knot p) {
856 double tension = (double)lua_tonumber(L,-1);
857 lua_pop(L,1);
858 if (!mp_set_knot_right_tension(mp, p, tension)) {
859 return 0;
860 }
861 return 1;
862 }
863
864 static int set_right_control (lua_State * L, MP mp, mp_knot p) {
865 double x, y;
866 x = (double)lua_tonumber(L,-1);
867 lua_pop(L,1);
868 mplib_push_S(right_y);
869 lua_rawget(L,-2);
870 if (!lua_isnumber(L,-1)) {
871 return 0;
872 }
873 y = (double)lua_tonumber(L,-1);
874 lua_pop(L,1);
875 if (!mp_set_knot_right_control(mp, p, x, y)) {
876 return 0;
877 }
878 return 1;
879 }
880
881 #if 0
882
883 #define ROUNDED_ZERO(v) (fabs((v))<0.00001 ? 0 : (v))
884 #define PI 3.1415926535897932384626433832795028841971
885 #define RADIANS(a) (mp_number_as_double(mp,(a)) / 16.0) * PI/180.0
886
887 void mp_dump_path (MP mp, mp_knot h) {
888 mp_knot p, q;
889 if (h == NULL)
890 return;
891 p = h;
892 do {
893 q=mp_knot_next(mp,p);
894 if ( (p==NULL)||(q==NULL) ) {
895 printf("\n???");
896 return; /* this won't happen */
897 }
898 printf ("(%g,%g)", mp_number_as_double(mp,mp_knot_x_coord(mp,p)),
899 mp_number_as_double(mp,mp_knot_y_coord(mp,p)));
900 switch (mp_knot_right_type(mp,p)) {
901 case mp_endpoint:
902 if ( mp_knot_left_type(mp,p)==mp_open )
903 printf("{open?}");
904 if ( (mp_knot_left_type(mp,q)!=mp_endpoint)||(q!=h) )
905 q=NULL; /* force an error */
906 goto DONE;
907 break;
908 case mp_explicit:
909 printf ("..controls (%g,%g)",
910 mp_number_as_double(mp,mp_knot_right_x(mp,p)),
911 mp_number_as_double(mp,mp_knot_right_y(mp,p)));
912 printf(" and ");
913 if ( mp_knot_left_type(mp,q)!=mp_explicit ) {
914 printf("??");
915 } else {
916 printf ("(%g,%g)",mp_number_as_double(mp,mp_knot_left_x(mp,q)),
917 mp_number_as_double(mp,mp_knot_left_y(mp,q)));
918 }
919 goto DONE;
920 break;
921 case mp_open:
922 if ( (mp_knot_left_type(mp,p)!=mp_explicit) &&
923 (mp_knot_left_type(mp,p)!=mp_open) ) {
924 printf("{open?}");
925 }
926 break;
927 case mp_curl:
928 case mp_given:
929 if ( mp_knot_left_type(mp,p)==mp_open )
930 printf("??");
931 if ( mp_knot_right_type(mp,p)==mp_curl ) {
932 printf("{curl %g}", mp_number_as_double(mp,mp_knot_right_curl(mp,p)));
933 } else {
934 double rad = RADIANS(mp_knot_right_curl(mp,p));
935 double n_cos = ROUNDED_ZERO(cos(rad)*4096);
936 double n_sin = ROUNDED_ZERO(sin(rad)*4096);
937 printf("{%g,%g}", n_cos, n_sin);
938 }
939 break;
940 }
941 if ( mp_knot_left_type(mp,q)<=mp_explicit ) {
942 printf("..control?"); /* can't happen */
943 } else if ((mp_number_as_double(mp,mp_knot_right_tension(mp,p))!=(1.0))||
944 (mp_number_as_double(mp,mp_knot_left_tension(mp,q)) !=(1.0))) {
945 printf("..tension ");
946 if ( mp_number_as_double(mp,mp_knot_right_tension(mp,p))<0.0 )
947 printf("atleast ");
948 printf("%g", fabs(mp_number_as_double(mp,mp_knot_right_tension(mp,p))));
949 if (mp_number_as_double(mp,mp_knot_right_tension(mp,p)) !=
950 mp_number_as_double(mp,mp_knot_left_tension(mp,q))) {
951 printf(" and ");
952 if (mp_number_as_double(mp,mp_knot_left_tension(mp,q))< 0.0)
953 printf("atleast ");
954 printf("%g", fabs(mp_number_as_double(mp,mp_knot_left_tension(mp,q))));
955 }
956 }
957 DONE:
958 p=q;
959 if ( p!=h || mp_knot_left_type(mp,h)!=mp_endpoint) {
960 printf ("\n ..");
961 if ( mp_knot_left_type(mp,p) == mp_given ) {
962 double rad = RADIANS(mp_knot_left_curl(mp,p));
963 double n_cos = ROUNDED_ZERO(cos(rad)*4096);
964 double n_sin = ROUNDED_ZERO(sin(rad)*4096);
965 printf("{%g,%g}", n_cos, n_sin);
966 } else if ( mp_knot_left_type(mp,p) ==mp_curl ){
967 printf("{curl %g}", mp_number_as_double(mp,mp_knot_left_curl(mp,p)));
968 }
969 }
970 } while (p!=h);
971 if ( mp_knot_left_type(mp,h)!=mp_endpoint )
972 printf("cycle");
973 printf (";\n");
974 }
975
976 #endif
977
978 static int mplib_solve_path(lua_State * L)
979 {
980 MP *mp_ptr;
981 MP mp = NULL;
982 int cyclic;
983 const char *errormsg = NULL;
984 mp_knot p, q, first;
985 int numpoints, i;
986 p = q = first = NULL;
987 if (lua_gettop(L) != 3) {
988 errormsg = "Wrong number of arguments";
989 goto BAD;
990 }
991 mp_ptr = is_mp(L, 1);
992 if (*mp_ptr == NULL || (!lua_istable(L,2)) || lua_objlen(L,2)<=0 || (!lua_isboolean(L,3))) {
993 errormsg = "Wrong argument types";
994 goto BAD;
995 }
996 mp = *mp_ptr;
997 cyclic = lua_toboolean(L,3);
998 lua_pop(L,1);
999 /*tex We build up the path. */
1000 numpoints = lua_objlen(L,2);
1001 first = p = NULL;
1002 for (i=1;i<=numpoints;i++) {
1003 int left_set = 0, right_set = 0;
1004 double x_coord, y_coord;
1005 lua_rawgeti(L,-1,i);
1006 if (!lua_istable(L,-1)) {
1007 errormsg = "Wrong argument types";
1008 goto BAD;
1009 }
1010 mplib_push_S(x_coord);
1011 lua_rawget(L,-2);
1012 if (!lua_isnumber(L,-1)) {
1013 errormsg = "Missing X coordinate";
1014 goto BAD;
1015 }
1016 x_coord = (double)lua_tonumber(L,-1);
1017 lua_pop(L,1);
1018 mplib_push_S(y_coord);
1019 lua_rawget(L,-2);
1020 if (!lua_isnumber(L,-1)) {
1021 errormsg = "Missing y coordinate";
1022 goto BAD;
1023 }
1024 y_coord = (double)lua_tonumber(L,-1);
1025 lua_pop(L,1);
1026 q = p;
1027 if (q!=NULL) {
1028 /*tex
1029
1030 We have to save the right_tension because |mp_append_knot|
1031 trashes it, believing that it is as yet uninitialized.
1032
1033 */
1034 double saved_tension = mp_number_as_double(mp, mp_knot_right_tension(mp,p));
1035 p = mp_append_knot(mp, p, x_coord, y_coord);
1036 if ( ! p ) {
1037 errormsg = "knot creation failure";
1038 goto BAD;
1039 }
1040 (void)mp_set_knot_right_tension(mp, q, saved_tension);
1041 } else {
1042 p = mp_append_knot(mp, p, x_coord, y_coord);
1043 if ( ! p ) {
1044 errormsg = "knot creation failure";
1045 goto BAD;
1046 }
1047 }
1048 if (first == NULL)
1049 first = p;
1050 mplib_push_S(left_curl);
1051 lua_rawget(L,-2);
1052 if (lua_isnumber(L,-1)) {
1053 if (!set_left_curl (L, mp, p)) {
1054 errormsg = "failed to set left curl";
1055 goto BAD;
1056 }
1057 left_set = 1;
1058 } else {
1059 /*tex A |nil| value. */
1060 lua_pop(L,1);
1061 }
1062 mplib_push_S(left_tension);
1063 lua_rawget(L,-2);
1064 if (lua_isnumber(L,-1)) {
1065 if (left_set) {
1066 errormsg = "Left side already set";
1067 goto BAD;
1068 } else {
1069 if (!set_left_tension(L, mp, p)) {
1070 errormsg = "Failed to set left tension";
1071 goto BAD;
1072 }
1073 left_set = 1;
1074 }
1075 } else {
1076 /*tex A |nil| value. */
1077 lua_pop(L,1);
1078 }
1079 mplib_push_S(left_x);
1080 lua_rawget(L,-2);
1081 if (lua_isnumber(L,-1)) {
1082 if (left_set) {
1083 errormsg = "Left side already set";
1084 goto BAD;
1085 } else {
1086 if (!set_left_control(L, mp, p)) {
1087 errormsg = "Failed to set left control";
1088 goto BAD;
1089 }
1090 }
1091 } else {
1092 lua_pop(L,1);
1093 }
1094 mplib_push_S(right_curl);
1095 lua_rawget(L,-2);
1096 if (lua_isnumber(L,-1)) {
1097 if (!set_right_curl (L, mp, p)) {
1098 errormsg = "failed to set right curl";
1099 goto BAD;
1100 }
1101 right_set = 1;
1102 } else {
1103 /*tex A |nil| value. */
1104 lua_pop(L,1);
1105 }
1106 mplib_push_S(right_tension);
1107 lua_rawget(L,-2);
1108 if (lua_isnumber(L,-1)) {
1109 if (right_set) {
1110 errormsg = "Right side already set";
1111 goto BAD;
1112 } else {
1113 if (!set_right_tension(L, mp, p)) {
1114 errormsg = "Failed to set right tension";
1115 goto BAD;
1116 }
1117 right_set = 1;
1118 }
1119 } else {
1120 lua_pop(L,1);
1121 }
1122 mplib_push_S(right_x);
1123 lua_rawget(L,-2);
1124 if (lua_isnumber(L,-1)) {
1125 if (right_set) {
1126 errormsg = "Right side already set";
1127 goto BAD;
1128 } else {
1129 if (!set_right_control(L, mp, p)) {
1130 errormsg = "Failed to set right control";
1131 goto BAD;
1132 }
1133 }
1134 } else {
1135 lua_pop(L,1);
1136 }
1137 mplib_push_S(direction_x);
1138 lua_rawget(L,-2);
1139 if (lua_isnumber(L,-1)) {
1140 if (!set_direction (L, mp, p)) {
1141 errormsg = "failed to set direction";
1142 goto BAD;
1143 }
1144 } else {
1145 /*tex A |nil| value. */
1146 lua_pop(L,1);
1147 }
1148 /*tex Up the next item */
1149 lua_pop(L,1);
1150 }
1151 if (cyclic) {
1152 mp_close_path_cycle (mp, p, first);
1153 } else {
1154 mp_close_path (mp, p, first);
1155 }
1156 #if 0
1157 mp_dump_path(mp,first);
1158 #endif
1159 /*tex We're finished reading arguments. */
1160 if (!mp_solve_path(mp,first)) {
1161 errormsg = "Failed to solve the path";
1162 goto BAD;
1163 }
1164 /*tex Squeeze the new values back into the table. */
1165 p = first;
1166 for (i=1;i<=numpoints;i++) {
1167 lua_rawgeti(L,-1, i);
1168 mplib_push_S(left_x); lua_pushnumber(L, mp_number_as_double(mp, mp_knot_left_x(mp, p))); lua_rawset(L,-3);
1169 mplib_push_S(left_y); lua_pushnumber(L, mp_number_as_double(mp, mp_knot_left_y(mp, p))); lua_rawset(L,-3);
1170 mplib_push_S(right_x); lua_pushnumber(L, mp_number_as_double(mp, mp_knot_right_x(mp, p))); lua_rawset(L,-3);
1171 mplib_push_S(right_y); lua_pushnumber(L, mp_number_as_double(mp, mp_knot_right_y(mp, p))); lua_rawset(L,-3);
1172 /*tex This is a bit overkill \unknown */
1173 mplib_push_S(left_tension); lua_pushnil(L); lua_rawset(L,-3);
1174 mplib_push_S(right_tension); lua_pushnil(L); lua_rawset(L,-3);
1175 mplib_push_S(left_curl); lua_pushnil(L); lua_rawset(L,-3);
1176 mplib_push_S(right_curl); lua_pushnil(L); lua_rawset(L,-3);
1177 mplib_push_S(direction_x); lua_pushnil(L); lua_rawset(L,-3);
1178 mplib_push_S(direction_y); lua_pushnil(L); lua_rawset(L,-3);
1179 /*tex \unknown\ till here. */
1180 mplib_push_S(left_type); lua_pushstring(L, knot_type_enum[mp_knot_left_type(mp, p)]); lua_rawset(L, -3);
1181 mplib_push_S(right_type); lua_pushstring(L, knot_type_enum[mp_knot_right_type(mp, p)]); lua_rawset(L, -3);
1182 lua_pop(L,1);
1183 p = mp_knot_next(mp,p);
1184 }
1185 lua_pushboolean(L, 1);
1186 return 1;
1187 BAD:
1188 if (p != NULL) {
1189 mp_close_path (mp, p, first);
1190 mp_free_path (mp, p);
1191 }
1192 lua_pushboolean(L, 0);
1193 lua_pushstring(L, errormsg);
1194 return 2;
1195 }
1196
1197 /*tex
1198
1199 The next methods are for collecting the results from |fig|.
1200
1201 */
1202
1203 static int mplib_fig_collect(lua_State * L)
1204 {
1205 struct mp_edge_object **hh = is_fig(L, 1);
1206 if (*hh != NULL) {
1207 mp_gr_toss_objects(*hh);
1208 *hh = NULL;
1209 }
1210 return 0;
1211 }
1212
1213 static int mplib_fig_body(lua_State * L)
1214 {
1215 int i = 1;
1216 struct mp_graphic_object **v;
1217 struct mp_graphic_object *p;
1218 struct mp_edge_object **hh = is_fig(L, 1);
1219 lua_newtable(L);
1220 p = (*hh)->body;
1221 while (p != NULL) {
1222 v = lua_newuserdata(L, sizeof(struct mp_graphic_object *));
1223 *v = p;
1224 luaL_getmetatable(L, MPLIB_GR_METATABLE);
1225 lua_setmetatable(L, -2);
1226 lua_rawseti(L, -2, i);
1227 i++;
1228 p = p->next;
1229 }
1230 /*tex Prevent a double free: */
1231 (*hh)->body = NULL;
1232 return 1;
1233 }
1234
1235 static int mplib_fig_copy_body(lua_State * L)
1236 {
1237 int i = 1;
1238 struct mp_graphic_object **v;
1239 struct mp_graphic_object *p;
1240 struct mp_edge_object **hh = is_fig(L, 1);
1241 lua_newtable(L);
1242 p = (*hh)->body;
1243 while (p != NULL) {
1244 v = lua_newuserdata(L, sizeof(struct mp_graphic_object *));
1245 *v = mp_gr_copy_object((*hh)->parent, p);
1246 luaL_getmetatable(L, MPLIB_GR_METATABLE);
1247 lua_setmetatable(L, -2);
1248 lua_rawseti(L, -2, i);
1249 i++;
1250 p = p->next;
1251 }
1252 return 1;
1253 }
1254
1255 static int mplib_fig_tostring(lua_State * L)
1256 {
1257 struct mp_edge_object **hh = is_fig(L, 1);
1258 (void)lua_pushfstring(L, "<figure %p>", *hh);
1259 return 1;
1260 }
1261
1262 static int mplib_fig_postscript(lua_State * L)
1263 {
1264 mp_run_data *res;
1265 struct mp_edge_object **hh = is_fig(L, 1);
1266 int prologues = (int)luaL_optnumber(L, 2, (lua_Number)-1);
1267 int procset = (int)luaL_optnumber(L, 3, (lua_Number)-1);
1268 if (mp_ps_ship_out(*hh, prologues, procset)
1269 && (res = mp_rundata((*hh)->parent))
1270 && (res->ship_out.size != 0)) {
1271 lua_pushstring(L, res->ship_out.data);
1272 } else {
1273 lua_pushnil(L);
1274 }
1275 return 1;
1276 }
1277
1278 static int mplib_fig_svg(lua_State * L)
1279 {
1280 mp_run_data *res;
1281 struct mp_edge_object **hh = is_fig(L, 1);
1282 int prologues = (int)luaL_optnumber(L, 2, (lua_Number)-1);
1283 if (mp_svg_ship_out(*hh, prologues)
1284 && (res = mp_rundata((*hh)->parent))
1285 && (res->ship_out.size != 0)) {
1286 lua_pushstring(L, res->ship_out.data);
1287 } else {
1288 lua_pushnil(L);
1289 }
1290 return 1;
1291 }
1292
1293 static int mplib_fig_png(lua_State * L)
1294 {
1295 mp_run_data *res;
1296 struct mp_edge_object **hh = is_fig(L, 1);
1297 const char *string = luaL_optstring(L, 2, NULL);
1298 if (mp_png_ship_out(*hh, string)
1299 && (res = mp_rundata((*hh)->parent))
1300 && (res->ship_out.size != 0)) {
1301 lua_pushlstring(L, res->ship_out.data, res->ship_out.size);
1302 } else {
1303 lua_pushnil(L);
1304 }
1305 return 1;
1306 }
1307
1308 static int mplib_fig_filename(lua_State * L)
1309 {
1310 struct mp_edge_object **hh = is_fig(L, 1);
1311 if (*hh != NULL) {
1312 char *s = (*hh)->filename;
1313 lua_pushstring(L, s);
1314 } else {
1315 lua_pushnil(L);
1316 }
1317 return 1;
1318 }
1319
1320 static int mplib_fig_width(lua_State * L)
1321 {
1322 struct mp_edge_object **hh = is_fig(L, 1);
1323 if (*hh != NULL) {
1324 lua_pushnumber(L, (double) (*hh)->width);
1325 } else {
1326 lua_pushnil(L);
1327 }
1328 return 1;
1329 }
1330
1331 static int mplib_fig_height(lua_State * L)
1332 {
1333 struct mp_edge_object **hh = is_fig(L, 1);
1334 if (*hh != NULL) {
1335 lua_pushnumber(L, (double) (*hh)->height);
1336 } else {
1337 lua_pushnil(L);
1338 }
1339 return 1;
1340 }
1341
1342 static int mplib_fig_depth(lua_State * L)
1343 {
1344 struct mp_edge_object **hh = is_fig(L, 1);
1345 if (*hh != NULL) {
1346 lua_pushnumber(L, (double) (*hh)->depth);
1347 } else {
1348 lua_pushnil(L);
1349 }
1350 return 1;
1351 }
1352
1353 static int mplib_fig_italcorr(lua_State * L)
1354 {
1355 struct mp_edge_object **hh = is_fig(L, 1);
1356 if (*hh != NULL) {
1357 lua_pushnumber(L, (double) (*hh)->ital_corr);
1358 } else {
1359 lua_pushnil(L);
1360 }
1361 return 1;
1362 }
1363
1364 static int mplib_fig_charcode(lua_State * L)
1365 {
1366 struct mp_edge_object **hh = is_fig(L, 1);
1367 if (*hh != NULL) {
1368 lua_pushnumber(L, (lua_Number)(*hh)->charcode);
1369 } else {
1370 lua_pushnil(L);
1371 }
1372 return 1;
1373 }
1374
1375 static int mplib_fig_bb(lua_State * L)
1376 {
1377 struct mp_edge_object **hh = is_fig(L, 1);
1378 lua_newtable(L);
1379 lua_pushnumber(L, (double) (*hh)->minx);
1380 lua_rawseti(L, -2, 1);
1381 lua_pushnumber(L, (double) (*hh)->miny);
1382 lua_rawseti(L, -2, 2);
1383 lua_pushnumber(L, (double) (*hh)->maxx);
1384 lua_rawseti(L, -2, 3);
1385 lua_pushnumber(L, (double) (*hh)->maxy);
1386 lua_rawseti(L, -2, 4);
1387 return 1;
1388 }
1389
1390 /*tex
1391
1392 The methods for the figure objects plus a few helpers.
1393
1394 */
1395
1396 static int mplib_gr_collect(lua_State * L)
1397 {
1398 struct mp_graphic_object **hh = is_gr_object(L, 1);
1399 if (*hh != NULL) {
1400 mp_gr_toss_object(*hh);
1401 *hh = NULL;
1402 }
1403 return 0;
1404 }
1405
1406 static int mplib_gr_tostring(lua_State * L)
1407 {
1408 struct mp_graphic_object **hh = is_gr_object(L, 1);
1409 (void)lua_pushfstring(L, "<object %p>", *hh);
1410 return 1;
1411 }
1412
1413 #define pyth(a,b) (sqrt((a)*(a) + (b)*(b)))
1414
1415 #define aspect_bound (10.0/65536.0)
1416 #define aspect_default 1.0
1417
1418 static double eps = 0.0001;
1419
1420 static double coord_range_x (mp_gr_knot h, double dz) {
1421 double z;
1422 double zlo = 0.0, zhi = 0.0;
1423 mp_gr_knot f = h;
1424 while (h != NULL) {
1425 z = h->x_coord;
1426 if (z < zlo)
1427 zlo = z;
1428 else if (z > zhi)
1429 zhi = z;
1430 z = h->right_x;
1431 if (z < zlo)
1432 zlo = z;
1433 else if (z > zhi)
1434 zhi = z;
1435 z = h->left_x;
1436 if (z < zlo)
1437 zlo = z;
1438 else if (z > zhi)
1439 zhi = z;
1440 h = h->next;
1441 if (h==f)
1442 break;
1443 }
1444 return (zhi - zlo <= dz ? aspect_bound : aspect_default);
1445 }
1446
1447 static double coord_range_y (mp_gr_knot h, double dz) {
1448 double z;
1449 double zlo = 0.0, zhi = 0.0;
1450 mp_gr_knot f = h;
1451 while (h != NULL) {
1452 z = h->y_coord;
1453 if (z < zlo)
1454 zlo = z;
1455 else if (z > zhi)
1456 zhi = z;
1457 z = h->right_y;
1458 if (z < zlo)
1459 zlo = z;
1460 else if (z > zhi)
1461 zhi = z;
1462 z = h->left_y;
1463 if (z < zlo)
1464 zlo = z;
1465 else if (z > zhi)
1466 zhi = z;
1467 h = h->next;
1468 if (h==f)
1469 break;
1470 }
1471 return (zhi - zlo <= dz ? aspect_bound : aspect_default);
1472 }
1473
1474 static int mplib_gr_peninfo(lua_State * L) {
1475 double x_coord, y_coord, left_x, left_y, right_x, right_y;
1476 double wx, wy;
1477 double rx = 1.0, sx = 0.0, sy = 0.0, ry = 1.0, tx = 0.0, ty = 0.0;
1478 double width = 1.0;
1479 mp_gr_knot p = NULL, path = NULL;
1480 struct mp_graphic_object **hh = is_gr_object(L, -1);
1481 if (!*hh) {
1482 lua_pushnil(L);
1483 return 1;
1484 }
1485 if ((*hh)->type == mp_fill_code) {
1486 p = ((mp_fill_object *)(*hh))->pen_p;
1487 path = ((mp_fill_object *)(*hh))->path_p;
1488 } else if ((*hh)->type == mp_stroked_code) {
1489 p = ((mp_stroked_object *)(*hh))->pen_p;
1490 path = ((mp_stroked_object *)(*hh))->path_p;
1491 }
1492 if (p==NULL || path == NULL) {
1493 lua_pushnil(L);
1494 return 1;
1495 }
1496 x_coord = p->x_coord;
1497 y_coord = p->y_coord;
1498 left_x = p->left_x;
1499 left_y = p->left_y;
1500 right_x = p->right_x;
1501 right_y = p->right_y;
1502 if ((right_x == x_coord) && (left_y == y_coord)) {
1503 wx = fabs(left_x - x_coord);
1504 wy = fabs(right_y - y_coord);
1505 } else {
1506 wx = pyth(left_x - x_coord, right_x - x_coord);
1507 wy = pyth(left_y - y_coord, right_y - y_coord);
1508 }
1509 if ((wy/coord_range_x(path, wx)) >= (wx/coord_range_y(path, wy)))
1510 width = wy;
1511 else
1512 width = wx;
1513 tx = x_coord;
1514 ty = y_coord;
1515 sx = left_x - tx;
1516 rx = left_y - ty;
1517 ry = right_x - tx;
1518 sy = right_y - ty;
1519 if (width !=1.0) {
1520 if (width == 0.0) {
1521 sx = 1.0; sy = 1.0;
1522 } else {
1523 rx/=width; ry/=width; sx/=width; sy/=width;
1524 }
1525 }
1526 if (fabs(sx) < eps)
1527 sx = eps;
1528 if (fabs(sy) < eps)
1529 sy = eps;
1530 lua_newtable(L);
1531 lua_pushnumber(L,width); lua_setfield(L,-2,"width");
1532 lua_pushnumber(L,rx); lua_setfield(L,-2,"rx");
1533 lua_pushnumber(L,sx); lua_setfield(L,-2,"sx");
1534 lua_pushnumber(L,sy); lua_setfield(L,-2,"sy");
1535 lua_pushnumber(L,ry); lua_setfield(L,-2,"ry");
1536 lua_pushnumber(L,tx); lua_setfield(L,-2,"tx");
1537 lua_pushnumber(L,ty); lua_setfield(L,-2,"ty");
1538 return 1;
1539 }
1540
1541 /*tex
1542
1543 Here is a helper that reports the valid field names of the possible
1544 objects.
1545
1546 */
1547
1548 static int mplib_gr_fields(lua_State * L)
1549 {
1550 const char **fields;
1551 int i;
1552 struct mp_graphic_object **hh = is_gr_object(L, 1);
1553 if (*hh) {
1554 switch ((*hh)->type) {
1555 case mp_fill_code:
1556 fields = fill_fields;
1557 break;
1558 case mp_stroked_code:
1559 fields = stroked_fields;
1560 break;
1561 case mp_text_code:
1562 fields = text_fields;
1563 break;
1564 case mp_special_code:
1565 fields = special_fields;
1566 break;
1567 case mp_start_clip_code:
1568 fields = start_clip_fields;
1569 break;
1570 case mp_start_bounds_code:
1571 fields = start_bounds_fields;
1572 break;
1573 case mp_stop_clip_code:
1574 fields = stop_clip_fields;
1575 break;
1576 case mp_stop_bounds_code:
1577 fields = stop_bounds_fields;
1578 break;
1579 default:
1580 fields = no_fields;
1581 }
1582 lua_newtable(L);
1583 for (i = 0; fields[i] != NULL; i++) {
1584 lua_pushstring(L, fields[i]);
1585 lua_rawseti(L, -2, (i + 1));
1586 }
1587 } else {
1588 lua_pushnil(L);
1589 }
1590 return 1;
1591 }
1592
1593 #define mplib_push_number(L,x) lua_pushnumber(L,(lua_Number)(x))
1594
1595 #define MPLIB_PATH 0
1596 #define MPLIB_PEN 1
1597
1598 static void mplib_push_path(lua_State * L, mp_gr_knot h, int is_pen)
1599 {
1600 mp_gr_knot p;
1601 int i = 1;
1602 p = h;
1603 if (p != NULL) {
1604 lua_newtable(L);
1605 do {
1606 lua_createtable(L, 0, 6);
1607 if (!is_pen) {
1608 if (p->data.types.left_type != mp_explicit) {
1609 mplib_push_S(left_type);
1610 lua_pushstring(L, knot_type_enum[p->data.types.left_type]);
1611 lua_rawset(L, -3);
1612 }
1613 if (p->data.types.right_type != mp_explicit) {
1614 mplib_push_S(right_type);
1615 lua_pushstring(L, knot_type_enum[p->data.types.right_type]);
1616 lua_rawset(L, -3);
1617 }
1618 }
1619 mplib_push_S(x_coord);
1620 mplib_push_number(L, p->x_coord);
1621 lua_rawset(L, -3);
1622 mplib_push_S(y_coord);
1623 mplib_push_number(L, p->y_coord);
1624 lua_rawset(L, -3);
1625 mplib_push_S(left_x);
1626 mplib_push_number(L, p->left_x);
1627 lua_rawset(L, -3);
1628 mplib_push_S(left_y);
1629 mplib_push_number(L, p->left_y);
1630 lua_rawset(L, -3);
1631 mplib_push_S(right_x);
1632 mplib_push_number(L, p->right_x);
1633 lua_rawset(L, -3);
1634 mplib_push_S(right_y);
1635 mplib_push_number(L, p->right_y);
1636 lua_rawset(L, -3);
1637 lua_rawseti(L, -2, i);
1638 i++;
1639 if (p->data.types.right_type == mp_endpoint) {
1640 return;
1641 }
1642 p = p->next;
1643 } while (p != h);
1644 } else {
1645 lua_pushnil(L);
1646 }
1647 }
1648
1649 static int mplib_get_path(lua_State * L)
1650 {
1651 MP *mp = is_mp(L, 1);
1652 if (*mp != NULL) {
1653 size_t l;
1654 const char *s = lua_tolstring(L, 2, &l);
1655 if (s != NULL) {
1656 mp_knot p = mp_get_path_value(*mp,s,l) ;
1657 if (p != NULL) {
1658 int i = 1;
1659 mp_knot h = p;
1660 lua_newtable(L);
1661 do {
1662 lua_createtable(L, 6, 1);
1663 mplib_push_number(L, mp_number_as_double(*mp,p->x_coord));
1664 lua_rawseti(L,-2,1);
1665 mplib_push_number(L, mp_number_as_double(*mp,p->y_coord));
1666 lua_rawseti(L,-2,2);
1667 mplib_push_number(L, mp_number_as_double(*mp,p->left_x));
1668 lua_rawseti(L,-2,3);
1669 mplib_push_number(L, mp_number_as_double(*mp,p->left_y));
1670 lua_rawseti(L,-2,4);
1671 mplib_push_number(L, mp_number_as_double(*mp,p->right_x));
1672 lua_rawseti(L,-2,5);
1673 mplib_push_number(L, mp_number_as_double(*mp,p->right_y));
1674 lua_rawseti(L,-2,6);
1675 lua_rawseti(L,-2, i);
1676 i++;
1677 if (p->data.types.right_type == mp_endpoint) {
1678 mplib_push_S(cycle);
1679 lua_pushboolean(L,0);
1680 lua_rawset(L,-3);
1681 return 1;
1682 }
1683 p = p->next;
1684 } while (p != h);
1685 mplib_push_S(cycle);
1686 lua_pushboolean(L,1);
1687 lua_rawset(L,-3);
1688 return 1;
1689 }
1690 }
1691 }
1692 return 0;
1693 }
1694
1695 /*tex
1696
1697 This assumes that the top of the stack is a table or nil already in the case.
1698 */
1699
1700 static void mplib_push_pentype(lua_State * L, mp_gr_knot h)
1701 {
1702 mp_gr_knot p;
1703 p = h;
1704 if (p == NULL) {
1705 /*tex Do nothing. */
1706 } else if (p == p->next) {
1707 mplib_push_S(type);
1708 mplib_push_S(elliptical);
1709 lua_rawset(L, -3);
1710 } else {
1711 }
1712 }
1713
1714 #define set_color_objects(pq) \
1715 object_color_model = pq->color_model; \
1716 object_color_a = pq->color.a_val; \
1717 object_color_b = pq->color.b_val; \
1718 object_color_c = pq->color.c_val; \
1719 object_color_d = pq->color.d_val;
1720
1721 static void mplib_push_color(lua_State * L, struct mp_graphic_object *p)
1722 {
1723 int object_color_model;
1724 double object_color_a, object_color_b, object_color_c, object_color_d;
1725 if (p != NULL) {
1726 if (p->type == mp_fill_code) {
1727 mp_fill_object *h = (mp_fill_object *) p;
1728 set_color_objects(h);
1729 } else if (p->type == mp_stroked_code) {
1730 mp_stroked_object *h = (mp_stroked_object *) p;
1731 set_color_objects(h);
1732 } else {
1733 mp_text_object *h = (mp_text_object *) p;
1734 set_color_objects(h);
1735 }
1736 lua_newtable(L);
1737 if (object_color_model >= mp_grey_model) {
1738 mplib_push_number(L, object_color_a);
1739 lua_rawseti(L, -2, 1);
1740 if (object_color_model >= mp_rgb_model) {
1741 mplib_push_number(L, object_color_b);
1742 lua_rawseti(L, -2, 2);
1743 mplib_push_number(L, object_color_c);
1744 lua_rawseti(L, -2, 3);
1745 if (object_color_model == mp_cmyk_model) {
1746 mplib_push_number(L, object_color_d);
1747 lua_rawseti(L, -2, 4);
1748 }
1749 }
1750 }
1751 } else {
1752 lua_pushnil(L);
1753 }
1754 }
1755
1756 /*tex
1757
1758 The dash scale is not exported, the field has no external value.
1759
1760 */
1761
1762 static void mplib_push_dash(lua_State * L, struct mp_stroked_object *h)
1763 {
1764 mp_dash_object *d;
1765 double ds;
1766 if (h != NULL && h->dash_p != NULL) {
1767 d = h->dash_p;
1768 lua_newtable(L);
1769 mplib_push_S(offset);
1770 mplib_push_number(L, d->offset);
1771 lua_rawset(L,-3);
1772 if (d->array != NULL) {
1773 int i = 0;
1774 mplib_push_S(dashes);
1775 lua_newtable(L);
1776 while (*(d->array + i) != -1) {
1777 ds = *(d->array + i);
1778 lua_pushnumber(L, ds);
1779 i++;
1780 lua_rawseti(L, -2, i);
1781 }
1782 lua_rawset(L,-3);
1783 }
1784 } else {
1785 lua_pushnil(L);
1786 }
1787 }
1788
1789 static void mplib_push_transform(lua_State * L, struct mp_text_object *h)
1790 {
1791 int i = 1;
1792 if (h != NULL) {
1793 lua_createtable(L, 6, 0);
1794 mplib_push_number(L, h->tx);
1795 lua_rawseti(L, -2, i);
1796 i++;
1797 mplib_push_number(L, h->ty);
1798 lua_rawseti(L, -2, i);
1799 i++;
1800 mplib_push_number(L, h->txx);
1801 lua_rawseti(L, -2, i);
1802 i++;
1803 mplib_push_number(L, h->tyx);
1804 lua_rawseti(L, -2, i);
1805 i++;
1806 mplib_push_number(L, h->txy);
1807 lua_rawseti(L, -2, i);
1808 i++;
1809 mplib_push_number(L, h->tyy);
1810 lua_rawseti(L, -2, i);
1811 i++;
1812 } else {
1813 lua_pushnil(L);
1814 }
1815 }
1816
1817 #define FIELD(A) (mplib_is_S(A,2))
1818
1819 static void mplib_fill(lua_State * L, struct mp_fill_object *h)
1820 {
1821 if (FIELD(path)) {
1822 mplib_push_path(L, h->path_p, MPLIB_PATH);
1823 } else if (FIELD(htap)) {
1824 mplib_push_path(L, h->htap_p, MPLIB_PATH);
1825 } else if (FIELD(pen)) {
1826 mplib_push_path(L, h->pen_p, MPLIB_PEN);
1827 mplib_push_pentype(L, h->pen_p);
1828 } else if (FIELD(color)) {
1829 mplib_push_color(L, (mp_graphic_object *) h);
1830 } else if (FIELD(linejoin)) {
1831 lua_pushnumber(L, (lua_Number)h->ljoin);
1832 } else if (FIELD(miterlimit)) {
1833 mplib_push_number(L, h->miterlim);
1834 } else if (FIELD(prescript)) {
1835 lua_pushstring(L, h->pre_script);
1836 } else if (FIELD(postscript)) {
1837 lua_pushstring(L, h->post_script);
1838 } else {
1839 lua_pushnil(L);
1840 }
1841 }
1842
1843 static void mplib_stroked(lua_State * L, struct mp_stroked_object *h)
1844 {
1845 if (FIELD(path)) {
1846 mplib_push_path(L, h->path_p, MPLIB_PATH);
1847 } else if (FIELD(pen)) {
1848 mplib_push_path(L, h->pen_p, MPLIB_PEN);
1849 mplib_push_pentype(L, h->pen_p);
1850 } else if (FIELD(color)) {
1851 mplib_push_color(L, (mp_graphic_object *) h);
1852 } else if (FIELD(dash)) {
1853 mplib_push_dash(L, h);
1854 } else if (FIELD(linecap)) {
1855 lua_pushnumber(L, (lua_Number)h->lcap);
1856 } else if (FIELD(linejoin)) {
1857 lua_pushnumber(L, (lua_Number)h->ljoin);
1858 } else if (FIELD(miterlimit)) {
1859 mplib_push_number(L, h->miterlim);
1860 } else if (FIELD(prescript)) {
1861 lua_pushstring(L, h->pre_script);
1862 } else if (FIELD(postscript)) {
1863 lua_pushstring(L, h->post_script);
1864 } else {
1865 lua_pushnil(L);
1866 }
1867 }
1868
1869 static void mplib_text(lua_State * L, struct mp_text_object *h)
1870 {
1871 if (FIELD(text)) {
1872 lua_pushlstring(L, h->text_p, h->text_l);
1873 } else if (FIELD(dsize)) {
1874 mplib_push_number(L, (h->font_dsize / 16));
1875 } else if (FIELD(font)) {
1876 lua_pushstring(L, h->font_name);
1877 } else if (FIELD(color)) {
1878 mplib_push_color(L, (mp_graphic_object *) h);
1879 } else if (FIELD(width)) {
1880 mplib_push_number(L, h->width);
1881 } else if (FIELD(height)) {
1882 mplib_push_number(L, h->height);
1883 } else if (FIELD(depth)) {
1884 mplib_push_number(L, h->depth);
1885 } else if (FIELD(transform)) {
1886 mplib_push_transform(L, h);
1887 } else if (FIELD(prescript)) {
1888 lua_pushstring(L, h->pre_script);
1889 } else if (FIELD(postscript)) {
1890 lua_pushstring(L, h->post_script);
1891 } else {
1892 lua_pushnil(L);
1893 }
1894 }
1895
1896 static void mplib_special(lua_State * L, struct mp_special_object *h)
1897 {
1898 if (FIELD(prescript)) {
1899 lua_pushstring(L, h->pre_script);
1900 } else {
1901 lua_pushnil(L);
1902 }
1903 }
1904
1905 static void mplib_start_bounds(lua_State * L, struct mp_bounds_object *h)
1906 {
1907 if (FIELD(path)) {
1908 mplib_push_path(L, h->path_p, MPLIB_PATH);
1909 } else {
1910 lua_pushnil(L);
1911 }
1912 }
1913
1914 static void mplib_start_clip(lua_State * L, struct mp_clip_object *h)
1915 {
1916 if (FIELD(path)) {
1917 mplib_push_path(L, h->path_p, MPLIB_PATH);
1918 } else {
1919 lua_pushnil(L);
1920 }
1921 }
1922
1923 static int mplib_gr_index(lua_State * L)
1924 {
1925 struct mp_graphic_object **hh = is_gr_object(L, 1);
1926 if (*hh) {
1927 struct mp_graphic_object *h = *hh;
1928 if (mplib_is_S(type, 2)) {
1929 lua_rawgeti(L, LUA_REGISTRYINDEX, mplib_type_Ses[h->type]);
1930 } else {
1931 switch (h->type) {
1932 case mp_fill_code:
1933 mplib_fill(L, (mp_fill_object *) h);
1934 break;
1935 case mp_stroked_code:
1936 mplib_stroked(L, (mp_stroked_object *) h);
1937 break;
1938 case mp_text_code:
1939 mplib_text(L, (mp_text_object *) h);
1940 break;
1941 case mp_special_code:
1942 mplib_special(L, (mp_special_object *) h);
1943 break;
1944 case mp_start_clip_code:
1945 mplib_start_clip(L, (mp_clip_object *) h);
1946 break;
1947 case mp_start_bounds_code:
1948 mplib_start_bounds(L, (mp_bounds_object *) h);
1949 break;
1950 case mp_stop_clip_code:
1951 case mp_stop_bounds_code:
1952 default:
1953 lua_pushnil(L);
1954 }
1955 }
1956 } else {
1957 lua_pushnil(L);
1958 }
1959 return 1;
1960 }
1961
1962 static const struct luaL_reg mplib_meta[] = {
1963 { "__gc", mplib_collect },
1964 { "__tostring", mplib_tostring },
1965 /*tex sentinel */
1966 { NULL, NULL}
1967 };
1968
1969 static const struct luaL_reg mplib_fig_meta[] = {
1970 { "__gc", mplib_fig_collect },
1971 { "__tostring", mplib_fig_tostring },
1972 { "objects", mplib_fig_body },
1973 { "copy_objects", mplib_fig_copy_body },
1974 { "filename", mplib_fig_filename },
1975 { "postscript", mplib_fig_postscript },
1976 { "png", mplib_fig_png },
1977 { "svg", mplib_fig_svg },
1978 { "boundingbox", mplib_fig_bb },
1979 { "width", mplib_fig_width },
1980 { "height", mplib_fig_height },
1981 { "depth", mplib_fig_depth },
1982 { "italcorr", mplib_fig_italcorr },
1983 { "charcode", mplib_fig_charcode },
1984 /*tex sentinel */
1985 { NULL, NULL}
1986 };
1987
1988 static const struct luaL_reg mplib_gr_meta[] = {
1989 { "__gc", mplib_gr_collect},
1990 { "__tostring", mplib_gr_tostring},
1991 { "__index", mplib_gr_index},
1992 /*tex sentinel */
1993 { NULL, NULL}
1994 };
1995
1996 static const struct luaL_reg mplib_d[] = {
1997 { "execute", mplib_execute },
1998 { "finish", mplib_finish },
1999 { "char_width", mplib_charwidth },
2000 { "char_height", mplib_charheight },
2001 { "char_depth", mplib_chardepth },
2002 { "statistics", mplib_statistics },
2003 { "solve_path", mplib_solve_path },
2004 { "get_numeric", mplib_get_numeric },
2005 { "get_number", mplib_get_numeric },
2006 { "get_boolean", mplib_get_boolean },
2007 { "get_string", mplib_get_string },
2008 { "get_path", mplib_get_path },
2009 /*tex sentinel */
2010 {NULL, NULL }
2011 };
2012
2013 static const struct luaL_reg mplib_m[] = {
2014 { "new", mplib_new },
2015 { "version", mplib_version },
2016 { "fields", mplib_gr_fields },
2017 /* indirect */
2018 { "execute", mplib_execute },
2019 { "finish", mplib_finish },
2020 { "char_width", mplib_charwidth },
2021 { "char_height", mplib_charheight },
2022 { "char_depth", mplib_chardepth },
2023 { "statistics", mplib_statistics },
2024 { "solve_path", mplib_solve_path },
2025 /* helpers */
2026 { "pen_info", mplib_gr_peninfo },
2027 { "get_numeric", mplib_get_numeric },
2028 { "get_number", mplib_get_numeric },
2029 { "get_boolean", mplib_get_boolean },
2030 { "get_string", mplib_get_string },
2031 { "get_path", mplib_get_path },
2032 /*tex sentinel */
2033 { NULL, NULL}
2034 };
2035
2036 int luaopen_mplib(lua_State * L)
2037 {
2038 mplib_init_Ses(L);
2039
2040 luaL_newmetatable(L, MPLIB_GR_METATABLE);
2041 lua_pushvalue(L, -1);
2042 lua_setfield(L, -2, "__index");
2043 luaL_register(L, NULL, mplib_gr_meta);
2044 lua_pop(L, 1);
2045
2046 luaL_newmetatable(L, MPLIB_FIG_METATABLE);
2047 lua_pushvalue(L, -1);
2048 lua_setfield(L, -2, "__index");
2049 luaL_register(L, NULL, mplib_fig_meta);
2050 lua_pop(L, 1);
2051
2052 luaL_newmetatable(L, MPLIB_METATABLE);
2053 lua_pushvalue(L, -1);
2054 lua_setfield(L, -2, "__index");
2055 luaL_register(L, NULL, mplib_meta);
2056 luaL_register(L, NULL, mplib_d);
2057 luaL_register(L, "mplib", mplib_m);
2058
2059 return 1;
2060 }