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 state
.doc
= fz_open_document (state
.ctx
, filename
);
492 if (fz_needs_password (state
.ctx
, state
.doc
)) {
493 if (password
&& !*password
) {
498 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
500 printd ("pass fail");
505 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
509 static void pdfinfo (void)
511 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
515 printd ("info PDF version\t%d.%d",
516 pdf
->version
/ 10, pdf
->version
% 10);
518 infoobj
= pdf_dict_gets (state
.ctx
, pdf_trailer (state
.ctx
,
523 char *items
[] = { "Title", "Author", "Creator",
524 "Producer", "CreationDate" };
526 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
527 pdf_obj
*obj
= pdf_dict_gets (state
.ctx
, infoobj
, items
[i
]);
528 s
= pdf_to_utf8 (state
.ctx
, obj
);
529 if (*s
) printd ("info %s\t%s", items
[i
], s
);
530 fz_free (state
.ctx
, s
);
537 static void unlinktile (struct tile
*tile
)
541 for (i
= 0; i
< tile
->slicecount
; ++i
) {
542 struct slice
*s
= &tile
->slices
[i
];
544 if (s
->texindex
!= -1) {
545 if (state
.texowners
[s
->texindex
].slice
== s
) {
546 state
.texowners
[s
->texindex
].slice
= NULL
;
552 static void freepage (struct page
*page
)
556 fz_drop_stext_page (state
.ctx
, page
->text
);
559 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
564 fz_drop_display_list (state
.ctx
, page
->dlist
);
565 fz_drop_page (state
.ctx
, page
->fzpage
);
569 static void freetile (struct tile
*tile
)
574 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
577 fz_drop_pixmap (state
.ctx
, state
.pig
);
579 state
.pig
= tile
->pixmap
;
584 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
593 static int cacheline32bytes
;
595 static void __attribute__ ((constructor
)) clcheck (void)
597 char **envp
= environ
;
602 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
604 cacheline32bytes
= auxv
[1] == 32;
610 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
612 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
613 if (cacheline32bytes
&& size
> 32) {
614 intptr_t a1
, a2
, diff
;
616 vector
unsigned char v
= vec_splat_u8 (-1);
617 vector
unsigned char *p
;
619 a1
= a2
= (intptr_t) pixmap
->samples
;
620 a2
= (a1
+ 31) & ~31;
625 while (a1
!= a2
) *(char *) a1
++ = 0xff;
626 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
627 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
629 vec_st (v
, i
+ 16, p
);
631 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
633 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
636 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
639 static void trimctm (pdf_page
*page
, int pindex
)
642 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
645 if (!pdim
->tctmready
) {
646 fz_rect realbox
, mediabox
;
647 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
649 fz_rotate (&rm
, -pdim
->rotate
);
650 fz_scale (&sm
, 1, -1);
651 fz_concat (&ctm
, &rm
, &sm
);
652 realbox
= pdim
->mediabox
;
653 fz_transform_rect (&realbox
, &ctm
);
654 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
655 fz_concat (&ctm1
, &ctm
, &tm
);
656 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
657 fz_invert_matrix (&im
, &page_ctm
);
658 fz_concat (&ctm
, &im
, &ctm1
);
664 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
667 int pdimno
= pdim
- state
.pagedims
;
669 if (pdf_specifics (state
.ctx
, state
.doc
)) {
670 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), pdimno
);
671 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
674 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
675 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
680 static fz_matrix
pagectm (struct page
*page
)
682 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
685 static void *loadpage (int pageno
, int pindex
)
690 page
= calloc (sizeof (struct page
), 1);
692 err (1, "calloc page %d", pageno
);
695 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
696 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
698 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
699 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
702 fz_catch (state
.ctx
) {
705 fz_close_device (state
.ctx
, dev
);
706 fz_drop_device (state
.ctx
, dev
);
708 page
->pdimno
= pindex
;
709 page
->pageno
= pageno
;
710 page
->sgen
= state
.gen
;
711 page
->agen
= state
.gen
;
712 page
->tgen
= state
.gen
;
716 static struct tile
*alloctile (int h
)
723 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
724 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
725 tile
= calloc (tilesize
, 1);
727 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
729 for (i
= 0; i
< slicecount
; ++i
) {
730 int sh
= fz_mini (h
, state
.sliceheight
);
731 tile
->slices
[i
].h
= sh
;
732 tile
->slices
[i
].texindex
= -1;
735 tile
->slicecount
= slicecount
;
736 tile
->sliceheight
= state
.sliceheight
;
740 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
748 struct pagedim
*pdim
;
750 tile
= alloctile (h
);
751 pdim
= &state
.pagedims
[page
->pdimno
];
756 bbox
.x1
= bbox
.x0
+ w
;
757 bbox
.y1
= bbox
.y0
+ h
;
760 if (state
.pig
->w
== w
762 && state
.pig
->colorspace
== state
.colorspace
) {
763 tile
->pixmap
= state
.pig
;
764 tile
->pixmap
->x
= bbox
.x0
;
765 tile
->pixmap
->y
= bbox
.y0
;
768 fz_drop_pixmap (state
.ctx
, state
.pig
);
775 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
781 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
, 1);
787 clearpixmap (tile
->pixmap
);
789 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
790 ctm
= pagectm (page
);
791 fz_rect_from_irect (&rect
, &bbox
);
792 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
793 fz_close_device (state
.ctx
, dev
);
794 fz_drop_device (state
.ctx
, dev
);
799 #ifdef CACHE_PAGEREFS
800 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
801 thanks to Robin Watts */
803 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
805 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
809 if (state
.pdflut
.idx
== state
.pagecount
) return;
811 kids
= pdf_dict_gets (ctx
, node
, "Kids");
812 len
= pdf_array_len (ctx
, kids
);
815 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
817 if (pdf_mark_obj (ctx
, node
))
818 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
819 for (i
= 0; i
< len
; i
++) {
820 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
821 char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
823 ? !strcmp (type
, "Pages")
824 : pdf_dict_gets (ctx
, kid
, "Kids")
825 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
826 pdf_collect_pages (doc
, kid
);
830 ? strcmp (type
, "Page") != 0
831 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
832 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
833 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
836 pdf_unmark_obj (ctx
, node
);
840 pdf_load_page_objs (pdf_document
*doc
)
842 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
843 pdf_trailer (state
.ctx
, doc
), "Root");
844 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
847 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
849 state
.pdflut
.idx
= 0;
850 pdf_collect_pages (doc
, node
);
854 static void initpdims (int wthack
)
858 fz_rect rootmediabox
;
859 int pageno
, trim
, show
;
860 int trimw
= 0, cxcount
;
861 fz_context
*ctx
= state
.ctx
;
862 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
869 if (state
.trimmargins
&& state
.trimcachepath
) {
870 trimf
= fopen (state
.trimcachepath
, "rb");
872 trimf
= fopen (state
.trimcachepath
, "wb");
877 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
878 cxcount
= state
.pagecount
;
880 cxcount
= fz_mini (state
.pagecount
, 1);
884 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
885 "Root/Pages/MediaBox");
886 pdf_to_rect (ctx
, obj
, &rootmediabox
);
889 #ifdef CACHE_PAGEREFS
890 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
891 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
892 if (!state
.pdflut
.objs
) {
893 err (1, "malloc pageobjs %zu %d %zu failed",
894 sizeof (*state
.pdflut
.objs
), cxcount
,
895 sizeof (*state
.pdflut
.objs
) * cxcount
);
897 state
.pdflut
.count
= cxcount
;
898 pdf_load_page_objs (pdf
);
899 state
.pdflut
.pdf
= pdf
;
903 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
910 pdf_obj
*pageref
, *pageobj
;
912 #ifdef CACHE_PAGEREFS
913 pageref
= state
.pdflut
.objs
[pageno
];
915 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
917 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
918 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
920 if (state
.trimmargins
) {
925 page
= pdf_load_page (ctx
, pdf
, pageno
);
926 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
927 trim
= state
.trimanew
|| !obj
;
931 fz_matrix ctm
, page_ctm
;
933 dev
= fz_new_bbox_device (ctx
, &rect
);
934 dev
->hints
|= FZ_IGNORE_SHADE
;
935 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
936 fz_invert_matrix (&ctm
, &page_ctm
);
937 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
938 fz_close_device (ctx
, dev
);
939 fz_drop_device (ctx
, dev
);
941 rect
.x0
+= state
.trimfuzz
.x0
;
942 rect
.x1
+= state
.trimfuzz
.x1
;
943 rect
.y0
+= state
.trimfuzz
.y0
;
944 rect
.y1
+= state
.trimfuzz
.y1
;
945 fz_transform_rect (&rect
, &ctm
);
946 fz_intersect_rect (&rect
, &mediabox
);
948 if (!fz_is_empty_rect (&rect
)) {
952 obj
= pdf_new_array (ctx
, pdf
, 4);
953 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
955 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
957 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
959 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
961 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
964 mediabox
.x0
= pdf_to_real (ctx
,
965 pdf_array_get (ctx
, obj
, 0));
966 mediabox
.y0
= pdf_to_real (ctx
,
967 pdf_array_get (ctx
, obj
, 1));
968 mediabox
.x1
= pdf_to_real (ctx
,
969 pdf_array_get (ctx
, obj
, 2));
970 mediabox
.y1
= pdf_to_real (ctx
,
971 pdf_array_get (ctx
, obj
, 3));
974 fz_drop_page (ctx
, &page
->super
);
975 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
977 printd ("progress %f Trimming %d",
978 (double) (pageno
+ 1) / state
.pagecount
,
983 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
991 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
993 if (fz_is_empty_rect (&mediabox
)) {
1002 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1004 if (!fz_is_empty_rect (&cropbox
)) {
1009 fz_intersect_rect (&mediabox
, &cropbox
);
1014 if (fz_is_empty_rect (&rootmediabox
)) {
1016 "cannot find page size for page %d\n",
1020 mediabox
= rootmediabox
;
1027 if (state
.trimmargins
&& trimw
) {
1031 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1032 fz_bound_page (ctx
, page
, &mediabox
);
1033 if (state
.trimmargins
) {
1037 dev
= fz_new_bbox_device (ctx
, &rect
);
1038 dev
->hints
|= FZ_IGNORE_SHADE
;
1039 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1040 fz_close_device (ctx
, dev
);
1041 fz_drop_device (ctx
, dev
);
1043 rect
.x0
+= state
.trimfuzz
.x0
;
1044 rect
.x1
+= state
.trimfuzz
.x1
;
1045 rect
.y0
+= state
.trimfuzz
.y0
;
1046 rect
.y1
+= state
.trimfuzz
.y1
;
1047 fz_intersect_rect (&rect
, &mediabox
);
1049 if (!fz_is_empty_rect (&rect
)) {
1053 fz_drop_page (ctx
, page
);
1055 printd ("progress %f loading %d",
1056 (double) (pageno
+ 1) / state
.pagecount
,
1063 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1065 err (1, "fwrite trim mediabox");
1071 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1073 err (1, "fread trim mediabox %d", pageno
);
1079 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1080 fz_bound_page (ctx
, page
, &mediabox
);
1081 fz_drop_page (ctx
, page
);
1083 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1085 printd ("progress %f Gathering dimensions %d",
1086 (double) (pageno
) / state
.pagecount
,
1091 fprintf (stderr
, "failed to load page %d\n", pageno
);
1097 if (state
.pagedimcount
== 0
1098 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1099 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1102 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1103 state
.pagedims
= realloc (state
.pagedims
, size
);
1104 if (!state
.pagedims
) {
1105 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1106 size
, state
.pagedimcount
+ 1);
1109 p
= &state
.pagedims
[state
.pagedimcount
++];
1111 p
->mediabox
= mediabox
;
1117 printd ("progress 1 %s %d pages in %f seconds",
1118 state
.trimmargins
? "Trimmed" : "Processed",
1119 state
.pagecount
, end
- start
);
1123 if (fclose (trimf
)) {
1129 static void layout (void)
1134 struct pagedim
*p
= p
;
1135 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1137 if (state
.pagedimcount
== 0) return;
1139 switch (state
.fitmodel
) {
1140 case FitProportional
:
1141 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1144 p
= &state
.pagedims
[pindex
];
1145 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1147 fz_transform_rect (&box
, &rm
);
1149 x0
= fz_min (box
.x0
, box
.x1
);
1150 x1
= fz_max (box
.x0
, box
.x1
);
1153 maxw
= fz_max (w
, maxw
);
1154 zoom
= state
.w
/ maxw
;
1166 ARSERT (0 && state
.fitmodel
);
1169 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1173 p
= &state
.pagedims
[pindex
];
1174 fz_rotate (&ctm
, state
.rotate
);
1175 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1177 fz_transform_rect (&box
, &rm
);
1178 w
= box
.x1
- box
.x0
;
1179 switch (state
.fitmodel
) {
1180 case FitProportional
:
1181 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1187 h
= box
.y1
- box
.y0
;
1189 zoom
= fz_min (zw
, zh
);
1190 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1199 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1200 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1202 fz_rotate (&rm
, p
->rotate
);
1203 p
->pagebox
= p
->mediabox
;
1204 fz_transform_rect (&p
->pagebox
, &rm
);
1205 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1206 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1210 fz_transform_rect (&rect
, &ctm
);
1211 fz_round_rect (&p
->bounds
, &rect
);
1214 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1215 fz_scale (&sm
, zoom
, -zoom
);
1216 fz_concat (&ctm
, &tm
, &sm
);
1222 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1223 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1224 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1225 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1226 int boundw
= x1
- x0
;
1227 int boundh
= y1
- y0
;
1229 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1230 } while (p
-- != state
.pagedims
);
1233 struct pagedim
*pdimofpageno (int pageno
)
1236 struct pagedim
*pdim
= state
.pagedims
;
1238 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1239 if (state
.pagedims
[i
].pageno
> pageno
)
1241 pdim
= &state
.pagedims
[i
];
1247 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1248 uritoanchor (const char *uri
)
1254 a
.n
= fz_resolve_link (state
.ctx
, state
.doc
, uri
, &p
.x
, &p
.y
);
1256 struct pagedim
*pdim
= pdimofpageno (a
.n
);
1257 fz_transform_point (&p
, &pdim
->ctm
);
1260 a
.h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1265 static void recurse_outline (fz_outline
*outline
, int level
)
1268 struct anchor a
= uritoanchor (outline
->uri
);
1270 printd ("o %d %d %d %d %s", level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1273 printd ("on %d %s", level
, outline
->title
);
1275 if (outline
->down
) {
1276 recurse_outline (outline
->down
, level
+ 1);
1278 outline
= outline
->next
;
1282 static void process_outline (void)
1284 fz_outline
*outline
;
1286 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1288 state
.needoutline
= 0;
1289 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1291 recurse_outline (outline
, 0);
1292 fz_drop_outline (state
.ctx
, outline
);
1296 static char *strofspan (fz_stext_span
*span
)
1301 size_t size
= 0, cap
= 80;
1303 p
= malloc (cap
+ 1);
1304 if (!p
) return NULL
;
1306 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1307 int n
= fz_runetochar (utf8
, ch
->c
);
1308 if (size
+ n
> cap
) {
1310 p
= realloc (p
, cap
+ 1);
1311 if (!p
) return NULL
;
1314 memcpy (p
+ size
, utf8
, n
);
1321 static int matchspan (regex_t
*re
, fz_stext_span
*span
,
1322 int stop
, int pageno
, double start
)
1329 fz_point p1
, p2
, p3
, p4
;
1331 p
= strofspan (span
);
1334 ret
= regexec (re
, p
, 1, &rm
, 0);
1337 if (ret
!= REG_NOMATCH
) {
1340 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1341 printd ("msg regexec error `%.*s'",
1342 (int) size
, errbuf
);
1350 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1351 c
+= fz_runelen (span
->text
[a
].c
);
1353 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1354 c
+= fz_runelen (span
->text
[b
].c
);
1357 if (fz_runelen (span
->text
[b
].c
) > 1) {
1358 b
= fz_maxi (0, b
-1);
1361 fz_stext_char_bbox (state
.ctx
, &sb
, span
, a
);
1362 fz_stext_char_bbox (state
.ctx
, &eb
, span
, b
);
1374 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1381 printd ("progress 1 found at %d `%.*s' in %f sec",
1382 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1386 printd ("match %d %d %f %f %f %f %f %f %f %f",
1398 static int compareblocks (const void *l
, const void *r
)
1400 fz_stext_block
const *ls
= l
;
1401 fz_stext_block
const *rs
= r
;
1402 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1405 /* wishful thinking function */
1406 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1410 fz_stext_page
*text
;
1411 fz_stext_sheet
*sheet
;
1412 struct pagedim
*pdim
;
1413 int stop
= 0, niters
= 0;
1418 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1419 if (niters
++ == 5) {
1422 printd ("progress 1 attention requested aborting search at %d",
1427 printd ("progress %f searching in page %d",
1428 (double) (pageno
+ 1) / state
.pagecount
,
1432 pdim
= pdimofpageno (pageno
);
1433 sheet
= fz_new_stext_sheet (state
.ctx
);
1434 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1435 tdev
= fz_new_stext_device (state
.ctx
, sheet
, text
, 0);
1437 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1439 fz_matrix ctm
= pagectm1 (page
, pdim
);
1440 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1443 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1444 fz_close_device (state
.ctx
, tdev
);
1445 fz_drop_device (state
.ctx
, tdev
);
1447 for (j
= 0; j
< text
->len
; ++j
) {
1450 fz_stext_block
*block
;
1452 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1453 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1456 for (k
= 0; k
< block
->len
; ++k
) {
1457 fz_stext_line
*line
;
1458 fz_stext_span
*span
;
1461 line
= &block
->lines
[k
];
1462 if (line
->bbox
.y0
< y
+ 1) continue;
1465 line
= &block
->lines
[block
->len
- 1 - k
];
1466 if (line
->bbox
.y0
> y
- 1) continue;
1469 for (span
= line
->first_span
; span
; span
= span
->next
) {
1470 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1472 case 1: stop
= 1; break;
1473 case -1: stop
= 1; goto endloop
;
1487 fz_drop_stext_page (state
.ctx
, text
);
1488 fz_drop_stext_sheet (state
.ctx
, sheet
);
1489 fz_drop_page (state
.ctx
, page
);
1493 printd ("progress 1 no matches %f sec", end
- start
);
1495 printd ("clearrects");
1498 static void set_tex_params (int colorspace
)
1505 switch (colorspace
) {
1507 state
.texiform
= GL_RGBA8
;
1508 state
.texform
= GL_RGBA
;
1509 state
.texty
= GL_UNSIGNED_BYTE
;
1510 state
.colorspace
= fz_device_rgb (state
.ctx
);
1513 state
.texiform
= GL_RGBA8
;
1514 state
.texform
= GL_BGRA
;
1515 state
.texty
= endianness
.s
> 1
1516 ? GL_UNSIGNED_INT_8_8_8_8
1517 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1518 state
.colorspace
= fz_device_bgr (state
.ctx
);
1521 state
.texiform
= GL_LUMINANCE_ALPHA
;
1522 state
.texform
= GL_LUMINANCE_ALPHA
;
1523 state
.texty
= GL_UNSIGNED_BYTE
;
1524 state
.colorspace
= fz_device_gray (state
.ctx
);
1527 errx (1, "invalid colorspce %d", colorspace
);
1531 static void realloctexts (int texcount
)
1535 if (texcount
== state
.texcount
) return;
1537 if (texcount
< state
.texcount
) {
1538 glDeleteTextures (state
.texcount
- texcount
,
1539 state
.texids
+ texcount
);
1542 size
= texcount
* sizeof (*state
.texids
);
1543 state
.texids
= realloc (state
.texids
, size
);
1544 if (!state
.texids
) {
1545 err (1, "realloc texids %" FMT_s
, size
);
1548 size
= texcount
* sizeof (*state
.texowners
);
1549 state
.texowners
= realloc (state
.texowners
, size
);
1550 if (!state
.texowners
) {
1551 err (1, "realloc texowners %" FMT_s
, size
);
1553 if (texcount
> state
.texcount
) {
1556 glGenTextures (texcount
- state
.texcount
,
1557 state
.texids
+ state
.texcount
);
1558 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1559 state
.texowners
[i
].w
= -1;
1560 state
.texowners
[i
].slice
= NULL
;
1563 state
.texcount
= texcount
;
1567 static char *mbtoutf8 (char *s
)
1573 len
= mbstowcs (NULL
, s
, strlen (s
));
1578 if (len
== (size_t) -1) {
1583 tmp
= malloc (len
* sizeof (wchar_t));
1588 ret
= mbstowcs (tmp
, s
, len
);
1589 if (ret
== (size_t) -1) {
1595 for (i
= 0; i
< ret
; ++i
) {
1596 len
+= fz_runelen (tmp
[i
]);
1599 p
= r
= malloc (len
+ 1);
1605 for (i
= 0; i
< ret
; ++i
) {
1606 p
+= fz_runetochar (p
, tmp
[i
]);
1613 CAMLprim value
ml_mbtoutf8 (value s_v
)
1619 s
= String_val (s_v
);
1625 ret_v
= caml_copy_string (r
);
1631 static void * mainloop (void UNUSED_ATTR
*unused
)
1634 int len
, ret
, oldlen
= 0;
1639 len
= readlen (state
.csock
);
1641 errx (1, "readlen returned 0");
1644 if (oldlen
< len
+ 1) {
1645 p
= realloc (p
, len
+ 1);
1647 err (1, "realloc %d failed", len
+ 1);
1651 readdata (state
.csock
, p
, len
);
1654 if (!strncmp ("open", p
, 4)) {
1655 int wthack
, off
, ok
= 0;
1662 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1664 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1667 filename
= p
+ 5 + off
;
1668 filenamelen
= strlen (filename
);
1669 password
= filename
+ filenamelen
+ 1;
1671 if (password
[strlen (password
) + 1]) {
1672 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 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",
4542 haspboext
= Bool_val (Field (params_v
, 9));
4544 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4545 fz_register_document_handlers (state
.ctx
);
4547 #ifdef USE_FONTCONFIG
4548 if (Bool_val (Field (params_v
, 11))) {
4549 fz_install_load_system_font_funcs (
4550 state
.ctx
, fc_load_system_font_func
, NULL
4555 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4556 fuzz_v
= Field (trim_v
, 1);
4557 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4558 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4559 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4560 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4562 set_tex_params (colorspace
);
4565 #ifndef USE_FONTCONFIG
4566 state
.face
= load_font (fontpath
);
4570 char *buf
= fontpath
;
4571 FcPattern
*pat
, *pat1
;
4574 fc
.config
= FcInitLoadConfigAndFonts ();
4576 errx (1, "FcInitLoadConfigAndFonts failed");
4579 pat
= FcNameParse ((FcChar8
*) buf
);
4581 errx (1, "FcNameParse failed");
4584 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4585 errx (1, "FcConfigSubstitute failed");
4587 FcDefaultSubstitute (pat
);
4589 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4591 errx (1, "FcFontMatch failed");
4594 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4595 errx (1, "FcPatternGetString failed");
4598 state
.face
= load_font ((char *) path
);
4599 FcPatternDestroy (pat
);
4600 FcPatternDestroy (pat1
);
4605 const char *data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0,
4607 state
.face
= load_builtin_font (data
, len
);
4609 if (!state
.face
) _exit (1);
4611 realloctexts (texcount
);
4619 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4621 errx (1, "pthread_create: %s", strerror (ret
));
4624 CAMLreturn (Val_unit
);