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
)) {
1037 fz_intersect_rect (&mediabox
, &cropbox
);
1042 if (fz_is_empty_rect (&rootmediabox
)) {
1044 "cannot find page size for page %d\n",
1048 mediabox
= rootmediabox
;
1052 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1061 fz_try (state
.ctx
) {
1062 page
= xps_load_page (state
.u
.xps
, pageno
);
1063 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1065 if (state
.trimmargins
) {
1069 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1070 dev
->hints
|= FZ_IGNORE_SHADE
;
1071 xps_run_page (state
.u
.xps
, page
, dev
,
1072 &fz_identity
, NULL
);
1073 fz_free_device (dev
);
1075 rect
.x0
+= state
.trimfuzz
.x0
;
1076 rect
.x1
+= state
.trimfuzz
.x1
;
1077 rect
.y0
+= state
.trimfuzz
.y0
;
1078 rect
.y1
+= state
.trimfuzz
.y1
;
1079 fz_intersect_rect (&rect
, &mediabox
);
1081 if (!fz_is_empty_rect (&rect
)) {
1085 xps_free_page (state
.u
.xps
, page
);
1087 printd ("progress %f loading %d",
1088 (double) (pageno
+ 1) / state
.pagecount
,
1092 fz_catch (state
.ctx
) {
1104 if (state
.trimmargins
&& trimw
) {
1107 fz_try (state
.ctx
) {
1108 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1109 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1110 cbz_free_page (state
.u
.cbz
, page
);
1111 printd ("progress %f Trimming %d",
1112 (double) (pageno
+ 1) / state
.pagecount
,
1115 fz_catch (state
.ctx
) {
1116 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1119 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1121 err (1, "fwrite trim mediabox");
1127 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1129 err (1, "fread trim mediabox");
1135 fz_try (state
.ctx
) {
1136 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1137 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1138 cbz_free_page (state
.u
.cbz
, page
);
1140 fz_catch (state
.ctx
) {
1141 fprintf (stderr
, "failed to load cbz page\n");
1159 fz_try (state
.ctx
) {
1160 page
= image_load_page (state
.u
.img
, pageno
);
1161 image_bound_page (state
.u
.img
, page
, &mediabox
);
1162 image_free_page (state
.u
.img
, page
);
1164 fz_catch (state
.ctx
) {
1165 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1171 ARSERT (0 && state
.type
);
1174 if (state
.pagedimcount
== 0
1175 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1176 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1179 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1180 state
.pagedims
= realloc (state
.pagedims
, size
);
1181 if (!state
.pagedims
) {
1182 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1183 size
, state
.pagedimcount
+ 1);
1186 p
= &state
.pagedims
[state
.pagedimcount
++];
1188 p
->mediabox
= mediabox
;
1193 if (state
.trimmargins
) {
1194 printd ("progress 1 Trimmed %d pages in %f seconds",
1195 state
.pagecount
, end
- start
);
1198 printd ("vmsg Processed %d pages in %f seconds",
1199 state
.pagecount
, end
- start
);
1203 if (fclose (trimf
)) {
1209 static void layout (void)
1214 struct pagedim
*p
= p
;
1215 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1217 if (state
.pagedimcount
== 0) return;
1219 switch (state
.fitmodel
) {
1220 case FitProportional
:
1221 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1224 p
= &state
.pagedims
[pindex
];
1225 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1227 fz_transform_rect (&box
, &rm
);
1229 x0
= MIN (box
.x0
, box
.x1
);
1230 x1
= MAX (box
.x0
, box
.x1
);
1233 maxw
= MAX (w
, maxw
);
1234 zoom
= state
.w
/ maxw
;
1246 ARSERT (0 && state
.fitmodel
);
1249 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1253 p
= &state
.pagedims
[pindex
];
1254 fz_rotate (&ctm
, state
.rotate
);
1255 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1257 fz_transform_rect (&box
, &rm
);
1258 w
= box
.x1
- box
.x0
;
1259 switch (state
.fitmodel
) {
1260 case FitProportional
:
1261 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1267 h
= box
.y1
- box
.y0
;
1269 zoom
= MIN (zw
, zh
);
1270 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1279 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1280 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1282 fz_rotate (&rm
, p
->rotate
);
1283 p
->pagebox
= p
->mediabox
;
1284 fz_transform_rect (&p
->pagebox
, &rm
);
1285 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1286 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1290 fz_transform_rect (&rect
, &ctm
);
1291 fz_round_rect (&p
->bounds
, &rect
);
1294 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1295 fz_scale (&sm
, zoom
, -zoom
);
1296 fz_concat (&ctm
, &tm
, &sm
);
1297 fz_concat (&p
->lctm
, &ctm
, &rm
);
1303 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1304 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1305 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1306 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1310 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1311 } while (p
-- != state
.pagedims
);
1315 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1316 desttoanchor (fz_link_dest
*dest
)
1320 struct pagedim
*pdim
= state
.pagedims
;
1325 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1326 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1328 pdim
= &state
.pagedims
[i
];
1330 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1332 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1333 p
.x
= dest
->ld
.gotor
.lt
.x
;
1336 p
.y
= dest
->ld
.gotor
.lt
.y
;
1337 fz_transform_point (&p
, &pdim
->lctm
);
1341 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1342 double x0
, x1
, y0
, y1
;
1344 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1345 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1347 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1348 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1350 a
.n
= dest
->ld
.gotor
.page
;
1355 static void recurse_outline (fz_outline
*outline
, int level
)
1358 switch (outline
->dest
.kind
) {
1361 struct anchor a
= desttoanchor (&outline
->dest
);
1364 printd ("o %d %d %d %d %s",
1365 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1371 printd ("ou %d %" FMT_s
" %s %s", level
,
1372 strlen (outline
->title
), outline
->title
,
1373 outline
->dest
.ld
.uri
.uri
);
1377 printd ("on %d %s", level
, outline
->title
);
1381 printd ("emsg Unhandled outline kind %d for %s\n",
1382 outline
->dest
.kind
, outline
->title
);
1385 if (outline
->down
) {
1386 recurse_outline (outline
->down
, level
+ 1);
1388 outline
= outline
->next
;
1392 static void process_outline (void)
1394 fz_outline
*outline
;
1396 if (!state
.needoutline
) return;
1398 state
.needoutline
= 0;
1399 switch (state
.type
) {
1401 outline
= pdf_load_outline (state
.u
.pdf
);
1404 outline
= xps_load_outline (state
.u
.xps
);
1411 recurse_outline (outline
, 0);
1412 fz_free_outline (state
.ctx
, outline
);
1416 static char *strofspan (fz_text_span
*span
)
1421 size_t size
= 0, cap
= 80;
1423 p
= malloc (cap
+ 1);
1424 if (!p
) return NULL
;
1426 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1427 int n
= fz_runetochar (utf8
, ch
->c
);
1428 if (size
+ n
> cap
) {
1430 p
= realloc (p
, cap
+ 1);
1431 if (!p
) return NULL
;
1434 memcpy (p
+ size
, utf8
, n
);
1441 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1442 fz_text_span
*span
, fz_matrix ctm
,
1443 int stop
, int pageno
, double start
)
1450 fz_point p1
, p2
, p3
, p4
;
1452 p
= strofspan (span
);
1455 ret
= regexec (re
, p
, 1, &rm
, 0);
1458 if (ret
!= REG_NOMATCH
) {
1461 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1462 printd ("msg regexec error `%.*s'",
1463 (int) size
, errbuf
);
1471 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1472 c
+= fz_runelen (span
->text
[a
].c
);
1474 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1475 c
+= fz_runelen (span
->text
[b
].c
);
1478 if (fz_runelen (span
->text
[b
].c
) > 1) {
1482 fz_text_char_bbox (&sb
, span
, a
);
1483 fz_text_char_bbox (&eb
, span
, b
);
1495 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1502 printd ("progress 1 found at %d `%.*s' in %f sec",
1503 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1507 printd ("match %d %d %f %f %f %f %f %f %f %f",
1519 static int compareblocks (const void *l
, const void *r
)
1521 fz_text_block
const *ls
= l
;
1522 fz_text_block
const* rs
= r
;
1523 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1526 /* wishful thinking function */
1527 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1532 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1534 fz_text_sheet
*sheet
;
1535 struct pagedim
*pdim
, *pdimprev
;
1536 int stop
= 0, niters
= 0;
1539 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1543 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1544 if (niters
++ == 5) {
1547 printd ("progress 1 attention requested aborting search at %d",
1552 printd ("progress %f searching in page %d",
1553 (double) (pageno
+ 1) / state
.pagecount
,
1558 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1559 pdim
= &state
.pagedims
[i
];
1560 if (pdim
->pageno
== pageno
) {
1563 if (pdim
->pageno
> pageno
) {
1572 sheet
= fz_new_text_sheet (state
.ctx
);
1573 text
= fz_new_text_page (state
.ctx
);
1574 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1576 switch (state
.type
) {
1579 fz_try (state
.ctx
) {
1580 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1581 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1582 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1583 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1584 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1587 fz_catch (state
.ctx
) {
1588 fz_free_device (tdev
);
1595 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1596 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1600 ARSERT (0 && state
.type
);
1603 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1604 fz_free_device (tdev
);
1606 for (j
= 0; j
< text
->len
; ++j
) {
1609 fz_text_block
*block
;
1611 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1612 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1615 for (k
= 0; k
< block
->len
; ++k
) {
1620 line
= &block
->lines
[k
];
1621 if (line
->bbox
.y0
< y
+ 1) continue;
1624 line
= &block
->lines
[block
->len
- 1 - k
];
1625 if (line
->bbox
.y0
> y
- 1) continue;
1628 for (span
= line
->first_span
; span
; span
= span
->next
) {
1629 switch (matchspan (re
, text
, span
, ctm
,
1630 stop
, pageno
, start
)) {
1632 case 1: stop
= 1; break;
1633 case -1: stop
= 1; goto endloop
;
1648 fz_free_text_page (state
.ctx
, text
);
1649 fz_free_text_sheet (state
.ctx
, sheet
);
1651 state
.freepage (u
.ptr
);
1656 printd ("progress 1 no matches %f sec", end
- start
);
1658 printd ("clearrects");
1661 static void set_tex_params (int colorspace
)
1668 switch (colorspace
) {
1670 state
.texiform
= GL_RGBA8
;
1671 state
.texform
= GL_RGBA
;
1672 state
.texty
= GL_UNSIGNED_BYTE
;
1673 state
.colorspace
= fz_device_rgb (state
.ctx
);
1676 state
.texiform
= GL_RGBA8
;
1677 state
.texform
= GL_BGRA
;
1678 state
.texty
= endianness
.s
> 1
1679 ? GL_UNSIGNED_INT_8_8_8_8
1680 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1681 state
.colorspace
= fz_device_bgr (state
.ctx
);
1684 state
.texiform
= GL_LUMINANCE_ALPHA
;
1685 state
.texform
= GL_LUMINANCE_ALPHA
;
1686 state
.texty
= GL_UNSIGNED_BYTE
;
1687 state
.colorspace
= fz_device_gray (state
.ctx
);
1690 errx (1, "invalid colorspce %d", colorspace
);
1694 static void realloctexts (int texcount
)
1698 if (texcount
== state
.texcount
) return;
1700 if (texcount
< state
.texcount
) {
1701 glDeleteTextures (state
.texcount
- texcount
,
1702 state
.texids
+ texcount
);
1705 size
= texcount
* sizeof (*state
.texids
);
1706 state
.texids
= realloc (state
.texids
, size
);
1707 if (!state
.texids
) {
1708 err (1, "realloc texids %" FMT_s
, size
);
1711 size
= texcount
* sizeof (*state
.texowners
);
1712 state
.texowners
= realloc (state
.texowners
, size
);
1713 if (!state
.texowners
) {
1714 err (1, "realloc texowners %" FMT_s
, size
);
1716 if (texcount
> state
.texcount
) {
1719 glGenTextures (texcount
- state
.texcount
,
1720 state
.texids
+ state
.texcount
);
1721 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1722 state
.texowners
[i
].w
= -1;
1723 state
.texowners
[i
].slice
= NULL
;
1726 state
.texcount
= texcount
;
1730 static char *mbtoutf8 (char *s
)
1736 len
= mbstowcs (NULL
, s
, strlen (s
));
1741 if (len
== (size_t) -1) {
1746 tmp
= malloc (len
* sizeof (wchar_t));
1751 ret
= mbstowcs (tmp
, s
, len
);
1752 if (ret
== (size_t) -1) {
1758 for (i
= 0; i
< ret
; ++i
) {
1759 len
+= fz_runelen (tmp
[i
]);
1762 p
= r
= malloc (len
+ 1);
1768 for (i
= 0; i
< ret
; ++i
) {
1769 p
+= fz_runetochar (p
, tmp
[i
]);
1776 CAMLprim value
ml_mbtoutf8 (value s_v
)
1782 s
= String_val (s_v
);
1788 ret_v
= caml_copy_string (r
);
1794 static void * mainloop (void *unused
)
1797 int len
, ret
, oldlen
= 0;
1802 errx (1, "readlen returned 0");
1805 if (oldlen
< len
+ 1) {
1806 p
= realloc (p
, len
+ 1);
1808 err (1, "realloc %d failed", len
+ 1);
1815 if (!strncmp ("open", p
, 4)) {
1822 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1824 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1827 filename
= p
+ 5 + off
;
1828 filenamelen
= strlen (filename
);
1829 password
= filename
+ filenamelen
+ 1;
1832 openxref (filename
, password
);
1838 utf8filename
= mbtoutf8 (filename
);
1839 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1840 if (utf8filename
!= filename
) {
1841 free (utf8filename
);
1844 state
.needoutline
= 1;
1846 else if (!strncmp ("cs", p
, 2)) {
1849 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1851 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1854 set_tex_params (colorspace
);
1855 for (i
= 0; i
< state
.texcount
; ++i
) {
1856 state
.texowners
[i
].w
= -1;
1857 state
.texowners
[i
].slice
= NULL
;
1861 else if (!strncmp ("freepage", p
, 8)) {
1864 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1866 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1870 else if (!strncmp ("freetile", p
, 8)) {
1873 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1875 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1879 else if (!strncmp ("search", p
, 6)) {
1880 int icase
, pageno
, y
, ret
, len2
, forward
;
1884 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1885 &icase
, &pageno
, &y
, &forward
, &len2
);
1887 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1890 pattern
= p
+ 6 + len2
;
1891 ret
= regcomp (&re
, pattern
,
1892 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1897 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1898 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1901 search (&re
, pageno
, y
, forward
);
1905 else if (!strncmp ("geometry", p
, 8)) {
1909 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1911 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1919 for (i
= 0; i
< state
.texcount
; ++i
) {
1920 state
.texowners
[i
].slice
= NULL
;
1923 state
.fitmodel
= fitmodel
;
1928 unlock ("geometry");
1929 printd ("continue %d", state
.pagecount
);
1931 else if (!strncmp ("reqlayout", p
, 9)) {
1933 int rotate
, fitmodel
, off
, h
;
1936 ret
= sscanf (p
+ 9, " %d %d %d %n",
1937 &rotate
, &fitmodel
, &h
, &off
);
1939 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1942 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1945 state
.rotate
= rotate
;
1946 state
.fitmodel
= fitmodel
;
1951 nameddest
= p
+ 9 + off
;
1952 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1955 pdf_obj
*needle
, *obj
;
1957 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
1958 strlen (nameddest
));
1959 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1961 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
1963 a
= desttoanchor (&dest
);
1965 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1968 printd ("emsg failed to parse destination `%s'\n",
1973 printd ("emsg destination `%s' not found\n",
1976 pdf_drop_obj (needle
);
1980 unlock ("reqlayout");
1981 printd ("continue %d", state
.pagecount
);
1983 else if (!strncmp ("page", p
, 4)) {
1986 int pageno
, pindex
, ret
;
1988 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1990 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1995 page
= loadpage (pageno
, pindex
);
1999 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
2001 else if (!strncmp ("tile", p
, 4)) {
2002 int x
, y
, w
, h
, ret
;
2008 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
2009 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
2011 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2016 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2020 printd ("tile %d %d %" FMT_ptr
" %u %f",
2022 FMT_ptr_cast2 (tile
),
2023 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2026 else if (!strncmp ("settrim", p
, 7)) {
2030 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2031 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2033 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2037 state
.trimmargins
= trimmargins
;
2038 state
.needoutline
= 1;
2039 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2041 state
.trimfuzz
= fuzz
;
2043 state
.pagedimcount
= 0;
2044 free (state
.pagedims
);
2045 state
.pagedims
= NULL
;
2050 printd ("continue %d", state
.pagecount
);
2052 else if (!strncmp ("sliceh", p
, 6)) {
2055 ret
= sscanf (p
+ 6, " %d", &h
);
2057 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2059 if (h
!= state
.sliceheight
) {
2062 state
.sliceheight
= h
;
2063 for (i
= 0; i
< state
.texcount
; ++i
) {
2064 state
.texowners
[i
].w
= -1;
2065 state
.texowners
[i
].h
= -1;
2066 state
.texowners
[i
].slice
= NULL
;
2070 else if (!strncmp ("interrupt", p
, 9)) {
2071 printd ("vmsg interrupted");
2074 errx (1, "unknown command %.*s", len
, p
);
2080 CAMLprim value
ml_realloctexts (value texcount_v
)
2082 CAMLparam1 (texcount_v
);
2085 if (trylock ("ml_realloctexts")) {
2089 realloctexts (Int_val (texcount_v
));
2091 unlock ("ml_realloctexts");
2094 CAMLreturn (Val_bool (ok
));
2097 static void recti (int x0
, int y0
, int x1
, int y1
)
2099 GLfloat
*v
= state
.vertices
;
2101 glVertexPointer (2, GL_FLOAT
, 0, v
);
2102 v
[0] = x0
; v
[1] = y0
;
2103 v
[2] = x1
; v
[3] = y0
;
2104 v
[4] = x0
; v
[5] = y1
;
2105 v
[6] = x1
; v
[7] = y1
;
2106 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2109 static void showsel (struct page
*page
, int ox
, int oy
)
2115 fz_page_block
*pageb
;
2116 fz_text_block
*block
;
2117 struct mark first
, last
;
2119 first
= page
->fmark
;
2122 if (!first
.span
|| !last
.span
) return;
2124 glEnable (GL_BLEND
);
2125 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2126 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
2128 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2129 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2130 for (pageb
= page
->text
->blocks
;
2131 pageb
< page
->text
->blocks
+ page
->text
->len
;
2133 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2134 block
= pageb
->u
.text
;
2136 for (line
= block
->lines
;
2137 line
< block
->lines
+ block
->len
;
2140 rect
= fz_empty_rect
;
2142 for (span
= line
->first_span
; span
; span
= span
->next
) {
2144 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2149 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2151 j
= MIN (first
.i
, last
.i
);
2152 k
= MAX (first
.i
, last
.i
);
2154 else if (span
== first
.span
) {
2158 else if (span
== last
.span
) {
2164 for (i
= j
; i
<= k
; ++i
) {
2166 fz_union_rect (&rect
,
2167 fz_text_char_bbox (&bbox
, span
, i
));
2169 fz_round_rect (&bbox
, &rect
);
2170 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2177 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2178 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2179 if (span
== last
.span
) {
2182 rect
= fz_empty_rect
;
2188 glDisable (GL_BLEND
);
2193 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2195 fz_matrix ctm
, tm
, pm
;
2196 fz_link
*link
, *links
;
2197 GLfloat
*texcoords
= state
.texcoords
;
2198 GLfloat
*vertices
= state
.vertices
;
2200 switch (page
->type
) {
2202 links
= page
->u
.pdfpage
->links
;
2206 links
= page
->u
.xpspage
->links
;
2213 glEnable (GL_TEXTURE_1D
);
2214 glEnable (GL_BLEND
);
2215 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2216 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2218 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2219 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2220 fz_translate (&tm
, xoff
, yoff
);
2221 pm
= pagectm (page
);
2222 fz_concat (&ctm
, &pm
, &tm
);
2224 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2225 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2227 for (link
= links
; link
; link
= link
->next
) {
2228 fz_point p1
, p2
, p3
, p4
;
2230 p1
.x
= link
->rect
.x0
;
2231 p1
.y
= link
->rect
.y0
;
2233 p2
.x
= link
->rect
.x1
;
2234 p2
.y
= link
->rect
.y0
;
2236 p3
.x
= link
->rect
.x1
;
2237 p3
.y
= link
->rect
.y1
;
2239 p4
.x
= link
->rect
.x0
;
2240 p4
.y
= link
->rect
.y1
;
2242 fz_transform_point (&p1
, &ctm
);
2243 fz_transform_point (&p2
, &ctm
);
2244 fz_transform_point (&p3
, &ctm
);
2245 fz_transform_point (&p4
, &ctm
);
2247 switch (link
->dest
.kind
) {
2248 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2249 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2250 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2251 default: glColor3ub (0, 0, 0); break;
2259 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2263 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2265 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2266 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2268 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2269 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2271 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2272 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2274 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2275 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2277 glDrawArrays (GL_LINES
, 0, 8);
2280 glDisable (GL_BLEND
);
2281 glDisable (GL_TEXTURE_1D
);
2284 static int compareslinks (const void *l
, const void *r
)
2286 struct slink
const *ls
= l
;
2287 struct slink
const *rs
= r
;
2288 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2289 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2291 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2294 static void droptext (struct page
*page
)
2297 fz_free_text_page (state
.ctx
, page
->text
);
2300 page
->fmark
.span
= NULL
;
2301 page
->lmark
.span
= NULL
;
2305 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2310 static void dropslinks (struct page
*page
)
2313 free (page
->slinks
);
2314 page
->slinks
= NULL
;
2315 page
->slinkcount
= 0;
2319 static void ensureslinks (struct page
*page
)
2323 size_t slinksize
= sizeof (*page
->slinks
);
2324 fz_link
*link
, *links
;
2326 if (state
.gen
!= page
->sgen
) {
2328 page
->sgen
= state
.gen
;
2330 if (page
->slinks
) return;
2332 switch (page
->type
) {
2334 links
= page
->u
.pdfpage
->links
;
2335 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2337 &state
.pagedims
[page
->pdimno
].tctm
,
2338 &state
.pagedims
[page
->pdimno
].ctm
);
2342 links
= page
->u
.xpspage
->links
;
2343 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2350 for (link
= links
; link
; link
= link
->next
) {
2354 page
->slinkcount
= count
;
2355 page
->slinks
= calloc (count
, slinksize
);
2356 if (!page
->slinks
) {
2357 err (1, "realloc slinks %d", count
);
2360 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2364 fz_transform_rect (&rect
, &ctm
);
2365 page
->slinks
[i
].link
= link
;
2366 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2368 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2372 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2373 static void fmt_linkn (char *s
, unsigned int u
)
2375 unsigned int len
; unsigned int q
;
2376 int zma
= 'z' - 'a' + 1;
2378 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2381 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2382 /* handles u == 0 */
2387 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2388 int noff
, char *targ
, int tlen
, int hfsize
)
2392 struct slink
*slink
;
2393 double x0
, y0
, x1
, y1
, w
;
2395 ensureslinks (page
);
2396 glColor3ub (0xc3, 0xb0, 0x91);
2397 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2398 fmt_linkn (buf
, i
+ noff
);
2399 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2400 slink
= &page
->slinks
[i
];
2402 x0
= slink
->bbox
.x0
+ xoff
- 5;
2403 y1
= slink
->bbox
.y0
+ yoff
- 5;
2404 y0
= y1
+ 10 + hfsize
;
2405 w
= measure_string (state
.face
, hfsize
, buf
);
2407 recti (x0
, y0
, x1
, y1
);
2411 glEnable (GL_BLEND
);
2412 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2413 glEnable (GL_TEXTURE_2D
);
2414 glColor3ub (0, 0, 0);
2415 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2416 fmt_linkn (buf
, i
+ noff
);
2417 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2418 slink
= &page
->slinks
[i
];
2420 x0
= slink
->bbox
.x0
+ xoff
;
2421 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2422 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2425 glDisable (GL_TEXTURE_2D
);
2426 glDisable (GL_BLEND
);
2430 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2433 struct slice
*slice1
;
2434 unsigned char *texdata
;
2437 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2438 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2440 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2441 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2442 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2446 int texindex
= state
.texindex
++ % state
.texcount
;
2448 if (state
.texowners
[texindex
].w
== tile
->w
) {
2449 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2453 state
.texowners
[texindex
].h
= slice
->h
;
2457 state
.texowners
[texindex
].h
= slice
->h
;
2460 state
.texowners
[texindex
].w
= tile
->w
;
2461 state
.texowners
[texindex
].slice
= slice
;
2462 slice
->texindex
= texindex
;
2464 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2466 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2470 texdata
= tile
->pixmap
->samples
;
2473 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2485 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2497 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2502 CAMLprim value
ml_begintiles (value unit_v
)
2504 CAMLparam1 (unit_v
);
2505 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2506 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2507 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2508 CAMLreturn (unit_v
);
2511 CAMLprim value
ml_endtiles (value unit_v
)
2513 CAMLparam1 (unit_v
);
2514 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2515 CAMLreturn (unit_v
);
2518 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2520 CAMLparam2 (args_v
, ptr_v
);
2521 int dispx
= Int_val (Field (args_v
, 0));
2522 int dispy
= Int_val (Field (args_v
, 1));
2523 int dispw
= Int_val (Field (args_v
, 2));
2524 int disph
= Int_val (Field (args_v
, 3));
2525 int tilex
= Int_val (Field (args_v
, 4));
2526 int tiley
= Int_val (Field (args_v
, 5));
2527 char *s
= String_val (ptr_v
);
2528 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2529 int slicey
, firstslice
;
2530 struct slice
*slice
;
2531 GLfloat
*texcoords
= state
.texcoords
;
2532 GLfloat
*vertices
= state
.vertices
;
2534 firstslice
= tiley
/ tile
->sliceheight
;
2535 slice
= &tile
->slices
[firstslice
];
2536 slicey
= tiley
% tile
->sliceheight
;
2541 dh
= slice
->h
- slicey
;
2542 dh
= MIN (disph
, dh
);
2543 uploadslice (tile
, slice
);
2545 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2546 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2547 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2548 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2550 vertices
[0] = dispx
; vertices
[1] = dispy
;
2551 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2552 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2553 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2555 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2559 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2562 CAMLreturn (Val_unit
);
2565 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2566 value xoff_v
, value yoff_v
,
2569 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2570 int xoff
= Int_val (xoff_v
);
2571 int yoff
= Int_val (yoff_v
);
2572 int noff
= Int_val (Field (li_v
, 0));
2573 char *targ
= String_val (Field (li_v
, 1));
2574 int tlen
= caml_string_length (Field (li_v
, 1));
2575 int hfsize
= Int_val (Field (li_v
, 2));
2576 char *s
= String_val (ptr_v
);
2577 int hlmask
= Int_val (hlinks_v
);
2578 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2581 /* deal with loadpage failed pages */
2585 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2586 if (trylock ("ml_postprocess")) {
2591 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2592 noff
= page
->slinkcount
;
2594 if (page
->tgen
== state
.gen
) {
2595 showsel (page
, xoff
, yoff
);
2597 unlock ("ml_postprocess");
2600 CAMLreturn (Val_int (noff
));
2603 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2607 const fz_matrix
*tctm
;
2608 fz_link
*link
, *links
;
2610 switch (page
->type
) {
2612 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2613 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2614 links
= page
->u
.pdfpage
->links
;
2618 tctm
= &fz_identity
;
2619 links
= page
->u
.xpspage
->links
;
2628 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2629 fz_invert_matrix (&ctm
, &ctm
);
2630 fz_transform_point (&p
, &ctm
);
2632 for (link
= links
; link
; link
= link
->next
) {
2633 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2634 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2642 static void ensuretext (struct page
*page
)
2644 if (state
.gen
!= page
->tgen
) {
2646 page
->tgen
= state
.gen
;
2652 page
->text
= fz_new_text_page (state
.ctx
);
2653 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2654 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2655 ctm
= pagectm (page
);
2656 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2657 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2658 qsort (page
->text
->blocks
, page
->text
->len
,
2659 sizeof (*page
->text
->blocks
), compareblocks
);
2661 fz_free_device (tdev
);
2665 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2667 CAMLparam2 (start_page_v
, dir_v
);
2669 int i
, dir
= Int_val (dir_v
);
2670 int start_page
= Int_val (start_page_v
);
2671 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2673 ret_v
= Val_int (0);
2674 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2678 lock ("ml_findpage_with_links");
2679 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2682 switch (state
.type
) {
2685 pdf_page
*page
= NULL
;
2687 fz_try (state
.ctx
) {
2688 page
= pdf_load_page (state
.u
.pdf
, i
);
2689 found
= !!page
->links
;
2691 fz_catch (state
.ctx
) {
2701 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2702 found
= !!page
->links
;
2708 ARSERT (0 && "invalid document type");
2712 ret_v
= caml_alloc_small (1, 1);
2713 Field (ret_v
, 0) = Val_int (i
);
2718 unlock ("ml_findpage_with_links");
2724 enum { dir_first
, dir_last
};
2725 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2727 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2729 CAMLparam2 (ptr_v
, dir_v
);
2730 CAMLlocal2 (ret_v
, pos_v
);
2732 int dirtag
, i
, slinkindex
;
2733 struct slink
*found
= NULL
,*slink
;
2734 char *s
= String_val (ptr_v
);
2736 page
= parse_pointer ("ml_findlink", s
);
2737 ret_v
= Val_int (0);
2738 if (trylock ("ml_findlink")) {
2742 ensureslinks (page
);
2744 if (Is_block (dir_v
)) {
2745 dirtag
= Tag_val (dir_v
);
2747 case dir_first_visible
:
2749 int x0
, y0
, dir
, first_index
, last_index
;
2751 pos_v
= Field (dir_v
, 0);
2752 x0
= Int_val (Field (pos_v
, 0));
2753 y0
= Int_val (Field (pos_v
, 1));
2754 dir
= Int_val (Field (pos_v
, 2));
2759 last_index
= page
->slinkcount
;
2762 first_index
= page
->slinkcount
- 1;
2766 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2767 slink
= &page
->slinks
[i
];
2768 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2777 slinkindex
= Int_val (Field (dir_v
, 0));
2778 found
= &page
->slinks
[slinkindex
];
2779 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2780 slink
= &page
->slinks
[i
];
2781 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2789 slinkindex
= Int_val (Field (dir_v
, 0));
2790 found
= &page
->slinks
[slinkindex
];
2791 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2792 slink
= &page
->slinks
[i
];
2793 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2801 slinkindex
= Int_val (Field (dir_v
, 0));
2802 found
= &page
->slinks
[slinkindex
];
2803 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2804 slink
= &page
->slinks
[i
];
2805 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2813 slinkindex
= Int_val (Field (dir_v
, 0));
2814 found
= &page
->slinks
[slinkindex
];
2815 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2816 slink
= &page
->slinks
[i
];
2817 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2826 dirtag
= Int_val (dir_v
);
2829 found
= page
->slinks
;
2834 found
= page
->slinks
+ (page
->slinkcount
- 1);
2840 ret_v
= caml_alloc_small (2, 1);
2841 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2844 unlock ("ml_findlink");
2849 enum { uuri
, ugoto
, utext
, uunexpected
,
2850 ulaunch
, unamed
, uremote
, uremotedest
};
2856 switch (link->dest.kind) { \
2857 case FZ_LINK_GOTO: \
2861 pageno = link->dest.ld.gotor.page; \
2865 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2866 p.y = link->dest.ld.gotor.lt.y; \
2867 fz_transform_point (&p, &pdim->lctm); \
2869 tup_v = caml_alloc_tuple (2); \
2870 ret_v = caml_alloc_small (1, ugoto); \
2871 Field (tup_v, 0) = Val_int (pageno); \
2872 Field (tup_v, 1) = Val_int (p.y); \
2873 Field (ret_v, 0) = tup_v; \
2878 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2879 ret_v = caml_alloc_small (1, uuri); \
2880 Field (ret_v, 0) = str_v; \
2883 case FZ_LINK_LAUNCH: \
2884 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2885 ret_v = caml_alloc_small (1, ulaunch); \
2886 Field (ret_v, 0) = str_v; \
2889 case FZ_LINK_NAMED: \
2890 str_v = caml_copy_string (link->dest.ld.named.named); \
2891 ret_v = caml_alloc_small (1, unamed); \
2892 Field (ret_v, 0) = str_v; \
2895 case FZ_LINK_GOTOR: \
2899 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2900 pageno = link->dest.ld.gotor.page; \
2901 if (pageno == -1) { \
2902 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2903 rty = uremotedest; \
2906 gr_v = Val_int (pageno); \
2909 tup_v = caml_alloc_tuple (2); \
2910 ret_v = caml_alloc_small (1, rty); \
2911 Field (tup_v, 0) = str_v; \
2912 Field (tup_v, 1) = gr_v; \
2913 Field (ret_v, 0) = tup_v; \
2921 snprintf (buf, sizeof (buf), \
2922 "unhandled link kind %d", link->dest.kind); \
2923 str_v = caml_copy_string (buf); \
2924 ret_v = caml_alloc_small (1, uunexpected); \
2925 Field (ret_v, 0) = str_v; \
2931 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2933 CAMLparam2 (ptr_v
, n_v
);
2934 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2937 struct pagedim
*pdim
;
2938 char *s
= String_val (ptr_v
);
2940 ret_v
= Val_int (0);
2941 if (trylock ("ml_getlink")) {
2945 page
= parse_pointer ("ml_getlink", s
);
2946 ensureslinks (page
);
2947 pdim
= &state
.pagedims
[page
->pdimno
];
2948 link
= page
->slinks
[Int_val (n_v
)].link
;
2951 unlock ("ml_getlink");
2956 CAMLprim value
ml_getlinkcount (value ptr_v
)
2960 char *s
= String_val (ptr_v
);
2962 page
= parse_pointer ("ml_getlinkcount", s
);
2963 CAMLreturn (Val_int (page
->slinkcount
));
2966 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2968 CAMLparam2 (ptr_v
, n_v
);
2971 struct slink
*slink
;
2972 char *s
= String_val (ptr_v
);
2974 page
= parse_pointer ("ml_getlinkrect", s
);
2975 ret_v
= caml_alloc_tuple (4);
2976 if (trylock ("ml_getlinkrect")) {
2977 Field (ret_v
, 0) = Val_int (0);
2978 Field (ret_v
, 1) = Val_int (0);
2979 Field (ret_v
, 2) = Val_int (0);
2980 Field (ret_v
, 3) = Val_int (0);
2983 ensureslinks (page
);
2985 slink
= &page
->slinks
[Int_val (n_v
)];
2986 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2987 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2988 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2989 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2990 unlock ("ml_getlinkrect");
2996 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2998 CAMLparam3 (ptr_v
, x_v
, y_v
);
2999 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3002 char *s
= String_val (ptr_v
);
3003 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3004 struct pagedim
*pdim
;
3006 ret_v
= Val_int (0);
3007 if (trylock ("ml_whatsunder")) {
3011 page
= parse_pointer ("ml_whatsunder", s
);
3012 pdim
= &state
.pagedims
[page
->pdimno
];
3013 x
+= pdim
->bounds
.x0
;
3014 y
+= pdim
->bounds
.y0
;
3015 link
= getlink (page
, x
, y
);
3021 fz_page_block
*pageb
;
3022 fz_text_block
*block
;
3025 for (pageb
= page
->text
->blocks
;
3026 pageb
< page
->text
->blocks
+ page
->text
->len
;
3029 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3030 block
= pageb
->u
.text
;
3033 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3036 for (line
= block
->lines
;
3037 line
< block
->lines
+ block
->len
;
3042 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3045 for (span
= line
->first_span
; span
; span
= span
->next
) {
3049 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3052 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3054 fz_text_char_bbox (&bbox
, span
, charnum
);
3057 if (x
>= b
->x0
&& x
<= b
->x1
3058 && y
>= b
->y0
&& y
<= b
->y1
) {
3059 fz_text_style
*style
= span
->text
->style
;
3061 style
->font
&& style
->font
->name
3063 : "Span has no font name"
3065 FT_FaceRec
*face
= style
->font
->ft_face
;
3066 if (face
&& face
->family_name
) {
3068 char *n1
= face
->family_name
;
3069 size_t l1
= strlen (n1
);
3070 size_t l2
= strlen (n2
);
3072 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3073 s
= malloc (l1
+ l2
+ 2);
3077 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3078 str_v
= caml_copy_string (s
);
3084 str_v
= caml_copy_string (n2
);
3086 ret_v
= caml_alloc_small (1, utext
);
3087 Field (ret_v
, 0) = str_v
;
3096 unlock ("ml_whatsunder");
3102 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3104 static int uninteresting (int c
)
3106 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3110 CAMLprim value
ml_clearmark (value ptr_v
)
3113 char *s
= String_val (ptr_v
);
3116 if (trylock ("ml_clearmark")) {
3120 page
= parse_pointer ("ml_clearmark", s
);
3121 page
->fmark
.span
= NULL
;
3122 page
->lmark
.span
= NULL
;
3126 unlock ("ml_clearmark");
3128 CAMLreturn (Val_unit
);
3131 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3133 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3138 fz_page_block
*pageb
;
3139 fz_text_block
*block
;
3140 struct pagedim
*pdim
;
3141 int mark
= Int_val (mark_v
);
3142 char *s
= String_val (ptr_v
);
3143 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3145 ret_v
= Val_bool (0);
3146 if (trylock ("ml_markunder")) {
3150 page
= parse_pointer ("ml_markunder", s
);
3151 pdim
= &state
.pagedims
[page
->pdimno
];
3155 if (mark
== mark_page
) {
3157 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3159 for (i
= 0; i
< page
->text
->len
; ++i
) {
3160 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3161 pb1
= &page
->text
->blocks
[i
];
3165 if (!pb1
) goto unlock
;
3167 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3168 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3169 pb2
= &page
->text
->blocks
[i
];
3173 if (!pb2
) goto unlock
;
3175 block
= pb1
->u
.text
;
3178 page
->fmark
.span
= block
->lines
->first_span
;
3180 block
= pb2
->u
.text
;
3181 line
= &block
->lines
[block
->len
- 1];
3182 page
->lmark
.i
= line
->last_span
->len
- 1;
3183 page
->lmark
.span
= line
->last_span
;
3184 ret_v
= Val_bool (1);
3188 x
+= pdim
->bounds
.x0
;
3189 y
+= pdim
->bounds
.y0
;
3191 for (pageb
= page
->text
->blocks
;
3192 pageb
< page
->text
->blocks
+ page
->text
->len
;
3194 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3195 block
= pageb
->u
.text
;
3198 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3201 if (mark
== mark_block
) {
3203 page
->fmark
.span
= block
->lines
->first_span
;
3205 line
= &block
->lines
[block
->len
- 1];
3206 page
->lmark
.i
= line
->last_span
->len
- 1;
3207 page
->lmark
.span
= line
->last_span
;
3208 ret_v
= Val_bool (1);
3212 for (line
= block
->lines
;
3213 line
< block
->lines
+ block
->len
;
3218 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3221 if (mark
== mark_line
) {
3223 page
->fmark
.span
= line
->first_span
;
3225 page
->lmark
.i
= line
->last_span
->len
- 1;
3226 page
->lmark
.span
= line
->last_span
;
3227 ret_v
= Val_bool (1);
3231 for (span
= line
->first_span
; span
; span
= span
->next
) {
3235 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3238 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3240 fz_text_char_bbox (&bbox
, span
, charnum
);
3243 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3245 int charnum2
, charnum3
= -1, charnum4
= -1;
3247 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3249 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3250 if (uninteresting (span
->text
[charnum2
].c
)) {
3251 charnum3
= charnum2
+ 1;
3255 if (charnum3
== -1) charnum3
= 0;
3257 for (charnum2
= charnum
+ 1;
3258 charnum2
< span
->len
;
3260 if (uninteresting (span
->text
[charnum2
].c
)) break;
3261 charnum4
= charnum2
;
3263 if (charnum4
== -1) goto unlock
;
3265 page
->fmark
.i
= charnum3
;
3266 page
->fmark
.span
= span
;
3268 page
->lmark
.i
= charnum4
;
3269 page
->lmark
.span
= span
;
3270 ret_v
= Val_bool (1);
3278 if (!Bool_val (ret_v
)) {
3279 page
->fmark
.span
= NULL
;
3280 page
->lmark
.span
= NULL
;
3284 unlock ("ml_markunder");
3290 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3292 CAMLparam3 (ptr_v
, x_v
, y_v
);
3293 CAMLlocal2 (ret_v
, res_v
);
3296 fz_page_block
*pageb
;
3297 struct pagedim
*pdim
;
3298 char *s
= String_val (ptr_v
);
3299 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3301 ret_v
= Val_int (0);
3302 if (trylock ("ml_rectofblock")) {
3306 page
= parse_pointer ("ml_rectofblock", s
);
3307 pdim
= &state
.pagedims
[page
->pdimno
];
3308 x
+= pdim
->bounds
.x0
;
3309 y
+= pdim
->bounds
.y0
;
3313 for (pageb
= page
->text
->blocks
;
3314 pageb
< page
->text
->blocks
+ page
->text
->len
;
3316 switch (pageb
->type
) {
3317 case FZ_PAGE_BLOCK_TEXT
:
3318 b
= &pageb
->u
.text
->bbox
;
3321 case FZ_PAGE_BLOCK_IMAGE
:
3322 b
= &pageb
->u
.image
->bbox
;
3329 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3334 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3335 ret_v
= caml_alloc_small (1, 1);
3336 Store_double_field (res_v
, 0, b
->x0
);
3337 Store_double_field (res_v
, 1, b
->x1
);
3338 Store_double_field (res_v
, 2, b
->y0
);
3339 Store_double_field (res_v
, 3, b
->y1
);
3340 Field (ret_v
, 0) = res_v
;
3342 unlock ("ml_rectofblock");
3348 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3350 CAMLparam2 (ptr_v
, rect_v
);
3353 struct pagedim
*pdim
;
3354 char *s
= String_val (ptr_v
);
3355 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3356 fz_page_block
*pageb
;
3357 fz_text_block
*block
;
3358 fz_text_span
*span
, *fspan
, *lspan
;
3359 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
3361 if (trylock ("ml_seltext")) {
3365 page
= parse_pointer ("ml_seltext", s
);
3368 pdim
= &state
.pagedims
[page
->pdimno
];
3369 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3370 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3371 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3372 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3383 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3384 glColor3ub (128, 128, 128);
3385 recti (x0
, y0
, x1
, y1
);
3386 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3390 fspan
= page
->fmark
.span
;
3393 lspan
= page
->lmark
.span
;
3395 for (pageb
= page
->text
->blocks
;
3396 pageb
< page
->text
->blocks
+ page
->text
->len
;
3398 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3399 block
= pageb
->u
.text
;
3400 for (line
= block
->lines
;
3401 line
< block
->lines
+ block
->len
;
3405 for (span
= line
->first_span
; span
; span
= span
->next
) {
3406 for (i
= 0; i
< span
->len
; ++i
) {
3409 fz_text_char_bbox (&b
, span
, i
);
3411 if (x0
>= b
.x0
&& x0
<= b
.x1
3412 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3418 if (x1
>= b
.x0
&& x1
<= b
.x1
3419 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3425 if (0 && selected
) {
3426 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3427 glColor3ub (128, 128, 128);
3428 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3429 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3435 if (y1
< y0
|| x1
< x0
) {
3438 if (fspan
== lspan
) {
3443 if (fline
!= lline
) {
3462 page
->fmark
.span
= fspan
;
3465 page
->lmark
.span
= lspan
;
3467 unlock ("ml_seltext");
3470 CAMLreturn (Val_unit
);
3473 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3478 for (i
= a
; i
<= b
; ++i
) {
3479 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3480 ret
= fwrite (buf
, len
, 1, f
);
3483 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3484 len
, ret
, strerror (errno
));
3492 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3494 caml_failwith ("ml_popen not implemented under Cygwin");
3497 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3499 CAMLparam2 (command_v
, fds_v
);
3500 CAMLlocal2 (l_v
, tup_v
);
3503 value earg_v
= Nothing
;
3504 posix_spawnattr_t attr
;
3505 posix_spawn_file_actions_t fa
;
3506 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3508 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3509 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3512 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3513 msg
= "posix_spawnattr_init";
3517 #ifdef POSIX_SPAWN_USEVFORK
3518 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3519 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3524 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3527 tup_v
= Field (l_v
, 0);
3528 fd1
= Int_val (Field (tup_v
, 0));
3529 fd2
= Int_val (Field (tup_v
, 1));
3531 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3532 msg
= "posix_spawn_file_actions_addclose";
3538 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3539 msg
= "posix_spawn_file_actions_adddup2";
3546 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3547 msg
= "posix_spawn";
3552 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3553 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3557 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3558 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3563 unix_error (ret
, msg
, earg_v
);
3565 CAMLreturn (Val_unit
);
3569 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3571 CAMLparam2 (fd_v
, ptr_v
);
3576 fz_page_block
*pageb
;
3577 fz_text_block
*block
;
3578 int fd
= Int_val (fd_v
);
3579 char *s
= String_val (ptr_v
);
3581 if (trylock ("ml_copysel")) {
3585 page
= parse_pointer ("ml_copysel", s
);
3587 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3588 fprintf (stderr
, "nothing to copy\n");
3592 f
= fdopen (fd
, "w");
3594 fprintf (stderr
, "failed to fopen sel pipe (from fd %d): %s\n",
3595 fd
, strerror (errno
));
3599 for (pageb
= page
->text
->blocks
;
3600 pageb
< page
->text
->blocks
+ page
->text
->len
;
3602 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3603 block
= pageb
->u
.text
;
3604 for (line
= block
->lines
;
3605 line
< block
->lines
+ block
->len
;
3609 for (span
= line
->first_span
; span
; span
= span
->next
) {
3612 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3613 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3614 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3617 if (pipespan (f
, span
, a
, b
)) {
3620 if (span
== page
->lmark
.span
) {
3623 if (span
== line
->last_span
) {
3624 if (putc ('\n', f
) == EOF
) {
3626 "failed break line on sel pipe: %s\n",
3637 int ret
= fclose (f
);
3640 if (errno
!= ECHILD
) {
3641 fprintf (stderr
, "failed to close sel pipe: %s\n",
3647 unlock ("ml_copysel");
3652 fprintf (stderr
, "failed to close sel pipe: %s\n",
3656 CAMLreturn (Val_unit
);
3659 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3661 CAMLparam1 (pagedimno_v
);
3663 int pagedimno
= Int_val (pagedimno_v
);
3666 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3667 if (trylock ("ml_getpdimrect")) {
3668 box
= fz_empty_rect
;
3671 box
= state
.pagedims
[pagedimno
].mediabox
;
3672 unlock ("ml_getpdimrect");
3675 Store_double_field (ret_v
, 0, box
.x0
);
3676 Store_double_field (ret_v
, 1, box
.x1
);
3677 Store_double_field (ret_v
, 2, box
.y0
);
3678 Store_double_field (ret_v
, 3, box
.y1
);
3683 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3684 value dw_v
, value cols_v
)
3686 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3692 double winw
= Int_val (winw_v
);
3693 double winh
= Int_val (winh_v
);
3694 double dw
= Int_val (dw_v
);
3695 double cols
= Int_val (cols_v
);
3696 double pw
= 1.0, ph
= 1.0;
3698 if (trylock ("ml_zoom_for_height")) {
3702 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3703 double w
= p
->pagebox
.x1
/ cols
;
3704 double h
= p
->pagebox
.y1
;
3708 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3710 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3713 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3714 unlock ("ml_zoom_for_height");
3716 ret_v
= caml_copy_double (zoom
);
3720 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3722 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3724 int pt
= Int_val(pt_v
);
3725 int x
= Int_val (x_v
);
3726 int y
= Int_val (y_v
);
3729 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3730 ret_v
= caml_copy_double (w
);
3734 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3736 CAMLparam2 (pt_v
, string_v
);
3738 int pt
= Int_val (pt_v
);
3741 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3742 ret_v
= caml_copy_double (w
);
3746 CAMLprim value
ml_getpagebox (value opaque_v
)
3748 CAMLparam1 (opaque_v
);
3754 char *s
= String_val (opaque_v
);
3755 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3757 ret_v
= caml_alloc_tuple (4);
3758 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3759 dev
->hints
|= FZ_IGNORE_SHADE
;
3761 switch (page
->type
) {
3763 ctm
= pagectm (page
);
3764 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3768 ctm
= pagectm (page
);
3769 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3773 rect
= fz_infinite_rect
;
3777 fz_free_device (dev
);
3778 fz_round_rect (&bbox
, &rect
);
3779 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3780 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3781 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3782 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3787 CAMLprim value
ml_setaalevel (value level_v
)
3789 CAMLparam1 (level_v
);
3791 state
.aalevel
= Int_val (level_v
);
3792 CAMLreturn (Val_unit
);
3796 #include <X11/Xlib.h>
3802 GLXDrawable drawable
;
3805 #include "keysym2ucs.c"
3807 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3809 CAMLparam1 (keysym_v
);
3811 KeySym keysym
= Int_val (keysym_v
);
3816 rune
= keysym2ucs (keysym
);
3817 len
= fz_runetochar (buf
, rune
);
3819 str_v
= caml_copy_string (buf
);
3823 CAMLprim value
ml_glx (value win_v
)
3826 XVisualInfo
*visual
;
3827 int screen
, wid
= Int_val (win_v
);
3828 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3830 glx
.dpy
= XOpenDisplay (NULL
);
3832 caml_failwith ("XOpenDisplay");
3835 screen
= DefaultScreen (glx
.dpy
);
3836 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3838 XCloseDisplay (glx
.dpy
);
3840 caml_failwith ("glXChooseVisual");
3843 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3846 XCloseDisplay (glx
.dpy
);
3848 caml_failwith ("glXCreateContext");
3851 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3852 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3853 XCloseDisplay (glx
.dpy
);
3856 caml_failwith ("glXMakeCurrent");
3859 CAMLreturn (Val_unit
);
3862 CAMLprim value
ml_swapb (value unit_v
)
3864 CAMLparam1 (unit_v
);
3865 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3866 CAMLreturn (Val_unit
);
3869 CAMLprim value
ml_glxsync (value unit_v
)
3871 CAMLparam1 (unit_v
);
3872 if (glx
.dpy
&& glx
.ctx
) {
3876 CAMLreturn (Val_unit
);
3879 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3880 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3882 CAMLprim value
ml_platform (value unit_v
)
3884 CAMLparam1 (unit_v
);
3885 int platid
= piunknown
;
3887 #if defined __linux__
3889 #elif defined __CYGWIN__
3891 #elif defined __DragonFly__
3892 platid
= pidragonflybsd
;
3893 #elif defined __FreeBSD__
3895 #elif defined __OpenBSD__
3897 #elif defined __NetBSD__
3899 #elif defined __sun__
3901 #elif defined __APPLE__
3904 CAMLreturn (Val_int (platid
));
3907 CAMLprim value
ml_cloexec (value fd_v
)
3910 int fd
= Int_val (fd_v
);
3912 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3913 uerror ("fcntl", Nothing
);
3915 CAMLreturn (Val_unit
);
3918 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3920 CAMLparam2 (w_v
, h_v
);
3923 int w
= Int_val (w_v
);
3924 int h
= Int_val (h_v
);
3925 int cs
= Int_val (cs_v
);
3927 if (state
.pbo_usable
) {
3928 pbo
= calloc (sizeof (*pbo
), 1);
3930 err (1, "calloc pbo");
3942 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3945 state
.glGenBuffersARB (1, &pbo
->id
);
3946 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3947 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3948 NULL
, GL_STREAM_DRAW
);
3949 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3951 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3953 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3954 state
.glDeleteBuffersARB (1, &pbo
->id
);
3956 ret_v
= caml_copy_string ("0");
3962 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3964 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3968 err (1, "malloc %d bytes failed", res
+1);
3970 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3972 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3974 ret_v
= caml_copy_string (s
);
3979 ret_v
= caml_copy_string ("0");
3984 CAMLprim value
ml_freepbo (value s_v
)
3987 char *s
= String_val (s_v
);
3988 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3991 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3993 tile
->pbo
->ptr
= NULL
;
3994 tile
->pbo
->size
= -1;
3996 CAMLreturn (Val_unit
);
3999 CAMLprim value
ml_unmappbo (value s_v
)
4002 char *s
= String_val (s_v
);
4003 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4006 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4007 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4008 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4010 tile
->pbo
->ptr
= NULL
;
4011 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4013 CAMLreturn (Val_unit
);
4016 static void setuppbo (void)
4018 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4019 state
.pbo_usable
= GGPA (glBindBufferARB
)
4020 && GGPA (glUnmapBufferARB
)
4021 && GGPA (glMapBufferARB
)
4022 && GGPA (glBufferDataARB
)
4023 && GGPA (glGenBuffersARB
)
4024 && GGPA (glDeleteBuffersARB
);
4028 CAMLprim value
ml_pbo_usable (value unit_v
)
4030 CAMLparam1 (unit_v
);
4031 CAMLreturn (Val_bool (state
.pbo_usable
));
4034 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4036 CAMLparam3 (ptr_v
, x_v
, y_v
);
4037 CAMLlocal2 (ret_v
, tup_v
);
4039 char *s
= String_val (ptr_v
);
4040 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4041 struct pagedim
*pdim
;
4045 page
= parse_pointer ("ml_unproject", s
);
4046 pdim
= &state
.pagedims
[page
->pdimno
];
4048 ret_v
= Val_int (0);
4049 if (trylock ("ml_unproject")) {
4053 switch (page
->type
) {
4055 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4064 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4065 fz_invert_matrix (&ctm
, &ctm
);
4066 fz_transform_point (&p
, &ctm
);
4068 tup_v
= caml_alloc_tuple (2);
4069 ret_v
= caml_alloc_small (1, 1);
4070 Field (tup_v
, 0) = Val_int (p
.x
);
4071 Field (tup_v
, 1) = Val_int (p
.y
);
4072 Field (ret_v
, 0) = tup_v
;
4074 unlock ("ml_unproject");
4079 static void makestippletex (void)
4081 const char pixels
[] = "\xff\xff\0\0";
4082 glGenTextures (1, &state
.stid
);
4083 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4084 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4085 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4098 CAMLprim value
ml_fz_version (value unit_v
)
4100 return caml_copy_string (FZ_VERSION
);
4103 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4105 CAMLparam2 (pipe_v
, params_v
);
4106 CAMLlocal2 (trim_v
, fuzz_v
);
4113 struct sigaction sa
;
4115 state
.cr
= Int_val (Field (pipe_v
, 0));
4116 state
.cw
= Int_val (Field (pipe_v
, 1));
4117 state
.rotate
= Int_val (Field (params_v
, 0));
4118 state
.fitmodel
= Int_val (Field (params_v
, 1));
4119 trim_v
= Field (params_v
, 2);
4120 texcount
= Int_val (Field (params_v
, 3));
4121 state
.sliceheight
= Int_val (Field (params_v
, 4));
4122 mustoresize
= Int_val (Field (params_v
, 5));
4123 colorspace
= Int_val (Field (params_v
, 6));
4124 fontpath
= String_val (Field (params_v
, 7));
4126 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4127 if (!state
.trimcachepath
) {
4128 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4131 haspboext
= Bool_val (Field (params_v
, 9));
4133 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4135 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4136 fuzz_v
= Field (trim_v
, 1);
4137 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4138 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4139 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4140 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4142 set_tex_params (colorspace
);
4145 state
.face
= load_font (fontpath
);
4149 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4151 state
.face
= load_builtin_font (base
, len
);
4153 if (!state
.face
) _exit (1);
4155 realloctexts (texcount
);
4164 sa
.sa_handler
= SIG_IGN
;
4165 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4167 sa
.sa_handler
= SIG_DFL
;
4168 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4170 if (sigemptyset (&sa
.sa_mask
)) {
4171 err (1, "sigemptyset");
4173 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4174 err (1, "sigaction");
4177 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4179 errx (1, "pthread_create: %s", strerror (ret
));
4182 CAMLreturn (Val_unit
);