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 const fz_matrix
*tctm
;
2720 fz_link
*link
, *links
;
2722 tctm
= &fz_identity
;
2723 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2728 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2729 fz_invert_matrix (&ctm
, &ctm
);
2730 fz_transform_point (&p
, &ctm
);
2732 for (link
= links
; link
; link
= link
->next
) {
2733 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2734 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2742 static void ensuretext (struct page
*page
)
2744 if (state
.gen
!= page
->tgen
) {
2746 page
->tgen
= state
.gen
;
2752 page
->text
= fz_new_stext_page (state
.ctx
);
2753 page
->sheet
= fz_new_stext_sheet (state
.ctx
);
2754 tdev
= fz_new_stext_device (state
.ctx
, page
->sheet
, page
->text
);
2755 ctm
= pagectm (page
);
2756 fz_begin_page (state
.ctx
, tdev
, &fz_infinite_rect
, &ctm
);
2757 fz_run_display_list (state
.ctx
, page
->dlist
,
2758 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2759 qsort (page
->text
->blocks
, page
->text
->len
,
2760 sizeof (*page
->text
->blocks
), compareblocks
);
2761 fz_end_page (state
.ctx
, tdev
);
2762 fz_drop_device (state
.ctx
, tdev
);
2766 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2768 CAMLparam2 (start_page_v
, dir_v
);
2770 int i
, dir
= Int_val (dir_v
);
2771 int start_page
= Int_val (start_page_v
);
2772 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2773 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2776 ret_v
= Val_int (0);
2778 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2783 pdf_page
*page
= NULL
;
2785 fz_try (state
.ctx
) {
2786 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2787 found
= !!page
->links
|| !!page
->annots
;
2789 fz_catch (state
.ctx
) {
2793 fz_drop_page (state
.ctx
, &page
->super
);
2797 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2798 found
= !!fz_load_links (state
.ctx
, page
);
2799 fz_drop_page (state
.ctx
, page
);
2803 ret_v
= caml_alloc_small (1, 1);
2804 Field (ret_v
, 0) = Val_int (i
);
2813 enum { dir_first
, dir_last
};
2814 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2816 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2818 CAMLparam2 (ptr_v
, dir_v
);
2819 CAMLlocal2 (ret_v
, pos_v
);
2821 int dirtag
, i
, slinkindex
;
2822 struct slink
*found
= NULL
,*slink
;
2823 char *s
= String_val (ptr_v
);
2825 page
= parse_pointer (__func__
, s
);
2826 ret_v
= Val_int (0);
2827 /* This is scary we are not taking locks here ensureslinks does
2828 not modify state and given that we obtained the page it can not
2829 disappear under us either */
2831 ensureslinks (page
);
2833 if (Is_block (dir_v
)) {
2834 dirtag
= Tag_val (dir_v
);
2836 case dir_first_visible
:
2838 int x0
, y0
, dir
, first_index
, last_index
;
2840 pos_v
= Field (dir_v
, 0);
2841 x0
= Int_val (Field (pos_v
, 0));
2842 y0
= Int_val (Field (pos_v
, 1));
2843 dir
= Int_val (Field (pos_v
, 2));
2848 last_index
= page
->slinkcount
;
2851 first_index
= page
->slinkcount
- 1;
2855 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2856 slink
= &page
->slinks
[i
];
2857 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2866 slinkindex
= Int_val (Field (dir_v
, 0));
2867 found
= &page
->slinks
[slinkindex
];
2868 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2869 slink
= &page
->slinks
[i
];
2870 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2878 slinkindex
= Int_val (Field (dir_v
, 0));
2879 found
= &page
->slinks
[slinkindex
];
2880 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2881 slink
= &page
->slinks
[i
];
2882 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2890 slinkindex
= Int_val (Field (dir_v
, 0));
2891 found
= &page
->slinks
[slinkindex
];
2892 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2893 slink
= &page
->slinks
[i
];
2894 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2902 slinkindex
= Int_val (Field (dir_v
, 0));
2903 found
= &page
->slinks
[slinkindex
];
2904 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2905 slink
= &page
->slinks
[i
];
2906 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2915 dirtag
= Int_val (dir_v
);
2918 found
= page
->slinks
;
2923 found
= page
->slinks
+ (page
->slinkcount
- 1);
2929 ret_v
= caml_alloc_small (2, 1);
2930 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2937 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
2938 unamed
, uremote
, uremotedest
, uannot
};
2944 switch (link->dest.kind) { \
2945 case FZ_LINK_GOTO: \
2949 pageno = link->dest.ld.gotor.page; \
2953 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2954 p.y = link->dest.ld.gotor.lt.y; \
2955 fz_transform_point (&p, &pdim->lctm); \
2956 if (p.y < 0) p.y = 0; \
2958 tup_v = caml_alloc_tuple (2); \
2959 ret_v = caml_alloc_small (1, ugoto); \
2960 Field (tup_v, 0) = Val_int (pageno); \
2961 Field (tup_v, 1) = Val_int (p.y); \
2962 Field (ret_v, 0) = tup_v; \
2967 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2968 ret_v = caml_alloc_small (1, uuri); \
2969 Field (ret_v, 0) = str_v; \
2972 case FZ_LINK_LAUNCH: \
2973 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2974 ret_v = caml_alloc_small (1, ulaunch); \
2975 Field (ret_v, 0) = str_v; \
2978 case FZ_LINK_NAMED: \
2979 str_v = caml_copy_string (link->dest.ld.named.named); \
2980 ret_v = caml_alloc_small (1, unamed); \
2981 Field (ret_v, 0) = str_v; \
2984 case FZ_LINK_GOTOR: \
2988 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2989 pageno = link->dest.ld.gotor.page; \
2990 if (pageno == -1) { \
2991 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2992 rty = uremotedest; \
2995 gr_v = Val_int (pageno); \
2998 tup_v = caml_alloc_tuple (2); \
2999 ret_v = caml_alloc_small (1, rty); \
3000 Field (tup_v, 0) = str_v; \
3001 Field (tup_v, 1) = gr_v; \
3002 Field (ret_v, 0) = tup_v; \
3010 snprintf (buf, sizeof (buf), \
3011 "unhandled link kind %d", link->dest.kind); \
3012 str_v = caml_copy_string (buf); \
3013 ret_v = caml_alloc_small (1, uunexpected); \
3014 Field (ret_v, 0) = str_v; \
3020 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3022 CAMLparam2 (ptr_v
, n_v
);
3023 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3026 struct pagedim
*pdim
;
3027 char *s
= String_val (ptr_v
);
3028 struct slink
*slink
;
3030 /* See ml_findlink for caveat */
3032 ret_v
= Val_int (0);
3033 page
= parse_pointer (__func__
, s
);
3034 ensureslinks (page
);
3035 pdim
= &state
.pagedims
[page
->pdimno
];
3036 slink
= &page
->slinks
[Int_val (n_v
)];
3037 if (slink
->tag
== SLINK
) {
3038 link
= slink
->u
.link
;
3042 ret_v
= caml_alloc_small (1, uannot
);
3043 tup_v
= caml_alloc_tuple (2);
3044 Field (ret_v
, 0) = tup_v
;
3045 Field (tup_v
, 0) = ptr_v
;
3046 Field (tup_v
, 1) = n_v
;
3052 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
3054 CAMLparam2 (ptr_v
, n_v
);
3055 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3057 char *s
= String_val (ptr_v
);
3059 struct slink
*slink
;
3061 page
= parse_pointer (__func__
, s
);
3062 slink
= &page
->slinks
[Int_val (n_v
)];
3063 CAMLreturn (caml_copy_string (
3064 pdf_annot_contents (state
.ctx
, pdf
,
3065 (pdf_annot
*) slink
->u
.annot
)));
3068 CAMLreturn (caml_copy_string (""));
3072 CAMLprim value
ml_getlinkcount (value ptr_v
)
3076 char *s
= String_val (ptr_v
);
3078 page
= parse_pointer (__func__
, s
);
3079 CAMLreturn (Val_int (page
->slinkcount
));
3082 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3084 CAMLparam2 (ptr_v
, n_v
);
3087 struct slink
*slink
;
3088 char *s
= String_val (ptr_v
);
3089 /* See ml_findlink for caveat */
3091 page
= parse_pointer (__func__
, s
);
3092 ret_v
= caml_alloc_tuple (4);
3093 ensureslinks (page
);
3095 slink
= &page
->slinks
[Int_val (n_v
)];
3096 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3097 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3098 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3099 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3104 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3106 CAMLparam3 (ptr_v
, x_v
, y_v
);
3107 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3109 struct annot
*annot
;
3111 char *ptr
= String_val (ptr_v
);
3112 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3113 struct pagedim
*pdim
;
3115 ret_v
= Val_int (0);
3116 if (trylock (__func__
)) {
3120 page
= parse_pointer (__func__
, ptr
);
3121 pdim
= &state
.pagedims
[page
->pdimno
];
3122 x
+= pdim
->bounds
.x0
;
3123 y
+= pdim
->bounds
.y0
;
3126 annot
= getannot (page
, x
, y
);
3130 ensureslinks (page
);
3131 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3132 if (page
->slinks
[i
].tag
== SANNOT
3133 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3138 ret_v
= caml_alloc_small (1, uannot
);
3139 tup_v
= caml_alloc_tuple (2);
3140 Field (ret_v
, 0) = tup_v
;
3141 Field (tup_v
, 0) = ptr_v
;
3142 Field (tup_v
, 1) = Val_int (n
);
3147 link
= getlink (page
, x
, y
);
3153 fz_page_block
*pageb
;
3154 fz_stext_block
*block
;
3157 for (pageb
= page
->text
->blocks
;
3158 pageb
< page
->text
->blocks
+ page
->text
->len
;
3160 fz_stext_line
*line
;
3161 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3162 block
= pageb
->u
.text
;
3165 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3168 for (line
= block
->lines
;
3169 line
< block
->lines
+ block
->len
;
3171 fz_stext_span
*span
;
3174 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3177 for (span
= line
->first_span
; span
; span
= span
->next
) {
3181 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3184 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3186 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3189 if (x
>= b
->x0
&& x
<= b
->x1
3190 && y
>= b
->y0
&& y
<= b
->y1
) {
3191 fz_stext_style
*style
= span
->text
->style
;
3195 : "Span has no font name"
3197 FT_FaceRec
*face
= style
->font
->ft_face
;
3198 if (face
&& face
->family_name
) {
3200 char *n1
= face
->family_name
;
3201 size_t l1
= strlen (n1
);
3202 size_t l2
= strlen (n2
);
3204 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3205 s
= malloc (l1
+ l2
+ 2);
3209 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3210 str_v
= caml_copy_string (s
);
3215 if (str_v
== Val_unit
) {
3216 str_v
= caml_copy_string (n2
);
3218 ret_v
= caml_alloc_small (1, utext
);
3219 Field (ret_v
, 0) = str_v
;
3234 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3236 static int uninteresting (int c
)
3238 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3242 CAMLprim value
ml_clearmark (value ptr_v
)
3245 char *s
= String_val (ptr_v
);
3248 if (trylock (__func__
)) {
3252 page
= parse_pointer (__func__
, s
);
3253 page
->fmark
.span
= NULL
;
3254 page
->lmark
.span
= NULL
;
3260 CAMLreturn (Val_unit
);
3263 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3265 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3269 fz_stext_line
*line
;
3270 fz_page_block
*pageb
;
3271 fz_stext_block
*block
;
3272 struct pagedim
*pdim
;
3273 int mark
= Int_val (mark_v
);
3274 char *s
= String_val (ptr_v
);
3275 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3277 ret_v
= Val_bool (0);
3278 if (trylock (__func__
)) {
3282 page
= parse_pointer (__func__
, s
);
3283 pdim
= &state
.pagedims
[page
->pdimno
];
3287 if (mark
== mark_page
) {
3289 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3291 for (i
= 0; i
< page
->text
->len
; ++i
) {
3292 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3293 pb1
= &page
->text
->blocks
[i
];
3297 if (!pb1
) goto unlock
;
3299 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3300 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3301 pb2
= &page
->text
->blocks
[i
];
3305 if (!pb2
) goto unlock
;
3307 block
= pb1
->u
.text
;
3310 page
->fmark
.span
= block
->lines
->first_span
;
3312 block
= pb2
->u
.text
;
3313 line
= &block
->lines
[block
->len
- 1];
3314 page
->lmark
.i
= line
->last_span
->len
- 1;
3315 page
->lmark
.span
= line
->last_span
;
3316 ret_v
= Val_bool (1);
3320 x
+= pdim
->bounds
.x0
;
3321 y
+= pdim
->bounds
.y0
;
3323 for (pageb
= page
->text
->blocks
;
3324 pageb
< page
->text
->blocks
+ page
->text
->len
;
3326 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3327 block
= pageb
->u
.text
;
3330 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3333 if (mark
== mark_block
) {
3335 page
->fmark
.span
= block
->lines
->first_span
;
3337 line
= &block
->lines
[block
->len
- 1];
3338 page
->lmark
.i
= line
->last_span
->len
- 1;
3339 page
->lmark
.span
= line
->last_span
;
3340 ret_v
= Val_bool (1);
3344 for (line
= block
->lines
;
3345 line
< block
->lines
+ block
->len
;
3347 fz_stext_span
*span
;
3350 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3353 if (mark
== mark_line
) {
3355 page
->fmark
.span
= line
->first_span
;
3357 page
->lmark
.i
= line
->last_span
->len
- 1;
3358 page
->lmark
.span
= line
->last_span
;
3359 ret_v
= Val_bool (1);
3363 for (span
= line
->first_span
; span
; span
= span
->next
) {
3367 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3370 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3372 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3375 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3377 int charnum2
, charnum3
= -1, charnum4
= -1;
3379 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3381 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3382 if (uninteresting (span
->text
[charnum2
].c
)) {
3383 charnum3
= charnum2
+ 1;
3387 if (charnum3
== -1) charnum3
= 0;
3389 for (charnum2
= charnum
+ 1;
3390 charnum2
< span
->len
;
3392 if (uninteresting (span
->text
[charnum2
].c
)) break;
3393 charnum4
= charnum2
;
3395 if (charnum4
== -1) goto unlock
;
3397 page
->fmark
.i
= charnum3
;
3398 page
->fmark
.span
= span
;
3400 page
->lmark
.i
= charnum4
;
3401 page
->lmark
.span
= span
;
3402 ret_v
= Val_bool (1);
3410 if (!Bool_val (ret_v
)) {
3411 page
->fmark
.span
= NULL
;
3412 page
->lmark
.span
= NULL
;
3422 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3424 CAMLparam3 (ptr_v
, x_v
, y_v
);
3425 CAMLlocal2 (ret_v
, res_v
);
3428 fz_page_block
*pageb
;
3429 struct pagedim
*pdim
;
3430 char *s
= String_val (ptr_v
);
3431 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3433 ret_v
= Val_int (0);
3434 if (trylock (__func__
)) {
3438 page
= parse_pointer (__func__
, s
);
3439 pdim
= &state
.pagedims
[page
->pdimno
];
3440 x
+= pdim
->bounds
.x0
;
3441 y
+= pdim
->bounds
.y0
;
3445 for (pageb
= page
->text
->blocks
;
3446 pageb
< page
->text
->blocks
+ page
->text
->len
;
3448 switch (pageb
->type
) {
3449 case FZ_PAGE_BLOCK_TEXT
:
3450 b
= &pageb
->u
.text
->bbox
;
3453 case FZ_PAGE_BLOCK_IMAGE
:
3454 b
= &pageb
->u
.image
->bbox
;
3461 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3466 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3467 ret_v
= caml_alloc_small (1, 1);
3468 Store_double_field (res_v
, 0, b
->x0
);
3469 Store_double_field (res_v
, 1, b
->x1
);
3470 Store_double_field (res_v
, 2, b
->y0
);
3471 Store_double_field (res_v
, 3, b
->y1
);
3472 Field (ret_v
, 0) = res_v
;
3480 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3482 CAMLparam2 (ptr_v
, rect_v
);
3485 struct pagedim
*pdim
;
3486 char *s
= String_val (ptr_v
);
3487 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3488 fz_stext_line
*line
;
3489 fz_page_block
*pageb
;
3490 fz_stext_block
*block
;
3491 fz_stext_span
*span
, *fspan
, *lspan
;
3493 if (trylock (__func__
)) {
3497 page
= parse_pointer (__func__
, s
);
3500 pdim
= &state
.pagedims
[page
->pdimno
];
3501 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3502 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3503 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3504 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3515 fspan
= page
->fmark
.span
;
3518 lspan
= page
->lmark
.span
;
3520 for (pageb
= page
->text
->blocks
;
3521 pageb
< page
->text
->blocks
+ page
->text
->len
;
3523 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3524 block
= pageb
->u
.text
;
3525 for (line
= block
->lines
;
3526 line
< block
->lines
+ block
->len
;
3529 for (span
= line
->first_span
; span
; span
= span
->next
) {
3530 for (i
= 0; i
< span
->len
; ++i
) {
3531 fz_stext_char_bbox (state
.ctx
, &b
, span
, i
);
3533 if (x0
>= b
.x0
&& x0
<= b
.x1
3534 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3538 if (x1
>= b
.x0
&& x1
<= b
.x1
3539 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3547 if (x1
< x0
&& fspan
== lspan
) {
3559 page
->fmark
.span
= fspan
;
3562 page
->lmark
.span
= lspan
;
3567 CAMLreturn (Val_unit
);
3570 static int UNUSED_ATTR
pipespan (FILE *f
, fz_stext_span
*span
, int a
, int b
)
3575 for (i
= a
; i
<= b
; ++i
) {
3576 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3577 ret
= fwrite (buf
, len
, 1, f
);
3580 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3581 len
, ret
, strerror (errno
));
3589 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3591 caml_failwith ("ml_popen not implemented under Cygwin");
3594 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3596 CAMLparam2 (command_v
, fds_v
);
3597 CAMLlocal2 (l_v
, tup_v
);
3601 value earg_v
= Nothing
;
3602 posix_spawnattr_t attr
;
3603 posix_spawn_file_actions_t fa
;
3604 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3606 argv
[2] = String_val (command_v
);
3608 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3609 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3612 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3613 msg
= "posix_spawnattr_init";
3617 #ifdef POSIX_SPAWN_USEVFORK
3618 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3619 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3624 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3627 tup_v
= Field (l_v
, 0);
3628 fd1
= Int_val (Field (tup_v
, 0));
3629 fd2
= Int_val (Field (tup_v
, 1));
3631 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3632 msg
= "posix_spawn_file_actions_addclose";
3638 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3639 msg
= "posix_spawn_file_actions_adddup2";
3646 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3647 msg
= "posix_spawn";
3652 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3653 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3657 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3658 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3663 unix_error (ret
, msg
, earg_v
);
3665 CAMLreturn (Val_int (pid
));
3669 CAMLprim value
ml_hassel (value ptr_v
)
3674 char *s
= String_val (ptr_v
);
3676 ret_v
= Val_bool (0);
3677 if (trylock (__func__
)) {
3681 page
= parse_pointer (__func__
, s
);
3682 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3688 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3690 CAMLparam2 (fd_v
, ptr_v
);
3694 fz_stext_line
*line
;
3695 fz_page_block
*pageb
;
3696 fz_stext_block
*block
;
3697 int fd
= Int_val (fd_v
);
3698 char *s
= String_val (ptr_v
);
3700 if (trylock (__func__
)) {
3704 page
= parse_pointer (__func__
, s
);
3706 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3707 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3711 f
= fdopen (fd
, "w");
3713 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3714 fd
, strerror (errno
));
3718 for (pageb
= page
->text
->blocks
;
3719 pageb
< page
->text
->blocks
+ page
->text
->len
;
3721 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3722 block
= pageb
->u
.text
;
3723 for (line
= block
->lines
;
3724 line
< block
->lines
+ block
->len
;
3726 fz_stext_span
*span
;
3728 for (span
= line
->first_span
; span
; span
= span
->next
) {
3731 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3732 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3733 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3736 if (pipespan (f
, span
, a
, b
)) {
3739 if (span
== page
->lmark
.span
) {
3742 if (span
== line
->last_span
) {
3743 if (putc ('\n', f
) == EOF
) {
3745 "failed break line on sel pipe: %s\n",
3756 int ret
= fclose (f
);
3759 if (errno
!= ECHILD
) {
3760 fprintf (stderr
, "failed to close sel pipe: %s\n",
3771 fprintf (stderr
, "failed to close sel pipe: %s\n",
3775 CAMLreturn (Val_unit
);
3778 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3780 CAMLparam1 (pagedimno_v
);
3782 int pagedimno
= Int_val (pagedimno_v
);
3785 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3786 if (trylock (__func__
)) {
3787 box
= fz_empty_rect
;
3790 box
= state
.pagedims
[pagedimno
].mediabox
;
3794 Store_double_field (ret_v
, 0, box
.x0
);
3795 Store_double_field (ret_v
, 1, box
.x1
);
3796 Store_double_field (ret_v
, 2, box
.y0
);
3797 Store_double_field (ret_v
, 3, box
.y1
);
3802 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3803 value dw_v
, value cols_v
)
3805 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3811 double winw
= Int_val (winw_v
);
3812 double winh
= Int_val (winh_v
);
3813 double dw
= Int_val (dw_v
);
3814 double cols
= Int_val (cols_v
);
3815 double pw
= 1.0, ph
= 1.0;
3817 if (trylock (__func__
)) {
3821 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3822 double w
= p
->pagebox
.x1
/ cols
;
3823 double h
= p
->pagebox
.y1
;
3827 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3829 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3832 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3835 ret_v
= caml_copy_double (zoom
);
3839 CAMLprim value
ml_getmaxw (value unit_v
)
3841 CAMLparam1 (unit_v
);
3847 if (trylock (__func__
)) {
3851 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3852 double w
= p
->pagebox
.x1
;
3853 maxw
= MAX (maxw
, w
);
3858 ret_v
= caml_copy_double (maxw
);
3862 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3864 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3866 int pt
= Int_val(pt_v
);
3867 int x
= Int_val (x_v
);
3868 int y
= Int_val (y_v
);
3871 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3872 ret_v
= caml_copy_double (w
);
3876 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3878 CAMLparam2 (pt_v
, string_v
);
3880 int pt
= Int_val (pt_v
);
3883 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3884 ret_v
= caml_copy_double (w
);
3888 CAMLprim value
ml_getpagebox (value opaque_v
)
3890 CAMLparam1 (opaque_v
);
3896 char *s
= String_val (opaque_v
);
3897 struct page
*page
= parse_pointer (__func__
, s
);
3899 ret_v
= caml_alloc_tuple (4);
3900 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3901 dev
->hints
|= FZ_IGNORE_SHADE
;
3903 ctm
= pagectm (page
);
3904 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3906 fz_drop_device (state
.ctx
, dev
);
3907 fz_round_rect (&bbox
, &rect
);
3908 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3909 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3910 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3911 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3916 CAMLprim value
ml_setaalevel (value level_v
)
3918 CAMLparam1 (level_v
);
3920 state
.aalevel
= Int_val (level_v
);
3921 CAMLreturn (Val_unit
);
3924 #pragma GCC diagnostic push
3925 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3926 #include <X11/Xlib.h>
3927 #include <X11/cursorfont.h>
3928 #pragma GCC diagnostic pop
3931 #include <EGL/egl.h>
3936 static const int shapes
[] = {
3937 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3940 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3953 XVisualInfo
*visual
;
3954 Cursor curs
[CURS_COUNT
];
3958 static VisualID
initvisual (void)
3960 /* On this system with: `Haswell-ULT Integrated Graphics
3961 Controller' and Mesa 11.0.6; perf stat reports [1] that when
3962 using glX chosen visual and auto scrolling some document in
3963 fullscreen the power/energy-gpu is more than 1 joule bigger
3964 than when using hand picked visual that stands alone in glxinfo
3965 output: it's dead last in the list and it's the only one with
3966 `visual dep' (sic) of 32
3968 No clue what's going on here...
3970 [1] perf stat -a -I 1200 -e "power/energy-gpu/"
3976 info
.class = TrueColor
;
3977 glx
.visual
= XGetVisualInfo (glx
.dpy
, VisualDepthMask
| VisualClassMask
,
3979 if (!ret
|| !glx
.visual
) {
3980 XCloseDisplay (glx
.dpy
);
3981 caml_failwith ("XGetVisualInfo");
3983 return glx
.visual
->visualid
;
3987 static void initcurs (void)
3989 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3990 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3994 CAMLprim
void ml_setbgcol (value color_v
)
3996 CAMLparam1 (color_v
);
3997 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
4002 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4004 CAMLparam3 (display_v
, wid_v
, screen_v
);
4008 EGLint attribs
[] = {
4010 EGL_NATIVE_VISUAL_ID
, 0,
4014 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
4015 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
4020 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4022 caml_failwith ("XOpenDisplay");
4025 eglBindAPI (EGL_OPENGL_API
);
4027 glx
.edpy
= eglGetDisplay (glx
.dpy
);
4028 if (glx
.dpy
== EGL_NO_DISPLAY
) {
4029 caml_failwith ("eglGetDisplay");
4032 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
4033 caml_failwith ("eglInitialize");
4037 attribs
[1] = visid
= initvisual ();
4040 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
4042 caml_failwith ("eglChooseConfig");
4047 if (!eglGetConfigAttrib (glx
.edpy
, glx
.conf
,
4048 EGL_NATIVE_VISUAL_ID
, &visid
)) {
4049 caml_failwith ("eglGetConfigAttrib");
4054 glx
.wid
= Int_val (wid_v
);
4055 CAMLreturn (Val_int (visid
));
4058 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4060 CAMLparam1 (unit_v
);
4062 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
4064 caml_failwith ("eglCreateContext");
4067 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
4069 if (glx
.win
== EGL_NO_SURFACE
) {
4070 caml_failwith ("eglCreateWindowSurface");
4074 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
4076 caml_failwith ("eglMakeCurrent");
4078 CAMLreturn (Val_unit
);
4081 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4083 CAMLparam3 (display_v
, wid_v
, screen_v
);
4085 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4087 caml_failwith ("XOpenDisplay");
4093 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4094 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4096 XCloseDisplay (glx
.dpy
);
4097 caml_failwith ("glXChooseVisual");
4102 glx
.wid
= Int_val (wid_v
);
4103 CAMLreturn (Val_int (glx
.visual
->visualid
));
4106 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4108 CAMLparam1 (unit_v
);
4110 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4112 caml_failwith ("glXCreateContext");
4118 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4119 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4121 caml_failwith ("glXMakeCurrent");
4123 CAMLreturn (Val_unit
);
4127 CAMLprim value
ml_setcursor (value cursor_v
)
4129 CAMLparam1 (cursor_v
);
4130 size_t cursn
= Int_val (cursor_v
);
4132 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
4133 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
4135 CAMLreturn (Val_unit
);
4138 CAMLprim value
ml_swapb (value unit_v
)
4140 CAMLparam1 (unit_v
);
4142 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
4143 caml_failwith ("eglSwapBuffers");
4146 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4148 CAMLreturn (Val_unit
);
4151 #include "keysym2ucs.c"
4153 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4155 CAMLparam1 (keysym_v
);
4157 KeySym keysym
= Int_val (keysym_v
);
4162 rune
= keysym2ucs (keysym
);
4163 len
= fz_runetochar (buf
, rune
);
4165 str_v
= caml_copy_string (buf
);
4169 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4171 CAMLprim value
ml_platform (value unit_v
)
4173 CAMLparam1 (unit_v
);
4174 CAMLlocal2 (tup_v
, arr_v
);
4175 int platid
= piunknown
;
4178 #if defined __linux__
4180 #elif defined __CYGWIN__
4182 #elif defined __DragonFly__ || defined __FreeBSD__
4183 || defined __OpenBSD__
|| defined __NetBSD__
4185 #elif defined __sun__
4187 #elif defined __APPLE__
4190 if (uname (&buf
)) err (1, "uname");
4192 tup_v
= caml_alloc_tuple (2);
4194 char const *sar
[] = {
4201 arr_v
= caml_copy_string_array (sar
);
4203 Field (tup_v
, 0) = Val_int (platid
);
4204 Field (tup_v
, 1) = arr_v
;
4208 CAMLprim value
ml_cloexec (value fd_v
)
4211 int fd
= Int_val (fd_v
);
4213 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4214 uerror ("fcntl", Nothing
);
4216 CAMLreturn (Val_unit
);
4219 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4221 CAMLparam2 (w_v
, h_v
);
4224 int w
= Int_val (w_v
);
4225 int h
= Int_val (h_v
);
4226 int cs
= Int_val (cs_v
);
4228 if (state
.bo_usable
) {
4229 pbo
= calloc (sizeof (*pbo
), 1);
4231 err (1, "calloc pbo");
4243 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
4246 state
.glGenBuffersARB (1, &pbo
->id
);
4247 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4248 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4249 NULL
, GL_STREAM_DRAW
);
4250 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4252 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4254 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4255 state
.glDeleteBuffersARB (1, &pbo
->id
);
4257 ret_v
= caml_copy_string ("0");
4263 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4265 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4269 err (1, "malloc %d bytes failed", res
+1);
4271 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4273 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4275 ret_v
= caml_copy_string (s
);
4280 ret_v
= caml_copy_string ("0");
4285 CAMLprim value
ml_freepbo (value s_v
)
4288 char *s
= String_val (s_v
);
4289 struct tile
*tile
= parse_pointer (__func__
, s
);
4292 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4294 tile
->pbo
->ptr
= NULL
;
4295 tile
->pbo
->size
= -1;
4297 CAMLreturn (Val_unit
);
4300 CAMLprim value
ml_unmappbo (value s_v
)
4303 char *s
= String_val (s_v
);
4304 struct tile
*tile
= parse_pointer (__func__
, s
);
4307 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4308 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4309 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4311 tile
->pbo
->ptr
= NULL
;
4312 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4314 CAMLreturn (Val_unit
);
4317 static void setuppbo (void)
4320 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
4322 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4324 state
.bo_usable
= GGPA (glBindBufferARB
)
4325 && GGPA (glUnmapBufferARB
)
4326 && GGPA (glMapBufferARB
)
4327 && GGPA (glBufferDataARB
)
4328 && GGPA (glGenBuffersARB
)
4329 && GGPA (glDeleteBuffersARB
);
4333 CAMLprim value
ml_bo_usable (value unit_v
)
4335 CAMLparam1 (unit_v
);
4336 CAMLreturn (Val_bool (state
.bo_usable
));
4339 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4341 CAMLparam3 (ptr_v
, x_v
, y_v
);
4342 CAMLlocal2 (ret_v
, tup_v
);
4344 char *s
= String_val (ptr_v
);
4345 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4346 struct pagedim
*pdim
;
4350 page
= parse_pointer (__func__
, s
);
4351 pdim
= &state
.pagedims
[page
->pdimno
];
4353 ret_v
= Val_int (0);
4354 if (trylock (__func__
)) {
4358 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4359 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
4360 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4365 p
.x
= x
+ pdim
->bounds
.x0
;
4366 p
.y
= y
+ pdim
->bounds
.y0
;
4368 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4369 fz_invert_matrix (&ctm
, &ctm
);
4370 fz_transform_point (&p
, &ctm
);
4372 tup_v
= caml_alloc_tuple (2);
4373 ret_v
= caml_alloc_small (1, 1);
4374 Field (tup_v
, 0) = Val_int (p
.x
);
4375 Field (tup_v
, 1) = Val_int (p
.y
);
4376 Field (ret_v
, 0) = tup_v
;
4383 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4384 value x_v
, value y_v
)
4386 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4389 char *s
= String_val (ptr_v
);
4390 int pageno
= Int_val (pageno_v
);
4391 int pdimno
= Int_val (pdimno_v
);
4392 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4393 struct pagedim
*pdim
;
4397 ret_v
= Val_int (0);
4401 page
= loadpage (pageno
, pdimno
);
4404 page
= parse_pointer (__func__
, s
);
4406 pdim
= &state
.pagedims
[pdimno
];
4408 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4409 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
4410 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4415 p
.x
= x
+ pdim
->bounds
.x0
;
4416 p
.y
= y
+ pdim
->bounds
.y0
;
4418 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4419 fz_transform_point (&p
, &ctm
);
4421 ret_v
= caml_alloc_tuple (2);
4422 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4423 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4432 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4435 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4436 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4442 char *s
= String_val (ptr_v
);
4444 page
= parse_pointer (__func__
, s
);
4445 annot
= pdf_create_annot (state
.ctx
, pdf
,
4446 (pdf_page
*) page
->fzpage
, FZ_ANNOT_TEXT
);
4447 p
.x
= Int_val (x_v
);
4448 p
.y
= Int_val (y_v
);
4449 pdf_set_annot_contents (state
.ctx
, pdf
, annot
, String_val (contents_v
));
4450 pdf_set_text_annot_position (state
.ctx
, pdf
, annot
, p
);
4453 CAMLreturn (Val_unit
);
4456 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4458 CAMLparam2 (ptr_v
, n_v
);
4459 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4463 char *s
= String_val (ptr_v
);
4464 struct slink
*slink
;
4466 page
= parse_pointer (__func__
, s
);
4467 slink
= &page
->slinks
[Int_val (n_v
)];
4468 pdf_delete_annot (state
.ctx
, pdf
,
4469 (pdf_page
*) page
->fzpage
,
4470 (pdf_annot
*) slink
->u
.annot
);
4473 CAMLreturn (Val_unit
);
4476 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4478 CAMLparam3 (ptr_v
, n_v
, str_v
);
4479 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4483 char *s
= String_val (ptr_v
);
4484 struct slink
*slink
;
4486 page
= parse_pointer (__func__
, s
);
4487 slink
= &page
->slinks
[Int_val (n_v
)];
4488 pdf_set_annot_contents (state
.ctx
, pdf
, (pdf_annot
*) slink
->u
.annot
,
4489 String_val (str_v
));
4492 CAMLreturn (Val_unit
);
4495 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4497 CAMLparam1 (unit_v
);
4498 CAMLreturn (Val_bool (state
.dirty
));
4501 CAMLprim value
ml_savedoc (value path_v
)
4503 CAMLparam1 (path_v
);
4504 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4507 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4509 CAMLreturn (Val_unit
);
4512 static void makestippletex (void)
4514 const char pixels
[] = "\xff\xff\0\0";
4515 glGenTextures (1, &state
.stid
);
4516 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4517 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4518 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4531 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4533 return caml_copy_string (FZ_VERSION
);
4536 #ifdef USE_FONTCONFIG
4542 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4546 int UNUSED_ATTR needs_exact_metrics
)
4553 FcPattern
*pat
, *pat1
;
4555 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4556 name
, bold
, italic
, needs_exact_metrics
);
4559 fc
.config
= FcInitLoadConfigAndFonts ();
4561 lprintf ("FcInitLoadConfigAndFonts failed\n");
4565 if (!fc
.config
) return NULL
;
4567 size
= strlen (name
);
4568 if (bold
) size
+= sizeof (":bold") - 1;
4569 if (italic
) size
+= sizeof (":italic") - 1;
4572 buf
= malloc (size
);
4574 err (1, "malloc %zu failed", size
);
4578 if (bold
&& italic
) {
4579 strcat (buf
, ":bold:italic");
4582 if (bold
) strcat (buf
, ":bold");
4583 if (italic
) strcat (buf
, ":italic");
4585 for (i
= 0; i
< size
; ++i
) {
4586 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4589 lprintf ("fcbuf=%s\n", buf
);
4590 pat
= FcNameParse ((FcChar8
*) buf
);
4592 printd ("emsg FcNameParse failed\n");
4597 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4598 printd ("emsg FcConfigSubstitute failed\n");
4602 FcDefaultSubstitute (pat
);
4604 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4606 printd ("emsg FcFontMatch failed\n");
4607 FcPatternDestroy (pat
);
4612 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4613 printd ("emsg FcPatternGetString failed\n");
4614 FcPatternDestroy (pat
);
4615 FcPatternDestroy (pat1
);
4621 printd ("emsg name=%s path=%s\n", name
, path
);
4623 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4624 FcPatternDestroy (pat
);
4625 FcPatternDestroy (pat1
);
4631 CAMLprim value
ml_init (value csock_v
, value params_v
)
4633 CAMLparam2 (csock_v
, params_v
);
4634 CAMLlocal2 (trim_v
, fuzz_v
);
4642 state
.csock
= Int_val (csock_v
);
4643 state
.rotate
= Int_val (Field (params_v
, 0));
4644 state
.fitmodel
= Int_val (Field (params_v
, 1));
4645 trim_v
= Field (params_v
, 2);
4646 texcount
= Int_val (Field (params_v
, 3));
4647 state
.sliceheight
= Int_val (Field (params_v
, 4));
4648 mustoresize
= Int_val (Field (params_v
, 5));
4649 colorspace
= Int_val (Field (params_v
, 6));
4650 fontpath
= String_val (Field (params_v
, 7));
4652 if (caml_string_length (Field (params_v
, 8)) > 0) {
4653 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4655 if (!state
.trimcachepath
) {
4656 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4660 haspboext
= Bool_val (Field (params_v
, 9));
4662 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4663 fz_register_document_handlers (state
.ctx
);
4665 #ifdef USE_FONTCONFIG
4666 if (Bool_val (Field (params_v
, 10))) {
4667 fz_install_load_system_font_funcs (
4668 state
.ctx
, fc_load_system_font_func
, NULL
4673 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4674 fuzz_v
= Field (trim_v
, 1);
4675 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4676 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4677 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4678 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4680 set_tex_params (colorspace
);
4683 #ifndef USE_FONTCONFIG
4684 state
.face
= load_font (fontpath
);
4688 char *buf
= fontpath
;
4689 FcPattern
*pat
, *pat1
;
4692 fc
.config
= FcInitLoadConfigAndFonts ();
4694 errx (1, "FcInitLoadConfigAndFonts failed");
4697 pat
= FcNameParse ((FcChar8
*) buf
);
4699 errx (1, "FcNameParse failed");
4702 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4703 errx (1, "FcConfigSubstitute failed");
4705 FcDefaultSubstitute (pat
);
4707 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4709 errx (1, "FcFontMatch failed");
4712 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4713 errx (1, "FcPatternGetString failed");
4716 state
.face
= load_font ((char *) path
);
4717 FcPatternDestroy (pat
);
4718 FcPatternDestroy (pat1
);
4723 void *base
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
4725 state
.face
= load_builtin_font (base
, len
);
4727 if (!state
.face
) _exit (1);
4729 realloctexts (texcount
);
4737 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4739 errx (1, "pthread_create: %s", strerror (ret
));
4742 CAMLreturn (Val_unit
);