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 */
29 #include <caml/fail.h>
30 #include <caml/alloc.h>
31 #include <caml/memory.h>
32 #include <caml/unixsupport.h>
36 #include <mupdf-internal.h>
38 #include <muxps-internal.h>
41 #include FT_FREETYPE_H
46 extern char **environ
;
49 #define MIN(a,b) ((a) < (b) ? (a) : (b))
50 #define MAX(a,b) ((a) > (b) ? (a) : (b))
53 #define NORETURN_ATTR __attribute__ ((noreturn))
54 #define UNUSED_ATTR __attribute__ ((unused))
55 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
56 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
60 #define OPTIMIZE_ATTR(n)
61 #define GCC_FMT_ATTR(a, b)
67 #define FMT_ptr_cast(p) (p)
68 #define FMT_ptr_cast2(p) (p)
70 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
71 err (int exitcode
, const char *fmt
, ...)
78 vfprintf (stderr
, fmt
, ap
);
80 fprintf (stderr
, ": %s\n", strerror (savederrno
));
85 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
86 errx (int exitcode
, const char *fmt
, ...)
91 vfprintf (stderr
, fmt
, ap
);
98 #ifndef GL_TEXTURE_RECTANGLE_ARB
99 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
103 #define GL_BGRA 0x80E1
106 #ifndef GL_UNSIGNED_INT_8_8_8_8
107 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
110 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
111 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
115 #define lprintf printf
120 #define ARSERT(cond) for (;;) { \
122 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
138 struct slice slices
[1];
149 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
157 enum { DPDF
, DXPS
, DCBZ
};
166 fz_text_sheet
*sheet
;
173 fz_display_list
*dlist
;
175 struct slink
*slinks
;
180 void (*freepage
) (void *);
186 struct pagedim
*pagedims
;
205 fz_colorspace
*colorspace
;
227 void (*closedoc
) (void);
228 void (*freepage
) (void *);
233 void (*glBindBufferARB
) (GLenum
, GLuint
);
234 GLboolean (*glUnmapBufferARB
) (GLenum
);
235 void *(*glMapBufferARB
) (GLenum
, GLenum
);
236 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
237 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
238 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
247 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
249 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
252 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_bbox r
)
254 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
257 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
259 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
260 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
263 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
265 static void lock (const char *cap
)
267 int ret
= pthread_mutex_lock (&mutex
);
269 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
273 static void unlock (const char *cap
)
275 int ret
= pthread_mutex_unlock (&mutex
);
277 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
281 static int trylock (const char *cap
)
283 int ret
= pthread_mutex_trylock (&mutex
);
284 if (ret
&& ret
!= EBUSY
) {
285 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
290 static void *parse_pointer (const char *cap
, const char *s
)
295 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
297 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
302 static double now (void)
306 if (gettimeofday (&tv
, NULL
)) {
307 err (1, "gettimeofday");
309 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
312 static int hasdata (void)
315 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
316 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
320 CAMLprim value
ml_hasdata (value fd_v
)
325 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
326 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
327 CAMLreturn (Val_bool (avail
> 0));
330 static void readdata (void *p
, int size
)
335 n
= read (state
.cr
, p
, size
);
337 if (errno
== EINTR
) goto again
;
338 err (1, "read (req %d, ret %zd)", size
, n
);
341 if (!n
) errx (1, "EOF while reading");
342 errx (1, "read (req %d, ret %zd)", size
, n
);
346 static void writedata (char *p
, int size
)
351 buf
[0] = (size
>> 24) & 0xff;
352 buf
[1] = (size
>> 16) & 0xff;
353 buf
[2] = (size
>> 8) & 0xff;
354 buf
[3] = (size
>> 0) & 0xff;
356 n
= write (state
.cw
, buf
, 4);
358 if (!n
) errx (1, "EOF while writing length");
359 err (1, "write %zd", n
);
362 n
= write (state
.cw
, p
, size
);
364 if (!n
) errx (1, "EOF while writing data");
365 err (1, "write (req %d, ret %zd)", size
, n
);
369 static int readlen (void)
374 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
377 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
385 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
388 len
= vsnprintf (buf
, size
, fmt
, ap
);
391 if (len
> -1 && len
< size
) {
392 writedata (buf
, len
);
402 buf
= realloc (buf
, size
);
407 static void closepdf (void)
410 pdf_close_document (state
.u
.pdf
);
415 static void closexps (void)
418 xps_close_document (state
.u
.xps
);
423 static void closecbz (void)
426 cbz_close_document (state
.u
.cbz
);
431 static void freepdfpage (void *ptr
)
433 pdf_free_page (state
.u
.pdf
, ptr
);
436 static void freeemptyxxxpage (void *ptr
)
441 static void freexpspage (void *ptr
)
443 xps_free_page (state
.u
.xps
, ptr
);
446 static void freecbzpage (void *ptr
)
448 cbz_free_page (state
.u
.cbz
, ptr
);
451 static void openxref (char *filename
, char *password
)
455 for (i
= 0; i
< state
.texcount
; ++i
) {
456 state
.texowners
[i
].w
= -1;
457 state
.texowners
[i
].slice
= NULL
;
460 if (state
.closedoc
) state
.closedoc ();
462 len
= strlen (filename
);
468 ext
[0] = tolower ((int) filename
[len
-3]);
469 ext
[1] = tolower ((int) filename
[len
-2]);
470 ext
[2] = tolower ((int) filename
[len
-1]);
472 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
475 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
480 if (state
.pagedims
) {
481 free (state
.pagedims
);
482 state
.pagedims
= NULL
;
484 state
.pagedimcount
= 0;
486 fz_set_aa_level (state
.ctx
, state
.aalevel
);
487 switch (state
.type
) {
489 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
490 if (pdf_needs_password (state
.u
.pdf
)) {
491 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
493 errx (1, "invalid password");
496 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
497 state
.closedoc
= closepdf
;
498 state
.freepage
= freepdfpage
;
502 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
503 state
.pagecount
= xps_count_pages (state
.u
.xps
);
504 state
.closedoc
= closexps
;
505 state
.freepage
= freexpspage
;
509 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
510 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
511 state
.closedoc
= closecbz
;
512 state
.freepage
= freecbzpage
;
517 static void pdfinfo (void)
519 if (state
.type
== DPDF
) {
522 printd ("info PDF version\t%d.%d",
523 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
525 infoobj
= pdf_dict_gets (state
.u
.pdf
->trailer
, "Info");
529 char *items
[] = { "Title", "Author", "Creator",
530 "Producer", "CreationDate" };
532 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
533 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
534 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
537 printd ("title %s", s
);
539 printd ("info %s\t%s", items
[i
], s
);
541 fz_free (state
.ctx
, s
);
548 static void unlinktile (struct tile
*tile
)
552 for (i
= 0; i
< tile
->slicecount
; ++i
) {
553 struct slice
*s
= &tile
->slices
[i
];
555 if (s
->texindex
!= -1) {
556 if (state
.texowners
[s
->texindex
].slice
== s
) {
557 state
.texowners
[s
->texindex
].slice
= NULL
;
563 static void freepage (struct page
*page
)
566 fz_free_text_page (state
.ctx
, page
->text
);
569 fz_free_text_sheet (state
.ctx
, page
->sheet
);
574 page
->freepage (page
->u
.ptr
);
575 fz_free_display_list (state
.ctx
, page
->dlist
);
579 static void freetile (struct tile
*tile
)
584 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
587 fz_drop_pixmap (state
.ctx
, state
.pig
);
589 state
.pig
= tile
->pixmap
;
594 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
602 static int cacheline32bytes
;
604 static void __attribute__ ((constructor
)) clcheck (void)
606 char **envp
= environ
;
611 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
613 cacheline32bytes
= auxv
[1] == 32;
619 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
621 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
622 if (cacheline32bytes
&& size
> 32) {
623 intptr_t a1
, a2
, diff
;
625 vector
unsigned char v
= vec_splat_u8 (-1);
626 vector
unsigned char *p
;
628 a1
= a2
= (intptr_t) pixmap
->samples
;
629 a2
= (a1
+ 31) & ~31;
634 while (a1
!= a2
) *(char *) a1
++ = 0xff;
635 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
636 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
638 vec_st (v
, i
+ 16, p
);
640 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
642 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
645 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
648 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
651 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
653 if (!pdim
->tctmready
) {
654 if (state
.trimmargins
) {
657 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
658 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
659 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
660 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
671 static fz_matrix
pagectm (struct page
*page
)
673 if (page
->type
== DPDF
) {
674 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
675 state
.pagedims
[page
->pdimno
].ctm
);
679 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
681 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
682 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
683 -pdim
->mediabox
.y0
), ctm
);
688 static void *loadpage (int pageno
, int pindex
)
691 struct page
*page
= NULL
;
693 page
= calloc (sizeof (struct page
), 1);
695 err (1, "calloc page %d", pageno
);
698 page
->dlist
= fz_new_display_list (state
.ctx
);
699 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
701 switch (state
.type
) {
703 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
704 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
705 page
->freepage
= freepdfpage
;
709 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
710 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
711 page
->freepage
= freexpspage
;
715 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
716 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
717 page
->freepage
= freecbzpage
;
721 fz_catch (state
.ctx
) {
723 page
->freepage
= freeemptyxxxpage
;
725 fz_free_device (dev
);
727 page
->pdimno
= pindex
;
728 page
->pageno
= pageno
;
729 page
->sgen
= state
.gen
;
730 page
->tgen
= state
.gen
;
731 page
->type
= state
.type
;
736 static struct tile
*alloctile (int h
)
743 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
744 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
745 tile
= calloc (tilesize
, 1);
747 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
749 for (i
= 0; i
< slicecount
; ++i
) {
750 int sh
= MIN (h
, state
.sliceheight
);
751 tile
->slices
[i
].h
= sh
;
752 tile
->slices
[i
].texindex
= -1;
755 tile
->slicecount
= slicecount
;
756 tile
->sliceheight
= state
.sliceheight
;
766 static void obs_fill_image (fz_device
*dev
, fz_image
*image
, fz_matrix ctm
,
769 struct obs
*obs
= dev
->user
;
771 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
772 fz_bbox b
= fz_round_rect (fz_transform_rect (ctm
, fz_unit_rect
));
773 b
= fz_intersect_bbox (b
, obs
->b
);
774 obs
->cured
= b
.x0
== obs
->b
.x0
777 && b
.y1
== obs
->b
.y1
;
781 static int obscured (struct page
*page
, fz_bbox bbox
)
786 memset (&dev
, 0, sizeof (dev
));
787 memset (&obs
, 0, sizeof (obs
));
792 dev
.fill_image
= obs_fill_image
;
794 fz_run_display_list (page
->dlist
, &dev
, pagectm (page
), bbox
, NULL
);
797 #define OBSCURED obscured
799 #define OBSCURED(a, b) 0
802 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
808 struct pagedim
*pdim
;
810 tile
= alloctile (h
);
811 pdim
= &state
.pagedims
[page
->pdimno
];
816 bbox
.x1
= bbox
.x0
+ w
;
817 bbox
.y1
= bbox
.y0
+ h
;
820 if (state
.pig
->w
== w
822 && state
.pig
->colorspace
== state
.colorspace
) {
823 tile
->pixmap
= state
.pig
;
824 tile
->pixmap
->x
= bbox
.x0
;
825 tile
->pixmap
->y
= bbox
.y0
;
828 fz_drop_pixmap (state
.ctx
, state
.pig
);
835 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
841 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
);
847 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
848 clearpixmap (tile
->pixmap
);
850 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
851 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
852 fz_free_device (dev
);
857 static void initpdims (void)
860 int pageno
, trim
, show
;
866 if (state
.trimmargins
&& state
.trimcachepath
) {
867 trimf
= fopen (state
.trimcachepath
, "rb");
869 trimf
= fopen (state
.trimcachepath
, "wb");
873 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
878 switch (state
.type
) {
880 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
882 if (state
.trimmargins
) {
887 page
= pdf_load_page (state
.u
.pdf
, pageno
);
888 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
889 trim
= state
.trimanew
|| !obj
;
896 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
897 dev
->hints
|= FZ_IGNORE_SHADE
;
898 ctm
= fz_invert_matrix (page
->ctm
);
899 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
900 fz_free_device (dev
);
902 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
903 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
904 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
905 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
906 rect
= fz_transform_rect (ctm
, rect
);
907 rect
= fz_intersect_rect (rect
, page
->mediabox
);
909 if (fz_is_empty_rect (rect
)) {
910 mediabox
= page
->mediabox
;
916 obj
= pdf_new_array (state
.ctx
, 4);
917 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x0
));
918 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y0
));
919 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x1
));
920 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y1
));
921 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
924 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
925 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
926 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
927 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
930 rotate
= page
->rotate
;
931 pdf_free_page (state
.u
.pdf
, page
);
933 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
935 printd ("progress %f Trimming %d",
936 (double) (pageno
+ 1) / state
.pagecount
,
940 fz_catch (state
.ctx
) {
941 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
947 mediabox
= pdf_to_rect (state
.ctx
,
948 pdf_dict_gets (pageobj
, "MediaBox"));
949 if (fz_is_empty_rect (mediabox
)) {
950 fprintf (stderr
, "cannot find page size for page %d\n",
958 cropbox
= pdf_to_rect (state
.ctx
,
959 pdf_dict_gets (pageobj
, "CropBox"));
960 if (!fz_is_empty_rect (cropbox
)) {
961 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
963 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
973 page
= xps_load_page (state
.u
.xps
, pageno
);
974 mediabox
= xps_bound_page (state
.u
.xps
, page
);
976 if (state
.trimmargins
) {
981 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
982 dev
->hints
|= FZ_IGNORE_SHADE
;
983 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
984 fz_free_device (dev
);
986 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
987 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
988 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
989 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
990 rect
= fz_intersect_rect (rect
, mediabox
);
992 if (!fz_is_empty_rect (rect
)) {
996 xps_free_page (state
.u
.xps
, page
);
997 printd ("progress %f loading %d",
998 (double) (pageno
+ 1) / state
.pagecount
,
1001 fz_catch (state
.ctx
) {
1009 if (state
.trimmargins
&& trimw
) {
1012 fz_try (state
.ctx
) {
1013 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1014 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
1015 cbz_free_page (state
.u
.cbz
, page
);
1016 printd ("progress %f Trimming %d",
1017 (double) (pageno
+ 1) / state
.pagecount
,
1020 fz_catch (state
.ctx
) {
1021 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1028 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1030 err (1, "fwrite trim mediabox");
1036 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1038 err (1, "fread trim mediabox");
1042 mediabox
.x0
= mediabox
.y0
= 0;
1051 ARSERT (0 && state
.type
);
1054 if (state
.pagedimcount
== 0
1055 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1056 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1059 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1060 state
.pagedims
= realloc (state
.pagedims
, size
);
1061 if (!state
.pagedims
) {
1062 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1063 size
, state
.pagedimcount
+ 1);
1066 p
= &state
.pagedims
[state
.pagedimcount
++];
1068 p
->mediabox
= mediabox
;
1073 if (state
.trimmargins
) {
1074 printd ("progress 1 Trimmed %d pages in %f seconds",
1075 state
.pagecount
, end
- start
);
1078 printd ("vmsg Processed %d pages in %f seconds",
1079 state
.pagecount
, end
- start
);
1083 if (fclose (trimf
)) {
1089 static void layout (void)
1094 double zoom
, w
, maxw
= 0;
1095 struct pagedim
*p
= state
.pagedims
;
1097 if (state
.proportional
) {
1098 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1099 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1101 w
= box
.x1
- box
.x0
;
1102 maxw
= MAX (w
, maxw
);
1107 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1110 ctm
= fz_rotate (state
.rotate
);
1111 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1113 w
= box
.x1
- box
.x0
;
1115 if (state
.proportional
) {
1116 double scale
= w
/ maxw
;
1117 zoom
= (state
.w
/ w
) * scale
;
1123 p
->zoomctm
= fz_scale (zoom
, zoom
);
1124 ctm
= fz_concat (p
->zoomctm
, ctm
);
1126 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1127 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1128 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1131 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1134 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1138 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1139 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1140 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1146 while (p
-- != state
.pagedims
) {
1147 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1148 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1149 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1150 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1154 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1158 static void recurse_outline (fz_outline
*outline
, int level
)
1163 struct pagedim
*pdim
= state
.pagedims
;
1165 dest
= &outline
->dest
;
1166 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1167 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1169 pdim
= &state
.pagedims
[i
];
1171 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1174 p
.y
= dest
->ld
.gotor
.lt
.y
;
1175 p
= fz_transform_point (pdim
->lctm
, p
);
1178 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1182 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1183 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1185 printd ("o %d %d %d %d %s",
1186 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1188 if (outline
->down
) {
1189 recurse_outline (outline
->down
, level
+ 1);
1191 outline
= outline
->next
;
1195 static void process_outline (void)
1197 fz_outline
*outline
;
1199 if (!state
.needoutline
) return;
1201 state
.needoutline
= 0;
1202 switch (state
.type
) {
1204 outline
= pdf_load_outline (state
.u
.pdf
);
1207 outline
= xps_load_outline (state
.u
.xps
);
1214 recurse_outline (outline
, 0);
1215 fz_free_outline (state
.ctx
, outline
);
1219 static char *strofspan (fz_text_span
*span
)
1224 size_t size
= 0, cap
= 80;
1226 p
= malloc (cap
+ 1);
1227 if (!p
) return NULL
;
1229 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1230 int n
= fz_runetochar (utf8
, ch
->c
);
1231 if (size
+ n
> cap
) {
1233 p
= realloc (p
, cap
+ 1);
1234 if (!p
) return NULL
;
1237 memcpy (p
+ size
, utf8
, n
);
1244 static int matchspan (regex_t
*re
, fz_text_span
*span
, fz_matrix ctm
,
1245 int stop
, int pageno
, double start
)
1252 fz_point p1
, p2
, p3
, p4
;
1254 p
= strofspan (span
);
1257 ret
= regexec (re
, p
, 1, &rm
, 0);
1260 if (ret
!= REG_NOMATCH
) {
1263 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1264 printd ("msg regexec error `%.*s'",
1265 (int) size
, errbuf
);
1272 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1273 c
+= fz_runelen (span
->text
[a
].c
);
1275 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1276 c
+= fz_runelen (span
->text
[b
].c
);
1279 if (fz_runelen (span
->text
[b
].c
) > 1) {
1282 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1283 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
1295 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1302 printd ("progress 1 found at %d `%.*s' in %f sec",
1303 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1307 printd ("match %d %d %f %f %f %f %f %f %f %f",
1319 static int compareblocks (const void *l
, const void *r
)
1321 fz_text_block
const *ls
= l
;
1322 fz_text_block
const* rs
= r
;
1323 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1326 /* wishful thinking function */
1327 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1332 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1334 fz_text_sheet
*sheet
;
1335 struct pagedim
*pdim
, *pdimprev
;
1336 int stop
= 0, niters
= 0;
1339 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1343 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1344 if (niters
++ == 5) {
1347 printd ("progress 1 attention requested aborting search at %d",
1352 printd ("progress %f searching in page %d",
1353 (double) (pageno
+ 1) / state
.pagecount
,
1358 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1359 pdim
= &state
.pagedims
[i
];
1360 if (pdim
->pageno
== pageno
) {
1363 if (pdim
->pageno
> pageno
) {
1372 sheet
= fz_new_text_sheet (state
.ctx
);
1373 text
= fz_new_text_page (state
.ctx
, fz_infinite_rect
);
1374 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1376 switch (state
.type
) {
1379 fz_try (state
.ctx
) {
1380 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1381 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1382 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1383 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, ctm
, NULL
);
1385 fz_catch (state
.ctx
) {
1386 fz_free_device (tdev
);
1393 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1395 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, ctm
, NULL
);
1399 ARSERT (0 && state
.type
);
1402 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1403 fz_free_device (tdev
);
1405 for (j
= 0; j
< text
->len
; ++j
) {
1407 fz_text_block
*block
;
1409 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1411 for (k
= 0; k
< block
->len
; ++k
) {
1416 line
= &block
->lines
[k
];
1417 if (line
->bbox
.y0
< y
+ 1) continue;
1420 line
= &block
->lines
[block
->len
- 1 - k
];
1421 if (line
->bbox
.y0
> y
- 1) continue;
1424 for (span
= line
->spans
;
1425 span
< line
->spans
+ line
->len
;
1428 switch (matchspan (re
, span
, ctm
, stop
, pageno
, start
)) {
1430 case 1: stop
= 1; break;
1431 case -1: stop
= 1; goto endloop
;
1446 fz_free_text_page (state
.ctx
, text
);
1447 fz_free_text_sheet (state
.ctx
, sheet
);
1449 state
.freepage (u
.ptr
);
1454 printd ("progress 1 no matches %f sec", end
- start
);
1456 printd ("clearrects");
1459 static void set_tex_params (int colorspace
)
1466 switch (colorspace
) {
1468 state
.texiform
= GL_RGBA8
;
1469 state
.texform
= GL_RGBA
;
1470 state
.texty
= GL_UNSIGNED_BYTE
;
1471 state
.colorspace
= fz_device_rgb
;
1474 state
.texiform
= GL_RGBA8
;
1475 state
.texform
= GL_BGRA
;
1476 state
.texty
= endianness
.s
> 1
1477 ? GL_UNSIGNED_INT_8_8_8_8
1478 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1479 state
.colorspace
= fz_device_bgr
;
1482 state
.texiform
= GL_LUMINANCE_ALPHA
;
1483 state
.texform
= GL_LUMINANCE_ALPHA
;
1484 state
.texty
= GL_UNSIGNED_BYTE
;
1485 state
.colorspace
= fz_device_gray
;
1488 errx (1, "invalid colorspce %d", colorspace
);
1492 static void realloctexts (int texcount
)
1496 if (texcount
== state
.texcount
) return;
1498 if (texcount
< state
.texcount
) {
1499 glDeleteTextures (state
.texcount
- texcount
,
1500 state
.texids
+ texcount
);
1503 size
= texcount
* sizeof (*state
.texids
);
1504 state
.texids
= realloc (state
.texids
, size
);
1505 if (!state
.texids
) {
1506 err (1, "realloc texids %" FMT_s
, size
);
1509 size
= texcount
* sizeof (*state
.texowners
);
1510 state
.texowners
= realloc (state
.texowners
, size
);
1511 if (!state
.texowners
) {
1512 err (1, "realloc texowners %" FMT_s
, size
);
1514 if (texcount
> state
.texcount
) {
1517 glGenTextures (texcount
- state
.texcount
,
1518 state
.texids
+ state
.texcount
);
1519 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1520 state
.texowners
[i
].w
= -1;
1521 state
.texowners
[i
].slice
= NULL
;
1524 state
.texcount
= texcount
;
1528 static char *mbtoutf8 (char *s
)
1534 len
= mbstowcs (NULL
, s
, strlen (s
));
1539 if (len
== (size_t) -1) {
1544 tmp
= malloc (len
* sizeof (wchar_t));
1549 ret
= mbstowcs (tmp
, s
, len
);
1550 if (ret
== (size_t) -1) {
1556 for (i
= 0; i
< ret
; ++i
) {
1557 len
+= fz_runelen (tmp
[i
]);
1560 p
= r
= malloc (len
+ 1);
1566 for (i
= 0; i
< ret
; ++i
) {
1567 p
+= fz_runetochar (p
, tmp
[i
]);
1574 CAMLprim value
ml_mbtoutf8 (value s_v
)
1580 s
= String_val (s_v
);
1586 ret_v
= caml_copy_string (r
);
1592 static void * mainloop (void *unused
)
1595 int len
, ret
, oldlen
= 0;
1600 errx (1, "readlen returned 0");
1603 if (oldlen
< len
+ 1) {
1604 p
= realloc (p
, len
+ 1);
1606 err (1, "realloc %d failed", len
+ 1);
1613 if (!strncmp ("open", p
, 4)) {
1616 char *filename
= p
+ 5;
1619 filenamelen
= strlen (filename
);
1620 password
= filename
+ filenamelen
+ 1;
1622 openxref (filename
, password
);
1625 utf8filename
= mbtoutf8 (filename
);
1626 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1627 if (utf8filename
!= filename
) {
1628 free (utf8filename
);
1630 state
.needoutline
= 1;
1632 else if (!strncmp ("cs", p
, 2)) {
1635 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1637 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1640 set_tex_params (colorspace
);
1641 for (i
= 0; i
< state
.texcount
; ++i
) {
1642 state
.texowners
[i
].w
= -1;
1643 state
.texowners
[i
].slice
= NULL
;
1647 else if (!strncmp ("freepage", p
, 8)) {
1650 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1652 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1656 else if (!strncmp ("freetile", p
, 8)) {
1659 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1661 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1665 else if (!strncmp ("search", p
, 6)) {
1666 int icase
, pageno
, y
, ret
, len2
, forward
;
1670 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1671 &icase
, &pageno
, &y
, &forward
, &len2
);
1673 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1676 pattern
= p
+ 6 + len2
;
1677 ret
= regcomp (&re
, pattern
,
1678 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1683 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1684 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1687 search (&re
, pageno
, y
, forward
);
1691 else if (!strncmp ("geometry", p
, 8)) {
1695 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1697 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1705 for (i
= 0; i
< state
.texcount
; ++i
) {
1706 state
.texowners
[i
].slice
= NULL
;
1712 unlock ("geometry");
1713 printd ("continue %d", state
.pagecount
);
1715 else if (!strncmp ("reqlayout", p
, 9)) {
1716 int rotate
, proportional
;
1719 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1721 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1724 if (state
.rotate
!= rotate
|| state
.proportional
!= proportional
) {
1727 state
.rotate
= rotate
;
1728 state
.proportional
= proportional
;
1732 unlock ("reqlayout");
1733 printd ("continue %d", state
.pagecount
);
1735 else if (!strncmp ("page", p
, 4)) {
1738 int pageno
, pindex
, ret
;
1740 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1742 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1747 page
= loadpage (pageno
, pindex
);
1751 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1753 else if (!strncmp ("tile", p
, 4)) {
1754 int x
, y
, w
, h
, ret
;
1760 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1761 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1763 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1768 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1772 printd ("tile %d %d %" FMT_ptr
" %u %f",
1774 FMT_ptr_cast2 (tile
),
1775 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1778 else if (!strncmp ("settrim", p
, 7)) {
1782 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1783 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1785 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1789 state
.trimmargins
= trimmargins
;
1790 state
.needoutline
= 1;
1791 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1793 state
.trimfuzz
= fuzz
;
1795 state
.pagedimcount
= 0;
1796 free (state
.pagedims
);
1797 state
.pagedims
= NULL
;
1802 printd ("continue %d", state
.pagecount
);
1804 else if (!strncmp ("sliceh", p
, 6)) {
1807 ret
= sscanf (p
+ 6, " %d", &h
);
1809 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1811 if (h
!= state
.sliceheight
) {
1814 state
.sliceheight
= h
;
1815 for (i
= 0; i
< state
.texcount
; ++i
) {
1816 state
.texowners
[i
].w
= -1;
1817 state
.texowners
[i
].h
= -1;
1818 state
.texowners
[i
].slice
= NULL
;
1822 else if (!strncmp ("interrupt", p
, 9)) {
1823 printd ("vmsg interrupted");
1826 errx (1, "unknown command %.*s", len
, p
);
1832 CAMLprim value
ml_realloctexts (value texcount_v
)
1834 CAMLparam1 (texcount_v
);
1837 if (trylock ("ml_realloctexts")) {
1841 realloctexts (Int_val (texcount_v
));
1843 unlock ("ml_realloctexts");
1846 CAMLreturn (Val_bool (ok
));
1849 static void showsel (struct page
*page
, int ox
, int oy
)
1855 fz_text_block
*block
;
1856 struct mark first
, last
;
1858 first
= page
->fmark
;
1861 if (!first
.span
|| !last
.span
) return;
1863 glEnable (GL_BLEND
);
1864 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1865 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1867 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1868 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1869 for (block
= page
->text
->blocks
;
1870 block
< page
->text
->blocks
+ page
->text
->len
;
1872 for (line
= block
->lines
;
1873 line
< block
->lines
+ block
->len
;
1875 for (span
= line
->spans
;
1876 span
< line
->spans
+ line
->len
;
1880 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1885 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1887 j
= MIN (first
.i
, last
.i
);
1888 k
= MAX (first
.i
, last
.i
);
1890 else if (span
== first
.span
) {
1894 else if (span
== last
.span
) {
1900 for (i
= j
; i
<= k
; ++i
) {
1901 fz_bbox b
= fz_round_rect (span
->text
[i
].bbox
);
1902 bbox
= fz_union_bbox (bbox
, b
);
1904 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1911 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1912 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1913 if (span
== last
.span
) {
1921 glDisable (GL_BLEND
);
1926 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1929 fz_link
*link
, *links
;
1931 switch (page
->type
) {
1933 links
= page
->u
.pdfpage
->links
;
1937 links
= page
->u
.xpspage
->links
;
1944 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1945 glEnable (GL_LINE_STIPPLE
);
1946 glLineStipple (0.5, 0xcccc);
1948 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1949 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1950 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1953 for (link
= links
; link
; link
= link
->next
) {
1954 fz_point p1
, p2
, p3
, p4
;
1956 p1
.x
= link
->rect
.x0
;
1957 p1
.y
= link
->rect
.y0
;
1959 p2
.x
= link
->rect
.x1
;
1960 p2
.y
= link
->rect
.y0
;
1962 p3
.x
= link
->rect
.x1
;
1963 p3
.y
= link
->rect
.y1
;
1965 p4
.x
= link
->rect
.x0
;
1966 p4
.y
= link
->rect
.y1
;
1968 p1
= fz_transform_point (ctm
, p1
);
1969 p2
= fz_transform_point (ctm
, p2
);
1970 p3
= fz_transform_point (ctm
, p3
);
1971 p4
= fz_transform_point (ctm
, p4
);
1973 switch (link
->dest
.kind
) {
1974 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1975 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1976 default: glColor3ub (0, 0, 0); break;
1979 glVertex2f (p1
.x
, p1
.y
);
1980 glVertex2f (p2
.x
, p2
.y
);
1981 glVertex2f (p3
.x
, p3
.y
);
1982 glVertex2f (p4
.x
, p4
.y
);
1986 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1987 glDisable (GL_LINE_STIPPLE
);
1990 static int compareslinks (const void *l
, const void *r
)
1992 struct slink
const *ls
= l
;
1993 struct slink
const *rs
= r
;
1994 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1995 return rs
->bbox
.x0
- rs
->bbox
.x0
;
1997 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2000 static void droptext (struct page
*page
)
2003 fz_free_text_page (state
.ctx
, page
->text
);
2006 page
->fmark
.span
= NULL
;
2007 page
->lmark
.span
= NULL
;
2011 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2015 static void dropslinks (struct page
*page
)
2018 free (page
->slinks
);
2019 page
->slinks
= NULL
;
2020 page
->slinkcount
= 0;
2024 static void ensureslinks (struct page
*page
)
2028 size_t slinksize
= sizeof (*page
->slinks
);
2029 fz_link
*link
, *links
;
2031 if (state
.gen
!= page
->sgen
) {
2033 page
->sgen
= state
.gen
;
2035 if (page
->slinks
) return;
2037 switch (page
->type
) {
2039 links
= page
->u
.pdfpage
->links
;
2040 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
2041 state
.pagedims
[page
->pdimno
].ctm
);
2045 links
= page
->u
.xpspage
->links
;
2046 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2053 for (link
= links
; link
; link
= link
->next
) {
2057 page
->slinkcount
= count
;
2058 page
->slinks
= calloc (count
, slinksize
);
2059 if (!page
->slinks
) {
2060 err (1, "realloc slinks %d", count
);
2063 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2064 page
->slinks
[i
].link
= link
;
2065 page
->slinks
[i
].bbox
=
2066 fz_round_rect (fz_transform_rect (ctm
, link
->rect
));
2068 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2072 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2073 static void fmt_linkn (char *s
, unsigned int u
)
2075 unsigned int len
; unsigned int q
;
2076 int zma
= 'z' - 'a' + 1;
2078 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2081 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2082 /* handles u == 0 */
2087 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2088 int noff
, char *targ
, int tlen
, int hfsize
)
2092 struct slink
*slink
;
2093 double x0
, y0
, x1
, y1
, w
;
2095 ensureslinks (page
);
2096 glColor3ub (0xc3, 0xb0, 0x91);
2097 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2098 fmt_linkn (buf
, i
+ noff
);
2099 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2100 slink
= &page
->slinks
[i
];
2102 x0
= slink
->bbox
.x0
+ xoff
- 5;
2103 y1
= slink
->bbox
.y0
+ yoff
- 5;
2104 y0
= y1
+ 10 + hfsize
;
2105 w
= measure_string (state
.face
, hfsize
, buf
);
2107 glRectd (x0
, y0
, x1
, y1
);
2111 glEnable (GL_BLEND
);
2112 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2113 glEnable (GL_TEXTURE_2D
);
2114 glColor3ub (0, 0, 0);
2115 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2116 fmt_linkn (buf
, i
+ noff
);
2117 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2118 slink
= &page
->slinks
[i
];
2120 x0
= slink
->bbox
.x0
+ xoff
;
2121 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2122 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2125 glDisable (GL_TEXTURE_2D
);
2126 glDisable (GL_BLEND
);
2130 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2133 struct slice
*slice1
;
2134 unsigned char *texdata
;
2137 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2138 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2140 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2141 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2142 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2146 int texindex
= state
.texindex
++ % state
.texcount
;
2148 if (state
.texowners
[texindex
].w
== tile
->w
) {
2149 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2153 state
.texowners
[texindex
].h
= slice
->h
;
2157 state
.texowners
[texindex
].h
= slice
->h
;
2160 state
.texowners
[texindex
].w
= tile
->w
;
2161 state
.texowners
[texindex
].slice
= slice
;
2162 slice
->texindex
= texindex
;
2164 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2166 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2170 texdata
= tile
->pixmap
->samples
;
2173 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2185 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2197 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2202 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2204 CAMLparam2 (args_v
, ptr_v
);
2205 int dispx
= Int_val (Field (args_v
, 0));
2206 int dispy
= Int_val (Field (args_v
, 1));
2207 int dispw
= Int_val (Field (args_v
, 2));
2208 int disph
= Int_val (Field (args_v
, 3));
2209 int tilex
= Int_val (Field (args_v
, 4));
2210 int tiley
= Int_val (Field (args_v
, 5));
2211 char *s
= String_val (ptr_v
);
2212 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2214 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2216 int slicey
, firstslice
;
2217 struct slice
*slice
;
2219 firstslice
= tiley
/ tile
->sliceheight
;
2220 slice
= &tile
->slices
[firstslice
];
2221 slicey
= tiley
% tile
->sliceheight
;
2226 dh
= slice
->h
- slicey
;
2227 dh
= MIN (disph
, dh
);
2228 uploadslice (tile
, slice
);
2232 glTexCoord2i (tilex
, slicey
);
2233 glVertex2i (dispx
, dispy
);
2235 glTexCoord2i (tilex
+dispw
, slicey
);
2236 glVertex2i (dispx
+dispw
, dispy
);
2238 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2239 glVertex2i (dispx
+dispw
, dispy
+dh
);
2241 glTexCoord2i (tilex
, slicey
+dh
);
2242 glVertex2i (dispx
, dispy
+dh
);
2249 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2253 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2254 CAMLreturn (Val_unit
);
2257 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2258 value xoff_v
, value yoff_v
,
2261 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2262 int xoff
= Int_val (xoff_v
);
2263 int yoff
= Int_val (yoff_v
);
2264 int noff
= Int_val (Field (li_v
, 0));
2265 char *targ
= String_val (Field (li_v
, 1));
2266 int tlen
= caml_string_length (Field (li_v
, 1));
2267 int hfsize
= Int_val (Field (li_v
, 2));
2268 char *s
= String_val (ptr_v
);
2269 int hlmask
= Int_val (hlinks_v
);
2270 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2273 /* deal with loadpage failed pages */
2277 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2278 if (trylock ("ml_postprocess")) {
2283 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2284 noff
= page
->slinkcount
;
2286 showsel (page
, xoff
, yoff
);
2287 unlock ("ml_postprocess");
2290 CAMLreturn (Val_int (noff
));
2293 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2297 fz_link
*link
, *links
;
2299 switch (page
->type
) {
2301 ctm
= trimctm (page
->u
.pdfpage
, page
->pdimno
);
2302 links
= page
->u
.pdfpage
->links
;
2307 links
= page
->u
.xpspage
->links
;
2316 ctm
= fz_concat (ctm
, state
.pagedims
[page
->pdimno
].ctm
);
2317 ctm
= fz_invert_matrix (ctm
);
2318 p
= fz_transform_point (ctm
, p
);
2320 for (link
= links
; link
; link
= link
->next
) {
2321 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2322 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2330 static void ensuretext (struct page
*page
)
2332 if (state
.gen
!= page
->tgen
) {
2334 page
->tgen
= state
.gen
;
2339 page
->text
= fz_new_text_page (state
.ctx
, fz_infinite_rect
);
2340 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2341 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2342 fz_run_display_list (page
->dlist
,
2345 fz_infinite_bbox
, NULL
);
2346 qsort (page
->text
->blocks
, page
->text
->len
,
2347 sizeof (*page
->text
->blocks
), compareblocks
);
2348 fz_free_device (tdev
);
2352 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2354 CAMLparam2 (start_page_v
, dir_v
);
2356 int i
, dir
= Int_val (dir_v
);
2357 int start_page
= Int_val (start_page_v
);
2358 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2360 ret_v
= Val_int (0);
2361 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2365 lock ("ml_findpage_with_links");
2366 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2369 switch (state
.type
) {
2372 pdf_page
*page
= NULL
;
2374 fz_try (state
.ctx
) {
2375 page
= pdf_load_page (state
.u
.pdf
, i
);
2376 found
= !!page
->links
;
2378 fz_catch (state
.ctx
) {
2388 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2389 found
= !!page
->links
;
2395 ARSERT ("invalid document type");
2399 ret_v
= caml_alloc_small (1, 1);
2400 Field (ret_v
, 0) = Val_int (i
);
2405 unlock ("ml_findpage_with_links");
2411 enum { dir_first
, dir_last
};
2412 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2414 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2416 CAMLparam2 (ptr_v
, dir_v
);
2417 CAMLlocal2 (ret_v
, pos_v
);
2419 int dirtag
, i
, slinkindex
;
2420 struct slink
*found
= NULL
,*slink
;
2421 char *s
= String_val (ptr_v
);
2423 page
= parse_pointer ("ml_findlink", s
);
2424 ret_v
= Val_int (0);
2425 if (trylock ("ml_findlink")) {
2429 ensureslinks (page
);
2431 if (Is_block (dir_v
)) {
2432 dirtag
= Tag_val (dir_v
);
2434 case dir_first_visible
:
2436 int x0
, y0
, dir
, first_index
, last_index
;
2438 pos_v
= Field (dir_v
, 0);
2439 x0
= Int_val (Field (pos_v
, 0));
2440 y0
= Int_val (Field (pos_v
, 1));
2441 dir
= Int_val (Field (pos_v
, 2));
2446 last_index
= page
->slinkcount
;
2449 first_index
= page
->slinkcount
- 1;
2453 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2454 slink
= &page
->slinks
[i
];
2455 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2464 slinkindex
= Int_val (Field (dir_v
, 0));
2465 found
= &page
->slinks
[slinkindex
];
2466 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2467 slink
= &page
->slinks
[i
];
2468 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2476 slinkindex
= Int_val (Field (dir_v
, 0));
2477 found
= &page
->slinks
[slinkindex
];
2478 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2479 slink
= &page
->slinks
[i
];
2480 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2488 slinkindex
= Int_val (Field (dir_v
, 0));
2489 found
= &page
->slinks
[slinkindex
];
2490 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2491 slink
= &page
->slinks
[i
];
2492 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2500 slinkindex
= Int_val (Field (dir_v
, 0));
2501 found
= &page
->slinks
[slinkindex
];
2502 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2503 slink
= &page
->slinks
[i
];
2504 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2513 dirtag
= Int_val (dir_v
);
2516 found
= page
->slinks
;
2521 found
= page
->slinks
+ (page
->slinkcount
- 1);
2527 ret_v
= caml_alloc_small (2, 1);
2528 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2531 unlock ("ml_findlink");
2536 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2542 switch (link->dest.kind) { \
2543 case FZ_LINK_GOTO: \
2547 pageno = link->dest.ld.gotor.page; \
2551 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2552 p.y = link->dest.ld.gotor.lt.y; \
2553 p = fz_transform_point (pdim->lctm, p); \
2555 tup_v = caml_alloc_tuple (2); \
2556 ret_v = caml_alloc_small (1, ugoto); \
2557 Field (tup_v, 0) = Val_int (pageno); \
2558 Field (tup_v, 1) = Val_int (p.y); \
2559 Field (ret_v, 0) = tup_v; \
2564 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2565 ret_v = caml_alloc_small (1, uuri); \
2566 Field (ret_v, 0) = str_v; \
2569 case FZ_LINK_LAUNCH: \
2570 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2571 ret_v = caml_alloc_small (1, ulaunch); \
2572 Field (ret_v, 0) = str_v; \
2575 case FZ_LINK_NAMED: \
2576 str_v = caml_copy_string (link->dest.ld.named.named); \
2577 ret_v = caml_alloc_small (1, unamed); \
2578 Field (ret_v, 0) = str_v; \
2581 case FZ_LINK_GOTOR: \
2582 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2583 pageno = link->dest.ld.gotor.page; \
2584 tup_v = caml_alloc_tuple (2); \
2585 ret_v = caml_alloc_small (1, uremote); \
2586 Field (tup_v, 0) = str_v; \
2587 Field (tup_v, 1) = Val_int (pageno); \
2588 Field (ret_v, 0) = tup_v; \
2595 snprintf (buf, sizeof (buf), \
2596 "unhandled link kind %d", link->dest.kind); \
2597 str_v = caml_copy_string (buf); \
2598 ret_v = caml_alloc_small (1, uunexpected); \
2599 Field (ret_v, 0) = str_v; \
2605 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2607 CAMLparam2 (ptr_v
, n_v
);
2608 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2611 struct pagedim
*pdim
;
2612 char *s
= String_val (ptr_v
);
2614 ret_v
= Val_int (0);
2615 if (trylock ("ml_getlink")) {
2619 page
= parse_pointer ("ml_getlink", s
);
2620 ensureslinks (page
);
2621 pdim
= &state
.pagedims
[page
->pdimno
];
2622 link
= page
->slinks
[Int_val (n_v
)].link
;
2625 unlock ("ml_getlink");
2630 CAMLprim value
ml_getlinkcount (value ptr_v
)
2634 char *s
= String_val (ptr_v
);
2636 page
= parse_pointer ("ml_getlinkcount", s
);
2637 CAMLreturn (Val_int (page
->slinkcount
));
2640 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2642 CAMLparam2 (ptr_v
, n_v
);
2645 struct slink
*slink
;
2646 char *s
= String_val (ptr_v
);
2648 page
= parse_pointer ("ml_getlinkrect", s
);
2649 ret_v
= caml_alloc_tuple (4);
2650 if (trylock ("ml_getlinkrect")) {
2651 Field (ret_v
, 0) = Val_int (0);
2652 Field (ret_v
, 1) = Val_int (0);
2653 Field (ret_v
, 2) = Val_int (0);
2654 Field (ret_v
, 3) = Val_int (0);
2657 ensureslinks (page
);
2659 slink
= &page
->slinks
[Int_val (n_v
)];
2660 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2661 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2662 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2663 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2664 unlock ("ml_getlinkrect");
2670 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2672 CAMLparam3 (ptr_v
, x_v
, y_v
);
2673 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2676 char *s
= String_val (ptr_v
);
2677 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2678 struct pagedim
*pdim
;
2680 ret_v
= Val_int (0);
2681 if (trylock ("ml_whatsunder")) {
2685 page
= parse_pointer ("ml_whatsunder", s
);
2686 pdim
= &state
.pagedims
[page
->pdimno
];
2687 x
+= pdim
->bounds
.x0
;
2688 y
+= pdim
->bounds
.y0
;
2689 link
= getlink (page
, x
, y
);
2695 fz_text_block
*block
;
2698 for (block
= page
->text
->blocks
;
2699 block
< page
->text
->blocks
+ page
->text
->len
;
2704 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2707 for (line
= block
->lines
;
2708 line
< block
->lines
+ block
->len
;
2713 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2716 for (span
= line
->spans
;
2717 span
< line
->spans
+ line
->len
;
2722 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2725 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
2728 if (x
>= b
->x0
&& x
<= b
->x1
2729 && y
>= b
->y0
&& y
<= b
->y1
) {
2731 span
->style
->font
&& span
->style
->font
->name
2732 ? span
->style
->font
->name
2733 : "Span has no font name"
2735 FT_FaceRec
*face
= span
->style
->font
->ft_face
;
2736 if (face
&& face
->family_name
) {
2738 char *n1
= face
->family_name
;
2739 size_t l1
= strlen (n1
);
2740 size_t l2
= strlen (n2
);
2742 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2743 s
= malloc (l1
+ l2
+ 2);
2747 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2748 str_v
= caml_copy_string (s
);
2754 str_v
= caml_copy_string (n2
);
2756 ret_v
= caml_alloc_small (1, utext
);
2757 Field (ret_v
, 0) = str_v
;
2766 unlock ("ml_whatsunder");
2772 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2774 CAMLparam2 (ptr_v
, rect_v
);
2777 struct pagedim
*pdim
;
2778 int i
, x0
, x1
, y0
, y1
;
2779 char *s
= String_val (ptr_v
);
2781 fz_text_block
*block
;
2782 fz_text_span
*span
, *fspan
, *lspan
;
2783 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2785 if (trylock ("ml_seltext")) {
2789 page
= parse_pointer ("ml_seltext", s
);
2792 pdim
= &state
.pagedims
[page
->pdimno
];
2793 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2794 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2795 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2796 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2799 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2800 glColor3ub (128, 128, 128);
2801 glRecti (x0
, y0
, x1
, y1
);
2802 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2805 fspan
= lspan
= NULL
;
2807 for (block
= page
->text
->blocks
;
2808 block
< page
->text
->blocks
+ page
->text
->len
;
2810 for (line
= block
->lines
;
2811 line
< block
->lines
+ block
->len
;
2813 for (span
= line
->spans
;
2814 span
< line
->spans
+ line
->len
;
2816 for (i
= 0; i
< span
->len
; ++i
) {
2817 b
= &span
->text
[i
].bbox
;
2820 if (x0
>= b
->x0
&& x0
<= b
->x1
2821 && y0
>= b
->y0
&& y0
<= b
->y1
) {
2827 if (x1
>= b
->x0
&& x1
<= b
->x1
2828 && y1
>= b
->y0
&& y1
<= b
->y1
) {
2834 if (0 && selected
) {
2835 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2836 glColor3ub (128, 128, 128);
2837 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2838 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2844 if (y1
< y0
|| x1
< x0
) {
2847 if (fspan
== lspan
) {
2852 if (fline
!= lline
) {
2871 page
->fmark
.span
= fspan
;
2874 page
->lmark
.span
= lspan
;
2876 unlock ("ml_seltext");
2879 CAMLreturn (Val_unit
);
2882 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2887 for (i
= a
; i
<= b
; ++i
) {
2888 len
= fz_runetochar (buf
, span
->text
[i
].c
);
2889 ret
= fwrite (buf
, len
, 1, f
);
2892 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2893 len
, ret
, strerror (errno
));
2901 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
2903 caml_failwith ("ml_popen not implemented under Cygwin");
2906 CAMLprim value
ml_popen (value command_v
, value fds_v
)
2908 CAMLparam2 (command_v
, fds_v
);
2909 CAMLlocal2 (l_v
, tup_v
);
2912 value earg_v
= Nothing
;
2913 posix_spawnattr_t attr
;
2914 posix_spawn_file_actions_t fa
;
2915 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
2917 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
2918 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
2921 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
2922 msg
= "posix_spawnattr_init";
2926 #ifdef POSIX_SPAWN_USEVFORK
2927 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
2928 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
2933 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
2936 tup_v
= Field (l_v
, 0);
2937 fd1
= Int_val (Field (tup_v
, 0));
2938 fd2
= Int_val (Field (tup_v
, 1));
2940 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
2941 msg
= "posix_spawn_file_actions_addclose";
2947 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
2948 msg
= "posix_spawn_file_actions_adddup2";
2955 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
2956 msg
= "posix_spawn";
2961 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
2962 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
2966 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
2967 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
2972 unix_error (ret
, msg
, earg_v
);
2974 CAMLreturn (Val_unit
);
2978 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
2986 fz_text_block
*block
;
2987 int fd
= Int_val (fd_v
);
2988 char *s
= String_val (ptr_v
);
2990 if (trylock ("ml_copysel")) {
2994 page
= parse_pointer ("ml_sopysel", s
);
2996 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2997 fprintf (stderr
, "nothing to copy\n");
3001 f
= fdopen (fd
, "w");
3003 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3008 for (block
= page
->text
->blocks
;
3009 block
< page
->text
->blocks
+ page
->text
->len
;
3011 for (line
= block
->lines
;
3012 line
< block
->lines
+ block
->len
;
3014 for (span
= line
->spans
;
3015 span
< line
->spans
+ line
->len
;
3019 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3020 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3021 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3024 if (pipespan (f
, span
, a
, b
)) {
3027 if (span
== line
->spans
+ line
->len
- 1) {
3028 if (putc ('\n', f
) == EOF
) {
3030 "failed break line on sel pipe: %s\n",
3035 if (span
== page
->lmark
.span
) {
3043 page
->lmark
.span
= NULL
;
3044 page
->fmark
.span
= NULL
;
3048 int ret
= fclose (f
);
3051 if (errno
!= ECHILD
) {
3052 fprintf (stderr
, "failed to close sel pipe: %s\n",
3058 unlock ("ml_copysel");
3063 fprintf (stderr
, "failed to close sel pipe: %s\n",
3067 CAMLreturn (Val_unit
);
3070 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3072 CAMLparam1 (pagedimno_v
);
3074 int pagedimno
= Int_val (pagedimno_v
);
3077 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3078 if (trylock ("ml_getpdimrect")) {
3079 box
= fz_empty_rect
;
3082 box
= state
.pagedims
[pagedimno
].mediabox
;
3083 unlock ("ml_getpdimrect");
3086 Store_double_field (ret_v
, 0, box
.x0
);
3087 Store_double_field (ret_v
, 1, box
.x1
);
3088 Store_double_field (ret_v
, 2, box
.y0
);
3089 Store_double_field (ret_v
, 3, box
.y1
);
3094 static double getmaxw (void)
3100 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3103 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
3104 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
3107 maxw
= MAX (w
, maxw
);
3112 CAMLprim value
ml_getmaxw (value unit_v
)
3114 CAMLparam1 (unit_v
);
3118 if (trylock ("ml_getmaxw")) {
3122 unlock ("ml_getmaxw");
3124 ret_v
= caml_copy_double (maxw
);
3128 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3129 value dw_v
, value cols_v
)
3131 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3135 double maxw
= 0.0, maxh
= 0.0;
3137 double winw
= Int_val (winw_v
);
3138 double winh
= Int_val (winh_v
);
3139 double dw
= Int_val (dw_v
);
3140 double cols
= Int_val (cols_v
);
3141 double pw
= 1.0, ph
= 1.0, aspect
;
3143 if (trylock ("ml_zoom_for_height")) {
3147 if (state
.proportional
) {
3148 maxw
= getmaxw () / cols
;
3151 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3152 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
3155 rect
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
3157 x0
= MIN (rect
.x0
, rect
.x1
);
3158 x1
= MAX (rect
.x0
, rect
.x1
);
3159 y0
= MIN (rect
.y0
, rect
.y1
);
3160 y1
= MAX (rect
.y0
, rect
.y1
);
3162 w
= (x1
- x0
) / cols
;
3165 if (state
.proportional
) {
3167 scaledh
= h
* scale
;
3174 if (scaledh
> maxh
) {
3182 zoom
= (winh
* aspect
+ dw
) / winw
;
3184 unlock ("ml_zoom_for_height");
3186 ret_v
= caml_copy_double (zoom
);
3190 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3192 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3194 int pt
= Int_val(pt_v
);
3195 int x
= Int_val (x_v
);
3196 int y
= Int_val (y_v
);
3199 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3200 ret_v
= caml_copy_double (w
);
3204 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3206 CAMLparam2 (pt_v
, string_v
);
3208 int pt
= Int_val (pt_v
);
3211 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3212 ret_v
= caml_copy_double (w
);
3216 CAMLprim value
ml_getpagebox (value opaque_v
)
3218 CAMLparam1 (opaque_v
);
3222 char *s
= String_val (opaque_v
);
3223 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3225 ret_v
= caml_alloc_tuple (4);
3226 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
3227 dev
->hints
|= FZ_IGNORE_SHADE
;
3229 switch (page
->type
) {
3231 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
3235 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
3239 bbox
= fz_infinite_bbox
;
3243 fz_free_device (dev
);
3244 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3245 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3246 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3247 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3252 CAMLprim value
ml_setaalevel (value level_v
)
3254 CAMLparam1 (level_v
);
3256 state
.aalevel
= Int_val (level_v
);
3257 CAMLreturn (Val_unit
);
3261 #include <X11/Xlib.h>
3267 GLXDrawable drawable
;
3270 #include "keysym2ucs.c"
3272 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3274 CAMLparam1 (keysym_v
);
3276 KeySym keysym
= Int_val (keysym_v
);
3281 rune
= keysym2ucs (keysym
);
3282 len
= fz_runetochar (buf
, rune
);
3284 str_v
= caml_copy_string (buf
);
3288 CAMLprim value
ml_glx (value win_v
)
3291 XVisualInfo
*visual
;
3292 int screen
, wid
= Int_val (win_v
);
3293 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3295 glx
.dpy
= XOpenDisplay (NULL
);
3297 caml_failwith ("XOpenDisplay");
3300 screen
= DefaultScreen (glx
.dpy
);
3301 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3303 XCloseDisplay (glx
.dpy
);
3305 caml_failwith ("glXChooseVisual");
3308 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3311 XCloseDisplay (glx
.dpy
);
3313 caml_failwith ("glXCreateContext");
3316 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3317 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3318 XCloseDisplay (glx
.dpy
);
3321 caml_failwith ("glXMakeCurrent");
3324 CAMLreturn (Val_unit
);
3327 CAMLprim value
ml_swapb (value unit_v
)
3329 CAMLparam1 (unit_v
);
3330 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3331 CAMLreturn (Val_unit
);
3334 CAMLprim value
ml_glxsync (value unit_v
)
3336 CAMLparam1 (unit_v
);
3337 if (glx
.dpy
&& glx
.ctx
) {
3341 CAMLreturn (Val_unit
);
3344 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3345 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3347 CAMLprim value
ml_platform (value unit_v
)
3349 CAMLparam1 (unit_v
);
3350 int platid
= piunknown
;
3352 #if defined __linux__
3354 #elif defined __CYGWIN__
3356 #elif defined __DragonFly__
3357 platid
= pidragonflybsd
;
3358 #elif defined __FreeBSD__
3360 #elif defined __OpenBSD__
3362 #elif defined __NetBSD__
3364 #elif defined __sun__
3366 #elif defined __APPLE__
3369 CAMLreturn (Val_int (platid
));
3372 CAMLprim value
ml_cloexec (value fd_v
)
3375 int fd
= Int_val (fd_v
);
3377 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3378 uerror ("fcntl", Nothing
);
3380 CAMLreturn (Val_unit
);
3383 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3385 CAMLparam2 (w_v
, h_v
);
3388 int w
= Int_val (w_v
);
3389 int h
= Int_val (h_v
);
3390 int cs
= Int_val (cs_v
);
3392 if (state
.pbo_usable
) {
3393 pbo
= calloc (sizeof (*pbo
), 1);
3395 err (1, "calloc pbo");
3407 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3410 state
.glGenBuffersARB (1, &pbo
->id
);
3411 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3412 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3413 NULL
, GL_STREAM_DRAW
);
3414 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3416 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3418 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3419 state
.glDeleteBuffersARB (1, &pbo
->id
);
3421 ret_v
= caml_copy_string ("0");
3427 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3429 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3433 err (1, "malloc %d bytes failed", res
+1);
3435 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3437 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3439 ret_v
= caml_copy_string (s
);
3444 ret_v
= caml_copy_string ("0");
3449 CAMLprim value
ml_freepbo (value s_v
)
3452 char *s
= String_val (s_v
);
3453 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3456 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3458 tile
->pbo
->ptr
= NULL
;
3459 tile
->pbo
->size
= -1;
3461 CAMLreturn (Val_unit
);
3464 CAMLprim value
ml_unmappbo (value s_v
)
3467 char *s
= String_val (s_v
);
3468 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3471 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3472 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3473 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3475 tile
->pbo
->ptr
= NULL
;
3476 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3478 CAMLreturn (Val_unit
);
3481 static void setuppbo (void)
3483 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3484 GGPA (glBindBufferARB
);
3485 if (state
.glBindBufferARB
) {
3486 GGPA (glUnmapBufferARB
);
3487 if (state
.glUnmapBufferARB
) {
3488 GGPA (glMapBufferARB
);
3489 if (state
.glMapBufferARB
) {
3490 GGPA (glBufferDataARB
);
3491 if (state
.glBufferDataARB
) {
3492 GGPA (glGenBuffersARB
);
3493 if (state
.glGenBuffersARB
) {
3494 GGPA (glDeleteBuffersARB
);
3495 if (state
.glDeleteBuffersARB
) {
3496 state
.pbo_usable
= 1;
3506 CAMLprim value
ml_pbo_usable (value unit_v
)
3508 CAMLparam1 (unit_v
);
3509 CAMLreturn (Val_bool (state
.pbo_usable
));
3512 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3514 CAMLparam2 (pipe_v
, params_v
);
3515 CAMLlocal2 (trim_v
, fuzz_v
);
3522 struct sigaction sa
;
3524 state
.cr
= Int_val (Field (pipe_v
, 0));
3525 state
.cw
= Int_val (Field (pipe_v
, 1));
3526 state
.rotate
= Int_val (Field (params_v
, 0));
3527 state
.proportional
= Bool_val (Field (params_v
, 1));
3528 trim_v
= Field (params_v
, 2);
3529 texcount
= Int_val (Field (params_v
, 3));
3530 state
.sliceheight
= Int_val (Field (params_v
, 4));
3531 mustoresize
= Int_val (Field (params_v
, 5));
3532 colorspace
= Int_val (Field (params_v
, 6));
3533 fontpath
= String_val (Field (params_v
, 7));
3534 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3535 if (!state
.trimcachepath
) {
3536 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3539 haspboext
= Bool_val (Field (params_v
, 9));
3541 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3543 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3544 fuzz_v
= Field (trim_v
, 1);
3545 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3546 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3547 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3548 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3550 set_tex_params (colorspace
);
3553 state
.face
= load_font (fontpath
);
3557 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3559 state
.face
= load_builtin_font (base
, len
);
3561 if (!state
.face
) _exit (1);
3563 realloctexts (texcount
);
3570 sa
.sa_handler
= SIG_IGN
;
3571 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3573 sa
.sa_handler
= SIG_DFL
;
3574 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3576 if (sigemptyset (&sa
.sa_mask
)) {
3577 err (1, "sigemptyset");
3579 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3580 err (1, "sigaction");
3583 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3585 errx (1, "pthread_create: %s", strerror (ret
));
3588 CAMLreturn (Val_unit
);