1 /* lots of code c&p-ed directly from mupdf */
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
15 #include <sys/utsname.h>
18 #include <cygwin/socket.h> /* FIONREAD */
30 #include <caml/fail.h>
31 #include <caml/alloc.h>
32 #include <caml/memory.h>
33 #include <caml/unixsupport.h>
35 #include <mupdf/fitz.h>
36 #include <mupdf/cbz.h>
37 #include <mupdf/pdf.h>
38 #include <mupdf/xps.h>
39 #include <mupdf/img.h>
42 #include FT_FREETYPE_H
45 #include <fontconfig/fontconfig.h>
49 #define SUMATRA_LOOKUP
52 extern char **environ
;
55 #define MIN(a,b) ((a) < (b) ? (a) : (b))
56 #define MAX(a,b) ((a) > (b) ? (a) : (b))
59 #define NORETURN_ATTR __attribute__ ((noreturn))
60 #define UNUSED_ATTR __attribute__ ((unused))
61 #if !defined __clang__
62 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
64 #define OPTIMIZE_ATTR(n)
66 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
70 #define OPTIMIZE_ATTR(n)
71 #define GCC_FMT_ATTR(a, b)
76 #define FMT_ptr PRIxPTR
77 #define SCN_ptr SCNxPTR
78 #define FMT_ptr_cast(p) ((uintptr_t) (p))
79 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
81 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
82 err (int exitcode
, const char *fmt
, ...)
89 vfprintf (stderr
, fmt
, ap
);
91 fprintf (stderr
, ": %s\n", strerror (savederrno
));
96 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
97 errx (int exitcode
, const char *fmt
, ...)
102 vfprintf (stderr
, fmt
, ap
);
104 fputc ('\n', stderr
);
109 #ifndef GL_TEXTURE_RECTANGLE_ARB
110 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
114 #define GL_BGRA 0x80E1
117 #ifndef GL_UNSIGNED_INT_8_8_8_8
118 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
121 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
122 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
126 #define lprintf printf
131 #define ARSERT(cond) for (;;) { \
133 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
149 struct slice slices
[1];
160 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
168 enum { DPDF
, DXPS
, DCBZ
, DIMG
};
177 fz_text_sheet
*sheet
;
185 fz_display_list
*dlist
;
187 struct slink
*slinks
;
192 void (*freepage
) (void *);
198 struct pagedim
*pagedims
;
218 fz_colorspace
*colorspace
;
226 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
240 void (*closedoc
) (void);
241 void (*freepage
) (void *);
249 void (*glBindBufferARB
) (GLenum
, GLuint
);
250 GLboolean (*glUnmapBufferARB
) (GLenum
);
251 void *(*glMapBufferARB
) (GLenum
, GLenum
);
252 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
253 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
254 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
256 GLfloat texcoords
[8];
257 GLfloat vertices
[16];
259 #ifdef SUMATRA_LOOKUP
274 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
276 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
279 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
281 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
284 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
286 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
287 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
290 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
292 static void lock (const char *cap
)
294 int ret
= pthread_mutex_lock (&mutex
);
296 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
300 static void unlock (const char *cap
)
302 int ret
= pthread_mutex_unlock (&mutex
);
304 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
308 static int trylock (const char *cap
)
310 int ret
= pthread_mutex_trylock (&mutex
);
311 if (ret
&& ret
!= EBUSY
) {
312 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
317 static void *parse_pointer (const char *cap
, const char *s
)
322 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
324 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
329 static double now (void)
333 if (gettimeofday (&tv
, NULL
)) {
334 err (1, "gettimeofday");
336 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
339 static int hasdata (void)
342 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
343 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
347 CAMLprim value
ml_hasdata (value fd_v
)
352 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
353 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
354 CAMLreturn (Val_bool (avail
> 0));
357 static void readdata (void *p
, int size
)
362 n
= read (state
.csock
, p
, size
);
364 if (errno
== EINTR
) goto again
;
365 err (1, "read (req %d, ret %zd)", size
, n
);
368 if (!n
) errx (1, "EOF while reading");
369 errx (1, "read (req %d, ret %zd)", size
, n
);
373 static void writedata (char *p
, int size
)
377 p
[0] = (size
>> 24) & 0xff;
378 p
[1] = (size
>> 16) & 0xff;
379 p
[2] = (size
>> 8) & 0xff;
380 p
[3] = (size
>> 0) & 0xff;
382 n
= write (state
.csock
, p
, size
+ 4);
384 if (!n
) errx (1, "EOF while writing data");
385 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
389 static int readlen (void)
394 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
397 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
405 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
408 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
412 if (len
< size
- 4) {
413 writedata (buf
, len
);
419 err (1, "vsnprintf for `%s' failed", fmt
);
421 buf
= realloc (buf
, size
);
426 static void closepdf (void)
428 #ifdef SUMATRA_LOOKUP
429 if (state
.pdflut
.objs
) {
432 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
433 pdf_drop_obj (state
.pdflut
.objs
[i
]);
435 free (state
.pdflut
.objs
);
436 state
.pdflut
.objs
= NULL
;
441 pdf_close_document (state
.u
.pdf
);
446 static void closexps (void)
449 xps_close_document (state
.u
.xps
);
454 static void closecbz (void)
457 cbz_close_document (state
.u
.cbz
);
462 static void closeimg (void)
465 image_close_document (state
.u
.img
);
470 static void freepdfpage (void *ptr
)
472 pdf_free_page (state
.u
.pdf
, ptr
);
475 static void freeemptyxxxpage (void *ptr
)
480 static void freexpspage (void *ptr
)
482 xps_free_page (state
.u
.xps
, ptr
);
485 static void freecbzpage (void *ptr
)
487 cbz_free_page (state
.u
.cbz
, ptr
);
490 static void freeimgpage (void *ptr
)
492 image_free_page (state
.u
.img
, ptr
);
495 static void openxref (char *filename
, char *password
)
499 for (i
= 0; i
< state
.texcount
; ++i
) {
500 state
.texowners
[i
].w
= -1;
501 state
.texowners
[i
].slice
= NULL
;
504 if (state
.closedoc
) state
.closedoc ();
506 len
= strlen (filename
);
524 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
525 int len2
= strlen (tbl
[i
].ext
);
527 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
528 state
.type
= tbl
[i
].type
;
534 if (state
.pagedims
) {
535 free (state
.pagedims
);
536 state
.pagedims
= NULL
;
538 state
.pagedimcount
= 0;
540 fz_set_aa_level (state
.ctx
, state
.aalevel
);
541 switch (state
.type
) {
543 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
544 if (pdf_needs_password (state
.u
.pdf
)) {
545 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
547 errx (1, "invalid password");
550 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
551 state
.closedoc
= closepdf
;
552 state
.freepage
= freepdfpage
;
556 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
557 state
.pagecount
= xps_count_pages (state
.u
.xps
);
558 state
.closedoc
= closexps
;
559 state
.freepage
= freexpspage
;
563 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
564 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
565 state
.closedoc
= closecbz
;
566 state
.freepage
= freecbzpage
;
570 state
.u
.img
= image_open_document (state
.ctx
, filename
);
572 state
.closedoc
= closeimg
;
573 state
.freepage
= freeimgpage
;
578 static void pdfinfo (void)
580 if (state
.type
== DPDF
) {
583 printd ("info PDF version\t%d.%d",
584 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
586 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
590 char *items
[] = { "Title", "Author", "Creator",
591 "Producer", "CreationDate" };
593 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
594 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
595 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
598 printd ("title %s", s
);
600 printd ("info %s\t%s", items
[i
], s
);
602 fz_free (state
.ctx
, s
);
609 static void unlinktile (struct tile
*tile
)
613 for (i
= 0; i
< tile
->slicecount
; ++i
) {
614 struct slice
*s
= &tile
->slices
[i
];
616 if (s
->texindex
!= -1) {
617 if (state
.texowners
[s
->texindex
].slice
== s
) {
618 state
.texowners
[s
->texindex
].slice
= NULL
;
624 static void freepage (struct page
*page
)
627 fz_free_text_page (state
.ctx
, page
->text
);
630 fz_free_text_sheet (state
.ctx
, page
->sheet
);
635 fz_drop_display_list (state
.ctx
, page
->dlist
);
636 page
->freepage (page
->u
.ptr
);
640 static void freetile (struct tile
*tile
)
645 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
648 fz_drop_pixmap (state
.ctx
, state
.pig
);
650 state
.pig
= tile
->pixmap
;
655 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
664 static int cacheline32bytes
;
666 static void __attribute__ ((constructor
)) clcheck (void)
668 char **envp
= environ
;
673 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
675 cacheline32bytes
= auxv
[1] == 32;
681 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
683 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
684 if (cacheline32bytes
&& size
> 32) {
685 intptr_t a1
, a2
, diff
;
687 vector
unsigned char v
= vec_splat_u8 (-1);
688 vector
unsigned char *p
;
690 a1
= a2
= (intptr_t) pixmap
->samples
;
691 a2
= (a1
+ 31) & ~31;
696 while (a1
!= a2
) *(char *) a1
++ = 0xff;
697 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
698 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
700 vec_st (v
, i
+ 16, p
);
702 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
704 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
707 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
710 static void trimctm (pdf_page
*page
, int pindex
)
713 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
715 if (!pdim
->tctmready
) {
716 if (state
.trimmargins
) {
718 fz_matrix rm
, sm
, tm
, im
, ctm1
;
720 fz_rotate (&rm
, -pdim
->rotate
);
721 fz_scale (&sm
, 1, -1);
722 fz_concat (&ctm
, &rm
, &sm
);
723 realbox
= pdim
->mediabox
;
724 fz_transform_rect (&realbox
, &ctm
);
725 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
726 fz_concat (&ctm1
, &ctm
, &tm
);
727 fz_invert_matrix (&im
, &page
->ctm
);
728 fz_concat (&ctm
, &im
, &ctm1
);
738 static fz_matrix
pagectm (struct page
*page
)
742 if (page
->type
== DPDF
) {
743 trimctm (page
->u
.pdfpage
, page
->pdimno
);
745 &state
.pagedims
[page
->pdimno
].tctm
,
746 &state
.pagedims
[page
->pdimno
].ctm
);
749 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
751 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
752 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
757 static void *loadpage (int pageno
, int pindex
)
762 page
= calloc (sizeof (struct page
), 1);
764 err (1, "calloc page %d", pageno
);
767 page
->dlist
= fz_new_display_list (state
.ctx
);
768 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
770 switch (state
.type
) {
772 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
773 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
775 page
->freepage
= freepdfpage
;
779 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
780 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
782 page
->freepage
= freexpspage
;
786 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
787 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
789 page
->freepage
= freecbzpage
;
793 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
794 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
796 page
->freepage
= freeimgpage
;
800 fz_catch (state
.ctx
) {
802 page
->freepage
= freeemptyxxxpage
;
804 fz_free_device (dev
);
806 page
->pdimno
= pindex
;
807 page
->pageno
= pageno
;
808 page
->sgen
= state
.gen
;
809 page
->tgen
= state
.gen
;
810 page
->type
= state
.type
;
815 static struct tile
*alloctile (int h
)
822 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
823 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
824 tile
= calloc (tilesize
, 1);
826 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
828 for (i
= 0; i
< slicecount
; ++i
) {
829 int sh
= MIN (h
, state
.sliceheight
);
830 tile
->slices
[i
].h
= sh
;
831 tile
->slices
[i
].texindex
= -1;
834 tile
->slicecount
= slicecount
;
835 tile
->sliceheight
= state
.sliceheight
;
845 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
846 const fz_matrix
*ctm
, float alpha
)
848 struct obs
*obs
= dev
->user
;
850 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
852 fz_rect rect
= fz_unit_rect
;
854 fz_transform_rect (&rect
, ctm
);
855 fz_round_rect (&b
, &rect
);
856 fz_intersect_irect (&b
, &obs
->b
);
857 obs
->cured
= b
.x0
== obs
->b
.x0
860 && b
.y1
== obs
->b
.y1
;
864 static int obscured (struct page
*page
, fz_irect bbox
)
871 memset (&dev
, 0, sizeof (dev
));
872 memset (&obs
, 0, sizeof (obs
));
877 dev
.fill_image
= obs_fill_image
;
879 fz_rect_from_irect (&rect
, &bbox
);
880 ctm
= pagectm (page
);
881 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
884 #define OBSCURED obscured
886 #define OBSCURED(a, b) 0
889 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
897 struct pagedim
*pdim
;
899 tile
= alloctile (h
);
900 pdim
= &state
.pagedims
[page
->pdimno
];
905 bbox
.x1
= bbox
.x0
+ w
;
906 bbox
.y1
= bbox
.y0
+ h
;
909 if (state
.pig
->w
== w
911 && state
.pig
->colorspace
== state
.colorspace
) {
912 tile
->pixmap
= state
.pig
;
913 tile
->pixmap
->x
= bbox
.x0
;
914 tile
->pixmap
->y
= bbox
.y0
;
917 fz_drop_pixmap (state
.ctx
, state
.pig
);
924 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
930 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
936 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
937 clearpixmap (tile
->pixmap
);
939 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
940 ctm
= pagectm (page
);
941 fz_rect_from_irect (&rect
, &bbox
);
942 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
943 fz_free_device (dev
);
948 #ifdef SUMATRA_LOOKUP
949 /* Following two functions are taken (almost) verbatim from SumatraPDF
953 pdf_load_page_objs_rec(pdf_document
*doc
, pdf_obj
*node
, int *page_no
, pdf_obj
**page_objs
)
955 fz_context
*ctx
= doc
->ctx
;
957 if (pdf_mark_obj(node
))
958 fz_throw(ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
961 pdf_obj
*kids
= pdf_dict_gets(node
, "Kids");
962 int len
= pdf_array_len(kids
), i
;
963 for (i
= 0; i
< len
; i
++) {
964 pdf_obj
*kid
= pdf_array_get(kids
, i
);
965 char *type
= pdf_to_name(pdf_dict_gets(kid
, "Type"));
966 if (!strcmp(type
, "Page")) {
967 if (*page_no
> pdf_count_pages(doc
))
968 fz_throw(ctx
, FZ_ERROR_GENERIC
, "found more /Page objects than anticipated");
969 page_objs
[*page_no
- 1] = pdf_keep_obj(kid
);
972 else if (!strcmp(type
, "Pages")) {
973 int count
= pdf_to_int(pdf_dict_gets(kid
, "Count"));
975 pdf_load_page_objs_rec(doc
, kid
, page_no
, page_objs
);
978 fz_warn(ctx
, "non-page object in page tree (%s)", type
);
983 pdf_unmark_obj(node
);
991 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
993 pdf_obj
*pages
= pdf_dict_getp(pdf_trailer(doc
), "Root/Pages");
995 pdf_load_page_objs_rec(doc
, pages
, &page_no
, page_objs
);
999 static void initpdims (void)
1003 fz_rect rootmediabox
;
1004 int pageno
, trim
, show
;
1005 int trimw
= 0, cxcount
;
1009 if (state
.trimmargins
&& state
.trimcachepath
) {
1010 trimf
= fopen (state
.trimcachepath
, "rb");
1012 trimf
= fopen (state
.trimcachepath
, "wb");
1017 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1018 cxcount
= state
.pagecount
;
1020 cxcount
= MIN (state
.pagecount
, 1);
1022 if (state
.type
== DPDF
) {
1024 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1025 "Root/Pages/MediaBox");
1026 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1029 #ifdef SUMATRA_LOOKUP
1030 if (state
.type
== DPDF
1031 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1032 state
.pdflut
.objs
= malloc (sizeof (*state
.pdflut
.objs
) * cxcount
);
1033 if (!state
.pdflut
.objs
) {
1034 err (1, "malloc pageobjs %zu %d %zu failed",
1035 sizeof (*state
.pdflut
.objs
), cxcount
,
1036 sizeof (*state
.pdflut
.objs
) * cxcount
);
1038 state
.pdflut
.count
= cxcount
;
1039 pdf_load_page_objs (state
.u
.pdf
, state
.pdflut
.objs
);
1040 state
.pdflut
.pdf
= state
.u
.pdf
;
1044 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1049 switch (state
.type
) {
1051 pdf_document
*pdf
= state
.u
.pdf
;
1052 pdf_obj
*pageref
, *pageobj
;
1054 #ifdef SUMATRA_LOOKUP
1055 pageref
= state
.pdflut
.objs
[pageno
];
1057 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1059 pageobj
= pdf_resolve_indirect (pageref
);
1061 if (state
.trimmargins
) {
1065 fz_try (state
.ctx
) {
1066 page
= pdf_load_page (pdf
, pageno
);
1067 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1068 trim
= state
.trimanew
|| !obj
;
1074 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1075 dev
->hints
|= FZ_IGNORE_SHADE
;
1076 fz_invert_matrix (&ctm
, &page
->ctm
);
1077 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1078 fz_free_device (dev
);
1080 rect
.x0
+= state
.trimfuzz
.x0
;
1081 rect
.x1
+= state
.trimfuzz
.x1
;
1082 rect
.y0
+= state
.trimfuzz
.y0
;
1083 rect
.y1
+= state
.trimfuzz
.y1
;
1084 fz_transform_rect (&rect
, &ctm
);
1085 fz_intersect_rect (&rect
, &page
->mediabox
);
1087 if (fz_is_empty_rect (&rect
)) {
1088 mediabox
= page
->mediabox
;
1094 obj
= pdf_new_array (pdf
, 4);
1095 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1096 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1097 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1098 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1099 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1102 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1103 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1104 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1105 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1108 rotate
= page
->rotate
;
1109 pdf_free_page (pdf
, page
);
1111 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1113 printd ("progress %f Trimming %d",
1114 (double) (pageno
+ 1) / state
.pagecount
,
1118 fz_catch (state
.ctx
) {
1119 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1126 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1128 if (fz_is_empty_rect (&mediabox
)) {
1136 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1138 if (!fz_is_empty_rect (&cropbox
)) {
1143 fz_intersect_rect (&mediabox
, &cropbox
);
1148 if (fz_is_empty_rect (&rootmediabox
)) {
1150 "cannot find page size for page %d\n",
1154 mediabox
= rootmediabox
;
1158 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1167 fz_try (state
.ctx
) {
1168 page
= xps_load_page (state
.u
.xps
, pageno
);
1169 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1171 if (state
.trimmargins
) {
1175 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1176 dev
->hints
|= FZ_IGNORE_SHADE
;
1177 xps_run_page (state
.u
.xps
, page
, dev
,
1178 &fz_identity
, NULL
);
1179 fz_free_device (dev
);
1181 rect
.x0
+= state
.trimfuzz
.x0
;
1182 rect
.x1
+= state
.trimfuzz
.x1
;
1183 rect
.y0
+= state
.trimfuzz
.y0
;
1184 rect
.y1
+= state
.trimfuzz
.y1
;
1185 fz_intersect_rect (&rect
, &mediabox
);
1187 if (!fz_is_empty_rect (&rect
)) {
1191 xps_free_page (state
.u
.xps
, page
);
1193 printd ("progress %f loading %d",
1194 (double) (pageno
+ 1) / state
.pagecount
,
1198 fz_catch (state
.ctx
) {
1210 if (state
.trimmargins
&& trimw
) {
1213 fz_try (state
.ctx
) {
1214 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1215 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1216 cbz_free_page (state
.u
.cbz
, page
);
1217 printd ("progress %f Trimming %d",
1218 (double) (pageno
+ 1) / state
.pagecount
,
1221 fz_catch (state
.ctx
) {
1222 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1225 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1227 err (1, "fwrite trim mediabox");
1233 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1235 err (1, "fread trim mediabox");
1241 fz_try (state
.ctx
) {
1242 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1243 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1244 cbz_free_page (state
.u
.cbz
, page
);
1246 fz_catch (state
.ctx
) {
1247 fprintf (stderr
, "failed to load cbz page\n");
1265 fz_try (state
.ctx
) {
1266 page
= image_load_page (state
.u
.img
, pageno
);
1267 image_bound_page (state
.u
.img
, page
, &mediabox
);
1268 image_free_page (state
.u
.img
, page
);
1270 fz_catch (state
.ctx
) {
1271 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1277 ARSERT (0 && state
.type
);
1280 if (state
.pagedimcount
== 0
1281 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1282 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1285 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1286 state
.pagedims
= realloc (state
.pagedims
, size
);
1287 if (!state
.pagedims
) {
1288 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1289 size
, state
.pagedimcount
+ 1);
1292 p
= &state
.pagedims
[state
.pagedimcount
++];
1294 p
->mediabox
= mediabox
;
1299 if (state
.trimmargins
) {
1300 printd ("progress 1 Trimmed %d pages in %f seconds",
1301 state
.pagecount
, end
- start
);
1304 printd ("vmsg Processed %d pages in %f seconds",
1305 state
.pagecount
, end
- start
);
1309 if (fclose (trimf
)) {
1315 static void layout (void)
1320 struct pagedim
*p
= p
;
1321 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1323 if (state
.pagedimcount
== 0) return;
1325 switch (state
.fitmodel
) {
1326 case FitProportional
:
1327 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1330 p
= &state
.pagedims
[pindex
];
1331 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1333 fz_transform_rect (&box
, &rm
);
1335 x0
= MIN (box
.x0
, box
.x1
);
1336 x1
= MAX (box
.x0
, box
.x1
);
1339 maxw
= MAX (w
, maxw
);
1340 zoom
= state
.w
/ maxw
;
1352 ARSERT (0 && state
.fitmodel
);
1355 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1359 p
= &state
.pagedims
[pindex
];
1360 fz_rotate (&ctm
, state
.rotate
);
1361 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1363 fz_transform_rect (&box
, &rm
);
1364 w
= box
.x1
- box
.x0
;
1365 switch (state
.fitmodel
) {
1366 case FitProportional
:
1367 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1373 h
= box
.y1
- box
.y0
;
1375 zoom
= MIN (zw
, zh
);
1376 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1385 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1386 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1388 fz_rotate (&rm
, p
->rotate
);
1389 p
->pagebox
= p
->mediabox
;
1390 fz_transform_rect (&p
->pagebox
, &rm
);
1391 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1392 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1396 fz_transform_rect (&rect
, &ctm
);
1397 fz_round_rect (&p
->bounds
, &rect
);
1400 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1401 fz_scale (&sm
, zoom
, -zoom
);
1402 fz_concat (&ctm
, &tm
, &sm
);
1403 fz_concat (&p
->lctm
, &ctm
, &rm
);
1409 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1410 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1411 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1412 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1416 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1417 } while (p
-- != state
.pagedims
);
1421 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1422 desttoanchor (fz_link_dest
*dest
)
1426 struct pagedim
*pdim
= state
.pagedims
;
1431 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1432 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1434 pdim
= &state
.pagedims
[i
];
1436 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1438 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1439 p
.x
= dest
->ld
.gotor
.lt
.x
;
1442 p
.y
= dest
->ld
.gotor
.lt
.y
;
1443 fz_transform_point (&p
, &pdim
->lctm
);
1447 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1448 double x0
, x1
, y0
, y1
;
1450 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1451 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1453 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1454 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1456 a
.n
= dest
->ld
.gotor
.page
;
1461 static void recurse_outline (fz_outline
*outline
, int level
)
1464 switch (outline
->dest
.kind
) {
1467 struct anchor a
= desttoanchor (&outline
->dest
);
1470 printd ("o %d %d %d %d %s",
1471 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1477 printd ("ou %d %" FMT_s
" %s %s", level
,
1478 strlen (outline
->title
), outline
->title
,
1479 outline
->dest
.ld
.uri
.uri
);
1483 printd ("on %d %s", level
, outline
->title
);
1487 printd ("emsg Unhandled outline kind %d for %s\n",
1488 outline
->dest
.kind
, outline
->title
);
1491 if (outline
->down
) {
1492 recurse_outline (outline
->down
, level
+ 1);
1494 outline
= outline
->next
;
1498 static void process_outline (void)
1500 fz_outline
*outline
;
1502 if (!state
.needoutline
) return;
1504 state
.needoutline
= 0;
1505 switch (state
.type
) {
1507 outline
= pdf_load_outline (state
.u
.pdf
);
1510 outline
= xps_load_outline (state
.u
.xps
);
1517 recurse_outline (outline
, 0);
1518 fz_free_outline (state
.ctx
, outline
);
1522 static char *strofspan (fz_text_span
*span
)
1527 size_t size
= 0, cap
= 80;
1529 p
= malloc (cap
+ 1);
1530 if (!p
) return NULL
;
1532 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1533 int n
= fz_runetochar (utf8
, ch
->c
);
1534 if (size
+ n
> cap
) {
1536 p
= realloc (p
, cap
+ 1);
1537 if (!p
) return NULL
;
1540 memcpy (p
+ size
, utf8
, n
);
1547 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1548 fz_text_span
*span
, fz_matrix ctm
,
1549 int stop
, int pageno
, double start
)
1556 fz_point p1
, p2
, p3
, p4
;
1558 p
= strofspan (span
);
1561 ret
= regexec (re
, p
, 1, &rm
, 0);
1564 if (ret
!= REG_NOMATCH
) {
1567 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1568 printd ("msg regexec error `%.*s'",
1569 (int) size
, errbuf
);
1577 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1578 c
+= fz_runelen (span
->text
[a
].c
);
1580 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1581 c
+= fz_runelen (span
->text
[b
].c
);
1584 if (fz_runelen (span
->text
[b
].c
) > 1) {
1588 fz_text_char_bbox (&sb
, span
, a
);
1589 fz_text_char_bbox (&eb
, span
, b
);
1601 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1608 printd ("progress 1 found at %d `%.*s' in %f sec",
1609 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1613 printd ("match %d %d %f %f %f %f %f %f %f %f",
1625 static int compareblocks (const void *l
, const void *r
)
1627 fz_text_block
const *ls
= l
;
1628 fz_text_block
const* rs
= r
;
1629 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1632 /* wishful thinking function */
1633 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1638 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1640 fz_text_sheet
*sheet
;
1641 struct pagedim
*pdim
, *pdimprev
;
1642 int stop
= 0, niters
= 0;
1645 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1649 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1650 if (niters
++ == 5) {
1653 printd ("progress 1 attention requested aborting search at %d",
1658 printd ("progress %f searching in page %d",
1659 (double) (pageno
+ 1) / state
.pagecount
,
1664 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1665 pdim
= &state
.pagedims
[i
];
1666 if (pdim
->pageno
== pageno
) {
1669 if (pdim
->pageno
> pageno
) {
1678 sheet
= fz_new_text_sheet (state
.ctx
);
1679 text
= fz_new_text_page (state
.ctx
);
1680 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1682 switch (state
.type
) {
1685 fz_try (state
.ctx
) {
1686 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1687 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1688 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1689 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1690 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1693 fz_catch (state
.ctx
) {
1694 fz_free_device (tdev
);
1701 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1702 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1706 ARSERT (0 && state
.type
);
1709 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1710 fz_free_device (tdev
);
1712 for (j
= 0; j
< text
->len
; ++j
) {
1715 fz_text_block
*block
;
1717 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1718 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1721 for (k
= 0; k
< block
->len
; ++k
) {
1726 line
= &block
->lines
[k
];
1727 if (line
->bbox
.y0
< y
+ 1) continue;
1730 line
= &block
->lines
[block
->len
- 1 - k
];
1731 if (line
->bbox
.y0
> y
- 1) continue;
1734 for (span
= line
->first_span
; span
; span
= span
->next
) {
1735 switch (matchspan (re
, text
, span
, ctm
,
1736 stop
, pageno
, start
)) {
1738 case 1: stop
= 1; break;
1739 case -1: stop
= 1; goto endloop
;
1754 fz_free_text_page (state
.ctx
, text
);
1755 fz_free_text_sheet (state
.ctx
, sheet
);
1757 state
.freepage (u
.ptr
);
1762 printd ("progress 1 no matches %f sec", end
- start
);
1764 printd ("clearrects");
1767 static void set_tex_params (int colorspace
)
1774 switch (colorspace
) {
1776 state
.texiform
= GL_RGBA8
;
1777 state
.texform
= GL_RGBA
;
1778 state
.texty
= GL_UNSIGNED_BYTE
;
1779 state
.colorspace
= fz_device_rgb (state
.ctx
);
1782 state
.texiform
= GL_RGBA8
;
1783 state
.texform
= GL_BGRA
;
1784 state
.texty
= endianness
.s
> 1
1785 ? GL_UNSIGNED_INT_8_8_8_8
1786 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1787 state
.colorspace
= fz_device_bgr (state
.ctx
);
1790 state
.texiform
= GL_LUMINANCE_ALPHA
;
1791 state
.texform
= GL_LUMINANCE_ALPHA
;
1792 state
.texty
= GL_UNSIGNED_BYTE
;
1793 state
.colorspace
= fz_device_gray (state
.ctx
);
1796 errx (1, "invalid colorspce %d", colorspace
);
1800 static void realloctexts (int texcount
)
1804 if (texcount
== state
.texcount
) return;
1806 if (texcount
< state
.texcount
) {
1807 glDeleteTextures (state
.texcount
- texcount
,
1808 state
.texids
+ texcount
);
1811 size
= texcount
* sizeof (*state
.texids
);
1812 state
.texids
= realloc (state
.texids
, size
);
1813 if (!state
.texids
) {
1814 err (1, "realloc texids %" FMT_s
, size
);
1817 size
= texcount
* sizeof (*state
.texowners
);
1818 state
.texowners
= realloc (state
.texowners
, size
);
1819 if (!state
.texowners
) {
1820 err (1, "realloc texowners %" FMT_s
, size
);
1822 if (texcount
> state
.texcount
) {
1825 glGenTextures (texcount
- state
.texcount
,
1826 state
.texids
+ state
.texcount
);
1827 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1828 state
.texowners
[i
].w
= -1;
1829 state
.texowners
[i
].slice
= NULL
;
1832 state
.texcount
= texcount
;
1836 static char *mbtoutf8 (char *s
)
1842 len
= mbstowcs (NULL
, s
, strlen (s
));
1847 if (len
== (size_t) -1) {
1852 tmp
= malloc (len
* sizeof (wchar_t));
1857 ret
= mbstowcs (tmp
, s
, len
);
1858 if (ret
== (size_t) -1) {
1864 for (i
= 0; i
< ret
; ++i
) {
1865 len
+= fz_runelen (tmp
[i
]);
1868 p
= r
= malloc (len
+ 1);
1874 for (i
= 0; i
< ret
; ++i
) {
1875 p
+= fz_runetochar (p
, tmp
[i
]);
1882 CAMLprim value
ml_mbtoutf8 (value s_v
)
1888 s
= String_val (s_v
);
1894 ret_v
= caml_copy_string (r
);
1900 static void * mainloop (void *unused
)
1903 int len
, ret
, oldlen
= 0;
1908 errx (1, "readlen returned 0");
1911 if (oldlen
< len
+ 1) {
1912 p
= realloc (p
, len
+ 1);
1914 err (1, "realloc %d failed", len
+ 1);
1921 if (!strncmp ("open", p
, 4)) {
1922 int wthack
, off
, ok
= 0;
1928 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1930 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1933 filename
= p
+ 5 + off
;
1934 filenamelen
= strlen (filename
);
1935 password
= filename
+ filenamelen
+ 1;
1938 fz_try (state
.ctx
) {
1939 openxref (filename
, password
);
1942 fz_catch (state
.ctx
) {
1943 utf8filename
= mbtoutf8 (filename
);
1944 printd ("msg Could not open %s", utf8filename
);
1954 utf8filename
= mbtoutf8 (filename
);
1955 printd ("msg Opened %s (press h/F1 to get help)",
1957 if (utf8filename
!= filename
) {
1958 free (utf8filename
);
1961 state
.needoutline
= 1;
1964 else if (!strncmp ("cs", p
, 2)) {
1967 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1969 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1972 set_tex_params (colorspace
);
1973 for (i
= 0; i
< state
.texcount
; ++i
) {
1974 state
.texowners
[i
].w
= -1;
1975 state
.texowners
[i
].slice
= NULL
;
1979 else if (!strncmp ("freepage", p
, 8)) {
1982 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1984 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1988 else if (!strncmp ("freetile", p
, 8)) {
1991 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1993 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1997 else if (!strncmp ("search", p
, 6)) {
1998 int icase
, pageno
, y
, ret
, len2
, forward
;
2002 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2003 &icase
, &pageno
, &y
, &forward
, &len2
);
2005 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2008 pattern
= p
+ 6 + len2
;
2009 ret
= regcomp (&re
, pattern
,
2010 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2015 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2016 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2019 search (&re
, pageno
, y
, forward
);
2023 else if (!strncmp ("geometry", p
, 8)) {
2027 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2029 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2037 for (i
= 0; i
< state
.texcount
; ++i
) {
2038 state
.texowners
[i
].slice
= NULL
;
2041 state
.fitmodel
= fitmodel
;
2046 unlock ("geometry");
2047 printd ("continue %d", state
.pagecount
);
2049 else if (!strncmp ("reqlayout", p
, 9)) {
2051 int rotate
, fitmodel
, off
, h
;
2054 ret
= sscanf (p
+ 9, " %d %d %d %n",
2055 &rotate
, &fitmodel
, &h
, &off
);
2057 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2060 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2063 state
.rotate
= rotate
;
2064 state
.fitmodel
= fitmodel
;
2069 nameddest
= p
+ 9 + off
;
2070 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2073 pdf_obj
*needle
, *obj
;
2075 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2076 strlen (nameddest
));
2077 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2079 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2081 a
= desttoanchor (&dest
);
2083 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2086 printd ("emsg failed to parse destination `%s'\n",
2091 printd ("emsg destination `%s' not found\n",
2094 pdf_drop_obj (needle
);
2098 unlock ("reqlayout");
2099 printd ("continue %d", state
.pagecount
);
2101 else if (!strncmp ("page", p
, 4)) {
2104 int pageno
, pindex
, ret
;
2106 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2108 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2113 page
= loadpage (pageno
, pindex
);
2117 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2119 else if (!strncmp ("tile", p
, 4)) {
2120 int x
, y
, w
, h
, ret
;
2126 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2127 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2128 SCN_ptr_cast (&data
));
2130 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2135 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2139 printd ("tile %d %d %" FMT_ptr
" %u %f",
2141 FMT_ptr_cast (tile
),
2142 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2145 else if (!strncmp ("settrim", p
, 7)) {
2149 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2150 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2152 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2156 state
.trimmargins
= trimmargins
;
2157 state
.needoutline
= 1;
2158 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2160 state
.trimfuzz
= fuzz
;
2162 state
.pagedimcount
= 0;
2163 free (state
.pagedims
);
2164 state
.pagedims
= NULL
;
2169 printd ("continue %d", state
.pagecount
);
2171 else if (!strncmp ("sliceh", p
, 6)) {
2174 ret
= sscanf (p
+ 6, " %d", &h
);
2176 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2178 if (h
!= state
.sliceheight
) {
2181 state
.sliceheight
= h
;
2182 for (i
= 0; i
< state
.texcount
; ++i
) {
2183 state
.texowners
[i
].w
= -1;
2184 state
.texowners
[i
].h
= -1;
2185 state
.texowners
[i
].slice
= NULL
;
2189 else if (!strncmp ("interrupt", p
, 9)) {
2190 printd ("vmsg interrupted");
2193 errx (1, "unknown command %.*s", len
, p
);
2199 CAMLprim value
ml_realloctexts (value texcount_v
)
2201 CAMLparam1 (texcount_v
);
2204 if (trylock ("ml_realloctexts")) {
2208 realloctexts (Int_val (texcount_v
));
2210 unlock ("ml_realloctexts");
2213 CAMLreturn (Val_bool (ok
));
2216 static void recti (int x0
, int y0
, int x1
, int y1
)
2218 GLfloat
*v
= state
.vertices
;
2220 glVertexPointer (2, GL_FLOAT
, 0, v
);
2221 v
[0] = x0
; v
[1] = y0
;
2222 v
[2] = x1
; v
[3] = y0
;
2223 v
[4] = x0
; v
[5] = y1
;
2224 v
[6] = x1
; v
[7] = y1
;
2225 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2228 static void showsel (struct page
*page
, int ox
, int oy
)
2234 fz_page_block
*pageb
;
2235 fz_text_block
*block
;
2236 struct mark first
, last
;
2237 unsigned char selcolor
[] = {15,15,15,140};
2239 first
= page
->fmark
;
2242 if (!first
.span
|| !last
.span
) return;
2244 glEnable (GL_BLEND
);
2245 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2246 glColor4ubv (selcolor
);
2248 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2249 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2250 for (pageb
= page
->text
->blocks
;
2251 pageb
< page
->text
->blocks
+ page
->text
->len
;
2253 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2254 block
= pageb
->u
.text
;
2256 for (line
= block
->lines
;
2257 line
< block
->lines
+ block
->len
;
2260 rect
= fz_empty_rect
;
2262 for (span
= line
->first_span
; span
; span
= span
->next
) {
2264 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2269 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2271 j
= MIN (first
.i
, last
.i
);
2272 k
= MAX (first
.i
, last
.i
);
2275 if (span
== first
.span
) {
2279 else if (span
== last
.span
) {
2286 for (i
= j
; i
<= k
; ++i
) {
2288 fz_union_rect (&rect
,
2289 fz_text_char_bbox (&bbox
, span
, i
));
2291 fz_round_rect (&bbox
, &rect
);
2292 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2299 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2300 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2301 if (span
== last
.span
) {
2304 rect
= fz_empty_rect
;
2310 glDisable (GL_BLEND
);
2315 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2317 fz_matrix ctm
, tm
, pm
;
2318 fz_link
*link
, *links
;
2319 GLfloat
*texcoords
= state
.texcoords
;
2320 GLfloat
*vertices
= state
.vertices
;
2322 switch (page
->type
) {
2324 links
= page
->u
.pdfpage
->links
;
2328 links
= page
->u
.xpspage
->links
;
2335 glEnable (GL_TEXTURE_1D
);
2336 glEnable (GL_BLEND
);
2337 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2338 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2340 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2341 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2342 fz_translate (&tm
, xoff
, yoff
);
2343 pm
= pagectm (page
);
2344 fz_concat (&ctm
, &pm
, &tm
);
2346 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2347 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2349 for (link
= links
; link
; link
= link
->next
) {
2350 fz_point p1
, p2
, p3
, p4
;
2352 p1
.x
= link
->rect
.x0
;
2353 p1
.y
= link
->rect
.y0
;
2355 p2
.x
= link
->rect
.x1
;
2356 p2
.y
= link
->rect
.y0
;
2358 p3
.x
= link
->rect
.x1
;
2359 p3
.y
= link
->rect
.y1
;
2361 p4
.x
= link
->rect
.x0
;
2362 p4
.y
= link
->rect
.y1
;
2364 fz_transform_point (&p1
, &ctm
);
2365 fz_transform_point (&p2
, &ctm
);
2366 fz_transform_point (&p3
, &ctm
);
2367 fz_transform_point (&p4
, &ctm
);
2369 switch (link
->dest
.kind
) {
2370 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2371 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2372 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2373 default: glColor3ub (0, 0, 0); break;
2381 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2385 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2387 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2388 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2390 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2391 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2393 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2394 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2396 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2397 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2399 glDrawArrays (GL_LINES
, 0, 8);
2402 glDisable (GL_BLEND
);
2403 glDisable (GL_TEXTURE_1D
);
2406 static int compareslinks (const void *l
, const void *r
)
2408 struct slink
const *ls
= l
;
2409 struct slink
const *rs
= r
;
2410 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2411 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2413 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2416 static void droptext (struct page
*page
)
2419 fz_free_text_page (state
.ctx
, page
->text
);
2422 page
->fmark
.span
= NULL
;
2423 page
->lmark
.span
= NULL
;
2427 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2432 static void dropslinks (struct page
*page
)
2435 free (page
->slinks
);
2436 page
->slinks
= NULL
;
2437 page
->slinkcount
= 0;
2441 static void ensureslinks (struct page
*page
)
2445 size_t slinksize
= sizeof (*page
->slinks
);
2446 fz_link
*link
, *links
;
2448 if (state
.gen
!= page
->sgen
) {
2450 page
->sgen
= state
.gen
;
2452 if (page
->slinks
) return;
2454 switch (page
->type
) {
2456 links
= page
->u
.pdfpage
->links
;
2457 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2459 &state
.pagedims
[page
->pdimno
].tctm
,
2460 &state
.pagedims
[page
->pdimno
].ctm
);
2464 links
= page
->u
.xpspage
->links
;
2465 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2472 for (link
= links
; link
; link
= link
->next
) {
2476 page
->slinkcount
= count
;
2477 page
->slinks
= calloc (count
, slinksize
);
2478 if (!page
->slinks
) {
2479 err (1, "realloc slinks %d", count
);
2482 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2486 fz_transform_rect (&rect
, &ctm
);
2487 page
->slinks
[i
].link
= link
;
2488 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2490 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2494 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2495 static void fmt_linkn (char *s
, unsigned int u
)
2497 unsigned int len
; unsigned int q
;
2498 int zma
= 'z' - 'a' + 1;
2500 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2503 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2504 /* handles u == 0 */
2509 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2510 int noff
, char *targ
, int tlen
, int hfsize
)
2514 struct slink
*slink
;
2515 double x0
, y0
, x1
, y1
, w
;
2517 ensureslinks (page
);
2518 glColor3ub (0xc3, 0xb0, 0x91);
2519 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2520 fmt_linkn (buf
, i
+ noff
);
2521 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2522 slink
= &page
->slinks
[i
];
2524 x0
= slink
->bbox
.x0
+ xoff
- 5;
2525 y1
= slink
->bbox
.y0
+ yoff
- 5;
2526 y0
= y1
+ 10 + hfsize
;
2527 w
= measure_string (state
.face
, hfsize
, buf
);
2529 recti (x0
, y0
, x1
, y1
);
2533 glEnable (GL_BLEND
);
2534 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2535 glEnable (GL_TEXTURE_2D
);
2536 glColor3ub (0, 0, 0);
2537 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2538 fmt_linkn (buf
, i
+ noff
);
2539 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2540 slink
= &page
->slinks
[i
];
2542 x0
= slink
->bbox
.x0
+ xoff
;
2543 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2544 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2547 glDisable (GL_TEXTURE_2D
);
2548 glDisable (GL_BLEND
);
2551 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2554 struct slice
*slice1
;
2555 unsigned char *texdata
;
2558 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2559 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2561 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2562 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2563 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2567 int texindex
= state
.texindex
++ % state
.texcount
;
2569 if (state
.texowners
[texindex
].w
== tile
->w
) {
2570 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2574 state
.texowners
[texindex
].h
= slice
->h
;
2578 state
.texowners
[texindex
].h
= slice
->h
;
2581 state
.texowners
[texindex
].w
= tile
->w
;
2582 state
.texowners
[texindex
].slice
= slice
;
2583 slice
->texindex
= texindex
;
2585 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2587 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2591 texdata
= tile
->pixmap
->samples
;
2594 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2606 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2618 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2623 CAMLprim value
ml_begintiles (value unit_v
)
2625 CAMLparam1 (unit_v
);
2626 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2627 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2628 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2629 CAMLreturn (unit_v
);
2632 CAMLprim value
ml_endtiles (value unit_v
)
2634 CAMLparam1 (unit_v
);
2635 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2636 CAMLreturn (unit_v
);
2639 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2641 CAMLparam2 (args_v
, ptr_v
);
2642 int dispx
= Int_val (Field (args_v
, 0));
2643 int dispy
= Int_val (Field (args_v
, 1));
2644 int dispw
= Int_val (Field (args_v
, 2));
2645 int disph
= Int_val (Field (args_v
, 3));
2646 int tilex
= Int_val (Field (args_v
, 4));
2647 int tiley
= Int_val (Field (args_v
, 5));
2648 char *s
= String_val (ptr_v
);
2649 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2650 int slicey
, firstslice
;
2651 struct slice
*slice
;
2652 GLfloat
*texcoords
= state
.texcoords
;
2653 GLfloat
*vertices
= state
.vertices
;
2655 firstslice
= tiley
/ tile
->sliceheight
;
2656 slice
= &tile
->slices
[firstslice
];
2657 slicey
= tiley
% tile
->sliceheight
;
2662 dh
= slice
->h
- slicey
;
2663 dh
= MIN (disph
, dh
);
2664 uploadslice (tile
, slice
);
2666 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2667 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2668 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2669 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2671 vertices
[0] = dispx
; vertices
[1] = dispy
;
2672 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2673 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2674 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2676 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2680 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2683 CAMLreturn (Val_unit
);
2686 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2687 value xoff_v
, value yoff_v
,
2690 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2691 int xoff
= Int_val (xoff_v
);
2692 int yoff
= Int_val (yoff_v
);
2693 int noff
= Int_val (Field (li_v
, 0));
2694 char *targ
= String_val (Field (li_v
, 1));
2695 int tlen
= caml_string_length (Field (li_v
, 1));
2696 int hfsize
= Int_val (Field (li_v
, 2));
2697 char *s
= String_val (ptr_v
);
2698 int hlmask
= Int_val (hlinks_v
);
2699 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2702 /* deal with loadpage failed pages */
2706 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2707 if (trylock ("ml_postprocess")) {
2712 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2713 noff
= page
->slinkcount
;
2715 if (page
->tgen
== state
.gen
) {
2716 showsel (page
, xoff
, yoff
);
2718 unlock ("ml_postprocess");
2721 CAMLreturn (Val_int (noff
));
2724 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2728 const fz_matrix
*tctm
;
2729 fz_link
*link
, *links
;
2731 switch (page
->type
) {
2733 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2734 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2735 links
= page
->u
.pdfpage
->links
;
2739 tctm
= &fz_identity
;
2740 links
= page
->u
.xpspage
->links
;
2749 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2750 fz_invert_matrix (&ctm
, &ctm
);
2751 fz_transform_point (&p
, &ctm
);
2753 for (link
= links
; link
; link
= link
->next
) {
2754 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2755 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2763 static void ensuretext (struct page
*page
)
2765 if (state
.gen
!= page
->tgen
) {
2767 page
->tgen
= state
.gen
;
2773 page
->text
= fz_new_text_page (state
.ctx
);
2774 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2775 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2776 ctm
= pagectm (page
);
2777 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2778 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2779 qsort (page
->text
->blocks
, page
->text
->len
,
2780 sizeof (*page
->text
->blocks
), compareblocks
);
2782 fz_free_device (tdev
);
2786 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2788 CAMLparam2 (start_page_v
, dir_v
);
2790 int i
, dir
= Int_val (dir_v
);
2791 int start_page
= Int_val (start_page_v
);
2792 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2794 ret_v
= Val_int (0);
2795 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2799 lock ("ml_findpage_with_links");
2800 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2803 switch (state
.type
) {
2806 pdf_page
*page
= NULL
;
2808 fz_try (state
.ctx
) {
2809 page
= pdf_load_page (state
.u
.pdf
, i
);
2810 found
= !!page
->links
;
2812 fz_catch (state
.ctx
) {
2822 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2823 found
= !!page
->links
;
2829 ARSERT (0 && "invalid document type");
2833 ret_v
= caml_alloc_small (1, 1);
2834 Field (ret_v
, 0) = Val_int (i
);
2839 unlock ("ml_findpage_with_links");
2845 enum { dir_first
, dir_last
};
2846 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2848 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2850 CAMLparam2 (ptr_v
, dir_v
);
2851 CAMLlocal2 (ret_v
, pos_v
);
2853 int dirtag
, i
, slinkindex
;
2854 struct slink
*found
= NULL
,*slink
;
2855 char *s
= String_val (ptr_v
);
2857 page
= parse_pointer ("ml_findlink", s
);
2858 ret_v
= Val_int (0);
2859 if (trylock ("ml_findlink")) {
2863 ensureslinks (page
);
2865 if (Is_block (dir_v
)) {
2866 dirtag
= Tag_val (dir_v
);
2868 case dir_first_visible
:
2870 int x0
, y0
, dir
, first_index
, last_index
;
2872 pos_v
= Field (dir_v
, 0);
2873 x0
= Int_val (Field (pos_v
, 0));
2874 y0
= Int_val (Field (pos_v
, 1));
2875 dir
= Int_val (Field (pos_v
, 2));
2880 last_index
= page
->slinkcount
;
2883 first_index
= page
->slinkcount
- 1;
2887 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2888 slink
= &page
->slinks
[i
];
2889 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2898 slinkindex
= Int_val (Field (dir_v
, 0));
2899 found
= &page
->slinks
[slinkindex
];
2900 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2901 slink
= &page
->slinks
[i
];
2902 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2910 slinkindex
= Int_val (Field (dir_v
, 0));
2911 found
= &page
->slinks
[slinkindex
];
2912 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2913 slink
= &page
->slinks
[i
];
2914 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2922 slinkindex
= Int_val (Field (dir_v
, 0));
2923 found
= &page
->slinks
[slinkindex
];
2924 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2925 slink
= &page
->slinks
[i
];
2926 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2934 slinkindex
= Int_val (Field (dir_v
, 0));
2935 found
= &page
->slinks
[slinkindex
];
2936 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2937 slink
= &page
->slinks
[i
];
2938 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2947 dirtag
= Int_val (dir_v
);
2950 found
= page
->slinks
;
2955 found
= page
->slinks
+ (page
->slinkcount
- 1);
2961 ret_v
= caml_alloc_small (2, 1);
2962 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2965 unlock ("ml_findlink");
2970 enum { uuri
, ugoto
, utext
, uunexpected
,
2971 ulaunch
, unamed
, uremote
, uremotedest
};
2977 switch (link->dest.kind) { \
2978 case FZ_LINK_GOTO: \
2982 pageno = link->dest.ld.gotor.page; \
2986 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2987 p.y = link->dest.ld.gotor.lt.y; \
2988 fz_transform_point (&p, &pdim->lctm); \
2989 if (p.y < 0) p.y = 0; \
2991 tup_v = caml_alloc_tuple (2); \
2992 ret_v = caml_alloc_small (1, ugoto); \
2993 Field (tup_v, 0) = Val_int (pageno); \
2994 Field (tup_v, 1) = Val_int (p.y); \
2995 Field (ret_v, 0) = tup_v; \
3000 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3001 ret_v = caml_alloc_small (1, uuri); \
3002 Field (ret_v, 0) = str_v; \
3005 case FZ_LINK_LAUNCH: \
3006 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3007 ret_v = caml_alloc_small (1, ulaunch); \
3008 Field (ret_v, 0) = str_v; \
3011 case FZ_LINK_NAMED: \
3012 str_v = caml_copy_string (link->dest.ld.named.named); \
3013 ret_v = caml_alloc_small (1, unamed); \
3014 Field (ret_v, 0) = str_v; \
3017 case FZ_LINK_GOTOR: \
3021 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3022 pageno = link->dest.ld.gotor.page; \
3023 if (pageno == -1) { \
3024 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3025 rty = uremotedest; \
3028 gr_v = Val_int (pageno); \
3031 tup_v = caml_alloc_tuple (2); \
3032 ret_v = caml_alloc_small (1, rty); \
3033 Field (tup_v, 0) = str_v; \
3034 Field (tup_v, 1) = gr_v; \
3035 Field (ret_v, 0) = tup_v; \
3043 snprintf (buf, sizeof (buf), \
3044 "unhandled link kind %d", link->dest.kind); \
3045 str_v = caml_copy_string (buf); \
3046 ret_v = caml_alloc_small (1, uunexpected); \
3047 Field (ret_v, 0) = str_v; \
3053 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3055 CAMLparam2 (ptr_v
, n_v
);
3056 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3059 struct pagedim
*pdim
;
3060 char *s
= String_val (ptr_v
);
3062 ret_v
= Val_int (0);
3063 if (trylock ("ml_getlink")) {
3067 page
= parse_pointer ("ml_getlink", s
);
3068 ensureslinks (page
);
3069 pdim
= &state
.pagedims
[page
->pdimno
];
3070 link
= page
->slinks
[Int_val (n_v
)].link
;
3073 unlock ("ml_getlink");
3078 CAMLprim value
ml_getlinkcount (value ptr_v
)
3082 char *s
= String_val (ptr_v
);
3084 page
= parse_pointer ("ml_getlinkcount", s
);
3085 CAMLreturn (Val_int (page
->slinkcount
));
3088 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3090 CAMLparam2 (ptr_v
, n_v
);
3093 struct slink
*slink
;
3094 char *s
= String_val (ptr_v
);
3096 page
= parse_pointer ("ml_getlinkrect", s
);
3097 ret_v
= caml_alloc_tuple (4);
3098 if (trylock ("ml_getlinkrect")) {
3099 Field (ret_v
, 0) = Val_int (0);
3100 Field (ret_v
, 1) = Val_int (0);
3101 Field (ret_v
, 2) = Val_int (0);
3102 Field (ret_v
, 3) = Val_int (0);
3105 ensureslinks (page
);
3107 slink
= &page
->slinks
[Int_val (n_v
)];
3108 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3109 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3110 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3111 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3112 unlock ("ml_getlinkrect");
3118 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3120 CAMLparam3 (ptr_v
, x_v
, y_v
);
3121 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3124 char *s
= String_val (ptr_v
);
3125 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3126 struct pagedim
*pdim
;
3128 ret_v
= Val_int (0);
3129 if (trylock ("ml_whatsunder")) {
3133 page
= parse_pointer ("ml_whatsunder", s
);
3134 pdim
= &state
.pagedims
[page
->pdimno
];
3135 x
+= pdim
->bounds
.x0
;
3136 y
+= pdim
->bounds
.y0
;
3137 link
= getlink (page
, x
, y
);
3143 fz_page_block
*pageb
;
3144 fz_text_block
*block
;
3147 for (pageb
= page
->text
->blocks
;
3148 pageb
< page
->text
->blocks
+ page
->text
->len
;
3151 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3152 block
= pageb
->u
.text
;
3155 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3158 for (line
= block
->lines
;
3159 line
< block
->lines
+ block
->len
;
3164 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3167 for (span
= line
->first_span
; span
; span
= span
->next
) {
3171 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3174 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3176 fz_text_char_bbox (&bbox
, span
, charnum
);
3179 if (x
>= b
->x0
&& x
<= b
->x1
3180 && y
>= b
->y0
&& y
<= b
->y1
) {
3181 fz_text_style
*style
= span
->text
->style
;
3183 style
->font
&& style
->font
->name
3185 : "Span has no font name"
3187 FT_FaceRec
*face
= style
->font
->ft_face
;
3188 if (face
&& face
->family_name
) {
3190 char *n1
= face
->family_name
;
3191 size_t l1
= strlen (n1
);
3192 size_t l2
= strlen (n2
);
3194 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3195 s
= malloc (l1
+ l2
+ 2);
3199 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3200 str_v
= caml_copy_string (s
);
3206 str_v
= caml_copy_string (n2
);
3208 ret_v
= caml_alloc_small (1, utext
);
3209 Field (ret_v
, 0) = str_v
;
3218 unlock ("ml_whatsunder");
3224 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3226 static int uninteresting (int c
)
3228 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3232 CAMLprim value
ml_clearmark (value ptr_v
)
3235 char *s
= String_val (ptr_v
);
3238 if (trylock ("ml_clearmark")) {
3242 page
= parse_pointer ("ml_clearmark", s
);
3243 page
->fmark
.span
= NULL
;
3244 page
->lmark
.span
= NULL
;
3248 unlock ("ml_clearmark");
3250 CAMLreturn (Val_unit
);
3253 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3255 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3260 fz_page_block
*pageb
;
3261 fz_text_block
*block
;
3262 struct pagedim
*pdim
;
3263 int mark
= Int_val (mark_v
);
3264 char *s
= String_val (ptr_v
);
3265 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3267 ret_v
= Val_bool (0);
3268 if (trylock ("ml_markunder")) {
3272 page
= parse_pointer ("ml_markunder", s
);
3273 pdim
= &state
.pagedims
[page
->pdimno
];
3277 if (mark
== mark_page
) {
3279 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3281 for (i
= 0; i
< page
->text
->len
; ++i
) {
3282 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3283 pb1
= &page
->text
->blocks
[i
];
3287 if (!pb1
) goto unlock
;
3289 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3290 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3291 pb2
= &page
->text
->blocks
[i
];
3295 if (!pb2
) goto unlock
;
3297 block
= pb1
->u
.text
;
3300 page
->fmark
.span
= block
->lines
->first_span
;
3302 block
= pb2
->u
.text
;
3303 line
= &block
->lines
[block
->len
- 1];
3304 page
->lmark
.i
= line
->last_span
->len
- 1;
3305 page
->lmark
.span
= line
->last_span
;
3306 ret_v
= Val_bool (1);
3310 x
+= pdim
->bounds
.x0
;
3311 y
+= pdim
->bounds
.y0
;
3313 for (pageb
= page
->text
->blocks
;
3314 pageb
< page
->text
->blocks
+ page
->text
->len
;
3316 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3317 block
= pageb
->u
.text
;
3320 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3323 if (mark
== mark_block
) {
3325 page
->fmark
.span
= block
->lines
->first_span
;
3327 line
= &block
->lines
[block
->len
- 1];
3328 page
->lmark
.i
= line
->last_span
->len
- 1;
3329 page
->lmark
.span
= line
->last_span
;
3330 ret_v
= Val_bool (1);
3334 for (line
= block
->lines
;
3335 line
< block
->lines
+ block
->len
;
3340 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3343 if (mark
== mark_line
) {
3345 page
->fmark
.span
= line
->first_span
;
3347 page
->lmark
.i
= line
->last_span
->len
- 1;
3348 page
->lmark
.span
= line
->last_span
;
3349 ret_v
= Val_bool (1);
3353 for (span
= line
->first_span
; span
; span
= span
->next
) {
3357 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3360 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3362 fz_text_char_bbox (&bbox
, span
, charnum
);
3365 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3367 int charnum2
, charnum3
= -1, charnum4
= -1;
3369 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3371 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3372 if (uninteresting (span
->text
[charnum2
].c
)) {
3373 charnum3
= charnum2
+ 1;
3377 if (charnum3
== -1) charnum3
= 0;
3379 for (charnum2
= charnum
+ 1;
3380 charnum2
< span
->len
;
3382 if (uninteresting (span
->text
[charnum2
].c
)) break;
3383 charnum4
= charnum2
;
3385 if (charnum4
== -1) goto unlock
;
3387 page
->fmark
.i
= charnum3
;
3388 page
->fmark
.span
= span
;
3390 page
->lmark
.i
= charnum4
;
3391 page
->lmark
.span
= span
;
3392 ret_v
= Val_bool (1);
3400 if (!Bool_val (ret_v
)) {
3401 page
->fmark
.span
= NULL
;
3402 page
->lmark
.span
= NULL
;
3406 unlock ("ml_markunder");
3412 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3414 CAMLparam3 (ptr_v
, x_v
, y_v
);
3415 CAMLlocal2 (ret_v
, res_v
);
3418 fz_page_block
*pageb
;
3419 struct pagedim
*pdim
;
3420 char *s
= String_val (ptr_v
);
3421 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3423 ret_v
= Val_int (0);
3424 if (trylock ("ml_rectofblock")) {
3428 page
= parse_pointer ("ml_rectofblock", s
);
3429 pdim
= &state
.pagedims
[page
->pdimno
];
3430 x
+= pdim
->bounds
.x0
;
3431 y
+= pdim
->bounds
.y0
;
3435 for (pageb
= page
->text
->blocks
;
3436 pageb
< page
->text
->blocks
+ page
->text
->len
;
3438 switch (pageb
->type
) {
3439 case FZ_PAGE_BLOCK_TEXT
:
3440 b
= &pageb
->u
.text
->bbox
;
3443 case FZ_PAGE_BLOCK_IMAGE
:
3444 b
= &pageb
->u
.image
->bbox
;
3451 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3456 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3457 ret_v
= caml_alloc_small (1, 1);
3458 Store_double_field (res_v
, 0, b
->x0
);
3459 Store_double_field (res_v
, 1, b
->x1
);
3460 Store_double_field (res_v
, 2, b
->y0
);
3461 Store_double_field (res_v
, 3, b
->y1
);
3462 Field (ret_v
, 0) = res_v
;
3464 unlock ("ml_rectofblock");
3470 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3472 CAMLparam2 (ptr_v
, rect_v
);
3475 struct pagedim
*pdim
;
3476 char *s
= String_val (ptr_v
);
3477 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3479 fz_page_block
*pageb
;
3480 fz_text_block
*block
;
3481 fz_text_span
*span
, *fspan
, *lspan
;
3483 if (trylock ("ml_seltext")) {
3487 page
= parse_pointer ("ml_seltext", s
);
3490 pdim
= &state
.pagedims
[page
->pdimno
];
3491 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3492 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3493 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3494 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3505 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3506 glColor3ub (128, 128, 128);
3507 recti (x0
, y0
, x1
, y1
);
3508 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3512 fspan
= page
->fmark
.span
;
3515 lspan
= page
->lmark
.span
;
3517 for (pageb
= page
->text
->blocks
;
3518 pageb
< page
->text
->blocks
+ page
->text
->len
;
3520 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3521 block
= pageb
->u
.text
;
3522 for (line
= block
->lines
;
3523 line
< block
->lines
+ block
->len
;
3527 for (span
= line
->first_span
; span
; span
= span
->next
) {
3528 for (i
= 0; i
< span
->len
; ++i
) {
3531 fz_text_char_bbox (&b
, span
, i
);
3533 if (x0
>= b
.x0
&& x0
<= b
.x1
3534 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3539 if (x1
>= b
.x0
&& x1
<= b
.x1
3540 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3545 if (0 && selected
) {
3546 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3547 glColor3ub (128, 128, 128);
3548 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3549 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3555 if (x1
< x0
&& fspan
== lspan
) {
3567 page
->fmark
.span
= fspan
;
3570 page
->lmark
.span
= lspan
;
3572 unlock ("ml_seltext");
3575 CAMLreturn (Val_unit
);
3578 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3583 for (i
= a
; i
<= b
; ++i
) {
3584 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3585 ret
= fwrite (buf
, len
, 1, f
);
3588 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3589 len
, ret
, strerror (errno
));
3597 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3599 caml_failwith ("ml_popen not implemented under Cygwin");
3602 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3604 CAMLparam2 (command_v
, fds_v
);
3605 CAMLlocal2 (l_v
, tup_v
);
3608 value earg_v
= Nothing
;
3609 posix_spawnattr_t attr
;
3610 posix_spawn_file_actions_t fa
;
3611 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3613 argv
[2] = String_val (command_v
);
3615 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3616 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3619 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3620 msg
= "posix_spawnattr_init";
3624 #ifdef POSIX_SPAWN_USEVFORK
3625 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3626 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3631 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3634 tup_v
= Field (l_v
, 0);
3635 fd1
= Int_val (Field (tup_v
, 0));
3636 fd2
= Int_val (Field (tup_v
, 1));
3638 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3639 msg
= "posix_spawn_file_actions_addclose";
3645 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3646 msg
= "posix_spawn_file_actions_adddup2";
3653 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3654 msg
= "posix_spawn";
3659 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3660 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3664 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3665 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3670 unix_error (ret
, msg
, earg_v
);
3672 CAMLreturn (Val_unit
);
3676 CAMLprim value
ml_hassel (value ptr_v
)
3681 char *s
= String_val (ptr_v
);
3683 ret_v
= Val_bool (0);
3684 if (trylock ("ml_hassel")) {
3688 page
= parse_pointer ("ml_hassel", s
);
3689 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3690 unlock ("ml_hassel");
3695 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3697 CAMLparam2 (fd_v
, ptr_v
);
3702 fz_page_block
*pageb
;
3703 fz_text_block
*block
;
3704 int fd
= Int_val (fd_v
);
3705 char *s
= String_val (ptr_v
);
3707 if (trylock ("ml_copysel")) {
3711 page
= parse_pointer ("ml_copysel", s
);
3713 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3714 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3718 f
= fdopen (fd
, "w");
3720 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3721 fd
, strerror (errno
));
3725 for (pageb
= page
->text
->blocks
;
3726 pageb
< page
->text
->blocks
+ page
->text
->len
;
3728 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3729 block
= pageb
->u
.text
;
3730 for (line
= block
->lines
;
3731 line
< block
->lines
+ block
->len
;
3735 for (span
= line
->first_span
; span
; span
= span
->next
) {
3738 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3739 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3740 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3743 if (pipespan (f
, span
, a
, b
)) {
3746 if (span
== page
->lmark
.span
) {
3749 if (span
== line
->last_span
) {
3750 if (putc ('\n', f
) == EOF
) {
3752 "failed break line on sel pipe: %s\n",
3763 int ret
= fclose (f
);
3766 if (errno
!= ECHILD
) {
3767 fprintf (stderr
, "failed to close sel pipe: %s\n",
3773 unlock ("ml_copysel");
3778 fprintf (stderr
, "failed to close sel pipe: %s\n",
3782 CAMLreturn (Val_unit
);
3785 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3787 CAMLparam1 (pagedimno_v
);
3789 int pagedimno
= Int_val (pagedimno_v
);
3792 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3793 if (trylock ("ml_getpdimrect")) {
3794 box
= fz_empty_rect
;
3797 box
= state
.pagedims
[pagedimno
].mediabox
;
3798 unlock ("ml_getpdimrect");
3801 Store_double_field (ret_v
, 0, box
.x0
);
3802 Store_double_field (ret_v
, 1, box
.x1
);
3803 Store_double_field (ret_v
, 2, box
.y0
);
3804 Store_double_field (ret_v
, 3, box
.y1
);
3809 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3810 value dw_v
, value cols_v
)
3812 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3818 double winw
= Int_val (winw_v
);
3819 double winh
= Int_val (winh_v
);
3820 double dw
= Int_val (dw_v
);
3821 double cols
= Int_val (cols_v
);
3822 double pw
= 1.0, ph
= 1.0;
3824 if (trylock ("ml_zoom_for_height")) {
3828 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3829 double w
= p
->pagebox
.x1
/ cols
;
3830 double h
= p
->pagebox
.y1
;
3834 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3836 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3839 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3840 unlock ("ml_zoom_for_height");
3842 ret_v
= caml_copy_double (zoom
);
3846 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3848 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3850 int pt
= Int_val(pt_v
);
3851 int x
= Int_val (x_v
);
3852 int y
= Int_val (y_v
);
3855 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3856 ret_v
= caml_copy_double (w
);
3860 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3862 CAMLparam2 (pt_v
, string_v
);
3864 int pt
= Int_val (pt_v
);
3867 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3868 ret_v
= caml_copy_double (w
);
3872 CAMLprim value
ml_getpagebox (value opaque_v
)
3874 CAMLparam1 (opaque_v
);
3880 char *s
= String_val (opaque_v
);
3881 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3883 ret_v
= caml_alloc_tuple (4);
3884 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3885 dev
->hints
|= FZ_IGNORE_SHADE
;
3887 switch (page
->type
) {
3889 ctm
= pagectm (page
);
3890 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3894 ctm
= pagectm (page
);
3895 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3899 rect
= fz_infinite_rect
;
3903 fz_free_device (dev
);
3904 fz_round_rect (&bbox
, &rect
);
3905 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3906 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3907 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3908 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3913 CAMLprim value
ml_setaalevel (value level_v
)
3915 CAMLparam1 (level_v
);
3917 state
.aalevel
= Int_val (level_v
);
3918 CAMLreturn (Val_unit
);
3921 #pragma GCC diagnostic push
3922 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3923 #include <X11/Xlib.h>
3924 #pragma GCC diagnostic pop
3932 XVisualInfo
*visual
;
3935 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3937 CAMLparam3 (display_v
, wid_v
, screen_v
);
3938 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3940 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3942 caml_failwith ("XOpenDisplay");
3945 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3947 XCloseDisplay (glx
.dpy
);
3948 caml_failwith ("glXChooseVisual");
3951 glx
.wid
= Int_val (wid_v
);
3952 CAMLreturn (Val_int (glx
.visual
->visualid
));
3955 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3957 CAMLparam1 (unit_v
);
3959 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3961 caml_failwith ("glXCreateContext");
3967 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3968 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3970 caml_failwith ("glXMakeCurrent");
3972 CAMLreturn (Val_unit
);
3975 CAMLprim value
ml_swapb (value unit_v
)
3977 CAMLparam1 (unit_v
);
3978 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3979 CAMLreturn (Val_unit
);
3982 #include "keysym2ucs.c"
3984 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3986 CAMLparam1 (keysym_v
);
3988 KeySym keysym
= Int_val (keysym_v
);
3993 rune
= keysym2ucs (keysym
);
3994 len
= fz_runetochar (buf
, rune
);
3996 str_v
= caml_copy_string (buf
);
4000 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4002 CAMLprim value
ml_platform (value unit_v
)
4004 CAMLparam1 (unit_v
);
4005 CAMLlocal2 (tup_v
, arr_v
);
4006 int platid
= piunknown
;
4009 #if defined __linux__
4011 #elif defined __CYGWIN__
4013 #elif defined __DragonFly__ || defined __FreeBSD__
4014 || defined __OpenBSD__
|| defined __NetBSD__
4016 #elif defined __sun__
4018 #elif defined __APPLE__
4021 if (uname (&buf
)) err (1, "uname");
4023 tup_v
= caml_alloc_tuple (2);
4025 char const *sar
[] = {
4032 arr_v
= caml_copy_string_array (sar
);
4034 Field (tup_v
, 0) = Val_int (platid
);
4035 Field (tup_v
, 1) = arr_v
;
4039 CAMLprim value
ml_cloexec (value fd_v
)
4042 int fd
= Int_val (fd_v
);
4044 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4045 uerror ("fcntl", Nothing
);
4047 CAMLreturn (Val_unit
);
4050 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4052 CAMLparam2 (w_v
, h_v
);
4055 int w
= Int_val (w_v
);
4056 int h
= Int_val (h_v
);
4057 int cs
= Int_val (cs_v
);
4059 if (state
.pbo_usable
) {
4060 pbo
= calloc (sizeof (*pbo
), 1);
4062 err (1, "calloc pbo");
4074 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4077 state
.glGenBuffersARB (1, &pbo
->id
);
4078 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4079 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4080 NULL
, GL_STREAM_DRAW
);
4081 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4083 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4085 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4086 state
.glDeleteBuffersARB (1, &pbo
->id
);
4088 ret_v
= caml_copy_string ("0");
4094 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4096 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4100 err (1, "malloc %d bytes failed", res
+1);
4102 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4104 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4106 ret_v
= caml_copy_string (s
);
4111 ret_v
= caml_copy_string ("0");
4116 CAMLprim value
ml_freepbo (value s_v
)
4119 char *s
= String_val (s_v
);
4120 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4123 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4125 tile
->pbo
->ptr
= NULL
;
4126 tile
->pbo
->size
= -1;
4128 CAMLreturn (Val_unit
);
4131 CAMLprim value
ml_unmappbo (value s_v
)
4134 char *s
= String_val (s_v
);
4135 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4138 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4139 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4140 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4142 tile
->pbo
->ptr
= NULL
;
4143 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4145 CAMLreturn (Val_unit
);
4148 static void setuppbo (void)
4150 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4151 state
.pbo_usable
= GGPA (glBindBufferARB
)
4152 && GGPA (glUnmapBufferARB
)
4153 && GGPA (glMapBufferARB
)
4154 && GGPA (glBufferDataARB
)
4155 && GGPA (glGenBuffersARB
)
4156 && GGPA (glDeleteBuffersARB
);
4160 CAMLprim value
ml_pbo_usable (value unit_v
)
4162 CAMLparam1 (unit_v
);
4163 CAMLreturn (Val_bool (state
.pbo_usable
));
4166 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4168 CAMLparam3 (ptr_v
, x_v
, y_v
);
4169 CAMLlocal2 (ret_v
, tup_v
);
4171 char *s
= String_val (ptr_v
);
4172 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4173 struct pagedim
*pdim
;
4177 page
= parse_pointer ("ml_unproject", s
);
4178 pdim
= &state
.pagedims
[page
->pdimno
];
4180 ret_v
= Val_int (0);
4181 if (trylock ("ml_unproject")) {
4185 switch (page
->type
) {
4187 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4193 p
.x
= x
+ pdim
->bounds
.x0
;
4194 p
.y
= y
+ pdim
->bounds
.y0
;
4196 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4197 fz_invert_matrix (&ctm
, &ctm
);
4198 fz_transform_point (&p
, &ctm
);
4200 tup_v
= caml_alloc_tuple (2);
4201 ret_v
= caml_alloc_small (1, 1);
4202 Field (tup_v
, 0) = Val_int (p
.x
);
4203 Field (tup_v
, 1) = Val_int (p
.y
);
4204 Field (ret_v
, 0) = tup_v
;
4206 unlock ("ml_unproject");
4211 static void makestippletex (void)
4213 const char pixels
[] = "\xff\xff\0\0";
4214 glGenTextures (1, &state
.stid
);
4215 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4216 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4217 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4230 CAMLprim value
ml_fz_version (value unit_v
)
4232 return caml_copy_string (FZ_VERSION
);
4235 #ifdef USE_FONTCONFIG
4241 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4245 int needs_exact_metrics
)
4252 FcPattern
*pat
, *pat1
;
4254 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4255 name
, bold
, italic
, needs_exact_metrics
);
4258 fc
.config
= FcInitLoadConfigAndFonts ();
4260 lprintf ("FcInitLoadConfigAndFonts failed\n");
4264 if (!fc
.config
) return NULL
;
4266 size
= strlen (name
);
4267 if (bold
) size
+= sizeof (":bold") - 1;
4268 if (italic
) size
+= sizeof (":italic") - 1;
4271 buf
= malloc (size
);
4273 err (1, "malloc %zu failed", size
);
4277 if (bold
&& italic
) {
4278 strcat (buf
, ":bold:italic");
4281 if (bold
) strcat (buf
, ":bold");
4282 if (italic
) strcat (buf
, ":italic");
4284 for (i
= 0; i
< size
; ++i
) {
4285 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4288 lprintf ("fcbuf=%s\n", buf
);
4289 pat
= FcNameParse ((FcChar8
*) buf
);
4291 printd ("emsg FcNameParse failed\n");
4296 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4297 printd ("emsg FcConfigSubstitute failed\n");
4301 FcDefaultSubstitute (pat
);
4303 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4305 printd ("emsg FcFontMatch failed\n");
4306 FcPatternDestroy (pat
);
4311 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4312 printd ("emsg FcPatternGetString failed\n");
4313 FcPatternDestroy (pat
);
4314 FcPatternDestroy (pat1
);
4320 printd ("emsg name=%s path=%s\n", name
, path
);
4322 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4323 FcPatternDestroy (pat
);
4324 FcPatternDestroy (pat1
);
4330 CAMLprim value
ml_init (value csock_v
, value params_v
)
4332 CAMLparam2 (csock_v
, params_v
);
4333 CAMLlocal2 (trim_v
, fuzz_v
);
4340 struct sigaction sa
;
4342 state
.csock
= Int_val (csock_v
);
4343 state
.rotate
= Int_val (Field (params_v
, 0));
4344 state
.fitmodel
= Int_val (Field (params_v
, 1));
4345 trim_v
= Field (params_v
, 2);
4346 texcount
= Int_val (Field (params_v
, 3));
4347 state
.sliceheight
= Int_val (Field (params_v
, 4));
4348 mustoresize
= Int_val (Field (params_v
, 5));
4349 colorspace
= Int_val (Field (params_v
, 6));
4350 fontpath
= String_val (Field (params_v
, 7));
4352 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4353 if (!state
.trimcachepath
) {
4354 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4357 haspboext
= Bool_val (Field (params_v
, 9));
4359 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4361 #ifdef USE_FONTCONFIG
4362 fz_install_load_system_font_funcs (
4363 state
.ctx
, fc_load_system_font_func
, NULL
4367 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4368 fuzz_v
= Field (trim_v
, 1);
4369 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4370 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4371 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4372 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4374 set_tex_params (colorspace
);
4377 #ifndef USE_FONTCONFIG
4378 state
.face
= load_font (fontpath
);
4382 char *buf
= fontpath
;
4383 FcPattern
*pat
, *pat1
;
4386 fc
.config
= FcInitLoadConfigAndFonts ();
4388 errx (1, "FcInitLoadConfigAndFonts failed");
4391 pat
= FcNameParse ((FcChar8
*) buf
);
4393 errx (1, "FcNameParse failed");
4396 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4397 errx (1, "FcConfigSubstitute failed");
4399 FcDefaultSubstitute (pat
);
4401 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4403 errx (1, "FcFontMatch failed");
4406 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4407 errx (1, "FcPatternGetString failed");
4410 state
.face
= load_font ((char *) path
);
4411 FcPatternDestroy (pat
);
4412 FcPatternDestroy (pat1
);
4417 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4419 state
.face
= load_builtin_font (base
, len
);
4421 if (!state
.face
) _exit (1);
4423 realloctexts (texcount
);
4432 sa
.sa_handler
= SIG_IGN
;
4433 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4435 sa
.sa_handler
= SIG_DFL
;
4436 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4438 if (sigemptyset (&sa
.sa_mask
)) {
4439 err (1, "sigemptyset");
4441 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4442 err (1, "sigaction");
4445 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4447 errx (1, "pthread_create: %s", strerror (ret
));
4450 CAMLreturn (Val_unit
);