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 */
28 #include <caml/fail.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/unixsupport.h>
35 #include <mupdf-internal.h>
37 #include <muxps-internal.h>
40 #include FT_FREETYPE_H
45 extern char **environ
;
48 #define MIN(a,b) ((a) < (b) ? (a) : (b))
49 #define MAX(a,b) ((a) > (b) ? (a) : (b))
52 #define NORETURN_ATTR __attribute__ ((noreturn))
53 #define UNUSED_ATTR __attribute__ ((unused))
54 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
55 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
59 #define OPTIMIZE_ATTR(n)
60 #define GCC_FMT_ATTR(a, b)
66 #define FMT_ptr_cast(p) (p)
67 #define FMT_ptr_cast2(p) (p)
69 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
70 err (int exitcode
, const char *fmt
, ...)
77 vfprintf (stderr
, fmt
, ap
);
79 fprintf (stderr
, ": %s\n", strerror (savederrno
));
84 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
85 errx (int exitcode
, const char *fmt
, ...)
90 vfprintf (stderr
, fmt
, ap
);
97 #ifndef GL_TEXTURE_RECTANGLE_ARB
98 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
102 #define GL_BGRA 0x80E1
105 #ifndef GL_UNSIGNED_INT_8_8_8_8
106 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
109 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
110 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
114 #define lprintf printf
119 #define ARSERT(cond) for (;;) { \
121 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
137 struct slice slices
[1];
148 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
156 enum { DPDF
, DXPS
, DCBZ
};
165 fz_text_sheet
*sheet
;
172 fz_display_list
*dlist
;
174 struct slink
*slinks
;
179 void (*freepage
) (void *);
185 struct pagedim
*pagedims
;
204 fz_colorspace
*colorspace
;
212 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
226 void (*closedoc
) (void);
227 void (*freepage
) (void *);
234 void (*glBindBufferARB
) (GLenum
, GLuint
);
235 GLboolean (*glUnmapBufferARB
) (GLenum
);
236 void *(*glMapBufferARB
) (GLenum
, GLenum
);
237 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
238 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
239 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
248 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
250 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
253 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
255 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
258 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
260 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
261 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
264 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
266 static void lock (const char *cap
)
268 int ret
= pthread_mutex_lock (&mutex
);
270 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
274 static void unlock (const char *cap
)
276 int ret
= pthread_mutex_unlock (&mutex
);
278 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
282 static int trylock (const char *cap
)
284 int ret
= pthread_mutex_trylock (&mutex
);
285 if (ret
&& ret
!= EBUSY
) {
286 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
291 static void *parse_pointer (const char *cap
, const char *s
)
296 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
298 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
303 static double now (void)
307 if (gettimeofday (&tv
, NULL
)) {
308 err (1, "gettimeofday");
310 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
313 static int hasdata (void)
316 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
317 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
321 CAMLprim value
ml_hasdata (value fd_v
)
326 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
327 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
328 CAMLreturn (Val_bool (avail
> 0));
331 static void readdata (void *p
, int size
)
336 n
= read (state
.cr
, p
, size
);
338 if (errno
== EINTR
) goto again
;
339 err (1, "read (req %d, ret %zd)", size
, n
);
342 if (!n
) errx (1, "EOF while reading");
343 errx (1, "read (req %d, ret %zd)", size
, n
);
347 static void writedata (char *p
, int size
)
352 buf
[0] = (size
>> 24) & 0xff;
353 buf
[1] = (size
>> 16) & 0xff;
354 buf
[2] = (size
>> 8) & 0xff;
355 buf
[3] = (size
>> 0) & 0xff;
357 n
= write (state
.cw
, buf
, 4);
359 if (!n
) errx (1, "EOF while writing length");
360 err (1, "write %zd", n
);
363 n
= write (state
.cw
, p
, size
);
365 if (!n
) errx (1, "EOF while writing data");
366 err (1, "write (req %d, ret %zd)", size
, n
);
370 static int readlen (void)
375 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
378 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
386 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
389 len
= vsnprintf (buf
, size
, fmt
, ap
);
392 if (len
> -1 && len
< size
) {
393 writedata (buf
, len
);
403 buf
= realloc (buf
, size
);
408 static void closepdf (void)
411 pdf_close_document (state
.u
.pdf
);
416 static void closexps (void)
419 xps_close_document (state
.u
.xps
);
424 static void closecbz (void)
427 cbz_close_document (state
.u
.cbz
);
432 static void freepdfpage (void *ptr
)
434 pdf_free_page (state
.u
.pdf
, ptr
);
437 static void freeemptyxxxpage (void *ptr
)
442 static void freexpspage (void *ptr
)
444 xps_free_page (state
.u
.xps
, ptr
);
447 static void freecbzpage (void *ptr
)
449 cbz_free_page (state
.u
.cbz
, ptr
);
452 static void openxref (char *filename
, char *password
)
456 for (i
= 0; i
< state
.texcount
; ++i
) {
457 state
.texowners
[i
].w
= -1;
458 state
.texowners
[i
].slice
= NULL
;
461 if (state
.closedoc
) state
.closedoc ();
463 len
= strlen (filename
);
469 ext
[0] = tolower ((int) filename
[len
-3]);
470 ext
[1] = tolower ((int) filename
[len
-2]);
471 ext
[2] = tolower ((int) filename
[len
-1]);
473 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
476 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
481 if (state
.pagedims
) {
482 free (state
.pagedims
);
483 state
.pagedims
= NULL
;
485 state
.pagedimcount
= 0;
487 fz_set_aa_level (state
.ctx
, state
.aalevel
);
488 switch (state
.type
) {
490 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
491 if (pdf_needs_password (state
.u
.pdf
)) {
492 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
494 errx (1, "invalid password");
497 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
498 state
.closedoc
= closepdf
;
499 state
.freepage
= freepdfpage
;
503 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
504 state
.pagecount
= xps_count_pages (state
.u
.xps
);
505 state
.closedoc
= closexps
;
506 state
.freepage
= freexpspage
;
510 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
511 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
512 state
.closedoc
= closecbz
;
513 state
.freepage
= freecbzpage
;
518 static void pdfinfo (void)
520 if (state
.type
== DPDF
) {
523 printd ("info PDF version\t%d.%d",
524 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
526 infoobj
= pdf_dict_gets (state
.u
.pdf
->trailer
, "Info");
530 char *items
[] = { "Title", "Author", "Creator",
531 "Producer", "CreationDate" };
533 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
534 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
535 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
538 printd ("title %s", s
);
540 printd ("info %s\t%s", items
[i
], s
);
542 fz_free (state
.ctx
, s
);
549 static void unlinktile (struct tile
*tile
)
553 for (i
= 0; i
< tile
->slicecount
; ++i
) {
554 struct slice
*s
= &tile
->slices
[i
];
556 if (s
->texindex
!= -1) {
557 if (state
.texowners
[s
->texindex
].slice
== s
) {
558 state
.texowners
[s
->texindex
].slice
= NULL
;
564 static void freepage (struct page
*page
)
567 fz_free_text_page (state
.ctx
, page
->text
);
570 fz_free_text_sheet (state
.ctx
, page
->sheet
);
575 page
->freepage (page
->u
.ptr
);
576 fz_free_display_list (state
.ctx
, page
->dlist
);
580 static void freetile (struct tile
*tile
)
585 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
588 fz_drop_pixmap (state
.ctx
, state
.pig
);
590 state
.pig
= tile
->pixmap
;
595 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
604 static int cacheline32bytes
;
606 static void __attribute__ ((constructor
)) clcheck (void)
608 char **envp
= environ
;
613 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
615 cacheline32bytes
= auxv
[1] == 32;
621 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
623 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
624 if (cacheline32bytes
&& size
> 32) {
625 intptr_t a1
, a2
, diff
;
627 vector
unsigned char v
= vec_splat_u8 (-1);
628 vector
unsigned char *p
;
630 a1
= a2
= (intptr_t) pixmap
->samples
;
631 a2
= (a1
+ 31) & ~31;
636 while (a1
!= a2
) *(char *) a1
++ = 0xff;
637 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
638 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
640 vec_st (v
, i
+ 16, p
);
642 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
644 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
647 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
650 static void trimctm (pdf_page
*page
, int pindex
)
653 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
655 if (!pdim
->tctmready
) {
656 if (state
.trimmargins
) {
658 fz_matrix rm
, sm
, tm
, im
, ctm1
;
660 fz_rotate (&rm
, -pdim
->rotate
);
661 fz_scale (&sm
, 1, -1);
662 fz_concat (&ctm
, &rm
, &sm
);
663 realbox
= pdim
->mediabox
;
664 fz_transform_rect (&realbox
, &ctm
);
665 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
666 fz_concat (&ctm1
, &ctm
, &tm
);
667 fz_invert_matrix (&im
, &page
->ctm
);
668 fz_concat (&ctm
, &im
, &ctm1
);
678 static fz_matrix
pagectm (struct page
*page
)
682 if (page
->type
== DPDF
) {
683 trimctm (page
->u
.pdfpage
, page
->pdimno
);
685 &state
.pagedims
[page
->pdimno
].tctm
,
686 &state
.pagedims
[page
->pdimno
].ctm
);
689 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
691 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
692 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
697 static void *loadpage (int pageno
, int pindex
)
700 struct page
*page
= NULL
;
702 page
= calloc (sizeof (struct page
), 1);
704 err (1, "calloc page %d", pageno
);
707 page
->dlist
= fz_new_display_list (state
.ctx
);
708 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
710 switch (state
.type
) {
712 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
713 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
715 page
->freepage
= freepdfpage
;
719 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
720 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
722 page
->freepage
= freexpspage
;
726 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
727 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
729 page
->freepage
= freecbzpage
;
733 fz_catch (state
.ctx
) {
735 page
->freepage
= freeemptyxxxpage
;
737 fz_free_device (dev
);
739 page
->pdimno
= pindex
;
740 page
->pageno
= pageno
;
741 page
->sgen
= state
.gen
;
742 page
->tgen
= state
.gen
;
743 page
->type
= state
.type
;
748 static struct tile
*alloctile (int h
)
755 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
756 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
757 tile
= calloc (tilesize
, 1);
759 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
761 for (i
= 0; i
< slicecount
; ++i
) {
762 int sh
= MIN (h
, state
.sliceheight
);
763 tile
->slices
[i
].h
= sh
;
764 tile
->slices
[i
].texindex
= -1;
767 tile
->slicecount
= slicecount
;
768 tile
->sliceheight
= state
.sliceheight
;
778 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
779 const fz_matrix
*ctm
, float alpha
)
781 struct obs
*obs
= dev
->user
;
783 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
785 fz_rect rect
= fz_unit_rect
;
787 fz_transform_rect (&rect
, ctm
);
788 fz_round_rect (&b
, &rect
);
789 fz_intersect_irect (&b
, &obs
->b
);
790 obs
->cured
= b
.x0
== obs
->b
.x0
793 && b
.y1
== obs
->b
.y1
;
797 static int obscured (struct page
*page
, fz_irect bbox
)
804 memset (&dev
, 0, sizeof (dev
));
805 memset (&obs
, 0, sizeof (obs
));
810 dev
.fill_image
= obs_fill_image
;
812 fz_rect_from_irect (&rect
, &bbox
);
813 ctm
= pagectm (page
);
814 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
817 #define OBSCURED obscured
819 #define OBSCURED(a, b) 0
822 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
830 struct pagedim
*pdim
;
832 tile
= alloctile (h
);
833 pdim
= &state
.pagedims
[page
->pdimno
];
838 bbox
.x1
= bbox
.x0
+ w
;
839 bbox
.y1
= bbox
.y0
+ h
;
842 if (state
.pig
->w
== w
844 && state
.pig
->colorspace
== state
.colorspace
) {
845 tile
->pixmap
= state
.pig
;
846 tile
->pixmap
->x
= bbox
.x0
;
847 tile
->pixmap
->y
= bbox
.y0
;
850 fz_drop_pixmap (state
.ctx
, state
.pig
);
857 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
863 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
869 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
870 clearpixmap (tile
->pixmap
);
872 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
873 ctm
= pagectm (page
);
874 fz_rect_from_irect (&rect
, &bbox
);
875 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
876 fz_free_device (dev
);
881 static void initpdims (void)
884 int pageno
, trim
, show
;
890 if (state
.trimmargins
&& state
.trimcachepath
) {
891 trimf
= fopen (state
.trimcachepath
, "rb");
893 trimf
= fopen (state
.trimcachepath
, "wb");
897 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
902 switch (state
.type
) {
904 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
906 if (state
.trimmargins
) {
911 page
= pdf_load_page (state
.u
.pdf
, pageno
);
912 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
913 trim
= state
.trimanew
|| !obj
;
919 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
920 dev
->hints
|= FZ_IGNORE_SHADE
;
921 fz_invert_matrix (&ctm
, &page
->ctm
);
922 pdf_run_page (state
.u
.pdf
, page
, dev
, &fz_identity
, NULL
);
923 fz_free_device (dev
);
925 rect
.x0
+= state
.trimfuzz
.x0
;
926 rect
.x1
+= state
.trimfuzz
.x1
;
927 rect
.y0
+= state
.trimfuzz
.y0
;
928 rect
.y1
+= state
.trimfuzz
.y1
;
929 fz_transform_rect (&rect
, &ctm
);
930 fz_intersect_rect (&rect
, &page
->mediabox
);
932 if (fz_is_empty_rect (&rect
)) {
933 mediabox
= page
->mediabox
;
939 obj
= pdf_new_array (state
.ctx
, 4);
940 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x0
));
941 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y0
));
942 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x1
));
943 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y1
));
944 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
947 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
948 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
949 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
950 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
953 rotate
= page
->rotate
;
954 pdf_free_page (state
.u
.pdf
, page
);
956 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
958 printd ("progress %f Trimming %d",
959 (double) (pageno
+ 1) / state
.pagecount
,
963 fz_catch (state
.ctx
) {
964 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
970 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
972 if (fz_is_empty_rect (&mediabox
)) {
973 fprintf (stderr
, "cannot find page size for page %d\n",
981 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
983 if (!fz_is_empty_rect (&cropbox
)) {
984 fz_intersect_rect (&mediabox
, &cropbox
);
986 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
996 page
= xps_load_page (state
.u
.xps
, pageno
);
997 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
999 if (state
.trimmargins
) {
1003 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1004 dev
->hints
|= FZ_IGNORE_SHADE
;
1005 xps_run_page (state
.u
.xps
, page
, dev
,
1006 &fz_identity
, NULL
);
1007 fz_free_device (dev
);
1009 rect
.x0
+= state
.trimfuzz
.x0
;
1010 rect
.x1
+= state
.trimfuzz
.x1
;
1011 rect
.y0
+= state
.trimfuzz
.y0
;
1012 rect
.y1
+= state
.trimfuzz
.y1
;
1013 fz_intersect_rect (&rect
, &mediabox
);
1015 if (!fz_is_empty_rect (&rect
)) {
1019 xps_free_page (state
.u
.xps
, page
);
1020 printd ("progress %f loading %d",
1021 (double) (pageno
+ 1) / state
.pagecount
,
1024 fz_catch (state
.ctx
) {
1032 if (state
.trimmargins
&& trimw
) {
1035 fz_try (state
.ctx
) {
1036 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1037 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1038 cbz_free_page (state
.u
.cbz
, page
);
1039 printd ("progress %f Trimming %d",
1040 (double) (pageno
+ 1) / state
.pagecount
,
1043 fz_catch (state
.ctx
) {
1044 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1051 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1053 err (1, "fwrite trim mediabox");
1059 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1061 err (1, "fread trim mediabox");
1065 mediabox
.x0
= mediabox
.y0
= 0;
1074 ARSERT (0 && state
.type
);
1077 if (state
.pagedimcount
== 0
1078 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1079 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1082 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1083 state
.pagedims
= realloc (state
.pagedims
, size
);
1084 if (!state
.pagedims
) {
1085 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1086 size
, state
.pagedimcount
+ 1);
1089 p
= &state
.pagedims
[state
.pagedimcount
++];
1091 p
->mediabox
= mediabox
;
1096 if (state
.trimmargins
) {
1097 printd ("progress 1 Trimmed %d pages in %f seconds",
1098 state
.pagecount
, end
- start
);
1101 printd ("vmsg Processed %d pages in %f seconds",
1102 state
.pagecount
, end
- start
);
1106 if (fclose (trimf
)) {
1112 static void layout (double zoom1
)
1117 double zoom
, w
, maxw
= 0.0;
1118 struct pagedim
*p
= state
.pagedims
;
1120 if (state
.fitmodel
== FitProportional
) {
1124 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1129 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1131 fz_transform_rect (&rect
, &rm
);
1133 x0
= MIN (rect
.x0
, rect
.x1
);
1134 x1
= MAX (rect
.x0
, rect
.x1
);
1137 maxw
= MAX (w
, maxw
);
1142 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1144 fz_matrix tm
, sm
, rm
;
1146 fz_rotate (&ctm
, state
.rotate
);
1147 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1149 fz_transform_rect (&box
, &rm
);
1150 w
= box
.x1
- box
.x0
;
1152 switch (state
.fitmodel
) {
1153 case FitProportional
:
1159 h
= box
.y1
- box
.y0
;
1170 ARSERT (0 && state
.fitmodel
);
1173 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1174 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1176 fz_rotate (&rm
, p
->rotate
);
1177 p
->pagebox
= p
->mediabox
;
1178 fz_transform_rect (&p
->pagebox
, &rm
);
1179 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1180 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1184 fz_transform_rect (&rect
, &ctm
);
1185 fz_round_rect (&p
->bounds
, &rect
);
1187 p
->left
= (state
.fitmodel
== FitProportional
)
1188 ? ((maxw
- w
) * zoom
) / 2.0
1192 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1193 fz_scale (&sm
, zoom
, -zoom
);
1194 fz_concat (&ctm
, &tm
, &sm
);
1195 fz_concat (&p
->lctm
, &ctm
, &rm
);
1200 while (p
-- != state
.pagedims
) {
1201 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1202 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1203 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1204 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1208 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1213 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1214 desttoanchor (fz_link_dest
*dest
)
1218 struct pagedim
*pdim
= state
.pagedims
;
1223 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1224 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1226 pdim
= &state
.pagedims
[i
];
1228 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1230 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1231 p
.x
= dest
->ld
.gotor
.lt
.x
;
1232 p
.y
= dest
->ld
.gotor
.lt
.y
;
1233 fz_transform_point (&p
, &pdim
->lctm
);
1237 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1238 double x0
, x1
, y0
, y1
;
1240 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1241 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1243 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1244 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1246 a
.n
= dest
->ld
.gotor
.page
;
1251 static void recurse_outline (fz_outline
*outline
, int level
)
1254 struct anchor a
= desttoanchor (&outline
->dest
);
1257 printd ("o %d %d %d %d %s",
1258 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1260 if (outline
->down
) {
1261 recurse_outline (outline
->down
, level
+ 1);
1263 outline
= outline
->next
;
1267 static void process_outline (void)
1269 fz_outline
*outline
;
1271 if (!state
.needoutline
) return;
1273 state
.needoutline
= 0;
1274 switch (state
.type
) {
1276 outline
= pdf_load_outline (state
.u
.pdf
);
1279 outline
= xps_load_outline (state
.u
.xps
);
1286 recurse_outline (outline
, 0);
1287 fz_free_outline (state
.ctx
, outline
);
1291 static char *strofspan (fz_text_span
*span
)
1296 size_t size
= 0, cap
= 80;
1298 p
= malloc (cap
+ 1);
1299 if (!p
) return NULL
;
1301 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1302 int n
= fz_runetochar (utf8
, ch
->c
);
1303 if (size
+ n
> cap
) {
1305 p
= realloc (p
, cap
+ 1);
1306 if (!p
) return NULL
;
1309 memcpy (p
+ size
, utf8
, n
);
1316 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1317 fz_text_span
*span
, fz_matrix ctm
,
1318 int stop
, int pageno
, double start
)
1325 fz_point p1
, p2
, p3
, p4
;
1327 p
= strofspan (span
);
1330 ret
= regexec (re
, p
, 1, &rm
, 0);
1333 if (ret
!= REG_NOMATCH
) {
1336 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1337 printd ("msg regexec error `%.*s'",
1338 (int) size
, errbuf
);
1346 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1347 c
+= fz_runelen (span
->text
[a
].c
);
1349 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1350 c
+= fz_runelen (span
->text
[b
].c
);
1353 if (fz_runelen (span
->text
[b
].c
) > 1) {
1357 fz_text_char_bbox (&sb
, span
, a
);
1358 fz_text_char_bbox (&eb
, span
, b
);
1370 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1377 printd ("progress 1 found at %d `%.*s' in %f sec",
1378 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1382 printd ("match %d %d %f %f %f %f %f %f %f %f",
1394 static int compareblocks (const void *l
, const void *r
)
1396 fz_text_block
const *ls
= l
;
1397 fz_text_block
const* rs
= r
;
1398 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1401 /* wishful thinking function */
1402 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1407 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1409 fz_text_sheet
*sheet
;
1410 struct pagedim
*pdim
, *pdimprev
;
1411 int stop
= 0, niters
= 0;
1414 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1418 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1419 if (niters
++ == 5) {
1422 printd ("progress 1 attention requested aborting search at %d",
1427 printd ("progress %f searching in page %d",
1428 (double) (pageno
+ 1) / state
.pagecount
,
1433 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1434 pdim
= &state
.pagedims
[i
];
1435 if (pdim
->pageno
== pageno
) {
1438 if (pdim
->pageno
> pageno
) {
1447 sheet
= fz_new_text_sheet (state
.ctx
);
1448 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1449 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1451 switch (state
.type
) {
1454 fz_try (state
.ctx
) {
1455 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1456 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1457 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1458 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1460 fz_catch (state
.ctx
) {
1461 fz_free_device (tdev
);
1468 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1469 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1473 ARSERT (0 && state
.type
);
1476 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1477 fz_free_device (tdev
);
1479 for (j
= 0; j
< text
->len
; ++j
) {
1481 fz_text_block
*block
;
1483 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1485 for (k
= 0; k
< block
->len
; ++k
) {
1490 line
= &block
->lines
[k
];
1491 if (line
->bbox
.y0
< y
+ 1) continue;
1494 line
= &block
->lines
[block
->len
- 1 - k
];
1495 if (line
->bbox
.y0
> y
- 1) continue;
1498 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
1499 fz_text_span
*span
= line
->spans
[spannum
];
1501 switch (matchspan (re
, text
, span
, ctm
,
1502 stop
, pageno
, start
)) {
1504 case 1: stop
= 1; break;
1505 case -1: stop
= 1; goto endloop
;
1520 fz_free_text_page (state
.ctx
, text
);
1521 fz_free_text_sheet (state
.ctx
, sheet
);
1523 state
.freepage (u
.ptr
);
1528 printd ("progress 1 no matches %f sec", end
- start
);
1530 printd ("clearrects");
1533 static void set_tex_params (int colorspace
)
1540 switch (colorspace
) {
1542 state
.texiform
= GL_RGBA8
;
1543 state
.texform
= GL_RGBA
;
1544 state
.texty
= GL_UNSIGNED_BYTE
;
1545 state
.colorspace
= fz_device_rgb
;
1548 state
.texiform
= GL_RGBA8
;
1549 state
.texform
= GL_BGRA
;
1550 state
.texty
= endianness
.s
> 1
1551 ? GL_UNSIGNED_INT_8_8_8_8
1552 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1553 state
.colorspace
= fz_device_bgr
;
1556 state
.texiform
= GL_LUMINANCE_ALPHA
;
1557 state
.texform
= GL_LUMINANCE_ALPHA
;
1558 state
.texty
= GL_UNSIGNED_BYTE
;
1559 state
.colorspace
= fz_device_gray
;
1562 errx (1, "invalid colorspce %d", colorspace
);
1566 static void realloctexts (int texcount
)
1570 if (texcount
== state
.texcount
) return;
1572 if (texcount
< state
.texcount
) {
1573 glDeleteTextures (state
.texcount
- texcount
,
1574 state
.texids
+ texcount
);
1577 size
= texcount
* sizeof (*state
.texids
);
1578 state
.texids
= realloc (state
.texids
, size
);
1579 if (!state
.texids
) {
1580 err (1, "realloc texids %" FMT_s
, size
);
1583 size
= texcount
* sizeof (*state
.texowners
);
1584 state
.texowners
= realloc (state
.texowners
, size
);
1585 if (!state
.texowners
) {
1586 err (1, "realloc texowners %" FMT_s
, size
);
1588 if (texcount
> state
.texcount
) {
1591 glGenTextures (texcount
- state
.texcount
,
1592 state
.texids
+ state
.texcount
);
1593 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1594 state
.texowners
[i
].w
= -1;
1595 state
.texowners
[i
].slice
= NULL
;
1598 state
.texcount
= texcount
;
1602 static char *mbtoutf8 (char *s
)
1608 len
= mbstowcs (NULL
, s
, strlen (s
));
1613 if (len
== (size_t) -1) {
1618 tmp
= malloc (len
* sizeof (wchar_t));
1623 ret
= mbstowcs (tmp
, s
, len
);
1624 if (ret
== (size_t) -1) {
1630 for (i
= 0; i
< ret
; ++i
) {
1631 len
+= fz_runelen (tmp
[i
]);
1634 p
= r
= malloc (len
+ 1);
1640 for (i
= 0; i
< ret
; ++i
) {
1641 p
+= fz_runetochar (p
, tmp
[i
]);
1648 CAMLprim value
ml_mbtoutf8 (value s_v
)
1654 s
= String_val (s_v
);
1660 ret_v
= caml_copy_string (r
);
1666 static void * mainloop (void *unused
)
1669 int len
, ret
, oldlen
= 0;
1674 errx (1, "readlen returned 0");
1677 if (oldlen
< len
+ 1) {
1678 p
= realloc (p
, len
+ 1);
1680 err (1, "realloc %d failed", len
+ 1);
1687 if (!strncmp ("open", p
, 4)) {
1694 ret
= sscanf (p
+ 5, " %d %n", &wthack
, &off
);
1696 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1699 filename
= p
+ 5 + off
;
1700 filenamelen
= strlen (filename
);
1701 password
= filename
+ filenamelen
+ 1;
1704 openxref (filename
, password
);
1710 utf8filename
= mbtoutf8 (filename
);
1711 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1712 if (utf8filename
!= filename
) {
1713 free (utf8filename
);
1716 state
.needoutline
= 1;
1718 else if (!strncmp ("cs", p
, 2)) {
1721 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1723 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1726 set_tex_params (colorspace
);
1727 for (i
= 0; i
< state
.texcount
; ++i
) {
1728 state
.texowners
[i
].w
= -1;
1729 state
.texowners
[i
].slice
= NULL
;
1733 else if (!strncmp ("freepage", p
, 8)) {
1736 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1738 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1742 else if (!strncmp ("freetile", p
, 8)) {
1745 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1747 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1751 else if (!strncmp ("search", p
, 6)) {
1752 int icase
, pageno
, y
, ret
, len2
, forward
;
1756 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1757 &icase
, &pageno
, &y
, &forward
, &len2
);
1759 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1762 pattern
= p
+ 6 + len2
;
1763 ret
= regcomp (&re
, pattern
,
1764 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1769 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1770 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1773 search (&re
, pageno
, y
, forward
);
1777 else if (!strncmp ("geometry", p
, 8)) {
1782 ret
= sscanf (p
+ 8, " %d %d %f %d", &w
, &h
, &zoom
, &fitmodel
);
1784 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1792 for (i
= 0; i
< state
.texcount
; ++i
) {
1793 state
.texowners
[i
].slice
= NULL
;
1796 state
.fitmodel
= fitmodel
;
1801 unlock ("geometry");
1802 printd ("continue %d", state
.pagecount
);
1804 else if (!strncmp ("reqlayout", p
, 9)) {
1807 int rotate
, fitmodel
, off
;
1810 ret
= sscanf (p
+ 9, " %d %d %f %n",
1811 &rotate
, &fitmodel
, &zoom
, &off
);
1813 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1816 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1819 state
.rotate
= rotate
;
1820 state
.fitmodel
= fitmodel
;
1824 nameddest
= p
+ 9 + off
;
1825 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1828 pdf_obj
*needle
, *obj
;
1830 needle
= pdf_new_string (state
.ctx
, nameddest
,
1831 strlen (nameddest
));
1832 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1834 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1836 a
= desttoanchor (&dest
);
1838 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1841 printd ("emsg failed to parse destination `%s'\n",
1846 printd ("emsg destination `%s' not found\n",
1849 pdf_drop_obj (needle
);
1853 unlock ("reqlayout");
1854 printd ("continue %d", state
.pagecount
);
1856 else if (!strncmp ("page", p
, 4)) {
1859 int pageno
, pindex
, ret
;
1861 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1863 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1868 page
= loadpage (pageno
, pindex
);
1872 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1874 else if (!strncmp ("tile", p
, 4)) {
1875 int x
, y
, w
, h
, ret
;
1881 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1882 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1884 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1889 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1893 printd ("tile %d %d %" FMT_ptr
" %u %f",
1895 FMT_ptr_cast2 (tile
),
1896 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1899 else if (!strncmp ("settrim", p
, 7)) {
1904 ret
= sscanf (p
+ 7, " %d %d %d %d %d %f",
1905 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
,
1908 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1912 state
.trimmargins
= trimmargins
;
1913 state
.needoutline
= 1;
1914 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1916 state
.trimfuzz
= fuzz
;
1918 state
.pagedimcount
= 0;
1919 free (state
.pagedims
);
1920 state
.pagedims
= NULL
;
1925 printd ("continue %d", state
.pagecount
);
1927 else if (!strncmp ("sliceh", p
, 6)) {
1930 ret
= sscanf (p
+ 6, " %d", &h
);
1932 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1934 if (h
!= state
.sliceheight
) {
1937 state
.sliceheight
= h
;
1938 for (i
= 0; i
< state
.texcount
; ++i
) {
1939 state
.texowners
[i
].w
= -1;
1940 state
.texowners
[i
].h
= -1;
1941 state
.texowners
[i
].slice
= NULL
;
1945 else if (!strncmp ("interrupt", p
, 9)) {
1946 printd ("vmsg interrupted");
1949 errx (1, "unknown command %.*s", len
, p
);
1955 CAMLprim value
ml_realloctexts (value texcount_v
)
1957 CAMLparam1 (texcount_v
);
1960 if (trylock ("ml_realloctexts")) {
1964 realloctexts (Int_val (texcount_v
));
1966 unlock ("ml_realloctexts");
1969 CAMLreturn (Val_bool (ok
));
1972 static void showsel (struct page
*page
, int ox
, int oy
)
1978 fz_text_block
*block
;
1979 struct mark first
, last
;
1981 first
= page
->fmark
;
1984 if (!first
.span
|| !last
.span
) return;
1986 glEnable (GL_BLEND
);
1987 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1988 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1990 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1991 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1992 for (block
= page
->text
->blocks
;
1993 block
< page
->text
->blocks
+ page
->text
->len
;
1995 for (line
= block
->lines
;
1996 line
< block
->lines
+ block
->len
;
1999 rect
= fz_empty_rect
;
2000 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
2002 fz_text_span
*span
= line
->spans
[spannum
];
2004 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2009 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2011 j
= MIN (first
.i
, last
.i
);
2012 k
= MAX (first
.i
, last
.i
);
2014 else if (span
== first
.span
) {
2018 else if (span
== last
.span
) {
2024 for (i
= j
; i
<= k
; ++i
) {
2026 fz_union_rect (&rect
,
2027 fz_text_char_bbox (&bbox
, span
, i
));
2029 fz_round_rect (&bbox
, &rect
);
2030 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2037 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2038 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2039 if (span
== last
.span
) {
2047 glDisable (GL_BLEND
);
2052 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2054 fz_matrix ctm
, tm
, pm
;
2055 fz_link
*link
, *links
;
2057 switch (page
->type
) {
2059 links
= page
->u
.pdfpage
->links
;
2063 links
= page
->u
.xpspage
->links
;
2070 glEnable (GL_TEXTURE_1D
);
2071 glEnable (GL_BLEND
);
2072 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2074 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2075 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2076 fz_translate (&tm
, xoff
, yoff
);
2077 pm
= pagectm (page
);
2078 fz_concat (&ctm
, &pm
, &tm
);
2081 for (link
= links
; link
; link
= link
->next
) {
2082 fz_point p1
, p2
, p3
, p4
;
2084 p1
.x
= link
->rect
.x0
;
2085 p1
.y
= link
->rect
.y0
;
2087 p2
.x
= link
->rect
.x1
;
2088 p2
.y
= link
->rect
.y0
;
2090 p3
.x
= link
->rect
.x1
;
2091 p3
.y
= link
->rect
.y1
;
2093 p4
.x
= link
->rect
.x0
;
2094 p4
.y
= link
->rect
.y1
;
2096 fz_transform_point (&p1
, &ctm
);
2097 fz_transform_point (&p2
, &ctm
);
2098 fz_transform_point (&p3
, &ctm
);
2099 fz_transform_point (&p4
, &ctm
);
2101 switch (link
->dest
.kind
) {
2102 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2103 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2104 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2105 default: glColor3ub (0, 0, 0); break;
2113 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2117 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2120 glVertex2f (p1
.x
, p1
.y
);
2122 glVertex2f (p2
.x
, p2
.y
);
2125 glVertex2f (p2
.x
, p2
.y
);
2127 glVertex2f (p3
.x
, p3
.y
);
2130 glVertex2f (p3
.x
, p3
.y
);
2132 glVertex2f (p4
.x
, p4
.y
);
2135 glVertex2f (p4
.x
, p4
.y
);
2137 glVertex2f (p1
.x
, p1
.y
);
2142 glDisable (GL_BLEND
);
2143 glDisable (GL_TEXTURE_1D
);
2146 static int compareslinks (const void *l
, const void *r
)
2148 struct slink
const *ls
= l
;
2149 struct slink
const *rs
= r
;
2150 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2151 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2153 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2156 static void droptext (struct page
*page
)
2159 fz_free_text_page (state
.ctx
, page
->text
);
2162 page
->fmark
.span
= NULL
;
2163 page
->lmark
.span
= NULL
;
2167 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2171 static void dropslinks (struct page
*page
)
2174 free (page
->slinks
);
2175 page
->slinks
= NULL
;
2176 page
->slinkcount
= 0;
2180 static void ensureslinks (struct page
*page
)
2184 size_t slinksize
= sizeof (*page
->slinks
);
2185 fz_link
*link
, *links
;
2187 if (state
.gen
!= page
->sgen
) {
2189 page
->sgen
= state
.gen
;
2191 if (page
->slinks
) return;
2193 switch (page
->type
) {
2195 links
= page
->u
.pdfpage
->links
;
2196 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2198 &state
.pagedims
[page
->pdimno
].tctm
,
2199 &state
.pagedims
[page
->pdimno
].ctm
);
2203 links
= page
->u
.xpspage
->links
;
2204 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2211 for (link
= links
; link
; link
= link
->next
) {
2215 page
->slinkcount
= count
;
2216 page
->slinks
= calloc (count
, slinksize
);
2217 if (!page
->slinks
) {
2218 err (1, "realloc slinks %d", count
);
2221 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2225 fz_transform_rect (&rect
, &ctm
);
2226 page
->slinks
[i
].link
= link
;
2227 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2229 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2233 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2234 static void fmt_linkn (char *s
, unsigned int u
)
2236 unsigned int len
; unsigned int q
;
2237 int zma
= 'z' - 'a' + 1;
2239 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2242 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2243 /* handles u == 0 */
2248 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2249 int noff
, char *targ
, int tlen
, int hfsize
)
2253 struct slink
*slink
;
2254 double x0
, y0
, x1
, y1
, w
;
2256 ensureslinks (page
);
2257 glColor3ub (0xc3, 0xb0, 0x91);
2258 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2259 fmt_linkn (buf
, i
+ noff
);
2260 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2261 slink
= &page
->slinks
[i
];
2263 x0
= slink
->bbox
.x0
+ xoff
- 5;
2264 y1
= slink
->bbox
.y0
+ yoff
- 5;
2265 y0
= y1
+ 10 + hfsize
;
2266 w
= measure_string (state
.face
, hfsize
, buf
);
2268 glRectd (x0
, y0
, x1
, y1
);
2272 glEnable (GL_BLEND
);
2273 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2274 glEnable (GL_TEXTURE_2D
);
2275 glColor3ub (0, 0, 0);
2276 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2277 fmt_linkn (buf
, i
+ noff
);
2278 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2279 slink
= &page
->slinks
[i
];
2281 x0
= slink
->bbox
.x0
+ xoff
;
2282 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2283 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2286 glDisable (GL_TEXTURE_2D
);
2287 glDisable (GL_BLEND
);
2291 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2294 struct slice
*slice1
;
2295 unsigned char *texdata
;
2298 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2299 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2301 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2302 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2303 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2307 int texindex
= state
.texindex
++ % state
.texcount
;
2309 if (state
.texowners
[texindex
].w
== tile
->w
) {
2310 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2314 state
.texowners
[texindex
].h
= slice
->h
;
2318 state
.texowners
[texindex
].h
= slice
->h
;
2321 state
.texowners
[texindex
].w
= tile
->w
;
2322 state
.texowners
[texindex
].slice
= slice
;
2323 slice
->texindex
= texindex
;
2325 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2327 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2331 texdata
= tile
->pixmap
->samples
;
2334 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2346 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2358 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2363 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2365 CAMLparam2 (args_v
, ptr_v
);
2366 int dispx
= Int_val (Field (args_v
, 0));
2367 int dispy
= Int_val (Field (args_v
, 1));
2368 int dispw
= Int_val (Field (args_v
, 2));
2369 int disph
= Int_val (Field (args_v
, 3));
2370 int tilex
= Int_val (Field (args_v
, 4));
2371 int tiley
= Int_val (Field (args_v
, 5));
2372 char *s
= String_val (ptr_v
);
2373 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2375 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2377 int slicey
, firstslice
;
2378 struct slice
*slice
;
2380 firstslice
= tiley
/ tile
->sliceheight
;
2381 slice
= &tile
->slices
[firstslice
];
2382 slicey
= tiley
% tile
->sliceheight
;
2387 dh
= slice
->h
- slicey
;
2388 dh
= MIN (disph
, dh
);
2389 uploadslice (tile
, slice
);
2393 glTexCoord2i (tilex
, slicey
);
2394 glVertex2i (dispx
, dispy
);
2396 glTexCoord2i (tilex
+dispw
, slicey
);
2397 glVertex2i (dispx
+dispw
, dispy
);
2399 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2400 glVertex2i (dispx
+dispw
, dispy
+dh
);
2402 glTexCoord2i (tilex
, slicey
+dh
);
2403 glVertex2i (dispx
, dispy
+dh
);
2410 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2414 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2415 CAMLreturn (Val_unit
);
2418 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2419 value xoff_v
, value yoff_v
,
2422 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2423 int xoff
= Int_val (xoff_v
);
2424 int yoff
= Int_val (yoff_v
);
2425 int noff
= Int_val (Field (li_v
, 0));
2426 char *targ
= String_val (Field (li_v
, 1));
2427 int tlen
= caml_string_length (Field (li_v
, 1));
2428 int hfsize
= Int_val (Field (li_v
, 2));
2429 char *s
= String_val (ptr_v
);
2430 int hlmask
= Int_val (hlinks_v
);
2431 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2434 /* deal with loadpage failed pages */
2438 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2439 if (trylock ("ml_postprocess")) {
2444 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2445 noff
= page
->slinkcount
;
2447 showsel (page
, xoff
, yoff
);
2448 unlock ("ml_postprocess");
2451 CAMLreturn (Val_int (noff
));
2454 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2458 const fz_matrix
*tctm
;
2459 fz_link
*link
, *links
;
2461 switch (page
->type
) {
2463 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2464 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2465 links
= page
->u
.pdfpage
->links
;
2469 tctm
= &fz_identity
;
2470 links
= page
->u
.xpspage
->links
;
2479 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2480 fz_invert_matrix (&ctm
, &ctm
);
2481 fz_transform_point (&p
, &ctm
);
2483 for (link
= links
; link
; link
= link
->next
) {
2484 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2485 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2493 static void ensuretext (struct page
*page
)
2495 if (state
.gen
!= page
->tgen
) {
2497 page
->tgen
= state
.gen
;
2503 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2504 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2505 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2506 ctm
= pagectm (page
);
2507 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2508 qsort (page
->text
->blocks
, page
->text
->len
,
2509 sizeof (*page
->text
->blocks
), compareblocks
);
2510 fz_free_device (tdev
);
2514 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2516 CAMLparam2 (start_page_v
, dir_v
);
2518 int i
, dir
= Int_val (dir_v
);
2519 int start_page
= Int_val (start_page_v
);
2520 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2522 ret_v
= Val_int (0);
2523 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2527 lock ("ml_findpage_with_links");
2528 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2531 switch (state
.type
) {
2534 pdf_page
*page
= NULL
;
2536 fz_try (state
.ctx
) {
2537 page
= pdf_load_page (state
.u
.pdf
, i
);
2538 found
= !!page
->links
;
2540 fz_catch (state
.ctx
) {
2550 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2551 found
= !!page
->links
;
2557 ARSERT ("invalid document type");
2561 ret_v
= caml_alloc_small (1, 1);
2562 Field (ret_v
, 0) = Val_int (i
);
2567 unlock ("ml_findpage_with_links");
2573 enum { dir_first
, dir_last
};
2574 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2576 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2578 CAMLparam2 (ptr_v
, dir_v
);
2579 CAMLlocal2 (ret_v
, pos_v
);
2581 int dirtag
, i
, slinkindex
;
2582 struct slink
*found
= NULL
,*slink
;
2583 char *s
= String_val (ptr_v
);
2585 page
= parse_pointer ("ml_findlink", s
);
2586 ret_v
= Val_int (0);
2587 if (trylock ("ml_findlink")) {
2591 ensureslinks (page
);
2593 if (Is_block (dir_v
)) {
2594 dirtag
= Tag_val (dir_v
);
2596 case dir_first_visible
:
2598 int x0
, y0
, dir
, first_index
, last_index
;
2600 pos_v
= Field (dir_v
, 0);
2601 x0
= Int_val (Field (pos_v
, 0));
2602 y0
= Int_val (Field (pos_v
, 1));
2603 dir
= Int_val (Field (pos_v
, 2));
2608 last_index
= page
->slinkcount
;
2611 first_index
= page
->slinkcount
- 1;
2615 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2616 slink
= &page
->slinks
[i
];
2617 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2626 slinkindex
= Int_val (Field (dir_v
, 0));
2627 found
= &page
->slinks
[slinkindex
];
2628 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2629 slink
= &page
->slinks
[i
];
2630 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2638 slinkindex
= Int_val (Field (dir_v
, 0));
2639 found
= &page
->slinks
[slinkindex
];
2640 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2641 slink
= &page
->slinks
[i
];
2642 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2650 slinkindex
= Int_val (Field (dir_v
, 0));
2651 found
= &page
->slinks
[slinkindex
];
2652 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2653 slink
= &page
->slinks
[i
];
2654 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2662 slinkindex
= Int_val (Field (dir_v
, 0));
2663 found
= &page
->slinks
[slinkindex
];
2664 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2665 slink
= &page
->slinks
[i
];
2666 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2675 dirtag
= Int_val (dir_v
);
2678 found
= page
->slinks
;
2683 found
= page
->slinks
+ (page
->slinkcount
- 1);
2689 ret_v
= caml_alloc_small (2, 1);
2690 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2693 unlock ("ml_findlink");
2698 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2704 switch (link->dest.kind) { \
2705 case FZ_LINK_GOTO: \
2709 pageno = link->dest.ld.gotor.page; \
2713 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2714 p.y = link->dest.ld.gotor.lt.y; \
2715 fz_transform_point (&p, &pdim->lctm); \
2717 tup_v = caml_alloc_tuple (2); \
2718 ret_v = caml_alloc_small (1, ugoto); \
2719 Field (tup_v, 0) = Val_int (pageno); \
2720 Field (tup_v, 1) = Val_int (p.y); \
2721 Field (ret_v, 0) = tup_v; \
2726 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2727 ret_v = caml_alloc_small (1, uuri); \
2728 Field (ret_v, 0) = str_v; \
2731 case FZ_LINK_LAUNCH: \
2732 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2733 ret_v = caml_alloc_small (1, ulaunch); \
2734 Field (ret_v, 0) = str_v; \
2737 case FZ_LINK_NAMED: \
2738 str_v = caml_copy_string (link->dest.ld.named.named); \
2739 ret_v = caml_alloc_small (1, unamed); \
2740 Field (ret_v, 0) = str_v; \
2743 case FZ_LINK_GOTOR: \
2744 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2745 pageno = link->dest.ld.gotor.page; \
2746 tup_v = caml_alloc_tuple (2); \
2747 ret_v = caml_alloc_small (1, uremote); \
2748 Field (tup_v, 0) = str_v; \
2749 Field (tup_v, 1) = Val_int (pageno); \
2750 Field (ret_v, 0) = tup_v; \
2757 snprintf (buf, sizeof (buf), \
2758 "unhandled link kind %d", link->dest.kind); \
2759 str_v = caml_copy_string (buf); \
2760 ret_v = caml_alloc_small (1, uunexpected); \
2761 Field (ret_v, 0) = str_v; \
2767 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2769 CAMLparam2 (ptr_v
, n_v
);
2770 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2773 struct pagedim
*pdim
;
2774 char *s
= String_val (ptr_v
);
2776 ret_v
= Val_int (0);
2777 if (trylock ("ml_getlink")) {
2781 page
= parse_pointer ("ml_getlink", s
);
2782 ensureslinks (page
);
2783 pdim
= &state
.pagedims
[page
->pdimno
];
2784 link
= page
->slinks
[Int_val (n_v
)].link
;
2787 unlock ("ml_getlink");
2792 CAMLprim value
ml_getlinkcount (value ptr_v
)
2796 char *s
= String_val (ptr_v
);
2798 page
= parse_pointer ("ml_getlinkcount", s
);
2799 CAMLreturn (Val_int (page
->slinkcount
));
2802 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2804 CAMLparam2 (ptr_v
, n_v
);
2807 struct slink
*slink
;
2808 char *s
= String_val (ptr_v
);
2810 page
= parse_pointer ("ml_getlinkrect", s
);
2811 ret_v
= caml_alloc_tuple (4);
2812 if (trylock ("ml_getlinkrect")) {
2813 Field (ret_v
, 0) = Val_int (0);
2814 Field (ret_v
, 1) = Val_int (0);
2815 Field (ret_v
, 2) = Val_int (0);
2816 Field (ret_v
, 3) = Val_int (0);
2819 ensureslinks (page
);
2821 slink
= &page
->slinks
[Int_val (n_v
)];
2822 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2823 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2824 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2825 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2826 unlock ("ml_getlinkrect");
2832 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2834 CAMLparam3 (ptr_v
, x_v
, y_v
);
2835 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2838 char *s
= String_val (ptr_v
);
2839 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2840 struct pagedim
*pdim
;
2842 ret_v
= Val_int (0);
2843 if (trylock ("ml_whatsunder")) {
2847 page
= parse_pointer ("ml_whatsunder", s
);
2848 pdim
= &state
.pagedims
[page
->pdimno
];
2849 x
+= pdim
->bounds
.x0
;
2850 y
+= pdim
->bounds
.y0
;
2851 link
= getlink (page
, x
, y
);
2857 fz_text_block
*block
;
2860 for (block
= page
->text
->blocks
;
2861 block
< page
->text
->blocks
+ page
->text
->len
;
2866 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2869 for (line
= block
->lines
;
2870 line
< block
->lines
+ block
->len
;
2875 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2878 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
2880 fz_text_span
*span
= line
->spans
[spannum
];
2883 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2886 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
2888 fz_text_char_bbox (&bbox
, span
, charnum
);
2891 if (x
>= b
->x0
&& x
<= b
->x1
2892 && y
>= b
->y0
&& y
<= b
->y1
) {
2893 fz_text_style
*style
= span
->text
->style
;
2895 style
->font
&& style
->font
->name
2897 : "Span has no font name"
2899 FT_FaceRec
*face
= style
->font
->ft_face
;
2900 if (face
&& face
->family_name
) {
2902 char *n1
= face
->family_name
;
2903 size_t l1
= strlen (n1
);
2904 size_t l2
= strlen (n2
);
2906 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2907 s
= malloc (l1
+ l2
+ 2);
2911 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2912 str_v
= caml_copy_string (s
);
2918 str_v
= caml_copy_string (n2
);
2920 ret_v
= caml_alloc_small (1, utext
);
2921 Field (ret_v
, 0) = str_v
;
2930 unlock ("ml_whatsunder");
2936 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2938 CAMLparam2 (ptr_v
, rect_v
);
2941 struct pagedim
*pdim
;
2942 int i
, x0
, x1
, y0
, y1
;
2943 char *s
= String_val (ptr_v
);
2945 fz_text_block
*block
;
2946 fz_text_span
*span
, *fspan
, *lspan
;
2947 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2949 if (trylock ("ml_seltext")) {
2953 page
= parse_pointer ("ml_seltext", s
);
2956 pdim
= &state
.pagedims
[page
->pdimno
];
2957 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2958 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2959 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2960 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2963 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2964 glColor3ub (128, 128, 128);
2965 glRecti (x0
, y0
, x1
, y1
);
2966 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2969 fspan
= lspan
= NULL
;
2971 for (block
= page
->text
->blocks
;
2972 block
< page
->text
->blocks
+ page
->text
->len
;
2974 for (line
= block
->lines
;
2975 line
< block
->lines
+ block
->len
;
2979 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
2980 span
= line
->spans
[spannum
];
2982 for (i
= 0; i
< span
->len
; ++i
) {
2985 fz_text_char_bbox (&b
, span
, i
);
2987 if (x0
>= b
.x0
&& x0
<= b
.x1
2988 && y0
>= b
.y0
&& y0
<= b
.y1
) {
2994 if (x1
>= b
.x0
&& x1
<= b
.x1
2995 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3001 if (0 && selected
) {
3002 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3003 glColor3ub (128, 128, 128);
3004 glRecti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3005 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3011 if (y1
< y0
|| x1
< x0
) {
3014 if (fspan
== lspan
) {
3019 if (fline
!= lline
) {
3038 page
->fmark
.span
= fspan
;
3041 page
->lmark
.span
= lspan
;
3043 unlock ("ml_seltext");
3046 CAMLreturn (Val_unit
);
3049 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3054 for (i
= a
; i
<= b
; ++i
) {
3055 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3056 ret
= fwrite (buf
, len
, 1, f
);
3059 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3060 len
, ret
, strerror (errno
));
3068 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3070 caml_failwith ("ml_popen not implemented under Cygwin");
3073 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3075 CAMLparam2 (command_v
, fds_v
);
3076 CAMLlocal2 (l_v
, tup_v
);
3079 value earg_v
= Nothing
;
3080 posix_spawnattr_t attr
;
3081 posix_spawn_file_actions_t fa
;
3082 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3084 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3085 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3088 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3089 msg
= "posix_spawnattr_init";
3093 #ifdef POSIX_SPAWN_USEVFORK
3094 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3095 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3100 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3103 tup_v
= Field (l_v
, 0);
3104 fd1
= Int_val (Field (tup_v
, 0));
3105 fd2
= Int_val (Field (tup_v
, 1));
3107 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3108 msg
= "posix_spawn_file_actions_addclose";
3114 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3115 msg
= "posix_spawn_file_actions_adddup2";
3122 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3123 msg
= "posix_spawn";
3128 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3129 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3133 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3134 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3139 unix_error (ret
, msg
, earg_v
);
3141 CAMLreturn (Val_unit
);
3145 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3152 fz_text_block
*block
;
3153 int fd
= Int_val (fd_v
);
3154 char *s
= String_val (ptr_v
);
3156 if (trylock ("ml_copysel")) {
3160 page
= parse_pointer ("ml_sopysel", s
);
3162 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3163 fprintf (stderr
, "nothing to copy\n");
3167 f
= fdopen (fd
, "w");
3169 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3174 for (block
= page
->text
->blocks
;
3175 block
< page
->text
->blocks
+ page
->text
->len
;
3177 for (line
= block
->lines
;
3178 line
< block
->lines
+ block
->len
;
3182 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
3184 fz_text_span
*span
= line
->spans
[spannum
];
3186 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3187 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3188 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3191 if (pipespan (f
, span
, a
, b
)) {
3194 if (spannum
== line
->len
- 1) {
3195 if (putc ('\n', f
) == EOF
) {
3197 "failed break line on sel pipe: %s\n",
3202 if (span
== page
->lmark
.span
) {
3210 page
->lmark
.span
= NULL
;
3211 page
->fmark
.span
= NULL
;
3215 int ret
= fclose (f
);
3218 if (errno
!= ECHILD
) {
3219 fprintf (stderr
, "failed to close sel pipe: %s\n",
3225 unlock ("ml_copysel");
3230 fprintf (stderr
, "failed to close sel pipe: %s\n",
3234 CAMLreturn (Val_unit
);
3237 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3239 CAMLparam1 (pagedimno_v
);
3241 int pagedimno
= Int_val (pagedimno_v
);
3244 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3245 if (trylock ("ml_getpdimrect")) {
3246 box
= fz_empty_rect
;
3249 box
= state
.pagedims
[pagedimno
].mediabox
;
3250 unlock ("ml_getpdimrect");
3253 Store_double_field (ret_v
, 0, box
.x0
);
3254 Store_double_field (ret_v
, 1, box
.x1
);
3255 Store_double_field (ret_v
, 2, box
.y0
);
3256 Store_double_field (ret_v
, 3, box
.y1
);
3261 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3262 value dw_v
, value cols_v
)
3264 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3270 double winw
= Int_val (winw_v
);
3271 double winh
= Int_val (winh_v
);
3272 double dw
= Int_val (dw_v
);
3273 double cols
= Int_val (cols_v
);
3274 double pw
= 1.0, ph
= 1.0;
3276 if (trylock ("ml_zoom_for_height")) {
3280 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3281 double w
= p
->pagebox
.x1
/ cols
;
3282 double h
= p
->pagebox
.y1
;
3286 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3288 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3291 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3292 unlock ("ml_zoom_for_height");
3294 ret_v
= caml_copy_double (zoom
);
3298 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3300 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3302 int pt
= Int_val(pt_v
);
3303 int x
= Int_val (x_v
);
3304 int y
= Int_val (y_v
);
3307 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3308 ret_v
= caml_copy_double (w
);
3312 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3314 CAMLparam2 (pt_v
, string_v
);
3316 int pt
= Int_val (pt_v
);
3319 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3320 ret_v
= caml_copy_double (w
);
3324 CAMLprim value
ml_getpagebox (value opaque_v
)
3326 CAMLparam1 (opaque_v
);
3332 char *s
= String_val (opaque_v
);
3333 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3335 ret_v
= caml_alloc_tuple (4);
3336 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3337 dev
->hints
|= FZ_IGNORE_SHADE
;
3339 switch (page
->type
) {
3341 ctm
= pagectm (page
);
3342 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3346 ctm
= pagectm (page
);
3347 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3351 rect
= fz_infinite_rect
;
3355 fz_free_device (dev
);
3356 fz_round_rect (&bbox
, &rect
);
3357 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3358 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3359 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3360 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3365 CAMLprim value
ml_setaalevel (value level_v
)
3367 CAMLparam1 (level_v
);
3369 state
.aalevel
= Int_val (level_v
);
3370 CAMLreturn (Val_unit
);
3374 #include <X11/Xlib.h>
3380 GLXDrawable drawable
;
3383 #include "keysym2ucs.c"
3385 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3387 CAMLparam1 (keysym_v
);
3389 KeySym keysym
= Int_val (keysym_v
);
3394 rune
= keysym2ucs (keysym
);
3395 len
= fz_runetochar (buf
, rune
);
3397 str_v
= caml_copy_string (buf
);
3401 CAMLprim value
ml_glx (value win_v
)
3404 XVisualInfo
*visual
;
3405 int screen
, wid
= Int_val (win_v
);
3406 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3408 glx
.dpy
= XOpenDisplay (NULL
);
3410 caml_failwith ("XOpenDisplay");
3413 screen
= DefaultScreen (glx
.dpy
);
3414 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3416 XCloseDisplay (glx
.dpy
);
3418 caml_failwith ("glXChooseVisual");
3421 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3424 XCloseDisplay (glx
.dpy
);
3426 caml_failwith ("glXCreateContext");
3429 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3430 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3431 XCloseDisplay (glx
.dpy
);
3434 caml_failwith ("glXMakeCurrent");
3437 CAMLreturn (Val_unit
);
3440 CAMLprim value
ml_swapb (value unit_v
)
3442 CAMLparam1 (unit_v
);
3443 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3444 CAMLreturn (Val_unit
);
3447 CAMLprim value
ml_glxsync (value unit_v
)
3449 CAMLparam1 (unit_v
);
3450 if (glx
.dpy
&& glx
.ctx
) {
3454 CAMLreturn (Val_unit
);
3457 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3458 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3460 CAMLprim value
ml_platform (value unit_v
)
3462 CAMLparam1 (unit_v
);
3463 int platid
= piunknown
;
3465 #if defined __linux__
3467 #elif defined __CYGWIN__
3469 #elif defined __DragonFly__
3470 platid
= pidragonflybsd
;
3471 #elif defined __FreeBSD__
3473 #elif defined __OpenBSD__
3475 #elif defined __NetBSD__
3477 #elif defined __sun__
3479 #elif defined __APPLE__
3482 CAMLreturn (Val_int (platid
));
3485 CAMLprim value
ml_cloexec (value fd_v
)
3488 int fd
= Int_val (fd_v
);
3490 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3491 uerror ("fcntl", Nothing
);
3493 CAMLreturn (Val_unit
);
3496 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3498 CAMLparam2 (w_v
, h_v
);
3501 int w
= Int_val (w_v
);
3502 int h
= Int_val (h_v
);
3503 int cs
= Int_val (cs_v
);
3505 if (state
.pbo_usable
) {
3506 pbo
= calloc (sizeof (*pbo
), 1);
3508 err (1, "calloc pbo");
3520 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3523 state
.glGenBuffersARB (1, &pbo
->id
);
3524 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3525 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3526 NULL
, GL_STREAM_DRAW
);
3527 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3529 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3531 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3532 state
.glDeleteBuffersARB (1, &pbo
->id
);
3534 ret_v
= caml_copy_string ("0");
3540 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3542 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3546 err (1, "malloc %d bytes failed", res
+1);
3548 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3550 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3552 ret_v
= caml_copy_string (s
);
3557 ret_v
= caml_copy_string ("0");
3562 CAMLprim value
ml_freepbo (value s_v
)
3565 char *s
= String_val (s_v
);
3566 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3569 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3571 tile
->pbo
->ptr
= NULL
;
3572 tile
->pbo
->size
= -1;
3574 CAMLreturn (Val_unit
);
3577 CAMLprim value
ml_unmappbo (value s_v
)
3580 char *s
= String_val (s_v
);
3581 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3584 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3585 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3586 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3588 tile
->pbo
->ptr
= NULL
;
3589 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3591 CAMLreturn (Val_unit
);
3594 static void setuppbo (void)
3596 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3597 GGPA (glBindBufferARB
);
3598 if (state
.glBindBufferARB
) {
3599 GGPA (glUnmapBufferARB
);
3600 if (state
.glUnmapBufferARB
) {
3601 GGPA (glMapBufferARB
);
3602 if (state
.glMapBufferARB
) {
3603 GGPA (glBufferDataARB
);
3604 if (state
.glBufferDataARB
) {
3605 GGPA (glGenBuffersARB
);
3606 if (state
.glGenBuffersARB
) {
3607 GGPA (glDeleteBuffersARB
);
3608 if (state
.glDeleteBuffersARB
) {
3609 state
.pbo_usable
= 1;
3619 CAMLprim value
ml_pbo_usable (value unit_v
)
3621 CAMLparam1 (unit_v
);
3622 CAMLreturn (Val_bool (state
.pbo_usable
));
3625 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3627 CAMLparam3 (ptr_v
, x_v
, y_v
);
3628 CAMLlocal2 (ret_v
, tup_v
);
3630 char *s
= String_val (ptr_v
);
3631 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3632 struct pagedim
*pdim
;
3636 page
= parse_pointer ("ml_unproject", s
);
3637 pdim
= &state
.pagedims
[page
->pdimno
];
3639 ret_v
= Val_int (0);
3640 if (trylock ("ml_unproject")) {
3644 switch (page
->type
) {
3646 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3655 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3656 fz_invert_matrix (&ctm
, &ctm
);
3657 fz_transform_point (&p
, &ctm
);
3659 tup_v
= caml_alloc_tuple (2);
3660 ret_v
= caml_alloc_small (1, 1);
3661 Field (tup_v
, 0) = Val_int (p
.x
);
3662 Field (tup_v
, 1) = Val_int (p
.y
);
3663 Field (ret_v
, 0) = tup_v
;
3665 unlock ("ml_unproject");
3670 static void makestippletex (void)
3672 const char *pixels
= "\xff\xff\0\0";
3673 glGenTextures (1, &state
.stid
);
3674 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3675 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3676 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3689 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3691 CAMLparam2 (pipe_v
, params_v
);
3692 CAMLlocal2 (trim_v
, fuzz_v
);
3699 struct sigaction sa
;
3701 state
.cr
= Int_val (Field (pipe_v
, 0));
3702 state
.cw
= Int_val (Field (pipe_v
, 1));
3703 state
.rotate
= Int_val (Field (params_v
, 0));
3704 state
.fitmodel
= Int_val (Field (params_v
, 1));
3705 trim_v
= Field (params_v
, 2);
3706 texcount
= Int_val (Field (params_v
, 3));
3707 state
.sliceheight
= Int_val (Field (params_v
, 4));
3708 mustoresize
= Int_val (Field (params_v
, 5));
3709 colorspace
= Int_val (Field (params_v
, 6));
3710 fontpath
= String_val (Field (params_v
, 7));
3711 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3712 if (!state
.trimcachepath
) {
3713 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3716 haspboext
= Bool_val (Field (params_v
, 9));
3718 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3720 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3721 fuzz_v
= Field (trim_v
, 1);
3722 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3723 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3724 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3725 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3727 set_tex_params (colorspace
);
3730 state
.face
= load_font (fontpath
);
3734 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3736 state
.face
= load_builtin_font (base
, len
);
3738 if (!state
.face
) _exit (1);
3740 realloctexts (texcount
);
3748 sa
.sa_handler
= SIG_IGN
;
3749 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3751 sa
.sa_handler
= SIG_DFL
;
3752 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3754 if (sigemptyset (&sa
.sa_mask
)) {
3755 err (1, "sigemptyset");
3757 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3758 err (1, "sigaction");
3761 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3763 errx (1, "pthread_create: %s", strerror (ret
));
3766 CAMLreturn (Val_unit
);