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 void (*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 freeemptypdfpage (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
;
601 static int cacheline32bytes
;
603 static void __attribute__ ((constructor
)) clcheck (void)
605 char **envp
= environ
;
610 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
612 cacheline32bytes
= auxv
[1] == 32;
618 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
620 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
621 if (cacheline32bytes
&& size
> 32) {
622 intptr_t a1
, a2
, diff
;
624 vector
unsigned char v
= vec_splat_u8 (-1);
625 vector
unsigned char *p
;
627 a1
= a2
= (intptr_t) pixmap
->samples
;
628 a2
= (a1
+ 31) & ~31;
633 while (a1
!= a2
) *(char *) a1
++ = 0xff;
634 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
635 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
637 vec_st (v
, i
+ 16, p
);
639 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
641 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
644 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
647 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
650 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
652 if (!pdim
->tctmready
) {
653 if (state
.trimmargins
) {
656 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
657 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
658 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
659 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
670 static fz_matrix
pagectm (struct page
*page
)
672 if (page
->type
== DPDF
) {
673 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
674 state
.pagedims
[page
->pdimno
].ctm
);
678 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
680 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
681 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
682 -pdim
->mediabox
.y0
), ctm
);
687 static void *loadpage (int pageno
, int pindex
)
690 struct page
*page
= NULL
;
692 page
= calloc (sizeof (struct page
), 1);
694 err (1, "calloc page %d", pageno
);
697 page
->dlist
= fz_new_display_list (state
.ctx
);
698 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
699 switch (state
.type
) {
702 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
703 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
704 page
->freepage
= freepdfpage
;
706 fz_catch (state
.ctx
) {
708 page
->freepage
= freeemptypdfpage
;
713 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
714 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
715 page
->freepage
= freexpspage
;
719 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
720 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
721 page
->freepage
= freecbzpage
;
724 fz_free_device (dev
);
726 page
->pdimno
= pindex
;
727 page
->pageno
= pageno
;
728 page
->sgen
= state
.gen
;
729 page
->tgen
= state
.gen
;
730 page
->type
= state
.type
;
735 static struct tile
*alloctile (int h
)
742 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
743 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
744 tile
= calloc (tilesize
, 1);
746 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
748 for (i
= 0; i
< slicecount
; ++i
) {
749 int sh
= MIN (h
, state
.sliceheight
);
750 tile
->slices
[i
].h
= sh
;
751 tile
->slices
[i
].texindex
= -1;
754 tile
->slicecount
= slicecount
;
755 tile
->sliceheight
= state
.sliceheight
;
765 static void obs_fill_image (fz_device
*dev
, fz_image
*image
, fz_matrix ctm
,
768 struct obs
*obs
= dev
->user
;
770 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
771 fz_bbox b
= fz_round_rect (fz_transform_rect (ctm
, fz_unit_rect
));
772 b
= fz_intersect_bbox (b
, obs
->b
);
773 obs
->cured
= b
.x0
== obs
->b
.x0
776 && b
.y1
== obs
->b
.y1
;
780 static int obscured (struct page
*page
, fz_bbox bbox
)
785 memset (&dev
, 0, sizeof (dev
));
786 memset (&obs
, 0, sizeof (obs
));
791 dev
.fill_image
= obs_fill_image
;
793 fz_run_display_list (page
->dlist
, &dev
, pagectm (page
), bbox
, NULL
);
796 #define OBSCURED obscured
798 #define OBSCURED(a, b) 0
801 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
807 struct pagedim
*pdim
;
809 tile
= alloctile (h
);
810 pdim
= &state
.pagedims
[page
->pdimno
];
815 bbox
.x1
= bbox
.x0
+ w
;
816 bbox
.y1
= bbox
.y0
+ h
;
819 if (state
.pig
->w
== w
821 && state
.pig
->colorspace
== state
.colorspace
) {
822 tile
->pixmap
= state
.pig
;
823 tile
->pixmap
->x
= bbox
.x0
;
824 tile
->pixmap
->y
= bbox
.y0
;
827 fz_drop_pixmap (state
.ctx
, state
.pig
);
834 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
840 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
);
846 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
847 clearpixmap (tile
->pixmap
);
849 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
850 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
851 fz_free_device (dev
);
856 static void initpdims (void)
859 int pageno
, trim
, show
;
865 if (state
.trimmargins
&& state
.trimcachepath
) {
866 trimf
= fopen (state
.trimcachepath
, "rb");
868 trimf
= fopen (state
.trimcachepath
, "wb");
872 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
877 switch (state
.type
) {
879 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
881 if (state
.trimmargins
) {
886 page
= pdf_load_page (state
.u
.pdf
, pageno
);
887 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
888 trim
= state
.trimanew
|| !obj
;
895 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
896 dev
->hints
|= FZ_IGNORE_SHADE
;
897 ctm
= fz_invert_matrix (page
->ctm
);
898 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
899 fz_free_device (dev
);
901 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
902 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
903 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
904 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
905 rect
= fz_transform_rect (ctm
, rect
);
906 rect
= fz_intersect_rect (rect
, page
->mediabox
);
908 if (fz_is_empty_rect (rect
)) {
909 mediabox
= page
->mediabox
;
915 obj
= pdf_new_array (state
.ctx
, 4);
916 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x0
));
917 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y0
));
918 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x1
));
919 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y1
));
920 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
923 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
924 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
925 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
926 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
929 rotate
= page
->rotate
;
930 pdf_free_page (state
.u
.pdf
, page
);
932 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
934 printd ("progress %f Trimming %d",
935 (double) (pageno
+ 1) / state
.pagecount
,
939 fz_catch (state
.ctx
) {
945 mediabox
= pdf_to_rect (state
.ctx
,
946 pdf_dict_gets (pageobj
, "MediaBox"));
947 if (fz_is_empty_rect (mediabox
)) {
948 fprintf (stderr
, "cannot find page size for page %d\n",
956 cropbox
= pdf_to_rect (state
.ctx
,
957 pdf_dict_gets (pageobj
, "CropBox"));
958 if (!fz_is_empty_rect (cropbox
)) {
959 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
961 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
970 page
= xps_load_page (state
.u
.xps
, pageno
);
971 mediabox
= xps_bound_page (state
.u
.xps
, page
);
973 if (state
.trimmargins
) {
978 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
979 dev
->hints
|= FZ_IGNORE_SHADE
;
980 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
981 fz_free_device (dev
);
983 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
984 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
985 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
986 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
987 rect
= fz_intersect_rect (rect
, mediabox
);
989 if (!fz_is_empty_rect (rect
)) {
993 xps_free_page (state
.u
.xps
, page
);
994 printd ("progress %f loading %d",
995 (double) (pageno
+ 1) / state
.pagecount
,
1003 if (state
.trimmargins
&& trimw
) {
1006 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1007 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
1008 cbz_free_page (state
.u
.cbz
, page
);
1009 printd ("progress %f Trimming %d",
1010 (double) (pageno
+ 1) / state
.pagecount
,
1013 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1015 err (1, "fwrite trim mediabox");
1021 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1023 err (1, "fread trim mediabox");
1027 mediabox
.x0
= mediabox
.y0
= 0;
1036 ARSERT (0 && state
.type
);
1039 if (state
.pagedimcount
== 0
1040 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1041 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1044 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1045 state
.pagedims
= realloc (state
.pagedims
, size
);
1046 if (!state
.pagedims
) {
1047 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1048 size
, state
.pagedimcount
+ 1);
1051 p
= &state
.pagedims
[state
.pagedimcount
++];
1053 p
->mediabox
= mediabox
;
1058 if (state
.trimmargins
) {
1059 printd ("progress 1 Trimmed %d pages in %f seconds",
1060 state
.pagecount
, end
- start
);
1063 printd ("vmsg Processed %d pages in %f seconds",
1064 state
.pagecount
, end
- start
);
1068 if (fclose (trimf
)) {
1074 static void layout (void)
1079 double zoom
, w
, maxw
= 0;
1080 struct pagedim
*p
= state
.pagedims
;
1082 if (state
.proportional
) {
1083 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1084 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1086 w
= box
.x1
- box
.x0
;
1087 maxw
= MAX (w
, maxw
);
1092 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1095 ctm
= fz_rotate (state
.rotate
);
1096 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1098 w
= box
.x1
- box
.x0
;
1100 if (state
.proportional
) {
1101 double scale
= w
/ maxw
;
1102 zoom
= (state
.w
/ w
) * scale
;
1108 p
->zoomctm
= fz_scale (zoom
, zoom
);
1109 ctm
= fz_concat (p
->zoomctm
, ctm
);
1111 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1112 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1113 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1116 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1119 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1123 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1124 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1125 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1131 while (p
-- != state
.pagedims
) {
1132 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1133 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1134 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1135 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1139 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1143 static void recurse_outline (fz_outline
*outline
, int level
)
1148 struct pagedim
*pdim
= state
.pagedims
;
1150 dest
= &outline
->dest
;
1151 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1152 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1154 pdim
= &state
.pagedims
[i
];
1156 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1159 p
.y
= dest
->ld
.gotor
.lt
.y
;
1160 p
= fz_transform_point (pdim
->lctm
, p
);
1163 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1167 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1168 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1170 printd ("o %d %d %d %d %s",
1171 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1173 if (outline
->down
) {
1174 recurse_outline (outline
->down
, level
+ 1);
1176 outline
= outline
->next
;
1180 static void process_outline (void)
1182 fz_outline
*outline
;
1184 if (!state
.needoutline
) return;
1186 state
.needoutline
= 0;
1187 switch (state
.type
) {
1189 outline
= pdf_load_outline (state
.u
.pdf
);
1192 outline
= xps_load_outline (state
.u
.xps
);
1199 recurse_outline (outline
, 0);
1200 fz_free_outline (state
.ctx
, outline
);
1204 static char *strofspan (fz_text_span
*span
)
1209 size_t size
= 0, cap
= 80;
1211 p
= malloc (cap
+ 1);
1212 if (!p
) return NULL
;
1214 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1215 int n
= fz_runetochar (utf8
, ch
->c
);
1216 if (size
+ n
> cap
) {
1218 p
= realloc (p
, cap
+ 1);
1219 if (!p
) return NULL
;
1222 memcpy (p
+ size
, utf8
, n
);
1229 static int matchspan (regex_t
*re
, fz_text_span
*span
, fz_matrix ctm
,
1230 int stop
, int pageno
, double start
)
1237 fz_point p1
, p2
, p3
, p4
;
1239 p
= strofspan (span
);
1242 ret
= regexec (re
, p
, 1, &rm
, 0);
1245 if (ret
!= REG_NOMATCH
) {
1248 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1249 printd ("msg regexec error `%.*s'",
1250 (int) size
, errbuf
);
1257 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1258 c
+= fz_runelen (span
->text
[a
].c
);
1260 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1261 c
+= fz_runelen (span
->text
[b
].c
);
1264 if (fz_runelen (span
->text
[b
].c
) > 1) {
1267 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1268 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
1280 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1287 printd ("progress 1 found at %d `%.*s' in %f sec",
1288 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1292 printd ("match %d %d %f %f %f %f %f %f %f %f",
1304 static int compareblocks (const void *l
, const void *r
)
1306 fz_text_block
const *ls
= l
;
1307 fz_text_block
const* rs
= r
;
1308 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1311 /* wishful thinking function */
1312 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1317 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1319 fz_text_sheet
*sheet
;
1320 struct pagedim
*pdim
, *pdimprev
;
1321 int stop
= 0, niters
= 0;
1324 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1328 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1329 if (niters
++ == 5) {
1332 printd ("progress 1 attention requested aborting search at %d",
1337 printd ("progress %f searching in page %d",
1338 (double) (pageno
+ 1) / state
.pagecount
,
1343 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1344 pdim
= &state
.pagedims
[i
];
1345 if (pdim
->pageno
== pageno
) {
1348 if (pdim
->pageno
> pageno
) {
1357 sheet
= fz_new_text_sheet (state
.ctx
);
1358 text
= fz_new_text_page (state
.ctx
, fz_infinite_rect
);
1359 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1361 switch (state
.type
) {
1364 fz_try (state
.ctx
) {
1365 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1366 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1367 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1368 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, ctm
, NULL
);
1370 fz_catch (state
.ctx
) {
1371 fz_free_device (tdev
);
1378 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1380 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, ctm
, NULL
);
1384 ARSERT (0 && state
.type
);
1387 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1388 fz_free_device (tdev
);
1390 for (j
= 0; j
< text
->len
; ++j
) {
1392 fz_text_block
*block
;
1394 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1396 for (k
= 0; k
< block
->len
; ++k
) {
1401 line
= &block
->lines
[k
];
1402 if (line
->bbox
.y0
< y
+ 1) continue;
1405 line
= &block
->lines
[block
->len
- 1 - k
];
1406 if (line
->bbox
.y0
> y
- 1) continue;
1409 for (span
= line
->spans
;
1410 span
< line
->spans
+ line
->len
;
1413 switch (matchspan (re
, span
, ctm
, stop
, pageno
, start
)) {
1415 case 1: stop
= 1; break;
1416 case -1: stop
= 1; goto endloop
;
1431 fz_free_text_page (state
.ctx
, text
);
1432 fz_free_text_sheet (state
.ctx
, sheet
);
1434 state
.freepage (u
.ptr
);
1439 printd ("progress 1 no matches %f sec", end
- start
);
1441 printd ("clearrects");
1444 static void set_tex_params (int colorspace
)
1451 switch (colorspace
) {
1453 state
.texiform
= GL_RGBA8
;
1454 state
.texform
= GL_RGBA
;
1455 state
.texty
= GL_UNSIGNED_BYTE
;
1456 state
.colorspace
= fz_device_rgb
;
1459 state
.texiform
= GL_RGBA8
;
1460 state
.texform
= GL_BGRA
;
1461 state
.texty
= endianness
.s
> 1
1462 ? GL_UNSIGNED_INT_8_8_8_8
1463 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1464 state
.colorspace
= fz_device_bgr
;
1467 state
.texiform
= GL_LUMINANCE_ALPHA
;
1468 state
.texform
= GL_LUMINANCE_ALPHA
;
1469 state
.texty
= GL_UNSIGNED_BYTE
;
1470 state
.colorspace
= fz_device_gray
;
1473 errx (1, "invalid colorspce %d", colorspace
);
1477 static void realloctexts (int texcount
)
1481 if (texcount
== state
.texcount
) return;
1483 if (texcount
< state
.texcount
) {
1484 glDeleteTextures (state
.texcount
- texcount
,
1485 state
.texids
+ texcount
);
1488 size
= texcount
* sizeof (*state
.texids
);
1489 state
.texids
= realloc (state
.texids
, size
);
1490 if (!state
.texids
) {
1491 err (1, "realloc texids %" FMT_s
, size
);
1494 size
= texcount
* sizeof (*state
.texowners
);
1495 state
.texowners
= realloc (state
.texowners
, size
);
1496 if (!state
.texowners
) {
1497 err (1, "realloc texowners %" FMT_s
, size
);
1499 if (texcount
> state
.texcount
) {
1502 glGenTextures (texcount
- state
.texcount
,
1503 state
.texids
+ state
.texcount
);
1504 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1505 state
.texowners
[i
].w
= -1;
1506 state
.texowners
[i
].slice
= NULL
;
1509 state
.texcount
= texcount
;
1513 static char *mbtoutf8 (char *s
)
1519 len
= mbstowcs (NULL
, s
, strlen (s
));
1524 if (len
== (size_t) -1) {
1529 tmp
= malloc (len
* sizeof (wchar_t));
1534 ret
= mbstowcs (tmp
, s
, len
);
1535 if (ret
== (size_t) -1) {
1541 for (i
= 0; i
< ret
; ++i
) {
1542 len
+= fz_runelen (tmp
[i
]);
1545 p
= r
= malloc (len
+ 1);
1551 for (i
= 0; i
< ret
; ++i
) {
1552 p
+= fz_runetochar (p
, tmp
[i
]);
1559 CAMLprim value
ml_mbtoutf8 (value s_v
)
1565 s
= String_val (s_v
);
1571 ret_v
= caml_copy_string (r
);
1577 static void * mainloop (void *unused
)
1580 int len
, ret
, oldlen
= 0;
1585 errx (1, "readlen returned 0");
1588 if (oldlen
< len
+ 1) {
1589 p
= realloc (p
, len
+ 1);
1591 err (1, "realloc %d failed", len
+ 1);
1598 if (!strncmp ("open", p
, 4)) {
1601 char *filename
= p
+ 5;
1604 filenamelen
= strlen (filename
);
1605 password
= filename
+ filenamelen
+ 1;
1607 openxref (filename
, password
);
1610 utf8filename
= mbtoutf8 (filename
);
1611 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1612 if (utf8filename
!= filename
) {
1613 free (utf8filename
);
1615 state
.needoutline
= 1;
1617 else if (!strncmp ("cs", p
, 2)) {
1620 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1622 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1625 set_tex_params (colorspace
);
1626 for (i
= 0; i
< state
.texcount
; ++i
) {
1627 state
.texowners
[i
].w
= -1;
1628 state
.texowners
[i
].slice
= NULL
;
1632 else if (!strncmp ("freepage", p
, 8)) {
1635 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1637 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1641 else if (!strncmp ("freetile", p
, 8)) {
1644 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1646 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1650 else if (!strncmp ("search", p
, 6)) {
1651 int icase
, pageno
, y
, ret
, len2
, forward
;
1655 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1656 &icase
, &pageno
, &y
, &forward
, &len2
);
1658 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1661 pattern
= p
+ 6 + len2
;
1662 ret
= regcomp (&re
, pattern
,
1663 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1668 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1669 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1672 search (&re
, pageno
, y
, forward
);
1676 else if (!strncmp ("geometry", p
, 8)) {
1680 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1682 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1690 for (i
= 0; i
< state
.texcount
; ++i
) {
1691 state
.texowners
[i
].slice
= NULL
;
1697 unlock ("geometry");
1698 printd ("continue %d", state
.pagecount
);
1700 else if (!strncmp ("reqlayout", p
, 9)) {
1701 int rotate
, proportional
;
1704 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1706 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1709 if (state
.rotate
!= rotate
|| state
.proportional
!= proportional
) {
1712 state
.rotate
= rotate
;
1713 state
.proportional
= proportional
;
1717 unlock ("reqlayout");
1718 printd ("continue %d", state
.pagecount
);
1720 else if (!strncmp ("page", p
, 4)) {
1723 int pageno
, pindex
, ret
;
1725 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1727 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1732 page
= loadpage (pageno
, pindex
);
1736 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1738 else if (!strncmp ("tile", p
, 4)) {
1739 int x
, y
, w
, h
, ret
;
1745 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1746 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1748 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1753 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1757 printd ("tile %d %d %" FMT_ptr
" %u %f",
1759 FMT_ptr_cast2 (tile
),
1760 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1763 else if (!strncmp ("settrim", p
, 7)) {
1767 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1768 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1770 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1774 state
.trimmargins
= trimmargins
;
1775 state
.needoutline
= 1;
1776 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1778 state
.trimfuzz
= fuzz
;
1780 state
.pagedimcount
= 0;
1781 free (state
.pagedims
);
1782 state
.pagedims
= NULL
;
1787 printd ("continue %d", state
.pagecount
);
1789 else if (!strncmp ("sliceh", p
, 6)) {
1792 ret
= sscanf (p
+ 6, " %d", &h
);
1794 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1796 if (h
!= state
.sliceheight
) {
1799 state
.sliceheight
= h
;
1800 for (i
= 0; i
< state
.texcount
; ++i
) {
1801 state
.texowners
[i
].w
= -1;
1802 state
.texowners
[i
].h
= -1;
1803 state
.texowners
[i
].slice
= NULL
;
1807 else if (!strncmp ("interrupt", p
, 9)) {
1808 printd ("vmsg interrupted");
1811 errx (1, "unknown command %.*s", len
, p
);
1817 CAMLprim value
ml_realloctexts (value texcount_v
)
1819 CAMLparam1 (texcount_v
);
1822 if (trylock ("ml_realloctexts")) {
1826 realloctexts (Int_val (texcount_v
));
1828 unlock ("ml_realloctexts");
1831 CAMLreturn (Val_bool (ok
));
1834 static void showsel (struct page
*page
, int ox
, int oy
)
1840 fz_text_block
*block
;
1841 struct mark first
, last
;
1843 first
= page
->fmark
;
1846 if (!first
.span
|| !last
.span
) return;
1848 glEnable (GL_BLEND
);
1849 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1850 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1852 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1853 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1854 for (block
= page
->text
->blocks
;
1855 block
< page
->text
->blocks
+ page
->text
->len
;
1857 for (line
= block
->lines
;
1858 line
< block
->lines
+ block
->len
;
1860 for (span
= line
->spans
;
1861 span
< line
->spans
+ line
->len
;
1865 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1870 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1872 j
= MIN (first
.i
, last
.i
);
1873 k
= MAX (first
.i
, last
.i
);
1875 else if (span
== first
.span
) {
1879 else if (span
== last
.span
) {
1885 for (i
= j
; i
<= k
; ++i
) {
1886 fz_bbox b
= fz_round_rect (span
->text
[i
].bbox
);
1887 bbox
= fz_union_bbox (bbox
, b
);
1889 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1896 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1897 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1898 if (span
== last
.span
) {
1906 glDisable (GL_BLEND
);
1911 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1914 fz_link
*link
, *links
;
1916 switch (page
->type
) {
1918 links
= page
->u
.pdfpage
->links
;
1922 links
= page
->u
.xpspage
->links
;
1929 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1930 glEnable (GL_LINE_STIPPLE
);
1931 glLineStipple (0.5, 0xcccc);
1933 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1934 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1935 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1938 for (link
= links
; link
; link
= link
->next
) {
1939 fz_point p1
, p2
, p3
, p4
;
1941 p1
.x
= link
->rect
.x0
;
1942 p1
.y
= link
->rect
.y0
;
1944 p2
.x
= link
->rect
.x1
;
1945 p2
.y
= link
->rect
.y0
;
1947 p3
.x
= link
->rect
.x1
;
1948 p3
.y
= link
->rect
.y1
;
1950 p4
.x
= link
->rect
.x0
;
1951 p4
.y
= link
->rect
.y1
;
1953 p1
= fz_transform_point (ctm
, p1
);
1954 p2
= fz_transform_point (ctm
, p2
);
1955 p3
= fz_transform_point (ctm
, p3
);
1956 p4
= fz_transform_point (ctm
, p4
);
1958 switch (link
->dest
.kind
) {
1959 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1960 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1961 default: glColor3ub (0, 0, 0); break;
1964 glVertex2f (p1
.x
, p1
.y
);
1965 glVertex2f (p2
.x
, p2
.y
);
1966 glVertex2f (p3
.x
, p3
.y
);
1967 glVertex2f (p4
.x
, p4
.y
);
1971 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1972 glDisable (GL_LINE_STIPPLE
);
1975 static int compareslinks (const void *l
, const void *r
)
1977 struct slink
const *ls
= l
;
1978 struct slink
const *rs
= r
;
1979 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1980 return rs
->bbox
.x0
- rs
->bbox
.x0
;
1982 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1985 static void droptext (struct page
*page
)
1988 fz_free_text_page (state
.ctx
, page
->text
);
1991 page
->fmark
.span
= NULL
;
1992 page
->lmark
.span
= NULL
;
1996 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2000 static void dropslinks (struct page
*page
)
2003 free (page
->slinks
);
2004 page
->slinks
= NULL
;
2005 page
->slinkcount
= 0;
2009 static void ensureslinks (struct page
*page
)
2013 size_t slinksize
= sizeof (*page
->slinks
);
2014 fz_link
*link
, *links
;
2016 if (state
.gen
!= page
->sgen
) {
2018 page
->sgen
= state
.gen
;
2020 if (page
->slinks
) return;
2022 switch (page
->type
) {
2024 links
= page
->u
.pdfpage
->links
;
2025 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
2026 state
.pagedims
[page
->pdimno
].ctm
);
2030 links
= page
->u
.xpspage
->links
;
2031 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2038 for (link
= links
; link
; link
= link
->next
) {
2042 page
->slinkcount
= count
;
2043 page
->slinks
= calloc (count
, slinksize
);
2044 if (!page
->slinks
) {
2045 err (1, "realloc slinks %d", count
);
2048 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2049 page
->slinks
[i
].link
= link
;
2050 page
->slinks
[i
].bbox
=
2051 fz_round_rect (fz_transform_rect (ctm
, link
->rect
));
2053 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2057 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2058 static void fmt_linkn (char *s
, unsigned int u
)
2060 unsigned int len
; unsigned int q
;
2061 int zma
= 'z' - 'a' + 1;
2063 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2066 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2067 /* handles u == 0 */
2072 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2073 int noff
, char *targ
, int tlen
, int hfsize
)
2077 struct slink
*slink
;
2078 double x0
, y0
, x1
, y1
, w
;
2080 ensureslinks (page
);
2081 glColor3ub (0xc3, 0xb0, 0x91);
2082 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2083 fmt_linkn (buf
, i
+ noff
);
2084 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2085 slink
= &page
->slinks
[i
];
2087 x0
= slink
->bbox
.x0
+ xoff
- 5;
2088 y1
= slink
->bbox
.y0
+ yoff
- 5;
2089 y0
= y1
+ 10 + hfsize
;
2090 w
= measure_string (state
.face
, hfsize
, buf
);
2092 glRectd (x0
, y0
, x1
, y1
);
2096 glEnable (GL_BLEND
);
2097 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2098 glEnable (GL_TEXTURE_2D
);
2099 glColor3ub (0, 0, 0);
2100 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2101 fmt_linkn (buf
, i
+ noff
);
2102 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2103 slink
= &page
->slinks
[i
];
2105 x0
= slink
->bbox
.x0
+ xoff
;
2106 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2107 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2110 glDisable (GL_TEXTURE_2D
);
2111 glDisable (GL_BLEND
);
2115 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2118 struct slice
*slice1
;
2119 unsigned char *texdata
;
2122 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2123 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2125 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2126 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2127 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2131 int texindex
= state
.texindex
++ % state
.texcount
;
2133 if (state
.texowners
[texindex
].w
== tile
->w
) {
2134 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2138 state
.texowners
[texindex
].h
= slice
->h
;
2142 state
.texowners
[texindex
].h
= slice
->h
;
2145 state
.texowners
[texindex
].w
= tile
->w
;
2146 state
.texowners
[texindex
].slice
= slice
;
2147 slice
->texindex
= texindex
;
2149 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2151 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2155 texdata
= tile
->pixmap
->samples
;
2158 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2170 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2182 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2187 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2189 CAMLparam2 (args_v
, ptr_v
);
2190 int dispx
= Int_val (Field (args_v
, 0));
2191 int dispy
= Int_val (Field (args_v
, 1));
2192 int dispw
= Int_val (Field (args_v
, 2));
2193 int disph
= Int_val (Field (args_v
, 3));
2194 int tilex
= Int_val (Field (args_v
, 4));
2195 int tiley
= Int_val (Field (args_v
, 5));
2196 char *s
= String_val (ptr_v
);
2197 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2199 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2201 int slicey
, firstslice
;
2202 struct slice
*slice
;
2204 firstslice
= tiley
/ tile
->sliceheight
;
2205 slice
= &tile
->slices
[firstslice
];
2206 slicey
= tiley
% tile
->sliceheight
;
2211 dh
= slice
->h
- slicey
;
2212 dh
= MIN (disph
, dh
);
2213 uploadslice (tile
, slice
);
2217 glTexCoord2i (tilex
, slicey
);
2218 glVertex2i (dispx
, dispy
);
2220 glTexCoord2i (tilex
+dispw
, slicey
);
2221 glVertex2i (dispx
+dispw
, dispy
);
2223 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2224 glVertex2i (dispx
+dispw
, dispy
+dh
);
2226 glTexCoord2i (tilex
, slicey
+dh
);
2227 glVertex2i (dispx
, dispy
+dh
);
2234 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2238 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2239 CAMLreturn (Val_unit
);
2242 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2243 value xoff_v
, value yoff_v
,
2246 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2247 int xoff
= Int_val (xoff_v
);
2248 int yoff
= Int_val (yoff_v
);
2249 int noff
= Int_val (Field (li_v
, 0));
2250 char *targ
= String_val (Field (li_v
, 1));
2251 int tlen
= caml_string_length (Field (li_v
, 1));
2252 int hfsize
= Int_val (Field (li_v
, 2));
2253 char *s
= String_val (ptr_v
);
2254 int hlmask
= Int_val (hlinks_v
);
2255 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2258 /* deal with loadpage failed pages */
2262 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2263 if (trylock ("ml_postprocess")) {
2268 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2269 noff
= page
->slinkcount
;
2271 showsel (page
, xoff
, yoff
);
2272 unlock ("ml_postprocess");
2275 CAMLreturn (Val_int (noff
));
2278 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2282 fz_link
*link
, *links
;
2284 switch (page
->type
) {
2286 ctm
= trimctm (page
->u
.pdfpage
, page
->pdimno
);
2287 links
= page
->u
.pdfpage
->links
;
2292 links
= page
->u
.xpspage
->links
;
2301 ctm
= fz_concat (ctm
, state
.pagedims
[page
->pdimno
].ctm
);
2302 ctm
= fz_invert_matrix (ctm
);
2303 p
= fz_transform_point (ctm
, p
);
2305 for (link
= links
; link
; link
= link
->next
) {
2306 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2307 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2315 static void ensuretext (struct page
*page
)
2317 if (state
.gen
!= page
->tgen
) {
2319 page
->tgen
= state
.gen
;
2324 page
->text
= fz_new_text_page (state
.ctx
, fz_infinite_rect
);
2325 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2326 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2327 fz_run_display_list (page
->dlist
,
2330 fz_infinite_bbox
, NULL
);
2331 qsort (page
->text
->blocks
, page
->text
->len
,
2332 sizeof (*page
->text
->blocks
), compareblocks
);
2333 fz_free_device (tdev
);
2337 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2339 CAMLparam2 (start_page_v
, dir_v
);
2341 int i
, dir
= Int_val (dir_v
);
2342 int start_page
= Int_val (start_page_v
);
2343 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2345 ret_v
= Val_int (0);
2346 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2350 lock ("ml_findpage_with_links");
2351 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2354 switch (state
.type
) {
2357 pdf_page
*page
= NULL
;
2359 fz_try (state
.ctx
) {
2360 page
= pdf_load_page (state
.u
.pdf
, i
);
2361 found
= !!page
->links
;
2363 fz_catch (state
.ctx
) {
2373 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2374 found
= !!page
->links
;
2380 ARSERT ("invalid document type");
2384 ret_v
= caml_alloc_small (1, 1);
2385 Field (ret_v
, 0) = Val_int (i
);
2390 unlock ("ml_findpage_with_links");
2396 enum { dir_first
, dir_last
};
2397 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2399 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2401 CAMLparam2 (ptr_v
, dir_v
);
2402 CAMLlocal2 (ret_v
, pos_v
);
2404 int dirtag
, i
, slinkindex
;
2405 struct slink
*found
= NULL
,*slink
;
2406 char *s
= String_val (ptr_v
);
2408 page
= parse_pointer ("ml_findlink", s
);
2409 ret_v
= Val_int (0);
2410 if (trylock ("ml_findlink")) {
2414 ensureslinks (page
);
2416 if (Is_block (dir_v
)) {
2417 dirtag
= Tag_val (dir_v
);
2419 case dir_first_visible
:
2421 int x0
, y0
, dir
, first_index
, last_index
;
2423 pos_v
= Field (dir_v
, 0);
2424 x0
= Int_val (Field (pos_v
, 0));
2425 y0
= Int_val (Field (pos_v
, 1));
2426 dir
= Int_val (Field (pos_v
, 2));
2431 last_index
= page
->slinkcount
;
2434 first_index
= page
->slinkcount
- 1;
2438 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2439 slink
= &page
->slinks
[i
];
2440 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2449 slinkindex
= Int_val (Field (dir_v
, 0));
2450 found
= &page
->slinks
[slinkindex
];
2451 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2452 slink
= &page
->slinks
[i
];
2453 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2461 slinkindex
= Int_val (Field (dir_v
, 0));
2462 found
= &page
->slinks
[slinkindex
];
2463 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2464 slink
= &page
->slinks
[i
];
2465 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2473 slinkindex
= Int_val (Field (dir_v
, 0));
2474 found
= &page
->slinks
[slinkindex
];
2475 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2476 slink
= &page
->slinks
[i
];
2477 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2485 slinkindex
= Int_val (Field (dir_v
, 0));
2486 found
= &page
->slinks
[slinkindex
];
2487 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2488 slink
= &page
->slinks
[i
];
2489 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2498 dirtag
= Int_val (dir_v
);
2501 found
= page
->slinks
;
2506 found
= page
->slinks
+ (page
->slinkcount
- 1);
2512 ret_v
= caml_alloc_small (2, 1);
2513 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2516 unlock ("ml_findlink");
2521 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2527 switch (link->dest.kind) { \
2528 case FZ_LINK_GOTO: \
2532 pageno = link->dest.ld.gotor.page; \
2536 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2537 p.y = link->dest.ld.gotor.lt.y; \
2538 p = fz_transform_point (pdim->lctm, p); \
2540 tup_v = caml_alloc_tuple (2); \
2541 ret_v = caml_alloc_small (1, ugoto); \
2542 Field (tup_v, 0) = Val_int (pageno); \
2543 Field (tup_v, 1) = Val_int (p.y); \
2544 Field (ret_v, 0) = tup_v; \
2549 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2550 ret_v = caml_alloc_small (1, uuri); \
2551 Field (ret_v, 0) = str_v; \
2554 case FZ_LINK_LAUNCH: \
2555 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2556 ret_v = caml_alloc_small (1, ulaunch); \
2557 Field (ret_v, 0) = str_v; \
2560 case FZ_LINK_NAMED: \
2561 str_v = caml_copy_string (link->dest.ld.named.named); \
2562 ret_v = caml_alloc_small (1, unamed); \
2563 Field (ret_v, 0) = str_v; \
2566 case FZ_LINK_GOTOR: \
2567 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2568 pageno = link->dest.ld.gotor.page; \
2569 tup_v = caml_alloc_tuple (2); \
2570 ret_v = caml_alloc_small (1, uremote); \
2571 Field (tup_v, 0) = str_v; \
2572 Field (tup_v, 1) = Val_int (pageno); \
2573 Field (ret_v, 0) = tup_v; \
2580 snprintf (buf, sizeof (buf), \
2581 "unhandled link kind %d", link->dest.kind); \
2582 str_v = caml_copy_string (buf); \
2583 ret_v = caml_alloc_small (1, uunexpected); \
2584 Field (ret_v, 0) = str_v; \
2590 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2592 CAMLparam2 (ptr_v
, n_v
);
2593 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2596 struct pagedim
*pdim
;
2597 char *s
= String_val (ptr_v
);
2599 ret_v
= Val_int (0);
2600 if (trylock ("ml_getlink")) {
2604 page
= parse_pointer ("ml_getlink", s
);
2605 ensureslinks (page
);
2606 pdim
= &state
.pagedims
[page
->pdimno
];
2607 link
= page
->slinks
[Int_val (n_v
)].link
;
2610 unlock ("ml_getlink");
2615 CAMLprim value
ml_getlinkcount (value ptr_v
)
2619 char *s
= String_val (ptr_v
);
2621 page
= parse_pointer ("ml_getlinkcount", s
);
2622 CAMLreturn (Val_int (page
->slinkcount
));
2625 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2627 CAMLparam2 (ptr_v
, n_v
);
2630 struct slink
*slink
;
2631 char *s
= String_val (ptr_v
);
2633 page
= parse_pointer ("ml_getlinkrect", s
);
2634 ret_v
= caml_alloc_tuple (4);
2635 if (trylock ("ml_getlinkrect")) {
2636 Field (ret_v
, 0) = Val_int (0);
2637 Field (ret_v
, 1) = Val_int (0);
2638 Field (ret_v
, 2) = Val_int (0);
2639 Field (ret_v
, 3) = Val_int (0);
2642 ensureslinks (page
);
2644 slink
= &page
->slinks
[Int_val (n_v
)];
2645 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2646 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2647 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2648 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2649 unlock ("ml_getlinkrect");
2655 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2657 CAMLparam3 (ptr_v
, x_v
, y_v
);
2658 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2661 char *s
= String_val (ptr_v
);
2662 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2663 struct pagedim
*pdim
;
2665 ret_v
= Val_int (0);
2666 if (trylock ("ml_whatsunder")) {
2670 page
= parse_pointer ("ml_whatsunder", s
);
2671 pdim
= &state
.pagedims
[page
->pdimno
];
2672 x
+= pdim
->bounds
.x0
;
2673 y
+= pdim
->bounds
.y0
;
2674 link
= getlink (page
, x
, y
);
2680 fz_text_block
*block
;
2683 for (block
= page
->text
->blocks
;
2684 block
< page
->text
->blocks
+ page
->text
->len
;
2689 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2692 for (line
= block
->lines
;
2693 line
< block
->lines
+ block
->len
;
2698 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2701 for (span
= line
->spans
;
2702 span
< line
->spans
+ line
->len
;
2707 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2710 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
2713 if (x
>= b
->x0
&& x
<= b
->x1
2714 && y
>= b
->y0
&& y
<= b
->y1
) {
2716 span
->style
->font
&& span
->style
->font
->name
2717 ? span
->style
->font
->name
2718 : "Span has no font name"
2720 FT_FaceRec
*face
= span
->style
->font
->ft_face
;
2721 if (face
&& face
->family_name
) {
2723 char *n1
= face
->family_name
;
2724 size_t l1
= strlen (n1
);
2725 size_t l2
= strlen (n2
);
2727 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2728 s
= malloc (l1
+ l2
+ 2);
2732 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2733 str_v
= caml_copy_string (s
);
2739 str_v
= caml_copy_string (n2
);
2741 ret_v
= caml_alloc_small (1, utext
);
2742 Field (ret_v
, 0) = str_v
;
2751 unlock ("ml_whatsunder");
2757 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2759 CAMLparam2 (ptr_v
, rect_v
);
2762 struct pagedim
*pdim
;
2763 int i
, x0
, x1
, y0
, y1
;
2764 char *s
= String_val (ptr_v
);
2766 fz_text_block
*block
;
2767 fz_text_span
*span
, *fspan
, *lspan
;
2768 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2770 if (trylock ("ml_seltext")) {
2774 page
= parse_pointer ("ml_seltext", s
);
2777 pdim
= &state
.pagedims
[page
->pdimno
];
2778 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2779 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2780 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2781 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2784 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2785 glColor3ub (128, 128, 128);
2786 glRecti (x0
, y0
, x1
, y1
);
2787 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2790 fspan
= lspan
= NULL
;
2792 for (block
= page
->text
->blocks
;
2793 block
< page
->text
->blocks
+ page
->text
->len
;
2795 for (line
= block
->lines
;
2796 line
< block
->lines
+ block
->len
;
2798 for (span
= line
->spans
;
2799 span
< line
->spans
+ line
->len
;
2801 for (i
= 0; i
< span
->len
; ++i
) {
2802 b
= &span
->text
[i
].bbox
;
2805 if (x0
>= b
->x0
&& x0
<= b
->x1
2806 && y0
>= b
->y0
&& y0
<= b
->y1
) {
2812 if (x1
>= b
->x0
&& x1
<= b
->x1
2813 && y1
>= b
->y0
&& y1
<= b
->y1
) {
2819 if (0 && selected
) {
2820 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2821 glColor3ub (128, 128, 128);
2822 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2823 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2829 if (y1
< y0
|| x1
< x0
) {
2832 if (fspan
== lspan
) {
2837 if (fline
!= lline
) {
2856 page
->fmark
.span
= fspan
;
2859 page
->lmark
.span
= lspan
;
2861 unlock ("ml_seltext");
2864 CAMLreturn (Val_unit
);
2867 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2872 for (i
= a
; i
<= b
; ++i
) {
2873 len
= fz_runetochar (buf
, span
->text
[i
].c
);
2874 ret
= fwrite (buf
, len
, 1, f
);
2877 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2878 len
, ret
, strerror (errno
));
2886 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
2888 caml_failwith ("ml_popen not implemented under Cygwin");
2891 CAMLprim value
ml_popen (value command_v
, value fds_v
)
2893 CAMLparam2 (command_v
, fds_v
);
2894 CAMLlocal2 (l_v
, tup_v
);
2897 value earg_v
= Nothing
;
2898 posix_spawnattr_t attr
;
2899 posix_spawn_file_actions_t fa
;
2900 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
2902 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
2903 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
2906 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
2907 msg
= "posix_spawnattr_init";
2911 #ifdef POSIX_SPAWN_USEVFORK
2912 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
2913 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
2918 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
2921 tup_v
= Field (l_v
, 0);
2922 fd1
= Int_val (Field (tup_v
, 0));
2923 fd2
= Int_val (Field (tup_v
, 1));
2925 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
2926 msg
= "posix_spawn_file_actions_addclose";
2932 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
2933 msg
= "posix_spawn_file_actions_adddup2";
2940 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
2941 msg
= "posix_spawn";
2946 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
2947 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
2951 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
2952 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
2957 unix_error (ret
, msg
, earg_v
);
2959 CAMLreturn (Val_unit
);
2963 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
2971 fz_text_block
*block
;
2972 int fd
= Int_val (fd_v
);
2973 char *s
= String_val (ptr_v
);
2975 if (trylock ("ml_copysel")) {
2979 page
= parse_pointer ("ml_sopysel", s
);
2981 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2982 fprintf (stderr
, "nothing to copy\n");
2986 f
= fdopen (fd
, "w");
2988 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
2993 for (block
= page
->text
->blocks
;
2994 block
< page
->text
->blocks
+ page
->text
->len
;
2996 for (line
= block
->lines
;
2997 line
< block
->lines
+ block
->len
;
2999 for (span
= line
->spans
;
3000 span
< line
->spans
+ line
->len
;
3004 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3005 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3006 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3009 if (pipespan (f
, span
, a
, b
)) {
3012 if (span
== line
->spans
+ line
->len
- 1) {
3013 if (putc ('\n', f
) == EOF
) {
3015 "failed break line on sel pipe: %s\n",
3020 if (span
== page
->lmark
.span
) {
3028 page
->lmark
.span
= NULL
;
3029 page
->fmark
.span
= NULL
;
3033 int ret
= fclose (f
);
3036 if (errno
!= ECHILD
) {
3037 fprintf (stderr
, "failed to close sel pipe: %s\n",
3043 unlock ("ml_copysel");
3048 fprintf (stderr
, "failed to close sel pipe: %s\n",
3052 CAMLreturn (Val_unit
);
3055 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3057 CAMLparam1 (pagedimno_v
);
3059 int pagedimno
= Int_val (pagedimno_v
);
3062 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3063 if (trylock ("ml_getpdimrect")) {
3064 box
= fz_empty_rect
;
3067 box
= state
.pagedims
[pagedimno
].mediabox
;
3068 unlock ("ml_getpdimrect");
3071 Store_double_field (ret_v
, 0, box
.x0
);
3072 Store_double_field (ret_v
, 1, box
.x1
);
3073 Store_double_field (ret_v
, 2, box
.y0
);
3074 Store_double_field (ret_v
, 3, box
.y1
);
3079 static double getmaxw (void)
3085 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3088 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
3089 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
3092 maxw
= MAX (w
, maxw
);
3097 CAMLprim value
ml_getmaxw (value unit_v
)
3099 CAMLparam1 (unit_v
);
3103 if (trylock ("ml_getmaxw")) {
3107 unlock ("ml_getmaxw");
3109 ret_v
= caml_copy_double (maxw
);
3113 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3114 value dw_v
, value cols_v
)
3116 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3120 double maxw
= 0.0, maxh
= 0.0;
3122 double winw
= Int_val (winw_v
);
3123 double winh
= Int_val (winh_v
);
3124 double dw
= Int_val (dw_v
);
3125 double cols
= Int_val (cols_v
);
3126 double pw
= 1.0, ph
= 1.0, aspect
;
3128 if (trylock ("ml_zoom_for_height")) {
3132 if (state
.proportional
) {
3133 maxw
= getmaxw () / cols
;
3136 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3137 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
3140 rect
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
3142 x0
= MIN (rect
.x0
, rect
.x1
);
3143 x1
= MAX (rect
.x0
, rect
.x1
);
3144 y0
= MIN (rect
.y0
, rect
.y1
);
3145 y1
= MAX (rect
.y0
, rect
.y1
);
3147 w
= (x1
- x0
) / cols
;
3150 if (state
.proportional
) {
3152 scaledh
= h
* scale
;
3159 if (scaledh
> maxh
) {
3167 zoom
= (winh
* aspect
+ dw
) / winw
;
3169 unlock ("ml_zoom_for_height");
3171 ret_v
= caml_copy_double (zoom
);
3175 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3177 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3179 int pt
= Int_val(pt_v
);
3180 int x
= Int_val (x_v
);
3181 int y
= Int_val (y_v
);
3184 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3185 ret_v
= caml_copy_double (w
);
3189 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3191 CAMLparam2 (pt_v
, string_v
);
3193 int pt
= Int_val (pt_v
);
3196 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3197 ret_v
= caml_copy_double (w
);
3201 CAMLprim value
ml_getpagebox (value opaque_v
)
3203 CAMLparam1 (opaque_v
);
3207 char *s
= String_val (opaque_v
);
3208 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3210 ret_v
= caml_alloc_tuple (4);
3211 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
3212 dev
->hints
|= FZ_IGNORE_SHADE
;
3214 switch (page
->type
) {
3216 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
3220 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
3224 bbox
= fz_infinite_bbox
;
3228 fz_free_device (dev
);
3229 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3230 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3231 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3232 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3237 CAMLprim value
ml_setaalevel (value level_v
)
3239 CAMLparam1 (level_v
);
3241 state
.aalevel
= Int_val (level_v
);
3242 CAMLreturn (Val_unit
);
3246 #include <X11/Xlib.h>
3252 GLXDrawable drawable
;
3255 #include "keysym2ucs.c"
3257 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3259 CAMLparam1 (keysym_v
);
3261 KeySym keysym
= Int_val (keysym_v
);
3266 rune
= keysym2ucs (keysym
);
3267 len
= fz_runetochar (buf
, rune
);
3269 str_v
= caml_copy_string (buf
);
3273 CAMLprim value
ml_glx (value win_v
)
3276 XVisualInfo
*visual
;
3277 int screen
, wid
= Int_val (win_v
);
3278 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3280 glx
.dpy
= XOpenDisplay (NULL
);
3282 caml_failwith ("XOpenDisplay");
3285 screen
= DefaultScreen (glx
.dpy
);
3286 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3288 XCloseDisplay (glx
.dpy
);
3290 caml_failwith ("glXChooseVisual");
3293 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3296 XCloseDisplay (glx
.dpy
);
3298 caml_failwith ("glXCreateContext");
3301 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3302 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3303 XCloseDisplay (glx
.dpy
);
3306 caml_failwith ("glXMakeCurrent");
3309 CAMLreturn (Val_unit
);
3312 CAMLprim value
ml_swapb (value unit_v
)
3314 CAMLparam1 (unit_v
);
3315 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3316 CAMLreturn (Val_unit
);
3319 CAMLprim value
ml_glxsync (value unit_v
)
3321 CAMLparam1 (unit_v
);
3322 if (glx
.dpy
&& glx
.ctx
) {
3326 CAMLreturn (Val_unit
);
3329 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3330 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3332 CAMLprim value
ml_platform (value unit_v
)
3334 CAMLparam1 (unit_v
);
3335 int platid
= piunknown
;
3337 #if defined __linux__
3339 #elif defined __CYGWIN__
3341 #elif defined __DragonFly__
3342 platid
= pidragonflybsd
;
3343 #elif defined __FreeBSD__
3345 #elif defined __OpenBSD__
3347 #elif defined __NetBSD__
3349 #elif defined __sun__
3351 #elif defined __APPLE__
3354 CAMLreturn (Val_int (platid
));
3357 CAMLprim value
ml_cloexec (value fd_v
)
3360 int fd
= Int_val (fd_v
);
3362 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3363 uerror ("fcntl", Nothing
);
3365 CAMLreturn (Val_unit
);
3368 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3370 CAMLparam2 (w_v
, h_v
);
3373 int w
= Int_val (w_v
);
3374 int h
= Int_val (h_v
);
3375 int cs
= Int_val (cs_v
);
3377 if (state
.pbo_usable
) {
3378 pbo
= calloc (sizeof (*pbo
), 1);
3380 err (1, "calloc pbo");
3392 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3395 state
.glGenBuffersARB (1, &pbo
->id
);
3396 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3397 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3398 NULL
, GL_STREAM_DRAW
);
3399 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3401 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3403 fprintf (stderr
, "glMapBufferARB failed: %#x", glGetError ());
3404 state
.glDeleteBuffersARB (1, &pbo
->id
);
3406 ret_v
= caml_copy_string ("0");
3412 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3414 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3418 err (1, "malloc %d bytes failed", res
+1);
3420 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3422 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3424 ret_v
= caml_copy_string (s
);
3429 ret_v
= caml_copy_string ("0");
3434 CAMLprim value
ml_freepbo (value s_v
)
3437 char *s
= String_val (s_v
);
3438 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3441 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3443 tile
->pbo
->ptr
= NULL
;
3444 tile
->pbo
->size
= -1;
3446 CAMLreturn (Val_unit
);
3449 CAMLprim value
ml_unmappbo (value s_v
)
3452 char *s
= String_val (s_v
);
3453 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3456 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3457 state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
);
3458 tile
->pbo
->ptr
= NULL
;
3459 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3461 CAMLreturn (Val_unit
);
3464 static void setuppbo (void)
3466 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3467 GGPA (glBindBufferARB
);
3468 if (state
.glBindBufferARB
) {
3469 GGPA (glUnmapBufferARB
);
3470 if (state
.glUnmapBufferARB
) {
3471 GGPA (glMapBufferARB
);
3472 if (state
.glMapBufferARB
) {
3473 GGPA (glBufferDataARB
);
3474 if (state
.glBufferDataARB
) {
3475 GGPA (glGenBuffersARB
);
3476 if (state
.glGenBuffersARB
) {
3477 GGPA (glDeleteBuffersARB
);
3478 if (state
.glDeleteBuffersARB
) {
3479 state
.pbo_usable
= 1;
3489 CAMLprim value
ml_pbo_usable (value unit_v
)
3491 CAMLparam1 (unit_v
);
3492 CAMLreturn (Val_bool (state
.pbo_usable
));
3495 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3497 CAMLparam2 (pipe_v
, params_v
);
3498 CAMLlocal2 (trim_v
, fuzz_v
);
3505 struct sigaction sa
;
3507 state
.cr
= Int_val (Field (pipe_v
, 0));
3508 state
.cw
= Int_val (Field (pipe_v
, 1));
3509 state
.rotate
= Int_val (Field (params_v
, 0));
3510 state
.proportional
= Bool_val (Field (params_v
, 1));
3511 trim_v
= Field (params_v
, 2);
3512 texcount
= Int_val (Field (params_v
, 3));
3513 state
.sliceheight
= Int_val (Field (params_v
, 4));
3514 mustoresize
= Int_val (Field (params_v
, 5));
3515 colorspace
= Int_val (Field (params_v
, 6));
3516 fontpath
= String_val (Field (params_v
, 7));
3517 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3518 if (!state
.trimcachepath
) {
3519 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3522 haspboext
= Bool_val (Field (params_v
, 9));
3524 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3526 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3527 fuzz_v
= Field (trim_v
, 1);
3528 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3529 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3530 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3531 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3533 set_tex_params (colorspace
);
3536 state
.face
= load_font (fontpath
);
3540 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3542 state
.face
= load_builtin_font (base
, len
);
3544 if (!state
.face
) _exit (1);
3546 realloctexts (texcount
);
3553 sa
.sa_handler
= SIG_IGN
;
3554 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3556 sa
.sa_handler
= SIG_DFL
;
3557 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3559 if (sigemptyset (&sa
.sa_mask
)) {
3560 err (1, "sigemptyset");
3562 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3563 err (1, "sigaction");
3566 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3568 errx (1, "pthread_create: %s", strerror (ret
));
3571 CAMLreturn (Val_unit
);