1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/utsname.h>
20 #include <cygwin/socket.h> /* FIONREAD */
32 #include <caml/fail.h>
33 #include <caml/alloc.h>
34 #include <caml/memory.h>
35 #include <caml/unixsupport.h>
37 #include <mupdf/fitz.h>
38 #include <mupdf/cbz.h>
39 #include <mupdf/pdf.h>
40 #include <mupdf/xps.h>
41 #include <mupdf/img.h>
44 #include FT_FREETYPE_H
47 #include <fontconfig/fontconfig.h>
51 #define CACHE_PAGEREFS
54 extern char **environ
;
57 #define MIN(a,b) ((a) < (b) ? (a) : (b))
58 #define MAX(a,b) ((a) > (b) ? (a) : (b))
61 #define NORETURN_ATTR __attribute__ ((noreturn))
62 #define UNUSED_ATTR __attribute__ ((unused))
63 #if !defined __clang__
64 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
66 #define OPTIMIZE_ATTR(n)
68 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
72 #define OPTIMIZE_ATTR(n)
73 #define GCC_FMT_ATTR(a, b)
78 #define FMT_ptr PRIxPTR
79 #define SCN_ptr SCNxPTR
80 #define FMT_ptr_cast(p) ((uintptr_t) (p))
81 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
83 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
84 err (int exitcode
, const char *fmt
, ...)
91 vfprintf (stderr
, fmt
, ap
);
93 fprintf (stderr
, ": %s\n", strerror (savederrno
));
98 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
99 errx (int exitcode
, const char *fmt
, ...)
104 vfprintf (stderr
, fmt
, ap
);
106 fputc ('\n', stderr
);
111 #ifndef GL_TEXTURE_RECTANGLE_ARB
112 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
116 #define GL_BGRA 0x80E1
119 #ifndef GL_UNSIGNED_INT_8_8_8_8
120 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
123 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
124 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
128 #define lprintf printf
133 #define ARSERT(cond) for (;;) { \
135 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
151 struct slice slices
[1];
162 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
175 enum { DNONE
, DPDF
, DXPS
, DCBZ
, DIMG
};
184 fz_text_sheet
*sheet
;
192 fz_display_list
*dlist
;
194 struct slink
*slinks
;
196 struct annot
*annots
;
201 void (*freepage
) (void *);
207 struct pagedim
*pagedims
;
227 fz_colorspace
*colorspace
;
235 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
249 void (*closedoc
) (void);
250 void (*freepage
) (void *);
258 void (*glBindBufferARB
) (GLenum
, GLuint
);
259 GLboolean (*glUnmapBufferARB
) (GLenum
);
260 void *(*glMapBufferARB
) (GLenum
, GLenum
);
261 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
262 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
263 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
265 GLfloat texcoords
[8];
266 GLfloat vertices
[16];
268 #ifdef CACHE_PAGEREFS
284 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
286 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
289 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
291 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
294 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
296 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
297 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
300 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
302 static void lock (const char *cap
)
304 int ret
= pthread_mutex_lock (&mutex
);
306 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
310 static void unlock (const char *cap
)
312 int ret
= pthread_mutex_unlock (&mutex
);
314 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
318 static int trylock (const char *cap
)
320 int ret
= pthread_mutex_trylock (&mutex
);
321 if (ret
&& ret
!= EBUSY
) {
322 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
327 static void *parse_pointer (const char *cap
, const char *s
)
332 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
334 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
339 static double now (void)
343 if (gettimeofday (&tv
, NULL
)) {
344 err (1, "gettimeofday");
346 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
349 static int hasdata (void)
352 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
353 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
357 CAMLprim value
ml_hasdata (value fd_v
)
362 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
363 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
364 CAMLreturn (Val_bool (avail
> 0));
367 static void readdata (void *p
, int size
)
372 n
= read (state
.csock
, p
, size
);
374 if (errno
== EINTR
) goto again
;
375 err (1, "read (req %d, ret %zd)", size
, n
);
378 if (!n
) errx (1, "EOF while reading");
379 errx (1, "read (req %d, ret %zd)", size
, n
);
383 static void writedata (char *p
, int size
)
387 p
[0] = (size
>> 24) & 0xff;
388 p
[1] = (size
>> 16) & 0xff;
389 p
[2] = (size
>> 8) & 0xff;
390 p
[3] = (size
>> 0) & 0xff;
392 n
= write (state
.csock
, p
, size
+ 4);
394 if (!n
) errx (1, "EOF while writing data");
395 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
399 static int readlen (void)
404 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
407 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
415 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
418 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
422 if (len
< size
- 4) {
423 writedata (buf
, len
);
429 err (1, "vsnprintf for `%s' failed", fmt
);
431 buf
= realloc (buf
, size
);
436 static void closepdf (void)
438 #ifdef CACHE_PAGEREFS
439 if (state
.pdflut
.objs
) {
442 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
443 pdf_drop_obj (state
.pdflut
.objs
[i
]);
445 free (state
.pdflut
.objs
);
446 state
.pdflut
.objs
= NULL
;
447 state
.pdflut
.idx
= 0;
452 pdf_close_document (state
.u
.pdf
);
457 static void closexps (void)
460 xps_close_document (state
.u
.xps
);
465 static void closecbz (void)
468 cbz_close_document (state
.u
.cbz
);
473 static void closeimg (void)
476 image_close_document (state
.u
.img
);
481 static void freepdfpage (void *ptr
)
483 pdf_free_page (state
.u
.pdf
, ptr
);
486 static void freeemptyxxxpage (void *ptr
)
491 static void freexpspage (void *ptr
)
493 xps_free_page (state
.u
.xps
, ptr
);
496 static void freecbzpage (void *ptr
)
498 cbz_free_page (state
.u
.cbz
, ptr
);
501 static void freeimgpage (void *ptr
)
503 image_free_page (state
.u
.img
, ptr
);
506 static void openxref (char *filename
, char *password
)
510 for (i
= 0; i
< state
.texcount
; ++i
) {
511 state
.texowners
[i
].w
= -1;
512 state
.texowners
[i
].slice
= NULL
;
515 if (state
.closedoc
) state
.closedoc ();
517 len
= strlen (filename
);
535 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
536 const char *ext
= tbl
[j
].ext
;
537 int len2
= strlen (ext
);
539 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
540 state
.type
= tbl
[j
].type
;
546 if (state
.pagedims
) {
547 free (state
.pagedims
);
548 state
.pagedims
= NULL
;
550 state
.pagedimcount
= 0;
552 fz_set_aa_level (state
.ctx
, state
.aalevel
);
553 switch (state
.type
) {
555 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
556 if (pdf_needs_password (state
.u
.pdf
)) {
557 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
559 errx (1, "invalid password");
562 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
563 state
.closedoc
= closepdf
;
564 state
.freepage
= freepdfpage
;
568 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
569 state
.pagecount
= xps_count_pages (state
.u
.xps
);
570 state
.closedoc
= closexps
;
571 state
.freepage
= freexpspage
;
575 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
576 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
577 state
.closedoc
= closecbz
;
578 state
.freepage
= freecbzpage
;
582 state
.u
.img
= image_open_document (state
.ctx
, filename
);
584 state
.closedoc
= closeimg
;
585 state
.freepage
= freeimgpage
;
590 static void pdfinfo (void)
592 if (state
.type
== DPDF
) {
595 printd ("info PDF version\t%d.%d",
596 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
598 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
602 char *items
[] = { "Title", "Author", "Creator",
603 "Producer", "CreationDate" };
605 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
606 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
607 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
608 if (*s
) printd ("info %s\t%s", items
[i
], s
);
609 fz_free (state
.ctx
, s
);
616 static void unlinktile (struct tile
*tile
)
620 for (i
= 0; i
< tile
->slicecount
; ++i
) {
621 struct slice
*s
= &tile
->slices
[i
];
623 if (s
->texindex
!= -1) {
624 if (state
.texowners
[s
->texindex
].slice
== s
) {
625 state
.texowners
[s
->texindex
].slice
= NULL
;
631 static void freepage (struct page
*page
)
634 fz_free_text_page (state
.ctx
, page
->text
);
637 fz_free_text_sheet (state
.ctx
, page
->sheet
);
642 fz_drop_display_list (state
.ctx
, page
->dlist
);
643 page
->freepage (page
->u
.ptr
);
647 static void freetile (struct tile
*tile
)
652 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
655 fz_drop_pixmap (state
.ctx
, state
.pig
);
657 state
.pig
= tile
->pixmap
;
662 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
671 static int cacheline32bytes
;
673 static void __attribute__ ((constructor
)) clcheck (void)
675 char **envp
= environ
;
680 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
682 cacheline32bytes
= auxv
[1] == 32;
688 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
690 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
691 if (cacheline32bytes
&& size
> 32) {
692 intptr_t a1
, a2
, diff
;
694 vector
unsigned char v
= vec_splat_u8 (-1);
695 vector
unsigned char *p
;
697 a1
= a2
= (intptr_t) pixmap
->samples
;
698 a2
= (a1
+ 31) & ~31;
703 while (a1
!= a2
) *(char *) a1
++ = 0xff;
704 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
705 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
707 vec_st (v
, i
+ 16, p
);
709 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
711 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
714 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
717 static void trimctm (pdf_page
*page
, int pindex
)
720 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
722 if (!pdim
->tctmready
) {
723 if (state
.trimmargins
) {
725 fz_matrix rm
, sm
, tm
, im
, ctm1
;
727 fz_rotate (&rm
, -pdim
->rotate
);
728 fz_scale (&sm
, 1, -1);
729 fz_concat (&ctm
, &rm
, &sm
);
730 realbox
= pdim
->mediabox
;
731 fz_transform_rect (&realbox
, &ctm
);
732 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
733 fz_concat (&ctm1
, &ctm
, &tm
);
734 fz_invert_matrix (&im
, &page
->ctm
);
735 fz_concat (&ctm
, &im
, &ctm1
);
745 static fz_matrix
pagectm (struct page
*page
)
749 if (page
->type
== DPDF
) {
750 trimctm (page
->u
.pdfpage
, page
->pdimno
);
752 &state
.pagedims
[page
->pdimno
].tctm
,
753 &state
.pagedims
[page
->pdimno
].ctm
);
756 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
758 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
759 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
764 static void *loadpage (int pageno
, int pindex
)
769 page
= calloc (sizeof (struct page
), 1);
771 err (1, "calloc page %d", pageno
);
774 page
->dlist
= fz_new_display_list (state
.ctx
);
775 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
777 switch (state
.type
) {
779 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
780 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
782 page
->freepage
= freepdfpage
;
786 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
787 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
789 page
->freepage
= freexpspage
;
793 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
794 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
796 page
->freepage
= freecbzpage
;
800 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
801 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
803 page
->freepage
= freeimgpage
;
807 fz_catch (state
.ctx
) {
809 page
->freepage
= freeemptyxxxpage
;
811 fz_free_device (dev
);
813 page
->pdimno
= pindex
;
814 page
->pageno
= pageno
;
815 page
->sgen
= state
.gen
;
816 page
->tgen
= state
.gen
;
817 page
->type
= state
.type
;
822 static struct tile
*alloctile (int h
)
829 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
830 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
831 tile
= calloc (tilesize
, 1);
833 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
835 for (i
= 0; i
< slicecount
; ++i
) {
836 int sh
= MIN (h
, state
.sliceheight
);
837 tile
->slices
[i
].h
= sh
;
838 tile
->slices
[i
].texindex
= -1;
841 tile
->slicecount
= slicecount
;
842 tile
->sliceheight
= state
.sliceheight
;
852 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
853 const fz_matrix
*ctm
, float alpha
)
855 struct obs
*obs
= dev
->user
;
857 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
859 fz_rect rect
= fz_unit_rect
;
861 fz_transform_rect (&rect
, ctm
);
862 fz_round_rect (&b
, &rect
);
863 fz_intersect_irect (&b
, &obs
->b
);
864 obs
->cured
= b
.x0
== obs
->b
.x0
867 && b
.y1
== obs
->b
.y1
;
871 static int obscured (struct page
*page
, fz_irect bbox
)
878 memset (&dev
, 0, sizeof (dev
));
879 memset (&obs
, 0, sizeof (obs
));
884 dev
.fill_image
= obs_fill_image
;
886 fz_rect_from_irect (&rect
, &bbox
);
887 ctm
= pagectm (page
);
888 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
891 #define OBSCURED obscured
893 #define OBSCURED(a, b) 0
896 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
904 struct pagedim
*pdim
;
906 tile
= alloctile (h
);
907 pdim
= &state
.pagedims
[page
->pdimno
];
912 bbox
.x1
= bbox
.x0
+ w
;
913 bbox
.y1
= bbox
.y0
+ h
;
916 if (state
.pig
->w
== w
918 && state
.pig
->colorspace
== state
.colorspace
) {
919 tile
->pixmap
= state
.pig
;
920 tile
->pixmap
->x
= bbox
.x0
;
921 tile
->pixmap
->y
= bbox
.y0
;
924 fz_drop_pixmap (state
.ctx
, state
.pig
);
931 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
937 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
943 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
944 clearpixmap (tile
->pixmap
);
946 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
947 ctm
= pagectm (page
);
948 fz_rect_from_irect (&rect
, &bbox
);
949 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
950 fz_free_device (dev
);
955 #ifdef CACHE_PAGEREFS
956 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
957 thanks to Robin Watts */
959 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
961 fz_context
*ctx
= doc
->ctx
;
965 if (state
.pdflut
.idx
== state
.pagecount
) return;
967 kids
= pdf_dict_gets (node
, "Kids");
968 len
= pdf_array_len (kids
);
971 fz_throw (ctx
, FZ_ERROR_GENERIC
, "Malformed pages tree");
973 if (pdf_mark_obj (node
))
974 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
975 for (i
= 0; i
< len
; i
++) {
976 pdf_obj
*kid
= pdf_array_get (kids
, i
);
977 char *type
= pdf_to_name (pdf_dict_gets (kid
, "Type"));
979 ? !strcmp (type
, "Pages")
980 : pdf_dict_gets (kid
, "Kids")
981 && !pdf_dict_gets (kid
, "MediaBox")) {
982 pdf_collect_pages (doc
, kid
);
986 ? strcmp (type
, "Page") != 0
987 : !pdf_dict_gets (kid
, "MediaBox"))
988 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
989 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (kid
);
992 pdf_unmark_obj (node
);
996 pdf_load_page_objs (pdf_document
*doc
)
998 pdf_obj
*root
= pdf_dict_gets (pdf_trailer (doc
), "Root");
999 pdf_obj
*node
= pdf_dict_gets (root
, "Pages");
1002 fz_throw (doc
->ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
1004 state
.pdflut
.idx
= 0;
1005 pdf_collect_pages (doc
, node
);
1009 static void initpdims (void)
1013 fz_rect rootmediabox
;
1014 int pageno
, trim
, show
;
1015 int trimw
= 0, cxcount
;
1019 if (state
.trimmargins
&& state
.trimcachepath
) {
1020 trimf
= fopen (state
.trimcachepath
, "rb");
1022 trimf
= fopen (state
.trimcachepath
, "wb");
1027 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1028 cxcount
= state
.pagecount
;
1030 cxcount
= MIN (state
.pagecount
, 1);
1032 if (state
.type
== DPDF
) {
1034 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1035 "Root/Pages/MediaBox");
1036 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1039 #ifdef CACHE_PAGEREFS
1040 if (state
.type
== DPDF
1041 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1042 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
1043 if (!state
.pdflut
.objs
) {
1044 err (1, "malloc pageobjs %zu %d %zu failed",
1045 sizeof (*state
.pdflut
.objs
), cxcount
,
1046 sizeof (*state
.pdflut
.objs
) * cxcount
);
1048 state
.pdflut
.count
= cxcount
;
1049 pdf_load_page_objs (state
.u
.pdf
);
1050 state
.pdflut
.pdf
= state
.u
.pdf
;
1054 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1059 switch (state
.type
) {
1061 pdf_document
*pdf
= state
.u
.pdf
;
1062 pdf_obj
*pageref
, *pageobj
;
1064 #ifdef CACHE_PAGEREFS
1065 pageref
= state
.pdflut
.objs
[pageno
];
1067 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1068 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1070 printd ("progress %f Gathering dimensions %d",
1071 (double) (pageno
) / state
.pagecount
,
1075 pageobj
= pdf_resolve_indirect (pageref
);
1077 if (state
.trimmargins
) {
1081 fz_try (state
.ctx
) {
1082 page
= pdf_load_page (pdf
, pageno
);
1083 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1084 trim
= state
.trimanew
|| !obj
;
1090 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1091 dev
->hints
|= FZ_IGNORE_SHADE
;
1092 fz_invert_matrix (&ctm
, &page
->ctm
);
1093 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1094 fz_free_device (dev
);
1096 rect
.x0
+= state
.trimfuzz
.x0
;
1097 rect
.x1
+= state
.trimfuzz
.x1
;
1098 rect
.y0
+= state
.trimfuzz
.y0
;
1099 rect
.y1
+= state
.trimfuzz
.y1
;
1100 fz_transform_rect (&rect
, &ctm
);
1101 fz_intersect_rect (&rect
, &page
->mediabox
);
1103 if (fz_is_empty_rect (&rect
)) {
1104 mediabox
= page
->mediabox
;
1110 obj
= pdf_new_array (pdf
, 4);
1111 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1112 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1113 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1114 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1115 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1118 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1119 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1120 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1121 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1124 rotate
= page
->rotate
;
1125 pdf_free_page (pdf
, page
);
1127 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1129 printd ("progress %f Trimming %d",
1130 (double) (pageno
+ 1) / state
.pagecount
,
1134 fz_catch (state
.ctx
) {
1135 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1142 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1144 if (fz_is_empty_rect (&mediabox
)) {
1152 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1154 if (!fz_is_empty_rect (&cropbox
)) {
1159 fz_intersect_rect (&mediabox
, &cropbox
);
1164 if (fz_is_empty_rect (&rootmediabox
)) {
1166 "cannot find page size for page %d\n",
1170 mediabox
= rootmediabox
;
1174 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1183 fz_try (state
.ctx
) {
1184 page
= xps_load_page (state
.u
.xps
, pageno
);
1185 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1187 if (state
.trimmargins
) {
1191 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1192 dev
->hints
|= FZ_IGNORE_SHADE
;
1193 xps_run_page (state
.u
.xps
, page
, dev
,
1194 &fz_identity
, NULL
);
1195 fz_free_device (dev
);
1197 rect
.x0
+= state
.trimfuzz
.x0
;
1198 rect
.x1
+= state
.trimfuzz
.x1
;
1199 rect
.y0
+= state
.trimfuzz
.y0
;
1200 rect
.y1
+= state
.trimfuzz
.y1
;
1201 fz_intersect_rect (&rect
, &mediabox
);
1203 if (!fz_is_empty_rect (&rect
)) {
1207 xps_free_page (state
.u
.xps
, page
);
1209 printd ("progress %f loading %d",
1210 (double) (pageno
+ 1) / state
.pagecount
,
1214 fz_catch (state
.ctx
) {
1226 if (state
.trimmargins
&& trimw
) {
1229 fz_try (state
.ctx
) {
1230 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1231 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1232 cbz_free_page (state
.u
.cbz
, page
);
1233 printd ("progress %f Trimming %d",
1234 (double) (pageno
+ 1) / state
.pagecount
,
1237 fz_catch (state
.ctx
) {
1238 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1241 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1243 err (1, "fwrite trim mediabox");
1249 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1251 err (1, "fread trim mediabox");
1257 fz_try (state
.ctx
) {
1258 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1259 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1260 cbz_free_page (state
.u
.cbz
, page
);
1262 fz_catch (state
.ctx
) {
1263 fprintf (stderr
, "failed to load cbz page\n");
1281 fz_try (state
.ctx
) {
1282 page
= image_load_page (state
.u
.img
, pageno
);
1283 image_bound_page (state
.u
.img
, page
, &mediabox
);
1284 image_free_page (state
.u
.img
, page
);
1286 fz_catch (state
.ctx
) {
1287 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1293 ARSERT (0 && state
.type
);
1296 if (state
.pagedimcount
== 0
1297 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1298 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1301 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1302 state
.pagedims
= realloc (state
.pagedims
, size
);
1303 if (!state
.pagedims
) {
1304 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1305 size
, state
.pagedimcount
+ 1);
1308 p
= &state
.pagedims
[state
.pagedimcount
++];
1310 p
->mediabox
= mediabox
;
1315 if (state
.trimmargins
) {
1316 printd ("progress 1 Trimmed %d pages in %f seconds",
1317 state
.pagecount
, end
- start
);
1320 if (state
.type
== DPDF
)
1321 printd ("progress 1 Processed %d pages in %f seconds",
1322 state
.pagecount
, end
- start
);
1324 printd ("vmsg Processed %d pages in %f seconds",
1325 state
.pagecount
, end
- start
);
1329 if (fclose (trimf
)) {
1335 static void layout (void)
1340 struct pagedim
*p
= p
;
1341 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1343 if (state
.pagedimcount
== 0) return;
1345 switch (state
.fitmodel
) {
1346 case FitProportional
:
1347 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1350 p
= &state
.pagedims
[pindex
];
1351 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1353 fz_transform_rect (&box
, &rm
);
1355 x0
= MIN (box
.x0
, box
.x1
);
1356 x1
= MAX (box
.x0
, box
.x1
);
1359 maxw
= MAX (w
, maxw
);
1360 zoom
= state
.w
/ maxw
;
1372 ARSERT (0 && state
.fitmodel
);
1375 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1379 p
= &state
.pagedims
[pindex
];
1380 fz_rotate (&ctm
, state
.rotate
);
1381 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1383 fz_transform_rect (&box
, &rm
);
1384 w
= box
.x1
- box
.x0
;
1385 switch (state
.fitmodel
) {
1386 case FitProportional
:
1387 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1393 h
= box
.y1
- box
.y0
;
1395 zoom
= MIN (zw
, zh
);
1396 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1405 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1406 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1408 fz_rotate (&rm
, p
->rotate
);
1409 p
->pagebox
= p
->mediabox
;
1410 fz_transform_rect (&p
->pagebox
, &rm
);
1411 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1412 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1416 fz_transform_rect (&rect
, &ctm
);
1417 fz_round_rect (&p
->bounds
, &rect
);
1420 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1421 fz_scale (&sm
, zoom
, -zoom
);
1422 fz_concat (&ctm
, &tm
, &sm
);
1423 fz_concat (&p
->lctm
, &ctm
, &rm
);
1429 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1430 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1431 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1432 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1433 int boundw
= x1
- x0
;
1434 int boundh
= y1
- y0
;
1436 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1437 } while (p
-- != state
.pagedims
);
1441 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1442 desttoanchor (fz_link_dest
*dest
)
1446 struct pagedim
*pdim
= state
.pagedims
;
1451 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1452 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1454 pdim
= &state
.pagedims
[i
];
1456 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1458 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1459 p
.x
= dest
->ld
.gotor
.lt
.x
;
1462 p
.y
= dest
->ld
.gotor
.lt
.y
;
1463 fz_transform_point (&p
, &pdim
->lctm
);
1467 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1468 double x0
, x1
, y0
, y1
;
1470 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1471 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1473 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1474 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1476 a
.n
= dest
->ld
.gotor
.page
;
1481 static void recurse_outline (fz_outline
*outline
, int level
)
1484 switch (outline
->dest
.kind
) {
1487 struct anchor a
= desttoanchor (&outline
->dest
);
1490 printd ("o %d %d %d %d %s",
1491 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1497 printd ("ou %d %" FMT_s
" %s %s", level
,
1498 strlen (outline
->title
), outline
->title
,
1499 outline
->dest
.ld
.uri
.uri
);
1503 printd ("on %d %s", level
, outline
->title
);
1507 printd ("emsg Unhandled outline kind %d for %s\n",
1508 outline
->dest
.kind
, outline
->title
);
1511 if (outline
->down
) {
1512 recurse_outline (outline
->down
, level
+ 1);
1514 outline
= outline
->next
;
1518 static void process_outline (void)
1520 fz_outline
*outline
;
1522 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1524 state
.needoutline
= 0;
1525 switch (state
.type
) {
1527 outline
= pdf_load_outline (state
.u
.pdf
);
1530 outline
= xps_load_outline (state
.u
.xps
);
1537 recurse_outline (outline
, 0);
1538 fz_free_outline (state
.ctx
, outline
);
1542 static char *strofspan (fz_text_span
*span
)
1547 size_t size
= 0, cap
= 80;
1549 p
= malloc (cap
+ 1);
1550 if (!p
) return NULL
;
1552 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1553 int n
= fz_runetochar (utf8
, ch
->c
);
1554 if (size
+ n
> cap
) {
1556 p
= realloc (p
, cap
+ 1);
1557 if (!p
) return NULL
;
1560 memcpy (p
+ size
, utf8
, n
);
1567 static int matchspan (regex_t
*re
, fz_text_page UNUSED_ATTR
*page
,
1568 fz_text_span
*span
, fz_matrix UNUSED_ATTR ctm
,
1569 int stop
, int pageno
, double start
)
1576 fz_point p1
, p2
, p3
, p4
;
1578 p
= strofspan (span
);
1581 ret
= regexec (re
, p
, 1, &rm
, 0);
1584 if (ret
!= REG_NOMATCH
) {
1587 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1588 printd ("msg regexec error `%.*s'",
1589 (int) size
, errbuf
);
1597 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1598 c
+= fz_runelen (span
->text
[a
].c
);
1600 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1601 c
+= fz_runelen (span
->text
[b
].c
);
1604 if (fz_runelen (span
->text
[b
].c
) > 1) {
1608 fz_text_char_bbox (&sb
, span
, a
);
1609 fz_text_char_bbox (&eb
, span
, b
);
1621 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1628 printd ("progress 1 found at %d `%.*s' in %f sec",
1629 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1633 printd ("match %d %d %f %f %f %f %f %f %f %f",
1645 static int compareblocks (const void *l
, const void *r
)
1647 fz_text_block
const *ls
= l
;
1648 fz_text_block
const* rs
= r
;
1649 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1652 /* wishful thinking function */
1653 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1658 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1660 fz_text_sheet
*sheet
;
1661 struct pagedim
*pdim
, *pdimprev
;
1662 int stop
= 0, niters
= 0;
1665 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1669 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1670 if (niters
++ == 5) {
1673 printd ("progress 1 attention requested aborting search at %d",
1678 printd ("progress %f searching in page %d",
1679 (double) (pageno
+ 1) / state
.pagecount
,
1684 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1685 pdim
= &state
.pagedims
[i
];
1686 if (pdim
->pageno
== pageno
) {
1689 if (pdim
->pageno
> pageno
) {
1698 sheet
= fz_new_text_sheet (state
.ctx
);
1699 text
= fz_new_text_page (state
.ctx
);
1700 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1702 switch (state
.type
) {
1705 fz_try (state
.ctx
) {
1706 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1707 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1708 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1709 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1710 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1713 fz_catch (state
.ctx
) {
1714 fz_free_device (tdev
);
1721 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1722 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1726 ARSERT (0 && state
.type
);
1729 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1730 fz_free_device (tdev
);
1732 for (j
= 0; j
< text
->len
; ++j
) {
1735 fz_text_block
*block
;
1737 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1738 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1741 for (k
= 0; k
< block
->len
; ++k
) {
1746 line
= &block
->lines
[k
];
1747 if (line
->bbox
.y0
< y
+ 1) continue;
1750 line
= &block
->lines
[block
->len
- 1 - k
];
1751 if (line
->bbox
.y0
> y
- 1) continue;
1754 for (span
= line
->first_span
; span
; span
= span
->next
) {
1755 switch (matchspan (re
, text
, span
, ctm
,
1756 stop
, pageno
, start
)) {
1758 case 1: stop
= 1; break;
1759 case -1: stop
= 1; goto endloop
;
1774 fz_free_text_page (state
.ctx
, text
);
1775 fz_free_text_sheet (state
.ctx
, sheet
);
1777 state
.freepage (u
.ptr
);
1782 printd ("progress 1 no matches %f sec", end
- start
);
1784 printd ("clearrects");
1787 static void set_tex_params (int colorspace
)
1794 switch (colorspace
) {
1796 state
.texiform
= GL_RGBA8
;
1797 state
.texform
= GL_RGBA
;
1798 state
.texty
= GL_UNSIGNED_BYTE
;
1799 state
.colorspace
= fz_device_rgb (state
.ctx
);
1802 state
.texiform
= GL_RGBA8
;
1803 state
.texform
= GL_BGRA
;
1804 state
.texty
= endianness
.s
> 1
1805 ? GL_UNSIGNED_INT_8_8_8_8
1806 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1807 state
.colorspace
= fz_device_bgr (state
.ctx
);
1810 state
.texiform
= GL_LUMINANCE_ALPHA
;
1811 state
.texform
= GL_LUMINANCE_ALPHA
;
1812 state
.texty
= GL_UNSIGNED_BYTE
;
1813 state
.colorspace
= fz_device_gray (state
.ctx
);
1816 errx (1, "invalid colorspce %d", colorspace
);
1820 static void realloctexts (int texcount
)
1824 if (texcount
== state
.texcount
) return;
1826 if (texcount
< state
.texcount
) {
1827 glDeleteTextures (state
.texcount
- texcount
,
1828 state
.texids
+ texcount
);
1831 size
= texcount
* sizeof (*state
.texids
);
1832 state
.texids
= realloc (state
.texids
, size
);
1833 if (!state
.texids
) {
1834 err (1, "realloc texids %" FMT_s
, size
);
1837 size
= texcount
* sizeof (*state
.texowners
);
1838 state
.texowners
= realloc (state
.texowners
, size
);
1839 if (!state
.texowners
) {
1840 err (1, "realloc texowners %" FMT_s
, size
);
1842 if (texcount
> state
.texcount
) {
1845 glGenTextures (texcount
- state
.texcount
,
1846 state
.texids
+ state
.texcount
);
1847 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1848 state
.texowners
[i
].w
= -1;
1849 state
.texowners
[i
].slice
= NULL
;
1852 state
.texcount
= texcount
;
1856 static char *mbtoutf8 (char *s
)
1862 len
= mbstowcs (NULL
, s
, strlen (s
));
1867 if (len
== (size_t) -1) {
1872 tmp
= malloc (len
* sizeof (wchar_t));
1877 ret
= mbstowcs (tmp
, s
, len
);
1878 if (ret
== (size_t) -1) {
1884 for (i
= 0; i
< ret
; ++i
) {
1885 len
+= fz_runelen (tmp
[i
]);
1888 p
= r
= malloc (len
+ 1);
1894 for (i
= 0; i
< ret
; ++i
) {
1895 p
+= fz_runetochar (p
, tmp
[i
]);
1902 CAMLprim value
ml_mbtoutf8 (value s_v
)
1908 s
= String_val (s_v
);
1914 ret_v
= caml_copy_string (r
);
1920 static void * mainloop (void UNUSED_ATTR
*unused
)
1923 int len
, ret
, oldlen
= 0;
1928 errx (1, "readlen returned 0");
1931 if (oldlen
< len
+ 1) {
1932 p
= realloc (p
, len
+ 1);
1934 err (1, "realloc %d failed", len
+ 1);
1941 if (!strncmp ("open", p
, 4)) {
1942 int wthack
, off
, ok
= 0;
1948 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1950 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1953 filename
= p
+ 5 + off
;
1954 filenamelen
= strlen (filename
);
1955 password
= filename
+ filenamelen
+ 1;
1958 fz_try (state
.ctx
) {
1959 openxref (filename
, password
);
1962 fz_catch (state
.ctx
) {
1963 utf8filename
= mbtoutf8 (filename
);
1964 printd ("msg Could not open %s", utf8filename
);
1974 utf8filename
= mbtoutf8 (filename
);
1975 printd ("msg Opened %s (press h/F1 to get help)",
1977 if (utf8filename
!= filename
) {
1978 free (utf8filename
);
1981 state
.needoutline
= 1;
1984 else if (!strncmp ("cs", p
, 2)) {
1987 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1989 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1992 set_tex_params (colorspace
);
1993 for (i
= 0; i
< state
.texcount
; ++i
) {
1994 state
.texowners
[i
].w
= -1;
1995 state
.texowners
[i
].slice
= NULL
;
1999 else if (!strncmp ("freepage", p
, 8)) {
2002 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2004 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
2008 else if (!strncmp ("freetile", p
, 8)) {
2011 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2013 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
2017 else if (!strncmp ("search", p
, 6)) {
2018 int icase
, pageno
, y
, len2
, forward
;
2022 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2023 &icase
, &pageno
, &y
, &forward
, &len2
);
2025 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2028 pattern
= p
+ 6 + len2
;
2029 ret
= regcomp (&re
, pattern
,
2030 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2035 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2036 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2039 search (&re
, pageno
, y
, forward
);
2043 else if (!strncmp ("geometry", p
, 8)) {
2047 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2049 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2057 for (i
= 0; i
< state
.texcount
; ++i
) {
2058 state
.texowners
[i
].slice
= NULL
;
2061 state
.fitmodel
= fitmodel
;
2066 unlock ("geometry");
2067 printd ("continue %d", state
.pagecount
);
2069 else if (!strncmp ("reqlayout", p
, 9)) {
2072 unsigned int fitmodel
;
2075 ret
= sscanf (p
+ 9, " %d %u %d %n",
2076 &rotate
, &fitmodel
, &h
, &off
);
2078 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2081 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2084 state
.rotate
= rotate
;
2085 state
.fitmodel
= fitmodel
;
2090 nameddest
= p
+ 9 + off
;
2091 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2094 pdf_obj
*needle
, *obj
;
2096 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2097 strlen (nameddest
));
2098 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2100 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2102 a
= desttoanchor (&dest
);
2104 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2107 printd ("emsg failed to parse destination `%s'\n",
2112 printd ("emsg destination `%s' not found\n",
2115 pdf_drop_obj (needle
);
2119 unlock ("reqlayout");
2120 printd ("continue %d", state
.pagecount
);
2122 else if (!strncmp ("page", p
, 4)) {
2127 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2129 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2134 page
= loadpage (pageno
, pindex
);
2138 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2140 else if (!strncmp ("tile", p
, 4)) {
2147 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2148 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2149 SCN_ptr_cast (&data
));
2151 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2156 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2160 printd ("tile %d %d %" FMT_ptr
" %u %f",
2162 FMT_ptr_cast (tile
),
2163 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2166 else if (!strncmp ("trimset", p
, 7)) {
2170 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2171 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2173 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
2176 state
.trimmargins
= trimmargins
;
2177 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2179 state
.trimfuzz
= fuzz
;
2183 else if (!strncmp ("settrim", p
, 7)) {
2187 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2188 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2190 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2194 state
.trimmargins
= trimmargins
;
2195 state
.needoutline
= 1;
2196 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2198 state
.trimfuzz
= fuzz
;
2200 state
.pagedimcount
= 0;
2201 free (state
.pagedims
);
2202 state
.pagedims
= NULL
;
2207 printd ("continue %d", state
.pagecount
);
2209 else if (!strncmp ("sliceh", p
, 6)) {
2212 ret
= sscanf (p
+ 6, " %d", &h
);
2214 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2216 if (h
!= state
.sliceheight
) {
2219 state
.sliceheight
= h
;
2220 for (i
= 0; i
< state
.texcount
; ++i
) {
2221 state
.texowners
[i
].w
= -1;
2222 state
.texowners
[i
].h
= -1;
2223 state
.texowners
[i
].slice
= NULL
;
2227 else if (!strncmp ("interrupt", p
, 9)) {
2228 printd ("vmsg interrupted");
2231 errx (1, "unknown command %.*s", len
, p
);
2237 CAMLprim value
ml_realloctexts (value texcount_v
)
2239 CAMLparam1 (texcount_v
);
2242 if (trylock ("ml_realloctexts")) {
2246 realloctexts (Int_val (texcount_v
));
2248 unlock ("ml_realloctexts");
2251 CAMLreturn (Val_bool (ok
));
2254 static void recti (int x0
, int y0
, int x1
, int y1
)
2256 GLfloat
*v
= state
.vertices
;
2258 glVertexPointer (2, GL_FLOAT
, 0, v
);
2259 v
[0] = x0
; v
[1] = y0
;
2260 v
[2] = x1
; v
[3] = y0
;
2261 v
[4] = x0
; v
[5] = y1
;
2262 v
[6] = x1
; v
[7] = y1
;
2263 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2266 static void showsel (struct page
*page
, int ox
, int oy
)
2272 fz_page_block
*pageb
;
2273 fz_text_block
*block
;
2274 struct mark first
, last
;
2275 unsigned char selcolor
[] = {15,15,15,140};
2277 first
= page
->fmark
;
2280 if (!first
.span
|| !last
.span
) return;
2282 glEnable (GL_BLEND
);
2283 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2284 glColor4ubv (selcolor
);
2286 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2287 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2288 for (pageb
= page
->text
->blocks
;
2289 pageb
< page
->text
->blocks
+ page
->text
->len
;
2291 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2292 block
= pageb
->u
.text
;
2294 for (line
= block
->lines
;
2295 line
< block
->lines
+ block
->len
;
2298 rect
= fz_empty_rect
;
2300 for (span
= line
->first_span
; span
; span
= span
->next
) {
2302 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2307 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2309 j
= MIN (first
.i
, last
.i
);
2310 k
= MAX (first
.i
, last
.i
);
2313 if (span
== first
.span
) {
2317 else if (span
== last
.span
) {
2324 for (i
= j
; i
<= k
; ++i
) {
2326 fz_union_rect (&rect
,
2327 fz_text_char_bbox (&bbox1
, span
, i
));
2329 fz_round_rect (&bbox
, &rect
);
2330 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2337 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2338 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2339 if (span
== last
.span
) {
2342 rect
= fz_empty_rect
;
2348 glDisable (GL_BLEND
);
2353 static void stipplerect (fz_matrix
*m
,
2361 fz_transform_point (p1
, m
);
2362 fz_transform_point (p2
, m
);
2363 fz_transform_point (p3
, m
);
2364 fz_transform_point (p4
, m
);
2370 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2374 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2376 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2377 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2379 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2380 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2382 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2383 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2385 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2386 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2388 glDrawArrays (GL_LINES
, 0, 8);
2391 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2394 fz_matrix ctm
, tm
, pm
;
2395 fz_link
*link
, *links
;
2396 GLfloat
*texcoords
= state
.texcoords
;
2397 GLfloat
*vertices
= state
.vertices
;
2399 switch (page
->type
) {
2401 links
= page
->u
.pdfpage
->links
;
2405 links
= page
->u
.xpspage
->links
;
2412 glEnable (GL_TEXTURE_1D
);
2413 glEnable (GL_BLEND
);
2414 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2415 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2417 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2418 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2419 fz_translate (&tm
, xoff
, yoff
);
2420 pm
= pagectm (page
);
2421 fz_concat (&ctm
, &pm
, &tm
);
2423 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2424 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2426 for (link
= links
; link
; link
= link
->next
) {
2427 fz_point p1
, p2
, p3
, p4
;
2429 p1
.x
= link
->rect
.x0
;
2430 p1
.y
= link
->rect
.y0
;
2432 p2
.x
= link
->rect
.x1
;
2433 p2
.y
= link
->rect
.y0
;
2435 p3
.x
= link
->rect
.x1
;
2436 p3
.y
= link
->rect
.y1
;
2438 p4
.x
= link
->rect
.x0
;
2439 p4
.y
= link
->rect
.y1
;
2441 switch (link
->dest
.kind
) {
2442 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2443 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2444 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2445 default: glColor3ub (0, 0, 0); break;
2447 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2450 for (i
= 0; i
< page
->annotcount
; ++i
) {
2451 fz_point p1
, p2
, p3
, p4
;
2452 struct annot
*annot
= &page
->annots
[i
];
2454 p1
.x
= annot
->bbox
.x0
;
2455 p1
.y
= annot
->bbox
.y0
;
2457 p2
.x
= annot
->bbox
.x1
;
2458 p2
.y
= annot
->bbox
.y0
;
2460 p3
.x
= annot
->bbox
.x1
;
2461 p3
.y
= annot
->bbox
.y1
;
2463 p4
.x
= annot
->bbox
.x0
;
2464 p4
.y
= annot
->bbox
.y1
;
2466 glColor3ub (0, 0, 128);
2467 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2470 glDisable (GL_BLEND
);
2471 glDisable (GL_TEXTURE_1D
);
2474 static int compareslinks (const void *l
, const void *r
)
2476 struct slink
const *ls
= l
;
2477 struct slink
const *rs
= r
;
2478 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2479 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2481 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2484 static void droptext (struct page
*page
)
2487 fz_free_text_page (state
.ctx
, page
->text
);
2490 page
->fmark
.span
= NULL
;
2491 page
->lmark
.span
= NULL
;
2495 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2500 static void dropslinks (struct page
*page
)
2503 free (page
->slinks
);
2504 page
->slinks
= NULL
;
2505 page
->slinkcount
= 0;
2509 static void ensureslinks (struct page
*page
)
2513 size_t slinksize
= sizeof (*page
->slinks
);
2514 fz_link
*link
, *links
;
2516 if (state
.gen
!= page
->sgen
) {
2518 page
->sgen
= state
.gen
;
2520 if (page
->slinks
) return;
2522 switch (page
->type
) {
2524 links
= page
->u
.pdfpage
->links
;
2525 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2527 &state
.pagedims
[page
->pdimno
].tctm
,
2528 &state
.pagedims
[page
->pdimno
].ctm
);
2532 links
= page
->u
.xpspage
->links
;
2533 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2540 for (link
= links
; link
; link
= link
->next
) {
2544 page
->slinkcount
= count
;
2545 page
->slinks
= calloc (count
, slinksize
);
2546 if (!page
->slinks
) {
2547 err (1, "calloc slinks %d", count
);
2550 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2554 fz_transform_rect (&rect
, &ctm
);
2555 page
->slinks
[i
].link
= link
;
2556 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2558 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2562 static void dropanots (struct page
*page
)
2565 free (page
->annots
);
2566 page
->annots
= NULL
;
2567 page
->annotcount
= 0;
2571 static void ensureanots (struct page
*page
)
2575 size_t anotsize
= sizeof (*page
->annots
);
2578 if (state
.gen
!= page
->sgen
) {
2580 page
->sgen
= state
.gen
;
2582 if (page
->annots
) return;
2584 switch (page
->type
) {
2586 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2588 &state
.pagedims
[page
->pdimno
].tctm
,
2589 &state
.pagedims
[page
->pdimno
].ctm
);
2596 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
);
2598 annot
= pdf_next_annot (state
.u
.pdf
, annot
)) {
2603 page
->annotcount
= count
;
2604 page
->annots
= calloc (count
, anotsize
);
2605 if (!page
->annots
) {
2606 err (1, "calloc annots %d", count
);
2609 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
), i
= 0;
2611 annot
= pdf_next_annot (state
.u
.pdf
, annot
), i
++) {
2614 pdf_bound_annot (state
.u
.pdf
, annot
, &rect
);
2615 fz_transform_rect (&rect
, &ctm
);
2616 page
->annots
[i
].annot
= annot
;
2617 fz_round_rect (&page
->annots
[i
].bbox
, &annot
->pagerect
);
2622 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2623 static void fmt_linkn (char *s
, unsigned int u
)
2625 unsigned int len
; unsigned int q
;
2626 unsigned int zma
= 'z' - 'a' + 1;
2628 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2631 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2632 /* handles u == 0 */
2637 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2638 int noff
, char *targ
, int tlen
, int hfsize
)
2642 struct slink
*slink
;
2643 double x0
, y0
, x1
, y1
, w
;
2645 ensureslinks (page
);
2646 glColor3ub (0xc3, 0xb0, 0x91);
2647 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2648 fmt_linkn (buf
, i
+ noff
);
2649 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2650 slink
= &page
->slinks
[i
];
2652 x0
= slink
->bbox
.x0
+ xoff
- 5;
2653 y1
= slink
->bbox
.y0
+ yoff
- 5;
2654 y0
= y1
+ 10 + hfsize
;
2655 w
= measure_string (state
.face
, hfsize
, buf
);
2657 recti (x0
, y0
, x1
, y1
);
2661 glEnable (GL_BLEND
);
2662 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2663 glEnable (GL_TEXTURE_2D
);
2664 glColor3ub (0, 0, 0);
2665 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2666 fmt_linkn (buf
, i
+ noff
);
2667 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2668 slink
= &page
->slinks
[i
];
2670 x0
= slink
->bbox
.x0
+ xoff
;
2671 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2672 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2675 glDisable (GL_TEXTURE_2D
);
2676 glDisable (GL_BLEND
);
2679 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2682 struct slice
*slice1
;
2683 unsigned char *texdata
;
2686 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2687 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2689 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2690 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2691 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2695 int texindex
= state
.texindex
++ % state
.texcount
;
2697 if (state
.texowners
[texindex
].w
== tile
->w
) {
2698 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2702 state
.texowners
[texindex
].h
= slice
->h
;
2706 state
.texowners
[texindex
].h
= slice
->h
;
2709 state
.texowners
[texindex
].w
= tile
->w
;
2710 state
.texowners
[texindex
].slice
= slice
;
2711 slice
->texindex
= texindex
;
2713 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2715 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2719 texdata
= tile
->pixmap
->samples
;
2722 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2734 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2746 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2751 CAMLprim value
ml_begintiles (value unit_v
)
2753 CAMLparam1 (unit_v
);
2754 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2755 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2756 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2757 CAMLreturn (unit_v
);
2760 CAMLprim value
ml_endtiles (value unit_v
)
2762 CAMLparam1 (unit_v
);
2763 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2764 CAMLreturn (unit_v
);
2767 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2769 CAMLparam2 (args_v
, ptr_v
);
2770 int dispx
= Int_val (Field (args_v
, 0));
2771 int dispy
= Int_val (Field (args_v
, 1));
2772 int dispw
= Int_val (Field (args_v
, 2));
2773 int disph
= Int_val (Field (args_v
, 3));
2774 int tilex
= Int_val (Field (args_v
, 4));
2775 int tiley
= Int_val (Field (args_v
, 5));
2776 char *s
= String_val (ptr_v
);
2777 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2778 int slicey
, firstslice
;
2779 struct slice
*slice
;
2780 GLfloat
*texcoords
= state
.texcoords
;
2781 GLfloat
*vertices
= state
.vertices
;
2783 firstslice
= tiley
/ tile
->sliceheight
;
2784 slice
= &tile
->slices
[firstslice
];
2785 slicey
= tiley
% tile
->sliceheight
;
2790 dh
= slice
->h
- slicey
;
2791 dh
= MIN (disph
, dh
);
2792 uploadslice (tile
, slice
);
2794 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2795 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2796 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2797 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2799 vertices
[0] = dispx
; vertices
[1] = dispy
;
2800 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2801 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2802 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2804 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2808 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2811 CAMLreturn (Val_unit
);
2814 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2815 value xoff_v
, value yoff_v
,
2818 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2819 int xoff
= Int_val (xoff_v
);
2820 int yoff
= Int_val (yoff_v
);
2821 int noff
= Int_val (Field (li_v
, 0));
2822 char *targ
= String_val (Field (li_v
, 1));
2823 int tlen
= caml_string_length (Field (li_v
, 1));
2824 int hfsize
= Int_val (Field (li_v
, 2));
2825 char *s
= String_val (ptr_v
);
2826 int hlmask
= Int_val (hlinks_v
);
2827 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2830 /* deal with loadpage failed pages */
2836 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2837 if (trylock ("ml_postprocess")) {
2842 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2843 noff
= page
->slinkcount
;
2845 if (page
->tgen
== state
.gen
) {
2846 showsel (page
, xoff
, yoff
);
2848 unlock ("ml_postprocess");
2851 CAMLreturn (Val_int (noff
));
2854 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2859 const fz_matrix
*tctm
;
2861 if (!page
->annots
) return NULL
;
2863 switch (page
->type
) {
2865 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2866 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2870 tctm
= &fz_identity
;
2880 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2881 fz_invert_matrix (&ctm
, &ctm
);
2882 fz_transform_point (&p
, &ctm
);
2884 for (i
= 0; i
< page
->annotcount
; ++i
) {
2885 struct annot
*a
= &page
->annots
[i
];
2886 if (p
.x
>= a
->annot
->pagerect
.x0
&& p
.x
<= a
->annot
->pagerect
.x1
) {
2887 if (p
.y
>= a
->annot
->pagerect
.y0
&& p
.y
<= a
->annot
->pagerect
.y1
) {
2895 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2899 const fz_matrix
*tctm
;
2900 fz_link
*link
, *links
;
2902 switch (page
->type
) {
2904 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2905 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2906 links
= page
->u
.pdfpage
->links
;
2910 tctm
= &fz_identity
;
2911 links
= page
->u
.xpspage
->links
;
2920 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2921 fz_invert_matrix (&ctm
, &ctm
);
2922 fz_transform_point (&p
, &ctm
);
2924 for (link
= links
; link
; link
= link
->next
) {
2925 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2926 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2934 static void ensuretext (struct page
*page
)
2936 if (state
.gen
!= page
->tgen
) {
2938 page
->tgen
= state
.gen
;
2944 page
->text
= fz_new_text_page (state
.ctx
);
2945 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2946 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2947 ctm
= pagectm (page
);
2948 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2949 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2950 qsort (page
->text
->blocks
, page
->text
->len
,
2951 sizeof (*page
->text
->blocks
), compareblocks
);
2953 fz_free_device (tdev
);
2957 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2959 CAMLparam2 (start_page_v
, dir_v
);
2961 int i
, dir
= Int_val (dir_v
);
2962 int start_page
= Int_val (start_page_v
);
2963 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2965 ret_v
= Val_int (0);
2966 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2970 lock ("ml_findpage_with_links");
2971 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2974 switch (state
.type
) {
2977 pdf_page
*page
= NULL
;
2979 fz_try (state
.ctx
) {
2980 page
= pdf_load_page (state
.u
.pdf
, i
);
2981 found
= !!page
->links
;
2983 fz_catch (state
.ctx
) {
2993 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2994 found
= !!page
->links
;
3000 ARSERT (0 && "invalid document type");
3004 ret_v
= caml_alloc_small (1, 1);
3005 Field (ret_v
, 0) = Val_int (i
);
3010 unlock ("ml_findpage_with_links");
3016 enum { dir_first
, dir_last
};
3017 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
3019 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
3021 CAMLparam2 (ptr_v
, dir_v
);
3022 CAMLlocal2 (ret_v
, pos_v
);
3024 int dirtag
, i
, slinkindex
;
3025 struct slink
*found
= NULL
,*slink
;
3026 char *s
= String_val (ptr_v
);
3028 page
= parse_pointer ("ml_findlink", s
);
3029 ret_v
= Val_int (0);
3030 if (trylock ("ml_findlink")) {
3034 ensureslinks (page
);
3036 if (Is_block (dir_v
)) {
3037 dirtag
= Tag_val (dir_v
);
3039 case dir_first_visible
:
3041 int x0
, y0
, dir
, first_index
, last_index
;
3043 pos_v
= Field (dir_v
, 0);
3044 x0
= Int_val (Field (pos_v
, 0));
3045 y0
= Int_val (Field (pos_v
, 1));
3046 dir
= Int_val (Field (pos_v
, 2));
3051 last_index
= page
->slinkcount
;
3054 first_index
= page
->slinkcount
- 1;
3058 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
3059 slink
= &page
->slinks
[i
];
3060 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
3069 slinkindex
= Int_val (Field (dir_v
, 0));
3070 found
= &page
->slinks
[slinkindex
];
3071 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3072 slink
= &page
->slinks
[i
];
3073 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
3081 slinkindex
= Int_val (Field (dir_v
, 0));
3082 found
= &page
->slinks
[slinkindex
];
3083 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3084 slink
= &page
->slinks
[i
];
3085 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
3093 slinkindex
= Int_val (Field (dir_v
, 0));
3094 found
= &page
->slinks
[slinkindex
];
3095 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3096 slink
= &page
->slinks
[i
];
3097 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
3105 slinkindex
= Int_val (Field (dir_v
, 0));
3106 found
= &page
->slinks
[slinkindex
];
3107 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3108 slink
= &page
->slinks
[i
];
3109 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
3118 dirtag
= Int_val (dir_v
);
3121 found
= page
->slinks
;
3126 found
= page
->slinks
+ (page
->slinkcount
- 1);
3132 ret_v
= caml_alloc_small (2, 1);
3133 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
3136 unlock ("ml_findlink");
3141 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
3142 unamed
, uremote
, uremotedest
, uannot
};
3148 switch (link->dest.kind) { \
3149 case FZ_LINK_GOTO: \
3153 pageno = link->dest.ld.gotor.page; \
3157 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
3158 p.y = link->dest.ld.gotor.lt.y; \
3159 fz_transform_point (&p, &pdim->lctm); \
3160 if (p.y < 0) p.y = 0; \
3162 tup_v = caml_alloc_tuple (2); \
3163 ret_v = caml_alloc_small (1, ugoto); \
3164 Field (tup_v, 0) = Val_int (pageno); \
3165 Field (tup_v, 1) = Val_int (p.y); \
3166 Field (ret_v, 0) = tup_v; \
3171 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3172 ret_v = caml_alloc_small (1, uuri); \
3173 Field (ret_v, 0) = str_v; \
3176 case FZ_LINK_LAUNCH: \
3177 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3178 ret_v = caml_alloc_small (1, ulaunch); \
3179 Field (ret_v, 0) = str_v; \
3182 case FZ_LINK_NAMED: \
3183 str_v = caml_copy_string (link->dest.ld.named.named); \
3184 ret_v = caml_alloc_small (1, unamed); \
3185 Field (ret_v, 0) = str_v; \
3188 case FZ_LINK_GOTOR: \
3192 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3193 pageno = link->dest.ld.gotor.page; \
3194 if (pageno == -1) { \
3195 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3196 rty = uremotedest; \
3199 gr_v = Val_int (pageno); \
3202 tup_v = caml_alloc_tuple (2); \
3203 ret_v = caml_alloc_small (1, rty); \
3204 Field (tup_v, 0) = str_v; \
3205 Field (tup_v, 1) = gr_v; \
3206 Field (ret_v, 0) = tup_v; \
3214 snprintf (buf, sizeof (buf), \
3215 "unhandled link kind %d", link->dest.kind); \
3216 str_v = caml_copy_string (buf); \
3217 ret_v = caml_alloc_small (1, uunexpected); \
3218 Field (ret_v, 0) = str_v; \
3224 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3226 CAMLparam2 (ptr_v
, n_v
);
3227 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3230 struct pagedim
*pdim
;
3231 char *s
= String_val (ptr_v
);
3233 ret_v
= Val_int (0);
3234 if (trylock ("ml_getlink")) {
3238 page
= parse_pointer ("ml_getlink", s
);
3239 ensureslinks (page
);
3240 pdim
= &state
.pagedims
[page
->pdimno
];
3241 link
= page
->slinks
[Int_val (n_v
)].link
;
3244 unlock ("ml_getlink");
3249 CAMLprim value
ml_getlinkcount (value ptr_v
)
3253 char *s
= String_val (ptr_v
);
3255 page
= parse_pointer ("ml_getlinkcount", s
);
3256 CAMLreturn (Val_int (page
->slinkcount
));
3259 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3261 CAMLparam2 (ptr_v
, n_v
);
3264 struct slink
*slink
;
3265 char *s
= String_val (ptr_v
);
3267 page
= parse_pointer ("ml_getlinkrect", s
);
3268 ret_v
= caml_alloc_tuple (4);
3269 if (trylock ("ml_getlinkrect")) {
3270 Field (ret_v
, 0) = Val_int (0);
3271 Field (ret_v
, 1) = Val_int (0);
3272 Field (ret_v
, 2) = Val_int (0);
3273 Field (ret_v
, 3) = Val_int (0);
3276 ensureslinks (page
);
3278 slink
= &page
->slinks
[Int_val (n_v
)];
3279 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3280 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3281 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3282 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3283 unlock ("ml_getlinkrect");
3289 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3291 CAMLparam3 (ptr_v
, x_v
, y_v
);
3292 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3294 struct annot
*annot
;
3296 char *ptr
= String_val (ptr_v
);
3297 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3298 struct pagedim
*pdim
;
3300 ret_v
= Val_int (0);
3301 if (trylock ("ml_whatsunder")) {
3305 page
= parse_pointer ("ml_whatsunder", ptr
);
3306 pdim
= &state
.pagedims
[page
->pdimno
];
3307 x
+= pdim
->bounds
.x0
;
3308 y
+= pdim
->bounds
.y0
;
3310 if (state
.type
== DPDF
) {
3311 annot
= getannot (page
, x
, y
);
3313 str_v
= caml_copy_string (
3314 pdf_annot_contents (state
.u
.pdf
, annot
->annot
)
3316 ret_v
= caml_alloc_small (1, uannot
);
3317 Field (ret_v
, 0) = str_v
;
3322 link
= getlink (page
, x
, y
);
3328 fz_page_block
*pageb
;
3329 fz_text_block
*block
;
3332 for (pageb
= page
->text
->blocks
;
3333 pageb
< page
->text
->blocks
+ page
->text
->len
;
3336 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3337 block
= pageb
->u
.text
;
3340 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3343 for (line
= block
->lines
;
3344 line
< block
->lines
+ block
->len
;
3349 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3352 for (span
= line
->first_span
; span
; span
= span
->next
) {
3356 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3359 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3361 fz_text_char_bbox (&bbox
, span
, charnum
);
3364 if (x
>= b
->x0
&& x
<= b
->x1
3365 && y
>= b
->y0
&& y
<= b
->y1
) {
3366 fz_text_style
*style
= span
->text
->style
;
3368 style
->font
&& style
->font
->name
3370 : "Span has no font name"
3372 FT_FaceRec
*face
= style
->font
->ft_face
;
3373 if (face
&& face
->family_name
) {
3375 char *n1
= face
->family_name
;
3376 size_t l1
= strlen (n1
);
3377 size_t l2
= strlen (n2
);
3379 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3380 s
= malloc (l1
+ l2
+ 2);
3384 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3385 str_v
= caml_copy_string (s
);
3390 if (str_v
== Val_unit
) {
3391 str_v
= caml_copy_string (n2
);
3393 ret_v
= caml_alloc_small (1, utext
);
3394 Field (ret_v
, 0) = str_v
;
3403 unlock ("ml_whatsunder");
3409 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3411 static int uninteresting (int c
)
3413 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3417 CAMLprim value
ml_clearmark (value ptr_v
)
3420 char *s
= String_val (ptr_v
);
3423 if (trylock ("ml_clearmark")) {
3427 page
= parse_pointer ("ml_clearmark", s
);
3428 page
->fmark
.span
= NULL
;
3429 page
->lmark
.span
= NULL
;
3433 unlock ("ml_clearmark");
3435 CAMLreturn (Val_unit
);
3438 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3440 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3445 fz_page_block
*pageb
;
3446 fz_text_block
*block
;
3447 struct pagedim
*pdim
;
3448 int mark
= Int_val (mark_v
);
3449 char *s
= String_val (ptr_v
);
3450 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3452 ret_v
= Val_bool (0);
3453 if (trylock ("ml_markunder")) {
3457 page
= parse_pointer ("ml_markunder", s
);
3458 pdim
= &state
.pagedims
[page
->pdimno
];
3462 if (mark
== mark_page
) {
3464 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3466 for (i
= 0; i
< page
->text
->len
; ++i
) {
3467 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3468 pb1
= &page
->text
->blocks
[i
];
3472 if (!pb1
) goto unlock
;
3474 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3475 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3476 pb2
= &page
->text
->blocks
[i
];
3480 if (!pb2
) goto unlock
;
3482 block
= pb1
->u
.text
;
3485 page
->fmark
.span
= block
->lines
->first_span
;
3487 block
= pb2
->u
.text
;
3488 line
= &block
->lines
[block
->len
- 1];
3489 page
->lmark
.i
= line
->last_span
->len
- 1;
3490 page
->lmark
.span
= line
->last_span
;
3491 ret_v
= Val_bool (1);
3495 x
+= pdim
->bounds
.x0
;
3496 y
+= pdim
->bounds
.y0
;
3498 for (pageb
= page
->text
->blocks
;
3499 pageb
< page
->text
->blocks
+ page
->text
->len
;
3501 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3502 block
= pageb
->u
.text
;
3505 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3508 if (mark
== mark_block
) {
3510 page
->fmark
.span
= block
->lines
->first_span
;
3512 line
= &block
->lines
[block
->len
- 1];
3513 page
->lmark
.i
= line
->last_span
->len
- 1;
3514 page
->lmark
.span
= line
->last_span
;
3515 ret_v
= Val_bool (1);
3519 for (line
= block
->lines
;
3520 line
< block
->lines
+ block
->len
;
3525 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3528 if (mark
== mark_line
) {
3530 page
->fmark
.span
= line
->first_span
;
3532 page
->lmark
.i
= line
->last_span
->len
- 1;
3533 page
->lmark
.span
= line
->last_span
;
3534 ret_v
= Val_bool (1);
3538 for (span
= line
->first_span
; span
; span
= span
->next
) {
3542 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3545 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3547 fz_text_char_bbox (&bbox
, span
, charnum
);
3550 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3552 int charnum2
, charnum3
= -1, charnum4
= -1;
3554 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3556 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3557 if (uninteresting (span
->text
[charnum2
].c
)) {
3558 charnum3
= charnum2
+ 1;
3562 if (charnum3
== -1) charnum3
= 0;
3564 for (charnum2
= charnum
+ 1;
3565 charnum2
< span
->len
;
3567 if (uninteresting (span
->text
[charnum2
].c
)) break;
3568 charnum4
= charnum2
;
3570 if (charnum4
== -1) goto unlock
;
3572 page
->fmark
.i
= charnum3
;
3573 page
->fmark
.span
= span
;
3575 page
->lmark
.i
= charnum4
;
3576 page
->lmark
.span
= span
;
3577 ret_v
= Val_bool (1);
3585 if (!Bool_val (ret_v
)) {
3586 page
->fmark
.span
= NULL
;
3587 page
->lmark
.span
= NULL
;
3591 unlock ("ml_markunder");
3597 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3599 CAMLparam3 (ptr_v
, x_v
, y_v
);
3600 CAMLlocal2 (ret_v
, res_v
);
3603 fz_page_block
*pageb
;
3604 struct pagedim
*pdim
;
3605 char *s
= String_val (ptr_v
);
3606 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3608 ret_v
= Val_int (0);
3609 if (trylock ("ml_rectofblock")) {
3613 page
= parse_pointer ("ml_rectofblock", s
);
3614 pdim
= &state
.pagedims
[page
->pdimno
];
3615 x
+= pdim
->bounds
.x0
;
3616 y
+= pdim
->bounds
.y0
;
3620 for (pageb
= page
->text
->blocks
;
3621 pageb
< page
->text
->blocks
+ page
->text
->len
;
3623 switch (pageb
->type
) {
3624 case FZ_PAGE_BLOCK_TEXT
:
3625 b
= &pageb
->u
.text
->bbox
;
3628 case FZ_PAGE_BLOCK_IMAGE
:
3629 b
= &pageb
->u
.image
->bbox
;
3636 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3641 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3642 ret_v
= caml_alloc_small (1, 1);
3643 Store_double_field (res_v
, 0, b
->x0
);
3644 Store_double_field (res_v
, 1, b
->x1
);
3645 Store_double_field (res_v
, 2, b
->y0
);
3646 Store_double_field (res_v
, 3, b
->y1
);
3647 Field (ret_v
, 0) = res_v
;
3649 unlock ("ml_rectofblock");
3655 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3657 CAMLparam2 (ptr_v
, rect_v
);
3660 struct pagedim
*pdim
;
3661 char *s
= String_val (ptr_v
);
3662 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3664 fz_page_block
*pageb
;
3665 fz_text_block
*block
;
3666 fz_text_span
*span
, *fspan
, *lspan
;
3668 if (trylock ("ml_seltext")) {
3672 page
= parse_pointer ("ml_seltext", s
);
3675 pdim
= &state
.pagedims
[page
->pdimno
];
3676 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3677 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3678 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3679 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3690 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3691 glColor3ub (128, 128, 128);
3692 recti (x0
, y0
, x1
, y1
);
3693 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3697 fspan
= page
->fmark
.span
;
3700 lspan
= page
->lmark
.span
;
3702 for (pageb
= page
->text
->blocks
;
3703 pageb
< page
->text
->blocks
+ page
->text
->len
;
3705 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3706 block
= pageb
->u
.text
;
3707 for (line
= block
->lines
;
3708 line
< block
->lines
+ block
->len
;
3711 for (span
= line
->first_span
; span
; span
= span
->next
) {
3712 for (i
= 0; i
< span
->len
; ++i
) {
3715 fz_text_char_bbox (&b
, span
, i
);
3717 if (x0
>= b
.x0
&& x0
<= b
.x1
3718 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3723 if (x1
>= b
.x0
&& x1
<= b
.x1
3724 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3729 if (0 && selected
) {
3730 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3731 glColor3ub (128, 128, 128);
3732 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3733 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3739 if (x1
< x0
&& fspan
== lspan
) {
3751 page
->fmark
.span
= fspan
;
3754 page
->lmark
.span
= lspan
;
3756 unlock ("ml_seltext");
3759 CAMLreturn (Val_unit
);
3762 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3767 for (i
= a
; i
<= b
; ++i
) {
3768 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3769 ret
= fwrite (buf
, len
, 1, f
);
3772 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3773 len
, ret
, strerror (errno
));
3781 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3783 caml_failwith ("ml_popen not implemented under Cygwin");
3786 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3788 CAMLparam2 (command_v
, fds_v
);
3789 CAMLlocal2 (l_v
, tup_v
);
3792 value earg_v
= Nothing
;
3793 posix_spawnattr_t attr
;
3794 posix_spawn_file_actions_t fa
;
3795 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3797 argv
[2] = String_val (command_v
);
3799 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3800 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3803 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3804 msg
= "posix_spawnattr_init";
3808 #ifdef POSIX_SPAWN_USEVFORK
3809 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3810 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3815 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3818 tup_v
= Field (l_v
, 0);
3819 fd1
= Int_val (Field (tup_v
, 0));
3820 fd2
= Int_val (Field (tup_v
, 1));
3822 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3823 msg
= "posix_spawn_file_actions_addclose";
3829 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3830 msg
= "posix_spawn_file_actions_adddup2";
3837 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3838 msg
= "posix_spawn";
3843 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3844 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3848 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3849 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3854 unix_error (ret
, msg
, earg_v
);
3856 CAMLreturn (Val_unit
);
3860 CAMLprim value
ml_hassel (value ptr_v
)
3865 char *s
= String_val (ptr_v
);
3867 ret_v
= Val_bool (0);
3868 if (trylock ("ml_hassel")) {
3872 page
= parse_pointer ("ml_hassel", s
);
3873 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3874 unlock ("ml_hassel");
3879 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3881 CAMLparam2 (fd_v
, ptr_v
);
3886 fz_page_block
*pageb
;
3887 fz_text_block
*block
;
3888 int fd
= Int_val (fd_v
);
3889 char *s
= String_val (ptr_v
);
3891 if (trylock ("ml_copysel")) {
3895 page
= parse_pointer ("ml_copysel", s
);
3897 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3898 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3902 f
= fdopen (fd
, "w");
3904 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3905 fd
, strerror (errno
));
3909 for (pageb
= page
->text
->blocks
;
3910 pageb
< page
->text
->blocks
+ page
->text
->len
;
3912 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3913 block
= pageb
->u
.text
;
3914 for (line
= block
->lines
;
3915 line
< block
->lines
+ block
->len
;
3919 for (span
= line
->first_span
; span
; span
= span
->next
) {
3922 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3923 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3924 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3927 if (pipespan (f
, span
, a
, b
)) {
3930 if (span
== page
->lmark
.span
) {
3933 if (span
== line
->last_span
) {
3934 if (putc ('\n', f
) == EOF
) {
3936 "failed break line on sel pipe: %s\n",
3947 int ret
= fclose (f
);
3950 if (errno
!= ECHILD
) {
3951 fprintf (stderr
, "failed to close sel pipe: %s\n",
3957 unlock ("ml_copysel");
3962 fprintf (stderr
, "failed to close sel pipe: %s\n",
3966 CAMLreturn (Val_unit
);
3969 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3971 CAMLparam1 (pagedimno_v
);
3973 int pagedimno
= Int_val (pagedimno_v
);
3976 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3977 if (trylock ("ml_getpdimrect")) {
3978 box
= fz_empty_rect
;
3981 box
= state
.pagedims
[pagedimno
].mediabox
;
3982 unlock ("ml_getpdimrect");
3985 Store_double_field (ret_v
, 0, box
.x0
);
3986 Store_double_field (ret_v
, 1, box
.x1
);
3987 Store_double_field (ret_v
, 2, box
.y0
);
3988 Store_double_field (ret_v
, 3, box
.y1
);
3993 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3994 value dw_v
, value cols_v
)
3996 CAMLparam3 (winw_v
, winh_v
, dw_v
);
4002 double winw
= Int_val (winw_v
);
4003 double winh
= Int_val (winh_v
);
4004 double dw
= Int_val (dw_v
);
4005 double cols
= Int_val (cols_v
);
4006 double pw
= 1.0, ph
= 1.0;
4008 if (trylock ("ml_zoom_for_height")) {
4012 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
4013 double w
= p
->pagebox
.x1
/ cols
;
4014 double h
= p
->pagebox
.y1
;
4018 if (state
.fitmodel
!= FitProportional
) pw
= w
;
4020 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
4023 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
4024 unlock ("ml_zoom_for_height");
4026 ret_v
= caml_copy_double (zoom
);
4030 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
4032 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
4034 int pt
= Int_val(pt_v
);
4035 int x
= Int_val (x_v
);
4036 int y
= Int_val (y_v
);
4039 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
4040 ret_v
= caml_copy_double (w
);
4044 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
4046 CAMLparam2 (pt_v
, string_v
);
4048 int pt
= Int_val (pt_v
);
4051 w
= measure_string (state
.face
, pt
, String_val (string_v
));
4052 ret_v
= caml_copy_double (w
);
4056 CAMLprim value
ml_getpagebox (value opaque_v
)
4058 CAMLparam1 (opaque_v
);
4064 char *s
= String_val (opaque_v
);
4065 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
4067 ret_v
= caml_alloc_tuple (4);
4068 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
4069 dev
->hints
|= FZ_IGNORE_SHADE
;
4071 switch (page
->type
) {
4073 ctm
= pagectm (page
);
4074 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
4078 ctm
= pagectm (page
);
4079 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
4083 rect
= fz_infinite_rect
;
4087 fz_free_device (dev
);
4088 fz_round_rect (&bbox
, &rect
);
4089 Field (ret_v
, 0) = Val_int (bbox
.x0
);
4090 Field (ret_v
, 1) = Val_int (bbox
.y0
);
4091 Field (ret_v
, 2) = Val_int (bbox
.x1
);
4092 Field (ret_v
, 3) = Val_int (bbox
.y1
);
4097 CAMLprim value
ml_setaalevel (value level_v
)
4099 CAMLparam1 (level_v
);
4101 state
.aalevel
= Int_val (level_v
);
4102 CAMLreturn (Val_unit
);
4105 #pragma GCC diagnostic push
4106 #pragma GCC diagnostic ignored "-Wvariadic-macros"
4107 #include <X11/Xlib.h>
4108 #pragma GCC diagnostic pop
4116 XVisualInfo
*visual
;
4119 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4121 CAMLparam3 (display_v
, wid_v
, screen_v
);
4122 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4124 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4126 caml_failwith ("XOpenDisplay");
4129 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4131 XCloseDisplay (glx
.dpy
);
4132 caml_failwith ("glXChooseVisual");
4135 glx
.wid
= Int_val (wid_v
);
4136 CAMLreturn (Val_int (glx
.visual
->visualid
));
4139 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4141 CAMLparam1 (unit_v
);
4143 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4145 caml_failwith ("glXCreateContext");
4151 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4152 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4154 caml_failwith ("glXMakeCurrent");
4156 CAMLreturn (Val_unit
);
4159 CAMLprim value
ml_swapb (value unit_v
)
4161 CAMLparam1 (unit_v
);
4162 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4163 CAMLreturn (Val_unit
);
4166 #include "keysym2ucs.c"
4168 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4170 CAMLparam1 (keysym_v
);
4172 KeySym keysym
= Int_val (keysym_v
);
4177 rune
= keysym2ucs (keysym
);
4178 len
= fz_runetochar (buf
, rune
);
4180 str_v
= caml_copy_string (buf
);
4184 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4186 CAMLprim value
ml_platform (value unit_v
)
4188 CAMLparam1 (unit_v
);
4189 CAMLlocal2 (tup_v
, arr_v
);
4190 int platid
= piunknown
;
4193 #if defined __linux__
4195 #elif defined __CYGWIN__
4197 #elif defined __DragonFly__ || defined __FreeBSD__
4198 || defined __OpenBSD__
|| defined __NetBSD__
4200 #elif defined __sun__
4202 #elif defined __APPLE__
4205 if (uname (&buf
)) err (1, "uname");
4207 tup_v
= caml_alloc_tuple (2);
4209 char const *sar
[] = {
4216 arr_v
= caml_copy_string_array (sar
);
4218 Field (tup_v
, 0) = Val_int (platid
);
4219 Field (tup_v
, 1) = arr_v
;
4223 CAMLprim value
ml_cloexec (value fd_v
)
4226 int fd
= Int_val (fd_v
);
4228 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4229 uerror ("fcntl", Nothing
);
4231 CAMLreturn (Val_unit
);
4234 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4236 CAMLparam2 (w_v
, h_v
);
4239 int w
= Int_val (w_v
);
4240 int h
= Int_val (h_v
);
4241 int cs
= Int_val (cs_v
);
4243 if (state
.pbo_usable
) {
4244 pbo
= calloc (sizeof (*pbo
), 1);
4246 err (1, "calloc pbo");
4258 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4261 state
.glGenBuffersARB (1, &pbo
->id
);
4262 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4263 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4264 NULL
, GL_STREAM_DRAW
);
4265 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4267 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4269 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4270 state
.glDeleteBuffersARB (1, &pbo
->id
);
4272 ret_v
= caml_copy_string ("0");
4278 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4280 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4284 err (1, "malloc %d bytes failed", res
+1);
4286 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4288 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4290 ret_v
= caml_copy_string (s
);
4295 ret_v
= caml_copy_string ("0");
4300 CAMLprim value
ml_freepbo (value s_v
)
4303 char *s
= String_val (s_v
);
4304 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4307 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4309 tile
->pbo
->ptr
= NULL
;
4310 tile
->pbo
->size
= -1;
4312 CAMLreturn (Val_unit
);
4315 CAMLprim value
ml_unmappbo (value s_v
)
4318 char *s
= String_val (s_v
);
4319 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4322 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4323 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4324 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4326 tile
->pbo
->ptr
= NULL
;
4327 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4329 CAMLreturn (Val_unit
);
4332 static void setuppbo (void)
4334 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4335 state
.pbo_usable
= GGPA (glBindBufferARB
)
4336 && GGPA (glUnmapBufferARB
)
4337 && GGPA (glMapBufferARB
)
4338 && GGPA (glBufferDataARB
)
4339 && GGPA (glGenBuffersARB
)
4340 && GGPA (glDeleteBuffersARB
);
4344 CAMLprim value
ml_pbo_usable (value unit_v
)
4346 CAMLparam1 (unit_v
);
4347 CAMLreturn (Val_bool (state
.pbo_usable
));
4350 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4352 CAMLparam3 (ptr_v
, x_v
, y_v
);
4353 CAMLlocal2 (ret_v
, tup_v
);
4355 char *s
= String_val (ptr_v
);
4356 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4357 struct pagedim
*pdim
;
4361 page
= parse_pointer ("ml_unproject", s
);
4362 pdim
= &state
.pagedims
[page
->pdimno
];
4364 ret_v
= Val_int (0);
4365 if (trylock ("ml_unproject")) {
4369 switch (page
->type
) {
4371 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4377 p
.x
= x
+ pdim
->bounds
.x0
;
4378 p
.y
= y
+ pdim
->bounds
.y0
;
4380 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4381 fz_invert_matrix (&ctm
, &ctm
);
4382 fz_transform_point (&p
, &ctm
);
4384 tup_v
= caml_alloc_tuple (2);
4385 ret_v
= caml_alloc_small (1, 1);
4386 Field (tup_v
, 0) = Val_int (p
.x
);
4387 Field (tup_v
, 1) = Val_int (p
.y
);
4388 Field (ret_v
, 0) = tup_v
;
4390 unlock ("ml_unproject");
4395 static void makestippletex (void)
4397 const char pixels
[] = "\xff\xff\0\0";
4398 glGenTextures (1, &state
.stid
);
4399 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4400 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4401 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4414 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4416 return caml_copy_string (FZ_VERSION
);
4419 #ifdef USE_FONTCONFIG
4425 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4429 int UNUSED_ATTR needs_exact_metrics
)
4436 FcPattern
*pat
, *pat1
;
4438 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4439 name
, bold
, italic
, needs_exact_metrics
);
4442 fc
.config
= FcInitLoadConfigAndFonts ();
4444 lprintf ("FcInitLoadConfigAndFonts failed\n");
4448 if (!fc
.config
) return NULL
;
4450 size
= strlen (name
);
4451 if (bold
) size
+= sizeof (":bold") - 1;
4452 if (italic
) size
+= sizeof (":italic") - 1;
4455 buf
= malloc (size
);
4457 err (1, "malloc %zu failed", size
);
4461 if (bold
&& italic
) {
4462 strcat (buf
, ":bold:italic");
4465 if (bold
) strcat (buf
, ":bold");
4466 if (italic
) strcat (buf
, ":italic");
4468 for (i
= 0; i
< size
; ++i
) {
4469 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4472 lprintf ("fcbuf=%s\n", buf
);
4473 pat
= FcNameParse ((FcChar8
*) buf
);
4475 printd ("emsg FcNameParse failed\n");
4480 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4481 printd ("emsg FcConfigSubstitute failed\n");
4485 FcDefaultSubstitute (pat
);
4487 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4489 printd ("emsg FcFontMatch failed\n");
4490 FcPatternDestroy (pat
);
4495 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4496 printd ("emsg FcPatternGetString failed\n");
4497 FcPatternDestroy (pat
);
4498 FcPatternDestroy (pat1
);
4504 printd ("emsg name=%s path=%s\n", name
, path
);
4506 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4507 FcPatternDestroy (pat
);
4508 FcPatternDestroy (pat1
);
4514 CAMLprim value
ml_init (value csock_v
, value params_v
)
4516 CAMLparam2 (csock_v
, params_v
);
4517 CAMLlocal2 (trim_v
, fuzz_v
);
4524 struct sigaction sa
;
4526 state
.csock
= Int_val (csock_v
);
4527 state
.rotate
= Int_val (Field (params_v
, 0));
4528 state
.fitmodel
= Int_val (Field (params_v
, 1));
4529 trim_v
= Field (params_v
, 2);
4530 texcount
= Int_val (Field (params_v
, 3));
4531 state
.sliceheight
= Int_val (Field (params_v
, 4));
4532 mustoresize
= Int_val (Field (params_v
, 5));
4533 colorspace
= Int_val (Field (params_v
, 6));
4534 fontpath
= String_val (Field (params_v
, 7));
4536 if (caml_string_length (Field (params_v
, 8)) > 0) {
4537 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4539 if (!state
.trimcachepath
) {
4540 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4544 haspboext
= Bool_val (Field (params_v
, 9));
4546 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4548 #ifdef USE_FONTCONFIG
4549 if (Bool_val (Field (params_v
, 10))) {
4550 fz_install_load_system_font_funcs (
4551 state
.ctx
, fc_load_system_font_func
, NULL
4556 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4557 fuzz_v
= Field (trim_v
, 1);
4558 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4559 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4560 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4561 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4563 set_tex_params (colorspace
);
4566 #ifndef USE_FONTCONFIG
4567 state
.face
= load_font (fontpath
);
4571 char *buf
= fontpath
;
4572 FcPattern
*pat
, *pat1
;
4575 fc
.config
= FcInitLoadConfigAndFonts ();
4577 errx (1, "FcInitLoadConfigAndFonts failed");
4580 pat
= FcNameParse ((FcChar8
*) buf
);
4582 errx (1, "FcNameParse failed");
4585 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4586 errx (1, "FcConfigSubstitute failed");
4588 FcDefaultSubstitute (pat
);
4590 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4592 errx (1, "FcFontMatch failed");
4595 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4596 errx (1, "FcPatternGetString failed");
4599 state
.face
= load_font ((char *) path
);
4600 FcPatternDestroy (pat
);
4601 FcPatternDestroy (pat1
);
4606 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4608 state
.face
= load_builtin_font (base
, len
);
4610 if (!state
.face
) _exit (1);
4612 realloctexts (texcount
);
4621 sa
.sa_handler
= SIG_IGN
;
4622 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4624 sa
.sa_handler
= SIG_DFL
;
4625 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4627 if (sigemptyset (&sa
.sa_mask
)) {
4628 err (1, "sigemptyset");
4630 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4631 err (1, "sigaction");
4634 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4636 errx (1, "pthread_create: %s", strerror (ret
));
4639 CAMLreturn (Val_unit
);