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
);
490 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
491 int len2
= strlen (tbl
[i
].ext
);
493 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
494 state
.type
= tbl
[i
].type
;
500 if (state
.pagedims
) {
501 free (state
.pagedims
);
502 state
.pagedims
= NULL
;
504 state
.pagedimcount
= 0;
506 fz_set_aa_level (state
.ctx
, state
.aalevel
);
507 switch (state
.type
) {
509 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
510 if (pdf_needs_password (state
.u
.pdf
)) {
511 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
513 errx (1, "invalid password");
516 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
517 state
.closedoc
= closepdf
;
518 state
.freepage
= freepdfpage
;
522 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
523 state
.pagecount
= xps_count_pages (state
.u
.xps
);
524 state
.closedoc
= closexps
;
525 state
.freepage
= freexpspage
;
529 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
530 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
531 state
.closedoc
= closecbz
;
532 state
.freepage
= freecbzpage
;
536 state
.u
.img
= image_open_document (state
.ctx
, filename
);
538 state
.closedoc
= closeimg
;
539 state
.freepage
= freeimgpage
;
544 static void pdfinfo (void)
546 if (state
.type
== DPDF
) {
549 printd ("info PDF version\t%d.%d",
550 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
552 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
556 char *items
[] = { "Title", "Author", "Creator",
557 "Producer", "CreationDate" };
559 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
560 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
561 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
564 printd ("title %s", s
);
566 printd ("info %s\t%s", items
[i
], s
);
568 fz_free (state
.ctx
, s
);
575 static void unlinktile (struct tile
*tile
)
579 for (i
= 0; i
< tile
->slicecount
; ++i
) {
580 struct slice
*s
= &tile
->slices
[i
];
582 if (s
->texindex
!= -1) {
583 if (state
.texowners
[s
->texindex
].slice
== s
) {
584 state
.texowners
[s
->texindex
].slice
= NULL
;
590 static void freepage (struct page
*page
)
593 fz_free_text_page (state
.ctx
, page
->text
);
596 fz_free_text_sheet (state
.ctx
, page
->sheet
);
601 fz_drop_display_list (state
.ctx
, page
->dlist
);
602 page
->freepage (page
->u
.ptr
);
606 static void freetile (struct tile
*tile
)
611 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
614 fz_drop_pixmap (state
.ctx
, state
.pig
);
616 state
.pig
= tile
->pixmap
;
621 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
630 static int cacheline32bytes
;
632 static void __attribute__ ((constructor
)) clcheck (void)
634 char **envp
= environ
;
639 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
641 cacheline32bytes
= auxv
[1] == 32;
647 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
649 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
650 if (cacheline32bytes
&& size
> 32) {
651 intptr_t a1
, a2
, diff
;
653 vector
unsigned char v
= vec_splat_u8 (-1);
654 vector
unsigned char *p
;
656 a1
= a2
= (intptr_t) pixmap
->samples
;
657 a2
= (a1
+ 31) & ~31;
662 while (a1
!= a2
) *(char *) a1
++ = 0xff;
663 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
664 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
666 vec_st (v
, i
+ 16, p
);
668 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
670 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
673 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
676 static void trimctm (pdf_page
*page
, int pindex
)
679 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
681 if (!pdim
->tctmready
) {
682 if (state
.trimmargins
) {
684 fz_matrix rm
, sm
, tm
, im
, ctm1
;
686 fz_rotate (&rm
, -pdim
->rotate
);
687 fz_scale (&sm
, 1, -1);
688 fz_concat (&ctm
, &rm
, &sm
);
689 realbox
= pdim
->mediabox
;
690 fz_transform_rect (&realbox
, &ctm
);
691 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
692 fz_concat (&ctm1
, &ctm
, &tm
);
693 fz_invert_matrix (&im
, &page
->ctm
);
694 fz_concat (&ctm
, &im
, &ctm1
);
704 static fz_matrix
pagectm (struct page
*page
)
708 if (page
->type
== DPDF
) {
709 trimctm (page
->u
.pdfpage
, page
->pdimno
);
711 &state
.pagedims
[page
->pdimno
].tctm
,
712 &state
.pagedims
[page
->pdimno
].ctm
);
715 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
717 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
718 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
723 static void *loadpage (int pageno
, int pindex
)
728 page
= calloc (sizeof (struct page
), 1);
730 err (1, "calloc page %d", pageno
);
733 page
->dlist
= fz_new_display_list (state
.ctx
);
734 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
736 switch (state
.type
) {
738 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
739 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
741 page
->freepage
= freepdfpage
;
745 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
746 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
748 page
->freepage
= freexpspage
;
752 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
753 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
755 page
->freepage
= freecbzpage
;
759 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
760 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
762 page
->freepage
= freeimgpage
;
766 fz_catch (state
.ctx
) {
768 page
->freepage
= freeemptyxxxpage
;
770 fz_free_device (dev
);
772 page
->pdimno
= pindex
;
773 page
->pageno
= pageno
;
774 page
->sgen
= state
.gen
;
775 page
->tgen
= state
.gen
;
776 page
->type
= state
.type
;
781 static struct tile
*alloctile (int h
)
788 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
789 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
790 tile
= calloc (tilesize
, 1);
792 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
794 for (i
= 0; i
< slicecount
; ++i
) {
795 int sh
= MIN (h
, state
.sliceheight
);
796 tile
->slices
[i
].h
= sh
;
797 tile
->slices
[i
].texindex
= -1;
800 tile
->slicecount
= slicecount
;
801 tile
->sliceheight
= state
.sliceheight
;
811 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
812 const fz_matrix
*ctm
, float alpha
)
814 struct obs
*obs
= dev
->user
;
816 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
818 fz_rect rect
= fz_unit_rect
;
820 fz_transform_rect (&rect
, ctm
);
821 fz_round_rect (&b
, &rect
);
822 fz_intersect_irect (&b
, &obs
->b
);
823 obs
->cured
= b
.x0
== obs
->b
.x0
826 && b
.y1
== obs
->b
.y1
;
830 static int obscured (struct page
*page
, fz_irect bbox
)
837 memset (&dev
, 0, sizeof (dev
));
838 memset (&obs
, 0, sizeof (obs
));
843 dev
.fill_image
= obs_fill_image
;
845 fz_rect_from_irect (&rect
, &bbox
);
846 ctm
= pagectm (page
);
847 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
850 #define OBSCURED obscured
852 #define OBSCURED(a, b) 0
855 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
863 struct pagedim
*pdim
;
865 tile
= alloctile (h
);
866 pdim
= &state
.pagedims
[page
->pdimno
];
871 bbox
.x1
= bbox
.x0
+ w
;
872 bbox
.y1
= bbox
.y0
+ h
;
875 if (state
.pig
->w
== w
877 && state
.pig
->colorspace
== state
.colorspace
) {
878 tile
->pixmap
= state
.pig
;
879 tile
->pixmap
->x
= bbox
.x0
;
880 tile
->pixmap
->y
= bbox
.y0
;
883 fz_drop_pixmap (state
.ctx
, state
.pig
);
890 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
896 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
902 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
903 clearpixmap (tile
->pixmap
);
905 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
906 ctm
= pagectm (page
);
907 fz_rect_from_irect (&rect
, &bbox
);
908 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
909 fz_free_device (dev
);
914 static void initpdims (void)
918 fz_rect rootmediabox
;
919 int pageno
, trim
, show
;
920 int trimw
= 0, cxcount
;
924 if (state
.trimmargins
&& state
.trimcachepath
) {
925 trimf
= fopen (state
.trimcachepath
, "rb");
927 trimf
= fopen (state
.trimcachepath
, "wb");
932 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
933 cxcount
= state
.pagecount
;
935 cxcount
= MIN (state
.pagecount
, 1);
937 if (state
.type
== DPDF
) {
939 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
940 "Root/Pages/MediaBox");
941 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
944 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
949 switch (state
.type
) {
951 pdf_document
*pdf
= state
.u
.pdf
;
952 pdf_obj
*pageref
= pdf_lookup_page_obj (pdf
, pageno
);
953 pdf_obj
*pageobj
= pdf_resolve_indirect (pageref
);
955 if (state
.trimmargins
) {
960 page
= pdf_load_page (pdf
, pageno
);
961 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
962 trim
= state
.trimanew
|| !obj
;
968 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
969 dev
->hints
|= FZ_IGNORE_SHADE
;
970 fz_invert_matrix (&ctm
, &page
->ctm
);
971 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
972 fz_free_device (dev
);
974 rect
.x0
+= state
.trimfuzz
.x0
;
975 rect
.x1
+= state
.trimfuzz
.x1
;
976 rect
.y0
+= state
.trimfuzz
.y0
;
977 rect
.y1
+= state
.trimfuzz
.y1
;
978 fz_transform_rect (&rect
, &ctm
);
979 fz_intersect_rect (&rect
, &page
->mediabox
);
981 if (fz_is_empty_rect (&rect
)) {
982 mediabox
= page
->mediabox
;
988 obj
= pdf_new_array (pdf
, 4);
989 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
990 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
991 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
992 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
993 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
996 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
997 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
998 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
999 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1002 rotate
= page
->rotate
;
1003 pdf_free_page (pdf
, page
);
1005 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1007 printd ("progress %f Trimming %d",
1008 (double) (pageno
+ 1) / state
.pagecount
,
1012 fz_catch (state
.ctx
) {
1013 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1020 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1022 if (fz_is_empty_rect (&mediabox
)) {
1030 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1032 if (!fz_is_empty_rect (&cropbox
)) {
1033 fz_intersect_rect (&mediabox
, &cropbox
);
1037 if (fz_is_empty_rect (&rootmediabox
)) {
1039 "cannot find page size for page %d\n",
1043 mediabox
= rootmediabox
;
1047 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1056 fz_try (state
.ctx
) {
1057 page
= xps_load_page (state
.u
.xps
, pageno
);
1058 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1060 if (state
.trimmargins
) {
1064 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1065 dev
->hints
|= FZ_IGNORE_SHADE
;
1066 xps_run_page (state
.u
.xps
, page
, dev
,
1067 &fz_identity
, NULL
);
1068 fz_free_device (dev
);
1070 rect
.x0
+= state
.trimfuzz
.x0
;
1071 rect
.x1
+= state
.trimfuzz
.x1
;
1072 rect
.y0
+= state
.trimfuzz
.y0
;
1073 rect
.y1
+= state
.trimfuzz
.y1
;
1074 fz_intersect_rect (&rect
, &mediabox
);
1076 if (!fz_is_empty_rect (&rect
)) {
1080 xps_free_page (state
.u
.xps
, page
);
1082 printd ("progress %f loading %d",
1083 (double) (pageno
+ 1) / state
.pagecount
,
1087 fz_catch (state
.ctx
) {
1099 if (state
.trimmargins
&& trimw
) {
1102 fz_try (state
.ctx
) {
1103 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1104 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1105 cbz_free_page (state
.u
.cbz
, page
);
1106 printd ("progress %f Trimming %d",
1107 (double) (pageno
+ 1) / state
.pagecount
,
1110 fz_catch (state
.ctx
) {
1111 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1114 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1116 err (1, "fwrite trim mediabox");
1122 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1124 err (1, "fread trim mediabox");
1130 fz_try (state
.ctx
) {
1131 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1132 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1133 cbz_free_page (state
.u
.cbz
, page
);
1135 fz_catch (state
.ctx
) {
1136 fprintf (stderr
, "failed to load cbz page\n");
1154 fz_try (state
.ctx
) {
1155 page
= image_load_page (state
.u
.img
, pageno
);
1156 image_bound_page (state
.u
.img
, page
, &mediabox
);
1157 image_free_page (state
.u
.img
, page
);
1159 fz_catch (state
.ctx
) {
1160 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1166 ARSERT (0 && state
.type
);
1169 if (state
.pagedimcount
== 0
1170 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1171 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1174 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1175 state
.pagedims
= realloc (state
.pagedims
, size
);
1176 if (!state
.pagedims
) {
1177 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1178 size
, state
.pagedimcount
+ 1);
1181 p
= &state
.pagedims
[state
.pagedimcount
++];
1183 p
->mediabox
= mediabox
;
1188 if (state
.trimmargins
) {
1189 printd ("progress 1 Trimmed %d pages in %f seconds",
1190 state
.pagecount
, end
- start
);
1193 printd ("vmsg Processed %d pages in %f seconds",
1194 state
.pagecount
, end
- start
);
1198 if (fclose (trimf
)) {
1204 static void layout (void)
1209 struct pagedim
*p
= p
;
1210 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1212 if (state
.pagedimcount
== 0) return;
1214 switch (state
.fitmodel
) {
1215 case FitProportional
:
1216 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1219 p
= &state
.pagedims
[pindex
];
1220 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1222 fz_transform_rect (&box
, &rm
);
1224 x0
= MIN (box
.x0
, box
.x1
);
1225 x1
= MAX (box
.x0
, box
.x1
);
1228 maxw
= MAX (w
, maxw
);
1229 zoom
= state
.w
/ maxw
;
1241 ARSERT (0 && state
.fitmodel
);
1244 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1248 p
= &state
.pagedims
[pindex
];
1249 fz_rotate (&ctm
, state
.rotate
);
1250 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1252 fz_transform_rect (&box
, &rm
);
1253 w
= box
.x1
- box
.x0
;
1254 switch (state
.fitmodel
) {
1255 case FitProportional
:
1256 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1262 h
= box
.y1
- box
.y0
;
1264 zoom
= MIN (zw
, zh
);
1265 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1274 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1275 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1277 fz_rotate (&rm
, p
->rotate
);
1278 p
->pagebox
= p
->mediabox
;
1279 fz_transform_rect (&p
->pagebox
, &rm
);
1280 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1281 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1285 fz_transform_rect (&rect
, &ctm
);
1286 fz_round_rect (&p
->bounds
, &rect
);
1289 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1290 fz_scale (&sm
, zoom
, -zoom
);
1291 fz_concat (&ctm
, &tm
, &sm
);
1292 fz_concat (&p
->lctm
, &ctm
, &rm
);
1298 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1299 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1300 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1301 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1305 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1306 } while (p
-- != state
.pagedims
);
1310 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1311 desttoanchor (fz_link_dest
*dest
)
1315 struct pagedim
*pdim
= state
.pagedims
;
1320 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1321 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1323 pdim
= &state
.pagedims
[i
];
1325 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1327 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1328 p
.x
= dest
->ld
.gotor
.lt
.x
;
1331 p
.y
= dest
->ld
.gotor
.lt
.y
;
1332 fz_transform_point (&p
, &pdim
->lctm
);
1336 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1337 double x0
, x1
, y0
, y1
;
1339 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1340 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1342 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1343 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1345 a
.n
= dest
->ld
.gotor
.page
;
1350 static void recurse_outline (fz_outline
*outline
, int level
)
1353 switch (outline
->dest
.kind
) {
1356 struct anchor a
= desttoanchor (&outline
->dest
);
1359 printd ("o %d %d %d %d %s",
1360 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1366 printd ("ou %d %" FMT_s
" %s %s", level
,
1367 strlen (outline
->title
), outline
->title
,
1368 outline
->dest
.ld
.uri
.uri
);
1372 printd ("on %d %s", level
, outline
->title
);
1376 printd ("emsg Unhandled outline kind %d for %s\n",
1377 outline
->dest
.kind
, outline
->title
);
1380 if (outline
->down
) {
1381 recurse_outline (outline
->down
, level
+ 1);
1383 outline
= outline
->next
;
1387 static void process_outline (void)
1389 fz_outline
*outline
;
1391 if (!state
.needoutline
) return;
1393 state
.needoutline
= 0;
1394 switch (state
.type
) {
1396 outline
= pdf_load_outline (state
.u
.pdf
);
1399 outline
= xps_load_outline (state
.u
.xps
);
1406 recurse_outline (outline
, 0);
1407 fz_free_outline (state
.ctx
, outline
);
1411 static char *strofspan (fz_text_span
*span
)
1416 size_t size
= 0, cap
= 80;
1418 p
= malloc (cap
+ 1);
1419 if (!p
) return NULL
;
1421 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1422 int n
= fz_runetochar (utf8
, ch
->c
);
1423 if (size
+ n
> cap
) {
1425 p
= realloc (p
, cap
+ 1);
1426 if (!p
) return NULL
;
1429 memcpy (p
+ size
, utf8
, n
);
1436 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1437 fz_text_span
*span
, fz_matrix ctm
,
1438 int stop
, int pageno
, double start
)
1445 fz_point p1
, p2
, p3
, p4
;
1447 p
= strofspan (span
);
1450 ret
= regexec (re
, p
, 1, &rm
, 0);
1453 if (ret
!= REG_NOMATCH
) {
1456 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1457 printd ("msg regexec error `%.*s'",
1458 (int) size
, errbuf
);
1466 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1467 c
+= fz_runelen (span
->text
[a
].c
);
1469 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1470 c
+= fz_runelen (span
->text
[b
].c
);
1473 if (fz_runelen (span
->text
[b
].c
) > 1) {
1477 fz_text_char_bbox (&sb
, span
, a
);
1478 fz_text_char_bbox (&eb
, span
, b
);
1490 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1497 printd ("progress 1 found at %d `%.*s' in %f sec",
1498 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1502 printd ("match %d %d %f %f %f %f %f %f %f %f",
1514 static int compareblocks (const void *l
, const void *r
)
1516 fz_text_block
const *ls
= l
;
1517 fz_text_block
const* rs
= r
;
1518 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1521 /* wishful thinking function */
1522 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1527 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1529 fz_text_sheet
*sheet
;
1530 struct pagedim
*pdim
, *pdimprev
;
1531 int stop
= 0, niters
= 0;
1534 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1538 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1539 if (niters
++ == 5) {
1542 printd ("progress 1 attention requested aborting search at %d",
1547 printd ("progress %f searching in page %d",
1548 (double) (pageno
+ 1) / state
.pagecount
,
1553 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1554 pdim
= &state
.pagedims
[i
];
1555 if (pdim
->pageno
== pageno
) {
1558 if (pdim
->pageno
> pageno
) {
1567 sheet
= fz_new_text_sheet (state
.ctx
);
1568 text
= fz_new_text_page (state
.ctx
);
1569 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1571 switch (state
.type
) {
1574 fz_try (state
.ctx
) {
1575 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1576 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1577 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1578 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1579 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1582 fz_catch (state
.ctx
) {
1583 fz_free_device (tdev
);
1590 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1591 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1595 ARSERT (0 && state
.type
);
1598 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1599 fz_free_device (tdev
);
1601 for (j
= 0; j
< text
->len
; ++j
) {
1604 fz_text_block
*block
;
1606 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1607 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1610 for (k
= 0; k
< block
->len
; ++k
) {
1615 line
= &block
->lines
[k
];
1616 if (line
->bbox
.y0
< y
+ 1) continue;
1619 line
= &block
->lines
[block
->len
- 1 - k
];
1620 if (line
->bbox
.y0
> y
- 1) continue;
1623 for (span
= line
->first_span
; span
; span
= span
->next
) {
1624 switch (matchspan (re
, text
, span
, ctm
,
1625 stop
, pageno
, start
)) {
1627 case 1: stop
= 1; break;
1628 case -1: stop
= 1; goto endloop
;
1643 fz_free_text_page (state
.ctx
, text
);
1644 fz_free_text_sheet (state
.ctx
, sheet
);
1646 state
.freepage (u
.ptr
);
1651 printd ("progress 1 no matches %f sec", end
- start
);
1653 printd ("clearrects");
1656 static void set_tex_params (int colorspace
)
1663 switch (colorspace
) {
1665 state
.texiform
= GL_RGBA8
;
1666 state
.texform
= GL_RGBA
;
1667 state
.texty
= GL_UNSIGNED_BYTE
;
1668 state
.colorspace
= fz_device_rgb (state
.ctx
);
1671 state
.texiform
= GL_RGBA8
;
1672 state
.texform
= GL_BGRA
;
1673 state
.texty
= endianness
.s
> 1
1674 ? GL_UNSIGNED_INT_8_8_8_8
1675 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1676 state
.colorspace
= fz_device_bgr (state
.ctx
);
1679 state
.texiform
= GL_LUMINANCE_ALPHA
;
1680 state
.texform
= GL_LUMINANCE_ALPHA
;
1681 state
.texty
= GL_UNSIGNED_BYTE
;
1682 state
.colorspace
= fz_device_gray (state
.ctx
);
1685 errx (1, "invalid colorspce %d", colorspace
);
1689 static void realloctexts (int texcount
)
1693 if (texcount
== state
.texcount
) return;
1695 if (texcount
< state
.texcount
) {
1696 glDeleteTextures (state
.texcount
- texcount
,
1697 state
.texids
+ texcount
);
1700 size
= texcount
* sizeof (*state
.texids
);
1701 state
.texids
= realloc (state
.texids
, size
);
1702 if (!state
.texids
) {
1703 err (1, "realloc texids %" FMT_s
, size
);
1706 size
= texcount
* sizeof (*state
.texowners
);
1707 state
.texowners
= realloc (state
.texowners
, size
);
1708 if (!state
.texowners
) {
1709 err (1, "realloc texowners %" FMT_s
, size
);
1711 if (texcount
> state
.texcount
) {
1714 glGenTextures (texcount
- state
.texcount
,
1715 state
.texids
+ state
.texcount
);
1716 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1717 state
.texowners
[i
].w
= -1;
1718 state
.texowners
[i
].slice
= NULL
;
1721 state
.texcount
= texcount
;
1725 static char *mbtoutf8 (char *s
)
1731 len
= mbstowcs (NULL
, s
, strlen (s
));
1736 if (len
== (size_t) -1) {
1741 tmp
= malloc (len
* sizeof (wchar_t));
1746 ret
= mbstowcs (tmp
, s
, len
);
1747 if (ret
== (size_t) -1) {
1753 for (i
= 0; i
< ret
; ++i
) {
1754 len
+= fz_runelen (tmp
[i
]);
1757 p
= r
= malloc (len
+ 1);
1763 for (i
= 0; i
< ret
; ++i
) {
1764 p
+= fz_runetochar (p
, tmp
[i
]);
1771 CAMLprim value
ml_mbtoutf8 (value s_v
)
1777 s
= String_val (s_v
);
1783 ret_v
= caml_copy_string (r
);
1789 static void * mainloop (void *unused
)
1792 int len
, ret
, oldlen
= 0;
1797 errx (1, "readlen returned 0");
1800 if (oldlen
< len
+ 1) {
1801 p
= realloc (p
, len
+ 1);
1803 err (1, "realloc %d failed", len
+ 1);
1810 if (!strncmp ("open", p
, 4)) {
1817 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1819 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1822 filename
= p
+ 5 + off
;
1823 filenamelen
= strlen (filename
);
1824 password
= filename
+ filenamelen
+ 1;
1827 openxref (filename
, password
);
1833 utf8filename
= mbtoutf8 (filename
);
1834 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1835 if (utf8filename
!= filename
) {
1836 free (utf8filename
);
1839 state
.needoutline
= 1;
1841 else if (!strncmp ("cs", p
, 2)) {
1844 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1846 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1849 set_tex_params (colorspace
);
1850 for (i
= 0; i
< state
.texcount
; ++i
) {
1851 state
.texowners
[i
].w
= -1;
1852 state
.texowners
[i
].slice
= NULL
;
1856 else if (!strncmp ("freepage", p
, 8)) {
1859 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1861 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1865 else if (!strncmp ("freetile", p
, 8)) {
1868 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1870 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1874 else if (!strncmp ("search", p
, 6)) {
1875 int icase
, pageno
, y
, ret
, len2
, forward
;
1879 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1880 &icase
, &pageno
, &y
, &forward
, &len2
);
1882 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1885 pattern
= p
+ 6 + len2
;
1886 ret
= regcomp (&re
, pattern
,
1887 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1892 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1893 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1896 search (&re
, pageno
, y
, forward
);
1900 else if (!strncmp ("geometry", p
, 8)) {
1904 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1906 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1914 for (i
= 0; i
< state
.texcount
; ++i
) {
1915 state
.texowners
[i
].slice
= NULL
;
1918 state
.fitmodel
= fitmodel
;
1923 unlock ("geometry");
1924 printd ("continue %d", state
.pagecount
);
1926 else if (!strncmp ("reqlayout", p
, 9)) {
1928 int rotate
, fitmodel
, off
, h
;
1931 ret
= sscanf (p
+ 9, " %d %d %d %n",
1932 &rotate
, &fitmodel
, &h
, &off
);
1934 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1937 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1940 state
.rotate
= rotate
;
1941 state
.fitmodel
= fitmodel
;
1946 nameddest
= p
+ 9 + off
;
1947 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1950 pdf_obj
*needle
, *obj
;
1952 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
1953 strlen (nameddest
));
1954 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1956 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
1958 a
= desttoanchor (&dest
);
1960 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1963 printd ("emsg failed to parse destination `%s'\n",
1968 printd ("emsg destination `%s' not found\n",
1971 pdf_drop_obj (needle
);
1975 unlock ("reqlayout");
1976 printd ("continue %d", state
.pagecount
);
1978 else if (!strncmp ("page", p
, 4)) {
1981 int pageno
, pindex
, ret
;
1983 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1985 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1990 page
= loadpage (pageno
, pindex
);
1994 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1996 else if (!strncmp ("tile", p
, 4)) {
1997 int x
, y
, w
, h
, ret
;
2003 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
2004 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
2006 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2011 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2015 printd ("tile %d %d %" FMT_ptr
" %u %f",
2017 FMT_ptr_cast2 (tile
),
2018 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2021 else if (!strncmp ("settrim", p
, 7)) {
2025 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2026 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2028 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2032 state
.trimmargins
= trimmargins
;
2033 state
.needoutline
= 1;
2034 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2036 state
.trimfuzz
= fuzz
;
2038 state
.pagedimcount
= 0;
2039 free (state
.pagedims
);
2040 state
.pagedims
= NULL
;
2045 printd ("continue %d", state
.pagecount
);
2047 else if (!strncmp ("sliceh", p
, 6)) {
2050 ret
= sscanf (p
+ 6, " %d", &h
);
2052 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2054 if (h
!= state
.sliceheight
) {
2057 state
.sliceheight
= h
;
2058 for (i
= 0; i
< state
.texcount
; ++i
) {
2059 state
.texowners
[i
].w
= -1;
2060 state
.texowners
[i
].h
= -1;
2061 state
.texowners
[i
].slice
= NULL
;
2065 else if (!strncmp ("interrupt", p
, 9)) {
2066 printd ("vmsg interrupted");
2069 errx (1, "unknown command %.*s", len
, p
);
2075 CAMLprim value
ml_realloctexts (value texcount_v
)
2077 CAMLparam1 (texcount_v
);
2080 if (trylock ("ml_realloctexts")) {
2084 realloctexts (Int_val (texcount_v
));
2086 unlock ("ml_realloctexts");
2089 CAMLreturn (Val_bool (ok
));
2092 static void recti (int x0
, int y0
, int x1
, int y1
)
2094 GLfloat
*v
= state
.vertices
;
2096 glVertexPointer (2, GL_FLOAT
, 0, v
);
2097 v
[0] = x0
; v
[1] = y0
;
2098 v
[2] = x1
; v
[3] = y0
;
2099 v
[4] = x0
; v
[5] = y1
;
2100 v
[6] = x1
; v
[7] = y1
;
2101 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2104 static void showsel (struct page
*page
, int ox
, int oy
)
2110 fz_page_block
*pageb
;
2111 fz_text_block
*block
;
2112 struct mark first
, last
;
2114 first
= page
->fmark
;
2117 if (!first
.span
|| !last
.span
) return;
2119 glEnable (GL_BLEND
);
2120 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2121 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
2123 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2124 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2125 for (pageb
= page
->text
->blocks
;
2126 pageb
< page
->text
->blocks
+ page
->text
->len
;
2128 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2129 block
= pageb
->u
.text
;
2131 for (line
= block
->lines
;
2132 line
< block
->lines
+ block
->len
;
2135 rect
= fz_empty_rect
;
2137 for (span
= line
->first_span
; span
; span
= span
->next
) {
2139 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2144 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2146 j
= MIN (first
.i
, last
.i
);
2147 k
= MAX (first
.i
, last
.i
);
2149 else if (span
== first
.span
) {
2153 else if (span
== last
.span
) {
2159 for (i
= j
; i
<= k
; ++i
) {
2161 fz_union_rect (&rect
,
2162 fz_text_char_bbox (&bbox
, span
, i
));
2164 fz_round_rect (&bbox
, &rect
);
2165 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2172 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2173 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2174 if (span
== last
.span
) {
2177 rect
= fz_empty_rect
;
2183 glDisable (GL_BLEND
);
2188 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2190 fz_matrix ctm
, tm
, pm
;
2191 fz_link
*link
, *links
;
2192 GLfloat
*texcoords
= state
.texcoords
;
2193 GLfloat
*vertices
= state
.vertices
;
2195 switch (page
->type
) {
2197 links
= page
->u
.pdfpage
->links
;
2201 links
= page
->u
.xpspage
->links
;
2208 glEnable (GL_TEXTURE_1D
);
2209 glEnable (GL_BLEND
);
2210 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2211 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2213 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2214 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2215 fz_translate (&tm
, xoff
, yoff
);
2216 pm
= pagectm (page
);
2217 fz_concat (&ctm
, &pm
, &tm
);
2219 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2220 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2222 for (link
= links
; link
; link
= link
->next
) {
2223 fz_point p1
, p2
, p3
, p4
;
2225 p1
.x
= link
->rect
.x0
;
2226 p1
.y
= link
->rect
.y0
;
2228 p2
.x
= link
->rect
.x1
;
2229 p2
.y
= link
->rect
.y0
;
2231 p3
.x
= link
->rect
.x1
;
2232 p3
.y
= link
->rect
.y1
;
2234 p4
.x
= link
->rect
.x0
;
2235 p4
.y
= link
->rect
.y1
;
2237 fz_transform_point (&p1
, &ctm
);
2238 fz_transform_point (&p2
, &ctm
);
2239 fz_transform_point (&p3
, &ctm
);
2240 fz_transform_point (&p4
, &ctm
);
2242 switch (link
->dest
.kind
) {
2243 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2244 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2245 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2246 default: glColor3ub (0, 0, 0); break;
2254 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2258 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2260 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2261 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2263 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2264 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2266 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2267 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2269 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2270 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2272 glDrawArrays (GL_LINES
, 0, 8);
2275 glDisable (GL_BLEND
);
2276 glDisable (GL_TEXTURE_1D
);
2279 static int compareslinks (const void *l
, const void *r
)
2281 struct slink
const *ls
= l
;
2282 struct slink
const *rs
= r
;
2283 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2284 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2286 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2289 static void droptext (struct page
*page
)
2292 fz_free_text_page (state
.ctx
, page
->text
);
2295 page
->fmark
.span
= NULL
;
2296 page
->lmark
.span
= NULL
;
2300 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2305 static void dropslinks (struct page
*page
)
2308 free (page
->slinks
);
2309 page
->slinks
= NULL
;
2310 page
->slinkcount
= 0;
2314 static void ensureslinks (struct page
*page
)
2318 size_t slinksize
= sizeof (*page
->slinks
);
2319 fz_link
*link
, *links
;
2321 if (state
.gen
!= page
->sgen
) {
2323 page
->sgen
= state
.gen
;
2325 if (page
->slinks
) return;
2327 switch (page
->type
) {
2329 links
= page
->u
.pdfpage
->links
;
2330 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2332 &state
.pagedims
[page
->pdimno
].tctm
,
2333 &state
.pagedims
[page
->pdimno
].ctm
);
2337 links
= page
->u
.xpspage
->links
;
2338 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2345 for (link
= links
; link
; link
= link
->next
) {
2349 page
->slinkcount
= count
;
2350 page
->slinks
= calloc (count
, slinksize
);
2351 if (!page
->slinks
) {
2352 err (1, "realloc slinks %d", count
);
2355 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2359 fz_transform_rect (&rect
, &ctm
);
2360 page
->slinks
[i
].link
= link
;
2361 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2363 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2367 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2368 static void fmt_linkn (char *s
, unsigned int u
)
2370 unsigned int len
; unsigned int q
;
2371 int zma
= 'z' - 'a' + 1;
2373 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2376 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2377 /* handles u == 0 */
2382 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2383 int noff
, char *targ
, int tlen
, int hfsize
)
2387 struct slink
*slink
;
2388 double x0
, y0
, x1
, y1
, w
;
2390 ensureslinks (page
);
2391 glColor3ub (0xc3, 0xb0, 0x91);
2392 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2393 fmt_linkn (buf
, i
+ noff
);
2394 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2395 slink
= &page
->slinks
[i
];
2397 x0
= slink
->bbox
.x0
+ xoff
- 5;
2398 y1
= slink
->bbox
.y0
+ yoff
- 5;
2399 y0
= y1
+ 10 + hfsize
;
2400 w
= measure_string (state
.face
, hfsize
, buf
);
2402 recti (x0
, y0
, x1
, y1
);
2406 glEnable (GL_BLEND
);
2407 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2408 glEnable (GL_TEXTURE_2D
);
2409 glColor3ub (0, 0, 0);
2410 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2411 fmt_linkn (buf
, i
+ noff
);
2412 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2413 slink
= &page
->slinks
[i
];
2415 x0
= slink
->bbox
.x0
+ xoff
;
2416 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2417 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2420 glDisable (GL_TEXTURE_2D
);
2421 glDisable (GL_BLEND
);
2425 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2428 struct slice
*slice1
;
2429 unsigned char *texdata
;
2432 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2433 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2435 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2436 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2437 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2441 int texindex
= state
.texindex
++ % state
.texcount
;
2443 if (state
.texowners
[texindex
].w
== tile
->w
) {
2444 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2448 state
.texowners
[texindex
].h
= slice
->h
;
2452 state
.texowners
[texindex
].h
= slice
->h
;
2455 state
.texowners
[texindex
].w
= tile
->w
;
2456 state
.texowners
[texindex
].slice
= slice
;
2457 slice
->texindex
= texindex
;
2459 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2461 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2465 texdata
= tile
->pixmap
->samples
;
2468 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2480 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2492 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2497 CAMLprim value
ml_begintiles (value unit_v
)
2499 CAMLparam1 (unit_v
);
2500 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2501 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2502 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2503 CAMLreturn (unit_v
);
2506 CAMLprim value
ml_endtiles (value unit_v
)
2508 CAMLparam1 (unit_v
);
2509 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2510 CAMLreturn (unit_v
);
2513 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2515 CAMLparam2 (args_v
, ptr_v
);
2516 int dispx
= Int_val (Field (args_v
, 0));
2517 int dispy
= Int_val (Field (args_v
, 1));
2518 int dispw
= Int_val (Field (args_v
, 2));
2519 int disph
= Int_val (Field (args_v
, 3));
2520 int tilex
= Int_val (Field (args_v
, 4));
2521 int tiley
= Int_val (Field (args_v
, 5));
2522 char *s
= String_val (ptr_v
);
2523 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2524 int slicey
, firstslice
;
2525 struct slice
*slice
;
2526 GLfloat
*texcoords
= state
.texcoords
;
2527 GLfloat
*vertices
= state
.vertices
;
2529 firstslice
= tiley
/ tile
->sliceheight
;
2530 slice
= &tile
->slices
[firstslice
];
2531 slicey
= tiley
% tile
->sliceheight
;
2536 dh
= slice
->h
- slicey
;
2537 dh
= MIN (disph
, dh
);
2538 uploadslice (tile
, slice
);
2540 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2541 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2542 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2543 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2545 vertices
[0] = dispx
; vertices
[1] = dispy
;
2546 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2547 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2548 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2550 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2554 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2557 CAMLreturn (Val_unit
);
2560 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2561 value xoff_v
, value yoff_v
,
2564 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2565 int xoff
= Int_val (xoff_v
);
2566 int yoff
= Int_val (yoff_v
);
2567 int noff
= Int_val (Field (li_v
, 0));
2568 char *targ
= String_val (Field (li_v
, 1));
2569 int tlen
= caml_string_length (Field (li_v
, 1));
2570 int hfsize
= Int_val (Field (li_v
, 2));
2571 char *s
= String_val (ptr_v
);
2572 int hlmask
= Int_val (hlinks_v
);
2573 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2576 /* deal with loadpage failed pages */
2580 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2581 if (trylock ("ml_postprocess")) {
2586 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2587 noff
= page
->slinkcount
;
2589 if (page
->tgen
== state
.gen
) {
2590 showsel (page
, xoff
, yoff
);
2592 unlock ("ml_postprocess");
2595 CAMLreturn (Val_int (noff
));
2598 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2602 const fz_matrix
*tctm
;
2603 fz_link
*link
, *links
;
2605 switch (page
->type
) {
2607 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2608 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2609 links
= page
->u
.pdfpage
->links
;
2613 tctm
= &fz_identity
;
2614 links
= page
->u
.xpspage
->links
;
2623 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2624 fz_invert_matrix (&ctm
, &ctm
);
2625 fz_transform_point (&p
, &ctm
);
2627 for (link
= links
; link
; link
= link
->next
) {
2628 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2629 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2637 static void ensuretext (struct page
*page
)
2639 if (state
.gen
!= page
->tgen
) {
2641 page
->tgen
= state
.gen
;
2647 page
->text
= fz_new_text_page (state
.ctx
);
2648 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2649 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2650 ctm
= pagectm (page
);
2651 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2652 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2653 qsort (page
->text
->blocks
, page
->text
->len
,
2654 sizeof (*page
->text
->blocks
), compareblocks
);
2656 fz_free_device (tdev
);
2660 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2662 CAMLparam2 (start_page_v
, dir_v
);
2664 int i
, dir
= Int_val (dir_v
);
2665 int start_page
= Int_val (start_page_v
);
2666 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2668 ret_v
= Val_int (0);
2669 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2673 lock ("ml_findpage_with_links");
2674 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2677 switch (state
.type
) {
2680 pdf_page
*page
= NULL
;
2682 fz_try (state
.ctx
) {
2683 page
= pdf_load_page (state
.u
.pdf
, i
);
2684 found
= !!page
->links
;
2686 fz_catch (state
.ctx
) {
2696 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2697 found
= !!page
->links
;
2703 ARSERT (0 && "invalid document type");
2707 ret_v
= caml_alloc_small (1, 1);
2708 Field (ret_v
, 0) = Val_int (i
);
2713 unlock ("ml_findpage_with_links");
2719 enum { dir_first
, dir_last
};
2720 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2722 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2724 CAMLparam2 (ptr_v
, dir_v
);
2725 CAMLlocal2 (ret_v
, pos_v
);
2727 int dirtag
, i
, slinkindex
;
2728 struct slink
*found
= NULL
,*slink
;
2729 char *s
= String_val (ptr_v
);
2731 page
= parse_pointer ("ml_findlink", s
);
2732 ret_v
= Val_int (0);
2733 if (trylock ("ml_findlink")) {
2737 ensureslinks (page
);
2739 if (Is_block (dir_v
)) {
2740 dirtag
= Tag_val (dir_v
);
2742 case dir_first_visible
:
2744 int x0
, y0
, dir
, first_index
, last_index
;
2746 pos_v
= Field (dir_v
, 0);
2747 x0
= Int_val (Field (pos_v
, 0));
2748 y0
= Int_val (Field (pos_v
, 1));
2749 dir
= Int_val (Field (pos_v
, 2));
2754 last_index
= page
->slinkcount
;
2757 first_index
= page
->slinkcount
- 1;
2761 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2762 slink
= &page
->slinks
[i
];
2763 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2772 slinkindex
= Int_val (Field (dir_v
, 0));
2773 found
= &page
->slinks
[slinkindex
];
2774 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2775 slink
= &page
->slinks
[i
];
2776 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2784 slinkindex
= Int_val (Field (dir_v
, 0));
2785 found
= &page
->slinks
[slinkindex
];
2786 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2787 slink
= &page
->slinks
[i
];
2788 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2796 slinkindex
= Int_val (Field (dir_v
, 0));
2797 found
= &page
->slinks
[slinkindex
];
2798 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2799 slink
= &page
->slinks
[i
];
2800 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2808 slinkindex
= Int_val (Field (dir_v
, 0));
2809 found
= &page
->slinks
[slinkindex
];
2810 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2811 slink
= &page
->slinks
[i
];
2812 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2821 dirtag
= Int_val (dir_v
);
2824 found
= page
->slinks
;
2829 found
= page
->slinks
+ (page
->slinkcount
- 1);
2835 ret_v
= caml_alloc_small (2, 1);
2836 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2839 unlock ("ml_findlink");
2844 enum { uuri
, ugoto
, utext
, uunexpected
,
2845 ulaunch
, unamed
, uremote
, uremotedest
};
2851 switch (link->dest.kind) { \
2852 case FZ_LINK_GOTO: \
2856 pageno = link->dest.ld.gotor.page; \
2860 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2861 p.y = link->dest.ld.gotor.lt.y; \
2862 fz_transform_point (&p, &pdim->lctm); \
2864 tup_v = caml_alloc_tuple (2); \
2865 ret_v = caml_alloc_small (1, ugoto); \
2866 Field (tup_v, 0) = Val_int (pageno); \
2867 Field (tup_v, 1) = Val_int (p.y); \
2868 Field (ret_v, 0) = tup_v; \
2873 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2874 ret_v = caml_alloc_small (1, uuri); \
2875 Field (ret_v, 0) = str_v; \
2878 case FZ_LINK_LAUNCH: \
2879 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2880 ret_v = caml_alloc_small (1, ulaunch); \
2881 Field (ret_v, 0) = str_v; \
2884 case FZ_LINK_NAMED: \
2885 str_v = caml_copy_string (link->dest.ld.named.named); \
2886 ret_v = caml_alloc_small (1, unamed); \
2887 Field (ret_v, 0) = str_v; \
2890 case FZ_LINK_GOTOR: \
2894 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2895 pageno = link->dest.ld.gotor.page; \
2896 if (pageno == -1) { \
2897 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2898 rty = uremotedest; \
2901 gr_v = Val_int (pageno); \
2904 tup_v = caml_alloc_tuple (2); \
2905 ret_v = caml_alloc_small (1, rty); \
2906 Field (tup_v, 0) = str_v; \
2907 Field (tup_v, 1) = gr_v; \
2908 Field (ret_v, 0) = tup_v; \
2916 snprintf (buf, sizeof (buf), \
2917 "unhandled link kind %d", link->dest.kind); \
2918 str_v = caml_copy_string (buf); \
2919 ret_v = caml_alloc_small (1, uunexpected); \
2920 Field (ret_v, 0) = str_v; \
2926 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2928 CAMLparam2 (ptr_v
, n_v
);
2929 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2932 struct pagedim
*pdim
;
2933 char *s
= String_val (ptr_v
);
2935 ret_v
= Val_int (0);
2936 if (trylock ("ml_getlink")) {
2940 page
= parse_pointer ("ml_getlink", s
);
2941 ensureslinks (page
);
2942 pdim
= &state
.pagedims
[page
->pdimno
];
2943 link
= page
->slinks
[Int_val (n_v
)].link
;
2946 unlock ("ml_getlink");
2951 CAMLprim value
ml_getlinkcount (value ptr_v
)
2955 char *s
= String_val (ptr_v
);
2957 page
= parse_pointer ("ml_getlinkcount", s
);
2958 CAMLreturn (Val_int (page
->slinkcount
));
2961 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2963 CAMLparam2 (ptr_v
, n_v
);
2966 struct slink
*slink
;
2967 char *s
= String_val (ptr_v
);
2969 page
= parse_pointer ("ml_getlinkrect", s
);
2970 ret_v
= caml_alloc_tuple (4);
2971 if (trylock ("ml_getlinkrect")) {
2972 Field (ret_v
, 0) = Val_int (0);
2973 Field (ret_v
, 1) = Val_int (0);
2974 Field (ret_v
, 2) = Val_int (0);
2975 Field (ret_v
, 3) = Val_int (0);
2978 ensureslinks (page
);
2980 slink
= &page
->slinks
[Int_val (n_v
)];
2981 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2982 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2983 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2984 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2985 unlock ("ml_getlinkrect");
2991 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2993 CAMLparam3 (ptr_v
, x_v
, y_v
);
2994 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2997 char *s
= String_val (ptr_v
);
2998 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2999 struct pagedim
*pdim
;
3001 ret_v
= Val_int (0);
3002 if (trylock ("ml_whatsunder")) {
3006 page
= parse_pointer ("ml_whatsunder", s
);
3007 pdim
= &state
.pagedims
[page
->pdimno
];
3008 x
+= pdim
->bounds
.x0
;
3009 y
+= pdim
->bounds
.y0
;
3010 link
= getlink (page
, x
, y
);
3016 fz_page_block
*pageb
;
3017 fz_text_block
*block
;
3020 for (pageb
= page
->text
->blocks
;
3021 pageb
< page
->text
->blocks
+ page
->text
->len
;
3024 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3025 block
= pageb
->u
.text
;
3028 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3031 for (line
= block
->lines
;
3032 line
< block
->lines
+ block
->len
;
3037 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3040 for (span
= line
->first_span
; span
; span
= span
->next
) {
3044 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3047 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3049 fz_text_char_bbox (&bbox
, span
, charnum
);
3052 if (x
>= b
->x0
&& x
<= b
->x1
3053 && y
>= b
->y0
&& y
<= b
->y1
) {
3054 fz_text_style
*style
= span
->text
->style
;
3056 style
->font
&& style
->font
->name
3058 : "Span has no font name"
3060 FT_FaceRec
*face
= style
->font
->ft_face
;
3061 if (face
&& face
->family_name
) {
3063 char *n1
= face
->family_name
;
3064 size_t l1
= strlen (n1
);
3065 size_t l2
= strlen (n2
);
3067 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3068 s
= malloc (l1
+ l2
+ 2);
3072 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3073 str_v
= caml_copy_string (s
);
3079 str_v
= caml_copy_string (n2
);
3081 ret_v
= caml_alloc_small (1, utext
);
3082 Field (ret_v
, 0) = str_v
;
3091 unlock ("ml_whatsunder");
3097 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3099 static int uninteresting (int c
)
3101 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3105 CAMLprim value
ml_clearmark (value ptr_v
)
3108 char *s
= String_val (ptr_v
);
3111 if (trylock ("ml_clearmark")) {
3115 page
= parse_pointer ("ml_clearmark", s
);
3116 page
->fmark
.span
= NULL
;
3117 page
->lmark
.span
= NULL
;
3121 unlock ("ml_clearmark");
3123 CAMLreturn (Val_unit
);
3126 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3128 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3133 fz_page_block
*pageb
;
3134 fz_text_block
*block
;
3135 struct pagedim
*pdim
;
3136 int mark
= Int_val (mark_v
);
3137 char *s
= String_val (ptr_v
);
3138 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3140 ret_v
= Val_bool (0);
3141 if (trylock ("ml_markunder")) {
3145 page
= parse_pointer ("ml_markunder", s
);
3146 pdim
= &state
.pagedims
[page
->pdimno
];
3150 if (mark
== mark_page
) {
3152 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3154 for (i
= 0; i
< page
->text
->len
; ++i
) {
3155 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3156 pb1
= &page
->text
->blocks
[i
];
3160 if (!pb1
) goto unlock
;
3162 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3163 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3164 pb2
= &page
->text
->blocks
[i
];
3168 if (!pb2
) goto unlock
;
3170 block
= pb1
->u
.text
;
3173 page
->fmark
.span
= block
->lines
->first_span
;
3175 block
= pb2
->u
.text
;
3176 line
= &block
->lines
[block
->len
- 1];
3177 page
->lmark
.i
= line
->last_span
->len
- 1;
3178 page
->lmark
.span
= line
->last_span
;
3179 ret_v
= Val_bool (1);
3183 x
+= pdim
->bounds
.x0
;
3184 y
+= pdim
->bounds
.y0
;
3186 for (pageb
= page
->text
->blocks
;
3187 pageb
< page
->text
->blocks
+ page
->text
->len
;
3189 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3190 block
= pageb
->u
.text
;
3193 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3196 if (mark
== mark_block
) {
3198 page
->fmark
.span
= block
->lines
->first_span
;
3200 line
= &block
->lines
[block
->len
- 1];
3201 page
->lmark
.i
= line
->last_span
->len
- 1;
3202 page
->lmark
.span
= line
->last_span
;
3203 ret_v
= Val_bool (1);
3207 for (line
= block
->lines
;
3208 line
< block
->lines
+ block
->len
;
3213 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3216 if (mark
== mark_line
) {
3218 page
->fmark
.span
= line
->first_span
;
3220 page
->lmark
.i
= line
->last_span
->len
- 1;
3221 page
->lmark
.span
= line
->last_span
;
3222 ret_v
= Val_bool (1);
3226 for (span
= line
->first_span
; span
; span
= span
->next
) {
3230 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3233 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3235 fz_text_char_bbox (&bbox
, span
, charnum
);
3238 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3240 int charnum2
, charnum3
= -1, charnum4
= -1;
3242 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3244 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3245 if (uninteresting (span
->text
[charnum2
].c
)) {
3246 charnum3
= charnum2
+ 1;
3250 if (charnum3
== -1) charnum3
= 0;
3252 for (charnum2
= charnum
+ 1;
3253 charnum2
< span
->len
;
3255 if (uninteresting (span
->text
[charnum2
].c
)) break;
3256 charnum4
= charnum2
;
3258 if (charnum4
== -1) goto unlock
;
3260 page
->fmark
.i
= charnum3
;
3261 page
->fmark
.span
= span
;
3263 page
->lmark
.i
= charnum4
;
3264 page
->lmark
.span
= span
;
3265 ret_v
= Val_bool (1);
3273 if (!Bool_val (ret_v
)) {
3274 page
->fmark
.span
= NULL
;
3275 page
->lmark
.span
= NULL
;
3279 unlock ("ml_markunder");
3285 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3287 CAMLparam3 (ptr_v
, x_v
, y_v
);
3288 CAMLlocal2 (ret_v
, res_v
);
3291 fz_page_block
*pageb
;
3292 struct pagedim
*pdim
;
3293 char *s
= String_val (ptr_v
);
3294 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3296 ret_v
= Val_int (0);
3297 if (trylock ("ml_rectofblock")) {
3301 page
= parse_pointer ("ml_rectofblock", s
);
3302 pdim
= &state
.pagedims
[page
->pdimno
];
3303 x
+= pdim
->bounds
.x0
;
3304 y
+= pdim
->bounds
.y0
;
3308 for (pageb
= page
->text
->blocks
;
3309 pageb
< page
->text
->blocks
+ page
->text
->len
;
3311 switch (pageb
->type
) {
3312 case FZ_PAGE_BLOCK_TEXT
:
3313 b
= &pageb
->u
.text
->bbox
;
3316 case FZ_PAGE_BLOCK_IMAGE
:
3317 b
= &pageb
->u
.image
->bbox
;
3324 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3329 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3330 ret_v
= caml_alloc_small (1, 1);
3331 Store_double_field (res_v
, 0, b
->x0
);
3332 Store_double_field (res_v
, 1, b
->x1
);
3333 Store_double_field (res_v
, 2, b
->y0
);
3334 Store_double_field (res_v
, 3, b
->y1
);
3335 Field (ret_v
, 0) = res_v
;
3337 unlock ("ml_rectofblock");
3343 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3345 CAMLparam2 (ptr_v
, rect_v
);
3348 struct pagedim
*pdim
;
3349 char *s
= String_val (ptr_v
);
3350 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3351 fz_page_block
*pageb
;
3352 fz_text_block
*block
;
3353 fz_text_span
*span
, *fspan
, *lspan
;
3354 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
3356 if (trylock ("ml_seltext")) {
3360 page
= parse_pointer ("ml_seltext", s
);
3363 pdim
= &state
.pagedims
[page
->pdimno
];
3364 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3365 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3366 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3367 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3378 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3379 glColor3ub (128, 128, 128);
3380 recti (x0
, y0
, x1
, y1
);
3381 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3385 fspan
= page
->fmark
.span
;
3388 lspan
= page
->lmark
.span
;
3390 for (pageb
= page
->text
->blocks
;
3391 pageb
< page
->text
->blocks
+ page
->text
->len
;
3393 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3394 block
= pageb
->u
.text
;
3395 for (line
= block
->lines
;
3396 line
< block
->lines
+ block
->len
;
3400 for (span
= line
->first_span
; span
; span
= span
->next
) {
3401 for (i
= 0; i
< span
->len
; ++i
) {
3404 fz_text_char_bbox (&b
, span
, i
);
3406 if (x0
>= b
.x0
&& x0
<= b
.x1
3407 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3413 if (x1
>= b
.x0
&& x1
<= b
.x1
3414 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3420 if (0 && selected
) {
3421 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3422 glColor3ub (128, 128, 128);
3423 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3424 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3430 if (y1
< y0
|| x1
< x0
) {
3433 if (fspan
== lspan
) {
3438 if (fline
!= lline
) {
3457 page
->fmark
.span
= fspan
;
3460 page
->lmark
.span
= lspan
;
3462 unlock ("ml_seltext");
3465 CAMLreturn (Val_unit
);
3468 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3473 for (i
= a
; i
<= b
; ++i
) {
3474 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3475 ret
= fwrite (buf
, len
, 1, f
);
3478 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3479 len
, ret
, strerror (errno
));
3487 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3489 caml_failwith ("ml_popen not implemented under Cygwin");
3492 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3494 CAMLparam2 (command_v
, fds_v
);
3495 CAMLlocal2 (l_v
, tup_v
);
3498 value earg_v
= Nothing
;
3499 posix_spawnattr_t attr
;
3500 posix_spawn_file_actions_t fa
;
3501 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3503 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3504 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3507 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3508 msg
= "posix_spawnattr_init";
3512 #ifdef POSIX_SPAWN_USEVFORK
3513 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3514 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3519 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3522 tup_v
= Field (l_v
, 0);
3523 fd1
= Int_val (Field (tup_v
, 0));
3524 fd2
= Int_val (Field (tup_v
, 1));
3526 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3527 msg
= "posix_spawn_file_actions_addclose";
3533 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3534 msg
= "posix_spawn_file_actions_adddup2";
3541 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3542 msg
= "posix_spawn";
3547 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3548 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3552 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3553 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3558 unix_error (ret
, msg
, earg_v
);
3560 CAMLreturn (Val_unit
);
3564 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
, value clear_v
)
3566 CAMLparam3 (fd_v
, ptr_v
, clear_v
);
3571 fz_page_block
*pageb
;
3572 fz_text_block
*block
;
3573 int fd
= Int_val (fd_v
);
3574 char *s
= String_val (ptr_v
);
3575 int clear
= Bool_val (clear_v
);
3577 if (trylock ("ml_copysel")) {
3581 page
= parse_pointer ("ml_sopysel", s
);
3583 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3584 fprintf (stderr
, "nothing to copy\n");
3588 f
= fdopen (fd
, "w");
3590 fprintf (stderr
, "failed to fopen sel pipe (from fd %d): %s\n",
3591 fd
, strerror (errno
));
3595 for (pageb
= page
->text
->blocks
;
3596 pageb
< page
->text
->blocks
+ page
->text
->len
;
3598 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3599 block
= pageb
->u
.text
;
3600 for (line
= block
->lines
;
3601 line
< block
->lines
+ block
->len
;
3605 for (span
= line
->first_span
; span
; span
= span
->next
) {
3608 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3609 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3610 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3613 if (pipespan (f
, span
, a
, b
)) {
3616 if (span
== line
->last_span
) {
3617 if (putc ('\n', f
) == EOF
) {
3619 "failed break line on sel pipe: %s\n",
3624 if (span
== page
->lmark
.span
) {
3633 page
->lmark
.span
= NULL
;
3634 page
->fmark
.span
= NULL
;
3639 int ret
= fclose (f
);
3642 if (errno
!= ECHILD
) {
3643 fprintf (stderr
, "failed to close sel pipe: %s\n",
3649 unlock ("ml_copysel");
3654 fprintf (stderr
, "failed to close sel pipe: %s\n",
3658 CAMLreturn (Val_unit
);
3661 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3663 CAMLparam1 (pagedimno_v
);
3665 int pagedimno
= Int_val (pagedimno_v
);
3668 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3669 if (trylock ("ml_getpdimrect")) {
3670 box
= fz_empty_rect
;
3673 box
= state
.pagedims
[pagedimno
].mediabox
;
3674 unlock ("ml_getpdimrect");
3677 Store_double_field (ret_v
, 0, box
.x0
);
3678 Store_double_field (ret_v
, 1, box
.x1
);
3679 Store_double_field (ret_v
, 2, box
.y0
);
3680 Store_double_field (ret_v
, 3, box
.y1
);
3685 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3686 value dw_v
, value cols_v
)
3688 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3694 double winw
= Int_val (winw_v
);
3695 double winh
= Int_val (winh_v
);
3696 double dw
= Int_val (dw_v
);
3697 double cols
= Int_val (cols_v
);
3698 double pw
= 1.0, ph
= 1.0;
3700 if (trylock ("ml_zoom_for_height")) {
3704 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3705 double w
= p
->pagebox
.x1
/ cols
;
3706 double h
= p
->pagebox
.y1
;
3710 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3712 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3715 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3716 unlock ("ml_zoom_for_height");
3718 ret_v
= caml_copy_double (zoom
);
3722 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3724 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3726 int pt
= Int_val(pt_v
);
3727 int x
= Int_val (x_v
);
3728 int y
= Int_val (y_v
);
3731 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3732 ret_v
= caml_copy_double (w
);
3736 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3738 CAMLparam2 (pt_v
, string_v
);
3740 int pt
= Int_val (pt_v
);
3743 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3744 ret_v
= caml_copy_double (w
);
3748 CAMLprim value
ml_getpagebox (value opaque_v
)
3750 CAMLparam1 (opaque_v
);
3756 char *s
= String_val (opaque_v
);
3757 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3759 ret_v
= caml_alloc_tuple (4);
3760 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3761 dev
->hints
|= FZ_IGNORE_SHADE
;
3763 switch (page
->type
) {
3765 ctm
= pagectm (page
);
3766 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3770 ctm
= pagectm (page
);
3771 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3775 rect
= fz_infinite_rect
;
3779 fz_free_device (dev
);
3780 fz_round_rect (&bbox
, &rect
);
3781 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3782 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3783 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3784 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3789 CAMLprim value
ml_setaalevel (value level_v
)
3791 CAMLparam1 (level_v
);
3793 state
.aalevel
= Int_val (level_v
);
3794 CAMLreturn (Val_unit
);
3798 #include <X11/Xlib.h>
3804 GLXDrawable drawable
;
3807 #include "keysym2ucs.c"
3809 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3811 CAMLparam1 (keysym_v
);
3813 KeySym keysym
= Int_val (keysym_v
);
3818 rune
= keysym2ucs (keysym
);
3819 len
= fz_runetochar (buf
, rune
);
3821 str_v
= caml_copy_string (buf
);
3825 CAMLprim value
ml_glx (value win_v
)
3828 XVisualInfo
*visual
;
3829 int screen
, wid
= Int_val (win_v
);
3830 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3832 glx
.dpy
= XOpenDisplay (NULL
);
3834 caml_failwith ("XOpenDisplay");
3837 screen
= DefaultScreen (glx
.dpy
);
3838 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3840 XCloseDisplay (glx
.dpy
);
3842 caml_failwith ("glXChooseVisual");
3845 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3848 XCloseDisplay (glx
.dpy
);
3850 caml_failwith ("glXCreateContext");
3853 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3854 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3855 XCloseDisplay (glx
.dpy
);
3858 caml_failwith ("glXMakeCurrent");
3861 CAMLreturn (Val_unit
);
3864 CAMLprim value
ml_swapb (value unit_v
)
3866 CAMLparam1 (unit_v
);
3867 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3868 CAMLreturn (Val_unit
);
3871 CAMLprim value
ml_glxsync (value unit_v
)
3873 CAMLparam1 (unit_v
);
3874 if (glx
.dpy
&& glx
.ctx
) {
3878 CAMLreturn (Val_unit
);
3881 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3882 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3884 CAMLprim value
ml_platform (value unit_v
)
3886 CAMLparam1 (unit_v
);
3887 int platid
= piunknown
;
3889 #if defined __linux__
3891 #elif defined __CYGWIN__
3893 #elif defined __DragonFly__
3894 platid
= pidragonflybsd
;
3895 #elif defined __FreeBSD__
3897 #elif defined __OpenBSD__
3899 #elif defined __NetBSD__
3901 #elif defined __sun__
3903 #elif defined __APPLE__
3906 CAMLreturn (Val_int (platid
));
3909 CAMLprim value
ml_cloexec (value fd_v
)
3912 int fd
= Int_val (fd_v
);
3914 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3915 uerror ("fcntl", Nothing
);
3917 CAMLreturn (Val_unit
);
3920 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3922 CAMLparam2 (w_v
, h_v
);
3925 int w
= Int_val (w_v
);
3926 int h
= Int_val (h_v
);
3927 int cs
= Int_val (cs_v
);
3929 if (state
.pbo_usable
) {
3930 pbo
= calloc (sizeof (*pbo
), 1);
3932 err (1, "calloc pbo");
3944 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3947 state
.glGenBuffersARB (1, &pbo
->id
);
3948 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3949 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3950 NULL
, GL_STREAM_DRAW
);
3951 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3953 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3955 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3956 state
.glDeleteBuffersARB (1, &pbo
->id
);
3958 ret_v
= caml_copy_string ("0");
3964 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3966 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3970 err (1, "malloc %d bytes failed", res
+1);
3972 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3974 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3976 ret_v
= caml_copy_string (s
);
3981 ret_v
= caml_copy_string ("0");
3986 CAMLprim value
ml_freepbo (value s_v
)
3989 char *s
= String_val (s_v
);
3990 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3993 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3995 tile
->pbo
->ptr
= NULL
;
3996 tile
->pbo
->size
= -1;
3998 CAMLreturn (Val_unit
);
4001 CAMLprim value
ml_unmappbo (value s_v
)
4004 char *s
= String_val (s_v
);
4005 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4008 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4009 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4010 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4012 tile
->pbo
->ptr
= NULL
;
4013 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4015 CAMLreturn (Val_unit
);
4018 static void setuppbo (void)
4020 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4021 state
.pbo_usable
= GGPA (glBindBufferARB
)
4022 && GGPA (glUnmapBufferARB
)
4023 && GGPA (glMapBufferARB
)
4024 && GGPA (glBufferDataARB
)
4025 && GGPA (glGenBuffersARB
)
4026 && GGPA (glDeleteBuffersARB
);
4030 CAMLprim value
ml_pbo_usable (value unit_v
)
4032 CAMLparam1 (unit_v
);
4033 CAMLreturn (Val_bool (state
.pbo_usable
));
4036 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4038 CAMLparam3 (ptr_v
, x_v
, y_v
);
4039 CAMLlocal2 (ret_v
, tup_v
);
4041 char *s
= String_val (ptr_v
);
4042 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4043 struct pagedim
*pdim
;
4047 page
= parse_pointer ("ml_unproject", s
);
4048 pdim
= &state
.pagedims
[page
->pdimno
];
4050 ret_v
= Val_int (0);
4051 if (trylock ("ml_unproject")) {
4055 switch (page
->type
) {
4057 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4066 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4067 fz_invert_matrix (&ctm
, &ctm
);
4068 fz_transform_point (&p
, &ctm
);
4070 tup_v
= caml_alloc_tuple (2);
4071 ret_v
= caml_alloc_small (1, 1);
4072 Field (tup_v
, 0) = Val_int (p
.x
);
4073 Field (tup_v
, 1) = Val_int (p
.y
);
4074 Field (ret_v
, 0) = tup_v
;
4076 unlock ("ml_unproject");
4081 static void makestippletex (void)
4083 const char pixels
[] = "\xff\xff\0\0";
4084 glGenTextures (1, &state
.stid
);
4085 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4086 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4087 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4100 CAMLprim value
ml_fz_version (value unit_v
)
4102 return caml_copy_string (FZ_VERSION
);
4105 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4107 CAMLparam2 (pipe_v
, params_v
);
4108 CAMLlocal2 (trim_v
, fuzz_v
);
4115 struct sigaction sa
;
4117 state
.cr
= Int_val (Field (pipe_v
, 0));
4118 state
.cw
= Int_val (Field (pipe_v
, 1));
4119 state
.rotate
= Int_val (Field (params_v
, 0));
4120 state
.fitmodel
= Int_val (Field (params_v
, 1));
4121 trim_v
= Field (params_v
, 2);
4122 texcount
= Int_val (Field (params_v
, 3));
4123 state
.sliceheight
= Int_val (Field (params_v
, 4));
4124 mustoresize
= Int_val (Field (params_v
, 5));
4125 colorspace
= Int_val (Field (params_v
, 6));
4126 fontpath
= String_val (Field (params_v
, 7));
4128 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4129 if (!state
.trimcachepath
) {
4130 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4133 haspboext
= Bool_val (Field (params_v
, 9));
4135 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4137 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4138 fuzz_v
= Field (trim_v
, 1);
4139 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4140 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4141 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4142 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4144 set_tex_params (colorspace
);
4147 state
.face
= load_font (fontpath
);
4151 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4153 state
.face
= load_builtin_font (base
, len
);
4155 if (!state
.face
) _exit (1);
4157 realloctexts (texcount
);
4166 sa
.sa_handler
= SIG_IGN
;
4167 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4169 sa
.sa_handler
= SIG_DFL
;
4170 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4172 if (sigemptyset (&sa
.sa_mask
)) {
4173 err (1, "sigemptyset");
4175 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4176 err (1, "sigaction");
4179 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4181 errx (1, "pthread_create: %s", strerror (ret
));
4184 CAMLreturn (Val_unit
);