1 /* lots of code c&p-ed directly from mupdf */
11 #define WIN32_LEAN_AND_MEAN
21 #pragma warning (disable:4244)
22 #pragma warning (disable:4996)
23 #pragma warning (disable:4995)
24 #define NORETURN __declspec (noreturn)
27 #elif defined __GNUC__
28 #define NORETURN __attribute__ ((noreturn))
29 #define UNUSED __attribute__ ((unused))
30 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
31 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
39 /* some versions of MingW have non idempotent %p */
41 #define FMT_ptr PRIxPTR
42 #define FMT_ptr_cast(p) ((intptr_t *) (p))
43 #define FMT_ptr_cast2(p) ((intptr_t) (p))
46 #define FMT_ptr_cast(p) (p)
47 #define FMT_ptr_cast2(p) (p)
63 #include <sys/types.h>
64 #include <sys/ioctl.h>
66 static void NORETURN
GCC_FMT_ATTR (2, 3)
67 winerr (int exitcode
, const char *fmt
, ...)
70 DWORD savederror
= GetLastError ();
73 vfprintf (stderr
, fmt
, ap
);
75 fprintf (stderr
, ": 0x%lx\n", savederror
);
81 static void NORETURN
GCC_FMT_ATTR (2, 3)
82 err (int exitcode
, const char *fmt
, ...)
89 vfprintf (stderr
, fmt
, ap
);
91 fprintf (stderr
, ": %s\n", strerror (savederrno
));
96 static void NORETURN
GCC_FMT_ATTR (2, 3)
97 errx (int exitcode
, const char *fmt
, ...)
102 vfprintf (stderr
, fmt
, ap
);
104 fputc ('\n', stderr
);
110 #include <OpenGL/gl.h>
115 #ifndef GL_TEXTURE_RECTANGLE_ARB
116 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
120 #define GL_BGRA 0x80E1
123 #ifndef GL_UNSIGNED_INT_8_8_8_8
124 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
127 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
128 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
131 #include <caml/fail.h>
132 #include <caml/alloc.h>
133 #include <caml/memory.h>
134 #include <caml/unixsupport.h>
141 #include FT_FREETYPE_H
144 #define lprintf printf
149 #define ARSERT(cond) for (;;) { \
151 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
166 struct slice slices
[1];
177 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
180 enum { DPDF
, DXPS
, DCBZ
};
194 fz_display_list
*dlist
;
199 void (*freepage
) (void *);
210 struct pagedim
*pagedims
;
219 fz_glyph_cache
*cache
;
230 fz_colorspace
*colorspace
;
255 void (*closedoc
) (void);
256 void (*freepage
) (void *);
259 static void UNUSED
debug_rect (const char *cap
, fz_rect r
)
261 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
264 static void UNUSED
debug_bbox (const char *cap
, fz_bbox r
)
266 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
269 static void UNUSED
debug_matrix (const char *cap
, fz_matrix m
)
271 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
272 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
276 static CRITICAL_SECTION critsec
;
278 static void lock (void *unused
)
281 EnterCriticalSection (&critsec
);
284 static void unlock (void *unused
)
287 LeaveCriticalSection (&critsec
);
290 static int trylock (void *unused
)
292 return TryEnterCriticalSection (&critsec
) == 0;
295 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
297 static void lock (const char *cap
)
299 int ret
= pthread_mutex_lock (&mutex
);
301 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
305 static void unlock (const char *cap
)
307 int ret
= pthread_mutex_unlock (&mutex
);
309 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
313 static int trylock (const char *cap
)
315 int ret
= pthread_mutex_trylock (&mutex
);
317 if (ret
&& ret
!= EBUSY
) {
318 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
324 static void *parse_pointer (const char *cap
, const char *s
)
329 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
331 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
337 static double now (void)
342 GetSystemTimeAsFileTime (&ft
);
343 tmp
= ft
.dwHighDateTime
;
345 tmp
|= ft
.dwLowDateTime
;
349 static int hasdata (void)
354 okay
= PeekNamedPipe (state
.cr
, NULL
, 0, NULL
, &avail
, NULL
);
355 if (!okay
) winerr (1, "PeekNamedPipe");
359 static void readdata (void *p
, int size
)
364 okay
= ReadFile (state
.cr
, p
, size
, &nread
, NULL
);
365 if (!okay
|| nread
- size
) {
366 err (1, "ReadFile (req %d, okay %d, ret %ld)", size
, okay
, nread
);
370 static int readlen (void)
375 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
378 static void writedata (char *p
, int size
)
384 buf
[0] = (size
>> 24) & 0xff;
385 buf
[1] = (size
>> 16) & 0xff;
386 buf
[2] = (size
>> 8) & 0xff;
387 buf
[3] = (size
>> 0) & 0xff;
389 okay
= WriteFile (state
.cw
, buf
, 4, &nwritten
, NULL
);
390 if (!okay
|| nwritten
!= 4) {
391 winerr (1, "WriteFile okay %d ret %ld", okay
, nwritten
);
394 okay
= WriteFile (state
.cw
, p
, size
, &nwritten
, NULL
);
395 if (!okay
|| nwritten
- size
) {
396 winerr (1, "WriteFile (req %d, okay %d, ret %ld)",
397 size
, okay
, nwritten
);
401 static double now (void)
405 if (gettimeofday (&tv
, NULL
)) {
406 err (1, "gettimeofday");
408 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
411 static int hasdata (void)
414 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
415 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
419 static void readdata (void *p
, int size
)
423 n
= read (state
.cr
, p
, size
);
425 if (!n
) errx (1, "EOF while reading");
426 err (1, "read (req %d, ret %zd)", size
, n
);
430 static int readlen (void)
435 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
438 static void writedata (char *p
, int size
)
443 buf
[0] = (size
>> 24) & 0xff;
444 buf
[1] = (size
>> 16) & 0xff;
445 buf
[2] = (size
>> 8) & 0xff;
446 buf
[3] = (size
>> 0) & 0xff;
448 n
= write (state
.cw
, buf
, 4);
450 if (!n
) errx (1, "EOF while writing length");
451 err (1, "write %zd", n
);
454 n
= write (state
.cw
, p
, size
);
456 if (!n
) errx (1, "EOF while writing data");
457 err (1, "write (req %d, ret %zd)", size
, n
);
462 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
470 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
473 len
= vsnprintf (buf
, size
, fmt
, ap
);
476 if (len
> -1 && len
< size
) {
477 writedata (buf
, len
);
487 buf
= realloc (buf
, size
);
492 static void closepdf (void)
495 pdf_close_document (state
.u
.pdf
);
500 static void closexps (void)
503 xps_close_document (state
.u
.xps
);
508 static void closecbz (void)
511 cbz_close_document (state
.u
.cbz
);
516 static void freepdfpage (void *ptr
)
518 pdf_free_page (state
.u
.pdf
, ptr
);
521 static void freexpspage (void *ptr
)
523 xps_free_page (state
.u
.xps
, ptr
);
526 static void freecbzpage (void *ptr
)
528 cbz_free_page (state
.u
.cbz
, ptr
);
531 static void openxref (char *filename
, char *password
)
535 for (i
= 0; i
< state
.texcount
; ++i
) {
536 state
.texowners
[i
].w
= -1;
537 state
.texowners
[i
].slice
= NULL
;
540 if (state
.closedoc
) state
.closedoc ();
542 len
= strlen (filename
);
548 ext
[0] = tolower (filename
[len
-3]);
549 ext
[1] = tolower (filename
[len
-2]);
550 ext
[2] = tolower (filename
[len
-1]);
552 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
555 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
560 if (state
.pagedims
) {
561 free (state
.pagedims
);
562 state
.pagedims
= NULL
;
564 state
.pagedimcount
= 0;
566 fz_set_aa_level (state
.ctx
, state
.aalevel
);
567 switch (state
.type
) {
569 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
570 if (pdf_needs_password (state
.u
.pdf
)) {
571 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
573 errx (1, "invalid password");
576 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
577 state
.closedoc
= closepdf
;
578 state
.freepage
= freepdfpage
;
582 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
583 state
.pagecount
= xps_count_pages (state
.u
.xps
);
584 state
.closedoc
= closexps
;
585 state
.freepage
= freexpspage
;
589 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
590 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
591 state
.closedoc
= closecbz
;
592 state
.freepage
= freecbzpage
;
597 static void pdfinfo (void)
599 if (state
.type
== DPDF
) {
602 printd ("info PDF version\t%d.%d",
603 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
605 infoobj
= fz_dict_gets (state
.u
.pdf
->trailer
, "Info");
609 char *items
[] = { "Title", "Author", "Creator",
610 "Producer", "CreationDate" };
612 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
613 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
614 s
= pdf_to_utf8 (state
.ctx
, obj
);
617 printd ("title %s", s
);
619 printd ("info %s\t%s", items
[i
], s
);
621 fz_free (state
.ctx
, s
);
628 static void unlinktile (struct tile
*tile
)
632 for (i
= 0; i
< tile
->slicecount
; ++i
) {
633 struct slice
*s
= &tile
->slices
[i
];
635 if (s
->texindex
!= -1) {
636 if (state
.texowners
[s
->texindex
].slice
== s
) {
637 state
.texowners
[s
->texindex
].slice
= NULL
;
643 static void freepage (struct page
*page
)
646 fz_free_text_span (state
.ctx
, page
->text
);
648 page
->freepage (page
->u
.ptr
);
649 fz_free_display_list (state
.ctx
, page
->dlist
);
653 static void freetile (struct tile
*tile
)
657 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
660 fz_drop_pixmap (state
.ctx
, state
.pig
);
662 state
.pig
= tile
->pixmap
;
670 static int cacheline32bytes
;
671 extern char **environ
;
673 static void __attribute__ ((constructor
)) clcheck (void)
675 char **envp
= environ
;
680 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
682 cacheline32bytes
= auxv
[1] == 32;
688 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
690 if (cacheline32bytes
) {
691 intptr_t a1
, a2
, diff
;
692 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
693 vector
unsigned char v
= vec_splat_u8 (-1);
694 vector
unsigned char *p
;
696 a1
= a2
= (intptr_t) pixmap
->samples
;
697 a2
= (a1
+ 31) & ~31;
702 while (a1
!= a2
) *(char *) a1
++ = 0xff;
703 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
704 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
706 vec_st (v
, i
+ 16, p
);
708 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
710 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
713 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
716 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
719 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
721 if (!pdim
->tctmready
) {
722 if (state
.trimmargins
) {
725 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
726 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
727 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
728 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
739 static fz_matrix
pagectm (struct page
*page
)
741 if (page
->type
== DPDF
) {
742 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
743 state
.pagedims
[page
->pdimno
].ctm
);
747 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
749 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
750 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
751 -pdim
->mediabox
.y0
), ctm
);
756 static void *loadpage (int pageno
, int pindex
)
759 struct page
*page
= NULL
;
761 page
= calloc (sizeof (struct page
), 1);
763 err (1, "calloc page %d", pageno
);
766 page
->dlist
= fz_new_display_list (state
.ctx
);
767 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
768 switch (state
.type
) {
770 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
771 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
772 page
->freepage
= freepdfpage
;
776 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
777 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
778 page
->freepage
= freexpspage
;
782 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
783 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
784 page
->freepage
= freecbzpage
;
787 fz_free_device (dev
);
789 page
->pdimno
= pindex
;
790 page
->pageno
= pageno
;
791 page
->gen
= state
.gen
;
792 page
->type
= state
.type
;
797 static struct tile
*alloctile (int h
)
804 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
805 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
806 tile
= calloc (tilesize
, 1);
808 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
810 for (i
= 0; i
< slicecount
; ++i
) {
811 int sh
= MIN (h
, state
.sliceheight
);
812 tile
->slices
[i
].h
= sh
;
813 tile
->slices
[i
].texindex
= -1;
816 tile
->slicecount
= slicecount
;
817 tile
->sliceheight
= state
.sliceheight
;
821 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
826 struct pagedim
*pdim
;
828 tile
= alloctile (h
);
829 pdim
= &state
.pagedims
[page
->pdimno
];
834 bbox
.x1
= bbox
.x0
+ w
;
835 bbox
.y1
= bbox
.y0
+ h
;
838 if (state
.pig
->w
== w
840 && state
.pig
->colorspace
== state
.colorspace
) {
841 tile
->pixmap
= state
.pig
;
842 tile
->pixmap
->x
= bbox
.x0
;
843 tile
->pixmap
->y
= bbox
.y0
;
846 fz_drop_pixmap (state
.ctx
, state
.pig
);
852 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
857 clearpixmap (tile
->pixmap
);
858 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
859 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
860 fz_free_device (dev
);
865 static void initpdims (void)
871 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
877 switch (state
.type
) {
879 pageobj
= state
.u
.pdf
->page_objs
[pageno
];
881 if (state
.trimmargins
) {
885 page
= pdf_load_page (state
.u
.pdf
, pageno
);
886 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
887 if (state
.trimanew
|| !obj
) {
893 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
894 dev
->hints
|= FZ_IGNORE_SHADE
;
895 ctm
= fz_invert_matrix (page
->ctm
);
896 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
897 fz_free_device (dev
);
899 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
900 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
901 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
902 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
903 rect
= fz_transform_rect (ctm
, rect
);
904 rect
= fz_intersect_rect (rect
, page
->mediabox
);
906 if (fz_is_empty_rect (rect
)) {
907 mediabox
= page
->mediabox
;
913 obj
= fz_new_array (state
.ctx
, 4);
914 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
915 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
916 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
917 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
918 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
921 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
922 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
923 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
924 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
927 rotate
= page
->rotate
;
928 pdf_free_page (state
.u
.pdf
, page
);
930 printd ("progress %f Trimming %d",
931 (double) (pageno
+ 1) / state
.pagecount
,
937 mediabox
= pdf_to_rect (state
.ctx
,
938 fz_dict_gets (pageobj
, "MediaBox"));
939 if (fz_is_empty_rect (mediabox
)) {
940 fprintf (stderr
, "cannot find page size for page %d\n",
948 cropbox
= pdf_to_rect (state
.ctx
,
949 fz_dict_gets (pageobj
, "CropBox"));
950 if (!fz_is_empty_rect (cropbox
)) {
951 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
953 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
961 page
= xps_load_page (state
.u
.xps
, pageno
);
962 mediabox
= xps_bound_page (state
.u
.xps
, page
);
964 if (state
.trimmargins
) {
969 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
970 dev
->hints
|= FZ_IGNORE_SHADE
;
971 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
972 fz_free_device (dev
);
974 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
975 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
976 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
977 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
978 rect
= fz_intersect_rect (rect
, mediabox
);
980 if (!fz_is_empty_rect (rect
)) {
984 xps_free_page (state
.u
.xps
, page
);
985 printd ("progress %f loading %d",
986 (double) (pageno
+ 1) / state
.pagecount
,
994 if (state
.trimmargins
) {
997 page
= cbz_load_page (state
.u
.cbz
, pageno
);
998 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
999 cbz_free_page (state
.u
.cbz
, page
);
1000 printd ("progress %f Trimming %d",
1001 (double) (pageno
+ 1) / state
.pagecount
,
1005 mediabox
.x0
= mediabox
.y0
= 0;
1013 ARSERT (0 && state
.type
);
1016 if (state
.pagedimcount
== 0
1017 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1018 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1021 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1022 state
.pagedims
= realloc (state
.pagedims
, size
);
1023 if (!state
.pagedims
) {
1024 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1025 size
, state
.pagedimcount
+ 1);
1028 p
= &state
.pagedims
[state
.pagedimcount
++];
1030 p
->mediabox
= mediabox
;
1035 if (state
.trimmargins
) {
1036 printd ("progress 1 Trimmed %d pages in %f seconds",
1037 state
.pagecount
, end
- start
);
1040 printd ("vmsg Processed %d pages in %f seconds",
1041 state
.pagecount
, end
- start
);
1046 static void layout (void)
1051 double zoom
, w
, maxw
= 0;
1052 struct pagedim
*p
= state
.pagedims
;
1054 if (state
.proportional
) {
1055 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1056 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1058 w
= box
.x1
- box
.x0
;
1059 maxw
= MAX (w
, maxw
);
1064 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1067 ctm
= fz_rotate (state
.rotate
);
1068 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1070 w
= box
.x1
- box
.x0
;
1072 if (state
.proportional
) {
1073 double scale
= w
/ maxw
;
1074 zoom
= (state
.w
/ w
) * scale
;
1080 p
->zoomctm
= fz_scale (zoom
, zoom
);
1081 ctm
= fz_concat (p
->zoomctm
, ctm
);
1083 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1084 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1085 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1088 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1091 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1095 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1096 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1097 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1103 while (p
-- != state
.pagedims
) {
1104 int w
= p
->bounds
.x1
- p
->bounds
.x0
;
1105 int h
= p
->bounds
.y1
- p
->bounds
.y0
;
1107 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1111 static void recurse_outline (fz_outline
*outline
, int level
)
1116 struct pagedim
*pdim
= state
.pagedims
;
1118 dest
= &outline
->dest
;
1119 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1120 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1122 pdim
= &state
.pagedims
[i
];
1124 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1127 p
.y
= dest
->ld
.gotor
.lt
.y
;
1128 p
= fz_transform_point (pdim
->lctm
, p
);
1131 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1135 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1136 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1138 printd ("o %d %d %d %d %s",
1139 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1141 if (outline
->down
) {
1142 recurse_outline (outline
->down
, level
+ 1);
1144 outline
= outline
->next
;
1148 static void process_outline (void)
1150 fz_outline
*outline
;
1152 if (!state
.needoutline
) return;
1154 state
.needoutline
= 0;
1155 switch (state
.type
) {
1157 outline
= pdf_load_outline (state
.u
.pdf
);
1160 outline
= xps_load_outline (state
.u
.xps
);
1167 recurse_outline (outline
, 0);
1168 fz_free_outline (state
.ctx
, outline
);
1172 static int comparespans (const void *l
, const void *r
)
1174 fz_text_span
const *const*ls
= l
;
1175 fz_text_span
const *const*rs
= r
;
1176 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
1179 /* wishful thinking function */
1180 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1188 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1189 fz_text_span
*text
, *span
, **pspan
;
1190 struct pagedim
*pdim
, *pdimprev
;
1196 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1200 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1201 if (niters
++ == 5) {
1204 printd ("progress 1 attention requested aborting search at %d",
1209 printd ("progress %f searching in page %d",
1210 (double) (pageno
+ 1) / state
.pagecount
,
1215 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1216 pdim
= &state
.pagedims
[i
];
1217 if (pdim
->pageno
== pageno
) {
1220 if (pdim
->pageno
> pageno
) {
1229 text
= fz_new_text_span (state
.ctx
);
1230 tdev
= fz_new_text_device (state
.ctx
, text
);
1232 switch (state
.type
) {
1234 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1235 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, fz_identity
, NULL
);
1239 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1240 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, fz_identity
, NULL
);
1244 ARSERT (0 && state
.type
);
1247 fz_free_device (tdev
);
1250 for (span
= text
; span
; span
= span
->next
) {
1253 pspan
= malloc (sizeof (void *) * nspans
);
1255 err (1, "malloc span pointers %" FMT_s
, sizeof (void *) * nspans
);
1257 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1260 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1262 j
= forward
? 0 : nspans
- 1;
1267 j
+= forward
? 1 : -1;
1269 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
1271 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1276 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1280 if (span
->text
[i
].c
< 256) {
1281 *p
++ = span
->text
[i
].c
;
1292 ret
= regexec (re
, buf
, 1, &rm
, 0);
1294 if (ret
!= REG_NOMATCH
) {
1297 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1298 printd ("msg regexec error `%.*s'",
1299 (int) size
, errbuf
);
1300 fz_free_text_span (state
.ctx
, text
);
1301 state
.freepage (u
.ptr
);
1308 fz_point p1
, p2
, p3
, p4
;
1310 sb
= &span
->text
[rm
.rm_so
].bbox
;
1311 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1322 switch (state
.type
) {
1324 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1325 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1333 p1
= fz_transform_point (ctm
, p1
);
1334 p2
= fz_transform_point (ctm
, p2
);
1335 p3
= fz_transform_point (ctm
, p3
);
1336 p4
= fz_transform_point (ctm
, p4
);
1339 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1346 printd ("progress 1 found at %d `%.*s' in %f sec",
1347 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1351 printd ("match %d %d %f %f %f %f %f %f %f %f",
1369 fz_free_text_span (state
.ctx
, text
);
1370 state
.freepage (u
.ptr
);
1375 printd ("progress 1 no matches %f sec", end
- start
);
1377 printd ("clearrects");
1380 static void set_tex_params (int colorspace
)
1382 switch (colorspace
) {
1384 state
.texiform
= GL_RGBA8
;
1385 state
.texform
= GL_RGBA
;
1386 state
.texty
= GL_UNSIGNED_BYTE
;
1387 state
.colorspace
= fz_device_rgb
;
1390 state
.texiform
= GL_RGBA8
;
1391 state
.texform
= GL_BGRA
;
1392 state
.texty
= fz_is_big_endian ()
1393 ? GL_UNSIGNED_INT_8_8_8_8
1394 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1395 state
.colorspace
= fz_device_bgr
;
1398 state
.texiform
= GL_LUMINANCE_ALPHA
;
1399 state
.texform
= GL_LUMINANCE_ALPHA
;
1400 state
.texty
= GL_UNSIGNED_BYTE
;
1401 state
.colorspace
= fz_device_gray
;
1404 errx (1, "invalid colorspce %d", colorspace
);
1408 static void realloctexts (int texcount
)
1412 if (texcount
== state
.texcount
) return;
1414 if (texcount
< state
.texcount
) {
1415 glDeleteTextures (state
.texcount
- texcount
,
1416 state
.texids
+ texcount
);
1419 size
= texcount
* sizeof (*state
.texids
);
1420 state
.texids
= realloc (state
.texids
, size
);
1421 if (!state
.texids
) {
1422 err (1, "realloc texids %" FMT_s
, size
);
1425 size
= texcount
* sizeof (*state
.texowners
);
1426 state
.texowners
= realloc (state
.texowners
, size
);
1427 if (!state
.texowners
) {
1428 err (1, "realloc texowners %" FMT_s
, size
);
1430 if (texcount
> state
.texcount
) {
1433 glGenTextures (texcount
- state
.texcount
,
1434 state
.texids
+ state
.texcount
);
1435 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1436 state
.texowners
[i
].w
= -1;
1437 state
.texowners
[i
].slice
= NULL
;
1440 state
.texcount
= texcount
;
1450 mainloop (void *unused
)
1453 int len
, ret
, oldlen
= 0;
1458 errx (1, "readlen returned 0");
1461 if (oldlen
< len
+ 1) {
1462 p
= realloc (p
, len
+ 1);
1464 err (1, "realloc %d failed", len
+ 1);
1471 if (!strncmp ("open", p
, 4)) {
1474 char *filename
= p
+ 5;
1476 filenamelen
= strlen (filename
);
1477 password
= filename
+ filenamelen
+ 1;
1479 openxref (filename
, password
);
1482 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1483 state
.needoutline
= 1;
1485 else if (!strncmp ("cs", p
, 2)) {
1488 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1490 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1493 set_tex_params (colorspace
);
1494 for (i
= 0; i
< state
.texcount
; ++i
) {
1495 state
.texowners
[i
].w
= -1;
1496 state
.texowners
[i
].slice
= NULL
;
1500 else if (!strncmp ("freepage", p
, 8)) {
1503 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1505 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1509 else if (!strncmp ("freetile", p
, 8)) {
1512 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1514 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1518 else if (!strncmp ("search", p
, 6)) {
1519 int icase
, pageno
, y
, ret
, len2
, forward
;
1523 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1524 &icase
, &pageno
, &y
, &forward
, &len2
);
1526 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1529 pattern
= p
+ 6 + len2
;
1530 ret
= regcomp (&re
, pattern
,
1531 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1536 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1537 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1540 search (&re
, pageno
, y
, forward
);
1544 else if (!strncmp ("geometry", p
, 8)) {
1548 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1550 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1558 for (i
= 0; i
< state
.texcount
; ++i
) {
1559 state
.texowners
[i
].slice
= NULL
;
1565 unlock ("geometry");
1566 printd ("continue %d", state
.pagecount
);
1568 else if (!strncmp ("reqlayout", p
, 9)) {
1569 int rotate
, proportional
;
1572 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1574 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1577 state
.rotate
= rotate
;
1578 state
.proportional
= proportional
;
1580 unlock ("reqlayout");
1581 printd ("continue %d", state
.pagecount
);
1583 else if (!strncmp ("page", p
, 4)) {
1586 int pageno
, pindex
, ret
;
1588 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1590 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1595 page
= loadpage (pageno
, pindex
);
1599 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1601 else if (!strncmp ("tile", p
, 4)) {
1602 int x
, y
, w
, h
, ret
;
1607 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1608 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1610 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1615 tile
= rendertile (page
, x
, y
, w
, h
);
1619 printd ("tile %d %d %" FMT_ptr
" %u %f",
1621 FMT_ptr_cast2 (tile
),
1622 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1625 else if (!strncmp ("settrim", p
, 7)) {
1629 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1630 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1632 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1636 state
.trimmargins
= trimmargins
;
1637 state
.needoutline
= 1;
1638 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1640 state
.trimfuzz
= fuzz
;
1642 state
.pagedimcount
= 0;
1643 free (state
.pagedims
);
1644 state
.pagedims
= NULL
;
1649 printd ("continue %d", state
.pagecount
);
1651 else if (!strncmp ("sliceh", p
, 6)) {
1654 ret
= sscanf (p
+ 6, " %d", &h
);
1656 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1658 if (h
!= state
.sliceheight
) {
1661 state
.sliceheight
= h
;
1662 for (i
= 0; i
< state
.texcount
; ++i
) {
1663 state
.texowners
[i
].w
= -1;
1664 state
.texowners
[i
].h
= -1;
1665 state
.texowners
[i
].slice
= NULL
;
1669 else if (!strncmp ("interrupt", p
, 9)) {
1670 printd ("vmsg interrupted");
1672 else if (!strncmp ("quit", p
, 4)) {
1676 errx (1, "unknown command %.*s", len
, p
);
1682 CAMLprim value
ml_realloctexts (value texcount_v
)
1684 CAMLparam1 (texcount_v
);
1687 if (trylock ("ml_realloctexts")) {
1691 realloctexts (Int_val (texcount_v
));
1693 unlock ("ml_realloctexts");
1696 CAMLreturn (Val_bool (ok
));
1699 static void showsel (struct page
*page
, int ox
, int oy
)
1703 struct mark first
, last
;
1705 first
= page
->fmark
;
1708 if (!first
.span
|| !last
.span
) return;
1710 glEnable (GL_BLEND
);
1711 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1712 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1714 ox
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1715 oy
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1716 for (span
= first
.span
; span
; span
= span
->next
) {
1719 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1724 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1725 j
= MIN (first
.i
, last
.i
);
1726 k
= MAX (first
.i
, last
.i
);
1728 else if (span
== first
.span
) {
1731 else if (span
== last
.span
) {
1735 for (i
= j
; i
<= k
; ++i
) {
1736 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1738 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1745 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1747 if (span
== last
.span
) break;
1749 glDisable (GL_BLEND
);
1752 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1755 fz_link
*link
, *links
;
1757 switch (page
->type
) {
1759 links
= page
->u
.pdfpage
->links
;
1763 links
= page
->u
.xpspage
->links
;
1770 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1771 glEnable (GL_LINE_STIPPLE
);
1772 glLineStipple (0.5, 0xcccc);
1774 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1777 for (link
= links
; link
; link
= link
->next
) {
1778 fz_point p1
, p2
, p3
, p4
;
1780 p1
.x
= link
->rect
.x0
;
1781 p1
.y
= link
->rect
.y0
;
1783 p2
.x
= link
->rect
.x1
;
1784 p2
.y
= link
->rect
.y0
;
1786 p3
.x
= link
->rect
.x1
;
1787 p3
.y
= link
->rect
.y1
;
1789 p4
.x
= link
->rect
.x0
;
1790 p4
.y
= link
->rect
.y1
;
1792 p1
= fz_transform_point (ctm
, p1
);
1793 p2
= fz_transform_point (ctm
, p2
);
1794 p3
= fz_transform_point (ctm
, p3
);
1795 p4
= fz_transform_point (ctm
, p4
);
1797 switch (link
->dest
.kind
) {
1798 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1799 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1800 default: glColor3ub (0, 0, 0); break;
1803 glVertex2f (p1
.x
, p1
.y
);
1804 glVertex2f (p2
.x
, p2
.y
);
1805 glVertex2f (p3
.x
, p3
.y
);
1806 glVertex2f (p4
.x
, p4
.y
);
1810 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1811 glDisable (GL_LINE_STIPPLE
);
1814 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1817 struct slice
*slice1
;
1820 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1821 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1823 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
1824 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1825 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1829 int texindex
= state
.texindex
++ % state
.texcount
;
1831 if (state
.texowners
[texindex
].w
== tile
->w
) {
1832 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1836 state
.texowners
[texindex
].h
= slice
->h
;
1840 state
.texowners
[texindex
].h
= slice
->h
;
1843 state
.texowners
[texindex
].w
= tile
->w
;
1844 state
.texowners
[texindex
].slice
= slice
;
1845 slice
->texindex
= texindex
;
1847 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1849 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1857 tile
->pixmap
->samples
+offset
1861 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1869 tile
->pixmap
->samples
+offset
1875 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1877 CAMLparam2 (args_v
, ptr_v
);
1878 int dispx
= Int_val (Field (args_v
, 0));
1879 int dispy
= Int_val (Field (args_v
, 1));
1880 int dispw
= Int_val (Field (args_v
, 2));
1881 int disph
= Int_val (Field (args_v
, 3));
1882 int tilex
= Int_val (Field (args_v
, 4));
1883 int tiley
= Int_val (Field (args_v
, 5));
1884 char *s
= String_val (ptr_v
);
1885 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1887 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1889 int slicey
, firstslice
;
1890 struct slice
*slice
;
1892 firstslice
= tiley
/ tile
->sliceheight
;
1893 slice
= &tile
->slices
[firstslice
];
1894 slicey
= tiley
% tile
->sliceheight
;
1899 dh
= slice
->h
- slicey
;
1900 dh
= MIN (disph
, dh
);
1901 uploadslice (tile
, slice
);
1905 glTexCoord2i (tilex
, slicey
);
1906 glVertex2i (dispx
, dispy
);
1908 glTexCoord2i (tilex
+dispw
, slicey
);
1909 glVertex2i (dispx
+dispw
, dispy
);
1911 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1912 glVertex2i (dispx
+dispw
, dispy
+dh
);
1914 glTexCoord2i (tilex
, slicey
+dh
);
1915 glVertex2i (dispx
, dispy
+dh
);
1922 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1926 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1927 CAMLreturn (Val_unit
);
1930 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1931 value xoff_v
, value yoff_v
)
1933 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1934 int xoff
= Int_val (xoff_v
);
1935 int yoff
= Int_val (yoff_v
);
1936 char *s
= String_val (ptr_v
);
1937 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1939 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1941 if (trylock ("ml_postprocess")) {
1944 showsel (page
, xoff
, yoff
);
1945 unlock ("ml_postprocess");
1948 CAMLreturn (Val_unit
);
1951 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1955 fz_link
*link
, *links
;
1957 switch (page
->type
) {
1959 links
= page
->u
.pdfpage
->links
;
1963 links
= page
->u
.xpspage
->links
;
1972 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1973 state
.pagedims
[page
->pdimno
].ctm
);
1974 ctm
= fz_invert_matrix (ctm
);
1975 p
= fz_transform_point (ctm
, p
);
1977 for (link
= links
; link
; link
= link
->next
) {
1978 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1979 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1987 static void droptext (struct page
*page
)
1990 fz_free_text_span (state
.ctx
, page
->text
);
1993 page
->fmark
.span
= NULL
;
1994 page
->lmark
.span
= NULL
;
1999 static void ensuretext (struct page
*page
)
2001 if (state
.gen
!= page
->gen
) {
2003 page
->gen
= state
.gen
;
2008 page
->text
= fz_new_text_span (state
.ctx
);
2009 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
2010 fz_run_display_list (page
->dlist
,
2013 fz_infinite_bbox
, NULL
);
2014 fz_free_device (tdev
);
2018 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2020 CAMLparam3 (ptr_v
, x_v
, y_v
);
2021 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2025 char *s
= String_val (ptr_v
);
2026 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2027 struct pagedim
*pdim
;
2029 ret_v
= Val_int (0);
2030 if (trylock ("ml_whatsunder")) {
2034 page
= parse_pointer ("ml_whatsunder", s
);
2035 pdim
= &state
.pagedims
[page
->pdimno
];
2036 x
+= pdim
->bounds
.x0
;
2037 y
+= pdim
->bounds
.y0
;
2038 link
= getlink (page
, x
, y
);
2040 switch (link
->dest
.kind
) {
2045 pageno
= link
->dest
.ld
.gotor
.page
;
2049 if (link
->dest
.ld
.gotor
.flags
& fz_link_flag_t_valid
) {
2050 p
.y
= link
->dest
.ld
.gotor
.lt
.y
;
2051 p
= fz_transform_point (pdim
->lctm
, p
);
2053 tup_v
= caml_alloc_tuple (2);
2054 ret_v
= caml_alloc_small (1, 1);
2055 Field (tup_v
, 0) = Val_int (pageno
);
2056 Field (tup_v
, 1) = Val_int (p
.y
);
2057 Field (ret_v
, 0) = tup_v
;
2062 str_v
= caml_copy_string (link
->dest
.ld
.uri
.uri
);
2063 ret_v
= caml_alloc_small (1, 0);
2064 Field (ret_v
, 0) = str_v
;
2067 case FZ_LINK_LAUNCH
:
2068 str_v
= caml_copy_string (link
->dest
.ld
.launch
.file_spec
);
2069 ret_v
= caml_alloc_small (1, 4);
2070 Field (ret_v
, 0) = str_v
;
2074 str_v
= caml_copy_string (link
->dest
.ld
.named
.named
);
2075 ret_v
= caml_alloc_small (1, 5);
2076 Field (ret_v
, 0) = str_v
;
2080 str_v
= caml_copy_string (link
->dest
.ld
.gotor
.file_spec
);
2081 pageno
= link
->dest
.ld
.gotor
.page
;
2082 tup_v
= caml_alloc_tuple (2);
2083 ret_v
= caml_alloc_small (1, 6);
2084 Field (tup_v
, 0) = str_v
;
2085 Field (tup_v
, 1) = Val_int (pageno
);
2086 Field (ret_v
, 0) = tup_v
;
2093 snprintf (buf
, sizeof (buf
),
2094 "unhandled link kind %d", link
->dest
.kind
);
2095 str_v
= caml_copy_string (buf
);
2096 ret_v
= caml_alloc_small (1, 3);
2097 Field (ret_v
, 0) = str_v
;
2107 for (span
= page
->text
; span
; span
= span
->next
) {
2108 for (i
= 0; i
< span
->len
; ++i
) {
2110 b
= &span
->text
[i
].bbox
;
2111 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
2113 span
->font
&& span
->font
->name
2115 : "Span has no font name"
2117 FT_FaceRec
*face
= span
->font
->ft_face
;
2118 if (face
&& face
->family_name
) {
2120 char *n1
= face
->family_name
;
2121 size_t l1
= strlen (n1
);
2122 size_t l2
= strlen (n2
);
2124 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2125 s
= malloc (l1
+ l2
+ 2);
2129 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2130 str_v
= caml_copy_string (s
);
2136 str_v
= caml_copy_string (n2
);
2138 ret_v
= caml_alloc_small (1, 2);
2139 Field (ret_v
, 0) = str_v
;
2146 unlock ("ml_whatsunder");
2152 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2154 CAMLparam2 (ptr_v
, rect_v
);
2158 struct mark first
, last
;
2159 int i
, x0
, x1
, y0
, y1
;
2160 struct pagedim
*pdim
;
2161 char *s
= String_val (ptr_v
);
2163 if (trylock ("ml_seltext")) {
2167 page
= parse_pointer ("ml_seltext", s
);
2170 pdim
= &state
.pagedims
[page
->pdimno
];
2172 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
2173 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2174 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2175 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2178 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2179 glColor3ub (128, 128, 128);
2180 glRecti (x0
, y0
, x1
, y1
);
2181 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2187 last
.i
= first
.i
= 0;
2188 first
.span
= page
->text
;
2189 for (span
= page
->text
; span
; span
= span
->next
) {
2190 for (i
= 0; i
< span
->len
; ++i
) {
2191 b
= &span
->text
[i
].bbox
;
2194 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
2199 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
2204 if (0 && selected
) {
2205 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2206 glColor3ub (128, 128, 128);
2207 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2208 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2213 if (y1
< y0
|| x1
< x0
) {
2216 if (first
.span
== last
.span
) {
2221 for (span
= first
.span
; span
&& span
!= last
.span
;
2222 span
= span
->next
) {
2235 first
.span
= last
.span
;
2241 page
->fmark
= first
;
2244 unlock ("ml_seltext");
2247 CAMLreturn (Val_unit
);
2250 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2255 for (i
= a
; i
<= b
; ++i
) {
2256 len
= runetochar (buf
, &span
->text
[i
].c
);
2257 ret
= fwrite (buf
, len
, 1, f
);
2260 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2261 len
, ret
, strerror (errno
));
2268 CAMLprim value
ml_copysel (value command_v
, value ptr_v
)
2274 char *s
= String_val (ptr_v
);
2275 char *command
= String_val (command_v
);
2277 if (trylock ("ml_copysel")) {
2282 page
= parse_pointer ("ml_sopysel", s
);
2284 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2285 fprintf (stderr
, "nothing to copy\n");
2289 f
= popen (command
, "w");
2291 fprintf (stderr
, "failed to open sel pipe: %s\n",
2296 for (span
= page
->fmark
.span
;
2297 span
&& span
!= page
->lmark
.span
->next
;
2298 span
= span
->next
) {
2299 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2300 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2301 if (pipespan (f
, span
, a
, b
)) {
2305 if (putc ('\n', f
) == EOF
) {
2306 fprintf (stderr
, "failed break line on sel pipe: %s\n",
2312 page
->lmark
.span
= NULL
;
2313 page
->fmark
.span
= NULL
;
2317 int ret
= pclose (f
);
2319 if (errno
!= ECHILD
) {
2320 fprintf (stderr
, "failed to close sel pipe: %s\n",
2326 unlock ("ml_copysel");
2329 CAMLreturn (Val_unit
);
2332 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2334 CAMLparam1 (pagedimno_v
);
2336 int pagedimno
= Int_val (pagedimno_v
);
2339 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2340 if (trylock ("ml_getpdimrect")) {
2341 box
= fz_empty_rect
;
2344 box
= state
.pagedims
[pagedimno
].mediabox
;
2345 unlock ("ml_getpdimrect");
2348 Store_double_field (ret_v
, 0, box
.x0
);
2349 Store_double_field (ret_v
, 1, box
.x1
);
2350 Store_double_field (ret_v
, 2, box
.y0
);
2351 Store_double_field (ret_v
, 3, box
.y1
);
2356 static double getmaxw (void)
2362 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2365 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2366 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2369 maxw
= MAX (w
, maxw
);
2374 CAMLprim value
ml_getmaxw (value unit_v
)
2376 CAMLparam1 (unit_v
);
2380 if (trylock ("ml_getmaxw")) {
2384 unlock ("ml_getmaxw");
2386 ret_v
= caml_copy_double (maxw
);
2390 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2392 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2396 double maxw
= 0.0, maxh
= 0.0;
2398 double winw
= Int_val (winw_v
);
2399 double winh
= Int_val (winh_v
);
2400 double dw
= Int_val (dw_v
);
2401 double pw
= 1.0, ph
= 1.0, num
, den
;
2403 if (trylock ("ml_zoom_for_height")) {
2407 if (state
.proportional
) {
2411 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2412 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2414 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2415 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2416 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2417 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2422 if (state
.proportional
) {
2424 scaledh
= h
* scale
;
2431 if (scaledh
> maxh
) {
2438 num
= (winh
* pw
) + (ph
* dw
);
2442 unlock ("ml_zoom_for_height");
2444 ret_v
= caml_copy_double (zoom
);
2450 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2452 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2454 int pt
= Int_val(pt_v
);
2455 int x
= Int_val (x_v
);
2456 int y
= Int_val (y_v
);
2459 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2460 ret_v
= caml_copy_double (w
);
2464 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2466 CAMLparam2 (pt_v
, string_v
);
2468 int pt
= Int_val (pt_v
);
2471 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2472 ret_v
= caml_copy_double (w
);
2476 CAMLprim value
ml_getpagebox (value opaque_v
)
2478 CAMLparam1 (opaque_v
);
2482 char *s
= String_val (opaque_v
);
2483 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2485 ret_v
= caml_alloc_tuple (4);
2486 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2487 dev
->hints
|= FZ_IGNORE_SHADE
;
2489 switch (page
->type
) {
2491 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
2495 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
2499 bbox
= fz_infinite_bbox
;
2503 fz_free_device (dev
);
2504 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2505 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2506 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2507 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2512 CAMLprim value
ml_setaalevel (value level_v
)
2514 CAMLparam1 (level_v
);
2516 state
.aalevel
= Int_val (level_v
);
2517 CAMLreturn (Val_unit
);
2520 #if !defined _WIN32 && !defined __APPLE__
2523 #include <X11/Xlib.h>
2524 #include <X11/Xutil.h>
2527 static void set_wm_class (int hack
)
2535 display
= getenv ("DISPLAY");
2536 dpy
= XOpenDisplay (display
);
2538 fprintf (stderr
, "XOpenDisplay `%s' failed\n",
2539 display
? display
: "null");
2542 hint
.res_name
= "llpp";
2543 hint
.res_class
= "llpp";
2544 win
= glXGetCurrentDrawable ();
2546 fprintf (stderr
, "glXGetCurrentDrawable returned None\n");
2547 XCloseDisplay (dpy
);
2550 XSetClassHint (dpy
, win
, &hint
);
2551 XCloseDisplay (dpy
);
2555 #define set_wm_class(h) (void) (h)
2558 enum { piunknown
, pilinux
, piwindows
, piosx
,
2559 pisun
, pifreebsd
, pidragonflybsd
,
2560 piopenbsd
, pinetbsd
, pimingw
, picygwin
};
2562 #define NOZOMBIESPLEASE
2564 CAMLprim value
ml_platform (value unit_v
)
2566 CAMLparam1 (unit_v
);
2567 int platid
= piunknown
;
2569 #if defined __linux__
2571 #elif defined __CYGWIN__
2572 #undef NOZOMBIESPLEASE
2574 #elif defined __MINGW32__
2575 #undef NOZOMBIESPLEASE
2577 #elif defined _WIN32
2578 #undef NOZOMBIESPLEASE
2580 #elif defined __DragonFly__
2581 platid
= pidragonflybsd
;
2582 #elif defined __FreeBSD__
2584 #elif defined __OpenBSD__
2586 #elif defined __NetBSD__
2588 #elif defined __sun__
2590 #elif defined __APPLE__
2593 CAMLreturn (Val_int (platid
));
2596 #ifdef NOZOMBIESPLEASE
2599 static void nozombies (void)
2601 struct sigaction sa
;
2603 sa
.sa_handler
= SIG_DFL
;
2604 if (sigemptyset (&sa
.sa_mask
)) {
2605 err (1, "sigemptyset");
2607 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
2608 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
2609 err (1, "sigaction");
2616 CAMLprim value
ml_init (value pipe_v
, value params_v
)
2618 CAMLparam2 (pipe_v
, params_v
);
2619 CAMLlocal2 (trim_v
, fuzz_v
);
2627 state
.cr
= Handle_val (Field (pipe_v
, 0));
2628 state
.cw
= Handle_val (Field (pipe_v
, 1));
2630 state
.cr
= Int_val (Field (pipe_v
, 0));
2631 state
.cw
= Int_val (Field (pipe_v
, 1));
2633 state
.rotate
= Int_val (Field (params_v
, 0));
2634 state
.proportional
= Bool_val (Field (params_v
, 1));
2635 trim_v
= Field (params_v
, 2);
2636 texcount
= Int_val (Field (params_v
, 3));
2637 state
.sliceheight
= Int_val (Field (params_v
, 4));
2638 mustoresize
= Int_val (Field (params_v
, 5));
2639 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
2640 colorspace
= Int_val (Field (params_v
, 6));
2641 wmclasshack
= Bool_val (Field (params_v
, 7));
2642 fontpath
= String_val (Field (params_v
, 8));
2644 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2645 fuzz_v
= Field (trim_v
, 1);
2646 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2647 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2648 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2649 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2651 set_tex_params (colorspace
);
2652 set_wm_class (wmclasshack
);
2655 state
.face
= load_font (fontpath
);
2659 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2661 state
.face
= load_builtin_font (base
, len
);
2663 if (!state
.face
) _exit (1);
2665 realloctexts (texcount
);
2670 InitializeCriticalSection (&critsec
);
2671 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2672 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2673 errx (1, "CreateThread failed: %lx", GetLastError ());
2677 int ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2679 errx (1, "pthread_create: %s", strerror (ret
));
2684 CAMLreturn (Val_unit
);