1 /* lots of code c&p-ed directly from mupdf */
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
15 #include <sys/utsname.h>
18 #include <cygwin/socket.h> /* FIONREAD */
30 #include <caml/fail.h>
31 #include <caml/alloc.h>
32 #include <caml/memory.h>
33 #include <caml/unixsupport.h>
35 #include <mupdf/fitz.h>
36 #include <mupdf/cbz.h>
37 #include <mupdf/pdf.h>
38 #include <mupdf/xps.h>
39 #include <mupdf/img.h>
42 #include FT_FREETYPE_H
45 #include <fontconfig/fontconfig.h>
49 /* #define SUMATRA_LOOKUP */
52 extern char **environ
;
55 #define MIN(a,b) ((a) < (b) ? (a) : (b))
56 #define MAX(a,b) ((a) > (b) ? (a) : (b))
59 #define NORETURN_ATTR __attribute__ ((noreturn))
60 #define UNUSED_ATTR __attribute__ ((unused))
61 #if !defined __clang__
62 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
64 #define OPTIMIZE_ATTR(n)
66 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
70 #define OPTIMIZE_ATTR(n)
71 #define GCC_FMT_ATTR(a, b)
76 #define FMT_ptr PRIxPTR
77 #define SCN_ptr SCNxPTR
78 #define FMT_ptr_cast(p) ((uintptr_t) (p))
79 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
81 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
82 err (int exitcode
, const char *fmt
, ...)
89 vfprintf (stderr
, fmt
, ap
);
91 fprintf (stderr
, ": %s\n", strerror (savederrno
));
96 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
97 errx (int exitcode
, const char *fmt
, ...)
102 vfprintf (stderr
, fmt
, ap
);
104 fputc ('\n', stderr
);
109 #ifndef GL_TEXTURE_RECTANGLE_ARB
110 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
114 #define GL_BGRA 0x80E1
117 #ifndef GL_UNSIGNED_INT_8_8_8_8
118 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
121 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
122 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
126 #define lprintf printf
131 #define ARSERT(cond) for (;;) { \
133 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
149 struct slice slices
[1];
160 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
168 enum { DNONE
, DPDF
, DXPS
, DCBZ
, DIMG
};
177 fz_text_sheet
*sheet
;
185 fz_display_list
*dlist
;
187 struct slink
*slinks
;
192 void (*freepage
) (void *);
198 struct pagedim
*pagedims
;
218 fz_colorspace
*colorspace
;
226 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
240 void (*closedoc
) (void);
241 void (*freepage
) (void *);
249 void (*glBindBufferARB
) (GLenum
, GLuint
);
250 GLboolean (*glUnmapBufferARB
) (GLenum
);
251 void *(*glMapBufferARB
) (GLenum
, GLenum
);
252 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
253 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
254 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
256 GLfloat texcoords
[8];
257 GLfloat vertices
[16];
259 #ifdef SUMATRA_LOOKUP
274 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
276 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
279 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
281 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
284 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
286 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
287 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
290 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
292 static void lock (const char *cap
)
294 int ret
= pthread_mutex_lock (&mutex
);
296 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
300 static void unlock (const char *cap
)
302 int ret
= pthread_mutex_unlock (&mutex
);
304 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
308 static int trylock (const char *cap
)
310 int ret
= pthread_mutex_trylock (&mutex
);
311 if (ret
&& ret
!= EBUSY
) {
312 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
317 static void *parse_pointer (const char *cap
, const char *s
)
322 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
324 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
329 static double now (void)
333 if (gettimeofday (&tv
, NULL
)) {
334 err (1, "gettimeofday");
336 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
339 static int hasdata (void)
342 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
343 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
347 CAMLprim value
ml_hasdata (value fd_v
)
352 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
353 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
354 CAMLreturn (Val_bool (avail
> 0));
357 static void readdata (void *p
, int size
)
362 n
= read (state
.csock
, p
, size
);
364 if (errno
== EINTR
) goto again
;
365 err (1, "read (req %d, ret %zd)", size
, n
);
368 if (!n
) errx (1, "EOF while reading");
369 errx (1, "read (req %d, ret %zd)", size
, n
);
373 static void writedata (char *p
, int size
)
377 p
[0] = (size
>> 24) & 0xff;
378 p
[1] = (size
>> 16) & 0xff;
379 p
[2] = (size
>> 8) & 0xff;
380 p
[3] = (size
>> 0) & 0xff;
382 n
= write (state
.csock
, p
, size
+ 4);
384 if (!n
) errx (1, "EOF while writing data");
385 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
389 static int readlen (void)
394 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
397 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
405 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
408 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
412 if (len
< size
- 4) {
413 writedata (buf
, len
);
419 err (1, "vsnprintf for `%s' failed", fmt
);
421 buf
= realloc (buf
, size
);
426 static void closepdf (void)
428 #ifdef SUMATRA_LOOKUP
429 if (state
.pdflut
.objs
) {
432 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
433 pdf_drop_obj (state
.pdflut
.objs
[i
]);
435 free (state
.pdflut
.objs
);
436 state
.pdflut
.objs
= NULL
;
441 pdf_close_document (state
.u
.pdf
);
446 static void closexps (void)
449 xps_close_document (state
.u
.xps
);
454 static void closecbz (void)
457 cbz_close_document (state
.u
.cbz
);
462 static void closeimg (void)
465 image_close_document (state
.u
.img
);
470 static void freepdfpage (void *ptr
)
472 pdf_free_page (state
.u
.pdf
, ptr
);
475 static void freeemptyxxxpage (void *ptr
)
480 static void freexpspage (void *ptr
)
482 xps_free_page (state
.u
.xps
, ptr
);
485 static void freecbzpage (void *ptr
)
487 cbz_free_page (state
.u
.cbz
, ptr
);
490 static void freeimgpage (void *ptr
)
492 image_free_page (state
.u
.img
, ptr
);
495 static void openxref (char *filename
, char *password
)
499 for (i
= 0; i
< state
.texcount
; ++i
) {
500 state
.texowners
[i
].w
= -1;
501 state
.texowners
[i
].slice
= NULL
;
504 if (state
.closedoc
) state
.closedoc ();
506 len
= strlen (filename
);
524 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
525 const char *ext
= tbl
[j
].ext
;
526 int len2
= strlen (ext
);
528 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
529 state
.type
= tbl
[j
].type
;
535 if (state
.pagedims
) {
536 free (state
.pagedims
);
537 state
.pagedims
= NULL
;
539 state
.pagedimcount
= 0;
541 fz_set_aa_level (state
.ctx
, state
.aalevel
);
542 switch (state
.type
) {
544 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
545 if (pdf_needs_password (state
.u
.pdf
)) {
546 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
548 errx (1, "invalid password");
551 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
552 state
.closedoc
= closepdf
;
553 state
.freepage
= freepdfpage
;
557 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
558 state
.pagecount
= xps_count_pages (state
.u
.xps
);
559 state
.closedoc
= closexps
;
560 state
.freepage
= freexpspage
;
564 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
565 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
566 state
.closedoc
= closecbz
;
567 state
.freepage
= freecbzpage
;
571 state
.u
.img
= image_open_document (state
.ctx
, filename
);
573 state
.closedoc
= closeimg
;
574 state
.freepage
= freeimgpage
;
579 static void pdfinfo (void)
581 if (state
.type
== DPDF
) {
584 printd ("info PDF version\t%d.%d",
585 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
587 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
591 char *items
[] = { "Title", "Author", "Creator",
592 "Producer", "CreationDate" };
594 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
595 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
596 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
599 printd ("title %s", s
);
601 printd ("info %s\t%s", items
[i
], s
);
603 fz_free (state
.ctx
, s
);
610 static void unlinktile (struct tile
*tile
)
614 for (i
= 0; i
< tile
->slicecount
; ++i
) {
615 struct slice
*s
= &tile
->slices
[i
];
617 if (s
->texindex
!= -1) {
618 if (state
.texowners
[s
->texindex
].slice
== s
) {
619 state
.texowners
[s
->texindex
].slice
= NULL
;
625 static void freepage (struct page
*page
)
628 fz_free_text_page (state
.ctx
, page
->text
);
631 fz_free_text_sheet (state
.ctx
, page
->sheet
);
636 fz_drop_display_list (state
.ctx
, page
->dlist
);
637 page
->freepage (page
->u
.ptr
);
641 static void freetile (struct tile
*tile
)
646 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
649 fz_drop_pixmap (state
.ctx
, state
.pig
);
651 state
.pig
= tile
->pixmap
;
656 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
665 static int cacheline32bytes
;
667 static void __attribute__ ((constructor
)) clcheck (void)
669 char **envp
= environ
;
674 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
676 cacheline32bytes
= auxv
[1] == 32;
682 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
684 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
685 if (cacheline32bytes
&& size
> 32) {
686 intptr_t a1
, a2
, diff
;
688 vector
unsigned char v
= vec_splat_u8 (-1);
689 vector
unsigned char *p
;
691 a1
= a2
= (intptr_t) pixmap
->samples
;
692 a2
= (a1
+ 31) & ~31;
697 while (a1
!= a2
) *(char *) a1
++ = 0xff;
698 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
699 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
701 vec_st (v
, i
+ 16, p
);
703 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
705 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
708 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
711 static void trimctm (pdf_page
*page
, int pindex
)
714 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
716 if (!pdim
->tctmready
) {
717 if (state
.trimmargins
) {
719 fz_matrix rm
, sm
, tm
, im
, ctm1
;
721 fz_rotate (&rm
, -pdim
->rotate
);
722 fz_scale (&sm
, 1, -1);
723 fz_concat (&ctm
, &rm
, &sm
);
724 realbox
= pdim
->mediabox
;
725 fz_transform_rect (&realbox
, &ctm
);
726 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
727 fz_concat (&ctm1
, &ctm
, &tm
);
728 fz_invert_matrix (&im
, &page
->ctm
);
729 fz_concat (&ctm
, &im
, &ctm1
);
739 static fz_matrix
pagectm (struct page
*page
)
743 if (page
->type
== DPDF
) {
744 trimctm (page
->u
.pdfpage
, page
->pdimno
);
746 &state
.pagedims
[page
->pdimno
].tctm
,
747 &state
.pagedims
[page
->pdimno
].ctm
);
750 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
752 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
753 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
758 static void *loadpage (int pageno
, int pindex
)
763 page
= calloc (sizeof (struct page
), 1);
765 err (1, "calloc page %d", pageno
);
768 page
->dlist
= fz_new_display_list (state
.ctx
);
769 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
771 switch (state
.type
) {
773 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
774 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
776 page
->freepage
= freepdfpage
;
780 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
781 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
783 page
->freepage
= freexpspage
;
787 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
788 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
790 page
->freepage
= freecbzpage
;
794 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
795 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
797 page
->freepage
= freeimgpage
;
801 fz_catch (state
.ctx
) {
803 page
->freepage
= freeemptyxxxpage
;
805 fz_free_device (dev
);
807 page
->pdimno
= pindex
;
808 page
->pageno
= pageno
;
809 page
->sgen
= state
.gen
;
810 page
->tgen
= state
.gen
;
811 page
->type
= state
.type
;
816 static struct tile
*alloctile (int h
)
823 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
824 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
825 tile
= calloc (tilesize
, 1);
827 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
829 for (i
= 0; i
< slicecount
; ++i
) {
830 int sh
= MIN (h
, state
.sliceheight
);
831 tile
->slices
[i
].h
= sh
;
832 tile
->slices
[i
].texindex
= -1;
835 tile
->slicecount
= slicecount
;
836 tile
->sliceheight
= state
.sliceheight
;
846 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
847 const fz_matrix
*ctm
, float alpha
)
849 struct obs
*obs
= dev
->user
;
851 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
853 fz_rect rect
= fz_unit_rect
;
855 fz_transform_rect (&rect
, ctm
);
856 fz_round_rect (&b
, &rect
);
857 fz_intersect_irect (&b
, &obs
->b
);
858 obs
->cured
= b
.x0
== obs
->b
.x0
861 && b
.y1
== obs
->b
.y1
;
865 static int obscured (struct page
*page
, fz_irect bbox
)
872 memset (&dev
, 0, sizeof (dev
));
873 memset (&obs
, 0, sizeof (obs
));
878 dev
.fill_image
= obs_fill_image
;
880 fz_rect_from_irect (&rect
, &bbox
);
881 ctm
= pagectm (page
);
882 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
885 #define OBSCURED obscured
887 #define OBSCURED(a, b) 0
890 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
898 struct pagedim
*pdim
;
900 tile
= alloctile (h
);
901 pdim
= &state
.pagedims
[page
->pdimno
];
906 bbox
.x1
= bbox
.x0
+ w
;
907 bbox
.y1
= bbox
.y0
+ h
;
910 if (state
.pig
->w
== w
912 && state
.pig
->colorspace
== state
.colorspace
) {
913 tile
->pixmap
= state
.pig
;
914 tile
->pixmap
->x
= bbox
.x0
;
915 tile
->pixmap
->y
= bbox
.y0
;
918 fz_drop_pixmap (state
.ctx
, state
.pig
);
925 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
931 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
937 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
938 clearpixmap (tile
->pixmap
);
940 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
941 ctm
= pagectm (page
);
942 fz_rect_from_irect (&rect
, &bbox
);
943 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
944 fz_free_device (dev
);
949 #ifdef SUMATRA_LOOKUP
951 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
957 static void initpdims (void)
961 fz_rect rootmediabox
;
962 int pageno
, trim
, show
;
963 int trimw
= 0, cxcount
;
967 if (state
.trimmargins
&& state
.trimcachepath
) {
968 trimf
= fopen (state
.trimcachepath
, "rb");
970 trimf
= fopen (state
.trimcachepath
, "wb");
975 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
976 cxcount
= state
.pagecount
;
978 cxcount
= MIN (state
.pagecount
, 1);
980 if (state
.type
== DPDF
) {
982 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
983 "Root/Pages/MediaBox");
984 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
987 #ifdef SUMATRA_LOOKUP
988 if (state
.type
== DPDF
989 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
990 state
.pdflut
.objs
= malloc (sizeof (*state
.pdflut
.objs
) * cxcount
);
991 if (!state
.pdflut
.objs
) {
992 err (1, "malloc pageobjs %zu %d %zu failed",
993 sizeof (*state
.pdflut
.objs
), cxcount
,
994 sizeof (*state
.pdflut
.objs
) * cxcount
);
996 state
.pdflut
.count
= cxcount
;
997 pdf_load_page_objs (state
.u
.pdf
, state
.pdflut
.objs
);
998 state
.pdflut
.pdf
= state
.u
.pdf
;
1002 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1007 switch (state
.type
) {
1009 pdf_document
*pdf
= state
.u
.pdf
;
1010 pdf_obj
*pageref
, *pageobj
;
1012 #ifdef SUMATRA_LOOKUP
1013 pageref
= state
.pdflut
.objs
[pageno
];
1015 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1016 show
= !trim
&& pageno
% 20 == 0;
1018 printd ("progress %f Gathering dimensions %d",
1019 (double) (pageno
) / state
.pagecount
,
1023 pageobj
= pdf_resolve_indirect (pageref
);
1025 if (state
.trimmargins
) {
1029 fz_try (state
.ctx
) {
1030 page
= pdf_load_page (pdf
, pageno
);
1031 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1032 trim
= state
.trimanew
|| !obj
;
1038 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1039 dev
->hints
|= FZ_IGNORE_SHADE
;
1040 fz_invert_matrix (&ctm
, &page
->ctm
);
1041 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1042 fz_free_device (dev
);
1044 rect
.x0
+= state
.trimfuzz
.x0
;
1045 rect
.x1
+= state
.trimfuzz
.x1
;
1046 rect
.y0
+= state
.trimfuzz
.y0
;
1047 rect
.y1
+= state
.trimfuzz
.y1
;
1048 fz_transform_rect (&rect
, &ctm
);
1049 fz_intersect_rect (&rect
, &page
->mediabox
);
1051 if (fz_is_empty_rect (&rect
)) {
1052 mediabox
= page
->mediabox
;
1058 obj
= pdf_new_array (pdf
, 4);
1059 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1060 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1061 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1062 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1063 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1066 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1067 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1068 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1069 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1072 rotate
= page
->rotate
;
1073 pdf_free_page (pdf
, page
);
1075 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1077 printd ("progress %f Trimming %d",
1078 (double) (pageno
+ 1) / state
.pagecount
,
1082 fz_catch (state
.ctx
) {
1083 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1090 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1092 if (fz_is_empty_rect (&mediabox
)) {
1100 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1102 if (!fz_is_empty_rect (&cropbox
)) {
1107 fz_intersect_rect (&mediabox
, &cropbox
);
1112 if (fz_is_empty_rect (&rootmediabox
)) {
1114 "cannot find page size for page %d\n",
1118 mediabox
= rootmediabox
;
1122 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1131 fz_try (state
.ctx
) {
1132 page
= xps_load_page (state
.u
.xps
, pageno
);
1133 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1135 if (state
.trimmargins
) {
1139 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1140 dev
->hints
|= FZ_IGNORE_SHADE
;
1141 xps_run_page (state
.u
.xps
, page
, dev
,
1142 &fz_identity
, NULL
);
1143 fz_free_device (dev
);
1145 rect
.x0
+= state
.trimfuzz
.x0
;
1146 rect
.x1
+= state
.trimfuzz
.x1
;
1147 rect
.y0
+= state
.trimfuzz
.y0
;
1148 rect
.y1
+= state
.trimfuzz
.y1
;
1149 fz_intersect_rect (&rect
, &mediabox
);
1151 if (!fz_is_empty_rect (&rect
)) {
1155 xps_free_page (state
.u
.xps
, page
);
1157 printd ("progress %f loading %d",
1158 (double) (pageno
+ 1) / state
.pagecount
,
1162 fz_catch (state
.ctx
) {
1174 if (state
.trimmargins
&& trimw
) {
1177 fz_try (state
.ctx
) {
1178 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1179 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1180 cbz_free_page (state
.u
.cbz
, page
);
1181 printd ("progress %f Trimming %d",
1182 (double) (pageno
+ 1) / state
.pagecount
,
1185 fz_catch (state
.ctx
) {
1186 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1189 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1191 err (1, "fwrite trim mediabox");
1197 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1199 err (1, "fread trim mediabox");
1205 fz_try (state
.ctx
) {
1206 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1207 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1208 cbz_free_page (state
.u
.cbz
, page
);
1210 fz_catch (state
.ctx
) {
1211 fprintf (stderr
, "failed to load cbz page\n");
1229 fz_try (state
.ctx
) {
1230 page
= image_load_page (state
.u
.img
, pageno
);
1231 image_bound_page (state
.u
.img
, page
, &mediabox
);
1232 image_free_page (state
.u
.img
, page
);
1234 fz_catch (state
.ctx
) {
1235 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1241 ARSERT (0 && state
.type
);
1244 if (state
.pagedimcount
== 0
1245 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1246 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1249 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1250 state
.pagedims
= realloc (state
.pagedims
, size
);
1251 if (!state
.pagedims
) {
1252 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1253 size
, state
.pagedimcount
+ 1);
1256 p
= &state
.pagedims
[state
.pagedimcount
++];
1258 p
->mediabox
= mediabox
;
1263 if (state
.trimmargins
) {
1264 printd ("progress 1 Trimmed %d pages in %f seconds",
1265 state
.pagecount
, end
- start
);
1268 if (state
.type
== DPDF
)
1269 printd ("progress 1 Processed %d pages in %f seconds",
1270 state
.pagecount
, end
- start
);
1272 printd ("vmsg Processed %d pages in %f seconds",
1273 state
.pagecount
, end
- start
);
1277 if (fclose (trimf
)) {
1283 static void layout (void)
1288 struct pagedim
*p
= p
;
1289 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1291 if (state
.pagedimcount
== 0) return;
1293 switch (state
.fitmodel
) {
1294 case FitProportional
:
1295 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1298 p
= &state
.pagedims
[pindex
];
1299 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1301 fz_transform_rect (&box
, &rm
);
1303 x0
= MIN (box
.x0
, box
.x1
);
1304 x1
= MAX (box
.x0
, box
.x1
);
1307 maxw
= MAX (w
, maxw
);
1308 zoom
= state
.w
/ maxw
;
1320 ARSERT (0 && state
.fitmodel
);
1323 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1327 p
= &state
.pagedims
[pindex
];
1328 fz_rotate (&ctm
, state
.rotate
);
1329 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1331 fz_transform_rect (&box
, &rm
);
1332 w
= box
.x1
- box
.x0
;
1333 switch (state
.fitmodel
) {
1334 case FitProportional
:
1335 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1341 h
= box
.y1
- box
.y0
;
1343 zoom
= MIN (zw
, zh
);
1344 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1353 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1354 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1356 fz_rotate (&rm
, p
->rotate
);
1357 p
->pagebox
= p
->mediabox
;
1358 fz_transform_rect (&p
->pagebox
, &rm
);
1359 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1360 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1364 fz_transform_rect (&rect
, &ctm
);
1365 fz_round_rect (&p
->bounds
, &rect
);
1368 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1369 fz_scale (&sm
, zoom
, -zoom
);
1370 fz_concat (&ctm
, &tm
, &sm
);
1371 fz_concat (&p
->lctm
, &ctm
, &rm
);
1377 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1378 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1379 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1380 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1381 int boundw
= x1
- x0
;
1382 int boundh
= y1
- y0
;
1384 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1385 } while (p
-- != state
.pagedims
);
1389 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1390 desttoanchor (fz_link_dest
*dest
)
1394 struct pagedim
*pdim
= state
.pagedims
;
1399 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1400 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1402 pdim
= &state
.pagedims
[i
];
1404 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1406 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1407 p
.x
= dest
->ld
.gotor
.lt
.x
;
1410 p
.y
= dest
->ld
.gotor
.lt
.y
;
1411 fz_transform_point (&p
, &pdim
->lctm
);
1415 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1416 double x0
, x1
, y0
, y1
;
1418 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1419 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1421 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1422 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1424 a
.n
= dest
->ld
.gotor
.page
;
1429 static void recurse_outline (fz_outline
*outline
, int level
)
1432 switch (outline
->dest
.kind
) {
1435 struct anchor a
= desttoanchor (&outline
->dest
);
1438 printd ("o %d %d %d %d %s",
1439 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1445 printd ("ou %d %" FMT_s
" %s %s", level
,
1446 strlen (outline
->title
), outline
->title
,
1447 outline
->dest
.ld
.uri
.uri
);
1451 printd ("on %d %s", level
, outline
->title
);
1455 printd ("emsg Unhandled outline kind %d for %s\n",
1456 outline
->dest
.kind
, outline
->title
);
1459 if (outline
->down
) {
1460 recurse_outline (outline
->down
, level
+ 1);
1462 outline
= outline
->next
;
1466 static void process_outline (void)
1468 fz_outline
*outline
;
1470 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1472 state
.needoutline
= 0;
1473 switch (state
.type
) {
1475 outline
= pdf_load_outline (state
.u
.pdf
);
1478 outline
= xps_load_outline (state
.u
.xps
);
1485 recurse_outline (outline
, 0);
1486 fz_free_outline (state
.ctx
, outline
);
1490 static char *strofspan (fz_text_span
*span
)
1495 size_t size
= 0, cap
= 80;
1497 p
= malloc (cap
+ 1);
1498 if (!p
) return NULL
;
1500 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1501 int n
= fz_runetochar (utf8
, ch
->c
);
1502 if (size
+ n
> cap
) {
1504 p
= realloc (p
, cap
+ 1);
1505 if (!p
) return NULL
;
1508 memcpy (p
+ size
, utf8
, n
);
1515 static int matchspan (regex_t
*re
, fz_text_page UNUSED_ATTR
*page
,
1516 fz_text_span
*span
, fz_matrix UNUSED_ATTR ctm
,
1517 int stop
, int pageno
, double start
)
1524 fz_point p1
, p2
, p3
, p4
;
1526 p
= strofspan (span
);
1529 ret
= regexec (re
, p
, 1, &rm
, 0);
1532 if (ret
!= REG_NOMATCH
) {
1535 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1536 printd ("msg regexec error `%.*s'",
1537 (int) size
, errbuf
);
1545 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1546 c
+= fz_runelen (span
->text
[a
].c
);
1548 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1549 c
+= fz_runelen (span
->text
[b
].c
);
1552 if (fz_runelen (span
->text
[b
].c
) > 1) {
1556 fz_text_char_bbox (&sb
, span
, a
);
1557 fz_text_char_bbox (&eb
, span
, b
);
1569 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1576 printd ("progress 1 found at %d `%.*s' in %f sec",
1577 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1581 printd ("match %d %d %f %f %f %f %f %f %f %f",
1593 static int compareblocks (const void *l
, const void *r
)
1595 fz_text_block
const *ls
= l
;
1596 fz_text_block
const* rs
= r
;
1597 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1600 /* wishful thinking function */
1601 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1606 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1608 fz_text_sheet
*sheet
;
1609 struct pagedim
*pdim
, *pdimprev
;
1610 int stop
= 0, niters
= 0;
1613 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1617 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1618 if (niters
++ == 5) {
1621 printd ("progress 1 attention requested aborting search at %d",
1626 printd ("progress %f searching in page %d",
1627 (double) (pageno
+ 1) / state
.pagecount
,
1632 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1633 pdim
= &state
.pagedims
[i
];
1634 if (pdim
->pageno
== pageno
) {
1637 if (pdim
->pageno
> pageno
) {
1646 sheet
= fz_new_text_sheet (state
.ctx
);
1647 text
= fz_new_text_page (state
.ctx
);
1648 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1650 switch (state
.type
) {
1653 fz_try (state
.ctx
) {
1654 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1655 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1656 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1657 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1658 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1661 fz_catch (state
.ctx
) {
1662 fz_free_device (tdev
);
1669 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1670 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1674 ARSERT (0 && state
.type
);
1677 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1678 fz_free_device (tdev
);
1680 for (j
= 0; j
< text
->len
; ++j
) {
1683 fz_text_block
*block
;
1685 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1686 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1689 for (k
= 0; k
< block
->len
; ++k
) {
1694 line
= &block
->lines
[k
];
1695 if (line
->bbox
.y0
< y
+ 1) continue;
1698 line
= &block
->lines
[block
->len
- 1 - k
];
1699 if (line
->bbox
.y0
> y
- 1) continue;
1702 for (span
= line
->first_span
; span
; span
= span
->next
) {
1703 switch (matchspan (re
, text
, span
, ctm
,
1704 stop
, pageno
, start
)) {
1706 case 1: stop
= 1; break;
1707 case -1: stop
= 1; goto endloop
;
1722 fz_free_text_page (state
.ctx
, text
);
1723 fz_free_text_sheet (state
.ctx
, sheet
);
1725 state
.freepage (u
.ptr
);
1730 printd ("progress 1 no matches %f sec", end
- start
);
1732 printd ("clearrects");
1735 static void set_tex_params (int colorspace
)
1742 switch (colorspace
) {
1744 state
.texiform
= GL_RGBA8
;
1745 state
.texform
= GL_RGBA
;
1746 state
.texty
= GL_UNSIGNED_BYTE
;
1747 state
.colorspace
= fz_device_rgb (state
.ctx
);
1750 state
.texiform
= GL_RGBA8
;
1751 state
.texform
= GL_BGRA
;
1752 state
.texty
= endianness
.s
> 1
1753 ? GL_UNSIGNED_INT_8_8_8_8
1754 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1755 state
.colorspace
= fz_device_bgr (state
.ctx
);
1758 state
.texiform
= GL_LUMINANCE_ALPHA
;
1759 state
.texform
= GL_LUMINANCE_ALPHA
;
1760 state
.texty
= GL_UNSIGNED_BYTE
;
1761 state
.colorspace
= fz_device_gray (state
.ctx
);
1764 errx (1, "invalid colorspce %d", colorspace
);
1768 static void realloctexts (int texcount
)
1772 if (texcount
== state
.texcount
) return;
1774 if (texcount
< state
.texcount
) {
1775 glDeleteTextures (state
.texcount
- texcount
,
1776 state
.texids
+ texcount
);
1779 size
= texcount
* sizeof (*state
.texids
);
1780 state
.texids
= realloc (state
.texids
, size
);
1781 if (!state
.texids
) {
1782 err (1, "realloc texids %" FMT_s
, size
);
1785 size
= texcount
* sizeof (*state
.texowners
);
1786 state
.texowners
= realloc (state
.texowners
, size
);
1787 if (!state
.texowners
) {
1788 err (1, "realloc texowners %" FMT_s
, size
);
1790 if (texcount
> state
.texcount
) {
1793 glGenTextures (texcount
- state
.texcount
,
1794 state
.texids
+ state
.texcount
);
1795 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1796 state
.texowners
[i
].w
= -1;
1797 state
.texowners
[i
].slice
= NULL
;
1800 state
.texcount
= texcount
;
1804 static char *mbtoutf8 (char *s
)
1810 len
= mbstowcs (NULL
, s
, strlen (s
));
1815 if (len
== (size_t) -1) {
1820 tmp
= malloc (len
* sizeof (wchar_t));
1825 ret
= mbstowcs (tmp
, s
, len
);
1826 if (ret
== (size_t) -1) {
1832 for (i
= 0; i
< ret
; ++i
) {
1833 len
+= fz_runelen (tmp
[i
]);
1836 p
= r
= malloc (len
+ 1);
1842 for (i
= 0; i
< ret
; ++i
) {
1843 p
+= fz_runetochar (p
, tmp
[i
]);
1850 CAMLprim value
ml_mbtoutf8 (value s_v
)
1856 s
= String_val (s_v
);
1862 ret_v
= caml_copy_string (r
);
1868 static void * mainloop (void UNUSED_ATTR
*unused
)
1871 int len
, ret
, oldlen
= 0;
1876 errx (1, "readlen returned 0");
1879 if (oldlen
< len
+ 1) {
1880 p
= realloc (p
, len
+ 1);
1882 err (1, "realloc %d failed", len
+ 1);
1889 if (!strncmp ("open", p
, 4)) {
1890 int wthack
, off
, ok
= 0;
1896 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1898 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1901 filename
= p
+ 5 + off
;
1902 filenamelen
= strlen (filename
);
1903 password
= filename
+ filenamelen
+ 1;
1906 fz_try (state
.ctx
) {
1907 openxref (filename
, password
);
1910 fz_catch (state
.ctx
) {
1911 utf8filename
= mbtoutf8 (filename
);
1912 printd ("msg Could not open %s", utf8filename
);
1919 utf8filename
= mbtoutf8 (filename
);
1920 printd ("msg Opened %s (press h/F1 to get help)",
1922 if (utf8filename
!= filename
) {
1923 free (utf8filename
);
1926 state
.needoutline
= 1;
1929 else if (!strncmp ("cs", p
, 2)) {
1932 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1934 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1937 set_tex_params (colorspace
);
1938 for (i
= 0; i
< state
.texcount
; ++i
) {
1939 state
.texowners
[i
].w
= -1;
1940 state
.texowners
[i
].slice
= NULL
;
1944 else if (!strncmp ("freepage", p
, 8)) {
1947 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1949 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1953 else if (!strncmp ("freetile", p
, 8)) {
1956 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1958 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1962 else if (!strncmp ("search", p
, 6)) {
1963 int icase
, pageno
, y
, len2
, forward
;
1967 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1968 &icase
, &pageno
, &y
, &forward
, &len2
);
1970 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1973 pattern
= p
+ 6 + len2
;
1974 ret
= regcomp (&re
, pattern
,
1975 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1980 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1981 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1984 search (&re
, pageno
, y
, forward
);
1988 else if (!strncmp ("geometry", p
, 8)) {
1992 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1994 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2002 for (i
= 0; i
< state
.texcount
; ++i
) {
2003 state
.texowners
[i
].slice
= NULL
;
2006 state
.fitmodel
= fitmodel
;
2011 unlock ("geometry");
2012 printd ("continue %d", state
.pagecount
);
2014 else if (!strncmp ("reqlayout", p
, 9)) {
2017 unsigned int fitmodel
;
2020 ret
= sscanf (p
+ 9, " %d %u %d %n",
2021 &rotate
, &fitmodel
, &h
, &off
);
2023 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2026 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2029 state
.rotate
= rotate
;
2030 state
.fitmodel
= fitmodel
;
2035 nameddest
= p
+ 9 + off
;
2036 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2039 pdf_obj
*needle
, *obj
;
2041 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2042 strlen (nameddest
));
2043 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2045 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2047 a
= desttoanchor (&dest
);
2049 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2052 printd ("emsg failed to parse destination `%s'\n",
2057 printd ("emsg destination `%s' not found\n",
2060 pdf_drop_obj (needle
);
2064 unlock ("reqlayout");
2065 printd ("continue %d", state
.pagecount
);
2067 else if (!strncmp ("page", p
, 4)) {
2072 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2074 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2079 page
= loadpage (pageno
, pindex
);
2083 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2085 else if (!strncmp ("tile", p
, 4)) {
2092 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2093 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2094 SCN_ptr_cast (&data
));
2096 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2101 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2105 printd ("tile %d %d %" FMT_ptr
" %u %f",
2107 FMT_ptr_cast (tile
),
2108 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2111 else if (!strncmp ("settrim", p
, 7)) {
2115 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2116 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2118 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2122 state
.trimmargins
= trimmargins
;
2123 state
.needoutline
= 1;
2124 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2126 state
.trimfuzz
= fuzz
;
2128 state
.pagedimcount
= 0;
2129 free (state
.pagedims
);
2130 state
.pagedims
= NULL
;
2135 printd ("continue %d", state
.pagecount
);
2137 else if (!strncmp ("sliceh", p
, 6)) {
2140 ret
= sscanf (p
+ 6, " %d", &h
);
2142 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2144 if (h
!= state
.sliceheight
) {
2147 state
.sliceheight
= h
;
2148 for (i
= 0; i
< state
.texcount
; ++i
) {
2149 state
.texowners
[i
].w
= -1;
2150 state
.texowners
[i
].h
= -1;
2151 state
.texowners
[i
].slice
= NULL
;
2155 else if (!strncmp ("interrupt", p
, 9)) {
2156 printd ("vmsg interrupted");
2159 errx (1, "unknown command %.*s", len
, p
);
2165 CAMLprim value
ml_realloctexts (value texcount_v
)
2167 CAMLparam1 (texcount_v
);
2170 if (trylock ("ml_realloctexts")) {
2174 realloctexts (Int_val (texcount_v
));
2176 unlock ("ml_realloctexts");
2179 CAMLreturn (Val_bool (ok
));
2182 static void recti (int x0
, int y0
, int x1
, int y1
)
2184 GLfloat
*v
= state
.vertices
;
2186 glVertexPointer (2, GL_FLOAT
, 0, v
);
2187 v
[0] = x0
; v
[1] = y0
;
2188 v
[2] = x1
; v
[3] = y0
;
2189 v
[4] = x0
; v
[5] = y1
;
2190 v
[6] = x1
; v
[7] = y1
;
2191 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2194 static void showsel (struct page
*page
, int ox
, int oy
)
2200 fz_page_block
*pageb
;
2201 fz_text_block
*block
;
2202 struct mark first
, last
;
2203 unsigned char selcolor
[] = {15,15,15,140};
2205 first
= page
->fmark
;
2208 if (!first
.span
|| !last
.span
) return;
2210 glEnable (GL_BLEND
);
2211 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2212 glColor4ubv (selcolor
);
2214 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2215 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2216 for (pageb
= page
->text
->blocks
;
2217 pageb
< page
->text
->blocks
+ page
->text
->len
;
2219 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2220 block
= pageb
->u
.text
;
2222 for (line
= block
->lines
;
2223 line
< block
->lines
+ block
->len
;
2226 rect
= fz_empty_rect
;
2228 for (span
= line
->first_span
; span
; span
= span
->next
) {
2230 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2235 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2237 j
= MIN (first
.i
, last
.i
);
2238 k
= MAX (first
.i
, last
.i
);
2241 if (span
== first
.span
) {
2245 else if (span
== last
.span
) {
2252 for (i
= j
; i
<= k
; ++i
) {
2254 fz_union_rect (&rect
,
2255 fz_text_char_bbox (&bbox1
, span
, i
));
2257 fz_round_rect (&bbox
, &rect
);
2258 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2265 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2266 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2267 if (span
== last
.span
) {
2270 rect
= fz_empty_rect
;
2276 glDisable (GL_BLEND
);
2281 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2283 fz_matrix ctm
, tm
, pm
;
2284 fz_link
*link
, *links
;
2285 GLfloat
*texcoords
= state
.texcoords
;
2286 GLfloat
*vertices
= state
.vertices
;
2288 switch (page
->type
) {
2290 links
= page
->u
.pdfpage
->links
;
2294 links
= page
->u
.xpspage
->links
;
2301 glEnable (GL_TEXTURE_1D
);
2302 glEnable (GL_BLEND
);
2303 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2304 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2306 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2307 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2308 fz_translate (&tm
, xoff
, yoff
);
2309 pm
= pagectm (page
);
2310 fz_concat (&ctm
, &pm
, &tm
);
2312 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2313 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2315 for (link
= links
; link
; link
= link
->next
) {
2316 fz_point p1
, p2
, p3
, p4
;
2318 p1
.x
= link
->rect
.x0
;
2319 p1
.y
= link
->rect
.y0
;
2321 p2
.x
= link
->rect
.x1
;
2322 p2
.y
= link
->rect
.y0
;
2324 p3
.x
= link
->rect
.x1
;
2325 p3
.y
= link
->rect
.y1
;
2327 p4
.x
= link
->rect
.x0
;
2328 p4
.y
= link
->rect
.y1
;
2330 fz_transform_point (&p1
, &ctm
);
2331 fz_transform_point (&p2
, &ctm
);
2332 fz_transform_point (&p3
, &ctm
);
2333 fz_transform_point (&p4
, &ctm
);
2335 switch (link
->dest
.kind
) {
2336 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2337 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2338 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2339 default: glColor3ub (0, 0, 0); break;
2347 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2351 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2353 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2354 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2356 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2357 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2359 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2360 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2362 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2363 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2365 glDrawArrays (GL_LINES
, 0, 8);
2368 glDisable (GL_BLEND
);
2369 glDisable (GL_TEXTURE_1D
);
2372 static int compareslinks (const void *l
, const void *r
)
2374 struct slink
const *ls
= l
;
2375 struct slink
const *rs
= r
;
2376 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2377 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2379 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2382 static void droptext (struct page
*page
)
2385 fz_free_text_page (state
.ctx
, page
->text
);
2388 page
->fmark
.span
= NULL
;
2389 page
->lmark
.span
= NULL
;
2393 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2398 static void dropslinks (struct page
*page
)
2401 free (page
->slinks
);
2402 page
->slinks
= NULL
;
2403 page
->slinkcount
= 0;
2407 static void ensureslinks (struct page
*page
)
2411 size_t slinksize
= sizeof (*page
->slinks
);
2412 fz_link
*link
, *links
;
2414 if (state
.gen
!= page
->sgen
) {
2416 page
->sgen
= state
.gen
;
2418 if (page
->slinks
) return;
2420 switch (page
->type
) {
2422 links
= page
->u
.pdfpage
->links
;
2423 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2425 &state
.pagedims
[page
->pdimno
].tctm
,
2426 &state
.pagedims
[page
->pdimno
].ctm
);
2430 links
= page
->u
.xpspage
->links
;
2431 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2438 for (link
= links
; link
; link
= link
->next
) {
2442 page
->slinkcount
= count
;
2443 page
->slinks
= calloc (count
, slinksize
);
2444 if (!page
->slinks
) {
2445 err (1, "realloc slinks %d", count
);
2448 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2452 fz_transform_rect (&rect
, &ctm
);
2453 page
->slinks
[i
].link
= link
;
2454 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2456 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2460 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2461 static void fmt_linkn (char *s
, unsigned int u
)
2463 unsigned int len
; unsigned int q
;
2464 unsigned int zma
= 'z' - 'a' + 1;
2466 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2469 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2470 /* handles u == 0 */
2475 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2476 int noff
, char *targ
, int tlen
, int hfsize
)
2480 struct slink
*slink
;
2481 double x0
, y0
, x1
, y1
, w
;
2483 ensureslinks (page
);
2484 glColor3ub (0xc3, 0xb0, 0x91);
2485 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2486 fmt_linkn (buf
, i
+ noff
);
2487 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2488 slink
= &page
->slinks
[i
];
2490 x0
= slink
->bbox
.x0
+ xoff
- 5;
2491 y1
= slink
->bbox
.y0
+ yoff
- 5;
2492 y0
= y1
+ 10 + hfsize
;
2493 w
= measure_string (state
.face
, hfsize
, buf
);
2495 recti (x0
, y0
, x1
, y1
);
2499 glEnable (GL_BLEND
);
2500 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2501 glEnable (GL_TEXTURE_2D
);
2502 glColor3ub (0, 0, 0);
2503 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2504 fmt_linkn (buf
, i
+ noff
);
2505 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2506 slink
= &page
->slinks
[i
];
2508 x0
= slink
->bbox
.x0
+ xoff
;
2509 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2510 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2513 glDisable (GL_TEXTURE_2D
);
2514 glDisable (GL_BLEND
);
2517 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2520 struct slice
*slice1
;
2521 unsigned char *texdata
;
2524 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2525 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2527 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2528 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2529 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2533 int texindex
= state
.texindex
++ % state
.texcount
;
2535 if (state
.texowners
[texindex
].w
== tile
->w
) {
2536 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2540 state
.texowners
[texindex
].h
= slice
->h
;
2544 state
.texowners
[texindex
].h
= slice
->h
;
2547 state
.texowners
[texindex
].w
= tile
->w
;
2548 state
.texowners
[texindex
].slice
= slice
;
2549 slice
->texindex
= texindex
;
2551 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2553 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2557 texdata
= tile
->pixmap
->samples
;
2560 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2572 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2584 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2589 CAMLprim value
ml_begintiles (value unit_v
)
2591 CAMLparam1 (unit_v
);
2592 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2593 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2594 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2595 CAMLreturn (unit_v
);
2598 CAMLprim value
ml_endtiles (value unit_v
)
2600 CAMLparam1 (unit_v
);
2601 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2602 CAMLreturn (unit_v
);
2605 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2607 CAMLparam2 (args_v
, ptr_v
);
2608 int dispx
= Int_val (Field (args_v
, 0));
2609 int dispy
= Int_val (Field (args_v
, 1));
2610 int dispw
= Int_val (Field (args_v
, 2));
2611 int disph
= Int_val (Field (args_v
, 3));
2612 int tilex
= Int_val (Field (args_v
, 4));
2613 int tiley
= Int_val (Field (args_v
, 5));
2614 char *s
= String_val (ptr_v
);
2615 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2616 int slicey
, firstslice
;
2617 struct slice
*slice
;
2618 GLfloat
*texcoords
= state
.texcoords
;
2619 GLfloat
*vertices
= state
.vertices
;
2621 firstslice
= tiley
/ tile
->sliceheight
;
2622 slice
= &tile
->slices
[firstslice
];
2623 slicey
= tiley
% tile
->sliceheight
;
2628 dh
= slice
->h
- slicey
;
2629 dh
= MIN (disph
, dh
);
2630 uploadslice (tile
, slice
);
2632 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2633 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2634 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2635 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2637 vertices
[0] = dispx
; vertices
[1] = dispy
;
2638 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2639 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2640 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2642 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2646 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2649 CAMLreturn (Val_unit
);
2652 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2653 value xoff_v
, value yoff_v
,
2656 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2657 int xoff
= Int_val (xoff_v
);
2658 int yoff
= Int_val (yoff_v
);
2659 int noff
= Int_val (Field (li_v
, 0));
2660 char *targ
= String_val (Field (li_v
, 1));
2661 int tlen
= caml_string_length (Field (li_v
, 1));
2662 int hfsize
= Int_val (Field (li_v
, 2));
2663 char *s
= String_val (ptr_v
);
2664 int hlmask
= Int_val (hlinks_v
);
2665 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2668 /* deal with loadpage failed pages */
2672 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2673 if (trylock ("ml_postprocess")) {
2678 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2679 noff
= page
->slinkcount
;
2681 if (page
->tgen
== state
.gen
) {
2682 showsel (page
, xoff
, yoff
);
2684 unlock ("ml_postprocess");
2687 CAMLreturn (Val_int (noff
));
2690 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2694 const fz_matrix
*tctm
;
2695 fz_link
*link
, *links
;
2697 switch (page
->type
) {
2699 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2700 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2701 links
= page
->u
.pdfpage
->links
;
2705 tctm
= &fz_identity
;
2706 links
= page
->u
.xpspage
->links
;
2715 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2716 fz_invert_matrix (&ctm
, &ctm
);
2717 fz_transform_point (&p
, &ctm
);
2719 for (link
= links
; link
; link
= link
->next
) {
2720 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2721 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2729 static void ensuretext (struct page
*page
)
2731 if (state
.gen
!= page
->tgen
) {
2733 page
->tgen
= state
.gen
;
2739 page
->text
= fz_new_text_page (state
.ctx
);
2740 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2741 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2742 ctm
= pagectm (page
);
2743 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2744 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2745 qsort (page
->text
->blocks
, page
->text
->len
,
2746 sizeof (*page
->text
->blocks
), compareblocks
);
2748 fz_free_device (tdev
);
2752 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2754 CAMLparam2 (start_page_v
, dir_v
);
2756 int i
, dir
= Int_val (dir_v
);
2757 int start_page
= Int_val (start_page_v
);
2758 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2760 ret_v
= Val_int (0);
2761 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2765 lock ("ml_findpage_with_links");
2766 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2769 switch (state
.type
) {
2772 pdf_page
*page
= NULL
;
2774 fz_try (state
.ctx
) {
2775 page
= pdf_load_page (state
.u
.pdf
, i
);
2776 found
= !!page
->links
;
2778 fz_catch (state
.ctx
) {
2788 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2789 found
= !!page
->links
;
2795 ARSERT (0 && "invalid document type");
2799 ret_v
= caml_alloc_small (1, 1);
2800 Field (ret_v
, 0) = Val_int (i
);
2805 unlock ("ml_findpage_with_links");
2811 enum { dir_first
, dir_last
};
2812 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2814 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2816 CAMLparam2 (ptr_v
, dir_v
);
2817 CAMLlocal2 (ret_v
, pos_v
);
2819 int dirtag
, i
, slinkindex
;
2820 struct slink
*found
= NULL
,*slink
;
2821 char *s
= String_val (ptr_v
);
2823 page
= parse_pointer ("ml_findlink", s
);
2824 ret_v
= Val_int (0);
2825 if (trylock ("ml_findlink")) {
2829 ensureslinks (page
);
2831 if (Is_block (dir_v
)) {
2832 dirtag
= Tag_val (dir_v
);
2834 case dir_first_visible
:
2836 int x0
, y0
, dir
, first_index
, last_index
;
2838 pos_v
= Field (dir_v
, 0);
2839 x0
= Int_val (Field (pos_v
, 0));
2840 y0
= Int_val (Field (pos_v
, 1));
2841 dir
= Int_val (Field (pos_v
, 2));
2846 last_index
= page
->slinkcount
;
2849 first_index
= page
->slinkcount
- 1;
2853 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2854 slink
= &page
->slinks
[i
];
2855 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2864 slinkindex
= Int_val (Field (dir_v
, 0));
2865 found
= &page
->slinks
[slinkindex
];
2866 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2867 slink
= &page
->slinks
[i
];
2868 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2876 slinkindex
= Int_val (Field (dir_v
, 0));
2877 found
= &page
->slinks
[slinkindex
];
2878 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2879 slink
= &page
->slinks
[i
];
2880 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2888 slinkindex
= Int_val (Field (dir_v
, 0));
2889 found
= &page
->slinks
[slinkindex
];
2890 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2891 slink
= &page
->slinks
[i
];
2892 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2900 slinkindex
= Int_val (Field (dir_v
, 0));
2901 found
= &page
->slinks
[slinkindex
];
2902 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2903 slink
= &page
->slinks
[i
];
2904 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2913 dirtag
= Int_val (dir_v
);
2916 found
= page
->slinks
;
2921 found
= page
->slinks
+ (page
->slinkcount
- 1);
2927 ret_v
= caml_alloc_small (2, 1);
2928 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2931 unlock ("ml_findlink");
2936 enum { uuri
, ugoto
, utext
, uunexpected
,
2937 ulaunch
, unamed
, uremote
, uremotedest
};
2943 switch (link->dest.kind) { \
2944 case FZ_LINK_GOTO: \
2948 pageno = link->dest.ld.gotor.page; \
2952 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2953 p.y = link->dest.ld.gotor.lt.y; \
2954 fz_transform_point (&p, &pdim->lctm); \
2955 if (p.y < 0) p.y = 0; \
2957 tup_v = caml_alloc_tuple (2); \
2958 ret_v = caml_alloc_small (1, ugoto); \
2959 Field (tup_v, 0) = Val_int (pageno); \
2960 Field (tup_v, 1) = Val_int (p.y); \
2961 Field (ret_v, 0) = tup_v; \
2966 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2967 ret_v = caml_alloc_small (1, uuri); \
2968 Field (ret_v, 0) = str_v; \
2971 case FZ_LINK_LAUNCH: \
2972 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2973 ret_v = caml_alloc_small (1, ulaunch); \
2974 Field (ret_v, 0) = str_v; \
2977 case FZ_LINK_NAMED: \
2978 str_v = caml_copy_string (link->dest.ld.named.named); \
2979 ret_v = caml_alloc_small (1, unamed); \
2980 Field (ret_v, 0) = str_v; \
2983 case FZ_LINK_GOTOR: \
2987 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2988 pageno = link->dest.ld.gotor.page; \
2989 if (pageno == -1) { \
2990 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2991 rty = uremotedest; \
2994 gr_v = Val_int (pageno); \
2997 tup_v = caml_alloc_tuple (2); \
2998 ret_v = caml_alloc_small (1, rty); \
2999 Field (tup_v, 0) = str_v; \
3000 Field (tup_v, 1) = gr_v; \
3001 Field (ret_v, 0) = tup_v; \
3009 snprintf (buf, sizeof (buf), \
3010 "unhandled link kind %d", link->dest.kind); \
3011 str_v = caml_copy_string (buf); \
3012 ret_v = caml_alloc_small (1, uunexpected); \
3013 Field (ret_v, 0) = str_v; \
3019 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3021 CAMLparam2 (ptr_v
, n_v
);
3022 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3025 struct pagedim
*pdim
;
3026 char *s
= String_val (ptr_v
);
3028 ret_v
= Val_int (0);
3029 if (trylock ("ml_getlink")) {
3033 page
= parse_pointer ("ml_getlink", s
);
3034 ensureslinks (page
);
3035 pdim
= &state
.pagedims
[page
->pdimno
];
3036 link
= page
->slinks
[Int_val (n_v
)].link
;
3039 unlock ("ml_getlink");
3044 CAMLprim value
ml_getlinkcount (value ptr_v
)
3048 char *s
= String_val (ptr_v
);
3050 page
= parse_pointer ("ml_getlinkcount", s
);
3051 CAMLreturn (Val_int (page
->slinkcount
));
3054 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3056 CAMLparam2 (ptr_v
, n_v
);
3059 struct slink
*slink
;
3060 char *s
= String_val (ptr_v
);
3062 page
= parse_pointer ("ml_getlinkrect", s
);
3063 ret_v
= caml_alloc_tuple (4);
3064 if (trylock ("ml_getlinkrect")) {
3065 Field (ret_v
, 0) = Val_int (0);
3066 Field (ret_v
, 1) = Val_int (0);
3067 Field (ret_v
, 2) = Val_int (0);
3068 Field (ret_v
, 3) = Val_int (0);
3071 ensureslinks (page
);
3073 slink
= &page
->slinks
[Int_val (n_v
)];
3074 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3075 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3076 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3077 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3078 unlock ("ml_getlinkrect");
3084 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3086 CAMLparam3 (ptr_v
, x_v
, y_v
);
3087 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3090 char *ptr
= String_val (ptr_v
);
3091 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3092 struct pagedim
*pdim
;
3094 ret_v
= Val_int (0);
3095 if (trylock ("ml_whatsunder")) {
3099 page
= parse_pointer ("ml_whatsunder", ptr
);
3100 pdim
= &state
.pagedims
[page
->pdimno
];
3101 x
+= pdim
->bounds
.x0
;
3102 y
+= pdim
->bounds
.y0
;
3103 link
= getlink (page
, x
, y
);
3109 fz_page_block
*pageb
;
3110 fz_text_block
*block
;
3113 for (pageb
= page
->text
->blocks
;
3114 pageb
< page
->text
->blocks
+ page
->text
->len
;
3117 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3118 block
= pageb
->u
.text
;
3121 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3124 for (line
= block
->lines
;
3125 line
< block
->lines
+ block
->len
;
3130 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3133 for (span
= line
->first_span
; span
; span
= span
->next
) {
3137 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3140 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3142 fz_text_char_bbox (&bbox
, span
, charnum
);
3145 if (x
>= b
->x0
&& x
<= b
->x1
3146 && y
>= b
->y0
&& y
<= b
->y1
) {
3147 fz_text_style
*style
= span
->text
->style
;
3149 style
->font
&& style
->font
->name
3151 : "Span has no font name"
3153 FT_FaceRec
*face
= style
->font
->ft_face
;
3154 if (face
&& face
->family_name
) {
3156 char *n1
= face
->family_name
;
3157 size_t l1
= strlen (n1
);
3158 size_t l2
= strlen (n2
);
3160 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3161 s
= malloc (l1
+ l2
+ 2);
3165 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3166 str_v
= caml_copy_string (s
);
3171 if (str_v
== Val_unit
) {
3172 str_v
= caml_copy_string (n2
);
3174 ret_v
= caml_alloc_small (1, utext
);
3175 Field (ret_v
, 0) = str_v
;
3184 unlock ("ml_whatsunder");
3190 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3192 static int uninteresting (int c
)
3194 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3198 CAMLprim value
ml_clearmark (value ptr_v
)
3201 char *s
= String_val (ptr_v
);
3204 if (trylock ("ml_clearmark")) {
3208 page
= parse_pointer ("ml_clearmark", s
);
3209 page
->fmark
.span
= NULL
;
3210 page
->lmark
.span
= NULL
;
3214 unlock ("ml_clearmark");
3216 CAMLreturn (Val_unit
);
3219 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3221 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3226 fz_page_block
*pageb
;
3227 fz_text_block
*block
;
3228 struct pagedim
*pdim
;
3229 int mark
= Int_val (mark_v
);
3230 char *s
= String_val (ptr_v
);
3231 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3233 ret_v
= Val_bool (0);
3234 if (trylock ("ml_markunder")) {
3238 page
= parse_pointer ("ml_markunder", s
);
3239 pdim
= &state
.pagedims
[page
->pdimno
];
3243 if (mark
== mark_page
) {
3245 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3247 for (i
= 0; i
< page
->text
->len
; ++i
) {
3248 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3249 pb1
= &page
->text
->blocks
[i
];
3253 if (!pb1
) goto unlock
;
3255 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3256 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3257 pb2
= &page
->text
->blocks
[i
];
3261 if (!pb2
) goto unlock
;
3263 block
= pb1
->u
.text
;
3266 page
->fmark
.span
= block
->lines
->first_span
;
3268 block
= pb2
->u
.text
;
3269 line
= &block
->lines
[block
->len
- 1];
3270 page
->lmark
.i
= line
->last_span
->len
- 1;
3271 page
->lmark
.span
= line
->last_span
;
3272 ret_v
= Val_bool (1);
3276 x
+= pdim
->bounds
.x0
;
3277 y
+= pdim
->bounds
.y0
;
3279 for (pageb
= page
->text
->blocks
;
3280 pageb
< page
->text
->blocks
+ page
->text
->len
;
3282 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3283 block
= pageb
->u
.text
;
3286 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3289 if (mark
== mark_block
) {
3291 page
->fmark
.span
= block
->lines
->first_span
;
3293 line
= &block
->lines
[block
->len
- 1];
3294 page
->lmark
.i
= line
->last_span
->len
- 1;
3295 page
->lmark
.span
= line
->last_span
;
3296 ret_v
= Val_bool (1);
3300 for (line
= block
->lines
;
3301 line
< block
->lines
+ block
->len
;
3306 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3309 if (mark
== mark_line
) {
3311 page
->fmark
.span
= line
->first_span
;
3313 page
->lmark
.i
= line
->last_span
->len
- 1;
3314 page
->lmark
.span
= line
->last_span
;
3315 ret_v
= Val_bool (1);
3319 for (span
= line
->first_span
; span
; span
= span
->next
) {
3323 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3326 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3328 fz_text_char_bbox (&bbox
, span
, charnum
);
3331 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3333 int charnum2
, charnum3
= -1, charnum4
= -1;
3335 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3337 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3338 if (uninteresting (span
->text
[charnum2
].c
)) {
3339 charnum3
= charnum2
+ 1;
3343 if (charnum3
== -1) charnum3
= 0;
3345 for (charnum2
= charnum
+ 1;
3346 charnum2
< span
->len
;
3348 if (uninteresting (span
->text
[charnum2
].c
)) break;
3349 charnum4
= charnum2
;
3351 if (charnum4
== -1) goto unlock
;
3353 page
->fmark
.i
= charnum3
;
3354 page
->fmark
.span
= span
;
3356 page
->lmark
.i
= charnum4
;
3357 page
->lmark
.span
= span
;
3358 ret_v
= Val_bool (1);
3366 if (!Bool_val (ret_v
)) {
3367 page
->fmark
.span
= NULL
;
3368 page
->lmark
.span
= NULL
;
3372 unlock ("ml_markunder");
3378 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3380 CAMLparam3 (ptr_v
, x_v
, y_v
);
3381 CAMLlocal2 (ret_v
, res_v
);
3384 fz_page_block
*pageb
;
3385 struct pagedim
*pdim
;
3386 char *s
= String_val (ptr_v
);
3387 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3389 ret_v
= Val_int (0);
3390 if (trylock ("ml_rectofblock")) {
3394 page
= parse_pointer ("ml_rectofblock", s
);
3395 pdim
= &state
.pagedims
[page
->pdimno
];
3396 x
+= pdim
->bounds
.x0
;
3397 y
+= pdim
->bounds
.y0
;
3401 for (pageb
= page
->text
->blocks
;
3402 pageb
< page
->text
->blocks
+ page
->text
->len
;
3404 switch (pageb
->type
) {
3405 case FZ_PAGE_BLOCK_TEXT
:
3406 b
= &pageb
->u
.text
->bbox
;
3409 case FZ_PAGE_BLOCK_IMAGE
:
3410 b
= &pageb
->u
.image
->bbox
;
3417 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3422 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3423 ret_v
= caml_alloc_small (1, 1);
3424 Store_double_field (res_v
, 0, b
->x0
);
3425 Store_double_field (res_v
, 1, b
->x1
);
3426 Store_double_field (res_v
, 2, b
->y0
);
3427 Store_double_field (res_v
, 3, b
->y1
);
3428 Field (ret_v
, 0) = res_v
;
3430 unlock ("ml_rectofblock");
3436 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3438 CAMLparam2 (ptr_v
, rect_v
);
3441 struct pagedim
*pdim
;
3442 char *s
= String_val (ptr_v
);
3443 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3445 fz_page_block
*pageb
;
3446 fz_text_block
*block
;
3447 fz_text_span
*span
, *fspan
, *lspan
;
3449 if (trylock ("ml_seltext")) {
3453 page
= parse_pointer ("ml_seltext", s
);
3456 pdim
= &state
.pagedims
[page
->pdimno
];
3457 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3458 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3459 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3460 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3471 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3472 glColor3ub (128, 128, 128);
3473 recti (x0
, y0
, x1
, y1
);
3474 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3478 fspan
= page
->fmark
.span
;
3481 lspan
= page
->lmark
.span
;
3483 for (pageb
= page
->text
->blocks
;
3484 pageb
< page
->text
->blocks
+ page
->text
->len
;
3486 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3487 block
= pageb
->u
.text
;
3488 for (line
= block
->lines
;
3489 line
< block
->lines
+ block
->len
;
3492 for (span
= line
->first_span
; span
; span
= span
->next
) {
3493 for (i
= 0; i
< span
->len
; ++i
) {
3496 fz_text_char_bbox (&b
, span
, i
);
3498 if (x0
>= b
.x0
&& x0
<= b
.x1
3499 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3504 if (x1
>= b
.x0
&& x1
<= b
.x1
3505 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3510 if (0 && selected
) {
3511 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3512 glColor3ub (128, 128, 128);
3513 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3514 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3520 if (x1
< x0
&& fspan
== lspan
) {
3532 page
->fmark
.span
= fspan
;
3535 page
->lmark
.span
= lspan
;
3537 unlock ("ml_seltext");
3540 CAMLreturn (Val_unit
);
3543 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3548 for (i
= a
; i
<= b
; ++i
) {
3549 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3550 ret
= fwrite (buf
, len
, 1, f
);
3553 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3554 len
, ret
, strerror (errno
));
3562 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3564 caml_failwith ("ml_popen not implemented under Cygwin");
3567 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3569 CAMLparam2 (command_v
, fds_v
);
3570 CAMLlocal2 (l_v
, tup_v
);
3573 value earg_v
= Nothing
;
3574 posix_spawnattr_t attr
;
3575 posix_spawn_file_actions_t fa
;
3576 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3578 argv
[2] = String_val (command_v
);
3580 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3581 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3584 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3585 msg
= "posix_spawnattr_init";
3589 #ifdef POSIX_SPAWN_USEVFORK
3590 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3591 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3596 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3599 tup_v
= Field (l_v
, 0);
3600 fd1
= Int_val (Field (tup_v
, 0));
3601 fd2
= Int_val (Field (tup_v
, 1));
3603 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3604 msg
= "posix_spawn_file_actions_addclose";
3610 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3611 msg
= "posix_spawn_file_actions_adddup2";
3618 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3619 msg
= "posix_spawn";
3624 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3625 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3629 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3630 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3635 unix_error (ret
, msg
, earg_v
);
3637 CAMLreturn (Val_unit
);
3641 CAMLprim value
ml_hassel (value ptr_v
)
3646 char *s
= String_val (ptr_v
);
3648 ret_v
= Val_bool (0);
3649 if (trylock ("ml_hassel")) {
3653 page
= parse_pointer ("ml_hassel", s
);
3654 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3655 unlock ("ml_hassel");
3660 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3662 CAMLparam2 (fd_v
, ptr_v
);
3667 fz_page_block
*pageb
;
3668 fz_text_block
*block
;
3669 int fd
= Int_val (fd_v
);
3670 char *s
= String_val (ptr_v
);
3672 if (trylock ("ml_copysel")) {
3676 page
= parse_pointer ("ml_copysel", s
);
3678 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3679 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3683 f
= fdopen (fd
, "w");
3685 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3686 fd
, strerror (errno
));
3690 for (pageb
= page
->text
->blocks
;
3691 pageb
< page
->text
->blocks
+ page
->text
->len
;
3693 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3694 block
= pageb
->u
.text
;
3695 for (line
= block
->lines
;
3696 line
< block
->lines
+ block
->len
;
3700 for (span
= line
->first_span
; span
; span
= span
->next
) {
3703 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3704 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3705 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3708 if (pipespan (f
, span
, a
, b
)) {
3711 if (span
== page
->lmark
.span
) {
3714 if (span
== line
->last_span
) {
3715 if (putc ('\n', f
) == EOF
) {
3717 "failed break line on sel pipe: %s\n",
3728 int ret
= fclose (f
);
3731 if (errno
!= ECHILD
) {
3732 fprintf (stderr
, "failed to close sel pipe: %s\n",
3738 unlock ("ml_copysel");
3743 fprintf (stderr
, "failed to close sel pipe: %s\n",
3747 CAMLreturn (Val_unit
);
3750 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3752 CAMLparam1 (pagedimno_v
);
3754 int pagedimno
= Int_val (pagedimno_v
);
3757 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3758 if (trylock ("ml_getpdimrect")) {
3759 box
= fz_empty_rect
;
3762 box
= state
.pagedims
[pagedimno
].mediabox
;
3763 unlock ("ml_getpdimrect");
3766 Store_double_field (ret_v
, 0, box
.x0
);
3767 Store_double_field (ret_v
, 1, box
.x1
);
3768 Store_double_field (ret_v
, 2, box
.y0
);
3769 Store_double_field (ret_v
, 3, box
.y1
);
3774 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3775 value dw_v
, value cols_v
)
3777 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3783 double winw
= Int_val (winw_v
);
3784 double winh
= Int_val (winh_v
);
3785 double dw
= Int_val (dw_v
);
3786 double cols
= Int_val (cols_v
);
3787 double pw
= 1.0, ph
= 1.0;
3789 if (trylock ("ml_zoom_for_height")) {
3793 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3794 double w
= p
->pagebox
.x1
/ cols
;
3795 double h
= p
->pagebox
.y1
;
3799 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3801 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3804 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3805 unlock ("ml_zoom_for_height");
3807 ret_v
= caml_copy_double (zoom
);
3811 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3813 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3815 int pt
= Int_val(pt_v
);
3816 int x
= Int_val (x_v
);
3817 int y
= Int_val (y_v
);
3820 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3821 ret_v
= caml_copy_double (w
);
3825 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3827 CAMLparam2 (pt_v
, string_v
);
3829 int pt
= Int_val (pt_v
);
3832 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3833 ret_v
= caml_copy_double (w
);
3837 CAMLprim value
ml_getpagebox (value opaque_v
)
3839 CAMLparam1 (opaque_v
);
3845 char *s
= String_val (opaque_v
);
3846 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3848 ret_v
= caml_alloc_tuple (4);
3849 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3850 dev
->hints
|= FZ_IGNORE_SHADE
;
3852 switch (page
->type
) {
3854 ctm
= pagectm (page
);
3855 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3859 ctm
= pagectm (page
);
3860 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3864 rect
= fz_infinite_rect
;
3868 fz_free_device (dev
);
3869 fz_round_rect (&bbox
, &rect
);
3870 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3871 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3872 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3873 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3878 CAMLprim value
ml_setaalevel (value level_v
)
3880 CAMLparam1 (level_v
);
3882 state
.aalevel
= Int_val (level_v
);
3883 CAMLreturn (Val_unit
);
3886 #pragma GCC diagnostic push
3887 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3888 #include <X11/Xlib.h>
3889 #pragma GCC diagnostic pop
3897 XVisualInfo
*visual
;
3900 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3902 CAMLparam3 (display_v
, wid_v
, screen_v
);
3903 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3905 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3907 caml_failwith ("XOpenDisplay");
3910 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3912 XCloseDisplay (glx
.dpy
);
3913 caml_failwith ("glXChooseVisual");
3916 glx
.wid
= Int_val (wid_v
);
3917 CAMLreturn (Val_int (glx
.visual
->visualid
));
3920 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3922 CAMLparam1 (unit_v
);
3924 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3926 caml_failwith ("glXCreateContext");
3932 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3933 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3935 caml_failwith ("glXMakeCurrent");
3937 CAMLreturn (Val_unit
);
3940 CAMLprim value
ml_swapb (value unit_v
)
3942 CAMLparam1 (unit_v
);
3943 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3944 CAMLreturn (Val_unit
);
3947 #include "keysym2ucs.c"
3949 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3951 CAMLparam1 (keysym_v
);
3953 KeySym keysym
= Int_val (keysym_v
);
3958 rune
= keysym2ucs (keysym
);
3959 len
= fz_runetochar (buf
, rune
);
3961 str_v
= caml_copy_string (buf
);
3965 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
3967 CAMLprim value
ml_platform (value unit_v
)
3969 CAMLparam1 (unit_v
);
3970 CAMLlocal2 (tup_v
, arr_v
);
3971 int platid
= piunknown
;
3974 #if defined __linux__
3976 #elif defined __CYGWIN__
3978 #elif defined __DragonFly__ || defined __FreeBSD__
3979 || defined __OpenBSD__
|| defined __NetBSD__
3981 #elif defined __sun__
3983 #elif defined __APPLE__
3986 if (uname (&buf
)) err (1, "uname");
3988 tup_v
= caml_alloc_tuple (2);
3990 char const *sar
[] = {
3997 arr_v
= caml_copy_string_array (sar
);
3999 Field (tup_v
, 0) = Val_int (platid
);
4000 Field (tup_v
, 1) = arr_v
;
4004 CAMLprim value
ml_cloexec (value fd_v
)
4007 int fd
= Int_val (fd_v
);
4009 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4010 uerror ("fcntl", Nothing
);
4012 CAMLreturn (Val_unit
);
4015 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4017 CAMLparam2 (w_v
, h_v
);
4020 int w
= Int_val (w_v
);
4021 int h
= Int_val (h_v
);
4022 int cs
= Int_val (cs_v
);
4024 if (state
.pbo_usable
) {
4025 pbo
= calloc (sizeof (*pbo
), 1);
4027 err (1, "calloc pbo");
4039 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4042 state
.glGenBuffersARB (1, &pbo
->id
);
4043 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4044 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4045 NULL
, GL_STREAM_DRAW
);
4046 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4048 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4050 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4051 state
.glDeleteBuffersARB (1, &pbo
->id
);
4053 ret_v
= caml_copy_string ("0");
4059 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4061 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4065 err (1, "malloc %d bytes failed", res
+1);
4067 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4069 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4071 ret_v
= caml_copy_string (s
);
4076 ret_v
= caml_copy_string ("0");
4081 CAMLprim value
ml_freepbo (value s_v
)
4084 char *s
= String_val (s_v
);
4085 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4088 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4090 tile
->pbo
->ptr
= NULL
;
4091 tile
->pbo
->size
= -1;
4093 CAMLreturn (Val_unit
);
4096 CAMLprim value
ml_unmappbo (value s_v
)
4099 char *s
= String_val (s_v
);
4100 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4103 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4104 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4105 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4107 tile
->pbo
->ptr
= NULL
;
4108 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4110 CAMLreturn (Val_unit
);
4113 static void setuppbo (void)
4115 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4116 state
.pbo_usable
= GGPA (glBindBufferARB
)
4117 && GGPA (glUnmapBufferARB
)
4118 && GGPA (glMapBufferARB
)
4119 && GGPA (glBufferDataARB
)
4120 && GGPA (glGenBuffersARB
)
4121 && GGPA (glDeleteBuffersARB
);
4125 CAMLprim value
ml_pbo_usable (value unit_v
)
4127 CAMLparam1 (unit_v
);
4128 CAMLreturn (Val_bool (state
.pbo_usable
));
4131 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4133 CAMLparam3 (ptr_v
, x_v
, y_v
);
4134 CAMLlocal2 (ret_v
, tup_v
);
4136 char *s
= String_val (ptr_v
);
4137 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4138 struct pagedim
*pdim
;
4142 page
= parse_pointer ("ml_unproject", s
);
4143 pdim
= &state
.pagedims
[page
->pdimno
];
4145 ret_v
= Val_int (0);
4146 if (trylock ("ml_unproject")) {
4150 switch (page
->type
) {
4152 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4158 p
.x
= x
+ pdim
->bounds
.x0
;
4159 p
.y
= y
+ pdim
->bounds
.y0
;
4161 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4162 fz_invert_matrix (&ctm
, &ctm
);
4163 fz_transform_point (&p
, &ctm
);
4165 tup_v
= caml_alloc_tuple (2);
4166 ret_v
= caml_alloc_small (1, 1);
4167 Field (tup_v
, 0) = Val_int (p
.x
);
4168 Field (tup_v
, 1) = Val_int (p
.y
);
4169 Field (ret_v
, 0) = tup_v
;
4171 unlock ("ml_unproject");
4176 static void makestippletex (void)
4178 const char pixels
[] = "\xff\xff\0\0";
4179 glGenTextures (1, &state
.stid
);
4180 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4181 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4182 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4195 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4197 return caml_copy_string (FZ_VERSION
);
4200 #ifdef USE_FONTCONFIG
4206 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4210 int UNUSED_ATTR needs_exact_metrics
)
4217 FcPattern
*pat
, *pat1
;
4219 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4220 name
, bold
, italic
, needs_exact_metrics
);
4223 fc
.config
= FcInitLoadConfigAndFonts ();
4225 lprintf ("FcInitLoadConfigAndFonts failed\n");
4229 if (!fc
.config
) return NULL
;
4231 size
= strlen (name
);
4232 if (bold
) size
+= sizeof (":bold") - 1;
4233 if (italic
) size
+= sizeof (":italic") - 1;
4236 buf
= malloc (size
);
4238 err (1, "malloc %zu failed", size
);
4242 if (bold
&& italic
) {
4243 strcat (buf
, ":bold:italic");
4246 if (bold
) strcat (buf
, ":bold");
4247 if (italic
) strcat (buf
, ":italic");
4249 for (i
= 0; i
< size
; ++i
) {
4250 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4253 lprintf ("fcbuf=%s\n", buf
);
4254 pat
= FcNameParse ((FcChar8
*) buf
);
4256 printd ("emsg FcNameParse failed\n");
4261 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4262 printd ("emsg FcConfigSubstitute failed\n");
4266 FcDefaultSubstitute (pat
);
4268 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4270 printd ("emsg FcFontMatch failed\n");
4271 FcPatternDestroy (pat
);
4276 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4277 printd ("emsg FcPatternGetString failed\n");
4278 FcPatternDestroy (pat
);
4279 FcPatternDestroy (pat1
);
4285 printd ("emsg name=%s path=%s\n", name
, path
);
4287 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4288 FcPatternDestroy (pat
);
4289 FcPatternDestroy (pat1
);
4295 CAMLprim value
ml_init (value csock_v
, value params_v
)
4297 CAMLparam2 (csock_v
, params_v
);
4298 CAMLlocal2 (trim_v
, fuzz_v
);
4305 struct sigaction sa
;
4307 state
.csock
= Int_val (csock_v
);
4308 state
.rotate
= Int_val (Field (params_v
, 0));
4309 state
.fitmodel
= Int_val (Field (params_v
, 1));
4310 trim_v
= Field (params_v
, 2);
4311 texcount
= Int_val (Field (params_v
, 3));
4312 state
.sliceheight
= Int_val (Field (params_v
, 4));
4313 mustoresize
= Int_val (Field (params_v
, 5));
4314 colorspace
= Int_val (Field (params_v
, 6));
4315 fontpath
= String_val (Field (params_v
, 7));
4317 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4318 if (!state
.trimcachepath
) {
4319 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4322 haspboext
= Bool_val (Field (params_v
, 9));
4324 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4326 #ifdef USE_FONTCONFIG
4327 if (Bool_val (Field (params_v
, 10))) {
4328 fz_install_load_system_font_funcs (
4329 state
.ctx
, fc_load_system_font_func
, NULL
4334 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4335 fuzz_v
= Field (trim_v
, 1);
4336 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4337 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4338 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4339 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4341 set_tex_params (colorspace
);
4344 #ifndef USE_FONTCONFIG
4345 state
.face
= load_font (fontpath
);
4349 char *buf
= fontpath
;
4350 FcPattern
*pat
, *pat1
;
4353 fc
.config
= FcInitLoadConfigAndFonts ();
4355 errx (1, "FcInitLoadConfigAndFonts failed");
4358 pat
= FcNameParse ((FcChar8
*) buf
);
4360 errx (1, "FcNameParse failed");
4363 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4364 errx (1, "FcConfigSubstitute failed");
4366 FcDefaultSubstitute (pat
);
4368 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4370 errx (1, "FcFontMatch failed");
4373 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4374 errx (1, "FcPatternGetString failed");
4377 state
.face
= load_font ((char *) path
);
4378 FcPatternDestroy (pat
);
4379 FcPatternDestroy (pat1
);
4384 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4386 state
.face
= load_builtin_font (base
, len
);
4388 if (!state
.face
) _exit (1);
4390 realloctexts (texcount
);
4399 sa
.sa_handler
= SIG_IGN
;
4400 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4402 sa
.sa_handler
= SIG_DFL
;
4403 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4405 if (sigemptyset (&sa
.sa_mask
)) {
4406 err (1, "sigemptyset");
4408 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4409 err (1, "sigaction");
4412 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4414 errx (1, "pthread_create: %s", strerror (ret
));
4417 CAMLreturn (Val_unit
);