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)
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)
32 P__ZERO, P_ERROR_LINE, P_HALF_LINE, P_MAX_LINE, P_MAIN_MEMORY,
33 P_HASH_SIZE, P_HASH_PRIME, P_PARAM_SIZE, P_IN_OPEN, P_RANDOM_SEED,
34 P_INTERACTION, P_INI_VERSION, P_TROFF_MODE, P_PRINT_NAMES, P_COMMAND_LINE,
35 P_MEM_NAME, P_JOB_NAME, P_FIND_FILE, P_OPEN_FILE, P_CLOSE_FILE,
36 P_EOF_FILE, P_FLUSH_FILE, P_WRITE_ASCII, P_READ_ASCII, P_WRITE_BINARY,
37 P_READ_BINARY, P_RUN_EDITOR, P_RUN_MAKEMPX, P_SHIPOUT, P__SENTINEL,
41 const char *name; /* parameter name */
42 parm_idx idx; /* parameter index */
43 int class; /* parameter class */
46 const char *interaction_options[] =
47 { "unknownmode","batchmode","nonstopmode","scrollmode","errorstopmode", NULL};
49 /* the third field (type) is currently not used */
51 mplib_parm_struct mplib_parms[] = {
52 {NULL, P__ZERO, 0 }, /* dummy; lua indices run from 1 */
53 {"error_line", P_ERROR_LINE, 'i' },
54 {"half_error_line", P_HALF_LINE, 'i' },
55 {"max_print_line", P_MAX_LINE, 'i' },
56 {"main_memory", P_MAIN_MEMORY, 'i' },
57 {"hash_size", P_HASH_SIZE, 'i' },
58 {"hash_prime", P_HASH_PRIME, 'i' },
59 {"param_size", P_PARAM_SIZE, 'i' },
60 {"max_in_open", P_IN_OPEN, 'i' },
61 {"random_seed", P_RANDOM_SEED, 'i' },
62 {"interaction", P_INTERACTION, 'e' },
63 {"ini_version", P_INI_VERSION, 'b' },
64 {"troff_mode", P_TROFF_MODE, 'b' },
65 {"print_found_names", P_PRINT_NAMES, 'b' },
66 {"command_line", P_COMMAND_LINE,'s' },
67 {"mem_name", P_MEM_NAME, 's' },
68 {"job_name", P_JOB_NAME, 's' },
69 {"find_file", P_FIND_FILE, 'p' }, /* intercepted */
70 {NULL, P__SENTINEL, 0 }
73 typedef struct _FILE_ITEM {
77 typedef struct _FILE_ITEM File;
79 /* Start by defining all the callback routines for the library
80 * except |run_make_mpx| and |run_editor|.
83 char *mplib_filetype_names[] = {"term", "error", "mp", "log", "ps",
84 "mem", "tfm", "map", "pfb", "enc", NULL};
88 char *mplib_find_file (char *fname, char *fmode, int ftype) {
92 lua_getfield(L,LUA_REGISTRYINDEX,"mplib_file_finder");
93 if (lua_isfunction(L,-1)) {
94 char *s = NULL, *x = NULL;
95 lua_pushstring(L, fname);
96 lua_pushstring(L, fmode);
97 if (ftype >= mp_filetype_text) {
98 lua_pushnumber(L, ftype-mp_filetype_text);
100 lua_pushstring(L, mplib_filetype_names[ftype]);
102 if(lua_pcall(L,3,1,0) != 0) {
103 fprintf(stdout,"Error in mp.find_file: %s\n", (char *)lua_tostring(L,-1));
106 x = (char *)lua_tostring(L,-1);
109 lua_pop(L,1); /* pop the string */
115 if (fmode[0] != 'r' || (! access (fname,R_OK)) || ftype) {
116 return strdup(fname);
122 mplib_find_file_function (lua_State *L) {
123 if (lua_isfunction(L,-1)) {
125 } else if (lua_isnil(L,-1)) {
128 return 1; /* error */
130 lua_pushstring(L, "mplib_file_finder");
132 lua_rawset(L,LUA_REGISTRYINDEX);
136 void *term_file_ptr = NULL;
137 void *err_file_ptr = NULL;
138 void *log_file_ptr = NULL;
139 void *ps_file_ptr = NULL;
141 void *mplib_open_file(char *fname, char *fmode, int ftype) {
142 File *ff = malloc(sizeof (File));
145 if (ftype==mp_filetype_terminal) {
146 if (fmode[0] == 'r') {
149 xfree(term_file_ptr);
151 term_file_ptr = ff->f;
153 } else if (ftype==mp_filetype_error) {
156 err_file_ptr = ff->f;
157 } else if (ftype == mp_filetype_log) {
160 log_file_ptr = ff->f;
161 } else if (ftype == mp_filetype_postscript) {
167 if (fmode[0] == 'r') {
168 f = mplib_find_file(fname,fmode,ftype);
172 ff->f = fopen(f, fmode);
173 if ((fmode[0] == 'r') && (ff->f == NULL)) {
183 static char * input_data = NULL;
184 static char * input_data_ptr = NULL;
185 static size_t input_data_len = 0;
187 #define GET_CHAR() do { \
188 if (f==stdin && input_data != NULL) { \
189 if (input_data_len==0) { \
190 if (input_data_ptr!=NULL) \
191 input_data_ptr = NULL; \
197 c = *input_data_ptr++; \
204 #define UNGET_CHAR() do { \
205 if (f==stdin && input_data != NULL) { \
206 input_data_len++; input_data_ptr--; \
213 char *mplib_read_ascii_file (void *ff, size_t *size) {
215 size_t len = 0, lim = 128;
218 FILE *f = ((File *)ff)->f;
226 if (s==NULL) return NULL;
227 while (c!=EOF && c!='\n' && c!='\r') {
229 s =realloc(s, (lim+(lim>>2)));
230 if (s==NULL) return NULL;
238 if (c!=EOF && c!='\n')
247 static char *term_out = NULL;
248 static char *error_out = NULL;
249 static char *log_out = NULL;
250 static char *ps_out = NULL;
252 #define APPEND_STRING(a,b) do { \
256 a = realloc(a, strlen(a)+strlen(b)+1); \
257 strcpy(a+strlen(a),b); \
261 void mplib_write_ascii_file (void *ff, char *s) {
263 void *f = ((File *)ff)->f;
265 if (f==term_file_ptr) {
266 APPEND_STRING(term_out,s);
267 } else if (f==err_file_ptr) {
268 APPEND_STRING(error_out,s);
269 } else if (f==log_file_ptr) {
270 APPEND_STRING(log_out,s);
271 } else if (f==ps_file_ptr) {
272 APPEND_STRING(ps_out,s);
274 fprintf((FILE *)f,s);
280 void mplib_read_binary_file (void *ff, void **data, size_t *size) {
283 FILE *f = ((File *)ff)->f;
285 len = fread(*data,1,*size,f);
290 void mplib_write_binary_file (void *ff, void *s, size_t size) {
292 FILE *f = ((File *)ff)->f;
299 void mplib_close_file (void *ff) {
301 void *f = ((File *)ff)->f;
302 if (f != NULL && f != term_file_ptr && f != err_file_ptr
303 && f != log_file_ptr && f != ps_file_ptr) {
310 int mplib_eof_file (void *ff) {
312 FILE *f = ((File *)ff)->f;
315 if (f==stdin && input_data != NULL) {
316 return (input_data_len==0);
323 void mplib_flush_file (void *ff) {
327 static struct mp_edge_object *edges = NULL;
329 #define APPEND_TO_EDGES(a) do { \
333 struct mp_edge_object *p = edges; \
334 while (p->_next!=NULL) { p = p->_next; } \
339 void mplib_shipout_backend (MP mp, int h) {
340 struct mp_edge_object *hh;
341 hh = mp_gr_export(mp, h);
349 mplib_setup_file_ops(struct MP_options * options) {
350 options->find_file = mplib_find_file;
351 options->open_file = mplib_open_file;
352 options->close_file = mplib_close_file;
353 options->eof_file = mplib_eof_file;
354 options->flush_file = mplib_flush_file;
355 options->write_ascii_file = mplib_write_ascii_file;
356 options->read_ascii_file = mplib_read_ascii_file;
357 options->write_binary_file = mplib_write_binary_file;
358 options->read_binary_file = mplib_read_binary_file;
359 options->shipout_backend = mplib_shipout_backend;
363 mplib_new (lua_State *L) {
366 struct MP_options * options; /* instance options */
367 mp_ptr = lua_newuserdata(L, sizeof(MP *));
369 options = mp_options();
370 mplib_setup_file_ops(options);
371 options->noninteractive = 1; /* required ! */
372 options->print_found_names = 0;
373 if (lua_type(L,1)==LUA_TTABLE) {
374 for (i=1;mplib_parms[i].name!=NULL;i++) {
375 lua_getfield(L,1,mplib_parms[i].name);
376 if (lua_isnil(L,-1)) {
378 continue; /* skip unset */
380 switch(mplib_parms[i].idx) {
382 options->error_line = lua_tointeger(L,-1);
385 options->half_error_line = lua_tointeger(L,-1);
388 options->max_print_line = lua_tointeger(L,-1);
391 options->main_memory = lua_tointeger(L,-1);
394 options->hash_size = lua_tointeger(L,-1);
397 options->hash_prime = lua_tointeger(L,-1);
400 options->param_size = lua_tointeger(L,-1);
403 options->max_in_open = lua_tointeger(L,-1);
406 options->random_seed = lua_tointeger(L,-1);
409 options->interaction = luaL_checkoption(L,-1,"errorstopmode", interaction_options);
412 options->ini_version = lua_toboolean(L,-1);
415 options->troff_mode = lua_toboolean(L,-1);
418 options->print_found_names = lua_toboolean(L,-1);
421 options->command_line = strdup((char *)lua_tostring(L,-1));
424 options->mem_name = strdup((char *)lua_tostring(L,-1));
427 options->job_name = strdup((char *)lua_tostring(L,-1));
430 if(mplib_find_file_function(L)) { /* error here */
431 fprintf(stdout,"Invalid arguments to mp.new({find_file=...})\n");
440 *mp_ptr = mp_new(options);
441 xfree(options->command_line);
442 xfree(options->mem_name);
443 xfree(options->job_name);
446 h = mp_initialize(*mp_ptr);
448 luaL_getmetatable(L,MPLIB_METATABLE);
449 lua_setmetatable(L,-2);
459 mplib_collect (lua_State *L) {
460 MP *mp_ptr = is_mp(L,1);
469 mplib_tostring (lua_State *L) {
470 MP *mp_ptr = is_mp(L,1);
472 lua_pushfstring(L,"<MP %p>",*mp_ptr);
479 mplib_run (lua_State *L) {
480 MP *mp_ptr = is_mp(L,1);
482 int h = mp_run(*mp_ptr);
491 mplib_wrapresults(lua_State *L,int h) {
494 if (term_out != NULL) {
495 lua_pushstring(L,term_out);
496 lua_setfield(L,-2,"term");
497 free(term_out); term_out = NULL;
499 if (error_out != NULL) {
500 lua_pushstring(L,error_out);
501 lua_setfield(L,-2,"error");
502 free(error_out); error_out = NULL;
504 if (log_out != NULL ) {
505 lua_pushstring(L,log_out);
506 lua_setfield(L,-2,"log");
507 free(log_out); log_out = NULL;
509 if (edges != NULL ) {
510 struct mp_edge_object **v;
511 struct mp_edge_object *p = edges;
515 v = lua_newuserdata (L, sizeof(struct mp_edge_object *));
517 luaL_getmetatable(L,MPLIB_FIG_METATABLE);
518 lua_setmetatable(L,-2);
519 lua_rawseti(L,-2,i); i++;
522 lua_setfield(L,-2,"fig");
526 lua_setfield(L,-2,"status");
531 mplib_execute (lua_State *L) {
532 MP *mp_ptr = is_mp(L,1);
533 if (*mp_ptr!=NULL && lua_isstring(L,2)) {
534 if (input_data_len>0) { /* this should NOT happen */
535 fprintf(stderr,"Can't do concurrency yet!\n");
537 input_data = (char *)lua_tolstring(L,2, &input_data_len);
538 input_data_ptr = input_data;
539 int h = mp_execute(*mp_ptr);
540 return mplib_wrapresults(L, h);
550 mplib_finish (lua_State *L) {
551 MP *mp_ptr = is_mp(L,1);
553 int h = mp_finish(*mp_ptr);
554 return mplib_wrapresults(L, h);
564 mplib_fig_collect (lua_State *L) {
565 struct mp_edge_object **hh = is_fig(L,1);
567 mp_gr_toss_objects (*hh);
574 mplib_fig_body (lua_State *L) {
576 struct mp_graphic_object **v;
577 struct mp_graphic_object *p;
578 struct mp_edge_object **hh = is_fig(L,1);
582 v = lua_newuserdata (L, sizeof(struct mp_graphic_object *));
584 luaL_getmetatable(L,MPLIB_GR_METATABLE);
585 lua_setmetatable(L,-2);
586 lua_rawseti(L,-2,i); i++;
589 (*hh)->body = NULL; /* prevent double free */
595 mplib_fig_tostring (lua_State *L) {
596 struct mp_edge_object **hh = is_fig(L,1);
597 lua_pushfstring(L,"<figure %p>",*hh);
604 mp_wrapped_shipout (struct mp_edge_object *hh, int prologues, int procset) {
606 if (setjmp(mp->jump_buf)) {
609 mp_gr_ship_out(hh,prologues,procset);
614 mplib_fig_postscript (lua_State *L) {
615 struct mp_edge_object **hh = is_fig(L,1);
616 int prologues = luaL_optnumber(L,2,-1);
617 int procset = luaL_optnumber(L,3,-1);
618 if (ps_out == NULL) {
619 if (mp_wrapped_shipout(*hh,prologues, procset)) {
621 lua_pushstring(L, ps_out);
622 free(ps_out); ps_out = NULL;
629 lua_pushstring(L,log_out);
630 free(ps_out); ps_out = NULL;
639 mplib_fig_bb (lua_State *L) {
640 struct mp_edge_object **hh = is_fig(L,1);
642 lua_pushnumber(L, (double)(*hh)->_minx/65536.0);
644 lua_pushnumber(L, (double)(*hh)->_miny/65536.0);
646 lua_pushnumber(L, (double)(*hh)->_maxx/65536.0);
648 lua_pushnumber(L, (double)(*hh)->_maxy/65536.0);
656 mplib_gr_collect (lua_State *L) {
657 struct mp_graphic_object **hh = is_gr_object(L,1);
659 mp_gr_toss_object(*hh);
666 mplib_gr_tostring (lua_State *L) {
667 struct mp_graphic_object **hh = is_gr_object(L,1);
668 lua_pushfstring(L,"<object %p>",*hh);
672 /* only "endpoint" and "explicit" actually happen */
674 char *knot_type_enum[] = {
675 "endpoint", "explicit", "given", "curl", "open", "end_cycle"
678 char *knot_originator_enum[] = { "program" ,"user" };
683 mplib_push_path (lua_State *L, struct mp_knot *h ) {
684 struct mp_knot *p; /* for scanning the path */
691 lua_pushstring(L,knot_originator_enum[p->originator_field]);
692 lua_setfield(L,-2,"originator");
693 lua_pushstring(L,knot_type_enum[p->left_type_field]);
694 lua_setfield(L,-2,"left_type");
695 lua_pushstring(L,knot_type_enum[p->right_type_field]);
696 lua_setfield(L,-2,"right_type");
697 lua_pushnumber(L,p->x_coord_field);
698 lua_setfield(L,-2,"x_coord");
699 lua_pushnumber(L,p->y_coord_field);
700 lua_setfield(L,-2,"y_coord");
701 lua_pushnumber(L,p->left_x_field);
702 lua_setfield(L,-2,"left_x");
703 lua_pushnumber(L,p->left_y_field);
704 lua_setfield(L,-2,"left_y");
705 lua_pushnumber(L,p->right_x_field);
706 lua_setfield(L,-2,"right_x");
707 lua_pushnumber(L,p->right_y_field);
708 lua_setfield(L,-2,"right_y");
709 lua_rawseti(L,-2,i); i++;
710 if ( p->right_type_field==mp_endpoint ) {
720 char *color_model_enum[] = { NULL, "none", NULL, "grey",
721 NULL, "rgb", NULL, "cmyk", NULL, "uninitialized" };
724 mplib_push_color (lua_State *L, struct mp_graphic_object *h ) {
727 lua_pushstring(L,color_model_enum[h->color_model_field]);
728 lua_setfield(L,-2,"model");
730 if (h->color_model_field == mp_rgb_model ||
731 h->color_model_field == mp_uninitialized_model) {
733 lua_pushnumber(L,h->color_field.rgb._red_val);
735 lua_pushnumber(L,h->color_field.rgb._green_val);
737 lua_pushnumber(L,h->color_field.rgb._blue_val);
739 lua_setfield(L,-2,"rgb");
742 if (h->color_model_field == mp_cmyk_model ||
743 h->color_model_field == mp_uninitialized_model) {
745 lua_pushnumber(L,h->color_field.cmyk._cyan_val);
747 lua_pushnumber(L,h->color_field.cmyk._magenta_val);
749 lua_pushnumber(L,h->color_field.cmyk._yellow_val);
751 lua_pushnumber(L,h->color_field.cmyk._black_val);
753 lua_setfield(L,-2,"cmyk");
755 if (h->color_model_field == mp_grey_model ||
756 h->color_model_field == mp_uninitialized_model) {
758 lua_pushnumber(L,h->color_field.grey._grey_val);
760 lua_setfield(L,-2,"grey");
768 /* dashes are complicated, perhaps it would be better if the
769 object had a PS-compatible representation */
771 mplib_push_dash (lua_State *L, struct mp_graphic_object *h ) {
774 lua_pushnumber(L,h->dash_scale_field);
775 lua_setfield(L,-2,"scale");
777 lua_pushnumber(L,(int)h->dash_p_field);
778 lua_setfield(L,-2,"dashes");
785 mplib_push_transform (lua_State *L, struct mp_graphic_object *h ) {
789 lua_pushnumber(L,h->tx_field);
790 lua_rawseti(L,-2,i); i++;
791 lua_pushnumber(L,h->ty_field);
792 lua_rawseti(L,-2,i); i++;
793 lua_pushnumber(L,h->txx_field);
794 lua_rawseti(L,-2,i); i++;
795 lua_pushnumber(L,h->txy_field);
796 lua_rawseti(L,-2,i); i++;
797 lua_pushnumber(L,h->tyx_field);
798 lua_rawseti(L,-2,i); i++;
799 lua_pushnumber(L,h->tyy_field);
800 lua_rawseti(L,-2,i); i++;
806 static char *fill_fields[] = { "type", "path", "htap", "pen", "color", "ljoin", "miterlim",
807 "prescript", "postscript", NULL };
809 #define FIELD(A) (strcmp(field,#A)==0)
812 mplib_fill_field (lua_State *L, struct mp_graphic_object *h, char *field) {
814 lua_pushstring(L,"fill");
815 } else if (FIELD(path)) {
816 mplib_push_path(L, h->path_p_field);
817 } else if (FIELD(htap)) {
818 mplib_push_path(L, h->htap_p_field);
819 } else if (FIELD(pen)) {
820 mplib_push_path(L, h->pen_p_field);
821 } else if (FIELD(color)) {
822 mplib_push_color(L, h);
823 } else if (FIELD(ljoin)) {
824 lua_pushnumber(L,h->ljoin_field);
825 } else if (FIELD(miterlim)) {
826 lua_pushnumber(L,h->miterlim_field);
827 } else if (FIELD(prescript)) {
828 lua_pushstring(L,h->pre_script_field);
829 } else if (FIELD(postscript)) {
830 lua_pushstring(L,h->post_script_field);
836 static char *stroked_fields[] = {"type", "path", "pen", "color", "ljoin", "miterlim",
837 "lcap", "dash", "prescript", "postscript", NULL };
840 mplib_stroked_field (lua_State *L, struct mp_graphic_object *h, char *field) {
842 lua_pushstring(L,"stroked");
843 } else if (FIELD(path)) {
844 mplib_push_path(L, h->path_p_field);
845 } else if (FIELD(pen)) {
846 mplib_push_path(L, h->pen_p_field);
847 } else if (FIELD(color)) {
848 mplib_push_color(L, h);
849 } else if (FIELD(dash)) {
850 mplib_push_dash(L, h);
851 } else if (FIELD(lcap)) {
852 lua_pushnumber(L,h->lcap_field);
853 } else if (FIELD(ljoin)) {
854 lua_pushnumber(L,h->ljoin_field);
855 } else if (FIELD(miterlim)) {
856 lua_pushnumber(L,h->miterlim_field);
857 } else if (FIELD(prescript)) {
858 lua_pushstring(L,h->pre_script_field);
859 } else if (FIELD(postscript)) {
860 lua_pushstring(L,h->post_script_field);
866 static char *text_fields[] = { "type", "text", "dsize", "font", "color",
867 "width", "height", "depth", "transform",
868 "prescript", "postscript", NULL };
871 mplib_text_field (lua_State *L, struct mp_graphic_object *h, char *field) {
874 lua_pushstring(L,"text");
875 } else if (FIELD(text)) {
876 lua_pushstring(L,h->text_p_field);
877 } else if (FIELD(dsize)) {
878 lua_pushnumber(L,h->font_dsize_field);
879 } else if (FIELD(font)) {
880 lua_pushstring(L,h->font_name_field);
881 } else if (FIELD(color)) {
882 mplib_push_color(L, h);
883 } else if (FIELD(width)) {
884 lua_pushnumber(L,h->width_field);
885 } else if (FIELD(height)) {
886 lua_pushnumber(L,h->height_field);
887 } else if (FIELD(depth)) {
888 lua_pushnumber(L,h->depth_field);
889 } else if (FIELD(transform)) {
890 mplib_push_transform(L,h);
891 } else if (FIELD(prescript)) {
892 lua_pushstring(L,h->pre_script_field);
893 } else if (FIELD(postscript)) {
894 lua_pushstring(L,h->post_script_field);
900 static char *special_fields[] = { "type", "prescript", NULL };
903 mplib_special_field (lua_State *L, struct mp_graphic_object *h, char *field) {
905 lua_pushstring(L,"special");
906 } else if (FIELD(prescript)) {
907 lua_pushstring(L,h->pre_script_field);
913 static char *start_bounds_fields[] = { "type", "path", NULL };
916 mplib_start_bounds_field (lua_State *L, struct mp_graphic_object *h, char *field) {
918 lua_pushstring(L,"start_bounds");
919 } else if (FIELD(path)) {
920 mplib_push_path(L,h->path_p_field);
926 static char *start_clip_fields[] = { "type", "path", NULL };
929 mplib_start_clip_field (lua_State *L, struct mp_graphic_object *h, char *field) {
931 lua_pushstring(L,"start_clip");
932 } else if (FIELD(path)) {
933 mplib_push_path(L,h->path_p_field);
939 static char *stop_bounds_fields[] = { "type", NULL };
942 mplib_stop_bounds_field (lua_State *L, struct mp_graphic_object *h, char *field) {
943 if (h!=NULL && FIELD(type)) {
944 lua_pushstring(L,"stop_bounds");
950 static char *stop_clip_fields[] = { "type", NULL };
953 mplib_stop_clip_field (lua_State *L, struct mp_graphic_object *h, char *field) {
954 if (h!=NULL && FIELD(type)) {
955 lua_pushstring(L,"stop_clip");
961 static char *no_fields[] = { NULL };
964 mplib_gr_fields (lua_State *L) {
968 struct mp_graphic_object **hh = is_gr_object(L,1);
970 switch ((*hh)->_type_field) {
971 case mp_fill_code: fields = fill_fields; break;
972 case mp_stroked_code: fields = stroked_fields; break;
973 case mp_text_code: fields = text_fields; break;
974 case mp_special_code: fields = special_fields; break;
975 case mp_start_clip_code: fields = start_clip_fields; break;
976 case mp_start_bounds_code: fields = start_bounds_fields; break;
977 case mp_stop_clip_code: fields = stop_clip_fields; break;
978 case mp_stop_bounds_code: fields = stop_bounds_fields; break;
979 default: fields = no_fields;
982 for (f = *fields; f != NULL; f++) {
984 lua_rawseti(L,-2,i); i++;
993 mplib_gr_index (lua_State *L) {
994 struct mp_graphic_object **hh = is_gr_object(L,1);
995 char *field = (char *)luaL_checkstring(L,2);
997 switch ((*hh)->_type_field) {
998 case mp_fill_code: mplib_fill_field(L,*hh,field); break;
999 case mp_stroked_code: mplib_stroked_field(L,*hh,field); break;
1000 case mp_text_code: mplib_text_field(L,*hh,field); break;
1001 case mp_special_code: mplib_special_field(L,*hh,field); break;
1002 case mp_start_clip_code: mplib_start_clip_field(L,*hh,field); break;
1003 case mp_start_bounds_code: mplib_start_bounds_field(L,*hh,field); break;
1004 case mp_stop_clip_code: mplib_stop_clip_field(L,*hh,field); break;
1005 case mp_stop_bounds_code: mplib_stop_bounds_field(L,*hh,field); break;
1006 default: lua_pushnil(L);
1015 static const struct luaL_reg mplib_meta[] = {
1016 {"__gc", mplib_collect},
1017 {"__tostring", mplib_tostring},
1018 {NULL, NULL} /* sentinel */
1021 static const struct luaL_reg mplib_fig_meta[] = {
1022 {"__gc", mplib_fig_collect },
1023 {"__tostring", mplib_fig_tostring },
1024 {"objects", mplib_fig_body },
1025 {"postscript", mplib_fig_postscript },
1026 {"boundingbox", mplib_fig_bb },
1027 {NULL, NULL} /* sentinel */
1030 static const struct luaL_reg mplib_gr_meta[] = {
1031 {"__gc", mplib_gr_collect },
1032 {"__tostring", mplib_gr_tostring },
1033 {"fields", mplib_gr_fields },
1034 {"__index", mplib_gr_index },
1035 {NULL, NULL} /* sentinel */
1039 static const struct luaL_reg mplib_d [] = {
1040 {"run", mplib_run },
1041 {"execute", mplib_execute },
1042 {"finish", mplib_finish },
1043 {NULL, NULL} /* sentinel */
1047 static const struct luaL_reg mplib_m[] = {
1049 {NULL, NULL} /* sentinel */
1054 luaopen_mp (lua_State *L) {
1055 luaL_newmetatable(L,MPLIB_GR_METATABLE);
1056 lua_pushvalue(L, -1); /* push metatable */
1057 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1058 luaL_register(L, NULL, mplib_gr_meta); /* object meta methods */
1061 luaL_newmetatable(L,MPLIB_FIG_METATABLE);
1062 lua_pushvalue(L, -1); /* push metatable */
1063 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1064 luaL_register(L, NULL, mplib_fig_meta); /* figure meta methods */
1067 luaL_newmetatable(L,MPLIB_METATABLE);
1068 lua_pushvalue(L, -1); /* push metatable */
1069 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1070 luaL_register(L, NULL, mplib_meta); /* meta methods */
1071 luaL_register(L, NULL, mplib_d); /* dict methods */
1072 luaL_register(L, "mp", mplib_m); /* module functions */