1 /* lots of code c&p-ed directly from mupdf */
2 #define CAML_NAME_SPACE
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/utsname.h>
21 #include <cygwin/socket.h> /* FIONREAD */
33 #include <caml/fail.h>
34 #include <caml/alloc.h>
35 #include <caml/memory.h>
36 #include <caml/unixsupport.h>
38 #if __GNUC__ < 5 && !defined __clang__
39 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
40 clobbered diagnostics */
41 #pragma GCC diagnostic ignored "-Wclobbered"
44 #include <mupdf/fitz.h>
45 #include <mupdf/pdf.h>
48 #include FT_FREETYPE_H
51 #include <fontconfig/fontconfig.h>
55 #define CACHE_PAGEREFS
58 extern char **environ
;
62 #define NORETURN_ATTR __attribute__ ((noreturn))
63 #define UNUSED_ATTR __attribute__ ((unused))
64 #if !defined __clang__
65 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
67 #define OPTIMIZE_ATTR(n)
69 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
73 #define OPTIMIZE_ATTR(n)
74 #define GCC_FMT_ATTR(a, b)
79 #define FMT_ptr PRIxPTR
80 #define SCN_ptr SCNxPTR
81 #define FMT_ptr_cast(p) ((uintptr_t) (p))
82 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
84 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
85 err (int exitcode
, const char *fmt
, ...)
92 vfprintf (stderr
, fmt
, ap
);
94 fprintf (stderr
, ": %s\n", strerror (savederrno
));
99 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
100 errx (int exitcode
, const char *fmt
, ...)
105 vfprintf (stderr
, fmt
, ap
);
107 fputc ('\n', stderr
);
112 #ifndef GL_TEXTURE_RECTANGLE_ARB
113 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
117 #define TEXT_TYPE GL_TEXTURE_2D
119 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
123 #define GL_BGRA 0x80E1
126 #ifndef GL_UNSIGNED_INT_8_8_8_8
127 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
130 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
131 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
135 #define lprintf printf
140 #define ARSERT(cond) for (;;) { \
142 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
158 struct slice slices
[1];
169 fz_matrix ctm
, zoomctm
, tctm
;
173 enum { SLINK
, SANNOT
} tag
;
193 fz_stext_sheet
*sheet
;
195 fz_display_list
*dlist
;
197 struct slink
*slinks
;
199 struct annot
*annots
;
208 struct pagedim
*pagedims
;
223 fz_colorspace
*colorspace
;
231 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
254 void (*glBindBufferARB
) (GLenum
, GLuint
);
255 GLboolean (*glUnmapBufferARB
) (GLenum
);
256 void *(*glMapBufferARB
) (GLenum
, GLenum
);
257 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
258 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
259 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
261 GLfloat texcoords
[8];
262 GLfloat vertices
[16];
264 #ifdef CACHE_PAGEREFS
280 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
282 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
285 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
287 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
290 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
292 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
293 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
296 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
298 static void lock (const char *cap
)
300 int ret
= pthread_mutex_lock (&mutex
);
302 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
306 static void unlock (const char *cap
)
308 int ret
= pthread_mutex_unlock (&mutex
);
310 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
314 static int trylock (const char *cap
)
316 int ret
= pthread_mutex_trylock (&mutex
);
317 if (ret
&& ret
!= EBUSY
) {
318 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
323 static void *parse_pointer (const char *cap
, const char *s
)
328 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
330 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
335 static double now (void)
339 if (gettimeofday (&tv
, NULL
)) {
340 err (1, "gettimeofday");
342 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
345 static int hasdata (void)
348 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
349 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
353 CAMLprim value
ml_hasdata (value fd_v
)
358 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
359 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
360 CAMLreturn (Val_bool (avail
> 0));
363 static void readdata (int fd
, void *p
, int size
)
368 n
= read (fd
, p
, size
);
370 if (errno
== EINTR
) goto again
;
371 err (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
374 if (!n
) errx (1, "EOF while reading");
375 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
379 static void writedata (int fd
, char *p
, int size
)
383 /* One should lookup type punning/strict aliasing etc in standard,DRs,Web to
384 convince herself that this is:
386 b. practically the only way to achieve the result
387 (union puns notwithstanding) */
388 memcpy (p
, &size
, 4);
389 n
= write (fd
, p
, size
+ 4);
391 if (!n
) errx (1, "EOF while writing data");
392 err (1, "write (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
396 static int readlen (int fd
)
398 /* Type punned unions here. Why? Less code (Adjusted by more comments).
399 https://en.wikipedia.org/wiki/Type_punning */
400 union { int len
; char raw
[4]; } buf
;
401 readdata (fd
, buf
.raw
, 4);
405 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
407 CAMLparam3 (fd_v
, bytes_v
, len_v
);
408 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
412 CAMLprim value
ml_rcmd (value fd_v
)
415 CAMLlocal1 (strdata_v
);
416 int fd
= Int_val (fd_v
);
417 int len
= readlen (fd
);
418 strdata_v
= caml_alloc_string (len
);
419 readdata (fd
, String_val (strdata_v
), len
);
420 CAMLreturn (strdata_v
);
423 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
431 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
434 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
438 if (len
< size
- 4) {
439 writedata (state
.csock
, buf
, len
);
445 err (1, "vsnprintf for `%s' failed", fmt
);
447 buf
= realloc (buf
, size
);
452 static void closedoc (void)
454 #ifdef CACHE_PAGEREFS
455 if (state
.pdflut
.objs
) {
458 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
459 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
461 free (state
.pdflut
.objs
);
462 state
.pdflut
.objs
= NULL
;
463 state
.pdflut
.idx
= 0;
467 fz_drop_document (state
.ctx
, state
.doc
);
472 static int openxref (char *filename
, char *password
)
476 for (i
= 0; i
< state
.texcount
; ++i
) {
477 state
.texowners
[i
].w
= -1;
478 state
.texowners
[i
].slice
= NULL
;
484 if (state
.pagedims
) {
485 free (state
.pagedims
);
486 state
.pagedims
= NULL
;
488 state
.pagedimcount
= 0;
490 fz_set_aa_level (state
.ctx
, state
.aalevel
);
491 #ifdef CSS_HACK_TO_READ_EPUBS_COMFORTABLY
492 fz_set_user_css (state
.ctx
,
493 "body { margin-left: 20%; margin-right: 20%; }");
495 state
.doc
= fz_open_document (state
.ctx
, filename
);
496 if (fz_needs_password (state
.ctx
, state
.doc
)) {
497 if (password
&& !*password
) {
502 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
504 printd ("pass fail");
509 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
513 static void pdfinfo (void)
515 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
519 printd ("info PDF version\t%d.%d",
520 pdf
->version
/ 10, pdf
->version
% 10);
522 infoobj
= pdf_dict_gets (state
.ctx
, pdf_trailer (state
.ctx
,
527 char *items
[] = { "Title", "Author", "Creator",
528 "Producer", "CreationDate" };
530 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
531 pdf_obj
*obj
= pdf_dict_gets (state
.ctx
, infoobj
, items
[i
]);
532 s
= pdf_to_utf8 (state
.ctx
, obj
);
533 if (*s
) printd ("info %s\t%s", items
[i
], s
);
534 fz_free (state
.ctx
, s
);
541 static void unlinktile (struct tile
*tile
)
545 for (i
= 0; i
< tile
->slicecount
; ++i
) {
546 struct slice
*s
= &tile
->slices
[i
];
548 if (s
->texindex
!= -1) {
549 if (state
.texowners
[s
->texindex
].slice
== s
) {
550 state
.texowners
[s
->texindex
].slice
= NULL
;
556 static void freepage (struct page
*page
)
560 fz_drop_stext_page (state
.ctx
, page
->text
);
563 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
568 fz_drop_display_list (state
.ctx
, page
->dlist
);
569 fz_drop_page (state
.ctx
, page
->fzpage
);
573 static void freetile (struct tile
*tile
)
578 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
581 fz_drop_pixmap (state
.ctx
, state
.pig
);
583 state
.pig
= tile
->pixmap
;
588 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
597 static int cacheline32bytes
;
599 static void __attribute__ ((constructor
)) clcheck (void)
601 char **envp
= environ
;
606 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
608 cacheline32bytes
= auxv
[1] == 32;
614 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
616 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
617 if (cacheline32bytes
&& size
> 32) {
618 intptr_t a1
, a2
, diff
;
620 vector
unsigned char v
= vec_splat_u8 (-1);
621 vector
unsigned char *p
;
623 a1
= a2
= (intptr_t) pixmap
->samples
;
624 a2
= (a1
+ 31) & ~31;
629 while (a1
!= a2
) *(char *) a1
++ = 0xff;
630 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
631 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
633 vec_st (v
, i
+ 16, p
);
635 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
637 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
640 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
643 static void trimctm (pdf_page
*page
, int pindex
)
646 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
649 if (!pdim
->tctmready
) {
650 fz_rect realbox
, mediabox
;
651 fz_matrix rm
, sm
, tm
, im
, ctm1
, page_ctm
;
653 fz_rotate (&rm
, -pdim
->rotate
);
654 fz_scale (&sm
, 1, -1);
655 fz_concat (&ctm
, &rm
, &sm
);
656 realbox
= pdim
->mediabox
;
657 fz_transform_rect (&realbox
, &ctm
);
658 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
659 fz_concat (&ctm1
, &ctm
, &tm
);
660 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
661 fz_invert_matrix (&im
, &page_ctm
);
662 fz_concat (&ctm
, &im
, &ctm1
);
668 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
671 int pdimno
= pdim
- state
.pagedims
;
673 if (pdf_specifics (state
.ctx
, state
.doc
)) {
674 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), pdimno
);
675 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
678 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
679 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
684 static fz_matrix
pagectm (struct page
*page
)
686 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
689 static void *loadpage (int pageno
, int pindex
)
694 page
= calloc (sizeof (struct page
), 1);
696 err (1, "calloc page %d", pageno
);
699 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
700 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
702 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
703 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
706 fz_catch (state
.ctx
) {
709 fz_close_device (state
.ctx
, dev
);
710 fz_drop_device (state
.ctx
, dev
);
712 page
->pdimno
= pindex
;
713 page
->pageno
= pageno
;
714 page
->sgen
= state
.gen
;
715 page
->agen
= state
.gen
;
716 page
->tgen
= state
.gen
;
720 static struct tile
*alloctile (int h
)
727 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
728 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
729 tile
= calloc (tilesize
, 1);
731 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
733 for (i
= 0; i
< slicecount
; ++i
) {
734 int sh
= fz_mini (h
, state
.sliceheight
);
735 tile
->slices
[i
].h
= sh
;
736 tile
->slices
[i
].texindex
= -1;
739 tile
->slicecount
= slicecount
;
740 tile
->sliceheight
= state
.sliceheight
;
744 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
752 struct pagedim
*pdim
;
754 tile
= alloctile (h
);
755 pdim
= &state
.pagedims
[page
->pdimno
];
760 bbox
.x1
= bbox
.x0
+ w
;
761 bbox
.y1
= bbox
.y0
+ h
;
764 if (state
.pig
->w
== w
766 && state
.pig
->colorspace
== state
.colorspace
) {
767 tile
->pixmap
= state
.pig
;
768 tile
->pixmap
->x
= bbox
.x0
;
769 tile
->pixmap
->y
= bbox
.y0
;
772 fz_drop_pixmap (state
.ctx
, state
.pig
);
779 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
785 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
, 1);
791 clearpixmap (tile
->pixmap
);
793 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
794 ctm
= pagectm (page
);
795 fz_rect_from_irect (&rect
, &bbox
);
796 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
797 fz_close_device (state
.ctx
, dev
);
798 fz_drop_device (state
.ctx
, dev
);
803 #ifdef CACHE_PAGEREFS
804 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
805 thanks to Robin Watts */
807 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
809 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
813 if (state
.pdflut
.idx
== state
.pagecount
) return;
815 kids
= pdf_dict_gets (ctx
, node
, "Kids");
816 len
= pdf_array_len (ctx
, kids
);
819 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
821 if (pdf_mark_obj (ctx
, node
))
822 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
823 for (i
= 0; i
< len
; i
++) {
824 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
825 char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
827 ? !strcmp (type
, "Pages")
828 : pdf_dict_gets (ctx
, kid
, "Kids")
829 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
830 pdf_collect_pages (doc
, kid
);
834 ? strcmp (type
, "Page") != 0
835 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
836 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
837 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
840 pdf_unmark_obj (ctx
, node
);
844 pdf_load_page_objs (pdf_document
*doc
)
846 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
847 pdf_trailer (state
.ctx
, doc
), "Root");
848 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
851 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
853 state
.pdflut
.idx
= 0;
854 pdf_collect_pages (doc
, node
);
858 static void initpdims (int wthack
)
862 fz_rect rootmediabox
;
863 int pageno
, trim
, show
;
864 int trimw
= 0, cxcount
;
865 fz_context
*ctx
= state
.ctx
;
866 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
873 if (state
.trimmargins
&& state
.trimcachepath
) {
874 trimf
= fopen (state
.trimcachepath
, "rb");
876 trimf
= fopen (state
.trimcachepath
, "wb");
881 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
882 cxcount
= state
.pagecount
;
884 cxcount
= fz_mini (state
.pagecount
, 1);
888 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
889 "Root/Pages/MediaBox");
890 pdf_to_rect (ctx
, obj
, &rootmediabox
);
893 #ifdef CACHE_PAGEREFS
894 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
895 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
896 if (!state
.pdflut
.objs
) {
897 err (1, "malloc pageobjs %zu %d %zu failed",
898 sizeof (*state
.pdflut
.objs
), cxcount
,
899 sizeof (*state
.pdflut
.objs
) * cxcount
);
901 state
.pdflut
.count
= cxcount
;
902 pdf_load_page_objs (pdf
);
903 state
.pdflut
.pdf
= pdf
;
907 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
914 pdf_obj
*pageref
, *pageobj
;
916 #ifdef CACHE_PAGEREFS
917 pageref
= state
.pdflut
.objs
[pageno
];
919 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
921 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
922 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
924 if (state
.trimmargins
) {
929 page
= pdf_load_page (ctx
, pdf
, pageno
);
930 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
931 trim
= state
.trimanew
|| !obj
;
935 fz_matrix ctm
, page_ctm
;
937 dev
= fz_new_bbox_device (ctx
, &rect
);
938 dev
->hints
|= FZ_IGNORE_SHADE
;
939 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
940 fz_invert_matrix (&ctm
, &page_ctm
);
941 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
942 fz_close_device (ctx
, dev
);
943 fz_drop_device (ctx
, dev
);
945 rect
.x0
+= state
.trimfuzz
.x0
;
946 rect
.x1
+= state
.trimfuzz
.x1
;
947 rect
.y0
+= state
.trimfuzz
.y0
;
948 rect
.y1
+= state
.trimfuzz
.y1
;
949 fz_transform_rect (&rect
, &ctm
);
950 fz_intersect_rect (&rect
, &mediabox
);
952 if (!fz_is_empty_rect (&rect
)) {
956 obj
= pdf_new_array (ctx
, pdf
, 4);
957 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
959 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
961 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
963 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
965 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
968 mediabox
.x0
= pdf_to_real (ctx
,
969 pdf_array_get (ctx
, obj
, 0));
970 mediabox
.y0
= pdf_to_real (ctx
,
971 pdf_array_get (ctx
, obj
, 1));
972 mediabox
.x1
= pdf_to_real (ctx
,
973 pdf_array_get (ctx
, obj
, 2));
974 mediabox
.y1
= pdf_to_real (ctx
,
975 pdf_array_get (ctx
, obj
, 3));
978 fz_drop_page (ctx
, &page
->super
);
979 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
981 printd ("progress %f Trimming %d",
982 (double) (pageno
+ 1) / state
.pagecount
,
987 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
995 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
997 if (fz_is_empty_rect (&mediabox
)) {
1006 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1008 if (!fz_is_empty_rect (&cropbox
)) {
1013 fz_intersect_rect (&mediabox
, &cropbox
);
1018 if (fz_is_empty_rect (&rootmediabox
)) {
1020 "cannot find page size for page %d\n",
1024 mediabox
= rootmediabox
;
1031 if (state
.trimmargins
&& trimw
) {
1035 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1036 fz_bound_page (ctx
, page
, &mediabox
);
1037 if (state
.trimmargins
) {
1041 dev
= fz_new_bbox_device (ctx
, &rect
);
1042 dev
->hints
|= FZ_IGNORE_SHADE
;
1043 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1044 fz_close_device (ctx
, dev
);
1045 fz_drop_device (ctx
, dev
);
1047 rect
.x0
+= state
.trimfuzz
.x0
;
1048 rect
.x1
+= state
.trimfuzz
.x1
;
1049 rect
.y0
+= state
.trimfuzz
.y0
;
1050 rect
.y1
+= state
.trimfuzz
.y1
;
1051 fz_intersect_rect (&rect
, &mediabox
);
1053 if (!fz_is_empty_rect (&rect
)) {
1057 fz_drop_page (ctx
, page
);
1059 printd ("progress %f loading %d",
1060 (double) (pageno
+ 1) / state
.pagecount
,
1067 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1069 err (1, "fwrite trim mediabox");
1075 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1077 err (1, "fread trim mediabox %d", pageno
);
1083 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1084 fz_bound_page (ctx
, page
, &mediabox
);
1085 fz_drop_page (ctx
, page
);
1087 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1089 printd ("progress %f Gathering dimensions %d",
1090 (double) (pageno
) / state
.pagecount
,
1095 fprintf (stderr
, "failed to load page %d\n", pageno
);
1101 if (state
.pagedimcount
== 0
1102 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1103 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1106 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1107 state
.pagedims
= realloc (state
.pagedims
, size
);
1108 if (!state
.pagedims
) {
1109 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1110 size
, state
.pagedimcount
+ 1);
1113 p
= &state
.pagedims
[state
.pagedimcount
++];
1115 p
->mediabox
= mediabox
;
1121 printd ("progress 1 %s %d pages in %f seconds",
1122 state
.trimmargins
? "Trimmed" : "Processed",
1123 state
.pagecount
, end
- start
);
1127 if (fclose (trimf
)) {
1133 static void layout (void)
1138 struct pagedim
*p
= p
;
1139 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1141 if (state
.pagedimcount
== 0) return;
1143 switch (state
.fitmodel
) {
1144 case FitProportional
:
1145 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1148 p
= &state
.pagedims
[pindex
];
1149 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1151 fz_transform_rect (&box
, &rm
);
1153 x0
= fz_min (box
.x0
, box
.x1
);
1154 x1
= fz_max (box
.x0
, box
.x1
);
1157 maxw
= fz_max (w
, maxw
);
1158 zoom
= state
.w
/ maxw
;
1170 ARSERT (0 && state
.fitmodel
);
1173 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1177 p
= &state
.pagedims
[pindex
];
1178 fz_rotate (&ctm
, state
.rotate
);
1179 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1181 fz_transform_rect (&box
, &rm
);
1182 w
= box
.x1
- box
.x0
;
1183 switch (state
.fitmodel
) {
1184 case FitProportional
:
1185 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1191 h
= box
.y1
- box
.y0
;
1193 zoom
= fz_min (zw
, zh
);
1194 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1203 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1204 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1206 fz_rotate (&rm
, p
->rotate
);
1207 p
->pagebox
= p
->mediabox
;
1208 fz_transform_rect (&p
->pagebox
, &rm
);
1209 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1210 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1214 fz_transform_rect (&rect
, &ctm
);
1215 fz_round_rect (&p
->bounds
, &rect
);
1218 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1219 fz_scale (&sm
, zoom
, -zoom
);
1220 fz_concat (&ctm
, &tm
, &sm
);
1226 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1227 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1228 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1229 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1230 int boundw
= x1
- x0
;
1231 int boundh
= y1
- y0
;
1233 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1234 } while (p
-- != state
.pagedims
);
1238 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1239 uritoanchor (const char *uri
)
1245 a
.n
= fz_resolve_link (state
.ctx
, state
.doc
, uri
, &p
.x
, &p
.y
);
1248 struct pagedim
*pdim
= state
.pagedims
;
1250 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1251 if (state
.pagedims
[i
].pageno
> a
.n
)
1253 pdim
= &state
.pagedims
[i
];
1256 fz_transform_point (&p
, &pdim
->ctm
);
1259 a
.h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
1264 static void recurse_outline (fz_outline
*outline
, int level
)
1267 struct anchor a
= uritoanchor (outline
->uri
);
1269 printd ("o %d %d %d %d %s", level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1272 printd ("on %d %s", level
, outline
->title
);
1274 if (outline
->down
) {
1275 recurse_outline (outline
->down
, level
+ 1);
1277 outline
= outline
->next
;
1281 static void process_outline (void)
1283 fz_outline
*outline
;
1285 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1287 state
.needoutline
= 0;
1288 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1290 recurse_outline (outline
, 0);
1291 fz_drop_outline (state
.ctx
, outline
);
1295 static char *strofspan (fz_stext_span
*span
)
1300 size_t size
= 0, cap
= 80;
1302 p
= malloc (cap
+ 1);
1303 if (!p
) return NULL
;
1305 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1306 int n
= fz_runetochar (utf8
, ch
->c
);
1307 if (size
+ n
> cap
) {
1309 p
= realloc (p
, cap
+ 1);
1310 if (!p
) return NULL
;
1313 memcpy (p
+ size
, utf8
, n
);
1320 static int matchspan (regex_t
*re
, fz_stext_span
*span
,
1321 int stop
, int pageno
, double start
)
1328 fz_point p1
, p2
, p3
, p4
;
1330 p
= strofspan (span
);
1333 ret
= regexec (re
, p
, 1, &rm
, 0);
1336 if (ret
!= REG_NOMATCH
) {
1339 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1340 printd ("msg regexec error `%.*s'",
1341 (int) size
, errbuf
);
1349 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1350 c
+= fz_runelen (span
->text
[a
].c
);
1352 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1353 c
+= fz_runelen (span
->text
[b
].c
);
1356 if (fz_runelen (span
->text
[b
].c
) > 1) {
1357 b
= fz_maxi (0, b
-1);
1360 fz_stext_char_bbox (state
.ctx
, &sb
, span
, a
);
1361 fz_stext_char_bbox (state
.ctx
, &eb
, span
, b
);
1373 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1380 printd ("progress 1 found at %d `%.*s' in %f sec",
1381 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1385 printd ("match %d %d %f %f %f %f %f %f %f %f",
1397 static int compareblocks (const void *l
, const void *r
)
1399 fz_stext_block
const *ls
= l
;
1400 fz_stext_block
const *rs
= r
;
1401 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1404 /* wishful thinking function */
1405 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1409 fz_stext_page
*text
;
1410 fz_stext_sheet
*sheet
;
1411 struct pagedim
*pdim
, *pdimprev
;
1412 int stop
= 0, niters
= 0;
1417 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1418 if (niters
++ == 5) {
1421 printd ("progress 1 attention requested aborting search at %d",
1426 printd ("progress %f searching in page %d",
1427 (double) (pageno
+ 1) / state
.pagecount
,
1432 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1433 pdim
= &state
.pagedims
[i
];
1434 if (pdim
->pageno
== pageno
) {
1437 if (pdim
->pageno
> pageno
) {
1446 sheet
= fz_new_stext_sheet (state
.ctx
);
1447 text
= fz_new_stext_page (state
.ctx
, &pdim
->mediabox
);
1448 tdev
= fz_new_stext_device (state
.ctx
, sheet
, text
, 0);
1450 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1452 fz_matrix ctm
= pagectm1 (page
, pdim
);
1453 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1456 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1457 fz_close_device (state
.ctx
, tdev
);
1458 fz_drop_device (state
.ctx
, tdev
);
1460 for (j
= 0; j
< text
->len
; ++j
) {
1463 fz_stext_block
*block
;
1465 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1466 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1469 for (k
= 0; k
< block
->len
; ++k
) {
1470 fz_stext_line
*line
;
1471 fz_stext_span
*span
;
1474 line
= &block
->lines
[k
];
1475 if (line
->bbox
.y0
< y
+ 1) continue;
1478 line
= &block
->lines
[block
->len
- 1 - k
];
1479 if (line
->bbox
.y0
> y
- 1) continue;
1482 for (span
= line
->first_span
; span
; span
= span
->next
) {
1483 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1485 case 1: stop
= 1; break;
1486 case -1: stop
= 1; goto endloop
;
1500 fz_drop_stext_page (state
.ctx
, text
);
1501 fz_drop_stext_sheet (state
.ctx
, sheet
);
1502 fz_drop_page (state
.ctx
, page
);
1506 printd ("progress 1 no matches %f sec", end
- start
);
1508 printd ("clearrects");
1511 static void set_tex_params (int colorspace
)
1518 switch (colorspace
) {
1520 state
.texiform
= GL_RGBA8
;
1521 state
.texform
= GL_RGBA
;
1522 state
.texty
= GL_UNSIGNED_BYTE
;
1523 state
.colorspace
= fz_device_rgb (state
.ctx
);
1526 state
.texiform
= GL_RGBA8
;
1527 state
.texform
= GL_BGRA
;
1528 state
.texty
= endianness
.s
> 1
1529 ? GL_UNSIGNED_INT_8_8_8_8
1530 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1531 state
.colorspace
= fz_device_bgr (state
.ctx
);
1534 state
.texiform
= GL_LUMINANCE_ALPHA
;
1535 state
.texform
= GL_LUMINANCE_ALPHA
;
1536 state
.texty
= GL_UNSIGNED_BYTE
;
1537 state
.colorspace
= fz_device_gray (state
.ctx
);
1540 errx (1, "invalid colorspce %d", colorspace
);
1544 static void realloctexts (int texcount
)
1548 if (texcount
== state
.texcount
) return;
1550 if (texcount
< state
.texcount
) {
1551 glDeleteTextures (state
.texcount
- texcount
,
1552 state
.texids
+ texcount
);
1555 size
= texcount
* sizeof (*state
.texids
);
1556 state
.texids
= realloc (state
.texids
, size
);
1557 if (!state
.texids
) {
1558 err (1, "realloc texids %" FMT_s
, size
);
1561 size
= texcount
* sizeof (*state
.texowners
);
1562 state
.texowners
= realloc (state
.texowners
, size
);
1563 if (!state
.texowners
) {
1564 err (1, "realloc texowners %" FMT_s
, size
);
1566 if (texcount
> state
.texcount
) {
1569 glGenTextures (texcount
- state
.texcount
,
1570 state
.texids
+ state
.texcount
);
1571 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1572 state
.texowners
[i
].w
= -1;
1573 state
.texowners
[i
].slice
= NULL
;
1576 state
.texcount
= texcount
;
1580 static char *mbtoutf8 (char *s
)
1586 len
= mbstowcs (NULL
, s
, strlen (s
));
1591 if (len
== (size_t) -1) {
1596 tmp
= malloc (len
* sizeof (wchar_t));
1601 ret
= mbstowcs (tmp
, s
, len
);
1602 if (ret
== (size_t) -1) {
1608 for (i
= 0; i
< ret
; ++i
) {
1609 len
+= fz_runelen (tmp
[i
]);
1612 p
= r
= malloc (len
+ 1);
1618 for (i
= 0; i
< ret
; ++i
) {
1619 p
+= fz_runetochar (p
, tmp
[i
]);
1626 CAMLprim value
ml_transform_page_point (value pageno_v
, value x_v
, value y_v
)
1628 CAMLparam3 (pageno_v
, x_v
, y_v
);
1631 struct pagedim
*pdim
= state
.pagedims
;
1632 fz_point p
= { .x
= Int_val (x_v
), .y
= Int_val (y_v
) };
1634 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1635 if (state
.pagedims
[i
].pageno
> Int_val (pageno_v
))
1637 pdim
= &state
.pagedims
[i
];
1639 fz_transform_point (&p
, &pdim
->ctm
);
1641 ret_v
= caml_alloc_small (2 * Double_wosize
, Double_array_tag
);
1642 Store_double_field (ret_v
, 0, p
.x
);
1643 Store_double_field (ret_v
, 1, p
.y
);
1648 CAMLprim value
ml_mbtoutf8 (value s_v
)
1654 s
= String_val (s_v
);
1660 ret_v
= caml_copy_string (r
);
1666 static void * mainloop (void UNUSED_ATTR
*unused
)
1669 int len
, ret
, oldlen
= 0;
1674 len
= readlen (state
.csock
);
1676 errx (1, "readlen returned 0");
1679 if (oldlen
< len
+ 1) {
1680 p
= realloc (p
, len
+ 1);
1682 err (1, "realloc %d failed", len
+ 1);
1686 readdata (state
.csock
, p
, len
);
1689 if (!strncmp ("open", p
, 4)) {
1690 int wthack
, off
, ok
= 0;
1697 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1699 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1702 filename
= p
+ 5 + off
;
1703 filenamelen
= strlen (filename
);
1704 password
= filename
+ filenamelen
+ 1;
1707 fz_try (state
.ctx
) {
1708 ok
= openxref (filename
, password
);
1710 fz_catch (state
.ctx
) {
1711 utf8filename
= mbtoutf8 (filename
);
1712 printd ("msg Could not open %s", utf8filename
);
1722 utf8filename
= mbtoutf8 (filename
);
1723 printd ("msg Opened %s (press h/F1 to get help)",
1725 if (utf8filename
!= filename
) {
1726 free (utf8filename
);
1729 state
.needoutline
= 1;
1732 else if (!strncmp ("cs", p
, 2)) {
1735 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1737 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1740 set_tex_params (colorspace
);
1741 for (i
= 0; i
< state
.texcount
; ++i
) {
1742 state
.texowners
[i
].w
= -1;
1743 state
.texowners
[i
].slice
= NULL
;
1747 else if (!strncmp ("freepage", p
, 8)) {
1750 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1752 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1756 else if (!strncmp ("freetile", p
, 8)) {
1759 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1761 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1765 else if (!strncmp ("search", p
, 6)) {
1766 int icase
, pageno
, y
, len2
, forward
;
1770 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1771 &icase
, &pageno
, &y
, &forward
, &len2
);
1773 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1776 pattern
= p
+ 6 + len2
;
1777 ret
= regcomp (&re
, pattern
,
1778 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1783 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1784 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1787 search (&re
, pageno
, y
, forward
);
1791 else if (!strncmp ("geometry", p
, 8)) {
1795 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1797 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1805 for (i
= 0; i
< state
.texcount
; ++i
) {
1806 state
.texowners
[i
].slice
= NULL
;
1809 state
.fitmodel
= fitmodel
;
1814 unlock ("geometry");
1815 printd ("continue %d", state
.pagecount
);
1817 else if (!strncmp ("reqlayout", p
, 9)) {
1820 unsigned int fitmodel
;
1824 ret
= sscanf (p
+ 9, " %d %u %d %n",
1825 &rotate
, &fitmodel
, &h
, &off
);
1827 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1830 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1831 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1834 state
.rotate
= rotate
;
1835 state
.fitmodel
= fitmodel
;
1840 nameddest
= p
+ 9 + off
;
1841 if (pdf
&& nameddest
&& *nameddest
) {
1845 pdf_obj
*needle
, *obj
;
1847 needle
= pdf_new_string (state
.ctx
, pdf
, nameddest
,
1848 strlen (nameddest
));
1849 obj
= pdf_lookup_dest (state
.ctx
, pdf
, needle
);
1851 dest
= pdf_parse_link_dest (state
.ctx
, pdf
,
1854 a
= desttoanchor (&dest
);
1856 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1859 printd ("emsg failed to parse destination `%s'\n",
1864 printd ("emsg destination `%s' not found\n",
1867 pdf_drop_obj (state
.ctx
, needle
);
1869 printd ("emsg nameddest `%s'\n", nameddest
);
1874 unlock ("reqlayout");
1875 printd ("continue %d", state
.pagecount
);
1877 else if (!strncmp ("page", p
, 4)) {
1882 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1884 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1889 page
= loadpage (pageno
, pindex
);
1893 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1895 else if (!strncmp ("tile", p
, 4)) {
1902 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1903 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1904 SCN_ptr_cast (&data
));
1906 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1911 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1915 printd ("tile %d %d %" FMT_ptr
" %u %f",
1917 FMT_ptr_cast (tile
),
1918 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1921 else if (!strncmp ("trimset", p
, 7)) {
1925 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1926 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1928 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1931 state
.trimmargins
= trimmargins
;
1932 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1934 state
.trimfuzz
= fuzz
;
1938 else if (!strncmp ("settrim", p
, 7)) {
1942 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1943 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1945 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1949 state
.trimmargins
= trimmargins
;
1950 state
.needoutline
= 1;
1951 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1953 state
.trimfuzz
= fuzz
;
1955 state
.pagedimcount
= 0;
1956 free (state
.pagedims
);
1957 state
.pagedims
= NULL
;
1962 printd ("continue %d", state
.pagecount
);
1964 else if (!strncmp ("sliceh", p
, 6)) {
1967 ret
= sscanf (p
+ 6, " %d", &h
);
1969 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1971 if (h
!= state
.sliceheight
) {
1974 state
.sliceheight
= h
;
1975 for (i
= 0; i
< state
.texcount
; ++i
) {
1976 state
.texowners
[i
].w
= -1;
1977 state
.texowners
[i
].h
= -1;
1978 state
.texowners
[i
].slice
= NULL
;
1982 else if (!strncmp ("interrupt", p
, 9)) {
1983 printd ("vmsg interrupted");
1986 errx (1, "unknown command %.*s", len
, p
);
1992 CAMLprim value
ml_realloctexts (value texcount_v
)
1994 CAMLparam1 (texcount_v
);
1997 if (trylock (__func__
)) {
2001 realloctexts (Int_val (texcount_v
));
2006 CAMLreturn (Val_bool (ok
));
2009 static void recti (int x0
, int y0
, int x1
, int y1
)
2011 GLfloat
*v
= state
.vertices
;
2013 glVertexPointer (2, GL_FLOAT
, 0, v
);
2014 v
[0] = x0
; v
[1] = y0
;
2015 v
[2] = x1
; v
[3] = y0
;
2016 v
[4] = x0
; v
[5] = y1
;
2017 v
[6] = x1
; v
[7] = y1
;
2018 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2021 static void showsel (struct page
*page
, int ox
, int oy
)
2026 fz_stext_line
*line
;
2027 fz_page_block
*pageb
;
2028 fz_stext_block
*block
;
2029 struct mark first
, last
;
2030 unsigned char selcolor
[] = {15,15,15,140};
2032 first
= page
->fmark
;
2035 if (!first
.span
|| !last
.span
) return;
2037 glEnable (GL_BLEND
);
2038 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2039 glColor4ubv (selcolor
);
2041 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2042 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2043 for (pageb
= page
->text
->blocks
;
2044 pageb
< page
->text
->blocks
+ page
->text
->len
;
2046 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2047 block
= pageb
->u
.text
;
2049 for (line
= block
->lines
;
2050 line
< block
->lines
+ block
->len
;
2052 fz_stext_span
*span
;
2053 rect
= fz_empty_rect
;
2055 for (span
= line
->first_span
; span
; span
= span
->next
) {
2057 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2062 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2064 j
= fz_mini (first
.i
, last
.i
);
2065 k
= fz_maxi (first
.i
, last
.i
);
2068 if (span
== first
.span
) {
2072 else if (span
== last
.span
) {
2079 for (i
= j
; i
<= k
; ++i
) {
2081 fz_union_rect (&rect
,
2082 fz_stext_char_bbox (state
.ctx
, &bbox1
,
2085 fz_round_rect (&bbox
, &rect
);
2086 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2093 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2094 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2095 if (span
== last
.span
) {
2098 rect
= fz_empty_rect
;
2104 glDisable (GL_BLEND
);
2109 static void stipplerect (fz_matrix
*m
,
2117 fz_transform_point (p1
, m
);
2118 fz_transform_point (p2
, m
);
2119 fz_transform_point (p3
, m
);
2120 fz_transform_point (p4
, m
);
2126 t
= hypotf (w
, h
) * .25f
;
2130 s
= hypotf (w
, h
) * .25f
;
2132 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2133 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2135 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2136 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2138 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2139 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2141 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2142 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2144 glDrawArrays (GL_LINES
, 0, 8);
2147 static void solidrect (fz_matrix
*m
,
2154 fz_transform_point (p1
, m
);
2155 fz_transform_point (p2
, m
);
2156 fz_transform_point (p3
, m
);
2157 fz_transform_point (p4
, m
);
2158 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2159 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2161 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2162 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2163 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2166 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2169 fz_matrix ctm
, tm
, pm
;
2170 fz_link
*link
, *links
;
2171 GLfloat
*texcoords
= state
.texcoords
;
2172 GLfloat
*vertices
= state
.vertices
;
2174 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2176 glEnable (GL_TEXTURE_1D
);
2177 glEnable (GL_BLEND
);
2178 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2179 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2181 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2182 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2183 fz_translate (&tm
, xoff
, yoff
);
2184 pm
= pagectm (page
);
2185 fz_concat (&ctm
, &pm
, &tm
);
2187 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2188 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2190 for (link
= links
; link
; link
= link
->next
) {
2191 fz_point p1
, p2
, p3
, p4
;
2193 p1
.x
= link
->rect
.x0
;
2194 p1
.y
= link
->rect
.y0
;
2196 p2
.x
= link
->rect
.x1
;
2197 p2
.y
= link
->rect
.y0
;
2199 p3
.x
= link
->rect
.x1
;
2200 p3
.y
= link
->rect
.y1
;
2202 p4
.x
= link
->rect
.x0
;
2203 p4
.y
= link
->rect
.y1
;
2206 switch (link
->dest
.kind
) {
2207 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2208 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2209 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2210 default: glColor3ub (0, 0, 0); break;
2213 glColor3ub (255, 0, 0);
2215 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2218 for (i
= 0; i
< page
->annotcount
; ++i
) {
2219 fz_point p1
, p2
, p3
, p4
;
2220 struct annot
*annot
= &page
->annots
[i
];
2222 p1
.x
= annot
->bbox
.x0
;
2223 p1
.y
= annot
->bbox
.y0
;
2225 p2
.x
= annot
->bbox
.x1
;
2226 p2
.y
= annot
->bbox
.y0
;
2228 p3
.x
= annot
->bbox
.x1
;
2229 p3
.y
= annot
->bbox
.y1
;
2231 p4
.x
= annot
->bbox
.x0
;
2232 p4
.y
= annot
->bbox
.y1
;
2234 glColor3ub (0, 0, 128);
2235 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2238 glDisable (GL_BLEND
);
2239 glDisable (GL_TEXTURE_1D
);
2242 static int compareslinks (const void *l
, const void *r
)
2244 struct slink
const *ls
= l
;
2245 struct slink
const *rs
= r
;
2246 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2247 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2249 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2252 static void droptext (struct page
*page
)
2255 fz_drop_stext_page (state
.ctx
, page
->text
);
2258 page
->fmark
.span
= NULL
;
2259 page
->lmark
.span
= NULL
;
2263 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
2268 static void dropannots (struct page
*page
)
2271 free (page
->annots
);
2272 page
->annots
= NULL
;
2273 page
->annotcount
= 0;
2277 static void ensureannots (struct page
*page
)
2280 size_t annotsize
= sizeof (*page
->annots
);
2283 if (state
.gen
!= page
->agen
) {
2285 page
->agen
= state
.gen
;
2287 if (page
->annots
) return;
2289 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2291 annot
= fz_next_annot (state
.ctx
, annot
)) {
2296 page
->annotcount
= count
;
2297 page
->annots
= calloc (count
, annotsize
);
2298 if (!page
->annots
) {
2299 err (1, "calloc annots %d", count
);
2302 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2304 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2307 fz_bound_annot (state
.ctx
, annot
, &rect
);
2308 page
->annots
[i
].annot
= annot
;
2309 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2314 static void dropslinks (struct page
*page
)
2317 free (page
->slinks
);
2318 page
->slinks
= NULL
;
2319 page
->slinkcount
= 0;
2323 static void ensureslinks (struct page
*page
)
2327 size_t slinksize
= sizeof (*page
->slinks
);
2328 fz_link
*link
, *links
;
2330 ensureannots (page
);
2331 if (state
.gen
!= page
->sgen
) {
2333 page
->sgen
= state
.gen
;
2335 if (page
->slinks
) return;
2337 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2338 ctm
= pagectm (page
);
2340 count
= page
->annotcount
;
2341 for (link
= links
; link
; link
= link
->next
) {
2347 page
->slinkcount
= count
;
2348 page
->slinks
= calloc (count
, slinksize
);
2349 if (!page
->slinks
) {
2350 err (1, "calloc slinks %d", count
);
2353 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2357 fz_transform_rect (&rect
, &ctm
);
2358 page
->slinks
[i
].tag
= SLINK
;
2359 page
->slinks
[i
].u
.link
= link
;
2360 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2362 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2364 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2365 fz_transform_rect (&rect
, &ctm
);
2366 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2368 page
->slinks
[i
].tag
= SANNOT
;
2369 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2371 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2375 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2376 static void fmt_linkn (char *s
, unsigned int u
)
2378 unsigned int len
; unsigned int q
;
2379 unsigned int zma
= 'z' - 'a' + 1;
2381 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2384 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2385 /* handles u == 0 */
2390 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2391 int noff
, char *targ
, int tlen
, int hfsize
)
2395 struct slink
*slink
;
2396 double x0
, y0
, x1
, y1
, w
;
2398 ensureslinks (page
);
2399 glColor3ub (0xc3, 0xb0, 0x91);
2400 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2401 fmt_linkn (buf
, i
+ noff
);
2402 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2403 slink
= &page
->slinks
[i
];
2405 x0
= slink
->bbox
.x0
+ xoff
- 5;
2406 y1
= slink
->bbox
.y0
+ yoff
- 5;
2407 y0
= y1
+ 10 + hfsize
;
2408 w
= measure_string (state
.face
, hfsize
, buf
);
2410 recti (x0
, y0
, x1
, y1
);
2414 glEnable (GL_BLEND
);
2415 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2416 glEnable (GL_TEXTURE_2D
);
2417 glColor3ub (0, 0, 0);
2418 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2419 fmt_linkn (buf
, i
+ noff
);
2420 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2421 slink
= &page
->slinks
[i
];
2423 x0
= slink
->bbox
.x0
+ xoff
;
2424 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2425 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2428 glDisable (GL_TEXTURE_2D
);
2429 glDisable (GL_BLEND
);
2432 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2435 struct slice
*slice1
;
2436 unsigned char *texdata
;
2439 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2440 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2442 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2443 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2444 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2448 int texindex
= state
.texindex
++ % state
.texcount
;
2450 if (state
.texowners
[texindex
].w
== tile
->w
) {
2451 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2455 state
.texowners
[texindex
].h
= slice
->h
;
2459 state
.texowners
[texindex
].h
= slice
->h
;
2462 state
.texowners
[texindex
].w
= tile
->w
;
2463 state
.texowners
[texindex
].slice
= slice
;
2464 slice
->texindex
= texindex
;
2466 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2467 #if TEXT_TYPE == GL_TEXTURE_2D
2468 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2469 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2470 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2471 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2474 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2478 texdata
= tile
->pixmap
->samples
;
2481 glTexSubImage2D (TEXT_TYPE
,
2493 glTexImage2D (TEXT_TYPE
,
2505 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2510 CAMLprim value
ml_begintiles (value unit_v
)
2512 CAMLparam1 (unit_v
);
2513 glEnable (TEXT_TYPE
);
2514 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2515 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2516 CAMLreturn (unit_v
);
2519 CAMLprim value
ml_endtiles (value unit_v
)
2521 CAMLparam1 (unit_v
);
2522 glDisable (TEXT_TYPE
);
2523 CAMLreturn (unit_v
);
2526 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2528 CAMLparam2 (args_v
, ptr_v
);
2529 int dispx
= Int_val (Field (args_v
, 0));
2530 int dispy
= Int_val (Field (args_v
, 1));
2531 int dispw
= Int_val (Field (args_v
, 2));
2532 int disph
= Int_val (Field (args_v
, 3));
2533 int tilex
= Int_val (Field (args_v
, 4));
2534 int tiley
= Int_val (Field (args_v
, 5));
2535 char *s
= String_val (ptr_v
);
2536 struct tile
*tile
= parse_pointer (__func__
, s
);
2537 int slicey
, firstslice
;
2538 struct slice
*slice
;
2539 GLfloat
*texcoords
= state
.texcoords
;
2540 GLfloat
*vertices
= state
.vertices
;
2542 firstslice
= tiley
/ tile
->sliceheight
;
2543 slice
= &tile
->slices
[firstslice
];
2544 slicey
= tiley
% tile
->sliceheight
;
2549 dh
= slice
->h
- slicey
;
2550 dh
= fz_mini (disph
, dh
);
2551 uploadslice (tile
, slice
);
2553 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2554 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2555 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2556 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2558 vertices
[0] = dispx
; vertices
[1] = dispy
;
2559 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2560 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2561 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2563 #if TEXT_TYPE == GL_TEXTURE_2D
2564 for (int i
= 0; i
< 8; ++i
) {
2565 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2569 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2573 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2576 CAMLreturn (Val_unit
);
2579 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2581 fz_matrix ctm
, tm
, pm
;
2582 fz_point p1
, p2
, p3
, p4
;
2583 GLfloat
*vertices
= state
.vertices
;
2584 double *v
= (double *) rects_v
;
2586 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2587 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2588 fz_translate (&tm
, xoff
, yoff
);
2589 pm
= pagectm (page
);
2590 fz_concat (&ctm
, &pm
, &tm
);
2592 glEnable (GL_BLEND
);
2593 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2607 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2608 glDisable (GL_BLEND
);
2611 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2612 value xoff_v
, value yoff_v
,
2615 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2616 int xoff
= Int_val (xoff_v
);
2617 int yoff
= Int_val (yoff_v
);
2618 int noff
= Int_val (Field (li_v
, 0));
2619 char *targ
= String_val (Field (li_v
, 1));
2620 int tlen
= caml_string_length (Field (li_v
, 1));
2621 int hfsize
= Int_val (Field (li_v
, 2));
2622 char *s
= String_val (ptr_v
);
2623 int hlmask
= Int_val (hlinks_v
);
2624 struct page
*page
= parse_pointer (__func__
, s
);
2626 if (!page
->fzpage
) {
2627 /* deal with loadpage failed pages */
2631 if (trylock (__func__
)) {
2636 ensureannots (page
);
2637 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2639 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2640 noff
= page
->slinkcount
;
2642 if (page
->tgen
== state
.gen
) {
2643 showsel (page
, xoff
, yoff
);
2648 CAMLreturn (Val_int (noff
));
2651 CAMLprim value
ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2654 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2655 int xoff
= Int_val (xoff_v
);
2656 int yoff
= Int_val (yoff_v
);
2657 char *s
= String_val (ptr_v
);
2658 struct page
*page
= parse_pointer (__func__
, s
);
2660 drawprect (page
, xoff
, yoff
, rects_v
);
2661 CAMLreturn (Val_unit
);
2664 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2669 const fz_matrix
*tctm
;
2670 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2672 if (!page
->annots
) return NULL
;
2675 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2676 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2679 tctm
= &fz_identity
;
2685 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2686 fz_invert_matrix (&ctm
, &ctm
);
2687 fz_transform_point (&p
, &ctm
);
2690 for (i
= 0; i
< page
->annotcount
; ++i
) {
2691 struct annot
*a
= &page
->annots
[i
];
2694 fz_bound_annot (state
.ctx
, a
->annot
, &rect
);
2695 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2696 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2704 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2708 fz_link
*link
, *links
;
2710 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2715 ctm
= pagectm (page
);
2716 fz_invert_matrix (&ctm
, &ctm
);
2717 fz_transform_point (&p
, &ctm
);
2719 for (link
= links
; link
; link
= link
->next
) {
2720 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2721 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2729 static void ensuretext (struct page
*page
)
2731 if (state
.gen
!= page
->tgen
) {
2733 page
->tgen
= state
.gen
;
2739 page
->text
= fz_new_stext_page (state
.ctx
,
2740 &state
.pagedims
[page
->pdimno
].mediabox
);
2741 page
->sheet
= fz_new_stext_sheet (state
.ctx
);
2742 tdev
= fz_new_stext_device (state
.ctx
, page
->sheet
, page
->text
, 0);
2743 ctm
= pagectm (page
);
2744 fz_run_display_list (state
.ctx
, page
->dlist
,
2745 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2746 qsort (page
->text
->blocks
, page
->text
->len
,
2747 sizeof (*page
->text
->blocks
), compareblocks
);
2748 fz_close_device (state
.ctx
, tdev
);
2749 fz_drop_device (state
.ctx
, tdev
);
2753 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2755 CAMLparam2 (start_page_v
, dir_v
);
2757 int i
, dir
= Int_val (dir_v
);
2758 int start_page
= Int_val (start_page_v
);
2759 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2763 ret_v
= Val_int (0);
2765 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2766 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2771 pdf_page
*page
= NULL
;
2774 fz_try (state
.ctx
) {
2775 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2776 found
= !!page
->links
|| !!page
->annots
;
2778 fz_catch (state
.ctx
) {
2782 fz_drop_page (state
.ctx
, &page
->super
);
2786 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2787 found
= !!fz_load_links (state
.ctx
, page
);
2788 fz_drop_page (state
.ctx
, page
);
2792 ret_v
= caml_alloc_small (1, 1);
2793 Field (ret_v
, 0) = Val_int (i
);
2802 enum { dir_first
, dir_last
};
2803 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2805 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2807 CAMLparam2 (ptr_v
, dir_v
);
2808 CAMLlocal2 (ret_v
, pos_v
);
2810 int dirtag
, i
, slinkindex
;
2811 struct slink
*found
= NULL
,*slink
;
2812 char *s
= String_val (ptr_v
);
2814 page
= parse_pointer (__func__
, s
);
2815 ret_v
= Val_int (0);
2817 ensureslinks (page
);
2819 if (Is_block (dir_v
)) {
2820 dirtag
= Tag_val (dir_v
);
2822 case dir_first_visible
:
2824 int x0
, y0
, dir
, first_index
, last_index
;
2826 pos_v
= Field (dir_v
, 0);
2827 x0
= Int_val (Field (pos_v
, 0));
2828 y0
= Int_val (Field (pos_v
, 1));
2829 dir
= Int_val (Field (pos_v
, 2));
2834 last_index
= page
->slinkcount
;
2837 first_index
= page
->slinkcount
- 1;
2841 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2842 slink
= &page
->slinks
[i
];
2843 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2852 slinkindex
= Int_val (Field (dir_v
, 0));
2853 found
= &page
->slinks
[slinkindex
];
2854 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2855 slink
= &page
->slinks
[i
];
2856 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2864 slinkindex
= Int_val (Field (dir_v
, 0));
2865 found
= &page
->slinks
[slinkindex
];
2866 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2867 slink
= &page
->slinks
[i
];
2868 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2876 slinkindex
= Int_val (Field (dir_v
, 0));
2877 found
= &page
->slinks
[slinkindex
];
2878 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2879 slink
= &page
->slinks
[i
];
2880 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2888 slinkindex
= Int_val (Field (dir_v
, 0));
2889 found
= &page
->slinks
[slinkindex
];
2890 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2891 slink
= &page
->slinks
[i
];
2892 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2901 dirtag
= Int_val (dir_v
);
2904 found
= page
->slinks
;
2909 found
= page
->slinks
+ (page
->slinkcount
- 1);
2915 ret_v
= caml_alloc_small (2, 1);
2916 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2923 enum { uuri
, utext
, uannot
};
2925 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2927 CAMLparam2 (ptr_v
, n_v
);
2928 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2931 char *s
= String_val (ptr_v
);
2932 struct slink
*slink
;
2934 ret_v
= Val_int (0);
2935 page
= parse_pointer (__func__
, s
);
2938 ensureslinks (page
);
2939 slink
= &page
->slinks
[Int_val (n_v
)];
2940 if (slink
->tag
== SLINK
) {
2941 link
= slink
->u
.link
;
2942 str_v
= caml_copy_string (link
->uri
);
2943 ret_v
= caml_alloc_small (1, uuri
);
2944 Field (ret_v
, 0) = str_v
;
2947 ret_v
= caml_alloc_small (1, uannot
);
2948 tup_v
= caml_alloc_tuple (2);
2949 Field (ret_v
, 0) = tup_v
;
2950 Field (tup_v
, 0) = ptr_v
;
2951 Field (tup_v
, 1) = n_v
;
2958 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
2960 CAMLparam2 (ptr_v
, n_v
);
2962 const char *contents
= "";
2965 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2967 char *s
= String_val (ptr_v
);
2969 struct slink
*slink
;
2971 page
= parse_pointer (__func__
, s
);
2972 slink
= &page
->slinks
[Int_val (n_v
)];
2973 contents
= pdf_annot_contents (state
.ctx
,
2974 (pdf_annot
*) slink
->u
.annot
);
2977 CAMLreturn (caml_copy_string (contents
));
2980 CAMLprim value
ml_getlinkcount (value ptr_v
)
2984 char *s
= String_val (ptr_v
);
2986 page
= parse_pointer (__func__
, s
);
2987 CAMLreturn (Val_int (page
->slinkcount
));
2990 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2992 CAMLparam2 (ptr_v
, n_v
);
2995 struct slink
*slink
;
2996 char *s
= String_val (ptr_v
);
2998 page
= parse_pointer (__func__
, s
);
2999 ret_v
= caml_alloc_tuple (4);
3001 ensureslinks (page
);
3003 slink
= &page
->slinks
[Int_val (n_v
)];
3004 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3005 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3006 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3007 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3012 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3014 CAMLparam3 (ptr_v
, x_v
, y_v
);
3015 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3017 struct annot
*annot
;
3019 char *ptr
= String_val (ptr_v
);
3020 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3021 struct pagedim
*pdim
;
3023 ret_v
= Val_int (0);
3024 if (trylock (__func__
)) {
3028 page
= parse_pointer (__func__
, ptr
);
3029 pdim
= &state
.pagedims
[page
->pdimno
];
3030 x
+= pdim
->bounds
.x0
;
3031 y
+= pdim
->bounds
.y0
;
3034 annot
= getannot (page
, x
, y
);
3038 ensureslinks (page
);
3039 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3040 if (page
->slinks
[i
].tag
== SANNOT
3041 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3046 ret_v
= caml_alloc_small (1, uannot
);
3047 tup_v
= caml_alloc_tuple (2);
3048 Field (ret_v
, 0) = tup_v
;
3049 Field (tup_v
, 0) = ptr_v
;
3050 Field (tup_v
, 1) = Val_int (n
);
3055 link
= getlink (page
, x
, y
);
3057 str_v
= caml_copy_string (link
->uri
);
3058 ret_v
= caml_alloc_small (1, uuri
);
3059 Field (ret_v
, 0) = str_v
;
3063 fz_page_block
*pageb
;
3064 fz_stext_block
*block
;
3067 for (pageb
= page
->text
->blocks
;
3068 pageb
< page
->text
->blocks
+ page
->text
->len
;
3070 fz_stext_line
*line
;
3071 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3072 block
= pageb
->u
.text
;
3075 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3078 for (line
= block
->lines
;
3079 line
< block
->lines
+ block
->len
;
3081 fz_stext_span
*span
;
3084 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3087 for (span
= line
->first_span
; span
; span
= span
->next
) {
3091 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3094 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3096 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3099 if (x
>= b
->x0
&& x
<= b
->x1
3100 && y
>= b
->y0
&& y
<= b
->y1
) {
3101 fz_stext_style
*style
= span
->text
->style
;
3104 ? fz_font_name (state
.ctx
, style
->font
)
3105 : "Span has no font name"
3107 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
3109 if (face
&& face
->family_name
) {
3111 char *n1
= face
->family_name
;
3112 size_t l1
= strlen (n1
);
3113 size_t l2
= strlen (n2
);
3115 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3116 s
= malloc (l1
+ l2
+ 2);
3120 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3121 str_v
= caml_copy_string (s
);
3126 if (str_v
== Val_unit
) {
3127 str_v
= caml_copy_string (n2
);
3129 ret_v
= caml_alloc_small (1, utext
);
3130 Field (ret_v
, 0) = str_v
;
3145 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3147 static int uninteresting (int c
)
3149 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3153 CAMLprim value
ml_clearmark (value ptr_v
)
3156 char *s
= String_val (ptr_v
);
3159 if (trylock (__func__
)) {
3163 page
= parse_pointer (__func__
, s
);
3164 page
->fmark
.span
= NULL
;
3165 page
->lmark
.span
= NULL
;
3171 CAMLreturn (Val_unit
);
3174 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3176 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3180 fz_stext_line
*line
;
3181 fz_page_block
*pageb
;
3182 fz_stext_block
*block
;
3183 struct pagedim
*pdim
;
3184 int mark
= Int_val (mark_v
);
3185 char *s
= String_val (ptr_v
);
3186 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3188 ret_v
= Val_bool (0);
3189 if (trylock (__func__
)) {
3193 page
= parse_pointer (__func__
, s
);
3194 pdim
= &state
.pagedims
[page
->pdimno
];
3198 if (mark
== mark_page
) {
3200 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3202 for (i
= 0; i
< page
->text
->len
; ++i
) {
3203 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3204 pb1
= &page
->text
->blocks
[i
];
3208 if (!pb1
) goto unlock
;
3210 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3211 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3212 pb2
= &page
->text
->blocks
[i
];
3216 if (!pb2
) goto unlock
;
3218 block
= pb1
->u
.text
;
3221 page
->fmark
.span
= block
->lines
->first_span
;
3223 block
= pb2
->u
.text
;
3224 line
= &block
->lines
[block
->len
- 1];
3225 page
->lmark
.i
= line
->last_span
->len
- 1;
3226 page
->lmark
.span
= line
->last_span
;
3227 ret_v
= Val_bool (1);
3231 x
+= pdim
->bounds
.x0
;
3232 y
+= pdim
->bounds
.y0
;
3234 for (pageb
= page
->text
->blocks
;
3235 pageb
< page
->text
->blocks
+ page
->text
->len
;
3237 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3238 block
= pageb
->u
.text
;
3241 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3244 if (mark
== mark_block
) {
3246 page
->fmark
.span
= block
->lines
->first_span
;
3248 line
= &block
->lines
[block
->len
- 1];
3249 page
->lmark
.i
= line
->last_span
->len
- 1;
3250 page
->lmark
.span
= line
->last_span
;
3251 ret_v
= Val_bool (1);
3255 for (line
= block
->lines
;
3256 line
< block
->lines
+ block
->len
;
3258 fz_stext_span
*span
;
3261 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3264 if (mark
== mark_line
) {
3266 page
->fmark
.span
= line
->first_span
;
3268 page
->lmark
.i
= line
->last_span
->len
- 1;
3269 page
->lmark
.span
= line
->last_span
;
3270 ret_v
= Val_bool (1);
3274 for (span
= line
->first_span
; span
; span
= span
->next
) {
3278 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3281 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3283 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3286 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3288 int charnum2
, charnum3
= -1, charnum4
= -1;
3290 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3292 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3293 if (uninteresting (span
->text
[charnum2
].c
)) {
3294 charnum3
= charnum2
+ 1;
3298 if (charnum3
== -1) charnum3
= 0;
3301 for (charnum2
= charnum
+ 1;
3302 charnum2
< span
->len
;
3304 if (uninteresting (span
->text
[charnum2
].c
)) break;
3305 charnum4
= charnum2
;
3308 page
->fmark
.i
= charnum3
;
3309 page
->fmark
.span
= span
;
3311 page
->lmark
.i
= charnum4
;
3312 page
->lmark
.span
= span
;
3313 ret_v
= Val_bool (1);
3321 if (!Bool_val (ret_v
)) {
3322 page
->fmark
.span
= NULL
;
3323 page
->lmark
.span
= NULL
;
3333 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3335 CAMLparam3 (ptr_v
, x_v
, y_v
);
3336 CAMLlocal2 (ret_v
, res_v
);
3339 fz_page_block
*pageb
;
3340 struct pagedim
*pdim
;
3341 char *s
= String_val (ptr_v
);
3342 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3344 ret_v
= Val_int (0);
3345 if (trylock (__func__
)) {
3349 page
= parse_pointer (__func__
, s
);
3350 pdim
= &state
.pagedims
[page
->pdimno
];
3351 x
+= pdim
->bounds
.x0
;
3352 y
+= pdim
->bounds
.y0
;
3356 for (pageb
= page
->text
->blocks
;
3357 pageb
< page
->text
->blocks
+ page
->text
->len
;
3359 switch (pageb
->type
) {
3360 case FZ_PAGE_BLOCK_TEXT
:
3361 b
= &pageb
->u
.text
->bbox
;
3364 case FZ_PAGE_BLOCK_IMAGE
:
3365 b
= &pageb
->u
.image
->bbox
;
3372 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3377 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3378 ret_v
= caml_alloc_small (1, 1);
3379 Store_double_field (res_v
, 0, b
->x0
);
3380 Store_double_field (res_v
, 1, b
->x1
);
3381 Store_double_field (res_v
, 2, b
->y0
);
3382 Store_double_field (res_v
, 3, b
->y1
);
3383 Field (ret_v
, 0) = res_v
;
3391 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3393 CAMLparam2 (ptr_v
, rect_v
);
3396 struct pagedim
*pdim
;
3397 char *s
= String_val (ptr_v
);
3398 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3399 fz_stext_line
*line
;
3400 fz_page_block
*pageb
;
3401 fz_stext_block
*block
;
3402 fz_stext_span
*span
, *fspan
, *lspan
;
3404 if (trylock (__func__
)) {
3408 page
= parse_pointer (__func__
, s
);
3411 pdim
= &state
.pagedims
[page
->pdimno
];
3412 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3413 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3414 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3415 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3426 fspan
= page
->fmark
.span
;
3429 lspan
= page
->lmark
.span
;
3431 for (pageb
= page
->text
->blocks
;
3432 pageb
< page
->text
->blocks
+ page
->text
->len
;
3434 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3435 block
= pageb
->u
.text
;
3436 for (line
= block
->lines
;
3437 line
< block
->lines
+ block
->len
;
3440 for (span
= line
->first_span
; span
; span
= span
->next
) {
3441 for (i
= 0; i
< span
->len
; ++i
) {
3442 fz_stext_char_bbox (state
.ctx
, &b
, span
, i
);
3444 if (x0
>= b
.x0
&& x0
<= b
.x1
3445 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3449 if (x1
>= b
.x0
&& x1
<= b
.x1
3450 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3458 if (x1
< x0
&& fspan
== lspan
) {
3470 page
->fmark
.span
= fspan
;
3473 page
->lmark
.span
= lspan
;
3478 CAMLreturn (Val_unit
);
3481 static int UNUSED_ATTR
pipespan (FILE *f
, fz_stext_span
*span
, int a
, int b
)
3486 for (i
= a
; i
<= b
; ++i
) {
3487 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3488 ret
= fwrite (buf
, len
, 1, f
);
3491 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3492 len
, ret
, strerror (errno
));
3500 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3502 caml_failwith ("ml_popen not implemented under Cygwin");
3505 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3507 CAMLparam2 (command_v
, fds_v
);
3508 CAMLlocal2 (l_v
, tup_v
);
3512 value earg_v
= Nothing
;
3513 posix_spawnattr_t attr
;
3514 posix_spawn_file_actions_t fa
;
3515 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3517 argv
[2] = String_val (command_v
);
3519 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3520 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3523 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3524 msg
= "posix_spawnattr_init";
3528 #ifdef POSIX_SPAWN_USEVFORK
3529 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3530 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3535 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3538 tup_v
= Field (l_v
, 0);
3539 fd1
= Int_val (Field (tup_v
, 0));
3540 fd2
= Int_val (Field (tup_v
, 1));
3542 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3543 msg
= "posix_spawn_file_actions_addclose";
3549 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3550 msg
= "posix_spawn_file_actions_adddup2";
3557 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3558 msg
= "posix_spawn";
3563 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3564 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3568 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3569 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3574 unix_error (ret
, msg
, earg_v
);
3576 CAMLreturn (Val_int (pid
));
3580 CAMLprim value
ml_hassel (value ptr_v
)
3585 char *s
= String_val (ptr_v
);
3587 ret_v
= Val_bool (0);
3588 if (trylock (__func__
)) {
3592 page
= parse_pointer (__func__
, s
);
3593 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3599 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3601 CAMLparam2 (fd_v
, ptr_v
);
3605 fz_stext_line
*line
;
3606 fz_page_block
*pageb
;
3607 fz_stext_block
*block
;
3608 int fd
= Int_val (fd_v
);
3609 char *s
= String_val (ptr_v
);
3611 if (trylock (__func__
)) {
3615 page
= parse_pointer (__func__
, s
);
3617 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3618 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3622 f
= fdopen (fd
, "w");
3624 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3625 fd
, strerror (errno
));
3629 for (pageb
= page
->text
->blocks
;
3630 pageb
< page
->text
->blocks
+ page
->text
->len
;
3632 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3633 block
= pageb
->u
.text
;
3634 for (line
= block
->lines
;
3635 line
< block
->lines
+ block
->len
;
3637 fz_stext_span
*span
;
3639 for (span
= line
->first_span
; span
; span
= span
->next
) {
3642 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3643 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3644 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3647 if (pipespan (f
, span
, a
, b
)) {
3650 if (span
== page
->lmark
.span
) {
3653 if (span
== line
->last_span
) {
3654 if (putc ('\n', f
) == EOF
) {
3656 "failed break line on sel pipe: %s\n",
3667 int ret
= fclose (f
);
3670 if (errno
!= ECHILD
) {
3671 fprintf (stderr
, "failed to close sel pipe: %s\n",
3682 fprintf (stderr
, "failed to close sel pipe: %s\n",
3686 CAMLreturn (Val_unit
);
3689 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3691 CAMLparam1 (pagedimno_v
);
3693 int pagedimno
= Int_val (pagedimno_v
);
3696 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3697 if (trylock (__func__
)) {
3698 box
= fz_empty_rect
;
3701 box
= state
.pagedims
[pagedimno
].mediabox
;
3705 Store_double_field (ret_v
, 0, box
.x0
);
3706 Store_double_field (ret_v
, 1, box
.x1
);
3707 Store_double_field (ret_v
, 2, box
.y0
);
3708 Store_double_field (ret_v
, 3, box
.y1
);
3713 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3714 value dw_v
, value cols_v
)
3716 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3722 double winw
= Int_val (winw_v
);
3723 double winh
= Int_val (winh_v
);
3724 double dw
= Int_val (dw_v
);
3725 double cols
= Int_val (cols_v
);
3726 double pw
= 1.0, ph
= 1.0;
3728 if (trylock (__func__
)) {
3732 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3733 double w
= p
->pagebox
.x1
/ cols
;
3734 double h
= p
->pagebox
.y1
;
3738 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3740 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3743 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3746 ret_v
= caml_copy_double (zoom
);
3750 CAMLprim value
ml_getmaxw (value unit_v
)
3752 CAMLparam1 (unit_v
);
3758 if (trylock (__func__
)) {
3762 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3763 double w
= p
->pagebox
.x1
;
3764 maxw
= fz_max (maxw
, w
);
3769 ret_v
= caml_copy_double (maxw
);
3773 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3775 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3777 int pt
= Int_val(pt_v
);
3778 int x
= Int_val (x_v
);
3779 int y
= Int_val (y_v
);
3782 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3783 ret_v
= caml_copy_double (w
);
3787 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3789 CAMLparam2 (pt_v
, string_v
);
3791 int pt
= Int_val (pt_v
);
3794 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3795 ret_v
= caml_copy_double (w
);
3799 CAMLprim value
ml_getpagebox (value opaque_v
)
3801 CAMLparam1 (opaque_v
);
3807 char *s
= String_val (opaque_v
);
3808 struct page
*page
= parse_pointer (__func__
, s
);
3810 ret_v
= caml_alloc_tuple (4);
3811 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3812 dev
->hints
|= FZ_IGNORE_SHADE
;
3814 ctm
= pagectm (page
);
3815 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3817 fz_close_device (state
.ctx
, dev
);
3818 fz_drop_device (state
.ctx
, dev
);
3819 fz_round_rect (&bbox
, &rect
);
3820 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3821 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3822 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3823 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3828 CAMLprim value
ml_setaalevel (value level_v
)
3830 CAMLparam1 (level_v
);
3832 state
.aalevel
= Int_val (level_v
);
3833 CAMLreturn (Val_unit
);
3836 #pragma GCC diagnostic push
3837 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3838 #include <X11/Xlib.h>
3839 #include <X11/cursorfont.h>
3840 #pragma GCC diagnostic pop
3843 #include <EGL/egl.h>
3848 static const int shapes
[] = {
3849 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3852 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3865 XVisualInfo
*visual
;
3866 Cursor curs
[CURS_COUNT
];
3870 static VisualID
initvisual (void)
3872 /* On this system with: `Haswell-ULT Integrated Graphics
3873 Controller' and Mesa 11.0.6; perf stat reports [1] that when
3874 using glX chosen visual and auto scrolling some document in
3875 fullscreen the power/energy-gpu is more than 1 joule bigger
3876 than when using hand picked visual that stands alone in glxinfo
3877 output: it's dead last in the list and it's the only one with
3878 `visual dep' (sic) of 32
3880 No clue what's going on here...
3882 [1] perf stat -a -I 1200 -e "power/energy-gpu/"
3888 info
.class = TrueColor
;
3889 glx
.visual
= XGetVisualInfo (glx
.dpy
, VisualDepthMask
| VisualClassMask
,
3891 if (!ret
|| !glx
.visual
) {
3892 XCloseDisplay (glx
.dpy
);
3893 caml_failwith ("XGetVisualInfo");
3895 return glx
.visual
->visualid
;
3899 static void initcurs (void)
3901 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3902 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3906 CAMLprim
void ml_setbgcol (value color_v
)
3908 CAMLparam1 (color_v
);
3909 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3914 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3916 CAMLparam3 (display_v
, wid_v
, screen_v
);
3920 EGLint attribs
[] = {
3922 EGL_NATIVE_VISUAL_ID
, 0,
3926 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
3927 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
3932 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3934 caml_failwith ("XOpenDisplay");
3937 eglBindAPI (EGL_OPENGL_API
);
3939 glx
.edpy
= eglGetDisplay (glx
.dpy
);
3940 if (glx
.edpy
== EGL_NO_DISPLAY
) {
3941 caml_failwith ("eglGetDisplay");
3944 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
3945 caml_failwith ("eglInitialize");
3949 attribs
[1] = visid
= initvisual ();
3952 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
3954 caml_failwith ("eglChooseConfig");
3959 if (!eglGetConfigAttrib (glx
.edpy
, glx
.conf
,
3960 EGL_NATIVE_VISUAL_ID
, &visid
)) {
3961 caml_failwith ("eglGetConfigAttrib");
3966 glx
.wid
= Int_val (wid_v
);
3967 CAMLreturn (Val_int (visid
));
3970 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3972 CAMLparam1 (unit_v
);
3974 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
3976 caml_failwith ("eglCreateContext");
3979 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
3981 if (glx
.win
== EGL_NO_SURFACE
) {
3982 caml_failwith ("eglCreateWindowSurface");
3986 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
3988 caml_failwith ("eglMakeCurrent");
3990 CAMLreturn (Val_unit
);
3993 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3995 CAMLparam3 (display_v
, wid_v
, screen_v
);
3997 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3999 caml_failwith ("XOpenDisplay");
4005 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4006 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4008 XCloseDisplay (glx
.dpy
);
4009 caml_failwith ("glXChooseVisual");
4014 glx
.wid
= Int_val (wid_v
);
4015 CAMLreturn (Val_int (glx
.visual
->visualid
));
4018 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4020 CAMLparam1 (unit_v
);
4022 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4024 caml_failwith ("glXCreateContext");
4030 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4031 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4033 caml_failwith ("glXMakeCurrent");
4035 CAMLreturn (Val_unit
);
4039 CAMLprim value
ml_setcursor (value cursor_v
)
4041 CAMLparam1 (cursor_v
);
4042 size_t cursn
= Int_val (cursor_v
);
4044 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
4045 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
4047 CAMLreturn (Val_unit
);
4050 CAMLprim value
ml_swapb (value unit_v
)
4052 CAMLparam1 (unit_v
);
4054 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
4055 caml_failwith ("eglSwapBuffers");
4058 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4060 CAMLreturn (Val_unit
);
4063 #include "keysym2ucs.c"
4065 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4067 CAMLparam1 (keysym_v
);
4069 KeySym keysym
= Int_val (keysym_v
);
4074 rune
= keysym2ucs (keysym
);
4075 len
= fz_runetochar (buf
, rune
);
4077 str_v
= caml_copy_string (buf
);
4081 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4083 CAMLprim value
ml_platform (value unit_v
)
4085 CAMLparam1 (unit_v
);
4086 CAMLlocal2 (tup_v
, arr_v
);
4087 int platid
= piunknown
;
4090 #if defined __linux__
4092 #elif defined __CYGWIN__
4094 #elif defined __DragonFly__ || defined __FreeBSD__
4095 || defined __OpenBSD__
|| defined __NetBSD__
4097 #elif defined __sun__
4099 #elif defined __APPLE__
4102 if (uname (&buf
)) err (1, "uname");
4104 tup_v
= caml_alloc_tuple (2);
4106 char const *sar
[] = {
4113 arr_v
= caml_copy_string_array (sar
);
4115 Field (tup_v
, 0) = Val_int (platid
);
4116 Field (tup_v
, 1) = arr_v
;
4120 CAMLprim value
ml_cloexec (value fd_v
)
4123 int fd
= Int_val (fd_v
);
4125 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4126 uerror ("fcntl", Nothing
);
4128 CAMLreturn (Val_unit
);
4131 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4133 CAMLparam2 (w_v
, h_v
);
4136 int w
= Int_val (w_v
);
4137 int h
= Int_val (h_v
);
4138 int cs
= Int_val (cs_v
);
4140 if (state
.bo_usable
) {
4141 pbo
= calloc (sizeof (*pbo
), 1);
4143 err (1, "calloc pbo");
4155 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
4158 state
.glGenBuffersARB (1, &pbo
->id
);
4159 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4160 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4161 NULL
, GL_STREAM_DRAW
);
4162 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4164 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4166 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4167 state
.glDeleteBuffersARB (1, &pbo
->id
);
4169 ret_v
= caml_copy_string ("0");
4175 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4177 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4181 err (1, "malloc %d bytes failed", res
+1);
4183 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4185 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4187 ret_v
= caml_copy_string (s
);
4192 ret_v
= caml_copy_string ("0");
4197 CAMLprim value
ml_freepbo (value s_v
)
4200 char *s
= String_val (s_v
);
4201 struct tile
*tile
= parse_pointer (__func__
, s
);
4204 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4206 tile
->pbo
->ptr
= NULL
;
4207 tile
->pbo
->size
= -1;
4209 CAMLreturn (Val_unit
);
4212 CAMLprim value
ml_unmappbo (value s_v
)
4215 char *s
= String_val (s_v
);
4216 struct tile
*tile
= parse_pointer (__func__
, s
);
4219 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4220 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4221 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4223 tile
->pbo
->ptr
= NULL
;
4224 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4226 CAMLreturn (Val_unit
);
4229 static void setuppbo (void)
4232 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
4234 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4236 state
.bo_usable
= GGPA (glBindBufferARB
)
4237 && GGPA (glUnmapBufferARB
)
4238 && GGPA (glMapBufferARB
)
4239 && GGPA (glBufferDataARB
)
4240 && GGPA (glGenBuffersARB
)
4241 && GGPA (glDeleteBuffersARB
);
4245 CAMLprim value
ml_bo_usable (value unit_v
)
4247 CAMLparam1 (unit_v
);
4248 CAMLreturn (Val_bool (state
.bo_usable
));
4251 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4253 CAMLparam3 (ptr_v
, x_v
, y_v
);
4254 CAMLlocal2 (ret_v
, tup_v
);
4256 char *s
= String_val (ptr_v
);
4257 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4258 struct pagedim
*pdim
;
4262 page
= parse_pointer (__func__
, s
);
4263 pdim
= &state
.pagedims
[page
->pdimno
];
4265 ret_v
= Val_int (0);
4266 if (trylock (__func__
)) {
4270 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4271 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4272 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4277 p
.x
= x
+ pdim
->bounds
.x0
;
4278 p
.y
= y
+ pdim
->bounds
.y0
;
4280 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4281 fz_invert_matrix (&ctm
, &ctm
);
4282 fz_transform_point (&p
, &ctm
);
4284 tup_v
= caml_alloc_tuple (2);
4285 ret_v
= caml_alloc_small (1, 1);
4286 Field (tup_v
, 0) = Val_int (p
.x
);
4287 Field (tup_v
, 1) = Val_int (p
.y
);
4288 Field (ret_v
, 0) = tup_v
;
4295 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4296 value x_v
, value y_v
)
4298 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4301 char *s
= String_val (ptr_v
);
4302 int pageno
= Int_val (pageno_v
);
4303 int pdimno
= Int_val (pdimno_v
);
4304 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4305 struct pagedim
*pdim
;
4309 ret_v
= Val_int (0);
4313 page
= loadpage (pageno
, pdimno
);
4316 page
= parse_pointer (__func__
, s
);
4318 pdim
= &state
.pagedims
[pdimno
];
4320 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4321 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
4322 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4327 p
.x
= x
+ pdim
->bounds
.x0
;
4328 p
.y
= y
+ pdim
->bounds
.y0
;
4330 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4331 fz_transform_point (&p
, &ctm
);
4333 ret_v
= caml_alloc_tuple (2);
4334 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4335 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4344 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4347 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4348 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4354 char *s
= String_val (ptr_v
);
4356 page
= parse_pointer (__func__
, s
);
4357 annot
= pdf_create_annot (state
.ctx
,
4358 pdf_page_from_fz_page (state
.ctx
,
4361 p
.x
= Int_val (x_v
);
4362 p
.y
= Int_val (y_v
);
4363 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
4364 pdf_set_text_annot_position (state
.ctx
, annot
, p
);
4367 CAMLreturn (Val_unit
);
4370 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4372 CAMLparam2 (ptr_v
, n_v
);
4373 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4377 char *s
= String_val (ptr_v
);
4378 struct slink
*slink
;
4380 page
= parse_pointer (__func__
, s
);
4381 slink
= &page
->slinks
[Int_val (n_v
)];
4382 pdf_delete_annot (state
.ctx
,
4383 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
4384 (pdf_annot
*) slink
->u
.annot
);
4387 CAMLreturn (Val_unit
);
4390 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4392 CAMLparam3 (ptr_v
, n_v
, str_v
);
4393 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4397 char *s
= String_val (ptr_v
);
4398 struct slink
*slink
;
4400 page
= parse_pointer (__func__
, s
);
4401 slink
= &page
->slinks
[Int_val (n_v
)];
4402 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
4403 String_val (str_v
));
4406 CAMLreturn (Val_unit
);
4409 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4411 CAMLparam1 (unit_v
);
4412 CAMLreturn (Val_bool (state
.dirty
));
4415 CAMLprim value
ml_savedoc (value path_v
)
4417 CAMLparam1 (path_v
);
4418 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4421 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4423 CAMLreturn (Val_unit
);
4426 static void makestippletex (void)
4428 const char pixels
[] = "\xff\xff\0\0";
4429 glGenTextures (1, &state
.stid
);
4430 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4431 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4432 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4445 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4447 return caml_copy_string (FZ_VERSION
);
4450 #ifdef USE_FONTCONFIG
4456 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4460 int UNUSED_ATTR needs_exact_metrics
)
4467 FcPattern
*pat
, *pat1
;
4469 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4470 name
, bold
, italic
, needs_exact_metrics
);
4473 fc
.config
= FcInitLoadConfigAndFonts ();
4475 lprintf ("FcInitLoadConfigAndFonts failed\n");
4479 if (!fc
.config
) return NULL
;
4481 size
= strlen (name
);
4482 if (bold
) size
+= sizeof (":bold") - 1;
4483 if (italic
) size
+= sizeof (":italic") - 1;
4486 buf
= malloc (size
);
4488 err (1, "malloc %zu failed", size
);
4492 if (bold
&& italic
) {
4493 strcat (buf
, ":bold:italic");
4496 if (bold
) strcat (buf
, ":bold");
4497 if (italic
) strcat (buf
, ":italic");
4499 for (i
= 0; i
< size
; ++i
) {
4500 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4503 lprintf ("fcbuf=%s\n", buf
);
4504 pat
= FcNameParse ((FcChar8
*) buf
);
4506 printd ("emsg FcNameParse failed\n");
4511 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4512 printd ("emsg FcConfigSubstitute failed\n");
4516 FcDefaultSubstitute (pat
);
4518 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4520 printd ("emsg FcFontMatch failed\n");
4521 FcPatternDestroy (pat
);
4526 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4527 printd ("emsg FcPatternGetString failed\n");
4528 FcPatternDestroy (pat
);
4529 FcPatternDestroy (pat1
);
4535 printd ("emsg name=%s path=%s\n", name
, path
);
4537 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4538 FcPatternDestroy (pat
);
4539 FcPatternDestroy (pat1
);
4545 CAMLprim value
ml_init (value csock_v
, value params_v
)
4547 CAMLparam2 (csock_v
, params_v
);
4548 CAMLlocal2 (trim_v
, fuzz_v
);
4556 state
.csock
= Int_val (csock_v
);
4557 state
.rotate
= Int_val (Field (params_v
, 0));
4558 state
.fitmodel
= Int_val (Field (params_v
, 1));
4559 trim_v
= Field (params_v
, 2);
4560 texcount
= Int_val (Field (params_v
, 3));
4561 state
.sliceheight
= Int_val (Field (params_v
, 4));
4562 mustoresize
= Int_val (Field (params_v
, 5));
4563 colorspace
= Int_val (Field (params_v
, 6));
4564 fontpath
= String_val (Field (params_v
, 7));
4566 if (caml_string_length (Field (params_v
, 8)) > 0) {
4567 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4569 if (!state
.trimcachepath
) {
4570 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4574 haspboext
= Bool_val (Field (params_v
, 9));
4576 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4577 fz_register_document_handlers (state
.ctx
);
4579 #ifdef USE_FONTCONFIG
4580 if (Bool_val (Field (params_v
, 10))) {
4581 fz_install_load_system_font_funcs (
4582 state
.ctx
, fc_load_system_font_func
, NULL
4587 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4588 fuzz_v
= Field (trim_v
, 1);
4589 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4590 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4591 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4592 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4594 set_tex_params (colorspace
);
4597 #ifndef USE_FONTCONFIG
4598 state
.face
= load_font (fontpath
);
4602 char *buf
= fontpath
;
4603 FcPattern
*pat
, *pat1
;
4606 fc
.config
= FcInitLoadConfigAndFonts ();
4608 errx (1, "FcInitLoadConfigAndFonts failed");
4611 pat
= FcNameParse ((FcChar8
*) buf
);
4613 errx (1, "FcNameParse failed");
4616 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4617 errx (1, "FcConfigSubstitute failed");
4619 FcDefaultSubstitute (pat
);
4621 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4623 errx (1, "FcFontMatch failed");
4626 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4627 errx (1, "FcPatternGetString failed");
4630 state
.face
= load_font ((char *) path
);
4631 FcPatternDestroy (pat
);
4632 FcPatternDestroy (pat1
);
4637 const char *data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0,
4639 state
.face
= load_builtin_font (data
, len
);
4641 if (!state
.face
) _exit (1);
4643 realloctexts (texcount
);
4651 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4653 errx (1, "pthread_create: %s", strerror (ret
));
4656 CAMLreturn (Val_unit
);