1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/utsname.h>
25 #include <cygwin/socket.h> /* FIONREAD */
36 #include <CoreFoundation/CoreFoundation.h>
40 #include <OpenGL/gl.h>
45 #include <caml/fail.h>
46 #include <caml/alloc.h>
47 #include <caml/memory.h>
48 #include <caml/unixsupport.h>
50 #if __GNUC__ < 5 && !defined __clang__
51 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
52 clobbered diagnostics */
53 #pragma GCC diagnostic ignored "-Wclobbered"
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wunused-parameter"
58 #include <mupdf/fitz.h>
59 #include <mupdf/pdf.h>
60 #pragma GCC diagnostic pop
63 #include FT_FREETYPE_H
66 #define CACHE_PAGEREFS
69 extern char **environ
;
73 #define NORETURN_ATTR __attribute__ ((noreturn))
74 #define UNUSED_ATTR __attribute__ ((unused))
75 #if !defined __clang__
76 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
78 #define OPTIMIZE_ATTR(n)
80 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
84 #define OPTIMIZE_ATTR(n)
85 #define GCC_FMT_ATTR(a, b)
90 #define FMT_ptr PRIxPTR
91 #define SCN_ptr SCNxPTR
92 #define FMT_ptr_cast(p) ((uintptr_t) (p))
93 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
95 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
96 err (int exitcode
, const char *fmt
, ...)
103 vfprintf (stderr
, fmt
, ap
);
105 fprintf (stderr
, ": %s\n", strerror (savederrno
));
110 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
111 errx (int exitcode
, const char *fmt
, ...)
116 vfprintf (stderr
, fmt
, ap
);
118 fputc ('\n', stderr
);
123 #ifndef GL_TEXTURE_RECTANGLE_ARB
124 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
128 #define TEXT_TYPE GL_TEXTURE_2D
130 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
134 #define GL_BGRA 0x80E1
137 #ifndef GL_UNSIGNED_INT_8_8_8_8
138 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
141 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
142 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
146 #define lprintf printf
151 #define ARSERT(cond) for (;;) { \
153 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
169 struct slice slices
[1];
180 fz_matrix ctm
, zoomctm
, tctm
;
184 enum { SLINK
, SANNOT
} tag
;
205 fz_display_list
*dlist
;
207 struct slink
*slinks
;
209 struct annot
*annots
;
217 struct pagedim
*pagedims
;
232 fz_colorspace
*colorspace
;
240 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
263 void (*glBindBufferARB
) (GLenum
, GLuint
);
264 GLboolean (*glUnmapBufferARB
) (GLenum
);
265 void *(*glMapBufferARB
) (GLenum
, GLenum
);
266 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
267 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
268 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
270 GLfloat texcoords
[8];
271 GLfloat vertices
[16];
273 #ifdef CACHE_PAGEREFS
290 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
292 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
295 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
297 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
300 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
302 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
303 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
306 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
308 static void lock (const char *cap
)
310 int ret
= pthread_mutex_lock (&mutex
);
312 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
316 static void unlock (const char *cap
)
318 int ret
= pthread_mutex_unlock (&mutex
);
320 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
324 static int trylock (const char *cap
)
326 int ret
= pthread_mutex_trylock (&mutex
);
327 if (ret
&& ret
!= EBUSY
) {
328 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
333 static void *parse_pointer (const char *cap
, const char *s
)
338 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
340 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
345 static double now (void)
349 if (gettimeofday (&tv
, NULL
)) {
350 err (1, "gettimeofday");
352 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
355 static int hasdata (void)
358 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
359 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
363 CAMLprim value
ml_hasdata (value fd_v
)
368 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
369 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
370 CAMLreturn (Val_bool (avail
> 0));
373 static void readdata (int fd
, void *p
, int size
)
378 n
= read (fd
, p
, size
);
380 if (n
< 0 && errno
== EINTR
) goto again
;
381 if (!n
) errx (1, "EOF while reading");
382 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
386 static void writedata (int fd
, char *p
, int size
)
389 uint32_t size4
= size
;
390 struct iovec iov
[2] = {
391 { .iov_base
= &size4
, .iov_len
= 4 },
392 { .iov_base
= p
, .iov_len
= size
}
396 n
= writev (fd
, iov
, 2);
397 if (n
< 0 && errno
== EINTR
) goto again
;
399 if (!n
) errx (1, "EOF while writing data");
400 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
404 static int readlen (int fd
)
406 /* Type punned unions here. Why? Less code (Adjusted by more comments).
407 https://en.wikipedia.org/wiki/Type_punning */
408 /* Then again https://bugs.llvm.org/show_bug.cgi?id=31928 - hmm */
409 union { uint32_t len
; char raw
[4]; } buf
;
410 readdata (fd
, buf
.raw
, 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
, ...)
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 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
513 static void pdfinfo (void)
515 struct { char *tag
; char *name
; } metatbl
[] = {
516 { FZ_META_INFO_TITLE
, "Title" },
517 { FZ_META_INFO_AUTHOR
, "Author" },
518 { FZ_META_FORMAT
, "Format" },
519 { FZ_META_ENCRYPTION
, "Encryption" },
520 { "info:Creator", "Creator" },
521 { "info:Producer", "Producer" },
522 { "info:CreationDate", "Creation date" },
527 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
530 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
531 metatbl
[i
].tag
, buf
, len
);
534 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
537 buf
= realloc (buf
, need
+ 1);
538 if (!buf
) err (1, "pdfinfo realloc %d", need
+ 1);
549 static void unlinktile (struct tile
*tile
)
551 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
552 struct slice
*s
= &tile
->slices
[i
];
554 if (s
->texindex
!= -1) {
555 if (state
.texowners
[s
->texindex
].slice
== s
) {
556 state
.texowners
[s
->texindex
].slice
= NULL
;
562 static void freepage (struct page
*page
)
566 fz_drop_stext_page (state
.ctx
, page
->text
);
571 fz_drop_display_list (state
.ctx
, page
->dlist
);
572 fz_drop_page (state
.ctx
, page
->fzpage
);
576 static void freetile (struct tile
*tile
)
581 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
584 fz_drop_pixmap (state
.ctx
, state
.pig
);
586 state
.pig
= tile
->pixmap
;
591 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
600 static int cacheline32bytes
;
602 static void __attribute__ ((constructor
)) clcheck (void)
604 char **envp
= environ
;
609 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
611 cacheline32bytes
= auxv
[1] == 32;
617 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
619 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
620 if (cacheline32bytes
&& size
> 32) {
621 intptr_t a1
, a2
, diff
;
623 vector
unsigned char v
= vec_splat_u8 (-1);
624 vector
unsigned char *p
;
626 a1
= a2
= (intptr_t) pixmap
->samples
;
627 a2
= (a1
+ 31) & ~31;
632 while (a1
!= a2
) *(char *) a1
++ = 0xff;
633 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
634 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
636 vec_st (v
, i
+ 16, p
);
638 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
640 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
643 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
646 static void trimctm (pdf_page
*page
, int pindex
)
649 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
652 if (!pdim
->tctmready
) {
653 fz_rect realbox
, mediabox
;
654 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
656 fz_rotate (&rm
, -pdim
->rotate
);
657 fz_scale (&sm
, 1, -1);
658 fz_concat (&ctm
, &rm
, &sm
);
659 realbox
= pdim
->mediabox
;
660 fz_transform_rect (&realbox
, &ctm
);
661 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
662 fz_concat (&ctm1
, &ctm
, &tm
);
663 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
664 fz_invert_matrix (&im
, &page_ctm
);
665 fz_concat (&ctm
, &im
, &ctm1
);
671 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
674 int pdimno
= pdim
- state
.pagedims
;
676 if (pdf_specifics (state
.ctx
, state
.doc
)) {
677 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), pdimno
);
678 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
681 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
682 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
687 static fz_matrix
pagectm (struct page
*page
)
689 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
692 static void *loadpage (int pageno
, int pindex
)
697 page
= calloc (sizeof (struct page
), 1);
699 err (1, "calloc page %d", pageno
);
702 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
703 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
705 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
706 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
709 fz_catch (state
.ctx
) {
712 fz_close_device (state
.ctx
, dev
);
713 fz_drop_device (state
.ctx
, dev
);
715 page
->pdimno
= pindex
;
716 page
->pageno
= pageno
;
717 page
->sgen
= state
.gen
;
718 page
->agen
= state
.gen
;
719 page
->tgen
= state
.gen
;
723 static struct tile
*alloctile (int h
)
729 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
730 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
731 tile
= calloc (tilesize
, 1);
733 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
735 for (int i
= 0; i
< slicecount
; ++i
) {
736 int sh
= fz_mini (h
, state
.sliceheight
);
737 tile
->slices
[i
].h
= sh
;
738 tile
->slices
[i
].texindex
= -1;
741 tile
->slicecount
= slicecount
;
742 tile
->sliceheight
= state
.sliceheight
;
746 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
754 struct pagedim
*pdim
;
756 tile
= alloctile (h
);
757 pdim
= &state
.pagedims
[page
->pdimno
];
762 bbox
.x1
= bbox
.x0
+ w
;
763 bbox
.y1
= bbox
.y0
+ h
;
766 if (state
.pig
->w
== w
768 && state
.pig
->colorspace
== state
.colorspace
) {
769 tile
->pixmap
= state
.pig
;
770 tile
->pixmap
->x
= bbox
.x0
;
771 tile
->pixmap
->y
= bbox
.y0
;
774 fz_drop_pixmap (state
.ctx
, state
.pig
);
781 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
782 &bbox
, NULL
, 1, pbo
->ptr
);
787 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
,
794 clearpixmap (tile
->pixmap
);
796 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
797 ctm
= pagectm (page
);
798 fz_rect_from_irect (&rect
, &bbox
);
799 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
800 fz_close_device (state
.ctx
, dev
);
801 fz_drop_device (state
.ctx
, dev
);
806 #ifdef CACHE_PAGEREFS
807 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
808 thanks to Robin Watts */
810 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
812 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
816 if (state
.pdflut
.idx
== state
.pagecount
) return;
818 kids
= pdf_dict_gets (ctx
, node
, "Kids");
819 len
= pdf_array_len (ctx
, kids
);
822 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
824 if (pdf_mark_obj (ctx
, node
))
825 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
826 for (int i
= 0; i
< len
; i
++) {
827 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
828 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
830 ? !strcmp (type
, "Pages")
831 : pdf_dict_gets (ctx
, kid
, "Kids")
832 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
833 pdf_collect_pages (doc
, kid
);
837 ? strcmp (type
, "Page") != 0
838 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
839 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
840 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
843 pdf_unmark_obj (ctx
, node
);
847 pdf_load_page_objs (pdf_document
*doc
)
849 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
850 pdf_trailer (state
.ctx
, doc
), "Root");
851 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
854 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
856 state
.pdflut
.idx
= 0;
857 pdf_collect_pages (doc
, node
);
861 static void initpdims (int wthack
)
865 fz_rect rootmediabox
;
866 int pageno
, trim
, show
;
867 int trimw
= 0, cxcount
;
868 fz_context
*ctx
= state
.ctx
;
869 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
876 if (state
.trimmargins
&& state
.trimcachepath
) {
877 trimf
= fopen (state
.trimcachepath
, "rb");
879 trimf
= fopen (state
.trimcachepath
, "wb");
884 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
885 cxcount
= state
.pagecount
;
887 cxcount
= fz_mini (state
.pagecount
, 1);
891 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
892 "Root/Pages/MediaBox");
893 pdf_to_rect (ctx
, obj
, &rootmediabox
);
896 #ifdef CACHE_PAGEREFS
897 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
898 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
899 if (!state
.pdflut
.objs
) {
900 err (1, "malloc pageobjs %zu %d %zu failed",
901 sizeof (*state
.pdflut
.objs
), cxcount
,
902 sizeof (*state
.pdflut
.objs
) * cxcount
);
904 state
.pdflut
.count
= cxcount
;
905 pdf_load_page_objs (pdf
);
906 state
.pdflut
.pdf
= pdf
;
910 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
917 pdf_obj
*pageref
, *pageobj
;
919 #ifdef CACHE_PAGEREFS
920 pageref
= state
.pdflut
.objs
[pageno
];
922 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
924 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
925 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
927 if (state
.trimmargins
) {
932 page
= pdf_load_page (ctx
, pdf
, pageno
);
933 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
934 trim
= state
.trimanew
|| !obj
;
938 fz_matrix ctm
, page_ctm
;
940 dev
= fz_new_bbox_device (ctx
, &rect
);
941 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
942 fz_invert_matrix (&ctm
, &page_ctm
);
943 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
944 fz_close_device (ctx
, dev
);
945 fz_drop_device (ctx
, dev
);
947 rect
.x0
+= state
.trimfuzz
.x0
;
948 rect
.x1
+= state
.trimfuzz
.x1
;
949 rect
.y0
+= state
.trimfuzz
.y0
;
950 rect
.y1
+= state
.trimfuzz
.y1
;
951 fz_transform_rect (&rect
, &ctm
);
952 fz_intersect_rect (&rect
, &mediabox
);
954 if (!fz_is_empty_rect (&rect
)) {
958 obj
= pdf_new_array (ctx
, pdf
, 4);
959 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
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_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
970 mediabox
.x0
= pdf_to_real (ctx
,
971 pdf_array_get (ctx
, obj
, 0));
972 mediabox
.y0
= pdf_to_real (ctx
,
973 pdf_array_get (ctx
, obj
, 1));
974 mediabox
.x1
= pdf_to_real (ctx
,
975 pdf_array_get (ctx
, obj
, 2));
976 mediabox
.y1
= pdf_to_real (ctx
,
977 pdf_array_get (ctx
, obj
, 3));
980 fz_drop_page (ctx
, &page
->super
);
981 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
983 printd ("progress %f Trimming %d",
984 (double) (pageno
+ 1) / state
.pagecount
,
989 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
997 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
999 if (fz_is_empty_rect (&mediabox
)) {
1008 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1010 if (!fz_is_empty_rect (&cropbox
)) {
1015 fz_intersect_rect (&mediabox
, &cropbox
);
1020 if (fz_is_empty_rect (&rootmediabox
)) {
1022 "cannot find page size for page %d\n",
1026 mediabox
= rootmediabox
;
1033 if (state
.trimmargins
&& trimw
) {
1037 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1038 fz_bound_page (ctx
, page
, &mediabox
);
1039 if (state
.trimmargins
) {
1043 dev
= fz_new_bbox_device (ctx
, &rect
);
1044 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1045 fz_close_device (ctx
, dev
);
1046 fz_drop_device (ctx
, dev
);
1048 rect
.x0
+= state
.trimfuzz
.x0
;
1049 rect
.x1
+= state
.trimfuzz
.x1
;
1050 rect
.y0
+= state
.trimfuzz
.y0
;
1051 rect
.y1
+= state
.trimfuzz
.y1
;
1052 fz_intersect_rect (&rect
, &mediabox
);
1054 if (!fz_is_empty_rect (&rect
)) {
1058 fz_drop_page (ctx
, page
);
1060 printd ("progress %f loading %d",
1061 (double) (pageno
+ 1) / state
.pagecount
,
1068 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1070 err (1, "fwrite trim mediabox");
1076 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1078 err (1, "fread trim mediabox %d", pageno
);
1084 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1085 fz_bound_page (ctx
, page
, &mediabox
);
1086 fz_drop_page (ctx
, page
);
1088 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1090 printd ("progress %f Gathering dimensions %d",
1091 (double) (pageno
) / state
.pagecount
,
1096 fprintf (stderr
, "failed to load page %d\n", pageno
);
1102 if (state
.pagedimcount
== 0
1103 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1104 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1107 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1108 state
.pagedims
= realloc (state
.pagedims
, size
);
1109 if (!state
.pagedims
) {
1110 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1111 size
, state
.pagedimcount
+ 1);
1114 p
= &state
.pagedims
[state
.pagedimcount
++];
1116 p
->mediabox
= mediabox
;
1122 printd ("progress 1 %s %d pages in %f seconds",
1123 state
.trimmargins
? "Trimmed" : "Processed",
1124 state
.pagecount
, end
- start
);
1128 if (fclose (trimf
)) {
1134 static void layout (void)
1139 struct pagedim
*p
= p
;
1140 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1142 if (state
.pagedimcount
== 0) return;
1144 switch (state
.fitmodel
) {
1145 case FitProportional
:
1146 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1149 p
= &state
.pagedims
[pindex
];
1150 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1152 fz_transform_rect (&box
, &rm
);
1154 x0
= fz_min (box
.x0
, box
.x1
);
1155 x1
= fz_max (box
.x0
, box
.x1
);
1158 maxw
= fz_max (w
, maxw
);
1159 zoom
= state
.w
/ maxw
;
1171 ARSERT (0 && state
.fitmodel
);
1174 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1178 p
= &state
.pagedims
[pindex
];
1179 fz_rotate (&ctm
, state
.rotate
);
1180 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1182 fz_transform_rect (&box
, &rm
);
1183 w
= box
.x1
- box
.x0
;
1184 switch (state
.fitmodel
) {
1185 case FitProportional
:
1186 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1192 h
= box
.y1
- box
.y0
;
1194 zoom
= fz_min (zw
, zh
);
1195 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1204 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1205 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1207 fz_rotate (&rm
, p
->rotate
);
1208 p
->pagebox
= p
->mediabox
;
1209 fz_transform_rect (&p
->pagebox
, &rm
);
1210 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1211 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1215 fz_transform_rect (&rect
, &ctm
);
1216 fz_round_rect (&p
->bounds
, &rect
);
1219 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1220 fz_scale (&sm
, zoom
, -zoom
);
1221 fz_concat (&ctm
, &tm
, &sm
);
1227 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1228 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1229 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1230 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1231 int boundw
= x1
- x0
;
1232 int boundh
= y1
- y0
;
1234 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1235 } while (p
-- != state
.pagedims
);
1238 struct pagedim
*pdimofpageno (int pageno
)
1240 struct pagedim
*pdim
= state
.pagedims
;
1242 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1243 if (state
.pagedims
[i
].pageno
> pageno
)
1245 pdim
= &state
.pagedims
[i
];
1251 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1252 uritoanchor (const char *uri
)
1258 a
.n
= fz_resolve_link (state
.ctx
, state
.doc
, uri
, &p
.x
, &p
.y
);
1260 struct pagedim
*pdim
= pdimofpageno (a
.n
);
1261 fz_transform_point (&p
, &pdim
->ctm
);
1264 a
.h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1269 static void recurse_outline (fz_outline
*outline
, int level
)
1272 struct anchor a
= uritoanchor (outline
->uri
);
1274 printd ("o %d %d %d %d %s", level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1277 printd ("on %d %s", level
, outline
->title
);
1279 if (outline
->down
) {
1280 recurse_outline (outline
->down
, level
+ 1);
1282 outline
= outline
->next
;
1286 static void process_outline (void)
1288 fz_outline
*outline
;
1290 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1292 state
.needoutline
= 0;
1293 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1295 recurse_outline (outline
, 0);
1296 fz_drop_outline (state
.ctx
, outline
);
1300 static char *strofline (fz_stext_line
*line
)
1305 size_t size
= 0, cap
= 80;
1307 p
= malloc (cap
+ 1);
1308 if (!p
) return NULL
;
1310 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1311 int n
= fz_runetochar (utf8
, ch
->c
);
1312 if (size
+ n
> cap
) {
1314 p
= realloc (p
, cap
+ 1);
1315 if (!p
) return NULL
;
1318 memcpy (p
+ size
, utf8
, n
);
1325 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1326 int stop
, int pageno
, double start
)
1332 p
= strofline (line
);
1335 ret
= regexec (re
, p
, 1, &rm
, 0);
1338 if (ret
!= REG_NOMATCH
) {
1341 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1342 printd ("msg regexec error `%.*s'",
1343 (int) size
, errbuf
);
1349 fz_point p1
, p2
, p3
, p4
;
1350 fz_rect s
= {0,0,0,0}, e
;
1354 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1355 o
+= fz_runelen (ch
->c
);
1361 for (;ch
; ch
= ch
->next
) {
1362 o
+= fz_runelen (ch
->c
);
1363 if (o
> rm
.rm_eo
) break;
1377 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1384 printd ("progress 1 found at %d `%.*s' in %f sec",
1385 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1389 printd ("match %d %d %f %f %f %f %f %f %f %f",
1401 /* wishful thinking function */
1402 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1405 fz_stext_page
*text
;
1406 struct pagedim
*pdim
;
1407 int stop
= 0, niters
= 0;
1410 fz_stext_block
*block
;
1413 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1414 if (niters
++ == 5) {
1417 printd ("progress 1 attention requested aborting search at %d",
1422 printd ("progress %f searching in page %d",
1423 (double) (pageno
+ 1) / state
.pagecount
,
1427 pdim
= pdimofpageno (pageno
);
1428 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1429 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1431 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1433 fz_matrix ctm
= pagectm1 (page
, pdim
);
1434 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1437 fz_close_device (state
.ctx
, tdev
);
1438 fz_drop_device (state
.ctx
, tdev
);
1441 for (block
= text
->first_block
; block
; block
= block
->next
) {
1442 fz_stext_line
*line
;
1444 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1445 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1446 if (line
->bbox
.y0
< y
+ 1) continue;
1448 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1450 case 1: stop
= 1; break;
1451 case -1: stop
= 1; goto endloop
;
1457 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1458 fz_stext_line
*line
;
1460 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1461 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1462 if (line
->bbox
.y0
< y
+ 1) continue;
1464 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1466 case 1: stop
= 1; break;
1467 case -1: stop
= 1; goto endloop
;
1482 fz_drop_stext_page (state
.ctx
, text
);
1483 fz_drop_page (state
.ctx
, page
);
1487 printd ("progress 1 no matches %f sec", end
- start
);
1489 printd ("clearrects");
1492 static void set_tex_params (int colorspace
)
1499 switch (colorspace
) {
1501 state
.texiform
= GL_RGBA8
;
1502 state
.texform
= GL_RGBA
;
1503 state
.texty
= GL_UNSIGNED_BYTE
;
1504 state
.colorspace
= fz_device_rgb (state
.ctx
);
1507 state
.texiform
= GL_RGBA8
;
1508 state
.texform
= GL_BGRA
;
1509 state
.texty
= endianness
.s
> 1
1510 ? GL_UNSIGNED_INT_8_8_8_8
1511 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1512 state
.colorspace
= fz_device_bgr (state
.ctx
);
1515 state
.texiform
= GL_LUMINANCE_ALPHA
;
1516 state
.texform
= GL_LUMINANCE_ALPHA
;
1517 state
.texty
= GL_UNSIGNED_BYTE
;
1518 state
.colorspace
= fz_device_gray (state
.ctx
);
1521 errx (1, "invalid colorspce %d", colorspace
);
1525 static void realloctexts (int texcount
)
1529 if (texcount
== state
.texcount
) return;
1531 if (texcount
< state
.texcount
) {
1532 glDeleteTextures (state
.texcount
- texcount
,
1533 state
.texids
+ texcount
);
1536 size
= texcount
* sizeof (*state
.texids
);
1537 state
.texids
= realloc (state
.texids
, size
);
1538 if (!state
.texids
) {
1539 err (1, "realloc texids %" FMT_s
, size
);
1542 size
= texcount
* sizeof (*state
.texowners
);
1543 state
.texowners
= realloc (state
.texowners
, size
);
1544 if (!state
.texowners
) {
1545 err (1, "realloc texowners %" FMT_s
, size
);
1547 if (texcount
> state
.texcount
) {
1548 glGenTextures (texcount
- state
.texcount
,
1549 state
.texids
+ state
.texcount
);
1550 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1551 state
.texowners
[i
].w
= -1;
1552 state
.texowners
[i
].slice
= NULL
;
1555 state
.texcount
= texcount
;
1559 static char *mbtoutf8 (char *s
)
1569 len
= mbstowcs (NULL
, s
, strlen (s
));
1574 if (len
== (size_t) -1) {
1575 printd ("emsg mbtoutf8: mbstowcs %d:%s\n", errno
, strerror (errno
));
1580 tmp
= calloc (len
, sizeof (wchar_t));
1582 printd ("emsg mbtoutf8: calloc(%zu, %zu) %d:%s",
1583 len
, sizeof (wchar_t), errno
, strerror (errno
));
1587 ret
= mbstowcs (tmp
, s
, len
);
1588 if (ret
== (size_t) -1) {
1589 printd ("emsg mbtoutf8: mbswcs %zu characters failed %d:%s\n",
1590 len
, errno
, strerror (errno
));
1596 for (i
= 0; i
< ret
; ++i
) {
1597 len
+= fz_runelen (tmp
[i
]);
1600 p
= r
= malloc (len
+ 1);
1602 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1607 for (i
= 0; i
< ret
; ++i
) {
1608 p
+= fz_runetochar (p
, tmp
[i
]);
1615 CAMLprim value
ml_mbtoutf8 (value s_v
)
1621 s
= String_val (s_v
);
1627 ret_v
= caml_copy_string (r
);
1633 static void * mainloop (void UNUSED_ATTR
*unused
)
1636 int len
, ret
, oldlen
= 0;
1641 len
= readlen (state
.csock
);
1643 errx (1, "readlen returned 0");
1646 if (oldlen
< len
+ 1) {
1647 p
= realloc (p
, len
+ 1);
1649 err (1, "realloc %d failed", len
+ 1);
1653 readdata (state
.csock
, p
, len
);
1656 if (!strncmp ("open", p
, 4)) {
1657 int wthack
, off
, usedoccss
, ok
= 0;
1664 ret
= sscanf (p
+ 5, " %d %d %d %n",
1665 &wthack
, &state
.cxack
, &usedoccss
, &off
);
1667 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1670 filename
= p
+ 5 + off
;
1671 filenamelen
= strlen (filename
);
1672 password
= filename
+ filenamelen
+ 1;
1674 if (password
[strlen (password
) + 1]) {
1675 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1679 fz_set_use_document_css (state
.ctx
, usedoccss
);
1680 fz_try (state
.ctx
) {
1681 ok
= openxref (filename
, password
);
1683 fz_catch (state
.ctx
) {
1684 utf8filename
= mbtoutf8 (filename
);
1685 printd ("msg Could not open %s", utf8filename
);
1695 utf8filename
= mbtoutf8 (filename
);
1696 printd ("msg Opened %s (press h/F1 to get help)",
1698 if (utf8filename
!= filename
) {
1699 free (utf8filename
);
1702 state
.needoutline
= 1;
1705 else if (!strncmp ("cs", p
, 2)) {
1708 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1710 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1713 set_tex_params (colorspace
);
1714 for (i
= 0; i
< state
.texcount
; ++i
) {
1715 state
.texowners
[i
].w
= -1;
1716 state
.texowners
[i
].slice
= NULL
;
1720 else if (!strncmp ("freepage", p
, 8)) {
1723 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1725 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1729 else if (!strncmp ("freetile", p
, 8)) {
1732 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1734 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1738 else if (!strncmp ("search", p
, 6)) {
1739 int icase
, pageno
, y
, len2
, forward
;
1743 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1744 &icase
, &pageno
, &y
, &forward
, &len2
);
1746 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1749 pattern
= p
+ 6 + len2
;
1750 ret
= regcomp (&re
, pattern
,
1751 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1756 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1757 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1760 search (&re
, pageno
, y
, forward
);
1764 else if (!strncmp ("geometry", p
, 8)) {
1768 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1770 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1777 for (int i
= 0; i
< state
.texcount
; ++i
) {
1778 state
.texowners
[i
].slice
= NULL
;
1781 state
.fitmodel
= fitmodel
;
1786 unlock ("geometry");
1787 printd ("continue %d", state
.pagecount
);
1789 else if (!strncmp ("reqlayout", p
, 9)) {
1792 unsigned int fitmodel
;
1796 ret
= sscanf (p
+ 9, " %d %u %d %n",
1797 &rotate
, &fitmodel
, &h
, &off
);
1799 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1802 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1803 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1806 state
.rotate
= rotate
;
1807 state
.fitmodel
= fitmodel
;
1812 nameddest
= p
+ 9 + off
;
1813 if (pdf
&& nameddest
&& *nameddest
) {
1815 struct pagedim
*pdim
;
1816 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1818 pdim
= pdimofpageno (pageno
);
1819 fz_transform_point (&xy
, &pdim
->ctm
);
1820 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1824 unlock ("reqlayout");
1825 printd ("continue %d", state
.pagecount
);
1827 else if (!strncmp ("page", p
, 4)) {
1832 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1834 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1839 page
= loadpage (pageno
, pindex
);
1843 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1845 else if (!strncmp ("tile", p
, 4)) {
1852 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1853 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1854 SCN_ptr_cast (&data
));
1856 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1861 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1865 printd ("tile %d %d %" FMT_ptr
" %u %f",
1867 FMT_ptr_cast (tile
),
1868 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1871 else if (!strncmp ("trimset", p
, 7)) {
1875 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1876 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1878 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1881 state
.trimmargins
= trimmargins
;
1882 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1884 state
.trimfuzz
= fuzz
;
1888 else if (!strncmp ("settrim", p
, 7)) {
1892 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1893 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1895 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1899 state
.trimmargins
= trimmargins
;
1900 state
.needoutline
= 1;
1901 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1903 state
.trimfuzz
= fuzz
;
1905 state
.pagedimcount
= 0;
1906 free (state
.pagedims
);
1907 state
.pagedims
= NULL
;
1912 printd ("continue %d", state
.pagecount
);
1914 else if (!strncmp ("sliceh", p
, 6)) {
1917 ret
= sscanf (p
+ 6, " %d", &h
);
1919 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1921 if (h
!= state
.sliceheight
) {
1922 state
.sliceheight
= h
;
1923 for (int i
= 0; i
< state
.texcount
; ++i
) {
1924 state
.texowners
[i
].w
= -1;
1925 state
.texowners
[i
].h
= -1;
1926 state
.texowners
[i
].slice
= NULL
;
1930 else if (!strncmp ("interrupt", p
, 9)) {
1931 printd ("vmsg interrupted");
1934 errx (1, "unknown command %.*s", len
, p
);
1940 CAMLprim value
ml_isexternallink (value uri_v
)
1943 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1944 CAMLreturn (Val_bool (ext
));
1947 CAMLprim value
ml_uritolocation (value uri_v
)
1953 struct pagedim
*pdim
;
1955 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1957 pdim
= pdimofpageno (pageno
);
1958 fz_transform_point (&xy
, &pdim
->ctm
);
1959 ret_v
= caml_alloc_tuple (3);
1960 Field (ret_v
, 0) = Val_int (pageno
);
1961 Field (ret_v
, 1) = caml_copy_double (xy
.x
);
1962 Field (ret_v
, 2) = caml_copy_double (xy
.y
);
1966 CAMLprim value
ml_realloctexts (value texcount_v
)
1968 CAMLparam1 (texcount_v
);
1971 if (trylock (__func__
)) {
1975 realloctexts (Int_val (texcount_v
));
1980 CAMLreturn (Val_bool (ok
));
1983 static void recti (int x0
, int y0
, int x1
, int y1
)
1985 GLfloat
*v
= state
.vertices
;
1987 glVertexPointer (2, GL_FLOAT
, 0, v
);
1988 v
[0] = x0
; v
[1] = y0
;
1989 v
[2] = x1
; v
[3] = y0
;
1990 v
[4] = x0
; v
[5] = y1
;
1991 v
[6] = x1
; v
[7] = y1
;
1992 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1995 static void showsel (struct page
*page
, int ox
, int oy
)
1999 fz_stext_block
*block
;
2001 unsigned char selcolor
[] = {15,15,15,140};
2003 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) return;
2005 glEnable (GL_BLEND
);
2006 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2007 glColor4ubv (selcolor
);
2009 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2010 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2012 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2013 fz_stext_line
*line
;
2015 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2016 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2019 rect
= fz_empty_rect
;
2020 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2021 if (ch
== page
->fmark
.ch
) seen
= 1;
2022 if (seen
) fz_union_rect (&rect
, &ch
->bbox
);
2023 if (ch
== page
->lmark
.ch
) {
2024 fz_round_rect (&bbox
, &rect
);
2025 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2026 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2030 fz_round_rect (&bbox
, &rect
);
2031 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2032 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2036 glDisable (GL_BLEND
);
2041 static void stipplerect (fz_matrix
*m
,
2049 fz_transform_point (p1
, m
);
2050 fz_transform_point (p2
, m
);
2051 fz_transform_point (p3
, m
);
2052 fz_transform_point (p4
, m
);
2058 t
= hypotf (w
, h
) * .25f
;
2062 s
= hypotf (w
, h
) * .25f
;
2064 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2065 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2067 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2068 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2070 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2071 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2073 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2074 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2076 glDrawArrays (GL_LINES
, 0, 8);
2079 static void solidrect (fz_matrix
*m
,
2086 fz_transform_point (p1
, m
);
2087 fz_transform_point (p2
, m
);
2088 fz_transform_point (p3
, m
);
2089 fz_transform_point (p4
, m
);
2090 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2091 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2093 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2094 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2095 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2098 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2100 fz_matrix ctm
, tm
, pm
;
2101 fz_link
*link
, *links
;
2102 GLfloat
*texcoords
= state
.texcoords
;
2103 GLfloat
*vertices
= state
.vertices
;
2105 links
= fz_load_links (state
.ctx
, page
->fzpage
);
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
= 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;
2242 static void ensureslinks (struct page
*page
)
2246 size_t slinksize
= sizeof (*page
->slinks
);
2247 fz_link
*link
, *links
;
2249 ensureannots (page
);
2250 if (state
.gen
!= page
->sgen
) {
2252 page
->sgen
= state
.gen
;
2254 if (page
->slinks
) return;
2256 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2257 ctm
= pagectm (page
);
2259 count
= page
->annotcount
;
2260 for (link
= links
; link
; link
= link
->next
) {
2266 page
->slinkcount
= count
;
2267 page
->slinks
= calloc (count
, slinksize
);
2268 if (!page
->slinks
) {
2269 err (1, "calloc slinks %d", count
);
2272 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2276 fz_transform_rect (&rect
, &ctm
);
2277 page
->slinks
[i
].tag
= SLINK
;
2278 page
->slinks
[i
].u
.link
= link
;
2279 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2281 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2283 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2284 fz_transform_rect (&rect
, &ctm
);
2285 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2287 page
->slinks
[i
].tag
= SANNOT
;
2288 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2290 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2294 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2295 static void fmt_linkn (char *s
, unsigned int u
)
2297 unsigned int len
; unsigned int q
;
2298 unsigned int zma
= 'z' - 'a' + 1;
2300 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2303 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2304 /* handles u == 0 */
2309 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2310 int noff
, char *targ
, int tlen
, int hfsize
)
2313 struct slink
*slink
;
2314 double x0
, y0
, x1
, y1
, w
;
2316 ensureslinks (page
);
2317 glColor3ub (0xc3, 0xb0, 0x91);
2318 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2319 fmt_linkn (buf
, i
+ noff
);
2320 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2321 slink
= &page
->slinks
[i
];
2323 x0
= slink
->bbox
.x0
+ xoff
- 5;
2324 y1
= slink
->bbox
.y0
+ yoff
- 5;
2325 y0
= y1
+ 10 + hfsize
;
2326 w
= measure_string (state
.face
, hfsize
, buf
);
2328 recti (x0
, y0
, x1
, y1
);
2332 glEnable (GL_BLEND
);
2333 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2334 glEnable (GL_TEXTURE_2D
);
2335 glColor3ub (0, 0, 0);
2336 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2337 fmt_linkn (buf
, i
+ noff
);
2338 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2339 slink
= &page
->slinks
[i
];
2341 x0
= slink
->bbox
.x0
+ xoff
;
2342 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2343 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2346 glDisable (GL_TEXTURE_2D
);
2347 glDisable (GL_BLEND
);
2350 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2353 struct slice
*slice1
;
2354 unsigned char *texdata
;
2357 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2358 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2360 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2361 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2362 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2366 int texindex
= state
.texindex
++ % state
.texcount
;
2368 if (state
.texowners
[texindex
].w
== tile
->w
) {
2369 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2373 state
.texowners
[texindex
].h
= slice
->h
;
2377 state
.texowners
[texindex
].h
= slice
->h
;
2380 state
.texowners
[texindex
].w
= tile
->w
;
2381 state
.texowners
[texindex
].slice
= slice
;
2382 slice
->texindex
= texindex
;
2384 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2385 #if TEXT_TYPE == GL_TEXTURE_2D
2386 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2387 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2388 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2389 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2392 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2396 texdata
= tile
->pixmap
->samples
;
2399 glTexSubImage2D (TEXT_TYPE
,
2411 glTexImage2D (TEXT_TYPE
,
2423 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2428 CAMLprim
void ml_begintiles (value unit_v
)
2430 CAMLparam1 (unit_v
);
2431 glEnable (TEXT_TYPE
);
2432 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2433 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2437 CAMLprim
void ml_endtiles (value unit_v
)
2439 CAMLparam1 (unit_v
);
2440 glDisable (TEXT_TYPE
);
2444 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2446 CAMLparam2 (args_v
, ptr_v
);
2447 int dispx
= Int_val (Field (args_v
, 0));
2448 int dispy
= Int_val (Field (args_v
, 1));
2449 int dispw
= Int_val (Field (args_v
, 2));
2450 int disph
= Int_val (Field (args_v
, 3));
2451 int tilex
= Int_val (Field (args_v
, 4));
2452 int tiley
= Int_val (Field (args_v
, 5));
2453 char *s
= String_val (ptr_v
);
2454 struct tile
*tile
= parse_pointer (__func__
, s
);
2455 int slicey
, firstslice
;
2456 struct slice
*slice
;
2457 GLfloat
*texcoords
= state
.texcoords
;
2458 GLfloat
*vertices
= state
.vertices
;
2460 firstslice
= tiley
/ tile
->sliceheight
;
2461 slice
= &tile
->slices
[firstslice
];
2462 slicey
= tiley
% tile
->sliceheight
;
2467 dh
= slice
->h
- slicey
;
2468 dh
= fz_mini (disph
, dh
);
2469 uploadslice (tile
, slice
);
2471 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2472 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2473 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2474 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2476 vertices
[0] = dispx
; vertices
[1] = dispy
;
2477 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2478 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2479 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2481 #if TEXT_TYPE == GL_TEXTURE_2D
2482 for (int i
= 0; i
< 8; ++i
) {
2483 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2487 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2491 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2497 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2499 fz_matrix ctm
, tm
, pm
;
2500 fz_point p1
, p2
, p3
, p4
;
2501 GLfloat
*vertices
= state
.vertices
;
2502 double *v
= (double *) rects_v
;
2504 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2505 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2506 fz_translate (&tm
, xoff
, yoff
);
2507 pm
= pagectm (page
);
2508 fz_concat (&ctm
, &pm
, &tm
);
2510 glEnable (GL_BLEND
);
2511 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2525 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2526 glDisable (GL_BLEND
);
2529 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2530 value xoff_v
, value yoff_v
,
2533 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2534 int xoff
= Int_val (xoff_v
);
2535 int yoff
= Int_val (yoff_v
);
2536 int noff
= Int_val (Field (li_v
, 0));
2537 char *targ
= String_val (Field (li_v
, 1));
2538 int tlen
= caml_string_length (Field (li_v
, 1));
2539 int hfsize
= Int_val (Field (li_v
, 2));
2540 char *s
= String_val (ptr_v
);
2541 int hlmask
= Int_val (hlinks_v
);
2542 struct page
*page
= parse_pointer (__func__
, s
);
2544 if (!page
->fzpage
) {
2545 /* deal with loadpage failed pages */
2549 if (trylock (__func__
)) {
2554 ensureannots (page
);
2555 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2557 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2558 noff
= page
->slinkcount
;
2560 if (page
->tgen
== state
.gen
) {
2561 showsel (page
, xoff
, yoff
);
2566 CAMLreturn (Val_int (noff
));
2569 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2572 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2573 int xoff
= Int_val (xoff_v
);
2574 int yoff
= Int_val (yoff_v
);
2575 char *s
= String_val (ptr_v
);
2576 struct page
*page
= parse_pointer (__func__
, s
);
2578 drawprect (page
, xoff
, yoff
, rects_v
);
2582 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2586 const fz_matrix
*tctm
;
2587 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2589 if (!page
->annots
) return NULL
;
2592 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2593 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2596 tctm
= &fz_identity
;
2602 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2603 fz_invert_matrix (&ctm
, &ctm
);
2604 fz_transform_point (&p
, &ctm
);
2607 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2608 struct annot
*a
= &page
->annots
[i
];
2611 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2612 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2613 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2621 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2625 fz_link
*link
, *links
;
2627 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2632 ctm
= pagectm (page
);
2633 fz_invert_matrix (&ctm
, &ctm
);
2634 fz_transform_point (&p
, &ctm
);
2636 for (link
= links
; link
; link
= link
->next
) {
2637 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2638 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2646 static void ensuretext (struct page
*page
)
2648 if (state
.gen
!= page
->tgen
) {
2650 page
->tgen
= state
.gen
;
2656 page
->text
= fz_new_stext_page (state
.ctx
,
2657 &state
.pagedims
[page
->pdimno
].mediabox
);
2658 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2659 ctm
= pagectm (page
);
2660 fz_run_display_list (state
.ctx
, page
->dlist
,
2661 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2662 fz_close_device (state
.ctx
, tdev
);
2663 fz_drop_device (state
.ctx
, tdev
);
2667 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2669 CAMLparam2 (start_page_v
, dir_v
);
2671 int i
, dir
= Int_val (dir_v
);
2672 int start_page
= Int_val (start_page_v
);
2673 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2677 ret_v
= Val_int (0);
2679 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2680 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2685 pdf_page
*page
= NULL
;
2688 fz_try (state
.ctx
) {
2689 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2690 found
= !!page
->links
|| !!page
->annots
;
2692 fz_catch (state
.ctx
) {
2696 fz_drop_page (state
.ctx
, &page
->super
);
2700 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2701 found
= !!fz_load_links (state
.ctx
, page
);
2702 fz_drop_page (state
.ctx
, page
);
2706 ret_v
= caml_alloc_small (1, 1);
2707 Field (ret_v
, 0) = Val_int (i
);
2716 enum { dir_first
, dir_last
};
2717 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2719 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2721 CAMLparam2 (ptr_v
, dir_v
);
2722 CAMLlocal2 (ret_v
, pos_v
);
2724 int dirtag
, i
, slinkindex
;
2725 struct slink
*found
= NULL
,*slink
;
2726 char *s
= String_val (ptr_v
);
2728 page
= parse_pointer (__func__
, s
);
2729 ret_v
= Val_int (0);
2731 ensureslinks (page
);
2733 if (Is_block (dir_v
)) {
2734 dirtag
= Tag_val (dir_v
);
2736 case dir_first_visible
:
2738 int x0
, y0
, dir
, first_index
, last_index
;
2740 pos_v
= Field (dir_v
, 0);
2741 x0
= Int_val (Field (pos_v
, 0));
2742 y0
= Int_val (Field (pos_v
, 1));
2743 dir
= Int_val (Field (pos_v
, 2));
2748 last_index
= page
->slinkcount
;
2751 first_index
= page
->slinkcount
- 1;
2755 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2756 slink
= &page
->slinks
[i
];
2757 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2766 slinkindex
= Int_val (Field (dir_v
, 0));
2767 found
= &page
->slinks
[slinkindex
];
2768 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2769 slink
= &page
->slinks
[i
];
2770 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2778 slinkindex
= Int_val (Field (dir_v
, 0));
2779 found
= &page
->slinks
[slinkindex
];
2780 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2781 slink
= &page
->slinks
[i
];
2782 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2790 slinkindex
= Int_val (Field (dir_v
, 0));
2791 found
= &page
->slinks
[slinkindex
];
2792 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2793 slink
= &page
->slinks
[i
];
2794 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2802 slinkindex
= Int_val (Field (dir_v
, 0));
2803 found
= &page
->slinks
[slinkindex
];
2804 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2805 slink
= &page
->slinks
[i
];
2806 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2815 dirtag
= Int_val (dir_v
);
2818 found
= page
->slinks
;
2823 found
= page
->slinks
+ (page
->slinkcount
- 1);
2829 ret_v
= caml_alloc_small (2, 1);
2830 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2837 enum { uuri
, utext
, uannot
};
2839 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2841 CAMLparam2 (ptr_v
, n_v
);
2842 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2845 char *s
= String_val (ptr_v
);
2846 struct slink
*slink
;
2848 ret_v
= Val_int (0);
2849 page
= parse_pointer (__func__
, s
);
2852 ensureslinks (page
);
2853 slink
= &page
->slinks
[Int_val (n_v
)];
2854 if (slink
->tag
== SLINK
) {
2855 link
= slink
->u
.link
;
2856 str_v
= caml_copy_string (link
->uri
);
2857 ret_v
= caml_alloc_small (1, uuri
);
2858 Field (ret_v
, 0) = str_v
;
2861 ret_v
= caml_alloc_small (1, uannot
);
2862 tup_v
= caml_alloc_tuple (2);
2863 Field (ret_v
, 0) = tup_v
;
2864 Field (tup_v
, 0) = ptr_v
;
2865 Field (tup_v
, 1) = n_v
;
2872 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2874 CAMLparam2 (ptr_v
, n_v
);
2876 const char *contents
= "";
2879 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2881 char *s
= String_val (ptr_v
);
2883 struct slink
*slink
;
2885 page
= parse_pointer (__func__
, s
);
2886 slink
= &page
->slinks
[Int_val (n_v
)];
2887 contents
= pdf_annot_contents (state
.ctx
,
2888 (pdf_annot
*) slink
->u
.annot
);
2891 CAMLreturn (caml_copy_string (contents
));
2894 CAMLprim value
ml_getlinkcount (value ptr_v
)
2898 char *s
= String_val (ptr_v
);
2900 page
= parse_pointer (__func__
, s
);
2901 CAMLreturn (Val_int (page
->slinkcount
));
2904 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2906 CAMLparam2 (ptr_v
, n_v
);
2909 struct slink
*slink
;
2910 char *s
= String_val (ptr_v
);
2912 page
= parse_pointer (__func__
, s
);
2913 ret_v
= caml_alloc_tuple (4);
2915 ensureslinks (page
);
2917 slink
= &page
->slinks
[Int_val (n_v
)];
2918 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2919 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2920 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2921 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2926 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2928 CAMLparam3 (ptr_v
, x_v
, y_v
);
2929 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2931 struct annot
*annot
;
2933 char *ptr
= String_val (ptr_v
);
2934 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2935 struct pagedim
*pdim
;
2937 ret_v
= Val_int (0);
2938 if (trylock (__func__
)) {
2942 page
= parse_pointer (__func__
, ptr
);
2943 pdim
= &state
.pagedims
[page
->pdimno
];
2944 x
+= pdim
->bounds
.x0
;
2945 y
+= pdim
->bounds
.y0
;
2948 annot
= getannot (page
, x
, y
);
2952 ensureslinks (page
);
2953 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2954 if (page
->slinks
[i
].tag
== SANNOT
2955 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2960 ret_v
= caml_alloc_small (1, uannot
);
2961 tup_v
= caml_alloc_tuple (2);
2962 Field (ret_v
, 0) = tup_v
;
2963 Field (tup_v
, 0) = ptr_v
;
2964 Field (tup_v
, 1) = Val_int (n
);
2969 link
= getlink (page
, x
, y
);
2971 str_v
= caml_copy_string (link
->uri
);
2972 ret_v
= caml_alloc_small (1, uuri
);
2973 Field (ret_v
, 0) = str_v
;
2977 fz_stext_block
*block
;
2981 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2982 fz_stext_line
*line
;
2984 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2986 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2989 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2993 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2996 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2999 /* fz_stext_char_bbox (state.ctx, &bbox, line, ch); */
3002 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3003 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
3004 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3007 if (!n2
) n2
= "<unknown font>";
3009 if (face
&& face
->family_name
) {
3011 char *n1
= face
->family_name
;
3012 size_t l1
= strlen (n1
);
3013 size_t l2
= strlen (n2
);
3015 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3016 s
= malloc (l1
+ l2
+ 2);
3020 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3021 str_v
= caml_copy_string (s
);
3026 if (str_v
== Val_unit
) {
3027 str_v
= caml_copy_string (n2
);
3029 ret_v
= caml_alloc_small (1, utext
);
3030 Field (ret_v
, 0) = str_v
;
3044 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3046 static int uninteresting (int c
)
3048 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3052 CAMLprim
void ml_clearmark (value ptr_v
)
3055 char *s
= String_val (ptr_v
);
3058 if (trylock (__func__
)) {
3062 page
= parse_pointer (__func__
, s
);
3063 page
->fmark
.ch
= NULL
;
3064 page
->lmark
.ch
= NULL
;
3071 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3073 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3077 fz_stext_line
*line
;
3078 fz_stext_block
*block
;
3079 struct pagedim
*pdim
;
3080 int mark
= Int_val (mark_v
);
3081 char *s
= String_val (ptr_v
);
3082 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3084 ret_v
= Val_bool (0);
3085 if (trylock (__func__
)) {
3089 page
= parse_pointer (__func__
, s
);
3090 pdim
= &state
.pagedims
[page
->pdimno
];
3094 if (mark
== mark_page
) {
3095 page
->fmark
.ch
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
3096 page
->lmark
.ch
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
3097 ret_v
= Val_bool (1);
3101 x
+= pdim
->bounds
.x0
;
3102 y
+= pdim
->bounds
.y0
;
3104 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3105 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3107 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3110 if (mark
== mark_block
) {
3111 page
->fmark
.ch
= block
->u
.t
.first_line
->first_char
;
3112 page
->lmark
.ch
= block
->u
.t
.last_line
->last_char
;
3113 ret_v
= Val_bool (1);
3117 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3121 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3124 if (mark
== mark_line
) {
3125 page
->fmark
.ch
= line
->first_char
;
3126 page
->lmark
.ch
= line
->last_char
;
3127 ret_v
= Val_bool (1);
3131 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3132 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
3134 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3135 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
3136 if (uninteresting (ch2
->c
)) first
= NULL
;
3137 else if (!first
) first
= ch2
;
3139 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
3140 if (uninteresting (ch2
->c
)) break;
3144 page
->fmark
.ch
= first
;
3145 page
->lmark
.ch
= last
;
3146 ret_v
= Val_bool (1);
3153 if (!Bool_val (ret_v
)) {
3154 page
->fmark
.ch
= NULL
;
3155 page
->lmark
.ch
= NULL
;
3163 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3165 CAMLparam3 (ptr_v
, x_v
, y_v
);
3166 CAMLlocal2 (ret_v
, res_v
);
3169 struct pagedim
*pdim
;
3170 fz_stext_block
*block
;
3171 char *s
= String_val (ptr_v
);
3172 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3174 ret_v
= Val_int (0);
3175 if (trylock (__func__
)) {
3179 page
= parse_pointer (__func__
, s
);
3180 pdim
= &state
.pagedims
[page
->pdimno
];
3181 x
+= pdim
->bounds
.x0
;
3182 y
+= pdim
->bounds
.y0
;
3186 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3187 switch (block
->type
) {
3188 case FZ_STEXT_BLOCK_TEXT
:
3192 case FZ_STEXT_BLOCK_IMAGE
:
3200 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3205 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3206 ret_v
= caml_alloc_small (1, 1);
3207 Store_double_field (res_v
, 0, b
->x0
);
3208 Store_double_field (res_v
, 1, b
->x1
);
3209 Store_double_field (res_v
, 2, b
->y0
);
3210 Store_double_field (res_v
, 3, b
->y1
);
3211 Field (ret_v
, 0) = res_v
;
3219 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3221 CAMLparam2 (ptr_v
, rect_v
);
3224 struct pagedim
*pdim
;
3225 char *s
= String_val (ptr_v
);
3228 fz_stext_line
*line
;
3229 fz_stext_block
*block
;
3230 fz_stext_char
*fc
, *lc
;
3232 if (trylock (__func__
)) {
3236 page
= parse_pointer (__func__
, s
);
3239 pdim
= &state
.pagedims
[page
->pdimno
];
3240 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3241 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3242 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3243 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3253 fc
= page
->fmark
.ch
;
3254 lc
= page
->lmark
.ch
;
3256 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3257 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3258 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3259 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3261 if (x0
>= b
.x0
&& x0
<= b
.x1
&& y0
>= b
.y0
&& y0
<= b
.y1
) {
3264 if (x1
>= b
.x0
&& x1
<= b
.x1
&& y1
>= b
.y0
&& y1
<= b
.y1
) {
3270 if (x1
< x0
&& fc
== lc
) {
3278 page
->fmark
.ch
= fc
;
3279 page
->lmark
.ch
= lc
;
3287 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3292 len
= fz_runetochar (buf
, ch
->c
);
3293 ret
= fwrite (buf
, len
, 1, f
);
3295 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3296 len
, ret
, strerror (errno
));
3303 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3305 caml_failwith ("ml_popen not implemented under Cygwin");
3308 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3310 CAMLparam2 (command_v
, fds_v
);
3311 CAMLlocal2 (l_v
, tup_v
);
3315 value earg_v
= Nothing
;
3316 posix_spawnattr_t attr
;
3317 posix_spawn_file_actions_t fa
;
3318 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3320 argv
[2] = String_val (command_v
);
3322 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3323 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3326 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3327 msg
= "posix_spawnattr_init";
3331 #ifdef POSIX_SPAWN_USEVFORK
3332 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3333 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3338 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3341 tup_v
= Field (l_v
, 0);
3342 fd1
= Int_val (Field (tup_v
, 0));
3343 fd2
= Int_val (Field (tup_v
, 1));
3345 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3346 msg
= "posix_spawn_file_actions_addclose";
3352 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3353 msg
= "posix_spawn_file_actions_adddup2";
3360 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3361 msg
= "posix_spawn";
3366 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3367 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3371 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3372 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3377 unix_error (ret
, msg
, earg_v
);
3379 CAMLreturn (Val_int (pid
));
3383 CAMLprim value
ml_hassel (value ptr_v
)
3388 char *s
= String_val (ptr_v
);
3390 ret_v
= Val_bool (0);
3391 if (trylock (__func__
)) {
3395 page
= parse_pointer (__func__
, s
);
3396 ret_v
= Val_bool (page
->fmark
.ch
&& page
->lmark
.ch
);
3402 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3404 CAMLparam2 (fd_v
, ptr_v
);
3408 fz_stext_line
*line
;
3409 fz_stext_block
*block
;
3410 int fd
= Int_val (fd_v
);
3411 char *s
= String_val (ptr_v
);
3413 if (trylock (__func__
)) {
3417 page
= parse_pointer (__func__
, s
);
3419 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) {
3420 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3424 f
= fdopen (fd
, "w");
3426 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3427 fd
, strerror (errno
));
3431 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3432 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3433 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3435 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3436 if (seen
|| ch
== page
->fmark
.ch
) {
3439 if (ch
== page
->lmark
.ch
) goto close
;
3440 } while ((ch
= ch
->next
));
3445 if (seen
) fputc ('\n', f
);
3450 int ret
= fclose (f
);
3453 if (errno
!= ECHILD
) {
3454 fprintf (stderr
, "failed to close sel pipe: %s\n",
3465 fprintf (stderr
, "failed to close sel pipe: %s\n",
3472 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3474 CAMLparam1 (pagedimno_v
);
3476 int pagedimno
= Int_val (pagedimno_v
);
3479 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3480 if (trylock (__func__
)) {
3481 box
= fz_empty_rect
;
3484 box
= state
.pagedims
[pagedimno
].mediabox
;
3488 Store_double_field (ret_v
, 0, box
.x0
);
3489 Store_double_field (ret_v
, 1, box
.x1
);
3490 Store_double_field (ret_v
, 2, box
.y0
);
3491 Store_double_field (ret_v
, 3, box
.y1
);
3496 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3497 value dw_v
, value cols_v
)
3499 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3505 double winw
= Int_val (winw_v
);
3506 double winh
= Int_val (winh_v
);
3507 double dw
= Int_val (dw_v
);
3508 double cols
= Int_val (cols_v
);
3509 double pw
= 1.0, ph
= 1.0;
3511 if (trylock (__func__
)) {
3515 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3516 double w
= p
->pagebox
.x1
/ cols
;
3517 double h
= p
->pagebox
.y1
;
3521 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3523 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3526 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3529 ret_v
= caml_copy_double (zoom
);
3533 CAMLprim value
ml_getmaxw (value unit_v
)
3535 CAMLparam1 (unit_v
);
3541 if (trylock (__func__
)) {
3545 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3546 double w
= p
->pagebox
.x1
;
3547 maxw
= fz_max (maxw
, w
);
3552 ret_v
= caml_copy_double (maxw
);
3556 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3558 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3560 int pt
= Int_val(pt_v
);
3561 int x
= Int_val (x_v
);
3562 int y
= Int_val (y_v
);
3565 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3566 ret_v
= caml_copy_double (w
);
3570 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3572 CAMLparam2 (pt_v
, string_v
);
3574 int pt
= Int_val (pt_v
);
3577 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3578 ret_v
= caml_copy_double (w
);
3582 CAMLprim value
ml_getpagebox (value opaque_v
)
3584 CAMLparam1 (opaque_v
);
3590 char *s
= String_val (opaque_v
);
3591 struct page
*page
= parse_pointer (__func__
, s
);
3593 ret_v
= caml_alloc_tuple (4);
3594 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3596 ctm
= pagectm (page
);
3597 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3599 fz_close_device (state
.ctx
, dev
);
3600 fz_drop_device (state
.ctx
, dev
);
3601 fz_round_rect (&bbox
, &rect
);
3602 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3603 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3604 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3605 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3610 CAMLprim
void ml_setaalevel (value level_v
)
3612 CAMLparam1 (level_v
);
3614 state
.aalevel
= Int_val (level_v
);
3619 #pragma GCC diagnostic push
3620 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3621 #include <X11/Xlib.h>
3622 #include <X11/cursorfont.h>
3623 #pragma GCC diagnostic pop
3626 #include <EGL/egl.h>
3631 static const int shapes
[] = {
3632 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3635 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3648 XVisualInfo
*visual
;
3649 Cursor curs
[CURS_COUNT
];
3653 static void initcurs (void)
3655 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3656 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3660 CAMLprim
void ml_setbgcol (value color_v
)
3662 CAMLparam1 (color_v
);
3663 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3668 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3670 CAMLparam3 (display_v
, wid_v
, screen_v
);
3674 EGLint attribs
[] = {
3675 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3676 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3681 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3683 caml_failwith ("XOpenDisplay");
3686 eglBindAPI (EGL_OPENGL_API
);
3688 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3689 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3690 caml_failwith ("eglGetDisplay");
3693 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3694 caml_failwith ("eglInitialize");
3697 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3699 caml_failwith ("eglChooseConfig");
3702 if (!eglGetConfigAttrib (glx
.edpy
, conf
, EGL_NATIVE_VISUAL_ID
, &visid
)) {
3703 caml_failwith ("eglGetConfigAttrib");
3709 glx
.wid
= Int_val (wid_v
);
3710 CAMLreturn (Val_int (visid
));
3713 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3715 CAMLparam1 (unit_v
);
3717 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3719 caml_failwith ("eglCreateContext");
3722 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3724 if (glx
.win
== EGL_NO_SURFACE
) {
3725 caml_failwith ("eglCreateWindowSurface");
3728 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3730 caml_failwith ("eglMakeCurrent");
3735 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3737 CAMLparam3 (display_v
, wid_v
, screen_v
);
3739 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3741 caml_failwith ("XOpenDisplay");
3744 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3745 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3747 XCloseDisplay (glx
.dpy
);
3748 caml_failwith ("glXChooseVisual");
3753 glx
.wid
= Int_val (wid_v
);
3754 CAMLreturn (Val_int (glx
.visual
->visualid
));
3757 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3759 CAMLparam1 (unit_v
);
3761 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3763 caml_failwith ("glXCreateContext");
3769 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3770 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3772 caml_failwith ("glXMakeCurrent");
3778 CAMLprim
void ml_setcursor (value cursor_v
)
3780 CAMLparam1 (cursor_v
);
3781 size_t cursn
= Int_val (cursor_v
);
3783 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
3784 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
3789 CAMLprim
void ml_swapb (value unit_v
)
3791 CAMLparam1 (unit_v
);
3793 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
3794 caml_failwith ("eglSwapBuffers");
3797 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3802 #include "keysym2ucs.c"
3804 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3806 CAMLparam1 (keysym_v
);
3808 KeySym keysym
= Int_val (keysym_v
);
3813 rune
= keysym2ucs (keysym
);
3814 len
= fz_runetochar (buf
, rune
);
3816 str_v
= caml_copy_string (buf
);
3820 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3822 CAMLparam1 (keysym_v
);
3824 long ucs_v
= Long_val (keysym_v
);
3828 len
= fz_runetochar (buf
, ucs_v
);
3830 str_v
= caml_copy_string (buf
);
3835 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
3837 CAMLprim value
ml_platform (value unit_v
)
3839 CAMLparam1 (unit_v
);
3840 CAMLlocal2 (tup_v
, arr_v
);
3841 int platid
= piunknown
;
3844 #if defined __linux__
3846 #elif defined __CYGWIN__
3848 #elif defined __DragonFly__ || defined __FreeBSD__
3849 || defined __OpenBSD__
|| defined __NetBSD__
3851 #elif defined __sun__
3853 #elif defined __APPLE__
3856 if (uname (&buf
)) err (1, "uname");
3858 tup_v
= caml_alloc_tuple (2);
3860 char const *sar
[] = {
3867 arr_v
= caml_copy_string_array (sar
);
3869 Field (tup_v
, 0) = Val_int (platid
);
3870 Field (tup_v
, 1) = arr_v
;
3874 CAMLprim
void ml_cloexec (value fd_v
)
3877 int fd
= Int_val (fd_v
);
3879 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3880 uerror ("fcntl", Nothing
);
3885 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3887 CAMLparam2 (w_v
, h_v
);
3890 int w
= Int_val (w_v
);
3891 int h
= Int_val (h_v
);
3892 int cs
= Int_val (cs_v
);
3894 if (state
.bo_usable
) {
3895 pbo
= calloc (sizeof (*pbo
), 1);
3897 err (1, "calloc pbo");
3909 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3912 state
.glGenBuffersARB (1, &pbo
->id
);
3913 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3914 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3915 NULL
, GL_STREAM_DRAW
);
3916 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3918 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3920 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3921 state
.glDeleteBuffersARB (1, &pbo
->id
);
3923 ret_v
= caml_copy_string ("0");
3929 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3931 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3935 err (1, "malloc %d bytes failed", res
+1);
3937 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3939 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3941 ret_v
= caml_copy_string (s
);
3946 ret_v
= caml_copy_string ("0");
3951 CAMLprim
void ml_freepbo (value s_v
)
3954 char *s
= String_val (s_v
);
3955 struct tile
*tile
= parse_pointer (__func__
, s
);
3958 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3960 tile
->pbo
->ptr
= NULL
;
3961 tile
->pbo
->size
= -1;
3966 CAMLprim
void ml_unmappbo (value s_v
)
3969 char *s
= String_val (s_v
);
3970 struct tile
*tile
= parse_pointer (__func__
, s
);
3973 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3974 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3975 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3977 tile
->pbo
->ptr
= NULL
;
3978 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3983 static void setuppbo (void)
3986 static CFBundleRef framework
= NULL
;
3987 if (framework
== NULL
)
3988 framework
= CFBundleGetBundleWithIdentifier (CFSTR ("com.apple.opengl"));
3989 #define GGPA(n) (&state.n = CFBundleGetFunctionPointerForName (framework, CFSTR (#n)))
3992 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
3994 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
3996 state
.bo_usable
= GGPA (glBindBufferARB
)
3997 && GGPA (glUnmapBufferARB
)
3998 && GGPA (glMapBufferARB
)
3999 && GGPA (glBufferDataARB
)
4000 && GGPA (glGenBuffersARB
)
4001 && GGPA (glDeleteBuffersARB
);
4006 CAMLprim value
ml_bo_usable (value unit_v
)
4008 CAMLparam1 (unit_v
);
4009 CAMLreturn (Val_bool (state
.bo_usable
));
4012 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4014 CAMLparam3 (ptr_v
, x_v
, y_v
);
4015 CAMLlocal2 (ret_v
, tup_v
);
4017 char *s
= String_val (ptr_v
);
4018 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4019 struct pagedim
*pdim
;
4023 page
= parse_pointer (__func__
, s
);
4024 pdim
= &state
.pagedims
[page
->pdimno
];
4026 ret_v
= Val_int (0);
4027 if (trylock (__func__
)) {
4031 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4032 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4033 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4038 p
.x
= x
+ pdim
->bounds
.x0
;
4039 p
.y
= y
+ pdim
->bounds
.y0
;
4041 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4042 fz_invert_matrix (&ctm
, &ctm
);
4043 fz_transform_point (&p
, &ctm
);
4045 tup_v
= caml_alloc_tuple (2);
4046 ret_v
= caml_alloc_small (1, 1);
4047 Field (tup_v
, 0) = Val_int (p
.x
);
4048 Field (tup_v
, 1) = Val_int (p
.y
);
4049 Field (ret_v
, 0) = tup_v
;
4056 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4057 value x_v
, value y_v
)
4059 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4062 char *s
= String_val (ptr_v
);
4063 int pageno
= Int_val (pageno_v
);
4064 int pdimno
= Int_val (pdimno_v
);
4065 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4066 struct pagedim
*pdim
;
4070 ret_v
= Val_int (0);
4074 page
= loadpage (pageno
, pdimno
);
4077 page
= parse_pointer (__func__
, s
);
4079 pdim
= &state
.pagedims
[pdimno
];
4081 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4082 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4083 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4088 p
.x
= x
+ pdim
->bounds
.x0
;
4089 p
.y
= y
+ pdim
->bounds
.y0
;
4091 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4092 fz_transform_point (&p
, &ctm
);
4094 ret_v
= caml_alloc_tuple (2);
4095 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4096 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4105 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
4108 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4109 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4115 char *s
= String_val (ptr_v
);
4117 page
= parse_pointer (__func__
, s
);
4118 annot
= pdf_create_annot (state
.ctx
,
4119 pdf_page_from_fz_page (state
.ctx
,
4122 p
.x
= Int_val (x_v
);
4123 p
.y
= Int_val (y_v
);
4124 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4125 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4131 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
4133 CAMLparam2 (ptr_v
, n_v
);
4134 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4138 char *s
= String_val (ptr_v
);
4139 struct slink
*slink
;
4141 page
= parse_pointer (__func__
, s
);
4142 slink
= &page
->slinks
[Int_val (n_v
)];
4143 pdf_delete_annot (state
.ctx
,
4144 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4145 (pdf_annot
*) slink
->u
.annot
);
4151 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
4153 CAMLparam3 (ptr_v
, n_v
, str_v
);
4154 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4158 char *s
= String_val (ptr_v
);
4159 struct slink
*slink
;
4161 page
= parse_pointer (__func__
, s
);
4162 slink
= &page
->slinks
[Int_val (n_v
)];
4163 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4164 String_val (str_v
));
4170 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4172 CAMLparam1 (unit_v
);
4173 CAMLreturn (Val_bool (state
.dirty
));
4176 CAMLprim
void ml_savedoc (value path_v
)
4178 CAMLparam1 (path_v
);
4179 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4182 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4187 static void makestippletex (void)
4189 const char pixels
[] = "\xff\xff\0\0";
4190 glGenTextures (1, &state
.stid
);
4191 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4192 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4193 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4206 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4208 return caml_copy_string (FZ_VERSION
);
4211 CAMLprim
void ml_init (value csock_v
, value params_v
)
4213 CAMLparam2 (csock_v
, params_v
);
4214 CAMLlocal2 (trim_v
, fuzz_v
);
4222 /* Without following call to setlocale mbstowcs fails for, at
4223 least, strings containing chinese symbols (δΈ for instance)
4224 (with glibc citing EILSEQ="Invalid or incomplete multibyte or
4225 wide character" as the reason of failure and with macOS
4226 producing bogus output) */
4227 if (setlocale (LC_CTYPE
, "")) {
4228 /* Following two lines were taken from dvtm/vt.c */
4229 const char *cset
= nl_langinfo (CODESET
);
4230 state
.utf8cs
= !strcmp (cset
, "UTF-8");
4233 fprintf (stderr
, "setlocale failed\n");
4236 state
.csock
= Int_val (csock_v
);
4237 state
.rotate
= Int_val (Field (params_v
, 0));
4238 state
.fitmodel
= Int_val (Field (params_v
, 1));
4239 trim_v
= Field (params_v
, 2);
4240 texcount
= Int_val (Field (params_v
, 3));
4241 state
.sliceheight
= Int_val (Field (params_v
, 4));
4242 mustoresize
= Int_val (Field (params_v
, 5));
4243 colorspace
= Int_val (Field (params_v
, 6));
4244 fontpath
= String_val (Field (params_v
, 7));
4246 if (caml_string_length (Field (params_v
, 8)) > 0) {
4247 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4249 if (!state
.trimcachepath
) {
4250 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4255 haspboext
= Bool_val (Field (params_v
, 9));
4257 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4258 fz_register_document_handlers (state
.ctx
);
4260 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4261 fuzz_v
= Field (trim_v
, 1);
4262 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4263 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4264 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4265 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4267 set_tex_params (colorspace
);
4270 state
.face
= load_font (fontpath
);
4274 const unsigned char *data
;
4276 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
4277 state
.face
= load_builtin_font (data
, len
);
4279 if (!state
.face
) _exit (1);
4281 realloctexts (texcount
);
4289 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4291 errx (1, "pthread_create: %s", strerror (ret
));