1 /* lots of code c&p-ed directly from mupdf */
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
16 #include <cygwin/socket.h> /* FIONREAD */
28 #include <caml/fail.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/unixsupport.h>
35 #include <mupdf-internal.h>
37 #include <muxps-internal.h>
40 #include FT_FREETYPE_H
45 extern char **environ
;
48 #define MIN(a,b) ((a) < (b) ? (a) : (b))
49 #define MAX(a,b) ((a) > (b) ? (a) : (b))
52 #define NORETURN_ATTR __attribute__ ((noreturn))
53 #define UNUSED_ATTR __attribute__ ((unused))
54 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
55 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
59 #define OPTIMIZE_ATTR(n)
60 #define GCC_FMT_ATTR(a, b)
66 #define FMT_ptr_cast(p) (p)
67 #define FMT_ptr_cast2(p) (p)
69 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
70 err (int exitcode
, const char *fmt
, ...)
77 vfprintf (stderr
, fmt
, ap
);
79 fprintf (stderr
, ": %s\n", strerror (savederrno
));
84 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
85 errx (int exitcode
, const char *fmt
, ...)
90 vfprintf (stderr
, fmt
, ap
);
97 #ifndef GL_TEXTURE_RECTANGLE_ARB
98 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
102 #define GL_BGRA 0x80E1
105 #ifndef GL_UNSIGNED_INT_8_8_8_8
106 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
109 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
110 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
114 #define lprintf printf
119 #define ARSERT(cond) for (;;) { \
121 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
136 struct slice slices
[1];
147 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
155 enum { DPDF
, DXPS
, DCBZ
};
164 fz_text_sheet
*sheet
;
171 fz_display_list
*dlist
;
173 struct slink
*slinks
;
178 void (*freepage
) (void *);
184 struct pagedim
*pagedims
;
203 fz_colorspace
*colorspace
;
225 void (*closedoc
) (void);
226 void (*freepage
) (void *);
231 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
233 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
236 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_bbox r
)
238 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
241 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
243 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
244 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
247 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
249 static void lock (const char *cap
)
251 int ret
= pthread_mutex_lock (&mutex
);
253 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
257 static void unlock (const char *cap
)
259 int ret
= pthread_mutex_unlock (&mutex
);
261 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
265 static int trylock (const char *cap
)
267 int ret
= pthread_mutex_trylock (&mutex
);
268 if (ret
&& ret
!= EBUSY
) {
269 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
274 static void *parse_pointer (const char *cap
, const char *s
)
279 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
281 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
286 static double now (void)
290 if (gettimeofday (&tv
, NULL
)) {
291 err (1, "gettimeofday");
293 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
296 static int hasdata (void)
299 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
300 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
304 CAMLprim value
ml_hasdata (value fd_v
)
309 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
310 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
311 CAMLreturn (Val_bool (avail
> 0));
314 static void readdata (void *p
, int size
)
319 n
= read (state
.cr
, p
, size
);
321 if (errno
== EINTR
) goto again
;
322 err (1, "read (req %d, ret %zd)", size
, n
);
325 if (!n
) errx (1, "EOF while reading");
326 errx (1, "read (req %d, ret %zd)", size
, n
);
330 static void writedata (char *p
, int size
)
335 buf
[0] = (size
>> 24) & 0xff;
336 buf
[1] = (size
>> 16) & 0xff;
337 buf
[2] = (size
>> 8) & 0xff;
338 buf
[3] = (size
>> 0) & 0xff;
340 n
= write (state
.cw
, buf
, 4);
342 if (!n
) errx (1, "EOF while writing length");
343 err (1, "write %zd", n
);
346 n
= write (state
.cw
, p
, size
);
348 if (!n
) errx (1, "EOF while writing data");
349 err (1, "write (req %d, ret %zd)", size
, n
);
353 static int readlen (void)
358 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
361 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
369 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
372 len
= vsnprintf (buf
, size
, fmt
, ap
);
375 if (len
> -1 && len
< size
) {
376 writedata (buf
, len
);
386 buf
= realloc (buf
, size
);
391 static void closepdf (void)
394 pdf_close_document (state
.u
.pdf
);
399 static void closexps (void)
402 xps_close_document (state
.u
.xps
);
407 static void closecbz (void)
410 cbz_close_document (state
.u
.cbz
);
415 static void freepdfpage (void *ptr
)
417 pdf_free_page (state
.u
.pdf
, ptr
);
420 static void freeemptypdfpage (void *ptr
)
425 static void freexpspage (void *ptr
)
427 xps_free_page (state
.u
.xps
, ptr
);
430 static void freecbzpage (void *ptr
)
432 cbz_free_page (state
.u
.cbz
, ptr
);
435 static void openxref (char *filename
, char *password
)
439 for (i
= 0; i
< state
.texcount
; ++i
) {
440 state
.texowners
[i
].w
= -1;
441 state
.texowners
[i
].slice
= NULL
;
444 if (state
.closedoc
) state
.closedoc ();
446 len
= strlen (filename
);
452 ext
[0] = tolower ((int) filename
[len
-3]);
453 ext
[1] = tolower ((int) filename
[len
-2]);
454 ext
[2] = tolower ((int) filename
[len
-1]);
456 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
459 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
464 if (state
.pagedims
) {
465 free (state
.pagedims
);
466 state
.pagedims
= NULL
;
468 state
.pagedimcount
= 0;
470 fz_set_aa_level (state
.ctx
, state
.aalevel
);
471 switch (state
.type
) {
473 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
474 if (pdf_needs_password (state
.u
.pdf
)) {
475 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
477 errx (1, "invalid password");
480 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
481 state
.closedoc
= closepdf
;
482 state
.freepage
= freepdfpage
;
486 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
487 state
.pagecount
= xps_count_pages (state
.u
.xps
);
488 state
.closedoc
= closexps
;
489 state
.freepage
= freexpspage
;
493 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
494 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
495 state
.closedoc
= closecbz
;
496 state
.freepage
= freecbzpage
;
501 static void pdfinfo (void)
503 if (state
.type
== DPDF
) {
506 printd ("info PDF version\t%d.%d",
507 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
509 infoobj
= pdf_dict_gets (state
.u
.pdf
->trailer
, "Info");
513 char *items
[] = { "Title", "Author", "Creator",
514 "Producer", "CreationDate" };
516 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
517 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
518 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
521 printd ("title %s", s
);
523 printd ("info %s\t%s", items
[i
], s
);
525 fz_free (state
.ctx
, s
);
532 static void unlinktile (struct tile
*tile
)
536 for (i
= 0; i
< tile
->slicecount
; ++i
) {
537 struct slice
*s
= &tile
->slices
[i
];
539 if (s
->texindex
!= -1) {
540 if (state
.texowners
[s
->texindex
].slice
== s
) {
541 state
.texowners
[s
->texindex
].slice
= NULL
;
547 static void freepage (struct page
*page
)
550 fz_free_text_page (state
.ctx
, page
->text
);
553 fz_free_text_sheet (state
.ctx
, page
->sheet
);
558 page
->freepage (page
->u
.ptr
);
559 fz_free_display_list (state
.ctx
, page
->dlist
);
563 static void freetile (struct tile
*tile
)
567 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
570 fz_drop_pixmap (state
.ctx
, state
.pig
);
572 state
.pig
= tile
->pixmap
;
580 static int cacheline32bytes
;
582 static void __attribute__ ((constructor
)) clcheck (void)
584 char **envp
= environ
;
589 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
591 cacheline32bytes
= auxv
[1] == 32;
597 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
599 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
600 if (cacheline32bytes
&& size
> 32) {
601 intptr_t a1
, a2
, diff
;
603 vector
unsigned char v
= vec_splat_u8 (-1);
604 vector
unsigned char *p
;
606 a1
= a2
= (intptr_t) pixmap
->samples
;
607 a2
= (a1
+ 31) & ~31;
612 while (a1
!= a2
) *(char *) a1
++ = 0xff;
613 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
614 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
616 vec_st (v
, i
+ 16, p
);
618 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
620 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
623 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
626 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
629 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
631 if (!pdim
->tctmready
) {
632 if (state
.trimmargins
) {
635 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
636 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
637 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
638 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
649 static fz_matrix
pagectm (struct page
*page
)
651 if (page
->type
== DPDF
) {
652 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
653 state
.pagedims
[page
->pdimno
].ctm
);
657 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
659 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
660 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
661 -pdim
->mediabox
.y0
), ctm
);
666 static void *loadpage (int pageno
, int pindex
)
669 struct page
*page
= NULL
;
671 page
= calloc (sizeof (struct page
), 1);
673 err (1, "calloc page %d", pageno
);
676 page
->dlist
= fz_new_display_list (state
.ctx
);
677 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
678 switch (state
.type
) {
681 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
682 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
683 page
->freepage
= freepdfpage
;
685 fz_catch (state
.ctx
) {
687 page
->freepage
= freeemptypdfpage
;
692 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
693 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
694 page
->freepage
= freexpspage
;
698 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
699 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
700 page
->freepage
= freecbzpage
;
703 fz_free_device (dev
);
705 page
->pdimno
= pindex
;
706 page
->pageno
= pageno
;
707 page
->sgen
= state
.gen
;
708 page
->tgen
= state
.gen
;
709 page
->type
= state
.type
;
714 static struct tile
*alloctile (int h
)
721 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
722 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
723 tile
= calloc (tilesize
, 1);
725 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
727 for (i
= 0; i
< slicecount
; ++i
) {
728 int sh
= MIN (h
, state
.sliceheight
);
729 tile
->slices
[i
].h
= sh
;
730 tile
->slices
[i
].texindex
= -1;
733 tile
->slicecount
= slicecount
;
734 tile
->sliceheight
= state
.sliceheight
;
744 static void obs_fill_image (fz_device
*dev
, fz_image
*image
, fz_matrix ctm
,
747 struct obs
*obs
= dev
->user
;
749 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
750 fz_bbox b
= fz_round_rect (fz_transform_rect (ctm
, fz_unit_rect
));
751 b
= fz_intersect_bbox (b
, obs
->b
);
752 obs
->cured
= b
.x0
== obs
->b
.x0
755 && b
.y1
== obs
->b
.y1
;
759 static int obscured (struct page
*page
, fz_bbox bbox
)
764 memset (&dev
, 0, sizeof (dev
));
765 memset (&obs
, 0, sizeof (obs
));
770 dev
.fill_image
= obs_fill_image
;
772 fz_run_display_list (page
->dlist
, &dev
, pagectm (page
), bbox
, NULL
);
775 #define OBSCURED obscured
777 #define OBSCURED(a, b) 0
780 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
785 struct pagedim
*pdim
;
787 tile
= alloctile (h
);
788 pdim
= &state
.pagedims
[page
->pdimno
];
793 bbox
.x1
= bbox
.x0
+ w
;
794 bbox
.y1
= bbox
.y0
+ h
;
797 if (state
.pig
->w
== w
799 && state
.pig
->colorspace
== state
.colorspace
) {
800 tile
->pixmap
= state
.pig
;
801 tile
->pixmap
->x
= bbox
.x0
;
802 tile
->pixmap
->y
= bbox
.y0
;
805 fz_drop_pixmap (state
.ctx
, state
.pig
);
811 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
);
816 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
817 clearpixmap (tile
->pixmap
);
819 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
820 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
821 fz_free_device (dev
);
826 static void initpdims (void)
829 int pageno
, trim
, show
;
835 if (state
.trimmargins
&& state
.trimcachepath
) {
836 trimf
= fopen (state
.trimcachepath
, "rb");
838 trimf
= fopen (state
.trimcachepath
, "wb");
842 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
847 switch (state
.type
) {
849 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
851 if (state
.trimmargins
) {
856 page
= pdf_load_page (state
.u
.pdf
, pageno
);
857 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
858 trim
= state
.trimanew
|| !obj
;
865 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
866 dev
->hints
|= FZ_IGNORE_SHADE
;
867 ctm
= fz_invert_matrix (page
->ctm
);
868 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
869 fz_free_device (dev
);
871 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
872 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
873 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
874 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
875 rect
= fz_transform_rect (ctm
, rect
);
876 rect
= fz_intersect_rect (rect
, page
->mediabox
);
878 if (fz_is_empty_rect (rect
)) {
879 mediabox
= page
->mediabox
;
885 obj
= pdf_new_array (state
.ctx
, 4);
886 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x0
));
887 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y0
));
888 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x1
));
889 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y1
));
890 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
893 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
894 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
895 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
896 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
899 rotate
= page
->rotate
;
900 pdf_free_page (state
.u
.pdf
, page
);
902 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
904 printd ("progress %f Trimming %d",
905 (double) (pageno
+ 1) / state
.pagecount
,
909 fz_catch (state
.ctx
) {
915 mediabox
= pdf_to_rect (state
.ctx
,
916 pdf_dict_gets (pageobj
, "MediaBox"));
917 if (fz_is_empty_rect (mediabox
)) {
918 fprintf (stderr
, "cannot find page size for page %d\n",
926 cropbox
= pdf_to_rect (state
.ctx
,
927 pdf_dict_gets (pageobj
, "CropBox"));
928 if (!fz_is_empty_rect (cropbox
)) {
929 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
931 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
940 page
= xps_load_page (state
.u
.xps
, pageno
);
941 mediabox
= xps_bound_page (state
.u
.xps
, page
);
943 if (state
.trimmargins
) {
948 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
949 dev
->hints
|= FZ_IGNORE_SHADE
;
950 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
951 fz_free_device (dev
);
953 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
954 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
955 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
956 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
957 rect
= fz_intersect_rect (rect
, mediabox
);
959 if (!fz_is_empty_rect (rect
)) {
963 xps_free_page (state
.u
.xps
, page
);
964 printd ("progress %f loading %d",
965 (double) (pageno
+ 1) / state
.pagecount
,
973 if (state
.trimmargins
&& trimw
) {
976 page
= cbz_load_page (state
.u
.cbz
, pageno
);
977 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
978 cbz_free_page (state
.u
.cbz
, page
);
979 printd ("progress %f Trimming %d",
980 (double) (pageno
+ 1) / state
.pagecount
,
983 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
985 err (1, "fwrite trim mediabox");
991 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
993 err (1, "fread trim mediabox");
997 mediabox
.x0
= mediabox
.y0
= 0;
1006 ARSERT (0 && state
.type
);
1009 if (state
.pagedimcount
== 0
1010 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1011 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1014 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1015 state
.pagedims
= realloc (state
.pagedims
, size
);
1016 if (!state
.pagedims
) {
1017 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1018 size
, state
.pagedimcount
+ 1);
1021 p
= &state
.pagedims
[state
.pagedimcount
++];
1023 p
->mediabox
= mediabox
;
1028 if (state
.trimmargins
) {
1029 printd ("progress 1 Trimmed %d pages in %f seconds",
1030 state
.pagecount
, end
- start
);
1033 printd ("vmsg Processed %d pages in %f seconds",
1034 state
.pagecount
, end
- start
);
1038 if (fclose (trimf
)) {
1044 static void layout (void)
1049 double zoom
, w
, maxw
= 0;
1050 struct pagedim
*p
= state
.pagedims
;
1052 if (state
.proportional
) {
1053 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1054 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1056 w
= box
.x1
- box
.x0
;
1057 maxw
= MAX (w
, maxw
);
1062 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1065 ctm
= fz_rotate (state
.rotate
);
1066 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1068 w
= box
.x1
- box
.x0
;
1070 if (state
.proportional
) {
1071 double scale
= w
/ maxw
;
1072 zoom
= (state
.w
/ w
) * scale
;
1078 p
->zoomctm
= fz_scale (zoom
, zoom
);
1079 ctm
= fz_concat (p
->zoomctm
, ctm
);
1081 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1082 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1083 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1086 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1089 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1093 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1094 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1095 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1101 while (p
-- != state
.pagedims
) {
1102 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1103 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1104 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1105 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1109 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1113 static void recurse_outline (fz_outline
*outline
, int level
)
1118 struct pagedim
*pdim
= state
.pagedims
;
1120 dest
= &outline
->dest
;
1121 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1122 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1124 pdim
= &state
.pagedims
[i
];
1126 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1129 p
.y
= dest
->ld
.gotor
.lt
.y
;
1130 p
= fz_transform_point (pdim
->lctm
, p
);
1133 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1137 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1138 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1140 printd ("o %d %d %d %d %s",
1141 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1143 if (outline
->down
) {
1144 recurse_outline (outline
->down
, level
+ 1);
1146 outline
= outline
->next
;
1150 static void process_outline (void)
1152 fz_outline
*outline
;
1154 if (!state
.needoutline
) return;
1156 state
.needoutline
= 0;
1157 switch (state
.type
) {
1159 outline
= pdf_load_outline (state
.u
.pdf
);
1162 outline
= xps_load_outline (state
.u
.xps
);
1169 recurse_outline (outline
, 0);
1170 fz_free_outline (state
.ctx
, outline
);
1174 static char *strofspan (fz_text_span
*span
)
1179 size_t size
= 0, cap
= 80;
1181 p
= malloc (cap
+ 1);
1182 if (!p
) return NULL
;
1184 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1185 int n
= fz_runetochar (utf8
, ch
->c
);
1186 if (size
+ n
> cap
) {
1188 p
= realloc (p
, cap
+ 1);
1189 if (!p
) return NULL
;
1192 memcpy (p
+ size
, utf8
, n
);
1199 static int matchspan (regex_t
*re
, fz_text_span
*span
, fz_matrix ctm
,
1200 int stop
, int pageno
, double start
)
1207 fz_point p1
, p2
, p3
, p4
;
1209 p
= strofspan (span
);
1212 ret
= regexec (re
, p
, 1, &rm
, 0);
1215 if (ret
!= REG_NOMATCH
) {
1218 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1219 printd ("msg regexec error `%.*s'",
1220 (int) size
, errbuf
);
1227 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1228 c
+= fz_runelen (span
->text
[a
].c
);
1230 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1231 c
+= fz_runelen (span
->text
[b
].c
);
1234 if (fz_runelen (span
->text
[b
].c
) > 1) {
1237 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1238 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
1250 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1257 printd ("progress 1 found at %d `%.*s' in %f sec",
1258 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1262 printd ("match %d %d %f %f %f %f %f %f %f %f",
1274 static int compareblocks (const void *l
, const void *r
)
1276 fz_text_block
const *ls
= l
;
1277 fz_text_block
const* rs
= r
;
1278 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1281 /* wishful thinking function */
1282 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1287 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1289 fz_text_sheet
*sheet
;
1290 struct pagedim
*pdim
, *pdimprev
;
1291 int stop
= 0, niters
= 0;
1294 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1298 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1299 if (niters
++ == 5) {
1302 printd ("progress 1 attention requested aborting search at %d",
1307 printd ("progress %f searching in page %d",
1308 (double) (pageno
+ 1) / state
.pagecount
,
1313 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1314 pdim
= &state
.pagedims
[i
];
1315 if (pdim
->pageno
== pageno
) {
1318 if (pdim
->pageno
> pageno
) {
1327 sheet
= fz_new_text_sheet (state
.ctx
);
1328 text
= fz_new_text_page (state
.ctx
, fz_infinite_rect
);
1329 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1331 switch (state
.type
) {
1334 fz_try (state
.ctx
) {
1335 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1336 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1337 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1338 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, ctm
, NULL
);
1340 fz_catch (state
.ctx
) {
1341 fz_free_device (tdev
);
1348 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1350 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, ctm
, NULL
);
1354 ARSERT (0 && state
.type
);
1357 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1358 fz_free_device (tdev
);
1360 for (j
= 0; j
< text
->len
; ++j
) {
1362 fz_text_block
*block
;
1364 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1366 for (k
= 0; k
< block
->len
; ++k
) {
1371 line
= &block
->lines
[k
];
1372 if (line
->bbox
.y0
< y
+ 1) continue;
1375 line
= &block
->lines
[block
->len
- 1 - k
];
1376 if (line
->bbox
.y0
> y
- 1) continue;
1379 for (span
= line
->spans
;
1380 span
< line
->spans
+ line
->len
;
1383 switch (matchspan (re
, span
, ctm
, stop
, pageno
, start
)) {
1385 case 1: stop
= 1; break;
1386 case -1: stop
= 1; goto endloop
;
1401 fz_free_text_page (state
.ctx
, text
);
1402 fz_free_text_sheet (state
.ctx
, sheet
);
1404 state
.freepage (u
.ptr
);
1409 printd ("progress 1 no matches %f sec", end
- start
);
1411 printd ("clearrects");
1414 static void set_tex_params (int colorspace
)
1421 switch (colorspace
) {
1423 state
.texiform
= GL_RGBA8
;
1424 state
.texform
= GL_RGBA
;
1425 state
.texty
= GL_UNSIGNED_BYTE
;
1426 state
.colorspace
= fz_device_rgb
;
1429 state
.texiform
= GL_RGBA8
;
1430 state
.texform
= GL_BGRA
;
1431 state
.texty
= endianness
.s
> 1
1432 ? GL_UNSIGNED_INT_8_8_8_8
1433 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1434 state
.colorspace
= fz_device_bgr
;
1437 state
.texiform
= GL_LUMINANCE_ALPHA
;
1438 state
.texform
= GL_LUMINANCE_ALPHA
;
1439 state
.texty
= GL_UNSIGNED_BYTE
;
1440 state
.colorspace
= fz_device_gray
;
1443 errx (1, "invalid colorspce %d", colorspace
);
1447 static void realloctexts (int texcount
)
1451 if (texcount
== state
.texcount
) return;
1453 if (texcount
< state
.texcount
) {
1454 glDeleteTextures (state
.texcount
- texcount
,
1455 state
.texids
+ texcount
);
1458 size
= texcount
* sizeof (*state
.texids
);
1459 state
.texids
= realloc (state
.texids
, size
);
1460 if (!state
.texids
) {
1461 err (1, "realloc texids %" FMT_s
, size
);
1464 size
= texcount
* sizeof (*state
.texowners
);
1465 state
.texowners
= realloc (state
.texowners
, size
);
1466 if (!state
.texowners
) {
1467 err (1, "realloc texowners %" FMT_s
, size
);
1469 if (texcount
> state
.texcount
) {
1472 glGenTextures (texcount
- state
.texcount
,
1473 state
.texids
+ state
.texcount
);
1474 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1475 state
.texowners
[i
].w
= -1;
1476 state
.texowners
[i
].slice
= NULL
;
1479 state
.texcount
= texcount
;
1483 static void * mainloop (void *unused
)
1486 int len
, ret
, oldlen
= 0;
1491 errx (1, "readlen returned 0");
1494 if (oldlen
< len
+ 1) {
1495 p
= realloc (p
, len
+ 1);
1497 err (1, "realloc %d failed", len
+ 1);
1504 if (!strncmp ("open", p
, 4)) {
1507 char *filename
= p
+ 5;
1509 filenamelen
= strlen (filename
);
1510 password
= filename
+ filenamelen
+ 1;
1512 openxref (filename
, password
);
1515 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1516 state
.needoutline
= 1;
1518 else if (!strncmp ("cs", p
, 2)) {
1521 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1523 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1526 set_tex_params (colorspace
);
1527 for (i
= 0; i
< state
.texcount
; ++i
) {
1528 state
.texowners
[i
].w
= -1;
1529 state
.texowners
[i
].slice
= NULL
;
1533 else if (!strncmp ("freepage", p
, 8)) {
1536 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1538 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1542 else if (!strncmp ("freetile", p
, 8)) {
1545 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1547 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1551 else if (!strncmp ("search", p
, 6)) {
1552 int icase
, pageno
, y
, ret
, len2
, forward
;
1556 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1557 &icase
, &pageno
, &y
, &forward
, &len2
);
1559 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1562 pattern
= p
+ 6 + len2
;
1563 ret
= regcomp (&re
, pattern
,
1564 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1569 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1570 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1573 search (&re
, pageno
, y
, forward
);
1577 else if (!strncmp ("geometry", p
, 8)) {
1581 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1583 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1591 for (i
= 0; i
< state
.texcount
; ++i
) {
1592 state
.texowners
[i
].slice
= NULL
;
1598 unlock ("geometry");
1599 printd ("continue %d", state
.pagecount
);
1601 else if (!strncmp ("reqlayout", p
, 9)) {
1602 int rotate
, proportional
;
1605 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1607 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1610 if (state
.rotate
!= rotate
|| state
.proportional
!= proportional
) {
1613 state
.rotate
= rotate
;
1614 state
.proportional
= proportional
;
1618 unlock ("reqlayout");
1619 printd ("continue %d", state
.pagecount
);
1621 else if (!strncmp ("page", p
, 4)) {
1624 int pageno
, pindex
, ret
;
1626 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1628 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1633 page
= loadpage (pageno
, pindex
);
1637 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1639 else if (!strncmp ("tile", p
, 4)) {
1640 int x
, y
, w
, h
, ret
;
1645 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1646 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1648 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1653 tile
= rendertile (page
, x
, y
, w
, h
);
1657 printd ("tile %d %d %" FMT_ptr
" %u %f",
1659 FMT_ptr_cast2 (tile
),
1660 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1663 else if (!strncmp ("settrim", p
, 7)) {
1667 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1668 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1670 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1674 state
.trimmargins
= trimmargins
;
1675 state
.needoutline
= 1;
1676 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1678 state
.trimfuzz
= fuzz
;
1680 state
.pagedimcount
= 0;
1681 free (state
.pagedims
);
1682 state
.pagedims
= NULL
;
1687 printd ("continue %d", state
.pagecount
);
1689 else if (!strncmp ("sliceh", p
, 6)) {
1692 ret
= sscanf (p
+ 6, " %d", &h
);
1694 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1696 if (h
!= state
.sliceheight
) {
1699 state
.sliceheight
= h
;
1700 for (i
= 0; i
< state
.texcount
; ++i
) {
1701 state
.texowners
[i
].w
= -1;
1702 state
.texowners
[i
].h
= -1;
1703 state
.texowners
[i
].slice
= NULL
;
1707 else if (!strncmp ("interrupt", p
, 9)) {
1708 printd ("vmsg interrupted");
1711 errx (1, "unknown command %.*s", len
, p
);
1717 CAMLprim value
ml_realloctexts (value texcount_v
)
1719 CAMLparam1 (texcount_v
);
1722 if (trylock ("ml_realloctexts")) {
1726 realloctexts (Int_val (texcount_v
));
1728 unlock ("ml_realloctexts");
1731 CAMLreturn (Val_bool (ok
));
1734 static void showsel (struct page
*page
, int ox
, int oy
)
1740 fz_text_block
*block
;
1741 struct mark first
, last
;
1743 first
= page
->fmark
;
1746 if (!first
.span
|| !last
.span
) return;
1748 glEnable (GL_BLEND
);
1749 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1750 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1752 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1753 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1754 for (block
= page
->text
->blocks
;
1755 block
< page
->text
->blocks
+ page
->text
->len
;
1757 for (line
= block
->lines
;
1758 line
< block
->lines
+ block
->len
;
1760 for (span
= line
->spans
;
1761 span
< line
->spans
+ line
->len
;
1765 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1770 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1772 j
= MIN (first
.i
, last
.i
);
1773 k
= MAX (first
.i
, last
.i
);
1775 else if (span
== first
.span
) {
1779 else if (span
== last
.span
) {
1785 for (i
= j
; i
<= k
; ++i
) {
1786 fz_bbox b
= fz_round_rect (span
->text
[i
].bbox
);
1787 bbox
= fz_union_bbox (bbox
, b
);
1789 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1796 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1797 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1798 if (span
== last
.span
) {
1806 glDisable (GL_BLEND
);
1811 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1814 fz_link
*link
, *links
;
1816 switch (page
->type
) {
1818 links
= page
->u
.pdfpage
->links
;
1822 links
= page
->u
.xpspage
->links
;
1829 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1830 glEnable (GL_LINE_STIPPLE
);
1831 glLineStipple (0.5, 0xcccc);
1833 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1834 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1835 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1838 for (link
= links
; link
; link
= link
->next
) {
1839 fz_point p1
, p2
, p3
, p4
;
1841 p1
.x
= link
->rect
.x0
;
1842 p1
.y
= link
->rect
.y0
;
1844 p2
.x
= link
->rect
.x1
;
1845 p2
.y
= link
->rect
.y0
;
1847 p3
.x
= link
->rect
.x1
;
1848 p3
.y
= link
->rect
.y1
;
1850 p4
.x
= link
->rect
.x0
;
1851 p4
.y
= link
->rect
.y1
;
1853 p1
= fz_transform_point (ctm
, p1
);
1854 p2
= fz_transform_point (ctm
, p2
);
1855 p3
= fz_transform_point (ctm
, p3
);
1856 p4
= fz_transform_point (ctm
, p4
);
1858 switch (link
->dest
.kind
) {
1859 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1860 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1861 default: glColor3ub (0, 0, 0); break;
1864 glVertex2f (p1
.x
, p1
.y
);
1865 glVertex2f (p2
.x
, p2
.y
);
1866 glVertex2f (p3
.x
, p3
.y
);
1867 glVertex2f (p4
.x
, p4
.y
);
1871 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1872 glDisable (GL_LINE_STIPPLE
);
1875 static int compareslinks (const void *l
, const void *r
)
1877 struct slink
const *ls
= l
;
1878 struct slink
const *rs
= r
;
1879 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1880 return rs
->bbox
.x0
- rs
->bbox
.x0
;
1882 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1885 static void droptext (struct page
*page
)
1888 fz_free_text_page (state
.ctx
, page
->text
);
1891 page
->fmark
.span
= NULL
;
1892 page
->lmark
.span
= NULL
;
1896 fz_free_text_sheet (state
.ctx
, page
->sheet
);
1900 static void dropslinks (struct page
*page
)
1903 free (page
->slinks
);
1904 page
->slinks
= NULL
;
1905 page
->slinkcount
= 0;
1909 static void ensureslinks (struct page
*page
)
1913 size_t slinksize
= sizeof (*page
->slinks
);
1914 fz_link
*link
, *links
;
1916 if (state
.gen
!= page
->sgen
) {
1918 page
->sgen
= state
.gen
;
1920 if (page
->slinks
) return;
1922 switch (page
->type
) {
1924 links
= page
->u
.pdfpage
->links
;
1925 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1926 state
.pagedims
[page
->pdimno
].ctm
);
1930 links
= page
->u
.xpspage
->links
;
1931 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
1938 for (link
= links
; link
; link
= link
->next
) {
1942 page
->slinkcount
= count
;
1943 page
->slinks
= calloc (count
, slinksize
);
1944 if (!page
->slinks
) {
1945 err (1, "realloc slinks %d", count
);
1948 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
1949 page
->slinks
[i
].link
= link
;
1950 page
->slinks
[i
].bbox
=
1951 fz_round_rect (fz_transform_rect (ctm
, link
->rect
));
1953 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
1957 /* slightly tweaked fmt_ulong by D.J. Bernstein */
1958 static void fmt_linkn (char *s
, unsigned int u
)
1960 unsigned int len
; unsigned int q
;
1961 int zma
= 'z' - 'a' + 1;
1963 while (q
> zma
- 1) { ++len
; q
/= zma
; }
1966 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
1967 /* handles u == 0 */
1972 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
1973 int noff
, char *targ
, int tlen
, int hfsize
)
1977 struct slink
*slink
;
1978 double x0
, y0
, x1
, y1
, w
;
1980 ensureslinks (page
);
1981 glColor3ub (0xc3, 0xb0, 0x91);
1982 for (i
= 0; i
< page
->slinkcount
; ++i
) {
1983 fmt_linkn (buf
, i
+ noff
);
1984 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
1985 slink
= &page
->slinks
[i
];
1987 x0
= slink
->bbox
.x0
+ xoff
- 5;
1988 y1
= slink
->bbox
.y0
+ yoff
- 5;
1989 y0
= y1
+ 10 + hfsize
;
1990 w
= measure_string (state
.face
, hfsize
, buf
);
1992 glRectd (x0
, y0
, x1
, y1
);
1996 glEnable (GL_BLEND
);
1997 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1998 glEnable (GL_TEXTURE_2D
);
1999 glColor3ub (0, 0, 0);
2000 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2001 fmt_linkn (buf
, i
+ noff
);
2002 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2003 slink
= &page
->slinks
[i
];
2005 x0
= slink
->bbox
.x0
+ xoff
;
2006 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2007 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2010 glDisable (GL_TEXTURE_2D
);
2011 glDisable (GL_BLEND
);
2015 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2018 struct slice
*slice1
;
2021 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2022 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2024 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2025 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2026 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2030 int texindex
= state
.texindex
++ % state
.texcount
;
2032 if (state
.texowners
[texindex
].w
== tile
->w
) {
2033 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2037 state
.texowners
[texindex
].h
= slice
->h
;
2041 state
.texowners
[texindex
].h
= slice
->h
;
2044 state
.texowners
[texindex
].w
= tile
->w
;
2045 state
.texowners
[texindex
].slice
= slice
;
2046 slice
->texindex
= texindex
;
2048 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2050 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2058 tile
->pixmap
->samples
+offset
2062 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2070 tile
->pixmap
->samples
+offset
2076 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2078 CAMLparam2 (args_v
, ptr_v
);
2079 int dispx
= Int_val (Field (args_v
, 0));
2080 int dispy
= Int_val (Field (args_v
, 1));
2081 int dispw
= Int_val (Field (args_v
, 2));
2082 int disph
= Int_val (Field (args_v
, 3));
2083 int tilex
= Int_val (Field (args_v
, 4));
2084 int tiley
= Int_val (Field (args_v
, 5));
2085 char *s
= String_val (ptr_v
);
2086 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2088 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2090 int slicey
, firstslice
;
2091 struct slice
*slice
;
2093 firstslice
= tiley
/ tile
->sliceheight
;
2094 slice
= &tile
->slices
[firstslice
];
2095 slicey
= tiley
% tile
->sliceheight
;
2100 dh
= slice
->h
- slicey
;
2101 dh
= MIN (disph
, dh
);
2102 uploadslice (tile
, slice
);
2106 glTexCoord2i (tilex
, slicey
);
2107 glVertex2i (dispx
, dispy
);
2109 glTexCoord2i (tilex
+dispw
, slicey
);
2110 glVertex2i (dispx
+dispw
, dispy
);
2112 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2113 glVertex2i (dispx
+dispw
, dispy
+dh
);
2115 glTexCoord2i (tilex
, slicey
+dh
);
2116 glVertex2i (dispx
, dispy
+dh
);
2123 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2127 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2128 CAMLreturn (Val_unit
);
2131 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2132 value xoff_v
, value yoff_v
,
2135 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2136 int xoff
= Int_val (xoff_v
);
2137 int yoff
= Int_val (yoff_v
);
2138 int noff
= Int_val (Field (li_v
, 0));
2139 char *targ
= String_val (Field (li_v
, 1));
2140 int tlen
= caml_string_length (Field (li_v
, 1));
2141 int hfsize
= Int_val (Field (li_v
, 2));
2142 char *s
= String_val (ptr_v
);
2143 int hlmask
= Int_val (hlinks_v
);
2144 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2147 /* deal with loadpage failed pages */
2151 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2152 if (trylock ("ml_postprocess")) {
2157 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2158 noff
= page
->slinkcount
;
2160 showsel (page
, xoff
, yoff
);
2161 unlock ("ml_postprocess");
2164 CAMLreturn (Val_int (noff
));
2167 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2171 fz_link
*link
, *links
;
2173 switch (page
->type
) {
2175 ctm
= trimctm (page
->u
.pdfpage
, page
->pdimno
);
2176 links
= page
->u
.pdfpage
->links
;
2181 links
= page
->u
.xpspage
->links
;
2190 ctm
= fz_concat (ctm
, state
.pagedims
[page
->pdimno
].ctm
);
2191 ctm
= fz_invert_matrix (ctm
);
2192 p
= fz_transform_point (ctm
, p
);
2194 for (link
= links
; link
; link
= link
->next
) {
2195 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2196 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2204 static void ensuretext (struct page
*page
)
2206 if (state
.gen
!= page
->tgen
) {
2208 page
->tgen
= state
.gen
;
2213 page
->text
= fz_new_text_page (state
.ctx
, fz_infinite_rect
);
2214 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2215 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2216 fz_run_display_list (page
->dlist
,
2219 fz_infinite_bbox
, NULL
);
2220 qsort (page
->text
->blocks
, page
->text
->len
,
2221 sizeof (*page
->text
->blocks
), compareblocks
);
2222 fz_free_device (tdev
);
2226 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2228 CAMLparam2 (start_page_v
, dir_v
);
2230 int i
, dir
= Int_val (dir_v
);
2231 int start_page
= Int_val (start_page_v
);
2232 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2234 ret_v
= Val_int (0);
2235 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2239 lock ("ml_findpage_with_links");
2240 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2243 switch (state
.type
) {
2246 pdf_page
*page
= NULL
;
2248 fz_try (state
.ctx
) {
2249 page
= pdf_load_page (state
.u
.pdf
, i
);
2250 found
= !!page
->links
;
2252 fz_catch (state
.ctx
) {
2262 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2263 found
= !!page
->links
;
2269 ARSERT ("invalid document type");
2273 ret_v
= caml_alloc_small (1, 1);
2274 Field (ret_v
, 0) = Val_int (i
);
2279 unlock ("ml_findpage_with_links");
2285 enum { dir_first
, dir_last
};
2286 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2288 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2290 CAMLparam2 (ptr_v
, dir_v
);
2291 CAMLlocal2 (ret_v
, pos_v
);
2293 int dirtag
, i
, slinkindex
;
2294 struct slink
*found
= NULL
,*slink
;
2295 char *s
= String_val (ptr_v
);
2297 page
= parse_pointer ("ml_findlink", s
);
2298 ret_v
= Val_int (0);
2299 if (trylock ("ml_findlink")) {
2303 ensureslinks (page
);
2305 if (Is_block (dir_v
)) {
2306 dirtag
= Tag_val (dir_v
);
2308 case dir_first_visible
:
2310 int x0
, y0
, dir
, first_index
, last_index
;
2312 pos_v
= Field (dir_v
, 0);
2313 x0
= Int_val (Field (pos_v
, 0));
2314 y0
= Int_val (Field (pos_v
, 1));
2315 dir
= Int_val (Field (pos_v
, 2));
2320 last_index
= page
->slinkcount
;
2323 first_index
= page
->slinkcount
- 1;
2327 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2328 slink
= &page
->slinks
[i
];
2329 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2338 slinkindex
= Int_val (Field (dir_v
, 0));
2339 found
= &page
->slinks
[slinkindex
];
2340 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2341 slink
= &page
->slinks
[i
];
2342 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2350 slinkindex
= Int_val (Field (dir_v
, 0));
2351 found
= &page
->slinks
[slinkindex
];
2352 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2353 slink
= &page
->slinks
[i
];
2354 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2362 slinkindex
= Int_val (Field (dir_v
, 0));
2363 found
= &page
->slinks
[slinkindex
];
2364 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2365 slink
= &page
->slinks
[i
];
2366 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2374 slinkindex
= Int_val (Field (dir_v
, 0));
2375 found
= &page
->slinks
[slinkindex
];
2376 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2377 slink
= &page
->slinks
[i
];
2378 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2387 dirtag
= Int_val (dir_v
);
2390 found
= page
->slinks
;
2395 found
= page
->slinks
+ (page
->slinkcount
- 1);
2401 ret_v
= caml_alloc_small (2, 1);
2402 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2405 unlock ("ml_findlink");
2410 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2416 switch (link->dest.kind) { \
2417 case FZ_LINK_GOTO: \
2421 pageno = link->dest.ld.gotor.page; \
2425 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2426 p.y = link->dest.ld.gotor.lt.y; \
2427 p = fz_transform_point (pdim->lctm, p); \
2429 tup_v = caml_alloc_tuple (2); \
2430 ret_v = caml_alloc_small (1, ugoto); \
2431 Field (tup_v, 0) = Val_int (pageno); \
2432 Field (tup_v, 1) = Val_int (p.y); \
2433 Field (ret_v, 0) = tup_v; \
2438 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2439 ret_v = caml_alloc_small (1, uuri); \
2440 Field (ret_v, 0) = str_v; \
2443 case FZ_LINK_LAUNCH: \
2444 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2445 ret_v = caml_alloc_small (1, ulaunch); \
2446 Field (ret_v, 0) = str_v; \
2449 case FZ_LINK_NAMED: \
2450 str_v = caml_copy_string (link->dest.ld.named.named); \
2451 ret_v = caml_alloc_small (1, unamed); \
2452 Field (ret_v, 0) = str_v; \
2455 case FZ_LINK_GOTOR: \
2456 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2457 pageno = link->dest.ld.gotor.page; \
2458 tup_v = caml_alloc_tuple (2); \
2459 ret_v = caml_alloc_small (1, uremote); \
2460 Field (tup_v, 0) = str_v; \
2461 Field (tup_v, 1) = Val_int (pageno); \
2462 Field (ret_v, 0) = tup_v; \
2469 snprintf (buf, sizeof (buf), \
2470 "unhandled link kind %d", link->dest.kind); \
2471 str_v = caml_copy_string (buf); \
2472 ret_v = caml_alloc_small (1, uunexpected); \
2473 Field (ret_v, 0) = str_v; \
2479 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2481 CAMLparam2 (ptr_v
, n_v
);
2482 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2485 struct pagedim
*pdim
;
2486 char *s
= String_val (ptr_v
);
2488 ret_v
= Val_int (0);
2489 if (trylock ("ml_getlink")) {
2493 page
= parse_pointer ("ml_getlink", s
);
2494 ensureslinks (page
);
2495 pdim
= &state
.pagedims
[page
->pdimno
];
2496 link
= page
->slinks
[Int_val (n_v
)].link
;
2499 unlock ("ml_getlink");
2504 CAMLprim value
ml_getlinkcount (value ptr_v
)
2508 char *s
= String_val (ptr_v
);
2510 page
= parse_pointer ("ml_getlinkcount", s
);
2511 CAMLreturn (Val_int (page
->slinkcount
));
2514 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2516 CAMLparam2 (ptr_v
, n_v
);
2519 struct slink
*slink
;
2520 char *s
= String_val (ptr_v
);
2522 page
= parse_pointer ("ml_getlinkrect", s
);
2523 ret_v
= caml_alloc_tuple (4);
2524 if (trylock ("ml_getlinkrect")) {
2525 Field (ret_v
, 0) = Val_int (0);
2526 Field (ret_v
, 1) = Val_int (0);
2527 Field (ret_v
, 2) = Val_int (0);
2528 Field (ret_v
, 3) = Val_int (0);
2531 ensureslinks (page
);
2533 slink
= &page
->slinks
[Int_val (n_v
)];
2534 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2535 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2536 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2537 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2538 unlock ("ml_getlinkrect");
2544 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2546 CAMLparam3 (ptr_v
, x_v
, y_v
);
2547 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2550 char *s
= String_val (ptr_v
);
2551 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2552 struct pagedim
*pdim
;
2554 ret_v
= Val_int (0);
2555 if (trylock ("ml_whatsunder")) {
2559 page
= parse_pointer ("ml_whatsunder", s
);
2560 pdim
= &state
.pagedims
[page
->pdimno
];
2561 x
+= pdim
->bounds
.x0
;
2562 y
+= pdim
->bounds
.y0
;
2563 link
= getlink (page
, x
, y
);
2569 fz_text_block
*block
;
2572 for (block
= page
->text
->blocks
;
2573 block
< page
->text
->blocks
+ page
->text
->len
;
2578 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2581 for (line
= block
->lines
;
2582 line
< block
->lines
+ block
->len
;
2587 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2590 for (span
= line
->spans
;
2591 span
< line
->spans
+ line
->len
;
2596 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2599 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
2602 if (x
>= b
->x0
&& x
<= b
->x1
2603 && y
>= b
->y0
&& y
<= b
->y1
) {
2605 span
->style
->font
&& span
->style
->font
->name
2606 ? span
->style
->font
->name
2607 : "Span has no font name"
2609 FT_FaceRec
*face
= span
->style
->font
->ft_face
;
2610 if (face
&& face
->family_name
) {
2612 char *n1
= face
->family_name
;
2613 size_t l1
= strlen (n1
);
2614 size_t l2
= strlen (n2
);
2616 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2617 s
= malloc (l1
+ l2
+ 2);
2621 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2622 str_v
= caml_copy_string (s
);
2628 str_v
= caml_copy_string (n2
);
2630 ret_v
= caml_alloc_small (1, utext
);
2631 Field (ret_v
, 0) = str_v
;
2640 unlock ("ml_whatsunder");
2646 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2648 CAMLparam2 (ptr_v
, rect_v
);
2651 struct pagedim
*pdim
;
2652 int i
, x0
, x1
, y0
, y1
;
2653 char *s
= String_val (ptr_v
);
2655 fz_text_block
*block
;
2656 fz_text_span
*span
, *fspan
, *lspan
;
2657 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2659 if (trylock ("ml_seltext")) {
2663 page
= parse_pointer ("ml_seltext", s
);
2666 pdim
= &state
.pagedims
[page
->pdimno
];
2667 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2668 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2669 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2670 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2673 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2674 glColor3ub (128, 128, 128);
2675 glRecti (x0
, y0
, x1
, y1
);
2676 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2679 fspan
= lspan
= NULL
;
2681 for (block
= page
->text
->blocks
;
2682 block
< page
->text
->blocks
+ page
->text
->len
;
2684 for (line
= block
->lines
;
2685 line
< block
->lines
+ block
->len
;
2687 for (span
= line
->spans
;
2688 span
< line
->spans
+ line
->len
;
2690 for (i
= 0; i
< span
->len
; ++i
) {
2691 b
= &span
->text
[i
].bbox
;
2694 if (x0
>= b
->x0
&& x0
<= b
->x1
2695 && y0
>= b
->y0
&& y0
<= b
->y1
) {
2701 if (x1
>= b
->x0
&& x1
<= b
->x1
2702 && y1
>= b
->y0
&& y1
<= b
->y1
) {
2708 if (0 && selected
) {
2709 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2710 glColor3ub (128, 128, 128);
2711 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2712 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2718 if (y1
< y0
|| x1
< x0
) {
2721 if (fspan
== lspan
) {
2726 if (fline
!= lline
) {
2745 page
->fmark
.span
= fspan
;
2748 page
->lmark
.span
= lspan
;
2750 unlock ("ml_seltext");
2753 CAMLreturn (Val_unit
);
2756 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2761 for (i
= a
; i
<= b
; ++i
) {
2762 len
= fz_runetochar (buf
, span
->text
[i
].c
);
2763 ret
= fwrite (buf
, len
, 1, f
);
2766 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2767 len
, ret
, strerror (errno
));
2775 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
2777 caml_failwith ("ml_popen not implemented under Cygwin");
2780 CAMLprim value
ml_popen (value command_v
, value fds_v
)
2782 CAMLparam2 (command_v
, fds_v
);
2783 CAMLlocal2 (l_v
, tup_v
);
2786 value earg_v
= Nothing
;
2787 posix_spawnattr_t attr
;
2788 posix_spawn_file_actions_t fa
;
2789 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
2791 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
2792 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
2795 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
2796 msg
= "posix_spawnattr_init";
2800 #ifdef POSIX_SPAWN_USEVFORK
2801 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
2802 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
2807 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
2810 tup_v
= Field (l_v
, 0);
2811 fd1
= Int_val (Field (tup_v
, 0));
2812 fd2
= Int_val (Field (tup_v
, 1));
2814 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
2815 msg
= "posix_spawn_file_actions_addclose";
2821 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
2822 msg
= "posix_spawn_file_actions_adddup2";
2829 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
2830 msg
= "posix_spawn";
2835 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
2836 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
2840 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
2841 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
2846 unix_error (ret
, msg
, earg_v
);
2848 CAMLreturn (Val_unit
);
2852 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
2860 fz_text_block
*block
;
2861 int fd
= Int_val (fd_v
);
2862 char *s
= String_val (ptr_v
);
2864 if (trylock ("ml_copysel")) {
2868 page
= parse_pointer ("ml_sopysel", s
);
2870 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2871 fprintf (stderr
, "nothing to copy\n");
2875 f
= fdopen (fd
, "w");
2877 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
2882 for (block
= page
->text
->blocks
;
2883 block
< page
->text
->blocks
+ page
->text
->len
;
2885 for (line
= block
->lines
;
2886 line
< block
->lines
+ block
->len
;
2888 for (span
= line
->spans
;
2889 span
< line
->spans
+ line
->len
;
2893 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
2894 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2895 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2898 if (pipespan (f
, span
, a
, b
)) {
2901 if (span
== line
->spans
+ line
->len
- 1) {
2902 if (putc ('\n', f
) == EOF
) {
2904 "failed break line on sel pipe: %s\n",
2909 if (span
== page
->lmark
.span
) {
2917 page
->lmark
.span
= NULL
;
2918 page
->fmark
.span
= NULL
;
2922 int ret
= fclose (f
);
2925 if (errno
!= ECHILD
) {
2926 fprintf (stderr
, "failed to close sel pipe: %s\n",
2932 unlock ("ml_copysel");
2937 fprintf (stderr
, "failed to close sel pipe: %s\n",
2941 CAMLreturn (Val_unit
);
2944 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2946 CAMLparam1 (pagedimno_v
);
2948 int pagedimno
= Int_val (pagedimno_v
);
2951 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2952 if (trylock ("ml_getpdimrect")) {
2953 box
= fz_empty_rect
;
2956 box
= state
.pagedims
[pagedimno
].mediabox
;
2957 unlock ("ml_getpdimrect");
2960 Store_double_field (ret_v
, 0, box
.x0
);
2961 Store_double_field (ret_v
, 1, box
.x1
);
2962 Store_double_field (ret_v
, 2, box
.y0
);
2963 Store_double_field (ret_v
, 3, box
.y1
);
2968 static double getmaxw (void)
2974 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2977 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2978 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2981 maxw
= MAX (w
, maxw
);
2986 CAMLprim value
ml_getmaxw (value unit_v
)
2988 CAMLparam1 (unit_v
);
2992 if (trylock ("ml_getmaxw")) {
2996 unlock ("ml_getmaxw");
2998 ret_v
= caml_copy_double (maxw
);
3002 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3003 value dw_v
, value cols_v
)
3005 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3009 double maxw
= 0.0, maxh
= 0.0;
3011 double winw
= Int_val (winw_v
);
3012 double winh
= Int_val (winh_v
);
3013 double dw
= Int_val (dw_v
);
3014 double cols
= Int_val (cols_v
);
3015 double pw
= 1.0, ph
= 1.0, aspect
;
3017 if (trylock ("ml_zoom_for_height")) {
3021 if (state
.proportional
) {
3022 maxw
= getmaxw () / cols
;
3025 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3026 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
3029 rect
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
3031 x0
= MIN (rect
.x0
, rect
.x1
);
3032 x1
= MAX (rect
.x0
, rect
.x1
);
3033 y0
= MIN (rect
.y0
, rect
.y1
);
3034 y1
= MAX (rect
.y0
, rect
.y1
);
3036 w
= (x1
- x0
) / cols
;
3039 if (state
.proportional
) {
3041 scaledh
= h
* scale
;
3048 if (scaledh
> maxh
) {
3056 zoom
= (winh
* aspect
+ dw
) / winw
;
3058 unlock ("ml_zoom_for_height");
3060 ret_v
= caml_copy_double (zoom
);
3064 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3066 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3068 int pt
= Int_val(pt_v
);
3069 int x
= Int_val (x_v
);
3070 int y
= Int_val (y_v
);
3073 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3074 ret_v
= caml_copy_double (w
);
3078 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3080 CAMLparam2 (pt_v
, string_v
);
3082 int pt
= Int_val (pt_v
);
3085 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3086 ret_v
= caml_copy_double (w
);
3090 CAMLprim value
ml_getpagebox (value opaque_v
)
3092 CAMLparam1 (opaque_v
);
3096 char *s
= String_val (opaque_v
);
3097 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3099 ret_v
= caml_alloc_tuple (4);
3100 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
3101 dev
->hints
|= FZ_IGNORE_SHADE
;
3103 switch (page
->type
) {
3105 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
3109 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
3113 bbox
= fz_infinite_bbox
;
3117 fz_free_device (dev
);
3118 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3119 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3120 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3121 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3126 CAMLprim value
ml_setaalevel (value level_v
)
3128 CAMLparam1 (level_v
);
3130 state
.aalevel
= Int_val (level_v
);
3131 CAMLreturn (Val_unit
);
3135 #include <X11/Xlib.h>
3141 GLXDrawable drawable
;
3144 #include "keysym2ucs.c"
3146 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3148 CAMLparam1 (keysym_v
);
3150 KeySym keysym
= Int_val (keysym_v
);
3155 rune
= keysym2ucs (keysym
);
3156 len
= fz_runetochar (buf
, rune
);
3158 str_v
= caml_copy_string (buf
);
3162 CAMLprim value
ml_glx (value win_v
)
3165 XVisualInfo
*visual
;
3166 int screen
, wid
= Int_val (win_v
);
3167 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3169 glx
.dpy
= XOpenDisplay (NULL
);
3171 caml_failwith ("XOpenDisplay failed");
3174 screen
= DefaultScreen (glx
.dpy
);
3175 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3177 XCloseDisplay (glx
.dpy
);
3179 caml_failwith ("glXChooseVisual");
3182 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3185 XCloseDisplay (glx
.dpy
);
3187 caml_failwith ("glXCreateContext");
3190 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3191 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3192 XCloseDisplay (glx
.dpy
);
3195 caml_failwith ("glXMakeCurrent");
3201 (int (*) (int)) glXGetProcAddress ((GLubyte
*) "glXSwapIntervalMESA");
3202 printf ("sync %d\n", f (0));
3204 CAMLreturn (Val_unit
);
3207 CAMLprim value
ml_swapb (value unit_v
)
3209 CAMLparam1 (unit_v
);
3210 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3211 CAMLreturn (Val_unit
);
3214 CAMLprim value
ml_glxsync (value unit_v
)
3216 CAMLparam1 (unit_v
);
3217 if (glx
.dpy
&& glx
.ctx
) {
3221 CAMLreturn (Val_unit
);
3224 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3225 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3227 CAMLprim value
ml_platform (value unit_v
)
3229 CAMLparam1 (unit_v
);
3230 int platid
= piunknown
;
3232 #if defined __linux__
3234 #elif defined __CYGWIN__
3236 #elif defined __DragonFly__
3237 platid
= pidragonflybsd
;
3238 #elif defined __FreeBSD__
3240 #elif defined __OpenBSD__
3242 #elif defined __NetBSD__
3244 #elif defined __sun__
3246 #elif defined __APPLE__
3249 CAMLreturn (Val_int (platid
));
3252 CAMLprim value
ml_cloexec (value fd_v
)
3255 int fd
= Int_val (fd_v
);
3257 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3258 uerror ("fcntl", Nothing
);
3260 CAMLreturn (Val_unit
);
3263 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3265 CAMLparam2 (pipe_v
, params_v
);
3266 CAMLlocal2 (trim_v
, fuzz_v
);
3272 struct sigaction sa
;
3274 state
.cr
= Int_val (Field (pipe_v
, 0));
3275 state
.cw
= Int_val (Field (pipe_v
, 1));
3276 state
.rotate
= Int_val (Field (params_v
, 0));
3277 state
.proportional
= Bool_val (Field (params_v
, 1));
3278 trim_v
= Field (params_v
, 2);
3279 texcount
= Int_val (Field (params_v
, 3));
3280 state
.sliceheight
= Int_val (Field (params_v
, 4));
3281 mustoresize
= Int_val (Field (params_v
, 5));
3282 colorspace
= Int_val (Field (params_v
, 6));
3283 fontpath
= String_val (Field (params_v
, 7));
3284 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3286 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3288 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3289 fuzz_v
= Field (trim_v
, 1);
3290 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3291 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3292 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3293 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3295 set_tex_params (colorspace
);
3298 state
.face
= load_font (fontpath
);
3302 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3304 state
.face
= load_builtin_font (base
, len
);
3306 if (!state
.face
) _exit (1);
3308 realloctexts (texcount
);
3311 sa
.sa_handler
= SIG_IGN
;
3312 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3314 sa
.sa_handler
= SIG_DFL
;
3315 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3317 if (sigemptyset (&sa
.sa_mask
)) {
3318 err (1, "sigemptyset");
3320 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3321 err (1, "sigaction");
3324 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3326 errx (1, "pthread_create: %s", strerror (ret
));
3329 CAMLreturn (Val_unit
);