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 mplib_init_S(a) do { \
26 lua_pushliteral(L,#a); \
27 mplib_##a##_ptr = (char *)lua_tostring(L,-1); \
28 mplib_##a##_index = luaL_ref (L,LUA_REGISTRYINDEX); \
31 #define mplib_push_S(a) do { \
32 lua_rawgeti(L,LUA_REGISTRYINDEX,mplib_##a##_index); \
35 #define mplib_is_S(a,i) (mplib_##a##_ptr==(char *)lua_tostring(L,i))
37 #define mplib_make_S(a) \
38 static int mplib_##a##_index = 0; \
39 static char *mplib_##a##_ptr = NULL;
41 mplib_make_S(left_type);
42 mplib_make_S(right_type);
43 mplib_make_S(x_coord);
44 mplib_make_S(y_coord);
47 mplib_make_S(right_x);
48 mplib_make_S(right_y);
57 mplib_make_S(linecap);
58 mplib_make_S(linejoin);
59 mplib_make_S(miterlimit);
62 mplib_make_S(postscript);
63 mplib_make_S(prescript);
65 mplib_make_S(transform);
69 void mplib_init_Ses(lua_State *L) {
70 mplib_init_S(left_type);
71 mplib_init_S(right_type);
72 mplib_init_S(x_coord);
73 mplib_init_S(y_coord);
76 mplib_init_S(right_x);
77 mplib_init_S(right_y);
86 mplib_init_S(linecap);
87 mplib_init_S(linejoin);
88 mplib_init_S(miterlimit);
91 mplib_init_S(postscript);
92 mplib_init_S(prescript);
94 mplib_init_S(transform);
102 #define xfree(A) if ((A)!=NULL) { free((A)); A = NULL; }
104 #define is_mp(L,b) (MP *)luaL_checkudata(L,b,MPLIB_METATABLE)
105 #define is_fig(L,b) (struct mp_edge_object **)luaL_checkudata(L,b,MPLIB_FIG_METATABLE)
106 #define is_gr_object(L,b) (struct mp_graphic_object **)luaL_checkudata(L,b,MPLIB_GR_METATABLE)
108 /* Enumeration string arrays */
110 static const char *interaction_options[] =
111 { "unknown","batch","nonstop","scroll","errorstop", NULL};
113 static const char *mplib_filetype_names[] =
114 {"term", "error", "mp", "log", "ps", "mem", "tfm", "map", "pfb", "enc", NULL};
116 /* only "endpoint" and "explicit" actually happen in paths,
117 as well as "open" in elliptical pens */
119 static const char *knot_type_enum[] =
120 { "endpoint", "explicit", "given", "curl", "open", "end_cycle" };
122 /* this looks a bit funny because of the holes */
124 static const char *color_model_enum[] =
125 { NULL, "none", NULL, "grey", NULL, "rgb", NULL, "cmyk", NULL, "uninitialized" };
129 #define FIELD(A) (mplib_is_S(A,field))
131 static const char *fill_fields[] =
132 { "type", "path", "htap", "pen", "color", "linejoin", "miterlimit",
133 "prescript", "postscript", NULL };
135 static const char *stroked_fields[] =
136 { "type", "path", "pen", "color", "linejoin", "miterlimit", "linecap", "dash",
137 "prescript", "postscript", NULL };
139 static const char *text_fields[] =
140 { "type", "text", "dsize", "font", "color", "width", "height", "depth", "transform",
141 "prescript", "postscript", NULL };
143 static const char *special_fields[] =
144 { "type", "prescript", NULL };
146 static const char *start_bounds_fields[] =
147 { "type", "path", NULL };
149 static const char *start_clip_fields[] =
150 { "type", "path", NULL };
152 static const char *stop_bounds_fields[] =
155 static const char *stop_clip_fields[] =
158 static const char *no_fields[] =
162 P_ERROR_LINE, P_HALF_LINE, P_MAX_LINE, P_MAIN_MEMORY,
163 P_HASH_SIZE, P_HASH_PRIME, P_PARAM_SIZE, P_IN_OPEN, P_RANDOM_SEED,
164 P_INTERACTION, P_INI_VERSION, P_TROFF_MODE, P_PRINT_NAMES, P_MEM_NAME,
165 P_JOB_NAME, P_FIND_FILE, P__SENTINEL,
169 const char *name; /* parameter name */
170 mplib_parm_idx idx; /* parameter index */
173 static mplib_parm_struct mplib_parms[] = {
174 {"error_line", P_ERROR_LINE },
175 {"half_error_line", P_HALF_LINE },
176 {"max_print_line", P_MAX_LINE },
177 {"main_memory", P_MAIN_MEMORY },
178 {"hash_size", P_HASH_SIZE },
179 {"hash_prime", P_HASH_PRIME },
180 {"param_size", P_PARAM_SIZE },
181 {"max_in_open", P_IN_OPEN },
182 {"random_seed", P_RANDOM_SEED },
183 {"interaction", P_INTERACTION },
184 {"ini_version", P_INI_VERSION },
185 {"troff_mode", P_TROFF_MODE },
186 {"print_found_names", P_PRINT_NAMES },
187 {"mem_name", P_MEM_NAME },
188 {"job_name", P_JOB_NAME },
189 {"find_file", P_FIND_FILE },
193 typedef struct _FILE_ITEM {
197 typedef struct _FILE_ITEM File;
199 #define make_stream_buf(A) char *A; size_t A##_size; size_t A##_used
201 #define free_stream_buf(A) xfree(mplib_data->A); mplib_data->A##_size = 0; mplib_data->A##_used = 0
203 typedef struct _MPLIB_INSTANCE_DATA {
208 make_stream_buf(term_out);
209 make_stream_buf(error_out);
210 make_stream_buf(log_out);
211 make_stream_buf(ps_out);
213 char *input_data_ptr;
214 size_t input_data_len;
215 struct mp_edge_object *edges ;
217 } _MPLIB_INSTANCE_DATA;
219 typedef struct _MPLIB_INSTANCE_DATA mplib_instance;
221 static mplib_instance *mplib_get_data (MP mp) {
222 return (mplib_instance *)mp->userdata;
225 static mplib_instance *mplib_make_data (void) {
226 mplib_instance *mplib_data = malloc(sizeof(mplib_instance));
227 memset(mplib_data,0,sizeof(mplib_instance));
232 /* Start by defining all the callback routines for the library
233 * except |run_make_mpx| and |run_editor|.
237 char *mplib_find_file (MP mp, char *fname, char *fmode, int ftype) {
238 mplib_instance *mplib_data = mplib_get_data(mp);
239 lua_State *L = mplib_data->LL;
241 lua_getfield(L,LUA_REGISTRYINDEX,"mplib_file_finder");
242 if (lua_isfunction(L,-1)) {
243 char *s = NULL, *x = NULL;
244 lua_pushstring(L, fname);
245 lua_pushstring(L, fmode);
246 if (ftype >= mp_filetype_text) {
247 lua_pushnumber(L, ftype-mp_filetype_text);
249 lua_pushstring(L, mplib_filetype_names[ftype]);
251 if(lua_pcall(L,3,1,0) != 0) {
252 fprintf(stdout,"Error in mp.find_file: %s\n", (char *)lua_tostring(L,-1));
255 x = (char *)lua_tostring(L,-1);
258 lua_pop(L,1); /* pop the string */
263 if (fmode[0] != 'r' || (! access (fname,R_OK)) || ftype) {
264 return strdup(fname);
270 mplib_find_file_function (lua_State *L) {
271 if (! (lua_isfunction(L,-1)|| lua_isnil(L,-1) )) {
272 return 1; /* error */
274 lua_pushstring(L, "mplib_file_finder");
276 lua_rawset(L,LUA_REGISTRYINDEX);
280 void *mplib_open_file(MP mp, char *fname, char *fmode, int ftype) {
281 File *ff = malloc(sizeof (File));
283 mplib_instance *mplib_data = mplib_get_data(mp);
285 if (ftype==mp_filetype_terminal) {
286 if (fmode[0] == 'r') {
289 xfree(mplib_data->term_file_ptr);
291 mplib_data->term_file_ptr = ff->f;
293 } else if (ftype==mp_filetype_error) {
294 xfree(mplib_data->err_file_ptr);
296 mplib_data->err_file_ptr = ff->f;
297 } else if (ftype == mp_filetype_log) {
298 xfree(mplib_data->log_file_ptr);
300 mplib_data->log_file_ptr = ff->f;
301 } else if (ftype == mp_filetype_postscript) {
302 xfree(mplib_data->ps_file_ptr);
304 mplib_data->ps_file_ptr = ff->f;
308 f = mplib_find_file(mp, fname,fmode,ftype);
311 realmode[0] = *fmode;
314 ff->f = fopen(f, realmode);
315 if ((fmode[0] == 'r') && (ff->f == NULL)) {
326 mplib_get_char (void *f, mplib_instance *mplib_data) {
328 if (f==stdin && mplib_data->input_data != NULL) {
329 if (mplib_data->input_data_len==0) {
330 if (mplib_data->input_data_ptr!=NULL)
331 mplib_data->input_data_ptr = NULL;
333 mplib_data->input_data = NULL;
336 mplib_data->input_data_len--;
337 c = *(mplib_data->input_data_ptr)++;
346 mplib_unget_char (void *f, mplib_instance *mplib_data, int c) {
347 if (f==stdin && mplib_data->input_data_ptr != NULL) {
348 mplib_data->input_data_len++;
349 mplib_data->input_data_ptr--;
356 char *mplib_read_ascii_file (MP mp, void *ff, size_t *size) {
360 size_t len = 0, lim = 128;
361 mplib_instance *mplib_data = mplib_get_data(mp);
362 FILE *f = ((File *)ff)->f;
366 c = mplib_get_char(f,mplib_data);
370 if (s==NULL) return NULL;
371 while (c!=EOF && c!='\n' && c!='\r') {
373 s =realloc(s, (lim+(lim>>2)));
374 if (s==NULL) return NULL;
378 c = mplib_get_char(f,mplib_data);
381 c = mplib_get_char(f,mplib_data);
382 if (c!=EOF && c!='\n')
383 mplib_unget_char(f,mplib_data,c);
391 #define APPEND_STRING(a,b) do { \
392 if ((mplib_data->a##_used+strlen(b))>=mplib_data->a##_size) { \
393 mplib_data->a##_size += 256+(mplib_data->a##_size)/5+strlen(b); \
394 mplib_data->a = realloc(mplib_data->a,mplib_data->a##_size); \
396 (void)strcpy(mplib_data->a+mplib_data->a##_used,b); \
397 mplib_data->a##_used += strlen(b); \
400 void mplib_write_ascii_file (MP mp, void *ff, char *s) {
402 void *f = ((File *)ff)->f;
403 mplib_instance *mplib_data = mplib_get_data(mp);
405 if (f==mplib_data->term_file_ptr) {
406 APPEND_STRING(term_out,s);
407 } else if (f==mplib_data->err_file_ptr) {
408 APPEND_STRING(error_out,s);
409 } else if (f==mplib_data->log_file_ptr) {
410 APPEND_STRING(log_out,s);
411 } else if (f==mplib_data->ps_file_ptr) {
412 APPEND_STRING(ps_out,s);
414 fprintf((FILE *)f,s);
420 void mplib_read_binary_file (MP mp, void *ff, void **data, size_t *size) {
424 FILE *f = ((File *)ff)->f;
426 len = fread(*data,1,*size,f);
431 void mplib_write_binary_file (MP mp, void *ff, void *s, size_t size) {
434 FILE *f = ((File *)ff)->f;
441 void mplib_close_file (MP mp, void *ff) {
443 mplib_instance *mplib_data = mplib_get_data(mp);
444 void *f = ((File *)ff)->f;
445 if (f != NULL && f != mplib_data->term_file_ptr && f != mplib_data->err_file_ptr
446 && f != mplib_data->log_file_ptr && f != mplib_data->ps_file_ptr) {
453 int mplib_eof_file (MP mp, void *ff) {
455 mplib_instance *mplib_data = mplib_get_data(mp);
456 FILE *f = ((File *)ff)->f;
459 if (f==stdin && mplib_data->input_data != NULL) {
460 return (mplib_data->input_data_len==0);
467 void mplib_flush_file (MP mp, void *ff) {
473 #define APPEND_TO_EDGES(a) do { \
474 if (mplib_data->edges==NULL) { \
475 mplib_data->edges = hh; \
477 struct mp_edge_object *p = mplib_data->edges; \
478 while (p->_next!=NULL) { p = p->_next; } \
483 void mplib_shipout_backend (MP mp, int h) {
484 struct mp_edge_object *hh = mp_gr_export(mp, h);
486 mplib_instance *mplib_data = mplib_get_data(mp);
493 mplib_setup_file_ops(struct MP_options * options) {
494 options->find_file = mplib_find_file;
495 options->open_file = mplib_open_file;
496 options->close_file = mplib_close_file;
497 options->eof_file = mplib_eof_file;
498 options->flush_file = mplib_flush_file;
499 options->write_ascii_file = mplib_write_ascii_file;
500 options->read_ascii_file = mplib_read_ascii_file;
501 options->write_binary_file = mplib_write_binary_file;
502 options->read_binary_file = mplib_read_binary_file;
503 options->shipout_backend = mplib_shipout_backend;
507 mplib_new (lua_State *L) {
509 mp_ptr = lua_newuserdata(L, sizeof(MP *));
512 mplib_instance *mplib_data;
513 struct MP_options * options; /* instance options */
514 options = mp_options();
515 mplib_setup_file_ops(options);
516 mplib_data = mplib_make_data();
518 options->userdata = (void *)mplib_data;
519 options->noninteractive = 1; /* required ! */
520 options->print_found_names = 0;
521 if (lua_type(L,1)==LUA_TTABLE) {
522 for (i=0;mplib_parms[i].name!=NULL;i++) {
523 lua_getfield(L,1,mplib_parms[i].name);
524 if (lua_isnil(L,-1)) {
526 continue; /* skip unset */
528 switch(mplib_parms[i].idx) {
530 options->error_line = lua_tointeger(L,-1);
533 options->half_error_line = lua_tointeger(L,-1);
536 options->max_print_line = lua_tointeger(L,-1);
539 options->main_memory = lua_tointeger(L,-1);
542 options->hash_size = lua_tointeger(L,-1);
545 options->hash_prime = lua_tointeger(L,-1);
548 options->param_size = lua_tointeger(L,-1);
551 options->max_in_open = lua_tointeger(L,-1);
554 options->random_seed = lua_tointeger(L,-1);
557 options->interaction = luaL_checkoption(L,-1,"errorstopmode", interaction_options);
560 options->ini_version = lua_toboolean(L,-1);
563 options->troff_mode = lua_toboolean(L,-1);
566 options->print_found_names = lua_toboolean(L,-1);
570 options->command_line = strdup((char *)lua_tostring(L,-1));
574 options->mem_name = strdup((char *)lua_tostring(L,-1));
577 options->job_name = strdup((char *)lua_tostring(L,-1));
580 if(mplib_find_file_function(L)) { /* error here */
581 fprintf(stdout,"Invalid arguments to mp.new({find_file=...})\n");
590 *mp_ptr = mp_new(options);
591 xfree(options->command_line);
592 xfree(options->mem_name);
593 xfree(options->job_name);
596 luaL_getmetatable(L,MPLIB_METATABLE);
597 lua_setmetatable(L,-2);
606 mplib_collect (lua_State *L) {
607 MP *mp_ptr = is_mp(L,1);
613 mplib_tostring (lua_State *L) {
614 MP *mp_ptr = is_mp(L,1);
616 lua_pushfstring(L,"<MP %p>",*mp_ptr);
623 mplib_wrapresults(lua_State *L, mplib_instance *mplib_data, int h) {
626 if (mplib_data->term_out != NULL) {
627 lua_pushstring(L,mplib_data->term_out);
628 lua_setfield(L,-2,"term");
629 free_stream_buf(term_out);
631 if (mplib_data->error_out != NULL) {
632 lua_pushstring(L,mplib_data->error_out);
633 lua_setfield(L,-2,"error");
634 free_stream_buf(error_out);
636 if (mplib_data->log_out != NULL ) {
637 lua_pushstring(L,mplib_data->log_out);
638 lua_setfield(L,-2,"log");
639 free_stream_buf(log_out);
641 if (mplib_data->edges != NULL ) {
642 struct mp_edge_object **v;
643 struct mp_edge_object *p = mplib_data->edges;
647 v = lua_newuserdata (L, sizeof(struct mp_edge_object *));
649 luaL_getmetatable(L,MPLIB_FIG_METATABLE);
650 lua_setmetatable(L,-2);
651 lua_rawseti(L,-2,i); i++;
654 lua_setfield(L,-2,"fig");
655 mplib_data->edges = NULL;
658 lua_setfield(L,-2,"status");
663 mplib_execute (lua_State *L) {
664 MP *mp_ptr = is_mp(L,1);
665 if (*mp_ptr!=NULL && lua_isstring(L,2)) {
667 mplib_instance *mplib_data = mplib_get_data(*mp_ptr);
668 mplib_data->input_data = (char *)lua_tolstring(L,2, &(mplib_data->input_data_len));
669 mplib_data->input_data_ptr = mplib_data->input_data;
670 if ((*mp_ptr)->run_state==0) {
671 h = mp_initialize(*mp_ptr);
673 h = mp_execute(*mp_ptr);
674 if (mplib_data->input_data_len!=0) {
675 mplib_data->input_data = NULL;
676 mplib_data->input_data_ptr = NULL;
677 mplib_data->input_data_len=0;
679 return mplib_wrapresults(L, mplib_data, h);
687 mplib_finish (lua_State *L) {
688 MP *mp_ptr = is_mp(L,1);
690 mplib_instance *mplib_data = mplib_get_data(*mp_ptr);
691 int h = mp_finish(*mp_ptr);
692 return mplib_wrapresults(L, mplib_data, h);
702 mplib_fig_collect (lua_State *L) {
703 struct mp_edge_object **hh = is_fig(L,1);
705 mp_gr_toss_objects (*hh);
712 mplib_fig_body (lua_State *L) {
714 struct mp_graphic_object **v;
715 struct mp_graphic_object *p;
716 struct mp_edge_object **hh = is_fig(L,1);
720 v = lua_newuserdata (L, sizeof(struct mp_graphic_object *));
722 luaL_getmetatable(L,MPLIB_GR_METATABLE);
723 lua_setmetatable(L,-2);
724 lua_rawseti(L,-2,i); i++;
727 (*hh)->body = NULL; /* prevent double free */
732 mplib_fig_copy_body (lua_State *L) {
734 struct mp_graphic_object **v;
735 struct mp_graphic_object *p;
736 struct mp_edge_object **hh = is_fig(L,1);
740 v = lua_newuserdata (L, sizeof(struct mp_graphic_object *));
741 *v = mp_gr_copy_object((*hh)->_parent,p);
742 luaL_getmetatable(L,MPLIB_GR_METATABLE);
743 lua_setmetatable(L,-2);
744 lua_rawseti(L,-2,i); i++;
752 mplib_fig_tostring (lua_State *L) {
753 struct mp_edge_object **hh = is_fig(L,1);
754 lua_pushfstring(L,"<figure %p>",*hh);
761 mp_wrapped_shipout (struct mp_edge_object *hh, int prologues, int procset) {
763 if (setjmp(mp->jump_buf)) {
766 mp_gr_ship_out(hh,prologues,procset);
771 mplib_fig_postscript (lua_State *L) {
772 struct mp_edge_object **hh = is_fig(L,1);
773 int prologues = luaL_optnumber(L,2,-1);
774 int procset = luaL_optnumber(L,3,-1);
775 mplib_instance *mplib_data = mplib_get_data((*hh)->_parent);
776 if (mplib_data->ps_out == NULL) {
777 if (mp_wrapped_shipout(*hh,prologues, procset)) {
778 if (mplib_data->ps_out!=NULL ) {
779 lua_pushstring(L, mplib_data->ps_out);
780 free_stream_buf(ps_out);
787 lua_pushstring(L,mplib_data->log_out);
788 xfree(mplib_data->ps_out);
797 mplib_fig_filename (lua_State *L) {
798 struct mp_edge_object **hh = is_fig(L,1);
800 char *s = (*hh)->_filename;
810 mplib_fig_bb (lua_State *L) {
811 struct mp_edge_object **hh = is_fig(L,1);
813 lua_pushnumber(L, (double)(*hh)->_minx/65536.0);
815 lua_pushnumber(L, (double)(*hh)->_miny/65536.0);
817 lua_pushnumber(L, (double)(*hh)->_maxx/65536.0);
819 lua_pushnumber(L, (double)(*hh)->_maxy/65536.0);
827 mplib_gr_collect (lua_State *L) {
828 struct mp_graphic_object **hh = is_gr_object(L,1);
830 mp_gr_toss_object(*hh);
837 mplib_gr_tostring (lua_State *L) {
838 struct mp_graphic_object **hh = is_gr_object(L,1);
839 lua_pushfstring(L,"<object %p>",*hh);
843 #define mplib_push_number(L,x) lua_pushnumber(L,(lua_Number)(x)/65536.0)
849 mplib_push_path (lua_State *L, struct mp_knot *h, int is_pen) {
850 struct mp_knot *p; /* for scanning the path */
856 lua_createtable(L,0,6);
858 if (p->left_type_field != mp_explicit) {
859 mplib_push_S(left_type);
860 lua_pushstring(L,knot_type_enum[p->left_type_field]);
863 if (p->right_type_field != mp_explicit) {
864 mplib_push_S(right_type);
865 lua_pushstring(L,knot_type_enum[p->right_type_field]);
869 mplib_push_S(x_coord);
870 mplib_push_number(L,p->x_coord_field);
872 mplib_push_S(y_coord);
873 mplib_push_number(L,p->y_coord_field);
875 mplib_push_S(left_x);
876 mplib_push_number(L,p->left_x_field);
878 mplib_push_S(left_y);
879 mplib_push_number(L,p->left_y_field);
881 mplib_push_S(right_x);
882 mplib_push_number(L,p->right_x_field);
884 mplib_push_S(right_y);
885 mplib_push_number(L,p->right_y_field);
887 lua_rawseti(L,-2,i); i++;
888 if ( p->right_type_field==mp_endpoint ) {
898 #define set_color_objects(pq) \
899 object_color_model = pq->color_model_field; \
900 object_color_a = pq->color_field._a_val; \
901 object_color_b = pq->color_field._b_val; \
902 object_color_c = pq->color_field._c_val; \
903 object_color_d = pq->color_field._d_val;
907 mplib_push_color (lua_State *L, struct mp_graphic_object *p ) {
908 int object_color_model;
909 int object_color_a, object_color_b, object_color_c, object_color_d ;
911 if (p->_type_field == mp_fill_code) {
912 mp_fill_object *h = (mp_fill_object *)p;
913 set_color_objects(h);
914 } else if (p->_type_field == mp_stroked_code) {
915 mp_stroked_object *h = (mp_stroked_object *)p;
916 set_color_objects(h);
918 mp_text_object *h = (mp_text_object *)p;
919 set_color_objects(h);
922 lua_pushstring(L,color_model_enum[object_color_model]);
923 lua_setfield(L,-2,"model");
924 if (object_color_model == mp_rgb_model ||
925 object_color_model == mp_uninitialized_model) {
927 mplib_push_number(L,object_color_a);
929 mplib_push_number(L,object_color_b);
931 mplib_push_number(L,object_color_c);
933 lua_setfield(L,-2,"rgb");
936 if (object_color_model == mp_cmyk_model ||
937 object_color_model == mp_uninitialized_model) {
939 mplib_push_number(L,object_color_a);
941 mplib_push_number(L,object_color_b);
943 mplib_push_number(L,object_color_c);
945 mplib_push_number(L,object_color_d);
947 lua_setfield(L,-2,"cmyk");
949 if (object_color_model == mp_grey_model ||
950 object_color_model == mp_uninitialized_model) {
952 mplib_push_number(L,object_color_a);
954 lua_setfield(L,-2,"grey");
962 /* the dash scale is not exported, the field has no external value */
964 mplib_push_dash (lua_State *L, struct mp_stroked_object *h ) {
966 if (h!=NULL && h->dash_p_field != NULL) {
969 mplib_push_number(L,d->offset_field);
970 lua_setfield(L,-2,"offset");
971 if (d->array_field!=NULL ) {
974 while (*(d->array_field+i) != -1) {
975 mplib_push_number(L, *(d->array_field+1));
979 lua_setfield(L,-2,"dashes");
987 mplib_push_transform (lua_State *L, struct mp_text_object *h ) {
990 lua_createtable(L,6,0);
991 mplib_push_number(L,h->tx_field);
992 lua_rawseti(L,-2,i); i++;
993 mplib_push_number(L,h->ty_field);
994 lua_rawseti(L,-2,i); i++;
995 mplib_push_number(L,h->txx_field);
996 lua_rawseti(L,-2,i); i++;
997 mplib_push_number(L,h->tyx_field);
998 lua_rawseti(L,-2,i); i++;
999 mplib_push_number(L,h->txy_field);
1000 lua_rawseti(L,-2,i); i++;
1001 mplib_push_number(L,h->tyy_field);
1002 lua_rawseti(L,-2,i); i++;
1010 mplib_fill_field (lua_State *L, struct mp_fill_object *h, int field) {
1012 lua_pushstring(L,"fill");
1013 } else if (FIELD(path)) {
1014 mplib_push_path(L, h->path_p_field, MPLIB_PATH);
1015 } else if (FIELD(htap)) {
1016 mplib_push_path(L, h->htap_p_field, MPLIB_PATH);
1017 } else if (FIELD(pen)) {
1018 mplib_push_path(L, h->pen_p_field, MPLIB_PEN);
1019 } else if (FIELD(color)) {
1020 mplib_push_color(L,(mp_graphic_object *)h);
1021 } else if (FIELD(linejoin)) {
1022 lua_pushnumber(L,h->ljoin_field);
1023 } else if (FIELD(miterlimit)) {
1024 mplib_push_number(L,h->miterlim_field);
1025 } else if (FIELD(prescript)) {
1026 lua_pushstring(L,h->pre_script_field);
1027 } else if (FIELD(postscript)) {
1028 lua_pushstring(L,h->post_script_field);
1035 mplib_stroked_field (lua_State *L, struct mp_stroked_object *h, int field) {
1037 lua_pushstring(L,"outline");
1038 } else if (FIELD(path)) {
1039 mplib_push_path(L, h->path_p_field, MPLIB_PATH);
1040 } else if (FIELD(pen)) {
1041 mplib_push_path(L, h->pen_p_field, MPLIB_PEN);
1042 } else if (FIELD(color)) {
1043 mplib_push_color(L, (mp_graphic_object *)h);
1044 } else if (FIELD(dash)) {
1045 mplib_push_dash(L,h);
1046 } else if (FIELD(linecap)) {
1047 lua_pushnumber(L,h->lcap_field);
1048 } else if (FIELD(linejoin)) {
1049 lua_pushnumber(L,h->ljoin_field);
1050 } else if (FIELD(miterlimit)) {
1051 mplib_push_number(L,h->miterlim_field);
1052 } else if (FIELD(prescript)) {
1053 lua_pushstring(L,h->pre_script_field);
1054 } else if (FIELD(postscript)) {
1055 lua_pushstring(L,h->post_script_field);
1062 mplib_text_field (lua_State *L, struct mp_text_object *h, int field) {
1065 lua_pushstring(L,"text");
1066 } else if (FIELD(text)) {
1067 lua_pushstring(L,h->text_p_field);
1068 } else if (FIELD(dsize)) {
1069 mplib_push_number(L,(h->font_dsize_field/16));
1070 } else if (FIELD(font)) {
1071 lua_pushstring(L,h->font_name_field);
1072 } else if (FIELD(color)) {
1073 mplib_push_color(L,(mp_graphic_object *)h);
1074 } else if (FIELD(width)) {
1075 mplib_push_number(L,h->width_field);
1076 } else if (FIELD(height)) {
1077 mplib_push_number(L,h->height_field);
1078 } else if (FIELD(depth)) {
1079 mplib_push_number(L,h->depth_field);
1080 } else if (FIELD(transform)) {
1081 mplib_push_transform(L,h);
1082 } else if (FIELD(prescript)) {
1083 lua_pushstring(L,h->pre_script_field);
1084 } else if (FIELD(postscript)) {
1085 lua_pushstring(L,h->post_script_field);
1092 mplib_special_field (lua_State *L, struct mp_special_object *h, int field) {
1094 lua_pushstring(L,"special");
1095 } else if (FIELD(prescript)) {
1096 lua_pushstring(L,h->pre_script_field);
1103 mplib_start_bounds_field (lua_State *L, struct mp_bounds_object *h, int field) {
1105 lua_pushstring(L,"start_bounds");
1106 } else if (FIELD(path)) {
1107 mplib_push_path(L,h->path_p_field, MPLIB_PATH);
1114 mplib_start_clip_field (lua_State *L, struct mp_clip_object *h, int field) {
1116 lua_pushstring(L,"start_clip");
1117 } else if (FIELD(path)) {
1118 mplib_push_path(L,h->path_p_field, MPLIB_PATH);
1125 mplib_stop_bounds_field (lua_State *L, struct mp_graphic_object *h, int field) {
1126 if (h!=NULL && FIELD(type)) {
1127 lua_pushstring(L,"stop_bounds");
1134 mplib_stop_clip_field (lua_State *L, struct mp_graphic_object *h, int field) {
1135 if (h!=NULL && FIELD(type)) {
1136 lua_pushstring(L,"stop_clip");
1143 mplib_gr_fields (lua_State *L) {
1144 const char **fields;
1147 struct mp_graphic_object **hh = is_gr_object(L,1);
1149 switch ((*hh)->_type_field) {
1150 case mp_fill_code: fields = fill_fields; break;
1151 case mp_stroked_code: fields = stroked_fields; break;
1152 case mp_text_code: fields = text_fields; break;
1153 case mp_special_code: fields = special_fields; break;
1154 case mp_start_clip_code: fields = start_clip_fields; break;
1155 case mp_start_bounds_code: fields = start_bounds_fields; break;
1156 case mp_stop_clip_code: fields = stop_clip_fields; break;
1157 case mp_stop_bounds_code: fields = stop_bounds_fields; break;
1158 default: fields = no_fields;
1161 for (f = *fields; f != NULL; f++) {
1162 lua_pushstring(L,f);
1163 lua_rawseti(L,-2,i); i++;
1172 mplib_gr_index (lua_State *L) {
1173 struct mp_graphic_object **hh = is_gr_object(L,1);
1175 struct mp_graphic_object *h = *hh;
1176 switch (h->_type_field) {
1177 case mp_fill_code: mplib_fill_field(L,(mp_fill_object *)h,2); break;
1178 case mp_stroked_code: mplib_stroked_field(L,(mp_stroked_object *)h,2); break;
1179 case mp_text_code: mplib_text_field(L,(mp_text_object *)h,2); break;
1180 case mp_special_code: mplib_special_field(L,(mp_special_object *)h,2); break;
1181 case mp_start_clip_code: mplib_start_clip_field(L,(mp_clip_object *)h,2); break;
1182 case mp_start_bounds_code: mplib_start_bounds_field(L,(mp_bounds_object *)h,2); break;
1183 case mp_stop_clip_code: mplib_stop_clip_field(L,h,2); break;
1184 case mp_stop_bounds_code: mplib_stop_bounds_field(L,h,2); break;
1185 default: lua_pushnil(L);
1194 static const struct luaL_reg mplib_meta[] = {
1195 {"__gc", mplib_collect},
1196 {"__tostring", mplib_tostring},
1197 {NULL, NULL} /* sentinel */
1200 static const struct luaL_reg mplib_fig_meta[] = {
1201 {"__gc", mplib_fig_collect },
1202 {"__tostring", mplib_fig_tostring },
1203 {"objects", mplib_fig_body },
1204 {"copy_objects", mplib_fig_copy_body },
1205 {"filename", mplib_fig_filename },
1206 {"postscript", mplib_fig_postscript },
1207 {"boundingbox", mplib_fig_bb },
1208 {NULL, NULL} /* sentinel */
1211 static const struct luaL_reg mplib_gr_meta[] = {
1212 {"__gc", mplib_gr_collect },
1213 {"__tostring", mplib_gr_tostring },
1214 {"__index", mplib_gr_index },
1215 {"fields", mplib_gr_fields },
1216 {NULL, NULL} /* sentinel */
1220 static const struct luaL_reg mplib_d [] = {
1221 {"execute", mplib_execute },
1222 {"finish", mplib_finish },
1223 {NULL, NULL} /* sentinel */
1227 static const struct luaL_reg mplib_m[] = {
1229 {NULL, NULL} /* sentinel */
1234 luaopen_mp (lua_State *L) {
1236 luaL_newmetatable(L,MPLIB_GR_METATABLE);
1237 lua_pushvalue(L, -1); /* push metatable */
1238 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1239 luaL_register(L, NULL, mplib_gr_meta); /* object meta methods */
1242 luaL_newmetatable(L,MPLIB_FIG_METATABLE);
1243 lua_pushvalue(L, -1); /* push metatable */
1244 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1245 luaL_register(L, NULL, mplib_fig_meta); /* figure meta methods */
1248 luaL_newmetatable(L,MPLIB_METATABLE);
1249 lua_pushvalue(L, -1); /* push metatable */
1250 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
1251 luaL_register(L, NULL, mplib_meta); /* meta methods */
1252 luaL_register(L, NULL, mplib_d); /* dict methods */
1253 luaL_register(L, "mplib", mplib_m); /* module functions */