1 #include "pdfapp.h"
2 #include "curl_stream.h"
3 #include "mupdf/helpers/pkcs7-openssl.h"
4
5 #include <string.h>
6 #include <limits.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <sys/stat.h>
10
11 #ifdef _MSC_VER
12 #define stat _stat
13 #endif
14
15 #ifdef _WIN32
16 #include <windows.h>
17 #include <direct.h> /* for getcwd */
18 #else
19 #include <unistd.h> /* for getcwd */
20 #endif
21
22 #define BEYOND_THRESHHOLD 40
23
24 #ifndef PATH_MAX
25 #define PATH_MAX 4096
26 #endif
27
28 #ifndef MAX
29 #define MAX(a,b) ((a) > (b) ? (a) : (b))
30 #endif
31
32 time_t
stat_mtime(const char * path)33 stat_mtime(const char *path)
34 {
35 struct stat info;
36
37 if (stat(path, &info) < 0)
38 return 0;
39
40 return info.st_mtime;
41 }
42
convert_to_accel_path(fz_context * ctx,char outname[],char * absname,size_t len)43 static int convert_to_accel_path(fz_context *ctx, char outname[], char *absname, size_t len)
44 {
45 char *tmpdir;
46 char *s;
47
48 tmpdir = getenv("TEMP");
49 if (!tmpdir)
50 tmpdir = getenv("TMP");
51 if (!tmpdir)
52 tmpdir = "/var/tmp";
53 if (!fz_is_directory(ctx, tmpdir))
54 tmpdir = "/tmp";
55
56 if (absname[0] == '/' || absname[0] == '\\')
57 ++absname;
58
59 s = absname;
60 while (*s) {
61 if (*s == '/' || *s == '\\' || *s == ':')
62 *s = '%';
63 ++s;
64 }
65
66 if (fz_snprintf(outname, len, "%s/%s.accel", tmpdir, absname) >= len)
67 return 0;
68 return 1;
69 }
70
get_accelerator_filename(fz_context * ctx,char outname[],size_t len,const char * filename)71 static int get_accelerator_filename(fz_context *ctx, char outname[], size_t len, const char *filename)
72 {
73 char absname[PATH_MAX];
74 if (!fz_realpath(filename, absname))
75 return 0;
76 if (!convert_to_accel_path(ctx, outname, absname, len))
77 return 0;
78 return 1;
79 }
80
save_accelerator(fz_context * ctx,fz_document * doc,const char * filename)81 static void save_accelerator(fz_context *ctx, fz_document *doc, const char *filename)
82 {
83 char absname[PATH_MAX];
84
85 if (!doc)
86 return;
87 if (!fz_document_supports_accelerator(ctx, doc))
88 return;
89 if (!get_accelerator_filename(ctx, absname, sizeof(absname), filename))
90 return;
91
92 fz_save_accelerator(ctx, doc, absname);
93 }
94
95 enum panning
96 {
97 DONT_PAN = 0,
98 PAN_TO_TOP,
99 PAN_TO_BOTTOM
100 };
101
102 enum
103 {
104 PDFAPP_OUTLINE_DEFERRED = 1,
105 PDFAPP_OUTLINE_LOAD_NOW = 2
106 };
107
108 #ifdef HAVE_CURL
pdfapp_sleep(int ms)109 static void pdfapp_sleep(int ms)
110 {
111 #ifdef _WIN32
112 Sleep(ms);
113 #else
114 usleep(ms * 1000);
115 #endif
116 }
117 #endif
118
119 static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repaint, int transition, int searching);
120
121 static const int zoomlist[] = {
122 18, 24, 36, 54, 72, 96, 120, 144, 180,
123 216, 288, 360, 432, 504, 576, 648, 720,
124 792, 864, 936, 1008, 1080, 1152
125 };
126
zoom_in(int oldres)127 static int zoom_in(int oldres)
128 {
129 int i;
130 for (i = 0; i < (int)nelem(zoomlist) - 1; ++i)
131 if (zoomlist[i] <= oldres && zoomlist[i+1] > oldres)
132 return zoomlist[i+1];
133 return zoomlist[i];
134 }
135
zoom_out(int oldres)136 static int zoom_out(int oldres)
137 {
138 int i;
139 for (i = 0; i < (int)nelem(zoomlist) - 1; ++i)
140 if (zoomlist[i] < oldres && zoomlist[i+1] >= oldres)
141 return zoomlist[i];
142 return zoomlist[0];
143 }
144
pdfapp_warn(pdfapp_t * app,const char * fmt,...)145 void pdfapp_warn(pdfapp_t *app, const char *fmt, ...)
146 {
147 char buf[1024];
148 va_list ap;
149 va_start(ap, fmt);
150 fz_vsnprintf(buf, sizeof(buf), fmt, ap);
151 va_end(ap);
152 buf[sizeof(buf)-1] = 0;
153 winwarn(app, buf);
154 }
155
pdfapp_error(pdfapp_t * app,char * msg)156 void pdfapp_error(pdfapp_t *app, char *msg)
157 {
158 winerror(app, msg);
159 }
160
pdfapp_version(pdfapp_t * app)161 char *pdfapp_version(pdfapp_t *app)
162 {
163 return
164 "MuPDF " FZ_VERSION "\n"
165 "Copyright 2006-2020 Artifex Software, Inc.\n";
166 }
167
pdfapp_usage(pdfapp_t * app)168 char *pdfapp_usage(pdfapp_t *app)
169 {
170 return
171 "[\t\t-- rotate left\n"
172 "]\t\t-- rotate right\n"
173 "h left\t\t-- scroll left\n"
174 "j down\t\t-- scroll down\n"
175 "k up\t\t-- scroll up\n"
176 "l right\t\t-- scroll right\n"
177 "+\t\t-- zoom in\n"
178 "-\t\t-- zoom out\n"
179 "W\t\t-- zoom to fit window width\n"
180 "H\t\t-- zoom to fit window height\n"
181 "Z\t\t-- zoom to fit page\n"
182 "z\t\t-- reset zoom\n"
183 "<\t\t-- decrease font size (EPUB only)\n"
184 ">\t\t-- increase font size (EPUB only)\n"
185 "w\t\t-- shrinkwrap\n"
186 "f\t\t-- fullscreen\n"
187 "r\t\t-- reload file\n"
188 ". pgdn space\t-- next page\n"
189 ", pgup b\t-- previous page\n"
190 "m\t\t-- mark page for snap back\n"
191 "t\t\t-- pop back to latest mark\n"
192 "1m\t\t-- mark page in register 1\n"
193 "1t\t\t-- go to page in register 1\n"
194 "G\t\t-- go to last page\n"
195 "123g\t\t-- go to page 123\n"
196 "/\t\t-- search forwards for text\n"
197 "?\t\t-- search backwards for text\n"
198 "n\t\t-- find next search result\n"
199 "N\t\t-- find previous search result\n"
200 "c\t\t-- toggle between color and grayscale\n"
201 "I\t\t-- toggle inverted color mode\n"
202 "C\t\t-- toggle tinted color mode\n"
203 "E\t\t-- enable/disable ICC color mode\n"
204 "e\t\t-- enable/disable spot color mode\n"
205 "q\t\t-- quit\n"
206 ;
207 }
208
pdfapp_init(fz_context * ctx,pdfapp_t * app)209 void pdfapp_init(fz_context *ctx, pdfapp_t *app)
210 {
211 memset(app, 0, sizeof(pdfapp_t));
212 app->scrw = 640;
213 app->scrh = 480;
214 app->resolution = 72;
215 app->ctx = ctx;
216
217 app->layout_w = FZ_DEFAULT_LAYOUT_W;
218 app->layout_h = FZ_DEFAULT_LAYOUT_H;
219 app->layout_em = FZ_DEFAULT_LAYOUT_EM;
220 app->layout_css = NULL;
221 app->layout_use_doc_css = 1;
222
223 app->transition.duration = 0.25f;
224 app->transition.type = FZ_TRANSITION_FADE;
225 #ifdef _WIN32
226 app->colorspace = fz_device_bgr(ctx);
227 #else
228 app->colorspace = fz_device_rgb(ctx);
229 #endif
230 app->tint_white = 0xFFFAF0;
231
232 app->useicc = 1;
233 app->useseparations = 0;
234 app->aalevel = 8;
235 }
236
pdfapp_setresolution(pdfapp_t * app,int res)237 void pdfapp_setresolution(pdfapp_t *app, int res)
238 {
239 app->default_resolution = res;
240 app->resolution = res;
241 }
242
pdfapp_invert(pdfapp_t * app,fz_rect rect)243 void pdfapp_invert(pdfapp_t *app, fz_rect rect)
244 {
245 fz_invert_pixmap_rect(app->ctx, app->image, fz_round_rect(rect));
246 }
247
pdfapp_reloadfile(pdfapp_t * app)248 void pdfapp_reloadfile(pdfapp_t *app)
249 {
250 char filename[PATH_MAX];
251 fz_strlcpy(filename, app->docpath, PATH_MAX);
252 pdfapp_close(app);
253 pdfapp_open(app, filename, 1);
254 }
255
event_cb(fz_context * ctx,pdf_document * doc,pdf_doc_event * event,void * data)256 static void event_cb(fz_context *ctx, pdf_document *doc, pdf_doc_event *event, void *data)
257 {
258 pdfapp_t *app = (pdfapp_t *)data;
259
260 switch (event->type)
261 {
262 case PDF_DOCUMENT_EVENT_ALERT:
263 {
264 pdf_alert_event *alert = pdf_access_alert_event(ctx, event);
265 winalert(app, alert);
266 }
267 break;
268
269 case PDF_DOCUMENT_EVENT_PRINT:
270 winprint(app);
271 break;
272
273 case PDF_DOCUMENT_EVENT_EXEC_MENU_ITEM:
274 {
275 const char *item = pdf_access_exec_menu_item_event(ctx, event);
276
277 if (!strcmp(item, "Print"))
278 winprint(app);
279 else
280 pdfapp_warn(app, "The document attempted to execute menu item: %s. (Not supported)", item);
281 }
282 break;
283
284 case PDF_DOCUMENT_EVENT_LAUNCH_URL:
285 {
286 pdf_launch_url_event *launch_url = pdf_access_launch_url_event(ctx, event);
287
288 pdfapp_warn(app, "The document attempted to open url: %s. (Not supported by app)", launch_url->url);
289 }
290 break;
291
292 case PDF_DOCUMENT_EVENT_MAIL_DOC:
293 {
294 pdf_mail_doc_event *mail_doc = pdf_access_mail_doc_event(ctx, event);
295
296 pdfapp_warn(app, "The document attempted to mail the document%s%s%s%s%s%s%s%s (Not supported)",
297 mail_doc->to[0]?", To: ":"", mail_doc->to,
298 mail_doc->cc[0]?", Cc: ":"", mail_doc->cc,
299 mail_doc->bcc[0]?", Bcc: ":"", mail_doc->bcc,
300 mail_doc->subject[0]?", Subject: ":"", mail_doc->subject);
301 }
302 break;
303 }
304 }
305
pdfapp_open(pdfapp_t * app,char * filename,int reload)306 void pdfapp_open(pdfapp_t *app, char *filename, int reload)
307 {
308 pdfapp_open_progressive(app, filename, reload, 0);
309 }
310
311 #ifdef HAVE_CURL
312 static void
pdfapp_more_data(void * app_,int complete)313 pdfapp_more_data(void *app_, int complete)
314 {
315 pdfapp_t *app = (pdfapp_t *)app_;
316 if (complete && app->outline_deferred == PDFAPP_OUTLINE_DEFERRED)
317 {
318 app->outline_deferred = PDFAPP_OUTLINE_LOAD_NOW;
319 winreloadpage(app);
320 }
321 else if (app->incomplete)
322 winreloadpage(app);
323 }
324 #endif
325
make_fake_doc(pdfapp_t * app)326 static int make_fake_doc(pdfapp_t *app)
327 {
328 fz_context *ctx = app->ctx;
329 pdf_document *pdf = NULL;
330 fz_buffer *contents = NULL;
331 pdf_obj *page_obj = NULL;
332
333 fz_var(contents);
334 fz_var(page_obj);
335
336 fz_try(ctx)
337 {
338 fz_rect mediabox = { 0, 0, app->winw, app->winh };
339 int i;
340
341 pdf = pdf_create_document(ctx);
342
343 contents = fz_new_buffer(ctx, 100);
344 fz_append_printf(ctx, contents, "1 0 0 RG %g w 0 0 m %g %g l 0 %g m %g 0 l s\n",
345 fz_min(mediabox.x1, mediabox.y1) / 20,
346 mediabox.x1, mediabox.y1,
347 mediabox.y1, mediabox.x1);
348
349 /* Create enough copies of our blank(ish) page so that the
350 * page number is preserved if and when a subsequent load
351 * works. */
352 page_obj = pdf_add_page(ctx, pdf, mediabox, 0, NULL, contents);
353 for (i = 0; i < app->pagecount; i++)
354 pdf_insert_page(ctx, pdf, -1, page_obj);
355 }
356 fz_always(ctx)
357 {
358 pdf_drop_obj(ctx, page_obj);
359 fz_drop_buffer(ctx, contents);
360 }
361 fz_catch(ctx)
362 {
363 fz_drop_document(ctx, (fz_document *) pdf);
364 return 1;
365 }
366
367 app->doc = (fz_document*)pdf;
368 return 0;
369 }
370
pdfapp_open_progressive(pdfapp_t * app,char * filename,int reload,int kbps)371 void pdfapp_open_progressive(pdfapp_t *app, char *filename, int reload, int kbps)
372 {
373 fz_context *ctx = app->ctx;
374 char *password = "";
375 pdf_document *idoc;
376
377 fz_try(ctx)
378 {
379 fz_register_document_handlers(ctx);
380
381 if (app->layout_css)
382 {
383 fz_buffer *buf = fz_read_file(ctx, app->layout_css);
384 fz_set_user_css(ctx, fz_string_from_buffer(ctx, buf));
385 fz_drop_buffer(ctx, buf);
386 }
387
388 fz_set_use_document_css(ctx, app->layout_use_doc_css);
389
390 #ifdef HAVE_CURL
391 if (!strncmp(filename, "http://", 7) || !strncmp(filename, "https://", 8))
392 {
393 app->stream = fz_open_url(ctx, filename, kbps, pdfapp_more_data, app);
394 while (1)
395 {
396 fz_try(ctx)
397 {
398 fz_seek(ctx, app->stream, 0, SEEK_SET);
399 app->doc = fz_open_document_with_stream(ctx, filename, app->stream);
400 }
401 fz_catch(ctx)
402 {
403 if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
404 {
405 pdfapp_sleep(100);
406 continue;
407 }
408 fz_rethrow(ctx);
409 }
410 break;
411 }
412 }
413 else if (kbps > 0)
414 {
415 fz_stream *stream = fz_open_file_progressive(ctx, filename, kbps, pdfapp_more_data, app);
416 while (1)
417 {
418 fz_try(ctx)
419 {
420 fz_seek(ctx, stream, 0, SEEK_SET);
421 app->doc = fz_open_document_with_stream(ctx, filename, stream);
422 }
423 fz_catch(ctx)
424 {
425 if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
426 {
427 pdfapp_sleep(100);
428 continue;
429 }
430 fz_rethrow(ctx);
431 }
432 break;
433 }
434 }
435 else
436 #endif
437 {
438 char accelpath[PATH_MAX];
439 char *accel = NULL;
440 time_t atime;
441 time_t dtime;
442
443 /* If there was an accelerator to load, what would it be called? */
444 if (get_accelerator_filename(ctx, accelpath, sizeof(accelpath), filename))
445 {
446 /* Check whether that file exists, and isn't older than
447 * the document. */
448 atime = stat_mtime(accelpath);
449 dtime = stat_mtime(filename);
450 if (atime == 0)
451 {
452 /* No accelerator */
453 }
454 else if (atime > dtime)
455 accel = accelpath;
456 else
457 {
458 /* Accelerator data is out of date */
459 unlink(accelpath);
460 accel = NULL; /* In case we have jumped up from below */
461 }
462 }
463
464 app->doc = fz_open_accelerated_document(ctx, filename, accel);
465 }
466 }
467 fz_catch(ctx)
468 {
469 if (!reload || make_fake_doc(app))
470 pdfapp_error(app, "cannot open document");
471 }
472
473 idoc = pdf_specifics(app->ctx, app->doc);
474 if (idoc)
475 {
476 fz_try(ctx)
477 {
478 pdf_enable_js(ctx, idoc);
479 pdf_set_doc_event_callback(ctx, idoc, event_cb, app);
480 }
481 fz_catch(ctx)
482 {
483 pdfapp_error(app, "cannot load javascript embedded in document");
484 }
485 }
486
487 fz_try(ctx)
488 {
489
490 if (fz_needs_password(app->ctx, app->doc))
491 {
492 int okay = fz_authenticate_password(app->ctx, app->doc, password);
493 while (!okay)
494 {
495 password = winpassword(app, filename);
496 if (!password)
497 fz_throw(ctx, FZ_ERROR_GENERIC, "Needs a password");
498 okay = fz_authenticate_password(app->ctx, app->doc, password);
499 if (!okay)
500 pdfapp_warn(app, "Invalid password.");
501 }
502 }
503
504 app->docpath = fz_strdup(ctx, filename);
505 app->doctitle = filename;
506 if (strrchr(app->doctitle, '\\'))
507 app->doctitle = strrchr(app->doctitle, '\\') + 1;
508 if (strrchr(app->doctitle, '/'))
509 app->doctitle = strrchr(app->doctitle, '/') + 1;
510 app->doctitle = fz_strdup(ctx, app->doctitle);
511
512 fz_layout_document(app->ctx, app->doc, app->layout_w, app->layout_h, app->layout_em);
513
514 while (1)
515 {
516 fz_try(ctx)
517 {
518 app->pagecount = fz_count_pages(app->ctx, app->doc);
519 if (app->pagecount <= 0)
520 fz_throw(ctx, FZ_ERROR_GENERIC, "No pages in document");
521 }
522 fz_catch(ctx)
523 {
524 if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
525 {
526 continue;
527 }
528 fz_rethrow(ctx);
529 }
530 break;
531 }
532 while (1)
533 {
534 fz_try(ctx)
535 {
536 app->outline = fz_load_outline(app->ctx, app->doc);
537 }
538 fz_catch(ctx)
539 {
540 app->outline = NULL;
541 if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
542 app->outline_deferred = PDFAPP_OUTLINE_DEFERRED;
543 else
544 pdfapp_warn(app, "Failed to load outline.");
545 }
546 break;
547 }
548 }
549 fz_catch(ctx)
550 {
551 pdfapp_error(app, "cannot open document");
552 }
553
554 if (app->pageno < 1)
555 app->pageno = 1;
556 if (app->pageno > app->pagecount)
557 app->pageno = app->pagecount;
558 if (app->resolution < MINRES)
559 app->resolution = MINRES;
560 if (app->resolution > MAXRES)
561 app->resolution = MAXRES;
562
563 if (!reload)
564 {
565 app->shrinkwrap = 1;
566 app->rotate = 0;
567 app->panx = 0;
568 app->pany = 0;
569 }
570
571 pdfapp_showpage(app, 1, 1, 1, 0, 0);
572 }
573
pdfapp_close(pdfapp_t * app)574 void pdfapp_close(pdfapp_t *app)
575 {
576 fz_drop_display_list(app->ctx, app->page_list);
577 app->page_list = NULL;
578
579 fz_drop_display_list(app->ctx, app->annotations_list);
580 app->annotations_list = NULL;
581
582 fz_drop_separations(app->ctx, app->seps);
583 app->seps = NULL;
584
585 fz_drop_stext_page(app->ctx, app->page_text);
586 app->page_text = NULL;
587
588 fz_drop_link(app->ctx, app->page_links);
589 app->page_links = NULL;
590
591 fz_free(app->ctx, app->doctitle);
592 app->doctitle = NULL;
593
594 fz_free(app->ctx, app->docpath);
595 app->docpath = NULL;
596
597 fz_drop_pixmap(app->ctx, app->image);
598 app->image = NULL;
599
600 fz_drop_pixmap(app->ctx, app->new_image);
601 app->new_image = NULL;
602
603 fz_drop_pixmap(app->ctx, app->old_image);
604 app->old_image = NULL;
605
606 fz_drop_outline(app->ctx, app->outline);
607 app->outline = NULL;
608
609 fz_drop_page(app->ctx, app->page);
610 app->page = NULL;
611
612 fz_drop_document(app->ctx, app->doc);
613 app->doc = NULL;
614
615 #ifdef HAVE_CURL
616 fz_drop_stream(app->ctx, app->stream);
617 #endif
618
619 fz_flush_warnings(app->ctx);
620 }
621
gen_tmp_file(char * buf,int len)622 static int gen_tmp_file(char *buf, int len)
623 {
624 int i;
625 char *name = strrchr(buf, '/');
626
627 if (name == NULL)
628 name = strrchr(buf, '\\');
629
630 if (name != NULL)
631 name++;
632 else
633 name = buf;
634
635 for (i = 0; i < 10000; i++)
636 {
637 FILE *f;
638 sprintf(name, "tmp%04d", i);
639 f = fopen(buf, "r");
640 if (f == NULL)
641 return 1;
642 fclose(f);
643 }
644
645 return 0;
646 }
647
pdfapp_save(pdfapp_t * app)648 static int pdfapp_save(pdfapp_t *app)
649 {
650 char buf[PATH_MAX];
651
652 pdf_document *idoc = pdf_specifics(app->ctx, app->doc);
653 if (!idoc)
654 return 0;
655
656 if (wingetsavepath(app, buf, PATH_MAX))
657 {
658 pdf_write_options opts = pdf_default_write_options;
659
660 opts.do_incremental = pdf_can_be_saved_incrementally(app->ctx, idoc);
661
662 if (strcmp(buf, app->docpath) != 0)
663 {
664 wincopyfile(app, app->docpath, buf);
665 pdf_save_document(app->ctx, idoc, buf, &opts);
666 pdfapp_close(app);
667 pdfapp_open(app, buf, 1);
668 return 1;
669 }
670
671 if (gen_tmp_file(buf, PATH_MAX))
672 {
673 int written = 0;
674
675 fz_try(app->ctx)
676 {
677 wincopyfile(app, app->docpath, buf);
678 pdf_save_document(app->ctx, idoc, buf, &opts);
679 written = 1;
680 }
681 fz_catch(app->ctx)
682 {
683 /* Ignore any error, so we drop out with
684 * failure below. */
685 }
686
687 if (written)
688 {
689 char buf2[PATH_MAX];
690 fz_strlcpy(buf2, app->docpath, PATH_MAX);
691 pdfapp_close(app);
692 winreplacefile(app, buf, buf2);
693 pdfapp_open(app, buf2, 1);
694
695 return written;
696 }
697 }
698 }
699
700 return 0;
701 }
702
pdfapp_preclose(pdfapp_t * app)703 int pdfapp_preclose(pdfapp_t *app)
704 {
705 pdf_document *idoc = pdf_specifics(app->ctx, app->doc);
706
707 if (idoc && pdf_has_unsaved_changes(app->ctx, idoc))
708 {
709 switch (winsavequery(app))
710 {
711 case DISCARD:
712 return 1;
713
714 case CANCEL:
715 return 0;
716
717 case SAVE:
718 return pdfapp_save(app);
719 }
720 }
721
722 return 1;
723 }
724
pdfapp_viewctm(fz_matrix * mat,pdfapp_t * app)725 static void pdfapp_viewctm(fz_matrix *mat, pdfapp_t *app)
726 {
727 *mat = fz_transform_page(app->page_bbox, app->resolution, app->rotate);
728 }
729
pdfapp_panview(pdfapp_t * app,int newx,int newy)730 static void pdfapp_panview(pdfapp_t *app, int newx, int newy)
731 {
732 if (newx > 0)
733 newx = 0;
734 if (newy > 0)
735 newy = 0;
736
737 if (newx + app->imgw < app->winw)
738 newx = app->winw - app->imgw;
739 if (newy + app->imgh < app->winh)
740 newy = app->winh - app->imgh;
741
742 if (app->winw >= app->imgw)
743 newx = (app->winw - app->imgw) / 2;
744 if (app->winh >= app->imgh)
745 newy = (app->winh - app->imgh) / 2;
746
747 if (newx != app->panx || newy != app->pany)
748 winrepaint(app);
749
750 app->panx = newx;
751 app->pany = newy;
752 }
753
pdfapp_loadpage(pdfapp_t * app,int no_cache)754 static void pdfapp_loadpage(pdfapp_t *app, int no_cache)
755 {
756 fz_device *mdev = NULL;
757 int errored = 0;
758 fz_cookie cookie = { 0 };
759
760 fz_var(mdev);
761
762 fz_drop_display_list(app->ctx, app->page_list);
763 fz_drop_display_list(app->ctx, app->annotations_list);
764 fz_drop_separations(app->ctx, app->seps);
765 fz_drop_stext_page(app->ctx, app->page_text);
766 fz_drop_link(app->ctx, app->page_links);
767 fz_drop_page(app->ctx, app->page);
768
769 app->page_list = NULL;
770 app->annotations_list = NULL;
771 app->seps = NULL;
772 app->page_text = NULL;
773 app->page_links = NULL;
774 app->page = NULL;
775 app->page_bbox.x0 = 0;
776 app->page_bbox.y0 = 0;
777 app->page_bbox.x1 = 100;
778 app->page_bbox.y1 = 100;
779
780 app->incomplete = 0;
781
782 fz_try(app->ctx)
783 {
784 app->page = fz_load_page(app->ctx, app->doc, app->pageno - 1);
785 if (app->page && app->page->incomplete)
786 app->incomplete = 1;
787 app->page_bbox = fz_bound_page(app->ctx, app->page);
788 app->page_links = fz_load_links(app->ctx, app->page);
789 }
790 fz_catch(app->ctx)
791 {
792 if (fz_caught(app->ctx) == FZ_ERROR_TRYLATER)
793 app->incomplete = 1;
794 else
795 pdfapp_warn(app, "Failed to load page.");
796 return;
797 }
798
799 if (app->useicc)
800 fz_enable_icc(app->ctx);
801 else
802 fz_disable_icc(app->ctx);
803
804 fz_set_aa_level(app->ctx, app->aalevel);
805
806 if (app->useseparations)
807 {
808 fz_try(app->ctx)
809 {
810 app->seps = fz_page_separations(app->ctx, app->page);
811 if (app->seps)
812 {
813 int i, n = fz_count_separations(app->ctx, app->seps);
814 for (i = 0; i < n; i++)
815 fz_set_separation_behavior(app->ctx, app->seps, i, FZ_SEPARATION_COMPOSITE);
816 }
817 else if (fz_page_uses_overprint(app->ctx, app->page))
818 {
819 /* This page uses overprint, so we need an empty
820 * sep object to force the overprint simulation on. */
821 app->seps = fz_new_separations(app->ctx, 0);
822 }
823 else if (fz_document_output_intent(app->ctx, app->doc))
824 {
825 /* We have an output intent. Force the overprint
826 *simulation on, because this ensures that
827 * we 'simulate' the output intent too. */
828 app->seps = fz_new_separations(app->ctx, 0);
829 }
830 }
831 fz_catch(app->ctx)
832 {
833 if (fz_caught(app->ctx) == FZ_ERROR_TRYLATER)
834 app->incomplete = 1;
835 else
836 pdfapp_warn(app, "Failed to load page.");
837 errored = 1;
838 }
839 }
840
841 fz_try(app->ctx)
842 {
843 /* Create display lists */
844 app->page_list = fz_new_display_list(app->ctx, fz_infinite_rect);
845 mdev = fz_new_list_device(app->ctx, app->page_list);
846 if (no_cache)
847 fz_enable_device_hints(app->ctx, mdev, FZ_NO_CACHE);
848 fz_run_page_contents(app->ctx, app->page, mdev, fz_identity, &cookie);
849 fz_close_device(app->ctx, mdev);
850 fz_drop_device(app->ctx, mdev);
851 mdev = NULL;
852 app->annotations_list = fz_new_display_list(app->ctx, fz_infinite_rect);
853 mdev = fz_new_list_device(app->ctx, app->annotations_list);
854 fz_run_page_annots(app->ctx, app->page, mdev, fz_identity, &cookie);
855 fz_run_page_widgets(app->ctx, app->page, mdev, fz_identity, &cookie);
856 if (cookie.incomplete)
857 {
858 app->incomplete = 1;
859 }
860 else if (cookie.errors)
861 {
862 pdfapp_warn(app, "Errors found on page.");
863 errored = 1;
864 }
865 fz_close_device(app->ctx, mdev);
866 }
867 fz_always(app->ctx)
868 {
869 fz_drop_device(app->ctx, mdev);
870 }
871 fz_catch(app->ctx)
872 {
873 if (fz_caught(app->ctx) == FZ_ERROR_TRYLATER)
874 app->incomplete = 1;
875 else
876 pdfapp_warn(app, "Failed to load page.");
877 errored = 1;
878 }
879
880 app->errored = errored;
881 }
882
pdfapp_runpage(pdfapp_t * app,fz_device * dev,const fz_matrix ctm,fz_rect scissor,fz_cookie * cookie)883 static void pdfapp_runpage(pdfapp_t *app, fz_device *dev, const fz_matrix ctm, fz_rect scissor, fz_cookie *cookie)
884 {
885 if (app->page_list)
886 fz_run_display_list(app->ctx, app->page_list, dev, ctm, scissor, cookie);
887 if (app->annotations_list)
888 fz_run_display_list(app->ctx, app->annotations_list, dev, ctm, scissor, cookie);
889 }
890
891 #define MAX_TITLE 256
892
pdfapp_reloadpage(pdfapp_t * app)893 void pdfapp_reloadpage(pdfapp_t *app)
894 {
895 if (app->outline_deferred == PDFAPP_OUTLINE_LOAD_NOW)
896 {
897 fz_try(app->ctx)
898 app->outline = fz_load_outline(app->ctx, app->doc);
899 fz_catch(app->ctx)
900 app->outline = NULL;
901 app->outline_deferred = 0;
902 }
903 pdfapp_showpage(app, 1, 1, 1, 0, 0);
904 }
905
pdfapp_showpage(pdfapp_t * app,int loadpage,int drawpage,int repaint,int transition,int searching)906 static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repaint, int transition, int searching)
907 {
908 char buf[MAX_TITLE];
909 fz_device *idev = NULL;
910 fz_device *tdev;
911 fz_colorspace *colorspace;
912 fz_matrix ctm;
913 fz_rect bounds;
914 fz_irect ibounds;
915 fz_cookie cookie = { 0 };
916
917 if (!app->nowaitcursor)
918 wincursor(app, WAIT);
919
920 if (!app->transitions_enabled || !app->presentation_mode)
921 transition = 0;
922
923 if (transition)
924 {
925 app->old_image = app->image;
926 app->image = NULL;
927 app->imgw = 0;
928 app->imgh = 0;
929 }
930
931 /* Always reload page if it was flagged incomplete */
932 if (app->incomplete)
933 loadpage = 1;
934
935 if (loadpage)
936 {
937 fz_rect mediabox;
938 pdfapp_loadpage(app, searching);
939
940 /* Zero search hit position */
941 app->hit_count = 0;
942
943 /* Extract text */
944 fz_try(app->ctx)
945 mediabox = fz_bound_page(app->ctx, app->page);
946 fz_catch(app->ctx)
947 {
948 if (fz_caught(app->ctx) != FZ_ERROR_TRYLATER)
949 fz_rethrow(app->ctx);
950 mediabox = fz_make_rect(0, 0, 100, 100);
951 app->incomplete = 1;
952 }
953
954 app->page_text = fz_new_stext_page(app->ctx, mediabox);
955
956 if (app->page_list || app->annotations_list)
957 {
958 tdev = fz_new_stext_device(app->ctx, app->page_text, NULL);
959 fz_try(app->ctx)
960 {
961 pdfapp_runpage(app, tdev, fz_identity, fz_infinite_rect, &cookie);
962 fz_close_device(app->ctx, tdev);
963 }
964 fz_always(app->ctx)
965 fz_drop_device(app->ctx, tdev);
966 fz_catch(app->ctx)
967 fz_rethrow(app->ctx);
968 }
969 }
970
971 if (drawpage)
972 {
973 char buf2[64];
974 size_t len;
975
976 sprintf(buf2, " - %d/%d (%g dpi)",
977 app->pageno, app->pagecount, app->resolution);
978 len = MAX_TITLE-strlen(buf2);
979 if (strlen(app->doctitle) > len)
980 {
981 fz_strlcpy(buf, app->doctitle, len-3);
982 fz_strlcat(buf, "...", MAX_TITLE);
983 fz_strlcat(buf, buf2, MAX_TITLE);
984 }
985 else
986 sprintf(buf, "%s%s", app->doctitle, buf2);
987 wintitle(app, buf);
988
989 pdfapp_viewctm(&ctm, app);
990 bounds = fz_transform_rect(app->page_bbox, ctm);
991 ibounds = fz_round_rect(bounds);
992 bounds = fz_rect_from_irect(ibounds);
993
994 /* Draw */
995 fz_drop_pixmap(app->ctx, app->image);
996 if (app->grayscale)
997 colorspace = fz_device_gray(app->ctx);
998 else
999 colorspace = app->colorspace;
1000
1001 app->image = NULL;
1002 app->imgw = 0;
1003 app->imgh = 0;
1004
1005 fz_var(app->image);
1006 fz_var(idev);
1007
1008 fz_try(app->ctx)
1009 {
1010 app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, ibounds, app->seps, 1);
1011 app->imgw = fz_pixmap_width(app->ctx, app->image);
1012 app->imgh = fz_pixmap_height(app->ctx, app->image);
1013
1014 fz_clear_pixmap_with_value(app->ctx, app->image, 255);
1015 if (app->page_list || app->annotations_list)
1016 {
1017 idev = fz_new_draw_device(app->ctx, fz_identity, app->image);
1018 pdfapp_runpage(app, idev, ctm, bounds, &cookie);
1019 fz_close_device(app->ctx, idev);
1020 }
1021 if (app->invert)
1022 {
1023 fz_invert_pixmap_luminance(app->ctx, app->image);
1024 fz_gamma_pixmap(app->ctx, app->image, 1 / 1.4f);
1025 }
1026 if (app->tint)
1027 fz_tint_pixmap(app->ctx, app->image, 0, app->tint_white);
1028 }
1029 fz_always(app->ctx)
1030 fz_drop_device(app->ctx, idev);
1031 fz_catch(app->ctx)
1032 cookie.errors++;
1033 }
1034
1035 if (transition)
1036 {
1037 app->new_image = app->image;
1038 app->image = NULL;
1039 app->imgw = 0;
1040 app->imgh = 0;
1041
1042 if (app->grayscale)
1043 colorspace = fz_device_gray(app->ctx);
1044 else
1045 colorspace = app->colorspace;
1046 app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, ibounds, app->seps, 1);
1047 app->imgw = fz_pixmap_width(app->ctx, app->image);
1048 app->imgh = fz_pixmap_height(app->ctx, app->image);
1049
1050 app->duration = 0;
1051 fz_page_presentation(app->ctx, app->page, &app->transition, &app->duration);
1052 if (app->duration == 0)
1053 app->duration = 5;
1054 app->in_transit = fz_generate_transition(app->ctx, app->image, app->old_image, app->new_image, 0, &app->transition);
1055 if (!app->in_transit)
1056 {
1057 if (app->duration != 0)
1058 winadvancetimer(app, app->duration);
1059 }
1060 app->start_time = clock();
1061 }
1062
1063 if (repaint)
1064 {
1065 pdfapp_panview(app, app->panx, app->pany);
1066
1067 if (!app->image)
1068 {
1069 /* there is no image to blit, but there might be an error message */
1070 winresize(app, app->layout_w, app->layout_h);
1071 }
1072 else if (app->shrinkwrap)
1073 {
1074 int w = app->imgw;
1075 int h = app->imgh;
1076 if (app->winw == w)
1077 app->panx = 0;
1078 if (app->winh == h)
1079 app->pany = 0;
1080 if (w > app->scrw * 90 / 100)
1081 w = app->scrw * 90 / 100;
1082 if (h > app->scrh * 90 / 100)
1083 h = app->scrh * 90 / 100;
1084 if (w != app->winw || h != app->winh)
1085 winresize(app, w, h);
1086 }
1087
1088 winrepaint(app);
1089
1090 wincursor(app, ARROW);
1091 }
1092
1093 if (cookie.errors && app->errored == 0)
1094 {
1095 app->errored = 1;
1096 pdfapp_warn(app, "Errors found on page. Page rendering may be incomplete.");
1097 }
1098
1099 fz_flush_warnings(app->ctx);
1100 }
1101
pdfapp_gotouri(pdfapp_t * app,char * uri)1102 static void pdfapp_gotouri(pdfapp_t *app, char *uri)
1103 {
1104 char buf[PATH_MAX];
1105
1106 /* Relative file:// URI, make it absolute! */
1107 if (!strncmp(uri, "file://", 7) && uri[7] != '/')
1108 {
1109 char buf_base[PATH_MAX];
1110 char buf_cwd[PATH_MAX];
1111 fz_dirname(buf_base, app->docpath, sizeof buf_base);
1112 getcwd(buf_cwd, sizeof buf_cwd);
1113 fz_snprintf(buf, sizeof buf, "file://%s/%s/%s", buf_cwd, buf_base, uri+7);
1114 fz_cleanname(buf+7);
1115 uri = buf;
1116 }
1117
1118 winopenuri(app, uri);
1119 }
1120
pdfapp_gotopage(pdfapp_t * app,int number)1121 void pdfapp_gotopage(pdfapp_t *app, int number)
1122 {
1123 app->issearching = 0;
1124 winrepaint(app);
1125
1126 if (number < 1)
1127 number = 1;
1128 if (number > app->pagecount)
1129 number = app->pagecount;
1130
1131 if (number == app->pageno)
1132 return;
1133
1134 if (app->histlen + 1 == 256)
1135 {
1136 memmove(app->hist, app->hist + 1, sizeof(int) * 255);
1137 app->histlen --;
1138 }
1139 app->hist[app->histlen++] = app->pageno;
1140 app->pageno = number;
1141 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1142 }
1143
pdfapp_inverthit(pdfapp_t * app)1144 void pdfapp_inverthit(pdfapp_t *app)
1145 {
1146 fz_rect bbox;
1147 fz_matrix ctm;
1148 int i;
1149
1150 pdfapp_viewctm(&ctm, app);
1151
1152 for (i = 0; i < app->hit_count; i++)
1153 {
1154 bbox = fz_rect_from_quad(app->hit_bbox[i]);
1155 bbox = fz_transform_rect(bbox, ctm);
1156 pdfapp_invert(app, bbox);
1157 }
1158 }
1159
pdfapp_search_in_direction(pdfapp_t * app,enum panning * panto,int dir)1160 static void pdfapp_search_in_direction(pdfapp_t *app, enum panning *panto, int dir)
1161 {
1162 int firstpage, page;
1163
1164 /* abort if no search string */
1165 if (app->search[0] == 0)
1166 {
1167 winrepaint(app);
1168 return;
1169 }
1170
1171 wincursor(app, WAIT);
1172
1173 firstpage = app->pageno;
1174 if (app->searchpage == app->pageno)
1175 page = app->pageno + dir;
1176 else
1177 page = app->pageno;
1178
1179 if (page < 1) page = app->pagecount;
1180 if (page > app->pagecount) page = 1;
1181
1182 do
1183 {
1184 if (page != app->pageno)
1185 {
1186 app->pageno = page;
1187 pdfapp_showpage(app, 1, 0, 0, 0, 1);
1188 }
1189
1190 app->hit_count = fz_search_stext_page(app->ctx, app->page_text, app->search, app->hit_bbox, nelem(app->hit_bbox));
1191 if (app->hit_count > 0)
1192 {
1193 *panto = dir == 1 ? PAN_TO_TOP : PAN_TO_BOTTOM;
1194 app->searchpage = app->pageno;
1195 wincursor(app, HAND);
1196 winrepaint(app);
1197 return;
1198 }
1199
1200 page += dir;
1201 if (page < 1) page = app->pagecount;
1202 if (page > app->pagecount) page = 1;
1203 } while (page != firstpage);
1204
1205 pdfapp_warn(app, "String '%s' not found.", app->search);
1206
1207 app->pageno = firstpage;
1208 pdfapp_showpage(app, 1, 0, 0, 0, 0);
1209 wincursor(app, HAND);
1210 winrepaint(app);
1211 }
1212
pdfapp_onresize(pdfapp_t * app,int w,int h)1213 void pdfapp_onresize(pdfapp_t *app, int w, int h)
1214 {
1215 if (app->winw != w || app->winh != h)
1216 {
1217 app->winw = w;
1218 app->winh = h;
1219 pdfapp_panview(app, app->panx, app->pany);
1220 winrepaint(app);
1221 }
1222 }
1223
pdfapp_autozoom_vertical(pdfapp_t * app)1224 void pdfapp_autozoom_vertical(pdfapp_t *app)
1225 {
1226 app->resolution *= (float) app->winh / app->imgh;
1227 if (app->resolution > MAXRES)
1228 app->resolution = MAXRES;
1229 else if (app->resolution < MINRES)
1230 app->resolution = MINRES;
1231 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1232 }
1233
pdfapp_autozoom_horizontal(pdfapp_t * app)1234 void pdfapp_autozoom_horizontal(pdfapp_t *app)
1235 {
1236 app->resolution *= (float) app->winw / app->imgw;
1237 if (app->resolution > MAXRES)
1238 app->resolution = MAXRES;
1239 else if (app->resolution < MINRES)
1240 app->resolution = MINRES;
1241 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1242 }
1243
pdfapp_autozoom(pdfapp_t * app)1244 void pdfapp_autozoom(pdfapp_t *app)
1245 {
1246 float page_aspect = (float) app->imgw / app->imgh;
1247 float win_aspect = (float) app->winw / app->winh;
1248 if (page_aspect > win_aspect)
1249 pdfapp_autozoom_horizontal(app);
1250 else
1251 pdfapp_autozoom_vertical(app);
1252 }
1253
pdfapp_onkey(pdfapp_t * app,int c,int modifiers)1254 void pdfapp_onkey(pdfapp_t *app, int c, int modifiers)
1255 {
1256 int oldpage = app->pageno;
1257 enum panning panto = PAN_TO_TOP;
1258 int loadpage = 1;
1259
1260 if (app->issearching)
1261 {
1262 size_t n = strlen(app->search);
1263 if (c < ' ')
1264 {
1265 if (c == '\b' && n > 0)
1266 {
1267 app->search[n - 1] = 0;
1268 winrepaintsearch(app);
1269 }
1270 if (c == '\n' || c == '\r')
1271 {
1272 app->issearching = 0;
1273 if (n > 0)
1274 {
1275 winrepaintsearch(app);
1276
1277 if (app->searchdir < 0)
1278 {
1279 if (app->pageno == 1)
1280 app->pageno = app->pagecount;
1281 else
1282 app->pageno--;
1283 pdfapp_showpage(app, 1, 1, 0, 0, 1);
1284 }
1285
1286 pdfapp_onkey(app, 'n', 0);
1287 }
1288 else
1289 winrepaint(app);
1290 }
1291 if (c == '\033')
1292 {
1293 app->issearching = 0;
1294 winrepaint(app);
1295 }
1296 }
1297 else
1298 {
1299 if (n + 2 < sizeof app->search)
1300 {
1301 app->search[n] = c;
1302 app->search[n + 1] = 0;
1303 winrepaintsearch(app);
1304 }
1305 }
1306 return;
1307 }
1308
1309 /*
1310 * Save numbers typed for later
1311 */
1312
1313 if (c >= '0' && c <= '9')
1314 {
1315 app->number[app->numberlen++] = c;
1316 app->number[app->numberlen] = '\0';
1317 }
1318
1319 switch (c)
1320 {
1321 case 'q':
1322 save_accelerator(app->ctx, app->doc, app->docpath);
1323 winclose(app);
1324 break;
1325
1326 case '<':
1327 if (app->layout_em > 6)
1328 {
1329 fz_bookmark mark = fz_make_bookmark(app->ctx, app->doc, fz_location_from_page_number(app->ctx, app->doc, app->pageno));
1330 app->layout_em -= 1;
1331 fz_layout_document(app->ctx, app->doc, app->layout_w, app->layout_h, app->layout_em);
1332 app->pagecount = fz_count_pages(app->ctx, app->doc);
1333 app->pageno = fz_page_number_from_location(app->ctx, app->doc, fz_lookup_bookmark(app->ctx, app->doc, mark));
1334 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1335 }
1336 break;
1337 case '>':
1338 if (app->layout_em < 36)
1339 {
1340 fz_bookmark mark = fz_make_bookmark(app->ctx, app->doc, fz_location_from_page_number(app->ctx, app->doc, app->pageno));
1341 app->layout_em += 1;
1342 fz_layout_document(app->ctx, app->doc, app->layout_w, app->layout_h, app->layout_em);
1343 app->pagecount = fz_count_pages(app->ctx, app->doc);
1344 app->pageno = fz_page_number_from_location(app->ctx, app->doc, fz_lookup_bookmark(app->ctx, app->doc, mark));
1345 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1346 }
1347 break;
1348
1349 /*
1350 * Zoom and rotate
1351 */
1352
1353 case '+':
1354 app->resolution = zoom_in(app->resolution);
1355 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1356 break;
1357 case '-':
1358 app->resolution = zoom_out(app->resolution);
1359 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1360 break;
1361
1362 case 'W':
1363 pdfapp_autozoom_horizontal(app);
1364 break;
1365 case 'H':
1366 pdfapp_autozoom_vertical(app);
1367 break;
1368 case 'Z':
1369 pdfapp_autozoom(app);
1370 break;
1371 case 'z':
1372 if (app->numberlen > 0)
1373 app->resolution = atoi(app->number);
1374 else
1375 app->resolution = app->default_resolution;
1376 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1377 break;
1378
1379 case '[':
1380 if (app->numberlen > 0)
1381 app->rotate -= atoi(app->number);
1382 else
1383 app->rotate -= 90;
1384 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1385 break;
1386 case ']':
1387 if (app->numberlen > 0)
1388 app->rotate += atoi(app->number);
1389 else
1390 app->rotate += 90;
1391 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1392 break;
1393
1394 /*
1395 * Rendering and color management parameters.
1396 */
1397
1398 case 'C':
1399 app->tint ^= 1;
1400 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1401 break;
1402
1403 case 'c':
1404 app->grayscale ^= 1;
1405 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1406 break;
1407
1408 case 'I':
1409 app->invert ^= 1;
1410 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1411 break;
1412
1413 case 'E':
1414 app->useicc ^= 1;
1415 if (app->useicc)
1416 pdfapp_warn(app, "Using icc.");
1417 else
1418 pdfapp_warn(app, "Not using icc.");
1419 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1420 break;
1421
1422 case 'e':
1423 app->useseparations ^= 1;
1424 if (app->useseparations)
1425 pdfapp_warn(app, "Using separations.");
1426 else
1427 pdfapp_warn(app, "Not using separations.");
1428 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1429 break;
1430
1431 case 'A':
1432 if (app->numberlen > 0)
1433 app->aalevel = atoi(app->number);
1434 else
1435 app->aalevel = (app->aalevel == 8 ? 0 : 8);
1436 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1437 break;
1438
1439 /*
1440 * Pan view, but don't need to repaint image
1441 */
1442
1443 case 'f':
1444 app->shrinkwrap = 0;
1445 winfullscreen(app, !app->fullscreen);
1446 app->fullscreen = !app->fullscreen;
1447 break;
1448
1449 case 'w':
1450 if (app->fullscreen)
1451 {
1452 winfullscreen(app, 0);
1453 app->fullscreen = 0;
1454 }
1455 app->shrinkwrap = 1;
1456 app->panx = app->pany = 0;
1457 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1458 break;
1459
1460 case 'h':
1461 app->panx += app->imgw / 10;
1462 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1463 break;
1464
1465 case 'j':
1466 {
1467 if (app->pany + app->imgh <= app->winh)
1468 goto pagedown;
1469 if (app->imgh <= app->winh || app->pany <= app->winh - app->imgh)
1470 {
1471 panto = PAN_TO_TOP;
1472 app->pageno++;
1473 }
1474 else
1475 {
1476 app->pany -= app->imgh / 10;
1477 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1478 }
1479 break;
1480 }
1481
1482 case 'k':
1483 {
1484 if (app->pany >= 0)
1485 goto pageup;
1486 if (app->imgh <= app->winh || app->pany == 0)
1487 {
1488 panto = PAN_TO_BOTTOM;
1489 app->pageno--;
1490 }
1491 else
1492 {
1493 app->pany += app->imgh / 10;
1494 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1495 }
1496 break;
1497 }
1498
1499 case 'l':
1500 app->panx -= app->imgw / 10;
1501 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1502 break;
1503
1504 /*
1505 * Page navigation
1506 */
1507
1508 case 'g':
1509 if (app->numberlen > 0)
1510 pdfapp_gotopage(app, atoi(app->number));
1511 else
1512 pdfapp_gotopage(app, 1);
1513 break;
1514
1515 case 'G':
1516 pdfapp_gotopage(app, app->pagecount);
1517 break;
1518
1519 case 'm':
1520 if (app->numberlen > 0)
1521 {
1522 int idx = atoi(app->number);
1523 if (idx >= 0 && idx < (int)nelem(app->marks))
1524 app->marks[idx] = app->pageno;
1525 }
1526 else
1527 {
1528 if (app->histlen + 1 == 256)
1529 {
1530 memmove(app->hist, app->hist + 1, sizeof(int) * 255);
1531 app->histlen --;
1532 }
1533 app->hist[app->histlen++] = app->pageno;
1534 }
1535 break;
1536
1537 case 't':
1538 if (app->numberlen > 0)
1539 {
1540 int idx = atoi(app->number);
1541
1542 if (idx >= 0 && idx < (int)nelem(app->marks))
1543 if (app->marks[idx] > 0)
1544 app->pageno = app->marks[idx];
1545 }
1546 else if (app->histlen > 0)
1547 app->pageno = app->hist[--app->histlen];
1548 break;
1549
1550 case 'p':
1551 app->presentation_mode = !app->presentation_mode;
1552 break;
1553
1554 /*
1555 * Back and forth ...
1556 */
1557
1558 case ',':
1559 pageup:
1560 panto = DONT_PAN;
1561 if (app->numberlen > 0)
1562 app->pageno -= atoi(app->number);
1563 else
1564 app->pageno--;
1565 break;
1566
1567 case '.':
1568 pagedown:
1569 panto = DONT_PAN;
1570 if (app->numberlen > 0)
1571 app->pageno += atoi(app->number);
1572 else
1573 app->pageno++;
1574 break;
1575
1576 case 'b':
1577 {
1578 int number = 1;
1579 if (app->numberlen > 0)
1580 number = fz_maxi(atoi(app->number), number);
1581 while (number--)
1582 {
1583 if (app->pany >= -app->imgh/20)
1584 {
1585 if (app->panx >= -app->imgw/20)
1586 {
1587 if (app->pageno - 1 > 0)
1588 {
1589 app->panx = INT_MIN;
1590 app->pany = INT_MIN;
1591 app->pageno--;
1592 panto = DONT_PAN;
1593 }
1594 }
1595 else
1596 {
1597 app->pany = -app->imgh;
1598 app->panx += app->winw * 9 / 10;
1599 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1600 }
1601 }
1602 else
1603 {
1604 app->pany += app->winh * 9 / 10;
1605 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1606 }
1607 }
1608 }
1609 break;
1610
1611 case ' ':
1612 {
1613 int number = 1;
1614 if (app->numberlen > 0)
1615 number = fz_maxi(atoi(app->number), number);
1616 while (number--)
1617 {
1618 if (app->imgh + app->pany <= app->winh + app->imgh/20)
1619 {
1620 if (app->imgw + app->panx <= app->winw + app->imgw/20)
1621 {
1622 if (app->pageno + 1 <= app->pagecount)
1623 {
1624 app->panx = 0;
1625 app->pany = 0;
1626 app->pageno++;
1627 panto = DONT_PAN;
1628 }
1629 }
1630 else
1631 {
1632 app->pany = 0;
1633 app->panx -= app->winw * 9 / 10;
1634 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1635 }
1636 }
1637 else
1638 {
1639 app->pany -= app->winh * 9 / 10;
1640 pdfapp_showpage(app, 0, 0, 1, 0, 0);
1641 }
1642 }
1643 }
1644 break;
1645
1646 /*
1647 * Saving the file
1648 */
1649 case 'S':
1650 pdfapp_save(app);
1651 break;
1652
1653 /*
1654 * Reloading the file...
1655 */
1656
1657 case 'r':
1658 panto = DONT_PAN;
1659 oldpage = -1;
1660 pdfapp_reloadfile(app);
1661 break;
1662
1663 /*
1664 * Searching
1665 */
1666
1667 case '?':
1668 app->issearching = 1;
1669 app->searchdir = -1;
1670 app->search[0] = 0;
1671 app->hit_count = 0;
1672 app->searchpage = -1;
1673 winrepaintsearch(app);
1674 break;
1675
1676 case '/':
1677 app->issearching = 1;
1678 app->searchdir = 1;
1679 app->search[0] = 0;
1680 app->hit_count = 0;
1681 app->searchpage = -1;
1682 winrepaintsearch(app);
1683 break;
1684
1685 case 'n':
1686 if (app->searchdir > 0)
1687 pdfapp_search_in_direction(app, &panto, 1);
1688 else
1689 pdfapp_search_in_direction(app, &panto, -1);
1690 loadpage = 0;
1691 break;
1692
1693 case 'N':
1694 if (app->searchdir > 0)
1695 pdfapp_search_in_direction(app, &panto, -1);
1696 else
1697 pdfapp_search_in_direction(app, &panto, 1);
1698 loadpage = 0;
1699 break;
1700 }
1701
1702 if (c < '0' || c > '9')
1703 app->numberlen = 0;
1704
1705 if (app->pageno < 1)
1706 app->pageno = 1;
1707 if (app->pageno > app->pagecount)
1708 app->pageno = app->pagecount;
1709
1710 if (app->pageno != oldpage)
1711 {
1712 switch (panto)
1713 {
1714 case PAN_TO_TOP:
1715 app->pany = 0;
1716 break;
1717 case PAN_TO_BOTTOM:
1718 app->pany = INT_MIN;
1719 break;
1720 case DONT_PAN:
1721 break;
1722 }
1723 pdfapp_showpage(app, loadpage, 1, 1, 1, 0);
1724 }
1725 }
1726
handlescroll(pdfapp_t * app,int modifiers,int dir)1727 static void handlescroll(pdfapp_t *app, int modifiers, int dir)
1728 {
1729 app->ispanning = app->iscopying = 0;
1730 if (modifiers & (1<<2))
1731 {
1732 /* zoom in/out if ctrl is pressed */
1733 if (dir > 0)
1734 app->resolution = zoom_in(app->resolution);
1735 else
1736 app->resolution = zoom_out(app->resolution);
1737 if (app->resolution > MAXRES)
1738 app->resolution = MAXRES;
1739 if (app->resolution < MINRES)
1740 app->resolution = MINRES;
1741 pdfapp_showpage(app, 0, 1, 1, 0, 0);
1742 }
1743 else
1744 {
1745 /* scroll up/down, or left/right if
1746 shift is pressed */
1747 int xstep = 0;
1748 int ystep = 0;
1749 int pagestep = 0;
1750 if (modifiers & (1<<0))
1751 {
1752 if (dir > 0 && app->panx >= 0)
1753 pagestep = -1;
1754 else if (dir < 0 && app->panx <= app->winw - app->imgw)
1755 pagestep = 1;
1756 else
1757 xstep = 20 * dir;
1758 }
1759 else
1760 {
1761 if (dir > 0 && app->pany >= 0)
1762 pagestep = -1;
1763 else if (dir < 0 && app->pany <= app->winh - app->imgh)
1764 pagestep = 1;
1765 else
1766 ystep = 20 * dir;
1767 }
1768 if (pagestep == 0)
1769 pdfapp_panview(app, app->panx + xstep, app->pany + ystep);
1770 else if (pagestep > 0 && app->pageno < app->pagecount)
1771 {
1772 app->pageno++;
1773 app->pany = 0;
1774 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1775 }
1776 else if (pagestep < 0 && app->pageno > 1)
1777 {
1778 app->pageno--;
1779 app->pany = INT_MIN;
1780 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1781 }
1782 }
1783 }
1784
pdfapp_onmouse(pdfapp_t * app,int x,int y,int btn,int modifiers,int state)1785 void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state)
1786 {
1787 fz_context *ctx = app->ctx;
1788 fz_irect irect = { 0, 0, app->layout_w, app->layout_h };
1789 fz_link *link;
1790 fz_matrix ctm;
1791 fz_point p;
1792 int processed = 0;
1793
1794 if (app->image)
1795 irect = fz_pixmap_bbox(app->ctx, app->image);
1796 p.x = x - app->panx + irect.x0;
1797 p.y = y - app->pany + irect.y0;
1798
1799 pdfapp_viewctm(&ctm, app);
1800 ctm = fz_invert_matrix(ctm);
1801
1802 p = fz_transform_point(p, ctm);
1803
1804 for (link = app->page_links; link; link = link->next)
1805 {
1806 if (p.x >= link->rect.x0 && p.x <= link->rect.x1)
1807 if (p.y >= link->rect.y0 && p.y <= link->rect.y1)
1808 break;
1809 }
1810
1811 if (link)
1812 {
1813 wincursor(app, HAND);
1814 if (btn == 1 && state == 1 && !processed)
1815 {
1816 if (fz_is_external_link(ctx, link->uri))
1817 pdfapp_gotouri(app, link->uri);
1818 else
1819 {
1820 fz_location loc = fz_resolve_link(ctx, app->doc, link->uri, NULL, NULL);
1821 pdfapp_gotopage(app, fz_page_number_from_location(ctx, app->doc, loc)+1);
1822 }
1823 return;
1824 }
1825 }
1826 else
1827 {
1828 wincursor(app, ARROW);
1829 }
1830
1831 if (state == 1 && !processed)
1832 {
1833 if (btn == 1 && !app->iscopying)
1834 {
1835 app->ispanning = 1;
1836 app->selx = x;
1837 app->sely = y;
1838 app->beyondy = 0;
1839 }
1840 if (btn == 3 && !app->ispanning)
1841 {
1842 app->iscopying = 1;
1843 app->selx = x;
1844 app->sely = y;
1845 app->selr.x0 = x;
1846 app->selr.x1 = x;
1847 app->selr.y0 = y;
1848 app->selr.y1 = y;
1849 }
1850 if (btn == 4 || btn == 5) /* scroll wheel */
1851 {
1852 handlescroll(app, modifiers, btn == 4 ? 1 : -1);
1853 }
1854 if (btn == 6 || btn == 7) /* scroll wheel (horizontal) */
1855 {
1856 /* scroll left/right or up/down if shift is pressed */
1857 handlescroll(app, modifiers ^ (1<<0), btn == 6 ? 1 : -1);
1858 }
1859 if (app->presentation_mode)
1860 {
1861 if (btn == 1 && app->pageno < app->pagecount)
1862 {
1863 app->pageno++;
1864 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1865 }
1866 if (btn == 3 && app->pageno > 1)
1867 {
1868 app->pageno--;
1869 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1870 }
1871 }
1872 }
1873
1874 else if (state == -1)
1875 {
1876 if (app->iscopying)
1877 {
1878 app->iscopying = 0;
1879 app->selr.x0 = fz_mini(app->selx, x) - app->panx + irect.x0;
1880 app->selr.x1 = fz_maxi(app->selx, x) - app->panx + irect.x0;
1881 app->selr.y0 = fz_mini(app->sely, y) - app->pany + irect.y0;
1882 app->selr.y1 = fz_maxi(app->sely, y) - app->pany + irect.y0;
1883 winrepaint(app);
1884 if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1)
1885 windocopy(app);
1886 }
1887 app->ispanning = 0;
1888 }
1889
1890 else if (app->ispanning)
1891 {
1892 int newx = app->panx + x - app->selx;
1893 int newy = app->pany + y - app->sely;
1894 int imgh = app->winh;
1895 if (app->image)
1896 imgh = fz_pixmap_height(app->ctx, app->image);
1897
1898 /* Scrolling beyond limits implies flipping pages */
1899 /* Are we requested to scroll beyond limits? */
1900 if (newy + imgh < app->winh || newy > 0)
1901 {
1902 /* Yes. We can assume that deltay != 0 */
1903 int deltay = y - app->sely;
1904 /* Check whether the panning has occurred in the
1905 * direction that we are already crossing the
1906 * limit it. If not, we can conclude that we
1907 * have switched ends of the page and will thus
1908 * start over counting.
1909 */
1910 if( app->beyondy == 0 || (app->beyondy ^ deltay) >= 0 )
1911 {
1912 /* Updating how far we are beyond and
1913 * flipping pages if beyond threshold
1914 */
1915 app->beyondy += deltay;
1916 if (app->beyondy > BEYOND_THRESHHOLD)
1917 {
1918 if( app->pageno > 1 )
1919 {
1920 app->pageno--;
1921 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1922 if (app->image)
1923 newy = -fz_pixmap_height(app->ctx, app->image);
1924 }
1925 app->beyondy = 0;
1926 }
1927 else if (app->beyondy < -BEYOND_THRESHHOLD)
1928 {
1929 if( app->pageno < app->pagecount )
1930 {
1931 app->pageno++;
1932 pdfapp_showpage(app, 1, 1, 1, 0, 0);
1933 newy = 0;
1934 }
1935 app->beyondy = 0;
1936 }
1937 }
1938 else
1939 app->beyondy = 0;
1940 }
1941 /* Although at this point we've already determined that
1942 * or that no scrolling will be performed in
1943 * y-direction, the x-direction has not yet been taken
1944 * care off. Therefore
1945 */
1946 pdfapp_panview(app, newx, newy);
1947
1948 app->selx = x;
1949 app->sely = y;
1950 }
1951
1952 else if (app->iscopying)
1953 {
1954 app->selr.x0 = fz_mini(app->selx, x) - app->panx + irect.x0;
1955 app->selr.x1 = fz_maxi(app->selx, x) - app->panx + irect.x0;
1956 app->selr.y0 = fz_mini(app->sely, y) - app->pany + irect.y0;
1957 app->selr.y1 = fz_maxi(app->sely, y) - app->pany + irect.y0;
1958 winrepaint(app);
1959 }
1960 }
1961
pdfapp_oncopy(pdfapp_t * app,unsigned short * ucsbuf,int ucslen)1962 void pdfapp_oncopy(pdfapp_t *app, unsigned short *ucsbuf, int ucslen)
1963 {
1964 fz_matrix ctm;
1965 fz_stext_page *page = app->page_text;
1966 int p, need_newline;
1967 fz_stext_block *block;
1968 fz_stext_line *line;
1969 fz_stext_char *ch;
1970 fz_rect sel;
1971
1972 pdfapp_viewctm(&ctm, app);
1973 ctm = fz_invert_matrix(ctm);
1974 sel = fz_transform_rect(app->selr, ctm);
1975
1976 p = 0;
1977 need_newline = 0;
1978
1979 for (block = page->first_block; block; block = block->next)
1980 {
1981 if (block->type != FZ_STEXT_BLOCK_TEXT)
1982 continue;
1983
1984 for (line = block->u.t.first_line; line; line = line->next)
1985 {
1986 int saw_text = 0;
1987 for (ch = line->first_char; ch; ch = ch->next)
1988 {
1989 fz_rect bbox = fz_rect_from_quad(ch->quad);
1990 int c = ch->c;
1991 if (c < 32)
1992 c = 0xFFFD;
1993 if (bbox.x1 >= sel.x0 && bbox.x0 <= sel.x1 && bbox.y1 >= sel.y0 && bbox.y0 <= sel.y1)
1994 {
1995 saw_text = 1;
1996 if (need_newline)
1997 {
1998 #ifdef _WIN32
1999 if (p < ucslen - 1)
2000 ucsbuf[p++] = '\r';
2001 #endif
2002 if (p < ucslen - 1)
2003 ucsbuf[p++] = '\n';
2004 need_newline = 0;
2005 }
2006 if (p < ucslen - 1)
2007 ucsbuf[p++] = c;
2008 }
2009 }
2010 if (saw_text)
2011 need_newline = 1;
2012 }
2013 }
2014
2015 ucsbuf[p] = 0;
2016 }
2017
pdfapp_postblit(pdfapp_t * app)2018 void pdfapp_postblit(pdfapp_t *app)
2019 {
2020 clock_t time;
2021 float seconds;
2022 int llama;
2023
2024 app->transitions_enabled = 1;
2025 if (!app->in_transit)
2026 return;
2027 time = clock();
2028 seconds = (float)(time - app->start_time) / CLOCKS_PER_SEC;
2029 llama = seconds * 256 / app->transition.duration;
2030 if (llama >= 256)
2031 {
2032 /* Completed. */
2033 fz_drop_pixmap(app->ctx, app->image);
2034 app->image = app->new_image;
2035 app->new_image = NULL;
2036 app->imgw = fz_pixmap_width(app->ctx, app->image);
2037 app->imgh = fz_pixmap_height(app->ctx, app->image);
2038 fz_drop_pixmap(app->ctx, app->old_image);
2039 app->old_image = NULL;
2040 if (app->duration != 0)
2041 winadvancetimer(app, app->duration);
2042 }
2043 else
2044 fz_generate_transition(app->ctx, app->image, app->old_image, app->new_image, llama, &app->transition);
2045 winrepaint(app);
2046 if (llama >= 256)
2047 {
2048 /* Completed. */
2049 app->in_transit = 0;
2050 }
2051 }
2052