void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload) { if (strstr(filename, ".xps") || strstr(filename, ".XPS") || strstr(filename, ".rels")) pdfapp_open_xps(app, filename, fd); else pdfapp_open_pdf(app, filename, fd); app->cache = fz_new_glyph_cache(); if (app->pageno < 1) app->pageno = 1; if (app->pageno > app->pagecount) app->pageno = app->pagecount; if (app->resolution < MINRES) app->resolution = MINRES; if (app->resolution > MAXRES) app->resolution = MAXRES; if (!reload) { app->shrinkwrap = 1; app->rotate = 0; app->panx = 0; app->pany = 0; } pdfapp_showpage(app, 1, 1, 1); }
static void pdfapp_searchbackward(pdfapp_t *app, enum panning *panto) { int matchlen; int test; int len; int startpage; wincursor(app, WAIT); startpage = app->pageno; do { len = textlen(app->page_text); if (app->hit >= 0) test = app->hit - 1; else test = len; while (test >= 0) { matchlen = match(app->search, app->page_text, test); if (matchlen) { app->hit = test; app->hitlen = matchlen; wincursor(app, HAND); winrepaint(app); return; } test--; } app->pageno--; if (app->pageno < 1) app->pageno = app->pagecount; pdfapp_showpage(app, 1, 0, 0); *panto = PAN_TO_BOTTOM; } while (app->pageno != startpage); if (app->pageno == startpage) { pdfapp_warn(app, "String '%s' not found.", app->search); winrepaintsearch(app); } else winrepaint(app); wincursor(app, HAND); }
static void pdfapp_searchforward(pdfapp_t *app, enum panning *panto) { int matchlen; int test; int len; int startpage; wincursor(app, WAIT); startpage = app->pageno; do { len = textlen(app->page_text); if (app->hit >= 0) test = app->hit + strlen(app->search); else test = 0; while (test < len) { matchlen = match(app->search, app->page_text, test); if (matchlen) { app->hit = test; app->hitlen = matchlen; wincursor(app, HAND); winrepaint(app); return; } test++; } app->pageno++; if (app->pageno > app->pagecount) app->pageno = 1; pdfapp_showpage(app, 1, 0, 0); *panto = PAN_TO_TOP; } while (app->pageno != startpage); if (app->pageno == startpage) { pdfapp_warn(app, "String '%s' not found.", app->search); winrepaintsearch(app); } else winrepaint(app); wincursor(app, HAND); }
static void pdfapp_gotopage(pdfapp_t *app, fz_obj *obj) { int page; page = pdf_findpageobject(app->xref, obj); if (app->histlen + 1 == 256) { memmove(app->hist, app->hist + 1, sizeof(int) * 255); app->histlen --; } app->hist[app->histlen++] = app->pageno; app->pageno = page; pdfapp_showpage(app, 1, 1); }
static void pdfapp_searchbackward(pdfapp_t *app) { int matchlen; int test; int len; int startpage; wincursor(app, WAIT); startpage = app->pageno; do { len = textlen(app->page_text); if (app->hit >= 0) test = app->hit - 1; else test = len; while (test >= 0) { matchlen = match(app->search, app->page_text, test); if (matchlen) { app->hit = test; app->hitlen = matchlen; wincursor(app, HAND); return; } test--; } app->pageno--; if (app->pageno < 1) app->pageno = app->pagecount; pdfapp_showpage(app, 1, 0, 0); app->pany = -2000; } while (app->pageno != startpage); if (app->pageno == startpage) printf("hit not found\n"); wincursor(app, HAND); }
static void pdfapp_searchforward(pdfapp_t *app) { int matchlen; int test; int len; int startpage; wincursor(app, WAIT); startpage = app->pageno; do { len = textlen(app->page_text); if (app->hit >= 0) test = app->hit + strlen(app->search); else test = 0; while (test < len) { matchlen = match(app->search, app->page_text, test); if (matchlen) { app->hit = test; app->hitlen = matchlen; wincursor(app, HAND); return; } test++; } app->pageno++; if (app->pageno > app->pagecount) app->pageno = 1; pdfapp_showpage(app, 1, 0, 0); app->pany = 0; } while (app->pageno != startpage); if (app->pageno == startpage) printf("hit not found\n"); wincursor(app, HAND); }
static void pdfapp_gotopage(pdfapp_t *app, fz_obj *obj) { int number; number = pdf_find_page_number(app->xref, obj); if (number < 0) return; if (app->histlen + 1 == 256) { memmove(app->hist, app->hist + 1, sizeof(int) * 255); app->histlen --; } app->hist[app->histlen++] = app->pageno; app->pageno = number + 1; pdfapp_showpage(app, 1, 1, 1); }
void pdfapp_gotopage(pdfapp_t *app, fz_obj *obj) { int oid = fz_tonum(obj); int i; for (i = 0; i < pdf_getpagecount(app->pages); i++) { if (fz_tonum(app->pages->pref[i]) == oid) { if (app->histlen + 1 == 256) { memmove(app->hist, app->hist + 1, sizeof(int) * 255); app->histlen --; } app->hist[app->histlen++] = app->pageno; app->pageno = i + 1; pdfapp_showpage(app, 1, 1); return; } } }
void pdfapp_open(pdfapp_t *app, char *filename) { fz_error *error; fz_obj *obj; char *password = ""; /* * Open PDF and load xref table */ app->filename = filename; error = pdf_newxref(&app->xref); if (error) pdfapp_error(app, error); error = pdf_loadxref(app->xref, filename); if (error) { if (!strncmp(error->msg, "ioerror", 7)) pdfapp_error(app, error); pdfapp_warn(app, "There was a problem with file \"%s\".\n" "It may be corrupted, or generated by broken software.\n\n" "%s\n\nTrying to continue anyway...", filename, error->msg); error = pdf_repairxref(app->xref, filename); if (error) pdfapp_error(app, error); } /* * Handle encrypted PDF files */ error = pdf_decryptxref(app->xref); if (error) pdfapp_error(app, error); if (app->xref->crypt) { error = pdf_setpassword(app->xref->crypt, password); while (error) { fz_droperror(error); password = winpassword(app, filename); if (!password) exit(1); error = pdf_setpassword(app->xref->crypt, password); if (error) pdfapp_warn(app, "Invalid password."); } } /* * Load page tree */ error = pdf_loadpagetree(&app->pages, app->xref); if (error) pdfapp_error(app, error); /* * Load meta information * TODO: move this into mupdf library */ obj = fz_dictgets(app->xref->trailer, "Root"); if (!obj) pdfapp_error(app, fz_throw("syntaxerror: missing Root object")); error = pdf_loadindirect(&app->xref->root, app->xref, obj); if (error) pdfapp_error(app, error); obj = fz_dictgets(app->xref->trailer, "Info"); if (obj) { error = pdf_loadindirect(&app->xref->info, app->xref, obj); if (error) pdfapp_error(app, error); } error = pdf_loadnametrees(app->xref); if (error) pdfapp_error(app, error); error = pdf_loadoutline(&app->outline, app->xref); if (error) pdfapp_error(app, error); app->doctitle = filename; if (strrchr(app->doctitle, '\\')) app->doctitle = strrchr(app->doctitle, '\\') + 1; if (strrchr(app->doctitle, '/')) app->doctitle = strrchr(app->doctitle, '/') + 1; if (app->xref->info) { obj = fz_dictgets(app->xref->info, "Title"); if (obj) { error = pdf_toutf8(&app->doctitle, obj); if (error) pdfapp_error(app, error); } } /* * Start at first page */ app->shrinkwrap = 1; if (app->pageno < 1) app->pageno = 1; if (app->zoom <= 0.0) app->zoom = 1.0; app->rotate = 0; app->panx = 0; app->pany = 0; pdfapp_showpage(app, 1, 1); }
void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state) { pdf_link *link; fz_matrix ctm; fz_point p; p.x = x - app->panx + app->image->x; p.y = y - app->pany + app->image->y; ctm = pdfapp_viewctm(app); ctm = fz_invertmatrix(ctm); p = fz_transformpoint(ctm, p); for (link = app->page->links; link; link = link->next) { if (p.x >= link->rect.x0 && p.x <= link->rect.x1) if (p.y >= link->rect.y0 && p.y <= link->rect.y1) break; } if (link) { wincursor(app, HAND); if (btn == 1 && state == 1) { if (fz_isstring(link->dest)) pdfapp_gotouri(app, link->dest); if (fz_isindirect(link->dest)) pdfapp_gotopage(app, link->dest); return; } } else { wincursor(app, ARROW); } if (state == 1) { if (btn == 1 && !app->iscopying) { app->ispanning = 1; app->selx = x; app->sely = y; } if (btn == 3 && !app->ispanning) { app->iscopying = 1; app->selx = x; app->sely = y; app->selr.x0 = x; app->selr.x1 = x; app->selr.y0 = y; app->selr.y1 = y; } if (btn == 4 || btn == 5) /* scroll wheel */ { int dir = btn == 4 ? 1 : -1; app->ispanning = app->iscopying = 0; if (modifiers & (1<<2)) { /* zoom in/out if ctrl is pressed */ app->zoom += 0.1 * dir; if (app->zoom > 3.0) app->zoom = 3.0; if (app->zoom < 0.1) app->zoom = 0.1; pdfapp_showpage(app, 0, 1); } else { /* scroll up/down, or left/right if shift is pressed */ int isx = (modifiers & (1<<0)); int xstep = isx ? 20 * dir : 0; int ystep = !isx ? 20 * dir : 0; pdfapp_panview(app, app->panx + xstep, app->pany + ystep); } } } else if (state == -1) { if (app->iscopying) { app->iscopying = 0; app->selr.x0 = MIN(app->selx, x); app->selr.x1 = MAX(app->selx, x); app->selr.y0 = MIN(app->sely, y); app->selr.y1 = MAX(app->sely, y); winrepaint(app); if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1) windocopy(app); } if (app->ispanning) app->ispanning = 0; } else if (app->ispanning) { int newx = app->panx + x - app->selx; int newy = app->pany + y - app->sely; pdfapp_panview(app, newx, newy); app->selx = x; app->sely = y; } else if (app->iscopying) { app->selr.x0 = MIN(app->selx, x); app->selr.x1 = MAX(app->selx, x); app->selr.y0 = MIN(app->sely, y); app->selr.y1 = MAX(app->sely, y); winrepaint(app); } }
void pdfapp_onkey(pdfapp_t *app, int c) { int oldpage = app->pageno; enum panning panto = PAN_TO_TOP; /* * Save numbers typed for later */ if (c >= '0' && c <= '9') { app->number[app->numberlen++] = c; app->number[app->numberlen] = '\0'; } switch (c) { /* * Zoom and rotate */ case '+': case '=': app->zoom += 0.1; if (app->zoom > 3.0) app->zoom = 3.0; pdfapp_showpage(app, 0, 1); break; case '-': app->zoom -= 0.1; if (app->zoom < 0.1) app->zoom = 0.1; pdfapp_showpage(app, 0, 1); break; case 'l': case '<': app->rotate -= 90; pdfapp_showpage(app, 0, 1); break; case 'r': case '>': app->rotate += 90; pdfapp_showpage(app, 0, 1); break; case 'a': app->rotate -= 15; pdfapp_showpage(app, 0, 1); break; case 's': app->rotate += 15; pdfapp_showpage(app, 0, 1); break; /* * Pan view, but dont need to repaint image */ case 'w': app->shrinkwrap = 1; app->panx = app->pany = 0; pdfapp_showpage(app, 0, 0); break; case 'd': app->pany -= app->image->h / 10; pdfapp_showpage(app, 0, 0); break; case 'u': app->pany += app->image->h / 10; pdfapp_showpage(app, 0, 0); break; case ',': app->panx += app->image->w / 10; pdfapp_showpage(app, 0, 0); break; case '.': app->panx -= app->image->w / 10; pdfapp_showpage(app, 0, 0); break; /* * Page navigation */ case 'g': case '\n': case '\r': if (app->numberlen > 0) app->pageno = atoi(app->number); break; case 'G': app->pageno = app->pagecount; break; case 'm': if (app->histlen + 1 == 256) { memmove(app->hist, app->hist + 1, sizeof(int) * 255); app->histlen --; } app->hist[app->histlen++] = app->pageno; break; case 't': if (app->histlen > 0) app->pageno = app->hist[--app->histlen]; break; /* * Back and forth ... */ case 'p': panto = PAN_TO_BOTTOM; if (app->numberlen > 0) app->pageno -= atoi(app->number); else app->pageno--; break; case 'n': panto = PAN_TO_TOP; if (app->numberlen > 0) app->pageno += atoi(app->number); else app->pageno++; break; case 'b': case '\b': panto = DONT_PAN; if (app->numberlen > 0) app->pageno -= atoi(app->number); else app->pageno--; break; case 'f': case ' ': panto = DONT_PAN; if (app->numberlen > 0) app->pageno += atoi(app->number); else app->pageno++; break; case 'B': panto = PAN_TO_TOP; app->pageno -= 10; break; case 'F': panto = PAN_TO_TOP; app->pageno += 10; break; } if (c < '0' || c > '9') app->numberlen = 0; if (app->pageno < 1) app->pageno = 1; if (app->pageno > app->pagecount) app->pageno = app->pagecount; if (app->pageno != oldpage) { switch (panto) { case PAN_TO_TOP: app->pany = 0; break; case PAN_TO_BOTTOM: app->pany = -2000; break; case DONT_PAN: break; } pdfapp_showpage(app, 1, 1); } }
int main(int argc, char **argv) { int c; int len; char buf[128]; KeySym keysym; int oldx = 0; int oldy = 0; int resolution = 72; int pageno = 1; int pagemark = 0; int accelerate = 1; int fd; fd_set fds; int width = -1; int height = -1; while ((c = fz_getopt(argc, argv, "p:r:b:A")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'r': resolution = atoi(fz_optarg); break; case 'A': accelerate = 0; break; case 'b': fz_set_aa_level(atoi(fz_optarg)); break; default: usage(); } } if (resolution < MINRES) resolution = MINRES; if (resolution > MAXRES) resolution = MAXRES; if (argc - fz_optind == 0) usage(); if (accelerate) fz_accelerate(); filename = argv[fz_optind++]; winopen(); pdfapp_init(&gapp); gapp.scrw = DisplayWidth(xdpy, xscr); gapp.scrh = DisplayHeight(xdpy, xscr); gapp.resolution = resolution; if (argc - fz_optind > 1) pageno = atoi(argv[fz_optind++]); gapp.pageno = pageno; fd = open(filename, O_BINARY | O_RDONLY, 0666); if (fd < 0) winerror(&gapp, fz_throw("cannot open file '%s'", filename)); pdfapp_open(&gapp, filename, fd, 0); if (pdfapp_calcfilehash(&gapp, fd)) { pdfapp_getmarkfile(&gapp); pagemark = pdfapp_getpgmark(&gapp); if (pagemark > 0) { if (pagemark <= gapp.pagecount) { gapp.pageno = pagemark; pdfapp_showpage(&gapp, 1, 1, 1); } else pdfapp_closemarkfile(&gapp); } } FD_ZERO(&fds); FD_SET(x11fd, &fds); signal(SIGHUP, signal_handler); atexit(exit_cb); while (!closing) { do { XNextEvent(xdpy, &xevt); switch (xevt.type) { case Expose: dirty = 1; break; case ConfigureNotify: if (gapp.image) { if (xevt.xconfigure.width != reqw || xevt.xconfigure.height != reqh) gapp.shrinkwrap = 0; } width = xevt.xconfigure.width; height = xevt.xconfigure.height; break; case KeyPress: len = XLookupString(&xevt.xkey, buf, sizeof buf, &keysym, NULL); if (!gapp.isediting) switch (keysym) { case XK_Escape: len = 1; buf[0] = '\033'; break; case XK_Up: len = 1; buf[0] = 'k'; break; case XK_Down: len = 1; buf[0] = 'j'; break; case XK_Left: len = 1; buf[0] = 'b'; break; case XK_Right: len = 1; buf[0] = ' '; break; case XK_Page_Up: len = 1; buf[0] = ','; break; case XK_Page_Down: len = 1; buf[0] = '.'; break; } if (len) onkey(buf[0]); onmouse(oldx, oldy, 0, 0, 0); break; case MotionNotify: oldx = xevt.xbutton.x; oldy = xevt.xbutton.y; onmouse(xevt.xbutton.x, xevt.xbutton.y, xevt.xbutton.button, xevt.xbutton.state, 0); break; case ButtonPress: onmouse(xevt.xbutton.x, xevt.xbutton.y, xevt.xbutton.button, xevt.xbutton.state, 1); break; case ButtonRelease: copytime = xevt.xbutton.time; onmouse(xevt.xbutton.x, xevt.xbutton.y, xevt.xbutton.button, xevt.xbutton.state, -1); break; case SelectionRequest: onselreq(xevt.xselectionrequest.requestor, xevt.xselectionrequest.selection, xevt.xselectionrequest.target, xevt.xselectionrequest.property, xevt.xselectionrequest.time); break; case ClientMessage: if (xevt.xclient.format == 32 && xevt.xclient.data.l[0] == WM_DELETE_WINDOW) closing = 1; break; } } while (!closing && XPending(xdpy)); if (closing) continue; if (width != -1 || height != -1) { pdfapp_onresize(&gapp, width, height); width = -1; height = -1; } if (dirty || dirtysearch) { if (dirty) winblit(&gapp); else if (dirtysearch) winblitsearch(&gapp); dirty = 0; dirtysearch = 0; } if (XPending(xdpy)) continue; if (select(x11fd + 1, &fds, NULL, NULL, NULL) < 0) { if (reloading) { winreloadfile(&gapp); reloading = 0; } } } pdfapp_close(&gapp); XDestroyWindow(xdpy, xwin); XFreePixmap(xdpy, xicon); XFreeCursor(xdpy, xcwait); XFreeCursor(xdpy, xchand); XFreeCursor(xdpy, xcarrow); XFreeGC(xdpy, xgc); XCloseDisplay(xdpy); return 0; }
int main(int argc, char** argv) { int result; int fd; fz_context *ctx; char buf[10]; int c; int pagewidth, pageheight; result = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); if(result != 0) { printf("Could not init SDL: %s\n", SDL_GetError()); exit(1); } PDL_Init(0); PDL_SetOrientation(PDL_ORIENTATION_0); PDL_RegisterJSHandler("flipPage",flip_page); PDL_RegisterJSHandler("panPage",pan_page); // PDL_RegisterJSHandler("pause", pause); // PDL_RegisterJSHandler("resume", resume); PDL_JSRegistrationComplete(); PDL_CallJS("ready", NULL, 0); memset(&pitch_action, 0, sizeof(PDF_Pitch_t)); //gesture_data = (Gesture_Event *)malloc(sizeof(Gesture_Event)); memset(&gesture_data, 0, sizeof(Gesture_Event)); pitch_action.scale = 1.0; Surface = SDL_SetVideoMode(0, 0, 0, SDL_SWSURFACE); SDL_FillRect(Surface, NULL, 0xF0F0F); SDL_Flip(Surface); ctx = fz_new_context(NULL, FZ_STORE_DEFAULT); pdfapp_init(ctx, &app); app.scrw = 1024; app.scrh = 768; app.pageno = 10; src.x = 20; src.y = 20; src.w = 1024; src.h = 768; dest.x = 0; dest.y = 0; fd = open(argv[1], O_RDONLY, 0666); if (fd < 0) { printf("cannot open file\n"); fz_throw(app.ctx, "cannot open file '%s'", argv[1]); } pdfapp_open(&app,argv[1],fd,0); //app.rotate = 90 * 3; pdfapp_showpage(&app,1,1,1); draw_pdf(); #if 0 content = SDL_CreateRGBSurfaceFrom(app.image->samples, app.image->w, app.image->h, 32, app.image->w * 4, 0x00, 0x00, 0x00, 0x00); SDL_BlitSurface(content,NULL, Surface, NULL); SDL_Flip(Surface); #endif SDL_Event Event; Event.type = SDL_NOEVENT; int temp1, temp2; do { if (Paused) { SDL_WaitEvent(&Event); if (Event.type == SDL_ACTIVEEVENT) { if ((Event.active.state & SDL_APPACTIVE) && (Event.active.gain == 1)) { Paused = SDL_FALSE; } } } else { while(SDL_PollEvent(&Event)) { handle_events(Event); // switch (Event.type) { #if 0 case SDL_MOUSEBUTTONDOWN: fingerNum = Event.button.which + 1; mouse_state = SDL_MOUSEBUTTONDOWN; printf("%2d fingers touch down\n",Event.button.which); break; case SDL_MOUSEBUTTONUP: if(fingerNum == 2) { memset(&pitch_action, 0, sizeof(PDF_Pitch_t)-sizeof(float32)); pinch = SDL_FALSE; //pitch_action.scale = 1.0; } if(fingerNum == 1 && mouse_state == SDL_MOUSEBUTTONDOWN) { if(Event.button.x > (app.image->h / 2)) { buf[0] = '.'; } else { buf[0] = ','; } pdfapp_onkey(&app,buf[0]); printf("Flip page\n"); draw_pdf(); } // printf("%2d fingers touching\n",fingerNum); mouse_state = NULL; printf("%2d fingers touch up\n",Event.button.which); if(fingerNum > 0) fingerNum -= 1; break; case SDL_MOUSEMOTION: if(fingerNum == 2) { if(Event.motion.which == 0) { pitch_action.x0 = Event.motion.x; pitch_action.y0 = Event.motion.y; } if(Event.motion.which == 1) { pitch_action.x1 = Event.motion.x; pitch_action.y1 = Event.motion.y; } pitch_action.cx = (pitch_action.x0 + pitch_action.x1)/2; pitch_action.cy = (pitch_action.y0 + pitch_action.y1)/2; temp1 = pow((pitch_action.x0 - pitch_action.x1),2); temp2 = pow((pitch_action.y0 - pitch_action.y1),2); pitch_action.distance1 = sqrt(temp1 + temp2); pitch_action.offset = pitch_action.distance1 - pitch_action.distance0; pitch_action.distance0 = pitch_action.distance1; pitch_action.offset = (int)pitch_action.offset; // printf("cx:%4d, cy:%4d, offset:%4f \n",pitch_action.cx, // pitch_action.cy, pitch_action.offset); #if 1 if(pitch_action.offset > 0 && pitch_action.offset < 5) { #if 0 app.resolution *= (Uint16)pitch_action.offset; if (app.resolution > MAXRES) app.resolution = MAXRES; pdfapp_showpage(&app, 0, 1, 0); draw_pdf(); #endif pinch = SDL_TRUE; pitch_action.scale += 0.008; if(pitch_action.scale > 2) pitch_action.scale = 2; printf("-------------------------------------pinch out\n"); } else if(pitch_action.offset < 0) { #if 0 app.resolution /=(Uint16)( abs(pitch_action.offset)); if (app.resolution < MINRES) app.resolution = MINRES; pdfapp_showpage(&app, 0, 1, 0); draw_pdf(); #endif pinch = SDL_TRUE; pitch_action.scale -= 0.008; if(pitch_action.scale < 0.9) pitch_action.scale = 0.9; printf("----------------------------------pinch in\n"); } else { pinch = SDL_FALSE; } #endif } //printf("which: %d, x = %4d, y = %4d \n",Event.motion.which, // Event.motion.x, Event.motion.y); mouse_state = SDL_MOUSEMOTION; printf("Finger move \n"); break; #endif /* // handle deactivation by pausing our animation case SDL_ACTIVEEVENT: if ((Event.active.state & SDL_APPACTIVE) && (Event.active.gain == 0)) { Paused = SDL_TRUE; } break; default: break; // } */ } #if 1 if(pinch) { #if 1 if(pitch_action.scale == 1.4) { app.resolution = 72 * 1.4; pdfapp_showpage(&app,0,1,1); draw_pdf(); printf("----increase pdf resolution \n"); } if(pitch_action.scale == 1) { app.resolution = 72; pdfapp_showpage(&app,0,1,1); draw_pdf(); } content = zoomSurface(page_copy,pitch_action.scale,pitch_action.scale,SMOOTHING_OFF); SDL_FillRect(Surface, NULL, SDL_MapRGBA(Surface->format, 45, 45, 45, 0)); SDL_BlitSurface(content,NULL, Surface, NULL); SDL_Flip(Surface); SDL_FreeSurface(content); #else app.resolution *= pitch_action.scale; if(app.resolution > MAXRES) app.resolution = MAXRES; if (app.resolution < MINRES) app.resolution = MINRES; pdfapp_showpage(&app, 0, 1, 1); draw_pdf(); #endif } #endif SDL_Delay(0); } }while(Event.type != SDL_QUIT); //cleanup SDL_FreeSurface(content); SDL_FreeSurface(Surface); pdfapp_close(&app); PDL_Quit(); SDL_Quit(); return 0; }
void pdfapp_open(pdfapp_t *app, char *filename) { fz_obj *obj; fz_obj *info; char *password = ""; /* * Open PDF and load xref table */ app->filename = filename; app->xref = pdf_openxref(filename); if (!app->xref) pdfapp_error(app, -1); /* * Handle encrypted PDF files */ if (pdf_needspassword(app->xref)) { int okay = pdf_authenticatepassword(app->xref, password); while (!okay) { password = winpassword(app, filename); if (!password) exit(1); okay = pdf_authenticatepassword(app->xref, password); if (!okay) pdfapp_warn(app, "Invalid password."); } } /* * Load meta information */ app->outline = pdf_loadoutline(app->xref); app->doctitle = filename; if (strrchr(app->doctitle, '\\')) app->doctitle = strrchr(app->doctitle, '\\') + 1; if (strrchr(app->doctitle, '/')) app->doctitle = strrchr(app->doctitle, '/') + 1; info = fz_dictgets(app->xref->trailer, "Info"); if (info) { obj = fz_dictgets(info, "Title"); if (obj) app->doctitle = pdf_toutf8(obj); } /* * Start at first page */ app->pagecount = pdf_getpagecount(app->xref); app->shrinkwrap = 1; if (app->pageno < 1) app->pageno = 1; if (app->zoom < 0.1) app->zoom = 0.1; if (app->zoom > 3.0) app->zoom = 3.0; app->rotate = 0; app->panx = 0; app->pany = 0; //code change by kakai //highlight color settings kno_setHighlightColor(app, 0x00ffff00); //code change by kakai pdfapp_showpage(app, 1, 1); }
void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state) { pdf_link *link; fz_matrix ctm; fz_point p; p.x = x - app->panx + app->image->x; p.y = y - app->pany + app->image->y; ctm = pdfapp_viewctm(app); ctm = fz_invertmatrix(ctm); p = fz_transformpoint(ctm, p); for (link = app->page->links; link; link = link->next) { if (p.x >= link->rect.x0 && p.x <= link->rect.x1) if (p.y >= link->rect.y0 && p.y <= link->rect.y1) break; } if (link) { wincursor(app, HAND); if (btn == 1 && state == 1) { if (link->kind == PDF_LURI) pdfapp_gotouri(app, link->dest); else if (link->kind == PDF_LGOTO) pdfapp_gotopage(app, link->dest); return; } } else { wincursor(app, ARROW); } if (state == 1) { if (btn == 1 && !app->iscopying) { app->ispanning = 1; app->selx = x; app->sely = y; } if (btn == 3 && !app->ispanning) { kno_clearselect(app); //code change by kakai app->iscopying = 1; app->selx = x; app->sely = y; app->selr.x0 = x; app->selr.x1 = x; app->selr.y0 = y; app->selr.y1 = y; } if (btn == 4 || btn == 5) /* scroll wheel */ { int dir = btn == 4 ? 1 : -1; app->ispanning = app->iscopying = 0; if (modifiers & (1<<2)) { /* zoom in/out if ctrl is pressed */ app->zoom += 0.1 * dir; if (app->zoom > 3.0) app->zoom = 3.0; if (app->zoom < 0.1) app->zoom = 0.1; pdfapp_showpage(app, 0, 1); } else { /* scroll up/down, or left/right if shift is pressed */ int isx = (modifiers & (1<<0)); int xstep = isx ? 20 * dir : 0; int ystep = !isx ? 20 * dir : 0; pdfapp_panview(app, app->panx + xstep, app->pany + ystep); } } } else if (state == -1) { //Code change by Kakai //Hit testing kno_hitdata *hitdata = kno_gethitdata(app, x, y); printf("hit test char is: %c\n", hitdata->ucs); //Code change by Kakai if (app->iscopying) { app->iscopying = 0; app->selr.x0 = MIN(app->selx, x); app->selr.x1 = MAX(app->selx, x); app->selr.y0 = MIN(app->sely, y); app->selr.y1 = MAX(app->sely, y); winrepaint(app); if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1) windocopy(app); } if (app->ispanning) app->ispanning = 0; } else if (app->ispanning) { int newx = app->panx + x - app->selx; int newy = app->pany + y - app->sely; pdfapp_panview(app, newx, newy); app->selx = x; app->sely = y; } else if (app->iscopying) { app->selr.x0 = MIN(app->selx, x); app->selr.x1 = MAX(app->selx, x); app->selr.y0 = MIN(app->sely, y); app->selr.y1 = MAX(app->sely, y); //code change by kakai //IsHighlightable and selection testing int closestx, closesty; closestx = closesty = 0; if (kno_ishighlightable(app, x, y, &closestx, &closesty) == 1) kno_onselect(app); //code change by kakai else { printf("x is %d\n", closestx); printf("y is %d\n", closesty); } //code change by kakai winrepaint(app); } }
void pdfapp_onkey(pdfapp_t *app, int c) { int oldpage = app->pageno; enum panning panto = PAN_TO_TOP; int loadpage = 1; if (app->isediting) { int n = strlen(app->search); if (c < ' ') { if (c == '\b' && n > 0) { app->search[n - 1] = 0; winrepaintsearch(app); } if (c == '\n' || c == '\r') { app->isediting = 0; if (n > 0) { winrepaintsearch(app); pdfapp_onkey(app, 'n'); } else winrepaint(app); } if (c == '\033') { app->isediting = 0; winrepaint(app); } } else { if (n + 2 < sizeof app->search) { app->search[n] = c; app->search[n + 1] = 0; winrepaintsearch(app); } } return; } /* * Save numbers typed for later */ if (c >= '0' && c <= '9') { app->number[app->numberlen++] = c; app->number[app->numberlen] = '\0'; } switch (c) { case '?': winhelp(app); break; case 'q': winclose(app); break; /* * Zoom and rotate */ case '+': case '=': app->resolution *= ZOOMSTEP; if (app->resolution > MAXRES) app->resolution = MAXRES; pdfapp_showpage(app, 0, 1, 1); break; case '-': app->resolution /= ZOOMSTEP; if (app->resolution < MINRES) app->resolution = MINRES; pdfapp_showpage(app, 0, 1, 1); break; case 'L': app->rotate -= 90; pdfapp_showpage(app, 0, 1, 1); break; case 'R': app->rotate += 90; pdfapp_showpage(app, 0, 1, 1); break; case 'c': app->grayscale ^= 1; pdfapp_showpage(app, 0, 1, 1); break; #ifndef NDEBUG case 'a': app->rotate -= 15; pdfapp_showpage(app, 0, 1, 1); break; case 's': app->rotate += 15; pdfapp_showpage(app, 0, 1, 1); break; #endif /* * Pan view, but dont need to repaint image */ case 'w': app->shrinkwrap = 1; app->panx = app->pany = 0; pdfapp_showpage(app, 0, 0, 1); break; case 'h': app->panx += app->image->w / 10; pdfapp_showpage(app, 0, 0, 1); break; case 'j': app->pany -= app->image->h / 10; pdfapp_showpage(app, 0, 0, 1); break; case 'k': app->pany += app->image->h / 10; pdfapp_showpage(app, 0, 0, 1); break; case 'l': app->panx -= app->image->w / 10; pdfapp_showpage(app, 0, 0, 1); break; /* * Page navigation */ case 'g': case '\n': case '\r': if (app->numberlen > 0) app->pageno = atoi(app->number); else app->pageno = 1; break; case 'G': app->pageno = app->pagecount; break; case 'm': if (app->numberlen > 0) { int idx = atoi(app->number); if (idx >= 0 && idx < nelem(app->marks)) app->marks[idx] = app->pageno; } else { if (app->histlen + 1 == 256) { memmove(app->hist, app->hist + 1, sizeof(int) * 255); app->histlen --; } app->hist[app->histlen++] = app->pageno; } break; case 't': if (app->numberlen > 0) { int idx = atoi(app->number); if (idx >= 0 && idx < nelem(app->marks)) if (app->marks[idx] > 0) app->pageno = app->marks[idx]; } else if (app->histlen > 0) app->pageno = app->hist[--app->histlen]; break; /* * Back and forth ... */ case ',': panto = PAN_TO_BOTTOM; if (app->numberlen > 0) app->pageno -= atoi(app->number); else app->pageno--; break; case '.': panto = PAN_TO_TOP; if (app->numberlen > 0) app->pageno += atoi(app->number); else app->pageno++; break; case 'b': panto = DONT_PAN; if (app->numberlen > 0) app->pageno -= atoi(app->number); else app->pageno--; break; case ' ': panto = DONT_PAN; if (app->numberlen > 0) app->pageno += atoi(app->number); else app->pageno++; break; case '<': panto = PAN_TO_TOP; app->pageno -= 10; break; case '>': panto = PAN_TO_TOP; app->pageno += 10; break; /* * Reloading the file... */ case 'r': panto = DONT_PAN; oldpage = -1; winreloadfile(app); break; /* * Searching */ case '/': app->isediting = 1; app->search[0] = 0; app->hit = -1; app->hitlen = 0; winrepaintsearch(app); break; case 'n': pdfapp_searchforward(app, &panto); loadpage = 0; break; case 'N': pdfapp_searchbackward(app, &panto); loadpage = 0; break; } if (c < '0' || c > '9') app->numberlen = 0; if (app->pageno < 1) app->pageno = 1; if (app->pageno > app->pagecount) app->pageno = app->pagecount; if (app->pageno != oldpage) { switch (panto) { case PAN_TO_TOP: app->pany = 0; break; case PAN_TO_BOTTOM: app->pany = -2000; break; case DONT_PAN: break; } pdfapp_showpage(app, loadpage, 1, 1); } }
void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state) { pdf_link *link; fz_matrix ctm; fz_point p; p.x = x - app->panx + app->image->x; p.y = y - app->pany + app->image->y; ctm = pdfapp_viewctm(app); ctm = fz_invert_matrix(ctm); p = fz_transform_point(ctm, p); for (link = app->page_links; link; link = link->next) { if (p.x >= link->rect.x0 && p.x <= link->rect.x1) if (p.y >= link->rect.y0 && p.y <= link->rect.y1) break; } if (link) { wincursor(app, HAND); if (btn == 1 && state == 1) { if (link->kind == PDF_LINK_URI) pdfapp_gotouri(app, link->dest); else if (link->kind == PDF_LINK_GOTO) pdfapp_gotopage(app, fz_array_get(link->dest, 0)); /* [ pageobj ... ] */ return; } } else { wincursor(app, ARROW); } if (state == 1) { if (btn == 1 && !app->iscopying) { app->ispanning = 1; app->selx = x; app->sely = y; app->beyondy = 0; } if (btn == 3 && !app->ispanning) { app->iscopying = 1; app->selx = x; app->sely = y; app->selr.x0 = x; app->selr.x1 = x; app->selr.y0 = y; app->selr.y1 = y; } if (btn == 4 || btn == 5) /* scroll wheel */ { int dir = btn == 4 ? 1 : -1; app->ispanning = app->iscopying = 0; if (modifiers & (1<<2)) { /* zoom in/out if ctrl is pressed */ if (dir > 0) app->resolution *= ZOOMSTEP; else app->resolution /= ZOOMSTEP; if (app->resolution > MAXRES) app->resolution = MAXRES; if (app->resolution < MINRES) app->resolution = MINRES; pdfapp_showpage(app, 0, 1, 1); } else { /* scroll up/down, or left/right if shift is pressed */ int isx = (modifiers & (1<<0)); int xstep = isx ? 20 * dir : 0; int ystep = !isx ? 20 * dir : 0; pdfapp_panview(app, app->panx + xstep, app->pany + ystep); } } } else if (state == -1) { if (app->iscopying) { app->iscopying = 0; app->selr.x0 = MIN(app->selx, x) - app->panx + app->image->x; app->selr.x1 = MAX(app->selx, x) - app->panx + app->image->x; app->selr.y0 = MIN(app->sely, y) - app->pany + app->image->y; app->selr.y1 = MAX(app->sely, y) - app->pany + app->image->y; winrepaint(app); if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1) windocopy(app); } if (app->ispanning) app->ispanning = 0; } else if (app->ispanning) { int newx = app->panx + x - app->selx; int newy = app->pany + y - app->sely; /* Scrolling beyond limits implies flipping pages */ /* Are we requested to scroll beyond limits? */ if (newy + app->image->h < app->winh || newy > 0) { /* Yes. We can assume that deltay != 0 */ int deltay = y - app->sely; /* Check whether the panning has occured in the * direction that we are already crossing the * limit it. If not, we can conclude that we * have switched ends of the page and will thus * start over counting. */ if( app->beyondy == 0 || (app->beyondy ^ deltay) >= 0 ) { /* Updating how far we are beyond and * flipping pages if beyond threshhold */ app->beyondy += deltay; if (app->beyondy > BEYOND_THRESHHOLD) { if( app->pageno > 1 ) { app->pageno--; pdfapp_showpage(app, 1, 1, 1); newy = -app->image->h; } app->beyondy = 0; } else if (app->beyondy < -BEYOND_THRESHHOLD) { if( app->pageno < app->pagecount ) { app->pageno++; pdfapp_showpage(app, 1, 1, 1); newy = 0; } app->beyondy = 0; } } else app->beyondy = 0; } /* Although at this point we've already determined that * or that no scrolling will be performed in * y-direction, the x-direction has not yet been taken * care off. Therefore */ pdfapp_panview(app, newx, newy); app->selx = x; app->sely = y; } else if (app->iscopying) { app->selr.x0 = MIN(app->selx, x) - app->panx + app->image->x; app->selr.x1 = MAX(app->selx, x) - app->panx + app->image->x; app->selr.y0 = MIN(app->sely, y) - app->pany + app->image->y; app->selr.y1 = MAX(app->sely, y) - app->pany + app->image->y; winrepaint(app); } }
void pdfapp_onkey(pdfapp_t *app, int c) { int oldpage = app->pageno; int panto = 0; /* 0 = top, 1 = bottom, 2 = leave alone */ /* * Save numbers typed for later */ if (c >= '0' && c <= '9') app->number[app->numberlen++] = c; else if (c != 'g' && c != 'G') app->numberlen = 0; switch (c) { /* * Zoom and rotate */ case '+': case '=': app->zoom += 0.1; if (app->zoom > 3.0) app->zoom = 3.0; pdfapp_showpage(app, 0, 1); break; case '-': app->zoom -= 0.1; if (app->zoom < 0.1) app->zoom = 0.1; pdfapp_showpage(app, 0, 1); break; case 'l': case '<': app->rotate -= 90; pdfapp_showpage(app, 0, 1); break; case 'r': case '>': app->rotate += 90; pdfapp_showpage(app, 0, 1); break; case 'a': app->rotate -= 15; pdfapp_showpage(app, 0, 1); break; case 's': app->rotate += 15; pdfapp_showpage(app, 0, 1); break; /* * Pan view, but dont need to repaint image */ case 'w': app->shrinkwrap = 1; app->panx = app->pany = 0; pdfapp_showpage(app, 0, 0); break; case 'd': app->pany -= app->image->h / 10; pdfapp_showpage(app, 0, 0); break; case 'u': app->pany += app->image->h / 10; pdfapp_showpage(app, 0, 0); break; case ',': app->panx += app->image->w / 10; pdfapp_showpage(app, 0, 0); break; case '.': app->panx -= app->image->w / 10; pdfapp_showpage(app, 0, 0); break; /* * Page navigation */ case 'g': case '\n': case '\r': if (app->numberlen > 0) { app->number[app->numberlen] = '\0'; app->pageno = atoi(app->number); app->numberlen = 0; } break; case 'G': app->pageno = pdf_getpagecount(app->pages); break; case 'm': if (app->histlen + 1 == 256) { memmove(app->hist, app->hist + 1, sizeof(int) * 255); app->histlen --; } app->hist[app->histlen++] = app->pageno; break; case 't': if (app->histlen > 0) app->pageno = app->hist[--app->histlen]; break; /* * Back and forth ... */ case 'p': panto = 2; app->pageno--; break; case 'b': case '\b': panto = 1; app->pageno--; break; case 'n': panto = 2; case 'f': case ' ': app->pageno++; break; case 'B': app->pageno -= 10; break; case 'F': app->pageno += 10; break; } if (app->pageno < 1) app->pageno = 1; if (app->pageno > pdf_getpagecount(app->pages)) app->pageno = pdf_getpagecount(app->pages); if (app->pageno != oldpage) { switch (panto) { case 0: app->pany = 0; break; case 1: app->pany = -2000; break; case 2: break; } pdfapp_showpage(app, 1, 1); } }