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 CACHE_PAGEREFS
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 { DNONE
, 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 CACHE_PAGEREFS
275 static void UNUSED_ATTR
debug_rect (const char *cap
, fz_rect r
)
277 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
280 static void UNUSED_ATTR
debug_bbox (const char *cap
, fz_irect r
)
282 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
285 static void UNUSED_ATTR
debug_matrix (const char *cap
, fz_matrix m
)
287 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
288 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
291 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
293 static void lock (const char *cap
)
295 int ret
= pthread_mutex_lock (&mutex
);
297 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
301 static void unlock (const char *cap
)
303 int ret
= pthread_mutex_unlock (&mutex
);
305 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
309 static int trylock (const char *cap
)
311 int ret
= pthread_mutex_trylock (&mutex
);
312 if (ret
&& ret
!= EBUSY
) {
313 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
318 static void *parse_pointer (const char *cap
, const char *s
)
323 ret
= sscanf (s
, "%" SCN_ptr
, SCN_ptr_cast (&ptr
));
325 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
330 static double now (void)
334 if (gettimeofday (&tv
, NULL
)) {
335 err (1, "gettimeofday");
337 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
340 static int hasdata (void)
343 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
344 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
348 CAMLprim value
ml_hasdata (value fd_v
)
353 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
354 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
355 CAMLreturn (Val_bool (avail
> 0));
358 static void readdata (void *p
, int size
)
363 n
= read (state
.csock
, p
, size
);
365 if (errno
== EINTR
) goto again
;
366 err (1, "read (req %d, ret %zd)", size
, n
);
369 if (!n
) errx (1, "EOF while reading");
370 errx (1, "read (req %d, ret %zd)", size
, n
);
374 static void writedata (char *p
, int size
)
378 p
[0] = (size
>> 24) & 0xff;
379 p
[1] = (size
>> 16) & 0xff;
380 p
[2] = (size
>> 8) & 0xff;
381 p
[3] = (size
>> 0) & 0xff;
383 n
= write (state
.csock
, p
, size
+ 4);
385 if (!n
) errx (1, "EOF while writing data");
386 err (1, "write (req %d, ret %zd)", size
+ 4, n
);
390 static int readlen (void)
395 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
398 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
406 if (!buf
) err (1, "malloc for temp buf (%d bytes) failed", size
);
409 len
= vsnprintf (buf
+ 4, size
- 4, fmt
, ap
);
413 if (len
< size
- 4) {
414 writedata (buf
, len
);
420 err (1, "vsnprintf for `%s' failed", fmt
);
422 buf
= realloc (buf
, size
);
427 static void closepdf (void)
429 #ifdef CACHE_PAGEREFS
430 if (state
.pdflut
.objs
) {
433 for (i
= 0; i
< state
.pdflut
.count
; ++i
) {
434 pdf_drop_obj (state
.pdflut
.objs
[i
]);
436 free (state
.pdflut
.objs
);
437 state
.pdflut
.objs
= NULL
;
438 state
.pdflut
.idx
= 0;
443 pdf_close_document (state
.u
.pdf
);
448 static void closexps (void)
451 xps_close_document (state
.u
.xps
);
456 static void closecbz (void)
459 cbz_close_document (state
.u
.cbz
);
464 static void closeimg (void)
467 image_close_document (state
.u
.img
);
472 static void freepdfpage (void *ptr
)
474 pdf_free_page (state
.u
.pdf
, ptr
);
477 static void freeemptyxxxpage (void *ptr
)
482 static void freexpspage (void *ptr
)
484 xps_free_page (state
.u
.xps
, ptr
);
487 static void freecbzpage (void *ptr
)
489 cbz_free_page (state
.u
.cbz
, ptr
);
492 static void freeimgpage (void *ptr
)
494 image_free_page (state
.u
.img
, ptr
);
497 static void openxref (char *filename
, char *password
)
501 for (i
= 0; i
< state
.texcount
; ++i
) {
502 state
.texowners
[i
].w
= -1;
503 state
.texowners
[i
].slice
= NULL
;
506 if (state
.closedoc
) state
.closedoc ();
508 len
= strlen (filename
);
526 for (j
= 0; j
< sizeof (tbl
) / sizeof (*tbl
); ++j
) {
527 const char *ext
= tbl
[j
].ext
;
528 int len2
= strlen (ext
);
530 if (len2
< len
&& !strcasecmp (ext
, filename
+ len
- len2
)) {
531 state
.type
= tbl
[j
].type
;
537 if (state
.pagedims
) {
538 free (state
.pagedims
);
539 state
.pagedims
= NULL
;
541 state
.pagedimcount
= 0;
543 fz_set_aa_level (state
.ctx
, state
.aalevel
);
544 switch (state
.type
) {
546 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
547 if (pdf_needs_password (state
.u
.pdf
)) {
548 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
550 errx (1, "invalid password");
553 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
554 state
.closedoc
= closepdf
;
555 state
.freepage
= freepdfpage
;
559 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
560 state
.pagecount
= xps_count_pages (state
.u
.xps
);
561 state
.closedoc
= closexps
;
562 state
.freepage
= freexpspage
;
566 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
567 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
568 state
.closedoc
= closecbz
;
569 state
.freepage
= freecbzpage
;
573 state
.u
.img
= image_open_document (state
.ctx
, filename
);
575 state
.closedoc
= closeimg
;
576 state
.freepage
= freeimgpage
;
581 static void pdfinfo (void)
583 if (state
.type
== DPDF
) {
586 printd ("info PDF version\t%d.%d",
587 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
589 infoobj
= pdf_dict_gets (pdf_trailer (state
.u
.pdf
), "Info");
593 char *items
[] = { "Title", "Author", "Creator",
594 "Producer", "CreationDate" };
596 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
597 pdf_obj
*obj
= pdf_dict_gets (infoobj
, items
[i
]);
598 s
= pdf_to_utf8 (state
.u
.pdf
, obj
);
599 if (*s
) printd ("info %s\t%s", items
[i
], s
);
600 fz_free (state
.ctx
, s
);
607 static void unlinktile (struct tile
*tile
)
611 for (i
= 0; i
< tile
->slicecount
; ++i
) {
612 struct slice
*s
= &tile
->slices
[i
];
614 if (s
->texindex
!= -1) {
615 if (state
.texowners
[s
->texindex
].slice
== s
) {
616 state
.texowners
[s
->texindex
].slice
= NULL
;
622 static void freepage (struct page
*page
)
625 fz_free_text_page (state
.ctx
, page
->text
);
628 fz_free_text_sheet (state
.ctx
, page
->sheet
);
633 fz_drop_display_list (state
.ctx
, page
->dlist
);
634 page
->freepage (page
->u
.ptr
);
638 static void freetile (struct tile
*tile
)
643 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
646 fz_drop_pixmap (state
.ctx
, state
.pig
);
648 state
.pig
= tile
->pixmap
;
653 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
662 static int cacheline32bytes
;
664 static void __attribute__ ((constructor
)) clcheck (void)
666 char **envp
= environ
;
671 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
673 cacheline32bytes
= auxv
[1] == 32;
679 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap
*pixmap
)
681 size_t size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
682 if (cacheline32bytes
&& size
> 32) {
683 intptr_t a1
, a2
, diff
;
685 vector
unsigned char v
= vec_splat_u8 (-1);
686 vector
unsigned char *p
;
688 a1
= a2
= (intptr_t) pixmap
->samples
;
689 a2
= (a1
+ 31) & ~31;
694 while (a1
!= a2
) *(char *) a1
++ = 0xff;
695 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
696 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
698 vec_st (v
, i
+ 16, p
);
700 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
702 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
705 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
708 static void trimctm (pdf_page
*page
, int pindex
)
711 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
713 if (!pdim
->tctmready
) {
714 if (state
.trimmargins
) {
716 fz_matrix rm
, sm
, tm
, im
, ctm1
;
718 fz_rotate (&rm
, -pdim
->rotate
);
719 fz_scale (&sm
, 1, -1);
720 fz_concat (&ctm
, &rm
, &sm
);
721 realbox
= pdim
->mediabox
;
722 fz_transform_rect (&realbox
, &ctm
);
723 fz_translate (&tm
, -realbox
.x0
, -realbox
.y0
);
724 fz_concat (&ctm1
, &ctm
, &tm
);
725 fz_invert_matrix (&im
, &page
->ctm
);
726 fz_concat (&ctm
, &im
, &ctm1
);
736 static fz_matrix
pagectm (struct page
*page
)
740 if (page
->type
== DPDF
) {
741 trimctm (page
->u
.pdfpage
, page
->pdimno
);
743 &state
.pagedims
[page
->pdimno
].tctm
,
744 &state
.pagedims
[page
->pdimno
].ctm
);
747 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
749 fz_translate (&tm
, -pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
);
750 fz_concat (&ctm
, &tm
, &state
.pagedims
[page
->pdimno
].ctm
);
755 static void *loadpage (int pageno
, int pindex
)
760 page
= calloc (sizeof (struct page
), 1);
762 err (1, "calloc page %d", pageno
);
765 page
->dlist
= fz_new_display_list (state
.ctx
);
766 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
768 switch (state
.type
) {
770 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
771 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
,
773 page
->freepage
= freepdfpage
;
777 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
778 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
,
780 page
->freepage
= freexpspage
;
784 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
785 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
,
787 page
->freepage
= freecbzpage
;
791 page
->u
.imgpage
= image_load_page (state
.u
.img
, pageno
);
792 image_run_page (state
.u
.img
, page
->u
.imgpage
, dev
,
794 page
->freepage
= freeimgpage
;
798 fz_catch (state
.ctx
) {
800 page
->freepage
= freeemptyxxxpage
;
802 fz_free_device (dev
);
804 page
->pdimno
= pindex
;
805 page
->pageno
= pageno
;
806 page
->sgen
= state
.gen
;
807 page
->tgen
= state
.gen
;
808 page
->type
= state
.type
;
813 static struct tile
*alloctile (int h
)
820 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
821 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
822 tile
= calloc (tilesize
, 1);
824 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
826 for (i
= 0; i
< slicecount
; ++i
) {
827 int sh
= MIN (h
, state
.sliceheight
);
828 tile
->slices
[i
].h
= sh
;
829 tile
->slices
[i
].texindex
= -1;
832 tile
->slicecount
= slicecount
;
833 tile
->sliceheight
= state
.sliceheight
;
843 static void obs_fill_image (fz_device
*dev
, fz_image UNUSED_ATTR
*image
,
844 const fz_matrix
*ctm
, float alpha
)
846 struct obs
*obs
= dev
->user
;
848 if (!obs
->cured
&& fabs (1.0 - alpha
) < 1e6
) {
850 fz_rect rect
= fz_unit_rect
;
852 fz_transform_rect (&rect
, ctm
);
853 fz_round_rect (&b
, &rect
);
854 fz_intersect_irect (&b
, &obs
->b
);
855 obs
->cured
= b
.x0
== obs
->b
.x0
858 && b
.y1
== obs
->b
.y1
;
862 static int obscured (struct page
*page
, fz_irect bbox
)
869 memset (&dev
, 0, sizeof (dev
));
870 memset (&obs
, 0, sizeof (obs
));
875 dev
.fill_image
= obs_fill_image
;
877 fz_rect_from_irect (&rect
, &bbox
);
878 ctm
= pagectm (page
);
879 fz_run_display_list (page
->dlist
, &dev
, &ctm
, &rect
, NULL
);
882 #define OBSCURED obscured
884 #define OBSCURED(a, b) 0
887 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
895 struct pagedim
*pdim
;
897 tile
= alloctile (h
);
898 pdim
= &state
.pagedims
[page
->pdimno
];
903 bbox
.x1
= bbox
.x0
+ w
;
904 bbox
.y1
= bbox
.y0
+ h
;
907 if (state
.pig
->w
== w
909 && state
.pig
->colorspace
== state
.colorspace
) {
910 tile
->pixmap
= state
.pig
;
911 tile
->pixmap
->x
= bbox
.x0
;
912 tile
->pixmap
->y
= bbox
.y0
;
915 fz_drop_pixmap (state
.ctx
, state
.pig
);
922 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
928 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, &bbox
);
934 if (!page
->u
.ptr
|| ((w
< 128 && h
< 128) || !OBSCURED (page
, bbox
))) {
935 clearpixmap (tile
->pixmap
);
937 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
938 ctm
= pagectm (page
);
939 fz_rect_from_irect (&rect
, &bbox
);
940 fz_run_display_list (page
->dlist
, dev
, &ctm
, &rect
, NULL
);
941 fz_free_device (dev
);
946 #ifdef CACHE_PAGEREFS
947 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
948 thanks to Robin Watts */
950 pdf_collect_pages(pdf_document
*doc
, pdf_obj
*node
)
952 fz_context
*ctx
= doc
->ctx
;
956 if (state
.pdflut
.idx
== state
.pagecount
) return;
958 kids
= pdf_dict_gets (node
, "Kids");
959 len
= pdf_array_len (kids
);
962 fz_throw (ctx
, FZ_ERROR_GENERIC
, "Malformed pages tree");
964 if (pdf_mark_obj (node
))
965 fz_throw (ctx
, FZ_ERROR_GENERIC
, "cycle in page tree");
966 for (i
= 0; i
< len
; i
++) {
967 pdf_obj
*kid
= pdf_array_get (kids
, i
);
968 char *type
= pdf_to_name (pdf_dict_gets (kid
, "Type"));
970 ? !strcmp (type
, "Pages")
971 : pdf_dict_gets (kid
, "Kids")
972 && !pdf_dict_gets (kid
, "MediaBox")) {
973 pdf_collect_pages (doc
, kid
);
977 ? strcmp (type
, "Page") != 0
978 : !pdf_dict_gets (kid
, "MediaBox"))
979 fz_warn (ctx
, "non-page object in page tree (%s)", type
);
980 state
.pdflut
.objs
[state
.pdflut
.idx
++] = pdf_keep_obj (kid
);
983 pdf_unmark_obj (node
);
987 pdf_load_page_objs (pdf_document
*doc
)
989 pdf_obj
*root
= pdf_dict_gets (pdf_trailer (doc
), "Root");
990 pdf_obj
*node
= pdf_dict_gets (root
, "Pages");
993 fz_throw (doc
->ctx
, FZ_ERROR_GENERIC
, "cannot find page tree");
995 state
.pdflut
.idx
= 0;
996 pdf_collect_pages (doc
, node
);
1000 static void initpdims (void)
1004 fz_rect rootmediabox
;
1005 int pageno
, trim
, show
;
1006 int trimw
= 0, cxcount
;
1010 if (state
.trimmargins
&& state
.trimcachepath
) {
1011 trimf
= fopen (state
.trimcachepath
, "rb");
1013 trimf
= fopen (state
.trimcachepath
, "wb");
1018 if (state
.trimmargins
|| state
.type
== DPDF
|| !state
.cxack
)
1019 cxcount
= state
.pagecount
;
1021 cxcount
= MIN (state
.pagecount
, 1);
1023 if (state
.type
== DPDF
) {
1025 obj
= pdf_dict_getp (pdf_trailer (state
.u
.pdf
),
1026 "Root/Pages/MediaBox");
1027 pdf_to_rect (state
.ctx
, obj
, &rootmediabox
);
1030 #ifdef CACHE_PAGEREFS
1031 if (state
.type
== DPDF
1032 && (!state
.pdflut
.objs
|| state
.pdflut
.pdf
!= state
.u
.pdf
)) {
1033 state
.pdflut
.objs
= calloc (sizeof (*state
.pdflut
.objs
), cxcount
);
1034 if (!state
.pdflut
.objs
) {
1035 err (1, "malloc pageobjs %zu %d %zu failed",
1036 sizeof (*state
.pdflut
.objs
), cxcount
,
1037 sizeof (*state
.pdflut
.objs
) * cxcount
);
1039 state
.pdflut
.count
= cxcount
;
1040 pdf_load_page_objs (state
.u
.pdf
);
1041 state
.pdflut
.pdf
= state
.u
.pdf
;
1045 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
1050 switch (state
.type
) {
1052 pdf_document
*pdf
= state
.u
.pdf
;
1053 pdf_obj
*pageref
, *pageobj
;
1055 #ifdef CACHE_PAGEREFS
1056 pageref
= state
.pdflut
.objs
[pageno
];
1058 pageref
= pdf_lookup_page_obj (pdf
, pageno
);
1059 show
= !state
.trimmargins
&& pageno
% 20 == 0;
1061 printd ("progress %f Gathering dimensions %d",
1062 (double) (pageno
) / state
.pagecount
,
1066 pageobj
= pdf_resolve_indirect (pageref
);
1068 if (state
.trimmargins
) {
1072 fz_try (state
.ctx
) {
1073 page
= pdf_load_page (pdf
, pageno
);
1074 obj
= pdf_dict_gets (pageobj
, "llpp.TrimBox");
1075 trim
= state
.trimanew
|| !obj
;
1081 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1082 dev
->hints
|= FZ_IGNORE_SHADE
;
1083 fz_invert_matrix (&ctm
, &page
->ctm
);
1084 pdf_run_page (pdf
, page
, dev
, &fz_identity
, NULL
);
1085 fz_free_device (dev
);
1087 rect
.x0
+= state
.trimfuzz
.x0
;
1088 rect
.x1
+= state
.trimfuzz
.x1
;
1089 rect
.y0
+= state
.trimfuzz
.y0
;
1090 rect
.y1
+= state
.trimfuzz
.y1
;
1091 fz_transform_rect (&rect
, &ctm
);
1092 fz_intersect_rect (&rect
, &page
->mediabox
);
1094 if (fz_is_empty_rect (&rect
)) {
1095 mediabox
= page
->mediabox
;
1101 obj
= pdf_new_array (pdf
, 4);
1102 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x0
));
1103 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y0
));
1104 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.x1
));
1105 pdf_array_push (obj
, pdf_new_real (pdf
, mediabox
.y1
));
1106 pdf_dict_puts (pageobj
, "llpp.TrimBox", obj
);
1109 mediabox
.x0
= pdf_to_real (pdf_array_get (obj
, 0));
1110 mediabox
.y0
= pdf_to_real (pdf_array_get (obj
, 1));
1111 mediabox
.x1
= pdf_to_real (pdf_array_get (obj
, 2));
1112 mediabox
.y1
= pdf_to_real (pdf_array_get (obj
, 3));
1115 rotate
= page
->rotate
;
1116 pdf_free_page (pdf
, page
);
1118 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
1120 printd ("progress %f Trimming %d",
1121 (double) (pageno
+ 1) / state
.pagecount
,
1125 fz_catch (state
.ctx
) {
1126 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1133 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "MediaBox"),
1135 if (fz_is_empty_rect (&mediabox
)) {
1143 pdf_to_rect (state
.ctx
, pdf_dict_gets (pageobj
, "CropBox"),
1145 if (!fz_is_empty_rect (&cropbox
)) {
1150 fz_intersect_rect (&mediabox
, &cropbox
);
1155 if (fz_is_empty_rect (&rootmediabox
)) {
1157 "cannot find page size for page %d\n",
1161 mediabox
= rootmediabox
;
1165 rotate
= pdf_to_int (pdf_dict_gets (pageobj
, "Rotate"));
1174 fz_try (state
.ctx
) {
1175 page
= xps_load_page (state
.u
.xps
, pageno
);
1176 xps_bound_page (state
.u
.xps
, page
, &mediabox
);
1178 if (state
.trimmargins
) {
1182 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
1183 dev
->hints
|= FZ_IGNORE_SHADE
;
1184 xps_run_page (state
.u
.xps
, page
, dev
,
1185 &fz_identity
, NULL
);
1186 fz_free_device (dev
);
1188 rect
.x0
+= state
.trimfuzz
.x0
;
1189 rect
.x1
+= state
.trimfuzz
.x1
;
1190 rect
.y0
+= state
.trimfuzz
.y0
;
1191 rect
.y1
+= state
.trimfuzz
.y1
;
1192 fz_intersect_rect (&rect
, &mediabox
);
1194 if (!fz_is_empty_rect (&rect
)) {
1198 xps_free_page (state
.u
.xps
, page
);
1200 printd ("progress %f loading %d",
1201 (double) (pageno
+ 1) / state
.pagecount
,
1205 fz_catch (state
.ctx
) {
1217 if (state
.trimmargins
&& trimw
) {
1220 fz_try (state
.ctx
) {
1221 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1222 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1223 cbz_free_page (state
.u
.cbz
, page
);
1224 printd ("progress %f Trimming %d",
1225 (double) (pageno
+ 1) / state
.pagecount
,
1228 fz_catch (state
.ctx
) {
1229 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1232 int n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
1234 err (1, "fwrite trim mediabox");
1240 int n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
1242 err (1, "fread trim mediabox");
1248 fz_try (state
.ctx
) {
1249 page
= cbz_load_page (state
.u
.cbz
, pageno
);
1250 cbz_bound_page (state
.u
.cbz
, page
, &mediabox
);
1251 cbz_free_page (state
.u
.cbz
, page
);
1253 fz_catch (state
.ctx
) {
1254 fprintf (stderr
, "failed to load cbz page\n");
1272 fz_try (state
.ctx
) {
1273 page
= image_load_page (state
.u
.img
, pageno
);
1274 image_bound_page (state
.u
.img
, page
, &mediabox
);
1275 image_free_page (state
.u
.img
, page
);
1277 fz_catch (state
.ctx
) {
1278 fprintf (stderr
, "failed to load page %d\n", pageno
+1);
1284 ARSERT (0 && state
.type
);
1287 if (state
.pagedimcount
== 0
1288 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
1289 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
1292 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
1293 state
.pagedims
= realloc (state
.pagedims
, size
);
1294 if (!state
.pagedims
) {
1295 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
1296 size
, state
.pagedimcount
+ 1);
1299 p
= &state
.pagedims
[state
.pagedimcount
++];
1301 p
->mediabox
= mediabox
;
1306 if (state
.trimmargins
) {
1307 printd ("progress 1 Trimmed %d pages in %f seconds",
1308 state
.pagecount
, end
- start
);
1311 if (state
.type
== DPDF
)
1312 printd ("progress 1 Processed %d pages in %f seconds",
1313 state
.pagecount
, end
- start
);
1315 printd ("vmsg Processed %d pages in %f seconds",
1316 state
.pagecount
, end
- start
);
1320 if (fclose (trimf
)) {
1326 static void layout (void)
1331 struct pagedim
*p
= p
;
1332 double zw
, w
, maxw
= 0.0, zoom
= zoom
;
1334 if (state
.pagedimcount
== 0) return;
1336 switch (state
.fitmodel
) {
1337 case FitProportional
:
1338 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1341 p
= &state
.pagedims
[pindex
];
1342 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1344 fz_transform_rect (&box
, &rm
);
1346 x0
= MIN (box
.x0
, box
.x1
);
1347 x1
= MAX (box
.x0
, box
.x1
);
1350 maxw
= MAX (w
, maxw
);
1351 zoom
= state
.w
/ maxw
;
1363 ARSERT (0 && state
.fitmodel
);
1366 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
1370 p
= &state
.pagedims
[pindex
];
1371 fz_rotate (&ctm
, state
.rotate
);
1372 fz_rotate (&rm
, p
->rotate
+ state
.rotate
);
1374 fz_transform_rect (&box
, &rm
);
1375 w
= box
.x1
- box
.x0
;
1376 switch (state
.fitmodel
) {
1377 case FitProportional
:
1378 p
->left
= ((maxw
- w
) * zoom
) / 2.0;
1384 h
= box
.y1
- box
.y0
;
1386 zoom
= MIN (zw
, zh
);
1387 p
->left
= (maxw
- (w
* zoom
)) / 2.0;
1396 fz_scale (&p
->zoomctm
, zoom
, zoom
);
1397 fz_concat (&ctm
, &p
->zoomctm
, &ctm
);
1399 fz_rotate (&rm
, p
->rotate
);
1400 p
->pagebox
= p
->mediabox
;
1401 fz_transform_rect (&p
->pagebox
, &rm
);
1402 p
->pagebox
.x1
-= p
->pagebox
.x0
;
1403 p
->pagebox
.y1
-= p
->pagebox
.y0
;
1407 fz_transform_rect (&rect
, &ctm
);
1408 fz_round_rect (&p
->bounds
, &rect
);
1411 fz_translate (&tm
, 0, -p
->mediabox
.y1
);
1412 fz_scale (&sm
, zoom
, -zoom
);
1413 fz_concat (&ctm
, &tm
, &sm
);
1414 fz_concat (&p
->lctm
, &ctm
, &rm
);
1420 int x0
= MIN (p
->bounds
.x0
, p
->bounds
.x1
);
1421 int y0
= MIN (p
->bounds
.y0
, p
->bounds
.y1
);
1422 int x1
= MAX (p
->bounds
.x0
, p
->bounds
.x1
);
1423 int y1
= MAX (p
->bounds
.y0
, p
->bounds
.y1
);
1424 int boundw
= x1
- x0
;
1425 int boundh
= y1
- y0
;
1427 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
1428 } while (p
-- != state
.pagedims
);
1432 struct anchor
{ int n
; int x
; int y
; int w
; int h
; }
1433 desttoanchor (fz_link_dest
*dest
)
1437 struct pagedim
*pdim
= state
.pagedims
;
1442 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1443 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
1445 pdim
= &state
.pagedims
[i
];
1447 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1449 if (dest
->ld
.gotor
.flags
& fz_link_flag_l_valid
)
1450 p
.x
= dest
->ld
.gotor
.lt
.x
;
1453 p
.y
= dest
->ld
.gotor
.lt
.y
;
1454 fz_transform_point (&p
, &pdim
->lctm
);
1458 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1459 double x0
, x1
, y0
, y1
;
1461 x0
= MIN (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1462 x1
= MAX (pdim
->bounds
.x0
, pdim
->bounds
.x1
);
1464 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1465 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1467 a
.n
= dest
->ld
.gotor
.page
;
1472 static void recurse_outline (fz_outline
*outline
, int level
)
1475 switch (outline
->dest
.kind
) {
1478 struct anchor a
= desttoanchor (&outline
->dest
);
1481 printd ("o %d %d %d %d %s",
1482 level
, a
.n
, a
.y
, a
.h
, outline
->title
);
1488 printd ("ou %d %" FMT_s
" %s %s", level
,
1489 strlen (outline
->title
), outline
->title
,
1490 outline
->dest
.ld
.uri
.uri
);
1494 printd ("on %d %s", level
, outline
->title
);
1498 printd ("emsg Unhandled outline kind %d for %s\n",
1499 outline
->dest
.kind
, outline
->title
);
1502 if (outline
->down
) {
1503 recurse_outline (outline
->down
, level
+ 1);
1505 outline
= outline
->next
;
1509 static void process_outline (void)
1511 fz_outline
*outline
;
1513 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1515 state
.needoutline
= 0;
1516 switch (state
.type
) {
1518 outline
= pdf_load_outline (state
.u
.pdf
);
1521 outline
= xps_load_outline (state
.u
.xps
);
1528 recurse_outline (outline
, 0);
1529 fz_free_outline (state
.ctx
, outline
);
1533 static char *strofspan (fz_text_span
*span
)
1538 size_t size
= 0, cap
= 80;
1540 p
= malloc (cap
+ 1);
1541 if (!p
) return NULL
;
1543 for (ch
= span
->text
; ch
< span
->text
+ span
->len
; ++ch
) {
1544 int n
= fz_runetochar (utf8
, ch
->c
);
1545 if (size
+ n
> cap
) {
1547 p
= realloc (p
, cap
+ 1);
1548 if (!p
) return NULL
;
1551 memcpy (p
+ size
, utf8
, n
);
1558 static int matchspan (regex_t
*re
, fz_text_page UNUSED_ATTR
*page
,
1559 fz_text_span
*span
, fz_matrix UNUSED_ATTR ctm
,
1560 int stop
, int pageno
, double start
)
1567 fz_point p1
, p2
, p3
, p4
;
1569 p
= strofspan (span
);
1572 ret
= regexec (re
, p
, 1, &rm
, 0);
1575 if (ret
!= REG_NOMATCH
) {
1578 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1579 printd ("msg regexec error `%.*s'",
1580 (int) size
, errbuf
);
1588 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1589 c
+= fz_runelen (span
->text
[a
].c
);
1591 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1592 c
+= fz_runelen (span
->text
[b
].c
);
1595 if (fz_runelen (span
->text
[b
].c
) > 1) {
1599 fz_text_char_bbox (&sb
, span
, a
);
1600 fz_text_char_bbox (&eb
, span
, b
);
1612 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1619 printd ("progress 1 found at %d `%.*s' in %f sec",
1620 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1624 printd ("match %d %d %f %f %f %f %f %f %f %f",
1636 static int compareblocks (const void *l
, const void *r
)
1638 fz_text_block
const *ls
= l
;
1639 fz_text_block
const* rs
= r
;
1640 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1643 /* wishful thinking function */
1644 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1649 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1651 fz_text_sheet
*sheet
;
1652 struct pagedim
*pdim
, *pdimprev
;
1653 int stop
= 0, niters
= 0;
1656 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1660 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1661 if (niters
++ == 5) {
1664 printd ("progress 1 attention requested aborting search at %d",
1669 printd ("progress %f searching in page %d",
1670 (double) (pageno
+ 1) / state
.pagecount
,
1675 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1676 pdim
= &state
.pagedims
[i
];
1677 if (pdim
->pageno
== pageno
) {
1680 if (pdim
->pageno
> pageno
) {
1689 sheet
= fz_new_text_sheet (state
.ctx
);
1690 text
= fz_new_text_page (state
.ctx
);
1691 tdev
= fz_new_text_device (state
.ctx
, sheet
, text
);
1693 switch (state
.type
) {
1696 fz_try (state
.ctx
) {
1697 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1698 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1699 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->zoomctm
);
1700 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
1701 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, &ctm
, NULL
);
1704 fz_catch (state
.ctx
) {
1705 fz_free_device (tdev
);
1712 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1713 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, &pdim
->ctm
, NULL
);
1717 ARSERT (0 && state
.type
);
1720 qsort (text
->blocks
, text
->len
, sizeof (*text
->blocks
), compareblocks
);
1721 fz_free_device (tdev
);
1723 for (j
= 0; j
< text
->len
; ++j
) {
1726 fz_text_block
*block
;
1728 pb
= &text
->blocks
[forward
? j
: text
->len
- 1 - j
];
1729 if (pb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
1732 for (k
= 0; k
< block
->len
; ++k
) {
1737 line
= &block
->lines
[k
];
1738 if (line
->bbox
.y0
< y
+ 1) continue;
1741 line
= &block
->lines
[block
->len
- 1 - k
];
1742 if (line
->bbox
.y0
> y
- 1) continue;
1745 for (span
= line
->first_span
; span
; span
= span
->next
) {
1746 switch (matchspan (re
, text
, span
, ctm
,
1747 stop
, pageno
, start
)) {
1749 case 1: stop
= 1; break;
1750 case -1: stop
= 1; goto endloop
;
1765 fz_free_text_page (state
.ctx
, text
);
1766 fz_free_text_sheet (state
.ctx
, sheet
);
1768 state
.freepage (u
.ptr
);
1773 printd ("progress 1 no matches %f sec", end
- start
);
1775 printd ("clearrects");
1778 static void set_tex_params (int colorspace
)
1785 switch (colorspace
) {
1787 state
.texiform
= GL_RGBA8
;
1788 state
.texform
= GL_RGBA
;
1789 state
.texty
= GL_UNSIGNED_BYTE
;
1790 state
.colorspace
= fz_device_rgb (state
.ctx
);
1793 state
.texiform
= GL_RGBA8
;
1794 state
.texform
= GL_BGRA
;
1795 state
.texty
= endianness
.s
> 1
1796 ? GL_UNSIGNED_INT_8_8_8_8
1797 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1798 state
.colorspace
= fz_device_bgr (state
.ctx
);
1801 state
.texiform
= GL_LUMINANCE_ALPHA
;
1802 state
.texform
= GL_LUMINANCE_ALPHA
;
1803 state
.texty
= GL_UNSIGNED_BYTE
;
1804 state
.colorspace
= fz_device_gray (state
.ctx
);
1807 errx (1, "invalid colorspce %d", colorspace
);
1811 static void realloctexts (int texcount
)
1815 if (texcount
== state
.texcount
) return;
1817 if (texcount
< state
.texcount
) {
1818 glDeleteTextures (state
.texcount
- texcount
,
1819 state
.texids
+ texcount
);
1822 size
= texcount
* sizeof (*state
.texids
);
1823 state
.texids
= realloc (state
.texids
, size
);
1824 if (!state
.texids
) {
1825 err (1, "realloc texids %" FMT_s
, size
);
1828 size
= texcount
* sizeof (*state
.texowners
);
1829 state
.texowners
= realloc (state
.texowners
, size
);
1830 if (!state
.texowners
) {
1831 err (1, "realloc texowners %" FMT_s
, size
);
1833 if (texcount
> state
.texcount
) {
1836 glGenTextures (texcount
- state
.texcount
,
1837 state
.texids
+ state
.texcount
);
1838 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1839 state
.texowners
[i
].w
= -1;
1840 state
.texowners
[i
].slice
= NULL
;
1843 state
.texcount
= texcount
;
1847 static char *mbtoutf8 (char *s
)
1853 len
= mbstowcs (NULL
, s
, strlen (s
));
1858 if (len
== (size_t) -1) {
1863 tmp
= malloc (len
* sizeof (wchar_t));
1868 ret
= mbstowcs (tmp
, s
, len
);
1869 if (ret
== (size_t) -1) {
1875 for (i
= 0; i
< ret
; ++i
) {
1876 len
+= fz_runelen (tmp
[i
]);
1879 p
= r
= malloc (len
+ 1);
1885 for (i
= 0; i
< ret
; ++i
) {
1886 p
+= fz_runetochar (p
, tmp
[i
]);
1893 CAMLprim value
ml_mbtoutf8 (value s_v
)
1899 s
= String_val (s_v
);
1905 ret_v
= caml_copy_string (r
);
1911 static void * mainloop (void UNUSED_ATTR
*unused
)
1914 int len
, ret
, oldlen
= 0;
1919 errx (1, "readlen returned 0");
1922 if (oldlen
< len
+ 1) {
1923 p
= realloc (p
, len
+ 1);
1925 err (1, "realloc %d failed", len
+ 1);
1932 if (!strncmp ("open", p
, 4)) {
1933 int wthack
, off
, ok
= 0;
1939 ret
= sscanf (p
+ 5, " %d %d %n", &wthack
, &state
.cxack
, &off
);
1941 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1944 filename
= p
+ 5 + off
;
1945 filenamelen
= strlen (filename
);
1946 password
= filename
+ filenamelen
+ 1;
1949 fz_try (state
.ctx
) {
1950 openxref (filename
, password
);
1953 fz_catch (state
.ctx
) {
1954 utf8filename
= mbtoutf8 (filename
);
1955 printd ("msg Could not open %s", utf8filename
);
1965 utf8filename
= mbtoutf8 (filename
);
1966 printd ("msg Opened %s (press h/F1 to get help)",
1968 if (utf8filename
!= filename
) {
1969 free (utf8filename
);
1972 state
.needoutline
= 1;
1975 else if (!strncmp ("cs", p
, 2)) {
1978 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1980 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1983 set_tex_params (colorspace
);
1984 for (i
= 0; i
< state
.texcount
; ++i
) {
1985 state
.texowners
[i
].w
= -1;
1986 state
.texowners
[i
].slice
= NULL
;
1990 else if (!strncmp ("freepage", p
, 8)) {
1993 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
1995 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1999 else if (!strncmp ("freetile", p
, 8)) {
2002 ret
= sscanf (p
+ 8, " %" SCN_ptr
, SCN_ptr_cast (&ptr
));
2004 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
2008 else if (!strncmp ("search", p
, 6)) {
2009 int icase
, pageno
, y
, len2
, forward
;
2013 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
2014 &icase
, &pageno
, &y
, &forward
, &len2
);
2016 errx (1, "malformed search `%s' ret=%d", p
, ret
);
2019 pattern
= p
+ 6 + len2
;
2020 ret
= regcomp (&re
, pattern
,
2021 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
2026 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
2027 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
2030 search (&re
, pageno
, y
, forward
);
2034 else if (!strncmp ("geometry", p
, 8)) {
2038 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
2040 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
2048 for (i
= 0; i
< state
.texcount
; ++i
) {
2049 state
.texowners
[i
].slice
= NULL
;
2052 state
.fitmodel
= fitmodel
;
2057 unlock ("geometry");
2058 printd ("continue %d", state
.pagecount
);
2060 else if (!strncmp ("reqlayout", p
, 9)) {
2063 unsigned int fitmodel
;
2066 ret
= sscanf (p
+ 9, " %d %u %d %n",
2067 &rotate
, &fitmodel
, &h
, &off
);
2069 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
2072 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
2075 state
.rotate
= rotate
;
2076 state
.fitmodel
= fitmodel
;
2081 nameddest
= p
+ 9 + off
;
2082 if (state
.type
== DPDF
&& nameddest
&& *nameddest
) {
2085 pdf_obj
*needle
, *obj
;
2087 needle
= pdf_new_string (state
.u
.pdf
, nameddest
,
2088 strlen (nameddest
));
2089 obj
= pdf_lookup_dest (state
.u
.pdf
, needle
);
2091 dest
= pdf_parse_link_dest (state
.u
.pdf
, FZ_LINK_GOTO
, obj
);
2093 a
= desttoanchor (&dest
);
2095 printd ("a %d %d %d", a
.n
, a
.x
, a
.y
);
2098 printd ("emsg failed to parse destination `%s'\n",
2103 printd ("emsg destination `%s' not found\n",
2106 pdf_drop_obj (needle
);
2110 unlock ("reqlayout");
2111 printd ("continue %d", state
.pagecount
);
2113 else if (!strncmp ("page", p
, 4)) {
2118 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
2120 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
2125 page
= loadpage (pageno
, pindex
);
2129 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast (page
), b
- a
);
2131 else if (!strncmp ("tile", p
, 4)) {
2138 ret
= sscanf (p
+ 4, " %" SCN_ptr
" %d %d %d %d %" SCN_ptr
,
2139 SCN_ptr_cast (&page
), &x
, &y
, &w
, &h
,
2140 SCN_ptr_cast (&data
));
2142 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
2147 tile
= rendertile (page
, x
, y
, w
, h
, data
);
2151 printd ("tile %d %d %" FMT_ptr
" %u %f",
2153 FMT_ptr_cast (tile
),
2154 tile
->w
* tile
->h
* tile
->pixmap
->n
,
2157 else if (!strncmp ("trimset", p
, 7)) {
2161 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2162 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2164 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
2167 state
.trimmargins
= trimmargins
;
2168 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2170 state
.trimfuzz
= fuzz
;
2174 else if (!strncmp ("settrim", p
, 7)) {
2178 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
2179 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
2181 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
2185 state
.trimmargins
= trimmargins
;
2186 state
.needoutline
= 1;
2187 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
2189 state
.trimfuzz
= fuzz
;
2191 state
.pagedimcount
= 0;
2192 free (state
.pagedims
);
2193 state
.pagedims
= NULL
;
2198 printd ("continue %d", state
.pagecount
);
2200 else if (!strncmp ("sliceh", p
, 6)) {
2203 ret
= sscanf (p
+ 6, " %d", &h
);
2205 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
2207 if (h
!= state
.sliceheight
) {
2210 state
.sliceheight
= h
;
2211 for (i
= 0; i
< state
.texcount
; ++i
) {
2212 state
.texowners
[i
].w
= -1;
2213 state
.texowners
[i
].h
= -1;
2214 state
.texowners
[i
].slice
= NULL
;
2218 else if (!strncmp ("interrupt", p
, 9)) {
2219 printd ("vmsg interrupted");
2222 errx (1, "unknown command %.*s", len
, p
);
2228 CAMLprim value
ml_realloctexts (value texcount_v
)
2230 CAMLparam1 (texcount_v
);
2233 if (trylock ("ml_realloctexts")) {
2237 realloctexts (Int_val (texcount_v
));
2239 unlock ("ml_realloctexts");
2242 CAMLreturn (Val_bool (ok
));
2245 static void recti (int x0
, int y0
, int x1
, int y1
)
2247 GLfloat
*v
= state
.vertices
;
2249 glVertexPointer (2, GL_FLOAT
, 0, v
);
2250 v
[0] = x0
; v
[1] = y0
;
2251 v
[2] = x1
; v
[3] = y0
;
2252 v
[4] = x0
; v
[5] = y1
;
2253 v
[6] = x1
; v
[7] = y1
;
2254 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2257 static void showsel (struct page
*page
, int ox
, int oy
)
2263 fz_page_block
*pageb
;
2264 fz_text_block
*block
;
2265 struct mark first
, last
;
2266 unsigned char selcolor
[] = {15,15,15,140};
2268 first
= page
->fmark
;
2271 if (!first
.span
|| !last
.span
) return;
2273 glEnable (GL_BLEND
);
2274 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
2275 glColor4ubv (selcolor
);
2277 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2278 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2279 for (pageb
= page
->text
->blocks
;
2280 pageb
< page
->text
->blocks
+ page
->text
->len
;
2282 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
2283 block
= pageb
->u
.text
;
2285 for (line
= block
->lines
;
2286 line
< block
->lines
+ block
->len
;
2289 rect
= fz_empty_rect
;
2291 for (span
= line
->first_span
; span
; span
= span
->next
) {
2293 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
2298 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
2300 j
= MIN (first
.i
, last
.i
);
2301 k
= MAX (first
.i
, last
.i
);
2304 if (span
== first
.span
) {
2308 else if (span
== last
.span
) {
2315 for (i
= j
; i
<= k
; ++i
) {
2317 fz_union_rect (&rect
,
2318 fz_text_char_bbox (&bbox1
, span
, i
));
2320 fz_round_rect (&bbox
, &rect
);
2321 lprintf ("%d %d %d %d oy=%d ox=%d\n",
2328 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
2329 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
2330 if (span
== last
.span
) {
2333 rect
= fz_empty_rect
;
2339 glDisable (GL_BLEND
);
2344 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
2346 fz_matrix ctm
, tm
, pm
;
2347 fz_link
*link
, *links
;
2348 GLfloat
*texcoords
= state
.texcoords
;
2349 GLfloat
*vertices
= state
.vertices
;
2351 switch (page
->type
) {
2353 links
= page
->u
.pdfpage
->links
;
2357 links
= page
->u
.xpspage
->links
;
2364 glEnable (GL_TEXTURE_1D
);
2365 glEnable (GL_BLEND
);
2366 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2367 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
2369 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2370 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2371 fz_translate (&tm
, xoff
, yoff
);
2372 pm
= pagectm (page
);
2373 fz_concat (&ctm
, &pm
, &tm
);
2375 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
2376 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2378 for (link
= links
; link
; link
= link
->next
) {
2379 fz_point p1
, p2
, p3
, p4
;
2381 p1
.x
= link
->rect
.x0
;
2382 p1
.y
= link
->rect
.y0
;
2384 p2
.x
= link
->rect
.x1
;
2385 p2
.y
= link
->rect
.y0
;
2387 p3
.x
= link
->rect
.x1
;
2388 p3
.y
= link
->rect
.y1
;
2390 p4
.x
= link
->rect
.x0
;
2391 p4
.y
= link
->rect
.y1
;
2393 fz_transform_point (&p1
, &ctm
);
2394 fz_transform_point (&p2
, &ctm
);
2395 fz_transform_point (&p3
, &ctm
);
2396 fz_transform_point (&p4
, &ctm
);
2398 switch (link
->dest
.kind
) {
2399 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
2400 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
2401 case FZ_LINK_LAUNCH
: glColor3ub (0, 255, 0); break;
2402 default: glColor3ub (0, 0, 0); break;
2410 t
= sqrtf (w
*w
+ h
*h
) * .25f
;
2414 s
= sqrtf (w
*w
+ h
*h
) * .25f
;
2416 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
2417 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
2419 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
2420 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
2422 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
2423 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
2425 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
2426 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
2428 glDrawArrays (GL_LINES
, 0, 8);
2431 glDisable (GL_BLEND
);
2432 glDisable (GL_TEXTURE_1D
);
2435 static int compareslinks (const void *l
, const void *r
)
2437 struct slink
const *ls
= l
;
2438 struct slink
const *rs
= r
;
2439 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
2440 return rs
->bbox
.x0
- rs
->bbox
.x0
;
2442 return ls
->bbox
.y0
- rs
->bbox
.y0
;
2445 static void droptext (struct page
*page
)
2448 fz_free_text_page (state
.ctx
, page
->text
);
2451 page
->fmark
.span
= NULL
;
2452 page
->lmark
.span
= NULL
;
2456 fz_free_text_sheet (state
.ctx
, page
->sheet
);
2461 static void dropslinks (struct page
*page
)
2464 free (page
->slinks
);
2465 page
->slinks
= NULL
;
2466 page
->slinkcount
= 0;
2470 static void ensureslinks (struct page
*page
)
2474 size_t slinksize
= sizeof (*page
->slinks
);
2475 fz_link
*link
, *links
;
2477 if (state
.gen
!= page
->sgen
) {
2479 page
->sgen
= state
.gen
;
2481 if (page
->slinks
) return;
2483 switch (page
->type
) {
2485 links
= page
->u
.pdfpage
->links
;
2486 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2488 &state
.pagedims
[page
->pdimno
].tctm
,
2489 &state
.pagedims
[page
->pdimno
].ctm
);
2493 links
= page
->u
.xpspage
->links
;
2494 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
2501 for (link
= links
; link
; link
= link
->next
) {
2505 page
->slinkcount
= count
;
2506 page
->slinks
= calloc (count
, slinksize
);
2507 if (!page
->slinks
) {
2508 err (1, "realloc slinks %d", count
);
2511 for (i
= 0, link
= links
; link
; ++i
, link
= link
->next
) {
2515 fz_transform_rect (&rect
, &ctm
);
2516 page
->slinks
[i
].link
= link
;
2517 fz_round_rect (&page
->slinks
[i
].bbox
, &rect
);
2519 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2523 /* slightly tweaked fmt_ulong by D.J. Bernstein */
2524 static void fmt_linkn (char *s
, unsigned int u
)
2526 unsigned int len
; unsigned int q
;
2527 unsigned int zma
= 'z' - 'a' + 1;
2529 while (q
> zma
- 1) { ++len
; q
/= zma
; }
2532 do { *--s
= 'a' + (u
% zma
) - (u
< zma
&& len
> 1); u
/= zma
; } while(u
);
2533 /* handles u == 0 */
2538 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2539 int noff
, char *targ
, int tlen
, int hfsize
)
2543 struct slink
*slink
;
2544 double x0
, y0
, x1
, y1
, w
;
2546 ensureslinks (page
);
2547 glColor3ub (0xc3, 0xb0, 0x91);
2548 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2549 fmt_linkn (buf
, i
+ noff
);
2550 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2551 slink
= &page
->slinks
[i
];
2553 x0
= slink
->bbox
.x0
+ xoff
- 5;
2554 y1
= slink
->bbox
.y0
+ yoff
- 5;
2555 y0
= y1
+ 10 + hfsize
;
2556 w
= measure_string (state
.face
, hfsize
, buf
);
2558 recti (x0
, y0
, x1
, y1
);
2562 glEnable (GL_BLEND
);
2563 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2564 glEnable (GL_TEXTURE_2D
);
2565 glColor3ub (0, 0, 0);
2566 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2567 fmt_linkn (buf
, i
+ noff
);
2568 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2569 slink
= &page
->slinks
[i
];
2571 x0
= slink
->bbox
.x0
+ xoff
;
2572 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2573 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2576 glDisable (GL_TEXTURE_2D
);
2577 glDisable (GL_BLEND
);
2580 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2583 struct slice
*slice1
;
2584 unsigned char *texdata
;
2587 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2588 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2590 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2591 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2592 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
2596 int texindex
= state
.texindex
++ % state
.texcount
;
2598 if (state
.texowners
[texindex
].w
== tile
->w
) {
2599 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2603 state
.texowners
[texindex
].h
= slice
->h
;
2607 state
.texowners
[texindex
].h
= slice
->h
;
2610 state
.texowners
[texindex
].w
= tile
->w
;
2611 state
.texowners
[texindex
].slice
= slice
;
2612 slice
->texindex
= texindex
;
2614 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
2616 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2620 texdata
= tile
->pixmap
->samples
;
2623 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2635 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
2647 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2652 CAMLprim value
ml_begintiles (value unit_v
)
2654 CAMLparam1 (unit_v
);
2655 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
2656 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2657 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2658 CAMLreturn (unit_v
);
2661 CAMLprim value
ml_endtiles (value unit_v
)
2663 CAMLparam1 (unit_v
);
2664 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
2665 CAMLreturn (unit_v
);
2668 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
2670 CAMLparam2 (args_v
, ptr_v
);
2671 int dispx
= Int_val (Field (args_v
, 0));
2672 int dispy
= Int_val (Field (args_v
, 1));
2673 int dispw
= Int_val (Field (args_v
, 2));
2674 int disph
= Int_val (Field (args_v
, 3));
2675 int tilex
= Int_val (Field (args_v
, 4));
2676 int tiley
= Int_val (Field (args_v
, 5));
2677 char *s
= String_val (ptr_v
);
2678 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
2679 int slicey
, firstslice
;
2680 struct slice
*slice
;
2681 GLfloat
*texcoords
= state
.texcoords
;
2682 GLfloat
*vertices
= state
.vertices
;
2684 firstslice
= tiley
/ tile
->sliceheight
;
2685 slice
= &tile
->slices
[firstslice
];
2686 slicey
= tiley
% tile
->sliceheight
;
2691 dh
= slice
->h
- slicey
;
2692 dh
= MIN (disph
, dh
);
2693 uploadslice (tile
, slice
);
2695 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2696 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2697 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2698 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2700 vertices
[0] = dispx
; vertices
[1] = dispy
;
2701 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2702 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2703 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2705 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2709 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2712 CAMLreturn (Val_unit
);
2715 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
2716 value xoff_v
, value yoff_v
,
2719 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2720 int xoff
= Int_val (xoff_v
);
2721 int yoff
= Int_val (yoff_v
);
2722 int noff
= Int_val (Field (li_v
, 0));
2723 char *targ
= String_val (Field (li_v
, 1));
2724 int tlen
= caml_string_length (Field (li_v
, 1));
2725 int hfsize
= Int_val (Field (li_v
, 2));
2726 char *s
= String_val (ptr_v
);
2727 int hlmask
= Int_val (hlinks_v
);
2728 struct page
*page
= parse_pointer ("ml_postprocess", s
);
2731 /* deal with loadpage failed pages */
2735 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2736 if (trylock ("ml_postprocess")) {
2741 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2742 noff
= page
->slinkcount
;
2744 if (page
->tgen
== state
.gen
) {
2745 showsel (page
, xoff
, yoff
);
2747 unlock ("ml_postprocess");
2750 CAMLreturn (Val_int (noff
));
2753 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2757 const fz_matrix
*tctm
;
2758 fz_link
*link
, *links
;
2760 switch (page
->type
) {
2762 trimctm (page
->u
.pdfpage
, page
->pdimno
);
2763 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2764 links
= page
->u
.pdfpage
->links
;
2768 tctm
= &fz_identity
;
2769 links
= page
->u
.xpspage
->links
;
2778 fz_concat (&ctm
, tctm
, &state
.pagedims
[page
->pdimno
].ctm
);
2779 fz_invert_matrix (&ctm
, &ctm
);
2780 fz_transform_point (&p
, &ctm
);
2782 for (link
= links
; link
; link
= link
->next
) {
2783 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2784 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2792 static void ensuretext (struct page
*page
)
2794 if (state
.gen
!= page
->tgen
) {
2796 page
->tgen
= state
.gen
;
2802 page
->text
= fz_new_text_page (state
.ctx
);
2803 page
->sheet
= fz_new_text_sheet (state
.ctx
);
2804 tdev
= fz_new_text_device (state
.ctx
, page
->sheet
, page
->text
);
2805 ctm
= pagectm (page
);
2806 fz_begin_page (tdev
, &fz_infinite_rect
, &ctm
);
2807 fz_run_display_list (page
->dlist
, tdev
, &ctm
, &fz_infinite_rect
, NULL
);
2808 qsort (page
->text
->blocks
, page
->text
->len
,
2809 sizeof (*page
->text
->blocks
), compareblocks
);
2811 fz_free_device (tdev
);
2815 CAMLprim value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2817 CAMLparam2 (start_page_v
, dir_v
);
2819 int i
, dir
= Int_val (dir_v
);
2820 int start_page
= Int_val (start_page_v
);
2821 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2823 ret_v
= Val_int (0);
2824 if (!(state
.type
== DPDF
|| state
.type
== DXPS
)) {
2828 lock ("ml_findpage_with_links");
2829 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2832 switch (state
.type
) {
2835 pdf_page
*page
= NULL
;
2837 fz_try (state
.ctx
) {
2838 page
= pdf_load_page (state
.u
.pdf
, i
);
2839 found
= !!page
->links
;
2841 fz_catch (state
.ctx
) {
2851 xps_page
*page
= xps_load_page (state
.u
.xps
, i
);
2852 found
= !!page
->links
;
2858 ARSERT (0 && "invalid document type");
2862 ret_v
= caml_alloc_small (1, 1);
2863 Field (ret_v
, 0) = Val_int (i
);
2868 unlock ("ml_findpage_with_links");
2874 enum { dir_first
, dir_last
};
2875 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2877 CAMLprim value
ml_findlink (value ptr_v
, value dir_v
)
2879 CAMLparam2 (ptr_v
, dir_v
);
2880 CAMLlocal2 (ret_v
, pos_v
);
2882 int dirtag
, i
, slinkindex
;
2883 struct slink
*found
= NULL
,*slink
;
2884 char *s
= String_val (ptr_v
);
2886 page
= parse_pointer ("ml_findlink", s
);
2887 ret_v
= Val_int (0);
2888 if (trylock ("ml_findlink")) {
2892 ensureslinks (page
);
2894 if (Is_block (dir_v
)) {
2895 dirtag
= Tag_val (dir_v
);
2897 case dir_first_visible
:
2899 int x0
, y0
, dir
, first_index
, last_index
;
2901 pos_v
= Field (dir_v
, 0);
2902 x0
= Int_val (Field (pos_v
, 0));
2903 y0
= Int_val (Field (pos_v
, 1));
2904 dir
= Int_val (Field (pos_v
, 2));
2909 last_index
= page
->slinkcount
;
2912 first_index
= page
->slinkcount
- 1;
2916 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2917 slink
= &page
->slinks
[i
];
2918 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2927 slinkindex
= Int_val (Field (dir_v
, 0));
2928 found
= &page
->slinks
[slinkindex
];
2929 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2930 slink
= &page
->slinks
[i
];
2931 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2939 slinkindex
= Int_val (Field (dir_v
, 0));
2940 found
= &page
->slinks
[slinkindex
];
2941 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2942 slink
= &page
->slinks
[i
];
2943 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2951 slinkindex
= Int_val (Field (dir_v
, 0));
2952 found
= &page
->slinks
[slinkindex
];
2953 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2954 slink
= &page
->slinks
[i
];
2955 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2963 slinkindex
= Int_val (Field (dir_v
, 0));
2964 found
= &page
->slinks
[slinkindex
];
2965 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2966 slink
= &page
->slinks
[i
];
2967 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2976 dirtag
= Int_val (dir_v
);
2979 found
= page
->slinks
;
2984 found
= page
->slinks
+ (page
->slinkcount
- 1);
2990 ret_v
= caml_alloc_small (2, 1);
2991 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2994 unlock ("ml_findlink");
2999 enum { uuri
, ugoto
, utext
, uunexpected
,
3000 ulaunch
, unamed
, uremote
, uremotedest
};
3006 switch (link->dest.kind) { \
3007 case FZ_LINK_GOTO: \
3011 pageno = link->dest.ld.gotor.page; \
3015 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
3016 p.y = link->dest.ld.gotor.lt.y; \
3017 fz_transform_point (&p, &pdim->lctm); \
3018 if (p.y < 0) p.y = 0; \
3020 tup_v = caml_alloc_tuple (2); \
3021 ret_v = caml_alloc_small (1, ugoto); \
3022 Field (tup_v, 0) = Val_int (pageno); \
3023 Field (tup_v, 1) = Val_int (p.y); \
3024 Field (ret_v, 0) = tup_v; \
3029 str_v = caml_copy_string (link->dest.ld.uri.uri); \
3030 ret_v = caml_alloc_small (1, uuri); \
3031 Field (ret_v, 0) = str_v; \
3034 case FZ_LINK_LAUNCH: \
3035 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
3036 ret_v = caml_alloc_small (1, ulaunch); \
3037 Field (ret_v, 0) = str_v; \
3040 case FZ_LINK_NAMED: \
3041 str_v = caml_copy_string (link->dest.ld.named.named); \
3042 ret_v = caml_alloc_small (1, unamed); \
3043 Field (ret_v, 0) = str_v; \
3046 case FZ_LINK_GOTOR: \
3050 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
3051 pageno = link->dest.ld.gotor.page; \
3052 if (pageno == -1) { \
3053 gr_v = caml_copy_string (link->dest.ld.gotor.dest); \
3054 rty = uremotedest; \
3057 gr_v = Val_int (pageno); \
3060 tup_v = caml_alloc_tuple (2); \
3061 ret_v = caml_alloc_small (1, rty); \
3062 Field (tup_v, 0) = str_v; \
3063 Field (tup_v, 1) = gr_v; \
3064 Field (ret_v, 0) = tup_v; \
3072 snprintf (buf, sizeof (buf), \
3073 "unhandled link kind %d", link->dest.kind); \
3074 str_v = caml_copy_string (buf); \
3075 ret_v = caml_alloc_small (1, uunexpected); \
3076 Field (ret_v, 0) = str_v; \
3082 CAMLprim value
ml_getlink (value ptr_v
, value n_v
)
3084 CAMLparam2 (ptr_v
, n_v
);
3085 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3088 struct pagedim
*pdim
;
3089 char *s
= String_val (ptr_v
);
3091 ret_v
= Val_int (0);
3092 if (trylock ("ml_getlink")) {
3096 page
= parse_pointer ("ml_getlink", s
);
3097 ensureslinks (page
);
3098 pdim
= &state
.pagedims
[page
->pdimno
];
3099 link
= page
->slinks
[Int_val (n_v
)].link
;
3102 unlock ("ml_getlink");
3107 CAMLprim value
ml_getlinkcount (value ptr_v
)
3111 char *s
= String_val (ptr_v
);
3113 page
= parse_pointer ("ml_getlinkcount", s
);
3114 CAMLreturn (Val_int (page
->slinkcount
));
3117 CAMLprim value
ml_getlinkrect (value ptr_v
, value n_v
)
3119 CAMLparam2 (ptr_v
, n_v
);
3122 struct slink
*slink
;
3123 char *s
= String_val (ptr_v
);
3125 page
= parse_pointer ("ml_getlinkrect", s
);
3126 ret_v
= caml_alloc_tuple (4);
3127 if (trylock ("ml_getlinkrect")) {
3128 Field (ret_v
, 0) = Val_int (0);
3129 Field (ret_v
, 1) = Val_int (0);
3130 Field (ret_v
, 2) = Val_int (0);
3131 Field (ret_v
, 3) = Val_int (0);
3134 ensureslinks (page
);
3136 slink
= &page
->slinks
[Int_val (n_v
)];
3137 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
3138 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
3139 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
3140 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
3141 unlock ("ml_getlinkrect");
3147 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
3149 CAMLparam3 (ptr_v
, x_v
, y_v
);
3150 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
3153 char *ptr
= String_val (ptr_v
);
3154 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3155 struct pagedim
*pdim
;
3157 ret_v
= Val_int (0);
3158 if (trylock ("ml_whatsunder")) {
3162 page
= parse_pointer ("ml_whatsunder", ptr
);
3163 pdim
= &state
.pagedims
[page
->pdimno
];
3164 x
+= pdim
->bounds
.x0
;
3165 y
+= pdim
->bounds
.y0
;
3166 link
= getlink (page
, x
, y
);
3172 fz_page_block
*pageb
;
3173 fz_text_block
*block
;
3176 for (pageb
= page
->text
->blocks
;
3177 pageb
< page
->text
->blocks
+ page
->text
->len
;
3180 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3181 block
= pageb
->u
.text
;
3184 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3187 for (line
= block
->lines
;
3188 line
< block
->lines
+ block
->len
;
3193 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3196 for (span
= line
->first_span
; span
; span
= span
->next
) {
3200 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3203 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3205 fz_text_char_bbox (&bbox
, span
, charnum
);
3208 if (x
>= b
->x0
&& x
<= b
->x1
3209 && y
>= b
->y0
&& y
<= b
->y1
) {
3210 fz_text_style
*style
= span
->text
->style
;
3212 style
->font
&& style
->font
->name
3214 : "Span has no font name"
3216 FT_FaceRec
*face
= style
->font
->ft_face
;
3217 if (face
&& face
->family_name
) {
3219 char *n1
= face
->family_name
;
3220 size_t l1
= strlen (n1
);
3221 size_t l2
= strlen (n2
);
3223 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
3224 s
= malloc (l1
+ l2
+ 2);
3228 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
3229 str_v
= caml_copy_string (s
);
3234 if (str_v
== Val_unit
) {
3235 str_v
= caml_copy_string (n2
);
3237 ret_v
= caml_alloc_small (1, utext
);
3238 Field (ret_v
, 0) = str_v
;
3247 unlock ("ml_whatsunder");
3253 enum { mark_page
, mark_block
, mark_line
, mark_word
};
3255 static int uninteresting (int c
)
3257 return c
== ' ' || c
== '\n' || c
== '\t' || c
== '\n' || c
== '\r'
3261 CAMLprim value
ml_clearmark (value ptr_v
)
3264 char *s
= String_val (ptr_v
);
3267 if (trylock ("ml_clearmark")) {
3271 page
= parse_pointer ("ml_clearmark", s
);
3272 page
->fmark
.span
= NULL
;
3273 page
->lmark
.span
= NULL
;
3277 unlock ("ml_clearmark");
3279 CAMLreturn (Val_unit
);
3282 CAMLprim value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
3284 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
3289 fz_page_block
*pageb
;
3290 fz_text_block
*block
;
3291 struct pagedim
*pdim
;
3292 int mark
= Int_val (mark_v
);
3293 char *s
= String_val (ptr_v
);
3294 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3296 ret_v
= Val_bool (0);
3297 if (trylock ("ml_markunder")) {
3301 page
= parse_pointer ("ml_markunder", s
);
3302 pdim
= &state
.pagedims
[page
->pdimno
];
3306 if (mark
== mark_page
) {
3308 fz_page_block
*pb1
= NULL
, *pb2
= NULL
;
3310 for (i
= 0; i
< page
->text
->len
; ++i
) {
3311 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3312 pb1
= &page
->text
->blocks
[i
];
3316 if (!pb1
) goto unlock
;
3318 for (i
= page
->text
->len
- 1; i
>= 0; --i
) {
3319 if (page
->text
->blocks
[i
].type
== FZ_PAGE_BLOCK_TEXT
) {
3320 pb2
= &page
->text
->blocks
[i
];
3324 if (!pb2
) goto unlock
;
3326 block
= pb1
->u
.text
;
3329 page
->fmark
.span
= block
->lines
->first_span
;
3331 block
= pb2
->u
.text
;
3332 line
= &block
->lines
[block
->len
- 1];
3333 page
->lmark
.i
= line
->last_span
->len
- 1;
3334 page
->lmark
.span
= line
->last_span
;
3335 ret_v
= Val_bool (1);
3339 x
+= pdim
->bounds
.x0
;
3340 y
+= pdim
->bounds
.y0
;
3342 for (pageb
= page
->text
->blocks
;
3343 pageb
< page
->text
->blocks
+ page
->text
->len
;
3345 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3346 block
= pageb
->u
.text
;
3349 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3352 if (mark
== mark_block
) {
3354 page
->fmark
.span
= block
->lines
->first_span
;
3356 line
= &block
->lines
[block
->len
- 1];
3357 page
->lmark
.i
= line
->last_span
->len
- 1;
3358 page
->lmark
.span
= line
->last_span
;
3359 ret_v
= Val_bool (1);
3363 for (line
= block
->lines
;
3364 line
< block
->lines
+ block
->len
;
3369 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3372 if (mark
== mark_line
) {
3374 page
->fmark
.span
= line
->first_span
;
3376 page
->lmark
.i
= line
->last_span
->len
- 1;
3377 page
->lmark
.span
= line
->last_span
;
3378 ret_v
= Val_bool (1);
3382 for (span
= line
->first_span
; span
; span
= span
->next
) {
3386 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
3389 for (charnum
= 0; charnum
< span
->len
; ++charnum
) {
3391 fz_text_char_bbox (&bbox
, span
, charnum
);
3394 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
) {
3396 int charnum2
, charnum3
= -1, charnum4
= -1;
3398 if (uninteresting (span
->text
[charnum
].c
)) goto unlock
;
3400 for (charnum2
= charnum
; charnum2
>= 0; --charnum2
) {
3401 if (uninteresting (span
->text
[charnum2
].c
)) {
3402 charnum3
= charnum2
+ 1;
3406 if (charnum3
== -1) charnum3
= 0;
3408 for (charnum2
= charnum
+ 1;
3409 charnum2
< span
->len
;
3411 if (uninteresting (span
->text
[charnum2
].c
)) break;
3412 charnum4
= charnum2
;
3414 if (charnum4
== -1) goto unlock
;
3416 page
->fmark
.i
= charnum3
;
3417 page
->fmark
.span
= span
;
3419 page
->lmark
.i
= charnum4
;
3420 page
->lmark
.span
= span
;
3421 ret_v
= Val_bool (1);
3429 if (!Bool_val (ret_v
)) {
3430 page
->fmark
.span
= NULL
;
3431 page
->lmark
.span
= NULL
;
3435 unlock ("ml_markunder");
3441 CAMLprim value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
3443 CAMLparam3 (ptr_v
, x_v
, y_v
);
3444 CAMLlocal2 (ret_v
, res_v
);
3447 fz_page_block
*pageb
;
3448 struct pagedim
*pdim
;
3449 char *s
= String_val (ptr_v
);
3450 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3452 ret_v
= Val_int (0);
3453 if (trylock ("ml_rectofblock")) {
3457 page
= parse_pointer ("ml_rectofblock", s
);
3458 pdim
= &state
.pagedims
[page
->pdimno
];
3459 x
+= pdim
->bounds
.x0
;
3460 y
+= pdim
->bounds
.y0
;
3464 for (pageb
= page
->text
->blocks
;
3465 pageb
< page
->text
->blocks
+ page
->text
->len
;
3467 switch (pageb
->type
) {
3468 case FZ_PAGE_BLOCK_TEXT
:
3469 b
= &pageb
->u
.text
->bbox
;
3472 case FZ_PAGE_BLOCK_IMAGE
:
3473 b
= &pageb
->u
.image
->bbox
;
3480 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
3485 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3486 ret_v
= caml_alloc_small (1, 1);
3487 Store_double_field (res_v
, 0, b
->x0
);
3488 Store_double_field (res_v
, 1, b
->x1
);
3489 Store_double_field (res_v
, 2, b
->y0
);
3490 Store_double_field (res_v
, 3, b
->y1
);
3491 Field (ret_v
, 0) = res_v
;
3493 unlock ("ml_rectofblock");
3499 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
3501 CAMLparam2 (ptr_v
, rect_v
);
3504 struct pagedim
*pdim
;
3505 char *s
= String_val (ptr_v
);
3506 int i
, x0
, x1
, y0
, y1
, fi
, li
;
3508 fz_page_block
*pageb
;
3509 fz_text_block
*block
;
3510 fz_text_span
*span
, *fspan
, *lspan
;
3512 if (trylock ("ml_seltext")) {
3516 page
= parse_pointer ("ml_seltext", s
);
3519 pdim
= &state
.pagedims
[page
->pdimno
];
3520 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
3521 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
3522 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
3523 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
3534 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3535 glColor3ub (128, 128, 128);
3536 recti (x0
, y0
, x1
, y1
);
3537 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3541 fspan
= page
->fmark
.span
;
3544 lspan
= page
->lmark
.span
;
3546 for (pageb
= page
->text
->blocks
;
3547 pageb
< page
->text
->blocks
+ page
->text
->len
;
3549 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3550 block
= pageb
->u
.text
;
3551 for (line
= block
->lines
;
3552 line
< block
->lines
+ block
->len
;
3555 for (span
= line
->first_span
; span
; span
= span
->next
) {
3556 for (i
= 0; i
< span
->len
; ++i
) {
3559 fz_text_char_bbox (&b
, span
, i
);
3561 if (x0
>= b
.x0
&& x0
<= b
.x1
3562 && y0
>= b
.y0
&& y0
<= b
.y1
) {
3567 if (x1
>= b
.x0
&& x1
<= b
.x1
3568 && y1
>= b
.y0
&& y1
<= b
.y1
) {
3573 if (0 && selected
) {
3574 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
3575 glColor3ub (128, 128, 128);
3576 recti (b
.x0
, b
.y0
, b
.x1
, b
.y1
);
3577 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
3583 if (x1
< x0
&& fspan
== lspan
) {
3595 page
->fmark
.span
= fspan
;
3598 page
->lmark
.span
= lspan
;
3600 unlock ("ml_seltext");
3603 CAMLreturn (Val_unit
);
3606 static int UNUSED_ATTR
pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
3611 for (i
= a
; i
<= b
; ++i
) {
3612 len
= fz_runetochar (buf
, span
->text
[i
].c
);
3613 ret
= fwrite (buf
, len
, 1, f
);
3616 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
3617 len
, ret
, strerror (errno
));
3625 value
ml_popen (value UNUSED_ATTR u1
, value UNUSED_ATTR u2
)
3627 caml_failwith ("ml_popen not implemented under Cygwin");
3630 CAMLprim value
ml_popen (value command_v
, value fds_v
)
3632 CAMLparam2 (command_v
, fds_v
);
3633 CAMLlocal2 (l_v
, tup_v
);
3636 value earg_v
= Nothing
;
3637 posix_spawnattr_t attr
;
3638 posix_spawn_file_actions_t fa
;
3639 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3641 argv
[2] = String_val (command_v
);
3643 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3644 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3647 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3648 msg
= "posix_spawnattr_init";
3652 #ifdef POSIX_SPAWN_USEVFORK
3653 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3654 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3659 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3662 tup_v
= Field (l_v
, 0);
3663 fd1
= Int_val (Field (tup_v
, 0));
3664 fd2
= Int_val (Field (tup_v
, 1));
3666 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3667 msg
= "posix_spawn_file_actions_addclose";
3673 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3674 msg
= "posix_spawn_file_actions_adddup2";
3681 if ((ret
= posix_spawn (NULL
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3682 msg
= "posix_spawn";
3687 if ((ret
= posix_spawnattr_destroy (&attr
)) != 0) {
3688 fprintf (stderr
, "posix_spawnattr_destroy: %s\n", strerror (ret
));
3692 if ((ret
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3693 fprintf (stderr
, "posix_spawn_file_actions_destroy: %s\n",
3698 unix_error (ret
, msg
, earg_v
);
3700 CAMLreturn (Val_unit
);
3704 CAMLprim value
ml_hassel (value ptr_v
)
3709 char *s
= String_val (ptr_v
);
3711 ret_v
= Val_bool (0);
3712 if (trylock ("ml_hassel")) {
3716 page
= parse_pointer ("ml_hassel", s
);
3717 ret_v
= Val_bool (page
->fmark
.span
&& page
->lmark
.span
);
3718 unlock ("ml_hassel");
3723 CAMLprim value
ml_copysel (value fd_v
, value ptr_v
)
3725 CAMLparam2 (fd_v
, ptr_v
);
3730 fz_page_block
*pageb
;
3731 fz_text_block
*block
;
3732 int fd
= Int_val (fd_v
);
3733 char *s
= String_val (ptr_v
);
3735 if (trylock ("ml_copysel")) {
3739 page
= parse_pointer ("ml_copysel", s
);
3741 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
3742 fprintf (stderr
, "nothing to copy on page %d\n", page
->pageno
);
3746 f
= fdopen (fd
, "w");
3748 fprintf (stderr
, "failed to fdopen sel pipe (from fd %d): %s\n",
3749 fd
, strerror (errno
));
3753 for (pageb
= page
->text
->blocks
;
3754 pageb
< page
->text
->blocks
+ page
->text
->len
;
3756 if (pageb
->type
!= FZ_PAGE_BLOCK_TEXT
) continue;
3757 block
= pageb
->u
.text
;
3758 for (line
= block
->lines
;
3759 line
< block
->lines
+ block
->len
;
3763 for (span
= line
->first_span
; span
; span
= span
->next
) {
3766 seen
|= span
== page
->fmark
.span
|| span
== page
->lmark
.span
;
3767 a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
3768 b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
3771 if (pipespan (f
, span
, a
, b
)) {
3774 if (span
== page
->lmark
.span
) {
3777 if (span
== line
->last_span
) {
3778 if (putc ('\n', f
) == EOF
) {
3780 "failed break line on sel pipe: %s\n",
3791 int ret
= fclose (f
);
3794 if (errno
!= ECHILD
) {
3795 fprintf (stderr
, "failed to close sel pipe: %s\n",
3801 unlock ("ml_copysel");
3806 fprintf (stderr
, "failed to close sel pipe: %s\n",
3810 CAMLreturn (Val_unit
);
3813 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
3815 CAMLparam1 (pagedimno_v
);
3817 int pagedimno
= Int_val (pagedimno_v
);
3820 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3821 if (trylock ("ml_getpdimrect")) {
3822 box
= fz_empty_rect
;
3825 box
= state
.pagedims
[pagedimno
].mediabox
;
3826 unlock ("ml_getpdimrect");
3829 Store_double_field (ret_v
, 0, box
.x0
);
3830 Store_double_field (ret_v
, 1, box
.x1
);
3831 Store_double_field (ret_v
, 2, box
.y0
);
3832 Store_double_field (ret_v
, 3, box
.y1
);
3837 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
,
3838 value dw_v
, value cols_v
)
3840 CAMLparam3 (winw_v
, winh_v
, dw_v
);
3846 double winw
= Int_val (winw_v
);
3847 double winh
= Int_val (winh_v
);
3848 double dw
= Int_val (dw_v
);
3849 double cols
= Int_val (cols_v
);
3850 double pw
= 1.0, ph
= 1.0;
3852 if (trylock ("ml_zoom_for_height")) {
3856 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3857 double w
= p
->pagebox
.x1
/ cols
;
3858 double h
= p
->pagebox
.y1
;
3862 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3864 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3867 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3868 unlock ("ml_zoom_for_height");
3870 ret_v
= caml_copy_double (zoom
);
3874 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
3876 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3878 int pt
= Int_val(pt_v
);
3879 int x
= Int_val (x_v
);
3880 int y
= Int_val (y_v
);
3883 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3884 ret_v
= caml_copy_double (w
);
3888 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
3890 CAMLparam2 (pt_v
, string_v
);
3892 int pt
= Int_val (pt_v
);
3895 w
= measure_string (state
.face
, pt
, String_val (string_v
));
3896 ret_v
= caml_copy_double (w
);
3900 CAMLprim value
ml_getpagebox (value opaque_v
)
3902 CAMLparam1 (opaque_v
);
3908 char *s
= String_val (opaque_v
);
3909 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
3911 ret_v
= caml_alloc_tuple (4);
3912 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3913 dev
->hints
|= FZ_IGNORE_SHADE
;
3915 switch (page
->type
) {
3917 ctm
= pagectm (page
);
3918 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, &ctm
, NULL
);
3922 ctm
= pagectm (page
);
3923 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, &ctm
, NULL
);
3927 rect
= fz_infinite_rect
;
3931 fz_free_device (dev
);
3932 fz_round_rect (&bbox
, &rect
);
3933 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3934 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3935 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3936 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3941 CAMLprim value
ml_setaalevel (value level_v
)
3943 CAMLparam1 (level_v
);
3945 state
.aalevel
= Int_val (level_v
);
3946 CAMLreturn (Val_unit
);
3949 #pragma GCC diagnostic push
3950 #pragma GCC diagnostic ignored "-Wvariadic-macros"
3951 #include <X11/Xlib.h>
3952 #pragma GCC diagnostic pop
3960 XVisualInfo
*visual
;
3963 CAMLprim value
ml_glxinit (value display_v
, value wid_v
, value screen_v
)
3965 CAMLparam3 (display_v
, wid_v
, screen_v
);
3966 int attribs
[] = {GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
3968 glx
.dpy
= XOpenDisplay (String_val (display_v
));
3970 caml_failwith ("XOpenDisplay");
3973 glx
.visual
= glXChooseVisual (glx
.dpy
, Int_val (screen_v
), attribs
);
3975 XCloseDisplay (glx
.dpy
);
3976 caml_failwith ("glXChooseVisual");
3979 glx
.wid
= Int_val (wid_v
);
3980 CAMLreturn (Val_int (glx
.visual
->visualid
));
3983 CAMLprim value
ml_glxcompleteinit (value unit_v
)
3985 CAMLparam1 (unit_v
);
3987 glx
.ctx
= glXCreateContext (glx
.dpy
, glx
.visual
, NULL
, True
);
3989 caml_failwith ("glXCreateContext");
3995 if (!glXMakeCurrent (glx
.dpy
, glx
.wid
, glx
.ctx
)) {
3996 glXDestroyContext (glx
.dpy
, glx
.ctx
);
3998 caml_failwith ("glXMakeCurrent");
4000 CAMLreturn (Val_unit
);
4003 CAMLprim value
ml_swapb (value unit_v
)
4005 CAMLparam1 (unit_v
);
4006 glXSwapBuffers (glx
.dpy
, glx
.wid
);
4007 CAMLreturn (Val_unit
);
4010 #include "keysym2ucs.c"
4012 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
4014 CAMLparam1 (keysym_v
);
4016 KeySym keysym
= Int_val (keysym_v
);
4021 rune
= keysym2ucs (keysym
);
4022 len
= fz_runetochar (buf
, rune
);
4024 str_v
= caml_copy_string (buf
);
4028 enum { piunknown
, pilinux
, piosx
, pisun
, pibsd
, picygwin
};
4030 CAMLprim value
ml_platform (value unit_v
)
4032 CAMLparam1 (unit_v
);
4033 CAMLlocal2 (tup_v
, arr_v
);
4034 int platid
= piunknown
;
4037 #if defined __linux__
4039 #elif defined __CYGWIN__
4041 #elif defined __DragonFly__ || defined __FreeBSD__
4042 || defined __OpenBSD__
|| defined __NetBSD__
4044 #elif defined __sun__
4046 #elif defined __APPLE__
4049 if (uname (&buf
)) err (1, "uname");
4051 tup_v
= caml_alloc_tuple (2);
4053 char const *sar
[] = {
4060 arr_v
= caml_copy_string_array (sar
);
4062 Field (tup_v
, 0) = Val_int (platid
);
4063 Field (tup_v
, 1) = arr_v
;
4067 CAMLprim value
ml_cloexec (value fd_v
)
4070 int fd
= Int_val (fd_v
);
4072 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
4073 uerror ("fcntl", Nothing
);
4075 CAMLreturn (Val_unit
);
4078 CAMLprim value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
4080 CAMLparam2 (w_v
, h_v
);
4083 int w
= Int_val (w_v
);
4084 int h
= Int_val (h_v
);
4085 int cs
= Int_val (cs_v
);
4087 if (state
.pbo_usable
) {
4088 pbo
= calloc (sizeof (*pbo
), 1);
4090 err (1, "calloc pbo");
4102 errx (1, "ml_getpbo: invalid colorspace %d", cs
);
4105 state
.glGenBuffersARB (1, &pbo
->id
);
4106 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
4107 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->size
,
4108 NULL
, GL_STREAM_DRAW
);
4109 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
4111 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4113 fprintf (stderr
, "glMapBufferARB failed: %#x\n", glGetError ());
4114 state
.glDeleteBuffersARB (1, &pbo
->id
);
4116 ret_v
= caml_copy_string ("0");
4122 res
= snprintf (NULL
, 0, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4124 err (1, "snprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4128 err (1, "malloc %d bytes failed", res
+1);
4130 res
= sprintf (s
, "%" FMT_ptr
, FMT_ptr_cast (pbo
));
4132 err (1, "sprintf %" FMT_ptr
" failed", FMT_ptr_cast (pbo
));
4134 ret_v
= caml_copy_string (s
);
4139 ret_v
= caml_copy_string ("0");
4144 CAMLprim value
ml_freepbo (value s_v
)
4147 char *s
= String_val (s_v
);
4148 struct tile
*tile
= parse_pointer ("ml_freepbo", s
);
4151 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
4153 tile
->pbo
->ptr
= NULL
;
4154 tile
->pbo
->size
= -1;
4156 CAMLreturn (Val_unit
);
4159 CAMLprim value
ml_unmappbo (value s_v
)
4162 char *s
= String_val (s_v
);
4163 struct tile
*tile
= parse_pointer ("ml_unmappbo", s
);
4166 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
4167 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
4168 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
4170 tile
->pbo
->ptr
= NULL
;
4171 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
4173 CAMLreturn (Val_unit
);
4176 static void setuppbo (void)
4178 #define GGPA(n) (*(void (**) ()) &state.n = glXGetProcAddress ((GLubyte *) #n))
4179 state
.pbo_usable
= GGPA (glBindBufferARB
)
4180 && GGPA (glUnmapBufferARB
)
4181 && GGPA (glMapBufferARB
)
4182 && GGPA (glBufferDataARB
)
4183 && GGPA (glGenBuffersARB
)
4184 && GGPA (glDeleteBuffersARB
);
4188 CAMLprim value
ml_pbo_usable (value unit_v
)
4190 CAMLparam1 (unit_v
);
4191 CAMLreturn (Val_bool (state
.pbo_usable
));
4194 CAMLprim value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
4196 CAMLparam3 (ptr_v
, x_v
, y_v
);
4197 CAMLlocal2 (ret_v
, tup_v
);
4199 char *s
= String_val (ptr_v
);
4200 int x
= Int_val (x_v
), y
= Int_val (y_v
);
4201 struct pagedim
*pdim
;
4205 page
= parse_pointer ("ml_unproject", s
);
4206 pdim
= &state
.pagedims
[page
->pdimno
];
4208 ret_v
= Val_int (0);
4209 if (trylock ("ml_unproject")) {
4213 switch (page
->type
) {
4215 trimctm (page
->u
.pdfpage
, page
->pdimno
);
4221 p
.x
= x
+ pdim
->bounds
.x0
;
4222 p
.y
= y
+ pdim
->bounds
.y0
;
4224 fz_concat (&ctm
, &pdim
->tctm
, &pdim
->ctm
);
4225 fz_invert_matrix (&ctm
, &ctm
);
4226 fz_transform_point (&p
, &ctm
);
4228 tup_v
= caml_alloc_tuple (2);
4229 ret_v
= caml_alloc_small (1, 1);
4230 Field (tup_v
, 0) = Val_int (p
.x
);
4231 Field (tup_v
, 1) = Val_int (p
.y
);
4232 Field (ret_v
, 0) = tup_v
;
4234 unlock ("ml_unproject");
4239 static void makestippletex (void)
4241 const char pixels
[] = "\xff\xff\0\0";
4242 glGenTextures (1, &state
.stid
);
4243 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
4244 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
4245 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
4258 CAMLprim value
ml_fz_version (value UNUSED_ATTR unit_v
)
4260 return caml_copy_string (FZ_VERSION
);
4263 #ifdef USE_FONTCONFIG
4269 static fz_font
*fc_load_system_font_func (fz_context
*ctx
,
4273 int UNUSED_ATTR needs_exact_metrics
)
4280 FcPattern
*pat
, *pat1
;
4282 lprintf ("looking up %s bold:%d italic:%d needs_exact_metrics:%d\n",
4283 name
, bold
, italic
, needs_exact_metrics
);
4286 fc
.config
= FcInitLoadConfigAndFonts ();
4288 lprintf ("FcInitLoadConfigAndFonts failed\n");
4292 if (!fc
.config
) return NULL
;
4294 size
= strlen (name
);
4295 if (bold
) size
+= sizeof (":bold") - 1;
4296 if (italic
) size
+= sizeof (":italic") - 1;
4299 buf
= malloc (size
);
4301 err (1, "malloc %zu failed", size
);
4305 if (bold
&& italic
) {
4306 strcat (buf
, ":bold:italic");
4309 if (bold
) strcat (buf
, ":bold");
4310 if (italic
) strcat (buf
, ":italic");
4312 for (i
= 0; i
< size
; ++i
) {
4313 if (buf
[i
] == ',' || buf
[i
] == '-') buf
[i
] = ':';
4316 lprintf ("fcbuf=%s\n", buf
);
4317 pat
= FcNameParse ((FcChar8
*) buf
);
4319 printd ("emsg FcNameParse failed\n");
4324 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4325 printd ("emsg FcConfigSubstitute failed\n");
4329 FcDefaultSubstitute (pat
);
4331 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4333 printd ("emsg FcFontMatch failed\n");
4334 FcPatternDestroy (pat
);
4339 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4340 printd ("emsg FcPatternGetString failed\n");
4341 FcPatternDestroy (pat
);
4342 FcPatternDestroy (pat1
);
4348 printd ("emsg name=%s path=%s\n", name
, path
);
4350 font
= fz_new_font_from_file (ctx
, name
, (char *) path
, 0, 0);
4351 FcPatternDestroy (pat
);
4352 FcPatternDestroy (pat1
);
4358 CAMLprim value
ml_init (value csock_v
, value params_v
)
4360 CAMLparam2 (csock_v
, params_v
);
4361 CAMLlocal2 (trim_v
, fuzz_v
);
4368 struct sigaction sa
;
4370 state
.csock
= Int_val (csock_v
);
4371 state
.rotate
= Int_val (Field (params_v
, 0));
4372 state
.fitmodel
= Int_val (Field (params_v
, 1));
4373 trim_v
= Field (params_v
, 2);
4374 texcount
= Int_val (Field (params_v
, 3));
4375 state
.sliceheight
= Int_val (Field (params_v
, 4));
4376 mustoresize
= Int_val (Field (params_v
, 5));
4377 colorspace
= Int_val (Field (params_v
, 6));
4378 fontpath
= String_val (Field (params_v
, 7));
4380 if (caml_string_length (Field (params_v
, 8)) > 0) {
4381 state
.trimcachepath
= strdup (String_val (Field (params_v
, 8)));
4383 if (!state
.trimcachepath
) {
4384 fprintf (stderr
, "failed to strdup trimcachepath: %s\n",
4388 haspboext
= Bool_val (Field (params_v
, 9));
4390 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
4392 #ifdef USE_FONTCONFIG
4393 if (Bool_val (Field (params_v
, 10))) {
4394 fz_install_load_system_font_funcs (
4395 state
.ctx
, fc_load_system_font_func
, NULL
4400 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
4401 fuzz_v
= Field (trim_v
, 1);
4402 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
4403 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
4404 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
4405 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
4407 set_tex_params (colorspace
);
4410 #ifndef USE_FONTCONFIG
4411 state
.face
= load_font (fontpath
);
4415 char *buf
= fontpath
;
4416 FcPattern
*pat
, *pat1
;
4419 fc
.config
= FcInitLoadConfigAndFonts ();
4421 errx (1, "FcInitLoadConfigAndFonts failed");
4424 pat
= FcNameParse ((FcChar8
*) buf
);
4426 errx (1, "FcNameParse failed");
4429 if (!FcConfigSubstitute (fc
.config
, pat
, FcMatchPattern
)) {
4430 errx (1, "FcConfigSubstitute failed");
4432 FcDefaultSubstitute (pat
);
4434 pat1
= FcFontMatch (fc
.config
, pat
, &result
);
4436 errx (1, "FcFontMatch failed");
4439 if (FcPatternGetString (pat1
, FC_FILE
, 0, &path
) != FcResultMatch
) {
4440 errx (1, "FcPatternGetString failed");
4443 state
.face
= load_font ((char *) path
);
4444 FcPatternDestroy (pat
);
4445 FcPatternDestroy (pat1
);
4450 void *base
= pdf_lookup_substitute_font (0, 0, 0, 0, &len
);
4452 state
.face
= load_builtin_font (base
, len
);
4454 if (!state
.face
) _exit (1);
4456 realloctexts (texcount
);
4465 sa
.sa_handler
= SIG_IGN
;
4466 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4468 sa
.sa_handler
= SIG_DFL
;
4469 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
4471 if (sigemptyset (&sa
.sa_mask
)) {
4472 err (1, "sigemptyset");
4474 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
4475 err (1, "sigaction");
4478 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
4480 errx (1, "pthread_create: %s", strerror (ret
));
4483 CAMLreturn (Val_unit
);