1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/utsname.h>
22 #include <cygwin/socket.h> /* FIONREAD */
33 #include <CoreFoundation/CoreFoundation.h>
37 #include <OpenGL/gl.h>
42 #include <caml/fail.h>
43 #include <caml/alloc.h>
44 #include <caml/memory.h>
45 #include <caml/unixsupport.h>
47 #if __GNUC__ < 5 && !defined __clang__
48 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
49 clobbered diagnostics */
50 #pragma GCC diagnostic ignored "-Wclobbered"
53 #include <mupdf/fitz.h>
54 #include <mupdf/pdf.h>
57 #include FT_FREETYPE_H
60 #define CACHE_PAGEREFS
63 extern char **environ
;
67 #define NORETURN_ATTR __attribute__ ((noreturn))
68 #define UNUSED_ATTR __attribute__ ((unused))
69 #if !defined __clang__
70 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
72 #define OPTIMIZE_ATTR(n)
74 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
78 #define OPTIMIZE_ATTR(n)
79 #define GCC_FMT_ATTR(a, b)
84 #define FMT_ptr PRIxPTR
85 #define SCN_ptr SCNxPTR
86 #define FMT_ptr_cast(p) ((uintptr_t) (p))
87 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
89 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
90 err (int exitcode
, const char *fmt
, ...)
97 vfprintf (stderr
, fmt
, ap
);
99 fprintf (stderr
, ": %s\n", strerror (savederrno
));
104 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
105 errx (int exitcode
, const char *fmt
, ...)
110 vfprintf (stderr
, fmt
, ap
);
112 fputc ('\n', stderr
);
117 #ifndef GL_TEXTURE_RECTANGLE_ARB
118 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
122 #define TEXT_TYPE GL_TEXTURE_2D
124 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
128 #define GL_BGRA 0x80E1
131 #ifndef GL_UNSIGNED_INT_8_8_8_8
132 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
135 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
136 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
140 #define lprintf printf
145 #define ARSERT(cond) for (;;) { \
147 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
163 struct slice slices
[1];
174 fz_matrix ctm
, zoomctm
, tctm
;
178 enum { SLINK
, SANNOT
} tag
;
198 fz_stext_sheet
*sheet
;
200 fz_display_list
*dlist
;
202 struct slink
*slinks
;
204 struct annot
*annots
;
213 struct pagedim
*pagedims
;
228 fz_colorspace
*colorspace
;
236 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
259 void (*glBindBufferARB
) (GLenum
, GLuint
);
260 GLboolean (*glUnmapBufferARB
) (GLenum
);
261 void *(*glMapBufferARB
) (GLenum
, GLenum
);
262 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
263 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
264 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
266 GLfloat texcoords
[8];
267 GLfloat vertices
[16];
269 #ifdef CACHE_PAGEREFS
285 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
287 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
290 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
292 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
295 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
297 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
298 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
301 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
303 static void lock (const char *cap
)
305 int ret
= pthread_mutex_lock (&mutex
);
307 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
311 static void unlock (const char *cap
)
313 int ret
= pthread_mutex_unlock (&mutex
);
315 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
319 static int trylock (const char *cap
)
321 int ret
= pthread_mutex_trylock (&mutex
);
322 if (ret
&& ret
!= EBUSY
) {
323 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
328 static void *parse_pointer (const char *cap
, const char *s
)
333 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
335 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
340 static double now (void)
344 if (gettimeofday (&tv
, NULL
)) {
345 err (1, "gettimeofday");
347 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
350 static int hasdata (void)
353 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
354 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
358 CAMLprim value
ml_hasdata (value fd_v
)
363 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
364 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
365 CAMLreturn (Val_bool (avail
> 0));
368 static void readdata (int fd
, void *p
, int size
)
373 n
= read (fd
, p
, size
);
375 if (n
< 0 && errno
== EINTR
) goto again
;
376 if (!n
) errx (1, "EOF while reading");
377 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
381 static void writedata (int fd
, char *p
, int size
)
384 uint32_t size4
= size
;
385 struct iovec iov
[2] = {
386 { .iov_base
= &size4
, .iov_len
= 4 },
387 { .iov_base
= p
, .iov_len
= size
}
391 n
= writev (fd
, iov
, 2);
392 if (n
< 0 && errno
== EINTR
) goto again
;
394 if (!n
) errx (1, "EOF while writing data");
395 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
399 static int readlen (int fd
)
401 /* Type punned unions here. Why? Less code (Adjusted by more comments).
402 https://en.wikipedia.org/wiki/Type_punning */
403 union { uint32_t len
; char raw
[4]; } buf
;
404 readdata (fd
, buf
.raw
, 4);
408 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
410 CAMLparam3 (fd_v
, bytes_v
, len_v
);
411 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
415 CAMLprim value
ml_rcmd (value fd_v
)
418 CAMLlocal1 (strdata_v
);
419 int fd
= Int_val (fd_v
);
420 int len
= readlen (fd
);
421 strdata_v
= caml_alloc_string (len
);
422 readdata (fd
, String_val (strdata_v
), len
);
423 CAMLreturn (strdata_v
);
426 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
435 len
= vsnprintf (buf
, size
, fmt
, ap
);
439 if (len
< size
- 4) {
440 writedata (state
.csock
, buf
, len
);
446 err (1, "vsnprintf for `%s' failed", fmt
);
448 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
449 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
451 if (buf
!= fbuf
) free (buf
);
454 static void closedoc (void)
456 #ifdef CACHE_PAGEREFS
457 if (state
.pdflut
.objs
) {
458 for (int i
= 0; i
< state
.pdflut
.count
; ++i
) {
459 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
461 free (state
.pdflut
.objs
);
462 state
.pdflut
.objs
= NULL
;
463 state
.pdflut
.idx
= 0;
467 fz_drop_document (state
.ctx
, state
.doc
);
472 static int openxref (char *filename
, char *password
)
474 for (int i
= 0; i
< state
.texcount
; ++i
) {
475 state
.texowners
[i
].w
= -1;
476 state
.texowners
[i
].slice
= NULL
;
482 if (state
.pagedims
) {
483 free (state
.pagedims
);
484 state
.pagedims
= NULL
;
486 state
.pagedimcount
= 0;
488 fz_set_aa_level (state
.ctx
, state
.aalevel
);
489 state
.doc
= fz_open_document (state
.ctx
, filename
);
490 if (fz_needs_password (state
.ctx
, state
.doc
)) {
491 if (password
&& !*password
) {
496 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
498 printd ("pass fail");
503 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
507 static void pdfinfo (void)
509 struct { char *tag
; char *name
; } metatbl
[] = {
510 { FZ_META_INFO_TITLE
, "Title" },
511 { FZ_META_INFO_AUTHOR
, "Author" },
512 { FZ_META_FORMAT
, "Format" },
513 { FZ_META_ENCRYPTION
, "Encryption" },
514 { "info:Creator", "Creator" },
515 { "info:Producer", "Producer" },
516 { "info:CreationDate", "Creation date" },
521 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
524 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
525 metatbl
[i
].tag
, buf
, len
);
528 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
531 buf
= realloc (buf
, need
+ 1);
532 if (!buf
) err (1, "pdfinfo realloc %d", need
+ 1);
543 static void unlinktile (struct tile
*tile
)
545 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
546 struct slice
*s
= &tile
->slices
[i
];
548 if (s
->texindex
!= -1) {
549 if (state
.texowners
[s
->texindex
].slice
== s
) {
550 state
.texowners
[s
->texindex
].slice
= NULL
;
556 static void freepage (struct page
*page
)
560 fz_drop_stext_page (state
.ctx
, page
->text
);
563 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
568 fz_drop_display_list (state
.ctx
, page
->dlist
);
569 fz_drop_page (state
.ctx
, page
->fzpage
);
573 static void freetile (struct tile
*tile
)
578 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
581 fz_drop_pixmap (state
.ctx
, state
.pig
);
583 state
.pig
= tile
->pixmap
;
588 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
597 static int cacheline32bytes
;
599 static void __attribute__ ((constructor
)) clcheck (void)
601 char **envp
= environ
;
606 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
608 cacheline32bytes
= auxv
[1] == 32;
614 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
616 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
617 if (cacheline32bytes
&& size
> 32) {
618 intptr_t a1
, a2
, diff
;
620 vector
unsigned char v
= vec_splat_u8 (-1);
621 vector
unsigned char *p
;
623 a1
= a2
= (intptr_t) pixmap
->samples
;
624 a2
= (a1
+ 31) & ~31;
629 while (a1
!= a2
) *(char *) a1
++ = 0xff;
630 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
631 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
633 vec_st (v
, i
+ 16, p
);
635 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
637 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
640 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
643 static void trimctm (pdf_page
*page
, int pindex
)
646 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
649 if (!pdim
->tctmready
) {
650 fz_rect realbox
, mediabox
;
651 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
653 fz_rotate (&rm
, -pdim
->rotate
);
654 fz_scale (&sm
, 1, -1);
655 fz_concat (&ctm
, &rm
, &sm
);
656 realbox
= pdim
->mediabox
;
657 fz_transform_rect (&realbox
, &ctm
);
658 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
659 fz_concat (&ctm1
, &ctm
, &tm
);
660 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
661 fz_invert_matrix (&im
, &page_ctm
);
662 fz_concat (&ctm
, &im
, &ctm1
);
668 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
671 int pdimno
= pdim
- state
.pagedims
;
673 if (pdf_specifics (state
.ctx
, state
.doc
)) {
674 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), pdimno
);
675 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
678 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
679 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
684 static fz_matrix
pagectm (struct page
*page
)
686 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
689 static void *loadpage (int pageno
, int pindex
)
694 page
= calloc (sizeof (struct page
), 1);
696 err (1, "calloc page %d", pageno
);
699 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
700 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
702 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
703 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
706 fz_catch (state
.ctx
) {
709 fz_close_device (state
.ctx
, dev
);
710 fz_drop_device (state
.ctx
, dev
);
712 page
->pdimno
= pindex
;
713 page
->pageno
= pageno
;
714 page
->sgen
= state
.gen
;
715 page
->agen
= state
.gen
;
716 page
->tgen
= state
.gen
;
720 static struct tile
*alloctile (int h
)
726 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
727 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
728 tile
= calloc (tilesize
, 1);
730 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
732 for (int i
= 0; i
< slicecount
; ++i
) {
733 int sh
= fz_mini (h
, state
.sliceheight
);
734 tile
->slices
[i
].h
= sh
;
735 tile
->slices
[i
].texindex
= -1;
738 tile
->slicecount
= slicecount
;
739 tile
->sliceheight
= state
.sliceheight
;
743 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
751 struct pagedim
*pdim
;
753 tile
= alloctile (h
);
754 pdim
= &state
.pagedims
[page
->pdimno
];
759 bbox
.x1
= bbox
.x0
+ w
;
760 bbox
.y1
= bbox
.y0
+ h
;
763 if (state
.pig
->w
== w
765 && state
.pig
->colorspace
== state
.colorspace
) {
766 tile
->pixmap
= state
.pig
;
767 tile
->pixmap
->x
= bbox
.x0
;
768 tile
->pixmap
->y
= bbox
.y0
;
771 fz_drop_pixmap (state
.ctx
, state
.pig
);
778 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
784 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
, 1);
790 clearpixmap (tile
->pixmap
);
792 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
793 ctm
= pagectm (page
);
794 fz_rect_from_irect (&rect
, &bbox
);
795 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
796 fz_close_device (state
.ctx
, dev
);
797 fz_drop_device (state
.ctx
, dev
);
802 #ifdef CACHE_PAGEREFS
803 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
804 thanks to Robin Watts */
806 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
808 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
812 if (state
.pdflut
.idx
== state
.pagecount
) return;
814 kids
= pdf_dict_gets (ctx
, node
, "Kids");
815 len
= pdf_array_len (ctx
, kids
);
818 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
820 if (pdf_mark_obj (ctx
, node
))
821 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
822 for (int i
= 0; i
< len
; i
++) {
823 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
824 char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
826 ? !strcmp (type
, "Pages")
827 : pdf_dict_gets (ctx
, kid
, "Kids")
828 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
829 pdf_collect_pages (doc
, kid
);
833 ? strcmp (type
, "Page") != 0
834 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
835 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
836 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
839 pdf_unmark_obj (ctx
, node
);
843 pdf_load_page_objs (pdf_document
*doc
)
845 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
846 pdf_trailer (state
.ctx
, doc
), "Root");
847 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
850 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
852 state
.pdflut
.idx
= 0;
853 pdf_collect_pages (doc
, node
);
857 static void initpdims (int wthack
)
861 fz_rect rootmediabox
;
862 int pageno
, trim
, show
;
863 int trimw
= 0, cxcount
;
864 fz_context
*ctx
= state
.ctx
;
865 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
872 if (state
.trimmargins
&& state
.trimcachepath
) {
873 trimf
= fopen (state
.trimcachepath
, "rb");
875 trimf
= fopen (state
.trimcachepath
, "wb");
880 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
881 cxcount
= state
.pagecount
;
883 cxcount
= fz_mini (state
.pagecount
, 1);
887 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
888 "Root/Pages/MediaBox");
889 pdf_to_rect (ctx
, obj
, &rootmediabox
);
892 #ifdef CACHE_PAGEREFS
893 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
894 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
895 if (!state
.pdflut
.objs
) {
896 err (1, "malloc pageobjs %zu %d %zu failed",
897 sizeof (*state
.pdflut
.objs
), cxcount
,
898 sizeof (*state
.pdflut
.objs
) * cxcount
);
900 state
.pdflut
.count
= cxcount
;
901 pdf_load_page_objs (pdf
);
902 state
.pdflut
.pdf
= pdf
;
906 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
913 pdf_obj
*pageref
, *pageobj
;
915 #ifdef CACHE_PAGEREFS
916 pageref
= state
.pdflut
.objs
[pageno
];
918 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
920 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
921 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
923 if (state
.trimmargins
) {
928 page
= pdf_load_page (ctx
, pdf
, pageno
);
929 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
930 trim
= state
.trimanew
|| !obj
;
934 fz_matrix ctm
, page_ctm
;
936 dev
= fz_new_bbox_device (ctx
, &rect
);
937 dev
->hints
|= FZ_IGNORE_SHADE
;
938 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
939 fz_invert_matrix (&ctm
, &page_ctm
);
940 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
941 fz_close_device (ctx
, dev
);
942 fz_drop_device (ctx
, dev
);
944 rect
.x0
+= state
.trimfuzz
.x0
;
945 rect
.x1
+= state
.trimfuzz
.x1
;
946 rect
.y0
+= state
.trimfuzz
.y0
;
947 rect
.y1
+= state
.trimfuzz
.y1
;
948 fz_transform_rect (&rect
, &ctm
);
949 fz_intersect_rect (&rect
, &mediabox
);
951 if (!fz_is_empty_rect (&rect
)) {
955 obj
= pdf_new_array (ctx
, pdf
, 4);
956 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
958 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
960 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
962 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
964 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
967 mediabox
.x0
= pdf_to_real (ctx
,
968 pdf_array_get (ctx
, obj
, 0));
969 mediabox
.y0
= pdf_to_real (ctx
,
970 pdf_array_get (ctx
, obj
, 1));
971 mediabox
.x1
= pdf_to_real (ctx
,
972 pdf_array_get (ctx
, obj
, 2));
973 mediabox
.y1
= pdf_to_real (ctx
,
974 pdf_array_get (ctx
, obj
, 3));
977 fz_drop_page (ctx
, &page
->super
);
978 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
980 printd ("progress %f Trimming %d",
981 (double) (pageno
+ 1) / state
.pagecount
,
986 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
994 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
996 if (fz_is_empty_rect (&mediabox
)) {
1005 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1007 if (!fz_is_empty_rect (&cropbox
)) {
1012 fz_intersect_rect (&mediabox
, &cropbox
);
1017 if (fz_is_empty_rect (&rootmediabox
)) {
1019 "cannot find page size for page %d\n",
1023 mediabox
= rootmediabox
;
1030 if (state
.trimmargins
&& trimw
) {
1034 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1035 fz_bound_page (ctx
, page
, &mediabox
);
1036 if (state
.trimmargins
) {
1040 dev
= fz_new_bbox_device (ctx
, &rect
);
1041 dev
->hints
|= FZ_IGNORE_SHADE
;
1042 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1043 fz_close_device (ctx
, dev
);
1044 fz_drop_device (ctx
, dev
);
1046 rect
.x0
+= state
.trimfuzz
.x0
;
1047 rect
.x1
+= state
.trimfuzz
.x1
;
1048 rect
.y0
+= state
.trimfuzz
.y0
;
1049 rect
.y1
+= state
.trimfuzz
.y1
;
1050 fz_intersect_rect (&rect
, &mediabox
);
1052 if (!fz_is_empty_rect (&rect
)) {
1056 fz_drop_page (ctx
, page
);
1058 printd ("progress %f loading %d",
1059 (double) (pageno
+ 1) / state
.pagecount
,
1066 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1068 err (1, "fwrite trim mediabox");
1074 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1076 err (1, "fread trim mediabox %d", pageno
);
1082 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1083 fz_bound_page (ctx
, page
, &mediabox
);
1084 fz_drop_page (ctx
, page
);
1086 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1088 printd ("progress %f Gathering dimensions %d",
1089 (double) (pageno
) / state
.pagecount
,
1094 fprintf (stderr
, "failed to load page %d\n", pageno
);
1100 if (state
.pagedimcount
== 0
1101 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1102 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1105 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1106 state
.pagedims
= realloc (state
.pagedims
, size
);
1107 if (!state
.pagedims
) {
1108 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1109 size
, state
.pagedimcount
+ 1);
1112 p
= &state
.pagedims
[state
.pagedimcount
++];
1114 p
->mediabox
= mediabox
;
1120 printd ("progress 1 %s %d pages in %f seconds",
1121 state
.trimmargins
? "Trimmed" : "Processed",
1122 state
.pagecount
, end
- start
);
1126 if (fclose (trimf
)) {
1132 static void layout (void)
1137 struct pagedim
*p
= p
;
1138 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1140 if (state
.pagedimcount
== 0) return;
1142 switch (state
.fitmodel
) {
1143 case FitProportional
:
1144 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1147 p
= &state
.pagedims
[pindex
];
1148 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1150 fz_transform_rect (&box
, &rm
);
1152 x0
= fz_min (box
.x0
, box
.x1
);
1153 x1
= fz_max (box
.x0
, box
.x1
);
1156 maxw
= fz_max (w
, maxw
);
1157 zoom
= state
.w
/ maxw
;
1169 ARSERT (0 && state
.fitmodel
);
1172 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1176 p
= &state
.pagedims
[pindex
];
1177 fz_rotate (&ctm
, state
.rotate
);
1178 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1180 fz_transform_rect (&box
, &rm
);
1181 w
= box
.x1
- box
.x0
;
1182 switch (state
.fitmodel
) {
1183 case FitProportional
:
1184 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1190 h
= box
.y1
- box
.y0
;
1192 zoom
= fz_min (zw
, zh
);
1193 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1202 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1203 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1205 fz_rotate (&rm
, p
->rotate
);
1206 p
->pagebox
= p
->mediabox
;
1207 fz_transform_rect (&p
->pagebox
, &rm
);
1208 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1209 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1213 fz_transform_rect (&rect
, &ctm
);
1214 fz_round_rect (&p
->bounds
, &rect
);
1217 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1218 fz_scale (&sm
, zoom
, -zoom
);
1219 fz_concat (&ctm
, &tm
, &sm
);
1225 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1226 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1227 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1228 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1229 int boundw
= x1
- x0
;
1230 int boundh
= y1
- y0
;
1232 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1233 } while (p
-- != state
.pagedims
);
1236 struct pagedim
*pdimofpageno (int pageno
)
1238 struct pagedim
*pdim
= state
.pagedims
;
1240 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1241 if (state
.pagedims
[i
].pageno
> pageno
)
1243 pdim
= &state
.pagedims
[i
];
1249 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1250 uritoanchor (const char *uri
)
1256 a
.n
= fz_resolve_link (state
.ctx
, state
.doc
, uri
, &p
.x
, &p
.y
);
1258 struct pagedim
*pdim
= pdimofpageno (a
.n
);
1259 fz_transform_point (&p
, &pdim
->ctm
);
1262 a
.h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1267 static void recurse_outline (fz_outline
*outline
, int level
)
1270 struct anchor a
= uritoanchor (outline
->uri
);
1272 printd ("o %d %d %d %d %s", level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1275 printd ("on %d %s", level
, outline
->title
);
1277 if (outline
->down
) {
1278 recurse_outline (outline
->down
, level
+ 1);
1280 outline
= outline
->next
;
1284 static void process_outline (void)
1286 fz_outline
*outline
;
1288 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1290 state
.needoutline
= 0;
1291 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1293 recurse_outline (outline
, 0);
1294 fz_drop_outline (state
.ctx
, outline
);
1298 static char *strofspan (fz_stext_span
*span
)
1303 size_t size
= 0, cap
= 80;
1305 p
= malloc (cap
+ 1);
1306 if (!p
) return NULL
;
1308 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1309 int n
= fz_runetochar (utf8
, ch
->c
);
1310 if (size
+ n
> cap
) {
1312 p
= realloc (p
, cap
+ 1);
1313 if (!p
) return NULL
;
1316 memcpy (p
+ size
, utf8
, n
);
1323 static int matchspan (regex_t
*re
, fz_stext_span
*span
,
1324 int stop
, int pageno
, double start
)
1331 fz_point p1
, p2
, p3
, p4
;
1333 p
= strofspan (span
);
1336 ret
= regexec (re
, p
, 1, &rm
, 0);
1339 if (ret
!= REG_NOMATCH
) {
1342 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1343 printd ("msg regexec error `%.*s'",
1344 (int) size
, errbuf
);
1352 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1353 c
+= fz_runelen (span
->text
[a
].c
);
1355 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1356 c
+= fz_runelen (span
->text
[b
].c
);
1359 if (fz_runelen (span
->text
[b
].c
) > 1) {
1360 b
= fz_maxi (0, b
-1);
1363 fz_stext_char_bbox (state
.ctx
, &sb
, span
, a
);
1364 fz_stext_char_bbox (state
.ctx
, &eb
, span
, b
);
1376 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1383 printd ("progress 1 found at %d `%.*s' in %f sec",
1384 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1388 printd ("match %d %d %f %f %f %f %f %f %f %f",
1400 static int compareblocks (const void *l
, const void *r
)
1402 fz_stext_block
const *ls
= l
;
1403 fz_stext_block
const *rs
= r
;
1404 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1407 /* wishful thinking function */
1408 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1412 fz_stext_page
*text
;
1413 fz_stext_sheet
*sheet
;
1414 struct pagedim
*pdim
;
1415 int stop
= 0, niters
= 0;
1420 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1421 if (niters
++ == 5) {
1424 printd ("progress 1 attention requested aborting search at %d",
1429 printd ("progress %f searching in page %d",
1430 (double) (pageno
+ 1) / state
.pagecount
,
1434 pdim
= pdimofpageno (pageno
);
1435 sheet
= fz_new_stext_sheet (state
.ctx
);
1436 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1437 tdev
= fz_new_stext_device (state
.ctx
, sheet
, text
, 0);
1439 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1441 fz_matrix ctm
= pagectm1 (page
, pdim
);
1442 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1445 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1446 fz_close_device (state
.ctx
, tdev
);
1447 fz_drop_device (state
.ctx
, tdev
);
1449 for (j
= 0; j
< text
->len
; ++j
) {
1452 fz_stext_block
*block
;
1454 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1455 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1458 for (k
= 0; k
< block
->len
; ++k
) {
1459 fz_stext_line
*line
;
1460 fz_stext_span
*span
;
1463 line
= &block
->lines
[k
];
1464 if (line
->bbox
.y0
< y
+ 1) continue;
1467 line
= &block
->lines
[block
->len
- 1 - k
];
1468 if (line
->bbox
.y0
> y
- 1) continue;
1471 for (span
= line
->first_span
; span
; span
= span
->next
) {
1472 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1474 case 1: stop
= 1; break;
1475 case -1: stop
= 1; goto endloop
;
1489 fz_drop_stext_page (state
.ctx
, text
);
1490 fz_drop_stext_sheet (state
.ctx
, sheet
);
1491 fz_drop_page (state
.ctx
, page
);
1495 printd ("progress 1 no matches %f sec", end
- start
);
1497 printd ("clearrects");
1500 static void set_tex_params (int colorspace
)
1507 switch (colorspace
) {
1509 state
.texiform
= GL_RGBA8
;
1510 state
.texform
= GL_RGBA
;
1511 state
.texty
= GL_UNSIGNED_BYTE
;
1512 state
.colorspace
= fz_device_rgb (state
.ctx
);
1515 state
.texiform
= GL_RGBA8
;
1516 state
.texform
= GL_BGRA
;
1517 state
.texty
= endianness
.s
> 1
1518 ? GL_UNSIGNED_INT_8_8_8_8
1519 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1520 state
.colorspace
= fz_device_bgr (state
.ctx
);
1523 state
.texiform
= GL_LUMINANCE_ALPHA
;
1524 state
.texform
= GL_LUMINANCE_ALPHA
;
1525 state
.texty
= GL_UNSIGNED_BYTE
;
1526 state
.colorspace
= fz_device_gray (state
.ctx
);
1529 errx (1, "invalid colorspce %d", colorspace
);
1533 static void realloctexts (int texcount
)
1537 if (texcount
== state
.texcount
) return;
1539 if (texcount
< state
.texcount
) {
1540 glDeleteTextures (state
.texcount
- texcount
,
1541 state
.texids
+ texcount
);
1544 size
= texcount
* sizeof (*state
.texids
);
1545 state
.texids
= realloc (state
.texids
, size
);
1546 if (!state
.texids
) {
1547 err (1, "realloc texids %" FMT_s
, size
);
1550 size
= texcount
* sizeof (*state
.texowners
);
1551 state
.texowners
= realloc (state
.texowners
, size
);
1552 if (!state
.texowners
) {
1553 err (1, "realloc texowners %" FMT_s
, size
);
1555 if (texcount
> state
.texcount
) {
1556 glGenTextures (texcount
- state
.texcount
,
1557 state
.texids
+ state
.texcount
);
1558 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1559 state
.texowners
[i
].w
= -1;
1560 state
.texowners
[i
].slice
= NULL
;
1563 state
.texcount
= texcount
;
1567 static char *mbtoutf8 (char *s
)
1573 len
= mbstowcs (NULL
, s
, strlen (s
));
1578 if (len
== (size_t) -1) {
1583 tmp
= malloc (len
* sizeof (wchar_t));
1588 ret
= mbstowcs (tmp
, s
, len
);
1589 if (ret
== (size_t) -1) {
1595 for (i
= 0; i
< ret
; ++i
) {
1596 len
+= fz_runelen (tmp
[i
]);
1599 p
= r
= malloc (len
+ 1);
1605 for (i
= 0; i
< ret
; ++i
) {
1606 p
+= fz_runetochar (p
, tmp
[i
]);
1613 CAMLprim value
ml_mbtoutf8 (value s_v
)
1619 s
= String_val (s_v
);
1625 ret_v
= caml_copy_string (r
);
1631 static void * mainloop (void UNUSED_ATTR
*unused
)
1634 int len
, ret
, oldlen
= 0;
1639 len
= readlen (state
.csock
);
1641 errx (1, "readlen returned 0");
1644 if (oldlen
< len
+ 1) {
1645 p
= realloc (p
, len
+ 1);
1647 err (1, "realloc %d failed", len
+ 1);
1651 readdata (state
.csock
, p
, len
);
1654 if (!strncmp ("open", p
, 4)) {
1655 int wthack
, off
, usedoccss
, ok
= 0;
1662 ret
= sscanf (p
+ 5, " %d %d %d %n",
1663 &wthack
, &state
.cxack
, &usedoccss
, &off
);
1665 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1668 filename
= p
+ 5 + off
;
1669 filenamelen
= strlen (filename
);
1670 password
= filename
+ filenamelen
+ 1;
1672 if (password
[strlen (password
) + 1]) {
1673 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1677 fz_set_use_document_css (state
.ctx
, usedoccss
);
1678 fz_try (state
.ctx
) {
1679 ok
= openxref (filename
, password
);
1681 fz_catch (state
.ctx
) {
1682 utf8filename
= mbtoutf8 (filename
);
1683 printd ("msg Could not open %s", utf8filename
);
1693 utf8filename
= mbtoutf8 (filename
);
1694 printd ("msg Opened %s (press h/F1 to get help)",
1696 if (utf8filename
!= filename
) {
1697 free (utf8filename
);
1700 state
.needoutline
= 1;
1703 else if (!strncmp ("cs", p
, 2)) {
1706 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1708 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1711 set_tex_params (colorspace
);
1712 for (i
= 0; i
< state
.texcount
; ++i
) {
1713 state
.texowners
[i
].w
= -1;
1714 state
.texowners
[i
].slice
= NULL
;
1718 else if (!strncmp ("freepage", p
, 8)) {
1721 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1723 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1727 else if (!strncmp ("freetile", p
, 8)) {
1730 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1732 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1736 else if (!strncmp ("search", p
, 6)) {
1737 int icase
, pageno
, y
, len2
, forward
;
1741 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1742 &icase
, &pageno
, &y
, &forward
, &len2
);
1744 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1747 pattern
= p
+ 6 + len2
;
1748 ret
= regcomp (&re
, pattern
,
1749 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1754 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1755 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1758 search (&re
, pageno
, y
, forward
);
1762 else if (!strncmp ("geometry", p
, 8)) {
1766 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1768 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1775 for (int i
= 0; i
< state
.texcount
; ++i
) {
1776 state
.texowners
[i
].slice
= NULL
;
1779 state
.fitmodel
= fitmodel
;
1784 unlock ("geometry");
1785 printd ("continue %d", state
.pagecount
);
1787 else if (!strncmp ("reqlayout", p
, 9)) {
1790 unsigned int fitmodel
;
1794 ret
= sscanf (p
+ 9, " %d %u %d %n",
1795 &rotate
, &fitmodel
, &h
, &off
);
1797 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1800 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1801 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1804 state
.rotate
= rotate
;
1805 state
.fitmodel
= fitmodel
;
1810 nameddest
= p
+ 9 + off
;
1811 if (pdf
&& nameddest
&& *nameddest
) {
1813 struct pagedim
*pdim
;
1814 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1816 pdim
= pdimofpageno (pageno
);
1817 fz_transform_point (&xy
, &pdim
->ctm
);
1818 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1822 unlock ("reqlayout");
1823 printd ("continue %d", state
.pagecount
);
1825 else if (!strncmp ("page", p
, 4)) {
1830 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1832 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1837 page
= loadpage (pageno
, pindex
);
1841 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1843 else if (!strncmp ("tile", p
, 4)) {
1850 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1851 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1852 SCN_ptr_cast (&data
));
1854 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1859 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1863 printd ("tile %d %d %" FMT_ptr
" %u %f",
1865 FMT_ptr_cast (tile
),
1866 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1869 else if (!strncmp ("trimset", p
, 7)) {
1873 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1874 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1876 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1879 state
.trimmargins
= trimmargins
;
1880 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1882 state
.trimfuzz
= fuzz
;
1886 else if (!strncmp ("settrim", p
, 7)) {
1890 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1891 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1893 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1897 state
.trimmargins
= trimmargins
;
1898 state
.needoutline
= 1;
1899 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1901 state
.trimfuzz
= fuzz
;
1903 state
.pagedimcount
= 0;
1904 free (state
.pagedims
);
1905 state
.pagedims
= NULL
;
1910 printd ("continue %d", state
.pagecount
);
1912 else if (!strncmp ("sliceh", p
, 6)) {
1915 ret
= sscanf (p
+ 6, " %d", &h
);
1917 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1919 if (h
!= state
.sliceheight
) {
1920 state
.sliceheight
= h
;
1921 for (int i
= 0; i
< state
.texcount
; ++i
) {
1922 state
.texowners
[i
].w
= -1;
1923 state
.texowners
[i
].h
= -1;
1924 state
.texowners
[i
].slice
= NULL
;
1928 else if (!strncmp ("interrupt", p
, 9)) {
1929 printd ("vmsg interrupted");
1932 errx (1, "unknown command %.*s", len
, p
);
1938 CAMLprim value
ml_isexternallink (value uri_v
)
1941 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1942 CAMLreturn (Val_bool (ext
));
1945 CAMLprim value
ml_uritolocation (value uri_v
)
1951 struct pagedim
*pdim
;
1953 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1955 pdim
= pdimofpageno (pageno
);
1956 fz_transform_point (&xy
, &pdim
->ctm
);
1957 ret_v
= caml_alloc_tuple (3);
1958 Field (ret_v
, 0) = Val_int (pageno
);
1959 Field (ret_v
, 1) = caml_copy_double (xy
.x
);
1960 Field (ret_v
, 2) = caml_copy_double (xy
.y
);
1964 CAMLprim value
ml_realloctexts (value texcount_v
)
1966 CAMLparam1 (texcount_v
);
1969 if (trylock (__func__
)) {
1973 realloctexts (Int_val (texcount_v
));
1978 CAMLreturn (Val_bool (ok
));
1981 static void recti (int x0
, int y0
, int x1
, int y1
)
1983 GLfloat
*v
= state
.vertices
;
1985 glVertexPointer (2, GL_FLOAT
, 0, v
);
1986 v
[0] = x0
; v
[1] = y0
;
1987 v
[2] = x1
; v
[3] = y0
;
1988 v
[4] = x0
; v
[5] = y1
;
1989 v
[6] = x1
; v
[7] = y1
;
1990 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1993 static void showsel (struct page
*page
, int ox
, int oy
)
1998 fz_stext_line
*line
;
1999 fz_page_block
*pageb
;
2000 fz_stext_block
*block
;
2001 struct mark first
, last
;
2002 unsigned char selcolor
[] = {15,15,15,140};
2004 first
= page
->fmark
;
2007 if (!first
.span
|| !last
.span
) return;
2009 glEnable (GL_BLEND
);
2010 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2011 glColor4ubv (selcolor
);
2013 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2014 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2015 for (pageb
= page
->text
->blocks
;
2016 pageb
< page
->text
->blocks
+ page
->text
->len
;
2018 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2019 block
= pageb
->u
.text
;
2021 for (line
= block
->lines
;
2022 line
< block
->lines
+ block
->len
;
2024 fz_stext_span
*span
;
2025 rect
= fz_empty_rect
;
2027 for (span
= line
->first_span
; span
; span
= span
->next
) {
2029 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2034 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2036 j
= fz_mini (first
.i
, last
.i
);
2037 k
= fz_maxi (first
.i
, last
.i
);
2040 if (span
== first
.span
) {
2044 else if (span
== last
.span
) {
2051 for (i
= j
; i
<= k
; ++i
) {
2053 fz_union_rect (&rect
,
2054 fz_stext_char_bbox (state
.ctx
, &bbox1
,
2057 fz_round_rect (&bbox
, &rect
);
2058 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2065 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2066 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2067 if (span
== last
.span
) {
2070 rect
= fz_empty_rect
;
2076 glDisable (GL_BLEND
);
2081 static void stipplerect (fz_matrix
*m
,
2089 fz_transform_point (p1
, m
);
2090 fz_transform_point (p2
, m
);
2091 fz_transform_point (p3
, m
);
2092 fz_transform_point (p4
, m
);
2098 t
= hypotf (w
, h
) * .25f
;
2102 s
= hypotf (w
, h
) * .25f
;
2104 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2105 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2107 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2108 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2110 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2111 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2113 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2114 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2116 glDrawArrays (GL_LINES
, 0, 8);
2119 static void solidrect (fz_matrix
*m
,
2126 fz_transform_point (p1
, m
);
2127 fz_transform_point (p2
, m
);
2128 fz_transform_point (p3
, m
);
2129 fz_transform_point (p4
, m
);
2130 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2131 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2133 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2134 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2135 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2138 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2140 fz_matrix ctm
, tm
, pm
;
2141 fz_link
*link
, *links
;
2142 GLfloat
*texcoords
= state
.texcoords
;
2143 GLfloat
*vertices
= state
.vertices
;
2145 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2147 glEnable (GL_TEXTURE_1D
);
2148 glEnable (GL_BLEND
);
2149 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2150 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2152 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2153 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2154 fz_translate (&tm
, xoff
, yoff
);
2155 pm
= pagectm (page
);
2156 fz_concat (&ctm
, &pm
, &tm
);
2158 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2159 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2161 for (link
= links
; link
; link
= link
->next
) {
2162 fz_point p1
, p2
, p3
, p4
;
2164 p1
.x
= link
->rect
.x0
;
2165 p1
.y
= link
->rect
.y0
;
2167 p2
.x
= link
->rect
.x1
;
2168 p2
.y
= link
->rect
.y0
;
2170 p3
.x
= link
->rect
.x1
;
2171 p3
.y
= link
->rect
.y1
;
2173 p4
.x
= link
->rect
.x0
;
2174 p4
.y
= link
->rect
.y1
;
2176 /* TODO: different colours for different schemes */
2177 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
2178 else glColor3ub (255, 0, 0);
2180 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2183 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2184 fz_point p1
, p2
, p3
, p4
;
2185 struct annot
*annot
= &page
->annots
[i
];
2187 p1
.x
= annot
->bbox
.x0
;
2188 p1
.y
= annot
->bbox
.y0
;
2190 p2
.x
= annot
->bbox
.x1
;
2191 p2
.y
= annot
->bbox
.y0
;
2193 p3
.x
= annot
->bbox
.x1
;
2194 p3
.y
= annot
->bbox
.y1
;
2196 p4
.x
= annot
->bbox
.x0
;
2197 p4
.y
= annot
->bbox
.y1
;
2199 glColor3ub (0, 0, 128);
2200 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2203 glDisable (GL_BLEND
);
2204 glDisable (GL_TEXTURE_1D
);
2207 static int compareslinks (const void *l
, const void *r
)
2209 struct slink
const *ls
= l
;
2210 struct slink
const *rs
= r
;
2211 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2212 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2214 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2217 static void droptext (struct page
*page
)
2220 fz_drop_stext_page (state
.ctx
, page
->text
);
2223 page
->fmark
.span
= NULL
;
2224 page
->lmark
.span
= NULL
;
2228 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
2233 static void dropannots (struct page
*page
)
2236 free (page
->annots
);
2237 page
->annots
= NULL
;
2238 page
->annotcount
= 0;
2242 static void ensureannots (struct page
*page
)
2245 size_t annotsize
= sizeof (*page
->annots
);
2248 if (state
.gen
!= page
->agen
) {
2250 page
->agen
= state
.gen
;
2252 if (page
->annots
) return;
2254 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2256 annot
= fz_next_annot (state
.ctx
, annot
)) {
2261 page
->annotcount
= count
;
2262 page
->annots
= calloc (count
, annotsize
);
2263 if (!page
->annots
) {
2264 err (1, "calloc annots %d", count
);
2267 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2269 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2272 fz_bound_annot (state
.ctx
, annot
, &rect
);
2273 page
->annots
[i
].annot
= annot
;
2274 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2279 static void dropslinks (struct page
*page
)
2282 free (page
->slinks
);
2283 page
->slinks
= NULL
;
2284 page
->slinkcount
= 0;
2288 static void ensureslinks (struct page
*page
)
2292 size_t slinksize
= sizeof (*page
->slinks
);
2293 fz_link
*link
, *links
;
2295 ensureannots (page
);
2296 if (state
.gen
!= page
->sgen
) {
2298 page
->sgen
= state
.gen
;
2300 if (page
->slinks
) return;
2302 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2303 ctm
= pagectm (page
);
2305 count
= page
->annotcount
;
2306 for (link
= links
; link
; link
= link
->next
) {
2312 page
->slinkcount
= count
;
2313 page
->slinks
= calloc (count
, slinksize
);
2314 if (!page
->slinks
) {
2315 err (1, "calloc slinks %d", count
);
2318 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2322 fz_transform_rect (&rect
, &ctm
);
2323 page
->slinks
[i
].tag
= SLINK
;
2324 page
->slinks
[i
].u
.link
= link
;
2325 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2327 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2329 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2330 fz_transform_rect (&rect
, &ctm
);
2331 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2333 page
->slinks
[i
].tag
= SANNOT
;
2334 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2336 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2340 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2341 static void fmt_linkn (char *s
, unsigned int u
)
2343 unsigned int len
; unsigned int q
;
2344 unsigned int zma
= 'z' - 'a' + 1;
2346 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2349 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2350 /* handles u == 0 */
2355 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2356 int noff
, char *targ
, int tlen
, int hfsize
)
2359 struct slink
*slink
;
2360 double x0
, y0
, x1
, y1
, w
;
2362 ensureslinks (page
);
2363 glColor3ub (0xc3, 0xb0, 0x91);
2364 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2365 fmt_linkn (buf
, i
+ noff
);
2366 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2367 slink
= &page
->slinks
[i
];
2369 x0
= slink
->bbox
.x0
+ xoff
- 5;
2370 y1
= slink
->bbox
.y0
+ yoff
- 5;
2371 y0
= y1
+ 10 + hfsize
;
2372 w
= measure_string (state
.face
, hfsize
, buf
);
2374 recti (x0
, y0
, x1
, y1
);
2378 glEnable (GL_BLEND
);
2379 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2380 glEnable (GL_TEXTURE_2D
);
2381 glColor3ub (0, 0, 0);
2382 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2383 fmt_linkn (buf
, i
+ noff
);
2384 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2385 slink
= &page
->slinks
[i
];
2387 x0
= slink
->bbox
.x0
+ xoff
;
2388 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2389 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2392 glDisable (GL_TEXTURE_2D
);
2393 glDisable (GL_BLEND
);
2396 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2399 struct slice
*slice1
;
2400 unsigned char *texdata
;
2403 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2404 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2406 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2407 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2408 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2412 int texindex
= state
.texindex
++ % state
.texcount
;
2414 if (state
.texowners
[texindex
].w
== tile
->w
) {
2415 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2419 state
.texowners
[texindex
].h
= slice
->h
;
2423 state
.texowners
[texindex
].h
= slice
->h
;
2426 state
.texowners
[texindex
].w
= tile
->w
;
2427 state
.texowners
[texindex
].slice
= slice
;
2428 slice
->texindex
= texindex
;
2430 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2431 #if TEXT_TYPE == GL_TEXTURE_2D
2432 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2433 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2434 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2435 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2438 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2442 texdata
= tile
->pixmap
->samples
;
2445 glTexSubImage2D (TEXT_TYPE
,
2457 glTexImage2D (TEXT_TYPE
,
2469 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2474 CAMLprim value
ml_begintiles (value unit_v
)
2476 CAMLparam1 (unit_v
);
2477 glEnable (TEXT_TYPE
);
2478 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2479 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2480 CAMLreturn (unit_v
);
2483 CAMLprim value
ml_endtiles (value unit_v
)
2485 CAMLparam1 (unit_v
);
2486 glDisable (TEXT_TYPE
);
2487 CAMLreturn (unit_v
);
2490 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2492 CAMLparam2 (args_v
, ptr_v
);
2493 int dispx
= Int_val (Field (args_v
, 0));
2494 int dispy
= Int_val (Field (args_v
, 1));
2495 int dispw
= Int_val (Field (args_v
, 2));
2496 int disph
= Int_val (Field (args_v
, 3));
2497 int tilex
= Int_val (Field (args_v
, 4));
2498 int tiley
= Int_val (Field (args_v
, 5));
2499 char *s
= String_val (ptr_v
);
2500 struct tile
*tile
= parse_pointer (__func__
, s
);
2501 int slicey
, firstslice
;
2502 struct slice
*slice
;
2503 GLfloat
*texcoords
= state
.texcoords
;
2504 GLfloat
*vertices
= state
.vertices
;
2506 firstslice
= tiley
/ tile
->sliceheight
;
2507 slice
= &tile
->slices
[firstslice
];
2508 slicey
= tiley
% tile
->sliceheight
;
2513 dh
= slice
->h
- slicey
;
2514 dh
= fz_mini (disph
, dh
);
2515 uploadslice (tile
, slice
);
2517 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2518 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2519 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2520 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2522 vertices
[0] = dispx
; vertices
[1] = dispy
;
2523 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2524 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2525 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2527 #if TEXT_TYPE == GL_TEXTURE_2D
2528 for (int i
= 0; i
< 8; ++i
) {
2529 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2533 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2537 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2543 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2545 fz_matrix ctm
, tm
, pm
;
2546 fz_point p1
, p2
, p3
, p4
;
2547 GLfloat
*vertices
= state
.vertices
;
2548 double *v
= (double *) rects_v
;
2550 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2551 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2552 fz_translate (&tm
, xoff
, yoff
);
2553 pm
= pagectm (page
);
2554 fz_concat (&ctm
, &pm
, &tm
);
2556 glEnable (GL_BLEND
);
2557 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2571 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2572 glDisable (GL_BLEND
);
2575 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2576 value xoff_v
, value yoff_v
,
2579 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2580 int xoff
= Int_val (xoff_v
);
2581 int yoff
= Int_val (yoff_v
);
2582 int noff
= Int_val (Field (li_v
, 0));
2583 char *targ
= String_val (Field (li_v
, 1));
2584 int tlen
= caml_string_length (Field (li_v
, 1));
2585 int hfsize
= Int_val (Field (li_v
, 2));
2586 char *s
= String_val (ptr_v
);
2587 int hlmask
= Int_val (hlinks_v
);
2588 struct page
*page
= parse_pointer (__func__
, s
);
2590 if (!page
->fzpage
) {
2591 /* deal with loadpage failed pages */
2595 if (trylock (__func__
)) {
2600 ensureannots (page
);
2601 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2603 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2604 noff
= page
->slinkcount
;
2606 if (page
->tgen
== state
.gen
) {
2607 showsel (page
, xoff
, yoff
);
2612 CAMLreturn (Val_int (noff
));
2615 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2618 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2619 int xoff
= Int_val (xoff_v
);
2620 int yoff
= Int_val (yoff_v
);
2621 char *s
= String_val (ptr_v
);
2622 struct page
*page
= parse_pointer (__func__
, s
);
2624 drawprect (page
, xoff
, yoff
, rects_v
);
2628 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2632 const fz_matrix
*tctm
;
2633 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2635 if (!page
->annots
) return NULL
;
2638 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2639 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2642 tctm
= &fz_identity
;
2648 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2649 fz_invert_matrix (&ctm
, &ctm
);
2650 fz_transform_point (&p
, &ctm
);
2653 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2654 struct annot
*a
= &page
->annots
[i
];
2657 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2658 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2659 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2667 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2671 fz_link
*link
, *links
;
2673 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2678 ctm
= pagectm (page
);
2679 fz_invert_matrix (&ctm
, &ctm
);
2680 fz_transform_point (&p
, &ctm
);
2682 for (link
= links
; link
; link
= link
->next
) {
2683 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2684 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2692 static void ensuretext (struct page
*page
)
2694 if (state
.gen
!= page
->tgen
) {
2696 page
->tgen
= state
.gen
;
2702 page
->text
= fz_new_stext_page (state
.ctx
,
2703 &state
.pagedims
[page
->pdimno
].mediabox
);
2704 page
->sheet
= fz_new_stext_sheet (state
.ctx
);
2705 tdev
= fz_new_stext_device (state
.ctx
, page
->sheet
, page
->text
, 0);
2706 ctm
= pagectm (page
);
2707 fz_run_display_list (state
.ctx
, page
->dlist
,
2708 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2709 qsort (page
->text
->blocks
, page
->text
->len
,
2710 sizeof (*page
->text
->blocks
), compareblocks
);
2711 fz_close_device (state
.ctx
, tdev
);
2712 fz_drop_device (state
.ctx
, tdev
);
2716 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2718 CAMLparam2 (start_page_v
, dir_v
);
2720 int i
, dir
= Int_val (dir_v
);
2721 int start_page
= Int_val (start_page_v
);
2722 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2726 ret_v
= Val_int (0);
2728 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2729 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2734 pdf_page
*page
= NULL
;
2737 fz_try (state
.ctx
) {
2738 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2739 found
= !!page
->links
|| !!page
->annots
;
2741 fz_catch (state
.ctx
) {
2745 fz_drop_page (state
.ctx
, &page
->super
);
2749 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2750 found
= !!fz_load_links (state
.ctx
, page
);
2751 fz_drop_page (state
.ctx
, page
);
2755 ret_v
= caml_alloc_small (1, 1);
2756 Field (ret_v
, 0) = Val_int (i
);
2765 enum { dir_first
, dir_last
};
2766 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2768 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2770 CAMLparam2 (ptr_v
, dir_v
);
2771 CAMLlocal2 (ret_v
, pos_v
);
2773 int dirtag
, i
, slinkindex
;
2774 struct slink
*found
= NULL
,*slink
;
2775 char *s
= String_val (ptr_v
);
2777 page
= parse_pointer (__func__
, s
);
2778 ret_v
= Val_int (0);
2780 ensureslinks (page
);
2782 if (Is_block (dir_v
)) {
2783 dirtag
= Tag_val (dir_v
);
2785 case dir_first_visible
:
2787 int x0
, y0
, dir
, first_index
, last_index
;
2789 pos_v
= Field (dir_v
, 0);
2790 x0
= Int_val (Field (pos_v
, 0));
2791 y0
= Int_val (Field (pos_v
, 1));
2792 dir
= Int_val (Field (pos_v
, 2));
2797 last_index
= page
->slinkcount
;
2800 first_index
= page
->slinkcount
- 1;
2804 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2805 slink
= &page
->slinks
[i
];
2806 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2815 slinkindex
= Int_val (Field (dir_v
, 0));
2816 found
= &page
->slinks
[slinkindex
];
2817 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2818 slink
= &page
->slinks
[i
];
2819 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2827 slinkindex
= Int_val (Field (dir_v
, 0));
2828 found
= &page
->slinks
[slinkindex
];
2829 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2830 slink
= &page
->slinks
[i
];
2831 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2839 slinkindex
= Int_val (Field (dir_v
, 0));
2840 found
= &page
->slinks
[slinkindex
];
2841 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2842 slink
= &page
->slinks
[i
];
2843 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2851 slinkindex
= Int_val (Field (dir_v
, 0));
2852 found
= &page
->slinks
[slinkindex
];
2853 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2854 slink
= &page
->slinks
[i
];
2855 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2864 dirtag
= Int_val (dir_v
);
2867 found
= page
->slinks
;
2872 found
= page
->slinks
+ (page
->slinkcount
- 1);
2878 ret_v
= caml_alloc_small (2, 1);
2879 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2886 enum { uuri
, utext
, uannot
};
2888 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2890 CAMLparam2 (ptr_v
, n_v
);
2891 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2894 char *s
= String_val (ptr_v
);
2895 struct slink
*slink
;
2897 ret_v
= Val_int (0);
2898 page
= parse_pointer (__func__
, s
);
2901 ensureslinks (page
);
2902 slink
= &page
->slinks
[Int_val (n_v
)];
2903 if (slink
->tag
== SLINK
) {
2904 link
= slink
->u
.link
;
2905 str_v
= caml_copy_string (link
->uri
);
2906 ret_v
= caml_alloc_small (1, uuri
);
2907 Field (ret_v
, 0) = str_v
;
2910 ret_v
= caml_alloc_small (1, uannot
);
2911 tup_v
= caml_alloc_tuple (2);
2912 Field (ret_v
, 0) = tup_v
;
2913 Field (tup_v
, 0) = ptr_v
;
2914 Field (tup_v
, 1) = n_v
;
2921 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2923 CAMLparam2 (ptr_v
, n_v
);
2925 const char *contents
= "";
2928 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2930 char *s
= String_val (ptr_v
);
2932 struct slink
*slink
;
2934 page
= parse_pointer (__func__
, s
);
2935 slink
= &page
->slinks
[Int_val (n_v
)];
2936 contents
= pdf_annot_contents (state
.ctx
,
2937 (pdf_annot
*) slink
->u
.annot
);
2940 CAMLreturn (caml_copy_string (contents
));
2943 CAMLprim value
ml_getlinkcount (value ptr_v
)
2947 char *s
= String_val (ptr_v
);
2949 page
= parse_pointer (__func__
, s
);
2950 CAMLreturn (Val_int (page
->slinkcount
));
2953 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2955 CAMLparam2 (ptr_v
, n_v
);
2958 struct slink
*slink
;
2959 char *s
= String_val (ptr_v
);
2961 page
= parse_pointer (__func__
, s
);
2962 ret_v
= caml_alloc_tuple (4);
2964 ensureslinks (page
);
2966 slink
= &page
->slinks
[Int_val (n_v
)];
2967 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2968 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2969 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2970 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2975 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2977 CAMLparam3 (ptr_v
, x_v
, y_v
);
2978 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2980 struct annot
*annot
;
2982 char *ptr
= String_val (ptr_v
);
2983 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2984 struct pagedim
*pdim
;
2986 ret_v
= Val_int (0);
2987 if (trylock (__func__
)) {
2991 page
= parse_pointer (__func__
, ptr
);
2992 pdim
= &state
.pagedims
[page
->pdimno
];
2993 x
+= pdim
->bounds
.x0
;
2994 y
+= pdim
->bounds
.y0
;
2997 annot
= getannot (page
, x
, y
);
3001 ensureslinks (page
);
3002 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3003 if (page
->slinks
[i
].tag
== SANNOT
3004 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3009 ret_v
= caml_alloc_small (1, uannot
);
3010 tup_v
= caml_alloc_tuple (2);
3011 Field (ret_v
, 0) = tup_v
;
3012 Field (tup_v
, 0) = ptr_v
;
3013 Field (tup_v
, 1) = Val_int (n
);
3018 link
= getlink (page
, x
, y
);
3020 str_v
= caml_copy_string (link
->uri
);
3021 ret_v
= caml_alloc_small (1, uuri
);
3022 Field (ret_v
, 0) = str_v
;
3026 fz_page_block
*pageb
;
3027 fz_stext_block
*block
;
3030 for (pageb
= page
->text
->blocks
;
3031 pageb
< page
->text
->blocks
+ page
->text
->len
;
3033 fz_stext_line
*line
;
3034 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3035 block
= pageb
->u
.text
;
3038 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3041 for (line
= block
->lines
;
3042 line
< block
->lines
+ block
->len
;
3044 fz_stext_span
*span
;
3047 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3050 for (span
= line
->first_span
; span
; span
= span
->next
) {
3054 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3057 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3059 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3062 if (x
>= b
->x0
&& x
<= b
->x1
3063 && y
>= b
->y0
&& y
<= b
->y1
) {
3064 fz_stext_style
*style
= span
->text
->style
;
3067 ? fz_font_name (state
.ctx
, style
->font
)
3068 : "Span has no font name"
3070 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3072 if (face
&& face
->family_name
) {
3074 char *n1
= face
->family_name
;
3075 size_t l1
= strlen (n1
);
3076 size_t l2
= strlen (n2
);
3078 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3079 s
= malloc (l1
+ l2
+ 2);
3083 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3084 str_v
= caml_copy_string (s
);
3089 if (str_v
== Val_unit
) {
3090 str_v
= caml_copy_string (n2
);
3092 ret_v
= caml_alloc_small (1, utext
);
3093 Field (ret_v
, 0) = str_v
;
3108 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3110 static int uninteresting (int c
)
3112 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3116 CAMLprim
void ml_clearmark (value ptr_v
)
3119 char *s
= String_val (ptr_v
);
3122 if (trylock (__func__
)) {
3126 page
= parse_pointer (__func__
, s
);
3127 page
->fmark
.span
= NULL
;
3128 page
->lmark
.span
= NULL
;
3137 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3139 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3143 fz_stext_line
*line
;
3144 fz_page_block
*pageb
;
3145 fz_stext_block
*block
;
3146 struct pagedim
*pdim
;
3147 int mark
= Int_val (mark_v
);
3148 char *s
= String_val (ptr_v
);
3149 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3151 ret_v
= Val_bool (0);
3152 if (trylock (__func__
)) {
3156 page
= parse_pointer (__func__
, s
);
3157 pdim
= &state
.pagedims
[page
->pdimno
];
3161 if (mark
== mark_page
) {
3163 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3165 for (i
= 0; i
< page
->text
->len
; ++i
) {
3166 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3167 pb1
= &page
->text
->blocks
[i
];
3171 if (!pb1
) goto unlock
;
3173 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3174 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3175 pb2
= &page
->text
->blocks
[i
];
3179 if (!pb2
) goto unlock
;
3181 block
= pb1
->u
.text
;
3184 page
->fmark
.span
= block
->lines
->first_span
;
3186 block
= pb2
->u
.text
;
3187 line
= &block
->lines
[block
->len
- 1];
3188 page
->lmark
.i
= line
->last_span
->len
- 1;
3189 page
->lmark
.span
= line
->last_span
;
3190 ret_v
= Val_bool (1);
3194 x
+= pdim
->bounds
.x0
;
3195 y
+= pdim
->bounds
.y0
;
3197 for (pageb
= page
->text
->blocks
;
3198 pageb
< page
->text
->blocks
+ page
->text
->len
;
3200 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3201 block
= pageb
->u
.text
;
3204 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3207 if (mark
== mark_block
) {
3209 page
->fmark
.span
= block
->lines
->first_span
;
3211 line
= &block
->lines
[block
->len
- 1];
3212 page
->lmark
.i
= line
->last_span
->len
- 1;
3213 page
->lmark
.span
= line
->last_span
;
3214 ret_v
= Val_bool (1);
3218 for (line
= block
->lines
;
3219 line
< block
->lines
+ block
->len
;
3221 fz_stext_span
*span
;
3224 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3227 if (mark
== mark_line
) {
3229 page
->fmark
.span
= line
->first_span
;
3231 page
->lmark
.i
= line
->last_span
->len
- 1;
3232 page
->lmark
.span
= line
->last_span
;
3233 ret_v
= Val_bool (1);
3237 for (span
= line
->first_span
; span
; span
= span
->next
) {
3241 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3244 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3246 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3249 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3251 int charnum2
, charnum3
= -1, charnum4
= -1;
3253 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3255 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3256 if (uninteresting (span
->text
[charnum2
].c
)) {
3257 charnum3
= charnum2
+ 1;
3261 if (charnum3
== -1) charnum3
= 0;
3264 for (charnum2
= charnum
+ 1;
3265 charnum2
< span
->len
;
3267 if (uninteresting (span
->text
[charnum2
].c
)) break;
3268 charnum4
= charnum2
;
3271 page
->fmark
.i
= charnum3
;
3272 page
->fmark
.span
= span
;
3274 page
->lmark
.i
= charnum4
;
3275 page
->lmark
.span
= span
;
3276 ret_v
= Val_bool (1);
3284 if (!Bool_val (ret_v
)) {
3285 page
->fmark
.span
= NULL
;
3286 page
->lmark
.span
= NULL
;
3296 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3298 CAMLparam3 (ptr_v
, x_v
, y_v
);
3299 CAMLlocal2 (ret_v
, res_v
);
3302 fz_page_block
*pageb
;
3303 struct pagedim
*pdim
;
3304 char *s
= String_val (ptr_v
);
3305 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3307 ret_v
= Val_int (0);
3308 if (trylock (__func__
)) {
3312 page
= parse_pointer (__func__
, s
);
3313 pdim
= &state
.pagedims
[page
->pdimno
];
3314 x
+= pdim
->bounds
.x0
;
3315 y
+= pdim
->bounds
.y0
;
3319 for (pageb
= page
->text
->blocks
;
3320 pageb
< page
->text
->blocks
+ page
->text
->len
;
3322 switch (pageb
->type
) {
3323 case FZ_PAGE_BLOCK_TEXT
:
3324 b
= &pageb
->u
.text
->bbox
;
3327 case FZ_PAGE_BLOCK_IMAGE
:
3328 b
= &pageb
->u
.image
->bbox
;
3335 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3340 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3341 ret_v
= caml_alloc_small (1, 1);
3342 Store_double_field (res_v
, 0, b
->x0
);
3343 Store_double_field (res_v
, 1, b
->x1
);
3344 Store_double_field (res_v
, 2, b
->y0
);
3345 Store_double_field (res_v
, 3, b
->y1
);
3346 Field (ret_v
, 0) = res_v
;
3354 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3356 CAMLparam2 (ptr_v
, rect_v
);
3359 struct pagedim
*pdim
;
3360 char *s
= String_val (ptr_v
);
3361 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3362 fz_stext_line
*line
;
3363 fz_page_block
*pageb
;
3364 fz_stext_block
*block
;
3365 fz_stext_span
*span
, *fspan
, *lspan
;
3367 if (trylock (__func__
)) {
3371 page
= parse_pointer (__func__
, s
);
3374 pdim
= &state
.pagedims
[page
->pdimno
];
3375 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3376 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3377 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3378 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3389 fspan
= page
->fmark
.span
;
3392 lspan
= page
->lmark
.span
;
3394 for (pageb
= page
->text
->blocks
;
3395 pageb
< page
->text
->blocks
+ page
->text
->len
;
3397 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3398 block
= pageb
->u
.text
;
3399 for (line
= block
->lines
;
3400 line
< block
->lines
+ block
->len
;
3403 for (span
= line
->first_span
; span
; span
= span
->next
) {
3404 for (i
= 0; i
< span
->len
; ++i
) {
3405 fz_stext_char_bbox (state
.ctx
, &b
, span
, i
);
3407 if (x0
>= b
.x0
&& x0
<= b
.x1
3408 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3412 if (x1
>= b
.x0
&& x1
<= b
.x1
3413 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3421 if (x1
< x0
&& fspan
== lspan
) {
3433 page
->fmark
.span
= fspan
;
3436 page
->lmark
.span
= lspan
;
3444 static int UNUSED_ATTR
pipespan (FILE *f
, fz_stext_span
*span
, int a
, int b
)
3449 for (i
= a
; i
<= b
; ++i
) {
3450 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3451 ret
= fwrite (buf
, len
, 1, f
);
3454 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3455 len
, ret
, strerror (errno
));
3463 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3465 caml_failwith ("ml_popen not implemented under Cygwin");
3468 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3470 CAMLparam2 (command_v
, fds_v
);
3471 CAMLlocal2 (l_v
, tup_v
);
3475 value earg_v
= Nothing
;
3476 posix_spawnattr_t attr
;
3477 posix_spawn_file_actions_t fa
;
3478 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3480 argv
[2] = String_val (command_v
);
3482 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3483 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3486 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3487 msg
= "posix_spawnattr_init";
3491 #ifdef POSIX_SPAWN_USEVFORK
3492 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3493 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3498 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3501 tup_v
= Field (l_v
, 0);
3502 fd1
= Int_val (Field (tup_v
, 0));
3503 fd2
= Int_val (Field (tup_v
, 1));
3505 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3506 msg
= "posix_spawn_file_actions_addclose";
3512 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3513 msg
= "posix_spawn_file_actions_adddup2";
3520 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3521 msg
= "posix_spawn";
3526 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3527 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3531 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3532 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3537 unix_error (ret
, msg
, earg_v
);
3539 CAMLreturn (Val_int (pid
));
3543 CAMLprim value
ml_hassel (value ptr_v
)
3548 char *s
= String_val (ptr_v
);
3550 ret_v
= Val_bool (0);
3551 if (trylock (__func__
)) {
3555 page
= parse_pointer (__func__
, s
);
3556 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3562 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3564 CAMLparam2 (fd_v
, ptr_v
);
3568 fz_stext_line
*line
;
3569 fz_page_block
*pageb
;
3570 fz_stext_block
*block
;
3571 int fd
= Int_val (fd_v
);
3572 char *s
= String_val (ptr_v
);
3574 if (trylock (__func__
)) {
3578 page
= parse_pointer (__func__
, s
);
3580 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3581 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3585 f
= fdopen (fd
, "w");
3587 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3588 fd
, strerror (errno
));
3592 for (pageb
= page
->text
->blocks
;
3593 pageb
< page
->text
->blocks
+ page
->text
->len
;
3595 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3596 block
= pageb
->u
.text
;
3597 for (line
= block
->lines
;
3598 line
< block
->lines
+ block
->len
;
3600 fz_stext_span
*span
;
3602 for (span
= line
->first_span
; span
; span
= span
->next
) {
3605 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3606 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3607 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3610 if (pipespan (f
, span
, a
, b
)) {
3613 if (span
== page
->lmark
.span
) {
3616 if (span
== line
->last_span
) {
3617 if (putc ('\n', f
) == EOF
) {
3619 "failed break line on sel pipe: %s\n",
3630 int ret
= fclose (f
);
3633 if (errno
!= ECHILD
) {
3634 fprintf (stderr
, "failed to close sel pipe: %s\n",
3645 fprintf (stderr
, "failed to close sel pipe: %s\n",
3652 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3654 CAMLparam1 (pagedimno_v
);
3656 int pagedimno
= Int_val (pagedimno_v
);
3659 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3660 if (trylock (__func__
)) {
3661 box
= fz_empty_rect
;
3664 box
= state
.pagedims
[pagedimno
].mediabox
;
3668 Store_double_field (ret_v
, 0, box
.x0
);
3669 Store_double_field (ret_v
, 1, box
.x1
);
3670 Store_double_field (ret_v
, 2, box
.y0
);
3671 Store_double_field (ret_v
, 3, box
.y1
);
3676 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3677 value dw_v
, value cols_v
)
3679 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3685 double winw
= Int_val (winw_v
);
3686 double winh
= Int_val (winh_v
);
3687 double dw
= Int_val (dw_v
);
3688 double cols
= Int_val (cols_v
);
3689 double pw
= 1.0, ph
= 1.0;
3691 if (trylock (__func__
)) {
3695 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3696 double w
= p
->pagebox
.x1
/ cols
;
3697 double h
= p
->pagebox
.y1
;
3701 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3703 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3706 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3709 ret_v
= caml_copy_double (zoom
);
3713 CAMLprim value
ml_getmaxw (value unit_v
)
3715 CAMLparam1 (unit_v
);
3721 if (trylock (__func__
)) {
3725 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3726 double w
= p
->pagebox
.x1
;
3727 maxw
= fz_max (maxw
, w
);
3732 ret_v
= caml_copy_double (maxw
);
3736 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3738 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3740 int pt
= Int_val(pt_v
);
3741 int x
= Int_val (x_v
);
3742 int y
= Int_val (y_v
);
3745 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3746 ret_v
= caml_copy_double (w
);
3750 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3752 CAMLparam2 (pt_v
, string_v
);
3754 int pt
= Int_val (pt_v
);
3757 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3758 ret_v
= caml_copy_double (w
);
3762 CAMLprim value
ml_getpagebox (value opaque_v
)
3764 CAMLparam1 (opaque_v
);
3770 char *s
= String_val (opaque_v
);
3771 struct page
*page
= parse_pointer (__func__
, s
);
3773 ret_v
= caml_alloc_tuple (4);
3774 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3775 dev
->hints
|= FZ_IGNORE_SHADE
;
3777 ctm
= pagectm (page
);
3778 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3780 fz_close_device (state
.ctx
, dev
);
3781 fz_drop_device (state
.ctx
, dev
);
3782 fz_round_rect (&bbox
, &rect
);
3783 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3784 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3785 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3786 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3791 CAMLprim
void ml_setaalevel (value level_v
)
3793 CAMLparam1 (level_v
);
3795 state
.aalevel
= Int_val (level_v
);
3799 #pragma GCC diagnostic push
3800 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3803 #include <X11/Xlib.h>
3804 #include <X11/cursorfont.h>
3805 #pragma GCC diagnostic pop
3808 #include <EGL/egl.h>
3813 static const int shapes
[] = {
3814 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3817 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3830 XVisualInfo
*visual
;
3831 Cursor curs
[CURS_COUNT
];
3835 static void initcurs (void)
3837 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3838 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3842 CAMLprim
void ml_setbgcol (value color_v
)
3844 CAMLparam1 (color_v
);
3845 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3850 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3852 CAMLparam3 (display_v
, wid_v
, screen_v
);
3856 EGLint attribs
[] = {
3858 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3859 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3864 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3866 caml_failwith ("XOpenDisplay");
3869 eglBindAPI (EGL_OPENGL_API
);
3871 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3872 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3873 caml_failwith ("eglGetDisplay");
3876 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3877 caml_failwith ("eglInitialize");
3880 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3882 caml_failwith ("eglChooseConfig");
3885 if (!eglGetConfigAttrib (glx
.edpy
, conf
, EGL_NATIVE_VISUAL_ID
, &visid
)) {
3886 caml_failwith ("eglGetConfigAttrib");
3892 glx
.wid
= Int_val (wid_v
);
3893 CAMLreturn (Val_int (visid
));
3896 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3898 CAMLparam1 (unit_v
);
3900 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3902 caml_failwith ("eglCreateContext");
3905 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3907 if (glx
.win
== EGL_NO_SURFACE
) {
3908 caml_failwith ("eglCreateWindowSurface");
3912 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3914 caml_failwith ("eglMakeCurrent");
3916 CAMLreturn (unit_v
);
3919 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3921 CAMLparam3 (display_v
, wid_v
, screen_v
);
3923 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3925 caml_failwith ("XOpenDisplay");
3928 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3929 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3931 XCloseDisplay (glx
.dpy
);
3932 caml_failwith ("glXChooseVisual");
3937 glx
.wid
= Int_val (wid_v
);
3938 CAMLreturn (Val_int (glx
.visual
->visualid
));
3941 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3943 CAMLparam1 (unit_v
);
3945 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3947 caml_failwith ("glXCreateContext");
3953 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3954 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3956 caml_failwith ("glXMakeCurrent");
3958 CAMLreturn (unit_v
);
3962 CAMLprim
void ml_setcursor (value cursor_v
)
3964 CAMLparam1 (cursor_v
);
3965 size_t cursn
= Int_val (cursor_v
);
3967 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
3968 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
3973 CAMLprim value
ml_swapb (value unit_v
)
3975 CAMLparam1 (unit_v
);
3977 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
3978 caml_failwith ("eglSwapBuffers");
3981 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3983 CAMLreturn (unit_v
);
3986 #include "keysym2ucs.c"
3988 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3990 CAMLparam1 (keysym_v
);
3992 KeySym keysym
= Int_val (keysym_v
);
3997 rune
= keysym2ucs (keysym
);
3998 len
= fz_runetochar (buf
, rune
);
4000 str_v
= caml_copy_string (buf
);
4004 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4006 CAMLparam1 (keysym_v
);
4008 long ucs_v
= Long_val (keysym_v
);
4012 len
= fz_runetochar (buf
, ucs_v
);
4014 str_v
= caml_copy_string (buf
);
4019 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4021 CAMLprim value
ml_platform (value unit_v
)
4023 CAMLparam1 (unit_v
);
4024 CAMLlocal2 (tup_v
, arr_v
);
4025 int platid
= piunknown
;
4028 #if defined __linux__
4030 #elif defined __CYGWIN__
4032 #elif defined __DragonFly__ || defined __FreeBSD__
4033 || defined __OpenBSD__
|| defined __NetBSD__
4035 #elif defined __sun__
4037 #elif defined __APPLE__
4040 if (uname (&buf
)) err (1, "uname");
4042 tup_v
= caml_alloc_tuple (2);
4044 char const *sar
[] = {
4051 arr_v
= caml_copy_string_array (sar
);
4053 Field (tup_v
, 0) = Val_int (platid
);
4054 Field (tup_v
, 1) = arr_v
;
4058 CAMLprim
void ml_cloexec (value fd_v
)
4061 int fd
= Int_val (fd_v
);
4063 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4064 uerror ("fcntl", Nothing
);
4069 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4071 CAMLparam2 (w_v
, h_v
);
4074 int w
= Int_val (w_v
);
4075 int h
= Int_val (h_v
);
4076 int cs
= Int_val (cs_v
);
4078 if (state
.bo_usable
) {
4079 pbo
= calloc (sizeof (*pbo
), 1);
4081 err (1, "calloc pbo");
4093 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
4096 state
.glGenBuffersARB (1, &pbo
->id
);
4097 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4098 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4099 NULL
, GL_STREAM_DRAW
);
4100 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4102 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4104 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4105 state
.glDeleteBuffersARB (1, &pbo
->id
);
4107 ret_v
= caml_copy_string ("0");
4113 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4115 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4119 err (1, "malloc %d bytes failed", res
+1);
4121 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4123 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4125 ret_v
= caml_copy_string (s
);
4130 ret_v
= caml_copy_string ("0");
4135 CAMLprim
void ml_freepbo (value s_v
)
4138 char *s
= String_val (s_v
);
4139 struct tile
*tile
= parse_pointer (__func__
, s
);
4142 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4144 tile
->pbo
->ptr
= NULL
;
4145 tile
->pbo
->size
= -1;
4150 CAMLprim
void ml_unmappbo (value s_v
)
4153 char *s
= String_val (s_v
);
4154 struct tile
*tile
= parse_pointer (__func__
, s
);
4157 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4158 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4159 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4161 tile
->pbo
->ptr
= NULL
;
4162 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4167 static void setuppbo (void)
4170 static CFBundleRef framework
= NULL
;
4171 if (framework
== NULL
)
4172 framework
= CFBundleGetBundleWithIdentifier (CFSTR ("com.apple.opengl"));
4173 #define GGPA(n) (&state.n = CFBundleGetFunctionPointerForName (framework, CFSTR (#n)))
4176 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
4178 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4180 state
.bo_usable
= GGPA (glBindBufferARB
)
4181 && GGPA (glUnmapBufferARB
)
4182 && GGPA (glMapBufferARB
)
4183 && GGPA (glBufferDataARB
)
4184 && GGPA (glGenBuffersARB
)
4185 && GGPA (glDeleteBuffersARB
);
4190 CAMLprim value
ml_bo_usable (value unit_v
)
4192 CAMLparam1 (unit_v
);
4193 CAMLreturn (Val_bool (state
.bo_usable
));
4196 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4198 CAMLparam3 (ptr_v
, x_v
, y_v
);
4199 CAMLlocal2 (ret_v
, tup_v
);
4201 char *s
= String_val (ptr_v
);
4202 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4203 struct pagedim
*pdim
;
4207 page
= parse_pointer (__func__
, s
);
4208 pdim
= &state
.pagedims
[page
->pdimno
];
4210 ret_v
= Val_int (0);
4211 if (trylock (__func__
)) {
4215 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4216 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4217 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4222 p
.x
= x
+ pdim
->bounds
.x0
;
4223 p
.y
= y
+ pdim
->bounds
.y0
;
4225 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4226 fz_invert_matrix (&ctm
, &ctm
);
4227 fz_transform_point (&p
, &ctm
);
4229 tup_v
= caml_alloc_tuple (2);
4230 ret_v
= caml_alloc_small (1, 1);
4231 Field (tup_v
, 0) = Val_int (p
.x
);
4232 Field (tup_v
, 1) = Val_int (p
.y
);
4233 Field (ret_v
, 0) = tup_v
;
4240 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4241 value x_v
, value y_v
)
4243 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4246 char *s
= String_val (ptr_v
);
4247 int pageno
= Int_val (pageno_v
);
4248 int pdimno
= Int_val (pdimno_v
);
4249 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4250 struct pagedim
*pdim
;
4254 ret_v
= Val_int (0);
4258 page
= loadpage (pageno
, pdimno
);
4261 page
= parse_pointer (__func__
, s
);
4263 pdim
= &state
.pagedims
[pdimno
];
4265 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4266 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4267 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4272 p
.x
= x
+ pdim
->bounds
.x0
;
4273 p
.y
= y
+ pdim
->bounds
.y0
;
4275 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4276 fz_transform_point (&p
, &ctm
);
4278 ret_v
= caml_alloc_tuple (2);
4279 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4280 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4289 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
4292 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4293 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4299 char *s
= String_val (ptr_v
);
4301 page
= parse_pointer (__func__
, s
);
4302 annot
= pdf_create_annot (state
.ctx
,
4303 pdf_page_from_fz_page (state
.ctx
,
4306 p
.x
= Int_val (x_v
);
4307 p
.y
= Int_val (y_v
);
4308 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4309 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4315 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
4317 CAMLparam2 (ptr_v
, n_v
);
4318 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4322 char *s
= String_val (ptr_v
);
4323 struct slink
*slink
;
4325 page
= parse_pointer (__func__
, s
);
4326 slink
= &page
->slinks
[Int_val (n_v
)];
4327 pdf_delete_annot (state
.ctx
,
4328 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4329 (pdf_annot
*) slink
->u
.annot
);
4335 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
4337 CAMLparam3 (ptr_v
, n_v
, str_v
);
4338 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4342 char *s
= String_val (ptr_v
);
4343 struct slink
*slink
;
4345 page
= parse_pointer (__func__
, s
);
4346 slink
= &page
->slinks
[Int_val (n_v
)];
4347 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4348 String_val (str_v
));
4354 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4356 CAMLparam1 (unit_v
);
4357 CAMLreturn (Val_bool (state
.dirty
));
4360 CAMLprim
void ml_savedoc (value path_v
)
4362 CAMLparam1 (path_v
);
4363 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4366 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4371 static void makestippletex (void)
4373 const char pixels
[] = "\xff\xff\0\0";
4374 glGenTextures (1, &state
.stid
);
4375 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4376 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4377 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4390 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4392 return caml_copy_string (FZ_VERSION
);
4395 CAMLprim
void ml_init (value csock_v
, value params_v
)
4397 CAMLparam2 (csock_v
, params_v
);
4398 CAMLlocal2 (trim_v
, fuzz_v
);
4406 state
.csock
= Int_val (csock_v
);
4407 state
.rotate
= Int_val (Field (params_v
, 0));
4408 state
.fitmodel
= Int_val (Field (params_v
, 1));
4409 trim_v
= Field (params_v
, 2);
4410 texcount
= Int_val (Field (params_v
, 3));
4411 state
.sliceheight
= Int_val (Field (params_v
, 4));
4412 mustoresize
= Int_val (Field (params_v
, 5));
4413 colorspace
= Int_val (Field (params_v
, 6));
4414 fontpath
= String_val (Field (params_v
, 7));
4416 if (caml_string_length (Field (params_v
, 8)) > 0) {
4417 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4419 if (!state
.trimcachepath
) {
4420 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4425 haspboext
= Bool_val (Field (params_v
, 9));
4427 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4428 fz_register_document_handlers (state
.ctx
);
4430 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4431 fuzz_v
= Field (trim_v
, 1);
4432 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4433 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4434 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4435 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4437 set_tex_params (colorspace
);
4440 state
.face
= load_font (fontpath
);
4444 const char *data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0,
4446 state
.face
= load_builtin_font (data
, len
);
4448 if (!state
.face
) _exit (1);
4450 realloctexts (texcount
);
4458 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4460 errx (1, "pthread_create: %s", strerror (ret
));