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>
35 #include <mupdf-internal.h>
37 #include <muxps-internal.h>
40 #include FT_FREETYPE_H
45 extern char **environ
;
48 #define MIN(a,b) ((a) < (b) ? (a) : (b))
49 #define MAX(a,b) ((a) > (b) ? (a) : (b))
52 #define NORETURN_ATTR __attribute__ ((noreturn))
53 #define UNUSED_ATTR __attribute__ ((unused))
54 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
55 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
59 #define OPTIMIZE_ATTR(n)
60 #define GCC_FMT_ATTR(a, b)
66 #define FMT_ptr_cast(p) (p)
67 #define FMT_ptr_cast2(p) (p)
69 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
70 err (int exitcode
, const char *fmt
, ...)
77 vfprintf (stderr
, fmt
, ap
);
79 fprintf (stderr
, ": %s\n", strerror (savederrno
));
84 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
85 errx (int exitcode
, const char *fmt
, ...)
90 vfprintf (stderr
, fmt
, ap
);
97 #ifndef GL_TEXTURE_RECTANGLE_ARB
98 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
102 #define GL_BGRA 0x80E1
105 #ifndef GL_UNSIGNED_INT_8_8_8_8
106 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
109 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
110 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
114 #define lprintf printf
119 #define ARSERT(cond) for (;;) { \
121 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
137 struct slice slices
[1];
148 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
156 enum { DPDF
, DXPS
, DCBZ
};
165 fz_text_sheet
*sheet
;
172 fz_display_list
*dlist
;
174 struct slink
*slinks
;
179 void (*freepage
) (void *);
185 struct pagedim
*pagedims
;
204 fz_colorspace
*colorspace
;
212 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
226 void (*closedoc
) (void);
227 void (*freepage
) (void *);
235 void (*glBindBufferARB
) (GLenum
, GLuint
);
236 GLboolean (*glUnmapBufferARB
) (GLenum
);
237 void *(*glMapBufferARB
) (GLenum
, GLenum
);
238 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
239 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
240 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
249 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
251 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
254 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
256 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
259 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
261 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
262 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
265 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
267 static void lock (const char *cap
)
269 int ret
= pthread_mutex_lock (&mutex
);
271 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
275 static void unlock (const char *cap
)
277 int ret
= pthread_mutex_unlock (&mutex
);
279 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
283 static int trylock (const char *cap
)
285 int ret
= pthread_mutex_trylock (&mutex
);
286 if (ret
&& ret
!= EBUSY
) {
287 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
292 static void *parse_pointer (const char *cap
, const char *s
)
297 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
299 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
304 static double now (void)
308 if (gettimeofday (&tv
, NULL
)) {
309 err (1, "gettimeofday");
311 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
314 static int hasdata (void)
317 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
318 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
322 CAMLprim value
ml_hasdata (value fd_v
)
327 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
328 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
329 CAMLreturn (Val_bool (avail
> 0));
332 static void readdata (void *p
, int size
)
337 n
= read (state
.cr
, p
, size
);
339 if (errno
== EINTR
) goto again
;
340 err (1, "read (req %d, ret %zd)", size
, n
);
343 if (!n
) errx (1, "EOF while reading");
344 errx (1, "read (req %d, ret %zd)", size
, n
);
348 static void writedata (char *p
, int size
)
353 buf
[0] = (size
>> 24) & 0xff;
354 buf
[1] = (size
>> 16) & 0xff;
355 buf
[2] = (size
>> 8) & 0xff;
356 buf
[3] = (size
>> 0) & 0xff;
358 n
= write (state
.cw
, buf
, 4);
360 if (!n
) errx (1, "EOF while writing length");
361 err (1, "write %zd", n
);
364 n
= write (state
.cw
, p
, size
);
366 if (!n
) errx (1, "EOF while writing data");
367 err (1, "write (req %d, ret %zd)", size
, n
);
371 static int readlen (void)
376 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
379 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
387 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
390 len
= vsnprintf (buf
, size
, fmt
, ap
);
393 if (len
> -1 && len
< size
) {
394 writedata (buf
, len
);
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 freepdfpage (void *ptr
)
435 pdf_free_page (state
.u
.pdf
, ptr
);
438 static void freeemptyxxxpage (void *ptr
)
443 static void freexpspage (void *ptr
)
445 xps_free_page (state
.u
.xps
, ptr
);
448 static void freecbzpage (void *ptr
)
450 cbz_free_page (state
.u
.cbz
, ptr
);
453 static void openxref (char *filename
, char *password
)
457 for (i
= 0; i
< state
.texcount
; ++i
) {
458 state
.texowners
[i
].w
= -1;
459 state
.texowners
[i
].slice
= NULL
;
462 if (state
.closedoc
) state
.closedoc ();
464 len
= strlen (filename
);
470 ext
[0] = tolower ((int) filename
[len
-3]);
471 ext
[1] = tolower ((int) filename
[len
-2]);
472 ext
[2] = tolower ((int) filename
[len
-1]);
474 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
477 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
482 if (state
.pagedims
) {
483 free (state
.pagedims
);
484 state
.pagedims
= NULL
;
486 state
.pagedimcount
= 0;
488 fz_set_aa_level (state
.ctx
, state
.aalevel
);
489 switch (state
.type
) {
491 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
492 if (pdf_needs_password (state
.u
.pdf
)) {
493 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
495 errx (1, "invalid password");
498 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
499 state
.closedoc
= closepdf
;
500 state
.freepage
= freepdfpage
;
504 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
505 state
.pagecount
= xps_count_pages (state
.u
.xps
);
506 state
.closedoc
= closexps
;
507 state
.freepage
= freexpspage
;
511 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
512 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
513 state
.closedoc
= closecbz
;
514 state
.freepage
= freecbzpage
;
519 static void pdfinfo (void)
521 if (state
.type
== DPDF
) {
524 printd ("info PDF version\t%d.%d",
525 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
527 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
531 char *items
[] = { "Title", "Author", "Creator",
532 "Producer", "CreationDate" };
534 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
535 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
536 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
539 printd ("title %s", s
);
541 printd ("info %s\t%s", items
[i
], s
);
543 fz_free (state
.ctx
, s
);
550 static void unlinktile (struct tile
*tile
)
554 for (i
= 0; i
< tile
->slicecount
; ++i
) {
555 struct slice
*s
= &tile
->slices
[i
];
557 if (s
->texindex
!= -1) {
558 if (state
.texowners
[s
->texindex
].slice
== s
) {
559 state
.texowners
[s
->texindex
].slice
= NULL
;
565 static void freepage (struct page
*page
)
568 fz_free_text_page (state
.ctx
, page
->text
);
571 fz_free_text_sheet (state
.ctx
, page
->sheet
);
576 page
->freepage (page
->u
.ptr
);
577 fz_free_display_list (state
.ctx
, page
->dlist
);
581 static void freetile (struct tile
*tile
)
586 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
589 fz_drop_pixmap (state
.ctx
, state
.pig
);
591 state
.pig
= tile
->pixmap
;
596 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
605 static int cacheline32bytes
;
607 static void __attribute__ ((constructor
)) clcheck (void)
609 char **envp
= environ
;
614 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
616 cacheline32bytes
= auxv
[1] == 32;
622 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
624 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
625 if (cacheline32bytes
&& size
> 32) {
626 intptr_t a1
, a2
, diff
;
628 vector
unsigned char v
= vec_splat_u8 (-1);
629 vector
unsigned char *p
;
631 a1
= a2
= (intptr_t) pixmap
->samples
;
632 a2
= (a1
+ 31) & ~31;
637 while (a1
!= a2
) *(char *) a1
++ = 0xff;
638 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
639 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
641 vec_st (v
, i
+ 16, p
);
643 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
645 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
648 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
651 static void trimctm (pdf_page
*page
, int pindex
)
654 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
656 if (!pdim
->tctmready
) {
657 if (state
.trimmargins
) {
659 fz_matrix rm
, sm
, tm
, im
, ctm1
;
661 fz_rotate (&rm
, -pdim
->rotate
);
662 fz_scale (&sm
, 1, -1);
663 fz_concat (&ctm
, &rm
, &sm
);
664 realbox
= pdim
->mediabox
;
665 fz_transform_rect (&realbox
, &ctm
);
666 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
667 fz_concat (&ctm1
, &ctm
, &tm
);
668 fz_invert_matrix (&im
, &page
->ctm
);
669 fz_concat (&ctm
, &im
, &ctm1
);
679 static fz_matrix
pagectm (struct page
*page
)
683 if (page
->type
== DPDF
) {
684 trimctm (page
->u
.pdfpage
, page
->pdimno
);
686 &state
.pagedims
[page
->pdimno
].tctm
,
687 &state
.pagedims
[page
->pdimno
].ctm
);
690 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
692 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
693 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
698 static void *loadpage (int pageno
, int pindex
)
701 struct page
*page
= NULL
;
703 page
= calloc (sizeof (struct page
), 1);
705 err (1, "calloc page %d", pageno
);
708 page
->dlist
= fz_new_display_list (state
.ctx
);
709 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
711 switch (state
.type
) {
713 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
714 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
716 page
->freepage
= freepdfpage
;
720 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
721 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
723 page
->freepage
= freexpspage
;
727 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
728 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
730 page
->freepage
= freecbzpage
;
734 fz_catch (state
.ctx
) {
736 page
->freepage
= freeemptyxxxpage
;
738 fz_free_device (dev
);
740 page
->pdimno
= pindex
;
741 page
->pageno
= pageno
;
742 page
->sgen
= state
.gen
;
743 page
->tgen
= state
.gen
;
744 page
->type
= state
.type
;
749 static struct tile
*alloctile (int h
)
756 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
757 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
758 tile
= calloc (tilesize
, 1);
760 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
762 for (i
= 0; i
< slicecount
; ++i
) {
763 int sh
= MIN (h
, state
.sliceheight
);
764 tile
->slices
[i
].h
= sh
;
765 tile
->slices
[i
].texindex
= -1;
768 tile
->slicecount
= slicecount
;
769 tile
->sliceheight
= state
.sliceheight
;
779 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
780 const fz_matrix
*ctm
, float alpha
)
782 struct obs
*obs
= dev
->user
;
784 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
786 fz_rect rect
= fz_unit_rect
;
788 fz_transform_rect (&rect
, ctm
);
789 fz_round_rect (&b
, &rect
);
790 fz_intersect_irect (&b
, &obs
->b
);
791 obs
->cured
= b
.x0
== obs
->b
.x0
794 && b
.y1
== obs
->b
.y1
;
798 static int obscured (struct page
*page
, fz_irect bbox
)
805 memset (&dev
, 0, sizeof (dev
));
806 memset (&obs
, 0, sizeof (obs
));
811 dev
.fill_image
= obs_fill_image
;
813 fz_rect_from_irect (&rect
, &bbox
);
814 ctm
= pagectm (page
);
815 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
818 #define OBSCURED obscured
820 #define OBSCURED(a, b) 0
823 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
831 struct pagedim
*pdim
;
833 tile
= alloctile (h
);
834 pdim
= &state
.pagedims
[page
->pdimno
];
839 bbox
.x1
= bbox
.x0
+ w
;
840 bbox
.y1
= bbox
.y0
+ h
;
843 if (state
.pig
->w
== w
845 && state
.pig
->colorspace
== state
.colorspace
) {
846 tile
->pixmap
= state
.pig
;
847 tile
->pixmap
->x
= bbox
.x0
;
848 tile
->pixmap
->y
= bbox
.y0
;
851 fz_drop_pixmap (state
.ctx
, state
.pig
);
858 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
864 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
870 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
871 clearpixmap (tile
->pixmap
);
873 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
874 ctm
= pagectm (page
);
875 fz_rect_from_irect (&rect
, &bbox
);
876 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
877 fz_free_device (dev
);
882 static void initpdims (void)
885 int pageno
, trim
, show
;
887 int trimw
= 0, cxcount
;
891 if (state
.trimmargins
&& state
.trimcachepath
) {
892 trimf
= fopen (state
.trimcachepath
, "rb");
894 trimf
= fopen (state
.trimcachepath
, "wb");
899 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
900 cxcount
= state
.pagecount
;
902 cxcount
= MIN (state
.pagecount
, 1);
904 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
909 switch (state
.type
) {
911 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
913 if (state
.trimmargins
) {
918 page
= pdf_load_page (state
.u
.pdf
, pageno
);
919 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
920 trim
= state
.trimanew
|| !obj
;
926 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
927 dev
->hints
|= FZ_IGNORE_SHADE
;
928 fz_invert_matrix (&ctm
, &page
->ctm
);
929 pdf_run_page (state
.u
.pdf
, page
, dev
,
931 fz_free_device (dev
);
933 rect
.x0
+= state
.trimfuzz
.x0
;
934 rect
.x1
+= state
.trimfuzz
.x1
;
935 rect
.y0
+= state
.trimfuzz
.y0
;
936 rect
.y1
+= state
.trimfuzz
.y1
;
937 fz_transform_rect (&rect
, &ctm
);
938 fz_intersect_rect (&rect
, &page
->mediabox
);
940 if (fz_is_empty_rect (&rect
)) {
941 mediabox
= page
->mediabox
;
947 obj
= pdf_new_array (state
.ctx
, 4);
949 pdf_new_real (state
.ctx
, mediabox
.x0
));
951 pdf_new_real (state
.ctx
, mediabox
.y0
));
953 pdf_new_real (state
.ctx
, mediabox
.x1
));
955 pdf_new_real (state
.ctx
, mediabox
.y1
));
956 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
959 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
960 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
961 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
962 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
965 rotate
= page
->rotate
;
966 pdf_free_page (state
.u
.pdf
, page
);
968 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
970 printd ("progress %f Trimming %d",
971 (double) (pageno
+ 1) / state
.pagecount
,
975 fz_catch (state
.ctx
) {
976 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
982 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
984 if (fz_is_empty_rect (&mediabox
)) {
985 fprintf (stderr
, "cannot find page size for page %d\n",
993 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
995 if (!fz_is_empty_rect (&cropbox
)) {
996 fz_intersect_rect (&mediabox
, &cropbox
);
998 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1007 fz_try (state
.ctx
) {
1008 page
= xps_load_page (state
.u
.xps
, pageno
);
1009 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1011 if (state
.trimmargins
) {
1015 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1016 dev
->hints
|= FZ_IGNORE_SHADE
;
1017 xps_run_page (state
.u
.xps
, page
, dev
,
1018 &fz_identity
, NULL
);
1019 fz_free_device (dev
);
1021 rect
.x0
+= state
.trimfuzz
.x0
;
1022 rect
.x1
+= state
.trimfuzz
.x1
;
1023 rect
.y0
+= state
.trimfuzz
.y0
;
1024 rect
.y1
+= state
.trimfuzz
.y1
;
1025 fz_intersect_rect (&rect
, &mediabox
);
1027 if (!fz_is_empty_rect (&rect
)) {
1031 xps_free_page (state
.u
.xps
, page
);
1032 printd ("progress %f loading %d",
1033 (double) (pageno
+ 1) / state
.pagecount
,
1036 fz_catch (state
.ctx
) {
1044 if (state
.trimmargins
&& trimw
) {
1047 fz_try (state
.ctx
) {
1048 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1049 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1050 cbz_free_page (state
.u
.cbz
, page
);
1051 printd ("progress %f Trimming %d",
1052 (double) (pageno
+ 1) / state
.pagecount
,
1055 fz_catch (state
.ctx
) {
1056 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1063 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1065 err (1, "fwrite trim mediabox");
1071 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1073 err (1, "fread trim mediabox");
1077 mediabox
.x0
= mediabox
.y0
= 0;
1086 ARSERT (0 && state
.type
);
1089 if (state
.pagedimcount
== 0
1090 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1091 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1094 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1095 state
.pagedims
= realloc (state
.pagedims
, size
);
1096 if (!state
.pagedims
) {
1097 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1098 size
, state
.pagedimcount
+ 1);
1101 p
= &state
.pagedims
[state
.pagedimcount
++];
1103 p
->mediabox
= mediabox
;
1108 if (state
.trimmargins
) {
1109 printd ("progress 1 Trimmed %d pages in %f seconds",
1110 state
.pagecount
, end
- start
);
1113 printd ("vmsg Processed %d pages in %f seconds",
1114 state
.pagecount
, end
- start
);
1118 if (fclose (trimf
)) {
1124 static void layout (void)
1129 struct pagedim
*p
= p
;
1130 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1132 if (state
.pagedimcount
== 0) return;
1134 switch (state
.fitmodel
) {
1135 case FitProportional
:
1136 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1139 p
= &state
.pagedims
[pindex
];
1140 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1142 fz_transform_rect (&box
, &rm
);
1144 x0
= MIN (box
.x0
, box
.x1
);
1145 x1
= MAX (box
.x0
, box
.x1
);
1148 maxw
= MAX (w
, maxw
);
1149 zoom
= state
.w
/ maxw
;
1161 ARSERT (0 && state
.fitmodel
);
1164 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1168 p
= &state
.pagedims
[pindex
];
1169 fz_rotate (&ctm
, state
.rotate
);
1170 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1172 fz_transform_rect (&box
, &rm
);
1173 w
= box
.x1
- box
.x0
;
1174 switch (state
.fitmodel
) {
1175 case FitProportional
:
1176 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1182 h
= box
.y1
- box
.y0
;
1184 zoom
= MIN (zw
, zh
);
1185 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1194 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1195 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1197 fz_rotate (&rm
, p
->rotate
);
1198 p
->pagebox
= p
->mediabox
;
1199 fz_transform_rect (&p
->pagebox
, &rm
);
1200 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1201 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1205 fz_transform_rect (&rect
, &ctm
);
1206 fz_round_rect (&p
->bounds
, &rect
);
1209 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1210 fz_scale (&sm
, zoom
, -zoom
);
1211 fz_concat (&ctm
, &tm
, &sm
);
1212 fz_concat (&p
->lctm
, &ctm
, &rm
);
1218 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1219 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1220 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1221 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1225 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1226 } while (p
-- != state
.pagedims
);
1230 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1231 desttoanchor (fz_link_dest
*dest
)
1235 struct pagedim
*pdim
= state
.pagedims
;
1240 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1241 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1243 pdim
= &state
.pagedims
[i
];
1245 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1247 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1248 p
.x
= dest
->ld
.gotor
.lt
.x
;
1251 p
.y
= dest
->ld
.gotor
.lt
.y
;
1252 fz_transform_point (&p
, &pdim
->lctm
);
1256 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1257 double x0
, x1
, y0
, y1
;
1259 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1260 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1262 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1263 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1265 a
.n
= dest
->ld
.gotor
.page
;
1270 static void recurse_outline (fz_outline
*outline
, int level
)
1273 if (outline
->dest
.kind
== FZ_LINK_GOTO
) {
1274 struct anchor a
= desttoanchor (&outline
->dest
);
1277 printd ("o %d %d %d %d %s",
1278 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1282 printd ("emsg Unhandled outline kind %d for %s\n",
1283 outline
->dest
.kind
, outline
->title
);
1285 if (outline
->down
) {
1286 recurse_outline (outline
->down
, level
+ 1);
1288 outline
= outline
->next
;
1292 static void process_outline (void)
1294 fz_outline
*outline
;
1296 if (!state
.needoutline
) return;
1298 state
.needoutline
= 0;
1299 switch (state
.type
) {
1301 outline
= pdf_load_outline (state
.u
.pdf
);
1304 outline
= xps_load_outline (state
.u
.xps
);
1311 recurse_outline (outline
, 0);
1312 fz_free_outline (state
.ctx
, outline
);
1316 static char *strofspan (fz_text_span
*span
)
1321 size_t size
= 0, cap
= 80;
1323 p
= malloc (cap
+ 1);
1324 if (!p
) return NULL
;
1326 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1327 int n
= fz_runetochar (utf8
, ch
->c
);
1328 if (size
+ n
> cap
) {
1330 p
= realloc (p
, cap
+ 1);
1331 if (!p
) return NULL
;
1334 memcpy (p
+ size
, utf8
, n
);
1341 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1342 fz_text_span
*span
, fz_matrix ctm
,
1343 int stop
, int pageno
, double start
)
1350 fz_point p1
, p2
, p3
, p4
;
1352 p
= strofspan (span
);
1355 ret
= regexec (re
, p
, 1, &rm
, 0);
1358 if (ret
!= REG_NOMATCH
) {
1361 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1362 printd ("msg regexec error `%.*s'",
1363 (int) size
, errbuf
);
1371 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1372 c
+= fz_runelen (span
->text
[a
].c
);
1374 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1375 c
+= fz_runelen (span
->text
[b
].c
);
1378 if (fz_runelen (span
->text
[b
].c
) > 1) {
1382 fz_text_char_bbox (&sb
, span
, a
);
1383 fz_text_char_bbox (&eb
, span
, b
);
1395 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1402 printd ("progress 1 found at %d `%.*s' in %f sec",
1403 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1407 printd ("match %d %d %f %f %f %f %f %f %f %f",
1419 static int compareblocks (const void *l
, const void *r
)
1421 fz_text_block
const *ls
= l
;
1422 fz_text_block
const* rs
= r
;
1423 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1426 /* wishful thinking function */
1427 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1432 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1434 fz_text_sheet
*sheet
;
1435 struct pagedim
*pdim
, *pdimprev
;
1436 int stop
= 0, niters
= 0;
1439 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1443 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1444 if (niters
++ == 5) {
1447 printd ("progress 1 attention requested aborting search at %d",
1452 printd ("progress %f searching in page %d",
1453 (double) (pageno
+ 1) / state
.pagecount
,
1458 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1459 pdim
= &state
.pagedims
[i
];
1460 if (pdim
->pageno
== pageno
) {
1463 if (pdim
->pageno
> pageno
) {
1472 sheet
= fz_new_text_sheet (state
.ctx
);
1473 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1474 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1476 switch (state
.type
) {
1479 fz_try (state
.ctx
) {
1480 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1481 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1482 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1483 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1485 fz_catch (state
.ctx
) {
1486 fz_free_device (tdev
);
1493 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1494 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1498 ARSERT (0 && state
.type
);
1501 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1502 fz_free_device (tdev
);
1504 for (j
= 0; j
< text
->len
; ++j
) {
1507 fz_text_block
*block
;
1509 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1510 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1513 for (k
= 0; k
< block
->len
; ++k
) {
1518 line
= &block
->lines
[k
];
1519 if (line
->bbox
.y0
< y
+ 1) continue;
1522 line
= &block
->lines
[block
->len
- 1 - k
];
1523 if (line
->bbox
.y0
> y
- 1) continue;
1526 for (span
= line
->first_span
; span
; span
= span
->next
) {
1527 switch (matchspan (re
, text
, span
, ctm
,
1528 stop
, pageno
, start
)) {
1530 case 1: stop
= 1; break;
1531 case -1: stop
= 1; goto endloop
;
1546 fz_free_text_page (state
.ctx
, text
);
1547 fz_free_text_sheet (state
.ctx
, sheet
);
1549 state
.freepage (u
.ptr
);
1554 printd ("progress 1 no matches %f sec", end
- start
);
1556 printd ("clearrects");
1559 static void set_tex_params (int colorspace
)
1566 switch (colorspace
) {
1568 state
.texiform
= GL_RGBA8
;
1569 state
.texform
= GL_RGBA
;
1570 state
.texty
= GL_UNSIGNED_BYTE
;
1571 state
.colorspace
= fz_device_rgb (state
.ctx
);
1574 state
.texiform
= GL_RGBA8
;
1575 state
.texform
= GL_BGRA
;
1576 state
.texty
= endianness
.s
> 1
1577 ? GL_UNSIGNED_INT_8_8_8_8
1578 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1579 state
.colorspace
= fz_device_bgr (state
.ctx
);
1582 state
.texiform
= GL_LUMINANCE_ALPHA
;
1583 state
.texform
= GL_LUMINANCE_ALPHA
;
1584 state
.texty
= GL_UNSIGNED_BYTE
;
1585 state
.colorspace
= fz_device_gray (state
.ctx
);
1588 errx (1, "invalid colorspce %d", colorspace
);
1592 static void realloctexts (int texcount
)
1596 if (texcount
== state
.texcount
) return;
1598 if (texcount
< state
.texcount
) {
1599 glDeleteTextures (state
.texcount
- texcount
,
1600 state
.texids
+ texcount
);
1603 size
= texcount
* sizeof (*state
.texids
);
1604 state
.texids
= realloc (state
.texids
, size
);
1605 if (!state
.texids
) {
1606 err (1, "realloc texids %" FMT_s
, size
);
1609 size
= texcount
* sizeof (*state
.texowners
);
1610 state
.texowners
= realloc (state
.texowners
, size
);
1611 if (!state
.texowners
) {
1612 err (1, "realloc texowners %" FMT_s
, size
);
1614 if (texcount
> state
.texcount
) {
1617 glGenTextures (texcount
- state
.texcount
,
1618 state
.texids
+ state
.texcount
);
1619 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1620 state
.texowners
[i
].w
= -1;
1621 state
.texowners
[i
].slice
= NULL
;
1624 state
.texcount
= texcount
;
1628 static char *mbtoutf8 (char *s
)
1634 len
= mbstowcs (NULL
, s
, strlen (s
));
1639 if (len
== (size_t) -1) {
1644 tmp
= malloc (len
* sizeof (wchar_t));
1649 ret
= mbstowcs (tmp
, s
, len
);
1650 if (ret
== (size_t) -1) {
1656 for (i
= 0; i
< ret
; ++i
) {
1657 len
+= fz_runelen (tmp
[i
]);
1660 p
= r
= malloc (len
+ 1);
1666 for (i
= 0; i
< ret
; ++i
) {
1667 p
+= fz_runetochar (p
, tmp
[i
]);
1674 CAMLprim value
ml_mbtoutf8 (value s_v
)
1680 s
= String_val (s_v
);
1686 ret_v
= caml_copy_string (r
);
1692 static void * mainloop (void *unused
)
1695 int len
, ret
, oldlen
= 0;
1700 errx (1, "readlen returned 0");
1703 if (oldlen
< len
+ 1) {
1704 p
= realloc (p
, len
+ 1);
1706 err (1, "realloc %d failed", len
+ 1);
1713 if (!strncmp ("open", p
, 4)) {
1720 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1722 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1725 filename
= p
+ 5 + off
;
1726 filenamelen
= strlen (filename
);
1727 password
= filename
+ filenamelen
+ 1;
1730 openxref (filename
, password
);
1736 utf8filename
= mbtoutf8 (filename
);
1737 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1738 if (utf8filename
!= filename
) {
1739 free (utf8filename
);
1742 state
.needoutline
= 1;
1744 else if (!strncmp ("cs", p
, 2)) {
1747 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1749 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1752 set_tex_params (colorspace
);
1753 for (i
= 0; i
< state
.texcount
; ++i
) {
1754 state
.texowners
[i
].w
= -1;
1755 state
.texowners
[i
].slice
= NULL
;
1759 else if (!strncmp ("freepage", p
, 8)) {
1762 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1764 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1768 else if (!strncmp ("freetile", p
, 8)) {
1771 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1773 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1777 else if (!strncmp ("search", p
, 6)) {
1778 int icase
, pageno
, y
, ret
, len2
, forward
;
1782 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1783 &icase
, &pageno
, &y
, &forward
, &len2
);
1785 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1788 pattern
= p
+ 6 + len2
;
1789 ret
= regcomp (&re
, pattern
,
1790 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1795 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1796 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1799 search (&re
, pageno
, y
, forward
);
1803 else if (!strncmp ("geometry", p
, 8)) {
1807 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1809 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1817 for (i
= 0; i
< state
.texcount
; ++i
) {
1818 state
.texowners
[i
].slice
= NULL
;
1821 state
.fitmodel
= fitmodel
;
1826 unlock ("geometry");
1827 printd ("continue %d", state
.pagecount
);
1829 else if (!strncmp ("reqlayout", p
, 9)) {
1831 int rotate
, fitmodel
, off
, h
;
1834 ret
= sscanf (p
+ 9, " %d %d %d %n",
1835 &rotate
, &fitmodel
, &h
, &off
);
1837 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1840 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1843 state
.rotate
= rotate
;
1844 state
.fitmodel
= fitmodel
;
1849 nameddest
= p
+ 9 + off
;
1850 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1853 pdf_obj
*needle
, *obj
;
1855 needle
= pdf_new_string (state
.ctx
, nameddest
,
1856 strlen (nameddest
));
1857 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1859 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1861 a
= desttoanchor (&dest
);
1863 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1866 printd ("emsg failed to parse destination `%s'\n",
1871 printd ("emsg destination `%s' not found\n",
1874 pdf_drop_obj (needle
);
1878 unlock ("reqlayout");
1879 printd ("continue %d", state
.pagecount
);
1881 else if (!strncmp ("page", p
, 4)) {
1884 int pageno
, pindex
, ret
;
1886 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1888 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1893 page
= loadpage (pageno
, pindex
);
1897 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1899 else if (!strncmp ("tile", p
, 4)) {
1900 int x
, y
, w
, h
, ret
;
1906 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1907 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1909 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1914 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1918 printd ("tile %d %d %" FMT_ptr
" %u %f",
1920 FMT_ptr_cast2 (tile
),
1921 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1924 else if (!strncmp ("settrim", p
, 7)) {
1928 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1929 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1931 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1935 state
.trimmargins
= trimmargins
;
1936 state
.needoutline
= 1;
1937 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1939 state
.trimfuzz
= fuzz
;
1941 state
.pagedimcount
= 0;
1942 free (state
.pagedims
);
1943 state
.pagedims
= NULL
;
1948 printd ("continue %d", state
.pagecount
);
1950 else if (!strncmp ("sliceh", p
, 6)) {
1953 ret
= sscanf (p
+ 6, " %d", &h
);
1955 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1957 if (h
!= state
.sliceheight
) {
1960 state
.sliceheight
= h
;
1961 for (i
= 0; i
< state
.texcount
; ++i
) {
1962 state
.texowners
[i
].w
= -1;
1963 state
.texowners
[i
].h
= -1;
1964 state
.texowners
[i
].slice
= NULL
;
1968 else if (!strncmp ("interrupt", p
, 9)) {
1969 printd ("vmsg interrupted");
1972 errx (1, "unknown command %.*s", len
, p
);
1978 CAMLprim value
ml_realloctexts (value texcount_v
)
1980 CAMLparam1 (texcount_v
);
1983 if (trylock ("ml_realloctexts")) {
1987 realloctexts (Int_val (texcount_v
));
1989 unlock ("ml_realloctexts");
1992 CAMLreturn (Val_bool (ok
));
1995 static void showsel (struct page
*page
, int ox
, int oy
)
2001 fz_page_block
*pageb
;
2002 fz_text_block
*block
;
2003 struct mark first
, last
;
2005 first
= page
->fmark
;
2008 if (!first
.span
|| !last
.span
) return;
2010 glEnable (GL_BLEND
);
2011 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2012 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
2014 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2015 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2016 for (pageb
= page
->text
->blocks
;
2017 pageb
< page
->text
->blocks
+ page
->text
->len
;
2019 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2020 block
= pageb
->u
.text
;
2022 for (line
= block
->lines
;
2023 line
< block
->lines
+ block
->len
;
2026 rect
= fz_empty_rect
;
2028 for (span
= line
->first_span
; span
; span
= span
->next
) {
2030 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2035 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2037 j
= MIN (first
.i
, last
.i
);
2038 k
= MAX (first
.i
, last
.i
);
2040 else if (span
== first
.span
) {
2044 else if (span
== last
.span
) {
2050 for (i
= j
; i
<= k
; ++i
) {
2052 fz_union_rect (&rect
,
2053 fz_text_char_bbox (&bbox
, span
, i
));
2055 fz_round_rect (&bbox
, &rect
);
2056 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2063 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2064 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2065 if (span
== last
.span
) {
2073 glDisable (GL_BLEND
);
2078 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2080 fz_matrix ctm
, tm
, pm
;
2081 fz_link
*link
, *links
;
2083 switch (page
->type
) {
2085 links
= page
->u
.pdfpage
->links
;
2089 links
= page
->u
.xpspage
->links
;
2096 glEnable (GL_TEXTURE_1D
);
2097 glEnable (GL_BLEND
);
2098 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2100 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2101 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2102 fz_translate (&tm
, xoff
, yoff
);
2103 pm
= pagectm (page
);
2104 fz_concat (&ctm
, &pm
, &tm
);
2107 for (link
= links
; link
; link
= link
->next
) {
2108 fz_point p1
, p2
, p3
, p4
;
2110 p1
.x
= link
->rect
.x0
;
2111 p1
.y
= link
->rect
.y0
;
2113 p2
.x
= link
->rect
.x1
;
2114 p2
.y
= link
->rect
.y0
;
2116 p3
.x
= link
->rect
.x1
;
2117 p3
.y
= link
->rect
.y1
;
2119 p4
.x
= link
->rect
.x0
;
2120 p4
.y
= link
->rect
.y1
;
2122 fz_transform_point (&p1
, &ctm
);
2123 fz_transform_point (&p2
, &ctm
);
2124 fz_transform_point (&p3
, &ctm
);
2125 fz_transform_point (&p4
, &ctm
);
2127 switch (link
->dest
.kind
) {
2128 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2129 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2130 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2131 default: glColor3ub (0, 0, 0); break;
2139 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2143 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2146 glVertex2f (p1
.x
, p1
.y
);
2148 glVertex2f (p2
.x
, p2
.y
);
2151 glVertex2f (p2
.x
, p2
.y
);
2153 glVertex2f (p3
.x
, p3
.y
);
2156 glVertex2f (p3
.x
, p3
.y
);
2158 glVertex2f (p4
.x
, p4
.y
);
2161 glVertex2f (p4
.x
, p4
.y
);
2163 glVertex2f (p1
.x
, p1
.y
);
2168 glDisable (GL_BLEND
);
2169 glDisable (GL_TEXTURE_1D
);
2172 static int compareslinks (const void *l
, const void *r
)
2174 struct slink
const *ls
= l
;
2175 struct slink
const *rs
= r
;
2176 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2177 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2179 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2182 static void droptext (struct page
*page
)
2185 fz_free_text_page (state
.ctx
, page
->text
);
2188 page
->fmark
.span
= NULL
;
2189 page
->lmark
.span
= NULL
;
2193 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2198 static void dropslinks (struct page
*page
)
2201 free (page
->slinks
);
2202 page
->slinks
= NULL
;
2203 page
->slinkcount
= 0;
2207 static void ensureslinks (struct page
*page
)
2211 size_t slinksize
= sizeof (*page
->slinks
);
2212 fz_link
*link
, *links
;
2214 if (state
.gen
!= page
->sgen
) {
2216 page
->sgen
= state
.gen
;
2218 if (page
->slinks
) return;
2220 switch (page
->type
) {
2222 links
= page
->u
.pdfpage
->links
;
2223 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2225 &state
.pagedims
[page
->pdimno
].tctm
,
2226 &state
.pagedims
[page
->pdimno
].ctm
);
2230 links
= page
->u
.xpspage
->links
;
2231 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2238 for (link
= links
; link
; link
= link
->next
) {
2242 page
->slinkcount
= count
;
2243 page
->slinks
= calloc (count
, slinksize
);
2244 if (!page
->slinks
) {
2245 err (1, "realloc slinks %d", count
);
2248 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2252 fz_transform_rect (&rect
, &ctm
);
2253 page
->slinks
[i
].link
= link
;
2254 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2256 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2260 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2261 static void fmt_linkn (char *s
, unsigned int u
)
2263 unsigned int len
; unsigned int q
;
2264 int zma
= 'z' - 'a' + 1;
2266 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2269 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2270 /* handles u == 0 */
2275 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2276 int noff
, char *targ
, int tlen
, int hfsize
)
2280 struct slink
*slink
;
2281 double x0
, y0
, x1
, y1
, w
;
2283 ensureslinks (page
);
2284 glColor3ub (0xc3, 0xb0, 0x91);
2285 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2286 fmt_linkn (buf
, i
+ noff
);
2287 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2288 slink
= &page
->slinks
[i
];
2290 x0
= slink
->bbox
.x0
+ xoff
- 5;
2291 y1
= slink
->bbox
.y0
+ yoff
- 5;
2292 y0
= y1
+ 10 + hfsize
;
2293 w
= measure_string (state
.face
, hfsize
, buf
);
2295 glRectd (x0
, y0
, x1
, y1
);
2299 glEnable (GL_BLEND
);
2300 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2301 glEnable (GL_TEXTURE_2D
);
2302 glColor3ub (0, 0, 0);
2303 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2304 fmt_linkn (buf
, i
+ noff
);
2305 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2306 slink
= &page
->slinks
[i
];
2308 x0
= slink
->bbox
.x0
+ xoff
;
2309 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2310 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2313 glDisable (GL_TEXTURE_2D
);
2314 glDisable (GL_BLEND
);
2318 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2321 struct slice
*slice1
;
2322 unsigned char *texdata
;
2325 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2326 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2328 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2329 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2330 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2334 int texindex
= state
.texindex
++ % state
.texcount
;
2336 if (state
.texowners
[texindex
].w
== tile
->w
) {
2337 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2341 state
.texowners
[texindex
].h
= slice
->h
;
2345 state
.texowners
[texindex
].h
= slice
->h
;
2348 state
.texowners
[texindex
].w
= tile
->w
;
2349 state
.texowners
[texindex
].slice
= slice
;
2350 slice
->texindex
= texindex
;
2352 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2354 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2358 texdata
= tile
->pixmap
->samples
;
2361 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2373 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2385 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2390 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2392 CAMLparam2 (args_v
, ptr_v
);
2393 int dispx
= Int_val (Field (args_v
, 0));
2394 int dispy
= Int_val (Field (args_v
, 1));
2395 int dispw
= Int_val (Field (args_v
, 2));
2396 int disph
= Int_val (Field (args_v
, 3));
2397 int tilex
= Int_val (Field (args_v
, 4));
2398 int tiley
= Int_val (Field (args_v
, 5));
2399 char *s
= String_val (ptr_v
);
2400 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2402 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2404 int slicey
, firstslice
;
2405 struct slice
*slice
;
2407 firstslice
= tiley
/ tile
->sliceheight
;
2408 slice
= &tile
->slices
[firstslice
];
2409 slicey
= tiley
% tile
->sliceheight
;
2414 dh
= slice
->h
- slicey
;
2415 dh
= MIN (disph
, dh
);
2416 uploadslice (tile
, slice
);
2420 glTexCoord2i (tilex
, slicey
);
2421 glVertex2i (dispx
, dispy
);
2423 glTexCoord2i (tilex
+dispw
, slicey
);
2424 glVertex2i (dispx
+dispw
, dispy
);
2426 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2427 glVertex2i (dispx
+dispw
, dispy
+dh
);
2429 glTexCoord2i (tilex
, slicey
+dh
);
2430 glVertex2i (dispx
, dispy
+dh
);
2437 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2441 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2442 CAMLreturn (Val_unit
);
2445 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2446 value xoff_v
, value yoff_v
,
2449 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2450 int xoff
= Int_val (xoff_v
);
2451 int yoff
= Int_val (yoff_v
);
2452 int noff
= Int_val (Field (li_v
, 0));
2453 char *targ
= String_val (Field (li_v
, 1));
2454 int tlen
= caml_string_length (Field (li_v
, 1));
2455 int hfsize
= Int_val (Field (li_v
, 2));
2456 char *s
= String_val (ptr_v
);
2457 int hlmask
= Int_val (hlinks_v
);
2458 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2461 /* deal with loadpage failed pages */
2465 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2466 if (trylock ("ml_postprocess")) {
2471 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2472 noff
= page
->slinkcount
;
2474 if (page
->tgen
== state
.gen
) {
2475 showsel (page
, xoff
, yoff
);
2477 unlock ("ml_postprocess");
2480 CAMLreturn (Val_int (noff
));
2483 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2487 const fz_matrix
*tctm
;
2488 fz_link
*link
, *links
;
2490 switch (page
->type
) {
2492 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2493 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2494 links
= page
->u
.pdfpage
->links
;
2498 tctm
= &fz_identity
;
2499 links
= page
->u
.xpspage
->links
;
2508 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2509 fz_invert_matrix (&ctm
, &ctm
);
2510 fz_transform_point (&p
, &ctm
);
2512 for (link
= links
; link
; link
= link
->next
) {
2513 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2514 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2522 static void ensuretext (struct page
*page
)
2524 if (state
.gen
!= page
->tgen
) {
2526 page
->tgen
= state
.gen
;
2532 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2533 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2534 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2535 ctm
= pagectm (page
);
2536 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2537 qsort (page
->text
->blocks
, page
->text
->len
,
2538 sizeof (*page
->text
->blocks
), compareblocks
);
2539 fz_free_device (tdev
);
2543 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2545 CAMLparam2 (start_page_v
, dir_v
);
2547 int i
, dir
= Int_val (dir_v
);
2548 int start_page
= Int_val (start_page_v
);
2549 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2551 ret_v
= Val_int (0);
2552 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2556 lock ("ml_findpage_with_links");
2557 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2560 switch (state
.type
) {
2563 pdf_page
*page
= NULL
;
2565 fz_try (state
.ctx
) {
2566 page
= pdf_load_page (state
.u
.pdf
, i
);
2567 found
= !!page
->links
;
2569 fz_catch (state
.ctx
) {
2579 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2580 found
= !!page
->links
;
2586 ARSERT (0 && "invalid document type");
2590 ret_v
= caml_alloc_small (1, 1);
2591 Field (ret_v
, 0) = Val_int (i
);
2596 unlock ("ml_findpage_with_links");
2602 enum { dir_first
, dir_last
};
2603 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2605 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2607 CAMLparam2 (ptr_v
, dir_v
);
2608 CAMLlocal2 (ret_v
, pos_v
);
2610 int dirtag
, i
, slinkindex
;
2611 struct slink
*found
= NULL
,*slink
;
2612 char *s
= String_val (ptr_v
);
2614 page
= parse_pointer ("ml_findlink", s
);
2615 ret_v
= Val_int (0);
2616 if (trylock ("ml_findlink")) {
2620 ensureslinks (page
);
2622 if (Is_block (dir_v
)) {
2623 dirtag
= Tag_val (dir_v
);
2625 case dir_first_visible
:
2627 int x0
, y0
, dir
, first_index
, last_index
;
2629 pos_v
= Field (dir_v
, 0);
2630 x0
= Int_val (Field (pos_v
, 0));
2631 y0
= Int_val (Field (pos_v
, 1));
2632 dir
= Int_val (Field (pos_v
, 2));
2637 last_index
= page
->slinkcount
;
2640 first_index
= page
->slinkcount
- 1;
2644 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2645 slink
= &page
->slinks
[i
];
2646 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2655 slinkindex
= Int_val (Field (dir_v
, 0));
2656 found
= &page
->slinks
[slinkindex
];
2657 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2658 slink
= &page
->slinks
[i
];
2659 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2667 slinkindex
= Int_val (Field (dir_v
, 0));
2668 found
= &page
->slinks
[slinkindex
];
2669 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2670 slink
= &page
->slinks
[i
];
2671 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2679 slinkindex
= Int_val (Field (dir_v
, 0));
2680 found
= &page
->slinks
[slinkindex
];
2681 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2682 slink
= &page
->slinks
[i
];
2683 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2691 slinkindex
= Int_val (Field (dir_v
, 0));
2692 found
= &page
->slinks
[slinkindex
];
2693 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2694 slink
= &page
->slinks
[i
];
2695 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2704 dirtag
= Int_val (dir_v
);
2707 found
= page
->slinks
;
2712 found
= page
->slinks
+ (page
->slinkcount
- 1);
2718 ret_v
= caml_alloc_small (2, 1);
2719 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2722 unlock ("ml_findlink");
2727 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2733 switch (link->dest.kind) { \
2734 case FZ_LINK_GOTO: \
2738 pageno = link->dest.ld.gotor.page; \
2742 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2743 p.y = link->dest.ld.gotor.lt.y; \
2744 fz_transform_point (&p, &pdim->lctm); \
2746 tup_v = caml_alloc_tuple (2); \
2747 ret_v = caml_alloc_small (1, ugoto); \
2748 Field (tup_v, 0) = Val_int (pageno); \
2749 Field (tup_v, 1) = Val_int (p.y); \
2750 Field (ret_v, 0) = tup_v; \
2755 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2756 ret_v = caml_alloc_small (1, uuri); \
2757 Field (ret_v, 0) = str_v; \
2760 case FZ_LINK_LAUNCH: \
2761 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2762 ret_v = caml_alloc_small (1, ulaunch); \
2763 Field (ret_v, 0) = str_v; \
2766 case FZ_LINK_NAMED: \
2767 str_v = caml_copy_string (link->dest.ld.named.named); \
2768 ret_v = caml_alloc_small (1, unamed); \
2769 Field (ret_v, 0) = str_v; \
2772 case FZ_LINK_GOTOR: \
2773 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2774 pageno = link->dest.ld.gotor.page; \
2775 tup_v = caml_alloc_tuple (2); \
2776 ret_v = caml_alloc_small (1, uremote); \
2777 Field (tup_v, 0) = str_v; \
2778 Field (tup_v, 1) = Val_int (pageno); \
2779 Field (ret_v, 0) = tup_v; \
2786 snprintf (buf, sizeof (buf), \
2787 "unhandled link kind %d", link->dest.kind); \
2788 str_v = caml_copy_string (buf); \
2789 ret_v = caml_alloc_small (1, uunexpected); \
2790 Field (ret_v, 0) = str_v; \
2796 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2798 CAMLparam2 (ptr_v
, n_v
);
2799 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2802 struct pagedim
*pdim
;
2803 char *s
= String_val (ptr_v
);
2805 ret_v
= Val_int (0);
2806 if (trylock ("ml_getlink")) {
2810 page
= parse_pointer ("ml_getlink", s
);
2811 ensureslinks (page
);
2812 pdim
= &state
.pagedims
[page
->pdimno
];
2813 link
= page
->slinks
[Int_val (n_v
)].link
;
2816 unlock ("ml_getlink");
2821 CAMLprim value
ml_getlinkcount (value ptr_v
)
2825 char *s
= String_val (ptr_v
);
2827 page
= parse_pointer ("ml_getlinkcount", s
);
2828 CAMLreturn (Val_int (page
->slinkcount
));
2831 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2833 CAMLparam2 (ptr_v
, n_v
);
2836 struct slink
*slink
;
2837 char *s
= String_val (ptr_v
);
2839 page
= parse_pointer ("ml_getlinkrect", s
);
2840 ret_v
= caml_alloc_tuple (4);
2841 if (trylock ("ml_getlinkrect")) {
2842 Field (ret_v
, 0) = Val_int (0);
2843 Field (ret_v
, 1) = Val_int (0);
2844 Field (ret_v
, 2) = Val_int (0);
2845 Field (ret_v
, 3) = Val_int (0);
2848 ensureslinks (page
);
2850 slink
= &page
->slinks
[Int_val (n_v
)];
2851 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2852 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2853 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2854 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2855 unlock ("ml_getlinkrect");
2861 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2863 CAMLparam3 (ptr_v
, x_v
, y_v
);
2864 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2867 char *s
= String_val (ptr_v
);
2868 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2869 struct pagedim
*pdim
;
2871 ret_v
= Val_int (0);
2872 if (trylock ("ml_whatsunder")) {
2876 page
= parse_pointer ("ml_whatsunder", s
);
2877 pdim
= &state
.pagedims
[page
->pdimno
];
2878 x
+= pdim
->bounds
.x0
;
2879 y
+= pdim
->bounds
.y0
;
2880 link
= getlink (page
, x
, y
);
2886 fz_page_block
*pageb
;
2887 fz_text_block
*block
;
2890 for (pageb
= page
->text
->blocks
;
2891 pageb
< page
->text
->blocks
+ page
->text
->len
;
2894 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2895 block
= pageb
->u
.text
;
2898 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2901 for (line
= block
->lines
;
2902 line
< block
->lines
+ block
->len
;
2907 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2910 for (span
= line
->first_span
; span
; span
= span
->next
) {
2914 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2917 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
2919 fz_text_char_bbox (&bbox
, span
, charnum
);
2922 if (x
>= b
->x0
&& x
<= b
->x1
2923 && y
>= b
->y0
&& y
<= b
->y1
) {
2924 fz_text_style
*style
= span
->text
->style
;
2926 style
->font
&& style
->font
->name
2928 : "Span has no font name"
2930 FT_FaceRec
*face
= style
->font
->ft_face
;
2931 if (face
&& face
->family_name
) {
2933 char *n1
= face
->family_name
;
2934 size_t l1
= strlen (n1
);
2935 size_t l2
= strlen (n2
);
2937 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2938 s
= malloc (l1
+ l2
+ 2);
2942 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2943 str_v
= caml_copy_string (s
);
2949 str_v
= caml_copy_string (n2
);
2951 ret_v
= caml_alloc_small (1, utext
);
2952 Field (ret_v
, 0) = str_v
;
2961 unlock ("ml_whatsunder");
2967 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2969 static int uninteresting (int c
)
2971 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
2975 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2977 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2982 fz_page_block
*pageb
;
2983 fz_text_block
*block
;
2984 struct pagedim
*pdim
;
2985 int mark
= Int_val (mark_v
);
2986 char *s
= String_val (ptr_v
);
2987 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2989 ret_v
= Val_bool (0);
2990 if (trylock ("ml_markunder")) {
2994 page
= parse_pointer ("ml_markunder", s
);
2995 pdim
= &state
.pagedims
[page
->pdimno
];
2996 x
+= pdim
->bounds
.x0
;
2997 y
+= pdim
->bounds
.y0
;
3001 if (mark
== mark_page
) {
3004 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3005 pageb
= &page
->text
->blocks
[i
];
3006 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3008 if (i
< 0) goto unlock
;
3010 block
= pageb
->u
.text
;
3013 page
->fmark
.span
= block
->lines
->first_span
;
3015 line
= &block
->lines
[block
->len
- 1];
3016 page
->lmark
.i
= line
->last_span
->len
- 1;
3017 page
->lmark
.span
= line
->last_span
;
3018 ret_v
= Val_bool (1);
3022 for (pageb
= page
->text
->blocks
;
3023 pageb
< page
->text
->blocks
+ page
->text
->len
;
3025 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3026 block
= pageb
->u
.text
;
3029 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3032 if (mark
== mark_block
) {
3034 page
->fmark
.span
= block
->lines
->first_span
;
3036 line
= &block
->lines
[block
->len
- 1];
3037 page
->lmark
.i
= line
->last_span
->len
- 1;
3038 page
->lmark
.span
= line
->last_span
;
3039 ret_v
= Val_bool (1);
3043 for (line
= block
->lines
;
3044 line
< block
->lines
+ block
->len
;
3049 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3052 if (mark
== mark_line
) {
3054 page
->fmark
.span
= line
->first_span
;
3056 page
->lmark
.i
= line
->last_span
->len
- 1;
3057 page
->lmark
.span
= line
->last_span
;
3058 ret_v
= Val_bool (1);
3062 for (span
= line
->first_span
; span
; span
= span
->next
) {
3066 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3069 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3071 fz_text_char_bbox (&bbox
, span
, charnum
);
3074 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3076 int charnum2
, charnum3
= -1, charnum4
= -1;
3078 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3080 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3081 if (uninteresting (span
->text
[charnum2
].c
)) {
3082 charnum3
= charnum2
+ 1;
3086 if (charnum3
== -1) charnum3
= 0;
3088 for (charnum2
= charnum
+ 1;
3089 charnum2
< span
->len
;
3091 if (uninteresting (span
->text
[charnum2
].c
)) break;
3092 charnum4
= charnum2
;
3094 if (charnum4
== -1) goto unlock
;
3096 page
->fmark
.i
= charnum3
;
3097 page
->fmark
.span
= span
;
3099 page
->lmark
.i
= charnum4
;
3100 page
->lmark
.span
= span
;
3101 ret_v
= Val_bool (1);
3109 if (!Bool_val (ret_v
)) {
3110 page
->fmark
.span
= NULL
;
3111 page
->lmark
.span
= NULL
;
3115 unlock ("ml_markunder");
3121 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3123 CAMLparam3 (ptr_v
, x_v
, y_v
);
3124 CAMLlocal2 (ret_v
, res_v
);
3127 fz_page_block
*pageb
;
3128 struct pagedim
*pdim
;
3129 char *s
= String_val (ptr_v
);
3130 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3132 ret_v
= Val_int (0);
3133 if (trylock ("ml_rectofblock")) {
3137 page
= parse_pointer ("ml_rectofblock", s
);
3138 pdim
= &state
.pagedims
[page
->pdimno
];
3139 x
+= pdim
->bounds
.x0
;
3140 y
+= pdim
->bounds
.y0
;
3144 for (pageb
= page
->text
->blocks
;
3145 pageb
< page
->text
->blocks
+ page
->text
->len
;
3147 switch (pageb
->type
) {
3148 case FZ_PAGE_BLOCK_TEXT
:
3149 b
= &pageb
->u
.text
->bbox
;
3152 case FZ_PAGE_BLOCK_IMAGE
:
3153 b
= &pageb
->u
.image
->bbox
;
3160 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3165 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3166 ret_v
= caml_alloc_small (1, 1);
3167 Store_double_field (res_v
, 0, b
->x0
);
3168 Store_double_field (res_v
, 1, b
->x1
);
3169 Store_double_field (res_v
, 2, b
->y0
);
3170 Store_double_field (res_v
, 3, b
->y1
);
3171 Field (ret_v
, 0) = res_v
;
3173 unlock ("ml_rectofblock");
3179 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3181 CAMLparam2 (ptr_v
, rect_v
);
3184 struct pagedim
*pdim
;
3185 int i
, x0
, x1
, y0
, y1
;
3186 char *s
= String_val (ptr_v
);
3188 fz_page_block
*pageb
;
3189 fz_text_block
*block
;
3190 fz_text_span
*span
, *fspan
, *lspan
;
3191 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
3193 if (trylock ("ml_seltext")) {
3197 page
= parse_pointer ("ml_seltext", s
);
3200 pdim
= &state
.pagedims
[page
->pdimno
];
3201 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
3202 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3203 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3204 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3207 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3208 glColor3ub (128, 128, 128);
3209 glRecti (x0
, y0
, x1
, y1
);
3210 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3213 fspan
= lspan
= NULL
;
3215 for (pageb
= page
->text
->blocks
;
3216 pageb
< page
->text
->blocks
+ page
->text
->len
;
3218 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3219 block
= pageb
->u
.text
;
3220 for (line
= block
->lines
;
3221 line
< block
->lines
+ block
->len
;
3225 for (span
= line
->first_span
; span
; span
= span
->next
) {
3226 for (i
= 0; i
< span
->len
; ++i
) {
3229 fz_text_char_bbox (&b
, span
, i
);
3231 if (x0
>= b
.x0
&& x0
<= b
.x1
3232 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3238 if (x1
>= b
.x0
&& x1
<= b
.x1
3239 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3245 if (0 && selected
) {
3246 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3247 glColor3ub (128, 128, 128);
3248 glRecti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3249 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3255 if (y1
< y0
|| x1
< x0
) {
3258 if (fspan
== lspan
) {
3263 if (fline
!= lline
) {
3282 page
->fmark
.span
= fspan
;
3285 page
->lmark
.span
= lspan
;
3287 unlock ("ml_seltext");
3290 CAMLreturn (Val_unit
);
3293 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3298 for (i
= a
; i
<= b
; ++i
) {
3299 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3300 ret
= fwrite (buf
, len
, 1, f
);
3303 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3304 len
, ret
, strerror (errno
));
3312 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3314 caml_failwith ("ml_popen not implemented under Cygwin");
3317 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3319 CAMLparam2 (command_v
, fds_v
);
3320 CAMLlocal2 (l_v
, tup_v
);
3323 value earg_v
= Nothing
;
3324 posix_spawnattr_t attr
;
3325 posix_spawn_file_actions_t fa
;
3326 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3328 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3329 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3332 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3333 msg
= "posix_spawnattr_init";
3337 #ifdef POSIX_SPAWN_USEVFORK
3338 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3339 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3344 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3347 tup_v
= Field (l_v
, 0);
3348 fd1
= Int_val (Field (tup_v
, 0));
3349 fd2
= Int_val (Field (tup_v
, 1));
3351 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3352 msg
= "posix_spawn_file_actions_addclose";
3358 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3359 msg
= "posix_spawn_file_actions_adddup2";
3366 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3367 msg
= "posix_spawn";
3372 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3373 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3377 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3378 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3383 unix_error (ret
, msg
, earg_v
);
3385 CAMLreturn (Val_unit
);
3389 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
, value clear_v
)
3391 CAMLparam3 (fd_v
, ptr_v
, clear_v
);
3396 fz_page_block
*pageb
;
3397 fz_text_block
*block
;
3398 int fd
= Int_val (fd_v
);
3399 char *s
= String_val (ptr_v
);
3400 int clear
= Bool_val (clear_v
);
3402 if (trylock ("ml_copysel")) {
3406 page
= parse_pointer ("ml_sopysel", s
);
3408 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3409 fprintf (stderr
, "nothing to copy\n");
3413 f
= fdopen (fd
, "w");
3415 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3420 for (pageb
= page
->text
->blocks
;
3421 pageb
< page
->text
->blocks
+ page
->text
->len
;
3423 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3424 block
= pageb
->u
.text
;
3425 for (line
= block
->lines
;
3426 line
< block
->lines
+ block
->len
;
3430 for (span
= line
->first_span
; span
; span
= span
->next
) {
3433 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3434 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3435 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3438 if (pipespan (f
, span
, a
, b
)) {
3441 if (span
== line
->last_span
) {
3442 if (putc ('\n', f
) == EOF
) {
3444 "failed break line on sel pipe: %s\n",
3449 if (span
== page
->lmark
.span
) {
3458 page
->lmark
.span
= NULL
;
3459 page
->fmark
.span
= NULL
;
3464 int ret
= fclose (f
);
3467 if (errno
!= ECHILD
) {
3468 fprintf (stderr
, "failed to close sel pipe: %s\n",
3474 unlock ("ml_copysel");
3479 fprintf (stderr
, "failed to close sel pipe: %s\n",
3483 CAMLreturn (Val_unit
);
3486 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3488 CAMLparam1 (pagedimno_v
);
3490 int pagedimno
= Int_val (pagedimno_v
);
3493 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3494 if (trylock ("ml_getpdimrect")) {
3495 box
= fz_empty_rect
;
3498 box
= state
.pagedims
[pagedimno
].mediabox
;
3499 unlock ("ml_getpdimrect");
3502 Store_double_field (ret_v
, 0, box
.x0
);
3503 Store_double_field (ret_v
, 1, box
.x1
);
3504 Store_double_field (ret_v
, 2, box
.y0
);
3505 Store_double_field (ret_v
, 3, box
.y1
);
3510 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3511 value dw_v
, value cols_v
)
3513 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3519 double winw
= Int_val (winw_v
);
3520 double winh
= Int_val (winh_v
);
3521 double dw
= Int_val (dw_v
);
3522 double cols
= Int_val (cols_v
);
3523 double pw
= 1.0, ph
= 1.0;
3525 if (trylock ("ml_zoom_for_height")) {
3529 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3530 double w
= p
->pagebox
.x1
/ cols
;
3531 double h
= p
->pagebox
.y1
;
3535 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3537 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3540 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3541 unlock ("ml_zoom_for_height");
3543 ret_v
= caml_copy_double (zoom
);
3547 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3549 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3551 int pt
= Int_val(pt_v
);
3552 int x
= Int_val (x_v
);
3553 int y
= Int_val (y_v
);
3556 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3557 ret_v
= caml_copy_double (w
);
3561 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3563 CAMLparam2 (pt_v
, string_v
);
3565 int pt
= Int_val (pt_v
);
3568 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3569 ret_v
= caml_copy_double (w
);
3573 CAMLprim value
ml_getpagebox (value opaque_v
)
3575 CAMLparam1 (opaque_v
);
3581 char *s
= String_val (opaque_v
);
3582 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3584 ret_v
= caml_alloc_tuple (4);
3585 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3586 dev
->hints
|= FZ_IGNORE_SHADE
;
3588 switch (page
->type
) {
3590 ctm
= pagectm (page
);
3591 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3595 ctm
= pagectm (page
);
3596 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3600 rect
= fz_infinite_rect
;
3604 fz_free_device (dev
);
3605 fz_round_rect (&bbox
, &rect
);
3606 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3607 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3608 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3609 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3614 CAMLprim value
ml_setaalevel (value level_v
)
3616 CAMLparam1 (level_v
);
3618 state
.aalevel
= Int_val (level_v
);
3619 CAMLreturn (Val_unit
);
3623 #include <X11/Xlib.h>
3629 GLXDrawable drawable
;
3632 #include "keysym2ucs.c"
3634 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3636 CAMLparam1 (keysym_v
);
3638 KeySym keysym
= Int_val (keysym_v
);
3643 rune
= keysym2ucs (keysym
);
3644 len
= fz_runetochar (buf
, rune
);
3646 str_v
= caml_copy_string (buf
);
3650 CAMLprim value
ml_glx (value win_v
)
3653 XVisualInfo
*visual
;
3654 int screen
, wid
= Int_val (win_v
);
3655 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3657 glx
.dpy
= XOpenDisplay (NULL
);
3659 caml_failwith ("XOpenDisplay");
3662 screen
= DefaultScreen (glx
.dpy
);
3663 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3665 XCloseDisplay (glx
.dpy
);
3667 caml_failwith ("glXChooseVisual");
3670 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3673 XCloseDisplay (glx
.dpy
);
3675 caml_failwith ("glXCreateContext");
3678 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3679 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3680 XCloseDisplay (glx
.dpy
);
3683 caml_failwith ("glXMakeCurrent");
3686 CAMLreturn (Val_unit
);
3689 CAMLprim value
ml_swapb (value unit_v
)
3691 CAMLparam1 (unit_v
);
3692 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3693 CAMLreturn (Val_unit
);
3696 CAMLprim value
ml_glxsync (value unit_v
)
3698 CAMLparam1 (unit_v
);
3699 if (glx
.dpy
&& glx
.ctx
) {
3703 CAMLreturn (Val_unit
);
3706 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3707 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3709 CAMLprim value
ml_platform (value unit_v
)
3711 CAMLparam1 (unit_v
);
3712 int platid
= piunknown
;
3714 #if defined __linux__
3716 #elif defined __CYGWIN__
3718 #elif defined __DragonFly__
3719 platid
= pidragonflybsd
;
3720 #elif defined __FreeBSD__
3722 #elif defined __OpenBSD__
3724 #elif defined __NetBSD__
3726 #elif defined __sun__
3728 #elif defined __APPLE__
3731 CAMLreturn (Val_int (platid
));
3734 CAMLprim value
ml_cloexec (value fd_v
)
3737 int fd
= Int_val (fd_v
);
3739 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3740 uerror ("fcntl", Nothing
);
3742 CAMLreturn (Val_unit
);
3745 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3747 CAMLparam2 (w_v
, h_v
);
3750 int w
= Int_val (w_v
);
3751 int h
= Int_val (h_v
);
3752 int cs
= Int_val (cs_v
);
3754 if (state
.pbo_usable
) {
3755 pbo
= calloc (sizeof (*pbo
), 1);
3757 err (1, "calloc pbo");
3769 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3772 state
.glGenBuffersARB (1, &pbo
->id
);
3773 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3774 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3775 NULL
, GL_STREAM_DRAW
);
3776 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3778 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3780 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3781 state
.glDeleteBuffersARB (1, &pbo
->id
);
3783 ret_v
= caml_copy_string ("0");
3789 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3791 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3795 err (1, "malloc %d bytes failed", res
+1);
3797 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3799 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3801 ret_v
= caml_copy_string (s
);
3806 ret_v
= caml_copy_string ("0");
3811 CAMLprim value
ml_freepbo (value s_v
)
3814 char *s
= String_val (s_v
);
3815 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3818 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3820 tile
->pbo
->ptr
= NULL
;
3821 tile
->pbo
->size
= -1;
3823 CAMLreturn (Val_unit
);
3826 CAMLprim value
ml_unmappbo (value s_v
)
3829 char *s
= String_val (s_v
);
3830 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3833 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3834 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3835 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3837 tile
->pbo
->ptr
= NULL
;
3838 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3840 CAMLreturn (Val_unit
);
3843 static void setuppbo (void)
3845 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3846 GGPA (glBindBufferARB
);
3847 if (state
.glBindBufferARB
) {
3848 GGPA (glUnmapBufferARB
);
3849 if (state
.glUnmapBufferARB
) {
3850 GGPA (glMapBufferARB
);
3851 if (state
.glMapBufferARB
) {
3852 GGPA (glBufferDataARB
);
3853 if (state
.glBufferDataARB
) {
3854 GGPA (glGenBuffersARB
);
3855 if (state
.glGenBuffersARB
) {
3856 GGPA (glDeleteBuffersARB
);
3857 if (state
.glDeleteBuffersARB
) {
3858 state
.pbo_usable
= 1;
3868 CAMLprim value
ml_pbo_usable (value unit_v
)
3870 CAMLparam1 (unit_v
);
3871 CAMLreturn (Val_bool (state
.pbo_usable
));
3874 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3876 CAMLparam3 (ptr_v
, x_v
, y_v
);
3877 CAMLlocal2 (ret_v
, tup_v
);
3879 char *s
= String_val (ptr_v
);
3880 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3881 struct pagedim
*pdim
;
3885 page
= parse_pointer ("ml_unproject", s
);
3886 pdim
= &state
.pagedims
[page
->pdimno
];
3888 ret_v
= Val_int (0);
3889 if (trylock ("ml_unproject")) {
3893 switch (page
->type
) {
3895 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3904 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3905 fz_invert_matrix (&ctm
, &ctm
);
3906 fz_transform_point (&p
, &ctm
);
3908 tup_v
= caml_alloc_tuple (2);
3909 ret_v
= caml_alloc_small (1, 1);
3910 Field (tup_v
, 0) = Val_int (p
.x
);
3911 Field (tup_v
, 1) = Val_int (p
.y
);
3912 Field (ret_v
, 0) = tup_v
;
3914 unlock ("ml_unproject");
3919 static void makestippletex (void)
3921 const char pixels
[] = "\xff\xff\0\0";
3922 glGenTextures (1, &state
.stid
);
3923 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3924 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3925 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3938 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3940 CAMLparam2 (pipe_v
, params_v
);
3941 CAMLlocal2 (trim_v
, fuzz_v
);
3948 struct sigaction sa
;
3950 state
.cr
= Int_val (Field (pipe_v
, 0));
3951 state
.cw
= Int_val (Field (pipe_v
, 1));
3952 state
.rotate
= Int_val (Field (params_v
, 0));
3953 state
.fitmodel
= Int_val (Field (params_v
, 1));
3954 trim_v
= Field (params_v
, 2);
3955 texcount
= Int_val (Field (params_v
, 3));
3956 state
.sliceheight
= Int_val (Field (params_v
, 4));
3957 mustoresize
= Int_val (Field (params_v
, 5));
3958 colorspace
= Int_val (Field (params_v
, 6));
3959 fontpath
= String_val (Field (params_v
, 7));
3960 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3961 if (!state
.trimcachepath
) {
3962 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3965 haspboext
= Bool_val (Field (params_v
, 9));
3967 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3969 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3970 fuzz_v
= Field (trim_v
, 1);
3971 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3972 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3973 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3974 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3976 set_tex_params (colorspace
);
3979 state
.face
= load_font (fontpath
);
3983 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3985 state
.face
= load_builtin_font (base
, len
);
3987 if (!state
.face
) _exit (1);
3989 realloctexts (texcount
);
3997 sa
.sa_handler
= SIG_IGN
;
3998 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4000 sa
.sa_handler
= SIG_DFL
;
4001 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4003 if (sigemptyset (&sa
.sa_mask
)) {
4004 err (1, "sigemptyset");
4006 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4007 err (1, "sigaction");
4010 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4012 errx (1, "pthread_create: %s", strerror (ret
));
4015 CAMLreturn (Val_unit
);