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
950 /* Following two functions are taken (almost) verbatim from SumatraPDF
954 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
956 #error broken for now
960 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
962 pdf_obj
*pages
= pdf_dict_getp(pdf_trailer(doc
), "Root/Pages");
964 pdf_load_page_objs_rec(doc
, pages
, &page_no
, page_objs
);
968 static void initpdims (void)
972 fz_rect rootmediabox
;
973 int pageno
, trim
, show
;
974 int trimw
= 0, cxcount
;
978 if (state
.trimmargins
&& state
.trimcachepath
) {
979 trimf
= fopen (state
.trimcachepath
, "rb");
981 trimf
= fopen (state
.trimcachepath
, "wb");
986 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
987 cxcount
= state
.pagecount
;
989 cxcount
= MIN (state
.pagecount
, 1);
991 if (state
.type
== DPDF
) {
993 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
994 "Root/Pages/MediaBox");
995 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
998 #ifdef SUMATRA_LOOKUP
999 if (state
.type
== DPDF
1000 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1001 state
.pdflut
.objs
= malloc (sizeof (*state
.pdflut
.objs
) * cxcount
);
1002 if (!state
.pdflut
.objs
) {
1003 err (1, "malloc pageobjs %zu %d %zu failed",
1004 sizeof (*state
.pdflut
.objs
), cxcount
,
1005 sizeof (*state
.pdflut
.objs
) * cxcount
);
1007 state
.pdflut
.count
= cxcount
;
1008 pdf_load_page_objs (state
.u
.pdf
, state
.pdflut
.objs
);
1009 state
.pdflut
.pdf
= state
.u
.pdf
;
1013 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1018 switch (state
.type
) {
1020 pdf_document
*pdf
= state
.u
.pdf
;
1021 pdf_obj
*pageref
, *pageobj
;
1023 #ifdef SUMATRA_LOOKUP
1024 pageref
= state
.pdflut
.objs
[pageno
];
1026 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1028 pageobj
= pdf_resolve_indirect (pageref
);
1030 if (state
.trimmargins
) {
1034 fz_try (state
.ctx
) {
1035 page
= pdf_load_page (pdf
, pageno
);
1036 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1037 trim
= state
.trimanew
|| !obj
;
1043 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1044 dev
->hints
|= FZ_IGNORE_SHADE
;
1045 fz_invert_matrix (&ctm
, &page
->ctm
);
1046 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1047 fz_free_device (dev
);
1049 rect
.x0
+= state
.trimfuzz
.x0
;
1050 rect
.x1
+= state
.trimfuzz
.x1
;
1051 rect
.y0
+= state
.trimfuzz
.y0
;
1052 rect
.y1
+= state
.trimfuzz
.y1
;
1053 fz_transform_rect (&rect
, &ctm
);
1054 fz_intersect_rect (&rect
, &page
->mediabox
);
1056 if (fz_is_empty_rect (&rect
)) {
1057 mediabox
= page
->mediabox
;
1063 obj
= pdf_new_array (pdf
, 4);
1064 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1065 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1066 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1067 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1068 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1071 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1072 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1073 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1074 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1077 rotate
= page
->rotate
;
1078 pdf_free_page (pdf
, page
);
1080 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1082 printd ("progress %f Trimming %d",
1083 (double) (pageno
+ 1) / state
.pagecount
,
1087 fz_catch (state
.ctx
) {
1088 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1095 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1097 if (fz_is_empty_rect (&mediabox
)) {
1105 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1107 if (!fz_is_empty_rect (&cropbox
)) {
1112 fz_intersect_rect (&mediabox
, &cropbox
);
1117 if (fz_is_empty_rect (&rootmediabox
)) {
1119 "cannot find page size for page %d\n",
1123 mediabox
= rootmediabox
;
1127 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1136 fz_try (state
.ctx
) {
1137 page
= xps_load_page (state
.u
.xps
, pageno
);
1138 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1140 if (state
.trimmargins
) {
1144 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1145 dev
->hints
|= FZ_IGNORE_SHADE
;
1146 xps_run_page (state
.u
.xps
, page
, dev
,
1147 &fz_identity
, NULL
);
1148 fz_free_device (dev
);
1150 rect
.x0
+= state
.trimfuzz
.x0
;
1151 rect
.x1
+= state
.trimfuzz
.x1
;
1152 rect
.y0
+= state
.trimfuzz
.y0
;
1153 rect
.y1
+= state
.trimfuzz
.y1
;
1154 fz_intersect_rect (&rect
, &mediabox
);
1156 if (!fz_is_empty_rect (&rect
)) {
1160 xps_free_page (state
.u
.xps
, page
);
1162 printd ("progress %f loading %d",
1163 (double) (pageno
+ 1) / state
.pagecount
,
1167 fz_catch (state
.ctx
) {
1179 if (state
.trimmargins
&& trimw
) {
1182 fz_try (state
.ctx
) {
1183 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1184 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1185 cbz_free_page (state
.u
.cbz
, page
);
1186 printd ("progress %f Trimming %d",
1187 (double) (pageno
+ 1) / state
.pagecount
,
1190 fz_catch (state
.ctx
) {
1191 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1194 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1196 err (1, "fwrite trim mediabox");
1202 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1204 err (1, "fread trim mediabox");
1210 fz_try (state
.ctx
) {
1211 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1212 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1213 cbz_free_page (state
.u
.cbz
, page
);
1215 fz_catch (state
.ctx
) {
1216 fprintf (stderr
, "failed to load cbz page\n");
1234 fz_try (state
.ctx
) {
1235 page
= image_load_page (state
.u
.img
, pageno
);
1236 image_bound_page (state
.u
.img
, page
, &mediabox
);
1237 image_free_page (state
.u
.img
, page
);
1239 fz_catch (state
.ctx
) {
1240 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1246 ARSERT (0 && state
.type
);
1249 if (state
.pagedimcount
== 0
1250 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1251 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1254 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1255 state
.pagedims
= realloc (state
.pagedims
, size
);
1256 if (!state
.pagedims
) {
1257 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1258 size
, state
.pagedimcount
+ 1);
1261 p
= &state
.pagedims
[state
.pagedimcount
++];
1263 p
->mediabox
= mediabox
;
1268 if (state
.trimmargins
) {
1269 printd ("progress 1 Trimmed %d pages in %f seconds",
1270 state
.pagecount
, end
- start
);
1273 printd ("vmsg Processed %d pages in %f seconds",
1274 state
.pagecount
, end
- start
);
1278 if (fclose (trimf
)) {
1284 static void layout (void)
1289 struct pagedim
*p
= p
;
1290 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1292 if (state
.pagedimcount
== 0) return;
1294 switch (state
.fitmodel
) {
1295 case FitProportional
:
1296 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1299 p
= &state
.pagedims
[pindex
];
1300 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1302 fz_transform_rect (&box
, &rm
);
1304 x0
= MIN (box
.x0
, box
.x1
);
1305 x1
= MAX (box
.x0
, box
.x1
);
1308 maxw
= MAX (w
, maxw
);
1309 zoom
= state
.w
/ maxw
;
1321 ARSERT (0 && state
.fitmodel
);
1324 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1328 p
= &state
.pagedims
[pindex
];
1329 fz_rotate (&ctm
, state
.rotate
);
1330 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1332 fz_transform_rect (&box
, &rm
);
1333 w
= box
.x1
- box
.x0
;
1334 switch (state
.fitmodel
) {
1335 case FitProportional
:
1336 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1342 h
= box
.y1
- box
.y0
;
1344 zoom
= MIN (zw
, zh
);
1345 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1354 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1355 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1357 fz_rotate (&rm
, p
->rotate
);
1358 p
->pagebox
= p
->mediabox
;
1359 fz_transform_rect (&p
->pagebox
, &rm
);
1360 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1361 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1365 fz_transform_rect (&rect
, &ctm
);
1366 fz_round_rect (&p
->bounds
, &rect
);
1369 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1370 fz_scale (&sm
, zoom
, -zoom
);
1371 fz_concat (&ctm
, &tm
, &sm
);
1372 fz_concat (&p
->lctm
, &ctm
, &rm
);
1378 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1379 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1380 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1381 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1382 int boundw
= x1
- x0
;
1383 int boundh
= y1
- y0
;
1385 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1386 } while (p
-- != state
.pagedims
);
1390 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1391 desttoanchor (fz_link_dest
*dest
)
1395 struct pagedim
*pdim
= state
.pagedims
;
1400 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1401 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1403 pdim
= &state
.pagedims
[i
];
1405 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1407 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1408 p
.x
= dest
->ld
.gotor
.lt
.x
;
1411 p
.y
= dest
->ld
.gotor
.lt
.y
;
1412 fz_transform_point (&p
, &pdim
->lctm
);
1416 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1417 double x0
, x1
, y0
, y1
;
1419 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1420 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1422 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1423 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1425 a
.n
= dest
->ld
.gotor
.page
;
1430 static void recurse_outline (fz_outline
*outline
, int level
)
1433 switch (outline
->dest
.kind
) {
1436 struct anchor a
= desttoanchor (&outline
->dest
);
1439 printd ("o %d %d %d %d %s",
1440 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1446 printd ("ou %d %" FMT_s
" %s %s", level
,
1447 strlen (outline
->title
), outline
->title
,
1448 outline
->dest
.ld
.uri
.uri
);
1452 printd ("on %d %s", level
, outline
->title
);
1456 printd ("emsg Unhandled outline kind %d for %s\n",
1457 outline
->dest
.kind
, outline
->title
);
1460 if (outline
->down
) {
1461 recurse_outline (outline
->down
, level
+ 1);
1463 outline
= outline
->next
;
1467 static void process_outline (void)
1469 fz_outline
*outline
;
1471 if (!state
.needoutline
) return;
1473 state
.needoutline
= 0;
1474 switch (state
.type
) {
1476 outline
= pdf_load_outline (state
.u
.pdf
);
1479 outline
= xps_load_outline (state
.u
.xps
);
1486 recurse_outline (outline
, 0);
1487 fz_free_outline (state
.ctx
, outline
);
1491 static char *strofspan (fz_text_span
*span
)
1496 size_t size
= 0, cap
= 80;
1498 p
= malloc (cap
+ 1);
1499 if (!p
) return NULL
;
1501 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1502 int n
= fz_runetochar (utf8
, ch
->c
);
1503 if (size
+ n
> cap
) {
1505 p
= realloc (p
, cap
+ 1);
1506 if (!p
) return NULL
;
1509 memcpy (p
+ size
, utf8
, n
);
1516 static int matchspan (regex_t
*re
, fz_text_page UNUSED_ATTR
*page
,
1517 fz_text_span
*span
, fz_matrix UNUSED_ATTR ctm
,
1518 int stop
, int pageno
, double start
)
1525 fz_point p1
, p2
, p3
, p4
;
1527 p
= strofspan (span
);
1530 ret
= regexec (re
, p
, 1, &rm
, 0);
1533 if (ret
!= REG_NOMATCH
) {
1536 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1537 printd ("msg regexec error `%.*s'",
1538 (int) size
, errbuf
);
1546 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1547 c
+= fz_runelen (span
->text
[a
].c
);
1549 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1550 c
+= fz_runelen (span
->text
[b
].c
);
1553 if (fz_runelen (span
->text
[b
].c
) > 1) {
1557 fz_text_char_bbox (&sb
, span
, a
);
1558 fz_text_char_bbox (&eb
, span
, b
);
1570 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1577 printd ("progress 1 found at %d `%.*s' in %f sec",
1578 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1582 printd ("match %d %d %f %f %f %f %f %f %f %f",
1594 static int compareblocks (const void *l
, const void *r
)
1596 fz_text_block
const *ls
= l
;
1597 fz_text_block
const* rs
= r
;
1598 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1601 /* wishful thinking function */
1602 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1607 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1609 fz_text_sheet
*sheet
;
1610 struct pagedim
*pdim
, *pdimprev
;
1611 int stop
= 0, niters
= 0;
1614 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1618 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1619 if (niters
++ == 5) {
1622 printd ("progress 1 attention requested aborting search at %d",
1627 printd ("progress %f searching in page %d",
1628 (double) (pageno
+ 1) / state
.pagecount
,
1633 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1634 pdim
= &state
.pagedims
[i
];
1635 if (pdim
->pageno
== pageno
) {
1638 if (pdim
->pageno
> pageno
) {
1647 sheet
= fz_new_text_sheet (state
.ctx
);
1648 text
= fz_new_text_page (state
.ctx
);
1649 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1651 switch (state
.type
) {
1654 fz_try (state
.ctx
) {
1655 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1656 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1657 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1658 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1659 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1662 fz_catch (state
.ctx
) {
1663 fz_free_device (tdev
);
1670 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1671 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1675 ARSERT (0 && state
.type
);
1678 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1679 fz_free_device (tdev
);
1681 for (j
= 0; j
< text
->len
; ++j
) {
1684 fz_text_block
*block
;
1686 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1687 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1690 for (k
= 0; k
< block
->len
; ++k
) {
1695 line
= &block
->lines
[k
];
1696 if (line
->bbox
.y0
< y
+ 1) continue;
1699 line
= &block
->lines
[block
->len
- 1 - k
];
1700 if (line
->bbox
.y0
> y
- 1) continue;
1703 for (span
= line
->first_span
; span
; span
= span
->next
) {
1704 switch (matchspan (re
, text
, span
, ctm
,
1705 stop
, pageno
, start
)) {
1707 case 1: stop
= 1; break;
1708 case -1: stop
= 1; goto endloop
;
1723 fz_free_text_page (state
.ctx
, text
);
1724 fz_free_text_sheet (state
.ctx
, sheet
);
1726 state
.freepage (u
.ptr
);
1731 printd ("progress 1 no matches %f sec", end
- start
);
1733 printd ("clearrects");
1736 static void set_tex_params (int colorspace
)
1743 switch (colorspace
) {
1745 state
.texiform
= GL_RGBA8
;
1746 state
.texform
= GL_RGBA
;
1747 state
.texty
= GL_UNSIGNED_BYTE
;
1748 state
.colorspace
= fz_device_rgb (state
.ctx
);
1751 state
.texiform
= GL_RGBA8
;
1752 state
.texform
= GL_BGRA
;
1753 state
.texty
= endianness
.s
> 1
1754 ? GL_UNSIGNED_INT_8_8_8_8
1755 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1756 state
.colorspace
= fz_device_bgr (state
.ctx
);
1759 state
.texiform
= GL_LUMINANCE_ALPHA
;
1760 state
.texform
= GL_LUMINANCE_ALPHA
;
1761 state
.texty
= GL_UNSIGNED_BYTE
;
1762 state
.colorspace
= fz_device_gray (state
.ctx
);
1765 errx (1, "invalid colorspce %d", colorspace
);
1769 static void realloctexts (int texcount
)
1773 if (texcount
== state
.texcount
) return;
1775 if (texcount
< state
.texcount
) {
1776 glDeleteTextures (state
.texcount
- texcount
,
1777 state
.texids
+ texcount
);
1780 size
= texcount
* sizeof (*state
.texids
);
1781 state
.texids
= realloc (state
.texids
, size
);
1782 if (!state
.texids
) {
1783 err (1, "realloc texids %" FMT_s
, size
);
1786 size
= texcount
* sizeof (*state
.texowners
);
1787 state
.texowners
= realloc (state
.texowners
, size
);
1788 if (!state
.texowners
) {
1789 err (1, "realloc texowners %" FMT_s
, size
);
1791 if (texcount
> state
.texcount
) {
1794 glGenTextures (texcount
- state
.texcount
,
1795 state
.texids
+ state
.texcount
);
1796 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1797 state
.texowners
[i
].w
= -1;
1798 state
.texowners
[i
].slice
= NULL
;
1801 state
.texcount
= texcount
;
1805 static char *mbtoutf8 (char *s
)
1811 len
= mbstowcs (NULL
, s
, strlen (s
));
1816 if (len
== (size_t) -1) {
1821 tmp
= malloc (len
* sizeof (wchar_t));
1826 ret
= mbstowcs (tmp
, s
, len
);
1827 if (ret
== (size_t) -1) {
1833 for (i
= 0; i
< ret
; ++i
) {
1834 len
+= fz_runelen (tmp
[i
]);
1837 p
= r
= malloc (len
+ 1);
1843 for (i
= 0; i
< ret
; ++i
) {
1844 p
+= fz_runetochar (p
, tmp
[i
]);
1851 CAMLprim value
ml_mbtoutf8 (value s_v
)
1857 s
= String_val (s_v
);
1863 ret_v
= caml_copy_string (r
);
1869 static void * mainloop (void UNUSED_ATTR
*unused
)
1872 int len
, ret
, oldlen
= 0;
1877 errx (1, "readlen returned 0");
1880 if (oldlen
< len
+ 1) {
1881 p
= realloc (p
, len
+ 1);
1883 err (1, "realloc %d failed", len
+ 1);
1890 if (!strncmp ("open", p
, 4)) {
1891 int wthack
, off
, ok
= 0;
1897 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1899 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1902 filename
= p
+ 5 + off
;
1903 filenamelen
= strlen (filename
);
1904 password
= filename
+ filenamelen
+ 1;
1907 fz_try (state
.ctx
) {
1908 openxref (filename
, password
);
1911 fz_catch (state
.ctx
) {
1912 utf8filename
= mbtoutf8 (filename
);
1913 printd ("msg Could not open %s", utf8filename
);
1923 utf8filename
= mbtoutf8 (filename
);
1924 printd ("msg Opened %s (press h/F1 to get help)",
1926 if (utf8filename
!= filename
) {
1927 free (utf8filename
);
1930 state
.needoutline
= 1;
1933 else if (!strncmp ("cs", p
, 2)) {
1936 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1938 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1941 set_tex_params (colorspace
);
1942 for (i
= 0; i
< state
.texcount
; ++i
) {
1943 state
.texowners
[i
].w
= -1;
1944 state
.texowners
[i
].slice
= NULL
;
1948 else if (!strncmp ("freepage", p
, 8)) {
1951 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1953 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1957 else if (!strncmp ("freetile", p
, 8)) {
1960 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1962 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1966 else if (!strncmp ("search", p
, 6)) {
1967 int icase
, pageno
, y
, len2
, forward
;
1971 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1972 &icase
, &pageno
, &y
, &forward
, &len2
);
1974 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1977 pattern
= p
+ 6 + len2
;
1978 ret
= regcomp (&re
, pattern
,
1979 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1984 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1985 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1988 search (&re
, pageno
, y
, forward
);
1992 else if (!strncmp ("geometry", p
, 8)) {
1996 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1998 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2006 for (i
= 0; i
< state
.texcount
; ++i
) {
2007 state
.texowners
[i
].slice
= NULL
;
2010 state
.fitmodel
= fitmodel
;
2015 unlock ("geometry");
2016 printd ("continue %d", state
.pagecount
);
2018 else if (!strncmp ("reqlayout", p
, 9)) {
2021 unsigned int fitmodel
;
2024 ret
= sscanf (p
+ 9, " %d %u %d %n",
2025 &rotate
, &fitmodel
, &h
, &off
);
2027 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2030 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2033 state
.rotate
= rotate
;
2034 state
.fitmodel
= fitmodel
;
2039 nameddest
= p
+ 9 + off
;
2040 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2043 pdf_obj
*needle
, *obj
;
2045 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2046 strlen (nameddest
));
2047 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2049 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2051 a
= desttoanchor (&dest
);
2053 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2056 printd ("emsg failed to parse destination `%s'\n",
2061 printd ("emsg destination `%s' not found\n",
2064 pdf_drop_obj (needle
);
2068 unlock ("reqlayout");
2069 printd ("continue %d", state
.pagecount
);
2071 else if (!strncmp ("page", p
, 4)) {
2076 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2078 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2083 page
= loadpage (pageno
, pindex
);
2087 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2089 else if (!strncmp ("tile", p
, 4)) {
2096 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2097 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2098 SCN_ptr_cast (&data
));
2100 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2105 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2109 printd ("tile %d %d %" FMT_ptr
" %u %f",
2111 FMT_ptr_cast (tile
),
2112 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2115 else if (!strncmp ("settrim", p
, 7)) {
2119 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2120 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2122 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2126 state
.trimmargins
= trimmargins
;
2127 state
.needoutline
= 1;
2128 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2130 state
.trimfuzz
= fuzz
;
2132 state
.pagedimcount
= 0;
2133 free (state
.pagedims
);
2134 state
.pagedims
= NULL
;
2139 printd ("continue %d", state
.pagecount
);
2141 else if (!strncmp ("sliceh", p
, 6)) {
2144 ret
= sscanf (p
+ 6, " %d", &h
);
2146 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2148 if (h
!= state
.sliceheight
) {
2151 state
.sliceheight
= h
;
2152 for (i
= 0; i
< state
.texcount
; ++i
) {
2153 state
.texowners
[i
].w
= -1;
2154 state
.texowners
[i
].h
= -1;
2155 state
.texowners
[i
].slice
= NULL
;
2159 else if (!strncmp ("interrupt", p
, 9)) {
2160 printd ("vmsg interrupted");
2163 errx (1, "unknown command %.*s", len
, p
);
2169 CAMLprim value
ml_realloctexts (value texcount_v
)
2171 CAMLparam1 (texcount_v
);
2174 if (trylock ("ml_realloctexts")) {
2178 realloctexts (Int_val (texcount_v
));
2180 unlock ("ml_realloctexts");
2183 CAMLreturn (Val_bool (ok
));
2186 static void recti (int x0
, int y0
, int x1
, int y1
)
2188 GLfloat
*v
= state
.vertices
;
2190 glVertexPointer (2, GL_FLOAT
, 0, v
);
2191 v
[0] = x0
; v
[1] = y0
;
2192 v
[2] = x1
; v
[3] = y0
;
2193 v
[4] = x0
; v
[5] = y1
;
2194 v
[6] = x1
; v
[7] = y1
;
2195 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2198 static void showsel (struct page
*page
, int ox
, int oy
)
2204 fz_page_block
*pageb
;
2205 fz_text_block
*block
;
2206 struct mark first
, last
;
2207 unsigned char selcolor
[] = {15,15,15,140};
2209 first
= page
->fmark
;
2212 if (!first
.span
|| !last
.span
) return;
2214 glEnable (GL_BLEND
);
2215 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2216 glColor4ubv (selcolor
);
2218 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2219 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2220 for (pageb
= page
->text
->blocks
;
2221 pageb
< page
->text
->blocks
+ page
->text
->len
;
2223 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2224 block
= pageb
->u
.text
;
2226 for (line
= block
->lines
;
2227 line
< block
->lines
+ block
->len
;
2230 rect
= fz_empty_rect
;
2232 for (span
= line
->first_span
; span
; span
= span
->next
) {
2234 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2239 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2241 j
= MIN (first
.i
, last
.i
);
2242 k
= MAX (first
.i
, last
.i
);
2245 if (span
== first
.span
) {
2249 else if (span
== last
.span
) {
2256 for (i
= j
; i
<= k
; ++i
) {
2258 fz_union_rect (&rect
,
2259 fz_text_char_bbox (&bbox1
, span
, i
));
2261 fz_round_rect (&bbox
, &rect
);
2262 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2269 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2270 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2271 if (span
== last
.span
) {
2274 rect
= fz_empty_rect
;
2280 glDisable (GL_BLEND
);
2285 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2287 fz_matrix ctm
, tm
, pm
;
2288 fz_link
*link
, *links
;
2289 GLfloat
*texcoords
= state
.texcoords
;
2290 GLfloat
*vertices
= state
.vertices
;
2292 switch (page
->type
) {
2294 links
= page
->u
.pdfpage
->links
;
2298 links
= page
->u
.xpspage
->links
;
2305 glEnable (GL_TEXTURE_1D
);
2306 glEnable (GL_BLEND
);
2307 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2308 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2310 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2311 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2312 fz_translate (&tm
, xoff
, yoff
);
2313 pm
= pagectm (page
);
2314 fz_concat (&ctm
, &pm
, &tm
);
2316 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2317 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2319 for (link
= links
; link
; link
= link
->next
) {
2320 fz_point p1
, p2
, p3
, p4
;
2322 p1
.x
= link
->rect
.x0
;
2323 p1
.y
= link
->rect
.y0
;
2325 p2
.x
= link
->rect
.x1
;
2326 p2
.y
= link
->rect
.y0
;
2328 p3
.x
= link
->rect
.x1
;
2329 p3
.y
= link
->rect
.y1
;
2331 p4
.x
= link
->rect
.x0
;
2332 p4
.y
= link
->rect
.y1
;
2334 fz_transform_point (&p1
, &ctm
);
2335 fz_transform_point (&p2
, &ctm
);
2336 fz_transform_point (&p3
, &ctm
);
2337 fz_transform_point (&p4
, &ctm
);
2339 switch (link
->dest
.kind
) {
2340 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2341 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2342 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2343 default: glColor3ub (0, 0, 0); break;
2351 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2355 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2357 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2358 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2360 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2361 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2363 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2364 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2366 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2367 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2369 glDrawArrays (GL_LINES
, 0, 8);
2372 glDisable (GL_BLEND
);
2373 glDisable (GL_TEXTURE_1D
);
2376 static int compareslinks (const void *l
, const void *r
)
2378 struct slink
const *ls
= l
;
2379 struct slink
const *rs
= r
;
2380 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2381 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2383 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2386 static void droptext (struct page
*page
)
2389 fz_free_text_page (state
.ctx
, page
->text
);
2392 page
->fmark
.span
= NULL
;
2393 page
->lmark
.span
= NULL
;
2397 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2402 static void dropslinks (struct page
*page
)
2405 free (page
->slinks
);
2406 page
->slinks
= NULL
;
2407 page
->slinkcount
= 0;
2411 static void ensureslinks (struct page
*page
)
2415 size_t slinksize
= sizeof (*page
->slinks
);
2416 fz_link
*link
, *links
;
2418 if (state
.gen
!= page
->sgen
) {
2420 page
->sgen
= state
.gen
;
2422 if (page
->slinks
) return;
2424 switch (page
->type
) {
2426 links
= page
->u
.pdfpage
->links
;
2427 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2429 &state
.pagedims
[page
->pdimno
].tctm
,
2430 &state
.pagedims
[page
->pdimno
].ctm
);
2434 links
= page
->u
.xpspage
->links
;
2435 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2442 for (link
= links
; link
; link
= link
->next
) {
2446 page
->slinkcount
= count
;
2447 page
->slinks
= calloc (count
, slinksize
);
2448 if (!page
->slinks
) {
2449 err (1, "realloc slinks %d", count
);
2452 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2456 fz_transform_rect (&rect
, &ctm
);
2457 page
->slinks
[i
].link
= link
;
2458 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2460 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2464 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2465 static void fmt_linkn (char *s
, unsigned int u
)
2467 unsigned int len
; unsigned int q
;
2468 unsigned int zma
= 'z' - 'a' + 1;
2470 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2473 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2474 /* handles u == 0 */
2479 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2480 int noff
, char *targ
, int tlen
, int hfsize
)
2484 struct slink
*slink
;
2485 double x0
, y0
, x1
, y1
, w
;
2487 ensureslinks (page
);
2488 glColor3ub (0xc3, 0xb0, 0x91);
2489 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2490 fmt_linkn (buf
, i
+ noff
);
2491 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2492 slink
= &page
->slinks
[i
];
2494 x0
= slink
->bbox
.x0
+ xoff
- 5;
2495 y1
= slink
->bbox
.y0
+ yoff
- 5;
2496 y0
= y1
+ 10 + hfsize
;
2497 w
= measure_string (state
.face
, hfsize
, buf
);
2499 recti (x0
, y0
, x1
, y1
);
2503 glEnable (GL_BLEND
);
2504 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2505 glEnable (GL_TEXTURE_2D
);
2506 glColor3ub (0, 0, 0);
2507 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2508 fmt_linkn (buf
, i
+ noff
);
2509 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2510 slink
= &page
->slinks
[i
];
2512 x0
= slink
->bbox
.x0
+ xoff
;
2513 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2514 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2517 glDisable (GL_TEXTURE_2D
);
2518 glDisable (GL_BLEND
);
2521 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2524 struct slice
*slice1
;
2525 unsigned char *texdata
;
2528 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2529 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2531 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2532 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2533 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2537 int texindex
= state
.texindex
++ % state
.texcount
;
2539 if (state
.texowners
[texindex
].w
== tile
->w
) {
2540 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2544 state
.texowners
[texindex
].h
= slice
->h
;
2548 state
.texowners
[texindex
].h
= slice
->h
;
2551 state
.texowners
[texindex
].w
= tile
->w
;
2552 state
.texowners
[texindex
].slice
= slice
;
2553 slice
->texindex
= texindex
;
2555 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2557 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2561 texdata
= tile
->pixmap
->samples
;
2564 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2576 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2588 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2593 CAMLprim value
ml_begintiles (value unit_v
)
2595 CAMLparam1 (unit_v
);
2596 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2597 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2598 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2599 CAMLreturn (unit_v
);
2602 CAMLprim value
ml_endtiles (value unit_v
)
2604 CAMLparam1 (unit_v
);
2605 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2606 CAMLreturn (unit_v
);
2609 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2611 CAMLparam2 (args_v
, ptr_v
);
2612 int dispx
= Int_val (Field (args_v
, 0));
2613 int dispy
= Int_val (Field (args_v
, 1));
2614 int dispw
= Int_val (Field (args_v
, 2));
2615 int disph
= Int_val (Field (args_v
, 3));
2616 int tilex
= Int_val (Field (args_v
, 4));
2617 int tiley
= Int_val (Field (args_v
, 5));
2618 char *s
= String_val (ptr_v
);
2619 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2620 int slicey
, firstslice
;
2621 struct slice
*slice
;
2622 GLfloat
*texcoords
= state
.texcoords
;
2623 GLfloat
*vertices
= state
.vertices
;
2625 firstslice
= tiley
/ tile
->sliceheight
;
2626 slice
= &tile
->slices
[firstslice
];
2627 slicey
= tiley
% tile
->sliceheight
;
2632 dh
= slice
->h
- slicey
;
2633 dh
= MIN (disph
, dh
);
2634 uploadslice (tile
, slice
);
2636 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2637 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2638 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2639 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2641 vertices
[0] = dispx
; vertices
[1] = dispy
;
2642 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2643 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2644 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2646 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2650 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2653 CAMLreturn (Val_unit
);
2656 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2657 value xoff_v
, value yoff_v
,
2660 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2661 int xoff
= Int_val (xoff_v
);
2662 int yoff
= Int_val (yoff_v
);
2663 int noff
= Int_val (Field (li_v
, 0));
2664 char *targ
= String_val (Field (li_v
, 1));
2665 int tlen
= caml_string_length (Field (li_v
, 1));
2666 int hfsize
= Int_val (Field (li_v
, 2));
2667 char *s
= String_val (ptr_v
);
2668 int hlmask
= Int_val (hlinks_v
);
2669 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2672 /* deal with loadpage failed pages */
2676 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2677 if (trylock ("ml_postprocess")) {
2682 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2683 noff
= page
->slinkcount
;
2685 if (page
->tgen
== state
.gen
) {
2686 showsel (page
, xoff
, yoff
);
2688 unlock ("ml_postprocess");
2691 CAMLreturn (Val_int (noff
));
2694 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2698 const fz_matrix
*tctm
;
2699 fz_link
*link
, *links
;
2701 switch (page
->type
) {
2703 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2704 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2705 links
= page
->u
.pdfpage
->links
;
2709 tctm
= &fz_identity
;
2710 links
= page
->u
.xpspage
->links
;
2719 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2720 fz_invert_matrix (&ctm
, &ctm
);
2721 fz_transform_point (&p
, &ctm
);
2723 for (link
= links
; link
; link
= link
->next
) {
2724 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2725 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2733 static void ensuretext (struct page
*page
)
2735 if (state
.gen
!= page
->tgen
) {
2737 page
->tgen
= state
.gen
;
2743 page
->text
= fz_new_text_page (state
.ctx
);
2744 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2745 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2746 ctm
= pagectm (page
);
2747 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2748 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2749 qsort (page
->text
->blocks
, page
->text
->len
,
2750 sizeof (*page
->text
->blocks
), compareblocks
);
2752 fz_free_device (tdev
);
2756 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2758 CAMLparam2 (start_page_v
, dir_v
);
2760 int i
, dir
= Int_val (dir_v
);
2761 int start_page
= Int_val (start_page_v
);
2762 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2764 ret_v
= Val_int (0);
2765 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2769 lock ("ml_findpage_with_links");
2770 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2773 switch (state
.type
) {
2776 pdf_page
*page
= NULL
;
2778 fz_try (state
.ctx
) {
2779 page
= pdf_load_page (state
.u
.pdf
, i
);
2780 found
= !!page
->links
;
2782 fz_catch (state
.ctx
) {
2792 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2793 found
= !!page
->links
;
2799 ARSERT (0 && "invalid document type");
2803 ret_v
= caml_alloc_small (1, 1);
2804 Field (ret_v
, 0) = Val_int (i
);
2809 unlock ("ml_findpage_with_links");
2815 enum { dir_first
, dir_last
};
2816 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2818 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2820 CAMLparam2 (ptr_v
, dir_v
);
2821 CAMLlocal2 (ret_v
, pos_v
);
2823 int dirtag
, i
, slinkindex
;
2824 struct slink
*found
= NULL
,*slink
;
2825 char *s
= String_val (ptr_v
);
2827 page
= parse_pointer ("ml_findlink", s
);
2828 ret_v
= Val_int (0);
2829 if (trylock ("ml_findlink")) {
2833 ensureslinks (page
);
2835 if (Is_block (dir_v
)) {
2836 dirtag
= Tag_val (dir_v
);
2838 case dir_first_visible
:
2840 int x0
, y0
, dir
, first_index
, last_index
;
2842 pos_v
= Field (dir_v
, 0);
2843 x0
= Int_val (Field (pos_v
, 0));
2844 y0
= Int_val (Field (pos_v
, 1));
2845 dir
= Int_val (Field (pos_v
, 2));
2850 last_index
= page
->slinkcount
;
2853 first_index
= page
->slinkcount
- 1;
2857 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2858 slink
= &page
->slinks
[i
];
2859 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2868 slinkindex
= Int_val (Field (dir_v
, 0));
2869 found
= &page
->slinks
[slinkindex
];
2870 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2871 slink
= &page
->slinks
[i
];
2872 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2880 slinkindex
= Int_val (Field (dir_v
, 0));
2881 found
= &page
->slinks
[slinkindex
];
2882 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2883 slink
= &page
->slinks
[i
];
2884 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2892 slinkindex
= Int_val (Field (dir_v
, 0));
2893 found
= &page
->slinks
[slinkindex
];
2894 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2895 slink
= &page
->slinks
[i
];
2896 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2904 slinkindex
= Int_val (Field (dir_v
, 0));
2905 found
= &page
->slinks
[slinkindex
];
2906 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2907 slink
= &page
->slinks
[i
];
2908 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2917 dirtag
= Int_val (dir_v
);
2920 found
= page
->slinks
;
2925 found
= page
->slinks
+ (page
->slinkcount
- 1);
2931 ret_v
= caml_alloc_small (2, 1);
2932 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2935 unlock ("ml_findlink");
2940 enum { uuri
, ugoto
, utext
, uunexpected
,
2941 ulaunch
, unamed
, uremote
, uremotedest
};
2947 switch (link->dest.kind) { \
2948 case FZ_LINK_GOTO: \
2952 pageno = link->dest.ld.gotor.page; \
2956 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2957 p.y = link->dest.ld.gotor.lt.y; \
2958 fz_transform_point (&p, &pdim->lctm); \
2959 if (p.y < 0) p.y = 0; \
2961 tup_v = caml_alloc_tuple (2); \
2962 ret_v = caml_alloc_small (1, ugoto); \
2963 Field (tup_v, 0) = Val_int (pageno); \
2964 Field (tup_v, 1) = Val_int (p.y); \
2965 Field (ret_v, 0) = tup_v; \
2970 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2971 ret_v = caml_alloc_small (1, uuri); \
2972 Field (ret_v, 0) = str_v; \
2975 case FZ_LINK_LAUNCH: \
2976 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2977 ret_v = caml_alloc_small (1, ulaunch); \
2978 Field (ret_v, 0) = str_v; \
2981 case FZ_LINK_NAMED: \
2982 str_v = caml_copy_string (link->dest.ld.named.named); \
2983 ret_v = caml_alloc_small (1, unamed); \
2984 Field (ret_v, 0) = str_v; \
2987 case FZ_LINK_GOTOR: \
2991 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2992 pageno = link->dest.ld.gotor.page; \
2993 if (pageno == -1) { \
2994 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2995 rty = uremotedest; \
2998 gr_v = Val_int (pageno); \
3001 tup_v = caml_alloc_tuple (2); \
3002 ret_v = caml_alloc_small (1, rty); \
3003 Field (tup_v, 0) = str_v; \
3004 Field (tup_v, 1) = gr_v; \
3005 Field (ret_v, 0) = tup_v; \
3013 snprintf (buf, sizeof (buf), \
3014 "unhandled link kind %d", link->dest.kind); \
3015 str_v = caml_copy_string (buf); \
3016 ret_v = caml_alloc_small (1, uunexpected); \
3017 Field (ret_v, 0) = str_v; \
3023 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3025 CAMLparam2 (ptr_v
, n_v
);
3026 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3029 struct pagedim
*pdim
;
3030 char *s
= String_val (ptr_v
);
3032 ret_v
= Val_int (0);
3033 if (trylock ("ml_getlink")) {
3037 page
= parse_pointer ("ml_getlink", s
);
3038 ensureslinks (page
);
3039 pdim
= &state
.pagedims
[page
->pdimno
];
3040 link
= page
->slinks
[Int_val (n_v
)].link
;
3043 unlock ("ml_getlink");
3048 CAMLprim value
ml_getlinkcount (value ptr_v
)
3052 char *s
= String_val (ptr_v
);
3054 page
= parse_pointer ("ml_getlinkcount", s
);
3055 CAMLreturn (Val_int (page
->slinkcount
));
3058 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3060 CAMLparam2 (ptr_v
, n_v
);
3063 struct slink
*slink
;
3064 char *s
= String_val (ptr_v
);
3066 page
= parse_pointer ("ml_getlinkrect", s
);
3067 ret_v
= caml_alloc_tuple (4);
3068 if (trylock ("ml_getlinkrect")) {
3069 Field (ret_v
, 0) = Val_int (0);
3070 Field (ret_v
, 1) = Val_int (0);
3071 Field (ret_v
, 2) = Val_int (0);
3072 Field (ret_v
, 3) = Val_int (0);
3075 ensureslinks (page
);
3077 slink
= &page
->slinks
[Int_val (n_v
)];
3078 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3079 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3080 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3081 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3082 unlock ("ml_getlinkrect");
3088 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3090 CAMLparam3 (ptr_v
, x_v
, y_v
);
3091 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3094 char *ptr
= String_val (ptr_v
);
3095 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3096 struct pagedim
*pdim
;
3098 ret_v
= Val_int (0);
3099 if (trylock ("ml_whatsunder")) {
3103 page
= parse_pointer ("ml_whatsunder", ptr
);
3104 pdim
= &state
.pagedims
[page
->pdimno
];
3105 x
+= pdim
->bounds
.x0
;
3106 y
+= pdim
->bounds
.y0
;
3107 link
= getlink (page
, x
, y
);
3113 fz_page_block
*pageb
;
3114 fz_text_block
*block
;
3117 for (pageb
= page
->text
->blocks
;
3118 pageb
< page
->text
->blocks
+ page
->text
->len
;
3121 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3122 block
= pageb
->u
.text
;
3125 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3128 for (line
= block
->lines
;
3129 line
< block
->lines
+ block
->len
;
3134 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3137 for (span
= line
->first_span
; span
; span
= span
->next
) {
3141 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3144 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3146 fz_text_char_bbox (&bbox
, span
, charnum
);
3149 if (x
>= b
->x0
&& x
<= b
->x1
3150 && y
>= b
->y0
&& y
<= b
->y1
) {
3151 fz_text_style
*style
= span
->text
->style
;
3153 style
->font
&& style
->font
->name
3155 : "Span has no font name"
3157 FT_FaceRec
*face
= style
->font
->ft_face
;
3158 if (face
&& face
->family_name
) {
3160 char *n1
= face
->family_name
;
3161 size_t l1
= strlen (n1
);
3162 size_t l2
= strlen (n2
);
3164 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3165 s
= malloc (l1
+ l2
+ 2);
3169 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3170 str_v
= caml_copy_string (s
);
3175 if (str_v
== Val_unit
) {
3176 str_v
= caml_copy_string (n2
);
3178 ret_v
= caml_alloc_small (1, utext
);
3179 Field (ret_v
, 0) = str_v
;
3188 unlock ("ml_whatsunder");
3194 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3196 static int uninteresting (int c
)
3198 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3202 CAMLprim value
ml_clearmark (value ptr_v
)
3205 char *s
= String_val (ptr_v
);
3208 if (trylock ("ml_clearmark")) {
3212 page
= parse_pointer ("ml_clearmark", s
);
3213 page
->fmark
.span
= NULL
;
3214 page
->lmark
.span
= NULL
;
3218 unlock ("ml_clearmark");
3220 CAMLreturn (Val_unit
);
3223 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3225 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3230 fz_page_block
*pageb
;
3231 fz_text_block
*block
;
3232 struct pagedim
*pdim
;
3233 int mark
= Int_val (mark_v
);
3234 char *s
= String_val (ptr_v
);
3235 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3237 ret_v
= Val_bool (0);
3238 if (trylock ("ml_markunder")) {
3242 page
= parse_pointer ("ml_markunder", s
);
3243 pdim
= &state
.pagedims
[page
->pdimno
];
3247 if (mark
== mark_page
) {
3249 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3251 for (i
= 0; i
< page
->text
->len
; ++i
) {
3252 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3253 pb1
= &page
->text
->blocks
[i
];
3257 if (!pb1
) goto unlock
;
3259 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3260 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3261 pb2
= &page
->text
->blocks
[i
];
3265 if (!pb2
) goto unlock
;
3267 block
= pb1
->u
.text
;
3270 page
->fmark
.span
= block
->lines
->first_span
;
3272 block
= pb2
->u
.text
;
3273 line
= &block
->lines
[block
->len
- 1];
3274 page
->lmark
.i
= line
->last_span
->len
- 1;
3275 page
->lmark
.span
= line
->last_span
;
3276 ret_v
= Val_bool (1);
3280 x
+= pdim
->bounds
.x0
;
3281 y
+= pdim
->bounds
.y0
;
3283 for (pageb
= page
->text
->blocks
;
3284 pageb
< page
->text
->blocks
+ page
->text
->len
;
3286 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3287 block
= pageb
->u
.text
;
3290 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3293 if (mark
== mark_block
) {
3295 page
->fmark
.span
= block
->lines
->first_span
;
3297 line
= &block
->lines
[block
->len
- 1];
3298 page
->lmark
.i
= line
->last_span
->len
- 1;
3299 page
->lmark
.span
= line
->last_span
;
3300 ret_v
= Val_bool (1);
3304 for (line
= block
->lines
;
3305 line
< block
->lines
+ block
->len
;
3310 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3313 if (mark
== mark_line
) {
3315 page
->fmark
.span
= line
->first_span
;
3317 page
->lmark
.i
= line
->last_span
->len
- 1;
3318 page
->lmark
.span
= line
->last_span
;
3319 ret_v
= Val_bool (1);
3323 for (span
= line
->first_span
; span
; span
= span
->next
) {
3327 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3330 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3332 fz_text_char_bbox (&bbox
, span
, charnum
);
3335 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3337 int charnum2
, charnum3
= -1, charnum4
= -1;
3339 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3341 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3342 if (uninteresting (span
->text
[charnum2
].c
)) {
3343 charnum3
= charnum2
+ 1;
3347 if (charnum3
== -1) charnum3
= 0;
3349 for (charnum2
= charnum
+ 1;
3350 charnum2
< span
->len
;
3352 if (uninteresting (span
->text
[charnum2
].c
)) break;
3353 charnum4
= charnum2
;
3355 if (charnum4
== -1) goto unlock
;
3357 page
->fmark
.i
= charnum3
;
3358 page
->fmark
.span
= span
;
3360 page
->lmark
.i
= charnum4
;
3361 page
->lmark
.span
= span
;
3362 ret_v
= Val_bool (1);
3370 if (!Bool_val (ret_v
)) {
3371 page
->fmark
.span
= NULL
;
3372 page
->lmark
.span
= NULL
;
3376 unlock ("ml_markunder");
3382 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3384 CAMLparam3 (ptr_v
, x_v
, y_v
);
3385 CAMLlocal2 (ret_v
, res_v
);
3388 fz_page_block
*pageb
;
3389 struct pagedim
*pdim
;
3390 char *s
= String_val (ptr_v
);
3391 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3393 ret_v
= Val_int (0);
3394 if (trylock ("ml_rectofblock")) {
3398 page
= parse_pointer ("ml_rectofblock", s
);
3399 pdim
= &state
.pagedims
[page
->pdimno
];
3400 x
+= pdim
->bounds
.x0
;
3401 y
+= pdim
->bounds
.y0
;
3405 for (pageb
= page
->text
->blocks
;
3406 pageb
< page
->text
->blocks
+ page
->text
->len
;
3408 switch (pageb
->type
) {
3409 case FZ_PAGE_BLOCK_TEXT
:
3410 b
= &pageb
->u
.text
->bbox
;
3413 case FZ_PAGE_BLOCK_IMAGE
:
3414 b
= &pageb
->u
.image
->bbox
;
3421 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3426 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3427 ret_v
= caml_alloc_small (1, 1);
3428 Store_double_field (res_v
, 0, b
->x0
);
3429 Store_double_field (res_v
, 1, b
->x1
);
3430 Store_double_field (res_v
, 2, b
->y0
);
3431 Store_double_field (res_v
, 3, b
->y1
);
3432 Field (ret_v
, 0) = res_v
;
3434 unlock ("ml_rectofblock");
3440 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3442 CAMLparam2 (ptr_v
, rect_v
);
3445 struct pagedim
*pdim
;
3446 char *s
= String_val (ptr_v
);
3447 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3449 fz_page_block
*pageb
;
3450 fz_text_block
*block
;
3451 fz_text_span
*span
, *fspan
, *lspan
;
3453 if (trylock ("ml_seltext")) {
3457 page
= parse_pointer ("ml_seltext", s
);
3460 pdim
= &state
.pagedims
[page
->pdimno
];
3461 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3462 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3463 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3464 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3475 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3476 glColor3ub (128, 128, 128);
3477 recti (x0
, y0
, x1
, y1
);
3478 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3482 fspan
= page
->fmark
.span
;
3485 lspan
= page
->lmark
.span
;
3487 for (pageb
= page
->text
->blocks
;
3488 pageb
< page
->text
->blocks
+ page
->text
->len
;
3490 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3491 block
= pageb
->u
.text
;
3492 for (line
= block
->lines
;
3493 line
< block
->lines
+ block
->len
;
3496 for (span
= line
->first_span
; span
; span
= span
->next
) {
3497 for (i
= 0; i
< span
->len
; ++i
) {
3500 fz_text_char_bbox (&b
, span
, i
);
3502 if (x0
>= b
.x0
&& x0
<= b
.x1
3503 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3508 if (x1
>= b
.x0
&& x1
<= b
.x1
3509 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3514 if (0 && selected
) {
3515 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3516 glColor3ub (128, 128, 128);
3517 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3518 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3524 if (x1
< x0
&& fspan
== lspan
) {
3536 page
->fmark
.span
= fspan
;
3539 page
->lmark
.span
= lspan
;
3541 unlock ("ml_seltext");
3544 CAMLreturn (Val_unit
);
3547 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3552 for (i
= a
; i
<= b
; ++i
) {
3553 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3554 ret
= fwrite (buf
, len
, 1, f
);
3557 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3558 len
, ret
, strerror (errno
));
3566 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3568 caml_failwith ("ml_popen not implemented under Cygwin");
3571 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3573 CAMLparam2 (command_v
, fds_v
);
3574 CAMLlocal2 (l_v
, tup_v
);
3577 value earg_v
= Nothing
;
3578 posix_spawnattr_t attr
;
3579 posix_spawn_file_actions_t fa
;
3580 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3582 argv
[2] = String_val (command_v
);
3584 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3585 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3588 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3589 msg
= "posix_spawnattr_init";
3593 #ifdef POSIX_SPAWN_USEVFORK
3594 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3595 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3600 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3603 tup_v
= Field (l_v
, 0);
3604 fd1
= Int_val (Field (tup_v
, 0));
3605 fd2
= Int_val (Field (tup_v
, 1));
3607 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3608 msg
= "posix_spawn_file_actions_addclose";
3614 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3615 msg
= "posix_spawn_file_actions_adddup2";
3622 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3623 msg
= "posix_spawn";
3628 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3629 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3633 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3634 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3639 unix_error (ret
, msg
, earg_v
);
3641 CAMLreturn (Val_unit
);
3645 CAMLprim value
ml_hassel (value ptr_v
)
3650 char *s
= String_val (ptr_v
);
3652 ret_v
= Val_bool (0);
3653 if (trylock ("ml_hassel")) {
3657 page
= parse_pointer ("ml_hassel", s
);
3658 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3659 unlock ("ml_hassel");
3664 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3666 CAMLparam2 (fd_v
, ptr_v
);
3671 fz_page_block
*pageb
;
3672 fz_text_block
*block
;
3673 int fd
= Int_val (fd_v
);
3674 char *s
= String_val (ptr_v
);
3676 if (trylock ("ml_copysel")) {
3680 page
= parse_pointer ("ml_copysel", s
);
3682 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3683 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3687 f
= fdopen (fd
, "w");
3689 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3690 fd
, strerror (errno
));
3694 for (pageb
= page
->text
->blocks
;
3695 pageb
< page
->text
->blocks
+ page
->text
->len
;
3697 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3698 block
= pageb
->u
.text
;
3699 for (line
= block
->lines
;
3700 line
< block
->lines
+ block
->len
;
3704 for (span
= line
->first_span
; span
; span
= span
->next
) {
3707 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3708 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3709 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3712 if (pipespan (f
, span
, a
, b
)) {
3715 if (span
== page
->lmark
.span
) {
3718 if (span
== line
->last_span
) {
3719 if (putc ('\n', f
) == EOF
) {
3721 "failed break line on sel pipe: %s\n",
3732 int ret
= fclose (f
);
3735 if (errno
!= ECHILD
) {
3736 fprintf (stderr
, "failed to close sel pipe: %s\n",
3742 unlock ("ml_copysel");
3747 fprintf (stderr
, "failed to close sel pipe: %s\n",
3751 CAMLreturn (Val_unit
);
3754 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3756 CAMLparam1 (pagedimno_v
);
3758 int pagedimno
= Int_val (pagedimno_v
);
3761 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3762 if (trylock ("ml_getpdimrect")) {
3763 box
= fz_empty_rect
;
3766 box
= state
.pagedims
[pagedimno
].mediabox
;
3767 unlock ("ml_getpdimrect");
3770 Store_double_field (ret_v
, 0, box
.x0
);
3771 Store_double_field (ret_v
, 1, box
.x1
);
3772 Store_double_field (ret_v
, 2, box
.y0
);
3773 Store_double_field (ret_v
, 3, box
.y1
);
3778 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3779 value dw_v
, value cols_v
)
3781 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3787 double winw
= Int_val (winw_v
);
3788 double winh
= Int_val (winh_v
);
3789 double dw
= Int_val (dw_v
);
3790 double cols
= Int_val (cols_v
);
3791 double pw
= 1.0, ph
= 1.0;
3793 if (trylock ("ml_zoom_for_height")) {
3797 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3798 double w
= p
->pagebox
.x1
/ cols
;
3799 double h
= p
->pagebox
.y1
;
3803 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3805 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3808 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3809 unlock ("ml_zoom_for_height");
3811 ret_v
= caml_copy_double (zoom
);
3815 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3817 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3819 int pt
= Int_val(pt_v
);
3820 int x
= Int_val (x_v
);
3821 int y
= Int_val (y_v
);
3824 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3825 ret_v
= caml_copy_double (w
);
3829 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3831 CAMLparam2 (pt_v
, string_v
);
3833 int pt
= Int_val (pt_v
);
3836 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3837 ret_v
= caml_copy_double (w
);
3841 CAMLprim value
ml_getpagebox (value opaque_v
)
3843 CAMLparam1 (opaque_v
);
3849 char *s
= String_val (opaque_v
);
3850 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3852 ret_v
= caml_alloc_tuple (4);
3853 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3854 dev
->hints
|= FZ_IGNORE_SHADE
;
3856 switch (page
->type
) {
3858 ctm
= pagectm (page
);
3859 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3863 ctm
= pagectm (page
);
3864 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3868 rect
= fz_infinite_rect
;
3872 fz_free_device (dev
);
3873 fz_round_rect (&bbox
, &rect
);
3874 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3875 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3876 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3877 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3882 CAMLprim value
ml_setaalevel (value level_v
)
3884 CAMLparam1 (level_v
);
3886 state
.aalevel
= Int_val (level_v
);
3887 CAMLreturn (Val_unit
);
3890 #pragma GCC diagnostic push
3891 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3892 #include <X11/Xlib.h>
3893 #pragma GCC diagnostic pop
3901 XVisualInfo
*visual
;
3904 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3906 CAMLparam3 (display_v
, wid_v
, screen_v
);
3907 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3909 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3911 caml_failwith ("XOpenDisplay");
3914 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3916 XCloseDisplay (glx
.dpy
);
3917 caml_failwith ("glXChooseVisual");
3920 glx
.wid
= Int_val (wid_v
);
3921 CAMLreturn (Val_int (glx
.visual
->visualid
));
3924 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3926 CAMLparam1 (unit_v
);
3928 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3930 caml_failwith ("glXCreateContext");
3936 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3937 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3939 caml_failwith ("glXMakeCurrent");
3941 CAMLreturn (Val_unit
);
3944 CAMLprim value
ml_swapb (value unit_v
)
3946 CAMLparam1 (unit_v
);
3947 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3948 CAMLreturn (Val_unit
);
3951 #include "keysym2ucs.c"
3953 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3955 CAMLparam1 (keysym_v
);
3957 KeySym keysym
= Int_val (keysym_v
);
3962 rune
= keysym2ucs (keysym
);
3963 len
= fz_runetochar (buf
, rune
);
3965 str_v
= caml_copy_string (buf
);
3969 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
3971 CAMLprim value
ml_platform (value unit_v
)
3973 CAMLparam1 (unit_v
);
3974 CAMLlocal2 (tup_v
, arr_v
);
3975 int platid
= piunknown
;
3978 #if defined __linux__
3980 #elif defined __CYGWIN__
3982 #elif defined __DragonFly__ || defined __FreeBSD__
3983 || defined __OpenBSD__
|| defined __NetBSD__
3985 #elif defined __sun__
3987 #elif defined __APPLE__
3990 if (uname (&buf
)) err (1, "uname");
3992 tup_v
= caml_alloc_tuple (2);
3994 char const *sar
[] = {
4001 arr_v
= caml_copy_string_array (sar
);
4003 Field (tup_v
, 0) = Val_int (platid
);
4004 Field (tup_v
, 1) = arr_v
;
4008 CAMLprim value
ml_cloexec (value fd_v
)
4011 int fd
= Int_val (fd_v
);
4013 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4014 uerror ("fcntl", Nothing
);
4016 CAMLreturn (Val_unit
);
4019 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4021 CAMLparam2 (w_v
, h_v
);
4024 int w
= Int_val (w_v
);
4025 int h
= Int_val (h_v
);
4026 int cs
= Int_val (cs_v
);
4028 if (state
.pbo_usable
) {
4029 pbo
= calloc (sizeof (*pbo
), 1);
4031 err (1, "calloc pbo");
4043 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4046 state
.glGenBuffersARB (1, &pbo
->id
);
4047 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4048 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4049 NULL
, GL_STREAM_DRAW
);
4050 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4052 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4054 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4055 state
.glDeleteBuffersARB (1, &pbo
->id
);
4057 ret_v
= caml_copy_string ("0");
4063 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4065 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4069 err (1, "malloc %d bytes failed", res
+1);
4071 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4073 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4075 ret_v
= caml_copy_string (s
);
4080 ret_v
= caml_copy_string ("0");
4085 CAMLprim value
ml_freepbo (value s_v
)
4088 char *s
= String_val (s_v
);
4089 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4092 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4094 tile
->pbo
->ptr
= NULL
;
4095 tile
->pbo
->size
= -1;
4097 CAMLreturn (Val_unit
);
4100 CAMLprim value
ml_unmappbo (value s_v
)
4103 char *s
= String_val (s_v
);
4104 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4107 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4108 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4109 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4111 tile
->pbo
->ptr
= NULL
;
4112 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4114 CAMLreturn (Val_unit
);
4117 static void setuppbo (void)
4119 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4120 state
.pbo_usable
= GGPA (glBindBufferARB
)
4121 && GGPA (glUnmapBufferARB
)
4122 && GGPA (glMapBufferARB
)
4123 && GGPA (glBufferDataARB
)
4124 && GGPA (glGenBuffersARB
)
4125 && GGPA (glDeleteBuffersARB
);
4129 CAMLprim value
ml_pbo_usable (value unit_v
)
4131 CAMLparam1 (unit_v
);
4132 CAMLreturn (Val_bool (state
.pbo_usable
));
4135 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4137 CAMLparam3 (ptr_v
, x_v
, y_v
);
4138 CAMLlocal2 (ret_v
, tup_v
);
4140 char *s
= String_val (ptr_v
);
4141 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4142 struct pagedim
*pdim
;
4146 page
= parse_pointer ("ml_unproject", s
);
4147 pdim
= &state
.pagedims
[page
->pdimno
];
4149 ret_v
= Val_int (0);
4150 if (trylock ("ml_unproject")) {
4154 switch (page
->type
) {
4156 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4162 p
.x
= x
+ pdim
->bounds
.x0
;
4163 p
.y
= y
+ pdim
->bounds
.y0
;
4165 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4166 fz_invert_matrix (&ctm
, &ctm
);
4167 fz_transform_point (&p
, &ctm
);
4169 tup_v
= caml_alloc_tuple (2);
4170 ret_v
= caml_alloc_small (1, 1);
4171 Field (tup_v
, 0) = Val_int (p
.x
);
4172 Field (tup_v
, 1) = Val_int (p
.y
);
4173 Field (ret_v
, 0) = tup_v
;
4175 unlock ("ml_unproject");
4180 static void makestippletex (void)
4182 const char pixels
[] = "\xff\xff\0\0";
4183 glGenTextures (1, &state
.stid
);
4184 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4185 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4186 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4199 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4201 return caml_copy_string (FZ_VERSION
);
4204 #ifdef USE_FONTCONFIG
4210 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4214 int UNUSED_ATTR needs_exact_metrics
)
4221 FcPattern
*pat
, *pat1
;
4223 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4224 name
, bold
, italic
, needs_exact_metrics
);
4227 fc
.config
= FcInitLoadConfigAndFonts ();
4229 lprintf ("FcInitLoadConfigAndFonts failed\n");
4233 if (!fc
.config
) return NULL
;
4235 size
= strlen (name
);
4236 if (bold
) size
+= sizeof (":bold") - 1;
4237 if (italic
) size
+= sizeof (":italic") - 1;
4240 buf
= malloc (size
);
4242 err (1, "malloc %zu failed", size
);
4246 if (bold
&& italic
) {
4247 strcat (buf
, ":bold:italic");
4250 if (bold
) strcat (buf
, ":bold");
4251 if (italic
) strcat (buf
, ":italic");
4253 for (i
= 0; i
< size
; ++i
) {
4254 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4257 lprintf ("fcbuf=%s\n", buf
);
4258 pat
= FcNameParse ((FcChar8
*) buf
);
4260 printd ("emsg FcNameParse failed\n");
4265 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4266 printd ("emsg FcConfigSubstitute failed\n");
4270 FcDefaultSubstitute (pat
);
4272 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4274 printd ("emsg FcFontMatch failed\n");
4275 FcPatternDestroy (pat
);
4280 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4281 printd ("emsg FcPatternGetString failed\n");
4282 FcPatternDestroy (pat
);
4283 FcPatternDestroy (pat1
);
4289 printd ("emsg name=%s path=%s\n", name
, path
);
4291 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4292 FcPatternDestroy (pat
);
4293 FcPatternDestroy (pat1
);
4299 CAMLprim value
ml_init (value csock_v
, value params_v
)
4301 CAMLparam2 (csock_v
, params_v
);
4302 CAMLlocal2 (trim_v
, fuzz_v
);
4309 struct sigaction sa
;
4311 state
.csock
= Int_val (csock_v
);
4312 state
.rotate
= Int_val (Field (params_v
, 0));
4313 state
.fitmodel
= Int_val (Field (params_v
, 1));
4314 trim_v
= Field (params_v
, 2);
4315 texcount
= Int_val (Field (params_v
, 3));
4316 state
.sliceheight
= Int_val (Field (params_v
, 4));
4317 mustoresize
= Int_val (Field (params_v
, 5));
4318 colorspace
= Int_val (Field (params_v
, 6));
4319 fontpath
= String_val (Field (params_v
, 7));
4321 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4322 if (!state
.trimcachepath
) {
4323 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4326 haspboext
= Bool_val (Field (params_v
, 9));
4328 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4330 #ifdef USE_FONTCONFIG
4331 if (Bool_val (Field (params_v
, 10))) {
4332 fz_install_load_system_font_funcs (
4333 state
.ctx
, fc_load_system_font_func
, NULL
4338 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4339 fuzz_v
= Field (trim_v
, 1);
4340 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4341 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4342 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4343 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4345 set_tex_params (colorspace
);
4348 #ifndef USE_FONTCONFIG
4349 state
.face
= load_font (fontpath
);
4353 char *buf
= fontpath
;
4354 FcPattern
*pat
, *pat1
;
4357 fc
.config
= FcInitLoadConfigAndFonts ();
4359 errx (1, "FcInitLoadConfigAndFonts failed");
4362 pat
= FcNameParse ((FcChar8
*) buf
);
4364 errx (1, "FcNameParse failed");
4367 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4368 errx (1, "FcConfigSubstitute failed");
4370 FcDefaultSubstitute (pat
);
4372 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4374 errx (1, "FcFontMatch failed");
4377 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4378 errx (1, "FcPatternGetString failed");
4381 state
.face
= load_font ((char *) path
);
4382 FcPatternDestroy (pat
);
4383 FcPatternDestroy (pat1
);
4388 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4390 state
.face
= load_builtin_font (base
, len
);
4392 if (!state
.face
) _exit (1);
4394 realloctexts (texcount
);
4403 sa
.sa_handler
= SIG_IGN
;
4404 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4406 sa
.sa_handler
= SIG_DFL
;
4407 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4409 if (sigemptyset (&sa
.sa_mask
)) {
4410 err (1, "sigemptyset");
4412 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4413 err (1, "sigaction");
4416 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4418 errx (1, "pthread_create: %s", strerror (ret
));
4421 CAMLreturn (Val_unit
);