1 /* lots of code c&p-ed directly from mupdf */
6 #define CAML_NAME_SPACE
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/utsname.h>
41 #include <CoreFoundation/CoreFoundation.h>
45 #include <OpenGL/gl.h>
50 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
54 #pragma GCC diagnostic ignored "-Wpedantic"
55 #include <caml/fail.h>
56 #include <caml/alloc.h>
57 #include <caml/memory.h>
58 #include <caml/unixsupport.h>
60 #if __GNUC__ < 5 && !defined __clang__
61 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
62 clobbered diagnostics */
63 #pragma GCC diagnostic ignored "-Wclobbered"
66 #include <mupdf/fitz.h>
67 #include <mupdf/pdf.h>
70 #include FT_FREETYPE_H
71 #pragma GCC diagnostic pop
74 #define CACHE_PAGEREFS
77 #define NORETURN_ATTR __attribute__ ((noreturn))
78 #define UNUSED_ATTR __attribute__ ((unused))
79 #if !defined __clang__
80 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
82 #define OPTIMIZE_ATTR(n)
84 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
88 #define OPTIMIZE_ATTR(n)
89 #define GCC_FMT_ATTR(a, b)
94 #define FMT_ptr PRIxPTR
95 #define SCN_ptr SCNxPTR
96 #define FMT_ptr_cast(p) ((uintptr_t) (p))
97 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
99 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
100 err (int exitcode
, const char *fmt
, ...)
107 vfprintf (stderr
, fmt
, ap
);
109 fprintf (stderr
, ": %s\n", strerror (savederrno
));
114 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
115 errx (int exitcode
, const char *fmt
, ...)
120 vfprintf (stderr
, fmt
, ap
);
122 fputc ('\n', stderr
);
127 #ifndef GL_TEXTURE_RECTANGLE_ARB
128 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
132 #define TEXT_TYPE GL_TEXTURE_2D
134 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
138 #define GL_BGRA 0x80E1
141 #ifndef GL_UNSIGNED_INT_8_8_8_8
142 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
145 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
146 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
150 #define lprintf printf
155 #define ARSERT(cond) for (;;) { \
157 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
173 struct slice slices
[1];
184 fz_matrix ctm
, zoomctm
, tctm
;
188 enum { SLINK
, SANNOT
} tag
;
209 fz_display_list
*dlist
;
212 struct slink
*slinks
;
214 struct annot
*annots
;
220 enum { FitWidth
, FitProportional
, FitPage
};
224 struct pagedim
*pagedims
;
239 fz_colorspace
*colorspace
;
269 void (*glBindBufferARB
) (GLenum
, GLuint
);
270 GLboolean (*glUnmapBufferARB
) (GLenum
);
271 void *(*glMapBufferARB
) (GLenum
, GLenum
);
272 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
273 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
274 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
276 GLfloat texcoords
[8];
277 GLfloat vertices
[16];
279 #ifdef CACHE_PAGEREFS
296 #pragma GCC diagnostic ignored "-Wdouble-promotion"
297 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
299 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
302 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
304 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
307 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
309 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
310 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
312 #pragma GCC diagnostic error "-Wdouble-promotion"
314 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
316 static void lock (const char *cap
)
318 int ret
= pthread_mutex_lock (&mutex
);
320 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
324 static void unlock (const char *cap
)
326 int ret
= pthread_mutex_unlock (&mutex
);
328 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
332 static int trylock (const char *cap
)
334 int ret
= pthread_mutex_trylock (&mutex
);
335 if (ret
&& ret
!= EBUSY
) {
336 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
341 static void *parse_pointer (const char *cap
, const char *s
)
346 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
348 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
353 static double now (void)
357 if (gettimeofday (&tv
, NULL
)) {
358 err (1, "gettimeofday");
360 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
363 static int hasdata (void)
366 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
367 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
371 CAMLprim value
ml_hasdata (value fd_v
)
376 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
377 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
378 CAMLreturn (Val_bool (avail
> 0));
381 static void readdata (int fd
, void *p
, int size
)
386 n
= read (fd
, p
, size
);
388 if (n
< 0 && errno
== EINTR
) goto again
;
389 if (!n
) errx (1, "EOF while reading");
390 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
394 static void writedata (int fd
, char *p
, int size
)
397 uint32_t size4
= size
;
398 struct iovec iov
[2] = {
399 { .iov_base
= &size4
, .iov_len
= 4 },
400 { .iov_base
= p
, .iov_len
= size
}
404 n
= writev (fd
, iov
, 2);
405 if (n
< 0 && errno
== EINTR
) goto again
;
407 if (!n
) errx (1, "EOF while writing data");
408 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
412 static int readlen (int fd
)
415 readdata (fd
, &u
, 4);
419 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
421 CAMLparam3 (fd_v
, bytes_v
, len_v
);
422 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
426 CAMLprim value
ml_rcmd (value fd_v
)
429 CAMLlocal1 (strdata_v
);
430 int fd
= Int_val (fd_v
);
431 int len
= readlen (fd
);
432 strdata_v
= caml_alloc_string (len
);
433 readdata (fd
, String_val (strdata_v
), len
);
434 CAMLreturn (strdata_v
);
437 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
440 int size
= sizeof (fbuf
), len
;
446 len
= vsnprintf (buf
, size
, fmt
, ap
);
450 if (len
< size
- 4) {
451 writedata (state
.csock
, buf
, len
);
457 err (1, "vsnprintf for `%s' failed", fmt
);
459 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
460 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
462 if (buf
!= fbuf
) free (buf
);
465 static void closedoc (void)
467 #ifdef CACHE_PAGEREFS
468 if (state
.pdflut
.objs
) {
469 for (int i
= 0; i
< state
.pdflut
.count
; ++i
) {
470 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
472 free (state
.pdflut
.objs
);
473 state
.pdflut
.objs
= NULL
;
474 state
.pdflut
.idx
= 0;
478 fz_drop_document (state
.ctx
, state
.doc
);
483 static int openxref (char *filename
, char *password
, int layouth
)
485 for (int i
= 0; i
< state
.texcount
; ++i
) {
486 state
.texowners
[i
].w
= -1;
487 state
.texowners
[i
].slice
= NULL
;
493 if (state
.pagedims
) {
494 free (state
.pagedims
);
495 state
.pagedims
= NULL
;
497 state
.pagedimcount
= 0;
499 fz_set_aa_level (state
.ctx
, state
.aalevel
);
500 state
.doc
= fz_open_document (state
.ctx
, filename
);
501 if (fz_needs_password (state
.ctx
, state
.doc
)) {
502 if (password
&& !*password
) {
507 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
509 printd ("pass fail");
515 fz_layout_document (state
.ctx
, state
.doc
, 460, layouth
, 12);
516 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
520 static void pdfinfo (void)
522 struct { char *tag
; char *name
; } metatbl
[] = {
523 { FZ_META_INFO_TITLE
, "Title" },
524 { FZ_META_INFO_AUTHOR
, "Author" },
525 { FZ_META_FORMAT
, "Format" },
526 { FZ_META_ENCRYPTION
, "Encryption" },
527 { "info:Creator", "Creator" },
528 { "info:Producer", "Producer" },
529 { "info:CreationDate", "Creation date" },
534 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
537 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
538 metatbl
[i
].tag
, buf
, len
);
541 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
544 buf
= realloc (buf
, need
+ 1);
545 if (!buf
) err (1, "pdfinfo realloc %d", need
+ 1);
556 static void unlinktile (struct tile
*tile
)
558 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
559 struct slice
*s
= &tile
->slices
[i
];
561 if (s
->texindex
!= -1) {
562 if (state
.texowners
[s
->texindex
].slice
== s
) {
563 state
.texowners
[s
->texindex
].slice
= NULL
;
569 static void freepage (struct page
*page
)
573 fz_drop_stext_page (state
.ctx
, page
->text
);
578 fz_drop_display_list (state
.ctx
, page
->dlist
);
579 fz_drop_page (state
.ctx
, page
->fzpage
);
583 static void freetile (struct tile
*tile
)
588 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
591 fz_drop_pixmap (state
.ctx
, state
.pig
);
593 state
.pig
= tile
->pixmap
;
598 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
607 static int cacheline32bytes
;
609 static void __attribute__ ((constructor
)) clcheck (void)
611 char **envp
= environ
;
616 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
618 cacheline32bytes
= auxv
[1] == 32;
624 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
626 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
627 if (cacheline32bytes
&& size
> 32) {
628 intptr_t a1
, a2
, diff
;
630 vector
unsigned char v
= vec_splat_u8 (-1);
631 vector
unsigned char *p
;
633 a1
= a2
= (intptr_t) pixmap
->samples
;
634 a2
= (a1
+ 31) & ~31;
639 while (a1
!= a2
) *(char *) a1
++ = 0xff;
640 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
641 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
643 vec_st (v
, i
+ 16, p
);
645 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
647 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
650 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
653 static void trimctm (pdf_page
*page
, int pindex
)
656 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
659 if (!pdim
->tctmready
) {
660 fz_rect realbox
, mediabox
;
661 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
663 fz_rotate (&rm
, -pdim
->rotate
);
664 fz_scale (&sm
, 1, -1);
665 fz_concat (&ctm
, &rm
, &sm
);
666 realbox
= pdim
->mediabox
;
667 fz_transform_rect (&realbox
, &ctm
);
668 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
669 fz_concat (&ctm1
, &ctm
, &tm
);
670 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
671 fz_invert_matrix (&im
, &page_ctm
);
672 fz_concat (&ctm
, &im
, &ctm1
);
678 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
681 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
683 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
684 if (pdf_specifics (state
.ctx
, state
.doc
)) {
685 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
686 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
689 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
690 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
695 static fz_matrix
pagectm (struct page
*page
)
697 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
700 static void *loadpage (int pageno
, int pindex
)
705 page
= calloc (sizeof (struct page
), 1);
707 err (1, "calloc page %d", pageno
);
710 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
711 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
713 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
714 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
717 fz_catch (state
.ctx
) {
720 fz_close_device (state
.ctx
, dev
);
721 fz_drop_device (state
.ctx
, dev
);
723 page
->pdimno
= pindex
;
724 page
->pageno
= pageno
;
725 page
->sgen
= state
.gen
;
726 page
->agen
= state
.gen
;
727 page
->tgen
= state
.gen
;
731 static struct tile
*alloctile (int h
)
737 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
738 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
739 tile
= calloc (tilesize
, 1);
741 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
743 for (int i
= 0; i
< slicecount
; ++i
) {
744 int sh
= fz_mini (h
, state
.sliceheight
);
745 tile
->slices
[i
].h
= sh
;
746 tile
->slices
[i
].texindex
= -1;
749 tile
->slicecount
= slicecount
;
750 tile
->sliceheight
= state
.sliceheight
;
754 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
762 struct pagedim
*pdim
;
764 tile
= alloctile (h
);
765 pdim
= &state
.pagedims
[page
->pdimno
];
770 bbox
.x1
= bbox
.x0
+ w
;
771 bbox
.y1
= bbox
.y0
+ h
;
774 if (state
.pig
->w
== w
776 && state
.pig
->colorspace
== state
.colorspace
) {
777 tile
->pixmap
= state
.pig
;
778 tile
->pixmap
->x
= bbox
.x0
;
779 tile
->pixmap
->y
= bbox
.y0
;
782 fz_drop_pixmap (state
.ctx
, state
.pig
);
789 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
790 &bbox
, NULL
, 1, pbo
->ptr
);
795 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
,
802 clearpixmap (tile
->pixmap
);
804 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
805 ctm
= pagectm (page
);
806 fz_rect_from_irect (&rect
, &bbox
);
807 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
808 fz_close_device (state
.ctx
, dev
);
809 fz_drop_device (state
.ctx
, dev
);
814 #ifdef CACHE_PAGEREFS
815 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
816 thanks to Robin Watts */
818 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
820 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
824 if (state
.pdflut
.idx
== state
.pagecount
) return;
826 kids
= pdf_dict_gets (ctx
, node
, "Kids");
827 len
= pdf_array_len (ctx
, kids
);
830 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
832 if (pdf_mark_obj (ctx
, node
))
833 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
834 for (int i
= 0; i
< len
; i
++) {
835 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
836 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
838 ? !strcmp (type
, "Pages")
839 : pdf_dict_gets (ctx
, kid
, "Kids")
840 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
841 pdf_collect_pages (doc
, kid
);
845 ? strcmp (type
, "Page") != 0
846 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
847 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
848 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
851 pdf_unmark_obj (ctx
, node
);
855 pdf_load_page_objs (pdf_document
*doc
)
857 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
858 pdf_trailer (state
.ctx
, doc
), "Root");
859 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
862 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
864 state
.pdflut
.idx
= 0;
865 pdf_collect_pages (doc
, node
);
869 static void initpdims (void)
873 fz_rect rootmediabox
= fz_empty_rect
;
874 int pageno
, trim
, show
;
875 int trimw
= 0, cxcount
;
876 fz_context
*ctx
= state
.ctx
;
877 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
884 if (state
.trimmargins
&& state
.trimcachepath
) {
885 trimf
= fopen (state
.trimcachepath
, "rb");
887 trimf
= fopen (state
.trimcachepath
, "wb");
892 if (state
.trimmargins
|| pdf
)
893 cxcount
= state
.pagecount
;
895 cxcount
= fz_mini (state
.pagecount
, 1);
899 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
900 "Root/Pages/MediaBox");
901 pdf_to_rect (ctx
, obj
, &rootmediabox
);
904 #ifdef CACHE_PAGEREFS
905 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
906 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
907 if (!state
.pdflut
.objs
) {
908 err (1, "malloc pageobjs %zu %d %zu failed",
909 sizeof (*state
.pdflut
.objs
), cxcount
,
910 sizeof (*state
.pdflut
.objs
) * cxcount
);
912 state
.pdflut
.count
= cxcount
;
913 pdf_load_page_objs (pdf
);
914 state
.pdflut
.pdf
= pdf
;
918 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
921 fz_rect mediabox
= fz_empty_rect
;
925 pdf_obj
*pageref
, *pageobj
;
927 #ifdef CACHE_PAGEREFS
928 pageref
= state
.pdflut
.objs
[pageno
];
930 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
932 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
933 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
935 if (state
.trimmargins
) {
940 page
= pdf_load_page (ctx
, pdf
, pageno
);
941 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
942 trim
= state
.trimanew
|| !obj
;
946 fz_matrix ctm
, page_ctm
;
948 dev
= fz_new_bbox_device (ctx
, &rect
);
949 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
950 fz_invert_matrix (&ctm
, &page_ctm
);
951 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
952 fz_close_device (ctx
, dev
);
953 fz_drop_device (ctx
, dev
);
955 rect
.x0
+= state
.trimfuzz
.x0
;
956 rect
.x1
+= state
.trimfuzz
.x1
;
957 rect
.y0
+= state
.trimfuzz
.y0
;
958 rect
.y1
+= state
.trimfuzz
.y1
;
959 fz_transform_rect (&rect
, &ctm
);
960 fz_intersect_rect (&rect
, &mediabox
);
962 if (!fz_is_empty_rect (&rect
)) {
966 obj
= pdf_new_array (ctx
, pdf
, 4);
967 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
969 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
971 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
973 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
975 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
978 mediabox
.x0
= pdf_to_real (ctx
,
979 pdf_array_get (ctx
, obj
, 0));
980 mediabox
.y0
= pdf_to_real (ctx
,
981 pdf_array_get (ctx
, obj
, 1));
982 mediabox
.x1
= pdf_to_real (ctx
,
983 pdf_array_get (ctx
, obj
, 2));
984 mediabox
.y1
= pdf_to_real (ctx
,
985 pdf_array_get (ctx
, obj
, 3));
988 fz_drop_page (ctx
, &page
->super
);
989 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
991 printd ("progress %f Trimming %d",
992 (double) (pageno
+ 1) / state
.pagecount
,
997 printd ("emsg failed to load page %d", pageno
);
1005 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
1007 if (fz_is_empty_rect (&mediabox
)) {
1016 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1018 if (!fz_is_empty_rect (&cropbox
)) {
1023 fz_intersect_rect (&mediabox
, &cropbox
);
1028 if (fz_is_empty_rect (&rootmediabox
)) {
1029 printd ("emsg cannot find page size for page %d",
1033 mediabox
= rootmediabox
;
1040 if (state
.trimmargins
&& trimw
) {
1044 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1045 fz_bound_page (ctx
, page
, &mediabox
);
1046 if (state
.trimmargins
) {
1050 dev
= fz_new_bbox_device (ctx
, &rect
);
1051 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1052 fz_close_device (ctx
, dev
);
1053 fz_drop_device (ctx
, dev
);
1055 rect
.x0
+= state
.trimfuzz
.x0
;
1056 rect
.x1
+= state
.trimfuzz
.x1
;
1057 rect
.y0
+= state
.trimfuzz
.y0
;
1058 rect
.y1
+= state
.trimfuzz
.y1
;
1059 fz_intersect_rect (&rect
, &mediabox
);
1061 if (!fz_is_empty_rect (&rect
)) {
1065 fz_drop_page (ctx
, page
);
1070 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1072 err (1, "fwrite trim mediabox");
1078 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1080 err (1, "fread trim mediabox %d", pageno
);
1086 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1087 fz_bound_page (ctx
, page
, &mediabox
);
1088 fz_drop_page (ctx
, page
);
1090 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1092 printd ("progress %f Gathering dimensions %d",
1093 (double) (pageno
) / state
.pagecount
,
1098 printd ("emsg failed to load page %d", pageno
);
1104 if (state
.pagedimcount
== 0
1105 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
1106 , p
->rotate
!= rotate
)
1107 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1110 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1111 state
.pagedims
= realloc (state
.pagedims
, size
);
1112 if (!state
.pagedims
) {
1113 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1114 size
, state
.pagedimcount
+ 1);
1117 p
= &state
.pagedims
[state
.pagedimcount
++];
1119 p
->mediabox
= mediabox
;
1124 printd ("progress 1 %s %d pages in %f seconds",
1125 state
.trimmargins
? "Trimmed" : "Processed",
1126 state
.pagecount
, end
- start
);
1129 if (fclose (trimf
)) {
1135 static void layout (void)
1140 struct pagedim
*p
= NULL
;
1141 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
1143 if (state
.pagedimcount
== 0) return;
1145 switch (state
.fitmodel
) {
1146 case FitProportional
:
1147 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1150 p
= &state
.pagedims
[pindex
];
1151 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1153 fz_transform_rect (&box
, &rm
);
1155 x0
= fz_min (box
.x0
, box
.x1
);
1156 x1
= fz_max (box
.x0
, box
.x1
);
1159 maxw
= fz_max (w
, maxw
);
1160 zoom
= state
.w
/ maxw
;
1172 ARSERT (0 && state
.fitmodel
);
1175 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1179 p
= &state
.pagedims
[pindex
];
1180 fz_rotate (&ctm
, state
.rotate
);
1181 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1183 fz_transform_rect (&box
, &rm
);
1184 w
= box
.x1
- box
.x0
;
1185 switch (state
.fitmodel
) {
1186 case FitProportional
:
1187 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
1193 h
= box
.y1
- box
.y0
;
1195 zoom
= fz_min (zw
, zh
);
1196 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
1205 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1206 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1208 fz_rotate (&rm
, p
->rotate
);
1209 p
->pagebox
= p
->mediabox
;
1210 fz_transform_rect (&p
->pagebox
, &rm
);
1211 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1212 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1216 fz_transform_rect (&rect
, &ctm
);
1217 fz_round_rect (&p
->bounds
, &rect
);
1220 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1221 fz_scale (&sm
, zoom
, -zoom
);
1222 fz_concat (&ctm
, &tm
, &sm
);
1228 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1229 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1230 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1231 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1232 int boundw
= x1
- x0
;
1233 int boundh
= y1
- y0
;
1235 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1236 } while (p
-- != state
.pagedims
);
1239 struct pagedim
*pdimofpageno (int pageno
)
1241 struct pagedim
*pdim
= state
.pagedims
;
1243 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1244 if (state
.pagedims
[i
].pageno
> pageno
)
1246 pdim
= &state
.pagedims
[i
];
1251 static void recurse_outline (fz_outline
*outline
, int level
)
1254 if (outline
->page
>= 0) {
1255 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
1256 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
1257 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1258 fz_transform_point (&p
, &pdim
->ctm
);
1259 printd ("o %d %d %d %d %s",
1260 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
1263 printd ("on %d %s", level
, outline
->title
);
1265 if (outline
->down
) {
1266 recurse_outline (outline
->down
, level
+ 1);
1268 outline
= outline
->next
;
1272 static void process_outline (void)
1274 fz_outline
*outline
;
1276 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1278 state
.needoutline
= 0;
1279 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1281 recurse_outline (outline
, 0);
1282 fz_drop_outline (state
.ctx
, outline
);
1286 static char *strofline (fz_stext_line
*line
)
1291 size_t size
= 0, cap
= 80;
1293 p
= malloc (cap
+ 1);
1294 if (!p
) return NULL
;
1296 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1297 int n
= fz_runetochar (utf8
, ch
->c
);
1298 if (size
+ n
> cap
) {
1300 p
= realloc (p
, cap
+ 1);
1301 if (!p
) return NULL
;
1304 memcpy (p
+ size
, utf8
, n
);
1311 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1312 int stop
, int pageno
, double start
)
1318 p
= strofline (line
);
1321 ret
= regexec (re
, p
, 1, &rm
, 0);
1324 if (ret
!= REG_NOMATCH
) {
1327 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1328 printd ("msg regexec error `%.*s'",
1329 (int) size
, errbuf
);
1335 fz_point p1
, p2
, p3
, p4
;
1336 fz_rect s
= {0,0,0,0}, e
;
1340 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1341 o
+= fz_runelen (ch
->c
);
1347 for (;ch
; ch
= ch
->next
) {
1348 o
+= fz_runelen (ch
->c
);
1349 if (o
> rm
.rm_eo
) break;
1362 #pragma GCC diagnostic ignored "-Wdouble-promotion"
1364 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1371 printd ("progress 1 found at %d `%.*s' in %f sec",
1372 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1376 printd ("match %d %d %f %f %f %f %f %f %f %f",
1383 #pragma GCC diagnostic error "-Wdouble-promotion"
1389 /* wishful thinking function */
1390 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1393 fz_stext_page
*text
;
1394 struct pagedim
*pdim
;
1395 int stop
= 0, niters
= 0;
1398 fz_stext_block
*block
;
1401 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1402 if (niters
++ == 5) {
1405 printd ("progress 1 attention requested aborting search at %d",
1410 printd ("progress %f searching in page %d",
1411 (double) (pageno
+ 1) / state
.pagecount
,
1415 pdim
= pdimofpageno (pageno
);
1416 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1417 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1419 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1421 fz_matrix ctm
= pagectm1 (page
, pdim
);
1422 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1425 fz_close_device (state
.ctx
, tdev
);
1426 fz_drop_device (state
.ctx
, tdev
);
1429 for (block
= text
->first_block
; block
; block
= block
->next
) {
1430 fz_stext_line
*line
;
1432 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1433 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1434 if (line
->bbox
.y0
< y
+ 1) continue;
1436 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1438 case 1: stop
= 1; break;
1439 case -1: stop
= 1; goto endloop
;
1445 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1446 fz_stext_line
*line
;
1448 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1449 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1450 if (line
->bbox
.y0
< y
+ 1) continue;
1452 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1454 case 1: stop
= 1; break;
1455 case -1: stop
= 1; goto endloop
;
1470 fz_drop_stext_page (state
.ctx
, text
);
1471 fz_drop_page (state
.ctx
, page
);
1475 printd ("progress 1 no matches %f sec", end
- start
);
1477 printd ("clearrects");
1480 static void set_tex_params (int colorspace
)
1482 switch (colorspace
) {
1484 state
.texiform
= GL_RGBA8
;
1485 state
.texform
= GL_RGBA
;
1486 state
.texty
= GL_UNSIGNED_BYTE
;
1487 state
.colorspace
= fz_device_rgb (state
.ctx
);
1490 state
.texiform
= GL_RGBA8
;
1491 state
.texform
= GL_BGRA
;
1492 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1493 state
.texty
= GL_UNSIGNED_INT_8_8_8_8_REV
;
1495 state
.texty
= GL_UNSIGNED_INT_8_8_8_8
;
1497 state
.colorspace
= fz_device_bgr (state
.ctx
);
1500 state
.texiform
= GL_LUMINANCE_ALPHA
;
1501 state
.texform
= GL_LUMINANCE_ALPHA
;
1502 state
.texty
= GL_UNSIGNED_BYTE
;
1503 state
.colorspace
= fz_device_gray (state
.ctx
);
1506 errx (1, "invalid colorspce %d", colorspace
);
1510 static void realloctexts (int texcount
)
1514 if (texcount
== state
.texcount
) return;
1516 if (texcount
< state
.texcount
) {
1517 glDeleteTextures (state
.texcount
- texcount
,
1518 state
.texids
+ texcount
);
1521 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1522 state
.texids
= realloc (state
.texids
, size
);
1523 if (!state
.texids
) {
1524 err (1, "realloc texs %" FMT_s
, size
);
1527 state
.texowners
= (void *) (state
.texids
+ texcount
);
1528 if (texcount
> state
.texcount
) {
1529 glGenTextures (texcount
- state
.texcount
,
1530 state
.texids
+ state
.texcount
);
1531 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1532 state
.texowners
[i
].w
= -1;
1533 state
.texowners
[i
].slice
= NULL
;
1536 state
.texcount
= texcount
;
1540 static char *mbtoutf8 (char *s
)
1550 len
= mbstowcs (NULL
, s
, strlen (s
));
1555 if (len
== (size_t) -1) {
1556 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1561 tmp
= calloc (len
, sizeof (wchar_t));
1563 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1564 len
, sizeof (wchar_t), errno
, strerror (errno
));
1568 ret
= mbstowcs (tmp
, s
, len
);
1569 if (ret
== (size_t) -1) {
1570 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1571 len
, errno
, strerror (errno
));
1577 for (i
= 0; i
< ret
; ++i
) {
1578 len
+= fz_runelen (tmp
[i
]);
1581 p
= r
= malloc (len
+ 1);
1583 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1588 for (i
= 0; i
< ret
; ++i
) {
1589 p
+= fz_runetochar (p
, tmp
[i
]);
1596 CAMLprim value
ml_mbtoutf8 (value s_v
)
1602 s
= String_val (s_v
);
1608 ret_v
= caml_copy_string (r
);
1614 static void * mainloop (void UNUSED_ATTR
*unused
)
1617 int len
, ret
, oldlen
= 0;
1622 len
= readlen (state
.csock
);
1624 errx (1, "readlen returned 0");
1627 if (oldlen
< len
+ 1) {
1628 p
= realloc (p
, len
+ 1);
1630 err (1, "realloc %d failed", len
+ 1);
1634 readdata (state
.csock
, p
, len
);
1637 if (!strncmp ("open", p
, 4)) {
1638 int off
, usedoccss
, ok
= 0, layouth
;
1645 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1647 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1650 filename
= p
+ 5 + off
;
1651 filenamelen
= strlen (filename
);
1652 password
= filename
+ filenamelen
+ 1;
1654 if (password
[strlen (password
) + 1]) {
1655 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1659 fz_set_use_document_css (state
.ctx
, usedoccss
);
1660 fz_try (state
.ctx
) {
1661 ok
= openxref (filename
, password
, layouth
);
1663 fz_catch (state
.ctx
) {
1664 utf8filename
= mbtoutf8 (filename
);
1665 printd ("msg Could not open %s", utf8filename
);
1666 if (utf8filename
!= filename
) {
1667 free (utf8filename
);
1677 utf8filename
= mbtoutf8 (filename
);
1678 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1679 if (utf8filename
!= filename
) {
1680 free (utf8filename
);
1682 state
.needoutline
= 1;
1685 else if (!strncmp ("cs", p
, 2)) {
1688 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1690 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1693 set_tex_params (colorspace
);
1694 for (i
= 0; i
< state
.texcount
; ++i
) {
1695 state
.texowners
[i
].w
= -1;
1696 state
.texowners
[i
].slice
= NULL
;
1700 else if (!strncmp ("freepage", p
, 8)) {
1703 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1705 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1709 unlock ("freepage");
1711 else if (!strncmp ("freetile", p
, 8)) {
1714 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1716 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1720 unlock ("freetile");
1722 else if (!strncmp ("search", p
, 6)) {
1723 int icase
, pageno
, y
, len2
, forward
;
1727 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1728 &icase
, &pageno
, &y
, &forward
, &len2
);
1730 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1733 pattern
= p
+ 6 + len2
;
1734 ret
= regcomp (&re
, pattern
,
1735 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1740 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1741 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1744 search (&re
, pageno
, y
, forward
);
1748 else if (!strncmp ("geometry", p
, 8)) {
1752 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1754 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1761 for (int i
= 0; i
< state
.texcount
; ++i
) {
1762 state
.texowners
[i
].slice
= NULL
;
1765 state
.fitmodel
= fitmodel
;
1770 unlock ("geometry");
1771 printd ("continue %d", state
.pagecount
);
1773 else if (!strncmp ("reqlayout", p
, 9)) {
1780 ret
= sscanf (p
+ 9, " %d %d %d %n",
1781 &rotate
, &fitmodel
, &h
, &off
);
1783 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1786 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1787 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1790 state
.rotate
= rotate
;
1791 state
.fitmodel
= fitmodel
;
1796 nameddest
= p
+ 9 + off
;
1797 if (pdf
&& nameddest
&& *nameddest
) {
1799 struct pagedim
*pdim
;
1800 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1802 pdim
= pdimofpageno (pageno
);
1803 fz_transform_point (&xy
, &pdim
->ctm
);
1804 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1808 unlock ("reqlayout");
1809 printd ("continue %d", state
.pagecount
);
1811 else if (!strncmp ("page", p
, 4)) {
1816 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1818 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1823 page
= loadpage (pageno
, pindex
);
1827 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1829 else if (!strncmp ("tile", p
, 4)) {
1836 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1837 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1838 SCN_ptr_cast (&data
));
1840 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1845 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1849 printd ("tile %d %d %" FMT_ptr
" %u %f",
1851 FMT_ptr_cast (tile
),
1852 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1855 else if (!strncmp ("trimset", p
, 7)) {
1859 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1860 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1862 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1865 state
.trimmargins
= trimmargins
;
1866 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1868 state
.trimfuzz
= fuzz
;
1872 else if (!strncmp ("settrim", p
, 7)) {
1876 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1877 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1879 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1883 state
.trimmargins
= trimmargins
;
1884 state
.needoutline
= 1;
1885 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1887 state
.trimfuzz
= fuzz
;
1889 state
.pagedimcount
= 0;
1890 free (state
.pagedims
);
1891 state
.pagedims
= NULL
;
1896 printd ("continue %d", state
.pagecount
);
1898 else if (!strncmp ("sliceh", p
, 6)) {
1901 ret
= sscanf (p
+ 6, " %d", &h
);
1903 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1905 if (h
!= state
.sliceheight
) {
1906 state
.sliceheight
= h
;
1907 for (int i
= 0; i
< state
.texcount
; ++i
) {
1908 state
.texowners
[i
].w
= -1;
1909 state
.texowners
[i
].h
= -1;
1910 state
.texowners
[i
].slice
= NULL
;
1914 else if (!strncmp ("interrupt", p
, 9)) {
1915 printd ("vmsg interrupted");
1918 errx (1, "unknown command %.*s", len
, p
);
1924 CAMLprim value
ml_isexternallink (value uri_v
)
1927 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1928 CAMLreturn (Val_bool (ext
));
1931 CAMLprim value
ml_uritolocation (value uri_v
)
1937 struct pagedim
*pdim
;
1939 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1941 pdim
= pdimofpageno (pageno
);
1942 fz_transform_point (&xy
, &pdim
->ctm
);
1943 ret_v
= caml_alloc_tuple (3);
1944 Field (ret_v
, 0) = Val_int (pageno
);
1945 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1946 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1950 CAMLprim value
ml_realloctexts (value texcount_v
)
1952 CAMLparam1 (texcount_v
);
1955 if (trylock (__func__
)) {
1959 realloctexts (Int_val (texcount_v
));
1964 CAMLreturn (Val_bool (ok
));
1967 static void recti (int x0
, int y0
, int x1
, int y1
)
1969 GLfloat
*v
= state
.vertices
;
1971 glVertexPointer (2, GL_FLOAT
, 0, v
);
1972 v
[0] = x0
; v
[1] = y0
;
1973 v
[2] = x1
; v
[3] = y0
;
1974 v
[4] = x0
; v
[5] = y1
;
1975 v
[6] = x1
; v
[7] = y1
;
1976 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1979 static void showsel (struct page
*page
, int ox
, int oy
)
1983 fz_stext_block
*block
;
1985 unsigned char selcolor
[] = {15,15,15,140};
1987 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) return;
1989 glEnable (GL_BLEND
);
1990 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1991 glColor4ubv (selcolor
);
1993 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1994 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1996 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1997 fz_stext_line
*line
;
1999 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2000 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2003 rect
= fz_empty_rect
;
2004 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2005 if (ch
== page
->fmark
.ch
) seen
= 1;
2006 if (seen
) fz_union_rect (&rect
, &ch
->bbox
);
2007 if (ch
== page
->lmark
.ch
) {
2008 fz_round_rect (&bbox
, &rect
);
2009 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2010 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2014 fz_round_rect (&bbox
, &rect
);
2015 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2016 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2020 glDisable (GL_BLEND
);
2023 #pragma GCC diagnostic push
2024 #pragma GCC diagnostic ignored "-Wdouble-promotion"
2025 #pragma GCC diagnostic ignored "-Wconversion"
2027 #pragma GCC diagnostic pop
2029 static void stipplerect (fz_matrix
*m
,
2037 fz_transform_point (p1
, m
);
2038 fz_transform_point (p2
, m
);
2039 fz_transform_point (p3
, m
);
2040 fz_transform_point (p4
, m
);
2046 t
= hypotf (w
, h
) * .25f
;
2050 s
= hypotf (w
, h
) * .25f
;
2052 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2053 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2055 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2056 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2058 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2059 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2061 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2062 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2064 glDrawArrays (GL_LINES
, 0, 8);
2067 static void solidrect (fz_matrix
*m
,
2074 fz_transform_point (p1
, m
);
2075 fz_transform_point (p2
, m
);
2076 fz_transform_point (p3
, m
);
2077 fz_transform_point (p4
, m
);
2078 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2079 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2081 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2082 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2083 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2086 static void ensurelinks (struct page
*page
)
2089 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
2092 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2094 fz_matrix ctm
, tm
, pm
;
2096 GLfloat
*texcoords
= state
.texcoords
;
2097 GLfloat
*vertices
= state
.vertices
;
2101 glEnable (GL_TEXTURE_1D
);
2102 glEnable (GL_BLEND
);
2103 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2104 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2106 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2107 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2108 fz_translate (&tm
, xoff
, yoff
);
2109 pm
= pagectm (page
);
2110 fz_concat (&ctm
, &pm
, &tm
);
2112 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2113 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2115 for (link
= page
->links
; link
; link
= link
->next
) {
2116 fz_point p1
, p2
, p3
, p4
;
2118 p1
.x
= link
->rect
.x0
;
2119 p1
.y
= link
->rect
.y0
;
2121 p2
.x
= link
->rect
.x1
;
2122 p2
.y
= link
->rect
.y0
;
2124 p3
.x
= link
->rect
.x1
;
2125 p3
.y
= link
->rect
.y1
;
2127 p4
.x
= link
->rect
.x0
;
2128 p4
.y
= link
->rect
.y1
;
2130 /* TODO: different colours for different schemes */
2131 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
2132 else glColor3ub (255, 0, 0);
2134 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2137 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2138 fz_point p1
, p2
, p3
, p4
;
2139 struct annot
*annot
= &page
->annots
[i
];
2141 p1
.x
= annot
->bbox
.x0
;
2142 p1
.y
= annot
->bbox
.y0
;
2144 p2
.x
= annot
->bbox
.x1
;
2145 p2
.y
= annot
->bbox
.y0
;
2147 p3
.x
= annot
->bbox
.x1
;
2148 p3
.y
= annot
->bbox
.y1
;
2150 p4
.x
= annot
->bbox
.x0
;
2151 p4
.y
= annot
->bbox
.y1
;
2153 glColor3ub (0, 0, 128);
2154 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2157 glDisable (GL_BLEND
);
2158 glDisable (GL_TEXTURE_1D
);
2161 static int compareslinks (const void *l
, const void *r
)
2163 struct slink
const *ls
= l
;
2164 struct slink
const *rs
= r
;
2165 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2166 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2168 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2171 static void droptext (struct page
*page
)
2174 fz_drop_stext_page (state
.ctx
, page
->text
);
2175 page
->fmark
.ch
= NULL
;
2176 page
->lmark
.ch
= NULL
;
2181 static void dropannots (struct page
*page
)
2184 free (page
->annots
);
2185 page
->annots
= NULL
;
2186 page
->annotcount
= 0;
2190 static void ensureannots (struct page
*page
)
2193 size_t annotsize
= sizeof (*page
->annots
);
2196 if (state
.gen
!= page
->agen
) {
2198 page
->agen
= state
.gen
;
2200 if (page
->annots
) return;
2202 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2204 annot
= fz_next_annot (state
.ctx
, annot
)) {
2209 page
->annotcount
= count
;
2210 page
->annots
= calloc (count
, annotsize
);
2211 if (!page
->annots
) {
2212 err (1, "calloc annots %d", count
);
2215 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2217 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2220 fz_bound_annot (state
.ctx
, annot
, &rect
);
2221 page
->annots
[i
].annot
= annot
;
2222 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2227 static void dropslinks (struct page
*page
)
2230 free (page
->slinks
);
2231 page
->slinks
= NULL
;
2232 page
->slinkcount
= 0;
2235 fz_drop_link (state
.ctx
, page
->links
);
2240 static void ensureslinks (struct page
*page
)
2244 size_t slinksize
= sizeof (*page
->slinks
);
2247 ensureannots (page
);
2248 if (state
.gen
!= page
->sgen
) {
2250 page
->sgen
= state
.gen
;
2252 if (page
->slinks
) return;
2255 ctm
= pagectm (page
);
2257 count
= page
->annotcount
;
2258 for (link
= page
->links
; link
; link
= link
->next
) {
2264 page
->slinkcount
= count
;
2265 page
->slinks
= calloc (count
, slinksize
);
2266 if (!page
->slinks
) {
2267 err (1, "calloc slinks %d", count
);
2270 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
2274 fz_transform_rect (&rect
, &ctm
);
2275 page
->slinks
[i
].tag
= SLINK
;
2276 page
->slinks
[i
].u
.link
= link
;
2277 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2279 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2281 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2282 fz_transform_rect (&rect
, &ctm
);
2283 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2285 page
->slinks
[i
].tag
= SANNOT
;
2286 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2288 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2292 #pragma GCC diagnostic push
2293 #pragma GCC diagnostic ignored "-Wconversion"
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 */
2308 #pragma GCC diagnostic pop
2310 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2311 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2314 struct slink
*slink
;
2315 float x0
, y0
, x1
, y1
, w
;
2317 ensureslinks (page
);
2318 glColor3ub (0xc3, 0xb0, 0x91);
2319 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2320 fmt_linkn (buf
, i
+ noff
);
2321 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2322 slink
= &page
->slinks
[i
];
2324 x0
= slink
->bbox
.x0
+ xoff
- 5;
2325 y1
= slink
->bbox
.y0
+ yoff
- 5;
2326 y0
= y1
+ 10 + hfsize
;
2327 w
= measure_string (state
.face
, hfsize
, buf
);
2329 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2333 glEnable (GL_BLEND
);
2334 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2335 glEnable (GL_TEXTURE_2D
);
2336 glColor3ub (0, 0, 0);
2337 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2338 fmt_linkn (buf
, i
+ noff
);
2339 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2340 slink
= &page
->slinks
[i
];
2342 x0
= slink
->bbox
.x0
+ xoff
;
2343 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2344 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2347 glDisable (GL_TEXTURE_2D
);
2348 glDisable (GL_BLEND
);
2351 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2354 struct slice
*slice1
;
2355 unsigned char *texdata
;
2358 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2359 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2361 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2362 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2363 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2367 int texindex
= state
.texindex
++ % state
.texcount
;
2369 if (state
.texowners
[texindex
].w
== tile
->w
) {
2370 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2374 state
.texowners
[texindex
].h
= slice
->h
;
2378 state
.texowners
[texindex
].h
= slice
->h
;
2381 state
.texowners
[texindex
].w
= tile
->w
;
2382 state
.texowners
[texindex
].slice
= slice
;
2383 slice
->texindex
= texindex
;
2385 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2386 #if TEXT_TYPE == GL_TEXTURE_2D
2387 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2388 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2389 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2390 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2393 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2397 texdata
= tile
->pixmap
->samples
;
2400 glTexSubImage2D (TEXT_TYPE
,
2412 glTexImage2D (TEXT_TYPE
,
2424 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2429 CAMLprim
void ml_begintiles (value unit_v
)
2431 CAMLparam1 (unit_v
);
2432 glEnable (TEXT_TYPE
);
2433 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2434 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2438 CAMLprim
void ml_endtiles (value unit_v
)
2440 CAMLparam1 (unit_v
);
2441 glDisable (TEXT_TYPE
);
2445 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2447 CAMLparam2 (args_v
, ptr_v
);
2448 int dispx
= Int_val (Field (args_v
, 0));
2449 int dispy
= Int_val (Field (args_v
, 1));
2450 int dispw
= Int_val (Field (args_v
, 2));
2451 int disph
= Int_val (Field (args_v
, 3));
2452 int tilex
= Int_val (Field (args_v
, 4));
2453 int tiley
= Int_val (Field (args_v
, 5));
2454 char *s
= String_val (ptr_v
);
2455 struct tile
*tile
= parse_pointer (__func__
, s
);
2456 int slicey
, firstslice
;
2457 struct slice
*slice
;
2458 GLfloat
*texcoords
= state
.texcoords
;
2459 GLfloat
*vertices
= state
.vertices
;
2461 firstslice
= tiley
/ tile
->sliceheight
;
2462 slice
= &tile
->slices
[firstslice
];
2463 slicey
= tiley
% tile
->sliceheight
;
2468 dh
= slice
->h
- slicey
;
2469 dh
= fz_mini (disph
, dh
);
2470 uploadslice (tile
, slice
);
2472 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2473 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2474 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2475 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2477 vertices
[0] = dispx
; vertices
[1] = dispy
;
2478 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2479 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2480 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2482 #if TEXT_TYPE == GL_TEXTURE_2D
2483 for (int i
= 0; i
< 8; ++i
) {
2484 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2488 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2492 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2498 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2500 fz_matrix ctm
, tm
, pm
;
2501 fz_point p1
, p2
, p3
, p4
;
2502 GLfloat
*vertices
= state
.vertices
;
2503 double *v
= (double *) rects_v
;
2505 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2506 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2507 fz_translate (&tm
, xoff
, yoff
);
2508 pm
= pagectm (page
);
2509 fz_concat (&ctm
, &pm
, &tm
);
2511 glEnable (GL_BLEND
);
2512 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2515 p1
.x
= (float) v
[4];
2516 p1
.y
= (float) v
[5];
2518 p2
.x
= (float) v
[6];
2519 p2
.y
= (float) v
[5];
2521 p3
.x
= (float) v
[6];
2522 p3
.y
= (float) v
[7];
2524 p4
.x
= (float) v
[4];
2525 p4
.y
= (float) v
[7];
2526 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2527 glDisable (GL_BLEND
);
2530 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2531 value xoff_v
, value yoff_v
,
2534 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2535 int xoff
= Int_val (xoff_v
);
2536 int yoff
= Int_val (yoff_v
);
2537 int noff
= Int_val (Field (li_v
, 0));
2538 char *targ
= String_val (Field (li_v
, 1));
2539 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2540 int hfsize
= Int_val (Field (li_v
, 2));
2541 char *s
= String_val (ptr_v
);
2542 int hlmask
= Int_val (hlinks_v
);
2543 struct page
*page
= parse_pointer (__func__
, s
);
2545 if (!page
->fzpage
) {
2546 /* deal with loadpage failed pages */
2550 if (trylock (__func__
)) {
2555 ensureannots (page
);
2556 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2558 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2559 noff
= page
->slinkcount
;
2561 if (page
->tgen
== state
.gen
) {
2562 showsel (page
, xoff
, yoff
);
2567 CAMLreturn (Val_int (noff
));
2570 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2573 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2574 int xoff
= Int_val (xoff_v
);
2575 int yoff
= Int_val (yoff_v
);
2576 char *s
= String_val (ptr_v
);
2577 struct page
*page
= parse_pointer (__func__
, s
);
2579 drawprect (page
, xoff
, yoff
, rects_v
);
2583 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2587 const fz_matrix
*tctm
;
2588 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2590 if (!page
->annots
) return NULL
;
2593 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2594 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2597 tctm
= &fz_identity
;
2603 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2604 fz_invert_matrix (&ctm
, &ctm
);
2605 fz_transform_point (&p
, &ctm
);
2608 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2609 struct annot
*a
= &page
->annots
[i
];
2612 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2613 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2614 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2622 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2628 ensureslinks (page
);
2633 ctm
= pagectm (page
);
2634 fz_invert_matrix (&ctm
, &ctm
);
2635 fz_transform_point (&p
, &ctm
);
2637 for (link
= page
->links
; link
; link
= link
->next
) {
2638 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2639 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2647 static void ensuretext (struct page
*page
)
2649 if (state
.gen
!= page
->tgen
) {
2651 page
->tgen
= state
.gen
;
2657 page
->text
= fz_new_stext_page (state
.ctx
,
2658 &state
.pagedims
[page
->pdimno
].mediabox
);
2659 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2660 ctm
= pagectm (page
);
2661 fz_run_display_list (state
.ctx
, page
->dlist
,
2662 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2663 fz_close_device (state
.ctx
, tdev
);
2664 fz_drop_device (state
.ctx
, tdev
);
2668 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2670 CAMLparam2 (start_page_v
, dir_v
);
2672 int i
, dir
= Int_val (dir_v
);
2673 int start_page
= Int_val (start_page_v
);
2674 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2678 ret_v
= Val_int (0);
2680 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2681 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2686 pdf_page
*page
= NULL
;
2689 fz_try (state
.ctx
) {
2690 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2691 found
= !!page
->links
|| !!page
->annots
;
2693 fz_catch (state
.ctx
) {
2697 fz_drop_page (state
.ctx
, &page
->super
);
2701 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2702 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2704 fz_drop_link (state
.ctx
, link
);
2705 fz_drop_page (state
.ctx
, page
);
2709 ret_v
= caml_alloc_small (1, 1);
2710 Field (ret_v
, 0) = Val_int (i
);
2719 enum { dir_first
, dir_last
};
2720 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2722 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2724 CAMLparam2 (ptr_v
, dir_v
);
2725 CAMLlocal2 (ret_v
, pos_v
);
2727 int dirtag
, i
, slinkindex
;
2728 struct slink
*found
= NULL
,*slink
;
2729 char *s
= String_val (ptr_v
);
2731 page
= parse_pointer (__func__
, s
);
2732 ret_v
= Val_int (0);
2734 ensureslinks (page
);
2736 if (Is_block (dir_v
)) {
2737 dirtag
= Tag_val (dir_v
);
2739 case dir_first_visible
:
2741 int x0
, y0
, dir
, first_index
, last_index
;
2743 pos_v
= Field (dir_v
, 0);
2744 x0
= Int_val (Field (pos_v
, 0));
2745 y0
= Int_val (Field (pos_v
, 1));
2746 dir
= Int_val (Field (pos_v
, 2));
2751 last_index
= page
->slinkcount
;
2754 first_index
= page
->slinkcount
- 1;
2758 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2759 slink
= &page
->slinks
[i
];
2760 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2769 slinkindex
= Int_val (Field (dir_v
, 0));
2770 found
= &page
->slinks
[slinkindex
];
2771 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2772 slink
= &page
->slinks
[i
];
2773 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2781 slinkindex
= Int_val (Field (dir_v
, 0));
2782 found
= &page
->slinks
[slinkindex
];
2783 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2784 slink
= &page
->slinks
[i
];
2785 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2793 slinkindex
= Int_val (Field (dir_v
, 0));
2794 found
= &page
->slinks
[slinkindex
];
2795 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2796 slink
= &page
->slinks
[i
];
2797 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2805 slinkindex
= Int_val (Field (dir_v
, 0));
2806 found
= &page
->slinks
[slinkindex
];
2807 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2808 slink
= &page
->slinks
[i
];
2809 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2818 dirtag
= Int_val (dir_v
);
2821 found
= page
->slinks
;
2826 found
= page
->slinks
+ (page
->slinkcount
- 1);
2832 ret_v
= caml_alloc_small (2, 1);
2833 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2840 enum { uuri
, utext
, uannot
};
2842 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2844 CAMLparam2 (ptr_v
, n_v
);
2845 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2848 char *s
= String_val (ptr_v
);
2849 struct slink
*slink
;
2851 ret_v
= Val_int (0);
2852 page
= parse_pointer (__func__
, s
);
2855 ensureslinks (page
);
2856 slink
= &page
->slinks
[Int_val (n_v
)];
2857 if (slink
->tag
== SLINK
) {
2858 link
= slink
->u
.link
;
2859 str_v
= caml_copy_string (link
->uri
);
2860 ret_v
= caml_alloc_small (1, uuri
);
2861 Field (ret_v
, 0) = str_v
;
2864 ret_v
= caml_alloc_small (1, uannot
);
2865 tup_v
= caml_alloc_tuple (2);
2866 Field (ret_v
, 0) = tup_v
;
2867 Field (tup_v
, 0) = ptr_v
;
2868 Field (tup_v
, 1) = n_v
;
2875 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2877 CAMLparam2 (ptr_v
, n_v
);
2880 char *contents
= NULL
;
2883 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2885 char *s
= String_val (ptr_v
);
2887 struct slink
*slink
;
2889 page
= parse_pointer (__func__
, s
);
2890 slink
= &page
->slinks
[Int_val (n_v
)];
2891 contents
= pdf_copy_annot_contents (state
.ctx
,
2892 (pdf_annot
*) slink
->u
.annot
);
2896 ret_v
= caml_copy_string (contents
);
2897 fz_free (state
.ctx
, contents
);
2900 ret_v
= caml_copy_string ("");
2905 CAMLprim value
ml_getlinkcount (value ptr_v
)
2909 char *s
= String_val (ptr_v
);
2911 page
= parse_pointer (__func__
, s
);
2912 CAMLreturn (Val_int (page
->slinkcount
));
2915 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2917 CAMLparam2 (ptr_v
, n_v
);
2920 struct slink
*slink
;
2921 char *s
= String_val (ptr_v
);
2923 page
= parse_pointer (__func__
, s
);
2924 ret_v
= caml_alloc_tuple (4);
2926 ensureslinks (page
);
2928 slink
= &page
->slinks
[Int_val (n_v
)];
2929 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2930 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2931 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2932 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2937 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2939 CAMLparam3 (ptr_v
, x_v
, y_v
);
2940 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2942 struct annot
*annot
;
2944 char *ptr
= String_val (ptr_v
);
2945 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2946 struct pagedim
*pdim
;
2948 ret_v
= Val_int (0);
2949 if (trylock (__func__
)) {
2953 page
= parse_pointer (__func__
, ptr
);
2954 pdim
= &state
.pagedims
[page
->pdimno
];
2955 x
+= pdim
->bounds
.x0
;
2956 y
+= pdim
->bounds
.y0
;
2959 annot
= getannot (page
, x
, y
);
2963 ensureslinks (page
);
2964 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2965 if (page
->slinks
[i
].tag
== SANNOT
2966 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2971 ret_v
= caml_alloc_small (1, uannot
);
2972 tup_v
= caml_alloc_tuple (2);
2973 Field (ret_v
, 0) = tup_v
;
2974 Field (tup_v
, 0) = ptr_v
;
2975 Field (tup_v
, 1) = Val_int (n
);
2980 link
= getlink (page
, x
, y
);
2982 str_v
= caml_copy_string (link
->uri
);
2983 ret_v
= caml_alloc_small (1, uuri
);
2984 Field (ret_v
, 0) = str_v
;
2988 fz_stext_block
*block
;
2992 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2993 fz_stext_line
*line
;
2995 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2997 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3000 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3004 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3007 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3010 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3011 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
3012 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3015 if (!n2
) n2
= "<unknown font>";
3017 if (face
&& face
->family_name
) {
3019 char *n1
= face
->family_name
;
3020 size_t l1
= strlen (n1
);
3021 size_t l2
= strlen (n2
);
3023 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3024 s
= malloc (l1
+ l2
+ 2);
3028 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3029 str_v
= caml_copy_string (s
);
3034 if (str_v
== Val_unit
) {
3035 str_v
= caml_copy_string (n2
);
3037 ret_v
= caml_alloc_small (1, utext
);
3038 Field (ret_v
, 0) = str_v
;
3052 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3054 static int uninteresting (int c
)
3056 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3060 CAMLprim
void ml_clearmark (value ptr_v
)
3063 char *s
= String_val (ptr_v
);
3066 if (trylock (__func__
)) {
3070 page
= parse_pointer (__func__
, s
);
3071 page
->fmark
.ch
= NULL
;
3072 page
->lmark
.ch
= NULL
;
3079 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3081 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3085 fz_stext_line
*line
;
3086 fz_stext_block
*block
;
3087 struct pagedim
*pdim
;
3088 int mark
= Int_val (mark_v
);
3089 char *s
= String_val (ptr_v
);
3090 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3092 ret_v
= Val_bool (0);
3093 if (trylock (__func__
)) {
3097 page
= parse_pointer (__func__
, s
);
3098 pdim
= &state
.pagedims
[page
->pdimno
];
3102 if (mark
== mark_page
) {
3103 page
->fmark
.ch
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
3104 page
->lmark
.ch
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
3105 ret_v
= Val_bool (1);
3109 x
+= pdim
->bounds
.x0
;
3110 y
+= pdim
->bounds
.y0
;
3112 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3113 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3115 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3118 if (mark
== mark_block
) {
3119 page
->fmark
.ch
= block
->u
.t
.first_line
->first_char
;
3120 page
->lmark
.ch
= block
->u
.t
.last_line
->last_char
;
3121 ret_v
= Val_bool (1);
3125 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3129 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3132 if (mark
== mark_line
) {
3133 page
->fmark
.ch
= line
->first_char
;
3134 page
->lmark
.ch
= line
->last_char
;
3135 ret_v
= Val_bool (1);
3139 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3140 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
3142 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3143 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
3144 if (uninteresting (ch2
->c
)) first
= NULL
;
3145 else if (!first
) first
= ch2
;
3147 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
3148 if (uninteresting (ch2
->c
)) break;
3152 page
->fmark
.ch
= first
;
3153 page
->lmark
.ch
= last
;
3154 ret_v
= Val_bool (1);
3161 if (!Bool_val (ret_v
)) {
3162 page
->fmark
.ch
= NULL
;
3163 page
->lmark
.ch
= NULL
;
3171 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3173 CAMLparam3 (ptr_v
, x_v
, y_v
);
3174 CAMLlocal2 (ret_v
, res_v
);
3177 struct pagedim
*pdim
;
3178 fz_stext_block
*block
;
3179 char *s
= String_val (ptr_v
);
3180 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3182 ret_v
= Val_int (0);
3183 if (trylock (__func__
)) {
3187 page
= parse_pointer (__func__
, s
);
3188 pdim
= &state
.pagedims
[page
->pdimno
];
3189 x
+= pdim
->bounds
.x0
;
3190 y
+= pdim
->bounds
.y0
;
3194 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3195 switch (block
->type
) {
3196 case FZ_STEXT_BLOCK_TEXT
:
3200 case FZ_STEXT_BLOCK_IMAGE
:
3208 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3213 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3214 ret_v
= caml_alloc_small (1, 1);
3215 Store_double_field (res_v
, 0, (double) b
->x0
);
3216 Store_double_field (res_v
, 1, (double) b
->x1
);
3217 Store_double_field (res_v
, 2, (double) b
->y0
);
3218 Store_double_field (res_v
, 3, (double) b
->y1
);
3219 Field (ret_v
, 0) = res_v
;
3227 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3229 CAMLparam2 (ptr_v
, rect_v
);
3232 struct pagedim
*pdim
;
3233 char *s
= String_val (ptr_v
);
3236 fz_stext_line
*line
;
3237 fz_stext_block
*block
;
3238 fz_stext_char
*fc
, *lc
;
3240 if (trylock (__func__
)) {
3244 page
= parse_pointer (__func__
, s
);
3247 pdim
= &state
.pagedims
[page
->pdimno
];
3248 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3249 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3250 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3251 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3261 fc
= page
->fmark
.ch
;
3262 lc
= page
->lmark
.ch
;
3264 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3265 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3266 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3267 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3269 if (x0
>= b
.x0
&& x0
<= b
.x1
&& y0
>= b
.y0
&& y0
<= b
.y1
) {
3272 if (x1
>= b
.x0
&& x1
<= b
.x1
&& y1
>= b
.y0
&& y1
<= b
.y1
) {
3278 if (x1
< x0
&& fc
== lc
) {
3286 page
->fmark
.ch
= fc
;
3287 page
->lmark
.ch
= lc
;
3295 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3301 len
= fz_runetochar (buf
, ch
->c
);
3302 ret
= fwrite (buf
, len
, 1, f
);
3304 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3305 len
, ret
, errno
, strerror (errno
));
3311 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3313 CAMLparam2 (command_v
, fds_v
);
3314 CAMLlocal2 (l_v
, tup_v
);
3316 pid_t pid
= (pid_t
) -1;
3318 value earg_v
= Nothing
;
3319 posix_spawnattr_t attr
;
3320 posix_spawn_file_actions_t fa
;
3321 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3323 argv
[2] = String_val (command_v
);
3325 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3326 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3329 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3330 msg
= "posix_spawnattr_init";
3334 #ifdef POSIX_SPAWN_USEVFORK
3335 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3336 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3341 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3344 tup_v
= Field (l_v
, 0);
3345 fd1
= Int_val (Field (tup_v
, 0));
3346 fd2
= Int_val (Field (tup_v
, 1));
3348 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3349 msg
= "posix_spawn_file_actions_addclose";
3355 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3356 msg
= "posix_spawn_file_actions_adddup2";
3363 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3364 msg
= "posix_spawn";
3369 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3370 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3374 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3375 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3376 ret1
, strerror (ret1
));
3380 unix_error (ret
, msg
, earg_v
);
3382 CAMLreturn (Val_int (pid
));
3385 CAMLprim value
ml_hassel (value ptr_v
)
3390 char *s
= String_val (ptr_v
);
3392 ret_v
= Val_bool (0);
3393 if (trylock (__func__
)) {
3397 page
= parse_pointer (__func__
, s
);
3398 ret_v
= Val_bool (page
->fmark
.ch
&& page
->lmark
.ch
);
3404 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3406 CAMLparam2 (fd_v
, ptr_v
);
3410 fz_stext_line
*line
;
3411 fz_stext_block
*block
;
3412 int fd
= Int_val (fd_v
);
3413 char *s
= String_val (ptr_v
);
3415 if (trylock (__func__
)) {
3419 page
= parse_pointer (__func__
, s
);
3421 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) {
3422 printd ("emsg nothing to copy on page %d", page
->pageno
);
3426 f
= fdopen (fd
, "w");
3428 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3429 fd
, errno
, strerror (errno
));
3433 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3434 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3435 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3437 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3438 if (seen
|| ch
== page
->fmark
.ch
) {
3441 if (ch
== page
->lmark
.ch
) goto close
;
3442 } while ((ch
= ch
->next
));
3447 if (seen
) fputc ('\n', f
);
3452 int ret
= fclose (f
);
3455 if (errno
!= ECHILD
) {
3456 printd ("emsg failed to close sel pipe: %d:%s",
3457 errno
, strerror (errno
));
3467 printd ("emsg failed to close sel pipe: %d:%s",
3468 errno
, strerror (errno
));
3474 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3476 CAMLparam1 (pagedimno_v
);
3478 int pagedimno
= Int_val (pagedimno_v
);
3481 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3482 if (trylock (__func__
)) {
3483 box
= fz_empty_rect
;
3486 box
= state
.pagedims
[pagedimno
].mediabox
;
3490 Store_double_field (ret_v
, 0, (double) box
.x0
);
3491 Store_double_field (ret_v
, 1, (double) box
.x1
);
3492 Store_double_field (ret_v
, 2, (double) box
.y0
);
3493 Store_double_field (ret_v
, 3, (double) box
.y1
);
3498 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3499 value dw_v
, value cols_v
)
3501 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3507 float winw
= Int_val (winw_v
);
3508 float winh
= Int_val (winh_v
);
3509 float dw
= Int_val (dw_v
);
3510 float cols
= Int_val (cols_v
);
3511 float pw
= 1.0, ph
= 1.0;
3513 if (trylock (__func__
)) {
3517 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3518 float w
= p
->pagebox
.x1
/ cols
;
3519 float h
= p
->pagebox
.y1
;
3523 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3525 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3528 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3531 ret_v
= caml_copy_double ((double) zoom
);
3535 CAMLprim value
ml_getmaxw (value unit_v
)
3537 CAMLparam1 (unit_v
);
3543 if (trylock (__func__
)) {
3547 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3548 float w
= p
->pagebox
.x1
;
3549 maxw
= fz_max (maxw
, w
);
3554 ret_v
= caml_copy_double ((double) maxw
);
3558 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3560 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3562 int pt
= Int_val(pt_v
);
3563 int x
= Int_val (x_v
);
3564 int y
= Int_val (y_v
);
3567 w
= (double) draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3568 ret_v
= caml_copy_double (w
);
3572 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3574 CAMLparam2 (pt_v
, string_v
);
3576 int pt
= Int_val (pt_v
);
3579 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3580 ret_v
= caml_copy_double (w
);
3584 CAMLprim value
ml_getpagebox (value opaque_v
)
3586 CAMLparam1 (opaque_v
);
3592 char *s
= String_val (opaque_v
);
3593 struct page
*page
= parse_pointer (__func__
, s
);
3595 ret_v
= caml_alloc_tuple (4);
3596 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3598 ctm
= pagectm (page
);
3599 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3601 fz_close_device (state
.ctx
, dev
);
3602 fz_drop_device (state
.ctx
, dev
);
3603 fz_round_rect (&bbox
, &rect
);
3604 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3605 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3606 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3607 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3612 CAMLprim
void ml_setaalevel (value level_v
)
3614 CAMLparam1 (level_v
);
3616 state
.aalevel
= Int_val (level_v
);
3621 #pragma GCC diagnostic push
3622 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3623 #include <X11/Xlib.h>
3624 #include <X11/cursorfont.h>
3625 #pragma GCC diagnostic pop
3628 #include <EGL/egl.h>
3633 static const int shapes
[] = {
3634 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3637 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3650 XVisualInfo
*visual
;
3651 Cursor curs
[CURS_COUNT
];
3655 static void initcurs (void)
3657 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3658 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3663 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3665 CAMLparam3 (display_v
, wid_v
, screen_v
);
3669 EGLint attribs
[] = {
3670 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3671 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3676 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3678 caml_failwith ("XOpenDisplay");
3681 eglBindAPI (EGL_OPENGL_API
);
3683 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3684 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3685 caml_failwith ("eglGetDisplay");
3688 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3689 caml_failwith ("eglInitialize");
3692 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3694 caml_failwith ("eglChooseConfig");
3697 if (!eglGetConfigAttrib (glx
.edpy
, conf
, EGL_NATIVE_VISUAL_ID
, &visid
)) {
3698 caml_failwith ("eglGetConfigAttrib");
3704 glx
.wid
= Int_val (wid_v
);
3705 CAMLreturn (Val_int (visid
));
3708 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3710 CAMLparam1 (unit_v
);
3712 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3714 caml_failwith ("eglCreateContext");
3717 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3719 if (glx
.win
== EGL_NO_SURFACE
) {
3720 caml_failwith ("eglCreateWindowSurface");
3723 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3725 caml_failwith ("eglMakeCurrent");
3730 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3732 CAMLparam3 (display_v
, wid_v
, screen_v
);
3734 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3736 caml_failwith ("XOpenDisplay");
3739 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3740 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3742 XCloseDisplay (glx
.dpy
);
3743 caml_failwith ("glXChooseVisual");
3748 glx
.wid
= Int_val (wid_v
);
3749 CAMLreturn (Val_int (glx
.visual
->visualid
));
3752 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3754 CAMLparam1 (unit_v
);
3756 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3758 caml_failwith ("glXCreateContext");
3764 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3765 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3767 caml_failwith ("glXMakeCurrent");
3773 CAMLprim
void ml_setcursor (value cursor_v
)
3775 CAMLparam1 (cursor_v
);
3776 size_t cursn
= Int_val (cursor_v
);
3778 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
3779 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
3784 CAMLprim
void ml_swapb (value unit_v
)
3786 CAMLparam1 (unit_v
);
3788 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
3789 caml_failwith ("eglSwapBuffers");
3792 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3797 long keysym2ucs (KeySym
);
3798 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3800 CAMLparam1 (keysym_v
);
3802 KeySym keysym
= Int_val (keysym_v
);
3807 rune
= (Rune
) keysym2ucs (keysym
);
3808 len
= fz_runetochar (buf
, rune
);
3810 str_v
= caml_copy_string (buf
);
3814 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3816 CAMLparam1 (keysym_v
);
3818 long ucs_v
= Long_val (keysym_v
);
3822 len
= fz_runetochar (buf
, (int) ucs_v
);
3824 str_v
= caml_copy_string (buf
);
3829 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
};
3831 CAMLprim value
ml_platform (value unit_v
)
3833 CAMLparam1 (unit_v
);
3834 CAMLlocal2 (tup_v
, arr_v
);
3835 int platid
= piunknown
;
3838 #if defined __linux__
3840 #elif defined __DragonFly__ || defined __FreeBSD__
3841 || defined __OpenBSD__
|| defined __NetBSD__
3843 #elif defined __sun__
3845 #elif defined __APPLE__
3848 if (uname (&buf
)) err (1, "uname");
3850 tup_v
= caml_alloc_tuple (2);
3852 char const *sar
[] = {
3859 arr_v
= caml_copy_string_array (sar
);
3861 Field (tup_v
, 0) = Val_int (platid
);
3862 Field (tup_v
, 1) = arr_v
;
3866 CAMLprim
void ml_cloexec (value fd_v
)
3869 int fd
= Int_val (fd_v
);
3871 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3872 uerror ("fcntl", Nothing
);
3877 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3879 CAMLparam2 (w_v
, h_v
);
3882 int w
= Int_val (w_v
);
3883 int h
= Int_val (h_v
);
3884 int cs
= Int_val (cs_v
);
3886 if (state
.bo_usable
) {
3887 pbo
= calloc (sizeof (*pbo
), 1);
3889 err (1, "calloc pbo");
3901 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3904 state
.glGenBuffersARB (1, &pbo
->id
);
3905 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3906 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3907 NULL
, GL_STREAM_DRAW
);
3908 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3910 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3912 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3913 state
.glDeleteBuffersARB (1, &pbo
->id
);
3915 ret_v
= caml_copy_string ("0");
3921 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3923 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3927 err (1, "malloc %d bytes failed", res
+1);
3929 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3931 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3933 ret_v
= caml_copy_string (s
);
3938 ret_v
= caml_copy_string ("0");
3943 CAMLprim
void ml_freepbo (value s_v
)
3946 char *s
= String_val (s_v
);
3947 struct tile
*tile
= parse_pointer (__func__
, s
);
3950 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3952 tile
->pbo
->ptr
= NULL
;
3953 tile
->pbo
->size
= -1;
3958 CAMLprim
void ml_unmappbo (value s_v
)
3961 char *s
= String_val (s_v
);
3962 struct tile
*tile
= parse_pointer (__func__
, s
);
3965 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3966 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3967 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3969 tile
->pbo
->ptr
= NULL
;
3970 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3975 static void setuppbo (void)
3978 #pragma GCC diagnostic ignored "-Wunused-macros"
3981 static CFBundleRef framework
= NULL
;
3982 if (framework
== NULL
)
3983 framework
= CFBundleGetBundleWithIdentifier (CFSTR ("com.apple.opengl"));
3984 #define GGPA(n) (&state.n = CFBundleGetFunctionPointerForName (framework, CFSTR (#n)))
3987 #define GGPA(n) (*(void (**) (void)) &state.n = eglGetProcAddress (#n))
3989 #define GGPA(n) (*(void (**) (void)) &state.n = glXGetProcAddress ((GLubyte *) #n))
3991 state
.bo_usable
= GGPA (glBindBufferARB
)
3992 && GGPA (glUnmapBufferARB
)
3993 && GGPA (glMapBufferARB
)
3994 && GGPA (glBufferDataARB
)
3995 && GGPA (glGenBuffersARB
)
3996 && GGPA (glDeleteBuffersARB
);
4001 CAMLprim value
ml_bo_usable (value unit_v
)
4003 CAMLparam1 (unit_v
);
4004 CAMLreturn (Val_bool (state
.bo_usable
));
4007 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4009 CAMLparam3 (ptr_v
, x_v
, y_v
);
4010 CAMLlocal2 (ret_v
, tup_v
);
4012 char *s
= String_val (ptr_v
);
4013 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4014 struct pagedim
*pdim
;
4018 page
= parse_pointer (__func__
, s
);
4019 pdim
= &state
.pagedims
[page
->pdimno
];
4021 ret_v
= Val_int (0);
4022 if (trylock (__func__
)) {
4026 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4027 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4028 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4033 p
.x
= x
+ pdim
->bounds
.x0
;
4034 p
.y
= y
+ pdim
->bounds
.y0
;
4036 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4037 fz_invert_matrix (&ctm
, &ctm
);
4038 fz_transform_point (&p
, &ctm
);
4040 tup_v
= caml_alloc_tuple (2);
4041 ret_v
= caml_alloc_small (1, 1);
4042 Field (tup_v
, 0) = Val_int (p
.x
);
4043 Field (tup_v
, 1) = Val_int (p
.y
);
4044 Field (ret_v
, 0) = tup_v
;
4051 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4052 value x_v
, value y_v
)
4054 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4057 char *s
= String_val (ptr_v
);
4058 int pageno
= Int_val (pageno_v
);
4059 int pdimno
= Int_val (pdimno_v
);
4060 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
4061 struct pagedim
*pdim
;
4065 ret_v
= Val_int (0);
4069 page
= loadpage (pageno
, pdimno
);
4072 page
= parse_pointer (__func__
, s
);
4074 pdim
= &state
.pagedims
[pdimno
];
4076 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4077 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4078 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4083 p
.x
= x
+ pdim
->bounds
.x0
;
4084 p
.y
= y
+ pdim
->bounds
.y0
;
4086 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4087 fz_transform_point (&p
, &ctm
);
4089 ret_v
= caml_alloc_tuple (2);
4090 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
4091 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
4100 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
4103 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4104 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4110 char *s
= String_val (ptr_v
);
4112 page
= parse_pointer (__func__
, s
);
4113 annot
= pdf_create_annot (state
.ctx
,
4114 pdf_page_from_fz_page (state
.ctx
,
4117 p
.x
= Int_val (x_v
);
4118 p
.y
= Int_val (y_v
);
4119 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4120 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4126 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
4128 CAMLparam2 (ptr_v
, n_v
);
4129 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4133 char *s
= String_val (ptr_v
);
4134 struct slink
*slink
;
4136 page
= parse_pointer (__func__
, s
);
4137 slink
= &page
->slinks
[Int_val (n_v
)];
4138 pdf_delete_annot (state
.ctx
,
4139 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4140 (pdf_annot
*) slink
->u
.annot
);
4146 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
4148 CAMLparam3 (ptr_v
, n_v
, str_v
);
4149 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4153 char *s
= String_val (ptr_v
);
4154 struct slink
*slink
;
4156 page
= parse_pointer (__func__
, s
);
4157 slink
= &page
->slinks
[Int_val (n_v
)];
4158 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4159 String_val (str_v
));
4165 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4167 CAMLparam1 (unit_v
);
4168 CAMLreturn (Val_bool (state
.dirty
));
4171 CAMLprim
void ml_savedoc (value path_v
)
4173 CAMLparam1 (path_v
);
4174 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4177 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4182 static void makestippletex (void)
4184 const char pixels
[] = "\xff\xff\0\0";
4185 glGenTextures (1, &state
.stid
);
4186 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4187 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4188 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4201 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4203 return caml_copy_string (FZ_VERSION
);
4206 static char *ystrdup (const char *s
)
4208 size_t len
= strlen (s
);
4210 char *r
= malloc (len
+1);
4211 if (!r
) errx (1, "malloc %zu", len
+1);
4212 memcpy (r
, s
, len
+1);
4218 CAMLprim
void ml_init (value csock_v
, value params_v
)
4220 CAMLparam2 (csock_v
, params_v
);
4221 CAMLlocal2 (trim_v
, fuzz_v
);
4229 state
.csock
= Int_val (csock_v
);
4230 state
.rotate
= Int_val (Field (params_v
, 0));
4231 state
.fitmodel
= Int_val (Field (params_v
, 1));
4232 trim_v
= Field (params_v
, 2);
4233 texcount
= Int_val (Field (params_v
, 3));
4234 state
.sliceheight
= Int_val (Field (params_v
, 4));
4235 mustoresize
= Int_val (Field (params_v
, 5));
4236 colorspace
= Int_val (Field (params_v
, 6));
4237 fontpath
= String_val (Field (params_v
, 7));
4239 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
4240 if (setlocale (LC_CTYPE
, "")) {
4241 const char *cset
= nl_langinfo (CODESET
);
4242 state
.utf8cs
= !strcmp (cset
, "UTF-8");
4245 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
4248 if (caml_string_length (Field (params_v
, 8)) > 0) {
4249 state
.trimcachepath
= ystrdup (String_val (Field (params_v
, 8)));
4251 if (!state
.trimcachepath
) {
4252 printd ("emsg failed to strdup trimcachepath: %d:%s",
4253 errno
, strerror (errno
));
4257 haspboext
= Bool_val (Field (params_v
, 9));
4259 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4260 fz_register_document_handlers (state
.ctx
);
4262 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4263 fuzz_v
= Field (trim_v
, 1);
4264 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4265 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4266 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4267 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4269 set_tex_params (colorspace
);
4272 state
.face
= load_font (fontpath
);
4276 const unsigned char *data
;
4278 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
4279 state
.face
= load_builtin_font (data
, len
);
4281 if (!state
.face
) _exit (1);
4283 realloctexts (texcount
);
4291 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4293 errx (1, "pthread_create: %s", strerror (ret
));
4300 static void OPTIMIZE_ATTR (0) UNUSED_ATTR
refmacs (void) {}