diff options
author | Robin Watts <robin.watts@artifex.com> | 2012-06-13 18:59:31 +0100 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2012-06-14 19:15:41 +0100 |
commit | f5d721764811484be4e98d7cddc5b83ae72e6277 (patch) | |
tree | a337b40388ee11022fa92d1261a9aba65e65131c /apps | |
parent | cd180f924225eadf5093fc075e09e7ffbf8aa546 (diff) | |
download | mupdf-f5d721764811484be4e98d7cddc5b83ae72e6277.tar.xz |
First version of mujstest-v8.pdf and associated solution changes.
Simple command line tool made from cutting all the windows specifics
out of win_main.c and adding a simple script handler in.
Read lines from the script, and feed those events to pdfapp. Screenshot
pages as required.
Diffstat (limited to 'apps')
-rw-r--r-- | apps/jstest_main.c | 302 | ||||
-rw-r--r-- | apps/pdfapp.c | 2 | ||||
-rw-r--r-- | apps/pdfapp.h | 1 |
3 files changed, 304 insertions, 1 deletions
diff --git a/apps/jstest_main.c b/apps/jstest_main.c new file mode 100644 index 00000000..69cab0b5 --- /dev/null +++ b/apps/jstest_main.c @@ -0,0 +1,302 @@ +#include "fitz.h" +#include "mupdf.h" +#include "muxps.h" +#include "mucbz.h" +#include "pdfapp.h" +#include <ctype.h> + +static pdfapp_t gapp; +static int file_open = 0; +static char filename[1024] = ""; +static char *scriptname; +static char *output; +static int shotcount = 0; + +static char getline_buffer[1024]; + +void winwarn(pdfapp_t *app, char *msg) +{ + fprintf(stderr, "warning: %s", msg); +} + +void winerror(pdfapp_t *app, char *msg) +{ + fprintf(stderr, msg); + exit(1); +} + +static char pd_filename[256] = "The file is encrypted."; +static char pd_password[256] = ""; +static char td_textinput[1024] = ""; +static int pd_okay = 0; + +char *winpassword(pdfapp_t *app, char *filename) +{ + if (pd_password[0] == 0) + return NULL; + return pd_password; +} + +char *wintextinput(pdfapp_t *app, char *inittext) +{ + if (td_textinput[0] != 0) + return td_textinput; + return inittext; +} + +void winhelp(pdfapp_t*app) +{ +} + +void winclose(pdfapp_t *app) +{ + pdfapp_close(app); + exit(0); +} + +void wincursor(pdfapp_t *app, int curs) +{ +} + +void wintitle(pdfapp_t *app, char *title) +{ +} + +void windrawrect(pdfapp_t *app, int x0, int y0, int x1, int y1) +{ +} + +void windrawstring(pdfapp_t *app, int x, int y, char *s) +{ +} + +void winresize(pdfapp_t *app, int w, int h) +{ +} + +void winrepaint(pdfapp_t *app) +{ +} + +void winrepaintsearch(pdfapp_t *app) +{ +} + +void winfullscreen(pdfapp_t *app, int state) +{ +} + +/* + * Event handling + */ + +void windocopy(pdfapp_t *app) +{ +} + +void winreloadfile(pdfapp_t *app) +{ + pdfapp_close(app); + pdfapp_open(app, filename, 1); +} + +void winopenuri(pdfapp_t *app, char *buf) +{ +} + +static void +usage(void) +{ + fprintf(stderr, "mujstest: Scriptable tester for mupdf + js\n"); + fprintf(stderr, "\nSyntax: mujstest -o <filename> <scriptfile>\n"); + fprintf(stderr, "\n<filename> should sensibly be of the form file-%d.png\n"); + fprintf(stderr, "\nscriptfile contains a list of commands:\n"); + fprintf(stderr, "\tPASSWORD <password>\tSet the password\n"); + fprintf(stderr, "\tOPEN <filename>\tOpen a file\n"); + fprintf(stderr, "\tGOTO <page>\tJump to a particular page\n"); + fprintf(stderr, "\tSCREENSHOT\tSave a screenshot\n"); + fprintf(stderr, "\tRESIZE <w> <h>\tResize the screen to a given size\n"); + fprintf(stderr, "\tCLICK <x> <y> <btn>\tClick at a given position\n"); + fprintf(stderr, "\tTEXT <string>\tSet a value to be entered\n"); + exit(1); +} + +static char * +getline(FILE *file) +{ + char c; + char *d = getline_buffer; + + /* Skip over any prefix of whitespace */ + do + { + c = fgetc(file); + } + while (isspace(c)); + + if (c < 0) + return NULL; + + do + { + *d++ = c; + c = fgetc(file); + } + while (c >= 32); + + *d = 0; + + return getline_buffer; +} + +static int +match(char **line, const char *match) +{ + char *s = *line; + + if (s == NULL) + return 0; + + while (isspace(*s)) + s++; + + while (*s == *match) + { + s++; + match++; + } + + if (*match != 0) + return 0; + + /* We matched! Skip over any whitespace */ + while (isspace(*s)) + s++; + + *line = s; + + /* Trim whitespace off the end of the line */ + /* Run to the end of the line */ + while (*s) + s++; + + /* Run back until we find where we started, or non whitespace */ + while (s != *line && isspace(s[-1])) + s--; + + /* Remove the suffix of whitespace */ + *s = 0; + + return 1; +} + +int +main(int argc, char *argv[]) +{ + fz_context *ctx; + FILE *script = NULL; + int c; + + while ((c = fz_getopt(argc, argv, "o:")) != -1) + { + switch(c) + { + case 'o': output = fz_optarg; break; + default: usage(); break; + } + } + + if (fz_optind == argc) + usage(); + + ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); + if (!ctx) + { + fprintf(stderr, "cannot initialise context\n"); + exit(1); + } + pdfapp_init(ctx, &gapp); + gapp.scrw = 640; + gapp.scrh = 480; + + fz_try(ctx) + { + while (fz_optind < argc) + { + scriptname = argv[fz_optind++]; + script = fopen(scriptname, "rb"); + if (script == NULL) + fz_throw(ctx, "cannot open script: %s", scriptname); + + do + { + char *line = getline(script); + if (match(&line, "%")) + { + /* Comment */ + } + else if (match(&line, "PASSWORD")) + { + strcpy(pd_password, line); + } + else if (match(&line, "OPEN")) + { + if (file_open) + pdfapp_close(&gapp); + strcpy(filename, line); + pdfapp_open(&gapp, line, 0); + file_open = 1; + } + else if (match(&line, "GOTO")) + { + pdfapp_gotopage(&gapp, atoi(line)-1); + } + else if (match(&line, "SCREENSHOT")) + { + char text[1024]; + + sprintf(text, output, ++shotcount); + fz_write_png(ctx, gapp.image, text, 0); + } + else if (match(&line, "RESIZE")) + { + int w, h; + sscanf(line, "%d %d", &w, &h); + pdfapp_onresize(&gapp, w, h); + } + else if (match(&line, "CLICK")) + { + float x, y, b; + int n; + n = sscanf(line, "%f %f %d", &x, &y, &b); + if (n < 1) + x = 0.0f; + if (n < 2) + y = 0.0f; + if (n < 3) + b = 1; + /* state = 1 = transition down */ + pdfapp_onmouse(&gapp, (int)x, (int)y, b, 0, 1); + /* state = -1 = transition up */ + pdfapp_onmouse(&gapp, (int)x, (int)y, b, 0, -1); + } + else if (match(&line, "TEXT")) + { + strcpy(td_textinput, line); + } + } + while (!feof(script)); + + fclose(script); + } + } + fz_catch(ctx) + { + fprintf(stderr, "error: cannot execute '%s'\n", scriptname); + } + + if (file_open) + pdfapp_close(&gapp); + + return 0; +} diff --git a/apps/pdfapp.c b/apps/pdfapp.c index de08a1ce..c37163cf 100644 --- a/apps/pdfapp.c +++ b/apps/pdfapp.c @@ -412,7 +412,7 @@ static void pdfapp_gotouri(pdfapp_t *app, char *uri) winopenuri(app, uri); } -static void pdfapp_gotopage(pdfapp_t *app, int number) +void pdfapp_gotopage(pdfapp_t *app, int number) { app->isediting = 0; winrepaint(app); diff --git a/apps/pdfapp.h b/apps/pdfapp.h index bfad92bd..75030c19 100644 --- a/apps/pdfapp.h +++ b/apps/pdfapp.h @@ -114,6 +114,7 @@ void pdfapp_onkey(pdfapp_t *app, int c); void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state); void pdfapp_oncopy(pdfapp_t *app, unsigned short *ucsbuf, int ucslen); void pdfapp_onresize(pdfapp_t *app, int w, int h); +void pdfapp_gotopage(pdfapp_t *app, int number); void pdfapp_invert(pdfapp_t *app, fz_bbox rect); void pdfapp_inverthit(pdfapp_t *app); |