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
,
924 fz_free_device (dev
);
926 rect
.x0
+= state
.trimfuzz
.x0
;
927 rect
.x1
+= state
.trimfuzz
.x1
;
928 rect
.y0
+= state
.trimfuzz
.y0
;
929 rect
.y1
+= state
.trimfuzz
.y1
;
930 fz_transform_rect (&rect
, &ctm
);
931 fz_intersect_rect (&rect
, &page
->mediabox
);
933 if (fz_is_empty_rect (&rect
)) {
934 mediabox
= page
->mediabox
;
940 obj
= pdf_new_array (state
.ctx
, 4);
942 pdf_new_real (state
.ctx
, mediabox
.x0
));
944 pdf_new_real (state
.ctx
, mediabox
.y0
));
946 pdf_new_real (state
.ctx
, mediabox
.x1
));
948 pdf_new_real (state
.ctx
, mediabox
.y1
));
949 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
952 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
953 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
954 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
955 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
958 rotate
= page
->rotate
;
959 pdf_free_page (state
.u
.pdf
, page
);
961 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
963 printd ("progress %f Trimming %d",
964 (double) (pageno
+ 1) / state
.pagecount
,
968 fz_catch (state
.ctx
) {
969 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
975 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
977 if (fz_is_empty_rect (&mediabox
)) {
978 fprintf (stderr
, "cannot find page size for page %d\n",
986 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
988 if (!fz_is_empty_rect (&cropbox
)) {
989 fz_intersect_rect (&mediabox
, &cropbox
);
991 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1000 fz_try (state
.ctx
) {
1001 page
= xps_load_page (state
.u
.xps
, pageno
);
1002 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1004 if (state
.trimmargins
) {
1008 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1009 dev
->hints
|= FZ_IGNORE_SHADE
;
1010 xps_run_page (state
.u
.xps
, page
, dev
,
1011 &fz_identity
, NULL
);
1012 fz_free_device (dev
);
1014 rect
.x0
+= state
.trimfuzz
.x0
;
1015 rect
.x1
+= state
.trimfuzz
.x1
;
1016 rect
.y0
+= state
.trimfuzz
.y0
;
1017 rect
.y1
+= state
.trimfuzz
.y1
;
1018 fz_intersect_rect (&rect
, &mediabox
);
1020 if (!fz_is_empty_rect (&rect
)) {
1024 xps_free_page (state
.u
.xps
, page
);
1025 printd ("progress %f loading %d",
1026 (double) (pageno
+ 1) / state
.pagecount
,
1029 fz_catch (state
.ctx
) {
1037 if (state
.trimmargins
&& trimw
) {
1040 fz_try (state
.ctx
) {
1041 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1042 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1043 cbz_free_page (state
.u
.cbz
, page
);
1044 printd ("progress %f Trimming %d",
1045 (double) (pageno
+ 1) / state
.pagecount
,
1048 fz_catch (state
.ctx
) {
1049 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1056 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1058 err (1, "fwrite trim mediabox");
1064 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1066 err (1, "fread trim mediabox");
1070 mediabox
.x0
= mediabox
.y0
= 0;
1079 ARSERT (0 && state
.type
);
1082 if (state
.pagedimcount
== 0
1083 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1084 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1087 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1088 state
.pagedims
= realloc (state
.pagedims
, size
);
1089 if (!state
.pagedims
) {
1090 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1091 size
, state
.pagedimcount
+ 1);
1094 p
= &state
.pagedims
[state
.pagedimcount
++];
1096 p
->mediabox
= mediabox
;
1101 if (state
.trimmargins
) {
1102 printd ("progress 1 Trimmed %d pages in %f seconds",
1103 state
.pagecount
, end
- start
);
1106 printd ("vmsg Processed %d pages in %f seconds",
1107 state
.pagecount
, end
- start
);
1111 if (fclose (trimf
)) {
1117 static void layout (void)
1122 struct pagedim
*p
= p
;
1123 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1125 if (state
.pagedimcount
== 0) return;
1127 switch (state
.fitmodel
) {
1128 case FitProportional
:
1129 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1132 p
= &state
.pagedims
[pindex
];
1133 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1135 fz_transform_rect (&box
, &rm
);
1137 x0
= MIN (box
.x0
, box
.x1
);
1138 x1
= MAX (box
.x0
, box
.x1
);
1141 maxw
= MAX (w
, maxw
);
1142 zoom
= state
.w
/ maxw
;
1154 ARSERT (0 && state
.fitmodel
);
1157 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1161 p
= &state
.pagedims
[pindex
];
1162 fz_rotate (&ctm
, state
.rotate
);
1163 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1165 fz_transform_rect (&box
, &rm
);
1166 w
= box
.x1
- box
.x0
;
1167 switch (state
.fitmodel
) {
1168 case FitProportional
:
1169 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1175 h
= box
.y1
- box
.y0
;
1177 zoom
= MIN (zw
, zh
);
1178 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1187 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1188 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1190 fz_rotate (&rm
, p
->rotate
);
1191 p
->pagebox
= p
->mediabox
;
1192 fz_transform_rect (&p
->pagebox
, &rm
);
1193 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1194 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1198 fz_transform_rect (&rect
, &ctm
);
1199 fz_round_rect (&p
->bounds
, &rect
);
1202 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1203 fz_scale (&sm
, zoom
, -zoom
);
1204 fz_concat (&ctm
, &tm
, &sm
);
1205 fz_concat (&p
->lctm
, &ctm
, &rm
);
1211 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1212 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1213 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1214 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1218 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1219 } while (p
-- != state
.pagedims
);
1223 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1224 desttoanchor (fz_link_dest
*dest
)
1228 struct pagedim
*pdim
= state
.pagedims
;
1233 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1234 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1236 pdim
= &state
.pagedims
[i
];
1238 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1240 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1241 p
.x
= dest
->ld
.gotor
.lt
.x
;
1242 p
.y
= dest
->ld
.gotor
.lt
.y
;
1243 fz_transform_point (&p
, &pdim
->lctm
);
1247 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1248 double x0
, x1
, y0
, y1
;
1250 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1251 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1253 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1254 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1256 a
.n
= dest
->ld
.gotor
.page
;
1261 static void recurse_outline (fz_outline
*outline
, int level
)
1264 struct anchor a
= desttoanchor (&outline
->dest
);
1267 printd ("o %d %d %d %d %s",
1268 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1270 if (outline
->down
) {
1271 recurse_outline (outline
->down
, level
+ 1);
1273 outline
= outline
->next
;
1277 static void process_outline (void)
1279 fz_outline
*outline
;
1281 if (!state
.needoutline
) return;
1283 state
.needoutline
= 0;
1284 switch (state
.type
) {
1286 outline
= pdf_load_outline (state
.u
.pdf
);
1289 outline
= xps_load_outline (state
.u
.xps
);
1296 recurse_outline (outline
, 0);
1297 fz_free_outline (state
.ctx
, outline
);
1301 static char *strofspan (fz_text_span
*span
)
1306 size_t size
= 0, cap
= 80;
1308 p
= malloc (cap
+ 1);
1309 if (!p
) return NULL
;
1311 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1312 int n
= fz_runetochar (utf8
, ch
->c
);
1313 if (size
+ n
> cap
) {
1315 p
= realloc (p
, cap
+ 1);
1316 if (!p
) return NULL
;
1319 memcpy (p
+ size
, utf8
, n
);
1326 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1327 fz_text_span
*span
, fz_matrix ctm
,
1328 int stop
, int pageno
, double start
)
1335 fz_point p1
, p2
, p3
, p4
;
1337 p
= strofspan (span
);
1340 ret
= regexec (re
, p
, 1, &rm
, 0);
1343 if (ret
!= REG_NOMATCH
) {
1346 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1347 printd ("msg regexec error `%.*s'",
1348 (int) size
, errbuf
);
1356 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1357 c
+= fz_runelen (span
->text
[a
].c
);
1359 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1360 c
+= fz_runelen (span
->text
[b
].c
);
1363 if (fz_runelen (span
->text
[b
].c
) > 1) {
1367 fz_text_char_bbox (&sb
, span
, a
);
1368 fz_text_char_bbox (&eb
, span
, b
);
1380 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1387 printd ("progress 1 found at %d `%.*s' in %f sec",
1388 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1392 printd ("match %d %d %f %f %f %f %f %f %f %f",
1404 static int compareblocks (const void *l
, const void *r
)
1406 fz_text_block
const *ls
= l
;
1407 fz_text_block
const* rs
= r
;
1408 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1411 /* wishful thinking function */
1412 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1417 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1419 fz_text_sheet
*sheet
;
1420 struct pagedim
*pdim
, *pdimprev
;
1421 int stop
= 0, niters
= 0;
1424 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1428 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1429 if (niters
++ == 5) {
1432 printd ("progress 1 attention requested aborting search at %d",
1437 printd ("progress %f searching in page %d",
1438 (double) (pageno
+ 1) / state
.pagecount
,
1443 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1444 pdim
= &state
.pagedims
[i
];
1445 if (pdim
->pageno
== pageno
) {
1448 if (pdim
->pageno
> pageno
) {
1457 sheet
= fz_new_text_sheet (state
.ctx
);
1458 text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
1459 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1461 switch (state
.type
) {
1464 fz_try (state
.ctx
) {
1465 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1466 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1467 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1468 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1470 fz_catch (state
.ctx
) {
1471 fz_free_device (tdev
);
1478 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1479 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1483 ARSERT (0 && state
.type
);
1486 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1487 fz_free_device (tdev
);
1489 for (j
= 0; j
< text
->len
; ++j
) {
1492 fz_text_block
*block
;
1494 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1495 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1498 for (k
= 0; k
< block
->len
; ++k
) {
1503 line
= &block
->lines
[k
];
1504 if (line
->bbox
.y0
< y
+ 1) continue;
1507 line
= &block
->lines
[block
->len
- 1 - k
];
1508 if (line
->bbox
.y0
> y
- 1) continue;
1511 for (span
= line
->first_span
; span
; span
= span
->next
) {
1512 switch (matchspan (re
, text
, span
, ctm
,
1513 stop
, pageno
, start
)) {
1515 case 1: stop
= 1; break;
1516 case -1: stop
= 1; goto endloop
;
1531 fz_free_text_page (state
.ctx
, text
);
1532 fz_free_text_sheet (state
.ctx
, sheet
);
1534 state
.freepage (u
.ptr
);
1539 printd ("progress 1 no matches %f sec", end
- start
);
1541 printd ("clearrects");
1544 static void set_tex_params (int colorspace
)
1551 switch (colorspace
) {
1553 state
.texiform
= GL_RGBA8
;
1554 state
.texform
= GL_RGBA
;
1555 state
.texty
= GL_UNSIGNED_BYTE
;
1556 state
.colorspace
= fz_device_rgb (state
.ctx
);
1559 state
.texiform
= GL_RGBA8
;
1560 state
.texform
= GL_BGRA
;
1561 state
.texty
= endianness
.s
> 1
1562 ? GL_UNSIGNED_INT_8_8_8_8
1563 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1564 state
.colorspace
= fz_device_bgr (state
.ctx
);
1567 state
.texiform
= GL_LUMINANCE_ALPHA
;
1568 state
.texform
= GL_LUMINANCE_ALPHA
;
1569 state
.texty
= GL_UNSIGNED_BYTE
;
1570 state
.colorspace
= fz_device_gray (state
.ctx
);
1573 errx (1, "invalid colorspce %d", colorspace
);
1577 static void realloctexts (int texcount
)
1581 if (texcount
== state
.texcount
) return;
1583 if (texcount
< state
.texcount
) {
1584 glDeleteTextures (state
.texcount
- texcount
,
1585 state
.texids
+ texcount
);
1588 size
= texcount
* sizeof (*state
.texids
);
1589 state
.texids
= realloc (state
.texids
, size
);
1590 if (!state
.texids
) {
1591 err (1, "realloc texids %" FMT_s
, size
);
1594 size
= texcount
* sizeof (*state
.texowners
);
1595 state
.texowners
= realloc (state
.texowners
, size
);
1596 if (!state
.texowners
) {
1597 err (1, "realloc texowners %" FMT_s
, size
);
1599 if (texcount
> state
.texcount
) {
1602 glGenTextures (texcount
- state
.texcount
,
1603 state
.texids
+ state
.texcount
);
1604 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1605 state
.texowners
[i
].w
= -1;
1606 state
.texowners
[i
].slice
= NULL
;
1609 state
.texcount
= texcount
;
1613 static char *mbtoutf8 (char *s
)
1619 len
= mbstowcs (NULL
, s
, strlen (s
));
1624 if (len
== (size_t) -1) {
1629 tmp
= malloc (len
* sizeof (wchar_t));
1634 ret
= mbstowcs (tmp
, s
, len
);
1635 if (ret
== (size_t) -1) {
1641 for (i
= 0; i
< ret
; ++i
) {
1642 len
+= fz_runelen (tmp
[i
]);
1645 p
= r
= malloc (len
+ 1);
1651 for (i
= 0; i
< ret
; ++i
) {
1652 p
+= fz_runetochar (p
, tmp
[i
]);
1659 CAMLprim value
ml_mbtoutf8 (value s_v
)
1665 s
= String_val (s_v
);
1671 ret_v
= caml_copy_string (r
);
1677 static void * mainloop (void *unused
)
1680 int len
, ret
, oldlen
= 0;
1685 errx (1, "readlen returned 0");
1688 if (oldlen
< len
+ 1) {
1689 p
= realloc (p
, len
+ 1);
1691 err (1, "realloc %d failed", len
+ 1);
1698 if (!strncmp ("open", p
, 4)) {
1705 ret
= sscanf (p
+ 5, " %d %n", &wthack
, &off
);
1707 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1710 filename
= p
+ 5 + off
;
1711 filenamelen
= strlen (filename
);
1712 password
= filename
+ filenamelen
+ 1;
1715 openxref (filename
, password
);
1721 utf8filename
= mbtoutf8 (filename
);
1722 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1723 if (utf8filename
!= filename
) {
1724 free (utf8filename
);
1727 state
.needoutline
= 1;
1729 else if (!strncmp ("cs", p
, 2)) {
1732 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1734 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1737 set_tex_params (colorspace
);
1738 for (i
= 0; i
< state
.texcount
; ++i
) {
1739 state
.texowners
[i
].w
= -1;
1740 state
.texowners
[i
].slice
= NULL
;
1744 else if (!strncmp ("freepage", p
, 8)) {
1747 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1749 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1753 else if (!strncmp ("freetile", p
, 8)) {
1756 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1758 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1762 else if (!strncmp ("search", p
, 6)) {
1763 int icase
, pageno
, y
, ret
, len2
, forward
;
1767 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1768 &icase
, &pageno
, &y
, &forward
, &len2
);
1770 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1773 pattern
= p
+ 6 + len2
;
1774 ret
= regcomp (&re
, pattern
,
1775 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1780 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1781 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1784 search (&re
, pageno
, y
, forward
);
1788 else if (!strncmp ("geometry", p
, 8)) {
1792 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1794 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1802 for (i
= 0; i
< state
.texcount
; ++i
) {
1803 state
.texowners
[i
].slice
= NULL
;
1806 state
.fitmodel
= fitmodel
;
1811 unlock ("geometry");
1812 printd ("continue %d", state
.pagecount
);
1814 else if (!strncmp ("reqlayout", p
, 9)) {
1816 int rotate
, fitmodel
, off
, h
;
1819 ret
= sscanf (p
+ 9, " %d %d %d %n",
1820 &rotate
, &fitmodel
, &h
, &off
);
1822 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1825 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1828 state
.rotate
= rotate
;
1829 state
.fitmodel
= fitmodel
;
1834 nameddest
= p
+ 9 + off
;
1835 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
1838 pdf_obj
*needle
, *obj
;
1840 needle
= pdf_new_string (state
.ctx
, nameddest
,
1841 strlen (nameddest
));
1842 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
1844 dest
= pdf_parse_link_dest (state
.u
.pdf
, obj
);
1846 a
= desttoanchor (&dest
);
1848 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
1851 printd ("emsg failed to parse destination `%s'\n",
1856 printd ("emsg destination `%s' not found\n",
1859 pdf_drop_obj (needle
);
1863 unlock ("reqlayout");
1864 printd ("continue %d", state
.pagecount
);
1866 else if (!strncmp ("page", p
, 4)) {
1869 int pageno
, pindex
, ret
;
1871 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1873 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1878 page
= loadpage (pageno
, pindex
);
1882 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1884 else if (!strncmp ("tile", p
, 4)) {
1885 int x
, y
, w
, h
, ret
;
1891 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
1892 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
1894 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1899 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1903 printd ("tile %d %d %" FMT_ptr
" %u %f",
1905 FMT_ptr_cast2 (tile
),
1906 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1909 else if (!strncmp ("settrim", p
, 7)) {
1913 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1914 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1916 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1920 state
.trimmargins
= trimmargins
;
1921 state
.needoutline
= 1;
1922 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1924 state
.trimfuzz
= fuzz
;
1926 state
.pagedimcount
= 0;
1927 free (state
.pagedims
);
1928 state
.pagedims
= NULL
;
1933 printd ("continue %d", state
.pagecount
);
1935 else if (!strncmp ("sliceh", p
, 6)) {
1938 ret
= sscanf (p
+ 6, " %d", &h
);
1940 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1942 if (h
!= state
.sliceheight
) {
1945 state
.sliceheight
= h
;
1946 for (i
= 0; i
< state
.texcount
; ++i
) {
1947 state
.texowners
[i
].w
= -1;
1948 state
.texowners
[i
].h
= -1;
1949 state
.texowners
[i
].slice
= NULL
;
1953 else if (!strncmp ("interrupt", p
, 9)) {
1954 printd ("vmsg interrupted");
1957 errx (1, "unknown command %.*s", len
, p
);
1963 CAMLprim value
ml_realloctexts (value texcount_v
)
1965 CAMLparam1 (texcount_v
);
1968 if (trylock ("ml_realloctexts")) {
1972 realloctexts (Int_val (texcount_v
));
1974 unlock ("ml_realloctexts");
1977 CAMLreturn (Val_bool (ok
));
1980 static void showsel (struct page
*page
, int ox
, int oy
)
1986 fz_page_block
*pageb
;
1987 fz_text_block
*block
;
1988 struct mark first
, last
;
1990 first
= page
->fmark
;
1993 if (!first
.span
|| !last
.span
) return;
1995 glEnable (GL_BLEND
);
1996 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1997 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1999 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2000 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2001 for (pageb
= page
->text
->blocks
;
2002 pageb
< page
->text
->blocks
+ page
->text
->len
;
2004 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2005 block
= pageb
->u
.text
;
2007 for (line
= block
->lines
;
2008 line
< block
->lines
+ block
->len
;
2011 rect
= fz_empty_rect
;
2013 for (span
= line
->first_span
; span
; span
= span
->next
) {
2015 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2020 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2022 j
= MIN (first
.i
, last
.i
);
2023 k
= MAX (first
.i
, last
.i
);
2025 else if (span
== first
.span
) {
2029 else if (span
== last
.span
) {
2035 for (i
= j
; i
<= k
; ++i
) {
2037 fz_union_rect (&rect
,
2038 fz_text_char_bbox (&bbox
, span
, i
));
2040 fz_round_rect (&bbox
, &rect
);
2041 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2048 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2049 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2050 if (span
== last
.span
) {
2058 glDisable (GL_BLEND
);
2063 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2065 fz_matrix ctm
, tm
, pm
;
2066 fz_link
*link
, *links
;
2068 switch (page
->type
) {
2070 links
= page
->u
.pdfpage
->links
;
2074 links
= page
->u
.xpspage
->links
;
2081 glEnable (GL_TEXTURE_1D
);
2082 glEnable (GL_BLEND
);
2083 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2085 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2086 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2087 fz_translate (&tm
, xoff
, yoff
);
2088 pm
= pagectm (page
);
2089 fz_concat (&ctm
, &pm
, &tm
);
2092 for (link
= links
; link
; link
= link
->next
) {
2093 fz_point p1
, p2
, p3
, p4
;
2095 p1
.x
= link
->rect
.x0
;
2096 p1
.y
= link
->rect
.y0
;
2098 p2
.x
= link
->rect
.x1
;
2099 p2
.y
= link
->rect
.y0
;
2101 p3
.x
= link
->rect
.x1
;
2102 p3
.y
= link
->rect
.y1
;
2104 p4
.x
= link
->rect
.x0
;
2105 p4
.y
= link
->rect
.y1
;
2107 fz_transform_point (&p1
, &ctm
);
2108 fz_transform_point (&p2
, &ctm
);
2109 fz_transform_point (&p3
, &ctm
);
2110 fz_transform_point (&p4
, &ctm
);
2112 switch (link
->dest
.kind
) {
2113 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2114 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2115 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2116 default: glColor3ub (0, 0, 0); break;
2124 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2128 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2131 glVertex2f (p1
.x
, p1
.y
);
2133 glVertex2f (p2
.x
, p2
.y
);
2136 glVertex2f (p2
.x
, p2
.y
);
2138 glVertex2f (p3
.x
, p3
.y
);
2141 glVertex2f (p3
.x
, p3
.y
);
2143 glVertex2f (p4
.x
, p4
.y
);
2146 glVertex2f (p4
.x
, p4
.y
);
2148 glVertex2f (p1
.x
, p1
.y
);
2153 glDisable (GL_BLEND
);
2154 glDisable (GL_TEXTURE_1D
);
2157 static int compareslinks (const void *l
, const void *r
)
2159 struct slink
const *ls
= l
;
2160 struct slink
const *rs
= r
;
2161 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2162 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2164 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2167 static void droptext (struct page
*page
)
2170 fz_free_text_page (state
.ctx
, page
->text
);
2173 page
->fmark
.span
= NULL
;
2174 page
->lmark
.span
= NULL
;
2178 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2183 static void dropslinks (struct page
*page
)
2186 free (page
->slinks
);
2187 page
->slinks
= NULL
;
2188 page
->slinkcount
= 0;
2192 static void ensureslinks (struct page
*page
)
2196 size_t slinksize
= sizeof (*page
->slinks
);
2197 fz_link
*link
, *links
;
2199 if (state
.gen
!= page
->sgen
) {
2201 page
->sgen
= state
.gen
;
2203 if (page
->slinks
) return;
2205 switch (page
->type
) {
2207 links
= page
->u
.pdfpage
->links
;
2208 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2210 &state
.pagedims
[page
->pdimno
].tctm
,
2211 &state
.pagedims
[page
->pdimno
].ctm
);
2215 links
= page
->u
.xpspage
->links
;
2216 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2223 for (link
= links
; link
; link
= link
->next
) {
2227 page
->slinkcount
= count
;
2228 page
->slinks
= calloc (count
, slinksize
);
2229 if (!page
->slinks
) {
2230 err (1, "realloc slinks %d", count
);
2233 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2237 fz_transform_rect (&rect
, &ctm
);
2238 page
->slinks
[i
].link
= link
;
2239 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2241 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2245 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2246 static void fmt_linkn (char *s
, unsigned int u
)
2248 unsigned int len
; unsigned int q
;
2249 int zma
= 'z' - 'a' + 1;
2251 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2254 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2255 /* handles u == 0 */
2260 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2261 int noff
, char *targ
, int tlen
, int hfsize
)
2265 struct slink
*slink
;
2266 double x0
, y0
, x1
, y1
, w
;
2268 ensureslinks (page
);
2269 glColor3ub (0xc3, 0xb0, 0x91);
2270 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2271 fmt_linkn (buf
, i
+ noff
);
2272 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2273 slink
= &page
->slinks
[i
];
2275 x0
= slink
->bbox
.x0
+ xoff
- 5;
2276 y1
= slink
->bbox
.y0
+ yoff
- 5;
2277 y0
= y1
+ 10 + hfsize
;
2278 w
= measure_string (state
.face
, hfsize
, buf
);
2280 glRectd (x0
, y0
, x1
, y1
);
2284 glEnable (GL_BLEND
);
2285 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2286 glEnable (GL_TEXTURE_2D
);
2287 glColor3ub (0, 0, 0);
2288 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2289 fmt_linkn (buf
, i
+ noff
);
2290 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2291 slink
= &page
->slinks
[i
];
2293 x0
= slink
->bbox
.x0
+ xoff
;
2294 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2295 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2298 glDisable (GL_TEXTURE_2D
);
2299 glDisable (GL_BLEND
);
2303 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2306 struct slice
*slice1
;
2307 unsigned char *texdata
;
2310 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2311 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2313 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2314 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2315 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2319 int texindex
= state
.texindex
++ % state
.texcount
;
2321 if (state
.texowners
[texindex
].w
== tile
->w
) {
2322 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2326 state
.texowners
[texindex
].h
= slice
->h
;
2330 state
.texowners
[texindex
].h
= slice
->h
;
2333 state
.texowners
[texindex
].w
= tile
->w
;
2334 state
.texowners
[texindex
].slice
= slice
;
2335 slice
->texindex
= texindex
;
2337 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2339 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2343 texdata
= tile
->pixmap
->samples
;
2346 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2358 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2370 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2375 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2377 CAMLparam2 (args_v
, ptr_v
);
2378 int dispx
= Int_val (Field (args_v
, 0));
2379 int dispy
= Int_val (Field (args_v
, 1));
2380 int dispw
= Int_val (Field (args_v
, 2));
2381 int disph
= Int_val (Field (args_v
, 3));
2382 int tilex
= Int_val (Field (args_v
, 4));
2383 int tiley
= Int_val (Field (args_v
, 5));
2384 char *s
= String_val (ptr_v
);
2385 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2387 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2389 int slicey
, firstslice
;
2390 struct slice
*slice
;
2392 firstslice
= tiley
/ tile
->sliceheight
;
2393 slice
= &tile
->slices
[firstslice
];
2394 slicey
= tiley
% tile
->sliceheight
;
2399 dh
= slice
->h
- slicey
;
2400 dh
= MIN (disph
, dh
);
2401 uploadslice (tile
, slice
);
2405 glTexCoord2i (tilex
, slicey
);
2406 glVertex2i (dispx
, dispy
);
2408 glTexCoord2i (tilex
+dispw
, slicey
);
2409 glVertex2i (dispx
+dispw
, dispy
);
2411 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
2412 glVertex2i (dispx
+dispw
, dispy
+dh
);
2414 glTexCoord2i (tilex
, slicey
+dh
);
2415 glVertex2i (dispx
, dispy
+dh
);
2422 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2426 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2427 CAMLreturn (Val_unit
);
2430 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2431 value xoff_v
, value yoff_v
,
2434 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2435 int xoff
= Int_val (xoff_v
);
2436 int yoff
= Int_val (yoff_v
);
2437 int noff
= Int_val (Field (li_v
, 0));
2438 char *targ
= String_val (Field (li_v
, 1));
2439 int tlen
= caml_string_length (Field (li_v
, 1));
2440 int hfsize
= Int_val (Field (li_v
, 2));
2441 char *s
= String_val (ptr_v
);
2442 int hlmask
= Int_val (hlinks_v
);
2443 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2446 /* deal with loadpage failed pages */
2450 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2451 if (trylock ("ml_postprocess")) {
2456 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2457 noff
= page
->slinkcount
;
2459 if (page
->tgen
== state
.gen
) {
2460 showsel (page
, xoff
, yoff
);
2462 unlock ("ml_postprocess");
2465 CAMLreturn (Val_int (noff
));
2468 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2472 const fz_matrix
*tctm
;
2473 fz_link
*link
, *links
;
2475 switch (page
->type
) {
2477 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2478 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2479 links
= page
->u
.pdfpage
->links
;
2483 tctm
= &fz_identity
;
2484 links
= page
->u
.xpspage
->links
;
2493 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2494 fz_invert_matrix (&ctm
, &ctm
);
2495 fz_transform_point (&p
, &ctm
);
2497 for (link
= links
; link
; link
= link
->next
) {
2498 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2499 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2507 static void ensuretext (struct page
*page
)
2509 if (state
.gen
!= page
->tgen
) {
2511 page
->tgen
= state
.gen
;
2517 page
->text
= fz_new_text_page (state
.ctx
, &fz_infinite_rect
);
2518 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2519 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2520 ctm
= pagectm (page
);
2521 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2522 qsort (page
->text
->blocks
, page
->text
->len
,
2523 sizeof (*page
->text
->blocks
), compareblocks
);
2524 fz_free_device (tdev
);
2528 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2530 CAMLparam2 (start_page_v
, dir_v
);
2532 int i
, dir
= Int_val (dir_v
);
2533 int start_page
= Int_val (start_page_v
);
2534 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2536 ret_v
= Val_int (0);
2537 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2541 lock ("ml_findpage_with_links");
2542 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2545 switch (state
.type
) {
2548 pdf_page
*page
= NULL
;
2550 fz_try (state
.ctx
) {
2551 page
= pdf_load_page (state
.u
.pdf
, i
);
2552 found
= !!page
->links
;
2554 fz_catch (state
.ctx
) {
2564 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2565 found
= !!page
->links
;
2571 ARSERT (0 && "invalid document type");
2575 ret_v
= caml_alloc_small (1, 1);
2576 Field (ret_v
, 0) = Val_int (i
);
2581 unlock ("ml_findpage_with_links");
2587 enum { dir_first
, dir_last
};
2588 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2590 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2592 CAMLparam2 (ptr_v
, dir_v
);
2593 CAMLlocal2 (ret_v
, pos_v
);
2595 int dirtag
, i
, slinkindex
;
2596 struct slink
*found
= NULL
,*slink
;
2597 char *s
= String_val (ptr_v
);
2599 page
= parse_pointer ("ml_findlink", s
);
2600 ret_v
= Val_int (0);
2601 if (trylock ("ml_findlink")) {
2605 ensureslinks (page
);
2607 if (Is_block (dir_v
)) {
2608 dirtag
= Tag_val (dir_v
);
2610 case dir_first_visible
:
2612 int x0
, y0
, dir
, first_index
, last_index
;
2614 pos_v
= Field (dir_v
, 0);
2615 x0
= Int_val (Field (pos_v
, 0));
2616 y0
= Int_val (Field (pos_v
, 1));
2617 dir
= Int_val (Field (pos_v
, 2));
2622 last_index
= page
->slinkcount
;
2625 first_index
= page
->slinkcount
- 1;
2629 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2630 slink
= &page
->slinks
[i
];
2631 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2640 slinkindex
= Int_val (Field (dir_v
, 0));
2641 found
= &page
->slinks
[slinkindex
];
2642 for (i
= slinkindex
- 1; i
>= 0; --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
.x0
> found
->bbox
.x0
) {
2664 slinkindex
= Int_val (Field (dir_v
, 0));
2665 found
= &page
->slinks
[slinkindex
];
2666 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2667 slink
= &page
->slinks
[i
];
2668 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2676 slinkindex
= Int_val (Field (dir_v
, 0));
2677 found
= &page
->slinks
[slinkindex
];
2678 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2679 slink
= &page
->slinks
[i
];
2680 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2689 dirtag
= Int_val (dir_v
);
2692 found
= page
->slinks
;
2697 found
= page
->slinks
+ (page
->slinkcount
- 1);
2703 ret_v
= caml_alloc_small (2, 1);
2704 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2707 unlock ("ml_findlink");
2712 enum { uuri
, ugoto
, utext
, uunexpected
, ulaunch
, unamed
, uremote
};
2718 switch (link->dest.kind) { \
2719 case FZ_LINK_GOTO: \
2723 pageno = link->dest.ld.gotor.page; \
2727 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2728 p.y = link->dest.ld.gotor.lt.y; \
2729 fz_transform_point (&p, &pdim->lctm); \
2731 tup_v = caml_alloc_tuple (2); \
2732 ret_v = caml_alloc_small (1, ugoto); \
2733 Field (tup_v, 0) = Val_int (pageno); \
2734 Field (tup_v, 1) = Val_int (p.y); \
2735 Field (ret_v, 0) = tup_v; \
2740 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2741 ret_v = caml_alloc_small (1, uuri); \
2742 Field (ret_v, 0) = str_v; \
2745 case FZ_LINK_LAUNCH: \
2746 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2747 ret_v = caml_alloc_small (1, ulaunch); \
2748 Field (ret_v, 0) = str_v; \
2751 case FZ_LINK_NAMED: \
2752 str_v = caml_copy_string (link->dest.ld.named.named); \
2753 ret_v = caml_alloc_small (1, unamed); \
2754 Field (ret_v, 0) = str_v; \
2757 case FZ_LINK_GOTOR: \
2758 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2759 pageno = link->dest.ld.gotor.page; \
2760 tup_v = caml_alloc_tuple (2); \
2761 ret_v = caml_alloc_small (1, uremote); \
2762 Field (tup_v, 0) = str_v; \
2763 Field (tup_v, 1) = Val_int (pageno); \
2764 Field (ret_v, 0) = tup_v; \
2771 snprintf (buf, sizeof (buf), \
2772 "unhandled link kind %d", link->dest.kind); \
2773 str_v = caml_copy_string (buf); \
2774 ret_v = caml_alloc_small (1, uunexpected); \
2775 Field (ret_v, 0) = str_v; \
2781 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
2783 CAMLparam2 (ptr_v
, n_v
);
2784 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2787 struct pagedim
*pdim
;
2788 char *s
= String_val (ptr_v
);
2790 ret_v
= Val_int (0);
2791 if (trylock ("ml_getlink")) {
2795 page
= parse_pointer ("ml_getlink", s
);
2796 ensureslinks (page
);
2797 pdim
= &state
.pagedims
[page
->pdimno
];
2798 link
= page
->slinks
[Int_val (n_v
)].link
;
2801 unlock ("ml_getlink");
2806 CAMLprim value
ml_getlinkcount (value ptr_v
)
2810 char *s
= String_val (ptr_v
);
2812 page
= parse_pointer ("ml_getlinkcount", s
);
2813 CAMLreturn (Val_int (page
->slinkcount
));
2816 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
2818 CAMLparam2 (ptr_v
, n_v
);
2821 struct slink
*slink
;
2822 char *s
= String_val (ptr_v
);
2824 page
= parse_pointer ("ml_getlinkrect", s
);
2825 ret_v
= caml_alloc_tuple (4);
2826 if (trylock ("ml_getlinkrect")) {
2827 Field (ret_v
, 0) = Val_int (0);
2828 Field (ret_v
, 1) = Val_int (0);
2829 Field (ret_v
, 2) = Val_int (0);
2830 Field (ret_v
, 3) = Val_int (0);
2833 ensureslinks (page
);
2835 slink
= &page
->slinks
[Int_val (n_v
)];
2836 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2837 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2838 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2839 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2840 unlock ("ml_getlinkrect");
2846 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2848 CAMLparam3 (ptr_v
, x_v
, y_v
);
2849 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2852 char *s
= String_val (ptr_v
);
2853 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2854 struct pagedim
*pdim
;
2856 ret_v
= Val_int (0);
2857 if (trylock ("ml_whatsunder")) {
2861 page
= parse_pointer ("ml_whatsunder", s
);
2862 pdim
= &state
.pagedims
[page
->pdimno
];
2863 x
+= pdim
->bounds
.x0
;
2864 y
+= pdim
->bounds
.y0
;
2865 link
= getlink (page
, x
, y
);
2871 fz_page_block
*pageb
;
2872 fz_text_block
*block
;
2875 for (pageb
= page
->text
->blocks
;
2876 pageb
< page
->text
->blocks
+ page
->text
->len
;
2879 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2880 block
= pageb
->u
.text
;
2883 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2886 for (line
= block
->lines
;
2887 line
< block
->lines
+ block
->len
;
2892 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2895 for (span
= line
->first_span
; span
; span
= span
->next
) {
2899 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2902 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
2904 fz_text_char_bbox (&bbox
, span
, charnum
);
2907 if (x
>= b
->x0
&& x
<= b
->x1
2908 && y
>= b
->y0
&& y
<= b
->y1
) {
2909 fz_text_style
*style
= span
->text
->style
;
2911 style
->font
&& style
->font
->name
2913 : "Span has no font name"
2915 FT_FaceRec
*face
= style
->font
->ft_face
;
2916 if (face
&& face
->family_name
) {
2918 char *n1
= face
->family_name
;
2919 size_t l1
= strlen (n1
);
2920 size_t l2
= strlen (n2
);
2922 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2923 s
= malloc (l1
+ l2
+ 2);
2927 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2928 str_v
= caml_copy_string (s
);
2934 str_v
= caml_copy_string (n2
);
2936 ret_v
= caml_alloc_small (1, utext
);
2937 Field (ret_v
, 0) = str_v
;
2946 unlock ("ml_whatsunder");
2952 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2954 static int white (int c
)
2956 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r';
2959 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2961 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2962 CAMLlocal3 (ret_v
, tup_v
, str_v
);
2966 fz_page_block
*pageb
;
2967 fz_text_block
*block
;
2968 struct pagedim
*pdim
;
2969 int mark
= Int_val (mark_v
);
2970 char *s
= String_val (ptr_v
);
2971 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2973 ret_v
= Val_bool (0);
2974 if (trylock ("ml_markunder")) {
2978 page
= parse_pointer ("ml_markunder", s
);
2979 pdim
= &state
.pagedims
[page
->pdimno
];
2980 x
+= pdim
->bounds
.x0
;
2981 y
+= pdim
->bounds
.y0
;
2985 if (mark
== mark_page
) {
2988 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
2989 pageb
= &page
->text
->blocks
[i
];
2990 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2992 if (i
< 0) goto unlock
;
2994 block
= pageb
->u
.text
;
2997 page
->fmark
.span
= block
->lines
->first_span
;
2999 line
= &block
->lines
[block
->len
- 1];
3000 page
->lmark
.i
= line
->last_span
->len
- 1;
3001 page
->lmark
.span
= line
->last_span
;
3002 ret_v
= Val_bool (1);
3006 for (pageb
= page
->text
->blocks
;
3007 pageb
< page
->text
->blocks
+ page
->text
->len
;
3009 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3010 block
= pageb
->u
.text
;
3013 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3016 if (mark
== mark_block
) {
3018 page
->fmark
.span
= block
->lines
->first_span
;
3020 line
= &block
->lines
[block
->len
- 1];
3021 page
->lmark
.i
= line
->last_span
->len
- 1;
3022 page
->lmark
.span
= line
->last_span
;
3023 ret_v
= Val_bool (1);
3027 for (line
= block
->lines
;
3028 line
< block
->lines
+ block
->len
;
3033 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3036 if (mark
== mark_line
) {
3038 page
->fmark
.span
= line
->first_span
;
3040 page
->lmark
.i
= line
->last_span
->len
- 1;
3041 page
->lmark
.span
= line
->last_span
;
3042 ret_v
= Val_bool (1);
3046 for (span
= line
->first_span
; span
; span
= span
->next
) {
3050 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3053 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3055 fz_text_char_bbox (&bbox
, span
, charnum
);
3058 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3060 int charnum2
, charnum3
= -1, charnum4
= -1;
3062 if (white (span
->text
[charnum
].c
)) goto unlock
;
3064 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3065 if (white (span
->text
[charnum2
].c
)) {
3066 charnum3
= charnum2
+ 1;
3070 if (charnum3
== -1) charnum3
= 0;
3072 for (charnum2
= charnum
+ 1;
3073 charnum2
< span
->len
;
3075 if (white (span
->text
[charnum2
].c
)) break;
3076 charnum4
= charnum2
;
3078 if (charnum4
== -1) goto unlock
;
3080 page
->fmark
.i
= charnum3
;
3081 page
->fmark
.span
= span
;
3083 page
->lmark
.i
= charnum4
;
3084 page
->lmark
.span
= span
;
3085 ret_v
= Val_bool (1);
3093 if (!Bool_val (ret_v
)) {
3094 page
->fmark
.span
= NULL
;
3095 page
->lmark
.span
= NULL
;
3099 unlock ("ml_markunder");
3105 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3107 CAMLparam2 (ptr_v
, rect_v
);
3110 struct pagedim
*pdim
;
3111 int i
, x0
, x1
, y0
, y1
;
3112 char *s
= String_val (ptr_v
);
3114 fz_page_block
*pageb
;
3115 fz_text_block
*block
;
3116 fz_text_span
*span
, *fspan
, *lspan
;
3117 fz_text_line
*line
, *fline
= NULL
, *lline
= NULL
;
3119 if (trylock ("ml_seltext")) {
3123 page
= parse_pointer ("ml_seltext", s
);
3126 pdim
= &state
.pagedims
[page
->pdimno
];
3127 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;;
3128 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3129 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3130 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3133 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3134 glColor3ub (128, 128, 128);
3135 glRecti (x0
, y0
, x1
, y1
);
3136 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3139 fspan
= lspan
= NULL
;
3141 for (pageb
= page
->text
->blocks
;
3142 pageb
< page
->text
->blocks
+ page
->text
->len
;
3144 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3145 block
= pageb
->u
.text
;
3146 for (line
= block
->lines
;
3147 line
< block
->lines
+ block
->len
;
3151 for (span
= line
->first_span
; span
; span
= span
->next
) {
3152 for (i
= 0; i
< span
->len
; ++i
) {
3155 fz_text_char_bbox (&b
, span
, i
);
3157 if (x0
>= b
.x0
&& x0
<= b
.x1
3158 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3164 if (x1
>= b
.x0
&& x1
<= b
.x1
3165 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3171 if (0 && selected
) {
3172 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3173 glColor3ub (128, 128, 128);
3174 glRecti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3175 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3181 if (y1
< y0
|| x1
< x0
) {
3184 if (fspan
== lspan
) {
3189 if (fline
!= lline
) {
3208 page
->fmark
.span
= fspan
;
3211 page
->lmark
.span
= lspan
;
3213 unlock ("ml_seltext");
3216 CAMLreturn (Val_unit
);
3219 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3224 for (i
= a
; i
<= b
; ++i
) {
3225 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3226 ret
= fwrite (buf
, len
, 1, f
);
3229 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3230 len
, ret
, strerror (errno
));
3238 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3240 caml_failwith ("ml_popen not implemented under Cygwin");
3243 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3245 CAMLparam2 (command_v
, fds_v
);
3246 CAMLlocal2 (l_v
, tup_v
);
3249 value earg_v
= Nothing
;
3250 posix_spawnattr_t attr
;
3251 posix_spawn_file_actions_t fa
;
3252 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3254 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3255 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3258 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3259 msg
= "posix_spawnattr_init";
3263 #ifdef POSIX_SPAWN_USEVFORK
3264 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3265 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3270 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3273 tup_v
= Field (l_v
, 0);
3274 fd1
= Int_val (Field (tup_v
, 0));
3275 fd2
= Int_val (Field (tup_v
, 1));
3277 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3278 msg
= "posix_spawn_file_actions_addclose";
3284 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3285 msg
= "posix_spawn_file_actions_adddup2";
3292 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3293 msg
= "posix_spawn";
3298 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3299 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3303 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3304 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3309 unix_error (ret
, msg
, earg_v
);
3311 CAMLreturn (Val_unit
);
3315 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
, value clear_v
)
3317 CAMLparam3 (fd_v
, ptr_v
, clear_v
);
3322 fz_page_block
*pageb
;
3323 fz_text_block
*block
;
3324 int fd
= Int_val (fd_v
);
3325 char *s
= String_val (ptr_v
);
3326 int clear
= Bool_val (clear_v
);
3328 if (trylock ("ml_copysel")) {
3332 page
= parse_pointer ("ml_sopysel", s
);
3334 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3335 fprintf (stderr
, "nothing to copy\n");
3339 f
= fdopen (fd
, "w");
3341 fprintf (stderr
, "failed to fopen sel pipe: %s\n",
3346 for (pageb
= page
->text
->blocks
;
3347 pageb
< page
->text
->blocks
+ page
->text
->len
;
3349 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3350 block
= pageb
->u
.text
;
3351 for (line
= block
->lines
;
3352 line
< block
->lines
+ block
->len
;
3356 for (span
= line
->first_span
; span
; span
= span
->next
) {
3359 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3360 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3361 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3364 if (pipespan (f
, span
, a
, b
)) {
3367 if (span
== line
->last_span
) {
3368 if (putc ('\n', f
) == EOF
) {
3370 "failed break line on sel pipe: %s\n",
3375 if (span
== page
->lmark
.span
) {
3384 page
->lmark
.span
= NULL
;
3385 page
->fmark
.span
= NULL
;
3390 int ret
= fclose (f
);
3393 if (errno
!= ECHILD
) {
3394 fprintf (stderr
, "failed to close sel pipe: %s\n",
3400 unlock ("ml_copysel");
3405 fprintf (stderr
, "failed to close sel pipe: %s\n",
3409 CAMLreturn (Val_unit
);
3412 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3414 CAMLparam1 (pagedimno_v
);
3416 int pagedimno
= Int_val (pagedimno_v
);
3419 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3420 if (trylock ("ml_getpdimrect")) {
3421 box
= fz_empty_rect
;
3424 box
= state
.pagedims
[pagedimno
].mediabox
;
3425 unlock ("ml_getpdimrect");
3428 Store_double_field (ret_v
, 0, box
.x0
);
3429 Store_double_field (ret_v
, 1, box
.x1
);
3430 Store_double_field (ret_v
, 2, box
.y0
);
3431 Store_double_field (ret_v
, 3, box
.y1
);
3436 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3437 value dw_v
, value cols_v
)
3439 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3445 double winw
= Int_val (winw_v
);
3446 double winh
= Int_val (winh_v
);
3447 double dw
= Int_val (dw_v
);
3448 double cols
= Int_val (cols_v
);
3449 double pw
= 1.0, ph
= 1.0;
3451 if (trylock ("ml_zoom_for_height")) {
3455 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3456 double w
= p
->pagebox
.x1
/ cols
;
3457 double h
= p
->pagebox
.y1
;
3461 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3463 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3466 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3467 unlock ("ml_zoom_for_height");
3469 ret_v
= caml_copy_double (zoom
);
3473 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3475 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3477 int pt
= Int_val(pt_v
);
3478 int x
= Int_val (x_v
);
3479 int y
= Int_val (y_v
);
3482 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3483 ret_v
= caml_copy_double (w
);
3487 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3489 CAMLparam2 (pt_v
, string_v
);
3491 int pt
= Int_val (pt_v
);
3494 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3495 ret_v
= caml_copy_double (w
);
3499 CAMLprim value
ml_getpagebox (value opaque_v
)
3501 CAMLparam1 (opaque_v
);
3507 char *s
= String_val (opaque_v
);
3508 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3510 ret_v
= caml_alloc_tuple (4);
3511 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3512 dev
->hints
|= FZ_IGNORE_SHADE
;
3514 switch (page
->type
) {
3516 ctm
= pagectm (page
);
3517 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3521 ctm
= pagectm (page
);
3522 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3526 rect
= fz_infinite_rect
;
3530 fz_free_device (dev
);
3531 fz_round_rect (&bbox
, &rect
);
3532 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3533 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3534 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3535 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3540 CAMLprim value
ml_setaalevel (value level_v
)
3542 CAMLparam1 (level_v
);
3544 state
.aalevel
= Int_val (level_v
);
3545 CAMLreturn (Val_unit
);
3549 #include <X11/Xlib.h>
3555 GLXDrawable drawable
;
3558 #include "keysym2ucs.c"
3560 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3562 CAMLparam1 (keysym_v
);
3564 KeySym keysym
= Int_val (keysym_v
);
3569 rune
= keysym2ucs (keysym
);
3570 len
= fz_runetochar (buf
, rune
);
3572 str_v
= caml_copy_string (buf
);
3576 CAMLprim value
ml_glx (value win_v
)
3579 XVisualInfo
*visual
;
3580 int screen
, wid
= Int_val (win_v
);
3581 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3583 glx
.dpy
= XOpenDisplay (NULL
);
3585 caml_failwith ("XOpenDisplay");
3588 screen
= DefaultScreen (glx
.dpy
);
3589 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3591 XCloseDisplay (glx
.dpy
);
3593 caml_failwith ("glXChooseVisual");
3596 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3599 XCloseDisplay (glx
.dpy
);
3601 caml_failwith ("glXCreateContext");
3604 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3605 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3606 XCloseDisplay (glx
.dpy
);
3609 caml_failwith ("glXMakeCurrent");
3612 CAMLreturn (Val_unit
);
3615 CAMLprim value
ml_swapb (value unit_v
)
3617 CAMLparam1 (unit_v
);
3618 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3619 CAMLreturn (Val_unit
);
3622 CAMLprim value
ml_glxsync (value unit_v
)
3624 CAMLparam1 (unit_v
);
3625 if (glx
.dpy
&& glx
.ctx
) {
3629 CAMLreturn (Val_unit
);
3632 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3633 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3635 CAMLprim value
ml_platform (value unit_v
)
3637 CAMLparam1 (unit_v
);
3638 int platid
= piunknown
;
3640 #if defined __linux__
3642 #elif defined __CYGWIN__
3644 #elif defined __DragonFly__
3645 platid
= pidragonflybsd
;
3646 #elif defined __FreeBSD__
3648 #elif defined __OpenBSD__
3650 #elif defined __NetBSD__
3652 #elif defined __sun__
3654 #elif defined __APPLE__
3657 CAMLreturn (Val_int (platid
));
3660 CAMLprim value
ml_cloexec (value fd_v
)
3663 int fd
= Int_val (fd_v
);
3665 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3666 uerror ("fcntl", Nothing
);
3668 CAMLreturn (Val_unit
);
3671 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3673 CAMLparam2 (w_v
, h_v
);
3676 int w
= Int_val (w_v
);
3677 int h
= Int_val (h_v
);
3678 int cs
= Int_val (cs_v
);
3680 if (state
.pbo_usable
) {
3681 pbo
= calloc (sizeof (*pbo
), 1);
3683 err (1, "calloc pbo");
3695 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
3698 state
.glGenBuffersARB (1, &pbo
->id
);
3699 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3700 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
3701 NULL
, GL_STREAM_DRAW
);
3702 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3704 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3706 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
3707 state
.glDeleteBuffersARB (1, &pbo
->id
);
3709 ret_v
= caml_copy_string ("0");
3715 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
3717 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
3721 err (1, "malloc %d bytes failed", res
+1);
3723 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
3725 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
3727 ret_v
= caml_copy_string (s
);
3732 ret_v
= caml_copy_string ("0");
3737 CAMLprim value
ml_freepbo (value s_v
)
3740 char *s
= String_val (s_v
);
3741 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
3744 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3746 tile
->pbo
->ptr
= NULL
;
3747 tile
->pbo
->size
= -1;
3749 CAMLreturn (Val_unit
);
3752 CAMLprim value
ml_unmappbo (value s_v
)
3755 char *s
= String_val (s_v
);
3756 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
3759 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3760 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3761 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3763 tile
->pbo
->ptr
= NULL
;
3764 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3766 CAMLreturn (Val_unit
);
3769 static void setuppbo (void)
3771 #define GGPA(n) *(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n)
3772 GGPA (glBindBufferARB
);
3773 if (state
.glBindBufferARB
) {
3774 GGPA (glUnmapBufferARB
);
3775 if (state
.glUnmapBufferARB
) {
3776 GGPA (glMapBufferARB
);
3777 if (state
.glMapBufferARB
) {
3778 GGPA (glBufferDataARB
);
3779 if (state
.glBufferDataARB
) {
3780 GGPA (glGenBuffersARB
);
3781 if (state
.glGenBuffersARB
) {
3782 GGPA (glDeleteBuffersARB
);
3783 if (state
.glDeleteBuffersARB
) {
3784 state
.pbo_usable
= 1;
3794 CAMLprim value
ml_pbo_usable (value unit_v
)
3796 CAMLparam1 (unit_v
);
3797 CAMLreturn (Val_bool (state
.pbo_usable
));
3800 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3802 CAMLparam3 (ptr_v
, x_v
, y_v
);
3803 CAMLlocal2 (ret_v
, tup_v
);
3805 char *s
= String_val (ptr_v
);
3806 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3807 struct pagedim
*pdim
;
3811 page
= parse_pointer ("ml_unproject", s
);
3812 pdim
= &state
.pagedims
[page
->pdimno
];
3814 ret_v
= Val_int (0);
3815 if (trylock ("ml_unproject")) {
3819 switch (page
->type
) {
3821 trimctm (page
->u
.pdfpage
, page
->pdimno
);
3830 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
3831 fz_invert_matrix (&ctm
, &ctm
);
3832 fz_transform_point (&p
, &ctm
);
3834 tup_v
= caml_alloc_tuple (2);
3835 ret_v
= caml_alloc_small (1, 1);
3836 Field (tup_v
, 0) = Val_int (p
.x
);
3837 Field (tup_v
, 1) = Val_int (p
.y
);
3838 Field (ret_v
, 0) = tup_v
;
3840 unlock ("ml_unproject");
3845 static void makestippletex (void)
3847 const char pixels
[] = "\xff\xff\0\0";
3848 glGenTextures (1, &state
.stid
);
3849 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3850 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3851 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3864 CAMLprim value
ml_init (value pipe_v
, value params_v
)
3866 CAMLparam2 (pipe_v
, params_v
);
3867 CAMLlocal2 (trim_v
, fuzz_v
);
3874 struct sigaction sa
;
3876 state
.cr
= Int_val (Field (pipe_v
, 0));
3877 state
.cw
= Int_val (Field (pipe_v
, 1));
3878 state
.rotate
= Int_val (Field (params_v
, 0));
3879 state
.fitmodel
= Int_val (Field (params_v
, 1));
3880 trim_v
= Field (params_v
, 2);
3881 texcount
= Int_val (Field (params_v
, 3));
3882 state
.sliceheight
= Int_val (Field (params_v
, 4));
3883 mustoresize
= Int_val (Field (params_v
, 5));
3884 colorspace
= Int_val (Field (params_v
, 6));
3885 fontpath
= String_val (Field (params_v
, 7));
3886 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
3887 if (!state
.trimcachepath
) {
3888 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
3891 haspboext
= Bool_val (Field (params_v
, 9));
3893 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3895 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3896 fuzz_v
= Field (trim_v
, 1);
3897 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3898 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3899 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3900 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3902 set_tex_params (colorspace
);
3905 state
.face
= load_font (fontpath
);
3909 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
3911 state
.face
= load_builtin_font (base
, len
);
3913 if (!state
.face
) _exit (1);
3915 realloctexts (texcount
);
3923 sa
.sa_handler
= SIG_IGN
;
3924 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
3926 sa
.sa_handler
= SIG_DFL
;
3927 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
3929 if (sigemptyset (&sa
.sa_mask
)) {
3930 err (1, "sigemptyset");
3932 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
3933 err (1, "sigaction");
3936 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3938 errx (1, "pthread_create: %s", strerror (ret
));
3941 CAMLreturn (Val_unit
);