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
);
1033 printd ("progress %f loading %d",
1034 (double) (pageno
+ 1) / state
.pagecount
,
1038 fz_catch (state
.ctx
) {
1050 if (state
.trimmargins
&& trimw
) {
1053 fz_try (state
.ctx
) {
1054 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1055 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1056 cbz_free_page (state
.u
.cbz
, page
);
1057 printd ("progress %f Trimming %d",
1058 (double) (pageno
+ 1) / state
.pagecount
,
1061 fz_catch (state
.ctx
) {
1062 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1065 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1067 err (1, "fwrite trim mediabox");
1073 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1075 err (1, "fread trim mediabox");
1081 fz_try (state
.ctx
) {
1082 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1083 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1084 cbz_free_page (state
.u
.cbz
, page
);
1086 fz_catch (state
.ctx
) {
1087 fprintf (stderr
, "failed to load cbz page\n");
1096 ARSERT (0 && state
.type
);
1099 if (state
.pagedimcount
== 0
1100 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1101 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1104 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1105 state
.pagedims
= realloc (state
.pagedims
, size
);
1106 if (!state
.pagedims
) {
1107 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1108 size
, state
.pagedimcount
+ 1);
1111 p
= &state
.pagedims
[state
.pagedimcount
++];
1113 p
->mediabox
= mediabox
;
1118 if (state
.trimmargins
) {
1119 printd ("progress 1 Trimmed %d pages in %f seconds",
1120 state
.pagecount
, end
- start
);
1123 printd ("vmsg Processed %d pages in %f seconds",
1124 state
.pagecount
, end
- start
);
1128 if (fclose (trimf
)) {
1134 static void layout (void)
1139 struct pagedim
*p
= p
;
1140 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1142 if (state
.pagedimcount
== 0) return;
1144 switch (state
.fitmodel
) {
1145 case FitProportional
:
1146 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1149 p
= &state
.pagedims
[pindex
];
1150 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1152 fz_transform_rect (&box
, &rm
);
1154 x0
= MIN (box
.x0
, box
.x1
);
1155 x1
= MAX (box
.x0
, box
.x1
);
1158 maxw
= MAX (w
, maxw
);
1159 zoom
= state
.w
/ maxw
;
1171 ARSERT (0 && state
.fitmodel
);
1174 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1178 p
= &state
.pagedims
[pindex
];
1179 fz_rotate (&ctm
, state
.rotate
);
1180 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1182 fz_transform_rect (&box
, &rm
);
1183 w
= box
.x1
- box
.x0
;
1184 switch (state
.fitmodel
) {
1185 case FitProportional
:
1186 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1192 h
= box
.y1
- box
.y0
;
1194 zoom
= MIN (zw
, zh
);
1195 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1204 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1205 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1207 fz_rotate (&rm
, p
->rotate
);
1208 p
->pagebox
= p
->mediabox
;
1209 fz_transform_rect (&p
->pagebox
, &rm
);
1210 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1211 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1215 fz_transform_rect (&rect
, &ctm
);
1216 fz_round_rect (&p
->bounds
, &rect
);
1219 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1220 fz_scale (&sm
, zoom
, -zoom
);
1221 fz_concat (&ctm
, &tm
, &sm
);
1222 fz_concat (&p
->lctm
, &ctm
, &rm
);
1228 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1229 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1230 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1231 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1235 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1236 } while (p
-- != state
.pagedims
);
1240 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1241 desttoanchor (fz_link_dest
*dest
)
1245 struct pagedim
*pdim
= state
.pagedims
;
1250 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1251 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1253 pdim
= &state
.pagedims
[i
];
1255 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1257 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1258 p
.x
= dest
->ld
.gotor
.lt
.x
;
1261 p
.y
= dest
->ld
.gotor
.lt
.y
;
1262 fz_transform_point (&p
, &pdim
->lctm
);
1266 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1267 double x0
, x1
, y0
, y1
;
1269 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1270 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1272 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1273 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1275 a
.n
= dest
->ld
.gotor
.page
;
1280 static void recurse_outline (fz_outline
*outline
, int level
)
1283 if (outline
->dest
.kind
== FZ_LINK_GOTO
) {
1284 struct anchor a
= desttoanchor (&outline
->dest
);
1287 printd ("o %d %d %d %d %s",
1288 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1292 printd ("emsg Unhandled outline kind %d for %s\n",
1293 outline
->dest
.kind
, outline
->title
);
1295 if (outline
->down
) {
1296 recurse_outline (outline
->down
, level
+ 1);
1298 outline
= outline
->next
;
1302 static void process_outline (void)
1304 fz_outline
*outline
;
1306 if (!state
.needoutline
) return;
1308 state
.needoutline
= 0;
1309 switch (state
.type
) {
1311 outline
= pdf_load_outline (state
.u
.pdf
);
1314 outline
= xps_load_outline (state
.u
.xps
);
1321 recurse_outline (outline
, 0);
1322 fz_free_outline (state
.ctx
, outline
);
1326 static char *strofspan (fz_text_span
*span
)
1331 size_t size
= 0, cap
= 80;
1333 p
= malloc (cap
+ 1);
1334 if (!p
) return NULL
;
1336 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1337 int n
= fz_runetochar (utf8
, ch
->c
);
1338 if (size
+ n
> cap
) {
1340 p
= realloc (p
, cap
+ 1);
1341 if (!p
) return NULL
;
1344 memcpy (p
+ size
, utf8
, n
);
1351 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1352 fz_text_span
*span
, fz_matrix ctm
,
1353 int stop
, int pageno
, double start
)
1360 fz_point p1
, p2
, p3
, p4
;
1362 p
= strofspan (span
);
1365 ret
= regexec (re
, p
, 1, &rm
, 0);
1368 if (ret
!= REG_NOMATCH
) {
1371 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1372 printd ("msg regexec error `%.*s'",
1373 (int) size
, errbuf
);
1381 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1382 c
+= fz_runelen (span
->text
[a
].c
);
1384 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1385 c
+= fz_runelen (span
->text
[b
].c
);
1388 if (fz_runelen (span
->text
[b
].c
) > 1) {
1392 fz_text_char_bbox (&sb
, span
, a
);
1393 fz_text_char_bbox (&eb
, span
, b
);
1405 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1412 printd ("progress 1 found at %d `%.*s' in %f sec",
1413 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1417 printd ("match %d %d %f %f %f %f %f %f %f %f",
1429 static int compareblocks (const void *l
, const void *r
)
1431 fz_text_block
const *ls
= l
;
1432 fz_text_block
const* rs
= r
;
1433 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1436 /* wishful thinking function */
1437 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1442 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1444 fz_text_sheet
*sheet
;
1445 struct pagedim
*pdim
, *pdimprev
;
1446 int stop
= 0, niters
= 0;
1449 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1453 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1454 if (niters
++ == 5) {
1457 printd ("progress 1 attention requested aborting search at %d",
1462 printd ("progress %f searching in page %d",
1463 (double) (pageno
+ 1) / state
.pagecount
,
1468 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1469 pdim
= &state
.pagedims
[i
];
1470 if (pdim
->pageno
== pageno
) {
1473 if (pdim
->pageno
> pageno
) {
1482 sheet
= fz_new_text_sheet (state
.ctx
);
1483 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1484 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1486 switch (state
.type
) {
1489 fz_try (state
.ctx
) {
1490 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1491 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1492 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1493 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1495 fz_catch (state
.ctx
) {
1496 fz_free_device (tdev
);
1503 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1504 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1508 ARSERT (0 && state
.type
);
1511 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1512 fz_free_device (tdev
);
1514 for (j
= 0; j
< text
->len
; ++j
) {
1517 fz_text_block
*block
;
1519 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1520 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1523 for (k
= 0; k
< block
->len
; ++k
) {
1528 line
= &block
->lines
[k
];
1529 if (line
->bbox
.y0
< y
+ 1) continue;
1532 line
= &block
->lines
[block
->len
- 1 - k
];
1533 if (line
->bbox
.y0
> y
- 1) continue;
1536 for (span
= line
->first_span
; span
; span
= span
->next
) {
1537 switch (matchspan (re
, text
, span
, ctm
,
1538 stop
, pageno
, start
)) {
1540 case 1: stop
= 1; break;
1541 case -1: stop
= 1; goto endloop
;
1556 fz_free_text_page (state
.ctx
, text
);
1557 fz_free_text_sheet (state
.ctx
, sheet
);
1559 state
.freepage (u
.ptr
);
1564 printd ("progress 1 no matches %f sec", end
- start
);
1566 printd ("clearrects");
1569 static void set_tex_params (int colorspace
)
1576 switch (colorspace
) {
1578 state
.texiform
= GL_RGBA8
;
1579 state
.texform
= GL_RGBA
;
1580 state
.texty
= GL_UNSIGNED_BYTE
;
1581 state
.colorspace
= fz_device_rgb (state
.ctx
);
1584 state
.texiform
= GL_RGBA8
;
1585 state
.texform
= GL_BGRA
;
1586 state
.texty
= endianness
.s
> 1
1587 ? GL_UNSIGNED_INT_8_8_8_8
1588 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1589 state
.colorspace
= fz_device_bgr (state
.ctx
);
1592 state
.texiform
= GL_LUMINANCE_ALPHA
;
1593 state
.texform
= GL_LUMINANCE_ALPHA
;
1594 state
.texty
= GL_UNSIGNED_BYTE
;
1595 state
.colorspace
= fz_device_gray (state
.ctx
);
1598 errx (1, "invalid colorspce %d", colorspace
);
1602 static void realloctexts (int texcount
)
1606 if (texcount
== state
.texcount
) return;
1608 if (texcount
< state
.texcount
) {
1609 glDeleteTextures (state
.texcount
- texcount
,
1610 state
.texids
+ texcount
);
1613 size
= texcount
* sizeof (*state
.texids
);
1614 state
.texids
= realloc (state
.texids
, size
);
1615 if (!state
.texids
) {
1616 err (1, "realloc texids %" FMT_s
, size
);
1619 size
= texcount
* sizeof (*state
.texowners
);
1620 state
.texowners
= realloc (state
.texowners
, size
);
1621 if (!state
.texowners
) {
1622 err (1, "realloc texowners %" FMT_s
, size
);
1624 if (texcount
> state
.texcount
) {
1627 glGenTextures (texcount
- state
.texcount
,
1628 state
.texids
+ state
.texcount
);
1629 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1630 state
.texowners
[i
].w
= -1;
1631 state
.texowners
[i
].slice
= NULL
;
1634 state
.texcount
= texcount
;
1638 static char *mbtoutf8 (char *s
)
1644 len
= mbstowcs (NULL
, s
, strlen (s
));
1649 if (len
== (size_t) -1) {
1654 tmp
= malloc (len
* sizeof (wchar_t));
1659 ret
= mbstowcs (tmp
, s
, len
);
1660 if (ret
== (size_t) -1) {
1666 for (i
= 0; i
< ret
; ++i
) {
1667 len
+= fz_runelen (tmp
[i
]);
1670 p
= r
= malloc (len
+ 1);
1676 for (i
= 0; i
< ret
; ++i
) {
1677 p
+= fz_runetochar (p
, tmp
[i
]);
1684 CAMLprim value
ml_mbtoutf8 (value s_v
)
1690 s
= String_val (s_v
);
1696 ret_v
= caml_copy_string (r
);
1702 static void * mainloop (void *unused
)
1705 int len
, ret
, oldlen
= 0;
1710 errx (1, "readlen returned 0");
1713 if (oldlen
< len
+ 1) {
1714 p
= realloc (p
, len
+ 1);
1716 err (1, "realloc %d failed", len
+ 1);
1723 if (!strncmp ("open", p
, 4)) {
1730 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1732 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1735 filename
= p
+ 5 + off
;
1736 filenamelen
= strlen (filename
);
1737 password
= filename
+ filenamelen
+ 1;
1740 openxref (filename
, password
);
1746 utf8filename
= mbtoutf8 (filename
);
1747 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1748 if (utf8filename
!= filename
) {
1749 free (utf8filename
);
1752 state
.needoutline
= 1;
1754 else if (!strncmp ("cs", p
, 2)) {
1757 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1759 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1762 set_tex_params (colorspace
);
1763 for (i
= 0; i
< state
.texcount
; ++i
) {
1764 state
.texowners
[i
].w
= -1;
1765 state
.texowners
[i
].slice
= NULL
;
1769 else if (!strncmp ("freepage", p
, 8)) {
1772 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1774 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1778 else if (!strncmp ("freetile", p
, 8)) {
1781 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1783 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1787 else if (!strncmp ("search", p
, 6)) {
1788 int icase
, pageno
, y
, ret
, len2
, forward
;
1792 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1793 &icase
, &pageno
, &y
, &forward
, &len2
);
1795 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1798 pattern
= p
+ 6 + len2
;
1799 ret
= regcomp (&re
, pattern
,
1800 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1805 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1806 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1809 search (&re
, pageno
, y
, forward
);
1813 else if (!strncmp ("geometry", p
, 8)) {
1817 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1819 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1827 for (i
= 0; i
< state
.texcount
; ++i
) {
1828 state
.texowners
[i
].slice
= NULL
;
1831 state
.fitmodel
= fitmodel
;
1836 unlock ("geometry");
1837 printd ("continue %d", state
.pagecount
);
1839 else if (!strncmp ("reqlayout", p
, 9)) {
1841 int rotate
, fitmodel
, off
, h
;
1844 ret
= sscanf (p
+ 9, " %d %d %d %n",
1845 &rotate
, &fitmodel
, &h
, &off
);
1847 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1850 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1853 state
.rotate
= rotate
;
1854 state
.fitmodel
= fitmodel
;
1859 nameddest
= p
+ 9 + off
;
1860 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1863 pdf_obj
*needle
, *obj
;
1865 needle
= pdf_new_string (state
.ctx
, nameddest
,
1866 strlen (nameddest
));
1867 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1869 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1871 a
= desttoanchor (&dest
);
1873 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1876 printd ("emsg failed to parse destination `%s'\n",
1881 printd ("emsg destination `%s' not found\n",
1884 pdf_drop_obj (needle
);
1888 unlock ("reqlayout");
1889 printd ("continue %d", state
.pagecount
);
1891 else if (!strncmp ("page", p
, 4)) {
1894 int pageno
, pindex
, ret
;
1896 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1898 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1903 page
= loadpage (pageno
, pindex
);
1907 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1909 else if (!strncmp ("tile", p
, 4)) {
1910 int x
, y
, w
, h
, ret
;
1916 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1917 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1919 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1924 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1928 printd ("tile %d %d %" FMT_ptr
" %u %f",
1930 FMT_ptr_cast2 (tile
),
1931 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1934 else if (!strncmp ("settrim", p
, 7)) {
1938 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1939 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1941 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1945 state
.trimmargins
= trimmargins
;
1946 state
.needoutline
= 1;
1947 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1949 state
.trimfuzz
= fuzz
;
1951 state
.pagedimcount
= 0;
1952 free (state
.pagedims
);
1953 state
.pagedims
= NULL
;
1958 printd ("continue %d", state
.pagecount
);
1960 else if (!strncmp ("sliceh", p
, 6)) {
1963 ret
= sscanf (p
+ 6, " %d", &h
);
1965 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1967 if (h
!= state
.sliceheight
) {
1970 state
.sliceheight
= h
;
1971 for (i
= 0; i
< state
.texcount
; ++i
) {
1972 state
.texowners
[i
].w
= -1;
1973 state
.texowners
[i
].h
= -1;
1974 state
.texowners
[i
].slice
= NULL
;
1978 else if (!strncmp ("interrupt", p
, 9)) {
1979 printd ("vmsg interrupted");
1982 errx (1, "unknown command %.*s", len
, p
);
1988 CAMLprim value
ml_realloctexts (value texcount_v
)
1990 CAMLparam1 (texcount_v
);
1993 if (trylock ("ml_realloctexts")) {
1997 realloctexts (Int_val (texcount_v
));
1999 unlock ("ml_realloctexts");
2002 CAMLreturn (Val_bool (ok
));
2005 static void showsel (struct page
*page
, int ox
, int oy
)
2011 fz_page_block
*pageb
;
2012 fz_text_block
*block
;
2013 struct mark first
, last
;
2015 first
= page
->fmark
;
2018 if (!first
.span
|| !last
.span
) return;
2020 glEnable (GL_BLEND
);
2021 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2022 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
2024 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2025 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2026 for (pageb
= page
->text
->blocks
;
2027 pageb
< page
->text
->blocks
+ page
->text
->len
;
2029 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2030 block
= pageb
->u
.text
;
2032 for (line
= block
->lines
;
2033 line
< block
->lines
+ block
->len
;
2036 rect
= fz_empty_rect
;
2038 for (span
= line
->first_span
; span
; span
= span
->next
) {
2040 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2045 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2047 j
= MIN (first
.i
, last
.i
);
2048 k
= MAX (first
.i
, last
.i
);
2050 else if (span
== first
.span
) {
2054 else if (span
== last
.span
) {
2060 for (i
= j
; i
<= k
; ++i
) {
2062 fz_union_rect (&rect
,
2063 fz_text_char_bbox (&bbox
, span
, i
));
2065 fz_round_rect (&bbox
, &rect
);
2066 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2073 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2074 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2075 if (span
== last
.span
) {
2083 glDisable (GL_BLEND
);
2088 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2090 fz_matrix ctm
, tm
, pm
;
2091 fz_link
*link
, *links
;
2093 switch (page
->type
) {
2095 links
= page
->u
.pdfpage
->links
;
2099 links
= page
->u
.xpspage
->links
;
2106 glEnable (GL_TEXTURE_1D
);
2107 glEnable (GL_BLEND
);
2108 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2110 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2111 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2112 fz_translate (&tm
, xoff
, yoff
);
2113 pm
= pagectm (page
);
2114 fz_concat (&ctm
, &pm
, &tm
);
2117 for (link
= links
; link
; link
= link
->next
) {
2118 fz_point p1
, p2
, p3
, p4
;
2120 p1
.x
= link
->rect
.x0
;
2121 p1
.y
= link
->rect
.y0
;
2123 p2
.x
= link
->rect
.x1
;
2124 p2
.y
= link
->rect
.y0
;
2126 p3
.x
= link
->rect
.x1
;
2127 p3
.y
= link
->rect
.y1
;
2129 p4
.x
= link
->rect
.x0
;
2130 p4
.y
= link
->rect
.y1
;
2132 fz_transform_point (&p1
, &ctm
);
2133 fz_transform_point (&p2
, &ctm
);
2134 fz_transform_point (&p3
, &ctm
);
2135 fz_transform_point (&p4
, &ctm
);
2137 switch (link
->dest
.kind
) {
2138 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2139 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2140 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2141 default: glColor3ub (0, 0, 0); break;
2149 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2153 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2156 glVertex2f (p1
.x
, p1
.y
);
2158 glVertex2f (p2
.x
, p2
.y
);
2161 glVertex2f (p2
.x
, p2
.y
);
2163 glVertex2f (p3
.x
, p3
.y
);
2166 glVertex2f (p3
.x
, p3
.y
);
2168 glVertex2f (p4
.x
, p4
.y
);
2171 glVertex2f (p4
.x
, p4
.y
);
2173 glVertex2f (p1
.x
, p1
.y
);
2178 glDisable (GL_BLEND
);
2179 glDisable (GL_TEXTURE_1D
);
2182 static int compareslinks (const void *l
, const void *r
)
2184 struct slink
const *ls
= l
;
2185 struct slink
const *rs
= r
;
2186 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2187 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2189 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2192 static void droptext (struct page
*page
)
2195 fz_free_text_page (state
.ctx
, page
->text
);
2198 page
->fmark
.span
= NULL
;
2199 page
->lmark
.span
= NULL
;
2203 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2208 static void dropslinks (struct page
*page
)
2211 free (page
->slinks
);
2212 page
->slinks
= NULL
;
2213 page
->slinkcount
= 0;
2217 static void ensureslinks (struct page
*page
)
2221 size_t slinksize
= sizeof (*page
->slinks
);
2222 fz_link
*link
, *links
;
2224 if (state
.gen
!= page
->sgen
) {
2226 page
->sgen
= state
.gen
;
2228 if (page
->slinks
) return;
2230 switch (page
->type
) {
2232 links
= page
->u
.pdfpage
->links
;
2233 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2235 &state
.pagedims
[page
->pdimno
].tctm
,
2236 &state
.pagedims
[page
->pdimno
].ctm
);
2240 links
= page
->u
.xpspage
->links
;
2241 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2248 for (link
= links
; link
; link
= link
->next
) {
2252 page
->slinkcount
= count
;
2253 page
->slinks
= calloc (count
, slinksize
);
2254 if (!page
->slinks
) {
2255 err (1, "realloc slinks %d", count
);
2258 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2262 fz_transform_rect (&rect
, &ctm
);
2263 page
->slinks
[i
].link
= link
;
2264 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2266 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2270 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2271 static void fmt_linkn (char *s
, unsigned int u
)
2273 unsigned int len
; unsigned int q
;
2274 int zma
= 'z' - 'a' + 1;
2276 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2279 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2280 /* handles u == 0 */
2285 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2286 int noff
, char *targ
, int tlen
, int hfsize
)
2290 struct slink
*slink
;
2291 double x0
, y0
, x1
, y1
, w
;
2293 ensureslinks (page
);
2294 glColor3ub (0xc3, 0xb0, 0x91);
2295 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2296 fmt_linkn (buf
, i
+ noff
);
2297 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2298 slink
= &page
->slinks
[i
];
2300 x0
= slink
->bbox
.x0
+ xoff
- 5;
2301 y1
= slink
->bbox
.y0
+ yoff
- 5;
2302 y0
= y1
+ 10 + hfsize
;
2303 w
= measure_string (state
.face
, hfsize
, buf
);
2305 glRectd (x0
, y0
, x1
, y1
);
2309 glEnable (GL_BLEND
);
2310 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2311 glEnable (GL_TEXTURE_2D
);
2312 glColor3ub (0, 0, 0);
2313 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2314 fmt_linkn (buf
, i
+ noff
);
2315 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2316 slink
= &page
->slinks
[i
];
2318 x0
= slink
->bbox
.x0
+ xoff
;
2319 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2320 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2323 glDisable (GL_TEXTURE_2D
);
2324 glDisable (GL_BLEND
);
2328 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2331 struct slice
*slice1
;
2332 unsigned char *texdata
;
2335 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2336 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2338 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2339 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2340 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2344 int texindex
= state
.texindex
++ % state
.texcount
;
2346 if (state
.texowners
[texindex
].w
== tile
->w
) {
2347 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2351 state
.texowners
[texindex
].h
= slice
->h
;
2355 state
.texowners
[texindex
].h
= slice
->h
;
2358 state
.texowners
[texindex
].w
= tile
->w
;
2359 state
.texowners
[texindex
].slice
= slice
;
2360 slice
->texindex
= texindex
;
2362 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2364 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2368 texdata
= tile
->pixmap
->samples
;
2371 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2383 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2395 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2400 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2402 CAMLparam2 (args_v
, ptr_v
);
2403 int dispx
= Int_val (Field (args_v
, 0));
2404 int dispy
= Int_val (Field (args_v
, 1));
2405 int dispw
= Int_val (Field (args_v
, 2));
2406 int disph
= Int_val (Field (args_v
, 3));
2407 int tilex
= Int_val (Field (args_v
, 4));
2408 int tiley
= Int_val (Field (args_v
, 5));
2409 char *s
= String_val (ptr_v
);
2410 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2412 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2414 int slicey
, firstslice
;
2415 struct slice
*slice
;
2417 firstslice
= tiley
/ tile
->sliceheight
;
2418 slice
= &tile
->slices
[firstslice
];
2419 slicey
= tiley
% tile
->sliceheight
;
2424 dh
= slice
->h
- slicey
;
2425 dh
= MIN (disph
, dh
);
2426 uploadslice (tile
, slice
);
2430 glTexCoord2i (tilex
, slicey
);
2431 glVertex2i (dispx
, dispy
);
2433 glTexCoord2i (tilex
+dispw
, slicey
);
2434 glVertex2i (dispx
+dispw
, dispy
);
2436 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2437 glVertex2i (dispx
+dispw
, dispy
+dh
);
2439 glTexCoord2i (tilex
, slicey
+dh
);
2440 glVertex2i (dispx
, dispy
+dh
);
2447 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2451 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2452 CAMLreturn (Val_unit
);
2455 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2456 value xoff_v
, value yoff_v
,
2459 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2460 int xoff
= Int_val (xoff_v
);
2461 int yoff
= Int_val (yoff_v
);
2462 int noff
= Int_val (Field (li_v
, 0));
2463 char *targ
= String_val (Field (li_v
, 1));
2464 int tlen
= caml_string_length (Field (li_v
, 1));
2465 int hfsize
= Int_val (Field (li_v
, 2));
2466 char *s
= String_val (ptr_v
);
2467 int hlmask
= Int_val (hlinks_v
);
2468 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2471 /* deal with loadpage failed pages */
2475 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2476 if (trylock ("ml_postprocess")) {
2481 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2482 noff
= page
->slinkcount
;
2484 if (page
->tgen
== state
.gen
) {
2485 showsel (page
, xoff
, yoff
);
2487 unlock ("ml_postprocess");
2490 CAMLreturn (Val_int (noff
));
2493 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2497 const fz_matrix
*tctm
;
2498 fz_link
*link
, *links
;
2500 switch (page
->type
) {
2502 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2503 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2504 links
= page
->u
.pdfpage
->links
;
2508 tctm
= &fz_identity
;
2509 links
= page
->u
.xpspage
->links
;
2518 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2519 fz_invert_matrix (&ctm
, &ctm
);
2520 fz_transform_point (&p
, &ctm
);
2522 for (link
= links
; link
; link
= link
->next
) {
2523 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2524 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2532 static void ensuretext (struct page
*page
)
2534 if (state
.gen
!= page
->tgen
) {
2536 page
->tgen
= state
.gen
;
2542 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2543 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2544 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2545 ctm
= pagectm (page
);
2546 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2547 qsort (page
->text
->blocks
, page
->text
->len
,
2548 sizeof (*page
->text
->blocks
), compareblocks
);
2549 fz_free_device (tdev
);
2553 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2555 CAMLparam2 (start_page_v
, dir_v
);
2557 int i
, dir
= Int_val (dir_v
);
2558 int start_page
= Int_val (start_page_v
);
2559 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2561 ret_v
= Val_int (0);
2562 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2566 lock ("ml_findpage_with_links");
2567 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2570 switch (state
.type
) {
2573 pdf_page
*page
= NULL
;
2575 fz_try (state
.ctx
) {
2576 page
= pdf_load_page (state
.u
.pdf
, i
);
2577 found
= !!page
->links
;
2579 fz_catch (state
.ctx
) {
2589 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2590 found
= !!page
->links
;
2596 ARSERT (0 && "invalid document type");
2600 ret_v
= caml_alloc_small (1, 1);
2601 Field (ret_v
, 0) = Val_int (i
);
2606 unlock ("ml_findpage_with_links");
2612 enum { dir_first
, dir_last
};
2613 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2615 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2617 CAMLparam2 (ptr_v
, dir_v
);
2618 CAMLlocal2 (ret_v
, pos_v
);
2620 int dirtag
, i
, slinkindex
;
2621 struct slink
*found
= NULL
,*slink
;
2622 char *s
= String_val (ptr_v
);
2624 page
= parse_pointer ("ml_findlink", s
);
2625 ret_v
= Val_int (0);
2626 if (trylock ("ml_findlink")) {
2630 ensureslinks (page
);
2632 if (Is_block (dir_v
)) {
2633 dirtag
= Tag_val (dir_v
);
2635 case dir_first_visible
:
2637 int x0
, y0
, dir
, first_index
, last_index
;
2639 pos_v
= Field (dir_v
, 0);
2640 x0
= Int_val (Field (pos_v
, 0));
2641 y0
= Int_val (Field (pos_v
, 1));
2642 dir
= Int_val (Field (pos_v
, 2));
2647 last_index
= page
->slinkcount
;
2650 first_index
= page
->slinkcount
- 1;
2654 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2655 slink
= &page
->slinks
[i
];
2656 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2665 slinkindex
= Int_val (Field (dir_v
, 0));
2666 found
= &page
->slinks
[slinkindex
];
2667 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2668 slink
= &page
->slinks
[i
];
2669 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2677 slinkindex
= Int_val (Field (dir_v
, 0));
2678 found
= &page
->slinks
[slinkindex
];
2679 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2680 slink
= &page
->slinks
[i
];
2681 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2689 slinkindex
= Int_val (Field (dir_v
, 0));
2690 found
= &page
->slinks
[slinkindex
];
2691 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2692 slink
= &page
->slinks
[i
];
2693 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2701 slinkindex
= Int_val (Field (dir_v
, 0));
2702 found
= &page
->slinks
[slinkindex
];
2703 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2704 slink
= &page
->slinks
[i
];
2705 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2714 dirtag
= Int_val (dir_v
);
2717 found
= page
->slinks
;
2722 found
= page
->slinks
+ (page
->slinkcount
- 1);
2728 ret_v
= caml_alloc_small (2, 1);
2729 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2732 unlock ("ml_findlink");
2737 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2743 switch (link->dest.kind) { \
2744 case FZ_LINK_GOTO: \
2748 pageno = link->dest.ld.gotor.page; \
2752 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2753 p.y = link->dest.ld.gotor.lt.y; \
2754 fz_transform_point (&p, &pdim->lctm); \
2756 tup_v = caml_alloc_tuple (2); \
2757 ret_v = caml_alloc_small (1, ugoto); \
2758 Field (tup_v, 0) = Val_int (pageno); \
2759 Field (tup_v, 1) = Val_int (p.y); \
2760 Field (ret_v, 0) = tup_v; \
2765 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2766 ret_v = caml_alloc_small (1, uuri); \
2767 Field (ret_v, 0) = str_v; \
2770 case FZ_LINK_LAUNCH: \
2771 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2772 ret_v = caml_alloc_small (1, ulaunch); \
2773 Field (ret_v, 0) = str_v; \
2776 case FZ_LINK_NAMED: \
2777 str_v = caml_copy_string (link->dest.ld.named.named); \
2778 ret_v = caml_alloc_small (1, unamed); \
2779 Field (ret_v, 0) = str_v; \
2782 case FZ_LINK_GOTOR: \
2783 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2784 pageno = link->dest.ld.gotor.page; \
2785 tup_v = caml_alloc_tuple (2); \
2786 ret_v = caml_alloc_small (1, uremote); \
2787 Field (tup_v, 0) = str_v; \
2788 Field (tup_v, 1) = Val_int (pageno); \
2789 Field (ret_v, 0) = tup_v; \
2796 snprintf (buf, sizeof (buf), \
2797 "unhandled link kind %d", link->dest.kind); \
2798 str_v = caml_copy_string (buf); \
2799 ret_v = caml_alloc_small (1, uunexpected); \
2800 Field (ret_v, 0) = str_v; \
2806 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2808 CAMLparam2 (ptr_v
, n_v
);
2809 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2812 struct pagedim
*pdim
;
2813 char *s
= String_val (ptr_v
);
2815 ret_v
= Val_int (0);
2816 if (trylock ("ml_getlink")) {
2820 page
= parse_pointer ("ml_getlink", s
);
2821 ensureslinks (page
);
2822 pdim
= &state
.pagedims
[page
->pdimno
];
2823 link
= page
->slinks
[Int_val (n_v
)].link
;
2826 unlock ("ml_getlink");
2831 CAMLprim value
ml_getlinkcount (value ptr_v
)
2835 char *s
= String_val (ptr_v
);
2837 page
= parse_pointer ("ml_getlinkcount", s
);
2838 CAMLreturn (Val_int (page
->slinkcount
));
2841 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2843 CAMLparam2 (ptr_v
, n_v
);
2846 struct slink
*slink
;
2847 char *s
= String_val (ptr_v
);
2849 page
= parse_pointer ("ml_getlinkrect", s
);
2850 ret_v
= caml_alloc_tuple (4);
2851 if (trylock ("ml_getlinkrect")) {
2852 Field (ret_v
, 0) = Val_int (0);
2853 Field (ret_v
, 1) = Val_int (0);
2854 Field (ret_v
, 2) = Val_int (0);
2855 Field (ret_v
, 3) = Val_int (0);
2858 ensureslinks (page
);
2860 slink
= &page
->slinks
[Int_val (n_v
)];
2861 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2862 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2863 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2864 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2865 unlock ("ml_getlinkrect");
2871 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2873 CAMLparam3 (ptr_v
, x_v
, y_v
);
2874 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2877 char *s
= String_val (ptr_v
);
2878 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2879 struct pagedim
*pdim
;
2881 ret_v
= Val_int (0);
2882 if (trylock ("ml_whatsunder")) {
2886 page
= parse_pointer ("ml_whatsunder", s
);
2887 pdim
= &state
.pagedims
[page
->pdimno
];
2888 x
+= pdim
->bounds
.x0
;
2889 y
+= pdim
->bounds
.y0
;
2890 link
= getlink (page
, x
, y
);
2896 fz_page_block
*pageb
;
2897 fz_text_block
*block
;
2900 for (pageb
= page
->text
->blocks
;
2901 pageb
< page
->text
->blocks
+ page
->text
->len
;
2904 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2905 block
= pageb
->u
.text
;
2908 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2911 for (line
= block
->lines
;
2912 line
< block
->lines
+ block
->len
;
2917 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2920 for (span
= line
->first_span
; span
; span
= span
->next
) {
2924 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2927 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
2929 fz_text_char_bbox (&bbox
, span
, charnum
);
2932 if (x
>= b
->x0
&& x
<= b
->x1
2933 && y
>= b
->y0
&& y
<= b
->y1
) {
2934 fz_text_style
*style
= span
->text
->style
;
2936 style
->font
&& style
->font
->name
2938 : "Span has no font name"
2940 FT_FaceRec
*face
= style
->font
->ft_face
;
2941 if (face
&& face
->family_name
) {
2943 char *n1
= face
->family_name
;
2944 size_t l1
= strlen (n1
);
2945 size_t l2
= strlen (n2
);
2947 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2948 s
= malloc (l1
+ l2
+ 2);
2952 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2953 str_v
= caml_copy_string (s
);
2959 str_v
= caml_copy_string (n2
);
2961 ret_v
= caml_alloc_small (1, utext
);
2962 Field (ret_v
, 0) = str_v
;
2971 unlock ("ml_whatsunder");
2977 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2979 static int uninteresting (int c
)
2981 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
2985 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2987 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2992 fz_page_block
*pageb
;
2993 fz_text_block
*block
;
2994 struct pagedim
*pdim
;
2995 int mark
= Int_val (mark_v
);
2996 char *s
= String_val (ptr_v
);
2997 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2999 ret_v
= Val_bool (0);
3000 if (trylock ("ml_markunder")) {
3004 page
= parse_pointer ("ml_markunder", s
);
3005 pdim
= &state
.pagedims
[page
->pdimno
];
3006 x
+= pdim
->bounds
.x0
;
3007 y
+= pdim
->bounds
.y0
;
3011 if (mark
== mark_page
) {
3014 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3015 pageb
= &page
->text
->blocks
[i
];
3016 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3018 if (i
< 0) goto unlock
;
3020 block
= pageb
->u
.text
;
3023 page
->fmark
.span
= block
->lines
->first_span
;
3025 line
= &block
->lines
[block
->len
- 1];
3026 page
->lmark
.i
= line
->last_span
->len
- 1;
3027 page
->lmark
.span
= line
->last_span
;
3028 ret_v
= Val_bool (1);
3032 for (pageb
= page
->text
->blocks
;
3033 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 if (mark
== mark_block
) {
3044 page
->fmark
.span
= block
->lines
->first_span
;
3046 line
= &block
->lines
[block
->len
- 1];
3047 page
->lmark
.i
= line
->last_span
->len
- 1;
3048 page
->lmark
.span
= line
->last_span
;
3049 ret_v
= Val_bool (1);
3053 for (line
= block
->lines
;
3054 line
< block
->lines
+ block
->len
;
3059 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3062 if (mark
== mark_line
) {
3064 page
->fmark
.span
= line
->first_span
;
3066 page
->lmark
.i
= line
->last_span
->len
- 1;
3067 page
->lmark
.span
= line
->last_span
;
3068 ret_v
= Val_bool (1);
3072 for (span
= line
->first_span
; span
; span
= span
->next
) {
3076 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3079 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3081 fz_text_char_bbox (&bbox
, span
, charnum
);
3084 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3086 int charnum2
, charnum3
= -1, charnum4
= -1;
3088 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3090 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3091 if (uninteresting (span
->text
[charnum2
].c
)) {
3092 charnum3
= charnum2
+ 1;
3096 if (charnum3
== -1) charnum3
= 0;
3098 for (charnum2
= charnum
+ 1;
3099 charnum2
< span
->len
;
3101 if (uninteresting (span
->text
[charnum2
].c
)) break;
3102 charnum4
= charnum2
;
3104 if (charnum4
== -1) goto unlock
;
3106 page
->fmark
.i
= charnum3
;
3107 page
->fmark
.span
= span
;
3109 page
->lmark
.i
= charnum4
;
3110 page
->lmark
.span
= span
;
3111 ret_v
= Val_bool (1);
3119 if (!Bool_val (ret_v
)) {
3120 page
->fmark
.span
= NULL
;
3121 page
->lmark
.span
= NULL
;
3125 unlock ("ml_markunder");
3131 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3133 CAMLparam3 (ptr_v
, x_v
, y_v
);
3134 CAMLlocal2 (ret_v
, res_v
);
3137 fz_page_block
*pageb
;
3138 struct pagedim
*pdim
;
3139 char *s
= String_val (ptr_v
);
3140 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3142 ret_v
= Val_int (0);
3143 if (trylock ("ml_rectofblock")) {
3147 page
= parse_pointer ("ml_rectofblock", s
);
3148 pdim
= &state
.pagedims
[page
->pdimno
];
3149 x
+= pdim
->bounds
.x0
;
3150 y
+= pdim
->bounds
.y0
;
3154 for (pageb
= page
->text
->blocks
;
3155 pageb
< page
->text
->blocks
+ page
->text
->len
;
3157 switch (pageb
->type
) {
3158 case FZ_PAGE_BLOCK_TEXT
:
3159 b
= &pageb
->u
.text
->bbox
;
3162 case FZ_PAGE_BLOCK_IMAGE
:
3163 b
= &pageb
->u
.image
->bbox
;
3170 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3175 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3176 ret_v
= caml_alloc_small (1, 1);
3177 Store_double_field (res_v
, 0, b
->x0
);
3178 Store_double_field (res_v
, 1, b
->x1
);
3179 Store_double_field (res_v
, 2, b
->y0
);
3180 Store_double_field (res_v
, 3, b
->y1
);
3181 Field (ret_v
, 0) = res_v
;
3183 unlock ("ml_rectofblock");
3189 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3191 CAMLparam2 (ptr_v
, rect_v
);
3194 struct pagedim
*pdim
;
3195 int i
, x0
, x1
, y0
, y1
;
3196 char *s
= String_val (ptr_v
);
3198 fz_page_block
*pageb
;
3199 fz_text_block
*block
;
3200 fz_text_span
*span
, *fspan
, *lspan
;
3201 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
3203 if (trylock ("ml_seltext")) {
3207 page
= parse_pointer ("ml_seltext", s
);
3210 pdim
= &state
.pagedims
[page
->pdimno
];
3211 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
3212 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3213 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3214 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3217 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3218 glColor3ub (128, 128, 128);
3219 glRecti (x0
, y0
, x1
, y1
);
3220 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3223 fspan
= lspan
= NULL
;
3225 for (pageb
= page
->text
->blocks
;
3226 pageb
< page
->text
->blocks
+ page
->text
->len
;
3228 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3229 block
= pageb
->u
.text
;
3230 for (line
= block
->lines
;
3231 line
< block
->lines
+ block
->len
;
3235 for (span
= line
->first_span
; span
; span
= span
->next
) {
3236 for (i
= 0; i
< span
->len
; ++i
) {
3239 fz_text_char_bbox (&b
, span
, i
);
3241 if (x0
>= b
.x0
&& x0
<= b
.x1
3242 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3248 if (x1
>= b
.x0
&& x1
<= b
.x1
3249 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3255 if (0 && selected
) {
3256 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3257 glColor3ub (128, 128, 128);
3258 glRecti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3259 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3265 if (y1
< y0
|| x1
< x0
) {
3268 if (fspan
== lspan
) {
3273 if (fline
!= lline
) {
3292 page
->fmark
.span
= fspan
;
3295 page
->lmark
.span
= lspan
;
3297 unlock ("ml_seltext");
3300 CAMLreturn (Val_unit
);
3303 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3308 for (i
= a
; i
<= b
; ++i
) {
3309 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3310 ret
= fwrite (buf
, len
, 1, f
);
3313 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3314 len
, ret
, strerror (errno
));
3322 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3324 caml_failwith ("ml_popen not implemented under Cygwin");
3327 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3329 CAMLparam2 (command_v
, fds_v
);
3330 CAMLlocal2 (l_v
, tup_v
);
3333 value earg_v
= Nothing
;
3334 posix_spawnattr_t attr
;
3335 posix_spawn_file_actions_t fa
;
3336 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3338 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3339 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3342 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3343 msg
= "posix_spawnattr_init";
3347 #ifdef POSIX_SPAWN_USEVFORK
3348 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3349 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3354 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3357 tup_v
= Field (l_v
, 0);
3358 fd1
= Int_val (Field (tup_v
, 0));
3359 fd2
= Int_val (Field (tup_v
, 1));
3361 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3362 msg
= "posix_spawn_file_actions_addclose";
3368 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3369 msg
= "posix_spawn_file_actions_adddup2";
3376 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3377 msg
= "posix_spawn";
3382 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3383 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3387 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3388 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3393 unix_error (ret
, msg
, earg_v
);
3395 CAMLreturn (Val_unit
);
3399 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
, value clear_v
)
3401 CAMLparam3 (fd_v
, ptr_v
, clear_v
);
3406 fz_page_block
*pageb
;
3407 fz_text_block
*block
;
3408 int fd
= Int_val (fd_v
);
3409 char *s
= String_val (ptr_v
);
3410 int clear
= Bool_val (clear_v
);
3412 if (trylock ("ml_copysel")) {
3416 page
= parse_pointer ("ml_sopysel", s
);
3418 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3419 fprintf (stderr
, "nothing to copy\n");
3423 f
= fdopen (fd
, "w");
3425 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3430 for (pageb
= page
->text
->blocks
;
3431 pageb
< page
->text
->blocks
+ page
->text
->len
;
3433 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3434 block
= pageb
->u
.text
;
3435 for (line
= block
->lines
;
3436 line
< block
->lines
+ block
->len
;
3440 for (span
= line
->first_span
; span
; span
= span
->next
) {
3443 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3444 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3445 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3448 if (pipespan (f
, span
, a
, b
)) {
3451 if (span
== line
->last_span
) {
3452 if (putc ('\n', f
) == EOF
) {
3454 "failed break line on sel pipe: %s\n",
3459 if (span
== page
->lmark
.span
) {
3468 page
->lmark
.span
= NULL
;
3469 page
->fmark
.span
= NULL
;
3474 int ret
= fclose (f
);
3477 if (errno
!= ECHILD
) {
3478 fprintf (stderr
, "failed to close sel pipe: %s\n",
3484 unlock ("ml_copysel");
3489 fprintf (stderr
, "failed to close sel pipe: %s\n",
3493 CAMLreturn (Val_unit
);
3496 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3498 CAMLparam1 (pagedimno_v
);
3500 int pagedimno
= Int_val (pagedimno_v
);
3503 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3504 if (trylock ("ml_getpdimrect")) {
3505 box
= fz_empty_rect
;
3508 box
= state
.pagedims
[pagedimno
].mediabox
;
3509 unlock ("ml_getpdimrect");
3512 Store_double_field (ret_v
, 0, box
.x0
);
3513 Store_double_field (ret_v
, 1, box
.x1
);
3514 Store_double_field (ret_v
, 2, box
.y0
);
3515 Store_double_field (ret_v
, 3, box
.y1
);
3520 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3521 value dw_v
, value cols_v
)
3523 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3529 double winw
= Int_val (winw_v
);
3530 double winh
= Int_val (winh_v
);
3531 double dw
= Int_val (dw_v
);
3532 double cols
= Int_val (cols_v
);
3533 double pw
= 1.0, ph
= 1.0;
3535 if (trylock ("ml_zoom_for_height")) {
3539 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3540 double w
= p
->pagebox
.x1
/ cols
;
3541 double h
= p
->pagebox
.y1
;
3545 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3547 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3550 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3551 unlock ("ml_zoom_for_height");
3553 ret_v
= caml_copy_double (zoom
);
3557 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3559 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3561 int pt
= Int_val(pt_v
);
3562 int x
= Int_val (x_v
);
3563 int y
= Int_val (y_v
);
3566 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3567 ret_v
= caml_copy_double (w
);
3571 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3573 CAMLparam2 (pt_v
, string_v
);
3575 int pt
= Int_val (pt_v
);
3578 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3579 ret_v
= caml_copy_double (w
);
3583 CAMLprim value
ml_getpagebox (value opaque_v
)
3585 CAMLparam1 (opaque_v
);
3591 char *s
= String_val (opaque_v
);
3592 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3594 ret_v
= caml_alloc_tuple (4);
3595 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3596 dev
->hints
|= FZ_IGNORE_SHADE
;
3598 switch (page
->type
) {
3600 ctm
= pagectm (page
);
3601 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3605 ctm
= pagectm (page
);
3606 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3610 rect
= fz_infinite_rect
;
3614 fz_free_device (dev
);
3615 fz_round_rect (&bbox
, &rect
);
3616 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3617 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3618 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3619 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3624 CAMLprim value
ml_setaalevel (value level_v
)
3626 CAMLparam1 (level_v
);
3628 state
.aalevel
= Int_val (level_v
);
3629 CAMLreturn (Val_unit
);
3633 #include <X11/Xlib.h>
3639 GLXDrawable drawable
;
3642 #include "keysym2ucs.c"
3644 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3646 CAMLparam1 (keysym_v
);
3648 KeySym keysym
= Int_val (keysym_v
);
3653 rune
= keysym2ucs (keysym
);
3654 len
= fz_runetochar (buf
, rune
);
3656 str_v
= caml_copy_string (buf
);
3660 CAMLprim value
ml_glx (value win_v
)
3663 XVisualInfo
*visual
;
3664 int screen
, wid
= Int_val (win_v
);
3665 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3667 glx
.dpy
= XOpenDisplay (NULL
);
3669 caml_failwith ("XOpenDisplay");
3672 screen
= DefaultScreen (glx
.dpy
);
3673 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3675 XCloseDisplay (glx
.dpy
);
3677 caml_failwith ("glXChooseVisual");
3680 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3683 XCloseDisplay (glx
.dpy
);
3685 caml_failwith ("glXCreateContext");
3688 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3689 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3690 XCloseDisplay (glx
.dpy
);
3693 caml_failwith ("glXMakeCurrent");
3696 CAMLreturn (Val_unit
);
3699 CAMLprim value
ml_swapb (value unit_v
)
3701 CAMLparam1 (unit_v
);
3702 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3703 CAMLreturn (Val_unit
);
3706 CAMLprim value
ml_glxsync (value unit_v
)
3708 CAMLparam1 (unit_v
);
3709 if (glx
.dpy
&& glx
.ctx
) {
3713 CAMLreturn (Val_unit
);
3716 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3717 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3719 CAMLprim value
ml_platform (value unit_v
)
3721 CAMLparam1 (unit_v
);
3722 int platid
= piunknown
;
3724 #if defined __linux__
3726 #elif defined __CYGWIN__
3728 #elif defined __DragonFly__
3729 platid
= pidragonflybsd
;
3730 #elif defined __FreeBSD__
3732 #elif defined __OpenBSD__
3734 #elif defined __NetBSD__
3736 #elif defined __sun__
3738 #elif defined __APPLE__
3741 CAMLreturn (Val_int (platid
));
3744 CAMLprim value
ml_cloexec (value fd_v
)
3747 int fd
= Int_val (fd_v
);
3749 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3750 uerror ("fcntl", Nothing
);
3752 CAMLreturn (Val_unit
);
3755 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3757 CAMLparam2 (w_v
, h_v
);
3760 int w
= Int_val (w_v
);
3761 int h
= Int_val (h_v
);
3762 int cs
= Int_val (cs_v
);
3764 if (state
.pbo_usable
) {
3765 pbo
= calloc (sizeof (*pbo
), 1);
3767 err (1, "calloc pbo");
3779 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3782 state
.glGenBuffersARB (1, &pbo
->id
);
3783 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3784 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3785 NULL
, GL_STREAM_DRAW
);
3786 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3788 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3790 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3791 state
.glDeleteBuffersARB (1, &pbo
->id
);
3793 ret_v
= caml_copy_string ("0");
3799 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3801 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3805 err (1, "malloc %d bytes failed", res
+1);
3807 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3809 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3811 ret_v
= caml_copy_string (s
);
3816 ret_v
= caml_copy_string ("0");
3821 CAMLprim value
ml_freepbo (value s_v
)
3824 char *s
= String_val (s_v
);
3825 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3828 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3830 tile
->pbo
->ptr
= NULL
;
3831 tile
->pbo
->size
= -1;
3833 CAMLreturn (Val_unit
);
3836 CAMLprim value
ml_unmappbo (value s_v
)
3839 char *s
= String_val (s_v
);
3840 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3843 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3844 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3845 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3847 tile
->pbo
->ptr
= NULL
;
3848 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3850 CAMLreturn (Val_unit
);
3853 static void setuppbo (void)
3855 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3856 GGPA (glBindBufferARB
);
3857 if (state
.glBindBufferARB
) {
3858 GGPA (glUnmapBufferARB
);
3859 if (state
.glUnmapBufferARB
) {
3860 GGPA (glMapBufferARB
);
3861 if (state
.glMapBufferARB
) {
3862 GGPA (glBufferDataARB
);
3863 if (state
.glBufferDataARB
) {
3864 GGPA (glGenBuffersARB
);
3865 if (state
.glGenBuffersARB
) {
3866 GGPA (glDeleteBuffersARB
);
3867 if (state
.glDeleteBuffersARB
) {
3868 state
.pbo_usable
= 1;
3878 CAMLprim value
ml_pbo_usable (value unit_v
)
3880 CAMLparam1 (unit_v
);
3881 CAMLreturn (Val_bool (state
.pbo_usable
));
3884 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3886 CAMLparam3 (ptr_v
, x_v
, y_v
);
3887 CAMLlocal2 (ret_v
, tup_v
);
3889 char *s
= String_val (ptr_v
);
3890 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3891 struct pagedim
*pdim
;
3895 page
= parse_pointer ("ml_unproject", s
);
3896 pdim
= &state
.pagedims
[page
->pdimno
];
3898 ret_v
= Val_int (0);
3899 if (trylock ("ml_unproject")) {
3903 switch (page
->type
) {
3905 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3914 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3915 fz_invert_matrix (&ctm
, &ctm
);
3916 fz_transform_point (&p
, &ctm
);
3918 tup_v
= caml_alloc_tuple (2);
3919 ret_v
= caml_alloc_small (1, 1);
3920 Field (tup_v
, 0) = Val_int (p
.x
);
3921 Field (tup_v
, 1) = Val_int (p
.y
);
3922 Field (ret_v
, 0) = tup_v
;
3924 unlock ("ml_unproject");
3929 static void makestippletex (void)
3931 const char pixels
[] = "\xff\xff\0\0";
3932 glGenTextures (1, &state
.stid
);
3933 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3934 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3935 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3948 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3950 CAMLparam2 (pipe_v
, params_v
);
3951 CAMLlocal2 (trim_v
, fuzz_v
);
3958 struct sigaction sa
;
3960 state
.cr
= Int_val (Field (pipe_v
, 0));
3961 state
.cw
= Int_val (Field (pipe_v
, 1));
3962 state
.rotate
= Int_val (Field (params_v
, 0));
3963 state
.fitmodel
= Int_val (Field (params_v
, 1));
3964 trim_v
= Field (params_v
, 2);
3965 texcount
= Int_val (Field (params_v
, 3));
3966 state
.sliceheight
= Int_val (Field (params_v
, 4));
3967 mustoresize
= Int_val (Field (params_v
, 5));
3968 colorspace
= Int_val (Field (params_v
, 6));
3969 fontpath
= String_val (Field (params_v
, 7));
3970 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3971 if (!state
.trimcachepath
) {
3972 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3975 haspboext
= Bool_val (Field (params_v
, 9));
3977 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3979 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3980 fuzz_v
= Field (trim_v
, 1);
3981 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3982 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3983 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3984 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3986 set_tex_params (colorspace
);
3989 state
.face
= load_font (fontpath
);
3993 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3995 state
.face
= load_builtin_font (base
, len
);
3997 if (!state
.face
) _exit (1);
3999 realloctexts (texcount
);
4007 sa
.sa_handler
= SIG_IGN
;
4008 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4010 sa
.sa_handler
= SIG_DFL
;
4011 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4013 if (sigemptyset (&sa
.sa_mask
)) {
4014 err (1, "sigemptyset");
4016 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4017 err (1, "sigaction");
4020 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4022 errx (1, "pthread_create: %s", strerror (ret
));
4025 CAMLreturn (Val_unit
);