1 /* lots of code c&p-ed directly from mupdf */
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
17 #include <cygwin/socket.h> /* FIONREAD */
28 #include <caml/fail.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/unixsupport.h>
33 #include <mupdf/fitz.h>
34 #include <mupdf/cbz.h>
35 #include <mupdf/pdf.h>
36 #include <mupdf/xps.h>
37 #include <mupdf/img.h>
39 #include FT_FREETYPE_H
42 #include <fontconfig/fontconfig.h>
48 extern char **environ
;
51 #define MIN(a,b) ((a) < (b) ? (a) : (b))
52 #define MAX(a,b) ((a) > (b) ? (a) : (b))
55 #define NORETURN_ATTR __attribute__ ((noreturn))
56 #define UNUSED_ATTR __attribute__ ((unused))
57 #if !defined __clang__
58 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
60 #define OPTIMIZE_ATTR(n)
62 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
66 #define OPTIMIZE_ATTR(n)
67 #define GCC_FMT_ATTR(a, b)
73 #define FMT_ptr_cast(p) (p)
74 #define FMT_ptr_cast2(p) (p)
76 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
77 err (int exitcode
, const char *fmt
, ...)
84 vfprintf (stderr
, fmt
, ap
);
86 fprintf (stderr
, ": %s\n", strerror (savederrno
));
91 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
92 errx (int exitcode
, const char *fmt
, ...)
97 vfprintf (stderr
, fmt
, ap
);
104 #ifndef GL_TEXTURE_RECTANGLE_ARB
105 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
109 #define GL_BGRA 0x80E1
112 #ifndef GL_UNSIGNED_INT_8_8_8_8
113 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
116 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
117 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
121 #define lprintf printf
126 #define ARSERT(cond) for (;;) { \
128 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
144 struct slice slices
[1];
155 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
163 enum { DPDF
, DXPS
, DCBZ
, DIMG
};
172 fz_text_sheet
*sheet
;
180 fz_display_list
*dlist
;
182 struct slink
*slinks
;
187 void (*freepage
) (void *);
193 struct pagedim
*pagedims
;
213 fz_colorspace
*colorspace
;
221 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
235 void (*closedoc
) (void);
236 void (*freepage
) (void *);
244 void (*glBindBufferARB
) (GLenum
, GLuint
);
245 GLboolean (*glUnmapBufferARB
) (GLenum
);
246 void *(*glMapBufferARB
) (GLenum
, GLenum
);
247 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
248 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
249 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
251 GLfloat texcoords
[8];
252 GLfloat vertices
[16];
261 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
263 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
266 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
268 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
271 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
273 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
274 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
277 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
279 static void lock (const char *cap
)
281 int ret
= pthread_mutex_lock (&mutex
);
283 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
287 static void unlock (const char *cap
)
289 int ret
= pthread_mutex_unlock (&mutex
);
291 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
295 static int trylock (const char *cap
)
297 int ret
= pthread_mutex_trylock (&mutex
);
298 if (ret
&& ret
!= EBUSY
) {
299 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
304 static void *parse_pointer (const char *cap
, const char *s
)
309 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
311 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
316 static double now (void)
320 if (gettimeofday (&tv
, NULL
)) {
321 err (1, "gettimeofday");
323 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
326 static int hasdata (void)
329 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
330 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
334 CAMLprim value
ml_hasdata (value fd_v
)
339 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
340 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
341 CAMLreturn (Val_bool (avail
> 0));
344 static void readdata (void *p
, int size
)
349 n
= read (state
.cr
, p
, size
);
351 if (errno
== EINTR
) goto again
;
352 err (1, "read (req %d, ret %zd)", size
, n
);
355 if (!n
) errx (1, "EOF while reading");
356 errx (1, "read (req %d, ret %zd)", size
, n
);
360 static void writedata (char *p
, int size
)
364 p
[0] = (size
>> 24) & 0xff;
365 p
[1] = (size
>> 16) & 0xff;
366 p
[2] = (size
>> 8) & 0xff;
367 p
[3] = (size
>> 0) & 0xff;
369 n
= write (state
.cw
, p
, size
+ 4);
371 if (!n
) errx (1, "EOF while writing data");
372 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
376 static int readlen (void)
381 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
384 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
392 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
395 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
399 if (len
< size
- 4) {
400 writedata (buf
, len
);
406 err (1, "vsnprintf for `%s' failed", fmt
);
408 buf
= realloc (buf
, size
);
413 static void closepdf (void)
416 pdf_close_document (state
.u
.pdf
);
421 static void closexps (void)
424 xps_close_document (state
.u
.xps
);
429 static void closecbz (void)
432 cbz_close_document (state
.u
.cbz
);
437 static void closeimg (void)
440 image_close_document (state
.u
.img
);
445 static void freepdfpage (void *ptr
)
447 pdf_free_page (state
.u
.pdf
, ptr
);
450 static void freeemptyxxxpage (void *ptr
)
455 static void freexpspage (void *ptr
)
457 xps_free_page (state
.u
.xps
, ptr
);
460 static void freecbzpage (void *ptr
)
462 cbz_free_page (state
.u
.cbz
, ptr
);
465 static void freeimgpage (void *ptr
)
467 image_free_page (state
.u
.img
, ptr
);
470 static void openxref (char *filename
, char *password
)
474 for (i
= 0; i
< state
.texcount
; ++i
) {
475 state
.texowners
[i
].w
= -1;
476 state
.texowners
[i
].slice
= NULL
;
479 if (state
.closedoc
) state
.closedoc ();
481 len
= strlen (filename
);
498 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
499 int len2
= strlen (tbl
[i
].ext
);
501 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
502 state
.type
= tbl
[i
].type
;
508 if (state
.pagedims
) {
509 free (state
.pagedims
);
510 state
.pagedims
= NULL
;
512 state
.pagedimcount
= 0;
514 fz_set_aa_level (state
.ctx
, state
.aalevel
);
515 switch (state
.type
) {
517 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
518 if (pdf_needs_password (state
.u
.pdf
)) {
519 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
521 errx (1, "invalid password");
524 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
525 state
.closedoc
= closepdf
;
526 state
.freepage
= freepdfpage
;
530 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
531 state
.pagecount
= xps_count_pages (state
.u
.xps
);
532 state
.closedoc
= closexps
;
533 state
.freepage
= freexpspage
;
537 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
538 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
539 state
.closedoc
= closecbz
;
540 state
.freepage
= freecbzpage
;
544 state
.u
.img
= image_open_document (state
.ctx
, filename
);
546 state
.closedoc
= closeimg
;
547 state
.freepage
= freeimgpage
;
552 static void pdfinfo (void)
554 if (state
.type
== DPDF
) {
557 printd ("info PDF version\t%d.%d",
558 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
560 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
564 char *items
[] = { "Title", "Author", "Creator",
565 "Producer", "CreationDate" };
567 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
568 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
569 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
572 printd ("title %s", s
);
574 printd ("info %s\t%s", items
[i
], s
);
576 fz_free (state
.ctx
, s
);
583 static void unlinktile (struct tile
*tile
)
587 for (i
= 0; i
< tile
->slicecount
; ++i
) {
588 struct slice
*s
= &tile
->slices
[i
];
590 if (s
->texindex
!= -1) {
591 if (state
.texowners
[s
->texindex
].slice
== s
) {
592 state
.texowners
[s
->texindex
].slice
= NULL
;
598 static void freepage (struct page
*page
)
601 fz_free_text_page (state
.ctx
, page
->text
);
604 fz_free_text_sheet (state
.ctx
, page
->sheet
);
609 fz_drop_display_list (state
.ctx
, page
->dlist
);
610 page
->freepage (page
->u
.ptr
);
614 static void freetile (struct tile
*tile
)
619 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
622 fz_drop_pixmap (state
.ctx
, state
.pig
);
624 state
.pig
= tile
->pixmap
;
629 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
638 static int cacheline32bytes
;
640 static void __attribute__ ((constructor
)) clcheck (void)
642 char **envp
= environ
;
647 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
649 cacheline32bytes
= auxv
[1] == 32;
655 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
657 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
658 if (cacheline32bytes
&& size
> 32) {
659 intptr_t a1
, a2
, diff
;
661 vector
unsigned char v
= vec_splat_u8 (-1);
662 vector
unsigned char *p
;
664 a1
= a2
= (intptr_t) pixmap
->samples
;
665 a2
= (a1
+ 31) & ~31;
670 while (a1
!= a2
) *(char *) a1
++ = 0xff;
671 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
672 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
674 vec_st (v
, i
+ 16, p
);
676 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
678 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
681 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
684 static void trimctm (pdf_page
*page
, int pindex
)
687 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
689 if (!pdim
->tctmready
) {
690 if (state
.trimmargins
) {
692 fz_matrix rm
, sm
, tm
, im
, ctm1
;
694 fz_rotate (&rm
, -pdim
->rotate
);
695 fz_scale (&sm
, 1, -1);
696 fz_concat (&ctm
, &rm
, &sm
);
697 realbox
= pdim
->mediabox
;
698 fz_transform_rect (&realbox
, &ctm
);
699 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
700 fz_concat (&ctm1
, &ctm
, &tm
);
701 fz_invert_matrix (&im
, &page
->ctm
);
702 fz_concat (&ctm
, &im
, &ctm1
);
712 static fz_matrix
pagectm (struct page
*page
)
716 if (page
->type
== DPDF
) {
717 trimctm (page
->u
.pdfpage
, page
->pdimno
);
719 &state
.pagedims
[page
->pdimno
].tctm
,
720 &state
.pagedims
[page
->pdimno
].ctm
);
723 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
725 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
726 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
731 static void *loadpage (int pageno
, int pindex
)
736 page
= calloc (sizeof (struct page
), 1);
738 err (1, "calloc page %d", pageno
);
741 page
->dlist
= fz_new_display_list (state
.ctx
);
742 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
744 switch (state
.type
) {
746 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
747 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
749 page
->freepage
= freepdfpage
;
753 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
754 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
756 page
->freepage
= freexpspage
;
760 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
761 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
763 page
->freepage
= freecbzpage
;
767 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
768 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
770 page
->freepage
= freeimgpage
;
774 fz_catch (state
.ctx
) {
776 page
->freepage
= freeemptyxxxpage
;
778 fz_free_device (dev
);
780 page
->pdimno
= pindex
;
781 page
->pageno
= pageno
;
782 page
->sgen
= state
.gen
;
783 page
->tgen
= state
.gen
;
784 page
->type
= state
.type
;
789 static struct tile
*alloctile (int h
)
796 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
797 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
798 tile
= calloc (tilesize
, 1);
800 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
802 for (i
= 0; i
< slicecount
; ++i
) {
803 int sh
= MIN (h
, state
.sliceheight
);
804 tile
->slices
[i
].h
= sh
;
805 tile
->slices
[i
].texindex
= -1;
808 tile
->slicecount
= slicecount
;
809 tile
->sliceheight
= state
.sliceheight
;
819 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
820 const fz_matrix
*ctm
, float alpha
)
822 struct obs
*obs
= dev
->user
;
824 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
826 fz_rect rect
= fz_unit_rect
;
828 fz_transform_rect (&rect
, ctm
);
829 fz_round_rect (&b
, &rect
);
830 fz_intersect_irect (&b
, &obs
->b
);
831 obs
->cured
= b
.x0
== obs
->b
.x0
834 && b
.y1
== obs
->b
.y1
;
838 static int obscured (struct page
*page
, fz_irect bbox
)
845 memset (&dev
, 0, sizeof (dev
));
846 memset (&obs
, 0, sizeof (obs
));
851 dev
.fill_image
= obs_fill_image
;
853 fz_rect_from_irect (&rect
, &bbox
);
854 ctm
= pagectm (page
);
855 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
858 #define OBSCURED obscured
860 #define OBSCURED(a, b) 0
863 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
871 struct pagedim
*pdim
;
873 tile
= alloctile (h
);
874 pdim
= &state
.pagedims
[page
->pdimno
];
879 bbox
.x1
= bbox
.x0
+ w
;
880 bbox
.y1
= bbox
.y0
+ h
;
883 if (state
.pig
->w
== w
885 && state
.pig
->colorspace
== state
.colorspace
) {
886 tile
->pixmap
= state
.pig
;
887 tile
->pixmap
->x
= bbox
.x0
;
888 tile
->pixmap
->y
= bbox
.y0
;
891 fz_drop_pixmap (state
.ctx
, state
.pig
);
898 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
904 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
910 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
911 clearpixmap (tile
->pixmap
);
913 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
914 ctm
= pagectm (page
);
915 fz_rect_from_irect (&rect
, &bbox
);
916 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
917 fz_free_device (dev
);
922 static void initpdims (void)
926 fz_rect rootmediabox
;
927 int pageno
, trim
, show
;
928 int trimw
= 0, cxcount
;
932 if (state
.trimmargins
&& state
.trimcachepath
) {
933 trimf
= fopen (state
.trimcachepath
, "rb");
935 trimf
= fopen (state
.trimcachepath
, "wb");
940 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
941 cxcount
= state
.pagecount
;
943 cxcount
= MIN (state
.pagecount
, 1);
945 if (state
.type
== DPDF
) {
947 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
948 "Root/Pages/MediaBox");
949 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
952 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
957 switch (state
.type
) {
959 pdf_document
*pdf
= state
.u
.pdf
;
960 pdf_obj
*pageref
= pdf_lookup_page_obj (pdf
, pageno
);
961 pdf_obj
*pageobj
= pdf_resolve_indirect (pageref
);
963 if (state
.trimmargins
) {
968 page
= pdf_load_page (pdf
, pageno
);
969 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
970 trim
= state
.trimanew
|| !obj
;
976 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
977 dev
->hints
|= FZ_IGNORE_SHADE
;
978 fz_invert_matrix (&ctm
, &page
->ctm
);
979 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
980 fz_free_device (dev
);
982 rect
.x0
+= state
.trimfuzz
.x0
;
983 rect
.x1
+= state
.trimfuzz
.x1
;
984 rect
.y0
+= state
.trimfuzz
.y0
;
985 rect
.y1
+= state
.trimfuzz
.y1
;
986 fz_transform_rect (&rect
, &ctm
);
987 fz_intersect_rect (&rect
, &page
->mediabox
);
989 if (fz_is_empty_rect (&rect
)) {
990 mediabox
= page
->mediabox
;
996 obj
= pdf_new_array (pdf
, 4);
997 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
998 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
999 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1000 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1001 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1004 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1005 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1006 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1007 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1010 rotate
= page
->rotate
;
1011 pdf_free_page (pdf
, page
);
1013 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1015 printd ("progress %f Trimming %d",
1016 (double) (pageno
+ 1) / state
.pagecount
,
1020 fz_catch (state
.ctx
) {
1021 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1028 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1030 if (fz_is_empty_rect (&mediabox
)) {
1038 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1040 if (!fz_is_empty_rect (&cropbox
)) {
1045 fz_intersect_rect (&mediabox
, &cropbox
);
1050 if (fz_is_empty_rect (&rootmediabox
)) {
1052 "cannot find page size for page %d\n",
1056 mediabox
= rootmediabox
;
1060 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1069 fz_try (state
.ctx
) {
1070 page
= xps_load_page (state
.u
.xps
, pageno
);
1071 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1073 if (state
.trimmargins
) {
1077 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1078 dev
->hints
|= FZ_IGNORE_SHADE
;
1079 xps_run_page (state
.u
.xps
, page
, dev
,
1080 &fz_identity
, NULL
);
1081 fz_free_device (dev
);
1083 rect
.x0
+= state
.trimfuzz
.x0
;
1084 rect
.x1
+= state
.trimfuzz
.x1
;
1085 rect
.y0
+= state
.trimfuzz
.y0
;
1086 rect
.y1
+= state
.trimfuzz
.y1
;
1087 fz_intersect_rect (&rect
, &mediabox
);
1089 if (!fz_is_empty_rect (&rect
)) {
1093 xps_free_page (state
.u
.xps
, page
);
1095 printd ("progress %f loading %d",
1096 (double) (pageno
+ 1) / state
.pagecount
,
1100 fz_catch (state
.ctx
) {
1112 if (state
.trimmargins
&& trimw
) {
1115 fz_try (state
.ctx
) {
1116 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1117 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1118 cbz_free_page (state
.u
.cbz
, page
);
1119 printd ("progress %f Trimming %d",
1120 (double) (pageno
+ 1) / state
.pagecount
,
1123 fz_catch (state
.ctx
) {
1124 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1127 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1129 err (1, "fwrite trim mediabox");
1135 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1137 err (1, "fread trim mediabox");
1143 fz_try (state
.ctx
) {
1144 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1145 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1146 cbz_free_page (state
.u
.cbz
, page
);
1148 fz_catch (state
.ctx
) {
1149 fprintf (stderr
, "failed to load cbz page\n");
1167 fz_try (state
.ctx
) {
1168 page
= image_load_page (state
.u
.img
, pageno
);
1169 image_bound_page (state
.u
.img
, page
, &mediabox
);
1170 image_free_page (state
.u
.img
, page
);
1172 fz_catch (state
.ctx
) {
1173 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1179 ARSERT (0 && state
.type
);
1182 if (state
.pagedimcount
== 0
1183 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1184 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1187 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1188 state
.pagedims
= realloc (state
.pagedims
, size
);
1189 if (!state
.pagedims
) {
1190 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1191 size
, state
.pagedimcount
+ 1);
1194 p
= &state
.pagedims
[state
.pagedimcount
++];
1196 p
->mediabox
= mediabox
;
1201 if (state
.trimmargins
) {
1202 printd ("progress 1 Trimmed %d pages in %f seconds",
1203 state
.pagecount
, end
- start
);
1206 printd ("vmsg Processed %d pages in %f seconds",
1207 state
.pagecount
, end
- start
);
1211 if (fclose (trimf
)) {
1217 static void layout (void)
1222 struct pagedim
*p
= p
;
1223 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1225 if (state
.pagedimcount
== 0) return;
1227 switch (state
.fitmodel
) {
1228 case FitProportional
:
1229 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1232 p
= &state
.pagedims
[pindex
];
1233 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1235 fz_transform_rect (&box
, &rm
);
1237 x0
= MIN (box
.x0
, box
.x1
);
1238 x1
= MAX (box
.x0
, box
.x1
);
1241 maxw
= MAX (w
, maxw
);
1242 zoom
= state
.w
/ maxw
;
1254 ARSERT (0 && state
.fitmodel
);
1257 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1261 p
= &state
.pagedims
[pindex
];
1262 fz_rotate (&ctm
, state
.rotate
);
1263 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1265 fz_transform_rect (&box
, &rm
);
1266 w
= box
.x1
- box
.x0
;
1267 switch (state
.fitmodel
) {
1268 case FitProportional
:
1269 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1275 h
= box
.y1
- box
.y0
;
1277 zoom
= MIN (zw
, zh
);
1278 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1287 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1288 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1290 fz_rotate (&rm
, p
->rotate
);
1291 p
->pagebox
= p
->mediabox
;
1292 fz_transform_rect (&p
->pagebox
, &rm
);
1293 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1294 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1298 fz_transform_rect (&rect
, &ctm
);
1299 fz_round_rect (&p
->bounds
, &rect
);
1302 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1303 fz_scale (&sm
, zoom
, -zoom
);
1304 fz_concat (&ctm
, &tm
, &sm
);
1305 fz_concat (&p
->lctm
, &ctm
, &rm
);
1311 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1312 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1313 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1314 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1318 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1319 } while (p
-- != state
.pagedims
);
1323 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1324 desttoanchor (fz_link_dest
*dest
)
1328 struct pagedim
*pdim
= state
.pagedims
;
1333 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1334 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1336 pdim
= &state
.pagedims
[i
];
1338 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1340 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1341 p
.x
= dest
->ld
.gotor
.lt
.x
;
1344 p
.y
= dest
->ld
.gotor
.lt
.y
;
1345 fz_transform_point (&p
, &pdim
->lctm
);
1349 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1350 double x0
, x1
, y0
, y1
;
1352 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1353 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1355 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1356 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1358 a
.n
= dest
->ld
.gotor
.page
;
1363 static void recurse_outline (fz_outline
*outline
, int level
)
1366 switch (outline
->dest
.kind
) {
1369 struct anchor a
= desttoanchor (&outline
->dest
);
1372 printd ("o %d %d %d %d %s",
1373 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1379 printd ("ou %d %" FMT_s
" %s %s", level
,
1380 strlen (outline
->title
), outline
->title
,
1381 outline
->dest
.ld
.uri
.uri
);
1385 printd ("on %d %s", level
, outline
->title
);
1389 printd ("emsg Unhandled outline kind %d for %s\n",
1390 outline
->dest
.kind
, outline
->title
);
1393 if (outline
->down
) {
1394 recurse_outline (outline
->down
, level
+ 1);
1396 outline
= outline
->next
;
1400 static void process_outline (void)
1402 fz_outline
*outline
;
1404 if (!state
.needoutline
) return;
1406 state
.needoutline
= 0;
1407 switch (state
.type
) {
1409 outline
= pdf_load_outline (state
.u
.pdf
);
1412 outline
= xps_load_outline (state
.u
.xps
);
1419 recurse_outline (outline
, 0);
1420 fz_free_outline (state
.ctx
, outline
);
1424 static char *strofspan (fz_text_span
*span
)
1429 size_t size
= 0, cap
= 80;
1431 p
= malloc (cap
+ 1);
1432 if (!p
) return NULL
;
1434 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1435 int n
= fz_runetochar (utf8
, ch
->c
);
1436 if (size
+ n
> cap
) {
1438 p
= realloc (p
, cap
+ 1);
1439 if (!p
) return NULL
;
1442 memcpy (p
+ size
, utf8
, n
);
1449 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1450 fz_text_span
*span
, fz_matrix ctm
,
1451 int stop
, int pageno
, double start
)
1458 fz_point p1
, p2
, p3
, p4
;
1460 p
= strofspan (span
);
1463 ret
= regexec (re
, p
, 1, &rm
, 0);
1466 if (ret
!= REG_NOMATCH
) {
1469 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1470 printd ("msg regexec error `%.*s'",
1471 (int) size
, errbuf
);
1479 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1480 c
+= fz_runelen (span
->text
[a
].c
);
1482 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1483 c
+= fz_runelen (span
->text
[b
].c
);
1486 if (fz_runelen (span
->text
[b
].c
) > 1) {
1490 fz_text_char_bbox (&sb
, span
, a
);
1491 fz_text_char_bbox (&eb
, span
, b
);
1503 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1510 printd ("progress 1 found at %d `%.*s' in %f sec",
1511 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1515 printd ("match %d %d %f %f %f %f %f %f %f %f",
1527 static int compareblocks (const void *l
, const void *r
)
1529 fz_text_block
const *ls
= l
;
1530 fz_text_block
const* rs
= r
;
1531 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1534 /* wishful thinking function */
1535 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1540 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1542 fz_text_sheet
*sheet
;
1543 struct pagedim
*pdim
, *pdimprev
;
1544 int stop
= 0, niters
= 0;
1547 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1551 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1552 if (niters
++ == 5) {
1555 printd ("progress 1 attention requested aborting search at %d",
1560 printd ("progress %f searching in page %d",
1561 (double) (pageno
+ 1) / state
.pagecount
,
1566 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1567 pdim
= &state
.pagedims
[i
];
1568 if (pdim
->pageno
== pageno
) {
1571 if (pdim
->pageno
> pageno
) {
1580 sheet
= fz_new_text_sheet (state
.ctx
);
1581 text
= fz_new_text_page (state
.ctx
);
1582 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1584 switch (state
.type
) {
1587 fz_try (state
.ctx
) {
1588 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1589 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1590 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1591 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1592 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1595 fz_catch (state
.ctx
) {
1596 fz_free_device (tdev
);
1603 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1604 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1608 ARSERT (0 && state
.type
);
1611 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1612 fz_free_device (tdev
);
1614 for (j
= 0; j
< text
->len
; ++j
) {
1617 fz_text_block
*block
;
1619 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1620 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1623 for (k
= 0; k
< block
->len
; ++k
) {
1628 line
= &block
->lines
[k
];
1629 if (line
->bbox
.y0
< y
+ 1) continue;
1632 line
= &block
->lines
[block
->len
- 1 - k
];
1633 if (line
->bbox
.y0
> y
- 1) continue;
1636 for (span
= line
->first_span
; span
; span
= span
->next
) {
1637 switch (matchspan (re
, text
, span
, ctm
,
1638 stop
, pageno
, start
)) {
1640 case 1: stop
= 1; break;
1641 case -1: stop
= 1; goto endloop
;
1656 fz_free_text_page (state
.ctx
, text
);
1657 fz_free_text_sheet (state
.ctx
, sheet
);
1659 state
.freepage (u
.ptr
);
1664 printd ("progress 1 no matches %f sec", end
- start
);
1666 printd ("clearrects");
1669 static void set_tex_params (int colorspace
)
1676 switch (colorspace
) {
1678 state
.texiform
= GL_RGBA8
;
1679 state
.texform
= GL_RGBA
;
1680 state
.texty
= GL_UNSIGNED_BYTE
;
1681 state
.colorspace
= fz_device_rgb (state
.ctx
);
1684 state
.texiform
= GL_RGBA8
;
1685 state
.texform
= GL_BGRA
;
1686 state
.texty
= endianness
.s
> 1
1687 ? GL_UNSIGNED_INT_8_8_8_8
1688 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1689 state
.colorspace
= fz_device_bgr (state
.ctx
);
1692 state
.texiform
= GL_LUMINANCE_ALPHA
;
1693 state
.texform
= GL_LUMINANCE_ALPHA
;
1694 state
.texty
= GL_UNSIGNED_BYTE
;
1695 state
.colorspace
= fz_device_gray (state
.ctx
);
1698 errx (1, "invalid colorspce %d", colorspace
);
1702 static void realloctexts (int texcount
)
1706 if (texcount
== state
.texcount
) return;
1708 if (texcount
< state
.texcount
) {
1709 glDeleteTextures (state
.texcount
- texcount
,
1710 state
.texids
+ texcount
);
1713 size
= texcount
* sizeof (*state
.texids
);
1714 state
.texids
= realloc (state
.texids
, size
);
1715 if (!state
.texids
) {
1716 err (1, "realloc texids %" FMT_s
, size
);
1719 size
= texcount
* sizeof (*state
.texowners
);
1720 state
.texowners
= realloc (state
.texowners
, size
);
1721 if (!state
.texowners
) {
1722 err (1, "realloc texowners %" FMT_s
, size
);
1724 if (texcount
> state
.texcount
) {
1727 glGenTextures (texcount
- state
.texcount
,
1728 state
.texids
+ state
.texcount
);
1729 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1730 state
.texowners
[i
].w
= -1;
1731 state
.texowners
[i
].slice
= NULL
;
1734 state
.texcount
= texcount
;
1738 static char *mbtoutf8 (char *s
)
1744 len
= mbstowcs (NULL
, s
, strlen (s
));
1749 if (len
== (size_t) -1) {
1754 tmp
= malloc (len
* sizeof (wchar_t));
1759 ret
= mbstowcs (tmp
, s
, len
);
1760 if (ret
== (size_t) -1) {
1766 for (i
= 0; i
< ret
; ++i
) {
1767 len
+= fz_runelen (tmp
[i
]);
1770 p
= r
= malloc (len
+ 1);
1776 for (i
= 0; i
< ret
; ++i
) {
1777 p
+= fz_runetochar (p
, tmp
[i
]);
1784 CAMLprim value
ml_mbtoutf8 (value s_v
)
1790 s
= String_val (s_v
);
1796 ret_v
= caml_copy_string (r
);
1802 static void * mainloop (void *unused
)
1805 int len
, ret
, oldlen
= 0;
1810 errx (1, "readlen returned 0");
1813 if (oldlen
< len
+ 1) {
1814 p
= realloc (p
, len
+ 1);
1816 err (1, "realloc %d failed", len
+ 1);
1823 if (!strncmp ("open", p
, 4)) {
1830 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1832 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1835 filename
= p
+ 5 + off
;
1836 filenamelen
= strlen (filename
);
1837 password
= filename
+ filenamelen
+ 1;
1840 openxref (filename
, password
);
1846 utf8filename
= mbtoutf8 (filename
);
1847 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1848 if (utf8filename
!= filename
) {
1849 free (utf8filename
);
1852 state
.needoutline
= 1;
1854 else if (!strncmp ("cs", p
, 2)) {
1857 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1859 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1862 set_tex_params (colorspace
);
1863 for (i
= 0; i
< state
.texcount
; ++i
) {
1864 state
.texowners
[i
].w
= -1;
1865 state
.texowners
[i
].slice
= NULL
;
1869 else if (!strncmp ("freepage", p
, 8)) {
1872 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1874 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1878 else if (!strncmp ("freetile", p
, 8)) {
1881 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1883 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1887 else if (!strncmp ("search", p
, 6)) {
1888 int icase
, pageno
, y
, ret
, len2
, forward
;
1892 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1893 &icase
, &pageno
, &y
, &forward
, &len2
);
1895 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1898 pattern
= p
+ 6 + len2
;
1899 ret
= regcomp (&re
, pattern
,
1900 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1905 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1906 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1909 search (&re
, pageno
, y
, forward
);
1913 else if (!strncmp ("geometry", p
, 8)) {
1917 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1919 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1927 for (i
= 0; i
< state
.texcount
; ++i
) {
1928 state
.texowners
[i
].slice
= NULL
;
1931 state
.fitmodel
= fitmodel
;
1936 unlock ("geometry");
1937 printd ("continue %d", state
.pagecount
);
1939 else if (!strncmp ("reqlayout", p
, 9)) {
1941 int rotate
, fitmodel
, off
, h
;
1944 ret
= sscanf (p
+ 9, " %d %d %d %n",
1945 &rotate
, &fitmodel
, &h
, &off
);
1947 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1950 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1953 state
.rotate
= rotate
;
1954 state
.fitmodel
= fitmodel
;
1959 nameddest
= p
+ 9 + off
;
1960 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1963 pdf_obj
*needle
, *obj
;
1965 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
1966 strlen (nameddest
));
1967 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1969 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
1971 a
= desttoanchor (&dest
);
1973 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1976 printd ("emsg failed to parse destination `%s'\n",
1981 printd ("emsg destination `%s' not found\n",
1984 pdf_drop_obj (needle
);
1988 unlock ("reqlayout");
1989 printd ("continue %d", state
.pagecount
);
1991 else if (!strncmp ("page", p
, 4)) {
1994 int pageno
, pindex
, ret
;
1996 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1998 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2003 page
= loadpage (pageno
, pindex
);
2007 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
2009 else if (!strncmp ("tile", p
, 4)) {
2010 int x
, y
, w
, h
, ret
;
2016 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
2017 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
2019 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2024 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2028 printd ("tile %d %d %" FMT_ptr
" %u %f",
2030 FMT_ptr_cast2 (tile
),
2031 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2034 else if (!strncmp ("settrim", p
, 7)) {
2038 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2039 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2041 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2045 state
.trimmargins
= trimmargins
;
2046 state
.needoutline
= 1;
2047 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2049 state
.trimfuzz
= fuzz
;
2051 state
.pagedimcount
= 0;
2052 free (state
.pagedims
);
2053 state
.pagedims
= NULL
;
2058 printd ("continue %d", state
.pagecount
);
2060 else if (!strncmp ("sliceh", p
, 6)) {
2063 ret
= sscanf (p
+ 6, " %d", &h
);
2065 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2067 if (h
!= state
.sliceheight
) {
2070 state
.sliceheight
= h
;
2071 for (i
= 0; i
< state
.texcount
; ++i
) {
2072 state
.texowners
[i
].w
= -1;
2073 state
.texowners
[i
].h
= -1;
2074 state
.texowners
[i
].slice
= NULL
;
2078 else if (!strncmp ("interrupt", p
, 9)) {
2079 printd ("vmsg interrupted");
2082 errx (1, "unknown command %.*s", len
, p
);
2088 CAMLprim value
ml_realloctexts (value texcount_v
)
2090 CAMLparam1 (texcount_v
);
2093 if (trylock ("ml_realloctexts")) {
2097 realloctexts (Int_val (texcount_v
));
2099 unlock ("ml_realloctexts");
2102 CAMLreturn (Val_bool (ok
));
2105 static void recti (int x0
, int y0
, int x1
, int y1
)
2107 GLfloat
*v
= state
.vertices
;
2109 glVertexPointer (2, GL_FLOAT
, 0, v
);
2110 v
[0] = x0
; v
[1] = y0
;
2111 v
[2] = x1
; v
[3] = y0
;
2112 v
[4] = x0
; v
[5] = y1
;
2113 v
[6] = x1
; v
[7] = y1
;
2114 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2117 static void showsel (struct page
*page
, int ox
, int oy
)
2123 fz_page_block
*pageb
;
2124 fz_text_block
*block
;
2125 struct mark first
, last
;
2126 unsigned char selcolor
[] = {15,15,15,140};
2128 first
= page
->fmark
;
2131 if (!first
.span
|| !last
.span
) return;
2133 glEnable (GL_BLEND
);
2134 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2135 glColor4ubv (selcolor
);
2137 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2138 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2139 for (pageb
= page
->text
->blocks
;
2140 pageb
< page
->text
->blocks
+ page
->text
->len
;
2142 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2143 block
= pageb
->u
.text
;
2145 for (line
= block
->lines
;
2146 line
< block
->lines
+ block
->len
;
2149 rect
= fz_empty_rect
;
2151 for (span
= line
->first_span
; span
; span
= span
->next
) {
2153 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2158 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2160 j
= MIN (first
.i
, last
.i
);
2161 k
= MAX (first
.i
, last
.i
);
2164 if (span
== first
.span
) {
2168 else if (span
== last
.span
) {
2175 for (i
= j
; i
<= k
; ++i
) {
2177 fz_union_rect (&rect
,
2178 fz_text_char_bbox (&bbox
, span
, i
));
2180 fz_round_rect (&bbox
, &rect
);
2181 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2188 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2189 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2190 if (span
== last
.span
) {
2193 rect
= fz_empty_rect
;
2199 glDisable (GL_BLEND
);
2204 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2206 fz_matrix ctm
, tm
, pm
;
2207 fz_link
*link
, *links
;
2208 GLfloat
*texcoords
= state
.texcoords
;
2209 GLfloat
*vertices
= state
.vertices
;
2211 switch (page
->type
) {
2213 links
= page
->u
.pdfpage
->links
;
2217 links
= page
->u
.xpspage
->links
;
2224 glEnable (GL_TEXTURE_1D
);
2225 glEnable (GL_BLEND
);
2226 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2227 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2229 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2230 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2231 fz_translate (&tm
, xoff
, yoff
);
2232 pm
= pagectm (page
);
2233 fz_concat (&ctm
, &pm
, &tm
);
2235 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2236 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2238 for (link
= links
; link
; link
= link
->next
) {
2239 fz_point p1
, p2
, p3
, p4
;
2241 p1
.x
= link
->rect
.x0
;
2242 p1
.y
= link
->rect
.y0
;
2244 p2
.x
= link
->rect
.x1
;
2245 p2
.y
= link
->rect
.y0
;
2247 p3
.x
= link
->rect
.x1
;
2248 p3
.y
= link
->rect
.y1
;
2250 p4
.x
= link
->rect
.x0
;
2251 p4
.y
= link
->rect
.y1
;
2253 fz_transform_point (&p1
, &ctm
);
2254 fz_transform_point (&p2
, &ctm
);
2255 fz_transform_point (&p3
, &ctm
);
2256 fz_transform_point (&p4
, &ctm
);
2258 switch (link
->dest
.kind
) {
2259 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2260 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2261 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2262 default: glColor3ub (0, 0, 0); break;
2270 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2274 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2276 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2277 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2279 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2280 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2282 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2283 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2285 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2286 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2288 glDrawArrays (GL_LINES
, 0, 8);
2291 glDisable (GL_BLEND
);
2292 glDisable (GL_TEXTURE_1D
);
2295 static int compareslinks (const void *l
, const void *r
)
2297 struct slink
const *ls
= l
;
2298 struct slink
const *rs
= r
;
2299 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2300 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2302 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2305 static void droptext (struct page
*page
)
2308 fz_free_text_page (state
.ctx
, page
->text
);
2311 page
->fmark
.span
= NULL
;
2312 page
->lmark
.span
= NULL
;
2316 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2321 static void dropslinks (struct page
*page
)
2324 free (page
->slinks
);
2325 page
->slinks
= NULL
;
2326 page
->slinkcount
= 0;
2330 static void ensureslinks (struct page
*page
)
2334 size_t slinksize
= sizeof (*page
->slinks
);
2335 fz_link
*link
, *links
;
2337 if (state
.gen
!= page
->sgen
) {
2339 page
->sgen
= state
.gen
;
2341 if (page
->slinks
) return;
2343 switch (page
->type
) {
2345 links
= page
->u
.pdfpage
->links
;
2346 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2348 &state
.pagedims
[page
->pdimno
].tctm
,
2349 &state
.pagedims
[page
->pdimno
].ctm
);
2353 links
= page
->u
.xpspage
->links
;
2354 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2361 for (link
= links
; link
; link
= link
->next
) {
2365 page
->slinkcount
= count
;
2366 page
->slinks
= calloc (count
, slinksize
);
2367 if (!page
->slinks
) {
2368 err (1, "realloc slinks %d", count
);
2371 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2375 fz_transform_rect (&rect
, &ctm
);
2376 page
->slinks
[i
].link
= link
;
2377 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2379 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2383 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2384 static void fmt_linkn (char *s
, unsigned int u
)
2386 unsigned int len
; unsigned int q
;
2387 int zma
= 'z' - 'a' + 1;
2389 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2392 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2393 /* handles u == 0 */
2398 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2399 int noff
, char *targ
, int tlen
, int hfsize
)
2403 struct slink
*slink
;
2404 double x0
, y0
, x1
, y1
, w
;
2406 ensureslinks (page
);
2407 glColor3ub (0xc3, 0xb0, 0x91);
2408 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2409 fmt_linkn (buf
, i
+ noff
);
2410 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2411 slink
= &page
->slinks
[i
];
2413 x0
= slink
->bbox
.x0
+ xoff
- 5;
2414 y1
= slink
->bbox
.y0
+ yoff
- 5;
2415 y0
= y1
+ 10 + hfsize
;
2416 w
= measure_string (state
.face
, hfsize
, buf
);
2418 recti (x0
, y0
, x1
, y1
);
2422 glEnable (GL_BLEND
);
2423 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2424 glEnable (GL_TEXTURE_2D
);
2425 glColor3ub (0, 0, 0);
2426 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2427 fmt_linkn (buf
, i
+ noff
);
2428 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2429 slink
= &page
->slinks
[i
];
2431 x0
= slink
->bbox
.x0
+ xoff
;
2432 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2433 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2436 glDisable (GL_TEXTURE_2D
);
2437 glDisable (GL_BLEND
);
2440 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2443 struct slice
*slice1
;
2444 unsigned char *texdata
;
2447 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2448 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2450 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2451 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2452 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2456 int texindex
= state
.texindex
++ % state
.texcount
;
2458 if (state
.texowners
[texindex
].w
== tile
->w
) {
2459 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2463 state
.texowners
[texindex
].h
= slice
->h
;
2467 state
.texowners
[texindex
].h
= slice
->h
;
2470 state
.texowners
[texindex
].w
= tile
->w
;
2471 state
.texowners
[texindex
].slice
= slice
;
2472 slice
->texindex
= texindex
;
2474 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2476 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2480 texdata
= tile
->pixmap
->samples
;
2483 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2495 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2507 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2512 CAMLprim value
ml_begintiles (value unit_v
)
2514 CAMLparam1 (unit_v
);
2515 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2516 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2517 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2518 CAMLreturn (unit_v
);
2521 CAMLprim value
ml_endtiles (value unit_v
)
2523 CAMLparam1 (unit_v
);
2524 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2525 CAMLreturn (unit_v
);
2528 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2530 CAMLparam2 (args_v
, ptr_v
);
2531 int dispx
= Int_val (Field (args_v
, 0));
2532 int dispy
= Int_val (Field (args_v
, 1));
2533 int dispw
= Int_val (Field (args_v
, 2));
2534 int disph
= Int_val (Field (args_v
, 3));
2535 int tilex
= Int_val (Field (args_v
, 4));
2536 int tiley
= Int_val (Field (args_v
, 5));
2537 char *s
= String_val (ptr_v
);
2538 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2539 int slicey
, firstslice
;
2540 struct slice
*slice
;
2541 GLfloat
*texcoords
= state
.texcoords
;
2542 GLfloat
*vertices
= state
.vertices
;
2544 firstslice
= tiley
/ tile
->sliceheight
;
2545 slice
= &tile
->slices
[firstslice
];
2546 slicey
= tiley
% tile
->sliceheight
;
2551 dh
= slice
->h
- slicey
;
2552 dh
= MIN (disph
, dh
);
2553 uploadslice (tile
, slice
);
2555 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2556 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2557 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2558 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2560 vertices
[0] = dispx
; vertices
[1] = dispy
;
2561 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2562 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2563 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2565 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2569 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2572 CAMLreturn (Val_unit
);
2575 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2576 value xoff_v
, value yoff_v
,
2579 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2580 int xoff
= Int_val (xoff_v
);
2581 int yoff
= Int_val (yoff_v
);
2582 int noff
= Int_val (Field (li_v
, 0));
2583 char *targ
= String_val (Field (li_v
, 1));
2584 int tlen
= caml_string_length (Field (li_v
, 1));
2585 int hfsize
= Int_val (Field (li_v
, 2));
2586 char *s
= String_val (ptr_v
);
2587 int hlmask
= Int_val (hlinks_v
);
2588 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2591 /* deal with loadpage failed pages */
2595 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2596 if (trylock ("ml_postprocess")) {
2601 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2602 noff
= page
->slinkcount
;
2604 if (page
->tgen
== state
.gen
) {
2605 showsel (page
, xoff
, yoff
);
2607 unlock ("ml_postprocess");
2610 CAMLreturn (Val_int (noff
));
2613 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2617 const fz_matrix
*tctm
;
2618 fz_link
*link
, *links
;
2620 switch (page
->type
) {
2622 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2623 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2624 links
= page
->u
.pdfpage
->links
;
2628 tctm
= &fz_identity
;
2629 links
= page
->u
.xpspage
->links
;
2638 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2639 fz_invert_matrix (&ctm
, &ctm
);
2640 fz_transform_point (&p
, &ctm
);
2642 for (link
= links
; link
; link
= link
->next
) {
2643 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2644 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2652 static void ensuretext (struct page
*page
)
2654 if (state
.gen
!= page
->tgen
) {
2656 page
->tgen
= state
.gen
;
2662 page
->text
= fz_new_text_page (state
.ctx
);
2663 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2664 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2665 ctm
= pagectm (page
);
2666 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2667 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2668 qsort (page
->text
->blocks
, page
->text
->len
,
2669 sizeof (*page
->text
->blocks
), compareblocks
);
2671 fz_free_device (tdev
);
2675 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2677 CAMLparam2 (start_page_v
, dir_v
);
2679 int i
, dir
= Int_val (dir_v
);
2680 int start_page
= Int_val (start_page_v
);
2681 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2683 ret_v
= Val_int (0);
2684 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2688 lock ("ml_findpage_with_links");
2689 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2692 switch (state
.type
) {
2695 pdf_page
*page
= NULL
;
2697 fz_try (state
.ctx
) {
2698 page
= pdf_load_page (state
.u
.pdf
, i
);
2699 found
= !!page
->links
;
2701 fz_catch (state
.ctx
) {
2711 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2712 found
= !!page
->links
;
2718 ARSERT (0 && "invalid document type");
2722 ret_v
= caml_alloc_small (1, 1);
2723 Field (ret_v
, 0) = Val_int (i
);
2728 unlock ("ml_findpage_with_links");
2734 enum { dir_first
, dir_last
};
2735 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2737 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2739 CAMLparam2 (ptr_v
, dir_v
);
2740 CAMLlocal2 (ret_v
, pos_v
);
2742 int dirtag
, i
, slinkindex
;
2743 struct slink
*found
= NULL
,*slink
;
2744 char *s
= String_val (ptr_v
);
2746 page
= parse_pointer ("ml_findlink", s
);
2747 ret_v
= Val_int (0);
2748 if (trylock ("ml_findlink")) {
2752 ensureslinks (page
);
2754 if (Is_block (dir_v
)) {
2755 dirtag
= Tag_val (dir_v
);
2757 case dir_first_visible
:
2759 int x0
, y0
, dir
, first_index
, last_index
;
2761 pos_v
= Field (dir_v
, 0);
2762 x0
= Int_val (Field (pos_v
, 0));
2763 y0
= Int_val (Field (pos_v
, 1));
2764 dir
= Int_val (Field (pos_v
, 2));
2769 last_index
= page
->slinkcount
;
2772 first_index
= page
->slinkcount
- 1;
2776 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2777 slink
= &page
->slinks
[i
];
2778 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2787 slinkindex
= Int_val (Field (dir_v
, 0));
2788 found
= &page
->slinks
[slinkindex
];
2789 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2790 slink
= &page
->slinks
[i
];
2791 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2799 slinkindex
= Int_val (Field (dir_v
, 0));
2800 found
= &page
->slinks
[slinkindex
];
2801 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2802 slink
= &page
->slinks
[i
];
2803 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2811 slinkindex
= Int_val (Field (dir_v
, 0));
2812 found
= &page
->slinks
[slinkindex
];
2813 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2814 slink
= &page
->slinks
[i
];
2815 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2823 slinkindex
= Int_val (Field (dir_v
, 0));
2824 found
= &page
->slinks
[slinkindex
];
2825 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2826 slink
= &page
->slinks
[i
];
2827 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2836 dirtag
= Int_val (dir_v
);
2839 found
= page
->slinks
;
2844 found
= page
->slinks
+ (page
->slinkcount
- 1);
2850 ret_v
= caml_alloc_small (2, 1);
2851 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2854 unlock ("ml_findlink");
2859 enum { uuri
, ugoto
, utext
, uunexpected
,
2860 ulaunch
, unamed
, uremote
, uremotedest
};
2866 switch (link->dest.kind) { \
2867 case FZ_LINK_GOTO: \
2871 pageno = link->dest.ld.gotor.page; \
2875 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2876 p.y = link->dest.ld.gotor.lt.y; \
2877 fz_transform_point (&p, &pdim->lctm); \
2879 tup_v = caml_alloc_tuple (2); \
2880 ret_v = caml_alloc_small (1, ugoto); \
2881 Field (tup_v, 0) = Val_int (pageno); \
2882 Field (tup_v, 1) = Val_int (p.y); \
2883 Field (ret_v, 0) = tup_v; \
2888 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2889 ret_v = caml_alloc_small (1, uuri); \
2890 Field (ret_v, 0) = str_v; \
2893 case FZ_LINK_LAUNCH: \
2894 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2895 ret_v = caml_alloc_small (1, ulaunch); \
2896 Field (ret_v, 0) = str_v; \
2899 case FZ_LINK_NAMED: \
2900 str_v = caml_copy_string (link->dest.ld.named.named); \
2901 ret_v = caml_alloc_small (1, unamed); \
2902 Field (ret_v, 0) = str_v; \
2905 case FZ_LINK_GOTOR: \
2909 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2910 pageno = link->dest.ld.gotor.page; \
2911 if (pageno == -1) { \
2912 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2913 rty = uremotedest; \
2916 gr_v = Val_int (pageno); \
2919 tup_v = caml_alloc_tuple (2); \
2920 ret_v = caml_alloc_small (1, rty); \
2921 Field (tup_v, 0) = str_v; \
2922 Field (tup_v, 1) = gr_v; \
2923 Field (ret_v, 0) = tup_v; \
2931 snprintf (buf, sizeof (buf), \
2932 "unhandled link kind %d", link->dest.kind); \
2933 str_v = caml_copy_string (buf); \
2934 ret_v = caml_alloc_small (1, uunexpected); \
2935 Field (ret_v, 0) = str_v; \
2941 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2943 CAMLparam2 (ptr_v
, n_v
);
2944 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2947 struct pagedim
*pdim
;
2948 char *s
= String_val (ptr_v
);
2950 ret_v
= Val_int (0);
2951 if (trylock ("ml_getlink")) {
2955 page
= parse_pointer ("ml_getlink", s
);
2956 ensureslinks (page
);
2957 pdim
= &state
.pagedims
[page
->pdimno
];
2958 link
= page
->slinks
[Int_val (n_v
)].link
;
2961 unlock ("ml_getlink");
2966 CAMLprim value
ml_getlinkcount (value ptr_v
)
2970 char *s
= String_val (ptr_v
);
2972 page
= parse_pointer ("ml_getlinkcount", s
);
2973 CAMLreturn (Val_int (page
->slinkcount
));
2976 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2978 CAMLparam2 (ptr_v
, n_v
);
2981 struct slink
*slink
;
2982 char *s
= String_val (ptr_v
);
2984 page
= parse_pointer ("ml_getlinkrect", s
);
2985 ret_v
= caml_alloc_tuple (4);
2986 if (trylock ("ml_getlinkrect")) {
2987 Field (ret_v
, 0) = Val_int (0);
2988 Field (ret_v
, 1) = Val_int (0);
2989 Field (ret_v
, 2) = Val_int (0);
2990 Field (ret_v
, 3) = Val_int (0);
2993 ensureslinks (page
);
2995 slink
= &page
->slinks
[Int_val (n_v
)];
2996 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2997 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2998 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2999 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3000 unlock ("ml_getlinkrect");
3006 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3008 CAMLparam3 (ptr_v
, x_v
, y_v
);
3009 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3012 char *s
= String_val (ptr_v
);
3013 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3014 struct pagedim
*pdim
;
3016 ret_v
= Val_int (0);
3017 if (trylock ("ml_whatsunder")) {
3021 page
= parse_pointer ("ml_whatsunder", s
);
3022 pdim
= &state
.pagedims
[page
->pdimno
];
3023 x
+= pdim
->bounds
.x0
;
3024 y
+= pdim
->bounds
.y0
;
3025 link
= getlink (page
, x
, y
);
3031 fz_page_block
*pageb
;
3032 fz_text_block
*block
;
3035 for (pageb
= page
->text
->blocks
;
3036 pageb
< page
->text
->blocks
+ page
->text
->len
;
3039 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3040 block
= pageb
->u
.text
;
3043 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3046 for (line
= block
->lines
;
3047 line
< block
->lines
+ block
->len
;
3052 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3055 for (span
= line
->first_span
; span
; span
= span
->next
) {
3059 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3062 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3064 fz_text_char_bbox (&bbox
, span
, charnum
);
3067 if (x
>= b
->x0
&& x
<= b
->x1
3068 && y
>= b
->y0
&& y
<= b
->y1
) {
3069 fz_text_style
*style
= span
->text
->style
;
3071 style
->font
&& style
->font
->name
3073 : "Span has no font name"
3075 FT_FaceRec
*face
= style
->font
->ft_face
;
3076 if (face
&& face
->family_name
) {
3078 char *n1
= face
->family_name
;
3079 size_t l1
= strlen (n1
);
3080 size_t l2
= strlen (n2
);
3082 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3083 s
= malloc (l1
+ l2
+ 2);
3087 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3088 str_v
= caml_copy_string (s
);
3094 str_v
= caml_copy_string (n2
);
3096 ret_v
= caml_alloc_small (1, utext
);
3097 Field (ret_v
, 0) = str_v
;
3106 unlock ("ml_whatsunder");
3112 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3114 static int uninteresting (int c
)
3116 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3120 CAMLprim value
ml_clearmark (value ptr_v
)
3123 char *s
= String_val (ptr_v
);
3126 if (trylock ("ml_clearmark")) {
3130 page
= parse_pointer ("ml_clearmark", s
);
3131 page
->fmark
.span
= NULL
;
3132 page
->lmark
.span
= NULL
;
3136 unlock ("ml_clearmark");
3138 CAMLreturn (Val_unit
);
3141 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3143 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3148 fz_page_block
*pageb
;
3149 fz_text_block
*block
;
3150 struct pagedim
*pdim
;
3151 int mark
= Int_val (mark_v
);
3152 char *s
= String_val (ptr_v
);
3153 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3155 ret_v
= Val_bool (0);
3156 if (trylock ("ml_markunder")) {
3160 page
= parse_pointer ("ml_markunder", s
);
3161 pdim
= &state
.pagedims
[page
->pdimno
];
3165 if (mark
== mark_page
) {
3167 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3169 for (i
= 0; i
< page
->text
->len
; ++i
) {
3170 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3171 pb1
= &page
->text
->blocks
[i
];
3175 if (!pb1
) goto unlock
;
3177 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3178 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3179 pb2
= &page
->text
->blocks
[i
];
3183 if (!pb2
) goto unlock
;
3185 block
= pb1
->u
.text
;
3188 page
->fmark
.span
= block
->lines
->first_span
;
3190 block
= pb2
->u
.text
;
3191 line
= &block
->lines
[block
->len
- 1];
3192 page
->lmark
.i
= line
->last_span
->len
- 1;
3193 page
->lmark
.span
= line
->last_span
;
3194 ret_v
= Val_bool (1);
3198 x
+= pdim
->bounds
.x0
;
3199 y
+= pdim
->bounds
.y0
;
3201 for (pageb
= page
->text
->blocks
;
3202 pageb
< page
->text
->blocks
+ page
->text
->len
;
3204 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3205 block
= pageb
->u
.text
;
3208 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3211 if (mark
== mark_block
) {
3213 page
->fmark
.span
= block
->lines
->first_span
;
3215 line
= &block
->lines
[block
->len
- 1];
3216 page
->lmark
.i
= line
->last_span
->len
- 1;
3217 page
->lmark
.span
= line
->last_span
;
3218 ret_v
= Val_bool (1);
3222 for (line
= block
->lines
;
3223 line
< block
->lines
+ block
->len
;
3228 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3231 if (mark
== mark_line
) {
3233 page
->fmark
.span
= line
->first_span
;
3235 page
->lmark
.i
= line
->last_span
->len
- 1;
3236 page
->lmark
.span
= line
->last_span
;
3237 ret_v
= Val_bool (1);
3241 for (span
= line
->first_span
; span
; span
= span
->next
) {
3245 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3248 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3250 fz_text_char_bbox (&bbox
, span
, charnum
);
3253 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3255 int charnum2
, charnum3
= -1, charnum4
= -1;
3257 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3259 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3260 if (uninteresting (span
->text
[charnum2
].c
)) {
3261 charnum3
= charnum2
+ 1;
3265 if (charnum3
== -1) charnum3
= 0;
3267 for (charnum2
= charnum
+ 1;
3268 charnum2
< span
->len
;
3270 if (uninteresting (span
->text
[charnum2
].c
)) break;
3271 charnum4
= charnum2
;
3273 if (charnum4
== -1) goto unlock
;
3275 page
->fmark
.i
= charnum3
;
3276 page
->fmark
.span
= span
;
3278 page
->lmark
.i
= charnum4
;
3279 page
->lmark
.span
= span
;
3280 ret_v
= Val_bool (1);
3288 if (!Bool_val (ret_v
)) {
3289 page
->fmark
.span
= NULL
;
3290 page
->lmark
.span
= NULL
;
3294 unlock ("ml_markunder");
3300 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3302 CAMLparam3 (ptr_v
, x_v
, y_v
);
3303 CAMLlocal2 (ret_v
, res_v
);
3306 fz_page_block
*pageb
;
3307 struct pagedim
*pdim
;
3308 char *s
= String_val (ptr_v
);
3309 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3311 ret_v
= Val_int (0);
3312 if (trylock ("ml_rectofblock")) {
3316 page
= parse_pointer ("ml_rectofblock", s
);
3317 pdim
= &state
.pagedims
[page
->pdimno
];
3318 x
+= pdim
->bounds
.x0
;
3319 y
+= pdim
->bounds
.y0
;
3323 for (pageb
= page
->text
->blocks
;
3324 pageb
< page
->text
->blocks
+ page
->text
->len
;
3326 switch (pageb
->type
) {
3327 case FZ_PAGE_BLOCK_TEXT
:
3328 b
= &pageb
->u
.text
->bbox
;
3331 case FZ_PAGE_BLOCK_IMAGE
:
3332 b
= &pageb
->u
.image
->bbox
;
3339 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3344 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3345 ret_v
= caml_alloc_small (1, 1);
3346 Store_double_field (res_v
, 0, b
->x0
);
3347 Store_double_field (res_v
, 1, b
->x1
);
3348 Store_double_field (res_v
, 2, b
->y0
);
3349 Store_double_field (res_v
, 3, b
->y1
);
3350 Field (ret_v
, 0) = res_v
;
3352 unlock ("ml_rectofblock");
3358 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3360 CAMLparam2 (ptr_v
, rect_v
);
3363 struct pagedim
*pdim
;
3364 char *s
= String_val (ptr_v
);
3365 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3367 fz_page_block
*pageb
;
3368 fz_text_block
*block
;
3369 fz_text_span
*span
, *fspan
, *lspan
;
3371 if (trylock ("ml_seltext")) {
3375 page
= parse_pointer ("ml_seltext", s
);
3378 pdim
= &state
.pagedims
[page
->pdimno
];
3379 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3380 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3381 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3382 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3393 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3394 glColor3ub (128, 128, 128);
3395 recti (x0
, y0
, x1
, y1
);
3396 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3400 fspan
= page
->fmark
.span
;
3403 lspan
= page
->lmark
.span
;
3405 for (pageb
= page
->text
->blocks
;
3406 pageb
< page
->text
->blocks
+ page
->text
->len
;
3408 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3409 block
= pageb
->u
.text
;
3410 for (line
= block
->lines
;
3411 line
< block
->lines
+ block
->len
;
3415 for (span
= line
->first_span
; span
; span
= span
->next
) {
3416 for (i
= 0; i
< span
->len
; ++i
) {
3419 fz_text_char_bbox (&b
, span
, i
);
3421 if (x0
>= b
.x0
&& x0
<= b
.x1
3422 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3427 if (x1
>= b
.x0
&& x1
<= b
.x1
3428 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3433 if (0 && selected
) {
3434 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3435 glColor3ub (128, 128, 128);
3436 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3437 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3443 if (x1
< x0
&& fspan
== lspan
) {
3455 page
->fmark
.span
= fspan
;
3458 page
->lmark
.span
= lspan
;
3460 unlock ("ml_seltext");
3463 CAMLreturn (Val_unit
);
3466 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3471 for (i
= a
; i
<= b
; ++i
) {
3472 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3473 ret
= fwrite (buf
, len
, 1, f
);
3476 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3477 len
, ret
, strerror (errno
));
3485 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3487 caml_failwith ("ml_popen not implemented under Cygwin");
3490 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3492 CAMLparam2 (command_v
, fds_v
);
3493 CAMLlocal2 (l_v
, tup_v
);
3496 value earg_v
= Nothing
;
3497 posix_spawnattr_t attr
;
3498 posix_spawn_file_actions_t fa
;
3499 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3501 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3502 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3505 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3506 msg
= "posix_spawnattr_init";
3510 #ifdef POSIX_SPAWN_USEVFORK
3511 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3512 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3517 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3520 tup_v
= Field (l_v
, 0);
3521 fd1
= Int_val (Field (tup_v
, 0));
3522 fd2
= Int_val (Field (tup_v
, 1));
3524 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3525 msg
= "posix_spawn_file_actions_addclose";
3531 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3532 msg
= "posix_spawn_file_actions_adddup2";
3539 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3540 msg
= "posix_spawn";
3545 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3546 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3550 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3551 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3556 unix_error (ret
, msg
, earg_v
);
3558 CAMLreturn (Val_unit
);
3562 CAMLprim value
ml_hassel (value ptr_v
)
3567 char *s
= String_val (ptr_v
);
3569 ret_v
= Val_bool (0);
3570 if (trylock ("ml_hassel")) {
3574 page
= parse_pointer ("ml_hassel", s
);
3575 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3576 unlock ("ml_hassel");
3581 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3583 CAMLparam2 (fd_v
, ptr_v
);
3588 fz_page_block
*pageb
;
3589 fz_text_block
*block
;
3590 int fd
= Int_val (fd_v
);
3591 char *s
= String_val (ptr_v
);
3593 if (trylock ("ml_copysel")) {
3597 page
= parse_pointer ("ml_copysel", s
);
3599 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3600 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3604 f
= fdopen (fd
, "w");
3606 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3607 fd
, strerror (errno
));
3611 for (pageb
= page
->text
->blocks
;
3612 pageb
< page
->text
->blocks
+ page
->text
->len
;
3614 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3615 block
= pageb
->u
.text
;
3616 for (line
= block
->lines
;
3617 line
< block
->lines
+ block
->len
;
3621 for (span
= line
->first_span
; span
; span
= span
->next
) {
3624 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3625 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3626 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3629 if (pipespan (f
, span
, a
, b
)) {
3632 if (span
== page
->lmark
.span
) {
3635 if (span
== line
->last_span
) {
3636 if (putc ('\n', f
) == EOF
) {
3638 "failed break line on sel pipe: %s\n",
3649 int ret
= fclose (f
);
3652 if (errno
!= ECHILD
) {
3653 fprintf (stderr
, "failed to close sel pipe: %s\n",
3659 unlock ("ml_copysel");
3664 fprintf (stderr
, "failed to close sel pipe: %s\n",
3668 CAMLreturn (Val_unit
);
3671 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3673 CAMLparam1 (pagedimno_v
);
3675 int pagedimno
= Int_val (pagedimno_v
);
3678 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3679 if (trylock ("ml_getpdimrect")) {
3680 box
= fz_empty_rect
;
3683 box
= state
.pagedims
[pagedimno
].mediabox
;
3684 unlock ("ml_getpdimrect");
3687 Store_double_field (ret_v
, 0, box
.x0
);
3688 Store_double_field (ret_v
, 1, box
.x1
);
3689 Store_double_field (ret_v
, 2, box
.y0
);
3690 Store_double_field (ret_v
, 3, box
.y1
);
3695 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3696 value dw_v
, value cols_v
)
3698 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3704 double winw
= Int_val (winw_v
);
3705 double winh
= Int_val (winh_v
);
3706 double dw
= Int_val (dw_v
);
3707 double cols
= Int_val (cols_v
);
3708 double pw
= 1.0, ph
= 1.0;
3710 if (trylock ("ml_zoom_for_height")) {
3714 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3715 double w
= p
->pagebox
.x1
/ cols
;
3716 double h
= p
->pagebox
.y1
;
3720 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3722 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3725 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3726 unlock ("ml_zoom_for_height");
3728 ret_v
= caml_copy_double (zoom
);
3732 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3734 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3736 int pt
= Int_val(pt_v
);
3737 int x
= Int_val (x_v
);
3738 int y
= Int_val (y_v
);
3741 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3742 ret_v
= caml_copy_double (w
);
3746 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3748 CAMLparam2 (pt_v
, string_v
);
3750 int pt
= Int_val (pt_v
);
3753 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3754 ret_v
= caml_copy_double (w
);
3758 CAMLprim value
ml_getpagebox (value opaque_v
)
3760 CAMLparam1 (opaque_v
);
3766 char *s
= String_val (opaque_v
);
3767 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3769 ret_v
= caml_alloc_tuple (4);
3770 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3771 dev
->hints
|= FZ_IGNORE_SHADE
;
3773 switch (page
->type
) {
3775 ctm
= pagectm (page
);
3776 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3780 ctm
= pagectm (page
);
3781 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3785 rect
= fz_infinite_rect
;
3789 fz_free_device (dev
);
3790 fz_round_rect (&bbox
, &rect
);
3791 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3792 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3793 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3794 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3799 CAMLprim value
ml_setaalevel (value level_v
)
3801 CAMLparam1 (level_v
);
3803 state
.aalevel
= Int_val (level_v
);
3804 CAMLreturn (Val_unit
);
3808 #include <X11/Xlib.h>
3814 GLXDrawable drawable
;
3817 #include "keysym2ucs.c"
3819 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3821 CAMLparam1 (keysym_v
);
3823 KeySym keysym
= Int_val (keysym_v
);
3828 rune
= keysym2ucs (keysym
);
3829 len
= fz_runetochar (buf
, rune
);
3831 str_v
= caml_copy_string (buf
);
3835 CAMLprim value
ml_glx (value win_v
)
3838 XVisualInfo
*visual
;
3839 int screen
, wid
= Int_val (win_v
);
3840 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3842 glx
.dpy
= XOpenDisplay (NULL
);
3844 caml_failwith ("XOpenDisplay");
3847 screen
= DefaultScreen (glx
.dpy
);
3848 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3850 XCloseDisplay (glx
.dpy
);
3852 caml_failwith ("glXChooseVisual");
3855 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3858 XCloseDisplay (glx
.dpy
);
3860 caml_failwith ("glXCreateContext");
3863 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3864 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3865 XCloseDisplay (glx
.dpy
);
3868 caml_failwith ("glXMakeCurrent");
3871 CAMLreturn (Val_unit
);
3874 CAMLprim value
ml_swapb (value unit_v
)
3876 CAMLparam1 (unit_v
);
3877 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3878 CAMLreturn (Val_unit
);
3881 CAMLprim value
ml_glxsync (value unit_v
)
3883 CAMLparam1 (unit_v
);
3884 if (glx
.dpy
&& glx
.ctx
) {
3888 CAMLreturn (Val_unit
);
3891 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3892 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3894 CAMLprim value
ml_platform (value unit_v
)
3896 CAMLparam1 (unit_v
);
3897 int platid
= piunknown
;
3899 #if defined __linux__
3901 #elif defined __CYGWIN__
3903 #elif defined __DragonFly__
3904 platid
= pidragonflybsd
;
3905 #elif defined __FreeBSD__
3907 #elif defined __OpenBSD__
3909 #elif defined __NetBSD__
3911 #elif defined __sun__
3913 #elif defined __APPLE__
3916 CAMLreturn (Val_int (platid
));
3919 CAMLprim value
ml_cloexec (value fd_v
)
3922 int fd
= Int_val (fd_v
);
3924 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3925 uerror ("fcntl", Nothing
);
3927 CAMLreturn (Val_unit
);
3930 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3932 CAMLparam2 (w_v
, h_v
);
3935 int w
= Int_val (w_v
);
3936 int h
= Int_val (h_v
);
3937 int cs
= Int_val (cs_v
);
3939 if (state
.pbo_usable
) {
3940 pbo
= calloc (sizeof (*pbo
), 1);
3942 err (1, "calloc pbo");
3954 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3957 state
.glGenBuffersARB (1, &pbo
->id
);
3958 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3959 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3960 NULL
, GL_STREAM_DRAW
);
3961 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3963 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3965 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3966 state
.glDeleteBuffersARB (1, &pbo
->id
);
3968 ret_v
= caml_copy_string ("0");
3974 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3976 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3980 err (1, "malloc %d bytes failed", res
+1);
3982 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3984 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3986 ret_v
= caml_copy_string (s
);
3991 ret_v
= caml_copy_string ("0");
3996 CAMLprim value
ml_freepbo (value s_v
)
3999 char *s
= String_val (s_v
);
4000 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4003 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4005 tile
->pbo
->ptr
= NULL
;
4006 tile
->pbo
->size
= -1;
4008 CAMLreturn (Val_unit
);
4011 CAMLprim value
ml_unmappbo (value s_v
)
4014 char *s
= String_val (s_v
);
4015 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4018 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4019 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4020 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4022 tile
->pbo
->ptr
= NULL
;
4023 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4025 CAMLreturn (Val_unit
);
4028 static void setuppbo (void)
4030 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4031 state
.pbo_usable
= GGPA (glBindBufferARB
)
4032 && GGPA (glUnmapBufferARB
)
4033 && GGPA (glMapBufferARB
)
4034 && GGPA (glBufferDataARB
)
4035 && GGPA (glGenBuffersARB
)
4036 && GGPA (glDeleteBuffersARB
);
4040 CAMLprim value
ml_pbo_usable (value unit_v
)
4042 CAMLparam1 (unit_v
);
4043 CAMLreturn (Val_bool (state
.pbo_usable
));
4046 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4048 CAMLparam3 (ptr_v
, x_v
, y_v
);
4049 CAMLlocal2 (ret_v
, tup_v
);
4051 char *s
= String_val (ptr_v
);
4052 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4053 struct pagedim
*pdim
;
4057 page
= parse_pointer ("ml_unproject", s
);
4058 pdim
= &state
.pagedims
[page
->pdimno
];
4060 ret_v
= Val_int (0);
4061 if (trylock ("ml_unproject")) {
4065 switch (page
->type
) {
4067 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4073 p
.x
= x
+ pdim
->bounds
.x0
;
4074 p
.y
= y
+ pdim
->bounds
.y0
;
4076 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4077 fz_invert_matrix (&ctm
, &ctm
);
4078 fz_transform_point (&p
, &ctm
);
4080 tup_v
= caml_alloc_tuple (2);
4081 ret_v
= caml_alloc_small (1, 1);
4082 Field (tup_v
, 0) = Val_int (p
.x
);
4083 Field (tup_v
, 1) = Val_int (p
.y
);
4084 Field (ret_v
, 0) = tup_v
;
4086 unlock ("ml_unproject");
4091 static void makestippletex (void)
4093 const char pixels
[] = "\xff\xff\0\0";
4094 glGenTextures (1, &state
.stid
);
4095 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4096 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4097 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4110 CAMLprim value
ml_fz_version (value unit_v
)
4112 return caml_copy_string (FZ_VERSION
);
4115 #ifdef USE_FONTCONFIG
4121 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4125 int needs_exact_metrics
)
4132 FcPattern
*pat
, *pat1
;
4134 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4135 name
, bold
, italic
, needs_exact_metrics
);
4138 fc
.config
= FcInitLoadConfigAndFonts ();
4140 lprintf ("FcInitLoadConfigAndFonts failed\n");
4144 if (!fc
.config
) return NULL
;
4146 size
= strlen (name
);
4147 if (bold
) size
+= sizeof (":bold") - 1;
4148 if (italic
) size
+= sizeof (":italic") - 1;
4151 buf
= malloc (size
);
4153 err (1, "malloc %zu failed", size
);
4157 if (bold
&& italic
) {
4158 strcat (buf
, ":bold:italic");
4161 if (bold
) strcat (buf
, ":bold");
4162 if (italic
) strcat (buf
, ":italic");
4164 for (i
= 0; i
< size
; ++i
) {
4165 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4168 lprintf ("fcbuf=%s\n", buf
);
4169 pat
= FcNameParse ((FcChar8
*) buf
);
4171 printd ("emsg FcNameParse failed\n");
4176 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4177 printd ("emsg FcConfigSubstitute failed\n");
4181 FcDefaultSubstitute (pat
);
4183 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4185 printd ("emsg FcFontMatch failed\n");
4186 FcPatternDestroy (pat
);
4191 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4192 printd ("emsg FcPatternGetString failed\n");
4193 FcPatternDestroy (pat
);
4194 FcPatternDestroy (pat1
);
4200 printd ("emsg name=%s path=%s\n", name
, path
);
4202 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4203 FcPatternDestroy (pat
);
4204 FcPatternDestroy (pat1
);
4210 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4212 CAMLparam2 (pipe_v
, params_v
);
4213 CAMLlocal2 (trim_v
, fuzz_v
);
4220 struct sigaction sa
;
4222 state
.cr
= Int_val (Field (pipe_v
, 0));
4223 state
.cw
= Int_val (Field (pipe_v
, 1));
4224 state
.rotate
= Int_val (Field (params_v
, 0));
4225 state
.fitmodel
= Int_val (Field (params_v
, 1));
4226 trim_v
= Field (params_v
, 2);
4227 texcount
= Int_val (Field (params_v
, 3));
4228 state
.sliceheight
= Int_val (Field (params_v
, 4));
4229 mustoresize
= Int_val (Field (params_v
, 5));
4230 colorspace
= Int_val (Field (params_v
, 6));
4231 fontpath
= String_val (Field (params_v
, 7));
4233 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4234 if (!state
.trimcachepath
) {
4235 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4238 haspboext
= Bool_val (Field (params_v
, 9));
4240 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4242 #ifdef USE_FONTCONFIG
4243 fz_install_load_system_font_funcs (state
.ctx
, fc_load_system_font_func
, NULL
);
4246 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4247 fuzz_v
= Field (trim_v
, 1);
4248 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4249 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4250 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4251 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4253 set_tex_params (colorspace
);
4256 #ifndef USE_FONTCONFIG
4257 state
.face
= load_font (fontpath
);
4261 char *buf
= fontpath
;
4262 FcPattern
*pat
, *pat1
;
4265 fc
.config
= FcInitLoadConfigAndFonts ();
4267 errx (1, "FcInitLoadConfigAndFonts failed");
4270 pat
= FcNameParse ((FcChar8
*) buf
);
4272 errx (1, "FcNameParse failed");
4275 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4276 errx (1, "FcConfigSubstitute failed");
4278 FcDefaultSubstitute (pat
);
4280 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4282 errx (1, "FcFontMatch failed");
4285 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4286 errx (1, "FcPatternGetString failed");
4289 state
.face
= load_font ((char *) path
);
4290 FcPatternDestroy (pat
);
4291 FcPatternDestroy (pat1
);
4296 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4298 state
.face
= load_builtin_font (base
, len
);
4300 if (!state
.face
) _exit (1);
4302 realloctexts (texcount
);
4311 sa
.sa_handler
= SIG_IGN
;
4312 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4314 sa
.sa_handler
= SIG_DFL
;
4315 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4317 if (sigemptyset (&sa
.sa_mask
)) {
4318 err (1, "sigemptyset");
4320 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4321 err (1, "sigaction");
4324 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4326 errx (1, "pthread_create: %s", strerror (ret
));
4329 CAMLreturn (Val_unit
);