1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
35 #include <CoreFoundation/CoreFoundation.h>
39 #include <OpenGL/gl.h>
44 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
48 #pragma GCC diagnostic ignored "-Wpedantic"
49 #include <caml/fail.h>
50 #include <caml/alloc.h>
51 #include <caml/memory.h>
52 #include <caml/unixsupport.h>
54 #if __GNUC__ < 5 && !defined __clang__
55 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
56 clobbered diagnostics */
57 #pragma GCC diagnostic ignored "-Wclobbered"
60 #include <mupdf/fitz.h>
61 #include <mupdf/pdf.h>
64 #include FT_FREETYPE_H
65 #pragma GCC diagnostic pop
68 #define CACHE_PAGEREFS
71 #define NORETURN_ATTR __attribute__ ((noreturn))
72 #define UNUSED_ATTR __attribute__ ((unused))
73 #if !defined __clang__
74 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
76 #define OPTIMIZE_ATTR(n)
78 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
82 #define OPTIMIZE_ATTR(n)
83 #define GCC_FMT_ATTR(a, b)
88 #define FMT_ptr PRIxPTR
89 #define SCN_ptr SCNxPTR
90 #define FMT_ptr_cast(p) ((uintptr_t) (p))
91 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
93 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
94 err (int exitcode
, const char *fmt
, ...)
101 vfprintf (stderr
, fmt
, ap
);
103 fprintf (stderr
, ": %s\n", strerror (savederrno
));
108 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
109 errx (int exitcode
, const char *fmt
, ...)
114 vfprintf (stderr
, fmt
, ap
);
116 fputc ('\n', stderr
);
121 #ifndef GL_TEXTURE_RECTANGLE_ARB
122 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
126 #define TEXT_TYPE GL_TEXTURE_2D
128 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
132 #define GL_BGRA 0x80E1
135 #ifndef GL_UNSIGNED_INT_8_8_8_8
136 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
139 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
140 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
144 #define lprintf printf
149 #define ARSERT(cond) for (;;) { \
151 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
167 struct slice slices
[1];
178 fz_matrix ctm
, zoomctm
, tctm
;
182 enum { SLINK
, SANNOT
} tag
;
203 fz_display_list
*dlist
;
206 struct slink
*slinks
;
208 struct annot
*annots
;
214 enum { FitWidth
, FitProportional
, FitPage
};
218 struct pagedim
*pagedims
;
233 fz_colorspace
*colorspace
;
264 void (*glBindBufferARB
) (GLenum
, GLuint
);
265 GLboolean (*glUnmapBufferARB
) (GLenum
);
266 void *(*glMapBufferARB
) (GLenum
, GLenum
);
267 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
268 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
269 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
271 GLfloat texcoords
[8];
272 GLfloat vertices
[16];
274 #ifdef CACHE_PAGEREFS
291 #pragma GCC diagnostic ignored "-Wdouble-promotion"
292 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
294 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
297 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
299 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
302 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
304 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
305 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
307 #pragma GCC diagnostic error "-Wdouble-promotion"
309 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
311 static void lock (const char *cap
)
313 int ret
= pthread_mutex_lock (&mutex
);
315 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
319 static void unlock (const char *cap
)
321 int ret
= pthread_mutex_unlock (&mutex
);
323 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
327 static int trylock (const char *cap
)
329 int ret
= pthread_mutex_trylock (&mutex
);
330 if (ret
&& ret
!= EBUSY
) {
331 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
336 static void *parse_pointer (const char *cap
, const char *s
)
341 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
343 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
348 static double now (void)
352 if (gettimeofday (&tv
, NULL
)) {
353 err (1, "gettimeofday");
355 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
358 static int hasdata (void)
361 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
362 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
366 CAMLprim value
ml_hasdata (value fd_v
)
371 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
372 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
373 CAMLreturn (Val_bool (avail
> 0));
376 static void readdata (int fd
, void *p
, int size
)
381 n
= read (fd
, p
, size
);
383 if (n
< 0 && errno
== EINTR
) goto again
;
384 if (!n
) errx (1, "EOF while reading");
385 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
389 static void writedata (int fd
, char *p
, int size
)
392 uint32_t size4
= size
;
393 struct iovec iov
[2] = {
394 { .iov_base
= &size4
, .iov_len
= 4 },
395 { .iov_base
= p
, .iov_len
= size
}
399 n
= writev (fd
, iov
, 2);
400 if (n
< 0 && errno
== EINTR
) goto again
;
402 if (!n
) errx (1, "EOF while writing data");
403 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
407 static int readlen (int fd
)
410 readdata (fd
, &u
, 4);
414 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
416 CAMLparam3 (fd_v
, bytes_v
, len_v
);
417 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
421 CAMLprim value
ml_rcmd (value fd_v
)
424 CAMLlocal1 (strdata_v
);
425 int fd
= Int_val (fd_v
);
426 int len
= readlen (fd
);
427 strdata_v
= caml_alloc_string (len
);
428 readdata (fd
, String_val (strdata_v
), len
);
429 CAMLreturn (strdata_v
);
432 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
435 int size
= sizeof (fbuf
), len
;
441 len
= vsnprintf (buf
, size
, fmt
, ap
);
445 if (len
< size
- 4) {
446 writedata (state
.csock
, buf
, len
);
452 err (1, "vsnprintf for `%s' failed", fmt
);
454 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
455 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
457 if (buf
!= fbuf
) free (buf
);
460 static void closedoc (void)
462 #ifdef CACHE_PAGEREFS
463 if (state
.pdflut
.objs
) {
464 for (int i
= 0; i
< state
.pdflut
.count
; ++i
) {
465 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
467 free (state
.pdflut
.objs
);
468 state
.pdflut
.objs
= NULL
;
469 state
.pdflut
.idx
= 0;
473 fz_drop_document (state
.ctx
, state
.doc
);
478 static int openxref (char *filename
, char *password
)
480 for (int i
= 0; i
< state
.texcount
; ++i
) {
481 state
.texowners
[i
].w
= -1;
482 state
.texowners
[i
].slice
= NULL
;
488 if (state
.pagedims
) {
489 free (state
.pagedims
);
490 state
.pagedims
= NULL
;
492 state
.pagedimcount
= 0;
494 fz_set_aa_level (state
.ctx
, state
.aalevel
);
495 state
.doc
= fz_open_document (state
.ctx
, filename
);
496 if (fz_needs_password (state
.ctx
, state
.doc
)) {
497 if (password
&& !*password
) {
502 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
504 printd ("pass fail");
509 fz_layout_document (state
.ctx
, state
.doc
, 460, 0, 12);
510 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
514 static void pdfinfo (void)
516 struct { char *tag
; char *name
; } metatbl
[] = {
517 { FZ_META_INFO_TITLE
, "Title" },
518 { FZ_META_INFO_AUTHOR
, "Author" },
519 { FZ_META_FORMAT
, "Format" },
520 { FZ_META_ENCRYPTION
, "Encryption" },
521 { "info:Creator", "Creator" },
522 { "info:Producer", "Producer" },
523 { "info:CreationDate", "Creation date" },
528 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
531 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
532 metatbl
[i
].tag
, buf
, len
);
535 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
538 buf
= realloc (buf
, need
+ 1);
539 if (!buf
) err (1, "pdfinfo realloc %d", need
+ 1);
550 static void unlinktile (struct tile
*tile
)
552 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
553 struct slice
*s
= &tile
->slices
[i
];
555 if (s
->texindex
!= -1) {
556 if (state
.texowners
[s
->texindex
].slice
== s
) {
557 state
.texowners
[s
->texindex
].slice
= NULL
;
563 static void freepage (struct page
*page
)
567 fz_drop_stext_page (state
.ctx
, page
->text
);
572 fz_drop_display_list (state
.ctx
, page
->dlist
);
573 fz_drop_page (state
.ctx
, page
->fzpage
);
577 static void freetile (struct tile
*tile
)
582 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
585 fz_drop_pixmap (state
.ctx
, state
.pig
);
587 state
.pig
= tile
->pixmap
;
592 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
601 static int cacheline32bytes
;
603 static void __attribute__ ((constructor
)) clcheck (void)
605 char **envp
= environ
;
610 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
612 cacheline32bytes
= auxv
[1] == 32;
618 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
620 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
621 if (cacheline32bytes
&& size
> 32) {
622 intptr_t a1
, a2
, diff
;
624 vector
unsigned char v
= vec_splat_u8 (-1);
625 vector
unsigned char *p
;
627 a1
= a2
= (intptr_t) pixmap
->samples
;
628 a2
= (a1
+ 31) & ~31;
633 while (a1
!= a2
) *(char *) a1
++ = 0xff;
634 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
635 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
637 vec_st (v
, i
+ 16, p
);
639 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
641 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
644 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
647 static void trimctm (pdf_page
*page
, int pindex
)
650 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
653 if (!pdim
->tctmready
) {
654 fz_rect realbox
, mediabox
;
655 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
657 fz_rotate (&rm
, -pdim
->rotate
);
658 fz_scale (&sm
, 1, -1);
659 fz_concat (&ctm
, &rm
, &sm
);
660 realbox
= pdim
->mediabox
;
661 fz_transform_rect (&realbox
, &ctm
);
662 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
663 fz_concat (&ctm1
, &ctm
, &tm
);
664 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
665 fz_invert_matrix (&im
, &page_ctm
);
666 fz_concat (&ctm
, &im
, &ctm1
);
672 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
675 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
677 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
678 if (pdf_specifics (state
.ctx
, state
.doc
)) {
679 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
680 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
683 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
684 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
689 static fz_matrix
pagectm (struct page
*page
)
691 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
694 static void *loadpage (int pageno
, int pindex
)
699 page
= calloc (sizeof (struct page
), 1);
701 err (1, "calloc page %d", pageno
);
704 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
705 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
707 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
708 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
711 fz_catch (state
.ctx
) {
714 fz_close_device (state
.ctx
, dev
);
715 fz_drop_device (state
.ctx
, dev
);
717 page
->pdimno
= pindex
;
718 page
->pageno
= pageno
;
719 page
->sgen
= state
.gen
;
720 page
->agen
= state
.gen
;
721 page
->tgen
= state
.gen
;
725 static struct tile
*alloctile (int h
)
731 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
732 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
733 tile
= calloc (tilesize
, 1);
735 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
737 for (int i
= 0; i
< slicecount
; ++i
) {
738 int sh
= fz_mini (h
, state
.sliceheight
);
739 tile
->slices
[i
].h
= sh
;
740 tile
->slices
[i
].texindex
= -1;
743 tile
->slicecount
= slicecount
;
744 tile
->sliceheight
= state
.sliceheight
;
748 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
756 struct pagedim
*pdim
;
758 tile
= alloctile (h
);
759 pdim
= &state
.pagedims
[page
->pdimno
];
764 bbox
.x1
= bbox
.x0
+ w
;
765 bbox
.y1
= bbox
.y0
+ h
;
768 if (state
.pig
->w
== w
770 && state
.pig
->colorspace
== state
.colorspace
) {
771 tile
->pixmap
= state
.pig
;
772 tile
->pixmap
->x
= bbox
.x0
;
773 tile
->pixmap
->y
= bbox
.y0
;
776 fz_drop_pixmap (state
.ctx
, state
.pig
);
783 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
784 &bbox
, NULL
, 1, pbo
->ptr
);
789 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
,
796 clearpixmap (tile
->pixmap
);
798 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
799 ctm
= pagectm (page
);
800 fz_rect_from_irect (&rect
, &bbox
);
801 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
802 fz_close_device (state
.ctx
, dev
);
803 fz_drop_device (state
.ctx
, dev
);
808 #ifdef CACHE_PAGEREFS
809 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
810 thanks to Robin Watts */
812 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
814 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
818 if (state
.pdflut
.idx
== state
.pagecount
) return;
820 kids
= pdf_dict_gets (ctx
, node
, "Kids");
821 len
= pdf_array_len (ctx
, kids
);
824 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
826 if (pdf_mark_obj (ctx
, node
))
827 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
828 for (int i
= 0; i
< len
; i
++) {
829 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
830 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
832 ? !strcmp (type
, "Pages")
833 : pdf_dict_gets (ctx
, kid
, "Kids")
834 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
835 pdf_collect_pages (doc
, kid
);
839 ? strcmp (type
, "Page") != 0
840 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
841 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
842 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
845 pdf_unmark_obj (ctx
, node
);
849 pdf_load_page_objs (pdf_document
*doc
)
851 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
852 pdf_trailer (state
.ctx
, doc
), "Root");
853 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
856 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
858 state
.pdflut
.idx
= 0;
859 pdf_collect_pages (doc
, node
);
863 static void initpdims (int wthack
)
867 fz_rect rootmediabox
= fz_empty_rect
;
868 int pageno
, trim
, show
;
869 int trimw
= 0, cxcount
;
870 fz_context
*ctx
= state
.ctx
;
871 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
878 if (state
.trimmargins
&& state
.trimcachepath
) {
879 trimf
= fopen (state
.trimcachepath
, "rb");
881 trimf
= fopen (state
.trimcachepath
, "wb");
886 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
887 cxcount
= state
.pagecount
;
889 cxcount
= fz_mini (state
.pagecount
, 1);
893 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
894 "Root/Pages/MediaBox");
895 pdf_to_rect (ctx
, obj
, &rootmediabox
);
898 #ifdef CACHE_PAGEREFS
899 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
900 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
901 if (!state
.pdflut
.objs
) {
902 err (1, "malloc pageobjs %zu %d %zu failed",
903 sizeof (*state
.pdflut
.objs
), cxcount
,
904 sizeof (*state
.pdflut
.objs
) * cxcount
);
906 state
.pdflut
.count
= cxcount
;
907 pdf_load_page_objs (pdf
);
908 state
.pdflut
.pdf
= pdf
;
912 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
915 fz_rect mediabox
= fz_empty_rect
;
919 pdf_obj
*pageref
, *pageobj
;
921 #ifdef CACHE_PAGEREFS
922 pageref
= state
.pdflut
.objs
[pageno
];
924 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
926 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
927 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
929 if (state
.trimmargins
) {
934 page
= pdf_load_page (ctx
, pdf
, pageno
);
935 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
936 trim
= state
.trimanew
|| !obj
;
940 fz_matrix ctm
, page_ctm
;
942 dev
= fz_new_bbox_device (ctx
, &rect
);
943 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
944 fz_invert_matrix (&ctm
, &page_ctm
);
945 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
946 fz_close_device (ctx
, dev
);
947 fz_drop_device (ctx
, dev
);
949 rect
.x0
+= state
.trimfuzz
.x0
;
950 rect
.x1
+= state
.trimfuzz
.x1
;
951 rect
.y0
+= state
.trimfuzz
.y0
;
952 rect
.y1
+= state
.trimfuzz
.y1
;
953 fz_transform_rect (&rect
, &ctm
);
954 fz_intersect_rect (&rect
, &mediabox
);
956 if (!fz_is_empty_rect (&rect
)) {
960 obj
= pdf_new_array (ctx
, pdf
, 4);
961 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
963 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
965 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
967 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
969 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
972 mediabox
.x0
= pdf_to_real (ctx
,
973 pdf_array_get (ctx
, obj
, 0));
974 mediabox
.y0
= pdf_to_real (ctx
,
975 pdf_array_get (ctx
, obj
, 1));
976 mediabox
.x1
= pdf_to_real (ctx
,
977 pdf_array_get (ctx
, obj
, 2));
978 mediabox
.y1
= pdf_to_real (ctx
,
979 pdf_array_get (ctx
, obj
, 3));
982 fz_drop_page (ctx
, &page
->super
);
983 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
985 printd ("progress %f Trimming %d",
986 (double) (pageno
+ 1) / state
.pagecount
,
991 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
999 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
1001 if (fz_is_empty_rect (&mediabox
)) {
1010 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1012 if (!fz_is_empty_rect (&cropbox
)) {
1017 fz_intersect_rect (&mediabox
, &cropbox
);
1022 if (fz_is_empty_rect (&rootmediabox
)) {
1024 "cannot find page size for page %d\n",
1028 mediabox
= rootmediabox
;
1035 if (state
.trimmargins
&& trimw
) {
1039 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1040 fz_bound_page (ctx
, page
, &mediabox
);
1041 if (state
.trimmargins
) {
1045 dev
= fz_new_bbox_device (ctx
, &rect
);
1046 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1047 fz_close_device (ctx
, dev
);
1048 fz_drop_device (ctx
, dev
);
1050 rect
.x0
+= state
.trimfuzz
.x0
;
1051 rect
.x1
+= state
.trimfuzz
.x1
;
1052 rect
.y0
+= state
.trimfuzz
.y0
;
1053 rect
.y1
+= state
.trimfuzz
.y1
;
1054 fz_intersect_rect (&rect
, &mediabox
);
1056 if (!fz_is_empty_rect (&rect
)) {
1060 fz_drop_page (ctx
, page
);
1062 printd ("progress %f loading %d",
1063 (double) (pageno
+ 1) / state
.pagecount
,
1070 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1072 err (1, "fwrite trim mediabox");
1078 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1080 err (1, "fread trim mediabox %d", pageno
);
1086 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1087 fz_bound_page (ctx
, page
, &mediabox
);
1088 fz_drop_page (ctx
, page
);
1090 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1092 printd ("progress %f Gathering dimensions %d",
1093 (double) (pageno
) / state
.pagecount
,
1098 fprintf (stderr
, "failed to load page %d\n", pageno
);
1104 if (state
.pagedimcount
== 0
1105 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
1106 , p
->rotate
!= rotate
)
1107 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1110 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1111 state
.pagedims
= realloc (state
.pagedims
, size
);
1112 if (!state
.pagedims
) {
1113 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1114 size
, state
.pagedimcount
+ 1);
1117 p
= &state
.pagedims
[state
.pagedimcount
++];
1119 p
->mediabox
= mediabox
;
1125 printd ("progress 1 %s %d pages in %f seconds",
1126 state
.trimmargins
? "Trimmed" : "Processed",
1127 state
.pagecount
, end
- start
);
1131 if (fclose (trimf
)) {
1137 static void layout (void)
1142 struct pagedim
*p
= NULL
;
1143 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
1145 if (state
.pagedimcount
== 0) return;
1147 switch (state
.fitmodel
) {
1148 case FitProportional
:
1149 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1152 p
= &state
.pagedims
[pindex
];
1153 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1155 fz_transform_rect (&box
, &rm
);
1157 x0
= fz_min (box
.x0
, box
.x1
);
1158 x1
= fz_max (box
.x0
, box
.x1
);
1161 maxw
= fz_max (w
, maxw
);
1162 zoom
= state
.w
/ maxw
;
1174 ARSERT (0 && state
.fitmodel
);
1177 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1181 p
= &state
.pagedims
[pindex
];
1182 fz_rotate (&ctm
, state
.rotate
);
1183 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1185 fz_transform_rect (&box
, &rm
);
1186 w
= box
.x1
- box
.x0
;
1187 switch (state
.fitmodel
) {
1188 case FitProportional
:
1189 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
1195 h
= box
.y1
- box
.y0
;
1197 zoom
= fz_min (zw
, zh
);
1198 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
1207 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1208 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1210 fz_rotate (&rm
, p
->rotate
);
1211 p
->pagebox
= p
->mediabox
;
1212 fz_transform_rect (&p
->pagebox
, &rm
);
1213 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1214 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1218 fz_transform_rect (&rect
, &ctm
);
1219 fz_round_rect (&p
->bounds
, &rect
);
1222 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1223 fz_scale (&sm
, zoom
, -zoom
);
1224 fz_concat (&ctm
, &tm
, &sm
);
1230 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1231 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1232 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1233 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1234 int boundw
= x1
- x0
;
1235 int boundh
= y1
- y0
;
1237 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1238 } while (p
-- != state
.pagedims
);
1241 struct pagedim
*pdimofpageno (int pageno
)
1243 struct pagedim
*pdim
= state
.pagedims
;
1245 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1246 if (state
.pagedims
[i
].pageno
> pageno
)
1248 pdim
= &state
.pagedims
[i
];
1253 static void recurse_outline (fz_outline
*outline
, int level
)
1256 if (outline
->page
>= 0) {
1257 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
1258 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
1259 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1260 fz_transform_point (&p
, &pdim
->ctm
);
1261 printd ("o %d %d %d %d %s",
1262 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
1265 printd ("on %d %s", level
, outline
->title
);
1267 if (outline
->down
) {
1268 recurse_outline (outline
->down
, level
+ 1);
1270 outline
= outline
->next
;
1274 static void process_outline (void)
1276 fz_outline
*outline
;
1278 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1280 state
.needoutline
= 0;
1281 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1283 recurse_outline (outline
, 0);
1284 fz_drop_outline (state
.ctx
, outline
);
1288 static char *strofline (fz_stext_line
*line
)
1293 size_t size
= 0, cap
= 80;
1295 p
= malloc (cap
+ 1);
1296 if (!p
) return NULL
;
1298 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1299 int n
= fz_runetochar (utf8
, ch
->c
);
1300 if (size
+ n
> cap
) {
1302 p
= realloc (p
, cap
+ 1);
1303 if (!p
) return NULL
;
1306 memcpy (p
+ size
, utf8
, n
);
1313 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1314 int stop
, int pageno
, double start
)
1320 p
= strofline (line
);
1323 ret
= regexec (re
, p
, 1, &rm
, 0);
1326 if (ret
!= REG_NOMATCH
) {
1329 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1330 printd ("msg regexec error `%.*s'",
1331 (int) size
, errbuf
);
1337 fz_point p1
, p2
, p3
, p4
;
1338 fz_rect s
= {0,0,0,0}, e
;
1342 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1343 o
+= fz_runelen (ch
->c
);
1349 for (;ch
; ch
= ch
->next
) {
1350 o
+= fz_runelen (ch
->c
);
1351 if (o
> rm
.rm_eo
) break;
1364 #pragma GCC diagnostic ignored "-Wdouble-promotion"
1366 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1373 printd ("progress 1 found at %d `%.*s' in %f sec",
1374 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1378 printd ("match %d %d %f %f %f %f %f %f %f %f",
1385 #pragma GCC diagnostic error "-Wdouble-promotion"
1391 /* wishful thinking function */
1392 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1395 fz_stext_page
*text
;
1396 struct pagedim
*pdim
;
1397 int stop
= 0, niters
= 0;
1400 fz_stext_block
*block
;
1403 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1404 if (niters
++ == 5) {
1407 printd ("progress 1 attention requested aborting search at %d",
1412 printd ("progress %f searching in page %d",
1413 (double) (pageno
+ 1) / state
.pagecount
,
1417 pdim
= pdimofpageno (pageno
);
1418 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1419 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1421 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1423 fz_matrix ctm
= pagectm1 (page
, pdim
);
1424 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1427 fz_close_device (state
.ctx
, tdev
);
1428 fz_drop_device (state
.ctx
, tdev
);
1431 for (block
= text
->first_block
; block
; block
= block
->next
) {
1432 fz_stext_line
*line
;
1434 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1435 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1436 if (line
->bbox
.y0
< y
+ 1) continue;
1438 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1440 case 1: stop
= 1; break;
1441 case -1: stop
= 1; goto endloop
;
1447 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1448 fz_stext_line
*line
;
1450 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1451 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1452 if (line
->bbox
.y0
< y
+ 1) continue;
1454 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1456 case 1: stop
= 1; break;
1457 case -1: stop
= 1; goto endloop
;
1472 fz_drop_stext_page (state
.ctx
, text
);
1473 fz_drop_page (state
.ctx
, page
);
1477 printd ("progress 1 no matches %f sec", end
- start
);
1479 printd ("clearrects");
1482 static void set_tex_params (int colorspace
)
1484 switch (colorspace
) {
1486 state
.texiform
= GL_RGBA8
;
1487 state
.texform
= GL_RGBA
;
1488 state
.texty
= GL_UNSIGNED_BYTE
;
1489 state
.colorspace
= fz_device_rgb (state
.ctx
);
1492 state
.texiform
= GL_RGBA8
;
1493 state
.texform
= GL_BGRA
;
1494 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1495 state
.texty
= GL_UNSIGNED_INT_8_8_8_8_REV
;
1497 state
.texty
= GL_UNSIGNED_INT_8_8_8_8
;
1499 state
.colorspace
= fz_device_bgr (state
.ctx
);
1502 state
.texiform
= GL_LUMINANCE_ALPHA
;
1503 state
.texform
= GL_LUMINANCE_ALPHA
;
1504 state
.texty
= GL_UNSIGNED_BYTE
;
1505 state
.colorspace
= fz_device_gray (state
.ctx
);
1508 errx (1, "invalid colorspce %d", colorspace
);
1512 static void realloctexts (int texcount
)
1516 if (texcount
== state
.texcount
) return;
1518 if (texcount
< state
.texcount
) {
1519 glDeleteTextures (state
.texcount
- texcount
,
1520 state
.texids
+ texcount
);
1523 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1524 state
.texids
= realloc (state
.texids
, size
);
1525 if (!state
.texids
) {
1526 err (1, "realloc texs %" FMT_s
, size
);
1529 state
.texowners
= (void *) (state
.texids
+ texcount
);
1530 if (texcount
> state
.texcount
) {
1531 glGenTextures (texcount
- state
.texcount
,
1532 state
.texids
+ state
.texcount
);
1533 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1534 state
.texowners
[i
].w
= -1;
1535 state
.texowners
[i
].slice
= NULL
;
1538 state
.texcount
= texcount
;
1542 static char *mbtoutf8 (char *s
)
1552 len
= mbstowcs (NULL
, s
, strlen (s
));
1557 if (len
== (size_t) -1) {
1558 printd ("emsg mbtoutf8: mbstowcs %d:%s\n", errno
, strerror (errno
));
1563 tmp
= calloc (len
, sizeof (wchar_t));
1565 printd ("emsg mbtoutf8: calloc(%zu, %zu) %d:%s",
1566 len
, sizeof (wchar_t), errno
, strerror (errno
));
1570 ret
= mbstowcs (tmp
, s
, len
);
1571 if (ret
== (size_t) -1) {
1572 printd ("emsg mbtoutf8: mbswcs %zu characters failed %d:%s\n",
1573 len
, errno
, strerror (errno
));
1579 for (i
= 0; i
< ret
; ++i
) {
1580 len
+= fz_runelen (tmp
[i
]);
1583 p
= r
= malloc (len
+ 1);
1585 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1590 for (i
= 0; i
< ret
; ++i
) {
1591 p
+= fz_runetochar (p
, tmp
[i
]);
1598 CAMLprim value
ml_mbtoutf8 (value s_v
)
1604 s
= String_val (s_v
);
1610 ret_v
= caml_copy_string (r
);
1616 static void * mainloop (void UNUSED_ATTR
*unused
)
1619 int len
, ret
, oldlen
= 0;
1624 len
= readlen (state
.csock
);
1626 errx (1, "readlen returned 0");
1629 if (oldlen
< len
+ 1) {
1630 p
= realloc (p
, len
+ 1);
1632 err (1, "realloc %d failed", len
+ 1);
1636 readdata (state
.csock
, p
, len
);
1639 if (!strncmp ("open", p
, 4)) {
1640 int wthack
, off
, usedoccss
, ok
= 0;
1647 ret
= sscanf (p
+ 5, " %d %d %d %n",
1648 &wthack
, &state
.cxack
, &usedoccss
, &off
);
1650 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1653 filename
= p
+ 5 + off
;
1654 filenamelen
= strlen (filename
);
1655 password
= filename
+ filenamelen
+ 1;
1657 if (password
[strlen (password
) + 1]) {
1658 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1662 fz_set_use_document_css (state
.ctx
, usedoccss
);
1663 fz_try (state
.ctx
) {
1664 ok
= openxref (filename
, password
);
1666 fz_catch (state
.ctx
) {
1667 utf8filename
= mbtoutf8 (filename
);
1668 printd ("msg Could not open %s", utf8filename
);
1669 if (utf8filename
!= filename
) {
1670 free (utf8filename
);
1681 utf8filename
= mbtoutf8 (filename
);
1682 printd ("msg Opened %s (press h/F1 to get help)",
1684 if (utf8filename
!= filename
) {
1685 free (utf8filename
);
1688 state
.needoutline
= 1;
1691 else if (!strncmp ("cs", p
, 2)) {
1694 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1696 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1699 set_tex_params (colorspace
);
1700 for (i
= 0; i
< state
.texcount
; ++i
) {
1701 state
.texowners
[i
].w
= -1;
1702 state
.texowners
[i
].slice
= NULL
;
1706 else if (!strncmp ("freepage", p
, 8)) {
1709 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1711 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1715 unlock ("freepage");
1717 else if (!strncmp ("freetile", p
, 8)) {
1720 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1722 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1726 unlock ("freetile");
1728 else if (!strncmp ("search", p
, 6)) {
1729 int icase
, pageno
, y
, len2
, forward
;
1733 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1734 &icase
, &pageno
, &y
, &forward
, &len2
);
1736 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1739 pattern
= p
+ 6 + len2
;
1740 ret
= regcomp (&re
, pattern
,
1741 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1746 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1747 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1750 search (&re
, pageno
, y
, forward
);
1754 else if (!strncmp ("geometry", p
, 8)) {
1758 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1760 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1767 for (int i
= 0; i
< state
.texcount
; ++i
) {
1768 state
.texowners
[i
].slice
= NULL
;
1771 state
.fitmodel
= fitmodel
;
1776 unlock ("geometry");
1777 printd ("continue %d", state
.pagecount
);
1779 else if (!strncmp ("reqlayout", p
, 9)) {
1786 ret
= sscanf (p
+ 9, " %d %d %d %n",
1787 &rotate
, &fitmodel
, &h
, &off
);
1789 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1792 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1793 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1796 state
.rotate
= rotate
;
1797 state
.fitmodel
= fitmodel
;
1802 nameddest
= p
+ 9 + off
;
1803 if (pdf
&& nameddest
&& *nameddest
) {
1805 struct pagedim
*pdim
;
1806 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1808 pdim
= pdimofpageno (pageno
);
1809 fz_transform_point (&xy
, &pdim
->ctm
);
1810 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1814 unlock ("reqlayout");
1815 printd ("continue %d", state
.pagecount
);
1817 else if (!strncmp ("page", p
, 4)) {
1822 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1824 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1829 page
= loadpage (pageno
, pindex
);
1833 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1835 else if (!strncmp ("tile", p
, 4)) {
1842 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1843 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1844 SCN_ptr_cast (&data
));
1846 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1851 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1855 printd ("tile %d %d %" FMT_ptr
" %u %f",
1857 FMT_ptr_cast (tile
),
1858 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1861 else if (!strncmp ("trimset", p
, 7)) {
1865 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1866 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1868 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1871 state
.trimmargins
= trimmargins
;
1872 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1874 state
.trimfuzz
= fuzz
;
1878 else if (!strncmp ("settrim", p
, 7)) {
1882 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1883 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1885 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1889 state
.trimmargins
= trimmargins
;
1890 state
.needoutline
= 1;
1891 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1893 state
.trimfuzz
= fuzz
;
1895 state
.pagedimcount
= 0;
1896 free (state
.pagedims
);
1897 state
.pagedims
= NULL
;
1902 printd ("continue %d", state
.pagecount
);
1904 else if (!strncmp ("sliceh", p
, 6)) {
1907 ret
= sscanf (p
+ 6, " %d", &h
);
1909 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1911 if (h
!= state
.sliceheight
) {
1912 state
.sliceheight
= h
;
1913 for (int i
= 0; i
< state
.texcount
; ++i
) {
1914 state
.texowners
[i
].w
= -1;
1915 state
.texowners
[i
].h
= -1;
1916 state
.texowners
[i
].slice
= NULL
;
1920 else if (!strncmp ("interrupt", p
, 9)) {
1921 printd ("vmsg interrupted");
1924 errx (1, "unknown command %.*s", len
, p
);
1930 CAMLprim value
ml_isexternallink (value uri_v
)
1933 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1934 CAMLreturn (Val_bool (ext
));
1937 CAMLprim value
ml_uritolocation (value uri_v
)
1943 struct pagedim
*pdim
;
1945 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1947 pdim
= pdimofpageno (pageno
);
1948 fz_transform_point (&xy
, &pdim
->ctm
);
1949 ret_v
= caml_alloc_tuple (3);
1950 Field (ret_v
, 0) = Val_int (pageno
);
1951 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1952 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1956 CAMLprim value
ml_realloctexts (value texcount_v
)
1958 CAMLparam1 (texcount_v
);
1961 if (trylock (__func__
)) {
1965 realloctexts (Int_val (texcount_v
));
1970 CAMLreturn (Val_bool (ok
));
1973 static void recti (int x0
, int y0
, int x1
, int y1
)
1975 GLfloat
*v
= state
.vertices
;
1977 glVertexPointer (2, GL_FLOAT
, 0, v
);
1978 v
[0] = x0
; v
[1] = y0
;
1979 v
[2] = x1
; v
[3] = y0
;
1980 v
[4] = x0
; v
[5] = y1
;
1981 v
[6] = x1
; v
[7] = y1
;
1982 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1985 static void showsel (struct page
*page
, int ox
, int oy
)
1989 fz_stext_block
*block
;
1991 unsigned char selcolor
[] = {15,15,15,140};
1993 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) return;
1995 glEnable (GL_BLEND
);
1996 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1997 glColor4ubv (selcolor
);
1999 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2000 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2002 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2003 fz_stext_line
*line
;
2005 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2006 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2009 rect
= fz_empty_rect
;
2010 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2011 if (ch
== page
->fmark
.ch
) seen
= 1;
2012 if (seen
) fz_union_rect (&rect
, &ch
->bbox
);
2013 if (ch
== page
->lmark
.ch
) {
2014 fz_round_rect (&bbox
, &rect
);
2015 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2016 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2020 fz_round_rect (&bbox
, &rect
);
2021 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2022 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2026 glDisable (GL_BLEND
);
2029 #pragma GCC diagnostic push
2030 #pragma GCC diagnostic ignored "-Wdouble-promotion"
2031 #pragma GCC diagnostic ignored "-Wconversion"
2033 #pragma GCC diagnostic pop
2035 static void stipplerect (fz_matrix
*m
,
2043 fz_transform_point (p1
, m
);
2044 fz_transform_point (p2
, m
);
2045 fz_transform_point (p3
, m
);
2046 fz_transform_point (p4
, m
);
2052 t
= hypotf (w
, h
) * .25f
;
2056 s
= hypotf (w
, h
) * .25f
;
2058 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2059 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2061 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2062 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2064 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2065 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2067 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2068 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2070 glDrawArrays (GL_LINES
, 0, 8);
2073 static void solidrect (fz_matrix
*m
,
2080 fz_transform_point (p1
, m
);
2081 fz_transform_point (p2
, m
);
2082 fz_transform_point (p3
, m
);
2083 fz_transform_point (p4
, m
);
2084 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2085 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2087 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2088 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2089 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2092 static void ensurelinks (struct page
*page
)
2095 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
2098 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2100 fz_matrix ctm
, tm
, pm
;
2102 GLfloat
*texcoords
= state
.texcoords
;
2103 GLfloat
*vertices
= state
.vertices
;
2107 glEnable (GL_TEXTURE_1D
);
2108 glEnable (GL_BLEND
);
2109 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2110 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2112 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2113 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2114 fz_translate (&tm
, xoff
, yoff
);
2115 pm
= pagectm (page
);
2116 fz_concat (&ctm
, &pm
, &tm
);
2118 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2119 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2121 for (link
= page
->links
; link
; link
= link
->next
) {
2122 fz_point p1
, p2
, p3
, p4
;
2124 p1
.x
= link
->rect
.x0
;
2125 p1
.y
= link
->rect
.y0
;
2127 p2
.x
= link
->rect
.x1
;
2128 p2
.y
= link
->rect
.y0
;
2130 p3
.x
= link
->rect
.x1
;
2131 p3
.y
= link
->rect
.y1
;
2133 p4
.x
= link
->rect
.x0
;
2134 p4
.y
= link
->rect
.y1
;
2136 /* TODO: different colours for different schemes */
2137 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
2138 else glColor3ub (255, 0, 0);
2140 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2143 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2144 fz_point p1
, p2
, p3
, p4
;
2145 struct annot
*annot
= &page
->annots
[i
];
2147 p1
.x
= annot
->bbox
.x0
;
2148 p1
.y
= annot
->bbox
.y0
;
2150 p2
.x
= annot
->bbox
.x1
;
2151 p2
.y
= annot
->bbox
.y0
;
2153 p3
.x
= annot
->bbox
.x1
;
2154 p3
.y
= annot
->bbox
.y1
;
2156 p4
.x
= annot
->bbox
.x0
;
2157 p4
.y
= annot
->bbox
.y1
;
2159 glColor3ub (0, 0, 128);
2160 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2163 glDisable (GL_BLEND
);
2164 glDisable (GL_TEXTURE_1D
);
2167 static int compareslinks (const void *l
, const void *r
)
2169 struct slink
const *ls
= l
;
2170 struct slink
const *rs
= r
;
2171 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2172 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2174 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2177 static void droptext (struct page
*page
)
2180 fz_drop_stext_page (state
.ctx
, page
->text
);
2181 page
->fmark
.ch
= NULL
;
2182 page
->lmark
.ch
= NULL
;
2187 static void dropannots (struct page
*page
)
2190 free (page
->annots
);
2191 page
->annots
= NULL
;
2192 page
->annotcount
= 0;
2196 static void ensureannots (struct page
*page
)
2199 size_t annotsize
= sizeof (*page
->annots
);
2202 if (state
.gen
!= page
->agen
) {
2204 page
->agen
= state
.gen
;
2206 if (page
->annots
) return;
2208 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2210 annot
= fz_next_annot (state
.ctx
, annot
)) {
2215 page
->annotcount
= count
;
2216 page
->annots
= calloc (count
, annotsize
);
2217 if (!page
->annots
) {
2218 err (1, "calloc annots %d", count
);
2221 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2223 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2226 fz_bound_annot (state
.ctx
, annot
, &rect
);
2227 page
->annots
[i
].annot
= annot
;
2228 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2233 static void dropslinks (struct page
*page
)
2236 free (page
->slinks
);
2237 page
->slinks
= NULL
;
2238 page
->slinkcount
= 0;
2241 fz_drop_link (state
.ctx
, page
->links
);
2246 static void ensureslinks (struct page
*page
)
2250 size_t slinksize
= sizeof (*page
->slinks
);
2253 ensureannots (page
);
2254 if (state
.gen
!= page
->sgen
) {
2256 page
->sgen
= state
.gen
;
2258 if (page
->slinks
) return;
2261 ctm
= pagectm (page
);
2263 count
= page
->annotcount
;
2264 for (link
= page
->links
; link
; link
= link
->next
) {
2270 page
->slinkcount
= count
;
2271 page
->slinks
= calloc (count
, slinksize
);
2272 if (!page
->slinks
) {
2273 err (1, "calloc slinks %d", count
);
2276 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
2280 fz_transform_rect (&rect
, &ctm
);
2281 page
->slinks
[i
].tag
= SLINK
;
2282 page
->slinks
[i
].u
.link
= link
;
2283 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2285 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2287 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2288 fz_transform_rect (&rect
, &ctm
);
2289 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2291 page
->slinks
[i
].tag
= SANNOT
;
2292 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2294 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2298 #pragma GCC diagnostic push
2299 #pragma GCC diagnostic ignored "-Wconversion"
2300 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2301 static void fmt_linkn (char *s
, unsigned int u
)
2303 unsigned int len
; unsigned int q
;
2304 unsigned int zma
= 'z' - 'a' + 1;
2306 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2309 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2310 /* handles u == 0 */
2314 #pragma GCC diagnostic pop
2316 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2317 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2320 struct slink
*slink
;
2321 float x0
, y0
, x1
, y1
, w
;
2323 ensureslinks (page
);
2324 glColor3ub (0xc3, 0xb0, 0x91);
2325 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2326 fmt_linkn (buf
, i
+ noff
);
2327 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2328 slink
= &page
->slinks
[i
];
2330 x0
= slink
->bbox
.x0
+ xoff
- 5;
2331 y1
= slink
->bbox
.y0
+ yoff
- 5;
2332 y0
= y1
+ 10 + hfsize
;
2333 w
= measure_string (state
.face
, hfsize
, buf
);
2335 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2339 glEnable (GL_BLEND
);
2340 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2341 glEnable (GL_TEXTURE_2D
);
2342 glColor3ub (0, 0, 0);
2343 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2344 fmt_linkn (buf
, i
+ noff
);
2345 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2346 slink
= &page
->slinks
[i
];
2348 x0
= slink
->bbox
.x0
+ xoff
;
2349 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2350 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2353 glDisable (GL_TEXTURE_2D
);
2354 glDisable (GL_BLEND
);
2357 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2360 struct slice
*slice1
;
2361 unsigned char *texdata
;
2364 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2365 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2367 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2368 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2369 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2373 int texindex
= state
.texindex
++ % state
.texcount
;
2375 if (state
.texowners
[texindex
].w
== tile
->w
) {
2376 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2380 state
.texowners
[texindex
].h
= slice
->h
;
2384 state
.texowners
[texindex
].h
= slice
->h
;
2387 state
.texowners
[texindex
].w
= tile
->w
;
2388 state
.texowners
[texindex
].slice
= slice
;
2389 slice
->texindex
= texindex
;
2391 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2392 #if TEXT_TYPE == GL_TEXTURE_2D
2393 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2394 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2395 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2396 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2399 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2403 texdata
= tile
->pixmap
->samples
;
2406 glTexSubImage2D (TEXT_TYPE
,
2418 glTexImage2D (TEXT_TYPE
,
2430 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2435 CAMLprim
void ml_begintiles (value unit_v
)
2437 CAMLparam1 (unit_v
);
2438 glEnable (TEXT_TYPE
);
2439 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2440 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2444 CAMLprim
void ml_endtiles (value unit_v
)
2446 CAMLparam1 (unit_v
);
2447 glDisable (TEXT_TYPE
);
2451 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2453 CAMLparam2 (args_v
, ptr_v
);
2454 int dispx
= Int_val (Field (args_v
, 0));
2455 int dispy
= Int_val (Field (args_v
, 1));
2456 int dispw
= Int_val (Field (args_v
, 2));
2457 int disph
= Int_val (Field (args_v
, 3));
2458 int tilex
= Int_val (Field (args_v
, 4));
2459 int tiley
= Int_val (Field (args_v
, 5));
2460 char *s
= String_val (ptr_v
);
2461 struct tile
*tile
= parse_pointer (__func__
, s
);
2462 int slicey
, firstslice
;
2463 struct slice
*slice
;
2464 GLfloat
*texcoords
= state
.texcoords
;
2465 GLfloat
*vertices
= state
.vertices
;
2467 firstslice
= tiley
/ tile
->sliceheight
;
2468 slice
= &tile
->slices
[firstslice
];
2469 slicey
= tiley
% tile
->sliceheight
;
2474 dh
= slice
->h
- slicey
;
2475 dh
= fz_mini (disph
, dh
);
2476 uploadslice (tile
, slice
);
2478 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2479 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2480 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2481 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2483 vertices
[0] = dispx
; vertices
[1] = dispy
;
2484 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2485 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2486 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2488 #if TEXT_TYPE == GL_TEXTURE_2D
2489 for (int i
= 0; i
< 8; ++i
) {
2490 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2494 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2498 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2504 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2506 fz_matrix ctm
, tm
, pm
;
2507 fz_point p1
, p2
, p3
, p4
;
2508 GLfloat
*vertices
= state
.vertices
;
2509 double *v
= (double *) rects_v
;
2511 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2512 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2513 fz_translate (&tm
, xoff
, yoff
);
2514 pm
= pagectm (page
);
2515 fz_concat (&ctm
, &pm
, &tm
);
2517 glEnable (GL_BLEND
);
2518 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2521 p1
.x
= (float) v
[4];
2522 p1
.y
= (float) v
[5];
2524 p2
.x
= (float) v
[6];
2525 p2
.y
= (float) v
[5];
2527 p3
.x
= (float) v
[6];
2528 p3
.y
= (float) v
[7];
2530 p4
.x
= (float) v
[4];
2531 p4
.y
= (float) v
[7];
2532 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2533 glDisable (GL_BLEND
);
2536 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2537 value xoff_v
, value yoff_v
,
2540 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2541 int xoff
= Int_val (xoff_v
);
2542 int yoff
= Int_val (yoff_v
);
2543 int noff
= Int_val (Field (li_v
, 0));
2544 char *targ
= String_val (Field (li_v
, 1));
2545 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2546 int hfsize
= Int_val (Field (li_v
, 2));
2547 char *s
= String_val (ptr_v
);
2548 int hlmask
= Int_val (hlinks_v
);
2549 struct page
*page
= parse_pointer (__func__
, s
);
2551 if (!page
->fzpage
) {
2552 /* deal with loadpage failed pages */
2556 if (trylock (__func__
)) {
2561 ensureannots (page
);
2562 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2564 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2565 noff
= page
->slinkcount
;
2567 if (page
->tgen
== state
.gen
) {
2568 showsel (page
, xoff
, yoff
);
2573 CAMLreturn (Val_int (noff
));
2576 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2579 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2580 int xoff
= Int_val (xoff_v
);
2581 int yoff
= Int_val (yoff_v
);
2582 char *s
= String_val (ptr_v
);
2583 struct page
*page
= parse_pointer (__func__
, s
);
2585 drawprect (page
, xoff
, yoff
, rects_v
);
2589 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2593 const fz_matrix
*tctm
;
2594 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2596 if (!page
->annots
) return NULL
;
2599 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2600 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2603 tctm
= &fz_identity
;
2609 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2610 fz_invert_matrix (&ctm
, &ctm
);
2611 fz_transform_point (&p
, &ctm
);
2614 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2615 struct annot
*a
= &page
->annots
[i
];
2618 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2619 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2620 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2628 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2634 ensureslinks (page
);
2639 ctm
= pagectm (page
);
2640 fz_invert_matrix (&ctm
, &ctm
);
2641 fz_transform_point (&p
, &ctm
);
2643 for (link
= page
->links
; link
; link
= link
->next
) {
2644 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2645 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2653 static void ensuretext (struct page
*page
)
2655 if (state
.gen
!= page
->tgen
) {
2657 page
->tgen
= state
.gen
;
2663 page
->text
= fz_new_stext_page (state
.ctx
,
2664 &state
.pagedims
[page
->pdimno
].mediabox
);
2665 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2666 ctm
= pagectm (page
);
2667 fz_run_display_list (state
.ctx
, page
->dlist
,
2668 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2669 fz_close_device (state
.ctx
, tdev
);
2670 fz_drop_device (state
.ctx
, tdev
);
2674 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2676 CAMLparam2 (start_page_v
, dir_v
);
2678 int i
, dir
= Int_val (dir_v
);
2679 int start_page
= Int_val (start_page_v
);
2680 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2684 ret_v
= Val_int (0);
2686 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2687 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2692 pdf_page
*page
= NULL
;
2695 fz_try (state
.ctx
) {
2696 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2697 found
= !!page
->links
|| !!page
->annots
;
2699 fz_catch (state
.ctx
) {
2703 fz_drop_page (state
.ctx
, &page
->super
);
2707 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2708 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2710 fz_drop_link (state
.ctx
, link
);
2711 fz_drop_page (state
.ctx
, page
);
2715 ret_v
= caml_alloc_small (1, 1);
2716 Field (ret_v
, 0) = Val_int (i
);
2725 enum { dir_first
, dir_last
};
2726 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2728 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2730 CAMLparam2 (ptr_v
, dir_v
);
2731 CAMLlocal2 (ret_v
, pos_v
);
2733 int dirtag
, i
, slinkindex
;
2734 struct slink
*found
= NULL
,*slink
;
2735 char *s
= String_val (ptr_v
);
2737 page
= parse_pointer (__func__
, s
);
2738 ret_v
= Val_int (0);
2740 ensureslinks (page
);
2742 if (Is_block (dir_v
)) {
2743 dirtag
= Tag_val (dir_v
);
2745 case dir_first_visible
:
2747 int x0
, y0
, dir
, first_index
, last_index
;
2749 pos_v
= Field (dir_v
, 0);
2750 x0
= Int_val (Field (pos_v
, 0));
2751 y0
= Int_val (Field (pos_v
, 1));
2752 dir
= Int_val (Field (pos_v
, 2));
2757 last_index
= page
->slinkcount
;
2760 first_index
= page
->slinkcount
- 1;
2764 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2765 slink
= &page
->slinks
[i
];
2766 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2775 slinkindex
= Int_val (Field (dir_v
, 0));
2776 found
= &page
->slinks
[slinkindex
];
2777 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2778 slink
= &page
->slinks
[i
];
2779 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2787 slinkindex
= Int_val (Field (dir_v
, 0));
2788 found
= &page
->slinks
[slinkindex
];
2789 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2790 slink
= &page
->slinks
[i
];
2791 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2799 slinkindex
= Int_val (Field (dir_v
, 0));
2800 found
= &page
->slinks
[slinkindex
];
2801 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2802 slink
= &page
->slinks
[i
];
2803 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2811 slinkindex
= Int_val (Field (dir_v
, 0));
2812 found
= &page
->slinks
[slinkindex
];
2813 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2814 slink
= &page
->slinks
[i
];
2815 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2824 dirtag
= Int_val (dir_v
);
2827 found
= page
->slinks
;
2832 found
= page
->slinks
+ (page
->slinkcount
- 1);
2838 ret_v
= caml_alloc_small (2, 1);
2839 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2846 enum { uuri
, utext
, uannot
};
2848 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2850 CAMLparam2 (ptr_v
, n_v
);
2851 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2854 char *s
= String_val (ptr_v
);
2855 struct slink
*slink
;
2857 ret_v
= Val_int (0);
2858 page
= parse_pointer (__func__
, s
);
2861 ensureslinks (page
);
2862 slink
= &page
->slinks
[Int_val (n_v
)];
2863 if (slink
->tag
== SLINK
) {
2864 link
= slink
->u
.link
;
2865 str_v
= caml_copy_string (link
->uri
);
2866 ret_v
= caml_alloc_small (1, uuri
);
2867 Field (ret_v
, 0) = str_v
;
2870 ret_v
= caml_alloc_small (1, uannot
);
2871 tup_v
= caml_alloc_tuple (2);
2872 Field (ret_v
, 0) = tup_v
;
2873 Field (tup_v
, 0) = ptr_v
;
2874 Field (tup_v
, 1) = n_v
;
2881 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2883 CAMLparam2 (ptr_v
, n_v
);
2886 char *contents
= NULL
;
2889 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2891 char *s
= String_val (ptr_v
);
2893 struct slink
*slink
;
2895 page
= parse_pointer (__func__
, s
);
2896 slink
= &page
->slinks
[Int_val (n_v
)];
2897 contents
= pdf_copy_annot_contents (state
.ctx
,
2898 (pdf_annot
*) slink
->u
.annot
);
2902 ret_v
= caml_copy_string (contents
);
2903 fz_free (state
.ctx
, contents
);
2906 ret_v
= caml_copy_string ("");
2911 CAMLprim value
ml_getlinkcount (value ptr_v
)
2915 char *s
= String_val (ptr_v
);
2917 page
= parse_pointer (__func__
, s
);
2918 CAMLreturn (Val_int (page
->slinkcount
));
2921 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2923 CAMLparam2 (ptr_v
, n_v
);
2926 struct slink
*slink
;
2927 char *s
= String_val (ptr_v
);
2929 page
= parse_pointer (__func__
, s
);
2930 ret_v
= caml_alloc_tuple (4);
2932 ensureslinks (page
);
2934 slink
= &page
->slinks
[Int_val (n_v
)];
2935 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2936 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2937 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2938 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2943 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2945 CAMLparam3 (ptr_v
, x_v
, y_v
);
2946 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2948 struct annot
*annot
;
2950 char *ptr
= String_val (ptr_v
);
2951 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2952 struct pagedim
*pdim
;
2954 ret_v
= Val_int (0);
2955 if (trylock (__func__
)) {
2959 page
= parse_pointer (__func__
, ptr
);
2960 pdim
= &state
.pagedims
[page
->pdimno
];
2961 x
+= pdim
->bounds
.x0
;
2962 y
+= pdim
->bounds
.y0
;
2965 annot
= getannot (page
, x
, y
);
2969 ensureslinks (page
);
2970 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2971 if (page
->slinks
[i
].tag
== SANNOT
2972 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2977 ret_v
= caml_alloc_small (1, uannot
);
2978 tup_v
= caml_alloc_tuple (2);
2979 Field (ret_v
, 0) = tup_v
;
2980 Field (tup_v
, 0) = ptr_v
;
2981 Field (tup_v
, 1) = Val_int (n
);
2986 link
= getlink (page
, x
, y
);
2988 str_v
= caml_copy_string (link
->uri
);
2989 ret_v
= caml_alloc_small (1, uuri
);
2990 Field (ret_v
, 0) = str_v
;
2994 fz_stext_block
*block
;
2998 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2999 fz_stext_line
*line
;
3001 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3003 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3006 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3010 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3013 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3016 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3017 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
3018 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3021 if (!n2
) n2
= "<unknown font>";
3023 if (face
&& face
->family_name
) {
3025 char *n1
= face
->family_name
;
3026 size_t l1
= strlen (n1
);
3027 size_t l2
= strlen (n2
);
3029 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3030 s
= malloc (l1
+ l2
+ 2);
3034 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3035 str_v
= caml_copy_string (s
);
3040 if (str_v
== Val_unit
) {
3041 str_v
= caml_copy_string (n2
);
3043 ret_v
= caml_alloc_small (1, utext
);
3044 Field (ret_v
, 0) = str_v
;
3058 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3060 static int uninteresting (int c
)
3062 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3066 CAMLprim
void ml_clearmark (value ptr_v
)
3069 char *s
= String_val (ptr_v
);
3072 if (trylock (__func__
)) {
3076 page
= parse_pointer (__func__
, s
);
3077 page
->fmark
.ch
= NULL
;
3078 page
->lmark
.ch
= NULL
;
3085 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3087 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3091 fz_stext_line
*line
;
3092 fz_stext_block
*block
;
3093 struct pagedim
*pdim
;
3094 int mark
= Int_val (mark_v
);
3095 char *s
= String_val (ptr_v
);
3096 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3098 ret_v
= Val_bool (0);
3099 if (trylock (__func__
)) {
3103 page
= parse_pointer (__func__
, s
);
3104 pdim
= &state
.pagedims
[page
->pdimno
];
3108 if (mark
== mark_page
) {
3109 page
->fmark
.ch
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
3110 page
->lmark
.ch
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
3111 ret_v
= Val_bool (1);
3115 x
+= pdim
->bounds
.x0
;
3116 y
+= pdim
->bounds
.y0
;
3118 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3119 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3121 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3124 if (mark
== mark_block
) {
3125 page
->fmark
.ch
= block
->u
.t
.first_line
->first_char
;
3126 page
->lmark
.ch
= block
->u
.t
.last_line
->last_char
;
3127 ret_v
= Val_bool (1);
3131 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3135 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3138 if (mark
== mark_line
) {
3139 page
->fmark
.ch
= line
->first_char
;
3140 page
->lmark
.ch
= line
->last_char
;
3141 ret_v
= Val_bool (1);
3145 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3146 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
3148 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3149 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
3150 if (uninteresting (ch2
->c
)) first
= NULL
;
3151 else if (!first
) first
= ch2
;
3153 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
3154 if (uninteresting (ch2
->c
)) break;
3158 page
->fmark
.ch
= first
;
3159 page
->lmark
.ch
= last
;
3160 ret_v
= Val_bool (1);
3167 if (!Bool_val (ret_v
)) {
3168 page
->fmark
.ch
= NULL
;
3169 page
->lmark
.ch
= NULL
;
3177 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3179 CAMLparam3 (ptr_v
, x_v
, y_v
);
3180 CAMLlocal2 (ret_v
, res_v
);
3183 struct pagedim
*pdim
;
3184 fz_stext_block
*block
;
3185 char *s
= String_val (ptr_v
);
3186 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3188 ret_v
= Val_int (0);
3189 if (trylock (__func__
)) {
3193 page
= parse_pointer (__func__
, s
);
3194 pdim
= &state
.pagedims
[page
->pdimno
];
3195 x
+= pdim
->bounds
.x0
;
3196 y
+= pdim
->bounds
.y0
;
3200 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3201 switch (block
->type
) {
3202 case FZ_STEXT_BLOCK_TEXT
:
3206 case FZ_STEXT_BLOCK_IMAGE
:
3214 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3219 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3220 ret_v
= caml_alloc_small (1, 1);
3221 Store_double_field (res_v
, 0, (double) b
->x0
);
3222 Store_double_field (res_v
, 1, (double) b
->x1
);
3223 Store_double_field (res_v
, 2, (double) b
->y0
);
3224 Store_double_field (res_v
, 3, (double) b
->y1
);
3225 Field (ret_v
, 0) = res_v
;
3233 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3235 CAMLparam2 (ptr_v
, rect_v
);
3238 struct pagedim
*pdim
;
3239 char *s
= String_val (ptr_v
);
3242 fz_stext_line
*line
;
3243 fz_stext_block
*block
;
3244 fz_stext_char
*fc
, *lc
;
3246 if (trylock (__func__
)) {
3250 page
= parse_pointer (__func__
, s
);
3253 pdim
= &state
.pagedims
[page
->pdimno
];
3254 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3255 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3256 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3257 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3267 fc
= page
->fmark
.ch
;
3268 lc
= page
->lmark
.ch
;
3270 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3271 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3272 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3273 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3275 if (x0
>= b
.x0
&& x0
<= b
.x1
&& y0
>= b
.y0
&& y0
<= b
.y1
) {
3278 if (x1
>= b
.x0
&& x1
<= b
.x1
&& y1
>= b
.y0
&& y1
<= b
.y1
) {
3284 if (x1
< x0
&& fc
== lc
) {
3292 page
->fmark
.ch
= fc
;
3293 page
->lmark
.ch
= lc
;
3301 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3307 len
= fz_runetochar (buf
, ch
->c
);
3308 ret
= fwrite (buf
, len
, 1, f
);
3310 fprintf (stderr
, "failed to write %d bytes ret=%zu: %s\n",
3311 len
, ret
, strerror (errno
));
3317 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3319 CAMLparam2 (command_v
, fds_v
);
3320 CAMLlocal2 (l_v
, tup_v
);
3322 pid_t pid
= (pid_t
) -1;
3324 value earg_v
= Nothing
;
3325 posix_spawnattr_t attr
;
3326 posix_spawn_file_actions_t fa
;
3327 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3329 argv
[2] = String_val (command_v
);
3331 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3332 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3335 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3336 msg
= "posix_spawnattr_init";
3340 #ifdef POSIX_SPAWN_USEVFORK
3341 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3342 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3347 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3350 tup_v
= Field (l_v
, 0);
3351 fd1
= Int_val (Field (tup_v
, 0));
3352 fd2
= Int_val (Field (tup_v
, 1));
3354 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3355 msg
= "posix_spawn_file_actions_addclose";
3361 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3362 msg
= "posix_spawn_file_actions_adddup2";
3369 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3370 msg
= "posix_spawn";
3375 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3376 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3380 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3381 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3386 unix_error (ret
, msg
, earg_v
);
3388 CAMLreturn (Val_int (pid
));
3391 CAMLprim value
ml_hassel (value ptr_v
)
3396 char *s
= String_val (ptr_v
);
3398 ret_v
= Val_bool (0);
3399 if (trylock (__func__
)) {
3403 page
= parse_pointer (__func__
, s
);
3404 ret_v
= Val_bool (page
->fmark
.ch
&& page
->lmark
.ch
);
3410 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3412 CAMLparam2 (fd_v
, ptr_v
);
3416 fz_stext_line
*line
;
3417 fz_stext_block
*block
;
3418 int fd
= Int_val (fd_v
);
3419 char *s
= String_val (ptr_v
);
3421 if (trylock (__func__
)) {
3425 page
= parse_pointer (__func__
, s
);
3427 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) {
3428 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3432 f
= fdopen (fd
, "w");
3434 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3435 fd
, strerror (errno
));
3439 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3440 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3441 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3443 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3444 if (seen
|| ch
== page
->fmark
.ch
) {
3447 if (ch
== page
->lmark
.ch
) goto close
;
3448 } while ((ch
= ch
->next
));
3453 if (seen
) fputc ('\n', f
);
3458 int ret
= fclose (f
);
3461 if (errno
!= ECHILD
) {
3462 fprintf (stderr
, "failed to close sel pipe: %s\n",
3473 fprintf (stderr
, "failed to close sel pipe: %s\n",
3480 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3482 CAMLparam1 (pagedimno_v
);
3484 int pagedimno
= Int_val (pagedimno_v
);
3487 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3488 if (trylock (__func__
)) {
3489 box
= fz_empty_rect
;
3492 box
= state
.pagedims
[pagedimno
].mediabox
;
3496 Store_double_field (ret_v
, 0, (double) box
.x0
);
3497 Store_double_field (ret_v
, 1, (double) box
.x1
);
3498 Store_double_field (ret_v
, 2, (double) box
.y0
);
3499 Store_double_field (ret_v
, 3, (double) box
.y1
);
3504 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3505 value dw_v
, value cols_v
)
3507 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3513 float winw
= Int_val (winw_v
);
3514 float winh
= Int_val (winh_v
);
3515 float dw
= Int_val (dw_v
);
3516 float cols
= Int_val (cols_v
);
3517 float pw
= 1.0, ph
= 1.0;
3519 if (trylock (__func__
)) {
3523 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3524 float w
= p
->pagebox
.x1
/ cols
;
3525 float h
= p
->pagebox
.y1
;
3529 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3531 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3534 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3537 ret_v
= caml_copy_double ((double) zoom
);
3541 CAMLprim value
ml_getmaxw (value unit_v
)
3543 CAMLparam1 (unit_v
);
3549 if (trylock (__func__
)) {
3553 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3554 float w
= p
->pagebox
.x1
;
3555 maxw
= fz_max (maxw
, w
);
3560 ret_v
= caml_copy_double ((double) maxw
);
3564 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3566 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3568 int pt
= Int_val(pt_v
);
3569 int x
= Int_val (x_v
);
3570 int y
= Int_val (y_v
);
3573 w
= (double) draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3574 ret_v
= caml_copy_double (w
);
3578 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3580 CAMLparam2 (pt_v
, string_v
);
3582 int pt
= Int_val (pt_v
);
3585 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3586 ret_v
= caml_copy_double (w
);
3590 CAMLprim value
ml_getpagebox (value opaque_v
)
3592 CAMLparam1 (opaque_v
);
3598 char *s
= String_val (opaque_v
);
3599 struct page
*page
= parse_pointer (__func__
, s
);
3601 ret_v
= caml_alloc_tuple (4);
3602 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3604 ctm
= pagectm (page
);
3605 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3607 fz_close_device (state
.ctx
, dev
);
3608 fz_drop_device (state
.ctx
, dev
);
3609 fz_round_rect (&bbox
, &rect
);
3610 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3611 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3612 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3613 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3618 CAMLprim
void ml_setaalevel (value level_v
)
3620 CAMLparam1 (level_v
);
3622 state
.aalevel
= Int_val (level_v
);
3627 #pragma GCC diagnostic push
3628 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3629 #include <X11/Xlib.h>
3630 #include <X11/cursorfont.h>
3631 #pragma GCC diagnostic pop
3634 #include <EGL/egl.h>
3639 static const int shapes
[] = {
3640 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3643 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3656 XVisualInfo
*visual
;
3657 Cursor curs
[CURS_COUNT
];
3661 static void initcurs (void)
3663 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3664 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3669 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3671 CAMLparam3 (display_v
, wid_v
, screen_v
);
3675 EGLint attribs
[] = {
3676 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3677 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3682 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3684 caml_failwith ("XOpenDisplay");
3687 eglBindAPI (EGL_OPENGL_API
);
3689 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3690 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3691 caml_failwith ("eglGetDisplay");
3694 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3695 caml_failwith ("eglInitialize");
3698 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3700 caml_failwith ("eglChooseConfig");
3703 if (!eglGetConfigAttrib (glx
.edpy
, conf
, EGL_NATIVE_VISUAL_ID
, &visid
)) {
3704 caml_failwith ("eglGetConfigAttrib");
3710 glx
.wid
= Int_val (wid_v
);
3711 CAMLreturn (Val_int (visid
));
3714 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3716 CAMLparam1 (unit_v
);
3718 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3720 caml_failwith ("eglCreateContext");
3723 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3725 if (glx
.win
== EGL_NO_SURFACE
) {
3726 caml_failwith ("eglCreateWindowSurface");
3729 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3731 caml_failwith ("eglMakeCurrent");
3736 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3738 CAMLparam3 (display_v
, wid_v
, screen_v
);
3740 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3742 caml_failwith ("XOpenDisplay");
3745 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3746 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3748 XCloseDisplay (glx
.dpy
);
3749 caml_failwith ("glXChooseVisual");
3754 glx
.wid
= Int_val (wid_v
);
3755 CAMLreturn (Val_int (glx
.visual
->visualid
));
3758 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3760 CAMLparam1 (unit_v
);
3762 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3764 caml_failwith ("glXCreateContext");
3770 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3771 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3773 caml_failwith ("glXMakeCurrent");
3779 CAMLprim
void ml_setcursor (value cursor_v
)
3781 CAMLparam1 (cursor_v
);
3782 size_t cursn
= Int_val (cursor_v
);
3784 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
3785 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
3790 CAMLprim
void ml_swapb (value unit_v
)
3792 CAMLparam1 (unit_v
);
3794 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
3795 caml_failwith ("eglSwapBuffers");
3798 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3803 #pragma GCC diagnostic push
3805 #pragma GCC diagnostic ignored "-Wmissing-variable-declarations"
3807 #include "keysym2ucs.c"
3808 #pragma GCC diagnostic pop
3810 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3812 CAMLparam1 (keysym_v
);
3814 KeySym keysym
= Int_val (keysym_v
);
3819 rune
= (Rune
) keysym2ucs (keysym
);
3820 len
= fz_runetochar (buf
, rune
);
3822 str_v
= caml_copy_string (buf
);
3826 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3828 CAMLparam1 (keysym_v
);
3830 long ucs_v
= Long_val (keysym_v
);
3834 len
= fz_runetochar (buf
, ucs_v
);
3836 str_v
= caml_copy_string (buf
);
3841 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
};
3843 CAMLprim value
ml_platform (value unit_v
)
3845 CAMLparam1 (unit_v
);
3846 CAMLlocal2 (tup_v
, arr_v
);
3847 int platid
= piunknown
;
3850 #if defined __linux__
3852 #elif defined __DragonFly__ || defined __FreeBSD__
3853 || defined __OpenBSD__
|| defined __NetBSD__
3855 #elif defined __sun__
3857 #elif defined __APPLE__
3860 if (uname (&buf
)) err (1, "uname");
3862 tup_v
= caml_alloc_tuple (2);
3864 char const *sar
[] = {
3871 arr_v
= caml_copy_string_array (sar
);
3873 Field (tup_v
, 0) = Val_int (platid
);
3874 Field (tup_v
, 1) = arr_v
;
3878 CAMLprim
void ml_cloexec (value fd_v
)
3881 int fd
= Int_val (fd_v
);
3883 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3884 uerror ("fcntl", Nothing
);
3889 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3891 CAMLparam2 (w_v
, h_v
);
3894 int w
= Int_val (w_v
);
3895 int h
= Int_val (h_v
);
3896 int cs
= Int_val (cs_v
);
3898 if (state
.bo_usable
) {
3899 pbo
= calloc (sizeof (*pbo
), 1);
3901 err (1, "calloc pbo");
3913 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3916 state
.glGenBuffersARB (1, &pbo
->id
);
3917 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3918 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3919 NULL
, GL_STREAM_DRAW
);
3920 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3922 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3924 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3925 state
.glDeleteBuffersARB (1, &pbo
->id
);
3927 ret_v
= caml_copy_string ("0");
3933 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3935 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3939 err (1, "malloc %d bytes failed", res
+1);
3941 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3943 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3945 ret_v
= caml_copy_string (s
);
3950 ret_v
= caml_copy_string ("0");
3955 CAMLprim
void ml_freepbo (value s_v
)
3958 char *s
= String_val (s_v
);
3959 struct tile
*tile
= parse_pointer (__func__
, s
);
3962 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3964 tile
->pbo
->ptr
= NULL
;
3965 tile
->pbo
->size
= -1;
3970 CAMLprim
void ml_unmappbo (value s_v
)
3973 char *s
= String_val (s_v
);
3974 struct tile
*tile
= parse_pointer (__func__
, s
);
3977 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3978 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3979 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3981 tile
->pbo
->ptr
= NULL
;
3982 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3987 static void setuppbo (void)
3990 static CFBundleRef framework
= NULL
;
3991 if (framework
== NULL
)
3992 framework
= CFBundleGetBundleWithIdentifier (CFSTR ("com.apple.opengl"));
3993 #define GGPA(n) (&state.n = CFBundleGetFunctionPointerForName (framework, CFSTR (#n)))
3996 #define GGPA(n) (*(void (**) (void)) &state.n = eglGetProcAddress (#n))
3998 #define GGPA(n) (*(void (**) (void)) &state.n = glXGetProcAddress ((GLubyte *) #n))
4000 state
.bo_usable
= GGPA (glBindBufferARB
)
4001 && GGPA (glUnmapBufferARB
)
4002 && GGPA (glMapBufferARB
)
4003 && GGPA (glBufferDataARB
)
4004 && GGPA (glGenBuffersARB
)
4005 && GGPA (glDeleteBuffersARB
);
4010 CAMLprim value
ml_bo_usable (value unit_v
)
4012 CAMLparam1 (unit_v
);
4013 CAMLreturn (Val_bool (state
.bo_usable
));
4016 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4018 CAMLparam3 (ptr_v
, x_v
, y_v
);
4019 CAMLlocal2 (ret_v
, tup_v
);
4021 char *s
= String_val (ptr_v
);
4022 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4023 struct pagedim
*pdim
;
4027 page
= parse_pointer (__func__
, s
);
4028 pdim
= &state
.pagedims
[page
->pdimno
];
4030 ret_v
= Val_int (0);
4031 if (trylock (__func__
)) {
4035 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4036 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4037 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4042 p
.x
= x
+ pdim
->bounds
.x0
;
4043 p
.y
= y
+ pdim
->bounds
.y0
;
4045 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4046 fz_invert_matrix (&ctm
, &ctm
);
4047 fz_transform_point (&p
, &ctm
);
4049 tup_v
= caml_alloc_tuple (2);
4050 ret_v
= caml_alloc_small (1, 1);
4051 Field (tup_v
, 0) = Val_int (p
.x
);
4052 Field (tup_v
, 1) = Val_int (p
.y
);
4053 Field (ret_v
, 0) = tup_v
;
4060 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4061 value x_v
, value y_v
)
4063 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4066 char *s
= String_val (ptr_v
);
4067 int pageno
= Int_val (pageno_v
);
4068 int pdimno
= Int_val (pdimno_v
);
4069 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
4070 struct pagedim
*pdim
;
4074 ret_v
= Val_int (0);
4078 page
= loadpage (pageno
, pdimno
);
4081 page
= parse_pointer (__func__
, s
);
4083 pdim
= &state
.pagedims
[pdimno
];
4085 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4086 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4087 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4092 p
.x
= x
+ pdim
->bounds
.x0
;
4093 p
.y
= y
+ pdim
->bounds
.y0
;
4095 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4096 fz_transform_point (&p
, &ctm
);
4098 ret_v
= caml_alloc_tuple (2);
4099 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
4100 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
4109 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
4112 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4113 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4119 char *s
= String_val (ptr_v
);
4121 page
= parse_pointer (__func__
, s
);
4122 annot
= pdf_create_annot (state
.ctx
,
4123 pdf_page_from_fz_page (state
.ctx
,
4126 p
.x
= Int_val (x_v
);
4127 p
.y
= Int_val (y_v
);
4128 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4129 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4135 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
4137 CAMLparam2 (ptr_v
, n_v
);
4138 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4142 char *s
= String_val (ptr_v
);
4143 struct slink
*slink
;
4145 page
= parse_pointer (__func__
, s
);
4146 slink
= &page
->slinks
[Int_val (n_v
)];
4147 pdf_delete_annot (state
.ctx
,
4148 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4149 (pdf_annot
*) slink
->u
.annot
);
4155 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
4157 CAMLparam3 (ptr_v
, n_v
, str_v
);
4158 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4162 char *s
= String_val (ptr_v
);
4163 struct slink
*slink
;
4165 page
= parse_pointer (__func__
, s
);
4166 slink
= &page
->slinks
[Int_val (n_v
)];
4167 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4168 String_val (str_v
));
4174 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4176 CAMLparam1 (unit_v
);
4177 CAMLreturn (Val_bool (state
.dirty
));
4180 CAMLprim
void ml_savedoc (value path_v
)
4182 CAMLparam1 (path_v
);
4183 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4186 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4191 static void makestippletex (void)
4193 const char pixels
[] = "\xff\xff\0\0";
4194 glGenTextures (1, &state
.stid
);
4195 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4196 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4197 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4210 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4212 return caml_copy_string (FZ_VERSION
);
4215 CAMLprim
void ml_init (value csock_v
, value params_v
)
4217 CAMLparam2 (csock_v
, params_v
);
4218 CAMLlocal2 (trim_v
, fuzz_v
);
4226 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
4227 if (setlocale (LC_CTYPE
, "")) {
4228 const char *cset
= nl_langinfo (CODESET
);
4229 state
.utf8cs
= !strcmp (cset
, "UTF-8");
4232 fprintf (stderr
, "setlocale failed\n");
4235 state
.csock
= Int_val (csock_v
);
4236 state
.rotate
= Int_val (Field (params_v
, 0));
4237 state
.fitmodel
= Int_val (Field (params_v
, 1));
4238 trim_v
= Field (params_v
, 2);
4239 texcount
= Int_val (Field (params_v
, 3));
4240 state
.sliceheight
= Int_val (Field (params_v
, 4));
4241 mustoresize
= Int_val (Field (params_v
, 5));
4242 colorspace
= Int_val (Field (params_v
, 6));
4243 fontpath
= String_val (Field (params_v
, 7));
4245 if (caml_string_length (Field (params_v
, 8)) > 0) {
4246 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4248 if (!state
.trimcachepath
) {
4249 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4254 haspboext
= Bool_val (Field (params_v
, 9));
4256 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4257 fz_register_document_handlers (state
.ctx
);
4259 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4260 fuzz_v
= Field (trim_v
, 1);
4261 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4262 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4263 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4264 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4266 set_tex_params (colorspace
);
4269 state
.face
= load_font (fontpath
);
4273 const unsigned char *data
;
4275 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
4276 state
.face
= load_builtin_font (data
, len
);
4278 if (!state
.face
) _exit (1);
4280 realloctexts (texcount
);
4288 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4290 errx (1, "pthread_create: %s", strerror (ret
));
4297 static void OPTIMIZE_ATTR (0) UNUSED_ATTR
refmacs (void) {}