1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/utsname.h>
21 #include <cygwin/socket.h> /* FIONREAD */
33 #include <caml/fail.h>
34 #include <caml/alloc.h>
35 #include <caml/memory.h>
36 #include <caml/unixsupport.h>
38 #if __GNUC__ < 5 && !defined __clang__
39 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
40 clobbered diagnostics */
41 #pragma GCC diagnostic ignored "-Wclobbered"
44 #include <mupdf/fitz.h>
45 #include <mupdf/pdf.h>
48 #include FT_FREETYPE_H
51 #include <fontconfig/fontconfig.h>
55 #define CACHE_PAGEREFS
58 extern char **environ
;
62 #define NORETURN_ATTR __attribute__ ((noreturn))
63 #define UNUSED_ATTR __attribute__ ((unused))
64 #if !defined __clang__
65 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
67 #define OPTIMIZE_ATTR(n)
69 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
73 #define OPTIMIZE_ATTR(n)
74 #define GCC_FMT_ATTR(a, b)
79 #define FMT_ptr PRIxPTR
80 #define SCN_ptr SCNxPTR
81 #define FMT_ptr_cast(p) ((uintptr_t) (p))
82 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
84 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
85 err (int exitcode
, const char *fmt
, ...)
92 vfprintf (stderr
, fmt
, ap
);
94 fprintf (stderr
, ": %s\n", strerror (savederrno
));
99 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
100 errx (int exitcode
, const char *fmt
, ...)
105 vfprintf (stderr
, fmt
, ap
);
107 fputc ('\n', stderr
);
112 #ifndef GL_TEXTURE_RECTANGLE_ARB
113 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
117 #define TEXT_TYPE GL_TEXTURE_2D
119 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
123 #define GL_BGRA 0x80E1
126 #ifndef GL_UNSIGNED_INT_8_8_8_8
127 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
130 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
131 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
135 #define lprintf printf
140 #define ARSERT(cond) for (;;) { \
142 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
158 struct slice slices
[1];
169 fz_matrix ctm
, zoomctm
, tctm
;
173 enum { SLINK
, SANNOT
} tag
;
193 fz_stext_sheet
*sheet
;
195 fz_display_list
*dlist
;
197 struct slink
*slinks
;
199 struct annot
*annots
;
208 struct pagedim
*pagedims
;
223 fz_colorspace
*colorspace
;
231 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
254 void (*glBindBufferARB
) (GLenum
, GLuint
);
255 GLboolean (*glUnmapBufferARB
) (GLenum
);
256 void *(*glMapBufferARB
) (GLenum
, GLenum
);
257 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
258 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
259 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
261 GLfloat texcoords
[8];
262 GLfloat vertices
[16];
264 #ifdef CACHE_PAGEREFS
280 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
282 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
285 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
287 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
290 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
292 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
293 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
296 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
298 static void lock (const char *cap
)
300 int ret
= pthread_mutex_lock (&mutex
);
302 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
306 static void unlock (const char *cap
)
308 int ret
= pthread_mutex_unlock (&mutex
);
310 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
314 static int trylock (const char *cap
)
316 int ret
= pthread_mutex_trylock (&mutex
);
317 if (ret
&& ret
!= EBUSY
) {
318 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
323 static void *parse_pointer (const char *cap
, const char *s
)
328 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
330 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
335 static double now (void)
339 if (gettimeofday (&tv
, NULL
)) {
340 err (1, "gettimeofday");
342 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
345 static int hasdata (void)
348 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
349 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
353 CAMLprim value
ml_hasdata (value fd_v
)
358 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
359 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
360 CAMLreturn (Val_bool (avail
> 0));
363 static void readdata (int fd
, void *p
, int size
)
368 n
= read (fd
, p
, size
);
370 if (errno
== EINTR
) goto again
;
371 err (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
374 if (!n
) errx (1, "EOF while reading");
375 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
379 static void writedata (int fd
, char *p
, int size
)
383 /* One should lookup type punning/strict aliasing etc in standard,DRs,Web to
384 convince herself that this is:
386 b. practically the only way to achieve the result
387 (union puns notwithstanding) */
388 memcpy (p
, &size
, 4);
389 n
= write (fd
, p
, size
+ 4);
391 if (!n
) errx (1, "EOF while writing data");
392 err (1, "write (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
396 static int readlen (int fd
)
398 /* Type punned unions here. Why? Less code (Adjusted by more comments).
399 https://en.wikipedia.org/wiki/Type_punning */
400 union { int len
; char raw
[4]; } buf
;
401 readdata (fd
, buf
.raw
, 4);
405 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
407 CAMLparam3 (fd_v
, bytes_v
, len_v
);
408 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
412 CAMLprim value
ml_rcmd (value fd_v
)
415 CAMLlocal1 (strdata_v
);
416 int fd
= Int_val (fd_v
);
417 int len
= readlen (fd
);
418 strdata_v
= caml_alloc_string (len
);
419 readdata (fd
, String_val (strdata_v
), len
);
420 CAMLreturn (strdata_v
);
423 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
431 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
434 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
438 if (len
< size
- 4) {
439 writedata (state
.csock
, buf
, len
);
445 err (1, "vsnprintf for `%s' failed", fmt
);
447 buf
= realloc (buf
, size
);
452 static void closedoc (void)
454 #ifdef CACHE_PAGEREFS
455 if (state
.pdflut
.objs
) {
458 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
459 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
461 free (state
.pdflut
.objs
);
462 state
.pdflut
.objs
= NULL
;
463 state
.pdflut
.idx
= 0;
467 fz_drop_document (state
.ctx
, state
.doc
);
472 static int openxref (char *filename
, char *password
)
476 for (i
= 0; i
< state
.texcount
; ++i
) {
477 state
.texowners
[i
].w
= -1;
478 state
.texowners
[i
].slice
= NULL
;
484 if (state
.pagedims
) {
485 free (state
.pagedims
);
486 state
.pagedims
= NULL
;
488 state
.pagedimcount
= 0;
490 fz_set_aa_level (state
.ctx
, state
.aalevel
);
491 #ifdef CSS_HACK_TO_READ_EPUBS_COMFORTABLY
492 fz_set_user_css (state
.ctx
,
493 "body { margin-left: 20%; margin-right: 20%; }");
495 state
.doc
= fz_open_document (state
.ctx
, filename
);
496 if (fz_needs_password (state
.ctx
, state
.doc
)) {
497 if (password
&& !*password
) {
502 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
504 printd ("pass fail");
509 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
513 static void pdfinfo (void)
515 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
519 printd ("info PDF version\t%d.%d",
520 pdf
->version
/ 10, pdf
->version
% 10);
522 infoobj
= pdf_dict_gets (state
.ctx
, pdf_trailer (state
.ctx
,
527 char *items
[] = { "Title", "Author", "Creator",
528 "Producer", "CreationDate" };
530 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
531 pdf_obj
*obj
= pdf_dict_gets (state
.ctx
, infoobj
, items
[i
]);
532 s
= pdf_to_utf8 (state
.ctx
, obj
);
533 if (*s
) printd ("info %s\t%s", items
[i
], s
);
534 fz_free (state
.ctx
, s
);
541 static void unlinktile (struct tile
*tile
)
545 for (i
= 0; i
< tile
->slicecount
; ++i
) {
546 struct slice
*s
= &tile
->slices
[i
];
548 if (s
->texindex
!= -1) {
549 if (state
.texowners
[s
->texindex
].slice
== s
) {
550 state
.texowners
[s
->texindex
].slice
= NULL
;
556 static void freepage (struct page
*page
)
560 fz_drop_stext_page (state
.ctx
, page
->text
);
563 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
568 fz_drop_display_list (state
.ctx
, page
->dlist
);
569 fz_drop_page (state
.ctx
, page
->fzpage
);
573 static void freetile (struct tile
*tile
)
578 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
581 fz_drop_pixmap (state
.ctx
, state
.pig
);
583 state
.pig
= tile
->pixmap
;
588 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
597 static int cacheline32bytes
;
599 static void __attribute__ ((constructor
)) clcheck (void)
601 char **envp
= environ
;
606 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
608 cacheline32bytes
= auxv
[1] == 32;
614 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
616 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
617 if (cacheline32bytes
&& size
> 32) {
618 intptr_t a1
, a2
, diff
;
620 vector
unsigned char v
= vec_splat_u8 (-1);
621 vector
unsigned char *p
;
623 a1
= a2
= (intptr_t) pixmap
->samples
;
624 a2
= (a1
+ 31) & ~31;
629 while (a1
!= a2
) *(char *) a1
++ = 0xff;
630 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
631 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
633 vec_st (v
, i
+ 16, p
);
635 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
637 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
640 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
643 static void trimctm (pdf_page
*page
, int pindex
)
646 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
649 if (!pdim
->tctmready
) {
650 fz_rect realbox
, mediabox
;
651 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
653 fz_rotate (&rm
, -pdim
->rotate
);
654 fz_scale (&sm
, 1, -1);
655 fz_concat (&ctm
, &rm
, &sm
);
656 realbox
= pdim
->mediabox
;
657 fz_transform_rect (&realbox
, &ctm
);
658 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
659 fz_concat (&ctm1
, &ctm
, &tm
);
660 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
661 fz_invert_matrix (&im
, &page_ctm
);
662 fz_concat (&ctm
, &im
, &ctm1
);
668 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
671 int pdimno
= pdim
- state
.pagedims
;
673 if (pdf_specifics (state
.ctx
, state
.doc
)) {
674 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), pdimno
);
675 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
678 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
679 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
684 static fz_matrix
pagectm (struct page
*page
)
686 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
689 static void *loadpage (int pageno
, int pindex
)
694 page
= calloc (sizeof (struct page
), 1);
696 err (1, "calloc page %d", pageno
);
699 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
700 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
702 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
703 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
706 fz_catch (state
.ctx
) {
709 fz_close_device (state
.ctx
, dev
);
710 fz_drop_device (state
.ctx
, dev
);
712 page
->pdimno
= pindex
;
713 page
->pageno
= pageno
;
714 page
->sgen
= state
.gen
;
715 page
->agen
= state
.gen
;
716 page
->tgen
= state
.gen
;
720 static struct tile
*alloctile (int h
)
727 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
728 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
729 tile
= calloc (tilesize
, 1);
731 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
733 for (i
= 0; i
< slicecount
; ++i
) {
734 int sh
= fz_mini (h
, state
.sliceheight
);
735 tile
->slices
[i
].h
= sh
;
736 tile
->slices
[i
].texindex
= -1;
739 tile
->slicecount
= slicecount
;
740 tile
->sliceheight
= state
.sliceheight
;
744 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
752 struct pagedim
*pdim
;
754 tile
= alloctile (h
);
755 pdim
= &state
.pagedims
[page
->pdimno
];
760 bbox
.x1
= bbox
.x0
+ w
;
761 bbox
.y1
= bbox
.y0
+ h
;
764 if (state
.pig
->w
== w
766 && state
.pig
->colorspace
== state
.colorspace
) {
767 tile
->pixmap
= state
.pig
;
768 tile
->pixmap
->x
= bbox
.x0
;
769 tile
->pixmap
->y
= bbox
.y0
;
772 fz_drop_pixmap (state
.ctx
, state
.pig
);
779 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
785 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
, 1);
791 clearpixmap (tile
->pixmap
);
793 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
794 ctm
= pagectm (page
);
795 fz_rect_from_irect (&rect
, &bbox
);
796 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
797 fz_close_device (state
.ctx
, dev
);
798 fz_drop_device (state
.ctx
, dev
);
803 #ifdef CACHE_PAGEREFS
804 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
805 thanks to Robin Watts */
807 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
809 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
813 if (state
.pdflut
.idx
== state
.pagecount
) return;
815 kids
= pdf_dict_gets (ctx
, node
, "Kids");
816 len
= pdf_array_len (ctx
, kids
);
819 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
821 if (pdf_mark_obj (ctx
, node
))
822 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
823 for (i
= 0; i
< len
; i
++) {
824 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
825 char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
827 ? !strcmp (type
, "Pages")
828 : pdf_dict_gets (ctx
, kid
, "Kids")
829 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
830 pdf_collect_pages (doc
, kid
);
834 ? strcmp (type
, "Page") != 0
835 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
836 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
837 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
840 pdf_unmark_obj (ctx
, node
);
844 pdf_load_page_objs (pdf_document
*doc
)
846 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
847 pdf_trailer (state
.ctx
, doc
), "Root");
848 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
851 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
853 state
.pdflut
.idx
= 0;
854 pdf_collect_pages (doc
, node
);
858 static void initpdims (int wthack
)
862 fz_rect rootmediabox
;
863 int pageno
, trim
, show
;
864 int trimw
= 0, cxcount
;
865 fz_context
*ctx
= state
.ctx
;
866 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
873 if (state
.trimmargins
&& state
.trimcachepath
) {
874 trimf
= fopen (state
.trimcachepath
, "rb");
876 trimf
= fopen (state
.trimcachepath
, "wb");
881 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
882 cxcount
= state
.pagecount
;
884 cxcount
= fz_mini (state
.pagecount
, 1);
888 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
889 "Root/Pages/MediaBox");
890 pdf_to_rect (ctx
, obj
, &rootmediabox
);
893 #ifdef CACHE_PAGEREFS
894 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
895 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
896 if (!state
.pdflut
.objs
) {
897 err (1, "malloc pageobjs %zu %d %zu failed",
898 sizeof (*state
.pdflut
.objs
), cxcount
,
899 sizeof (*state
.pdflut
.objs
) * cxcount
);
901 state
.pdflut
.count
= cxcount
;
902 pdf_load_page_objs (pdf
);
903 state
.pdflut
.pdf
= pdf
;
907 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
914 pdf_obj
*pageref
, *pageobj
;
916 #ifdef CACHE_PAGEREFS
917 pageref
= state
.pdflut
.objs
[pageno
];
919 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
921 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
922 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
924 if (state
.trimmargins
) {
929 page
= pdf_load_page (ctx
, pdf
, pageno
);
930 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
931 trim
= state
.trimanew
|| !obj
;
935 fz_matrix ctm
, page_ctm
;
937 dev
= fz_new_bbox_device (ctx
, &rect
);
938 dev
->hints
|= FZ_IGNORE_SHADE
;
939 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
940 fz_invert_matrix (&ctm
, &page_ctm
);
941 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
942 fz_close_device (ctx
, dev
);
943 fz_drop_device (ctx
, dev
);
945 rect
.x0
+= state
.trimfuzz
.x0
;
946 rect
.x1
+= state
.trimfuzz
.x1
;
947 rect
.y0
+= state
.trimfuzz
.y0
;
948 rect
.y1
+= state
.trimfuzz
.y1
;
949 fz_transform_rect (&rect
, &ctm
);
950 fz_intersect_rect (&rect
, &mediabox
);
952 if (!fz_is_empty_rect (&rect
)) {
956 obj
= pdf_new_array (ctx
, pdf
, 4);
957 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
959 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
961 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
963 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
965 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
968 mediabox
.x0
= pdf_to_real (ctx
,
969 pdf_array_get (ctx
, obj
, 0));
970 mediabox
.y0
= pdf_to_real (ctx
,
971 pdf_array_get (ctx
, obj
, 1));
972 mediabox
.x1
= pdf_to_real (ctx
,
973 pdf_array_get (ctx
, obj
, 2));
974 mediabox
.y1
= pdf_to_real (ctx
,
975 pdf_array_get (ctx
, obj
, 3));
978 fz_drop_page (ctx
, &page
->super
);
979 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
981 printd ("progress %f Trimming %d",
982 (double) (pageno
+ 1) / state
.pagecount
,
987 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
995 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
997 if (fz_is_empty_rect (&mediabox
)) {
1006 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1008 if (!fz_is_empty_rect (&cropbox
)) {
1013 fz_intersect_rect (&mediabox
, &cropbox
);
1018 if (fz_is_empty_rect (&rootmediabox
)) {
1020 "cannot find page size for page %d\n",
1024 mediabox
= rootmediabox
;
1031 if (state
.trimmargins
&& trimw
) {
1035 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1036 fz_bound_page (ctx
, page
, &mediabox
);
1037 if (state
.trimmargins
) {
1041 dev
= fz_new_bbox_device (ctx
, &rect
);
1042 dev
->hints
|= FZ_IGNORE_SHADE
;
1043 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1044 fz_close_device (ctx
, dev
);
1045 fz_drop_device (ctx
, dev
);
1047 rect
.x0
+= state
.trimfuzz
.x0
;
1048 rect
.x1
+= state
.trimfuzz
.x1
;
1049 rect
.y0
+= state
.trimfuzz
.y0
;
1050 rect
.y1
+= state
.trimfuzz
.y1
;
1051 fz_intersect_rect (&rect
, &mediabox
);
1053 if (!fz_is_empty_rect (&rect
)) {
1057 fz_drop_page (ctx
, page
);
1059 printd ("progress %f loading %d",
1060 (double) (pageno
+ 1) / state
.pagecount
,
1067 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1069 err (1, "fwrite trim mediabox");
1075 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1077 err (1, "fread trim mediabox %d", pageno
);
1083 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1084 fz_bound_page (ctx
, page
, &mediabox
);
1085 fz_drop_page (ctx
, page
);
1087 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1089 printd ("progress %f Gathering dimensions %d",
1090 (double) (pageno
) / state
.pagecount
,
1095 fprintf (stderr
, "failed to load page %d\n", pageno
);
1101 if (state
.pagedimcount
== 0
1102 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1103 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1106 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1107 state
.pagedims
= realloc (state
.pagedims
, size
);
1108 if (!state
.pagedims
) {
1109 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1110 size
, state
.pagedimcount
+ 1);
1113 p
= &state
.pagedims
[state
.pagedimcount
++];
1115 p
->mediabox
= mediabox
;
1121 printd ("progress 1 %s %d pages in %f seconds",
1122 state
.trimmargins
? "Trimmed" : "Processed",
1123 state
.pagecount
, end
- start
);
1127 if (fclose (trimf
)) {
1133 static void layout (void)
1138 struct pagedim
*p
= p
;
1139 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1141 if (state
.pagedimcount
== 0) return;
1143 switch (state
.fitmodel
) {
1144 case FitProportional
:
1145 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1148 p
= &state
.pagedims
[pindex
];
1149 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1151 fz_transform_rect (&box
, &rm
);
1153 x0
= fz_min (box
.x0
, box
.x1
);
1154 x1
= fz_max (box
.x0
, box
.x1
);
1157 maxw
= fz_max (w
, maxw
);
1158 zoom
= state
.w
/ maxw
;
1170 ARSERT (0 && state
.fitmodel
);
1173 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1177 p
= &state
.pagedims
[pindex
];
1178 fz_rotate (&ctm
, state
.rotate
);
1179 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1181 fz_transform_rect (&box
, &rm
);
1182 w
= box
.x1
- box
.x0
;
1183 switch (state
.fitmodel
) {
1184 case FitProportional
:
1185 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1191 h
= box
.y1
- box
.y0
;
1193 zoom
= fz_min (zw
, zh
);
1194 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1203 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1204 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1206 fz_rotate (&rm
, p
->rotate
);
1207 p
->pagebox
= p
->mediabox
;
1208 fz_transform_rect (&p
->pagebox
, &rm
);
1209 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1210 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1214 fz_transform_rect (&rect
, &ctm
);
1215 fz_round_rect (&p
->bounds
, &rect
);
1218 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1219 fz_scale (&sm
, zoom
, -zoom
);
1220 fz_concat (&ctm
, &tm
, &sm
);
1226 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1227 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1228 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1229 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1230 int boundw
= x1
- x0
;
1231 int boundh
= y1
- y0
;
1233 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1234 } while (p
-- != state
.pagedims
);
1237 struct pagedim
*pdimofpageno (int pageno
)
1240 struct pagedim
*pdim
= state
.pagedims
;
1242 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1243 if (state
.pagedims
[i
].pageno
> pageno
)
1245 pdim
= &state
.pagedims
[i
];
1251 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1252 uritoanchor (const char *uri
)
1258 a
.n
= fz_resolve_link (state
.ctx
, state
.doc
, uri
, &p
.x
, &p
.y
);
1260 struct pagedim
*pdim
= pdimofpageno (a
.n
);
1261 fz_transform_point (&p
, &pdim
->ctm
);
1264 a
.h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1269 static void recurse_outline (fz_outline
*outline
, int level
)
1272 struct anchor a
= uritoanchor (outline
->uri
);
1274 printd ("o %d %d %d %d %s", level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1277 printd ("on %d %s", level
, outline
->title
);
1279 if (outline
->down
) {
1280 recurse_outline (outline
->down
, level
+ 1);
1282 outline
= outline
->next
;
1286 static void process_outline (void)
1288 fz_outline
*outline
;
1290 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1292 state
.needoutline
= 0;
1293 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1295 recurse_outline (outline
, 0);
1296 fz_drop_outline (state
.ctx
, outline
);
1300 static char *strofspan (fz_stext_span
*span
)
1305 size_t size
= 0, cap
= 80;
1307 p
= malloc (cap
+ 1);
1308 if (!p
) return NULL
;
1310 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1311 int n
= fz_runetochar (utf8
, ch
->c
);
1312 if (size
+ n
> cap
) {
1314 p
= realloc (p
, cap
+ 1);
1315 if (!p
) return NULL
;
1318 memcpy (p
+ size
, utf8
, n
);
1325 static int matchspan (regex_t
*re
, fz_stext_span
*span
,
1326 int stop
, int pageno
, double start
)
1333 fz_point p1
, p2
, p3
, p4
;
1335 p
= strofspan (span
);
1338 ret
= regexec (re
, p
, 1, &rm
, 0);
1341 if (ret
!= REG_NOMATCH
) {
1344 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1345 printd ("msg regexec error `%.*s'",
1346 (int) size
, errbuf
);
1354 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1355 c
+= fz_runelen (span
->text
[a
].c
);
1357 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1358 c
+= fz_runelen (span
->text
[b
].c
);
1361 if (fz_runelen (span
->text
[b
].c
) > 1) {
1362 b
= fz_maxi (0, b
-1);
1365 fz_stext_char_bbox (state
.ctx
, &sb
, span
, a
);
1366 fz_stext_char_bbox (state
.ctx
, &eb
, span
, b
);
1378 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1385 printd ("progress 1 found at %d `%.*s' in %f sec",
1386 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1390 printd ("match %d %d %f %f %f %f %f %f %f %f",
1402 static int compareblocks (const void *l
, const void *r
)
1404 fz_stext_block
const *ls
= l
;
1405 fz_stext_block
const *rs
= r
;
1406 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1409 /* wishful thinking function */
1410 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1414 fz_stext_page
*text
;
1415 fz_stext_sheet
*sheet
;
1416 struct pagedim
*pdim
;
1417 int stop
= 0, niters
= 0;
1422 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1423 if (niters
++ == 5) {
1426 printd ("progress 1 attention requested aborting search at %d",
1431 printd ("progress %f searching in page %d",
1432 (double) (pageno
+ 1) / state
.pagecount
,
1436 pdim
= pdimofpageno (pageno
);
1437 sheet
= fz_new_stext_sheet (state
.ctx
);
1438 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1439 tdev
= fz_new_stext_device (state
.ctx
, sheet
, text
, 0);
1441 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1443 fz_matrix ctm
= pagectm1 (page
, pdim
);
1444 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1447 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1448 fz_close_device (state
.ctx
, tdev
);
1449 fz_drop_device (state
.ctx
, tdev
);
1451 for (j
= 0; j
< text
->len
; ++j
) {
1454 fz_stext_block
*block
;
1456 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1457 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1460 for (k
= 0; k
< block
->len
; ++k
) {
1461 fz_stext_line
*line
;
1462 fz_stext_span
*span
;
1465 line
= &block
->lines
[k
];
1466 if (line
->bbox
.y0
< y
+ 1) continue;
1469 line
= &block
->lines
[block
->len
- 1 - k
];
1470 if (line
->bbox
.y0
> y
- 1) continue;
1473 for (span
= line
->first_span
; span
; span
= span
->next
) {
1474 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1476 case 1: stop
= 1; break;
1477 case -1: stop
= 1; goto endloop
;
1491 fz_drop_stext_page (state
.ctx
, text
);
1492 fz_drop_stext_sheet (state
.ctx
, sheet
);
1493 fz_drop_page (state
.ctx
, page
);
1497 printd ("progress 1 no matches %f sec", end
- start
);
1499 printd ("clearrects");
1502 static void set_tex_params (int colorspace
)
1509 switch (colorspace
) {
1511 state
.texiform
= GL_RGBA8
;
1512 state
.texform
= GL_RGBA
;
1513 state
.texty
= GL_UNSIGNED_BYTE
;
1514 state
.colorspace
= fz_device_rgb (state
.ctx
);
1517 state
.texiform
= GL_RGBA8
;
1518 state
.texform
= GL_BGRA
;
1519 state
.texty
= endianness
.s
> 1
1520 ? GL_UNSIGNED_INT_8_8_8_8
1521 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1522 state
.colorspace
= fz_device_bgr (state
.ctx
);
1525 state
.texiform
= GL_LUMINANCE_ALPHA
;
1526 state
.texform
= GL_LUMINANCE_ALPHA
;
1527 state
.texty
= GL_UNSIGNED_BYTE
;
1528 state
.colorspace
= fz_device_gray (state
.ctx
);
1531 errx (1, "invalid colorspce %d", colorspace
);
1535 static void realloctexts (int texcount
)
1539 if (texcount
== state
.texcount
) return;
1541 if (texcount
< state
.texcount
) {
1542 glDeleteTextures (state
.texcount
- texcount
,
1543 state
.texids
+ texcount
);
1546 size
= texcount
* sizeof (*state
.texids
);
1547 state
.texids
= realloc (state
.texids
, size
);
1548 if (!state
.texids
) {
1549 err (1, "realloc texids %" FMT_s
, size
);
1552 size
= texcount
* sizeof (*state
.texowners
);
1553 state
.texowners
= realloc (state
.texowners
, size
);
1554 if (!state
.texowners
) {
1555 err (1, "realloc texowners %" FMT_s
, size
);
1557 if (texcount
> state
.texcount
) {
1560 glGenTextures (texcount
- state
.texcount
,
1561 state
.texids
+ state
.texcount
);
1562 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1563 state
.texowners
[i
].w
= -1;
1564 state
.texowners
[i
].slice
= NULL
;
1567 state
.texcount
= texcount
;
1571 static char *mbtoutf8 (char *s
)
1577 len
= mbstowcs (NULL
, s
, strlen (s
));
1582 if (len
== (size_t) -1) {
1587 tmp
= malloc (len
* sizeof (wchar_t));
1592 ret
= mbstowcs (tmp
, s
, len
);
1593 if (ret
== (size_t) -1) {
1599 for (i
= 0; i
< ret
; ++i
) {
1600 len
+= fz_runelen (tmp
[i
]);
1603 p
= r
= malloc (len
+ 1);
1609 for (i
= 0; i
< ret
; ++i
) {
1610 p
+= fz_runetochar (p
, tmp
[i
]);
1617 CAMLprim value
ml_mbtoutf8 (value s_v
)
1623 s
= String_val (s_v
);
1629 ret_v
= caml_copy_string (r
);
1635 static void * mainloop (void UNUSED_ATTR
*unused
)
1638 int len
, ret
, oldlen
= 0;
1643 len
= readlen (state
.csock
);
1645 errx (1, "readlen returned 0");
1648 if (oldlen
< len
+ 1) {
1649 p
= realloc (p
, len
+ 1);
1651 err (1, "realloc %d failed", len
+ 1);
1655 readdata (state
.csock
, p
, len
);
1658 if (!strncmp ("open", p
, 4)) {
1659 int wthack
, off
, ok
= 0;
1666 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1668 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1671 filename
= p
+ 5 + off
;
1672 filenamelen
= strlen (filename
);
1673 password
= filename
+ filenamelen
+ 1;
1676 fz_try (state
.ctx
) {
1677 ok
= openxref (filename
, password
);
1679 fz_catch (state
.ctx
) {
1680 utf8filename
= mbtoutf8 (filename
);
1681 printd ("msg Could not open %s", utf8filename
);
1691 utf8filename
= mbtoutf8 (filename
);
1692 printd ("msg Opened %s (press h/F1 to get help)",
1694 if (utf8filename
!= filename
) {
1695 free (utf8filename
);
1698 state
.needoutline
= 1;
1701 else if (!strncmp ("cs", p
, 2)) {
1704 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1706 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1709 set_tex_params (colorspace
);
1710 for (i
= 0; i
< state
.texcount
; ++i
) {
1711 state
.texowners
[i
].w
= -1;
1712 state
.texowners
[i
].slice
= NULL
;
1716 else if (!strncmp ("freepage", p
, 8)) {
1719 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1721 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1725 else if (!strncmp ("freetile", p
, 8)) {
1728 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1730 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1734 else if (!strncmp ("search", p
, 6)) {
1735 int icase
, pageno
, y
, len2
, forward
;
1739 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1740 &icase
, &pageno
, &y
, &forward
, &len2
);
1742 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1745 pattern
= p
+ 6 + len2
;
1746 ret
= regcomp (&re
, pattern
,
1747 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1752 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1753 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1756 search (&re
, pageno
, y
, forward
);
1760 else if (!strncmp ("geometry", p
, 8)) {
1764 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1766 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1774 for (i
= 0; i
< state
.texcount
; ++i
) {
1775 state
.texowners
[i
].slice
= NULL
;
1778 state
.fitmodel
= fitmodel
;
1783 unlock ("geometry");
1784 printd ("continue %d", state
.pagecount
);
1786 else if (!strncmp ("reqlayout", p
, 9)) {
1789 unsigned int fitmodel
;
1793 ret
= sscanf (p
+ 9, " %d %u %d %n",
1794 &rotate
, &fitmodel
, &h
, &off
);
1796 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1799 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1800 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1803 state
.rotate
= rotate
;
1804 state
.fitmodel
= fitmodel
;
1809 nameddest
= p
+ 9 + off
;
1810 if (pdf
&& nameddest
&& *nameddest
) {
1812 struct pagedim
*pdim
;
1813 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1815 pdim
= pdimofpageno (pageno
);
1816 fz_transform_point (&xy
, &pdim
->ctm
);
1817 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1821 unlock ("reqlayout");
1822 printd ("continue %d", state
.pagecount
);
1824 else if (!strncmp ("page", p
, 4)) {
1829 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1831 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1836 page
= loadpage (pageno
, pindex
);
1840 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1842 else if (!strncmp ("tile", p
, 4)) {
1849 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1850 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1851 SCN_ptr_cast (&data
));
1853 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1858 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1862 printd ("tile %d %d %" FMT_ptr
" %u %f",
1864 FMT_ptr_cast (tile
),
1865 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1868 else if (!strncmp ("trimset", p
, 7)) {
1872 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1873 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1875 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1878 state
.trimmargins
= trimmargins
;
1879 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1881 state
.trimfuzz
= fuzz
;
1885 else if (!strncmp ("settrim", p
, 7)) {
1889 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1890 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1892 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1896 state
.trimmargins
= trimmargins
;
1897 state
.needoutline
= 1;
1898 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1900 state
.trimfuzz
= fuzz
;
1902 state
.pagedimcount
= 0;
1903 free (state
.pagedims
);
1904 state
.pagedims
= NULL
;
1909 printd ("continue %d", state
.pagecount
);
1911 else if (!strncmp ("sliceh", p
, 6)) {
1914 ret
= sscanf (p
+ 6, " %d", &h
);
1916 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1918 if (h
!= state
.sliceheight
) {
1921 state
.sliceheight
= h
;
1922 for (i
= 0; i
< state
.texcount
; ++i
) {
1923 state
.texowners
[i
].w
= -1;
1924 state
.texowners
[i
].h
= -1;
1925 state
.texowners
[i
].slice
= NULL
;
1929 else if (!strncmp ("interrupt", p
, 9)) {
1930 printd ("vmsg interrupted");
1933 errx (1, "unknown command %.*s", len
, p
);
1939 CAMLprim value
ml_isexternallink (value uri_v
)
1942 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1943 CAMLreturn (Val_bool (ext
));
1946 CAMLprim value
ml_uritolocation (value uri_v
)
1952 struct pagedim
*pdim
;
1954 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1956 pdim
= pdimofpageno (pageno
);
1957 fz_transform_point (&xy
, &pdim
->ctm
);
1958 ret_v
= caml_alloc_tuple (3);
1959 Field (ret_v
, 0) = Val_int (pageno
);
1960 Field (ret_v
, 1) = caml_copy_double (xy
.x
);
1961 Field (ret_v
, 2) = caml_copy_double (xy
.y
);
1965 CAMLprim value
ml_realloctexts (value texcount_v
)
1967 CAMLparam1 (texcount_v
);
1970 if (trylock (__func__
)) {
1974 realloctexts (Int_val (texcount_v
));
1979 CAMLreturn (Val_bool (ok
));
1982 static void recti (int x0
, int y0
, int x1
, int y1
)
1984 GLfloat
*v
= state
.vertices
;
1986 glVertexPointer (2, GL_FLOAT
, 0, v
);
1987 v
[0] = x0
; v
[1] = y0
;
1988 v
[2] = x1
; v
[3] = y0
;
1989 v
[4] = x0
; v
[5] = y1
;
1990 v
[6] = x1
; v
[7] = y1
;
1991 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1994 static void showsel (struct page
*page
, int ox
, int oy
)
1999 fz_stext_line
*line
;
2000 fz_page_block
*pageb
;
2001 fz_stext_block
*block
;
2002 struct mark first
, last
;
2003 unsigned char selcolor
[] = {15,15,15,140};
2005 first
= page
->fmark
;
2008 if (!first
.span
|| !last
.span
) return;
2010 glEnable (GL_BLEND
);
2011 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2012 glColor4ubv (selcolor
);
2014 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2015 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2016 for (pageb
= page
->text
->blocks
;
2017 pageb
< page
->text
->blocks
+ page
->text
->len
;
2019 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2020 block
= pageb
->u
.text
;
2022 for (line
= block
->lines
;
2023 line
< block
->lines
+ block
->len
;
2025 fz_stext_span
*span
;
2026 rect
= fz_empty_rect
;
2028 for (span
= line
->first_span
; span
; span
= span
->next
) {
2030 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2035 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2037 j
= fz_mini (first
.i
, last
.i
);
2038 k
= fz_maxi (first
.i
, last
.i
);
2041 if (span
== first
.span
) {
2045 else if (span
== last
.span
) {
2052 for (i
= j
; i
<= k
; ++i
) {
2054 fz_union_rect (&rect
,
2055 fz_stext_char_bbox (state
.ctx
, &bbox1
,
2058 fz_round_rect (&bbox
, &rect
);
2059 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2066 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2067 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2068 if (span
== last
.span
) {
2071 rect
= fz_empty_rect
;
2077 glDisable (GL_BLEND
);
2082 static void stipplerect (fz_matrix
*m
,
2090 fz_transform_point (p1
, m
);
2091 fz_transform_point (p2
, m
);
2092 fz_transform_point (p3
, m
);
2093 fz_transform_point (p4
, m
);
2099 t
= hypotf (w
, h
) * .25f
;
2103 s
= hypotf (w
, h
) * .25f
;
2105 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2106 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2108 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2109 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2111 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2112 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2114 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2115 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2117 glDrawArrays (GL_LINES
, 0, 8);
2120 static void solidrect (fz_matrix
*m
,
2127 fz_transform_point (p1
, m
);
2128 fz_transform_point (p2
, m
);
2129 fz_transform_point (p3
, m
);
2130 fz_transform_point (p4
, m
);
2131 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2132 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2134 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2135 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2136 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2139 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2142 fz_matrix ctm
, tm
, pm
;
2143 fz_link
*link
, *links
;
2144 GLfloat
*texcoords
= state
.texcoords
;
2145 GLfloat
*vertices
= state
.vertices
;
2147 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2149 glEnable (GL_TEXTURE_1D
);
2150 glEnable (GL_BLEND
);
2151 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2152 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2154 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2155 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2156 fz_translate (&tm
, xoff
, yoff
);
2157 pm
= pagectm (page
);
2158 fz_concat (&ctm
, &pm
, &tm
);
2160 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2161 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2163 for (link
= links
; link
; link
= link
->next
) {
2164 fz_point p1
, p2
, p3
, p4
;
2166 p1
.x
= link
->rect
.x0
;
2167 p1
.y
= link
->rect
.y0
;
2169 p2
.x
= link
->rect
.x1
;
2170 p2
.y
= link
->rect
.y0
;
2172 p3
.x
= link
->rect
.x1
;
2173 p3
.y
= link
->rect
.y1
;
2175 p4
.x
= link
->rect
.x0
;
2176 p4
.y
= link
->rect
.y1
;
2178 /* TODO: different colours for different schemes */
2179 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
2180 else glColor3ub (255, 0, 0);
2182 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2185 for (i
= 0; i
< page
->annotcount
; ++i
) {
2186 fz_point p1
, p2
, p3
, p4
;
2187 struct annot
*annot
= &page
->annots
[i
];
2189 p1
.x
= annot
->bbox
.x0
;
2190 p1
.y
= annot
->bbox
.y0
;
2192 p2
.x
= annot
->bbox
.x1
;
2193 p2
.y
= annot
->bbox
.y0
;
2195 p3
.x
= annot
->bbox
.x1
;
2196 p3
.y
= annot
->bbox
.y1
;
2198 p4
.x
= annot
->bbox
.x0
;
2199 p4
.y
= annot
->bbox
.y1
;
2201 glColor3ub (0, 0, 128);
2202 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2205 glDisable (GL_BLEND
);
2206 glDisable (GL_TEXTURE_1D
);
2209 static int compareslinks (const void *l
, const void *r
)
2211 struct slink
const *ls
= l
;
2212 struct slink
const *rs
= r
;
2213 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2214 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2216 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2219 static void droptext (struct page
*page
)
2222 fz_drop_stext_page (state
.ctx
, page
->text
);
2225 page
->fmark
.span
= NULL
;
2226 page
->lmark
.span
= NULL
;
2230 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
2235 static void dropannots (struct page
*page
)
2238 free (page
->annots
);
2239 page
->annots
= NULL
;
2240 page
->annotcount
= 0;
2244 static void ensureannots (struct page
*page
)
2247 size_t annotsize
= sizeof (*page
->annots
);
2250 if (state
.gen
!= page
->agen
) {
2252 page
->agen
= state
.gen
;
2254 if (page
->annots
) return;
2256 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2258 annot
= fz_next_annot (state
.ctx
, annot
)) {
2263 page
->annotcount
= count
;
2264 page
->annots
= calloc (count
, annotsize
);
2265 if (!page
->annots
) {
2266 err (1, "calloc annots %d", count
);
2269 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2271 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2274 fz_bound_annot (state
.ctx
, annot
, &rect
);
2275 page
->annots
[i
].annot
= annot
;
2276 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2281 static void dropslinks (struct page
*page
)
2284 free (page
->slinks
);
2285 page
->slinks
= NULL
;
2286 page
->slinkcount
= 0;
2290 static void ensureslinks (struct page
*page
)
2294 size_t slinksize
= sizeof (*page
->slinks
);
2295 fz_link
*link
, *links
;
2297 ensureannots (page
);
2298 if (state
.gen
!= page
->sgen
) {
2300 page
->sgen
= state
.gen
;
2302 if (page
->slinks
) return;
2304 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2305 ctm
= pagectm (page
);
2307 count
= page
->annotcount
;
2308 for (link
= links
; link
; link
= link
->next
) {
2314 page
->slinkcount
= count
;
2315 page
->slinks
= calloc (count
, slinksize
);
2316 if (!page
->slinks
) {
2317 err (1, "calloc slinks %d", count
);
2320 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2324 fz_transform_rect (&rect
, &ctm
);
2325 page
->slinks
[i
].tag
= SLINK
;
2326 page
->slinks
[i
].u
.link
= link
;
2327 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2329 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2331 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2332 fz_transform_rect (&rect
, &ctm
);
2333 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2335 page
->slinks
[i
].tag
= SANNOT
;
2336 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2338 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2342 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2343 static void fmt_linkn (char *s
, unsigned int u
)
2345 unsigned int len
; unsigned int q
;
2346 unsigned int zma
= 'z' - 'a' + 1;
2348 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2351 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2352 /* handles u == 0 */
2357 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2358 int noff
, char *targ
, int tlen
, int hfsize
)
2362 struct slink
*slink
;
2363 double x0
, y0
, x1
, y1
, w
;
2365 ensureslinks (page
);
2366 glColor3ub (0xc3, 0xb0, 0x91);
2367 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2368 fmt_linkn (buf
, i
+ noff
);
2369 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2370 slink
= &page
->slinks
[i
];
2372 x0
= slink
->bbox
.x0
+ xoff
- 5;
2373 y1
= slink
->bbox
.y0
+ yoff
- 5;
2374 y0
= y1
+ 10 + hfsize
;
2375 w
= measure_string (state
.face
, hfsize
, buf
);
2377 recti (x0
, y0
, x1
, y1
);
2381 glEnable (GL_BLEND
);
2382 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2383 glEnable (GL_TEXTURE_2D
);
2384 glColor3ub (0, 0, 0);
2385 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2386 fmt_linkn (buf
, i
+ noff
);
2387 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2388 slink
= &page
->slinks
[i
];
2390 x0
= slink
->bbox
.x0
+ xoff
;
2391 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2392 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2395 glDisable (GL_TEXTURE_2D
);
2396 glDisable (GL_BLEND
);
2399 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2402 struct slice
*slice1
;
2403 unsigned char *texdata
;
2406 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2407 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2409 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2410 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2411 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2415 int texindex
= state
.texindex
++ % state
.texcount
;
2417 if (state
.texowners
[texindex
].w
== tile
->w
) {
2418 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2422 state
.texowners
[texindex
].h
= slice
->h
;
2426 state
.texowners
[texindex
].h
= slice
->h
;
2429 state
.texowners
[texindex
].w
= tile
->w
;
2430 state
.texowners
[texindex
].slice
= slice
;
2431 slice
->texindex
= texindex
;
2433 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2434 #if TEXT_TYPE == GL_TEXTURE_2D
2435 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2436 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2437 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2438 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2441 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2445 texdata
= tile
->pixmap
->samples
;
2448 glTexSubImage2D (TEXT_TYPE
,
2460 glTexImage2D (TEXT_TYPE
,
2472 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2477 CAMLprim value
ml_begintiles (value unit_v
)
2479 CAMLparam1 (unit_v
);
2480 glEnable (TEXT_TYPE
);
2481 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2482 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2483 CAMLreturn (unit_v
);
2486 CAMLprim value
ml_endtiles (value unit_v
)
2488 CAMLparam1 (unit_v
);
2489 glDisable (TEXT_TYPE
);
2490 CAMLreturn (unit_v
);
2493 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2495 CAMLparam2 (args_v
, ptr_v
);
2496 int dispx
= Int_val (Field (args_v
, 0));
2497 int dispy
= Int_val (Field (args_v
, 1));
2498 int dispw
= Int_val (Field (args_v
, 2));
2499 int disph
= Int_val (Field (args_v
, 3));
2500 int tilex
= Int_val (Field (args_v
, 4));
2501 int tiley
= Int_val (Field (args_v
, 5));
2502 char *s
= String_val (ptr_v
);
2503 struct tile
*tile
= parse_pointer (__func__
, s
);
2504 int slicey
, firstslice
;
2505 struct slice
*slice
;
2506 GLfloat
*texcoords
= state
.texcoords
;
2507 GLfloat
*vertices
= state
.vertices
;
2509 firstslice
= tiley
/ tile
->sliceheight
;
2510 slice
= &tile
->slices
[firstslice
];
2511 slicey
= tiley
% tile
->sliceheight
;
2516 dh
= slice
->h
- slicey
;
2517 dh
= fz_mini (disph
, dh
);
2518 uploadslice (tile
, slice
);
2520 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2521 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2522 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2523 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2525 vertices
[0] = dispx
; vertices
[1] = dispy
;
2526 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2527 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2528 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2530 #if TEXT_TYPE == GL_TEXTURE_2D
2531 for (int i
= 0; i
< 8; ++i
) {
2532 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2536 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2540 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2543 CAMLreturn (Val_unit
);
2546 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2548 fz_matrix ctm
, tm
, pm
;
2549 fz_point p1
, p2
, p3
, p4
;
2550 GLfloat
*vertices
= state
.vertices
;
2551 double *v
= (double *) rects_v
;
2553 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2554 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2555 fz_translate (&tm
, xoff
, yoff
);
2556 pm
= pagectm (page
);
2557 fz_concat (&ctm
, &pm
, &tm
);
2559 glEnable (GL_BLEND
);
2560 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2574 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2575 glDisable (GL_BLEND
);
2578 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2579 value xoff_v
, value yoff_v
,
2582 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2583 int xoff
= Int_val (xoff_v
);
2584 int yoff
= Int_val (yoff_v
);
2585 int noff
= Int_val (Field (li_v
, 0));
2586 char *targ
= String_val (Field (li_v
, 1));
2587 int tlen
= caml_string_length (Field (li_v
, 1));
2588 int hfsize
= Int_val (Field (li_v
, 2));
2589 char *s
= String_val (ptr_v
);
2590 int hlmask
= Int_val (hlinks_v
);
2591 struct page
*page
= parse_pointer (__func__
, s
);
2593 if (!page
->fzpage
) {
2594 /* deal with loadpage failed pages */
2598 if (trylock (__func__
)) {
2603 ensureannots (page
);
2604 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2606 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2607 noff
= page
->slinkcount
;
2609 if (page
->tgen
== state
.gen
) {
2610 showsel (page
, xoff
, yoff
);
2615 CAMLreturn (Val_int (noff
));
2618 CAMLprim value
ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2621 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2622 int xoff
= Int_val (xoff_v
);
2623 int yoff
= Int_val (yoff_v
);
2624 char *s
= String_val (ptr_v
);
2625 struct page
*page
= parse_pointer (__func__
, s
);
2627 drawprect (page
, xoff
, yoff
, rects_v
);
2628 CAMLreturn (Val_unit
);
2631 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2636 const fz_matrix
*tctm
;
2637 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2639 if (!page
->annots
) return NULL
;
2642 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2643 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2646 tctm
= &fz_identity
;
2652 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2653 fz_invert_matrix (&ctm
, &ctm
);
2654 fz_transform_point (&p
, &ctm
);
2657 for (i
= 0; i
< page
->annotcount
; ++i
) {
2658 struct annot
*a
= &page
->annots
[i
];
2661 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2662 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2663 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2671 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2675 fz_link
*link
, *links
;
2677 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2682 ctm
= pagectm (page
);
2683 fz_invert_matrix (&ctm
, &ctm
);
2684 fz_transform_point (&p
, &ctm
);
2686 for (link
= links
; link
; link
= link
->next
) {
2687 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2688 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2696 static void ensuretext (struct page
*page
)
2698 if (state
.gen
!= page
->tgen
) {
2700 page
->tgen
= state
.gen
;
2706 page
->text
= fz_new_stext_page (state
.ctx
,
2707 &state
.pagedims
[page
->pdimno
].mediabox
);
2708 page
->sheet
= fz_new_stext_sheet (state
.ctx
);
2709 tdev
= fz_new_stext_device (state
.ctx
, page
->sheet
, page
->text
, 0);
2710 ctm
= pagectm (page
);
2711 fz_run_display_list (state
.ctx
, page
->dlist
,
2712 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2713 qsort (page
->text
->blocks
, page
->text
->len
,
2714 sizeof (*page
->text
->blocks
), compareblocks
);
2715 fz_close_device (state
.ctx
, tdev
);
2716 fz_drop_device (state
.ctx
, tdev
);
2720 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2722 CAMLparam2 (start_page_v
, dir_v
);
2724 int i
, dir
= Int_val (dir_v
);
2725 int start_page
= Int_val (start_page_v
);
2726 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2730 ret_v
= Val_int (0);
2732 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2733 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2738 pdf_page
*page
= NULL
;
2741 fz_try (state
.ctx
) {
2742 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2743 found
= !!page
->links
|| !!page
->annots
;
2745 fz_catch (state
.ctx
) {
2749 fz_drop_page (state
.ctx
, &page
->super
);
2753 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2754 found
= !!fz_load_links (state
.ctx
, page
);
2755 fz_drop_page (state
.ctx
, page
);
2759 ret_v
= caml_alloc_small (1, 1);
2760 Field (ret_v
, 0) = Val_int (i
);
2769 enum { dir_first
, dir_last
};
2770 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2772 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2774 CAMLparam2 (ptr_v
, dir_v
);
2775 CAMLlocal2 (ret_v
, pos_v
);
2777 int dirtag
, i
, slinkindex
;
2778 struct slink
*found
= NULL
,*slink
;
2779 char *s
= String_val (ptr_v
);
2781 page
= parse_pointer (__func__
, s
);
2782 ret_v
= Val_int (0);
2784 ensureslinks (page
);
2786 if (Is_block (dir_v
)) {
2787 dirtag
= Tag_val (dir_v
);
2789 case dir_first_visible
:
2791 int x0
, y0
, dir
, first_index
, last_index
;
2793 pos_v
= Field (dir_v
, 0);
2794 x0
= Int_val (Field (pos_v
, 0));
2795 y0
= Int_val (Field (pos_v
, 1));
2796 dir
= Int_val (Field (pos_v
, 2));
2801 last_index
= page
->slinkcount
;
2804 first_index
= page
->slinkcount
- 1;
2808 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2809 slink
= &page
->slinks
[i
];
2810 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2819 slinkindex
= Int_val (Field (dir_v
, 0));
2820 found
= &page
->slinks
[slinkindex
];
2821 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2822 slink
= &page
->slinks
[i
];
2823 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2831 slinkindex
= Int_val (Field (dir_v
, 0));
2832 found
= &page
->slinks
[slinkindex
];
2833 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2834 slink
= &page
->slinks
[i
];
2835 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2843 slinkindex
= Int_val (Field (dir_v
, 0));
2844 found
= &page
->slinks
[slinkindex
];
2845 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2846 slink
= &page
->slinks
[i
];
2847 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2855 slinkindex
= Int_val (Field (dir_v
, 0));
2856 found
= &page
->slinks
[slinkindex
];
2857 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2858 slink
= &page
->slinks
[i
];
2859 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2868 dirtag
= Int_val (dir_v
);
2871 found
= page
->slinks
;
2876 found
= page
->slinks
+ (page
->slinkcount
- 1);
2882 ret_v
= caml_alloc_small (2, 1);
2883 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2890 enum { uuri
, utext
, uannot
};
2892 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2894 CAMLparam2 (ptr_v
, n_v
);
2895 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2898 char *s
= String_val (ptr_v
);
2899 struct slink
*slink
;
2901 ret_v
= Val_int (0);
2902 page
= parse_pointer (__func__
, s
);
2905 ensureslinks (page
);
2906 slink
= &page
->slinks
[Int_val (n_v
)];
2907 if (slink
->tag
== SLINK
) {
2908 link
= slink
->u
.link
;
2909 str_v
= caml_copy_string (link
->uri
);
2910 ret_v
= caml_alloc_small (1, uuri
);
2911 Field (ret_v
, 0) = str_v
;
2914 ret_v
= caml_alloc_small (1, uannot
);
2915 tup_v
= caml_alloc_tuple (2);
2916 Field (ret_v
, 0) = tup_v
;
2917 Field (tup_v
, 0) = ptr_v
;
2918 Field (tup_v
, 1) = n_v
;
2925 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2927 CAMLparam2 (ptr_v
, n_v
);
2929 const char *contents
= "";
2932 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2934 char *s
= String_val (ptr_v
);
2936 struct slink
*slink
;
2938 page
= parse_pointer (__func__
, s
);
2939 slink
= &page
->slinks
[Int_val (n_v
)];
2940 contents
= pdf_annot_contents (state
.ctx
,
2941 (pdf_annot
*) slink
->u
.annot
);
2944 CAMLreturn (caml_copy_string (contents
));
2947 CAMLprim value
ml_getlinkcount (value ptr_v
)
2951 char *s
= String_val (ptr_v
);
2953 page
= parse_pointer (__func__
, s
);
2954 CAMLreturn (Val_int (page
->slinkcount
));
2957 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2959 CAMLparam2 (ptr_v
, n_v
);
2962 struct slink
*slink
;
2963 char *s
= String_val (ptr_v
);
2965 page
= parse_pointer (__func__
, s
);
2966 ret_v
= caml_alloc_tuple (4);
2968 ensureslinks (page
);
2970 slink
= &page
->slinks
[Int_val (n_v
)];
2971 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2972 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2973 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2974 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2979 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2981 CAMLparam3 (ptr_v
, x_v
, y_v
);
2982 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2984 struct annot
*annot
;
2986 char *ptr
= String_val (ptr_v
);
2987 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2988 struct pagedim
*pdim
;
2990 ret_v
= Val_int (0);
2991 if (trylock (__func__
)) {
2995 page
= parse_pointer (__func__
, ptr
);
2996 pdim
= &state
.pagedims
[page
->pdimno
];
2997 x
+= pdim
->bounds
.x0
;
2998 y
+= pdim
->bounds
.y0
;
3001 annot
= getannot (page
, x
, y
);
3005 ensureslinks (page
);
3006 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3007 if (page
->slinks
[i
].tag
== SANNOT
3008 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3013 ret_v
= caml_alloc_small (1, uannot
);
3014 tup_v
= caml_alloc_tuple (2);
3015 Field (ret_v
, 0) = tup_v
;
3016 Field (tup_v
, 0) = ptr_v
;
3017 Field (tup_v
, 1) = Val_int (n
);
3022 link
= getlink (page
, x
, y
);
3024 str_v
= caml_copy_string (link
->uri
);
3025 ret_v
= caml_alloc_small (1, uuri
);
3026 Field (ret_v
, 0) = str_v
;
3030 fz_page_block
*pageb
;
3031 fz_stext_block
*block
;
3034 for (pageb
= page
->text
->blocks
;
3035 pageb
< page
->text
->blocks
+ page
->text
->len
;
3037 fz_stext_line
*line
;
3038 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3039 block
= pageb
->u
.text
;
3042 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3045 for (line
= block
->lines
;
3046 line
< block
->lines
+ block
->len
;
3048 fz_stext_span
*span
;
3051 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3054 for (span
= line
->first_span
; span
; span
= span
->next
) {
3058 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3061 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3063 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3066 if (x
>= b
->x0
&& x
<= b
->x1
3067 && y
>= b
->y0
&& y
<= b
->y1
) {
3068 fz_stext_style
*style
= span
->text
->style
;
3071 ? fz_font_name (state
.ctx
, style
->font
)
3072 : "Span has no font name"
3074 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3076 if (face
&& face
->family_name
) {
3078 char *n1
= face
->family_name
;
3079 size_t l1
= strlen (n1
);
3080 size_t l2
= strlen (n2
);
3082 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3083 s
= malloc (l1
+ l2
+ 2);
3087 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3088 str_v
= caml_copy_string (s
);
3093 if (str_v
== Val_unit
) {
3094 str_v
= caml_copy_string (n2
);
3096 ret_v
= caml_alloc_small (1, utext
);
3097 Field (ret_v
, 0) = str_v
;
3112 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3114 static int uninteresting (int c
)
3116 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3120 CAMLprim value
ml_clearmark (value ptr_v
)
3123 char *s
= String_val (ptr_v
);
3126 if (trylock (__func__
)) {
3130 page
= parse_pointer (__func__
, s
);
3131 page
->fmark
.span
= NULL
;
3132 page
->lmark
.span
= NULL
;
3138 CAMLreturn (Val_unit
);
3141 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3143 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3147 fz_stext_line
*line
;
3148 fz_page_block
*pageb
;
3149 fz_stext_block
*block
;
3150 struct pagedim
*pdim
;
3151 int mark
= Int_val (mark_v
);
3152 char *s
= String_val (ptr_v
);
3153 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3155 ret_v
= Val_bool (0);
3156 if (trylock (__func__
)) {
3160 page
= parse_pointer (__func__
, s
);
3161 pdim
= &state
.pagedims
[page
->pdimno
];
3165 if (mark
== mark_page
) {
3167 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3169 for (i
= 0; i
< page
->text
->len
; ++i
) {
3170 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3171 pb1
= &page
->text
->blocks
[i
];
3175 if (!pb1
) goto unlock
;
3177 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3178 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3179 pb2
= &page
->text
->blocks
[i
];
3183 if (!pb2
) goto unlock
;
3185 block
= pb1
->u
.text
;
3188 page
->fmark
.span
= block
->lines
->first_span
;
3190 block
= pb2
->u
.text
;
3191 line
= &block
->lines
[block
->len
- 1];
3192 page
->lmark
.i
= line
->last_span
->len
- 1;
3193 page
->lmark
.span
= line
->last_span
;
3194 ret_v
= Val_bool (1);
3198 x
+= pdim
->bounds
.x0
;
3199 y
+= pdim
->bounds
.y0
;
3201 for (pageb
= page
->text
->blocks
;
3202 pageb
< page
->text
->blocks
+ page
->text
->len
;
3204 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3205 block
= pageb
->u
.text
;
3208 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3211 if (mark
== mark_block
) {
3213 page
->fmark
.span
= block
->lines
->first_span
;
3215 line
= &block
->lines
[block
->len
- 1];
3216 page
->lmark
.i
= line
->last_span
->len
- 1;
3217 page
->lmark
.span
= line
->last_span
;
3218 ret_v
= Val_bool (1);
3222 for (line
= block
->lines
;
3223 line
< block
->lines
+ block
->len
;
3225 fz_stext_span
*span
;
3228 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3231 if (mark
== mark_line
) {
3233 page
->fmark
.span
= line
->first_span
;
3235 page
->lmark
.i
= line
->last_span
->len
- 1;
3236 page
->lmark
.span
= line
->last_span
;
3237 ret_v
= Val_bool (1);
3241 for (span
= line
->first_span
; span
; span
= span
->next
) {
3245 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3248 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3250 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3253 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3255 int charnum2
, charnum3
= -1, charnum4
= -1;
3257 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3259 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3260 if (uninteresting (span
->text
[charnum2
].c
)) {
3261 charnum3
= charnum2
+ 1;
3265 if (charnum3
== -1) charnum3
= 0;
3268 for (charnum2
= charnum
+ 1;
3269 charnum2
< span
->len
;
3271 if (uninteresting (span
->text
[charnum2
].c
)) break;
3272 charnum4
= charnum2
;
3275 page
->fmark
.i
= charnum3
;
3276 page
->fmark
.span
= span
;
3278 page
->lmark
.i
= charnum4
;
3279 page
->lmark
.span
= span
;
3280 ret_v
= Val_bool (1);
3288 if (!Bool_val (ret_v
)) {
3289 page
->fmark
.span
= NULL
;
3290 page
->lmark
.span
= NULL
;
3300 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3302 CAMLparam3 (ptr_v
, x_v
, y_v
);
3303 CAMLlocal2 (ret_v
, res_v
);
3306 fz_page_block
*pageb
;
3307 struct pagedim
*pdim
;
3308 char *s
= String_val (ptr_v
);
3309 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3311 ret_v
= Val_int (0);
3312 if (trylock (__func__
)) {
3316 page
= parse_pointer (__func__
, s
);
3317 pdim
= &state
.pagedims
[page
->pdimno
];
3318 x
+= pdim
->bounds
.x0
;
3319 y
+= pdim
->bounds
.y0
;
3323 for (pageb
= page
->text
->blocks
;
3324 pageb
< page
->text
->blocks
+ page
->text
->len
;
3326 switch (pageb
->type
) {
3327 case FZ_PAGE_BLOCK_TEXT
:
3328 b
= &pageb
->u
.text
->bbox
;
3331 case FZ_PAGE_BLOCK_IMAGE
:
3332 b
= &pageb
->u
.image
->bbox
;
3339 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3344 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3345 ret_v
= caml_alloc_small (1, 1);
3346 Store_double_field (res_v
, 0, b
->x0
);
3347 Store_double_field (res_v
, 1, b
->x1
);
3348 Store_double_field (res_v
, 2, b
->y0
);
3349 Store_double_field (res_v
, 3, b
->y1
);
3350 Field (ret_v
, 0) = res_v
;
3358 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3360 CAMLparam2 (ptr_v
, rect_v
);
3363 struct pagedim
*pdim
;
3364 char *s
= String_val (ptr_v
);
3365 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3366 fz_stext_line
*line
;
3367 fz_page_block
*pageb
;
3368 fz_stext_block
*block
;
3369 fz_stext_span
*span
, *fspan
, *lspan
;
3371 if (trylock (__func__
)) {
3375 page
= parse_pointer (__func__
, s
);
3378 pdim
= &state
.pagedims
[page
->pdimno
];
3379 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3380 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3381 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3382 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3393 fspan
= page
->fmark
.span
;
3396 lspan
= page
->lmark
.span
;
3398 for (pageb
= page
->text
->blocks
;
3399 pageb
< page
->text
->blocks
+ page
->text
->len
;
3401 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3402 block
= pageb
->u
.text
;
3403 for (line
= block
->lines
;
3404 line
< block
->lines
+ block
->len
;
3407 for (span
= line
->first_span
; span
; span
= span
->next
) {
3408 for (i
= 0; i
< span
->len
; ++i
) {
3409 fz_stext_char_bbox (state
.ctx
, &b
, span
, i
);
3411 if (x0
>= b
.x0
&& x0
<= b
.x1
3412 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3416 if (x1
>= b
.x0
&& x1
<= b
.x1
3417 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3425 if (x1
< x0
&& fspan
== lspan
) {
3437 page
->fmark
.span
= fspan
;
3440 page
->lmark
.span
= lspan
;
3445 CAMLreturn (Val_unit
);
3448 static int UNUSED_ATTR
pipespan (FILE *f
, fz_stext_span
*span
, int a
, int b
)
3453 for (i
= a
; i
<= b
; ++i
) {
3454 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3455 ret
= fwrite (buf
, len
, 1, f
);
3458 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3459 len
, ret
, strerror (errno
));
3467 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3469 caml_failwith ("ml_popen not implemented under Cygwin");
3472 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3474 CAMLparam2 (command_v
, fds_v
);
3475 CAMLlocal2 (l_v
, tup_v
);
3479 value earg_v
= Nothing
;
3480 posix_spawnattr_t attr
;
3481 posix_spawn_file_actions_t fa
;
3482 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3484 argv
[2] = String_val (command_v
);
3486 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3487 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3490 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3491 msg
= "posix_spawnattr_init";
3495 #ifdef POSIX_SPAWN_USEVFORK
3496 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3497 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3502 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3505 tup_v
= Field (l_v
, 0);
3506 fd1
= Int_val (Field (tup_v
, 0));
3507 fd2
= Int_val (Field (tup_v
, 1));
3509 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3510 msg
= "posix_spawn_file_actions_addclose";
3516 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3517 msg
= "posix_spawn_file_actions_adddup2";
3524 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3525 msg
= "posix_spawn";
3530 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3531 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3535 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3536 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3541 unix_error (ret
, msg
, earg_v
);
3543 CAMLreturn (Val_int (pid
));
3547 CAMLprim value
ml_hassel (value ptr_v
)
3552 char *s
= String_val (ptr_v
);
3554 ret_v
= Val_bool (0);
3555 if (trylock (__func__
)) {
3559 page
= parse_pointer (__func__
, s
);
3560 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3566 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3568 CAMLparam2 (fd_v
, ptr_v
);
3572 fz_stext_line
*line
;
3573 fz_page_block
*pageb
;
3574 fz_stext_block
*block
;
3575 int fd
= Int_val (fd_v
);
3576 char *s
= String_val (ptr_v
);
3578 if (trylock (__func__
)) {
3582 page
= parse_pointer (__func__
, s
);
3584 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3585 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3589 f
= fdopen (fd
, "w");
3591 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3592 fd
, strerror (errno
));
3596 for (pageb
= page
->text
->blocks
;
3597 pageb
< page
->text
->blocks
+ page
->text
->len
;
3599 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3600 block
= pageb
->u
.text
;
3601 for (line
= block
->lines
;
3602 line
< block
->lines
+ block
->len
;
3604 fz_stext_span
*span
;
3606 for (span
= line
->first_span
; span
; span
= span
->next
) {
3609 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3610 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3611 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3614 if (pipespan (f
, span
, a
, b
)) {
3617 if (span
== page
->lmark
.span
) {
3620 if (span
== line
->last_span
) {
3621 if (putc ('\n', f
) == EOF
) {
3623 "failed break line on sel pipe: %s\n",
3634 int ret
= fclose (f
);
3637 if (errno
!= ECHILD
) {
3638 fprintf (stderr
, "failed to close sel pipe: %s\n",
3649 fprintf (stderr
, "failed to close sel pipe: %s\n",
3653 CAMLreturn (Val_unit
);
3656 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3658 CAMLparam1 (pagedimno_v
);
3660 int pagedimno
= Int_val (pagedimno_v
);
3663 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3664 if (trylock (__func__
)) {
3665 box
= fz_empty_rect
;
3668 box
= state
.pagedims
[pagedimno
].mediabox
;
3672 Store_double_field (ret_v
, 0, box
.x0
);
3673 Store_double_field (ret_v
, 1, box
.x1
);
3674 Store_double_field (ret_v
, 2, box
.y0
);
3675 Store_double_field (ret_v
, 3, box
.y1
);
3680 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3681 value dw_v
, value cols_v
)
3683 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3689 double winw
= Int_val (winw_v
);
3690 double winh
= Int_val (winh_v
);
3691 double dw
= Int_val (dw_v
);
3692 double cols
= Int_val (cols_v
);
3693 double pw
= 1.0, ph
= 1.0;
3695 if (trylock (__func__
)) {
3699 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3700 double w
= p
->pagebox
.x1
/ cols
;
3701 double h
= p
->pagebox
.y1
;
3705 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3707 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3710 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3713 ret_v
= caml_copy_double (zoom
);
3717 CAMLprim value
ml_getmaxw (value unit_v
)
3719 CAMLparam1 (unit_v
);
3725 if (trylock (__func__
)) {
3729 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3730 double w
= p
->pagebox
.x1
;
3731 maxw
= fz_max (maxw
, w
);
3736 ret_v
= caml_copy_double (maxw
);
3740 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3742 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3744 int pt
= Int_val(pt_v
);
3745 int x
= Int_val (x_v
);
3746 int y
= Int_val (y_v
);
3749 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3750 ret_v
= caml_copy_double (w
);
3754 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3756 CAMLparam2 (pt_v
, string_v
);
3758 int pt
= Int_val (pt_v
);
3761 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3762 ret_v
= caml_copy_double (w
);
3766 CAMLprim value
ml_getpagebox (value opaque_v
)
3768 CAMLparam1 (opaque_v
);
3774 char *s
= String_val (opaque_v
);
3775 struct page
*page
= parse_pointer (__func__
, s
);
3777 ret_v
= caml_alloc_tuple (4);
3778 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3779 dev
->hints
|= FZ_IGNORE_SHADE
;
3781 ctm
= pagectm (page
);
3782 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3784 fz_close_device (state
.ctx
, dev
);
3785 fz_drop_device (state
.ctx
, dev
);
3786 fz_round_rect (&bbox
, &rect
);
3787 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3788 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3789 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3790 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3795 CAMLprim value
ml_setaalevel (value level_v
)
3797 CAMLparam1 (level_v
);
3799 state
.aalevel
= Int_val (level_v
);
3800 CAMLreturn (Val_unit
);
3803 #pragma GCC diagnostic push
3804 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3805 #include <X11/Xlib.h>
3806 #include <X11/cursorfont.h>
3807 #pragma GCC diagnostic pop
3810 #include <EGL/egl.h>
3815 static const int shapes
[] = {
3816 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3819 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3832 XVisualInfo
*visual
;
3833 Cursor curs
[CURS_COUNT
];
3837 static VisualID
initvisual (void)
3839 /* On this system with: `Haswell-ULT Integrated Graphics
3840 Controller' and Mesa 11.0.6; perf stat reports [1] that when
3841 using glX chosen visual and auto scrolling some document in
3842 fullscreen the power/energy-gpu is more than 1 joule bigger
3843 than when using hand picked visual that stands alone in glxinfo
3844 output: it's dead last in the list and it's the only one with
3845 `visual dep' (sic) of 32
3847 No clue what's going on here...
3849 [1] perf stat -a -I 1200 -e "power/energy-gpu/"
3855 info
.class = TrueColor
;
3856 glx
.visual
= XGetVisualInfo (glx
.dpy
, VisualDepthMask
| VisualClassMask
,
3858 if (!ret
|| !glx
.visual
) {
3859 XCloseDisplay (glx
.dpy
);
3860 caml_failwith ("XGetVisualInfo");
3862 return glx
.visual
->visualid
;
3866 static void initcurs (void)
3868 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3869 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3873 CAMLprim
void ml_setbgcol (value color_v
)
3875 CAMLparam1 (color_v
);
3876 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3881 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3883 CAMLparam3 (display_v
, wid_v
, screen_v
);
3887 EGLint attribs
[] = {
3889 EGL_NATIVE_VISUAL_ID
, 0,
3893 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3894 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3899 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3901 caml_failwith ("XOpenDisplay");
3904 eglBindAPI (EGL_OPENGL_API
);
3906 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3907 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3908 caml_failwith ("eglGetDisplay");
3911 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3912 caml_failwith ("eglInitialize");
3916 attribs
[1] = visid
= initvisual ();
3919 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3921 caml_failwith ("eglChooseConfig");
3926 if (!eglGetConfigAttrib (glx
.edpy
, glx
.conf
,
3927 EGL_NATIVE_VISUAL_ID
, &visid
)) {
3928 caml_failwith ("eglGetConfigAttrib");
3933 glx
.wid
= Int_val (wid_v
);
3934 CAMLreturn (Val_int (visid
));
3937 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3939 CAMLparam1 (unit_v
);
3941 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3943 caml_failwith ("eglCreateContext");
3946 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3948 if (glx
.win
== EGL_NO_SURFACE
) {
3949 caml_failwith ("eglCreateWindowSurface");
3953 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3955 caml_failwith ("eglMakeCurrent");
3957 CAMLreturn (Val_unit
);
3960 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3962 CAMLparam3 (display_v
, wid_v
, screen_v
);
3964 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3966 caml_failwith ("XOpenDisplay");
3972 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3973 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3975 XCloseDisplay (glx
.dpy
);
3976 caml_failwith ("glXChooseVisual");
3981 glx
.wid
= Int_val (wid_v
);
3982 CAMLreturn (Val_int (glx
.visual
->visualid
));
3985 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3987 CAMLparam1 (unit_v
);
3989 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3991 caml_failwith ("glXCreateContext");
3997 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3998 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4000 caml_failwith ("glXMakeCurrent");
4002 CAMLreturn (Val_unit
);
4006 CAMLprim value
ml_setcursor (value cursor_v
)
4008 CAMLparam1 (cursor_v
);
4009 size_t cursn
= Int_val (cursor_v
);
4011 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
4012 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
4014 CAMLreturn (Val_unit
);
4017 CAMLprim value
ml_swapb (value unit_v
)
4019 CAMLparam1 (unit_v
);
4021 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
4022 caml_failwith ("eglSwapBuffers");
4025 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4027 CAMLreturn (Val_unit
);
4030 #include "keysym2ucs.c"
4032 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4034 CAMLparam1 (keysym_v
);
4036 KeySym keysym
= Int_val (keysym_v
);
4041 rune
= keysym2ucs (keysym
);
4042 len
= fz_runetochar (buf
, rune
);
4044 str_v
= caml_copy_string (buf
);
4048 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4050 CAMLprim value
ml_platform (value unit_v
)
4052 CAMLparam1 (unit_v
);
4053 CAMLlocal2 (tup_v
, arr_v
);
4054 int platid
= piunknown
;
4057 #if defined __linux__
4059 #elif defined __CYGWIN__
4061 #elif defined __DragonFly__ || defined __FreeBSD__
4062 || defined __OpenBSD__
|| defined __NetBSD__
4064 #elif defined __sun__
4066 #elif defined __APPLE__
4069 if (uname (&buf
)) err (1, "uname");
4071 tup_v
= caml_alloc_tuple (2);
4073 char const *sar
[] = {
4080 arr_v
= caml_copy_string_array (sar
);
4082 Field (tup_v
, 0) = Val_int (platid
);
4083 Field (tup_v
, 1) = arr_v
;
4087 CAMLprim value
ml_cloexec (value fd_v
)
4090 int fd
= Int_val (fd_v
);
4092 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4093 uerror ("fcntl", Nothing
);
4095 CAMLreturn (Val_unit
);
4098 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4100 CAMLparam2 (w_v
, h_v
);
4103 int w
= Int_val (w_v
);
4104 int h
= Int_val (h_v
);
4105 int cs
= Int_val (cs_v
);
4107 if (state
.bo_usable
) {
4108 pbo
= calloc (sizeof (*pbo
), 1);
4110 err (1, "calloc pbo");
4122 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
4125 state
.glGenBuffersARB (1, &pbo
->id
);
4126 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4127 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4128 NULL
, GL_STREAM_DRAW
);
4129 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4131 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4133 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4134 state
.glDeleteBuffersARB (1, &pbo
->id
);
4136 ret_v
= caml_copy_string ("0");
4142 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4144 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4148 err (1, "malloc %d bytes failed", res
+1);
4150 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4152 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4154 ret_v
= caml_copy_string (s
);
4159 ret_v
= caml_copy_string ("0");
4164 CAMLprim value
ml_freepbo (value s_v
)
4167 char *s
= String_val (s_v
);
4168 struct tile
*tile
= parse_pointer (__func__
, s
);
4171 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4173 tile
->pbo
->ptr
= NULL
;
4174 tile
->pbo
->size
= -1;
4176 CAMLreturn (Val_unit
);
4179 CAMLprim value
ml_unmappbo (value s_v
)
4182 char *s
= String_val (s_v
);
4183 struct tile
*tile
= parse_pointer (__func__
, s
);
4186 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4187 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4188 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4190 tile
->pbo
->ptr
= NULL
;
4191 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4193 CAMLreturn (Val_unit
);
4196 static void setuppbo (void)
4199 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
4201 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4203 state
.bo_usable
= GGPA (glBindBufferARB
)
4204 && GGPA (glUnmapBufferARB
)
4205 && GGPA (glMapBufferARB
)
4206 && GGPA (glBufferDataARB
)
4207 && GGPA (glGenBuffersARB
)
4208 && GGPA (glDeleteBuffersARB
);
4212 CAMLprim value
ml_bo_usable (value unit_v
)
4214 CAMLparam1 (unit_v
);
4215 CAMLreturn (Val_bool (state
.bo_usable
));
4218 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4220 CAMLparam3 (ptr_v
, x_v
, y_v
);
4221 CAMLlocal2 (ret_v
, tup_v
);
4223 char *s
= String_val (ptr_v
);
4224 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4225 struct pagedim
*pdim
;
4229 page
= parse_pointer (__func__
, s
);
4230 pdim
= &state
.pagedims
[page
->pdimno
];
4232 ret_v
= Val_int (0);
4233 if (trylock (__func__
)) {
4237 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4238 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4239 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4244 p
.x
= x
+ pdim
->bounds
.x0
;
4245 p
.y
= y
+ pdim
->bounds
.y0
;
4247 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4248 fz_invert_matrix (&ctm
, &ctm
);
4249 fz_transform_point (&p
, &ctm
);
4251 tup_v
= caml_alloc_tuple (2);
4252 ret_v
= caml_alloc_small (1, 1);
4253 Field (tup_v
, 0) = Val_int (p
.x
);
4254 Field (tup_v
, 1) = Val_int (p
.y
);
4255 Field (ret_v
, 0) = tup_v
;
4262 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4263 value x_v
, value y_v
)
4265 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4268 char *s
= String_val (ptr_v
);
4269 int pageno
= Int_val (pageno_v
);
4270 int pdimno
= Int_val (pdimno_v
);
4271 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4272 struct pagedim
*pdim
;
4276 ret_v
= Val_int (0);
4280 page
= loadpage (pageno
, pdimno
);
4283 page
= parse_pointer (__func__
, s
);
4285 pdim
= &state
.pagedims
[pdimno
];
4287 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4288 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4289 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4294 p
.x
= x
+ pdim
->bounds
.x0
;
4295 p
.y
= y
+ pdim
->bounds
.y0
;
4297 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4298 fz_transform_point (&p
, &ctm
);
4300 ret_v
= caml_alloc_tuple (2);
4301 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4302 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4311 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4314 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4315 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4321 char *s
= String_val (ptr_v
);
4323 page
= parse_pointer (__func__
, s
);
4324 annot
= pdf_create_annot (state
.ctx
,
4325 pdf_page_from_fz_page (state
.ctx
,
4328 p
.x
= Int_val (x_v
);
4329 p
.y
= Int_val (y_v
);
4330 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4331 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4334 CAMLreturn (Val_unit
);
4337 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4339 CAMLparam2 (ptr_v
, n_v
);
4340 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4344 char *s
= String_val (ptr_v
);
4345 struct slink
*slink
;
4347 page
= parse_pointer (__func__
, s
);
4348 slink
= &page
->slinks
[Int_val (n_v
)];
4349 pdf_delete_annot (state
.ctx
,
4350 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4351 (pdf_annot
*) slink
->u
.annot
);
4354 CAMLreturn (Val_unit
);
4357 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4359 CAMLparam3 (ptr_v
, n_v
, str_v
);
4360 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4364 char *s
= String_val (ptr_v
);
4365 struct slink
*slink
;
4367 page
= parse_pointer (__func__
, s
);
4368 slink
= &page
->slinks
[Int_val (n_v
)];
4369 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4370 String_val (str_v
));
4373 CAMLreturn (Val_unit
);
4376 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4378 CAMLparam1 (unit_v
);
4379 CAMLreturn (Val_bool (state
.dirty
));
4382 CAMLprim value
ml_savedoc (value path_v
)
4384 CAMLparam1 (path_v
);
4385 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4388 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4390 CAMLreturn (Val_unit
);
4393 static void makestippletex (void)
4395 const char pixels
[] = "\xff\xff\0\0";
4396 glGenTextures (1, &state
.stid
);
4397 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4398 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4399 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4412 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4414 return caml_copy_string (FZ_VERSION
);
4417 #ifdef USE_FONTCONFIG
4423 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4427 int UNUSED_ATTR needs_exact_metrics
)
4434 FcPattern
*pat
, *pat1
;
4436 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4437 name
, bold
, italic
, needs_exact_metrics
);
4440 fc
.config
= FcInitLoadConfigAndFonts ();
4442 lprintf ("FcInitLoadConfigAndFonts failed\n");
4446 if (!fc
.config
) return NULL
;
4448 size
= strlen (name
);
4449 if (bold
) size
+= sizeof (":bold") - 1;
4450 if (italic
) size
+= sizeof (":italic") - 1;
4453 buf
= malloc (size
);
4455 err (1, "malloc %zu failed", size
);
4459 if (bold
&& italic
) {
4460 strcat (buf
, ":bold:italic");
4463 if (bold
) strcat (buf
, ":bold");
4464 if (italic
) strcat (buf
, ":italic");
4466 for (i
= 0; i
< size
; ++i
) {
4467 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4470 lprintf ("fcbuf=%s\n", buf
);
4471 pat
= FcNameParse ((FcChar8
*) buf
);
4473 printd ("emsg FcNameParse failed\n");
4478 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4479 printd ("emsg FcConfigSubstitute failed\n");
4483 FcDefaultSubstitute (pat
);
4485 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4487 printd ("emsg FcFontMatch failed\n");
4488 FcPatternDestroy (pat
);
4493 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4494 printd ("emsg FcPatternGetString failed\n");
4495 FcPatternDestroy (pat
);
4496 FcPatternDestroy (pat1
);
4502 printd ("emsg name=%s path=%s\n", name
, path
);
4504 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4505 FcPatternDestroy (pat
);
4506 FcPatternDestroy (pat1
);
4512 CAMLprim value
ml_init (value csock_v
, value params_v
)
4514 CAMLparam2 (csock_v
, params_v
);
4515 CAMLlocal2 (trim_v
, fuzz_v
);
4523 state
.csock
= Int_val (csock_v
);
4524 state
.rotate
= Int_val (Field (params_v
, 0));
4525 state
.fitmodel
= Int_val (Field (params_v
, 1));
4526 trim_v
= Field (params_v
, 2);
4527 texcount
= Int_val (Field (params_v
, 3));
4528 state
.sliceheight
= Int_val (Field (params_v
, 4));
4529 mustoresize
= Int_val (Field (params_v
, 5));
4530 colorspace
= Int_val (Field (params_v
, 6));
4531 fontpath
= String_val (Field (params_v
, 7));
4533 if (caml_string_length (Field (params_v
, 8)) > 0) {
4534 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4536 if (!state
.trimcachepath
) {
4537 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4541 haspboext
= Bool_val (Field (params_v
, 9));
4543 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4544 fz_register_document_handlers (state
.ctx
);
4546 #ifdef USE_FONTCONFIG
4547 if (Bool_val (Field (params_v
, 10))) {
4548 fz_install_load_system_font_funcs (
4549 state
.ctx
, fc_load_system_font_func
, NULL
4554 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4555 fuzz_v
= Field (trim_v
, 1);
4556 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4557 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4558 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4559 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4561 set_tex_params (colorspace
);
4564 #ifndef USE_FONTCONFIG
4565 state
.face
= load_font (fontpath
);
4569 char *buf
= fontpath
;
4570 FcPattern
*pat
, *pat1
;
4573 fc
.config
= FcInitLoadConfigAndFonts ();
4575 errx (1, "FcInitLoadConfigAndFonts failed");
4578 pat
= FcNameParse ((FcChar8
*) buf
);
4580 errx (1, "FcNameParse failed");
4583 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4584 errx (1, "FcConfigSubstitute failed");
4586 FcDefaultSubstitute (pat
);
4588 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4590 errx (1, "FcFontMatch failed");
4593 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4594 errx (1, "FcPatternGetString failed");
4597 state
.face
= load_font ((char *) path
);
4598 FcPatternDestroy (pat
);
4599 FcPatternDestroy (pat1
);
4604 const char *data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0,
4606 state
.face
= load_builtin_font (data
, len
);
4608 if (!state
.face
) _exit (1);
4610 realloctexts (texcount
);
4618 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4620 errx (1, "pthread_create: %s", strerror (ret
));
4623 CAMLreturn (Val_unit
);