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>
33 #include <mupdf/fitz.h>
34 #include <mupdf/cbz.h>
35 #include <mupdf/pdf.h>
36 #include <mupdf/xps.h>
37 #include <mupdf/img.h>
39 #include FT_FREETYPE_H
42 #include <fontconfig/fontconfig.h>
46 #define SUMATRA_LOOKUP
49 extern char **environ
;
52 #define MIN(a,b) ((a) < (b) ? (a) : (b))
53 #define MAX(a,b) ((a) > (b) ? (a) : (b))
56 #define NORETURN_ATTR __attribute__ ((noreturn))
57 #define UNUSED_ATTR __attribute__ ((unused))
58 #if !defined __clang__
59 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
61 #define OPTIMIZE_ATTR(n)
63 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
67 #define OPTIMIZE_ATTR(n)
68 #define GCC_FMT_ATTR(a, b)
74 #define FMT_ptr_cast(p) (p)
75 #define FMT_ptr_cast2(p) (p)
77 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
78 err (int exitcode
, const char *fmt
, ...)
85 vfprintf (stderr
, fmt
, ap
);
87 fprintf (stderr
, ": %s\n", strerror (savederrno
));
92 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
93 errx (int exitcode
, const char *fmt
, ...)
98 vfprintf (stderr
, fmt
, ap
);
100 fputc ('\n', stderr
);
105 #ifndef GL_TEXTURE_RECTANGLE_ARB
106 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
110 #define GL_BGRA 0x80E1
113 #ifndef GL_UNSIGNED_INT_8_8_8_8
114 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
117 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
118 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
122 #define lprintf printf
127 #define ARSERT(cond) for (;;) { \
129 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
145 struct slice slices
[1];
156 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
164 enum { DPDF
, DXPS
, DCBZ
, DIMG
};
173 fz_text_sheet
*sheet
;
181 fz_display_list
*dlist
;
183 struct slink
*slinks
;
188 void (*freepage
) (void *);
194 struct pagedim
*pagedims
;
214 fz_colorspace
*colorspace
;
222 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
236 void (*closedoc
) (void);
237 void (*freepage
) (void *);
245 void (*glBindBufferARB
) (GLenum
, GLuint
);
246 GLboolean (*glUnmapBufferARB
) (GLenum
);
247 void *(*glMapBufferARB
) (GLenum
, GLenum
);
248 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
249 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
250 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
252 GLfloat texcoords
[8];
253 GLfloat vertices
[16];
255 #ifdef SUMATRA_LOOKUP
270 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
272 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
275 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
277 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
280 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
282 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
283 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
286 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
288 static void lock (const char *cap
)
290 int ret
= pthread_mutex_lock (&mutex
);
292 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
296 static void unlock (const char *cap
)
298 int ret
= pthread_mutex_unlock (&mutex
);
300 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
304 static int trylock (const char *cap
)
306 int ret
= pthread_mutex_trylock (&mutex
);
307 if (ret
&& ret
!= EBUSY
) {
308 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
313 static void *parse_pointer (const char *cap
, const char *s
)
318 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
320 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
325 static double now (void)
329 if (gettimeofday (&tv
, NULL
)) {
330 err (1, "gettimeofday");
332 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
335 static int hasdata (void)
338 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
339 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
343 CAMLprim value
ml_hasdata (value fd_v
)
348 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
349 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
350 CAMLreturn (Val_bool (avail
> 0));
353 static void readdata (void *p
, int size
)
358 n
= read (state
.cr
, p
, size
);
360 if (errno
== EINTR
) goto again
;
361 err (1, "read (req %d, ret %zd)", size
, n
);
364 if (!n
) errx (1, "EOF while reading");
365 errx (1, "read (req %d, ret %zd)", size
, n
);
369 static void writedata (char *p
, int size
)
373 p
[0] = (size
>> 24) & 0xff;
374 p
[1] = (size
>> 16) & 0xff;
375 p
[2] = (size
>> 8) & 0xff;
376 p
[3] = (size
>> 0) & 0xff;
378 n
= write (state
.cw
, p
, size
+ 4);
380 if (!n
) errx (1, "EOF while writing data");
381 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
385 static int readlen (void)
390 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
393 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
401 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
404 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
408 if (len
< size
- 4) {
409 writedata (buf
, len
);
415 err (1, "vsnprintf for `%s' failed", fmt
);
417 buf
= realloc (buf
, size
);
422 static void closepdf (void)
424 #ifdef SUMATRA_LOOKUP
425 if (state
.pdflut
.objs
) {
428 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
429 pdf_drop_obj (state
.pdflut
.objs
[i
]);
431 free (state
.pdflut
.objs
);
432 state
.pdflut
.objs
= NULL
;
437 pdf_close_document (state
.u
.pdf
);
442 static void closexps (void)
445 xps_close_document (state
.u
.xps
);
450 static void closecbz (void)
453 cbz_close_document (state
.u
.cbz
);
458 static void closeimg (void)
461 image_close_document (state
.u
.img
);
466 static void freepdfpage (void *ptr
)
468 pdf_free_page (state
.u
.pdf
, ptr
);
471 static void freeemptyxxxpage (void *ptr
)
476 static void freexpspage (void *ptr
)
478 xps_free_page (state
.u
.xps
, ptr
);
481 static void freecbzpage (void *ptr
)
483 cbz_free_page (state
.u
.cbz
, ptr
);
486 static void freeimgpage (void *ptr
)
488 image_free_page (state
.u
.img
, ptr
);
491 static void openxref (char *filename
, char *password
)
495 for (i
= 0; i
< state
.texcount
; ++i
) {
496 state
.texowners
[i
].w
= -1;
497 state
.texowners
[i
].slice
= NULL
;
500 if (state
.closedoc
) state
.closedoc ();
502 len
= strlen (filename
);
520 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
521 int len2
= strlen (tbl
[i
].ext
);
523 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
524 state
.type
= tbl
[i
].type
;
530 if (state
.pagedims
) {
531 free (state
.pagedims
);
532 state
.pagedims
= NULL
;
534 state
.pagedimcount
= 0;
536 fz_set_aa_level (state
.ctx
, state
.aalevel
);
537 switch (state
.type
) {
539 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
540 if (pdf_needs_password (state
.u
.pdf
)) {
541 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
543 errx (1, "invalid password");
546 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
547 state
.closedoc
= closepdf
;
548 state
.freepage
= freepdfpage
;
552 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
553 state
.pagecount
= xps_count_pages (state
.u
.xps
);
554 state
.closedoc
= closexps
;
555 state
.freepage
= freexpspage
;
559 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
560 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
561 state
.closedoc
= closecbz
;
562 state
.freepage
= freecbzpage
;
566 state
.u
.img
= image_open_document (state
.ctx
, filename
);
568 state
.closedoc
= closeimg
;
569 state
.freepage
= freeimgpage
;
574 static void pdfinfo (void)
576 if (state
.type
== DPDF
) {
579 printd ("info PDF version\t%d.%d",
580 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
582 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
586 char *items
[] = { "Title", "Author", "Creator",
587 "Producer", "CreationDate" };
589 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
590 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
591 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
594 printd ("title %s", s
);
596 printd ("info %s\t%s", items
[i
], s
);
598 fz_free (state
.ctx
, s
);
605 static void unlinktile (struct tile
*tile
)
609 for (i
= 0; i
< tile
->slicecount
; ++i
) {
610 struct slice
*s
= &tile
->slices
[i
];
612 if (s
->texindex
!= -1) {
613 if (state
.texowners
[s
->texindex
].slice
== s
) {
614 state
.texowners
[s
->texindex
].slice
= NULL
;
620 static void freepage (struct page
*page
)
623 fz_free_text_page (state
.ctx
, page
->text
);
626 fz_free_text_sheet (state
.ctx
, page
->sheet
);
631 fz_drop_display_list (state
.ctx
, page
->dlist
);
632 page
->freepage (page
->u
.ptr
);
636 static void freetile (struct tile
*tile
)
641 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
644 fz_drop_pixmap (state
.ctx
, state
.pig
);
646 state
.pig
= tile
->pixmap
;
651 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
660 static int cacheline32bytes
;
662 static void __attribute__ ((constructor
)) clcheck (void)
664 char **envp
= environ
;
669 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
671 cacheline32bytes
= auxv
[1] == 32;
677 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
679 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
680 if (cacheline32bytes
&& size
> 32) {
681 intptr_t a1
, a2
, diff
;
683 vector
unsigned char v
= vec_splat_u8 (-1);
684 vector
unsigned char *p
;
686 a1
= a2
= (intptr_t) pixmap
->samples
;
687 a2
= (a1
+ 31) & ~31;
692 while (a1
!= a2
) *(char *) a1
++ = 0xff;
693 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
694 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
696 vec_st (v
, i
+ 16, p
);
698 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
700 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
703 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
706 static void trimctm (pdf_page
*page
, int pindex
)
709 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
711 if (!pdim
->tctmready
) {
712 if (state
.trimmargins
) {
714 fz_matrix rm
, sm
, tm
, im
, ctm1
;
716 fz_rotate (&rm
, -pdim
->rotate
);
717 fz_scale (&sm
, 1, -1);
718 fz_concat (&ctm
, &rm
, &sm
);
719 realbox
= pdim
->mediabox
;
720 fz_transform_rect (&realbox
, &ctm
);
721 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
722 fz_concat (&ctm1
, &ctm
, &tm
);
723 fz_invert_matrix (&im
, &page
->ctm
);
724 fz_concat (&ctm
, &im
, &ctm1
);
734 static fz_matrix
pagectm (struct page
*page
)
738 if (page
->type
== DPDF
) {
739 trimctm (page
->u
.pdfpage
, page
->pdimno
);
741 &state
.pagedims
[page
->pdimno
].tctm
,
742 &state
.pagedims
[page
->pdimno
].ctm
);
745 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
747 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
748 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
753 static void *loadpage (int pageno
, int pindex
)
758 page
= calloc (sizeof (struct page
), 1);
760 err (1, "calloc page %d", pageno
);
763 page
->dlist
= fz_new_display_list (state
.ctx
);
764 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
766 switch (state
.type
) {
768 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
769 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
771 page
->freepage
= freepdfpage
;
775 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
776 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
778 page
->freepage
= freexpspage
;
782 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
783 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
785 page
->freepage
= freecbzpage
;
789 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
790 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
792 page
->freepage
= freeimgpage
;
796 fz_catch (state
.ctx
) {
798 page
->freepage
= freeemptyxxxpage
;
800 fz_free_device (dev
);
802 page
->pdimno
= pindex
;
803 page
->pageno
= pageno
;
804 page
->sgen
= state
.gen
;
805 page
->tgen
= state
.gen
;
806 page
->type
= state
.type
;
811 static struct tile
*alloctile (int h
)
818 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
819 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
820 tile
= calloc (tilesize
, 1);
822 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
824 for (i
= 0; i
< slicecount
; ++i
) {
825 int sh
= MIN (h
, state
.sliceheight
);
826 tile
->slices
[i
].h
= sh
;
827 tile
->slices
[i
].texindex
= -1;
830 tile
->slicecount
= slicecount
;
831 tile
->sliceheight
= state
.sliceheight
;
841 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
842 const fz_matrix
*ctm
, float alpha
)
844 struct obs
*obs
= dev
->user
;
846 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
848 fz_rect rect
= fz_unit_rect
;
850 fz_transform_rect (&rect
, ctm
);
851 fz_round_rect (&b
, &rect
);
852 fz_intersect_irect (&b
, &obs
->b
);
853 obs
->cured
= b
.x0
== obs
->b
.x0
856 && b
.y1
== obs
->b
.y1
;
860 static int obscured (struct page
*page
, fz_irect bbox
)
867 memset (&dev
, 0, sizeof (dev
));
868 memset (&obs
, 0, sizeof (obs
));
873 dev
.fill_image
= obs_fill_image
;
875 fz_rect_from_irect (&rect
, &bbox
);
876 ctm
= pagectm (page
);
877 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
880 #define OBSCURED obscured
882 #define OBSCURED(a, b) 0
885 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
893 struct pagedim
*pdim
;
895 tile
= alloctile (h
);
896 pdim
= &state
.pagedims
[page
->pdimno
];
901 bbox
.x1
= bbox
.x0
+ w
;
902 bbox
.y1
= bbox
.y0
+ h
;
905 if (state
.pig
->w
== w
907 && state
.pig
->colorspace
== state
.colorspace
) {
908 tile
->pixmap
= state
.pig
;
909 tile
->pixmap
->x
= bbox
.x0
;
910 tile
->pixmap
->y
= bbox
.y0
;
913 fz_drop_pixmap (state
.ctx
, state
.pig
);
920 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
926 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
932 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
933 clearpixmap (tile
->pixmap
);
935 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
936 ctm
= pagectm (page
);
937 fz_rect_from_irect (&rect
, &bbox
);
938 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
939 fz_free_device (dev
);
944 #ifdef SUMATRA_LOOKUP
945 /* Following two functions are taken (almost) verbatim from SumatraPDF
949 pdf_load_page_objs_rec(pdf_document
*doc
, pdf_obj
*node
, int *page_no
, pdf_obj
**page_objs
)
951 fz_context
*ctx
= doc
->ctx
;
953 if (pdf_mark_obj(node
))
954 fz_throw(ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
957 pdf_obj
*kids
= pdf_dict_gets(node
, "Kids");
958 int len
= pdf_array_len(kids
), i
;
959 for (i
= 0; i
< len
; i
++) {
960 pdf_obj
*kid
= pdf_array_get(kids
, i
);
961 char *type
= pdf_to_name(pdf_dict_gets(kid
, "Type"));
962 if (!strcmp(type
, "Page")) {
963 if (*page_no
> pdf_count_pages(doc
))
964 fz_throw(ctx
, FZ_ERROR_GENERIC
, "found more /Page objects than anticipated");
965 page_objs
[*page_no
- 1] = pdf_keep_obj(kid
);
968 else if (!strcmp(type
, "Pages")) {
969 int count
= pdf_to_int(pdf_dict_gets(kid
, "Count"));
971 pdf_load_page_objs_rec(doc
, kid
, page_no
, page_objs
);
974 fz_warn(ctx
, "non-page object in page tree (%s)", type
);
979 pdf_unmark_obj(node
);
987 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
989 pdf_obj
*pages
= pdf_dict_getp(pdf_trailer(doc
), "Root/Pages");
991 pdf_load_page_objs_rec(doc
, pages
, &page_no
, page_objs
);
995 static void initpdims (void)
999 fz_rect rootmediabox
;
1000 int pageno
, trim
, show
;
1001 int trimw
= 0, cxcount
;
1005 if (state
.trimmargins
&& state
.trimcachepath
) {
1006 trimf
= fopen (state
.trimcachepath
, "rb");
1008 trimf
= fopen (state
.trimcachepath
, "wb");
1013 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1014 cxcount
= state
.pagecount
;
1016 cxcount
= MIN (state
.pagecount
, 1);
1018 if (state
.type
== DPDF
) {
1020 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1021 "Root/Pages/MediaBox");
1022 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1025 #ifdef SUMATRA_LOOKUP
1026 if (state
.type
== DPDF
1027 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1028 state
.pdflut
.objs
= malloc (sizeof (*state
.pdflut
.objs
) * cxcount
);
1029 if (!state
.pdflut
.objs
) {
1030 err (1, "malloc pageobjs %zu %d %zu failed",
1031 sizeof (*state
.pdflut
.objs
), cxcount
,
1032 sizeof (*state
.pdflut
.objs
) * cxcount
);
1034 state
.pdflut
.count
= cxcount
;
1035 pdf_load_page_objs (state
.u
.pdf
, state
.pdflut
.objs
);
1036 state
.pdflut
.pdf
= state
.u
.pdf
;
1040 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1045 switch (state
.type
) {
1047 pdf_document
*pdf
= state
.u
.pdf
;
1048 pdf_obj
*pageref
, *pageobj
;
1050 #ifdef SUMATRA_LOOKUP
1051 pageref
= state
.pdflut
.objs
[pageno
];
1053 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1055 pageobj
= pdf_resolve_indirect (pageref
);
1057 if (state
.trimmargins
) {
1061 fz_try (state
.ctx
) {
1062 page
= pdf_load_page (pdf
, pageno
);
1063 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1064 trim
= state
.trimanew
|| !obj
;
1070 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1071 dev
->hints
|= FZ_IGNORE_SHADE
;
1072 fz_invert_matrix (&ctm
, &page
->ctm
);
1073 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1074 fz_free_device (dev
);
1076 rect
.x0
+= state
.trimfuzz
.x0
;
1077 rect
.x1
+= state
.trimfuzz
.x1
;
1078 rect
.y0
+= state
.trimfuzz
.y0
;
1079 rect
.y1
+= state
.trimfuzz
.y1
;
1080 fz_transform_rect (&rect
, &ctm
);
1081 fz_intersect_rect (&rect
, &page
->mediabox
);
1083 if (fz_is_empty_rect (&rect
)) {
1084 mediabox
= page
->mediabox
;
1090 obj
= pdf_new_array (pdf
, 4);
1091 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1092 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1093 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1094 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1095 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1098 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1099 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1100 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1101 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1104 rotate
= page
->rotate
;
1105 pdf_free_page (pdf
, page
);
1107 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1109 printd ("progress %f Trimming %d",
1110 (double) (pageno
+ 1) / state
.pagecount
,
1114 fz_catch (state
.ctx
) {
1115 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1122 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1124 if (fz_is_empty_rect (&mediabox
)) {
1132 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1134 if (!fz_is_empty_rect (&cropbox
)) {
1139 fz_intersect_rect (&mediabox
, &cropbox
);
1144 if (fz_is_empty_rect (&rootmediabox
)) {
1146 "cannot find page size for page %d\n",
1150 mediabox
= rootmediabox
;
1154 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1163 fz_try (state
.ctx
) {
1164 page
= xps_load_page (state
.u
.xps
, pageno
);
1165 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1167 if (state
.trimmargins
) {
1171 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1172 dev
->hints
|= FZ_IGNORE_SHADE
;
1173 xps_run_page (state
.u
.xps
, page
, dev
,
1174 &fz_identity
, NULL
);
1175 fz_free_device (dev
);
1177 rect
.x0
+= state
.trimfuzz
.x0
;
1178 rect
.x1
+= state
.trimfuzz
.x1
;
1179 rect
.y0
+= state
.trimfuzz
.y0
;
1180 rect
.y1
+= state
.trimfuzz
.y1
;
1181 fz_intersect_rect (&rect
, &mediabox
);
1183 if (!fz_is_empty_rect (&rect
)) {
1187 xps_free_page (state
.u
.xps
, page
);
1189 printd ("progress %f loading %d",
1190 (double) (pageno
+ 1) / state
.pagecount
,
1194 fz_catch (state
.ctx
) {
1206 if (state
.trimmargins
&& trimw
) {
1209 fz_try (state
.ctx
) {
1210 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1211 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1212 cbz_free_page (state
.u
.cbz
, page
);
1213 printd ("progress %f Trimming %d",
1214 (double) (pageno
+ 1) / state
.pagecount
,
1217 fz_catch (state
.ctx
) {
1218 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1221 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1223 err (1, "fwrite trim mediabox");
1229 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1231 err (1, "fread trim mediabox");
1237 fz_try (state
.ctx
) {
1238 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1239 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1240 cbz_free_page (state
.u
.cbz
, page
);
1242 fz_catch (state
.ctx
) {
1243 fprintf (stderr
, "failed to load cbz page\n");
1261 fz_try (state
.ctx
) {
1262 page
= image_load_page (state
.u
.img
, pageno
);
1263 image_bound_page (state
.u
.img
, page
, &mediabox
);
1264 image_free_page (state
.u
.img
, page
);
1266 fz_catch (state
.ctx
) {
1267 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1273 ARSERT (0 && state
.type
);
1276 if (state
.pagedimcount
== 0
1277 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1278 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1281 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1282 state
.pagedims
= realloc (state
.pagedims
, size
);
1283 if (!state
.pagedims
) {
1284 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1285 size
, state
.pagedimcount
+ 1);
1288 p
= &state
.pagedims
[state
.pagedimcount
++];
1290 p
->mediabox
= mediabox
;
1295 if (state
.trimmargins
) {
1296 printd ("progress 1 Trimmed %d pages in %f seconds",
1297 state
.pagecount
, end
- start
);
1300 printd ("vmsg Processed %d pages in %f seconds",
1301 state
.pagecount
, end
- start
);
1305 if (fclose (trimf
)) {
1311 static void layout (void)
1316 struct pagedim
*p
= p
;
1317 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1319 if (state
.pagedimcount
== 0) return;
1321 switch (state
.fitmodel
) {
1322 case FitProportional
:
1323 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1326 p
= &state
.pagedims
[pindex
];
1327 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1329 fz_transform_rect (&box
, &rm
);
1331 x0
= MIN (box
.x0
, box
.x1
);
1332 x1
= MAX (box
.x0
, box
.x1
);
1335 maxw
= MAX (w
, maxw
);
1336 zoom
= state
.w
/ maxw
;
1348 ARSERT (0 && state
.fitmodel
);
1351 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1355 p
= &state
.pagedims
[pindex
];
1356 fz_rotate (&ctm
, state
.rotate
);
1357 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1359 fz_transform_rect (&box
, &rm
);
1360 w
= box
.x1
- box
.x0
;
1361 switch (state
.fitmodel
) {
1362 case FitProportional
:
1363 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1369 h
= box
.y1
- box
.y0
;
1371 zoom
= MIN (zw
, zh
);
1372 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1381 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1382 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1384 fz_rotate (&rm
, p
->rotate
);
1385 p
->pagebox
= p
->mediabox
;
1386 fz_transform_rect (&p
->pagebox
, &rm
);
1387 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1388 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1392 fz_transform_rect (&rect
, &ctm
);
1393 fz_round_rect (&p
->bounds
, &rect
);
1396 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1397 fz_scale (&sm
, zoom
, -zoom
);
1398 fz_concat (&ctm
, &tm
, &sm
);
1399 fz_concat (&p
->lctm
, &ctm
, &rm
);
1405 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1406 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1407 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1408 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1412 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1413 } while (p
-- != state
.pagedims
);
1417 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1418 desttoanchor (fz_link_dest
*dest
)
1422 struct pagedim
*pdim
= state
.pagedims
;
1427 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1428 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1430 pdim
= &state
.pagedims
[i
];
1432 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1434 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1435 p
.x
= dest
->ld
.gotor
.lt
.x
;
1438 p
.y
= dest
->ld
.gotor
.lt
.y
;
1439 fz_transform_point (&p
, &pdim
->lctm
);
1443 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1444 double x0
, x1
, y0
, y1
;
1446 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1447 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1449 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1450 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1452 a
.n
= dest
->ld
.gotor
.page
;
1457 static void recurse_outline (fz_outline
*outline
, int level
)
1460 switch (outline
->dest
.kind
) {
1463 struct anchor a
= desttoanchor (&outline
->dest
);
1466 printd ("o %d %d %d %d %s",
1467 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1473 printd ("ou %d %" FMT_s
" %s %s", level
,
1474 strlen (outline
->title
), outline
->title
,
1475 outline
->dest
.ld
.uri
.uri
);
1479 printd ("on %d %s", level
, outline
->title
);
1483 printd ("emsg Unhandled outline kind %d for %s\n",
1484 outline
->dest
.kind
, outline
->title
);
1487 if (outline
->down
) {
1488 recurse_outline (outline
->down
, level
+ 1);
1490 outline
= outline
->next
;
1494 static void process_outline (void)
1496 fz_outline
*outline
;
1498 if (!state
.needoutline
) return;
1500 state
.needoutline
= 0;
1501 switch (state
.type
) {
1503 outline
= pdf_load_outline (state
.u
.pdf
);
1506 outline
= xps_load_outline (state
.u
.xps
);
1513 recurse_outline (outline
, 0);
1514 fz_free_outline (state
.ctx
, outline
);
1518 static char *strofspan (fz_text_span
*span
)
1523 size_t size
= 0, cap
= 80;
1525 p
= malloc (cap
+ 1);
1526 if (!p
) return NULL
;
1528 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1529 int n
= fz_runetochar (utf8
, ch
->c
);
1530 if (size
+ n
> cap
) {
1532 p
= realloc (p
, cap
+ 1);
1533 if (!p
) return NULL
;
1536 memcpy (p
+ size
, utf8
, n
);
1543 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1544 fz_text_span
*span
, fz_matrix ctm
,
1545 int stop
, int pageno
, double start
)
1552 fz_point p1
, p2
, p3
, p4
;
1554 p
= strofspan (span
);
1557 ret
= regexec (re
, p
, 1, &rm
, 0);
1560 if (ret
!= REG_NOMATCH
) {
1563 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1564 printd ("msg regexec error `%.*s'",
1565 (int) size
, errbuf
);
1573 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1574 c
+= fz_runelen (span
->text
[a
].c
);
1576 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1577 c
+= fz_runelen (span
->text
[b
].c
);
1580 if (fz_runelen (span
->text
[b
].c
) > 1) {
1584 fz_text_char_bbox (&sb
, span
, a
);
1585 fz_text_char_bbox (&eb
, span
, b
);
1597 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1604 printd ("progress 1 found at %d `%.*s' in %f sec",
1605 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1609 printd ("match %d %d %f %f %f %f %f %f %f %f",
1621 static int compareblocks (const void *l
, const void *r
)
1623 fz_text_block
const *ls
= l
;
1624 fz_text_block
const* rs
= r
;
1625 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1628 /* wishful thinking function */
1629 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1634 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1636 fz_text_sheet
*sheet
;
1637 struct pagedim
*pdim
, *pdimprev
;
1638 int stop
= 0, niters
= 0;
1641 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1645 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1646 if (niters
++ == 5) {
1649 printd ("progress 1 attention requested aborting search at %d",
1654 printd ("progress %f searching in page %d",
1655 (double) (pageno
+ 1) / state
.pagecount
,
1660 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1661 pdim
= &state
.pagedims
[i
];
1662 if (pdim
->pageno
== pageno
) {
1665 if (pdim
->pageno
> pageno
) {
1674 sheet
= fz_new_text_sheet (state
.ctx
);
1675 text
= fz_new_text_page (state
.ctx
);
1676 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1678 switch (state
.type
) {
1681 fz_try (state
.ctx
) {
1682 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1683 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1684 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1685 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1686 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1689 fz_catch (state
.ctx
) {
1690 fz_free_device (tdev
);
1697 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1698 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1702 ARSERT (0 && state
.type
);
1705 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1706 fz_free_device (tdev
);
1708 for (j
= 0; j
< text
->len
; ++j
) {
1711 fz_text_block
*block
;
1713 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1714 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1717 for (k
= 0; k
< block
->len
; ++k
) {
1722 line
= &block
->lines
[k
];
1723 if (line
->bbox
.y0
< y
+ 1) continue;
1726 line
= &block
->lines
[block
->len
- 1 - k
];
1727 if (line
->bbox
.y0
> y
- 1) continue;
1730 for (span
= line
->first_span
; span
; span
= span
->next
) {
1731 switch (matchspan (re
, text
, span
, ctm
,
1732 stop
, pageno
, start
)) {
1734 case 1: stop
= 1; break;
1735 case -1: stop
= 1; goto endloop
;
1750 fz_free_text_page (state
.ctx
, text
);
1751 fz_free_text_sheet (state
.ctx
, sheet
);
1753 state
.freepage (u
.ptr
);
1758 printd ("progress 1 no matches %f sec", end
- start
);
1760 printd ("clearrects");
1763 static void set_tex_params (int colorspace
)
1770 switch (colorspace
) {
1772 state
.texiform
= GL_RGBA8
;
1773 state
.texform
= GL_RGBA
;
1774 state
.texty
= GL_UNSIGNED_BYTE
;
1775 state
.colorspace
= fz_device_rgb (state
.ctx
);
1778 state
.texiform
= GL_RGBA8
;
1779 state
.texform
= GL_BGRA
;
1780 state
.texty
= endianness
.s
> 1
1781 ? GL_UNSIGNED_INT_8_8_8_8
1782 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1783 state
.colorspace
= fz_device_bgr (state
.ctx
);
1786 state
.texiform
= GL_LUMINANCE_ALPHA
;
1787 state
.texform
= GL_LUMINANCE_ALPHA
;
1788 state
.texty
= GL_UNSIGNED_BYTE
;
1789 state
.colorspace
= fz_device_gray (state
.ctx
);
1792 errx (1, "invalid colorspce %d", colorspace
);
1796 static void realloctexts (int texcount
)
1800 if (texcount
== state
.texcount
) return;
1802 if (texcount
< state
.texcount
) {
1803 glDeleteTextures (state
.texcount
- texcount
,
1804 state
.texids
+ texcount
);
1807 size
= texcount
* sizeof (*state
.texids
);
1808 state
.texids
= realloc (state
.texids
, size
);
1809 if (!state
.texids
) {
1810 err (1, "realloc texids %" FMT_s
, size
);
1813 size
= texcount
* sizeof (*state
.texowners
);
1814 state
.texowners
= realloc (state
.texowners
, size
);
1815 if (!state
.texowners
) {
1816 err (1, "realloc texowners %" FMT_s
, size
);
1818 if (texcount
> state
.texcount
) {
1821 glGenTextures (texcount
- state
.texcount
,
1822 state
.texids
+ state
.texcount
);
1823 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1824 state
.texowners
[i
].w
= -1;
1825 state
.texowners
[i
].slice
= NULL
;
1828 state
.texcount
= texcount
;
1832 static char *mbtoutf8 (char *s
)
1838 len
= mbstowcs (NULL
, s
, strlen (s
));
1843 if (len
== (size_t) -1) {
1848 tmp
= malloc (len
* sizeof (wchar_t));
1853 ret
= mbstowcs (tmp
, s
, len
);
1854 if (ret
== (size_t) -1) {
1860 for (i
= 0; i
< ret
; ++i
) {
1861 len
+= fz_runelen (tmp
[i
]);
1864 p
= r
= malloc (len
+ 1);
1870 for (i
= 0; i
< ret
; ++i
) {
1871 p
+= fz_runetochar (p
, tmp
[i
]);
1878 CAMLprim value
ml_mbtoutf8 (value s_v
)
1884 s
= String_val (s_v
);
1890 ret_v
= caml_copy_string (r
);
1896 static void * mainloop (void *unused
)
1899 int len
, ret
, oldlen
= 0;
1904 errx (1, "readlen returned 0");
1907 if (oldlen
< len
+ 1) {
1908 p
= realloc (p
, len
+ 1);
1910 err (1, "realloc %d failed", len
+ 1);
1917 if (!strncmp ("open", p
, 4)) {
1918 int wthack
, off
, ok
= 0;
1924 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1926 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1929 filename
= p
+ 5 + off
;
1930 filenamelen
= strlen (filename
);
1931 password
= filename
+ filenamelen
+ 1;
1934 fz_try (state
.ctx
) {
1935 openxref (filename
, password
);
1938 fz_catch (state
.ctx
) {
1939 utf8filename
= mbtoutf8 (filename
);
1940 printd ("msg Could not open %s", utf8filename
);
1950 utf8filename
= mbtoutf8 (filename
);
1951 printd ("msg Opened %s (press h/F1 to get help)",
1953 if (utf8filename
!= filename
) {
1954 free (utf8filename
);
1957 state
.needoutline
= 1;
1960 else if (!strncmp ("cs", p
, 2)) {
1963 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1965 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1968 set_tex_params (colorspace
);
1969 for (i
= 0; i
< state
.texcount
; ++i
) {
1970 state
.texowners
[i
].w
= -1;
1971 state
.texowners
[i
].slice
= NULL
;
1975 else if (!strncmp ("freepage", p
, 8)) {
1978 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1980 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1984 else if (!strncmp ("freetile", p
, 8)) {
1987 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1989 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1993 else if (!strncmp ("search", p
, 6)) {
1994 int icase
, pageno
, y
, ret
, len2
, forward
;
1998 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1999 &icase
, &pageno
, &y
, &forward
, &len2
);
2001 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2004 pattern
= p
+ 6 + len2
;
2005 ret
= regcomp (&re
, pattern
,
2006 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2011 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2012 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2015 search (&re
, pageno
, y
, forward
);
2019 else if (!strncmp ("geometry", p
, 8)) {
2023 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2025 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2033 for (i
= 0; i
< state
.texcount
; ++i
) {
2034 state
.texowners
[i
].slice
= NULL
;
2037 state
.fitmodel
= fitmodel
;
2042 unlock ("geometry");
2043 printd ("continue %d", state
.pagecount
);
2045 else if (!strncmp ("reqlayout", p
, 9)) {
2047 int rotate
, fitmodel
, off
, h
;
2050 ret
= sscanf (p
+ 9, " %d %d %d %n",
2051 &rotate
, &fitmodel
, &h
, &off
);
2053 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2056 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2059 state
.rotate
= rotate
;
2060 state
.fitmodel
= fitmodel
;
2065 nameddest
= p
+ 9 + off
;
2066 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2069 pdf_obj
*needle
, *obj
;
2071 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2072 strlen (nameddest
));
2073 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2075 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2077 a
= desttoanchor (&dest
);
2079 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2082 printd ("emsg failed to parse destination `%s'\n",
2087 printd ("emsg destination `%s' not found\n",
2090 pdf_drop_obj (needle
);
2094 unlock ("reqlayout");
2095 printd ("continue %d", state
.pagecount
);
2097 else if (!strncmp ("page", p
, 4)) {
2100 int pageno
, pindex
, ret
;
2102 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2104 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2109 page
= loadpage (pageno
, pindex
);
2113 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
2115 else if (!strncmp ("tile", p
, 4)) {
2116 int x
, y
, w
, h
, ret
;
2122 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
2123 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
2125 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2130 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2134 printd ("tile %d %d %" FMT_ptr
" %u %f",
2136 FMT_ptr_cast2 (tile
),
2137 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2140 else if (!strncmp ("settrim", p
, 7)) {
2144 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2145 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2147 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2151 state
.trimmargins
= trimmargins
;
2152 state
.needoutline
= 1;
2153 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2155 state
.trimfuzz
= fuzz
;
2157 state
.pagedimcount
= 0;
2158 free (state
.pagedims
);
2159 state
.pagedims
= NULL
;
2164 printd ("continue %d", state
.pagecount
);
2166 else if (!strncmp ("sliceh", p
, 6)) {
2169 ret
= sscanf (p
+ 6, " %d", &h
);
2171 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2173 if (h
!= state
.sliceheight
) {
2176 state
.sliceheight
= h
;
2177 for (i
= 0; i
< state
.texcount
; ++i
) {
2178 state
.texowners
[i
].w
= -1;
2179 state
.texowners
[i
].h
= -1;
2180 state
.texowners
[i
].slice
= NULL
;
2184 else if (!strncmp ("interrupt", p
, 9)) {
2185 printd ("vmsg interrupted");
2188 errx (1, "unknown command %.*s", len
, p
);
2194 CAMLprim value
ml_realloctexts (value texcount_v
)
2196 CAMLparam1 (texcount_v
);
2199 if (trylock ("ml_realloctexts")) {
2203 realloctexts (Int_val (texcount_v
));
2205 unlock ("ml_realloctexts");
2208 CAMLreturn (Val_bool (ok
));
2211 static void recti (int x0
, int y0
, int x1
, int y1
)
2213 GLfloat
*v
= state
.vertices
;
2215 glVertexPointer (2, GL_FLOAT
, 0, v
);
2216 v
[0] = x0
; v
[1] = y0
;
2217 v
[2] = x1
; v
[3] = y0
;
2218 v
[4] = x0
; v
[5] = y1
;
2219 v
[6] = x1
; v
[7] = y1
;
2220 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2223 static void showsel (struct page
*page
, int ox
, int oy
)
2229 fz_page_block
*pageb
;
2230 fz_text_block
*block
;
2231 struct mark first
, last
;
2232 unsigned char selcolor
[] = {15,15,15,140};
2234 first
= page
->fmark
;
2237 if (!first
.span
|| !last
.span
) return;
2239 glEnable (GL_BLEND
);
2240 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2241 glColor4ubv (selcolor
);
2243 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2244 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2245 for (pageb
= page
->text
->blocks
;
2246 pageb
< page
->text
->blocks
+ page
->text
->len
;
2248 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2249 block
= pageb
->u
.text
;
2251 for (line
= block
->lines
;
2252 line
< block
->lines
+ block
->len
;
2255 rect
= fz_empty_rect
;
2257 for (span
= line
->first_span
; span
; span
= span
->next
) {
2259 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2264 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2266 j
= MIN (first
.i
, last
.i
);
2267 k
= MAX (first
.i
, last
.i
);
2270 if (span
== first
.span
) {
2274 else if (span
== last
.span
) {
2281 for (i
= j
; i
<= k
; ++i
) {
2283 fz_union_rect (&rect
,
2284 fz_text_char_bbox (&bbox
, span
, i
));
2286 fz_round_rect (&bbox
, &rect
);
2287 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2294 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2295 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2296 if (span
== last
.span
) {
2299 rect
= fz_empty_rect
;
2305 glDisable (GL_BLEND
);
2310 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2312 fz_matrix ctm
, tm
, pm
;
2313 fz_link
*link
, *links
;
2314 GLfloat
*texcoords
= state
.texcoords
;
2315 GLfloat
*vertices
= state
.vertices
;
2317 switch (page
->type
) {
2319 links
= page
->u
.pdfpage
->links
;
2323 links
= page
->u
.xpspage
->links
;
2330 glEnable (GL_TEXTURE_1D
);
2331 glEnable (GL_BLEND
);
2332 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2333 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2335 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2336 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2337 fz_translate (&tm
, xoff
, yoff
);
2338 pm
= pagectm (page
);
2339 fz_concat (&ctm
, &pm
, &tm
);
2341 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2342 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2344 for (link
= links
; link
; link
= link
->next
) {
2345 fz_point p1
, p2
, p3
, p4
;
2347 p1
.x
= link
->rect
.x0
;
2348 p1
.y
= link
->rect
.y0
;
2350 p2
.x
= link
->rect
.x1
;
2351 p2
.y
= link
->rect
.y0
;
2353 p3
.x
= link
->rect
.x1
;
2354 p3
.y
= link
->rect
.y1
;
2356 p4
.x
= link
->rect
.x0
;
2357 p4
.y
= link
->rect
.y1
;
2359 fz_transform_point (&p1
, &ctm
);
2360 fz_transform_point (&p2
, &ctm
);
2361 fz_transform_point (&p3
, &ctm
);
2362 fz_transform_point (&p4
, &ctm
);
2364 switch (link
->dest
.kind
) {
2365 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2366 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2367 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2368 default: glColor3ub (0, 0, 0); break;
2376 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2380 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2382 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2383 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2385 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2386 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2388 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2389 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2391 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2392 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2394 glDrawArrays (GL_LINES
, 0, 8);
2397 glDisable (GL_BLEND
);
2398 glDisable (GL_TEXTURE_1D
);
2401 static int compareslinks (const void *l
, const void *r
)
2403 struct slink
const *ls
= l
;
2404 struct slink
const *rs
= r
;
2405 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2406 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2408 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2411 static void droptext (struct page
*page
)
2414 fz_free_text_page (state
.ctx
, page
->text
);
2417 page
->fmark
.span
= NULL
;
2418 page
->lmark
.span
= NULL
;
2422 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2427 static void dropslinks (struct page
*page
)
2430 free (page
->slinks
);
2431 page
->slinks
= NULL
;
2432 page
->slinkcount
= 0;
2436 static void ensureslinks (struct page
*page
)
2440 size_t slinksize
= sizeof (*page
->slinks
);
2441 fz_link
*link
, *links
;
2443 if (state
.gen
!= page
->sgen
) {
2445 page
->sgen
= state
.gen
;
2447 if (page
->slinks
) return;
2449 switch (page
->type
) {
2451 links
= page
->u
.pdfpage
->links
;
2452 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2454 &state
.pagedims
[page
->pdimno
].tctm
,
2455 &state
.pagedims
[page
->pdimno
].ctm
);
2459 links
= page
->u
.xpspage
->links
;
2460 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2467 for (link
= links
; link
; link
= link
->next
) {
2471 page
->slinkcount
= count
;
2472 page
->slinks
= calloc (count
, slinksize
);
2473 if (!page
->slinks
) {
2474 err (1, "realloc slinks %d", count
);
2477 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2481 fz_transform_rect (&rect
, &ctm
);
2482 page
->slinks
[i
].link
= link
;
2483 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2485 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2489 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2490 static void fmt_linkn (char *s
, unsigned int u
)
2492 unsigned int len
; unsigned int q
;
2493 int zma
= 'z' - 'a' + 1;
2495 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2498 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2499 /* handles u == 0 */
2504 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2505 int noff
, char *targ
, int tlen
, int hfsize
)
2509 struct slink
*slink
;
2510 double x0
, y0
, x1
, y1
, w
;
2512 ensureslinks (page
);
2513 glColor3ub (0xc3, 0xb0, 0x91);
2514 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2515 fmt_linkn (buf
, i
+ noff
);
2516 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2517 slink
= &page
->slinks
[i
];
2519 x0
= slink
->bbox
.x0
+ xoff
- 5;
2520 y1
= slink
->bbox
.y0
+ yoff
- 5;
2521 y0
= y1
+ 10 + hfsize
;
2522 w
= measure_string (state
.face
, hfsize
, buf
);
2524 recti (x0
, y0
, x1
, y1
);
2528 glEnable (GL_BLEND
);
2529 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2530 glEnable (GL_TEXTURE_2D
);
2531 glColor3ub (0, 0, 0);
2532 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2533 fmt_linkn (buf
, i
+ noff
);
2534 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2535 slink
= &page
->slinks
[i
];
2537 x0
= slink
->bbox
.x0
+ xoff
;
2538 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2539 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2542 glDisable (GL_TEXTURE_2D
);
2543 glDisable (GL_BLEND
);
2546 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2549 struct slice
*slice1
;
2550 unsigned char *texdata
;
2553 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2554 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2556 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2557 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2558 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2562 int texindex
= state
.texindex
++ % state
.texcount
;
2564 if (state
.texowners
[texindex
].w
== tile
->w
) {
2565 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2569 state
.texowners
[texindex
].h
= slice
->h
;
2573 state
.texowners
[texindex
].h
= slice
->h
;
2576 state
.texowners
[texindex
].w
= tile
->w
;
2577 state
.texowners
[texindex
].slice
= slice
;
2578 slice
->texindex
= texindex
;
2580 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2582 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2586 texdata
= tile
->pixmap
->samples
;
2589 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2601 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2613 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2618 CAMLprim value
ml_begintiles (value unit_v
)
2620 CAMLparam1 (unit_v
);
2621 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2622 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2623 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2624 CAMLreturn (unit_v
);
2627 CAMLprim value
ml_endtiles (value unit_v
)
2629 CAMLparam1 (unit_v
);
2630 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2631 CAMLreturn (unit_v
);
2634 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2636 CAMLparam2 (args_v
, ptr_v
);
2637 int dispx
= Int_val (Field (args_v
, 0));
2638 int dispy
= Int_val (Field (args_v
, 1));
2639 int dispw
= Int_val (Field (args_v
, 2));
2640 int disph
= Int_val (Field (args_v
, 3));
2641 int tilex
= Int_val (Field (args_v
, 4));
2642 int tiley
= Int_val (Field (args_v
, 5));
2643 char *s
= String_val (ptr_v
);
2644 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2645 int slicey
, firstslice
;
2646 struct slice
*slice
;
2647 GLfloat
*texcoords
= state
.texcoords
;
2648 GLfloat
*vertices
= state
.vertices
;
2650 firstslice
= tiley
/ tile
->sliceheight
;
2651 slice
= &tile
->slices
[firstslice
];
2652 slicey
= tiley
% tile
->sliceheight
;
2657 dh
= slice
->h
- slicey
;
2658 dh
= MIN (disph
, dh
);
2659 uploadslice (tile
, slice
);
2661 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2662 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2663 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2664 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2666 vertices
[0] = dispx
; vertices
[1] = dispy
;
2667 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2668 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2669 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2671 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2675 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2678 CAMLreturn (Val_unit
);
2681 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2682 value xoff_v
, value yoff_v
,
2685 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2686 int xoff
= Int_val (xoff_v
);
2687 int yoff
= Int_val (yoff_v
);
2688 int noff
= Int_val (Field (li_v
, 0));
2689 char *targ
= String_val (Field (li_v
, 1));
2690 int tlen
= caml_string_length (Field (li_v
, 1));
2691 int hfsize
= Int_val (Field (li_v
, 2));
2692 char *s
= String_val (ptr_v
);
2693 int hlmask
= Int_val (hlinks_v
);
2694 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2697 /* deal with loadpage failed pages */
2701 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2702 if (trylock ("ml_postprocess")) {
2707 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2708 noff
= page
->slinkcount
;
2710 if (page
->tgen
== state
.gen
) {
2711 showsel (page
, xoff
, yoff
);
2713 unlock ("ml_postprocess");
2716 CAMLreturn (Val_int (noff
));
2719 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2723 const fz_matrix
*tctm
;
2724 fz_link
*link
, *links
;
2726 switch (page
->type
) {
2728 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2729 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2730 links
= page
->u
.pdfpage
->links
;
2734 tctm
= &fz_identity
;
2735 links
= page
->u
.xpspage
->links
;
2744 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2745 fz_invert_matrix (&ctm
, &ctm
);
2746 fz_transform_point (&p
, &ctm
);
2748 for (link
= links
; link
; link
= link
->next
) {
2749 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2750 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2758 static void ensuretext (struct page
*page
)
2760 if (state
.gen
!= page
->tgen
) {
2762 page
->tgen
= state
.gen
;
2768 page
->text
= fz_new_text_page (state
.ctx
);
2769 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2770 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2771 ctm
= pagectm (page
);
2772 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2773 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2774 qsort (page
->text
->blocks
, page
->text
->len
,
2775 sizeof (*page
->text
->blocks
), compareblocks
);
2777 fz_free_device (tdev
);
2781 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2783 CAMLparam2 (start_page_v
, dir_v
);
2785 int i
, dir
= Int_val (dir_v
);
2786 int start_page
= Int_val (start_page_v
);
2787 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2789 ret_v
= Val_int (0);
2790 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2794 lock ("ml_findpage_with_links");
2795 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2798 switch (state
.type
) {
2801 pdf_page
*page
= NULL
;
2803 fz_try (state
.ctx
) {
2804 page
= pdf_load_page (state
.u
.pdf
, i
);
2805 found
= !!page
->links
;
2807 fz_catch (state
.ctx
) {
2817 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2818 found
= !!page
->links
;
2824 ARSERT (0 && "invalid document type");
2828 ret_v
= caml_alloc_small (1, 1);
2829 Field (ret_v
, 0) = Val_int (i
);
2834 unlock ("ml_findpage_with_links");
2840 enum { dir_first
, dir_last
};
2841 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2843 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2845 CAMLparam2 (ptr_v
, dir_v
);
2846 CAMLlocal2 (ret_v
, pos_v
);
2848 int dirtag
, i
, slinkindex
;
2849 struct slink
*found
= NULL
,*slink
;
2850 char *s
= String_val (ptr_v
);
2852 page
= parse_pointer ("ml_findlink", s
);
2853 ret_v
= Val_int (0);
2854 if (trylock ("ml_findlink")) {
2858 ensureslinks (page
);
2860 if (Is_block (dir_v
)) {
2861 dirtag
= Tag_val (dir_v
);
2863 case dir_first_visible
:
2865 int x0
, y0
, dir
, first_index
, last_index
;
2867 pos_v
= Field (dir_v
, 0);
2868 x0
= Int_val (Field (pos_v
, 0));
2869 y0
= Int_val (Field (pos_v
, 1));
2870 dir
= Int_val (Field (pos_v
, 2));
2875 last_index
= page
->slinkcount
;
2878 first_index
= page
->slinkcount
- 1;
2882 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2883 slink
= &page
->slinks
[i
];
2884 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2893 slinkindex
= Int_val (Field (dir_v
, 0));
2894 found
= &page
->slinks
[slinkindex
];
2895 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2896 slink
= &page
->slinks
[i
];
2897 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2905 slinkindex
= Int_val (Field (dir_v
, 0));
2906 found
= &page
->slinks
[slinkindex
];
2907 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2908 slink
= &page
->slinks
[i
];
2909 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2917 slinkindex
= Int_val (Field (dir_v
, 0));
2918 found
= &page
->slinks
[slinkindex
];
2919 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2920 slink
= &page
->slinks
[i
];
2921 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2929 slinkindex
= Int_val (Field (dir_v
, 0));
2930 found
= &page
->slinks
[slinkindex
];
2931 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2932 slink
= &page
->slinks
[i
];
2933 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2942 dirtag
= Int_val (dir_v
);
2945 found
= page
->slinks
;
2950 found
= page
->slinks
+ (page
->slinkcount
- 1);
2956 ret_v
= caml_alloc_small (2, 1);
2957 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2960 unlock ("ml_findlink");
2965 enum { uuri
, ugoto
, utext
, uunexpected
,
2966 ulaunch
, unamed
, uremote
, uremotedest
};
2972 switch (link->dest.kind) { \
2973 case FZ_LINK_GOTO: \
2977 pageno = link->dest.ld.gotor.page; \
2981 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2982 p.y = link->dest.ld.gotor.lt.y; \
2983 fz_transform_point (&p, &pdim->lctm); \
2985 tup_v = caml_alloc_tuple (2); \
2986 ret_v = caml_alloc_small (1, ugoto); \
2987 Field (tup_v, 0) = Val_int (pageno); \
2988 Field (tup_v, 1) = Val_int (p.y); \
2989 Field (ret_v, 0) = tup_v; \
2994 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2995 ret_v = caml_alloc_small (1, uuri); \
2996 Field (ret_v, 0) = str_v; \
2999 case FZ_LINK_LAUNCH: \
3000 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3001 ret_v = caml_alloc_small (1, ulaunch); \
3002 Field (ret_v, 0) = str_v; \
3005 case FZ_LINK_NAMED: \
3006 str_v = caml_copy_string (link->dest.ld.named.named); \
3007 ret_v = caml_alloc_small (1, unamed); \
3008 Field (ret_v, 0) = str_v; \
3011 case FZ_LINK_GOTOR: \
3015 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3016 pageno = link->dest.ld.gotor.page; \
3017 if (pageno == -1) { \
3018 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3019 rty = uremotedest; \
3022 gr_v = Val_int (pageno); \
3025 tup_v = caml_alloc_tuple (2); \
3026 ret_v = caml_alloc_small (1, rty); \
3027 Field (tup_v, 0) = str_v; \
3028 Field (tup_v, 1) = gr_v; \
3029 Field (ret_v, 0) = tup_v; \
3037 snprintf (buf, sizeof (buf), \
3038 "unhandled link kind %d", link->dest.kind); \
3039 str_v = caml_copy_string (buf); \
3040 ret_v = caml_alloc_small (1, uunexpected); \
3041 Field (ret_v, 0) = str_v; \
3047 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3049 CAMLparam2 (ptr_v
, n_v
);
3050 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3053 struct pagedim
*pdim
;
3054 char *s
= String_val (ptr_v
);
3056 ret_v
= Val_int (0);
3057 if (trylock ("ml_getlink")) {
3061 page
= parse_pointer ("ml_getlink", s
);
3062 ensureslinks (page
);
3063 pdim
= &state
.pagedims
[page
->pdimno
];
3064 link
= page
->slinks
[Int_val (n_v
)].link
;
3067 unlock ("ml_getlink");
3072 CAMLprim value
ml_getlinkcount (value ptr_v
)
3076 char *s
= String_val (ptr_v
);
3078 page
= parse_pointer ("ml_getlinkcount", s
);
3079 CAMLreturn (Val_int (page
->slinkcount
));
3082 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3084 CAMLparam2 (ptr_v
, n_v
);
3087 struct slink
*slink
;
3088 char *s
= String_val (ptr_v
);
3090 page
= parse_pointer ("ml_getlinkrect", s
);
3091 ret_v
= caml_alloc_tuple (4);
3092 if (trylock ("ml_getlinkrect")) {
3093 Field (ret_v
, 0) = Val_int (0);
3094 Field (ret_v
, 1) = Val_int (0);
3095 Field (ret_v
, 2) = Val_int (0);
3096 Field (ret_v
, 3) = Val_int (0);
3099 ensureslinks (page
);
3101 slink
= &page
->slinks
[Int_val (n_v
)];
3102 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3103 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3104 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3105 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3106 unlock ("ml_getlinkrect");
3112 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3114 CAMLparam3 (ptr_v
, x_v
, y_v
);
3115 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3118 char *s
= String_val (ptr_v
);
3119 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3120 struct pagedim
*pdim
;
3122 ret_v
= Val_int (0);
3123 if (trylock ("ml_whatsunder")) {
3127 page
= parse_pointer ("ml_whatsunder", s
);
3128 pdim
= &state
.pagedims
[page
->pdimno
];
3129 x
+= pdim
->bounds
.x0
;
3130 y
+= pdim
->bounds
.y0
;
3131 link
= getlink (page
, x
, y
);
3137 fz_page_block
*pageb
;
3138 fz_text_block
*block
;
3141 for (pageb
= page
->text
->blocks
;
3142 pageb
< page
->text
->blocks
+ page
->text
->len
;
3145 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3146 block
= pageb
->u
.text
;
3149 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3152 for (line
= block
->lines
;
3153 line
< block
->lines
+ block
->len
;
3158 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3161 for (span
= line
->first_span
; span
; span
= span
->next
) {
3165 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3168 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3170 fz_text_char_bbox (&bbox
, span
, charnum
);
3173 if (x
>= b
->x0
&& x
<= b
->x1
3174 && y
>= b
->y0
&& y
<= b
->y1
) {
3175 fz_text_style
*style
= span
->text
->style
;
3177 style
->font
&& style
->font
->name
3179 : "Span has no font name"
3181 FT_FaceRec
*face
= style
->font
->ft_face
;
3182 if (face
&& face
->family_name
) {
3184 char *n1
= face
->family_name
;
3185 size_t l1
= strlen (n1
);
3186 size_t l2
= strlen (n2
);
3188 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3189 s
= malloc (l1
+ l2
+ 2);
3193 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3194 str_v
= caml_copy_string (s
);
3200 str_v
= caml_copy_string (n2
);
3202 ret_v
= caml_alloc_small (1, utext
);
3203 Field (ret_v
, 0) = str_v
;
3212 unlock ("ml_whatsunder");
3218 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3220 static int uninteresting (int c
)
3222 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3226 CAMLprim value
ml_clearmark (value ptr_v
)
3229 char *s
= String_val (ptr_v
);
3232 if (trylock ("ml_clearmark")) {
3236 page
= parse_pointer ("ml_clearmark", s
);
3237 page
->fmark
.span
= NULL
;
3238 page
->lmark
.span
= NULL
;
3242 unlock ("ml_clearmark");
3244 CAMLreturn (Val_unit
);
3247 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3249 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3254 fz_page_block
*pageb
;
3255 fz_text_block
*block
;
3256 struct pagedim
*pdim
;
3257 int mark
= Int_val (mark_v
);
3258 char *s
= String_val (ptr_v
);
3259 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3261 ret_v
= Val_bool (0);
3262 if (trylock ("ml_markunder")) {
3266 page
= parse_pointer ("ml_markunder", s
);
3267 pdim
= &state
.pagedims
[page
->pdimno
];
3271 if (mark
== mark_page
) {
3273 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3275 for (i
= 0; i
< page
->text
->len
; ++i
) {
3276 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3277 pb1
= &page
->text
->blocks
[i
];
3281 if (!pb1
) goto unlock
;
3283 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3284 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3285 pb2
= &page
->text
->blocks
[i
];
3289 if (!pb2
) goto unlock
;
3291 block
= pb1
->u
.text
;
3294 page
->fmark
.span
= block
->lines
->first_span
;
3296 block
= pb2
->u
.text
;
3297 line
= &block
->lines
[block
->len
- 1];
3298 page
->lmark
.i
= line
->last_span
->len
- 1;
3299 page
->lmark
.span
= line
->last_span
;
3300 ret_v
= Val_bool (1);
3304 x
+= pdim
->bounds
.x0
;
3305 y
+= pdim
->bounds
.y0
;
3307 for (pageb
= page
->text
->blocks
;
3308 pageb
< page
->text
->blocks
+ page
->text
->len
;
3310 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3311 block
= pageb
->u
.text
;
3314 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3317 if (mark
== mark_block
) {
3319 page
->fmark
.span
= block
->lines
->first_span
;
3321 line
= &block
->lines
[block
->len
- 1];
3322 page
->lmark
.i
= line
->last_span
->len
- 1;
3323 page
->lmark
.span
= line
->last_span
;
3324 ret_v
= Val_bool (1);
3328 for (line
= block
->lines
;
3329 line
< block
->lines
+ block
->len
;
3334 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3337 if (mark
== mark_line
) {
3339 page
->fmark
.span
= line
->first_span
;
3341 page
->lmark
.i
= line
->last_span
->len
- 1;
3342 page
->lmark
.span
= line
->last_span
;
3343 ret_v
= Val_bool (1);
3347 for (span
= line
->first_span
; span
; span
= span
->next
) {
3351 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3354 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3356 fz_text_char_bbox (&bbox
, span
, charnum
);
3359 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3361 int charnum2
, charnum3
= -1, charnum4
= -1;
3363 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3365 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3366 if (uninteresting (span
->text
[charnum2
].c
)) {
3367 charnum3
= charnum2
+ 1;
3371 if (charnum3
== -1) charnum3
= 0;
3373 for (charnum2
= charnum
+ 1;
3374 charnum2
< span
->len
;
3376 if (uninteresting (span
->text
[charnum2
].c
)) break;
3377 charnum4
= charnum2
;
3379 if (charnum4
== -1) goto unlock
;
3381 page
->fmark
.i
= charnum3
;
3382 page
->fmark
.span
= span
;
3384 page
->lmark
.i
= charnum4
;
3385 page
->lmark
.span
= span
;
3386 ret_v
= Val_bool (1);
3394 if (!Bool_val (ret_v
)) {
3395 page
->fmark
.span
= NULL
;
3396 page
->lmark
.span
= NULL
;
3400 unlock ("ml_markunder");
3406 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3408 CAMLparam3 (ptr_v
, x_v
, y_v
);
3409 CAMLlocal2 (ret_v
, res_v
);
3412 fz_page_block
*pageb
;
3413 struct pagedim
*pdim
;
3414 char *s
= String_val (ptr_v
);
3415 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3417 ret_v
= Val_int (0);
3418 if (trylock ("ml_rectofblock")) {
3422 page
= parse_pointer ("ml_rectofblock", s
);
3423 pdim
= &state
.pagedims
[page
->pdimno
];
3424 x
+= pdim
->bounds
.x0
;
3425 y
+= pdim
->bounds
.y0
;
3429 for (pageb
= page
->text
->blocks
;
3430 pageb
< page
->text
->blocks
+ page
->text
->len
;
3432 switch (pageb
->type
) {
3433 case FZ_PAGE_BLOCK_TEXT
:
3434 b
= &pageb
->u
.text
->bbox
;
3437 case FZ_PAGE_BLOCK_IMAGE
:
3438 b
= &pageb
->u
.image
->bbox
;
3445 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3450 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3451 ret_v
= caml_alloc_small (1, 1);
3452 Store_double_field (res_v
, 0, b
->x0
);
3453 Store_double_field (res_v
, 1, b
->x1
);
3454 Store_double_field (res_v
, 2, b
->y0
);
3455 Store_double_field (res_v
, 3, b
->y1
);
3456 Field (ret_v
, 0) = res_v
;
3458 unlock ("ml_rectofblock");
3464 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3466 CAMLparam2 (ptr_v
, rect_v
);
3469 struct pagedim
*pdim
;
3470 char *s
= String_val (ptr_v
);
3471 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3473 fz_page_block
*pageb
;
3474 fz_text_block
*block
;
3475 fz_text_span
*span
, *fspan
, *lspan
;
3477 if (trylock ("ml_seltext")) {
3481 page
= parse_pointer ("ml_seltext", s
);
3484 pdim
= &state
.pagedims
[page
->pdimno
];
3485 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3486 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3487 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3488 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3499 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3500 glColor3ub (128, 128, 128);
3501 recti (x0
, y0
, x1
, y1
);
3502 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3506 fspan
= page
->fmark
.span
;
3509 lspan
= page
->lmark
.span
;
3511 for (pageb
= page
->text
->blocks
;
3512 pageb
< page
->text
->blocks
+ page
->text
->len
;
3514 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3515 block
= pageb
->u
.text
;
3516 for (line
= block
->lines
;
3517 line
< block
->lines
+ block
->len
;
3521 for (span
= line
->first_span
; span
; span
= span
->next
) {
3522 for (i
= 0; i
< span
->len
; ++i
) {
3525 fz_text_char_bbox (&b
, span
, i
);
3527 if (x0
>= b
.x0
&& x0
<= b
.x1
3528 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3533 if (x1
>= b
.x0
&& x1
<= b
.x1
3534 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3539 if (0 && selected
) {
3540 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3541 glColor3ub (128, 128, 128);
3542 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3543 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3549 if (x1
< x0
&& fspan
== lspan
) {
3561 page
->fmark
.span
= fspan
;
3564 page
->lmark
.span
= lspan
;
3566 unlock ("ml_seltext");
3569 CAMLreturn (Val_unit
);
3572 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3577 for (i
= a
; i
<= b
; ++i
) {
3578 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3579 ret
= fwrite (buf
, len
, 1, f
);
3582 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3583 len
, ret
, strerror (errno
));
3591 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3593 caml_failwith ("ml_popen not implemented under Cygwin");
3596 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3598 CAMLparam2 (command_v
, fds_v
);
3599 CAMLlocal2 (l_v
, tup_v
);
3602 value earg_v
= Nothing
;
3603 posix_spawnattr_t attr
;
3604 posix_spawn_file_actions_t fa
;
3605 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3607 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3608 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3611 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3612 msg
= "posix_spawnattr_init";
3616 #ifdef POSIX_SPAWN_USEVFORK
3617 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3618 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3623 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3626 tup_v
= Field (l_v
, 0);
3627 fd1
= Int_val (Field (tup_v
, 0));
3628 fd2
= Int_val (Field (tup_v
, 1));
3630 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3631 msg
= "posix_spawn_file_actions_addclose";
3637 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3638 msg
= "posix_spawn_file_actions_adddup2";
3645 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3646 msg
= "posix_spawn";
3651 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3652 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3656 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3657 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3662 unix_error (ret
, msg
, earg_v
);
3664 CAMLreturn (Val_unit
);
3668 CAMLprim value
ml_hassel (value ptr_v
)
3673 char *s
= String_val (ptr_v
);
3675 ret_v
= Val_bool (0);
3676 if (trylock ("ml_hassel")) {
3680 page
= parse_pointer ("ml_hassel", s
);
3681 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3682 unlock ("ml_hassel");
3687 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3689 CAMLparam2 (fd_v
, ptr_v
);
3694 fz_page_block
*pageb
;
3695 fz_text_block
*block
;
3696 int fd
= Int_val (fd_v
);
3697 char *s
= String_val (ptr_v
);
3699 if (trylock ("ml_copysel")) {
3703 page
= parse_pointer ("ml_copysel", s
);
3705 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3706 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3710 f
= fdopen (fd
, "w");
3712 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3713 fd
, strerror (errno
));
3717 for (pageb
= page
->text
->blocks
;
3718 pageb
< page
->text
->blocks
+ page
->text
->len
;
3720 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3721 block
= pageb
->u
.text
;
3722 for (line
= block
->lines
;
3723 line
< block
->lines
+ block
->len
;
3727 for (span
= line
->first_span
; span
; span
= span
->next
) {
3730 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3731 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3732 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3735 if (pipespan (f
, span
, a
, b
)) {
3738 if (span
== page
->lmark
.span
) {
3741 if (span
== line
->last_span
) {
3742 if (putc ('\n', f
) == EOF
) {
3744 "failed break line on sel pipe: %s\n",
3755 int ret
= fclose (f
);
3758 if (errno
!= ECHILD
) {
3759 fprintf (stderr
, "failed to close sel pipe: %s\n",
3765 unlock ("ml_copysel");
3770 fprintf (stderr
, "failed to close sel pipe: %s\n",
3774 CAMLreturn (Val_unit
);
3777 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3779 CAMLparam1 (pagedimno_v
);
3781 int pagedimno
= Int_val (pagedimno_v
);
3784 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3785 if (trylock ("ml_getpdimrect")) {
3786 box
= fz_empty_rect
;
3789 box
= state
.pagedims
[pagedimno
].mediabox
;
3790 unlock ("ml_getpdimrect");
3793 Store_double_field (ret_v
, 0, box
.x0
);
3794 Store_double_field (ret_v
, 1, box
.x1
);
3795 Store_double_field (ret_v
, 2, box
.y0
);
3796 Store_double_field (ret_v
, 3, box
.y1
);
3801 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3802 value dw_v
, value cols_v
)
3804 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3810 double winw
= Int_val (winw_v
);
3811 double winh
= Int_val (winh_v
);
3812 double dw
= Int_val (dw_v
);
3813 double cols
= Int_val (cols_v
);
3814 double pw
= 1.0, ph
= 1.0;
3816 if (trylock ("ml_zoom_for_height")) {
3820 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3821 double w
= p
->pagebox
.x1
/ cols
;
3822 double h
= p
->pagebox
.y1
;
3826 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3828 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3831 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3832 unlock ("ml_zoom_for_height");
3834 ret_v
= caml_copy_double (zoom
);
3838 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3840 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3842 int pt
= Int_val(pt_v
);
3843 int x
= Int_val (x_v
);
3844 int y
= Int_val (y_v
);
3847 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3848 ret_v
= caml_copy_double (w
);
3852 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3854 CAMLparam2 (pt_v
, string_v
);
3856 int pt
= Int_val (pt_v
);
3859 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3860 ret_v
= caml_copy_double (w
);
3864 CAMLprim value
ml_getpagebox (value opaque_v
)
3866 CAMLparam1 (opaque_v
);
3872 char *s
= String_val (opaque_v
);
3873 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3875 ret_v
= caml_alloc_tuple (4);
3876 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3877 dev
->hints
|= FZ_IGNORE_SHADE
;
3879 switch (page
->type
) {
3881 ctm
= pagectm (page
);
3882 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3886 ctm
= pagectm (page
);
3887 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3891 rect
= fz_infinite_rect
;
3895 fz_free_device (dev
);
3896 fz_round_rect (&bbox
, &rect
);
3897 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3898 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3899 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3900 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3905 CAMLprim value
ml_setaalevel (value level_v
)
3907 CAMLparam1 (level_v
);
3909 state
.aalevel
= Int_val (level_v
);
3910 CAMLreturn (Val_unit
);
3914 #include <X11/Xlib.h>
3920 GLXDrawable drawable
;
3923 #include "keysym2ucs.c"
3925 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3927 CAMLparam1 (keysym_v
);
3929 KeySym keysym
= Int_val (keysym_v
);
3934 rune
= keysym2ucs (keysym
);
3935 len
= fz_runetochar (buf
, rune
);
3937 str_v
= caml_copy_string (buf
);
3941 CAMLprim value
ml_glx (value win_v
)
3944 XVisualInfo
*visual
;
3945 int screen
, wid
= Int_val (win_v
);
3946 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3948 glx
.dpy
= XOpenDisplay (NULL
);
3950 caml_failwith ("XOpenDisplay");
3953 screen
= DefaultScreen (glx
.dpy
);
3954 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3956 XCloseDisplay (glx
.dpy
);
3958 caml_failwith ("glXChooseVisual");
3961 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3964 XCloseDisplay (glx
.dpy
);
3966 caml_failwith ("glXCreateContext");
3969 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3970 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3971 XCloseDisplay (glx
.dpy
);
3974 caml_failwith ("glXMakeCurrent");
3977 CAMLreturn (Val_unit
);
3980 CAMLprim value
ml_swapb (value unit_v
)
3982 CAMLparam1 (unit_v
);
3983 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3984 CAMLreturn (Val_unit
);
3987 CAMLprim value
ml_glxsync (value unit_v
)
3989 CAMLparam1 (unit_v
);
3990 if (glx
.dpy
&& glx
.ctx
) {
3994 CAMLreturn (Val_unit
);
3997 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3998 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
4000 CAMLprim value
ml_platform (value unit_v
)
4002 CAMLparam1 (unit_v
);
4003 int platid
= piunknown
;
4005 #if defined __linux__
4007 #elif defined __CYGWIN__
4009 #elif defined __DragonFly__
4010 platid
= pidragonflybsd
;
4011 #elif defined __FreeBSD__
4013 #elif defined __OpenBSD__
4015 #elif defined __NetBSD__
4017 #elif defined __sun__
4019 #elif defined __APPLE__
4022 CAMLreturn (Val_int (platid
));
4025 CAMLprim value
ml_cloexec (value fd_v
)
4028 int fd
= Int_val (fd_v
);
4030 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4031 uerror ("fcntl", Nothing
);
4033 CAMLreturn (Val_unit
);
4036 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4038 CAMLparam2 (w_v
, h_v
);
4041 int w
= Int_val (w_v
);
4042 int h
= Int_val (h_v
);
4043 int cs
= Int_val (cs_v
);
4045 if (state
.pbo_usable
) {
4046 pbo
= calloc (sizeof (*pbo
), 1);
4048 err (1, "calloc pbo");
4060 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4063 state
.glGenBuffersARB (1, &pbo
->id
);
4064 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4065 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4066 NULL
, GL_STREAM_DRAW
);
4067 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4069 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4071 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4072 state
.glDeleteBuffersARB (1, &pbo
->id
);
4074 ret_v
= caml_copy_string ("0");
4080 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
4082 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
4086 err (1, "malloc %d bytes failed", res
+1);
4088 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
4090 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
4092 ret_v
= caml_copy_string (s
);
4097 ret_v
= caml_copy_string ("0");
4102 CAMLprim value
ml_freepbo (value s_v
)
4105 char *s
= String_val (s_v
);
4106 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4109 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4111 tile
->pbo
->ptr
= NULL
;
4112 tile
->pbo
->size
= -1;
4114 CAMLreturn (Val_unit
);
4117 CAMLprim value
ml_unmappbo (value s_v
)
4120 char *s
= String_val (s_v
);
4121 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4124 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4125 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4126 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4128 tile
->pbo
->ptr
= NULL
;
4129 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4131 CAMLreturn (Val_unit
);
4134 static void setuppbo (void)
4136 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4137 state
.pbo_usable
= GGPA (glBindBufferARB
)
4138 && GGPA (glUnmapBufferARB
)
4139 && GGPA (glMapBufferARB
)
4140 && GGPA (glBufferDataARB
)
4141 && GGPA (glGenBuffersARB
)
4142 && GGPA (glDeleteBuffersARB
);
4146 CAMLprim value
ml_pbo_usable (value unit_v
)
4148 CAMLparam1 (unit_v
);
4149 CAMLreturn (Val_bool (state
.pbo_usable
));
4152 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4154 CAMLparam3 (ptr_v
, x_v
, y_v
);
4155 CAMLlocal2 (ret_v
, tup_v
);
4157 char *s
= String_val (ptr_v
);
4158 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4159 struct pagedim
*pdim
;
4163 page
= parse_pointer ("ml_unproject", s
);
4164 pdim
= &state
.pagedims
[page
->pdimno
];
4166 ret_v
= Val_int (0);
4167 if (trylock ("ml_unproject")) {
4171 switch (page
->type
) {
4173 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4179 p
.x
= x
+ pdim
->bounds
.x0
;
4180 p
.y
= y
+ pdim
->bounds
.y0
;
4182 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4183 fz_invert_matrix (&ctm
, &ctm
);
4184 fz_transform_point (&p
, &ctm
);
4186 tup_v
= caml_alloc_tuple (2);
4187 ret_v
= caml_alloc_small (1, 1);
4188 Field (tup_v
, 0) = Val_int (p
.x
);
4189 Field (tup_v
, 1) = Val_int (p
.y
);
4190 Field (ret_v
, 0) = tup_v
;
4192 unlock ("ml_unproject");
4197 static void makestippletex (void)
4199 const char pixels
[] = "\xff\xff\0\0";
4200 glGenTextures (1, &state
.stid
);
4201 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4202 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4203 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4216 CAMLprim value
ml_fz_version (value unit_v
)
4218 return caml_copy_string (FZ_VERSION
);
4221 #ifdef USE_FONTCONFIG
4227 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4231 int needs_exact_metrics
)
4238 FcPattern
*pat
, *pat1
;
4240 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4241 name
, bold
, italic
, needs_exact_metrics
);
4244 fc
.config
= FcInitLoadConfigAndFonts ();
4246 lprintf ("FcInitLoadConfigAndFonts failed\n");
4250 if (!fc
.config
) return NULL
;
4252 size
= strlen (name
);
4253 if (bold
) size
+= sizeof (":bold") - 1;
4254 if (italic
) size
+= sizeof (":italic") - 1;
4257 buf
= malloc (size
);
4259 err (1, "malloc %zu failed", size
);
4263 if (bold
&& italic
) {
4264 strcat (buf
, ":bold:italic");
4267 if (bold
) strcat (buf
, ":bold");
4268 if (italic
) strcat (buf
, ":italic");
4270 for (i
= 0; i
< size
; ++i
) {
4271 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4274 lprintf ("fcbuf=%s\n", buf
);
4275 pat
= FcNameParse ((FcChar8
*) buf
);
4277 printd ("emsg FcNameParse failed\n");
4282 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4283 printd ("emsg FcConfigSubstitute failed\n");
4287 FcDefaultSubstitute (pat
);
4289 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4291 printd ("emsg FcFontMatch failed\n");
4292 FcPatternDestroy (pat
);
4297 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4298 printd ("emsg FcPatternGetString failed\n");
4299 FcPatternDestroy (pat
);
4300 FcPatternDestroy (pat1
);
4306 printd ("emsg name=%s path=%s\n", name
, path
);
4308 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4309 FcPatternDestroy (pat
);
4310 FcPatternDestroy (pat1
);
4316 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4318 CAMLparam2 (pipe_v
, params_v
);
4319 CAMLlocal2 (trim_v
, fuzz_v
);
4326 struct sigaction sa
;
4328 state
.cr
= Int_val (Field (pipe_v
, 0));
4329 state
.cw
= Int_val (Field (pipe_v
, 1));
4330 state
.rotate
= Int_val (Field (params_v
, 0));
4331 state
.fitmodel
= Int_val (Field (params_v
, 1));
4332 trim_v
= Field (params_v
, 2);
4333 texcount
= Int_val (Field (params_v
, 3));
4334 state
.sliceheight
= Int_val (Field (params_v
, 4));
4335 mustoresize
= Int_val (Field (params_v
, 5));
4336 colorspace
= Int_val (Field (params_v
, 6));
4337 fontpath
= String_val (Field (params_v
, 7));
4339 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4340 if (!state
.trimcachepath
) {
4341 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4344 haspboext
= Bool_val (Field (params_v
, 9));
4346 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4348 #ifdef USE_FONTCONFIG
4349 fz_install_load_system_font_funcs (
4350 state
.ctx
, fc_load_system_font_func
, NULL
4354 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4355 fuzz_v
= Field (trim_v
, 1);
4356 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4357 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4358 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4359 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4361 set_tex_params (colorspace
);
4364 #ifndef USE_FONTCONFIG
4365 state
.face
= load_font (fontpath
);
4369 char *buf
= fontpath
;
4370 FcPattern
*pat
, *pat1
;
4373 fc
.config
= FcInitLoadConfigAndFonts ();
4375 errx (1, "FcInitLoadConfigAndFonts failed");
4378 pat
= FcNameParse ((FcChar8
*) buf
);
4380 errx (1, "FcNameParse failed");
4383 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4384 errx (1, "FcConfigSubstitute failed");
4386 FcDefaultSubstitute (pat
);
4388 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4390 errx (1, "FcFontMatch failed");
4393 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4394 errx (1, "FcPatternGetString failed");
4397 state
.face
= load_font ((char *) path
);
4398 FcPatternDestroy (pat
);
4399 FcPatternDestroy (pat1
);
4404 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4406 state
.face
= load_builtin_font (base
, len
);
4408 if (!state
.face
) _exit (1);
4410 realloctexts (texcount
);
4419 sa
.sa_handler
= SIG_IGN
;
4420 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4422 sa
.sa_handler
= SIG_DFL
;
4423 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4425 if (sigemptyset (&sa
.sa_mask
)) {
4426 err (1, "sigemptyset");
4428 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4429 err (1, "sigaction");
4432 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4434 errx (1, "pthread_create: %s", strerror (ret
));
4437 CAMLreturn (Val_unit
);