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>
38 /* At least gcc (Gentoo 4.9.3 p1.0, pie-0.6.2) 4.9.3 emits erroneous
39 clobbered diagnostics */
40 #pragma GCC diagnostic ignored "-Wclobbered"
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wunused-parameter"
45 #pragma GCC diagnostic ignored "-Wshadow"
46 #include <mupdf/fitz.h>
47 #pragma GCC diagnostic pop
48 #include <mupdf/pdf.h>
51 #include FT_FREETYPE_H
54 #include <fontconfig/fontconfig.h>
58 #define CACHE_PAGEREFS
61 extern char **environ
;
64 #define MIN(a,b) ((a) < (b) ? (a) : (b))
65 #define MAX(a,b) ((a) > (b) ? (a) : (b))
68 #define NORETURN_ATTR __attribute__ ((noreturn))
69 #define UNUSED_ATTR __attribute__ ((unused))
70 #if !defined __clang__
71 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
73 #define OPTIMIZE_ATTR(n)
75 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
79 #define OPTIMIZE_ATTR(n)
80 #define GCC_FMT_ATTR(a, b)
85 #define FMT_ptr PRIxPTR
86 #define SCN_ptr SCNxPTR
87 #define FMT_ptr_cast(p) ((uintptr_t) (p))
88 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
90 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
91 err (int exitcode
, const char *fmt
, ...)
98 vfprintf (stderr
, fmt
, ap
);
100 fprintf (stderr
, ": %s\n", strerror (savederrno
));
105 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
106 errx (int exitcode
, const char *fmt
, ...)
111 vfprintf (stderr
, fmt
, ap
);
113 fputc ('\n', stderr
);
118 #ifndef GL_TEXTURE_RECTANGLE_ARB
119 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
123 #define TEXT_TYPE GL_TEXTURE_2D
125 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
129 #define GL_BGRA 0x80E1
132 #ifndef GL_UNSIGNED_INT_8_8_8_8
133 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
136 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
137 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
141 #define lprintf printf
146 #define ARSERT(cond) for (;;) { \
148 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
164 struct slice slices
[1];
175 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
179 enum { SLINK
, SANNOT
} tag
;
199 fz_stext_sheet
*sheet
;
201 fz_display_list
*dlist
;
203 struct slink
*slinks
;
205 struct annot
*annots
;
214 struct pagedim
*pagedims
;
229 fz_colorspace
*colorspace
;
237 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
260 void (*glBindBufferARB
) (GLenum
, GLuint
);
261 GLboolean (*glUnmapBufferARB
) (GLenum
);
262 void *(*glMapBufferARB
) (GLenum
, GLenum
);
263 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
264 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
265 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
267 GLfloat texcoords
[8];
268 GLfloat vertices
[16];
270 #ifdef CACHE_PAGEREFS
286 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
288 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
291 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
293 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
296 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
298 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
299 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
302 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
304 static void lock (const char *cap
)
306 int ret
= pthread_mutex_lock (&mutex
);
308 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
312 static void unlock (const char *cap
)
314 int ret
= pthread_mutex_unlock (&mutex
);
316 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
320 static int trylock (const char *cap
)
322 int ret
= pthread_mutex_trylock (&mutex
);
323 if (ret
&& ret
!= EBUSY
) {
324 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
329 static void *parse_pointer (const char *cap
, const char *s
)
334 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
336 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
341 static double now (void)
345 if (gettimeofday (&tv
, NULL
)) {
346 err (1, "gettimeofday");
348 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
351 static int hasdata (void)
354 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
355 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
359 CAMLprim value
ml_hasdata (value fd_v
)
364 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
365 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
366 CAMLreturn (Val_bool (avail
> 0));
369 static void readdata (int fd
, void *p
, int size
)
374 n
= read (fd
, p
, size
);
376 if (errno
== EINTR
) goto again
;
377 err (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
380 if (!n
) errx (1, "EOF while reading");
381 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
385 static void writedata (int fd
, char *p
, int size
)
389 /* One should lookup type punning/strict aliasing etc in standard,DRs,Web to
390 convince herself that this is:
392 b. practically the only way to achieve the result
393 (union puns notwithstanding) */
394 memcpy (p
, &size
, 4);
395 n
= write (fd
, p
, size
+ 4);
397 if (!n
) errx (1, "EOF while writing data");
398 err (1, "write (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
402 static int readlen (int fd
)
404 /* Type punned unions here. Why? Less code (Adjusted by more comments).
405 https://en.wikipedia.org/wiki/Type_punning */
406 union { int len
; char raw
[4]; } buf
;
407 readdata (fd
, buf
.raw
, 4);
411 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
413 CAMLparam3 (fd_v
, bytes_v
, len_v
);
414 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
418 CAMLprim value
ml_rcmd (value fd_v
)
421 CAMLlocal1 (strdata_v
);
422 int fd
= Int_val (fd_v
);
423 int len
= readlen (fd
);
424 strdata_v
= caml_alloc_string (len
);
425 readdata (fd
, String_val (strdata_v
), len
);
426 CAMLreturn (strdata_v
);
429 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
437 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
440 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
444 if (len
< size
- 4) {
445 writedata (state
.csock
, buf
, len
);
451 err (1, "vsnprintf for `%s' failed", fmt
);
453 buf
= realloc (buf
, size
);
458 static void closedoc (void)
460 #ifdef CACHE_PAGEREFS
461 if (state
.pdflut
.objs
) {
464 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
465 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
467 free (state
.pdflut
.objs
);
468 state
.pdflut
.objs
= NULL
;
469 state
.pdflut
.idx
= 0;
473 fz_drop_document (state
.ctx
, state
.doc
);
478 static int openxref (char *filename
, char *password
)
482 for (i
= 0; i
< state
.texcount
; ++i
) {
483 state
.texowners
[i
].w
= -1;
484 state
.texowners
[i
].slice
= NULL
;
490 if (state
.pagedims
) {
491 free (state
.pagedims
);
492 state
.pagedims
= NULL
;
494 state
.pagedimcount
= 0;
496 fz_set_aa_level (state
.ctx
, state
.aalevel
);
497 #ifdef CSS_HACK_TO_READ_EPUBS_COMFORTABLY
498 fz_set_user_css (state
.ctx
,
499 "body { margin-left: 20%; margin-right: 20%; }");
501 state
.doc
= fz_open_document (state
.ctx
, filename
);
502 if (fz_needs_password (state
.ctx
, state
.doc
)) {
503 if (password
&& !*password
) {
508 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
510 printd ("pass fail");
515 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
519 static void pdfinfo (void)
521 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
525 printd ("info PDF version\t%d.%d",
526 pdf
->version
/ 10, pdf
->version
% 10);
528 infoobj
= pdf_dict_gets (state
.ctx
, pdf_trailer (state
.ctx
,
533 char *items
[] = { "Title", "Author", "Creator",
534 "Producer", "CreationDate" };
536 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
537 pdf_obj
*obj
= pdf_dict_gets (state
.ctx
, infoobj
, items
[i
]);
538 s
= pdf_to_utf8 (state
.ctx
, pdf
, obj
);
539 if (*s
) printd ("info %s\t%s", items
[i
], s
);
540 fz_free (state
.ctx
, s
);
547 static void unlinktile (struct tile
*tile
)
551 for (i
= 0; i
< tile
->slicecount
; ++i
) {
552 struct slice
*s
= &tile
->slices
[i
];
554 if (s
->texindex
!= -1) {
555 if (state
.texowners
[s
->texindex
].slice
== s
) {
556 state
.texowners
[s
->texindex
].slice
= NULL
;
562 static void freepage (struct page
*page
)
566 fz_drop_stext_page (state
.ctx
, page
->text
);
569 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
574 fz_drop_display_list (state
.ctx
, page
->dlist
);
575 fz_drop_page (state
.ctx
, page
->fzpage
);
579 static void freetile (struct tile
*tile
)
584 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
587 fz_drop_pixmap (state
.ctx
, state
.pig
);
589 state
.pig
= tile
->pixmap
;
594 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
603 static int cacheline32bytes
;
605 static void __attribute__ ((constructor
)) clcheck (void)
607 char **envp
= environ
;
612 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
614 cacheline32bytes
= auxv
[1] == 32;
620 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
622 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
623 if (cacheline32bytes
&& size
> 32) {
624 intptr_t a1
, a2
, diff
;
626 vector
unsigned char v
= vec_splat_u8 (-1);
627 vector
unsigned char *p
;
629 a1
= a2
= (intptr_t) pixmap
->samples
;
630 a2
= (a1
+ 31) & ~31;
635 while (a1
!= a2
) *(char *) a1
++ = 0xff;
636 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
637 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
639 vec_st (v
, i
+ 16, p
);
641 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
643 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
646 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
649 static void trimctm (pdf_page
*page
, int pindex
)
652 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
654 if (!pdim
->tctmready
) {
655 if (state
.trimmargins
) {
657 fz_matrix rm
, sm
, tm
, im
, ctm1
;
659 fz_rotate (&rm
, -pdim
->rotate
);
660 fz_scale (&sm
, 1, -1);
661 fz_concat (&ctm
, &rm
, &sm
);
662 realbox
= pdim
->mediabox
;
663 fz_transform_rect (&realbox
, &ctm
);
664 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
665 fz_concat (&ctm1
, &ctm
, &tm
);
666 fz_invert_matrix (&im
, &page
->ctm
);
667 fz_concat (&ctm
, &im
, &ctm1
);
677 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
680 int pdimno
= pdim
- state
.pagedims
;
682 if (pdf_specifics (state
.ctx
, state
.doc
)) {
683 trimctm ((pdf_page
*) fzpage
, pdimno
);
684 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
687 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
688 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
693 static fz_matrix
pagectm (struct page
*page
)
695 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
698 static void *loadpage (int pageno
, int pindex
)
703 page
= calloc (sizeof (struct page
), 1);
705 err (1, "calloc page %d", pageno
);
708 page
->dlist
= fz_new_display_list (state
.ctx
);
709 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
711 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
712 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
715 fz_catch (state
.ctx
) {
718 fz_drop_device (state
.ctx
, dev
);
720 page
->pdimno
= pindex
;
721 page
->pageno
= pageno
;
722 page
->sgen
= state
.gen
;
723 page
->agen
= state
.gen
;
724 page
->tgen
= state
.gen
;
728 static struct tile
*alloctile (int h
)
735 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
736 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
737 tile
= calloc (tilesize
, 1);
739 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
741 for (i
= 0; i
< slicecount
; ++i
) {
742 int sh
= MIN (h
, state
.sliceheight
);
743 tile
->slices
[i
].h
= sh
;
744 tile
->slices
[i
].texindex
= -1;
747 tile
->slicecount
= slicecount
;
748 tile
->sliceheight
= state
.sliceheight
;
752 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
760 struct pagedim
*pdim
;
762 tile
= alloctile (h
);
763 pdim
= &state
.pagedims
[page
->pdimno
];
768 bbox
.x1
= bbox
.x0
+ w
;
769 bbox
.y1
= bbox
.y0
+ h
;
772 if (state
.pig
->w
== w
774 && state
.pig
->colorspace
== state
.colorspace
) {
775 tile
->pixmap
= state
.pig
;
776 tile
->pixmap
->x
= bbox
.x0
;
777 tile
->pixmap
->y
= bbox
.y0
;
780 fz_drop_pixmap (state
.ctx
, state
.pig
);
787 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
793 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
799 clearpixmap (tile
->pixmap
);
801 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
802 ctm
= pagectm (page
);
803 fz_rect_from_irect (&rect
, &bbox
);
804 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
805 fz_drop_device (state
.ctx
, dev
);
810 #ifdef CACHE_PAGEREFS
811 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
812 thanks to Robin Watts */
814 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
816 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
820 if (state
.pdflut
.idx
== state
.pagecount
) return;
822 kids
= pdf_dict_gets (ctx
, node
, "Kids");
823 len
= pdf_array_len (ctx
, kids
);
826 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
828 if (pdf_mark_obj (ctx
, node
))
829 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
830 for (i
= 0; i
< len
; i
++) {
831 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
832 char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
834 ? !strcmp (type
, "Pages")
835 : pdf_dict_gets (ctx
, kid
, "Kids")
836 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
837 pdf_collect_pages (doc
, kid
);
841 ? strcmp (type
, "Page") != 0
842 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
843 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
844 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
847 pdf_unmark_obj (ctx
, node
);
851 pdf_load_page_objs (pdf_document
*doc
)
853 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
854 pdf_trailer (state
.ctx
, doc
), "Root");
855 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
858 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
860 state
.pdflut
.idx
= 0;
861 pdf_collect_pages (doc
, node
);
865 static void initpdims (int wthack
)
869 fz_rect rootmediabox
;
870 int pageno
, trim
, show
;
871 int trimw
= 0, cxcount
;
872 fz_context
*ctx
= state
.ctx
;
873 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
879 if (state
.trimmargins
&& state
.trimcachepath
) {
880 trimf
= fopen (state
.trimcachepath
, "rb");
882 trimf
= fopen (state
.trimcachepath
, "wb");
887 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
888 cxcount
= state
.pagecount
;
890 cxcount
= MIN (state
.pagecount
, 1);
894 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
895 "Root/Pages/MediaBox");
896 pdf_to_rect (ctx
, obj
, &rootmediabox
);
899 #ifdef CACHE_PAGEREFS
900 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
901 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
902 if (!state
.pdflut
.objs
) {
903 err (1, "malloc pageobjs %zu %d %zu failed",
904 sizeof (*state
.pdflut
.objs
), cxcount
,
905 sizeof (*state
.pdflut
.objs
) * cxcount
);
907 state
.pdflut
.count
= cxcount
;
908 pdf_load_page_objs (pdf
);
909 state
.pdflut
.pdf
= pdf
;
913 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
919 pdf_obj
*pageref
, *pageobj
;
921 #ifdef CACHE_PAGEREFS
922 pageref
= state
.pdflut
.objs
[pageno
];
924 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
926 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
928 if (state
.trimmargins
) {
933 page
= pdf_load_page (ctx
, pdf
, pageno
);
934 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
935 trim
= state
.trimanew
|| !obj
;
941 dev
= fz_new_bbox_device (ctx
, &rect
);
942 dev
->hints
|= FZ_IGNORE_SHADE
;
943 fz_invert_matrix (&ctm
, &page
->ctm
);
944 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
945 fz_drop_device (ctx
, dev
);
947 rect
.x0
+= state
.trimfuzz
.x0
;
948 rect
.x1
+= state
.trimfuzz
.x1
;
949 rect
.y0
+= state
.trimfuzz
.y0
;
950 rect
.y1
+= state
.trimfuzz
.y1
;
951 fz_transform_rect (&rect
, &ctm
);
952 fz_intersect_rect (&rect
, &page
->mediabox
);
954 if (fz_is_empty_rect (&rect
)) {
955 mediabox
= page
->mediabox
;
961 obj
= pdf_new_array (ctx
, pdf
, 4);
962 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
964 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
966 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
968 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
970 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
973 mediabox
.x0
= pdf_to_real (ctx
,
974 pdf_array_get (ctx
, obj
, 0));
975 mediabox
.y0
= pdf_to_real (ctx
,
976 pdf_array_get (ctx
, obj
, 1));
977 mediabox
.x1
= pdf_to_real (ctx
,
978 pdf_array_get (ctx
, obj
, 2));
979 mediabox
.y1
= pdf_to_real (ctx
,
980 pdf_array_get (ctx
, obj
, 3));
983 rotate
= page
->rotate
;
984 fz_drop_page (ctx
, &page
->super
);
986 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
988 printd ("progress %f Trimming %d",
989 (double) (pageno
+ 1) / state
.pagecount
,
994 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1002 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
1004 if (fz_is_empty_rect (&mediabox
)) {
1013 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1015 if (!fz_is_empty_rect (&cropbox
)) {
1020 fz_intersect_rect (&mediabox
, &cropbox
);
1025 if (fz_is_empty_rect (&rootmediabox
)) {
1027 "cannot find page size for page %d\n",
1031 mediabox
= rootmediabox
;
1035 rotate
= pdf_to_int (ctx
,
1036 pdf_dict_gets (ctx
, pageobj
, "Rotate"));
1040 if (state
.trimmargins
&& trimw
) {
1044 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1045 fz_bound_page (ctx
, page
, &mediabox
);
1047 if (state
.trimmargins
) {
1051 dev
= fz_new_bbox_device (ctx
, &rect
);
1052 dev
->hints
|= FZ_IGNORE_SHADE
;
1053 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1054 fz_drop_device (ctx
, dev
);
1056 rect
.x0
+= state
.trimfuzz
.x0
;
1057 rect
.x1
+= state
.trimfuzz
.x1
;
1058 rect
.y0
+= state
.trimfuzz
.y0
;
1059 rect
.y1
+= state
.trimfuzz
.y1
;
1060 fz_intersect_rect (&rect
, &mediabox
);
1062 if (!fz_is_empty_rect (&rect
)) {
1066 fz_drop_page (ctx
, page
);
1068 printd ("progress %f loading %d",
1069 (double) (pageno
+ 1) / state
.pagecount
,
1076 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1078 err (1, "fwrite trim mediabox");
1084 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1086 err (1, "fread trim mediabox %d", pageno
);
1092 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1093 fz_bound_page (ctx
, page
, &mediabox
);
1094 fz_drop_page (ctx
, page
);
1096 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1098 printd ("progress %f Gathering dimensions %d",
1099 (double) (pageno
) / state
.pagecount
,
1104 fprintf (stderr
, "failed to load page %d\n", pageno
);
1110 if (state
.pagedimcount
== 0
1111 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1112 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1115 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1116 state
.pagedims
= realloc (state
.pagedims
, size
);
1117 if (!state
.pagedims
) {
1118 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1119 size
, state
.pagedimcount
+ 1);
1122 p
= &state
.pagedims
[state
.pagedimcount
++];
1124 p
->mediabox
= mediabox
;
1130 printd ("progress 1 %s %d pages in %f seconds",
1131 state
.trimmargins
? "Trimmed" : "Processed",
1132 state
.pagecount
, end
- start
);
1136 if (fclose (trimf
)) {
1142 static void layout (void)
1147 struct pagedim
*p
= p
;
1148 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1150 if (state
.pagedimcount
== 0) return;
1152 switch (state
.fitmodel
) {
1153 case FitProportional
:
1154 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1157 p
= &state
.pagedims
[pindex
];
1158 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1160 fz_transform_rect (&box
, &rm
);
1162 x0
= MIN (box
.x0
, box
.x1
);
1163 x1
= MAX (box
.x0
, box
.x1
);
1166 maxw
= MAX (w
, maxw
);
1167 zoom
= state
.w
/ maxw
;
1179 ARSERT (0 && state
.fitmodel
);
1182 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1186 p
= &state
.pagedims
[pindex
];
1187 fz_rotate (&ctm
, state
.rotate
);
1188 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1190 fz_transform_rect (&box
, &rm
);
1191 w
= box
.x1
- box
.x0
;
1192 switch (state
.fitmodel
) {
1193 case FitProportional
:
1194 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1200 h
= box
.y1
- box
.y0
;
1202 zoom
= MIN (zw
, zh
);
1203 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1212 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1213 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1215 fz_rotate (&rm
, p
->rotate
);
1216 p
->pagebox
= p
->mediabox
;
1217 fz_transform_rect (&p
->pagebox
, &rm
);
1218 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1219 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1223 fz_transform_rect (&rect
, &ctm
);
1224 fz_round_rect (&p
->bounds
, &rect
);
1227 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1228 fz_scale (&sm
, zoom
, -zoom
);
1229 fz_concat (&ctm
, &tm
, &sm
);
1230 fz_concat (&p
->lctm
, &ctm
, &rm
);
1236 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1237 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1238 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1239 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1240 int boundw
= x1
- x0
;
1241 int boundh
= y1
- y0
;
1243 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1244 } while (p
-- != state
.pagedims
);
1248 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1249 desttoanchor (fz_link_dest
*dest
)
1253 struct pagedim
*pdim
= state
.pagedims
;
1258 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1259 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1261 pdim
= &state
.pagedims
[i
];
1263 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1265 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1266 p
.x
= dest
->ld
.gotor
.lt
.x
;
1269 p
.y
= dest
->ld
.gotor
.lt
.y
;
1270 fz_transform_point (&p
, &pdim
->lctm
);
1274 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1275 double x0
, x1
, y0
, y1
;
1277 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1278 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1280 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1281 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1283 a
.n
= dest
->ld
.gotor
.page
;
1288 static void recurse_outline (fz_outline
*outline
, int level
)
1291 switch (outline
->dest
.kind
) {
1294 struct anchor a
= desttoanchor (&outline
->dest
);
1297 printd ("o %d %d %d %d %s",
1298 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1304 printd ("ou %d %" FMT_s
" %s %s", level
,
1305 strlen (outline
->title
), outline
->title
,
1306 outline
->dest
.ld
.uri
.uri
);
1310 printd ("on %d %s", level
, outline
->title
);
1314 printd ("emsg Unhandled outline kind %d for %s\n",
1315 outline
->dest
.kind
, outline
->title
);
1318 if (outline
->down
) {
1319 recurse_outline (outline
->down
, level
+ 1);
1321 outline
= outline
->next
;
1325 static void process_outline (void)
1327 fz_outline
*outline
;
1329 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1331 state
.needoutline
= 0;
1332 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1334 recurse_outline (outline
, 0);
1335 fz_drop_outline (state
.ctx
, outline
);
1339 static char *strofspan (fz_stext_span
*span
)
1344 size_t size
= 0, cap
= 80;
1346 p
= malloc (cap
+ 1);
1347 if (!p
) return NULL
;
1349 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1350 int n
= fz_runetochar (utf8
, ch
->c
);
1351 if (size
+ n
> cap
) {
1353 p
= realloc (p
, cap
+ 1);
1354 if (!p
) return NULL
;
1357 memcpy (p
+ size
, utf8
, n
);
1364 static int matchspan (regex_t
*re
, fz_stext_span
*span
,
1365 int stop
, int pageno
, double start
)
1372 fz_point p1
, p2
, p3
, p4
;
1374 p
= strofspan (span
);
1377 ret
= regexec (re
, p
, 1, &rm
, 0);
1380 if (ret
!= REG_NOMATCH
) {
1383 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1384 printd ("msg regexec error `%.*s'",
1385 (int) size
, errbuf
);
1393 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1394 c
+= fz_runelen (span
->text
[a
].c
);
1396 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1397 c
+= fz_runelen (span
->text
[b
].c
);
1400 if (fz_runelen (span
->text
[b
].c
) > 1) {
1404 fz_stext_char_bbox (state
.ctx
, &sb
, span
, a
);
1405 fz_stext_char_bbox (state
.ctx
, &eb
, span
, b
);
1417 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1424 printd ("progress 1 found at %d `%.*s' in %f sec",
1425 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1429 printd ("match %d %d %f %f %f %f %f %f %f %f",
1441 static int compareblocks (const void *l
, const void *r
)
1443 fz_stext_block
const *ls
= l
;
1444 fz_stext_block
const *rs
= r
;
1445 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1448 /* wishful thinking function */
1449 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1453 fz_stext_page
*text
;
1454 fz_stext_sheet
*sheet
;
1455 struct pagedim
*pdim
, *pdimprev
;
1456 int stop
= 0, niters
= 0;
1461 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1462 if (niters
++ == 5) {
1465 printd ("progress 1 attention requested aborting search at %d",
1470 printd ("progress %f searching in page %d",
1471 (double) (pageno
+ 1) / state
.pagecount
,
1476 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1477 pdim
= &state
.pagedims
[i
];
1478 if (pdim
->pageno
== pageno
) {
1481 if (pdim
->pageno
> pageno
) {
1490 sheet
= fz_new_stext_sheet (state
.ctx
);
1491 text
= fz_new_stext_page (state
.ctx
);
1492 tdev
= fz_new_stext_device (state
.ctx
, sheet
, text
);
1494 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1496 fz_matrix ctm
= pagectm1 (page
, pdim
);
1497 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1500 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1501 fz_drop_device (state
.ctx
, tdev
);
1503 for (j
= 0; j
< text
->len
; ++j
) {
1506 fz_stext_block
*block
;
1508 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1509 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1512 for (k
= 0; k
< block
->len
; ++k
) {
1513 fz_stext_line
*line
;
1514 fz_stext_span
*span
;
1517 line
= &block
->lines
[k
];
1518 if (line
->bbox
.y0
< y
+ 1) continue;
1521 line
= &block
->lines
[block
->len
- 1 - k
];
1522 if (line
->bbox
.y0
> y
- 1) continue;
1525 for (span
= line
->first_span
; span
; span
= span
->next
) {
1526 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1528 case 1: stop
= 1; break;
1529 case -1: stop
= 1; goto endloop
;
1543 fz_drop_stext_page (state
.ctx
, text
);
1544 fz_drop_stext_sheet (state
.ctx
, sheet
);
1545 fz_drop_page (state
.ctx
, page
);
1549 printd ("progress 1 no matches %f sec", end
- start
);
1551 printd ("clearrects");
1554 static void set_tex_params (int colorspace
)
1561 switch (colorspace
) {
1563 state
.texiform
= GL_RGBA8
;
1564 state
.texform
= GL_RGBA
;
1565 state
.texty
= GL_UNSIGNED_BYTE
;
1566 state
.colorspace
= fz_device_rgb (state
.ctx
);
1569 state
.texiform
= GL_RGBA8
;
1570 state
.texform
= GL_BGRA
;
1571 state
.texty
= endianness
.s
> 1
1572 ? GL_UNSIGNED_INT_8_8_8_8
1573 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1574 state
.colorspace
= fz_device_bgr (state
.ctx
);
1577 state
.texiform
= GL_LUMINANCE_ALPHA
;
1578 state
.texform
= GL_LUMINANCE_ALPHA
;
1579 state
.texty
= GL_UNSIGNED_BYTE
;
1580 state
.colorspace
= fz_device_gray (state
.ctx
);
1583 errx (1, "invalid colorspce %d", colorspace
);
1587 static void realloctexts (int texcount
)
1591 if (texcount
== state
.texcount
) return;
1593 if (texcount
< state
.texcount
) {
1594 glDeleteTextures (state
.texcount
- texcount
,
1595 state
.texids
+ texcount
);
1598 size
= texcount
* sizeof (*state
.texids
);
1599 state
.texids
= realloc (state
.texids
, size
);
1600 if (!state
.texids
) {
1601 err (1, "realloc texids %" FMT_s
, size
);
1604 size
= texcount
* sizeof (*state
.texowners
);
1605 state
.texowners
= realloc (state
.texowners
, size
);
1606 if (!state
.texowners
) {
1607 err (1, "realloc texowners %" FMT_s
, size
);
1609 if (texcount
> state
.texcount
) {
1612 glGenTextures (texcount
- state
.texcount
,
1613 state
.texids
+ state
.texcount
);
1614 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1615 state
.texowners
[i
].w
= -1;
1616 state
.texowners
[i
].slice
= NULL
;
1619 state
.texcount
= texcount
;
1623 static char *mbtoutf8 (char *s
)
1629 len
= mbstowcs (NULL
, s
, strlen (s
));
1634 if (len
== (size_t) -1) {
1639 tmp
= malloc (len
* sizeof (wchar_t));
1644 ret
= mbstowcs (tmp
, s
, len
);
1645 if (ret
== (size_t) -1) {
1651 for (i
= 0; i
< ret
; ++i
) {
1652 len
+= fz_runelen (tmp
[i
]);
1655 p
= r
= malloc (len
+ 1);
1661 for (i
= 0; i
< ret
; ++i
) {
1662 p
+= fz_runetochar (p
, tmp
[i
]);
1669 CAMLprim value
ml_mbtoutf8 (value s_v
)
1675 s
= String_val (s_v
);
1681 ret_v
= caml_copy_string (r
);
1687 static void * mainloop (void UNUSED_ATTR
*unused
)
1690 int len
, ret
, oldlen
= 0;
1695 len
= readlen (state
.csock
);
1697 errx (1, "readlen returned 0");
1700 if (oldlen
< len
+ 1) {
1701 p
= realloc (p
, len
+ 1);
1703 err (1, "realloc %d failed", len
+ 1);
1707 readdata (state
.csock
, p
, len
);
1710 if (!strncmp ("open", p
, 4)) {
1711 int wthack
, off
, ok
= 0;
1718 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1720 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1723 filename
= p
+ 5 + off
;
1724 filenamelen
= strlen (filename
);
1725 password
= filename
+ filenamelen
+ 1;
1728 fz_try (state
.ctx
) {
1729 ok
= openxref (filename
, password
);
1731 fz_catch (state
.ctx
) {
1732 utf8filename
= mbtoutf8 (filename
);
1733 printd ("msg Could not open %s", utf8filename
);
1743 utf8filename
= mbtoutf8 (filename
);
1744 printd ("msg Opened %s (press h/F1 to get help)",
1746 if (utf8filename
!= filename
) {
1747 free (utf8filename
);
1750 state
.needoutline
= 1;
1753 else if (!strncmp ("cs", p
, 2)) {
1756 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1758 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1761 set_tex_params (colorspace
);
1762 for (i
= 0; i
< state
.texcount
; ++i
) {
1763 state
.texowners
[i
].w
= -1;
1764 state
.texowners
[i
].slice
= NULL
;
1768 else if (!strncmp ("freepage", p
, 8)) {
1771 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1773 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1777 else if (!strncmp ("freetile", p
, 8)) {
1780 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1782 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1786 else if (!strncmp ("search", p
, 6)) {
1787 int icase
, pageno
, y
, len2
, forward
;
1791 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1792 &icase
, &pageno
, &y
, &forward
, &len2
);
1794 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1797 pattern
= p
+ 6 + len2
;
1798 ret
= regcomp (&re
, pattern
,
1799 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1804 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1805 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1808 search (&re
, pageno
, y
, forward
);
1812 else if (!strncmp ("geometry", p
, 8)) {
1816 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1818 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1826 for (i
= 0; i
< state
.texcount
; ++i
) {
1827 state
.texowners
[i
].slice
= NULL
;
1830 state
.fitmodel
= fitmodel
;
1835 unlock ("geometry");
1836 printd ("continue %d", state
.pagecount
);
1838 else if (!strncmp ("reqlayout", p
, 9)) {
1841 unsigned int fitmodel
;
1842 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1845 ret
= sscanf (p
+ 9, " %d %u %d %n",
1846 &rotate
, &fitmodel
, &h
, &off
);
1848 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1851 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1854 state
.rotate
= rotate
;
1855 state
.fitmodel
= fitmodel
;
1860 nameddest
= p
+ 9 + off
;
1861 if (pdf
&& nameddest
&& *nameddest
) {
1864 pdf_obj
*needle
, *obj
;
1866 needle
= pdf_new_string (state
.ctx
, pdf
, nameddest
,
1867 strlen (nameddest
));
1868 obj
= pdf_lookup_dest (state
.ctx
, pdf
, needle
);
1870 dest
= pdf_parse_link_dest (state
.ctx
, pdf
,
1873 a
= desttoanchor (&dest
);
1875 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1878 printd ("emsg failed to parse destination `%s'\n",
1883 printd ("emsg destination `%s' not found\n",
1886 pdf_drop_obj (state
.ctx
, needle
);
1890 unlock ("reqlayout");
1891 printd ("continue %d", state
.pagecount
);
1893 else if (!strncmp ("page", p
, 4)) {
1898 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1900 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1905 page
= loadpage (pageno
, pindex
);
1909 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1911 else if (!strncmp ("tile", p
, 4)) {
1918 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1919 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1920 SCN_ptr_cast (&data
));
1922 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1927 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1931 printd ("tile %d %d %" FMT_ptr
" %u %f",
1933 FMT_ptr_cast (tile
),
1934 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1937 else if (!strncmp ("trimset", p
, 7)) {
1941 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1942 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1944 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1947 state
.trimmargins
= trimmargins
;
1948 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1950 state
.trimfuzz
= fuzz
;
1954 else if (!strncmp ("settrim", p
, 7)) {
1958 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1959 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1961 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1965 state
.trimmargins
= trimmargins
;
1966 state
.needoutline
= 1;
1967 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1969 state
.trimfuzz
= fuzz
;
1971 state
.pagedimcount
= 0;
1972 free (state
.pagedims
);
1973 state
.pagedims
= NULL
;
1978 printd ("continue %d", state
.pagecount
);
1980 else if (!strncmp ("sliceh", p
, 6)) {
1983 ret
= sscanf (p
+ 6, " %d", &h
);
1985 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1987 if (h
!= state
.sliceheight
) {
1990 state
.sliceheight
= h
;
1991 for (i
= 0; i
< state
.texcount
; ++i
) {
1992 state
.texowners
[i
].w
= -1;
1993 state
.texowners
[i
].h
= -1;
1994 state
.texowners
[i
].slice
= NULL
;
1998 else if (!strncmp ("interrupt", p
, 9)) {
1999 printd ("vmsg interrupted");
2002 errx (1, "unknown command %.*s", len
, p
);
2008 CAMLprim value
ml_realloctexts (value texcount_v
)
2010 CAMLparam1 (texcount_v
);
2013 if (trylock (__func__
)) {
2017 realloctexts (Int_val (texcount_v
));
2022 CAMLreturn (Val_bool (ok
));
2025 static void recti (int x0
, int y0
, int x1
, int y1
)
2027 GLfloat
*v
= state
.vertices
;
2029 glVertexPointer (2, GL_FLOAT
, 0, v
);
2030 v
[0] = x0
; v
[1] = y0
;
2031 v
[2] = x1
; v
[3] = y0
;
2032 v
[4] = x0
; v
[5] = y1
;
2033 v
[6] = x1
; v
[7] = y1
;
2034 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2037 static void showsel (struct page
*page
, int ox
, int oy
)
2042 fz_stext_line
*line
;
2043 fz_page_block
*pageb
;
2044 fz_stext_block
*block
;
2045 struct mark first
, last
;
2046 unsigned char selcolor
[] = {15,15,15,140};
2048 first
= page
->fmark
;
2051 if (!first
.span
|| !last
.span
) return;
2053 glEnable (GL_BLEND
);
2054 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2055 glColor4ubv (selcolor
);
2057 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2058 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2059 for (pageb
= page
->text
->blocks
;
2060 pageb
< page
->text
->blocks
+ page
->text
->len
;
2062 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2063 block
= pageb
->u
.text
;
2065 for (line
= block
->lines
;
2066 line
< block
->lines
+ block
->len
;
2068 fz_stext_span
*span
;
2069 rect
= fz_empty_rect
;
2071 for (span
= line
->first_span
; span
; span
= span
->next
) {
2073 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2078 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2080 j
= MIN (first
.i
, last
.i
);
2081 k
= MAX (first
.i
, last
.i
);
2084 if (span
== first
.span
) {
2088 else if (span
== last
.span
) {
2095 for (i
= j
; i
<= k
; ++i
) {
2097 fz_union_rect (&rect
,
2098 fz_stext_char_bbox (state
.ctx
, &bbox1
,
2101 fz_round_rect (&bbox
, &rect
);
2102 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2109 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2110 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2111 if (span
== last
.span
) {
2114 rect
= fz_empty_rect
;
2120 glDisable (GL_BLEND
);
2125 static void stipplerect (fz_matrix
*m
,
2133 fz_transform_point (p1
, m
);
2134 fz_transform_point (p2
, m
);
2135 fz_transform_point (p3
, m
);
2136 fz_transform_point (p4
, m
);
2142 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2146 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2148 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2149 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2151 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2152 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2154 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2155 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2157 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2158 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2160 glDrawArrays (GL_LINES
, 0, 8);
2163 static void solidrect (fz_matrix
*m
,
2170 fz_transform_point (p1
, m
);
2171 fz_transform_point (p2
, m
);
2172 fz_transform_point (p3
, m
);
2173 fz_transform_point (p4
, m
);
2174 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2175 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2177 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2178 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2179 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2182 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2185 fz_matrix ctm
, tm
, pm
;
2186 fz_link
*link
, *links
;
2187 GLfloat
*texcoords
= state
.texcoords
;
2188 GLfloat
*vertices
= state
.vertices
;
2190 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2192 glEnable (GL_TEXTURE_1D
);
2193 glEnable (GL_BLEND
);
2194 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2195 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2197 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2198 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2199 fz_translate (&tm
, xoff
, yoff
);
2200 pm
= pagectm (page
);
2201 fz_concat (&ctm
, &pm
, &tm
);
2203 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2204 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2206 for (link
= links
; link
; link
= link
->next
) {
2207 fz_point p1
, p2
, p3
, p4
;
2209 p1
.x
= link
->rect
.x0
;
2210 p1
.y
= link
->rect
.y0
;
2212 p2
.x
= link
->rect
.x1
;
2213 p2
.y
= link
->rect
.y0
;
2215 p3
.x
= link
->rect
.x1
;
2216 p3
.y
= link
->rect
.y1
;
2218 p4
.x
= link
->rect
.x0
;
2219 p4
.y
= link
->rect
.y1
;
2221 switch (link
->dest
.kind
) {
2222 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2223 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2224 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2225 default: glColor3ub (0, 0, 0); break;
2227 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2230 for (i
= 0; i
< page
->annotcount
; ++i
) {
2231 fz_point p1
, p2
, p3
, p4
;
2232 struct annot
*annot
= &page
->annots
[i
];
2234 p1
.x
= annot
->bbox
.x0
;
2235 p1
.y
= annot
->bbox
.y0
;
2237 p2
.x
= annot
->bbox
.x1
;
2238 p2
.y
= annot
->bbox
.y0
;
2240 p3
.x
= annot
->bbox
.x1
;
2241 p3
.y
= annot
->bbox
.y1
;
2243 p4
.x
= annot
->bbox
.x0
;
2244 p4
.y
= annot
->bbox
.y1
;
2246 glColor3ub (0, 0, 128);
2247 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2250 glDisable (GL_BLEND
);
2251 glDisable (GL_TEXTURE_1D
);
2254 static int compareslinks (const void *l
, const void *r
)
2256 struct slink
const *ls
= l
;
2257 struct slink
const *rs
= r
;
2258 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2259 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2261 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2264 static void droptext (struct page
*page
)
2267 fz_drop_stext_page (state
.ctx
, page
->text
);
2270 page
->fmark
.span
= NULL
;
2271 page
->lmark
.span
= NULL
;
2275 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
2280 static void dropannots (struct page
*page
)
2283 free (page
->annots
);
2284 page
->annots
= NULL
;
2285 page
->annotcount
= 0;
2289 static void ensureannots (struct page
*page
)
2292 size_t annotsize
= sizeof (*page
->annots
);
2295 if (state
.gen
!= page
->agen
) {
2297 page
->agen
= state
.gen
;
2299 if (page
->annots
) return;
2301 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2303 annot
= fz_next_annot (state
.ctx
, annot
)) {
2308 page
->annotcount
= count
;
2309 page
->annots
= calloc (count
, annotsize
);
2310 if (!page
->annots
) {
2311 err (1, "calloc annots %d", count
);
2314 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2316 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2319 fz_bound_annot (state
.ctx
, annot
, &rect
);
2320 page
->annots
[i
].annot
= annot
;
2321 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2326 static void dropslinks (struct page
*page
)
2329 free (page
->slinks
);
2330 page
->slinks
= NULL
;
2331 page
->slinkcount
= 0;
2335 static void ensureslinks (struct page
*page
)
2339 size_t slinksize
= sizeof (*page
->slinks
);
2340 fz_link
*link
, *links
;
2342 ensureannots (page
);
2343 if (state
.gen
!= page
->sgen
) {
2345 page
->sgen
= state
.gen
;
2347 if (page
->slinks
) return;
2349 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2350 ctm
= pagectm (page
);
2352 count
= page
->annotcount
;
2353 for (link
= links
; link
; link
= link
->next
) {
2359 page
->slinkcount
= count
;
2360 page
->slinks
= calloc (count
, slinksize
);
2361 if (!page
->slinks
) {
2362 err (1, "calloc slinks %d", count
);
2365 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2369 fz_transform_rect (&rect
, &ctm
);
2370 page
->slinks
[i
].tag
= SLINK
;
2371 page
->slinks
[i
].u
.link
= link
;
2372 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2374 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2376 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2377 fz_transform_rect (&rect
, &ctm
);
2378 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2380 page
->slinks
[i
].tag
= SANNOT
;
2381 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2383 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2387 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2388 static void fmt_linkn (char *s
, unsigned int u
)
2390 unsigned int len
; unsigned int q
;
2391 unsigned int zma
= 'z' - 'a' + 1;
2393 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2396 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2397 /* handles u == 0 */
2402 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2403 int noff
, char *targ
, int tlen
, int hfsize
)
2407 struct slink
*slink
;
2408 double x0
, y0
, x1
, y1
, w
;
2410 ensureslinks (page
);
2411 glColor3ub (0xc3, 0xb0, 0x91);
2412 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2413 fmt_linkn (buf
, i
+ noff
);
2414 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2415 slink
= &page
->slinks
[i
];
2417 x0
= slink
->bbox
.x0
+ xoff
- 5;
2418 y1
= slink
->bbox
.y0
+ yoff
- 5;
2419 y0
= y1
+ 10 + hfsize
;
2420 w
= measure_string (state
.face
, hfsize
, buf
);
2422 recti (x0
, y0
, x1
, y1
);
2426 glEnable (GL_BLEND
);
2427 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2428 glEnable (GL_TEXTURE_2D
);
2429 glColor3ub (0, 0, 0);
2430 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2431 fmt_linkn (buf
, i
+ noff
);
2432 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2433 slink
= &page
->slinks
[i
];
2435 x0
= slink
->bbox
.x0
+ xoff
;
2436 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2437 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2440 glDisable (GL_TEXTURE_2D
);
2441 glDisable (GL_BLEND
);
2444 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2447 struct slice
*slice1
;
2448 unsigned char *texdata
;
2451 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2452 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2454 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2455 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2456 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2460 int texindex
= state
.texindex
++ % state
.texcount
;
2462 if (state
.texowners
[texindex
].w
== tile
->w
) {
2463 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2467 state
.texowners
[texindex
].h
= slice
->h
;
2471 state
.texowners
[texindex
].h
= slice
->h
;
2474 state
.texowners
[texindex
].w
= tile
->w
;
2475 state
.texowners
[texindex
].slice
= slice
;
2476 slice
->texindex
= texindex
;
2478 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2479 #if TEXT_TYPE == GL_TEXTURE_2D
2480 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2481 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2482 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2483 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2486 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2490 texdata
= tile
->pixmap
->samples
;
2493 glTexSubImage2D (TEXT_TYPE
,
2505 glTexImage2D (TEXT_TYPE
,
2517 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2522 CAMLprim value
ml_begintiles (value unit_v
)
2524 CAMLparam1 (unit_v
);
2525 glEnable (TEXT_TYPE
);
2526 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2527 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2528 CAMLreturn (unit_v
);
2531 CAMLprim value
ml_endtiles (value unit_v
)
2533 CAMLparam1 (unit_v
);
2534 glDisable (TEXT_TYPE
);
2535 CAMLreturn (unit_v
);
2538 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2540 CAMLparam2 (args_v
, ptr_v
);
2541 int dispx
= Int_val (Field (args_v
, 0));
2542 int dispy
= Int_val (Field (args_v
, 1));
2543 int dispw
= Int_val (Field (args_v
, 2));
2544 int disph
= Int_val (Field (args_v
, 3));
2545 int tilex
= Int_val (Field (args_v
, 4));
2546 int tiley
= Int_val (Field (args_v
, 5));
2547 char *s
= String_val (ptr_v
);
2548 struct tile
*tile
= parse_pointer (__func__
, s
);
2549 int slicey
, firstslice
;
2550 struct slice
*slice
;
2551 GLfloat
*texcoords
= state
.texcoords
;
2552 GLfloat
*vertices
= state
.vertices
;
2554 firstslice
= tiley
/ tile
->sliceheight
;
2555 slice
= &tile
->slices
[firstslice
];
2556 slicey
= tiley
% tile
->sliceheight
;
2561 dh
= slice
->h
- slicey
;
2562 dh
= MIN (disph
, dh
);
2563 uploadslice (tile
, slice
);
2565 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2566 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2567 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2568 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2570 vertices
[0] = dispx
; vertices
[1] = dispy
;
2571 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2572 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2573 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2575 #if TEXT_TYPE == GL_TEXTURE_2D
2576 for (int i
= 0; i
< 8; ++i
) {
2577 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2581 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2585 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2588 CAMLreturn (Val_unit
);
2591 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2593 fz_matrix ctm
, tm
, pm
;
2594 fz_point p1
, p2
, p3
, p4
;
2595 GLfloat
*vertices
= state
.vertices
;
2596 double *v
= (double *) rects_v
;
2598 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2599 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2600 fz_translate (&tm
, xoff
, yoff
);
2601 pm
= pagectm (page
);
2602 fz_concat (&ctm
, &pm
, &tm
);
2604 glEnable (GL_BLEND
);
2605 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2619 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2620 glDisable (GL_BLEND
);
2623 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2624 value xoff_v
, value yoff_v
,
2627 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2628 int xoff
= Int_val (xoff_v
);
2629 int yoff
= Int_val (yoff_v
);
2630 int noff
= Int_val (Field (li_v
, 0));
2631 char *targ
= String_val (Field (li_v
, 1));
2632 int tlen
= caml_string_length (Field (li_v
, 1));
2633 int hfsize
= Int_val (Field (li_v
, 2));
2634 char *s
= String_val (ptr_v
);
2635 int hlmask
= Int_val (hlinks_v
);
2636 struct page
*page
= parse_pointer (__func__
, s
);
2638 if (!page
->fzpage
) {
2639 /* deal with loadpage failed pages */
2643 ensureannots (page
);
2645 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2646 if (trylock (__func__
)) {
2651 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2652 noff
= page
->slinkcount
;
2654 if (page
->tgen
== state
.gen
) {
2655 showsel (page
, xoff
, yoff
);
2660 CAMLreturn (Val_int (noff
));
2663 CAMLprim value
ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2666 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2667 int xoff
= Int_val (xoff_v
);
2668 int yoff
= Int_val (yoff_v
);
2669 char *s
= String_val (ptr_v
);
2670 struct page
*page
= parse_pointer (__func__
, s
);
2672 drawprect (page
, xoff
, yoff
, rects_v
);
2673 CAMLreturn (Val_unit
);
2676 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2681 const fz_matrix
*tctm
;
2682 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2684 if (!page
->annots
) return NULL
;
2687 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
2688 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2691 tctm
= &fz_identity
;
2697 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2698 fz_invert_matrix (&ctm
, &ctm
);
2699 fz_transform_point (&p
, &ctm
);
2702 for (i
= 0; i
< page
->annotcount
; ++i
) {
2703 struct annot
*a
= &page
->annots
[i
];
2704 pdf_annot
*annot
= (pdf_annot
*) a
->annot
;
2705 if (p
.x
>= annot
->pagerect
.x0
&& p
.x
<= annot
->pagerect
.x1
) {
2706 if (p
.y
>= annot
->pagerect
.y0
&& p
.y
<= annot
->pagerect
.y1
) {
2715 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2719 fz_link
*link
, *links
;
2721 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2726 fz_invert_matrix (&ctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2727 fz_transform_point (&p
, &ctm
);
2729 for (link
= links
; link
; link
= link
->next
) {
2730 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2731 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2739 static void ensuretext (struct page
*page
)
2741 if (state
.gen
!= page
->tgen
) {
2743 page
->tgen
= state
.gen
;
2749 page
->text
= fz_new_stext_page (state
.ctx
);
2750 page
->sheet
= fz_new_stext_sheet (state
.ctx
);
2751 tdev
= fz_new_stext_device (state
.ctx
, page
->sheet
, page
->text
);
2752 ctm
= pagectm (page
);
2753 fz_run_display_list (state
.ctx
, page
->dlist
,
2754 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2755 qsort (page
->text
->blocks
, page
->text
->len
,
2756 sizeof (*page
->text
->blocks
), compareblocks
);
2757 fz_drop_device (state
.ctx
, tdev
);
2761 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2763 CAMLparam2 (start_page_v
, dir_v
);
2765 int i
, dir
= Int_val (dir_v
);
2766 int start_page
= Int_val (start_page_v
);
2767 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2768 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2771 ret_v
= Val_int (0);
2773 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2778 pdf_page
*page
= NULL
;
2780 fz_try (state
.ctx
) {
2781 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2782 found
= !!page
->links
|| !!page
->annots
;
2784 fz_catch (state
.ctx
) {
2788 fz_drop_page (state
.ctx
, &page
->super
);
2792 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2793 found
= !!fz_load_links (state
.ctx
, page
);
2794 fz_drop_page (state
.ctx
, page
);
2798 ret_v
= caml_alloc_small (1, 1);
2799 Field (ret_v
, 0) = Val_int (i
);
2808 enum { dir_first
, dir_last
};
2809 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2811 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2813 CAMLparam2 (ptr_v
, dir_v
);
2814 CAMLlocal2 (ret_v
, pos_v
);
2816 int dirtag
, i
, slinkindex
;
2817 struct slink
*found
= NULL
,*slink
;
2818 char *s
= String_val (ptr_v
);
2820 page
= parse_pointer (__func__
, s
);
2821 ret_v
= Val_int (0);
2822 /* This is scary we are not taking locks here ensureslinks does
2823 not modify state and given that we obtained the page it can not
2824 disappear under us either */
2826 ensureslinks (page
);
2828 if (Is_block (dir_v
)) {
2829 dirtag
= Tag_val (dir_v
);
2831 case dir_first_visible
:
2833 int x0
, y0
, dir
, first_index
, last_index
;
2835 pos_v
= Field (dir_v
, 0);
2836 x0
= Int_val (Field (pos_v
, 0));
2837 y0
= Int_val (Field (pos_v
, 1));
2838 dir
= Int_val (Field (pos_v
, 2));
2843 last_index
= page
->slinkcount
;
2846 first_index
= page
->slinkcount
- 1;
2850 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2851 slink
= &page
->slinks
[i
];
2852 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2861 slinkindex
= Int_val (Field (dir_v
, 0));
2862 found
= &page
->slinks
[slinkindex
];
2863 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2864 slink
= &page
->slinks
[i
];
2865 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2873 slinkindex
= Int_val (Field (dir_v
, 0));
2874 found
= &page
->slinks
[slinkindex
];
2875 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2876 slink
= &page
->slinks
[i
];
2877 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2885 slinkindex
= Int_val (Field (dir_v
, 0));
2886 found
= &page
->slinks
[slinkindex
];
2887 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2888 slink
= &page
->slinks
[i
];
2889 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2897 slinkindex
= Int_val (Field (dir_v
, 0));
2898 found
= &page
->slinks
[slinkindex
];
2899 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2900 slink
= &page
->slinks
[i
];
2901 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2910 dirtag
= Int_val (dir_v
);
2913 found
= page
->slinks
;
2918 found
= page
->slinks
+ (page
->slinkcount
- 1);
2924 ret_v
= caml_alloc_small (2, 1);
2925 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2932 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
2933 unamed
, uremote
, uremotedest
, uannot
};
2939 switch (link->dest.kind) { \
2940 case FZ_LINK_GOTO: \
2944 pageno = link->dest.ld.gotor.page; \
2948 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2949 p.y = link->dest.ld.gotor.lt.y; \
2950 fz_transform_point (&p, &pdim->lctm); \
2951 if (p.y < 0) p.y = 0; \
2953 tup_v = caml_alloc_tuple (2); \
2954 ret_v = caml_alloc_small (1, ugoto); \
2955 Field (tup_v, 0) = Val_int (pageno); \
2956 Field (tup_v, 1) = Val_int (p.y); \
2957 Field (ret_v, 0) = tup_v; \
2962 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2963 ret_v = caml_alloc_small (1, uuri); \
2964 Field (ret_v, 0) = str_v; \
2967 case FZ_LINK_LAUNCH: \
2968 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2969 ret_v = caml_alloc_small (1, ulaunch); \
2970 Field (ret_v, 0) = str_v; \
2973 case FZ_LINK_NAMED: \
2974 str_v = caml_copy_string (link->dest.ld.named.named); \
2975 ret_v = caml_alloc_small (1, unamed); \
2976 Field (ret_v, 0) = str_v; \
2979 case FZ_LINK_GOTOR: \
2983 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2984 pageno = link->dest.ld.gotor.page; \
2985 if (pageno == -1) { \
2986 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2987 rty = uremotedest; \
2990 gr_v = Val_int (pageno); \
2993 tup_v = caml_alloc_tuple (2); \
2994 ret_v = caml_alloc_small (1, rty); \
2995 Field (tup_v, 0) = str_v; \
2996 Field (tup_v, 1) = gr_v; \
2997 Field (ret_v, 0) = tup_v; \
3005 snprintf (buf, sizeof (buf), \
3006 "unhandled link kind %d", link->dest.kind); \
3007 str_v = caml_copy_string (buf); \
3008 ret_v = caml_alloc_small (1, uunexpected); \
3009 Field (ret_v, 0) = str_v; \
3015 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3017 CAMLparam2 (ptr_v
, n_v
);
3018 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3021 struct pagedim
*pdim
;
3022 char *s
= String_val (ptr_v
);
3023 struct slink
*slink
;
3025 /* See ml_findlink for caveat */
3027 ret_v
= Val_int (0);
3028 page
= parse_pointer (__func__
, s
);
3029 ensureslinks (page
);
3030 pdim
= &state
.pagedims
[page
->pdimno
];
3031 slink
= &page
->slinks
[Int_val (n_v
)];
3032 if (slink
->tag
== SLINK
) {
3033 link
= slink
->u
.link
;
3037 ret_v
= caml_alloc_small (1, uannot
);
3038 tup_v
= caml_alloc_tuple (2);
3039 Field (ret_v
, 0) = tup_v
;
3040 Field (tup_v
, 0) = ptr_v
;
3041 Field (tup_v
, 1) = n_v
;
3047 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
3049 CAMLparam2 (ptr_v
, n_v
);
3050 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3052 char *s
= String_val (ptr_v
);
3054 struct slink
*slink
;
3056 page
= parse_pointer (__func__
, s
);
3057 slink
= &page
->slinks
[Int_val (n_v
)];
3058 CAMLreturn (caml_copy_string (
3059 pdf_annot_contents (state
.ctx
, pdf
,
3060 (pdf_annot
*) slink
->u
.annot
)));
3063 CAMLreturn (caml_copy_string (""));
3067 CAMLprim value
ml_getlinkcount (value ptr_v
)
3071 char *s
= String_val (ptr_v
);
3073 page
= parse_pointer (__func__
, s
);
3074 CAMLreturn (Val_int (page
->slinkcount
));
3077 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3079 CAMLparam2 (ptr_v
, n_v
);
3082 struct slink
*slink
;
3083 char *s
= String_val (ptr_v
);
3084 /* See ml_findlink for caveat */
3086 page
= parse_pointer (__func__
, s
);
3087 ret_v
= caml_alloc_tuple (4);
3088 ensureslinks (page
);
3090 slink
= &page
->slinks
[Int_val (n_v
)];
3091 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3092 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3093 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3094 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3099 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3101 CAMLparam3 (ptr_v
, x_v
, y_v
);
3102 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3104 struct annot
*annot
;
3106 char *ptr
= String_val (ptr_v
);
3107 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3108 struct pagedim
*pdim
;
3110 ret_v
= Val_int (0);
3111 if (trylock (__func__
)) {
3115 page
= parse_pointer (__func__
, ptr
);
3116 pdim
= &state
.pagedims
[page
->pdimno
];
3117 x
+= pdim
->bounds
.x0
;
3118 y
+= pdim
->bounds
.y0
;
3121 annot
= getannot (page
, x
, y
);
3125 ensureslinks (page
);
3126 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3127 if (page
->slinks
[i
].tag
== SANNOT
3128 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3133 ret_v
= caml_alloc_small (1, uannot
);
3134 tup_v
= caml_alloc_tuple (2);
3135 Field (ret_v
, 0) = tup_v
;
3136 Field (tup_v
, 0) = ptr_v
;
3137 Field (tup_v
, 1) = Val_int (n
);
3142 link
= getlink (page
, x
, y
);
3148 fz_page_block
*pageb
;
3149 fz_stext_block
*block
;
3152 for (pageb
= page
->text
->blocks
;
3153 pageb
< page
->text
->blocks
+ page
->text
->len
;
3155 fz_stext_line
*line
;
3156 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3157 block
= pageb
->u
.text
;
3160 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3163 for (line
= block
->lines
;
3164 line
< block
->lines
+ block
->len
;
3166 fz_stext_span
*span
;
3169 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3172 for (span
= line
->first_span
; span
; span
= span
->next
) {
3176 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3179 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3181 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3184 if (x
>= b
->x0
&& x
<= b
->x1
3185 && y
>= b
->y0
&& y
<= b
->y1
) {
3186 fz_stext_style
*style
= span
->text
->style
;
3190 : "Span has no font name"
3192 FT_FaceRec
*face
= style
->font
->ft_face
;
3193 if (face
&& face
->family_name
) {
3195 char *n1
= face
->family_name
;
3196 size_t l1
= strlen (n1
);
3197 size_t l2
= strlen (n2
);
3199 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3200 s
= malloc (l1
+ l2
+ 2);
3204 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3205 str_v
= caml_copy_string (s
);
3210 if (str_v
== Val_unit
) {
3211 str_v
= caml_copy_string (n2
);
3213 ret_v
= caml_alloc_small (1, utext
);
3214 Field (ret_v
, 0) = str_v
;
3229 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3231 static int uninteresting (int c
)
3233 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3237 CAMLprim value
ml_clearmark (value ptr_v
)
3240 char *s
= String_val (ptr_v
);
3243 if (trylock (__func__
)) {
3247 page
= parse_pointer (__func__
, s
);
3248 page
->fmark
.span
= NULL
;
3249 page
->lmark
.span
= NULL
;
3255 CAMLreturn (Val_unit
);
3258 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3260 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3264 fz_stext_line
*line
;
3265 fz_page_block
*pageb
;
3266 fz_stext_block
*block
;
3267 struct pagedim
*pdim
;
3268 int mark
= Int_val (mark_v
);
3269 char *s
= String_val (ptr_v
);
3270 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3272 ret_v
= Val_bool (0);
3273 if (trylock (__func__
)) {
3277 page
= parse_pointer (__func__
, s
);
3278 pdim
= &state
.pagedims
[page
->pdimno
];
3282 if (mark
== mark_page
) {
3284 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3286 for (i
= 0; i
< page
->text
->len
; ++i
) {
3287 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3288 pb1
= &page
->text
->blocks
[i
];
3292 if (!pb1
) goto unlock
;
3294 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3295 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3296 pb2
= &page
->text
->blocks
[i
];
3300 if (!pb2
) goto unlock
;
3302 block
= pb1
->u
.text
;
3305 page
->fmark
.span
= block
->lines
->first_span
;
3307 block
= pb2
->u
.text
;
3308 line
= &block
->lines
[block
->len
- 1];
3309 page
->lmark
.i
= line
->last_span
->len
- 1;
3310 page
->lmark
.span
= line
->last_span
;
3311 ret_v
= Val_bool (1);
3315 x
+= pdim
->bounds
.x0
;
3316 y
+= pdim
->bounds
.y0
;
3318 for (pageb
= page
->text
->blocks
;
3319 pageb
< page
->text
->blocks
+ page
->text
->len
;
3321 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3322 block
= pageb
->u
.text
;
3325 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3328 if (mark
== mark_block
) {
3330 page
->fmark
.span
= block
->lines
->first_span
;
3332 line
= &block
->lines
[block
->len
- 1];
3333 page
->lmark
.i
= line
->last_span
->len
- 1;
3334 page
->lmark
.span
= line
->last_span
;
3335 ret_v
= Val_bool (1);
3339 for (line
= block
->lines
;
3340 line
< block
->lines
+ block
->len
;
3342 fz_stext_span
*span
;
3345 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3348 if (mark
== mark_line
) {
3350 page
->fmark
.span
= line
->first_span
;
3352 page
->lmark
.i
= line
->last_span
->len
- 1;
3353 page
->lmark
.span
= line
->last_span
;
3354 ret_v
= Val_bool (1);
3358 for (span
= line
->first_span
; span
; span
= span
->next
) {
3362 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3365 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3367 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3370 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3372 int charnum2
, charnum3
= -1, charnum4
= -1;
3374 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3376 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3377 if (uninteresting (span
->text
[charnum2
].c
)) {
3378 charnum3
= charnum2
+ 1;
3382 if (charnum3
== -1) charnum3
= 0;
3385 for (charnum2
= charnum
+ 1;
3386 charnum2
< span
->len
;
3388 if (uninteresting (span
->text
[charnum2
].c
)) break;
3389 charnum4
= charnum2
;
3392 page
->fmark
.i
= charnum3
;
3393 page
->fmark
.span
= span
;
3395 page
->lmark
.i
= charnum4
;
3396 page
->lmark
.span
= span
;
3397 ret_v
= Val_bool (1);
3405 if (!Bool_val (ret_v
)) {
3406 page
->fmark
.span
= NULL
;
3407 page
->lmark
.span
= NULL
;
3417 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3419 CAMLparam3 (ptr_v
, x_v
, y_v
);
3420 CAMLlocal2 (ret_v
, res_v
);
3423 fz_page_block
*pageb
;
3424 struct pagedim
*pdim
;
3425 char *s
= String_val (ptr_v
);
3426 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3428 ret_v
= Val_int (0);
3429 if (trylock (__func__
)) {
3433 page
= parse_pointer (__func__
, s
);
3434 pdim
= &state
.pagedims
[page
->pdimno
];
3435 x
+= pdim
->bounds
.x0
;
3436 y
+= pdim
->bounds
.y0
;
3440 for (pageb
= page
->text
->blocks
;
3441 pageb
< page
->text
->blocks
+ page
->text
->len
;
3443 switch (pageb
->type
) {
3444 case FZ_PAGE_BLOCK_TEXT
:
3445 b
= &pageb
->u
.text
->bbox
;
3448 case FZ_PAGE_BLOCK_IMAGE
:
3449 b
= &pageb
->u
.image
->bbox
;
3456 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3461 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3462 ret_v
= caml_alloc_small (1, 1);
3463 Store_double_field (res_v
, 0, b
->x0
);
3464 Store_double_field (res_v
, 1, b
->x1
);
3465 Store_double_field (res_v
, 2, b
->y0
);
3466 Store_double_field (res_v
, 3, b
->y1
);
3467 Field (ret_v
, 0) = res_v
;
3475 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3477 CAMLparam2 (ptr_v
, rect_v
);
3480 struct pagedim
*pdim
;
3481 char *s
= String_val (ptr_v
);
3482 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3483 fz_stext_line
*line
;
3484 fz_page_block
*pageb
;
3485 fz_stext_block
*block
;
3486 fz_stext_span
*span
, *fspan
, *lspan
;
3488 if (trylock (__func__
)) {
3492 page
= parse_pointer (__func__
, s
);
3495 pdim
= &state
.pagedims
[page
->pdimno
];
3496 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3497 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3498 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3499 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3510 fspan
= page
->fmark
.span
;
3513 lspan
= page
->lmark
.span
;
3515 for (pageb
= page
->text
->blocks
;
3516 pageb
< page
->text
->blocks
+ page
->text
->len
;
3518 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3519 block
= pageb
->u
.text
;
3520 for (line
= block
->lines
;
3521 line
< block
->lines
+ block
->len
;
3524 for (span
= line
->first_span
; span
; span
= span
->next
) {
3525 for (i
= 0; i
< span
->len
; ++i
) {
3526 fz_stext_char_bbox (state
.ctx
, &b
, span
, i
);
3528 if (x0
>= b
.x0
&& x0
<= b
.x1
3529 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3533 if (x1
>= b
.x0
&& x1
<= b
.x1
3534 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3542 if (x1
< x0
&& fspan
== lspan
) {
3554 page
->fmark
.span
= fspan
;
3557 page
->lmark
.span
= lspan
;
3562 CAMLreturn (Val_unit
);
3565 static int UNUSED_ATTR
pipespan (FILE *f
, fz_stext_span
*span
, int a
, int b
)
3570 for (i
= a
; i
<= b
; ++i
) {
3571 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3572 ret
= fwrite (buf
, len
, 1, f
);
3575 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3576 len
, ret
, strerror (errno
));
3584 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3586 caml_failwith ("ml_popen not implemented under Cygwin");
3589 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3591 CAMLparam2 (command_v
, fds_v
);
3592 CAMLlocal2 (l_v
, tup_v
);
3596 value earg_v
= Nothing
;
3597 posix_spawnattr_t attr
;
3598 posix_spawn_file_actions_t fa
;
3599 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3601 argv
[2] = String_val (command_v
);
3603 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3604 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3607 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3608 msg
= "posix_spawnattr_init";
3612 #ifdef POSIX_SPAWN_USEVFORK
3613 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3614 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3619 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3622 tup_v
= Field (l_v
, 0);
3623 fd1
= Int_val (Field (tup_v
, 0));
3624 fd2
= Int_val (Field (tup_v
, 1));
3626 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3627 msg
= "posix_spawn_file_actions_addclose";
3633 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3634 msg
= "posix_spawn_file_actions_adddup2";
3641 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3642 msg
= "posix_spawn";
3647 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3648 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3652 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3653 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3658 unix_error (ret
, msg
, earg_v
);
3660 CAMLreturn (Val_int (pid
));
3664 CAMLprim value
ml_hassel (value ptr_v
)
3669 char *s
= String_val (ptr_v
);
3671 ret_v
= Val_bool (0);
3672 if (trylock (__func__
)) {
3676 page
= parse_pointer (__func__
, s
);
3677 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3683 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3685 CAMLparam2 (fd_v
, ptr_v
);
3689 fz_stext_line
*line
;
3690 fz_page_block
*pageb
;
3691 fz_stext_block
*block
;
3692 int fd
= Int_val (fd_v
);
3693 char *s
= String_val (ptr_v
);
3695 if (trylock (__func__
)) {
3699 page
= parse_pointer (__func__
, s
);
3701 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3702 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3706 f
= fdopen (fd
, "w");
3708 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3709 fd
, strerror (errno
));
3713 for (pageb
= page
->text
->blocks
;
3714 pageb
< page
->text
->blocks
+ page
->text
->len
;
3716 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3717 block
= pageb
->u
.text
;
3718 for (line
= block
->lines
;
3719 line
< block
->lines
+ block
->len
;
3721 fz_stext_span
*span
;
3723 for (span
= line
->first_span
; span
; span
= span
->next
) {
3726 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3727 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3728 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3731 if (pipespan (f
, span
, a
, b
)) {
3734 if (span
== page
->lmark
.span
) {
3737 if (span
== line
->last_span
) {
3738 if (putc ('\n', f
) == EOF
) {
3740 "failed break line on sel pipe: %s\n",
3751 int ret
= fclose (f
);
3754 if (errno
!= ECHILD
) {
3755 fprintf (stderr
, "failed to close sel pipe: %s\n",
3766 fprintf (stderr
, "failed to close sel pipe: %s\n",
3770 CAMLreturn (Val_unit
);
3773 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3775 CAMLparam1 (pagedimno_v
);
3777 int pagedimno
= Int_val (pagedimno_v
);
3780 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3781 if (trylock (__func__
)) {
3782 box
= fz_empty_rect
;
3785 box
= state
.pagedims
[pagedimno
].mediabox
;
3789 Store_double_field (ret_v
, 0, box
.x0
);
3790 Store_double_field (ret_v
, 1, box
.x1
);
3791 Store_double_field (ret_v
, 2, box
.y0
);
3792 Store_double_field (ret_v
, 3, box
.y1
);
3797 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3798 value dw_v
, value cols_v
)
3800 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3806 double winw
= Int_val (winw_v
);
3807 double winh
= Int_val (winh_v
);
3808 double dw
= Int_val (dw_v
);
3809 double cols
= Int_val (cols_v
);
3810 double pw
= 1.0, ph
= 1.0;
3812 if (trylock (__func__
)) {
3816 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3817 double w
= p
->pagebox
.x1
/ cols
;
3818 double h
= p
->pagebox
.y1
;
3822 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3824 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3827 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3830 ret_v
= caml_copy_double (zoom
);
3834 CAMLprim value
ml_getmaxw (value unit_v
)
3836 CAMLparam1 (unit_v
);
3842 if (trylock (__func__
)) {
3846 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3847 double w
= p
->pagebox
.x1
;
3848 maxw
= MAX (maxw
, w
);
3853 ret_v
= caml_copy_double (maxw
);
3857 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3859 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3861 int pt
= Int_val(pt_v
);
3862 int x
= Int_val (x_v
);
3863 int y
= Int_val (y_v
);
3866 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3867 ret_v
= caml_copy_double (w
);
3871 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3873 CAMLparam2 (pt_v
, string_v
);
3875 int pt
= Int_val (pt_v
);
3878 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3879 ret_v
= caml_copy_double (w
);
3883 CAMLprim value
ml_getpagebox (value opaque_v
)
3885 CAMLparam1 (opaque_v
);
3891 char *s
= String_val (opaque_v
);
3892 struct page
*page
= parse_pointer (__func__
, s
);
3894 ret_v
= caml_alloc_tuple (4);
3895 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3896 dev
->hints
|= FZ_IGNORE_SHADE
;
3898 ctm
= pagectm (page
);
3899 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3901 fz_drop_device (state
.ctx
, dev
);
3902 fz_round_rect (&bbox
, &rect
);
3903 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3904 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3905 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3906 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3911 CAMLprim value
ml_setaalevel (value level_v
)
3913 CAMLparam1 (level_v
);
3915 state
.aalevel
= Int_val (level_v
);
3916 CAMLreturn (Val_unit
);
3919 #pragma GCC diagnostic push
3920 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3921 #include <X11/Xlib.h>
3922 #include <X11/cursorfont.h>
3923 #pragma GCC diagnostic pop
3926 #include <EGL/egl.h>
3931 static const int shapes
[] = {
3932 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3935 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3948 XVisualInfo
*visual
;
3949 Cursor curs
[CURS_COUNT
];
3953 static VisualID
initvisual (void)
3955 /* On this system with: `Haswell-ULT Integrated Graphics
3956 Controller' and Mesa 11.0.6; perf stat reports [1] that when
3957 using glX chosen visual and auto scrolling some document in
3958 fullscreen the power/energy-gpu is more than 1 joule bigger
3959 than when using hand picked visual that stands alone in glxinfo
3960 output: it's dead last in the list and it's the only one with
3961 `visual dep' (sic) of 32
3963 No clue what's going on here...
3965 [1] perf stat -a -I 1200 -e "power/energy-gpu/"
3971 info
.class = TrueColor
;
3972 glx
.visual
= XGetVisualInfo (glx
.dpy
, VisualDepthMask
| VisualClassMask
,
3974 if (!ret
|| !glx
.visual
) {
3975 XCloseDisplay (glx
.dpy
);
3976 caml_failwith ("XGetVisualInfo");
3978 return glx
.visual
->visualid
;
3982 static void initcurs (void)
3984 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3985 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3989 CAMLprim
void ml_setbgcol (value color_v
)
3991 CAMLparam1 (color_v
);
3992 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3997 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3999 CAMLparam3 (display_v
, wid_v
, screen_v
);
4003 EGLint attribs
[] = {
4005 EGL_NATIVE_VISUAL_ID
, 0,
4009 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
4010 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
4015 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4017 caml_failwith ("XOpenDisplay");
4020 eglBindAPI (EGL_OPENGL_API
);
4022 glx
.edpy
= eglGetDisplay (glx
.dpy
);
4023 if (glx
.dpy
== EGL_NO_DISPLAY
) {
4024 caml_failwith ("eglGetDisplay");
4027 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
4028 caml_failwith ("eglInitialize");
4032 attribs
[1] = visid
= initvisual ();
4035 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
4037 caml_failwith ("eglChooseConfig");
4042 if (!eglGetConfigAttrib (glx
.edpy
, glx
.conf
,
4043 EGL_NATIVE_VISUAL_ID
, &visid
)) {
4044 caml_failwith ("eglGetConfigAttrib");
4049 glx
.wid
= Int_val (wid_v
);
4050 CAMLreturn (Val_int (visid
));
4053 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4055 CAMLparam1 (unit_v
);
4057 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
4059 caml_failwith ("eglCreateContext");
4062 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
4064 if (glx
.win
== EGL_NO_SURFACE
) {
4065 caml_failwith ("eglCreateWindowSurface");
4069 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
4071 caml_failwith ("eglMakeCurrent");
4073 CAMLreturn (Val_unit
);
4076 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4078 CAMLparam3 (display_v
, wid_v
, screen_v
);
4080 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4082 caml_failwith ("XOpenDisplay");
4088 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4089 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4091 XCloseDisplay (glx
.dpy
);
4092 caml_failwith ("glXChooseVisual");
4097 glx
.wid
= Int_val (wid_v
);
4098 CAMLreturn (Val_int (glx
.visual
->visualid
));
4101 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4103 CAMLparam1 (unit_v
);
4105 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4107 caml_failwith ("glXCreateContext");
4113 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4114 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4116 caml_failwith ("glXMakeCurrent");
4118 CAMLreturn (Val_unit
);
4122 CAMLprim value
ml_setcursor (value cursor_v
)
4124 CAMLparam1 (cursor_v
);
4125 size_t cursn
= Int_val (cursor_v
);
4127 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
4128 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
4130 CAMLreturn (Val_unit
);
4133 CAMLprim value
ml_swapb (value unit_v
)
4135 CAMLparam1 (unit_v
);
4137 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
4138 caml_failwith ("eglSwapBuffers");
4141 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4143 CAMLreturn (Val_unit
);
4146 #include "keysym2ucs.c"
4148 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4150 CAMLparam1 (keysym_v
);
4152 KeySym keysym
= Int_val (keysym_v
);
4157 rune
= keysym2ucs (keysym
);
4158 len
= fz_runetochar (buf
, rune
);
4160 str_v
= caml_copy_string (buf
);
4164 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4166 CAMLprim value
ml_platform (value unit_v
)
4168 CAMLparam1 (unit_v
);
4169 CAMLlocal2 (tup_v
, arr_v
);
4170 int platid
= piunknown
;
4173 #if defined __linux__
4175 #elif defined __CYGWIN__
4177 #elif defined __DragonFly__ || defined __FreeBSD__
4178 || defined __OpenBSD__
|| defined __NetBSD__
4180 #elif defined __sun__
4182 #elif defined __APPLE__
4185 if (uname (&buf
)) err (1, "uname");
4187 tup_v
= caml_alloc_tuple (2);
4189 char const *sar
[] = {
4196 arr_v
= caml_copy_string_array (sar
);
4198 Field (tup_v
, 0) = Val_int (platid
);
4199 Field (tup_v
, 1) = arr_v
;
4203 CAMLprim value
ml_cloexec (value fd_v
)
4206 int fd
= Int_val (fd_v
);
4208 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4209 uerror ("fcntl", Nothing
);
4211 CAMLreturn (Val_unit
);
4214 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4216 CAMLparam2 (w_v
, h_v
);
4219 int w
= Int_val (w_v
);
4220 int h
= Int_val (h_v
);
4221 int cs
= Int_val (cs_v
);
4223 if (state
.bo_usable
) {
4224 pbo
= calloc (sizeof (*pbo
), 1);
4226 err (1, "calloc pbo");
4238 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
4241 state
.glGenBuffersARB (1, &pbo
->id
);
4242 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4243 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4244 NULL
, GL_STREAM_DRAW
);
4245 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4247 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4249 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4250 state
.glDeleteBuffersARB (1, &pbo
->id
);
4252 ret_v
= caml_copy_string ("0");
4258 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4260 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4264 err (1, "malloc %d bytes failed", res
+1);
4266 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4268 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4270 ret_v
= caml_copy_string (s
);
4275 ret_v
= caml_copy_string ("0");
4280 CAMLprim value
ml_freepbo (value s_v
)
4283 char *s
= String_val (s_v
);
4284 struct tile
*tile
= parse_pointer (__func__
, s
);
4287 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4289 tile
->pbo
->ptr
= NULL
;
4290 tile
->pbo
->size
= -1;
4292 CAMLreturn (Val_unit
);
4295 CAMLprim value
ml_unmappbo (value s_v
)
4298 char *s
= String_val (s_v
);
4299 struct tile
*tile
= parse_pointer (__func__
, s
);
4302 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4303 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4304 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4306 tile
->pbo
->ptr
= NULL
;
4307 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4309 CAMLreturn (Val_unit
);
4312 static void setuppbo (void)
4315 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
4317 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4319 state
.bo_usable
= GGPA (glBindBufferARB
)
4320 && GGPA (glUnmapBufferARB
)
4321 && GGPA (glMapBufferARB
)
4322 && GGPA (glBufferDataARB
)
4323 && GGPA (glGenBuffersARB
)
4324 && GGPA (glDeleteBuffersARB
);
4328 CAMLprim value
ml_bo_usable (value unit_v
)
4330 CAMLparam1 (unit_v
);
4331 CAMLreturn (Val_bool (state
.bo_usable
));
4334 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4336 CAMLparam3 (ptr_v
, x_v
, y_v
);
4337 CAMLlocal2 (ret_v
, tup_v
);
4339 char *s
= String_val (ptr_v
);
4340 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4341 struct pagedim
*pdim
;
4345 page
= parse_pointer (__func__
, s
);
4346 pdim
= &state
.pagedims
[page
->pdimno
];
4348 ret_v
= Val_int (0);
4349 if (trylock (__func__
)) {
4353 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4354 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
4355 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4360 p
.x
= x
+ pdim
->bounds
.x0
;
4361 p
.y
= y
+ pdim
->bounds
.y0
;
4363 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4364 fz_invert_matrix (&ctm
, &ctm
);
4365 fz_transform_point (&p
, &ctm
);
4367 tup_v
= caml_alloc_tuple (2);
4368 ret_v
= caml_alloc_small (1, 1);
4369 Field (tup_v
, 0) = Val_int (p
.x
);
4370 Field (tup_v
, 1) = Val_int (p
.y
);
4371 Field (ret_v
, 0) = tup_v
;
4378 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4379 value x_v
, value y_v
)
4381 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4384 char *s
= String_val (ptr_v
);
4385 int pageno
= Int_val (pageno_v
);
4386 int pdimno
= Int_val (pdimno_v
);
4387 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4388 struct pagedim
*pdim
;
4392 ret_v
= Val_int (0);
4396 page
= loadpage (pageno
, pdimno
);
4399 page
= parse_pointer (__func__
, s
);
4401 pdim
= &state
.pagedims
[pdimno
];
4403 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4404 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
4405 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4410 p
.x
= x
+ pdim
->bounds
.x0
;
4411 p
.y
= y
+ pdim
->bounds
.y0
;
4413 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4414 fz_transform_point (&p
, &ctm
);
4416 ret_v
= caml_alloc_tuple (2);
4417 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4418 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4427 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4430 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4431 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4437 char *s
= String_val (ptr_v
);
4439 page
= parse_pointer (__func__
, s
);
4440 annot
= pdf_create_annot (state
.ctx
, pdf
,
4441 (pdf_page
*) page
->fzpage
, FZ_ANNOT_TEXT
);
4442 p
.x
= Int_val (x_v
);
4443 p
.y
= Int_val (y_v
);
4444 pdf_set_annot_contents (state
.ctx
, pdf
, annot
, String_val (contents_v
));
4445 pdf_set_text_annot_position (state
.ctx
, pdf
, annot
, p
);
4448 CAMLreturn (Val_unit
);
4451 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4453 CAMLparam2 (ptr_v
, n_v
);
4454 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4458 char *s
= String_val (ptr_v
);
4459 struct slink
*slink
;
4461 page
= parse_pointer (__func__
, s
);
4462 slink
= &page
->slinks
[Int_val (n_v
)];
4463 pdf_delete_annot (state
.ctx
, pdf
,
4464 (pdf_page
*) page
->fzpage
,
4465 (pdf_annot
*) slink
->u
.annot
);
4468 CAMLreturn (Val_unit
);
4471 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4473 CAMLparam3 (ptr_v
, n_v
, str_v
);
4474 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4478 char *s
= String_val (ptr_v
);
4479 struct slink
*slink
;
4481 page
= parse_pointer (__func__
, s
);
4482 slink
= &page
->slinks
[Int_val (n_v
)];
4483 pdf_set_annot_contents (state
.ctx
, pdf
, (pdf_annot
*) slink
->u
.annot
,
4484 String_val (str_v
));
4487 CAMLreturn (Val_unit
);
4490 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4492 CAMLparam1 (unit_v
);
4493 CAMLreturn (Val_bool (state
.dirty
));
4496 CAMLprim value
ml_savedoc (value path_v
)
4498 CAMLparam1 (path_v
);
4499 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4502 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4504 CAMLreturn (Val_unit
);
4507 static void makestippletex (void)
4509 const char pixels
[] = "\xff\xff\0\0";
4510 glGenTextures (1, &state
.stid
);
4511 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4512 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4513 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4526 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4528 return caml_copy_string (FZ_VERSION
);
4531 #ifdef USE_FONTCONFIG
4537 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4541 int UNUSED_ATTR needs_exact_metrics
)
4548 FcPattern
*pat
, *pat1
;
4550 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4551 name
, bold
, italic
, needs_exact_metrics
);
4554 fc
.config
= FcInitLoadConfigAndFonts ();
4556 lprintf ("FcInitLoadConfigAndFonts failed\n");
4560 if (!fc
.config
) return NULL
;
4562 size
= strlen (name
);
4563 if (bold
) size
+= sizeof (":bold") - 1;
4564 if (italic
) size
+= sizeof (":italic") - 1;
4567 buf
= malloc (size
);
4569 err (1, "malloc %zu failed", size
);
4573 if (bold
&& italic
) {
4574 strcat (buf
, ":bold:italic");
4577 if (bold
) strcat (buf
, ":bold");
4578 if (italic
) strcat (buf
, ":italic");
4580 for (i
= 0; i
< size
; ++i
) {
4581 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4584 lprintf ("fcbuf=%s\n", buf
);
4585 pat
= FcNameParse ((FcChar8
*) buf
);
4587 printd ("emsg FcNameParse failed\n");
4592 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4593 printd ("emsg FcConfigSubstitute failed\n");
4597 FcDefaultSubstitute (pat
);
4599 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4601 printd ("emsg FcFontMatch failed\n");
4602 FcPatternDestroy (pat
);
4607 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4608 printd ("emsg FcPatternGetString failed\n");
4609 FcPatternDestroy (pat
);
4610 FcPatternDestroy (pat1
);
4616 printd ("emsg name=%s path=%s\n", name
, path
);
4618 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4619 FcPatternDestroy (pat
);
4620 FcPatternDestroy (pat1
);
4626 CAMLprim value
ml_init (value csock_v
, value params_v
)
4628 CAMLparam2 (csock_v
, params_v
);
4629 CAMLlocal2 (trim_v
, fuzz_v
);
4637 state
.csock
= Int_val (csock_v
);
4638 state
.rotate
= Int_val (Field (params_v
, 0));
4639 state
.fitmodel
= Int_val (Field (params_v
, 1));
4640 trim_v
= Field (params_v
, 2);
4641 texcount
= Int_val (Field (params_v
, 3));
4642 state
.sliceheight
= Int_val (Field (params_v
, 4));
4643 mustoresize
= Int_val (Field (params_v
, 5));
4644 colorspace
= Int_val (Field (params_v
, 6));
4645 fontpath
= String_val (Field (params_v
, 7));
4647 if (caml_string_length (Field (params_v
, 8)) > 0) {
4648 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4650 if (!state
.trimcachepath
) {
4651 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4655 haspboext
= Bool_val (Field (params_v
, 9));
4657 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4658 fz_register_document_handlers (state
.ctx
);
4660 #ifdef USE_FONTCONFIG
4661 if (Bool_val (Field (params_v
, 10))) {
4662 fz_install_load_system_font_funcs (
4663 state
.ctx
, fc_load_system_font_func
, NULL
4668 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4669 fuzz_v
= Field (trim_v
, 1);
4670 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4671 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4672 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4673 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4675 set_tex_params (colorspace
);
4678 #ifndef USE_FONTCONFIG
4679 state
.face
= load_font (fontpath
);
4683 char *buf
= fontpath
;
4684 FcPattern
*pat
, *pat1
;
4687 fc
.config
= FcInitLoadConfigAndFonts ();
4689 errx (1, "FcInitLoadConfigAndFonts failed");
4692 pat
= FcNameParse ((FcChar8
*) buf
);
4694 errx (1, "FcNameParse failed");
4697 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4698 errx (1, "FcConfigSubstitute failed");
4700 FcDefaultSubstitute (pat
);
4702 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4704 errx (1, "FcFontMatch failed");
4707 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4708 errx (1, "FcPatternGetString failed");
4711 state
.face
= load_font ((char *) path
);
4712 FcPatternDestroy (pat
);
4713 FcPatternDestroy (pat1
);
4718 const char *data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0,
4720 state
.face
= load_builtin_font (data
, len
);
4722 if (!state
.face
) _exit (1);
4724 realloctexts (texcount
);
4732 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4734 errx (1, "pthread_create: %s", strerror (ret
));
4737 CAMLreturn (Val_unit
);