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 *);
263 void (*glBindBufferARB
) (GLenum
, GLuint
);
264 GLboolean (*glUnmapBufferARB
) (GLenum
);
265 void *(*glMapBufferARB
) (GLenum
, GLenum
);
266 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
267 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
268 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
270 GLfloat texcoords
[8];
271 GLfloat vertices
[16];
273 #ifdef CACHE_PAGEREFS
289 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
291 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
294 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
296 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
299 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
301 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
302 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
305 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
307 static void lock (const char *cap
)
309 int ret
= pthread_mutex_lock (&mutex
);
311 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
315 static void unlock (const char *cap
)
317 int ret
= pthread_mutex_unlock (&mutex
);
319 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
323 static int trylock (const char *cap
)
325 int ret
= pthread_mutex_trylock (&mutex
);
326 if (ret
&& ret
!= EBUSY
) {
327 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
332 static void *parse_pointer (const char *cap
, const char *s
)
337 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
339 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
344 static double now (void)
348 if (gettimeofday (&tv
, NULL
)) {
349 err (1, "gettimeofday");
351 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
354 static int hasdata (void)
357 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
358 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
362 CAMLprim value
ml_hasdata (value fd_v
)
367 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
368 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
369 CAMLreturn (Val_bool (avail
> 0));
372 static void readdata (void *p
, int size
)
377 n
= read (state
.csock
, p
, size
);
379 if (errno
== EINTR
) goto again
;
380 err (1, "read (req %d, ret %zd)", size
, n
);
383 if (!n
) errx (1, "EOF while reading");
384 errx (1, "read (req %d, ret %zd)", size
, n
);
388 static void writedata (char *p
, int size
)
392 p
[0] = (size
>> 24) & 0xff;
393 p
[1] = (size
>> 16) & 0xff;
394 p
[2] = (size
>> 8) & 0xff;
395 p
[3] = (size
>> 0) & 0xff;
397 n
= write (state
.csock
, p
, size
+ 4);
399 if (!n
) errx (1, "EOF while writing data");
400 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
404 static int readlen (void)
409 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
412 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
420 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
423 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
427 if (len
< size
- 4) {
428 writedata (buf
, len
);
434 err (1, "vsnprintf for `%s' failed", fmt
);
436 buf
= realloc (buf
, size
);
441 static void closepdf (void)
443 #ifdef CACHE_PAGEREFS
444 if (state
.pdflut
.objs
) {
447 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
448 pdf_drop_obj (state
.pdflut
.objs
[i
]);
450 free (state
.pdflut
.objs
);
451 state
.pdflut
.objs
= NULL
;
452 state
.pdflut
.idx
= 0;
457 pdf_close_document (state
.u
.pdf
);
462 static void closexps (void)
465 xps_close_document (state
.u
.xps
);
470 static void closecbz (void)
473 cbz_close_document (state
.u
.cbz
);
478 static void closeimg (void)
481 image_close_document (state
.u
.img
);
486 static void freepdfpage (void *ptr
)
488 pdf_free_page (state
.u
.pdf
, ptr
);
491 static void freeemptyxxxpage (void *ptr
)
496 static void freexpspage (void *ptr
)
498 xps_free_page (state
.u
.xps
, ptr
);
501 static void freecbzpage (void *ptr
)
503 cbz_free_page (state
.u
.cbz
, ptr
);
506 static void freeimgpage (void *ptr
)
508 image_free_page (state
.u
.img
, ptr
);
511 static int openxref (char *filename
, char *password
)
515 for (i
= 0; i
< state
.texcount
; ++i
) {
516 state
.texowners
[i
].w
= -1;
517 state
.texowners
[i
].slice
= NULL
;
520 if (state
.closedoc
) state
.closedoc ();
522 len
= strlen (filename
);
540 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
541 const char *ext
= tbl
[j
].ext
;
542 int len2
= strlen (ext
);
544 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
545 state
.type
= tbl
[j
].type
;
551 if (state
.pagedims
) {
552 free (state
.pagedims
);
553 state
.pagedims
= NULL
;
555 state
.pagedimcount
= 0;
557 fz_set_aa_level (state
.ctx
, state
.aalevel
);
558 switch (state
.type
) {
560 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
561 if (pdf_needs_password (state
.u
.pdf
)) {
562 if (password
&& !*password
) {
567 int ok
= pdf_authenticate_password (state
.u
.pdf
, password
);
569 printd ("pass fail");
574 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
575 state
.closedoc
= closepdf
;
576 state
.freepage
= freepdfpage
;
580 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
581 state
.pagecount
= xps_count_pages (state
.u
.xps
);
582 state
.closedoc
= closexps
;
583 state
.freepage
= freexpspage
;
587 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
588 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
589 state
.closedoc
= closecbz
;
590 state
.freepage
= freecbzpage
;
594 state
.u
.img
= image_open_document (state
.ctx
, filename
);
596 state
.closedoc
= closeimg
;
597 state
.freepage
= freeimgpage
;
603 static void pdfinfo (void)
605 if (state
.type
== DPDF
) {
608 printd ("info PDF version\t%d.%d",
609 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
611 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
615 char *items
[] = { "Title", "Author", "Creator",
616 "Producer", "CreationDate" };
618 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
619 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
620 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
621 if (*s
) printd ("info %s\t%s", items
[i
], s
);
622 fz_free (state
.ctx
, s
);
629 static void unlinktile (struct tile
*tile
)
633 for (i
= 0; i
< tile
->slicecount
; ++i
) {
634 struct slice
*s
= &tile
->slices
[i
];
636 if (s
->texindex
!= -1) {
637 if (state
.texowners
[s
->texindex
].slice
== s
) {
638 state
.texowners
[s
->texindex
].slice
= NULL
;
644 static void freepage (struct page
*page
)
647 fz_free_text_page (state
.ctx
, page
->text
);
650 fz_free_text_sheet (state
.ctx
, page
->sheet
);
655 fz_drop_display_list (state
.ctx
, page
->dlist
);
656 page
->freepage (page
->u
.ptr
);
660 static void freetile (struct tile
*tile
)
665 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
668 fz_drop_pixmap (state
.ctx
, state
.pig
);
670 state
.pig
= tile
->pixmap
;
675 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
684 static int cacheline32bytes
;
686 static void __attribute__ ((constructor
)) clcheck (void)
688 char **envp
= environ
;
693 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
695 cacheline32bytes
= auxv
[1] == 32;
701 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
703 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
704 if (cacheline32bytes
&& size
> 32) {
705 intptr_t a1
, a2
, diff
;
707 vector
unsigned char v
= vec_splat_u8 (-1);
708 vector
unsigned char *p
;
710 a1
= a2
= (intptr_t) pixmap
->samples
;
711 a2
= (a1
+ 31) & ~31;
716 while (a1
!= a2
) *(char *) a1
++ = 0xff;
717 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
718 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
720 vec_st (v
, i
+ 16, p
);
722 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
724 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
727 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
730 static void trimctm (pdf_page
*page
, int pindex
)
733 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
735 if (!pdim
->tctmready
) {
736 if (state
.trimmargins
) {
738 fz_matrix rm
, sm
, tm
, im
, ctm1
;
740 fz_rotate (&rm
, -pdim
->rotate
);
741 fz_scale (&sm
, 1, -1);
742 fz_concat (&ctm
, &rm
, &sm
);
743 realbox
= pdim
->mediabox
;
744 fz_transform_rect (&realbox
, &ctm
);
745 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
746 fz_concat (&ctm1
, &ctm
, &tm
);
747 fz_invert_matrix (&im
, &page
->ctm
);
748 fz_concat (&ctm
, &im
, &ctm1
);
758 static fz_matrix
pagectm (struct page
*page
)
762 if (page
->type
== DPDF
) {
763 trimctm (page
->u
.pdfpage
, page
->pdimno
);
765 &state
.pagedims
[page
->pdimno
].tctm
,
766 &state
.pagedims
[page
->pdimno
].ctm
);
769 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
771 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
772 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
777 static void *loadpage (int pageno
, int pindex
)
782 page
= calloc (sizeof (struct page
), 1);
784 err (1, "calloc page %d", pageno
);
787 page
->dlist
= fz_new_display_list (state
.ctx
);
788 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
790 switch (state
.type
) {
792 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
793 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
795 page
->freepage
= freepdfpage
;
799 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
800 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
802 page
->freepage
= freexpspage
;
806 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
807 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
809 page
->freepage
= freecbzpage
;
813 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
814 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
816 page
->freepage
= freeimgpage
;
820 fz_catch (state
.ctx
) {
822 page
->freepage
= freeemptyxxxpage
;
824 fz_free_device (dev
);
826 page
->pdimno
= pindex
;
827 page
->pageno
= pageno
;
828 page
->sgen
= state
.gen
;
829 page
->agen
= state
.gen
;
830 page
->tgen
= state
.gen
;
831 page
->type
= state
.type
;
836 static struct tile
*alloctile (int h
)
843 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
844 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
845 tile
= calloc (tilesize
, 1);
847 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
849 for (i
= 0; i
< slicecount
; ++i
) {
850 int sh
= MIN (h
, state
.sliceheight
);
851 tile
->slices
[i
].h
= sh
;
852 tile
->slices
[i
].texindex
= -1;
855 tile
->slicecount
= slicecount
;
856 tile
->sliceheight
= state
.sliceheight
;
866 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
867 const fz_matrix
*ctm
, float alpha
)
869 struct obs
*obs
= dev
->user
;
871 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
873 fz_rect rect
= fz_unit_rect
;
875 fz_transform_rect (&rect
, ctm
);
876 fz_round_rect (&b
, &rect
);
877 fz_intersect_irect (&b
, &obs
->b
);
878 obs
->cured
= b
.x0
== obs
->b
.x0
881 && b
.y1
== obs
->b
.y1
;
885 static int obscured (struct page
*page
, fz_irect bbox
)
892 memset (&dev
, 0, sizeof (dev
));
893 memset (&obs
, 0, sizeof (obs
));
898 dev
.fill_image
= obs_fill_image
;
900 fz_rect_from_irect (&rect
, &bbox
);
901 ctm
= pagectm (page
);
902 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
905 #define OBSCURED obscured
907 #define OBSCURED(a, b) 0
910 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
918 struct pagedim
*pdim
;
920 tile
= alloctile (h
);
921 pdim
= &state
.pagedims
[page
->pdimno
];
926 bbox
.x1
= bbox
.x0
+ w
;
927 bbox
.y1
= bbox
.y0
+ h
;
930 if (state
.pig
->w
== w
932 && state
.pig
->colorspace
== state
.colorspace
) {
933 tile
->pixmap
= state
.pig
;
934 tile
->pixmap
->x
= bbox
.x0
;
935 tile
->pixmap
->y
= bbox
.y0
;
938 fz_drop_pixmap (state
.ctx
, state
.pig
);
945 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
951 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
957 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
958 clearpixmap (tile
->pixmap
);
960 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
961 ctm
= pagectm (page
);
962 fz_rect_from_irect (&rect
, &bbox
);
963 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
964 fz_free_device (dev
);
969 #ifdef CACHE_PAGEREFS
970 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
971 thanks to Robin Watts */
973 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
975 fz_context
*ctx
= doc
->ctx
;
979 if (state
.pdflut
.idx
== state
.pagecount
) return;
981 kids
= pdf_dict_gets (node
, "Kids");
982 len
= pdf_array_len (kids
);
985 fz_throw (ctx
, FZ_ERROR_GENERIC
, "Malformed pages tree");
987 if (pdf_mark_obj (node
))
988 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
989 for (i
= 0; i
< len
; i
++) {
990 pdf_obj
*kid
= pdf_array_get (kids
, i
);
991 char *type
= pdf_to_name (pdf_dict_gets (kid
, "Type"));
993 ? !strcmp (type
, "Pages")
994 : pdf_dict_gets (kid
, "Kids")
995 && !pdf_dict_gets (kid
, "MediaBox")) {
996 pdf_collect_pages (doc
, kid
);
1000 ? strcmp (type
, "Page") != 0
1001 : !pdf_dict_gets (kid
, "MediaBox"))
1002 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
1003 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (kid
);
1006 pdf_unmark_obj (node
);
1010 pdf_load_page_objs (pdf_document
*doc
)
1012 pdf_obj
*root
= pdf_dict_gets (pdf_trailer (doc
), "Root");
1013 pdf_obj
*node
= pdf_dict_gets (root
, "Pages");
1016 fz_throw (doc
->ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
1018 state
.pdflut
.idx
= 0;
1019 pdf_collect_pages (doc
, node
);
1023 static void initpdims (void)
1027 fz_rect rootmediabox
;
1028 int pageno
, trim
, show
;
1029 int trimw
= 0, cxcount
;
1033 if (state
.trimmargins
&& state
.trimcachepath
) {
1034 trimf
= fopen (state
.trimcachepath
, "rb");
1036 trimf
= fopen (state
.trimcachepath
, "wb");
1041 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1042 cxcount
= state
.pagecount
;
1044 cxcount
= MIN (state
.pagecount
, 1);
1046 if (state
.type
== DPDF
) {
1048 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1049 "Root/Pages/MediaBox");
1050 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1053 #ifdef CACHE_PAGEREFS
1054 if (state
.type
== DPDF
1055 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1056 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
1057 if (!state
.pdflut
.objs
) {
1058 err (1, "malloc pageobjs %zu %d %zu failed",
1059 sizeof (*state
.pdflut
.objs
), cxcount
,
1060 sizeof (*state
.pdflut
.objs
) * cxcount
);
1062 state
.pdflut
.count
= cxcount
;
1063 pdf_load_page_objs (state
.u
.pdf
);
1064 state
.pdflut
.pdf
= state
.u
.pdf
;
1068 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1073 switch (state
.type
) {
1075 pdf_document
*pdf
= state
.u
.pdf
;
1076 pdf_obj
*pageref
, *pageobj
;
1078 #ifdef CACHE_PAGEREFS
1079 pageref
= state
.pdflut
.objs
[pageno
];
1081 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1082 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1084 printd ("progress %f Gathering dimensions %d",
1085 (double) (pageno
) / state
.pagecount
,
1089 pageobj
= pdf_resolve_indirect (pageref
);
1091 if (state
.trimmargins
) {
1095 fz_try (state
.ctx
) {
1096 page
= pdf_load_page (pdf
, pageno
);
1097 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1098 trim
= state
.trimanew
|| !obj
;
1104 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1105 dev
->hints
|= FZ_IGNORE_SHADE
;
1106 fz_invert_matrix (&ctm
, &page
->ctm
);
1107 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1108 fz_free_device (dev
);
1110 rect
.x0
+= state
.trimfuzz
.x0
;
1111 rect
.x1
+= state
.trimfuzz
.x1
;
1112 rect
.y0
+= state
.trimfuzz
.y0
;
1113 rect
.y1
+= state
.trimfuzz
.y1
;
1114 fz_transform_rect (&rect
, &ctm
);
1115 fz_intersect_rect (&rect
, &page
->mediabox
);
1117 if (fz_is_empty_rect (&rect
)) {
1118 mediabox
= page
->mediabox
;
1124 obj
= pdf_new_array (pdf
, 4);
1125 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1126 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1127 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1128 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1129 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1132 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1133 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1134 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1135 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1138 rotate
= page
->rotate
;
1139 pdf_free_page (pdf
, page
);
1141 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1143 printd ("progress %f Trimming %d",
1144 (double) (pageno
+ 1) / state
.pagecount
,
1148 fz_catch (state
.ctx
) {
1149 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1156 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1158 if (fz_is_empty_rect (&mediabox
)) {
1166 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1168 if (!fz_is_empty_rect (&cropbox
)) {
1173 fz_intersect_rect (&mediabox
, &cropbox
);
1178 if (fz_is_empty_rect (&rootmediabox
)) {
1180 "cannot find page size for page %d\n",
1184 mediabox
= rootmediabox
;
1188 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1197 fz_try (state
.ctx
) {
1198 page
= xps_load_page (state
.u
.xps
, pageno
);
1199 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1201 if (state
.trimmargins
) {
1205 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1206 dev
->hints
|= FZ_IGNORE_SHADE
;
1207 xps_run_page (state
.u
.xps
, page
, dev
,
1208 &fz_identity
, NULL
);
1209 fz_free_device (dev
);
1211 rect
.x0
+= state
.trimfuzz
.x0
;
1212 rect
.x1
+= state
.trimfuzz
.x1
;
1213 rect
.y0
+= state
.trimfuzz
.y0
;
1214 rect
.y1
+= state
.trimfuzz
.y1
;
1215 fz_intersect_rect (&rect
, &mediabox
);
1217 if (!fz_is_empty_rect (&rect
)) {
1221 xps_free_page (state
.u
.xps
, page
);
1223 printd ("progress %f loading %d",
1224 (double) (pageno
+ 1) / state
.pagecount
,
1228 fz_catch (state
.ctx
) {
1240 if (state
.trimmargins
&& trimw
) {
1243 fz_try (state
.ctx
) {
1244 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1245 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1246 cbz_free_page (state
.u
.cbz
, page
);
1247 printd ("progress %f Trimming %d",
1248 (double) (pageno
+ 1) / state
.pagecount
,
1251 fz_catch (state
.ctx
) {
1252 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1255 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1257 err (1, "fwrite trim mediabox");
1263 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1265 err (1, "fread trim mediabox");
1271 fz_try (state
.ctx
) {
1272 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1273 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1274 cbz_free_page (state
.u
.cbz
, page
);
1276 fz_catch (state
.ctx
) {
1277 fprintf (stderr
, "failed to load cbz page\n");
1295 fz_try (state
.ctx
) {
1296 page
= image_load_page (state
.u
.img
, pageno
);
1297 image_bound_page (state
.u
.img
, page
, &mediabox
);
1298 image_free_page (state
.u
.img
, page
);
1300 fz_catch (state
.ctx
) {
1301 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1307 ARSERT (0 && state
.type
);
1310 if (state
.pagedimcount
== 0
1311 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1312 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1315 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1316 state
.pagedims
= realloc (state
.pagedims
, size
);
1317 if (!state
.pagedims
) {
1318 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1319 size
, state
.pagedimcount
+ 1);
1322 p
= &state
.pagedims
[state
.pagedimcount
++];
1324 p
->mediabox
= mediabox
;
1329 if (state
.trimmargins
) {
1330 printd ("progress 1 Trimmed %d pages in %f seconds",
1331 state
.pagecount
, end
- start
);
1334 if (state
.type
== DPDF
)
1335 printd ("progress 1 Processed %d pages in %f seconds",
1336 state
.pagecount
, end
- start
);
1338 printd ("vmsg Processed %d pages in %f seconds",
1339 state
.pagecount
, end
- start
);
1343 if (fclose (trimf
)) {
1349 static void layout (void)
1354 struct pagedim
*p
= p
;
1355 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1357 if (state
.pagedimcount
== 0) return;
1359 switch (state
.fitmodel
) {
1360 case FitProportional
:
1361 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1364 p
= &state
.pagedims
[pindex
];
1365 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1367 fz_transform_rect (&box
, &rm
);
1369 x0
= MIN (box
.x0
, box
.x1
);
1370 x1
= MAX (box
.x0
, box
.x1
);
1373 maxw
= MAX (w
, maxw
);
1374 zoom
= state
.w
/ maxw
;
1386 ARSERT (0 && state
.fitmodel
);
1389 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1393 p
= &state
.pagedims
[pindex
];
1394 fz_rotate (&ctm
, state
.rotate
);
1395 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1397 fz_transform_rect (&box
, &rm
);
1398 w
= box
.x1
- box
.x0
;
1399 switch (state
.fitmodel
) {
1400 case FitProportional
:
1401 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1407 h
= box
.y1
- box
.y0
;
1409 zoom
= MIN (zw
, zh
);
1410 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1419 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1420 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1422 fz_rotate (&rm
, p
->rotate
);
1423 p
->pagebox
= p
->mediabox
;
1424 fz_transform_rect (&p
->pagebox
, &rm
);
1425 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1426 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1430 fz_transform_rect (&rect
, &ctm
);
1431 fz_round_rect (&p
->bounds
, &rect
);
1434 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1435 fz_scale (&sm
, zoom
, -zoom
);
1436 fz_concat (&ctm
, &tm
, &sm
);
1437 fz_concat (&p
->lctm
, &ctm
, &rm
);
1443 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1444 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1445 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1446 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1447 int boundw
= x1
- x0
;
1448 int boundh
= y1
- y0
;
1450 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1451 } while (p
-- != state
.pagedims
);
1455 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1456 desttoanchor (fz_link_dest
*dest
)
1460 struct pagedim
*pdim
= state
.pagedims
;
1465 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1466 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1468 pdim
= &state
.pagedims
[i
];
1470 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1472 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1473 p
.x
= dest
->ld
.gotor
.lt
.x
;
1476 p
.y
= dest
->ld
.gotor
.lt
.y
;
1477 fz_transform_point (&p
, &pdim
->lctm
);
1481 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1482 double x0
, x1
, y0
, y1
;
1484 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1485 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1487 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1488 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1490 a
.n
= dest
->ld
.gotor
.page
;
1495 static void recurse_outline (fz_outline
*outline
, int level
)
1498 switch (outline
->dest
.kind
) {
1501 struct anchor a
= desttoanchor (&outline
->dest
);
1504 printd ("o %d %d %d %d %s",
1505 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1511 printd ("ou %d %" FMT_s
" %s %s", level
,
1512 strlen (outline
->title
), outline
->title
,
1513 outline
->dest
.ld
.uri
.uri
);
1517 printd ("on %d %s", level
, outline
->title
);
1521 printd ("emsg Unhandled outline kind %d for %s\n",
1522 outline
->dest
.kind
, outline
->title
);
1525 if (outline
->down
) {
1526 recurse_outline (outline
->down
, level
+ 1);
1528 outline
= outline
->next
;
1532 static void process_outline (void)
1534 fz_outline
*outline
;
1536 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1538 state
.needoutline
= 0;
1539 switch (state
.type
) {
1541 outline
= pdf_load_outline (state
.u
.pdf
);
1544 outline
= xps_load_outline (state
.u
.xps
);
1551 recurse_outline (outline
, 0);
1552 fz_free_outline (state
.ctx
, outline
);
1556 static char *strofspan (fz_text_span
*span
)
1561 size_t size
= 0, cap
= 80;
1563 p
= malloc (cap
+ 1);
1564 if (!p
) return NULL
;
1566 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1567 int n
= fz_runetochar (utf8
, ch
->c
);
1568 if (size
+ n
> cap
) {
1570 p
= realloc (p
, cap
+ 1);
1571 if (!p
) return NULL
;
1574 memcpy (p
+ size
, utf8
, n
);
1581 static int matchspan (regex_t
*re
, fz_text_span
*span
,
1582 int stop
, int pageno
, double start
)
1589 fz_point p1
, p2
, p3
, p4
;
1591 p
= strofspan (span
);
1594 ret
= regexec (re
, p
, 1, &rm
, 0);
1597 if (ret
!= REG_NOMATCH
) {
1600 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1601 printd ("msg regexec error `%.*s'",
1602 (int) size
, errbuf
);
1610 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1611 c
+= fz_runelen (span
->text
[a
].c
);
1613 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1614 c
+= fz_runelen (span
->text
[b
].c
);
1617 if (fz_runelen (span
->text
[b
].c
) > 1) {
1621 fz_text_char_bbox (&sb
, span
, a
);
1622 fz_text_char_bbox (&eb
, span
, b
);
1634 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1641 printd ("progress 1 found at %d `%.*s' in %f sec",
1642 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1646 printd ("match %d %d %f %f %f %f %f %f %f %f",
1658 static int compareblocks (const void *l
, const void *r
)
1660 fz_text_block
const *ls
= l
;
1661 fz_text_block
const* rs
= r
;
1662 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1665 /* wishful thinking function */
1666 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1671 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1673 fz_text_sheet
*sheet
;
1674 struct pagedim
*pdim
, *pdimprev
;
1675 int stop
= 0, niters
= 0;
1678 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1682 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1683 if (niters
++ == 5) {
1686 printd ("progress 1 attention requested aborting search at %d",
1691 printd ("progress %f searching in page %d",
1692 (double) (pageno
+ 1) / state
.pagecount
,
1697 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1698 pdim
= &state
.pagedims
[i
];
1699 if (pdim
->pageno
== pageno
) {
1702 if (pdim
->pageno
> pageno
) {
1711 sheet
= fz_new_text_sheet (state
.ctx
);
1712 text
= fz_new_text_page (state
.ctx
);
1713 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1715 switch (state
.type
) {
1718 fz_try (state
.ctx
) {
1719 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1720 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1721 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1722 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1723 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1726 fz_catch (state
.ctx
) {
1727 fz_free_device (tdev
);
1734 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1735 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1739 ARSERT (0 && state
.type
);
1742 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1743 fz_free_device (tdev
);
1745 for (j
= 0; j
< text
->len
; ++j
) {
1748 fz_text_block
*block
;
1750 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1751 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1754 for (k
= 0; k
< block
->len
; ++k
) {
1759 line
= &block
->lines
[k
];
1760 if (line
->bbox
.y0
< y
+ 1) continue;
1763 line
= &block
->lines
[block
->len
- 1 - k
];
1764 if (line
->bbox
.y0
> y
- 1) continue;
1767 for (span
= line
->first_span
; span
; span
= span
->next
) {
1768 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1770 case 1: stop
= 1; break;
1771 case -1: stop
= 1; goto endloop
;
1786 fz_free_text_page (state
.ctx
, text
);
1787 fz_free_text_sheet (state
.ctx
, sheet
);
1789 state
.freepage (u
.ptr
);
1794 printd ("progress 1 no matches %f sec", end
- start
);
1796 printd ("clearrects");
1799 static void set_tex_params (int colorspace
)
1806 switch (colorspace
) {
1808 state
.texiform
= GL_RGBA8
;
1809 state
.texform
= GL_RGBA
;
1810 state
.texty
= GL_UNSIGNED_BYTE
;
1811 state
.colorspace
= fz_device_rgb (state
.ctx
);
1814 state
.texiform
= GL_RGBA8
;
1815 state
.texform
= GL_BGRA
;
1816 state
.texty
= endianness
.s
> 1
1817 ? GL_UNSIGNED_INT_8_8_8_8
1818 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1819 state
.colorspace
= fz_device_bgr (state
.ctx
);
1822 state
.texiform
= GL_LUMINANCE_ALPHA
;
1823 state
.texform
= GL_LUMINANCE_ALPHA
;
1824 state
.texty
= GL_UNSIGNED_BYTE
;
1825 state
.colorspace
= fz_device_gray (state
.ctx
);
1828 errx (1, "invalid colorspce %d", colorspace
);
1832 static void realloctexts (int texcount
)
1836 if (texcount
== state
.texcount
) return;
1838 if (texcount
< state
.texcount
) {
1839 glDeleteTextures (state
.texcount
- texcount
,
1840 state
.texids
+ texcount
);
1843 size
= texcount
* sizeof (*state
.texids
);
1844 state
.texids
= realloc (state
.texids
, size
);
1845 if (!state
.texids
) {
1846 err (1, "realloc texids %" FMT_s
, size
);
1849 size
= texcount
* sizeof (*state
.texowners
);
1850 state
.texowners
= realloc (state
.texowners
, size
);
1851 if (!state
.texowners
) {
1852 err (1, "realloc texowners %" FMT_s
, size
);
1854 if (texcount
> state
.texcount
) {
1857 glGenTextures (texcount
- state
.texcount
,
1858 state
.texids
+ state
.texcount
);
1859 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1860 state
.texowners
[i
].w
= -1;
1861 state
.texowners
[i
].slice
= NULL
;
1864 state
.texcount
= texcount
;
1868 static char *mbtoutf8 (char *s
)
1874 len
= mbstowcs (NULL
, s
, strlen (s
));
1879 if (len
== (size_t) -1) {
1884 tmp
= malloc (len
* sizeof (wchar_t));
1889 ret
= mbstowcs (tmp
, s
, len
);
1890 if (ret
== (size_t) -1) {
1896 for (i
= 0; i
< ret
; ++i
) {
1897 len
+= fz_runelen (tmp
[i
]);
1900 p
= r
= malloc (len
+ 1);
1906 for (i
= 0; i
< ret
; ++i
) {
1907 p
+= fz_runetochar (p
, tmp
[i
]);
1914 CAMLprim value
ml_mbtoutf8 (value s_v
)
1920 s
= String_val (s_v
);
1926 ret_v
= caml_copy_string (r
);
1932 static void * mainloop (void UNUSED_ATTR
*unused
)
1935 int len
, ret
, oldlen
= 0;
1940 errx (1, "readlen returned 0");
1943 if (oldlen
< len
+ 1) {
1944 p
= realloc (p
, len
+ 1);
1946 err (1, "realloc %d failed", len
+ 1);
1953 if (!strncmp ("open", p
, 4)) {
1954 int wthack
, off
, ok
= 0;
1960 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1962 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1965 filename
= p
+ 5 + off
;
1966 filenamelen
= strlen (filename
);
1967 password
= filename
+ filenamelen
+ 1;
1970 fz_try (state
.ctx
) {
1971 ok
= openxref (filename
, password
);
1973 fz_catch (state
.ctx
) {
1974 utf8filename
= mbtoutf8 (filename
);
1975 printd ("msg Could not open %s", utf8filename
);
1985 utf8filename
= mbtoutf8 (filename
);
1986 printd ("msg Opened %s (press h/F1 to get help)",
1988 if (utf8filename
!= filename
) {
1989 free (utf8filename
);
1992 state
.needoutline
= 1;
1995 else if (!strncmp ("cs", p
, 2)) {
1998 ret
= sscanf (p
+ 2, " %d", &colorspace
);
2000 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
2003 set_tex_params (colorspace
);
2004 for (i
= 0; i
< state
.texcount
; ++i
) {
2005 state
.texowners
[i
].w
= -1;
2006 state
.texowners
[i
].slice
= NULL
;
2010 else if (!strncmp ("freepage", p
, 8)) {
2013 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2015 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
2019 else if (!strncmp ("freetile", p
, 8)) {
2022 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2024 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
2028 else if (!strncmp ("search", p
, 6)) {
2029 int icase
, pageno
, y
, len2
, forward
;
2033 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2034 &icase
, &pageno
, &y
, &forward
, &len2
);
2036 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2039 pattern
= p
+ 6 + len2
;
2040 ret
= regcomp (&re
, pattern
,
2041 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2046 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2047 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2050 search (&re
, pageno
, y
, forward
);
2054 else if (!strncmp ("geometry", p
, 8)) {
2058 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2060 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2068 for (i
= 0; i
< state
.texcount
; ++i
) {
2069 state
.texowners
[i
].slice
= NULL
;
2072 state
.fitmodel
= fitmodel
;
2077 unlock ("geometry");
2078 printd ("continue %d", state
.pagecount
);
2080 else if (!strncmp ("reqlayout", p
, 9)) {
2083 unsigned int fitmodel
;
2086 ret
= sscanf (p
+ 9, " %d %u %d %n",
2087 &rotate
, &fitmodel
, &h
, &off
);
2089 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2092 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2095 state
.rotate
= rotate
;
2096 state
.fitmodel
= fitmodel
;
2101 nameddest
= p
+ 9 + off
;
2102 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2105 pdf_obj
*needle
, *obj
;
2107 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2108 strlen (nameddest
));
2109 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2111 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2113 a
= desttoanchor (&dest
);
2115 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2118 printd ("emsg failed to parse destination `%s'\n",
2123 printd ("emsg destination `%s' not found\n",
2126 pdf_drop_obj (needle
);
2130 unlock ("reqlayout");
2131 printd ("continue %d", state
.pagecount
);
2133 else if (!strncmp ("page", p
, 4)) {
2138 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2140 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2145 page
= loadpage (pageno
, pindex
);
2149 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2151 else if (!strncmp ("tile", p
, 4)) {
2158 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2159 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2160 SCN_ptr_cast (&data
));
2162 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2167 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2171 printd ("tile %d %d %" FMT_ptr
" %u %f",
2173 FMT_ptr_cast (tile
),
2174 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2177 else if (!strncmp ("trimset", p
, 7)) {
2181 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2182 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2184 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
2187 state
.trimmargins
= trimmargins
;
2188 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2190 state
.trimfuzz
= fuzz
;
2194 else if (!strncmp ("settrim", p
, 7)) {
2198 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2199 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2201 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2205 state
.trimmargins
= trimmargins
;
2206 state
.needoutline
= 1;
2207 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2209 state
.trimfuzz
= fuzz
;
2211 state
.pagedimcount
= 0;
2212 free (state
.pagedims
);
2213 state
.pagedims
= NULL
;
2218 printd ("continue %d", state
.pagecount
);
2220 else if (!strncmp ("sliceh", p
, 6)) {
2223 ret
= sscanf (p
+ 6, " %d", &h
);
2225 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2227 if (h
!= state
.sliceheight
) {
2230 state
.sliceheight
= h
;
2231 for (i
= 0; i
< state
.texcount
; ++i
) {
2232 state
.texowners
[i
].w
= -1;
2233 state
.texowners
[i
].h
= -1;
2234 state
.texowners
[i
].slice
= NULL
;
2238 else if (!strncmp ("interrupt", p
, 9)) {
2239 printd ("vmsg interrupted");
2242 errx (1, "unknown command %.*s", len
, p
);
2248 CAMLprim value
ml_realloctexts (value texcount_v
)
2250 CAMLparam1 (texcount_v
);
2253 if (trylock ("ml_realloctexts")) {
2257 realloctexts (Int_val (texcount_v
));
2259 unlock ("ml_realloctexts");
2262 CAMLreturn (Val_bool (ok
));
2265 static void recti (int x0
, int y0
, int x1
, int y1
)
2267 GLfloat
*v
= state
.vertices
;
2269 glVertexPointer (2, GL_FLOAT
, 0, v
);
2270 v
[0] = x0
; v
[1] = y0
;
2271 v
[2] = x1
; v
[3] = y0
;
2272 v
[4] = x0
; v
[5] = y1
;
2273 v
[6] = x1
; v
[7] = y1
;
2274 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2277 static void showsel (struct page
*page
, int ox
, int oy
)
2283 fz_page_block
*pageb
;
2284 fz_text_block
*block
;
2285 struct mark first
, last
;
2286 unsigned char selcolor
[] = {15,15,15,140};
2288 first
= page
->fmark
;
2291 if (!first
.span
|| !last
.span
) return;
2293 glEnable (GL_BLEND
);
2294 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2295 glColor4ubv (selcolor
);
2297 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2298 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2299 for (pageb
= page
->text
->blocks
;
2300 pageb
< page
->text
->blocks
+ page
->text
->len
;
2302 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2303 block
= pageb
->u
.text
;
2305 for (line
= block
->lines
;
2306 line
< block
->lines
+ block
->len
;
2309 rect
= fz_empty_rect
;
2311 for (span
= line
->first_span
; span
; span
= span
->next
) {
2313 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2318 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2320 j
= MIN (first
.i
, last
.i
);
2321 k
= MAX (first
.i
, last
.i
);
2324 if (span
== first
.span
) {
2328 else if (span
== last
.span
) {
2335 for (i
= j
; i
<= k
; ++i
) {
2337 fz_union_rect (&rect
,
2338 fz_text_char_bbox (&bbox1
, span
, i
));
2340 fz_round_rect (&bbox
, &rect
);
2341 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2348 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2349 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2350 if (span
== last
.span
) {
2353 rect
= fz_empty_rect
;
2359 glDisable (GL_BLEND
);
2364 static void stipplerect (fz_matrix
*m
,
2372 fz_transform_point (p1
, m
);
2373 fz_transform_point (p2
, m
);
2374 fz_transform_point (p3
, m
);
2375 fz_transform_point (p4
, m
);
2381 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2385 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2387 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2388 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2390 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2391 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2393 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2394 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2396 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2397 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2399 glDrawArrays (GL_LINES
, 0, 8);
2402 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2405 fz_matrix ctm
, tm
, pm
;
2406 fz_link
*link
, *links
;
2407 GLfloat
*texcoords
= state
.texcoords
;
2408 GLfloat
*vertices
= state
.vertices
;
2410 switch (page
->type
) {
2412 links
= page
->u
.pdfpage
->links
;
2416 links
= page
->u
.xpspage
->links
;
2423 glEnable (GL_TEXTURE_1D
);
2424 glEnable (GL_BLEND
);
2425 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2426 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2428 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2429 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2430 fz_translate (&tm
, xoff
, yoff
);
2431 pm
= pagectm (page
);
2432 fz_concat (&ctm
, &pm
, &tm
);
2434 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2435 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2437 for (link
= links
; link
; link
= link
->next
) {
2438 fz_point p1
, p2
, p3
, p4
;
2440 p1
.x
= link
->rect
.x0
;
2441 p1
.y
= link
->rect
.y0
;
2443 p2
.x
= link
->rect
.x1
;
2444 p2
.y
= link
->rect
.y0
;
2446 p3
.x
= link
->rect
.x1
;
2447 p3
.y
= link
->rect
.y1
;
2449 p4
.x
= link
->rect
.x0
;
2450 p4
.y
= link
->rect
.y1
;
2452 switch (link
->dest
.kind
) {
2453 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2454 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2455 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2456 default: glColor3ub (0, 0, 0); break;
2458 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2461 for (i
= 0; i
< page
->annotcount
; ++i
) {
2462 fz_point p1
, p2
, p3
, p4
;
2463 struct annot
*annot
= &page
->annots
[i
];
2465 p1
.x
= annot
->bbox
.x0
;
2466 p1
.y
= annot
->bbox
.y0
;
2468 p2
.x
= annot
->bbox
.x1
;
2469 p2
.y
= annot
->bbox
.y0
;
2471 p3
.x
= annot
->bbox
.x1
;
2472 p3
.y
= annot
->bbox
.y1
;
2474 p4
.x
= annot
->bbox
.x0
;
2475 p4
.y
= annot
->bbox
.y1
;
2477 glColor3ub (0, 0, 128);
2478 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2481 glDisable (GL_BLEND
);
2482 glDisable (GL_TEXTURE_1D
);
2485 static int compareslinks (const void *l
, const void *r
)
2487 struct slink
const *ls
= l
;
2488 struct slink
const *rs
= r
;
2489 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2490 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2492 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2495 static void droptext (struct page
*page
)
2498 fz_free_text_page (state
.ctx
, page
->text
);
2501 page
->fmark
.span
= NULL
;
2502 page
->lmark
.span
= NULL
;
2506 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2511 static void dropanots (struct page
*page
)
2514 free (page
->annots
);
2515 page
->annots
= NULL
;
2516 page
->annotcount
= 0;
2520 static void ensureanots (struct page
*page
)
2524 size_t anotsize
= sizeof (*page
->annots
);
2527 if (state
.gen
!= page
->agen
) {
2529 page
->agen
= state
.gen
;
2531 if (page
->annots
) return;
2533 switch (page
->type
) {
2535 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2537 &state
.pagedims
[page
->pdimno
].tctm
,
2538 &state
.pagedims
[page
->pdimno
].ctm
);
2545 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
);
2547 annot
= pdf_next_annot (state
.u
.pdf
, annot
)) {
2552 page
->annotcount
= count
;
2553 page
->annots
= calloc (count
, anotsize
);
2554 if (!page
->annots
) {
2555 err (1, "calloc annots %d", count
);
2558 for (annot
= pdf_first_annot (state
.u
.pdf
, page
->u
.pdfpage
), i
= 0;
2560 annot
= pdf_next_annot (state
.u
.pdf
, annot
), i
++) {
2563 pdf_bound_annot (state
.u
.pdf
, annot
, &rect
);
2564 fz_transform_rect (&rect
, &ctm
);
2565 page
->annots
[i
].annot
= annot
;
2566 fz_round_rect (&page
->annots
[i
].bbox
, &annot
->pagerect
);
2571 static void dropslinks (struct page
*page
)
2574 free (page
->slinks
);
2575 page
->slinks
= NULL
;
2576 page
->slinkcount
= 0;
2580 static void ensureslinks (struct page
*page
)
2584 size_t slinksize
= sizeof (*page
->slinks
);
2585 fz_link
*link
, *links
;
2588 if (state
.gen
!= page
->sgen
) {
2590 page
->sgen
= state
.gen
;
2592 if (page
->slinks
) return;
2594 switch (page
->type
) {
2596 links
= page
->u
.pdfpage
->links
;
2597 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2599 &state
.pagedims
[page
->pdimno
].tctm
,
2600 &state
.pagedims
[page
->pdimno
].ctm
);
2604 links
= page
->u
.xpspage
->links
;
2605 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2612 count
= page
->annotcount
;
2613 for (link
= links
; link
; link
= link
->next
) {
2619 page
->slinkcount
= count
;
2620 page
->slinks
= calloc (count
, slinksize
);
2621 if (!page
->slinks
) {
2622 err (1, "calloc slinks %d", count
);
2625 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2629 fz_transform_rect (&rect
, &ctm
);
2630 page
->slinks
[i
].tag
= SLINK
;
2631 page
->slinks
[i
].u
.link
= link
;
2632 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2634 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2637 rect
= page
->annots
[j
].annot
->pagerect
;
2638 fz_transform_rect (&rect
, &ctm
);
2639 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2641 page
->slinks
[i
].tag
= SANNOT
;
2642 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2644 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2648 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2649 static void fmt_linkn (char *s
, unsigned int u
)
2651 unsigned int len
; unsigned int q
;
2652 unsigned int zma
= 'z' - 'a' + 1;
2654 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2657 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2658 /* handles u == 0 */
2663 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2664 int noff
, char *targ
, int tlen
, int hfsize
)
2668 struct slink
*slink
;
2669 double x0
, y0
, x1
, y1
, w
;
2671 ensureslinks (page
);
2672 glColor3ub (0xc3, 0xb0, 0x91);
2673 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2674 fmt_linkn (buf
, i
+ noff
);
2675 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2676 slink
= &page
->slinks
[i
];
2678 x0
= slink
->bbox
.x0
+ xoff
- 5;
2679 y1
= slink
->bbox
.y0
+ yoff
- 5;
2680 y0
= y1
+ 10 + hfsize
;
2681 w
= measure_string (state
.face
, hfsize
, buf
);
2683 recti (x0
, y0
, x1
, y1
);
2687 glEnable (GL_BLEND
);
2688 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2689 glEnable (GL_TEXTURE_2D
);
2690 glColor3ub (0, 0, 0);
2691 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2692 fmt_linkn (buf
, i
+ noff
);
2693 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2694 slink
= &page
->slinks
[i
];
2696 x0
= slink
->bbox
.x0
+ xoff
;
2697 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2698 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2701 glDisable (GL_TEXTURE_2D
);
2702 glDisable (GL_BLEND
);
2705 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2708 struct slice
*slice1
;
2709 unsigned char *texdata
;
2712 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2713 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2715 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2716 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2717 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2721 int texindex
= state
.texindex
++ % state
.texcount
;
2723 if (state
.texowners
[texindex
].w
== tile
->w
) {
2724 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2728 state
.texowners
[texindex
].h
= slice
->h
;
2732 state
.texowners
[texindex
].h
= slice
->h
;
2735 state
.texowners
[texindex
].w
= tile
->w
;
2736 state
.texowners
[texindex
].slice
= slice
;
2737 slice
->texindex
= texindex
;
2739 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2741 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2745 texdata
= tile
->pixmap
->samples
;
2748 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2760 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2772 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2777 CAMLprim value
ml_begintiles (value unit_v
)
2779 CAMLparam1 (unit_v
);
2780 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2781 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2782 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2783 CAMLreturn (unit_v
);
2786 CAMLprim value
ml_endtiles (value unit_v
)
2788 CAMLparam1 (unit_v
);
2789 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2790 CAMLreturn (unit_v
);
2793 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2795 CAMLparam2 (args_v
, ptr_v
);
2796 int dispx
= Int_val (Field (args_v
, 0));
2797 int dispy
= Int_val (Field (args_v
, 1));
2798 int dispw
= Int_val (Field (args_v
, 2));
2799 int disph
= Int_val (Field (args_v
, 3));
2800 int tilex
= Int_val (Field (args_v
, 4));
2801 int tiley
= Int_val (Field (args_v
, 5));
2802 char *s
= String_val (ptr_v
);
2803 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2804 int slicey
, firstslice
;
2805 struct slice
*slice
;
2806 GLfloat
*texcoords
= state
.texcoords
;
2807 GLfloat
*vertices
= state
.vertices
;
2809 firstslice
= tiley
/ tile
->sliceheight
;
2810 slice
= &tile
->slices
[firstslice
];
2811 slicey
= tiley
% tile
->sliceheight
;
2816 dh
= slice
->h
- slicey
;
2817 dh
= MIN (disph
, dh
);
2818 uploadslice (tile
, slice
);
2820 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2821 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2822 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2823 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2825 vertices
[0] = dispx
; vertices
[1] = dispy
;
2826 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2827 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2828 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2830 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2834 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2837 CAMLreturn (Val_unit
);
2840 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2841 value xoff_v
, value yoff_v
,
2844 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2845 int xoff
= Int_val (xoff_v
);
2846 int yoff
= Int_val (yoff_v
);
2847 int noff
= Int_val (Field (li_v
, 0));
2848 char *targ
= String_val (Field (li_v
, 1));
2849 int tlen
= caml_string_length (Field (li_v
, 1));
2850 int hfsize
= Int_val (Field (li_v
, 2));
2851 char *s
= String_val (ptr_v
);
2852 int hlmask
= Int_val (hlinks_v
);
2853 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2856 /* deal with loadpage failed pages */
2862 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2863 if (trylock ("ml_postprocess")) {
2868 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2869 noff
= page
->slinkcount
;
2871 if (page
->tgen
== state
.gen
) {
2872 showsel (page
, xoff
, yoff
);
2874 unlock ("ml_postprocess");
2877 CAMLreturn (Val_int (noff
));
2880 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2885 const fz_matrix
*tctm
;
2887 if (!page
->annots
) return NULL
;
2889 switch (page
->type
) {
2891 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2892 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2896 tctm
= &fz_identity
;
2906 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2907 fz_invert_matrix (&ctm
, &ctm
);
2908 fz_transform_point (&p
, &ctm
);
2910 for (i
= 0; i
< page
->annotcount
; ++i
) {
2911 struct annot
*a
= &page
->annots
[i
];
2912 if (p
.x
>= a
->annot
->pagerect
.x0
&& p
.x
<= a
->annot
->pagerect
.x1
) {
2913 if (p
.y
>= a
->annot
->pagerect
.y0
&& p
.y
<= a
->annot
->pagerect
.y1
) {
2921 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2925 const fz_matrix
*tctm
;
2926 fz_link
*link
, *links
;
2928 switch (page
->type
) {
2930 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2931 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2932 links
= page
->u
.pdfpage
->links
;
2936 tctm
= &fz_identity
;
2937 links
= page
->u
.xpspage
->links
;
2946 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2947 fz_invert_matrix (&ctm
, &ctm
);
2948 fz_transform_point (&p
, &ctm
);
2950 for (link
= links
; link
; link
= link
->next
) {
2951 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2952 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2960 static void ensuretext (struct page
*page
)
2962 if (state
.gen
!= page
->tgen
) {
2964 page
->tgen
= state
.gen
;
2970 page
->text
= fz_new_text_page (state
.ctx
);
2971 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2972 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2973 ctm
= pagectm (page
);
2974 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2975 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2976 qsort (page
->text
->blocks
, page
->text
->len
,
2977 sizeof (*page
->text
->blocks
), compareblocks
);
2979 fz_free_device (tdev
);
2983 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2985 CAMLparam2 (start_page_v
, dir_v
);
2987 int i
, dir
= Int_val (dir_v
);
2988 int start_page
= Int_val (start_page_v
);
2989 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2991 ret_v
= Val_int (0);
2992 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2996 lock ("ml_findpage_with_links");
2997 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
3000 switch (state
.type
) {
3003 pdf_page
*page
= NULL
;
3005 fz_try (state
.ctx
) {
3006 page
= pdf_load_page (state
.u
.pdf
, i
);
3007 found
= !!page
->links
|| !!page
->annots
;
3009 fz_catch (state
.ctx
) {
3019 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
3020 found
= !!page
->links
;
3026 ARSERT (0 && "invalid document type");
3030 ret_v
= caml_alloc_small (1, 1);
3031 Field (ret_v
, 0) = Val_int (i
);
3036 unlock ("ml_findpage_with_links");
3042 enum { dir_first
, dir_last
};
3043 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
3045 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
3047 CAMLparam2 (ptr_v
, dir_v
);
3048 CAMLlocal2 (ret_v
, pos_v
);
3050 int dirtag
, i
, slinkindex
;
3051 struct slink
*found
= NULL
,*slink
;
3052 char *s
= String_val (ptr_v
);
3054 page
= parse_pointer ("ml_findlink", s
);
3055 ret_v
= Val_int (0);
3056 /* This is scary we are not taking locks here ensureslinks does
3057 not modify state and given that we obtained the page it can not
3058 disappear under us either */
3059 lock ("ml_findlink");
3060 ensureslinks (page
);
3062 if (Is_block (dir_v
)) {
3063 dirtag
= Tag_val (dir_v
);
3065 case dir_first_visible
:
3067 int x0
, y0
, dir
, first_index
, last_index
;
3069 pos_v
= Field (dir_v
, 0);
3070 x0
= Int_val (Field (pos_v
, 0));
3071 y0
= Int_val (Field (pos_v
, 1));
3072 dir
= Int_val (Field (pos_v
, 2));
3077 last_index
= page
->slinkcount
;
3080 first_index
= page
->slinkcount
- 1;
3084 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
3085 slink
= &page
->slinks
[i
];
3086 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
3095 slinkindex
= Int_val (Field (dir_v
, 0));
3096 found
= &page
->slinks
[slinkindex
];
3097 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3098 slink
= &page
->slinks
[i
];
3099 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
3107 slinkindex
= Int_val (Field (dir_v
, 0));
3108 found
= &page
->slinks
[slinkindex
];
3109 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3110 slink
= &page
->slinks
[i
];
3111 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
3119 slinkindex
= Int_val (Field (dir_v
, 0));
3120 found
= &page
->slinks
[slinkindex
];
3121 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
3122 slink
= &page
->slinks
[i
];
3123 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
3131 slinkindex
= Int_val (Field (dir_v
, 0));
3132 found
= &page
->slinks
[slinkindex
];
3133 for (i
= slinkindex
- 1; i
>= 0; --i
) {
3134 slink
= &page
->slinks
[i
];
3135 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
3144 dirtag
= Int_val (dir_v
);
3147 found
= page
->slinks
;
3152 found
= page
->slinks
+ (page
->slinkcount
- 1);
3158 ret_v
= caml_alloc_small (2, 1);
3159 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
3162 unlock ("ml_findlink");
3166 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
3167 unamed
, uremote
, uremotedest
, uannot
};
3173 switch (link->dest.kind) { \
3174 case FZ_LINK_GOTO: \
3178 pageno = link->dest.ld.gotor.page; \
3182 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
3183 p.y = link->dest.ld.gotor.lt.y; \
3184 fz_transform_point (&p, &pdim->lctm); \
3185 if (p.y < 0) p.y = 0; \
3187 tup_v = caml_alloc_tuple (2); \
3188 ret_v = caml_alloc_small (1, ugoto); \
3189 Field (tup_v, 0) = Val_int (pageno); \
3190 Field (tup_v, 1) = Val_int (p.y); \
3191 Field (ret_v, 0) = tup_v; \
3196 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3197 ret_v = caml_alloc_small (1, uuri); \
3198 Field (ret_v, 0) = str_v; \
3201 case FZ_LINK_LAUNCH: \
3202 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3203 ret_v = caml_alloc_small (1, ulaunch); \
3204 Field (ret_v, 0) = str_v; \
3207 case FZ_LINK_NAMED: \
3208 str_v = caml_copy_string (link->dest.ld.named.named); \
3209 ret_v = caml_alloc_small (1, unamed); \
3210 Field (ret_v, 0) = str_v; \
3213 case FZ_LINK_GOTOR: \
3217 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3218 pageno = link->dest.ld.gotor.page; \
3219 if (pageno == -1) { \
3220 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3221 rty = uremotedest; \
3224 gr_v = Val_int (pageno); \
3227 tup_v = caml_alloc_tuple (2); \
3228 ret_v = caml_alloc_small (1, rty); \
3229 Field (tup_v, 0) = str_v; \
3230 Field (tup_v, 1) = gr_v; \
3231 Field (ret_v, 0) = tup_v; \
3239 snprintf (buf, sizeof (buf), \
3240 "unhandled link kind %d", link->dest.kind); \
3241 str_v = caml_copy_string (buf); \
3242 ret_v = caml_alloc_small (1, uunexpected); \
3243 Field (ret_v, 0) = str_v; \
3249 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3251 CAMLparam2 (ptr_v
, n_v
);
3252 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3255 struct pagedim
*pdim
;
3256 char *s
= String_val (ptr_v
);
3257 struct slink
*slink
;
3259 /* See ml_findlink for caveat */
3261 ret_v
= Val_int (0);
3262 page
= parse_pointer ("ml_getlink", s
);
3263 ensureslinks (page
);
3264 pdim
= &state
.pagedims
[page
->pdimno
];
3265 slink
= &page
->slinks
[Int_val (n_v
)];
3266 if (slink
->tag
== SLINK
) {
3267 link
= slink
->u
.link
;
3271 ret_v
= caml_alloc_small (1, uannot
);
3272 tup_v
= caml_alloc_tuple (2);
3273 Field (ret_v
, 0) = tup_v
;
3274 Field (tup_v
, 0) = ptr_v
;
3275 Field (tup_v
, 1) = n_v
;
3278 unlock ("ml_getlink");
3282 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
3284 CAMLparam2 (ptr_v
, n_v
);
3285 char *s
= String_val (ptr_v
);
3287 struct slink
*slink
;
3289 page
= parse_pointer ("ml_getannotcontent", s
);
3290 slink
= &page
->slinks
[Int_val (n_v
)];
3291 CAMLreturn (caml_copy_string (pdf_annot_contents (state
.u
.pdf
,
3295 CAMLprim value
ml_getlinkcount (value ptr_v
)
3299 char *s
= String_val (ptr_v
);
3301 page
= parse_pointer ("ml_getlinkcount", s
);
3302 CAMLreturn (Val_int (page
->slinkcount
));
3305 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3307 CAMLparam2 (ptr_v
, n_v
);
3310 struct slink
*slink
;
3311 char *s
= String_val (ptr_v
);
3312 /* See ml_findlink for caveat */
3314 page
= parse_pointer ("ml_getlinkrect", s
);
3315 ret_v
= caml_alloc_tuple (4);
3316 ensureslinks (page
);
3318 slink
= &page
->slinks
[Int_val (n_v
)];
3319 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3320 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3321 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3322 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3323 unlock ("ml_getlinkrect");
3327 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3329 CAMLparam3 (ptr_v
, x_v
, y_v
);
3330 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3332 struct annot
*annot
;
3334 char *ptr
= String_val (ptr_v
);
3335 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3336 struct pagedim
*pdim
;
3338 ret_v
= Val_int (0);
3339 if (trylock ("ml_whatsunder")) {
3343 page
= parse_pointer ("ml_whatsunder", ptr
);
3344 pdim
= &state
.pagedims
[page
->pdimno
];
3345 x
+= pdim
->bounds
.x0
;
3346 y
+= pdim
->bounds
.y0
;
3348 if (state
.type
== DPDF
) {
3349 annot
= getannot (page
, x
, y
);
3353 ensureslinks (page
);
3354 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3355 if (page
->slinks
[i
].tag
== SANNOT
3356 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3361 ret_v
= caml_alloc_small (1, uannot
);
3362 tup_v
= caml_alloc_tuple (2);
3363 Field (ret_v
, 0) = tup_v
;
3364 Field (tup_v
, 0) = ptr_v
;
3365 Field (tup_v
, 1) = Val_int (n
);
3370 link
= getlink (page
, x
, y
);
3376 fz_page_block
*pageb
;
3377 fz_text_block
*block
;
3380 for (pageb
= page
->text
->blocks
;
3381 pageb
< page
->text
->blocks
+ page
->text
->len
;
3384 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3385 block
= pageb
->u
.text
;
3388 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3391 for (line
= block
->lines
;
3392 line
< block
->lines
+ block
->len
;
3397 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3400 for (span
= line
->first_span
; span
; span
= span
->next
) {
3404 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3407 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3409 fz_text_char_bbox (&bbox
, span
, charnum
);
3412 if (x
>= b
->x0
&& x
<= b
->x1
3413 && y
>= b
->y0
&& y
<= b
->y1
) {
3414 fz_text_style
*style
= span
->text
->style
;
3416 style
->font
&& style
->font
->name
3418 : "Span has no font name"
3420 FT_FaceRec
*face
= style
->font
->ft_face
;
3421 if (face
&& face
->family_name
) {
3423 char *n1
= face
->family_name
;
3424 size_t l1
= strlen (n1
);
3425 size_t l2
= strlen (n2
);
3427 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3428 s
= malloc (l1
+ l2
+ 2);
3432 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3433 str_v
= caml_copy_string (s
);
3438 if (str_v
== Val_unit
) {
3439 str_v
= caml_copy_string (n2
);
3441 ret_v
= caml_alloc_small (1, utext
);
3442 Field (ret_v
, 0) = str_v
;
3451 unlock ("ml_whatsunder");
3457 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3459 static int uninteresting (int c
)
3461 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3465 CAMLprim value
ml_clearmark (value ptr_v
)
3468 char *s
= String_val (ptr_v
);
3471 if (trylock ("ml_clearmark")) {
3475 page
= parse_pointer ("ml_clearmark", s
);
3476 page
->fmark
.span
= NULL
;
3477 page
->lmark
.span
= NULL
;
3481 unlock ("ml_clearmark");
3483 CAMLreturn (Val_unit
);
3486 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3488 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3493 fz_page_block
*pageb
;
3494 fz_text_block
*block
;
3495 struct pagedim
*pdim
;
3496 int mark
= Int_val (mark_v
);
3497 char *s
= String_val (ptr_v
);
3498 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3500 ret_v
= Val_bool (0);
3501 if (trylock ("ml_markunder")) {
3505 page
= parse_pointer ("ml_markunder", s
);
3506 pdim
= &state
.pagedims
[page
->pdimno
];
3510 if (mark
== mark_page
) {
3512 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3514 for (i
= 0; i
< page
->text
->len
; ++i
) {
3515 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3516 pb1
= &page
->text
->blocks
[i
];
3520 if (!pb1
) goto unlock
;
3522 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3523 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3524 pb2
= &page
->text
->blocks
[i
];
3528 if (!pb2
) goto unlock
;
3530 block
= pb1
->u
.text
;
3533 page
->fmark
.span
= block
->lines
->first_span
;
3535 block
= pb2
->u
.text
;
3536 line
= &block
->lines
[block
->len
- 1];
3537 page
->lmark
.i
= line
->last_span
->len
- 1;
3538 page
->lmark
.span
= line
->last_span
;
3539 ret_v
= Val_bool (1);
3543 x
+= pdim
->bounds
.x0
;
3544 y
+= pdim
->bounds
.y0
;
3546 for (pageb
= page
->text
->blocks
;
3547 pageb
< page
->text
->blocks
+ page
->text
->len
;
3549 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3550 block
= pageb
->u
.text
;
3553 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3556 if (mark
== mark_block
) {
3558 page
->fmark
.span
= block
->lines
->first_span
;
3560 line
= &block
->lines
[block
->len
- 1];
3561 page
->lmark
.i
= line
->last_span
->len
- 1;
3562 page
->lmark
.span
= line
->last_span
;
3563 ret_v
= Val_bool (1);
3567 for (line
= block
->lines
;
3568 line
< block
->lines
+ block
->len
;
3573 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3576 if (mark
== mark_line
) {
3578 page
->fmark
.span
= line
->first_span
;
3580 page
->lmark
.i
= line
->last_span
->len
- 1;
3581 page
->lmark
.span
= line
->last_span
;
3582 ret_v
= Val_bool (1);
3586 for (span
= line
->first_span
; span
; span
= span
->next
) {
3590 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3593 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3595 fz_text_char_bbox (&bbox
, span
, charnum
);
3598 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3600 int charnum2
, charnum3
= -1, charnum4
= -1;
3602 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3604 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3605 if (uninteresting (span
->text
[charnum2
].c
)) {
3606 charnum3
= charnum2
+ 1;
3610 if (charnum3
== -1) charnum3
= 0;
3612 for (charnum2
= charnum
+ 1;
3613 charnum2
< span
->len
;
3615 if (uninteresting (span
->text
[charnum2
].c
)) break;
3616 charnum4
= charnum2
;
3618 if (charnum4
== -1) goto unlock
;
3620 page
->fmark
.i
= charnum3
;
3621 page
->fmark
.span
= span
;
3623 page
->lmark
.i
= charnum4
;
3624 page
->lmark
.span
= span
;
3625 ret_v
= Val_bool (1);
3633 if (!Bool_val (ret_v
)) {
3634 page
->fmark
.span
= NULL
;
3635 page
->lmark
.span
= NULL
;
3639 unlock ("ml_markunder");
3645 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3647 CAMLparam3 (ptr_v
, x_v
, y_v
);
3648 CAMLlocal2 (ret_v
, res_v
);
3651 fz_page_block
*pageb
;
3652 struct pagedim
*pdim
;
3653 char *s
= String_val (ptr_v
);
3654 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3656 ret_v
= Val_int (0);
3657 if (trylock ("ml_rectofblock")) {
3661 page
= parse_pointer ("ml_rectofblock", s
);
3662 pdim
= &state
.pagedims
[page
->pdimno
];
3663 x
+= pdim
->bounds
.x0
;
3664 y
+= pdim
->bounds
.y0
;
3668 for (pageb
= page
->text
->blocks
;
3669 pageb
< page
->text
->blocks
+ page
->text
->len
;
3671 switch (pageb
->type
) {
3672 case FZ_PAGE_BLOCK_TEXT
:
3673 b
= &pageb
->u
.text
->bbox
;
3676 case FZ_PAGE_BLOCK_IMAGE
:
3677 b
= &pageb
->u
.image
->bbox
;
3684 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3689 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3690 ret_v
= caml_alloc_small (1, 1);
3691 Store_double_field (res_v
, 0, b
->x0
);
3692 Store_double_field (res_v
, 1, b
->x1
);
3693 Store_double_field (res_v
, 2, b
->y0
);
3694 Store_double_field (res_v
, 3, b
->y1
);
3695 Field (ret_v
, 0) = res_v
;
3697 unlock ("ml_rectofblock");
3703 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3705 CAMLparam2 (ptr_v
, rect_v
);
3708 struct pagedim
*pdim
;
3709 char *s
= String_val (ptr_v
);
3710 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3712 fz_page_block
*pageb
;
3713 fz_text_block
*block
;
3714 fz_text_span
*span
, *fspan
, *lspan
;
3716 if (trylock ("ml_seltext")) {
3720 page
= parse_pointer ("ml_seltext", s
);
3723 pdim
= &state
.pagedims
[page
->pdimno
];
3724 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3725 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3726 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3727 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3738 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3739 glColor3ub (128, 128, 128);
3740 recti (x0
, y0
, x1
, y1
);
3741 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3745 fspan
= page
->fmark
.span
;
3748 lspan
= page
->lmark
.span
;
3750 for (pageb
= page
->text
->blocks
;
3751 pageb
< page
->text
->blocks
+ page
->text
->len
;
3753 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3754 block
= pageb
->u
.text
;
3755 for (line
= block
->lines
;
3756 line
< block
->lines
+ block
->len
;
3759 for (span
= line
->first_span
; span
; span
= span
->next
) {
3760 for (i
= 0; i
< span
->len
; ++i
) {
3763 fz_text_char_bbox (&b
, span
, i
);
3765 if (x0
>= b
.x0
&& x0
<= b
.x1
3766 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3771 if (x1
>= b
.x0
&& x1
<= b
.x1
3772 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3777 if (0 && selected
) {
3778 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3779 glColor3ub (128, 128, 128);
3780 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3781 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3787 if (x1
< x0
&& fspan
== lspan
) {
3799 page
->fmark
.span
= fspan
;
3802 page
->lmark
.span
= lspan
;
3804 unlock ("ml_seltext");
3807 CAMLreturn (Val_unit
);
3810 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3815 for (i
= a
; i
<= b
; ++i
) {
3816 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3817 ret
= fwrite (buf
, len
, 1, f
);
3820 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3821 len
, ret
, strerror (errno
));
3829 CAMLprim value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3831 caml_failwith ("ml_popen not implemented under Cygwin");
3834 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3836 CAMLparam2 (command_v
, fds_v
);
3837 CAMLlocal2 (l_v
, tup_v
);
3841 value earg_v
= Nothing
;
3842 posix_spawnattr_t attr
;
3843 posix_spawn_file_actions_t fa
;
3844 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3846 argv
[2] = String_val (command_v
);
3848 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3849 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3852 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3853 msg
= "posix_spawnattr_init";
3857 #ifdef POSIX_SPAWN_USEVFORK
3858 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3859 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3864 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3867 tup_v
= Field (l_v
, 0);
3868 fd1
= Int_val (Field (tup_v
, 0));
3869 fd2
= Int_val (Field (tup_v
, 1));
3871 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3872 msg
= "posix_spawn_file_actions_addclose";
3878 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3879 msg
= "posix_spawn_file_actions_adddup2";
3886 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3887 msg
= "posix_spawn";
3892 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3893 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3897 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3898 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3903 unix_error (ret
, msg
, earg_v
);
3905 CAMLreturn (Val_int (pid
));
3909 CAMLprim value
ml_hassel (value ptr_v
)
3914 char *s
= String_val (ptr_v
);
3916 ret_v
= Val_bool (0);
3917 if (trylock ("ml_hassel")) {
3921 page
= parse_pointer ("ml_hassel", s
);
3922 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3923 unlock ("ml_hassel");
3928 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3930 CAMLparam2 (fd_v
, ptr_v
);
3935 fz_page_block
*pageb
;
3936 fz_text_block
*block
;
3937 int fd
= Int_val (fd_v
);
3938 char *s
= String_val (ptr_v
);
3940 if (trylock ("ml_copysel")) {
3944 page
= parse_pointer ("ml_copysel", s
);
3946 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3947 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3951 f
= fdopen (fd
, "w");
3953 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3954 fd
, strerror (errno
));
3958 for (pageb
= page
->text
->blocks
;
3959 pageb
< page
->text
->blocks
+ page
->text
->len
;
3961 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3962 block
= pageb
->u
.text
;
3963 for (line
= block
->lines
;
3964 line
< block
->lines
+ block
->len
;
3968 for (span
= line
->first_span
; span
; span
= span
->next
) {
3971 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3972 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3973 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3976 if (pipespan (f
, span
, a
, b
)) {
3979 if (span
== page
->lmark
.span
) {
3982 if (span
== line
->last_span
) {
3983 if (putc ('\n', f
) == EOF
) {
3985 "failed break line on sel pipe: %s\n",
3996 int ret
= fclose (f
);
3999 if (errno
!= ECHILD
) {
4000 fprintf (stderr
, "failed to close sel pipe: %s\n",
4006 unlock ("ml_copysel");
4011 fprintf (stderr
, "failed to close sel pipe: %s\n",
4015 CAMLreturn (Val_unit
);
4018 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
4020 CAMLparam1 (pagedimno_v
);
4022 int pagedimno
= Int_val (pagedimno_v
);
4025 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
4026 if (trylock ("ml_getpdimrect")) {
4027 box
= fz_empty_rect
;
4030 box
= state
.pagedims
[pagedimno
].mediabox
;
4031 unlock ("ml_getpdimrect");
4034 Store_double_field (ret_v
, 0, box
.x0
);
4035 Store_double_field (ret_v
, 1, box
.x1
);
4036 Store_double_field (ret_v
, 2, box
.y0
);
4037 Store_double_field (ret_v
, 3, box
.y1
);
4042 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
4043 value dw_v
, value cols_v
)
4045 CAMLparam3 (winw_v
, winh_v
, dw_v
);
4051 double winw
= Int_val (winw_v
);
4052 double winh
= Int_val (winh_v
);
4053 double dw
= Int_val (dw_v
);
4054 double cols
= Int_val (cols_v
);
4055 double pw
= 1.0, ph
= 1.0;
4057 if (trylock ("ml_zoom_for_height")) {
4061 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
4062 double w
= p
->pagebox
.x1
/ cols
;
4063 double h
= p
->pagebox
.y1
;
4067 if (state
.fitmodel
!= FitProportional
) pw
= w
;
4069 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
4072 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
4073 unlock ("ml_zoom_for_height");
4075 ret_v
= caml_copy_double (zoom
);
4079 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
4081 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
4083 int pt
= Int_val(pt_v
);
4084 int x
= Int_val (x_v
);
4085 int y
= Int_val (y_v
);
4088 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
4089 ret_v
= caml_copy_double (w
);
4093 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
4095 CAMLparam2 (pt_v
, string_v
);
4097 int pt
= Int_val (pt_v
);
4100 w
= measure_string (state
.face
, pt
, String_val (string_v
));
4101 ret_v
= caml_copy_double (w
);
4105 CAMLprim value
ml_getpagebox (value opaque_v
)
4107 CAMLparam1 (opaque_v
);
4113 char *s
= String_val (opaque_v
);
4114 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
4116 ret_v
= caml_alloc_tuple (4);
4117 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
4118 dev
->hints
|= FZ_IGNORE_SHADE
;
4120 switch (page
->type
) {
4122 ctm
= pagectm (page
);
4123 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
4127 ctm
= pagectm (page
);
4128 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
4132 rect
= fz_infinite_rect
;
4136 fz_free_device (dev
);
4137 fz_round_rect (&bbox
, &rect
);
4138 Field (ret_v
, 0) = Val_int (bbox
.x0
);
4139 Field (ret_v
, 1) = Val_int (bbox
.y0
);
4140 Field (ret_v
, 2) = Val_int (bbox
.x1
);
4141 Field (ret_v
, 3) = Val_int (bbox
.y1
);
4146 CAMLprim value
ml_setaalevel (value level_v
)
4148 CAMLparam1 (level_v
);
4150 state
.aalevel
= Int_val (level_v
);
4151 CAMLreturn (Val_unit
);
4154 #pragma GCC diagnostic push
4155 #pragma GCC diagnostic ignored "-Wvariadic-macros"
4156 #include <X11/Xlib.h>
4157 #pragma GCC diagnostic pop
4165 XVisualInfo
*visual
;
4168 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4170 CAMLparam3 (display_v
, wid_v
, screen_v
);
4171 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4173 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4175 caml_failwith ("XOpenDisplay");
4178 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4180 XCloseDisplay (glx
.dpy
);
4181 caml_failwith ("glXChooseVisual");
4184 glx
.wid
= Int_val (wid_v
);
4185 CAMLreturn (Val_int (glx
.visual
->visualid
));
4188 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4190 CAMLparam1 (unit_v
);
4192 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4194 caml_failwith ("glXCreateContext");
4200 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4201 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4203 caml_failwith ("glXMakeCurrent");
4205 CAMLreturn (Val_unit
);
4208 CAMLprim value
ml_swapb (value unit_v
)
4210 CAMLparam1 (unit_v
);
4211 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4212 CAMLreturn (Val_unit
);
4215 #include "keysym2ucs.c"
4217 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4219 CAMLparam1 (keysym_v
);
4221 KeySym keysym
= Int_val (keysym_v
);
4226 rune
= keysym2ucs (keysym
);
4227 len
= fz_runetochar (buf
, rune
);
4229 str_v
= caml_copy_string (buf
);
4233 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4235 CAMLprim value
ml_platform (value unit_v
)
4237 CAMLparam1 (unit_v
);
4238 CAMLlocal2 (tup_v
, arr_v
);
4239 int platid
= piunknown
;
4242 #if defined __linux__
4244 #elif defined __CYGWIN__
4246 #elif defined __DragonFly__ || defined __FreeBSD__
4247 || defined __OpenBSD__
|| defined __NetBSD__
4249 #elif defined __sun__
4251 #elif defined __APPLE__
4254 if (uname (&buf
)) err (1, "uname");
4256 tup_v
= caml_alloc_tuple (2);
4258 char const *sar
[] = {
4265 arr_v
= caml_copy_string_array (sar
);
4267 Field (tup_v
, 0) = Val_int (platid
);
4268 Field (tup_v
, 1) = arr_v
;
4272 CAMLprim value
ml_cloexec (value fd_v
)
4275 int fd
= Int_val (fd_v
);
4277 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4278 uerror ("fcntl", Nothing
);
4280 CAMLreturn (Val_unit
);
4283 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4285 CAMLparam2 (w_v
, h_v
);
4288 int w
= Int_val (w_v
);
4289 int h
= Int_val (h_v
);
4290 int cs
= Int_val (cs_v
);
4292 if (state
.pbo_usable
) {
4293 pbo
= calloc (sizeof (*pbo
), 1);
4295 err (1, "calloc pbo");
4307 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4310 state
.glGenBuffersARB (1, &pbo
->id
);
4311 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4312 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4313 NULL
, GL_STREAM_DRAW
);
4314 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4316 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4318 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4319 state
.glDeleteBuffersARB (1, &pbo
->id
);
4321 ret_v
= caml_copy_string ("0");
4327 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4329 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4333 err (1, "malloc %d bytes failed", res
+1);
4335 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4337 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4339 ret_v
= caml_copy_string (s
);
4344 ret_v
= caml_copy_string ("0");
4349 CAMLprim value
ml_freepbo (value s_v
)
4352 char *s
= String_val (s_v
);
4353 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4356 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4358 tile
->pbo
->ptr
= NULL
;
4359 tile
->pbo
->size
= -1;
4361 CAMLreturn (Val_unit
);
4364 CAMLprim value
ml_unmappbo (value s_v
)
4367 char *s
= String_val (s_v
);
4368 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4371 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4372 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4373 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4375 tile
->pbo
->ptr
= NULL
;
4376 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4378 CAMLreturn (Val_unit
);
4381 static void setuppbo (void)
4383 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4384 state
.pbo_usable
= GGPA (glBindBufferARB
)
4385 && GGPA (glUnmapBufferARB
)
4386 && GGPA (glMapBufferARB
)
4387 && GGPA (glBufferDataARB
)
4388 && GGPA (glGenBuffersARB
)
4389 && GGPA (glDeleteBuffersARB
);
4393 CAMLprim value
ml_pbo_usable (value unit_v
)
4395 CAMLparam1 (unit_v
);
4396 CAMLreturn (Val_bool (state
.pbo_usable
));
4399 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4401 CAMLparam3 (ptr_v
, x_v
, y_v
);
4402 CAMLlocal2 (ret_v
, tup_v
);
4404 char *s
= String_val (ptr_v
);
4405 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4406 struct pagedim
*pdim
;
4410 page
= parse_pointer ("ml_unproject", s
);
4411 pdim
= &state
.pagedims
[page
->pdimno
];
4413 ret_v
= Val_int (0);
4414 if (trylock ("ml_unproject")) {
4418 switch (page
->type
) {
4420 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4426 p
.x
= x
+ pdim
->bounds
.x0
;
4427 p
.y
= y
+ pdim
->bounds
.y0
;
4429 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4430 fz_invert_matrix (&ctm
, &ctm
);
4431 fz_transform_point (&p
, &ctm
);
4433 tup_v
= caml_alloc_tuple (2);
4434 ret_v
= caml_alloc_small (1, 1);
4435 Field (tup_v
, 0) = Val_int (p
.x
);
4436 Field (tup_v
, 1) = Val_int (p
.y
);
4437 Field (ret_v
, 0) = tup_v
;
4439 unlock ("ml_unproject");
4444 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4447 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4449 if (state
.type
== DPDF
) {
4454 char *s
= String_val (ptr_v
);
4457 page
= parse_pointer ("ml_addannot", s
);
4458 annot
= pdf_create_annot (pdf
, page
->u
.pdfpage
, FZ_ANNOT_TEXT
);
4459 p
.x
= Int_val (x_v
);
4460 p
.y
= Int_val (y_v
);
4461 pdf_set_annot_contents (pdf
, annot
, String_val (contents_v
));
4462 pdf_set_text_annot_position (pdf
, annot
, p
);
4464 CAMLreturn (Val_unit
);
4467 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4469 CAMLparam2 (ptr_v
, n_v
);
4471 if (state
.type
== DPDF
) {
4473 char *s
= String_val (ptr_v
);
4474 struct slink
*slink
;
4476 page
= parse_pointer ("ml_delannot", s
);
4477 slink
= &page
->slinks
[Int_val (n_v
)];
4478 pdf_delete_annot (state
.u
.pdf
, page
->u
.pdfpage
, slink
->u
.annot
);
4480 CAMLreturn (Val_unit
);
4483 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4485 CAMLparam3 (ptr_v
, n_v
, str_v
);
4487 if (state
.type
== DPDF
) {
4489 char *s
= String_val (ptr_v
);
4490 struct slink
*slink
;
4492 page
= parse_pointer ("ml_modannot", s
);
4493 slink
= &page
->slinks
[Int_val (n_v
)];
4494 pdf_set_annot_contents (state
.u
.pdf
, slink
->u
.annot
,
4495 String_val (str_v
));
4497 state
.u
.pdf
->dirty
= 1;
4499 CAMLreturn (Val_unit
);
4502 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4504 CAMLparam1 (unit_v
);
4507 if (state
.type
== DPDF
&& state
.u
.pdf
) {
4508 modified
= pdf_has_unsaved_changes (state
.u
.pdf
);
4510 CAMLreturn (Val_bool (modified
));
4513 CAMLprim value
ml_savedoc (value path_v
)
4515 CAMLparam1 (path_v
);
4517 if (state
.type
== DPDF
) {
4518 pdf_write_document (state
.u
.pdf
, String_val (path_v
), NULL
);
4520 CAMLreturn (Val_unit
);
4523 static void makestippletex (void)
4525 const char pixels
[] = "\xff\xff\0\0";
4526 glGenTextures (1, &state
.stid
);
4527 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4528 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4529 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4542 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4544 return caml_copy_string (FZ_VERSION
);
4547 #ifdef USE_FONTCONFIG
4553 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4557 int UNUSED_ATTR needs_exact_metrics
)
4564 FcPattern
*pat
, *pat1
;
4566 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4567 name
, bold
, italic
, needs_exact_metrics
);
4570 fc
.config
= FcInitLoadConfigAndFonts ();
4572 lprintf ("FcInitLoadConfigAndFonts failed\n");
4576 if (!fc
.config
) return NULL
;
4578 size
= strlen (name
);
4579 if (bold
) size
+= sizeof (":bold") - 1;
4580 if (italic
) size
+= sizeof (":italic") - 1;
4583 buf
= malloc (size
);
4585 err (1, "malloc %zu failed", size
);
4589 if (bold
&& italic
) {
4590 strcat (buf
, ":bold:italic");
4593 if (bold
) strcat (buf
, ":bold");
4594 if (italic
) strcat (buf
, ":italic");
4596 for (i
= 0; i
< size
; ++i
) {
4597 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4600 lprintf ("fcbuf=%s\n", buf
);
4601 pat
= FcNameParse ((FcChar8
*) buf
);
4603 printd ("emsg FcNameParse failed\n");
4608 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4609 printd ("emsg FcConfigSubstitute failed\n");
4613 FcDefaultSubstitute (pat
);
4615 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4617 printd ("emsg FcFontMatch failed\n");
4618 FcPatternDestroy (pat
);
4623 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4624 printd ("emsg FcPatternGetString failed\n");
4625 FcPatternDestroy (pat
);
4626 FcPatternDestroy (pat1
);
4632 printd ("emsg name=%s path=%s\n", name
, path
);
4634 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4635 FcPatternDestroy (pat
);
4636 FcPatternDestroy (pat1
);
4642 CAMLprim value
ml_init (value csock_v
, value params_v
)
4644 CAMLparam2 (csock_v
, params_v
);
4645 CAMLlocal2 (trim_v
, fuzz_v
);
4653 state
.csock
= Int_val (csock_v
);
4654 state
.rotate
= Int_val (Field (params_v
, 0));
4655 state
.fitmodel
= Int_val (Field (params_v
, 1));
4656 trim_v
= Field (params_v
, 2);
4657 texcount
= Int_val (Field (params_v
, 3));
4658 state
.sliceheight
= Int_val (Field (params_v
, 4));
4659 mustoresize
= Int_val (Field (params_v
, 5));
4660 colorspace
= Int_val (Field (params_v
, 6));
4661 fontpath
= String_val (Field (params_v
, 7));
4663 if (caml_string_length (Field (params_v
, 8)) > 0) {
4664 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4666 if (!state
.trimcachepath
) {
4667 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4671 haspboext
= Bool_val (Field (params_v
, 9));
4673 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4675 #ifdef USE_FONTCONFIG
4676 if (Bool_val (Field (params_v
, 10))) {
4677 fz_install_load_system_font_funcs (
4678 state
.ctx
, fc_load_system_font_func
, NULL
4683 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4684 fuzz_v
= Field (trim_v
, 1);
4685 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4686 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4687 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4688 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4690 set_tex_params (colorspace
);
4693 #ifndef USE_FONTCONFIG
4694 state
.face
= load_font (fontpath
);
4698 char *buf
= fontpath
;
4699 FcPattern
*pat
, *pat1
;
4702 fc
.config
= FcInitLoadConfigAndFonts ();
4704 errx (1, "FcInitLoadConfigAndFonts failed");
4707 pat
= FcNameParse ((FcChar8
*) buf
);
4709 errx (1, "FcNameParse failed");
4712 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4713 errx (1, "FcConfigSubstitute failed");
4715 FcDefaultSubstitute (pat
);
4717 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4719 errx (1, "FcFontMatch failed");
4722 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4723 errx (1, "FcPatternGetString failed");
4726 state
.face
= load_font ((char *) path
);
4727 FcPatternDestroy (pat
);
4728 FcPatternDestroy (pat1
);
4733 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4735 state
.face
= load_builtin_font (base
, len
);
4737 if (!state
.face
) _exit (1);
4739 realloctexts (texcount
);
4747 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4749 errx (1, "pthread_create: %s", strerror (ret
));
4752 CAMLreturn (Val_unit
);