1 #if defined __clang__ && !defined __APPLE__
2 #pragma GCC diagnostic error "-Weverything"
3 #pragma GCC diagnostic ignored "-Wfloat-equal"
4 #pragma GCC diagnostic ignored "-Wpadded"
5 #pragma GCC diagnostic ignored "-Wsign-conversion"
6 #pragma GCC diagnostic ignored "-Wdocumentation-unknown-command"
7 #pragma GCC diagnostic ignored "-Wdocumentation"
8 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
11 /* lots of code c&p-ed directly from mupdf */
12 #define CAML_NAME_SPACE
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/utsname.h>
38 #include <cygwin/socket.h> /* FIONREAD */
49 #include <CoreFoundation/CoreFoundation.h>
53 #include <OpenGL/gl.h>
58 #pragma GCC diagnostic push
60 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
62 #pragma GCC diagnostic ignored "-Wpedantic"
63 #include <caml/fail.h>
64 #include <caml/alloc.h>
65 #include <caml/memory.h>
66 #include <caml/unixsupport.h>
68 #if __GNUC__ < 5 && !defined __clang__
69 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
70 clobbered diagnostics */
71 #pragma GCC diagnostic ignored "-Wclobbered"
74 #include <mupdf/fitz.h>
75 #include <mupdf/pdf.h>
78 #include FT_FREETYPE_H
79 #pragma GCC diagnostic pop
82 #define CACHE_PAGEREFS
85 extern char **environ
;
89 #define NORETURN_ATTR __attribute__ ((noreturn))
90 #define UNUSED_ATTR __attribute__ ((unused))
91 #if !defined __clang__
92 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
94 #define OPTIMIZE_ATTR(n)
96 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
100 #define OPTIMIZE_ATTR(n)
101 #define GCC_FMT_ATTR(a, b)
106 #define FMT_ptr PRIxPTR
107 #define SCN_ptr SCNxPTR
108 #define FMT_ptr_cast(p) ((uintptr_t) (p))
109 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
111 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
112 err (int exitcode
, const char *fmt
, ...)
119 vfprintf (stderr
, fmt
, ap
);
121 fprintf (stderr
, ": %s\n", strerror (savederrno
));
126 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
127 errx (int exitcode
, const char *fmt
, ...)
132 vfprintf (stderr
, fmt
, ap
);
134 fputc ('\n', stderr
);
139 #ifndef GL_TEXTURE_RECTANGLE_ARB
140 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
144 #define TEXT_TYPE GL_TEXTURE_2D
146 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
150 #define GL_BGRA 0x80E1
153 #ifndef GL_UNSIGNED_INT_8_8_8_8
154 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
157 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
158 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
162 #define lprintf printf
167 #define ARSERT(cond) for (;;) { \
169 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
185 struct slice slices
[1];
196 fz_matrix ctm
, zoomctm
, tctm
;
200 enum { SLINK
, SANNOT
} tag
;
221 fz_display_list
*dlist
;
224 struct slink
*slinks
;
226 struct annot
*annots
;
234 struct pagedim
*pagedims
;
249 fz_colorspace
*colorspace
;
257 enum { FitWidth
, FitProportional
, FitPage
};
281 void (*glBindBufferARB
) (GLenum
, GLuint
);
282 GLboolean (*glUnmapBufferARB
) (GLenum
);
283 void *(*glMapBufferARB
) (GLenum
, GLenum
);
284 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
285 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
286 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
288 GLfloat texcoords
[8];
289 GLfloat vertices
[16];
291 #ifdef CACHE_PAGEREFS
308 #pragma GCC diagnostic ignored "-Wdouble-promotion"
309 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
311 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
314 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
316 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
319 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
321 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
322 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
324 #pragma GCC diagnostic error "-Wdouble-promotion"
326 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
328 static void lock (const char *cap
)
330 int ret
= pthread_mutex_lock (&mutex
);
332 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
336 static void unlock (const char *cap
)
338 int ret
= pthread_mutex_unlock (&mutex
);
340 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
344 static int trylock (const char *cap
)
346 int ret
= pthread_mutex_trylock (&mutex
);
347 if (ret
&& ret
!= EBUSY
) {
348 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
353 static void *parse_pointer (const char *cap
, const char *s
)
358 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
360 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
365 static double now (void)
369 if (gettimeofday (&tv
, NULL
)) {
370 err (1, "gettimeofday");
372 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
375 static int hasdata (void)
378 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
379 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
383 CAMLprim value
ml_hasdata (value fd_v
)
388 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
389 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
390 CAMLreturn (Val_bool (avail
> 0));
393 static void readdata (int fd
, void *p
, int size
)
398 n
= read (fd
, p
, size
);
400 if (n
< 0 && errno
== EINTR
) goto again
;
401 if (!n
) errx (1, "EOF while reading");
402 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
406 static void writedata (int fd
, char *p
, int size
)
409 uint32_t size4
= size
;
410 struct iovec iov
[2] = {
411 { .iov_base
= &size4
, .iov_len
= 4 },
412 { .iov_base
= p
, .iov_len
= size
}
416 n
= writev (fd
, iov
, 2);
417 if (n
< 0 && errno
== EINTR
) goto again
;
419 if (!n
) errx (1, "EOF while writing data");
420 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
424 static int readlen (int fd
)
427 readdata (fd
, &u
, 4);
431 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
433 CAMLparam3 (fd_v
, bytes_v
, len_v
);
434 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
438 CAMLprim value
ml_rcmd (value fd_v
)
441 CAMLlocal1 (strdata_v
);
442 int fd
= Int_val (fd_v
);
443 int len
= readlen (fd
);
444 strdata_v
= caml_alloc_string (len
);
445 readdata (fd
, String_val (strdata_v
), len
);
446 CAMLreturn (strdata_v
);
449 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
452 int size
= sizeof (fbuf
), len
;
458 len
= vsnprintf (buf
, size
, fmt
, ap
);
462 if (len
< size
- 4) {
463 writedata (state
.csock
, buf
, len
);
469 err (1, "vsnprintf for `%s' failed", fmt
);
471 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
472 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
474 if (buf
!= fbuf
) free (buf
);
477 static void closedoc (void)
479 #ifdef CACHE_PAGEREFS
480 if (state
.pdflut
.objs
) {
481 for (int i
= 0; i
< state
.pdflut
.count
; ++i
) {
482 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
484 free (state
.pdflut
.objs
);
485 state
.pdflut
.objs
= NULL
;
486 state
.pdflut
.idx
= 0;
490 fz_drop_document (state
.ctx
, state
.doc
);
495 static int openxref (char *filename
, char *password
)
497 for (int i
= 0; i
< state
.texcount
; ++i
) {
498 state
.texowners
[i
].w
= -1;
499 state
.texowners
[i
].slice
= NULL
;
505 if (state
.pagedims
) {
506 free (state
.pagedims
);
507 state
.pagedims
= NULL
;
509 state
.pagedimcount
= 0;
511 fz_set_aa_level (state
.ctx
, state
.aalevel
);
512 state
.doc
= fz_open_document (state
.ctx
, filename
);
513 if (fz_needs_password (state
.ctx
, state
.doc
)) {
514 if (password
&& !*password
) {
519 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
521 printd ("pass fail");
526 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
530 static void pdfinfo (void)
532 struct { char *tag
; char *name
; } metatbl
[] = {
533 { FZ_META_INFO_TITLE
, "Title" },
534 { FZ_META_INFO_AUTHOR
, "Author" },
535 { FZ_META_FORMAT
, "Format" },
536 { FZ_META_ENCRYPTION
, "Encryption" },
537 { "info:Creator", "Creator" },
538 { "info:Producer", "Producer" },
539 { "info:CreationDate", "Creation date" },
544 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
547 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
548 metatbl
[i
].tag
, buf
, len
);
551 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
554 buf
= realloc (buf
, need
+ 1);
555 if (!buf
) err (1, "pdfinfo realloc %d", need
+ 1);
566 static void unlinktile (struct tile
*tile
)
568 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
569 struct slice
*s
= &tile
->slices
[i
];
571 if (s
->texindex
!= -1) {
572 if (state
.texowners
[s
->texindex
].slice
== s
) {
573 state
.texowners
[s
->texindex
].slice
= NULL
;
579 static void freepage (struct page
*page
)
583 fz_drop_stext_page (state
.ctx
, page
->text
);
588 fz_drop_display_list (state
.ctx
, page
->dlist
);
589 fz_drop_page (state
.ctx
, page
->fzpage
);
593 static void freetile (struct tile
*tile
)
598 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
601 fz_drop_pixmap (state
.ctx
, state
.pig
);
603 state
.pig
= tile
->pixmap
;
608 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
617 static int cacheline32bytes
;
619 static void __attribute__ ((constructor
)) clcheck (void)
621 char **envp
= environ
;
626 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
628 cacheline32bytes
= auxv
[1] == 32;
634 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
636 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
637 if (cacheline32bytes
&& size
> 32) {
638 intptr_t a1
, a2
, diff
;
640 vector
unsigned char v
= vec_splat_u8 (-1);
641 vector
unsigned char *p
;
643 a1
= a2
= (intptr_t) pixmap
->samples
;
644 a2
= (a1
+ 31) & ~31;
649 while (a1
!= a2
) *(char *) a1
++ = 0xff;
650 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
651 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
653 vec_st (v
, i
+ 16, p
);
655 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
657 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
660 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
663 static void trimctm (pdf_page
*page
, int pindex
)
666 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
669 if (!pdim
->tctmready
) {
670 fz_rect realbox
, mediabox
;
671 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
673 fz_rotate (&rm
, -pdim
->rotate
);
674 fz_scale (&sm
, 1, -1);
675 fz_concat (&ctm
, &rm
, &sm
);
676 realbox
= pdim
->mediabox
;
677 fz_transform_rect (&realbox
, &ctm
);
678 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
679 fz_concat (&ctm1
, &ctm
, &tm
);
680 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
681 fz_invert_matrix (&im
, &page_ctm
);
682 fz_concat (&ctm
, &im
, &ctm1
);
688 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
691 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
693 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
694 if (pdf_specifics (state
.ctx
, state
.doc
)) {
695 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
696 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
699 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
700 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
705 static fz_matrix
pagectm (struct page
*page
)
707 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
710 static void *loadpage (int pageno
, int pindex
)
715 page
= calloc (sizeof (struct page
), 1);
717 err (1, "calloc page %d", pageno
);
720 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
721 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
723 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
724 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
727 fz_catch (state
.ctx
) {
730 fz_close_device (state
.ctx
, dev
);
731 fz_drop_device (state
.ctx
, dev
);
733 page
->pdimno
= pindex
;
734 page
->pageno
= pageno
;
735 page
->sgen
= state
.gen
;
736 page
->agen
= state
.gen
;
737 page
->tgen
= state
.gen
;
741 static struct tile
*alloctile (int h
)
747 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
748 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
749 tile
= calloc (tilesize
, 1);
751 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
753 for (int i
= 0; i
< slicecount
; ++i
) {
754 int sh
= fz_mini (h
, state
.sliceheight
);
755 tile
->slices
[i
].h
= sh
;
756 tile
->slices
[i
].texindex
= -1;
759 tile
->slicecount
= slicecount
;
760 tile
->sliceheight
= state
.sliceheight
;
764 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
772 struct pagedim
*pdim
;
774 tile
= alloctile (h
);
775 pdim
= &state
.pagedims
[page
->pdimno
];
780 bbox
.x1
= bbox
.x0
+ w
;
781 bbox
.y1
= bbox
.y0
+ h
;
784 if (state
.pig
->w
== w
786 && state
.pig
->colorspace
== state
.colorspace
) {
787 tile
->pixmap
= state
.pig
;
788 tile
->pixmap
->x
= bbox
.x0
;
789 tile
->pixmap
->y
= bbox
.y0
;
792 fz_drop_pixmap (state
.ctx
, state
.pig
);
799 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
800 &bbox
, NULL
, 1, pbo
->ptr
);
805 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
,
812 clearpixmap (tile
->pixmap
);
814 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
815 ctm
= pagectm (page
);
816 fz_rect_from_irect (&rect
, &bbox
);
817 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
818 fz_close_device (state
.ctx
, dev
);
819 fz_drop_device (state
.ctx
, dev
);
824 #ifdef CACHE_PAGEREFS
825 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
826 thanks to Robin Watts */
828 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
830 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
834 if (state
.pdflut
.idx
== state
.pagecount
) return;
836 kids
= pdf_dict_gets (ctx
, node
, "Kids");
837 len
= pdf_array_len (ctx
, kids
);
840 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
842 if (pdf_mark_obj (ctx
, node
))
843 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
844 for (int i
= 0; i
< len
; i
++) {
845 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
846 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
848 ? !strcmp (type
, "Pages")
849 : pdf_dict_gets (ctx
, kid
, "Kids")
850 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
851 pdf_collect_pages (doc
, kid
);
855 ? strcmp (type
, "Page") != 0
856 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
857 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
858 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
861 pdf_unmark_obj (ctx
, node
);
865 pdf_load_page_objs (pdf_document
*doc
)
867 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
868 pdf_trailer (state
.ctx
, doc
), "Root");
869 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
872 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
874 state
.pdflut
.idx
= 0;
875 pdf_collect_pages (doc
, node
);
879 static void initpdims (int wthack
)
883 fz_rect rootmediabox
= fz_empty_rect
;
884 int pageno
, trim
, show
;
885 int trimw
= 0, cxcount
;
886 fz_context
*ctx
= state
.ctx
;
887 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
894 if (state
.trimmargins
&& state
.trimcachepath
) {
895 trimf
= fopen (state
.trimcachepath
, "rb");
897 trimf
= fopen (state
.trimcachepath
, "wb");
902 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
903 cxcount
= state
.pagecount
;
905 cxcount
= fz_mini (state
.pagecount
, 1);
909 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
910 "Root/Pages/MediaBox");
911 pdf_to_rect (ctx
, obj
, &rootmediabox
);
914 #ifdef CACHE_PAGEREFS
915 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
916 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
917 if (!state
.pdflut
.objs
) {
918 err (1, "malloc pageobjs %zu %d %zu failed",
919 sizeof (*state
.pdflut
.objs
), cxcount
,
920 sizeof (*state
.pdflut
.objs
) * cxcount
);
922 state
.pdflut
.count
= cxcount
;
923 pdf_load_page_objs (pdf
);
924 state
.pdflut
.pdf
= pdf
;
928 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
931 fz_rect mediabox
= fz_empty_rect
;
935 pdf_obj
*pageref
, *pageobj
;
937 #ifdef CACHE_PAGEREFS
938 pageref
= state
.pdflut
.objs
[pageno
];
940 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
942 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
943 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
945 if (state
.trimmargins
) {
950 page
= pdf_load_page (ctx
, pdf
, pageno
);
951 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
952 trim
= state
.trimanew
|| !obj
;
956 fz_matrix ctm
, page_ctm
;
958 dev
= fz_new_bbox_device (ctx
, &rect
);
959 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
960 fz_invert_matrix (&ctm
, &page_ctm
);
961 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
962 fz_close_device (ctx
, dev
);
963 fz_drop_device (ctx
, dev
);
965 rect
.x0
+= state
.trimfuzz
.x0
;
966 rect
.x1
+= state
.trimfuzz
.x1
;
967 rect
.y0
+= state
.trimfuzz
.y0
;
968 rect
.y1
+= state
.trimfuzz
.y1
;
969 fz_transform_rect (&rect
, &ctm
);
970 fz_intersect_rect (&rect
, &mediabox
);
972 if (!fz_is_empty_rect (&rect
)) {
976 obj
= pdf_new_array (ctx
, pdf
, 4);
977 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
979 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
981 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
983 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
985 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
988 mediabox
.x0
= pdf_to_real (ctx
,
989 pdf_array_get (ctx
, obj
, 0));
990 mediabox
.y0
= pdf_to_real (ctx
,
991 pdf_array_get (ctx
, obj
, 1));
992 mediabox
.x1
= pdf_to_real (ctx
,
993 pdf_array_get (ctx
, obj
, 2));
994 mediabox
.y1
= pdf_to_real (ctx
,
995 pdf_array_get (ctx
, obj
, 3));
998 fz_drop_page (ctx
, &page
->super
);
999 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1001 printd ("progress %f Trimming %d",
1002 (double) (pageno
+ 1) / state
.pagecount
,
1007 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1015 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
1017 if (fz_is_empty_rect (&mediabox
)) {
1026 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1028 if (!fz_is_empty_rect (&cropbox
)) {
1033 fz_intersect_rect (&mediabox
, &cropbox
);
1038 if (fz_is_empty_rect (&rootmediabox
)) {
1040 "cannot find page size for page %d\n",
1044 mediabox
= rootmediabox
;
1051 if (state
.trimmargins
&& trimw
) {
1055 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1056 fz_bound_page (ctx
, page
, &mediabox
);
1057 if (state
.trimmargins
) {
1061 dev
= fz_new_bbox_device (ctx
, &rect
);
1062 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1063 fz_close_device (ctx
, dev
);
1064 fz_drop_device (ctx
, dev
);
1066 rect
.x0
+= state
.trimfuzz
.x0
;
1067 rect
.x1
+= state
.trimfuzz
.x1
;
1068 rect
.y0
+= state
.trimfuzz
.y0
;
1069 rect
.y1
+= state
.trimfuzz
.y1
;
1070 fz_intersect_rect (&rect
, &mediabox
);
1072 if (!fz_is_empty_rect (&rect
)) {
1076 fz_drop_page (ctx
, page
);
1078 printd ("progress %f loading %d",
1079 (double) (pageno
+ 1) / state
.pagecount
,
1086 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1088 err (1, "fwrite trim mediabox");
1094 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1096 err (1, "fread trim mediabox %d", pageno
);
1102 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1103 fz_bound_page (ctx
, page
, &mediabox
);
1104 fz_drop_page (ctx
, page
);
1106 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1108 printd ("progress %f Gathering dimensions %d",
1109 (double) (pageno
) / state
.pagecount
,
1114 fprintf (stderr
, "failed to load page %d\n", pageno
);
1120 if (state
.pagedimcount
== 0
1121 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
1122 , p
->rotate
!= rotate
)
1123 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1126 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1127 state
.pagedims
= realloc (state
.pagedims
, size
);
1128 if (!state
.pagedims
) {
1129 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1130 size
, state
.pagedimcount
+ 1);
1133 p
= &state
.pagedims
[state
.pagedimcount
++];
1135 p
->mediabox
= mediabox
;
1141 printd ("progress 1 %s %d pages in %f seconds",
1142 state
.trimmargins
? "Trimmed" : "Processed",
1143 state
.pagecount
, end
- start
);
1147 if (fclose (trimf
)) {
1153 static void layout (void)
1158 struct pagedim
*p
= NULL
;
1159 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
1161 if (state
.pagedimcount
== 0) return;
1163 switch (state
.fitmodel
) {
1164 case FitProportional
:
1165 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1168 p
= &state
.pagedims
[pindex
];
1169 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1171 fz_transform_rect (&box
, &rm
);
1173 x0
= fz_min (box
.x0
, box
.x1
);
1174 x1
= fz_max (box
.x0
, box
.x1
);
1177 maxw
= fz_max (w
, maxw
);
1178 zoom
= state
.w
/ maxw
;
1190 ARSERT (0 && state
.fitmodel
);
1193 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1197 p
= &state
.pagedims
[pindex
];
1198 fz_rotate (&ctm
, state
.rotate
);
1199 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1201 fz_transform_rect (&box
, &rm
);
1202 w
= box
.x1
- box
.x0
;
1203 switch (state
.fitmodel
) {
1204 case FitProportional
:
1205 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
1211 h
= box
.y1
- box
.y0
;
1213 zoom
= fz_min (zw
, zh
);
1214 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
1223 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1224 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1226 fz_rotate (&rm
, p
->rotate
);
1227 p
->pagebox
= p
->mediabox
;
1228 fz_transform_rect (&p
->pagebox
, &rm
);
1229 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1230 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1234 fz_transform_rect (&rect
, &ctm
);
1235 fz_round_rect (&p
->bounds
, &rect
);
1238 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1239 fz_scale (&sm
, zoom
, -zoom
);
1240 fz_concat (&ctm
, &tm
, &sm
);
1246 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1247 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1248 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1249 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1250 int boundw
= x1
- x0
;
1251 int boundh
= y1
- y0
;
1253 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1254 } while (p
-- != state
.pagedims
);
1257 struct pagedim
*pdimofpageno (int pageno
)
1259 struct pagedim
*pdim
= state
.pagedims
;
1261 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1262 if (state
.pagedims
[i
].pageno
> pageno
)
1264 pdim
= &state
.pagedims
[i
];
1269 static void recurse_outline (fz_outline
*outline
, int level
)
1272 if (outline
->page
>= 0) {
1273 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
1274 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
1275 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1276 fz_transform_point (&p
, &pdim
->ctm
);
1277 printd ("o %d %d %d %d %s",
1278 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
1281 printd ("on %d %s", level
, outline
->title
);
1283 if (outline
->down
) {
1284 recurse_outline (outline
->down
, level
+ 1);
1286 outline
= outline
->next
;
1290 static void process_outline (void)
1292 fz_outline
*outline
;
1294 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1296 state
.needoutline
= 0;
1297 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1299 recurse_outline (outline
, 0);
1300 fz_drop_outline (state
.ctx
, outline
);
1304 static char *strofline (fz_stext_line
*line
)
1309 size_t size
= 0, cap
= 80;
1311 p
= malloc (cap
+ 1);
1312 if (!p
) return NULL
;
1314 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1315 int n
= fz_runetochar (utf8
, ch
->c
);
1316 if (size
+ n
> cap
) {
1318 p
= realloc (p
, cap
+ 1);
1319 if (!p
) return NULL
;
1322 memcpy (p
+ size
, utf8
, n
);
1329 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1330 int stop
, int pageno
, double start
)
1336 p
= strofline (line
);
1339 ret
= regexec (re
, p
, 1, &rm
, 0);
1342 if (ret
!= REG_NOMATCH
) {
1345 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1346 printd ("msg regexec error `%.*s'",
1347 (int) size
, errbuf
);
1353 fz_point p1
, p2
, p3
, p4
;
1354 fz_rect s
= {0,0,0,0}, e
;
1358 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1359 o
+= fz_runelen (ch
->c
);
1365 for (;ch
; ch
= ch
->next
) {
1366 o
+= fz_runelen (ch
->c
);
1367 if (o
> rm
.rm_eo
) break;
1380 #pragma GCC diagnostic ignored "-Wdouble-promotion"
1382 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1389 printd ("progress 1 found at %d `%.*s' in %f sec",
1390 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1394 printd ("match %d %d %f %f %f %f %f %f %f %f",
1401 #pragma GCC diagnostic error "-Wdouble-promotion"
1407 /* wishful thinking function */
1408 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1411 fz_stext_page
*text
;
1412 struct pagedim
*pdim
;
1413 int stop
= 0, niters
= 0;
1416 fz_stext_block
*block
;
1419 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1420 if (niters
++ == 5) {
1423 printd ("progress 1 attention requested aborting search at %d",
1428 printd ("progress %f searching in page %d",
1429 (double) (pageno
+ 1) / state
.pagecount
,
1433 pdim
= pdimofpageno (pageno
);
1434 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1435 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1437 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1439 fz_matrix ctm
= pagectm1 (page
, pdim
);
1440 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1443 fz_close_device (state
.ctx
, tdev
);
1444 fz_drop_device (state
.ctx
, tdev
);
1447 for (block
= text
->first_block
; block
; block
= block
->next
) {
1448 fz_stext_line
*line
;
1450 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1451 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1452 if (line
->bbox
.y0
< y
+ 1) continue;
1454 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1456 case 1: stop
= 1; break;
1457 case -1: stop
= 1; goto endloop
;
1463 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1464 fz_stext_line
*line
;
1466 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1467 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1468 if (line
->bbox
.y0
< y
+ 1) continue;
1470 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1472 case 1: stop
= 1; break;
1473 case -1: stop
= 1; goto endloop
;
1488 fz_drop_stext_page (state
.ctx
, text
);
1489 fz_drop_page (state
.ctx
, page
);
1493 printd ("progress 1 no matches %f sec", end
- start
);
1495 printd ("clearrects");
1498 static void set_tex_params (int colorspace
)
1500 switch (colorspace
) {
1502 state
.texiform
= GL_RGBA8
;
1503 state
.texform
= GL_RGBA
;
1504 state
.texty
= GL_UNSIGNED_BYTE
;
1505 state
.colorspace
= fz_device_rgb (state
.ctx
);
1508 state
.texiform
= GL_RGBA8
;
1509 state
.texform
= GL_BGRA
;
1510 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1511 state
.texty
= GL_UNSIGNED_INT_8_8_8_8_REV
;
1513 state
.texty
= GL_UNSIGNED_INT_8_8_8_8
;
1515 state
.colorspace
= fz_device_bgr (state
.ctx
);
1518 state
.texiform
= GL_LUMINANCE_ALPHA
;
1519 state
.texform
= GL_LUMINANCE_ALPHA
;
1520 state
.texty
= GL_UNSIGNED_BYTE
;
1521 state
.colorspace
= fz_device_gray (state
.ctx
);
1524 errx (1, "invalid colorspce %d", colorspace
);
1528 static void realloctexts (int texcount
)
1532 if (texcount
== state
.texcount
) return;
1534 if (texcount
< state
.texcount
) {
1535 glDeleteTextures (state
.texcount
- texcount
,
1536 state
.texids
+ texcount
);
1539 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1540 state
.texids
= realloc (state
.texids
, size
);
1541 if (!state
.texids
) {
1542 err (1, "realloc texs %" FMT_s
, size
);
1545 state
.texowners
= (void *) (state
.texids
+ texcount
);
1546 if (texcount
> state
.texcount
) {
1547 glGenTextures (texcount
- state
.texcount
,
1548 state
.texids
+ state
.texcount
);
1549 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1550 state
.texowners
[i
].w
= -1;
1551 state
.texowners
[i
].slice
= NULL
;
1554 state
.texcount
= texcount
;
1558 static char *mbtoutf8 (char *s
)
1568 len
= mbstowcs (NULL
, s
, strlen (s
));
1573 if (len
== (size_t) -1) {
1574 printd ("emsg mbtoutf8: mbstowcs %d:%s\n", errno
, strerror (errno
));
1579 tmp
= calloc (len
, sizeof (wchar_t));
1581 printd ("emsg mbtoutf8: calloc(%zu, %zu) %d:%s",
1582 len
, sizeof (wchar_t), errno
, strerror (errno
));
1586 ret
= mbstowcs (tmp
, s
, len
);
1587 if (ret
== (size_t) -1) {
1588 printd ("emsg mbtoutf8: mbswcs %zu characters failed %d:%s\n",
1589 len
, errno
, strerror (errno
));
1595 for (i
= 0; i
< ret
; ++i
) {
1596 len
+= fz_runelen (tmp
[i
]);
1599 p
= r
= malloc (len
+ 1);
1601 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1606 for (i
= 0; i
< ret
; ++i
) {
1607 p
+= fz_runetochar (p
, tmp
[i
]);
1614 CAMLprim value
ml_mbtoutf8 (value s_v
)
1620 s
= String_val (s_v
);
1626 ret_v
= caml_copy_string (r
);
1632 static void * mainloop (void UNUSED_ATTR
*unused
)
1635 int len
, ret
, oldlen
= 0;
1640 len
= readlen (state
.csock
);
1642 errx (1, "readlen returned 0");
1645 if (oldlen
< len
+ 1) {
1646 p
= realloc (p
, len
+ 1);
1648 err (1, "realloc %d failed", len
+ 1);
1652 readdata (state
.csock
, p
, len
);
1655 if (!strncmp ("open", p
, 4)) {
1656 int wthack
, off
, usedoccss
, ok
= 0;
1663 ret
= sscanf (p
+ 5, " %d %d %d %n",
1664 &wthack
, &state
.cxack
, &usedoccss
, &off
);
1666 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1669 filename
= p
+ 5 + off
;
1670 filenamelen
= strlen (filename
);
1671 password
= filename
+ filenamelen
+ 1;
1673 if (password
[strlen (password
) + 1]) {
1674 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1678 fz_set_use_document_css (state
.ctx
, usedoccss
);
1679 fz_try (state
.ctx
) {
1680 ok
= openxref (filename
, password
);
1682 fz_catch (state
.ctx
) {
1683 utf8filename
= mbtoutf8 (filename
);
1684 printd ("msg Could not open %s", utf8filename
);
1685 if (utf8filename
!= filename
) {
1686 free (utf8filename
);
1697 utf8filename
= mbtoutf8 (filename
);
1698 printd ("msg Opened %s (press h/F1 to get help)",
1700 if (utf8filename
!= filename
) {
1701 free (utf8filename
);
1704 state
.needoutline
= 1;
1707 else if (!strncmp ("cs", p
, 2)) {
1710 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1712 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1715 set_tex_params (colorspace
);
1716 for (i
= 0; i
< state
.texcount
; ++i
) {
1717 state
.texowners
[i
].w
= -1;
1718 state
.texowners
[i
].slice
= NULL
;
1722 else if (!strncmp ("freepage", p
, 8)) {
1725 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1727 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1731 unlock ("freepage");
1733 else if (!strncmp ("freetile", p
, 8)) {
1736 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1738 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1742 unlock ("freetile");
1744 else if (!strncmp ("search", p
, 6)) {
1745 int icase
, pageno
, y
, len2
, forward
;
1749 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1750 &icase
, &pageno
, &y
, &forward
, &len2
);
1752 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1755 pattern
= p
+ 6 + len2
;
1756 ret
= regcomp (&re
, pattern
,
1757 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1762 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1763 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1766 search (&re
, pageno
, y
, forward
);
1770 else if (!strncmp ("geometry", p
, 8)) {
1774 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1776 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1783 for (int i
= 0; i
< state
.texcount
; ++i
) {
1784 state
.texowners
[i
].slice
= NULL
;
1787 state
.fitmodel
= fitmodel
;
1792 unlock ("geometry");
1793 printd ("continue %d", state
.pagecount
);
1795 else if (!strncmp ("reqlayout", p
, 9)) {
1802 ret
= sscanf (p
+ 9, " %d %d %d %n",
1803 &rotate
, &fitmodel
, &h
, &off
);
1805 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1808 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1809 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1812 state
.rotate
= rotate
;
1813 state
.fitmodel
= fitmodel
;
1818 nameddest
= p
+ 9 + off
;
1819 if (pdf
&& nameddest
&& *nameddest
) {
1821 struct pagedim
*pdim
;
1822 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1824 pdim
= pdimofpageno (pageno
);
1825 fz_transform_point (&xy
, &pdim
->ctm
);
1826 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1830 unlock ("reqlayout");
1831 printd ("continue %d", state
.pagecount
);
1833 else if (!strncmp ("page", p
, 4)) {
1838 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1840 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1845 page
= loadpage (pageno
, pindex
);
1849 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1851 else if (!strncmp ("tile", p
, 4)) {
1858 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1859 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1860 SCN_ptr_cast (&data
));
1862 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1867 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1871 printd ("tile %d %d %" FMT_ptr
" %u %f",
1873 FMT_ptr_cast (tile
),
1874 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1877 else if (!strncmp ("trimset", p
, 7)) {
1881 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1882 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1884 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1887 state
.trimmargins
= trimmargins
;
1888 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1890 state
.trimfuzz
= fuzz
;
1894 else if (!strncmp ("settrim", p
, 7)) {
1898 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1899 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1901 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1905 state
.trimmargins
= trimmargins
;
1906 state
.needoutline
= 1;
1907 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1909 state
.trimfuzz
= fuzz
;
1911 state
.pagedimcount
= 0;
1912 free (state
.pagedims
);
1913 state
.pagedims
= NULL
;
1918 printd ("continue %d", state
.pagecount
);
1920 else if (!strncmp ("sliceh", p
, 6)) {
1923 ret
= sscanf (p
+ 6, " %d", &h
);
1925 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1927 if (h
!= state
.sliceheight
) {
1928 state
.sliceheight
= h
;
1929 for (int i
= 0; i
< state
.texcount
; ++i
) {
1930 state
.texowners
[i
].w
= -1;
1931 state
.texowners
[i
].h
= -1;
1932 state
.texowners
[i
].slice
= NULL
;
1936 else if (!strncmp ("interrupt", p
, 9)) {
1937 printd ("vmsg interrupted");
1940 errx (1, "unknown command %.*s", len
, p
);
1946 CAMLprim value
ml_isexternallink (value uri_v
)
1949 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1950 CAMLreturn (Val_bool (ext
));
1953 CAMLprim value
ml_uritolocation (value uri_v
)
1959 struct pagedim
*pdim
;
1961 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1963 pdim
= pdimofpageno (pageno
);
1964 fz_transform_point (&xy
, &pdim
->ctm
);
1965 ret_v
= caml_alloc_tuple (3);
1966 Field (ret_v
, 0) = Val_int (pageno
);
1967 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1968 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1972 CAMLprim value
ml_realloctexts (value texcount_v
)
1974 CAMLparam1 (texcount_v
);
1977 if (trylock (__func__
)) {
1981 realloctexts (Int_val (texcount_v
));
1986 CAMLreturn (Val_bool (ok
));
1989 static void recti (int x0
, int y0
, int x1
, int y1
)
1991 GLfloat
*v
= state
.vertices
;
1993 glVertexPointer (2, GL_FLOAT
, 0, v
);
1994 v
[0] = x0
; v
[1] = y0
;
1995 v
[2] = x1
; v
[3] = y0
;
1996 v
[4] = x0
; v
[5] = y1
;
1997 v
[6] = x1
; v
[7] = y1
;
1998 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2001 static void showsel (struct page
*page
, int ox
, int oy
)
2005 fz_stext_block
*block
;
2007 unsigned char selcolor
[] = {15,15,15,140};
2009 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) return;
2011 glEnable (GL_BLEND
);
2012 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2013 glColor4ubv (selcolor
);
2015 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2016 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2018 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2019 fz_stext_line
*line
;
2021 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2022 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2025 rect
= fz_empty_rect
;
2026 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2027 if (ch
== page
->fmark
.ch
) seen
= 1;
2028 if (seen
) fz_union_rect (&rect
, &ch
->bbox
);
2029 if (ch
== page
->lmark
.ch
) {
2030 fz_round_rect (&bbox
, &rect
);
2031 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2032 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2036 fz_round_rect (&bbox
, &rect
);
2037 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2038 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2042 glDisable (GL_BLEND
);
2045 #pragma GCC diagnostic push
2046 #pragma GCC diagnostic ignored "-Weverything"
2048 #pragma GCC diagnostic pop
2050 static void stipplerect (fz_matrix
*m
,
2058 fz_transform_point (p1
, m
);
2059 fz_transform_point (p2
, m
);
2060 fz_transform_point (p3
, m
);
2061 fz_transform_point (p4
, m
);
2067 t
= hypotf (w
, h
) * .25f
;
2071 s
= hypotf (w
, h
) * .25f
;
2073 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2074 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2076 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2077 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2079 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2080 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2082 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2083 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2085 glDrawArrays (GL_LINES
, 0, 8);
2088 static void solidrect (fz_matrix
*m
,
2095 fz_transform_point (p1
, m
);
2096 fz_transform_point (p2
, m
);
2097 fz_transform_point (p3
, m
);
2098 fz_transform_point (p4
, m
);
2099 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2100 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2102 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2103 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2104 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2107 static void ensurelinks (struct page
*page
)
2110 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
2113 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2115 fz_matrix ctm
, tm
, pm
;
2117 GLfloat
*texcoords
= state
.texcoords
;
2118 GLfloat
*vertices
= state
.vertices
;
2122 glEnable (GL_TEXTURE_1D
);
2123 glEnable (GL_BLEND
);
2124 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2125 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2127 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2128 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2129 fz_translate (&tm
, xoff
, yoff
);
2130 pm
= pagectm (page
);
2131 fz_concat (&ctm
, &pm
, &tm
);
2133 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2134 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2136 for (link
= page
->links
; link
; link
= link
->next
) {
2137 fz_point p1
, p2
, p3
, p4
;
2139 p1
.x
= link
->rect
.x0
;
2140 p1
.y
= link
->rect
.y0
;
2142 p2
.x
= link
->rect
.x1
;
2143 p2
.y
= link
->rect
.y0
;
2145 p3
.x
= link
->rect
.x1
;
2146 p3
.y
= link
->rect
.y1
;
2148 p4
.x
= link
->rect
.x0
;
2149 p4
.y
= link
->rect
.y1
;
2151 /* TODO: different colours for different schemes */
2152 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
2153 else glColor3ub (255, 0, 0);
2155 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2158 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2159 fz_point p1
, p2
, p3
, p4
;
2160 struct annot
*annot
= &page
->annots
[i
];
2162 p1
.x
= annot
->bbox
.x0
;
2163 p1
.y
= annot
->bbox
.y0
;
2165 p2
.x
= annot
->bbox
.x1
;
2166 p2
.y
= annot
->bbox
.y0
;
2168 p3
.x
= annot
->bbox
.x1
;
2169 p3
.y
= annot
->bbox
.y1
;
2171 p4
.x
= annot
->bbox
.x0
;
2172 p4
.y
= annot
->bbox
.y1
;
2174 glColor3ub (0, 0, 128);
2175 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2178 glDisable (GL_BLEND
);
2179 glDisable (GL_TEXTURE_1D
);
2182 static int compareslinks (const void *l
, const void *r
)
2184 struct slink
const *ls
= l
;
2185 struct slink
const *rs
= r
;
2186 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2187 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2189 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2192 static void droptext (struct page
*page
)
2195 fz_drop_stext_page (state
.ctx
, page
->text
);
2196 page
->fmark
.ch
= NULL
;
2197 page
->lmark
.ch
= NULL
;
2202 static void dropannots (struct page
*page
)
2205 free (page
->annots
);
2206 page
->annots
= NULL
;
2207 page
->annotcount
= 0;
2211 static void ensureannots (struct page
*page
)
2214 size_t annotsize
= sizeof (*page
->annots
);
2217 if (state
.gen
!= page
->agen
) {
2219 page
->agen
= state
.gen
;
2221 if (page
->annots
) return;
2223 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2225 annot
= fz_next_annot (state
.ctx
, annot
)) {
2230 page
->annotcount
= count
;
2231 page
->annots
= calloc (count
, annotsize
);
2232 if (!page
->annots
) {
2233 err (1, "calloc annots %d", count
);
2236 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2238 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2241 fz_bound_annot (state
.ctx
, annot
, &rect
);
2242 page
->annots
[i
].annot
= annot
;
2243 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2248 static void dropslinks (struct page
*page
)
2251 free (page
->slinks
);
2252 page
->slinks
= NULL
;
2253 page
->slinkcount
= 0;
2256 fz_drop_link (state
.ctx
, page
->links
);
2261 static void ensureslinks (struct page
*page
)
2265 size_t slinksize
= sizeof (*page
->slinks
);
2268 ensureannots (page
);
2269 if (state
.gen
!= page
->sgen
) {
2271 page
->sgen
= state
.gen
;
2273 if (page
->slinks
) return;
2276 ctm
= pagectm (page
);
2278 count
= page
->annotcount
;
2279 for (link
= page
->links
; link
; link
= link
->next
) {
2285 page
->slinkcount
= count
;
2286 page
->slinks
= calloc (count
, slinksize
);
2287 if (!page
->slinks
) {
2288 err (1, "calloc slinks %d", count
);
2291 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
2295 fz_transform_rect (&rect
, &ctm
);
2296 page
->slinks
[i
].tag
= SLINK
;
2297 page
->slinks
[i
].u
.link
= link
;
2298 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2300 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2302 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2303 fz_transform_rect (&rect
, &ctm
);
2304 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2306 page
->slinks
[i
].tag
= SANNOT
;
2307 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2309 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2313 #pragma GCC diagnostic push
2314 #pragma GCC diagnostic ignored "-Wconversion"
2315 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2316 static void fmt_linkn (char *s
, unsigned int u
)
2318 unsigned int len
; unsigned int q
;
2319 unsigned int zma
= 'z' - 'a' + 1;
2321 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2324 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2325 /* handles u == 0 */
2329 #pragma GCC diagnostic pop
2331 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2332 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2335 struct slink
*slink
;
2336 float x0
, y0
, x1
, y1
, w
;
2338 ensureslinks (page
);
2339 glColor3ub (0xc3, 0xb0, 0x91);
2340 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2341 fmt_linkn (buf
, i
+ noff
);
2342 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2343 slink
= &page
->slinks
[i
];
2345 x0
= slink
->bbox
.x0
+ xoff
- 5;
2346 y1
= slink
->bbox
.y0
+ yoff
- 5;
2347 y0
= y1
+ 10 + hfsize
;
2348 w
= measure_string (state
.face
, hfsize
, buf
);
2350 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2354 glEnable (GL_BLEND
);
2355 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2356 glEnable (GL_TEXTURE_2D
);
2357 glColor3ub (0, 0, 0);
2358 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2359 fmt_linkn (buf
, i
+ noff
);
2360 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2361 slink
= &page
->slinks
[i
];
2363 x0
= slink
->bbox
.x0
+ xoff
;
2364 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2365 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2368 glDisable (GL_TEXTURE_2D
);
2369 glDisable (GL_BLEND
);
2372 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2375 struct slice
*slice1
;
2376 unsigned char *texdata
;
2379 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2380 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2382 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2383 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2384 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2388 int texindex
= state
.texindex
++ % state
.texcount
;
2390 if (state
.texowners
[texindex
].w
== tile
->w
) {
2391 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2395 state
.texowners
[texindex
].h
= slice
->h
;
2399 state
.texowners
[texindex
].h
= slice
->h
;
2402 state
.texowners
[texindex
].w
= tile
->w
;
2403 state
.texowners
[texindex
].slice
= slice
;
2404 slice
->texindex
= texindex
;
2406 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2407 #if TEXT_TYPE == GL_TEXTURE_2D
2408 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2409 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2410 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2411 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2414 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2418 texdata
= tile
->pixmap
->samples
;
2421 glTexSubImage2D (TEXT_TYPE
,
2433 glTexImage2D (TEXT_TYPE
,
2445 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2450 CAMLprim
void ml_begintiles (value unit_v
)
2452 CAMLparam1 (unit_v
);
2453 glEnable (TEXT_TYPE
);
2454 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2455 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2459 CAMLprim
void ml_endtiles (value unit_v
)
2461 CAMLparam1 (unit_v
);
2462 glDisable (TEXT_TYPE
);
2466 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2468 CAMLparam2 (args_v
, ptr_v
);
2469 int dispx
= Int_val (Field (args_v
, 0));
2470 int dispy
= Int_val (Field (args_v
, 1));
2471 int dispw
= Int_val (Field (args_v
, 2));
2472 int disph
= Int_val (Field (args_v
, 3));
2473 int tilex
= Int_val (Field (args_v
, 4));
2474 int tiley
= Int_val (Field (args_v
, 5));
2475 char *s
= String_val (ptr_v
);
2476 struct tile
*tile
= parse_pointer (__func__
, s
);
2477 int slicey
, firstslice
;
2478 struct slice
*slice
;
2479 GLfloat
*texcoords
= state
.texcoords
;
2480 GLfloat
*vertices
= state
.vertices
;
2482 firstslice
= tiley
/ tile
->sliceheight
;
2483 slice
= &tile
->slices
[firstslice
];
2484 slicey
= tiley
% tile
->sliceheight
;
2489 dh
= slice
->h
- slicey
;
2490 dh
= fz_mini (disph
, dh
);
2491 uploadslice (tile
, slice
);
2493 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2494 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2495 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2496 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2498 vertices
[0] = dispx
; vertices
[1] = dispy
;
2499 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2500 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2501 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2503 #if TEXT_TYPE == GL_TEXTURE_2D
2504 for (int i
= 0; i
< 8; ++i
) {
2505 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2509 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2513 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2519 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2521 fz_matrix ctm
, tm
, pm
;
2522 fz_point p1
, p2
, p3
, p4
;
2523 GLfloat
*vertices
= state
.vertices
;
2524 double *v
= (double *) rects_v
;
2526 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2527 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2528 fz_translate (&tm
, xoff
, yoff
);
2529 pm
= pagectm (page
);
2530 fz_concat (&ctm
, &pm
, &tm
);
2532 glEnable (GL_BLEND
);
2533 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2536 p1
.x
= (float) v
[4];
2537 p1
.y
= (float) v
[5];
2539 p2
.x
= (float) v
[6];
2540 p2
.y
= (float) v
[5];
2542 p3
.x
= (float) v
[6];
2543 p3
.y
= (float) v
[7];
2545 p4
.x
= (float) v
[4];
2546 p4
.y
= (float) v
[7];
2547 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2548 glDisable (GL_BLEND
);
2551 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2552 value xoff_v
, value yoff_v
,
2555 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2556 int xoff
= Int_val (xoff_v
);
2557 int yoff
= Int_val (yoff_v
);
2558 int noff
= Int_val (Field (li_v
, 0));
2559 char *targ
= String_val (Field (li_v
, 1));
2560 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2561 int hfsize
= Int_val (Field (li_v
, 2));
2562 char *s
= String_val (ptr_v
);
2563 int hlmask
= Int_val (hlinks_v
);
2564 struct page
*page
= parse_pointer (__func__
, s
);
2566 if (!page
->fzpage
) {
2567 /* deal with loadpage failed pages */
2571 if (trylock (__func__
)) {
2576 ensureannots (page
);
2577 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2579 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2580 noff
= page
->slinkcount
;
2582 if (page
->tgen
== state
.gen
) {
2583 showsel (page
, xoff
, yoff
);
2588 CAMLreturn (Val_int (noff
));
2591 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2594 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2595 int xoff
= Int_val (xoff_v
);
2596 int yoff
= Int_val (yoff_v
);
2597 char *s
= String_val (ptr_v
);
2598 struct page
*page
= parse_pointer (__func__
, s
);
2600 drawprect (page
, xoff
, yoff
, rects_v
);
2604 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2608 const fz_matrix
*tctm
;
2609 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2611 if (!page
->annots
) return NULL
;
2614 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2615 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2618 tctm
= &fz_identity
;
2624 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2625 fz_invert_matrix (&ctm
, &ctm
);
2626 fz_transform_point (&p
, &ctm
);
2629 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2630 struct annot
*a
= &page
->annots
[i
];
2633 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2634 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2635 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2643 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2649 ensureslinks (page
);
2654 ctm
= pagectm (page
);
2655 fz_invert_matrix (&ctm
, &ctm
);
2656 fz_transform_point (&p
, &ctm
);
2658 for (link
= page
->links
; link
; link
= link
->next
) {
2659 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2660 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2668 static void ensuretext (struct page
*page
)
2670 if (state
.gen
!= page
->tgen
) {
2672 page
->tgen
= state
.gen
;
2678 page
->text
= fz_new_stext_page (state
.ctx
,
2679 &state
.pagedims
[page
->pdimno
].mediabox
);
2680 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2681 ctm
= pagectm (page
);
2682 fz_run_display_list (state
.ctx
, page
->dlist
,
2683 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2684 fz_close_device (state
.ctx
, tdev
);
2685 fz_drop_device (state
.ctx
, tdev
);
2689 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2691 CAMLparam2 (start_page_v
, dir_v
);
2693 int i
, dir
= Int_val (dir_v
);
2694 int start_page
= Int_val (start_page_v
);
2695 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2699 ret_v
= Val_int (0);
2701 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2702 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2707 pdf_page
*page
= NULL
;
2710 fz_try (state
.ctx
) {
2711 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2712 found
= !!page
->links
|| !!page
->annots
;
2714 fz_catch (state
.ctx
) {
2718 fz_drop_page (state
.ctx
, &page
->super
);
2722 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2723 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2725 fz_drop_link (state
.ctx
, link
);
2726 fz_drop_page (state
.ctx
, page
);
2730 ret_v
= caml_alloc_small (1, 1);
2731 Field (ret_v
, 0) = Val_int (i
);
2740 enum { dir_first
, dir_last
};
2741 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2743 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2745 CAMLparam2 (ptr_v
, dir_v
);
2746 CAMLlocal2 (ret_v
, pos_v
);
2748 int dirtag
, i
, slinkindex
;
2749 struct slink
*found
= NULL
,*slink
;
2750 char *s
= String_val (ptr_v
);
2752 page
= parse_pointer (__func__
, s
);
2753 ret_v
= Val_int (0);
2755 ensureslinks (page
);
2757 if (Is_block (dir_v
)) {
2758 dirtag
= Tag_val (dir_v
);
2760 case dir_first_visible
:
2762 int x0
, y0
, dir
, first_index
, last_index
;
2764 pos_v
= Field (dir_v
, 0);
2765 x0
= Int_val (Field (pos_v
, 0));
2766 y0
= Int_val (Field (pos_v
, 1));
2767 dir
= Int_val (Field (pos_v
, 2));
2772 last_index
= page
->slinkcount
;
2775 first_index
= page
->slinkcount
- 1;
2779 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2780 slink
= &page
->slinks
[i
];
2781 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2790 slinkindex
= Int_val (Field (dir_v
, 0));
2791 found
= &page
->slinks
[slinkindex
];
2792 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2793 slink
= &page
->slinks
[i
];
2794 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2802 slinkindex
= Int_val (Field (dir_v
, 0));
2803 found
= &page
->slinks
[slinkindex
];
2804 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2805 slink
= &page
->slinks
[i
];
2806 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2814 slinkindex
= Int_val (Field (dir_v
, 0));
2815 found
= &page
->slinks
[slinkindex
];
2816 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2817 slink
= &page
->slinks
[i
];
2818 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2826 slinkindex
= Int_val (Field (dir_v
, 0));
2827 found
= &page
->slinks
[slinkindex
];
2828 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2829 slink
= &page
->slinks
[i
];
2830 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2839 dirtag
= Int_val (dir_v
);
2842 found
= page
->slinks
;
2847 found
= page
->slinks
+ (page
->slinkcount
- 1);
2853 ret_v
= caml_alloc_small (2, 1);
2854 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2861 enum { uuri
, utext
, uannot
};
2863 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2865 CAMLparam2 (ptr_v
, n_v
);
2866 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2869 char *s
= String_val (ptr_v
);
2870 struct slink
*slink
;
2872 ret_v
= Val_int (0);
2873 page
= parse_pointer (__func__
, s
);
2876 ensureslinks (page
);
2877 slink
= &page
->slinks
[Int_val (n_v
)];
2878 if (slink
->tag
== SLINK
) {
2879 link
= slink
->u
.link
;
2880 str_v
= caml_copy_string (link
->uri
);
2881 ret_v
= caml_alloc_small (1, uuri
);
2882 Field (ret_v
, 0) = str_v
;
2885 ret_v
= caml_alloc_small (1, uannot
);
2886 tup_v
= caml_alloc_tuple (2);
2887 Field (ret_v
, 0) = tup_v
;
2888 Field (tup_v
, 0) = ptr_v
;
2889 Field (tup_v
, 1) = n_v
;
2896 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2898 CAMLparam2 (ptr_v
, n_v
);
2901 char *contents
= NULL
;
2904 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2906 char *s
= String_val (ptr_v
);
2908 struct slink
*slink
;
2910 page
= parse_pointer (__func__
, s
);
2911 slink
= &page
->slinks
[Int_val (n_v
)];
2912 contents
= pdf_copy_annot_contents (state
.ctx
,
2913 (pdf_annot
*) slink
->u
.annot
);
2917 ret_v
= caml_copy_string (contents
);
2918 fz_free (state
.ctx
, contents
);
2921 ret_v
= caml_copy_string ("");
2926 CAMLprim value
ml_getlinkcount (value ptr_v
)
2930 char *s
= String_val (ptr_v
);
2932 page
= parse_pointer (__func__
, s
);
2933 CAMLreturn (Val_int (page
->slinkcount
));
2936 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2938 CAMLparam2 (ptr_v
, n_v
);
2941 struct slink
*slink
;
2942 char *s
= String_val (ptr_v
);
2944 page
= parse_pointer (__func__
, s
);
2945 ret_v
= caml_alloc_tuple (4);
2947 ensureslinks (page
);
2949 slink
= &page
->slinks
[Int_val (n_v
)];
2950 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2951 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2952 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2953 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2958 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2960 CAMLparam3 (ptr_v
, x_v
, y_v
);
2961 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2963 struct annot
*annot
;
2965 char *ptr
= String_val (ptr_v
);
2966 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2967 struct pagedim
*pdim
;
2969 ret_v
= Val_int (0);
2970 if (trylock (__func__
)) {
2974 page
= parse_pointer (__func__
, ptr
);
2975 pdim
= &state
.pagedims
[page
->pdimno
];
2976 x
+= pdim
->bounds
.x0
;
2977 y
+= pdim
->bounds
.y0
;
2980 annot
= getannot (page
, x
, y
);
2984 ensureslinks (page
);
2985 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2986 if (page
->slinks
[i
].tag
== SANNOT
2987 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2992 ret_v
= caml_alloc_small (1, uannot
);
2993 tup_v
= caml_alloc_tuple (2);
2994 Field (ret_v
, 0) = tup_v
;
2995 Field (tup_v
, 0) = ptr_v
;
2996 Field (tup_v
, 1) = Val_int (n
);
3001 link
= getlink (page
, x
, y
);
3003 str_v
= caml_copy_string (link
->uri
);
3004 ret_v
= caml_alloc_small (1, uuri
);
3005 Field (ret_v
, 0) = str_v
;
3009 fz_stext_block
*block
;
3013 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3014 fz_stext_line
*line
;
3016 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3018 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3021 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3025 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3028 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3031 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3032 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
3033 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3036 if (!n2
) n2
= "<unknown font>";
3038 if (face
&& face
->family_name
) {
3040 char *n1
= face
->family_name
;
3041 size_t l1
= strlen (n1
);
3042 size_t l2
= strlen (n2
);
3044 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3045 s
= malloc (l1
+ l2
+ 2);
3049 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3050 str_v
= caml_copy_string (s
);
3055 if (str_v
== Val_unit
) {
3056 str_v
= caml_copy_string (n2
);
3058 ret_v
= caml_alloc_small (1, utext
);
3059 Field (ret_v
, 0) = str_v
;
3073 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3075 static int uninteresting (int c
)
3077 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3081 CAMLprim
void ml_clearmark (value ptr_v
)
3084 char *s
= String_val (ptr_v
);
3087 if (trylock (__func__
)) {
3091 page
= parse_pointer (__func__
, s
);
3092 page
->fmark
.ch
= NULL
;
3093 page
->lmark
.ch
= NULL
;
3100 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3102 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3106 fz_stext_line
*line
;
3107 fz_stext_block
*block
;
3108 struct pagedim
*pdim
;
3109 int mark
= Int_val (mark_v
);
3110 char *s
= String_val (ptr_v
);
3111 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3113 ret_v
= Val_bool (0);
3114 if (trylock (__func__
)) {
3118 page
= parse_pointer (__func__
, s
);
3119 pdim
= &state
.pagedims
[page
->pdimno
];
3123 if (mark
== mark_page
) {
3124 page
->fmark
.ch
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
3125 page
->lmark
.ch
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
3126 ret_v
= Val_bool (1);
3130 x
+= pdim
->bounds
.x0
;
3131 y
+= pdim
->bounds
.y0
;
3133 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3134 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3136 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3139 if (mark
== mark_block
) {
3140 page
->fmark
.ch
= block
->u
.t
.first_line
->first_char
;
3141 page
->lmark
.ch
= block
->u
.t
.last_line
->last_char
;
3142 ret_v
= Val_bool (1);
3146 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3150 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3153 if (mark
== mark_line
) {
3154 page
->fmark
.ch
= line
->first_char
;
3155 page
->lmark
.ch
= line
->last_char
;
3156 ret_v
= Val_bool (1);
3160 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3161 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
3163 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3164 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
3165 if (uninteresting (ch2
->c
)) first
= NULL
;
3166 else if (!first
) first
= ch2
;
3168 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
3169 if (uninteresting (ch2
->c
)) break;
3173 page
->fmark
.ch
= first
;
3174 page
->lmark
.ch
= last
;
3175 ret_v
= Val_bool (1);
3182 if (!Bool_val (ret_v
)) {
3183 page
->fmark
.ch
= NULL
;
3184 page
->lmark
.ch
= NULL
;
3192 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3194 CAMLparam3 (ptr_v
, x_v
, y_v
);
3195 CAMLlocal2 (ret_v
, res_v
);
3198 struct pagedim
*pdim
;
3199 fz_stext_block
*block
;
3200 char *s
= String_val (ptr_v
);
3201 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3203 ret_v
= Val_int (0);
3204 if (trylock (__func__
)) {
3208 page
= parse_pointer (__func__
, s
);
3209 pdim
= &state
.pagedims
[page
->pdimno
];
3210 x
+= pdim
->bounds
.x0
;
3211 y
+= pdim
->bounds
.y0
;
3215 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3216 switch (block
->type
) {
3217 case FZ_STEXT_BLOCK_TEXT
:
3221 case FZ_STEXT_BLOCK_IMAGE
:
3229 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3234 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3235 ret_v
= caml_alloc_small (1, 1);
3236 Store_double_field (res_v
, 0, (double) b
->x0
);
3237 Store_double_field (res_v
, 1, (double) b
->x1
);
3238 Store_double_field (res_v
, 2, (double) b
->y0
);
3239 Store_double_field (res_v
, 3, (double) b
->y1
);
3240 Field (ret_v
, 0) = res_v
;
3248 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3250 CAMLparam2 (ptr_v
, rect_v
);
3253 struct pagedim
*pdim
;
3254 char *s
= String_val (ptr_v
);
3257 fz_stext_line
*line
;
3258 fz_stext_block
*block
;
3259 fz_stext_char
*fc
, *lc
;
3261 if (trylock (__func__
)) {
3265 page
= parse_pointer (__func__
, s
);
3268 pdim
= &state
.pagedims
[page
->pdimno
];
3269 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3270 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3271 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3272 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3282 fc
= page
->fmark
.ch
;
3283 lc
= page
->lmark
.ch
;
3285 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3286 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3287 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3288 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3290 if (x0
>= b
.x0
&& x0
<= b
.x1
&& y0
>= b
.y0
&& y0
<= b
.y1
) {
3293 if (x1
>= b
.x0
&& x1
<= b
.x1
&& y1
>= b
.y0
&& y1
<= b
.y1
) {
3299 if (x1
< x0
&& fc
== lc
) {
3307 page
->fmark
.ch
= fc
;
3308 page
->lmark
.ch
= lc
;
3316 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3322 len
= fz_runetochar (buf
, ch
->c
);
3323 ret
= fwrite (buf
, len
, 1, f
);
3325 fprintf (stderr
, "failed to write %d bytes ret=%zu: %s\n",
3326 len
, ret
, strerror (errno
));
3333 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3335 caml_failwith ("ml_popen not implemented under Cygwin");
3338 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3340 CAMLparam2 (command_v
, fds_v
);
3341 CAMLlocal2 (l_v
, tup_v
);
3343 pid_t pid
= (pid_t
) -1;
3345 value earg_v
= Nothing
;
3346 posix_spawnattr_t attr
;
3347 posix_spawn_file_actions_t fa
;
3348 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3350 argv
[2] = String_val (command_v
);
3352 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3353 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3356 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3357 msg
= "posix_spawnattr_init";
3361 #ifdef POSIX_SPAWN_USEVFORK
3362 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3363 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3368 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3371 tup_v
= Field (l_v
, 0);
3372 fd1
= Int_val (Field (tup_v
, 0));
3373 fd2
= Int_val (Field (tup_v
, 1));
3375 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3376 msg
= "posix_spawn_file_actions_addclose";
3382 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3383 msg
= "posix_spawn_file_actions_adddup2";
3390 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3391 msg
= "posix_spawn";
3396 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3397 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3401 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3402 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3407 unix_error (ret
, msg
, earg_v
);
3409 CAMLreturn (Val_int (pid
));
3413 CAMLprim value
ml_hassel (value ptr_v
)
3418 char *s
= String_val (ptr_v
);
3420 ret_v
= Val_bool (0);
3421 if (trylock (__func__
)) {
3425 page
= parse_pointer (__func__
, s
);
3426 ret_v
= Val_bool (page
->fmark
.ch
&& page
->lmark
.ch
);
3432 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3434 CAMLparam2 (fd_v
, ptr_v
);
3438 fz_stext_line
*line
;
3439 fz_stext_block
*block
;
3440 int fd
= Int_val (fd_v
);
3441 char *s
= String_val (ptr_v
);
3443 if (trylock (__func__
)) {
3447 page
= parse_pointer (__func__
, s
);
3449 if (!page
->fmark
.ch
|| !page
->lmark
.ch
) {
3450 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3454 f
= fdopen (fd
, "w");
3456 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3457 fd
, strerror (errno
));
3461 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3462 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3463 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3465 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3466 if (seen
|| ch
== page
->fmark
.ch
) {
3469 if (ch
== page
->lmark
.ch
) goto close
;
3470 } while ((ch
= ch
->next
));
3475 if (seen
) fputc ('\n', f
);
3480 int ret
= fclose (f
);
3483 if (errno
!= ECHILD
) {
3484 fprintf (stderr
, "failed to close sel pipe: %s\n",
3495 fprintf (stderr
, "failed to close sel pipe: %s\n",
3502 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3504 CAMLparam1 (pagedimno_v
);
3506 int pagedimno
= Int_val (pagedimno_v
);
3509 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3510 if (trylock (__func__
)) {
3511 box
= fz_empty_rect
;
3514 box
= state
.pagedims
[pagedimno
].mediabox
;
3518 Store_double_field (ret_v
, 0, (double) box
.x0
);
3519 Store_double_field (ret_v
, 1, (double) box
.x1
);
3520 Store_double_field (ret_v
, 2, (double) box
.y0
);
3521 Store_double_field (ret_v
, 3, (double) box
.y1
);
3526 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3527 value dw_v
, value cols_v
)
3529 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3535 float winw
= Int_val (winw_v
);
3536 float winh
= Int_val (winh_v
);
3537 float dw
= Int_val (dw_v
);
3538 float cols
= Int_val (cols_v
);
3539 float pw
= 1.0, ph
= 1.0;
3541 if (trylock (__func__
)) {
3545 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3546 float w
= p
->pagebox
.x1
/ cols
;
3547 float h
= p
->pagebox
.y1
;
3551 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3553 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3556 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3559 ret_v
= caml_copy_double ((double) zoom
);
3563 CAMLprim value
ml_getmaxw (value unit_v
)
3565 CAMLparam1 (unit_v
);
3571 if (trylock (__func__
)) {
3575 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3576 float w
= p
->pagebox
.x1
;
3577 maxw
= fz_max (maxw
, w
);
3582 ret_v
= caml_copy_double ((double) maxw
);
3586 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3588 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3590 int pt
= Int_val(pt_v
);
3591 int x
= Int_val (x_v
);
3592 int y
= Int_val (y_v
);
3595 w
= (double) draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3596 ret_v
= caml_copy_double (w
);
3600 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3602 CAMLparam2 (pt_v
, string_v
);
3604 int pt
= Int_val (pt_v
);
3607 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3608 ret_v
= caml_copy_double (w
);
3612 CAMLprim value
ml_getpagebox (value opaque_v
)
3614 CAMLparam1 (opaque_v
);
3620 char *s
= String_val (opaque_v
);
3621 struct page
*page
= parse_pointer (__func__
, s
);
3623 ret_v
= caml_alloc_tuple (4);
3624 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3626 ctm
= pagectm (page
);
3627 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3629 fz_close_device (state
.ctx
, dev
);
3630 fz_drop_device (state
.ctx
, dev
);
3631 fz_round_rect (&bbox
, &rect
);
3632 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3633 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3634 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3635 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3640 CAMLprim
void ml_setaalevel (value level_v
)
3642 CAMLparam1 (level_v
);
3644 state
.aalevel
= Int_val (level_v
);
3649 #pragma GCC diagnostic push
3650 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3651 #include <X11/Xlib.h>
3652 #include <X11/cursorfont.h>
3653 #pragma GCC diagnostic pop
3656 #include <EGL/egl.h>
3661 static const int shapes
[] = {
3662 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3665 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3678 XVisualInfo
*visual
;
3679 Cursor curs
[CURS_COUNT
];
3683 static void initcurs (void)
3685 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3686 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3690 CAMLprim
void ml_setbgcol (value color_v
)
3692 CAMLparam1 (color_v
);
3693 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3698 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3700 CAMLparam3 (display_v
, wid_v
, screen_v
);
3704 EGLint attribs
[] = {
3705 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3706 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3711 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3713 caml_failwith ("XOpenDisplay");
3716 eglBindAPI (EGL_OPENGL_API
);
3718 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3719 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3720 caml_failwith ("eglGetDisplay");
3723 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3724 caml_failwith ("eglInitialize");
3727 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3729 caml_failwith ("eglChooseConfig");
3732 if (!eglGetConfigAttrib (glx
.edpy
, conf
, EGL_NATIVE_VISUAL_ID
, &visid
)) {
3733 caml_failwith ("eglGetConfigAttrib");
3739 glx
.wid
= Int_val (wid_v
);
3740 CAMLreturn (Val_int (visid
));
3743 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3745 CAMLparam1 (unit_v
);
3747 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3749 caml_failwith ("eglCreateContext");
3752 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3754 if (glx
.win
== EGL_NO_SURFACE
) {
3755 caml_failwith ("eglCreateWindowSurface");
3758 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3760 caml_failwith ("eglMakeCurrent");
3765 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3767 CAMLparam3 (display_v
, wid_v
, screen_v
);
3769 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3771 caml_failwith ("XOpenDisplay");
3774 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3775 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3777 XCloseDisplay (glx
.dpy
);
3778 caml_failwith ("glXChooseVisual");
3783 glx
.wid
= Int_val (wid_v
);
3784 CAMLreturn (Val_int (glx
.visual
->visualid
));
3787 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3789 CAMLparam1 (unit_v
);
3791 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3793 caml_failwith ("glXCreateContext");
3799 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3800 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3802 caml_failwith ("glXMakeCurrent");
3808 CAMLprim
void ml_setcursor (value cursor_v
)
3810 CAMLparam1 (cursor_v
);
3811 size_t cursn
= Int_val (cursor_v
);
3813 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
3814 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
3819 CAMLprim
void ml_swapb (value unit_v
)
3821 CAMLparam1 (unit_v
);
3823 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
3824 caml_failwith ("eglSwapBuffers");
3827 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3832 #pragma GCC diagnostic push
3833 #pragma GCC diagnostic ignored "-Wmissing-variable-declarations"
3834 #include "keysym2ucs.c"
3835 #pragma GCC diagnostic pop
3837 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3839 CAMLparam1 (keysym_v
);
3841 KeySym keysym
= Int_val (keysym_v
);
3846 rune
= (Rune
) keysym2ucs (keysym
);
3847 len
= fz_runetochar (buf
, rune
);
3849 str_v
= caml_copy_string (buf
);
3853 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3855 CAMLparam1 (keysym_v
);
3857 long ucs_v
= Long_val (keysym_v
);
3861 len
= fz_runetochar (buf
, ucs_v
);
3863 str_v
= caml_copy_string (buf
);
3868 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
3870 CAMLprim value
ml_platform (value unit_v
)
3872 CAMLparam1 (unit_v
);
3873 CAMLlocal2 (tup_v
, arr_v
);
3874 int platid
= piunknown
;
3877 #if defined __linux__
3879 #elif defined __CYGWIN__
3881 #elif defined __DragonFly__ || defined __FreeBSD__
3882 || defined __OpenBSD__
|| defined __NetBSD__
3884 #elif defined __sun__
3886 #elif defined __APPLE__
3889 if (uname (&buf
)) err (1, "uname");
3891 tup_v
= caml_alloc_tuple (2);
3893 char const *sar
[] = {
3900 arr_v
= caml_copy_string_array (sar
);
3902 Field (tup_v
, 0) = Val_int (platid
);
3903 Field (tup_v
, 1) = arr_v
;
3907 CAMLprim
void ml_cloexec (value fd_v
)
3910 int fd
= Int_val (fd_v
);
3912 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3913 uerror ("fcntl", Nothing
);
3918 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3920 CAMLparam2 (w_v
, h_v
);
3923 int w
= Int_val (w_v
);
3924 int h
= Int_val (h_v
);
3925 int cs
= Int_val (cs_v
);
3927 if (state
.bo_usable
) {
3928 pbo
= calloc (sizeof (*pbo
), 1);
3930 err (1, "calloc pbo");
3942 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3945 state
.glGenBuffersARB (1, &pbo
->id
);
3946 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3947 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3948 NULL
, GL_STREAM_DRAW
);
3949 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3951 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3953 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3954 state
.glDeleteBuffersARB (1, &pbo
->id
);
3956 ret_v
= caml_copy_string ("0");
3962 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3964 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3968 err (1, "malloc %d bytes failed", res
+1);
3970 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
3972 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
3974 ret_v
= caml_copy_string (s
);
3979 ret_v
= caml_copy_string ("0");
3984 CAMLprim
void ml_freepbo (value s_v
)
3987 char *s
= String_val (s_v
);
3988 struct tile
*tile
= parse_pointer (__func__
, s
);
3991 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3993 tile
->pbo
->ptr
= NULL
;
3994 tile
->pbo
->size
= -1;
3999 CAMLprim
void ml_unmappbo (value s_v
)
4002 char *s
= String_val (s_v
);
4003 struct tile
*tile
= parse_pointer (__func__
, s
);
4006 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4007 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4008 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4010 tile
->pbo
->ptr
= NULL
;
4011 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4016 static void setuppbo (void)
4019 static CFBundleRef framework
= NULL
;
4020 if (framework
== NULL
)
4021 framework
= CFBundleGetBundleWithIdentifier (CFSTR ("com.apple.opengl"));
4022 #define GGPA(n) (&state.n = CFBundleGetFunctionPointerForName (framework, CFSTR (#n)))
4025 #define GGPA(n) (*(void (**) (void)) &state.n = eglGetProcAddress (#n))
4027 #define GGPA(n) (*(void (**) (void)) &state.n = glXGetProcAddress ((GLubyte *) #n))
4029 state
.bo_usable
= GGPA (glBindBufferARB
)
4030 && GGPA (glUnmapBufferARB
)
4031 && GGPA (glMapBufferARB
)
4032 && GGPA (glBufferDataARB
)
4033 && GGPA (glGenBuffersARB
)
4034 && GGPA (glDeleteBuffersARB
);
4039 CAMLprim value
ml_bo_usable (value unit_v
)
4041 CAMLparam1 (unit_v
);
4042 CAMLreturn (Val_bool (state
.bo_usable
));
4045 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4047 CAMLparam3 (ptr_v
, x_v
, y_v
);
4048 CAMLlocal2 (ret_v
, tup_v
);
4050 char *s
= String_val (ptr_v
);
4051 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4052 struct pagedim
*pdim
;
4056 page
= parse_pointer (__func__
, s
);
4057 pdim
= &state
.pagedims
[page
->pdimno
];
4059 ret_v
= Val_int (0);
4060 if (trylock (__func__
)) {
4064 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4065 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4066 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4071 p
.x
= x
+ pdim
->bounds
.x0
;
4072 p
.y
= y
+ pdim
->bounds
.y0
;
4074 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4075 fz_invert_matrix (&ctm
, &ctm
);
4076 fz_transform_point (&p
, &ctm
);
4078 tup_v
= caml_alloc_tuple (2);
4079 ret_v
= caml_alloc_small (1, 1);
4080 Field (tup_v
, 0) = Val_int (p
.x
);
4081 Field (tup_v
, 1) = Val_int (p
.y
);
4082 Field (ret_v
, 0) = tup_v
;
4089 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4090 value x_v
, value y_v
)
4092 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4095 char *s
= String_val (ptr_v
);
4096 int pageno
= Int_val (pageno_v
);
4097 int pdimno
= Int_val (pdimno_v
);
4098 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
4099 struct pagedim
*pdim
;
4103 ret_v
= Val_int (0);
4107 page
= loadpage (pageno
, pdimno
);
4110 page
= parse_pointer (__func__
, s
);
4112 pdim
= &state
.pagedims
[pdimno
];
4114 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4115 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4116 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4121 p
.x
= x
+ pdim
->bounds
.x0
;
4122 p
.y
= y
+ pdim
->bounds
.y0
;
4124 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4125 fz_transform_point (&p
, &ctm
);
4127 ret_v
= caml_alloc_tuple (2);
4128 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
4129 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
4138 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
4141 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4142 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4148 char *s
= String_val (ptr_v
);
4150 page
= parse_pointer (__func__
, s
);
4151 annot
= pdf_create_annot (state
.ctx
,
4152 pdf_page_from_fz_page (state
.ctx
,
4155 p
.x
= Int_val (x_v
);
4156 p
.y
= Int_val (y_v
);
4157 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4158 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4164 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
4166 CAMLparam2 (ptr_v
, n_v
);
4167 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4171 char *s
= String_val (ptr_v
);
4172 struct slink
*slink
;
4174 page
= parse_pointer (__func__
, s
);
4175 slink
= &page
->slinks
[Int_val (n_v
)];
4176 pdf_delete_annot (state
.ctx
,
4177 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4178 (pdf_annot
*) slink
->u
.annot
);
4184 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
4186 CAMLparam3 (ptr_v
, n_v
, str_v
);
4187 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4191 char *s
= String_val (ptr_v
);
4192 struct slink
*slink
;
4194 page
= parse_pointer (__func__
, s
);
4195 slink
= &page
->slinks
[Int_val (n_v
)];
4196 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4197 String_val (str_v
));
4203 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4205 CAMLparam1 (unit_v
);
4206 CAMLreturn (Val_bool (state
.dirty
));
4209 CAMLprim
void ml_savedoc (value path_v
)
4211 CAMLparam1 (path_v
);
4212 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4215 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4220 static void makestippletex (void)
4222 const char pixels
[] = "\xff\xff\0\0";
4223 glGenTextures (1, &state
.stid
);
4224 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4225 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4226 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4239 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4241 return caml_copy_string (FZ_VERSION
);
4244 CAMLprim
void ml_init (value csock_v
, value params_v
)
4246 CAMLparam2 (csock_v
, params_v
);
4247 CAMLlocal2 (trim_v
, fuzz_v
);
4255 /* Without following call to setlocale mbstowcs fails for, at
4256 least, strings containing chinese symbols (δΈ for instance)
4257 (with glibc citing EILSEQ="Invalid or incomplete multibyte or
4258 wide character" as the reason of failure and with macOS
4259 producing bogus output) */
4260 if (setlocale (LC_CTYPE
, "")) {
4261 /* Following two lines were taken from dvtm/vt.c */
4262 const char *cset
= nl_langinfo (CODESET
);
4263 state
.utf8cs
= !strcmp (cset
, "UTF-8");
4266 fprintf (stderr
, "setlocale failed\n");
4269 state
.csock
= Int_val (csock_v
);
4270 state
.rotate
= Int_val (Field (params_v
, 0));
4271 state
.fitmodel
= Int_val (Field (params_v
, 1));
4272 trim_v
= Field (params_v
, 2);
4273 texcount
= Int_val (Field (params_v
, 3));
4274 state
.sliceheight
= Int_val (Field (params_v
, 4));
4275 mustoresize
= Int_val (Field (params_v
, 5));
4276 colorspace
= Int_val (Field (params_v
, 6));
4277 fontpath
= String_val (Field (params_v
, 7));
4279 if (caml_string_length (Field (params_v
, 8)) > 0) {
4280 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4282 if (!state
.trimcachepath
) {
4283 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4288 haspboext
= Bool_val (Field (params_v
, 9));
4290 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4291 fz_register_document_handlers (state
.ctx
);
4293 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4294 fuzz_v
= Field (trim_v
, 1);
4295 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4296 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4297 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4298 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4300 set_tex_params (colorspace
);
4303 state
.face
= load_font (fontpath
);
4307 const unsigned char *data
;
4309 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
4310 state
.face
= load_builtin_font (data
, len
);
4312 if (!state
.face
) _exit (1);
4314 realloctexts (texcount
);
4322 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4324 errx (1, "pthread_create: %s", strerror (ret
));
4331 static void UNUSED_ATTR refmacs
OPTIMIZE_ATTR (0) (void) {}