1 /* lots of code c&p-ed directly from mupdf */
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
21 #include <OpenGL/gl.h>
26 #include <caml/fail.h>
27 #include <caml/alloc.h>
28 #include <caml/memory.h>
29 #include <caml/unixsupport.h>
36 #include FT_FREETYPE_H
41 #define NORETURN __attribute__ ((noreturn))
42 #define UNUSED __attribute__ ((unused))
43 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
44 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
49 #define GCC_FMT_ATTR(a, b)
55 #define FMT_ptr_cast(p) (p)
56 #define FMT_ptr_cast2(p) (p)
58 static void NORETURN
GCC_FMT_ATTR (2, 3)
59 err (int exitcode
, const char *fmt
, ...)
66 vfprintf (stderr
, fmt
, ap
);
68 fprintf (stderr
, ": %s\n", strerror (savederrno
));
73 static void NORETURN
GCC_FMT_ATTR (2, 3)
74 errx (int exitcode
, const char *fmt
, ...)
79 vfprintf (stderr
, fmt
, ap
);
86 #ifndef GL_TEXTURE_RECTANGLE_ARB
87 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
91 #define GL_BGRA 0x80E1
94 #ifndef GL_UNSIGNED_INT_8_8_8_8
95 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
98 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
99 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
103 #define lprintf printf
108 #define ARSERT(cond) for (;;) { \
110 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
125 struct slice slices
[1];
136 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
145 enum { DPDF
, DXPS
, DCBZ
};
159 fz_display_list
*dlist
;
161 struct slink
*slinks
;
166 void (*freepage
) (void *);
172 struct pagedim
*pagedims
;
181 fz_glyph_cache
*cache
;
192 fz_colorspace
*colorspace
;
214 void (*closedoc
) (void);
215 void (*freepage
) (void *);
218 static void UNUSED
debug_rect (const char *cap
, fz_rect r
)
220 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
223 static void UNUSED
debug_bbox (const char *cap
, fz_bbox r
)
225 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
228 static void UNUSED
debug_matrix (const char *cap
, fz_matrix m
)
230 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
231 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
234 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
236 static void lock (const char *cap
)
238 int ret
= pthread_mutex_lock (&mutex
);
240 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
244 static void unlock (const char *cap
)
246 int ret
= pthread_mutex_unlock (&mutex
);
248 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
252 static int trylock (const char *cap
)
254 int ret
= pthread_mutex_trylock (&mutex
);
256 if (ret
&& ret
!= EBUSY
) {
257 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
262 static void *parse_pointer (const char *cap
, const char *s
)
267 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
269 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
274 static double now (void)
278 if (gettimeofday (&tv
, NULL
)) {
279 err (1, "gettimeofday");
281 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
284 static int hasdata (void)
287 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
288 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
292 CAMLprim value
ml_hasdata (value fd_v
)
297 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
298 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
299 CAMLreturn (Val_bool (avail
> 0));
302 static void readdata (void *p
, int size
)
306 n
= read (state
.cr
, p
, size
);
308 if (!n
) errx (1, "EOF while reading");
309 err (1, "read (req %d, ret %zd)", size
, n
);
313 static void writedata (char *p
, int size
)
318 buf
[0] = (size
>> 24) & 0xff;
319 buf
[1] = (size
>> 16) & 0xff;
320 buf
[2] = (size
>> 8) & 0xff;
321 buf
[3] = (size
>> 0) & 0xff;
323 n
= write (state
.cw
, buf
, 4);
325 if (!n
) errx (1, "EOF while writing length");
326 err (1, "write %zd", n
);
329 n
= write (state
.cw
, p
, size
);
331 if (!n
) errx (1, "EOF while writing data");
332 err (1, "write (req %d, ret %zd)", size
, n
);
336 static int readlen (void)
341 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
344 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
352 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
355 len
= vsnprintf (buf
, size
, fmt
, ap
);
358 if (len
> -1 && len
< size
) {
359 writedata (buf
, len
);
369 buf
= realloc (buf
, size
);
374 static void closepdf (void)
377 pdf_close_document (state
.u
.pdf
);
382 static void closexps (void)
385 xps_close_document (state
.u
.xps
);
390 static void closecbz (void)
393 cbz_close_document (state
.u
.cbz
);
398 static void freepdfpage (void *ptr
)
400 pdf_free_page (state
.u
.pdf
, ptr
);
403 static void freexpspage (void *ptr
)
405 xps_free_page (state
.u
.xps
, ptr
);
408 static void freecbzpage (void *ptr
)
410 cbz_free_page (state
.u
.cbz
, ptr
);
413 static void openxref (char *filename
, char *password
)
417 for (i
= 0; i
< state
.texcount
; ++i
) {
418 state
.texowners
[i
].w
= -1;
419 state
.texowners
[i
].slice
= NULL
;
422 if (state
.closedoc
) state
.closedoc ();
424 len
= strlen (filename
);
430 ext
[0] = tolower (filename
[len
-3]);
431 ext
[1] = tolower (filename
[len
-2]);
432 ext
[2] = tolower (filename
[len
-1]);
434 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
437 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
442 if (state
.pagedims
) {
443 free (state
.pagedims
);
444 state
.pagedims
= NULL
;
446 state
.pagedimcount
= 0;
448 fz_set_aa_level (state
.ctx
, state
.aalevel
);
449 switch (state
.type
) {
451 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
452 if (pdf_needs_password (state
.u
.pdf
)) {
453 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
455 errx (1, "invalid password");
458 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
459 state
.closedoc
= closepdf
;
460 state
.freepage
= freepdfpage
;
464 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
465 state
.pagecount
= xps_count_pages (state
.u
.xps
);
466 state
.closedoc
= closexps
;
467 state
.freepage
= freexpspage
;
471 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
472 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
473 state
.closedoc
= closecbz
;
474 state
.freepage
= freecbzpage
;
479 static void pdfinfo (void)
481 if (state
.type
== DPDF
) {
484 printd ("info PDF version\t%d.%d",
485 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
487 infoobj
= fz_dict_gets (state
.u
.pdf
->trailer
, "Info");
491 char *items
[] = { "Title", "Author", "Creator",
492 "Producer", "CreationDate" };
494 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
495 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
496 s
= pdf_to_utf8 (state
.ctx
, obj
);
499 printd ("title %s", s
);
501 printd ("info %s\t%s", items
[i
], s
);
503 fz_free (state
.ctx
, s
);
510 static void unlinktile (struct tile
*tile
)
514 for (i
= 0; i
< tile
->slicecount
; ++i
) {
515 struct slice
*s
= &tile
->slices
[i
];
517 if (s
->texindex
!= -1) {
518 if (state
.texowners
[s
->texindex
].slice
== s
) {
519 state
.texowners
[s
->texindex
].slice
= NULL
;
525 static void freepage (struct page
*page
)
528 fz_free_text_span (state
.ctx
, page
->text
);
533 page
->freepage (page
->u
.ptr
);
534 fz_free_display_list (state
.ctx
, page
->dlist
);
538 static void freetile (struct tile
*tile
)
542 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
545 fz_drop_pixmap (state
.ctx
, state
.pig
);
547 state
.pig
= tile
->pixmap
;
555 static int cacheline32bytes
;
556 extern char **environ
;
558 static void __attribute__ ((constructor
)) clcheck (void)
560 char **envp
= environ
;
565 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
567 cacheline32bytes
= auxv
[1] == 32;
573 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
575 if (cacheline32bytes
) {
576 intptr_t a1
, a2
, diff
;
577 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
578 vector
unsigned char v
= vec_splat_u8 (-1);
579 vector
unsigned char *p
;
581 a1
= a2
= (intptr_t) pixmap
->samples
;
582 a2
= (a1
+ 31) & ~31;
587 while (a1
!= a2
) *(char *) a1
++ = 0xff;
588 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
589 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
591 vec_st (v
, i
+ 16, p
);
593 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
595 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
598 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
601 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
604 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
606 if (!pdim
->tctmready
) {
607 if (state
.trimmargins
) {
610 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
611 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
612 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
613 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
624 static fz_matrix
pagectm (struct page
*page
)
626 if (page
->type
== DPDF
) {
627 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
628 state
.pagedims
[page
->pdimno
].ctm
);
632 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
634 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
635 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
636 -pdim
->mediabox
.y0
), ctm
);
641 static void *loadpage (int pageno
, int pindex
)
644 struct page
*page
= NULL
;
646 page
= calloc (sizeof (struct page
), 1);
648 err (1, "calloc page %d", pageno
);
651 page
->dlist
= fz_new_display_list (state
.ctx
);
652 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
653 switch (state
.type
) {
655 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
656 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
657 page
->freepage
= freepdfpage
;
661 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
662 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
663 page
->freepage
= freexpspage
;
667 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
668 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
669 page
->freepage
= freecbzpage
;
672 fz_free_device (dev
);
674 page
->pdimno
= pindex
;
675 page
->pageno
= pageno
;
676 page
->gen
= state
.gen
;
677 page
->type
= state
.type
;
682 static struct tile
*alloctile (int h
)
689 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
690 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
691 tile
= calloc (tilesize
, 1);
693 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
695 for (i
= 0; i
< slicecount
; ++i
) {
696 int sh
= MIN (h
, state
.sliceheight
);
697 tile
->slices
[i
].h
= sh
;
698 tile
->slices
[i
].texindex
= -1;
701 tile
->slicecount
= slicecount
;
702 tile
->sliceheight
= state
.sliceheight
;
706 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
711 struct pagedim
*pdim
;
713 tile
= alloctile (h
);
714 pdim
= &state
.pagedims
[page
->pdimno
];
719 bbox
.x1
= bbox
.x0
+ w
;
720 bbox
.y1
= bbox
.y0
+ h
;
723 if (state
.pig
->w
== w
725 && state
.pig
->colorspace
== state
.colorspace
) {
726 tile
->pixmap
= state
.pig
;
727 tile
->pixmap
->x
= bbox
.x0
;
728 tile
->pixmap
->y
= bbox
.y0
;
731 fz_drop_pixmap (state
.ctx
, state
.pig
);
737 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
742 clearpixmap (tile
->pixmap
);
743 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
744 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
745 fz_free_device (dev
);
750 static void initpdims (void)
756 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
762 switch (state
.type
) {
764 pageobj
= state
.u
.pdf
->page_objs
[pageno
];
766 if (state
.trimmargins
) {
770 page
= pdf_load_page (state
.u
.pdf
, pageno
);
771 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
772 if (state
.trimanew
|| !obj
) {
778 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
779 dev
->hints
|= FZ_IGNORE_SHADE
;
780 ctm
= fz_invert_matrix (page
->ctm
);
781 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
782 fz_free_device (dev
);
784 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
785 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
786 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
787 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
788 rect
= fz_transform_rect (ctm
, rect
);
789 rect
= fz_intersect_rect (rect
, page
->mediabox
);
791 if (fz_is_empty_rect (rect
)) {
792 mediabox
= page
->mediabox
;
798 obj
= fz_new_array (state
.ctx
, 4);
799 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
800 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
801 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
802 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
803 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
806 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
807 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
808 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
809 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
812 rotate
= page
->rotate
;
813 pdf_free_page (state
.u
.pdf
, page
);
815 printd ("progress %f Trimming %d",
816 (double) (pageno
+ 1) / state
.pagecount
,
822 mediabox
= pdf_to_rect (state
.ctx
,
823 fz_dict_gets (pageobj
, "MediaBox"));
824 if (fz_is_empty_rect (mediabox
)) {
825 fprintf (stderr
, "cannot find page size for page %d\n",
833 cropbox
= pdf_to_rect (state
.ctx
,
834 fz_dict_gets (pageobj
, "CropBox"));
835 if (!fz_is_empty_rect (cropbox
)) {
836 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
838 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
846 page
= xps_load_page (state
.u
.xps
, pageno
);
847 mediabox
= xps_bound_page (state
.u
.xps
, page
);
849 if (state
.trimmargins
) {
854 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
855 dev
->hints
|= FZ_IGNORE_SHADE
;
856 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
857 fz_free_device (dev
);
859 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
860 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
861 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
862 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
863 rect
= fz_intersect_rect (rect
, mediabox
);
865 if (!fz_is_empty_rect (rect
)) {
869 xps_free_page (state
.u
.xps
, page
);
870 printd ("progress %f loading %d",
871 (double) (pageno
+ 1) / state
.pagecount
,
879 if (state
.trimmargins
) {
882 page
= cbz_load_page (state
.u
.cbz
, pageno
);
883 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
884 cbz_free_page (state
.u
.cbz
, page
);
885 printd ("progress %f Trimming %d",
886 (double) (pageno
+ 1) / state
.pagecount
,
890 mediabox
.x0
= mediabox
.y0
= 0;
898 ARSERT (0 && state
.type
);
901 if (state
.pagedimcount
== 0
902 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
903 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
906 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
907 state
.pagedims
= realloc (state
.pagedims
, size
);
908 if (!state
.pagedims
) {
909 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
910 size
, state
.pagedimcount
+ 1);
913 p
= &state
.pagedims
[state
.pagedimcount
++];
915 p
->mediabox
= mediabox
;
920 if (state
.trimmargins
) {
921 printd ("progress 1 Trimmed %d pages in %f seconds",
922 state
.pagecount
, end
- start
);
925 printd ("vmsg Processed %d pages in %f seconds",
926 state
.pagecount
, end
- start
);
931 static void layout (void)
936 double zoom
, w
, maxw
= 0;
937 struct pagedim
*p
= state
.pagedims
;
939 if (state
.proportional
) {
940 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
941 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
944 maxw
= MAX (w
, maxw
);
949 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
952 ctm
= fz_rotate (state
.rotate
);
953 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
957 if (state
.proportional
) {
958 double scale
= w
/ maxw
;
959 zoom
= (state
.w
/ w
) * scale
;
965 p
->zoomctm
= fz_scale (zoom
, zoom
);
966 ctm
= fz_concat (p
->zoomctm
, ctm
);
968 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
969 p
->pagebox
.x1
-= p
->pagebox
.x0
;
970 p
->pagebox
.y1
-= p
->pagebox
.y0
;
973 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
976 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
980 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
981 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
982 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
988 while (p
-- != state
.pagedims
) {
989 int w
= p
->bounds
.x1
;
990 int h
= p
->bounds
.y1
;
992 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
996 static void recurse_outline (fz_outline
*outline
, int level
)
1001 struct pagedim
*pdim
= state
.pagedims
;
1003 dest
= &outline
->dest
;
1004 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1005 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1007 pdim
= &state
.pagedims
[i
];
1009 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1012 p
.y
= dest
->ld
.gotor
.lt
.y
;
1013 p
= fz_transform_point (pdim
->lctm
, p
);
1016 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1020 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1021 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1023 printd ("o %d %d %d %d %s",
1024 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1026 if (outline
->down
) {
1027 recurse_outline (outline
->down
, level
+ 1);
1029 outline
= outline
->next
;
1033 static void process_outline (void)
1035 fz_outline
*outline
;
1037 if (!state
.needoutline
) return;
1039 state
.needoutline
= 0;
1040 switch (state
.type
) {
1042 outline
= pdf_load_outline (state
.u
.pdf
);
1045 outline
= xps_load_outline (state
.u
.xps
);
1052 recurse_outline (outline
, 0);
1053 fz_free_outline (state
.ctx
, outline
);
1057 static int comparespans (const void *l
, const void *r
)
1059 fz_text_span
const *const*ls
= l
;
1060 fz_text_span
const *const*rs
= r
;
1061 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
1064 /* wishful thinking function */
1065 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1073 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1074 fz_text_span
*text
, *span
, **pspan
;
1075 struct pagedim
*pdim
, *pdimprev
;
1081 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1085 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1086 if (niters
++ == 5) {
1089 printd ("progress 1 attention requested aborting search at %d",
1094 printd ("progress %f searching in page %d",
1095 (double) (pageno
+ 1) / state
.pagecount
,
1100 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1101 pdim
= &state
.pagedims
[i
];
1102 if (pdim
->pageno
== pageno
) {
1105 if (pdim
->pageno
> pageno
) {
1114 text
= fz_new_text_span (state
.ctx
);
1115 tdev
= fz_new_text_device (state
.ctx
, text
);
1117 switch (state
.type
) {
1119 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1120 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, fz_identity
, NULL
);
1124 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1125 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, fz_identity
, NULL
);
1129 ARSERT (0 && state
.type
);
1132 fz_free_device (tdev
);
1135 for (span
= text
; span
; span
= span
->next
) {
1138 pspan
= malloc (sizeof (void *) * nspans
);
1140 err (1, "malloc span pointers %" FMT_s
, sizeof (void *) * nspans
);
1142 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1145 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1147 j
= forward
? 0 : nspans
- 1;
1152 j
+= forward
? 1 : -1;
1154 for (i
= 0; i
< span
->len
; ++i
) {
1159 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1164 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1168 len
= runetochar (cbuf
, &span
->text
[i
].c
);
1169 if (sizeof (buf
) - 1 - (p
- buf
) > len
) {
1171 for (k
= 0; k
< len
; ++k
)
1183 ret
= regexec (re
, buf
, 1, &rm
, 0);
1185 if (ret
!= REG_NOMATCH
) {
1188 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1189 printd ("msg regexec error `%.*s'",
1190 (int) size
, errbuf
);
1191 fz_free_text_span (state
.ctx
, text
);
1192 state
.freepage (u
.ptr
);
1199 fz_point p1
, p2
, p3
, p4
;
1203 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1204 c
+= runelen (span
->text
[a
].c
);
1206 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1207 c
+= runelen (span
->text
[b
].c
);
1210 if (runelen (span
->text
[b
].c
) > 1) {
1213 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1214 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
1225 switch (state
.type
) {
1227 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1228 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1236 p1
= fz_transform_point (ctm
, p1
);
1237 p2
= fz_transform_point (ctm
, p2
);
1238 p3
= fz_transform_point (ctm
, p3
);
1239 p4
= fz_transform_point (ctm
, p4
);
1242 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1249 printd ("progress 1 found at %d `%.*s' in %f sec",
1250 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1254 printd ("match %d %d %f %f %f %f %f %f %f %f",
1272 fz_free_text_span (state
.ctx
, text
);
1273 state
.freepage (u
.ptr
);
1278 printd ("progress 1 no matches %f sec", end
- start
);
1280 printd ("clearrects");
1283 static void set_tex_params (int colorspace
)
1285 switch (colorspace
) {
1287 state
.texiform
= GL_RGBA8
;
1288 state
.texform
= GL_RGBA
;
1289 state
.texty
= GL_UNSIGNED_BYTE
;
1290 state
.colorspace
= fz_device_rgb
;
1293 state
.texiform
= GL_RGBA8
;
1294 state
.texform
= GL_BGRA
;
1295 state
.texty
= fz_is_big_endian ()
1296 ? GL_UNSIGNED_INT_8_8_8_8
1297 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1298 state
.colorspace
= fz_device_bgr
;
1301 state
.texiform
= GL_LUMINANCE_ALPHA
;
1302 state
.texform
= GL_LUMINANCE_ALPHA
;
1303 state
.texty
= GL_UNSIGNED_BYTE
;
1304 state
.colorspace
= fz_device_gray
;
1307 errx (1, "invalid colorspce %d", colorspace
);
1311 static void realloctexts (int texcount
)
1315 if (texcount
== state
.texcount
) return;
1317 if (texcount
< state
.texcount
) {
1318 glDeleteTextures (state
.texcount
- texcount
,
1319 state
.texids
+ texcount
);
1322 size
= texcount
* sizeof (*state
.texids
);
1323 state
.texids
= realloc (state
.texids
, size
);
1324 if (!state
.texids
) {
1325 err (1, "realloc texids %" FMT_s
, size
);
1328 size
= texcount
* sizeof (*state
.texowners
);
1329 state
.texowners
= realloc (state
.texowners
, size
);
1330 if (!state
.texowners
) {
1331 err (1, "realloc texowners %" FMT_s
, size
);
1333 if (texcount
> state
.texcount
) {
1336 glGenTextures (texcount
- state
.texcount
,
1337 state
.texids
+ state
.texcount
);
1338 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1339 state
.texowners
[i
].w
= -1;
1340 state
.texowners
[i
].slice
= NULL
;
1343 state
.texcount
= texcount
;
1347 static void * mainloop (void *unused
)
1350 int len
, ret
, oldlen
= 0;
1355 errx (1, "readlen returned 0");
1358 if (oldlen
< len
+ 1) {
1359 p
= realloc (p
, len
+ 1);
1361 err (1, "realloc %d failed", len
+ 1);
1368 if (!strncmp ("open", p
, 4)) {
1371 char *filename
= p
+ 5;
1373 filenamelen
= strlen (filename
);
1374 password
= filename
+ filenamelen
+ 1;
1376 openxref (filename
, password
);
1379 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1380 state
.needoutline
= 1;
1382 else if (!strncmp ("cs", p
, 2)) {
1385 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1387 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1390 set_tex_params (colorspace
);
1391 for (i
= 0; i
< state
.texcount
; ++i
) {
1392 state
.texowners
[i
].w
= -1;
1393 state
.texowners
[i
].slice
= NULL
;
1397 else if (!strncmp ("freepage", p
, 8)) {
1400 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1402 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1406 else if (!strncmp ("freetile", p
, 8)) {
1409 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1411 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1415 else if (!strncmp ("search", p
, 6)) {
1416 int icase
, pageno
, y
, ret
, len2
, forward
;
1420 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1421 &icase
, &pageno
, &y
, &forward
, &len2
);
1423 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1426 pattern
= p
+ 6 + len2
;
1427 ret
= regcomp (&re
, pattern
,
1428 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1433 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1434 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1437 search (&re
, pageno
, y
, forward
);
1441 else if (!strncmp ("geometry", p
, 8)) {
1445 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1447 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1455 for (i
= 0; i
< state
.texcount
; ++i
) {
1456 state
.texowners
[i
].slice
= NULL
;
1462 unlock ("geometry");
1463 printd ("continue %d", state
.pagecount
);
1465 else if (!strncmp ("reqlayout", p
, 9)) {
1466 int rotate
, proportional
;
1469 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1471 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1474 state
.rotate
= rotate
;
1475 state
.proportional
= proportional
;
1477 unlock ("reqlayout");
1478 printd ("continue %d", state
.pagecount
);
1480 else if (!strncmp ("page", p
, 4)) {
1483 int pageno
, pindex
, ret
;
1485 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1487 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1492 page
= loadpage (pageno
, pindex
);
1496 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1498 else if (!strncmp ("tile", p
, 4)) {
1499 int x
, y
, w
, h
, ret
;
1504 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1505 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1507 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1512 tile
= rendertile (page
, x
, y
, w
, h
);
1516 printd ("tile %d %d %" FMT_ptr
" %u %f",
1518 FMT_ptr_cast2 (tile
),
1519 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1522 else if (!strncmp ("settrim", p
, 7)) {
1526 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1527 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1529 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1533 state
.trimmargins
= trimmargins
;
1534 state
.needoutline
= 1;
1535 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1537 state
.trimfuzz
= fuzz
;
1539 state
.pagedimcount
= 0;
1540 free (state
.pagedims
);
1541 state
.pagedims
= NULL
;
1546 printd ("continue %d", state
.pagecount
);
1548 else if (!strncmp ("sliceh", p
, 6)) {
1551 ret
= sscanf (p
+ 6, " %d", &h
);
1553 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1555 if (h
!= state
.sliceheight
) {
1558 state
.sliceheight
= h
;
1559 for (i
= 0; i
< state
.texcount
; ++i
) {
1560 state
.texowners
[i
].w
= -1;
1561 state
.texowners
[i
].h
= -1;
1562 state
.texowners
[i
].slice
= NULL
;
1566 else if (!strncmp ("interrupt", p
, 9)) {
1567 printd ("vmsg interrupted");
1569 else if (!strncmp ("quit", p
, 4)) {
1573 errx (1, "unknown command %.*s", len
, p
);
1579 CAMLprim value
ml_realloctexts (value texcount_v
)
1581 CAMLparam1 (texcount_v
);
1584 if (trylock ("ml_realloctexts")) {
1588 realloctexts (Int_val (texcount_v
));
1590 unlock ("ml_realloctexts");
1593 CAMLreturn (Val_bool (ok
));
1596 static void showsel (struct page
*page
, int ox
, int oy
)
1600 struct mark first
, last
;
1602 first
= page
->fmark
;
1605 if (!first
.span
|| !last
.span
) return;
1607 glEnable (GL_BLEND
);
1608 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1609 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1611 for (span
= first
.span
; span
; span
= span
->next
) {
1614 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1619 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1620 j
= MIN (first
.i
, last
.i
);
1621 k
= MAX (first
.i
, last
.i
);
1623 else if (span
== first
.span
) {
1626 else if (span
== last
.span
) {
1630 for (i
= j
; i
<= k
; ++i
) {
1631 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1633 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1640 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1642 if (span
== last
.span
) break;
1644 glDisable (GL_BLEND
);
1647 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1650 fz_link
*link
, *links
;
1652 switch (page
->type
) {
1654 links
= page
->u
.pdfpage
->links
;
1658 links
= page
->u
.xpspage
->links
;
1665 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1666 glEnable (GL_LINE_STIPPLE
);
1667 glLineStipple (0.5, 0xcccc);
1669 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1672 for (link
= links
; link
; link
= link
->next
) {
1673 fz_point p1
, p2
, p3
, p4
;
1675 p1
.x
= link
->rect
.x0
;
1676 p1
.y
= link
->rect
.y0
;
1678 p2
.x
= link
->rect
.x1
;
1679 p2
.y
= link
->rect
.y0
;
1681 p3
.x
= link
->rect
.x1
;
1682 p3
.y
= link
->rect
.y1
;
1684 p4
.x
= link
->rect
.x0
;
1685 p4
.y
= link
->rect
.y1
;
1687 p1
= fz_transform_point (ctm
, p1
);
1688 p2
= fz_transform_point (ctm
, p2
);
1689 p3
= fz_transform_point (ctm
, p3
);
1690 p4
= fz_transform_point (ctm
, p4
);
1692 switch (link
->dest
.kind
) {
1693 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1694 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1695 default: glColor3ub (0, 0, 0); break;
1698 glVertex2f (p1
.x
, p1
.y
);
1699 glVertex2f (p2
.x
, p2
.y
);
1700 glVertex2f (p3
.x
, p3
.y
);
1701 glVertex2f (p4
.x
, p4
.y
);
1705 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1706 glDisable (GL_LINE_STIPPLE
);
1709 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1712 struct slice
*slice1
;
1715 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1716 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1718 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
1719 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1720 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1724 int texindex
= state
.texindex
++ % state
.texcount
;
1726 if (state
.texowners
[texindex
].w
== tile
->w
) {
1727 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1731 state
.texowners
[texindex
].h
= slice
->h
;
1735 state
.texowners
[texindex
].h
= slice
->h
;
1738 state
.texowners
[texindex
].w
= tile
->w
;
1739 state
.texowners
[texindex
].slice
= slice
;
1740 slice
->texindex
= texindex
;
1742 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1744 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1752 tile
->pixmap
->samples
+offset
1756 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1764 tile
->pixmap
->samples
+offset
1770 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1772 CAMLparam2 (args_v
, ptr_v
);
1773 int dispx
= Int_val (Field (args_v
, 0));
1774 int dispy
= Int_val (Field (args_v
, 1));
1775 int dispw
= Int_val (Field (args_v
, 2));
1776 int disph
= Int_val (Field (args_v
, 3));
1777 int tilex
= Int_val (Field (args_v
, 4));
1778 int tiley
= Int_val (Field (args_v
, 5));
1779 char *s
= String_val (ptr_v
);
1780 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1782 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1784 int slicey
, firstslice
;
1785 struct slice
*slice
;
1787 firstslice
= tiley
/ tile
->sliceheight
;
1788 slice
= &tile
->slices
[firstslice
];
1789 slicey
= tiley
% tile
->sliceheight
;
1794 dh
= slice
->h
- slicey
;
1795 dh
= MIN (disph
, dh
);
1796 uploadslice (tile
, slice
);
1800 glTexCoord2i (tilex
, slicey
);
1801 glVertex2i (dispx
, dispy
);
1803 glTexCoord2i (tilex
+dispw
, slicey
);
1804 glVertex2i (dispx
+dispw
, dispy
);
1806 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1807 glVertex2i (dispx
+dispw
, dispy
+dh
);
1809 glTexCoord2i (tilex
, slicey
+dh
);
1810 glVertex2i (dispx
, dispy
+dh
);
1817 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1821 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1822 CAMLreturn (Val_unit
);
1825 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1826 value xoff_v
, value yoff_v
)
1828 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1829 int xoff
= Int_val (xoff_v
);
1830 int yoff
= Int_val (yoff_v
);
1831 char *s
= String_val (ptr_v
);
1832 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1834 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1836 if (trylock ("ml_postprocess")) {
1839 showsel (page
, xoff
, yoff
);
1840 unlock ("ml_postprocess");
1843 CAMLreturn (Val_unit
);
1846 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1850 fz_link
*link
, *links
;
1852 switch (page
->type
) {
1854 ctm
= trimctm (page
->u
.pdfpage
, page
->pdimno
);
1855 links
= page
->u
.pdfpage
->links
;
1860 links
= page
->u
.xpspage
->links
;
1869 ctm
= fz_concat (ctm
, state
.pagedims
[page
->pdimno
].ctm
);
1870 ctm
= fz_invert_matrix (ctm
);
1871 p
= fz_transform_point (ctm
, p
);
1873 for (link
= links
; link
; link
= link
->next
) {
1874 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1875 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1883 static void droptext (struct page
*page
)
1886 fz_free_text_span (state
.ctx
, page
->text
);
1889 page
->fmark
.span
= NULL
;
1890 page
->lmark
.span
= NULL
;
1895 static void ensuretext (struct page
*page
)
1897 if (state
.gen
!= page
->gen
) {
1899 page
->gen
= state
.gen
;
1904 page
->text
= fz_new_text_span (state
.ctx
);
1905 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
1906 fz_run_display_list (page
->dlist
,
1909 fz_infinite_bbox
, NULL
);
1910 fz_free_device (tdev
);
1914 static int compareslinks (const void *l
, const void *r
)
1916 struct slink
const *ls
= l
;
1917 struct slink
const *rs
= r
;
1918 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1919 return rs
->bbox
.x0
- rs
->bbox
.x0
;
1921 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1924 static char *regiontext (struct page
*page
, fz_bbox bbox
)
1928 int bufcap
= 0, buflen
= 0;
1931 for (span
= page
->text
; span
; span
= span
->next
) {
1934 for (i
= 0; i
< span
->len
; ++i
) {
1937 tb
= span
->text
[i
].bbox
;
1938 b
= fz_intersect_bbox (bbox
, tb
);
1939 if (fz_is_empty_bbox (b
)) {
1943 len
= runetochar (utf8
, &span
->text
[i
].c
);
1944 if (buflen
+ len
+ 1 >= bufcap
) {
1946 bufcap
= bufcap
+ len
+ 1;
1948 buf
= realloc (buf
, size
);
1950 err (1, "realloc %" FMT_s
, size
);
1953 memcpy (buf
+ buflen
, utf8
, len
);
1964 static void ensureslinks (struct page
*page
)
1968 size_t slinksize
= sizeof (*page
->slinks
);
1969 fz_link
*link
, *links
;
1971 if (page
->slinks
) return;
1973 switch (page
->type
) {
1975 links
= page
->u
.pdfpage
->links
;
1976 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1977 state
.pagedims
[page
->pdimno
].ctm
);
1981 links
= page
->u
.xpspage
->links
;
1982 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
1989 for (link
= links
; link
; link
= link
->next
) {
1993 page
->slinkcount
= count
;
1994 page
->slinks
= calloc (count
, slinksize
);
1995 if (!page
->slinks
) {
1996 err (1, "realloc slinks %d", count
);
1999 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2000 page
->slinks
[i
].link
= link
;
2001 page
->slinks
[i
].bbox
=
2002 fz_round_rect (fz_transform_rect (ctm
, link
->rect
));
2003 page
->slinks
[i
].text
= regiontext (page
, page
->slinks
[i
].bbox
);
2005 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2009 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2011 CAMLparam2 (start_page_v
, dir_v
);
2013 int i
, dir
= Int_val (dir_v
);
2014 int start_page
= Int_val (start_page_v
);
2015 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2017 ret_v
= Val_int (0);
2018 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2022 lock ("ml_findpage_with_links");
2023 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2026 switch (state
.type
) {
2029 pdf_page
*page
= pdf_load_page (state
.u
.pdf
, i
);
2030 found
= !!page
->links
;
2036 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2037 found
= !!page
->links
;
2043 ARSERT ("invalid document type");
2047 ret_v
= caml_alloc_small (1, 1);
2048 Field (ret_v
, 0) = Val_int (i
);
2053 unlock ("ml_findpage_with_links");
2059 enum { dir_first
, dir_last
};
2060 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2062 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2064 CAMLparam2 (ptr_v
, dir_v
);
2065 CAMLlocal3 (ret_v
, tup_v
, pos_v
);
2067 int dirtag
, i
, slinkindex
;
2068 struct slink
*found
= NULL
,*slink
;
2069 char *s
= String_val (ptr_v
);
2071 lock ("ml_findlink");
2073 page
= parse_pointer ("ml_findlink", s
);
2074 ensureslinks (page
);
2076 ret_v
= Val_int (0);
2077 if (Is_block (dir_v
)) {
2078 dirtag
= Tag_val (dir_v
);
2080 case dir_first_visible
:
2084 pos_v
= Field (dir_v
, 0);
2085 x0
= Int_val (Field (pos_v
, 0));
2086 y0
= Int_val (Field (pos_v
, 1));
2087 dir
= Int_val (Field (pos_v
, 2));
2090 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2091 slink
= &page
->slinks
[i
];
2092 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2099 for (i
= page
->slinkcount
- 1; i
>= 0; --i
) {
2100 slink
= &page
->slinks
[i
];
2101 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2111 slinkindex
= Int_val (Field (dir_v
, 0));
2112 found
= &page
->slinks
[slinkindex
];
2113 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2114 slink
= &page
->slinks
[i
];
2115 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2123 slinkindex
= Int_val (Field (dir_v
, 0));
2124 found
= &page
->slinks
[slinkindex
];
2125 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2126 slink
= &page
->slinks
[i
];
2127 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2135 slinkindex
= Int_val (Field (dir_v
, 0));
2136 found
= &page
->slinks
[slinkindex
];
2137 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2138 slink
= &page
->slinks
[i
];
2139 if (slink
->bbox
.y0
> found
->bbox
.y0
) {
2147 slinkindex
= Int_val (Field (dir_v
, 0));
2148 found
= &page
->slinks
[slinkindex
];
2149 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2150 slink
= &page
->slinks
[i
];
2151 if (slink
->bbox
.y0
< found
->bbox
.y0
) {
2160 dirtag
= Int_val (dir_v
);
2163 found
= page
->slinks
;
2168 found
= page
->slinks
+ (page
->slinkcount
- 1);
2174 tup_v
= caml_alloc_tuple (5);
2175 ret_v
= caml_alloc_small (2, 1);
2176 Field (tup_v
, 0) = Val_int (found
- page
->slinks
);
2177 Field (tup_v
, 1) = Val_int (found
->bbox
.x0
);
2178 Field (tup_v
, 2) = Val_int (found
->bbox
.y0
);
2179 Field (tup_v
, 3) = Val_int (found
->bbox
.x1
);
2180 Field (tup_v
, 4) = Val_int (found
->bbox
.y1
);
2181 Field (ret_v
, 0) = tup_v
;
2184 unlock ("ml_findlink");
2188 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2194 switch (link->dest.kind) { \
2195 case FZ_LINK_GOTO: \
2199 pageno = link->dest.ld.gotor.page; \
2203 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2204 p.y = link->dest.ld.gotor.lt.y; \
2205 p = fz_transform_point (pdim->lctm, p); \
2207 tup_v = caml_alloc_tuple (2); \
2208 ret_v = caml_alloc_small (1, ugoto); \
2209 Field (tup_v, 0) = Val_int (pageno); \
2210 Field (tup_v, 1) = Val_int (p.y); \
2211 Field (ret_v, 0) = tup_v; \
2216 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2217 ret_v = caml_alloc_small (1, uuri); \
2218 Field (ret_v, 0) = str_v; \
2221 case FZ_LINK_LAUNCH: \
2222 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2223 ret_v = caml_alloc_small (1, ulaunch); \
2224 Field (ret_v, 0) = str_v; \
2227 case FZ_LINK_NAMED: \
2228 str_v = caml_copy_string (link->dest.ld.named.named); \
2229 ret_v = caml_alloc_small (1, unamed); \
2230 Field (ret_v, 0) = str_v; \
2233 case FZ_LINK_GOTOR: \
2234 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2235 pageno = link->dest.ld.gotor.page; \
2236 tup_v = caml_alloc_tuple (2); \
2237 ret_v = caml_alloc_small (1, uremote); \
2238 Field (tup_v, 0) = str_v; \
2239 Field (tup_v, 1) = Val_int (pageno); \
2240 Field (ret_v, 0) = tup_v; \
2247 snprintf (buf, sizeof (buf), \
2248 "unhandled link kind %d", link->dest.kind); \
2249 str_v = caml_copy_string (buf); \
2250 ret_v = caml_alloc_small (1, uunexpected); \
2251 Field (ret_v, 0) = str_v; \
2257 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2259 CAMLparam2 (ptr_v
, n_v
);
2260 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2263 struct pagedim
*pdim
;
2264 char *s
= String_val (ptr_v
);
2266 ret_v
= Val_int (0);
2267 page
= parse_pointer ("ml_getlink", s
);
2268 pdim
= &state
.pagedims
[page
->pdimno
];
2269 link
= page
->slinks
[Int_val (n_v
)].link
;
2274 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2276 CAMLparam3 (ptr_v
, x_v
, y_v
);
2277 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2280 char *s
= String_val (ptr_v
);
2281 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2282 struct pagedim
*pdim
;
2284 ret_v
= Val_int (0);
2285 if (trylock ("ml_whatsunder")) {
2289 page
= parse_pointer ("ml_whatsunder", s
);
2290 pdim
= &state
.pagedims
[page
->pdimno
];
2291 link
= getlink (page
, x
, y
);
2300 for (span
= page
->text
; span
; span
= span
->next
) {
2301 for (i
= 0; i
< span
->len
; ++i
) {
2303 b
= &span
->text
[i
].bbox
;
2304 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
2306 span
->font
&& span
->font
->name
2308 : "Span has no font name"
2310 FT_FaceRec
*face
= span
->font
->ft_face
;
2311 if (face
&& face
->family_name
) {
2313 char *n1
= face
->family_name
;
2314 size_t l1
= strlen (n1
);
2315 size_t l2
= strlen (n2
);
2317 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2318 s
= malloc (l1
+ l2
+ 2);
2322 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2323 str_v
= caml_copy_string (s
);
2329 str_v
= caml_copy_string (n2
);
2331 ret_v
= caml_alloc_small (1, utext
);
2332 Field (ret_v
, 0) = str_v
;
2339 unlock ("ml_whatsunder");
2345 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2347 CAMLparam2 (ptr_v
, rect_v
);
2351 struct mark first
, last
;
2352 int i
, x0
, x1
, y0
, y1
;
2353 char *s
= String_val (ptr_v
);
2355 if (trylock ("ml_seltext")) {
2359 page
= parse_pointer ("ml_seltext", s
);
2362 x0
= Int_val (Field (rect_v
, 0));
2363 y0
= Int_val (Field (rect_v
, 1));
2364 x1
= Int_val (Field (rect_v
, 2));
2365 y1
= Int_val (Field (rect_v
, 3));
2368 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2369 glColor3ub (128, 128, 128);
2370 glRecti (x0
, y0
, x1
, y1
);
2371 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2377 last
.i
= first
.i
= 0;
2378 first
.span
= page
->text
;
2379 for (span
= page
->text
; span
; span
= span
->next
) {
2380 for (i
= 0; i
< span
->len
; ++i
) {
2381 b
= &span
->text
[i
].bbox
;
2384 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
2389 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
2394 if (0 && selected
) {
2395 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2396 glColor3ub (128, 128, 128);
2397 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2398 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2403 if (y1
< y0
|| x1
< x0
) {
2406 if (first
.span
== last
.span
) {
2411 for (span
= first
.span
; span
&& span
!= last
.span
;
2412 span
= span
->next
) {
2425 first
.span
= last
.span
;
2431 page
->fmark
= first
;
2434 unlock ("ml_seltext");
2437 CAMLreturn (Val_unit
);
2440 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2445 for (i
= a
; i
<= b
; ++i
) {
2446 len
= runetochar (buf
, &span
->text
[i
].c
);
2447 ret
= fwrite (buf
, len
, 1, f
);
2450 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2451 len
, ret
, strerror (errno
));
2458 CAMLprim value
ml_copysel (value command_v
, value ptr_v
)
2464 char *s
= String_val (ptr_v
);
2465 char *command
= String_val (command_v
);
2467 if (trylock ("ml_copysel")) {
2471 page
= parse_pointer ("ml_sopysel", s
);
2473 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2474 fprintf (stderr
, "nothing to copy\n");
2478 f
= popen (command
, "w");
2480 fprintf (stderr
, "failed to open sel pipe: %s\n",
2485 for (span
= page
->fmark
.span
;
2486 span
&& span
!= page
->lmark
.span
->next
;
2487 span
= span
->next
) {
2488 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2489 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2490 if (pipespan (f
, span
, a
, b
)) {
2494 if (putc ('\n', f
) == EOF
) {
2495 fprintf (stderr
, "failed break line on sel pipe: %s\n",
2501 page
->lmark
.span
= NULL
;
2502 page
->fmark
.span
= NULL
;
2506 int ret
= pclose (f
);
2508 if (errno
!= ECHILD
) {
2509 fprintf (stderr
, "failed to close sel pipe: %s\n",
2515 unlock ("ml_copysel");
2518 CAMLreturn (Val_unit
);
2521 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2523 CAMLparam1 (pagedimno_v
);
2525 int pagedimno
= Int_val (pagedimno_v
);
2528 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2529 if (trylock ("ml_getpdimrect")) {
2530 box
= fz_empty_rect
;
2533 box
= state
.pagedims
[pagedimno
].mediabox
;
2534 unlock ("ml_getpdimrect");
2537 Store_double_field (ret_v
, 0, box
.x0
);
2538 Store_double_field (ret_v
, 1, box
.x1
);
2539 Store_double_field (ret_v
, 2, box
.y0
);
2540 Store_double_field (ret_v
, 3, box
.y1
);
2545 static double getmaxw (void)
2551 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2554 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2555 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2558 maxw
= MAX (w
, maxw
);
2563 CAMLprim value
ml_getmaxw (value unit_v
)
2565 CAMLparam1 (unit_v
);
2569 if (trylock ("ml_getmaxw")) {
2573 unlock ("ml_getmaxw");
2575 ret_v
= caml_copy_double (maxw
);
2579 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2581 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2585 double maxw
= 0.0, maxh
= 0.0;
2587 double winw
= Int_val (winw_v
);
2588 double winh
= Int_val (winh_v
);
2589 double dw
= Int_val (dw_v
);
2590 double pw
= 1.0, ph
= 1.0, num
, den
;
2592 if (trylock ("ml_zoom_for_height")) {
2596 if (state
.proportional
) {
2600 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2601 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2603 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2604 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2605 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2606 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2611 if (state
.proportional
) {
2613 scaledh
= h
* scale
;
2620 if (scaledh
> maxh
) {
2627 num
= (winh
* pw
) + (ph
* dw
);
2631 unlock ("ml_zoom_for_height");
2633 ret_v
= caml_copy_double (zoom
);
2639 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2641 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2643 int pt
= Int_val(pt_v
);
2644 int x
= Int_val (x_v
);
2645 int y
= Int_val (y_v
);
2648 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2649 ret_v
= caml_copy_double (w
);
2653 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2655 CAMLparam2 (pt_v
, string_v
);
2657 int pt
= Int_val (pt_v
);
2660 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2661 ret_v
= caml_copy_double (w
);
2665 CAMLprim value
ml_getpagebox (value opaque_v
)
2667 CAMLparam1 (opaque_v
);
2671 char *s
= String_val (opaque_v
);
2672 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2674 ret_v
= caml_alloc_tuple (4);
2675 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2676 dev
->hints
|= FZ_IGNORE_SHADE
;
2678 switch (page
->type
) {
2680 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
2684 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
2688 bbox
= fz_infinite_bbox
;
2692 fz_free_device (dev
);
2693 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2694 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2695 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2696 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2701 CAMLprim value
ml_setaalevel (value level_v
)
2703 CAMLparam1 (level_v
);
2705 state
.aalevel
= Int_val (level_v
);
2706 CAMLreturn (Val_unit
);
2710 #include <X11/Xlib.h>
2716 GLXDrawable drawable
;
2719 #include "keysym2ucs.c"
2721 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
2723 CAMLparam1 (keysym_v
);
2725 KeySym keysym
= Int_val (keysym_v
);
2730 rune
= keysym2ucs (keysym
);
2731 len
= runetochar (buf
, &rune
);
2733 str_v
= caml_copy_string (buf
);
2737 CAMLprim value
ml_glx (value win_v
)
2741 XVisualInfo
*visual
;
2742 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
2744 glx
.dpy
= XOpenDisplay (NULL
);
2746 caml_failwith ("XOpenDisplay failed");
2749 screen
= DefaultScreen (glx
.dpy
);
2750 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
2752 XCloseDisplay (glx
.dpy
);
2754 caml_failwith ("glXChooseVisual");
2757 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
2759 XCloseDisplay (glx
.dpy
);
2762 caml_failwith ("glXCreateContext");
2766 if (!glXMakeCurrent (glx
.dpy
, Int_val (win_v
), glx
.ctx
)) {
2767 glXDestroyContext (glx
.dpy
, glx
.ctx
);
2768 XCloseDisplay (glx
.dpy
);
2771 caml_failwith ("glXMakeCurrent");
2773 glx
.drawable
= Int_val (win_v
);
2775 CAMLreturn (Val_unit
);
2778 CAMLprim value
ml_swapb (value unit_v
)
2780 CAMLparam1 (unit_v
);
2781 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
2782 CAMLreturn (Val_unit
);
2785 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
2786 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
2788 CAMLprim value
ml_platform (value unit_v
)
2790 CAMLparam1 (unit_v
);
2791 int platid
= piunknown
;
2793 #if defined __linux__
2795 #elif defined __CYGWIN__
2797 #elif defined __DragonFly__
2798 platid
= pidragonflybsd
;
2799 #elif defined __FreeBSD__
2801 #elif defined __OpenBSD__
2803 #elif defined __NetBSD__
2805 #elif defined __sun__
2807 #elif defined __APPLE__
2810 CAMLreturn (Val_int (platid
));
2813 CAMLprim value
ml_cloexec (value fd_v
)
2816 int fd
= Int_val (fd_v
);
2818 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
2819 uerror ("fcntl", Nothing
);
2821 CAMLreturn (Val_unit
);
2824 CAMLprim value
ml_init (value pipe_v
, value params_v
)
2826 CAMLparam2 (pipe_v
, params_v
);
2827 CAMLlocal2 (trim_v
, fuzz_v
);
2833 struct sigaction sa
;
2835 state
.cr
= Int_val (Field (pipe_v
, 0));
2836 state
.cw
= Int_val (Field (pipe_v
, 1));
2837 state
.rotate
= Int_val (Field (params_v
, 0));
2838 state
.proportional
= Bool_val (Field (params_v
, 1));
2839 trim_v
= Field (params_v
, 2);
2840 texcount
= Int_val (Field (params_v
, 3));
2841 state
.sliceheight
= Int_val (Field (params_v
, 4));
2842 mustoresize
= Int_val (Field (params_v
, 5));
2843 colorspace
= Int_val (Field (params_v
, 6));
2844 fontpath
= String_val (Field (params_v
, 7));
2846 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
2847 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2849 fuzz_v
= Field (trim_v
, 1);
2850 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2851 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2852 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2853 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2855 set_tex_params (colorspace
);
2858 state
.face
= load_font (fontpath
);
2862 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2864 state
.face
= load_builtin_font (base
, len
);
2866 if (!state
.face
) _exit (1);
2868 realloctexts (texcount
);
2870 sa
.sa_handler
= SIG_DFL
;
2871 if (sigemptyset (&sa
.sa_mask
)) {
2872 err (1, "sigemptyset");
2874 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
2875 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
2876 err (1, "sigaction");
2879 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2881 errx (1, "pthread_create: %s", strerror (ret
));
2884 CAMLreturn (Val_unit
);