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
;
166 enum { SLINK
, SANNOT
} tag
;
179 enum { DNONE
, DPDF
, DXPS
, DCBZ
, DIMG
};
188 fz_text_sheet
*sheet
;
196 fz_display_list
*dlist
;
198 struct slink
*slinks
;
200 struct annot
*annots
;
205 void (*freepage
) (void *);
211 struct pagedim
*pagedims
;
231 fz_colorspace
*colorspace
;
239 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
253 void (*closedoc
) (void);
254 void (*freepage
) (void *);
262 void (*glBindBufferARB
) (GLenum
, GLuint
);
263 GLboolean (*glUnmapBufferARB
) (GLenum
);
264 void *(*glMapBufferARB
) (GLenum
, GLenum
);
265 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
266 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
267 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
269 GLfloat texcoords
[8];
270 GLfloat vertices
[16];
272 #ifdef CACHE_PAGEREFS
288 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
290 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
293 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
295 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
298 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
300 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
301 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
304 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
306 static void lock (const char *cap
)
308 int ret
= pthread_mutex_lock (&mutex
);
310 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
314 static void unlock (const char *cap
)
316 int ret
= pthread_mutex_unlock (&mutex
);
318 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
322 static int trylock (const char *cap
)
324 int ret
= pthread_mutex_trylock (&mutex
);
325 if (ret
&& ret
!= EBUSY
) {
326 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
331 static void *parse_pointer (const char *cap
, const char *s
)
336 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
338 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
343 static double now (void)
347 if (gettimeofday (&tv
, NULL
)) {
348 err (1, "gettimeofday");
350 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
353 static int hasdata (void)
356 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
357 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
361 CAMLprim value
ml_hasdata (value fd_v
)
366 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
367 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
368 CAMLreturn (Val_bool (avail
> 0));
371 static void readdata (void *p
, int size
)
376 n
= read (state
.csock
, p
, size
);
378 if (errno
== EINTR
) goto again
;
379 err (1, "read (req %d, ret %zd)", size
, n
);
382 if (!n
) errx (1, "EOF while reading");
383 errx (1, "read (req %d, ret %zd)", size
, n
);
387 static void writedata (char *p
, int size
)
391 p
[0] = (size
>> 24) & 0xff;
392 p
[1] = (size
>> 16) & 0xff;
393 p
[2] = (size
>> 8) & 0xff;
394 p
[3] = (size
>> 0) & 0xff;
396 n
= write (state
.csock
, p
, size
+ 4);
398 if (!n
) errx (1, "EOF while writing data");
399 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
403 static int readlen (void)
408 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
411 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
419 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
422 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
426 if (len
< size
- 4) {
427 writedata (buf
, len
);
433 err (1, "vsnprintf for `%s' failed", fmt
);
435 buf
= realloc (buf
, size
);
440 static void closepdf (void)
442 #ifdef CACHE_PAGEREFS
443 if (state
.pdflut
.objs
) {
446 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
447 pdf_drop_obj (state
.pdflut
.objs
[i
]);
449 free (state
.pdflut
.objs
);
450 state
.pdflut
.objs
= NULL
;
451 state
.pdflut
.idx
= 0;
456 pdf_close_document (state
.u
.pdf
);
461 static void closexps (void)
464 xps_close_document (state
.u
.xps
);
469 static void closecbz (void)
472 cbz_close_document (state
.u
.cbz
);
477 static void closeimg (void)
480 image_close_document (state
.u
.img
);
485 static void freepdfpage (void *ptr
)
487 pdf_free_page (state
.u
.pdf
, ptr
);
490 static void freeemptyxxxpage (void *ptr
)
495 static void freexpspage (void *ptr
)
497 xps_free_page (state
.u
.xps
, ptr
);
500 static void freecbzpage (void *ptr
)
502 cbz_free_page (state
.u
.cbz
, ptr
);
505 static void freeimgpage (void *ptr
)
507 image_free_page (state
.u
.img
, ptr
);
510 static void openxref (char *filename
, char *password
)
514 for (i
= 0; i
< state
.texcount
; ++i
) {
515 state
.texowners
[i
].w
= -1;
516 state
.texowners
[i
].slice
= NULL
;
519 if (state
.closedoc
) state
.closedoc ();
521 len
= strlen (filename
);
539 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
540 const char *ext
= tbl
[j
].ext
;
541 int len2
= strlen (ext
);
543 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
544 state
.type
= tbl
[j
].type
;
550 if (state
.pagedims
) {
551 free (state
.pagedims
);
552 state
.pagedims
= NULL
;
554 state
.pagedimcount
= 0;
556 fz_set_aa_level (state
.ctx
, state
.aalevel
);
557 switch (state
.type
) {
559 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
560 if (pdf_needs_password (state
.u
.pdf
)) {
561 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
563 errx (1, "invalid password");
566 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
567 state
.closedoc
= closepdf
;
568 state
.freepage
= freepdfpage
;
572 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
573 state
.pagecount
= xps_count_pages (state
.u
.xps
);
574 state
.closedoc
= closexps
;
575 state
.freepage
= freexpspage
;
579 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
580 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
581 state
.closedoc
= closecbz
;
582 state
.freepage
= freecbzpage
;
586 state
.u
.img
= image_open_document (state
.ctx
, filename
);
588 state
.closedoc
= closeimg
;
589 state
.freepage
= freeimgpage
;
594 static void pdfinfo (void)
596 if (state
.type
== DPDF
) {
599 printd ("info PDF version\t%d.%d",
600 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
602 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
606 char *items
[] = { "Title", "Author", "Creator",
607 "Producer", "CreationDate" };
609 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
610 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
611 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
612 if (*s
) printd ("info %s\t%s", items
[i
], s
);
613 fz_free (state
.ctx
, s
);
620 static void unlinktile (struct tile
*tile
)
624 for (i
= 0; i
< tile
->slicecount
; ++i
) {
625 struct slice
*s
= &tile
->slices
[i
];
627 if (s
->texindex
!= -1) {
628 if (state
.texowners
[s
->texindex
].slice
== s
) {
629 state
.texowners
[s
->texindex
].slice
= NULL
;
635 static void freepage (struct page
*page
)
638 fz_free_text_page (state
.ctx
, page
->text
);
641 fz_free_text_sheet (state
.ctx
, page
->sheet
);
646 fz_drop_display_list (state
.ctx
, page
->dlist
);
647 page
->freepage (page
->u
.ptr
);
651 static void freetile (struct tile
*tile
)
656 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
659 fz_drop_pixmap (state
.ctx
, state
.pig
);
661 state
.pig
= tile
->pixmap
;
666 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
675 static int cacheline32bytes
;
677 static void __attribute__ ((constructor
)) clcheck (void)
679 char **envp
= environ
;
684 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
686 cacheline32bytes
= auxv
[1] == 32;
692 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
694 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
695 if (cacheline32bytes
&& size
> 32) {
696 intptr_t a1
, a2
, diff
;
698 vector
unsigned char v
= vec_splat_u8 (-1);
699 vector
unsigned char *p
;
701 a1
= a2
= (intptr_t) pixmap
->samples
;
702 a2
= (a1
+ 31) & ~31;
707 while (a1
!= a2
) *(char *) a1
++ = 0xff;
708 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
709 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
711 vec_st (v
, i
+ 16, p
);
713 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
715 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
718 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
721 static void trimctm (pdf_page
*page
, int pindex
)
724 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
726 if (!pdim
->tctmready
) {
727 if (state
.trimmargins
) {
729 fz_matrix rm
, sm
, tm
, im
, ctm1
;
731 fz_rotate (&rm
, -pdim
->rotate
);
732 fz_scale (&sm
, 1, -1);
733 fz_concat (&ctm
, &rm
, &sm
);
734 realbox
= pdim
->mediabox
;
735 fz_transform_rect (&realbox
, &ctm
);
736 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
737 fz_concat (&ctm1
, &ctm
, &tm
);
738 fz_invert_matrix (&im
, &page
->ctm
);
739 fz_concat (&ctm
, &im
, &ctm1
);
749 static fz_matrix
pagectm (struct page
*page
)
753 if (page
->type
== DPDF
) {
754 trimctm (page
->u
.pdfpage
, page
->pdimno
);
756 &state
.pagedims
[page
->pdimno
].tctm
,
757 &state
.pagedims
[page
->pdimno
].ctm
);
760 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
762 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
763 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
768 static void *loadpage (int pageno
, int pindex
)
773 page
= calloc (sizeof (struct page
), 1);
775 err (1, "calloc page %d", pageno
);
778 page
->dlist
= fz_new_display_list (state
.ctx
);
779 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
781 switch (state
.type
) {
783 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
784 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
786 page
->freepage
= freepdfpage
;
790 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
791 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
793 page
->freepage
= freexpspage
;
797 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
798 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
800 page
->freepage
= freecbzpage
;
804 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
805 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
807 page
->freepage
= freeimgpage
;
811 fz_catch (state
.ctx
) {
813 page
->freepage
= freeemptyxxxpage
;
815 fz_free_device (dev
);
817 page
->pdimno
= pindex
;
818 page
->pageno
= pageno
;
819 page
->sgen
= state
.gen
;
820 page
->tgen
= state
.gen
;
821 page
->type
= state
.type
;
826 static struct tile
*alloctile (int h
)
833 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
834 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
835 tile
= calloc (tilesize
, 1);
837 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
839 for (i
= 0; i
< slicecount
; ++i
) {
840 int sh
= MIN (h
, state
.sliceheight
);
841 tile
->slices
[i
].h
= sh
;
842 tile
->slices
[i
].texindex
= -1;
845 tile
->slicecount
= slicecount
;
846 tile
->sliceheight
= state
.sliceheight
;
856 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
857 const fz_matrix
*ctm
, float alpha
)
859 struct obs
*obs
= dev
->user
;
861 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
863 fz_rect rect
= fz_unit_rect
;
865 fz_transform_rect (&rect
, ctm
);
866 fz_round_rect (&b
, &rect
);
867 fz_intersect_irect (&b
, &obs
->b
);
868 obs
->cured
= b
.x0
== obs
->b
.x0
871 && b
.y1
== obs
->b
.y1
;
875 static int obscured (struct page
*page
, fz_irect bbox
)
882 memset (&dev
, 0, sizeof (dev
));
883 memset (&obs
, 0, sizeof (obs
));
888 dev
.fill_image
= obs_fill_image
;
890 fz_rect_from_irect (&rect
, &bbox
);
891 ctm
= pagectm (page
);
892 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
895 #define OBSCURED obscured
897 #define OBSCURED(a, b) 0
900 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
908 struct pagedim
*pdim
;
910 tile
= alloctile (h
);
911 pdim
= &state
.pagedims
[page
->pdimno
];
916 bbox
.x1
= bbox
.x0
+ w
;
917 bbox
.y1
= bbox
.y0
+ h
;
920 if (state
.pig
->w
== w
922 && state
.pig
->colorspace
== state
.colorspace
) {
923 tile
->pixmap
= state
.pig
;
924 tile
->pixmap
->x
= bbox
.x0
;
925 tile
->pixmap
->y
= bbox
.y0
;
928 fz_drop_pixmap (state
.ctx
, state
.pig
);
935 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
941 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
947 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
948 clearpixmap (tile
->pixmap
);
950 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
951 ctm
= pagectm (page
);
952 fz_rect_from_irect (&rect
, &bbox
);
953 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
954 fz_free_device (dev
);
959 #ifdef CACHE_PAGEREFS
960 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
961 thanks to Robin Watts */
963 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
965 fz_context
*ctx
= doc
->ctx
;
969 if (state
.pdflut
.idx
== state
.pagecount
) return;
971 kids
= pdf_dict_gets (node
, "Kids");
972 len
= pdf_array_len (kids
);
975 fz_throw (ctx
, FZ_ERROR_GENERIC
, "Malformed pages tree");
977 if (pdf_mark_obj (node
))
978 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
979 for (i
= 0; i
< len
; i
++) {
980 pdf_obj
*kid
= pdf_array_get (kids
, i
);
981 char *type
= pdf_to_name (pdf_dict_gets (kid
, "Type"));
983 ? !strcmp (type
, "Pages")
984 : pdf_dict_gets (kid
, "Kids")
985 && !pdf_dict_gets (kid
, "MediaBox")) {
986 pdf_collect_pages (doc
, kid
);
990 ? strcmp (type
, "Page") != 0
991 : !pdf_dict_gets (kid
, "MediaBox"))
992 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
993 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (kid
);
996 pdf_unmark_obj (node
);
1000 pdf_load_page_objs (pdf_document
*doc
)
1002 pdf_obj
*root
= pdf_dict_gets (pdf_trailer (doc
), "Root");
1003 pdf_obj
*node
= pdf_dict_gets (root
, "Pages");
1006 fz_throw (doc
->ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
1008 state
.pdflut
.idx
= 0;
1009 pdf_collect_pages (doc
, node
);
1013 static void initpdims (void)
1017 fz_rect rootmediabox
;
1018 int pageno
, trim
, show
;
1019 int trimw
= 0, cxcount
;
1023 if (state
.trimmargins
&& state
.trimcachepath
) {
1024 trimf
= fopen (state
.trimcachepath
, "rb");
1026 trimf
= fopen (state
.trimcachepath
, "wb");
1031 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1032 cxcount
= state
.pagecount
;
1034 cxcount
= MIN (state
.pagecount
, 1);
1036 if (state
.type
== DPDF
) {
1038 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1039 "Root/Pages/MediaBox");
1040 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1043 #ifdef CACHE_PAGEREFS
1044 if (state
.type
== DPDF
1045 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1046 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
1047 if (!state
.pdflut
.objs
) {
1048 err (1, "malloc pageobjs %zu %d %zu failed",
1049 sizeof (*state
.pdflut
.objs
), cxcount
,
1050 sizeof (*state
.pdflut
.objs
) * cxcount
);
1052 state
.pdflut
.count
= cxcount
;
1053 pdf_load_page_objs (state
.u
.pdf
);
1054 state
.pdflut
.pdf
= state
.u
.pdf
;
1058 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1063 switch (state
.type
) {
1065 pdf_document
*pdf
= state
.u
.pdf
;
1066 pdf_obj
*pageref
, *pageobj
;
1068 #ifdef CACHE_PAGEREFS
1069 pageref
= state
.pdflut
.objs
[pageno
];
1071 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1072 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1074 printd ("progress %f Gathering dimensions %d",
1075 (double) (pageno
) / state
.pagecount
,
1079 pageobj
= pdf_resolve_indirect (pageref
);
1081 if (state
.trimmargins
) {
1085 fz_try (state
.ctx
) {
1086 page
= pdf_load_page (pdf
, pageno
);
1087 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1088 trim
= state
.trimanew
|| !obj
;
1094 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1095 dev
->hints
|= FZ_IGNORE_SHADE
;
1096 fz_invert_matrix (&ctm
, &page
->ctm
);
1097 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1098 fz_free_device (dev
);
1100 rect
.x0
+= state
.trimfuzz
.x0
;
1101 rect
.x1
+= state
.trimfuzz
.x1
;
1102 rect
.y0
+= state
.trimfuzz
.y0
;
1103 rect
.y1
+= state
.trimfuzz
.y1
;
1104 fz_transform_rect (&rect
, &ctm
);
1105 fz_intersect_rect (&rect
, &page
->mediabox
);
1107 if (fz_is_empty_rect (&rect
)) {
1108 mediabox
= page
->mediabox
;
1114 obj
= pdf_new_array (pdf
, 4);
1115 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1116 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1117 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1118 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1119 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1122 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1123 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1124 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1125 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1128 rotate
= page
->rotate
;
1129 pdf_free_page (pdf
, page
);
1131 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1133 printd ("progress %f Trimming %d",
1134 (double) (pageno
+ 1) / state
.pagecount
,
1138 fz_catch (state
.ctx
) {
1139 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1146 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1148 if (fz_is_empty_rect (&mediabox
)) {
1156 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1158 if (!fz_is_empty_rect (&cropbox
)) {
1163 fz_intersect_rect (&mediabox
, &cropbox
);
1168 if (fz_is_empty_rect (&rootmediabox
)) {
1170 "cannot find page size for page %d\n",
1174 mediabox
= rootmediabox
;
1178 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1187 fz_try (state
.ctx
) {
1188 page
= xps_load_page (state
.u
.xps
, pageno
);
1189 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1191 if (state
.trimmargins
) {
1195 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1196 dev
->hints
|= FZ_IGNORE_SHADE
;
1197 xps_run_page (state
.u
.xps
, page
, dev
,
1198 &fz_identity
, NULL
);
1199 fz_free_device (dev
);
1201 rect
.x0
+= state
.trimfuzz
.x0
;
1202 rect
.x1
+= state
.trimfuzz
.x1
;
1203 rect
.y0
+= state
.trimfuzz
.y0
;
1204 rect
.y1
+= state
.trimfuzz
.y1
;
1205 fz_intersect_rect (&rect
, &mediabox
);
1207 if (!fz_is_empty_rect (&rect
)) {
1211 xps_free_page (state
.u
.xps
, page
);
1213 printd ("progress %f loading %d",
1214 (double) (pageno
+ 1) / state
.pagecount
,
1218 fz_catch (state
.ctx
) {
1230 if (state
.trimmargins
&& trimw
) {
1233 fz_try (state
.ctx
) {
1234 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1235 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1236 cbz_free_page (state
.u
.cbz
, page
);
1237 printd ("progress %f Trimming %d",
1238 (double) (pageno
+ 1) / state
.pagecount
,
1241 fz_catch (state
.ctx
) {
1242 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1245 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1247 err (1, "fwrite trim mediabox");
1253 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1255 err (1, "fread trim mediabox");
1261 fz_try (state
.ctx
) {
1262 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1263 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1264 cbz_free_page (state
.u
.cbz
, page
);
1266 fz_catch (state
.ctx
) {
1267 fprintf (stderr
, "failed to load cbz page\n");
1285 fz_try (state
.ctx
) {
1286 page
= image_load_page (state
.u
.img
, pageno
);
1287 image_bound_page (state
.u
.img
, page
, &mediabox
);
1288 image_free_page (state
.u
.img
, page
);
1290 fz_catch (state
.ctx
) {
1291 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1297 ARSERT (0 && state
.type
);
1300 if (state
.pagedimcount
== 0
1301 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1302 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1305 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1306 state
.pagedims
= realloc (state
.pagedims
, size
);
1307 if (!state
.pagedims
) {
1308 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1309 size
, state
.pagedimcount
+ 1);
1312 p
= &state
.pagedims
[state
.pagedimcount
++];
1314 p
->mediabox
= mediabox
;
1319 if (state
.trimmargins
) {
1320 printd ("progress 1 Trimmed %d pages in %f seconds",
1321 state
.pagecount
, end
- start
);
1324 if (state
.type
== DPDF
)
1325 printd ("progress 1 Processed %d pages in %f seconds",
1326 state
.pagecount
, end
- start
);
1328 printd ("vmsg Processed %d pages in %f seconds",
1329 state
.pagecount
, end
- start
);
1333 if (fclose (trimf
)) {
1339 static void layout (void)
1344 struct pagedim
*p
= p
;
1345 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1347 if (state
.pagedimcount
== 0) return;
1349 switch (state
.fitmodel
) {
1350 case FitProportional
:
1351 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1354 p
= &state
.pagedims
[pindex
];
1355 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1357 fz_transform_rect (&box
, &rm
);
1359 x0
= MIN (box
.x0
, box
.x1
);
1360 x1
= MAX (box
.x0
, box
.x1
);
1363 maxw
= MAX (w
, maxw
);
1364 zoom
= state
.w
/ maxw
;
1376 ARSERT (0 && state
.fitmodel
);
1379 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1383 p
= &state
.pagedims
[pindex
];
1384 fz_rotate (&ctm
, state
.rotate
);
1385 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1387 fz_transform_rect (&box
, &rm
);
1388 w
= box
.x1
- box
.x0
;
1389 switch (state
.fitmodel
) {
1390 case FitProportional
:
1391 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1397 h
= box
.y1
- box
.y0
;
1399 zoom
= MIN (zw
, zh
);
1400 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1409 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1410 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1412 fz_rotate (&rm
, p
->rotate
);
1413 p
->pagebox
= p
->mediabox
;
1414 fz_transform_rect (&p
->pagebox
, &rm
);
1415 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1416 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1420 fz_transform_rect (&rect
, &ctm
);
1421 fz_round_rect (&p
->bounds
, &rect
);
1424 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1425 fz_scale (&sm
, zoom
, -zoom
);
1426 fz_concat (&ctm
, &tm
, &sm
);
1427 fz_concat (&p
->lctm
, &ctm
, &rm
);
1433 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1434 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1435 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1436 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1437 int boundw
= x1
- x0
;
1438 int boundh
= y1
- y0
;
1440 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1441 } while (p
-- != state
.pagedims
);
1445 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1446 desttoanchor (fz_link_dest
*dest
)
1450 struct pagedim
*pdim
= state
.pagedims
;
1455 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1456 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1458 pdim
= &state
.pagedims
[i
];
1460 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1462 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1463 p
.x
= dest
->ld
.gotor
.lt
.x
;
1466 p
.y
= dest
->ld
.gotor
.lt
.y
;
1467 fz_transform_point (&p
, &pdim
->lctm
);
1471 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1472 double x0
, x1
, y0
, y1
;
1474 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1475 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1477 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1478 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1480 a
.n
= dest
->ld
.gotor
.page
;
1485 static void recurse_outline (fz_outline
*outline
, int level
)
1488 switch (outline
->dest
.kind
) {
1491 struct anchor a
= desttoanchor (&outline
->dest
);
1494 printd ("o %d %d %d %d %s",
1495 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1501 printd ("ou %d %" FMT_s
" %s %s", level
,
1502 strlen (outline
->title
), outline
->title
,
1503 outline
->dest
.ld
.uri
.uri
);
1507 printd ("on %d %s", level
, outline
->title
);
1511 printd ("emsg Unhandled outline kind %d for %s\n",
1512 outline
->dest
.kind
, outline
->title
);
1515 if (outline
->down
) {
1516 recurse_outline (outline
->down
, level
+ 1);
1518 outline
= outline
->next
;
1522 static void process_outline (void)
1524 fz_outline
*outline
;
1526 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1528 state
.needoutline
= 0;
1529 switch (state
.type
) {
1531 outline
= pdf_load_outline (state
.u
.pdf
);
1534 outline
= xps_load_outline (state
.u
.xps
);
1541 recurse_outline (outline
, 0);
1542 fz_free_outline (state
.ctx
, outline
);
1546 static char *strofspan (fz_text_span
*span
)
1551 size_t size
= 0, cap
= 80;
1553 p
= malloc (cap
+ 1);
1554 if (!p
) return NULL
;
1556 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1557 int n
= fz_runetochar (utf8
, ch
->c
);
1558 if (size
+ n
> cap
) {
1560 p
= realloc (p
, cap
+ 1);
1561 if (!p
) return NULL
;
1564 memcpy (p
+ size
, utf8
, n
);
1571 static int matchspan (regex_t
*re
, fz_text_span
*span
,
1572 int stop
, int pageno
, double start
)
1579 fz_point p1
, p2
, p3
, p4
;
1581 p
= strofspan (span
);
1584 ret
= regexec (re
, p
, 1, &rm
, 0);
1587 if (ret
!= REG_NOMATCH
) {
1590 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1591 printd ("msg regexec error `%.*s'",
1592 (int) size
, errbuf
);
1600 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1601 c
+= fz_runelen (span
->text
[a
].c
);
1603 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1604 c
+= fz_runelen (span
->text
[b
].c
);
1607 if (fz_runelen (span
->text
[b
].c
) > 1) {
1611 fz_text_char_bbox (&sb
, span
, a
);
1612 fz_text_char_bbox (&eb
, span
, b
);
1624 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1631 printd ("progress 1 found at %d `%.*s' in %f sec",
1632 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1636 printd ("match %d %d %f %f %f %f %f %f %f %f",
1648 static int compareblocks (const void *l
, const void *r
)
1650 fz_text_block
const *ls
= l
;
1651 fz_text_block
const* rs
= r
;
1652 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1655 /* wishful thinking function */
1656 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1661 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1663 fz_text_sheet
*sheet
;
1664 struct pagedim
*pdim
, *pdimprev
;
1665 int stop
= 0, niters
= 0;
1668 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1672 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1673 if (niters
++ == 5) {
1676 printd ("progress 1 attention requested aborting search at %d",
1681 printd ("progress %f searching in page %d",
1682 (double) (pageno
+ 1) / state
.pagecount
,
1687 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1688 pdim
= &state
.pagedims
[i
];
1689 if (pdim
->pageno
== pageno
) {
1692 if (pdim
->pageno
> pageno
) {
1701 sheet
= fz_new_text_sheet (state
.ctx
);
1702 text
= fz_new_text_page (state
.ctx
);
1703 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1705 switch (state
.type
) {
1708 fz_try (state
.ctx
) {
1709 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1710 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1711 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1712 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1713 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1716 fz_catch (state
.ctx
) {
1717 fz_free_device (tdev
);
1724 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1725 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1729 ARSERT (0 && state
.type
);
1732 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1733 fz_free_device (tdev
);
1735 for (j
= 0; j
< text
->len
; ++j
) {
1738 fz_text_block
*block
;
1740 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1741 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1744 for (k
= 0; k
< block
->len
; ++k
) {
1749 line
= &block
->lines
[k
];
1750 if (line
->bbox
.y0
< y
+ 1) continue;
1753 line
= &block
->lines
[block
->len
- 1 - k
];
1754 if (line
->bbox
.y0
> y
- 1) continue;
1757 for (span
= line
->first_span
; span
; span
= span
->next
) {
1758 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1760 case 1: stop
= 1; break;
1761 case -1: stop
= 1; goto endloop
;
1776 fz_free_text_page (state
.ctx
, text
);
1777 fz_free_text_sheet (state
.ctx
, sheet
);
1779 state
.freepage (u
.ptr
);
1784 printd ("progress 1 no matches %f sec", end
- start
);
1786 printd ("clearrects");
1789 static void set_tex_params (int colorspace
)
1796 switch (colorspace
) {
1798 state
.texiform
= GL_RGBA8
;
1799 state
.texform
= GL_RGBA
;
1800 state
.texty
= GL_UNSIGNED_BYTE
;
1801 state
.colorspace
= fz_device_rgb (state
.ctx
);
1804 state
.texiform
= GL_RGBA8
;
1805 state
.texform
= GL_BGRA
;
1806 state
.texty
= endianness
.s
> 1
1807 ? GL_UNSIGNED_INT_8_8_8_8
1808 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1809 state
.colorspace
= fz_device_bgr (state
.ctx
);
1812 state
.texiform
= GL_LUMINANCE_ALPHA
;
1813 state
.texform
= GL_LUMINANCE_ALPHA
;
1814 state
.texty
= GL_UNSIGNED_BYTE
;
1815 state
.colorspace
= fz_device_gray (state
.ctx
);
1818 errx (1, "invalid colorspce %d", colorspace
);
1822 static void realloctexts (int texcount
)
1826 if (texcount
== state
.texcount
) return;
1828 if (texcount
< state
.texcount
) {
1829 glDeleteTextures (state
.texcount
- texcount
,
1830 state
.texids
+ texcount
);
1833 size
= texcount
* sizeof (*state
.texids
);
1834 state
.texids
= realloc (state
.texids
, size
);
1835 if (!state
.texids
) {
1836 err (1, "realloc texids %" FMT_s
, size
);
1839 size
= texcount
* sizeof (*state
.texowners
);
1840 state
.texowners
= realloc (state
.texowners
, size
);
1841 if (!state
.texowners
) {
1842 err (1, "realloc texowners %" FMT_s
, size
);
1844 if (texcount
> state
.texcount
) {
1847 glGenTextures (texcount
- state
.texcount
,
1848 state
.texids
+ state
.texcount
);
1849 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1850 state
.texowners
[i
].w
= -1;
1851 state
.texowners
[i
].slice
= NULL
;
1854 state
.texcount
= texcount
;
1858 static char *mbtoutf8 (char *s
)
1864 len
= mbstowcs (NULL
, s
, strlen (s
));
1869 if (len
== (size_t) -1) {
1874 tmp
= malloc (len
* sizeof (wchar_t));
1879 ret
= mbstowcs (tmp
, s
, len
);
1880 if (ret
== (size_t) -1) {
1886 for (i
= 0; i
< ret
; ++i
) {
1887 len
+= fz_runelen (tmp
[i
]);
1890 p
= r
= malloc (len
+ 1);
1896 for (i
= 0; i
< ret
; ++i
) {
1897 p
+= fz_runetochar (p
, tmp
[i
]);
1904 CAMLprim value
ml_mbtoutf8 (value s_v
)
1910 s
= String_val (s_v
);
1916 ret_v
= caml_copy_string (r
);
1922 static void * mainloop (void UNUSED_ATTR
*unused
)
1925 int len
, ret
, oldlen
= 0;
1930 errx (1, "readlen returned 0");
1933 if (oldlen
< len
+ 1) {
1934 p
= realloc (p
, len
+ 1);
1936 err (1, "realloc %d failed", len
+ 1);
1943 if (!strncmp ("open", p
, 4)) {
1944 int wthack
, off
, ok
= 0;
1950 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1952 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1955 filename
= p
+ 5 + off
;
1956 filenamelen
= strlen (filename
);
1957 password
= filename
+ filenamelen
+ 1;
1960 fz_try (state
.ctx
) {
1961 openxref (filename
, password
);
1964 fz_catch (state
.ctx
) {
1965 utf8filename
= mbtoutf8 (filename
);
1966 printd ("msg Could not open %s", utf8filename
);
1976 utf8filename
= mbtoutf8 (filename
);
1977 printd ("msg Opened %s (press h/F1 to get help)",
1979 if (utf8filename
!= filename
) {
1980 free (utf8filename
);
1983 state
.needoutline
= 1;
1986 else if (!strncmp ("cs", p
, 2)) {
1989 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1991 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1994 set_tex_params (colorspace
);
1995 for (i
= 0; i
< state
.texcount
; ++i
) {
1996 state
.texowners
[i
].w
= -1;
1997 state
.texowners
[i
].slice
= NULL
;
2001 else if (!strncmp ("freepage", p
, 8)) {
2004 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2006 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
2010 else if (!strncmp ("freetile", p
, 8)) {
2013 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2015 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
2019 else if (!strncmp ("search", p
, 6)) {
2020 int icase
, pageno
, y
, len2
, forward
;
2024 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2025 &icase
, &pageno
, &y
, &forward
, &len2
);
2027 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2030 pattern
= p
+ 6 + len2
;
2031 ret
= regcomp (&re
, pattern
,
2032 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2037 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2038 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2041 search (&re
, pageno
, y
, forward
);
2045 else if (!strncmp ("geometry", p
, 8)) {
2049 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2051 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2059 for (i
= 0; i
< state
.texcount
; ++i
) {
2060 state
.texowners
[i
].slice
= NULL
;
2063 state
.fitmodel
= fitmodel
;
2068 unlock ("geometry");
2069 printd ("continue %d", state
.pagecount
);
2071 else if (!strncmp ("reqlayout", p
, 9)) {
2074 unsigned int fitmodel
;
2077 ret
= sscanf (p
+ 9, " %d %u %d %n",
2078 &rotate
, &fitmodel
, &h
, &off
);
2080 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2083 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2086 state
.rotate
= rotate
;
2087 state
.fitmodel
= fitmodel
;
2092 nameddest
= p
+ 9 + off
;
2093 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2096 pdf_obj
*needle
, *obj
;
2098 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2099 strlen (nameddest
));
2100 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2102 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2104 a
= desttoanchor (&dest
);
2106 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2109 printd ("emsg failed to parse destination `%s'\n",
2114 printd ("emsg destination `%s' not found\n",
2117 pdf_drop_obj (needle
);
2121 unlock ("reqlayout");
2122 printd ("continue %d", state
.pagecount
);
2124 else if (!strncmp ("page", p
, 4)) {
2129 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2131 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2136 page
= loadpage (pageno
, pindex
);
2140 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2142 else if (!strncmp ("tile", p
, 4)) {
2149 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2150 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2151 SCN_ptr_cast (&data
));
2153 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2158 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2162 printd ("tile %d %d %" FMT_ptr
" %u %f",
2164 FMT_ptr_cast (tile
),
2165 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2168 else if (!strncmp ("trimset", p
, 7)) {
2172 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2173 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2175 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
2178 state
.trimmargins
= trimmargins
;
2179 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2181 state
.trimfuzz
= fuzz
;
2185 else if (!strncmp ("settrim", p
, 7)) {
2189 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2190 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2192 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2196 state
.trimmargins
= trimmargins
;
2197 state
.needoutline
= 1;
2198 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2200 state
.trimfuzz
= fuzz
;
2202 state
.pagedimcount
= 0;
2203 free (state
.pagedims
);
2204 state
.pagedims
= NULL
;
2209 printd ("continue %d", state
.pagecount
);
2211 else if (!strncmp ("sliceh", p
, 6)) {
2214 ret
= sscanf (p
+ 6, " %d", &h
);
2216 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2218 if (h
!= state
.sliceheight
) {
2221 state
.sliceheight
= h
;
2222 for (i
= 0; i
< state
.texcount
; ++i
) {
2223 state
.texowners
[i
].w
= -1;
2224 state
.texowners
[i
].h
= -1;
2225 state
.texowners
[i
].slice
= NULL
;
2229 else if (!strncmp ("interrupt", p
, 9)) {
2230 printd ("vmsg interrupted");
2233 errx (1, "unknown command %.*s", len
, p
);
2239 CAMLprim value
ml_realloctexts (value texcount_v
)
2241 CAMLparam1 (texcount_v
);
2244 if (trylock ("ml_realloctexts")) {
2248 realloctexts (Int_val (texcount_v
));
2250 unlock ("ml_realloctexts");
2253 CAMLreturn (Val_bool (ok
));
2256 static void recti (int x0
, int y0
, int x1
, int y1
)
2258 GLfloat
*v
= state
.vertices
;
2260 glVertexPointer (2, GL_FLOAT
, 0, v
);
2261 v
[0] = x0
; v
[1] = y0
;
2262 v
[2] = x1
; v
[3] = y0
;
2263 v
[4] = x0
; v
[5] = y1
;
2264 v
[6] = x1
; v
[7] = y1
;
2265 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2268 static void showsel (struct page
*page
, int ox
, int oy
)
2274 fz_page_block
*pageb
;
2275 fz_text_block
*block
;
2276 struct mark first
, last
;
2277 unsigned char selcolor
[] = {15,15,15,140};
2279 first
= page
->fmark
;
2282 if (!first
.span
|| !last
.span
) return;
2284 glEnable (GL_BLEND
);
2285 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2286 glColor4ubv (selcolor
);
2288 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2289 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2290 for (pageb
= page
->text
->blocks
;
2291 pageb
< page
->text
->blocks
+ page
->text
->len
;
2293 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2294 block
= pageb
->u
.text
;
2296 for (line
= block
->lines
;
2297 line
< block
->lines
+ block
->len
;
2300 rect
= fz_empty_rect
;
2302 for (span
= line
->first_span
; span
; span
= span
->next
) {
2304 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2309 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2311 j
= MIN (first
.i
, last
.i
);
2312 k
= MAX (first
.i
, last
.i
);
2315 if (span
== first
.span
) {
2319 else if (span
== last
.span
) {
2326 for (i
= j
; i
<= k
; ++i
) {
2328 fz_union_rect (&rect
,
2329 fz_text_char_bbox (&bbox1
, span
, i
));
2331 fz_round_rect (&bbox
, &rect
);
2332 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2339 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2340 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2341 if (span
== last
.span
) {
2344 rect
= fz_empty_rect
;
2350 glDisable (GL_BLEND
);
2355 static void stipplerect (fz_matrix
*m
,
2363 fz_transform_point (p1
, m
);
2364 fz_transform_point (p2
, m
);
2365 fz_transform_point (p3
, m
);
2366 fz_transform_point (p4
, m
);
2372 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2376 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2378 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2379 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2381 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2382 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2384 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2385 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2387 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2388 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2390 glDrawArrays (GL_LINES
, 0, 8);
2393 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2396 fz_matrix ctm
, tm
, pm
;
2397 fz_link
*link
, *links
;
2398 GLfloat
*texcoords
= state
.texcoords
;
2399 GLfloat
*vertices
= state
.vertices
;
2401 switch (page
->type
) {
2403 links
= page
->u
.pdfpage
->links
;
2407 links
= page
->u
.xpspage
->links
;
2414 glEnable (GL_TEXTURE_1D
);
2415 glEnable (GL_BLEND
);
2416 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2417 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2419 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2420 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2421 fz_translate (&tm
, xoff
, yoff
);
2422 pm
= pagectm (page
);
2423 fz_concat (&ctm
, &pm
, &tm
);
2425 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2426 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2428 for (link
= links
; link
; link
= link
->next
) {
2429 fz_point p1
, p2
, p3
, p4
;
2431 p1
.x
= link
->rect
.x0
;
2432 p1
.y
= link
->rect
.y0
;
2434 p2
.x
= link
->rect
.x1
;
2435 p2
.y
= link
->rect
.y0
;
2437 p3
.x
= link
->rect
.x1
;
2438 p3
.y
= link
->rect
.y1
;
2440 p4
.x
= link
->rect
.x0
;
2441 p4
.y
= link
->rect
.y1
;
2443 switch (link
->dest
.kind
) {
2444 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2445 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2446 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2447 default: glColor3ub (0, 0, 0); break;
2449 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2452 for (i
= 0; i
< page
->annotcount
; ++i
) {
2453 fz_point p1
, p2
, p3
, p4
;
2454 struct annot
*annot
= &page
->annots
[i
];
2456 p1
.x
= annot
->bbox
.x0
;
2457 p1
.y
= annot
->bbox
.y0
;
2459 p2
.x
= annot
->bbox
.x1
;
2460 p2
.y
= annot
->bbox
.y0
;
2462 p3
.x
= annot
->bbox
.x1
;
2463 p3
.y
= annot
->bbox
.y1
;
2465 p4
.x
= annot
->bbox
.x0
;
2466 p4
.y
= annot
->bbox
.y1
;
2468 glColor3ub (0, 0, 128);
2469 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2472 glDisable (GL_BLEND
);
2473 glDisable (GL_TEXTURE_1D
);
2476 static int compareslinks (const void *l
, const void *r
)
2478 struct slink
const *ls
= l
;
2479 struct slink
const *rs
= r
;
2480 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2481 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2483 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2486 static void droptext (struct page
*page
)
2489 fz_free_text_page (state
.ctx
, page
->text
);
2492 page
->fmark
.span
= NULL
;
2493 page
->lmark
.span
= NULL
;
2497 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2502 static void dropanots (struct page
*page
)
2505 free (page
->annots
);
2506 page
->annots
= NULL
;
2507 page
->annotcount
= 0;
2511 static void ensureanots (struct page
*page
)
2515 size_t anotsize
= sizeof (*page
->annots
);
2518 if (state
.gen
!= page
->sgen
) {
2520 page
->sgen
= state
.gen
;
2522 if (page
->annots
) return;
2524 switch (page
->type
) {
2526 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2528 &state
.pagedims
[page
->pdimno
].tctm
,
2529 &state
.pagedims
[page
->pdimno
].ctm
);
2536 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
);
2538 annot
= pdf_next_annot (state
.u
.pdf
, annot
)) {
2543 page
->annotcount
= count
;
2544 page
->annots
= calloc (count
, anotsize
);
2545 if (!page
->annots
) {
2546 err (1, "calloc annots %d", count
);
2549 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
), i
= 0;
2551 annot
= pdf_next_annot (state
.u
.pdf
, annot
), i
++) {
2554 pdf_bound_annot (state
.u
.pdf
, annot
, &rect
);
2555 fz_transform_rect (&rect
, &ctm
);
2556 page
->annots
[i
].annot
= annot
;
2557 fz_round_rect (&page
->annots
[i
].bbox
, &annot
->pagerect
);
2562 static void dropslinks (struct page
*page
)
2565 free (page
->slinks
);
2566 page
->slinks
= NULL
;
2567 page
->slinkcount
= 0;
2571 static void ensureslinks (struct page
*page
)
2575 size_t slinksize
= sizeof (*page
->slinks
);
2576 fz_link
*link
, *links
;
2579 if (state
.gen
!= page
->sgen
) {
2581 page
->sgen
= state
.gen
;
2583 if (page
->slinks
) return;
2585 switch (page
->type
) {
2587 links
= page
->u
.pdfpage
->links
;
2588 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2590 &state
.pagedims
[page
->pdimno
].tctm
,
2591 &state
.pagedims
[page
->pdimno
].ctm
);
2595 links
= page
->u
.xpspage
->links
;
2596 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2603 count
= page
->annotcount
;
2604 for (link
= links
; link
; link
= link
->next
) {
2610 page
->slinkcount
= count
;
2611 page
->slinks
= calloc (count
, slinksize
);
2612 if (!page
->slinks
) {
2613 err (1, "calloc slinks %d", count
);
2616 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2620 fz_transform_rect (&rect
, &ctm
);
2621 page
->slinks
[i
].tag
= SLINK
;
2622 page
->slinks
[i
].u
.link
= link
;
2623 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2625 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2628 rect
= page
->annots
[j
].annot
->pagerect
;
2629 fz_transform_rect (&rect
, &ctm
);
2630 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2632 page
->slinks
[i
].tag
= SANNOT
;
2633 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2635 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2639 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2640 static void fmt_linkn (char *s
, unsigned int u
)
2642 unsigned int len
; unsigned int q
;
2643 unsigned int zma
= 'z' - 'a' + 1;
2645 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2648 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2649 /* handles u == 0 */
2654 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2655 int noff
, char *targ
, int tlen
, int hfsize
)
2659 struct slink
*slink
;
2660 double x0
, y0
, x1
, y1
, w
;
2662 ensureslinks (page
);
2663 glColor3ub (0xc3, 0xb0, 0x91);
2664 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2665 fmt_linkn (buf
, i
+ noff
);
2666 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2667 slink
= &page
->slinks
[i
];
2669 x0
= slink
->bbox
.x0
+ xoff
- 5;
2670 y1
= slink
->bbox
.y0
+ yoff
- 5;
2671 y0
= y1
+ 10 + hfsize
;
2672 w
= measure_string (state
.face
, hfsize
, buf
);
2674 recti (x0
, y0
, x1
, y1
);
2678 glEnable (GL_BLEND
);
2679 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2680 glEnable (GL_TEXTURE_2D
);
2681 glColor3ub (0, 0, 0);
2682 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2683 fmt_linkn (buf
, i
+ noff
);
2684 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2685 slink
= &page
->slinks
[i
];
2687 x0
= slink
->bbox
.x0
+ xoff
;
2688 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2689 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2692 glDisable (GL_TEXTURE_2D
);
2693 glDisable (GL_BLEND
);
2696 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2699 struct slice
*slice1
;
2700 unsigned char *texdata
;
2703 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2704 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2706 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2707 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2708 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2712 int texindex
= state
.texindex
++ % state
.texcount
;
2714 if (state
.texowners
[texindex
].w
== tile
->w
) {
2715 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2719 state
.texowners
[texindex
].h
= slice
->h
;
2723 state
.texowners
[texindex
].h
= slice
->h
;
2726 state
.texowners
[texindex
].w
= tile
->w
;
2727 state
.texowners
[texindex
].slice
= slice
;
2728 slice
->texindex
= texindex
;
2730 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2732 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2736 texdata
= tile
->pixmap
->samples
;
2739 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2751 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2763 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2768 CAMLprim value
ml_begintiles (value unit_v
)
2770 CAMLparam1 (unit_v
);
2771 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2772 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2773 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2774 CAMLreturn (unit_v
);
2777 CAMLprim value
ml_endtiles (value unit_v
)
2779 CAMLparam1 (unit_v
);
2780 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2781 CAMLreturn (unit_v
);
2784 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2786 CAMLparam2 (args_v
, ptr_v
);
2787 int dispx
= Int_val (Field (args_v
, 0));
2788 int dispy
= Int_val (Field (args_v
, 1));
2789 int dispw
= Int_val (Field (args_v
, 2));
2790 int disph
= Int_val (Field (args_v
, 3));
2791 int tilex
= Int_val (Field (args_v
, 4));
2792 int tiley
= Int_val (Field (args_v
, 5));
2793 char *s
= String_val (ptr_v
);
2794 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2795 int slicey
, firstslice
;
2796 struct slice
*slice
;
2797 GLfloat
*texcoords
= state
.texcoords
;
2798 GLfloat
*vertices
= state
.vertices
;
2800 firstslice
= tiley
/ tile
->sliceheight
;
2801 slice
= &tile
->slices
[firstslice
];
2802 slicey
= tiley
% tile
->sliceheight
;
2807 dh
= slice
->h
- slicey
;
2808 dh
= MIN (disph
, dh
);
2809 uploadslice (tile
, slice
);
2811 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2812 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2813 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2814 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2816 vertices
[0] = dispx
; vertices
[1] = dispy
;
2817 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2818 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2819 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2821 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2825 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2828 CAMLreturn (Val_unit
);
2831 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2832 value xoff_v
, value yoff_v
,
2835 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2836 int xoff
= Int_val (xoff_v
);
2837 int yoff
= Int_val (yoff_v
);
2838 int noff
= Int_val (Field (li_v
, 0));
2839 char *targ
= String_val (Field (li_v
, 1));
2840 int tlen
= caml_string_length (Field (li_v
, 1));
2841 int hfsize
= Int_val (Field (li_v
, 2));
2842 char *s
= String_val (ptr_v
);
2843 int hlmask
= Int_val (hlinks_v
);
2844 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2847 /* deal with loadpage failed pages */
2853 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2854 if (trylock ("ml_postprocess")) {
2859 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2860 noff
= page
->slinkcount
;
2862 if (page
->tgen
== state
.gen
) {
2863 showsel (page
, xoff
, yoff
);
2865 unlock ("ml_postprocess");
2868 CAMLreturn (Val_int (noff
));
2871 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2876 const fz_matrix
*tctm
;
2878 if (!page
->annots
) return NULL
;
2880 switch (page
->type
) {
2882 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2883 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2887 tctm
= &fz_identity
;
2897 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2898 fz_invert_matrix (&ctm
, &ctm
);
2899 fz_transform_point (&p
, &ctm
);
2901 for (i
= 0; i
< page
->annotcount
; ++i
) {
2902 struct annot
*a
= &page
->annots
[i
];
2903 if (p
.x
>= a
->annot
->pagerect
.x0
&& p
.x
<= a
->annot
->pagerect
.x1
) {
2904 if (p
.y
>= a
->annot
->pagerect
.y0
&& p
.y
<= a
->annot
->pagerect
.y1
) {
2912 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2916 const fz_matrix
*tctm
;
2917 fz_link
*link
, *links
;
2919 switch (page
->type
) {
2921 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2922 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2923 links
= page
->u
.pdfpage
->links
;
2927 tctm
= &fz_identity
;
2928 links
= page
->u
.xpspage
->links
;
2937 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2938 fz_invert_matrix (&ctm
, &ctm
);
2939 fz_transform_point (&p
, &ctm
);
2941 for (link
= links
; link
; link
= link
->next
) {
2942 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2943 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2951 static void ensuretext (struct page
*page
)
2953 if (state
.gen
!= page
->tgen
) {
2955 page
->tgen
= state
.gen
;
2961 page
->text
= fz_new_text_page (state
.ctx
);
2962 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2963 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2964 ctm
= pagectm (page
);
2965 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2966 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2967 qsort (page
->text
->blocks
, page
->text
->len
,
2968 sizeof (*page
->text
->blocks
), compareblocks
);
2970 fz_free_device (tdev
);
2974 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2976 CAMLparam2 (start_page_v
, dir_v
);
2978 int i
, dir
= Int_val (dir_v
);
2979 int start_page
= Int_val (start_page_v
);
2980 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2982 ret_v
= Val_int (0);
2983 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2987 lock ("ml_findpage_with_links");
2988 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2991 switch (state
.type
) {
2994 pdf_page
*page
= NULL
;
2996 fz_try (state
.ctx
) {
2997 page
= pdf_load_page (state
.u
.pdf
, i
);
2998 found
= !!page
->links
|| !!page
->annots
;
3000 fz_catch (state
.ctx
) {
3010 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
3011 found
= !!page
->links
;
3017 ARSERT (0 && "invalid document type");
3021 ret_v
= caml_alloc_small (1, 1);
3022 Field (ret_v
, 0) = Val_int (i
);
3027 unlock ("ml_findpage_with_links");
3033 enum { dir_first
, dir_last
};
3034 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
3036 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
3038 CAMLparam2 (ptr_v
, dir_v
);
3039 CAMLlocal2 (ret_v
, pos_v
);
3041 int dirtag
, i
, slinkindex
;
3042 struct slink
*found
= NULL
,*slink
;
3043 char *s
= String_val (ptr_v
);
3045 page
= parse_pointer ("ml_findlink", s
);
3046 ret_v
= Val_int (0);
3047 if (trylock ("ml_findlink")) {
3051 ensureslinks (page
);
3053 if (Is_block (dir_v
)) {
3054 dirtag
= Tag_val (dir_v
);
3056 case dir_first_visible
:
3058 int x0
, y0
, dir
, first_index
, last_index
;
3060 pos_v
= Field (dir_v
, 0);
3061 x0
= Int_val (Field (pos_v
, 0));
3062 y0
= Int_val (Field (pos_v
, 1));
3063 dir
= Int_val (Field (pos_v
, 2));
3068 last_index
= page
->slinkcount
;
3071 first_index
= page
->slinkcount
- 1;
3075 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
3076 slink
= &page
->slinks
[i
];
3077 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
3086 slinkindex
= Int_val (Field (dir_v
, 0));
3087 found
= &page
->slinks
[slinkindex
];
3088 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3089 slink
= &page
->slinks
[i
];
3090 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
3098 slinkindex
= Int_val (Field (dir_v
, 0));
3099 found
= &page
->slinks
[slinkindex
];
3100 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3101 slink
= &page
->slinks
[i
];
3102 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
3110 slinkindex
= Int_val (Field (dir_v
, 0));
3111 found
= &page
->slinks
[slinkindex
];
3112 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3113 slink
= &page
->slinks
[i
];
3114 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
3122 slinkindex
= Int_val (Field (dir_v
, 0));
3123 found
= &page
->slinks
[slinkindex
];
3124 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3125 slink
= &page
->slinks
[i
];
3126 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
3135 dirtag
= Int_val (dir_v
);
3138 found
= page
->slinks
;
3143 found
= page
->slinks
+ (page
->slinkcount
- 1);
3149 ret_v
= caml_alloc_small (2, 1);
3150 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
3153 unlock ("ml_findlink");
3158 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
3159 unamed
, uremote
, uremotedest
, uannot
};
3165 switch (link->dest.kind) { \
3166 case FZ_LINK_GOTO: \
3170 pageno = link->dest.ld.gotor.page; \
3174 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
3175 p.y = link->dest.ld.gotor.lt.y; \
3176 fz_transform_point (&p, &pdim->lctm); \
3177 if (p.y < 0) p.y = 0; \
3179 tup_v = caml_alloc_tuple (2); \
3180 ret_v = caml_alloc_small (1, ugoto); \
3181 Field (tup_v, 0) = Val_int (pageno); \
3182 Field (tup_v, 1) = Val_int (p.y); \
3183 Field (ret_v, 0) = tup_v; \
3188 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3189 ret_v = caml_alloc_small (1, uuri); \
3190 Field (ret_v, 0) = str_v; \
3193 case FZ_LINK_LAUNCH: \
3194 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3195 ret_v = caml_alloc_small (1, ulaunch); \
3196 Field (ret_v, 0) = str_v; \
3199 case FZ_LINK_NAMED: \
3200 str_v = caml_copy_string (link->dest.ld.named.named); \
3201 ret_v = caml_alloc_small (1, unamed); \
3202 Field (ret_v, 0) = str_v; \
3205 case FZ_LINK_GOTOR: \
3209 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3210 pageno = link->dest.ld.gotor.page; \
3211 if (pageno == -1) { \
3212 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3213 rty = uremotedest; \
3216 gr_v = Val_int (pageno); \
3219 tup_v = caml_alloc_tuple (2); \
3220 ret_v = caml_alloc_small (1, rty); \
3221 Field (tup_v, 0) = str_v; \
3222 Field (tup_v, 1) = gr_v; \
3223 Field (ret_v, 0) = tup_v; \
3231 snprintf (buf, sizeof (buf), \
3232 "unhandled link kind %d", link->dest.kind); \
3233 str_v = caml_copy_string (buf); \
3234 ret_v = caml_alloc_small (1, uunexpected); \
3235 Field (ret_v, 0) = str_v; \
3241 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3243 CAMLparam2 (ptr_v
, n_v
);
3244 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3247 struct pagedim
*pdim
;
3248 char *s
= String_val (ptr_v
);
3249 struct slink
*slink
;
3251 ret_v
= Val_int (0);
3252 if (trylock ("ml_getlink")) {
3256 page
= parse_pointer ("ml_getlink", s
);
3257 ensureslinks (page
);
3258 pdim
= &state
.pagedims
[page
->pdimno
];
3259 slink
= &page
->slinks
[Int_val (n_v
)];
3260 if (slink
->tag
== SLINK
) {
3261 link
= slink
->u
.link
;
3265 str_v
= caml_copy_string (
3266 pdf_annot_contents (state
.u
.pdf
, slink
->u
.annot
)
3268 ret_v
= caml_alloc_small (1, uannot
);
3269 Field (ret_v
, 0) = str_v
;
3272 unlock ("ml_getlink");
3277 CAMLprim value
ml_getlinkcount (value ptr_v
)
3281 char *s
= String_val (ptr_v
);
3283 page
= parse_pointer ("ml_getlinkcount", s
);
3284 CAMLreturn (Val_int (page
->slinkcount
));
3287 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3289 CAMLparam2 (ptr_v
, n_v
);
3292 struct slink
*slink
;
3293 char *s
= String_val (ptr_v
);
3295 page
= parse_pointer ("ml_getlinkrect", s
);
3296 ret_v
= caml_alloc_tuple (4);
3297 if (trylock ("ml_getlinkrect")) {
3298 Field (ret_v
, 0) = Val_int (0);
3299 Field (ret_v
, 1) = Val_int (0);
3300 Field (ret_v
, 2) = Val_int (0);
3301 Field (ret_v
, 3) = Val_int (0);
3304 ensureslinks (page
);
3306 slink
= &page
->slinks
[Int_val (n_v
)];
3307 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3308 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3309 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3310 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3311 unlock ("ml_getlinkrect");
3317 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3319 CAMLparam3 (ptr_v
, x_v
, y_v
);
3320 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3322 struct annot
*annot
;
3324 char *ptr
= String_val (ptr_v
);
3325 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3326 struct pagedim
*pdim
;
3328 ret_v
= Val_int (0);
3329 if (trylock ("ml_whatsunder")) {
3333 page
= parse_pointer ("ml_whatsunder", ptr
);
3334 pdim
= &state
.pagedims
[page
->pdimno
];
3335 x
+= pdim
->bounds
.x0
;
3336 y
+= pdim
->bounds
.y0
;
3338 if (state
.type
== DPDF
) {
3339 annot
= getannot (page
, x
, y
);
3341 str_v
= caml_copy_string (
3342 pdf_annot_contents (state
.u
.pdf
, annot
->annot
)
3344 ret_v
= caml_alloc_small (1, uannot
);
3345 Field (ret_v
, 0) = str_v
;
3350 link
= getlink (page
, x
, y
);
3356 fz_page_block
*pageb
;
3357 fz_text_block
*block
;
3360 for (pageb
= page
->text
->blocks
;
3361 pageb
< page
->text
->blocks
+ page
->text
->len
;
3364 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3365 block
= pageb
->u
.text
;
3368 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3371 for (line
= block
->lines
;
3372 line
< block
->lines
+ block
->len
;
3377 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3380 for (span
= line
->first_span
; span
; span
= span
->next
) {
3384 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3387 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3389 fz_text_char_bbox (&bbox
, span
, charnum
);
3392 if (x
>= b
->x0
&& x
<= b
->x1
3393 && y
>= b
->y0
&& y
<= b
->y1
) {
3394 fz_text_style
*style
= span
->text
->style
;
3396 style
->font
&& style
->font
->name
3398 : "Span has no font name"
3400 FT_FaceRec
*face
= style
->font
->ft_face
;
3401 if (face
&& face
->family_name
) {
3403 char *n1
= face
->family_name
;
3404 size_t l1
= strlen (n1
);
3405 size_t l2
= strlen (n2
);
3407 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3408 s
= malloc (l1
+ l2
+ 2);
3412 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3413 str_v
= caml_copy_string (s
);
3418 if (str_v
== Val_unit
) {
3419 str_v
= caml_copy_string (n2
);
3421 ret_v
= caml_alloc_small (1, utext
);
3422 Field (ret_v
, 0) = str_v
;
3431 unlock ("ml_whatsunder");
3437 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3439 static int uninteresting (int c
)
3441 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3445 CAMLprim value
ml_clearmark (value ptr_v
)
3448 char *s
= String_val (ptr_v
);
3451 if (trylock ("ml_clearmark")) {
3455 page
= parse_pointer ("ml_clearmark", s
);
3456 page
->fmark
.span
= NULL
;
3457 page
->lmark
.span
= NULL
;
3461 unlock ("ml_clearmark");
3463 CAMLreturn (Val_unit
);
3466 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3468 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3473 fz_page_block
*pageb
;
3474 fz_text_block
*block
;
3475 struct pagedim
*pdim
;
3476 int mark
= Int_val (mark_v
);
3477 char *s
= String_val (ptr_v
);
3478 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3480 ret_v
= Val_bool (0);
3481 if (trylock ("ml_markunder")) {
3485 page
= parse_pointer ("ml_markunder", s
);
3486 pdim
= &state
.pagedims
[page
->pdimno
];
3490 if (mark
== mark_page
) {
3492 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3494 for (i
= 0; i
< page
->text
->len
; ++i
) {
3495 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3496 pb1
= &page
->text
->blocks
[i
];
3500 if (!pb1
) goto unlock
;
3502 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3503 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3504 pb2
= &page
->text
->blocks
[i
];
3508 if (!pb2
) goto unlock
;
3510 block
= pb1
->u
.text
;
3513 page
->fmark
.span
= block
->lines
->first_span
;
3515 block
= pb2
->u
.text
;
3516 line
= &block
->lines
[block
->len
- 1];
3517 page
->lmark
.i
= line
->last_span
->len
- 1;
3518 page
->lmark
.span
= line
->last_span
;
3519 ret_v
= Val_bool (1);
3523 x
+= pdim
->bounds
.x0
;
3524 y
+= pdim
->bounds
.y0
;
3526 for (pageb
= page
->text
->blocks
;
3527 pageb
< page
->text
->blocks
+ page
->text
->len
;
3529 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3530 block
= pageb
->u
.text
;
3533 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3536 if (mark
== mark_block
) {
3538 page
->fmark
.span
= block
->lines
->first_span
;
3540 line
= &block
->lines
[block
->len
- 1];
3541 page
->lmark
.i
= line
->last_span
->len
- 1;
3542 page
->lmark
.span
= line
->last_span
;
3543 ret_v
= Val_bool (1);
3547 for (line
= block
->lines
;
3548 line
< block
->lines
+ block
->len
;
3553 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3556 if (mark
== mark_line
) {
3558 page
->fmark
.span
= line
->first_span
;
3560 page
->lmark
.i
= line
->last_span
->len
- 1;
3561 page
->lmark
.span
= line
->last_span
;
3562 ret_v
= Val_bool (1);
3566 for (span
= line
->first_span
; span
; span
= span
->next
) {
3570 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3573 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3575 fz_text_char_bbox (&bbox
, span
, charnum
);
3578 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3580 int charnum2
, charnum3
= -1, charnum4
= -1;
3582 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3584 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3585 if (uninteresting (span
->text
[charnum2
].c
)) {
3586 charnum3
= charnum2
+ 1;
3590 if (charnum3
== -1) charnum3
= 0;
3592 for (charnum2
= charnum
+ 1;
3593 charnum2
< span
->len
;
3595 if (uninteresting (span
->text
[charnum2
].c
)) break;
3596 charnum4
= charnum2
;
3598 if (charnum4
== -1) goto unlock
;
3600 page
->fmark
.i
= charnum3
;
3601 page
->fmark
.span
= span
;
3603 page
->lmark
.i
= charnum4
;
3604 page
->lmark
.span
= span
;
3605 ret_v
= Val_bool (1);
3613 if (!Bool_val (ret_v
)) {
3614 page
->fmark
.span
= NULL
;
3615 page
->lmark
.span
= NULL
;
3619 unlock ("ml_markunder");
3625 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3627 CAMLparam3 (ptr_v
, x_v
, y_v
);
3628 CAMLlocal2 (ret_v
, res_v
);
3631 fz_page_block
*pageb
;
3632 struct pagedim
*pdim
;
3633 char *s
= String_val (ptr_v
);
3634 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3636 ret_v
= Val_int (0);
3637 if (trylock ("ml_rectofblock")) {
3641 page
= parse_pointer ("ml_rectofblock", s
);
3642 pdim
= &state
.pagedims
[page
->pdimno
];
3643 x
+= pdim
->bounds
.x0
;
3644 y
+= pdim
->bounds
.y0
;
3648 for (pageb
= page
->text
->blocks
;
3649 pageb
< page
->text
->blocks
+ page
->text
->len
;
3651 switch (pageb
->type
) {
3652 case FZ_PAGE_BLOCK_TEXT
:
3653 b
= &pageb
->u
.text
->bbox
;
3656 case FZ_PAGE_BLOCK_IMAGE
:
3657 b
= &pageb
->u
.image
->bbox
;
3664 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3669 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3670 ret_v
= caml_alloc_small (1, 1);
3671 Store_double_field (res_v
, 0, b
->x0
);
3672 Store_double_field (res_v
, 1, b
->x1
);
3673 Store_double_field (res_v
, 2, b
->y0
);
3674 Store_double_field (res_v
, 3, b
->y1
);
3675 Field (ret_v
, 0) = res_v
;
3677 unlock ("ml_rectofblock");
3683 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3685 CAMLparam2 (ptr_v
, rect_v
);
3688 struct pagedim
*pdim
;
3689 char *s
= String_val (ptr_v
);
3690 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3692 fz_page_block
*pageb
;
3693 fz_text_block
*block
;
3694 fz_text_span
*span
, *fspan
, *lspan
;
3696 if (trylock ("ml_seltext")) {
3700 page
= parse_pointer ("ml_seltext", s
);
3703 pdim
= &state
.pagedims
[page
->pdimno
];
3704 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3705 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3706 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3707 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3718 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3719 glColor3ub (128, 128, 128);
3720 recti (x0
, y0
, x1
, y1
);
3721 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3725 fspan
= page
->fmark
.span
;
3728 lspan
= page
->lmark
.span
;
3730 for (pageb
= page
->text
->blocks
;
3731 pageb
< page
->text
->blocks
+ page
->text
->len
;
3733 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3734 block
= pageb
->u
.text
;
3735 for (line
= block
->lines
;
3736 line
< block
->lines
+ block
->len
;
3739 for (span
= line
->first_span
; span
; span
= span
->next
) {
3740 for (i
= 0; i
< span
->len
; ++i
) {
3743 fz_text_char_bbox (&b
, span
, i
);
3745 if (x0
>= b
.x0
&& x0
<= b
.x1
3746 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3751 if (x1
>= b
.x0
&& x1
<= b
.x1
3752 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3757 if (0 && selected
) {
3758 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3759 glColor3ub (128, 128, 128);
3760 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3761 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3767 if (x1
< x0
&& fspan
== lspan
) {
3779 page
->fmark
.span
= fspan
;
3782 page
->lmark
.span
= lspan
;
3784 unlock ("ml_seltext");
3787 CAMLreturn (Val_unit
);
3790 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3795 for (i
= a
; i
<= b
; ++i
) {
3796 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3797 ret
= fwrite (buf
, len
, 1, f
);
3800 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3801 len
, ret
, strerror (errno
));
3809 CAMLprim value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3811 caml_failwith ("ml_popen not implemented under Cygwin");
3814 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3816 CAMLparam2 (command_v
, fds_v
);
3817 CAMLlocal2 (l_v
, tup_v
);
3820 value earg_v
= Nothing
;
3821 posix_spawnattr_t attr
;
3822 posix_spawn_file_actions_t fa
;
3823 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3825 argv
[2] = String_val (command_v
);
3827 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3828 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3831 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3832 msg
= "posix_spawnattr_init";
3836 #ifdef POSIX_SPAWN_USEVFORK
3837 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3838 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3843 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3846 tup_v
= Field (l_v
, 0);
3847 fd1
= Int_val (Field (tup_v
, 0));
3848 fd2
= Int_val (Field (tup_v
, 1));
3850 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3851 msg
= "posix_spawn_file_actions_addclose";
3857 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3858 msg
= "posix_spawn_file_actions_adddup2";
3865 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3866 msg
= "posix_spawn";
3871 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3872 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3876 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3877 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3882 unix_error (ret
, msg
, earg_v
);
3884 CAMLreturn (Val_unit
);
3888 CAMLprim value
ml_hassel (value ptr_v
)
3893 char *s
= String_val (ptr_v
);
3895 ret_v
= Val_bool (0);
3896 if (trylock ("ml_hassel")) {
3900 page
= parse_pointer ("ml_hassel", s
);
3901 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3902 unlock ("ml_hassel");
3907 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3909 CAMLparam2 (fd_v
, ptr_v
);
3914 fz_page_block
*pageb
;
3915 fz_text_block
*block
;
3916 int fd
= Int_val (fd_v
);
3917 char *s
= String_val (ptr_v
);
3919 if (trylock ("ml_copysel")) {
3923 page
= parse_pointer ("ml_copysel", s
);
3925 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3926 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3930 f
= fdopen (fd
, "w");
3932 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3933 fd
, strerror (errno
));
3937 for (pageb
= page
->text
->blocks
;
3938 pageb
< page
->text
->blocks
+ page
->text
->len
;
3940 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3941 block
= pageb
->u
.text
;
3942 for (line
= block
->lines
;
3943 line
< block
->lines
+ block
->len
;
3947 for (span
= line
->first_span
; span
; span
= span
->next
) {
3950 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3951 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3952 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3955 if (pipespan (f
, span
, a
, b
)) {
3958 if (span
== page
->lmark
.span
) {
3961 if (span
== line
->last_span
) {
3962 if (putc ('\n', f
) == EOF
) {
3964 "failed break line on sel pipe: %s\n",
3975 int ret
= fclose (f
);
3978 if (errno
!= ECHILD
) {
3979 fprintf (stderr
, "failed to close sel pipe: %s\n",
3985 unlock ("ml_copysel");
3990 fprintf (stderr
, "failed to close sel pipe: %s\n",
3994 CAMLreturn (Val_unit
);
3997 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3999 CAMLparam1 (pagedimno_v
);
4001 int pagedimno
= Int_val (pagedimno_v
);
4004 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
4005 if (trylock ("ml_getpdimrect")) {
4006 box
= fz_empty_rect
;
4009 box
= state
.pagedims
[pagedimno
].mediabox
;
4010 unlock ("ml_getpdimrect");
4013 Store_double_field (ret_v
, 0, box
.x0
);
4014 Store_double_field (ret_v
, 1, box
.x1
);
4015 Store_double_field (ret_v
, 2, box
.y0
);
4016 Store_double_field (ret_v
, 3, box
.y1
);
4021 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
4022 value dw_v
, value cols_v
)
4024 CAMLparam3 (winw_v
, winh_v
, dw_v
);
4030 double winw
= Int_val (winw_v
);
4031 double winh
= Int_val (winh_v
);
4032 double dw
= Int_val (dw_v
);
4033 double cols
= Int_val (cols_v
);
4034 double pw
= 1.0, ph
= 1.0;
4036 if (trylock ("ml_zoom_for_height")) {
4040 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
4041 double w
= p
->pagebox
.x1
/ cols
;
4042 double h
= p
->pagebox
.y1
;
4046 if (state
.fitmodel
!= FitProportional
) pw
= w
;
4048 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
4051 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
4052 unlock ("ml_zoom_for_height");
4054 ret_v
= caml_copy_double (zoom
);
4058 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
4060 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
4062 int pt
= Int_val(pt_v
);
4063 int x
= Int_val (x_v
);
4064 int y
= Int_val (y_v
);
4067 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
4068 ret_v
= caml_copy_double (w
);
4072 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
4074 CAMLparam2 (pt_v
, string_v
);
4076 int pt
= Int_val (pt_v
);
4079 w
= measure_string (state
.face
, pt
, String_val (string_v
));
4080 ret_v
= caml_copy_double (w
);
4084 CAMLprim value
ml_getpagebox (value opaque_v
)
4086 CAMLparam1 (opaque_v
);
4092 char *s
= String_val (opaque_v
);
4093 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
4095 ret_v
= caml_alloc_tuple (4);
4096 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
4097 dev
->hints
|= FZ_IGNORE_SHADE
;
4099 switch (page
->type
) {
4101 ctm
= pagectm (page
);
4102 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
4106 ctm
= pagectm (page
);
4107 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
4111 rect
= fz_infinite_rect
;
4115 fz_free_device (dev
);
4116 fz_round_rect (&bbox
, &rect
);
4117 Field (ret_v
, 0) = Val_int (bbox
.x0
);
4118 Field (ret_v
, 1) = Val_int (bbox
.y0
);
4119 Field (ret_v
, 2) = Val_int (bbox
.x1
);
4120 Field (ret_v
, 3) = Val_int (bbox
.y1
);
4125 CAMLprim value
ml_setaalevel (value level_v
)
4127 CAMLparam1 (level_v
);
4129 state
.aalevel
= Int_val (level_v
);
4130 CAMLreturn (Val_unit
);
4133 #pragma GCC diagnostic push
4134 #pragma GCC diagnostic ignored "-Wvariadic-macros"
4135 #include <X11/Xlib.h>
4136 #pragma GCC diagnostic pop
4144 XVisualInfo
*visual
;
4147 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4149 CAMLparam3 (display_v
, wid_v
, screen_v
);
4150 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4152 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4154 caml_failwith ("XOpenDisplay");
4157 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4159 XCloseDisplay (glx
.dpy
);
4160 caml_failwith ("glXChooseVisual");
4163 glx
.wid
= Int_val (wid_v
);
4164 CAMLreturn (Val_int (glx
.visual
->visualid
));
4167 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4169 CAMLparam1 (unit_v
);
4171 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4173 caml_failwith ("glXCreateContext");
4179 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4180 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4182 caml_failwith ("glXMakeCurrent");
4184 CAMLreturn (Val_unit
);
4187 CAMLprim value
ml_swapb (value unit_v
)
4189 CAMLparam1 (unit_v
);
4190 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4191 CAMLreturn (Val_unit
);
4194 #include "keysym2ucs.c"
4196 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4198 CAMLparam1 (keysym_v
);
4200 KeySym keysym
= Int_val (keysym_v
);
4205 rune
= keysym2ucs (keysym
);
4206 len
= fz_runetochar (buf
, rune
);
4208 str_v
= caml_copy_string (buf
);
4212 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4214 CAMLprim value
ml_platform (value unit_v
)
4216 CAMLparam1 (unit_v
);
4217 CAMLlocal2 (tup_v
, arr_v
);
4218 int platid
= piunknown
;
4221 #if defined __linux__
4223 #elif defined __CYGWIN__
4225 #elif defined __DragonFly__ || defined __FreeBSD__
4226 || defined __OpenBSD__
|| defined __NetBSD__
4228 #elif defined __sun__
4230 #elif defined __APPLE__
4233 if (uname (&buf
)) err (1, "uname");
4235 tup_v
= caml_alloc_tuple (2);
4237 char const *sar
[] = {
4244 arr_v
= caml_copy_string_array (sar
);
4246 Field (tup_v
, 0) = Val_int (platid
);
4247 Field (tup_v
, 1) = arr_v
;
4251 CAMLprim value
ml_cloexec (value fd_v
)
4254 int fd
= Int_val (fd_v
);
4256 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4257 uerror ("fcntl", Nothing
);
4259 CAMLreturn (Val_unit
);
4262 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4264 CAMLparam2 (w_v
, h_v
);
4267 int w
= Int_val (w_v
);
4268 int h
= Int_val (h_v
);
4269 int cs
= Int_val (cs_v
);
4271 if (state
.pbo_usable
) {
4272 pbo
= calloc (sizeof (*pbo
), 1);
4274 err (1, "calloc pbo");
4286 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4289 state
.glGenBuffersARB (1, &pbo
->id
);
4290 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4291 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4292 NULL
, GL_STREAM_DRAW
);
4293 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4295 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4297 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4298 state
.glDeleteBuffersARB (1, &pbo
->id
);
4300 ret_v
= caml_copy_string ("0");
4306 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4308 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4312 err (1, "malloc %d bytes failed", res
+1);
4314 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4316 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4318 ret_v
= caml_copy_string (s
);
4323 ret_v
= caml_copy_string ("0");
4328 CAMLprim value
ml_freepbo (value s_v
)
4331 char *s
= String_val (s_v
);
4332 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4335 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4337 tile
->pbo
->ptr
= NULL
;
4338 tile
->pbo
->size
= -1;
4340 CAMLreturn (Val_unit
);
4343 CAMLprim value
ml_unmappbo (value s_v
)
4346 char *s
= String_val (s_v
);
4347 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4350 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4351 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4352 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4354 tile
->pbo
->ptr
= NULL
;
4355 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4357 CAMLreturn (Val_unit
);
4360 static void setuppbo (void)
4362 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4363 state
.pbo_usable
= GGPA (glBindBufferARB
)
4364 && GGPA (glUnmapBufferARB
)
4365 && GGPA (glMapBufferARB
)
4366 && GGPA (glBufferDataARB
)
4367 && GGPA (glGenBuffersARB
)
4368 && GGPA (glDeleteBuffersARB
);
4372 CAMLprim value
ml_pbo_usable (value unit_v
)
4374 CAMLparam1 (unit_v
);
4375 CAMLreturn (Val_bool (state
.pbo_usable
));
4378 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4380 CAMLparam3 (ptr_v
, x_v
, y_v
);
4381 CAMLlocal2 (ret_v
, tup_v
);
4383 char *s
= String_val (ptr_v
);
4384 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4385 struct pagedim
*pdim
;
4389 page
= parse_pointer ("ml_unproject", s
);
4390 pdim
= &state
.pagedims
[page
->pdimno
];
4392 ret_v
= Val_int (0);
4393 if (trylock ("ml_unproject")) {
4397 switch (page
->type
) {
4399 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4405 p
.x
= x
+ pdim
->bounds
.x0
;
4406 p
.y
= y
+ pdim
->bounds
.y0
;
4408 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4409 fz_invert_matrix (&ctm
, &ctm
);
4410 fz_transform_point (&p
, &ctm
);
4412 tup_v
= caml_alloc_tuple (2);
4413 ret_v
= caml_alloc_small (1, 1);
4414 Field (tup_v
, 0) = Val_int (p
.x
);
4415 Field (tup_v
, 1) = Val_int (p
.y
);
4416 Field (ret_v
, 0) = tup_v
;
4418 unlock ("ml_unproject");
4423 static void makestippletex (void)
4425 const char pixels
[] = "\xff\xff\0\0";
4426 glGenTextures (1, &state
.stid
);
4427 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4428 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4429 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4442 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4444 return caml_copy_string (FZ_VERSION
);
4447 #ifdef USE_FONTCONFIG
4453 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4457 int UNUSED_ATTR needs_exact_metrics
)
4464 FcPattern
*pat
, *pat1
;
4466 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4467 name
, bold
, italic
, needs_exact_metrics
);
4470 fc
.config
= FcInitLoadConfigAndFonts ();
4472 lprintf ("FcInitLoadConfigAndFonts failed\n");
4476 if (!fc
.config
) return NULL
;
4478 size
= strlen (name
);
4479 if (bold
) size
+= sizeof (":bold") - 1;
4480 if (italic
) size
+= sizeof (":italic") - 1;
4483 buf
= malloc (size
);
4485 err (1, "malloc %zu failed", size
);
4489 if (bold
&& italic
) {
4490 strcat (buf
, ":bold:italic");
4493 if (bold
) strcat (buf
, ":bold");
4494 if (italic
) strcat (buf
, ":italic");
4496 for (i
= 0; i
< size
; ++i
) {
4497 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4500 lprintf ("fcbuf=%s\n", buf
);
4501 pat
= FcNameParse ((FcChar8
*) buf
);
4503 printd ("emsg FcNameParse failed\n");
4508 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4509 printd ("emsg FcConfigSubstitute failed\n");
4513 FcDefaultSubstitute (pat
);
4515 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4517 printd ("emsg FcFontMatch failed\n");
4518 FcPatternDestroy (pat
);
4523 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4524 printd ("emsg FcPatternGetString failed\n");
4525 FcPatternDestroy (pat
);
4526 FcPatternDestroy (pat1
);
4532 printd ("emsg name=%s path=%s\n", name
, path
);
4534 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4535 FcPatternDestroy (pat
);
4536 FcPatternDestroy (pat1
);
4542 CAMLprim value
ml_init (value csock_v
, value params_v
)
4544 CAMLparam2 (csock_v
, params_v
);
4545 CAMLlocal2 (trim_v
, fuzz_v
);
4552 struct sigaction sa
;
4554 state
.csock
= Int_val (csock_v
);
4555 state
.rotate
= Int_val (Field (params_v
, 0));
4556 state
.fitmodel
= Int_val (Field (params_v
, 1));
4557 trim_v
= Field (params_v
, 2);
4558 texcount
= Int_val (Field (params_v
, 3));
4559 state
.sliceheight
= Int_val (Field (params_v
, 4));
4560 mustoresize
= Int_val (Field (params_v
, 5));
4561 colorspace
= Int_val (Field (params_v
, 6));
4562 fontpath
= String_val (Field (params_v
, 7));
4564 if (caml_string_length (Field (params_v
, 8)) > 0) {
4565 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4567 if (!state
.trimcachepath
) {
4568 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4572 haspboext
= Bool_val (Field (params_v
, 9));
4574 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4576 #ifdef USE_FONTCONFIG
4577 if (Bool_val (Field (params_v
, 10))) {
4578 fz_install_load_system_font_funcs (
4579 state
.ctx
, fc_load_system_font_func
, NULL
4584 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4585 fuzz_v
= Field (trim_v
, 1);
4586 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4587 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4588 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4589 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4591 set_tex_params (colorspace
);
4594 #ifndef USE_FONTCONFIG
4595 state
.face
= load_font (fontpath
);
4599 char *buf
= fontpath
;
4600 FcPattern
*pat
, *pat1
;
4603 fc
.config
= FcInitLoadConfigAndFonts ();
4605 errx (1, "FcInitLoadConfigAndFonts failed");
4608 pat
= FcNameParse ((FcChar8
*) buf
);
4610 errx (1, "FcNameParse failed");
4613 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4614 errx (1, "FcConfigSubstitute failed");
4616 FcDefaultSubstitute (pat
);
4618 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4620 errx (1, "FcFontMatch failed");
4623 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4624 errx (1, "FcPatternGetString failed");
4627 state
.face
= load_font ((char *) path
);
4628 FcPatternDestroy (pat
);
4629 FcPatternDestroy (pat1
);
4634 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4636 state
.face
= load_builtin_font (base
, len
);
4638 if (!state
.face
) _exit (1);
4640 realloctexts (texcount
);
4649 sa
.sa_handler
= SIG_IGN
;
4650 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4652 sa
.sa_handler
= SIG_DFL
;
4653 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4655 if (sigemptyset (&sa
.sa_mask
)) {
4656 err (1, "sigemptyset");
4658 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4659 err (1, "sigaction");
4662 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4664 errx (1, "pthread_create: %s", strerror (ret
));
4667 CAMLreturn (Val_unit
);