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
);
603 static int cacheline32bytes
;
605 static void __attribute__ ((constructor
)) clcheck (void)
607 char **envp
= environ
;
612 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
614 cacheline32bytes
= auxv
[1] == 32;
620 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
622 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
623 if (cacheline32bytes
&& size
> 32) {
624 intptr_t a1
, a2
, diff
;
626 vector
unsigned char v
= vec_splat_u8 (-1);
627 vector
unsigned char *p
;
629 a1
= a2
= (intptr_t) pixmap
->samples
;
630 a2
= (a1
+ 31) & ~31;
635 while (a1
!= a2
) *(char *) a1
++ = 0xff;
636 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
637 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
639 vec_st (v
, i
+ 16, p
);
641 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
643 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
646 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
649 static void trimctm (pdf_page
*page
, int pindex
)
652 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
654 if (!pdim
->tctmready
) {
655 if (state
.trimmargins
) {
657 fz_matrix rm
, sm
, tm
, im
, ctm1
;
659 fz_rotate (&rm
, -pdim
->rotate
);
660 fz_scale (&sm
, 1, -1);
661 fz_concat (&ctm
, &rm
, &sm
);
662 realbox
= pdim
->mediabox
;
663 fz_transform_rect (&realbox
, &ctm
);
664 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
665 fz_concat (&ctm1
, &ctm
, &tm
);
666 fz_invert_matrix (&im
, &page
->ctm
);
667 fz_concat (&ctm
, &im
, &ctm1
);
677 static fz_matrix
pagectm (struct page
*page
)
681 if (page
->type
== DPDF
) {
682 trimctm (page
->u
.pdfpage
, page
->pdimno
);
684 &state
.pagedims
[page
->pdimno
].tctm
,
685 &state
.pagedims
[page
->pdimno
].ctm
);
688 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
690 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
691 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
696 static void *loadpage (int pageno
, int pindex
)
699 struct page
*page
= NULL
;
701 page
= calloc (sizeof (struct page
), 1);
703 err (1, "calloc page %d", pageno
);
706 page
->dlist
= fz_new_display_list (state
.ctx
);
707 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
709 switch (state
.type
) {
711 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
712 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
714 page
->freepage
= freepdfpage
;
718 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
719 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
721 page
->freepage
= freexpspage
;
725 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
726 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
728 page
->freepage
= freecbzpage
;
732 fz_catch (state
.ctx
) {
734 page
->freepage
= freeemptyxxxpage
;
736 fz_free_device (dev
);
738 page
->pdimno
= pindex
;
739 page
->pageno
= pageno
;
740 page
->sgen
= state
.gen
;
741 page
->tgen
= state
.gen
;
742 page
->type
= state
.type
;
747 static struct tile
*alloctile (int h
)
754 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
755 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
756 tile
= calloc (tilesize
, 1);
758 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
760 for (i
= 0; i
< slicecount
; ++i
) {
761 int sh
= MIN (h
, state
.sliceheight
);
762 tile
->slices
[i
].h
= sh
;
763 tile
->slices
[i
].texindex
= -1;
766 tile
->slicecount
= slicecount
;
767 tile
->sliceheight
= state
.sliceheight
;
777 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
778 const fz_matrix
*ctm
, float alpha
)
780 struct obs
*obs
= dev
->user
;
782 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
784 fz_rect rect
= fz_unit_rect
;
786 fz_transform_rect (&rect
, ctm
);
787 fz_round_rect (&b
, &rect
);
788 fz_intersect_irect (&b
, &obs
->b
);
789 obs
->cured
= b
.x0
== obs
->b
.x0
792 && b
.y1
== obs
->b
.y1
;
796 static int obscured (struct page
*page
, fz_irect bbox
)
803 memset (&dev
, 0, sizeof (dev
));
804 memset (&obs
, 0, sizeof (obs
));
809 dev
.fill_image
= obs_fill_image
;
811 fz_rect_from_irect (&rect
, &bbox
);
812 ctm
= pagectm (page
);
813 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
816 #define OBSCURED obscured
818 #define OBSCURED(a, b) 0
821 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
829 struct pagedim
*pdim
;
831 tile
= alloctile (h
);
832 pdim
= &state
.pagedims
[page
->pdimno
];
837 bbox
.x1
= bbox
.x0
+ w
;
838 bbox
.y1
= bbox
.y0
+ h
;
841 if (state
.pig
->w
== w
843 && state
.pig
->colorspace
== state
.colorspace
) {
844 tile
->pixmap
= state
.pig
;
845 tile
->pixmap
->x
= bbox
.x0
;
846 tile
->pixmap
->y
= bbox
.y0
;
849 fz_drop_pixmap (state
.ctx
, state
.pig
);
856 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
862 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
868 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
869 clearpixmap (tile
->pixmap
);
871 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
872 ctm
= pagectm (page
);
873 fz_rect_from_irect (&rect
, &bbox
);
874 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
875 fz_free_device (dev
);
880 static void initpdims (void)
883 int pageno
, trim
, show
;
889 if (state
.trimmargins
&& state
.trimcachepath
) {
890 trimf
= fopen (state
.trimcachepath
, "rb");
892 trimf
= fopen (state
.trimcachepath
, "wb");
896 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
901 switch (state
.type
) {
903 pdf_obj
*pageobj
= state
.u
.pdf
->page_objs
[pageno
];
905 if (state
.trimmargins
) {
910 page
= pdf_load_page (state
.u
.pdf
, pageno
);
911 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
912 trim
= state
.trimanew
|| !obj
;
918 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
919 dev
->hints
|= FZ_IGNORE_SHADE
;
920 fz_invert_matrix (&ctm
, &page
->ctm
);
921 pdf_run_page (state
.u
.pdf
, page
, dev
, &fz_identity
, NULL
);
922 fz_free_device (dev
);
924 rect
.x0
+= state
.trimfuzz
.x0
;
925 rect
.x1
+= state
.trimfuzz
.x1
;
926 rect
.y0
+= state
.trimfuzz
.y0
;
927 rect
.y1
+= state
.trimfuzz
.y1
;
928 fz_transform_rect (&rect
, &ctm
);
929 fz_intersect_rect (&rect
, &page
->mediabox
);
931 if (fz_is_empty_rect (&rect
)) {
932 mediabox
= page
->mediabox
;
938 obj
= pdf_new_array (state
.ctx
, 4);
939 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x0
));
940 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y0
));
941 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.x1
));
942 pdf_array_push (obj
, pdf_new_real (state
.ctx
, mediabox
.y1
));
943 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
946 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
947 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
948 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
949 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
952 rotate
= page
->rotate
;
953 pdf_free_page (state
.u
.pdf
, page
);
955 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
957 printd ("progress %f Trimming %d",
958 (double) (pageno
+ 1) / state
.pagecount
,
962 fz_catch (state
.ctx
) {
963 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
969 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
971 if (fz_is_empty_rect (&mediabox
)) {
972 fprintf (stderr
, "cannot find page size for page %d\n",
980 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
982 if (!fz_is_empty_rect (&cropbox
)) {
983 fz_intersect_rect (&mediabox
, &cropbox
);
985 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
995 page
= xps_load_page (state
.u
.xps
, pageno
);
996 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
998 if (state
.trimmargins
) {
1002 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1003 dev
->hints
|= FZ_IGNORE_SHADE
;
1004 xps_run_page (state
.u
.xps
, page
, dev
,
1005 &fz_identity
, NULL
);
1006 fz_free_device (dev
);
1008 rect
.x0
+= state
.trimfuzz
.x0
;
1009 rect
.x1
+= state
.trimfuzz
.x1
;
1010 rect
.y0
+= state
.trimfuzz
.y0
;
1011 rect
.y1
+= state
.trimfuzz
.y1
;
1012 fz_intersect_rect (&rect
, &mediabox
);
1014 if (!fz_is_empty_rect (&rect
)) {
1018 xps_free_page (state
.u
.xps
, page
);
1019 printd ("progress %f loading %d",
1020 (double) (pageno
+ 1) / state
.pagecount
,
1023 fz_catch (state
.ctx
) {
1031 if (state
.trimmargins
&& trimw
) {
1034 fz_try (state
.ctx
) {
1035 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1036 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1037 cbz_free_page (state
.u
.cbz
, page
);
1038 printd ("progress %f Trimming %d",
1039 (double) (pageno
+ 1) / state
.pagecount
,
1042 fz_catch (state
.ctx
) {
1043 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1050 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1052 err (1, "fwrite trim mediabox");
1058 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1060 err (1, "fread trim mediabox");
1064 mediabox
.x0
= mediabox
.y0
= 0;
1073 ARSERT (0 && state
.type
);
1076 if (state
.pagedimcount
== 0
1077 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1078 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1081 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1082 state
.pagedims
= realloc (state
.pagedims
, size
);
1083 if (!state
.pagedims
) {
1084 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1085 size
, state
.pagedimcount
+ 1);
1088 p
= &state
.pagedims
[state
.pagedimcount
++];
1090 p
->mediabox
= mediabox
;
1095 if (state
.trimmargins
) {
1096 printd ("progress 1 Trimmed %d pages in %f seconds",
1097 state
.pagecount
, end
- start
);
1100 printd ("vmsg Processed %d pages in %f seconds",
1101 state
.pagecount
, end
- start
);
1105 if (fclose (trimf
)) {
1111 static void layout (void)
1116 double zoom
, w
, maxw
= 0.0;
1117 struct pagedim
*p
= state
.pagedims
;
1119 if (state
.fitmodel
== FitProportional
) {
1123 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1128 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1130 fz_transform_rect (&rect
, &rm
);
1132 x0
= MIN (rect
.x0
, rect
.x1
);
1133 x1
= MAX (rect
.x0
, rect
.x1
);
1136 maxw
= MAX (w
, maxw
);
1141 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
1143 fz_matrix tm
, sm
, rm
;
1145 fz_rotate (&ctm
, state
.rotate
);
1146 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1148 fz_transform_rect (&box
, &rm
);
1149 w
= box
.x1
- box
.x0
;
1150 switch (state
.fitmodel
) {
1151 case FitProportional
:
1153 double scale
= w
/ maxw
;
1154 zoom
= (state
.w
/ w
) * scale
;
1160 h
= box
.y1
- box
.y0
;
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_span
*span
, fz_matrix ctm
,
1317 int stop
, int pageno
, double start
)
1324 fz_point p1
, p2
, p3
, p4
;
1326 p
= strofspan (span
);
1329 ret
= regexec (re
, p
, 1, &rm
, 0);
1332 if (ret
!= REG_NOMATCH
) {
1335 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1336 printd ("msg regexec error `%.*s'",
1337 (int) size
, errbuf
);
1344 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1345 c
+= fz_runelen (span
->text
[a
].c
);
1347 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1348 c
+= fz_runelen (span
->text
[b
].c
);
1351 if (fz_runelen (span
->text
[b
].c
) > 1) {
1354 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1355 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
1367 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1374 printd ("progress 1 found at %d `%.*s' in %f sec",
1375 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1379 printd ("match %d %d %f %f %f %f %f %f %f %f",
1391 static int compareblocks (const void *l
, const void *r
)
1393 fz_text_block
const *ls
= l
;
1394 fz_text_block
const* rs
= r
;
1395 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1398 /* wishful thinking function */
1399 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1404 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1406 fz_text_sheet
*sheet
;
1407 struct pagedim
*pdim
, *pdimprev
;
1408 int stop
= 0, niters
= 0;
1411 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1415 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1416 if (niters
++ == 5) {
1419 printd ("progress 1 attention requested aborting search at %d",
1424 printd ("progress %f searching in page %d",
1425 (double) (pageno
+ 1) / state
.pagecount
,
1430 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1431 pdim
= &state
.pagedims
[i
];
1432 if (pdim
->pageno
== pageno
) {
1435 if (pdim
->pageno
> pageno
) {
1444 sheet
= fz_new_text_sheet (state
.ctx
);
1445 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1446 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1448 switch (state
.type
) {
1451 fz_try (state
.ctx
) {
1452 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1453 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1454 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1455 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1457 fz_catch (state
.ctx
) {
1458 fz_free_device (tdev
);
1465 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1466 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1470 ARSERT (0 && state
.type
);
1473 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1474 fz_free_device (tdev
);
1476 for (j
= 0; j
< text
->len
; ++j
) {
1478 fz_text_block
*block
;
1480 block
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1482 for (k
= 0; k
< block
->len
; ++k
) {
1487 line
= &block
->lines
[k
];
1488 if (line
->bbox
.y0
< y
+ 1) continue;
1491 line
= &block
->lines
[block
->len
- 1 - k
];
1492 if (line
->bbox
.y0
> y
- 1) continue;
1495 for (span
= line
->spans
;
1496 span
< line
->spans
+ line
->len
;
1499 switch (matchspan (re
, span
, ctm
, stop
, pageno
, start
)) {
1501 case 1: stop
= 1; break;
1502 case -1: stop
= 1; goto endloop
;
1517 fz_free_text_page (state
.ctx
, text
);
1518 fz_free_text_sheet (state
.ctx
, sheet
);
1520 state
.freepage (u
.ptr
);
1525 printd ("progress 1 no matches %f sec", end
- start
);
1527 printd ("clearrects");
1530 static void set_tex_params (int colorspace
)
1537 switch (colorspace
) {
1539 state
.texiform
= GL_RGBA8
;
1540 state
.texform
= GL_RGBA
;
1541 state
.texty
= GL_UNSIGNED_BYTE
;
1542 state
.colorspace
= fz_device_rgb
;
1545 state
.texiform
= GL_RGBA8
;
1546 state
.texform
= GL_BGRA
;
1547 state
.texty
= endianness
.s
> 1
1548 ? GL_UNSIGNED_INT_8_8_8_8
1549 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1550 state
.colorspace
= fz_device_bgr
;
1553 state
.texiform
= GL_LUMINANCE_ALPHA
;
1554 state
.texform
= GL_LUMINANCE_ALPHA
;
1555 state
.texty
= GL_UNSIGNED_BYTE
;
1556 state
.colorspace
= fz_device_gray
;
1559 errx (1, "invalid colorspce %d", colorspace
);
1563 static void realloctexts (int texcount
)
1567 if (texcount
== state
.texcount
) return;
1569 if (texcount
< state
.texcount
) {
1570 glDeleteTextures (state
.texcount
- texcount
,
1571 state
.texids
+ texcount
);
1574 size
= texcount
* sizeof (*state
.texids
);
1575 state
.texids
= realloc (state
.texids
, size
);
1576 if (!state
.texids
) {
1577 err (1, "realloc texids %" FMT_s
, size
);
1580 size
= texcount
* sizeof (*state
.texowners
);
1581 state
.texowners
= realloc (state
.texowners
, size
);
1582 if (!state
.texowners
) {
1583 err (1, "realloc texowners %" FMT_s
, size
);
1585 if (texcount
> state
.texcount
) {
1588 glGenTextures (texcount
- state
.texcount
,
1589 state
.texids
+ state
.texcount
);
1590 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1591 state
.texowners
[i
].w
= -1;
1592 state
.texowners
[i
].slice
= NULL
;
1595 state
.texcount
= texcount
;
1599 static char *mbtoutf8 (char *s
)
1605 len
= mbstowcs (NULL
, s
, strlen (s
));
1610 if (len
== (size_t) -1) {
1615 tmp
= malloc (len
* sizeof (wchar_t));
1620 ret
= mbstowcs (tmp
, s
, len
);
1621 if (ret
== (size_t) -1) {
1627 for (i
= 0; i
< ret
; ++i
) {
1628 len
+= fz_runelen (tmp
[i
]);
1631 p
= r
= malloc (len
+ 1);
1637 for (i
= 0; i
< ret
; ++i
) {
1638 p
+= fz_runetochar (p
, tmp
[i
]);
1645 CAMLprim value
ml_mbtoutf8 (value s_v
)
1651 s
= String_val (s_v
);
1657 ret_v
= caml_copy_string (r
);
1663 static void * mainloop (void *unused
)
1666 int len
, ret
, oldlen
= 0;
1671 errx (1, "readlen returned 0");
1674 if (oldlen
< len
+ 1) {
1675 p
= realloc (p
, len
+ 1);
1677 err (1, "realloc %d failed", len
+ 1);
1684 if (!strncmp ("open", p
, 4)) {
1691 ret
= sscanf (p
+ 5, " %d %n", &wthack
, &off
);
1693 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1696 filename
= p
+ 5 + off
;
1697 filenamelen
= strlen (filename
);
1698 password
= filename
+ filenamelen
+ 1;
1701 openxref (filename
, password
);
1707 utf8filename
= mbtoutf8 (filename
);
1708 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1709 if (utf8filename
!= filename
) {
1710 free (utf8filename
);
1713 state
.needoutline
= 1;
1715 else if (!strncmp ("cs", p
, 2)) {
1718 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1720 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1723 set_tex_params (colorspace
);
1724 for (i
= 0; i
< state
.texcount
; ++i
) {
1725 state
.texowners
[i
].w
= -1;
1726 state
.texowners
[i
].slice
= NULL
;
1730 else if (!strncmp ("freepage", p
, 8)) {
1733 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1735 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1739 else if (!strncmp ("freetile", p
, 8)) {
1742 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1744 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1748 else if (!strncmp ("search", p
, 6)) {
1749 int icase
, pageno
, y
, ret
, len2
, forward
;
1753 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1754 &icase
, &pageno
, &y
, &forward
, &len2
);
1756 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1759 pattern
= p
+ 6 + len2
;
1760 ret
= regcomp (&re
, pattern
,
1761 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1766 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1767 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1770 search (&re
, pageno
, y
, forward
);
1774 else if (!strncmp ("geometry", p
, 8)) {
1778 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1780 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1788 for (i
= 0; i
< state
.texcount
; ++i
) {
1789 state
.texowners
[i
].slice
= NULL
;
1796 unlock ("geometry");
1797 printd ("continue %d", state
.pagecount
);
1799 else if (!strncmp ("reqlayout", p
, 9)) {
1801 int rotate
, fitmodel
, off
;
1804 ret
= sscanf (p
+ 9, " %d %d %n", &rotate
, &fitmodel
, &off
);
1806 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1809 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1812 state
.rotate
= rotate
;
1813 state
.fitmodel
= fitmodel
;
1817 nameddest
= p
+ 9 + off
;
1818 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1821 pdf_obj
*needle
, *obj
;
1823 needle
= pdf_new_string (state
.ctx
, nameddest
,
1824 strlen (nameddest
));
1825 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1827 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1829 a
= desttoanchor (&dest
);
1831 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1834 printd ("emsg failed to parse destination `%s'\n",
1839 printd ("emsg destination `%s' not found\n",
1842 pdf_drop_obj (needle
);
1846 unlock ("reqlayout");
1847 printd ("continue %d", state
.pagecount
);
1849 else if (!strncmp ("page", p
, 4)) {
1852 int pageno
, pindex
, ret
;
1854 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1856 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1861 page
= loadpage (pageno
, pindex
);
1865 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1867 else if (!strncmp ("tile", p
, 4)) {
1868 int x
, y
, w
, h
, ret
;
1874 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1875 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1877 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1882 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1886 printd ("tile %d %d %" FMT_ptr
" %u %f",
1888 FMT_ptr_cast2 (tile
),
1889 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1892 else if (!strncmp ("settrim", p
, 7)) {
1896 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1897 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1899 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1903 state
.trimmargins
= trimmargins
;
1904 state
.needoutline
= 1;
1905 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1907 state
.trimfuzz
= fuzz
;
1909 state
.pagedimcount
= 0;
1910 free (state
.pagedims
);
1911 state
.pagedims
= NULL
;
1916 printd ("continue %d", state
.pagecount
);
1918 else if (!strncmp ("sliceh", p
, 6)) {
1921 ret
= sscanf (p
+ 6, " %d", &h
);
1923 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1925 if (h
!= state
.sliceheight
) {
1928 state
.sliceheight
= h
;
1929 for (i
= 0; i
< state
.texcount
; ++i
) {
1930 state
.texowners
[i
].w
= -1;
1931 state
.texowners
[i
].h
= -1;
1932 state
.texowners
[i
].slice
= NULL
;
1936 else if (!strncmp ("interrupt", p
, 9)) {
1937 printd ("vmsg interrupted");
1940 errx (1, "unknown command %.*s", len
, p
);
1946 CAMLprim value
ml_realloctexts (value texcount_v
)
1948 CAMLparam1 (texcount_v
);
1951 if (trylock ("ml_realloctexts")) {
1955 realloctexts (Int_val (texcount_v
));
1957 unlock ("ml_realloctexts");
1960 CAMLreturn (Val_bool (ok
));
1963 static void showsel (struct page
*page
, int ox
, int oy
)
1970 fz_text_block
*block
;
1971 struct mark first
, last
;
1973 first
= page
->fmark
;
1976 if (!first
.span
|| !last
.span
) return;
1978 glEnable (GL_BLEND
);
1979 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1980 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1982 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1983 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1984 for (block
= page
->text
->blocks
;
1985 block
< page
->text
->blocks
+ page
->text
->len
;
1987 for (line
= block
->lines
;
1988 line
< block
->lines
+ block
->len
;
1990 rect
= fz_empty_rect
;
1991 for (span
= line
->spans
;
1992 span
< line
->spans
+ line
->len
;
1996 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2001 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2003 j
= MIN (first
.i
, last
.i
);
2004 k
= MAX (first
.i
, last
.i
);
2006 else if (span
== first
.span
) {
2010 else if (span
== last
.span
) {
2016 for (i
= j
; i
<= k
; ++i
) {
2017 fz_union_rect (&rect
, &span
->text
[i
].bbox
);
2019 fz_round_rect (&bbox
, &rect
);
2020 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2027 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2028 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2029 if (span
== last
.span
) {
2037 glDisable (GL_BLEND
);
2042 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2044 fz_matrix ctm
, tm
, pm
;
2045 fz_link
*link
, *links
;
2047 switch (page
->type
) {
2049 links
= page
->u
.pdfpage
->links
;
2053 links
= page
->u
.xpspage
->links
;
2060 glEnable (GL_TEXTURE_1D
);
2061 glEnable (GL_BLEND
);
2062 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2064 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2065 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2066 fz_translate (&tm
, xoff
, yoff
);
2067 pm
= pagectm (page
);
2068 fz_concat (&ctm
, &pm
, &tm
);
2071 for (link
= links
; link
; link
= link
->next
) {
2072 fz_point p1
, p2
, p3
, p4
;
2074 p1
.x
= link
->rect
.x0
;
2075 p1
.y
= link
->rect
.y0
;
2077 p2
.x
= link
->rect
.x1
;
2078 p2
.y
= link
->rect
.y0
;
2080 p3
.x
= link
->rect
.x1
;
2081 p3
.y
= link
->rect
.y1
;
2083 p4
.x
= link
->rect
.x0
;
2084 p4
.y
= link
->rect
.y1
;
2086 fz_transform_point (&p1
, &ctm
);
2087 fz_transform_point (&p2
, &ctm
);
2088 fz_transform_point (&p3
, &ctm
);
2089 fz_transform_point (&p4
, &ctm
);
2091 switch (link
->dest
.kind
) {
2092 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2093 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2094 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2095 default: glColor3ub (0, 0, 0); break;
2103 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2107 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2110 glVertex2f (p1
.x
, p1
.y
);
2112 glVertex2f (p2
.x
, p2
.y
);
2115 glVertex2f (p2
.x
, p2
.y
);
2117 glVertex2f (p3
.x
, p3
.y
);
2120 glVertex2f (p3
.x
, p3
.y
);
2122 glVertex2f (p4
.x
, p4
.y
);
2125 glVertex2f (p4
.x
, p4
.y
);
2127 glVertex2f (p1
.x
, p1
.y
);
2132 glDisable (GL_BLEND
);
2133 glDisable (GL_TEXTURE_1D
);
2136 static int compareslinks (const void *l
, const void *r
)
2138 struct slink
const *ls
= l
;
2139 struct slink
const *rs
= r
;
2140 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2141 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2143 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2146 static void droptext (struct page
*page
)
2149 fz_free_text_page (state
.ctx
, page
->text
);
2152 page
->fmark
.span
= NULL
;
2153 page
->lmark
.span
= NULL
;
2157 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2161 static void dropslinks (struct page
*page
)
2164 free (page
->slinks
);
2165 page
->slinks
= NULL
;
2166 page
->slinkcount
= 0;
2170 static void ensureslinks (struct page
*page
)
2174 size_t slinksize
= sizeof (*page
->slinks
);
2175 fz_link
*link
, *links
;
2177 if (state
.gen
!= page
->sgen
) {
2179 page
->sgen
= state
.gen
;
2181 if (page
->slinks
) return;
2183 switch (page
->type
) {
2185 links
= page
->u
.pdfpage
->links
;
2186 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2188 &state
.pagedims
[page
->pdimno
].tctm
,
2189 &state
.pagedims
[page
->pdimno
].ctm
);
2193 links
= page
->u
.xpspage
->links
;
2194 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2201 for (link
= links
; link
; link
= link
->next
) {
2205 page
->slinkcount
= count
;
2206 page
->slinks
= calloc (count
, slinksize
);
2207 if (!page
->slinks
) {
2208 err (1, "realloc slinks %d", count
);
2211 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2215 fz_transform_rect (&rect
, &ctm
);
2216 page
->slinks
[i
].link
= link
;
2217 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2219 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2223 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2224 static void fmt_linkn (char *s
, unsigned int u
)
2226 unsigned int len
; unsigned int q
;
2227 int zma
= 'z' - 'a' + 1;
2229 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2232 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2233 /* handles u == 0 */
2238 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2239 int noff
, char *targ
, int tlen
, int hfsize
)
2243 struct slink
*slink
;
2244 double x0
, y0
, x1
, y1
, w
;
2246 ensureslinks (page
);
2247 glColor3ub (0xc3, 0xb0, 0x91);
2248 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2249 fmt_linkn (buf
, i
+ noff
);
2250 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2251 slink
= &page
->slinks
[i
];
2253 x0
= slink
->bbox
.x0
+ xoff
- 5;
2254 y1
= slink
->bbox
.y0
+ yoff
- 5;
2255 y0
= y1
+ 10 + hfsize
;
2256 w
= measure_string (state
.face
, hfsize
, buf
);
2258 glRectd (x0
, y0
, x1
, y1
);
2262 glEnable (GL_BLEND
);
2263 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2264 glEnable (GL_TEXTURE_2D
);
2265 glColor3ub (0, 0, 0);
2266 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2267 fmt_linkn (buf
, i
+ noff
);
2268 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2269 slink
= &page
->slinks
[i
];
2271 x0
= slink
->bbox
.x0
+ xoff
;
2272 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2273 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2276 glDisable (GL_TEXTURE_2D
);
2277 glDisable (GL_BLEND
);
2281 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2284 struct slice
*slice1
;
2285 unsigned char *texdata
;
2288 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2289 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2291 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2292 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2293 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2297 int texindex
= state
.texindex
++ % state
.texcount
;
2299 if (state
.texowners
[texindex
].w
== tile
->w
) {
2300 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2304 state
.texowners
[texindex
].h
= slice
->h
;
2308 state
.texowners
[texindex
].h
= slice
->h
;
2311 state
.texowners
[texindex
].w
= tile
->w
;
2312 state
.texowners
[texindex
].slice
= slice
;
2313 slice
->texindex
= texindex
;
2315 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2317 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2321 texdata
= tile
->pixmap
->samples
;
2324 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2336 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2348 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2353 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2355 CAMLparam2 (args_v
, ptr_v
);
2356 int dispx
= Int_val (Field (args_v
, 0));
2357 int dispy
= Int_val (Field (args_v
, 1));
2358 int dispw
= Int_val (Field (args_v
, 2));
2359 int disph
= Int_val (Field (args_v
, 3));
2360 int tilex
= Int_val (Field (args_v
, 4));
2361 int tiley
= Int_val (Field (args_v
, 5));
2362 char *s
= String_val (ptr_v
);
2363 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2365 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2367 int slicey
, firstslice
;
2368 struct slice
*slice
;
2370 firstslice
= tiley
/ tile
->sliceheight
;
2371 slice
= &tile
->slices
[firstslice
];
2372 slicey
= tiley
% tile
->sliceheight
;
2377 dh
= slice
->h
- slicey
;
2378 dh
= MIN (disph
, dh
);
2379 uploadslice (tile
, slice
);
2383 glTexCoord2i (tilex
, slicey
);
2384 glVertex2i (dispx
, dispy
);
2386 glTexCoord2i (tilex
+dispw
, slicey
);
2387 glVertex2i (dispx
+dispw
, dispy
);
2389 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2390 glVertex2i (dispx
+dispw
, dispy
+dh
);
2392 glTexCoord2i (tilex
, slicey
+dh
);
2393 glVertex2i (dispx
, dispy
+dh
);
2400 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2404 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2405 CAMLreturn (Val_unit
);
2408 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2409 value xoff_v
, value yoff_v
,
2412 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2413 int xoff
= Int_val (xoff_v
);
2414 int yoff
= Int_val (yoff_v
);
2415 int noff
= Int_val (Field (li_v
, 0));
2416 char *targ
= String_val (Field (li_v
, 1));
2417 int tlen
= caml_string_length (Field (li_v
, 1));
2418 int hfsize
= Int_val (Field (li_v
, 2));
2419 char *s
= String_val (ptr_v
);
2420 int hlmask
= Int_val (hlinks_v
);
2421 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2424 /* deal with loadpage failed pages */
2428 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2429 if (trylock ("ml_postprocess")) {
2434 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2435 noff
= page
->slinkcount
;
2437 showsel (page
, xoff
, yoff
);
2438 unlock ("ml_postprocess");
2441 CAMLreturn (Val_int (noff
));
2444 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2448 const fz_matrix
*tctm
;
2449 fz_link
*link
, *links
;
2451 switch (page
->type
) {
2453 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2454 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2455 links
= page
->u
.pdfpage
->links
;
2459 tctm
= &fz_identity
;
2460 links
= page
->u
.xpspage
->links
;
2469 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2470 fz_invert_matrix (&ctm
, &ctm
);
2471 fz_transform_point (&p
, &ctm
);
2473 for (link
= links
; link
; link
= link
->next
) {
2474 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2475 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2483 static void ensuretext (struct page
*page
)
2485 if (state
.gen
!= page
->tgen
) {
2487 page
->tgen
= state
.gen
;
2493 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2494 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2495 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2496 ctm
= pagectm (page
);
2497 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2498 qsort (page
->text
->blocks
, page
->text
->len
,
2499 sizeof (*page
->text
->blocks
), compareblocks
);
2500 fz_free_device (tdev
);
2504 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2506 CAMLparam2 (start_page_v
, dir_v
);
2508 int i
, dir
= Int_val (dir_v
);
2509 int start_page
= Int_val (start_page_v
);
2510 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2512 ret_v
= Val_int (0);
2513 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2517 lock ("ml_findpage_with_links");
2518 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2521 switch (state
.type
) {
2524 pdf_page
*page
= NULL
;
2526 fz_try (state
.ctx
) {
2527 page
= pdf_load_page (state
.u
.pdf
, i
);
2528 found
= !!page
->links
;
2530 fz_catch (state
.ctx
) {
2540 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2541 found
= !!page
->links
;
2547 ARSERT ("invalid document type");
2551 ret_v
= caml_alloc_small (1, 1);
2552 Field (ret_v
, 0) = Val_int (i
);
2557 unlock ("ml_findpage_with_links");
2563 enum { dir_first
, dir_last
};
2564 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2566 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2568 CAMLparam2 (ptr_v
, dir_v
);
2569 CAMLlocal2 (ret_v
, pos_v
);
2571 int dirtag
, i
, slinkindex
;
2572 struct slink
*found
= NULL
,*slink
;
2573 char *s
= String_val (ptr_v
);
2575 page
= parse_pointer ("ml_findlink", s
);
2576 ret_v
= Val_int (0);
2577 if (trylock ("ml_findlink")) {
2581 ensureslinks (page
);
2583 if (Is_block (dir_v
)) {
2584 dirtag
= Tag_val (dir_v
);
2586 case dir_first_visible
:
2588 int x0
, y0
, dir
, first_index
, last_index
;
2590 pos_v
= Field (dir_v
, 0);
2591 x0
= Int_val (Field (pos_v
, 0));
2592 y0
= Int_val (Field (pos_v
, 1));
2593 dir
= Int_val (Field (pos_v
, 2));
2598 last_index
= page
->slinkcount
;
2601 first_index
= page
->slinkcount
- 1;
2605 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2606 slink
= &page
->slinks
[i
];
2607 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2616 slinkindex
= Int_val (Field (dir_v
, 0));
2617 found
= &page
->slinks
[slinkindex
];
2618 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2619 slink
= &page
->slinks
[i
];
2620 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2628 slinkindex
= Int_val (Field (dir_v
, 0));
2629 found
= &page
->slinks
[slinkindex
];
2630 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++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
.y0
>= found
->bbox
.y0
) {
2652 slinkindex
= Int_val (Field (dir_v
, 0));
2653 found
= &page
->slinks
[slinkindex
];
2654 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2655 slink
= &page
->slinks
[i
];
2656 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2665 dirtag
= Int_val (dir_v
);
2668 found
= page
->slinks
;
2673 found
= page
->slinks
+ (page
->slinkcount
- 1);
2679 ret_v
= caml_alloc_small (2, 1);
2680 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2683 unlock ("ml_findlink");
2688 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2694 switch (link->dest.kind) { \
2695 case FZ_LINK_GOTO: \
2699 pageno = link->dest.ld.gotor.page; \
2703 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2704 p.y = link->dest.ld.gotor.lt.y; \
2705 fz_transform_point (&p, &pdim->lctm); \
2707 tup_v = caml_alloc_tuple (2); \
2708 ret_v = caml_alloc_small (1, ugoto); \
2709 Field (tup_v, 0) = Val_int (pageno); \
2710 Field (tup_v, 1) = Val_int (p.y); \
2711 Field (ret_v, 0) = tup_v; \
2716 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2717 ret_v = caml_alloc_small (1, uuri); \
2718 Field (ret_v, 0) = str_v; \
2721 case FZ_LINK_LAUNCH: \
2722 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2723 ret_v = caml_alloc_small (1, ulaunch); \
2724 Field (ret_v, 0) = str_v; \
2727 case FZ_LINK_NAMED: \
2728 str_v = caml_copy_string (link->dest.ld.named.named); \
2729 ret_v = caml_alloc_small (1, unamed); \
2730 Field (ret_v, 0) = str_v; \
2733 case FZ_LINK_GOTOR: \
2734 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2735 pageno = link->dest.ld.gotor.page; \
2736 tup_v = caml_alloc_tuple (2); \
2737 ret_v = caml_alloc_small (1, uremote); \
2738 Field (tup_v, 0) = str_v; \
2739 Field (tup_v, 1) = Val_int (pageno); \
2740 Field (ret_v, 0) = tup_v; \
2747 snprintf (buf, sizeof (buf), \
2748 "unhandled link kind %d", link->dest.kind); \
2749 str_v = caml_copy_string (buf); \
2750 ret_v = caml_alloc_small (1, uunexpected); \
2751 Field (ret_v, 0) = str_v; \
2757 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2759 CAMLparam2 (ptr_v
, n_v
);
2760 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2763 struct pagedim
*pdim
;
2764 char *s
= String_val (ptr_v
);
2766 ret_v
= Val_int (0);
2767 if (trylock ("ml_getlink")) {
2771 page
= parse_pointer ("ml_getlink", s
);
2772 ensureslinks (page
);
2773 pdim
= &state
.pagedims
[page
->pdimno
];
2774 link
= page
->slinks
[Int_val (n_v
)].link
;
2777 unlock ("ml_getlink");
2782 CAMLprim value
ml_getlinkcount (value ptr_v
)
2786 char *s
= String_val (ptr_v
);
2788 page
= parse_pointer ("ml_getlinkcount", s
);
2789 CAMLreturn (Val_int (page
->slinkcount
));
2792 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2794 CAMLparam2 (ptr_v
, n_v
);
2797 struct slink
*slink
;
2798 char *s
= String_val (ptr_v
);
2800 page
= parse_pointer ("ml_getlinkrect", s
);
2801 ret_v
= caml_alloc_tuple (4);
2802 if (trylock ("ml_getlinkrect")) {
2803 Field (ret_v
, 0) = Val_int (0);
2804 Field (ret_v
, 1) = Val_int (0);
2805 Field (ret_v
, 2) = Val_int (0);
2806 Field (ret_v
, 3) = Val_int (0);
2809 ensureslinks (page
);
2811 slink
= &page
->slinks
[Int_val (n_v
)];
2812 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2813 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2814 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2815 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2816 unlock ("ml_getlinkrect");
2822 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2824 CAMLparam3 (ptr_v
, x_v
, y_v
);
2825 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2828 char *s
= String_val (ptr_v
);
2829 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2830 struct pagedim
*pdim
;
2832 ret_v
= Val_int (0);
2833 if (trylock ("ml_whatsunder")) {
2837 page
= parse_pointer ("ml_whatsunder", s
);
2838 pdim
= &state
.pagedims
[page
->pdimno
];
2839 x
+= pdim
->bounds
.x0
;
2840 y
+= pdim
->bounds
.y0
;
2841 link
= getlink (page
, x
, y
);
2847 fz_text_block
*block
;
2850 for (block
= page
->text
->blocks
;
2851 block
< page
->text
->blocks
+ page
->text
->len
;
2856 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2859 for (line
= block
->lines
;
2860 line
< block
->lines
+ block
->len
;
2865 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2868 for (span
= line
->spans
;
2869 span
< line
->spans
+ line
->len
;
2874 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2877 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
2880 if (x
>= b
->x0
&& x
<= b
->x1
2881 && y
>= b
->y0
&& y
<= b
->y1
) {
2883 span
->style
->font
&& span
->style
->font
->name
2884 ? span
->style
->font
->name
2885 : "Span has no font name"
2887 FT_FaceRec
*face
= span
->style
->font
->ft_face
;
2888 if (face
&& face
->family_name
) {
2890 char *n1
= face
->family_name
;
2891 size_t l1
= strlen (n1
);
2892 size_t l2
= strlen (n2
);
2894 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2895 s
= malloc (l1
+ l2
+ 2);
2899 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2900 str_v
= caml_copy_string (s
);
2906 str_v
= caml_copy_string (n2
);
2908 ret_v
= caml_alloc_small (1, utext
);
2909 Field (ret_v
, 0) = str_v
;
2918 unlock ("ml_whatsunder");
2924 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2926 CAMLparam2 (ptr_v
, rect_v
);
2929 struct pagedim
*pdim
;
2930 int i
, x0
, x1
, y0
, y1
;
2931 char *s
= String_val (ptr_v
);
2933 fz_text_block
*block
;
2934 fz_text_span
*span
, *fspan
, *lspan
;
2935 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
2937 if (trylock ("ml_seltext")) {
2941 page
= parse_pointer ("ml_seltext", s
);
2944 pdim
= &state
.pagedims
[page
->pdimno
];
2945 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
2946 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2947 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2948 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2951 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2952 glColor3ub (128, 128, 128);
2953 glRecti (x0
, y0
, x1
, y1
);
2954 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2957 fspan
= lspan
= NULL
;
2959 for (block
= page
->text
->blocks
;
2960 block
< page
->text
->blocks
+ page
->text
->len
;
2962 for (line
= block
->lines
;
2963 line
< block
->lines
+ block
->len
;
2965 for (span
= line
->spans
;
2966 span
< line
->spans
+ line
->len
;
2968 for (i
= 0; i
< span
->len
; ++i
) {
2969 b
= &span
->text
[i
].bbox
;
2972 if (x0
>= b
->x0
&& x0
<= b
->x1
2973 && y0
>= b
->y0
&& y0
<= b
->y1
) {
2979 if (x1
>= b
->x0
&& x1
<= b
->x1
2980 && y1
>= b
->y0
&& y1
<= b
->y1
) {
2986 if (0 && selected
) {
2987 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2988 glColor3ub (128, 128, 128);
2989 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2990 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2996 if (y1
< y0
|| x1
< x0
) {
2999 if (fspan
== lspan
) {
3004 if (fline
!= lline
) {
3023 page
->fmark
.span
= fspan
;
3026 page
->lmark
.span
= lspan
;
3028 unlock ("ml_seltext");
3031 CAMLreturn (Val_unit
);
3034 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3039 for (i
= a
; i
<= b
; ++i
) {
3040 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3041 ret
= fwrite (buf
, len
, 1, f
);
3044 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3045 len
, ret
, strerror (errno
));
3053 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3055 caml_failwith ("ml_popen not implemented under Cygwin");
3058 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3060 CAMLparam2 (command_v
, fds_v
);
3061 CAMLlocal2 (l_v
, tup_v
);
3064 value earg_v
= Nothing
;
3065 posix_spawnattr_t attr
;
3066 posix_spawn_file_actions_t fa
;
3067 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3069 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3070 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3073 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3074 msg
= "posix_spawnattr_init";
3078 #ifdef POSIX_SPAWN_USEVFORK
3079 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3080 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3085 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3088 tup_v
= Field (l_v
, 0);
3089 fd1
= Int_val (Field (tup_v
, 0));
3090 fd2
= Int_val (Field (tup_v
, 1));
3092 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3093 msg
= "posix_spawn_file_actions_addclose";
3099 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3100 msg
= "posix_spawn_file_actions_adddup2";
3107 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3108 msg
= "posix_spawn";
3113 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3114 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3118 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3119 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3124 unix_error (ret
, msg
, earg_v
);
3126 CAMLreturn (Val_unit
);
3130 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3138 fz_text_block
*block
;
3139 int fd
= Int_val (fd_v
);
3140 char *s
= String_val (ptr_v
);
3142 if (trylock ("ml_copysel")) {
3146 page
= parse_pointer ("ml_sopysel", s
);
3148 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3149 fprintf (stderr
, "nothing to copy\n");
3153 f
= fdopen (fd
, "w");
3155 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3160 for (block
= page
->text
->blocks
;
3161 block
< page
->text
->blocks
+ page
->text
->len
;
3163 for (line
= block
->lines
;
3164 line
< block
->lines
+ block
->len
;
3166 for (span
= line
->spans
;
3167 span
< line
->spans
+ line
->len
;
3171 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3172 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3173 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3176 if (pipespan (f
, span
, a
, b
)) {
3179 if (span
== line
->spans
+ line
->len
- 1) {
3180 if (putc ('\n', f
) == EOF
) {
3182 "failed break line on sel pipe: %s\n",
3187 if (span
== page
->lmark
.span
) {
3195 page
->lmark
.span
= NULL
;
3196 page
->fmark
.span
= NULL
;
3200 int ret
= fclose (f
);
3203 if (errno
!= ECHILD
) {
3204 fprintf (stderr
, "failed to close sel pipe: %s\n",
3210 unlock ("ml_copysel");
3215 fprintf (stderr
, "failed to close sel pipe: %s\n",
3219 CAMLreturn (Val_unit
);
3222 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3224 CAMLparam1 (pagedimno_v
);
3226 int pagedimno
= Int_val (pagedimno_v
);
3229 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3230 if (trylock ("ml_getpdimrect")) {
3231 box
= fz_empty_rect
;
3234 box
= state
.pagedims
[pagedimno
].mediabox
;
3235 unlock ("ml_getpdimrect");
3238 Store_double_field (ret_v
, 0, box
.x0
);
3239 Store_double_field (ret_v
, 1, box
.x1
);
3240 Store_double_field (ret_v
, 2, box
.y0
);
3241 Store_double_field (ret_v
, 3, box
.y1
);
3246 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3247 value dw_v
, value cols_v
)
3249 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3255 double winw
= Int_val (winw_v
);
3256 double winh
= Int_val (winh_v
);
3257 double dw
= Int_val (dw_v
);
3258 double cols
= Int_val (cols_v
);
3259 double pw
= 1.0, ph
= 1.0;
3261 if (trylock ("ml_zoom_for_height")) {
3265 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3266 double w
= p
->pagebox
.x1
/ cols
;
3267 double h
= p
->pagebox
.y1
;
3271 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3273 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3276 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3277 unlock ("ml_zoom_for_height");
3279 ret_v
= caml_copy_double (zoom
);
3283 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3285 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3287 int pt
= Int_val(pt_v
);
3288 int x
= Int_val (x_v
);
3289 int y
= Int_val (y_v
);
3292 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3293 ret_v
= caml_copy_double (w
);
3297 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3299 CAMLparam2 (pt_v
, string_v
);
3301 int pt
= Int_val (pt_v
);
3304 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3305 ret_v
= caml_copy_double (w
);
3309 CAMLprim value
ml_getpagebox (value opaque_v
)
3311 CAMLparam1 (opaque_v
);
3317 char *s
= String_val (opaque_v
);
3318 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3320 ret_v
= caml_alloc_tuple (4);
3321 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3322 dev
->hints
|= FZ_IGNORE_SHADE
;
3324 switch (page
->type
) {
3326 ctm
= pagectm (page
);
3327 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3331 ctm
= pagectm (page
);
3332 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3336 rect
= fz_infinite_rect
;
3340 fz_free_device (dev
);
3341 fz_round_rect (&bbox
, &rect
);
3342 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3343 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3344 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3345 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3350 CAMLprim value
ml_setaalevel (value level_v
)
3352 CAMLparam1 (level_v
);
3354 state
.aalevel
= Int_val (level_v
);
3355 CAMLreturn (Val_unit
);
3359 #include <X11/Xlib.h>
3365 GLXDrawable drawable
;
3368 #include "keysym2ucs.c"
3370 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3372 CAMLparam1 (keysym_v
);
3374 KeySym keysym
= Int_val (keysym_v
);
3379 rune
= keysym2ucs (keysym
);
3380 len
= fz_runetochar (buf
, rune
);
3382 str_v
= caml_copy_string (buf
);
3386 CAMLprim value
ml_glx (value win_v
)
3389 XVisualInfo
*visual
;
3390 int screen
, wid
= Int_val (win_v
);
3391 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3393 glx
.dpy
= XOpenDisplay (NULL
);
3395 caml_failwith ("XOpenDisplay");
3398 screen
= DefaultScreen (glx
.dpy
);
3399 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3401 XCloseDisplay (glx
.dpy
);
3403 caml_failwith ("glXChooseVisual");
3406 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3409 XCloseDisplay (glx
.dpy
);
3411 caml_failwith ("glXCreateContext");
3414 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3415 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3416 XCloseDisplay (glx
.dpy
);
3419 caml_failwith ("glXMakeCurrent");
3422 CAMLreturn (Val_unit
);
3425 CAMLprim value
ml_swapb (value unit_v
)
3427 CAMLparam1 (unit_v
);
3428 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3429 CAMLreturn (Val_unit
);
3432 CAMLprim value
ml_glxsync (value unit_v
)
3434 CAMLparam1 (unit_v
);
3435 if (glx
.dpy
&& glx
.ctx
) {
3439 CAMLreturn (Val_unit
);
3442 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3443 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3445 CAMLprim value
ml_platform (value unit_v
)
3447 CAMLparam1 (unit_v
);
3448 int platid
= piunknown
;
3450 #if defined __linux__
3452 #elif defined __CYGWIN__
3454 #elif defined __DragonFly__
3455 platid
= pidragonflybsd
;
3456 #elif defined __FreeBSD__
3458 #elif defined __OpenBSD__
3460 #elif defined __NetBSD__
3462 #elif defined __sun__
3464 #elif defined __APPLE__
3467 CAMLreturn (Val_int (platid
));
3470 CAMLprim value
ml_cloexec (value fd_v
)
3473 int fd
= Int_val (fd_v
);
3475 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3476 uerror ("fcntl", Nothing
);
3478 CAMLreturn (Val_unit
);
3481 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3483 CAMLparam2 (w_v
, h_v
);
3486 int w
= Int_val (w_v
);
3487 int h
= Int_val (h_v
);
3488 int cs
= Int_val (cs_v
);
3490 if (state
.pbo_usable
) {
3491 pbo
= calloc (sizeof (*pbo
), 1);
3493 err (1, "calloc pbo");
3505 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3508 state
.glGenBuffersARB (1, &pbo
->id
);
3509 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3510 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3511 NULL
, GL_STREAM_DRAW
);
3512 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3514 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3516 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3517 state
.glDeleteBuffersARB (1, &pbo
->id
);
3519 ret_v
= caml_copy_string ("0");
3525 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3527 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3531 err (1, "malloc %d bytes failed", res
+1);
3533 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3535 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3537 ret_v
= caml_copy_string (s
);
3542 ret_v
= caml_copy_string ("0");
3547 CAMLprim value
ml_freepbo (value s_v
)
3550 char *s
= String_val (s_v
);
3551 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3554 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3556 tile
->pbo
->ptr
= NULL
;
3557 tile
->pbo
->size
= -1;
3559 CAMLreturn (Val_unit
);
3562 CAMLprim value
ml_unmappbo (value s_v
)
3565 char *s
= String_val (s_v
);
3566 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3569 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3570 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3571 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3573 tile
->pbo
->ptr
= NULL
;
3574 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3576 CAMLreturn (Val_unit
);
3579 static void setuppbo (void)
3581 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3582 GGPA (glBindBufferARB
);
3583 if (state
.glBindBufferARB
) {
3584 GGPA (glUnmapBufferARB
);
3585 if (state
.glUnmapBufferARB
) {
3586 GGPA (glMapBufferARB
);
3587 if (state
.glMapBufferARB
) {
3588 GGPA (glBufferDataARB
);
3589 if (state
.glBufferDataARB
) {
3590 GGPA (glGenBuffersARB
);
3591 if (state
.glGenBuffersARB
) {
3592 GGPA (glDeleteBuffersARB
);
3593 if (state
.glDeleteBuffersARB
) {
3594 state
.pbo_usable
= 1;
3604 CAMLprim value
ml_pbo_usable (value unit_v
)
3606 CAMLparam1 (unit_v
);
3607 CAMLreturn (Val_bool (state
.pbo_usable
));
3610 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3612 CAMLparam3 (ptr_v
, x_v
, y_v
);
3613 CAMLlocal2 (ret_v
, tup_v
);
3615 char *s
= String_val (ptr_v
);
3616 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3617 struct pagedim
*pdim
;
3621 page
= parse_pointer ("ml_unproject", s
);
3622 pdim
= &state
.pagedims
[page
->pdimno
];
3624 ret_v
= Val_int (0);
3625 if (trylock ("ml_unproject")) {
3629 switch (page
->type
) {
3631 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3640 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3641 fz_invert_matrix (&ctm
, &ctm
);
3642 fz_transform_point (&p
, &ctm
);
3644 tup_v
= caml_alloc_tuple (2);
3645 ret_v
= caml_alloc_small (1, 1);
3646 Field (tup_v
, 0) = Val_int (p
.x
);
3647 Field (tup_v
, 1) = Val_int (p
.y
);
3648 Field (ret_v
, 0) = tup_v
;
3650 unlock ("ml_unproject");
3655 static void makestippletex (void)
3657 const char *pixels
= "\xff\xff\0\0";
3658 glGenTextures (1, &state
.stid
);
3659 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3660 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3661 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3674 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3676 CAMLparam2 (pipe_v
, params_v
);
3677 CAMLlocal2 (trim_v
, fuzz_v
);
3684 struct sigaction sa
;
3686 state
.cr
= Int_val (Field (pipe_v
, 0));
3687 state
.cw
= Int_val (Field (pipe_v
, 1));
3688 state
.rotate
= Int_val (Field (params_v
, 0));
3689 state
.fitmodel
= Int_val (Field (params_v
, 1));
3690 trim_v
= Field (params_v
, 2);
3691 texcount
= Int_val (Field (params_v
, 3));
3692 state
.sliceheight
= Int_val (Field (params_v
, 4));
3693 mustoresize
= Int_val (Field (params_v
, 5));
3694 colorspace
= Int_val (Field (params_v
, 6));
3695 fontpath
= String_val (Field (params_v
, 7));
3696 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3697 if (!state
.trimcachepath
) {
3698 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3701 haspboext
= Bool_val (Field (params_v
, 9));
3703 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3705 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3706 fuzz_v
= Field (trim_v
, 1);
3707 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3708 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3709 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3710 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3712 set_tex_params (colorspace
);
3715 state
.face
= load_font (fontpath
);
3719 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3721 state
.face
= load_builtin_font (base
, len
);
3723 if (!state
.face
) _exit (1);
3725 realloctexts (texcount
);
3733 sa
.sa_handler
= SIG_IGN
;
3734 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3736 sa
.sa_handler
= SIG_DFL
;
3737 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3739 if (sigemptyset (&sa
.sa_mask
)) {
3740 err (1, "sigemptyset");
3742 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3743 err (1, "sigaction");
3746 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3748 errx (1, "pthread_create: %s", strerror (ret
));
3751 CAMLreturn (Val_unit
);