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
)
511 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
514 if (!pdim
->tctmready
) {
515 fz_rect realbox
, mediabox
;
516 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
518 fz_rotate (&rm
, -pdim
->rotate
);
519 fz_scale (&sm
, 1, -1);
520 fz_concat (&ctm
, &rm
, &sm
);
521 realbox
= pdim
->mediabox
;
522 fz_transform_rect (&realbox
, &ctm
);
523 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
524 fz_concat (&ctm1
, &ctm
, &tm
);
525 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
526 fz_invert_matrix (&im
, &page_ctm
);
527 fz_concat (&ctm
, &im
, &ctm1
);
533 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
536 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
538 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
539 if (pdf_specifics (state
.ctx
, state
.doc
)) {
540 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
541 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
544 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
545 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
550 static fz_matrix
pagectm (struct page
*page
)
552 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
555 static void *loadpage (int pageno
, int pindex
)
560 page
= calloc (sizeof (struct page
), 1);
562 err (1, "calloc page %d", pageno
);
565 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
566 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
568 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
569 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
572 fz_catch (state
.ctx
) {
575 fz_close_device (state
.ctx
, dev
);
576 fz_drop_device (state
.ctx
, dev
);
578 page
->pdimno
= pindex
;
579 page
->pageno
= pageno
;
580 page
->sgen
= state
.gen
;
581 page
->agen
= state
.gen
;
582 page
->tgen
= state
.gen
;
586 static struct tile
*alloctile (int h
)
592 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
593 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
594 tile
= calloc (tilesize
, 1);
596 err (1, "cannot allocate tile (%zu bytes)", tilesize
);
598 for (int i
= 0; i
< slicecount
; ++i
) {
599 int sh
= fz_mini (h
, state
.sliceheight
);
600 tile
->slices
[i
].h
= sh
;
601 tile
->slices
[i
].texindex
= -1;
604 tile
->slicecount
= slicecount
;
605 tile
->sliceheight
= state
.sliceheight
;
609 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
617 struct pagedim
*pdim
;
619 tile
= alloctile (h
);
620 pdim
= &state
.pagedims
[page
->pdimno
];
625 bbox
.x1
= bbox
.x0
+ w
;
626 bbox
.y1
= bbox
.y0
+ h
;
629 if (state
.pig
->w
== w
631 && state
.pig
->colorspace
== state
.colorspace
) {
632 tile
->pixmap
= state
.pig
;
633 tile
->pixmap
->x
= bbox
.x0
;
634 tile
->pixmap
->y
= bbox
.y0
;
637 fz_drop_pixmap (state
.ctx
, state
.pig
);
644 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
645 &bbox
, NULL
, 1, pbo
->ptr
);
650 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
,
657 fz_clear_pixmap_with_value (state
.ctx
, tile
->pixmap
, 0xff);
659 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
660 ctm
= pagectm (page
);
661 fz_rect_from_irect (&rect
, &bbox
);
662 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
663 fz_close_device (state
.ctx
, dev
);
664 fz_drop_device (state
.ctx
, dev
);
669 #ifdef CACHE_PAGEREFS
670 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
671 thanks to Robin Watts */
673 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
675 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
679 if (state
.pdflut
.idx
== state
.pagecount
) return;
681 kids
= pdf_dict_gets (ctx
, node
, "Kids");
682 len
= pdf_array_len (ctx
, kids
);
685 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
687 if (pdf_mark_obj (ctx
, node
))
688 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
689 for (int i
= 0; i
< len
; i
++) {
690 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
691 const char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
693 ? !strcmp (type
, "Pages")
694 : pdf_dict_gets (ctx
, kid
, "Kids")
695 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
696 pdf_collect_pages (doc
, kid
);
700 ? strcmp (type
, "Page") != 0
701 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
702 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
703 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
706 pdf_unmark_obj (ctx
, node
);
710 pdf_load_page_objs (pdf_document
*doc
)
712 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
713 pdf_trailer (state
.ctx
, doc
), "Root");
714 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
717 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
719 state
.pdflut
.idx
= 0;
720 pdf_collect_pages (doc
, node
);
724 static void initpdims (void)
728 fz_rect rootmediabox
= fz_empty_rect
;
729 int pageno
, trim
, show
;
730 int trimw
= 0, cxcount
;
731 fz_context
*ctx
= state
.ctx
;
732 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
739 if (state
.trimmargins
&& state
.trimcachepath
) {
740 trimf
= fopen (state
.trimcachepath
, "rb");
742 trimf
= fopen (state
.trimcachepath
, "wb");
747 if (state
.trimmargins
|| pdf
)
748 cxcount
= state
.pagecount
;
750 cxcount
= fz_mini (state
.pagecount
, 1);
754 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
755 "Root/Pages/MediaBox");
756 pdf_to_rect (ctx
, obj
, &rootmediabox
);
759 #ifdef CACHE_PAGEREFS
760 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
761 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
762 if (!state
.pdflut
.objs
) {
763 err (1, "malloc pageobjs %zu %d %zu failed",
764 sizeof (*state
.pdflut
.objs
), cxcount
,
765 sizeof (*state
.pdflut
.objs
) * cxcount
);
767 state
.pdflut
.count
= cxcount
;
768 pdf_load_page_objs (pdf
);
769 state
.pdflut
.pdf
= pdf
;
773 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
776 fz_rect mediabox
= fz_empty_rect
;
780 pdf_obj
*pageref
, *pageobj
;
782 #ifdef CACHE_PAGEREFS
783 pageref
= state
.pdflut
.objs
[pageno
];
785 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
787 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
788 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
790 if (state
.trimmargins
) {
795 page
= pdf_load_page (ctx
, pdf
, pageno
);
796 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
797 trim
= state
.trimanew
|| !obj
;
801 fz_matrix ctm
, page_ctm
;
803 dev
= fz_new_bbox_device (ctx
, &rect
);
804 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
805 fz_invert_matrix (&ctm
, &page_ctm
);
806 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
807 fz_close_device (ctx
, dev
);
808 fz_drop_device (ctx
, dev
);
810 rect
.x0
+= state
.trimfuzz
.x0
;
811 rect
.x1
+= state
.trimfuzz
.x1
;
812 rect
.y0
+= state
.trimfuzz
.y0
;
813 rect
.y1
+= state
.trimfuzz
.y1
;
814 fz_transform_rect (&rect
, &ctm
);
815 fz_intersect_rect (&rect
, &mediabox
);
817 if (!fz_is_empty_rect (&rect
)) {
821 obj
= pdf_new_array (ctx
, pdf
, 4);
822 pdf_array_push (ctx
, obj
,
823 pdf_new_real (ctx
, mediabox
.x0
));
824 pdf_array_push (ctx
, obj
,
825 pdf_new_real (ctx
, mediabox
.y0
));
826 pdf_array_push (ctx
, obj
,
827 pdf_new_real (ctx
, mediabox
.x1
));
828 pdf_array_push (ctx
, obj
,
829 pdf_new_real (ctx
, mediabox
.y1
));
830 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
833 mediabox
.x0
= pdf_to_real (ctx
,
834 pdf_array_get (ctx
, obj
, 0));
835 mediabox
.y0
= pdf_to_real (ctx
,
836 pdf_array_get (ctx
, obj
, 1));
837 mediabox
.x1
= pdf_to_real (ctx
,
838 pdf_array_get (ctx
, obj
, 2));
839 mediabox
.y1
= pdf_to_real (ctx
,
840 pdf_array_get (ctx
, obj
, 3));
843 fz_drop_page (ctx
, &page
->super
);
844 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
846 printd ("progress %f Trimming %d",
847 (double) (pageno
+ 1) / state
.pagecount
,
852 printd ("emsg failed to load page %d", pageno
);
860 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
862 if (fz_is_empty_rect (&mediabox
)) {
871 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
873 if (!fz_is_empty_rect (&cropbox
)) {
878 fz_intersect_rect (&mediabox
, &cropbox
);
883 if (fz_is_empty_rect (&rootmediabox
)) {
884 printd ("emsg cannot find page size for page %d",
888 mediabox
= rootmediabox
;
895 if (state
.trimmargins
&& trimw
) {
899 page
= fz_load_page (ctx
, state
.doc
, pageno
);
900 fz_bound_page (ctx
, page
, &mediabox
);
901 if (state
.trimmargins
) {
905 dev
= fz_new_bbox_device (ctx
, &rect
);
906 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
907 fz_close_device (ctx
, dev
);
908 fz_drop_device (ctx
, dev
);
910 rect
.x0
+= state
.trimfuzz
.x0
;
911 rect
.x1
+= state
.trimfuzz
.x1
;
912 rect
.y0
+= state
.trimfuzz
.y0
;
913 rect
.y1
+= state
.trimfuzz
.y1
;
914 fz_intersect_rect (&rect
, &mediabox
);
916 if (!fz_is_empty_rect (&rect
)) {
920 fz_drop_page (ctx
, page
);
925 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
927 err (1, "fwrite trim mediabox");
933 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
935 err (1, "fread trim mediabox %d", pageno
);
941 page
= fz_load_page (ctx
, state
.doc
, pageno
);
942 fz_bound_page (ctx
, page
, &mediabox
);
943 fz_drop_page (ctx
, page
);
945 show
= !state
.trimmargins
&& pageno
% 20 == 0;
947 printd ("progress %f Gathering dimensions %d",
948 (double) (pageno
) / state
.pagecount
,
953 printd ("emsg failed to load page %d", pageno
);
959 if (state
.pagedimcount
== 0
960 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
961 , p
->rotate
!= rotate
)
962 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
965 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
966 state
.pagedims
= realloc (state
.pagedims
, size
);
967 if (!state
.pagedims
) {
968 err (1, "realloc pagedims to %zu (%d elems)",
969 size
, state
.pagedimcount
+ 1);
972 p
= &state
.pagedims
[state
.pagedimcount
++];
974 p
->mediabox
= mediabox
;
979 printd ("progress 1 %s %d pages in %f seconds",
980 state
.trimmargins
? "Trimmed" : "Processed",
981 state
.pagecount
, end
- start
);
984 if (fclose (trimf
)) {
990 static void layout (void)
995 struct pagedim
*p
= NULL
;
996 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
998 if (state
.pagedimcount
== 0) return;
1000 switch (state
.fitmodel
) {
1001 case FitProportional
:
1002 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1005 p
= &state
.pagedims
[pindex
];
1006 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1008 fz_transform_rect (&box
, &rm
);
1010 x0
= fz_min (box
.x0
, box
.x1
);
1011 x1
= fz_max (box
.x0
, box
.x1
);
1014 maxw
= fz_max (w
, maxw
);
1015 zoom
= state
.w
/ maxw
;
1027 ARSERT (0 && state
.fitmodel
);
1030 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1034 p
= &state
.pagedims
[pindex
];
1035 fz_rotate (&ctm
, state
.rotate
);
1036 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1038 fz_transform_rect (&box
, &rm
);
1039 w
= box
.x1
- box
.x0
;
1040 switch (state
.fitmodel
) {
1041 case FitProportional
:
1042 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
1048 h
= box
.y1
- box
.y0
;
1050 zoom
= fz_min (zw
, zh
);
1051 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
1060 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1061 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1063 fz_rotate (&rm
, p
->rotate
);
1064 p
->pagebox
= p
->mediabox
;
1065 fz_transform_rect (&p
->pagebox
, &rm
);
1066 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1067 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1071 fz_transform_rect (&rect
, &ctm
);
1072 fz_round_rect (&p
->bounds
, &rect
);
1075 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1076 fz_scale (&sm
, zoom
, -zoom
);
1077 fz_concat (&ctm
, &tm
, &sm
);
1083 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1084 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1085 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1086 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1087 int boundw
= x1
- x0
;
1088 int boundh
= y1
- y0
;
1090 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1091 } while (p
-- != state
.pagedims
);
1094 struct pagedim
*pdimofpageno (int pageno
)
1096 struct pagedim
*pdim
= state
.pagedims
;
1098 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
1099 if (state
.pagedims
[i
].pageno
> pageno
)
1101 pdim
= &state
.pagedims
[i
];
1106 static void recurse_outline (fz_outline
*outline
, int level
)
1109 if (outline
->page
>= 0) {
1110 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
1111 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
1112 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1113 fz_transform_point (&p
, &pdim
->ctm
);
1114 printd ("o %d %d %d %d %s",
1115 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
1118 printd ("on %d %s", level
, outline
->title
);
1120 if (outline
->down
) {
1121 recurse_outline (outline
->down
, level
+ 1);
1123 outline
= outline
->next
;
1127 static void process_outline (void)
1129 fz_outline
*outline
;
1131 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1133 state
.needoutline
= 0;
1134 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1136 recurse_outline (outline
, 0);
1137 fz_drop_outline (state
.ctx
, outline
);
1141 static char *strofline (fz_stext_line
*line
)
1146 size_t size
= 0, cap
= 80;
1148 p
= malloc (cap
+ 1);
1149 if (!p
) return NULL
;
1151 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1152 int n
= fz_runetochar (utf8
, ch
->c
);
1153 if (size
+ n
> cap
) {
1155 p
= realloc (p
, cap
+ 1);
1156 if (!p
) return NULL
;
1159 memcpy (p
+ size
, utf8
, n
);
1166 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1167 int stop
, int pageno
, double start
)
1173 p
= strofline (line
);
1176 ret
= regexec (re
, p
, 1, &rm
, 0);
1179 if (ret
!= REG_NOMATCH
) {
1182 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1183 printd ("msg regexec error `%.*s'",
1184 (int) size
, errbuf
);
1190 fz_point p1
, p2
, p3
, p4
;
1191 fz_rect s
= {0,0,0,0}, e
;
1195 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1196 o
+= fz_runelen (ch
->c
);
1202 for (;ch
; ch
= ch
->next
) {
1203 o
+= fz_runelen (ch
->c
);
1204 if (o
> rm
.rm_eo
) break;
1218 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1225 printd ("progress 1 found at %d `%.*s' in %f sec",
1226 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1230 printd ("match %d %d %f %f %f %f %f %f %f %f",
1242 /* wishful thinking function */
1243 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1246 fz_stext_page
*text
;
1247 struct pagedim
*pdim
;
1248 int stop
= 0, niters
= 0;
1251 fz_stext_block
*block
;
1254 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1255 if (niters
++ == 5) {
1258 printd ("progress 1 attention requested aborting search at %d",
1263 printd ("progress %f searching in page %d",
1264 (double) (pageno
+ 1) / state
.pagecount
,
1268 pdim
= pdimofpageno (pageno
);
1269 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1270 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1272 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1274 fz_matrix ctm
= pagectm1 (page
, pdim
);
1275 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1278 fz_close_device (state
.ctx
, tdev
);
1279 fz_drop_device (state
.ctx
, tdev
);
1282 for (block
= text
->first_block
; block
; block
= block
->next
) {
1283 fz_stext_line
*line
;
1285 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1286 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1287 if (line
->bbox
.y0
< y
+ 1) continue;
1289 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1291 case 1: stop
= 1; break;
1292 case -1: stop
= 1; goto endloop
;
1298 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1299 fz_stext_line
*line
;
1301 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1302 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1303 if (line
->bbox
.y0
< y
+ 1) continue;
1305 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1307 case 1: stop
= 1; break;
1308 case -1: stop
= 1; goto endloop
;
1323 fz_drop_stext_page (state
.ctx
, text
);
1324 fz_drop_page (state
.ctx
, page
);
1328 printd ("progress 1 no matches %f sec", end
- start
);
1330 printd ("clearrects");
1333 static void set_tex_params (int colorspace
)
1335 switch (colorspace
) {
1337 state
.texiform
= GL_RGBA8
;
1338 state
.texform
= GL_RGBA
;
1339 state
.texty
= GL_UNSIGNED_BYTE
;
1340 state
.colorspace
= fz_device_rgb (state
.ctx
);
1343 state
.texiform
= GL_LUMINANCE_ALPHA
;
1344 state
.texform
= GL_LUMINANCE_ALPHA
;
1345 state
.texty
= GL_UNSIGNED_BYTE
;
1346 state
.colorspace
= fz_device_gray (state
.ctx
);
1349 errx (1, "invalid colorspce %d", colorspace
);
1353 static void realloctexts (int texcount
)
1357 if (texcount
== state
.texcount
) return;
1359 if (texcount
< state
.texcount
) {
1360 glDeleteTextures (state
.texcount
- texcount
,
1361 state
.texids
+ texcount
);
1364 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1365 state
.texids
= realloc (state
.texids
, size
);
1366 if (!state
.texids
) {
1367 err (1, "realloc texs %zu", size
);
1370 state
.texowners
= (void *) (state
.texids
+ texcount
);
1371 if (texcount
> state
.texcount
) {
1372 glGenTextures (texcount
- state
.texcount
,
1373 state
.texids
+ state
.texcount
);
1374 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1375 state
.texowners
[i
].w
= -1;
1376 state
.texowners
[i
].slice
= NULL
;
1379 state
.texcount
= texcount
;
1383 static char *mbtoutf8 (char *s
)
1393 len
= mbstowcs (NULL
, s
, strlen (s
));
1394 if (len
== 0 || len
== (size_t) -1) {
1396 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1400 tmp
= calloc (len
, sizeof (wchar_t));
1402 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1403 len
, sizeof (wchar_t), errno
, strerror (errno
));
1407 ret
= mbstowcs (tmp
, s
, len
);
1408 if (ret
== (size_t) -1) {
1409 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1410 len
, errno
, strerror (errno
));
1416 for (i
= 0; i
< ret
; ++i
) {
1417 len
+= fz_runelen (tmp
[i
]);
1420 p
= r
= malloc (len
+ 1);
1422 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1427 for (i
= 0; i
< ret
; ++i
) {
1428 p
+= fz_runetochar (p
, tmp
[i
]);
1435 CAMLprim value
ml_mbtoutf8 (value s_v
)
1441 s
= String_val (s_v
);
1447 ret_v
= caml_copy_string (r
);
1453 static void * mainloop (void UNUSED_ATTR
*unused
)
1456 int len
, ret
, oldlen
= 0;
1461 len
= readlen (state
.csock
);
1463 errx (1, "readlen returned 0");
1466 if (oldlen
< len
+ 1) {
1467 p
= realloc (p
, len
+ 1);
1469 err (1, "realloc %d failed", len
+ 1);
1473 readdata (state
.csock
, p
, len
);
1476 if (!strncmp ("open", p
, 4)) {
1477 int off
, usedoccss
, ok
= 0, layouth
;
1484 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1486 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1489 filename
= p
+ 5 + off
;
1490 filenamelen
= strlen (filename
);
1491 password
= filename
+ filenamelen
+ 1;
1493 if (password
[strlen (password
) + 1]) {
1494 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1498 fz_set_use_document_css (state
.ctx
, usedoccss
);
1499 fz_try (state
.ctx
) {
1500 ok
= openxref (filename
, password
, layouth
);
1502 fz_catch (state
.ctx
) {
1503 utf8filename
= mbtoutf8 (filename
);
1504 printd ("msg Could not open %s", utf8filename
);
1505 if (utf8filename
!= filename
) {
1506 free (utf8filename
);
1516 utf8filename
= mbtoutf8 (filename
);
1517 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1518 if (utf8filename
!= filename
) {
1519 free (utf8filename
);
1521 state
.needoutline
= 1;
1524 else if (!strncmp ("cs", p
, 2)) {
1527 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1529 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1532 set_tex_params (colorspace
);
1533 for (i
= 0; i
< state
.texcount
; ++i
) {
1534 state
.texowners
[i
].w
= -1;
1535 state
.texowners
[i
].slice
= NULL
;
1539 else if (!strncmp ("freepage", p
, 8)) {
1542 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1544 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1548 unlock ("freepage");
1550 else if (!strncmp ("freetile", p
, 8)) {
1553 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1555 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1559 unlock ("freetile");
1561 else if (!strncmp ("search", p
, 6)) {
1562 int icase
, pageno
, y
, len2
, forward
;
1566 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1567 &icase
, &pageno
, &y
, &forward
, &len2
);
1569 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1572 pattern
= p
+ 6 + len2
;
1573 ret
= regcomp (&re
, pattern
,
1574 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1579 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1580 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1583 search (&re
, pageno
, y
, forward
);
1587 else if (!strncmp ("geometry", p
, 8)) {
1591 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1593 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1600 for (int i
= 0; i
< state
.texcount
; ++i
) {
1601 state
.texowners
[i
].slice
= NULL
;
1604 state
.fitmodel
= fitmodel
;
1609 unlock ("geometry");
1610 printd ("continue %d", state
.pagecount
);
1612 else if (!strncmp ("reqlayout", p
, 9)) {
1619 ret
= sscanf (p
+ 9, " %d %d %d %n",
1620 &rotate
, &fitmodel
, &h
, &off
);
1622 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1625 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1626 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1629 state
.rotate
= rotate
;
1630 state
.fitmodel
= fitmodel
;
1635 nameddest
= p
+ 9 + off
;
1636 if (pdf
&& nameddest
&& *nameddest
) {
1638 struct pagedim
*pdim
;
1639 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1641 pdim
= pdimofpageno (pageno
);
1642 fz_transform_point (&xy
, &pdim
->ctm
);
1643 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1647 unlock ("reqlayout");
1648 printd ("continue %d", state
.pagecount
);
1650 else if (!strncmp ("page", p
, 4)) {
1655 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1657 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1662 page
= loadpage (pageno
, pindex
);
1666 printd ("page %" PRIxPTR
" %f", (uintptr_t) page
, b
- a
);
1668 else if (!strncmp ("tile", p
, 4)) {
1675 ret
= sscanf (p
+ 4, " %" SCNxPTR
" %d %d %d %d %" SCNxPTR
,
1676 (uintptr_t *) &page
, &x
, &y
, &w
, &h
,
1677 (uintptr_t *) &data
);
1679 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1684 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1688 printd ("tile %d %d %" PRIxPTR
" %u %f",
1689 x
, y
, (uintptr_t) (tile
),
1690 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1693 else if (!strncmp ("trimset", p
, 7)) {
1697 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1698 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1700 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1703 state
.trimmargins
= trimmargins
;
1704 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1706 state
.trimfuzz
= fuzz
;
1710 else if (!strncmp ("settrim", p
, 7)) {
1714 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1715 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1717 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1721 state
.trimmargins
= trimmargins
;
1722 state
.needoutline
= 1;
1723 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1725 state
.trimfuzz
= fuzz
;
1727 state
.pagedimcount
= 0;
1728 free (state
.pagedims
);
1729 state
.pagedims
= NULL
;
1734 printd ("continue %d", state
.pagecount
);
1736 else if (!strncmp ("sliceh", p
, 6)) {
1739 ret
= sscanf (p
+ 6, " %d", &h
);
1741 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1743 if (h
!= state
.sliceheight
) {
1744 state
.sliceheight
= h
;
1745 for (int i
= 0; i
< state
.texcount
; ++i
) {
1746 state
.texowners
[i
].w
= -1;
1747 state
.texowners
[i
].h
= -1;
1748 state
.texowners
[i
].slice
= NULL
;
1752 else if (!strncmp ("interrupt", p
, 9)) {
1753 printd ("vmsg interrupted");
1756 errx (1, "unknown command %.*s", len
, p
);
1762 CAMLprim value
ml_isexternallink (value uri_v
)
1765 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1766 CAMLreturn (Val_bool (ext
));
1769 CAMLprim value
ml_uritolocation (value uri_v
)
1775 struct pagedim
*pdim
;
1777 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1779 pdim
= pdimofpageno (pageno
);
1780 fz_transform_point (&xy
, &pdim
->ctm
);
1781 ret_v
= caml_alloc_tuple (3);
1782 Field (ret_v
, 0) = Val_int (pageno
);
1783 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1784 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1788 CAMLprim value
ml_realloctexts (value texcount_v
)
1790 CAMLparam1 (texcount_v
);
1793 if (trylock (__func__
)) {
1797 realloctexts (Int_val (texcount_v
));
1802 CAMLreturn (Val_bool (ok
));
1805 static void recti (int x0
, int y0
, int x1
, int y1
)
1807 GLfloat
*v
= state
.vertices
;
1809 glVertexPointer (2, GL_FLOAT
, 0, v
);
1810 v
[0] = x0
; v
[1] = y0
;
1811 v
[2] = x1
; v
[3] = y0
;
1812 v
[4] = x0
; v
[5] = y1
;
1813 v
[6] = x1
; v
[7] = y1
;
1814 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1817 static void showsel (struct page
*page
, int ox
, int oy
)
1821 fz_stext_block
*block
;
1823 unsigned char selcolor
[] = {15,15,15,140};
1825 if (!page
->fmark
|| !page
->lmark
) return;
1827 glEnable (GL_BLEND
);
1828 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1829 glColor4ubv (selcolor
);
1831 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1832 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1834 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1835 fz_stext_line
*line
;
1837 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1838 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1841 rect
= fz_empty_rect
;
1842 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1843 if (ch
== page
->fmark
) seen
= 1;
1844 if (seen
) fz_union_rect (&rect
, &ch
->bbox
);
1845 if (ch
== page
->lmark
) {
1846 fz_round_rect (&bbox
, &rect
);
1847 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1848 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1852 fz_round_rect (&bbox
, &rect
);
1853 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1854 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1858 glDisable (GL_BLEND
);
1861 #pragma GCC diagnostic push
1862 #pragma GCC diagnostic ignored "-Wconversion"
1864 #pragma GCC diagnostic pop
1866 static void stipplerect (fz_matrix
*m
,
1874 fz_transform_point (p1
, m
);
1875 fz_transform_point (p2
, m
);
1876 fz_transform_point (p3
, m
);
1877 fz_transform_point (p4
, m
);
1883 t
= hypotf (w
, h
) * .25f
;
1887 s
= hypotf (w
, h
) * .25f
;
1889 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
1890 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
1892 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
1893 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
1895 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
1896 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
1898 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
1899 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
1901 glDrawArrays (GL_LINES
, 0, 8);
1904 static void solidrect (fz_matrix
*m
,
1911 fz_transform_point (p1
, m
);
1912 fz_transform_point (p2
, m
);
1913 fz_transform_point (p3
, m
);
1914 fz_transform_point (p4
, m
);
1915 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
1916 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
1918 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
1919 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
1920 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
1923 static void ensurelinks (struct page
*page
)
1926 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
1929 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1931 fz_matrix ctm
, tm
, pm
;
1933 GLfloat
*texcoords
= state
.texcoords
;
1934 GLfloat
*vertices
= state
.vertices
;
1938 glEnable (GL_TEXTURE_1D
);
1939 glEnable (GL_BLEND
);
1940 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1941 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
1943 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1944 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1945 fz_translate (&tm
, xoff
, yoff
);
1946 pm
= pagectm (page
);
1947 fz_concat (&ctm
, &pm
, &tm
);
1949 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
1950 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
1952 for (link
= page
->links
; link
; link
= link
->next
) {
1953 fz_point p1
, p2
, p3
, p4
;
1955 p1
.x
= link
->rect
.x0
;
1956 p1
.y
= link
->rect
.y0
;
1958 p2
.x
= link
->rect
.x1
;
1959 p2
.y
= link
->rect
.y0
;
1961 p3
.x
= link
->rect
.x1
;
1962 p3
.y
= link
->rect
.y1
;
1964 p4
.x
= link
->rect
.x0
;
1965 p4
.y
= link
->rect
.y1
;
1967 /* TODO: different colours for different schemes */
1968 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
1969 else glColor3ub (255, 0, 0);
1971 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
1974 for (int i
= 0; i
< page
->annotcount
; ++i
) {
1975 fz_point p1
, p2
, p3
, p4
;
1976 struct annot
*annot
= &page
->annots
[i
];
1978 p1
.x
= annot
->bbox
.x0
;
1979 p1
.y
= annot
->bbox
.y0
;
1981 p2
.x
= annot
->bbox
.x1
;
1982 p2
.y
= annot
->bbox
.y0
;
1984 p3
.x
= annot
->bbox
.x1
;
1985 p3
.y
= annot
->bbox
.y1
;
1987 p4
.x
= annot
->bbox
.x0
;
1988 p4
.y
= annot
->bbox
.y1
;
1990 glColor3ub (0, 0, 128);
1991 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
1994 glDisable (GL_BLEND
);
1995 glDisable (GL_TEXTURE_1D
);
1998 static int compareslinks (const void *l
, const void *r
)
2000 struct slink
const *ls
= l
;
2001 struct slink
const *rs
= r
;
2002 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2003 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2005 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2008 static void droptext (struct page
*page
)
2011 fz_drop_stext_page (state
.ctx
, page
->text
);
2018 static void dropannots (struct page
*page
)
2021 free (page
->annots
);
2022 page
->annots
= NULL
;
2023 page
->annotcount
= 0;
2027 static void ensureannots (struct page
*page
)
2030 size_t annotsize
= sizeof (*page
->annots
);
2033 if (state
.gen
!= page
->agen
) {
2035 page
->agen
= state
.gen
;
2037 if (page
->annots
) return;
2039 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2041 annot
= fz_next_annot (state
.ctx
, annot
)) {
2046 page
->annotcount
= count
;
2047 page
->annots
= calloc (count
, annotsize
);
2048 if (!page
->annots
) {
2049 err (1, "calloc annots %d", count
);
2052 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2054 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2057 fz_bound_annot (state
.ctx
, annot
, &rect
);
2058 page
->annots
[i
].annot
= annot
;
2059 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2064 static void dropslinks (struct page
*page
)
2067 free (page
->slinks
);
2068 page
->slinks
= NULL
;
2069 page
->slinkcount
= 0;
2072 fz_drop_link (state
.ctx
, page
->links
);
2077 static void ensureslinks (struct page
*page
)
2081 size_t slinksize
= sizeof (*page
->slinks
);
2084 ensureannots (page
);
2085 if (state
.gen
!= page
->sgen
) {
2087 page
->sgen
= state
.gen
;
2089 if (page
->slinks
) return;
2092 ctm
= pagectm (page
);
2094 count
= page
->annotcount
;
2095 for (link
= page
->links
; link
; link
= link
->next
) {
2101 page
->slinkcount
= count
;
2102 page
->slinks
= calloc (count
, slinksize
);
2103 if (!page
->slinks
) {
2104 err (1, "calloc slinks %d", count
);
2107 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
2111 fz_transform_rect (&rect
, &ctm
);
2112 page
->slinks
[i
].tag
= SLINK
;
2113 page
->slinks
[i
].u
.link
= link
;
2114 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2116 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2118 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2119 fz_transform_rect (&rect
, &ctm
);
2120 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2122 page
->slinks
[i
].tag
= SANNOT
;
2123 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2125 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2129 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2130 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2133 struct slink
*slink
;
2134 float x0
, y0
, x1
, y1
, w
;
2136 ensureslinks (page
);
2137 glColor3ub (0xc3, 0xb0, 0x91);
2138 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2139 fmt_linkn (buf
, i
+ noff
);
2140 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2141 slink
= &page
->slinks
[i
];
2143 x0
= slink
->bbox
.x0
+ xoff
- 5;
2144 y1
= slink
->bbox
.y0
+ yoff
- 5;
2145 y0
= y1
+ 10 + hfsize
;
2146 w
= measure_string (state
.face
, hfsize
, buf
);
2148 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2152 glEnable (GL_BLEND
);
2153 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2154 glEnable (GL_TEXTURE_2D
);
2155 glColor3ub (0, 0, 0);
2156 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2157 fmt_linkn (buf
, i
+ noff
);
2158 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2159 slink
= &page
->slinks
[i
];
2161 x0
= slink
->bbox
.x0
+ xoff
;
2162 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2163 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2166 glDisable (GL_TEXTURE_2D
);
2167 glDisable (GL_BLEND
);
2170 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2173 struct slice
*slice1
;
2174 unsigned char *texdata
;
2177 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2178 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2180 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2181 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2182 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2186 int texindex
= state
.texindex
++ % state
.texcount
;
2188 if (state
.texowners
[texindex
].w
== tile
->w
) {
2189 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2193 state
.texowners
[texindex
].h
= slice
->h
;
2197 state
.texowners
[texindex
].h
= slice
->h
;
2200 state
.texowners
[texindex
].w
= tile
->w
;
2201 state
.texowners
[texindex
].slice
= slice
;
2202 slice
->texindex
= texindex
;
2204 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2205 #if TEXT_TYPE == GL_TEXTURE_2D
2206 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2207 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2208 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2209 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2212 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2216 texdata
= tile
->pixmap
->samples
;
2219 glTexSubImage2D (TEXT_TYPE
,
2231 glTexImage2D (TEXT_TYPE
,
2243 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2248 CAMLprim
void ml_begintiles (void)
2250 glEnable (TEXT_TYPE
);
2251 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2252 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2255 CAMLprim
void ml_endtiles (void)
2257 glDisable (TEXT_TYPE
);
2260 CAMLprim
void ml_drawtile (value args_v
, value ptr_v
)
2262 CAMLparam2 (args_v
, ptr_v
);
2263 int dispx
= Int_val (Field (args_v
, 0));
2264 int dispy
= Int_val (Field (args_v
, 1));
2265 int dispw
= Int_val (Field (args_v
, 2));
2266 int disph
= Int_val (Field (args_v
, 3));
2267 int tilex
= Int_val (Field (args_v
, 4));
2268 int tiley
= Int_val (Field (args_v
, 5));
2269 char *s
= String_val (ptr_v
);
2270 struct tile
*tile
= parse_pointer (__func__
, s
);
2271 int slicey
, firstslice
;
2272 struct slice
*slice
;
2273 GLfloat
*texcoords
= state
.texcoords
;
2274 GLfloat
*vertices
= state
.vertices
;
2276 firstslice
= tiley
/ tile
->sliceheight
;
2277 slice
= &tile
->slices
[firstslice
];
2278 slicey
= tiley
% tile
->sliceheight
;
2283 dh
= slice
->h
- slicey
;
2284 dh
= fz_mini (disph
, dh
);
2285 uploadslice (tile
, slice
);
2287 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2288 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2289 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2290 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2292 vertices
[0] = dispx
; vertices
[1] = dispy
;
2293 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2294 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2295 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2297 #if TEXT_TYPE == GL_TEXTURE_2D
2298 for (int i
= 0; i
< 8; ++i
) {
2299 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2303 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2307 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2313 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2315 fz_matrix ctm
, tm
, pm
;
2316 fz_point p1
, p2
, p3
, p4
;
2317 GLfloat
*vertices
= state
.vertices
;
2318 double *v
= (double *) rects_v
;
2320 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2321 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2322 fz_translate (&tm
, xoff
, yoff
);
2323 pm
= pagectm (page
);
2324 fz_concat (&ctm
, &pm
, &tm
);
2326 glEnable (GL_BLEND
);
2327 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2330 p1
.x
= (float) v
[4];
2331 p1
.y
= (float) v
[5];
2333 p2
.x
= (float) v
[6];
2334 p2
.y
= (float) v
[5];
2336 p3
.x
= (float) v
[6];
2337 p3
.y
= (float) v
[7];
2339 p4
.x
= (float) v
[4];
2340 p4
.y
= (float) v
[7];
2341 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2342 glDisable (GL_BLEND
);
2345 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2346 value xoff_v
, value yoff_v
,
2349 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2350 int xoff
= Int_val (xoff_v
);
2351 int yoff
= Int_val (yoff_v
);
2352 int noff
= Int_val (Field (li_v
, 0));
2353 char *targ
= String_val (Field (li_v
, 1));
2354 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2355 int hfsize
= Int_val (Field (li_v
, 2));
2356 char *s
= String_val (ptr_v
);
2357 int hlmask
= Int_val (hlinks_v
);
2358 struct page
*page
= parse_pointer (__func__
, s
);
2360 if (!page
->fzpage
) {
2361 /* deal with loadpage failed pages */
2365 if (trylock (__func__
)) {
2370 ensureannots (page
);
2371 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2373 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2374 noff
= page
->slinkcount
;
2376 if (page
->tgen
== state
.gen
) {
2377 showsel (page
, xoff
, yoff
);
2382 CAMLreturn (Val_int (noff
));
2385 CAMLprim
void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2388 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2389 int xoff
= Int_val (xoff_v
);
2390 int yoff
= Int_val (yoff_v
);
2391 char *s
= String_val (ptr_v
);
2392 struct page
*page
= parse_pointer (__func__
, s
);
2394 drawprect (page
, xoff
, yoff
, rects_v
);
2398 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2402 const fz_matrix
*tctm
;
2403 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2405 if (!page
->annots
) return NULL
;
2408 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2409 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2412 tctm
= &fz_identity
;
2418 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2419 fz_invert_matrix (&ctm
, &ctm
);
2420 fz_transform_point (&p
, &ctm
);
2423 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2424 struct annot
*a
= &page
->annots
[i
];
2427 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2428 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2429 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2437 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2443 ensureslinks (page
);
2448 ctm
= pagectm (page
);
2449 fz_invert_matrix (&ctm
, &ctm
);
2450 fz_transform_point (&p
, &ctm
);
2452 for (link
= page
->links
; link
; link
= link
->next
) {
2453 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2454 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2462 static void ensuretext (struct page
*page
)
2464 if (state
.gen
!= page
->tgen
) {
2466 page
->tgen
= state
.gen
;
2472 page
->text
= fz_new_stext_page (state
.ctx
,
2473 &state
.pagedims
[page
->pdimno
].mediabox
);
2474 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2475 ctm
= pagectm (page
);
2476 fz_run_display_list (state
.ctx
, page
->dlist
,
2477 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2478 fz_close_device (state
.ctx
, tdev
);
2479 fz_drop_device (state
.ctx
, tdev
);
2483 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2485 CAMLparam2 (start_page_v
, dir_v
);
2487 int i
, dir
= Int_val (dir_v
);
2488 int start_page
= Int_val (start_page_v
);
2489 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2493 ret_v
= Val_int (0);
2495 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2496 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2501 pdf_page
*page
= NULL
;
2504 fz_try (state
.ctx
) {
2505 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2506 found
= !!page
->links
|| !!page
->annots
;
2508 fz_catch (state
.ctx
) {
2512 fz_drop_page (state
.ctx
, &page
->super
);
2516 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2517 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2519 fz_drop_link (state
.ctx
, link
);
2520 fz_drop_page (state
.ctx
, page
);
2524 ret_v
= caml_alloc_small (1, 1);
2525 Field (ret_v
, 0) = Val_int (i
);
2534 enum { dir_first
, dir_last
};
2535 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2537 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2539 CAMLparam2 (ptr_v
, dir_v
);
2540 CAMLlocal2 (ret_v
, pos_v
);
2542 int dirtag
, i
, slinkindex
;
2543 struct slink
*found
= NULL
,*slink
;
2544 char *s
= String_val (ptr_v
);
2546 page
= parse_pointer (__func__
, s
);
2547 ret_v
= Val_int (0);
2549 ensureslinks (page
);
2551 if (Is_block (dir_v
)) {
2552 dirtag
= Tag_val (dir_v
);
2554 case dir_first_visible
:
2556 int x0
, y0
, dir
, first_index
, last_index
;
2558 pos_v
= Field (dir_v
, 0);
2559 x0
= Int_val (Field (pos_v
, 0));
2560 y0
= Int_val (Field (pos_v
, 1));
2561 dir
= Int_val (Field (pos_v
, 2));
2566 last_index
= page
->slinkcount
;
2569 first_index
= page
->slinkcount
- 1;
2573 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2574 slink
= &page
->slinks
[i
];
2575 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2584 slinkindex
= Int_val (Field (dir_v
, 0));
2585 found
= &page
->slinks
[slinkindex
];
2586 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2587 slink
= &page
->slinks
[i
];
2588 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2596 slinkindex
= Int_val (Field (dir_v
, 0));
2597 found
= &page
->slinks
[slinkindex
];
2598 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2599 slink
= &page
->slinks
[i
];
2600 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2608 slinkindex
= Int_val (Field (dir_v
, 0));
2609 found
= &page
->slinks
[slinkindex
];
2610 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2611 slink
= &page
->slinks
[i
];
2612 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2620 slinkindex
= Int_val (Field (dir_v
, 0));
2621 found
= &page
->slinks
[slinkindex
];
2622 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2623 slink
= &page
->slinks
[i
];
2624 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2633 dirtag
= Int_val (dir_v
);
2636 found
= page
->slinks
;
2641 found
= page
->slinks
+ (page
->slinkcount
- 1);
2647 ret_v
= caml_alloc_small (2, 1);
2648 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2655 enum { uuri
, utext
, uannot
};
2657 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2659 CAMLparam2 (ptr_v
, n_v
);
2660 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2663 char *s
= String_val (ptr_v
);
2664 struct slink
*slink
;
2666 ret_v
= Val_int (0);
2667 page
= parse_pointer (__func__
, s
);
2670 ensureslinks (page
);
2671 slink
= &page
->slinks
[Int_val (n_v
)];
2672 if (slink
->tag
== SLINK
) {
2673 link
= slink
->u
.link
;
2674 str_v
= caml_copy_string (link
->uri
);
2675 ret_v
= caml_alloc_small (1, uuri
);
2676 Field (ret_v
, 0) = str_v
;
2679 ret_v
= caml_alloc_small (1, uannot
);
2680 tup_v
= caml_alloc_tuple (2);
2681 Field (ret_v
, 0) = tup_v
;
2682 Field (tup_v
, 0) = ptr_v
;
2683 Field (tup_v
, 1) = n_v
;
2690 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2692 CAMLparam2 (ptr_v
, n_v
);
2695 char *contents
= NULL
;
2698 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2700 char *s
= String_val (ptr_v
);
2702 struct slink
*slink
;
2704 page
= parse_pointer (__func__
, s
);
2705 slink
= &page
->slinks
[Int_val (n_v
)];
2706 contents
= pdf_copy_annot_contents (state
.ctx
,
2707 (pdf_annot
*) slink
->u
.annot
);
2711 ret_v
= caml_copy_string (contents
);
2712 fz_free (state
.ctx
, contents
);
2715 ret_v
= caml_copy_string ("");
2720 CAMLprim value
ml_getlinkcount (value ptr_v
)
2724 char *s
= String_val (ptr_v
);
2726 page
= parse_pointer (__func__
, s
);
2727 CAMLreturn (Val_int (page
->slinkcount
));
2730 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2732 CAMLparam2 (ptr_v
, n_v
);
2735 struct slink
*slink
;
2736 char *s
= String_val (ptr_v
);
2738 page
= parse_pointer (__func__
, s
);
2739 ret_v
= caml_alloc_tuple (4);
2741 ensureslinks (page
);
2743 slink
= &page
->slinks
[Int_val (n_v
)];
2744 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2745 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2746 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2747 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2752 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2754 CAMLparam3 (ptr_v
, x_v
, y_v
);
2755 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2757 struct annot
*annot
;
2759 char *ptr
= String_val (ptr_v
);
2760 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2761 struct pagedim
*pdim
;
2763 ret_v
= Val_int (0);
2764 if (trylock (__func__
)) {
2768 page
= parse_pointer (__func__
, ptr
);
2769 pdim
= &state
.pagedims
[page
->pdimno
];
2770 x
+= pdim
->bounds
.x0
;
2771 y
+= pdim
->bounds
.y0
;
2774 annot
= getannot (page
, x
, y
);
2778 ensureslinks (page
);
2779 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2780 if (page
->slinks
[i
].tag
== SANNOT
2781 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2786 ret_v
= caml_alloc_small (1, uannot
);
2787 tup_v
= caml_alloc_tuple (2);
2788 Field (ret_v
, 0) = tup_v
;
2789 Field (tup_v
, 0) = ptr_v
;
2790 Field (tup_v
, 1) = Val_int (n
);
2795 link
= getlink (page
, x
, y
);
2797 str_v
= caml_copy_string (link
->uri
);
2798 ret_v
= caml_alloc_small (1, uuri
);
2799 Field (ret_v
, 0) = str_v
;
2803 fz_stext_block
*block
;
2807 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2808 fz_stext_line
*line
;
2810 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2812 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2815 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2819 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2822 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2825 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
2826 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
2827 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
2830 if (!n2
) n2
= "<unknown font>";
2832 if (face
&& face
->family_name
) {
2834 char *n1
= face
->family_name
;
2835 size_t l1
= strlen (n1
);
2836 size_t l2
= strlen (n2
);
2838 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2839 s
= malloc (l1
+ l2
+ 2);
2843 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2844 str_v
= caml_copy_string (s
);
2849 if (str_v
== Val_unit
) {
2850 str_v
= caml_copy_string (n2
);
2852 ret_v
= caml_alloc_small (1, utext
);
2853 Field (ret_v
, 0) = str_v
;
2867 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2869 CAMLprim
void ml_clearmark (value ptr_v
)
2872 char *s
= String_val (ptr_v
);
2875 if (trylock (__func__
)) {
2879 page
= parse_pointer (__func__
, s
);
2888 static int uninteresting (int c
)
2890 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
2894 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2896 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2900 fz_stext_line
*line
;
2901 fz_stext_block
*block
;
2902 struct pagedim
*pdim
;
2903 int mark
= Int_val (mark_v
);
2904 char *s
= String_val (ptr_v
);
2905 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2907 ret_v
= Val_bool (0);
2908 if (trylock (__func__
)) {
2912 page
= parse_pointer (__func__
, s
);
2913 pdim
= &state
.pagedims
[page
->pdimno
];
2917 if (mark
== mark_page
) {
2918 page
->fmark
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
2919 page
->lmark
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
2920 ret_v
= Val_bool (1);
2924 x
+= pdim
->bounds
.x0
;
2925 y
+= pdim
->bounds
.y0
;
2927 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2928 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2930 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2933 if (mark
== mark_block
) {
2934 page
->fmark
= block
->u
.t
.first_line
->first_char
;
2935 page
->lmark
= block
->u
.t
.last_line
->last_char
;
2936 ret_v
= Val_bool (1);
2940 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2944 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2947 if (mark
== mark_line
) {
2948 page
->fmark
= line
->first_char
;
2949 page
->lmark
= line
->last_char
;
2950 ret_v
= Val_bool (1);
2954 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2955 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
2957 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
2958 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
2959 if (uninteresting (ch2
->c
)) first
= NULL
;
2960 else if (!first
) first
= ch2
;
2962 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
2963 if (uninteresting (ch2
->c
)) break;
2967 page
->fmark
= first
;
2969 ret_v
= Val_bool (1);
2976 if (!Bool_val (ret_v
)) {
2986 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
2988 CAMLparam3 (ptr_v
, x_v
, y_v
);
2989 CAMLlocal2 (ret_v
, res_v
);
2992 struct pagedim
*pdim
;
2993 fz_stext_block
*block
;
2994 char *s
= String_val (ptr_v
);
2995 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2997 ret_v
= Val_int (0);
2998 if (trylock (__func__
)) {
3002 page
= parse_pointer (__func__
, s
);
3003 pdim
= &state
.pagedims
[page
->pdimno
];
3004 x
+= pdim
->bounds
.x0
;
3005 y
+= pdim
->bounds
.y0
;
3009 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3010 switch (block
->type
) {
3011 case FZ_STEXT_BLOCK_TEXT
:
3015 case FZ_STEXT_BLOCK_IMAGE
:
3023 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3028 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3029 ret_v
= caml_alloc_small (1, 1);
3030 Store_double_field (res_v
, 0, (double) b
->x0
);
3031 Store_double_field (res_v
, 1, (double) b
->x1
);
3032 Store_double_field (res_v
, 2, (double) b
->y0
);
3033 Store_double_field (res_v
, 3, (double) b
->y1
);
3034 Field (ret_v
, 0) = res_v
;
3042 CAMLprim
void ml_seltext (value ptr_v
, value rect_v
)
3044 CAMLparam2 (ptr_v
, rect_v
);
3047 struct pagedim
*pdim
;
3048 char *s
= String_val (ptr_v
);
3051 fz_stext_line
*line
;
3052 fz_stext_block
*block
;
3053 fz_stext_char
*fc
, *lc
;
3055 if (trylock (__func__
)) {
3059 page
= parse_pointer (__func__
, s
);
3062 pdim
= &state
.pagedims
[page
->pdimno
];
3063 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3064 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3065 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3066 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3079 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3080 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3081 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3082 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3084 if (x0
>= b
.x0
&& x0
<= b
.x1
&& y0
>= b
.y0
&& y0
<= b
.y1
) {
3087 if (x1
>= b
.x0
&& x1
<= b
.x1
&& y1
>= b
.y0
&& y1
<= b
.y1
) {
3093 if (x1
< x0
&& fc
== lc
) {
3110 static int pipechar (FILE *f
, fz_stext_char
*ch
)
3116 len
= fz_runetochar (buf
, ch
->c
);
3117 ret
= fwrite (buf
, len
, 1, f
);
3119 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3120 len
, ret
, errno
, strerror (errno
));
3126 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3128 CAMLparam2 (command_v
, fds_v
);
3129 CAMLlocal2 (l_v
, tup_v
);
3131 pid_t pid
= (pid_t
) -1;
3133 value earg_v
= Nothing
;
3134 posix_spawnattr_t attr
;
3135 posix_spawn_file_actions_t fa
;
3136 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3138 argv
[2] = String_val (command_v
);
3140 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3141 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3144 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3145 msg
= "posix_spawnattr_init";
3149 #ifdef POSIX_SPAWN_USEVFORK
3150 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3151 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3156 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3159 tup_v
= Field (l_v
, 0);
3160 fd1
= Int_val (Field (tup_v
, 0));
3161 fd2
= Int_val (Field (tup_v
, 1));
3163 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3164 msg
= "posix_spawn_file_actions_addclose";
3170 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3171 msg
= "posix_spawn_file_actions_adddup2";
3178 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3179 msg
= "posix_spawn";
3184 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3185 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3189 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3190 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3191 ret1
, strerror (ret1
));
3195 unix_error (ret
, msg
, earg_v
);
3197 CAMLreturn (Val_int (pid
));
3200 CAMLprim value
ml_hassel (value ptr_v
)
3205 char *s
= String_val (ptr_v
);
3207 ret_v
= Val_bool (0);
3208 if (trylock (__func__
)) {
3212 page
= parse_pointer (__func__
, s
);
3213 ret_v
= Val_bool (page
->fmark
&& page
->lmark
);
3219 CAMLprim
void ml_copysel (value fd_v
, value ptr_v
)
3221 CAMLparam2 (fd_v
, ptr_v
);
3225 fz_stext_line
*line
;
3226 fz_stext_block
*block
;
3227 int fd
= Int_val (fd_v
);
3228 char *s
= String_val (ptr_v
);
3230 if (trylock (__func__
)) {
3234 page
= parse_pointer (__func__
, s
);
3236 if (!page
->fmark
|| !page
->lmark
) {
3237 printd ("emsg nothing to copy on page %d", page
->pageno
);
3241 f
= fdopen (fd
, "w");
3243 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3244 fd
, errno
, strerror (errno
));
3248 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3249 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3250 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3252 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3253 if (seen
|| ch
== page
->fmark
) {
3256 if (ch
== page
->lmark
) goto close
;
3257 } while ((ch
= ch
->next
));
3262 if (seen
) fputc ('\n', f
);
3267 int ret
= fclose (f
);
3270 if (errno
!= ECHILD
) {
3271 printd ("emsg failed to close sel pipe: %d:%s",
3272 errno
, strerror (errno
));
3282 printd ("emsg failed to close sel pipe: %d:%s",
3283 errno
, strerror (errno
));
3289 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3291 CAMLparam1 (pagedimno_v
);
3293 int pagedimno
= Int_val (pagedimno_v
);
3296 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3297 if (trylock (__func__
)) {
3298 box
= fz_empty_rect
;
3301 box
= state
.pagedims
[pagedimno
].mediabox
;
3305 Store_double_field (ret_v
, 0, (double) box
.x0
);
3306 Store_double_field (ret_v
, 1, (double) box
.x1
);
3307 Store_double_field (ret_v
, 2, (double) box
.y0
);
3308 Store_double_field (ret_v
, 3, (double) box
.y1
);
3313 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3314 value dw_v
, value cols_v
)
3316 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3322 float winw
= Int_val (winw_v
);
3323 float winh
= Int_val (winh_v
);
3324 float dw
= Int_val (dw_v
);
3325 float cols
= Int_val (cols_v
);
3326 float pw
= 1.0, ph
= 1.0;
3328 if (trylock (__func__
)) {
3332 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3333 float w
= p
->pagebox
.x1
/ cols
;
3334 float h
= p
->pagebox
.y1
;
3338 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3340 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3343 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3346 ret_v
= caml_copy_double ((double) zoom
);
3350 CAMLprim value
ml_getmaxw (value unit_v
)
3352 CAMLparam1 (unit_v
);
3358 if (trylock (__func__
)) {
3362 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3363 float w
= p
->pagebox
.x1
;
3364 maxw
= fz_max (maxw
, w
);
3369 ret_v
= caml_copy_double ((double) maxw
);
3373 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3375 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3377 int pt
= Int_val(pt_v
);
3378 int x
= Int_val (x_v
);
3379 int y
= Int_val (y_v
);
3382 w
= (double) draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3383 ret_v
= caml_copy_double (w
);
3387 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3389 CAMLparam2 (pt_v
, string_v
);
3391 int pt
= Int_val (pt_v
);
3394 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3395 ret_v
= caml_copy_double (w
);
3399 CAMLprim value
ml_getpagebox (value opaque_v
)
3401 CAMLparam1 (opaque_v
);
3407 char *s
= String_val (opaque_v
);
3408 struct page
*page
= parse_pointer (__func__
, s
);
3410 ret_v
= caml_alloc_tuple (4);
3411 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3413 ctm
= pagectm (page
);
3414 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3416 fz_close_device (state
.ctx
, dev
);
3417 fz_drop_device (state
.ctx
, dev
);
3418 fz_round_rect (&bbox
, &rect
);
3419 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3420 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3421 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3422 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3427 CAMLprim
void ml_setaalevel (value level_v
)
3429 CAMLparam1 (level_v
);
3431 state
.aalevel
= Int_val (level_v
);
3436 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3438 CAMLparam1 (keysym_v
);
3440 KeySym keysym
= Int_val (keysym_v
);
3442 extern long keysym2ucs (KeySym
);
3446 rune
= (Rune
) keysym2ucs (keysym
);
3447 len
= fz_runetochar (buf
, rune
);
3449 str_v
= caml_copy_string (buf
);
3453 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3455 CAMLparam1 (keysym_v
);
3457 long ucs_v
= Long_val (keysym_v
);
3461 len
= fz_runetochar (buf
, (int) ucs_v
);
3463 str_v
= caml_copy_string (buf
);
3468 enum { piunknown
, pilinux
, pimacos
, pibsd
};
3470 CAMLprim value
ml_platform (value unit_v
)
3472 CAMLparam1 (unit_v
);
3473 CAMLlocal2 (tup_v
, arr_v
);
3474 int platid
= piunknown
;
3477 #if defined __linux__
3479 #elif defined __DragonFly__ || defined __FreeBSD__
3480 || defined __OpenBSD__
|| defined __NetBSD__
3482 #elif defined __APPLE__
3485 if (uname (&buf
)) err (1, "uname");
3487 tup_v
= caml_alloc_tuple (2);
3489 char const *sar
[] = {
3496 arr_v
= caml_copy_string_array (sar
);
3498 Field (tup_v
, 0) = Val_int (platid
);
3499 Field (tup_v
, 1) = arr_v
;
3503 CAMLprim
void ml_cloexec (value fd_v
)
3506 int fd
= Int_val (fd_v
);
3508 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3509 uerror ("fcntl", Nothing
);
3514 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3516 CAMLparam2 (w_v
, h_v
);
3519 int w
= Int_val (w_v
);
3520 int h
= Int_val (h_v
);
3521 int cs
= Int_val (cs_v
);
3523 if (state
.bo_usable
) {
3524 pbo
= calloc (sizeof (*pbo
), 1);
3526 err (1, "calloc pbo");
3538 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3541 state
.glGenBuffersARB (1, &pbo
->id
);
3542 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3543 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3544 NULL
, GL_STREAM_DRAW
);
3545 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3547 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3549 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3550 state
.glDeleteBuffersARB (1, &pbo
->id
);
3552 ret_v
= caml_copy_string ("0");
3558 res
= snprintf (NULL
, 0, "%" PRIxPTR
, (uintptr_t) pbo
);
3560 err (1, "snprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3564 err (1, "malloc %d bytes failed", res
+1);
3566 res
= sprintf (s
, "%" PRIxPTR
, (uintptr_t) pbo
);
3568 err (1, "sprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3570 ret_v
= caml_copy_string (s
);
3575 ret_v
= caml_copy_string ("0");
3580 CAMLprim
void ml_freepbo (value s_v
)
3583 char *s
= String_val (s_v
);
3584 struct tile
*tile
= parse_pointer (__func__
, s
);
3587 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3589 tile
->pbo
->ptr
= NULL
;
3590 tile
->pbo
->size
= -1;
3595 CAMLprim
void ml_unmappbo (value s_v
)
3598 char *s
= String_val (s_v
);
3599 struct tile
*tile
= parse_pointer (__func__
, s
);
3602 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3603 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3604 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3606 tile
->pbo
->ptr
= NULL
;
3607 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3612 static void setuppbo (void)
3614 extern void (*wsigladdr (const char *name
)) (void);
3615 #pragma GCC diagnostic push
3617 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3619 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3620 state
.bo_usable
= GPA (glBindBufferARB
)
3621 && GPA (glUnmapBufferARB
)
3622 && GPA (glMapBufferARB
)
3623 && GPA (glBufferDataARB
)
3624 && GPA (glGenBuffersARB
)
3625 && GPA (glDeleteBuffersARB
);
3627 #pragma GCC diagnostic pop
3630 CAMLprim value
ml_bo_usable (void)
3632 return Val_bool (state
.bo_usable
);
3635 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3637 CAMLparam3 (ptr_v
, x_v
, y_v
);
3638 CAMLlocal2 (ret_v
, tup_v
);
3640 char *s
= String_val (ptr_v
);
3641 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3642 struct pagedim
*pdim
;
3646 page
= parse_pointer (__func__
, s
);
3647 pdim
= &state
.pagedims
[page
->pdimno
];
3649 ret_v
= Val_int (0);
3650 if (trylock (__func__
)) {
3654 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3655 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3656 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3661 p
.x
= x
+ pdim
->bounds
.x0
;
3662 p
.y
= y
+ pdim
->bounds
.y0
;
3664 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3665 fz_invert_matrix (&ctm
, &ctm
);
3666 fz_transform_point (&p
, &ctm
);
3668 tup_v
= caml_alloc_tuple (2);
3669 ret_v
= caml_alloc_small (1, 1);
3670 Field (tup_v
, 0) = Val_int (p
.x
);
3671 Field (tup_v
, 1) = Val_int (p
.y
);
3672 Field (ret_v
, 0) = tup_v
;
3679 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
3680 value x_v
, value y_v
)
3682 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
3685 char *s
= String_val (ptr_v
);
3686 int pageno
= Int_val (pageno_v
);
3687 int pdimno
= Int_val (pdimno_v
);
3688 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
3689 struct pagedim
*pdim
;
3693 ret_v
= Val_int (0);
3697 page
= loadpage (pageno
, pdimno
);
3700 page
= parse_pointer (__func__
, s
);
3702 pdim
= &state
.pagedims
[pdimno
];
3704 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3705 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3706 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3711 p
.x
= x
+ pdim
->bounds
.x0
;
3712 p
.y
= y
+ pdim
->bounds
.y0
;
3714 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3715 fz_transform_point (&p
, &ctm
);
3717 ret_v
= caml_alloc_tuple (2);
3718 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
3719 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
3728 CAMLprim
void ml_addannot (value ptr_v
, value x_v
, value y_v
,
3731 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
3732 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3738 char *s
= String_val (ptr_v
);
3740 page
= parse_pointer (__func__
, s
);
3741 annot
= pdf_create_annot (state
.ctx
,
3742 pdf_page_from_fz_page (state
.ctx
,
3745 p
.x
= Int_val (x_v
);
3746 p
.y
= Int_val (y_v
);
3747 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
3748 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
3754 CAMLprim
void ml_delannot (value ptr_v
, value n_v
)
3756 CAMLparam2 (ptr_v
, n_v
);
3757 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3761 char *s
= String_val (ptr_v
);
3762 struct slink
*slink
;
3764 page
= parse_pointer (__func__
, s
);
3765 slink
= &page
->slinks
[Int_val (n_v
)];
3766 pdf_delete_annot (state
.ctx
,
3767 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
3768 (pdf_annot
*) slink
->u
.annot
);
3774 CAMLprim
void ml_modannot (value ptr_v
, value n_v
, value str_v
)
3776 CAMLparam3 (ptr_v
, n_v
, str_v
);
3777 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3781 char *s
= String_val (ptr_v
);
3782 struct slink
*slink
;
3784 page
= parse_pointer (__func__
, s
);
3785 slink
= &page
->slinks
[Int_val (n_v
)];
3786 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
3787 String_val (str_v
));
3793 CAMLprim value
ml_hasunsavedchanges (void)
3795 return Val_bool (state
.dirty
);
3798 CAMLprim
void ml_savedoc (value path_v
)
3800 CAMLparam1 (path_v
);
3801 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3804 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
3809 static void makestippletex (void)
3811 const char pixels
[] = "\xff\xff\0\0";
3812 glGenTextures (1, &state
.stid
);
3813 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3814 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3815 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3828 CAMLprim value
ml_fz_version (void)
3830 return caml_copy_string (FZ_VERSION
);
3833 CAMLprim value
ml_llpp_version (void)
3835 extern char llpp_version
[];
3836 return caml_copy_string (llpp_version
);
3839 CAMLprim
void ml_init (value csock_v
, value params_v
)
3841 CAMLparam2 (csock_v
, params_v
);
3842 CAMLlocal2 (trim_v
, fuzz_v
);
3849 state
.csock
= Int_val (csock_v
);
3850 state
.rotate
= Int_val (Field (params_v
, 0));
3851 state
.fitmodel
= Int_val (Field (params_v
, 1));
3852 trim_v
= Field (params_v
, 2);
3853 texcount
= Int_val (Field (params_v
, 3));
3854 state
.sliceheight
= Int_val (Field (params_v
, 4));
3855 mustoresize
= Int_val (Field (params_v
, 5));
3856 colorspace
= Int_val (Field (params_v
, 6));
3857 fontpath
= String_val (Field (params_v
, 7));
3862 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3863 if (setlocale (LC_CTYPE
, "")) {
3864 const char *cset
= nl_langinfo (CODESET
);
3865 state
.utf8cs
= !strcmp (cset
, "UTF-8");
3868 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
3872 if (caml_string_length (Field (params_v
, 8)) > 0) {
3873 state
.trimcachepath
= ystrdup (String_val (Field (params_v
, 8)));
3875 if (!state
.trimcachepath
) {
3876 printd ("emsg failed to strdup trimcachepath: %d:%s",
3877 errno
, strerror (errno
));
3881 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3882 fz_register_document_handlers (state
.ctx
);
3884 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3885 fuzz_v
= Field (trim_v
, 1);
3886 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3887 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3888 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3889 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3891 set_tex_params (colorspace
);
3894 state
.face
= load_font (fontpath
);
3898 const unsigned char *data
;
3900 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
3901 state
.face
= load_builtin_font (data
, len
);
3903 if (!state
.face
) _exit (1);
3905 realloctexts (texcount
);
3909 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3911 errx (1, "pthread_create: %s", strerror (ret
));
3918 static void UNUSED_ATTR NO_OPTIMIZE_ATTR
refmacs (void) {}