1 /* lots of code c&p-ed directly from mupdf */
5 #pragma GCC diagnostic error "-Weverything"
6 #pragma GCC diagnostic ignored "-Wpadded"
7 #pragma GCC diagnostic ignored "-Wsign-conversion"
8 #pragma GCC diagnostic ignored "-Wdocumentation-unknown-command"
9 #pragma GCC diagnostic ignored "-Wdocumentation"
10 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
11 #pragma GCC diagnostic ignored "-Wdouble-promotion"
14 extern char **environ
;
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/utsname.h>
46 #include <OpenGL/gl.h>
51 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
55 #pragma GCC diagnostic ignored "-Wpedantic"
56 #define CAML_NAME_SPACE
57 #include <caml/fail.h>
58 #include <caml/alloc.h>
59 #include <caml/memory.h>
60 #include <caml/unixsupport.h>
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wfloat-equal"
64 #include <mupdf/fitz.h>
65 #include <mupdf/pdf.h>
66 #pragma GCC diagnostic pop
69 #include FT_FREETYPE_H
70 #pragma GCC diagnostic pop
75 #define TEXT_TYPE GL_TEXTURE_2D
77 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
81 #define lprintf printf
86 #define ARSERT(cond) for (;;) { \
88 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
104 struct slice slices
[1];
115 fz_matrix ctm
, zoomctm
, tctm
;
119 enum { SLINK
, SANNOT
} tag
;
140 fz_display_list
*dlist
;
143 struct slink
*slinks
;
145 struct annot
*annots
;
146 fz_stext_char
*fmark
, *lmark
;
149 enum { FitWidth
, FitProportional
, FitPage
};
153 struct pagedim
*pagedims
;
168 fz_colorspace
*colorspace
;
198 void (*glBindBufferARB
) (GLenum
, GLuint
);
199 GLboolean (*glUnmapBufferARB
) (GLenum
);
200 void *(*glMapBufferARB
) (GLenum
, GLenum
);
201 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
202 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
203 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
205 GLfloat texcoords
[8];
206 GLfloat vertices
[16];
208 #ifdef CACHE_PAGEREFS
225 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
227 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
230 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
232 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
235 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
237 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
238 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
241 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
243 static void lock (const char *cap
)
245 int ret
= pthread_mutex_lock (&mutex
);
247 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
251 static void unlock (const char *cap
)
253 int ret
= pthread_mutex_unlock (&mutex
);
255 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
259 static int trylock (const char *cap
)
261 int ret
= pthread_mutex_trylock (&mutex
);
262 if (ret
&& ret
!= EBUSY
) {
263 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
268 static int hasdata (void)
271 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
272 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
276 CAMLprim value
ml_hasdata (value fd_v
)
281 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
282 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
283 CAMLreturn (Val_bool (avail
> 0));
286 static void readdata (int fd
, void *p
, int size
)
291 n
= read (fd
, p
, size
);
293 if (n
< 0 && errno
== EINTR
) goto again
;
294 if (!n
) errx (1, "EOF while reading");
295 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
299 static void writedata (int fd
, char *p
, int size
)
302 uint32_t size4
= size
;
303 struct iovec iov
[2] = {
304 { .iov_base
= &size4
, .iov_len
= 4 },
305 { .iov_base
= p
, .iov_len
= size
}
309 n
= writev (fd
, iov
, 2);
310 if (n
< 0 && errno
== EINTR
) goto again
;
312 if (!n
) errx (1, "EOF while writing data");
313 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
317 static int readlen (int fd
)
320 readdata (fd
, &u
, 4);
324 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
326 CAMLparam3 (fd_v
, bytes_v
, len_v
);
327 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
331 CAMLprim value
ml_rcmd (value fd_v
)
334 CAMLlocal1 (strdata_v
);
335 int fd
= Int_val (fd_v
);
336 int len
= readlen (fd
);
337 strdata_v
= caml_alloc_string (len
);
338 readdata (fd
, String_val (strdata_v
), len
);
339 CAMLreturn (strdata_v
);
342 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
345 int size
= sizeof (fbuf
), len
;
351 len
= vsnprintf (buf
, size
, fmt
, ap
);
355 if (len
< size
- 4) {
356 writedata (state
.csock
, buf
, len
);
362 err (1, "vsnprintf for `%s' failed", fmt
);
364 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
365 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
367 if (buf
!= fbuf
) free (buf
);
370 static void closedoc (void)
372 #ifdef CACHE_PAGEREFS
373 if (state
.pdflut
.objs
) {
374 for (int i
= 0; i
< state
.pdflut
.count
; ++i
) {
375 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
377 free (state
.pdflut
.objs
);
378 state
.pdflut
.objs
= NULL
;
379 state
.pdflut
.idx
= 0;
383 fz_drop_document (state
.ctx
, state
.doc
);
388 static int openxref (char *filename
, char *password
, int layouth
)
390 for (int i
= 0; i
< state
.texcount
; ++i
) {
391 state
.texowners
[i
].w
= -1;
392 state
.texowners
[i
].slice
= NULL
;
398 if (state
.pagedims
) {
399 free (state
.pagedims
);
400 state
.pagedims
= NULL
;
402 state
.pagedimcount
= 0;
404 fz_set_aa_level (state
.ctx
, state
.aalevel
);
405 state
.doc
= fz_open_document (state
.ctx
, filename
);
406 if (fz_needs_password (state
.ctx
, state
.doc
)) {
407 if (password
&& !*password
) {
412 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
414 printd ("pass fail");
420 fz_layout_document (state
.ctx
, state
.doc
, 460, layouth
, 12);
421 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
425 static void docinfo (void)
427 struct { char *tag
; char *name
; } metatbl
[] = {
428 { FZ_META_INFO_TITLE
, "Title" },
429 { FZ_META_INFO_AUTHOR
, "Author" },
430 { FZ_META_FORMAT
, "Format" },
431 { FZ_META_ENCRYPTION
, "Encryption" },
432 { "info:Creator", "Creator" },
433 { "info:Producer", "Producer" },
434 { "info:CreationDate", "Creation date" },
439 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
442 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
443 metatbl
[i
].tag
, buf
, len
);
446 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
449 buf
= realloc (buf
, need
+ 1);
450 if (!buf
) err (1, "docinfo realloc %d", need
+ 1);
461 static void unlinktile (struct tile
*tile
)
463 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
464 struct slice
*s
= &tile
->slices
[i
];
466 if (s
->texindex
!= -1) {
467 if (state
.texowners
[s
->texindex
].slice
== s
) {
468 state
.texowners
[s
->texindex
].slice
= NULL
;
474 static void freepage (struct page
*page
)
478 fz_drop_stext_page (state
.ctx
, page
->text
);
483 fz_drop_display_list (state
.ctx
, page
->dlist
);
484 fz_drop_page (state
.ctx
, page
->fzpage
);
488 static void freetile (struct tile
*tile
)
493 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
494 #else /* piggyback */
496 fz_drop_pixmap (state
.ctx
, state
.pig
);
498 state
.pig
= tile
->pixmap
;
503 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
508 static void trimctm (pdf_page
*page
, int pindex
)
510 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
513 if (!pdim
->tctmready
) {
514 fz_rect realbox
, mediabox
;
515 fz_matrix page_ctm
, ctm
;
517 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
518 realbox
= fz_transform_rect (pdim
->mediabox
, ctm
);
519 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
520 pdim
->tctm
= fz_concat (
521 fz_invert_matrix (page_ctm
),
522 fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
)));
527 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
530 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
532 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
533 if (pdf_specifics (state
.ctx
, state
.doc
)) {
534 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
535 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
538 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
),
544 static fz_matrix
pagectm (struct page
*page
)
546 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
549 static void *loadpage (int pageno
, int pindex
)
554 page
= calloc (sizeof (struct page
), 1);
556 err (1, "calloc page %d", pageno
);
559 page
->dlist
= fz_new_display_list (state
.ctx
, fz_infinite_rect
);
560 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
562 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
563 fz_run_page (state
.ctx
, page
->fzpage
, dev
, fz_identity
, NULL
);
565 fz_catch (state
.ctx
) {
568 fz_close_device (state
.ctx
, dev
);
569 fz_drop_device (state
.ctx
, dev
);
571 page
->pdimno
= pindex
;
572 page
->pageno
= pageno
;
573 page
->sgen
= state
.gen
;
574 page
->agen
= state
.gen
;
575 page
->tgen
= state
.gen
;
579 static struct tile
*alloctile (int h
)
585 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
586 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
587 tile
= calloc (tilesize
, 1);
589 err (1, "cannot allocate tile (%zu bytes)", tilesize
);
591 for (int i
= 0; i
< slicecount
; ++i
) {
592 int sh
= fz_mini (h
, state
.sliceheight
);
593 tile
->slices
[i
].h
= sh
;
594 tile
->slices
[i
].texindex
= -1;
597 tile
->slicecount
= slicecount
;
598 tile
->sliceheight
= state
.sliceheight
;
602 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
609 struct pagedim
*pdim
;
611 tile
= alloctile (h
);
612 pdim
= &state
.pagedims
[page
->pdimno
];
617 bbox
.x1
= bbox
.x0
+ w
;
618 bbox
.y1
= bbox
.y0
+ h
;
621 if (state
.pig
->w
== w
623 && state
.pig
->colorspace
== state
.colorspace
) {
624 tile
->pixmap
= state
.pig
;
625 tile
->pixmap
->x
= bbox
.x0
;
626 tile
->pixmap
->y
= bbox
.y0
;
629 fz_drop_pixmap (state
.ctx
, state
.pig
);
636 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
637 bbox
, NULL
, 1, pbo
->ptr
);
642 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
,
649 fz_clear_pixmap_with_value (state
.ctx
, tile
->pixmap
, 0xff);
651 dev
= fz_new_draw_device (state
.ctx
, fz_identity
, tile
->pixmap
);
652 ctm
= pagectm (page
);
653 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, ctm
,
654 fz_rect_from_irect (bbox
), NULL
);
655 fz_close_device (state
.ctx
, dev
);
656 fz_drop_device (state
.ctx
, dev
);
661 #ifdef CACHE_PAGEREFS
662 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
663 thanks to Robin Watts */
665 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
667 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
671 if (state
.pdflut
.idx
== state
.pagecount
) return;
673 kids
= pdf_dict_gets (ctx
, node
, "Kids");
674 len
= pdf_array_len (ctx
, kids
);
677 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
679 if (pdf_mark_obj (ctx
, node
))
680 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
681 for (int i
= 0; i
< len
; i
++) {
682 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
683 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
685 ? !strcmp (type
, "Pages")
686 : pdf_dict_gets (ctx
, kid
, "Kids")
687 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
688 pdf_collect_pages (doc
, kid
);
692 ? strcmp (type
, "Page") != 0
693 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
694 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
695 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
698 pdf_unmark_obj (ctx
, node
);
702 pdf_load_page_objs (pdf_document
*doc
)
704 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
705 pdf_trailer (state
.ctx
, doc
), "Root");
706 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
709 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
711 state
.pdflut
.idx
= 0;
712 pdf_collect_pages (doc
, node
);
716 static void initpdims (void)
720 fz_rect rootmediabox
= fz_empty_rect
;
721 int pageno
, trim
, show
;
722 int trimw
= 0, cxcount
;
723 fz_context
*ctx
= state
.ctx
;
724 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
731 if (state
.trimmargins
&& state
.trimcachepath
) {
732 trimf
= fopen (state
.trimcachepath
, "rb");
734 trimf
= fopen (state
.trimcachepath
, "wb");
739 if (state
.trimmargins
|| pdf
)
740 cxcount
= state
.pagecount
;
742 cxcount
= fz_mini (state
.pagecount
, 1);
746 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
747 "Root/Pages/MediaBox");
748 rootmediabox
= pdf_to_rect (ctx
, obj
);
751 #ifdef CACHE_PAGEREFS
752 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
753 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
754 if (!state
.pdflut
.objs
) {
755 err (1, "malloc pageobjs %zu %d %zu failed",
756 sizeof (*state
.pdflut
.objs
), cxcount
,
757 sizeof (*state
.pdflut
.objs
) * cxcount
);
759 state
.pdflut
.count
= cxcount
;
760 pdf_load_page_objs (pdf
);
761 state
.pdflut
.pdf
= pdf
;
765 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
768 fz_rect mediabox
= fz_empty_rect
;
772 pdf_obj
*pageref
, *pageobj
;
774 #ifdef CACHE_PAGEREFS
775 pageref
= state
.pdflut
.objs
[pageno
];
777 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
779 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
780 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
782 if (state
.trimmargins
) {
787 page
= pdf_load_page (ctx
, pdf
, pageno
);
788 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
789 trim
= state
.trimanew
|| !obj
;
793 fz_matrix ctm
, page_ctm
;
795 dev
= fz_new_bbox_device (ctx
, &rect
);
796 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
797 ctm
= fz_invert_matrix (page_ctm
);
798 pdf_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
799 fz_close_device (ctx
, dev
);
800 fz_drop_device (ctx
, dev
);
802 rect
.x0
+= state
.trimfuzz
.x0
;
803 rect
.x1
+= state
.trimfuzz
.x1
;
804 rect
.y0
+= state
.trimfuzz
.y0
;
805 rect
.y1
+= state
.trimfuzz
.y1
;
806 rect
= fz_transform_rect (rect
, ctm
);
807 rect
= fz_intersect_rect (rect
, mediabox
);
809 if (!fz_is_empty_rect (rect
)) {
813 obj
= pdf_new_array (ctx
, pdf
, 4);
814 pdf_array_push (ctx
, obj
,
815 pdf_new_real (ctx
, mediabox
.x0
));
816 pdf_array_push (ctx
, obj
,
817 pdf_new_real (ctx
, mediabox
.y0
));
818 pdf_array_push (ctx
, obj
,
819 pdf_new_real (ctx
, mediabox
.x1
));
820 pdf_array_push (ctx
, obj
,
821 pdf_new_real (ctx
, mediabox
.y1
));
822 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
825 mediabox
.x0
= pdf_to_real (ctx
,
826 pdf_array_get (ctx
, obj
, 0));
827 mediabox
.y0
= pdf_to_real (ctx
,
828 pdf_array_get (ctx
, obj
, 1));
829 mediabox
.x1
= pdf_to_real (ctx
,
830 pdf_array_get (ctx
, obj
, 2));
831 mediabox
.y1
= pdf_to_real (ctx
,
832 pdf_array_get (ctx
, obj
, 3));
835 fz_drop_page (ctx
, &page
->super
);
836 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
838 printd ("progress %f Trimming %d",
839 (double) (pageno
+ 1) / state
.pagecount
,
844 printd ("emsg failed to load page %d", pageno
);
852 pdf_to_rect (ctx
, pdf_dict_gets (ctx
, pageobj
, "MediaBox"));
853 if (fz_is_empty_rect (mediabox
)) {
862 pdf_to_rect (ctx
, pdf_dict_gets (ctx
, pageobj
, "CropBox"));
863 if (!fz_is_empty_rect (cropbox
)) {
868 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
873 if (fz_is_empty_rect (rootmediabox
)) {
874 printd ("emsg cannot find page size for page %d",
878 mediabox
= rootmediabox
;
885 if (state
.trimmargins
&& trimw
) {
889 page
= fz_load_page (ctx
, state
.doc
, pageno
);
890 mediabox
= fz_bound_page (ctx
, page
);
891 if (state
.trimmargins
) {
895 dev
= fz_new_bbox_device (ctx
, &rect
);
896 fz_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
897 fz_close_device (ctx
, dev
);
898 fz_drop_device (ctx
, dev
);
900 rect
.x0
+= state
.trimfuzz
.x0
;
901 rect
.x1
+= state
.trimfuzz
.x1
;
902 rect
.y0
+= state
.trimfuzz
.y0
;
903 rect
.y1
+= state
.trimfuzz
.y1
;
904 rect
= fz_intersect_rect (rect
, mediabox
);
906 if (!fz_is_empty_rect (rect
)) {
910 fz_drop_page (ctx
, page
);
915 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
917 err (1, "fwrite trim mediabox");
923 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
925 err (1, "fread trim mediabox %d", pageno
);
931 page
= fz_load_page (ctx
, state
.doc
, pageno
);
932 mediabox
= fz_bound_page (ctx
, page
);
933 fz_drop_page (ctx
, page
);
935 show
= !state
.trimmargins
&& pageno
% 20 == 0;
937 printd ("progress %f Gathering dimensions %d",
938 (double) (pageno
) / state
.pagecount
,
943 printd ("emsg failed to load page %d", pageno
);
949 if (state
.pagedimcount
== 0
950 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
951 , p
->rotate
!= rotate
)
952 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
955 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
956 state
.pagedims
= realloc (state
.pagedims
, size
);
957 if (!state
.pagedims
) {
958 err (1, "realloc pagedims to %zu (%d elems)",
959 size
, state
.pagedimcount
+ 1);
962 p
= &state
.pagedims
[state
.pagedimcount
++];
964 p
->mediabox
= mediabox
;
969 printd ("progress 1 %s %d pages in %f seconds",
970 state
.trimmargins
? "Trimmed" : "Processed",
971 state
.pagecount
, end
- start
);
974 if (fclose (trimf
)) {
980 static void layout (void)
985 struct pagedim
*p
= NULL
;
986 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
988 if (state
.pagedimcount
== 0) return;
990 switch (state
.fitmodel
) {
991 case FitProportional
:
992 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
995 p
= &state
.pagedims
[pindex
];
996 box
= fz_transform_rect (p
->mediabox
,
997 fz_rotate (p
->rotate
+ state
.rotate
));
999 x0
= fz_min (box
.x0
, box
.x1
);
1000 x1
= fz_max (box
.x0
, box
.x1
);
1003 maxw
= fz_max (w
, maxw
);
1004 zoom
= state
.w
/ maxw
;
1016 ARSERT (0 && state
.fitmodel
);
1019 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1020 p
= &state
.pagedims
[pindex
];
1021 ctm
= fz_rotate (state
.rotate
);
1022 box
= fz_transform_rect (p
->mediabox
,
1023 fz_rotate (p
->rotate
+ state
.rotate
));
1024 w
= box
.x1
- box
.x0
;
1025 switch (state
.fitmodel
) {
1026 case FitProportional
:
1027 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
1033 h
= box
.y1
- box
.y0
;
1035 zoom
= fz_min (zw
, zh
);
1036 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
1045 p
->zoomctm
= fz_scale (zoom
, zoom
);
1046 ctm
= fz_concat (p
->zoomctm
, ctm
);
1048 p
->pagebox
= p
->mediabox
;
1049 p
->pagebox
= fz_transform_rect (p
->pagebox
, fz_rotate (p
->rotate
));
1050 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1051 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1054 p
->bounds
= fz_round_rect (fz_transform_rect (p
->pagebox
, ctm
));
1057 ctm
= fz_concat (fz_translate (0, -p
->mediabox
.y1
),
1058 fz_scale (zoom
, -zoom
));
1063 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1064 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1065 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1066 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1067 int boundw
= x1
- x0
;
1068 int boundh
= y1
- y0
;
1070 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1071 } while (p
-- != state
.pagedims
);
1074 struct pagedim
*pdimofpageno (int pageno
)
1076 struct pagedim
*pdim
= state
.pagedims
;
1078 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1079 if (state
.pagedims
[i
].pageno
> pageno
)
1081 pdim
= &state
.pagedims
[i
];
1086 static void recurse_outline (fz_outline
*outline
, int level
)
1089 if (outline
->page
>= 0) {
1090 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
1091 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
1092 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1093 p
= fz_transform_point (p
, pdim
->ctm
);
1094 printd ("o %d %d %d %d %s",
1095 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
1098 printd ("on %d %s", level
, outline
->title
);
1100 if (outline
->down
) {
1101 recurse_outline (outline
->down
, level
+ 1);
1103 outline
= outline
->next
;
1107 static void process_outline (void)
1109 fz_outline
*outline
;
1111 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1113 state
.needoutline
= 0;
1114 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1116 recurse_outline (outline
, 0);
1117 fz_drop_outline (state
.ctx
, outline
);
1121 static char *strofline (fz_stext_line
*line
)
1126 size_t size
= 0, cap
= 80;
1128 p
= malloc (cap
+ 1);
1129 if (!p
) return NULL
;
1131 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1132 int n
= fz_runetochar (utf8
, ch
->c
);
1133 if (size
+ n
> cap
) {
1135 p
= realloc (p
, cap
+ 1);
1136 if (!p
) return NULL
;
1139 memcpy (p
+ size
, utf8
, n
);
1146 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1147 int stop
, int pageno
, double start
)
1153 p
= strofline (line
);
1156 ret
= regexec (re
, p
, 1, &rm
, 0);
1159 if (ret
!= REG_NOMATCH
) {
1162 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1163 printd ("msg regexec error `%.*s'",
1164 (int) size
, errbuf
);
1174 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1175 o
+= fz_runelen (ch
->c
);
1181 for (;ch
; ch
= ch
->next
) {
1182 o
+= fz_runelen (ch
->c
);
1183 if (o
> rm
.rm_eo
) break;
1188 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1195 printd ("progress 1 found at %d `%.*s' in %f sec",
1196 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1200 printd ("match %d %d %f %f %f %f %f %f %f %f",
1212 /* wishful thinking function */
1213 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1216 fz_stext_page
*text
;
1217 struct pagedim
*pdim
;
1218 int stop
= 0, niters
= 0;
1221 fz_stext_block
*block
;
1224 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1225 if (niters
++ == 5) {
1228 printd ("progress 1 attention requested aborting search at %d",
1233 printd ("progress %f searching in page %d",
1234 (double) (pageno
+ 1) / state
.pagecount
,
1238 pdim
= pdimofpageno (pageno
);
1239 text
= fz_new_stext_page (state
.ctx
, pdim
->mediabox
);
1240 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1242 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1243 fz_run_page (state
.ctx
, page
, tdev
, pagectm1 (page
, pdim
), NULL
);
1245 fz_close_device (state
.ctx
, tdev
);
1246 fz_drop_device (state
.ctx
, tdev
);
1249 for (block
= text
->first_block
; block
; block
= block
->next
) {
1250 fz_stext_line
*line
;
1252 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1253 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1254 if (line
->bbox
.y0
< y
+ 1) continue;
1256 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1258 case 1: stop
= 1; break;
1259 case -1: stop
= 1; goto endloop
;
1265 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1266 fz_stext_line
*line
;
1268 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1269 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1270 if (line
->bbox
.y0
< y
+ 1) continue;
1272 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1274 case 1: stop
= 1; break;
1275 case -1: stop
= 1; goto endloop
;
1290 fz_drop_stext_page (state
.ctx
, text
);
1291 fz_drop_page (state
.ctx
, page
);
1295 printd ("progress 1 no matches %f sec", end
- start
);
1297 printd ("clearrects");
1300 static void set_tex_params (int colorspace
)
1302 switch (colorspace
) {
1304 state
.texiform
= GL_RGBA8
;
1305 state
.texform
= GL_RGBA
;
1306 state
.texty
= GL_UNSIGNED_BYTE
;
1307 state
.colorspace
= fz_device_rgb (state
.ctx
);
1310 state
.texiform
= GL_LUMINANCE_ALPHA
;
1311 state
.texform
= GL_LUMINANCE_ALPHA
;
1312 state
.texty
= GL_UNSIGNED_BYTE
;
1313 state
.colorspace
= fz_device_gray (state
.ctx
);
1316 errx (1, "invalid colorspce %d", colorspace
);
1320 static void realloctexts (int texcount
)
1324 if (texcount
== state
.texcount
) return;
1326 if (texcount
< state
.texcount
) {
1327 glDeleteTextures (state
.texcount
- texcount
,
1328 state
.texids
+ texcount
);
1331 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1332 state
.texids
= realloc (state
.texids
, size
);
1333 if (!state
.texids
) {
1334 err (1, "realloc texs %zu", size
);
1337 state
.texowners
= (void *) (state
.texids
+ texcount
);
1338 if (texcount
> state
.texcount
) {
1339 glGenTextures (texcount
- state
.texcount
,
1340 state
.texids
+ state
.texcount
);
1341 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1342 state
.texowners
[i
].w
= -1;
1343 state
.texowners
[i
].slice
= NULL
;
1346 state
.texcount
= texcount
;
1350 static char *mbtoutf8 (char *s
)
1360 len
= mbstowcs (NULL
, s
, strlen (s
));
1361 if (len
== 0 || len
== (size_t) -1) {
1363 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1367 tmp
= calloc (len
, sizeof (wchar_t));
1369 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1370 len
, sizeof (wchar_t), errno
, strerror (errno
));
1374 ret
= mbstowcs (tmp
, s
, len
);
1375 if (ret
== (size_t) -1) {
1376 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1377 len
, errno
, strerror (errno
));
1383 for (i
= 0; i
< ret
; ++i
) {
1384 len
+= fz_runelen (tmp
[i
]);
1387 p
= r
= malloc (len
+ 1);
1389 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1394 for (i
= 0; i
< ret
; ++i
) {
1395 p
+= fz_runetochar (p
, tmp
[i
]);
1402 CAMLprim value
ml_mbtoutf8 (value s_v
)
1408 s
= String_val (s_v
);
1414 ret_v
= caml_copy_string (r
);
1420 static void * mainloop (void UNUSED_ATTR
*unused
)
1423 int len
, ret
, oldlen
= 0;
1428 len
= readlen (state
.csock
);
1430 errx (1, "readlen returned 0");
1433 if (oldlen
< len
+ 1) {
1434 p
= realloc (p
, len
+ 1);
1436 err (1, "realloc %d failed", len
+ 1);
1440 readdata (state
.csock
, p
, len
);
1443 if (!strncmp ("open", p
, 4)) {
1444 int off
, usedoccss
, ok
= 0, layouth
;
1451 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1453 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1456 filename
= p
+ 5 + off
;
1457 filenamelen
= strlen (filename
);
1458 password
= filename
+ filenamelen
+ 1;
1460 if (password
[strlen (password
) + 1]) {
1461 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1465 fz_set_use_document_css (state
.ctx
, usedoccss
);
1466 fz_try (state
.ctx
) {
1467 ok
= openxref (filename
, password
, layouth
);
1469 fz_catch (state
.ctx
) {
1470 utf8filename
= mbtoutf8 (filename
);
1471 printd ("msg Could not open %s", utf8filename
);
1472 if (utf8filename
!= filename
) {
1473 free (utf8filename
);
1483 utf8filename
= mbtoutf8 (filename
);
1484 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1485 if (utf8filename
!= filename
) {
1486 free (utf8filename
);
1488 state
.needoutline
= 1;
1491 else if (!strncmp ("cs", p
, 2)) {
1494 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1496 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1499 set_tex_params (colorspace
);
1500 for (i
= 0; i
< state
.texcount
; ++i
) {
1501 state
.texowners
[i
].w
= -1;
1502 state
.texowners
[i
].slice
= NULL
;
1506 else if (!strncmp ("freepage", p
, 8)) {
1509 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1511 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1515 unlock ("freepage");
1517 else if (!strncmp ("freetile", p
, 8)) {
1520 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1522 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1526 unlock ("freetile");
1528 else if (!strncmp ("search", p
, 6)) {
1529 int icase
, pageno
, y
, len2
, forward
;
1533 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1534 &icase
, &pageno
, &y
, &forward
, &len2
);
1536 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1539 pattern
= p
+ 6 + len2
;
1540 ret
= regcomp (&re
, pattern
,
1541 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1546 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1547 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1550 search (&re
, pageno
, y
, forward
);
1554 else if (!strncmp ("geometry", p
, 8)) {
1558 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1560 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1567 for (int i
= 0; i
< state
.texcount
; ++i
) {
1568 state
.texowners
[i
].slice
= NULL
;
1571 state
.fitmodel
= fitmodel
;
1576 unlock ("geometry");
1577 printd ("continue %d", state
.pagecount
);
1579 else if (!strncmp ("reqlayout", p
, 9)) {
1586 ret
= sscanf (p
+ 9, " %d %d %d %n",
1587 &rotate
, &fitmodel
, &h
, &off
);
1589 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1592 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1593 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1596 state
.rotate
= rotate
;
1597 state
.fitmodel
= fitmodel
;
1602 nameddest
= p
+ 9 + off
;
1603 if (pdf
&& nameddest
&& *nameddest
) {
1605 struct pagedim
*pdim
;
1606 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1608 pdim
= pdimofpageno (pageno
);
1609 xy
= fz_transform_point (xy
, pdim
->ctm
);
1610 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1614 unlock ("reqlayout");
1615 printd ("continue %d", state
.pagecount
);
1617 else if (!strncmp ("page", p
, 4)) {
1622 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1624 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1629 page
= loadpage (pageno
, pindex
);
1633 printd ("page %" PRIxPTR
" %f", (uintptr_t) page
, b
- a
);
1635 else if (!strncmp ("tile", p
, 4)) {
1642 ret
= sscanf (p
+ 4, " %" SCNxPTR
" %d %d %d %d %" SCNxPTR
,
1643 (uintptr_t *) &page
, &x
, &y
, &w
, &h
,
1644 (uintptr_t *) &data
);
1646 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1651 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1655 printd ("tile %d %d %" PRIxPTR
" %u %f",
1656 x
, y
, (uintptr_t) (tile
),
1657 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1660 else if (!strncmp ("trimset", p
, 7)) {
1664 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1665 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1667 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1670 state
.trimmargins
= trimmargins
;
1671 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1673 state
.trimfuzz
= fuzz
;
1677 else if (!strncmp ("settrim", p
, 7)) {
1681 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1682 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1684 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1688 state
.trimmargins
= trimmargins
;
1689 state
.needoutline
= 1;
1690 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1692 state
.trimfuzz
= fuzz
;
1694 state
.pagedimcount
= 0;
1695 free (state
.pagedims
);
1696 state
.pagedims
= NULL
;
1701 printd ("continue %d", state
.pagecount
);
1703 else if (!strncmp ("sliceh", p
, 6)) {
1706 ret
= sscanf (p
+ 6, " %d", &h
);
1708 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1710 if (h
!= state
.sliceheight
) {
1711 state
.sliceheight
= h
;
1712 for (int i
= 0; i
< state
.texcount
; ++i
) {
1713 state
.texowners
[i
].w
= -1;
1714 state
.texowners
[i
].h
= -1;
1715 state
.texowners
[i
].slice
= NULL
;
1719 else if (!strncmp ("interrupt", p
, 9)) {
1720 printd ("vmsg interrupted");
1723 errx (1, "unknown command %.*s", len
, p
);
1729 CAMLprim value
ml_isexternallink (value uri_v
)
1732 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1733 CAMLreturn (Val_bool (ext
));
1736 CAMLprim value
ml_uritolocation (value uri_v
)
1742 struct pagedim
*pdim
;
1744 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1746 pdim
= pdimofpageno (pageno
);
1747 xy
= fz_transform_point (xy
, pdim
->ctm
);
1748 ret_v
= caml_alloc_tuple (3);
1749 Field (ret_v
, 0) = Val_int (pageno
);
1750 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1751 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1755 CAMLprim value
ml_realloctexts (value texcount_v
)
1757 CAMLparam1 (texcount_v
);
1760 if (trylock (__func__
)) {
1764 realloctexts (Int_val (texcount_v
));
1769 CAMLreturn (Val_bool (ok
));
1772 static void recti (int x0
, int y0
, int x1
, int y1
)
1774 GLfloat
*v
= state
.vertices
;
1776 glVertexPointer (2, GL_FLOAT
, 0, v
);
1777 v
[0] = x0
; v
[1] = y0
;
1778 v
[2] = x1
; v
[3] = y0
;
1779 v
[4] = x0
; v
[5] = y1
;
1780 v
[6] = x1
; v
[7] = y1
;
1781 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1784 static void showsel (struct page
*page
, int ox
, int oy
)
1788 fz_stext_block
*block
;
1790 unsigned char selcolor
[] = {15,15,15,140};
1792 if (!page
->fmark
|| !page
->lmark
) return;
1794 glEnable (GL_BLEND
);
1795 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1796 glColor4ubv (selcolor
);
1798 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1799 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1801 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1802 fz_stext_line
*line
;
1804 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1805 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1808 rect
= fz_empty_rect
;
1809 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1811 if (ch
== page
->fmark
) seen
= 1;
1812 r
= fz_rect_from_quad (ch
->quad
);
1813 if (seen
) rect
= fz_union_rect (rect
, r
);
1814 if (ch
== page
->lmark
) {
1815 bbox
= fz_round_rect (rect
);
1816 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1817 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1821 bbox
= fz_round_rect (rect
);
1822 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1823 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1827 glDisable (GL_BLEND
);
1830 #pragma GCC diagnostic push
1831 #pragma GCC diagnostic ignored "-Wconversion"
1833 #pragma GCC diagnostic pop
1835 static void stipplerect (fz_matrix m
,
1843 p1
= fz_transform_point (p1
, m
);
1844 p2
= fz_transform_point (p2
, m
);
1845 p3
= fz_transform_point (p3
, m
);
1846 p4
= fz_transform_point (p4
, m
);
1852 t
= hypotf (w
, h
) * .25f
;
1856 s
= hypotf (w
, h
) * .25f
;
1858 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1859 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1861 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
1862 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
1864 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
1865 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
1867 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
1868 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
1870 glDrawArrays (GL_LINES
, 0, 8);
1873 static void solidrect (fz_matrix m
,
1880 p1
= fz_transform_point (p1
, m
);
1881 p2
= fz_transform_point (p2
, m
);
1882 p3
= fz_transform_point (p3
, m
);
1883 p4
= fz_transform_point (p4
, m
);
1884 vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1885 vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1887 vertices
[4] = p3
.x
; vertices
[5] = p3
.y
;
1888 vertices
[6] = p4
.x
; vertices
[7] = p4
.y
;
1889 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
1892 static void ensurelinks (struct page
*page
)
1895 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
1898 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1902 GLfloat
*texcoords
= state
.texcoords
;
1903 GLfloat
*vertices
= state
.vertices
;
1907 glEnable (GL_TEXTURE_1D
);
1908 glEnable (GL_BLEND
);
1909 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1910 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
1912 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1913 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1914 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1916 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
1917 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
1919 for (link
= page
->links
; link
; link
= link
->next
) {
1920 fz_point p1
, p2
, p3
, p4
;
1922 p1
.x
= link
->rect
.x0
;
1923 p1
.y
= link
->rect
.y0
;
1925 p2
.x
= link
->rect
.x1
;
1926 p2
.y
= link
->rect
.y0
;
1928 p3
.x
= link
->rect
.x1
;
1929 p3
.y
= link
->rect
.y1
;
1931 p4
.x
= link
->rect
.x0
;
1932 p4
.y
= link
->rect
.y1
;
1934 /* TODO: different colours for different schemes */
1935 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
1936 else glColor3ub (255, 0, 0);
1938 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1941 for (int i
= 0; i
< page
->annotcount
; ++i
) {
1942 fz_point p1
, p2
, p3
, p4
;
1943 struct annot
*annot
= &page
->annots
[i
];
1945 p1
.x
= annot
->bbox
.x0
;
1946 p1
.y
= annot
->bbox
.y0
;
1948 p2
.x
= annot
->bbox
.x1
;
1949 p2
.y
= annot
->bbox
.y0
;
1951 p3
.x
= annot
->bbox
.x1
;
1952 p3
.y
= annot
->bbox
.y1
;
1954 p4
.x
= annot
->bbox
.x0
;
1955 p4
.y
= annot
->bbox
.y1
;
1957 glColor3ub (0, 0, 128);
1958 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1961 glDisable (GL_BLEND
);
1962 glDisable (GL_TEXTURE_1D
);
1965 static int compareslinks (const void *l
, const void *r
)
1967 struct slink
const *ls
= l
;
1968 struct slink
const *rs
= r
;
1969 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1970 return rs
->bbox
.x0
- rs
->bbox
.x0
;
1972 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1975 static void droptext (struct page
*page
)
1978 fz_drop_stext_page (state
.ctx
, page
->text
);
1985 static void dropannots (struct page
*page
)
1988 free (page
->annots
);
1989 page
->annots
= NULL
;
1990 page
->annotcount
= 0;
1994 static void ensureannots (struct page
*page
)
1997 size_t annotsize
= sizeof (*page
->annots
);
2000 if (state
.gen
!= page
->agen
) {
2002 page
->agen
= state
.gen
;
2004 if (page
->annots
) return;
2006 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2008 annot
= fz_next_annot (state
.ctx
, annot
)) {
2013 page
->annotcount
= count
;
2014 page
->annots
= calloc (count
, annotsize
);
2015 if (!page
->annots
) {
2016 err (1, "calloc annots %d", count
);
2019 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2021 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2024 rect
= fz_bound_annot (state
.ctx
, annot
);
2025 page
->annots
[i
].annot
= annot
;
2026 page
->annots
[i
].bbox
= fz_round_rect (rect
);
2031 static void dropslinks (struct page
*page
)
2034 free (page
->slinks
);
2035 page
->slinks
= NULL
;
2036 page
->slinkcount
= 0;
2039 fz_drop_link (state
.ctx
, page
->links
);
2044 static void ensureslinks (struct page
*page
)
2048 size_t slinksize
= sizeof (*page
->slinks
);
2051 ensureannots (page
);
2052 if (state
.gen
!= page
->sgen
) {
2054 page
->sgen
= state
.gen
;
2056 if (page
->slinks
) return;
2059 ctm
= pagectm (page
);
2061 count
= page
->annotcount
;
2062 for (link
= page
->links
; link
; link
= link
->next
) {
2068 page
->slinkcount
= count
;
2069 page
->slinks
= calloc (count
, slinksize
);
2070 if (!page
->slinks
) {
2071 err (1, "calloc slinks %d", count
);
2074 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
2078 rect
= fz_transform_rect (rect
, ctm
);
2079 page
->slinks
[i
].tag
= SLINK
;
2080 page
->slinks
[i
].u
.link
= link
;
2081 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
2083 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2085 rect
= fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
);
2086 rect
= fz_transform_rect (rect
, ctm
);
2087 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
2089 page
->slinks
[i
].tag
= SANNOT
;
2090 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2092 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2096 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2097 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2100 struct slink
*slink
;
2101 float x0
, y0
, x1
, y1
, w
;
2103 ensureslinks (page
);
2104 glColor3ub (0xc3, 0xb0, 0x91);
2105 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2106 fmt_linkn (buf
, i
+ noff
);
2107 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2108 slink
= &page
->slinks
[i
];
2110 x0
= slink
->bbox
.x0
+ xoff
- 5;
2111 y1
= slink
->bbox
.y0
+ yoff
- 5;
2112 y0
= y1
+ 10 + hfsize
;
2113 w
= measure_string (state
.face
, hfsize
, buf
);
2115 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2119 glEnable (GL_BLEND
);
2120 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2121 glEnable (GL_TEXTURE_2D
);
2122 glColor3ub (0, 0, 0);
2123 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2124 fmt_linkn (buf
, i
+ noff
);
2125 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2126 slink
= &page
->slinks
[i
];
2128 x0
= slink
->bbox
.x0
+ xoff
;
2129 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2130 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2133 glDisable (GL_TEXTURE_2D
);
2134 glDisable (GL_BLEND
);
2137 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2140 struct slice
*slice1
;
2141 unsigned char *texdata
;
2144 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2145 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2147 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2148 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2149 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2153 int texindex
= state
.texindex
++ % state
.texcount
;
2155 if (state
.texowners
[texindex
].w
== tile
->w
) {
2156 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2160 state
.texowners
[texindex
].h
= slice
->h
;
2164 state
.texowners
[texindex
].h
= slice
->h
;
2167 state
.texowners
[texindex
].w
= tile
->w
;
2168 state
.texowners
[texindex
].slice
= slice
;
2169 slice
->texindex
= texindex
;
2171 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2172 #if TEXT_TYPE == GL_TEXTURE_2D
2173 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2174 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2175 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2176 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2179 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2183 texdata
= tile
->pixmap
->samples
;
2186 glTexSubImage2D (TEXT_TYPE
,
2198 glTexImage2D (TEXT_TYPE
,
2210 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2215 CAMLprim
void ml_begintiles (void)
2217 glEnable (TEXT_TYPE
);
2218 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2219 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2222 CAMLprim
void ml_endtiles (void)
2224 glDisable (TEXT_TYPE
);
2227 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2229 CAMLparam2 (args_v
, ptr_v
);
2230 int dispx
= Int_val (Field (args_v
, 0));
2231 int dispy
= Int_val (Field (args_v
, 1));
2232 int dispw
= Int_val (Field (args_v
, 2));
2233 int disph
= Int_val (Field (args_v
, 3));
2234 int tilex
= Int_val (Field (args_v
, 4));
2235 int tiley
= Int_val (Field (args_v
, 5));
2236 char *s
= String_val (ptr_v
);
2237 struct tile
*tile
= parse_pointer (__func__
, s
);
2238 int slicey
, firstslice
;
2239 struct slice
*slice
;
2240 GLfloat
*texcoords
= state
.texcoords
;
2241 GLfloat
*vertices
= state
.vertices
;
2243 firstslice
= tiley
/ tile
->sliceheight
;
2244 slice
= &tile
->slices
[firstslice
];
2245 slicey
= tiley
% tile
->sliceheight
;
2250 dh
= slice
->h
- slicey
;
2251 dh
= fz_mini (disph
, dh
);
2252 uploadslice (tile
, slice
);
2254 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2255 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2256 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2257 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2259 vertices
[0] = dispx
; vertices
[1] = dispy
;
2260 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2261 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2262 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2264 #if TEXT_TYPE == GL_TEXTURE_2D
2265 for (int i
= 0; i
< 8; ++i
) {
2266 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2270 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2274 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2280 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2283 fz_point p1
, p2
, p3
, p4
;
2284 GLfloat
*vertices
= state
.vertices
;
2285 double *v
= (double *) rects_v
;
2287 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2288 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2289 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
2291 glEnable (GL_BLEND
);
2292 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2295 p1
.x
= (float) v
[4];
2296 p1
.y
= (float) v
[5];
2298 p2
.x
= (float) v
[6];
2299 p2
.y
= (float) v
[5];
2301 p3
.x
= (float) v
[6];
2302 p3
.y
= (float) v
[7];
2304 p4
.x
= (float) v
[4];
2305 p4
.y
= (float) v
[7];
2306 solidrect (ctm
, p1
, p2
, p3
, p4
, vertices
);
2307 glDisable (GL_BLEND
);
2310 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2311 value xoff_v
, value yoff_v
,
2314 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2315 int xoff
= Int_val (xoff_v
);
2316 int yoff
= Int_val (yoff_v
);
2317 int noff
= Int_val (Field (li_v
, 0));
2318 char *targ
= String_val (Field (li_v
, 1));
2319 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2320 int hfsize
= Int_val (Field (li_v
, 2));
2321 char *s
= String_val (ptr_v
);
2322 int hlmask
= Int_val (hlinks_v
);
2323 struct page
*page
= parse_pointer (__func__
, s
);
2325 if (!page
->fzpage
) {
2326 /* deal with loadpage failed pages */
2330 if (trylock (__func__
)) {
2335 ensureannots (page
);
2336 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2338 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2339 noff
= page
->slinkcount
;
2341 if (page
->tgen
== state
.gen
) {
2342 showsel (page
, xoff
, yoff
);
2347 CAMLreturn (Val_int (noff
));
2350 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2353 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2354 int xoff
= Int_val (xoff_v
);
2355 int yoff
= Int_val (yoff_v
);
2356 char *s
= String_val (ptr_v
);
2357 struct page
*page
= parse_pointer (__func__
, s
);
2359 drawprect (page
, xoff
, yoff
, rects_v
);
2363 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2367 const fz_matrix
*tctm
;
2368 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2370 if (!page
->annots
) return NULL
;
2373 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2374 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2377 tctm
= &fz_identity
;
2383 ctm
= fz_concat (*tctm
, state
.pagedims
[page
->pdimno
].ctm
);
2384 ctm
= fz_invert_matrix (ctm
);
2385 p
= fz_transform_point (p
, ctm
);
2388 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2389 struct annot
*a
= &page
->annots
[i
];
2392 rect
= fz_bound_annot (state
.ctx
, a
->annot
);
2393 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2394 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2402 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2407 ensureslinks (page
);
2412 p
= fz_transform_point (p
, fz_invert_matrix (pagectm (page
)));
2414 for (link
= page
->links
; link
; link
= link
->next
) {
2415 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2416 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2424 static void ensuretext (struct page
*page
)
2426 if (state
.gen
!= page
->tgen
) {
2428 page
->tgen
= state
.gen
;
2433 page
->text
= fz_new_stext_page (state
.ctx
,
2434 state
.pagedims
[page
->pdimno
].mediabox
);
2435 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2436 fz_run_display_list (state
.ctx
, page
->dlist
,
2437 tdev
, pagectm (page
), fz_infinite_rect
, NULL
);
2438 fz_close_device (state
.ctx
, tdev
);
2439 fz_drop_device (state
.ctx
, tdev
);
2443 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2445 CAMLparam2 (start_page_v
, dir_v
);
2447 int i
, dir
= Int_val (dir_v
);
2448 int start_page
= Int_val (start_page_v
);
2449 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2453 ret_v
= Val_int (0);
2455 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2456 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2461 pdf_page
*page
= NULL
;
2464 fz_try (state
.ctx
) {
2465 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2466 found
= !!page
->links
|| !!page
->annots
;
2468 fz_catch (state
.ctx
) {
2472 fz_drop_page (state
.ctx
, &page
->super
);
2476 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2477 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2479 fz_drop_link (state
.ctx
, link
);
2480 fz_drop_page (state
.ctx
, page
);
2484 ret_v
= caml_alloc_small (1, 1);
2485 Field (ret_v
, 0) = Val_int (i
);
2494 enum { dir_first
, dir_last
};
2495 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2497 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2499 CAMLparam2 (ptr_v
, dir_v
);
2500 CAMLlocal2 (ret_v
, pos_v
);
2502 int dirtag
, i
, slinkindex
;
2503 struct slink
*found
= NULL
,*slink
;
2504 char *s
= String_val (ptr_v
);
2506 page
= parse_pointer (__func__
, s
);
2507 ret_v
= Val_int (0);
2509 ensureslinks (page
);
2511 if (Is_block (dir_v
)) {
2512 dirtag
= Tag_val (dir_v
);
2514 case dir_first_visible
:
2516 int x0
, y0
, dir
, first_index
, last_index
;
2518 pos_v
= Field (dir_v
, 0);
2519 x0
= Int_val (Field (pos_v
, 0));
2520 y0
= Int_val (Field (pos_v
, 1));
2521 dir
= Int_val (Field (pos_v
, 2));
2526 last_index
= page
->slinkcount
;
2529 first_index
= page
->slinkcount
- 1;
2533 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2534 slink
= &page
->slinks
[i
];
2535 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2544 slinkindex
= Int_val (Field (dir_v
, 0));
2545 found
= &page
->slinks
[slinkindex
];
2546 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2547 slink
= &page
->slinks
[i
];
2548 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2556 slinkindex
= Int_val (Field (dir_v
, 0));
2557 found
= &page
->slinks
[slinkindex
];
2558 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2559 slink
= &page
->slinks
[i
];
2560 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2568 slinkindex
= Int_val (Field (dir_v
, 0));
2569 found
= &page
->slinks
[slinkindex
];
2570 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2571 slink
= &page
->slinks
[i
];
2572 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2580 slinkindex
= Int_val (Field (dir_v
, 0));
2581 found
= &page
->slinks
[slinkindex
];
2582 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2583 slink
= &page
->slinks
[i
];
2584 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2593 dirtag
= Int_val (dir_v
);
2596 found
= page
->slinks
;
2601 found
= page
->slinks
+ (page
->slinkcount
- 1);
2607 ret_v
= caml_alloc_small (2, 1);
2608 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2615 enum { uuri
, utext
, uannot
};
2617 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2619 CAMLparam2 (ptr_v
, n_v
);
2620 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2623 char *s
= String_val (ptr_v
);
2624 struct slink
*slink
;
2626 ret_v
= Val_int (0);
2627 page
= parse_pointer (__func__
, s
);
2630 ensureslinks (page
);
2631 slink
= &page
->slinks
[Int_val (n_v
)];
2632 if (slink
->tag
== SLINK
) {
2633 link
= slink
->u
.link
;
2634 str_v
= caml_copy_string (link
->uri
);
2635 ret_v
= caml_alloc_small (1, uuri
);
2636 Field (ret_v
, 0) = str_v
;
2639 ret_v
= caml_alloc_small (1, uannot
);
2640 tup_v
= caml_alloc_tuple (2);
2641 Field (ret_v
, 0) = tup_v
;
2642 Field (tup_v
, 0) = ptr_v
;
2643 Field (tup_v
, 1) = n_v
;
2650 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2652 CAMLparam2 (ptr_v
, n_v
);
2655 const char *contents
= "";
2658 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2660 char *s
= String_val (ptr_v
);
2662 struct slink
*slink
;
2664 page
= parse_pointer (__func__
, s
);
2665 slink
= &page
->slinks
[Int_val (n_v
)];
2666 contents
= pdf_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
);
2669 ret_v
= caml_copy_string (contents
);
2673 CAMLprim value
ml_getlinkcount (value ptr_v
)
2677 char *s
= String_val (ptr_v
);
2679 page
= parse_pointer (__func__
, s
);
2680 CAMLreturn (Val_int (page
->slinkcount
));
2683 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2685 CAMLparam2 (ptr_v
, n_v
);
2688 struct slink
*slink
;
2689 char *s
= String_val (ptr_v
);
2691 page
= parse_pointer (__func__
, s
);
2692 ret_v
= caml_alloc_tuple (4);
2694 ensureslinks (page
);
2696 slink
= &page
->slinks
[Int_val (n_v
)];
2697 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2698 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2699 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2700 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2705 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2707 CAMLparam3 (ptr_v
, x_v
, y_v
);
2708 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2710 struct annot
*annot
;
2712 char *ptr
= String_val (ptr_v
);
2713 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2714 struct pagedim
*pdim
;
2716 ret_v
= Val_int (0);
2717 if (trylock (__func__
)) {
2721 page
= parse_pointer (__func__
, ptr
);
2722 pdim
= &state
.pagedims
[page
->pdimno
];
2723 x
+= pdim
->bounds
.x0
;
2724 y
+= pdim
->bounds
.y0
;
2727 annot
= getannot (page
, x
, y
);
2731 ensureslinks (page
);
2732 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2733 if (page
->slinks
[i
].tag
== SANNOT
2734 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2739 ret_v
= caml_alloc_small (1, uannot
);
2740 tup_v
= caml_alloc_tuple (2);
2741 Field (ret_v
, 0) = tup_v
;
2742 Field (tup_v
, 0) = ptr_v
;
2743 Field (tup_v
, 1) = Val_int (n
);
2748 link
= getlink (page
, x
, y
);
2750 str_v
= caml_copy_string (link
->uri
);
2751 ret_v
= caml_alloc_small (1, uuri
);
2752 Field (ret_v
, 0) = str_v
;
2756 fz_stext_block
*block
;
2760 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2761 fz_stext_line
*line
;
2763 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2765 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2768 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2772 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2775 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2776 fz_quad
*q
= &ch
->quad
;
2778 if (x
>= q
->ul
.x
&& x
<= q
->ur
.x
2779 && y
>= q
->ul
.y
&& y
<= q
->ll
.y
) {
2780 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
2781 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
2784 if (!n2
) n2
= "<unknown font>";
2786 if (face
&& face
->family_name
) {
2788 char *n1
= face
->family_name
;
2789 size_t l1
= strlen (n1
);
2790 size_t l2
= strlen (n2
);
2792 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2793 s
= malloc (l1
+ l2
+ 2);
2797 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2798 str_v
= caml_copy_string (s
);
2803 if (str_v
== Val_unit
) {
2804 str_v
= caml_copy_string (n2
);
2806 ret_v
= caml_alloc_small (1, utext
);
2807 Field (ret_v
, 0) = str_v
;
2821 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2823 CAMLprim
void ml_clearmark (value ptr_v
)
2826 char *s
= String_val (ptr_v
);
2829 if (trylock (__func__
)) {
2833 page
= parse_pointer (__func__
, s
);
2842 static int uninteresting (int c
)
2844 return isspace (c
) || ispunct (c
);
2847 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2849 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2853 fz_stext_line
*line
;
2854 fz_stext_block
*block
;
2855 struct pagedim
*pdim
;
2856 int mark
= Int_val (mark_v
);
2857 char *s
= String_val (ptr_v
);
2858 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2860 ret_v
= Val_bool (0);
2861 if (trylock (__func__
)) {
2865 page
= parse_pointer (__func__
, s
);
2866 pdim
= &state
.pagedims
[page
->pdimno
];
2870 if (mark
== mark_page
) {
2871 page
->fmark
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
2872 page
->lmark
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
2873 ret_v
= Val_bool (1);
2877 x
+= pdim
->bounds
.x0
;
2878 y
+= pdim
->bounds
.y0
;
2880 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2881 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2883 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2886 if (mark
== mark_block
) {
2887 page
->fmark
= block
->u
.t
.first_line
->first_char
;
2888 page
->lmark
= block
->u
.t
.last_line
->last_char
;
2889 ret_v
= Val_bool (1);
2893 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2897 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2900 if (mark
== mark_line
) {
2901 page
->fmark
= line
->first_char
;
2902 page
->lmark
= line
->last_char
;
2903 ret_v
= Val_bool (1);
2907 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2908 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
2909 fz_quad
*q
= &ch
->quad
;
2910 if (x
>= q
->ul
.x
&& x
<= q
->ur
.x
2911 && y
>= q
->ul
.y
&& y
<= q
->ll
.y
) {
2912 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
2913 if (uninteresting (ch2
->c
)) first
= NULL
;
2914 else if (!first
) first
= ch2
;
2916 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
2917 if (uninteresting (ch2
->c
)) break;
2921 page
->fmark
= first
;
2923 ret_v
= Val_bool (1);
2930 if (!Bool_val (ret_v
)) {
2940 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
2942 CAMLparam3 (ptr_v
, x_v
, y_v
);
2943 CAMLlocal2 (ret_v
, res_v
);
2946 struct pagedim
*pdim
;
2947 fz_stext_block
*block
;
2948 char *s
= String_val (ptr_v
);
2949 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2951 ret_v
= Val_int (0);
2952 if (trylock (__func__
)) {
2956 page
= parse_pointer (__func__
, s
);
2957 pdim
= &state
.pagedims
[page
->pdimno
];
2958 x
+= pdim
->bounds
.x0
;
2959 y
+= pdim
->bounds
.y0
;
2963 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2964 switch (block
->type
) {
2965 case FZ_STEXT_BLOCK_TEXT
:
2969 case FZ_STEXT_BLOCK_IMAGE
:
2977 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
2982 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2983 ret_v
= caml_alloc_small (1, 1);
2984 Store_double_field (res_v
, 0, (double) b
->x0
);
2985 Store_double_field (res_v
, 1, (double) b
->x1
);
2986 Store_double_field (res_v
, 2, (double) b
->y0
);
2987 Store_double_field (res_v
, 3, (double) b
->y1
);
2988 Field (ret_v
, 0) = res_v
;
2996 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
2998 CAMLparam2 (ptr_v
, rect_v
);
3000 struct pagedim
*pdim
;
3001 char *s
= String_val (ptr_v
);
3004 fz_stext_line
*line
;
3005 fz_stext_block
*block
;
3006 fz_stext_char
*fc
, *lc
;
3008 if (trylock (__func__
)) {
3012 page
= parse_pointer (__func__
, s
);
3015 pdim
= &state
.pagedims
[page
->pdimno
];
3016 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3017 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3018 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3019 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3032 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3033 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3034 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3035 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3036 fz_quad q
= ch
->quad
;
3037 if (x0
>= q
.ul
.x
&& x0
<= q
.ur
.x
3038 && y0
>= q
.ul
.y
&& y0
<= q
.ll
.y
) {
3041 if (x1
>= q
.ul
.x
&& x1
<= q
.ur
.x
3042 && y1
>= q
.ul
.y
&& y1
<= q
.ll
.y
) {
3048 if (x1
< x0
&& fc
== lc
) {
3065 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3071 len
= fz_runetochar (buf
, ch
->c
);
3072 ret
= fwrite (buf
, len
, 1, f
);
3074 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3075 len
, ret
, errno
, strerror (errno
));
3081 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3083 CAMLparam2 (command_v
, fds_v
);
3084 CAMLlocal2 (l_v
, tup_v
);
3086 pid_t pid
= (pid_t
) -1;
3088 value earg_v
= Nothing
;
3089 posix_spawnattr_t attr
;
3090 posix_spawn_file_actions_t fa
;
3091 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3093 argv
[2] = String_val (command_v
);
3095 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3096 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3099 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3100 msg
= "posix_spawnattr_init";
3104 #ifdef POSIX_SPAWN_USEVFORK
3105 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3106 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3111 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3114 tup_v
= Field (l_v
, 0);
3115 fd1
= Int_val (Field (tup_v
, 0));
3116 fd2
= Int_val (Field (tup_v
, 1));
3118 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3119 msg
= "posix_spawn_file_actions_addclose";
3125 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3126 msg
= "posix_spawn_file_actions_adddup2";
3133 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3134 msg
= "posix_spawn";
3139 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3140 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3144 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3145 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3146 ret1
, strerror (ret1
));
3150 unix_error (ret
, msg
, earg_v
);
3152 CAMLreturn (Val_int (pid
));
3155 CAMLprim value
ml_hassel (value ptr_v
)
3160 char *s
= String_val (ptr_v
);
3162 ret_v
= Val_bool (0);
3163 if (trylock (__func__
)) {
3167 page
= parse_pointer (__func__
, s
);
3168 ret_v
= Val_bool (page
->fmark
&& page
->lmark
);
3174 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3176 CAMLparam2 (fd_v
, ptr_v
);
3180 fz_stext_line
*line
;
3181 fz_stext_block
*block
;
3182 int fd
= Int_val (fd_v
);
3183 char *s
= String_val (ptr_v
);
3185 if (trylock (__func__
)) {
3189 page
= parse_pointer (__func__
, s
);
3191 if (!page
->fmark
|| !page
->lmark
) {
3192 printd ("emsg nothing to copy on page %d", page
->pageno
);
3196 f
= fdopen (fd
, "w");
3198 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3199 fd
, errno
, strerror (errno
));
3203 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3204 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3205 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3207 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3208 if (seen
|| ch
== page
->fmark
) {
3211 if (ch
== page
->lmark
) goto close
;
3212 } while ((ch
= ch
->next
));
3217 if (seen
) fputc ('\n', f
);
3222 int ret
= fclose (f
);
3225 if (errno
!= ECHILD
) {
3226 printd ("emsg failed to close sel pipe: %d:%s",
3227 errno
, strerror (errno
));
3237 printd ("emsg failed to close sel pipe: %d:%s",
3238 errno
, strerror (errno
));
3244 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3246 CAMLparam1 (pagedimno_v
);
3248 int pagedimno
= Int_val (pagedimno_v
);
3251 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3252 if (trylock (__func__
)) {
3253 box
= fz_empty_rect
;
3256 box
= state
.pagedims
[pagedimno
].mediabox
;
3260 Store_double_field (ret_v
, 0, (double) box
.x0
);
3261 Store_double_field (ret_v
, 1, (double) box
.x1
);
3262 Store_double_field (ret_v
, 2, (double) box
.y0
);
3263 Store_double_field (ret_v
, 3, (double) box
.y1
);
3268 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3269 value dw_v
, value cols_v
)
3271 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3277 float winw
= Int_val (winw_v
);
3278 float winh
= Int_val (winh_v
);
3279 float dw
= Int_val (dw_v
);
3280 float cols
= Int_val (cols_v
);
3281 float pw
= 1.0, ph
= 1.0;
3283 if (trylock (__func__
)) {
3287 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3288 float w
= p
->pagebox
.x1
/ cols
;
3289 float h
= p
->pagebox
.y1
;
3293 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3295 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3298 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3301 ret_v
= caml_copy_double ((double) zoom
);
3305 CAMLprim value
ml_getmaxw (value unit_v
)
3307 CAMLparam1 (unit_v
);
3313 if (trylock (__func__
)) {
3317 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3318 float w
= p
->pagebox
.x1
;
3319 maxw
= fz_max (maxw
, w
);
3324 ret_v
= caml_copy_double ((double) maxw
);
3328 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3330 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3332 int pt
= Int_val(pt_v
);
3333 int x
= Int_val (x_v
);
3334 int y
= Int_val (y_v
);
3337 w
= (double) draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3338 ret_v
= caml_copy_double (w
);
3342 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3344 CAMLparam2 (pt_v
, string_v
);
3346 int pt
= Int_val (pt_v
);
3349 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3350 ret_v
= caml_copy_double (w
);
3354 CAMLprim value
ml_getpagebox (value opaque_v
)
3356 CAMLparam1 (opaque_v
);
3361 char *s
= String_val (opaque_v
);
3362 struct page
*page
= parse_pointer (__func__
, s
);
3364 ret_v
= caml_alloc_tuple (4);
3365 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3367 fz_run_page (state
.ctx
, page
->fzpage
, dev
, pagectm (page
), NULL
);
3369 fz_close_device (state
.ctx
, dev
);
3370 fz_drop_device (state
.ctx
, dev
);
3371 bbox
= fz_round_rect (rect
);
3372 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3373 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3374 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3375 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3380 CAMLprim
void ml_setaalevel (value level_v
)
3382 CAMLparam1 (level_v
);
3384 state
.aalevel
= Int_val (level_v
);
3389 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3391 CAMLparam1 (keysym_v
);
3393 KeySym keysym
= Int_val (keysym_v
);
3395 extern long keysym2ucs (KeySym
);
3399 rune
= (Rune
) keysym2ucs (keysym
);
3400 len
= fz_runetochar (buf
, rune
);
3402 str_v
= caml_copy_string (buf
);
3406 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3408 CAMLparam1 (keysym_v
);
3410 long ucs_v
= Long_val (keysym_v
);
3414 len
= fz_runetochar (buf
, (int) ucs_v
);
3416 str_v
= caml_copy_string (buf
);
3421 enum { piunknown
, pilinux
, pimacos
, pibsd
};
3423 CAMLprim value
ml_platform (value unit_v
)
3425 CAMLparam1 (unit_v
);
3426 CAMLlocal2 (tup_v
, arr_v
);
3427 int platid
= piunknown
;
3430 #if defined __linux__
3432 #elif defined __DragonFly__ || defined __FreeBSD__
3433 || defined __OpenBSD__
|| defined __NetBSD__
3435 #elif defined __APPLE__
3438 if (uname (&buf
)) err (1, "uname");
3440 tup_v
= caml_alloc_tuple (2);
3442 char const *sar
[] = {
3449 arr_v
= caml_copy_string_array (sar
);
3451 Field (tup_v
, 0) = Val_int (platid
);
3452 Field (tup_v
, 1) = arr_v
;
3456 CAMLprim
void ml_cloexec (value fd_v
)
3459 int fd
= Int_val (fd_v
);
3461 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3462 uerror ("fcntl", Nothing
);
3467 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3469 CAMLparam2 (w_v
, h_v
);
3472 int w
= Int_val (w_v
);
3473 int h
= Int_val (h_v
);
3474 int cs
= Int_val (cs_v
);
3476 if (state
.bo_usable
) {
3477 pbo
= calloc (sizeof (*pbo
), 1);
3479 err (1, "calloc pbo");
3491 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3494 state
.glGenBuffersARB (1, &pbo
->id
);
3495 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3496 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3497 NULL
, GL_STREAM_DRAW
);
3498 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3500 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3502 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3503 state
.glDeleteBuffersARB (1, &pbo
->id
);
3505 ret_v
= caml_copy_string ("0");
3511 res
= snprintf (NULL
, 0, "%" PRIxPTR
, (uintptr_t) pbo
);
3513 err (1, "snprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3517 err (1, "malloc %d bytes failed", res
+1);
3519 res
= sprintf (s
, "%" PRIxPTR
, (uintptr_t) pbo
);
3521 err (1, "sprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3523 ret_v
= caml_copy_string (s
);
3528 ret_v
= caml_copy_string ("0");
3533 CAMLprim
void ml_freepbo (value s_v
)
3536 char *s
= String_val (s_v
);
3537 struct tile
*tile
= parse_pointer (__func__
, s
);
3540 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3542 tile
->pbo
->ptr
= NULL
;
3543 tile
->pbo
->size
= -1;
3548 CAMLprim
void ml_unmappbo (value s_v
)
3551 char *s
= String_val (s_v
);
3552 struct tile
*tile
= parse_pointer (__func__
, s
);
3555 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3556 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3557 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3559 tile
->pbo
->ptr
= NULL
;
3560 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3565 static void setuppbo (void)
3567 extern void (*wsigladdr (const char *name
)) (void);
3568 #pragma GCC diagnostic push
3570 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3572 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3573 state
.bo_usable
= GPA (glBindBufferARB
)
3574 && GPA (glUnmapBufferARB
)
3575 && GPA (glMapBufferARB
)
3576 && GPA (glBufferDataARB
)
3577 && GPA (glGenBuffersARB
)
3578 && GPA (glDeleteBuffersARB
);
3580 #pragma GCC diagnostic pop
3583 CAMLprim value
ml_bo_usable (void)
3585 return Val_bool (state
.bo_usable
);
3588 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3590 CAMLparam3 (ptr_v
, x_v
, y_v
);
3591 CAMLlocal2 (ret_v
, tup_v
);
3593 char *s
= String_val (ptr_v
);
3594 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3595 struct pagedim
*pdim
;
3598 page
= parse_pointer (__func__
, s
);
3599 pdim
= &state
.pagedims
[page
->pdimno
];
3601 ret_v
= Val_int (0);
3602 if (trylock (__func__
)) {
3606 p
.x
= x
+ pdim
->bounds
.x0
;
3607 p
.y
= y
+ pdim
->bounds
.y0
;
3609 p
= fz_transform_point (p
, fz_invert_matrix (fz_concat (pdim
->tctm
,
3612 tup_v
= caml_alloc_tuple (2);
3613 ret_v
= caml_alloc_small (1, 1);
3614 Field (tup_v
, 0) = Val_int (p
.x
);
3615 Field (tup_v
, 1) = Val_int (p
.y
);
3616 Field (ret_v
, 0) = tup_v
;
3623 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
3624 value x_v
, value y_v
)
3626 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
3629 char *s
= String_val (ptr_v
);
3630 int pageno
= Int_val (pageno_v
);
3631 int pdimno
= Int_val (pdimno_v
);
3632 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
3633 struct pagedim
*pdim
;
3637 ret_v
= Val_int (0);
3641 page
= loadpage (pageno
, pdimno
);
3644 page
= parse_pointer (__func__
, s
);
3646 pdim
= &state
.pagedims
[pdimno
];
3648 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3649 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3650 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3655 p
.x
= x
+ pdim
->bounds
.x0
;
3656 p
.y
= y
+ pdim
->bounds
.y0
;
3658 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
3659 p
= fz_transform_point (p
, ctm
);
3661 ret_v
= caml_alloc_tuple (2);
3662 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
3663 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
3672 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
3675 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
3676 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3682 char *s
= String_val (ptr_v
);
3684 page
= parse_pointer (__func__
, s
);
3685 annot
= pdf_create_annot (state
.ctx
,
3686 pdf_page_from_fz_page (state
.ctx
,
3689 p
.x
= Int_val (x_v
);
3690 p
.y
= Int_val (y_v
);
3691 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
3692 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
3698 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
3700 CAMLparam2 (ptr_v
, n_v
);
3701 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3705 char *s
= String_val (ptr_v
);
3706 struct slink
*slink
;
3708 page
= parse_pointer (__func__
, s
);
3709 slink
= &page
->slinks
[Int_val (n_v
)];
3710 pdf_delete_annot (state
.ctx
,
3711 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
3712 (pdf_annot
*) slink
->u
.annot
);
3718 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
3720 CAMLparam3 (ptr_v
, n_v
, str_v
);
3721 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3725 char *s
= String_val (ptr_v
);
3726 struct slink
*slink
;
3728 page
= parse_pointer (__func__
, s
);
3729 slink
= &page
->slinks
[Int_val (n_v
)];
3730 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
3731 String_val (str_v
));
3737 CAMLprim value
ml_hasunsavedchanges (void)
3739 return Val_bool (state
.dirty
);
3742 CAMLprim
void ml_savedoc (value path_v
)
3744 CAMLparam1 (path_v
);
3745 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3748 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
3753 static void makestippletex (void)
3755 const char pixels
[] = "\xff\xff\0\0";
3756 glGenTextures (1, &state
.stid
);
3757 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3758 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3759 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3772 CAMLprim value
ml_fz_version (void)
3774 return caml_copy_string (FZ_VERSION
);
3777 CAMLprim value
ml_llpp_version (void)
3779 extern char llpp_version
[];
3780 return caml_copy_string (llpp_version
);
3783 CAMLprim
void ml_init (value csock_v
, value params_v
)
3785 CAMLparam2 (csock_v
, params_v
);
3786 CAMLlocal2 (trim_v
, fuzz_v
);
3793 state
.csock
= Int_val (csock_v
);
3794 state
.rotate
= Int_val (Field (params_v
, 0));
3795 state
.fitmodel
= Int_val (Field (params_v
, 1));
3796 trim_v
= Field (params_v
, 2);
3797 texcount
= Int_val (Field (params_v
, 3));
3798 state
.sliceheight
= Int_val (Field (params_v
, 4));
3799 mustoresize
= Int_val (Field (params_v
, 5));
3800 colorspace
= Int_val (Field (params_v
, 6));
3801 fontpath
= String_val (Field (params_v
, 7));
3806 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3807 if (setlocale (LC_CTYPE
, "")) {
3808 const char *cset
= nl_langinfo (CODESET
);
3809 state
.utf8cs
= !strcmp (cset
, "UTF-8");
3812 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
3816 if (caml_string_length (Field (params_v
, 8)) > 0) {
3817 state
.trimcachepath
= ystrdup (String_val (Field (params_v
, 8)));
3819 if (!state
.trimcachepath
) {
3820 printd ("emsg failed to strdup trimcachepath: %d:%s",
3821 errno
, strerror (errno
));
3825 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3826 fz_register_document_handlers (state
.ctx
);
3828 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3829 fuzz_v
= Field (trim_v
, 1);
3830 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3831 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3832 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3833 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3835 set_tex_params (colorspace
);
3838 state
.face
= load_font (fontpath
);
3842 const unsigned char *data
;
3844 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
3845 state
.face
= load_builtin_font (data
, len
);
3847 if (!state
.face
) _exit (1);
3849 realloctexts (texcount
);
3853 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3855 errx (1, "pthread_create: %s", strerror (ret
));
3862 static void UNUSED_ATTR NO_OPTIMIZE_ATTR
refmacs (void) {}