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 #include <mupdf/fitz.h>
44 #include <mupdf/pdf.h>
47 #include FT_FREETYPE_H
50 #include <fontconfig/fontconfig.h>
54 #define CACHE_PAGEREFS
57 extern char **environ
;
61 #define NORETURN_ATTR __attribute__ ((noreturn))
62 #define UNUSED_ATTR __attribute__ ((unused))
63 #if !defined __clang__
64 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
66 #define OPTIMIZE_ATTR(n)
68 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
72 #define OPTIMIZE_ATTR(n)
73 #define GCC_FMT_ATTR(a, b)
78 #define FMT_ptr PRIxPTR
79 #define SCN_ptr SCNxPTR
80 #define FMT_ptr_cast(p) ((uintptr_t) (p))
81 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
83 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
84 err (int exitcode
, const char *fmt
, ...)
91 vfprintf (stderr
, fmt
, ap
);
93 fprintf (stderr
, ": %s\n", strerror (savederrno
));
98 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
99 errx (int exitcode
, const char *fmt
, ...)
104 vfprintf (stderr
, fmt
, ap
);
106 fputc ('\n', stderr
);
111 #ifndef GL_TEXTURE_RECTANGLE_ARB
112 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
116 #define TEXT_TYPE GL_TEXTURE_2D
118 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
122 #define GL_BGRA 0x80E1
125 #ifndef GL_UNSIGNED_INT_8_8_8_8
126 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
129 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
130 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
134 #define lprintf printf
139 #define ARSERT(cond) for (;;) { \
141 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
157 struct slice slices
[1];
168 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
172 enum { SLINK
, SANNOT
} tag
;
192 fz_stext_sheet
*sheet
;
194 fz_display_list
*dlist
;
196 struct slink
*slinks
;
198 struct annot
*annots
;
207 struct pagedim
*pagedims
;
222 fz_colorspace
*colorspace
;
230 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
253 void (*glBindBufferARB
) (GLenum
, GLuint
);
254 GLboolean (*glUnmapBufferARB
) (GLenum
);
255 void *(*glMapBufferARB
) (GLenum
, GLenum
);
256 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
257 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
258 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
260 GLfloat texcoords
[8];
261 GLfloat vertices
[16];
263 #ifdef CACHE_PAGEREFS
279 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
281 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
284 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
286 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
289 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
291 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
292 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
295 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
297 static void lock (const char *cap
)
299 int ret
= pthread_mutex_lock (&mutex
);
301 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
305 static void unlock (const char *cap
)
307 int ret
= pthread_mutex_unlock (&mutex
);
309 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
313 static int trylock (const char *cap
)
315 int ret
= pthread_mutex_trylock (&mutex
);
316 if (ret
&& ret
!= EBUSY
) {
317 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
322 static void *parse_pointer (const char *cap
, const char *s
)
327 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
329 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
334 static double now (void)
338 if (gettimeofday (&tv
, NULL
)) {
339 err (1, "gettimeofday");
341 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
344 static int hasdata (void)
347 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
348 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
352 CAMLprim value
ml_hasdata (value fd_v
)
357 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
358 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
359 CAMLreturn (Val_bool (avail
> 0));
362 static void readdata (int fd
, void *p
, int size
)
367 n
= read (fd
, p
, size
);
369 if (errno
== EINTR
) goto again
;
370 err (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
373 if (!n
) errx (1, "EOF while reading");
374 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
378 static void writedata (int fd
, char *p
, int size
)
382 /* One should lookup type punning/strict aliasing etc in standard,DRs,Web to
383 convince herself that this is:
385 b. practically the only way to achieve the result
386 (union puns notwithstanding) */
387 memcpy (p
, &size
, 4);
388 n
= write (fd
, p
, size
+ 4);
390 if (!n
) errx (1, "EOF while writing data");
391 err (1, "write (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
395 static int readlen (int fd
)
397 /* Type punned unions here. Why? Less code (Adjusted by more comments).
398 https://en.wikipedia.org/wiki/Type_punning */
399 union { int len
; char raw
[4]; } buf
;
400 readdata (fd
, buf
.raw
, 4);
404 CAMLprim
void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
406 CAMLparam3 (fd_v
, bytes_v
, len_v
);
407 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
411 CAMLprim value
ml_rcmd (value fd_v
)
414 CAMLlocal1 (strdata_v
);
415 int fd
= Int_val (fd_v
);
416 int len
= readlen (fd
);
417 strdata_v
= caml_alloc_string (len
);
418 readdata (fd
, String_val (strdata_v
), len
);
419 CAMLreturn (strdata_v
);
422 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
430 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
433 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
437 if (len
< size
- 4) {
438 writedata (state
.csock
, buf
, len
);
444 err (1, "vsnprintf for `%s' failed", fmt
);
446 buf
= realloc (buf
, size
);
451 static void closedoc (void)
453 #ifdef CACHE_PAGEREFS
454 if (state
.pdflut
.objs
) {
457 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
458 pdf_drop_obj (state
.ctx
, state
.pdflut
.objs
[i
]);
460 free (state
.pdflut
.objs
);
461 state
.pdflut
.objs
= NULL
;
462 state
.pdflut
.idx
= 0;
466 fz_drop_document (state
.ctx
, state
.doc
);
471 static int openxref (char *filename
, char *password
)
475 for (i
= 0; i
< state
.texcount
; ++i
) {
476 state
.texowners
[i
].w
= -1;
477 state
.texowners
[i
].slice
= NULL
;
483 if (state
.pagedims
) {
484 free (state
.pagedims
);
485 state
.pagedims
= NULL
;
487 state
.pagedimcount
= 0;
489 fz_set_aa_level (state
.ctx
, state
.aalevel
);
490 #ifdef CSS_HACK_TO_READ_EPUBS_COMFORTABLY
491 fz_set_user_css (state
.ctx
,
492 "body { margin-left: 20%; margin-right: 20%; }");
494 state
.doc
= fz_open_document (state
.ctx
, filename
);
495 if (fz_needs_password (state
.ctx
, state
.doc
)) {
496 if (password
&& !*password
) {
501 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
503 printd ("pass fail");
508 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
512 static void pdfinfo (void)
514 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
518 printd ("info PDF version\t%d.%d",
519 pdf
->version
/ 10, pdf
->version
% 10);
521 infoobj
= pdf_dict_gets (state
.ctx
, pdf_trailer (state
.ctx
,
526 char *items
[] = { "Title", "Author", "Creator",
527 "Producer", "CreationDate" };
529 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
530 pdf_obj
*obj
= pdf_dict_gets (state
.ctx
, infoobj
, items
[i
]);
531 s
= pdf_to_utf8 (state
.ctx
, pdf
, obj
);
532 if (*s
) printd ("info %s\t%s", items
[i
], s
);
533 fz_free (state
.ctx
, s
);
540 static void unlinktile (struct tile
*tile
)
544 for (i
= 0; i
< tile
->slicecount
; ++i
) {
545 struct slice
*s
= &tile
->slices
[i
];
547 if (s
->texindex
!= -1) {
548 if (state
.texowners
[s
->texindex
].slice
== s
) {
549 state
.texowners
[s
->texindex
].slice
= NULL
;
555 static void freepage (struct page
*page
)
559 fz_drop_stext_page (state
.ctx
, page
->text
);
562 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
567 fz_drop_display_list (state
.ctx
, page
->dlist
);
568 fz_drop_page (state
.ctx
, page
->fzpage
);
572 static void freetile (struct tile
*tile
)
577 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
580 fz_drop_pixmap (state
.ctx
, state
.pig
);
582 state
.pig
= tile
->pixmap
;
587 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
596 static int cacheline32bytes
;
598 static void __attribute__ ((constructor
)) clcheck (void)
600 char **envp
= environ
;
605 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
607 cacheline32bytes
= auxv
[1] == 32;
613 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
615 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
616 if (cacheline32bytes
&& size
> 32) {
617 intptr_t a1
, a2
, diff
;
619 vector
unsigned char v
= vec_splat_u8 (-1);
620 vector
unsigned char *p
;
622 a1
= a2
= (intptr_t) pixmap
->samples
;
623 a2
= (a1
+ 31) & ~31;
628 while (a1
!= a2
) *(char *) a1
++ = 0xff;
629 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
630 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
632 vec_st (v
, i
+ 16, p
);
634 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
636 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
639 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
642 static void trimctm (pdf_page
*page
, int pindex
)
645 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
647 if (!pdim
->tctmready
) {
648 if (state
.trimmargins
) {
650 fz_matrix rm
, sm
, tm
, im
, ctm1
;
652 fz_rotate (&rm
, -pdim
->rotate
);
653 fz_scale (&sm
, 1, -1);
654 fz_concat (&ctm
, &rm
, &sm
);
655 realbox
= pdim
->mediabox
;
656 fz_transform_rect (&realbox
, &ctm
);
657 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
658 fz_concat (&ctm1
, &ctm
, &tm
);
659 fz_invert_matrix (&im
, &page
->ctm
);
660 fz_concat (&ctm
, &im
, &ctm1
);
670 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
673 int pdimno
= pdim
- state
.pagedims
;
675 if (pdf_specifics (state
.ctx
, state
.doc
)) {
676 trimctm ((pdf_page
*) fzpage
, pdimno
);
677 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
680 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
681 fz_concat (&ctm
, &tm
, &pdim
->ctm
);
686 static fz_matrix
pagectm (struct page
*page
)
688 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
691 static void *loadpage (int pageno
, int pindex
)
696 page
= calloc (sizeof (struct page
), 1);
698 err (1, "calloc page %d", pageno
);
701 page
->dlist
= fz_new_display_list (state
.ctx
, NULL
);
702 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
704 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
705 fz_run_page (state
.ctx
, page
->fzpage
, dev
,
708 fz_catch (state
.ctx
) {
711 fz_drop_device (state
.ctx
, dev
);
713 page
->pdimno
= pindex
;
714 page
->pageno
= pageno
;
715 page
->sgen
= state
.gen
;
716 page
->agen
= state
.gen
;
717 page
->tgen
= state
.gen
;
721 static struct tile
*alloctile (int h
)
728 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
729 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
730 tile
= calloc (tilesize
, 1);
732 err (1, "cannot allocate tile (%" FMT_s
" bytes)", tilesize
);
734 for (i
= 0; i
< slicecount
; ++i
) {
735 int sh
= fz_mini (h
, state
.sliceheight
);
736 tile
->slices
[i
].h
= sh
;
737 tile
->slices
[i
].texindex
= -1;
740 tile
->slicecount
= slicecount
;
741 tile
->sliceheight
= state
.sliceheight
;
745 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
753 struct pagedim
*pdim
;
755 tile
= alloctile (h
);
756 pdim
= &state
.pagedims
[page
->pdimno
];
761 bbox
.x1
= bbox
.x0
+ w
;
762 bbox
.y1
= bbox
.y0
+ h
;
765 if (state
.pig
->w
== w
767 && state
.pig
->colorspace
== state
.colorspace
) {
768 tile
->pixmap
= state
.pig
;
769 tile
->pixmap
->x
= bbox
.x0
;
770 tile
->pixmap
->y
= bbox
.y0
;
773 fz_drop_pixmap (state
.ctx
, state
.pig
);
780 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
786 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
, 1);
792 clearpixmap (tile
->pixmap
);
794 dev
= fz_new_draw_device (state
.ctx
, NULL
, tile
->pixmap
);
795 ctm
= pagectm (page
);
796 fz_rect_from_irect (&rect
, &bbox
);
797 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, &ctm
, &rect
, NULL
);
798 fz_drop_device (state
.ctx
, dev
);
803 #ifdef CACHE_PAGEREFS
804 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
805 thanks to Robin Watts */
807 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
809 fz_context
*ctx
= state
.ctx
; /* doc->ctx; */
813 if (state
.pdflut
.idx
== state
.pagecount
) return;
815 kids
= pdf_dict_gets (ctx
, node
, "Kids");
816 len
= pdf_array_len (ctx
, kids
);
819 fz_throw (ctx
, FZ_ERROR_GENERIC
, "malformed pages tree");
821 if (pdf_mark_obj (ctx
, node
))
822 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
823 for (i
= 0; i
< len
; i
++) {
824 pdf_obj
*kid
= pdf_array_get (ctx
, kids
, i
);
825 char *type
= pdf_to_name (ctx
, pdf_dict_gets (ctx
, kid
, "Type"));
827 ? !strcmp (type
, "Pages")
828 : pdf_dict_gets (ctx
, kid
, "Kids")
829 && !pdf_dict_gets (ctx
, kid
, "MediaBox")) {
830 pdf_collect_pages (doc
, kid
);
834 ? strcmp (type
, "Page") != 0
835 : !pdf_dict_gets (ctx
, kid
, "MediaBox"))
836 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
837 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (ctx
, kid
);
840 pdf_unmark_obj (ctx
, node
);
844 pdf_load_page_objs (pdf_document
*doc
)
846 pdf_obj
*root
= pdf_dict_gets (state
.ctx
,
847 pdf_trailer (state
.ctx
, doc
), "Root");
848 pdf_obj
*node
= pdf_dict_gets (state
.ctx
, root
, "Pages");
851 fz_throw (state
.ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
853 state
.pdflut
.idx
= 0;
854 pdf_collect_pages (doc
, node
);
858 static void initpdims (int wthack
)
862 fz_rect rootmediabox
;
863 int pageno
, trim
, show
;
864 int trimw
= 0, cxcount
;
865 fz_context
*ctx
= state
.ctx
;
866 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
873 if (state
.trimmargins
&& state
.trimcachepath
) {
874 trimf
= fopen (state
.trimcachepath
, "rb");
876 trimf
= fopen (state
.trimcachepath
, "wb");
881 if (state
.trimmargins
|| pdf
|| !state
.cxack
)
882 cxcount
= state
.pagecount
;
884 cxcount
= fz_mini (state
.pagecount
, 1);
888 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
889 "Root/Pages/MediaBox");
890 pdf_to_rect (ctx
, obj
, &rootmediabox
);
893 #ifdef CACHE_PAGEREFS
894 if (pdf
&& (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= pdf
)) {
895 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
896 if (!state
.pdflut
.objs
) {
897 err (1, "malloc pageobjs %zu %d %zu failed",
898 sizeof (*state
.pdflut
.objs
), cxcount
,
899 sizeof (*state
.pdflut
.objs
) * cxcount
);
901 state
.pdflut
.count
= cxcount
;
902 pdf_load_page_objs (pdf
);
903 state
.pdflut
.pdf
= pdf
;
907 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
914 pdf_obj
*pageref
, *pageobj
;
916 #ifdef CACHE_PAGEREFS
917 pageref
= state
.pdflut
.objs
[pageno
];
919 pageref
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
921 pageobj
= pdf_resolve_indirect (ctx
, pageref
);
923 if (state
.trimmargins
) {
928 page
= pdf_load_page (ctx
, pdf
, pageno
);
929 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
930 trim
= state
.trimanew
|| !obj
;
936 dev
= fz_new_bbox_device (ctx
, &rect
);
937 dev
->hints
|= FZ_IGNORE_SHADE
;
938 fz_invert_matrix (&ctm
, &page
->ctm
);
939 pdf_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
940 fz_drop_device (ctx
, dev
);
942 rect
.x0
+= state
.trimfuzz
.x0
;
943 rect
.x1
+= state
.trimfuzz
.x1
;
944 rect
.y0
+= state
.trimfuzz
.y0
;
945 rect
.y1
+= state
.trimfuzz
.y1
;
946 fz_transform_rect (&rect
, &ctm
);
947 fz_intersect_rect (&rect
, &page
->mediabox
);
949 if (fz_is_empty_rect (&rect
)) {
950 mediabox
= page
->mediabox
;
956 obj
= pdf_new_array (ctx
, pdf
, 4);
957 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
959 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
961 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
963 pdf_array_push (ctx
, obj
, pdf_new_real (ctx
, pdf
,
965 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
968 mediabox
.x0
= pdf_to_real (ctx
,
969 pdf_array_get (ctx
, obj
, 0));
970 mediabox
.y0
= pdf_to_real (ctx
,
971 pdf_array_get (ctx
, obj
, 1));
972 mediabox
.x1
= pdf_to_real (ctx
,
973 pdf_array_get (ctx
, obj
, 2));
974 mediabox
.y1
= pdf_to_real (ctx
,
975 pdf_array_get (ctx
, obj
, 3));
978 rotate
= page
->rotate
;
979 fz_drop_page (ctx
, &page
->super
);
981 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
983 printd ("progress %f Trimming %d",
984 (double) (pageno
+ 1) / state
.pagecount
,
989 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
997 pdf_dict_gets (ctx
, pageobj
, "MediaBox"),
999 if (fz_is_empty_rect (&mediabox
)) {
1008 pdf_dict_gets (ctx
, pageobj
, "CropBox"),
1010 if (!fz_is_empty_rect (&cropbox
)) {
1015 fz_intersect_rect (&mediabox
, &cropbox
);
1020 if (fz_is_empty_rect (&rootmediabox
)) {
1022 "cannot find page size for page %d\n",
1026 mediabox
= rootmediabox
;
1030 rotate
= pdf_to_int (ctx
,
1031 pdf_dict_gets (ctx
, pageobj
, "Rotate"));
1035 if (state
.trimmargins
&& trimw
) {
1039 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1040 fz_bound_page (ctx
, page
, &mediabox
);
1042 if (state
.trimmargins
) {
1046 dev
= fz_new_bbox_device (ctx
, &rect
);
1047 dev
->hints
|= FZ_IGNORE_SHADE
;
1048 fz_run_page (ctx
, page
, dev
, &fz_identity
, NULL
);
1049 fz_drop_device (ctx
, dev
);
1051 rect
.x0
+= state
.trimfuzz
.x0
;
1052 rect
.x1
+= state
.trimfuzz
.x1
;
1053 rect
.y0
+= state
.trimfuzz
.y0
;
1054 rect
.y1
+= state
.trimfuzz
.y1
;
1055 fz_intersect_rect (&rect
, &mediabox
);
1057 if (!fz_is_empty_rect (&rect
)) {
1061 fz_drop_page (ctx
, page
);
1063 printd ("progress %f loading %d",
1064 (double) (pageno
+ 1) / state
.pagecount
,
1071 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1073 err (1, "fwrite trim mediabox");
1079 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1081 err (1, "fread trim mediabox %d", pageno
);
1087 page
= fz_load_page (ctx
, state
.doc
, pageno
);
1088 fz_bound_page (ctx
, page
, &mediabox
);
1089 fz_drop_page (ctx
, page
);
1091 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1093 printd ("progress %f Gathering dimensions %d",
1094 (double) (pageno
) / state
.pagecount
,
1099 fprintf (stderr
, "failed to load page %d\n", pageno
);
1105 if (state
.pagedimcount
== 0
1106 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1107 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1110 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1111 state
.pagedims
= realloc (state
.pagedims
, size
);
1112 if (!state
.pagedims
) {
1113 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1114 size
, state
.pagedimcount
+ 1);
1117 p
= &state
.pagedims
[state
.pagedimcount
++];
1119 p
->mediabox
= mediabox
;
1125 printd ("progress 1 %s %d pages in %f seconds",
1126 state
.trimmargins
? "Trimmed" : "Processed",
1127 state
.pagecount
, end
- start
);
1131 if (fclose (trimf
)) {
1137 static void layout (void)
1142 struct pagedim
*p
= p
;
1143 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1145 if (state
.pagedimcount
== 0) return;
1147 switch (state
.fitmodel
) {
1148 case FitProportional
:
1149 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1152 p
= &state
.pagedims
[pindex
];
1153 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1155 fz_transform_rect (&box
, &rm
);
1157 x0
= fz_min (box
.x0
, box
.x1
);
1158 x1
= fz_max (box
.x0
, box
.x1
);
1161 maxw
= fz_max (w
, maxw
);
1162 zoom
= state
.w
/ maxw
;
1174 ARSERT (0 && state
.fitmodel
);
1177 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1181 p
= &state
.pagedims
[pindex
];
1182 fz_rotate (&ctm
, state
.rotate
);
1183 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1185 fz_transform_rect (&box
, &rm
);
1186 w
= box
.x1
- box
.x0
;
1187 switch (state
.fitmodel
) {
1188 case FitProportional
:
1189 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1195 h
= box
.y1
- box
.y0
;
1197 zoom
= fz_min (zw
, zh
);
1198 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1207 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1208 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1210 fz_rotate (&rm
, p
->rotate
);
1211 p
->pagebox
= p
->mediabox
;
1212 fz_transform_rect (&p
->pagebox
, &rm
);
1213 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1214 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1218 fz_transform_rect (&rect
, &ctm
);
1219 fz_round_rect (&p
->bounds
, &rect
);
1222 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1223 fz_scale (&sm
, zoom
, -zoom
);
1224 fz_concat (&ctm
, &tm
, &sm
);
1225 fz_concat (&p
->lctm
, &ctm
, &rm
);
1231 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
1232 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
1233 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
1234 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
1235 int boundw
= x1
- x0
;
1236 int boundh
= y1
- y0
;
1238 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1239 } while (p
-- != state
.pagedims
);
1243 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1244 desttoanchor (fz_link_dest
*dest
)
1248 struct pagedim
*pdim
= state
.pagedims
;
1253 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1254 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1256 pdim
= &state
.pagedims
[i
];
1258 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1260 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1261 p
.x
= dest
->ld
.gotor
.lt
.x
;
1264 p
.y
= dest
->ld
.gotor
.lt
.y
;
1265 fz_transform_point (&p
, &pdim
->lctm
);
1269 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1270 double x0
, x1
, y0
, y1
;
1272 x0
= fz_min (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1273 x1
= fz_max (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1275 y0
= fz_min (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1276 y1
= fz_max (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1278 a
.n
= dest
->ld
.gotor
.page
;
1283 static void recurse_outline (fz_outline
*outline
, int level
)
1286 switch (outline
->dest
.kind
) {
1289 struct anchor a
= desttoanchor (&outline
->dest
);
1292 printd ("o %d %d %d %d %s",
1293 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1299 printd ("ou %d %" FMT_s
" %s %s", level
,
1300 strlen (outline
->title
), outline
->title
,
1301 outline
->dest
.ld
.uri
.uri
);
1305 printd ("on %d %s", level
, outline
->title
);
1309 printd ("emsg Unhandled outline kind %d for %s\n",
1310 outline
->dest
.kind
, outline
->title
);
1313 if (outline
->down
) {
1314 recurse_outline (outline
->down
, level
+ 1);
1316 outline
= outline
->next
;
1320 static void process_outline (void)
1322 fz_outline
*outline
;
1324 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1326 state
.needoutline
= 0;
1327 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1329 recurse_outline (outline
, 0);
1330 fz_drop_outline (state
.ctx
, outline
);
1334 static char *strofspan (fz_stext_span
*span
)
1339 size_t size
= 0, cap
= 80;
1341 p
= malloc (cap
+ 1);
1342 if (!p
) return NULL
;
1344 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1345 int n
= fz_runetochar (utf8
, ch
->c
);
1346 if (size
+ n
> cap
) {
1348 p
= realloc (p
, cap
+ 1);
1349 if (!p
) return NULL
;
1352 memcpy (p
+ size
, utf8
, n
);
1359 static int matchspan (regex_t
*re
, fz_stext_span
*span
,
1360 int stop
, int pageno
, double start
)
1367 fz_point p1
, p2
, p3
, p4
;
1369 p
= strofspan (span
);
1372 ret
= regexec (re
, p
, 1, &rm
, 0);
1375 if (ret
!= REG_NOMATCH
) {
1378 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1379 printd ("msg regexec error `%.*s'",
1380 (int) size
, errbuf
);
1388 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1389 c
+= fz_runelen (span
->text
[a
].c
);
1391 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1392 c
+= fz_runelen (span
->text
[b
].c
);
1395 if (fz_runelen (span
->text
[b
].c
) > 1) {
1396 b
= fz_maxi (0, b
-1);
1399 fz_stext_char_bbox (state
.ctx
, &sb
, span
, a
);
1400 fz_stext_char_bbox (state
.ctx
, &eb
, span
, b
);
1412 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1419 printd ("progress 1 found at %d `%.*s' in %f sec",
1420 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1424 printd ("match %d %d %f %f %f %f %f %f %f %f",
1436 static int compareblocks (const void *l
, const void *r
)
1438 fz_stext_block
const *ls
= l
;
1439 fz_stext_block
const *rs
= r
;
1440 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1443 /* wishful thinking function */
1444 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1448 fz_stext_page
*text
;
1449 fz_stext_sheet
*sheet
;
1450 struct pagedim
*pdim
, *pdimprev
;
1451 int stop
= 0, niters
= 0;
1456 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1457 if (niters
++ == 5) {
1460 printd ("progress 1 attention requested aborting search at %d",
1465 printd ("progress %f searching in page %d",
1466 (double) (pageno
+ 1) / state
.pagecount
,
1471 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1472 pdim
= &state
.pagedims
[i
];
1473 if (pdim
->pageno
== pageno
) {
1476 if (pdim
->pageno
> pageno
) {
1485 sheet
= fz_new_stext_sheet (state
.ctx
);
1486 text
= fz_new_stext_page (state
.ctx
);
1487 tdev
= fz_new_stext_device (state
.ctx
, sheet
, text
);
1489 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1491 fz_matrix ctm
= pagectm1 (page
, pdim
);
1492 fz_run_page (state
.ctx
, page
, tdev
, &ctm
, NULL
);
1495 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1496 fz_drop_device (state
.ctx
, tdev
);
1498 for (j
= 0; j
< text
->len
; ++j
) {
1501 fz_stext_block
*block
;
1503 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1504 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1507 for (k
= 0; k
< block
->len
; ++k
) {
1508 fz_stext_line
*line
;
1509 fz_stext_span
*span
;
1512 line
= &block
->lines
[k
];
1513 if (line
->bbox
.y0
< y
+ 1) continue;
1516 line
= &block
->lines
[block
->len
- 1 - k
];
1517 if (line
->bbox
.y0
> y
- 1) continue;
1520 for (span
= line
->first_span
; span
; span
= span
->next
) {
1521 switch (matchspan (re
, span
, stop
, pageno
, start
)) {
1523 case 1: stop
= 1; break;
1524 case -1: stop
= 1; goto endloop
;
1538 fz_drop_stext_page (state
.ctx
, text
);
1539 fz_drop_stext_sheet (state
.ctx
, sheet
);
1540 fz_drop_page (state
.ctx
, page
);
1544 printd ("progress 1 no matches %f sec", end
- start
);
1546 printd ("clearrects");
1549 static void set_tex_params (int colorspace
)
1556 switch (colorspace
) {
1558 state
.texiform
= GL_RGBA8
;
1559 state
.texform
= GL_RGBA
;
1560 state
.texty
= GL_UNSIGNED_BYTE
;
1561 state
.colorspace
= fz_device_rgb (state
.ctx
);
1564 state
.texiform
= GL_RGBA8
;
1565 state
.texform
= GL_BGRA
;
1566 state
.texty
= endianness
.s
> 1
1567 ? GL_UNSIGNED_INT_8_8_8_8
1568 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1569 state
.colorspace
= fz_device_bgr (state
.ctx
);
1572 state
.texiform
= GL_LUMINANCE_ALPHA
;
1573 state
.texform
= GL_LUMINANCE_ALPHA
;
1574 state
.texty
= GL_UNSIGNED_BYTE
;
1575 state
.colorspace
= fz_device_gray (state
.ctx
);
1578 errx (1, "invalid colorspce %d", colorspace
);
1582 static void realloctexts (int texcount
)
1586 if (texcount
== state
.texcount
) return;
1588 if (texcount
< state
.texcount
) {
1589 glDeleteTextures (state
.texcount
- texcount
,
1590 state
.texids
+ texcount
);
1593 size
= texcount
* sizeof (*state
.texids
);
1594 state
.texids
= realloc (state
.texids
, size
);
1595 if (!state
.texids
) {
1596 err (1, "realloc texids %" FMT_s
, size
);
1599 size
= texcount
* sizeof (*state
.texowners
);
1600 state
.texowners
= realloc (state
.texowners
, size
);
1601 if (!state
.texowners
) {
1602 err (1, "realloc texowners %" FMT_s
, size
);
1604 if (texcount
> state
.texcount
) {
1607 glGenTextures (texcount
- state
.texcount
,
1608 state
.texids
+ state
.texcount
);
1609 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1610 state
.texowners
[i
].w
= -1;
1611 state
.texowners
[i
].slice
= NULL
;
1614 state
.texcount
= texcount
;
1618 static char *mbtoutf8 (char *s
)
1624 len
= mbstowcs (NULL
, s
, strlen (s
));
1629 if (len
== (size_t) -1) {
1634 tmp
= malloc (len
* sizeof (wchar_t));
1639 ret
= mbstowcs (tmp
, s
, len
);
1640 if (ret
== (size_t) -1) {
1646 for (i
= 0; i
< ret
; ++i
) {
1647 len
+= fz_runelen (tmp
[i
]);
1650 p
= r
= malloc (len
+ 1);
1656 for (i
= 0; i
< ret
; ++i
) {
1657 p
+= fz_runetochar (p
, tmp
[i
]);
1664 CAMLprim value
ml_mbtoutf8 (value s_v
)
1670 s
= String_val (s_v
);
1676 ret_v
= caml_copy_string (r
);
1682 static void * mainloop (void UNUSED_ATTR
*unused
)
1685 int len
, ret
, oldlen
= 0;
1690 len
= readlen (state
.csock
);
1692 errx (1, "readlen returned 0");
1695 if (oldlen
< len
+ 1) {
1696 p
= realloc (p
, len
+ 1);
1698 err (1, "realloc %d failed", len
+ 1);
1702 readdata (state
.csock
, p
, len
);
1705 if (!strncmp ("open", p
, 4)) {
1706 int wthack
, off
, ok
= 0;
1713 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1715 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1718 filename
= p
+ 5 + off
;
1719 filenamelen
= strlen (filename
);
1720 password
= filename
+ filenamelen
+ 1;
1723 fz_try (state
.ctx
) {
1724 ok
= openxref (filename
, password
);
1726 fz_catch (state
.ctx
) {
1727 utf8filename
= mbtoutf8 (filename
);
1728 printd ("msg Could not open %s", utf8filename
);
1738 utf8filename
= mbtoutf8 (filename
);
1739 printd ("msg Opened %s (press h/F1 to get help)",
1741 if (utf8filename
!= filename
) {
1742 free (utf8filename
);
1745 state
.needoutline
= 1;
1748 else if (!strncmp ("cs", p
, 2)) {
1751 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1753 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1756 set_tex_params (colorspace
);
1757 for (i
= 0; i
< state
.texcount
; ++i
) {
1758 state
.texowners
[i
].w
= -1;
1759 state
.texowners
[i
].slice
= NULL
;
1763 else if (!strncmp ("freepage", p
, 8)) {
1766 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1768 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1772 else if (!strncmp ("freetile", p
, 8)) {
1775 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1777 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1781 else if (!strncmp ("search", p
, 6)) {
1782 int icase
, pageno
, y
, len2
, forward
;
1786 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1787 &icase
, &pageno
, &y
, &forward
, &len2
);
1789 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1792 pattern
= p
+ 6 + len2
;
1793 ret
= regcomp (&re
, pattern
,
1794 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1799 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1800 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1803 search (&re
, pageno
, y
, forward
);
1807 else if (!strncmp ("geometry", p
, 8)) {
1811 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1813 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1821 for (i
= 0; i
< state
.texcount
; ++i
) {
1822 state
.texowners
[i
].slice
= NULL
;
1825 state
.fitmodel
= fitmodel
;
1830 unlock ("geometry");
1831 printd ("continue %d", state
.pagecount
);
1833 else if (!strncmp ("reqlayout", p
, 9)) {
1836 unsigned int fitmodel
;
1837 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1840 ret
= sscanf (p
+ 9, " %d %u %d %n",
1841 &rotate
, &fitmodel
, &h
, &off
);
1843 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1846 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1849 state
.rotate
= rotate
;
1850 state
.fitmodel
= fitmodel
;
1855 nameddest
= p
+ 9 + off
;
1856 if (pdf
&& nameddest
&& *nameddest
) {
1859 pdf_obj
*needle
, *obj
;
1861 needle
= pdf_new_string (state
.ctx
, pdf
, nameddest
,
1862 strlen (nameddest
));
1863 obj
= pdf_lookup_dest (state
.ctx
, pdf
, needle
);
1865 dest
= pdf_parse_link_dest (state
.ctx
, pdf
,
1868 a
= desttoanchor (&dest
);
1870 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1873 printd ("emsg failed to parse destination `%s'\n",
1878 printd ("emsg destination `%s' not found\n",
1881 pdf_drop_obj (state
.ctx
, needle
);
1885 unlock ("reqlayout");
1886 printd ("continue %d", state
.pagecount
);
1888 else if (!strncmp ("page", p
, 4)) {
1893 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1895 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1900 page
= loadpage (pageno
, pindex
);
1904 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
1906 else if (!strncmp ("tile", p
, 4)) {
1913 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
1914 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
1915 SCN_ptr_cast (&data
));
1917 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1922 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1926 printd ("tile %d %d %" FMT_ptr
" %u %f",
1928 FMT_ptr_cast (tile
),
1929 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1932 else if (!strncmp ("trimset", p
, 7)) {
1936 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1937 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1939 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1942 state
.trimmargins
= trimmargins
;
1943 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1945 state
.trimfuzz
= fuzz
;
1949 else if (!strncmp ("settrim", p
, 7)) {
1953 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1954 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1956 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1960 state
.trimmargins
= trimmargins
;
1961 state
.needoutline
= 1;
1962 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1964 state
.trimfuzz
= fuzz
;
1966 state
.pagedimcount
= 0;
1967 free (state
.pagedims
);
1968 state
.pagedims
= NULL
;
1973 printd ("continue %d", state
.pagecount
);
1975 else if (!strncmp ("sliceh", p
, 6)) {
1978 ret
= sscanf (p
+ 6, " %d", &h
);
1980 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1982 if (h
!= state
.sliceheight
) {
1985 state
.sliceheight
= h
;
1986 for (i
= 0; i
< state
.texcount
; ++i
) {
1987 state
.texowners
[i
].w
= -1;
1988 state
.texowners
[i
].h
= -1;
1989 state
.texowners
[i
].slice
= NULL
;
1993 else if (!strncmp ("interrupt", p
, 9)) {
1994 printd ("vmsg interrupted");
1997 errx (1, "unknown command %.*s", len
, p
);
2003 CAMLprim value
ml_realloctexts (value texcount_v
)
2005 CAMLparam1 (texcount_v
);
2008 if (trylock (__func__
)) {
2012 realloctexts (Int_val (texcount_v
));
2017 CAMLreturn (Val_bool (ok
));
2020 static void recti (int x0
, int y0
, int x1
, int y1
)
2022 GLfloat
*v
= state
.vertices
;
2024 glVertexPointer (2, GL_FLOAT
, 0, v
);
2025 v
[0] = x0
; v
[1] = y0
;
2026 v
[2] = x1
; v
[3] = y0
;
2027 v
[4] = x0
; v
[5] = y1
;
2028 v
[6] = x1
; v
[7] = y1
;
2029 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2032 static void showsel (struct page
*page
, int ox
, int oy
)
2037 fz_stext_line
*line
;
2038 fz_page_block
*pageb
;
2039 fz_stext_block
*block
;
2040 struct mark first
, last
;
2041 unsigned char selcolor
[] = {15,15,15,140};
2043 first
= page
->fmark
;
2046 if (!first
.span
|| !last
.span
) return;
2048 glEnable (GL_BLEND
);
2049 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2050 glColor4ubv (selcolor
);
2052 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2053 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2054 for (pageb
= page
->text
->blocks
;
2055 pageb
< page
->text
->blocks
+ page
->text
->len
;
2057 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2058 block
= pageb
->u
.text
;
2060 for (line
= block
->lines
;
2061 line
< block
->lines
+ block
->len
;
2063 fz_stext_span
*span
;
2064 rect
= fz_empty_rect
;
2066 for (span
= line
->first_span
; span
; span
= span
->next
) {
2068 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2073 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2075 j
= fz_mini (first
.i
, last
.i
);
2076 k
= fz_maxi (first
.i
, last
.i
);
2079 if (span
== first
.span
) {
2083 else if (span
== last
.span
) {
2090 for (i
= j
; i
<= k
; ++i
) {
2092 fz_union_rect (&rect
,
2093 fz_stext_char_bbox (state
.ctx
, &bbox1
,
2096 fz_round_rect (&bbox
, &rect
);
2097 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2104 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2105 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2106 if (span
== last
.span
) {
2109 rect
= fz_empty_rect
;
2115 glDisable (GL_BLEND
);
2120 static void stipplerect (fz_matrix
*m
,
2128 fz_transform_point (p1
, m
);
2129 fz_transform_point (p2
, m
);
2130 fz_transform_point (p3
, m
);
2131 fz_transform_point (p4
, m
);
2137 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2141 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2143 texcoords
[0] = 0; vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2144 texcoords
[1] = t
; vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2146 texcoords
[2] = 0; vertices
[4] = p2
->x
; vertices
[5] = p2
->y
;
2147 texcoords
[3] = s
; vertices
[6] = p3
->x
; vertices
[7] = p3
->y
;
2149 texcoords
[4] = 0; vertices
[8] = p3
->x
; vertices
[9] = p3
->y
;
2150 texcoords
[5] = t
; vertices
[10] = p4
->x
; vertices
[11] = p4
->y
;
2152 texcoords
[6] = 0; vertices
[12] = p4
->x
; vertices
[13] = p4
->y
;
2153 texcoords
[7] = s
; vertices
[14] = p1
->x
; vertices
[15] = p1
->y
;
2155 glDrawArrays (GL_LINES
, 0, 8);
2158 static void solidrect (fz_matrix
*m
,
2165 fz_transform_point (p1
, m
);
2166 fz_transform_point (p2
, m
);
2167 fz_transform_point (p3
, m
);
2168 fz_transform_point (p4
, m
);
2169 vertices
[0] = p1
->x
; vertices
[1] = p1
->y
;
2170 vertices
[2] = p2
->x
; vertices
[3] = p2
->y
;
2172 vertices
[4] = p3
->x
; vertices
[5] = p3
->y
;
2173 vertices
[6] = p4
->x
; vertices
[7] = p4
->y
;
2174 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
2177 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2180 fz_matrix ctm
, tm
, pm
;
2181 fz_link
*link
, *links
;
2182 GLfloat
*texcoords
= state
.texcoords
;
2183 GLfloat
*vertices
= state
.vertices
;
2185 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2187 glEnable (GL_TEXTURE_1D
);
2188 glEnable (GL_BLEND
);
2189 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2190 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2192 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2193 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2194 fz_translate (&tm
, xoff
, yoff
);
2195 pm
= pagectm (page
);
2196 fz_concat (&ctm
, &pm
, &tm
);
2198 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2199 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2201 for (link
= links
; link
; link
= link
->next
) {
2202 fz_point p1
, p2
, p3
, p4
;
2204 p1
.x
= link
->rect
.x0
;
2205 p1
.y
= link
->rect
.y0
;
2207 p2
.x
= link
->rect
.x1
;
2208 p2
.y
= link
->rect
.y0
;
2210 p3
.x
= link
->rect
.x1
;
2211 p3
.y
= link
->rect
.y1
;
2213 p4
.x
= link
->rect
.x0
;
2214 p4
.y
= link
->rect
.y1
;
2216 switch (link
->dest
.kind
) {
2217 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2218 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2219 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2220 default: glColor3ub (0, 0, 0); break;
2222 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2225 for (i
= 0; i
< page
->annotcount
; ++i
) {
2226 fz_point p1
, p2
, p3
, p4
;
2227 struct annot
*annot
= &page
->annots
[i
];
2229 p1
.x
= annot
->bbox
.x0
;
2230 p1
.y
= annot
->bbox
.y0
;
2232 p2
.x
= annot
->bbox
.x1
;
2233 p2
.y
= annot
->bbox
.y0
;
2235 p3
.x
= annot
->bbox
.x1
;
2236 p3
.y
= annot
->bbox
.y1
;
2238 p4
.x
= annot
->bbox
.x0
;
2239 p4
.y
= annot
->bbox
.y1
;
2241 glColor3ub (0, 0, 128);
2242 stipplerect (&ctm
, &p1
, &p2
, &p3
, &p4
, texcoords
, vertices
);
2245 glDisable (GL_BLEND
);
2246 glDisable (GL_TEXTURE_1D
);
2249 static int compareslinks (const void *l
, const void *r
)
2251 struct slink
const *ls
= l
;
2252 struct slink
const *rs
= r
;
2253 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2254 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2256 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2259 static void droptext (struct page
*page
)
2262 fz_drop_stext_page (state
.ctx
, page
->text
);
2265 page
->fmark
.span
= NULL
;
2266 page
->lmark
.span
= NULL
;
2270 fz_drop_stext_sheet (state
.ctx
, page
->sheet
);
2275 static void dropannots (struct page
*page
)
2278 free (page
->annots
);
2279 page
->annots
= NULL
;
2280 page
->annotcount
= 0;
2284 static void ensureannots (struct page
*page
)
2287 size_t annotsize
= sizeof (*page
->annots
);
2290 if (state
.gen
!= page
->agen
) {
2292 page
->agen
= state
.gen
;
2294 if (page
->annots
) return;
2296 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
);
2298 annot
= fz_next_annot (state
.ctx
, annot
)) {
2303 page
->annotcount
= count
;
2304 page
->annots
= calloc (count
, annotsize
);
2305 if (!page
->annots
) {
2306 err (1, "calloc annots %d", count
);
2309 for (annot
= fz_first_annot (state
.ctx
, page
->fzpage
), i
= 0;
2311 annot
= fz_next_annot (state
.ctx
, annot
), i
++) {
2314 fz_bound_annot (state
.ctx
, annot
, &rect
);
2315 page
->annots
[i
].annot
= annot
;
2316 fz_round_rect (&page
->annots
[i
].bbox
, &rect
);
2321 static void dropslinks (struct page
*page
)
2324 free (page
->slinks
);
2325 page
->slinks
= NULL
;
2326 page
->slinkcount
= 0;
2330 static void ensureslinks (struct page
*page
)
2334 size_t slinksize
= sizeof (*page
->slinks
);
2335 fz_link
*link
, *links
;
2337 ensureannots (page
);
2338 if (state
.gen
!= page
->sgen
) {
2340 page
->sgen
= state
.gen
;
2342 if (page
->slinks
) return;
2344 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2345 ctm
= pagectm (page
);
2347 count
= page
->annotcount
;
2348 for (link
= links
; link
; link
= link
->next
) {
2354 page
->slinkcount
= count
;
2355 page
->slinks
= calloc (count
, slinksize
);
2356 if (!page
->slinks
) {
2357 err (1, "calloc slinks %d", count
);
2360 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2364 fz_transform_rect (&rect
, &ctm
);
2365 page
->slinks
[i
].tag
= SLINK
;
2366 page
->slinks
[i
].u
.link
= link
;
2367 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2369 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
2371 fz_bound_annot (state
.ctx
, page
->annots
[j
].annot
, &rect
);
2372 fz_transform_rect (&rect
, &ctm
);
2373 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2375 page
->slinks
[i
].tag
= SANNOT
;
2376 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
2378 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2382 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2383 static void fmt_linkn (char *s
, unsigned int u
)
2385 unsigned int len
; unsigned int q
;
2386 unsigned int zma
= 'z' - 'a' + 1;
2388 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2391 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2392 /* handles u == 0 */
2397 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2398 int noff
, char *targ
, int tlen
, int hfsize
)
2402 struct slink
*slink
;
2403 double x0
, y0
, x1
, y1
, w
;
2405 ensureslinks (page
);
2406 glColor3ub (0xc3, 0xb0, 0x91);
2407 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2408 fmt_linkn (buf
, i
+ noff
);
2409 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2410 slink
= &page
->slinks
[i
];
2412 x0
= slink
->bbox
.x0
+ xoff
- 5;
2413 y1
= slink
->bbox
.y0
+ yoff
- 5;
2414 y0
= y1
+ 10 + hfsize
;
2415 w
= measure_string (state
.face
, hfsize
, buf
);
2417 recti (x0
, y0
, x1
, y1
);
2421 glEnable (GL_BLEND
);
2422 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2423 glEnable (GL_TEXTURE_2D
);
2424 glColor3ub (0, 0, 0);
2425 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2426 fmt_linkn (buf
, i
+ noff
);
2427 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2428 slink
= &page
->slinks
[i
];
2430 x0
= slink
->bbox
.x0
+ xoff
;
2431 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2432 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2435 glDisable (GL_TEXTURE_2D
);
2436 glDisable (GL_BLEND
);
2439 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2442 struct slice
*slice1
;
2443 unsigned char *texdata
;
2446 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2447 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2449 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2450 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2451 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2455 int texindex
= state
.texindex
++ % state
.texcount
;
2457 if (state
.texowners
[texindex
].w
== tile
->w
) {
2458 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2462 state
.texowners
[texindex
].h
= slice
->h
;
2466 state
.texowners
[texindex
].h
= slice
->h
;
2469 state
.texowners
[texindex
].w
= tile
->w
;
2470 state
.texowners
[texindex
].slice
= slice
;
2471 slice
->texindex
= texindex
;
2473 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2474 #if TEXT_TYPE == GL_TEXTURE_2D
2475 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2476 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2477 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2478 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2481 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2485 texdata
= tile
->pixmap
->samples
;
2488 glTexSubImage2D (TEXT_TYPE
,
2500 glTexImage2D (TEXT_TYPE
,
2512 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2517 CAMLprim value
ml_begintiles (value unit_v
)
2519 CAMLparam1 (unit_v
);
2520 glEnable (TEXT_TYPE
);
2521 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2522 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2523 CAMLreturn (unit_v
);
2526 CAMLprim value
ml_endtiles (value unit_v
)
2528 CAMLparam1 (unit_v
);
2529 glDisable (TEXT_TYPE
);
2530 CAMLreturn (unit_v
);
2533 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2535 CAMLparam2 (args_v
, ptr_v
);
2536 int dispx
= Int_val (Field (args_v
, 0));
2537 int dispy
= Int_val (Field (args_v
, 1));
2538 int dispw
= Int_val (Field (args_v
, 2));
2539 int disph
= Int_val (Field (args_v
, 3));
2540 int tilex
= Int_val (Field (args_v
, 4));
2541 int tiley
= Int_val (Field (args_v
, 5));
2542 char *s
= String_val (ptr_v
);
2543 struct tile
*tile
= parse_pointer (__func__
, s
);
2544 int slicey
, firstslice
;
2545 struct slice
*slice
;
2546 GLfloat
*texcoords
= state
.texcoords
;
2547 GLfloat
*vertices
= state
.vertices
;
2549 firstslice
= tiley
/ tile
->sliceheight
;
2550 slice
= &tile
->slices
[firstslice
];
2551 slicey
= tiley
% tile
->sliceheight
;
2556 dh
= slice
->h
- slicey
;
2557 dh
= fz_mini (disph
, dh
);
2558 uploadslice (tile
, slice
);
2560 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2561 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2562 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2563 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2565 vertices
[0] = dispx
; vertices
[1] = dispy
;
2566 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2567 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2568 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2570 #if TEXT_TYPE == GL_TEXTURE_2D
2571 for (int i
= 0; i
< 8; ++i
) {
2572 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2576 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2580 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2583 CAMLreturn (Val_unit
);
2586 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2588 fz_matrix ctm
, tm
, pm
;
2589 fz_point p1
, p2
, p3
, p4
;
2590 GLfloat
*vertices
= state
.vertices
;
2591 double *v
= (double *) rects_v
;
2593 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2594 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2595 fz_translate (&tm
, xoff
, yoff
);
2596 pm
= pagectm (page
);
2597 fz_concat (&ctm
, &pm
, &tm
);
2599 glEnable (GL_BLEND
);
2600 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2614 solidrect (&ctm
, &p1
, &p2
, &p3
, &p4
, vertices
);
2615 glDisable (GL_BLEND
);
2618 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2619 value xoff_v
, value yoff_v
,
2622 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2623 int xoff
= Int_val (xoff_v
);
2624 int yoff
= Int_val (yoff_v
);
2625 int noff
= Int_val (Field (li_v
, 0));
2626 char *targ
= String_val (Field (li_v
, 1));
2627 int tlen
= caml_string_length (Field (li_v
, 1));
2628 int hfsize
= Int_val (Field (li_v
, 2));
2629 char *s
= String_val (ptr_v
);
2630 int hlmask
= Int_val (hlinks_v
);
2631 struct page
*page
= parse_pointer (__func__
, s
);
2633 if (!page
->fzpage
) {
2634 /* deal with loadpage failed pages */
2638 ensureannots (page
);
2640 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2641 if (trylock (__func__
)) {
2646 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2647 noff
= page
->slinkcount
;
2649 if (page
->tgen
== state
.gen
) {
2650 showsel (page
, xoff
, yoff
);
2655 CAMLreturn (Val_int (noff
));
2658 CAMLprim value
ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
,
2661 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2662 int xoff
= Int_val (xoff_v
);
2663 int yoff
= Int_val (yoff_v
);
2664 char *s
= String_val (ptr_v
);
2665 struct page
*page
= parse_pointer (__func__
, s
);
2667 drawprect (page
, xoff
, yoff
, rects_v
);
2668 CAMLreturn (Val_unit
);
2671 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2676 const fz_matrix
*tctm
;
2677 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2679 if (!page
->annots
) return NULL
;
2682 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
2683 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2686 tctm
= &fz_identity
;
2692 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2693 fz_invert_matrix (&ctm
, &ctm
);
2694 fz_transform_point (&p
, &ctm
);
2697 for (i
= 0; i
< page
->annotcount
; ++i
) {
2698 struct annot
*a
= &page
->annots
[i
];
2699 pdf_annot
*annot
= (pdf_annot
*) a
->annot
;
2700 if (p
.x
>= annot
->pagerect
.x0
&& p
.x
<= annot
->pagerect
.x1
) {
2701 if (p
.y
>= annot
->pagerect
.y0
&& p
.y
<= annot
->pagerect
.y1
) {
2710 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2714 fz_link
*link
, *links
;
2716 links
= fz_load_links (state
.ctx
, page
->fzpage
);
2721 ctm
= pagectm (page
);
2722 fz_invert_matrix (&ctm
, &ctm
);
2723 fz_transform_point (&p
, &ctm
);
2725 for (link
= links
; link
; link
= link
->next
) {
2726 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2727 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2735 static void ensuretext (struct page
*page
)
2737 if (state
.gen
!= page
->tgen
) {
2739 page
->tgen
= state
.gen
;
2745 page
->text
= fz_new_stext_page (state
.ctx
);
2746 page
->sheet
= fz_new_stext_sheet (state
.ctx
);
2747 tdev
= fz_new_stext_device (state
.ctx
, page
->sheet
, page
->text
);
2748 ctm
= pagectm (page
);
2749 fz_run_display_list (state
.ctx
, page
->dlist
,
2750 tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2751 qsort (page
->text
->blocks
, page
->text
->len
,
2752 sizeof (*page
->text
->blocks
), compareblocks
);
2753 fz_drop_device (state
.ctx
, tdev
);
2757 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2759 CAMLparam2 (start_page_v
, dir_v
);
2761 int i
, dir
= Int_val (dir_v
);
2762 int start_page
= Int_val (start_page_v
);
2763 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2764 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2767 ret_v
= Val_int (0);
2769 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2774 pdf_page
*page
= NULL
;
2777 fz_try (state
.ctx
) {
2778 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2779 found
= !!page
->links
|| !!page
->annots
;
2781 fz_catch (state
.ctx
) {
2785 fz_drop_page (state
.ctx
, &page
->super
);
2789 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2790 found
= !!fz_load_links (state
.ctx
, page
);
2791 fz_drop_page (state
.ctx
, page
);
2795 ret_v
= caml_alloc_small (1, 1);
2796 Field (ret_v
, 0) = Val_int (i
);
2805 enum { dir_first
, dir_last
};
2806 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2808 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2810 CAMLparam2 (ptr_v
, dir_v
);
2811 CAMLlocal2 (ret_v
, pos_v
);
2813 int dirtag
, i
, slinkindex
;
2814 struct slink
*found
= NULL
,*slink
;
2815 char *s
= String_val (ptr_v
);
2817 page
= parse_pointer (__func__
, s
);
2818 ret_v
= Val_int (0);
2819 /* This is scary we are not taking locks here ensureslinks does
2820 not modify state and given that we obtained the page it can not
2821 disappear under us either */
2823 ensureslinks (page
);
2825 if (Is_block (dir_v
)) {
2826 dirtag
= Tag_val (dir_v
);
2828 case dir_first_visible
:
2830 int x0
, y0
, dir
, first_index
, last_index
;
2832 pos_v
= Field (dir_v
, 0);
2833 x0
= Int_val (Field (pos_v
, 0));
2834 y0
= Int_val (Field (pos_v
, 1));
2835 dir
= Int_val (Field (pos_v
, 2));
2840 last_index
= page
->slinkcount
;
2843 first_index
= page
->slinkcount
- 1;
2847 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2848 slink
= &page
->slinks
[i
];
2849 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2858 slinkindex
= Int_val (Field (dir_v
, 0));
2859 found
= &page
->slinks
[slinkindex
];
2860 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2861 slink
= &page
->slinks
[i
];
2862 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2870 slinkindex
= Int_val (Field (dir_v
, 0));
2871 found
= &page
->slinks
[slinkindex
];
2872 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2873 slink
= &page
->slinks
[i
];
2874 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2882 slinkindex
= Int_val (Field (dir_v
, 0));
2883 found
= &page
->slinks
[slinkindex
];
2884 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2885 slink
= &page
->slinks
[i
];
2886 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2894 slinkindex
= Int_val (Field (dir_v
, 0));
2895 found
= &page
->slinks
[slinkindex
];
2896 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2897 slink
= &page
->slinks
[i
];
2898 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2907 dirtag
= Int_val (dir_v
);
2910 found
= page
->slinks
;
2915 found
= page
->slinks
+ (page
->slinkcount
- 1);
2921 ret_v
= caml_alloc_small (2, 1);
2922 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2929 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
,
2930 unamed
, uremote
, uremotedest
, uannot
};
2936 switch (link->dest.kind) { \
2937 case FZ_LINK_GOTO: \
2941 pageno = link->dest.ld.gotor.page; \
2945 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2946 p.y = link->dest.ld.gotor.lt.y; \
2947 fz_transform_point (&p, &pdim->lctm); \
2948 if (p.y < 0) p.y = 0; \
2950 tup_v = caml_alloc_tuple (2); \
2951 ret_v = caml_alloc_small (1, ugoto); \
2952 Field (tup_v, 0) = Val_int (pageno); \
2953 Field (tup_v, 1) = Val_int (p.y); \
2954 Field (ret_v, 0) = tup_v; \
2959 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2960 ret_v = caml_alloc_small (1, uuri); \
2961 Field (ret_v, 0) = str_v; \
2964 case FZ_LINK_LAUNCH: \
2965 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2966 ret_v = caml_alloc_small (1, ulaunch); \
2967 Field (ret_v, 0) = str_v; \
2970 case FZ_LINK_NAMED: \
2971 str_v = caml_copy_string (link->dest.ld.named.named); \
2972 ret_v = caml_alloc_small (1, unamed); \
2973 Field (ret_v, 0) = str_v; \
2976 case FZ_LINK_GOTOR: \
2980 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2981 pageno = link->dest.ld.gotor.page; \
2982 if (pageno == -1) { \
2983 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2984 rty = uremotedest; \
2987 gr_v = Val_int (pageno); \
2990 tup_v = caml_alloc_tuple (2); \
2991 ret_v = caml_alloc_small (1, rty); \
2992 Field (tup_v, 0) = str_v; \
2993 Field (tup_v, 1) = gr_v; \
2994 Field (ret_v, 0) = tup_v; \
3002 snprintf (buf, sizeof (buf), \
3003 "unhandled link kind %d", link->dest.kind); \
3004 str_v = caml_copy_string (buf); \
3005 ret_v = caml_alloc_small (1, uunexpected); \
3006 Field (ret_v, 0) = str_v; \
3012 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3014 CAMLparam2 (ptr_v
, n_v
);
3015 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3018 struct pagedim
*pdim
;
3019 char *s
= String_val (ptr_v
);
3020 struct slink
*slink
;
3022 /* See ml_findlink for caveat */
3024 ret_v
= Val_int (0);
3025 page
= parse_pointer (__func__
, s
);
3026 ensureslinks (page
);
3027 pdim
= &state
.pagedims
[page
->pdimno
];
3028 slink
= &page
->slinks
[Int_val (n_v
)];
3029 if (slink
->tag
== SLINK
) {
3030 link
= slink
->u
.link
;
3034 ret_v
= caml_alloc_small (1, uannot
);
3035 tup_v
= caml_alloc_tuple (2);
3036 Field (ret_v
, 0) = tup_v
;
3037 Field (tup_v
, 0) = ptr_v
;
3038 Field (tup_v
, 1) = n_v
;
3044 CAMLprim value
ml_getannotcontents (value ptr_v
, value n_v
)
3046 CAMLparam2 (ptr_v
, n_v
);
3047 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3049 char *s
= String_val (ptr_v
);
3051 struct slink
*slink
;
3053 page
= parse_pointer (__func__
, s
);
3054 slink
= &page
->slinks
[Int_val (n_v
)];
3055 CAMLreturn (caml_copy_string (
3056 pdf_annot_contents (state
.ctx
,
3057 (pdf_annot
*) slink
->u
.annot
)));
3060 CAMLreturn (caml_copy_string (""));
3064 CAMLprim value
ml_getlinkcount (value ptr_v
)
3068 char *s
= String_val (ptr_v
);
3070 page
= parse_pointer (__func__
, s
);
3071 CAMLreturn (Val_int (page
->slinkcount
));
3074 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3076 CAMLparam2 (ptr_v
, n_v
);
3079 struct slink
*slink
;
3080 char *s
= String_val (ptr_v
);
3081 /* See ml_findlink for caveat */
3083 page
= parse_pointer (__func__
, s
);
3084 ret_v
= caml_alloc_tuple (4);
3085 ensureslinks (page
);
3087 slink
= &page
->slinks
[Int_val (n_v
)];
3088 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3089 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3090 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3091 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3096 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3098 CAMLparam3 (ptr_v
, x_v
, y_v
);
3099 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3101 struct annot
*annot
;
3103 char *ptr
= String_val (ptr_v
);
3104 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3105 struct pagedim
*pdim
;
3107 ret_v
= Val_int (0);
3108 if (trylock (__func__
)) {
3112 page
= parse_pointer (__func__
, ptr
);
3113 pdim
= &state
.pagedims
[page
->pdimno
];
3114 x
+= pdim
->bounds
.x0
;
3115 y
+= pdim
->bounds
.y0
;
3118 annot
= getannot (page
, x
, y
);
3122 ensureslinks (page
);
3123 for (i
= 0; i
< page
->slinkcount
; ++i
) {
3124 if (page
->slinks
[i
].tag
== SANNOT
3125 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
3130 ret_v
= caml_alloc_small (1, uannot
);
3131 tup_v
= caml_alloc_tuple (2);
3132 Field (ret_v
, 0) = tup_v
;
3133 Field (tup_v
, 0) = ptr_v
;
3134 Field (tup_v
, 1) = Val_int (n
);
3139 link
= getlink (page
, x
, y
);
3145 fz_page_block
*pageb
;
3146 fz_stext_block
*block
;
3149 for (pageb
= page
->text
->blocks
;
3150 pageb
< page
->text
->blocks
+ page
->text
->len
;
3152 fz_stext_line
*line
;
3153 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3154 block
= pageb
->u
.text
;
3157 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3160 for (line
= block
->lines
;
3161 line
< block
->lines
+ block
->len
;
3163 fz_stext_span
*span
;
3166 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3169 for (span
= line
->first_span
; span
; span
= span
->next
) {
3173 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3176 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3178 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3181 if (x
>= b
->x0
&& x
<= b
->x1
3182 && y
>= b
->y0
&& y
<= b
->y1
) {
3183 fz_stext_style
*style
= span
->text
->style
;
3187 : "Span has no font name"
3189 FT_FaceRec
*face
= style
->font
->ft_face
;
3190 if (face
&& face
->family_name
) {
3192 char *n1
= face
->family_name
;
3193 size_t l1
= strlen (n1
);
3194 size_t l2
= strlen (n2
);
3196 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3197 s
= malloc (l1
+ l2
+ 2);
3201 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3202 str_v
= caml_copy_string (s
);
3207 if (str_v
== Val_unit
) {
3208 str_v
= caml_copy_string (n2
);
3210 ret_v
= caml_alloc_small (1, utext
);
3211 Field (ret_v
, 0) = str_v
;
3226 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3228 static int uninteresting (int c
)
3230 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3234 CAMLprim value
ml_clearmark (value ptr_v
)
3237 char *s
= String_val (ptr_v
);
3240 if (trylock (__func__
)) {
3244 page
= parse_pointer (__func__
, s
);
3245 page
->fmark
.span
= NULL
;
3246 page
->lmark
.span
= NULL
;
3252 CAMLreturn (Val_unit
);
3255 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3257 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3261 fz_stext_line
*line
;
3262 fz_page_block
*pageb
;
3263 fz_stext_block
*block
;
3264 struct pagedim
*pdim
;
3265 int mark
= Int_val (mark_v
);
3266 char *s
= String_val (ptr_v
);
3267 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3269 ret_v
= Val_bool (0);
3270 if (trylock (__func__
)) {
3274 page
= parse_pointer (__func__
, s
);
3275 pdim
= &state
.pagedims
[page
->pdimno
];
3279 if (mark
== mark_page
) {
3281 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3283 for (i
= 0; i
< page
->text
->len
; ++i
) {
3284 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3285 pb1
= &page
->text
->blocks
[i
];
3289 if (!pb1
) goto unlock
;
3291 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3292 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3293 pb2
= &page
->text
->blocks
[i
];
3297 if (!pb2
) goto unlock
;
3299 block
= pb1
->u
.text
;
3302 page
->fmark
.span
= block
->lines
->first_span
;
3304 block
= pb2
->u
.text
;
3305 line
= &block
->lines
[block
->len
- 1];
3306 page
->lmark
.i
= line
->last_span
->len
- 1;
3307 page
->lmark
.span
= line
->last_span
;
3308 ret_v
= Val_bool (1);
3312 x
+= pdim
->bounds
.x0
;
3313 y
+= pdim
->bounds
.y0
;
3315 for (pageb
= page
->text
->blocks
;
3316 pageb
< page
->text
->blocks
+ page
->text
->len
;
3318 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3319 block
= pageb
->u
.text
;
3322 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3325 if (mark
== mark_block
) {
3327 page
->fmark
.span
= block
->lines
->first_span
;
3329 line
= &block
->lines
[block
->len
- 1];
3330 page
->lmark
.i
= line
->last_span
->len
- 1;
3331 page
->lmark
.span
= line
->last_span
;
3332 ret_v
= Val_bool (1);
3336 for (line
= block
->lines
;
3337 line
< block
->lines
+ block
->len
;
3339 fz_stext_span
*span
;
3342 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3345 if (mark
== mark_line
) {
3347 page
->fmark
.span
= line
->first_span
;
3349 page
->lmark
.i
= line
->last_span
->len
- 1;
3350 page
->lmark
.span
= line
->last_span
;
3351 ret_v
= Val_bool (1);
3355 for (span
= line
->first_span
; span
; span
= span
->next
) {
3359 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3362 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3364 fz_stext_char_bbox (state
.ctx
, &bbox
, span
, charnum
);
3367 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3369 int charnum2
, charnum3
= -1, charnum4
= -1;
3371 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3373 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3374 if (uninteresting (span
->text
[charnum2
].c
)) {
3375 charnum3
= charnum2
+ 1;
3379 if (charnum3
== -1) charnum3
= 0;
3382 for (charnum2
= charnum
+ 1;
3383 charnum2
< span
->len
;
3385 if (uninteresting (span
->text
[charnum2
].c
)) break;
3386 charnum4
= charnum2
;
3389 page
->fmark
.i
= charnum3
;
3390 page
->fmark
.span
= span
;
3392 page
->lmark
.i
= charnum4
;
3393 page
->lmark
.span
= span
;
3394 ret_v
= Val_bool (1);
3402 if (!Bool_val (ret_v
)) {
3403 page
->fmark
.span
= NULL
;
3404 page
->lmark
.span
= NULL
;
3414 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3416 CAMLparam3 (ptr_v
, x_v
, y_v
);
3417 CAMLlocal2 (ret_v
, res_v
);
3420 fz_page_block
*pageb
;
3421 struct pagedim
*pdim
;
3422 char *s
= String_val (ptr_v
);
3423 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3425 ret_v
= Val_int (0);
3426 if (trylock (__func__
)) {
3430 page
= parse_pointer (__func__
, s
);
3431 pdim
= &state
.pagedims
[page
->pdimno
];
3432 x
+= pdim
->bounds
.x0
;
3433 y
+= pdim
->bounds
.y0
;
3437 for (pageb
= page
->text
->blocks
;
3438 pageb
< page
->text
->blocks
+ page
->text
->len
;
3440 switch (pageb
->type
) {
3441 case FZ_PAGE_BLOCK_TEXT
:
3442 b
= &pageb
->u
.text
->bbox
;
3445 case FZ_PAGE_BLOCK_IMAGE
:
3446 b
= &pageb
->u
.image
->bbox
;
3453 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3458 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3459 ret_v
= caml_alloc_small (1, 1);
3460 Store_double_field (res_v
, 0, b
->x0
);
3461 Store_double_field (res_v
, 1, b
->x1
);
3462 Store_double_field (res_v
, 2, b
->y0
);
3463 Store_double_field (res_v
, 3, b
->y1
);
3464 Field (ret_v
, 0) = res_v
;
3472 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3474 CAMLparam2 (ptr_v
, rect_v
);
3477 struct pagedim
*pdim
;
3478 char *s
= String_val (ptr_v
);
3479 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3480 fz_stext_line
*line
;
3481 fz_page_block
*pageb
;
3482 fz_stext_block
*block
;
3483 fz_stext_span
*span
, *fspan
, *lspan
;
3485 if (trylock (__func__
)) {
3489 page
= parse_pointer (__func__
, s
);
3492 pdim
= &state
.pagedims
[page
->pdimno
];
3493 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3494 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3495 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3496 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3507 fspan
= page
->fmark
.span
;
3510 lspan
= page
->lmark
.span
;
3512 for (pageb
= page
->text
->blocks
;
3513 pageb
< page
->text
->blocks
+ page
->text
->len
;
3515 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3516 block
= pageb
->u
.text
;
3517 for (line
= block
->lines
;
3518 line
< block
->lines
+ block
->len
;
3521 for (span
= line
->first_span
; span
; span
= span
->next
) {
3522 for (i
= 0; i
< span
->len
; ++i
) {
3523 fz_stext_char_bbox (state
.ctx
, &b
, span
, i
);
3525 if (x0
>= b
.x0
&& x0
<= b
.x1
3526 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3530 if (x1
>= b
.x0
&& x1
<= b
.x1
3531 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3539 if (x1
< x0
&& fspan
== lspan
) {
3551 page
->fmark
.span
= fspan
;
3554 page
->lmark
.span
= lspan
;
3559 CAMLreturn (Val_unit
);
3562 static int UNUSED_ATTR
pipespan (FILE *f
, fz_stext_span
*span
, int a
, int b
)
3567 for (i
= a
; i
<= b
; ++i
) {
3568 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3569 ret
= fwrite (buf
, len
, 1, f
);
3572 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3573 len
, ret
, strerror (errno
));
3581 CAMLprim value
ml_spawn (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3583 caml_failwith ("ml_popen not implemented under Cygwin");
3586 CAMLprim value
ml_spawn (value command_v
, value fds_v
)
3588 CAMLparam2 (command_v
, fds_v
);
3589 CAMLlocal2 (l_v
, tup_v
);
3593 value earg_v
= Nothing
;
3594 posix_spawnattr_t attr
;
3595 posix_spawn_file_actions_t fa
;
3596 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3598 argv
[2] = String_val (command_v
);
3600 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3601 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3604 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3605 msg
= "posix_spawnattr_init";
3609 #ifdef POSIX_SPAWN_USEVFORK
3610 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3611 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3616 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3619 tup_v
= Field (l_v
, 0);
3620 fd1
= Int_val (Field (tup_v
, 0));
3621 fd2
= Int_val (Field (tup_v
, 1));
3623 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3624 msg
= "posix_spawn_file_actions_addclose";
3630 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3631 msg
= "posix_spawn_file_actions_adddup2";
3638 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3639 msg
= "posix_spawn";
3644 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3645 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret1
));
3649 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3650 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3655 unix_error (ret
, msg
, earg_v
);
3657 CAMLreturn (Val_int (pid
));
3661 CAMLprim value
ml_hassel (value ptr_v
)
3666 char *s
= String_val (ptr_v
);
3668 ret_v
= Val_bool (0);
3669 if (trylock (__func__
)) {
3673 page
= parse_pointer (__func__
, s
);
3674 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3680 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3682 CAMLparam2 (fd_v
, ptr_v
);
3686 fz_stext_line
*line
;
3687 fz_page_block
*pageb
;
3688 fz_stext_block
*block
;
3689 int fd
= Int_val (fd_v
);
3690 char *s
= String_val (ptr_v
);
3692 if (trylock (__func__
)) {
3696 page
= parse_pointer (__func__
, s
);
3698 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3699 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3703 f
= fdopen (fd
, "w");
3705 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3706 fd
, strerror (errno
));
3710 for (pageb
= page
->text
->blocks
;
3711 pageb
< page
->text
->blocks
+ page
->text
->len
;
3713 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3714 block
= pageb
->u
.text
;
3715 for (line
= block
->lines
;
3716 line
< block
->lines
+ block
->len
;
3718 fz_stext_span
*span
;
3720 for (span
= line
->first_span
; span
; span
= span
->next
) {
3723 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3724 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3725 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3728 if (pipespan (f
, span
, a
, b
)) {
3731 if (span
== page
->lmark
.span
) {
3734 if (span
== line
->last_span
) {
3735 if (putc ('\n', f
) == EOF
) {
3737 "failed break line on sel pipe: %s\n",
3748 int ret
= fclose (f
);
3751 if (errno
!= ECHILD
) {
3752 fprintf (stderr
, "failed to close sel pipe: %s\n",
3763 fprintf (stderr
, "failed to close sel pipe: %s\n",
3767 CAMLreturn (Val_unit
);
3770 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3772 CAMLparam1 (pagedimno_v
);
3774 int pagedimno
= Int_val (pagedimno_v
);
3777 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3778 if (trylock (__func__
)) {
3779 box
= fz_empty_rect
;
3782 box
= state
.pagedims
[pagedimno
].mediabox
;
3786 Store_double_field (ret_v
, 0, box
.x0
);
3787 Store_double_field (ret_v
, 1, box
.x1
);
3788 Store_double_field (ret_v
, 2, box
.y0
);
3789 Store_double_field (ret_v
, 3, box
.y1
);
3794 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3795 value dw_v
, value cols_v
)
3797 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3803 double winw
= Int_val (winw_v
);
3804 double winh
= Int_val (winh_v
);
3805 double dw
= Int_val (dw_v
);
3806 double cols
= Int_val (cols_v
);
3807 double pw
= 1.0, ph
= 1.0;
3809 if (trylock (__func__
)) {
3813 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3814 double w
= p
->pagebox
.x1
/ cols
;
3815 double h
= p
->pagebox
.y1
;
3819 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3821 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3824 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3827 ret_v
= caml_copy_double (zoom
);
3831 CAMLprim value
ml_getmaxw (value unit_v
)
3833 CAMLparam1 (unit_v
);
3839 if (trylock (__func__
)) {
3843 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3844 double w
= p
->pagebox
.x1
;
3845 maxw
= fz_max (maxw
, w
);
3850 ret_v
= caml_copy_double (maxw
);
3854 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3856 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3858 int pt
= Int_val(pt_v
);
3859 int x
= Int_val (x_v
);
3860 int y
= Int_val (y_v
);
3863 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3864 ret_v
= caml_copy_double (w
);
3868 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3870 CAMLparam2 (pt_v
, string_v
);
3872 int pt
= Int_val (pt_v
);
3875 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3876 ret_v
= caml_copy_double (w
);
3880 CAMLprim value
ml_getpagebox (value opaque_v
)
3882 CAMLparam1 (opaque_v
);
3888 char *s
= String_val (opaque_v
);
3889 struct page
*page
= parse_pointer (__func__
, s
);
3891 ret_v
= caml_alloc_tuple (4);
3892 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3893 dev
->hints
|= FZ_IGNORE_SHADE
;
3895 ctm
= pagectm (page
);
3896 fz_run_page (state
.ctx
, page
->fzpage
, dev
, &ctm
, NULL
);
3898 fz_drop_device (state
.ctx
, dev
);
3899 fz_round_rect (&bbox
, &rect
);
3900 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3901 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3902 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3903 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3908 CAMLprim value
ml_setaalevel (value level_v
)
3910 CAMLparam1 (level_v
);
3912 state
.aalevel
= Int_val (level_v
);
3913 CAMLreturn (Val_unit
);
3916 #pragma GCC diagnostic push
3917 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3918 #include <X11/Xlib.h>
3919 #include <X11/cursorfont.h>
3920 #pragma GCC diagnostic pop
3923 #include <EGL/egl.h>
3928 static const int shapes
[] = {
3929 XC_left_ptr
, XC_hand2
, XC_exchange
, XC_fleur
, XC_xterm
3932 #define CURS_COUNT (sizeof (shapes) / sizeof (shapes[0]))
3945 XVisualInfo
*visual
;
3946 Cursor curs
[CURS_COUNT
];
3950 static VisualID
initvisual (void)
3952 /* On this system with: `Haswell-ULT Integrated Graphics
3953 Controller' and Mesa 11.0.6; perf stat reports [1] that when
3954 using glX chosen visual and auto scrolling some document in
3955 fullscreen the power/energy-gpu is more than 1 joule bigger
3956 than when using hand picked visual that stands alone in glxinfo
3957 output: it's dead last in the list and it's the only one with
3958 `visual dep' (sic) of 32
3960 No clue what's going on here...
3962 [1] perf stat -a -I 1200 -e "power/energy-gpu/"
3968 info
.class = TrueColor
;
3969 glx
.visual
= XGetVisualInfo (glx
.dpy
, VisualDepthMask
| VisualClassMask
,
3971 if (!ret
|| !glx
.visual
) {
3972 XCloseDisplay (glx
.dpy
);
3973 caml_failwith ("XGetVisualInfo");
3975 return glx
.visual
->visualid
;
3979 static void initcurs (void)
3981 for (size_t n
= 0; n
< CURS_COUNT
; ++n
) {
3982 glx
.curs
[n
] = XCreateFontCursor (glx
.dpy
, shapes
[n
]);
3986 CAMLprim
void ml_setbgcol (value color_v
)
3988 CAMLparam1 (color_v
);
3989 XSetWindowBackground (glx
.dpy
, glx
.wid
, Int_val (color_v
));
3994 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3996 CAMLparam3 (display_v
, wid_v
, screen_v
);
4000 EGLint attribs
[] = {
4002 EGL_NATIVE_VISUAL_ID
, 0,
4006 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
4007 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
4012 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4014 caml_failwith ("XOpenDisplay");
4017 eglBindAPI (EGL_OPENGL_API
);
4019 glx
.edpy
= eglGetDisplay (glx
.dpy
);
4020 if (glx
.edpy
== EGL_NO_DISPLAY
) {
4021 caml_failwith ("eglGetDisplay");
4024 if (!eglInitialize (glx
.edpy
, &major
, &minor
)) {
4025 caml_failwith ("eglInitialize");
4029 attribs
[1] = visid
= initvisual ();
4032 if (!eglChooseConfig (glx
.edpy
, attribs
, &conf
, 1, &num_conf
) ||
4034 caml_failwith ("eglChooseConfig");
4039 if (!eglGetConfigAttrib (glx
.edpy
, glx
.conf
,
4040 EGL_NATIVE_VISUAL_ID
, &visid
)) {
4041 caml_failwith ("eglGetConfigAttrib");
4046 glx
.wid
= Int_val (wid_v
);
4047 CAMLreturn (Val_int (visid
));
4050 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4052 CAMLparam1 (unit_v
);
4054 glx
.ctx
= eglCreateContext (glx
.edpy
, glx
.conf
, EGL_NO_CONTEXT
, NULL
);
4056 caml_failwith ("eglCreateContext");
4059 glx
.win
= eglCreateWindowSurface (glx
.edpy
, glx
.conf
,
4061 if (glx
.win
== EGL_NO_SURFACE
) {
4062 caml_failwith ("eglCreateWindowSurface");
4066 if (!eglMakeCurrent (glx
.edpy
, glx
.win
, glx
.win
, glx
.ctx
)) {
4068 caml_failwith ("eglMakeCurrent");
4070 CAMLreturn (Val_unit
);
4073 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
4075 CAMLparam3 (display_v
, wid_v
, screen_v
);
4077 glx
.dpy
= XOpenDisplay (String_val (display_v
));
4079 caml_failwith ("XOpenDisplay");
4085 int attribs
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
4086 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
4088 XCloseDisplay (glx
.dpy
);
4089 caml_failwith ("glXChooseVisual");
4094 glx
.wid
= Int_val (wid_v
);
4095 CAMLreturn (Val_int (glx
.visual
->visualid
));
4098 CAMLprim value
ml_glxcompleteinit (value unit_v
)
4100 CAMLparam1 (unit_v
);
4102 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
4104 caml_failwith ("glXCreateContext");
4110 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
4111 glXDestroyContext (glx
.dpy
, glx
.ctx
);
4113 caml_failwith ("glXMakeCurrent");
4115 CAMLreturn (Val_unit
);
4119 CAMLprim value
ml_setcursor (value cursor_v
)
4121 CAMLparam1 (cursor_v
);
4122 size_t cursn
= Int_val (cursor_v
);
4124 if (cursn
>= CURS_COUNT
) caml_failwith ("cursor index out of range");
4125 XDefineCursor (glx
.dpy
, glx
.wid
, glx
.curs
[cursn
]);
4127 CAMLreturn (Val_unit
);
4130 CAMLprim value
ml_swapb (value unit_v
)
4132 CAMLparam1 (unit_v
);
4134 if (!eglSwapBuffers (glx
.edpy
, glx
.win
)) {
4135 caml_failwith ("eglSwapBuffers");
4138 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4140 CAMLreturn (Val_unit
);
4143 #include "keysym2ucs.c"
4145 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4147 CAMLparam1 (keysym_v
);
4149 KeySym keysym
= Int_val (keysym_v
);
4154 rune
= keysym2ucs (keysym
);
4155 len
= fz_runetochar (buf
, rune
);
4157 str_v
= caml_copy_string (buf
);
4161 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4163 CAMLprim value
ml_platform (value unit_v
)
4165 CAMLparam1 (unit_v
);
4166 CAMLlocal2 (tup_v
, arr_v
);
4167 int platid
= piunknown
;
4170 #if defined __linux__
4172 #elif defined __CYGWIN__
4174 #elif defined __DragonFly__ || defined __FreeBSD__
4175 || defined __OpenBSD__
|| defined __NetBSD__
4177 #elif defined __sun__
4179 #elif defined __APPLE__
4182 if (uname (&buf
)) err (1, "uname");
4184 tup_v
= caml_alloc_tuple (2);
4186 char const *sar
[] = {
4193 arr_v
= caml_copy_string_array (sar
);
4195 Field (tup_v
, 0) = Val_int (platid
);
4196 Field (tup_v
, 1) = arr_v
;
4200 CAMLprim value
ml_cloexec (value fd_v
)
4203 int fd
= Int_val (fd_v
);
4205 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4206 uerror ("fcntl", Nothing
);
4208 CAMLreturn (Val_unit
);
4211 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4213 CAMLparam2 (w_v
, h_v
);
4216 int w
= Int_val (w_v
);
4217 int h
= Int_val (h_v
);
4218 int cs
= Int_val (cs_v
);
4220 if (state
.bo_usable
) {
4221 pbo
= calloc (sizeof (*pbo
), 1);
4223 err (1, "calloc pbo");
4235 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
4238 state
.glGenBuffersARB (1, &pbo
->id
);
4239 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4240 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4241 NULL
, GL_STREAM_DRAW
);
4242 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4244 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4246 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4247 state
.glDeleteBuffersARB (1, &pbo
->id
);
4249 ret_v
= caml_copy_string ("0");
4255 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4257 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4261 err (1, "malloc %d bytes failed", res
+1);
4263 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4265 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4267 ret_v
= caml_copy_string (s
);
4272 ret_v
= caml_copy_string ("0");
4277 CAMLprim value
ml_freepbo (value s_v
)
4280 char *s
= String_val (s_v
);
4281 struct tile
*tile
= parse_pointer (__func__
, s
);
4284 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4286 tile
->pbo
->ptr
= NULL
;
4287 tile
->pbo
->size
= -1;
4289 CAMLreturn (Val_unit
);
4292 CAMLprim value
ml_unmappbo (value s_v
)
4295 char *s
= String_val (s_v
);
4296 struct tile
*tile
= parse_pointer (__func__
, s
);
4299 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4300 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4301 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4303 tile
->pbo
->ptr
= NULL
;
4304 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4306 CAMLreturn (Val_unit
);
4309 static void setuppbo (void)
4312 #define GGPA(n) (*(void (**) ()) &state.n = eglGetProcAddress (#n))
4314 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4316 state
.bo_usable
= GGPA (glBindBufferARB
)
4317 && GGPA (glUnmapBufferARB
)
4318 && GGPA (glMapBufferARB
)
4319 && GGPA (glBufferDataARB
)
4320 && GGPA (glGenBuffersARB
)
4321 && GGPA (glDeleteBuffersARB
);
4325 CAMLprim value
ml_bo_usable (value unit_v
)
4327 CAMLparam1 (unit_v
);
4328 CAMLreturn (Val_bool (state
.bo_usable
));
4331 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4333 CAMLparam3 (ptr_v
, x_v
, y_v
);
4334 CAMLlocal2 (ret_v
, tup_v
);
4336 char *s
= String_val (ptr_v
);
4337 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4338 struct pagedim
*pdim
;
4342 page
= parse_pointer (__func__
, s
);
4343 pdim
= &state
.pagedims
[page
->pdimno
];
4345 ret_v
= Val_int (0);
4346 if (trylock (__func__
)) {
4350 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4351 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
4352 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4357 p
.x
= x
+ pdim
->bounds
.x0
;
4358 p
.y
= y
+ pdim
->bounds
.y0
;
4360 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4361 fz_invert_matrix (&ctm
, &ctm
);
4362 fz_transform_point (&p
, &ctm
);
4364 tup_v
= caml_alloc_tuple (2);
4365 ret_v
= caml_alloc_small (1, 1);
4366 Field (tup_v
, 0) = Val_int (p
.x
);
4367 Field (tup_v
, 1) = Val_int (p
.y
);
4368 Field (ret_v
, 0) = tup_v
;
4375 CAMLprim value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
4376 value x_v
, value y_v
)
4378 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
4381 char *s
= String_val (ptr_v
);
4382 int pageno
= Int_val (pageno_v
);
4383 int pdimno
= Int_val (pdimno_v
);
4384 double x
= Double_val (x_v
), y
= Double_val (y_v
);
4385 struct pagedim
*pdim
;
4389 ret_v
= Val_int (0);
4393 page
= loadpage (pageno
, pdimno
);
4396 page
= parse_pointer (__func__
, s
);
4398 pdim
= &state
.pagedims
[pdimno
];
4400 if (pdf_specifics (state
.ctx
, state
.doc
)) {
4401 trimctm ((pdf_page
*) page
->fzpage
, page
->pdimno
);
4402 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
4407 p
.x
= x
+ pdim
->bounds
.x0
;
4408 p
.y
= y
+ pdim
->bounds
.y0
;
4410 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4411 fz_transform_point (&p
, &ctm
);
4413 ret_v
= caml_alloc_tuple (2);
4414 Field (ret_v
, 0) = caml_copy_double (p
.x
);
4415 Field (ret_v
, 1) = caml_copy_double (p
.y
);
4424 CAMLprim value
ml_addannot (value ptr_v
, value x_v
, value y_v
,
4427 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
4428 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4434 char *s
= String_val (ptr_v
);
4436 page
= parse_pointer (__func__
, s
);
4437 annot
= pdf_create_annot (state
.ctx
, pdf
,
4438 (pdf_page
*) page
->fzpage
, FZ_ANNOT_TEXT
);
4439 p
.x
= Int_val (x_v
);
4440 p
.y
= Int_val (y_v
);
4441 pdf_set_annot_contents (state
.ctx
, pdf
, annot
, String_val (contents_v
));
4442 pdf_set_text_annot_position (state
.ctx
, pdf
, annot
, p
);
4445 CAMLreturn (Val_unit
);
4448 CAMLprim value
ml_delannot (value ptr_v
, value n_v
)
4450 CAMLparam2 (ptr_v
, n_v
);
4451 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4455 char *s
= String_val (ptr_v
);
4456 struct slink
*slink
;
4458 page
= parse_pointer (__func__
, s
);
4459 slink
= &page
->slinks
[Int_val (n_v
)];
4460 pdf_delete_annot (state
.ctx
, pdf
,
4461 (pdf_page
*) page
->fzpage
,
4462 (pdf_annot
*) slink
->u
.annot
);
4465 CAMLreturn (Val_unit
);
4468 CAMLprim value
ml_modannot (value ptr_v
, value n_v
, value str_v
)
4470 CAMLparam3 (ptr_v
, n_v
, str_v
);
4471 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4475 char *s
= String_val (ptr_v
);
4476 struct slink
*slink
;
4478 page
= parse_pointer (__func__
, s
);
4479 slink
= &page
->slinks
[Int_val (n_v
)];
4480 pdf_set_annot_contents (state
.ctx
, pdf
, (pdf_annot
*) slink
->u
.annot
,
4481 String_val (str_v
));
4484 CAMLreturn (Val_unit
);
4487 CAMLprim value
ml_hasunsavedchanges (value unit_v
)
4489 CAMLparam1 (unit_v
);
4490 CAMLreturn (Val_bool (state
.dirty
));
4493 CAMLprim value
ml_savedoc (value path_v
)
4495 CAMLparam1 (path_v
);
4496 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
4499 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
4501 CAMLreturn (Val_unit
);
4504 static void makestippletex (void)
4506 const char pixels
[] = "\xff\xff\0\0";
4507 glGenTextures (1, &state
.stid
);
4508 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4509 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4510 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4523 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4525 return caml_copy_string (FZ_VERSION
);
4528 #ifdef USE_FONTCONFIG
4534 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4538 int UNUSED_ATTR needs_exact_metrics
)
4545 FcPattern
*pat
, *pat1
;
4547 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4548 name
, bold
, italic
, needs_exact_metrics
);
4551 fc
.config
= FcInitLoadConfigAndFonts ();
4553 lprintf ("FcInitLoadConfigAndFonts failed\n");
4557 if (!fc
.config
) return NULL
;
4559 size
= strlen (name
);
4560 if (bold
) size
+= sizeof (":bold") - 1;
4561 if (italic
) size
+= sizeof (":italic") - 1;
4564 buf
= malloc (size
);
4566 err (1, "malloc %zu failed", size
);
4570 if (bold
&& italic
) {
4571 strcat (buf
, ":bold:italic");
4574 if (bold
) strcat (buf
, ":bold");
4575 if (italic
) strcat (buf
, ":italic");
4577 for (i
= 0; i
< size
; ++i
) {
4578 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4581 lprintf ("fcbuf=%s\n", buf
);
4582 pat
= FcNameParse ((FcChar8
*) buf
);
4584 printd ("emsg FcNameParse failed\n");
4589 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4590 printd ("emsg FcConfigSubstitute failed\n");
4594 FcDefaultSubstitute (pat
);
4596 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4598 printd ("emsg FcFontMatch failed\n");
4599 FcPatternDestroy (pat
);
4604 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4605 printd ("emsg FcPatternGetString failed\n");
4606 FcPatternDestroy (pat
);
4607 FcPatternDestroy (pat1
);
4613 printd ("emsg name=%s path=%s\n", name
, path
);
4615 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4616 FcPatternDestroy (pat
);
4617 FcPatternDestroy (pat1
);
4623 CAMLprim value
ml_init (value csock_v
, value params_v
)
4625 CAMLparam2 (csock_v
, params_v
);
4626 CAMLlocal2 (trim_v
, fuzz_v
);
4634 state
.csock
= Int_val (csock_v
);
4635 state
.rotate
= Int_val (Field (params_v
, 0));
4636 state
.fitmodel
= Int_val (Field (params_v
, 1));
4637 trim_v
= Field (params_v
, 2);
4638 texcount
= Int_val (Field (params_v
, 3));
4639 state
.sliceheight
= Int_val (Field (params_v
, 4));
4640 mustoresize
= Int_val (Field (params_v
, 5));
4641 colorspace
= Int_val (Field (params_v
, 6));
4642 fontpath
= String_val (Field (params_v
, 7));
4644 if (caml_string_length (Field (params_v
, 8)) > 0) {
4645 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4647 if (!state
.trimcachepath
) {
4648 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4652 haspboext
= Bool_val (Field (params_v
, 9));
4654 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4655 fz_register_document_handlers (state
.ctx
);
4657 #ifdef USE_FONTCONFIG
4658 if (Bool_val (Field (params_v
, 10))) {
4659 fz_install_load_system_font_funcs (
4660 state
.ctx
, fc_load_system_font_func
, NULL
4665 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4666 fuzz_v
= Field (trim_v
, 1);
4667 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4668 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4669 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4670 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4672 set_tex_params (colorspace
);
4675 #ifndef USE_FONTCONFIG
4676 state
.face
= load_font (fontpath
);
4680 char *buf
= fontpath
;
4681 FcPattern
*pat
, *pat1
;
4684 fc
.config
= FcInitLoadConfigAndFonts ();
4686 errx (1, "FcInitLoadConfigAndFonts failed");
4689 pat
= FcNameParse ((FcChar8
*) buf
);
4691 errx (1, "FcNameParse failed");
4694 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4695 errx (1, "FcConfigSubstitute failed");
4697 FcDefaultSubstitute (pat
);
4699 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4701 errx (1, "FcFontMatch failed");
4704 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4705 errx (1, "FcPatternGetString failed");
4708 state
.face
= load_font ((char *) path
);
4709 FcPatternDestroy (pat
);
4710 FcPatternDestroy (pat1
);
4715 const char *data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0,
4717 state
.face
= load_builtin_font (data
, len
);
4719 if (!state
.face
) _exit (1);
4721 realloctexts (texcount
);
4729 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4731 errx (1, "pthread_create: %s", strerror (ret
));
4734 CAMLreturn (Val_unit
);