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)
353 ret
= WaitForSingleObject (state
.cr
, 0);
354 if (ret
!= WAIT_OBJECT_0
&& ret
!= WAIT_TIMEOUT
) {
355 winerr (1, "hasdata, WaitForSingleObject ret=%d", ret
);
357 return ret
== WAIT_OBJECT_0
;
360 static void readdata (char *p
, int size
)
365 okay
= ReadFile (state
.cr
, p
, size
, &nread
, NULL
);
366 if (!okay
|| nread
- size
) {
367 err (1, "ReadFile (req %d, okay %d, ret %ld)", size
, okay
, nread
);
371 static int readlen (void)
376 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
379 static void writedata (char *p
, int size
)
385 buf
[0] = (size
>> 24) & 0xff;
386 buf
[1] = (size
>> 16) & 0xff;
387 buf
[2] = (size
>> 8) & 0xff;
388 buf
[3] = (size
>> 0) & 0xff;
390 okay
= WriteFile (state
.cw
, buf
, 4, &nwritten
, NULL
);
391 if (!okay
|| nwritten
!= 4) {
392 winerr (1, "WriteFile okay %d ret %ld", okay
, nwritten
);
395 okay
= WriteFile (state
.cw
, p
, size
, &nwritten
, NULL
);
396 if (!okay
|| nwritten
- size
) {
397 winerr (1, "WriteFile (req %d, okay %d, ret %ld)",
398 size
, okay
, nwritten
);
402 static double now (void)
406 if (gettimeofday (&tv
, NULL
)) {
407 err (1, "gettimeofday");
409 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
412 static int hasdata (void)
416 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
417 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
421 static void readdata (void *p
, int size
)
425 n
= read (state
.cr
, p
, size
);
427 if (!n
) errx (1, "EOF while reading");
428 err (1, "read (req %d, ret %zd)", size
, n
);
432 static int readlen (void)
437 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
440 static void writedata (char *p
, int size
)
445 buf
[0] = (size
>> 24) & 0xff;
446 buf
[1] = (size
>> 16) & 0xff;
447 buf
[2] = (size
>> 8) & 0xff;
448 buf
[3] = (size
>> 0) & 0xff;
450 n
= write (state
.cw
, buf
, 4);
452 if (!n
) errx (1, "EOF while writing length");
453 err (1, "write %zd", n
);
456 n
= write (state
.cw
, p
, size
);
458 if (!n
) errx (1, "EOF while writing data");
459 err (1, "write (req %d, ret %zd)", size
, n
);
464 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
472 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
475 len
= vsnprintf (buf
, size
, fmt
, ap
);
478 if (len
> -1 && len
< size
) {
479 writedata (buf
, len
);
489 buf
= realloc (buf
, size
);
494 static void closepdf (void)
497 pdf_close_document (state
.u
.pdf
);
502 static void closexps (void)
505 xps_close_document (state
.u
.xps
);
510 static void closecbz (void)
513 cbz_close_document (state
.u
.cbz
);
518 static void freepdfpage (void *ptr
)
520 pdf_free_page (state
.u
.pdf
, ptr
);
523 static void freexpspage (void *ptr
)
525 xps_free_page (state
.u
.xps
, ptr
);
528 static void freecbzpage (void *ptr
)
530 cbz_free_page (state
.u
.cbz
, ptr
);
533 static void openxref (char *filename
, char *password
)
537 for (i
= 0; i
< state
.texcount
; ++i
) {
538 state
.texowners
[i
].w
= -1;
539 state
.texowners
[i
].slice
= NULL
;
542 if (state
.closedoc
) state
.closedoc ();
544 len
= strlen (filename
);
550 ext
[0] = tolower (filename
[len
-3]);
551 ext
[1] = tolower (filename
[len
-2]);
552 ext
[2] = tolower (filename
[len
-1]);
554 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
557 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
562 if (state
.pagedims
) {
563 free (state
.pagedims
);
564 state
.pagedims
= NULL
;
566 state
.pagedimcount
= 0;
568 fz_set_aa_level (state
.ctx
, state
.aalevel
);
569 switch (state
.type
) {
571 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
572 if (pdf_needs_password (state
.u
.pdf
)) {
573 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
575 errx (1, "invalid password");
578 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
579 state
.closedoc
= closepdf
;
580 state
.freepage
= freepdfpage
;
584 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
585 state
.pagecount
= xps_count_pages (state
.u
.xps
);
586 state
.closedoc
= closexps
;
587 state
.freepage
= freexpspage
;
591 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
592 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
593 state
.closedoc
= closecbz
;
594 state
.freepage
= freecbzpage
;
599 static void pdfinfo (void)
601 if (state
.type
== DPDF
) {
604 printd ("info PDF version\t%d.%d",
605 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
607 infoobj
= fz_dict_gets (state
.u
.pdf
->trailer
, "Info");
611 char *items
[] = { "Title", "Author", "Creator",
612 "Producer", "CreationDate" };
614 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
615 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
616 s
= pdf_to_utf8 (state
.ctx
, obj
);
619 printd ("title %s", s
);
621 printd ("info %s\t%s", items
[i
], s
);
623 fz_free (state
.ctx
, s
);
630 static void unlinktile (struct tile
*tile
)
634 for (i
= 0; i
< tile
->slicecount
; ++i
) {
635 struct slice
*s
= &tile
->slices
[i
];
637 if (s
->texindex
!= -1) {
638 if (state
.texowners
[s
->texindex
].slice
== s
) {
639 state
.texowners
[s
->texindex
].slice
= NULL
;
645 static void freepage (struct page
*page
)
648 fz_free_text_span (state
.ctx
, page
->text
);
650 page
->freepage (page
->u
.ptr
);
651 fz_free_display_list (state
.ctx
, page
->dlist
);
655 static void freetile (struct tile
*tile
)
659 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
662 fz_drop_pixmap (state
.ctx
, state
.pig
);
664 state
.pig
= tile
->pixmap
;
672 static int cacheline32bytes
;
673 extern char **environ
;
675 static void __attribute__ ((constructor
)) clcheck (void)
677 char **envp
= environ
;
682 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
684 cacheline32bytes
= auxv
[1] == 32;
690 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
692 if (cacheline32bytes
) {
693 intptr_t a1
, a2
, diff
;
694 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
695 vector
unsigned char v
= vec_splat_u8 (-1);
696 vector
unsigned char *p
;
698 a1
= a2
= (intptr_t) pixmap
->samples
;
699 a2
= (a1
+ 31) & ~31;
704 while (a1
!= a2
) *(char *) a1
++ = 0xff;
705 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
706 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
708 vec_st (v
, i
+ 16, p
);
710 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
712 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
715 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
718 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
721 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
723 if (!pdim
->tctmready
) {
724 if (state
.trimmargins
) {
727 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
728 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
729 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
730 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
741 static fz_matrix
pagectm (struct page
*page
)
743 if (page
->type
== DPDF
) {
744 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
745 state
.pagedims
[page
->pdimno
].ctm
);
749 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
751 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
752 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
753 -pdim
->mediabox
.y0
), ctm
);
758 static void *loadpage (int pageno
, int pindex
)
761 struct page
*page
= NULL
;
763 page
= calloc (sizeof (struct page
), 1);
765 err (1, "calloc page %d", pageno
);
768 page
->dlist
= fz_new_display_list (state
.ctx
);
769 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
770 switch (state
.type
) {
772 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
773 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
774 page
->freepage
= freepdfpage
;
778 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
779 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
780 page
->freepage
= freexpspage
;
784 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
785 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
786 page
->freepage
= freecbzpage
;
789 fz_free_device (dev
);
791 page
->pdimno
= pindex
;
792 page
->pageno
= pageno
;
793 page
->gen
= state
.gen
;
794 page
->type
= state
.type
;
799 static struct tile
*alloctile (int h
)
806 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
807 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
808 tile
= calloc (tilesize
, 1);
810 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
812 for (i
= 0; i
< slicecount
; ++i
) {
813 int sh
= MIN (h
, state
.sliceheight
);
814 tile
->slices
[i
].h
= sh
;
815 tile
->slices
[i
].texindex
= -1;
818 tile
->slicecount
= slicecount
;
819 tile
->sliceheight
= state
.sliceheight
;
823 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
828 struct pagedim
*pdim
;
830 tile
= alloctile (h
);
831 pdim
= &state
.pagedims
[page
->pdimno
];
836 bbox
.x1
= bbox
.x0
+ w
;
837 bbox
.y1
= bbox
.y0
+ h
;
840 if (state
.pig
->w
== w
842 && state
.pig
->colorspace
== state
.colorspace
) {
843 tile
->pixmap
= state
.pig
;
844 tile
->pixmap
->x
= bbox
.x0
;
845 tile
->pixmap
->y
= bbox
.y0
;
848 fz_drop_pixmap (state
.ctx
, state
.pig
);
854 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
859 clearpixmap (tile
->pixmap
);
860 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
861 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
862 fz_free_device (dev
);
867 static void initpdims (void)
873 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
879 switch (state
.type
) {
881 pageobj
= state
.u
.pdf
->page_objs
[pageno
];
883 if (state
.trimmargins
) {
887 page
= pdf_load_page (state
.u
.pdf
, pageno
);
888 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
889 if (state
.trimanew
|| !obj
) {
895 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
896 dev
->hints
|= FZ_IGNORE_SHADE
;
897 ctm
= fz_invert_matrix (page
->ctm
);
898 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
899 fz_free_device (dev
);
901 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
902 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
903 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
904 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
905 rect
= fz_transform_rect (ctm
, rect
);
906 rect
= fz_intersect_rect (rect
, page
->mediabox
);
908 if (fz_is_empty_rect (rect
)) {
909 mediabox
= page
->mediabox
;
915 obj
= fz_new_array (state
.ctx
, 4);
916 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
917 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
918 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
919 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
920 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
923 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
924 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
925 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
926 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
929 rotate
= page
->rotate
;
930 pdf_free_page (state
.u
.pdf
, page
);
932 printd ("progress %f Trimming %d",
933 (double) (pageno
+ 1) / state
.pagecount
,
939 mediabox
= pdf_to_rect (state
.ctx
,
940 fz_dict_gets (pageobj
, "MediaBox"));
941 if (fz_is_empty_rect (mediabox
)) {
942 fprintf (stderr
, "cannot find page size for page %d\n",
950 cropbox
= pdf_to_rect (state
.ctx
,
951 fz_dict_gets (pageobj
, "CropBox"));
952 if (!fz_is_empty_rect (cropbox
)) {
953 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
955 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
963 page
= xps_load_page (state
.u
.xps
, pageno
);
964 mediabox
= xps_bound_page (state
.u
.xps
, page
);
966 if (state
.trimmargins
) {
971 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
972 dev
->hints
|= FZ_IGNORE_SHADE
;
973 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
974 fz_free_device (dev
);
976 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
977 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
978 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
979 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
980 rect
= fz_intersect_rect (rect
, mediabox
);
982 if (!fz_is_empty_rect (rect
)) {
986 xps_free_page (state
.u
.xps
, page
);
987 printd ("progress %f loading %d",
988 (double) (pageno
+ 1) / state
.pagecount
,
996 if (state
.trimmargins
) {
999 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1000 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
1001 cbz_free_page (state
.u
.cbz
, page
);
1002 printd ("progress %f Trimming %d",
1003 (double) (pageno
+ 1) / state
.pagecount
,
1007 mediabox
.x0
= mediabox
.y0
= 0;
1015 ARSERT (0 && state
.type
);
1018 if (state
.pagedimcount
== 0
1019 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1020 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1023 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1024 state
.pagedims
= realloc (state
.pagedims
, size
);
1025 if (!state
.pagedims
) {
1026 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1027 size
, state
.pagedimcount
+ 1);
1030 p
= &state
.pagedims
[state
.pagedimcount
++];
1032 p
->mediabox
= mediabox
;
1037 if (state
.trimmargins
) {
1038 printd ("progress 1 Trimmed %d pages in %f seconds",
1039 state
.pagecount
, end
- start
);
1042 printd ("vmsg Processed %d pages in %f seconds",
1043 state
.pagecount
, end
- start
);
1048 static void layout (void)
1053 double zoom
, w
, maxw
= 0;
1054 struct pagedim
*p
= state
.pagedims
;
1056 if (state
.proportional
) {
1057 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1058 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1060 w
= box
.x1
- box
.x0
;
1061 maxw
= MAX (w
, maxw
);
1066 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1069 ctm
= fz_rotate (state
.rotate
);
1070 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1072 w
= box
.x1
- box
.x0
;
1074 if (state
.proportional
) {
1075 double scale
= w
/ maxw
;
1076 zoom
= (state
.w
/ w
) * scale
;
1082 p
->zoomctm
= fz_scale (zoom
, zoom
);
1083 ctm
= fz_concat (p
->zoomctm
, ctm
);
1085 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1086 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1087 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1090 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1093 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1097 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1098 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1099 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1105 while (p
-- != state
.pagedims
) {
1106 int w
= p
->bounds
.x1
- p
->bounds
.x0
;
1107 int h
= p
->bounds
.y1
- p
->bounds
.y0
;
1109 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1113 static void recurse_outline (fz_outline
*outline
, int level
)
1118 struct pagedim
*pdim
= state
.pagedims
;
1120 dest
= &outline
->dest
;
1121 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1122 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1124 pdim
= &state
.pagedims
[i
];
1126 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1129 p
.y
= dest
->ld
.gotor
.lt
.y
;
1130 p
= fz_transform_point (pdim
->lctm
, p
);
1133 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1137 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1138 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1140 printd ("o %d %d %d %d %s",
1141 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1143 if (outline
->down
) {
1144 recurse_outline (outline
->down
, level
+ 1);
1146 outline
= outline
->next
;
1150 static void process_outline (void)
1152 fz_outline
*outline
;
1154 if (!state
.needoutline
) return;
1156 state
.needoutline
= 0;
1157 switch (state
.type
) {
1159 outline
= pdf_load_outline (state
.u
.pdf
);
1162 outline
= xps_load_outline (state
.u
.xps
);
1169 recurse_outline (outline
, 0);
1170 fz_free_outline (state
.ctx
, outline
);
1174 static int comparespans (const void *l
, const void *r
)
1176 fz_text_span
const *const*ls
= l
;
1177 fz_text_span
const *const*rs
= r
;
1178 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
1181 /* wishful thinking function */
1182 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1190 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1191 fz_text_span
*text
, *span
, **pspan
;
1192 struct pagedim
*pdim
, *pdimprev
;
1198 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1202 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1203 if (niters
++ == 5) {
1206 printd ("progress 1 attention requested aborting search at %d",
1211 printd ("progress %f searching in page %d",
1212 (double) (pageno
+ 1) / state
.pagecount
,
1217 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1218 pdim
= &state
.pagedims
[i
];
1219 if (pdim
->pageno
== pageno
) {
1222 if (pdim
->pageno
> pageno
) {
1231 text
= fz_new_text_span (state
.ctx
);
1232 tdev
= fz_new_text_device (state
.ctx
, text
);
1234 switch (state
.type
) {
1236 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1237 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, fz_identity
, NULL
);
1241 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1242 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, fz_identity
, NULL
);
1246 ARSERT (0 && state
.type
);
1249 fz_free_device (tdev
);
1252 for (span
= text
; span
; span
= span
->next
) {
1255 pspan
= malloc (sizeof (void *) * nspans
);
1257 err (1, "malloc span pointers %" FMT_s
, sizeof (void *) * nspans
);
1259 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1262 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1264 j
= forward
? 0 : nspans
- 1;
1269 j
+= forward
? 1 : -1;
1271 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
1273 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1278 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1282 if (span
->text
[i
].c
< 256) {
1283 *p
++ = span
->text
[i
].c
;
1294 ret
= regexec (re
, buf
, 1, &rm
, 0);
1296 if (ret
!= REG_NOMATCH
) {
1299 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1300 printd ("msg regexec error `%.*s'",
1301 (int) size
, errbuf
);
1302 fz_free_text_span (state
.ctx
, text
);
1303 state
.freepage (u
.ptr
);
1310 fz_point p1
, p2
, p3
, p4
;
1312 sb
= &span
->text
[rm
.rm_so
].bbox
;
1313 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1324 switch (state
.type
) {
1326 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1327 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1335 p1
= fz_transform_point (ctm
, p1
);
1336 p2
= fz_transform_point (ctm
, p2
);
1337 p3
= fz_transform_point (ctm
, p3
);
1338 p4
= fz_transform_point (ctm
, p4
);
1341 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1348 printd ("progress 1 found at %d `%.*s' in %f sec",
1349 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1353 printd ("match %d %d %f %f %f %f %f %f %f %f",
1371 fz_free_text_span (state
.ctx
, text
);
1372 state
.freepage (u
.ptr
);
1377 printd ("progress 1 no matches %f sec", end
- start
);
1379 printd ("clearrects");
1382 static void set_tex_params (int colorspace
)
1384 switch (colorspace
) {
1386 state
.texiform
= GL_RGBA8
;
1387 state
.texform
= GL_RGBA
;
1388 state
.texty
= GL_UNSIGNED_BYTE
;
1389 state
.colorspace
= fz_device_rgb
;
1392 state
.texiform
= GL_RGBA8
;
1393 state
.texform
= GL_BGRA
;
1394 state
.texty
= fz_is_big_endian ()
1395 ? GL_UNSIGNED_INT_8_8_8_8
1396 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1397 state
.colorspace
= fz_device_bgr
;
1400 state
.texiform
= GL_LUMINANCE_ALPHA
;
1401 state
.texform
= GL_LUMINANCE_ALPHA
;
1402 state
.texty
= GL_UNSIGNED_BYTE
;
1403 state
.colorspace
= fz_device_gray
;
1406 errx (1, "invalid colorspce %d", colorspace
);
1410 static void realloctexts (int texcount
)
1414 if (texcount
== state
.texcount
) return;
1416 if (texcount
< state
.texcount
) {
1417 glDeleteTextures (state
.texcount
- texcount
,
1418 state
.texids
+ texcount
);
1421 size
= texcount
* sizeof (*state
.texids
);
1422 state
.texids
= realloc (state
.texids
, size
);
1423 if (!state
.texids
) {
1424 err (1, "realloc texids %" FMT_s
, size
);
1427 size
= texcount
* sizeof (*state
.texowners
);
1428 state
.texowners
= realloc (state
.texowners
, size
);
1429 if (!state
.texowners
) {
1430 err (1, "realloc texowners %" FMT_s
, size
);
1432 if (texcount
> state
.texcount
) {
1435 glGenTextures (texcount
- state
.texcount
,
1436 state
.texids
+ state
.texcount
);
1437 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1438 state
.texowners
[i
].w
= -1;
1439 state
.texowners
[i
].slice
= NULL
;
1442 state
.texcount
= texcount
;
1452 mainloop (void *unused
)
1455 int len
, ret
, oldlen
= 0;
1460 errx (1, "readlen returned 0");
1463 if (oldlen
< len
+ 1) {
1464 p
= realloc (p
, len
+ 1);
1466 err (1, "realloc %d failed", len
+ 1);
1473 if (!strncmp ("open", p
, 4)) {
1476 char *filename
= p
+ 5;
1478 filenamelen
= strlen (filename
);
1479 password
= filename
+ filenamelen
+ 1;
1481 openxref (filename
, password
);
1484 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1485 state
.needoutline
= 1;
1487 else if (!strncmp ("cs", p
, 2)) {
1490 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1492 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1495 set_tex_params (colorspace
);
1496 for (i
= 0; i
< state
.texcount
; ++i
) {
1497 state
.texowners
[i
].w
= -1;
1498 state
.texowners
[i
].slice
= NULL
;
1502 else if (!strncmp ("freepage", p
, 8)) {
1505 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1507 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1511 else if (!strncmp ("freetile", p
, 8)) {
1514 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1516 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1520 else if (!strncmp ("search", p
, 6)) {
1521 int icase
, pageno
, y
, ret
, len2
, forward
;
1525 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1526 &icase
, &pageno
, &y
, &forward
, &len2
);
1528 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1531 pattern
= p
+ 6 + len2
;
1532 ret
= regcomp (&re
, pattern
,
1533 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1538 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1539 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1542 search (&re
, pageno
, y
, forward
);
1546 else if (!strncmp ("geometry", p
, 8)) {
1550 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1552 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1560 for (i
= 0; i
< state
.texcount
; ++i
) {
1561 state
.texowners
[i
].slice
= NULL
;
1567 unlock ("geometry");
1568 printd ("continue %d", state
.pagecount
);
1570 else if (!strncmp ("reqlayout", p
, 9)) {
1571 int rotate
, proportional
;
1574 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1576 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1579 state
.rotate
= rotate
;
1580 state
.proportional
= proportional
;
1582 unlock ("reqlayout");
1583 printd ("continue %d", state
.pagecount
);
1585 else if (!strncmp ("page", p
, 4)) {
1588 int pageno
, pindex
, ret
;
1590 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1592 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1597 page
= loadpage (pageno
, pindex
);
1601 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1603 else if (!strncmp ("tile", p
, 4)) {
1604 int x
, y
, w
, h
, ret
;
1609 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1610 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1612 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1617 tile
= rendertile (page
, x
, y
, w
, h
);
1621 printd ("tile %d %d %" FMT_ptr
" %u %f",
1623 FMT_ptr_cast2 (tile
),
1624 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1627 else if (!strncmp ("settrim", p
, 7)) {
1631 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1632 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1634 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1638 state
.trimmargins
= trimmargins
;
1639 state
.needoutline
= 1;
1640 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1642 state
.trimfuzz
= fuzz
;
1644 state
.pagedimcount
= 0;
1645 free (state
.pagedims
);
1646 state
.pagedims
= NULL
;
1651 printd ("continue %d", state
.pagecount
);
1653 else if (!strncmp ("sliceh", p
, 6)) {
1656 ret
= sscanf (p
+ 6, " %d", &h
);
1658 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1660 if (h
!= state
.sliceheight
) {
1663 state
.sliceheight
= h
;
1664 for (i
= 0; i
< state
.texcount
; ++i
) {
1665 state
.texowners
[i
].w
= -1;
1666 state
.texowners
[i
].h
= -1;
1667 state
.texowners
[i
].slice
= NULL
;
1671 else if (!strncmp ("interrupt", p
, 9)) {
1672 printd ("vmsg interrupted");
1674 else if (!strncmp ("quit", p
, 4)) {
1678 errx (1, "unknown command %.*s", len
, p
);
1684 CAMLprim value
ml_realloctexts (value texcount_v
)
1686 CAMLparam1 (texcount_v
);
1689 if (trylock ("ml_realloctexts")) {
1693 realloctexts (Int_val (texcount_v
));
1695 unlock ("ml_realloctexts");
1698 CAMLreturn (Val_bool (ok
));
1701 static void showsel (struct page
*page
, int ox
, int oy
)
1705 struct mark first
, last
;
1707 first
= page
->fmark
;
1710 if (!first
.span
|| !last
.span
) return;
1712 glEnable (GL_BLEND
);
1713 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1714 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1716 ox
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1717 oy
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1718 for (span
= first
.span
; span
; span
= span
->next
) {
1721 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1726 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1727 j
= MIN (first
.i
, last
.i
);
1728 k
= MAX (first
.i
, last
.i
);
1730 else if (span
== first
.span
) {
1733 else if (span
== last
.span
) {
1737 for (i
= j
; i
<= k
; ++i
) {
1738 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1740 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1747 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1749 if (span
== last
.span
) break;
1751 glDisable (GL_BLEND
);
1754 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1757 fz_link
*link
, *links
;
1759 switch (page
->type
) {
1761 links
= page
->u
.pdfpage
->links
;
1765 links
= page
->u
.xpspage
->links
;
1772 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1773 glEnable (GL_LINE_STIPPLE
);
1774 glLineStipple (0.5, 0xcccc);
1776 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1779 for (link
= links
; link
; link
= link
->next
) {
1780 fz_point p1
, p2
, p3
, p4
;
1782 p1
.x
= link
->rect
.x0
;
1783 p1
.y
= link
->rect
.y0
;
1785 p2
.x
= link
->rect
.x1
;
1786 p2
.y
= link
->rect
.y0
;
1788 p3
.x
= link
->rect
.x1
;
1789 p3
.y
= link
->rect
.y1
;
1791 p4
.x
= link
->rect
.x0
;
1792 p4
.y
= link
->rect
.y1
;
1794 p1
= fz_transform_point (ctm
, p1
);
1795 p2
= fz_transform_point (ctm
, p2
);
1796 p3
= fz_transform_point (ctm
, p3
);
1797 p4
= fz_transform_point (ctm
, p4
);
1799 switch (link
->dest
.kind
) {
1800 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1801 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1802 default: glColor3ub (0, 0, 0); break;
1805 glVertex2f (p1
.x
, p1
.y
);
1806 glVertex2f (p2
.x
, p2
.y
);
1807 glVertex2f (p3
.x
, p3
.y
);
1808 glVertex2f (p4
.x
, p4
.y
);
1812 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1813 glDisable (GL_LINE_STIPPLE
);
1816 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1819 struct slice
*slice1
;
1822 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1823 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1825 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
1826 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1827 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1831 int texindex
= state
.texindex
++ % state
.texcount
;
1833 if (state
.texowners
[texindex
].w
== tile
->w
) {
1834 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1838 state
.texowners
[texindex
].h
= slice
->h
;
1842 state
.texowners
[texindex
].h
= slice
->h
;
1845 state
.texowners
[texindex
].w
= tile
->w
;
1846 state
.texowners
[texindex
].slice
= slice
;
1847 slice
->texindex
= texindex
;
1849 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1851 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1859 tile
->pixmap
->samples
+offset
1863 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1871 tile
->pixmap
->samples
+offset
1877 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1879 CAMLparam2 (args_v
, ptr_v
);
1880 int dispx
= Int_val (Field (args_v
, 0));
1881 int dispy
= Int_val (Field (args_v
, 1));
1882 int dispw
= Int_val (Field (args_v
, 2));
1883 int disph
= Int_val (Field (args_v
, 3));
1884 int tilex
= Int_val (Field (args_v
, 4));
1885 int tiley
= Int_val (Field (args_v
, 5));
1886 char *s
= String_val (ptr_v
);
1887 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1889 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1891 int slicey
, firstslice
;
1892 struct slice
*slice
;
1894 firstslice
= tiley
/ tile
->sliceheight
;
1895 slice
= &tile
->slices
[firstslice
];
1896 slicey
= tiley
% tile
->sliceheight
;
1901 dh
= slice
->h
- slicey
;
1902 dh
= MIN (disph
, dh
);
1903 uploadslice (tile
, slice
);
1907 glTexCoord2i (tilex
, slicey
);
1908 glVertex2i (dispx
, dispy
);
1910 glTexCoord2i (tilex
+dispw
, slicey
);
1911 glVertex2i (dispx
+dispw
, dispy
);
1913 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1914 glVertex2i (dispx
+dispw
, dispy
+dh
);
1916 glTexCoord2i (tilex
, slicey
+dh
);
1917 glVertex2i (dispx
, dispy
+dh
);
1924 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1928 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1929 CAMLreturn (Val_unit
);
1932 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1933 value xoff_v
, value yoff_v
)
1935 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1936 int xoff
= Int_val (xoff_v
);
1937 int yoff
= Int_val (yoff_v
);
1938 char *s
= String_val (ptr_v
);
1939 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1941 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1943 if (trylock ("ml_postprocess")) {
1946 showsel (page
, xoff
, yoff
);
1947 unlock ("ml_postprocess");
1950 CAMLreturn (Val_unit
);
1953 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1957 fz_link
*link
, *links
;
1959 switch (page
->type
) {
1961 links
= page
->u
.pdfpage
->links
;
1965 links
= page
->u
.xpspage
->links
;
1974 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1975 state
.pagedims
[page
->pdimno
].ctm
);
1976 ctm
= fz_invert_matrix (ctm
);
1977 p
= fz_transform_point (ctm
, p
);
1979 for (link
= links
; link
; link
= link
->next
) {
1980 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1981 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1989 static void droptext (struct page
*page
)
1992 fz_free_text_span (state
.ctx
, page
->text
);
1995 page
->fmark
.span
= NULL
;
1996 page
->lmark
.span
= NULL
;
2001 static void ensuretext (struct page
*page
)
2003 if (state
.gen
!= page
->gen
) {
2005 page
->gen
= state
.gen
;
2010 page
->text
= fz_new_text_span (state
.ctx
);
2011 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
2012 fz_run_display_list (page
->dlist
,
2015 fz_infinite_bbox
, NULL
);
2016 fz_free_device (tdev
);
2020 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2022 CAMLparam3 (ptr_v
, x_v
, y_v
);
2023 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2027 char *s
= String_val (ptr_v
);
2028 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2029 struct pagedim
*pdim
;
2031 ret_v
= Val_int (0);
2032 if (trylock ("ml_whatsunder")) {
2036 page
= parse_pointer ("ml_whatsunder", s
);
2037 pdim
= &state
.pagedims
[page
->pdimno
];
2038 x
+= pdim
->bounds
.x0
;
2039 y
+= pdim
->bounds
.y0
;
2040 link
= getlink (page
, x
, y
);
2042 switch (link
->dest
.kind
) {
2047 pageno
= link
->dest
.ld
.gotor
.page
;
2051 if (link
->dest
.ld
.gotor
.flags
& fz_link_flag_t_valid
) {
2052 p
.y
= link
->dest
.ld
.gotor
.lt
.y
;
2053 p
= fz_transform_point (pdim
->lctm
, p
);
2055 tup_v
= caml_alloc_tuple (2);
2056 ret_v
= caml_alloc_small (1, 1);
2057 Field (tup_v
, 0) = Val_int (pageno
);
2058 Field (tup_v
, 1) = Val_int (p
.y
);
2059 Field (ret_v
, 0) = tup_v
;
2064 str_v
= caml_copy_string (link
->dest
.ld
.uri
.uri
);
2065 ret_v
= caml_alloc_small (1, 0);
2066 Field (ret_v
, 0) = str_v
;
2069 case FZ_LINK_LAUNCH
:
2070 str_v
= caml_copy_string (link
->dest
.ld
.launch
.file_spec
);
2071 ret_v
= caml_alloc_small (1, 4);
2072 Field (ret_v
, 0) = str_v
;
2076 str_v
= caml_copy_string (link
->dest
.ld
.named
.named
);
2077 ret_v
= caml_alloc_small (1, 5);
2078 Field (ret_v
, 0) = str_v
;
2082 str_v
= caml_copy_string (link
->dest
.ld
.gotor
.file_spec
);
2083 pageno
= link
->dest
.ld
.gotor
.page
;
2084 tup_v
= caml_alloc_tuple (2);
2085 ret_v
= caml_alloc_small (1, 6);
2086 Field (tup_v
, 0) = str_v
;
2087 Field (tup_v
, 1) = Val_int (pageno
);
2088 Field (ret_v
, 0) = tup_v
;
2095 snprintf (buf
, sizeof (buf
),
2096 "unhandled link kind %d", link
->dest
.kind
);
2097 str_v
= caml_copy_string (buf
);
2098 ret_v
= caml_alloc_small (1, 3);
2099 Field (ret_v
, 0) = str_v
;
2109 for (span
= page
->text
; span
; span
= span
->next
) {
2110 for (i
= 0; i
< span
->len
; ++i
) {
2112 b
= &span
->text
[i
].bbox
;
2113 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
2115 span
->font
&& span
->font
->name
2117 : "Span has no font name"
2119 FT_FaceRec
*face
= span
->font
->ft_face
;
2120 if (face
&& face
->family_name
) {
2122 char *n1
= face
->family_name
;
2123 size_t l1
= strlen (n1
);
2124 size_t l2
= strlen (n2
);
2126 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2127 s
= malloc (l1
+ l2
+ 2);
2131 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2132 str_v
= caml_copy_string (s
);
2138 str_v
= caml_copy_string (n2
);
2140 ret_v
= caml_alloc_small (1, 2);
2141 Field (ret_v
, 0) = str_v
;
2148 unlock ("ml_whatsunder");
2154 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2156 CAMLparam2 (ptr_v
, rect_v
);
2160 struct mark first
, last
;
2161 int i
, x0
, x1
, y0
, y1
;
2162 struct pagedim
*pdim
;
2163 char *s
= String_val (ptr_v
);
2165 if (trylock ("ml_seltext")) {
2169 page
= parse_pointer ("ml_seltext", s
);
2172 pdim
= &state
.pagedims
[page
->pdimno
];
2174 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
2175 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2176 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2177 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2180 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2181 glColor3ub (128, 128, 128);
2182 glRecti (x0
, y0
, x1
, y1
);
2183 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2189 last
.i
= first
.i
= 0;
2190 first
.span
= page
->text
;
2191 for (span
= page
->text
; span
; span
= span
->next
) {
2192 for (i
= 0; i
< span
->len
; ++i
) {
2193 b
= &span
->text
[i
].bbox
;
2196 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
2201 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
2206 if (0 && selected
) {
2207 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2208 glColor3ub (128, 128, 128);
2209 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2210 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2215 if (y1
< y0
|| x1
< x0
) {
2218 if (first
.span
== last
.span
) {
2223 for (span
= first
.span
; span
&& span
!= last
.span
;
2224 span
= span
->next
) {
2237 first
.span
= last
.span
;
2243 page
->fmark
= first
;
2246 unlock ("ml_seltext");
2249 CAMLreturn (Val_unit
);
2252 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2257 for (i
= a
; i
<= b
; ++i
) {
2258 len
= runetochar (buf
, &span
->text
[i
].c
);
2259 ret
= fwrite (buf
, len
, 1, f
);
2262 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2263 len
, ret
, strerror (errno
));
2270 CAMLprim value
ml_copysel (value command_v
, value ptr_v
)
2276 char *s
= String_val (ptr_v
);
2277 char *command
= String_val (command_v
);
2279 if (trylock ("ml_copysel")) {
2284 page
= parse_pointer ("ml_sopysel", s
);
2286 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2287 fprintf (stderr
, "nothing to copy\n");
2291 f
= popen (command
, "w");
2293 fprintf (stderr
, "failed to open sel pipe: %s\n",
2298 for (span
= page
->fmark
.span
;
2299 span
&& span
!= page
->lmark
.span
->next
;
2300 span
= span
->next
) {
2301 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2302 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2303 if (pipespan (f
, span
, a
, b
)) {
2307 if (putc ('\n', f
) == EOF
) {
2308 fprintf (stderr
, "failed break line on sel pipe: %s\n",
2314 page
->lmark
.span
= NULL
;
2315 page
->fmark
.span
= NULL
;
2319 int ret
= pclose (f
);
2321 if (errno
!= ECHILD
) {
2322 fprintf (stderr
, "failed to close sel pipe: %s\n",
2328 unlock ("ml_copysel");
2331 CAMLreturn (Val_unit
);
2334 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2336 CAMLparam1 (pagedimno_v
);
2338 int pagedimno
= Int_val (pagedimno_v
);
2341 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2342 if (trylock ("ml_getpdimrect")) {
2343 box
= fz_empty_rect
;
2346 box
= state
.pagedims
[pagedimno
].mediabox
;
2347 unlock ("ml_getpdimrect");
2350 Store_double_field (ret_v
, 0, box
.x0
);
2351 Store_double_field (ret_v
, 1, box
.x1
);
2352 Store_double_field (ret_v
, 2, box
.y0
);
2353 Store_double_field (ret_v
, 3, box
.y1
);
2358 static double getmaxw (void)
2364 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2367 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2368 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2371 maxw
= MAX (w
, maxw
);
2376 CAMLprim value
ml_getmaxw (value unit_v
)
2378 CAMLparam1 (unit_v
);
2382 if (trylock ("ml_getmaxw")) {
2386 unlock ("ml_getmaxw");
2388 ret_v
= caml_copy_double (maxw
);
2392 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2394 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2398 double maxw
= 0.0, maxh
= 0.0;
2400 double winw
= Int_val (winw_v
);
2401 double winh
= Int_val (winh_v
);
2402 double dw
= Int_val (dw_v
);
2403 double pw
= 1.0, ph
= 1.0, num
, den
;
2405 if (trylock ("ml_zoom_for_height")) {
2409 if (state
.proportional
) {
2413 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2414 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2416 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2417 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2418 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2419 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2424 if (state
.proportional
) {
2426 scaledh
= h
* scale
;
2433 if (scaledh
> maxh
) {
2440 num
= (winh
* pw
) + (ph
* dw
);
2444 unlock ("ml_zoom_for_height");
2446 ret_v
= caml_copy_double (zoom
);
2452 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2454 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2456 int pt
= Int_val(pt_v
);
2457 int x
= Int_val (x_v
);
2458 int y
= Int_val (y_v
);
2461 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2462 ret_v
= caml_copy_double (w
);
2466 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2468 CAMLparam2 (pt_v
, string_v
);
2470 int pt
= Int_val (pt_v
);
2473 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2474 ret_v
= caml_copy_double (w
);
2478 CAMLprim value
ml_getpagebox (value opaque_v
)
2480 CAMLparam1 (opaque_v
);
2484 char *s
= String_val (opaque_v
);
2485 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2487 ret_v
= caml_alloc_tuple (4);
2488 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2489 dev
->hints
|= FZ_IGNORE_SHADE
;
2491 switch (page
->type
) {
2493 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
2497 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
2501 bbox
= fz_infinite_bbox
;
2505 fz_free_device (dev
);
2506 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2507 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2508 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2509 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2514 CAMLprim value
ml_setaalevel (value level_v
)
2516 CAMLparam1 (level_v
);
2518 state
.aalevel
= Int_val (level_v
);
2519 CAMLreturn (Val_unit
);
2522 #if !defined _WIN32 && !defined __APPLE__
2525 #include <X11/Xlib.h>
2526 #include <X11/Xutil.h>
2529 static void set_wm_class (int hack
)
2537 display
= getenv ("DISPLAY");
2538 dpy
= XOpenDisplay (display
);
2540 fprintf (stderr
, "XOpenDisplay `%s' failed\n",
2541 display
? display
: "null");
2544 hint
.res_name
= "llpp";
2545 hint
.res_class
= "llpp";
2546 win
= glXGetCurrentDrawable ();
2548 fprintf (stderr
, "glXGetCurrentDrawable returned None\n");
2549 XCloseDisplay (dpy
);
2552 XSetClassHint (dpy
, win
, &hint
);
2553 XCloseDisplay (dpy
);
2557 #define set_wm_class(h) (void) (h)
2560 enum { piunknown
, pilinux
, piwindows
, piosx
,
2561 pisun
, pifreebsd
, pidragonflybsd
,
2562 piopenbsd
, pinetbsd
, pimingw
, picygwin
};
2564 #define NOZOMBIESPLEASE
2566 CAMLprim value
ml_platform (value unit_v
)
2568 CAMLparam1 (unit_v
);
2569 int platid
= piunknown
;
2571 #if defined __linux__
2573 #elif defined __CYGWIN__
2574 #undef NOZOMBIESPLEASE
2576 #elif defined __MINGW32__
2577 #undef NOZOMBIESPLEASE
2579 #elif defined _WIN32
2580 #undef NOZOMBIESPLEASE
2582 #elif defined __DragonFly__
2583 platid
= pidragonflybsd
;
2584 #elif defined __FreeBSD__
2586 #elif defined __OpenBSD__
2588 #elif defined __NetBSD__
2590 #elif defined __sun__
2592 #elif defined __APPLE__
2595 CAMLreturn (Val_int (platid
));
2598 #ifdef NOZOMBIESPLEASE
2601 static void nozombies (void)
2603 struct sigaction sa
;
2605 sa
.sa_handler
= SIG_DFL
;
2606 if (sigemptyset (&sa
.sa_mask
)) {
2607 err (1, "sigemptyset");
2609 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
2610 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
2611 err (1, "sigaction");
2618 CAMLprim value
ml_init (value pipe_v
, value params_v
)
2620 CAMLparam2 (pipe_v
, params_v
);
2621 CAMLlocal2 (trim_v
, fuzz_v
);
2629 state
.cr
= Handle_val (Field (pipe_v
, 0));
2630 state
.cw
= Handle_val (Field (pipe_v
, 1));
2632 state
.cr
= Int_val (Field (pipe_v
, 0));
2633 state
.cw
= Int_val (Field (pipe_v
, 1));
2635 state
.rotate
= Int_val (Field (params_v
, 0));
2636 state
.proportional
= Bool_val (Field (params_v
, 1));
2637 trim_v
= Field (params_v
, 2);
2638 texcount
= Int_val (Field (params_v
, 3));
2639 state
.sliceheight
= Int_val (Field (params_v
, 4));
2640 mustoresize
= Int_val (Field (params_v
, 5));
2641 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
2642 colorspace
= Int_val (Field (params_v
, 6));
2643 wmclasshack
= Bool_val (Field (params_v
, 7));
2644 fontpath
= String_val (Field (params_v
, 8));
2646 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2647 fuzz_v
= Field (trim_v
, 1);
2648 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2649 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2650 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2651 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2653 set_tex_params (colorspace
);
2654 set_wm_class (wmclasshack
);
2657 state
.face
= load_font (fontpath
);
2661 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2663 state
.face
= load_builtin_font (base
, len
);
2665 if (!state
.face
) _exit (1);
2667 realloctexts (texcount
);
2672 InitializeCriticalSection (&critsec
);
2673 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2674 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2675 errx (1, "CreateThread failed: %lx", GetLastError ());
2679 int ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2681 errx (1, "pthread_create: %s", strerror (ret
));
2686 CAMLreturn (Val_unit
);