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 */
29 #include <caml/fail.h>
30 #include <caml/alloc.h>
31 #include <caml/memory.h>
32 #include <caml/unixsupport.h>
34 #include <mupdf/fitz.h>
35 #include <mupdf/cbz.h>
36 #include <mupdf/pdf.h>
37 #include <mupdf/xps.h>
38 #include <mupdf/img.h>
41 #include FT_FREETYPE_H
44 #include <fontconfig/fontconfig.h>
48 #define SUMATRA_LOOKUP
51 extern char **environ
;
54 #define MIN(a,b) ((a) < (b) ? (a) : (b))
55 #define MAX(a,b) ((a) > (b) ? (a) : (b))
58 #define NORETURN_ATTR __attribute__ ((noreturn))
59 #define UNUSED_ATTR __attribute__ ((unused))
60 #if !defined __clang__
61 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
63 #define OPTIMIZE_ATTR(n)
65 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
69 #define OPTIMIZE_ATTR(n)
70 #define GCC_FMT_ATTR(a, b)
75 #define FMT_ptr PRIxPTR
76 #define SCN_ptr SCNxPTR
77 #define FMT_ptr_cast(p) ((uintptr_t) (p))
78 #define SCN_ptr_cast(p) ((uintptr_t *) (p))
80 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
81 err (int exitcode
, const char *fmt
, ...)
88 vfprintf (stderr
, fmt
, ap
);
90 fprintf (stderr
, ": %s\n", strerror (savederrno
));
95 static void NORETURN_ATTR
GCC_FMT_ATTR (2, 3)
96 errx (int exitcode
, const char *fmt
, ...)
101 vfprintf (stderr
, fmt
, ap
);
103 fputc ('\n', stderr
);
108 #ifndef GL_TEXTURE_RECTANGLE_ARB
109 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
113 #define GL_BGRA 0x80E1
116 #ifndef GL_UNSIGNED_INT_8_8_8_8
117 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
120 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
121 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
125 #define lprintf printf
130 #define ARSERT(cond) for (;;) { \
132 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
148 struct slice slices
[1];
159 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
167 enum { DPDF
, DXPS
, DCBZ
, DIMG
};
176 fz_text_sheet
*sheet
;
184 fz_display_list
*dlist
;
186 struct slink
*slinks
;
191 void (*freepage
) (void *);
197 struct pagedim
*pagedims
;
217 fz_colorspace
*colorspace
;
225 enum { FitWidth
, FitProportional
, FitPage
} fitmodel
;
239 void (*closedoc
) (void);
240 void (*freepage
) (void *);
248 void (*glBindBufferARB
) (GLenum
, GLuint
);
249 GLboolean (*glUnmapBufferARB
) (GLenum
);
250 void *(*glMapBufferARB
) (GLenum
, GLenum
);
251 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
252 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
253 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
255 GLfloat texcoords
[8];
256 GLfloat vertices
[16];
258 #ifdef SUMATRA_LOOKUP
273 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
275 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
278 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
280 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
283 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
285 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
286 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
289 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
291 static void lock (const char *cap
)
293 int ret
= pthread_mutex_lock (&mutex
);
295 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
299 static void unlock (const char *cap
)
301 int ret
= pthread_mutex_unlock (&mutex
);
303 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
307 static int trylock (const char *cap
)
309 int ret
= pthread_mutex_trylock (&mutex
);
310 if (ret
&& ret
!= EBUSY
) {
311 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
316 static void *parse_pointer (const char *cap
, const char *s
)
321 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
323 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
328 static double now (void)
332 if (gettimeofday (&tv
, NULL
)) {
333 err (1, "gettimeofday");
335 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
338 static int hasdata (void)
341 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
342 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
346 CAMLprim value
ml_hasdata (value fd_v
)
351 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
352 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
353 CAMLreturn (Val_bool (avail
> 0));
356 static void readdata (void *p
, int size
)
361 n
= read (state
.csock
, p
, size
);
363 if (errno
== EINTR
) goto again
;
364 err (1, "read (req %d, ret %zd)", size
, n
);
367 if (!n
) errx (1, "EOF while reading");
368 errx (1, "read (req %d, ret %zd)", size
, n
);
372 static void writedata (char *p
, int size
)
376 p
[0] = (size
>> 24) & 0xff;
377 p
[1] = (size
>> 16) & 0xff;
378 p
[2] = (size
>> 8) & 0xff;
379 p
[3] = (size
>> 0) & 0xff;
381 n
= write (state
.csock
, p
, size
+ 4);
383 if (!n
) errx (1, "EOF while writing data");
384 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
388 static int readlen (void)
393 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
396 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
404 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
407 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
411 if (len
< size
- 4) {
412 writedata (buf
, len
);
418 err (1, "vsnprintf for `%s' failed", fmt
);
420 buf
= realloc (buf
, size
);
425 static void closepdf (void)
427 #ifdef SUMATRA_LOOKUP
428 if (state
.pdflut
.objs
) {
431 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
432 pdf_drop_obj (state
.pdflut
.objs
[i
]);
434 free (state
.pdflut
.objs
);
435 state
.pdflut
.objs
= NULL
;
440 pdf_close_document (state
.u
.pdf
);
445 static void closexps (void)
448 xps_close_document (state
.u
.xps
);
453 static void closecbz (void)
456 cbz_close_document (state
.u
.cbz
);
461 static void closeimg (void)
464 image_close_document (state
.u
.img
);
469 static void freepdfpage (void *ptr
)
471 pdf_free_page (state
.u
.pdf
, ptr
);
474 static void freeemptyxxxpage (void *ptr
)
479 static void freexpspage (void *ptr
)
481 xps_free_page (state
.u
.xps
, ptr
);
484 static void freecbzpage (void *ptr
)
486 cbz_free_page (state
.u
.cbz
, ptr
);
489 static void freeimgpage (void *ptr
)
491 image_free_page (state
.u
.img
, ptr
);
494 static void openxref (char *filename
, char *password
)
498 for (i
= 0; i
< state
.texcount
; ++i
) {
499 state
.texowners
[i
].w
= -1;
500 state
.texowners
[i
].slice
= NULL
;
503 if (state
.closedoc
) state
.closedoc ();
505 len
= strlen (filename
);
523 for (i
= 0; i
< sizeof (tbl
) / sizeof (*tbl
); ++i
) {
524 int len2
= strlen (tbl
[i
].ext
);
526 if (len2
< len
&& !strcasecmp (tbl
[i
].ext
, filename
+ len
- len2
)) {
527 state
.type
= tbl
[i
].type
;
533 if (state
.pagedims
) {
534 free (state
.pagedims
);
535 state
.pagedims
= NULL
;
537 state
.pagedimcount
= 0;
539 fz_set_aa_level (state
.ctx
, state
.aalevel
);
540 switch (state
.type
) {
542 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
543 if (pdf_needs_password (state
.u
.pdf
)) {
544 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
546 errx (1, "invalid password");
549 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
550 state
.closedoc
= closepdf
;
551 state
.freepage
= freepdfpage
;
555 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
556 state
.pagecount
= xps_count_pages (state
.u
.xps
);
557 state
.closedoc
= closexps
;
558 state
.freepage
= freexpspage
;
562 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
563 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
564 state
.closedoc
= closecbz
;
565 state
.freepage
= freecbzpage
;
569 state
.u
.img
= image_open_document (state
.ctx
, filename
);
571 state
.closedoc
= closeimg
;
572 state
.freepage
= freeimgpage
;
577 static void pdfinfo (void)
579 if (state
.type
== DPDF
) {
582 printd ("info PDF version\t%d.%d",
583 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
585 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
589 char *items
[] = { "Title", "Author", "Creator",
590 "Producer", "CreationDate" };
592 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
593 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
594 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
597 printd ("title %s", s
);
599 printd ("info %s\t%s", items
[i
], s
);
601 fz_free (state
.ctx
, s
);
608 static void unlinktile (struct tile
*tile
)
612 for (i
= 0; i
< tile
->slicecount
; ++i
) {
613 struct slice
*s
= &tile
->slices
[i
];
615 if (s
->texindex
!= -1) {
616 if (state
.texowners
[s
->texindex
].slice
== s
) {
617 state
.texowners
[s
->texindex
].slice
= NULL
;
623 static void freepage (struct page
*page
)
626 fz_free_text_page (state
.ctx
, page
->text
);
629 fz_free_text_sheet (state
.ctx
, page
->sheet
);
634 fz_drop_display_list (state
.ctx
, page
->dlist
);
635 page
->freepage (page
->u
.ptr
);
639 static void freetile (struct tile
*tile
)
644 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
647 fz_drop_pixmap (state
.ctx
, state
.pig
);
649 state
.pig
= tile
->pixmap
;
654 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
663 static int cacheline32bytes
;
665 static void __attribute__ ((constructor
)) clcheck (void)
667 char **envp
= environ
;
672 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
674 cacheline32bytes
= auxv
[1] == 32;
680 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
682 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
683 if (cacheline32bytes
&& size
> 32) {
684 intptr_t a1
, a2
, diff
;
686 vector
unsigned char v
= vec_splat_u8 (-1);
687 vector
unsigned char *p
;
689 a1
= a2
= (intptr_t) pixmap
->samples
;
690 a2
= (a1
+ 31) & ~31;
695 while (a1
!= a2
) *(char *) a1
++ = 0xff;
696 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
697 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
699 vec_st (v
, i
+ 16, p
);
701 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
703 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
706 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
709 static void trimctm (pdf_page
*page
, int pindex
)
712 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
714 if (!pdim
->tctmready
) {
715 if (state
.trimmargins
) {
717 fz_matrix rm
, sm
, tm
, im
, ctm1
;
719 fz_rotate (&rm
, -pdim
->rotate
);
720 fz_scale (&sm
, 1, -1);
721 fz_concat (&ctm
, &rm
, &sm
);
722 realbox
= pdim
->mediabox
;
723 fz_transform_rect (&realbox
, &ctm
);
724 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
725 fz_concat (&ctm1
, &ctm
, &tm
);
726 fz_invert_matrix (&im
, &page
->ctm
);
727 fz_concat (&ctm
, &im
, &ctm1
);
737 static fz_matrix
pagectm (struct page
*page
)
741 if (page
->type
== DPDF
) {
742 trimctm (page
->u
.pdfpage
, page
->pdimno
);
744 &state
.pagedims
[page
->pdimno
].tctm
,
745 &state
.pagedims
[page
->pdimno
].ctm
);
748 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
750 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
751 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
756 static void *loadpage (int pageno
, int pindex
)
761 page
= calloc (sizeof (struct page
), 1);
763 err (1, "calloc page %d", pageno
);
766 page
->dlist
= fz_new_display_list (state
.ctx
);
767 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
769 switch (state
.type
) {
771 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
772 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
774 page
->freepage
= freepdfpage
;
778 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
779 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
781 page
->freepage
= freexpspage
;
785 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
786 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
788 page
->freepage
= freecbzpage
;
792 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
793 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
795 page
->freepage
= freeimgpage
;
799 fz_catch (state
.ctx
) {
801 page
->freepage
= freeemptyxxxpage
;
803 fz_free_device (dev
);
805 page
->pdimno
= pindex
;
806 page
->pageno
= pageno
;
807 page
->sgen
= state
.gen
;
808 page
->tgen
= state
.gen
;
809 page
->type
= state
.type
;
814 static struct tile
*alloctile (int h
)
821 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
822 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
823 tile
= calloc (tilesize
, 1);
825 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
827 for (i
= 0; i
< slicecount
; ++i
) {
828 int sh
= MIN (h
, state
.sliceheight
);
829 tile
->slices
[i
].h
= sh
;
830 tile
->slices
[i
].texindex
= -1;
833 tile
->slicecount
= slicecount
;
834 tile
->sliceheight
= state
.sliceheight
;
844 static void obs_fill_image (fz_device
*dev
, fz_image
*image
,
845 const fz_matrix
*ctm
, float alpha
)
847 struct obs
*obs
= dev
->user
;
849 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
851 fz_rect rect
= fz_unit_rect
;
853 fz_transform_rect (&rect
, ctm
);
854 fz_round_rect (&b
, &rect
);
855 fz_intersect_irect (&b
, &obs
->b
);
856 obs
->cured
= b
.x0
== obs
->b
.x0
859 && b
.y1
== obs
->b
.y1
;
863 static int obscured (struct page
*page
, fz_irect bbox
)
870 memset (&dev
, 0, sizeof (dev
));
871 memset (&obs
, 0, sizeof (obs
));
876 dev
.fill_image
= obs_fill_image
;
878 fz_rect_from_irect (&rect
, &bbox
);
879 ctm
= pagectm (page
);
880 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
883 #define OBSCURED obscured
885 #define OBSCURED(a, b) 0
888 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
896 struct pagedim
*pdim
;
898 tile
= alloctile (h
);
899 pdim
= &state
.pagedims
[page
->pdimno
];
904 bbox
.x1
= bbox
.x0
+ w
;
905 bbox
.y1
= bbox
.y0
+ h
;
908 if (state
.pig
->w
== w
910 && state
.pig
->colorspace
== state
.colorspace
) {
911 tile
->pixmap
= state
.pig
;
912 tile
->pixmap
->x
= bbox
.x0
;
913 tile
->pixmap
->y
= bbox
.y0
;
916 fz_drop_pixmap (state
.ctx
, state
.pig
);
923 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
929 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
935 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
936 clearpixmap (tile
->pixmap
);
938 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
939 ctm
= pagectm (page
);
940 fz_rect_from_irect (&rect
, &bbox
);
941 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
942 fz_free_device (dev
);
947 #ifdef SUMATRA_LOOKUP
948 /* Following two functions are taken (almost) verbatim from SumatraPDF
952 pdf_load_page_objs_rec(pdf_document
*doc
, pdf_obj
*node
, int *page_no
, pdf_obj
**page_objs
)
954 fz_context
*ctx
= doc
->ctx
;
956 if (pdf_mark_obj(node
))
957 fz_throw(ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
960 pdf_obj
*kids
= pdf_dict_gets(node
, "Kids");
961 int len
= pdf_array_len(kids
), i
;
962 for (i
= 0; i
< len
; i
++) {
963 pdf_obj
*kid
= pdf_array_get(kids
, i
);
964 char *type
= pdf_to_name(pdf_dict_gets(kid
, "Type"));
965 if (!strcmp(type
, "Page")) {
966 if (*page_no
> pdf_count_pages(doc
))
967 fz_throw(ctx
, FZ_ERROR_GENERIC
, "found more /Page objects than anticipated");
968 page_objs
[*page_no
- 1] = pdf_keep_obj(kid
);
971 else if (!strcmp(type
, "Pages")) {
972 int count
= pdf_to_int(pdf_dict_gets(kid
, "Count"));
974 pdf_load_page_objs_rec(doc
, kid
, page_no
, page_objs
);
977 fz_warn(ctx
, "non-page object in page tree (%s)", type
);
982 pdf_unmark_obj(node
);
990 pdf_load_page_objs(pdf_document
*doc
, pdf_obj
**page_objs
)
992 pdf_obj
*pages
= pdf_dict_getp(pdf_trailer(doc
), "Root/Pages");
994 pdf_load_page_objs_rec(doc
, pages
, &page_no
, page_objs
);
998 static void initpdims (void)
1002 fz_rect rootmediabox
;
1003 int pageno
, trim
, show
;
1004 int trimw
= 0, cxcount
;
1008 if (state
.trimmargins
&& state
.trimcachepath
) {
1009 trimf
= fopen (state
.trimcachepath
, "rb");
1011 trimf
= fopen (state
.trimcachepath
, "wb");
1016 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1017 cxcount
= state
.pagecount
;
1019 cxcount
= MIN (state
.pagecount
, 1);
1021 if (state
.type
== DPDF
) {
1023 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1024 "Root/Pages/MediaBox");
1025 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1028 #ifdef SUMATRA_LOOKUP
1029 if (state
.type
== DPDF
1030 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1031 state
.pdflut
.objs
= malloc (sizeof (*state
.pdflut
.objs
) * cxcount
);
1032 if (!state
.pdflut
.objs
) {
1033 err (1, "malloc pageobjs %zu %d %zu failed",
1034 sizeof (*state
.pdflut
.objs
), cxcount
,
1035 sizeof (*state
.pdflut
.objs
) * cxcount
);
1037 state
.pdflut
.count
= cxcount
;
1038 pdf_load_page_objs (state
.u
.pdf
, state
.pdflut
.objs
);
1039 state
.pdflut
.pdf
= state
.u
.pdf
;
1043 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1048 switch (state
.type
) {
1050 pdf_document
*pdf
= state
.u
.pdf
;
1051 pdf_obj
*pageref
, *pageobj
;
1053 #ifdef SUMATRA_LOOKUP
1054 pageref
= state
.pdflut
.objs
[pageno
];
1056 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1058 pageobj
= pdf_resolve_indirect (pageref
);
1060 if (state
.trimmargins
) {
1064 fz_try (state
.ctx
) {
1065 page
= pdf_load_page (pdf
, pageno
);
1066 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1067 trim
= state
.trimanew
|| !obj
;
1073 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1074 dev
->hints
|= FZ_IGNORE_SHADE
;
1075 fz_invert_matrix (&ctm
, &page
->ctm
);
1076 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1077 fz_free_device (dev
);
1079 rect
.x0
+= state
.trimfuzz
.x0
;
1080 rect
.x1
+= state
.trimfuzz
.x1
;
1081 rect
.y0
+= state
.trimfuzz
.y0
;
1082 rect
.y1
+= state
.trimfuzz
.y1
;
1083 fz_transform_rect (&rect
, &ctm
);
1084 fz_intersect_rect (&rect
, &page
->mediabox
);
1086 if (fz_is_empty_rect (&rect
)) {
1087 mediabox
= page
->mediabox
;
1093 obj
= pdf_new_array (pdf
, 4);
1094 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1095 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1096 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1097 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1098 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1101 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1102 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1103 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1104 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1107 rotate
= page
->rotate
;
1108 pdf_free_page (pdf
, page
);
1110 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1112 printd ("progress %f Trimming %d",
1113 (double) (pageno
+ 1) / state
.pagecount
,
1117 fz_catch (state
.ctx
) {
1118 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1125 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1127 if (fz_is_empty_rect (&mediabox
)) {
1135 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1137 if (!fz_is_empty_rect (&cropbox
)) {
1142 fz_intersect_rect (&mediabox
, &cropbox
);
1147 if (fz_is_empty_rect (&rootmediabox
)) {
1149 "cannot find page size for page %d\n",
1153 mediabox
= rootmediabox
;
1157 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1166 fz_try (state
.ctx
) {
1167 page
= xps_load_page (state
.u
.xps
, pageno
);
1168 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1170 if (state
.trimmargins
) {
1174 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1175 dev
->hints
|= FZ_IGNORE_SHADE
;
1176 xps_run_page (state
.u
.xps
, page
, dev
,
1177 &fz_identity
, NULL
);
1178 fz_free_device (dev
);
1180 rect
.x0
+= state
.trimfuzz
.x0
;
1181 rect
.x1
+= state
.trimfuzz
.x1
;
1182 rect
.y0
+= state
.trimfuzz
.y0
;
1183 rect
.y1
+= state
.trimfuzz
.y1
;
1184 fz_intersect_rect (&rect
, &mediabox
);
1186 if (!fz_is_empty_rect (&rect
)) {
1190 xps_free_page (state
.u
.xps
, page
);
1192 printd ("progress %f loading %d",
1193 (double) (pageno
+ 1) / state
.pagecount
,
1197 fz_catch (state
.ctx
) {
1209 if (state
.trimmargins
&& trimw
) {
1212 fz_try (state
.ctx
) {
1213 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1214 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1215 cbz_free_page (state
.u
.cbz
, page
);
1216 printd ("progress %f Trimming %d",
1217 (double) (pageno
+ 1) / state
.pagecount
,
1220 fz_catch (state
.ctx
) {
1221 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1224 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1226 err (1, "fwrite trim mediabox");
1232 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1234 err (1, "fread trim mediabox");
1240 fz_try (state
.ctx
) {
1241 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1242 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1243 cbz_free_page (state
.u
.cbz
, page
);
1245 fz_catch (state
.ctx
) {
1246 fprintf (stderr
, "failed to load cbz page\n");
1264 fz_try (state
.ctx
) {
1265 page
= image_load_page (state
.u
.img
, pageno
);
1266 image_bound_page (state
.u
.img
, page
, &mediabox
);
1267 image_free_page (state
.u
.img
, page
);
1269 fz_catch (state
.ctx
) {
1270 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1276 ARSERT (0 && state
.type
);
1279 if (state
.pagedimcount
== 0
1280 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1281 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1284 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1285 state
.pagedims
= realloc (state
.pagedims
, size
);
1286 if (!state
.pagedims
) {
1287 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1288 size
, state
.pagedimcount
+ 1);
1291 p
= &state
.pagedims
[state
.pagedimcount
++];
1293 p
->mediabox
= mediabox
;
1298 if (state
.trimmargins
) {
1299 printd ("progress 1 Trimmed %d pages in %f seconds",
1300 state
.pagecount
, end
- start
);
1303 printd ("vmsg Processed %d pages in %f seconds",
1304 state
.pagecount
, end
- start
);
1308 if (fclose (trimf
)) {
1314 static void layout (void)
1319 struct pagedim
*p
= p
;
1320 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1322 if (state
.pagedimcount
== 0) return;
1324 switch (state
.fitmodel
) {
1325 case FitProportional
:
1326 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1329 p
= &state
.pagedims
[pindex
];
1330 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1332 fz_transform_rect (&box
, &rm
);
1334 x0
= MIN (box
.x0
, box
.x1
);
1335 x1
= MAX (box
.x0
, box
.x1
);
1338 maxw
= MAX (w
, maxw
);
1339 zoom
= state
.w
/ maxw
;
1351 ARSERT (0 && state
.fitmodel
);
1354 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1358 p
= &state
.pagedims
[pindex
];
1359 fz_rotate (&ctm
, state
.rotate
);
1360 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1362 fz_transform_rect (&box
, &rm
);
1363 w
= box
.x1
- box
.x0
;
1364 switch (state
.fitmodel
) {
1365 case FitProportional
:
1366 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1372 h
= box
.y1
- box
.y0
;
1374 zoom
= MIN (zw
, zh
);
1375 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1384 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1385 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1387 fz_rotate (&rm
, p
->rotate
);
1388 p
->pagebox
= p
->mediabox
;
1389 fz_transform_rect (&p
->pagebox
, &rm
);
1390 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1391 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1395 fz_transform_rect (&rect
, &ctm
);
1396 fz_round_rect (&p
->bounds
, &rect
);
1399 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1400 fz_scale (&sm
, zoom
, -zoom
);
1401 fz_concat (&ctm
, &tm
, &sm
);
1402 fz_concat (&p
->lctm
, &ctm
, &rm
);
1408 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1409 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1410 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1411 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1415 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
1416 } while (p
-- != state
.pagedims
);
1420 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1421 desttoanchor (fz_link_dest
*dest
)
1425 struct pagedim
*pdim
= state
.pagedims
;
1430 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1431 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1433 pdim
= &state
.pagedims
[i
];
1435 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1437 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1438 p
.x
= dest
->ld
.gotor
.lt
.x
;
1441 p
.y
= dest
->ld
.gotor
.lt
.y
;
1442 fz_transform_point (&p
, &pdim
->lctm
);
1446 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1447 double x0
, x1
, y0
, y1
;
1449 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1450 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1452 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1453 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1455 a
.n
= dest
->ld
.gotor
.page
;
1460 static void recurse_outline (fz_outline
*outline
, int level
)
1463 switch (outline
->dest
.kind
) {
1466 struct anchor a
= desttoanchor (&outline
->dest
);
1469 printd ("o %d %d %d %d %s",
1470 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1476 printd ("ou %d %" FMT_s
" %s %s", level
,
1477 strlen (outline
->title
), outline
->title
,
1478 outline
->dest
.ld
.uri
.uri
);
1482 printd ("on %d %s", level
, outline
->title
);
1486 printd ("emsg Unhandled outline kind %d for %s\n",
1487 outline
->dest
.kind
, outline
->title
);
1490 if (outline
->down
) {
1491 recurse_outline (outline
->down
, level
+ 1);
1493 outline
= outline
->next
;
1497 static void process_outline (void)
1499 fz_outline
*outline
;
1501 if (!state
.needoutline
) return;
1503 state
.needoutline
= 0;
1504 switch (state
.type
) {
1506 outline
= pdf_load_outline (state
.u
.pdf
);
1509 outline
= xps_load_outline (state
.u
.xps
);
1516 recurse_outline (outline
, 0);
1517 fz_free_outline (state
.ctx
, outline
);
1521 static char *strofspan (fz_text_span
*span
)
1526 size_t size
= 0, cap
= 80;
1528 p
= malloc (cap
+ 1);
1529 if (!p
) return NULL
;
1531 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1532 int n
= fz_runetochar (utf8
, ch
->c
);
1533 if (size
+ n
> cap
) {
1535 p
= realloc (p
, cap
+ 1);
1536 if (!p
) return NULL
;
1539 memcpy (p
+ size
, utf8
, n
);
1546 static int matchspan (regex_t
*re
, fz_text_page
*page
,
1547 fz_text_span
*span
, fz_matrix ctm
,
1548 int stop
, int pageno
, double start
)
1555 fz_point p1
, p2
, p3
, p4
;
1557 p
= strofspan (span
);
1560 ret
= regexec (re
, p
, 1, &rm
, 0);
1563 if (ret
!= REG_NOMATCH
) {
1566 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1567 printd ("msg regexec error `%.*s'",
1568 (int) size
, errbuf
);
1576 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1577 c
+= fz_runelen (span
->text
[a
].c
);
1579 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1580 c
+= fz_runelen (span
->text
[b
].c
);
1583 if (fz_runelen (span
->text
[b
].c
) > 1) {
1587 fz_text_char_bbox (&sb
, span
, a
);
1588 fz_text_char_bbox (&eb
, span
, b
);
1600 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1607 printd ("progress 1 found at %d `%.*s' in %f sec",
1608 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1612 printd ("match %d %d %f %f %f %f %f %f %f %f",
1624 static int compareblocks (const void *l
, const void *r
)
1626 fz_text_block
const *ls
= l
;
1627 fz_text_block
const* rs
= r
;
1628 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1631 /* wishful thinking function */
1632 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1637 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1639 fz_text_sheet
*sheet
;
1640 struct pagedim
*pdim
, *pdimprev
;
1641 int stop
= 0, niters
= 0;
1644 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1648 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1649 if (niters
++ == 5) {
1652 printd ("progress 1 attention requested aborting search at %d",
1657 printd ("progress %f searching in page %d",
1658 (double) (pageno
+ 1) / state
.pagecount
,
1663 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1664 pdim
= &state
.pagedims
[i
];
1665 if (pdim
->pageno
== pageno
) {
1668 if (pdim
->pageno
> pageno
) {
1677 sheet
= fz_new_text_sheet (state
.ctx
);
1678 text
= fz_new_text_page (state
.ctx
);
1679 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1681 switch (state
.type
) {
1684 fz_try (state
.ctx
) {
1685 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1686 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1687 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1688 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1689 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1692 fz_catch (state
.ctx
) {
1693 fz_free_device (tdev
);
1700 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1701 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1705 ARSERT (0 && state
.type
);
1708 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1709 fz_free_device (tdev
);
1711 for (j
= 0; j
< text
->len
; ++j
) {
1714 fz_text_block
*block
;
1716 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1717 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1720 for (k
= 0; k
< block
->len
; ++k
) {
1725 line
= &block
->lines
[k
];
1726 if (line
->bbox
.y0
< y
+ 1) continue;
1729 line
= &block
->lines
[block
->len
- 1 - k
];
1730 if (line
->bbox
.y0
> y
- 1) continue;
1733 for (span
= line
->first_span
; span
; span
= span
->next
) {
1734 switch (matchspan (re
, text
, span
, ctm
,
1735 stop
, pageno
, start
)) {
1737 case 1: stop
= 1; break;
1738 case -1: stop
= 1; goto endloop
;
1753 fz_free_text_page (state
.ctx
, text
);
1754 fz_free_text_sheet (state
.ctx
, sheet
);
1756 state
.freepage (u
.ptr
);
1761 printd ("progress 1 no matches %f sec", end
- start
);
1763 printd ("clearrects");
1766 static void set_tex_params (int colorspace
)
1773 switch (colorspace
) {
1775 state
.texiform
= GL_RGBA8
;
1776 state
.texform
= GL_RGBA
;
1777 state
.texty
= GL_UNSIGNED_BYTE
;
1778 state
.colorspace
= fz_device_rgb (state
.ctx
);
1781 state
.texiform
= GL_RGBA8
;
1782 state
.texform
= GL_BGRA
;
1783 state
.texty
= endianness
.s
> 1
1784 ? GL_UNSIGNED_INT_8_8_8_8
1785 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1786 state
.colorspace
= fz_device_bgr (state
.ctx
);
1789 state
.texiform
= GL_LUMINANCE_ALPHA
;
1790 state
.texform
= GL_LUMINANCE_ALPHA
;
1791 state
.texty
= GL_UNSIGNED_BYTE
;
1792 state
.colorspace
= fz_device_gray (state
.ctx
);
1795 errx (1, "invalid colorspce %d", colorspace
);
1799 static void realloctexts (int texcount
)
1803 if (texcount
== state
.texcount
) return;
1805 if (texcount
< state
.texcount
) {
1806 glDeleteTextures (state
.texcount
- texcount
,
1807 state
.texids
+ texcount
);
1810 size
= texcount
* sizeof (*state
.texids
);
1811 state
.texids
= realloc (state
.texids
, size
);
1812 if (!state
.texids
) {
1813 err (1, "realloc texids %" FMT_s
, size
);
1816 size
= texcount
* sizeof (*state
.texowners
);
1817 state
.texowners
= realloc (state
.texowners
, size
);
1818 if (!state
.texowners
) {
1819 err (1, "realloc texowners %" FMT_s
, size
);
1821 if (texcount
> state
.texcount
) {
1824 glGenTextures (texcount
- state
.texcount
,
1825 state
.texids
+ state
.texcount
);
1826 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1827 state
.texowners
[i
].w
= -1;
1828 state
.texowners
[i
].slice
= NULL
;
1831 state
.texcount
= texcount
;
1835 static char *mbtoutf8 (char *s
)
1841 len
= mbstowcs (NULL
, s
, strlen (s
));
1846 if (len
== (size_t) -1) {
1851 tmp
= malloc (len
* sizeof (wchar_t));
1856 ret
= mbstowcs (tmp
, s
, len
);
1857 if (ret
== (size_t) -1) {
1863 for (i
= 0; i
< ret
; ++i
) {
1864 len
+= fz_runelen (tmp
[i
]);
1867 p
= r
= malloc (len
+ 1);
1873 for (i
= 0; i
< ret
; ++i
) {
1874 p
+= fz_runetochar (p
, tmp
[i
]);
1881 CAMLprim value
ml_mbtoutf8 (value s_v
)
1887 s
= String_val (s_v
);
1893 ret_v
= caml_copy_string (r
);
1899 static void * mainloop (void *unused
)
1902 int len
, ret
, oldlen
= 0;
1907 errx (1, "readlen returned 0");
1910 if (oldlen
< len
+ 1) {
1911 p
= realloc (p
, len
+ 1);
1913 err (1, "realloc %d failed", len
+ 1);
1920 if (!strncmp ("open", p
, 4)) {
1921 int wthack
, off
, ok
= 0;
1927 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1929 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1932 filename
= p
+ 5 + off
;
1933 filenamelen
= strlen (filename
);
1934 password
= filename
+ filenamelen
+ 1;
1937 fz_try (state
.ctx
) {
1938 openxref (filename
, password
);
1941 fz_catch (state
.ctx
) {
1942 utf8filename
= mbtoutf8 (filename
);
1943 printd ("msg Could not open %s", utf8filename
);
1953 utf8filename
= mbtoutf8 (filename
);
1954 printd ("msg Opened %s (press h/F1 to get help)",
1956 if (utf8filename
!= filename
) {
1957 free (utf8filename
);
1960 state
.needoutline
= 1;
1963 else if (!strncmp ("cs", p
, 2)) {
1966 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1968 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1971 set_tex_params (colorspace
);
1972 for (i
= 0; i
< state
.texcount
; ++i
) {
1973 state
.texowners
[i
].w
= -1;
1974 state
.texowners
[i
].slice
= NULL
;
1978 else if (!strncmp ("freepage", p
, 8)) {
1981 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1983 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1987 else if (!strncmp ("freetile", p
, 8)) {
1990 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1992 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1996 else if (!strncmp ("search", p
, 6)) {
1997 int icase
, pageno
, y
, ret
, len2
, forward
;
2001 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2002 &icase
, &pageno
, &y
, &forward
, &len2
);
2004 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2007 pattern
= p
+ 6 + len2
;
2008 ret
= regcomp (&re
, pattern
,
2009 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2014 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2015 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2018 search (&re
, pageno
, y
, forward
);
2022 else if (!strncmp ("geometry", p
, 8)) {
2026 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2028 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2036 for (i
= 0; i
< state
.texcount
; ++i
) {
2037 state
.texowners
[i
].slice
= NULL
;
2040 state
.fitmodel
= fitmodel
;
2045 unlock ("geometry");
2046 printd ("continue %d", state
.pagecount
);
2048 else if (!strncmp ("reqlayout", p
, 9)) {
2050 int rotate
, fitmodel
, off
, h
;
2053 ret
= sscanf (p
+ 9, " %d %d %d %n",
2054 &rotate
, &fitmodel
, &h
, &off
);
2056 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2059 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2062 state
.rotate
= rotate
;
2063 state
.fitmodel
= fitmodel
;
2068 nameddest
= p
+ 9 + off
;
2069 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2072 pdf_obj
*needle
, *obj
;
2074 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2075 strlen (nameddest
));
2076 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2078 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2080 a
= desttoanchor (&dest
);
2082 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2085 printd ("emsg failed to parse destination `%s'\n",
2090 printd ("emsg destination `%s' not found\n",
2093 pdf_drop_obj (needle
);
2097 unlock ("reqlayout");
2098 printd ("continue %d", state
.pagecount
);
2100 else if (!strncmp ("page", p
, 4)) {
2103 int pageno
, pindex
, ret
;
2105 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2107 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2112 page
= loadpage (pageno
, pindex
);
2116 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2118 else if (!strncmp ("tile", p
, 4)) {
2119 int x
, y
, w
, h
, ret
;
2125 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2126 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2127 SCN_ptr_cast (&data
));
2129 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2134 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2138 printd ("tile %d %d %" FMT_ptr
" %u %f",
2140 FMT_ptr_cast (tile
),
2141 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2144 else if (!strncmp ("settrim", p
, 7)) {
2148 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2149 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2151 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2155 state
.trimmargins
= trimmargins
;
2156 state
.needoutline
= 1;
2157 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2159 state
.trimfuzz
= fuzz
;
2161 state
.pagedimcount
= 0;
2162 free (state
.pagedims
);
2163 state
.pagedims
= NULL
;
2168 printd ("continue %d", state
.pagecount
);
2170 else if (!strncmp ("sliceh", p
, 6)) {
2173 ret
= sscanf (p
+ 6, " %d", &h
);
2175 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2177 if (h
!= state
.sliceheight
) {
2180 state
.sliceheight
= h
;
2181 for (i
= 0; i
< state
.texcount
; ++i
) {
2182 state
.texowners
[i
].w
= -1;
2183 state
.texowners
[i
].h
= -1;
2184 state
.texowners
[i
].slice
= NULL
;
2188 else if (!strncmp ("interrupt", p
, 9)) {
2189 printd ("vmsg interrupted");
2192 errx (1, "unknown command %.*s", len
, p
);
2198 CAMLprim value
ml_realloctexts (value texcount_v
)
2200 CAMLparam1 (texcount_v
);
2203 if (trylock ("ml_realloctexts")) {
2207 realloctexts (Int_val (texcount_v
));
2209 unlock ("ml_realloctexts");
2212 CAMLreturn (Val_bool (ok
));
2215 static void recti (int x0
, int y0
, int x1
, int y1
)
2217 GLfloat
*v
= state
.vertices
;
2219 glVertexPointer (2, GL_FLOAT
, 0, v
);
2220 v
[0] = x0
; v
[1] = y0
;
2221 v
[2] = x1
; v
[3] = y0
;
2222 v
[4] = x0
; v
[5] = y1
;
2223 v
[6] = x1
; v
[7] = y1
;
2224 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2227 static void showsel (struct page
*page
, int ox
, int oy
)
2233 fz_page_block
*pageb
;
2234 fz_text_block
*block
;
2235 struct mark first
, last
;
2236 unsigned char selcolor
[] = {15,15,15,140};
2238 first
= page
->fmark
;
2241 if (!first
.span
|| !last
.span
) return;
2243 glEnable (GL_BLEND
);
2244 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2245 glColor4ubv (selcolor
);
2247 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2248 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2249 for (pageb
= page
->text
->blocks
;
2250 pageb
< page
->text
->blocks
+ page
->text
->len
;
2252 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2253 block
= pageb
->u
.text
;
2255 for (line
= block
->lines
;
2256 line
< block
->lines
+ block
->len
;
2259 rect
= fz_empty_rect
;
2261 for (span
= line
->first_span
; span
; span
= span
->next
) {
2263 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2268 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2270 j
= MIN (first
.i
, last
.i
);
2271 k
= MAX (first
.i
, last
.i
);
2274 if (span
== first
.span
) {
2278 else if (span
== last
.span
) {
2285 for (i
= j
; i
<= k
; ++i
) {
2287 fz_union_rect (&rect
,
2288 fz_text_char_bbox (&bbox
, span
, i
));
2290 fz_round_rect (&bbox
, &rect
);
2291 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2298 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2299 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2300 if (span
== last
.span
) {
2303 rect
= fz_empty_rect
;
2309 glDisable (GL_BLEND
);
2314 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2316 fz_matrix ctm
, tm
, pm
;
2317 fz_link
*link
, *links
;
2318 GLfloat
*texcoords
= state
.texcoords
;
2319 GLfloat
*vertices
= state
.vertices
;
2321 switch (page
->type
) {
2323 links
= page
->u
.pdfpage
->links
;
2327 links
= page
->u
.xpspage
->links
;
2334 glEnable (GL_TEXTURE_1D
);
2335 glEnable (GL_BLEND
);
2336 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2337 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2339 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2340 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2341 fz_translate (&tm
, xoff
, yoff
);
2342 pm
= pagectm (page
);
2343 fz_concat (&ctm
, &pm
, &tm
);
2345 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2346 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2348 for (link
= links
; link
; link
= link
->next
) {
2349 fz_point p1
, p2
, p3
, p4
;
2351 p1
.x
= link
->rect
.x0
;
2352 p1
.y
= link
->rect
.y0
;
2354 p2
.x
= link
->rect
.x1
;
2355 p2
.y
= link
->rect
.y0
;
2357 p3
.x
= link
->rect
.x1
;
2358 p3
.y
= link
->rect
.y1
;
2360 p4
.x
= link
->rect
.x0
;
2361 p4
.y
= link
->rect
.y1
;
2363 fz_transform_point (&p1
, &ctm
);
2364 fz_transform_point (&p2
, &ctm
);
2365 fz_transform_point (&p3
, &ctm
);
2366 fz_transform_point (&p4
, &ctm
);
2368 switch (link
->dest
.kind
) {
2369 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2370 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2371 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2372 default: glColor3ub (0, 0, 0); break;
2380 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2384 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2386 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2387 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2389 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2390 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2392 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2393 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2395 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2396 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2398 glDrawArrays (GL_LINES
, 0, 8);
2401 glDisable (GL_BLEND
);
2402 glDisable (GL_TEXTURE_1D
);
2405 static int compareslinks (const void *l
, const void *r
)
2407 struct slink
const *ls
= l
;
2408 struct slink
const *rs
= r
;
2409 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2410 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2412 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2415 static void droptext (struct page
*page
)
2418 fz_free_text_page (state
.ctx
, page
->text
);
2421 page
->fmark
.span
= NULL
;
2422 page
->lmark
.span
= NULL
;
2426 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2431 static void dropslinks (struct page
*page
)
2434 free (page
->slinks
);
2435 page
->slinks
= NULL
;
2436 page
->slinkcount
= 0;
2440 static void ensureslinks (struct page
*page
)
2444 size_t slinksize
= sizeof (*page
->slinks
);
2445 fz_link
*link
, *links
;
2447 if (state
.gen
!= page
->sgen
) {
2449 page
->sgen
= state
.gen
;
2451 if (page
->slinks
) return;
2453 switch (page
->type
) {
2455 links
= page
->u
.pdfpage
->links
;
2456 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2458 &state
.pagedims
[page
->pdimno
].tctm
,
2459 &state
.pagedims
[page
->pdimno
].ctm
);
2463 links
= page
->u
.xpspage
->links
;
2464 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2471 for (link
= links
; link
; link
= link
->next
) {
2475 page
->slinkcount
= count
;
2476 page
->slinks
= calloc (count
, slinksize
);
2477 if (!page
->slinks
) {
2478 err (1, "realloc slinks %d", count
);
2481 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2485 fz_transform_rect (&rect
, &ctm
);
2486 page
->slinks
[i
].link
= link
;
2487 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2489 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2493 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2494 static void fmt_linkn (char *s
, unsigned int u
)
2496 unsigned int len
; unsigned int q
;
2497 int zma
= 'z' - 'a' + 1;
2499 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2502 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2503 /* handles u == 0 */
2508 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2509 int noff
, char *targ
, int tlen
, int hfsize
)
2513 struct slink
*slink
;
2514 double x0
, y0
, x1
, y1
, w
;
2516 ensureslinks (page
);
2517 glColor3ub (0xc3, 0xb0, 0x91);
2518 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2519 fmt_linkn (buf
, i
+ noff
);
2520 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2521 slink
= &page
->slinks
[i
];
2523 x0
= slink
->bbox
.x0
+ xoff
- 5;
2524 y1
= slink
->bbox
.y0
+ yoff
- 5;
2525 y0
= y1
+ 10 + hfsize
;
2526 w
= measure_string (state
.face
, hfsize
, buf
);
2528 recti (x0
, y0
, x1
, y1
);
2532 glEnable (GL_BLEND
);
2533 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2534 glEnable (GL_TEXTURE_2D
);
2535 glColor3ub (0, 0, 0);
2536 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2537 fmt_linkn (buf
, i
+ noff
);
2538 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2539 slink
= &page
->slinks
[i
];
2541 x0
= slink
->bbox
.x0
+ xoff
;
2542 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2543 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2546 glDisable (GL_TEXTURE_2D
);
2547 glDisable (GL_BLEND
);
2550 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2553 struct slice
*slice1
;
2554 unsigned char *texdata
;
2557 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2558 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2560 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2561 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2562 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2566 int texindex
= state
.texindex
++ % state
.texcount
;
2568 if (state
.texowners
[texindex
].w
== tile
->w
) {
2569 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2573 state
.texowners
[texindex
].h
= slice
->h
;
2577 state
.texowners
[texindex
].h
= slice
->h
;
2580 state
.texowners
[texindex
].w
= tile
->w
;
2581 state
.texowners
[texindex
].slice
= slice
;
2582 slice
->texindex
= texindex
;
2584 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2586 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2590 texdata
= tile
->pixmap
->samples
;
2593 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2605 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2617 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2622 CAMLprim value
ml_begintiles (value unit_v
)
2624 CAMLparam1 (unit_v
);
2625 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2626 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2627 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2628 CAMLreturn (unit_v
);
2631 CAMLprim value
ml_endtiles (value unit_v
)
2633 CAMLparam1 (unit_v
);
2634 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2635 CAMLreturn (unit_v
);
2638 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2640 CAMLparam2 (args_v
, ptr_v
);
2641 int dispx
= Int_val (Field (args_v
, 0));
2642 int dispy
= Int_val (Field (args_v
, 1));
2643 int dispw
= Int_val (Field (args_v
, 2));
2644 int disph
= Int_val (Field (args_v
, 3));
2645 int tilex
= Int_val (Field (args_v
, 4));
2646 int tiley
= Int_val (Field (args_v
, 5));
2647 char *s
= String_val (ptr_v
);
2648 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2649 int slicey
, firstslice
;
2650 struct slice
*slice
;
2651 GLfloat
*texcoords
= state
.texcoords
;
2652 GLfloat
*vertices
= state
.vertices
;
2654 firstslice
= tiley
/ tile
->sliceheight
;
2655 slice
= &tile
->slices
[firstslice
];
2656 slicey
= tiley
% tile
->sliceheight
;
2661 dh
= slice
->h
- slicey
;
2662 dh
= MIN (disph
, dh
);
2663 uploadslice (tile
, slice
);
2665 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2666 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2667 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2668 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2670 vertices
[0] = dispx
; vertices
[1] = dispy
;
2671 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2672 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2673 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2675 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2679 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2682 CAMLreturn (Val_unit
);
2685 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2686 value xoff_v
, value yoff_v
,
2689 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2690 int xoff
= Int_val (xoff_v
);
2691 int yoff
= Int_val (yoff_v
);
2692 int noff
= Int_val (Field (li_v
, 0));
2693 char *targ
= String_val (Field (li_v
, 1));
2694 int tlen
= caml_string_length (Field (li_v
, 1));
2695 int hfsize
= Int_val (Field (li_v
, 2));
2696 char *s
= String_val (ptr_v
);
2697 int hlmask
= Int_val (hlinks_v
);
2698 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2701 /* deal with loadpage failed pages */
2705 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2706 if (trylock ("ml_postprocess")) {
2711 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2712 noff
= page
->slinkcount
;
2714 if (page
->tgen
== state
.gen
) {
2715 showsel (page
, xoff
, yoff
);
2717 unlock ("ml_postprocess");
2720 CAMLreturn (Val_int (noff
));
2723 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2727 const fz_matrix
*tctm
;
2728 fz_link
*link
, *links
;
2730 switch (page
->type
) {
2732 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2733 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2734 links
= page
->u
.pdfpage
->links
;
2738 tctm
= &fz_identity
;
2739 links
= page
->u
.xpspage
->links
;
2748 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2749 fz_invert_matrix (&ctm
, &ctm
);
2750 fz_transform_point (&p
, &ctm
);
2752 for (link
= links
; link
; link
= link
->next
) {
2753 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2754 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2762 static void ensuretext (struct page
*page
)
2764 if (state
.gen
!= page
->tgen
) {
2766 page
->tgen
= state
.gen
;
2772 page
->text
= fz_new_text_page (state
.ctx
);
2773 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2774 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2775 ctm
= pagectm (page
);
2776 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2777 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2778 qsort (page
->text
->blocks
, page
->text
->len
,
2779 sizeof (*page
->text
->blocks
), compareblocks
);
2781 fz_free_device (tdev
);
2785 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2787 CAMLparam2 (start_page_v
, dir_v
);
2789 int i
, dir
= Int_val (dir_v
);
2790 int start_page
= Int_val (start_page_v
);
2791 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2793 ret_v
= Val_int (0);
2794 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2798 lock ("ml_findpage_with_links");
2799 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2802 switch (state
.type
) {
2805 pdf_page
*page
= NULL
;
2807 fz_try (state
.ctx
) {
2808 page
= pdf_load_page (state
.u
.pdf
, i
);
2809 found
= !!page
->links
;
2811 fz_catch (state
.ctx
) {
2821 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2822 found
= !!page
->links
;
2828 ARSERT (0 && "invalid document type");
2832 ret_v
= caml_alloc_small (1, 1);
2833 Field (ret_v
, 0) = Val_int (i
);
2838 unlock ("ml_findpage_with_links");
2844 enum { dir_first
, dir_last
};
2845 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2847 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2849 CAMLparam2 (ptr_v
, dir_v
);
2850 CAMLlocal2 (ret_v
, pos_v
);
2852 int dirtag
, i
, slinkindex
;
2853 struct slink
*found
= NULL
,*slink
;
2854 char *s
= String_val (ptr_v
);
2856 page
= parse_pointer ("ml_findlink", s
);
2857 ret_v
= Val_int (0);
2858 if (trylock ("ml_findlink")) {
2862 ensureslinks (page
);
2864 if (Is_block (dir_v
)) {
2865 dirtag
= Tag_val (dir_v
);
2867 case dir_first_visible
:
2869 int x0
, y0
, dir
, first_index
, last_index
;
2871 pos_v
= Field (dir_v
, 0);
2872 x0
= Int_val (Field (pos_v
, 0));
2873 y0
= Int_val (Field (pos_v
, 1));
2874 dir
= Int_val (Field (pos_v
, 2));
2879 last_index
= page
->slinkcount
;
2882 first_index
= page
->slinkcount
- 1;
2886 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2887 slink
= &page
->slinks
[i
];
2888 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2897 slinkindex
= Int_val (Field (dir_v
, 0));
2898 found
= &page
->slinks
[slinkindex
];
2899 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2900 slink
= &page
->slinks
[i
];
2901 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2909 slinkindex
= Int_val (Field (dir_v
, 0));
2910 found
= &page
->slinks
[slinkindex
];
2911 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2912 slink
= &page
->slinks
[i
];
2913 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2921 slinkindex
= Int_val (Field (dir_v
, 0));
2922 found
= &page
->slinks
[slinkindex
];
2923 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2924 slink
= &page
->slinks
[i
];
2925 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2933 slinkindex
= Int_val (Field (dir_v
, 0));
2934 found
= &page
->slinks
[slinkindex
];
2935 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2936 slink
= &page
->slinks
[i
];
2937 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2946 dirtag
= Int_val (dir_v
);
2949 found
= page
->slinks
;
2954 found
= page
->slinks
+ (page
->slinkcount
- 1);
2960 ret_v
= caml_alloc_small (2, 1);
2961 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2964 unlock ("ml_findlink");
2969 enum { uuri
, ugoto
, utext
, uunexpected
,
2970 ulaunch
, unamed
, uremote
, uremotedest
};
2976 switch (link->dest.kind) { \
2977 case FZ_LINK_GOTO: \
2981 pageno = link->dest.ld.gotor.page; \
2985 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2986 p.y = link->dest.ld.gotor.lt.y; \
2987 fz_transform_point (&p, &pdim->lctm); \
2988 if (p.y < 0) p.y = 0; \
2990 tup_v = caml_alloc_tuple (2); \
2991 ret_v = caml_alloc_small (1, ugoto); \
2992 Field (tup_v, 0) = Val_int (pageno); \
2993 Field (tup_v, 1) = Val_int (p.y); \
2994 Field (ret_v, 0) = tup_v; \
2999 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3000 ret_v = caml_alloc_small (1, uuri); \
3001 Field (ret_v, 0) = str_v; \
3004 case FZ_LINK_LAUNCH: \
3005 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3006 ret_v = caml_alloc_small (1, ulaunch); \
3007 Field (ret_v, 0) = str_v; \
3010 case FZ_LINK_NAMED: \
3011 str_v = caml_copy_string (link->dest.ld.named.named); \
3012 ret_v = caml_alloc_small (1, unamed); \
3013 Field (ret_v, 0) = str_v; \
3016 case FZ_LINK_GOTOR: \
3020 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3021 pageno = link->dest.ld.gotor.page; \
3022 if (pageno == -1) { \
3023 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3024 rty = uremotedest; \
3027 gr_v = Val_int (pageno); \
3030 tup_v = caml_alloc_tuple (2); \
3031 ret_v = caml_alloc_small (1, rty); \
3032 Field (tup_v, 0) = str_v; \
3033 Field (tup_v, 1) = gr_v; \
3034 Field (ret_v, 0) = tup_v; \
3042 snprintf (buf, sizeof (buf), \
3043 "unhandled link kind %d", link->dest.kind); \
3044 str_v = caml_copy_string (buf); \
3045 ret_v = caml_alloc_small (1, uunexpected); \
3046 Field (ret_v, 0) = str_v; \
3052 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3054 CAMLparam2 (ptr_v
, n_v
);
3055 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3058 struct pagedim
*pdim
;
3059 char *s
= String_val (ptr_v
);
3061 ret_v
= Val_int (0);
3062 if (trylock ("ml_getlink")) {
3066 page
= parse_pointer ("ml_getlink", s
);
3067 ensureslinks (page
);
3068 pdim
= &state
.pagedims
[page
->pdimno
];
3069 link
= page
->slinks
[Int_val (n_v
)].link
;
3072 unlock ("ml_getlink");
3077 CAMLprim value
ml_getlinkcount (value ptr_v
)
3081 char *s
= String_val (ptr_v
);
3083 page
= parse_pointer ("ml_getlinkcount", s
);
3084 CAMLreturn (Val_int (page
->slinkcount
));
3087 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3089 CAMLparam2 (ptr_v
, n_v
);
3092 struct slink
*slink
;
3093 char *s
= String_val (ptr_v
);
3095 page
= parse_pointer ("ml_getlinkrect", s
);
3096 ret_v
= caml_alloc_tuple (4);
3097 if (trylock ("ml_getlinkrect")) {
3098 Field (ret_v
, 0) = Val_int (0);
3099 Field (ret_v
, 1) = Val_int (0);
3100 Field (ret_v
, 2) = Val_int (0);
3101 Field (ret_v
, 3) = Val_int (0);
3104 ensureslinks (page
);
3106 slink
= &page
->slinks
[Int_val (n_v
)];
3107 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3108 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3109 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3110 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3111 unlock ("ml_getlinkrect");
3117 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3119 CAMLparam3 (ptr_v
, x_v
, y_v
);
3120 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3123 char *s
= String_val (ptr_v
);
3124 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3125 struct pagedim
*pdim
;
3127 ret_v
= Val_int (0);
3128 if (trylock ("ml_whatsunder")) {
3132 page
= parse_pointer ("ml_whatsunder", s
);
3133 pdim
= &state
.pagedims
[page
->pdimno
];
3134 x
+= pdim
->bounds
.x0
;
3135 y
+= pdim
->bounds
.y0
;
3136 link
= getlink (page
, x
, y
);
3142 fz_page_block
*pageb
;
3143 fz_text_block
*block
;
3146 for (pageb
= page
->text
->blocks
;
3147 pageb
< page
->text
->blocks
+ page
->text
->len
;
3150 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3151 block
= pageb
->u
.text
;
3154 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3157 for (line
= block
->lines
;
3158 line
< block
->lines
+ block
->len
;
3163 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3166 for (span
= line
->first_span
; span
; span
= span
->next
) {
3170 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3173 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3175 fz_text_char_bbox (&bbox
, span
, charnum
);
3178 if (x
>= b
->x0
&& x
<= b
->x1
3179 && y
>= b
->y0
&& y
<= b
->y1
) {
3180 fz_text_style
*style
= span
->text
->style
;
3182 style
->font
&& style
->font
->name
3184 : "Span has no font name"
3186 FT_FaceRec
*face
= style
->font
->ft_face
;
3187 if (face
&& face
->family_name
) {
3189 char *n1
= face
->family_name
;
3190 size_t l1
= strlen (n1
);
3191 size_t l2
= strlen (n2
);
3193 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3194 s
= malloc (l1
+ l2
+ 2);
3198 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3199 str_v
= caml_copy_string (s
);
3205 str_v
= caml_copy_string (n2
);
3207 ret_v
= caml_alloc_small (1, utext
);
3208 Field (ret_v
, 0) = str_v
;
3217 unlock ("ml_whatsunder");
3223 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3225 static int uninteresting (int c
)
3227 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3231 CAMLprim value
ml_clearmark (value ptr_v
)
3234 char *s
= String_val (ptr_v
);
3237 if (trylock ("ml_clearmark")) {
3241 page
= parse_pointer ("ml_clearmark", s
);
3242 page
->fmark
.span
= NULL
;
3243 page
->lmark
.span
= NULL
;
3247 unlock ("ml_clearmark");
3249 CAMLreturn (Val_unit
);
3252 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3254 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3259 fz_page_block
*pageb
;
3260 fz_text_block
*block
;
3261 struct pagedim
*pdim
;
3262 int mark
= Int_val (mark_v
);
3263 char *s
= String_val (ptr_v
);
3264 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3266 ret_v
= Val_bool (0);
3267 if (trylock ("ml_markunder")) {
3271 page
= parse_pointer ("ml_markunder", s
);
3272 pdim
= &state
.pagedims
[page
->pdimno
];
3276 if (mark
== mark_page
) {
3278 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3280 for (i
= 0; i
< page
->text
->len
; ++i
) {
3281 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3282 pb1
= &page
->text
->blocks
[i
];
3286 if (!pb1
) goto unlock
;
3288 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3289 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3290 pb2
= &page
->text
->blocks
[i
];
3294 if (!pb2
) goto unlock
;
3296 block
= pb1
->u
.text
;
3299 page
->fmark
.span
= block
->lines
->first_span
;
3301 block
= pb2
->u
.text
;
3302 line
= &block
->lines
[block
->len
- 1];
3303 page
->lmark
.i
= line
->last_span
->len
- 1;
3304 page
->lmark
.span
= line
->last_span
;
3305 ret_v
= Val_bool (1);
3309 x
+= pdim
->bounds
.x0
;
3310 y
+= pdim
->bounds
.y0
;
3312 for (pageb
= page
->text
->blocks
;
3313 pageb
< page
->text
->blocks
+ page
->text
->len
;
3315 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3316 block
= pageb
->u
.text
;
3319 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3322 if (mark
== mark_block
) {
3324 page
->fmark
.span
= block
->lines
->first_span
;
3326 line
= &block
->lines
[block
->len
- 1];
3327 page
->lmark
.i
= line
->last_span
->len
- 1;
3328 page
->lmark
.span
= line
->last_span
;
3329 ret_v
= Val_bool (1);
3333 for (line
= block
->lines
;
3334 line
< block
->lines
+ block
->len
;
3339 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3342 if (mark
== mark_line
) {
3344 page
->fmark
.span
= line
->first_span
;
3346 page
->lmark
.i
= line
->last_span
->len
- 1;
3347 page
->lmark
.span
= line
->last_span
;
3348 ret_v
= Val_bool (1);
3352 for (span
= line
->first_span
; span
; span
= span
->next
) {
3356 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3359 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3361 fz_text_char_bbox (&bbox
, span
, charnum
);
3364 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3366 int charnum2
, charnum3
= -1, charnum4
= -1;
3368 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3370 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3371 if (uninteresting (span
->text
[charnum2
].c
)) {
3372 charnum3
= charnum2
+ 1;
3376 if (charnum3
== -1) charnum3
= 0;
3378 for (charnum2
= charnum
+ 1;
3379 charnum2
< span
->len
;
3381 if (uninteresting (span
->text
[charnum2
].c
)) break;
3382 charnum4
= charnum2
;
3384 if (charnum4
== -1) goto unlock
;
3386 page
->fmark
.i
= charnum3
;
3387 page
->fmark
.span
= span
;
3389 page
->lmark
.i
= charnum4
;
3390 page
->lmark
.span
= span
;
3391 ret_v
= Val_bool (1);
3399 if (!Bool_val (ret_v
)) {
3400 page
->fmark
.span
= NULL
;
3401 page
->lmark
.span
= NULL
;
3405 unlock ("ml_markunder");
3411 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3413 CAMLparam3 (ptr_v
, x_v
, y_v
);
3414 CAMLlocal2 (ret_v
, res_v
);
3417 fz_page_block
*pageb
;
3418 struct pagedim
*pdim
;
3419 char *s
= String_val (ptr_v
);
3420 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3422 ret_v
= Val_int (0);
3423 if (trylock ("ml_rectofblock")) {
3427 page
= parse_pointer ("ml_rectofblock", s
);
3428 pdim
= &state
.pagedims
[page
->pdimno
];
3429 x
+= pdim
->bounds
.x0
;
3430 y
+= pdim
->bounds
.y0
;
3434 for (pageb
= page
->text
->blocks
;
3435 pageb
< page
->text
->blocks
+ page
->text
->len
;
3437 switch (pageb
->type
) {
3438 case FZ_PAGE_BLOCK_TEXT
:
3439 b
= &pageb
->u
.text
->bbox
;
3442 case FZ_PAGE_BLOCK_IMAGE
:
3443 b
= &pageb
->u
.image
->bbox
;
3450 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3455 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3456 ret_v
= caml_alloc_small (1, 1);
3457 Store_double_field (res_v
, 0, b
->x0
);
3458 Store_double_field (res_v
, 1, b
->x1
);
3459 Store_double_field (res_v
, 2, b
->y0
);
3460 Store_double_field (res_v
, 3, b
->y1
);
3461 Field (ret_v
, 0) = res_v
;
3463 unlock ("ml_rectofblock");
3469 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3471 CAMLparam2 (ptr_v
, rect_v
);
3474 struct pagedim
*pdim
;
3475 char *s
= String_val (ptr_v
);
3476 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3478 fz_page_block
*pageb
;
3479 fz_text_block
*block
;
3480 fz_text_span
*span
, *fspan
, *lspan
;
3482 if (trylock ("ml_seltext")) {
3486 page
= parse_pointer ("ml_seltext", s
);
3489 pdim
= &state
.pagedims
[page
->pdimno
];
3490 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3491 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3492 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3493 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3504 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3505 glColor3ub (128, 128, 128);
3506 recti (x0
, y0
, x1
, y1
);
3507 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3511 fspan
= page
->fmark
.span
;
3514 lspan
= page
->lmark
.span
;
3516 for (pageb
= page
->text
->blocks
;
3517 pageb
< page
->text
->blocks
+ page
->text
->len
;
3519 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3520 block
= pageb
->u
.text
;
3521 for (line
= block
->lines
;
3522 line
< block
->lines
+ block
->len
;
3526 for (span
= line
->first_span
; span
; span
= span
->next
) {
3527 for (i
= 0; i
< span
->len
; ++i
) {
3530 fz_text_char_bbox (&b
, span
, i
);
3532 if (x0
>= b
.x0
&& x0
<= b
.x1
3533 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3538 if (x1
>= b
.x0
&& x1
<= b
.x1
3539 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3544 if (0 && selected
) {
3545 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3546 glColor3ub (128, 128, 128);
3547 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3548 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3554 if (x1
< x0
&& fspan
== lspan
) {
3566 page
->fmark
.span
= fspan
;
3569 page
->lmark
.span
= lspan
;
3571 unlock ("ml_seltext");
3574 CAMLreturn (Val_unit
);
3577 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3582 for (i
= a
; i
<= b
; ++i
) {
3583 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3584 ret
= fwrite (buf
, len
, 1, f
);
3587 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3588 len
, ret
, strerror (errno
));
3596 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3598 caml_failwith ("ml_popen not implemented under Cygwin");
3601 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3603 CAMLparam2 (command_v
, fds_v
);
3604 CAMLlocal2 (l_v
, tup_v
);
3607 value earg_v
= Nothing
;
3608 posix_spawnattr_t attr
;
3609 posix_spawn_file_actions_t fa
;
3610 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3612 argv
[2] = String_val (command_v
);
3614 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3615 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3618 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3619 msg
= "posix_spawnattr_init";
3623 #ifdef POSIX_SPAWN_USEVFORK
3624 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3625 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3630 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3633 tup_v
= Field (l_v
, 0);
3634 fd1
= Int_val (Field (tup_v
, 0));
3635 fd2
= Int_val (Field (tup_v
, 1));
3637 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3638 msg
= "posix_spawn_file_actions_addclose";
3644 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3645 msg
= "posix_spawn_file_actions_adddup2";
3652 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3653 msg
= "posix_spawn";
3658 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3659 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3663 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3664 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3669 unix_error (ret
, msg
, earg_v
);
3671 CAMLreturn (Val_unit
);
3675 CAMLprim value
ml_hassel (value ptr_v
)
3680 char *s
= String_val (ptr_v
);
3682 ret_v
= Val_bool (0);
3683 if (trylock ("ml_hassel")) {
3687 page
= parse_pointer ("ml_hassel", s
);
3688 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3689 unlock ("ml_hassel");
3694 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3696 CAMLparam2 (fd_v
, ptr_v
);
3701 fz_page_block
*pageb
;
3702 fz_text_block
*block
;
3703 int fd
= Int_val (fd_v
);
3704 char *s
= String_val (ptr_v
);
3706 if (trylock ("ml_copysel")) {
3710 page
= parse_pointer ("ml_copysel", s
);
3712 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3713 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3717 f
= fdopen (fd
, "w");
3719 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3720 fd
, strerror (errno
));
3724 for (pageb
= page
->text
->blocks
;
3725 pageb
< page
->text
->blocks
+ page
->text
->len
;
3727 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3728 block
= pageb
->u
.text
;
3729 for (line
= block
->lines
;
3730 line
< block
->lines
+ block
->len
;
3734 for (span
= line
->first_span
; span
; span
= span
->next
) {
3737 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3738 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3739 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3742 if (pipespan (f
, span
, a
, b
)) {
3745 if (span
== page
->lmark
.span
) {
3748 if (span
== line
->last_span
) {
3749 if (putc ('\n', f
) == EOF
) {
3751 "failed break line on sel pipe: %s\n",
3762 int ret
= fclose (f
);
3765 if (errno
!= ECHILD
) {
3766 fprintf (stderr
, "failed to close sel pipe: %s\n",
3772 unlock ("ml_copysel");
3777 fprintf (stderr
, "failed to close sel pipe: %s\n",
3781 CAMLreturn (Val_unit
);
3784 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3786 CAMLparam1 (pagedimno_v
);
3788 int pagedimno
= Int_val (pagedimno_v
);
3791 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3792 if (trylock ("ml_getpdimrect")) {
3793 box
= fz_empty_rect
;
3796 box
= state
.pagedims
[pagedimno
].mediabox
;
3797 unlock ("ml_getpdimrect");
3800 Store_double_field (ret_v
, 0, box
.x0
);
3801 Store_double_field (ret_v
, 1, box
.x1
);
3802 Store_double_field (ret_v
, 2, box
.y0
);
3803 Store_double_field (ret_v
, 3, box
.y1
);
3808 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3809 value dw_v
, value cols_v
)
3811 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3817 double winw
= Int_val (winw_v
);
3818 double winh
= Int_val (winh_v
);
3819 double dw
= Int_val (dw_v
);
3820 double cols
= Int_val (cols_v
);
3821 double pw
= 1.0, ph
= 1.0;
3823 if (trylock ("ml_zoom_for_height")) {
3827 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3828 double w
= p
->pagebox
.x1
/ cols
;
3829 double h
= p
->pagebox
.y1
;
3833 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3835 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3838 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3839 unlock ("ml_zoom_for_height");
3841 ret_v
= caml_copy_double (zoom
);
3845 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3847 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3849 int pt
= Int_val(pt_v
);
3850 int x
= Int_val (x_v
);
3851 int y
= Int_val (y_v
);
3854 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3855 ret_v
= caml_copy_double (w
);
3859 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3861 CAMLparam2 (pt_v
, string_v
);
3863 int pt
= Int_val (pt_v
);
3866 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3867 ret_v
= caml_copy_double (w
);
3871 CAMLprim value
ml_getpagebox (value opaque_v
)
3873 CAMLparam1 (opaque_v
);
3879 char *s
= String_val (opaque_v
);
3880 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3882 ret_v
= caml_alloc_tuple (4);
3883 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3884 dev
->hints
|= FZ_IGNORE_SHADE
;
3886 switch (page
->type
) {
3888 ctm
= pagectm (page
);
3889 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3893 ctm
= pagectm (page
);
3894 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3898 rect
= fz_infinite_rect
;
3902 fz_free_device (dev
);
3903 fz_round_rect (&bbox
, &rect
);
3904 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3905 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3906 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3907 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3912 CAMLprim value
ml_setaalevel (value level_v
)
3914 CAMLparam1 (level_v
);
3916 state
.aalevel
= Int_val (level_v
);
3917 CAMLreturn (Val_unit
);
3920 #pragma GCC diagnostic push
3921 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3922 #include <X11/Xlib.h>
3923 #pragma GCC diagnostic pop
3931 XVisualInfo
*visual
;
3934 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3936 CAMLparam3 (display_v
, wid_v
, screen_v
);
3937 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3939 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3941 caml_failwith ("XOpenDisplay");
3944 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3946 XCloseDisplay (glx
.dpy
);
3947 caml_failwith ("glXChooseVisual");
3950 glx
.wid
= Int_val (wid_v
);
3951 CAMLreturn (Val_int (glx
.visual
->visualid
));
3954 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3956 CAMLparam1 (unit_v
);
3958 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3960 caml_failwith ("glXCreateContext");
3966 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3967 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3969 caml_failwith ("glXMakeCurrent");
3971 CAMLreturn (Val_unit
);
3974 CAMLprim value
ml_swapb (value unit_v
)
3976 CAMLparam1 (unit_v
);
3977 glXSwapBuffers (glx
.dpy
, glx
.wid
);
3978 CAMLreturn (Val_unit
);
3981 #include "keysym2ucs.c"
3983 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
3985 CAMLparam1 (keysym_v
);
3987 KeySym keysym
= Int_val (keysym_v
);
3992 rune
= keysym2ucs (keysym
);
3993 len
= fz_runetochar (buf
, rune
);
3995 str_v
= caml_copy_string (buf
);
3999 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
4000 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
4002 CAMLprim value
ml_platform (value unit_v
)
4004 CAMLparam1 (unit_v
);
4005 int platid
= piunknown
;
4007 #if defined __linux__
4009 #elif defined __CYGWIN__
4011 #elif defined __DragonFly__
4012 platid
= pidragonflybsd
;
4013 #elif defined __FreeBSD__
4015 #elif defined __OpenBSD__
4017 #elif defined __NetBSD__
4019 #elif defined __sun__
4021 #elif defined __APPLE__
4024 CAMLreturn (Val_int (platid
));
4027 CAMLprim value
ml_cloexec (value fd_v
)
4030 int fd
= Int_val (fd_v
);
4032 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4033 uerror ("fcntl", Nothing
);
4035 CAMLreturn (Val_unit
);
4038 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4040 CAMLparam2 (w_v
, h_v
);
4043 int w
= Int_val (w_v
);
4044 int h
= Int_val (h_v
);
4045 int cs
= Int_val (cs_v
);
4047 if (state
.pbo_usable
) {
4048 pbo
= calloc (sizeof (*pbo
), 1);
4050 err (1, "calloc pbo");
4062 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4065 state
.glGenBuffersARB (1, &pbo
->id
);
4066 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4067 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4068 NULL
, GL_STREAM_DRAW
);
4069 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4071 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4073 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4074 state
.glDeleteBuffersARB (1, &pbo
->id
);
4076 ret_v
= caml_copy_string ("0");
4082 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4084 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4088 err (1, "malloc %d bytes failed", res
+1);
4090 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4092 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4094 ret_v
= caml_copy_string (s
);
4099 ret_v
= caml_copy_string ("0");
4104 CAMLprim value
ml_freepbo (value s_v
)
4107 char *s
= String_val (s_v
);
4108 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4111 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4113 tile
->pbo
->ptr
= NULL
;
4114 tile
->pbo
->size
= -1;
4116 CAMLreturn (Val_unit
);
4119 CAMLprim value
ml_unmappbo (value s_v
)
4122 char *s
= String_val (s_v
);
4123 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4126 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4127 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4128 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4130 tile
->pbo
->ptr
= NULL
;
4131 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4133 CAMLreturn (Val_unit
);
4136 static void setuppbo (void)
4138 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4139 state
.pbo_usable
= GGPA (glBindBufferARB
)
4140 && GGPA (glUnmapBufferARB
)
4141 && GGPA (glMapBufferARB
)
4142 && GGPA (glBufferDataARB
)
4143 && GGPA (glGenBuffersARB
)
4144 && GGPA (glDeleteBuffersARB
);
4148 CAMLprim value
ml_pbo_usable (value unit_v
)
4150 CAMLparam1 (unit_v
);
4151 CAMLreturn (Val_bool (state
.pbo_usable
));
4154 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4156 CAMLparam3 (ptr_v
, x_v
, y_v
);
4157 CAMLlocal2 (ret_v
, tup_v
);
4159 char *s
= String_val (ptr_v
);
4160 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4161 struct pagedim
*pdim
;
4165 page
= parse_pointer ("ml_unproject", s
);
4166 pdim
= &state
.pagedims
[page
->pdimno
];
4168 ret_v
= Val_int (0);
4169 if (trylock ("ml_unproject")) {
4173 switch (page
->type
) {
4175 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4181 p
.x
= x
+ pdim
->bounds
.x0
;
4182 p
.y
= y
+ pdim
->bounds
.y0
;
4184 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4185 fz_invert_matrix (&ctm
, &ctm
);
4186 fz_transform_point (&p
, &ctm
);
4188 tup_v
= caml_alloc_tuple (2);
4189 ret_v
= caml_alloc_small (1, 1);
4190 Field (tup_v
, 0) = Val_int (p
.x
);
4191 Field (tup_v
, 1) = Val_int (p
.y
);
4192 Field (ret_v
, 0) = tup_v
;
4194 unlock ("ml_unproject");
4199 static void makestippletex (void)
4201 const char pixels
[] = "\xff\xff\0\0";
4202 glGenTextures (1, &state
.stid
);
4203 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4204 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4205 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4218 CAMLprim value
ml_fz_version (value unit_v
)
4220 return caml_copy_string (FZ_VERSION
);
4223 #ifdef USE_FONTCONFIG
4229 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4233 int needs_exact_metrics
)
4240 FcPattern
*pat
, *pat1
;
4242 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4243 name
, bold
, italic
, needs_exact_metrics
);
4246 fc
.config
= FcInitLoadConfigAndFonts ();
4248 lprintf ("FcInitLoadConfigAndFonts failed\n");
4252 if (!fc
.config
) return NULL
;
4254 size
= strlen (name
);
4255 if (bold
) size
+= sizeof (":bold") - 1;
4256 if (italic
) size
+= sizeof (":italic") - 1;
4259 buf
= malloc (size
);
4261 err (1, "malloc %zu failed", size
);
4265 if (bold
&& italic
) {
4266 strcat (buf
, ":bold:italic");
4269 if (bold
) strcat (buf
, ":bold");
4270 if (italic
) strcat (buf
, ":italic");
4272 for (i
= 0; i
< size
; ++i
) {
4273 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4276 lprintf ("fcbuf=%s\n", buf
);
4277 pat
= FcNameParse ((FcChar8
*) buf
);
4279 printd ("emsg FcNameParse failed\n");
4284 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4285 printd ("emsg FcConfigSubstitute failed\n");
4289 FcDefaultSubstitute (pat
);
4291 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4293 printd ("emsg FcFontMatch failed\n");
4294 FcPatternDestroy (pat
);
4299 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4300 printd ("emsg FcPatternGetString failed\n");
4301 FcPatternDestroy (pat
);
4302 FcPatternDestroy (pat1
);
4308 printd ("emsg name=%s path=%s\n", name
, path
);
4310 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4311 FcPatternDestroy (pat
);
4312 FcPatternDestroy (pat1
);
4318 CAMLprim value
ml_init (value csock_v
, value params_v
)
4320 CAMLparam2 (csock_v
, params_v
);
4321 CAMLlocal2 (trim_v
, fuzz_v
);
4328 struct sigaction sa
;
4330 state
.csock
= Int_val (csock_v
);
4331 state
.rotate
= Int_val (Field (params_v
, 0));
4332 state
.fitmodel
= Int_val (Field (params_v
, 1));
4333 trim_v
= Field (params_v
, 2);
4334 texcount
= Int_val (Field (params_v
, 3));
4335 state
.sliceheight
= Int_val (Field (params_v
, 4));
4336 mustoresize
= Int_val (Field (params_v
, 5));
4337 colorspace
= Int_val (Field (params_v
, 6));
4338 fontpath
= String_val (Field (params_v
, 7));
4340 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4341 if (!state
.trimcachepath
) {
4342 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4345 haspboext
= Bool_val (Field (params_v
, 9));
4347 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4349 #ifdef USE_FONTCONFIG
4350 fz_install_load_system_font_funcs (
4351 state
.ctx
, fc_load_system_font_func
, NULL
4355 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4356 fuzz_v
= Field (trim_v
, 1);
4357 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4358 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4359 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4360 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4362 set_tex_params (colorspace
);
4365 #ifndef USE_FONTCONFIG
4366 state
.face
= load_font (fontpath
);
4370 char *buf
= fontpath
;
4371 FcPattern
*pat
, *pat1
;
4374 fc
.config
= FcInitLoadConfigAndFonts ();
4376 errx (1, "FcInitLoadConfigAndFonts failed");
4379 pat
= FcNameParse ((FcChar8
*) buf
);
4381 errx (1, "FcNameParse failed");
4384 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4385 errx (1, "FcConfigSubstitute failed");
4387 FcDefaultSubstitute (pat
);
4389 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4391 errx (1, "FcFontMatch failed");
4394 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4395 errx (1, "FcPatternGetString failed");
4398 state
.face
= load_font ((char *) path
);
4399 FcPatternDestroy (pat
);
4400 FcPatternDestroy (pat1
);
4405 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4407 state
.face
= load_builtin_font (base
, len
);
4409 if (!state
.face
) _exit (1);
4411 realloctexts (texcount
);
4420 sa
.sa_handler
= SIG_IGN
;
4421 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4423 sa
.sa_handler
= SIG_DFL
;
4424 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4426 if (sigemptyset (&sa
.sa_mask
)) {
4427 err (1, "sigemptyset");
4429 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4430 err (1, "sigaction");
4433 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4435 errx (1, "pthread_create: %s", strerror (ret
));
4438 CAMLreturn (Val_unit
);