1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
|
#include "pdfapp.h"
#include <ctype.h> /* for tolower() */
#define ZOOMSTEP 1.142857
#define BEYOND_THRESHHOLD 40
#ifndef PATH_MAX
#define PATH_MAX (1024)
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
enum panning
{
DONT_PAN = 0,
PAN_TO_TOP,
PAN_TO_BOTTOM
};
static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repaint, int transition);
static void pdfapp_updatepage(pdfapp_t *app);
static void pdfapp_warn(pdfapp_t *app, const char *fmt, ...)
{
char buf[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = 0;
winwarn(app, buf);
}
static void pdfapp_error(pdfapp_t *app, char *msg)
{
winerror(app, msg);
}
char *pdfapp_version(pdfapp_t *app)
{
return
"MuPDF 1.2\n"
"Copyright 2006-2013 Artifex Software, Inc.\n";
}
char *pdfapp_usage(pdfapp_t *app)
{
return
"L\t\t-- rotate left\n"
"R\t\t-- rotate right\n"
"h\t\t-- scroll left\n"
"j down\t\t-- scroll down\n"
"k up\t\t-- scroll up\n"
"l\t\t-- scroll right\n"
"+\t\t-- zoom in\n"
"-\t\t-- zoom out\n"
"W\t\t-- zoom to fit window width\n"
"H\t\t-- zoom to fit window height\n"
"w\t\t-- shrinkwrap\n"
"f\t\t-- fullscreen\n"
"r\t\t-- reload file\n"
". pgdn right spc\t-- next page\n"
", pgup left b bkspc\t-- previous page\n"
">\t\t-- next 10 pages\n"
"<\t\t-- back 10 pages\n"
"m\t\t-- mark page for snap back\n"
"t\t\t-- pop back to latest mark\n"
"1m\t\t-- mark page in register 1\n"
"1t\t\t-- go to page in register 1\n"
"G\t\t-- go to last page\n"
"123g\t\t-- go to page 123\n"
"/\t\t-- search forwards for text\n"
"?\t\t-- search backwards for text\n"
"n\t\t-- find next search result\n"
"N\t\t-- find previous search result\n"
"c\t\t-- toggle between color and grayscale\n"
"i\t\t-- toggle inverted color mode\n"
"q\t\t-- quit\n"
;
}
void pdfapp_init(fz_context *ctx, pdfapp_t *app)
{
memset(app, 0, sizeof(pdfapp_t));
app->scrw = 640;
app->scrh = 480;
app->resolution = 72;
app->ctx = ctx;
#ifdef _WIN32
app->colorspace = fz_device_bgr(ctx);
#else
app->colorspace = fz_device_rgb(ctx);
#endif
}
void pdfapp_invert(pdfapp_t *app, const fz_rect *rect)
{
fz_irect b;
fz_invert_pixmap_rect(app->image, fz_round_rect(&b, rect));
}
static void event_cb(pdf_doc_event *event, void *data)
{
pdfapp_t *app = (pdfapp_t *)data;
switch (event->type)
{
case PDF_DOCUMENT_EVENT_ALERT:
{
pdf_alert_event *alert = pdf_access_alert_event(event);
winalert(app, alert);
}
break;
case PDF_DOCUMENT_EVENT_PRINT:
winprint(app);
break;
case PDF_DOCUMENT_EVENT_EXEC_MENU_ITEM:
{
char *item = pdf_access_exec_menu_item_event(event);
if (!strcmp(item, "Print"))
winprint(app);
else
pdfapp_warn(app, "The document attempted to execute menu item: %s. (Not supported)", item);
}
break;
case PDF_DOCUMENT_EVENT_EXEC_DIALOG:
pdfapp_warn(app, "The document attempted to open a dialog box. (Not supported)");
break;
case PDF_DOCUMENT_EVENT_LAUNCH_URL:
{
pdf_launch_url_event *launch_url = pdf_access_launch_url_event(event);
pdfapp_warn(app, "The document attempted to open url: %s. (Not supported by app)", launch_url->url);
}
break;
case PDF_DOCUMENT_EVENT_MAIL_DOC:
{
pdf_mail_doc_event *mail_doc = pdf_access_mail_doc_event(event);
pdfapp_warn(app, "The document attmepted to mail the document%s%s%s%s%s%s%s%s (Not supported)",
mail_doc->to[0]?", To: ":"", mail_doc->to,
mail_doc->cc[0]?", Cc: ":"", mail_doc->cc,
mail_doc->bcc[0]?", Bcc: ":"", mail_doc->bcc,
mail_doc->subject[0]?", Subject: ":"", mail_doc->subject);
}
break;
}
}
void pdfapp_open(pdfapp_t *app, char *filename, int reload)
{
fz_context *ctx = app->ctx;
char *password = "";
fz_try(ctx)
{
pdf_document *idoc;
app->doc = fz_open_document(ctx, filename);
idoc = pdf_specifics(app->doc);
if (idoc)
pdf_set_doc_event_callback(idoc, event_cb, app);
if (fz_needs_password(app->doc))
{
int okay = fz_authenticate_password(app->doc, password);
while (!okay)
{
password = winpassword(app, filename);
if (!password)
fz_throw(ctx, "Needs a password");
okay = fz_authenticate_password(app->doc, password);
if (!okay)
pdfapp_warn(app, "Invalid password.");
}
}
app->docpath = fz_strdup(ctx, filename);
app->doctitle = filename;
if (strrchr(app->doctitle, '\\'))
app->doctitle = strrchr(app->doctitle, '\\') + 1;
if (strrchr(app->doctitle, '/'))
app->doctitle = strrchr(app->doctitle, '/') + 1;
app->doctitle = fz_strdup(ctx, app->doctitle);
app->pagecount = fz_count_pages(app->doc);
app->outline = fz_load_outline(app->doc);
}
fz_catch(ctx)
{
pdfapp_error(app, "cannot open document");
}
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, 0);
}
void pdfapp_close(pdfapp_t *app)
{
fz_drop_display_list(app->ctx, app->page_list);
app->page_list = NULL;
fz_drop_display_list(app->ctx, app->annotations_list);
app->annotations_list = NULL;
fz_free_text_page(app->ctx, app->page_text);
app->page_text = NULL;
fz_free_text_sheet(app->ctx, app->page_sheet);
app->page_sheet = NULL;
fz_drop_link(app->ctx, app->page_links);
app->page_links = NULL;
fz_free(app->ctx, app->doctitle);
app->doctitle = NULL;
fz_free(app->ctx, app->docpath);
app->docpath = NULL;
fz_drop_pixmap(app->ctx, app->image);
app->image = NULL;
fz_drop_pixmap(app->ctx, app->new_image);
app->new_image = NULL;
fz_drop_pixmap(app->ctx, app->old_image);
app->old_image = NULL;
fz_free_outline(app->ctx, app->outline);
app->outline = NULL;
fz_free_page(app->doc, app->page);
app->page = NULL;
fz_close_document(app->doc);
app->doc = NULL;
fz_flush_warnings(app->ctx);
}
static int gen_tmp_file(char *buf, int len)
{
int i;
char *name = strrchr(buf, '/');
if (name == NULL)
name = strrchr(buf, '\\');
if (name != NULL)
name++;
else
name = buf;
for (i = 0; i < 10000; i++)
{
FILE *f;
snprintf(name, buf+len-name, "tmp%04d", i);
f = fopen(buf, "r");
if (f == NULL)
return 1;
fclose(f);
}
return 0;
}
static int pdfapp_save(pdfapp_t *app)
{
char buf[PATH_MAX];
if (wingetsavepath(app, buf, PATH_MAX))
{
fz_write_options opts;
opts.do_ascii = 1;
opts.do_expand = 0;
opts.do_garbage = 1;
opts.do_linear = 0;
if (strcmp(buf, app->docpath) != 0)
{
fz_write_document(app->doc, buf, &opts);
return 1;
}
if (gen_tmp_file(buf, PATH_MAX))
{
int written;
fz_try(app->ctx)
{
fz_write_document(app->doc, buf, &opts);
written = 1;
}
fz_catch(app->ctx)
{
written = 0;
}
if (written)
{
char buf2[PATH_MAX];
fz_strlcpy(buf2, app->docpath, PATH_MAX);
pdfapp_close(app);
winreplacefile(buf, buf2);
pdfapp_open(app, buf2, 1);
return written;
}
}
}
return 0;
}
int pdfapp_preclose(pdfapp_t *app)
{
pdf_document *idoc = pdf_specifics(app->doc);
if (idoc && pdf_has_unsaved_changes(idoc))
{
switch (winsavequery(app))
{
case DISCARD:
return 1;
case CANCEL:
return 0;
case SAVE:
return pdfapp_save(app);
}
}
return 1;
}
static void pdfapp_viewctm(fz_matrix *mat, pdfapp_t *app)
{
fz_pre_rotate(fz_scale(mat, app->resolution/72.0f, app->resolution/72.0f), app->rotate);
}
static void pdfapp_panview(pdfapp_t *app, int newx, int newy)
{
int image_w = fz_pixmap_width(app->ctx, app->image);
int image_h = fz_pixmap_height(app->ctx, app->image);
if (newx > 0)
newx = 0;
if (newy > 0)
newy = 0;
if (newx + image_w < app->winw)
newx = app->winw - image_w;
if (newy + image_h < app->winh)
newy = app->winh - image_h;
if (app->winw >= image_w)
newx = (app->winw - image_w) / 2;
if (app->winh >= image_h)
newy = (app->winh - image_h) / 2;
if (newx != app->panx || newy != app->pany)
winrepaint(app);
app->panx = newx;
app->pany = newy;
}
static void pdfapp_loadpage(pdfapp_t *app)
{
fz_device *mdev = NULL;
int errored = 0;
fz_cookie cookie = { 0 };
fz_var(mdev);
fz_drop_display_list(app->ctx, app->page_list);
fz_drop_display_list(app->ctx, app->annotations_list);
fz_free_text_page(app->ctx, app->page_text);
fz_free_text_sheet(app->ctx, app->page_sheet);
fz_drop_link(app->ctx, app->page_links);
fz_free_page(app->doc, app->page);
app->page_list = NULL;
app->annotations_list = NULL;
app->page_text = NULL;
app->page_sheet = NULL;
app->page_links = NULL;
app->page = NULL;
app->page_bbox.x0 = 0;
app->page_bbox.y0 = 0;
app->page_bbox.x1 = 100;
app->page_bbox.y1 = 100;
fz_try(app->ctx)
{
app->page = fz_load_page(app->doc, app->pageno - 1);
fz_bound_page(app->doc, app->page, &app->page_bbox);
}
fz_catch(app->ctx)
{
pdfapp_warn(app, "Cannot load page");
return;
}
fz_try(app->ctx)
{
fz_annot *annot;
/* Create display lists */
app->page_list = fz_new_display_list(app->ctx);
mdev = fz_new_list_device(app->ctx, app->page_list);
fz_run_page_contents(app->doc, app->page, mdev, &fz_identity, &cookie);
fz_free_device(mdev);
mdev = NULL;
app->annotations_list = fz_new_display_list(app->ctx);
mdev = fz_new_list_device(app->ctx, app->annotations_list);
for (annot = fz_first_annot(app->doc, app->page); annot; annot = fz_next_annot(app->doc, annot))
fz_run_annot(app->doc, app->page, annot, mdev, &fz_identity, &cookie);
if (cookie.errors)
{
pdfapp_warn(app, "Errors found on page");
errored = 1;
}
}
fz_always(app->ctx)
{
fz_free_device(mdev);
}
fz_catch(app->ctx)
{
pdfapp_warn(app, "Cannot load page");
errored = 1;
}
fz_try(app->ctx)
{
app->page_links = fz_load_links(app->doc, app->page);
}
fz_catch(app->ctx)
{
if (!errored)
pdfapp_warn(app, "Cannot load page");
}
app->errored = errored;
}
static void pdfapp_recreate_annotationslist(pdfapp_t *app)
{
fz_device *mdev = NULL;
int errored = 0;
fz_cookie cookie = { 0 };
fz_var(mdev);
fz_drop_display_list(app->ctx, app->annotations_list);
app->annotations_list = NULL;
fz_try(app->ctx)
{
fz_annot *annot;
/* Create display list */
app->annotations_list = fz_new_display_list(app->ctx);
mdev = fz_new_list_device(app->ctx, app->annotations_list);
for (annot = fz_first_annot(app->doc, app->page); annot; annot = fz_next_annot(app->doc, annot))
fz_run_annot(app->doc, app->page, annot, mdev, &fz_identity, &cookie);
if (cookie.errors)
{
pdfapp_warn(app, "Errors found on page");
errored = 1;
}
}
fz_always(app->ctx)
{
fz_free_device(mdev);
}
fz_catch(app->ctx)
{
pdfapp_warn(app, "Cannot load page");
errored = 1;
}
app->errored = errored;
}
static void pdfapp_runpage(pdfapp_t *app, fz_device *dev, const fz_matrix *ctm, const fz_rect *rect, fz_cookie *cookie)
{
fz_begin_page(dev, rect, ctm);
if (app->page_list)
fz_run_display_list(app->page_list, dev, ctm, rect, cookie);
if (app->annotations_list)
fz_run_display_list(app->annotations_list, dev, ctm, rect, cookie);
fz_end_page(dev);
}
#define MAX_TITLE 256
static void pdfapp_updatepage(pdfapp_t *app)
{
pdf_document *idoc = pdf_specifics(app->doc);
fz_device *idev;
fz_matrix ctm;
fz_annot *annot;
pdfapp_viewctm(&ctm, app);
pdf_update_page(idoc, (pdf_page *)app->page);
pdfapp_recreate_annotationslist(app);
while ((annot = (fz_annot *)pdf_poll_changed_annot(idoc, (pdf_page *)app->page)) != NULL)
{
fz_rect bounds;
fz_irect ibounds;
fz_transform_rect(fz_bound_annot(app->doc, annot, &bounds), &ctm);
fz_rect_from_irect(&bounds, fz_round_rect(&ibounds, &bounds));
fz_clear_pixmap_rect_with_value(app->ctx, app->image, 255, &ibounds);
idev = fz_new_draw_device_with_bbox(app->ctx, app->image, &ibounds);
pdfapp_runpage(app, idev, &ctm, &bounds, NULL);
fz_free_device(idev);
}
pdfapp_showpage(app, 0, 0, 1, 0);
}
static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repaint, int transition)
{
char buf[MAX_TITLE];
fz_device *idev;
fz_device *tdev;
fz_colorspace *colorspace;
fz_matrix ctm;
fz_rect bounds;
fz_irect ibounds;
fz_cookie cookie = { 0 };
if (!app->nowaitcursor)
wincursor(app, WAIT);
if (!app->transitions_enabled || !app->presentation_mode)
transition = 0;
if (transition)
{
app->old_image = app->image;
app->image = NULL;
}
if (loadpage)
{
pdfapp_loadpage(app);
/* Zero search hit position */
app->hit_count = 0;
/* Extract text */
app->page_sheet = fz_new_text_sheet(app->ctx);
app->page_text = fz_new_text_page(app->ctx);
if (app->page_list || app->annotations_list)
{
tdev = fz_new_text_device(app->ctx, app->page_sheet, app->page_text);
pdfapp_runpage(app, tdev, &fz_identity, &fz_infinite_rect, &cookie);
fz_free_device(tdev);
}
}
if (drawpage)
{
char buf2[64];
int len;
sprintf(buf2, " - %d/%d (%d dpi)",
app->pageno, app->pagecount, app->resolution);
len = MAX_TITLE-strlen(buf2);
if ((int)strlen(app->doctitle) > len)
{
snprintf(buf, len-3, "%s", app->doctitle);
strcat(buf, "...");
strcat(buf, buf2);
}
else
sprintf(buf, "%s%s", app->doctitle, buf2);
wintitle(app, buf);
pdfapp_viewctm(&ctm, app);
bounds = app->page_bbox;
fz_round_rect(&ibounds, fz_transform_rect(&bounds, &ctm));
fz_rect_from_irect(&bounds, &ibounds);
/* Draw */
if (app->image)
fz_drop_pixmap(app->ctx, app->image);
if (app->grayscale)
colorspace = fz_device_gray(app->ctx);
else
colorspace = app->colorspace;
app->image = NULL;
app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, &ibounds);
fz_clear_pixmap_with_value(app->ctx, app->image, 255);
if (app->page_list || app->annotations_list)
{
idev = fz_new_draw_device(app->ctx, app->image);
pdfapp_runpage(app, idev, &ctm, &bounds, &cookie);
fz_free_device(idev);
}
if (app->invert)
fz_invert_pixmap(app->ctx, app->image);
}
if (transition)
{
fz_transition *new_trans;
app->new_image = app->image;
app->image = NULL;
app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, &ibounds);
app->duration = 0;
new_trans = fz_page_presentation(app->doc, app->page, &app->duration);
if (new_trans)
app->transition = *new_trans;
else
{
/* If no transition specified, use a default one */
memset(&app->transition, 0, sizeof(*new_trans));
app->transition.duration = 1.0;
app->transition.type = FZ_TRANSITION_WIPE;
app->transition.vertical = 0;
app->transition.direction = 0;
}
if (app->duration == 0)
app->duration = 5;
app->in_transit = fz_generate_transition(app->image, app->old_image, app->new_image, 0, &app->transition);
if (!app->in_transit)
{
if (app->duration != 0)
winadvancetimer(app, app->duration);
}
app->start_time = clock();
}
if (repaint)
{
pdfapp_panview(app, app->panx, app->pany);
if (app->shrinkwrap)
{
int w = fz_pixmap_width(app->ctx, app->image);
int h = fz_pixmap_height(app->ctx, app->image);
if (app->winw == w)
app->panx = 0;
if (app->winh == h)
app->pany = 0;
if (w > app->scrw * 90 / 100)
w = app->scrw * 90 / 100;
if (h > app->scrh * 90 / 100)
h = app->scrh * 90 / 100;
if (w != app->winw || h != app->winh)
winresize(app, w, h);
}
winrepaint(app);
wincursor(app, ARROW);
}
if (cookie.errors && app->errored == 0)
{
app->errored = 1;
pdfapp_warn(app, "Errors found on page. Page rendering may be incomplete.");
}
fz_flush_warnings(app->ctx);
}
static void pdfapp_gotouri(pdfapp_t *app, char *uri)
{
winopenuri(app, uri);
}
void pdfapp_gotopage(pdfapp_t *app, int number)
{
app->isediting = 0;
winrepaint(app);
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, 0);
}
void pdfapp_inverthit(pdfapp_t *app)
{
fz_rect bbox;
fz_matrix ctm;
int i;
pdfapp_viewctm(&ctm, app);
for (i = 0; i < app->hit_count; i++)
{
bbox = app->hit_bbox[i];
pdfapp_invert(app, fz_transform_rect(&bbox, &ctm));
}
}
static void pdfapp_search_in_direction(pdfapp_t *app, enum panning *panto, int dir)
{
int firstpage, page;
wincursor(app, WAIT);
firstpage = app->pageno;
if (app->searchpage == app->pageno)
page = app->pageno + dir;
else
page = app->pageno;
if (page < 1) page = app->pagecount;
if (page > app->pagecount) page = 1;
do
{
if (page != app->pageno)
{
app->pageno = page;
pdfapp_showpage(app, 1, 0, 0, 0);
}
app->hit_count = fz_search_text_page(app->ctx, app->page_text, app->search, app->hit_bbox, nelem(app->hit_bbox));
if (app->hit_count > 0)
{
*panto = dir == 1 ? PAN_TO_TOP : PAN_TO_BOTTOM;
app->searchpage = app->pageno;
wincursor(app, HAND);
winrepaint(app);
return;
}
page += dir;
if (page < 1) page = app->pagecount;
if (page > app->pagecount) page = 1;
} while (page != firstpage);
pdfapp_warn(app, "String '%s' not found.", app->search);
app->pageno = firstpage;
pdfapp_showpage(app, 1, 0, 0, 0);
wincursor(app, HAND);
winrepaint(app);
}
void pdfapp_onresize(pdfapp_t *app, int w, int h)
{
if (app->winw != w || app->winh != h)
{
app->winw = w;
app->winh = h;
pdfapp_panview(app, app->panx, app->pany);
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);
if (app->searchdir < 0)
{
if (app->pageno == 1)
app->pageno = app->pagecount;
else
app->pageno--;
pdfapp_showpage(app, 1, 1, 0, 0);
}
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 '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, 0);
break;
case '-':
app->resolution /= ZOOMSTEP;
if (app->resolution < MINRES)
app->resolution = MINRES;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 'W':
app->resolution *= (double) app->winw / (double) fz_pixmap_width(app->ctx, app->image);
if (app->resolution > MAXRES)
app->resolution = MAXRES;
else if (app->resolution < MINRES)
app->resolution = MINRES;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 'H':
app->resolution *= (double) app->winh / (double) fz_pixmap_height(app->ctx, app->image);
if (app->resolution > MAXRES)
app->resolution = MAXRES;
else if (app->resolution < MINRES)
app->resolution = MINRES;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 'L':
app->rotate -= 90;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 'R':
app->rotate += 90;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 'c':
app->grayscale ^= 1;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 'i':
app->invert ^= 1;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
#ifndef NDEBUG
case 'a':
app->rotate -= 15;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
case 's':
app->rotate += 15;
pdfapp_showpage(app, 0, 1, 1, 0);
break;
#endif
/*
* Pan view, but don't need to repaint image
*/
case 'f':
app->shrinkwrap = 0;
winfullscreen(app, !app->fullscreen);
app->fullscreen = !app->fullscreen;
break;
case 'w':
if (app->fullscreen)
{
winfullscreen(app, 0);
app->fullscreen = 0;
}
app->shrinkwrap = 1;
app->panx = app->pany = 0;
pdfapp_showpage(app, 0, 0, 1, 0);
break;
case 'h':
app->panx += fz_pixmap_width(app->ctx, app->image) / 10;
pdfapp_showpage(app, 0, 0, 1, 0);
break;
case 'j':
app->pany -= fz_pixmap_height(app->ctx, app->image) / 10;
pdfapp_showpage(app, 0, 0, 1, 0);
break;
case 'k':
app->pany += fz_pixmap_height(app->ctx, app->image) / 10;
pdfapp_showpage(app, 0, 0, 1, 0);
break;
case 'l':
app->panx -= fz_pixmap_width(app->ctx, app->image) / 10;
pdfapp_showpage(app, 0, 0, 1, 0);
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;
case 'p':
app->presentation_mode = !app->presentation_mode;
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':
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;
/*
* Saving the file
*/
case 'S':
pdfapp_save(app);
break;
/*
* Reloading the file...
*/
case 'r':
panto = DONT_PAN;
oldpage = -1;
winreloadfile(app);
break;
/*
* Searching
*/
case '?':
app->isediting = 1;
app->searchdir = -1;
app->search[0] = 0;
app->hit_count = 0;
app->searchpage = -1;
winrepaintsearch(app);
break;
case '/':
app->isediting = 1;
app->searchdir = 1;
app->search[0] = 0;
app->hit_count = 0;
app->searchpage = -1;
winrepaintsearch(app);
break;
case 'n':
if (app->searchdir > 0)
pdfapp_search_in_direction(app, &panto, 1);
else
pdfapp_search_in_direction(app, &panto, -1);
loadpage = 0;
break;
case 'N':
if (app->searchdir > 0)
pdfapp_search_in_direction(app, &panto, -1);
else
pdfapp_search_in_direction(app, &panto, 1);
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, 1);
}
}
void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state)
{
fz_context *ctx = app->ctx;
fz_irect irect;
fz_link *link;
fz_matrix ctm;
fz_point p;
int processed = 0;
fz_pixmap_bbox(app->ctx, app->image, &irect);
p.x = x - app->panx + irect.x0;
p.y = y - app->pany + irect.y0;
pdfapp_viewctm(&ctm, app);
fz_invert_matrix(&ctm, &ctm);
fz_transform_point(&p, &ctm);
if (btn == 1 && (state == 1 || state == -1))
{
pdf_ui_event event;
pdf_document *idoc = pdf_specifics(app->doc);
event.etype = PDF_EVENT_TYPE_POINTER;
event.event.pointer.pt = p;
if (state == 1)
event.event.pointer.ptype = PDF_POINTER_DOWN;
else /* state == -1 */
event.event.pointer.ptype = PDF_POINTER_UP;
if (idoc && pdf_pass_event(idoc, (pdf_page *)app->page, &event))
{
pdf_widget *widget;
widget = pdf_focused_widget(idoc);
app->nowaitcursor = 1;
pdfapp_updatepage(app);
if (widget)
{
switch (pdf_widget_get_type(widget))
{
case PDF_WIDGET_TYPE_TEXT:
{
char *text = pdf_text_widget_text(idoc, widget);
char *current_text = text;
int retry = 0;
do
{
current_text = wintextinput(app, current_text, retry);
retry = 1;
}
while (current_text && !pdf_text_widget_set_text(idoc, widget, current_text));
fz_free(app->ctx, text);
pdfapp_updatepage(app);
}
break;
case PDF_WIDGET_TYPE_LISTBOX:
case PDF_WIDGET_TYPE_COMBOBOX:
{
int nopts;
int nvals;
char **opts = NULL;
char **vals = NULL;
fz_var(opts);
fz_var(vals);
fz_try(ctx)
{
nopts = pdf_choice_widget_options(idoc, widget, NULL);
opts = fz_malloc(ctx, nopts * sizeof(*opts));
(void)pdf_choice_widget_options(idoc, widget, opts);
nvals = pdf_choice_widget_value(idoc, widget, NULL);
vals = fz_malloc(ctx, MAX(nvals,nopts) * sizeof(*vals));
(void)pdf_choice_widget_value(idoc, widget, vals);
if (winchoiceinput(app, nopts, opts, &nvals, vals))
{
pdf_choice_widget_set_value(idoc, widget, nvals, vals);
pdfapp_updatepage(app);
}
}
fz_always(ctx)
{
fz_free(ctx, opts);
fz_free(ctx, vals);
}
fz_catch(ctx)
{
pdfapp_warn(app, "setting of choice failed");
}
}
break;
case PDF_WIDGET_TYPE_SIGNATURE:
{
char ebuf[256];
ebuf[0] = 0;
if (pdf_check_signature(ctx, idoc, widget, app->docpath, ebuf, sizeof(ebuf)))
{
winwarn(app, "Signature is valid");
}
else
{
if (ebuf[0] == 0)
winwarn(app, "Signature check failed for unknown reason");
else
winwarn(app, ebuf);
}
}
break;
}
}
app->nowaitcursor = 0;
processed = 1;
}
}
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 && !processed)
{
if (link->dest.kind == FZ_LINK_URI)
pdfapp_gotouri(app, link->dest.ld.uri.uri);
else if (link->dest.kind == FZ_LINK_GOTO)
pdfapp_gotopage(app, link->dest.ld.gotor.page);
return;
}
}
else
{
fz_annot *annot;
for (annot = fz_first_annot(app->doc, app->page); annot; annot = fz_next_annot(app->doc, annot))
{
fz_rect rect;
fz_bound_annot(app->doc, annot, &rect);
if (x >= rect.x0 && x < rect.x1)
if (y >= rect.y0 && y < rect.y1)
break;
}
if (annot)
wincursor(app, CARET);
else
wincursor(app, ARROW);
}
if (state == 1 && !processed)
{
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, 0);
}
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 = fz_mini(app->selx, x) - app->panx + irect.x0;
app->selr.x1 = fz_maxi(app->selx, x) - app->panx + irect.x0;
app->selr.y0 = fz_mini(app->sely, y) - app->pany + irect.y0;
app->selr.y1 = fz_maxi(app->sely, y) - app->pany + irect.y0;
winrepaint(app);
if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1)
windocopy(app);
}
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 + fz_pixmap_height(app->ctx, app->image) < app->winh || newy > 0)
{
/* Yes. We can assume that deltay != 0 */
int deltay = y - app->sely;
/* Check whether the panning has occurred 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 threshold
*/
app->beyondy += deltay;
if (app->beyondy > BEYOND_THRESHHOLD)
{
if( app->pageno > 1 )
{
app->pageno--;
pdfapp_showpage(app, 1, 1, 1, 0);
newy = -fz_pixmap_height(app->ctx, app->image);
}
app->beyondy = 0;
}
else if (app->beyondy < -BEYOND_THRESHHOLD)
{
if( app->pageno < app->pagecount )
{
app->pageno++;
pdfapp_showpage(app, 1, 1, 1, 0);
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 = fz_mini(app->selx, x) - app->panx + irect.x0;
app->selr.x1 = fz_maxi(app->selx, x) - app->panx + irect.x0;
app->selr.y0 = fz_mini(app->sely, y) - app->pany + irect.y0;
app->selr.y1 = fz_maxi(app->sely, y) - app->pany + irect.y0;
winrepaint(app);
}
}
void pdfapp_oncopy(pdfapp_t *app, unsigned short *ucsbuf, int ucslen)
{
fz_rect hitbox;
fz_matrix ctm;
fz_text_page *page = app->page_text;
int c, i, p;
int seen = 0;
int block_num;
int x0 = app->selr.x0;
int x1 = app->selr.x1;
int y0 = app->selr.y0;
int y1 = app->selr.y1;
pdfapp_viewctm(&ctm, app);
p = 0;
for (block_num = 0; block_num < page->len; block_num++)
{
fz_text_line *line;
fz_text_block *block;
fz_text_span *span;
if (page->blocks[block_num].type != FZ_PAGE_BLOCK_TEXT)
continue;
block = page->blocks[block_num].u.text;
for (line = block->lines; line < block->lines + block->len; line++)
{
for (span = line->first_span; span; span = span->next)
{
if (seen)
{
#ifdef _WIN32
if (p < ucslen - 1)
ucsbuf[p++] = '\r';
#endif
if (p < ucslen - 1)
ucsbuf[p++] = '\n';
}
seen = 0;
for (i = 0; i < span->len; i++)
{
fz_text_char_bbox(&hitbox, span, i);
fz_transform_rect(&hitbox, &ctm);
c = span->text[i].c;
if (c < 32)
c = '?';
if (hitbox.x1 >= x0 && hitbox.x0 <= x1 && hitbox.y1 >= y0 && hitbox.y0 <= y1)
{
if (p < ucslen - 1)
ucsbuf[p++] = c;
seen = 1;
}
}
seen = (seen && span == line->last_span);
}
}
}
ucsbuf[p] = 0;
}
void pdfapp_postblit(pdfapp_t *app)
{
clock_t time;
float seconds;
int llama;
app->transitions_enabled = 1;
if (!app->in_transit)
return;
time = clock();
seconds = (float)(time - app->start_time) / CLOCKS_PER_SEC;
llama = seconds * 256 / app->transition.duration;
if (llama >= 256)
{
/* Completed. */
fz_drop_pixmap(app->ctx, app->image);
app->image = app->new_image;
app->new_image = NULL;
fz_drop_pixmap(app->ctx, app->old_image);
app->old_image = NULL;
if (app->duration != 0)
winadvancetimer(app, app->duration);
}
else
fz_generate_transition(app->image, app->old_image, app->new_image, llama, &app->transition);
winrepaint(app);
if (llama >= 256)
{
/* Completed. */
app->in_transit = 0;
}
}
|