1 /* lots of code c&p-ed directly from mupdf */
3 #pragma GCC diagnostic error "-Weverything"
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 #pragma GCC diagnostic ignored "-Wdouble-promotion"
13 extern char **environ
;
15 #define CAML_NAME_SPACE
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/utsname.h>
48 #include <CoreFoundation/CoreFoundation.h>
49 #include <OpenGL/gl.h>
54 #pragma GCC diagnostic push
56 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
58 #pragma GCC diagnostic ignored "-Wpedantic"
59 #include <caml/fail.h>
60 #include <caml/alloc.h>
61 #include <caml/memory.h>
62 #include <caml/unixsupport.h>
64 #pragma GCC diagnostic push
65 #pragma GCC diagnostic ignored "-Wfloat-equal"
66 #include <mupdf/fitz.h>
67 #include <mupdf/pdf.h>
68 #pragma GCC diagnostic pop
71 #include FT_FREETYPE_H
72 #pragma GCC diagnostic pop
77 #define CACHE_PAGEREFS
80 #define TEXT_TYPE GL_TEXTURE_2D
82 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
86 #define lprintf printf
91 #define ARSERT(cond) for (;;) { \
93 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
109 struct slice slices
[1];
120 fz_matrix ctm
, zoomctm
, tctm
;
124 enum { SLINK
, SANNOT
} tag
;
145 fz_display_list
*dlist
;
148 struct slink
*slinks
;
150 struct annot
*annots
;
151 fz_stext_char
*fmark
, *lmark
;
154 enum { FitWidth
, FitProportional
, FitPage
};
158 struct pagedim
*pagedims
;
173 fz_colorspace
*colorspace
;
203 void (*glBindBufferARB
) (GLenum
, GLuint
);
204 GLboolean (*glUnmapBufferARB
) (GLenum
);
205 void *(*glMapBufferARB
) (GLenum
, GLenum
);
206 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
207 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
208 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
210 GLfloat texcoords
[8];
211 GLfloat vertices
[16];
213 #ifdef CACHE_PAGEREFS
230 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
232 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
235 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
237 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
240 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
242 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
243 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
246 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
248 static void lock (const char *cap
)
250 int ret
= pthread_mutex_lock (&mutex
);
252 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
256 static void unlock (const char *cap
)
258 int ret
= pthread_mutex_unlock (&mutex
);
260 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
264 static int trylock (const char *cap
)
266 int ret
= pthread_mutex_trylock (&mutex
);
267 if (ret
&& ret
!= EBUSY
) {
268 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
273 static int hasdata (void)
276 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
277 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
281 CAMLprim value
ml_hasdata (value fd_v
)
286 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
287 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
288 CAMLreturn (Val_bool (avail
> 0));
291 static void readdata (int fd
, void *p
, int size
)
296 n
= read (fd
, p
, size
);
298 if (n
< 0 && errno
== EINTR
) goto again
;
299 if (!n
) errx (1, "EOF while reading");
300 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
304 static void writedata (int fd
, char *p
, int size
)
307 uint32_t size4
= size
;
308 struct iovec iov
[2] = {
309 { .iov_base
= &size4
, .iov_len
= 4 },
310 { .iov_base
= p
, .iov_len
= size
}
314 n
= writev (fd
, iov
, 2);
315 if (n
< 0 && errno
== EINTR
) goto again
;
317 if (!n
) errx (1, "EOF while writing data");
318 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
322 static int readlen (int fd
)
325 readdata (fd
, &u
, 4);
329 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
331 CAMLparam3 (fd_v
, bytes_v
, len_v
);
332 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
336 CAMLprim value
ml_rcmd (value fd_v
)
339 CAMLlocal1 (strdata_v
);
340 int fd
= Int_val (fd_v
);
341 int len
= readlen (fd
);
342 strdata_v
= caml_alloc_string (len
);
343 readdata (fd
, String_val (strdata_v
), len
);
344 CAMLreturn (strdata_v
);
347 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
350 int size
= sizeof (fbuf
), len
;
356 len
= vsnprintf (buf
, size
, fmt
, ap
);
360 if (len
< size
- 4) {
361 writedata (state
.csock
, buf
, len
);
367 err (1, "vsnprintf for `%s' failed", fmt
);
369 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
370 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
372 if (buf
!= fbuf
) free (buf
);
375 static void closedoc (void)
377 #ifdef CACHE_PAGEREFS
378 if (state
.pdflut
.objs
) {
379 for (int i
= 0; i
< state
.pdflut
.count
; ++i
) {
380 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
382 free (state
.pdflut
.objs
);
383 state
.pdflut
.objs
= NULL
;
384 state
.pdflut
.idx
= 0;
388 fz_drop_document (state
.ctx
, state
.doc
);
393 static int openxref (char *filename
, char *password
, int layouth
)
395 for (int i
= 0; i
< state
.texcount
; ++i
) {
396 state
.texowners
[i
].w
= -1;
397 state
.texowners
[i
].slice
= NULL
;
403 if (state
.pagedims
) {
404 free (state
.pagedims
);
405 state
.pagedims
= NULL
;
407 state
.pagedimcount
= 0;
409 fz_set_aa_level (state
.ctx
, state
.aalevel
);
410 state
.doc
= fz_open_document (state
.ctx
, filename
);
411 if (fz_needs_password (state
.ctx
, state
.doc
)) {
412 if (password
&& !*password
) {
417 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
419 printd ("pass fail");
425 fz_layout_document (state
.ctx
, state
.doc
, 460, layouth
, 12);
426 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
430 static void docinfo (void)
432 struct { char *tag
; char *name
; } metatbl
[] = {
433 { FZ_META_INFO_TITLE
, "Title" },
434 { FZ_META_INFO_AUTHOR
, "Author" },
435 { FZ_META_FORMAT
, "Format" },
436 { FZ_META_ENCRYPTION
, "Encryption" },
437 { "info:Creator", "Creator" },
438 { "info:Producer", "Producer" },
439 { "info:CreationDate", "Creation date" },
444 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
447 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
448 metatbl
[i
].tag
, buf
, len
);
451 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
454 buf
= realloc (buf
, need
+ 1);
455 if (!buf
) err (1, "docinfo realloc %d", need
+ 1);
466 static void unlinktile (struct tile
*tile
)
468 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
469 struct slice
*s
= &tile
->slices
[i
];
471 if (s
->texindex
!= -1) {
472 if (state
.texowners
[s
->texindex
].slice
== s
) {
473 state
.texowners
[s
->texindex
].slice
= NULL
;
479 static void freepage (struct page
*page
)
483 fz_drop_stext_page (state
.ctx
, page
->text
);
488 fz_drop_display_list (state
.ctx
, page
->dlist
);
489 fz_drop_page (state
.ctx
, page
->fzpage
);
493 static void freetile (struct tile
*tile
)
498 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
501 fz_drop_pixmap (state
.ctx
, state
.pig
);
503 state
.pig
= tile
->pixmap
;
508 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
513 static void trimctm (pdf_page
*page
, int pindex
)
516 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
519 if (!pdim
->tctmready
) {
520 fz_rect realbox
, mediabox
;
521 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
523 fz_rotate (&rm
, -pdim
->rotate
);
524 fz_scale (&sm
, 1, -1);
525 fz_concat (&ctm
, &rm
, &sm
);
526 realbox
= pdim
->mediabox
;
527 fz_transform_rect (&realbox
, &ctm
);
528 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
529 fz_concat (&ctm1
, &ctm
, &tm
);
530 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
531 fz_invert_matrix (&im
, &page_ctm
);
532 fz_concat (&ctm
, &im
, &ctm1
);
538 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
541 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
543 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
544 if (pdf_specifics (state
.ctx
, state
.doc
)) {
545 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
546 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
549 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
550 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
555 static fz_matrix
pagectm (struct page
*page
)
557 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
560 static void *loadpage (int pageno
, int pindex
)
565 page
= calloc (sizeof (struct page
), 1);
567 err (1, "calloc page %d", pageno
);
570 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
571 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
573 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
574 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
577 fz_catch (state
.ctx
) {
580 fz_close_device (state
.ctx
, dev
);
581 fz_drop_device (state
.ctx
, dev
);
583 page
->pdimno
= pindex
;
584 page
->pageno
= pageno
;
585 page
->sgen
= state
.gen
;
586 page
->agen
= state
.gen
;
587 page
->tgen
= state
.gen
;
591 static struct tile
*alloctile (int h
)
597 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
598 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
599 tile
= calloc (tilesize
, 1);
601 err (1, "cannot allocate tile (%zu bytes)", tilesize
);
603 for (int i
= 0; i
< slicecount
; ++i
) {
604 int sh
= fz_mini (h
, state
.sliceheight
);
605 tile
->slices
[i
].h
= sh
;
606 tile
->slices
[i
].texindex
= -1;
609 tile
->slicecount
= slicecount
;
610 tile
->sliceheight
= state
.sliceheight
;
614 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
622 struct pagedim
*pdim
;
624 tile
= alloctile (h
);
625 pdim
= &state
.pagedims
[page
->pdimno
];
630 bbox
.x1
= bbox
.x0
+ w
;
631 bbox
.y1
= bbox
.y0
+ h
;
634 if (state
.pig
->w
== w
636 && state
.pig
->colorspace
== state
.colorspace
) {
637 tile
->pixmap
= state
.pig
;
638 tile
->pixmap
->x
= bbox
.x0
;
639 tile
->pixmap
->y
= bbox
.y0
;
642 fz_drop_pixmap (state
.ctx
, state
.pig
);
649 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
650 &bbox
, NULL
, 1, pbo
->ptr
);
655 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
,
662 fz_clear_pixmap_with_value (state
.ctx
, tile
->pixmap
, 0xff);
664 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
665 ctm
= pagectm (page
);
666 fz_rect_from_irect (&rect
, &bbox
);
667 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
668 fz_close_device (state
.ctx
, dev
);
669 fz_drop_device (state
.ctx
, dev
);
674 #ifdef CACHE_PAGEREFS
675 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
676 thanks to Robin Watts */
678 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
680 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
684 if (state
.pdflut
.idx
== state
.pagecount
) return;
686 kids
= pdf_dict_gets (ctx
, node
, "Kids");
687 len
= pdf_array_len (ctx
, kids
);
690 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
692 if (pdf_mark_obj (ctx
, node
))
693 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
694 for (int i
= 0; i
< len
; i
++) {
695 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
696 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
698 ? !strcmp (type
, "Pages")
699 : pdf_dict_gets (ctx
, kid
, "Kids")
700 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
701 pdf_collect_pages (doc
, kid
);
705 ? strcmp (type
, "Page") != 0
706 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
707 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
708 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
711 pdf_unmark_obj (ctx
, node
);
715 pdf_load_page_objs (pdf_document
*doc
)
717 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
718 pdf_trailer (state
.ctx
, doc
), "Root");
719 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
722 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
724 state
.pdflut
.idx
= 0;
725 pdf_collect_pages (doc
, node
);
729 static void initpdims (void)
733 fz_rect rootmediabox
= fz_empty_rect
;
734 int pageno
, trim
, show
;
735 int trimw
= 0, cxcount
;
736 fz_context
*ctx
= state
.ctx
;
737 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
744 if (state
.trimmargins
&& state
.trimcachepath
) {
745 trimf
= fopen (state
.trimcachepath
, "rb");
747 trimf
= fopen (state
.trimcachepath
, "wb");
752 if (state
.trimmargins
|| pdf
)
753 cxcount
= state
.pagecount
;
755 cxcount
= fz_mini (state
.pagecount
, 1);
759 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
760 "Root/Pages/MediaBox");
761 pdf_to_rect (ctx
, obj
, &rootmediabox
);
764 #ifdef CACHE_PAGEREFS
765 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
766 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
767 if (!state
.pdflut
.objs
) {
768 err (1, "malloc pageobjs %zu %d %zu failed",
769 sizeof (*state
.pdflut
.objs
), cxcount
,
770 sizeof (*state
.pdflut
.objs
) * cxcount
);
772 state
.pdflut
.count
= cxcount
;
773 pdf_load_page_objs (pdf
);
774 state
.pdflut
.pdf
= pdf
;
778 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
781 fz_rect mediabox
= fz_empty_rect
;
785 pdf_obj
*pageref
, *pageobj
;
787 #ifdef CACHE_PAGEREFS
788 pageref
= state
.pdflut
.objs
[pageno
];
790 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
792 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
793 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
795 if (state
.trimmargins
) {
800 page
= pdf_load_page (ctx
, pdf
, pageno
);
801 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
802 trim
= state
.trimanew
|| !obj
;
806 fz_matrix ctm
, page_ctm
;
808 dev
= fz_new_bbox_device (ctx
, &rect
);
809 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
810 fz_invert_matrix (&ctm
, &page_ctm
);
811 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
812 fz_close_device (ctx
, dev
);
813 fz_drop_device (ctx
, dev
);
815 rect
.x0
+= state
.trimfuzz
.x0
;
816 rect
.x1
+= state
.trimfuzz
.x1
;
817 rect
.y0
+= state
.trimfuzz
.y0
;
818 rect
.y1
+= state
.trimfuzz
.y1
;
819 fz_transform_rect (&rect
, &ctm
);
820 fz_intersect_rect (&rect
, &mediabox
);
822 if (!fz_is_empty_rect (&rect
)) {
826 obj
= pdf_new_array (ctx
, pdf
, 4);
827 pdf_array_push (ctx
, obj
,
828 pdf_new_real (ctx
, mediabox
.x0
));
829 pdf_array_push (ctx
, obj
,
830 pdf_new_real (ctx
, mediabox
.y0
));
831 pdf_array_push (ctx
, obj
,
832 pdf_new_real (ctx
, mediabox
.x1
));
833 pdf_array_push (ctx
, obj
,
834 pdf_new_real (ctx
, mediabox
.y1
));
835 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
838 mediabox
.x0
= pdf_to_real (ctx
,
839 pdf_array_get (ctx
, obj
, 0));
840 mediabox
.y0
= pdf_to_real (ctx
,
841 pdf_array_get (ctx
, obj
, 1));
842 mediabox
.x1
= pdf_to_real (ctx
,
843 pdf_array_get (ctx
, obj
, 2));
844 mediabox
.y1
= pdf_to_real (ctx
,
845 pdf_array_get (ctx
, obj
, 3));
848 fz_drop_page (ctx
, &page
->super
);
849 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
851 printd ("progress %f Trimming %d",
852 (double) (pageno
+ 1) / state
.pagecount
,
857 printd ("emsg failed to load page %d", pageno
);
865 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
867 if (fz_is_empty_rect (&mediabox
)) {
876 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
878 if (!fz_is_empty_rect (&cropbox
)) {
883 fz_intersect_rect (&mediabox
, &cropbox
);
888 if (fz_is_empty_rect (&rootmediabox
)) {
889 printd ("emsg cannot find page size for page %d",
893 mediabox
= rootmediabox
;
900 if (state
.trimmargins
&& trimw
) {
904 page
= fz_load_page (ctx
, state
.doc
, pageno
);
905 fz_bound_page (ctx
, page
, &mediabox
);
906 if (state
.trimmargins
) {
910 dev
= fz_new_bbox_device (ctx
, &rect
);
911 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
912 fz_close_device (ctx
, dev
);
913 fz_drop_device (ctx
, dev
);
915 rect
.x0
+= state
.trimfuzz
.x0
;
916 rect
.x1
+= state
.trimfuzz
.x1
;
917 rect
.y0
+= state
.trimfuzz
.y0
;
918 rect
.y1
+= state
.trimfuzz
.y1
;
919 fz_intersect_rect (&rect
, &mediabox
);
921 if (!fz_is_empty_rect (&rect
)) {
925 fz_drop_page (ctx
, page
);
930 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
932 err (1, "fwrite trim mediabox");
938 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
940 err (1, "fread trim mediabox %d", pageno
);
946 page
= fz_load_page (ctx
, state
.doc
, pageno
);
947 fz_bound_page (ctx
, page
, &mediabox
);
948 fz_drop_page (ctx
, page
);
950 show
= !state
.trimmargins
&& pageno
% 20 == 0;
952 printd ("progress %f Gathering dimensions %d",
953 (double) (pageno
) / state
.pagecount
,
958 printd ("emsg failed to load page %d", pageno
);
964 if (state
.pagedimcount
== 0
965 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
966 , p
->rotate
!= rotate
)
967 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
970 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
971 state
.pagedims
= realloc (state
.pagedims
, size
);
972 if (!state
.pagedims
) {
973 err (1, "realloc pagedims to %zu (%d elems)",
974 size
, state
.pagedimcount
+ 1);
977 p
= &state
.pagedims
[state
.pagedimcount
++];
979 p
->mediabox
= mediabox
;
984 printd ("progress 1 %s %d pages in %f seconds",
985 state
.trimmargins
? "Trimmed" : "Processed",
986 state
.pagecount
, end
- start
);
989 if (fclose (trimf
)) {
995 static void layout (void)
1000 struct pagedim
*p
= NULL
;
1001 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
1003 if (state
.pagedimcount
== 0) return;
1005 switch (state
.fitmodel
) {
1006 case FitProportional
:
1007 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1010 p
= &state
.pagedims
[pindex
];
1011 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1013 fz_transform_rect (&box
, &rm
);
1015 x0
= fz_min (box
.x0
, box
.x1
);
1016 x1
= fz_max (box
.x0
, box
.x1
);
1019 maxw
= fz_max (w
, maxw
);
1020 zoom
= state
.w
/ maxw
;
1032 ARSERT (0 && state
.fitmodel
);
1035 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1039 p
= &state
.pagedims
[pindex
];
1040 fz_rotate (&ctm
, state
.rotate
);
1041 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1043 fz_transform_rect (&box
, &rm
);
1044 w
= box
.x1
- box
.x0
;
1045 switch (state
.fitmodel
) {
1046 case FitProportional
:
1047 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
1053 h
= box
.y1
- box
.y0
;
1055 zoom
= fz_min (zw
, zh
);
1056 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
1065 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1066 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1068 fz_rotate (&rm
, p
->rotate
);
1069 p
->pagebox
= p
->mediabox
;
1070 fz_transform_rect (&p
->pagebox
, &rm
);
1071 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1072 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1076 fz_transform_rect (&rect
, &ctm
);
1077 fz_round_rect (&p
->bounds
, &rect
);
1080 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1081 fz_scale (&sm
, zoom
, -zoom
);
1082 fz_concat (&ctm
, &tm
, &sm
);
1088 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1089 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1090 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1091 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1092 int boundw
= x1
- x0
;
1093 int boundh
= y1
- y0
;
1095 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1096 } while (p
-- != state
.pagedims
);
1099 struct pagedim
*pdimofpageno (int pageno
)
1101 struct pagedim
*pdim
= state
.pagedims
;
1103 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1104 if (state
.pagedims
[i
].pageno
> pageno
)
1106 pdim
= &state
.pagedims
[i
];
1111 static void recurse_outline (fz_outline
*outline
, int level
)
1114 if (outline
->page
>= 0) {
1115 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
1116 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
1117 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1118 fz_transform_point (&p
, &pdim
->ctm
);
1119 printd ("o %d %d %d %d %s",
1120 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
1123 printd ("on %d %s", level
, outline
->title
);
1125 if (outline
->down
) {
1126 recurse_outline (outline
->down
, level
+ 1);
1128 outline
= outline
->next
;
1132 static void process_outline (void)
1134 fz_outline
*outline
;
1136 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1138 state
.needoutline
= 0;
1139 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1141 recurse_outline (outline
, 0);
1142 fz_drop_outline (state
.ctx
, outline
);
1146 static char *strofline (fz_stext_line
*line
)
1151 size_t size
= 0, cap
= 80;
1153 p
= malloc (cap
+ 1);
1154 if (!p
) return NULL
;
1156 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1157 int n
= fz_runetochar (utf8
, ch
->c
);
1158 if (size
+ n
> cap
) {
1160 p
= realloc (p
, cap
+ 1);
1161 if (!p
) return NULL
;
1164 memcpy (p
+ size
, utf8
, n
);
1171 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1172 int stop
, int pageno
, double start
)
1178 p
= strofline (line
);
1181 ret
= regexec (re
, p
, 1, &rm
, 0);
1184 if (ret
!= REG_NOMATCH
) {
1187 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1188 printd ("msg regexec error `%.*s'",
1189 (int) size
, errbuf
);
1195 fz_point p1
, p2
, p3
, p4
;
1196 fz_rect s
= {0,0,0,0}, e
;
1200 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1201 o
+= fz_runelen (ch
->c
);
1207 for (;ch
; ch
= ch
->next
) {
1208 o
+= fz_runelen (ch
->c
);
1209 if (o
> rm
.rm_eo
) break;
1223 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1230 printd ("progress 1 found at %d `%.*s' in %f sec",
1231 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1235 printd ("match %d %d %f %f %f %f %f %f %f %f",
1247 /* wishful thinking function */
1248 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1251 fz_stext_page
*text
;
1252 struct pagedim
*pdim
;
1253 int stop
= 0, niters
= 0;
1256 fz_stext_block
*block
;
1259 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1260 if (niters
++ == 5) {
1263 printd ("progress 1 attention requested aborting search at %d",
1268 printd ("progress %f searching in page %d",
1269 (double) (pageno
+ 1) / state
.pagecount
,
1273 pdim
= pdimofpageno (pageno
);
1274 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1275 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1277 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1279 fz_matrix ctm
= pagectm1 (page
, pdim
);
1280 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1283 fz_close_device (state
.ctx
, tdev
);
1284 fz_drop_device (state
.ctx
, tdev
);
1287 for (block
= text
->first_block
; block
; block
= block
->next
) {
1288 fz_stext_line
*line
;
1290 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1291 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1292 if (line
->bbox
.y0
< y
+ 1) continue;
1294 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1296 case 1: stop
= 1; break;
1297 case -1: stop
= 1; goto endloop
;
1303 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1304 fz_stext_line
*line
;
1306 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1307 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1308 if (line
->bbox
.y0
< y
+ 1) continue;
1310 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1312 case 1: stop
= 1; break;
1313 case -1: stop
= 1; goto endloop
;
1328 fz_drop_stext_page (state
.ctx
, text
);
1329 fz_drop_page (state
.ctx
, page
);
1333 printd ("progress 1 no matches %f sec", end
- start
);
1335 printd ("clearrects");
1338 static void set_tex_params (int colorspace
)
1340 switch (colorspace
) {
1342 state
.texiform
= GL_RGBA8
;
1343 state
.texform
= GL_RGBA
;
1344 state
.texty
= GL_UNSIGNED_BYTE
;
1345 state
.colorspace
= fz_device_rgb (state
.ctx
);
1348 state
.texiform
= GL_RGBA8
;
1349 state
.texform
= GL_BGRA
;
1350 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1351 state
.texty
= GL_UNSIGNED_INT_8_8_8_8_REV
;
1353 state
.texty
= GL_UNSIGNED_INT_8_8_8_8
;
1355 state
.colorspace
= fz_device_bgr (state
.ctx
);
1358 state
.texiform
= GL_LUMINANCE_ALPHA
;
1359 state
.texform
= GL_LUMINANCE_ALPHA
;
1360 state
.texty
= GL_UNSIGNED_BYTE
;
1361 state
.colorspace
= fz_device_gray (state
.ctx
);
1364 errx (1, "invalid colorspce %d", colorspace
);
1368 static void realloctexts (int texcount
)
1372 if (texcount
== state
.texcount
) return;
1374 if (texcount
< state
.texcount
) {
1375 glDeleteTextures (state
.texcount
- texcount
,
1376 state
.texids
+ texcount
);
1379 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1380 state
.texids
= realloc (state
.texids
, size
);
1381 if (!state
.texids
) {
1382 err (1, "realloc texs %zu", size
);
1385 state
.texowners
= (void *) (state
.texids
+ texcount
);
1386 if (texcount
> state
.texcount
) {
1387 glGenTextures (texcount
- state
.texcount
,
1388 state
.texids
+ state
.texcount
);
1389 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1390 state
.texowners
[i
].w
= -1;
1391 state
.texowners
[i
].slice
= NULL
;
1394 state
.texcount
= texcount
;
1398 static char *mbtoutf8 (char *s
)
1408 len
= mbstowcs (NULL
, s
, strlen (s
));
1413 if (len
== (size_t) -1) {
1414 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1419 tmp
= calloc (len
, sizeof (wchar_t));
1421 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1422 len
, sizeof (wchar_t), errno
, strerror (errno
));
1426 ret
= mbstowcs (tmp
, s
, len
);
1427 if (ret
== (size_t) -1) {
1428 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1429 len
, errno
, strerror (errno
));
1435 for (i
= 0; i
< ret
; ++i
) {
1436 len
+= fz_runelen (tmp
[i
]);
1439 p
= r
= malloc (len
+ 1);
1441 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1446 for (i
= 0; i
< ret
; ++i
) {
1447 p
+= fz_runetochar (p
, tmp
[i
]);
1454 CAMLprim value
ml_mbtoutf8 (value s_v
)
1460 s
= String_val (s_v
);
1466 ret_v
= caml_copy_string (r
);
1472 static void * mainloop (void UNUSED_ATTR
*unused
)
1475 int len
, ret
, oldlen
= 0;
1480 len
= readlen (state
.csock
);
1482 errx (1, "readlen returned 0");
1485 if (oldlen
< len
+ 1) {
1486 p
= realloc (p
, len
+ 1);
1488 err (1, "realloc %d failed", len
+ 1);
1492 readdata (state
.csock
, p
, len
);
1495 if (!strncmp ("open", p
, 4)) {
1496 int off
, usedoccss
, ok
= 0, layouth
;
1503 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1505 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1508 filename
= p
+ 5 + off
;
1509 filenamelen
= strlen (filename
);
1510 password
= filename
+ filenamelen
+ 1;
1512 if (password
[strlen (password
) + 1]) {
1513 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1517 fz_set_use_document_css (state
.ctx
, usedoccss
);
1518 fz_try (state
.ctx
) {
1519 ok
= openxref (filename
, password
, layouth
);
1521 fz_catch (state
.ctx
) {
1522 utf8filename
= mbtoutf8 (filename
);
1523 printd ("msg Could not open %s", utf8filename
);
1524 if (utf8filename
!= filename
) {
1525 free (utf8filename
);
1535 utf8filename
= mbtoutf8 (filename
);
1536 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1537 if (utf8filename
!= filename
) {
1538 free (utf8filename
);
1540 state
.needoutline
= 1;
1543 else if (!strncmp ("cs", p
, 2)) {
1546 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1548 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1551 set_tex_params (colorspace
);
1552 for (i
= 0; i
< state
.texcount
; ++i
) {
1553 state
.texowners
[i
].w
= -1;
1554 state
.texowners
[i
].slice
= NULL
;
1558 else if (!strncmp ("freepage", p
, 8)) {
1561 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1563 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1567 unlock ("freepage");
1569 else if (!strncmp ("freetile", p
, 8)) {
1572 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1574 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1578 unlock ("freetile");
1580 else if (!strncmp ("search", p
, 6)) {
1581 int icase
, pageno
, y
, len2
, forward
;
1585 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1586 &icase
, &pageno
, &y
, &forward
, &len2
);
1588 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1591 pattern
= p
+ 6 + len2
;
1592 ret
= regcomp (&re
, pattern
,
1593 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1598 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1599 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1602 search (&re
, pageno
, y
, forward
);
1606 else if (!strncmp ("geometry", p
, 8)) {
1610 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1612 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1619 for (int i
= 0; i
< state
.texcount
; ++i
) {
1620 state
.texowners
[i
].slice
= NULL
;
1623 state
.fitmodel
= fitmodel
;
1628 unlock ("geometry");
1629 printd ("continue %d", state
.pagecount
);
1631 else if (!strncmp ("reqlayout", p
, 9)) {
1638 ret
= sscanf (p
+ 9, " %d %d %d %n",
1639 &rotate
, &fitmodel
, &h
, &off
);
1641 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1644 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1645 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1648 state
.rotate
= rotate
;
1649 state
.fitmodel
= fitmodel
;
1654 nameddest
= p
+ 9 + off
;
1655 if (pdf
&& nameddest
&& *nameddest
) {
1657 struct pagedim
*pdim
;
1658 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1660 pdim
= pdimofpageno (pageno
);
1661 fz_transform_point (&xy
, &pdim
->ctm
);
1662 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1666 unlock ("reqlayout");
1667 printd ("continue %d", state
.pagecount
);
1669 else if (!strncmp ("page", p
, 4)) {
1674 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1676 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1681 page
= loadpage (pageno
, pindex
);
1685 printd ("page %" PRIxPTR
" %f", (uintptr_t) page
, b
- a
);
1687 else if (!strncmp ("tile", p
, 4)) {
1694 ret
= sscanf (p
+ 4, " %" SCNxPTR
" %d %d %d %d %" SCNxPTR
,
1695 (uintptr_t *) &page
, &x
, &y
, &w
, &h
,
1696 (uintptr_t *) &data
);
1698 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1703 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1707 printd ("tile %d %d %" PRIxPTR
" %u %f",
1708 x
, y
, (uintptr_t) (tile
),
1709 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1712 else if (!strncmp ("trimset", p
, 7)) {
1716 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1717 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1719 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1722 state
.trimmargins
= trimmargins
;
1723 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1725 state
.trimfuzz
= fuzz
;
1729 else if (!strncmp ("settrim", p
, 7)) {
1733 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1734 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1736 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1740 state
.trimmargins
= trimmargins
;
1741 state
.needoutline
= 1;
1742 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1744 state
.trimfuzz
= fuzz
;
1746 state
.pagedimcount
= 0;
1747 free (state
.pagedims
);
1748 state
.pagedims
= NULL
;
1753 printd ("continue %d", state
.pagecount
);
1755 else if (!strncmp ("sliceh", p
, 6)) {
1758 ret
= sscanf (p
+ 6, " %d", &h
);
1760 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1762 if (h
!= state
.sliceheight
) {
1763 state
.sliceheight
= h
;
1764 for (int i
= 0; i
< state
.texcount
; ++i
) {
1765 state
.texowners
[i
].w
= -1;
1766 state
.texowners
[i
].h
= -1;
1767 state
.texowners
[i
].slice
= NULL
;
1771 else if (!strncmp ("interrupt", p
, 9)) {
1772 printd ("vmsg interrupted");
1775 errx (1, "unknown command %.*s", len
, p
);
1781 CAMLprim value
ml_isexternallink (value uri_v
)
1784 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1785 CAMLreturn (Val_bool (ext
));
1788 CAMLprim value
ml_uritolocation (value uri_v
)
1794 struct pagedim
*pdim
;
1796 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1798 pdim
= pdimofpageno (pageno
);
1799 fz_transform_point (&xy
, &pdim
->ctm
);
1800 ret_v
= caml_alloc_tuple (3);
1801 Field (ret_v
, 0) = Val_int (pageno
);
1802 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1803 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1807 CAMLprim value
ml_realloctexts (value texcount_v
)
1809 CAMLparam1 (texcount_v
);
1812 if (trylock (__func__
)) {
1816 realloctexts (Int_val (texcount_v
));
1821 CAMLreturn (Val_bool (ok
));
1824 static void recti (int x0
, int y0
, int x1
, int y1
)
1826 GLfloat
*v
= state
.vertices
;
1828 glVertexPointer (2, GL_FLOAT
, 0, v
);
1829 v
[0] = x0
; v
[1] = y0
;
1830 v
[2] = x1
; v
[3] = y0
;
1831 v
[4] = x0
; v
[5] = y1
;
1832 v
[6] = x1
; v
[7] = y1
;
1833 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1836 static void showsel (struct page
*page
, int ox
, int oy
)
1840 fz_stext_block
*block
;
1842 unsigned char selcolor
[] = {15,15,15,140};
1844 if (!page
->fmark
|| !page
->lmark
) return;
1846 glEnable (GL_BLEND
);
1847 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1848 glColor4ubv (selcolor
);
1850 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1851 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1853 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1854 fz_stext_line
*line
;
1856 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1857 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1860 rect
= fz_empty_rect
;
1861 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1862 if (ch
== page
->fmark
) seen
= 1;
1863 if (seen
) fz_union_rect (&rect
, &ch
->bbox
);
1864 if (ch
== page
->lmark
) {
1865 fz_round_rect (&bbox
, &rect
);
1866 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1867 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1871 fz_round_rect (&bbox
, &rect
);
1872 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1873 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1877 glDisable (GL_BLEND
);
1880 #pragma GCC diagnostic push
1881 #pragma GCC diagnostic ignored "-Wconversion"
1883 #pragma GCC diagnostic pop
1885 static void stipplerect (fz_matrix
*m
,
1893 fz_transform_point (p1
, m
);
1894 fz_transform_point (p2
, m
);
1895 fz_transform_point (p3
, m
);
1896 fz_transform_point (p4
, m
);
1902 t
= hypotf (w
, h
) * .25f
;
1906 s
= hypotf (w
, h
) * .25f
;
1908 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
1909 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
1911 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
1912 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
1914 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
1915 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
1917 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
1918 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
1920 glDrawArrays (GL_LINES
, 0, 8);
1923 static void solidrect (fz_matrix
*m
,
1930 fz_transform_point (p1
, m
);
1931 fz_transform_point (p2
, m
);
1932 fz_transform_point (p3
, m
);
1933 fz_transform_point (p4
, m
);
1934 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
1935 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
1937 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
1938 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
1939 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
1942 static void ensurelinks (struct page
*page
)
1945 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
1948 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1950 fz_matrix ctm
, tm
, pm
;
1952 GLfloat
*texcoords
= state
.texcoords
;
1953 GLfloat
*vertices
= state
.vertices
;
1957 glEnable (GL_TEXTURE_1D
);
1958 glEnable (GL_BLEND
);
1959 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1960 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
1962 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1963 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1964 fz_translate (&tm
, xoff
, yoff
);
1965 pm
= pagectm (page
);
1966 fz_concat (&ctm
, &pm
, &tm
);
1968 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
1969 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
1971 for (link
= page
->links
; link
; link
= link
->next
) {
1972 fz_point p1
, p2
, p3
, p4
;
1974 p1
.x
= link
->rect
.x0
;
1975 p1
.y
= link
->rect
.y0
;
1977 p2
.x
= link
->rect
.x1
;
1978 p2
.y
= link
->rect
.y0
;
1980 p3
.x
= link
->rect
.x1
;
1981 p3
.y
= link
->rect
.y1
;
1983 p4
.x
= link
->rect
.x0
;
1984 p4
.y
= link
->rect
.y1
;
1986 /* TODO: different colours for different schemes */
1987 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
1988 else glColor3ub (255, 0, 0);
1990 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
1993 for (int i
= 0; i
< page
->annotcount
; ++i
) {
1994 fz_point p1
, p2
, p3
, p4
;
1995 struct annot
*annot
= &page
->annots
[i
];
1997 p1
.x
= annot
->bbox
.x0
;
1998 p1
.y
= annot
->bbox
.y0
;
2000 p2
.x
= annot
->bbox
.x1
;
2001 p2
.y
= annot
->bbox
.y0
;
2003 p3
.x
= annot
->bbox
.x1
;
2004 p3
.y
= annot
->bbox
.y1
;
2006 p4
.x
= annot
->bbox
.x0
;
2007 p4
.y
= annot
->bbox
.y1
;
2009 glColor3ub (0, 0, 128);
2010 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2013 glDisable (GL_BLEND
);
2014 glDisable (GL_TEXTURE_1D
);
2017 static int compareslinks (const void *l
, const void *r
)
2019 struct slink
const *ls
= l
;
2020 struct slink
const *rs
= r
;
2021 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2022 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2024 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2027 static void droptext (struct page
*page
)
2030 fz_drop_stext_page (state
.ctx
, page
->text
);
2037 static void dropannots (struct page
*page
)
2040 free (page
->annots
);
2041 page
->annots
= NULL
;
2042 page
->annotcount
= 0;
2046 static void ensureannots (struct page
*page
)
2049 size_t annotsize
= sizeof (*page
->annots
);
2052 if (state
.gen
!= page
->agen
) {
2054 page
->agen
= state
.gen
;
2056 if (page
->annots
) return;
2058 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2060 annot
= fz_next_annot (state
.ctx
, annot
)) {
2065 page
->annotcount
= count
;
2066 page
->annots
= calloc (count
, annotsize
);
2067 if (!page
->annots
) {
2068 err (1, "calloc annots %d", count
);
2071 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2073 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2076 fz_bound_annot (state
.ctx
, annot
, &rect
);
2077 page
->annots
[i
].annot
= annot
;
2078 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2083 static void dropslinks (struct page
*page
)
2086 free (page
->slinks
);
2087 page
->slinks
= NULL
;
2088 page
->slinkcount
= 0;
2091 fz_drop_link (state
.ctx
, page
->links
);
2096 static void ensureslinks (struct page
*page
)
2100 size_t slinksize
= sizeof (*page
->slinks
);
2103 ensureannots (page
);
2104 if (state
.gen
!= page
->sgen
) {
2106 page
->sgen
= state
.gen
;
2108 if (page
->slinks
) return;
2111 ctm
= pagectm (page
);
2113 count
= page
->annotcount
;
2114 for (link
= page
->links
; link
; link
= link
->next
) {
2120 page
->slinkcount
= count
;
2121 page
->slinks
= calloc (count
, slinksize
);
2122 if (!page
->slinks
) {
2123 err (1, "calloc slinks %d", count
);
2126 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
2130 fz_transform_rect (&rect
, &ctm
);
2131 page
->slinks
[i
].tag
= SLINK
;
2132 page
->slinks
[i
].u
.link
= link
;
2133 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2135 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2137 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2138 fz_transform_rect (&rect
, &ctm
);
2139 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2141 page
->slinks
[i
].tag
= SANNOT
;
2142 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2144 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2148 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2149 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2152 struct slink
*slink
;
2153 float x0
, y0
, x1
, y1
, w
;
2155 ensureslinks (page
);
2156 glColor3ub (0xc3, 0xb0, 0x91);
2157 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2158 fmt_linkn (buf
, i
+ noff
);
2159 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2160 slink
= &page
->slinks
[i
];
2162 x0
= slink
->bbox
.x0
+ xoff
- 5;
2163 y1
= slink
->bbox
.y0
+ yoff
- 5;
2164 y0
= y1
+ 10 + hfsize
;
2165 w
= measure_string (state
.face
, hfsize
, buf
);
2167 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2171 glEnable (GL_BLEND
);
2172 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2173 glEnable (GL_TEXTURE_2D
);
2174 glColor3ub (0, 0, 0);
2175 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2176 fmt_linkn (buf
, i
+ noff
);
2177 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2178 slink
= &page
->slinks
[i
];
2180 x0
= slink
->bbox
.x0
+ xoff
;
2181 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2182 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2185 glDisable (GL_TEXTURE_2D
);
2186 glDisable (GL_BLEND
);
2189 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2192 struct slice
*slice1
;
2193 unsigned char *texdata
;
2196 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2197 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2199 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2200 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2201 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2205 int texindex
= state
.texindex
++ % state
.texcount
;
2207 if (state
.texowners
[texindex
].w
== tile
->w
) {
2208 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2212 state
.texowners
[texindex
].h
= slice
->h
;
2216 state
.texowners
[texindex
].h
= slice
->h
;
2219 state
.texowners
[texindex
].w
= tile
->w
;
2220 state
.texowners
[texindex
].slice
= slice
;
2221 slice
->texindex
= texindex
;
2223 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2224 #if TEXT_TYPE == GL_TEXTURE_2D
2225 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2226 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2227 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2228 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2231 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2235 texdata
= tile
->pixmap
->samples
;
2238 glTexSubImage2D (TEXT_TYPE
,
2250 glTexImage2D (TEXT_TYPE
,
2262 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2267 CAMLprim
void ml_begintiles (value unit_v
)
2269 CAMLparam1 (unit_v
);
2270 glEnable (TEXT_TYPE
);
2271 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2272 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2276 CAMLprim
void ml_endtiles (value unit_v
)
2278 CAMLparam1 (unit_v
);
2279 glDisable (TEXT_TYPE
);
2283 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2285 CAMLparam2 (args_v
, ptr_v
);
2286 int dispx
= Int_val (Field (args_v
, 0));
2287 int dispy
= Int_val (Field (args_v
, 1));
2288 int dispw
= Int_val (Field (args_v
, 2));
2289 int disph
= Int_val (Field (args_v
, 3));
2290 int tilex
= Int_val (Field (args_v
, 4));
2291 int tiley
= Int_val (Field (args_v
, 5));
2292 char *s
= String_val (ptr_v
);
2293 struct tile
*tile
= parse_pointer (__func__
, s
);
2294 int slicey
, firstslice
;
2295 struct slice
*slice
;
2296 GLfloat
*texcoords
= state
.texcoords
;
2297 GLfloat
*vertices
= state
.vertices
;
2299 firstslice
= tiley
/ tile
->sliceheight
;
2300 slice
= &tile
->slices
[firstslice
];
2301 slicey
= tiley
% tile
->sliceheight
;
2306 dh
= slice
->h
- slicey
;
2307 dh
= fz_mini (disph
, dh
);
2308 uploadslice (tile
, slice
);
2310 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2311 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2312 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2313 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2315 vertices
[0] = dispx
; vertices
[1] = dispy
;
2316 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2317 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2318 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2320 #if TEXT_TYPE == GL_TEXTURE_2D
2321 for (int i
= 0; i
< 8; ++i
) {
2322 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2326 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2330 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2336 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2338 fz_matrix ctm
, tm
, pm
;
2339 fz_point p1
, p2
, p3
, p4
;
2340 GLfloat
*vertices
= state
.vertices
;
2341 double *v
= (double *) rects_v
;
2343 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2344 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2345 fz_translate (&tm
, xoff
, yoff
);
2346 pm
= pagectm (page
);
2347 fz_concat (&ctm
, &pm
, &tm
);
2349 glEnable (GL_BLEND
);
2350 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2353 p1
.x
= (float) v
[4];
2354 p1
.y
= (float) v
[5];
2356 p2
.x
= (float) v
[6];
2357 p2
.y
= (float) v
[5];
2359 p3
.x
= (float) v
[6];
2360 p3
.y
= (float) v
[7];
2362 p4
.x
= (float) v
[4];
2363 p4
.y
= (float) v
[7];
2364 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2365 glDisable (GL_BLEND
);
2368 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2369 value xoff_v
, value yoff_v
,
2372 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2373 int xoff
= Int_val (xoff_v
);
2374 int yoff
= Int_val (yoff_v
);
2375 int noff
= Int_val (Field (li_v
, 0));
2376 char *targ
= String_val (Field (li_v
, 1));
2377 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2378 int hfsize
= Int_val (Field (li_v
, 2));
2379 char *s
= String_val (ptr_v
);
2380 int hlmask
= Int_val (hlinks_v
);
2381 struct page
*page
= parse_pointer (__func__
, s
);
2383 if (!page
->fzpage
) {
2384 /* deal with loadpage failed pages */
2388 if (trylock (__func__
)) {
2393 ensureannots (page
);
2394 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2396 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2397 noff
= page
->slinkcount
;
2399 if (page
->tgen
== state
.gen
) {
2400 showsel (page
, xoff
, yoff
);
2405 CAMLreturn (Val_int (noff
));
2408 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2411 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2412 int xoff
= Int_val (xoff_v
);
2413 int yoff
= Int_val (yoff_v
);
2414 char *s
= String_val (ptr_v
);
2415 struct page
*page
= parse_pointer (__func__
, s
);
2417 drawprect (page
, xoff
, yoff
, rects_v
);
2421 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2425 const fz_matrix
*tctm
;
2426 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2428 if (!page
->annots
) return NULL
;
2431 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2432 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2435 tctm
= &fz_identity
;
2441 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2442 fz_invert_matrix (&ctm
, &ctm
);
2443 fz_transform_point (&p
, &ctm
);
2446 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2447 struct annot
*a
= &page
->annots
[i
];
2450 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2451 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2452 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2460 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2466 ensureslinks (page
);
2471 ctm
= pagectm (page
);
2472 fz_invert_matrix (&ctm
, &ctm
);
2473 fz_transform_point (&p
, &ctm
);
2475 for (link
= page
->links
; link
; link
= link
->next
) {
2476 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2477 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2485 static void ensuretext (struct page
*page
)
2487 if (state
.gen
!= page
->tgen
) {
2489 page
->tgen
= state
.gen
;
2495 page
->text
= fz_new_stext_page (state
.ctx
,
2496 &state
.pagedims
[page
->pdimno
].mediabox
);
2497 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2498 ctm
= pagectm (page
);
2499 fz_run_display_list (state
.ctx
, page
->dlist
,
2500 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2501 fz_close_device (state
.ctx
, tdev
);
2502 fz_drop_device (state
.ctx
, tdev
);
2506 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2508 CAMLparam2 (start_page_v
, dir_v
);
2510 int i
, dir
= Int_val (dir_v
);
2511 int start_page
= Int_val (start_page_v
);
2512 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2516 ret_v
= Val_int (0);
2518 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2519 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2524 pdf_page
*page
= NULL
;
2527 fz_try (state
.ctx
) {
2528 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2529 found
= !!page
->links
|| !!page
->annots
;
2531 fz_catch (state
.ctx
) {
2535 fz_drop_page (state
.ctx
, &page
->super
);
2539 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2540 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2542 fz_drop_link (state
.ctx
, link
);
2543 fz_drop_page (state
.ctx
, page
);
2547 ret_v
= caml_alloc_small (1, 1);
2548 Field (ret_v
, 0) = Val_int (i
);
2557 enum { dir_first
, dir_last
};
2558 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2560 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2562 CAMLparam2 (ptr_v
, dir_v
);
2563 CAMLlocal2 (ret_v
, pos_v
);
2565 int dirtag
, i
, slinkindex
;
2566 struct slink
*found
= NULL
,*slink
;
2567 char *s
= String_val (ptr_v
);
2569 page
= parse_pointer (__func__
, s
);
2570 ret_v
= Val_int (0);
2572 ensureslinks (page
);
2574 if (Is_block (dir_v
)) {
2575 dirtag
= Tag_val (dir_v
);
2577 case dir_first_visible
:
2579 int x0
, y0
, dir
, first_index
, last_index
;
2581 pos_v
= Field (dir_v
, 0);
2582 x0
= Int_val (Field (pos_v
, 0));
2583 y0
= Int_val (Field (pos_v
, 1));
2584 dir
= Int_val (Field (pos_v
, 2));
2589 last_index
= page
->slinkcount
;
2592 first_index
= page
->slinkcount
- 1;
2596 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2597 slink
= &page
->slinks
[i
];
2598 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2607 slinkindex
= Int_val (Field (dir_v
, 0));
2608 found
= &page
->slinks
[slinkindex
];
2609 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2610 slink
= &page
->slinks
[i
];
2611 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2619 slinkindex
= Int_val (Field (dir_v
, 0));
2620 found
= &page
->slinks
[slinkindex
];
2621 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2622 slink
= &page
->slinks
[i
];
2623 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2631 slinkindex
= Int_val (Field (dir_v
, 0));
2632 found
= &page
->slinks
[slinkindex
];
2633 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2634 slink
= &page
->slinks
[i
];
2635 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2643 slinkindex
= Int_val (Field (dir_v
, 0));
2644 found
= &page
->slinks
[slinkindex
];
2645 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2646 slink
= &page
->slinks
[i
];
2647 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2656 dirtag
= Int_val (dir_v
);
2659 found
= page
->slinks
;
2664 found
= page
->slinks
+ (page
->slinkcount
- 1);
2670 ret_v
= caml_alloc_small (2, 1);
2671 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2678 enum { uuri
, utext
, uannot
};
2680 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2682 CAMLparam2 (ptr_v
, n_v
);
2683 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2686 char *s
= String_val (ptr_v
);
2687 struct slink
*slink
;
2689 ret_v
= Val_int (0);
2690 page
= parse_pointer (__func__
, s
);
2693 ensureslinks (page
);
2694 slink
= &page
->slinks
[Int_val (n_v
)];
2695 if (slink
->tag
== SLINK
) {
2696 link
= slink
->u
.link
;
2697 str_v
= caml_copy_string (link
->uri
);
2698 ret_v
= caml_alloc_small (1, uuri
);
2699 Field (ret_v
, 0) = str_v
;
2702 ret_v
= caml_alloc_small (1, uannot
);
2703 tup_v
= caml_alloc_tuple (2);
2704 Field (ret_v
, 0) = tup_v
;
2705 Field (tup_v
, 0) = ptr_v
;
2706 Field (tup_v
, 1) = n_v
;
2713 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2715 CAMLparam2 (ptr_v
, n_v
);
2718 char *contents
= NULL
;
2721 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2723 char *s
= String_val (ptr_v
);
2725 struct slink
*slink
;
2727 page
= parse_pointer (__func__
, s
);
2728 slink
= &page
->slinks
[Int_val (n_v
)];
2729 contents
= pdf_copy_annot_contents (state
.ctx
,
2730 (pdf_annot
*) slink
->u
.annot
);
2734 ret_v
= caml_copy_string (contents
);
2735 fz_free (state
.ctx
, contents
);
2738 ret_v
= caml_copy_string ("");
2743 CAMLprim value
ml_getlinkcount (value ptr_v
)
2747 char *s
= String_val (ptr_v
);
2749 page
= parse_pointer (__func__
, s
);
2750 CAMLreturn (Val_int (page
->slinkcount
));
2753 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2755 CAMLparam2 (ptr_v
, n_v
);
2758 struct slink
*slink
;
2759 char *s
= String_val (ptr_v
);
2761 page
= parse_pointer (__func__
, s
);
2762 ret_v
= caml_alloc_tuple (4);
2764 ensureslinks (page
);
2766 slink
= &page
->slinks
[Int_val (n_v
)];
2767 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2768 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2769 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2770 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2775 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2777 CAMLparam3 (ptr_v
, x_v
, y_v
);
2778 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2780 struct annot
*annot
;
2782 char *ptr
= String_val (ptr_v
);
2783 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2784 struct pagedim
*pdim
;
2786 ret_v
= Val_int (0);
2787 if (trylock (__func__
)) {
2791 page
= parse_pointer (__func__
, ptr
);
2792 pdim
= &state
.pagedims
[page
->pdimno
];
2793 x
+= pdim
->bounds
.x0
;
2794 y
+= pdim
->bounds
.y0
;
2797 annot
= getannot (page
, x
, y
);
2801 ensureslinks (page
);
2802 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2803 if (page
->slinks
[i
].tag
== SANNOT
2804 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2809 ret_v
= caml_alloc_small (1, uannot
);
2810 tup_v
= caml_alloc_tuple (2);
2811 Field (ret_v
, 0) = tup_v
;
2812 Field (tup_v
, 0) = ptr_v
;
2813 Field (tup_v
, 1) = Val_int (n
);
2818 link
= getlink (page
, x
, y
);
2820 str_v
= caml_copy_string (link
->uri
);
2821 ret_v
= caml_alloc_small (1, uuri
);
2822 Field (ret_v
, 0) = str_v
;
2826 fz_stext_block
*block
;
2830 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2831 fz_stext_line
*line
;
2833 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2835 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2838 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2842 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2845 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2848 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
2849 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
2850 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
2853 if (!n2
) n2
= "<unknown font>";
2855 if (face
&& face
->family_name
) {
2857 char *n1
= face
->family_name
;
2858 size_t l1
= strlen (n1
);
2859 size_t l2
= strlen (n2
);
2861 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2862 s
= malloc (l1
+ l2
+ 2);
2866 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2867 str_v
= caml_copy_string (s
);
2872 if (str_v
== Val_unit
) {
2873 str_v
= caml_copy_string (n2
);
2875 ret_v
= caml_alloc_small (1, utext
);
2876 Field (ret_v
, 0) = str_v
;
2890 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2892 CAMLprim
void ml_clearmark (value ptr_v
)
2895 char *s
= String_val (ptr_v
);
2898 if (trylock (__func__
)) {
2902 page
= parse_pointer (__func__
, s
);
2911 static int uninteresting (int c
)
2913 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
2917 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2919 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2923 fz_stext_line
*line
;
2924 fz_stext_block
*block
;
2925 struct pagedim
*pdim
;
2926 int mark
= Int_val (mark_v
);
2927 char *s
= String_val (ptr_v
);
2928 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2930 ret_v
= Val_bool (0);
2931 if (trylock (__func__
)) {
2935 page
= parse_pointer (__func__
, s
);
2936 pdim
= &state
.pagedims
[page
->pdimno
];
2940 if (mark
== mark_page
) {
2941 page
->fmark
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
2942 page
->lmark
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
2943 ret_v
= Val_bool (1);
2947 x
+= pdim
->bounds
.x0
;
2948 y
+= pdim
->bounds
.y0
;
2950 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2951 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2953 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2956 if (mark
== mark_block
) {
2957 page
->fmark
= block
->u
.t
.first_line
->first_char
;
2958 page
->lmark
= block
->u
.t
.last_line
->last_char
;
2959 ret_v
= Val_bool (1);
2963 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2967 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2970 if (mark
== mark_line
) {
2971 page
->fmark
= line
->first_char
;
2972 page
->lmark
= line
->last_char
;
2973 ret_v
= Val_bool (1);
2977 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2978 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
2980 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
2981 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
2982 if (uninteresting (ch2
->c
)) first
= NULL
;
2983 else if (!first
) first
= ch2
;
2985 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
2986 if (uninteresting (ch2
->c
)) break;
2990 page
->fmark
= first
;
2992 ret_v
= Val_bool (1);
2999 if (!Bool_val (ret_v
)) {
3009 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3011 CAMLparam3 (ptr_v
, x_v
, y_v
);
3012 CAMLlocal2 (ret_v
, res_v
);
3015 struct pagedim
*pdim
;
3016 fz_stext_block
*block
;
3017 char *s
= String_val (ptr_v
);
3018 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3020 ret_v
= Val_int (0);
3021 if (trylock (__func__
)) {
3025 page
= parse_pointer (__func__
, s
);
3026 pdim
= &state
.pagedims
[page
->pdimno
];
3027 x
+= pdim
->bounds
.x0
;
3028 y
+= pdim
->bounds
.y0
;
3032 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3033 switch (block
->type
) {
3034 case FZ_STEXT_BLOCK_TEXT
:
3038 case FZ_STEXT_BLOCK_IMAGE
:
3046 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3051 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3052 ret_v
= caml_alloc_small (1, 1);
3053 Store_double_field (res_v
, 0, (double) b
->x0
);
3054 Store_double_field (res_v
, 1, (double) b
->x1
);
3055 Store_double_field (res_v
, 2, (double) b
->y0
);
3056 Store_double_field (res_v
, 3, (double) b
->y1
);
3057 Field (ret_v
, 0) = res_v
;
3065 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3067 CAMLparam2 (ptr_v
, rect_v
);
3070 struct pagedim
*pdim
;
3071 char *s
= String_val (ptr_v
);
3074 fz_stext_line
*line
;
3075 fz_stext_block
*block
;
3076 fz_stext_char
*fc
, *lc
;
3078 if (trylock (__func__
)) {
3082 page
= parse_pointer (__func__
, s
);
3085 pdim
= &state
.pagedims
[page
->pdimno
];
3086 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3087 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3088 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3089 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3102 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3103 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3104 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3105 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3107 if (x0
>= b
.x0
&& x0
<= b
.x1
&& y0
>= b
.y0
&& y0
<= b
.y1
) {
3110 if (x1
>= b
.x0
&& x1
<= b
.x1
&& y1
>= b
.y0
&& y1
<= b
.y1
) {
3116 if (x1
< x0
&& fc
== lc
) {
3133 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3139 len
= fz_runetochar (buf
, ch
->c
);
3140 ret
= fwrite (buf
, len
, 1, f
);
3142 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3143 len
, ret
, errno
, strerror (errno
));
3149 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3151 CAMLparam2 (command_v
, fds_v
);
3152 CAMLlocal2 (l_v
, tup_v
);
3154 pid_t pid
= (pid_t
) -1;
3156 value earg_v
= Nothing
;
3157 posix_spawnattr_t attr
;
3158 posix_spawn_file_actions_t fa
;
3159 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3161 argv
[2] = String_val (command_v
);
3163 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3164 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3167 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3168 msg
= "posix_spawnattr_init";
3172 #ifdef POSIX_SPAWN_USEVFORK
3173 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3174 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3179 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3182 tup_v
= Field (l_v
, 0);
3183 fd1
= Int_val (Field (tup_v
, 0));
3184 fd2
= Int_val (Field (tup_v
, 1));
3186 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3187 msg
= "posix_spawn_file_actions_addclose";
3193 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3194 msg
= "posix_spawn_file_actions_adddup2";
3201 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3202 msg
= "posix_spawn";
3207 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3208 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3212 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3213 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3214 ret1
, strerror (ret1
));
3218 unix_error (ret
, msg
, earg_v
);
3220 CAMLreturn (Val_int (pid
));
3223 CAMLprim value
ml_hassel (value ptr_v
)
3228 char *s
= String_val (ptr_v
);
3230 ret_v
= Val_bool (0);
3231 if (trylock (__func__
)) {
3235 page
= parse_pointer (__func__
, s
);
3236 ret_v
= Val_bool (page
->fmark
&& page
->lmark
);
3242 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3244 CAMLparam2 (fd_v
, ptr_v
);
3248 fz_stext_line
*line
;
3249 fz_stext_block
*block
;
3250 int fd
= Int_val (fd_v
);
3251 char *s
= String_val (ptr_v
);
3253 if (trylock (__func__
)) {
3257 page
= parse_pointer (__func__
, s
);
3259 if (!page
->fmark
|| !page
->lmark
) {
3260 printd ("emsg nothing to copy on page %d", page
->pageno
);
3264 f
= fdopen (fd
, "w");
3266 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3267 fd
, errno
, strerror (errno
));
3271 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3272 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3273 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3275 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3276 if (seen
|| ch
== page
->fmark
) {
3279 if (ch
== page
->lmark
) goto close
;
3280 } while ((ch
= ch
->next
));
3285 if (seen
) fputc ('\n', f
);
3290 int ret
= fclose (f
);
3293 if (errno
!= ECHILD
) {
3294 printd ("emsg failed to close sel pipe: %d:%s",
3295 errno
, strerror (errno
));
3305 printd ("emsg failed to close sel pipe: %d:%s",
3306 errno
, strerror (errno
));
3312 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3314 CAMLparam1 (pagedimno_v
);
3316 int pagedimno
= Int_val (pagedimno_v
);
3319 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3320 if (trylock (__func__
)) {
3321 box
= fz_empty_rect
;
3324 box
= state
.pagedims
[pagedimno
].mediabox
;
3328 Store_double_field (ret_v
, 0, (double) box
.x0
);
3329 Store_double_field (ret_v
, 1, (double) box
.x1
);
3330 Store_double_field (ret_v
, 2, (double) box
.y0
);
3331 Store_double_field (ret_v
, 3, (double) box
.y1
);
3336 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3337 value dw_v
, value cols_v
)
3339 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3345 float winw
= Int_val (winw_v
);
3346 float winh
= Int_val (winh_v
);
3347 float dw
= Int_val (dw_v
);
3348 float cols
= Int_val (cols_v
);
3349 float pw
= 1.0, ph
= 1.0;
3351 if (trylock (__func__
)) {
3355 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3356 float w
= p
->pagebox
.x1
/ cols
;
3357 float h
= p
->pagebox
.y1
;
3361 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3363 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3366 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3369 ret_v
= caml_copy_double ((double) zoom
);
3373 CAMLprim value
ml_getmaxw (value unit_v
)
3375 CAMLparam1 (unit_v
);
3381 if (trylock (__func__
)) {
3385 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3386 float w
= p
->pagebox
.x1
;
3387 maxw
= fz_max (maxw
, w
);
3392 ret_v
= caml_copy_double ((double) maxw
);
3396 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3398 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3400 int pt
= Int_val(pt_v
);
3401 int x
= Int_val (x_v
);
3402 int y
= Int_val (y_v
);
3405 w
= (double) draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3406 ret_v
= caml_copy_double (w
);
3410 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3412 CAMLparam2 (pt_v
, string_v
);
3414 int pt
= Int_val (pt_v
);
3417 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3418 ret_v
= caml_copy_double (w
);
3422 CAMLprim value
ml_getpagebox (value opaque_v
)
3424 CAMLparam1 (opaque_v
);
3430 char *s
= String_val (opaque_v
);
3431 struct page
*page
= parse_pointer (__func__
, s
);
3433 ret_v
= caml_alloc_tuple (4);
3434 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3436 ctm
= pagectm (page
);
3437 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3439 fz_close_device (state
.ctx
, dev
);
3440 fz_drop_device (state
.ctx
, dev
);
3441 fz_round_rect (&bbox
, &rect
);
3442 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3443 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3444 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3445 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3450 CAMLprim
void ml_setaalevel (value level_v
)
3452 CAMLparam1 (level_v
);
3454 state
.aalevel
= Int_val (level_v
);
3459 #pragma GCC diagnostic push
3460 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3461 #include <X11/Xlib.h>
3462 #include <X11/cursorfont.h>
3463 #pragma GCC diagnostic pop
3467 static const int shapes
[] = {
3468 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3471 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3477 XVisualInfo
*visual
;
3478 Cursor curs
[CURS_COUNT
];
3482 static void initcurs (void)
3484 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3485 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3489 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3491 CAMLparam3 (display_v
, wid_v
, screen_v
);
3493 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3495 caml_failwith ("XOpenDisplay");
3498 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3499 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3501 XCloseDisplay (glx
.dpy
);
3502 caml_failwith ("glXChooseVisual");
3507 glx
.wid
= Int_val (wid_v
);
3508 CAMLreturn (Val_int (glx
.visual
->visualid
));
3511 CAMLprim
void ml_glxcompleteinit (value unit_v
)
3513 CAMLparam1 (unit_v
);
3515 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3517 caml_failwith ("glXCreateContext");
3523 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3524 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3526 caml_failwith ("glXMakeCurrent");
3531 CAMLprim
void ml_setcursor (value cursor_v
)
3533 CAMLparam1 (cursor_v
);
3534 size_t cursn
= Int_val (cursor_v
);
3536 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
3537 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
3542 CAMLprim
void ml_swapb (value unit_v
)
3544 CAMLparam1 (unit_v
);
3545 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3549 long keysym2ucs (KeySym
);
3550 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3552 CAMLparam1 (keysym_v
);
3554 KeySym keysym
= Int_val (keysym_v
);
3559 rune
= (Rune
) keysym2ucs (keysym
);
3560 len
= fz_runetochar (buf
, rune
);
3562 str_v
= caml_copy_string (buf
);
3566 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3568 CAMLparam1 (keysym_v
);
3570 long ucs_v
= Long_val (keysym_v
);
3574 len
= fz_runetochar (buf
, (int) ucs_v
);
3576 str_v
= caml_copy_string (buf
);
3581 enum { piunknown
, pilinux
, pimacos
, pisun
, pibsd
};
3583 CAMLprim value
ml_platform (value unit_v
)
3585 CAMLparam1 (unit_v
);
3586 CAMLlocal2 (tup_v
, arr_v
);
3587 int platid
= piunknown
;
3590 #if defined __linux__
3592 #elif defined __DragonFly__ || defined __FreeBSD__
3593 || defined __OpenBSD__
|| defined __NetBSD__
3595 #elif defined __sun__
3597 #elif defined __APPLE__
3600 if (uname (&buf
)) err (1, "uname");
3602 tup_v
= caml_alloc_tuple (2);
3604 char const *sar
[] = {
3611 arr_v
= caml_copy_string_array (sar
);
3613 Field (tup_v
, 0) = Val_int (platid
);
3614 Field (tup_v
, 1) = arr_v
;
3618 CAMLprim
void ml_cloexec (value fd_v
)
3621 int fd
= Int_val (fd_v
);
3623 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3624 uerror ("fcntl", Nothing
);
3629 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3631 CAMLparam2 (w_v
, h_v
);
3634 int w
= Int_val (w_v
);
3635 int h
= Int_val (h_v
);
3636 int cs
= Int_val (cs_v
);
3638 if (state
.bo_usable
) {
3639 pbo
= calloc (sizeof (*pbo
), 1);
3641 err (1, "calloc pbo");
3653 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3656 state
.glGenBuffersARB (1, &pbo
->id
);
3657 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3658 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3659 NULL
, GL_STREAM_DRAW
);
3660 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3662 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3664 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3665 state
.glDeleteBuffersARB (1, &pbo
->id
);
3667 ret_v
= caml_copy_string ("0");
3673 res
= snprintf (NULL
, 0, "%" PRIxPTR
, (uintptr_t) pbo
);
3675 err (1, "snprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3679 err (1, "malloc %d bytes failed", res
+1);
3681 res
= sprintf (s
, "%" PRIxPTR
, (uintptr_t) pbo
);
3683 err (1, "sprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3685 ret_v
= caml_copy_string (s
);
3690 ret_v
= caml_copy_string ("0");
3695 CAMLprim
void ml_freepbo (value s_v
)
3698 char *s
= String_val (s_v
);
3699 struct tile
*tile
= parse_pointer (__func__
, s
);
3702 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3704 tile
->pbo
->ptr
= NULL
;
3705 tile
->pbo
->size
= -1;
3710 CAMLprim
void ml_unmappbo (value s_v
)
3713 char *s
= String_val (s_v
);
3714 struct tile
*tile
= parse_pointer (__func__
, s
);
3717 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3718 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3719 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3721 tile
->pbo
->ptr
= NULL
;
3722 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3727 static void setuppbo (void)
3730 #pragma GCC diagnostic ignored "-Wunused-macros"
3733 static CFBundleRef framework
= NULL
;
3734 if (framework
== NULL
)
3735 framework
= CFBundleGetBundleWithIdentifier (CFSTR ("com.apple.opengl"));
3737 (&state.n = CFBundleGetFunctionPointerForName (framework, CFSTR (#n)))
3740 (*(void (**) (void)) &state.n = glXGetProcAddress ((GLubyte *) #n))
3742 state
.bo_usable
= GGPA (glBindBufferARB
)
3743 && GGPA (glUnmapBufferARB
)
3744 && GGPA (glMapBufferARB
)
3745 && GGPA (glBufferDataARB
)
3746 && GGPA (glGenBuffersARB
)
3747 && GGPA (glDeleteBuffersARB
);
3751 CAMLprim value
ml_bo_usable (value unit_v
)
3753 CAMLparam1 (unit_v
);
3754 CAMLreturn (Val_bool (state
.bo_usable
));
3757 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3759 CAMLparam3 (ptr_v
, x_v
, y_v
);
3760 CAMLlocal2 (ret_v
, tup_v
);
3762 char *s
= String_val (ptr_v
);
3763 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3764 struct pagedim
*pdim
;
3768 page
= parse_pointer (__func__
, s
);
3769 pdim
= &state
.pagedims
[page
->pdimno
];
3771 ret_v
= Val_int (0);
3772 if (trylock (__func__
)) {
3776 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3777 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3778 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3783 p
.x
= x
+ pdim
->bounds
.x0
;
3784 p
.y
= y
+ pdim
->bounds
.y0
;
3786 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3787 fz_invert_matrix (&ctm
, &ctm
);
3788 fz_transform_point (&p
, &ctm
);
3790 tup_v
= caml_alloc_tuple (2);
3791 ret_v
= caml_alloc_small (1, 1);
3792 Field (tup_v
, 0) = Val_int (p
.x
);
3793 Field (tup_v
, 1) = Val_int (p
.y
);
3794 Field (ret_v
, 0) = tup_v
;
3801 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
3802 value x_v
, value y_v
)
3804 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
3807 char *s
= String_val (ptr_v
);
3808 int pageno
= Int_val (pageno_v
);
3809 int pdimno
= Int_val (pdimno_v
);
3810 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
3811 struct pagedim
*pdim
;
3815 ret_v
= Val_int (0);
3819 page
= loadpage (pageno
, pdimno
);
3822 page
= parse_pointer (__func__
, s
);
3824 pdim
= &state
.pagedims
[pdimno
];
3826 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3827 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3828 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3833 p
.x
= x
+ pdim
->bounds
.x0
;
3834 p
.y
= y
+ pdim
->bounds
.y0
;
3836 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3837 fz_transform_point (&p
, &ctm
);
3839 ret_v
= caml_alloc_tuple (2);
3840 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
3841 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
3850 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
3853 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
3854 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3860 char *s
= String_val (ptr_v
);
3862 page
= parse_pointer (__func__
, s
);
3863 annot
= pdf_create_annot (state
.ctx
,
3864 pdf_page_from_fz_page (state
.ctx
,
3867 p
.x
= Int_val (x_v
);
3868 p
.y
= Int_val (y_v
);
3869 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
3870 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
3876 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
3878 CAMLparam2 (ptr_v
, n_v
);
3879 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3883 char *s
= String_val (ptr_v
);
3884 struct slink
*slink
;
3886 page
= parse_pointer (__func__
, s
);
3887 slink
= &page
->slinks
[Int_val (n_v
)];
3888 pdf_delete_annot (state
.ctx
,
3889 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
3890 (pdf_annot
*) slink
->u
.annot
);
3896 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
3898 CAMLparam3 (ptr_v
, n_v
, str_v
);
3899 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3903 char *s
= String_val (ptr_v
);
3904 struct slink
*slink
;
3906 page
= parse_pointer (__func__
, s
);
3907 slink
= &page
->slinks
[Int_val (n_v
)];
3908 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
3909 String_val (str_v
));
3915 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
3917 CAMLparam1 (unit_v
);
3918 CAMLreturn (Val_bool (state
.dirty
));
3921 CAMLprim
void ml_savedoc (value path_v
)
3923 CAMLparam1 (path_v
);
3924 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3927 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
3932 static void makestippletex (void)
3934 const char pixels
[] = "\xff\xff\0\0";
3935 glGenTextures (1, &state
.stid
);
3936 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3937 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3938 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3951 CAMLprim value
ml_fz_version (void)
3953 return caml_copy_string (FZ_VERSION
);
3956 CAMLprim value
ml_llpp_version (void)
3958 extern char llpp_version
[];
3959 return caml_copy_string (llpp_version
);
3962 CAMLprim
void ml_init (value csock_v
, value params_v
)
3964 CAMLparam2 (csock_v
, params_v
);
3965 CAMLlocal2 (trim_v
, fuzz_v
);
3973 state
.csock
= Int_val (csock_v
);
3974 state
.rotate
= Int_val (Field (params_v
, 0));
3975 state
.fitmodel
= Int_val (Field (params_v
, 1));
3976 trim_v
= Field (params_v
, 2);
3977 texcount
= Int_val (Field (params_v
, 3));
3978 state
.sliceheight
= Int_val (Field (params_v
, 4));
3979 mustoresize
= Int_val (Field (params_v
, 5));
3980 colorspace
= Int_val (Field (params_v
, 6));
3981 fontpath
= String_val (Field (params_v
, 7));
3986 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3987 if (setlocale (LC_CTYPE
, "")) {
3988 const char *cset
= nl_langinfo (CODESET
);
3989 state
.utf8cs
= !strcmp (cset
, "UTF-8");
3992 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
3996 if (caml_string_length (Field (params_v
, 8)) > 0) {
3997 state
.trimcachepath
= ystrdup (String_val (Field (params_v
, 8)));
3999 if (!state
.trimcachepath
) {
4000 printd ("emsg failed to strdup trimcachepath: %d:%s",
4001 errno
, strerror (errno
));
4005 haspboext
= Bool_val (Field (params_v
, 9));
4007 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4008 fz_register_document_handlers (state
.ctx
);
4010 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4011 fuzz_v
= Field (trim_v
, 1);
4012 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4013 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4014 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4015 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4017 set_tex_params (colorspace
);
4020 state
.face
= load_font (fontpath
);
4024 const unsigned char *data
;
4026 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
4027 state
.face
= load_builtin_font (data
, len
);
4029 if (!state
.face
) _exit (1);
4031 realloctexts (texcount
);
4039 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4041 errx (1, "pthread_create: %s", strerror (ret
));
4048 static void UNUSED_ATTR NO_OPTIMIZE_ATTR
refmacs (void) {}