1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/utsname.h>
20 #include <cygwin/socket.h> /* FIONREAD */
32 #include <caml/fail.h>
33 #include <caml/alloc.h>
34 #include <caml/memory.h>
35 #include <caml/unixsupport.h>
37 #include <mupdf/fitz.h>
38 #include <mupdf/cbz.h>
39 #include <mupdf/pdf.h>
40 #include <mupdf/xps.h>
41 #include <mupdf/img.h>
44 #include FT_FREETYPE_H
47 #include <fontconfig/fontconfig.h>
51 #define CACHE_PAGEREFS
54 extern char **environ
;
57 #define MIN(a,b) ((a) < (b) ? (a) : (b))
58 #define MAX(a,b) ((a) > (b) ? (a) : (b))
61 #define NORETURN_ATTR __attribute__ ((noreturn))
62 #define UNUSED_ATTR __attribute__ ((unused))
63 #if !defined __clang__
64 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
66 #define OPTIMIZE_ATTR(n)
68 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
72 #define OPTIMIZE_ATTR(n)
73 #define GCC_FMT_ATTR(a, b)
78 #define FMT_ptr PRIxPTR
79 #define SCN_ptr SCNxPTR
80 #define FMT_ptr_cast(p) ((uintptr_t) (p))
81 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
83 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
84 err (int exitcode
, const char *fmt
, ...)
91 vfprintf (stderr
, fmt
, ap
);
93 fprintf (stderr
, ": %s\n", strerror (savederrno
));
98 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
99 errx (int exitcode
, const char *fmt
, ...)
104 vfprintf (stderr
, fmt
, ap
);
106 fputc ('\n', stderr
);
111 #ifndef GL_TEXTURE_RECTANGLE_ARB
112 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
116 #define GL_BGRA 0x80E1
119 #ifndef GL_UNSIGNED_INT_8_8_8_8
120 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
123 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
124 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
128 #define lprintf printf
133 #define ARSERT(cond) for (;;) { \
135 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
151 struct slice slices
[1];
162 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
166 enum { SLINK
, SANNOT
} tag
;
179 enum { DNONE
, DPDF
, DXPS
, DCBZ
, DIMG
};
189 fz_text_sheet
*sheet
;
197 fz_display_list
*dlist
;
199 struct slink
*slinks
;
201 struct annot
*annots
;
206 void (*freepage
) (void *);
212 struct pagedim
*pagedims
;
232 fz_colorspace
*colorspace
;
240 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
254 void (*closedoc
) (void);
255 void (*freepage
) (void *);
264 void (*glBindBufferARB
) (GLenum
, GLuint
);
265 GLboolean (*glUnmapBufferARB
) (GLenum
);
266 void *(*glMapBufferARB
) (GLenum
, GLenum
);
267 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
268 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
269 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
271 GLfloat texcoords
[8];
272 GLfloat vertices
[16];
274 #ifdef CACHE_PAGEREFS
290 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
292 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
295 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
297 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
300 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
302 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
303 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
306 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
308 static void lock (const char *cap
)
310 int ret
= pthread_mutex_lock (&mutex
);
312 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
316 static void unlock (const char *cap
)
318 int ret
= pthread_mutex_unlock (&mutex
);
320 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
324 static int trylock (const char *cap
)
326 int ret
= pthread_mutex_trylock (&mutex
);
327 if (ret
&& ret
!= EBUSY
) {
328 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
333 static void *parse_pointer (const char *cap
, const char *s
)
338 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
340 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
345 static double now (void)
349 if (gettimeofday (&tv
, NULL
)) {
350 err (1, "gettimeofday");
352 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
355 static int hasdata (void)
358 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
359 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
363 CAMLprim value
ml_hasdata (value fd_v
)
368 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
369 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
370 CAMLreturn (Val_bool (avail
> 0));
373 static void readdata (void *p
, int size
)
378 n
= read (state
.csock
, p
, size
);
380 if (errno
== EINTR
) goto again
;
381 err (1, "read (req %d, ret %zd)", size
, n
);
384 if (!n
) errx (1, "EOF while reading");
385 errx (1, "read (req %d, ret %zd)", size
, n
);
389 static void writedata (char *p
, int size
)
393 p
[0] = (size
>> 24) & 0xff;
394 p
[1] = (size
>> 16) & 0xff;
395 p
[2] = (size
>> 8) & 0xff;
396 p
[3] = (size
>> 0) & 0xff;
398 n
= write (state
.csock
, p
, size
+ 4);
400 if (!n
) errx (1, "EOF while writing data");
401 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
405 static int readlen (void)
410 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
413 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
421 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
424 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
428 if (len
< size
- 4) {
429 writedata (buf
, len
);
435 err (1, "vsnprintf for `%s' failed", fmt
);
437 buf
= realloc (buf
, size
);
442 static void closepdf (void)
444 #ifdef CACHE_PAGEREFS
445 if (state
.pdflut
.objs
) {
448 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
449 pdf_drop_obj (state
.pdflut
.objs
[i
]);
451 free (state
.pdflut
.objs
);
452 state
.pdflut
.objs
= NULL
;
453 state
.pdflut
.idx
= 0;
458 pdf_close_document (state
.u
.pdf
);
463 static void closexps (void)
466 xps_close_document (state
.u
.xps
);
471 static void closecbz (void)
474 cbz_close_document (state
.u
.cbz
);
479 static void closeimg (void)
482 image_close_document (state
.u
.img
);
487 static void freepdfpage (void *ptr
)
489 pdf_free_page (state
.u
.pdf
, ptr
);
492 static void freeemptyxxxpage (void *ptr
)
497 static void freexpspage (void *ptr
)
499 xps_free_page (state
.u
.xps
, ptr
);
502 static void freecbzpage (void *ptr
)
504 cbz_free_page (state
.u
.cbz
, ptr
);
507 static void freeimgpage (void *ptr
)
509 image_free_page (state
.u
.img
, ptr
);
512 static int openxref (char *filename
, char *password
)
516 for (i
= 0; i
< state
.texcount
; ++i
) {
517 state
.texowners
[i
].w
= -1;
518 state
.texowners
[i
].slice
= NULL
;
521 if (state
.closedoc
) state
.closedoc ();
524 len
= strlen (filename
);
542 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
543 const char *ext
= tbl
[j
].ext
;
544 int len2
= strlen (ext
);
546 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
547 state
.type
= tbl
[j
].type
;
553 if (state
.pagedims
) {
554 free (state
.pagedims
);
555 state
.pagedims
= NULL
;
557 state
.pagedimcount
= 0;
559 fz_set_aa_level (state
.ctx
, state
.aalevel
);
560 switch (state
.type
) {
562 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
563 if (pdf_needs_password (state
.u
.pdf
)) {
564 if (password
&& !*password
) {
569 int ok
= pdf_authenticate_password (state
.u
.pdf
, password
);
571 printd ("pass fail");
576 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
577 state
.closedoc
= closepdf
;
578 state
.freepage
= freepdfpage
;
582 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
583 state
.pagecount
= xps_count_pages (state
.u
.xps
);
584 state
.closedoc
= closexps
;
585 state
.freepage
= freexpspage
;
589 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
590 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
591 state
.closedoc
= closecbz
;
592 state
.freepage
= freecbzpage
;
596 state
.u
.img
= image_open_document (state
.ctx
, filename
);
598 state
.closedoc
= closeimg
;
599 state
.freepage
= freeimgpage
;
605 static void pdfinfo (void)
607 if (state
.type
== DPDF
) {
610 printd ("info PDF version\t%d.%d",
611 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
613 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
617 char *items
[] = { "Title", "Author", "Creator",
618 "Producer", "CreationDate" };
620 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
621 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
622 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
623 if (*s
) printd ("info %s\t%s", items
[i
], s
);
624 fz_free (state
.ctx
, s
);
631 static void unlinktile (struct tile
*tile
)
635 for (i
= 0; i
< tile
->slicecount
; ++i
) {
636 struct slice
*s
= &tile
->slices
[i
];
638 if (s
->texindex
!= -1) {
639 if (state
.texowners
[s
->texindex
].slice
== s
) {
640 state
.texowners
[s
->texindex
].slice
= NULL
;
646 static void freepage (struct page
*page
)
649 fz_free_text_page (state
.ctx
, page
->text
);
652 fz_free_text_sheet (state
.ctx
, page
->sheet
);
657 fz_drop_display_list (state
.ctx
, page
->dlist
);
658 page
->freepage (page
->u
.ptr
);
662 static void freetile (struct tile
*tile
)
667 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
670 fz_drop_pixmap (state
.ctx
, state
.pig
);
672 state
.pig
= tile
->pixmap
;
677 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
686 static int cacheline32bytes
;
688 static void __attribute__ ((constructor
)) clcheck (void)
690 char **envp
= environ
;
695 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
697 cacheline32bytes
= auxv
[1] == 32;
703 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
705 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
706 if (cacheline32bytes
&& size
> 32) {
707 intptr_t a1
, a2
, diff
;
709 vector
unsigned char v
= vec_splat_u8 (-1);
710 vector
unsigned char *p
;
712 a1
= a2
= (intptr_t) pixmap
->samples
;
713 a2
= (a1
+ 31) & ~31;
718 while (a1
!= a2
) *(char *) a1
++ = 0xff;
719 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
720 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
722 vec_st (v
, i
+ 16, p
);
724 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
726 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
729 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
732 static void trimctm (pdf_page
*page
, int pindex
)
735 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
737 if (!pdim
->tctmready
) {
738 if (state
.trimmargins
) {
740 fz_matrix rm
, sm
, tm
, im
, ctm1
;
742 fz_rotate (&rm
, -pdim
->rotate
);
743 fz_scale (&sm
, 1, -1);
744 fz_concat (&ctm
, &rm
, &sm
);
745 realbox
= pdim
->mediabox
;
746 fz_transform_rect (&realbox
, &ctm
);
747 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
748 fz_concat (&ctm1
, &ctm
, &tm
);
749 fz_invert_matrix (&im
, &page
->ctm
);
750 fz_concat (&ctm
, &im
, &ctm1
);
760 static fz_matrix
pagectm (struct page
*page
)
764 if (page
->type
== DPDF
) {
765 trimctm (page
->u
.pdfpage
, page
->pdimno
);
767 &state
.pagedims
[page
->pdimno
].tctm
,
768 &state
.pagedims
[page
->pdimno
].ctm
);
771 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
773 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
774 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
779 static void *loadpage (int pageno
, int pindex
)
784 page
= calloc (sizeof (struct page
), 1);
786 err (1, "calloc page %d", pageno
);
789 page
->dlist
= fz_new_display_list (state
.ctx
);
790 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
792 switch (state
.type
) {
794 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
795 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
797 page
->freepage
= freepdfpage
;
801 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
802 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
804 page
->freepage
= freexpspage
;
808 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
809 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
811 page
->freepage
= freecbzpage
;
815 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
816 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
818 page
->freepage
= freeimgpage
;
822 fz_catch (state
.ctx
) {
824 page
->freepage
= freeemptyxxxpage
;
826 fz_free_device (dev
);
828 page
->pdimno
= pindex
;
829 page
->pageno
= pageno
;
830 page
->sgen
= state
.gen
;
831 page
->agen
= state
.gen
;
832 page
->tgen
= state
.gen
;
833 page
->type
= state
.type
;
838 static struct tile
*alloctile (int h
)
845 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
846 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
847 tile
= calloc (tilesize
, 1);
849 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
851 for (i
= 0; i
< slicecount
; ++i
) {
852 int sh
= MIN (h
, state
.sliceheight
);
853 tile
->slices
[i
].h
= sh
;
854 tile
->slices
[i
].texindex
= -1;
857 tile
->slicecount
= slicecount
;
858 tile
->sliceheight
= state
.sliceheight
;
868 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
869 const fz_matrix
*ctm
, float alpha
)
871 struct obs
*obs
= dev
->user
;
873 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
875 fz_rect rect
= fz_unit_rect
;
877 fz_transform_rect (&rect
, ctm
);
878 fz_round_rect (&b
, &rect
);
879 fz_intersect_irect (&b
, &obs
->b
);
880 obs
->cured
= b
.x0
== obs
->b
.x0
883 && b
.y1
== obs
->b
.y1
;
887 static int obscured (struct page
*page
, fz_irect bbox
)
894 memset (&dev
, 0, sizeof (dev
));
895 memset (&obs
, 0, sizeof (obs
));
900 dev
.fill_image
= obs_fill_image
;
902 fz_rect_from_irect (&rect
, &bbox
);
903 ctm
= pagectm (page
);
904 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
907 #define OBSCURED obscured
909 #define OBSCURED(a, b) 0
912 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
920 struct pagedim
*pdim
;
922 tile
= alloctile (h
);
923 pdim
= &state
.pagedims
[page
->pdimno
];
928 bbox
.x1
= bbox
.x0
+ w
;
929 bbox
.y1
= bbox
.y0
+ h
;
932 if (state
.pig
->w
== w
934 && state
.pig
->colorspace
== state
.colorspace
) {
935 tile
->pixmap
= state
.pig
;
936 tile
->pixmap
->x
= bbox
.x0
;
937 tile
->pixmap
->y
= bbox
.y0
;
940 fz_drop_pixmap (state
.ctx
, state
.pig
);
947 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
953 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
959 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
960 clearpixmap (tile
->pixmap
);
962 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
963 ctm
= pagectm (page
);
964 fz_rect_from_irect (&rect
, &bbox
);
965 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
966 fz_free_device (dev
);
971 #ifdef CACHE_PAGEREFS
972 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
973 thanks to Robin Watts */
975 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
977 fz_context
*ctx
= doc
->ctx
;
981 if (state
.pdflut
.idx
== state
.pagecount
) return;
983 kids
= pdf_dict_gets (node
, "Kids");
984 len
= pdf_array_len (kids
);
987 fz_throw (ctx
, FZ_ERROR_GENERIC
, "Malformed pages tree");
989 if (pdf_mark_obj (node
))
990 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
991 for (i
= 0; i
< len
; i
++) {
992 pdf_obj
*kid
= pdf_array_get (kids
, i
);
993 char *type
= pdf_to_name (pdf_dict_gets (kid
, "Type"));
995 ? !strcmp (type
, "Pages")
996 : pdf_dict_gets (kid
, "Kids")
997 && !pdf_dict_gets (kid
, "MediaBox")) {
998 pdf_collect_pages (doc
, kid
);
1002 ? strcmp (type
, "Page") != 0
1003 : !pdf_dict_gets (kid
, "MediaBox"))
1004 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
1005 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (kid
);
1008 pdf_unmark_obj (node
);
1012 pdf_load_page_objs (pdf_document
*doc
)
1014 pdf_obj
*root
= pdf_dict_gets (pdf_trailer (doc
), "Root");
1015 pdf_obj
*node
= pdf_dict_gets (root
, "Pages");
1018 fz_throw (doc
->ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
1020 state
.pdflut
.idx
= 0;
1021 pdf_collect_pages (doc
, node
);
1025 static void initpdims (void)
1029 fz_rect rootmediabox
;
1030 int pageno
, trim
, show
;
1031 int trimw
= 0, cxcount
;
1035 if (state
.trimmargins
&& state
.trimcachepath
) {
1036 trimf
= fopen (state
.trimcachepath
, "rb");
1038 trimf
= fopen (state
.trimcachepath
, "wb");
1043 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1044 cxcount
= state
.pagecount
;
1046 cxcount
= MIN (state
.pagecount
, 1);
1048 if (state
.type
== DPDF
) {
1050 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1051 "Root/Pages/MediaBox");
1052 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1055 #ifdef CACHE_PAGEREFS
1056 if (state
.type
== DPDF
1057 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1058 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
1059 if (!state
.pdflut
.objs
) {
1060 err (1, "malloc pageobjs %zu %d %zu failed",
1061 sizeof (*state
.pdflut
.objs
), cxcount
,
1062 sizeof (*state
.pdflut
.objs
) * cxcount
);
1064 state
.pdflut
.count
= cxcount
;
1065 pdf_load_page_objs (state
.u
.pdf
);
1066 state
.pdflut
.pdf
= state
.u
.pdf
;
1070 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1075 switch (state
.type
) {
1077 pdf_document
*pdf
= state
.u
.pdf
;
1078 pdf_obj
*pageref
, *pageobj
;
1080 #ifdef CACHE_PAGEREFS
1081 pageref
= state
.pdflut
.objs
[pageno
];
1083 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1084 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1086 printd ("progress %f Gathering dimensions %d",
1087 (double) (pageno
) / state
.pagecount
,
1091 pageobj
= pdf_resolve_indirect (pageref
);
1093 if (state
.trimmargins
) {
1097 fz_try (state
.ctx
) {
1098 page
= pdf_load_page (pdf
, pageno
);
1099 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1100 trim
= state
.trimanew
|| !obj
;
1106 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1107 dev
->hints
|= FZ_IGNORE_SHADE
;
1108 fz_invert_matrix (&ctm
, &page
->ctm
);
1109 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1110 fz_free_device (dev
);
1112 rect
.x0
+= state
.trimfuzz
.x0
;
1113 rect
.x1
+= state
.trimfuzz
.x1
;
1114 rect
.y0
+= state
.trimfuzz
.y0
;
1115 rect
.y1
+= state
.trimfuzz
.y1
;
1116 fz_transform_rect (&rect
, &ctm
);
1117 fz_intersect_rect (&rect
, &page
->mediabox
);
1119 if (fz_is_empty_rect (&rect
)) {
1120 mediabox
= page
->mediabox
;
1126 obj
= pdf_new_array (pdf
, 4);
1127 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1128 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1129 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1130 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1131 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1134 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1135 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1136 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1137 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1140 rotate
= page
->rotate
;
1141 pdf_free_page (pdf
, page
);
1143 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1145 printd ("progress %f Trimming %d",
1146 (double) (pageno
+ 1) / state
.pagecount
,
1150 fz_catch (state
.ctx
) {
1151 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1158 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1160 if (fz_is_empty_rect (&mediabox
)) {
1168 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1170 if (!fz_is_empty_rect (&cropbox
)) {
1175 fz_intersect_rect (&mediabox
, &cropbox
);
1180 if (fz_is_empty_rect (&rootmediabox
)) {
1182 "cannot find page size for page %d\n",
1186 mediabox
= rootmediabox
;
1190 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1199 fz_try (state
.ctx
) {
1200 page
= xps_load_page (state
.u
.xps
, pageno
);
1201 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1203 if (state
.trimmargins
) {
1207 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1208 dev
->hints
|= FZ_IGNORE_SHADE
;
1209 xps_run_page (state
.u
.xps
, page
, dev
,
1210 &fz_identity
, NULL
);
1211 fz_free_device (dev
);
1213 rect
.x0
+= state
.trimfuzz
.x0
;
1214 rect
.x1
+= state
.trimfuzz
.x1
;
1215 rect
.y0
+= state
.trimfuzz
.y0
;
1216 rect
.y1
+= state
.trimfuzz
.y1
;
1217 fz_intersect_rect (&rect
, &mediabox
);
1219 if (!fz_is_empty_rect (&rect
)) {
1223 xps_free_page (state
.u
.xps
, page
);
1225 printd ("progress %f loading %d",
1226 (double) (pageno
+ 1) / state
.pagecount
,
1230 fz_catch (state
.ctx
) {
1242 if (state
.trimmargins
&& trimw
) {
1245 fz_try (state
.ctx
) {
1246 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1247 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1248 cbz_free_page (state
.u
.cbz
, page
);
1249 printd ("progress %f Trimming %d",
1250 (double) (pageno
+ 1) / state
.pagecount
,
1253 fz_catch (state
.ctx
) {
1254 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1257 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1259 err (1, "fwrite trim mediabox");
1265 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1267 err (1, "fread trim mediabox");
1273 fz_try (state
.ctx
) {
1274 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1275 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1276 cbz_free_page (state
.u
.cbz
, page
);
1278 fz_catch (state
.ctx
) {
1279 fprintf (stderr
, "failed to load cbz page\n");
1297 fz_try (state
.ctx
) {
1298 page
= image_load_page (state
.u
.img
, pageno
);
1299 image_bound_page (state
.u
.img
, page
, &mediabox
);
1300 image_free_page (state
.u
.img
, page
);
1302 fz_catch (state
.ctx
) {
1303 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1309 ARSERT (0 && state
.type
);
1312 if (state
.pagedimcount
== 0
1313 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1314 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1317 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1318 state
.pagedims
= realloc (state
.pagedims
, size
);
1319 if (!state
.pagedims
) {
1320 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1321 size
, state
.pagedimcount
+ 1);
1324 p
= &state
.pagedims
[state
.pagedimcount
++];
1326 p
->mediabox
= mediabox
;
1331 if (state
.trimmargins
) {
1332 printd ("progress 1 Trimmed %d pages in %f seconds",
1333 state
.pagecount
, end
- start
);
1336 if (state
.type
== DPDF
)
1337 printd ("progress 1 Processed %d pages in %f seconds",
1338 state
.pagecount
, end
- start
);
1340 printd ("vmsg Processed %d pages in %f seconds",
1341 state
.pagecount
, end
- start
);
1345 if (fclose (trimf
)) {
1351 static void layout (void)
1356 struct pagedim
*p
= p
;
1357 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1359 if (state
.pagedimcount
== 0) return;
1361 switch (state
.fitmodel
) {
1362 case FitProportional
:
1363 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1366 p
= &state
.pagedims
[pindex
];
1367 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1369 fz_transform_rect (&box
, &rm
);
1371 x0
= MIN (box
.x0
, box
.x1
);
1372 x1
= MAX (box
.x0
, box
.x1
);
1375 maxw
= MAX (w
, maxw
);
1376 zoom
= state
.w
/ maxw
;
1388 ARSERT (0 && state
.fitmodel
);
1391 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1395 p
= &state
.pagedims
[pindex
];
1396 fz_rotate (&ctm
, state
.rotate
);
1397 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1399 fz_transform_rect (&box
, &rm
);
1400 w
= box
.x1
- box
.x0
;
1401 switch (state
.fitmodel
) {
1402 case FitProportional
:
1403 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1409 h
= box
.y1
- box
.y0
;
1411 zoom
= MIN (zw
, zh
);
1412 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1421 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1422 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1424 fz_rotate (&rm
, p
->rotate
);
1425 p
->pagebox
= p
->mediabox
;
1426 fz_transform_rect (&p
->pagebox
, &rm
);
1427 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1428 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1432 fz_transform_rect (&rect
, &ctm
);
1433 fz_round_rect (&p
->bounds
, &rect
);
1436 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1437 fz_scale (&sm
, zoom
, -zoom
);
1438 fz_concat (&ctm
, &tm
, &sm
);
1439 fz_concat (&p
->lctm
, &ctm
, &rm
);
1445 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1446 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1447 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1448 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1449 int boundw
= x1
- x0
;
1450 int boundh
= y1
- y0
;
1452 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1453 } while (p
-- != state
.pagedims
);
1457 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1458 desttoanchor (fz_link_dest
*dest
)
1462 struct pagedim
*pdim
= state
.pagedims
;
1467 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1468 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1470 pdim
= &state
.pagedims
[i
];
1472 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1474 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1475 p
.x
= dest
->ld
.gotor
.lt
.x
;
1478 p
.y
= dest
->ld
.gotor
.lt
.y
;
1479 fz_transform_point (&p
, &pdim
->lctm
);
1483 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1484 double x0
, x1
, y0
, y1
;
1486 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1487 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1489 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1490 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1492 a
.n
= dest
->ld
.gotor
.page
;
1497 static void recurse_outline (fz_outline
*outline
, int level
)
1500 switch (outline
->dest
.kind
) {
1503 struct anchor a
= desttoanchor (&outline
->dest
);
1506 printd ("o %d %d %d %d %s",
1507 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1513 printd ("ou %d %" FMT_s
" %s %s", level
,
1514 strlen (outline
->title
), outline
->title
,
1515 outline
->dest
.ld
.uri
.uri
);
1519 printd ("on %d %s", level
, outline
->title
);
1523 printd ("emsg Unhandled outline kind %d for %s\n",
1524 outline
->dest
.kind
, outline
->title
);
1527 if (outline
->down
) {
1528 recurse_outline (outline
->down
, level
+ 1);
1530 outline
= outline
->next
;
1534 static void process_outline (void)
1536 fz_outline
*outline
;
1538 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1540 state
.needoutline
= 0;
1541 switch (state
.type
) {
1543 outline
= pdf_load_outline (state
.u
.pdf
);
1546 outline
= xps_load_outline (state
.u
.xps
);
1553 recurse_outline (outline
, 0);
1554 fz_free_outline (state
.ctx
, outline
);
1558 static char *strofspan (fz_text_span
*span
)
1563 size_t size
= 0, cap
= 80;
1565 p
= malloc (cap
+ 1);
1566 if (!p
) return NULL
;
1568 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1569 int n
= fz_runetochar (utf8
, ch
->c
);
1570 if (size
+ n
> cap
) {
1572 p
= realloc (p
, cap
+ 1);
1573 if (!p
) return NULL
;
1576 memcpy (p
+ size
, utf8
, n
);
1583 static int matchspan (regex_t
*re
, fz_text_span
*span
,
1584 int stop
, int pageno
, double start
)
1591 fz_point p1
, p2
, p3
, p4
;
1593 p
= strofspan (span
);
1596 ret
= regexec (re
, p
, 1, &rm
, 0);
1599 if (ret
!= REG_NOMATCH
) {
1602 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1603 printd ("msg regexec error `%.*s'",
1604 (int) size
, errbuf
);
1612 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1613 c
+= fz_runelen (span
->text
[a
].c
);
1615 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1616 c
+= fz_runelen (span
->text
[b
].c
);
1619 if (fz_runelen (span
->text
[b
].c
) > 1) {
1623 fz_text_char_bbox (&sb
, span
, a
);
1624 fz_text_char_bbox (&eb
, span
, b
);
1636 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1643 printd ("progress 1 found at %d `%.*s' in %f sec",
1644 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1648 printd ("match %d %d %f %f %f %f %f %f %f %f",
1660 static int compareblocks (const void *l
, const void *r
)
1662 fz_text_block
const *ls
= l
;
1663 fz_text_block
const* rs
= r
;
1664 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1667 /* wishful thinking function */
1668 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1673 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1675 fz_text_sheet
*sheet
;
1676 struct pagedim
*pdim
, *pdimprev
;
1677 int stop
= 0, niters
= 0;
1680 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1684 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1685 if (niters
++ == 5) {
1688 printd ("progress 1 attention requested aborting search at %d",
1693 printd ("progress %f searching in page %d",
1694 (double) (pageno
+ 1) / state
.pagecount
,
1699 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1700 pdim
= &state
.pagedims
[i
];
1701 if (pdim
->pageno
== pageno
) {
1704 if (pdim
->pageno
> pageno
) {
1713 sheet
= fz_new_text_sheet (state
.ctx
);
1714 text
= fz_new_text_page (state
.ctx
);
1715 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1717 switch (state
.type
) {
1720 fz_try (state
.ctx
) {
1721 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1722 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1723 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1724 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1725 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1728 fz_catch (state
.ctx
) {
1729 fz_free_device (tdev
);
1736 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1737 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1741 ARSERT (0 && state
.type
);
1744 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1745 fz_free_device (tdev
);
1747 for (j
= 0; j
< text
->len
; ++j
) {
1750 fz_text_block
*block
;
1752 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1753 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1756 for (k
= 0; k
< block
->len
; ++k
) {
1761 line
= &block
->lines
[k
];
1762 if (line
->bbox
.y0
< y
+ 1) continue;
1765 line
= &block
->lines
[block
->len
- 1 - k
];
1766 if (line
->bbox
.y0
> y
- 1) continue;
1769 for (span
= line
->first_span
; span
; span
= span
->next
) {
1770 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1772 case 1: stop
= 1; break;
1773 case -1: stop
= 1; goto endloop
;
1788 fz_free_text_page (state
.ctx
, text
);
1789 fz_free_text_sheet (state
.ctx
, sheet
);
1791 state
.freepage (u
.ptr
);
1796 printd ("progress 1 no matches %f sec", end
- start
);
1798 printd ("clearrects");
1801 static void set_tex_params (int colorspace
)
1808 switch (colorspace
) {
1810 state
.texiform
= GL_RGBA8
;
1811 state
.texform
= GL_RGBA
;
1812 state
.texty
= GL_UNSIGNED_BYTE
;
1813 state
.colorspace
= fz_device_rgb (state
.ctx
);
1816 state
.texiform
= GL_RGBA8
;
1817 state
.texform
= GL_BGRA
;
1818 state
.texty
= endianness
.s
> 1
1819 ? GL_UNSIGNED_INT_8_8_8_8
1820 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1821 state
.colorspace
= fz_device_bgr (state
.ctx
);
1824 state
.texiform
= GL_LUMINANCE_ALPHA
;
1825 state
.texform
= GL_LUMINANCE_ALPHA
;
1826 state
.texty
= GL_UNSIGNED_BYTE
;
1827 state
.colorspace
= fz_device_gray (state
.ctx
);
1830 errx (1, "invalid colorspce %d", colorspace
);
1834 static void realloctexts (int texcount
)
1838 if (texcount
== state
.texcount
) return;
1840 if (texcount
< state
.texcount
) {
1841 glDeleteTextures (state
.texcount
- texcount
,
1842 state
.texids
+ texcount
);
1845 size
= texcount
* sizeof (*state
.texids
);
1846 state
.texids
= realloc (state
.texids
, size
);
1847 if (!state
.texids
) {
1848 err (1, "realloc texids %" FMT_s
, size
);
1851 size
= texcount
* sizeof (*state
.texowners
);
1852 state
.texowners
= realloc (state
.texowners
, size
);
1853 if (!state
.texowners
) {
1854 err (1, "realloc texowners %" FMT_s
, size
);
1856 if (texcount
> state
.texcount
) {
1859 glGenTextures (texcount
- state
.texcount
,
1860 state
.texids
+ state
.texcount
);
1861 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1862 state
.texowners
[i
].w
= -1;
1863 state
.texowners
[i
].slice
= NULL
;
1866 state
.texcount
= texcount
;
1870 static char *mbtoutf8 (char *s
)
1876 len
= mbstowcs (NULL
, s
, strlen (s
));
1881 if (len
== (size_t) -1) {
1886 tmp
= malloc (len
* sizeof (wchar_t));
1891 ret
= mbstowcs (tmp
, s
, len
);
1892 if (ret
== (size_t) -1) {
1898 for (i
= 0; i
< ret
; ++i
) {
1899 len
+= fz_runelen (tmp
[i
]);
1902 p
= r
= malloc (len
+ 1);
1908 for (i
= 0; i
< ret
; ++i
) {
1909 p
+= fz_runetochar (p
, tmp
[i
]);
1916 CAMLprim value
ml_mbtoutf8 (value s_v
)
1922 s
= String_val (s_v
);
1928 ret_v
= caml_copy_string (r
);
1934 static void * mainloop (void UNUSED_ATTR
*unused
)
1937 int len
, ret
, oldlen
= 0;
1942 errx (1, "readlen returned 0");
1945 if (oldlen
< len
+ 1) {
1946 p
= realloc (p
, len
+ 1);
1948 err (1, "realloc %d failed", len
+ 1);
1955 if (!strncmp ("open", p
, 4)) {
1956 int wthack
, off
, ok
= 0;
1962 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1964 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1967 filename
= p
+ 5 + off
;
1968 filenamelen
= strlen (filename
);
1969 password
= filename
+ filenamelen
+ 1;
1972 fz_try (state
.ctx
) {
1973 ok
= openxref (filename
, password
);
1975 fz_catch (state
.ctx
) {
1976 utf8filename
= mbtoutf8 (filename
);
1977 printd ("msg Could not open %s", utf8filename
);
1987 utf8filename
= mbtoutf8 (filename
);
1988 printd ("msg Opened %s (press h/F1 to get help)",
1990 if (utf8filename
!= filename
) {
1991 free (utf8filename
);
1994 state
.needoutline
= 1;
1997 else if (!strncmp ("cs", p
, 2)) {
2000 ret
= sscanf (p
+ 2, " %d", &colorspace
);
2002 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
2005 set_tex_params (colorspace
);
2006 for (i
= 0; i
< state
.texcount
; ++i
) {
2007 state
.texowners
[i
].w
= -1;
2008 state
.texowners
[i
].slice
= NULL
;
2012 else if (!strncmp ("freepage", p
, 8)) {
2015 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2017 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
2021 else if (!strncmp ("freetile", p
, 8)) {
2024 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2026 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
2030 else if (!strncmp ("search", p
, 6)) {
2031 int icase
, pageno
, y
, len2
, forward
;
2035 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2036 &icase
, &pageno
, &y
, &forward
, &len2
);
2038 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2041 pattern
= p
+ 6 + len2
;
2042 ret
= regcomp (&re
, pattern
,
2043 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2048 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2049 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2052 search (&re
, pageno
, y
, forward
);
2056 else if (!strncmp ("geometry", p
, 8)) {
2060 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2062 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2070 for (i
= 0; i
< state
.texcount
; ++i
) {
2071 state
.texowners
[i
].slice
= NULL
;
2074 state
.fitmodel
= fitmodel
;
2079 unlock ("geometry");
2080 printd ("continue %d", state
.pagecount
);
2082 else if (!strncmp ("reqlayout", p
, 9)) {
2085 unsigned int fitmodel
;
2088 ret
= sscanf (p
+ 9, " %d %u %d %n",
2089 &rotate
, &fitmodel
, &h
, &off
);
2091 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2094 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2097 state
.rotate
= rotate
;
2098 state
.fitmodel
= fitmodel
;
2103 nameddest
= p
+ 9 + off
;
2104 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2107 pdf_obj
*needle
, *obj
;
2109 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2110 strlen (nameddest
));
2111 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2113 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2115 a
= desttoanchor (&dest
);
2117 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2120 printd ("emsg failed to parse destination `%s'\n",
2125 printd ("emsg destination `%s' not found\n",
2128 pdf_drop_obj (needle
);
2132 unlock ("reqlayout");
2133 printd ("continue %d", state
.pagecount
);
2135 else if (!strncmp ("page", p
, 4)) {
2140 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2142 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2147 page
= loadpage (pageno
, pindex
);
2151 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2153 else if (!strncmp ("tile", p
, 4)) {
2160 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2161 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2162 SCN_ptr_cast (&data
));
2164 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2169 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2173 printd ("tile %d %d %" FMT_ptr
" %u %f",
2175 FMT_ptr_cast (tile
),
2176 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2179 else if (!strncmp ("trimset", p
, 7)) {
2183 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2184 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2186 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
2189 state
.trimmargins
= trimmargins
;
2190 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2192 state
.trimfuzz
= fuzz
;
2196 else if (!strncmp ("settrim", p
, 7)) {
2200 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2201 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2203 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2207 state
.trimmargins
= trimmargins
;
2208 state
.needoutline
= 1;
2209 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2211 state
.trimfuzz
= fuzz
;
2213 state
.pagedimcount
= 0;
2214 free (state
.pagedims
);
2215 state
.pagedims
= NULL
;
2220 printd ("continue %d", state
.pagecount
);
2222 else if (!strncmp ("sliceh", p
, 6)) {
2225 ret
= sscanf (p
+ 6, " %d", &h
);
2227 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2229 if (h
!= state
.sliceheight
) {
2232 state
.sliceheight
= h
;
2233 for (i
= 0; i
< state
.texcount
; ++i
) {
2234 state
.texowners
[i
].w
= -1;
2235 state
.texowners
[i
].h
= -1;
2236 state
.texowners
[i
].slice
= NULL
;
2240 else if (!strncmp ("interrupt", p
, 9)) {
2241 printd ("vmsg interrupted");
2244 errx (1, "unknown command %.*s", len
, p
);
2250 CAMLprim value
ml_realloctexts (value texcount_v
)
2252 CAMLparam1 (texcount_v
);
2255 if (trylock ("ml_realloctexts")) {
2259 realloctexts (Int_val (texcount_v
));
2261 unlock ("ml_realloctexts");
2264 CAMLreturn (Val_bool (ok
));
2267 static void recti (int x0
, int y0
, int x1
, int y1
)
2269 GLfloat
*v
= state
.vertices
;
2271 glVertexPointer (2, GL_FLOAT
, 0, v
);
2272 v
[0] = x0
; v
[1] = y0
;
2273 v
[2] = x1
; v
[3] = y0
;
2274 v
[4] = x0
; v
[5] = y1
;
2275 v
[6] = x1
; v
[7] = y1
;
2276 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2279 static void showsel (struct page
*page
, int ox
, int oy
)
2285 fz_page_block
*pageb
;
2286 fz_text_block
*block
;
2287 struct mark first
, last
;
2288 unsigned char selcolor
[] = {15,15,15,140};
2290 first
= page
->fmark
;
2293 if (!first
.span
|| !last
.span
) return;
2295 glEnable (GL_BLEND
);
2296 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2297 glColor4ubv (selcolor
);
2299 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2300 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2301 for (pageb
= page
->text
->blocks
;
2302 pageb
< page
->text
->blocks
+ page
->text
->len
;
2304 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2305 block
= pageb
->u
.text
;
2307 for (line
= block
->lines
;
2308 line
< block
->lines
+ block
->len
;
2311 rect
= fz_empty_rect
;
2313 for (span
= line
->first_span
; span
; span
= span
->next
) {
2315 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2320 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2322 j
= MIN (first
.i
, last
.i
);
2323 k
= MAX (first
.i
, last
.i
);
2326 if (span
== first
.span
) {
2330 else if (span
== last
.span
) {
2337 for (i
= j
; i
<= k
; ++i
) {
2339 fz_union_rect (&rect
,
2340 fz_text_char_bbox (&bbox1
, span
, i
));
2342 fz_round_rect (&bbox
, &rect
);
2343 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2350 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2351 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2352 if (span
== last
.span
) {
2355 rect
= fz_empty_rect
;
2361 glDisable (GL_BLEND
);
2366 static void stipplerect (fz_matrix
*m
,
2374 fz_transform_point (p1
, m
);
2375 fz_transform_point (p2
, m
);
2376 fz_transform_point (p3
, m
);
2377 fz_transform_point (p4
, m
);
2383 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2387 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2389 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2390 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2392 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2393 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2395 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2396 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2398 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2399 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2401 glDrawArrays (GL_LINES
, 0, 8);
2404 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2407 fz_matrix ctm
, tm
, pm
;
2408 fz_link
*link
, *links
;
2409 GLfloat
*texcoords
= state
.texcoords
;
2410 GLfloat
*vertices
= state
.vertices
;
2412 switch (page
->type
) {
2414 links
= page
->u
.pdfpage
->links
;
2418 links
= page
->u
.xpspage
->links
;
2425 glEnable (GL_TEXTURE_1D
);
2426 glEnable (GL_BLEND
);
2427 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2428 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2430 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2431 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2432 fz_translate (&tm
, xoff
, yoff
);
2433 pm
= pagectm (page
);
2434 fz_concat (&ctm
, &pm
, &tm
);
2436 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2437 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2439 for (link
= links
; link
; link
= link
->next
) {
2440 fz_point p1
, p2
, p3
, p4
;
2442 p1
.x
= link
->rect
.x0
;
2443 p1
.y
= link
->rect
.y0
;
2445 p2
.x
= link
->rect
.x1
;
2446 p2
.y
= link
->rect
.y0
;
2448 p3
.x
= link
->rect
.x1
;
2449 p3
.y
= link
->rect
.y1
;
2451 p4
.x
= link
->rect
.x0
;
2452 p4
.y
= link
->rect
.y1
;
2454 switch (link
->dest
.kind
) {
2455 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2456 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2457 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2458 default: glColor3ub (0, 0, 0); break;
2460 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2463 for (i
= 0; i
< page
->annotcount
; ++i
) {
2464 fz_point p1
, p2
, p3
, p4
;
2465 struct annot
*annot
= &page
->annots
[i
];
2467 p1
.x
= annot
->bbox
.x0
;
2468 p1
.y
= annot
->bbox
.y0
;
2470 p2
.x
= annot
->bbox
.x1
;
2471 p2
.y
= annot
->bbox
.y0
;
2473 p3
.x
= annot
->bbox
.x1
;
2474 p3
.y
= annot
->bbox
.y1
;
2476 p4
.x
= annot
->bbox
.x0
;
2477 p4
.y
= annot
->bbox
.y1
;
2479 glColor3ub (0, 0, 128);
2480 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2483 glDisable (GL_BLEND
);
2484 glDisable (GL_TEXTURE_1D
);
2487 static int compareslinks (const void *l
, const void *r
)
2489 struct slink
const *ls
= l
;
2490 struct slink
const *rs
= r
;
2491 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2492 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2494 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2497 static void droptext (struct page
*page
)
2500 fz_free_text_page (state
.ctx
, page
->text
);
2503 page
->fmark
.span
= NULL
;
2504 page
->lmark
.span
= NULL
;
2508 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2513 static void dropanots (struct page
*page
)
2516 free (page
->annots
);
2517 page
->annots
= NULL
;
2518 page
->annotcount
= 0;
2522 static void ensureanots (struct page
*page
)
2526 size_t anotsize
= sizeof (*page
->annots
);
2529 if (state
.gen
!= page
->agen
) {
2531 page
->agen
= state
.gen
;
2533 if (page
->annots
) return;
2535 switch (page
->type
) {
2537 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2539 &state
.pagedims
[page
->pdimno
].tctm
,
2540 &state
.pagedims
[page
->pdimno
].ctm
);
2547 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
);
2549 annot
= pdf_next_annot (state
.u
.pdf
, annot
)) {
2554 page
->annotcount
= count
;
2555 page
->annots
= calloc (count
, anotsize
);
2556 if (!page
->annots
) {
2557 err (1, "calloc annots %d", count
);
2560 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
), i
= 0;
2562 annot
= pdf_next_annot (state
.u
.pdf
, annot
), i
++) {
2565 pdf_bound_annot (state
.u
.pdf
, annot
, &rect
);
2566 fz_transform_rect (&rect
, &ctm
);
2567 page
->annots
[i
].annot
= annot
;
2568 fz_round_rect (&page
->annots
[i
].bbox
, &annot
->pagerect
);
2573 static void dropslinks (struct page
*page
)
2576 free (page
->slinks
);
2577 page
->slinks
= NULL
;
2578 page
->slinkcount
= 0;
2582 static void ensureslinks (struct page
*page
)
2586 size_t slinksize
= sizeof (*page
->slinks
);
2587 fz_link
*link
, *links
;
2590 if (state
.gen
!= page
->sgen
) {
2592 page
->sgen
= state
.gen
;
2594 if (page
->slinks
) return;
2596 switch (page
->type
) {
2598 links
= page
->u
.pdfpage
->links
;
2599 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2601 &state
.pagedims
[page
->pdimno
].tctm
,
2602 &state
.pagedims
[page
->pdimno
].ctm
);
2606 links
= page
->u
.xpspage
->links
;
2607 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2614 count
= page
->annotcount
;
2615 for (link
= links
; link
; link
= link
->next
) {
2621 page
->slinkcount
= count
;
2622 page
->slinks
= calloc (count
, slinksize
);
2623 if (!page
->slinks
) {
2624 err (1, "calloc slinks %d", count
);
2627 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2631 fz_transform_rect (&rect
, &ctm
);
2632 page
->slinks
[i
].tag
= SLINK
;
2633 page
->slinks
[i
].u
.link
= link
;
2634 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2636 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2639 rect
= page
->annots
[j
].annot
->pagerect
;
2640 fz_transform_rect (&rect
, &ctm
);
2641 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2643 page
->slinks
[i
].tag
= SANNOT
;
2644 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2646 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2650 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2651 static void fmt_linkn (char *s
, unsigned int u
)
2653 unsigned int len
; unsigned int q
;
2654 unsigned int zma
= 'z' - 'a' + 1;
2656 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2659 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2660 /* handles u == 0 */
2665 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2666 int noff
, char *targ
, int tlen
, int hfsize
)
2670 struct slink
*slink
;
2671 double x0
, y0
, x1
, y1
, w
;
2673 ensureslinks (page
);
2674 glColor3ub (0xc3, 0xb0, 0x91);
2675 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2676 fmt_linkn (buf
, i
+ noff
);
2677 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2678 slink
= &page
->slinks
[i
];
2680 x0
= slink
->bbox
.x0
+ xoff
- 5;
2681 y1
= slink
->bbox
.y0
+ yoff
- 5;
2682 y0
= y1
+ 10 + hfsize
;
2683 w
= measure_string (state
.face
, hfsize
, buf
);
2685 recti (x0
, y0
, x1
, y1
);
2689 glEnable (GL_BLEND
);
2690 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2691 glEnable (GL_TEXTURE_2D
);
2692 glColor3ub (0, 0, 0);
2693 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2694 fmt_linkn (buf
, i
+ noff
);
2695 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2696 slink
= &page
->slinks
[i
];
2698 x0
= slink
->bbox
.x0
+ xoff
;
2699 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2700 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2703 glDisable (GL_TEXTURE_2D
);
2704 glDisable (GL_BLEND
);
2707 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2710 struct slice
*slice1
;
2711 unsigned char *texdata
;
2714 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2715 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2717 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2718 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2719 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2723 int texindex
= state
.texindex
++ % state
.texcount
;
2725 if (state
.texowners
[texindex
].w
== tile
->w
) {
2726 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2730 state
.texowners
[texindex
].h
= slice
->h
;
2734 state
.texowners
[texindex
].h
= slice
->h
;
2737 state
.texowners
[texindex
].w
= tile
->w
;
2738 state
.texowners
[texindex
].slice
= slice
;
2739 slice
->texindex
= texindex
;
2741 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2743 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2747 texdata
= tile
->pixmap
->samples
;
2750 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2762 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2774 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2779 CAMLprim value
ml_begintiles (value unit_v
)
2781 CAMLparam1 (unit_v
);
2782 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2783 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2784 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2785 CAMLreturn (unit_v
);
2788 CAMLprim value
ml_endtiles (value unit_v
)
2790 CAMLparam1 (unit_v
);
2791 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2792 CAMLreturn (unit_v
);
2795 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2797 CAMLparam2 (args_v
, ptr_v
);
2798 int dispx
= Int_val (Field (args_v
, 0));
2799 int dispy
= Int_val (Field (args_v
, 1));
2800 int dispw
= Int_val (Field (args_v
, 2));
2801 int disph
= Int_val (Field (args_v
, 3));
2802 int tilex
= Int_val (Field (args_v
, 4));
2803 int tiley
= Int_val (Field (args_v
, 5));
2804 char *s
= String_val (ptr_v
);
2805 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2806 int slicey
, firstslice
;
2807 struct slice
*slice
;
2808 GLfloat
*texcoords
= state
.texcoords
;
2809 GLfloat
*vertices
= state
.vertices
;
2811 firstslice
= tiley
/ tile
->sliceheight
;
2812 slice
= &tile
->slices
[firstslice
];
2813 slicey
= tiley
% tile
->sliceheight
;
2818 dh
= slice
->h
- slicey
;
2819 dh
= MIN (disph
, dh
);
2820 uploadslice (tile
, slice
);
2822 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2823 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2824 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2825 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2827 vertices
[0] = dispx
; vertices
[1] = dispy
;
2828 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2829 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2830 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2832 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2836 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2839 CAMLreturn (Val_unit
);
2842 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2843 value xoff_v
, value yoff_v
,
2846 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2847 int xoff
= Int_val (xoff_v
);
2848 int yoff
= Int_val (yoff_v
);
2849 int noff
= Int_val (Field (li_v
, 0));
2850 char *targ
= String_val (Field (li_v
, 1));
2851 int tlen
= caml_string_length (Field (li_v
, 1));
2852 int hfsize
= Int_val (Field (li_v
, 2));
2853 char *s
= String_val (ptr_v
);
2854 int hlmask
= Int_val (hlinks_v
);
2855 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2858 /* deal with loadpage failed pages */
2864 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2865 if (trylock ("ml_postprocess")) {
2870 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2871 noff
= page
->slinkcount
;
2873 if (page
->tgen
== state
.gen
) {
2874 showsel (page
, xoff
, yoff
);
2876 unlock ("ml_postprocess");
2879 CAMLreturn (Val_int (noff
));
2882 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2887 const fz_matrix
*tctm
;
2889 if (!page
->annots
) return NULL
;
2891 switch (page
->type
) {
2893 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2894 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2898 tctm
= &fz_identity
;
2908 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2909 fz_invert_matrix (&ctm
, &ctm
);
2910 fz_transform_point (&p
, &ctm
);
2912 for (i
= 0; i
< page
->annotcount
; ++i
) {
2913 struct annot
*a
= &page
->annots
[i
];
2914 if (p
.x
>= a
->annot
->pagerect
.x0
&& p
.x
<= a
->annot
->pagerect
.x1
) {
2915 if (p
.y
>= a
->annot
->pagerect
.y0
&& p
.y
<= a
->annot
->pagerect
.y1
) {
2923 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2927 const fz_matrix
*tctm
;
2928 fz_link
*link
, *links
;
2930 switch (page
->type
) {
2932 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2933 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2934 links
= page
->u
.pdfpage
->links
;
2938 tctm
= &fz_identity
;
2939 links
= page
->u
.xpspage
->links
;
2948 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2949 fz_invert_matrix (&ctm
, &ctm
);
2950 fz_transform_point (&p
, &ctm
);
2952 for (link
= links
; link
; link
= link
->next
) {
2953 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2954 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2962 static void ensuretext (struct page
*page
)
2964 if (state
.gen
!= page
->tgen
) {
2966 page
->tgen
= state
.gen
;
2972 page
->text
= fz_new_text_page (state
.ctx
);
2973 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2974 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2975 ctm
= pagectm (page
);
2976 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2977 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2978 qsort (page
->text
->blocks
, page
->text
->len
,
2979 sizeof (*page
->text
->blocks
), compareblocks
);
2981 fz_free_device (tdev
);
2985 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2987 CAMLparam2 (start_page_v
, dir_v
);
2989 int i
, dir
= Int_val (dir_v
);
2990 int start_page
= Int_val (start_page_v
);
2991 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2993 ret_v
= Val_int (0);
2994 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2998 lock ("ml_findpage_with_links");
2999 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
3002 switch (state
.type
) {
3005 pdf_page
*page
= NULL
;
3007 fz_try (state
.ctx
) {
3008 page
= pdf_load_page (state
.u
.pdf
, i
);
3009 found
= !!page
->links
|| !!page
->annots
;
3011 fz_catch (state
.ctx
) {
3021 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
3022 found
= !!page
->links
;
3028 ARSERT (0 && "invalid document type");
3032 ret_v
= caml_alloc_small (1, 1);
3033 Field (ret_v
, 0) = Val_int (i
);
3038 unlock ("ml_findpage_with_links");
3044 enum { dir_first
, dir_last
};
3045 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
3047 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
3049 CAMLparam2 (ptr_v
, dir_v
);
3050 CAMLlocal2 (ret_v
, pos_v
);
3052 int dirtag
, i
, slinkindex
;
3053 struct slink
*found
= NULL
,*slink
;
3054 char *s
= String_val (ptr_v
);
3056 page
= parse_pointer ("ml_findlink", s
);
3057 ret_v
= Val_int (0);
3058 /* This is scary we are not taking locks here ensureslinks does
3059 not modify state and given that we obtained the page it can not
3060 disappear under us either */
3061 lock ("ml_findlink");
3062 ensureslinks (page
);
3064 if (Is_block (dir_v
)) {
3065 dirtag
= Tag_val (dir_v
);
3067 case dir_first_visible
:
3069 int x0
, y0
, dir
, first_index
, last_index
;
3071 pos_v
= Field (dir_v
, 0);
3072 x0
= Int_val (Field (pos_v
, 0));
3073 y0
= Int_val (Field (pos_v
, 1));
3074 dir
= Int_val (Field (pos_v
, 2));
3079 last_index
= page
->slinkcount
;
3082 first_index
= page
->slinkcount
- 1;
3086 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
3087 slink
= &page
->slinks
[i
];
3088 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
3097 slinkindex
= Int_val (Field (dir_v
, 0));
3098 found
= &page
->slinks
[slinkindex
];
3099 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3100 slink
= &page
->slinks
[i
];
3101 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
3109 slinkindex
= Int_val (Field (dir_v
, 0));
3110 found
= &page
->slinks
[slinkindex
];
3111 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3112 slink
= &page
->slinks
[i
];
3113 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
3121 slinkindex
= Int_val (Field (dir_v
, 0));
3122 found
= &page
->slinks
[slinkindex
];
3123 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3124 slink
= &page
->slinks
[i
];
3125 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
3133 slinkindex
= Int_val (Field (dir_v
, 0));
3134 found
= &page
->slinks
[slinkindex
];
3135 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3136 slink
= &page
->slinks
[i
];
3137 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
3146 dirtag
= Int_val (dir_v
);
3149 found
= page
->slinks
;
3154 found
= page
->slinks
+ (page
->slinkcount
- 1);
3160 ret_v
= caml_alloc_small (2, 1);
3161 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
3164 unlock ("ml_findlink");
3168 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
3169 unamed
, uremote
, uremotedest
, uannot
};
3175 switch (link->dest.kind) { \
3176 case FZ_LINK_GOTO: \
3180 pageno = link->dest.ld.gotor.page; \
3184 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
3185 p.y = link->dest.ld.gotor.lt.y; \
3186 fz_transform_point (&p, &pdim->lctm); \
3187 if (p.y < 0) p.y = 0; \
3189 tup_v = caml_alloc_tuple (2); \
3190 ret_v = caml_alloc_small (1, ugoto); \
3191 Field (tup_v, 0) = Val_int (pageno); \
3192 Field (tup_v, 1) = Val_int (p.y); \
3193 Field (ret_v, 0) = tup_v; \
3198 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3199 ret_v = caml_alloc_small (1, uuri); \
3200 Field (ret_v, 0) = str_v; \
3203 case FZ_LINK_LAUNCH: \
3204 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3205 ret_v = caml_alloc_small (1, ulaunch); \
3206 Field (ret_v, 0) = str_v; \
3209 case FZ_LINK_NAMED: \
3210 str_v = caml_copy_string (link->dest.ld.named.named); \
3211 ret_v = caml_alloc_small (1, unamed); \
3212 Field (ret_v, 0) = str_v; \
3215 case FZ_LINK_GOTOR: \
3219 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3220 pageno = link->dest.ld.gotor.page; \
3221 if (pageno == -1) { \
3222 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3223 rty = uremotedest; \
3226 gr_v = Val_int (pageno); \
3229 tup_v = caml_alloc_tuple (2); \
3230 ret_v = caml_alloc_small (1, rty); \
3231 Field (tup_v, 0) = str_v; \
3232 Field (tup_v, 1) = gr_v; \
3233 Field (ret_v, 0) = tup_v; \
3241 snprintf (buf, sizeof (buf), \
3242 "unhandled link kind %d", link->dest.kind); \
3243 str_v = caml_copy_string (buf); \
3244 ret_v = caml_alloc_small (1, uunexpected); \
3245 Field (ret_v, 0) = str_v; \
3251 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3253 CAMLparam2 (ptr_v
, n_v
);
3254 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3257 struct pagedim
*pdim
;
3258 char *s
= String_val (ptr_v
);
3259 struct slink
*slink
;
3261 /* See ml_findlink for caveat */
3263 ret_v
= Val_int (0);
3264 page
= parse_pointer ("ml_getlink", s
);
3265 ensureslinks (page
);
3266 pdim
= &state
.pagedims
[page
->pdimno
];
3267 slink
= &page
->slinks
[Int_val (n_v
)];
3268 if (slink
->tag
== SLINK
) {
3269 link
= slink
->u
.link
;
3273 ret_v
= caml_alloc_small (1, uannot
);
3274 tup_v
= caml_alloc_tuple (2);
3275 Field (ret_v
, 0) = tup_v
;
3276 Field (tup_v
, 0) = ptr_v
;
3277 Field (tup_v
, 1) = n_v
;
3280 unlock ("ml_getlink");
3284 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
3286 CAMLparam2 (ptr_v
, n_v
);
3287 char *s
= String_val (ptr_v
);
3289 struct slink
*slink
;
3291 page
= parse_pointer ("ml_getannotcontent", s
);
3292 slink
= &page
->slinks
[Int_val (n_v
)];
3293 CAMLreturn (caml_copy_string (pdf_annot_contents (state
.u
.pdf
,
3297 CAMLprim value
ml_getlinkcount (value ptr_v
)
3301 char *s
= String_val (ptr_v
);
3303 page
= parse_pointer ("ml_getlinkcount", s
);
3304 CAMLreturn (Val_int (page
->slinkcount
));
3307 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3309 CAMLparam2 (ptr_v
, n_v
);
3312 struct slink
*slink
;
3313 char *s
= String_val (ptr_v
);
3314 /* See ml_findlink for caveat */
3316 page
= parse_pointer ("ml_getlinkrect", s
);
3317 ret_v
= caml_alloc_tuple (4);
3318 ensureslinks (page
);
3320 slink
= &page
->slinks
[Int_val (n_v
)];
3321 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3322 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3323 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3324 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3325 unlock ("ml_getlinkrect");
3329 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3331 CAMLparam3 (ptr_v
, x_v
, y_v
);
3332 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3334 struct annot
*annot
;
3336 char *ptr
= String_val (ptr_v
);
3337 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3338 struct pagedim
*pdim
;
3340 ret_v
= Val_int (0);
3341 if (trylock ("ml_whatsunder")) {
3345 page
= parse_pointer ("ml_whatsunder", ptr
);
3346 pdim
= &state
.pagedims
[page
->pdimno
];
3347 x
+= pdim
->bounds
.x0
;
3348 y
+= pdim
->bounds
.y0
;
3350 if (state
.type
== DPDF
) {
3351 annot
= getannot (page
, x
, y
);
3355 ensureslinks (page
);
3356 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3357 if (page
->slinks
[i
].tag
== SANNOT
3358 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3363 ret_v
= caml_alloc_small (1, uannot
);
3364 tup_v
= caml_alloc_tuple (2);
3365 Field (ret_v
, 0) = tup_v
;
3366 Field (tup_v
, 0) = ptr_v
;
3367 Field (tup_v
, 1) = Val_int (n
);
3372 link
= getlink (page
, x
, y
);
3378 fz_page_block
*pageb
;
3379 fz_text_block
*block
;
3382 for (pageb
= page
->text
->blocks
;
3383 pageb
< page
->text
->blocks
+ page
->text
->len
;
3386 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3387 block
= pageb
->u
.text
;
3390 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3393 for (line
= block
->lines
;
3394 line
< block
->lines
+ block
->len
;
3399 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3402 for (span
= line
->first_span
; span
; span
= span
->next
) {
3406 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3409 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3411 fz_text_char_bbox (&bbox
, span
, charnum
);
3414 if (x
>= b
->x0
&& x
<= b
->x1
3415 && y
>= b
->y0
&& y
<= b
->y1
) {
3416 fz_text_style
*style
= span
->text
->style
;
3418 style
->font
&& style
->font
->name
3420 : "Span has no font name"
3422 FT_FaceRec
*face
= style
->font
->ft_face
;
3423 if (face
&& face
->family_name
) {
3425 char *n1
= face
->family_name
;
3426 size_t l1
= strlen (n1
);
3427 size_t l2
= strlen (n2
);
3429 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3430 s
= malloc (l1
+ l2
+ 2);
3434 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3435 str_v
= caml_copy_string (s
);
3440 if (str_v
== Val_unit
) {
3441 str_v
= caml_copy_string (n2
);
3443 ret_v
= caml_alloc_small (1, utext
);
3444 Field (ret_v
, 0) = str_v
;
3453 unlock ("ml_whatsunder");
3459 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3461 static int uninteresting (int c
)
3463 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3467 CAMLprim value
ml_clearmark (value ptr_v
)
3470 char *s
= String_val (ptr_v
);
3473 if (trylock ("ml_clearmark")) {
3477 page
= parse_pointer ("ml_clearmark", s
);
3478 page
->fmark
.span
= NULL
;
3479 page
->lmark
.span
= NULL
;
3483 unlock ("ml_clearmark");
3485 CAMLreturn (Val_unit
);
3488 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3490 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3495 fz_page_block
*pageb
;
3496 fz_text_block
*block
;
3497 struct pagedim
*pdim
;
3498 int mark
= Int_val (mark_v
);
3499 char *s
= String_val (ptr_v
);
3500 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3502 ret_v
= Val_bool (0);
3503 if (trylock ("ml_markunder")) {
3507 page
= parse_pointer ("ml_markunder", s
);
3508 pdim
= &state
.pagedims
[page
->pdimno
];
3512 if (mark
== mark_page
) {
3514 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3516 for (i
= 0; i
< page
->text
->len
; ++i
) {
3517 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3518 pb1
= &page
->text
->blocks
[i
];
3522 if (!pb1
) goto unlock
;
3524 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3525 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3526 pb2
= &page
->text
->blocks
[i
];
3530 if (!pb2
) goto unlock
;
3532 block
= pb1
->u
.text
;
3535 page
->fmark
.span
= block
->lines
->first_span
;
3537 block
= pb2
->u
.text
;
3538 line
= &block
->lines
[block
->len
- 1];
3539 page
->lmark
.i
= line
->last_span
->len
- 1;
3540 page
->lmark
.span
= line
->last_span
;
3541 ret_v
= Val_bool (1);
3545 x
+= pdim
->bounds
.x0
;
3546 y
+= pdim
->bounds
.y0
;
3548 for (pageb
= page
->text
->blocks
;
3549 pageb
< page
->text
->blocks
+ page
->text
->len
;
3551 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3552 block
= pageb
->u
.text
;
3555 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3558 if (mark
== mark_block
) {
3560 page
->fmark
.span
= block
->lines
->first_span
;
3562 line
= &block
->lines
[block
->len
- 1];
3563 page
->lmark
.i
= line
->last_span
->len
- 1;
3564 page
->lmark
.span
= line
->last_span
;
3565 ret_v
= Val_bool (1);
3569 for (line
= block
->lines
;
3570 line
< block
->lines
+ block
->len
;
3575 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3578 if (mark
== mark_line
) {
3580 page
->fmark
.span
= line
->first_span
;
3582 page
->lmark
.i
= line
->last_span
->len
- 1;
3583 page
->lmark
.span
= line
->last_span
;
3584 ret_v
= Val_bool (1);
3588 for (span
= line
->first_span
; span
; span
= span
->next
) {
3592 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3595 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3597 fz_text_char_bbox (&bbox
, span
, charnum
);
3600 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3602 int charnum2
, charnum3
= -1, charnum4
= -1;
3604 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3606 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3607 if (uninteresting (span
->text
[charnum2
].c
)) {
3608 charnum3
= charnum2
+ 1;
3612 if (charnum3
== -1) charnum3
= 0;
3614 for (charnum2
= charnum
+ 1;
3615 charnum2
< span
->len
;
3617 if (uninteresting (span
->text
[charnum2
].c
)) break;
3618 charnum4
= charnum2
;
3620 if (charnum4
== -1) goto unlock
;
3622 page
->fmark
.i
= charnum3
;
3623 page
->fmark
.span
= span
;
3625 page
->lmark
.i
= charnum4
;
3626 page
->lmark
.span
= span
;
3627 ret_v
= Val_bool (1);
3635 if (!Bool_val (ret_v
)) {
3636 page
->fmark
.span
= NULL
;
3637 page
->lmark
.span
= NULL
;
3641 unlock ("ml_markunder");
3647 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3649 CAMLparam3 (ptr_v
, x_v
, y_v
);
3650 CAMLlocal2 (ret_v
, res_v
);
3653 fz_page_block
*pageb
;
3654 struct pagedim
*pdim
;
3655 char *s
= String_val (ptr_v
);
3656 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3658 ret_v
= Val_int (0);
3659 if (trylock ("ml_rectofblock")) {
3663 page
= parse_pointer ("ml_rectofblock", s
);
3664 pdim
= &state
.pagedims
[page
->pdimno
];
3665 x
+= pdim
->bounds
.x0
;
3666 y
+= pdim
->bounds
.y0
;
3670 for (pageb
= page
->text
->blocks
;
3671 pageb
< page
->text
->blocks
+ page
->text
->len
;
3673 switch (pageb
->type
) {
3674 case FZ_PAGE_BLOCK_TEXT
:
3675 b
= &pageb
->u
.text
->bbox
;
3678 case FZ_PAGE_BLOCK_IMAGE
:
3679 b
= &pageb
->u
.image
->bbox
;
3686 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3691 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3692 ret_v
= caml_alloc_small (1, 1);
3693 Store_double_field (res_v
, 0, b
->x0
);
3694 Store_double_field (res_v
, 1, b
->x1
);
3695 Store_double_field (res_v
, 2, b
->y0
);
3696 Store_double_field (res_v
, 3, b
->y1
);
3697 Field (ret_v
, 0) = res_v
;
3699 unlock ("ml_rectofblock");
3705 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3707 CAMLparam2 (ptr_v
, rect_v
);
3710 struct pagedim
*pdim
;
3711 char *s
= String_val (ptr_v
);
3712 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3714 fz_page_block
*pageb
;
3715 fz_text_block
*block
;
3716 fz_text_span
*span
, *fspan
, *lspan
;
3718 if (trylock ("ml_seltext")) {
3722 page
= parse_pointer ("ml_seltext", s
);
3725 pdim
= &state
.pagedims
[page
->pdimno
];
3726 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3727 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3728 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3729 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3740 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3741 glColor3ub (128, 128, 128);
3742 recti (x0
, y0
, x1
, y1
);
3743 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3747 fspan
= page
->fmark
.span
;
3750 lspan
= page
->lmark
.span
;
3752 for (pageb
= page
->text
->blocks
;
3753 pageb
< page
->text
->blocks
+ page
->text
->len
;
3755 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3756 block
= pageb
->u
.text
;
3757 for (line
= block
->lines
;
3758 line
< block
->lines
+ block
->len
;
3761 for (span
= line
->first_span
; span
; span
= span
->next
) {
3762 for (i
= 0; i
< span
->len
; ++i
) {
3765 fz_text_char_bbox (&b
, span
, i
);
3767 if (x0
>= b
.x0
&& x0
<= b
.x1
3768 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3773 if (x1
>= b
.x0
&& x1
<= b
.x1
3774 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3779 if (0 && selected
) {
3780 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3781 glColor3ub (128, 128, 128);
3782 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3783 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3789 if (x1
< x0
&& fspan
== lspan
) {
3801 page
->fmark
.span
= fspan
;
3804 page
->lmark
.span
= lspan
;
3806 unlock ("ml_seltext");
3809 CAMLreturn (Val_unit
);
3812 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3817 for (i
= a
; i
<= b
; ++i
) {
3818 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3819 ret
= fwrite (buf
, len
, 1, f
);
3822 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3823 len
, ret
, strerror (errno
));
3831 CAMLprim value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3833 caml_failwith ("ml_popen not implemented under Cygwin");
3836 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3838 CAMLparam2 (command_v
, fds_v
);
3839 CAMLlocal2 (l_v
, tup_v
);
3843 value earg_v
= Nothing
;
3844 posix_spawnattr_t attr
;
3845 posix_spawn_file_actions_t fa
;
3846 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3848 argv
[2] = String_val (command_v
);
3850 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3851 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3854 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3855 msg
= "posix_spawnattr_init";
3859 #ifdef POSIX_SPAWN_USEVFORK
3860 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3861 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3866 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3869 tup_v
= Field (l_v
, 0);
3870 fd1
= Int_val (Field (tup_v
, 0));
3871 fd2
= Int_val (Field (tup_v
, 1));
3873 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3874 msg
= "posix_spawn_file_actions_addclose";
3880 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3881 msg
= "posix_spawn_file_actions_adddup2";
3888 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3889 msg
= "posix_spawn";
3894 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3895 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3899 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3900 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3905 unix_error (ret
, msg
, earg_v
);
3907 CAMLreturn (Val_int (pid
));
3911 CAMLprim value
ml_hassel (value ptr_v
)
3916 char *s
= String_val (ptr_v
);
3918 ret_v
= Val_bool (0);
3919 if (trylock ("ml_hassel")) {
3923 page
= parse_pointer ("ml_hassel", s
);
3924 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3925 unlock ("ml_hassel");
3930 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3932 CAMLparam2 (fd_v
, ptr_v
);
3937 fz_page_block
*pageb
;
3938 fz_text_block
*block
;
3939 int fd
= Int_val (fd_v
);
3940 char *s
= String_val (ptr_v
);
3942 if (trylock ("ml_copysel")) {
3946 page
= parse_pointer ("ml_copysel", s
);
3948 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3949 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3953 f
= fdopen (fd
, "w");
3955 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3956 fd
, strerror (errno
));
3960 for (pageb
= page
->text
->blocks
;
3961 pageb
< page
->text
->blocks
+ page
->text
->len
;
3963 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3964 block
= pageb
->u
.text
;
3965 for (line
= block
->lines
;
3966 line
< block
->lines
+ block
->len
;
3970 for (span
= line
->first_span
; span
; span
= span
->next
) {
3973 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3974 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3975 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3978 if (pipespan (f
, span
, a
, b
)) {
3981 if (span
== page
->lmark
.span
) {
3984 if (span
== line
->last_span
) {
3985 if (putc ('\n', f
) == EOF
) {
3987 "failed break line on sel pipe: %s\n",
3998 int ret
= fclose (f
);
4001 if (errno
!= ECHILD
) {
4002 fprintf (stderr
, "failed to close sel pipe: %s\n",
4008 unlock ("ml_copysel");
4013 fprintf (stderr
, "failed to close sel pipe: %s\n",
4017 CAMLreturn (Val_unit
);
4020 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
4022 CAMLparam1 (pagedimno_v
);
4024 int pagedimno
= Int_val (pagedimno_v
);
4027 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
4028 if (trylock ("ml_getpdimrect")) {
4029 box
= fz_empty_rect
;
4032 box
= state
.pagedims
[pagedimno
].mediabox
;
4033 unlock ("ml_getpdimrect");
4036 Store_double_field (ret_v
, 0, box
.x0
);
4037 Store_double_field (ret_v
, 1, box
.x1
);
4038 Store_double_field (ret_v
, 2, box
.y0
);
4039 Store_double_field (ret_v
, 3, box
.y1
);
4044 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
4045 value dw_v
, value cols_v
)
4047 CAMLparam3 (winw_v
, winh_v
, dw_v
);
4053 double winw
= Int_val (winw_v
);
4054 double winh
= Int_val (winh_v
);
4055 double dw
= Int_val (dw_v
);
4056 double cols
= Int_val (cols_v
);
4057 double pw
= 1.0, ph
= 1.0;
4059 if (trylock ("ml_zoom_for_height")) {
4063 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
4064 double w
= p
->pagebox
.x1
/ cols
;
4065 double h
= p
->pagebox
.y1
;
4069 if (state
.fitmodel
!= FitProportional
) pw
= w
;
4071 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
4074 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
4075 unlock ("ml_zoom_for_height");
4077 ret_v
= caml_copy_double (zoom
);
4081 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
4083 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
4085 int pt
= Int_val(pt_v
);
4086 int x
= Int_val (x_v
);
4087 int y
= Int_val (y_v
);
4090 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
4091 ret_v
= caml_copy_double (w
);
4095 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
4097 CAMLparam2 (pt_v
, string_v
);
4099 int pt
= Int_val (pt_v
);
4102 w
= measure_string (state
.face
, pt
, String_val (string_v
));
4103 ret_v
= caml_copy_double (w
);
4107 CAMLprim value
ml_getpagebox (value opaque_v
)
4109 CAMLparam1 (opaque_v
);
4115 char *s
= String_val (opaque_v
);
4116 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
4118 ret_v
= caml_alloc_tuple (4);
4119 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
4120 dev
->hints
|= FZ_IGNORE_SHADE
;
4122 switch (page
->type
) {
4124 ctm
= pagectm (page
);
4125 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
4129 ctm
= pagectm (page
);
4130 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
4134 rect
= fz_infinite_rect
;
4138 fz_free_device (dev
);
4139 fz_round_rect (&bbox
, &rect
);
4140 Field (ret_v
, 0) = Val_int (bbox
.x0
);
4141 Field (ret_v
, 1) = Val_int (bbox
.y0
);
4142 Field (ret_v
, 2) = Val_int (bbox
.x1
);
4143 Field (ret_v
, 3) = Val_int (bbox
.y1
);
4148 CAMLprim value
ml_setaalevel (value level_v
)
4150 CAMLparam1 (level_v
);
4152 state
.aalevel
= Int_val (level_v
);
4153 CAMLreturn (Val_unit
);
4156 #pragma GCC diagnostic push
4157 #pragma GCC diagnostic ignored "-Wvariadic-macros"
4158 #include <X11/Xlib.h>
4159 #pragma GCC diagnostic pop
4167 XVisualInfo
*visual
;
4170 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4172 CAMLparam3 (display_v
, wid_v
, screen_v
);
4173 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4175 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4177 caml_failwith ("XOpenDisplay");
4180 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4182 XCloseDisplay (glx
.dpy
);
4183 caml_failwith ("glXChooseVisual");
4186 glx
.wid
= Int_val (wid_v
);
4187 CAMLreturn (Val_int (glx
.visual
->visualid
));
4190 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4192 CAMLparam1 (unit_v
);
4194 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4196 caml_failwith ("glXCreateContext");
4202 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4203 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4205 caml_failwith ("glXMakeCurrent");
4207 CAMLreturn (Val_unit
);
4210 CAMLprim value
ml_swapb (value unit_v
)
4212 CAMLparam1 (unit_v
);
4213 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4214 CAMLreturn (Val_unit
);
4217 #include "keysym2ucs.c"
4219 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4221 CAMLparam1 (keysym_v
);
4223 KeySym keysym
= Int_val (keysym_v
);
4228 rune
= keysym2ucs (keysym
);
4229 len
= fz_runetochar (buf
, rune
);
4231 str_v
= caml_copy_string (buf
);
4235 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4237 CAMLprim value
ml_platform (value unit_v
)
4239 CAMLparam1 (unit_v
);
4240 CAMLlocal2 (tup_v
, arr_v
);
4241 int platid
= piunknown
;
4244 #if defined __linux__
4246 #elif defined __CYGWIN__
4248 #elif defined __DragonFly__ || defined __FreeBSD__
4249 || defined __OpenBSD__
|| defined __NetBSD__
4251 #elif defined __sun__
4253 #elif defined __APPLE__
4256 if (uname (&buf
)) err (1, "uname");
4258 tup_v
= caml_alloc_tuple (2);
4260 char const *sar
[] = {
4267 arr_v
= caml_copy_string_array (sar
);
4269 Field (tup_v
, 0) = Val_int (platid
);
4270 Field (tup_v
, 1) = arr_v
;
4274 CAMLprim value
ml_cloexec (value fd_v
)
4277 int fd
= Int_val (fd_v
);
4279 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4280 uerror ("fcntl", Nothing
);
4282 CAMLreturn (Val_unit
);
4285 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4287 CAMLparam2 (w_v
, h_v
);
4290 int w
= Int_val (w_v
);
4291 int h
= Int_val (h_v
);
4292 int cs
= Int_val (cs_v
);
4294 if (state
.pbo_usable
) {
4295 pbo
= calloc (sizeof (*pbo
), 1);
4297 err (1, "calloc pbo");
4309 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4312 state
.glGenBuffersARB (1, &pbo
->id
);
4313 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4314 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4315 NULL
, GL_STREAM_DRAW
);
4316 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4318 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4320 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4321 state
.glDeleteBuffersARB (1, &pbo
->id
);
4323 ret_v
= caml_copy_string ("0");
4329 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4331 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4335 err (1, "malloc %d bytes failed", res
+1);
4337 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4339 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4341 ret_v
= caml_copy_string (s
);
4346 ret_v
= caml_copy_string ("0");
4351 CAMLprim value
ml_freepbo (value s_v
)
4354 char *s
= String_val (s_v
);
4355 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4358 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4360 tile
->pbo
->ptr
= NULL
;
4361 tile
->pbo
->size
= -1;
4363 CAMLreturn (Val_unit
);
4366 CAMLprim value
ml_unmappbo (value s_v
)
4369 char *s
= String_val (s_v
);
4370 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4373 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4374 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4375 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4377 tile
->pbo
->ptr
= NULL
;
4378 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4380 CAMLreturn (Val_unit
);
4383 static void setuppbo (void)
4385 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4386 state
.pbo_usable
= GGPA (glBindBufferARB
)
4387 && GGPA (glUnmapBufferARB
)
4388 && GGPA (glMapBufferARB
)
4389 && GGPA (glBufferDataARB
)
4390 && GGPA (glGenBuffersARB
)
4391 && GGPA (glDeleteBuffersARB
);
4395 CAMLprim value
ml_pbo_usable (value unit_v
)
4397 CAMLparam1 (unit_v
);
4398 CAMLreturn (Val_bool (state
.pbo_usable
));
4401 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4403 CAMLparam3 (ptr_v
, x_v
, y_v
);
4404 CAMLlocal2 (ret_v
, tup_v
);
4406 char *s
= String_val (ptr_v
);
4407 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4408 struct pagedim
*pdim
;
4412 page
= parse_pointer ("ml_unproject", s
);
4413 pdim
= &state
.pagedims
[page
->pdimno
];
4415 ret_v
= Val_int (0);
4416 if (trylock ("ml_unproject")) {
4420 switch (page
->type
) {
4422 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4428 p
.x
= x
+ pdim
->bounds
.x0
;
4429 p
.y
= y
+ pdim
->bounds
.y0
;
4431 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4432 fz_invert_matrix (&ctm
, &ctm
);
4433 fz_transform_point (&p
, &ctm
);
4435 tup_v
= caml_alloc_tuple (2);
4436 ret_v
= caml_alloc_small (1, 1);
4437 Field (tup_v
, 0) = Val_int (p
.x
);
4438 Field (tup_v
, 1) = Val_int (p
.y
);
4439 Field (ret_v
, 0) = tup_v
;
4441 unlock ("ml_unproject");
4446 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4449 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4451 if (state
.type
== DPDF
) {
4456 char *s
= String_val (ptr_v
);
4459 page
= parse_pointer ("ml_addannot", s
);
4460 annot
= pdf_create_annot (pdf
, page
->u
.pdfpage
, FZ_ANNOT_TEXT
);
4461 p
.x
= Int_val (x_v
);
4462 p
.y
= Int_val (y_v
);
4463 pdf_set_annot_contents (pdf
, annot
, String_val (contents_v
));
4464 pdf_set_text_annot_position (pdf
, annot
, p
);
4467 CAMLreturn (Val_unit
);
4470 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4472 CAMLparam2 (ptr_v
, n_v
);
4474 if (state
.type
== DPDF
) {
4476 char *s
= String_val (ptr_v
);
4477 struct slink
*slink
;
4479 page
= parse_pointer ("ml_delannot", s
);
4480 slink
= &page
->slinks
[Int_val (n_v
)];
4481 pdf_delete_annot (state
.u
.pdf
, page
->u
.pdfpage
, slink
->u
.annot
);
4484 CAMLreturn (Val_unit
);
4487 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4489 CAMLparam3 (ptr_v
, n_v
, str_v
);
4491 if (state
.type
== DPDF
) {
4493 char *s
= String_val (ptr_v
);
4494 struct slink
*slink
;
4496 page
= parse_pointer ("ml_modannot", s
);
4497 slink
= &page
->slinks
[Int_val (n_v
)];
4498 pdf_set_annot_contents (state
.u
.pdf
, slink
->u
.annot
,
4499 String_val (str_v
));
4502 CAMLreturn (Val_unit
);
4505 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4507 CAMLparam1 (unit_v
);
4508 CAMLreturn (Val_bool (state
.dirty
));
4511 CAMLprim value
ml_savedoc (value path_v
)
4513 CAMLparam1 (path_v
);
4515 if (state
.type
== DPDF
) {
4516 pdf_write_document (state
.u
.pdf
, String_val (path_v
), NULL
);
4518 CAMLreturn (Val_unit
);
4521 static void makestippletex (void)
4523 const char pixels
[] = "\xff\xff\0\0";
4524 glGenTextures (1, &state
.stid
);
4525 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4526 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4527 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4540 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4542 return caml_copy_string (FZ_VERSION
);
4545 #ifdef USE_FONTCONFIG
4551 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4555 int UNUSED_ATTR needs_exact_metrics
)
4562 FcPattern
*pat
, *pat1
;
4564 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4565 name
, bold
, italic
, needs_exact_metrics
);
4568 fc
.config
= FcInitLoadConfigAndFonts ();
4570 lprintf ("FcInitLoadConfigAndFonts failed\n");
4574 if (!fc
.config
) return NULL
;
4576 size
= strlen (name
);
4577 if (bold
) size
+= sizeof (":bold") - 1;
4578 if (italic
) size
+= sizeof (":italic") - 1;
4581 buf
= malloc (size
);
4583 err (1, "malloc %zu failed", size
);
4587 if (bold
&& italic
) {
4588 strcat (buf
, ":bold:italic");
4591 if (bold
) strcat (buf
, ":bold");
4592 if (italic
) strcat (buf
, ":italic");
4594 for (i
= 0; i
< size
; ++i
) {
4595 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4598 lprintf ("fcbuf=%s\n", buf
);
4599 pat
= FcNameParse ((FcChar8
*) buf
);
4601 printd ("emsg FcNameParse failed\n");
4606 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4607 printd ("emsg FcConfigSubstitute failed\n");
4611 FcDefaultSubstitute (pat
);
4613 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4615 printd ("emsg FcFontMatch failed\n");
4616 FcPatternDestroy (pat
);
4621 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4622 printd ("emsg FcPatternGetString failed\n");
4623 FcPatternDestroy (pat
);
4624 FcPatternDestroy (pat1
);
4630 printd ("emsg name=%s path=%s\n", name
, path
);
4632 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4633 FcPatternDestroy (pat
);
4634 FcPatternDestroy (pat1
);
4640 CAMLprim value
ml_init (value csock_v
, value params_v
)
4642 CAMLparam2 (csock_v
, params_v
);
4643 CAMLlocal2 (trim_v
, fuzz_v
);
4651 state
.csock
= Int_val (csock_v
);
4652 state
.rotate
= Int_val (Field (params_v
, 0));
4653 state
.fitmodel
= Int_val (Field (params_v
, 1));
4654 trim_v
= Field (params_v
, 2);
4655 texcount
= Int_val (Field (params_v
, 3));
4656 state
.sliceheight
= Int_val (Field (params_v
, 4));
4657 mustoresize
= Int_val (Field (params_v
, 5));
4658 colorspace
= Int_val (Field (params_v
, 6));
4659 fontpath
= String_val (Field (params_v
, 7));
4661 if (caml_string_length (Field (params_v
, 8)) > 0) {
4662 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4664 if (!state
.trimcachepath
) {
4665 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4669 haspboext
= Bool_val (Field (params_v
, 9));
4671 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4673 #ifdef USE_FONTCONFIG
4674 if (Bool_val (Field (params_v
, 10))) {
4675 fz_install_load_system_font_funcs (
4676 state
.ctx
, fc_load_system_font_func
, NULL
4681 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4682 fuzz_v
= Field (trim_v
, 1);
4683 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4684 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4685 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4686 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4688 set_tex_params (colorspace
);
4691 #ifndef USE_FONTCONFIG
4692 state
.face
= load_font (fontpath
);
4696 char *buf
= fontpath
;
4697 FcPattern
*pat
, *pat1
;
4700 fc
.config
= FcInitLoadConfigAndFonts ();
4702 errx (1, "FcInitLoadConfigAndFonts failed");
4705 pat
= FcNameParse ((FcChar8
*) buf
);
4707 errx (1, "FcNameParse failed");
4710 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4711 errx (1, "FcConfigSubstitute failed");
4713 FcDefaultSubstitute (pat
);
4715 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4717 errx (1, "FcFontMatch failed");
4720 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4721 errx (1, "FcPatternGetString failed");
4724 state
.face
= load_font ((char *) path
);
4725 FcPatternDestroy (pat
);
4726 FcPatternDestroy (pat1
);
4731 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4733 state
.face
= load_builtin_font (base
, len
);
4735 if (!state
.face
) _exit (1);
4737 realloctexts (texcount
);
4745 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4747 errx (1, "pthread_create: %s", strerror (ret
));
4750 CAMLreturn (Val_unit
);