12 #include <../lua51/lua.h>
13 #include <../lua51/lauxlib.h>
14 #include <../lua51/lualib.h>
19 #include "mppsout.h" /* for mp_edge_object */
21 #define MPLIB_METATABLE "MPlib"
22 #define MPLIB_FIG_METATABLE "MPlib.fig"
23 #define MPLIB_GR_METATABLE "MPlib.gr"
25 #define xfree(A) if ((A)!=NULL) { free((A)); A = NULL; }
27 #define is_mp(L,b) (MP *)luaL_checkudata(L,b,MPLIB_METATABLE)
28 #define is_fig(L,b) (struct mp_edge_object **)luaL_checkudata(L,b,MPLIB_FIG_METATABLE)
29 #define is_gr_object(L,b) (struct mp_graphic_object **)luaL_checkudata(L,b,MPLIB_GR_METATABLE)
31 /* Enumeration string arrays */
33 static const char *interaction_options[] =
34 { "unknown","batch","nonstop","scroll","errorstop", NULL};
36 static const char *mplib_filetype_names[] =
37 {"term", "error", "mp", "log", "ps", "mem", "tfm", "map", "pfb", "enc", NULL};
39 /* only "endpoint" and "explicit" actually happen in paths,
40 as well as "open" in elliptical pens */
42 static const char *knot_type_enum[] =
43 { "endpoint", "explicit", "given", "curl", "open", "end_cycle" };
45 static const char *knot_originator_enum[] =
46 { "program" ,"user" };
49 /* this looks a bit funny because of the holes */
51 static const char *color_model_enum[] =
52 { NULL, "none", NULL, "grey", NULL, "rgb", NULL, "cmyk", NULL, "uninitialized" };
56 #define FIELD(A) (strcmp(field,#A)==0)
58 static const char *fill_fields[] =
59 { "type", "path", "htap", "pen", "color", "linejoin", "miterlimit",
60 "prescript", "postscript", NULL };
62 static const char *stroked_fields[] =
63 { "type", "path", "pen", "color", "linejoin", "miterlimit", "linecap", "dash",
64 "prescript", "postscript", NULL };
66 static const char *text_fields[] =
67 { "type", "text", "dsize", "font", "color", "width", "height", "depth", "transform",
68 "prescript", "postscript", NULL };
70 static const char *special_fields[] =
71 { "type", "prescript", NULL };
73 static const char *start_bounds_fields[] =
74 { "type", "path", NULL };
76 static const char *start_clip_fields[] =
77 { "type", "path", NULL };
79 static const char *stop_bounds_fields[] =
82 static const char *stop_clip_fields[] =
85 static const char *no_fields[] =
89 P_ERROR_LINE, P_HALF_LINE, P_MAX_LINE, P_MAIN_MEMORY,
90 P_HASH_SIZE, P_HASH_PRIME, P_PARAM_SIZE, P_IN_OPEN, P_RANDOM_SEED,
91 P_INTERACTION, P_INI_VERSION, P_TROFF_MODE, P_PRINT_NAMES, P_MEM_NAME,
92 P_JOB_NAME, P_FIND_FILE, P__SENTINEL,
96 const char *name; /* parameter name */
97 mplib_parm_idx idx; /* parameter index */
100 static mplib_parm_struct mplib_parms[] = {
101 {"error_line", P_ERROR_LINE },
102 {"half_error_line", P_HALF_LINE },
103 {"max_print_line", P_MAX_LINE },
104 {"main_memory", P_MAIN_MEMORY },
105 {"hash_size", P_HASH_SIZE },
106 {"hash_prime", P_HASH_PRIME },
107 {"param_size", P_PARAM_SIZE },
108 {"max_in_open", P_IN_OPEN },
109 {"random_seed", P_RANDOM_SEED },
110 {"interaction", P_INTERACTION },
111 {"ini_version", P_INI_VERSION },
112 {"troff_mode", P_TROFF_MODE },
113 {"print_found_names", P_PRINT_NAMES },
114 {"mem_name", P_MEM_NAME },
115 {"job_name", P_JOB_NAME },
116 {"find_file", P_FIND_FILE },
120 typedef struct _FILE_ITEM {
124 typedef struct _FILE_ITEM File;
126 typedef struct _MPLIB_INSTANCE_DATA {
136 char *input_data_ptr;
137 size_t input_data_len;
138 struct mp_edge_object *edges ;
140 } _MPLIB_INSTANCE_DATA;
142 typedef struct _MPLIB_INSTANCE_DATA mplib_instance;
144 static mplib_instance *mplib_get_data (MP mp) {
145 return (mplib_instance *)mp->userdata;
148 static mplib_instance *mplib_make_data (void) {
149 mplib_instance *mplib_data = malloc(sizeof(mplib_instance));
150 memset(mplib_data,0,sizeof(mplib_instance));
155 /* Start by defining all the callback routines for the library
156 * except |run_make_mpx| and |run_editor|.
160 char *mplib_find_file (MP mp, char *fname, char *fmode, int ftype) {
161 mplib_instance *mplib_data = mplib_get_data(mp);
162 lua_State *L = mplib_data->LL;
164 lua_getfield(L,LUA_REGISTRYINDEX,"mplib_file_finder");
165 if (lua_isfunction(L,-1)) {
166 char *s = NULL, *x = NULL;
167 lua_pushstring(L, fname);
168 lua_pushstring(L, fmode);
169 if (ftype >= mp_filetype_text) {
170 lua_pushnumber(L, ftype-mp_filetype_text);
172 lua_pushstring(L, mplib_filetype_names[ftype]);
174 if(lua_pcall(L,3,1,0) != 0) {
175 fprintf(stdout,"Error in mp.find_file: %s\n", (char *)lua_tostring(L,-1));
178 x = (char *)lua_tostring(L,-1);
181 lua_pop(L,1); /* pop the string */
186 if (fmode[0] != 'r' || (! access (fname,R_OK)) || ftype) {
187 return strdup(fname);
193 mplib_find_file_function (lua_State *L) {
194 if (! (lua_isfunction(L,-1)|| lua_isnil(L,-1) )) {
195 return 1; /* error */
197 lua_pushstring(L, "mplib_file_finder");
199 lua_rawset(L,LUA_REGISTRYINDEX);
203 void *mplib_open_file(MP mp, char *fname, char *fmode, int ftype) {
204 mplib_instance *mplib_data = mplib_get_data(mp);
205 File *ff = malloc(sizeof (File));
208 if (ftype==mp_filetype_terminal) {
209 if (fmode[0] == 'r') {
212 xfree(mplib_data->term_file_ptr);
214 mplib_data->term_file_ptr = ff->f;
216 } else if (ftype==mp_filetype_error) {
217 xfree(mplib_data->err_file_ptr);
219 mplib_data->err_file_ptr = ff->f;
220 } else if (ftype == mp_filetype_log) {
221 xfree(mplib_data->log_file_ptr);
223 mplib_data->log_file_ptr = ff->f;
224 } else if (ftype == mp_filetype_postscript) {
225 xfree(mplib_data->ps_file_ptr);
227 mplib_data->ps_file_ptr = ff->f;
230 if (fmode[0] == 'r') {
231 f = mplib_find_file(mp, fname,fmode,ftype);
235 ff->f = fopen(f, fmode);
236 if ((fmode[0] == 'r') && (ff->f == NULL)) {
247 mplib_get_char (void *f, mplib_instance *mplib_data) {
249 if (f==stdin && mplib_data->input_data != NULL) {
250 if (mplib_data->input_data_len==0) {
251 if (mplib_data->input_data_ptr!=NULL)
252 mplib_data->input_data_ptr = NULL;
254 mplib_data->input_data = NULL;
257 mplib_data->input_data_len--;
258 c = *(mplib_data->input_data_ptr)++;
267 mplib_unget_char (void *f, mplib_instance *mplib_data, int c) {
268 if (f==stdin && mplib_data->input_data_ptr != NULL) {
269 mplib_data->input_data_len++;
270 mplib_data->input_data_ptr--;
277 char *mplib_read_ascii_file (MP mp, void *ff, size_t *size) {
279 size_t len = 0, lim = 128;
281 mplib_instance *mplib_data = mplib_get_data(mp);
283 FILE *f = ((File *)ff)->f;
287 c = mplib_get_char(f,mplib_data);
291 if (s==NULL) return NULL;
292 while (c!=EOF && c!='\n' && c!='\r') {
294 s =realloc(s, (lim+(lim>>2)));
295 if (s==NULL) return NULL;
299 c = mplib_get_char(f,mplib_data);
302 c = mplib_get_char(f,mplib_data);
303 if (c!=EOF && c!='\n')
304 mplib_unget_char(f,mplib_data,c);
312 #define APPEND_STRING(a,b) do { \
316 a = realloc(a, strlen(a)+strlen(b)+1); \
317 strcpy(a+strlen(a),b); \
321 void mplib_write_ascii_file (MP mp, void *ff, char *s) {
322 mplib_instance *mplib_data = mplib_get_data(mp);
324 void *f = ((File *)ff)->f;
326 if (f==mplib_data->term_file_ptr) {
327 APPEND_STRING(mplib_data->term_out,s);
328 } else if (f==mplib_data->err_file_ptr) {
329 APPEND_STRING(mplib_data->error_out,s);
330 } else if (f==mplib_data->log_file_ptr) {
331 APPEND_STRING(mplib_data->log_out,s);
332 } else if (f==mplib_data->ps_file_ptr) {
333 APPEND_STRING(mplib_data->ps_out,s);
335 fprintf((FILE *)f,s);
341 void mplib_read_binary_file (MP mp, void *ff, void **data, size_t *size) {
344 FILE *f = ((File *)ff)->f;
346 len = fread(*data,1,*size,f);
351 void mplib_write_binary_file (MP mp, void *ff, void *s, size_t size) {
353 FILE *f = ((File *)ff)->f;
360 void mplib_close_file (MP mp, void *ff) {
361 mplib_instance *mplib_data = mplib_get_data(mp);
363 void *f = ((File *)ff)->f;
364 if (f != NULL && f != mplib_data->term_file_ptr && f != mplib_data->err_file_ptr
365 && f != mplib_data->log_file_ptr && f != mplib_data->ps_file_ptr) {
372 int mplib_eof_file (MP mp, void *ff) {
373 mplib_instance *mplib_data = mplib_get_data(mp);
375 FILE *f = ((File *)ff)->f;
378 if (f==stdin && mplib_data->input_data != NULL) {
379 return (mplib_data->input_data_len==0);
386 void mplib_flush_file (MP mp, void *ff) {
390 #define APPEND_TO_EDGES(a) do { \
391 if (mplib_data->edges==NULL) { \
392 mplib_data->edges = hh; \
394 struct mp_edge_object *p = mplib_data->edges; \
395 while (p->_next!=NULL) { p = p->_next; } \
400 void mplib_shipout_backend (MP mp, int h) {
401 struct mp_edge_object *hh;
402 mplib_instance *mplib_data = mplib_get_data(mp);
403 hh = mp_gr_export(mp, h);
411 mplib_setup_file_ops(struct MP_options * options) {
412 options->find_file = mplib_find_file;
413 options->open_file = mplib_open_file;
414 options->close_file = mplib_close_file;
415 options->eof_file = mplib_eof_file;
416 options->flush_file = mplib_flush_file;
417 options->write_ascii_file = mplib_write_ascii_file;
418 options->read_ascii_file = mplib_read_ascii_file;
419 options->write_binary_file = mplib_write_binary_file;
420 options->read_binary_file = mplib_read_binary_file;
421 options->shipout_backend = mplib_shipout_backend;
425 mplib_new (lua_State *L) {
428 mplib_instance *mplib_data;
429 struct MP_options * options; /* instance options */
430 mp_ptr = lua_newuserdata(L, sizeof(MP *));
432 options = mp_options();
433 mplib_setup_file_ops(options);
434 mplib_data = mplib_make_data();
436 options->userdata = (void *)mplib_data;
437 options->noninteractive = 1; /* required ! */
438 options->print_found_names = 0;
439 if (lua_type(L,1)==LUA_TTABLE) {
440 for (i=0;mplib_parms[i].name!=NULL;i++) {
441 lua_getfield(L,1,mplib_parms[i].name);
442 if (lua_isnil(L,-1)) {
444 continue; /* skip unset */
446 switch(mplib_parms[i].idx) {
448 options->error_line = lua_tointeger(L,-1);
451 options->half_error_line = lua_tointeger(L,-1);
454 options->max_print_line = lua_tointeger(L,-1);
457 options->main_memory = lua_tointeger(L,-1);
460 options->hash_size = lua_tointeger(L,-1);
463 options->hash_prime = lua_tointeger(L,-1);
466 options->param_size = lua_tointeger(L,-1);
469 options->max_in_open = lua_tointeger(L,-1);
472 options->random_seed = lua_tointeger(L,-1);
475 options->interaction = luaL_checkoption(L,-1,"errorstopmode", interaction_options);
478 options->ini_version = lua_toboolean(L,-1);
481 options->troff_mode = lua_toboolean(L,-1);
484 options->print_found_names = lua_toboolean(L,-1);
488 options->command_line = strdup((char *)lua_tostring(L,-1));
492 options->mem_name = strdup((char *)lua_tostring(L,-1));
495 options->job_name = strdup((char *)lua_tostring(L,-1));
498 if(mplib_find_file_function(L)) { /* error here */
499 fprintf(stdout,"Invalid arguments to mp.new({find_file=...})\n");
508 *mp_ptr = mp_new(options);
509 xfree(options->command_line);
510 xfree(options->mem_name);
511 xfree(options->job_name);
514 luaL_getmetatable(L,MPLIB_METATABLE);
515 lua_setmetatable(L,-2);
524 mplib_collect (lua_State *L) {
525 MP *mp_ptr = is_mp(L,1);
531 mplib_tostring (lua_State *L) {
532 MP *mp_ptr = is_mp(L,1);
534 lua_pushfstring(L,"<MP %p>",*mp_ptr);
541 mplib_wrapresults(lua_State *L, mplib_instance *mplib_data, int h) {
544 if (mplib_data->term_out != NULL) {
545 lua_pushstring(L,mplib_data->term_out);
546 lua_setfield(L,-2,"term");
547 xfree(mplib_data->term_out);
549 if (mplib_data->error_out != NULL) {
550 lua_pushstring(L,mplib_data->error_out);
551 lua_setfield(L,-2,"error");
552 xfree(mplib_data->error_out);
554 if (mplib_data->log_out != NULL ) {
555 lua_pushstring(L,mplib_data->log_out);
556 lua_setfield(L,-2,"log");
557 xfree(mplib_data->log_out);
559 if (mplib_data->edges != NULL ) {
560 struct mp_edge_object **v;
561 struct mp_edge_object *p = mplib_data->edges;
565 v = lua_newuserdata (L, sizeof(struct mp_edge_object *));
567 luaL_getmetatable(L,MPLIB_FIG_METATABLE);
568 lua_setmetatable(L,-2);
569 lua_rawseti(L,-2,i); i++;
572 lua_setfield(L,-2,"fig");
573 mplib_data->edges = NULL;
576 lua_setfield(L,-2,"status");
581 mplib_execute (lua_State *L) {
583 MP *mp_ptr = is_mp(L,1);
584 if (*mp_ptr!=NULL && lua_isstring(L,2)) {
585 mplib_instance *mplib_data = mplib_get_data(*mp_ptr);
586 mplib_data->input_data = (char *)lua_tolstring(L,2, &(mplib_data->input_data_len));
587 mplib_data->input_data_ptr = mplib_data->input_data;
588 if ((*mp_ptr)->run_state==0) {
589 h = mp_initialize(*mp_ptr);
591 h = mp_execute(*mp_ptr);
592 if (mplib_data->input_data_len!=0) {
593 xfree(mplib_data->input_data);
594 xfree(mplib_data->input_data_ptr);
595 mplib_data->input_data_len=0;
597 return mplib_wrapresults(L, mplib_data, h);
605 mplib_finish (lua_State *L) {
606 MP *mp_ptr = is_mp(L,1);
608 mplib_instance *mplib_data = mplib_get_data(*mp_ptr);
609 int h = mp_finish(*mp_ptr);
610 return mplib_wrapresults(L, mplib_data, h);
620 mplib_fig_collect (lua_State *L) {
621 struct mp_edge_object **hh = is_fig(L,1);
623 mp_gr_toss_objects (*hh);
630 mplib_fig_body (lua_State *L) {
632 struct mp_graphic_object **v;
633 struct mp_graphic_object *p;
634 struct mp_edge_object **hh = is_fig(L,1);
638 v = lua_newuserdata (L, sizeof(struct mp_graphic_object *));
640 luaL_getmetatable(L,MPLIB_GR_METATABLE);
641 lua_setmetatable(L,-2);
642 lua_rawseti(L,-2,i); i++;
645 (*hh)->body = NULL; /* prevent double free */
651 mplib_fig_tostring (lua_State *L) {
652 struct mp_edge_object **hh = is_fig(L,1);
653 lua_pushfstring(L,"<figure %p>",*hh);
660 mp_wrapped_shipout (struct mp_edge_object *hh, int prologues, int procset) {
662 if (setjmp(mp->jump_buf)) {
665 mp_gr_ship_out(hh,prologues,procset);
670 mplib_fig_postscript (lua_State *L) {
671 struct mp_edge_object **hh = is_fig(L,1);
672 int prologues = luaL_optnumber(L,2,-1);
673 int procset = luaL_optnumber(L,3,-1);
674 mplib_instance *mplib_data = mplib_get_data((*hh)->_parent);
675 if (mplib_data->ps_out == NULL) {
676 if (mp_wrapped_shipout(*hh,prologues, procset)) {
677 if (mplib_data->ps_out!=NULL ) {
678 lua_pushstring(L, mplib_data->ps_out);
679 xfree(mplib_data->ps_out);
686 lua_pushstring(L,mplib_data->log_out);
687 xfree(mplib_data->ps_out);
696 mplib_fig_filename (lua_State *L) {
697 struct mp_edge_object **hh = is_fig(L,1);
699 char *s = (*hh)->_filename;
709 mplib_fig_bb (lua_State *L) {
710 struct mp_edge_object **hh = is_fig(L,1);
712 lua_pushnumber(L, (double)(*hh)->_minx/65536.0);
714 lua_pushnumber(L, (double)(*hh)->_miny/65536.0);
716 lua_pushnumber(L, (double)(*hh)->_maxx/65536.0);
718 lua_pushnumber(L, (double)(*hh)->_maxy/65536.0);
726 mplib_gr_collect (lua_State *L) {
727 struct mp_graphic_object **hh = is_gr_object(L,1);
729 mp_gr_toss_object(*hh);
736 mplib_gr_tostring (lua_State *L) {
737 struct mp_graphic_object **hh = is_gr_object(L,1);
738 lua_pushfstring(L,"<object %p>",*hh);
743 mplib_push_number (lua_State *L, integer x ) {
746 lua_pushnumber(L, y);
750 mplib_push_path (lua_State *L, struct mp_knot *h ) {
751 struct mp_knot *p; /* for scanning the path */
758 lua_pushstring(L,knot_originator_enum[p->originator_field]);
759 lua_setfield(L,-2,"originator");
760 lua_pushstring(L,knot_type_enum[p->left_type_field]);
761 lua_setfield(L,-2,"left_type");
762 lua_pushstring(L,knot_type_enum[p->right_type_field]);
763 lua_setfield(L,-2,"right_type");
764 mplib_push_number(L,p->x_coord_field);
765 lua_setfield(L,-2,"x_coord");
766 mplib_push_number(L,p->y_coord_field);
767 lua_setfield(L,-2,"y_coord");
768 mplib_push_number(L,p->left_x_field);
769 lua_setfield(L,-2,"left_x");
770 mplib_push_number(L,p->left_y_field);
771 lua_setfield(L,-2,"left_y");
772 mplib_push_number(L,p->right_x_field);
773 lua_setfield(L,-2,"right_x");
774 mplib_push_number(L,p->right_y_field);
775 lua_setfield(L,-2,"right_y");
776 lua_rawseti(L,-2,i); i++;
777 if ( p->right_type_field==mp_endpoint ) {
788 mplib_push_color (lua_State *L, struct mp_graphic_object *h ) {
791 lua_pushstring(L,color_model_enum[h->color_model_field]);
792 lua_setfield(L,-2,"model");
794 if (h->color_model_field == mp_rgb_model ||
795 h->color_model_field == mp_uninitialized_model) {
797 mplib_push_number(L,h->color_field.rgb._red_val);
799 mplib_push_number(L,h->color_field.rgb._green_val);
801 mplib_push_number(L,h->color_field.rgb._blue_val);
803 lua_setfield(L,-2,"rgb");
806 if (h->color_model_field == mp_cmyk_model ||
807 h->color_model_field == mp_uninitialized_model) {
809 mplib_push_number(L,h->color_field.cmyk._cyan_val);
811 mplib_push_number(L,h->color_field.cmyk._magenta_val);
813 mplib_push_number(L,h->color_field.cmyk._yellow_val);
815 mplib_push_number(L,h->color_field.cmyk._black_val);
817 lua_setfield(L,-2,"cmyk");
819 if (h->color_model_field == mp_grey_model ||
820 h->color_model_field == mp_uninitialized_model) {
822 mplib_push_number(L,h->color_field.grey._grey_val);
824 lua_setfield(L,-2,"grey");
832 /* dashes are complicated, perhaps it would be better if the
833 object had a PS-compatible representation */
835 mplib_push_dash (lua_State *L, struct mp_graphic_object *h ) {
838 mplib_push_number(L,h->dash_scale_field);
839 lua_setfield(L,-2,"scale");
841 lua_pushnumber(L,(int)h->dash_p_field);
842 lua_setfield(L,-2,"dashes");
849 mplib_push_transform (lua_State *L, struct mp_graphic_object *h ) {
853 mplib_push_number(L,h->tx_field);
854 lua_rawseti(L,-2,i); i++;
855 mplib_push_number(L,h->ty_field);
856 lua_rawseti(L,-2,i); i++;
857 mplib_push_number(L,h->txx_field);
858 lua_rawseti(L,-2,i); i++;
859 mplib_push_number(L,h->txy_field);
860 lua_rawseti(L,-2,i); i++;
861 mplib_push_number(L,h->tyx_field);
862 lua_rawseti(L,-2,i); i++;
863 mplib_push_number(L,h->tyy_field);
864 lua_rawseti(L,-2,i); i++;
872 mplib_fill_field (lua_State *L, struct mp_graphic_object *h, char *field) {
874 lua_pushstring(L,"fill");
875 } else if (FIELD(path)) {
876 mplib_push_path(L, h->path_p_field);
877 } else if (FIELD(htap)) {
878 mplib_push_path(L, h->htap_p_field);
879 } else if (FIELD(pen)) {
880 mplib_push_path(L, h->pen_p_field);
881 } else if (FIELD(color)) {
882 mplib_push_color(L, h);
883 } else if (FIELD(linejoin)) {
884 lua_pushnumber(L,h->ljoin_field);
885 } else if (FIELD(miterlimit)) {
886 mplib_push_number(L,h->miterlim_field);
887 } else if (FIELD(prescript)) {
888 lua_pushstring(L,h->pre_script_field);
889 } else if (FIELD(postscript)) {
890 lua_pushstring(L,h->post_script_field);
897 mplib_stroked_field (lua_State *L, struct mp_graphic_object *h, char *field) {
899 lua_pushstring(L,"outline");
900 } else if (FIELD(path)) {
901 mplib_push_path(L, h->path_p_field);
902 } else if (FIELD(pen)) {
903 mplib_push_path(L, h->pen_p_field);
904 } else if (FIELD(color)) {
905 mplib_push_color(L, h);
906 } else if (FIELD(dash)) {
907 mplib_push_dash(L, h);
908 } else if (FIELD(linecap)) {
909 lua_pushnumber(L,h->lcap_field);
910 } else if (FIELD(linejoin)) {
911 lua_pushnumber(L,h->ljoin_field);
912 } else if (FIELD(miterlimit)) {
913 mplib_push_number(L,h->miterlim_field);
914 } else if (FIELD(prescript)) {
915 lua_pushstring(L,h->pre_script_field);
916 } else if (FIELD(postscript)) {
917 lua_pushstring(L,h->post_script_field);
924 mplib_text_field (lua_State *L, struct mp_graphic_object *h, char *field) {
927 lua_pushstring(L,"text");
928 } else if (FIELD(text)) {
929 lua_pushstring(L,h->text_p_field);
930 } else if (FIELD(dsize)) {
931 lua_pushnumber(L,h->font_dsize_field);
932 } else if (FIELD(font)) {
933 lua_pushstring(L,h->font_name_field);
934 } else if (FIELD(color)) {
935 mplib_push_color(L, h);
936 } else if (FIELD(width)) {
937 mplib_push_number(L,h->width_field);
938 } else if (FIELD(height)) {
939 mplib_push_number(L,h->height_field);
940 } else if (FIELD(depth)) {
941 mplib_push_number(L,h->depth_field);
942 } else if (FIELD(transform)) {
943 mplib_push_transform(L,h);
944 } else if (FIELD(prescript)) {
945 lua_pushstring(L,h->pre_script_field);
946 } else if (FIELD(postscript)) {
947 lua_pushstring(L,h->post_script_field);
954 mplib_special_field (lua_State *L, struct mp_graphic_object *h, char *field) {
956 lua_pushstring(L,"special");
957 } else if (FIELD(prescript)) {
958 lua_pushstring(L,h->pre_script_field);
965 mplib_start_bounds_field (lua_State *L, struct mp_graphic_object *h, char *field) {
967 lua_pushstring(L,"start_bounds");
968 } else if (FIELD(path)) {
969 mplib_push_path(L,h->path_p_field);
976 mplib_start_clip_field (lua_State *L, struct mp_graphic_object *h, char *field) {
978 lua_pushstring(L,"start_clip");
979 } else if (FIELD(path)) {
980 mplib_push_path(L,h->path_p_field);
987 mplib_stop_bounds_field (lua_State *L, struct mp_graphic_object *h, char *field) {
988 if (h!=NULL && FIELD(type)) {
989 lua_pushstring(L,"stop_bounds");
996 mplib_stop_clip_field (lua_State *L, struct mp_graphic_object *h, char *field) {
997 if (h!=NULL && FIELD(type)) {
998 lua_pushstring(L,"stop_clip");
1005 mplib_gr_fields (lua_State *L) {
1006 const char **fields;
1009 struct mp_graphic_object **hh = is_gr_object(L,1);
1011 switch ((*hh)->_type_field) {
1012 case mp_fill_code: fields = fill_fields; break;
1013 case mp_stroked_code: fields = stroked_fields; break;
1014 case mp_text_code: fields = text_fields; break;
1015 case mp_special_code: fields = special_fields; break;
1016 case mp_start_clip_code: fields = start_clip_fields; break;
1017 case mp_start_bounds_code: fields = start_bounds_fields; break;
1018 case mp_stop_clip_code: fields = stop_clip_fields; break;
1019 case mp_stop_bounds_code: fields = stop_bounds_fields; break;
1020 default: fields = no_fields;
1023 for (f = *fields; f != NULL; f++) {
1024 lua_pushstring(L,f);
1025 lua_rawseti(L,-2,i); i++;
1034 mplib_gr_index (lua_State *L) {
1035 struct mp_graphic_object **hh = is_gr_object(L,1);
1036 char *field = (char *)luaL_checkstring(L,2);
1038 switch ((*hh)->_type_field) {
1039 case mp_fill_code: mplib_fill_field(L,*hh,field); break;
1040 case mp_stroked_code: mplib_stroked_field(L,*hh,field); break;
1041 case mp_text_code: mplib_text_field(L,*hh,field); break;
1042 case mp_special_code: mplib_special_field(L,*hh,field); break;
1043 case mp_start_clip_code: mplib_start_clip_field(L,*hh,field); break;
1044 case mp_start_bounds_code: mplib_start_bounds_field(L,*hh,field); break;
1045 case mp_stop_clip_code: mplib_stop_clip_field(L,*hh,field); break;
1046 case mp_stop_bounds_code: mplib_stop_bounds_field(L,*hh,field); break;
1047 default: lua_pushnil(L);
1056 static const struct luaL_reg mplib_meta[] = {
1057 {"__gc", mplib_collect},
1058 {"__tostring", mplib_tostring},
1059 {NULL, NULL} /* sentinel */
1062 static const struct luaL_reg mplib_fig_meta[] = {
1063 {"__gc", mplib_fig_collect },
1064 {"__tostring", mplib_fig_tostring },
1065 {"objects", mplib_fig_body },
1066 {"filename", mplib_fig_filename },
1067 {"postscript", mplib_fig_postscript },
1068 {"boundingbox", mplib_fig_bb },
1069 {NULL, NULL} /* sentinel */
1072 static const struct luaL_reg mplib_gr_meta[] = {
1073 {"__gc", mplib_gr_collect },
1074 {"__tostring", mplib_gr_tostring },
1075 {"__index", mplib_gr_index },
1076 {"fields", mplib_gr_fields },
1077 {NULL, NULL} /* sentinel */
1081 static const struct luaL_reg mplib_d [] = {
1082 {"execute", mplib_execute },
1083 {"finish", mplib_finish },
1084 {NULL, NULL} /* sentinel */
1088 static const struct luaL_reg mplib_m[] = {
1090 {NULL, NULL} /* sentinel */
1095 luaopen_mp (lua_State *L) {
1096 luaL_newmetatable(L,MPLIB_GR_METATABLE);
1097 lua_pushvalue(L, -1); /* push metatable */
1098 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1099 luaL_register(L, NULL, mplib_gr_meta); /* object meta methods */
1102 luaL_newmetatable(L,MPLIB_FIG_METATABLE);
1103 lua_pushvalue(L, -1); /* push metatable */
1104 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1105 luaL_register(L, NULL, mplib_fig_meta); /* figure meta methods */
1108 luaL_newmetatable(L,MPLIB_METATABLE);
1109 lua_pushvalue(L, -1); /* push metatable */
1110 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1111 luaL_register(L, NULL, mplib_meta); /* meta methods */
1112 luaL_register(L, NULL, mplib_d); /* dict methods */
1113 luaL_register(L, "mplib", mplib_m); /* module functions */