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
;
170 enum { DNONE
, DPDF
, DXPS
, DCBZ
, DIMG
};
179 fz_text_sheet
*sheet
;
187 fz_display_list
*dlist
;
189 struct slink
*slinks
;
194 void (*freepage
) (void *);
200 struct pagedim
*pagedims
;
220 fz_colorspace
*colorspace
;
228 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
242 void (*closedoc
) (void);
243 void (*freepage
) (void *);
251 void (*glBindBufferARB
) (GLenum
, GLuint
);
252 GLboolean (*glUnmapBufferARB
) (GLenum
);
253 void *(*glMapBufferARB
) (GLenum
, GLenum
);
254 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
255 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
256 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
258 GLfloat texcoords
[8];
259 GLfloat vertices
[16];
261 #ifdef CACHE_PAGEREFS
277 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
279 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
282 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
284 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
287 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
289 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
290 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
293 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
295 static void lock (const char *cap
)
297 int ret
= pthread_mutex_lock (&mutex
);
299 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
303 static void unlock (const char *cap
)
305 int ret
= pthread_mutex_unlock (&mutex
);
307 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
311 static int trylock (const char *cap
)
313 int ret
= pthread_mutex_trylock (&mutex
);
314 if (ret
&& ret
!= EBUSY
) {
315 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
320 static void *parse_pointer (const char *cap
, const char *s
)
325 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
327 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
332 static double now (void)
336 if (gettimeofday (&tv
, NULL
)) {
337 err (1, "gettimeofday");
339 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
342 static int hasdata (void)
345 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
346 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
350 CAMLprim value
ml_hasdata (value fd_v
)
355 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
356 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
357 CAMLreturn (Val_bool (avail
> 0));
360 static void readdata (void *p
, int size
)
365 n
= read (state
.csock
, p
, size
);
367 if (errno
== EINTR
) goto again
;
368 err (1, "read (req %d, ret %zd)", size
, n
);
371 if (!n
) errx (1, "EOF while reading");
372 errx (1, "read (req %d, ret %zd)", size
, n
);
376 static void writedata (char *p
, int size
)
380 p
[0] = (size
>> 24) & 0xff;
381 p
[1] = (size
>> 16) & 0xff;
382 p
[2] = (size
>> 8) & 0xff;
383 p
[3] = (size
>> 0) & 0xff;
385 n
= write (state
.csock
, p
, size
+ 4);
387 if (!n
) errx (1, "EOF while writing data");
388 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
392 static int readlen (void)
397 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
400 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
408 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
411 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
415 if (len
< size
- 4) {
416 writedata (buf
, len
);
422 err (1, "vsnprintf for `%s' failed", fmt
);
424 buf
= realloc (buf
, size
);
429 static void closepdf (void)
431 #ifdef CACHE_PAGEREFS
432 if (state
.pdflut
.objs
) {
435 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
436 pdf_drop_obj (state
.pdflut
.objs
[i
]);
438 free (state
.pdflut
.objs
);
439 state
.pdflut
.objs
= NULL
;
440 state
.pdflut
.idx
= 0;
445 pdf_close_document (state
.u
.pdf
);
450 static void closexps (void)
453 xps_close_document (state
.u
.xps
);
458 static void closecbz (void)
461 cbz_close_document (state
.u
.cbz
);
466 static void closeimg (void)
469 image_close_document (state
.u
.img
);
474 static void freepdfpage (void *ptr
)
476 pdf_free_page (state
.u
.pdf
, ptr
);
479 static void freeemptyxxxpage (void *ptr
)
484 static void freexpspage (void *ptr
)
486 xps_free_page (state
.u
.xps
, ptr
);
489 static void freecbzpage (void *ptr
)
491 cbz_free_page (state
.u
.cbz
, ptr
);
494 static void freeimgpage (void *ptr
)
496 image_free_page (state
.u
.img
, ptr
);
499 static void openxref (char *filename
, char *password
)
503 for (i
= 0; i
< state
.texcount
; ++i
) {
504 state
.texowners
[i
].w
= -1;
505 state
.texowners
[i
].slice
= NULL
;
508 if (state
.closedoc
) state
.closedoc ();
510 len
= strlen (filename
);
528 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
529 const char *ext
= tbl
[j
].ext
;
530 int len2
= strlen (ext
);
532 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
533 state
.type
= tbl
[j
].type
;
539 if (state
.pagedims
) {
540 free (state
.pagedims
);
541 state
.pagedims
= NULL
;
543 state
.pagedimcount
= 0;
545 fz_set_aa_level (state
.ctx
, state
.aalevel
);
546 switch (state
.type
) {
548 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
549 if (pdf_needs_password (state
.u
.pdf
)) {
550 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
552 errx (1, "invalid password");
555 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
556 state
.closedoc
= closepdf
;
557 state
.freepage
= freepdfpage
;
561 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
562 state
.pagecount
= xps_count_pages (state
.u
.xps
);
563 state
.closedoc
= closexps
;
564 state
.freepage
= freexpspage
;
568 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
569 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
570 state
.closedoc
= closecbz
;
571 state
.freepage
= freecbzpage
;
575 state
.u
.img
= image_open_document (state
.ctx
, filename
);
577 state
.closedoc
= closeimg
;
578 state
.freepage
= freeimgpage
;
583 static void pdfinfo (void)
585 if (state
.type
== DPDF
) {
588 printd ("info PDF version\t%d.%d",
589 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
591 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
595 char *items
[] = { "Title", "Author", "Creator",
596 "Producer", "CreationDate" };
598 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
599 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
600 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
601 if (*s
) printd ("info %s\t%s", items
[i
], s
);
602 fz_free (state
.ctx
, s
);
609 static void unlinktile (struct tile
*tile
)
613 for (i
= 0; i
< tile
->slicecount
; ++i
) {
614 struct slice
*s
= &tile
->slices
[i
];
616 if (s
->texindex
!= -1) {
617 if (state
.texowners
[s
->texindex
].slice
== s
) {
618 state
.texowners
[s
->texindex
].slice
= NULL
;
624 static void freepage (struct page
*page
)
627 fz_free_text_page (state
.ctx
, page
->text
);
630 fz_free_text_sheet (state
.ctx
, page
->sheet
);
635 fz_drop_display_list (state
.ctx
, page
->dlist
);
636 page
->freepage (page
->u
.ptr
);
640 static void freetile (struct tile
*tile
)
645 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
648 fz_drop_pixmap (state
.ctx
, state
.pig
);
650 state
.pig
= tile
->pixmap
;
655 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
664 static int cacheline32bytes
;
666 static void __attribute__ ((constructor
)) clcheck (void)
668 char **envp
= environ
;
673 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
675 cacheline32bytes
= auxv
[1] == 32;
681 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
683 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
684 if (cacheline32bytes
&& size
> 32) {
685 intptr_t a1
, a2
, diff
;
687 vector
unsigned char v
= vec_splat_u8 (-1);
688 vector
unsigned char *p
;
690 a1
= a2
= (intptr_t) pixmap
->samples
;
691 a2
= (a1
+ 31) & ~31;
696 while (a1
!= a2
) *(char *) a1
++ = 0xff;
697 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
698 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
700 vec_st (v
, i
+ 16, p
);
702 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
704 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
707 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
710 static void trimctm (pdf_page
*page
, int pindex
)
713 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
715 if (!pdim
->tctmready
) {
716 if (state
.trimmargins
) {
718 fz_matrix rm
, sm
, tm
, im
, ctm1
;
720 fz_rotate (&rm
, -pdim
->rotate
);
721 fz_scale (&sm
, 1, -1);
722 fz_concat (&ctm
, &rm
, &sm
);
723 realbox
= pdim
->mediabox
;
724 fz_transform_rect (&realbox
, &ctm
);
725 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
726 fz_concat (&ctm1
, &ctm
, &tm
);
727 fz_invert_matrix (&im
, &page
->ctm
);
728 fz_concat (&ctm
, &im
, &ctm1
);
738 static fz_matrix
pagectm (struct page
*page
)
742 if (page
->type
== DPDF
) {
743 trimctm (page
->u
.pdfpage
, page
->pdimno
);
745 &state
.pagedims
[page
->pdimno
].tctm
,
746 &state
.pagedims
[page
->pdimno
].ctm
);
749 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
751 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
752 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
757 static void *loadpage (int pageno
, int pindex
)
762 page
= calloc (sizeof (struct page
), 1);
764 err (1, "calloc page %d", pageno
);
767 page
->dlist
= fz_new_display_list (state
.ctx
);
768 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
770 switch (state
.type
) {
772 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
773 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
775 page
->freepage
= freepdfpage
;
779 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
780 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
782 page
->freepage
= freexpspage
;
786 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
787 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
789 page
->freepage
= freecbzpage
;
793 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
794 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
796 page
->freepage
= freeimgpage
;
800 fz_catch (state
.ctx
) {
802 page
->freepage
= freeemptyxxxpage
;
804 fz_free_device (dev
);
806 page
->pdimno
= pindex
;
807 page
->pageno
= pageno
;
808 page
->sgen
= state
.gen
;
809 page
->tgen
= state
.gen
;
810 page
->type
= state
.type
;
815 static struct tile
*alloctile (int h
)
822 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
823 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
824 tile
= calloc (tilesize
, 1);
826 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
828 for (i
= 0; i
< slicecount
; ++i
) {
829 int sh
= MIN (h
, state
.sliceheight
);
830 tile
->slices
[i
].h
= sh
;
831 tile
->slices
[i
].texindex
= -1;
834 tile
->slicecount
= slicecount
;
835 tile
->sliceheight
= state
.sliceheight
;
845 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
846 const fz_matrix
*ctm
, float alpha
)
848 struct obs
*obs
= dev
->user
;
850 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
852 fz_rect rect
= fz_unit_rect
;
854 fz_transform_rect (&rect
, ctm
);
855 fz_round_rect (&b
, &rect
);
856 fz_intersect_irect (&b
, &obs
->b
);
857 obs
->cured
= b
.x0
== obs
->b
.x0
860 && b
.y1
== obs
->b
.y1
;
864 static int obscured (struct page
*page
, fz_irect bbox
)
871 memset (&dev
, 0, sizeof (dev
));
872 memset (&obs
, 0, sizeof (obs
));
877 dev
.fill_image
= obs_fill_image
;
879 fz_rect_from_irect (&rect
, &bbox
);
880 ctm
= pagectm (page
);
881 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
884 #define OBSCURED obscured
886 #define OBSCURED(a, b) 0
889 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
897 struct pagedim
*pdim
;
899 tile
= alloctile (h
);
900 pdim
= &state
.pagedims
[page
->pdimno
];
905 bbox
.x1
= bbox
.x0
+ w
;
906 bbox
.y1
= bbox
.y0
+ h
;
909 if (state
.pig
->w
== w
911 && state
.pig
->colorspace
== state
.colorspace
) {
912 tile
->pixmap
= state
.pig
;
913 tile
->pixmap
->x
= bbox
.x0
;
914 tile
->pixmap
->y
= bbox
.y0
;
917 fz_drop_pixmap (state
.ctx
, state
.pig
);
924 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
930 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
936 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
937 clearpixmap (tile
->pixmap
);
939 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
940 ctm
= pagectm (page
);
941 fz_rect_from_irect (&rect
, &bbox
);
942 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
943 fz_free_device (dev
);
948 #ifdef CACHE_PAGEREFS
949 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
950 thanks to Robin Watts */
952 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
954 fz_context
*ctx
= doc
->ctx
;
958 if (state
.pdflut
.idx
== state
.pagecount
) return;
960 kids
= pdf_dict_gets (node
, "Kids");
961 len
= pdf_array_len (kids
);
964 fz_throw (ctx
, FZ_ERROR_GENERIC
, "Malformed pages tree");
966 if (pdf_mark_obj (node
))
967 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
968 for (i
= 0; i
< len
; i
++) {
969 pdf_obj
*kid
= pdf_array_get (kids
, i
);
970 char *type
= pdf_to_name (pdf_dict_gets (kid
, "Type"));
972 ? !strcmp (type
, "Pages")
973 : pdf_dict_gets (kid
, "Kids")
974 && !pdf_dict_gets (kid
, "MediaBox")) {
975 pdf_collect_pages (doc
, kid
);
979 ? strcmp (type
, "Page") != 0
980 : !pdf_dict_gets (kid
, "MediaBox"))
981 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
982 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (kid
);
985 pdf_unmark_obj (node
);
989 pdf_load_page_objs (pdf_document
*doc
)
991 pdf_obj
*root
= pdf_dict_gets (pdf_trailer (doc
), "Root");
992 pdf_obj
*node
= pdf_dict_gets (root
, "Pages");
995 fz_throw (doc
->ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
997 state
.pdflut
.idx
= 0;
998 pdf_collect_pages (doc
, node
);
1002 static void initpdims (void)
1006 fz_rect rootmediabox
;
1007 int pageno
, trim
, show
;
1008 int trimw
= 0, cxcount
;
1012 if (state
.trimmargins
&& state
.trimcachepath
) {
1013 trimf
= fopen (state
.trimcachepath
, "rb");
1015 trimf
= fopen (state
.trimcachepath
, "wb");
1020 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1021 cxcount
= state
.pagecount
;
1023 cxcount
= MIN (state
.pagecount
, 1);
1025 if (state
.type
== DPDF
) {
1027 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1028 "Root/Pages/MediaBox");
1029 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1032 #ifdef CACHE_PAGEREFS
1033 if (state
.type
== DPDF
1034 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1035 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
1036 if (!state
.pdflut
.objs
) {
1037 err (1, "malloc pageobjs %zu %d %zu failed",
1038 sizeof (*state
.pdflut
.objs
), cxcount
,
1039 sizeof (*state
.pdflut
.objs
) * cxcount
);
1041 state
.pdflut
.count
= cxcount
;
1042 pdf_load_page_objs (state
.u
.pdf
);
1043 state
.pdflut
.pdf
= state
.u
.pdf
;
1047 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1052 switch (state
.type
) {
1054 pdf_document
*pdf
= state
.u
.pdf
;
1055 pdf_obj
*pageref
, *pageobj
;
1057 #ifdef CACHE_PAGEREFS
1058 pageref
= state
.pdflut
.objs
[pageno
];
1060 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1061 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1063 printd ("progress %f Gathering dimensions %d",
1064 (double) (pageno
) / state
.pagecount
,
1068 pageobj
= pdf_resolve_indirect (pageref
);
1070 if (state
.trimmargins
) {
1074 fz_try (state
.ctx
) {
1075 page
= pdf_load_page (pdf
, pageno
);
1076 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1077 trim
= state
.trimanew
|| !obj
;
1083 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1084 dev
->hints
|= FZ_IGNORE_SHADE
;
1085 fz_invert_matrix (&ctm
, &page
->ctm
);
1086 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1087 fz_free_device (dev
);
1089 rect
.x0
+= state
.trimfuzz
.x0
;
1090 rect
.x1
+= state
.trimfuzz
.x1
;
1091 rect
.y0
+= state
.trimfuzz
.y0
;
1092 rect
.y1
+= state
.trimfuzz
.y1
;
1093 fz_transform_rect (&rect
, &ctm
);
1094 fz_intersect_rect (&rect
, &page
->mediabox
);
1096 if (fz_is_empty_rect (&rect
)) {
1097 mediabox
= page
->mediabox
;
1103 obj
= pdf_new_array (pdf
, 4);
1104 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1105 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1106 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1107 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1108 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1111 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1112 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1113 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1114 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1117 rotate
= page
->rotate
;
1118 pdf_free_page (pdf
, page
);
1120 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1122 printd ("progress %f Trimming %d",
1123 (double) (pageno
+ 1) / state
.pagecount
,
1127 fz_catch (state
.ctx
) {
1128 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1135 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1137 if (fz_is_empty_rect (&mediabox
)) {
1145 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1147 if (!fz_is_empty_rect (&cropbox
)) {
1152 fz_intersect_rect (&mediabox
, &cropbox
);
1157 if (fz_is_empty_rect (&rootmediabox
)) {
1159 "cannot find page size for page %d\n",
1163 mediabox
= rootmediabox
;
1167 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1176 fz_try (state
.ctx
) {
1177 page
= xps_load_page (state
.u
.xps
, pageno
);
1178 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1180 if (state
.trimmargins
) {
1184 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1185 dev
->hints
|= FZ_IGNORE_SHADE
;
1186 xps_run_page (state
.u
.xps
, page
, dev
,
1187 &fz_identity
, NULL
);
1188 fz_free_device (dev
);
1190 rect
.x0
+= state
.trimfuzz
.x0
;
1191 rect
.x1
+= state
.trimfuzz
.x1
;
1192 rect
.y0
+= state
.trimfuzz
.y0
;
1193 rect
.y1
+= state
.trimfuzz
.y1
;
1194 fz_intersect_rect (&rect
, &mediabox
);
1196 if (!fz_is_empty_rect (&rect
)) {
1200 xps_free_page (state
.u
.xps
, page
);
1202 printd ("progress %f loading %d",
1203 (double) (pageno
+ 1) / state
.pagecount
,
1207 fz_catch (state
.ctx
) {
1219 if (state
.trimmargins
&& trimw
) {
1222 fz_try (state
.ctx
) {
1223 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1224 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1225 cbz_free_page (state
.u
.cbz
, page
);
1226 printd ("progress %f Trimming %d",
1227 (double) (pageno
+ 1) / state
.pagecount
,
1230 fz_catch (state
.ctx
) {
1231 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1234 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1236 err (1, "fwrite trim mediabox");
1242 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1244 err (1, "fread trim mediabox");
1250 fz_try (state
.ctx
) {
1251 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1252 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1253 cbz_free_page (state
.u
.cbz
, page
);
1255 fz_catch (state
.ctx
) {
1256 fprintf (stderr
, "failed to load cbz page\n");
1274 fz_try (state
.ctx
) {
1275 page
= image_load_page (state
.u
.img
, pageno
);
1276 image_bound_page (state
.u
.img
, page
, &mediabox
);
1277 image_free_page (state
.u
.img
, page
);
1279 fz_catch (state
.ctx
) {
1280 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1286 ARSERT (0 && state
.type
);
1289 if (state
.pagedimcount
== 0
1290 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1291 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1294 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1295 state
.pagedims
= realloc (state
.pagedims
, size
);
1296 if (!state
.pagedims
) {
1297 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1298 size
, state
.pagedimcount
+ 1);
1301 p
= &state
.pagedims
[state
.pagedimcount
++];
1303 p
->mediabox
= mediabox
;
1308 if (state
.trimmargins
) {
1309 printd ("progress 1 Trimmed %d pages in %f seconds",
1310 state
.pagecount
, end
- start
);
1313 if (state
.type
== DPDF
)
1314 printd ("progress 1 Processed %d pages in %f seconds",
1315 state
.pagecount
, end
- start
);
1317 printd ("vmsg Processed %d pages in %f seconds",
1318 state
.pagecount
, end
- start
);
1322 if (fclose (trimf
)) {
1328 static void layout (void)
1333 struct pagedim
*p
= p
;
1334 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1336 if (state
.pagedimcount
== 0) return;
1338 switch (state
.fitmodel
) {
1339 case FitProportional
:
1340 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1343 p
= &state
.pagedims
[pindex
];
1344 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1346 fz_transform_rect (&box
, &rm
);
1348 x0
= MIN (box
.x0
, box
.x1
);
1349 x1
= MAX (box
.x0
, box
.x1
);
1352 maxw
= MAX (w
, maxw
);
1353 zoom
= state
.w
/ maxw
;
1365 ARSERT (0 && state
.fitmodel
);
1368 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1372 p
= &state
.pagedims
[pindex
];
1373 fz_rotate (&ctm
, state
.rotate
);
1374 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1376 fz_transform_rect (&box
, &rm
);
1377 w
= box
.x1
- box
.x0
;
1378 switch (state
.fitmodel
) {
1379 case FitProportional
:
1380 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1386 h
= box
.y1
- box
.y0
;
1388 zoom
= MIN (zw
, zh
);
1389 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1398 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1399 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1401 fz_rotate (&rm
, p
->rotate
);
1402 p
->pagebox
= p
->mediabox
;
1403 fz_transform_rect (&p
->pagebox
, &rm
);
1404 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1405 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1409 fz_transform_rect (&rect
, &ctm
);
1410 fz_round_rect (&p
->bounds
, &rect
);
1413 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1414 fz_scale (&sm
, zoom
, -zoom
);
1415 fz_concat (&ctm
, &tm
, &sm
);
1416 fz_concat (&p
->lctm
, &ctm
, &rm
);
1422 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1423 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1424 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1425 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1426 int boundw
= x1
- x0
;
1427 int boundh
= y1
- y0
;
1429 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1430 } while (p
-- != state
.pagedims
);
1434 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1435 desttoanchor (fz_link_dest
*dest
)
1439 struct pagedim
*pdim
= state
.pagedims
;
1444 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1445 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1447 pdim
= &state
.pagedims
[i
];
1449 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1451 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1452 p
.x
= dest
->ld
.gotor
.lt
.x
;
1455 p
.y
= dest
->ld
.gotor
.lt
.y
;
1456 fz_transform_point (&p
, &pdim
->lctm
);
1460 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1461 double x0
, x1
, y0
, y1
;
1463 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1464 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1466 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1467 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1469 a
.n
= dest
->ld
.gotor
.page
;
1474 static void recurse_outline (fz_outline
*outline
, int level
)
1477 switch (outline
->dest
.kind
) {
1480 struct anchor a
= desttoanchor (&outline
->dest
);
1483 printd ("o %d %d %d %d %s",
1484 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1490 printd ("ou %d %" FMT_s
" %s %s", level
,
1491 strlen (outline
->title
), outline
->title
,
1492 outline
->dest
.ld
.uri
.uri
);
1496 printd ("on %d %s", level
, outline
->title
);
1500 printd ("emsg Unhandled outline kind %d for %s\n",
1501 outline
->dest
.kind
, outline
->title
);
1504 if (outline
->down
) {
1505 recurse_outline (outline
->down
, level
+ 1);
1507 outline
= outline
->next
;
1511 static void process_outline (void)
1513 fz_outline
*outline
;
1515 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1517 state
.needoutline
= 0;
1518 switch (state
.type
) {
1520 outline
= pdf_load_outline (state
.u
.pdf
);
1523 outline
= xps_load_outline (state
.u
.xps
);
1530 recurse_outline (outline
, 0);
1531 fz_free_outline (state
.ctx
, outline
);
1535 static char *strofspan (fz_text_span
*span
)
1540 size_t size
= 0, cap
= 80;
1542 p
= malloc (cap
+ 1);
1543 if (!p
) return NULL
;
1545 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1546 int n
= fz_runetochar (utf8
, ch
->c
);
1547 if (size
+ n
> cap
) {
1549 p
= realloc (p
, cap
+ 1);
1550 if (!p
) return NULL
;
1553 memcpy (p
+ size
, utf8
, n
);
1560 static int matchspan (regex_t
*re
, fz_text_page UNUSED_ATTR
*page
,
1561 fz_text_span
*span
, fz_matrix UNUSED_ATTR ctm
,
1562 int stop
, int pageno
, double start
)
1569 fz_point p1
, p2
, p3
, p4
;
1571 p
= strofspan (span
);
1574 ret
= regexec (re
, p
, 1, &rm
, 0);
1577 if (ret
!= REG_NOMATCH
) {
1580 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1581 printd ("msg regexec error `%.*s'",
1582 (int) size
, errbuf
);
1590 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1591 c
+= fz_runelen (span
->text
[a
].c
);
1593 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1594 c
+= fz_runelen (span
->text
[b
].c
);
1597 if (fz_runelen (span
->text
[b
].c
) > 1) {
1601 fz_text_char_bbox (&sb
, span
, a
);
1602 fz_text_char_bbox (&eb
, span
, b
);
1614 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1621 printd ("progress 1 found at %d `%.*s' in %f sec",
1622 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1626 printd ("match %d %d %f %f %f %f %f %f %f %f",
1638 static int compareblocks (const void *l
, const void *r
)
1640 fz_text_block
const *ls
= l
;
1641 fz_text_block
const* rs
= r
;
1642 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1645 /* wishful thinking function */
1646 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1651 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1653 fz_text_sheet
*sheet
;
1654 struct pagedim
*pdim
, *pdimprev
;
1655 int stop
= 0, niters
= 0;
1658 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1662 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1663 if (niters
++ == 5) {
1666 printd ("progress 1 attention requested aborting search at %d",
1671 printd ("progress %f searching in page %d",
1672 (double) (pageno
+ 1) / state
.pagecount
,
1677 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1678 pdim
= &state
.pagedims
[i
];
1679 if (pdim
->pageno
== pageno
) {
1682 if (pdim
->pageno
> pageno
) {
1691 sheet
= fz_new_text_sheet (state
.ctx
);
1692 text
= fz_new_text_page (state
.ctx
);
1693 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1695 switch (state
.type
) {
1698 fz_try (state
.ctx
) {
1699 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1700 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1701 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1702 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1703 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1706 fz_catch (state
.ctx
) {
1707 fz_free_device (tdev
);
1714 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1715 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1719 ARSERT (0 && state
.type
);
1722 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1723 fz_free_device (tdev
);
1725 for (j
= 0; j
< text
->len
; ++j
) {
1728 fz_text_block
*block
;
1730 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1731 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1734 for (k
= 0; k
< block
->len
; ++k
) {
1739 line
= &block
->lines
[k
];
1740 if (line
->bbox
.y0
< y
+ 1) continue;
1743 line
= &block
->lines
[block
->len
- 1 - k
];
1744 if (line
->bbox
.y0
> y
- 1) continue;
1747 for (span
= line
->first_span
; span
; span
= span
->next
) {
1748 switch (matchspan (re
, text
, span
, ctm
,
1749 stop
, pageno
, start
)) {
1751 case 1: stop
= 1; break;
1752 case -1: stop
= 1; goto endloop
;
1767 fz_free_text_page (state
.ctx
, text
);
1768 fz_free_text_sheet (state
.ctx
, sheet
);
1770 state
.freepage (u
.ptr
);
1775 printd ("progress 1 no matches %f sec", end
- start
);
1777 printd ("clearrects");
1780 static void set_tex_params (int colorspace
)
1787 switch (colorspace
) {
1789 state
.texiform
= GL_RGBA8
;
1790 state
.texform
= GL_RGBA
;
1791 state
.texty
= GL_UNSIGNED_BYTE
;
1792 state
.colorspace
= fz_device_rgb (state
.ctx
);
1795 state
.texiform
= GL_RGBA8
;
1796 state
.texform
= GL_BGRA
;
1797 state
.texty
= endianness
.s
> 1
1798 ? GL_UNSIGNED_INT_8_8_8_8
1799 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1800 state
.colorspace
= fz_device_bgr (state
.ctx
);
1803 state
.texiform
= GL_LUMINANCE_ALPHA
;
1804 state
.texform
= GL_LUMINANCE_ALPHA
;
1805 state
.texty
= GL_UNSIGNED_BYTE
;
1806 state
.colorspace
= fz_device_gray (state
.ctx
);
1809 errx (1, "invalid colorspce %d", colorspace
);
1813 static void realloctexts (int texcount
)
1817 if (texcount
== state
.texcount
) return;
1819 if (texcount
< state
.texcount
) {
1820 glDeleteTextures (state
.texcount
- texcount
,
1821 state
.texids
+ texcount
);
1824 size
= texcount
* sizeof (*state
.texids
);
1825 state
.texids
= realloc (state
.texids
, size
);
1826 if (!state
.texids
) {
1827 err (1, "realloc texids %" FMT_s
, size
);
1830 size
= texcount
* sizeof (*state
.texowners
);
1831 state
.texowners
= realloc (state
.texowners
, size
);
1832 if (!state
.texowners
) {
1833 err (1, "realloc texowners %" FMT_s
, size
);
1835 if (texcount
> state
.texcount
) {
1838 glGenTextures (texcount
- state
.texcount
,
1839 state
.texids
+ state
.texcount
);
1840 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1841 state
.texowners
[i
].w
= -1;
1842 state
.texowners
[i
].slice
= NULL
;
1845 state
.texcount
= texcount
;
1849 static char *mbtoutf8 (char *s
)
1855 len
= mbstowcs (NULL
, s
, strlen (s
));
1860 if (len
== (size_t) -1) {
1865 tmp
= malloc (len
* sizeof (wchar_t));
1870 ret
= mbstowcs (tmp
, s
, len
);
1871 if (ret
== (size_t) -1) {
1877 for (i
= 0; i
< ret
; ++i
) {
1878 len
+= fz_runelen (tmp
[i
]);
1881 p
= r
= malloc (len
+ 1);
1887 for (i
= 0; i
< ret
; ++i
) {
1888 p
+= fz_runetochar (p
, tmp
[i
]);
1895 CAMLprim value
ml_mbtoutf8 (value s_v
)
1901 s
= String_val (s_v
);
1907 ret_v
= caml_copy_string (r
);
1913 static void * mainloop (void UNUSED_ATTR
*unused
)
1916 int len
, ret
, oldlen
= 0;
1921 errx (1, "readlen returned 0");
1924 if (oldlen
< len
+ 1) {
1925 p
= realloc (p
, len
+ 1);
1927 err (1, "realloc %d failed", len
+ 1);
1934 if (!strncmp ("open", p
, 4)) {
1935 int wthack
, off
, ok
= 0;
1941 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1943 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1946 filename
= p
+ 5 + off
;
1947 filenamelen
= strlen (filename
);
1948 password
= filename
+ filenamelen
+ 1;
1951 fz_try (state
.ctx
) {
1952 openxref (filename
, password
);
1955 fz_catch (state
.ctx
) {
1956 utf8filename
= mbtoutf8 (filename
);
1957 printd ("msg Could not open %s", utf8filename
);
1967 utf8filename
= mbtoutf8 (filename
);
1968 printd ("msg Opened %s (press h/F1 to get help)",
1970 if (utf8filename
!= filename
) {
1971 free (utf8filename
);
1974 state
.needoutline
= 1;
1977 else if (!strncmp ("cs", p
, 2)) {
1980 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1982 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1985 set_tex_params (colorspace
);
1986 for (i
= 0; i
< state
.texcount
; ++i
) {
1987 state
.texowners
[i
].w
= -1;
1988 state
.texowners
[i
].slice
= NULL
;
1992 else if (!strncmp ("freepage", p
, 8)) {
1995 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1997 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
2001 else if (!strncmp ("freetile", p
, 8)) {
2004 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2006 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
2010 else if (!strncmp ("search", p
, 6)) {
2011 int icase
, pageno
, y
, len2
, forward
;
2015 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2016 &icase
, &pageno
, &y
, &forward
, &len2
);
2018 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2021 pattern
= p
+ 6 + len2
;
2022 ret
= regcomp (&re
, pattern
,
2023 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2028 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2029 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2032 search (&re
, pageno
, y
, forward
);
2036 else if (!strncmp ("geometry", p
, 8)) {
2040 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2042 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2050 for (i
= 0; i
< state
.texcount
; ++i
) {
2051 state
.texowners
[i
].slice
= NULL
;
2054 state
.fitmodel
= fitmodel
;
2059 unlock ("geometry");
2060 printd ("continue %d", state
.pagecount
);
2062 else if (!strncmp ("reqlayout", p
, 9)) {
2065 unsigned int fitmodel
;
2068 ret
= sscanf (p
+ 9, " %d %u %d %n",
2069 &rotate
, &fitmodel
, &h
, &off
);
2071 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2074 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2077 state
.rotate
= rotate
;
2078 state
.fitmodel
= fitmodel
;
2083 nameddest
= p
+ 9 + off
;
2084 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2087 pdf_obj
*needle
, *obj
;
2089 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2090 strlen (nameddest
));
2091 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2093 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2095 a
= desttoanchor (&dest
);
2097 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2100 printd ("emsg failed to parse destination `%s'\n",
2105 printd ("emsg destination `%s' not found\n",
2108 pdf_drop_obj (needle
);
2112 unlock ("reqlayout");
2113 printd ("continue %d", state
.pagecount
);
2115 else if (!strncmp ("page", p
, 4)) {
2120 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2122 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2127 page
= loadpage (pageno
, pindex
);
2131 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2133 else if (!strncmp ("tile", p
, 4)) {
2140 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2141 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2142 SCN_ptr_cast (&data
));
2144 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2149 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2153 printd ("tile %d %d %" FMT_ptr
" %u %f",
2155 FMT_ptr_cast (tile
),
2156 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2159 else if (!strncmp ("trimset", p
, 7)) {
2163 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2164 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2166 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
2169 state
.trimmargins
= trimmargins
;
2170 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2172 state
.trimfuzz
= fuzz
;
2176 else if (!strncmp ("settrim", p
, 7)) {
2180 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2181 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2183 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2187 state
.trimmargins
= trimmargins
;
2188 state
.needoutline
= 1;
2189 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2191 state
.trimfuzz
= fuzz
;
2193 state
.pagedimcount
= 0;
2194 free (state
.pagedims
);
2195 state
.pagedims
= NULL
;
2200 printd ("continue %d", state
.pagecount
);
2202 else if (!strncmp ("sliceh", p
, 6)) {
2205 ret
= sscanf (p
+ 6, " %d", &h
);
2207 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2209 if (h
!= state
.sliceheight
) {
2212 state
.sliceheight
= h
;
2213 for (i
= 0; i
< state
.texcount
; ++i
) {
2214 state
.texowners
[i
].w
= -1;
2215 state
.texowners
[i
].h
= -1;
2216 state
.texowners
[i
].slice
= NULL
;
2220 else if (!strncmp ("interrupt", p
, 9)) {
2221 printd ("vmsg interrupted");
2224 errx (1, "unknown command %.*s", len
, p
);
2230 CAMLprim value
ml_realloctexts (value texcount_v
)
2232 CAMLparam1 (texcount_v
);
2235 if (trylock ("ml_realloctexts")) {
2239 realloctexts (Int_val (texcount_v
));
2241 unlock ("ml_realloctexts");
2244 CAMLreturn (Val_bool (ok
));
2247 static void recti (int x0
, int y0
, int x1
, int y1
)
2249 GLfloat
*v
= state
.vertices
;
2251 glVertexPointer (2, GL_FLOAT
, 0, v
);
2252 v
[0] = x0
; v
[1] = y0
;
2253 v
[2] = x1
; v
[3] = y0
;
2254 v
[4] = x0
; v
[5] = y1
;
2255 v
[6] = x1
; v
[7] = y1
;
2256 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2259 static void showsel (struct page
*page
, int ox
, int oy
)
2265 fz_page_block
*pageb
;
2266 fz_text_block
*block
;
2267 struct mark first
, last
;
2268 unsigned char selcolor
[] = {15,15,15,140};
2270 first
= page
->fmark
;
2273 if (!first
.span
|| !last
.span
) return;
2275 glEnable (GL_BLEND
);
2276 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2277 glColor4ubv (selcolor
);
2279 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2280 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2281 for (pageb
= page
->text
->blocks
;
2282 pageb
< page
->text
->blocks
+ page
->text
->len
;
2284 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2285 block
= pageb
->u
.text
;
2287 for (line
= block
->lines
;
2288 line
< block
->lines
+ block
->len
;
2291 rect
= fz_empty_rect
;
2293 for (span
= line
->first_span
; span
; span
= span
->next
) {
2295 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2300 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2302 j
= MIN (first
.i
, last
.i
);
2303 k
= MAX (first
.i
, last
.i
);
2306 if (span
== first
.span
) {
2310 else if (span
== last
.span
) {
2317 for (i
= j
; i
<= k
; ++i
) {
2319 fz_union_rect (&rect
,
2320 fz_text_char_bbox (&bbox1
, span
, i
));
2322 fz_round_rect (&bbox
, &rect
);
2323 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2330 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2331 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2332 if (span
== last
.span
) {
2335 rect
= fz_empty_rect
;
2341 glDisable (GL_BLEND
);
2346 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2348 fz_matrix ctm
, tm
, pm
;
2349 fz_link
*link
, *links
;
2350 GLfloat
*texcoords
= state
.texcoords
;
2351 GLfloat
*vertices
= state
.vertices
;
2353 switch (page
->type
) {
2355 links
= page
->u
.pdfpage
->links
;
2359 links
= page
->u
.xpspage
->links
;
2366 glEnable (GL_TEXTURE_1D
);
2367 glEnable (GL_BLEND
);
2368 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2369 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2371 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2372 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2373 fz_translate (&tm
, xoff
, yoff
);
2374 pm
= pagectm (page
);
2375 fz_concat (&ctm
, &pm
, &tm
);
2377 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2378 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2380 for (link
= links
; link
; link
= link
->next
) {
2381 fz_point p1
, p2
, p3
, p4
;
2383 p1
.x
= link
->rect
.x0
;
2384 p1
.y
= link
->rect
.y0
;
2386 p2
.x
= link
->rect
.x1
;
2387 p2
.y
= link
->rect
.y0
;
2389 p3
.x
= link
->rect
.x1
;
2390 p3
.y
= link
->rect
.y1
;
2392 p4
.x
= link
->rect
.x0
;
2393 p4
.y
= link
->rect
.y1
;
2395 fz_transform_point (&p1
, &ctm
);
2396 fz_transform_point (&p2
, &ctm
);
2397 fz_transform_point (&p3
, &ctm
);
2398 fz_transform_point (&p4
, &ctm
);
2400 switch (link
->dest
.kind
) {
2401 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2402 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2403 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2404 default: glColor3ub (0, 0, 0); break;
2412 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2416 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2418 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2419 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2421 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2422 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2424 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2425 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2427 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2428 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2430 glDrawArrays (GL_LINES
, 0, 8);
2433 glDisable (GL_BLEND
);
2434 glDisable (GL_TEXTURE_1D
);
2437 static int compareslinks (const void *l
, const void *r
)
2439 struct slink
const *ls
= l
;
2440 struct slink
const *rs
= r
;
2441 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2442 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2444 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2447 static void droptext (struct page
*page
)
2450 fz_free_text_page (state
.ctx
, page
->text
);
2453 page
->fmark
.span
= NULL
;
2454 page
->lmark
.span
= NULL
;
2458 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2463 static void dropslinks (struct page
*page
)
2466 free (page
->slinks
);
2467 page
->slinks
= NULL
;
2468 page
->slinkcount
= 0;
2472 static void ensureslinks (struct page
*page
)
2476 size_t slinksize
= sizeof (*page
->slinks
);
2477 fz_link
*link
, *links
;
2479 if (state
.gen
!= page
->sgen
) {
2481 page
->sgen
= state
.gen
;
2483 if (page
->slinks
) return;
2485 switch (page
->type
) {
2487 links
= page
->u
.pdfpage
->links
;
2488 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2490 &state
.pagedims
[page
->pdimno
].tctm
,
2491 &state
.pagedims
[page
->pdimno
].ctm
);
2495 links
= page
->u
.xpspage
->links
;
2496 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2503 for (link
= links
; link
; link
= link
->next
) {
2507 page
->slinkcount
= count
;
2508 page
->slinks
= calloc (count
, slinksize
);
2509 if (!page
->slinks
) {
2510 err (1, "realloc slinks %d", count
);
2513 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2517 fz_transform_rect (&rect
, &ctm
);
2518 page
->slinks
[i
].link
= link
;
2519 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2521 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2525 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2526 static void fmt_linkn (char *s
, unsigned int u
)
2528 unsigned int len
; unsigned int q
;
2529 unsigned int zma
= 'z' - 'a' + 1;
2531 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2534 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2535 /* handles u == 0 */
2540 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2541 int noff
, char *targ
, int tlen
, int hfsize
)
2545 struct slink
*slink
;
2546 double x0
, y0
, x1
, y1
, w
;
2548 ensureslinks (page
);
2549 glColor3ub (0xc3, 0xb0, 0x91);
2550 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2551 fmt_linkn (buf
, i
+ noff
);
2552 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2553 slink
= &page
->slinks
[i
];
2555 x0
= slink
->bbox
.x0
+ xoff
- 5;
2556 y1
= slink
->bbox
.y0
+ yoff
- 5;
2557 y0
= y1
+ 10 + hfsize
;
2558 w
= measure_string (state
.face
, hfsize
, buf
);
2560 recti (x0
, y0
, x1
, y1
);
2564 glEnable (GL_BLEND
);
2565 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2566 glEnable (GL_TEXTURE_2D
);
2567 glColor3ub (0, 0, 0);
2568 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2569 fmt_linkn (buf
, i
+ noff
);
2570 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2571 slink
= &page
->slinks
[i
];
2573 x0
= slink
->bbox
.x0
+ xoff
;
2574 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2575 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2578 glDisable (GL_TEXTURE_2D
);
2579 glDisable (GL_BLEND
);
2582 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2585 struct slice
*slice1
;
2586 unsigned char *texdata
;
2589 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2590 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2592 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2593 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2594 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2598 int texindex
= state
.texindex
++ % state
.texcount
;
2600 if (state
.texowners
[texindex
].w
== tile
->w
) {
2601 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2605 state
.texowners
[texindex
].h
= slice
->h
;
2609 state
.texowners
[texindex
].h
= slice
->h
;
2612 state
.texowners
[texindex
].w
= tile
->w
;
2613 state
.texowners
[texindex
].slice
= slice
;
2614 slice
->texindex
= texindex
;
2616 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2618 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2622 texdata
= tile
->pixmap
->samples
;
2625 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2637 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2649 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2654 CAMLprim value
ml_begintiles (value unit_v
)
2656 CAMLparam1 (unit_v
);
2657 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2658 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2659 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2660 CAMLreturn (unit_v
);
2663 CAMLprim value
ml_endtiles (value unit_v
)
2665 CAMLparam1 (unit_v
);
2666 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2667 CAMLreturn (unit_v
);
2670 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2672 CAMLparam2 (args_v
, ptr_v
);
2673 int dispx
= Int_val (Field (args_v
, 0));
2674 int dispy
= Int_val (Field (args_v
, 1));
2675 int dispw
= Int_val (Field (args_v
, 2));
2676 int disph
= Int_val (Field (args_v
, 3));
2677 int tilex
= Int_val (Field (args_v
, 4));
2678 int tiley
= Int_val (Field (args_v
, 5));
2679 char *s
= String_val (ptr_v
);
2680 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2681 int slicey
, firstslice
;
2682 struct slice
*slice
;
2683 GLfloat
*texcoords
= state
.texcoords
;
2684 GLfloat
*vertices
= state
.vertices
;
2686 firstslice
= tiley
/ tile
->sliceheight
;
2687 slice
= &tile
->slices
[firstslice
];
2688 slicey
= tiley
% tile
->sliceheight
;
2693 dh
= slice
->h
- slicey
;
2694 dh
= MIN (disph
, dh
);
2695 uploadslice (tile
, slice
);
2697 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2698 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2699 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2700 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2702 vertices
[0] = dispx
; vertices
[1] = dispy
;
2703 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2704 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2705 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2707 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2711 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2714 CAMLreturn (Val_unit
);
2717 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2718 value xoff_v
, value yoff_v
,
2721 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2722 int xoff
= Int_val (xoff_v
);
2723 int yoff
= Int_val (yoff_v
);
2724 int noff
= Int_val (Field (li_v
, 0));
2725 char *targ
= String_val (Field (li_v
, 1));
2726 int tlen
= caml_string_length (Field (li_v
, 1));
2727 int hfsize
= Int_val (Field (li_v
, 2));
2728 char *s
= String_val (ptr_v
);
2729 int hlmask
= Int_val (hlinks_v
);
2730 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2733 /* deal with loadpage failed pages */
2737 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2738 if (trylock ("ml_postprocess")) {
2743 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2744 noff
= page
->slinkcount
;
2746 if (page
->tgen
== state
.gen
) {
2747 showsel (page
, xoff
, yoff
);
2749 unlock ("ml_postprocess");
2752 CAMLreturn (Val_int (noff
));
2755 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2759 const fz_matrix
*tctm
;
2760 fz_link
*link
, *links
;
2762 switch (page
->type
) {
2764 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2765 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2766 links
= page
->u
.pdfpage
->links
;
2770 tctm
= &fz_identity
;
2771 links
= page
->u
.xpspage
->links
;
2780 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2781 fz_invert_matrix (&ctm
, &ctm
);
2782 fz_transform_point (&p
, &ctm
);
2784 for (link
= links
; link
; link
= link
->next
) {
2785 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2786 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2794 static void ensuretext (struct page
*page
)
2796 if (state
.gen
!= page
->tgen
) {
2798 page
->tgen
= state
.gen
;
2804 page
->text
= fz_new_text_page (state
.ctx
);
2805 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2806 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2807 ctm
= pagectm (page
);
2808 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2809 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2810 qsort (page
->text
->blocks
, page
->text
->len
,
2811 sizeof (*page
->text
->blocks
), compareblocks
);
2813 fz_free_device (tdev
);
2817 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2819 CAMLparam2 (start_page_v
, dir_v
);
2821 int i
, dir
= Int_val (dir_v
);
2822 int start_page
= Int_val (start_page_v
);
2823 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2825 ret_v
= Val_int (0);
2826 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2830 lock ("ml_findpage_with_links");
2831 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2834 switch (state
.type
) {
2837 pdf_page
*page
= NULL
;
2839 fz_try (state
.ctx
) {
2840 page
= pdf_load_page (state
.u
.pdf
, i
);
2841 found
= !!page
->links
;
2843 fz_catch (state
.ctx
) {
2853 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2854 found
= !!page
->links
;
2860 ARSERT (0 && "invalid document type");
2864 ret_v
= caml_alloc_small (1, 1);
2865 Field (ret_v
, 0) = Val_int (i
);
2870 unlock ("ml_findpage_with_links");
2876 enum { dir_first
, dir_last
};
2877 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2879 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2881 CAMLparam2 (ptr_v
, dir_v
);
2882 CAMLlocal2 (ret_v
, pos_v
);
2884 int dirtag
, i
, slinkindex
;
2885 struct slink
*found
= NULL
,*slink
;
2886 char *s
= String_val (ptr_v
);
2888 page
= parse_pointer ("ml_findlink", s
);
2889 ret_v
= Val_int (0);
2890 if (trylock ("ml_findlink")) {
2894 ensureslinks (page
);
2896 if (Is_block (dir_v
)) {
2897 dirtag
= Tag_val (dir_v
);
2899 case dir_first_visible
:
2901 int x0
, y0
, dir
, first_index
, last_index
;
2903 pos_v
= Field (dir_v
, 0);
2904 x0
= Int_val (Field (pos_v
, 0));
2905 y0
= Int_val (Field (pos_v
, 1));
2906 dir
= Int_val (Field (pos_v
, 2));
2911 last_index
= page
->slinkcount
;
2914 first_index
= page
->slinkcount
- 1;
2918 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2919 slink
= &page
->slinks
[i
];
2920 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2929 slinkindex
= Int_val (Field (dir_v
, 0));
2930 found
= &page
->slinks
[slinkindex
];
2931 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2932 slink
= &page
->slinks
[i
];
2933 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2941 slinkindex
= Int_val (Field (dir_v
, 0));
2942 found
= &page
->slinks
[slinkindex
];
2943 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2944 slink
= &page
->slinks
[i
];
2945 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2953 slinkindex
= Int_val (Field (dir_v
, 0));
2954 found
= &page
->slinks
[slinkindex
];
2955 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2956 slink
= &page
->slinks
[i
];
2957 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2965 slinkindex
= Int_val (Field (dir_v
, 0));
2966 found
= &page
->slinks
[slinkindex
];
2967 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2968 slink
= &page
->slinks
[i
];
2969 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2978 dirtag
= Int_val (dir_v
);
2981 found
= page
->slinks
;
2986 found
= page
->slinks
+ (page
->slinkcount
- 1);
2992 ret_v
= caml_alloc_small (2, 1);
2993 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2996 unlock ("ml_findlink");
3001 enum { uuri
, ugoto
, utext
, uunexpected
,
3002 ulaunch
, unamed
, uremote
, uremotedest
};
3008 switch (link->dest.kind) { \
3009 case FZ_LINK_GOTO: \
3013 pageno = link->dest.ld.gotor.page; \
3017 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
3018 p.y = link->dest.ld.gotor.lt.y; \
3019 fz_transform_point (&p, &pdim->lctm); \
3020 if (p.y < 0) p.y = 0; \
3022 tup_v = caml_alloc_tuple (2); \
3023 ret_v = caml_alloc_small (1, ugoto); \
3024 Field (tup_v, 0) = Val_int (pageno); \
3025 Field (tup_v, 1) = Val_int (p.y); \
3026 Field (ret_v, 0) = tup_v; \
3031 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3032 ret_v = caml_alloc_small (1, uuri); \
3033 Field (ret_v, 0) = str_v; \
3036 case FZ_LINK_LAUNCH: \
3037 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3038 ret_v = caml_alloc_small (1, ulaunch); \
3039 Field (ret_v, 0) = str_v; \
3042 case FZ_LINK_NAMED: \
3043 str_v = caml_copy_string (link->dest.ld.named.named); \
3044 ret_v = caml_alloc_small (1, unamed); \
3045 Field (ret_v, 0) = str_v; \
3048 case FZ_LINK_GOTOR: \
3052 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3053 pageno = link->dest.ld.gotor.page; \
3054 if (pageno == -1) { \
3055 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3056 rty = uremotedest; \
3059 gr_v = Val_int (pageno); \
3062 tup_v = caml_alloc_tuple (2); \
3063 ret_v = caml_alloc_small (1, rty); \
3064 Field (tup_v, 0) = str_v; \
3065 Field (tup_v, 1) = gr_v; \
3066 Field (ret_v, 0) = tup_v; \
3074 snprintf (buf, sizeof (buf), \
3075 "unhandled link kind %d", link->dest.kind); \
3076 str_v = caml_copy_string (buf); \
3077 ret_v = caml_alloc_small (1, uunexpected); \
3078 Field (ret_v, 0) = str_v; \
3084 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3086 CAMLparam2 (ptr_v
, n_v
);
3087 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3090 struct pagedim
*pdim
;
3091 char *s
= String_val (ptr_v
);
3093 ret_v
= Val_int (0);
3094 if (trylock ("ml_getlink")) {
3098 page
= parse_pointer ("ml_getlink", s
);
3099 ensureslinks (page
);
3100 pdim
= &state
.pagedims
[page
->pdimno
];
3101 link
= page
->slinks
[Int_val (n_v
)].link
;
3104 unlock ("ml_getlink");
3109 CAMLprim value
ml_getlinkcount (value ptr_v
)
3113 char *s
= String_val (ptr_v
);
3115 page
= parse_pointer ("ml_getlinkcount", s
);
3116 CAMLreturn (Val_int (page
->slinkcount
));
3119 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3121 CAMLparam2 (ptr_v
, n_v
);
3124 struct slink
*slink
;
3125 char *s
= String_val (ptr_v
);
3127 page
= parse_pointer ("ml_getlinkrect", s
);
3128 ret_v
= caml_alloc_tuple (4);
3129 if (trylock ("ml_getlinkrect")) {
3130 Field (ret_v
, 0) = Val_int (0);
3131 Field (ret_v
, 1) = Val_int (0);
3132 Field (ret_v
, 2) = Val_int (0);
3133 Field (ret_v
, 3) = Val_int (0);
3136 ensureslinks (page
);
3138 slink
= &page
->slinks
[Int_val (n_v
)];
3139 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3140 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3141 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3142 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3143 unlock ("ml_getlinkrect");
3149 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3151 CAMLparam3 (ptr_v
, x_v
, y_v
);
3152 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3155 char *ptr
= String_val (ptr_v
);
3156 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3157 struct pagedim
*pdim
;
3159 ret_v
= Val_int (0);
3160 if (trylock ("ml_whatsunder")) {
3164 page
= parse_pointer ("ml_whatsunder", ptr
);
3165 pdim
= &state
.pagedims
[page
->pdimno
];
3166 x
+= pdim
->bounds
.x0
;
3167 y
+= pdim
->bounds
.y0
;
3168 link
= getlink (page
, x
, y
);
3174 fz_page_block
*pageb
;
3175 fz_text_block
*block
;
3178 for (pageb
= page
->text
->blocks
;
3179 pageb
< page
->text
->blocks
+ page
->text
->len
;
3182 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3183 block
= pageb
->u
.text
;
3186 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3189 for (line
= block
->lines
;
3190 line
< block
->lines
+ block
->len
;
3195 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3198 for (span
= line
->first_span
; span
; span
= span
->next
) {
3202 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3205 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3207 fz_text_char_bbox (&bbox
, span
, charnum
);
3210 if (x
>= b
->x0
&& x
<= b
->x1
3211 && y
>= b
->y0
&& y
<= b
->y1
) {
3212 fz_text_style
*style
= span
->text
->style
;
3214 style
->font
&& style
->font
->name
3216 : "Span has no font name"
3218 FT_FaceRec
*face
= style
->font
->ft_face
;
3219 if (face
&& face
->family_name
) {
3221 char *n1
= face
->family_name
;
3222 size_t l1
= strlen (n1
);
3223 size_t l2
= strlen (n2
);
3225 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3226 s
= malloc (l1
+ l2
+ 2);
3230 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3231 str_v
= caml_copy_string (s
);
3236 if (str_v
== Val_unit
) {
3237 str_v
= caml_copy_string (n2
);
3239 ret_v
= caml_alloc_small (1, utext
);
3240 Field (ret_v
, 0) = str_v
;
3249 unlock ("ml_whatsunder");
3255 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3257 static int uninteresting (int c
)
3259 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3263 CAMLprim value
ml_clearmark (value ptr_v
)
3266 char *s
= String_val (ptr_v
);
3269 if (trylock ("ml_clearmark")) {
3273 page
= parse_pointer ("ml_clearmark", s
);
3274 page
->fmark
.span
= NULL
;
3275 page
->lmark
.span
= NULL
;
3279 unlock ("ml_clearmark");
3281 CAMLreturn (Val_unit
);
3284 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3286 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3291 fz_page_block
*pageb
;
3292 fz_text_block
*block
;
3293 struct pagedim
*pdim
;
3294 int mark
= Int_val (mark_v
);
3295 char *s
= String_val (ptr_v
);
3296 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3298 ret_v
= Val_bool (0);
3299 if (trylock ("ml_markunder")) {
3303 page
= parse_pointer ("ml_markunder", s
);
3304 pdim
= &state
.pagedims
[page
->pdimno
];
3308 if (mark
== mark_page
) {
3310 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3312 for (i
= 0; i
< page
->text
->len
; ++i
) {
3313 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3314 pb1
= &page
->text
->blocks
[i
];
3318 if (!pb1
) goto unlock
;
3320 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3321 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3322 pb2
= &page
->text
->blocks
[i
];
3326 if (!pb2
) goto unlock
;
3328 block
= pb1
->u
.text
;
3331 page
->fmark
.span
= block
->lines
->first_span
;
3333 block
= pb2
->u
.text
;
3334 line
= &block
->lines
[block
->len
- 1];
3335 page
->lmark
.i
= line
->last_span
->len
- 1;
3336 page
->lmark
.span
= line
->last_span
;
3337 ret_v
= Val_bool (1);
3341 x
+= pdim
->bounds
.x0
;
3342 y
+= pdim
->bounds
.y0
;
3344 for (pageb
= page
->text
->blocks
;
3345 pageb
< page
->text
->blocks
+ page
->text
->len
;
3347 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3348 block
= pageb
->u
.text
;
3351 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3354 if (mark
== mark_block
) {
3356 page
->fmark
.span
= block
->lines
->first_span
;
3358 line
= &block
->lines
[block
->len
- 1];
3359 page
->lmark
.i
= line
->last_span
->len
- 1;
3360 page
->lmark
.span
= line
->last_span
;
3361 ret_v
= Val_bool (1);
3365 for (line
= block
->lines
;
3366 line
< block
->lines
+ block
->len
;
3371 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3374 if (mark
== mark_line
) {
3376 page
->fmark
.span
= line
->first_span
;
3378 page
->lmark
.i
= line
->last_span
->len
- 1;
3379 page
->lmark
.span
= line
->last_span
;
3380 ret_v
= Val_bool (1);
3384 for (span
= line
->first_span
; span
; span
= span
->next
) {
3388 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3391 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3393 fz_text_char_bbox (&bbox
, span
, charnum
);
3396 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3398 int charnum2
, charnum3
= -1, charnum4
= -1;
3400 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3402 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3403 if (uninteresting (span
->text
[charnum2
].c
)) {
3404 charnum3
= charnum2
+ 1;
3408 if (charnum3
== -1) charnum3
= 0;
3410 for (charnum2
= charnum
+ 1;
3411 charnum2
< span
->len
;
3413 if (uninteresting (span
->text
[charnum2
].c
)) break;
3414 charnum4
= charnum2
;
3416 if (charnum4
== -1) goto unlock
;
3418 page
->fmark
.i
= charnum3
;
3419 page
->fmark
.span
= span
;
3421 page
->lmark
.i
= charnum4
;
3422 page
->lmark
.span
= span
;
3423 ret_v
= Val_bool (1);
3431 if (!Bool_val (ret_v
)) {
3432 page
->fmark
.span
= NULL
;
3433 page
->lmark
.span
= NULL
;
3437 unlock ("ml_markunder");
3443 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3445 CAMLparam3 (ptr_v
, x_v
, y_v
);
3446 CAMLlocal2 (ret_v
, res_v
);
3449 fz_page_block
*pageb
;
3450 struct pagedim
*pdim
;
3451 char *s
= String_val (ptr_v
);
3452 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3454 ret_v
= Val_int (0);
3455 if (trylock ("ml_rectofblock")) {
3459 page
= parse_pointer ("ml_rectofblock", s
);
3460 pdim
= &state
.pagedims
[page
->pdimno
];
3461 x
+= pdim
->bounds
.x0
;
3462 y
+= pdim
->bounds
.y0
;
3466 for (pageb
= page
->text
->blocks
;
3467 pageb
< page
->text
->blocks
+ page
->text
->len
;
3469 switch (pageb
->type
) {
3470 case FZ_PAGE_BLOCK_TEXT
:
3471 b
= &pageb
->u
.text
->bbox
;
3474 case FZ_PAGE_BLOCK_IMAGE
:
3475 b
= &pageb
->u
.image
->bbox
;
3482 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3487 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3488 ret_v
= caml_alloc_small (1, 1);
3489 Store_double_field (res_v
, 0, b
->x0
);
3490 Store_double_field (res_v
, 1, b
->x1
);
3491 Store_double_field (res_v
, 2, b
->y0
);
3492 Store_double_field (res_v
, 3, b
->y1
);
3493 Field (ret_v
, 0) = res_v
;
3495 unlock ("ml_rectofblock");
3501 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3503 CAMLparam2 (ptr_v
, rect_v
);
3506 struct pagedim
*pdim
;
3507 char *s
= String_val (ptr_v
);
3508 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3510 fz_page_block
*pageb
;
3511 fz_text_block
*block
;
3512 fz_text_span
*span
, *fspan
, *lspan
;
3514 if (trylock ("ml_seltext")) {
3518 page
= parse_pointer ("ml_seltext", s
);
3521 pdim
= &state
.pagedims
[page
->pdimno
];
3522 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3523 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3524 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3525 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3536 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3537 glColor3ub (128, 128, 128);
3538 recti (x0
, y0
, x1
, y1
);
3539 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3543 fspan
= page
->fmark
.span
;
3546 lspan
= page
->lmark
.span
;
3548 for (pageb
= page
->text
->blocks
;
3549 pageb
< page
->text
->blocks
+ page
->text
->len
;
3551 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3552 block
= pageb
->u
.text
;
3553 for (line
= block
->lines
;
3554 line
< block
->lines
+ block
->len
;
3557 for (span
= line
->first_span
; span
; span
= span
->next
) {
3558 for (i
= 0; i
< span
->len
; ++i
) {
3561 fz_text_char_bbox (&b
, span
, i
);
3563 if (x0
>= b
.x0
&& x0
<= b
.x1
3564 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3569 if (x1
>= b
.x0
&& x1
<= b
.x1
3570 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3575 if (0 && selected
) {
3576 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3577 glColor3ub (128, 128, 128);
3578 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3579 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3585 if (x1
< x0
&& fspan
== lspan
) {
3597 page
->fmark
.span
= fspan
;
3600 page
->lmark
.span
= lspan
;
3602 unlock ("ml_seltext");
3605 CAMLreturn (Val_unit
);
3608 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3613 for (i
= a
; i
<= b
; ++i
) {
3614 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3615 ret
= fwrite (buf
, len
, 1, f
);
3618 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3619 len
, ret
, strerror (errno
));
3627 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3629 caml_failwith ("ml_popen not implemented under Cygwin");
3632 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3634 CAMLparam2 (command_v
, fds_v
);
3635 CAMLlocal2 (l_v
, tup_v
);
3638 value earg_v
= Nothing
;
3639 posix_spawnattr_t attr
;
3640 posix_spawn_file_actions_t fa
;
3641 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3643 argv
[2] = String_val (command_v
);
3645 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3646 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3649 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3650 msg
= "posix_spawnattr_init";
3654 #ifdef POSIX_SPAWN_USEVFORK
3655 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3656 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3661 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3664 tup_v
= Field (l_v
, 0);
3665 fd1
= Int_val (Field (tup_v
, 0));
3666 fd2
= Int_val (Field (tup_v
, 1));
3668 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3669 msg
= "posix_spawn_file_actions_addclose";
3675 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3676 msg
= "posix_spawn_file_actions_adddup2";
3683 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3684 msg
= "posix_spawn";
3689 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3690 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3694 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3695 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3700 unix_error (ret
, msg
, earg_v
);
3702 CAMLreturn (Val_unit
);
3706 CAMLprim value
ml_hassel (value ptr_v
)
3711 char *s
= String_val (ptr_v
);
3713 ret_v
= Val_bool (0);
3714 if (trylock ("ml_hassel")) {
3718 page
= parse_pointer ("ml_hassel", s
);
3719 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3720 unlock ("ml_hassel");
3725 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3727 CAMLparam2 (fd_v
, ptr_v
);
3732 fz_page_block
*pageb
;
3733 fz_text_block
*block
;
3734 int fd
= Int_val (fd_v
);
3735 char *s
= String_val (ptr_v
);
3737 if (trylock ("ml_copysel")) {
3741 page
= parse_pointer ("ml_copysel", s
);
3743 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3744 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3748 f
= fdopen (fd
, "w");
3750 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3751 fd
, strerror (errno
));
3755 for (pageb
= page
->text
->blocks
;
3756 pageb
< page
->text
->blocks
+ page
->text
->len
;
3758 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3759 block
= pageb
->u
.text
;
3760 for (line
= block
->lines
;
3761 line
< block
->lines
+ block
->len
;
3765 for (span
= line
->first_span
; span
; span
= span
->next
) {
3768 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3769 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3770 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3773 if (pipespan (f
, span
, a
, b
)) {
3776 if (span
== page
->lmark
.span
) {
3779 if (span
== line
->last_span
) {
3780 if (putc ('\n', f
) == EOF
) {
3782 "failed break line on sel pipe: %s\n",
3793 int ret
= fclose (f
);
3796 if (errno
!= ECHILD
) {
3797 fprintf (stderr
, "failed to close sel pipe: %s\n",
3803 unlock ("ml_copysel");
3808 fprintf (stderr
, "failed to close sel pipe: %s\n",
3812 CAMLreturn (Val_unit
);
3815 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3817 CAMLparam1 (pagedimno_v
);
3819 int pagedimno
= Int_val (pagedimno_v
);
3822 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3823 if (trylock ("ml_getpdimrect")) {
3824 box
= fz_empty_rect
;
3827 box
= state
.pagedims
[pagedimno
].mediabox
;
3828 unlock ("ml_getpdimrect");
3831 Store_double_field (ret_v
, 0, box
.x0
);
3832 Store_double_field (ret_v
, 1, box
.x1
);
3833 Store_double_field (ret_v
, 2, box
.y0
);
3834 Store_double_field (ret_v
, 3, box
.y1
);
3839 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3840 value dw_v
, value cols_v
)
3842 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3848 double winw
= Int_val (winw_v
);
3849 double winh
= Int_val (winh_v
);
3850 double dw
= Int_val (dw_v
);
3851 double cols
= Int_val (cols_v
);
3852 double pw
= 1.0, ph
= 1.0;
3854 if (trylock ("ml_zoom_for_height")) {
3858 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3859 double w
= p
->pagebox
.x1
/ cols
;
3860 double h
= p
->pagebox
.y1
;
3864 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3866 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3869 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3870 unlock ("ml_zoom_for_height");
3872 ret_v
= caml_copy_double (zoom
);
3876 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3878 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3880 int pt
= Int_val(pt_v
);
3881 int x
= Int_val (x_v
);
3882 int y
= Int_val (y_v
);
3885 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3886 ret_v
= caml_copy_double (w
);
3890 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3892 CAMLparam2 (pt_v
, string_v
);
3894 int pt
= Int_val (pt_v
);
3897 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3898 ret_v
= caml_copy_double (w
);
3902 CAMLprim value
ml_getpagebox (value opaque_v
)
3904 CAMLparam1 (opaque_v
);
3910 char *s
= String_val (opaque_v
);
3911 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3913 ret_v
= caml_alloc_tuple (4);
3914 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3915 dev
->hints
|= FZ_IGNORE_SHADE
;
3917 switch (page
->type
) {
3919 ctm
= pagectm (page
);
3920 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3924 ctm
= pagectm (page
);
3925 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3929 rect
= fz_infinite_rect
;
3933 fz_free_device (dev
);
3934 fz_round_rect (&bbox
, &rect
);
3935 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3936 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3937 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3938 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3943 CAMLprim value
ml_setaalevel (value level_v
)
3945 CAMLparam1 (level_v
);
3947 state
.aalevel
= Int_val (level_v
);
3948 CAMLreturn (Val_unit
);
3951 #pragma GCC diagnostic push
3952 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3953 #include <X11/Xlib.h>
3954 #pragma GCC diagnostic pop
3962 XVisualInfo
*visual
;
3965 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3967 CAMLparam3 (display_v
, wid_v
, screen_v
);
3968 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3970 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3972 caml_failwith ("XOpenDisplay");
3975 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3977 XCloseDisplay (glx
.dpy
);
3978 caml_failwith ("glXChooseVisual");
3981 glx
.wid
= Int_val (wid_v
);
3982 CAMLreturn (Val_int (glx
.visual
->visualid
));
3985 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3987 CAMLparam1 (unit_v
);
3989 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3991 caml_failwith ("glXCreateContext");
3997 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3998 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4000 caml_failwith ("glXMakeCurrent");
4002 CAMLreturn (Val_unit
);
4005 CAMLprim value
ml_swapb (value unit_v
)
4007 CAMLparam1 (unit_v
);
4008 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4009 CAMLreturn (Val_unit
);
4012 #include "keysym2ucs.c"
4014 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4016 CAMLparam1 (keysym_v
);
4018 KeySym keysym
= Int_val (keysym_v
);
4023 rune
= keysym2ucs (keysym
);
4024 len
= fz_runetochar (buf
, rune
);
4026 str_v
= caml_copy_string (buf
);
4030 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4032 CAMLprim value
ml_platform (value unit_v
)
4034 CAMLparam1 (unit_v
);
4035 CAMLlocal2 (tup_v
, arr_v
);
4036 int platid
= piunknown
;
4039 #if defined __linux__
4041 #elif defined __CYGWIN__
4043 #elif defined __DragonFly__ || defined __FreeBSD__
4044 || defined __OpenBSD__
|| defined __NetBSD__
4046 #elif defined __sun__
4048 #elif defined __APPLE__
4051 if (uname (&buf
)) err (1, "uname");
4053 tup_v
= caml_alloc_tuple (2);
4055 char const *sar
[] = {
4062 arr_v
= caml_copy_string_array (sar
);
4064 Field (tup_v
, 0) = Val_int (platid
);
4065 Field (tup_v
, 1) = arr_v
;
4069 CAMLprim value
ml_cloexec (value fd_v
)
4072 int fd
= Int_val (fd_v
);
4074 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4075 uerror ("fcntl", Nothing
);
4077 CAMLreturn (Val_unit
);
4080 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4082 CAMLparam2 (w_v
, h_v
);
4085 int w
= Int_val (w_v
);
4086 int h
= Int_val (h_v
);
4087 int cs
= Int_val (cs_v
);
4089 if (state
.pbo_usable
) {
4090 pbo
= calloc (sizeof (*pbo
), 1);
4092 err (1, "calloc pbo");
4104 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4107 state
.glGenBuffersARB (1, &pbo
->id
);
4108 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4109 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4110 NULL
, GL_STREAM_DRAW
);
4111 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4113 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4115 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4116 state
.glDeleteBuffersARB (1, &pbo
->id
);
4118 ret_v
= caml_copy_string ("0");
4124 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4126 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4130 err (1, "malloc %d bytes failed", res
+1);
4132 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4134 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4136 ret_v
= caml_copy_string (s
);
4141 ret_v
= caml_copy_string ("0");
4146 CAMLprim value
ml_freepbo (value s_v
)
4149 char *s
= String_val (s_v
);
4150 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4153 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4155 tile
->pbo
->ptr
= NULL
;
4156 tile
->pbo
->size
= -1;
4158 CAMLreturn (Val_unit
);
4161 CAMLprim value
ml_unmappbo (value s_v
)
4164 char *s
= String_val (s_v
);
4165 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4168 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4169 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4170 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4172 tile
->pbo
->ptr
= NULL
;
4173 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4175 CAMLreturn (Val_unit
);
4178 static void setuppbo (void)
4180 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4181 state
.pbo_usable
= GGPA (glBindBufferARB
)
4182 && GGPA (glUnmapBufferARB
)
4183 && GGPA (glMapBufferARB
)
4184 && GGPA (glBufferDataARB
)
4185 && GGPA (glGenBuffersARB
)
4186 && GGPA (glDeleteBuffersARB
);
4190 CAMLprim value
ml_pbo_usable (value unit_v
)
4192 CAMLparam1 (unit_v
);
4193 CAMLreturn (Val_bool (state
.pbo_usable
));
4196 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4198 CAMLparam3 (ptr_v
, x_v
, y_v
);
4199 CAMLlocal2 (ret_v
, tup_v
);
4201 char *s
= String_val (ptr_v
);
4202 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4203 struct pagedim
*pdim
;
4207 page
= parse_pointer ("ml_unproject", s
);
4208 pdim
= &state
.pagedims
[page
->pdimno
];
4210 ret_v
= Val_int (0);
4211 if (trylock ("ml_unproject")) {
4215 switch (page
->type
) {
4217 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4223 p
.x
= x
+ pdim
->bounds
.x0
;
4224 p
.y
= y
+ pdim
->bounds
.y0
;
4226 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4227 fz_invert_matrix (&ctm
, &ctm
);
4228 fz_transform_point (&p
, &ctm
);
4230 tup_v
= caml_alloc_tuple (2);
4231 ret_v
= caml_alloc_small (1, 1);
4232 Field (tup_v
, 0) = Val_int (p
.x
);
4233 Field (tup_v
, 1) = Val_int (p
.y
);
4234 Field (ret_v
, 0) = tup_v
;
4236 unlock ("ml_unproject");
4241 static void makestippletex (void)
4243 const char pixels
[] = "\xff\xff\0\0";
4244 glGenTextures (1, &state
.stid
);
4245 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4246 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4247 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4260 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4262 return caml_copy_string (FZ_VERSION
);
4265 #ifdef USE_FONTCONFIG
4271 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4275 int UNUSED_ATTR needs_exact_metrics
)
4282 FcPattern
*pat
, *pat1
;
4284 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4285 name
, bold
, italic
, needs_exact_metrics
);
4288 fc
.config
= FcInitLoadConfigAndFonts ();
4290 lprintf ("FcInitLoadConfigAndFonts failed\n");
4294 if (!fc
.config
) return NULL
;
4296 size
= strlen (name
);
4297 if (bold
) size
+= sizeof (":bold") - 1;
4298 if (italic
) size
+= sizeof (":italic") - 1;
4301 buf
= malloc (size
);
4303 err (1, "malloc %zu failed", size
);
4307 if (bold
&& italic
) {
4308 strcat (buf
, ":bold:italic");
4311 if (bold
) strcat (buf
, ":bold");
4312 if (italic
) strcat (buf
, ":italic");
4314 for (i
= 0; i
< size
; ++i
) {
4315 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4318 lprintf ("fcbuf=%s\n", buf
);
4319 pat
= FcNameParse ((FcChar8
*) buf
);
4321 printd ("emsg FcNameParse failed\n");
4326 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4327 printd ("emsg FcConfigSubstitute failed\n");
4331 FcDefaultSubstitute (pat
);
4333 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4335 printd ("emsg FcFontMatch failed\n");
4336 FcPatternDestroy (pat
);
4341 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4342 printd ("emsg FcPatternGetString failed\n");
4343 FcPatternDestroy (pat
);
4344 FcPatternDestroy (pat1
);
4350 printd ("emsg name=%s path=%s\n", name
, path
);
4352 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4353 FcPatternDestroy (pat
);
4354 FcPatternDestroy (pat1
);
4360 CAMLprim value
ml_init (value csock_v
, value params_v
)
4362 CAMLparam2 (csock_v
, params_v
);
4363 CAMLlocal2 (trim_v
, fuzz_v
);
4370 struct sigaction sa
;
4372 state
.csock
= Int_val (csock_v
);
4373 state
.rotate
= Int_val (Field (params_v
, 0));
4374 state
.fitmodel
= Int_val (Field (params_v
, 1));
4375 trim_v
= Field (params_v
, 2);
4376 texcount
= Int_val (Field (params_v
, 3));
4377 state
.sliceheight
= Int_val (Field (params_v
, 4));
4378 mustoresize
= Int_val (Field (params_v
, 5));
4379 colorspace
= Int_val (Field (params_v
, 6));
4380 fontpath
= String_val (Field (params_v
, 7));
4382 if (caml_string_length (Field (params_v
, 8)) > 0) {
4383 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4385 if (!state
.trimcachepath
) {
4386 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4390 haspboext
= Bool_val (Field (params_v
, 9));
4392 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4394 #ifdef USE_FONTCONFIG
4395 if (Bool_val (Field (params_v
, 10))) {
4396 fz_install_load_system_font_funcs (
4397 state
.ctx
, fc_load_system_font_func
, NULL
4402 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4403 fuzz_v
= Field (trim_v
, 1);
4404 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4405 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4406 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4407 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4409 set_tex_params (colorspace
);
4412 #ifndef USE_FONTCONFIG
4413 state
.face
= load_font (fontpath
);
4417 char *buf
= fontpath
;
4418 FcPattern
*pat
, *pat1
;
4421 fc
.config
= FcInitLoadConfigAndFonts ();
4423 errx (1, "FcInitLoadConfigAndFonts failed");
4426 pat
= FcNameParse ((FcChar8
*) buf
);
4428 errx (1, "FcNameParse failed");
4431 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4432 errx (1, "FcConfigSubstitute failed");
4434 FcDefaultSubstitute (pat
);
4436 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4438 errx (1, "FcFontMatch failed");
4441 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4442 errx (1, "FcPatternGetString failed");
4445 state
.face
= load_font ((char *) path
);
4446 FcPatternDestroy (pat
);
4447 FcPatternDestroy (pat1
);
4452 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4454 state
.face
= load_builtin_font (base
, len
);
4456 if (!state
.face
) _exit (1);
4458 realloctexts (texcount
);
4467 sa
.sa_handler
= SIG_IGN
;
4468 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4470 sa
.sa_handler
= SIG_DFL
;
4471 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4473 if (sigemptyset (&sa
.sa_mask
)) {
4474 err (1, "sigemptyset");
4476 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4477 err (1, "sigaction");
4480 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4482 errx (1, "pthread_create: %s", strerror (ret
));
4485 CAMLreturn (Val_unit
);