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=%ld", ret
);
357 return ret
== WAIT_OBJECT_0
;
360 static void readdata (void *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)
415 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
416 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
420 static void readdata (void *p
, int size
)
424 n
= read (state
.cr
, p
, size
);
426 if (!n
) errx (1, "EOF while reading");
427 err (1, "read (req %d, ret %zd)", size
, n
);
431 static int readlen (void)
436 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
439 static void writedata (char *p
, int size
)
444 buf
[0] = (size
>> 24) & 0xff;
445 buf
[1] = (size
>> 16) & 0xff;
446 buf
[2] = (size
>> 8) & 0xff;
447 buf
[3] = (size
>> 0) & 0xff;
449 n
= write (state
.cw
, buf
, 4);
451 if (!n
) errx (1, "EOF while writing length");
452 err (1, "write %zd", n
);
455 n
= write (state
.cw
, p
, size
);
457 if (!n
) errx (1, "EOF while writing data");
458 err (1, "write (req %d, ret %zd)", size
, n
);
463 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
471 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
474 len
= vsnprintf (buf
, size
, fmt
, ap
);
477 if (len
> -1 && len
< size
) {
478 writedata (buf
, len
);
488 buf
= realloc (buf
, size
);
493 static void closepdf (void)
496 pdf_close_document (state
.u
.pdf
);
501 static void closexps (void)
504 xps_close_document (state
.u
.xps
);
509 static void closecbz (void)
512 cbz_close_document (state
.u
.cbz
);
517 static void freepdfpage (void *ptr
)
519 pdf_free_page (state
.u
.pdf
, ptr
);
522 static void freexpspage (void *ptr
)
524 xps_free_page (state
.u
.xps
, ptr
);
527 static void freecbzpage (void *ptr
)
529 cbz_free_page (state
.u
.cbz
, ptr
);
532 static void openxref (char *filename
, char *password
)
536 for (i
= 0; i
< state
.texcount
; ++i
) {
537 state
.texowners
[i
].w
= -1;
538 state
.texowners
[i
].slice
= NULL
;
541 if (state
.closedoc
) state
.closedoc ();
543 len
= strlen (filename
);
549 ext
[0] = tolower (filename
[len
-3]);
550 ext
[1] = tolower (filename
[len
-2]);
551 ext
[2] = tolower (filename
[len
-1]);
553 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
556 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
561 if (state
.pagedims
) {
562 free (state
.pagedims
);
563 state
.pagedims
= NULL
;
565 state
.pagedimcount
= 0;
567 fz_set_aa_level (state
.ctx
, state
.aalevel
);
568 switch (state
.type
) {
570 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
571 if (pdf_needs_password (state
.u
.pdf
)) {
572 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
574 errx (1, "invalid password");
577 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
578 state
.closedoc
= closepdf
;
579 state
.freepage
= freepdfpage
;
583 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
584 state
.pagecount
= xps_count_pages (state
.u
.xps
);
585 state
.closedoc
= closexps
;
586 state
.freepage
= freexpspage
;
590 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
591 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
592 state
.closedoc
= closecbz
;
593 state
.freepage
= freecbzpage
;
598 static void pdfinfo (void)
600 if (state
.type
== DPDF
) {
603 printd ("info PDF version\t%d.%d",
604 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
606 infoobj
= fz_dict_gets (state
.u
.pdf
->trailer
, "Info");
610 char *items
[] = { "Title", "Author", "Creator",
611 "Producer", "CreationDate" };
613 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
614 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
615 s
= pdf_to_utf8 (state
.ctx
, obj
);
618 printd ("title %s", s
);
620 printd ("info %s\t%s", items
[i
], s
);
622 fz_free (state
.ctx
, s
);
629 static void unlinktile (struct tile
*tile
)
633 for (i
= 0; i
< tile
->slicecount
; ++i
) {
634 struct slice
*s
= &tile
->slices
[i
];
636 if (s
->texindex
!= -1) {
637 if (state
.texowners
[s
->texindex
].slice
== s
) {
638 state
.texowners
[s
->texindex
].slice
= NULL
;
644 static void freepage (struct page
*page
)
647 fz_free_text_span (state
.ctx
, page
->text
);
649 page
->freepage (page
->u
.ptr
);
650 fz_free_display_list (state
.ctx
, page
->dlist
);
654 static void freetile (struct tile
*tile
)
658 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
661 fz_drop_pixmap (state
.ctx
, state
.pig
);
663 state
.pig
= tile
->pixmap
;
671 static int cacheline32bytes
;
672 extern char **environ
;
674 static void __attribute__ ((constructor
)) clcheck (void)
676 char **envp
= environ
;
681 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
683 cacheline32bytes
= auxv
[1] == 32;
689 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
691 if (cacheline32bytes
) {
692 intptr_t a1
, a2
, diff
;
693 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
694 vector
unsigned char v
= vec_splat_u8 (-1);
695 vector
unsigned char *p
;
697 a1
= a2
= (intptr_t) pixmap
->samples
;
698 a2
= (a1
+ 31) & ~31;
703 while (a1
!= a2
) *(char *) a1
++ = 0xff;
704 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
705 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
707 vec_st (v
, i
+ 16, p
);
709 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
711 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
714 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
717 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
720 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
722 if (!pdim
->tctmready
) {
723 if (state
.trimmargins
) {
726 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
727 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
728 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
729 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
740 static fz_matrix
pagectm (struct page
*page
)
742 if (page
->type
== DPDF
) {
743 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
744 state
.pagedims
[page
->pdimno
].ctm
);
748 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
750 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
751 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
752 -pdim
->mediabox
.y0
), ctm
);
757 static void *loadpage (int pageno
, int pindex
)
760 struct page
*page
= NULL
;
762 page
= calloc (sizeof (struct page
), 1);
764 err (1, "calloc page %d", pageno
);
767 page
->dlist
= fz_new_display_list (state
.ctx
);
768 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
769 switch (state
.type
) {
771 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
772 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
773 page
->freepage
= freepdfpage
;
777 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
778 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
779 page
->freepage
= freexpspage
;
783 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
784 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
785 page
->freepage
= freecbzpage
;
788 fz_free_device (dev
);
790 page
->pdimno
= pindex
;
791 page
->pageno
= pageno
;
792 page
->gen
= state
.gen
;
793 page
->type
= state
.type
;
798 static struct tile
*alloctile (int h
)
805 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
806 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
807 tile
= calloc (tilesize
, 1);
809 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
811 for (i
= 0; i
< slicecount
; ++i
) {
812 int sh
= MIN (h
, state
.sliceheight
);
813 tile
->slices
[i
].h
= sh
;
814 tile
->slices
[i
].texindex
= -1;
817 tile
->slicecount
= slicecount
;
818 tile
->sliceheight
= state
.sliceheight
;
822 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
827 struct pagedim
*pdim
;
829 tile
= alloctile (h
);
830 pdim
= &state
.pagedims
[page
->pdimno
];
835 bbox
.x1
= bbox
.x0
+ w
;
836 bbox
.y1
= bbox
.y0
+ h
;
839 if (state
.pig
->w
== w
841 && state
.pig
->colorspace
== state
.colorspace
) {
842 tile
->pixmap
= state
.pig
;
843 tile
->pixmap
->x
= bbox
.x0
;
844 tile
->pixmap
->y
= bbox
.y0
;
847 fz_drop_pixmap (state
.ctx
, state
.pig
);
853 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
858 clearpixmap (tile
->pixmap
);
859 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
860 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
861 fz_free_device (dev
);
866 static void initpdims (void)
872 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
878 switch (state
.type
) {
880 pageobj
= state
.u
.pdf
->page_objs
[pageno
];
882 if (state
.trimmargins
) {
886 page
= pdf_load_page (state
.u
.pdf
, pageno
);
887 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
888 if (state
.trimanew
|| !obj
) {
894 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
895 dev
->hints
|= FZ_IGNORE_SHADE
;
896 ctm
= fz_invert_matrix (page
->ctm
);
897 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
898 fz_free_device (dev
);
900 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
901 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
902 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
903 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
904 rect
= fz_transform_rect (ctm
, rect
);
905 rect
= fz_intersect_rect (rect
, page
->mediabox
);
907 if (fz_is_empty_rect (rect
)) {
908 mediabox
= page
->mediabox
;
914 obj
= fz_new_array (state
.ctx
, 4);
915 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
916 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
917 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
918 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
919 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
922 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
923 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
924 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
925 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
928 rotate
= page
->rotate
;
929 pdf_free_page (state
.u
.pdf
, page
);
931 printd ("progress %f Trimming %d",
932 (double) (pageno
+ 1) / state
.pagecount
,
938 mediabox
= pdf_to_rect (state
.ctx
,
939 fz_dict_gets (pageobj
, "MediaBox"));
940 if (fz_is_empty_rect (mediabox
)) {
941 fprintf (stderr
, "cannot find page size for page %d\n",
949 cropbox
= pdf_to_rect (state
.ctx
,
950 fz_dict_gets (pageobj
, "CropBox"));
951 if (!fz_is_empty_rect (cropbox
)) {
952 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
954 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
962 page
= xps_load_page (state
.u
.xps
, pageno
);
963 mediabox
= xps_bound_page (state
.u
.xps
, page
);
965 if (state
.trimmargins
) {
970 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
971 dev
->hints
|= FZ_IGNORE_SHADE
;
972 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
973 fz_free_device (dev
);
975 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
976 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
977 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
978 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
979 rect
= fz_intersect_rect (rect
, mediabox
);
981 if (!fz_is_empty_rect (rect
)) {
985 xps_free_page (state
.u
.xps
, page
);
986 printd ("progress %f loading %d",
987 (double) (pageno
+ 1) / state
.pagecount
,
995 if (state
.trimmargins
) {
998 page
= cbz_load_page (state
.u
.cbz
, pageno
);
999 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
1000 cbz_free_page (state
.u
.cbz
, page
);
1001 printd ("progress %f Trimming %d",
1002 (double) (pageno
+ 1) / state
.pagecount
,
1006 mediabox
.x0
= mediabox
.y0
= 0;
1014 ARSERT (0 && state
.type
);
1017 if (state
.pagedimcount
== 0
1018 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1019 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1022 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1023 state
.pagedims
= realloc (state
.pagedims
, size
);
1024 if (!state
.pagedims
) {
1025 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1026 size
, state
.pagedimcount
+ 1);
1029 p
= &state
.pagedims
[state
.pagedimcount
++];
1031 p
->mediabox
= mediabox
;
1036 if (state
.trimmargins
) {
1037 printd ("progress 1 Trimmed %d pages in %f seconds",
1038 state
.pagecount
, end
- start
);
1041 printd ("vmsg Processed %d pages in %f seconds",
1042 state
.pagecount
, end
- start
);
1047 static void layout (void)
1052 double zoom
, w
, maxw
= 0;
1053 struct pagedim
*p
= state
.pagedims
;
1055 if (state
.proportional
) {
1056 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1057 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1059 w
= box
.x1
- box
.x0
;
1060 maxw
= MAX (w
, maxw
);
1065 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1068 ctm
= fz_rotate (state
.rotate
);
1069 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
1071 w
= box
.x1
- box
.x0
;
1073 if (state
.proportional
) {
1074 double scale
= w
/ maxw
;
1075 zoom
= (state
.w
/ w
) * scale
;
1081 p
->zoomctm
= fz_scale (zoom
, zoom
);
1082 ctm
= fz_concat (p
->zoomctm
, ctm
);
1084 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
1085 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1086 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1089 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
1092 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1096 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
1097 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
1098 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
1104 while (p
-- != state
.pagedims
) {
1105 int w
= p
->bounds
.x1
- p
->bounds
.x0
;
1106 int h
= p
->bounds
.y1
- p
->bounds
.y0
;
1108 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1112 static void recurse_outline (fz_outline
*outline
, int level
)
1117 struct pagedim
*pdim
= state
.pagedims
;
1119 dest
= &outline
->dest
;
1120 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1121 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1123 pdim
= &state
.pagedims
[i
];
1125 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1128 p
.y
= dest
->ld
.gotor
.lt
.y
;
1129 p
= fz_transform_point (pdim
->lctm
, p
);
1132 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1136 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1137 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1139 printd ("o %d %d %d %d %s",
1140 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1142 if (outline
->down
) {
1143 recurse_outline (outline
->down
, level
+ 1);
1145 outline
= outline
->next
;
1149 static void process_outline (void)
1151 fz_outline
*outline
;
1153 if (!state
.needoutline
) return;
1155 state
.needoutline
= 0;
1156 switch (state
.type
) {
1158 outline
= pdf_load_outline (state
.u
.pdf
);
1161 outline
= xps_load_outline (state
.u
.xps
);
1168 recurse_outline (outline
, 0);
1169 fz_free_outline (state
.ctx
, outline
);
1173 static int comparespans (const void *l
, const void *r
)
1175 fz_text_span
const *const*ls
= l
;
1176 fz_text_span
const *const*rs
= r
;
1177 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
1180 /* wishful thinking function */
1181 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1189 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1190 fz_text_span
*text
, *span
, **pspan
;
1191 struct pagedim
*pdim
, *pdimprev
;
1197 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1201 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1202 if (niters
++ == 5) {
1205 printd ("progress 1 attention requested aborting search at %d",
1210 printd ("progress %f searching in page %d",
1211 (double) (pageno
+ 1) / state
.pagecount
,
1216 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1217 pdim
= &state
.pagedims
[i
];
1218 if (pdim
->pageno
== pageno
) {
1221 if (pdim
->pageno
> pageno
) {
1230 text
= fz_new_text_span (state
.ctx
);
1231 tdev
= fz_new_text_device (state
.ctx
, text
);
1233 switch (state
.type
) {
1235 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1236 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, fz_identity
, NULL
);
1240 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1241 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, fz_identity
, NULL
);
1245 ARSERT (0 && state
.type
);
1248 fz_free_device (tdev
);
1251 for (span
= text
; span
; span
= span
->next
) {
1254 pspan
= malloc (sizeof (void *) * nspans
);
1256 err (1, "malloc span pointers %" FMT_s
, sizeof (void *) * nspans
);
1258 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1261 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1263 j
= forward
? 0 : nspans
- 1;
1268 j
+= forward
? 1 : -1;
1270 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
1272 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1277 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1281 if (span
->text
[i
].c
< 256) {
1282 *p
++ = span
->text
[i
].c
;
1293 ret
= regexec (re
, buf
, 1, &rm
, 0);
1295 if (ret
!= REG_NOMATCH
) {
1298 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1299 printd ("msg regexec error `%.*s'",
1300 (int) size
, errbuf
);
1301 fz_free_text_span (state
.ctx
, text
);
1302 state
.freepage (u
.ptr
);
1309 fz_point p1
, p2
, p3
, p4
;
1311 sb
= &span
->text
[rm
.rm_so
].bbox
;
1312 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1323 switch (state
.type
) {
1325 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1326 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1334 p1
= fz_transform_point (ctm
, p1
);
1335 p2
= fz_transform_point (ctm
, p2
);
1336 p3
= fz_transform_point (ctm
, p3
);
1337 p4
= fz_transform_point (ctm
, p4
);
1340 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1347 printd ("progress 1 found at %d `%.*s' in %f sec",
1348 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1352 printd ("match %d %d %f %f %f %f %f %f %f %f",
1370 fz_free_text_span (state
.ctx
, text
);
1371 state
.freepage (u
.ptr
);
1376 printd ("progress 1 no matches %f sec", end
- start
);
1378 printd ("clearrects");
1381 static void set_tex_params (int colorspace
)
1383 switch (colorspace
) {
1385 state
.texiform
= GL_RGBA8
;
1386 state
.texform
= GL_RGBA
;
1387 state
.texty
= GL_UNSIGNED_BYTE
;
1388 state
.colorspace
= fz_device_rgb
;
1391 state
.texiform
= GL_RGBA8
;
1392 state
.texform
= GL_BGRA
;
1393 state
.texty
= fz_is_big_endian ()
1394 ? GL_UNSIGNED_INT_8_8_8_8
1395 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1396 state
.colorspace
= fz_device_bgr
;
1399 state
.texiform
= GL_LUMINANCE_ALPHA
;
1400 state
.texform
= GL_LUMINANCE_ALPHA
;
1401 state
.texty
= GL_UNSIGNED_BYTE
;
1402 state
.colorspace
= fz_device_gray
;
1405 errx (1, "invalid colorspce %d", colorspace
);
1409 static void realloctexts (int texcount
)
1413 if (texcount
== state
.texcount
) return;
1415 if (texcount
< state
.texcount
) {
1416 glDeleteTextures (state
.texcount
- texcount
,
1417 state
.texids
+ texcount
);
1420 size
= texcount
* sizeof (*state
.texids
);
1421 state
.texids
= realloc (state
.texids
, size
);
1422 if (!state
.texids
) {
1423 err (1, "realloc texids %" FMT_s
, size
);
1426 size
= texcount
* sizeof (*state
.texowners
);
1427 state
.texowners
= realloc (state
.texowners
, size
);
1428 if (!state
.texowners
) {
1429 err (1, "realloc texowners %" FMT_s
, size
);
1431 if (texcount
> state
.texcount
) {
1434 glGenTextures (texcount
- state
.texcount
,
1435 state
.texids
+ state
.texcount
);
1436 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1437 state
.texowners
[i
].w
= -1;
1438 state
.texowners
[i
].slice
= NULL
;
1441 state
.texcount
= texcount
;
1451 mainloop (void *unused
)
1454 int len
, ret
, oldlen
= 0;
1459 errx (1, "readlen returned 0");
1462 if (oldlen
< len
+ 1) {
1463 p
= realloc (p
, len
+ 1);
1465 err (1, "realloc %d failed", len
+ 1);
1472 if (!strncmp ("open", p
, 4)) {
1475 char *filename
= p
+ 5;
1477 filenamelen
= strlen (filename
);
1478 password
= filename
+ filenamelen
+ 1;
1480 openxref (filename
, password
);
1483 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1484 state
.needoutline
= 1;
1486 else if (!strncmp ("cs", p
, 2)) {
1489 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1491 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1494 set_tex_params (colorspace
);
1495 for (i
= 0; i
< state
.texcount
; ++i
) {
1496 state
.texowners
[i
].w
= -1;
1497 state
.texowners
[i
].slice
= NULL
;
1501 else if (!strncmp ("freepage", p
, 8)) {
1504 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1506 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1510 else if (!strncmp ("freetile", p
, 8)) {
1513 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1515 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1519 else if (!strncmp ("search", p
, 6)) {
1520 int icase
, pageno
, y
, ret
, len2
, forward
;
1524 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1525 &icase
, &pageno
, &y
, &forward
, &len2
);
1527 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1530 pattern
= p
+ 6 + len2
;
1531 ret
= regcomp (&re
, pattern
,
1532 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1537 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1538 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1541 search (&re
, pageno
, y
, forward
);
1545 else if (!strncmp ("geometry", p
, 8)) {
1549 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1551 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1559 for (i
= 0; i
< state
.texcount
; ++i
) {
1560 state
.texowners
[i
].slice
= NULL
;
1566 unlock ("geometry");
1567 printd ("continue %d", state
.pagecount
);
1569 else if (!strncmp ("reqlayout", p
, 9)) {
1570 int rotate
, proportional
;
1573 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1575 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1578 state
.rotate
= rotate
;
1579 state
.proportional
= proportional
;
1581 unlock ("reqlayout");
1582 printd ("continue %d", state
.pagecount
);
1584 else if (!strncmp ("page", p
, 4)) {
1587 int pageno
, pindex
, ret
;
1589 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1591 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1596 page
= loadpage (pageno
, pindex
);
1600 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1602 else if (!strncmp ("tile", p
, 4)) {
1603 int x
, y
, w
, h
, ret
;
1608 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1609 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1611 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1616 tile
= rendertile (page
, x
, y
, w
, h
);
1620 printd ("tile %d %d %" FMT_ptr
" %u %f",
1622 FMT_ptr_cast2 (tile
),
1623 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1626 else if (!strncmp ("settrim", p
, 7)) {
1630 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1631 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1633 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1637 state
.trimmargins
= trimmargins
;
1638 state
.needoutline
= 1;
1639 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1641 state
.trimfuzz
= fuzz
;
1643 state
.pagedimcount
= 0;
1644 free (state
.pagedims
);
1645 state
.pagedims
= NULL
;
1650 printd ("continue %d", state
.pagecount
);
1652 else if (!strncmp ("sliceh", p
, 6)) {
1655 ret
= sscanf (p
+ 6, " %d", &h
);
1657 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1659 if (h
!= state
.sliceheight
) {
1662 state
.sliceheight
= h
;
1663 for (i
= 0; i
< state
.texcount
; ++i
) {
1664 state
.texowners
[i
].w
= -1;
1665 state
.texowners
[i
].h
= -1;
1666 state
.texowners
[i
].slice
= NULL
;
1670 else if (!strncmp ("interrupt", p
, 9)) {
1671 printd ("vmsg interrupted");
1673 else if (!strncmp ("quit", p
, 4)) {
1677 errx (1, "unknown command %.*s", len
, p
);
1683 CAMLprim value
ml_realloctexts (value texcount_v
)
1685 CAMLparam1 (texcount_v
);
1688 if (trylock ("ml_realloctexts")) {
1692 realloctexts (Int_val (texcount_v
));
1694 unlock ("ml_realloctexts");
1697 CAMLreturn (Val_bool (ok
));
1700 static void showsel (struct page
*page
, int ox
, int oy
)
1704 struct mark first
, last
;
1706 first
= page
->fmark
;
1709 if (!first
.span
|| !last
.span
) return;
1711 glEnable (GL_BLEND
);
1712 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1713 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1715 ox
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1716 oy
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1717 for (span
= first
.span
; span
; span
= span
->next
) {
1720 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1725 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1726 j
= MIN (first
.i
, last
.i
);
1727 k
= MAX (first
.i
, last
.i
);
1729 else if (span
== first
.span
) {
1732 else if (span
== last
.span
) {
1736 for (i
= j
; i
<= k
; ++i
) {
1737 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1739 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1746 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1748 if (span
== last
.span
) break;
1750 glDisable (GL_BLEND
);
1753 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1756 fz_link
*link
, *links
;
1758 switch (page
->type
) {
1760 links
= page
->u
.pdfpage
->links
;
1764 links
= page
->u
.xpspage
->links
;
1771 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1772 glEnable (GL_LINE_STIPPLE
);
1773 glLineStipple (0.5, 0xcccc);
1775 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1778 for (link
= links
; link
; link
= link
->next
) {
1779 fz_point p1
, p2
, p3
, p4
;
1781 p1
.x
= link
->rect
.x0
;
1782 p1
.y
= link
->rect
.y0
;
1784 p2
.x
= link
->rect
.x1
;
1785 p2
.y
= link
->rect
.y0
;
1787 p3
.x
= link
->rect
.x1
;
1788 p3
.y
= link
->rect
.y1
;
1790 p4
.x
= link
->rect
.x0
;
1791 p4
.y
= link
->rect
.y1
;
1793 p1
= fz_transform_point (ctm
, p1
);
1794 p2
= fz_transform_point (ctm
, p2
);
1795 p3
= fz_transform_point (ctm
, p3
);
1796 p4
= fz_transform_point (ctm
, p4
);
1798 switch (link
->dest
.kind
) {
1799 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1800 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1801 default: glColor3ub (0, 0, 0); break;
1804 glVertex2f (p1
.x
, p1
.y
);
1805 glVertex2f (p2
.x
, p2
.y
);
1806 glVertex2f (p3
.x
, p3
.y
);
1807 glVertex2f (p4
.x
, p4
.y
);
1811 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1812 glDisable (GL_LINE_STIPPLE
);
1815 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1818 struct slice
*slice1
;
1821 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1822 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1824 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
1825 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1826 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1830 int texindex
= state
.texindex
++ % state
.texcount
;
1832 if (state
.texowners
[texindex
].w
== tile
->w
) {
1833 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1837 state
.texowners
[texindex
].h
= slice
->h
;
1841 state
.texowners
[texindex
].h
= slice
->h
;
1844 state
.texowners
[texindex
].w
= tile
->w
;
1845 state
.texowners
[texindex
].slice
= slice
;
1846 slice
->texindex
= texindex
;
1848 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1850 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1858 tile
->pixmap
->samples
+offset
1862 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1870 tile
->pixmap
->samples
+offset
1876 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1878 CAMLparam2 (args_v
, ptr_v
);
1879 int dispx
= Int_val (Field (args_v
, 0));
1880 int dispy
= Int_val (Field (args_v
, 1));
1881 int dispw
= Int_val (Field (args_v
, 2));
1882 int disph
= Int_val (Field (args_v
, 3));
1883 int tilex
= Int_val (Field (args_v
, 4));
1884 int tiley
= Int_val (Field (args_v
, 5));
1885 char *s
= String_val (ptr_v
);
1886 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1888 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1890 int slicey
, firstslice
;
1891 struct slice
*slice
;
1893 firstslice
= tiley
/ tile
->sliceheight
;
1894 slice
= &tile
->slices
[firstslice
];
1895 slicey
= tiley
% tile
->sliceheight
;
1900 dh
= slice
->h
- slicey
;
1901 dh
= MIN (disph
, dh
);
1902 uploadslice (tile
, slice
);
1906 glTexCoord2i (tilex
, slicey
);
1907 glVertex2i (dispx
, dispy
);
1909 glTexCoord2i (tilex
+dispw
, slicey
);
1910 glVertex2i (dispx
+dispw
, dispy
);
1912 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1913 glVertex2i (dispx
+dispw
, dispy
+dh
);
1915 glTexCoord2i (tilex
, slicey
+dh
);
1916 glVertex2i (dispx
, dispy
+dh
);
1923 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1927 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1928 CAMLreturn (Val_unit
);
1931 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1932 value xoff_v
, value yoff_v
)
1934 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1935 int xoff
= Int_val (xoff_v
);
1936 int yoff
= Int_val (yoff_v
);
1937 char *s
= String_val (ptr_v
);
1938 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1940 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1942 if (trylock ("ml_postprocess")) {
1945 showsel (page
, xoff
, yoff
);
1946 unlock ("ml_postprocess");
1949 CAMLreturn (Val_unit
);
1952 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1956 fz_link
*link
, *links
;
1958 switch (page
->type
) {
1960 links
= page
->u
.pdfpage
->links
;
1964 links
= page
->u
.xpspage
->links
;
1973 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1974 state
.pagedims
[page
->pdimno
].ctm
);
1975 ctm
= fz_invert_matrix (ctm
);
1976 p
= fz_transform_point (ctm
, p
);
1978 for (link
= links
; link
; link
= link
->next
) {
1979 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1980 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1988 static void droptext (struct page
*page
)
1991 fz_free_text_span (state
.ctx
, page
->text
);
1994 page
->fmark
.span
= NULL
;
1995 page
->lmark
.span
= NULL
;
2000 static void ensuretext (struct page
*page
)
2002 if (state
.gen
!= page
->gen
) {
2004 page
->gen
= state
.gen
;
2009 page
->text
= fz_new_text_span (state
.ctx
);
2010 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
2011 fz_run_display_list (page
->dlist
,
2014 fz_infinite_bbox
, NULL
);
2015 fz_free_device (tdev
);
2019 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2021 CAMLparam3 (ptr_v
, x_v
, y_v
);
2022 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2026 char *s
= String_val (ptr_v
);
2027 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2028 struct pagedim
*pdim
;
2030 ret_v
= Val_int (0);
2031 if (trylock ("ml_whatsunder")) {
2035 page
= parse_pointer ("ml_whatsunder", s
);
2036 pdim
= &state
.pagedims
[page
->pdimno
];
2037 x
+= pdim
->bounds
.x0
;
2038 y
+= pdim
->bounds
.y0
;
2039 link
= getlink (page
, x
, y
);
2041 switch (link
->dest
.kind
) {
2046 pageno
= link
->dest
.ld
.gotor
.page
;
2050 if (link
->dest
.ld
.gotor
.flags
& fz_link_flag_t_valid
) {
2051 p
.y
= link
->dest
.ld
.gotor
.lt
.y
;
2052 p
= fz_transform_point (pdim
->lctm
, p
);
2054 tup_v
= caml_alloc_tuple (2);
2055 ret_v
= caml_alloc_small (1, 1);
2056 Field (tup_v
, 0) = Val_int (pageno
);
2057 Field (tup_v
, 1) = Val_int (p
.y
);
2058 Field (ret_v
, 0) = tup_v
;
2063 str_v
= caml_copy_string (link
->dest
.ld
.uri
.uri
);
2064 ret_v
= caml_alloc_small (1, 0);
2065 Field (ret_v
, 0) = str_v
;
2068 case FZ_LINK_LAUNCH
:
2069 str_v
= caml_copy_string (link
->dest
.ld
.launch
.file_spec
);
2070 ret_v
= caml_alloc_small (1, 4);
2071 Field (ret_v
, 0) = str_v
;
2075 str_v
= caml_copy_string (link
->dest
.ld
.named
.named
);
2076 ret_v
= caml_alloc_small (1, 5);
2077 Field (ret_v
, 0) = str_v
;
2081 str_v
= caml_copy_string (link
->dest
.ld
.gotor
.file_spec
);
2082 pageno
= link
->dest
.ld
.gotor
.page
;
2083 tup_v
= caml_alloc_tuple (2);
2084 ret_v
= caml_alloc_small (1, 6);
2085 Field (tup_v
, 0) = str_v
;
2086 Field (tup_v
, 1) = Val_int (pageno
);
2087 Field (ret_v
, 0) = tup_v
;
2094 snprintf (buf
, sizeof (buf
),
2095 "unhandled link kind %d", link
->dest
.kind
);
2096 str_v
= caml_copy_string (buf
);
2097 ret_v
= caml_alloc_small (1, 3);
2098 Field (ret_v
, 0) = str_v
;
2108 for (span
= page
->text
; span
; span
= span
->next
) {
2109 for (i
= 0; i
< span
->len
; ++i
) {
2111 b
= &span
->text
[i
].bbox
;
2112 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
2114 span
->font
&& span
->font
->name
2116 : "Span has no font name"
2118 FT_FaceRec
*face
= span
->font
->ft_face
;
2119 if (face
&& face
->family_name
) {
2121 char *n1
= face
->family_name
;
2122 size_t l1
= strlen (n1
);
2123 size_t l2
= strlen (n2
);
2125 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2126 s
= malloc (l1
+ l2
+ 2);
2130 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2131 str_v
= caml_copy_string (s
);
2137 str_v
= caml_copy_string (n2
);
2139 ret_v
= caml_alloc_small (1, 2);
2140 Field (ret_v
, 0) = str_v
;
2147 unlock ("ml_whatsunder");
2153 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2155 CAMLparam2 (ptr_v
, rect_v
);
2159 struct mark first
, last
;
2160 int i
, x0
, x1
, y0
, y1
;
2161 struct pagedim
*pdim
;
2162 char *s
= String_val (ptr_v
);
2164 if (trylock ("ml_seltext")) {
2168 page
= parse_pointer ("ml_seltext", s
);
2171 pdim
= &state
.pagedims
[page
->pdimno
];
2173 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
2174 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2175 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2176 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2179 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2180 glColor3ub (128, 128, 128);
2181 glRecti (x0
, y0
, x1
, y1
);
2182 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2188 last
.i
= first
.i
= 0;
2189 first
.span
= page
->text
;
2190 for (span
= page
->text
; span
; span
= span
->next
) {
2191 for (i
= 0; i
< span
->len
; ++i
) {
2192 b
= &span
->text
[i
].bbox
;
2195 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
2200 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
2205 if (0 && selected
) {
2206 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2207 glColor3ub (128, 128, 128);
2208 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2209 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2214 if (y1
< y0
|| x1
< x0
) {
2217 if (first
.span
== last
.span
) {
2222 for (span
= first
.span
; span
&& span
!= last
.span
;
2223 span
= span
->next
) {
2236 first
.span
= last
.span
;
2242 page
->fmark
= first
;
2245 unlock ("ml_seltext");
2248 CAMLreturn (Val_unit
);
2251 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2256 for (i
= a
; i
<= b
; ++i
) {
2257 len
= runetochar (buf
, &span
->text
[i
].c
);
2258 ret
= fwrite (buf
, len
, 1, f
);
2261 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2262 len
, ret
, strerror (errno
));
2269 CAMLprim value
ml_copysel (value command_v
, value ptr_v
)
2275 char *s
= String_val (ptr_v
);
2276 char *command
= String_val (command_v
);
2278 if (trylock ("ml_copysel")) {
2283 page
= parse_pointer ("ml_sopysel", s
);
2285 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2286 fprintf (stderr
, "nothing to copy\n");
2290 f
= popen (command
, "w");
2292 fprintf (stderr
, "failed to open sel pipe: %s\n",
2297 for (span
= page
->fmark
.span
;
2298 span
&& span
!= page
->lmark
.span
->next
;
2299 span
= span
->next
) {
2300 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2301 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2302 if (pipespan (f
, span
, a
, b
)) {
2306 if (putc ('\n', f
) == EOF
) {
2307 fprintf (stderr
, "failed break line on sel pipe: %s\n",
2313 page
->lmark
.span
= NULL
;
2314 page
->fmark
.span
= NULL
;
2318 int ret
= pclose (f
);
2320 if (errno
!= ECHILD
) {
2321 fprintf (stderr
, "failed to close sel pipe: %s\n",
2327 unlock ("ml_copysel");
2330 CAMLreturn (Val_unit
);
2333 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2335 CAMLparam1 (pagedimno_v
);
2337 int pagedimno
= Int_val (pagedimno_v
);
2340 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2341 if (trylock ("ml_getpdimrect")) {
2342 box
= fz_empty_rect
;
2345 box
= state
.pagedims
[pagedimno
].mediabox
;
2346 unlock ("ml_getpdimrect");
2349 Store_double_field (ret_v
, 0, box
.x0
);
2350 Store_double_field (ret_v
, 1, box
.x1
);
2351 Store_double_field (ret_v
, 2, box
.y0
);
2352 Store_double_field (ret_v
, 3, box
.y1
);
2357 static double getmaxw (void)
2363 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2366 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2367 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2370 maxw
= MAX (w
, maxw
);
2375 CAMLprim value
ml_getmaxw (value unit_v
)
2377 CAMLparam1 (unit_v
);
2381 if (trylock ("ml_getmaxw")) {
2385 unlock ("ml_getmaxw");
2387 ret_v
= caml_copy_double (maxw
);
2391 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2393 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2397 double maxw
= 0.0, maxh
= 0.0;
2399 double winw
= Int_val (winw_v
);
2400 double winh
= Int_val (winh_v
);
2401 double dw
= Int_val (dw_v
);
2402 double pw
= 1.0, ph
= 1.0, num
, den
;
2404 if (trylock ("ml_zoom_for_height")) {
2408 if (state
.proportional
) {
2412 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2413 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2415 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2416 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2417 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2418 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2423 if (state
.proportional
) {
2425 scaledh
= h
* scale
;
2432 if (scaledh
> maxh
) {
2439 num
= (winh
* pw
) + (ph
* dw
);
2443 unlock ("ml_zoom_for_height");
2445 ret_v
= caml_copy_double (zoom
);
2451 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2453 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2455 int pt
= Int_val(pt_v
);
2456 int x
= Int_val (x_v
);
2457 int y
= Int_val (y_v
);
2460 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2461 ret_v
= caml_copy_double (w
);
2465 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2467 CAMLparam2 (pt_v
, string_v
);
2469 int pt
= Int_val (pt_v
);
2472 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2473 ret_v
= caml_copy_double (w
);
2477 CAMLprim value
ml_getpagebox (value opaque_v
)
2479 CAMLparam1 (opaque_v
);
2483 char *s
= String_val (opaque_v
);
2484 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2486 ret_v
= caml_alloc_tuple (4);
2487 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2488 dev
->hints
|= FZ_IGNORE_SHADE
;
2490 switch (page
->type
) {
2492 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
2496 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
2500 bbox
= fz_infinite_bbox
;
2504 fz_free_device (dev
);
2505 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2506 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2507 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2508 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2513 CAMLprim value
ml_setaalevel (value level_v
)
2515 CAMLparam1 (level_v
);
2517 state
.aalevel
= Int_val (level_v
);
2518 CAMLreturn (Val_unit
);
2521 #if !defined _WIN32 && !defined __APPLE__
2524 #include <X11/Xlib.h>
2525 #include <X11/Xutil.h>
2528 static void set_wm_class (int hack
)
2536 display
= getenv ("DISPLAY");
2537 dpy
= XOpenDisplay (display
);
2539 fprintf (stderr
, "XOpenDisplay `%s' failed\n",
2540 display
? display
: "null");
2543 hint
.res_name
= "llpp";
2544 hint
.res_class
= "llpp";
2545 win
= glXGetCurrentDrawable ();
2547 fprintf (stderr
, "glXGetCurrentDrawable returned None\n");
2548 XCloseDisplay (dpy
);
2551 XSetClassHint (dpy
, win
, &hint
);
2552 XCloseDisplay (dpy
);
2556 #define set_wm_class(h) (void) (h)
2559 enum { piunknown
, pilinux
, piwindows
, piosx
,
2560 pisun
, pifreebsd
, pidragonflybsd
,
2561 piopenbsd
, pinetbsd
, pimingw
, picygwin
};
2563 #define NOZOMBIESPLEASE
2565 CAMLprim value
ml_platform (value unit_v
)
2567 CAMLparam1 (unit_v
);
2568 int platid
= piunknown
;
2570 #if defined __linux__
2572 #elif defined __CYGWIN__
2573 #undef NOZOMBIESPLEASE
2575 #elif defined __MINGW32__
2576 #undef NOZOMBIESPLEASE
2578 #elif defined _WIN32
2579 #undef NOZOMBIESPLEASE
2581 #elif defined __DragonFly__
2582 platid
= pidragonflybsd
;
2583 #elif defined __FreeBSD__
2585 #elif defined __OpenBSD__
2587 #elif defined __NetBSD__
2589 #elif defined __sun__
2591 #elif defined __APPLE__
2594 CAMLreturn (Val_int (platid
));
2597 #ifdef NOZOMBIESPLEASE
2600 static void nozombies (void)
2602 struct sigaction sa
;
2604 sa
.sa_handler
= SIG_DFL
;
2605 if (sigemptyset (&sa
.sa_mask
)) {
2606 err (1, "sigemptyset");
2608 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
2609 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
2610 err (1, "sigaction");
2617 CAMLprim value
ml_init (value pipe_v
, value params_v
)
2619 CAMLparam2 (pipe_v
, params_v
);
2620 CAMLlocal2 (trim_v
, fuzz_v
);
2628 state
.cr
= Handle_val (Field (pipe_v
, 0));
2629 state
.cw
= Handle_val (Field (pipe_v
, 1));
2631 state
.cr
= Int_val (Field (pipe_v
, 0));
2632 state
.cw
= Int_val (Field (pipe_v
, 1));
2634 state
.rotate
= Int_val (Field (params_v
, 0));
2635 state
.proportional
= Bool_val (Field (params_v
, 1));
2636 trim_v
= Field (params_v
, 2);
2637 texcount
= Int_val (Field (params_v
, 3));
2638 state
.sliceheight
= Int_val (Field (params_v
, 4));
2639 mustoresize
= Int_val (Field (params_v
, 5));
2640 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
2641 colorspace
= Int_val (Field (params_v
, 6));
2642 wmclasshack
= Bool_val (Field (params_v
, 7));
2643 fontpath
= String_val (Field (params_v
, 8));
2645 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2646 fuzz_v
= Field (trim_v
, 1);
2647 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2648 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2649 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2650 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2652 set_tex_params (colorspace
);
2653 set_wm_class (wmclasshack
);
2656 state
.face
= load_font (fontpath
);
2660 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2662 state
.face
= load_builtin_font (base
, len
);
2664 if (!state
.face
) _exit (1);
2666 realloctexts (texcount
);
2671 InitializeCriticalSection (&critsec
);
2672 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2673 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2674 errx (1, "CreateThread failed: %lx", GetLastError ());
2678 int ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2680 errx (1, "pthread_create: %s", strerror (ret
));
2685 CAMLreturn (Val_unit
);