1 /* lots of code c&p-ed directly from mupdf */
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
17 #include <cygwin/socket.h> /* FIONREAD */
28 #include <caml/fail.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/unixsupport.h>
33 #include <mupdf/fitz.h>
34 #include <mupdf/cbz.h>
35 #include <mupdf/pdf.h>
36 #include <mupdf/xps.h>
37 #include <mupdf/img.h>
39 #include FT_FREETYPE_H
42 #include <fontconfig/fontconfig.h>
48 extern char **environ
;
51 #define MIN(a,b) ((a) < (b) ? (a) : (b))
52 #define MAX(a,b) ((a) > (b) ? (a) : (b))
55 #define NORETURN_ATTR __attribute__ ((noreturn))
56 #define UNUSED_ATTR __attribute__ ((unused))
57 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
58 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
62 #define OPTIMIZE_ATTR(n)
63 #define GCC_FMT_ATTR(a, b)
69 #define FMT_ptr_cast(p) (p)
70 #define FMT_ptr_cast2(p) (p)
72 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
73 err (int exitcode
, const char *fmt
, ...)
80 vfprintf (stderr
, fmt
, ap
);
82 fprintf (stderr
, ": %s\n", strerror (savederrno
));
87 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
88 errx (int exitcode
, const char *fmt
, ...)
93 vfprintf (stderr
, fmt
, ap
);
100 #ifndef GL_TEXTURE_RECTANGLE_ARB
101 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
105 #define GL_BGRA 0x80E1
108 #ifndef GL_UNSIGNED_INT_8_8_8_8
109 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
112 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
113 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
117 #define lprintf printf
122 #define ARSERT(cond) for (;;) { \
124 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
140 struct slice slices
[1];
151 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
159 enum { DPDF
, DXPS
, DCBZ
, DIMG
};
168 fz_text_sheet
*sheet
;
176 fz_display_list
*dlist
;
178 struct slink
*slinks
;
183 void (*freepage
) (void *);
189 struct pagedim
*pagedims
;
209 fz_colorspace
*colorspace
;
217 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
231 void (*closedoc
) (void);
232 void (*freepage
) (void *);
240 void (*glBindBufferARB
) (GLenum
, GLuint
);
241 GLboolean (*glUnmapBufferARB
) (GLenum
);
242 void *(*glMapBufferARB
) (GLenum
, GLenum
);
243 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
244 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
245 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
247 GLfloat texcoords
[8];
248 GLfloat vertices
[16];
257 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
259 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
262 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
264 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
267 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
269 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
270 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
273 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
275 static void lock (const char *cap
)
277 int ret
= pthread_mutex_lock (&mutex
);
279 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
283 static void unlock (const char *cap
)
285 int ret
= pthread_mutex_unlock (&mutex
);
287 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
291 static int trylock (const char *cap
)
293 int ret
= pthread_mutex_trylock (&mutex
);
294 if (ret
&& ret
!= EBUSY
) {
295 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
300 static void *parse_pointer (const char *cap
, const char *s
)
305 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
307 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
312 static double now (void)
316 if (gettimeofday (&tv
, NULL
)) {
317 err (1, "gettimeofday");
319 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
322 static int hasdata (void)
325 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
326 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
330 CAMLprim value
ml_hasdata (value fd_v
)
335 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
336 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
337 CAMLreturn (Val_bool (avail
> 0));
340 static void readdata (void *p
, int size
)
345 n
= read (state
.cr
, p
, size
);
347 if (errno
== EINTR
) goto again
;
348 err (1, "read (req %d, ret %zd)", size
, n
);
351 if (!n
) errx (1, "EOF while reading");
352 errx (1, "read (req %d, ret %zd)", size
, n
);
356 static void writedata (char *p
, int size
)
360 p
[0] = (size
>> 24) & 0xff;
361 p
[1] = (size
>> 16) & 0xff;
362 p
[2] = (size
>> 8) & 0xff;
363 p
[3] = (size
>> 0) & 0xff;
365 n
= write (state
.cw
, p
, size
+ 4);
367 if (!n
) errx (1, "EOF while writing data");
368 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
372 static int readlen (void)
377 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
380 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
388 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
391 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
395 if (len
< size
- 4) {
396 writedata (buf
, len
);
402 err (1, "vsnprintf for `%s' failed", fmt
);
404 buf
= realloc (buf
, size
);
409 static void closepdf (void)
412 pdf_close_document (state
.u
.pdf
);
417 static void closexps (void)
420 xps_close_document (state
.u
.xps
);
425 static void closecbz (void)
428 cbz_close_document (state
.u
.cbz
);
433 static void closeimg (void)
436 image_close_document (state
.u
.img
);
441 static void freepdfpage (void *ptr
)
443 pdf_free_page (state
.u
.pdf
, ptr
);
446 static void freeemptyxxxpage (void *ptr
)
451 static void freexpspage (void *ptr
)
453 xps_free_page (state
.u
.xps
, ptr
);
456 static void freecbzpage (void *ptr
)
458 cbz_free_page (state
.u
.cbz
, ptr
);
461 static void freeimgpage (void *ptr
)
463 image_free_page (state
.u
.img
, ptr
);
466 static void openxref (char *filename
, char *password
)
470 for (i
= 0; i
< state
.texcount
; ++i
) {
471 state
.texowners
[i
].w
= -1;
472 state
.texowners
[i
].slice
= NULL
;
475 if (state
.closedoc
) state
.closedoc ();
477 len
= strlen (filename
);
494 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
495 int len2
= strlen (tbl
[i
].ext
);
497 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
498 state
.type
= tbl
[i
].type
;
504 if (state
.pagedims
) {
505 free (state
.pagedims
);
506 state
.pagedims
= NULL
;
508 state
.pagedimcount
= 0;
510 fz_set_aa_level (state
.ctx
, state
.aalevel
);
511 switch (state
.type
) {
513 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
514 if (pdf_needs_password (state
.u
.pdf
)) {
515 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
517 errx (1, "invalid password");
520 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
521 state
.closedoc
= closepdf
;
522 state
.freepage
= freepdfpage
;
526 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
527 state
.pagecount
= xps_count_pages (state
.u
.xps
);
528 state
.closedoc
= closexps
;
529 state
.freepage
= freexpspage
;
533 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
534 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
535 state
.closedoc
= closecbz
;
536 state
.freepage
= freecbzpage
;
540 state
.u
.img
= image_open_document (state
.ctx
, filename
);
542 state
.closedoc
= closeimg
;
543 state
.freepage
= freeimgpage
;
548 static void pdfinfo (void)
550 if (state
.type
== DPDF
) {
553 printd ("info PDF version\t%d.%d",
554 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
556 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
560 char *items
[] = { "Title", "Author", "Creator",
561 "Producer", "CreationDate" };
563 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
564 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
565 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
568 printd ("title %s", s
);
570 printd ("info %s\t%s", items
[i
], s
);
572 fz_free (state
.ctx
, s
);
579 static void unlinktile (struct tile
*tile
)
583 for (i
= 0; i
< tile
->slicecount
; ++i
) {
584 struct slice
*s
= &tile
->slices
[i
];
586 if (s
->texindex
!= -1) {
587 if (state
.texowners
[s
->texindex
].slice
== s
) {
588 state
.texowners
[s
->texindex
].slice
= NULL
;
594 static void freepage (struct page
*page
)
597 fz_free_text_page (state
.ctx
, page
->text
);
600 fz_free_text_sheet (state
.ctx
, page
->sheet
);
605 fz_drop_display_list (state
.ctx
, page
->dlist
);
606 page
->freepage (page
->u
.ptr
);
610 static void freetile (struct tile
*tile
)
615 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
618 fz_drop_pixmap (state
.ctx
, state
.pig
);
620 state
.pig
= tile
->pixmap
;
625 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
634 static int cacheline32bytes
;
636 static void __attribute__ ((constructor
)) clcheck (void)
638 char **envp
= environ
;
643 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
645 cacheline32bytes
= auxv
[1] == 32;
651 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
653 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
654 if (cacheline32bytes
&& size
> 32) {
655 intptr_t a1
, a2
, diff
;
657 vector
unsigned char v
= vec_splat_u8 (-1);
658 vector
unsigned char *p
;
660 a1
= a2
= (intptr_t) pixmap
->samples
;
661 a2
= (a1
+ 31) & ~31;
666 while (a1
!= a2
) *(char *) a1
++ = 0xff;
667 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
668 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
670 vec_st (v
, i
+ 16, p
);
672 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
674 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
677 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
680 static void trimctm (pdf_page
*page
, int pindex
)
683 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
685 if (!pdim
->tctmready
) {
686 if (state
.trimmargins
) {
688 fz_matrix rm
, sm
, tm
, im
, ctm1
;
690 fz_rotate (&rm
, -pdim
->rotate
);
691 fz_scale (&sm
, 1, -1);
692 fz_concat (&ctm
, &rm
, &sm
);
693 realbox
= pdim
->mediabox
;
694 fz_transform_rect (&realbox
, &ctm
);
695 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
696 fz_concat (&ctm1
, &ctm
, &tm
);
697 fz_invert_matrix (&im
, &page
->ctm
);
698 fz_concat (&ctm
, &im
, &ctm1
);
708 static fz_matrix
pagectm (struct page
*page
)
712 if (page
->type
== DPDF
) {
713 trimctm (page
->u
.pdfpage
, page
->pdimno
);
715 &state
.pagedims
[page
->pdimno
].tctm
,
716 &state
.pagedims
[page
->pdimno
].ctm
);
719 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
721 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
722 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
727 static void *loadpage (int pageno
, int pindex
)
732 page
= calloc (sizeof (struct page
), 1);
734 err (1, "calloc page %d", pageno
);
737 page
->dlist
= fz_new_display_list (state
.ctx
);
738 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
740 switch (state
.type
) {
742 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
743 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
745 page
->freepage
= freepdfpage
;
749 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
750 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
752 page
->freepage
= freexpspage
;
756 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
757 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
759 page
->freepage
= freecbzpage
;
763 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
764 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
766 page
->freepage
= freeimgpage
;
770 fz_catch (state
.ctx
) {
772 page
->freepage
= freeemptyxxxpage
;
774 fz_free_device (dev
);
776 page
->pdimno
= pindex
;
777 page
->pageno
= pageno
;
778 page
->sgen
= state
.gen
;
779 page
->tgen
= state
.gen
;
780 page
->type
= state
.type
;
785 static struct tile
*alloctile (int h
)
792 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
793 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
794 tile
= calloc (tilesize
, 1);
796 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
798 for (i
= 0; i
< slicecount
; ++i
) {
799 int sh
= MIN (h
, state
.sliceheight
);
800 tile
->slices
[i
].h
= sh
;
801 tile
->slices
[i
].texindex
= -1;
804 tile
->slicecount
= slicecount
;
805 tile
->sliceheight
= state
.sliceheight
;
815 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
816 const fz_matrix
*ctm
, float alpha
)
818 struct obs
*obs
= dev
->user
;
820 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
822 fz_rect rect
= fz_unit_rect
;
824 fz_transform_rect (&rect
, ctm
);
825 fz_round_rect (&b
, &rect
);
826 fz_intersect_irect (&b
, &obs
->b
);
827 obs
->cured
= b
.x0
== obs
->b
.x0
830 && b
.y1
== obs
->b
.y1
;
834 static int obscured (struct page
*page
, fz_irect bbox
)
841 memset (&dev
, 0, sizeof (dev
));
842 memset (&obs
, 0, sizeof (obs
));
847 dev
.fill_image
= obs_fill_image
;
849 fz_rect_from_irect (&rect
, &bbox
);
850 ctm
= pagectm (page
);
851 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
854 #define OBSCURED obscured
856 #define OBSCURED(a, b) 0
859 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
867 struct pagedim
*pdim
;
869 tile
= alloctile (h
);
870 pdim
= &state
.pagedims
[page
->pdimno
];
875 bbox
.x1
= bbox
.x0
+ w
;
876 bbox
.y1
= bbox
.y0
+ h
;
879 if (state
.pig
->w
== w
881 && state
.pig
->colorspace
== state
.colorspace
) {
882 tile
->pixmap
= state
.pig
;
883 tile
->pixmap
->x
= bbox
.x0
;
884 tile
->pixmap
->y
= bbox
.y0
;
887 fz_drop_pixmap (state
.ctx
, state
.pig
);
894 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
900 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
906 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
907 clearpixmap (tile
->pixmap
);
909 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
910 ctm
= pagectm (page
);
911 fz_rect_from_irect (&rect
, &bbox
);
912 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
913 fz_free_device (dev
);
918 static void initpdims (void)
922 fz_rect rootmediabox
;
923 int pageno
, trim
, show
;
924 int trimw
= 0, cxcount
;
928 if (state
.trimmargins
&& state
.trimcachepath
) {
929 trimf
= fopen (state
.trimcachepath
, "rb");
931 trimf
= fopen (state
.trimcachepath
, "wb");
936 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
937 cxcount
= state
.pagecount
;
939 cxcount
= MIN (state
.pagecount
, 1);
941 if (state
.type
== DPDF
) {
943 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
944 "Root/Pages/MediaBox");
945 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
948 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
953 switch (state
.type
) {
955 pdf_document
*pdf
= state
.u
.pdf
;
956 pdf_obj
*pageref
= pdf_lookup_page_obj (pdf
, pageno
);
957 pdf_obj
*pageobj
= pdf_resolve_indirect (pageref
);
959 if (state
.trimmargins
) {
964 page
= pdf_load_page (pdf
, pageno
);
965 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
966 trim
= state
.trimanew
|| !obj
;
972 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
973 dev
->hints
|= FZ_IGNORE_SHADE
;
974 fz_invert_matrix (&ctm
, &page
->ctm
);
975 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
976 fz_free_device (dev
);
978 rect
.x0
+= state
.trimfuzz
.x0
;
979 rect
.x1
+= state
.trimfuzz
.x1
;
980 rect
.y0
+= state
.trimfuzz
.y0
;
981 rect
.y1
+= state
.trimfuzz
.y1
;
982 fz_transform_rect (&rect
, &ctm
);
983 fz_intersect_rect (&rect
, &page
->mediabox
);
985 if (fz_is_empty_rect (&rect
)) {
986 mediabox
= page
->mediabox
;
992 obj
= pdf_new_array (pdf
, 4);
993 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
994 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
995 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
996 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
997 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1000 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1001 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1002 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1003 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1006 rotate
= page
->rotate
;
1007 pdf_free_page (pdf
, page
);
1009 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1011 printd ("progress %f Trimming %d",
1012 (double) (pageno
+ 1) / state
.pagecount
,
1016 fz_catch (state
.ctx
) {
1017 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1024 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1026 if (fz_is_empty_rect (&mediabox
)) {
1034 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1036 if (!fz_is_empty_rect (&cropbox
)) {
1041 fz_intersect_rect (&mediabox
, &cropbox
);
1046 if (fz_is_empty_rect (&rootmediabox
)) {
1048 "cannot find page size for page %d\n",
1052 mediabox
= rootmediabox
;
1056 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1065 fz_try (state
.ctx
) {
1066 page
= xps_load_page (state
.u
.xps
, pageno
);
1067 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1069 if (state
.trimmargins
) {
1073 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1074 dev
->hints
|= FZ_IGNORE_SHADE
;
1075 xps_run_page (state
.u
.xps
, page
, dev
,
1076 &fz_identity
, NULL
);
1077 fz_free_device (dev
);
1079 rect
.x0
+= state
.trimfuzz
.x0
;
1080 rect
.x1
+= state
.trimfuzz
.x1
;
1081 rect
.y0
+= state
.trimfuzz
.y0
;
1082 rect
.y1
+= state
.trimfuzz
.y1
;
1083 fz_intersect_rect (&rect
, &mediabox
);
1085 if (!fz_is_empty_rect (&rect
)) {
1089 xps_free_page (state
.u
.xps
, page
);
1091 printd ("progress %f loading %d",
1092 (double) (pageno
+ 1) / state
.pagecount
,
1096 fz_catch (state
.ctx
) {
1108 if (state
.trimmargins
&& trimw
) {
1111 fz_try (state
.ctx
) {
1112 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1113 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1114 cbz_free_page (state
.u
.cbz
, page
);
1115 printd ("progress %f Trimming %d",
1116 (double) (pageno
+ 1) / state
.pagecount
,
1119 fz_catch (state
.ctx
) {
1120 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1123 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1125 err (1, "fwrite trim mediabox");
1131 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1133 err (1, "fread trim mediabox");
1139 fz_try (state
.ctx
) {
1140 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1141 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1142 cbz_free_page (state
.u
.cbz
, page
);
1144 fz_catch (state
.ctx
) {
1145 fprintf (stderr
, "failed to load cbz page\n");
1163 fz_try (state
.ctx
) {
1164 page
= image_load_page (state
.u
.img
, pageno
);
1165 image_bound_page (state
.u
.img
, page
, &mediabox
);
1166 image_free_page (state
.u
.img
, page
);
1168 fz_catch (state
.ctx
) {
1169 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1175 ARSERT (0 && state
.type
);
1178 if (state
.pagedimcount
== 0
1179 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1180 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1183 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1184 state
.pagedims
= realloc (state
.pagedims
, size
);
1185 if (!state
.pagedims
) {
1186 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1187 size
, state
.pagedimcount
+ 1);
1190 p
= &state
.pagedims
[state
.pagedimcount
++];
1192 p
->mediabox
= mediabox
;
1197 if (state
.trimmargins
) {
1198 printd ("progress 1 Trimmed %d pages in %f seconds",
1199 state
.pagecount
, end
- start
);
1202 printd ("vmsg Processed %d pages in %f seconds",
1203 state
.pagecount
, end
- start
);
1207 if (fclose (trimf
)) {
1213 static void layout (void)
1218 struct pagedim
*p
= p
;
1219 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1221 if (state
.pagedimcount
== 0) return;
1223 switch (state
.fitmodel
) {
1224 case FitProportional
:
1225 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1228 p
= &state
.pagedims
[pindex
];
1229 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1231 fz_transform_rect (&box
, &rm
);
1233 x0
= MIN (box
.x0
, box
.x1
);
1234 x1
= MAX (box
.x0
, box
.x1
);
1237 maxw
= MAX (w
, maxw
);
1238 zoom
= state
.w
/ maxw
;
1250 ARSERT (0 && state
.fitmodel
);
1253 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1257 p
= &state
.pagedims
[pindex
];
1258 fz_rotate (&ctm
, state
.rotate
);
1259 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1261 fz_transform_rect (&box
, &rm
);
1262 w
= box
.x1
- box
.x0
;
1263 switch (state
.fitmodel
) {
1264 case FitProportional
:
1265 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1271 h
= box
.y1
- box
.y0
;
1273 zoom
= MIN (zw
, zh
);
1274 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1283 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1284 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1286 fz_rotate (&rm
, p
->rotate
);
1287 p
->pagebox
= p
->mediabox
;
1288 fz_transform_rect (&p
->pagebox
, &rm
);
1289 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1290 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1294 fz_transform_rect (&rect
, &ctm
);
1295 fz_round_rect (&p
->bounds
, &rect
);
1298 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1299 fz_scale (&sm
, zoom
, -zoom
);
1300 fz_concat (&ctm
, &tm
, &sm
);
1301 fz_concat (&p
->lctm
, &ctm
, &rm
);
1307 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1308 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1309 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1310 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1314 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1315 } while (p
-- != state
.pagedims
);
1319 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1320 desttoanchor (fz_link_dest
*dest
)
1324 struct pagedim
*pdim
= state
.pagedims
;
1329 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1330 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1332 pdim
= &state
.pagedims
[i
];
1334 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1336 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1337 p
.x
= dest
->ld
.gotor
.lt
.x
;
1340 p
.y
= dest
->ld
.gotor
.lt
.y
;
1341 fz_transform_point (&p
, &pdim
->lctm
);
1345 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1346 double x0
, x1
, y0
, y1
;
1348 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1349 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1351 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1352 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1354 a
.n
= dest
->ld
.gotor
.page
;
1359 static void recurse_outline (fz_outline
*outline
, int level
)
1362 switch (outline
->dest
.kind
) {
1365 struct anchor a
= desttoanchor (&outline
->dest
);
1368 printd ("o %d %d %d %d %s",
1369 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1375 printd ("ou %d %" FMT_s
" %s %s", level
,
1376 strlen (outline
->title
), outline
->title
,
1377 outline
->dest
.ld
.uri
.uri
);
1381 printd ("on %d %s", level
, outline
->title
);
1385 printd ("emsg Unhandled outline kind %d for %s\n",
1386 outline
->dest
.kind
, outline
->title
);
1389 if (outline
->down
) {
1390 recurse_outline (outline
->down
, level
+ 1);
1392 outline
= outline
->next
;
1396 static void process_outline (void)
1398 fz_outline
*outline
;
1400 if (!state
.needoutline
) return;
1402 state
.needoutline
= 0;
1403 switch (state
.type
) {
1405 outline
= pdf_load_outline (state
.u
.pdf
);
1408 outline
= xps_load_outline (state
.u
.xps
);
1415 recurse_outline (outline
, 0);
1416 fz_free_outline (state
.ctx
, outline
);
1420 static char *strofspan (fz_text_span
*span
)
1425 size_t size
= 0, cap
= 80;
1427 p
= malloc (cap
+ 1);
1428 if (!p
) return NULL
;
1430 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1431 int n
= fz_runetochar (utf8
, ch
->c
);
1432 if (size
+ n
> cap
) {
1434 p
= realloc (p
, cap
+ 1);
1435 if (!p
) return NULL
;
1438 memcpy (p
+ size
, utf8
, n
);
1445 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1446 fz_text_span
*span
, fz_matrix ctm
,
1447 int stop
, int pageno
, double start
)
1454 fz_point p1
, p2
, p3
, p4
;
1456 p
= strofspan (span
);
1459 ret
= regexec (re
, p
, 1, &rm
, 0);
1462 if (ret
!= REG_NOMATCH
) {
1465 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1466 printd ("msg regexec error `%.*s'",
1467 (int) size
, errbuf
);
1475 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1476 c
+= fz_runelen (span
->text
[a
].c
);
1478 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1479 c
+= fz_runelen (span
->text
[b
].c
);
1482 if (fz_runelen (span
->text
[b
].c
) > 1) {
1486 fz_text_char_bbox (&sb
, span
, a
);
1487 fz_text_char_bbox (&eb
, span
, b
);
1499 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1506 printd ("progress 1 found at %d `%.*s' in %f sec",
1507 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1511 printd ("match %d %d %f %f %f %f %f %f %f %f",
1523 static int compareblocks (const void *l
, const void *r
)
1525 fz_text_block
const *ls
= l
;
1526 fz_text_block
const* rs
= r
;
1527 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1530 /* wishful thinking function */
1531 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1536 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1538 fz_text_sheet
*sheet
;
1539 struct pagedim
*pdim
, *pdimprev
;
1540 int stop
= 0, niters
= 0;
1543 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1547 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1548 if (niters
++ == 5) {
1551 printd ("progress 1 attention requested aborting search at %d",
1556 printd ("progress %f searching in page %d",
1557 (double) (pageno
+ 1) / state
.pagecount
,
1562 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1563 pdim
= &state
.pagedims
[i
];
1564 if (pdim
->pageno
== pageno
) {
1567 if (pdim
->pageno
> pageno
) {
1576 sheet
= fz_new_text_sheet (state
.ctx
);
1577 text
= fz_new_text_page (state
.ctx
);
1578 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1580 switch (state
.type
) {
1583 fz_try (state
.ctx
) {
1584 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1585 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1586 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1587 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1588 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1591 fz_catch (state
.ctx
) {
1592 fz_free_device (tdev
);
1599 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1600 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1604 ARSERT (0 && state
.type
);
1607 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1608 fz_free_device (tdev
);
1610 for (j
= 0; j
< text
->len
; ++j
) {
1613 fz_text_block
*block
;
1615 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1616 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1619 for (k
= 0; k
< block
->len
; ++k
) {
1624 line
= &block
->lines
[k
];
1625 if (line
->bbox
.y0
< y
+ 1) continue;
1628 line
= &block
->lines
[block
->len
- 1 - k
];
1629 if (line
->bbox
.y0
> y
- 1) continue;
1632 for (span
= line
->first_span
; span
; span
= span
->next
) {
1633 switch (matchspan (re
, text
, span
, ctm
,
1634 stop
, pageno
, start
)) {
1636 case 1: stop
= 1; break;
1637 case -1: stop
= 1; goto endloop
;
1652 fz_free_text_page (state
.ctx
, text
);
1653 fz_free_text_sheet (state
.ctx
, sheet
);
1655 state
.freepage (u
.ptr
);
1660 printd ("progress 1 no matches %f sec", end
- start
);
1662 printd ("clearrects");
1665 static void set_tex_params (int colorspace
)
1672 switch (colorspace
) {
1674 state
.texiform
= GL_RGBA8
;
1675 state
.texform
= GL_RGBA
;
1676 state
.texty
= GL_UNSIGNED_BYTE
;
1677 state
.colorspace
= fz_device_rgb (state
.ctx
);
1680 state
.texiform
= GL_RGBA8
;
1681 state
.texform
= GL_BGRA
;
1682 state
.texty
= endianness
.s
> 1
1683 ? GL_UNSIGNED_INT_8_8_8_8
1684 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1685 state
.colorspace
= fz_device_bgr (state
.ctx
);
1688 state
.texiform
= GL_LUMINANCE_ALPHA
;
1689 state
.texform
= GL_LUMINANCE_ALPHA
;
1690 state
.texty
= GL_UNSIGNED_BYTE
;
1691 state
.colorspace
= fz_device_gray (state
.ctx
);
1694 errx (1, "invalid colorspce %d", colorspace
);
1698 static void realloctexts (int texcount
)
1702 if (texcount
== state
.texcount
) return;
1704 if (texcount
< state
.texcount
) {
1705 glDeleteTextures (state
.texcount
- texcount
,
1706 state
.texids
+ texcount
);
1709 size
= texcount
* sizeof (*state
.texids
);
1710 state
.texids
= realloc (state
.texids
, size
);
1711 if (!state
.texids
) {
1712 err (1, "realloc texids %" FMT_s
, size
);
1715 size
= texcount
* sizeof (*state
.texowners
);
1716 state
.texowners
= realloc (state
.texowners
, size
);
1717 if (!state
.texowners
) {
1718 err (1, "realloc texowners %" FMT_s
, size
);
1720 if (texcount
> state
.texcount
) {
1723 glGenTextures (texcount
- state
.texcount
,
1724 state
.texids
+ state
.texcount
);
1725 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1726 state
.texowners
[i
].w
= -1;
1727 state
.texowners
[i
].slice
= NULL
;
1730 state
.texcount
= texcount
;
1734 static char *mbtoutf8 (char *s
)
1740 len
= mbstowcs (NULL
, s
, strlen (s
));
1745 if (len
== (size_t) -1) {
1750 tmp
= malloc (len
* sizeof (wchar_t));
1755 ret
= mbstowcs (tmp
, s
, len
);
1756 if (ret
== (size_t) -1) {
1762 for (i
= 0; i
< ret
; ++i
) {
1763 len
+= fz_runelen (tmp
[i
]);
1766 p
= r
= malloc (len
+ 1);
1772 for (i
= 0; i
< ret
; ++i
) {
1773 p
+= fz_runetochar (p
, tmp
[i
]);
1780 CAMLprim value
ml_mbtoutf8 (value s_v
)
1786 s
= String_val (s_v
);
1792 ret_v
= caml_copy_string (r
);
1798 static void * mainloop (void *unused
)
1801 int len
, ret
, oldlen
= 0;
1806 errx (1, "readlen returned 0");
1809 if (oldlen
< len
+ 1) {
1810 p
= realloc (p
, len
+ 1);
1812 err (1, "realloc %d failed", len
+ 1);
1819 if (!strncmp ("open", p
, 4)) {
1826 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1828 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1831 filename
= p
+ 5 + off
;
1832 filenamelen
= strlen (filename
);
1833 password
= filename
+ filenamelen
+ 1;
1836 openxref (filename
, password
);
1842 utf8filename
= mbtoutf8 (filename
);
1843 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1844 if (utf8filename
!= filename
) {
1845 free (utf8filename
);
1848 state
.needoutline
= 1;
1850 else if (!strncmp ("cs", p
, 2)) {
1853 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1855 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1858 set_tex_params (colorspace
);
1859 for (i
= 0; i
< state
.texcount
; ++i
) {
1860 state
.texowners
[i
].w
= -1;
1861 state
.texowners
[i
].slice
= NULL
;
1865 else if (!strncmp ("freepage", p
, 8)) {
1868 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1870 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1874 else if (!strncmp ("freetile", p
, 8)) {
1877 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1879 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1883 else if (!strncmp ("search", p
, 6)) {
1884 int icase
, pageno
, y
, ret
, len2
, forward
;
1888 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1889 &icase
, &pageno
, &y
, &forward
, &len2
);
1891 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1894 pattern
= p
+ 6 + len2
;
1895 ret
= regcomp (&re
, pattern
,
1896 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1901 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1902 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1905 search (&re
, pageno
, y
, forward
);
1909 else if (!strncmp ("geometry", p
, 8)) {
1913 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1915 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1923 for (i
= 0; i
< state
.texcount
; ++i
) {
1924 state
.texowners
[i
].slice
= NULL
;
1927 state
.fitmodel
= fitmodel
;
1932 unlock ("geometry");
1933 printd ("continue %d", state
.pagecount
);
1935 else if (!strncmp ("reqlayout", p
, 9)) {
1937 int rotate
, fitmodel
, off
, h
;
1940 ret
= sscanf (p
+ 9, " %d %d %d %n",
1941 &rotate
, &fitmodel
, &h
, &off
);
1943 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1946 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1949 state
.rotate
= rotate
;
1950 state
.fitmodel
= fitmodel
;
1955 nameddest
= p
+ 9 + off
;
1956 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1959 pdf_obj
*needle
, *obj
;
1961 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
1962 strlen (nameddest
));
1963 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1965 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
1967 a
= desttoanchor (&dest
);
1969 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1972 printd ("emsg failed to parse destination `%s'\n",
1977 printd ("emsg destination `%s' not found\n",
1980 pdf_drop_obj (needle
);
1984 unlock ("reqlayout");
1985 printd ("continue %d", state
.pagecount
);
1987 else if (!strncmp ("page", p
, 4)) {
1990 int pageno
, pindex
, ret
;
1992 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1994 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1999 page
= loadpage (pageno
, pindex
);
2003 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
2005 else if (!strncmp ("tile", p
, 4)) {
2006 int x
, y
, w
, h
, ret
;
2012 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
2013 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
2015 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2020 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2024 printd ("tile %d %d %" FMT_ptr
" %u %f",
2026 FMT_ptr_cast2 (tile
),
2027 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2030 else if (!strncmp ("settrim", p
, 7)) {
2034 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2035 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2037 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2041 state
.trimmargins
= trimmargins
;
2042 state
.needoutline
= 1;
2043 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2045 state
.trimfuzz
= fuzz
;
2047 state
.pagedimcount
= 0;
2048 free (state
.pagedims
);
2049 state
.pagedims
= NULL
;
2054 printd ("continue %d", state
.pagecount
);
2056 else if (!strncmp ("sliceh", p
, 6)) {
2059 ret
= sscanf (p
+ 6, " %d", &h
);
2061 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2063 if (h
!= state
.sliceheight
) {
2066 state
.sliceheight
= h
;
2067 for (i
= 0; i
< state
.texcount
; ++i
) {
2068 state
.texowners
[i
].w
= -1;
2069 state
.texowners
[i
].h
= -1;
2070 state
.texowners
[i
].slice
= NULL
;
2074 else if (!strncmp ("interrupt", p
, 9)) {
2075 printd ("vmsg interrupted");
2078 errx (1, "unknown command %.*s", len
, p
);
2084 CAMLprim value
ml_realloctexts (value texcount_v
)
2086 CAMLparam1 (texcount_v
);
2089 if (trylock ("ml_realloctexts")) {
2093 realloctexts (Int_val (texcount_v
));
2095 unlock ("ml_realloctexts");
2098 CAMLreturn (Val_bool (ok
));
2101 static void recti (int x0
, int y0
, int x1
, int y1
)
2103 GLfloat
*v
= state
.vertices
;
2105 glVertexPointer (2, GL_FLOAT
, 0, v
);
2106 v
[0] = x0
; v
[1] = y0
;
2107 v
[2] = x1
; v
[3] = y0
;
2108 v
[4] = x0
; v
[5] = y1
;
2109 v
[6] = x1
; v
[7] = y1
;
2110 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2113 static void showsel (struct page
*page
, int ox
, int oy
)
2119 fz_page_block
*pageb
;
2120 fz_text_block
*block
;
2121 struct mark first
, last
;
2123 first
= page
->fmark
;
2126 if (!first
.span
|| !last
.span
) return;
2128 glEnable (GL_BLEND
);
2129 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2130 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
2132 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2133 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2134 for (pageb
= page
->text
->blocks
;
2135 pageb
< page
->text
->blocks
+ page
->text
->len
;
2137 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2138 block
= pageb
->u
.text
;
2140 for (line
= block
->lines
;
2141 line
< block
->lines
+ block
->len
;
2144 rect
= fz_empty_rect
;
2146 for (span
= line
->first_span
; span
; span
= span
->next
) {
2148 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2153 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2155 j
= MIN (first
.i
, last
.i
);
2156 k
= MAX (first
.i
, last
.i
);
2159 if (span
== first
.span
) {
2163 else if (span
== last
.span
) {
2170 for (i
= j
; i
<= k
; ++i
) {
2172 fz_union_rect (&rect
,
2173 fz_text_char_bbox (&bbox
, span
, i
));
2175 fz_round_rect (&bbox
, &rect
);
2176 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2183 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2184 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2185 if (span
== last
.span
) {
2188 rect
= fz_empty_rect
;
2194 glDisable (GL_BLEND
);
2199 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2201 fz_matrix ctm
, tm
, pm
;
2202 fz_link
*link
, *links
;
2203 GLfloat
*texcoords
= state
.texcoords
;
2204 GLfloat
*vertices
= state
.vertices
;
2206 switch (page
->type
) {
2208 links
= page
->u
.pdfpage
->links
;
2212 links
= page
->u
.xpspage
->links
;
2219 glEnable (GL_TEXTURE_1D
);
2220 glEnable (GL_BLEND
);
2221 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2222 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2224 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2225 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2226 fz_translate (&tm
, xoff
, yoff
);
2227 pm
= pagectm (page
);
2228 fz_concat (&ctm
, &pm
, &tm
);
2230 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2231 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2233 for (link
= links
; link
; link
= link
->next
) {
2234 fz_point p1
, p2
, p3
, p4
;
2236 p1
.x
= link
->rect
.x0
;
2237 p1
.y
= link
->rect
.y0
;
2239 p2
.x
= link
->rect
.x1
;
2240 p2
.y
= link
->rect
.y0
;
2242 p3
.x
= link
->rect
.x1
;
2243 p3
.y
= link
->rect
.y1
;
2245 p4
.x
= link
->rect
.x0
;
2246 p4
.y
= link
->rect
.y1
;
2248 fz_transform_point (&p1
, &ctm
);
2249 fz_transform_point (&p2
, &ctm
);
2250 fz_transform_point (&p3
, &ctm
);
2251 fz_transform_point (&p4
, &ctm
);
2253 switch (link
->dest
.kind
) {
2254 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2255 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2256 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2257 default: glColor3ub (0, 0, 0); break;
2265 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2269 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2271 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2272 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2274 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2275 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2277 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2278 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2280 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2281 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2283 glDrawArrays (GL_LINES
, 0, 8);
2286 glDisable (GL_BLEND
);
2287 glDisable (GL_TEXTURE_1D
);
2290 static int compareslinks (const void *l
, const void *r
)
2292 struct slink
const *ls
= l
;
2293 struct slink
const *rs
= r
;
2294 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2295 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2297 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2300 static void droptext (struct page
*page
)
2303 fz_free_text_page (state
.ctx
, page
->text
);
2306 page
->fmark
.span
= NULL
;
2307 page
->lmark
.span
= NULL
;
2311 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2316 static void dropslinks (struct page
*page
)
2319 free (page
->slinks
);
2320 page
->slinks
= NULL
;
2321 page
->slinkcount
= 0;
2325 static void ensureslinks (struct page
*page
)
2329 size_t slinksize
= sizeof (*page
->slinks
);
2330 fz_link
*link
, *links
;
2332 if (state
.gen
!= page
->sgen
) {
2334 page
->sgen
= state
.gen
;
2336 if (page
->slinks
) return;
2338 switch (page
->type
) {
2340 links
= page
->u
.pdfpage
->links
;
2341 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2343 &state
.pagedims
[page
->pdimno
].tctm
,
2344 &state
.pagedims
[page
->pdimno
].ctm
);
2348 links
= page
->u
.xpspage
->links
;
2349 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2356 for (link
= links
; link
; link
= link
->next
) {
2360 page
->slinkcount
= count
;
2361 page
->slinks
= calloc (count
, slinksize
);
2362 if (!page
->slinks
) {
2363 err (1, "realloc slinks %d", count
);
2366 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2370 fz_transform_rect (&rect
, &ctm
);
2371 page
->slinks
[i
].link
= link
;
2372 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2374 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2378 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2379 static void fmt_linkn (char *s
, unsigned int u
)
2381 unsigned int len
; unsigned int q
;
2382 int zma
= 'z' - 'a' + 1;
2384 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2387 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2388 /* handles u == 0 */
2393 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2394 int noff
, char *targ
, int tlen
, int hfsize
)
2398 struct slink
*slink
;
2399 double x0
, y0
, x1
, y1
, w
;
2401 ensureslinks (page
);
2402 glColor3ub (0xc3, 0xb0, 0x91);
2403 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2404 fmt_linkn (buf
, i
+ noff
);
2405 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2406 slink
= &page
->slinks
[i
];
2408 x0
= slink
->bbox
.x0
+ xoff
- 5;
2409 y1
= slink
->bbox
.y0
+ yoff
- 5;
2410 y0
= y1
+ 10 + hfsize
;
2411 w
= measure_string (state
.face
, hfsize
, buf
);
2413 recti (x0
, y0
, x1
, y1
);
2417 glEnable (GL_BLEND
);
2418 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2419 glEnable (GL_TEXTURE_2D
);
2420 glColor3ub (0, 0, 0);
2421 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2422 fmt_linkn (buf
, i
+ noff
);
2423 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2424 slink
= &page
->slinks
[i
];
2426 x0
= slink
->bbox
.x0
+ xoff
;
2427 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2428 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2431 glDisable (GL_TEXTURE_2D
);
2432 glDisable (GL_BLEND
);
2436 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2439 struct slice
*slice1
;
2440 unsigned char *texdata
;
2443 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2444 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2446 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2447 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2448 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2452 int texindex
= state
.texindex
++ % state
.texcount
;
2454 if (state
.texowners
[texindex
].w
== tile
->w
) {
2455 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2459 state
.texowners
[texindex
].h
= slice
->h
;
2463 state
.texowners
[texindex
].h
= slice
->h
;
2466 state
.texowners
[texindex
].w
= tile
->w
;
2467 state
.texowners
[texindex
].slice
= slice
;
2468 slice
->texindex
= texindex
;
2470 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2472 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2476 texdata
= tile
->pixmap
->samples
;
2479 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2491 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2503 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2508 CAMLprim value
ml_begintiles (value unit_v
)
2510 CAMLparam1 (unit_v
);
2511 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2512 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2513 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2514 CAMLreturn (unit_v
);
2517 CAMLprim value
ml_endtiles (value unit_v
)
2519 CAMLparam1 (unit_v
);
2520 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2521 CAMLreturn (unit_v
);
2524 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2526 CAMLparam2 (args_v
, ptr_v
);
2527 int dispx
= Int_val (Field (args_v
, 0));
2528 int dispy
= Int_val (Field (args_v
, 1));
2529 int dispw
= Int_val (Field (args_v
, 2));
2530 int disph
= Int_val (Field (args_v
, 3));
2531 int tilex
= Int_val (Field (args_v
, 4));
2532 int tiley
= Int_val (Field (args_v
, 5));
2533 char *s
= String_val (ptr_v
);
2534 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2535 int slicey
, firstslice
;
2536 struct slice
*slice
;
2537 GLfloat
*texcoords
= state
.texcoords
;
2538 GLfloat
*vertices
= state
.vertices
;
2540 firstslice
= tiley
/ tile
->sliceheight
;
2541 slice
= &tile
->slices
[firstslice
];
2542 slicey
= tiley
% tile
->sliceheight
;
2547 dh
= slice
->h
- slicey
;
2548 dh
= MIN (disph
, dh
);
2549 uploadslice (tile
, slice
);
2551 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2552 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2553 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2554 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2556 vertices
[0] = dispx
; vertices
[1] = dispy
;
2557 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2558 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2559 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2561 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2565 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2568 CAMLreturn (Val_unit
);
2571 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2572 value xoff_v
, value yoff_v
,
2575 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2576 int xoff
= Int_val (xoff_v
);
2577 int yoff
= Int_val (yoff_v
);
2578 int noff
= Int_val (Field (li_v
, 0));
2579 char *targ
= String_val (Field (li_v
, 1));
2580 int tlen
= caml_string_length (Field (li_v
, 1));
2581 int hfsize
= Int_val (Field (li_v
, 2));
2582 char *s
= String_val (ptr_v
);
2583 int hlmask
= Int_val (hlinks_v
);
2584 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2587 /* deal with loadpage failed pages */
2591 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2592 if (trylock ("ml_postprocess")) {
2597 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2598 noff
= page
->slinkcount
;
2600 if (page
->tgen
== state
.gen
) {
2601 showsel (page
, xoff
, yoff
);
2603 unlock ("ml_postprocess");
2606 CAMLreturn (Val_int (noff
));
2609 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2613 const fz_matrix
*tctm
;
2614 fz_link
*link
, *links
;
2616 switch (page
->type
) {
2618 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2619 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2620 links
= page
->u
.pdfpage
->links
;
2624 tctm
= &fz_identity
;
2625 links
= page
->u
.xpspage
->links
;
2634 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2635 fz_invert_matrix (&ctm
, &ctm
);
2636 fz_transform_point (&p
, &ctm
);
2638 for (link
= links
; link
; link
= link
->next
) {
2639 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2640 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2648 static void ensuretext (struct page
*page
)
2650 if (state
.gen
!= page
->tgen
) {
2652 page
->tgen
= state
.gen
;
2658 page
->text
= fz_new_text_page (state
.ctx
);
2659 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2660 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2661 ctm
= pagectm (page
);
2662 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2663 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2664 qsort (page
->text
->blocks
, page
->text
->len
,
2665 sizeof (*page
->text
->blocks
), compareblocks
);
2667 fz_free_device (tdev
);
2671 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2673 CAMLparam2 (start_page_v
, dir_v
);
2675 int i
, dir
= Int_val (dir_v
);
2676 int start_page
= Int_val (start_page_v
);
2677 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2679 ret_v
= Val_int (0);
2680 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2684 lock ("ml_findpage_with_links");
2685 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2688 switch (state
.type
) {
2691 pdf_page
*page
= NULL
;
2693 fz_try (state
.ctx
) {
2694 page
= pdf_load_page (state
.u
.pdf
, i
);
2695 found
= !!page
->links
;
2697 fz_catch (state
.ctx
) {
2707 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2708 found
= !!page
->links
;
2714 ARSERT (0 && "invalid document type");
2718 ret_v
= caml_alloc_small (1, 1);
2719 Field (ret_v
, 0) = Val_int (i
);
2724 unlock ("ml_findpage_with_links");
2730 enum { dir_first
, dir_last
};
2731 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2733 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2735 CAMLparam2 (ptr_v
, dir_v
);
2736 CAMLlocal2 (ret_v
, pos_v
);
2738 int dirtag
, i
, slinkindex
;
2739 struct slink
*found
= NULL
,*slink
;
2740 char *s
= String_val (ptr_v
);
2742 page
= parse_pointer ("ml_findlink", s
);
2743 ret_v
= Val_int (0);
2744 if (trylock ("ml_findlink")) {
2748 ensureslinks (page
);
2750 if (Is_block (dir_v
)) {
2751 dirtag
= Tag_val (dir_v
);
2753 case dir_first_visible
:
2755 int x0
, y0
, dir
, first_index
, last_index
;
2757 pos_v
= Field (dir_v
, 0);
2758 x0
= Int_val (Field (pos_v
, 0));
2759 y0
= Int_val (Field (pos_v
, 1));
2760 dir
= Int_val (Field (pos_v
, 2));
2765 last_index
= page
->slinkcount
;
2768 first_index
= page
->slinkcount
- 1;
2772 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2773 slink
= &page
->slinks
[i
];
2774 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2783 slinkindex
= Int_val (Field (dir_v
, 0));
2784 found
= &page
->slinks
[slinkindex
];
2785 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2786 slink
= &page
->slinks
[i
];
2787 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2795 slinkindex
= Int_val (Field (dir_v
, 0));
2796 found
= &page
->slinks
[slinkindex
];
2797 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2798 slink
= &page
->slinks
[i
];
2799 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2807 slinkindex
= Int_val (Field (dir_v
, 0));
2808 found
= &page
->slinks
[slinkindex
];
2809 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2810 slink
= &page
->slinks
[i
];
2811 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2819 slinkindex
= Int_val (Field (dir_v
, 0));
2820 found
= &page
->slinks
[slinkindex
];
2821 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2822 slink
= &page
->slinks
[i
];
2823 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2832 dirtag
= Int_val (dir_v
);
2835 found
= page
->slinks
;
2840 found
= page
->slinks
+ (page
->slinkcount
- 1);
2846 ret_v
= caml_alloc_small (2, 1);
2847 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2850 unlock ("ml_findlink");
2855 enum { uuri
, ugoto
, utext
, uunexpected
,
2856 ulaunch
, unamed
, uremote
, uremotedest
};
2862 switch (link->dest.kind) { \
2863 case FZ_LINK_GOTO: \
2867 pageno = link->dest.ld.gotor.page; \
2871 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2872 p.y = link->dest.ld.gotor.lt.y; \
2873 fz_transform_point (&p, &pdim->lctm); \
2875 tup_v = caml_alloc_tuple (2); \
2876 ret_v = caml_alloc_small (1, ugoto); \
2877 Field (tup_v, 0) = Val_int (pageno); \
2878 Field (tup_v, 1) = Val_int (p.y); \
2879 Field (ret_v, 0) = tup_v; \
2884 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2885 ret_v = caml_alloc_small (1, uuri); \
2886 Field (ret_v, 0) = str_v; \
2889 case FZ_LINK_LAUNCH: \
2890 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2891 ret_v = caml_alloc_small (1, ulaunch); \
2892 Field (ret_v, 0) = str_v; \
2895 case FZ_LINK_NAMED: \
2896 str_v = caml_copy_string (link->dest.ld.named.named); \
2897 ret_v = caml_alloc_small (1, unamed); \
2898 Field (ret_v, 0) = str_v; \
2901 case FZ_LINK_GOTOR: \
2905 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2906 pageno = link->dest.ld.gotor.page; \
2907 if (pageno == -1) { \
2908 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
2909 rty = uremotedest; \
2912 gr_v = Val_int (pageno); \
2915 tup_v = caml_alloc_tuple (2); \
2916 ret_v = caml_alloc_small (1, rty); \
2917 Field (tup_v, 0) = str_v; \
2918 Field (tup_v, 1) = gr_v; \
2919 Field (ret_v, 0) = tup_v; \
2927 snprintf (buf, sizeof (buf), \
2928 "unhandled link kind %d", link->dest.kind); \
2929 str_v = caml_copy_string (buf); \
2930 ret_v = caml_alloc_small (1, uunexpected); \
2931 Field (ret_v, 0) = str_v; \
2937 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2939 CAMLparam2 (ptr_v
, n_v
);
2940 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2943 struct pagedim
*pdim
;
2944 char *s
= String_val (ptr_v
);
2946 ret_v
= Val_int (0);
2947 if (trylock ("ml_getlink")) {
2951 page
= parse_pointer ("ml_getlink", s
);
2952 ensureslinks (page
);
2953 pdim
= &state
.pagedims
[page
->pdimno
];
2954 link
= page
->slinks
[Int_val (n_v
)].link
;
2957 unlock ("ml_getlink");
2962 CAMLprim value
ml_getlinkcount (value ptr_v
)
2966 char *s
= String_val (ptr_v
);
2968 page
= parse_pointer ("ml_getlinkcount", s
);
2969 CAMLreturn (Val_int (page
->slinkcount
));
2972 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2974 CAMLparam2 (ptr_v
, n_v
);
2977 struct slink
*slink
;
2978 char *s
= String_val (ptr_v
);
2980 page
= parse_pointer ("ml_getlinkrect", s
);
2981 ret_v
= caml_alloc_tuple (4);
2982 if (trylock ("ml_getlinkrect")) {
2983 Field (ret_v
, 0) = Val_int (0);
2984 Field (ret_v
, 1) = Val_int (0);
2985 Field (ret_v
, 2) = Val_int (0);
2986 Field (ret_v
, 3) = Val_int (0);
2989 ensureslinks (page
);
2991 slink
= &page
->slinks
[Int_val (n_v
)];
2992 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2993 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2994 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2995 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2996 unlock ("ml_getlinkrect");
3002 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3004 CAMLparam3 (ptr_v
, x_v
, y_v
);
3005 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3008 char *s
= String_val (ptr_v
);
3009 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3010 struct pagedim
*pdim
;
3012 ret_v
= Val_int (0);
3013 if (trylock ("ml_whatsunder")) {
3017 page
= parse_pointer ("ml_whatsunder", s
);
3018 pdim
= &state
.pagedims
[page
->pdimno
];
3019 x
+= pdim
->bounds
.x0
;
3020 y
+= pdim
->bounds
.y0
;
3021 link
= getlink (page
, x
, y
);
3027 fz_page_block
*pageb
;
3028 fz_text_block
*block
;
3031 for (pageb
= page
->text
->blocks
;
3032 pageb
< page
->text
->blocks
+ page
->text
->len
;
3035 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3036 block
= pageb
->u
.text
;
3039 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3042 for (line
= block
->lines
;
3043 line
< block
->lines
+ block
->len
;
3048 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3051 for (span
= line
->first_span
; span
; span
= span
->next
) {
3055 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3058 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3060 fz_text_char_bbox (&bbox
, span
, charnum
);
3063 if (x
>= b
->x0
&& x
<= b
->x1
3064 && y
>= b
->y0
&& y
<= b
->y1
) {
3065 fz_text_style
*style
= span
->text
->style
;
3067 style
->font
&& style
->font
->name
3069 : "Span has no font name"
3071 FT_FaceRec
*face
= style
->font
->ft_face
;
3072 if (face
&& face
->family_name
) {
3074 char *n1
= face
->family_name
;
3075 size_t l1
= strlen (n1
);
3076 size_t l2
= strlen (n2
);
3078 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3079 s
= malloc (l1
+ l2
+ 2);
3083 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3084 str_v
= caml_copy_string (s
);
3090 str_v
= caml_copy_string (n2
);
3092 ret_v
= caml_alloc_small (1, utext
);
3093 Field (ret_v
, 0) = str_v
;
3102 unlock ("ml_whatsunder");
3108 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3110 static int uninteresting (int c
)
3112 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3116 CAMLprim value
ml_clearmark (value ptr_v
)
3119 char *s
= String_val (ptr_v
);
3122 if (trylock ("ml_clearmark")) {
3126 page
= parse_pointer ("ml_clearmark", s
);
3127 page
->fmark
.span
= NULL
;
3128 page
->lmark
.span
= NULL
;
3132 unlock ("ml_clearmark");
3134 CAMLreturn (Val_unit
);
3137 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3139 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3144 fz_page_block
*pageb
;
3145 fz_text_block
*block
;
3146 struct pagedim
*pdim
;
3147 int mark
= Int_val (mark_v
);
3148 char *s
= String_val (ptr_v
);
3149 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3151 ret_v
= Val_bool (0);
3152 if (trylock ("ml_markunder")) {
3156 page
= parse_pointer ("ml_markunder", s
);
3157 pdim
= &state
.pagedims
[page
->pdimno
];
3161 if (mark
== mark_page
) {
3163 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3165 for (i
= 0; i
< page
->text
->len
; ++i
) {
3166 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3167 pb1
= &page
->text
->blocks
[i
];
3171 if (!pb1
) goto unlock
;
3173 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3174 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3175 pb2
= &page
->text
->blocks
[i
];
3179 if (!pb2
) goto unlock
;
3181 block
= pb1
->u
.text
;
3184 page
->fmark
.span
= block
->lines
->first_span
;
3186 block
= pb2
->u
.text
;
3187 line
= &block
->lines
[block
->len
- 1];
3188 page
->lmark
.i
= line
->last_span
->len
- 1;
3189 page
->lmark
.span
= line
->last_span
;
3190 ret_v
= Val_bool (1);
3194 x
+= pdim
->bounds
.x0
;
3195 y
+= pdim
->bounds
.y0
;
3197 for (pageb
= page
->text
->blocks
;
3198 pageb
< page
->text
->blocks
+ page
->text
->len
;
3200 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3201 block
= pageb
->u
.text
;
3204 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3207 if (mark
== mark_block
) {
3209 page
->fmark
.span
= block
->lines
->first_span
;
3211 line
= &block
->lines
[block
->len
- 1];
3212 page
->lmark
.i
= line
->last_span
->len
- 1;
3213 page
->lmark
.span
= line
->last_span
;
3214 ret_v
= Val_bool (1);
3218 for (line
= block
->lines
;
3219 line
< block
->lines
+ block
->len
;
3224 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3227 if (mark
== mark_line
) {
3229 page
->fmark
.span
= line
->first_span
;
3231 page
->lmark
.i
= line
->last_span
->len
- 1;
3232 page
->lmark
.span
= line
->last_span
;
3233 ret_v
= Val_bool (1);
3237 for (span
= line
->first_span
; span
; span
= span
->next
) {
3241 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3244 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3246 fz_text_char_bbox (&bbox
, span
, charnum
);
3249 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3251 int charnum2
, charnum3
= -1, charnum4
= -1;
3253 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3255 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3256 if (uninteresting (span
->text
[charnum2
].c
)) {
3257 charnum3
= charnum2
+ 1;
3261 if (charnum3
== -1) charnum3
= 0;
3263 for (charnum2
= charnum
+ 1;
3264 charnum2
< span
->len
;
3266 if (uninteresting (span
->text
[charnum2
].c
)) break;
3267 charnum4
= charnum2
;
3269 if (charnum4
== -1) goto unlock
;
3271 page
->fmark
.i
= charnum3
;
3272 page
->fmark
.span
= span
;
3274 page
->lmark
.i
= charnum4
;
3275 page
->lmark
.span
= span
;
3276 ret_v
= Val_bool (1);
3284 if (!Bool_val (ret_v
)) {
3285 page
->fmark
.span
= NULL
;
3286 page
->lmark
.span
= NULL
;
3290 unlock ("ml_markunder");
3296 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3298 CAMLparam3 (ptr_v
, x_v
, y_v
);
3299 CAMLlocal2 (ret_v
, res_v
);
3302 fz_page_block
*pageb
;
3303 struct pagedim
*pdim
;
3304 char *s
= String_val (ptr_v
);
3305 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3307 ret_v
= Val_int (0);
3308 if (trylock ("ml_rectofblock")) {
3312 page
= parse_pointer ("ml_rectofblock", s
);
3313 pdim
= &state
.pagedims
[page
->pdimno
];
3314 x
+= pdim
->bounds
.x0
;
3315 y
+= pdim
->bounds
.y0
;
3319 for (pageb
= page
->text
->blocks
;
3320 pageb
< page
->text
->blocks
+ page
->text
->len
;
3322 switch (pageb
->type
) {
3323 case FZ_PAGE_BLOCK_TEXT
:
3324 b
= &pageb
->u
.text
->bbox
;
3327 case FZ_PAGE_BLOCK_IMAGE
:
3328 b
= &pageb
->u
.image
->bbox
;
3335 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3340 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3341 ret_v
= caml_alloc_small (1, 1);
3342 Store_double_field (res_v
, 0, b
->x0
);
3343 Store_double_field (res_v
, 1, b
->x1
);
3344 Store_double_field (res_v
, 2, b
->y0
);
3345 Store_double_field (res_v
, 3, b
->y1
);
3346 Field (ret_v
, 0) = res_v
;
3348 unlock ("ml_rectofblock");
3354 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3356 CAMLparam2 (ptr_v
, rect_v
);
3359 struct pagedim
*pdim
;
3360 char *s
= String_val (ptr_v
);
3361 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3363 fz_page_block
*pageb
;
3364 fz_text_block
*block
;
3365 fz_text_span
*span
, *fspan
, *lspan
;
3367 if (trylock ("ml_seltext")) {
3371 page
= parse_pointer ("ml_seltext", s
);
3374 pdim
= &state
.pagedims
[page
->pdimno
];
3375 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3376 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3377 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3378 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3389 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3390 glColor3ub (128, 128, 128);
3391 recti (x0
, y0
, x1
, y1
);
3392 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3396 fspan
= page
->fmark
.span
;
3399 lspan
= page
->lmark
.span
;
3401 for (pageb
= page
->text
->blocks
;
3402 pageb
< page
->text
->blocks
+ page
->text
->len
;
3404 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3405 block
= pageb
->u
.text
;
3406 for (line
= block
->lines
;
3407 line
< block
->lines
+ block
->len
;
3411 for (span
= line
->first_span
; span
; span
= span
->next
) {
3412 for (i
= 0; i
< span
->len
; ++i
) {
3415 fz_text_char_bbox (&b
, span
, i
);
3417 if (x0
>= b
.x0
&& x0
<= b
.x1
3418 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3423 if (x1
>= b
.x0
&& x1
<= b
.x1
3424 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3429 if (0 && selected
) {
3430 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3431 glColor3ub (128, 128, 128);
3432 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3433 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3439 if (x1
< x0
&& fspan
== lspan
) {
3451 page
->fmark
.span
= fspan
;
3454 page
->lmark
.span
= lspan
;
3456 unlock ("ml_seltext");
3459 CAMLreturn (Val_unit
);
3462 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3467 for (i
= a
; i
<= b
; ++i
) {
3468 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3469 ret
= fwrite (buf
, len
, 1, f
);
3472 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3473 len
, ret
, strerror (errno
));
3481 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3483 caml_failwith ("ml_popen not implemented under Cygwin");
3486 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3488 CAMLparam2 (command_v
, fds_v
);
3489 CAMLlocal2 (l_v
, tup_v
);
3492 value earg_v
= Nothing
;
3493 posix_spawnattr_t attr
;
3494 posix_spawn_file_actions_t fa
;
3495 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3497 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3498 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3501 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3502 msg
= "posix_spawnattr_init";
3506 #ifdef POSIX_SPAWN_USEVFORK
3507 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3508 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3513 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3516 tup_v
= Field (l_v
, 0);
3517 fd1
= Int_val (Field (tup_v
, 0));
3518 fd2
= Int_val (Field (tup_v
, 1));
3520 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3521 msg
= "posix_spawn_file_actions_addclose";
3527 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3528 msg
= "posix_spawn_file_actions_adddup2";
3535 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3536 msg
= "posix_spawn";
3541 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3542 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3546 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3547 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3552 unix_error (ret
, msg
, earg_v
);
3554 CAMLreturn (Val_unit
);
3558 CAMLprim value
ml_hassel (value ptr_v
)
3563 char *s
= String_val (ptr_v
);
3565 ret_v
= Val_bool (0);
3566 if (trylock ("ml_hassel")) {
3570 page
= parse_pointer ("ml_hassel", s
);
3571 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3572 unlock ("ml_hassel");
3577 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3579 CAMLparam2 (fd_v
, ptr_v
);
3584 fz_page_block
*pageb
;
3585 fz_text_block
*block
;
3586 int fd
= Int_val (fd_v
);
3587 char *s
= String_val (ptr_v
);
3589 if (trylock ("ml_copysel")) {
3593 page
= parse_pointer ("ml_copysel", s
);
3595 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3596 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3600 f
= fdopen (fd
, "w");
3602 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3603 fd
, strerror (errno
));
3607 for (pageb
= page
->text
->blocks
;
3608 pageb
< page
->text
->blocks
+ page
->text
->len
;
3610 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3611 block
= pageb
->u
.text
;
3612 for (line
= block
->lines
;
3613 line
< block
->lines
+ block
->len
;
3617 for (span
= line
->first_span
; span
; span
= span
->next
) {
3620 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3621 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3622 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3625 if (pipespan (f
, span
, a
, b
)) {
3628 if (span
== page
->lmark
.span
) {
3631 if (span
== line
->last_span
) {
3632 if (putc ('\n', f
) == EOF
) {
3634 "failed break line on sel pipe: %s\n",
3645 int ret
= fclose (f
);
3648 if (errno
!= ECHILD
) {
3649 fprintf (stderr
, "failed to close sel pipe: %s\n",
3655 unlock ("ml_copysel");
3660 fprintf (stderr
, "failed to close sel pipe: %s\n",
3664 CAMLreturn (Val_unit
);
3667 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3669 CAMLparam1 (pagedimno_v
);
3671 int pagedimno
= Int_val (pagedimno_v
);
3674 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3675 if (trylock ("ml_getpdimrect")) {
3676 box
= fz_empty_rect
;
3679 box
= state
.pagedims
[pagedimno
].mediabox
;
3680 unlock ("ml_getpdimrect");
3683 Store_double_field (ret_v
, 0, box
.x0
);
3684 Store_double_field (ret_v
, 1, box
.x1
);
3685 Store_double_field (ret_v
, 2, box
.y0
);
3686 Store_double_field (ret_v
, 3, box
.y1
);
3691 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3692 value dw_v
, value cols_v
)
3694 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3700 double winw
= Int_val (winw_v
);
3701 double winh
= Int_val (winh_v
);
3702 double dw
= Int_val (dw_v
);
3703 double cols
= Int_val (cols_v
);
3704 double pw
= 1.0, ph
= 1.0;
3706 if (trylock ("ml_zoom_for_height")) {
3710 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3711 double w
= p
->pagebox
.x1
/ cols
;
3712 double h
= p
->pagebox
.y1
;
3716 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3718 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3721 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3722 unlock ("ml_zoom_for_height");
3724 ret_v
= caml_copy_double (zoom
);
3728 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3730 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3732 int pt
= Int_val(pt_v
);
3733 int x
= Int_val (x_v
);
3734 int y
= Int_val (y_v
);
3737 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3738 ret_v
= caml_copy_double (w
);
3742 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3744 CAMLparam2 (pt_v
, string_v
);
3746 int pt
= Int_val (pt_v
);
3749 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3750 ret_v
= caml_copy_double (w
);
3754 CAMLprim value
ml_getpagebox (value opaque_v
)
3756 CAMLparam1 (opaque_v
);
3762 char *s
= String_val (opaque_v
);
3763 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3765 ret_v
= caml_alloc_tuple (4);
3766 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3767 dev
->hints
|= FZ_IGNORE_SHADE
;
3769 switch (page
->type
) {
3771 ctm
= pagectm (page
);
3772 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3776 ctm
= pagectm (page
);
3777 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3781 rect
= fz_infinite_rect
;
3785 fz_free_device (dev
);
3786 fz_round_rect (&bbox
, &rect
);
3787 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3788 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3789 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3790 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3795 CAMLprim value
ml_setaalevel (value level_v
)
3797 CAMLparam1 (level_v
);
3799 state
.aalevel
= Int_val (level_v
);
3800 CAMLreturn (Val_unit
);
3804 #include <X11/Xlib.h>
3810 GLXDrawable drawable
;
3813 #include "keysym2ucs.c"
3815 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3817 CAMLparam1 (keysym_v
);
3819 KeySym keysym
= Int_val (keysym_v
);
3824 rune
= keysym2ucs (keysym
);
3825 len
= fz_runetochar (buf
, rune
);
3827 str_v
= caml_copy_string (buf
);
3831 CAMLprim value
ml_glx (value win_v
)
3834 XVisualInfo
*visual
;
3835 int screen
, wid
= Int_val (win_v
);
3836 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3838 glx
.dpy
= XOpenDisplay (NULL
);
3840 caml_failwith ("XOpenDisplay");
3843 screen
= DefaultScreen (glx
.dpy
);
3844 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3846 XCloseDisplay (glx
.dpy
);
3848 caml_failwith ("glXChooseVisual");
3851 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3854 XCloseDisplay (glx
.dpy
);
3856 caml_failwith ("glXCreateContext");
3859 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3860 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3861 XCloseDisplay (glx
.dpy
);
3864 caml_failwith ("glXMakeCurrent");
3867 CAMLreturn (Val_unit
);
3870 CAMLprim value
ml_swapb (value unit_v
)
3872 CAMLparam1 (unit_v
);
3873 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3874 CAMLreturn (Val_unit
);
3877 CAMLprim value
ml_glxsync (value unit_v
)
3879 CAMLparam1 (unit_v
);
3880 if (glx
.dpy
&& glx
.ctx
) {
3884 CAMLreturn (Val_unit
);
3887 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3888 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3890 CAMLprim value
ml_platform (value unit_v
)
3892 CAMLparam1 (unit_v
);
3893 int platid
= piunknown
;
3895 #if defined __linux__
3897 #elif defined __CYGWIN__
3899 #elif defined __DragonFly__
3900 platid
= pidragonflybsd
;
3901 #elif defined __FreeBSD__
3903 #elif defined __OpenBSD__
3905 #elif defined __NetBSD__
3907 #elif defined __sun__
3909 #elif defined __APPLE__
3912 CAMLreturn (Val_int (platid
));
3915 CAMLprim value
ml_cloexec (value fd_v
)
3918 int fd
= Int_val (fd_v
);
3920 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3921 uerror ("fcntl", Nothing
);
3923 CAMLreturn (Val_unit
);
3926 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3928 CAMLparam2 (w_v
, h_v
);
3931 int w
= Int_val (w_v
);
3932 int h
= Int_val (h_v
);
3933 int cs
= Int_val (cs_v
);
3935 if (state
.pbo_usable
) {
3936 pbo
= calloc (sizeof (*pbo
), 1);
3938 err (1, "calloc pbo");
3950 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3953 state
.glGenBuffersARB (1, &pbo
->id
);
3954 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3955 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3956 NULL
, GL_STREAM_DRAW
);
3957 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3959 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3961 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3962 state
.glDeleteBuffersARB (1, &pbo
->id
);
3964 ret_v
= caml_copy_string ("0");
3970 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3972 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3976 err (1, "malloc %d bytes failed", res
+1);
3978 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3980 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3982 ret_v
= caml_copy_string (s
);
3987 ret_v
= caml_copy_string ("0");
3992 CAMLprim value
ml_freepbo (value s_v
)
3995 char *s
= String_val (s_v
);
3996 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3999 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4001 tile
->pbo
->ptr
= NULL
;
4002 tile
->pbo
->size
= -1;
4004 CAMLreturn (Val_unit
);
4007 CAMLprim value
ml_unmappbo (value s_v
)
4010 char *s
= String_val (s_v
);
4011 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4014 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4015 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4016 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4018 tile
->pbo
->ptr
= NULL
;
4019 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4021 CAMLreturn (Val_unit
);
4024 static void setuppbo (void)
4026 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4027 state
.pbo_usable
= GGPA (glBindBufferARB
)
4028 && GGPA (glUnmapBufferARB
)
4029 && GGPA (glMapBufferARB
)
4030 && GGPA (glBufferDataARB
)
4031 && GGPA (glGenBuffersARB
)
4032 && GGPA (glDeleteBuffersARB
);
4036 CAMLprim value
ml_pbo_usable (value unit_v
)
4038 CAMLparam1 (unit_v
);
4039 CAMLreturn (Val_bool (state
.pbo_usable
));
4042 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4044 CAMLparam3 (ptr_v
, x_v
, y_v
);
4045 CAMLlocal2 (ret_v
, tup_v
);
4047 char *s
= String_val (ptr_v
);
4048 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4049 struct pagedim
*pdim
;
4053 page
= parse_pointer ("ml_unproject", s
);
4054 pdim
= &state
.pagedims
[page
->pdimno
];
4056 ret_v
= Val_int (0);
4057 if (trylock ("ml_unproject")) {
4061 switch (page
->type
) {
4063 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4069 p
.x
= x
+ pdim
->bounds
.x0
;
4070 p
.y
= y
+ pdim
->bounds
.y0
;
4072 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4073 fz_invert_matrix (&ctm
, &ctm
);
4074 fz_transform_point (&p
, &ctm
);
4076 tup_v
= caml_alloc_tuple (2);
4077 ret_v
= caml_alloc_small (1, 1);
4078 Field (tup_v
, 0) = Val_int (p
.x
);
4079 Field (tup_v
, 1) = Val_int (p
.y
);
4080 Field (ret_v
, 0) = tup_v
;
4082 unlock ("ml_unproject");
4087 static void makestippletex (void)
4089 const char pixels
[] = "\xff\xff\0\0";
4090 glGenTextures (1, &state
.stid
);
4091 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4092 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4093 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4106 CAMLprim value
ml_fz_version (value unit_v
)
4108 return caml_copy_string (FZ_VERSION
);
4111 #ifdef USE_FONTCONFIG
4117 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4121 int needs_exact_metrics
)
4128 FcPattern
*pat
, *pat1
;
4130 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4131 name
, bold
, italic
, needs_exact_metrics
);
4134 fc
.config
= FcInitLoadConfigAndFonts ();
4136 lprintf ("FcInitLoadConfigAndFonts failed\n");
4140 if (!fc
.config
) return NULL
;
4142 size
= strlen (name
);
4143 if (bold
) size
+= sizeof (":bold") - 1;
4144 if (italic
) size
+= sizeof (":italic") - 1;
4147 buf
= malloc (size
);
4149 err (1, "malloc %zu failed", size
);
4153 if (bold
&& italic
) {
4154 strcat (buf
, ":bold:italic");
4157 if (bold
) strcat (buf
, ":bold");
4158 if (italic
) strcat (buf
, ":italic");
4160 for (i
= 0; i
< size
; ++i
) {
4161 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4164 lprintf ("fcbuf=%s\n", buf
);
4165 pat
= FcNameParse ((FcChar8
*) buf
);
4167 printd ("emsg FcNameParse failed\n");
4172 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4173 printd ("emsg FcConfigSubstitute failed\n");
4177 FcDefaultSubstitute (pat
);
4179 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4181 printd ("emsg FcFontMatch failed\n");
4182 FcPatternDestroy (pat
);
4187 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4188 printd ("emsg FcPatternGetString failed\n");
4189 FcPatternDestroy (pat
);
4190 FcPatternDestroy (pat1
);
4196 printd ("emsg name=%s path=%s\n", name
, path
);
4198 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4199 FcPatternDestroy (pat
);
4200 FcPatternDestroy (pat1
);
4206 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4208 CAMLparam2 (pipe_v
, params_v
);
4209 CAMLlocal2 (trim_v
, fuzz_v
);
4216 struct sigaction sa
;
4218 state
.cr
= Int_val (Field (pipe_v
, 0));
4219 state
.cw
= Int_val (Field (pipe_v
, 1));
4220 state
.rotate
= Int_val (Field (params_v
, 0));
4221 state
.fitmodel
= Int_val (Field (params_v
, 1));
4222 trim_v
= Field (params_v
, 2);
4223 texcount
= Int_val (Field (params_v
, 3));
4224 state
.sliceheight
= Int_val (Field (params_v
, 4));
4225 mustoresize
= Int_val (Field (params_v
, 5));
4226 colorspace
= Int_val (Field (params_v
, 6));
4227 fontpath
= String_val (Field (params_v
, 7));
4229 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4230 if (!state
.trimcachepath
) {
4231 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4234 haspboext
= Bool_val (Field (params_v
, 9));
4236 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4238 #ifdef USE_FONTCONFIG
4239 fz_install_load_system_font_funcs (state
.ctx
, fc_load_system_font_func
, NULL
);
4242 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4243 fuzz_v
= Field (trim_v
, 1);
4244 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4245 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4246 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4247 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4249 set_tex_params (colorspace
);
4252 state
.face
= load_font (fontpath
);
4256 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4258 state
.face
= load_builtin_font (base
, len
);
4260 if (!state
.face
) _exit (1);
4262 realloctexts (texcount
);
4271 sa
.sa_handler
= SIG_IGN
;
4272 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4274 sa
.sa_handler
= SIG_DFL
;
4275 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4277 if (sigemptyset (&sa
.sa_mask
)) {
4278 err (1, "sigemptyset");
4280 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4281 err (1, "sigaction");
4284 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4286 errx (1, "pthread_create: %s", strerror (ret
));
4289 CAMLreturn (Val_unit
);