-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.c
4742 lines (4109 loc) · 130 KB
/
link.c
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
/* lots of code c&p-ed directly from mupdf */
#define CAML_NAME_SPACE
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <wchar.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#ifdef __CYGWIN__
#include <cygwin/socket.h> /* FIONREAD */
#else
#include <spawn.h>
#endif
#include <regex.h>
#include <stdarg.h>
#include <limits.h>
#include <inttypes.h>
#include <GL/gl.h>
#include <caml/fail.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/unixsupport.h>
#if __GNUC__ < 5
/* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
clobbered diagnostics */
#pragma GCC diagnostic ignored "-Wclobbered"
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wshadow"
#include <mupdf/fitz.h>
#pragma GCC diagnostic pop
#include <mupdf/pdf.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#ifdef USE_FONTCONFIG
#include <fontconfig/fontconfig.h>
#endif
#define PIGGYBACK
#define CACHE_PAGEREFS
#ifndef __USE_GNU
extern char **environ;
#endif
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#if defined __GNUC__
#define NORETURN_ATTR __attribute__ ((noreturn))
#define UNUSED_ATTR __attribute__ ((unused))
#if !defined __clang__
#define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
#else
#define OPTIMIZE_ATTR(n)
#endif
#define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
#else
#define NORETURN_ATTR
#define UNUSED_ATTR
#define OPTIMIZE_ATTR(n)
#define GCC_FMT_ATTR(a, b)
#endif
#define FMT_s "zu"
#define FMT_ptr PRIxPTR
#define SCN_ptr SCNxPTR
#define FMT_ptr_cast(p) ((uintptr_t) (p))
#define SCN_ptr_cast(p) ((uintptr_t *) (p))
static void NORETURN_ATTR GCC_FMT_ATTR (2, 3)
err (int exitcode, const char *fmt, ...)
{
va_list ap;
int savederrno;
savederrno = errno;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ": %s\n", strerror (savederrno));
fflush (stderr);
_exit (exitcode);
}
static void NORETURN_ATTR GCC_FMT_ATTR (2, 3)
errx (int exitcode, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fputc ('\n', stderr);
fflush (stderr);
_exit (exitcode);
}
#ifndef GL_TEXTURE_RECTANGLE_ARB
#define GL_TEXTURE_RECTANGLE_ARB 0x84F5
#endif
#ifdef USE_NPOT
#define TEXT_TYPE GL_TEXTURE_2D
#else
#define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
#endif
#ifndef GL_BGRA
#define GL_BGRA 0x80E1
#endif
#ifndef GL_UNSIGNED_INT_8_8_8_8
#define GL_UNSIGNED_INT_8_8_8_8 0x8035
#endif
#ifndef GL_UNSIGNED_INT_8_8_8_8_REV
#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
#endif
#if 0
#define lprintf printf
#else
#define lprintf(...)
#endif
#define ARSERT(cond) for (;;) { \
if (!(cond)) { \
errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
} \
break; \
}
struct slice {
int h;
int texindex;
};
struct tile {
int w, h;
int slicecount;
int sliceheight;
struct bo *pbo;
fz_pixmap *pixmap;
struct slice slices[1];
};
struct pagedim {
int pageno;
int rotate;
int left;
int tctmready;
fz_irect bounds;
fz_rect pagebox;
fz_rect mediabox;
fz_matrix ctm, zoomctm, lctm, tctm;
};
struct slink {
enum { SLINK, SANNOT } tag;
fz_irect bbox;
union {
fz_link *link;
fz_annot *annot;
} u;
};
struct annot {
fz_irect bbox;
fz_annot *annot;
};
struct page {
int tgen;
int sgen;
int agen;
int pageno;
int pdimno;
fz_stext_page *text;
fz_stext_sheet *sheet;
fz_page *fzpage;
fz_display_list *dlist;
int slinkcount;
struct slink *slinks;
int annotcount;
struct annot *annots;
struct mark {
int i;
fz_stext_span *span;
} fmark, lmark;
};
struct {
int sliceheight;
struct pagedim *pagedims;
int pagecount;
int pagedimcount;
fz_document *doc;
fz_context *ctx;
int w, h;
int texindex;
int texcount;
GLuint *texids;
GLenum texiform;
GLenum texform;
GLenum texty;
fz_colorspace *colorspace;
struct {
int w, h;
struct slice *slice;
} *texowners;
int rotate;
enum { FitWidth, FitProportional, FitPage } fitmodel;
int trimmargins;
int needoutline;
int gen;
int aalevel;
int trimanew;
fz_irect trimfuzz;
fz_pixmap *pig;
pthread_t thread;
int csock;
FT_Face face;
char *trimcachepath;
int cxack;
int dirty;
GLuint stid;
int bo_usable;
GLuint boid;
void (*glBindBufferARB) (GLenum, GLuint);
GLboolean (*glUnmapBufferARB) (GLenum);
void *(*glMapBufferARB) (GLenum, GLenum);
void (*glBufferDataARB) (GLenum, GLsizei, void *, GLenum);
void (*glGenBuffersARB) (GLsizei, GLuint *);
void (*glDeleteBuffersARB) (GLsizei, GLuint *);
GLfloat texcoords[8];
GLfloat vertices[16];
#ifdef CACHE_PAGEREFS
struct {
int idx;
int count;
pdf_obj **objs;
pdf_document *pdf;
} pdflut;
#endif
} state;
struct bo {
GLuint id;
void *ptr;
size_t size;
};
static void UNUSED_ATTR debug_rect (const char *cap, fz_rect r)
{
printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap, r.x0, r.y0, r.x1, r.y1);
}
static void UNUSED_ATTR debug_bbox (const char *cap, fz_irect r)
{
printf ("%s(bbox) %d,%d,%d,%d\n", cap, r.x0, r.y0, r.x1, r.y1);
}
static void UNUSED_ATTR debug_matrix (const char *cap, fz_matrix m)
{
printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap,
m.a, m.b, m.c, m.d, m.e, m.f);
}
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void lock (const char *cap)
{
int ret = pthread_mutex_lock (&mutex);
if (ret) {
errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
}
}
static void unlock (const char *cap)
{
int ret = pthread_mutex_unlock (&mutex);
if (ret) {
errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
}
}
static int trylock (const char *cap)
{
int ret = pthread_mutex_trylock (&mutex);
if (ret && ret != EBUSY) {
errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
}
return ret == EBUSY;
}
static void *parse_pointer (const char *cap, const char *s)
{
int ret;
void *ptr;
ret = sscanf (s, "%" SCN_ptr, SCN_ptr_cast (&ptr));
if (ret != 1) {
errx (1, "%s: cannot parse pointer in `%s'", cap, s);
}
return ptr;
}
static double now (void)
{
struct timeval tv;
if (gettimeofday (&tv, NULL)) {
err (1, "gettimeofday");
}
return tv.tv_sec + tv.tv_usec*1e-6;
}
static int hasdata (void)
{
int ret, avail;
ret = ioctl (state.csock, FIONREAD, &avail);
if (ret) err (1, "hasdata: FIONREAD error ret=%d", ret);
return avail > 0;
}
CAMLprim value ml_hasdata (value fd_v)
{
CAMLparam1 (fd_v);
int ret, avail;
ret = ioctl (Int_val (fd_v), FIONREAD, &avail);
if (ret) uerror ("ioctl (FIONREAD)", Nothing);
CAMLreturn (Val_bool (avail > 0));
}
static void readdata (int fd, void *p, int size)
{
ssize_t n;
again:
n = read (fd, p, size);
if (n < 0) {
if (errno == EINTR) goto again;
err (1, "read (fd %d, req %d, ret %zd)", fd, size, n);
}
if (n - size) {
if (!n) errx (1, "EOF while reading");
errx (1, "read (fd %d, req %d, ret %zd)", fd, size, n);
}
}
static void writedata (int fd, char *p, int size)
{
ssize_t n;
/* One should lookup type punning/strict aliasing etc in standard,DRs,Web to
convince herself that this is:
a. safe
b. practically the only way to achieve the result
(union puns notwithstanding) */
memcpy (p, &size, 4);
n = write (fd, p, size + 4);
if (n - size - 4) {
if (!n) errx (1, "EOF while writing data");
err (1, "write (fd %d, req %d, ret %zd)", fd, size + 4, n);
}
}
static int readlen (int fd)
{
/* Type punned unions here. Why? Less code (Adjusted by more comments).
https://en.wikipedia.org/wiki/Type_punning */
union { int len; char raw[4]; } buf;
readdata (fd, buf.raw, 4);
return buf.len;
}
CAMLprim void ml_wcmd (value fd_v, value bytes_v, value len_v)
{
CAMLparam3 (fd_v, bytes_v, len_v);
writedata (Int_val (fd_v), &Byte (bytes_v, 0), Int_val (len_v));
CAMLreturn0;
}
CAMLprim value ml_rcmd (value fd_v)
{
CAMLparam1 (fd_v);
CAMLlocal1 (strdata_v);
int fd = Int_val (fd_v);
int len = readlen (fd);
strdata_v = caml_alloc_string (len);
readdata (fd, String_val (strdata_v), len);
CAMLreturn (strdata_v);
}
static void GCC_FMT_ATTR (1, 2) printd (const char *fmt, ...)
{
int size = 200, len;
va_list ap;
char *buf;
buf = malloc (size);
for (;;) {
if (!buf) err (1, "malloc for temp buf (%d bytes) failed", size);
va_start (ap, fmt);
len = vsnprintf (buf + 4, size - 4, fmt, ap);
va_end (ap);
if (len > -1) {
if (len < size - 4) {
writedata (state.csock, buf, len);
break;
}
else size = len + 5;
}
else {
err (1, "vsnprintf for `%s' failed", fmt);
}
buf = realloc (buf, size);
}
free (buf);
}
static void closedoc (void)
{
#ifdef CACHE_PAGEREFS
if (state.pdflut.objs) {
int i;
for (i = 0; i < state.pdflut.count; ++i) {
pdf_drop_obj (state.ctx, state.pdflut.objs[i]);
}
free (state.pdflut.objs);
state.pdflut.objs = NULL;
state.pdflut.idx = 0;
}
#endif
if (state.doc) {
fz_drop_document (state.ctx, state.doc);
state.doc = NULL;
}
}
static int openxref (char *filename, char *password)
{
int i;
for (i = 0; i < state.texcount; ++i) {
state.texowners[i].w = -1;
state.texowners[i].slice = NULL;
}
closedoc ();
state.dirty = 0;
if (state.pagedims) {
free (state.pagedims);
state.pagedims = NULL;
}
state.pagedimcount = 0;
fz_set_aa_level (state.ctx, state.aalevel);
#ifdef CSS_HACK_TO_READ_EPUBS_COMFORTABLY
fz_set_user_css (state.ctx,
"body { margin-left: 20%; margin-right: 20%; }");
#endif
state.doc = fz_open_document (state.ctx, filename);
if (fz_needs_password (state.ctx, state.doc)) {
if (password && !*password) {
printd ("pass");
return 0;
}
else {
int ok = fz_authenticate_password (state.ctx, state.doc, password);
if (!ok) {
printd ("pass fail");
return 0;
}
}
}
state.pagecount = fz_count_pages (state.ctx, state.doc);
return 1;
}
static void pdfinfo (void)
{
pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
if (pdf) {
pdf_obj *infoobj;
printd ("info PDF version\t%d.%d",
pdf->version / 10, pdf->version % 10);
infoobj = pdf_dict_gets (state.ctx, pdf_trailer (state.ctx,
pdf), "Info");
if (infoobj) {
unsigned int i;
char *s;
char *items[] = { "Title", "Author", "Creator",
"Producer", "CreationDate" };
for (i = 0; i < sizeof (items) / sizeof (*items); ++i) {
pdf_obj *obj = pdf_dict_gets (state.ctx, infoobj, items[i]);
s = pdf_to_utf8 (state.ctx, pdf, obj);
if (*s) printd ("info %s\t%s", items[i], s);
fz_free (state.ctx, s);
}
}
printd ("infoend");
}
}
static void unlinktile (struct tile *tile)
{
int i;
for (i = 0; i < tile->slicecount; ++i) {
struct slice *s = &tile->slices[i];
if (s->texindex != -1) {
if (state.texowners[s->texindex].slice == s) {
state.texowners[s->texindex].slice = NULL;
}
}
}
}
static void freepage (struct page *page)
{
if (!page) return;
if (page->text) {
fz_drop_stext_page (state.ctx, page->text);
}
if (page->sheet) {
fz_drop_stext_sheet (state.ctx, page->sheet);
}
if (page->slinks) {
free (page->slinks);
}
fz_drop_display_list (state.ctx, page->dlist);
fz_drop_page (state.ctx, page->fzpage);
free (page);
}
static void freetile (struct tile *tile)
{
unlinktile (tile);
if (!tile->pbo) {
#ifndef PIGGYBACK
fz_drop_pixmap (state.ctx, tile->pixmap);
#else
if (state.pig) {
fz_drop_pixmap (state.ctx, state.pig);
}
state.pig = tile->pixmap;
#endif
}
else {
free (tile->pbo);
fz_drop_pixmap (state.ctx, tile->pixmap);
}
free (tile);
}
#ifdef __ALTIVEC__
#include <stdint.h>
#include <altivec.h>
static int cacheline32bytes;
static void __attribute__ ((constructor)) clcheck (void)
{
char **envp = environ;
unsigned long *auxv;
while (*envp++);
for (auxv = (unsigned long *) envp; *auxv != 0; auxv += 2) {
if (*auxv == 19) {
cacheline32bytes = auxv[1] == 32;
return;
}
}
}
static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap *pixmap)
{
size_t size = pixmap->w * pixmap->h * pixmap->n;
if (cacheline32bytes && size > 32) {
intptr_t a1, a2, diff;
size_t sizea, i;
vector unsigned char v = vec_splat_u8 (-1);
vector unsigned char *p;
a1 = a2 = (intptr_t) pixmap->samples;
a2 = (a1 + 31) & ~31;
diff = a2 - a1;
sizea = size - diff;
p = (void *) a2;
while (a1 != a2) *(char *) a1++ = 0xff;
for (i = 0; i < (sizea & ~31); i += 32) {
__asm volatile ("dcbz %0, %1"::"b"(a2),"r"(i));
vec_st (v, i, p);
vec_st (v, i + 16, p);
}
while (i < sizea) *((char *) a1 + i++) = 0xff;
}
else fz_clear_pixmap_with_value (state.ctx, pixmap, 0xff);
}
#else
#define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
#endif
static void trimctm (pdf_page *page, int pindex)
{
fz_matrix ctm;
struct pagedim *pdim = &state.pagedims[pindex];
if (!pdim->tctmready) {
if (state.trimmargins) {
fz_rect realbox;
fz_matrix rm, sm, tm, im, ctm1;
fz_rotate (&rm, -pdim->rotate);
fz_scale (&sm, 1, -1);
fz_concat (&ctm, &rm, &sm);
realbox = pdim->mediabox;
fz_transform_rect (&realbox, &ctm);
fz_translate (&tm, -realbox.x0, -realbox.y0);
fz_concat (&ctm1, &ctm, &tm);
fz_invert_matrix (&im, &page->ctm);
fz_concat (&ctm, &im, &ctm1);
}
else {
ctm = fz_identity;
}
pdim->tctm = ctm;
pdim->tctmready = 1;
}
}
static fz_matrix pagectm1 (fz_page *fzpage, struct pagedim *pdim)
{
fz_matrix ctm, tm;
int pdimno = pdim - state.pagedims;
if (pdf_specifics (state.ctx, state.doc)) {
trimctm ((pdf_page *) fzpage, pdimno);
fz_concat (&ctm, &pdim->tctm, &pdim->ctm);
}
else {
fz_translate (&tm, -pdim->mediabox.x0, -pdim->mediabox.y0);
fz_concat (&ctm, &tm, &pdim->ctm);
}
return ctm;
}
static fz_matrix pagectm (struct page *page)
{
return pagectm1 (page->fzpage, &state.pagedims[page->pdimno]);
}
static void *loadpage (int pageno, int pindex)
{
fz_device *dev;
struct page *page;
page = calloc (sizeof (struct page), 1);
if (!page) {
err (1, "calloc page %d", pageno);
}
page->dlist = fz_new_display_list (state.ctx);
dev = fz_new_list_device (state.ctx, page->dlist);
fz_try (state.ctx) {
page->fzpage = fz_load_page (state.ctx, state.doc, pageno);
fz_run_page (state.ctx, page->fzpage, dev,
&fz_identity, NULL);
}
fz_catch (state.ctx) {
page->fzpage = NULL;
}
fz_drop_device (state.ctx, dev);
page->pdimno = pindex;
page->pageno = pageno;
page->sgen = state.gen;
page->agen = state.gen;
page->tgen = state.gen;
return page;
}
static struct tile *alloctile (int h)
{
int i;
int slicecount;
size_t tilesize;
struct tile *tile;
slicecount = (h + state.sliceheight - 1) / state.sliceheight;
tilesize = sizeof (*tile) + ((slicecount - 1) * sizeof (struct slice));
tile = calloc (tilesize, 1);
if (!tile) {
err (1, "cannot allocate tile (%" FMT_s " bytes)", tilesize);
}
for (i = 0; i < slicecount; ++i) {
int sh = MIN (h, state.sliceheight);
tile->slices[i].h = sh;
tile->slices[i].texindex = -1;
h -= sh;
}
tile->slicecount = slicecount;
tile->sliceheight = state.sliceheight;
return tile;
}
static struct tile *rendertile (struct page *page, int x, int y, int w, int h,
struct bo *pbo)
{
fz_rect rect;
fz_irect bbox;
fz_matrix ctm;
fz_device *dev;
struct tile *tile;
struct pagedim *pdim;
tile = alloctile (h);
pdim = &state.pagedims[page->pdimno];
bbox = pdim->bounds;
bbox.x0 += x;
bbox.y0 += y;
bbox.x1 = bbox.x0 + w;
bbox.y1 = bbox.y0 + h;
if (state.pig) {
if (state.pig->w == w
&& state.pig->h == h
&& state.pig->colorspace == state.colorspace) {
tile->pixmap = state.pig;
tile->pixmap->x = bbox.x0;
tile->pixmap->y = bbox.y0;
}
else {
fz_drop_pixmap (state.ctx, state.pig);
}
state.pig = NULL;
}
if (!tile->pixmap) {
if (pbo) {
tile->pixmap =
fz_new_pixmap_with_bbox_and_data (state.ctx, state.colorspace,
&bbox, pbo->ptr);
tile->pbo = pbo;
}
else {
tile->pixmap =
fz_new_pixmap_with_bbox (state.ctx, state.colorspace, &bbox);
}
}
tile->w = w;
tile->h = h;
clearpixmap (tile->pixmap);
dev = fz_new_draw_device (state.ctx, tile->pixmap);
ctm = pagectm (page);
fz_rect_from_irect (&rect, &bbox);
fz_run_display_list (state.ctx, page->dlist, dev, &ctm, &rect, NULL);
fz_drop_device (state.ctx, dev);
return tile;
}
#ifdef CACHE_PAGEREFS
/* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
thanks to Robin Watts */
static void
pdf_collect_pages(pdf_document *doc, pdf_obj *node)
{
fz_context *ctx = state.ctx; /* doc->ctx; */
pdf_obj *kids;
int i, len;
if (state.pdflut.idx == state.pagecount) return;
kids = pdf_dict_gets (ctx, node, "Kids");
len = pdf_array_len (ctx, kids);
if (len == 0)
fz_throw (ctx, FZ_ERROR_GENERIC, "malformed pages tree");
if (pdf_mark_obj (ctx, node))
fz_throw (ctx, FZ_ERROR_GENERIC, "cycle in page tree");
for (i = 0; i < len; i++) {
pdf_obj *kid = pdf_array_get (ctx, kids, i);
char *type = pdf_to_name (ctx, pdf_dict_gets (ctx, kid, "Type"));
if (*type
? !strcmp (type, "Pages")
: pdf_dict_gets (ctx, kid, "Kids")
&& !pdf_dict_gets (ctx, kid, "MediaBox")) {
pdf_collect_pages (doc, kid);
}
else {
if (*type
? strcmp (type, "Page") != 0
: !pdf_dict_gets (ctx, kid, "MediaBox"))
fz_warn (ctx, "non-page object in page tree (%s)", type);
state.pdflut.objs[state.pdflut.idx++] = pdf_keep_obj (ctx, kid);
}
}
pdf_unmark_obj (ctx, node);
}
static void
pdf_load_page_objs (pdf_document *doc)
{
pdf_obj *root = pdf_dict_gets (state.ctx,
pdf_trailer (state.ctx, doc), "Root");
pdf_obj *node = pdf_dict_gets (state.ctx, root, "Pages");
if (!node)
fz_throw (state.ctx, FZ_ERROR_GENERIC, "cannot find page tree");
state.pdflut.idx = 0;
pdf_collect_pages (doc, node);
}
#endif
static void initpdims (int wthack)
{
double start, end;
FILE *trimf = NULL;
fz_rect rootmediabox;
int pageno, trim, show;
int trimw = 0, cxcount;
fz_context *ctx = state.ctx;
pdf_document *pdf = pdf_specifics (ctx, state.doc);
fz_var (trimw);
fz_var (trimf);
fz_var (cxcount);
start = now ();
if (state.trimmargins && state.trimcachepath) {
trimf = fopen (state.trimcachepath, "rb");
if (!trimf) {
trimf = fopen (state.trimcachepath, "wb");
trimw = 1;
}
}
if (state.trimmargins || pdf || !state.cxack)
cxcount = state.pagecount;
else
cxcount = MIN (state.pagecount, 1);
if (pdf) {
pdf_obj *obj;
obj = pdf_dict_getp (ctx, pdf_trailer (ctx, pdf),
"Root/Pages/MediaBox");
pdf_to_rect (ctx, obj, &rootmediabox);
}
#ifdef CACHE_PAGEREFS
if (pdf && (!state.pdflut.objs || state.pdflut.pdf != pdf)) {
state.pdflut.objs = calloc (sizeof (*state.pdflut.objs), cxcount);
if (!state.pdflut.objs) {
err (1, "malloc pageobjs %zu %d %zu failed",
sizeof (*state.pdflut.objs), cxcount,
sizeof (*state.pdflut.objs) * cxcount);
}
state.pdflut.count = cxcount;
pdf_load_page_objs (pdf);
state.pdflut.pdf = pdf;
}
#endif
for (pageno = 0; pageno < cxcount; ++pageno) {
int rotate = 0;
struct pagedim *p;
fz_rect mediabox;
fz_var (rotate);
if (pdf) {
pdf_obj *pageref, *pageobj;
#ifdef CACHE_PAGEREFS
pageref = state.pdflut.objs[pageno];
#else
pageref = pdf_lookup_page_obj (ctx, pdf, pageno);
#endif
pageobj = pdf_resolve_indirect (ctx, pageref);
if (state.trimmargins) {
pdf_obj *obj;
pdf_page *page;
fz_try (ctx) {
page = pdf_load_page (ctx, pdf, pageno);
obj = pdf_dict_gets (ctx, pageobj, "llpp.TrimBox");
trim = state.trimanew || !obj;
if (trim) {
fz_rect rect;
fz_matrix ctm;
fz_device *dev;
dev = fz_new_bbox_device (ctx, &rect);
dev->hints |= FZ_IGNORE_SHADE;
fz_invert_matrix (&ctm, &page->ctm);
pdf_run_page (ctx, page, dev, &fz_identity, NULL);
fz_drop_device (ctx, dev);
rect.x0 += state.trimfuzz.x0;
rect.x1 += state.trimfuzz.x1;
rect.y0 += state.trimfuzz.y0;
rect.y1 += state.trimfuzz.y1;
fz_transform_rect (&rect, &ctm);
fz_intersect_rect (&rect, &page->mediabox);
if (fz_is_empty_rect (&rect)) {
mediabox = page->mediabox;
}
else {
mediabox = rect;
}
obj = pdf_new_array (ctx, pdf, 4);
pdf_array_push (ctx, obj, pdf_new_real (ctx, pdf,
mediabox.x0));
pdf_array_push (ctx, obj, pdf_new_real (ctx, pdf,
mediabox.y0));
pdf_array_push (ctx, obj, pdf_new_real (ctx, pdf,
mediabox.x1));
pdf_array_push (ctx, obj, pdf_new_real (ctx, pdf,
mediabox.y1));
pdf_dict_puts (ctx, pageobj, "llpp.TrimBox", obj);
}
else {
mediabox.x0 = pdf_to_real (ctx,
pdf_array_get (ctx, obj, 0));
mediabox.y0 = pdf_to_real (ctx,
pdf_array_get (ctx, obj, 1));
mediabox.x1 = pdf_to_real (ctx,
pdf_array_get (ctx, obj, 2));
mediabox.y1 = pdf_to_real (ctx,
pdf_array_get (ctx, obj, 3));
}
rotate = page->rotate;
fz_drop_page (ctx, &page->super);
show = trim ? pageno % 5 == 0 : pageno % 20 == 0;
if (show) {
printd ("progress %f Trimming %d",
(double) (pageno + 1) / state.pagecount,
pageno + 1);
}
}
fz_catch (ctx) {
fprintf (stderr, "failed to load page %d\n", pageno+1);
}
}
else {
int empty = 0;