1 /* lots of code c&p-ed directly from mupdf */
11 #pragma warning (disable:4244)
12 #pragma warning (disable:4996)
13 #pragma warning (disable:4995)
14 #define NORETURN __declspec (noreturn)
17 #define GCC_FMT_ATTR(a, b)
18 #elif defined __GNUC__
19 #define NORETURN __attribute__ ((noreturn))
20 #define UNUSED __attribute__ ((unused))
21 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
22 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
27 #define GCC_FMT_ATTR(a, b)
31 #define WIN32_LEAN_AND_MEAN
38 static void NORETURN
GCC_FMT_ATTR (2, 3)
39 winerr (int exitcode
, const char *fmt
, ...)
42 DWORD savederror
= GetLastError ();
45 vfprintf (stderr
, fmt
, ap
);
47 fprintf (stderr
, ": 0x%lx\n", savederror
);
56 #include <sys/types.h>
57 #include <sys/ioctl.h>
61 /* some versions of MingW have non idempotent %p */
63 #define FMT_ptr PRIxPTR
64 #define FMT_ptr_cast(p) ((intptr_t *) (p))
65 #define FMT_ptr_cast2(p) ((intptr_t) (p))
68 #define FMT_ptr_cast(p) (p)
69 #define FMT_ptr_cast2(p) (p)
77 static void NORETURN
GCC_FMT_ATTR (2, 3)
78 err (int exitcode
, const char *fmt
, ...)
85 vfprintf (stderr
, fmt
, ap
);
87 fprintf (stderr
, ": %s\n", strerror (savederrno
));
92 static void NORETURN
GCC_FMT_ATTR (2, 3)
93 errx (int exitcode
, const char *fmt
, ...)
98 vfprintf (stderr
, fmt
, ap
);
100 fputc ('\n', stderr
);
106 #include <OpenGL/gl.h>
111 #ifndef GL_TEXTURE_RECTANGLE_ARB
112 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
116 #define GL_BGRA 0x80E1
119 #ifndef GL_UNSIGNED_INT_8_8_8_8
120 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
123 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
124 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
127 #include <caml/fail.h>
128 #include <caml/alloc.h>
129 #include <caml/memory.h>
130 #include <caml/unixsupport.h>
137 #include FT_FREETYPE_H
140 #define lprintf printf
145 #define ARSERT(cond) for (;;) { \
147 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
162 struct slice slices
[1];
173 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
176 enum { DPDF
, DXPS
, DCBZ
};
190 fz_display_list
*dlist
;
195 void (*freepage
) (void *);
201 struct pagedim
*pagedims
;
210 fz_glyph_cache
*cache
;
221 fz_colorspace
*colorspace
;
248 void (*closedoc
) (void);
249 void (*freepage
) (void *);
252 static void UNUSED
debug_rect (const char *cap
, fz_rect r
)
254 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
257 static void UNUSED
debug_bbox (const char *cap
, fz_bbox r
)
259 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
262 static void UNUSED
debug_matrix (const char *cap
, fz_matrix m
)
264 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
265 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
269 static CRITICAL_SECTION critsec
;
271 static void lock (void *unused
)
274 EnterCriticalSection (&critsec
);
277 static void unlock (void *unused
)
280 LeaveCriticalSection (&critsec
);
283 static int trylock (void *unused
)
285 return TryEnterCriticalSection (&critsec
) == 0;
288 CAMLprim value
ml_seterrhandle (value is_gui_v
, value handle_v
)
290 /* http://stackoverflow.com/questions/5115569/c-win32-api-getstdhandlestd-output-handle-is-invalid-very-perplexing */
291 CAMLparam2 (is_gui_v
, handle_v
);
294 if (!SetStdHandle (STD_ERROR_HANDLE
, Handle_val (handle_v
))) {
295 win32_maperr (GetLastError ());
296 uerror ("SetStdHandle", Nothing
);
299 if (Bool_val (is_gui_v
)) {
303 fd
= _open_osfhandle ((intptr_t) Handle_val (handle_v
), 0);
305 uerror ("_open_osfhandle", Nothing
);
307 *stderr
= *_fdopen (fd
, "w");
310 uerror ("_open_osfhandle", Nothing
);
312 if (setvbuf (stderr
, NULL
, _IONBF
, 0)) {
313 uerror ("stvbuf", Nothing
);
316 CAMLreturn (Val_unit
);
319 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
321 static void lock (const char *cap
)
323 int ret
= pthread_mutex_lock (&mutex
);
325 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
329 static void unlock (const char *cap
)
331 int ret
= pthread_mutex_unlock (&mutex
);
333 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
337 static int trylock (const char *cap
)
339 int ret
= pthread_mutex_trylock (&mutex
);
341 if (ret
&& ret
!= EBUSY
) {
342 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
347 CAMLprim value
ml_seterrhandle (value is_gui_v
, value handle_v
)
349 CAMLparam2 (is_gui_v
, handle_v
);
352 CAMLreturn (Val_unit
);
356 static void *parse_pointer (const char *cap
, const char *s
)
361 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
363 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
369 static double now (void)
374 GetSystemTimeAsFileTime (&ft
);
375 tmp
= ft
.dwHighDateTime
;
377 tmp
|= ft
.dwLowDateTime
;
381 static int hasdata (void)
386 okay
= PeekNamedPipe (state
.cr
, NULL
, 0, NULL
, &avail
, NULL
);
387 if (!okay
) winerr (1, "PeekNamedPipe");
391 static void readdata (void *p
, int size
)
396 okay
= ReadFile (state
.cr
, p
, size
, &nread
, NULL
);
397 if (!okay
|| nread
- size
) {
398 err (1, "ReadFile (req %d, okay %d, ret %ld)", size
, okay
, nread
);
402 static void writedata (char *p
, int size
)
408 buf
[0] = (size
>> 24) & 0xff;
409 buf
[1] = (size
>> 16) & 0xff;
410 buf
[2] = (size
>> 8) & 0xff;
411 buf
[3] = (size
>> 0) & 0xff;
413 okay
= WriteFile (state
.cw
, buf
, 4, &nwritten
, NULL
);
414 if (!okay
|| nwritten
!= 4) {
415 winerr (1, "WriteFile okay %d ret %ld", okay
, nwritten
);
418 okay
= WriteFile (state
.cw
, p
, size
, &nwritten
, NULL
);
419 if (!okay
|| nwritten
- size
) {
420 winerr (1, "WriteFile (req %d, okay %d, ret %ld)",
421 size
, okay
, nwritten
);
425 static double now (void)
429 if (gettimeofday (&tv
, NULL
)) {
430 err (1, "gettimeofday");
432 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
435 static int hasdata (void)
438 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
439 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
443 static void readdata (void *p
, int size
)
447 n
= read (state
.cr
, p
, size
);
449 if (!n
) errx (1, "EOF while reading");
450 err (1, "read (req %d, ret %zd)", size
, n
);
454 static void writedata (char *p
, int size
)
459 buf
[0] = (size
>> 24) & 0xff;
460 buf
[1] = (size
>> 16) & 0xff;
461 buf
[2] = (size
>> 8) & 0xff;
462 buf
[3] = (size
>> 0) & 0xff;
464 n
= write (state
.cw
, buf
, 4);
466 if (!n
) errx (1, "EOF while writing length");
467 err (1, "write %zd", n
);
470 n
= write (state
.cw
, p
, size
);
472 if (!n
) errx (1, "EOF while writing data");
473 err (1, "write (req %d, ret %zd)", size
, n
);
478 static int readlen (void)
483 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
486 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
494 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
497 len
= vsnprintf (buf
, size
, fmt
, ap
);
500 if (len
> -1 && len
< size
) {
501 writedata (buf
, len
);
511 buf
= realloc (buf
, size
);
516 static void closepdf (void)
519 pdf_close_document (state
.u
.pdf
);
524 static void closexps (void)
527 xps_close_document (state
.u
.xps
);
532 static void closecbz (void)
535 cbz_close_document (state
.u
.cbz
);
540 static void freepdfpage (void *ptr
)
542 pdf_free_page (state
.u
.pdf
, ptr
);
545 static void freexpspage (void *ptr
)
547 xps_free_page (state
.u
.xps
, ptr
);
550 static void freecbzpage (void *ptr
)
552 cbz_free_page (state
.u
.cbz
, ptr
);
555 static void openxref (char *filename
, char *password
)
559 for (i
= 0; i
< state
.texcount
; ++i
) {
560 state
.texowners
[i
].w
= -1;
561 state
.texowners
[i
].slice
= NULL
;
564 if (state
.closedoc
) state
.closedoc ();
566 len
= strlen (filename
);
572 ext
[0] = tolower (filename
[len
-3]);
573 ext
[1] = tolower (filename
[len
-2]);
574 ext
[2] = tolower (filename
[len
-1]);
576 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
579 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
584 if (state
.pagedims
) {
585 free (state
.pagedims
);
586 state
.pagedims
= NULL
;
588 state
.pagedimcount
= 0;
590 fz_set_aa_level (state
.ctx
, state
.aalevel
);
591 switch (state
.type
) {
593 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
594 if (pdf_needs_password (state
.u
.pdf
)) {
595 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
597 errx (1, "invalid password");
600 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
601 state
.closedoc
= closepdf
;
602 state
.freepage
= freepdfpage
;
606 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
607 state
.pagecount
= xps_count_pages (state
.u
.xps
);
608 state
.closedoc
= closexps
;
609 state
.freepage
= freexpspage
;
613 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
614 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
615 state
.closedoc
= closecbz
;
616 state
.freepage
= freecbzpage
;
621 static void pdfinfo (void)
623 if (state
.type
== DPDF
) {
626 printd ("info PDF version\t%d.%d",
627 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
629 infoobj
= fz_dict_gets (state
.u
.pdf
->trailer
, "Info");
633 char *items
[] = { "Title", "Author", "Creator",
634 "Producer", "CreationDate" };
636 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
637 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
638 s
= pdf_to_utf8 (state
.ctx
, obj
);
641 printd ("title %s", s
);
643 printd ("info %s\t%s", items
[i
], s
);
645 fz_free (state
.ctx
, s
);
652 static void unlinktile (struct tile
*tile
)
656 for (i
= 0; i
< tile
->slicecount
; ++i
) {
657 struct slice
*s
= &tile
->slices
[i
];
659 if (s
->texindex
!= -1) {
660 if (state
.texowners
[s
->texindex
].slice
== s
) {
661 state
.texowners
[s
->texindex
].slice
= NULL
;
667 static void freepage (struct page
*page
)
670 fz_free_text_span (state
.ctx
, page
->text
);
672 page
->freepage (page
->u
.ptr
);
673 fz_free_display_list (state
.ctx
, page
->dlist
);
677 static void freetile (struct tile
*tile
)
681 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
684 fz_drop_pixmap (state
.ctx
, state
.pig
);
686 state
.pig
= tile
->pixmap
;
694 static int cacheline32bytes
;
695 extern char **environ
;
697 static void __attribute__ ((constructor
)) clcheck (void)
699 char **envp
= environ
;
704 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
706 cacheline32bytes
= auxv
[1] == 32;
712 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
714 if (cacheline32bytes
) {
715 intptr_t a1
, a2
, diff
;
716 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
717 vector
unsigned char v
= vec_splat_u8 (-1);
718 vector
unsigned char *p
;
720 a1
= a2
= (intptr_t) pixmap
->samples
;
721 a2
= (a1
+ 31) & ~31;
726 while (a1
!= a2
) *(char *) a1
++ = 0xff;
727 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
728 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
730 vec_st (v
, i
+ 16, p
);
732 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
734 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
737 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
740 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
743 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
745 if (!pdim
->tctmready
) {
746 if (state
.trimmargins
) {
749 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
750 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
751 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
752 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
763 static fz_matrix
pagectm (struct page
*page
)
765 if (page
->type
== DPDF
) {
766 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
767 state
.pagedims
[page
->pdimno
].ctm
);
771 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
773 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
774 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
775 -pdim
->mediabox
.y0
), ctm
);
780 static void *loadpage (int pageno
, int pindex
)
783 struct page
*page
= NULL
;
785 page
= calloc (sizeof (struct page
), 1);
787 err (1, "calloc page %d", pageno
);
790 page
->dlist
= fz_new_display_list (state
.ctx
);
791 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
792 switch (state
.type
) {
794 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
795 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
796 page
->freepage
= freepdfpage
;
800 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
801 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
802 page
->freepage
= freexpspage
;
806 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
807 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
808 page
->freepage
= freecbzpage
;
811 fz_free_device (dev
);
813 page
->pdimno
= pindex
;
814 page
->pageno
= pageno
;
815 page
->gen
= state
.gen
;
816 page
->type
= state
.type
;
821 static struct tile
*alloctile (int h
)
828 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
829 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
830 tile
= calloc (tilesize
, 1);
832 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
834 for (i
= 0; i
< slicecount
; ++i
) {
835 int sh
= MIN (h
, state
.sliceheight
);
836 tile
->slices
[i
].h
= sh
;
837 tile
->slices
[i
].texindex
= -1;
840 tile
->slicecount
= slicecount
;
841 tile
->sliceheight
= state
.sliceheight
;
845 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
850 struct pagedim
*pdim
;
852 tile
= alloctile (h
);
853 pdim
= &state
.pagedims
[page
->pdimno
];
858 bbox
.x1
= bbox
.x0
+ w
;
859 bbox
.y1
= bbox
.y0
+ h
;
862 if (state
.pig
->w
== w
864 && state
.pig
->colorspace
== state
.colorspace
) {
865 tile
->pixmap
= state
.pig
;
866 tile
->pixmap
->x
= bbox
.x0
;
867 tile
->pixmap
->y
= bbox
.y0
;
870 fz_drop_pixmap (state
.ctx
, state
.pig
);
876 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
881 clearpixmap (tile
->pixmap
);
882 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
883 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
884 fz_free_device (dev
);
889 static void initpdims (void)
895 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
901 switch (state
.type
) {
903 pageobj
= state
.u
.pdf
->page_objs
[pageno
];
905 if (state
.trimmargins
) {
909 page
= pdf_load_page (state
.u
.pdf
, pageno
);
910 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
911 if (state
.trimanew
|| !obj
) {
917 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
918 dev
->hints
|= FZ_IGNORE_SHADE
;
919 ctm
= fz_invert_matrix (page
->ctm
);
920 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
921 fz_free_device (dev
);
923 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
924 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
925 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
926 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
927 rect
= fz_transform_rect (ctm
, rect
);
928 rect
= fz_intersect_rect (rect
, page
->mediabox
);
930 if (fz_is_empty_rect (rect
)) {
931 mediabox
= page
->mediabox
;
937 obj
= fz_new_array (state
.ctx
, 4);
938 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
939 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
940 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
941 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
942 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
945 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
946 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
947 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
948 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
951 rotate
= page
->rotate
;
952 pdf_free_page (state
.u
.pdf
, page
);
954 printd ("progress %f Trimming %d",
955 (double) (pageno
+ 1) / state
.pagecount
,
961 mediabox
= pdf_to_rect (state
.ctx
,
962 fz_dict_gets (pageobj
, "MediaBox"));
963 if (fz_is_empty_rect (mediabox
)) {
964 fprintf (stderr
, "cannot find page size for page %d\n",
972 cropbox
= pdf_to_rect (state
.ctx
,
973 fz_dict_gets (pageobj
, "CropBox"));
974 if (!fz_is_empty_rect (cropbox
)) {
975 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
977 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
985 page
= xps_load_page (state
.u
.xps
, pageno
);
986 mediabox
= xps_bound_page (state
.u
.xps
, page
);
988 if (state
.trimmargins
) {
993 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
994 dev
->hints
|= FZ_IGNORE_SHADE
;
995 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
996 fz_free_device (dev
);
998 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
999 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
1000 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
1001 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
1002 rect
= fz_intersect_rect (rect
, mediabox
);
1004 if (!fz_is_empty_rect (rect
)) {
1008 xps_free_page (state
.u
.xps
, page
);
1009 printd ("progress %f loading %d",
1010 (double) (pageno
+ 1) / state
.pagecount
,
1018 if (state
.trimmargins
) {
1021 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1022 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
1023 cbz_free_page (state
.u
.cbz
, page
);
1024 printd ("progress %f Trimming %d",
1025 (double) (pageno
+ 1) / state
.pagecount
,
1029 mediabox
.x0
= mediabox
.y0
= 0;
1037 ARSERT (0 && state
.type
);
1040 if (state
.pagedimcount
== 0
1041 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1042 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1045 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1046 state
.pagedims
= realloc (state
.pagedims
, size
);
1047 if (!state
.pagedims
) {
1048 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1049 size
, state
.pagedimcount
+ 1);
1052 p
= &state
.pagedims
[state
.pagedimcount
++];
1054 p
->mediabox
= mediabox
;
1059 if (state
.trimmargins
) {
1060 printd ("progress 1 Trimmed %d pages in %f seconds",
1061 state
.pagecount
, end
- start
);
1064 printd ("vmsg Processed %d pages in %f seconds",
1065 state
.pagecount
, end
- start
);
1070 static void layout (void)
1075 double zoom
, w
, maxw
= 0;
1076 struct pagedim
*p
= state
.pagedims
;
1078 if (state
.proportional
) {
1079 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1080 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1082 w
= box
.x1
- box
.x0
;
1083 maxw
= MAX (w
, maxw
);
1088 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1091 ctm
= fz_rotate (state
.rotate
);
1092 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1094 w
= box
.x1
- box
.x0
;
1096 if (state
.proportional
) {
1097 double scale
= w
/ maxw
;
1098 zoom
= (state
.w
/ w
) * scale
;
1104 p
->zoomctm
= fz_scale (zoom
, zoom
);
1105 ctm
= fz_concat (p
->zoomctm
, ctm
);
1107 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1108 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1109 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1112 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1115 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1119 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1120 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1121 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1127 while (p
-- != state
.pagedims
) {
1128 int w
= p
->bounds
.x1
- p
->bounds
.x0
;
1129 int h
= p
->bounds
.y1
- p
->bounds
.y0
;
1131 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1135 static void recurse_outline (fz_outline
*outline
, int level
)
1140 struct pagedim
*pdim
= state
.pagedims
;
1142 dest
= &outline
->dest
;
1143 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1144 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1146 pdim
= &state
.pagedims
[i
];
1148 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1151 p
.y
= dest
->ld
.gotor
.lt
.y
;
1152 p
= fz_transform_point (pdim
->lctm
, p
);
1155 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1159 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1160 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1162 printd ("o %d %d %d %d %s",
1163 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1165 if (outline
->down
) {
1166 recurse_outline (outline
->down
, level
+ 1);
1168 outline
= outline
->next
;
1172 static void process_outline (void)
1174 fz_outline
*outline
;
1176 if (!state
.needoutline
) return;
1178 state
.needoutline
= 0;
1179 switch (state
.type
) {
1181 outline
= pdf_load_outline (state
.u
.pdf
);
1184 outline
= xps_load_outline (state
.u
.xps
);
1191 recurse_outline (outline
, 0);
1192 fz_free_outline (state
.ctx
, outline
);
1196 static int comparespans (const void *l
, const void *r
)
1198 fz_text_span
const *const*ls
= l
;
1199 fz_text_span
const *const*rs
= r
;
1200 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
1203 /* wishful thinking function */
1204 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1212 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1213 fz_text_span
*text
, *span
, **pspan
;
1214 struct pagedim
*pdim
, *pdimprev
;
1220 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1224 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1225 if (niters
++ == 5) {
1228 printd ("progress 1 attention requested aborting search at %d",
1233 printd ("progress %f searching in page %d",
1234 (double) (pageno
+ 1) / state
.pagecount
,
1239 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1240 pdim
= &state
.pagedims
[i
];
1241 if (pdim
->pageno
== pageno
) {
1244 if (pdim
->pageno
> pageno
) {
1253 text
= fz_new_text_span (state
.ctx
);
1254 tdev
= fz_new_text_device (state
.ctx
, text
);
1256 switch (state
.type
) {
1258 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1259 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, fz_identity
, NULL
);
1263 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1264 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, fz_identity
, NULL
);
1268 ARSERT (0 && state
.type
);
1271 fz_free_device (tdev
);
1274 for (span
= text
; span
; span
= span
->next
) {
1277 pspan
= malloc (sizeof (void *) * nspans
);
1279 err (1, "malloc span pointers %" FMT_s
, sizeof (void *) * nspans
);
1281 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1284 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1286 j
= forward
? 0 : nspans
- 1;
1291 j
+= forward
? 1 : -1;
1293 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
1295 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1300 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1304 if (span
->text
[i
].c
< 256) {
1305 *p
++ = span
->text
[i
].c
;
1316 ret
= regexec (re
, buf
, 1, &rm
, 0);
1318 if (ret
!= REG_NOMATCH
) {
1321 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1322 printd ("msg regexec error `%.*s'",
1323 (int) size
, errbuf
);
1324 fz_free_text_span (state
.ctx
, text
);
1325 state
.freepage (u
.ptr
);
1332 fz_point p1
, p2
, p3
, p4
;
1334 sb
= &span
->text
[rm
.rm_so
].bbox
;
1335 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1346 switch (state
.type
) {
1348 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1349 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1357 p1
= fz_transform_point (ctm
, p1
);
1358 p2
= fz_transform_point (ctm
, p2
);
1359 p3
= fz_transform_point (ctm
, p3
);
1360 p4
= fz_transform_point (ctm
, p4
);
1363 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1370 printd ("progress 1 found at %d `%.*s' in %f sec",
1371 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1375 printd ("match %d %d %f %f %f %f %f %f %f %f",
1393 fz_free_text_span (state
.ctx
, text
);
1394 state
.freepage (u
.ptr
);
1399 printd ("progress 1 no matches %f sec", end
- start
);
1401 printd ("clearrects");
1404 static void set_tex_params (int colorspace
)
1406 switch (colorspace
) {
1408 state
.texiform
= GL_RGBA8
;
1409 state
.texform
= GL_RGBA
;
1410 state
.texty
= GL_UNSIGNED_BYTE
;
1411 state
.colorspace
= fz_device_rgb
;
1414 state
.texiform
= GL_RGBA8
;
1415 state
.texform
= GL_BGRA
;
1416 state
.texty
= fz_is_big_endian ()
1417 ? GL_UNSIGNED_INT_8_8_8_8
1418 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1419 state
.colorspace
= fz_device_bgr
;
1422 state
.texiform
= GL_LUMINANCE_ALPHA
;
1423 state
.texform
= GL_LUMINANCE_ALPHA
;
1424 state
.texty
= GL_UNSIGNED_BYTE
;
1425 state
.colorspace
= fz_device_gray
;
1428 errx (1, "invalid colorspce %d", colorspace
);
1432 static void realloctexts (int texcount
)
1436 if (texcount
== state
.texcount
) return;
1438 if (texcount
< state
.texcount
) {
1439 glDeleteTextures (state
.texcount
- texcount
,
1440 state
.texids
+ texcount
);
1443 size
= texcount
* sizeof (*state
.texids
);
1444 state
.texids
= realloc (state
.texids
, size
);
1445 if (!state
.texids
) {
1446 err (1, "realloc texids %" FMT_s
, size
);
1449 size
= texcount
* sizeof (*state
.texowners
);
1450 state
.texowners
= realloc (state
.texowners
, size
);
1451 if (!state
.texowners
) {
1452 err (1, "realloc texowners %" FMT_s
, size
);
1454 if (texcount
> state
.texcount
) {
1457 glGenTextures (texcount
- state
.texcount
,
1458 state
.texids
+ state
.texcount
);
1459 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1460 state
.texowners
[i
].w
= -1;
1461 state
.texowners
[i
].slice
= NULL
;
1464 state
.texcount
= texcount
;
1474 mainloop (void *unused
)
1477 int len
, ret
, oldlen
= 0;
1482 errx (1, "readlen returned 0");
1485 if (oldlen
< len
+ 1) {
1486 p
= realloc (p
, len
+ 1);
1488 err (1, "realloc %d failed", len
+ 1);
1495 if (!strncmp ("open", p
, 4)) {
1498 char *filename
= p
+ 5;
1500 filenamelen
= strlen (filename
);
1501 password
= filename
+ filenamelen
+ 1;
1503 openxref (filename
, password
);
1506 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1507 state
.needoutline
= 1;
1509 else if (!strncmp ("cs", p
, 2)) {
1512 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1514 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1517 set_tex_params (colorspace
);
1518 for (i
= 0; i
< state
.texcount
; ++i
) {
1519 state
.texowners
[i
].w
= -1;
1520 state
.texowners
[i
].slice
= NULL
;
1524 else if (!strncmp ("freepage", p
, 8)) {
1527 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1529 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1533 else if (!strncmp ("freetile", p
, 8)) {
1536 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1538 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1542 else if (!strncmp ("search", p
, 6)) {
1543 int icase
, pageno
, y
, ret
, len2
, forward
;
1547 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1548 &icase
, &pageno
, &y
, &forward
, &len2
);
1550 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1553 pattern
= p
+ 6 + len2
;
1554 ret
= regcomp (&re
, pattern
,
1555 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1560 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1561 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1564 search (&re
, pageno
, y
, forward
);
1568 else if (!strncmp ("geometry", p
, 8)) {
1572 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1574 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1582 for (i
= 0; i
< state
.texcount
; ++i
) {
1583 state
.texowners
[i
].slice
= NULL
;
1589 unlock ("geometry");
1590 printd ("continue %d", state
.pagecount
);
1592 else if (!strncmp ("reqlayout", p
, 9)) {
1593 int rotate
, proportional
;
1596 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1598 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1601 state
.rotate
= rotate
;
1602 state
.proportional
= proportional
;
1604 unlock ("reqlayout");
1605 printd ("continue %d", state
.pagecount
);
1607 else if (!strncmp ("page", p
, 4)) {
1610 int pageno
, pindex
, ret
;
1612 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1614 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1619 page
= loadpage (pageno
, pindex
);
1623 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1625 else if (!strncmp ("tile", p
, 4)) {
1626 int x
, y
, w
, h
, ret
;
1631 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1632 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1634 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1639 tile
= rendertile (page
, x
, y
, w
, h
);
1643 printd ("tile %d %d %" FMT_ptr
" %u %f",
1645 FMT_ptr_cast2 (tile
),
1646 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1649 else if (!strncmp ("settrim", p
, 7)) {
1653 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1654 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1656 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1660 state
.trimmargins
= trimmargins
;
1661 state
.needoutline
= 1;
1662 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1664 state
.trimfuzz
= fuzz
;
1666 state
.pagedimcount
= 0;
1667 free (state
.pagedims
);
1668 state
.pagedims
= NULL
;
1673 printd ("continue %d", state
.pagecount
);
1675 else if (!strncmp ("sliceh", p
, 6)) {
1678 ret
= sscanf (p
+ 6, " %d", &h
);
1680 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1682 if (h
!= state
.sliceheight
) {
1685 state
.sliceheight
= h
;
1686 for (i
= 0; i
< state
.texcount
; ++i
) {
1687 state
.texowners
[i
].w
= -1;
1688 state
.texowners
[i
].h
= -1;
1689 state
.texowners
[i
].slice
= NULL
;
1693 else if (!strncmp ("interrupt", p
, 9)) {
1694 printd ("vmsg interrupted");
1696 else if (!strncmp ("quit", p
, 4)) {
1700 errx (1, "unknown command %.*s", len
, p
);
1706 CAMLprim value
ml_realloctexts (value texcount_v
)
1708 CAMLparam1 (texcount_v
);
1711 if (trylock ("ml_realloctexts")) {
1715 realloctexts (Int_val (texcount_v
));
1717 unlock ("ml_realloctexts");
1720 CAMLreturn (Val_bool (ok
));
1723 static void showsel (struct page
*page
, int ox
, int oy
)
1727 struct mark first
, last
;
1729 first
= page
->fmark
;
1732 if (!first
.span
|| !last
.span
) return;
1734 glEnable (GL_BLEND
);
1735 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1736 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1738 ox
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1739 oy
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1740 for (span
= first
.span
; span
; span
= span
->next
) {
1743 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1748 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1749 j
= MIN (first
.i
, last
.i
);
1750 k
= MAX (first
.i
, last
.i
);
1752 else if (span
== first
.span
) {
1755 else if (span
== last
.span
) {
1759 for (i
= j
; i
<= k
; ++i
) {
1760 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1762 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1769 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1771 if (span
== last
.span
) break;
1773 glDisable (GL_BLEND
);
1776 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1779 fz_link
*link
, *links
;
1781 switch (page
->type
) {
1783 links
= page
->u
.pdfpage
->links
;
1787 links
= page
->u
.xpspage
->links
;
1794 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1795 glEnable (GL_LINE_STIPPLE
);
1796 glLineStipple (0.5, 0xcccc);
1798 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1801 for (link
= links
; link
; link
= link
->next
) {
1802 fz_point p1
, p2
, p3
, p4
;
1804 p1
.x
= link
->rect
.x0
;
1805 p1
.y
= link
->rect
.y0
;
1807 p2
.x
= link
->rect
.x1
;
1808 p2
.y
= link
->rect
.y0
;
1810 p3
.x
= link
->rect
.x1
;
1811 p3
.y
= link
->rect
.y1
;
1813 p4
.x
= link
->rect
.x0
;
1814 p4
.y
= link
->rect
.y1
;
1816 p1
= fz_transform_point (ctm
, p1
);
1817 p2
= fz_transform_point (ctm
, p2
);
1818 p3
= fz_transform_point (ctm
, p3
);
1819 p4
= fz_transform_point (ctm
, p4
);
1821 switch (link
->dest
.kind
) {
1822 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1823 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1824 default: glColor3ub (0, 0, 0); break;
1827 glVertex2f (p1
.x
, p1
.y
);
1828 glVertex2f (p2
.x
, p2
.y
);
1829 glVertex2f (p3
.x
, p3
.y
);
1830 glVertex2f (p4
.x
, p4
.y
);
1834 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1835 glDisable (GL_LINE_STIPPLE
);
1838 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1841 struct slice
*slice1
;
1844 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1845 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1847 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
1848 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1849 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1853 int texindex
= state
.texindex
++ % state
.texcount
;
1855 if (state
.texowners
[texindex
].w
== tile
->w
) {
1856 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1860 state
.texowners
[texindex
].h
= slice
->h
;
1864 state
.texowners
[texindex
].h
= slice
->h
;
1867 state
.texowners
[texindex
].w
= tile
->w
;
1868 state
.texowners
[texindex
].slice
= slice
;
1869 slice
->texindex
= texindex
;
1871 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1873 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1881 tile
->pixmap
->samples
+offset
1885 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1893 tile
->pixmap
->samples
+offset
1899 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1901 CAMLparam2 (args_v
, ptr_v
);
1902 int dispx
= Int_val (Field (args_v
, 0));
1903 int dispy
= Int_val (Field (args_v
, 1));
1904 int dispw
= Int_val (Field (args_v
, 2));
1905 int disph
= Int_val (Field (args_v
, 3));
1906 int tilex
= Int_val (Field (args_v
, 4));
1907 int tiley
= Int_val (Field (args_v
, 5));
1908 char *s
= String_val (ptr_v
);
1909 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1911 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1913 int slicey
, firstslice
;
1914 struct slice
*slice
;
1916 firstslice
= tiley
/ tile
->sliceheight
;
1917 slice
= &tile
->slices
[firstslice
];
1918 slicey
= tiley
% tile
->sliceheight
;
1923 dh
= slice
->h
- slicey
;
1924 dh
= MIN (disph
, dh
);
1925 uploadslice (tile
, slice
);
1929 glTexCoord2i (tilex
, slicey
);
1930 glVertex2i (dispx
, dispy
);
1932 glTexCoord2i (tilex
+dispw
, slicey
);
1933 glVertex2i (dispx
+dispw
, dispy
);
1935 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1936 glVertex2i (dispx
+dispw
, dispy
+dh
);
1938 glTexCoord2i (tilex
, slicey
+dh
);
1939 glVertex2i (dispx
, dispy
+dh
);
1946 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1950 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1951 CAMLreturn (Val_unit
);
1954 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1955 value xoff_v
, value yoff_v
)
1957 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1958 int xoff
= Int_val (xoff_v
);
1959 int yoff
= Int_val (yoff_v
);
1960 char *s
= String_val (ptr_v
);
1961 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1963 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1965 if (trylock ("ml_postprocess")) {
1968 showsel (page
, xoff
, yoff
);
1969 unlock ("ml_postprocess");
1972 CAMLreturn (Val_unit
);
1975 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1979 fz_link
*link
, *links
;
1981 switch (page
->type
) {
1983 links
= page
->u
.pdfpage
->links
;
1987 links
= page
->u
.xpspage
->links
;
1996 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1997 state
.pagedims
[page
->pdimno
].ctm
);
1998 ctm
= fz_invert_matrix (ctm
);
1999 p
= fz_transform_point (ctm
, p
);
2001 for (link
= links
; link
; link
= link
->next
) {
2002 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2003 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2011 static void droptext (struct page
*page
)
2014 fz_free_text_span (state
.ctx
, page
->text
);
2017 page
->fmark
.span
= NULL
;
2018 page
->lmark
.span
= NULL
;
2023 static void ensuretext (struct page
*page
)
2025 if (state
.gen
!= page
->gen
) {
2027 page
->gen
= state
.gen
;
2032 page
->text
= fz_new_text_span (state
.ctx
);
2033 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
2034 fz_run_display_list (page
->dlist
,
2037 fz_infinite_bbox
, NULL
);
2038 fz_free_device (tdev
);
2042 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2044 CAMLparam3 (ptr_v
, x_v
, y_v
);
2045 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2049 char *s
= String_val (ptr_v
);
2050 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2051 struct pagedim
*pdim
;
2053 ret_v
= Val_int (0);
2054 if (trylock ("ml_whatsunder")) {
2058 page
= parse_pointer ("ml_whatsunder", s
);
2059 pdim
= &state
.pagedims
[page
->pdimno
];
2060 x
+= pdim
->bounds
.x0
;
2061 y
+= pdim
->bounds
.y0
;
2062 link
= getlink (page
, x
, y
);
2064 switch (link
->dest
.kind
) {
2069 pageno
= link
->dest
.ld
.gotor
.page
;
2073 if (link
->dest
.ld
.gotor
.flags
& fz_link_flag_t_valid
) {
2074 p
.y
= link
->dest
.ld
.gotor
.lt
.y
;
2075 p
= fz_transform_point (pdim
->lctm
, p
);
2077 tup_v
= caml_alloc_tuple (2);
2078 ret_v
= caml_alloc_small (1, 1);
2079 Field (tup_v
, 0) = Val_int (pageno
);
2080 Field (tup_v
, 1) = Val_int (p
.y
);
2081 Field (ret_v
, 0) = tup_v
;
2086 str_v
= caml_copy_string (link
->dest
.ld
.uri
.uri
);
2087 ret_v
= caml_alloc_small (1, 0);
2088 Field (ret_v
, 0) = str_v
;
2091 case FZ_LINK_LAUNCH
:
2092 str_v
= caml_copy_string (link
->dest
.ld
.launch
.file_spec
);
2093 ret_v
= caml_alloc_small (1, 4);
2094 Field (ret_v
, 0) = str_v
;
2098 str_v
= caml_copy_string (link
->dest
.ld
.named
.named
);
2099 ret_v
= caml_alloc_small (1, 5);
2100 Field (ret_v
, 0) = str_v
;
2104 str_v
= caml_copy_string (link
->dest
.ld
.gotor
.file_spec
);
2105 pageno
= link
->dest
.ld
.gotor
.page
;
2106 tup_v
= caml_alloc_tuple (2);
2107 ret_v
= caml_alloc_small (1, 6);
2108 Field (tup_v
, 0) = str_v
;
2109 Field (tup_v
, 1) = Val_int (pageno
);
2110 Field (ret_v
, 0) = tup_v
;
2117 snprintf (buf
, sizeof (buf
),
2118 "unhandled link kind %d", link
->dest
.kind
);
2119 str_v
= caml_copy_string (buf
);
2120 ret_v
= caml_alloc_small (1, 3);
2121 Field (ret_v
, 0) = str_v
;
2131 for (span
= page
->text
; span
; span
= span
->next
) {
2132 for (i
= 0; i
< span
->len
; ++i
) {
2134 b
= &span
->text
[i
].bbox
;
2135 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
2137 span
->font
&& span
->font
->name
2139 : "Span has no font name"
2141 FT_FaceRec
*face
= span
->font
->ft_face
;
2142 if (face
&& face
->family_name
) {
2144 char *n1
= face
->family_name
;
2145 size_t l1
= strlen (n1
);
2146 size_t l2
= strlen (n2
);
2148 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2149 s
= malloc (l1
+ l2
+ 2);
2153 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2154 str_v
= caml_copy_string (s
);
2160 str_v
= caml_copy_string (n2
);
2162 ret_v
= caml_alloc_small (1, 2);
2163 Field (ret_v
, 0) = str_v
;
2170 unlock ("ml_whatsunder");
2176 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2178 CAMLparam2 (ptr_v
, rect_v
);
2182 struct mark first
, last
;
2183 int i
, x0
, x1
, y0
, y1
;
2184 struct pagedim
*pdim
;
2185 char *s
= String_val (ptr_v
);
2187 if (trylock ("ml_seltext")) {
2191 page
= parse_pointer ("ml_seltext", s
);
2194 pdim
= &state
.pagedims
[page
->pdimno
];
2196 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
2197 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2198 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2199 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2202 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2203 glColor3ub (128, 128, 128);
2204 glRecti (x0
, y0
, x1
, y1
);
2205 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2211 last
.i
= first
.i
= 0;
2212 first
.span
= page
->text
;
2213 for (span
= page
->text
; span
; span
= span
->next
) {
2214 for (i
= 0; i
< span
->len
; ++i
) {
2215 b
= &span
->text
[i
].bbox
;
2218 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
2223 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
2228 if (0 && selected
) {
2229 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2230 glColor3ub (128, 128, 128);
2231 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2232 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2237 if (y1
< y0
|| x1
< x0
) {
2240 if (first
.span
== last
.span
) {
2245 for (span
= first
.span
; span
&& span
!= last
.span
;
2246 span
= span
->next
) {
2259 first
.span
= last
.span
;
2265 page
->fmark
= first
;
2268 unlock ("ml_seltext");
2271 CAMLreturn (Val_unit
);
2274 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2279 for (i
= a
; i
<= b
; ++i
) {
2280 len
= runetochar (buf
, &span
->text
[i
].c
);
2281 ret
= fwrite (buf
, len
, 1, f
);
2284 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2285 len
, ret
, strerror (errno
));
2292 CAMLprim value
ml_copysel (value command_v
, value ptr_v
)
2298 char *s
= String_val (ptr_v
);
2299 char *command
= String_val (command_v
);
2301 if (trylock ("ml_copysel")) {
2306 page
= parse_pointer ("ml_sopysel", s
);
2308 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2309 fprintf (stderr
, "nothing to copy\n");
2313 f
= popen (command
, "w");
2315 fprintf (stderr
, "failed to open sel pipe: %s\n",
2320 for (span
= page
->fmark
.span
;
2321 span
&& span
!= page
->lmark
.span
->next
;
2322 span
= span
->next
) {
2323 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2324 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2325 if (pipespan (f
, span
, a
, b
)) {
2329 if (putc ('\n', f
) == EOF
) {
2330 fprintf (stderr
, "failed break line on sel pipe: %s\n",
2336 page
->lmark
.span
= NULL
;
2337 page
->fmark
.span
= NULL
;
2341 int ret
= pclose (f
);
2343 if (errno
!= ECHILD
) {
2344 fprintf (stderr
, "failed to close sel pipe: %s\n",
2350 unlock ("ml_copysel");
2353 CAMLreturn (Val_unit
);
2356 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2358 CAMLparam1 (pagedimno_v
);
2360 int pagedimno
= Int_val (pagedimno_v
);
2363 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2364 if (trylock ("ml_getpdimrect")) {
2365 box
= fz_empty_rect
;
2368 box
= state
.pagedims
[pagedimno
].mediabox
;
2369 unlock ("ml_getpdimrect");
2372 Store_double_field (ret_v
, 0, box
.x0
);
2373 Store_double_field (ret_v
, 1, box
.x1
);
2374 Store_double_field (ret_v
, 2, box
.y0
);
2375 Store_double_field (ret_v
, 3, box
.y1
);
2380 static double getmaxw (void)
2386 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2389 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2390 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2393 maxw
= MAX (w
, maxw
);
2398 CAMLprim value
ml_getmaxw (value unit_v
)
2400 CAMLparam1 (unit_v
);
2404 if (trylock ("ml_getmaxw")) {
2408 unlock ("ml_getmaxw");
2410 ret_v
= caml_copy_double (maxw
);
2414 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2416 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2420 double maxw
= 0.0, maxh
= 0.0;
2422 double winw
= Int_val (winw_v
);
2423 double winh
= Int_val (winh_v
);
2424 double dw
= Int_val (dw_v
);
2425 double pw
= 1.0, ph
= 1.0, num
, den
;
2427 if (trylock ("ml_zoom_for_height")) {
2431 if (state
.proportional
) {
2435 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2436 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2438 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2439 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2440 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2441 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2446 if (state
.proportional
) {
2448 scaledh
= h
* scale
;
2455 if (scaledh
> maxh
) {
2462 num
= (winh
* pw
) + (ph
* dw
);
2466 unlock ("ml_zoom_for_height");
2468 ret_v
= caml_copy_double (zoom
);
2474 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2476 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2478 int pt
= Int_val(pt_v
);
2479 int x
= Int_val (x_v
);
2480 int y
= Int_val (y_v
);
2483 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2484 ret_v
= caml_copy_double (w
);
2488 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2490 CAMLparam2 (pt_v
, string_v
);
2492 int pt
= Int_val (pt_v
);
2495 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2496 ret_v
= caml_copy_double (w
);
2500 CAMLprim value
ml_getpagebox (value opaque_v
)
2502 CAMLparam1 (opaque_v
);
2506 char *s
= String_val (opaque_v
);
2507 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2509 ret_v
= caml_alloc_tuple (4);
2510 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2511 dev
->hints
|= FZ_IGNORE_SHADE
;
2513 switch (page
->type
) {
2515 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
2519 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
2523 bbox
= fz_infinite_bbox
;
2527 fz_free_device (dev
);
2528 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2529 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2530 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2531 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2536 CAMLprim value
ml_setaalevel (value level_v
)
2538 CAMLparam1 (level_v
);
2540 state
.aalevel
= Int_val (level_v
);
2541 CAMLreturn (Val_unit
);
2544 #if !defined _WIN32 && !defined __APPLE__
2547 #include <X11/Xlib.h>
2548 #include <X11/Xutil.h>
2551 static void set_wm_class (void)
2558 display
= getenv ("DISPLAY");
2559 dpy
= XOpenDisplay (display
);
2561 fprintf (stderr
, "XOpenDisplay `%s' failed\n",
2562 display
? display
: "null");
2565 hint
.res_name
= "llpp";
2566 hint
.res_class
= "llpp";
2567 win
= glXGetCurrentDrawable ();
2569 fprintf (stderr
, "glXGetCurrentDrawable returned None\n");
2570 XCloseDisplay (dpy
);
2573 XSetClassHint (dpy
, win
, &hint
);
2574 XCloseDisplay (dpy
);
2576 #define HAS_WM_CLASS_HACK
2579 enum { piunknown
, pilinux
, piwindows
, pwindowsgui
, piosx
,
2580 pisun
, pifreebsd
, pidragonflybsd
, piopenbsd
, pinetbsd
,
2581 pimingw
, pmingwgui
, picygwin
};
2583 #define NOZOMBIESPLEASE
2586 static int isgui (void)
2588 /* http://www.opensc.ws/c-snippets/12714-c-getprocaddressex.html
2590 char *p
= (char *) GetModuleHandle (NULL
);
2592 IMAGE_DOS_HEADER
*dh
= (IMAGE_DOS_HEADER
*) p
;
2593 IMAGE_NT_HEADERS
*nh
= (IMAGE_NT_HEADERS
*) (p
+ dh
->e_lfanew
);
2595 return nh
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
;
2601 CAMLprim value
ml_platform (value unit_v
)
2603 CAMLparam1 (unit_v
);
2604 int platid
= piunknown
;
2606 #if defined __linux__
2608 #elif defined __CYGWIN__
2609 #undef NOZOMBIESPLEASE
2611 #elif defined __MINGW32__
2612 #undef NOZOMBIESPLEASE
2613 platid
= pimingw
+ isgui ();
2614 #elif defined _WIN32
2615 #undef NOZOMBIESPLEASE
2616 platid
= piwindows
+ isgui ();
2617 #elif defined __DragonFly__
2618 platid
= pidragonflybsd
;
2619 #elif defined __FreeBSD__
2621 #elif defined __OpenBSD__
2623 #elif defined __NetBSD__
2625 #elif defined __sun__
2627 #elif defined __APPLE__
2630 CAMLreturn (Val_int (platid
));
2633 #ifdef NOZOMBIESPLEASE
2637 CAMLprim value
ml_init (value pipe_v
, value params_v
)
2639 CAMLparam2 (pipe_v
, params_v
);
2640 CAMLlocal2 (trim_v
, fuzz_v
);
2648 state
.cr
= Handle_val (Field (pipe_v
, 0));
2649 state
.cw
= Handle_val (Field (pipe_v
, 1));
2651 state
.cr
= Int_val (Field (pipe_v
, 0));
2652 state
.cw
= Int_val (Field (pipe_v
, 1));
2654 state
.rotate
= Int_val (Field (params_v
, 0));
2655 state
.proportional
= Bool_val (Field (params_v
, 1));
2656 trim_v
= Field (params_v
, 2);
2657 texcount
= Int_val (Field (params_v
, 3));
2658 state
.sliceheight
= Int_val (Field (params_v
, 4));
2659 mustoresize
= Int_val (Field (params_v
, 5));
2660 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
2661 colorspace
= Int_val (Field (params_v
, 6));
2662 wmclasshack
= Bool_val (Field (params_v
, 7));
2663 fontpath
= String_val (Field (params_v
, 8));
2665 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2666 fuzz_v
= Field (trim_v
, 1);
2667 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2668 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2669 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2670 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2672 set_tex_params (colorspace
);
2673 #ifdef HAS_WM_CLASS_HACK
2682 state
.face
= load_font (fontpath
);
2686 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2688 state
.face
= load_builtin_font (base
, len
);
2690 if (!state
.face
) _exit (1);
2692 realloctexts (texcount
);
2694 #ifdef NOZOMBIESPLEASE
2696 struct sigaction sa
;
2698 sa
.sa_handler
= SIG_DFL
;
2699 if (sigemptyset (&sa
.sa_mask
)) {
2700 err (1, "sigemptyset");
2702 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
2703 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
2704 err (1, "sigaction");
2710 InitializeCriticalSection (&critsec
);
2711 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2712 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2713 errx (1, "CreateThread failed: %lx", GetLastError ());
2717 int ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2719 errx (1, "pthread_create: %s", strerror (ret
));
2724 CAMLreturn (Val_unit
);