1 /* lots of code c&p-ed directly from mupdf */
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
17 #include <cygwin/socket.h> /* FIONREAD */
29 #include <caml/fail.h>
30 #include <caml/alloc.h>
31 #include <caml/memory.h>
32 #include <caml/unixsupport.h>
36 #include <mupdf-internal.h>
38 #include <muxps-internal.h>
41 #include FT_FREETYPE_H
46 extern char **environ
;
49 #define MIN(a,b) ((a) < (b) ? (a) : (b))
50 #define MAX(a,b) ((a) > (b) ? (a) : (b))
53 #define NORETURN_ATTR __attribute__ ((noreturn))
54 #define UNUSED_ATTR __attribute__ ((unused))
55 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
56 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
60 #define OPTIMIZE_ATTR(n)
61 #define GCC_FMT_ATTR(a, b)
67 #define FMT_ptr_cast(p) (p)
68 #define FMT_ptr_cast2(p) (p)
70 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
71 err (int exitcode
, const char *fmt
, ...)
78 vfprintf (stderr
, fmt
, ap
);
80 fprintf (stderr
, ": %s\n", strerror (savederrno
));
85 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
86 errx (int exitcode
, const char *fmt
, ...)
91 vfprintf (stderr
, fmt
, ap
);
98 #ifndef GL_TEXTURE_RECTANGLE_ARB
99 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
103 #define GL_BGRA 0x80E1
106 #ifndef GL_UNSIGNED_INT_8_8_8_8
107 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
110 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
111 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
115 #define lprintf printf
120 #define ARSERT(cond) for (;;) { \
122 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
138 struct slice slices
[1];
149 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
157 enum { DPDF
, DXPS
, DCBZ
};
166 fz_text_sheet
*sheet
;
173 fz_display_list
*dlist
;
175 struct slink
*slinks
;
180 void (*freepage
) (void *);
186 struct pagedim
*pagedims
;
205 fz_colorspace
*colorspace
;
227 void (*closedoc
) (void);
228 void (*freepage
) (void *);
233 void (*glBindBufferARB
) (GLenum
, GLuint
);
234 GLboolean (*glUnmapBufferARB
) (GLenum
);
235 void *(*glMapBufferARB
) (GLenum
, GLenum
);
236 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
237 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
238 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
247 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
249 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
252 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
254 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
257 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
259 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
260 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
263 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
265 static void lock (const char *cap
)
267 int ret
= pthread_mutex_lock (&mutex
);
269 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
273 static void unlock (const char *cap
)
275 int ret
= pthread_mutex_unlock (&mutex
);
277 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
281 static int trylock (const char *cap
)
283 int ret
= pthread_mutex_trylock (&mutex
);
284 if (ret
&& ret
!= EBUSY
) {
285 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
290 static void *parse_pointer (const char *cap
, const char *s
)
295 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
297 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
302 static double now (void)
306 if (gettimeofday (&tv
, NULL
)) {
307 err (1, "gettimeofday");
309 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
312 static int hasdata (void)
315 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
316 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
320 CAMLprim value
ml_hasdata (value fd_v
)
325 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
326 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
327 CAMLreturn (Val_bool (avail
> 0));
330 static void readdata (void *p
, int size
)
335 n
= read (state
.cr
, p
, size
);
337 if (errno
== EINTR
) goto again
;
338 err (1, "read (req %d, ret %zd)", size
, n
);
341 if (!n
) errx (1, "EOF while reading");
342 errx (1, "read (req %d, ret %zd)", size
, n
);
346 static void writedata (char *p
, int size
)
351 buf
[0] = (size
>> 24) & 0xff;
352 buf
[1] = (size
>> 16) & 0xff;
353 buf
[2] = (size
>> 8) & 0xff;
354 buf
[3] = (size
>> 0) & 0xff;
356 n
= write (state
.cw
, buf
, 4);
358 if (!n
) errx (1, "EOF while writing length");
359 err (1, "write %zd", n
);
362 n
= write (state
.cw
, p
, size
);
364 if (!n
) errx (1, "EOF while writing data");
365 err (1, "write (req %d, ret %zd)", size
, n
);
369 static int readlen (void)
374 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
377 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
385 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
388 len
= vsnprintf (buf
, size
, fmt
, ap
);
391 if (len
> -1 && len
< size
) {
392 writedata (buf
, len
);
402 buf
= realloc (buf
, size
);
407 static void closepdf (void)
410 pdf_close_document (state
.u
.pdf
);
415 static void closexps (void)
418 xps_close_document (state
.u
.xps
);
423 static void closecbz (void)
426 cbz_close_document (state
.u
.cbz
);
431 static void freepdfpage (void *ptr
)
433 pdf_free_page (state
.u
.pdf
, ptr
);
436 static void freeemptyxxxpage (void *ptr
)
441 static void freexpspage (void *ptr
)
443 xps_free_page (state
.u
.xps
, ptr
);
446 static void freecbzpage (void *ptr
)
448 cbz_free_page (state
.u
.cbz
, ptr
);
451 static void openxref (char *filename
, char *password
)
455 for (i
= 0; i
< state
.texcount
; ++i
) {
456 state
.texowners
[i
].w
= -1;
457 state
.texowners
[i
].slice
= NULL
;
460 if (state
.closedoc
) state
.closedoc ();
462 len
= strlen (filename
);
468 ext
[0] = tolower ((int) filename
[len
-3]);
469 ext
[1] = tolower ((int) filename
[len
-2]);
470 ext
[2] = tolower ((int) filename
[len
-1]);
472 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
475 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
480 if (state
.pagedims
) {
481 free (state
.pagedims
);
482 state
.pagedims
= NULL
;
484 state
.pagedimcount
= 0;
486 fz_set_aa_level (state
.ctx
, state
.aalevel
);
487 switch (state
.type
) {
489 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
490 if (pdf_needs_password (state
.u
.pdf
)) {
491 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
493 errx (1, "invalid password");
496 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
497 state
.closedoc
= closepdf
;
498 state
.freepage
= freepdfpage
;
502 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
503 state
.pagecount
= xps_count_pages (state
.u
.xps
);
504 state
.closedoc
= closexps
;
505 state
.freepage
= freexpspage
;
509 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
510 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
511 state
.closedoc
= closecbz
;
512 state
.freepage
= freecbzpage
;
517 static void pdfinfo (void)
519 if (state
.type
== DPDF
) {
522 printd ("info PDF version\t%d.%d",
523 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
525 infoobj
= pdf_dict_gets (state
.u
.pdf
->trailer
, "Info");
529 char *items
[] = { "Title", "Author", "Creator",
530 "Producer", "CreationDate" };
532 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
533 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
534 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
537 printd ("title %s", s
);
539 printd ("info %s\t%s", items
[i
], s
);
541 fz_free (state
.ctx
, s
);
548 static void unlinktile (struct tile
*tile
)
552 for (i
= 0; i
< tile
->slicecount
; ++i
) {
553 struct slice
*s
= &tile
->slices
[i
];
555 if (s
->texindex
!= -1) {
556 if (state
.texowners
[s
->texindex
].slice
== s
) {
557 state
.texowners
[s
->texindex
].slice
= NULL
;
563 static void freepage (struct page
*page
)
566 fz_free_text_page (state
.ctx
, page
->text
);
569 fz_free_text_sheet (state
.ctx
, page
->sheet
);
574 page
->freepage (page
->u
.ptr
);
575 fz_free_display_list (state
.ctx
, page
->dlist
);
579 static void freetile (struct tile
*tile
)
584 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
587 fz_drop_pixmap (state
.ctx
, state
.pig
);
589 state
.pig
= tile
->pixmap
;
594 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
602 static int cacheline32bytes
;
604 static void __attribute__ ((constructor
)) clcheck (void)
606 char **envp
= environ
;
611 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
613 cacheline32bytes
= auxv
[1] == 32;
619 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
621 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
622 if (cacheline32bytes
&& size
> 32) {
623 intptr_t a1
, a2
, diff
;
625 vector
unsigned char v
= vec_splat_u8 (-1);
626 vector
unsigned char *p
;
628 a1
= a2
= (intptr_t) pixmap
->samples
;
629 a2
= (a1
+ 31) & ~31;
634 while (a1
!= a2
) *(char *) a1
++ = 0xff;
635 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
636 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
638 vec_st (v
, i
+ 16, p
);
640 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
642 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
645 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
648 static void trimctm (pdf_page
*page
, int pindex
)
651 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
653 if (!pdim
->tctmready
) {
654 if (state
.trimmargins
) {
656 fz_matrix rm
, sm
, tm
, im
, ctm1
;
658 fz_rotate (&rm
, -pdim
->rotate
);
659 fz_scale (&sm
, 1, -1);
660 fz_concat (&ctm
, &rm
, &sm
);
661 realbox
= pdim
->mediabox
;
662 fz_transform_rect (&realbox
, &ctm
);
663 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
664 fz_concat (&ctm1
, &ctm
, &tm
);
665 fz_invert_matrix (&im
, &page
->ctm
);
666 fz_concat (&ctm
, &im
, &ctm1
);
676 static fz_matrix
pagectm (struct page
*page
)
680 if (page
->type
== DPDF
) {
681 trimctm (page
->u
.pdfpage
, page
->pdimno
);
683 &state
.pagedims
[page
->pdimno
].tctm
,
684 &state
.pagedims
[page
->pdimno
].ctm
);
687 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
689 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
690 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
695 static void *loadpage (int pageno
, int pindex
)
698 struct page
*page
= NULL
;
700 page
= calloc (sizeof (struct page
), 1);
702 err (1, "calloc page %d", pageno
);
705 page
->dlist
= fz_new_display_list (state
.ctx
);
706 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
708 switch (state
.type
) {
710 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
711 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
713 page
->freepage
= freepdfpage
;
717 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
718 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
720 page
->freepage
= freexpspage
;
724 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
725 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
727 page
->freepage
= freecbzpage
;
731 fz_catch (state
.ctx
) {
733 page
->freepage
= freeemptyxxxpage
;
735 fz_free_device (dev
);
737 page
->pdimno
= pindex
;
738 page
->pageno
= pageno
;
739 page
->sgen
= state
.gen
;
740 page
->tgen
= state
.gen
;
741 page
->type
= state
.type
;
746 static struct tile
*alloctile (int h
)
753 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
754 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
755 tile
= calloc (tilesize
, 1);
757 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
759 for (i
= 0; i
< slicecount
; ++i
) {
760 int sh
= MIN (h
, state
.sliceheight
);
761 tile
->slices
[i
].h
= sh
;
762 tile
->slices
[i
].texindex
= -1;
765 tile
->slicecount
= slicecount
;
766 tile
->sliceheight
= state
.sliceheight
;
776 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
777 const fz_matrix
*ctm
, float alpha
)
779 struct obs
*obs
= dev
->user
;
781 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
783 fz_rect rect
= fz_unit_rect
;
785 fz_transform_rect (&rect
, ctm
);
786 fz_round_rect (&b
, &rect
);
787 fz_intersect_irect (&b
, &obs
->b
);
788 obs
->cured
= b
.x0
== obs
->b
.x0
791 && b
.y1
== obs
->b
.y1
;
795 static int obscured (struct page
*page
, fz_irect bbox
)
802 memset (&dev
, 0, sizeof (dev
));
803 memset (&obs
, 0, sizeof (obs
));
808 dev
.fill_image
= obs_fill_image
;
810 fz_rect_from_irect (&rect
, &bbox
);
811 ctm
= pagectm (page
);
812 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
815 #define OBSCURED obscured
817 #define OBSCURED(a, b) 0
820 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
);
855 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
861 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
867 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
868 clearpixmap (tile
->pixmap
);
870 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
871 ctm
= pagectm (page
);
872 fz_rect_from_irect (&rect
, &bbox
);
873 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
874 fz_free_device (dev
);
879 static void initpdims (void)
882 int pageno
, trim
, show
;
888 if (state
.trimmargins
&& state
.trimcachepath
) {
889 trimf
= fopen (state
.trimcachepath
, "rb");
891 trimf
= fopen (state
.trimcachepath
, "wb");
895 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
900 switch (state
.type
) {
902 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
904 if (state
.trimmargins
) {
909 page
= pdf_load_page (state
.u
.pdf
, pageno
);
910 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
911 trim
= state
.trimanew
|| !obj
;
917 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
918 dev
->hints
|= FZ_IGNORE_SHADE
;
919 fz_invert_matrix (&ctm
, &page
->ctm
);
920 pdf_run_page (state
.u
.pdf
, page
, dev
, &fz_identity
, NULL
);
921 fz_free_device (dev
);
923 rect
.x0
+= state
.trimfuzz
.x0
;
924 rect
.x1
+= state
.trimfuzz
.x1
;
925 rect
.y0
+= state
.trimfuzz
.y0
;
926 rect
.y1
+= state
.trimfuzz
.y1
;
927 fz_transform_rect (&rect
, &ctm
);
928 fz_intersect_rect (&rect
, &page
->mediabox
);
930 if (fz_is_empty_rect (&rect
)) {
931 mediabox
= page
->mediabox
;
937 obj
= pdf_new_array (state
.ctx
, 4);
938 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x0
));
939 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y0
));
940 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x1
));
941 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y1
));
942 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
945 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
946 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
947 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
948 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
951 rotate
= page
->rotate
;
952 pdf_free_page (state
.u
.pdf
, page
);
954 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
956 printd ("progress %f Trimming %d",
957 (double) (pageno
+ 1) / state
.pagecount
,
961 fz_catch (state
.ctx
) {
962 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
968 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
970 if (fz_is_empty_rect (&mediabox
)) {
971 fprintf (stderr
, "cannot find page size for page %d\n",
979 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
981 if (!fz_is_empty_rect (&cropbox
)) {
982 fz_intersect_rect (&mediabox
, &cropbox
);
984 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
994 page
= xps_load_page (state
.u
.xps
, pageno
);
995 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
997 if (state
.trimmargins
) {
1001 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1002 dev
->hints
|= FZ_IGNORE_SHADE
;
1003 xps_run_page (state
.u
.xps
, page
, dev
,
1004 &fz_identity
, NULL
);
1005 fz_free_device (dev
);
1007 rect
.x0
+= state
.trimfuzz
.x0
;
1008 rect
.x1
+= state
.trimfuzz
.x1
;
1009 rect
.y0
+= state
.trimfuzz
.y0
;
1010 rect
.y1
+= state
.trimfuzz
.y1
;
1011 fz_intersect_rect (&rect
, &mediabox
);
1013 if (!fz_is_empty_rect (&rect
)) {
1017 xps_free_page (state
.u
.xps
, page
);
1018 printd ("progress %f loading %d",
1019 (double) (pageno
+ 1) / state
.pagecount
,
1022 fz_catch (state
.ctx
) {
1030 if (state
.trimmargins
&& trimw
) {
1033 fz_try (state
.ctx
) {
1034 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1035 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1036 cbz_free_page (state
.u
.cbz
, page
);
1037 printd ("progress %f Trimming %d",
1038 (double) (pageno
+ 1) / state
.pagecount
,
1041 fz_catch (state
.ctx
) {
1042 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1049 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1051 err (1, "fwrite trim mediabox");
1057 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1059 err (1, "fread trim mediabox");
1063 mediabox
.x0
= mediabox
.y0
= 0;
1072 ARSERT (0 && state
.type
);
1075 if (state
.pagedimcount
== 0
1076 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1077 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1080 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1081 state
.pagedims
= realloc (state
.pagedims
, size
);
1082 if (!state
.pagedims
) {
1083 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1084 size
, state
.pagedimcount
+ 1);
1087 p
= &state
.pagedims
[state
.pagedimcount
++];
1089 p
->mediabox
= mediabox
;
1094 if (state
.trimmargins
) {
1095 printd ("progress 1 Trimmed %d pages in %f seconds",
1096 state
.pagecount
, end
- start
);
1099 printd ("vmsg Processed %d pages in %f seconds",
1100 state
.pagecount
, end
- start
);
1104 if (fclose (trimf
)) {
1110 static void layout (void)
1115 double zoom
, w
, maxw
= 0;
1116 struct pagedim
*p
= state
.pagedims
;
1118 if (state
.proportional
) {
1119 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1122 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1124 fz_transform_rect (&box
, &rm
);
1125 w
= box
.x1
- box
.x0
;
1126 maxw
= MAX (w
, maxw
);
1131 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1133 fz_matrix tm
, sm
, rm
;
1135 fz_rotate (&ctm
, state
.rotate
);
1136 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1138 fz_transform_rect (&box
, &rm
);
1139 w
= box
.x1
- box
.x0
;
1141 if (state
.proportional
) {
1142 double scale
= w
/ maxw
;
1143 zoom
= (state
.w
/ w
) * scale
;
1149 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1150 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1152 fz_rotate (&rm
, p
->rotate
);
1153 p
->pagebox
= p
->mediabox
;
1154 fz_transform_rect (&p
->pagebox
, &rm
);
1155 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1156 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1160 fz_transform_rect (&rect
, &ctm
);
1161 fz_round_rect (&p
->bounds
, &rect
);
1163 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
1166 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1167 fz_scale (&sm
, zoom
, -zoom
);
1168 fz_concat (&ctm
, &tm
, &sm
);
1169 fz_concat (&p
->lctm
, &ctm
, &rm
);
1174 while (p
-- != state
.pagedims
) {
1175 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1176 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1177 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1178 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1182 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1187 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1188 desttoanchor (fz_link_dest
*dest
)
1192 struct pagedim
*pdim
= state
.pagedims
;
1197 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1198 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1200 pdim
= &state
.pagedims
[i
];
1202 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1204 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1205 p
.x
= dest
->ld
.gotor
.lt
.x
;
1206 p
.y
= dest
->ld
.gotor
.lt
.y
;
1207 fz_transform_point (&p
, &pdim
->lctm
);
1211 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1212 double x0
, x1
, y0
, y1
;
1214 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1215 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1217 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1218 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1220 a
.n
= dest
->ld
.gotor
.page
;
1225 static void recurse_outline (fz_outline
*outline
, int level
)
1228 struct anchor a
= desttoanchor (&outline
->dest
);
1231 printd ("o %d %d %d %d %s",
1232 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1234 if (outline
->down
) {
1235 recurse_outline (outline
->down
, level
+ 1);
1237 outline
= outline
->next
;
1241 static void process_outline (void)
1243 fz_outline
*outline
;
1245 if (!state
.needoutline
) return;
1247 state
.needoutline
= 0;
1248 switch (state
.type
) {
1250 outline
= pdf_load_outline (state
.u
.pdf
);
1253 outline
= xps_load_outline (state
.u
.xps
);
1260 recurse_outline (outline
, 0);
1261 fz_free_outline (state
.ctx
, outline
);
1265 static char *strofspan (fz_text_span
*span
)
1270 size_t size
= 0, cap
= 80;
1272 p
= malloc (cap
+ 1);
1273 if (!p
) return NULL
;
1275 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1276 int n
= fz_runetochar (utf8
, ch
->c
);
1277 if (size
+ n
> cap
) {
1279 p
= realloc (p
, cap
+ 1);
1280 if (!p
) return NULL
;
1283 memcpy (p
+ size
, utf8
, n
);
1290 static int matchspan (regex_t
*re
, fz_text_span
*span
, fz_matrix ctm
,
1291 int stop
, int pageno
, double start
)
1298 fz_point p1
, p2
, p3
, p4
;
1300 p
= strofspan (span
);
1303 ret
= regexec (re
, p
, 1, &rm
, 0);
1306 if (ret
!= REG_NOMATCH
) {
1309 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1310 printd ("msg regexec error `%.*s'",
1311 (int) size
, errbuf
);
1318 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1319 c
+= fz_runelen (span
->text
[a
].c
);
1321 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1322 c
+= fz_runelen (span
->text
[b
].c
);
1325 if (fz_runelen (span
->text
[b
].c
) > 1) {
1328 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1329 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
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
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1353 printd ("match %d %d %f %f %f %f %f %f %f %f",
1365 static int compareblocks (const void *l
, const void *r
)
1367 fz_text_block
const *ls
= l
;
1368 fz_text_block
const* rs
= r
;
1369 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1372 /* wishful thinking function */
1373 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1378 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1380 fz_text_sheet
*sheet
;
1381 struct pagedim
*pdim
, *pdimprev
;
1382 int stop
= 0, niters
= 0;
1385 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1389 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1390 if (niters
++ == 5) {
1393 printd ("progress 1 attention requested aborting search at %d",
1398 printd ("progress %f searching in page %d",
1399 (double) (pageno
+ 1) / state
.pagecount
,
1404 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1405 pdim
= &state
.pagedims
[i
];
1406 if (pdim
->pageno
== pageno
) {
1409 if (pdim
->pageno
> pageno
) {
1418 sheet
= fz_new_text_sheet (state
.ctx
);
1419 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1420 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1422 switch (state
.type
) {
1425 fz_try (state
.ctx
) {
1426 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1427 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1428 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1429 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1431 fz_catch (state
.ctx
) {
1432 fz_free_device (tdev
);
1439 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1440 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1444 ARSERT (0 && state
.type
);
1447 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1448 fz_free_device (tdev
);
1450 for (j
= 0; j
< text
->len
; ++j
) {
1452 fz_text_block
*block
;
1454 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1456 for (k
= 0; k
< block
->len
; ++k
) {
1461 line
= &block
->lines
[k
];
1462 if (line
->bbox
.y0
< y
+ 1) continue;
1465 line
= &block
->lines
[block
->len
- 1 - k
];
1466 if (line
->bbox
.y0
> y
- 1) continue;
1469 for (span
= line
->spans
;
1470 span
< line
->spans
+ line
->len
;
1473 switch (matchspan (re
, span
, ctm
, stop
, pageno
, start
)) {
1475 case 1: stop
= 1; break;
1476 case -1: stop
= 1; goto endloop
;
1491 fz_free_text_page (state
.ctx
, text
);
1492 fz_free_text_sheet (state
.ctx
, sheet
);
1494 state
.freepage (u
.ptr
);
1499 printd ("progress 1 no matches %f sec", end
- start
);
1501 printd ("clearrects");
1504 static void set_tex_params (int colorspace
)
1511 switch (colorspace
) {
1513 state
.texiform
= GL_RGBA8
;
1514 state
.texform
= GL_RGBA
;
1515 state
.texty
= GL_UNSIGNED_BYTE
;
1516 state
.colorspace
= fz_device_rgb
;
1519 state
.texiform
= GL_RGBA8
;
1520 state
.texform
= GL_BGRA
;
1521 state
.texty
= endianness
.s
> 1
1522 ? GL_UNSIGNED_INT_8_8_8_8
1523 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1524 state
.colorspace
= fz_device_bgr
;
1527 state
.texiform
= GL_LUMINANCE_ALPHA
;
1528 state
.texform
= GL_LUMINANCE_ALPHA
;
1529 state
.texty
= GL_UNSIGNED_BYTE
;
1530 state
.colorspace
= fz_device_gray
;
1533 errx (1, "invalid colorspce %d", colorspace
);
1537 static void realloctexts (int texcount
)
1541 if (texcount
== state
.texcount
) return;
1543 if (texcount
< state
.texcount
) {
1544 glDeleteTextures (state
.texcount
- texcount
,
1545 state
.texids
+ texcount
);
1548 size
= texcount
* sizeof (*state
.texids
);
1549 state
.texids
= realloc (state
.texids
, size
);
1550 if (!state
.texids
) {
1551 err (1, "realloc texids %" FMT_s
, size
);
1554 size
= texcount
* sizeof (*state
.texowners
);
1555 state
.texowners
= realloc (state
.texowners
, size
);
1556 if (!state
.texowners
) {
1557 err (1, "realloc texowners %" FMT_s
, size
);
1559 if (texcount
> state
.texcount
) {
1562 glGenTextures (texcount
- state
.texcount
,
1563 state
.texids
+ state
.texcount
);
1564 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1565 state
.texowners
[i
].w
= -1;
1566 state
.texowners
[i
].slice
= NULL
;
1569 state
.texcount
= texcount
;
1573 static char *mbtoutf8 (char *s
)
1579 len
= mbstowcs (NULL
, s
, strlen (s
));
1584 if (len
== (size_t) -1) {
1589 tmp
= malloc (len
* sizeof (wchar_t));
1594 ret
= mbstowcs (tmp
, s
, len
);
1595 if (ret
== (size_t) -1) {
1601 for (i
= 0; i
< ret
; ++i
) {
1602 len
+= fz_runelen (tmp
[i
]);
1605 p
= r
= malloc (len
+ 1);
1611 for (i
= 0; i
< ret
; ++i
) {
1612 p
+= fz_runetochar (p
, tmp
[i
]);
1619 CAMLprim value
ml_mbtoutf8 (value s_v
)
1625 s
= String_val (s_v
);
1631 ret_v
= caml_copy_string (r
);
1637 static void * mainloop (void *unused
)
1640 int len
, ret
, oldlen
= 0;
1645 errx (1, "readlen returned 0");
1648 if (oldlen
< len
+ 1) {
1649 p
= realloc (p
, len
+ 1);
1651 err (1, "realloc %d failed", len
+ 1);
1658 if (!strncmp ("open", p
, 4)) {
1665 ret
= sscanf (p
+ 5, " %d %n", &wthack
, &off
);
1667 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1670 filename
= p
+ 5 + off
;
1671 filenamelen
= strlen (filename
);
1672 password
= filename
+ filenamelen
+ 1;
1675 openxref (filename
, password
);
1681 utf8filename
= mbtoutf8 (filename
);
1682 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1683 if (utf8filename
!= filename
) {
1684 free (utf8filename
);
1687 state
.needoutline
= 1;
1689 else if (!strncmp ("cs", p
, 2)) {
1692 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1694 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1697 set_tex_params (colorspace
);
1698 for (i
= 0; i
< state
.texcount
; ++i
) {
1699 state
.texowners
[i
].w
= -1;
1700 state
.texowners
[i
].slice
= NULL
;
1704 else if (!strncmp ("freepage", p
, 8)) {
1707 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1709 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1713 else if (!strncmp ("freetile", p
, 8)) {
1716 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1718 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1722 else if (!strncmp ("search", p
, 6)) {
1723 int icase
, pageno
, y
, ret
, len2
, forward
;
1727 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1728 &icase
, &pageno
, &y
, &forward
, &len2
);
1730 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1733 pattern
= p
+ 6 + len2
;
1734 ret
= regcomp (&re
, pattern
,
1735 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1740 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1741 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1744 search (&re
, pageno
, y
, forward
);
1748 else if (!strncmp ("geometry", p
, 8)) {
1752 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1754 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1762 for (i
= 0; i
< state
.texcount
; ++i
) {
1763 state
.texowners
[i
].slice
= NULL
;
1770 unlock ("geometry");
1771 printd ("continue %d", state
.pagecount
);
1773 else if (!strncmp ("reqlayout", p
, 9)) {
1775 int rotate
, proportional
, off
;
1778 ret
= sscanf (p
+ 9, " %d %d %n", &rotate
, &proportional
, &off
);
1780 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1783 if (state
.rotate
!= rotate
|| state
.proportional
!= proportional
) {
1786 state
.rotate
= rotate
;
1787 state
.proportional
= proportional
;
1791 nameddest
= p
+ 9 + off
;
1792 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1795 pdf_obj
*needle
, *obj
;
1797 needle
= pdf_new_string (state
.ctx
, nameddest
,
1798 strlen (nameddest
));
1799 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1801 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1803 a
= desttoanchor (&dest
);
1805 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1808 printd ("emsg failed to parse destination `%s'\n",
1813 printd ("emsg destination `%s' not found\n",
1816 pdf_drop_obj (needle
);
1820 unlock ("reqlayout");
1821 printd ("continue %d", state
.pagecount
);
1823 else if (!strncmp ("page", p
, 4)) {
1826 int pageno
, pindex
, ret
;
1828 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1830 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1835 page
= loadpage (pageno
, pindex
);
1839 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1841 else if (!strncmp ("tile", p
, 4)) {
1842 int x
, y
, w
, h
, ret
;
1848 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1849 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1851 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1856 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1860 printd ("tile %d %d %" FMT_ptr
" %u %f",
1862 FMT_ptr_cast2 (tile
),
1863 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1866 else if (!strncmp ("settrim", p
, 7)) {
1870 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1871 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1873 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1877 state
.trimmargins
= trimmargins
;
1878 state
.needoutline
= 1;
1879 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1881 state
.trimfuzz
= fuzz
;
1883 state
.pagedimcount
= 0;
1884 free (state
.pagedims
);
1885 state
.pagedims
= NULL
;
1890 printd ("continue %d", state
.pagecount
);
1892 else if (!strncmp ("sliceh", p
, 6)) {
1895 ret
= sscanf (p
+ 6, " %d", &h
);
1897 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1899 if (h
!= state
.sliceheight
) {
1902 state
.sliceheight
= h
;
1903 for (i
= 0; i
< state
.texcount
; ++i
) {
1904 state
.texowners
[i
].w
= -1;
1905 state
.texowners
[i
].h
= -1;
1906 state
.texowners
[i
].slice
= NULL
;
1910 else if (!strncmp ("interrupt", p
, 9)) {
1911 printd ("vmsg interrupted");
1914 errx (1, "unknown command %.*s", len
, p
);
1920 CAMLprim value
ml_realloctexts (value texcount_v
)
1922 CAMLparam1 (texcount_v
);
1925 if (trylock ("ml_realloctexts")) {
1929 realloctexts (Int_val (texcount_v
));
1931 unlock ("ml_realloctexts");
1934 CAMLreturn (Val_bool (ok
));
1937 static void showsel (struct page
*page
, int ox
, int oy
)
1944 fz_text_block
*block
;
1945 struct mark first
, last
;
1947 first
= page
->fmark
;
1950 if (!first
.span
|| !last
.span
) return;
1952 glEnable (GL_BLEND
);
1953 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1954 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1956 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1957 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1958 for (block
= page
->text
->blocks
;
1959 block
< page
->text
->blocks
+ page
->text
->len
;
1961 for (line
= block
->lines
;
1962 line
< block
->lines
+ block
->len
;
1964 rect
= fz_empty_rect
;
1965 for (span
= line
->spans
;
1966 span
< line
->spans
+ line
->len
;
1970 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1975 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1977 j
= MIN (first
.i
, last
.i
);
1978 k
= MAX (first
.i
, last
.i
);
1980 else if (span
== first
.span
) {
1984 else if (span
== last
.span
) {
1990 for (i
= j
; i
<= k
; ++i
) {
1991 fz_union_rect (&rect
, &span
->text
[i
].bbox
);
1993 fz_round_rect (&bbox
, &rect
);
1994 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2001 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2002 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2003 if (span
== last
.span
) {
2011 glDisable (GL_BLEND
);
2016 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2018 fz_matrix ctm
, tm
, pm
;
2019 fz_link
*link
, *links
;
2021 switch (page
->type
) {
2023 links
= page
->u
.pdfpage
->links
;
2027 links
= page
->u
.xpspage
->links
;
2034 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2035 glEnable (GL_LINE_STIPPLE
);
2036 glLineStipple (0.5, 0xcccc);
2038 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2039 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2040 fz_translate (&tm
, xoff
, yoff
);
2041 pm
= pagectm (page
);
2042 fz_concat (&ctm
, &pm
, &tm
);
2045 for (link
= links
; link
; link
= link
->next
) {
2046 fz_point p1
, p2
, p3
, p4
;
2048 p1
.x
= link
->rect
.x0
;
2049 p1
.y
= link
->rect
.y0
;
2051 p2
.x
= link
->rect
.x1
;
2052 p2
.y
= link
->rect
.y0
;
2054 p3
.x
= link
->rect
.x1
;
2055 p3
.y
= link
->rect
.y1
;
2057 p4
.x
= link
->rect
.x0
;
2058 p4
.y
= link
->rect
.y1
;
2060 fz_transform_point (&p1
, &ctm
);
2061 fz_transform_point (&p2
, &ctm
);
2062 fz_transform_point (&p3
, &ctm
);
2063 fz_transform_point (&p4
, &ctm
);
2065 switch (link
->dest
.kind
) {
2066 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2067 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2068 default: glColor3ub (0, 0, 0); break;
2071 glVertex2f (p1
.x
, p1
.y
);
2072 glVertex2f (p2
.x
, p2
.y
);
2073 glVertex2f (p3
.x
, p3
.y
);
2074 glVertex2f (p4
.x
, p4
.y
);
2078 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2079 glDisable (GL_LINE_STIPPLE
);
2082 static int compareslinks (const void *l
, const void *r
)
2084 struct slink
const *ls
= l
;
2085 struct slink
const *rs
= r
;
2086 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2087 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2089 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2092 static void droptext (struct page
*page
)
2095 fz_free_text_page (state
.ctx
, page
->text
);
2098 page
->fmark
.span
= NULL
;
2099 page
->lmark
.span
= NULL
;
2103 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2107 static void dropslinks (struct page
*page
)
2110 free (page
->slinks
);
2111 page
->slinks
= NULL
;
2112 page
->slinkcount
= 0;
2116 static void ensureslinks (struct page
*page
)
2120 size_t slinksize
= sizeof (*page
->slinks
);
2121 fz_link
*link
, *links
;
2123 if (state
.gen
!= page
->sgen
) {
2125 page
->sgen
= state
.gen
;
2127 if (page
->slinks
) return;
2129 switch (page
->type
) {
2131 links
= page
->u
.pdfpage
->links
;
2132 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2134 &state
.pagedims
[page
->pdimno
].tctm
,
2135 &state
.pagedims
[page
->pdimno
].ctm
);
2139 links
= page
->u
.xpspage
->links
;
2140 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2147 for (link
= links
; link
; link
= link
->next
) {
2151 page
->slinkcount
= count
;
2152 page
->slinks
= calloc (count
, slinksize
);
2153 if (!page
->slinks
) {
2154 err (1, "realloc slinks %d", count
);
2157 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2161 fz_transform_rect (&rect
, &ctm
);
2162 page
->slinks
[i
].link
= link
;
2163 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2165 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2169 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2170 static void fmt_linkn (char *s
, unsigned int u
)
2172 unsigned int len
; unsigned int q
;
2173 int zma
= 'z' - 'a' + 1;
2175 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2178 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2179 /* handles u == 0 */
2184 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2185 int noff
, char *targ
, int tlen
, int hfsize
)
2189 struct slink
*slink
;
2190 double x0
, y0
, x1
, y1
, w
;
2192 ensureslinks (page
);
2193 glColor3ub (0xc3, 0xb0, 0x91);
2194 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2195 fmt_linkn (buf
, i
+ noff
);
2196 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2197 slink
= &page
->slinks
[i
];
2199 x0
= slink
->bbox
.x0
+ xoff
- 5;
2200 y1
= slink
->bbox
.y0
+ yoff
- 5;
2201 y0
= y1
+ 10 + hfsize
;
2202 w
= measure_string (state
.face
, hfsize
, buf
);
2204 glRectd (x0
, y0
, x1
, y1
);
2208 glEnable (GL_BLEND
);
2209 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2210 glEnable (GL_TEXTURE_2D
);
2211 glColor3ub (0, 0, 0);
2212 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2213 fmt_linkn (buf
, i
+ noff
);
2214 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2215 slink
= &page
->slinks
[i
];
2217 x0
= slink
->bbox
.x0
+ xoff
;
2218 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2219 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2222 glDisable (GL_TEXTURE_2D
);
2223 glDisable (GL_BLEND
);
2227 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2230 struct slice
*slice1
;
2231 unsigned char *texdata
;
2234 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2235 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2237 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2238 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2239 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2243 int texindex
= state
.texindex
++ % state
.texcount
;
2245 if (state
.texowners
[texindex
].w
== tile
->w
) {
2246 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2250 state
.texowners
[texindex
].h
= slice
->h
;
2254 state
.texowners
[texindex
].h
= slice
->h
;
2257 state
.texowners
[texindex
].w
= tile
->w
;
2258 state
.texowners
[texindex
].slice
= slice
;
2259 slice
->texindex
= texindex
;
2261 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2263 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2267 texdata
= tile
->pixmap
->samples
;
2270 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2282 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2294 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2299 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2301 CAMLparam2 (args_v
, ptr_v
);
2302 int dispx
= Int_val (Field (args_v
, 0));
2303 int dispy
= Int_val (Field (args_v
, 1));
2304 int dispw
= Int_val (Field (args_v
, 2));
2305 int disph
= Int_val (Field (args_v
, 3));
2306 int tilex
= Int_val (Field (args_v
, 4));
2307 int tiley
= Int_val (Field (args_v
, 5));
2308 char *s
= String_val (ptr_v
);
2309 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2311 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2313 int slicey
, firstslice
;
2314 struct slice
*slice
;
2316 firstslice
= tiley
/ tile
->sliceheight
;
2317 slice
= &tile
->slices
[firstslice
];
2318 slicey
= tiley
% tile
->sliceheight
;
2323 dh
= slice
->h
- slicey
;
2324 dh
= MIN (disph
, dh
);
2325 uploadslice (tile
, slice
);
2329 glTexCoord2i (tilex
, slicey
);
2330 glVertex2i (dispx
, dispy
);
2332 glTexCoord2i (tilex
+dispw
, slicey
);
2333 glVertex2i (dispx
+dispw
, dispy
);
2335 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2336 glVertex2i (dispx
+dispw
, dispy
+dh
);
2338 glTexCoord2i (tilex
, slicey
+dh
);
2339 glVertex2i (dispx
, dispy
+dh
);
2346 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2350 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2351 CAMLreturn (Val_unit
);
2354 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2355 value xoff_v
, value yoff_v
,
2358 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2359 int xoff
= Int_val (xoff_v
);
2360 int yoff
= Int_val (yoff_v
);
2361 int noff
= Int_val (Field (li_v
, 0));
2362 char *targ
= String_val (Field (li_v
, 1));
2363 int tlen
= caml_string_length (Field (li_v
, 1));
2364 int hfsize
= Int_val (Field (li_v
, 2));
2365 char *s
= String_val (ptr_v
);
2366 int hlmask
= Int_val (hlinks_v
);
2367 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2370 /* deal with loadpage failed pages */
2374 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2375 if (trylock ("ml_postprocess")) {
2380 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2381 noff
= page
->slinkcount
;
2383 showsel (page
, xoff
, yoff
);
2384 unlock ("ml_postprocess");
2387 CAMLreturn (Val_int (noff
));
2390 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2394 const fz_matrix
*tctm
;
2395 fz_link
*link
, *links
;
2397 switch (page
->type
) {
2399 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2400 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2401 links
= page
->u
.pdfpage
->links
;
2405 tctm
= &fz_identity
;
2406 links
= page
->u
.xpspage
->links
;
2415 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2416 fz_invert_matrix (&ctm
, &ctm
);
2417 fz_transform_point (&p
, &ctm
);
2419 for (link
= links
; link
; link
= link
->next
) {
2420 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2421 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2429 static void ensuretext (struct page
*page
)
2431 if (state
.gen
!= page
->tgen
) {
2433 page
->tgen
= state
.gen
;
2439 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2440 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2441 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2442 ctm
= pagectm (page
);
2443 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2444 qsort (page
->text
->blocks
, page
->text
->len
,
2445 sizeof (*page
->text
->blocks
), compareblocks
);
2446 fz_free_device (tdev
);
2450 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2452 CAMLparam2 (start_page_v
, dir_v
);
2454 int i
, dir
= Int_val (dir_v
);
2455 int start_page
= Int_val (start_page_v
);
2456 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2458 ret_v
= Val_int (0);
2459 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2463 lock ("ml_findpage_with_links");
2464 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2467 switch (state
.type
) {
2470 pdf_page
*page
= NULL
;
2472 fz_try (state
.ctx
) {
2473 page
= pdf_load_page (state
.u
.pdf
, i
);
2474 found
= !!page
->links
;
2476 fz_catch (state
.ctx
) {
2486 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2487 found
= !!page
->links
;
2493 ARSERT ("invalid document type");
2497 ret_v
= caml_alloc_small (1, 1);
2498 Field (ret_v
, 0) = Val_int (i
);
2503 unlock ("ml_findpage_with_links");
2509 enum { dir_first
, dir_last
};
2510 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2512 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2514 CAMLparam2 (ptr_v
, dir_v
);
2515 CAMLlocal2 (ret_v
, pos_v
);
2517 int dirtag
, i
, slinkindex
;
2518 struct slink
*found
= NULL
,*slink
;
2519 char *s
= String_val (ptr_v
);
2521 page
= parse_pointer ("ml_findlink", s
);
2522 ret_v
= Val_int (0);
2523 if (trylock ("ml_findlink")) {
2527 ensureslinks (page
);
2529 if (Is_block (dir_v
)) {
2530 dirtag
= Tag_val (dir_v
);
2532 case dir_first_visible
:
2534 int x0
, y0
, dir
, first_index
, last_index
;
2536 pos_v
= Field (dir_v
, 0);
2537 x0
= Int_val (Field (pos_v
, 0));
2538 y0
= Int_val (Field (pos_v
, 1));
2539 dir
= Int_val (Field (pos_v
, 2));
2544 last_index
= page
->slinkcount
;
2547 first_index
= page
->slinkcount
- 1;
2551 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2552 slink
= &page
->slinks
[i
];
2553 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2562 slinkindex
= Int_val (Field (dir_v
, 0));
2563 found
= &page
->slinks
[slinkindex
];
2564 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2565 slink
= &page
->slinks
[i
];
2566 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2574 slinkindex
= Int_val (Field (dir_v
, 0));
2575 found
= &page
->slinks
[slinkindex
];
2576 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2577 slink
= &page
->slinks
[i
];
2578 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2586 slinkindex
= Int_val (Field (dir_v
, 0));
2587 found
= &page
->slinks
[slinkindex
];
2588 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2589 slink
= &page
->slinks
[i
];
2590 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2598 slinkindex
= Int_val (Field (dir_v
, 0));
2599 found
= &page
->slinks
[slinkindex
];
2600 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2601 slink
= &page
->slinks
[i
];
2602 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2611 dirtag
= Int_val (dir_v
);
2614 found
= page
->slinks
;
2619 found
= page
->slinks
+ (page
->slinkcount
- 1);
2625 ret_v
= caml_alloc_small (2, 1);
2626 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2629 unlock ("ml_findlink");
2634 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2640 switch (link->dest.kind) { \
2641 case FZ_LINK_GOTO: \
2645 pageno = link->dest.ld.gotor.page; \
2649 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2650 p.y = link->dest.ld.gotor.lt.y; \
2651 fz_transform_point (&p, &pdim->lctm); \
2653 tup_v = caml_alloc_tuple (2); \
2654 ret_v = caml_alloc_small (1, ugoto); \
2655 Field (tup_v, 0) = Val_int (pageno); \
2656 Field (tup_v, 1) = Val_int (p.y); \
2657 Field (ret_v, 0) = tup_v; \
2662 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2663 ret_v = caml_alloc_small (1, uuri); \
2664 Field (ret_v, 0) = str_v; \
2667 case FZ_LINK_LAUNCH: \
2668 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2669 ret_v = caml_alloc_small (1, ulaunch); \
2670 Field (ret_v, 0) = str_v; \
2673 case FZ_LINK_NAMED: \
2674 str_v = caml_copy_string (link->dest.ld.named.named); \
2675 ret_v = caml_alloc_small (1, unamed); \
2676 Field (ret_v, 0) = str_v; \
2679 case FZ_LINK_GOTOR: \
2680 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2681 pageno = link->dest.ld.gotor.page; \
2682 tup_v = caml_alloc_tuple (2); \
2683 ret_v = caml_alloc_small (1, uremote); \
2684 Field (tup_v, 0) = str_v; \
2685 Field (tup_v, 1) = Val_int (pageno); \
2686 Field (ret_v, 0) = tup_v; \
2693 snprintf (buf, sizeof (buf), \
2694 "unhandled link kind %d", link->dest.kind); \
2695 str_v = caml_copy_string (buf); \
2696 ret_v = caml_alloc_small (1, uunexpected); \
2697 Field (ret_v, 0) = str_v; \
2703 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2705 CAMLparam2 (ptr_v
, n_v
);
2706 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2709 struct pagedim
*pdim
;
2710 char *s
= String_val (ptr_v
);
2712 ret_v
= Val_int (0);
2713 if (trylock ("ml_getlink")) {
2717 page
= parse_pointer ("ml_getlink", s
);
2718 ensureslinks (page
);
2719 pdim
= &state
.pagedims
[page
->pdimno
];
2720 link
= page
->slinks
[Int_val (n_v
)].link
;
2723 unlock ("ml_getlink");
2728 CAMLprim value
ml_getlinkcount (value ptr_v
)
2732 char *s
= String_val (ptr_v
);
2734 page
= parse_pointer ("ml_getlinkcount", s
);
2735 CAMLreturn (Val_int (page
->slinkcount
));
2738 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2740 CAMLparam2 (ptr_v
, n_v
);
2743 struct slink
*slink
;
2744 char *s
= String_val (ptr_v
);
2746 page
= parse_pointer ("ml_getlinkrect", s
);
2747 ret_v
= caml_alloc_tuple (4);
2748 if (trylock ("ml_getlinkrect")) {
2749 Field (ret_v
, 0) = Val_int (0);
2750 Field (ret_v
, 1) = Val_int (0);
2751 Field (ret_v
, 2) = Val_int (0);
2752 Field (ret_v
, 3) = Val_int (0);
2755 ensureslinks (page
);
2757 slink
= &page
->slinks
[Int_val (n_v
)];
2758 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2759 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2760 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2761 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2762 unlock ("ml_getlinkrect");
2768 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2770 CAMLparam3 (ptr_v
, x_v
, y_v
);
2771 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2774 char *s
= String_val (ptr_v
);
2775 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2776 struct pagedim
*pdim
;
2778 ret_v
= Val_int (0);
2779 if (trylock ("ml_whatsunder")) {
2783 page
= parse_pointer ("ml_whatsunder", s
);
2784 pdim
= &state
.pagedims
[page
->pdimno
];
2785 x
+= pdim
->bounds
.x0
;
2786 y
+= pdim
->bounds
.y0
;
2787 link
= getlink (page
, x
, y
);
2793 fz_text_block
*block
;
2796 for (block
= page
->text
->blocks
;
2797 block
< page
->text
->blocks
+ page
->text
->len
;
2802 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2805 for (line
= block
->lines
;
2806 line
< block
->lines
+ block
->len
;
2811 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2814 for (span
= line
->spans
;
2815 span
< line
->spans
+ line
->len
;
2820 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2823 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
2826 if (x
>= b
->x0
&& x
<= b
->x1
2827 && y
>= b
->y0
&& y
<= b
->y1
) {
2829 span
->style
->font
&& span
->style
->font
->name
2830 ? span
->style
->font
->name
2831 : "Span has no font name"
2833 FT_FaceRec
*face
= span
->style
->font
->ft_face
;
2834 if (face
&& face
->family_name
) {
2836 char *n1
= face
->family_name
;
2837 size_t l1
= strlen (n1
);
2838 size_t l2
= strlen (n2
);
2840 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2841 s
= malloc (l1
+ l2
+ 2);
2845 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2846 str_v
= caml_copy_string (s
);
2852 str_v
= caml_copy_string (n2
);
2854 ret_v
= caml_alloc_small (1, utext
);
2855 Field (ret_v
, 0) = str_v
;
2864 unlock ("ml_whatsunder");
2870 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2872 CAMLparam2 (ptr_v
, rect_v
);
2875 struct pagedim
*pdim
;
2876 int i
, x0
, x1
, y0
, y1
;
2877 char *s
= String_val (ptr_v
);
2879 fz_text_block
*block
;
2880 fz_text_span
*span
, *fspan
, *lspan
;
2881 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2883 if (trylock ("ml_seltext")) {
2887 page
= parse_pointer ("ml_seltext", s
);
2890 pdim
= &state
.pagedims
[page
->pdimno
];
2891 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2892 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2893 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2894 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2897 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2898 glColor3ub (128, 128, 128);
2899 glRecti (x0
, y0
, x1
, y1
);
2900 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2903 fspan
= lspan
= NULL
;
2905 for (block
= page
->text
->blocks
;
2906 block
< page
->text
->blocks
+ page
->text
->len
;
2908 for (line
= block
->lines
;
2909 line
< block
->lines
+ block
->len
;
2911 for (span
= line
->spans
;
2912 span
< line
->spans
+ line
->len
;
2914 for (i
= 0; i
< span
->len
; ++i
) {
2915 b
= &span
->text
[i
].bbox
;
2918 if (x0
>= b
->x0
&& x0
<= b
->x1
2919 && y0
>= b
->y0
&& y0
<= b
->y1
) {
2925 if (x1
>= b
->x0
&& x1
<= b
->x1
2926 && y1
>= b
->y0
&& y1
<= b
->y1
) {
2932 if (0 && selected
) {
2933 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2934 glColor3ub (128, 128, 128);
2935 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2936 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2942 if (y1
< y0
|| x1
< x0
) {
2945 if (fspan
== lspan
) {
2950 if (fline
!= lline
) {
2969 page
->fmark
.span
= fspan
;
2972 page
->lmark
.span
= lspan
;
2974 unlock ("ml_seltext");
2977 CAMLreturn (Val_unit
);
2980 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2985 for (i
= a
; i
<= b
; ++i
) {
2986 len
= fz_runetochar (buf
, span
->text
[i
].c
);
2987 ret
= fwrite (buf
, len
, 1, f
);
2990 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2991 len
, ret
, strerror (errno
));
2999 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3001 caml_failwith ("ml_popen not implemented under Cygwin");
3004 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3006 CAMLparam2 (command_v
, fds_v
);
3007 CAMLlocal2 (l_v
, tup_v
);
3010 value earg_v
= Nothing
;
3011 posix_spawnattr_t attr
;
3012 posix_spawn_file_actions_t fa
;
3013 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3015 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3016 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3019 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3020 msg
= "posix_spawnattr_init";
3024 #ifdef POSIX_SPAWN_USEVFORK
3025 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3026 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3031 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3034 tup_v
= Field (l_v
, 0);
3035 fd1
= Int_val (Field (tup_v
, 0));
3036 fd2
= Int_val (Field (tup_v
, 1));
3038 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3039 msg
= "posix_spawn_file_actions_addclose";
3045 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3046 msg
= "posix_spawn_file_actions_adddup2";
3053 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3054 msg
= "posix_spawn";
3059 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3060 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3064 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3065 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3070 unix_error (ret
, msg
, earg_v
);
3072 CAMLreturn (Val_unit
);
3076 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3084 fz_text_block
*block
;
3085 int fd
= Int_val (fd_v
);
3086 char *s
= String_val (ptr_v
);
3088 if (trylock ("ml_copysel")) {
3092 page
= parse_pointer ("ml_sopysel", s
);
3094 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3095 fprintf (stderr
, "nothing to copy\n");
3099 f
= fdopen (fd
, "w");
3101 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3106 for (block
= page
->text
->blocks
;
3107 block
< page
->text
->blocks
+ page
->text
->len
;
3109 for (line
= block
->lines
;
3110 line
< block
->lines
+ block
->len
;
3112 for (span
= line
->spans
;
3113 span
< line
->spans
+ line
->len
;
3117 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3118 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3119 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3122 if (pipespan (f
, span
, a
, b
)) {
3125 if (span
== line
->spans
+ line
->len
- 1) {
3126 if (putc ('\n', f
) == EOF
) {
3128 "failed break line on sel pipe: %s\n",
3133 if (span
== page
->lmark
.span
) {
3141 page
->lmark
.span
= NULL
;
3142 page
->fmark
.span
= NULL
;
3146 int ret
= fclose (f
);
3149 if (errno
!= ECHILD
) {
3150 fprintf (stderr
, "failed to close sel pipe: %s\n",
3156 unlock ("ml_copysel");
3161 fprintf (stderr
, "failed to close sel pipe: %s\n",
3165 CAMLreturn (Val_unit
);
3168 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3170 CAMLparam1 (pagedimno_v
);
3172 int pagedimno
= Int_val (pagedimno_v
);
3175 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3176 if (trylock ("ml_getpdimrect")) {
3177 box
= fz_empty_rect
;
3180 box
= state
.pagedims
[pagedimno
].mediabox
;
3181 unlock ("ml_getpdimrect");
3184 Store_double_field (ret_v
, 0, box
.x0
);
3185 Store_double_field (ret_v
, 1, box
.x1
);
3186 Store_double_field (ret_v
, 2, box
.y0
);
3187 Store_double_field (ret_v
, 3, box
.y1
);
3192 static double getmaxw (void)
3198 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3201 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
3202 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
3205 maxw
= MAX (w
, maxw
);
3210 CAMLprim value
ml_getmaxw (value unit_v
)
3212 CAMLparam1 (unit_v
);
3216 if (trylock ("ml_getmaxw")) {
3220 unlock ("ml_getmaxw");
3222 ret_v
= caml_copy_double (maxw
);
3226 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3227 value dw_v
, value cols_v
)
3229 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3233 double maxw
= 0.0, maxh
= 0.0;
3235 double winw
= Int_val (winw_v
);
3236 double winh
= Int_val (winh_v
);
3237 double dw
= Int_val (dw_v
);
3238 double cols
= Int_val (cols_v
);
3239 double pw
= 1.0, ph
= 1.0, aspect
;
3241 if (trylock ("ml_zoom_for_height")) {
3245 if (state
.proportional
) {
3246 maxw
= getmaxw () / cols
;
3249 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3252 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
3254 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
3256 fz_transform_rect (&rect
, &rm
);
3257 x0
= MIN (rect
.x0
, rect
.x1
);
3258 x1
= MAX (rect
.x0
, rect
.x1
);
3259 y0
= MIN (rect
.y0
, rect
.y1
);
3260 y1
= MAX (rect
.y0
, rect
.y1
);
3262 w
= (x1
- x0
) / cols
;
3265 if (state
.proportional
) {
3267 scaledh
= h
* scale
;
3274 if (scaledh
> maxh
) {
3282 zoom
= (winh
* aspect
+ dw
) / winw
;
3284 unlock ("ml_zoom_for_height");
3286 ret_v
= caml_copy_double (zoom
);
3290 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3292 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3294 int pt
= Int_val(pt_v
);
3295 int x
= Int_val (x_v
);
3296 int y
= Int_val (y_v
);
3299 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3300 ret_v
= caml_copy_double (w
);
3304 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3306 CAMLparam2 (pt_v
, string_v
);
3308 int pt
= Int_val (pt_v
);
3311 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3312 ret_v
= caml_copy_double (w
);
3316 CAMLprim value
ml_getpagebox (value opaque_v
)
3318 CAMLparam1 (opaque_v
);
3324 char *s
= String_val (opaque_v
);
3325 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3327 ret_v
= caml_alloc_tuple (4);
3328 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3329 dev
->hints
|= FZ_IGNORE_SHADE
;
3331 switch (page
->type
) {
3333 ctm
= pagectm (page
);
3334 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3338 ctm
= pagectm (page
);
3339 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3343 rect
= fz_infinite_rect
;
3347 fz_free_device (dev
);
3348 fz_round_rect (&bbox
, &rect
);
3349 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3350 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3351 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3352 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3357 CAMLprim value
ml_setaalevel (value level_v
)
3359 CAMLparam1 (level_v
);
3361 state
.aalevel
= Int_val (level_v
);
3362 CAMLreturn (Val_unit
);
3366 #include <X11/Xlib.h>
3372 GLXDrawable drawable
;
3375 #include "keysym2ucs.c"
3377 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3379 CAMLparam1 (keysym_v
);
3381 KeySym keysym
= Int_val (keysym_v
);
3386 rune
= keysym2ucs (keysym
);
3387 len
= fz_runetochar (buf
, rune
);
3389 str_v
= caml_copy_string (buf
);
3393 CAMLprim value
ml_glx (value win_v
)
3396 XVisualInfo
*visual
;
3397 int screen
, wid
= Int_val (win_v
);
3398 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3400 glx
.dpy
= XOpenDisplay (NULL
);
3402 caml_failwith ("XOpenDisplay");
3405 screen
= DefaultScreen (glx
.dpy
);
3406 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3408 XCloseDisplay (glx
.dpy
);
3410 caml_failwith ("glXChooseVisual");
3413 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3416 XCloseDisplay (glx
.dpy
);
3418 caml_failwith ("glXCreateContext");
3421 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3422 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3423 XCloseDisplay (glx
.dpy
);
3426 caml_failwith ("glXMakeCurrent");
3429 CAMLreturn (Val_unit
);
3432 CAMLprim value
ml_swapb (value unit_v
)
3434 CAMLparam1 (unit_v
);
3435 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3436 CAMLreturn (Val_unit
);
3439 CAMLprim value
ml_glxsync (value unit_v
)
3441 CAMLparam1 (unit_v
);
3442 if (glx
.dpy
&& glx
.ctx
) {
3446 CAMLreturn (Val_unit
);
3449 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3450 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3452 CAMLprim value
ml_platform (value unit_v
)
3454 CAMLparam1 (unit_v
);
3455 int platid
= piunknown
;
3457 #if defined __linux__
3459 #elif defined __CYGWIN__
3461 #elif defined __DragonFly__
3462 platid
= pidragonflybsd
;
3463 #elif defined __FreeBSD__
3465 #elif defined __OpenBSD__
3467 #elif defined __NetBSD__
3469 #elif defined __sun__
3471 #elif defined __APPLE__
3474 CAMLreturn (Val_int (platid
));
3477 CAMLprim value
ml_cloexec (value fd_v
)
3480 int fd
= Int_val (fd_v
);
3482 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3483 uerror ("fcntl", Nothing
);
3485 CAMLreturn (Val_unit
);
3488 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3490 CAMLparam2 (w_v
, h_v
);
3493 int w
= Int_val (w_v
);
3494 int h
= Int_val (h_v
);
3495 int cs
= Int_val (cs_v
);
3497 if (state
.pbo_usable
) {
3498 pbo
= calloc (sizeof (*pbo
), 1);
3500 err (1, "calloc pbo");
3512 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3515 state
.glGenBuffersARB (1, &pbo
->id
);
3516 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3517 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3518 NULL
, GL_STREAM_DRAW
);
3519 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3521 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3523 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3524 state
.glDeleteBuffersARB (1, &pbo
->id
);
3526 ret_v
= caml_copy_string ("0");
3532 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3534 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3538 err (1, "malloc %d bytes failed", res
+1);
3540 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3542 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3544 ret_v
= caml_copy_string (s
);
3549 ret_v
= caml_copy_string ("0");
3554 CAMLprim value
ml_freepbo (value s_v
)
3557 char *s
= String_val (s_v
);
3558 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3561 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3563 tile
->pbo
->ptr
= NULL
;
3564 tile
->pbo
->size
= -1;
3566 CAMLreturn (Val_unit
);
3569 CAMLprim value
ml_unmappbo (value s_v
)
3572 char *s
= String_val (s_v
);
3573 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3576 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3577 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3578 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3580 tile
->pbo
->ptr
= NULL
;
3581 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3583 CAMLreturn (Val_unit
);
3586 static void setuppbo (void)
3588 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3589 GGPA (glBindBufferARB
);
3590 if (state
.glBindBufferARB
) {
3591 GGPA (glUnmapBufferARB
);
3592 if (state
.glUnmapBufferARB
) {
3593 GGPA (glMapBufferARB
);
3594 if (state
.glMapBufferARB
) {
3595 GGPA (glBufferDataARB
);
3596 if (state
.glBufferDataARB
) {
3597 GGPA (glGenBuffersARB
);
3598 if (state
.glGenBuffersARB
) {
3599 GGPA (glDeleteBuffersARB
);
3600 if (state
.glDeleteBuffersARB
) {
3601 state
.pbo_usable
= 1;
3611 CAMLprim value
ml_pbo_usable (value unit_v
)
3613 CAMLparam1 (unit_v
);
3614 CAMLreturn (Val_bool (state
.pbo_usable
));
3617 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3619 CAMLparam3 (ptr_v
, x_v
, y_v
);
3620 CAMLlocal2 (ret_v
, tup_v
);
3622 char *s
= String_val (ptr_v
);
3623 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3624 struct pagedim
*pdim
;
3628 page
= parse_pointer ("ml_unproject", s
);
3629 pdim
= &state
.pagedims
[page
->pdimno
];
3631 ret_v
= Val_int (0);
3632 if (trylock ("ml_unproject")) {
3636 switch (page
->type
) {
3638 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3647 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3648 fz_invert_matrix (&ctm
, &ctm
);
3649 fz_transform_point (&p
, &ctm
);
3651 tup_v
= caml_alloc_tuple (2);
3652 ret_v
= caml_alloc_small (1, 1);
3653 Field (tup_v
, 0) = Val_int (p
.x
);
3654 Field (tup_v
, 1) = Val_int (p
.y
);
3655 Field (ret_v
, 0) = tup_v
;
3657 unlock ("ml_unproject");
3662 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3664 CAMLparam2 (pipe_v
, params_v
);
3665 CAMLlocal2 (trim_v
, fuzz_v
);
3672 struct sigaction sa
;
3674 state
.cr
= Int_val (Field (pipe_v
, 0));
3675 state
.cw
= Int_val (Field (pipe_v
, 1));
3676 state
.rotate
= Int_val (Field (params_v
, 0));
3677 state
.proportional
= Bool_val (Field (params_v
, 1));
3678 trim_v
= Field (params_v
, 2);
3679 texcount
= Int_val (Field (params_v
, 3));
3680 state
.sliceheight
= Int_val (Field (params_v
, 4));
3681 mustoresize
= Int_val (Field (params_v
, 5));
3682 colorspace
= Int_val (Field (params_v
, 6));
3683 fontpath
= String_val (Field (params_v
, 7));
3684 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3685 if (!state
.trimcachepath
) {
3686 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3689 haspboext
= Bool_val (Field (params_v
, 9));
3691 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3693 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3694 fuzz_v
= Field (trim_v
, 1);
3695 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3696 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3697 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3698 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3700 set_tex_params (colorspace
);
3703 state
.face
= load_font (fontpath
);
3707 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3709 state
.face
= load_builtin_font (base
, len
);
3711 if (!state
.face
) _exit (1);
3713 realloctexts (texcount
);
3720 sa
.sa_handler
= SIG_IGN
;
3721 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3723 sa
.sa_handler
= SIG_DFL
;
3724 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3726 if (sigemptyset (&sa
.sa_mask
)) {
3727 err (1, "sigemptyset");
3729 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3730 err (1, "sigaction");
3733 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3735 errx (1, "pthread_create: %s", strerror (ret
));
3738 CAMLreturn (Val_unit
);