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 (void)
1117 struct pagedim
*p
= p
;
1118 double zw
, w
, maxw
= 0.0;
1120 if (state
.pagedimcount
== 0) return;
1122 switch (state
.fitmodel
) {
1123 case FitProportional
:
1124 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1127 p
= &state
.pagedims
[pindex
];
1128 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1130 fz_transform_rect (&box
, &rm
);
1132 x0
= MIN (box
.x0
, box
.x1
);
1133 x1
= MAX (box
.x0
, box
.x1
);
1136 maxw
= MAX (w
, maxw
);
1148 ARSERT (0 && state
.fitmodel
);
1151 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1156 p
= &state
.pagedims
[pindex
];
1157 fz_rotate (&ctm
, state
.rotate
);
1158 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1160 fz_transform_rect (&box
, &rm
);
1161 w
= box
.x1
- box
.x0
;
1162 switch (state
.fitmodel
) {
1163 case FitProportional
:
1165 zoom
= zw
* (w
/ maxw
);
1166 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1172 h
= box
.y1
- box
.y0
;
1174 zoom
= MIN (zw
, zh
);
1175 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1184 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1185 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1187 fz_rotate (&rm
, p
->rotate
);
1188 p
->pagebox
= p
->mediabox
;
1189 fz_transform_rect (&p
->pagebox
, &rm
);
1190 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1191 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1195 fz_transform_rect (&rect
, &ctm
);
1196 fz_round_rect (&p
->bounds
, &rect
);
1199 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1200 fz_scale (&sm
, zoom
, -zoom
);
1201 fz_concat (&ctm
, &tm
, &sm
);
1202 fz_concat (&p
->lctm
, &ctm
, &rm
);
1208 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1209 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1210 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1211 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1215 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1216 } while (p
-- != state
.pagedims
);
1220 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1221 desttoanchor (fz_link_dest
*dest
)
1225 struct pagedim
*pdim
= state
.pagedims
;
1230 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1231 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1233 pdim
= &state
.pagedims
[i
];
1235 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1237 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1238 p
.x
= dest
->ld
.gotor
.lt
.x
;
1239 p
.y
= dest
->ld
.gotor
.lt
.y
;
1240 fz_transform_point (&p
, &pdim
->lctm
);
1244 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1245 double x0
, x1
, y0
, y1
;
1247 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1248 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1250 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1251 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1253 a
.n
= dest
->ld
.gotor
.page
;
1258 static void recurse_outline (fz_outline
*outline
, int level
)
1261 struct anchor a
= desttoanchor (&outline
->dest
);
1264 printd ("o %d %d %d %d %s",
1265 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1267 if (outline
->down
) {
1268 recurse_outline (outline
->down
, level
+ 1);
1270 outline
= outline
->next
;
1274 static void process_outline (void)
1276 fz_outline
*outline
;
1278 if (!state
.needoutline
) return;
1280 state
.needoutline
= 0;
1281 switch (state
.type
) {
1283 outline
= pdf_load_outline (state
.u
.pdf
);
1286 outline
= xps_load_outline (state
.u
.xps
);
1293 recurse_outline (outline
, 0);
1294 fz_free_outline (state
.ctx
, outline
);
1298 static char *strofspan (fz_text_span
*span
)
1303 size_t size
= 0, cap
= 80;
1305 p
= malloc (cap
+ 1);
1306 if (!p
) return NULL
;
1308 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1309 int n
= fz_runetochar (utf8
, ch
->c
);
1310 if (size
+ n
> cap
) {
1312 p
= realloc (p
, cap
+ 1);
1313 if (!p
) return NULL
;
1316 memcpy (p
+ size
, utf8
, n
);
1323 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1324 fz_text_span
*span
, fz_matrix ctm
,
1325 int stop
, int pageno
, double start
)
1332 fz_point p1
, p2
, p3
, p4
;
1334 p
= strofspan (span
);
1337 ret
= regexec (re
, p
, 1, &rm
, 0);
1340 if (ret
!= REG_NOMATCH
) {
1343 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1344 printd ("msg regexec error `%.*s'",
1345 (int) size
, errbuf
);
1353 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1354 c
+= fz_runelen (span
->text
[a
].c
);
1356 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1357 c
+= fz_runelen (span
->text
[b
].c
);
1360 if (fz_runelen (span
->text
[b
].c
) > 1) {
1364 fz_text_char_bbox (&sb
, span
, a
);
1365 fz_text_char_bbox (&eb
, span
, b
);
1377 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1384 printd ("progress 1 found at %d `%.*s' in %f sec",
1385 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1389 printd ("match %d %d %f %f %f %f %f %f %f %f",
1401 static int compareblocks (const void *l
, const void *r
)
1403 fz_text_block
const *ls
= l
;
1404 fz_text_block
const* rs
= r
;
1405 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1408 /* wishful thinking function */
1409 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1414 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1416 fz_text_sheet
*sheet
;
1417 struct pagedim
*pdim
, *pdimprev
;
1418 int stop
= 0, niters
= 0;
1421 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1425 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1426 if (niters
++ == 5) {
1429 printd ("progress 1 attention requested aborting search at %d",
1434 printd ("progress %f searching in page %d",
1435 (double) (pageno
+ 1) / state
.pagecount
,
1440 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1441 pdim
= &state
.pagedims
[i
];
1442 if (pdim
->pageno
== pageno
) {
1445 if (pdim
->pageno
> pageno
) {
1454 sheet
= fz_new_text_sheet (state
.ctx
);
1455 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1456 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1458 switch (state
.type
) {
1461 fz_try (state
.ctx
) {
1462 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1463 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1464 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1465 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1467 fz_catch (state
.ctx
) {
1468 fz_free_device (tdev
);
1475 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1476 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1480 ARSERT (0 && state
.type
);
1483 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1484 fz_free_device (tdev
);
1486 for (j
= 0; j
< text
->len
; ++j
) {
1488 fz_text_block
*block
;
1490 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1492 for (k
= 0; k
< block
->len
; ++k
) {
1497 line
= &block
->lines
[k
];
1498 if (line
->bbox
.y0
< y
+ 1) continue;
1501 line
= &block
->lines
[block
->len
- 1 - k
];
1502 if (line
->bbox
.y0
> y
- 1) continue;
1505 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
1506 fz_text_span
*span
= line
->spans
[spannum
];
1508 switch (matchspan (re
, text
, span
, ctm
,
1509 stop
, pageno
, start
)) {
1511 case 1: stop
= 1; break;
1512 case -1: stop
= 1; goto endloop
;
1527 fz_free_text_page (state
.ctx
, text
);
1528 fz_free_text_sheet (state
.ctx
, sheet
);
1530 state
.freepage (u
.ptr
);
1535 printd ("progress 1 no matches %f sec", end
- start
);
1537 printd ("clearrects");
1540 static void set_tex_params (int colorspace
)
1547 switch (colorspace
) {
1549 state
.texiform
= GL_RGBA8
;
1550 state
.texform
= GL_RGBA
;
1551 state
.texty
= GL_UNSIGNED_BYTE
;
1552 state
.colorspace
= fz_device_rgb
;
1555 state
.texiform
= GL_RGBA8
;
1556 state
.texform
= GL_BGRA
;
1557 state
.texty
= endianness
.s
> 1
1558 ? GL_UNSIGNED_INT_8_8_8_8
1559 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1560 state
.colorspace
= fz_device_bgr
;
1563 state
.texiform
= GL_LUMINANCE_ALPHA
;
1564 state
.texform
= GL_LUMINANCE_ALPHA
;
1565 state
.texty
= GL_UNSIGNED_BYTE
;
1566 state
.colorspace
= fz_device_gray
;
1569 errx (1, "invalid colorspce %d", colorspace
);
1573 static void realloctexts (int texcount
)
1577 if (texcount
== state
.texcount
) return;
1579 if (texcount
< state
.texcount
) {
1580 glDeleteTextures (state
.texcount
- texcount
,
1581 state
.texids
+ texcount
);
1584 size
= texcount
* sizeof (*state
.texids
);
1585 state
.texids
= realloc (state
.texids
, size
);
1586 if (!state
.texids
) {
1587 err (1, "realloc texids %" FMT_s
, size
);
1590 size
= texcount
* sizeof (*state
.texowners
);
1591 state
.texowners
= realloc (state
.texowners
, size
);
1592 if (!state
.texowners
) {
1593 err (1, "realloc texowners %" FMT_s
, size
);
1595 if (texcount
> state
.texcount
) {
1598 glGenTextures (texcount
- state
.texcount
,
1599 state
.texids
+ state
.texcount
);
1600 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1601 state
.texowners
[i
].w
= -1;
1602 state
.texowners
[i
].slice
= NULL
;
1605 state
.texcount
= texcount
;
1609 static char *mbtoutf8 (char *s
)
1615 len
= mbstowcs (NULL
, s
, strlen (s
));
1620 if (len
== (size_t) -1) {
1625 tmp
= malloc (len
* sizeof (wchar_t));
1630 ret
= mbstowcs (tmp
, s
, len
);
1631 if (ret
== (size_t) -1) {
1637 for (i
= 0; i
< ret
; ++i
) {
1638 len
+= fz_runelen (tmp
[i
]);
1641 p
= r
= malloc (len
+ 1);
1647 for (i
= 0; i
< ret
; ++i
) {
1648 p
+= fz_runetochar (p
, tmp
[i
]);
1655 CAMLprim value
ml_mbtoutf8 (value s_v
)
1661 s
= String_val (s_v
);
1667 ret_v
= caml_copy_string (r
);
1673 static void * mainloop (void *unused
)
1676 int len
, ret
, oldlen
= 0;
1681 errx (1, "readlen returned 0");
1684 if (oldlen
< len
+ 1) {
1685 p
= realloc (p
, len
+ 1);
1687 err (1, "realloc %d failed", len
+ 1);
1694 if (!strncmp ("open", p
, 4)) {
1701 ret
= sscanf (p
+ 5, " %d %n", &wthack
, &off
);
1703 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1706 filename
= p
+ 5 + off
;
1707 filenamelen
= strlen (filename
);
1708 password
= filename
+ filenamelen
+ 1;
1711 openxref (filename
, password
);
1717 utf8filename
= mbtoutf8 (filename
);
1718 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1719 if (utf8filename
!= filename
) {
1720 free (utf8filename
);
1723 state
.needoutline
= 1;
1725 else if (!strncmp ("cs", p
, 2)) {
1728 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1730 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1733 set_tex_params (colorspace
);
1734 for (i
= 0; i
< state
.texcount
; ++i
) {
1735 state
.texowners
[i
].w
= -1;
1736 state
.texowners
[i
].slice
= NULL
;
1740 else if (!strncmp ("freepage", p
, 8)) {
1743 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1745 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1749 else if (!strncmp ("freetile", p
, 8)) {
1752 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1754 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1758 else if (!strncmp ("search", p
, 6)) {
1759 int icase
, pageno
, y
, ret
, len2
, forward
;
1763 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1764 &icase
, &pageno
, &y
, &forward
, &len2
);
1766 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1769 pattern
= p
+ 6 + len2
;
1770 ret
= regcomp (&re
, pattern
,
1771 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1776 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1777 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1780 search (&re
, pageno
, y
, forward
);
1784 else if (!strncmp ("geometry", p
, 8)) {
1788 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1790 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1798 for (i
= 0; i
< state
.texcount
; ++i
) {
1799 state
.texowners
[i
].slice
= NULL
;
1802 state
.fitmodel
= fitmodel
;
1807 unlock ("geometry");
1808 printd ("continue %d", state
.pagecount
);
1810 else if (!strncmp ("reqlayout", p
, 9)) {
1812 int rotate
, fitmodel
, off
;
1815 ret
= sscanf (p
+ 9, " %d %d %n", &rotate
, &fitmodel
, &off
);
1817 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1820 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1823 state
.rotate
= rotate
;
1824 state
.fitmodel
= fitmodel
;
1828 nameddest
= p
+ 9 + off
;
1829 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1832 pdf_obj
*needle
, *obj
;
1834 needle
= pdf_new_string (state
.ctx
, nameddest
,
1835 strlen (nameddest
));
1836 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1838 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1840 a
= desttoanchor (&dest
);
1842 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1845 printd ("emsg failed to parse destination `%s'\n",
1850 printd ("emsg destination `%s' not found\n",
1853 pdf_drop_obj (needle
);
1857 unlock ("reqlayout");
1858 printd ("continue %d", state
.pagecount
);
1860 else if (!strncmp ("page", p
, 4)) {
1863 int pageno
, pindex
, ret
;
1865 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1867 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1872 page
= loadpage (pageno
, pindex
);
1876 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1878 else if (!strncmp ("tile", p
, 4)) {
1879 int x
, y
, w
, h
, ret
;
1885 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1886 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1888 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1893 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1897 printd ("tile %d %d %" FMT_ptr
" %u %f",
1899 FMT_ptr_cast2 (tile
),
1900 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1903 else if (!strncmp ("settrim", p
, 7)) {
1907 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1908 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1910 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1914 state
.trimmargins
= trimmargins
;
1915 state
.needoutline
= 1;
1916 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1918 state
.trimfuzz
= fuzz
;
1920 state
.pagedimcount
= 0;
1921 free (state
.pagedims
);
1922 state
.pagedims
= NULL
;
1927 printd ("continue %d", state
.pagecount
);
1929 else if (!strncmp ("sliceh", p
, 6)) {
1932 ret
= sscanf (p
+ 6, " %d", &h
);
1934 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1936 if (h
!= state
.sliceheight
) {
1939 state
.sliceheight
= h
;
1940 for (i
= 0; i
< state
.texcount
; ++i
) {
1941 state
.texowners
[i
].w
= -1;
1942 state
.texowners
[i
].h
= -1;
1943 state
.texowners
[i
].slice
= NULL
;
1947 else if (!strncmp ("interrupt", p
, 9)) {
1948 printd ("vmsg interrupted");
1951 errx (1, "unknown command %.*s", len
, p
);
1957 CAMLprim value
ml_realloctexts (value texcount_v
)
1959 CAMLparam1 (texcount_v
);
1962 if (trylock ("ml_realloctexts")) {
1966 realloctexts (Int_val (texcount_v
));
1968 unlock ("ml_realloctexts");
1971 CAMLreturn (Val_bool (ok
));
1974 static void showsel (struct page
*page
, int ox
, int oy
)
1980 fz_text_block
*block
;
1981 struct mark first
, last
;
1983 first
= page
->fmark
;
1986 if (!first
.span
|| !last
.span
) return;
1988 glEnable (GL_BLEND
);
1989 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1990 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1992 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1993 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1994 for (block
= page
->text
->blocks
;
1995 block
< page
->text
->blocks
+ page
->text
->len
;
1997 for (line
= block
->lines
;
1998 line
< block
->lines
+ block
->len
;
2001 rect
= fz_empty_rect
;
2002 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
2004 fz_text_span
*span
= line
->spans
[spannum
];
2006 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2011 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2013 j
= MIN (first
.i
, last
.i
);
2014 k
= MAX (first
.i
, last
.i
);
2016 else if (span
== first
.span
) {
2020 else if (span
== last
.span
) {
2026 for (i
= j
; i
<= k
; ++i
) {
2028 fz_union_rect (&rect
,
2029 fz_text_char_bbox (&bbox
, span
, i
));
2031 fz_round_rect (&bbox
, &rect
);
2032 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2039 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2040 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2041 if (span
== last
.span
) {
2049 glDisable (GL_BLEND
);
2054 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2056 fz_matrix ctm
, tm
, pm
;
2057 fz_link
*link
, *links
;
2059 switch (page
->type
) {
2061 links
= page
->u
.pdfpage
->links
;
2065 links
= page
->u
.xpspage
->links
;
2072 glEnable (GL_TEXTURE_1D
);
2073 glEnable (GL_BLEND
);
2074 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2076 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2077 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2078 fz_translate (&tm
, xoff
, yoff
);
2079 pm
= pagectm (page
);
2080 fz_concat (&ctm
, &pm
, &tm
);
2083 for (link
= links
; link
; link
= link
->next
) {
2084 fz_point p1
, p2
, p3
, p4
;
2086 p1
.x
= link
->rect
.x0
;
2087 p1
.y
= link
->rect
.y0
;
2089 p2
.x
= link
->rect
.x1
;
2090 p2
.y
= link
->rect
.y0
;
2092 p3
.x
= link
->rect
.x1
;
2093 p3
.y
= link
->rect
.y1
;
2095 p4
.x
= link
->rect
.x0
;
2096 p4
.y
= link
->rect
.y1
;
2098 fz_transform_point (&p1
, &ctm
);
2099 fz_transform_point (&p2
, &ctm
);
2100 fz_transform_point (&p3
, &ctm
);
2101 fz_transform_point (&p4
, &ctm
);
2103 switch (link
->dest
.kind
) {
2104 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2105 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2106 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2107 default: glColor3ub (0, 0, 0); break;
2115 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2119 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2122 glVertex2f (p1
.x
, p1
.y
);
2124 glVertex2f (p2
.x
, p2
.y
);
2127 glVertex2f (p2
.x
, p2
.y
);
2129 glVertex2f (p3
.x
, p3
.y
);
2132 glVertex2f (p3
.x
, p3
.y
);
2134 glVertex2f (p4
.x
, p4
.y
);
2137 glVertex2f (p4
.x
, p4
.y
);
2139 glVertex2f (p1
.x
, p1
.y
);
2144 glDisable (GL_BLEND
);
2145 glDisable (GL_TEXTURE_1D
);
2148 static int compareslinks (const void *l
, const void *r
)
2150 struct slink
const *ls
= l
;
2151 struct slink
const *rs
= r
;
2152 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2153 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2155 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2158 static void droptext (struct page
*page
)
2161 fz_free_text_page (state
.ctx
, page
->text
);
2164 page
->fmark
.span
= NULL
;
2165 page
->lmark
.span
= NULL
;
2169 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2173 static void dropslinks (struct page
*page
)
2176 free (page
->slinks
);
2177 page
->slinks
= NULL
;
2178 page
->slinkcount
= 0;
2182 static void ensureslinks (struct page
*page
)
2186 size_t slinksize
= sizeof (*page
->slinks
);
2187 fz_link
*link
, *links
;
2189 if (state
.gen
!= page
->sgen
) {
2191 page
->sgen
= state
.gen
;
2193 if (page
->slinks
) return;
2195 switch (page
->type
) {
2197 links
= page
->u
.pdfpage
->links
;
2198 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2200 &state
.pagedims
[page
->pdimno
].tctm
,
2201 &state
.pagedims
[page
->pdimno
].ctm
);
2205 links
= page
->u
.xpspage
->links
;
2206 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2213 for (link
= links
; link
; link
= link
->next
) {
2217 page
->slinkcount
= count
;
2218 page
->slinks
= calloc (count
, slinksize
);
2219 if (!page
->slinks
) {
2220 err (1, "realloc slinks %d", count
);
2223 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2227 fz_transform_rect (&rect
, &ctm
);
2228 page
->slinks
[i
].link
= link
;
2229 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2231 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2235 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2236 static void fmt_linkn (char *s
, unsigned int u
)
2238 unsigned int len
; unsigned int q
;
2239 int zma
= 'z' - 'a' + 1;
2241 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2244 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2245 /* handles u == 0 */
2250 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2251 int noff
, char *targ
, int tlen
, int hfsize
)
2255 struct slink
*slink
;
2256 double x0
, y0
, x1
, y1
, w
;
2258 ensureslinks (page
);
2259 glColor3ub (0xc3, 0xb0, 0x91);
2260 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2261 fmt_linkn (buf
, i
+ noff
);
2262 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2263 slink
= &page
->slinks
[i
];
2265 x0
= slink
->bbox
.x0
+ xoff
- 5;
2266 y1
= slink
->bbox
.y0
+ yoff
- 5;
2267 y0
= y1
+ 10 + hfsize
;
2268 w
= measure_string (state
.face
, hfsize
, buf
);
2270 glRectd (x0
, y0
, x1
, y1
);
2274 glEnable (GL_BLEND
);
2275 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2276 glEnable (GL_TEXTURE_2D
);
2277 glColor3ub (0, 0, 0);
2278 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2279 fmt_linkn (buf
, i
+ noff
);
2280 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2281 slink
= &page
->slinks
[i
];
2283 x0
= slink
->bbox
.x0
+ xoff
;
2284 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2285 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2288 glDisable (GL_TEXTURE_2D
);
2289 glDisable (GL_BLEND
);
2293 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2296 struct slice
*slice1
;
2297 unsigned char *texdata
;
2300 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2301 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2303 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2304 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2305 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2309 int texindex
= state
.texindex
++ % state
.texcount
;
2311 if (state
.texowners
[texindex
].w
== tile
->w
) {
2312 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2316 state
.texowners
[texindex
].h
= slice
->h
;
2320 state
.texowners
[texindex
].h
= slice
->h
;
2323 state
.texowners
[texindex
].w
= tile
->w
;
2324 state
.texowners
[texindex
].slice
= slice
;
2325 slice
->texindex
= texindex
;
2327 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2329 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2333 texdata
= tile
->pixmap
->samples
;
2336 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2348 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2360 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2365 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2367 CAMLparam2 (args_v
, ptr_v
);
2368 int dispx
= Int_val (Field (args_v
, 0));
2369 int dispy
= Int_val (Field (args_v
, 1));
2370 int dispw
= Int_val (Field (args_v
, 2));
2371 int disph
= Int_val (Field (args_v
, 3));
2372 int tilex
= Int_val (Field (args_v
, 4));
2373 int tiley
= Int_val (Field (args_v
, 5));
2374 char *s
= String_val (ptr_v
);
2375 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2377 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2379 int slicey
, firstslice
;
2380 struct slice
*slice
;
2382 firstslice
= tiley
/ tile
->sliceheight
;
2383 slice
= &tile
->slices
[firstslice
];
2384 slicey
= tiley
% tile
->sliceheight
;
2389 dh
= slice
->h
- slicey
;
2390 dh
= MIN (disph
, dh
);
2391 uploadslice (tile
, slice
);
2395 glTexCoord2i (tilex
, slicey
);
2396 glVertex2i (dispx
, dispy
);
2398 glTexCoord2i (tilex
+dispw
, slicey
);
2399 glVertex2i (dispx
+dispw
, dispy
);
2401 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2402 glVertex2i (dispx
+dispw
, dispy
+dh
);
2404 glTexCoord2i (tilex
, slicey
+dh
);
2405 glVertex2i (dispx
, dispy
+dh
);
2412 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2416 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2417 CAMLreturn (Val_unit
);
2420 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2421 value xoff_v
, value yoff_v
,
2424 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2425 int xoff
= Int_val (xoff_v
);
2426 int yoff
= Int_val (yoff_v
);
2427 int noff
= Int_val (Field (li_v
, 0));
2428 char *targ
= String_val (Field (li_v
, 1));
2429 int tlen
= caml_string_length (Field (li_v
, 1));
2430 int hfsize
= Int_val (Field (li_v
, 2));
2431 char *s
= String_val (ptr_v
);
2432 int hlmask
= Int_val (hlinks_v
);
2433 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2436 /* deal with loadpage failed pages */
2440 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2441 if (trylock ("ml_postprocess")) {
2446 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2447 noff
= page
->slinkcount
;
2449 showsel (page
, xoff
, yoff
);
2450 unlock ("ml_postprocess");
2453 CAMLreturn (Val_int (noff
));
2456 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2460 const fz_matrix
*tctm
;
2461 fz_link
*link
, *links
;
2463 switch (page
->type
) {
2465 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2466 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2467 links
= page
->u
.pdfpage
->links
;
2471 tctm
= &fz_identity
;
2472 links
= page
->u
.xpspage
->links
;
2481 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2482 fz_invert_matrix (&ctm
, &ctm
);
2483 fz_transform_point (&p
, &ctm
);
2485 for (link
= links
; link
; link
= link
->next
) {
2486 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2487 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2495 static void ensuretext (struct page
*page
)
2497 if (state
.gen
!= page
->tgen
) {
2499 page
->tgen
= state
.gen
;
2505 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2506 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2507 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2508 ctm
= pagectm (page
);
2509 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2510 qsort (page
->text
->blocks
, page
->text
->len
,
2511 sizeof (*page
->text
->blocks
), compareblocks
);
2512 fz_free_device (tdev
);
2516 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2518 CAMLparam2 (start_page_v
, dir_v
);
2520 int i
, dir
= Int_val (dir_v
);
2521 int start_page
= Int_val (start_page_v
);
2522 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2524 ret_v
= Val_int (0);
2525 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2529 lock ("ml_findpage_with_links");
2530 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2533 switch (state
.type
) {
2536 pdf_page
*page
= NULL
;
2538 fz_try (state
.ctx
) {
2539 page
= pdf_load_page (state
.u
.pdf
, i
);
2540 found
= !!page
->links
;
2542 fz_catch (state
.ctx
) {
2552 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2553 found
= !!page
->links
;
2559 ARSERT ("invalid document type");
2563 ret_v
= caml_alloc_small (1, 1);
2564 Field (ret_v
, 0) = Val_int (i
);
2569 unlock ("ml_findpage_with_links");
2575 enum { dir_first
, dir_last
};
2576 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2578 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2580 CAMLparam2 (ptr_v
, dir_v
);
2581 CAMLlocal2 (ret_v
, pos_v
);
2583 int dirtag
, i
, slinkindex
;
2584 struct slink
*found
= NULL
,*slink
;
2585 char *s
= String_val (ptr_v
);
2587 page
= parse_pointer ("ml_findlink", s
);
2588 ret_v
= Val_int (0);
2589 if (trylock ("ml_findlink")) {
2593 ensureslinks (page
);
2595 if (Is_block (dir_v
)) {
2596 dirtag
= Tag_val (dir_v
);
2598 case dir_first_visible
:
2600 int x0
, y0
, dir
, first_index
, last_index
;
2602 pos_v
= Field (dir_v
, 0);
2603 x0
= Int_val (Field (pos_v
, 0));
2604 y0
= Int_val (Field (pos_v
, 1));
2605 dir
= Int_val (Field (pos_v
, 2));
2610 last_index
= page
->slinkcount
;
2613 first_index
= page
->slinkcount
- 1;
2617 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2618 slink
= &page
->slinks
[i
];
2619 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2628 slinkindex
= Int_val (Field (dir_v
, 0));
2629 found
= &page
->slinks
[slinkindex
];
2630 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2631 slink
= &page
->slinks
[i
];
2632 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2640 slinkindex
= Int_val (Field (dir_v
, 0));
2641 found
= &page
->slinks
[slinkindex
];
2642 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2643 slink
= &page
->slinks
[i
];
2644 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2652 slinkindex
= Int_val (Field (dir_v
, 0));
2653 found
= &page
->slinks
[slinkindex
];
2654 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2655 slink
= &page
->slinks
[i
];
2656 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2664 slinkindex
= Int_val (Field (dir_v
, 0));
2665 found
= &page
->slinks
[slinkindex
];
2666 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2667 slink
= &page
->slinks
[i
];
2668 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2677 dirtag
= Int_val (dir_v
);
2680 found
= page
->slinks
;
2685 found
= page
->slinks
+ (page
->slinkcount
- 1);
2691 ret_v
= caml_alloc_small (2, 1);
2692 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2695 unlock ("ml_findlink");
2700 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2706 switch (link->dest.kind) { \
2707 case FZ_LINK_GOTO: \
2711 pageno = link->dest.ld.gotor.page; \
2715 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2716 p.y = link->dest.ld.gotor.lt.y; \
2717 fz_transform_point (&p, &pdim->lctm); \
2719 tup_v = caml_alloc_tuple (2); \
2720 ret_v = caml_alloc_small (1, ugoto); \
2721 Field (tup_v, 0) = Val_int (pageno); \
2722 Field (tup_v, 1) = Val_int (p.y); \
2723 Field (ret_v, 0) = tup_v; \
2728 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2729 ret_v = caml_alloc_small (1, uuri); \
2730 Field (ret_v, 0) = str_v; \
2733 case FZ_LINK_LAUNCH: \
2734 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2735 ret_v = caml_alloc_small (1, ulaunch); \
2736 Field (ret_v, 0) = str_v; \
2739 case FZ_LINK_NAMED: \
2740 str_v = caml_copy_string (link->dest.ld.named.named); \
2741 ret_v = caml_alloc_small (1, unamed); \
2742 Field (ret_v, 0) = str_v; \
2745 case FZ_LINK_GOTOR: \
2746 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2747 pageno = link->dest.ld.gotor.page; \
2748 tup_v = caml_alloc_tuple (2); \
2749 ret_v = caml_alloc_small (1, uremote); \
2750 Field (tup_v, 0) = str_v; \
2751 Field (tup_v, 1) = Val_int (pageno); \
2752 Field (ret_v, 0) = tup_v; \
2759 snprintf (buf, sizeof (buf), \
2760 "unhandled link kind %d", link->dest.kind); \
2761 str_v = caml_copy_string (buf); \
2762 ret_v = caml_alloc_small (1, uunexpected); \
2763 Field (ret_v, 0) = str_v; \
2769 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2771 CAMLparam2 (ptr_v
, n_v
);
2772 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2775 struct pagedim
*pdim
;
2776 char *s
= String_val (ptr_v
);
2778 ret_v
= Val_int (0);
2779 if (trylock ("ml_getlink")) {
2783 page
= parse_pointer ("ml_getlink", s
);
2784 ensureslinks (page
);
2785 pdim
= &state
.pagedims
[page
->pdimno
];
2786 link
= page
->slinks
[Int_val (n_v
)].link
;
2789 unlock ("ml_getlink");
2794 CAMLprim value
ml_getlinkcount (value ptr_v
)
2798 char *s
= String_val (ptr_v
);
2800 page
= parse_pointer ("ml_getlinkcount", s
);
2801 CAMLreturn (Val_int (page
->slinkcount
));
2804 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2806 CAMLparam2 (ptr_v
, n_v
);
2809 struct slink
*slink
;
2810 char *s
= String_val (ptr_v
);
2812 page
= parse_pointer ("ml_getlinkrect", s
);
2813 ret_v
= caml_alloc_tuple (4);
2814 if (trylock ("ml_getlinkrect")) {
2815 Field (ret_v
, 0) = Val_int (0);
2816 Field (ret_v
, 1) = Val_int (0);
2817 Field (ret_v
, 2) = Val_int (0);
2818 Field (ret_v
, 3) = Val_int (0);
2821 ensureslinks (page
);
2823 slink
= &page
->slinks
[Int_val (n_v
)];
2824 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2825 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2826 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2827 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2828 unlock ("ml_getlinkrect");
2834 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2836 CAMLparam3 (ptr_v
, x_v
, y_v
);
2837 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2840 char *s
= String_val (ptr_v
);
2841 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2842 struct pagedim
*pdim
;
2844 ret_v
= Val_int (0);
2845 if (trylock ("ml_whatsunder")) {
2849 page
= parse_pointer ("ml_whatsunder", s
);
2850 pdim
= &state
.pagedims
[page
->pdimno
];
2851 x
+= pdim
->bounds
.x0
;
2852 y
+= pdim
->bounds
.y0
;
2853 link
= getlink (page
, x
, y
);
2859 fz_text_block
*block
;
2862 for (block
= page
->text
->blocks
;
2863 block
< page
->text
->blocks
+ page
->text
->len
;
2868 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2871 for (line
= block
->lines
;
2872 line
< block
->lines
+ block
->len
;
2877 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2880 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
2882 fz_text_span
*span
= line
->spans
[spannum
];
2885 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2888 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
2890 fz_text_char_bbox (&bbox
, span
, charnum
);
2893 if (x
>= b
->x0
&& x
<= b
->x1
2894 && y
>= b
->y0
&& y
<= b
->y1
) {
2895 fz_text_style
*style
= span
->text
->style
;
2897 style
->font
&& style
->font
->name
2899 : "Span has no font name"
2901 FT_FaceRec
*face
= style
->font
->ft_face
;
2902 if (face
&& face
->family_name
) {
2904 char *n1
= face
->family_name
;
2905 size_t l1
= strlen (n1
);
2906 size_t l2
= strlen (n2
);
2908 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2909 s
= malloc (l1
+ l2
+ 2);
2913 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2914 str_v
= caml_copy_string (s
);
2920 str_v
= caml_copy_string (n2
);
2922 ret_v
= caml_alloc_small (1, utext
);
2923 Field (ret_v
, 0) = str_v
;
2932 unlock ("ml_whatsunder");
2938 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2940 CAMLparam2 (ptr_v
, rect_v
);
2943 struct pagedim
*pdim
;
2944 int i
, x0
, x1
, y0
, y1
;
2945 char *s
= String_val (ptr_v
);
2947 fz_text_block
*block
;
2948 fz_text_span
*span
, *fspan
, *lspan
;
2949 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2951 if (trylock ("ml_seltext")) {
2955 page
= parse_pointer ("ml_seltext", s
);
2958 pdim
= &state
.pagedims
[page
->pdimno
];
2959 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2960 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2961 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2962 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2965 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2966 glColor3ub (128, 128, 128);
2967 glRecti (x0
, y0
, x1
, y1
);
2968 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2971 fspan
= lspan
= NULL
;
2973 for (block
= page
->text
->blocks
;
2974 block
< page
->text
->blocks
+ page
->text
->len
;
2976 for (line
= block
->lines
;
2977 line
< block
->lines
+ block
->len
;
2981 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
2982 span
= line
->spans
[spannum
];
2984 for (i
= 0; i
< span
->len
; ++i
) {
2987 fz_text_char_bbox (&b
, span
, i
);
2989 if (x0
>= b
.x0
&& x0
<= b
.x1
2990 && y0
>= b
.y0
&& y0
<= b
.y1
) {
2996 if (x1
>= b
.x0
&& x1
<= b
.x1
2997 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3003 if (0 && selected
) {
3004 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3005 glColor3ub (128, 128, 128);
3006 glRecti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3007 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3013 if (y1
< y0
|| x1
< x0
) {
3016 if (fspan
== lspan
) {
3021 if (fline
!= lline
) {
3040 page
->fmark
.span
= fspan
;
3043 page
->lmark
.span
= lspan
;
3045 unlock ("ml_seltext");
3048 CAMLreturn (Val_unit
);
3051 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3056 for (i
= a
; i
<= b
; ++i
) {
3057 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3058 ret
= fwrite (buf
, len
, 1, f
);
3061 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3062 len
, ret
, strerror (errno
));
3070 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3072 caml_failwith ("ml_popen not implemented under Cygwin");
3075 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3077 CAMLparam2 (command_v
, fds_v
);
3078 CAMLlocal2 (l_v
, tup_v
);
3081 value earg_v
= Nothing
;
3082 posix_spawnattr_t attr
;
3083 posix_spawn_file_actions_t fa
;
3084 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3086 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3087 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3090 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3091 msg
= "posix_spawnattr_init";
3095 #ifdef POSIX_SPAWN_USEVFORK
3096 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3097 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3102 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3105 tup_v
= Field (l_v
, 0);
3106 fd1
= Int_val (Field (tup_v
, 0));
3107 fd2
= Int_val (Field (tup_v
, 1));
3109 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3110 msg
= "posix_spawn_file_actions_addclose";
3116 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3117 msg
= "posix_spawn_file_actions_adddup2";
3124 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3125 msg
= "posix_spawn";
3130 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3131 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3135 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3136 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3141 unix_error (ret
, msg
, earg_v
);
3143 CAMLreturn (Val_unit
);
3147 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3154 fz_text_block
*block
;
3155 int fd
= Int_val (fd_v
);
3156 char *s
= String_val (ptr_v
);
3158 if (trylock ("ml_copysel")) {
3162 page
= parse_pointer ("ml_sopysel", s
);
3164 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3165 fprintf (stderr
, "nothing to copy\n");
3169 f
= fdopen (fd
, "w");
3171 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3176 for (block
= page
->text
->blocks
;
3177 block
< page
->text
->blocks
+ page
->text
->len
;
3179 for (line
= block
->lines
;
3180 line
< block
->lines
+ block
->len
;
3184 for (spannum
= 0; spannum
< line
->len
; ++spannum
) {
3186 fz_text_span
*span
= line
->spans
[spannum
];
3188 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3189 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3190 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3193 if (pipespan (f
, span
, a
, b
)) {
3196 if (spannum
== line
->len
- 1) {
3197 if (putc ('\n', f
) == EOF
) {
3199 "failed break line on sel pipe: %s\n",
3204 if (span
== page
->lmark
.span
) {
3212 page
->lmark
.span
= NULL
;
3213 page
->fmark
.span
= NULL
;
3217 int ret
= fclose (f
);
3220 if (errno
!= ECHILD
) {
3221 fprintf (stderr
, "failed to close sel pipe: %s\n",
3227 unlock ("ml_copysel");
3232 fprintf (stderr
, "failed to close sel pipe: %s\n",
3236 CAMLreturn (Val_unit
);
3239 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3241 CAMLparam1 (pagedimno_v
);
3243 int pagedimno
= Int_val (pagedimno_v
);
3246 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3247 if (trylock ("ml_getpdimrect")) {
3248 box
= fz_empty_rect
;
3251 box
= state
.pagedims
[pagedimno
].mediabox
;
3252 unlock ("ml_getpdimrect");
3255 Store_double_field (ret_v
, 0, box
.x0
);
3256 Store_double_field (ret_v
, 1, box
.x1
);
3257 Store_double_field (ret_v
, 2, box
.y0
);
3258 Store_double_field (ret_v
, 3, box
.y1
);
3263 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3264 value dw_v
, value cols_v
)
3266 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3272 double winw
= Int_val (winw_v
);
3273 double winh
= Int_val (winh_v
);
3274 double dw
= Int_val (dw_v
);
3275 double cols
= Int_val (cols_v
);
3276 double pw
= 1.0, ph
= 1.0;
3278 if (trylock ("ml_zoom_for_height")) {
3282 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3283 double w
= p
->pagebox
.x1
/ cols
;
3284 double h
= p
->pagebox
.y1
;
3288 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3290 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3293 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3294 unlock ("ml_zoom_for_height");
3296 ret_v
= caml_copy_double (zoom
);
3300 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3302 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3304 int pt
= Int_val(pt_v
);
3305 int x
= Int_val (x_v
);
3306 int y
= Int_val (y_v
);
3309 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3310 ret_v
= caml_copy_double (w
);
3314 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3316 CAMLparam2 (pt_v
, string_v
);
3318 int pt
= Int_val (pt_v
);
3321 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3322 ret_v
= caml_copy_double (w
);
3326 CAMLprim value
ml_getpagebox (value opaque_v
)
3328 CAMLparam1 (opaque_v
);
3334 char *s
= String_val (opaque_v
);
3335 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3337 ret_v
= caml_alloc_tuple (4);
3338 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3339 dev
->hints
|= FZ_IGNORE_SHADE
;
3341 switch (page
->type
) {
3343 ctm
= pagectm (page
);
3344 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3348 ctm
= pagectm (page
);
3349 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3353 rect
= fz_infinite_rect
;
3357 fz_free_device (dev
);
3358 fz_round_rect (&bbox
, &rect
);
3359 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3360 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3361 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3362 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3367 CAMLprim value
ml_setaalevel (value level_v
)
3369 CAMLparam1 (level_v
);
3371 state
.aalevel
= Int_val (level_v
);
3372 CAMLreturn (Val_unit
);
3376 #include <X11/Xlib.h>
3382 GLXDrawable drawable
;
3385 #include "keysym2ucs.c"
3387 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3389 CAMLparam1 (keysym_v
);
3391 KeySym keysym
= Int_val (keysym_v
);
3396 rune
= keysym2ucs (keysym
);
3397 len
= fz_runetochar (buf
, rune
);
3399 str_v
= caml_copy_string (buf
);
3403 CAMLprim value
ml_glx (value win_v
)
3406 XVisualInfo
*visual
;
3407 int screen
, wid
= Int_val (win_v
);
3408 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3410 glx
.dpy
= XOpenDisplay (NULL
);
3412 caml_failwith ("XOpenDisplay");
3415 screen
= DefaultScreen (glx
.dpy
);
3416 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3418 XCloseDisplay (glx
.dpy
);
3420 caml_failwith ("glXChooseVisual");
3423 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3426 XCloseDisplay (glx
.dpy
);
3428 caml_failwith ("glXCreateContext");
3431 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3432 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3433 XCloseDisplay (glx
.dpy
);
3436 caml_failwith ("glXMakeCurrent");
3439 CAMLreturn (Val_unit
);
3442 CAMLprim value
ml_swapb (value unit_v
)
3444 CAMLparam1 (unit_v
);
3445 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3446 CAMLreturn (Val_unit
);
3449 CAMLprim value
ml_glxsync (value unit_v
)
3451 CAMLparam1 (unit_v
);
3452 if (glx
.dpy
&& glx
.ctx
) {
3456 CAMLreturn (Val_unit
);
3459 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3460 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3462 CAMLprim value
ml_platform (value unit_v
)
3464 CAMLparam1 (unit_v
);
3465 int platid
= piunknown
;
3467 #if defined __linux__
3469 #elif defined __CYGWIN__
3471 #elif defined __DragonFly__
3472 platid
= pidragonflybsd
;
3473 #elif defined __FreeBSD__
3475 #elif defined __OpenBSD__
3477 #elif defined __NetBSD__
3479 #elif defined __sun__
3481 #elif defined __APPLE__
3484 CAMLreturn (Val_int (platid
));
3487 CAMLprim value
ml_cloexec (value fd_v
)
3490 int fd
= Int_val (fd_v
);
3492 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3493 uerror ("fcntl", Nothing
);
3495 CAMLreturn (Val_unit
);
3498 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3500 CAMLparam2 (w_v
, h_v
);
3503 int w
= Int_val (w_v
);
3504 int h
= Int_val (h_v
);
3505 int cs
= Int_val (cs_v
);
3507 if (state
.pbo_usable
) {
3508 pbo
= calloc (sizeof (*pbo
), 1);
3510 err (1, "calloc pbo");
3522 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3525 state
.glGenBuffersARB (1, &pbo
->id
);
3526 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3527 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3528 NULL
, GL_STREAM_DRAW
);
3529 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3531 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3533 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3534 state
.glDeleteBuffersARB (1, &pbo
->id
);
3536 ret_v
= caml_copy_string ("0");
3542 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3544 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3548 err (1, "malloc %d bytes failed", res
+1);
3550 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3552 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3554 ret_v
= caml_copy_string (s
);
3559 ret_v
= caml_copy_string ("0");
3564 CAMLprim value
ml_freepbo (value s_v
)
3567 char *s
= String_val (s_v
);
3568 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3571 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3573 tile
->pbo
->ptr
= NULL
;
3574 tile
->pbo
->size
= -1;
3576 CAMLreturn (Val_unit
);
3579 CAMLprim value
ml_unmappbo (value s_v
)
3582 char *s
= String_val (s_v
);
3583 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3586 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3587 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3588 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3590 tile
->pbo
->ptr
= NULL
;
3591 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3593 CAMLreturn (Val_unit
);
3596 static void setuppbo (void)
3598 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3599 GGPA (glBindBufferARB
);
3600 if (state
.glBindBufferARB
) {
3601 GGPA (glUnmapBufferARB
);
3602 if (state
.glUnmapBufferARB
) {
3603 GGPA (glMapBufferARB
);
3604 if (state
.glMapBufferARB
) {
3605 GGPA (glBufferDataARB
);
3606 if (state
.glBufferDataARB
) {
3607 GGPA (glGenBuffersARB
);
3608 if (state
.glGenBuffersARB
) {
3609 GGPA (glDeleteBuffersARB
);
3610 if (state
.glDeleteBuffersARB
) {
3611 state
.pbo_usable
= 1;
3621 CAMLprim value
ml_pbo_usable (value unit_v
)
3623 CAMLparam1 (unit_v
);
3624 CAMLreturn (Val_bool (state
.pbo_usable
));
3627 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3629 CAMLparam3 (ptr_v
, x_v
, y_v
);
3630 CAMLlocal2 (ret_v
, tup_v
);
3632 char *s
= String_val (ptr_v
);
3633 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3634 struct pagedim
*pdim
;
3638 page
= parse_pointer ("ml_unproject", s
);
3639 pdim
= &state
.pagedims
[page
->pdimno
];
3641 ret_v
= Val_int (0);
3642 if (trylock ("ml_unproject")) {
3646 switch (page
->type
) {
3648 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3657 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3658 fz_invert_matrix (&ctm
, &ctm
);
3659 fz_transform_point (&p
, &ctm
);
3661 tup_v
= caml_alloc_tuple (2);
3662 ret_v
= caml_alloc_small (1, 1);
3663 Field (tup_v
, 0) = Val_int (p
.x
);
3664 Field (tup_v
, 1) = Val_int (p
.y
);
3665 Field (ret_v
, 0) = tup_v
;
3667 unlock ("ml_unproject");
3672 static void makestippletex (void)
3674 const char *pixels
= "\xff\xff\0\0";
3675 glGenTextures (1, &state
.stid
);
3676 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3677 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3678 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3691 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3693 CAMLparam2 (pipe_v
, params_v
);
3694 CAMLlocal2 (trim_v
, fuzz_v
);
3701 struct sigaction sa
;
3703 state
.cr
= Int_val (Field (pipe_v
, 0));
3704 state
.cw
= Int_val (Field (pipe_v
, 1));
3705 state
.rotate
= Int_val (Field (params_v
, 0));
3706 state
.fitmodel
= Int_val (Field (params_v
, 1));
3707 trim_v
= Field (params_v
, 2);
3708 texcount
= Int_val (Field (params_v
, 3));
3709 state
.sliceheight
= Int_val (Field (params_v
, 4));
3710 mustoresize
= Int_val (Field (params_v
, 5));
3711 colorspace
= Int_val (Field (params_v
, 6));
3712 fontpath
= String_val (Field (params_v
, 7));
3713 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3714 if (!state
.trimcachepath
) {
3715 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3718 haspboext
= Bool_val (Field (params_v
, 9));
3720 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3722 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3723 fuzz_v
= Field (trim_v
, 1);
3724 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3725 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3726 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3727 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3729 set_tex_params (colorspace
);
3732 state
.face
= load_font (fontpath
);
3736 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3738 state
.face
= load_builtin_font (base
, len
);
3740 if (!state
.face
) _exit (1);
3742 realloctexts (texcount
);
3750 sa
.sa_handler
= SIG_IGN
;
3751 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3753 sa
.sa_handler
= SIG_DFL
;
3754 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3756 if (sigemptyset (&sa
.sa_mask
)) {
3757 err (1, "sigemptyset");
3759 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3760 err (1, "sigaction");
3763 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3765 errx (1, "pthread_create: %s", strerror (ret
));
3768 CAMLreturn (Val_unit
);