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
44 extern char **environ
;
47 #define MIN(a,b) ((a) < (b) ? (a) : (b))
48 #define MAX(a,b) ((a) > (b) ? (a) : (b))
51 #define NORETURN_ATTR __attribute__ ((noreturn))
52 #define UNUSED_ATTR __attribute__ ((unused))
53 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
54 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
58 #define OPTIMIZE_ATTR(n)
59 #define GCC_FMT_ATTR(a, b)
65 #define FMT_ptr_cast(p) (p)
66 #define FMT_ptr_cast2(p) (p)
68 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
69 err (int exitcode
, const char *fmt
, ...)
76 vfprintf (stderr
, fmt
, ap
);
78 fprintf (stderr
, ": %s\n", strerror (savederrno
));
83 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
84 errx (int exitcode
, const char *fmt
, ...)
89 vfprintf (stderr
, fmt
, ap
);
96 #ifndef GL_TEXTURE_RECTANGLE_ARB
97 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
101 #define GL_BGRA 0x80E1
104 #ifndef GL_UNSIGNED_INT_8_8_8_8
105 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
108 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
109 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
113 #define lprintf printf
118 #define ARSERT(cond) for (;;) { \
120 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
136 struct slice slices
[1];
147 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
155 enum { DPDF
, DXPS
, DCBZ
, DIMG
};
164 fz_text_sheet
*sheet
;
172 fz_display_list
*dlist
;
174 struct slink
*slinks
;
179 void (*freepage
) (void *);
185 struct pagedim
*pagedims
;
205 fz_colorspace
*colorspace
;
213 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
227 void (*closedoc
) (void);
228 void (*freepage
) (void *);
236 void (*glBindBufferARB
) (GLenum
, GLuint
);
237 GLboolean (*glUnmapBufferARB
) (GLenum
);
238 void *(*glMapBufferARB
) (GLenum
, GLenum
);
239 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
240 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
241 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
243 GLfloat texcoords
[8];
244 GLfloat vertices
[16];
253 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
255 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
258 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
260 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
263 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
265 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
266 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
269 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
271 static void lock (const char *cap
)
273 int ret
= pthread_mutex_lock (&mutex
);
275 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
279 static void unlock (const char *cap
)
281 int ret
= pthread_mutex_unlock (&mutex
);
283 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
287 static int trylock (const char *cap
)
289 int ret
= pthread_mutex_trylock (&mutex
);
290 if (ret
&& ret
!= EBUSY
) {
291 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
296 static void *parse_pointer (const char *cap
, const char *s
)
301 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
303 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
308 static double now (void)
312 if (gettimeofday (&tv
, NULL
)) {
313 err (1, "gettimeofday");
315 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
318 static int hasdata (void)
321 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
322 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
326 CAMLprim value
ml_hasdata (value fd_v
)
331 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
332 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
333 CAMLreturn (Val_bool (avail
> 0));
336 static void readdata (void *p
, int size
)
341 n
= read (state
.cr
, p
, size
);
343 if (errno
== EINTR
) goto again
;
344 err (1, "read (req %d, ret %zd)", size
, n
);
347 if (!n
) errx (1, "EOF while reading");
348 errx (1, "read (req %d, ret %zd)", size
, n
);
352 static void writedata (char *p
, int size
)
356 p
[0] = (size
>> 24) & 0xff;
357 p
[1] = (size
>> 16) & 0xff;
358 p
[2] = (size
>> 8) & 0xff;
359 p
[3] = (size
>> 0) & 0xff;
361 n
= write (state
.cw
, p
, size
+ 4);
363 if (!n
) errx (1, "EOF while writing data");
364 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
368 static int readlen (void)
373 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
376 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
384 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
387 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
391 if (len
< size
- 4) {
392 writedata (buf
, len
);
398 err (1, "vsnprintf for `%s' failed", fmt
);
400 buf
= realloc (buf
, size
);
405 static void closepdf (void)
408 pdf_close_document (state
.u
.pdf
);
413 static void closexps (void)
416 xps_close_document (state
.u
.xps
);
421 static void closecbz (void)
424 cbz_close_document (state
.u
.cbz
);
429 static void closeimg (void)
432 image_close_document (state
.u
.img
);
437 static void freepdfpage (void *ptr
)
439 pdf_free_page (state
.u
.pdf
, ptr
);
442 static void freeemptyxxxpage (void *ptr
)
447 static void freexpspage (void *ptr
)
449 xps_free_page (state
.u
.xps
, ptr
);
452 static void freecbzpage (void *ptr
)
454 cbz_free_page (state
.u
.cbz
, ptr
);
457 static void freeimgpage (void *ptr
)
459 image_free_page (state
.u
.img
, ptr
);
462 static void openxref (char *filename
, char *password
)
466 for (i
= 0; i
< state
.texcount
; ++i
) {
467 state
.texowners
[i
].w
= -1;
468 state
.texowners
[i
].slice
= NULL
;
471 if (state
.closedoc
) state
.closedoc ();
473 len
= strlen (filename
);
489 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
490 int len2
= strlen (tbl
[i
].ext
);
492 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
-len2
)) {
493 state
.type
= tbl
[i
].type
;
499 if (state
.pagedims
) {
500 free (state
.pagedims
);
501 state
.pagedims
= NULL
;
503 state
.pagedimcount
= 0;
505 fz_set_aa_level (state
.ctx
, state
.aalevel
);
506 switch (state
.type
) {
508 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
509 if (pdf_needs_password (state
.u
.pdf
)) {
510 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
512 errx (1, "invalid password");
515 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
516 state
.closedoc
= closepdf
;
517 state
.freepage
= freepdfpage
;
521 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
522 state
.pagecount
= xps_count_pages (state
.u
.xps
);
523 state
.closedoc
= closexps
;
524 state
.freepage
= freexpspage
;
528 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
529 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
530 state
.closedoc
= closecbz
;
531 state
.freepage
= freecbzpage
;
535 state
.u
.img
= image_open_document (state
.ctx
, filename
);
537 state
.closedoc
= closeimg
;
538 state
.freepage
= freeimgpage
;
543 static void pdfinfo (void)
545 if (state
.type
== DPDF
) {
548 printd ("info PDF version\t%d.%d",
549 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
551 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
555 char *items
[] = { "Title", "Author", "Creator",
556 "Producer", "CreationDate" };
558 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
559 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
560 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
563 printd ("title %s", s
);
565 printd ("info %s\t%s", items
[i
], s
);
567 fz_free (state
.ctx
, s
);
574 static void unlinktile (struct tile
*tile
)
578 for (i
= 0; i
< tile
->slicecount
; ++i
) {
579 struct slice
*s
= &tile
->slices
[i
];
581 if (s
->texindex
!= -1) {
582 if (state
.texowners
[s
->texindex
].slice
== s
) {
583 state
.texowners
[s
->texindex
].slice
= NULL
;
589 static void freepage (struct page
*page
)
592 fz_free_text_page (state
.ctx
, page
->text
);
595 fz_free_text_sheet (state
.ctx
, page
->sheet
);
600 fz_drop_display_list (state
.ctx
, page
->dlist
);
601 page
->freepage (page
->u
.ptr
);
605 static void freetile (struct tile
*tile
)
610 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
613 fz_drop_pixmap (state
.ctx
, state
.pig
);
615 state
.pig
= tile
->pixmap
;
620 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
629 static int cacheline32bytes
;
631 static void __attribute__ ((constructor
)) clcheck (void)
633 char **envp
= environ
;
638 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
640 cacheline32bytes
= auxv
[1] == 32;
646 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
648 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
649 if (cacheline32bytes
&& size
> 32) {
650 intptr_t a1
, a2
, diff
;
652 vector
unsigned char v
= vec_splat_u8 (-1);
653 vector
unsigned char *p
;
655 a1
= a2
= (intptr_t) pixmap
->samples
;
656 a2
= (a1
+ 31) & ~31;
661 while (a1
!= a2
) *(char *) a1
++ = 0xff;
662 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
663 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
665 vec_st (v
, i
+ 16, p
);
667 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
669 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
672 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
675 static void trimctm (pdf_page
*page
, int pindex
)
678 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
680 if (!pdim
->tctmready
) {
681 if (state
.trimmargins
) {
683 fz_matrix rm
, sm
, tm
, im
, ctm1
;
685 fz_rotate (&rm
, -pdim
->rotate
);
686 fz_scale (&sm
, 1, -1);
687 fz_concat (&ctm
, &rm
, &sm
);
688 realbox
= pdim
->mediabox
;
689 fz_transform_rect (&realbox
, &ctm
);
690 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
691 fz_concat (&ctm1
, &ctm
, &tm
);
692 fz_invert_matrix (&im
, &page
->ctm
);
693 fz_concat (&ctm
, &im
, &ctm1
);
703 static fz_matrix
pagectm (struct page
*page
)
707 if (page
->type
== DPDF
) {
708 trimctm (page
->u
.pdfpage
, page
->pdimno
);
710 &state
.pagedims
[page
->pdimno
].tctm
,
711 &state
.pagedims
[page
->pdimno
].ctm
);
714 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
716 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
717 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
722 static void *loadpage (int pageno
, int pindex
)
727 page
= calloc (sizeof (struct page
), 1);
729 err (1, "calloc page %d", pageno
);
732 page
->dlist
= fz_new_display_list (state
.ctx
);
733 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
735 switch (state
.type
) {
737 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
738 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
740 page
->freepage
= freepdfpage
;
744 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
745 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
747 page
->freepage
= freexpspage
;
751 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
752 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
754 page
->freepage
= freecbzpage
;
758 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
759 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
761 page
->freepage
= freeimgpage
;
765 fz_catch (state
.ctx
) {
767 page
->freepage
= freeemptyxxxpage
;
769 fz_free_device (dev
);
771 page
->pdimno
= pindex
;
772 page
->pageno
= pageno
;
773 page
->sgen
= state
.gen
;
774 page
->tgen
= state
.gen
;
775 page
->type
= state
.type
;
780 static struct tile
*alloctile (int h
)
787 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
788 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
789 tile
= calloc (tilesize
, 1);
791 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
793 for (i
= 0; i
< slicecount
; ++i
) {
794 int sh
= MIN (h
, state
.sliceheight
);
795 tile
->slices
[i
].h
= sh
;
796 tile
->slices
[i
].texindex
= -1;
799 tile
->slicecount
= slicecount
;
800 tile
->sliceheight
= state
.sliceheight
;
810 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
811 const fz_matrix
*ctm
, float alpha
)
813 struct obs
*obs
= dev
->user
;
815 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
817 fz_rect rect
= fz_unit_rect
;
819 fz_transform_rect (&rect
, ctm
);
820 fz_round_rect (&b
, &rect
);
821 fz_intersect_irect (&b
, &obs
->b
);
822 obs
->cured
= b
.x0
== obs
->b
.x0
825 && b
.y1
== obs
->b
.y1
;
829 static int obscured (struct page
*page
, fz_irect bbox
)
836 memset (&dev
, 0, sizeof (dev
));
837 memset (&obs
, 0, sizeof (obs
));
842 dev
.fill_image
= obs_fill_image
;
844 fz_rect_from_irect (&rect
, &bbox
);
845 ctm
= pagectm (page
);
846 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
849 #define OBSCURED obscured
851 #define OBSCURED(a, b) 0
854 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
862 struct pagedim
*pdim
;
864 tile
= alloctile (h
);
865 pdim
= &state
.pagedims
[page
->pdimno
];
870 bbox
.x1
= bbox
.x0
+ w
;
871 bbox
.y1
= bbox
.y0
+ h
;
874 if (state
.pig
->w
== w
876 && state
.pig
->colorspace
== state
.colorspace
) {
877 tile
->pixmap
= state
.pig
;
878 tile
->pixmap
->x
= bbox
.x0
;
879 tile
->pixmap
->y
= bbox
.y0
;
882 fz_drop_pixmap (state
.ctx
, state
.pig
);
889 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
895 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
901 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
902 clearpixmap (tile
->pixmap
);
904 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
905 ctm
= pagectm (page
);
906 fz_rect_from_irect (&rect
, &bbox
);
907 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
908 fz_free_device (dev
);
913 static void initpdims (void)
917 fz_rect rootmediabox
;
918 int pageno
, trim
, show
;
919 int trimw
= 0, cxcount
;
923 if (state
.trimmargins
&& state
.trimcachepath
) {
924 trimf
= fopen (state
.trimcachepath
, "rb");
926 trimf
= fopen (state
.trimcachepath
, "wb");
931 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
932 cxcount
= state
.pagecount
;
934 cxcount
= MIN (state
.pagecount
, 1);
936 if (state
.type
== DPDF
) {
938 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
939 "Root/Pages/MediaBox");
940 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
943 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
948 switch (state
.type
) {
950 pdf_obj
*pageref
= pdf_lookup_page_obj (state
.u
.pdf
, pageno
);
951 pdf_obj
*pageobj
= pdf_resolve_indirect (pageref
);
953 if (state
.trimmargins
) {
958 page
= pdf_load_page (state
.u
.pdf
, pageno
);
959 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
960 trim
= state
.trimanew
|| !obj
;
966 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
967 dev
->hints
|= FZ_IGNORE_SHADE
;
968 fz_invert_matrix (&ctm
, &page
->ctm
);
969 pdf_run_page (state
.u
.pdf
, page
, dev
,
971 fz_free_device (dev
);
973 rect
.x0
+= state
.trimfuzz
.x0
;
974 rect
.x1
+= state
.trimfuzz
.x1
;
975 rect
.y0
+= state
.trimfuzz
.y0
;
976 rect
.y1
+= state
.trimfuzz
.y1
;
977 fz_transform_rect (&rect
, &ctm
);
978 fz_intersect_rect (&rect
, &page
->mediabox
);
980 if (fz_is_empty_rect (&rect
)) {
981 mediabox
= page
->mediabox
;
987 obj
= pdf_new_array (state
.u
.pdf
, 4);
988 pdf_array_push (obj
, pdf_new_real (state
.u
.pdf
,
990 pdf_array_push (obj
, pdf_new_real (state
.u
.pdf
,
992 pdf_array_push (obj
, pdf_new_real (state
.u
.pdf
,
994 pdf_array_push (obj
, pdf_new_real (state
.u
.pdf
,
996 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
999 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1000 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1001 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1002 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1005 rotate
= page
->rotate
;
1006 pdf_free_page (state
.u
.pdf
, page
);
1008 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1010 printd ("progress %f Trimming %d",
1011 (double) (pageno
+ 1) / state
.pagecount
,
1015 fz_catch (state
.ctx
) {
1016 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1023 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1025 if (fz_is_empty_rect (&mediabox
)) {
1033 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1035 if (!fz_is_empty_rect (&cropbox
)) {
1036 fz_intersect_rect (&mediabox
, &cropbox
);
1040 if (fz_is_empty_rect (&rootmediabox
)) {
1042 "cannot find page size for page %d\n",
1046 mediabox
= rootmediabox
;
1050 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1059 fz_try (state
.ctx
) {
1060 page
= xps_load_page (state
.u
.xps
, pageno
);
1061 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1063 if (state
.trimmargins
) {
1067 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1068 dev
->hints
|= FZ_IGNORE_SHADE
;
1069 xps_run_page (state
.u
.xps
, page
, dev
,
1070 &fz_identity
, NULL
);
1071 fz_free_device (dev
);
1073 rect
.x0
+= state
.trimfuzz
.x0
;
1074 rect
.x1
+= state
.trimfuzz
.x1
;
1075 rect
.y0
+= state
.trimfuzz
.y0
;
1076 rect
.y1
+= state
.trimfuzz
.y1
;
1077 fz_intersect_rect (&rect
, &mediabox
);
1079 if (!fz_is_empty_rect (&rect
)) {
1083 xps_free_page (state
.u
.xps
, page
);
1085 printd ("progress %f loading %d",
1086 (double) (pageno
+ 1) / state
.pagecount
,
1090 fz_catch (state
.ctx
) {
1102 if (state
.trimmargins
&& trimw
) {
1105 fz_try (state
.ctx
) {
1106 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1107 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1108 cbz_free_page (state
.u
.cbz
, page
);
1109 printd ("progress %f Trimming %d",
1110 (double) (pageno
+ 1) / state
.pagecount
,
1113 fz_catch (state
.ctx
) {
1114 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1117 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1119 err (1, "fwrite trim mediabox");
1125 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1127 err (1, "fread trim mediabox");
1133 fz_try (state
.ctx
) {
1134 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1135 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1136 cbz_free_page (state
.u
.cbz
, page
);
1138 fz_catch (state
.ctx
) {
1139 fprintf (stderr
, "failed to load cbz page\n");
1157 fz_try (state
.ctx
) {
1158 page
= image_load_page (state
.u
.img
, pageno
);
1159 image_bound_page (state
.u
.img
, page
, &mediabox
);
1160 image_free_page (state
.u
.img
, page
);
1162 fz_catch (state
.ctx
) {
1163 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1169 ARSERT (0 && state
.type
);
1172 if (state
.pagedimcount
== 0
1173 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1174 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1177 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1178 state
.pagedims
= realloc (state
.pagedims
, size
);
1179 if (!state
.pagedims
) {
1180 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1181 size
, state
.pagedimcount
+ 1);
1184 p
= &state
.pagedims
[state
.pagedimcount
++];
1186 p
->mediabox
= mediabox
;
1191 if (state
.trimmargins
) {
1192 printd ("progress 1 Trimmed %d pages in %f seconds",
1193 state
.pagecount
, end
- start
);
1196 printd ("vmsg Processed %d pages in %f seconds",
1197 state
.pagecount
, end
- start
);
1201 if (fclose (trimf
)) {
1207 static void layout (void)
1212 struct pagedim
*p
= p
;
1213 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1215 if (state
.pagedimcount
== 0) return;
1217 switch (state
.fitmodel
) {
1218 case FitProportional
:
1219 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1222 p
= &state
.pagedims
[pindex
];
1223 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1225 fz_transform_rect (&box
, &rm
);
1227 x0
= MIN (box
.x0
, box
.x1
);
1228 x1
= MAX (box
.x0
, box
.x1
);
1231 maxw
= MAX (w
, maxw
);
1232 zoom
= state
.w
/ maxw
;
1244 ARSERT (0 && state
.fitmodel
);
1247 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1251 p
= &state
.pagedims
[pindex
];
1252 fz_rotate (&ctm
, state
.rotate
);
1253 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1255 fz_transform_rect (&box
, &rm
);
1256 w
= box
.x1
- box
.x0
;
1257 switch (state
.fitmodel
) {
1258 case FitProportional
:
1259 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1265 h
= box
.y1
- box
.y0
;
1267 zoom
= MIN (zw
, zh
);
1268 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1277 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1278 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1280 fz_rotate (&rm
, p
->rotate
);
1281 p
->pagebox
= p
->mediabox
;
1282 fz_transform_rect (&p
->pagebox
, &rm
);
1283 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1284 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1288 fz_transform_rect (&rect
, &ctm
);
1289 fz_round_rect (&p
->bounds
, &rect
);
1292 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1293 fz_scale (&sm
, zoom
, -zoom
);
1294 fz_concat (&ctm
, &tm
, &sm
);
1295 fz_concat (&p
->lctm
, &ctm
, &rm
);
1301 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1302 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1303 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1304 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1308 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1309 } while (p
-- != state
.pagedims
);
1313 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1314 desttoanchor (fz_link_dest
*dest
)
1318 struct pagedim
*pdim
= state
.pagedims
;
1323 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1324 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1326 pdim
= &state
.pagedims
[i
];
1328 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1330 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1331 p
.x
= dest
->ld
.gotor
.lt
.x
;
1334 p
.y
= dest
->ld
.gotor
.lt
.y
;
1335 fz_transform_point (&p
, &pdim
->lctm
);
1339 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1340 double x0
, x1
, y0
, y1
;
1342 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1343 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1345 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1346 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1348 a
.n
= dest
->ld
.gotor
.page
;
1353 static void recurse_outline (fz_outline
*outline
, int level
)
1356 if (outline
->dest
.kind
== FZ_LINK_GOTO
) {
1357 struct anchor a
= desttoanchor (&outline
->dest
);
1360 printd ("o %d %d %d %d %s",
1361 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1365 printd ("emsg Unhandled outline kind %d for %s\n",
1366 outline
->dest
.kind
, outline
->title
);
1368 if (outline
->down
) {
1369 recurse_outline (outline
->down
, level
+ 1);
1371 outline
= outline
->next
;
1375 static void process_outline (void)
1377 fz_outline
*outline
;
1379 if (!state
.needoutline
) return;
1381 state
.needoutline
= 0;
1382 switch (state
.type
) {
1384 outline
= pdf_load_outline (state
.u
.pdf
);
1387 outline
= xps_load_outline (state
.u
.xps
);
1394 recurse_outline (outline
, 0);
1395 fz_free_outline (state
.ctx
, outline
);
1399 static char *strofspan (fz_text_span
*span
)
1404 size_t size
= 0, cap
= 80;
1406 p
= malloc (cap
+ 1);
1407 if (!p
) return NULL
;
1409 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1410 int n
= fz_runetochar (utf8
, ch
->c
);
1411 if (size
+ n
> cap
) {
1413 p
= realloc (p
, cap
+ 1);
1414 if (!p
) return NULL
;
1417 memcpy (p
+ size
, utf8
, n
);
1424 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1425 fz_text_span
*span
, fz_matrix ctm
,
1426 int stop
, int pageno
, double start
)
1433 fz_point p1
, p2
, p3
, p4
;
1435 p
= strofspan (span
);
1438 ret
= regexec (re
, p
, 1, &rm
, 0);
1441 if (ret
!= REG_NOMATCH
) {
1444 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1445 printd ("msg regexec error `%.*s'",
1446 (int) size
, errbuf
);
1454 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1455 c
+= fz_runelen (span
->text
[a
].c
);
1457 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1458 c
+= fz_runelen (span
->text
[b
].c
);
1461 if (fz_runelen (span
->text
[b
].c
) > 1) {
1465 fz_text_char_bbox (&sb
, span
, a
);
1466 fz_text_char_bbox (&eb
, span
, b
);
1478 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1485 printd ("progress 1 found at %d `%.*s' in %f sec",
1486 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1490 printd ("match %d %d %f %f %f %f %f %f %f %f",
1502 static int compareblocks (const void *l
, const void *r
)
1504 fz_text_block
const *ls
= l
;
1505 fz_text_block
const* rs
= r
;
1506 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1509 /* wishful thinking function */
1510 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1515 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1517 fz_text_sheet
*sheet
;
1518 struct pagedim
*pdim
, *pdimprev
;
1519 int stop
= 0, niters
= 0;
1522 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1526 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1527 if (niters
++ == 5) {
1530 printd ("progress 1 attention requested aborting search at %d",
1535 printd ("progress %f searching in page %d",
1536 (double) (pageno
+ 1) / state
.pagecount
,
1541 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1542 pdim
= &state
.pagedims
[i
];
1543 if (pdim
->pageno
== pageno
) {
1546 if (pdim
->pageno
> pageno
) {
1555 sheet
= fz_new_text_sheet (state
.ctx
);
1556 text
= fz_new_text_page (state
.ctx
);
1557 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1559 switch (state
.type
) {
1562 fz_try (state
.ctx
) {
1563 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1564 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1565 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1566 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1567 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1570 fz_catch (state
.ctx
) {
1571 fz_free_device (tdev
);
1578 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1579 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1583 ARSERT (0 && state
.type
);
1586 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1587 fz_free_device (tdev
);
1589 for (j
= 0; j
< text
->len
; ++j
) {
1592 fz_text_block
*block
;
1594 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1595 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1598 for (k
= 0; k
< block
->len
; ++k
) {
1603 line
= &block
->lines
[k
];
1604 if (line
->bbox
.y0
< y
+ 1) continue;
1607 line
= &block
->lines
[block
->len
- 1 - k
];
1608 if (line
->bbox
.y0
> y
- 1) continue;
1611 for (span
= line
->first_span
; span
; span
= span
->next
) {
1612 switch (matchspan (re
, text
, span
, ctm
,
1613 stop
, pageno
, start
)) {
1615 case 1: stop
= 1; break;
1616 case -1: stop
= 1; goto endloop
;
1631 fz_free_text_page (state
.ctx
, text
);
1632 fz_free_text_sheet (state
.ctx
, sheet
);
1634 state
.freepage (u
.ptr
);
1639 printd ("progress 1 no matches %f sec", end
- start
);
1641 printd ("clearrects");
1644 static void set_tex_params (int colorspace
)
1651 switch (colorspace
) {
1653 state
.texiform
= GL_RGBA8
;
1654 state
.texform
= GL_RGBA
;
1655 state
.texty
= GL_UNSIGNED_BYTE
;
1656 state
.colorspace
= fz_device_rgb (state
.ctx
);
1659 state
.texiform
= GL_RGBA8
;
1660 state
.texform
= GL_BGRA
;
1661 state
.texty
= endianness
.s
> 1
1662 ? GL_UNSIGNED_INT_8_8_8_8
1663 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1664 state
.colorspace
= fz_device_bgr (state
.ctx
);
1667 state
.texiform
= GL_LUMINANCE_ALPHA
;
1668 state
.texform
= GL_LUMINANCE_ALPHA
;
1669 state
.texty
= GL_UNSIGNED_BYTE
;
1670 state
.colorspace
= fz_device_gray (state
.ctx
);
1673 errx (1, "invalid colorspce %d", colorspace
);
1677 static void realloctexts (int texcount
)
1681 if (texcount
== state
.texcount
) return;
1683 if (texcount
< state
.texcount
) {
1684 glDeleteTextures (state
.texcount
- texcount
,
1685 state
.texids
+ texcount
);
1688 size
= texcount
* sizeof (*state
.texids
);
1689 state
.texids
= realloc (state
.texids
, size
);
1690 if (!state
.texids
) {
1691 err (1, "realloc texids %" FMT_s
, size
);
1694 size
= texcount
* sizeof (*state
.texowners
);
1695 state
.texowners
= realloc (state
.texowners
, size
);
1696 if (!state
.texowners
) {
1697 err (1, "realloc texowners %" FMT_s
, size
);
1699 if (texcount
> state
.texcount
) {
1702 glGenTextures (texcount
- state
.texcount
,
1703 state
.texids
+ state
.texcount
);
1704 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1705 state
.texowners
[i
].w
= -1;
1706 state
.texowners
[i
].slice
= NULL
;
1709 state
.texcount
= texcount
;
1713 static char *mbtoutf8 (char *s
)
1719 len
= mbstowcs (NULL
, s
, strlen (s
));
1724 if (len
== (size_t) -1) {
1729 tmp
= malloc (len
* sizeof (wchar_t));
1734 ret
= mbstowcs (tmp
, s
, len
);
1735 if (ret
== (size_t) -1) {
1741 for (i
= 0; i
< ret
; ++i
) {
1742 len
+= fz_runelen (tmp
[i
]);
1745 p
= r
= malloc (len
+ 1);
1751 for (i
= 0; i
< ret
; ++i
) {
1752 p
+= fz_runetochar (p
, tmp
[i
]);
1759 CAMLprim value
ml_mbtoutf8 (value s_v
)
1765 s
= String_val (s_v
);
1771 ret_v
= caml_copy_string (r
);
1777 static void * mainloop (void *unused
)
1780 int len
, ret
, oldlen
= 0;
1785 errx (1, "readlen returned 0");
1788 if (oldlen
< len
+ 1) {
1789 p
= realloc (p
, len
+ 1);
1791 err (1, "realloc %d failed", len
+ 1);
1798 if (!strncmp ("open", p
, 4)) {
1805 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1807 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1810 filename
= p
+ 5 + off
;
1811 filenamelen
= strlen (filename
);
1812 password
= filename
+ filenamelen
+ 1;
1815 openxref (filename
, password
);
1821 utf8filename
= mbtoutf8 (filename
);
1822 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1823 if (utf8filename
!= filename
) {
1824 free (utf8filename
);
1827 state
.needoutline
= 1;
1829 else if (!strncmp ("cs", p
, 2)) {
1832 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1834 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1837 set_tex_params (colorspace
);
1838 for (i
= 0; i
< state
.texcount
; ++i
) {
1839 state
.texowners
[i
].w
= -1;
1840 state
.texowners
[i
].slice
= NULL
;
1844 else if (!strncmp ("freepage", p
, 8)) {
1847 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1849 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1853 else if (!strncmp ("freetile", p
, 8)) {
1856 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1858 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1862 else if (!strncmp ("search", p
, 6)) {
1863 int icase
, pageno
, y
, ret
, len2
, forward
;
1867 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1868 &icase
, &pageno
, &y
, &forward
, &len2
);
1870 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1873 pattern
= p
+ 6 + len2
;
1874 ret
= regcomp (&re
, pattern
,
1875 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1880 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1881 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1884 search (&re
, pageno
, y
, forward
);
1888 else if (!strncmp ("geometry", p
, 8)) {
1892 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1894 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1902 for (i
= 0; i
< state
.texcount
; ++i
) {
1903 state
.texowners
[i
].slice
= NULL
;
1906 state
.fitmodel
= fitmodel
;
1911 unlock ("geometry");
1912 printd ("continue %d", state
.pagecount
);
1914 else if (!strncmp ("reqlayout", p
, 9)) {
1916 int rotate
, fitmodel
, off
, h
;
1919 ret
= sscanf (p
+ 9, " %d %d %d %n",
1920 &rotate
, &fitmodel
, &h
, &off
);
1922 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1925 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1928 state
.rotate
= rotate
;
1929 state
.fitmodel
= fitmodel
;
1934 nameddest
= p
+ 9 + off
;
1935 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1938 pdf_obj
*needle
, *obj
;
1940 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
1941 strlen (nameddest
));
1942 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1944 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
1946 a
= desttoanchor (&dest
);
1948 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1951 printd ("emsg failed to parse destination `%s'\n",
1956 printd ("emsg destination `%s' not found\n",
1959 pdf_drop_obj (needle
);
1963 unlock ("reqlayout");
1964 printd ("continue %d", state
.pagecount
);
1966 else if (!strncmp ("page", p
, 4)) {
1969 int pageno
, pindex
, ret
;
1971 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1973 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1978 page
= loadpage (pageno
, pindex
);
1982 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1984 else if (!strncmp ("tile", p
, 4)) {
1985 int x
, y
, w
, h
, ret
;
1991 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1992 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1994 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1999 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2003 printd ("tile %d %d %" FMT_ptr
" %u %f",
2005 FMT_ptr_cast2 (tile
),
2006 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2009 else if (!strncmp ("settrim", p
, 7)) {
2013 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2014 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2016 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2020 state
.trimmargins
= trimmargins
;
2021 state
.needoutline
= 1;
2022 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2024 state
.trimfuzz
= fuzz
;
2026 state
.pagedimcount
= 0;
2027 free (state
.pagedims
);
2028 state
.pagedims
= NULL
;
2033 printd ("continue %d", state
.pagecount
);
2035 else if (!strncmp ("sliceh", p
, 6)) {
2038 ret
= sscanf (p
+ 6, " %d", &h
);
2040 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2042 if (h
!= state
.sliceheight
) {
2045 state
.sliceheight
= h
;
2046 for (i
= 0; i
< state
.texcount
; ++i
) {
2047 state
.texowners
[i
].w
= -1;
2048 state
.texowners
[i
].h
= -1;
2049 state
.texowners
[i
].slice
= NULL
;
2053 else if (!strncmp ("interrupt", p
, 9)) {
2054 printd ("vmsg interrupted");
2057 errx (1, "unknown command %.*s", len
, p
);
2063 CAMLprim value
ml_realloctexts (value texcount_v
)
2065 CAMLparam1 (texcount_v
);
2068 if (trylock ("ml_realloctexts")) {
2072 realloctexts (Int_val (texcount_v
));
2074 unlock ("ml_realloctexts");
2077 CAMLreturn (Val_bool (ok
));
2080 static void recti (int x0
, int y0
, int x1
, int y1
)
2082 GLfloat
*v
= state
.vertices
;
2084 glVertexPointer (2, GL_FLOAT
, 0, v
);
2085 v
[0] = x0
; v
[1] = y0
;
2086 v
[2] = x1
; v
[3] = y0
;
2087 v
[4] = x0
; v
[5] = y1
;
2088 v
[6] = x1
; v
[7] = y1
;
2089 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2092 static void showsel (struct page
*page
, int ox
, int oy
)
2098 fz_page_block
*pageb
;
2099 fz_text_block
*block
;
2100 struct mark first
, last
;
2102 first
= page
->fmark
;
2105 if (!first
.span
|| !last
.span
) return;
2107 glEnable (GL_BLEND
);
2108 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2109 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
2111 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2112 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2113 for (pageb
= page
->text
->blocks
;
2114 pageb
< page
->text
->blocks
+ page
->text
->len
;
2116 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2117 block
= pageb
->u
.text
;
2119 for (line
= block
->lines
;
2120 line
< block
->lines
+ block
->len
;
2123 rect
= fz_empty_rect
;
2125 for (span
= line
->first_span
; span
; span
= span
->next
) {
2127 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2132 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2134 j
= MIN (first
.i
, last
.i
);
2135 k
= MAX (first
.i
, last
.i
);
2137 else if (span
== first
.span
) {
2141 else if (span
== last
.span
) {
2147 for (i
= j
; i
<= k
; ++i
) {
2149 fz_union_rect (&rect
,
2150 fz_text_char_bbox (&bbox
, span
, i
));
2152 fz_round_rect (&bbox
, &rect
);
2153 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2160 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2161 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2162 if (span
== last
.span
) {
2170 glDisable (GL_BLEND
);
2175 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2177 fz_matrix ctm
, tm
, pm
;
2178 fz_link
*link
, *links
;
2179 GLfloat
*texcoords
= state
.texcoords
;
2180 GLfloat
*vertices
= state
.vertices
;
2182 switch (page
->type
) {
2184 links
= page
->u
.pdfpage
->links
;
2188 links
= page
->u
.xpspage
->links
;
2195 glEnable (GL_TEXTURE_1D
);
2196 glEnable (GL_BLEND
);
2197 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2199 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2200 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2201 fz_translate (&tm
, xoff
, yoff
);
2202 pm
= pagectm (page
);
2203 fz_concat (&ctm
, &pm
, &tm
);
2205 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2206 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2208 for (link
= links
; link
; link
= link
->next
) {
2209 fz_point p1
, p2
, p3
, p4
;
2211 p1
.x
= link
->rect
.x0
;
2212 p1
.y
= link
->rect
.y0
;
2214 p2
.x
= link
->rect
.x1
;
2215 p2
.y
= link
->rect
.y0
;
2217 p3
.x
= link
->rect
.x1
;
2218 p3
.y
= link
->rect
.y1
;
2220 p4
.x
= link
->rect
.x0
;
2221 p4
.y
= link
->rect
.y1
;
2223 fz_transform_point (&p1
, &ctm
);
2224 fz_transform_point (&p2
, &ctm
);
2225 fz_transform_point (&p3
, &ctm
);
2226 fz_transform_point (&p4
, &ctm
);
2228 switch (link
->dest
.kind
) {
2229 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2230 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2231 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2232 default: glColor3ub (0, 0, 0); break;
2240 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2244 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2246 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2247 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2249 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2250 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2252 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2253 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2255 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2256 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2258 glDrawArrays (GL_LINES
, 0, 8);
2261 glDisable (GL_BLEND
);
2262 glDisable (GL_TEXTURE_1D
);
2265 static int compareslinks (const void *l
, const void *r
)
2267 struct slink
const *ls
= l
;
2268 struct slink
const *rs
= r
;
2269 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2270 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2272 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2275 static void droptext (struct page
*page
)
2278 fz_free_text_page (state
.ctx
, page
->text
);
2281 page
->fmark
.span
= NULL
;
2282 page
->lmark
.span
= NULL
;
2286 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2291 static void dropslinks (struct page
*page
)
2294 free (page
->slinks
);
2295 page
->slinks
= NULL
;
2296 page
->slinkcount
= 0;
2300 static void ensureslinks (struct page
*page
)
2304 size_t slinksize
= sizeof (*page
->slinks
);
2305 fz_link
*link
, *links
;
2307 if (state
.gen
!= page
->sgen
) {
2309 page
->sgen
= state
.gen
;
2311 if (page
->slinks
) return;
2313 switch (page
->type
) {
2315 links
= page
->u
.pdfpage
->links
;
2316 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2318 &state
.pagedims
[page
->pdimno
].tctm
,
2319 &state
.pagedims
[page
->pdimno
].ctm
);
2323 links
= page
->u
.xpspage
->links
;
2324 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2331 for (link
= links
; link
; link
= link
->next
) {
2335 page
->slinkcount
= count
;
2336 page
->slinks
= calloc (count
, slinksize
);
2337 if (!page
->slinks
) {
2338 err (1, "realloc slinks %d", count
);
2341 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2345 fz_transform_rect (&rect
, &ctm
);
2346 page
->slinks
[i
].link
= link
;
2347 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2349 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2353 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2354 static void fmt_linkn (char *s
, unsigned int u
)
2356 unsigned int len
; unsigned int q
;
2357 int zma
= 'z' - 'a' + 1;
2359 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2362 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2363 /* handles u == 0 */
2368 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2369 int noff
, char *targ
, int tlen
, int hfsize
)
2373 struct slink
*slink
;
2374 double x0
, y0
, x1
, y1
, w
;
2376 ensureslinks (page
);
2377 glColor3ub (0xc3, 0xb0, 0x91);
2378 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2379 fmt_linkn (buf
, i
+ noff
);
2380 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2381 slink
= &page
->slinks
[i
];
2383 x0
= slink
->bbox
.x0
+ xoff
- 5;
2384 y1
= slink
->bbox
.y0
+ yoff
- 5;
2385 y0
= y1
+ 10 + hfsize
;
2386 w
= measure_string (state
.face
, hfsize
, buf
);
2388 recti (x0
, y0
, x1
, y1
);
2392 glEnable (GL_BLEND
);
2393 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2394 glEnable (GL_TEXTURE_2D
);
2395 glColor3ub (0, 0, 0);
2396 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2397 fmt_linkn (buf
, i
+ noff
);
2398 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2399 slink
= &page
->slinks
[i
];
2401 x0
= slink
->bbox
.x0
+ xoff
;
2402 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2403 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2406 glDisable (GL_TEXTURE_2D
);
2407 glDisable (GL_BLEND
);
2411 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2414 struct slice
*slice1
;
2415 unsigned char *texdata
;
2418 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2419 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2421 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2422 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2423 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2427 int texindex
= state
.texindex
++ % state
.texcount
;
2429 if (state
.texowners
[texindex
].w
== tile
->w
) {
2430 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2434 state
.texowners
[texindex
].h
= slice
->h
;
2438 state
.texowners
[texindex
].h
= slice
->h
;
2441 state
.texowners
[texindex
].w
= tile
->w
;
2442 state
.texowners
[texindex
].slice
= slice
;
2443 slice
->texindex
= texindex
;
2445 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2447 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2451 texdata
= tile
->pixmap
->samples
;
2454 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2466 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2478 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2483 CAMLprim value
ml_begintiles (value unit_v
)
2485 CAMLparam1 (unit_v
);
2486 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2487 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2488 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2489 CAMLreturn (unit_v
);
2492 CAMLprim value
ml_endtiles (value unit_v
)
2494 CAMLparam1 (unit_v
);
2495 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2496 CAMLreturn (unit_v
);
2499 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2501 CAMLparam2 (args_v
, ptr_v
);
2502 int dispx
= Int_val (Field (args_v
, 0));
2503 int dispy
= Int_val (Field (args_v
, 1));
2504 int dispw
= Int_val (Field (args_v
, 2));
2505 int disph
= Int_val (Field (args_v
, 3));
2506 int tilex
= Int_val (Field (args_v
, 4));
2507 int tiley
= Int_val (Field (args_v
, 5));
2508 char *s
= String_val (ptr_v
);
2509 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2510 int slicey
, firstslice
;
2511 struct slice
*slice
;
2512 GLfloat
*texcoords
= state
.texcoords
;
2513 GLfloat
*vertices
= state
.vertices
;
2515 firstslice
= tiley
/ tile
->sliceheight
;
2516 slice
= &tile
->slices
[firstslice
];
2517 slicey
= tiley
% tile
->sliceheight
;
2522 dh
= slice
->h
- slicey
;
2523 dh
= MIN (disph
, dh
);
2524 uploadslice (tile
, slice
);
2526 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2527 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2528 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2529 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2531 vertices
[0] = dispx
; vertices
[1] = dispy
;
2532 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2533 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2534 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2536 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2540 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2543 CAMLreturn (Val_unit
);
2546 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2547 value xoff_v
, value yoff_v
,
2550 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2551 int xoff
= Int_val (xoff_v
);
2552 int yoff
= Int_val (yoff_v
);
2553 int noff
= Int_val (Field (li_v
, 0));
2554 char *targ
= String_val (Field (li_v
, 1));
2555 int tlen
= caml_string_length (Field (li_v
, 1));
2556 int hfsize
= Int_val (Field (li_v
, 2));
2557 char *s
= String_val (ptr_v
);
2558 int hlmask
= Int_val (hlinks_v
);
2559 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2562 /* deal with loadpage failed pages */
2566 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2567 if (trylock ("ml_postprocess")) {
2572 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2573 noff
= page
->slinkcount
;
2575 if (page
->tgen
== state
.gen
) {
2576 showsel (page
, xoff
, yoff
);
2578 unlock ("ml_postprocess");
2581 CAMLreturn (Val_int (noff
));
2584 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2588 const fz_matrix
*tctm
;
2589 fz_link
*link
, *links
;
2591 switch (page
->type
) {
2593 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2594 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2595 links
= page
->u
.pdfpage
->links
;
2599 tctm
= &fz_identity
;
2600 links
= page
->u
.xpspage
->links
;
2609 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2610 fz_invert_matrix (&ctm
, &ctm
);
2611 fz_transform_point (&p
, &ctm
);
2613 for (link
= links
; link
; link
= link
->next
) {
2614 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2615 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2623 static void ensuretext (struct page
*page
)
2625 if (state
.gen
!= page
->tgen
) {
2627 page
->tgen
= state
.gen
;
2633 page
->text
= fz_new_text_page (state
.ctx
);
2634 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2635 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2636 ctm
= pagectm (page
);
2637 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2638 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2639 qsort (page
->text
->blocks
, page
->text
->len
,
2640 sizeof (*page
->text
->blocks
), compareblocks
);
2642 fz_free_device (tdev
);
2646 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2648 CAMLparam2 (start_page_v
, dir_v
);
2650 int i
, dir
= Int_val (dir_v
);
2651 int start_page
= Int_val (start_page_v
);
2652 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2654 ret_v
= Val_int (0);
2655 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2659 lock ("ml_findpage_with_links");
2660 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2663 switch (state
.type
) {
2666 pdf_page
*page
= NULL
;
2668 fz_try (state
.ctx
) {
2669 page
= pdf_load_page (state
.u
.pdf
, i
);
2670 found
= !!page
->links
;
2672 fz_catch (state
.ctx
) {
2682 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2683 found
= !!page
->links
;
2689 ARSERT (0 && "invalid document type");
2693 ret_v
= caml_alloc_small (1, 1);
2694 Field (ret_v
, 0) = Val_int (i
);
2699 unlock ("ml_findpage_with_links");
2705 enum { dir_first
, dir_last
};
2706 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2708 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2710 CAMLparam2 (ptr_v
, dir_v
);
2711 CAMLlocal2 (ret_v
, pos_v
);
2713 int dirtag
, i
, slinkindex
;
2714 struct slink
*found
= NULL
,*slink
;
2715 char *s
= String_val (ptr_v
);
2717 page
= parse_pointer ("ml_findlink", s
);
2718 ret_v
= Val_int (0);
2719 if (trylock ("ml_findlink")) {
2723 ensureslinks (page
);
2725 if (Is_block (dir_v
)) {
2726 dirtag
= Tag_val (dir_v
);
2728 case dir_first_visible
:
2730 int x0
, y0
, dir
, first_index
, last_index
;
2732 pos_v
= Field (dir_v
, 0);
2733 x0
= Int_val (Field (pos_v
, 0));
2734 y0
= Int_val (Field (pos_v
, 1));
2735 dir
= Int_val (Field (pos_v
, 2));
2740 last_index
= page
->slinkcount
;
2743 first_index
= page
->slinkcount
- 1;
2747 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2748 slink
= &page
->slinks
[i
];
2749 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2758 slinkindex
= Int_val (Field (dir_v
, 0));
2759 found
= &page
->slinks
[slinkindex
];
2760 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2761 slink
= &page
->slinks
[i
];
2762 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2770 slinkindex
= Int_val (Field (dir_v
, 0));
2771 found
= &page
->slinks
[slinkindex
];
2772 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2773 slink
= &page
->slinks
[i
];
2774 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2782 slinkindex
= Int_val (Field (dir_v
, 0));
2783 found
= &page
->slinks
[slinkindex
];
2784 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2785 slink
= &page
->slinks
[i
];
2786 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2794 slinkindex
= Int_val (Field (dir_v
, 0));
2795 found
= &page
->slinks
[slinkindex
];
2796 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2797 slink
= &page
->slinks
[i
];
2798 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2807 dirtag
= Int_val (dir_v
);
2810 found
= page
->slinks
;
2815 found
= page
->slinks
+ (page
->slinkcount
- 1);
2821 ret_v
= caml_alloc_small (2, 1);
2822 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2825 unlock ("ml_findlink");
2830 enum { uuri
, ugoto
, utext
, uunexpected
,
2831 ulaunch
, unamed
, uremote
, uremotedest
};
2837 switch (link->dest.kind) { \
2838 case FZ_LINK_GOTO: \
2842 pageno = link->dest.ld.gotor.page; \
2846 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2847 p.y = link->dest.ld.gotor.lt.y; \
2848 fz_transform_point (&p, &pdim->lctm); \
2850 tup_v = caml_alloc_tuple (2); \
2851 ret_v = caml_alloc_small (1, ugoto); \
2852 Field (tup_v, 0) = Val_int (pageno); \
2853 Field (tup_v, 1) = Val_int (p.y); \
2854 Field (ret_v, 0) = tup_v; \
2859 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2860 ret_v = caml_alloc_small (1, uuri); \
2861 Field (ret_v, 0) = str_v; \
2864 case FZ_LINK_LAUNCH: \
2865 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2866 ret_v = caml_alloc_small (1, ulaunch); \
2867 Field (ret_v, 0) = str_v; \
2870 case FZ_LINK_NAMED: \
2871 str_v = caml_copy_string (link->dest.ld.named.named); \
2872 ret_v = caml_alloc_small (1, unamed); \
2873 Field (ret_v, 0) = str_v; \
2876 case FZ_LINK_GOTOR: \
2880 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2881 pageno = link->dest.ld.gotor.page; \
2882 if (pageno == -1) { \
2883 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2884 rty = uremotedest; \
2887 gr_v = Val_int (pageno); \
2890 tup_v = caml_alloc_tuple (2); \
2891 ret_v = caml_alloc_small (1, rty); \
2892 Field (tup_v, 0) = str_v; \
2893 Field (tup_v, 1) = gr_v; \
2894 Field (ret_v, 0) = tup_v; \
2902 snprintf (buf, sizeof (buf), \
2903 "unhandled link kind %d", link->dest.kind); \
2904 str_v = caml_copy_string (buf); \
2905 ret_v = caml_alloc_small (1, uunexpected); \
2906 Field (ret_v, 0) = str_v; \
2912 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2914 CAMLparam2 (ptr_v
, n_v
);
2915 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2918 struct pagedim
*pdim
;
2919 char *s
= String_val (ptr_v
);
2921 ret_v
= Val_int (0);
2922 if (trylock ("ml_getlink")) {
2926 page
= parse_pointer ("ml_getlink", s
);
2927 ensureslinks (page
);
2928 pdim
= &state
.pagedims
[page
->pdimno
];
2929 link
= page
->slinks
[Int_val (n_v
)].link
;
2932 unlock ("ml_getlink");
2937 CAMLprim value
ml_getlinkcount (value ptr_v
)
2941 char *s
= String_val (ptr_v
);
2943 page
= parse_pointer ("ml_getlinkcount", s
);
2944 CAMLreturn (Val_int (page
->slinkcount
));
2947 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2949 CAMLparam2 (ptr_v
, n_v
);
2952 struct slink
*slink
;
2953 char *s
= String_val (ptr_v
);
2955 page
= parse_pointer ("ml_getlinkrect", s
);
2956 ret_v
= caml_alloc_tuple (4);
2957 if (trylock ("ml_getlinkrect")) {
2958 Field (ret_v
, 0) = Val_int (0);
2959 Field (ret_v
, 1) = Val_int (0);
2960 Field (ret_v
, 2) = Val_int (0);
2961 Field (ret_v
, 3) = Val_int (0);
2964 ensureslinks (page
);
2966 slink
= &page
->slinks
[Int_val (n_v
)];
2967 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2968 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2969 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2970 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2971 unlock ("ml_getlinkrect");
2977 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2979 CAMLparam3 (ptr_v
, x_v
, y_v
);
2980 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2983 char *s
= String_val (ptr_v
);
2984 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2985 struct pagedim
*pdim
;
2987 ret_v
= Val_int (0);
2988 if (trylock ("ml_whatsunder")) {
2992 page
= parse_pointer ("ml_whatsunder", s
);
2993 pdim
= &state
.pagedims
[page
->pdimno
];
2994 x
+= pdim
->bounds
.x0
;
2995 y
+= pdim
->bounds
.y0
;
2996 link
= getlink (page
, x
, y
);
3002 fz_page_block
*pageb
;
3003 fz_text_block
*block
;
3006 for (pageb
= page
->text
->blocks
;
3007 pageb
< page
->text
->blocks
+ page
->text
->len
;
3010 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3011 block
= pageb
->u
.text
;
3014 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3017 for (line
= block
->lines
;
3018 line
< block
->lines
+ block
->len
;
3023 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3026 for (span
= line
->first_span
; span
; span
= span
->next
) {
3030 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3033 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3035 fz_text_char_bbox (&bbox
, span
, charnum
);
3038 if (x
>= b
->x0
&& x
<= b
->x1
3039 && y
>= b
->y0
&& y
<= b
->y1
) {
3040 fz_text_style
*style
= span
->text
->style
;
3042 style
->font
&& style
->font
->name
3044 : "Span has no font name"
3046 FT_FaceRec
*face
= style
->font
->ft_face
;
3047 if (face
&& face
->family_name
) {
3049 char *n1
= face
->family_name
;
3050 size_t l1
= strlen (n1
);
3051 size_t l2
= strlen (n2
);
3053 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3054 s
= malloc (l1
+ l2
+ 2);
3058 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3059 str_v
= caml_copy_string (s
);
3065 str_v
= caml_copy_string (n2
);
3067 ret_v
= caml_alloc_small (1, utext
);
3068 Field (ret_v
, 0) = str_v
;
3077 unlock ("ml_whatsunder");
3083 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3085 static int uninteresting (int c
)
3087 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3091 CAMLprim value
ml_clearmark (value ptr_v
)
3094 char *s
= String_val (ptr_v
);
3097 if (trylock ("ml_clearmark")) {
3101 page
= parse_pointer ("ml_clearmark", s
);
3102 page
->fmark
.span
= NULL
;
3103 page
->lmark
.span
= NULL
;
3107 unlock ("ml_clearmark");
3109 CAMLreturn (Val_unit
);
3112 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3114 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3119 fz_page_block
*pageb
;
3120 fz_text_block
*block
;
3121 struct pagedim
*pdim
;
3122 int mark
= Int_val (mark_v
);
3123 char *s
= String_val (ptr_v
);
3124 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3126 ret_v
= Val_bool (0);
3127 if (trylock ("ml_markunder")) {
3131 page
= parse_pointer ("ml_markunder", s
);
3132 pdim
= &state
.pagedims
[page
->pdimno
];
3136 if (mark
== mark_page
) {
3138 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3140 for (i
= 0; i
< page
->text
->len
; ++i
) {
3141 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3142 pb1
= &page
->text
->blocks
[i
];
3146 if (!pb1
) goto unlock
;
3148 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3149 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3150 pb2
= &page
->text
->blocks
[i
];
3154 if (!pb2
) goto unlock
;
3156 block
= pb1
->u
.text
;
3159 page
->fmark
.span
= block
->lines
->first_span
;
3161 block
= pb2
->u
.text
;
3162 line
= &block
->lines
[block
->len
- 1];
3163 page
->lmark
.i
= line
->last_span
->len
- 1;
3164 page
->lmark
.span
= line
->last_span
;
3165 ret_v
= Val_bool (1);
3169 x
+= pdim
->bounds
.x0
;
3170 y
+= pdim
->bounds
.y0
;
3172 for (pageb
= page
->text
->blocks
;
3173 pageb
< page
->text
->blocks
+ page
->text
->len
;
3175 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3176 block
= pageb
->u
.text
;
3179 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3182 if (mark
== mark_block
) {
3184 page
->fmark
.span
= block
->lines
->first_span
;
3186 line
= &block
->lines
[block
->len
- 1];
3187 page
->lmark
.i
= line
->last_span
->len
- 1;
3188 page
->lmark
.span
= line
->last_span
;
3189 ret_v
= Val_bool (1);
3193 for (line
= block
->lines
;
3194 line
< block
->lines
+ block
->len
;
3199 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3202 if (mark
== mark_line
) {
3204 page
->fmark
.span
= line
->first_span
;
3206 page
->lmark
.i
= line
->last_span
->len
- 1;
3207 page
->lmark
.span
= line
->last_span
;
3208 ret_v
= Val_bool (1);
3212 for (span
= line
->first_span
; span
; span
= span
->next
) {
3216 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3219 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3221 fz_text_char_bbox (&bbox
, span
, charnum
);
3224 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3226 int charnum2
, charnum3
= -1, charnum4
= -1;
3228 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3230 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3231 if (uninteresting (span
->text
[charnum2
].c
)) {
3232 charnum3
= charnum2
+ 1;
3236 if (charnum3
== -1) charnum3
= 0;
3238 for (charnum2
= charnum
+ 1;
3239 charnum2
< span
->len
;
3241 if (uninteresting (span
->text
[charnum2
].c
)) break;
3242 charnum4
= charnum2
;
3244 if (charnum4
== -1) goto unlock
;
3246 page
->fmark
.i
= charnum3
;
3247 page
->fmark
.span
= span
;
3249 page
->lmark
.i
= charnum4
;
3250 page
->lmark
.span
= span
;
3251 ret_v
= Val_bool (1);
3259 if (!Bool_val (ret_v
)) {
3260 page
->fmark
.span
= NULL
;
3261 page
->lmark
.span
= NULL
;
3265 unlock ("ml_markunder");
3271 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3273 CAMLparam3 (ptr_v
, x_v
, y_v
);
3274 CAMLlocal2 (ret_v
, res_v
);
3277 fz_page_block
*pageb
;
3278 struct pagedim
*pdim
;
3279 char *s
= String_val (ptr_v
);
3280 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3282 ret_v
= Val_int (0);
3283 if (trylock ("ml_rectofblock")) {
3287 page
= parse_pointer ("ml_rectofblock", s
);
3288 pdim
= &state
.pagedims
[page
->pdimno
];
3289 x
+= pdim
->bounds
.x0
;
3290 y
+= pdim
->bounds
.y0
;
3294 for (pageb
= page
->text
->blocks
;
3295 pageb
< page
->text
->blocks
+ page
->text
->len
;
3297 switch (pageb
->type
) {
3298 case FZ_PAGE_BLOCK_TEXT
:
3299 b
= &pageb
->u
.text
->bbox
;
3302 case FZ_PAGE_BLOCK_IMAGE
:
3303 b
= &pageb
->u
.image
->bbox
;
3310 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3315 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3316 ret_v
= caml_alloc_small (1, 1);
3317 Store_double_field (res_v
, 0, b
->x0
);
3318 Store_double_field (res_v
, 1, b
->x1
);
3319 Store_double_field (res_v
, 2, b
->y0
);
3320 Store_double_field (res_v
, 3, b
->y1
);
3321 Field (ret_v
, 0) = res_v
;
3323 unlock ("ml_rectofblock");
3329 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3331 CAMLparam2 (ptr_v
, rect_v
);
3334 struct pagedim
*pdim
;
3335 char *s
= String_val (ptr_v
);
3336 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3337 fz_page_block
*pageb
;
3338 fz_text_block
*block
;
3339 fz_text_span
*span
, *fspan
, *lspan
;
3340 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
3342 if (trylock ("ml_seltext")) {
3346 page
= parse_pointer ("ml_seltext", s
);
3349 pdim
= &state
.pagedims
[page
->pdimno
];
3350 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
3351 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3352 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3353 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3356 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3357 glColor3ub (128, 128, 128);
3358 recti (x0
, y0
, x1
, y1
);
3359 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3363 fspan
= page
->fmark
.span
;
3366 lspan
= page
->lmark
.span
;
3368 for (pageb
= page
->text
->blocks
;
3369 pageb
< page
->text
->blocks
+ page
->text
->len
;
3371 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3372 block
= pageb
->u
.text
;
3373 for (line
= block
->lines
;
3374 line
< block
->lines
+ block
->len
;
3378 for (span
= line
->first_span
; span
; span
= span
->next
) {
3379 for (i
= 0; i
< span
->len
; ++i
) {
3382 fz_text_char_bbox (&b
, span
, i
);
3384 if (x0
>= b
.x0
&& x0
<= b
.x1
3385 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3391 if (x1
>= b
.x0
&& x1
<= b
.x1
3392 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3398 if (0 && selected
) {
3399 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3400 glColor3ub (128, 128, 128);
3401 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3402 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3408 if (y1
< y0
|| x1
< x0
) {
3411 if (fspan
== lspan
) {
3416 if (fline
!= lline
) {
3435 page
->fmark
.span
= fspan
;
3438 page
->lmark
.span
= lspan
;
3440 unlock ("ml_seltext");
3443 CAMLreturn (Val_unit
);
3446 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3451 for (i
= a
; i
<= b
; ++i
) {
3452 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3453 ret
= fwrite (buf
, len
, 1, f
);
3456 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3457 len
, ret
, strerror (errno
));
3465 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3467 caml_failwith ("ml_popen not implemented under Cygwin");
3470 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3472 CAMLparam2 (command_v
, fds_v
);
3473 CAMLlocal2 (l_v
, tup_v
);
3476 value earg_v
= Nothing
;
3477 posix_spawnattr_t attr
;
3478 posix_spawn_file_actions_t fa
;
3479 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3481 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3482 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3485 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3486 msg
= "posix_spawnattr_init";
3490 #ifdef POSIX_SPAWN_USEVFORK
3491 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3492 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3497 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3500 tup_v
= Field (l_v
, 0);
3501 fd1
= Int_val (Field (tup_v
, 0));
3502 fd2
= Int_val (Field (tup_v
, 1));
3504 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3505 msg
= "posix_spawn_file_actions_addclose";
3511 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3512 msg
= "posix_spawn_file_actions_adddup2";
3519 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3520 msg
= "posix_spawn";
3525 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3526 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3530 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3531 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3536 unix_error (ret
, msg
, earg_v
);
3538 CAMLreturn (Val_unit
);
3542 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
, value clear_v
)
3544 CAMLparam3 (fd_v
, ptr_v
, clear_v
);
3549 fz_page_block
*pageb
;
3550 fz_text_block
*block
;
3551 int fd
= Int_val (fd_v
);
3552 char *s
= String_val (ptr_v
);
3553 int clear
= Bool_val (clear_v
);
3555 if (trylock ("ml_copysel")) {
3559 page
= parse_pointer ("ml_sopysel", s
);
3561 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3562 fprintf (stderr
, "nothing to copy\n");
3566 f
= fdopen (fd
, "w");
3568 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3573 for (pageb
= page
->text
->blocks
;
3574 pageb
< page
->text
->blocks
+ page
->text
->len
;
3576 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3577 block
= pageb
->u
.text
;
3578 for (line
= block
->lines
;
3579 line
< block
->lines
+ block
->len
;
3583 for (span
= line
->first_span
; span
; span
= span
->next
) {
3586 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3587 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3588 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3591 if (pipespan (f
, span
, a
, b
)) {
3594 if (span
== line
->last_span
) {
3595 if (putc ('\n', f
) == EOF
) {
3597 "failed break line on sel pipe: %s\n",
3602 if (span
== page
->lmark
.span
) {
3611 page
->lmark
.span
= NULL
;
3612 page
->fmark
.span
= NULL
;
3617 int ret
= fclose (f
);
3620 if (errno
!= ECHILD
) {
3621 fprintf (stderr
, "failed to close sel pipe: %s\n",
3627 unlock ("ml_copysel");
3632 fprintf (stderr
, "failed to close sel pipe: %s\n",
3636 CAMLreturn (Val_unit
);
3639 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3641 CAMLparam1 (pagedimno_v
);
3643 int pagedimno
= Int_val (pagedimno_v
);
3646 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3647 if (trylock ("ml_getpdimrect")) {
3648 box
= fz_empty_rect
;
3651 box
= state
.pagedims
[pagedimno
].mediabox
;
3652 unlock ("ml_getpdimrect");
3655 Store_double_field (ret_v
, 0, box
.x0
);
3656 Store_double_field (ret_v
, 1, box
.x1
);
3657 Store_double_field (ret_v
, 2, box
.y0
);
3658 Store_double_field (ret_v
, 3, box
.y1
);
3663 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3664 value dw_v
, value cols_v
)
3666 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3672 double winw
= Int_val (winw_v
);
3673 double winh
= Int_val (winh_v
);
3674 double dw
= Int_val (dw_v
);
3675 double cols
= Int_val (cols_v
);
3676 double pw
= 1.0, ph
= 1.0;
3678 if (trylock ("ml_zoom_for_height")) {
3682 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3683 double w
= p
->pagebox
.x1
/ cols
;
3684 double h
= p
->pagebox
.y1
;
3688 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3690 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3693 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3694 unlock ("ml_zoom_for_height");
3696 ret_v
= caml_copy_double (zoom
);
3700 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3702 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3704 int pt
= Int_val(pt_v
);
3705 int x
= Int_val (x_v
);
3706 int y
= Int_val (y_v
);
3709 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3710 ret_v
= caml_copy_double (w
);
3714 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3716 CAMLparam2 (pt_v
, string_v
);
3718 int pt
= Int_val (pt_v
);
3721 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3722 ret_v
= caml_copy_double (w
);
3726 CAMLprim value
ml_getpagebox (value opaque_v
)
3728 CAMLparam1 (opaque_v
);
3734 char *s
= String_val (opaque_v
);
3735 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3737 ret_v
= caml_alloc_tuple (4);
3738 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3739 dev
->hints
|= FZ_IGNORE_SHADE
;
3741 switch (page
->type
) {
3743 ctm
= pagectm (page
);
3744 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3748 ctm
= pagectm (page
);
3749 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3753 rect
= fz_infinite_rect
;
3757 fz_free_device (dev
);
3758 fz_round_rect (&bbox
, &rect
);
3759 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3760 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3761 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3762 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3767 CAMLprim value
ml_setaalevel (value level_v
)
3769 CAMLparam1 (level_v
);
3771 state
.aalevel
= Int_val (level_v
);
3772 CAMLreturn (Val_unit
);
3776 #include <X11/Xlib.h>
3782 GLXDrawable drawable
;
3785 #include "keysym2ucs.c"
3787 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3789 CAMLparam1 (keysym_v
);
3791 KeySym keysym
= Int_val (keysym_v
);
3796 rune
= keysym2ucs (keysym
);
3797 len
= fz_runetochar (buf
, rune
);
3799 str_v
= caml_copy_string (buf
);
3803 CAMLprim value
ml_glx (value win_v
)
3806 XVisualInfo
*visual
;
3807 int screen
, wid
= Int_val (win_v
);
3808 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3810 glx
.dpy
= XOpenDisplay (NULL
);
3812 caml_failwith ("XOpenDisplay");
3815 screen
= DefaultScreen (glx
.dpy
);
3816 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3818 XCloseDisplay (glx
.dpy
);
3820 caml_failwith ("glXChooseVisual");
3823 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3826 XCloseDisplay (glx
.dpy
);
3828 caml_failwith ("glXCreateContext");
3831 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3832 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3833 XCloseDisplay (glx
.dpy
);
3836 caml_failwith ("glXMakeCurrent");
3839 CAMLreturn (Val_unit
);
3842 CAMLprim value
ml_swapb (value unit_v
)
3844 CAMLparam1 (unit_v
);
3845 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3846 CAMLreturn (Val_unit
);
3849 CAMLprim value
ml_glxsync (value unit_v
)
3851 CAMLparam1 (unit_v
);
3852 if (glx
.dpy
&& glx
.ctx
) {
3856 CAMLreturn (Val_unit
);
3859 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3860 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3862 CAMLprim value
ml_platform (value unit_v
)
3864 CAMLparam1 (unit_v
);
3865 int platid
= piunknown
;
3867 #if defined __linux__
3869 #elif defined __CYGWIN__
3871 #elif defined __DragonFly__
3872 platid
= pidragonflybsd
;
3873 #elif defined __FreeBSD__
3875 #elif defined __OpenBSD__
3877 #elif defined __NetBSD__
3879 #elif defined __sun__
3881 #elif defined __APPLE__
3884 CAMLreturn (Val_int (platid
));
3887 CAMLprim value
ml_cloexec (value fd_v
)
3890 int fd
= Int_val (fd_v
);
3892 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3893 uerror ("fcntl", Nothing
);
3895 CAMLreturn (Val_unit
);
3898 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3900 CAMLparam2 (w_v
, h_v
);
3903 int w
= Int_val (w_v
);
3904 int h
= Int_val (h_v
);
3905 int cs
= Int_val (cs_v
);
3907 if (state
.pbo_usable
) {
3908 pbo
= calloc (sizeof (*pbo
), 1);
3910 err (1, "calloc pbo");
3922 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3925 state
.glGenBuffersARB (1, &pbo
->id
);
3926 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3927 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3928 NULL
, GL_STREAM_DRAW
);
3929 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3931 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3933 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3934 state
.glDeleteBuffersARB (1, &pbo
->id
);
3936 ret_v
= caml_copy_string ("0");
3942 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3944 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3948 err (1, "malloc %d bytes failed", res
+1);
3950 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3952 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3954 ret_v
= caml_copy_string (s
);
3959 ret_v
= caml_copy_string ("0");
3964 CAMLprim value
ml_freepbo (value s_v
)
3967 char *s
= String_val (s_v
);
3968 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3971 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3973 tile
->pbo
->ptr
= NULL
;
3974 tile
->pbo
->size
= -1;
3976 CAMLreturn (Val_unit
);
3979 CAMLprim value
ml_unmappbo (value s_v
)
3982 char *s
= String_val (s_v
);
3983 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3986 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3987 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3988 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3990 tile
->pbo
->ptr
= NULL
;
3991 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3993 CAMLreturn (Val_unit
);
3996 static void setuppbo (void)
3998 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
3999 state
.pbo_usable
= GGPA (glBindBufferARB
)
4000 && GGPA (glUnmapBufferARB
)
4001 && GGPA (glMapBufferARB
)
4002 && GGPA (glBufferDataARB
)
4003 && GGPA (glGenBuffersARB
)
4004 && GGPA (glDeleteBuffersARB
);
4008 CAMLprim value
ml_pbo_usable (value unit_v
)
4010 CAMLparam1 (unit_v
);
4011 CAMLreturn (Val_bool (state
.pbo_usable
));
4014 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4016 CAMLparam3 (ptr_v
, x_v
, y_v
);
4017 CAMLlocal2 (ret_v
, tup_v
);
4019 char *s
= String_val (ptr_v
);
4020 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4021 struct pagedim
*pdim
;
4025 page
= parse_pointer ("ml_unproject", s
);
4026 pdim
= &state
.pagedims
[page
->pdimno
];
4028 ret_v
= Val_int (0);
4029 if (trylock ("ml_unproject")) {
4033 switch (page
->type
) {
4035 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4044 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4045 fz_invert_matrix (&ctm
, &ctm
);
4046 fz_transform_point (&p
, &ctm
);
4048 tup_v
= caml_alloc_tuple (2);
4049 ret_v
= caml_alloc_small (1, 1);
4050 Field (tup_v
, 0) = Val_int (p
.x
);
4051 Field (tup_v
, 1) = Val_int (p
.y
);
4052 Field (ret_v
, 0) = tup_v
;
4054 unlock ("ml_unproject");
4059 static void makestippletex (void)
4061 const char pixels
[] = "\xff\xff\0\0";
4062 glGenTextures (1, &state
.stid
);
4063 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4064 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4065 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4078 CAMLprim value
ml_fz_version (value unit_v
)
4080 return caml_copy_string (FZ_VERSION
);
4083 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4085 CAMLparam2 (pipe_v
, params_v
);
4086 CAMLlocal2 (trim_v
, fuzz_v
);
4093 struct sigaction sa
;
4095 state
.cr
= Int_val (Field (pipe_v
, 0));
4096 state
.cw
= Int_val (Field (pipe_v
, 1));
4097 state
.rotate
= Int_val (Field (params_v
, 0));
4098 state
.fitmodel
= Int_val (Field (params_v
, 1));
4099 trim_v
= Field (params_v
, 2);
4100 texcount
= Int_val (Field (params_v
, 3));
4101 state
.sliceheight
= Int_val (Field (params_v
, 4));
4102 mustoresize
= Int_val (Field (params_v
, 5));
4103 colorspace
= Int_val (Field (params_v
, 6));
4104 fontpath
= String_val (Field (params_v
, 7));
4106 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4107 if (!state
.trimcachepath
) {
4108 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4111 haspboext
= Bool_val (Field (params_v
, 9));
4113 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4115 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4116 fuzz_v
= Field (trim_v
, 1);
4117 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4118 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4119 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4120 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4122 set_tex_params (colorspace
);
4125 state
.face
= load_font (fontpath
);
4129 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4131 state
.face
= load_builtin_font (base
, len
);
4133 if (!state
.face
) _exit (1);
4135 realloctexts (texcount
);
4144 sa
.sa_handler
= SIG_IGN
;
4145 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4147 sa
.sa_handler
= SIG_DFL
;
4148 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4150 if (sigemptyset (&sa
.sa_mask
)) {
4151 err (1, "sigemptyset");
4153 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4154 err (1, "sigaction");
4157 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4159 errx (1, "pthread_create: %s", strerror (ret
));
4162 CAMLreturn (Val_unit
);