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
);
519 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
520 int len2
= strlen (tbl
[i
].ext
);
522 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
523 state
.type
= tbl
[i
].type
;
529 if (state
.pagedims
) {
530 free (state
.pagedims
);
531 state
.pagedims
= NULL
;
533 state
.pagedimcount
= 0;
535 fz_set_aa_level (state
.ctx
, state
.aalevel
);
536 switch (state
.type
) {
538 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
539 if (pdf_needs_password (state
.u
.pdf
)) {
540 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
542 errx (1, "invalid password");
545 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
546 state
.closedoc
= closepdf
;
547 state
.freepage
= freepdfpage
;
551 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
552 state
.pagecount
= xps_count_pages (state
.u
.xps
);
553 state
.closedoc
= closexps
;
554 state
.freepage
= freexpspage
;
558 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
559 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
560 state
.closedoc
= closecbz
;
561 state
.freepage
= freecbzpage
;
565 state
.u
.img
= image_open_document (state
.ctx
, filename
);
567 state
.closedoc
= closeimg
;
568 state
.freepage
= freeimgpage
;
573 static void pdfinfo (void)
575 if (state
.type
== DPDF
) {
578 printd ("info PDF version\t%d.%d",
579 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
581 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
585 char *items
[] = { "Title", "Author", "Creator",
586 "Producer", "CreationDate" };
588 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
589 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
590 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
593 printd ("title %s", s
);
595 printd ("info %s\t%s", items
[i
], s
);
597 fz_free (state
.ctx
, s
);
604 static void unlinktile (struct tile
*tile
)
608 for (i
= 0; i
< tile
->slicecount
; ++i
) {
609 struct slice
*s
= &tile
->slices
[i
];
611 if (s
->texindex
!= -1) {
612 if (state
.texowners
[s
->texindex
].slice
== s
) {
613 state
.texowners
[s
->texindex
].slice
= NULL
;
619 static void freepage (struct page
*page
)
622 fz_free_text_page (state
.ctx
, page
->text
);
625 fz_free_text_sheet (state
.ctx
, page
->sheet
);
630 fz_drop_display_list (state
.ctx
, page
->dlist
);
631 page
->freepage (page
->u
.ptr
);
635 static void freetile (struct tile
*tile
)
640 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
643 fz_drop_pixmap (state
.ctx
, state
.pig
);
645 state
.pig
= tile
->pixmap
;
650 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
659 static int cacheline32bytes
;
661 static void __attribute__ ((constructor
)) clcheck (void)
663 char **envp
= environ
;
668 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
670 cacheline32bytes
= auxv
[1] == 32;
676 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
678 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
679 if (cacheline32bytes
&& size
> 32) {
680 intptr_t a1
, a2
, diff
;
682 vector
unsigned char v
= vec_splat_u8 (-1);
683 vector
unsigned char *p
;
685 a1
= a2
= (intptr_t) pixmap
->samples
;
686 a2
= (a1
+ 31) & ~31;
691 while (a1
!= a2
) *(char *) a1
++ = 0xff;
692 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
693 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
695 vec_st (v
, i
+ 16, p
);
697 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
699 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
702 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
705 static void trimctm (pdf_page
*page
, int pindex
)
708 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
710 if (!pdim
->tctmready
) {
711 if (state
.trimmargins
) {
713 fz_matrix rm
, sm
, tm
, im
, ctm1
;
715 fz_rotate (&rm
, -pdim
->rotate
);
716 fz_scale (&sm
, 1, -1);
717 fz_concat (&ctm
, &rm
, &sm
);
718 realbox
= pdim
->mediabox
;
719 fz_transform_rect (&realbox
, &ctm
);
720 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
721 fz_concat (&ctm1
, &ctm
, &tm
);
722 fz_invert_matrix (&im
, &page
->ctm
);
723 fz_concat (&ctm
, &im
, &ctm1
);
733 static fz_matrix
pagectm (struct page
*page
)
737 if (page
->type
== DPDF
) {
738 trimctm (page
->u
.pdfpage
, page
->pdimno
);
740 &state
.pagedims
[page
->pdimno
].tctm
,
741 &state
.pagedims
[page
->pdimno
].ctm
);
744 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
746 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
747 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
752 static void *loadpage (int pageno
, int pindex
)
757 page
= calloc (sizeof (struct page
), 1);
759 err (1, "calloc page %d", pageno
);
762 page
->dlist
= fz_new_display_list (state
.ctx
);
763 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
765 switch (state
.type
) {
767 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
768 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
770 page
->freepage
= freepdfpage
;
774 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
775 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
777 page
->freepage
= freexpspage
;
781 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
782 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
784 page
->freepage
= freecbzpage
;
788 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
789 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
791 page
->freepage
= freeimgpage
;
795 fz_catch (state
.ctx
) {
797 page
->freepage
= freeemptyxxxpage
;
799 fz_free_device (dev
);
801 page
->pdimno
= pindex
;
802 page
->pageno
= pageno
;
803 page
->sgen
= state
.gen
;
804 page
->tgen
= state
.gen
;
805 page
->type
= state
.type
;
810 static struct tile
*alloctile (int h
)
817 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
818 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
819 tile
= calloc (tilesize
, 1);
821 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
823 for (i
= 0; i
< slicecount
; ++i
) {
824 int sh
= MIN (h
, state
.sliceheight
);
825 tile
->slices
[i
].h
= sh
;
826 tile
->slices
[i
].texindex
= -1;
829 tile
->slicecount
= slicecount
;
830 tile
->sliceheight
= state
.sliceheight
;
840 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
841 const fz_matrix
*ctm
, float alpha
)
843 struct obs
*obs
= dev
->user
;
845 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
847 fz_rect rect
= fz_unit_rect
;
849 fz_transform_rect (&rect
, ctm
);
850 fz_round_rect (&b
, &rect
);
851 fz_intersect_irect (&b
, &obs
->b
);
852 obs
->cured
= b
.x0
== obs
->b
.x0
855 && b
.y1
== obs
->b
.y1
;
859 static int obscured (struct page
*page
, fz_irect bbox
)
866 memset (&dev
, 0, sizeof (dev
));
867 memset (&obs
, 0, sizeof (obs
));
872 dev
.fill_image
= obs_fill_image
;
874 fz_rect_from_irect (&rect
, &bbox
);
875 ctm
= pagectm (page
);
876 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
879 #define OBSCURED obscured
881 #define OBSCURED(a, b) 0
884 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
892 struct pagedim
*pdim
;
894 tile
= alloctile (h
);
895 pdim
= &state
.pagedims
[page
->pdimno
];
900 bbox
.x1
= bbox
.x0
+ w
;
901 bbox
.y1
= bbox
.y0
+ h
;
904 if (state
.pig
->w
== w
906 && state
.pig
->colorspace
== state
.colorspace
) {
907 tile
->pixmap
= state
.pig
;
908 tile
->pixmap
->x
= bbox
.x0
;
909 tile
->pixmap
->y
= bbox
.y0
;
912 fz_drop_pixmap (state
.ctx
, state
.pig
);
919 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
925 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
931 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
932 clearpixmap (tile
->pixmap
);
934 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
935 ctm
= pagectm (page
);
936 fz_rect_from_irect (&rect
, &bbox
);
937 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
938 fz_free_device (dev
);
943 #ifdef SUMATRA_LOOKUP
944 /* Following two functions are taken (almost) verbatim from SumatraPDF
948 pdf_load_page_objs_rec(pdf_document
*doc
, pdf_obj
*node
, int *page_no
, pdf_obj
**page_objs
)
950 fz_context
*ctx
= doc
->ctx
;
952 if (pdf_mark_obj(node
))
953 fz_throw(ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
956 pdf_obj
*kids
= pdf_dict_gets(node
, "Kids");
957 int len
= pdf_array_len(kids
), i
;
958 for (i
= 0; i
< len
; i
++) {
959 pdf_obj
*kid
= pdf_array_get(kids
, i
);
960 char *type
= pdf_to_name(pdf_dict_gets(kid
, "Type"));
961 if (!strcmp(type
, "Page")) {
962 if (*page_no
> pdf_count_pages(doc
))
963 fz_throw(ctx
, FZ_ERROR_GENERIC
, "found more /Page objects than anticipated");
964 page_objs
[*page_no
- 1] = pdf_keep_obj(kid
);
967 else if (!strcmp(type
, "Pages")) {
968 int count
= pdf_to_int(pdf_dict_gets(kid
, "Count"));
970 pdf_load_page_objs_rec(doc
, kid
, page_no
, page_objs
);
973 fz_warn(ctx
, "non-page object in page tree (%s)", type
);
978 pdf_unmark_obj(node
);
986 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
988 pdf_obj
*pages
= pdf_dict_getp(pdf_trailer(doc
), "Root/Pages");
990 pdf_load_page_objs_rec(doc
, pages
, &page_no
, page_objs
);
994 static void initpdims (void)
998 fz_rect rootmediabox
;
999 int pageno
, trim
, show
;
1000 int trimw
= 0, cxcount
;
1004 if (state
.trimmargins
&& state
.trimcachepath
) {
1005 trimf
= fopen (state
.trimcachepath
, "rb");
1007 trimf
= fopen (state
.trimcachepath
, "wb");
1012 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1013 cxcount
= state
.pagecount
;
1015 cxcount
= MIN (state
.pagecount
, 1);
1017 if (state
.type
== DPDF
) {
1019 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1020 "Root/Pages/MediaBox");
1021 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1024 #ifdef SUMATRA_LOOKUP
1025 if (state
.type
== DPDF
1026 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1027 state
.pdflut
.objs
= malloc (sizeof (*state
.pdflut
.objs
) * cxcount
);
1028 if (!state
.pdflut
.objs
) {
1029 err (1, "malloc pageobjs %zu %d %zu failed",
1030 sizeof (*state
.pdflut
.objs
), cxcount
,
1031 sizeof (*state
.pdflut
.objs
) * cxcount
);
1033 state
.pdflut
.count
= cxcount
;
1034 pdf_load_page_objs (state
.u
.pdf
, state
.pdflut
.objs
);
1035 state
.pdflut
.pdf
= state
.u
.pdf
;
1039 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1044 switch (state
.type
) {
1046 pdf_document
*pdf
= state
.u
.pdf
;
1047 pdf_obj
*pageref
, *pageobj
;
1049 #ifdef SUMATRA_LOOKUP
1050 pageref
= state
.pdflut
.objs
[pageno
];
1052 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1054 pageobj
= pdf_resolve_indirect (pageref
);
1056 if (state
.trimmargins
) {
1060 fz_try (state
.ctx
) {
1061 page
= pdf_load_page (pdf
, pageno
);
1062 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1063 trim
= state
.trimanew
|| !obj
;
1069 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1070 dev
->hints
|= FZ_IGNORE_SHADE
;
1071 fz_invert_matrix (&ctm
, &page
->ctm
);
1072 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1073 fz_free_device (dev
);
1075 rect
.x0
+= state
.trimfuzz
.x0
;
1076 rect
.x1
+= state
.trimfuzz
.x1
;
1077 rect
.y0
+= state
.trimfuzz
.y0
;
1078 rect
.y1
+= state
.trimfuzz
.y1
;
1079 fz_transform_rect (&rect
, &ctm
);
1080 fz_intersect_rect (&rect
, &page
->mediabox
);
1082 if (fz_is_empty_rect (&rect
)) {
1083 mediabox
= page
->mediabox
;
1089 obj
= pdf_new_array (pdf
, 4);
1090 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1091 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1092 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1093 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1094 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1097 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1098 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1099 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1100 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1103 rotate
= page
->rotate
;
1104 pdf_free_page (pdf
, page
);
1106 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1108 printd ("progress %f Trimming %d",
1109 (double) (pageno
+ 1) / state
.pagecount
,
1113 fz_catch (state
.ctx
) {
1114 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1121 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1123 if (fz_is_empty_rect (&mediabox
)) {
1131 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1133 if (!fz_is_empty_rect (&cropbox
)) {
1138 fz_intersect_rect (&mediabox
, &cropbox
);
1143 if (fz_is_empty_rect (&rootmediabox
)) {
1145 "cannot find page size for page %d\n",
1149 mediabox
= rootmediabox
;
1153 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1162 fz_try (state
.ctx
) {
1163 page
= xps_load_page (state
.u
.xps
, pageno
);
1164 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1166 if (state
.trimmargins
) {
1170 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1171 dev
->hints
|= FZ_IGNORE_SHADE
;
1172 xps_run_page (state
.u
.xps
, page
, dev
,
1173 &fz_identity
, NULL
);
1174 fz_free_device (dev
);
1176 rect
.x0
+= state
.trimfuzz
.x0
;
1177 rect
.x1
+= state
.trimfuzz
.x1
;
1178 rect
.y0
+= state
.trimfuzz
.y0
;
1179 rect
.y1
+= state
.trimfuzz
.y1
;
1180 fz_intersect_rect (&rect
, &mediabox
);
1182 if (!fz_is_empty_rect (&rect
)) {
1186 xps_free_page (state
.u
.xps
, page
);
1188 printd ("progress %f loading %d",
1189 (double) (pageno
+ 1) / state
.pagecount
,
1193 fz_catch (state
.ctx
) {
1205 if (state
.trimmargins
&& trimw
) {
1208 fz_try (state
.ctx
) {
1209 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1210 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1211 cbz_free_page (state
.u
.cbz
, page
);
1212 printd ("progress %f Trimming %d",
1213 (double) (pageno
+ 1) / state
.pagecount
,
1216 fz_catch (state
.ctx
) {
1217 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1220 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1222 err (1, "fwrite trim mediabox");
1228 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1230 err (1, "fread trim mediabox");
1236 fz_try (state
.ctx
) {
1237 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1238 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1239 cbz_free_page (state
.u
.cbz
, page
);
1241 fz_catch (state
.ctx
) {
1242 fprintf (stderr
, "failed to load cbz page\n");
1260 fz_try (state
.ctx
) {
1261 page
= image_load_page (state
.u
.img
, pageno
);
1262 image_bound_page (state
.u
.img
, page
, &mediabox
);
1263 image_free_page (state
.u
.img
, page
);
1265 fz_catch (state
.ctx
) {
1266 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1272 ARSERT (0 && state
.type
);
1275 if (state
.pagedimcount
== 0
1276 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1277 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1280 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1281 state
.pagedims
= realloc (state
.pagedims
, size
);
1282 if (!state
.pagedims
) {
1283 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1284 size
, state
.pagedimcount
+ 1);
1287 p
= &state
.pagedims
[state
.pagedimcount
++];
1289 p
->mediabox
= mediabox
;
1294 if (state
.trimmargins
) {
1295 printd ("progress 1 Trimmed %d pages in %f seconds",
1296 state
.pagecount
, end
- start
);
1299 printd ("vmsg Processed %d pages in %f seconds",
1300 state
.pagecount
, end
- start
);
1304 if (fclose (trimf
)) {
1310 static void layout (void)
1315 struct pagedim
*p
= p
;
1316 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1318 if (state
.pagedimcount
== 0) return;
1320 switch (state
.fitmodel
) {
1321 case FitProportional
:
1322 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1325 p
= &state
.pagedims
[pindex
];
1326 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1328 fz_transform_rect (&box
, &rm
);
1330 x0
= MIN (box
.x0
, box
.x1
);
1331 x1
= MAX (box
.x0
, box
.x1
);
1334 maxw
= MAX (w
, maxw
);
1335 zoom
= state
.w
/ maxw
;
1347 ARSERT (0 && state
.fitmodel
);
1350 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1354 p
= &state
.pagedims
[pindex
];
1355 fz_rotate (&ctm
, state
.rotate
);
1356 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1358 fz_transform_rect (&box
, &rm
);
1359 w
= box
.x1
- box
.x0
;
1360 switch (state
.fitmodel
) {
1361 case FitProportional
:
1362 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1368 h
= box
.y1
- box
.y0
;
1370 zoom
= MIN (zw
, zh
);
1371 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1380 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1381 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1383 fz_rotate (&rm
, p
->rotate
);
1384 p
->pagebox
= p
->mediabox
;
1385 fz_transform_rect (&p
->pagebox
, &rm
);
1386 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1387 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1391 fz_transform_rect (&rect
, &ctm
);
1392 fz_round_rect (&p
->bounds
, &rect
);
1395 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1396 fz_scale (&sm
, zoom
, -zoom
);
1397 fz_concat (&ctm
, &tm
, &sm
);
1398 fz_concat (&p
->lctm
, &ctm
, &rm
);
1404 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1405 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1406 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1407 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1411 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1412 } while (p
-- != state
.pagedims
);
1416 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1417 desttoanchor (fz_link_dest
*dest
)
1421 struct pagedim
*pdim
= state
.pagedims
;
1426 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1427 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1429 pdim
= &state
.pagedims
[i
];
1431 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1433 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1434 p
.x
= dest
->ld
.gotor
.lt
.x
;
1437 p
.y
= dest
->ld
.gotor
.lt
.y
;
1438 fz_transform_point (&p
, &pdim
->lctm
);
1442 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1443 double x0
, x1
, y0
, y1
;
1445 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1446 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1448 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1449 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1451 a
.n
= dest
->ld
.gotor
.page
;
1456 static void recurse_outline (fz_outline
*outline
, int level
)
1459 switch (outline
->dest
.kind
) {
1462 struct anchor a
= desttoanchor (&outline
->dest
);
1465 printd ("o %d %d %d %d %s",
1466 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1472 printd ("ou %d %" FMT_s
" %s %s", level
,
1473 strlen (outline
->title
), outline
->title
,
1474 outline
->dest
.ld
.uri
.uri
);
1478 printd ("on %d %s", level
, outline
->title
);
1482 printd ("emsg Unhandled outline kind %d for %s\n",
1483 outline
->dest
.kind
, outline
->title
);
1486 if (outline
->down
) {
1487 recurse_outline (outline
->down
, level
+ 1);
1489 outline
= outline
->next
;
1493 static void process_outline (void)
1495 fz_outline
*outline
;
1497 if (!state
.needoutline
) return;
1499 state
.needoutline
= 0;
1500 switch (state
.type
) {
1502 outline
= pdf_load_outline (state
.u
.pdf
);
1505 outline
= xps_load_outline (state
.u
.xps
);
1512 recurse_outline (outline
, 0);
1513 fz_free_outline (state
.ctx
, outline
);
1517 static char *strofspan (fz_text_span
*span
)
1522 size_t size
= 0, cap
= 80;
1524 p
= malloc (cap
+ 1);
1525 if (!p
) return NULL
;
1527 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1528 int n
= fz_runetochar (utf8
, ch
->c
);
1529 if (size
+ n
> cap
) {
1531 p
= realloc (p
, cap
+ 1);
1532 if (!p
) return NULL
;
1535 memcpy (p
+ size
, utf8
, n
);
1542 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1543 fz_text_span
*span
, fz_matrix ctm
,
1544 int stop
, int pageno
, double start
)
1551 fz_point p1
, p2
, p3
, p4
;
1553 p
= strofspan (span
);
1556 ret
= regexec (re
, p
, 1, &rm
, 0);
1559 if (ret
!= REG_NOMATCH
) {
1562 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1563 printd ("msg regexec error `%.*s'",
1564 (int) size
, errbuf
);
1572 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1573 c
+= fz_runelen (span
->text
[a
].c
);
1575 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1576 c
+= fz_runelen (span
->text
[b
].c
);
1579 if (fz_runelen (span
->text
[b
].c
) > 1) {
1583 fz_text_char_bbox (&sb
, span
, a
);
1584 fz_text_char_bbox (&eb
, span
, b
);
1596 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1603 printd ("progress 1 found at %d `%.*s' in %f sec",
1604 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1608 printd ("match %d %d %f %f %f %f %f %f %f %f",
1620 static int compareblocks (const void *l
, const void *r
)
1622 fz_text_block
const *ls
= l
;
1623 fz_text_block
const* rs
= r
;
1624 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1627 /* wishful thinking function */
1628 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1633 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1635 fz_text_sheet
*sheet
;
1636 struct pagedim
*pdim
, *pdimprev
;
1637 int stop
= 0, niters
= 0;
1640 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1644 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1645 if (niters
++ == 5) {
1648 printd ("progress 1 attention requested aborting search at %d",
1653 printd ("progress %f searching in page %d",
1654 (double) (pageno
+ 1) / state
.pagecount
,
1659 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1660 pdim
= &state
.pagedims
[i
];
1661 if (pdim
->pageno
== pageno
) {
1664 if (pdim
->pageno
> pageno
) {
1673 sheet
= fz_new_text_sheet (state
.ctx
);
1674 text
= fz_new_text_page (state
.ctx
);
1675 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1677 switch (state
.type
) {
1680 fz_try (state
.ctx
) {
1681 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1682 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1683 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1684 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1685 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1688 fz_catch (state
.ctx
) {
1689 fz_free_device (tdev
);
1696 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1697 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1701 ARSERT (0 && state
.type
);
1704 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1705 fz_free_device (tdev
);
1707 for (j
= 0; j
< text
->len
; ++j
) {
1710 fz_text_block
*block
;
1712 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1713 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1716 for (k
= 0; k
< block
->len
; ++k
) {
1721 line
= &block
->lines
[k
];
1722 if (line
->bbox
.y0
< y
+ 1) continue;
1725 line
= &block
->lines
[block
->len
- 1 - k
];
1726 if (line
->bbox
.y0
> y
- 1) continue;
1729 for (span
= line
->first_span
; span
; span
= span
->next
) {
1730 switch (matchspan (re
, text
, span
, ctm
,
1731 stop
, pageno
, start
)) {
1733 case 1: stop
= 1; break;
1734 case -1: stop
= 1; goto endloop
;
1749 fz_free_text_page (state
.ctx
, text
);
1750 fz_free_text_sheet (state
.ctx
, sheet
);
1752 state
.freepage (u
.ptr
);
1757 printd ("progress 1 no matches %f sec", end
- start
);
1759 printd ("clearrects");
1762 static void set_tex_params (int colorspace
)
1769 switch (colorspace
) {
1771 state
.texiform
= GL_RGBA8
;
1772 state
.texform
= GL_RGBA
;
1773 state
.texty
= GL_UNSIGNED_BYTE
;
1774 state
.colorspace
= fz_device_rgb (state
.ctx
);
1777 state
.texiform
= GL_RGBA8
;
1778 state
.texform
= GL_BGRA
;
1779 state
.texty
= endianness
.s
> 1
1780 ? GL_UNSIGNED_INT_8_8_8_8
1781 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1782 state
.colorspace
= fz_device_bgr (state
.ctx
);
1785 state
.texiform
= GL_LUMINANCE_ALPHA
;
1786 state
.texform
= GL_LUMINANCE_ALPHA
;
1787 state
.texty
= GL_UNSIGNED_BYTE
;
1788 state
.colorspace
= fz_device_gray (state
.ctx
);
1791 errx (1, "invalid colorspce %d", colorspace
);
1795 static void realloctexts (int texcount
)
1799 if (texcount
== state
.texcount
) return;
1801 if (texcount
< state
.texcount
) {
1802 glDeleteTextures (state
.texcount
- texcount
,
1803 state
.texids
+ texcount
);
1806 size
= texcount
* sizeof (*state
.texids
);
1807 state
.texids
= realloc (state
.texids
, size
);
1808 if (!state
.texids
) {
1809 err (1, "realloc texids %" FMT_s
, size
);
1812 size
= texcount
* sizeof (*state
.texowners
);
1813 state
.texowners
= realloc (state
.texowners
, size
);
1814 if (!state
.texowners
) {
1815 err (1, "realloc texowners %" FMT_s
, size
);
1817 if (texcount
> state
.texcount
) {
1820 glGenTextures (texcount
- state
.texcount
,
1821 state
.texids
+ state
.texcount
);
1822 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1823 state
.texowners
[i
].w
= -1;
1824 state
.texowners
[i
].slice
= NULL
;
1827 state
.texcount
= texcount
;
1831 static char *mbtoutf8 (char *s
)
1837 len
= mbstowcs (NULL
, s
, strlen (s
));
1842 if (len
== (size_t) -1) {
1847 tmp
= malloc (len
* sizeof (wchar_t));
1852 ret
= mbstowcs (tmp
, s
, len
);
1853 if (ret
== (size_t) -1) {
1859 for (i
= 0; i
< ret
; ++i
) {
1860 len
+= fz_runelen (tmp
[i
]);
1863 p
= r
= malloc (len
+ 1);
1869 for (i
= 0; i
< ret
; ++i
) {
1870 p
+= fz_runetochar (p
, tmp
[i
]);
1877 CAMLprim value
ml_mbtoutf8 (value s_v
)
1883 s
= String_val (s_v
);
1889 ret_v
= caml_copy_string (r
);
1895 static void * mainloop (void *unused
)
1898 int len
, ret
, oldlen
= 0;
1903 errx (1, "readlen returned 0");
1906 if (oldlen
< len
+ 1) {
1907 p
= realloc (p
, len
+ 1);
1909 err (1, "realloc %d failed", len
+ 1);
1916 if (!strncmp ("open", p
, 4)) {
1917 int wthack
, off
, ok
= 0;
1923 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1925 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1928 filename
= p
+ 5 + off
;
1929 filenamelen
= strlen (filename
);
1930 password
= filename
+ filenamelen
+ 1;
1933 fz_try (state
.ctx
) {
1934 openxref (filename
, password
);
1937 fz_catch (state
.ctx
) {
1938 utf8filename
= mbtoutf8 (filename
);
1939 printd ("msg Could not open %s", utf8filename
);
1949 utf8filename
= mbtoutf8 (filename
);
1950 printd ("msg Opened %s (press h/F1 to get help)",
1952 if (utf8filename
!= filename
) {
1953 free (utf8filename
);
1956 state
.needoutline
= 1;
1959 else if (!strncmp ("cs", p
, 2)) {
1962 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1964 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1967 set_tex_params (colorspace
);
1968 for (i
= 0; i
< state
.texcount
; ++i
) {
1969 state
.texowners
[i
].w
= -1;
1970 state
.texowners
[i
].slice
= NULL
;
1974 else if (!strncmp ("freepage", p
, 8)) {
1977 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1979 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1983 else if (!strncmp ("freetile", p
, 8)) {
1986 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1988 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1992 else if (!strncmp ("search", p
, 6)) {
1993 int icase
, pageno
, y
, ret
, len2
, forward
;
1997 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1998 &icase
, &pageno
, &y
, &forward
, &len2
);
2000 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2003 pattern
= p
+ 6 + len2
;
2004 ret
= regcomp (&re
, pattern
,
2005 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2010 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2011 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2014 search (&re
, pageno
, y
, forward
);
2018 else if (!strncmp ("geometry", p
, 8)) {
2022 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2024 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2032 for (i
= 0; i
< state
.texcount
; ++i
) {
2033 state
.texowners
[i
].slice
= NULL
;
2036 state
.fitmodel
= fitmodel
;
2041 unlock ("geometry");
2042 printd ("continue %d", state
.pagecount
);
2044 else if (!strncmp ("reqlayout", p
, 9)) {
2046 int rotate
, fitmodel
, off
, h
;
2049 ret
= sscanf (p
+ 9, " %d %d %d %n",
2050 &rotate
, &fitmodel
, &h
, &off
);
2052 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2055 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2058 state
.rotate
= rotate
;
2059 state
.fitmodel
= fitmodel
;
2064 nameddest
= p
+ 9 + off
;
2065 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2068 pdf_obj
*needle
, *obj
;
2070 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2071 strlen (nameddest
));
2072 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2074 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2076 a
= desttoanchor (&dest
);
2078 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2081 printd ("emsg failed to parse destination `%s'\n",
2086 printd ("emsg destination `%s' not found\n",
2089 pdf_drop_obj (needle
);
2093 unlock ("reqlayout");
2094 printd ("continue %d", state
.pagecount
);
2096 else if (!strncmp ("page", p
, 4)) {
2099 int pageno
, pindex
, ret
;
2101 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2103 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2108 page
= loadpage (pageno
, pindex
);
2112 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
2114 else if (!strncmp ("tile", p
, 4)) {
2115 int x
, y
, w
, h
, ret
;
2121 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d %" FMT_ptr
,
2122 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
, &data
);
2124 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2129 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2133 printd ("tile %d %d %" FMT_ptr
" %u %f",
2135 FMT_ptr_cast2 (tile
),
2136 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2139 else if (!strncmp ("settrim", p
, 7)) {
2143 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2144 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2146 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2150 state
.trimmargins
= trimmargins
;
2151 state
.needoutline
= 1;
2152 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2154 state
.trimfuzz
= fuzz
;
2156 state
.pagedimcount
= 0;
2157 free (state
.pagedims
);
2158 state
.pagedims
= NULL
;
2163 printd ("continue %d", state
.pagecount
);
2165 else if (!strncmp ("sliceh", p
, 6)) {
2168 ret
= sscanf (p
+ 6, " %d", &h
);
2170 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2172 if (h
!= state
.sliceheight
) {
2175 state
.sliceheight
= h
;
2176 for (i
= 0; i
< state
.texcount
; ++i
) {
2177 state
.texowners
[i
].w
= -1;
2178 state
.texowners
[i
].h
= -1;
2179 state
.texowners
[i
].slice
= NULL
;
2183 else if (!strncmp ("interrupt", p
, 9)) {
2184 printd ("vmsg interrupted");
2187 errx (1, "unknown command %.*s", len
, p
);
2193 CAMLprim value
ml_realloctexts (value texcount_v
)
2195 CAMLparam1 (texcount_v
);
2198 if (trylock ("ml_realloctexts")) {
2202 realloctexts (Int_val (texcount_v
));
2204 unlock ("ml_realloctexts");
2207 CAMLreturn (Val_bool (ok
));
2210 static void recti (int x0
, int y0
, int x1
, int y1
)
2212 GLfloat
*v
= state
.vertices
;
2214 glVertexPointer (2, GL_FLOAT
, 0, v
);
2215 v
[0] = x0
; v
[1] = y0
;
2216 v
[2] = x1
; v
[3] = y0
;
2217 v
[4] = x0
; v
[5] = y1
;
2218 v
[6] = x1
; v
[7] = y1
;
2219 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2222 static void showsel (struct page
*page
, int ox
, int oy
)
2228 fz_page_block
*pageb
;
2229 fz_text_block
*block
;
2230 struct mark first
, last
;
2231 unsigned char selcolor
[] = {15,15,15,140};
2233 first
= page
->fmark
;
2236 if (!first
.span
|| !last
.span
) return;
2238 glEnable (GL_BLEND
);
2239 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2240 glColor4ubv (selcolor
);
2242 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2243 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2244 for (pageb
= page
->text
->blocks
;
2245 pageb
< page
->text
->blocks
+ page
->text
->len
;
2247 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2248 block
= pageb
->u
.text
;
2250 for (line
= block
->lines
;
2251 line
< block
->lines
+ block
->len
;
2254 rect
= fz_empty_rect
;
2256 for (span
= line
->first_span
; span
; span
= span
->next
) {
2258 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2263 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2265 j
= MIN (first
.i
, last
.i
);
2266 k
= MAX (first
.i
, last
.i
);
2269 if (span
== first
.span
) {
2273 else if (span
== last
.span
) {
2280 for (i
= j
; i
<= k
; ++i
) {
2282 fz_union_rect (&rect
,
2283 fz_text_char_bbox (&bbox
, span
, i
));
2285 fz_round_rect (&bbox
, &rect
);
2286 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2293 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2294 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2295 if (span
== last
.span
) {
2298 rect
= fz_empty_rect
;
2304 glDisable (GL_BLEND
);
2309 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2311 fz_matrix ctm
, tm
, pm
;
2312 fz_link
*link
, *links
;
2313 GLfloat
*texcoords
= state
.texcoords
;
2314 GLfloat
*vertices
= state
.vertices
;
2316 switch (page
->type
) {
2318 links
= page
->u
.pdfpage
->links
;
2322 links
= page
->u
.xpspage
->links
;
2329 glEnable (GL_TEXTURE_1D
);
2330 glEnable (GL_BLEND
);
2331 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2332 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2334 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2335 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2336 fz_translate (&tm
, xoff
, yoff
);
2337 pm
= pagectm (page
);
2338 fz_concat (&ctm
, &pm
, &tm
);
2340 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2341 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2343 for (link
= links
; link
; link
= link
->next
) {
2344 fz_point p1
, p2
, p3
, p4
;
2346 p1
.x
= link
->rect
.x0
;
2347 p1
.y
= link
->rect
.y0
;
2349 p2
.x
= link
->rect
.x1
;
2350 p2
.y
= link
->rect
.y0
;
2352 p3
.x
= link
->rect
.x1
;
2353 p3
.y
= link
->rect
.y1
;
2355 p4
.x
= link
->rect
.x0
;
2356 p4
.y
= link
->rect
.y1
;
2358 fz_transform_point (&p1
, &ctm
);
2359 fz_transform_point (&p2
, &ctm
);
2360 fz_transform_point (&p3
, &ctm
);
2361 fz_transform_point (&p4
, &ctm
);
2363 switch (link
->dest
.kind
) {
2364 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2365 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2366 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2367 default: glColor3ub (0, 0, 0); break;
2375 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2379 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2381 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2382 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2384 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2385 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2387 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2388 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2390 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2391 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2393 glDrawArrays (GL_LINES
, 0, 8);
2396 glDisable (GL_BLEND
);
2397 glDisable (GL_TEXTURE_1D
);
2400 static int compareslinks (const void *l
, const void *r
)
2402 struct slink
const *ls
= l
;
2403 struct slink
const *rs
= r
;
2404 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2405 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2407 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2410 static void droptext (struct page
*page
)
2413 fz_free_text_page (state
.ctx
, page
->text
);
2416 page
->fmark
.span
= NULL
;
2417 page
->lmark
.span
= NULL
;
2421 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2426 static void dropslinks (struct page
*page
)
2429 free (page
->slinks
);
2430 page
->slinks
= NULL
;
2431 page
->slinkcount
= 0;
2435 static void ensureslinks (struct page
*page
)
2439 size_t slinksize
= sizeof (*page
->slinks
);
2440 fz_link
*link
, *links
;
2442 if (state
.gen
!= page
->sgen
) {
2444 page
->sgen
= state
.gen
;
2446 if (page
->slinks
) return;
2448 switch (page
->type
) {
2450 links
= page
->u
.pdfpage
->links
;
2451 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2453 &state
.pagedims
[page
->pdimno
].tctm
,
2454 &state
.pagedims
[page
->pdimno
].ctm
);
2458 links
= page
->u
.xpspage
->links
;
2459 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2466 for (link
= links
; link
; link
= link
->next
) {
2470 page
->slinkcount
= count
;
2471 page
->slinks
= calloc (count
, slinksize
);
2472 if (!page
->slinks
) {
2473 err (1, "realloc slinks %d", count
);
2476 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2480 fz_transform_rect (&rect
, &ctm
);
2481 page
->slinks
[i
].link
= link
;
2482 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2484 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2488 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2489 static void fmt_linkn (char *s
, unsigned int u
)
2491 unsigned int len
; unsigned int q
;
2492 int zma
= 'z' - 'a' + 1;
2494 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2497 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2498 /* handles u == 0 */
2503 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2504 int noff
, char *targ
, int tlen
, int hfsize
)
2508 struct slink
*slink
;
2509 double x0
, y0
, x1
, y1
, w
;
2511 ensureslinks (page
);
2512 glColor3ub (0xc3, 0xb0, 0x91);
2513 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2514 fmt_linkn (buf
, i
+ noff
);
2515 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2516 slink
= &page
->slinks
[i
];
2518 x0
= slink
->bbox
.x0
+ xoff
- 5;
2519 y1
= slink
->bbox
.y0
+ yoff
- 5;
2520 y0
= y1
+ 10 + hfsize
;
2521 w
= measure_string (state
.face
, hfsize
, buf
);
2523 recti (x0
, y0
, x1
, y1
);
2527 glEnable (GL_BLEND
);
2528 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2529 glEnable (GL_TEXTURE_2D
);
2530 glColor3ub (0, 0, 0);
2531 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2532 fmt_linkn (buf
, i
+ noff
);
2533 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2534 slink
= &page
->slinks
[i
];
2536 x0
= slink
->bbox
.x0
+ xoff
;
2537 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2538 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2541 glDisable (GL_TEXTURE_2D
);
2542 glDisable (GL_BLEND
);
2545 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2548 struct slice
*slice1
;
2549 unsigned char *texdata
;
2552 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2553 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2555 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2556 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2557 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2561 int texindex
= state
.texindex
++ % state
.texcount
;
2563 if (state
.texowners
[texindex
].w
== tile
->w
) {
2564 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2568 state
.texowners
[texindex
].h
= slice
->h
;
2572 state
.texowners
[texindex
].h
= slice
->h
;
2575 state
.texowners
[texindex
].w
= tile
->w
;
2576 state
.texowners
[texindex
].slice
= slice
;
2577 slice
->texindex
= texindex
;
2579 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2581 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2585 texdata
= tile
->pixmap
->samples
;
2588 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2600 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2612 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2617 CAMLprim value
ml_begintiles (value unit_v
)
2619 CAMLparam1 (unit_v
);
2620 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2621 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2622 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2623 CAMLreturn (unit_v
);
2626 CAMLprim value
ml_endtiles (value unit_v
)
2628 CAMLparam1 (unit_v
);
2629 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2630 CAMLreturn (unit_v
);
2633 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2635 CAMLparam2 (args_v
, ptr_v
);
2636 int dispx
= Int_val (Field (args_v
, 0));
2637 int dispy
= Int_val (Field (args_v
, 1));
2638 int dispw
= Int_val (Field (args_v
, 2));
2639 int disph
= Int_val (Field (args_v
, 3));
2640 int tilex
= Int_val (Field (args_v
, 4));
2641 int tiley
= Int_val (Field (args_v
, 5));
2642 char *s
= String_val (ptr_v
);
2643 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2644 int slicey
, firstslice
;
2645 struct slice
*slice
;
2646 GLfloat
*texcoords
= state
.texcoords
;
2647 GLfloat
*vertices
= state
.vertices
;
2649 firstslice
= tiley
/ tile
->sliceheight
;
2650 slice
= &tile
->slices
[firstslice
];
2651 slicey
= tiley
% tile
->sliceheight
;
2656 dh
= slice
->h
- slicey
;
2657 dh
= MIN (disph
, dh
);
2658 uploadslice (tile
, slice
);
2660 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2661 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2662 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2663 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2665 vertices
[0] = dispx
; vertices
[1] = dispy
;
2666 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2667 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2668 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2670 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2674 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2677 CAMLreturn (Val_unit
);
2680 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2681 value xoff_v
, value yoff_v
,
2684 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2685 int xoff
= Int_val (xoff_v
);
2686 int yoff
= Int_val (yoff_v
);
2687 int noff
= Int_val (Field (li_v
, 0));
2688 char *targ
= String_val (Field (li_v
, 1));
2689 int tlen
= caml_string_length (Field (li_v
, 1));
2690 int hfsize
= Int_val (Field (li_v
, 2));
2691 char *s
= String_val (ptr_v
);
2692 int hlmask
= Int_val (hlinks_v
);
2693 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2696 /* deal with loadpage failed pages */
2700 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2701 if (trylock ("ml_postprocess")) {
2706 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2707 noff
= page
->slinkcount
;
2709 if (page
->tgen
== state
.gen
) {
2710 showsel (page
, xoff
, yoff
);
2712 unlock ("ml_postprocess");
2715 CAMLreturn (Val_int (noff
));
2718 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2722 const fz_matrix
*tctm
;
2723 fz_link
*link
, *links
;
2725 switch (page
->type
) {
2727 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2728 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2729 links
= page
->u
.pdfpage
->links
;
2733 tctm
= &fz_identity
;
2734 links
= page
->u
.xpspage
->links
;
2743 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2744 fz_invert_matrix (&ctm
, &ctm
);
2745 fz_transform_point (&p
, &ctm
);
2747 for (link
= links
; link
; link
= link
->next
) {
2748 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2749 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2757 static void ensuretext (struct page
*page
)
2759 if (state
.gen
!= page
->tgen
) {
2761 page
->tgen
= state
.gen
;
2767 page
->text
= fz_new_text_page (state
.ctx
);
2768 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2769 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2770 ctm
= pagectm (page
);
2771 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2772 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2773 qsort (page
->text
->blocks
, page
->text
->len
,
2774 sizeof (*page
->text
->blocks
), compareblocks
);
2776 fz_free_device (tdev
);
2780 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2782 CAMLparam2 (start_page_v
, dir_v
);
2784 int i
, dir
= Int_val (dir_v
);
2785 int start_page
= Int_val (start_page_v
);
2786 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2788 ret_v
= Val_int (0);
2789 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2793 lock ("ml_findpage_with_links");
2794 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2797 switch (state
.type
) {
2800 pdf_page
*page
= NULL
;
2802 fz_try (state
.ctx
) {
2803 page
= pdf_load_page (state
.u
.pdf
, i
);
2804 found
= !!page
->links
;
2806 fz_catch (state
.ctx
) {
2816 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2817 found
= !!page
->links
;
2823 ARSERT (0 && "invalid document type");
2827 ret_v
= caml_alloc_small (1, 1);
2828 Field (ret_v
, 0) = Val_int (i
);
2833 unlock ("ml_findpage_with_links");
2839 enum { dir_first
, dir_last
};
2840 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2842 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2844 CAMLparam2 (ptr_v
, dir_v
);
2845 CAMLlocal2 (ret_v
, pos_v
);
2847 int dirtag
, i
, slinkindex
;
2848 struct slink
*found
= NULL
,*slink
;
2849 char *s
= String_val (ptr_v
);
2851 page
= parse_pointer ("ml_findlink", s
);
2852 ret_v
= Val_int (0);
2853 if (trylock ("ml_findlink")) {
2857 ensureslinks (page
);
2859 if (Is_block (dir_v
)) {
2860 dirtag
= Tag_val (dir_v
);
2862 case dir_first_visible
:
2864 int x0
, y0
, dir
, first_index
, last_index
;
2866 pos_v
= Field (dir_v
, 0);
2867 x0
= Int_val (Field (pos_v
, 0));
2868 y0
= Int_val (Field (pos_v
, 1));
2869 dir
= Int_val (Field (pos_v
, 2));
2874 last_index
= page
->slinkcount
;
2877 first_index
= page
->slinkcount
- 1;
2881 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2882 slink
= &page
->slinks
[i
];
2883 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2892 slinkindex
= Int_val (Field (dir_v
, 0));
2893 found
= &page
->slinks
[slinkindex
];
2894 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2895 slink
= &page
->slinks
[i
];
2896 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2904 slinkindex
= Int_val (Field (dir_v
, 0));
2905 found
= &page
->slinks
[slinkindex
];
2906 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2907 slink
= &page
->slinks
[i
];
2908 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2916 slinkindex
= Int_val (Field (dir_v
, 0));
2917 found
= &page
->slinks
[slinkindex
];
2918 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2919 slink
= &page
->slinks
[i
];
2920 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2928 slinkindex
= Int_val (Field (dir_v
, 0));
2929 found
= &page
->slinks
[slinkindex
];
2930 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2931 slink
= &page
->slinks
[i
];
2932 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2941 dirtag
= Int_val (dir_v
);
2944 found
= page
->slinks
;
2949 found
= page
->slinks
+ (page
->slinkcount
- 1);
2955 ret_v
= caml_alloc_small (2, 1);
2956 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2959 unlock ("ml_findlink");
2964 enum { uuri
, ugoto
, utext
, uunexpected
,
2965 ulaunch
, unamed
, uremote
, uremotedest
};
2971 switch (link->dest.kind) { \
2972 case FZ_LINK_GOTO: \
2976 pageno = link->dest.ld.gotor.page; \
2980 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2981 p.y = link->dest.ld.gotor.lt.y; \
2982 fz_transform_point (&p, &pdim->lctm); \
2984 tup_v = caml_alloc_tuple (2); \
2985 ret_v = caml_alloc_small (1, ugoto); \
2986 Field (tup_v, 0) = Val_int (pageno); \
2987 Field (tup_v, 1) = Val_int (p.y); \
2988 Field (ret_v, 0) = tup_v; \
2993 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2994 ret_v = caml_alloc_small (1, uuri); \
2995 Field (ret_v, 0) = str_v; \
2998 case FZ_LINK_LAUNCH: \
2999 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3000 ret_v = caml_alloc_small (1, ulaunch); \
3001 Field (ret_v, 0) = str_v; \
3004 case FZ_LINK_NAMED: \
3005 str_v = caml_copy_string (link->dest.ld.named.named); \
3006 ret_v = caml_alloc_small (1, unamed); \
3007 Field (ret_v, 0) = str_v; \
3010 case FZ_LINK_GOTOR: \
3014 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3015 pageno = link->dest.ld.gotor.page; \
3016 if (pageno == -1) { \
3017 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3018 rty = uremotedest; \
3021 gr_v = Val_int (pageno); \
3024 tup_v = caml_alloc_tuple (2); \
3025 ret_v = caml_alloc_small (1, rty); \
3026 Field (tup_v, 0) = str_v; \
3027 Field (tup_v, 1) = gr_v; \
3028 Field (ret_v, 0) = tup_v; \
3036 snprintf (buf, sizeof (buf), \
3037 "unhandled link kind %d", link->dest.kind); \
3038 str_v = caml_copy_string (buf); \
3039 ret_v = caml_alloc_small (1, uunexpected); \
3040 Field (ret_v, 0) = str_v; \
3046 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3048 CAMLparam2 (ptr_v
, n_v
);
3049 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3052 struct pagedim
*pdim
;
3053 char *s
= String_val (ptr_v
);
3055 ret_v
= Val_int (0);
3056 if (trylock ("ml_getlink")) {
3060 page
= parse_pointer ("ml_getlink", s
);
3061 ensureslinks (page
);
3062 pdim
= &state
.pagedims
[page
->pdimno
];
3063 link
= page
->slinks
[Int_val (n_v
)].link
;
3066 unlock ("ml_getlink");
3071 CAMLprim value
ml_getlinkcount (value ptr_v
)
3075 char *s
= String_val (ptr_v
);
3077 page
= parse_pointer ("ml_getlinkcount", s
);
3078 CAMLreturn (Val_int (page
->slinkcount
));
3081 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3083 CAMLparam2 (ptr_v
, n_v
);
3086 struct slink
*slink
;
3087 char *s
= String_val (ptr_v
);
3089 page
= parse_pointer ("ml_getlinkrect", s
);
3090 ret_v
= caml_alloc_tuple (4);
3091 if (trylock ("ml_getlinkrect")) {
3092 Field (ret_v
, 0) = Val_int (0);
3093 Field (ret_v
, 1) = Val_int (0);
3094 Field (ret_v
, 2) = Val_int (0);
3095 Field (ret_v
, 3) = Val_int (0);
3098 ensureslinks (page
);
3100 slink
= &page
->slinks
[Int_val (n_v
)];
3101 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3102 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3103 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3104 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3105 unlock ("ml_getlinkrect");
3111 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3113 CAMLparam3 (ptr_v
, x_v
, y_v
);
3114 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3117 char *s
= String_val (ptr_v
);
3118 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3119 struct pagedim
*pdim
;
3121 ret_v
= Val_int (0);
3122 if (trylock ("ml_whatsunder")) {
3126 page
= parse_pointer ("ml_whatsunder", s
);
3127 pdim
= &state
.pagedims
[page
->pdimno
];
3128 x
+= pdim
->bounds
.x0
;
3129 y
+= pdim
->bounds
.y0
;
3130 link
= getlink (page
, x
, y
);
3136 fz_page_block
*pageb
;
3137 fz_text_block
*block
;
3140 for (pageb
= page
->text
->blocks
;
3141 pageb
< page
->text
->blocks
+ page
->text
->len
;
3144 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3145 block
= pageb
->u
.text
;
3148 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3151 for (line
= block
->lines
;
3152 line
< block
->lines
+ block
->len
;
3157 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3160 for (span
= line
->first_span
; span
; span
= span
->next
) {
3164 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3167 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3169 fz_text_char_bbox (&bbox
, span
, charnum
);
3172 if (x
>= b
->x0
&& x
<= b
->x1
3173 && y
>= b
->y0
&& y
<= b
->y1
) {
3174 fz_text_style
*style
= span
->text
->style
;
3176 style
->font
&& style
->font
->name
3178 : "Span has no font name"
3180 FT_FaceRec
*face
= style
->font
->ft_face
;
3181 if (face
&& face
->family_name
) {
3183 char *n1
= face
->family_name
;
3184 size_t l1
= strlen (n1
);
3185 size_t l2
= strlen (n2
);
3187 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3188 s
= malloc (l1
+ l2
+ 2);
3192 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3193 str_v
= caml_copy_string (s
);
3199 str_v
= caml_copy_string (n2
);
3201 ret_v
= caml_alloc_small (1, utext
);
3202 Field (ret_v
, 0) = str_v
;
3211 unlock ("ml_whatsunder");
3217 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3219 static int uninteresting (int c
)
3221 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3225 CAMLprim value
ml_clearmark (value ptr_v
)
3228 char *s
= String_val (ptr_v
);
3231 if (trylock ("ml_clearmark")) {
3235 page
= parse_pointer ("ml_clearmark", s
);
3236 page
->fmark
.span
= NULL
;
3237 page
->lmark
.span
= NULL
;
3241 unlock ("ml_clearmark");
3243 CAMLreturn (Val_unit
);
3246 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3248 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3253 fz_page_block
*pageb
;
3254 fz_text_block
*block
;
3255 struct pagedim
*pdim
;
3256 int mark
= Int_val (mark_v
);
3257 char *s
= String_val (ptr_v
);
3258 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3260 ret_v
= Val_bool (0);
3261 if (trylock ("ml_markunder")) {
3265 page
= parse_pointer ("ml_markunder", s
);
3266 pdim
= &state
.pagedims
[page
->pdimno
];
3270 if (mark
== mark_page
) {
3272 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3274 for (i
= 0; i
< page
->text
->len
; ++i
) {
3275 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3276 pb1
= &page
->text
->blocks
[i
];
3280 if (!pb1
) goto unlock
;
3282 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3283 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3284 pb2
= &page
->text
->blocks
[i
];
3288 if (!pb2
) goto unlock
;
3290 block
= pb1
->u
.text
;
3293 page
->fmark
.span
= block
->lines
->first_span
;
3295 block
= pb2
->u
.text
;
3296 line
= &block
->lines
[block
->len
- 1];
3297 page
->lmark
.i
= line
->last_span
->len
- 1;
3298 page
->lmark
.span
= line
->last_span
;
3299 ret_v
= Val_bool (1);
3303 x
+= pdim
->bounds
.x0
;
3304 y
+= pdim
->bounds
.y0
;
3306 for (pageb
= page
->text
->blocks
;
3307 pageb
< page
->text
->blocks
+ page
->text
->len
;
3309 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3310 block
= pageb
->u
.text
;
3313 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3316 if (mark
== mark_block
) {
3318 page
->fmark
.span
= block
->lines
->first_span
;
3320 line
= &block
->lines
[block
->len
- 1];
3321 page
->lmark
.i
= line
->last_span
->len
- 1;
3322 page
->lmark
.span
= line
->last_span
;
3323 ret_v
= Val_bool (1);
3327 for (line
= block
->lines
;
3328 line
< block
->lines
+ block
->len
;
3333 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3336 if (mark
== mark_line
) {
3338 page
->fmark
.span
= line
->first_span
;
3340 page
->lmark
.i
= line
->last_span
->len
- 1;
3341 page
->lmark
.span
= line
->last_span
;
3342 ret_v
= Val_bool (1);
3346 for (span
= line
->first_span
; span
; span
= span
->next
) {
3350 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3353 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3355 fz_text_char_bbox (&bbox
, span
, charnum
);
3358 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3360 int charnum2
, charnum3
= -1, charnum4
= -1;
3362 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3364 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3365 if (uninteresting (span
->text
[charnum2
].c
)) {
3366 charnum3
= charnum2
+ 1;
3370 if (charnum3
== -1) charnum3
= 0;
3372 for (charnum2
= charnum
+ 1;
3373 charnum2
< span
->len
;
3375 if (uninteresting (span
->text
[charnum2
].c
)) break;
3376 charnum4
= charnum2
;
3378 if (charnum4
== -1) goto unlock
;
3380 page
->fmark
.i
= charnum3
;
3381 page
->fmark
.span
= span
;
3383 page
->lmark
.i
= charnum4
;
3384 page
->lmark
.span
= span
;
3385 ret_v
= Val_bool (1);
3393 if (!Bool_val (ret_v
)) {
3394 page
->fmark
.span
= NULL
;
3395 page
->lmark
.span
= NULL
;
3399 unlock ("ml_markunder");
3405 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3407 CAMLparam3 (ptr_v
, x_v
, y_v
);
3408 CAMLlocal2 (ret_v
, res_v
);
3411 fz_page_block
*pageb
;
3412 struct pagedim
*pdim
;
3413 char *s
= String_val (ptr_v
);
3414 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3416 ret_v
= Val_int (0);
3417 if (trylock ("ml_rectofblock")) {
3421 page
= parse_pointer ("ml_rectofblock", s
);
3422 pdim
= &state
.pagedims
[page
->pdimno
];
3423 x
+= pdim
->bounds
.x0
;
3424 y
+= pdim
->bounds
.y0
;
3428 for (pageb
= page
->text
->blocks
;
3429 pageb
< page
->text
->blocks
+ page
->text
->len
;
3431 switch (pageb
->type
) {
3432 case FZ_PAGE_BLOCK_TEXT
:
3433 b
= &pageb
->u
.text
->bbox
;
3436 case FZ_PAGE_BLOCK_IMAGE
:
3437 b
= &pageb
->u
.image
->bbox
;
3444 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3449 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3450 ret_v
= caml_alloc_small (1, 1);
3451 Store_double_field (res_v
, 0, b
->x0
);
3452 Store_double_field (res_v
, 1, b
->x1
);
3453 Store_double_field (res_v
, 2, b
->y0
);
3454 Store_double_field (res_v
, 3, b
->y1
);
3455 Field (ret_v
, 0) = res_v
;
3457 unlock ("ml_rectofblock");
3463 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3465 CAMLparam2 (ptr_v
, rect_v
);
3468 struct pagedim
*pdim
;
3469 char *s
= String_val (ptr_v
);
3470 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3472 fz_page_block
*pageb
;
3473 fz_text_block
*block
;
3474 fz_text_span
*span
, *fspan
, *lspan
;
3476 if (trylock ("ml_seltext")) {
3480 page
= parse_pointer ("ml_seltext", s
);
3483 pdim
= &state
.pagedims
[page
->pdimno
];
3484 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3485 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3486 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3487 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3498 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3499 glColor3ub (128, 128, 128);
3500 recti (x0
, y0
, x1
, y1
);
3501 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3505 fspan
= page
->fmark
.span
;
3508 lspan
= page
->lmark
.span
;
3510 for (pageb
= page
->text
->blocks
;
3511 pageb
< page
->text
->blocks
+ page
->text
->len
;
3513 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3514 block
= pageb
->u
.text
;
3515 for (line
= block
->lines
;
3516 line
< block
->lines
+ block
->len
;
3520 for (span
= line
->first_span
; span
; span
= span
->next
) {
3521 for (i
= 0; i
< span
->len
; ++i
) {
3524 fz_text_char_bbox (&b
, span
, i
);
3526 if (x0
>= b
.x0
&& x0
<= b
.x1
3527 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3532 if (x1
>= b
.x0
&& x1
<= b
.x1
3533 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3538 if (0 && selected
) {
3539 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3540 glColor3ub (128, 128, 128);
3541 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3542 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3548 if (x1
< x0
&& fspan
== lspan
) {
3560 page
->fmark
.span
= fspan
;
3563 page
->lmark
.span
= lspan
;
3565 unlock ("ml_seltext");
3568 CAMLreturn (Val_unit
);
3571 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3576 for (i
= a
; i
<= b
; ++i
) {
3577 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3578 ret
= fwrite (buf
, len
, 1, f
);
3581 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3582 len
, ret
, strerror (errno
));
3590 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3592 caml_failwith ("ml_popen not implemented under Cygwin");
3595 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3597 CAMLparam2 (command_v
, fds_v
);
3598 CAMLlocal2 (l_v
, tup_v
);
3601 value earg_v
= Nothing
;
3602 posix_spawnattr_t attr
;
3603 posix_spawn_file_actions_t fa
;
3604 char *argv
[] = { "/bin/sh", "-c", String_val (command_v
), NULL
};
3606 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3607 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3610 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3611 msg
= "posix_spawnattr_init";
3615 #ifdef POSIX_SPAWN_USEVFORK
3616 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3617 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3622 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3625 tup_v
= Field (l_v
, 0);
3626 fd1
= Int_val (Field (tup_v
, 0));
3627 fd2
= Int_val (Field (tup_v
, 1));
3629 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3630 msg
= "posix_spawn_file_actions_addclose";
3636 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3637 msg
= "posix_spawn_file_actions_adddup2";
3644 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3645 msg
= "posix_spawn";
3650 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3651 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3655 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3656 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3661 unix_error (ret
, msg
, earg_v
);
3663 CAMLreturn (Val_unit
);
3667 CAMLprim value
ml_hassel (value ptr_v
)
3672 char *s
= String_val (ptr_v
);
3674 ret_v
= Val_bool (0);
3675 if (trylock ("ml_hassel")) {
3679 page
= parse_pointer ("ml_hassel", s
);
3680 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3681 unlock ("ml_hassel");
3686 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3688 CAMLparam2 (fd_v
, ptr_v
);
3693 fz_page_block
*pageb
;
3694 fz_text_block
*block
;
3695 int fd
= Int_val (fd_v
);
3696 char *s
= String_val (ptr_v
);
3698 if (trylock ("ml_copysel")) {
3702 page
= parse_pointer ("ml_copysel", s
);
3704 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3705 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3709 f
= fdopen (fd
, "w");
3711 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3712 fd
, strerror (errno
));
3716 for (pageb
= page
->text
->blocks
;
3717 pageb
< page
->text
->blocks
+ page
->text
->len
;
3719 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3720 block
= pageb
->u
.text
;
3721 for (line
= block
->lines
;
3722 line
< block
->lines
+ block
->len
;
3726 for (span
= line
->first_span
; span
; span
= span
->next
) {
3729 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3730 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3731 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3734 if (pipespan (f
, span
, a
, b
)) {
3737 if (span
== page
->lmark
.span
) {
3740 if (span
== line
->last_span
) {
3741 if (putc ('\n', f
) == EOF
) {
3743 "failed break line on sel pipe: %s\n",
3754 int ret
= fclose (f
);
3757 if (errno
!= ECHILD
) {
3758 fprintf (stderr
, "failed to close sel pipe: %s\n",
3764 unlock ("ml_copysel");
3769 fprintf (stderr
, "failed to close sel pipe: %s\n",
3773 CAMLreturn (Val_unit
);
3776 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3778 CAMLparam1 (pagedimno_v
);
3780 int pagedimno
= Int_val (pagedimno_v
);
3783 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3784 if (trylock ("ml_getpdimrect")) {
3785 box
= fz_empty_rect
;
3788 box
= state
.pagedims
[pagedimno
].mediabox
;
3789 unlock ("ml_getpdimrect");
3792 Store_double_field (ret_v
, 0, box
.x0
);
3793 Store_double_field (ret_v
, 1, box
.x1
);
3794 Store_double_field (ret_v
, 2, box
.y0
);
3795 Store_double_field (ret_v
, 3, box
.y1
);
3800 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3801 value dw_v
, value cols_v
)
3803 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3809 double winw
= Int_val (winw_v
);
3810 double winh
= Int_val (winh_v
);
3811 double dw
= Int_val (dw_v
);
3812 double cols
= Int_val (cols_v
);
3813 double pw
= 1.0, ph
= 1.0;
3815 if (trylock ("ml_zoom_for_height")) {
3819 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3820 double w
= p
->pagebox
.x1
/ cols
;
3821 double h
= p
->pagebox
.y1
;
3825 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3827 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3830 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3831 unlock ("ml_zoom_for_height");
3833 ret_v
= caml_copy_double (zoom
);
3837 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3839 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3841 int pt
= Int_val(pt_v
);
3842 int x
= Int_val (x_v
);
3843 int y
= Int_val (y_v
);
3846 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3847 ret_v
= caml_copy_double (w
);
3851 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3853 CAMLparam2 (pt_v
, string_v
);
3855 int pt
= Int_val (pt_v
);
3858 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3859 ret_v
= caml_copy_double (w
);
3863 CAMLprim value
ml_getpagebox (value opaque_v
)
3865 CAMLparam1 (opaque_v
);
3871 char *s
= String_val (opaque_v
);
3872 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3874 ret_v
= caml_alloc_tuple (4);
3875 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3876 dev
->hints
|= FZ_IGNORE_SHADE
;
3878 switch (page
->type
) {
3880 ctm
= pagectm (page
);
3881 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3885 ctm
= pagectm (page
);
3886 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3890 rect
= fz_infinite_rect
;
3894 fz_free_device (dev
);
3895 fz_round_rect (&bbox
, &rect
);
3896 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3897 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3898 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3899 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3904 CAMLprim value
ml_setaalevel (value level_v
)
3906 CAMLparam1 (level_v
);
3908 state
.aalevel
= Int_val (level_v
);
3909 CAMLreturn (Val_unit
);
3913 #include <X11/Xlib.h>
3919 GLXDrawable drawable
;
3922 #include "keysym2ucs.c"
3924 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3926 CAMLparam1 (keysym_v
);
3928 KeySym keysym
= Int_val (keysym_v
);
3933 rune
= keysym2ucs (keysym
);
3934 len
= fz_runetochar (buf
, rune
);
3936 str_v
= caml_copy_string (buf
);
3940 CAMLprim value
ml_glx (value win_v
)
3943 XVisualInfo
*visual
;
3944 int screen
, wid
= Int_val (win_v
);
3945 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3947 glx
.dpy
= XOpenDisplay (NULL
);
3949 caml_failwith ("XOpenDisplay");
3952 screen
= DefaultScreen (glx
.dpy
);
3953 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
3955 XCloseDisplay (glx
.dpy
);
3957 caml_failwith ("glXChooseVisual");
3960 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
3963 XCloseDisplay (glx
.dpy
);
3965 caml_failwith ("glXCreateContext");
3968 if (!glXMakeCurrent (glx
.dpy
, wid
, glx
.ctx
)) {
3969 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3970 XCloseDisplay (glx
.dpy
);
3973 caml_failwith ("glXMakeCurrent");
3976 CAMLreturn (Val_unit
);
3979 CAMLprim value
ml_swapb (value unit_v
)
3981 CAMLparam1 (unit_v
);
3982 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
3983 CAMLreturn (Val_unit
);
3986 CAMLprim value
ml_glxsync (value unit_v
)
3988 CAMLparam1 (unit_v
);
3989 if (glx
.dpy
&& glx
.ctx
) {
3993 CAMLreturn (Val_unit
);
3996 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
3997 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
3999 CAMLprim value
ml_platform (value unit_v
)
4001 CAMLparam1 (unit_v
);
4002 int platid
= piunknown
;
4004 #if defined __linux__
4006 #elif defined __CYGWIN__
4008 #elif defined __DragonFly__
4009 platid
= pidragonflybsd
;
4010 #elif defined __FreeBSD__
4012 #elif defined __OpenBSD__
4014 #elif defined __NetBSD__
4016 #elif defined __sun__
4018 #elif defined __APPLE__
4021 CAMLreturn (Val_int (platid
));
4024 CAMLprim value
ml_cloexec (value fd_v
)
4027 int fd
= Int_val (fd_v
);
4029 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4030 uerror ("fcntl", Nothing
);
4032 CAMLreturn (Val_unit
);
4035 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4037 CAMLparam2 (w_v
, h_v
);
4040 int w
= Int_val (w_v
);
4041 int h
= Int_val (h_v
);
4042 int cs
= Int_val (cs_v
);
4044 if (state
.pbo_usable
) {
4045 pbo
= calloc (sizeof (*pbo
), 1);
4047 err (1, "calloc pbo");
4059 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4062 state
.glGenBuffersARB (1, &pbo
->id
);
4063 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4064 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4065 NULL
, GL_STREAM_DRAW
);
4066 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4068 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4070 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4071 state
.glDeleteBuffersARB (1, &pbo
->id
);
4073 ret_v
= caml_copy_string ("0");
4079 res
= snprintf (NULL
, 0, "%" FMT_ptr
, pbo
);
4081 err (1, "snprintf %" FMT_ptr
" failed", pbo
);
4085 err (1, "malloc %d bytes failed", res
+1);
4087 res
= sprintf (s
, "%" FMT_ptr
, pbo
);
4089 err (1, "sprintf %" FMT_ptr
" failed", pbo
);
4091 ret_v
= caml_copy_string (s
);
4096 ret_v
= caml_copy_string ("0");
4101 CAMLprim value
ml_freepbo (value s_v
)
4104 char *s
= String_val (s_v
);
4105 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4108 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4110 tile
->pbo
->ptr
= NULL
;
4111 tile
->pbo
->size
= -1;
4113 CAMLreturn (Val_unit
);
4116 CAMLprim value
ml_unmappbo (value s_v
)
4119 char *s
= String_val (s_v
);
4120 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4123 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4124 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4125 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4127 tile
->pbo
->ptr
= NULL
;
4128 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4130 CAMLreturn (Val_unit
);
4133 static void setuppbo (void)
4135 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4136 state
.pbo_usable
= GGPA (glBindBufferARB
)
4137 && GGPA (glUnmapBufferARB
)
4138 && GGPA (glMapBufferARB
)
4139 && GGPA (glBufferDataARB
)
4140 && GGPA (glGenBuffersARB
)
4141 && GGPA (glDeleteBuffersARB
);
4145 CAMLprim value
ml_pbo_usable (value unit_v
)
4147 CAMLparam1 (unit_v
);
4148 CAMLreturn (Val_bool (state
.pbo_usable
));
4151 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4153 CAMLparam3 (ptr_v
, x_v
, y_v
);
4154 CAMLlocal2 (ret_v
, tup_v
);
4156 char *s
= String_val (ptr_v
);
4157 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4158 struct pagedim
*pdim
;
4162 page
= parse_pointer ("ml_unproject", s
);
4163 pdim
= &state
.pagedims
[page
->pdimno
];
4165 ret_v
= Val_int (0);
4166 if (trylock ("ml_unproject")) {
4170 switch (page
->type
) {
4172 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4178 p
.x
= x
+ pdim
->bounds
.x0
;
4179 p
.y
= y
+ pdim
->bounds
.y0
;
4181 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4182 fz_invert_matrix (&ctm
, &ctm
);
4183 fz_transform_point (&p
, &ctm
);
4185 tup_v
= caml_alloc_tuple (2);
4186 ret_v
= caml_alloc_small (1, 1);
4187 Field (tup_v
, 0) = Val_int (p
.x
);
4188 Field (tup_v
, 1) = Val_int (p
.y
);
4189 Field (ret_v
, 0) = tup_v
;
4191 unlock ("ml_unproject");
4196 static void makestippletex (void)
4198 const char pixels
[] = "\xff\xff\0\0";
4199 glGenTextures (1, &state
.stid
);
4200 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4201 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4202 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4215 CAMLprim value
ml_fz_version (value unit_v
)
4217 return caml_copy_string (FZ_VERSION
);
4220 #ifdef USE_FONTCONFIG
4226 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4230 int needs_exact_metrics
)
4237 FcPattern
*pat
, *pat1
;
4239 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4240 name
, bold
, italic
, needs_exact_metrics
);
4243 fc
.config
= FcInitLoadConfigAndFonts ();
4245 lprintf ("FcInitLoadConfigAndFonts failed\n");
4249 if (!fc
.config
) return NULL
;
4251 size
= strlen (name
);
4252 if (bold
) size
+= sizeof (":bold") - 1;
4253 if (italic
) size
+= sizeof (":italic") - 1;
4256 buf
= malloc (size
);
4258 err (1, "malloc %zu failed", size
);
4262 if (bold
&& italic
) {
4263 strcat (buf
, ":bold:italic");
4266 if (bold
) strcat (buf
, ":bold");
4267 if (italic
) strcat (buf
, ":italic");
4269 for (i
= 0; i
< size
; ++i
) {
4270 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4273 lprintf ("fcbuf=%s\n", buf
);
4274 pat
= FcNameParse ((FcChar8
*) buf
);
4276 printd ("emsg FcNameParse failed\n");
4281 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4282 printd ("emsg FcConfigSubstitute failed\n");
4286 FcDefaultSubstitute (pat
);
4288 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4290 printd ("emsg FcFontMatch failed\n");
4291 FcPatternDestroy (pat
);
4296 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4297 printd ("emsg FcPatternGetString failed\n");
4298 FcPatternDestroy (pat
);
4299 FcPatternDestroy (pat1
);
4305 printd ("emsg name=%s path=%s\n", name
, path
);
4307 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4308 FcPatternDestroy (pat
);
4309 FcPatternDestroy (pat1
);
4315 CAMLprim value
ml_init (value pipe_v
, value params_v
)
4317 CAMLparam2 (pipe_v
, params_v
);
4318 CAMLlocal2 (trim_v
, fuzz_v
);
4325 struct sigaction sa
;
4327 state
.cr
= Int_val (Field (pipe_v
, 0));
4328 state
.cw
= Int_val (Field (pipe_v
, 1));
4329 state
.rotate
= Int_val (Field (params_v
, 0));
4330 state
.fitmodel
= Int_val (Field (params_v
, 1));
4331 trim_v
= Field (params_v
, 2);
4332 texcount
= Int_val (Field (params_v
, 3));
4333 state
.sliceheight
= Int_val (Field (params_v
, 4));
4334 mustoresize
= Int_val (Field (params_v
, 5));
4335 colorspace
= Int_val (Field (params_v
, 6));
4336 fontpath
= String_val (Field (params_v
, 7));
4338 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4339 if (!state
.trimcachepath
) {
4340 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4343 haspboext
= Bool_val (Field (params_v
, 9));
4345 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4347 #ifdef USE_FONTCONFIG
4348 fz_install_load_system_font_funcs (
4349 state
.ctx
, fc_load_system_font_func
, NULL
4353 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4354 fuzz_v
= Field (trim_v
, 1);
4355 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4356 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4357 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4358 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4360 set_tex_params (colorspace
);
4363 #ifndef USE_FONTCONFIG
4364 state
.face
= load_font (fontpath
);
4368 char *buf
= fontpath
;
4369 FcPattern
*pat
, *pat1
;
4372 fc
.config
= FcInitLoadConfigAndFonts ();
4374 errx (1, "FcInitLoadConfigAndFonts failed");
4377 pat
= FcNameParse ((FcChar8
*) buf
);
4379 errx (1, "FcNameParse failed");
4382 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4383 errx (1, "FcConfigSubstitute failed");
4385 FcDefaultSubstitute (pat
);
4387 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4389 errx (1, "FcFontMatch failed");
4392 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4393 errx (1, "FcPatternGetString failed");
4396 state
.face
= load_font ((char *) path
);
4397 FcPatternDestroy (pat
);
4398 FcPatternDestroy (pat1
);
4403 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4405 state
.face
= load_builtin_font (base
, len
);
4407 if (!state
.face
) _exit (1);
4409 realloctexts (texcount
);
4418 sa
.sa_handler
= SIG_IGN
;
4419 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4421 sa
.sa_handler
= SIG_DFL
;
4422 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4424 if (sigemptyset (&sa
.sa_mask
)) {
4425 err (1, "sigemptyset");
4427 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4428 err (1, "sigaction");
4431 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4433 errx (1, "pthread_create: %s", strerror (ret
));
4436 CAMLreturn (Val_unit
);