1 /* lots of code c&p-ed directly from mupdf */
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
21 #include <OpenGL/gl.h>
26 #include <caml/fail.h>
27 #include <caml/alloc.h>
28 #include <caml/memory.h>
29 #include <caml/unixsupport.h>
36 #include FT_FREETYPE_H
41 #define NORETURN __attribute__ ((noreturn))
42 #define UNUSED __attribute__ ((unused))
43 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
44 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
49 #define GCC_FMT_ATTR(a, b)
55 #define FMT_ptr_cast(p) (p)
56 #define FMT_ptr_cast2(p) (p)
58 static void NORETURN
GCC_FMT_ATTR (2, 3)
59 err (int exitcode
, const char *fmt
, ...)
66 vfprintf (stderr
, fmt
, ap
);
68 fprintf (stderr
, ": %s\n", strerror (savederrno
));
73 static void NORETURN
GCC_FMT_ATTR (2, 3)
74 errx (int exitcode
, const char *fmt
, ...)
79 vfprintf (stderr
, fmt
, ap
);
86 #ifndef GL_TEXTURE_RECTANGLE_ARB
87 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
91 #define GL_BGRA 0x80E1
94 #ifndef GL_UNSIGNED_INT_8_8_8_8
95 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
98 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
99 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
103 #define lprintf printf
108 #define ARSERT(cond) for (;;) { \
110 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
125 struct slice slices
[1];
136 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
139 enum { DPDF
, DXPS
, DCBZ
};
153 fz_display_list
*dlist
;
158 void (*freepage
) (void *);
164 struct pagedim
*pagedims
;
173 fz_glyph_cache
*cache
;
184 fz_colorspace
*colorspace
;
206 void (*closedoc
) (void);
207 void (*freepage
) (void *);
210 static void UNUSED
debug_rect (const char *cap
, fz_rect r
)
212 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
215 static void UNUSED
debug_bbox (const char *cap
, fz_bbox r
)
217 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
220 static void UNUSED
debug_matrix (const char *cap
, fz_matrix m
)
222 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
223 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
226 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
228 static void lock (const char *cap
)
230 int ret
= pthread_mutex_lock (&mutex
);
232 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
236 static void unlock (const char *cap
)
238 int ret
= pthread_mutex_unlock (&mutex
);
240 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
244 static int trylock (const char *cap
)
246 int ret
= pthread_mutex_trylock (&mutex
);
248 if (ret
&& ret
!= EBUSY
) {
249 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
254 static void *parse_pointer (const char *cap
, const char *s
)
259 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
261 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
266 static double now (void)
270 if (gettimeofday (&tv
, NULL
)) {
271 err (1, "gettimeofday");
273 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
276 static int hasdata (void)
279 ret
= ioctl (state
.cr
, FIONREAD
, &avail
);
280 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
284 CAMLprim value
ml_hasdata (value fd_v
)
289 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
290 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
291 CAMLreturn (Val_bool (avail
> 0));
294 static void readdata (void *p
, int size
)
298 n
= read (state
.cr
, p
, size
);
300 if (!n
) errx (1, "EOF while reading");
301 err (1, "read (req %d, ret %zd)", size
, n
);
305 static void writedata (char *p
, int size
)
310 buf
[0] = (size
>> 24) & 0xff;
311 buf
[1] = (size
>> 16) & 0xff;
312 buf
[2] = (size
>> 8) & 0xff;
313 buf
[3] = (size
>> 0) & 0xff;
315 n
= write (state
.cw
, buf
, 4);
317 if (!n
) errx (1, "EOF while writing length");
318 err (1, "write %zd", n
);
321 n
= write (state
.cw
, p
, size
);
323 if (!n
) errx (1, "EOF while writing data");
324 err (1, "write (req %d, ret %zd)", size
, n
);
328 static int readlen (void)
333 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
336 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
344 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
347 len
= vsnprintf (buf
, size
, fmt
, ap
);
350 if (len
> -1 && len
< size
) {
351 writedata (buf
, len
);
361 buf
= realloc (buf
, size
);
366 static void closepdf (void)
369 pdf_close_document (state
.u
.pdf
);
374 static void closexps (void)
377 xps_close_document (state
.u
.xps
);
382 static void closecbz (void)
385 cbz_close_document (state
.u
.cbz
);
390 static void freepdfpage (void *ptr
)
392 pdf_free_page (state
.u
.pdf
, ptr
);
395 static void freexpspage (void *ptr
)
397 xps_free_page (state
.u
.xps
, ptr
);
400 static void freecbzpage (void *ptr
)
402 cbz_free_page (state
.u
.cbz
, ptr
);
405 static void openxref (char *filename
, char *password
)
409 for (i
= 0; i
< state
.texcount
; ++i
) {
410 state
.texowners
[i
].w
= -1;
411 state
.texowners
[i
].slice
= NULL
;
414 if (state
.closedoc
) state
.closedoc ();
416 len
= strlen (filename
);
422 ext
[0] = tolower (filename
[len
-3]);
423 ext
[1] = tolower (filename
[len
-2]);
424 ext
[2] = tolower (filename
[len
-1]);
426 /**/ if (ext
[0] == 'x' && ext
[1] == 'p' && ext
[2] == 's') {
429 else if (ext
[0] == 'c' && ext
[1] == 'b' && ext
[2] == 'z') {
434 if (state
.pagedims
) {
435 free (state
.pagedims
);
436 state
.pagedims
= NULL
;
438 state
.pagedimcount
= 0;
440 fz_set_aa_level (state
.ctx
, state
.aalevel
);
441 switch (state
.type
) {
443 state
.u
.pdf
= pdf_open_document (state
.ctx
, filename
);
444 if (pdf_needs_password (state
.u
.pdf
)) {
445 int okay
= pdf_authenticate_password (state
.u
.pdf
, password
);
447 errx (1, "invalid password");
450 state
.pagecount
= pdf_count_pages (state
.u
.pdf
);
451 state
.closedoc
= closepdf
;
452 state
.freepage
= freepdfpage
;
456 state
.u
.xps
= xps_open_document (state
.ctx
, filename
);
457 state
.pagecount
= xps_count_pages (state
.u
.xps
);
458 state
.closedoc
= closexps
;
459 state
.freepage
= freexpspage
;
463 state
.u
.cbz
= cbz_open_document (state
.ctx
, filename
);
464 state
.pagecount
= cbz_count_pages (state
.u
.cbz
);
465 state
.closedoc
= closecbz
;
466 state
.freepage
= freecbzpage
;
471 static void pdfinfo (void)
473 if (state
.type
== DPDF
) {
476 printd ("info PDF version\t%d.%d",
477 state
.u
.pdf
->version
/ 10, state
.u
.pdf
->version
% 10);
479 infoobj
= fz_dict_gets (state
.u
.pdf
->trailer
, "Info");
483 char *items
[] = { "Title", "Author", "Creator",
484 "Producer", "CreationDate" };
486 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
487 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
488 s
= pdf_to_utf8 (state
.ctx
, obj
);
491 printd ("title %s", s
);
493 printd ("info %s\t%s", items
[i
], s
);
495 fz_free (state
.ctx
, s
);
502 static void unlinktile (struct tile
*tile
)
506 for (i
= 0; i
< tile
->slicecount
; ++i
) {
507 struct slice
*s
= &tile
->slices
[i
];
509 if (s
->texindex
!= -1) {
510 if (state
.texowners
[s
->texindex
].slice
== s
) {
511 state
.texowners
[s
->texindex
].slice
= NULL
;
517 static void freepage (struct page
*page
)
520 fz_free_text_span (state
.ctx
, page
->text
);
522 page
->freepage (page
->u
.ptr
);
523 fz_free_display_list (state
.ctx
, page
->dlist
);
527 static void freetile (struct tile
*tile
)
531 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
534 fz_drop_pixmap (state
.ctx
, state
.pig
);
536 state
.pig
= tile
->pixmap
;
544 static int cacheline32bytes
;
545 extern char **environ
;
547 static void __attribute__ ((constructor
)) clcheck (void)
549 char **envp
= environ
;
554 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
556 cacheline32bytes
= auxv
[1] == 32;
562 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
564 if (cacheline32bytes
) {
565 intptr_t a1
, a2
, diff
;
566 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
567 vector
unsigned char v
= vec_splat_u8 (-1);
568 vector
unsigned char *p
;
570 a1
= a2
= (intptr_t) pixmap
->samples
;
571 a2
= (a1
+ 31) & ~31;
576 while (a1
!= a2
) *(char *) a1
++ = 0xff;
577 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
578 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
580 vec_st (v
, i
+ 16, p
);
582 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
584 else fz_clear_pixmap_with_value (state
.ctx
, pixmap
, 0xff);
587 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
590 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
593 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
595 if (!pdim
->tctmready
) {
596 if (state
.trimmargins
) {
599 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
600 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
601 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
602 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
613 static fz_matrix
pagectm (struct page
*page
)
615 if (page
->type
== DPDF
) {
616 return fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
617 state
.pagedims
[page
->pdimno
].ctm
);
621 struct pagedim
*pdim
= &state
.pagedims
[page
->pdimno
];
623 ctm
= state
.pagedims
[page
->pdimno
].ctm
;
624 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
,
625 -pdim
->mediabox
.y0
), ctm
);
630 static void *loadpage (int pageno
, int pindex
)
633 struct page
*page
= NULL
;
635 page
= calloc (sizeof (struct page
), 1);
637 err (1, "calloc page %d", pageno
);
640 page
->dlist
= fz_new_display_list (state
.ctx
);
641 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
642 switch (state
.type
) {
644 page
->u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
645 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, fz_identity
, NULL
);
646 page
->freepage
= freepdfpage
;
650 page
->u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
651 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, fz_identity
, NULL
);
652 page
->freepage
= freexpspage
;
656 page
->u
.cbzpage
= cbz_load_page (state
.u
.cbz
, pageno
);
657 cbz_run_page (state
.u
.cbz
, page
->u
.cbzpage
, dev
, fz_identity
, NULL
);
658 page
->freepage
= freecbzpage
;
661 fz_free_device (dev
);
663 page
->pdimno
= pindex
;
664 page
->pageno
= pageno
;
665 page
->gen
= state
.gen
;
666 page
->type
= state
.type
;
671 static struct tile
*alloctile (int h
)
678 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
679 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
680 tile
= calloc (tilesize
, 1);
682 err (1, "can not allocate tile (%" FMT_s
" bytes)", tilesize
);
684 for (i
= 0; i
< slicecount
; ++i
) {
685 int sh
= MIN (h
, state
.sliceheight
);
686 tile
->slices
[i
].h
= sh
;
687 tile
->slices
[i
].texindex
= -1;
690 tile
->slicecount
= slicecount
;
691 tile
->sliceheight
= state
.sliceheight
;
695 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
700 struct pagedim
*pdim
;
702 tile
= alloctile (h
);
703 pdim
= &state
.pagedims
[page
->pdimno
];
708 bbox
.x1
= bbox
.x0
+ w
;
709 bbox
.y1
= bbox
.y0
+ h
;
712 if (state
.pig
->w
== w
714 && state
.pig
->colorspace
== state
.colorspace
) {
715 tile
->pixmap
= state
.pig
;
716 tile
->pixmap
->x
= bbox
.x0
;
717 tile
->pixmap
->y
= bbox
.y0
;
720 fz_drop_pixmap (state
.ctx
, state
.pig
);
726 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
731 clearpixmap (tile
->pixmap
);
732 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
733 fz_run_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
734 fz_free_device (dev
);
739 static void initpdims (void)
745 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
751 switch (state
.type
) {
753 pageobj
= state
.u
.pdf
->page_objs
[pageno
];
755 if (state
.trimmargins
) {
759 page
= pdf_load_page (state
.u
.pdf
, pageno
);
760 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
761 if (state
.trimanew
|| !obj
) {
767 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
768 dev
->hints
|= FZ_IGNORE_SHADE
;
769 ctm
= fz_invert_matrix (page
->ctm
);
770 pdf_run_page (state
.u
.pdf
, page
, dev
, fz_identity
, NULL
);
771 fz_free_device (dev
);
773 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
774 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
775 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
776 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
777 rect
= fz_transform_rect (ctm
, rect
);
778 rect
= fz_intersect_rect (rect
, page
->mediabox
);
780 if (fz_is_empty_rect (rect
)) {
781 mediabox
= page
->mediabox
;
787 obj
= fz_new_array (state
.ctx
, 4);
788 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
789 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
790 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
791 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
792 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
795 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
796 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
797 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
798 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
801 rotate
= page
->rotate
;
802 pdf_free_page (state
.u
.pdf
, page
);
804 printd ("progress %f Trimming %d",
805 (double) (pageno
+ 1) / state
.pagecount
,
811 mediabox
= pdf_to_rect (state
.ctx
,
812 fz_dict_gets (pageobj
, "MediaBox"));
813 if (fz_is_empty_rect (mediabox
)) {
814 fprintf (stderr
, "cannot find page size for page %d\n",
822 cropbox
= pdf_to_rect (state
.ctx
,
823 fz_dict_gets (pageobj
, "CropBox"));
824 if (!fz_is_empty_rect (cropbox
)) {
825 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
827 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
835 page
= xps_load_page (state
.u
.xps
, pageno
);
836 mediabox
= xps_bound_page (state
.u
.xps
, page
);
838 if (state
.trimmargins
) {
843 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
844 dev
->hints
|= FZ_IGNORE_SHADE
;
845 xps_run_page (state
.u
.xps
, page
, dev
, fz_identity
, NULL
);
846 fz_free_device (dev
);
848 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
849 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
850 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
851 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
852 rect
= fz_intersect_rect (rect
, mediabox
);
854 if (!fz_is_empty_rect (rect
)) {
858 xps_free_page (state
.u
.xps
, page
);
859 printd ("progress %f loading %d",
860 (double) (pageno
+ 1) / state
.pagecount
,
868 if (state
.trimmargins
) {
871 page
= cbz_load_page (state
.u
.cbz
, pageno
);
872 mediabox
= cbz_bound_page (state
.u
.cbz
, page
);
873 cbz_free_page (state
.u
.cbz
, page
);
874 printd ("progress %f Trimming %d",
875 (double) (pageno
+ 1) / state
.pagecount
,
879 mediabox
.x0
= mediabox
.y0
= 0;
887 ARSERT (0 && state
.type
);
890 if (state
.pagedimcount
== 0
891 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
892 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
895 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
896 state
.pagedims
= realloc (state
.pagedims
, size
);
897 if (!state
.pagedims
) {
898 err (1, "realloc pagedims to %" FMT_s
" (%d elems)",
899 size
, state
.pagedimcount
+ 1);
902 p
= &state
.pagedims
[state
.pagedimcount
++];
904 p
->mediabox
= mediabox
;
909 if (state
.trimmargins
) {
910 printd ("progress 1 Trimmed %d pages in %f seconds",
911 state
.pagecount
, end
- start
);
914 printd ("vmsg Processed %d pages in %f seconds",
915 state
.pagecount
, end
- start
);
920 static void layout (void)
925 double zoom
, w
, maxw
= 0;
926 struct pagedim
*p
= state
.pagedims
;
928 if (state
.proportional
) {
929 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
930 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
933 maxw
= MAX (w
, maxw
);
938 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
941 ctm
= fz_rotate (state
.rotate
);
942 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
946 if (state
.proportional
) {
947 double scale
= w
/ maxw
;
948 zoom
= (state
.w
/ w
) * scale
;
954 p
->zoomctm
= fz_scale (zoom
, zoom
);
955 ctm
= fz_concat (p
->zoomctm
, ctm
);
957 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
958 p
->pagebox
.x1
-= p
->pagebox
.x0
;
959 p
->pagebox
.y1
-= p
->pagebox
.y0
;
962 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
965 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
969 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
970 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
971 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
977 while (p
-- != state
.pagedims
) {
978 int w
= p
->bounds
.x1
;
979 int h
= p
->bounds
.y1
;
981 printd ("pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
985 static void recurse_outline (fz_outline
*outline
, int level
)
990 struct pagedim
*pdim
= state
.pagedims
;
992 dest
= &outline
->dest
;
993 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
994 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
996 pdim
= &state
.pagedims
[i
];
998 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1001 p
.y
= dest
->ld
.gotor
.lt
.y
;
1002 p
= fz_transform_point (pdim
->lctm
, p
);
1005 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
1009 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1010 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
1012 printd ("o %d %d %d %d %s",
1013 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
1015 if (outline
->down
) {
1016 recurse_outline (outline
->down
, level
+ 1);
1018 outline
= outline
->next
;
1022 static void process_outline (void)
1024 fz_outline
*outline
;
1026 if (!state
.needoutline
) return;
1028 state
.needoutline
= 0;
1029 switch (state
.type
) {
1031 outline
= pdf_load_outline (state
.u
.pdf
);
1034 outline
= xps_load_outline (state
.u
.xps
);
1041 recurse_outline (outline
, 0);
1042 fz_free_outline (state
.ctx
, outline
);
1046 static int comparespans (const void *l
, const void *r
)
1048 fz_text_span
const *const*ls
= l
;
1049 fz_text_span
const *const*rs
= r
;
1050 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
1053 /* wishful thinking function */
1054 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1062 union { void *ptr
; pdf_page
*pdfpage
; xps_page
*xpspage
; } u
;
1063 fz_text_span
*text
, *span
, **pspan
;
1064 struct pagedim
*pdim
, *pdimprev
;
1070 if (!(state
.type
== DPDF
|| state
.type
== DXPS
))
1074 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1075 if (niters
++ == 5) {
1078 printd ("progress 1 attention requested aborting search at %d",
1083 printd ("progress %f searching in page %d",
1084 (double) (pageno
+ 1) / state
.pagecount
,
1089 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
1090 pdim
= &state
.pagedims
[i
];
1091 if (pdim
->pageno
== pageno
) {
1094 if (pdim
->pageno
> pageno
) {
1103 text
= fz_new_text_span (state
.ctx
);
1104 tdev
= fz_new_text_device (state
.ctx
, text
);
1106 switch (state
.type
) {
1108 u
.pdfpage
= pdf_load_page (state
.u
.pdf
, pageno
);
1109 pdf_run_page (state
.u
.pdf
, u
.pdfpage
, tdev
, fz_identity
, NULL
);
1113 u
.xpspage
= xps_load_page (state
.u
.xps
, pageno
);
1114 xps_run_page (state
.u
.xps
, u
.xpspage
, tdev
, fz_identity
, NULL
);
1118 ARSERT (0 && state
.type
);
1121 fz_free_device (tdev
);
1124 for (span
= text
; span
; span
= span
->next
) {
1127 pspan
= malloc (sizeof (void *) * nspans
);
1129 err (1, "malloc span pointers %" FMT_s
, sizeof (void *) * nspans
);
1131 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1134 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1136 j
= forward
? 0 : nspans
- 1;
1141 j
+= forward
? 1 : -1;
1143 for (i
= 0; i
< span
->len
; ++i
) {
1148 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1153 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1157 len
= runetochar (cbuf
, &span
->text
[i
].c
);
1158 if (sizeof (buf
) - 1 - (p
- buf
) > len
) {
1160 for (k
= 0; k
< len
; ++k
)
1172 ret
= regexec (re
, buf
, 1, &rm
, 0);
1174 if (ret
!= REG_NOMATCH
) {
1177 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1178 printd ("msg regexec error `%.*s'",
1179 (int) size
, errbuf
);
1180 fz_free_text_span (state
.ctx
, text
);
1181 state
.freepage (u
.ptr
);
1188 fz_point p1
, p2
, p3
, p4
;
1192 for (a
= 0, c
= 0; c
< rm
.rm_so
&& a
< l
; a
++) {
1193 c
+= runelen (span
->text
[a
].c
);
1195 for (b
= a
; c
< rm
.rm_eo
- 1 && b
< l
; b
++) {
1196 c
+= runelen (span
->text
[b
].c
);
1199 if (runelen (span
->text
[b
].c
) > 1) {
1202 sb
= &span
->text
[MIN (a
, l
-1)].bbox
;
1203 eb
= &span
->text
[MIN (b
, l
-1)].bbox
;
1214 switch (state
.type
) {
1216 trimctm (u
.pdfpage
, pdim
- state
.pagedims
);
1217 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1225 p1
= fz_transform_point (ctm
, p1
);
1226 p2
= fz_transform_point (ctm
, p2
);
1227 p3
= fz_transform_point (ctm
, p3
);
1228 p4
= fz_transform_point (ctm
, p4
);
1231 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1238 printd ("progress 1 found at %d `%.*s' in %f sec",
1239 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1243 printd ("match %d %d %f %f %f %f %f %f %f %f",
1261 fz_free_text_span (state
.ctx
, text
);
1262 state
.freepage (u
.ptr
);
1267 printd ("progress 1 no matches %f sec", end
- start
);
1269 printd ("clearrects");
1272 static void set_tex_params (int colorspace
)
1274 switch (colorspace
) {
1276 state
.texiform
= GL_RGBA8
;
1277 state
.texform
= GL_RGBA
;
1278 state
.texty
= GL_UNSIGNED_BYTE
;
1279 state
.colorspace
= fz_device_rgb
;
1282 state
.texiform
= GL_RGBA8
;
1283 state
.texform
= GL_BGRA
;
1284 state
.texty
= fz_is_big_endian ()
1285 ? GL_UNSIGNED_INT_8_8_8_8
1286 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1287 state
.colorspace
= fz_device_bgr
;
1290 state
.texiform
= GL_LUMINANCE_ALPHA
;
1291 state
.texform
= GL_LUMINANCE_ALPHA
;
1292 state
.texty
= GL_UNSIGNED_BYTE
;
1293 state
.colorspace
= fz_device_gray
;
1296 errx (1, "invalid colorspce %d", colorspace
);
1300 static void realloctexts (int texcount
)
1304 if (texcount
== state
.texcount
) return;
1306 if (texcount
< state
.texcount
) {
1307 glDeleteTextures (state
.texcount
- texcount
,
1308 state
.texids
+ texcount
);
1311 size
= texcount
* sizeof (*state
.texids
);
1312 state
.texids
= realloc (state
.texids
, size
);
1313 if (!state
.texids
) {
1314 err (1, "realloc texids %" FMT_s
, size
);
1317 size
= texcount
* sizeof (*state
.texowners
);
1318 state
.texowners
= realloc (state
.texowners
, size
);
1319 if (!state
.texowners
) {
1320 err (1, "realloc texowners %" FMT_s
, size
);
1322 if (texcount
> state
.texcount
) {
1325 glGenTextures (texcount
- state
.texcount
,
1326 state
.texids
+ state
.texcount
);
1327 for (i
= state
.texcount
; i
< texcount
; ++i
) {
1328 state
.texowners
[i
].w
= -1;
1329 state
.texowners
[i
].slice
= NULL
;
1332 state
.texcount
= texcount
;
1336 static void * mainloop (void *unused
)
1339 int len
, ret
, oldlen
= 0;
1344 errx (1, "readlen returned 0");
1347 if (oldlen
< len
+ 1) {
1348 p
= realloc (p
, len
+ 1);
1350 err (1, "realloc %d failed", len
+ 1);
1357 if (!strncmp ("open", p
, 4)) {
1360 char *filename
= p
+ 5;
1362 filenamelen
= strlen (filename
);
1363 password
= filename
+ filenamelen
+ 1;
1365 openxref (filename
, password
);
1368 printd ("msg Opened %s (press h/F1 to get help)", filename
);
1369 state
.needoutline
= 1;
1371 else if (!strncmp ("cs", p
, 2)) {
1374 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1376 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1379 set_tex_params (colorspace
);
1380 for (i
= 0; i
< state
.texcount
; ++i
) {
1381 state
.texowners
[i
].w
= -1;
1382 state
.texowners
[i
].slice
= NULL
;
1386 else if (!strncmp ("freepage", p
, 8)) {
1389 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1391 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1395 else if (!strncmp ("freetile", p
, 8)) {
1398 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1400 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1404 else if (!strncmp ("search", p
, 6)) {
1405 int icase
, pageno
, y
, ret
, len2
, forward
;
1409 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1410 &icase
, &pageno
, &y
, &forward
, &len2
);
1412 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1415 pattern
= p
+ 6 + len2
;
1416 ret
= regcomp (&re
, pattern
,
1417 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1422 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1423 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1426 search (&re
, pageno
, y
, forward
);
1430 else if (!strncmp ("geometry", p
, 8)) {
1434 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1436 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1444 for (i
= 0; i
< state
.texcount
; ++i
) {
1445 state
.texowners
[i
].slice
= NULL
;
1451 unlock ("geometry");
1452 printd ("continue %d", state
.pagecount
);
1454 else if (!strncmp ("reqlayout", p
, 9)) {
1455 int rotate
, proportional
;
1458 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1460 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1463 state
.rotate
= rotate
;
1464 state
.proportional
= proportional
;
1466 unlock ("reqlayout");
1467 printd ("continue %d", state
.pagecount
);
1469 else if (!strncmp ("page", p
, 4)) {
1472 int pageno
, pindex
, ret
;
1474 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1476 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1481 page
= loadpage (pageno
, pindex
);
1485 printd ("page %" FMT_ptr
" %f", FMT_ptr_cast2 (page
), b
- a
);
1487 else if (!strncmp ("tile", p
, 4)) {
1488 int x
, y
, w
, h
, ret
;
1493 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1494 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1496 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1501 tile
= rendertile (page
, x
, y
, w
, h
);
1505 printd ("tile %d %d %" FMT_ptr
" %u %f",
1507 FMT_ptr_cast2 (tile
),
1508 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1511 else if (!strncmp ("settrim", p
, 7)) {
1515 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1516 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1518 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1522 state
.trimmargins
= trimmargins
;
1523 state
.needoutline
= 1;
1524 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1526 state
.trimfuzz
= fuzz
;
1528 state
.pagedimcount
= 0;
1529 free (state
.pagedims
);
1530 state
.pagedims
= NULL
;
1535 printd ("continue %d", state
.pagecount
);
1537 else if (!strncmp ("sliceh", p
, 6)) {
1540 ret
= sscanf (p
+ 6, " %d", &h
);
1542 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1544 if (h
!= state
.sliceheight
) {
1547 state
.sliceheight
= h
;
1548 for (i
= 0; i
< state
.texcount
; ++i
) {
1549 state
.texowners
[i
].w
= -1;
1550 state
.texowners
[i
].h
= -1;
1551 state
.texowners
[i
].slice
= NULL
;
1555 else if (!strncmp ("interrupt", p
, 9)) {
1556 printd ("vmsg interrupted");
1558 else if (!strncmp ("quit", p
, 4)) {
1562 errx (1, "unknown command %.*s", len
, p
);
1568 CAMLprim value
ml_realloctexts (value texcount_v
)
1570 CAMLparam1 (texcount_v
);
1573 if (trylock ("ml_realloctexts")) {
1577 realloctexts (Int_val (texcount_v
));
1579 unlock ("ml_realloctexts");
1582 CAMLreturn (Val_bool (ok
));
1585 static void showsel (struct page
*page
, int ox
, int oy
)
1589 struct mark first
, last
;
1591 first
= page
->fmark
;
1594 if (!first
.span
|| !last
.span
) return;
1596 glEnable (GL_BLEND
);
1597 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1598 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1600 for (span
= first
.span
; span
; span
= span
->next
) {
1603 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1608 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1609 j
= MIN (first
.i
, last
.i
);
1610 k
= MAX (first
.i
, last
.i
);
1612 else if (span
== first
.span
) {
1615 else if (span
== last
.span
) {
1619 for (i
= j
; i
<= k
; ++i
) {
1620 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1622 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1629 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1631 if (span
== last
.span
) break;
1633 glDisable (GL_BLEND
);
1636 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1639 fz_link
*link
, *links
;
1641 switch (page
->type
) {
1643 links
= page
->u
.pdfpage
->links
;
1647 links
= page
->u
.xpspage
->links
;
1654 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1655 glEnable (GL_LINE_STIPPLE
);
1656 glLineStipple (0.5, 0xcccc);
1658 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1661 for (link
= links
; link
; link
= link
->next
) {
1662 fz_point p1
, p2
, p3
, p4
;
1664 p1
.x
= link
->rect
.x0
;
1665 p1
.y
= link
->rect
.y0
;
1667 p2
.x
= link
->rect
.x1
;
1668 p2
.y
= link
->rect
.y0
;
1670 p3
.x
= link
->rect
.x1
;
1671 p3
.y
= link
->rect
.y1
;
1673 p4
.x
= link
->rect
.x0
;
1674 p4
.y
= link
->rect
.y1
;
1676 p1
= fz_transform_point (ctm
, p1
);
1677 p2
= fz_transform_point (ctm
, p2
);
1678 p3
= fz_transform_point (ctm
, p3
);
1679 p4
= fz_transform_point (ctm
, p4
);
1681 switch (link
->dest
.kind
) {
1682 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1683 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1684 default: glColor3ub (0, 0, 0); break;
1687 glVertex2f (p1
.x
, p1
.y
);
1688 glVertex2f (p2
.x
, p2
.y
);
1689 glVertex2f (p3
.x
, p3
.y
);
1690 glVertex2f (p4
.x
, p4
.y
);
1694 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1695 glDisable (GL_LINE_STIPPLE
);
1698 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1701 struct slice
*slice1
;
1704 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1705 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1707 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
1708 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1709 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1713 int texindex
= state
.texindex
++ % state
.texcount
;
1715 if (state
.texowners
[texindex
].w
== tile
->w
) {
1716 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1720 state
.texowners
[texindex
].h
= slice
->h
;
1724 state
.texowners
[texindex
].h
= slice
->h
;
1727 state
.texowners
[texindex
].w
= tile
->w
;
1728 state
.texowners
[texindex
].slice
= slice
;
1729 slice
->texindex
= texindex
;
1731 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1733 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1741 tile
->pixmap
->samples
+offset
1745 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1753 tile
->pixmap
->samples
+offset
1759 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1761 CAMLparam2 (args_v
, ptr_v
);
1762 int dispx
= Int_val (Field (args_v
, 0));
1763 int dispy
= Int_val (Field (args_v
, 1));
1764 int dispw
= Int_val (Field (args_v
, 2));
1765 int disph
= Int_val (Field (args_v
, 3));
1766 int tilex
= Int_val (Field (args_v
, 4));
1767 int tiley
= Int_val (Field (args_v
, 5));
1768 char *s
= String_val (ptr_v
);
1769 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1771 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1773 int slicey
, firstslice
;
1774 struct slice
*slice
;
1776 firstslice
= tiley
/ tile
->sliceheight
;
1777 slice
= &tile
->slices
[firstslice
];
1778 slicey
= tiley
% tile
->sliceheight
;
1783 dh
= slice
->h
- slicey
;
1784 dh
= MIN (disph
, dh
);
1785 uploadslice (tile
, slice
);
1789 glTexCoord2i (tilex
, slicey
);
1790 glVertex2i (dispx
, dispy
);
1792 glTexCoord2i (tilex
+dispw
, slicey
);
1793 glVertex2i (dispx
+dispw
, dispy
);
1795 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1796 glVertex2i (dispx
+dispw
, dispy
+dh
);
1798 glTexCoord2i (tilex
, slicey
+dh
);
1799 glVertex2i (dispx
, dispy
+dh
);
1806 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1810 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1811 CAMLreturn (Val_unit
);
1814 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1815 value xoff_v
, value yoff_v
)
1817 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1818 int xoff
= Int_val (xoff_v
);
1819 int yoff
= Int_val (yoff_v
);
1820 char *s
= String_val (ptr_v
);
1821 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1823 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1825 if (trylock ("ml_postprocess")) {
1828 showsel (page
, xoff
, yoff
);
1829 unlock ("ml_postprocess");
1832 CAMLreturn (Val_unit
);
1835 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1839 fz_link
*link
, *links
;
1841 switch (page
->type
) {
1843 links
= page
->u
.pdfpage
->links
;
1847 links
= page
->u
.xpspage
->links
;
1856 ctm
= fz_concat (trimctm (page
->u
.pdfpage
, page
->pdimno
),
1857 state
.pagedims
[page
->pdimno
].ctm
);
1858 ctm
= fz_invert_matrix (ctm
);
1859 p
= fz_transform_point (ctm
, p
);
1861 for (link
= links
; link
; link
= link
->next
) {
1862 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1863 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1871 static void droptext (struct page
*page
)
1874 fz_free_text_span (state
.ctx
, page
->text
);
1877 page
->fmark
.span
= NULL
;
1878 page
->lmark
.span
= NULL
;
1883 static void ensuretext (struct page
*page
)
1885 if (state
.gen
!= page
->gen
) {
1887 page
->gen
= state
.gen
;
1892 page
->text
= fz_new_text_span (state
.ctx
);
1893 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
1894 fz_run_display_list (page
->dlist
,
1897 fz_infinite_bbox
, NULL
);
1898 fz_free_device (tdev
);
1902 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1904 CAMLparam3 (ptr_v
, x_v
, y_v
);
1905 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1909 char *s
= String_val (ptr_v
);
1910 int x
= Int_val (x_v
), y
= Int_val (y_v
);
1911 struct pagedim
*pdim
;
1913 ret_v
= Val_int (0);
1914 if (trylock ("ml_whatsunder")) {
1918 page
= parse_pointer ("ml_whatsunder", s
);
1919 pdim
= &state
.pagedims
[page
->pdimno
];
1920 link
= getlink (page
, x
, y
);
1922 switch (link
->dest
.kind
) {
1927 pageno
= link
->dest
.ld
.gotor
.page
;
1931 if (link
->dest
.ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1932 p
.y
= link
->dest
.ld
.gotor
.lt
.y
;
1933 p
= fz_transform_point (pdim
->lctm
, p
);
1935 tup_v
= caml_alloc_tuple (2);
1936 ret_v
= caml_alloc_small (1, 1);
1937 Field (tup_v
, 0) = Val_int (pageno
);
1938 Field (tup_v
, 1) = Val_int (p
.y
);
1939 Field (ret_v
, 0) = tup_v
;
1944 str_v
= caml_copy_string (link
->dest
.ld
.uri
.uri
);
1945 ret_v
= caml_alloc_small (1, 0);
1946 Field (ret_v
, 0) = str_v
;
1949 case FZ_LINK_LAUNCH
:
1950 str_v
= caml_copy_string (link
->dest
.ld
.launch
.file_spec
);
1951 ret_v
= caml_alloc_small (1, 4);
1952 Field (ret_v
, 0) = str_v
;
1956 str_v
= caml_copy_string (link
->dest
.ld
.named
.named
);
1957 ret_v
= caml_alloc_small (1, 5);
1958 Field (ret_v
, 0) = str_v
;
1962 str_v
= caml_copy_string (link
->dest
.ld
.gotor
.file_spec
);
1963 pageno
= link
->dest
.ld
.gotor
.page
;
1964 tup_v
= caml_alloc_tuple (2);
1965 ret_v
= caml_alloc_small (1, 6);
1966 Field (tup_v
, 0) = str_v
;
1967 Field (tup_v
, 1) = Val_int (pageno
);
1968 Field (ret_v
, 0) = tup_v
;
1975 snprintf (buf
, sizeof (buf
),
1976 "unhandled link kind %d", link
->dest
.kind
);
1977 str_v
= caml_copy_string (buf
);
1978 ret_v
= caml_alloc_small (1, 3);
1979 Field (ret_v
, 0) = str_v
;
1989 for (span
= page
->text
; span
; span
= span
->next
) {
1990 for (i
= 0; i
< span
->len
; ++i
) {
1992 b
= &span
->text
[i
].bbox
;
1993 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1995 span
->font
&& span
->font
->name
1997 : "Span has no font name"
1999 FT_FaceRec
*face
= span
->font
->ft_face
;
2000 if (face
&& face
->family_name
) {
2002 char *n1
= face
->family_name
;
2003 size_t l1
= strlen (n1
);
2004 size_t l2
= strlen (n2
);
2006 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2007 s
= malloc (l1
+ l2
+ 2);
2011 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2012 str_v
= caml_copy_string (s
);
2018 str_v
= caml_copy_string (n2
);
2020 ret_v
= caml_alloc_small (1, 2);
2021 Field (ret_v
, 0) = str_v
;
2028 unlock ("ml_whatsunder");
2034 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
2036 CAMLparam2 (ptr_v
, rect_v
);
2040 struct mark first
, last
;
2041 int i
, x0
, x1
, y0
, y1
;
2042 char *s
= String_val (ptr_v
);
2044 if (trylock ("ml_seltext")) {
2048 page
= parse_pointer ("ml_seltext", s
);
2051 x0
= Int_val (Field (rect_v
, 0));
2052 y0
= Int_val (Field (rect_v
, 1));
2053 x1
= Int_val (Field (rect_v
, 2));
2054 y1
= Int_val (Field (rect_v
, 3));
2057 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2058 glColor3ub (128, 128, 128);
2059 glRecti (x0
, y0
, x1
, y1
);
2060 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2066 last
.i
= first
.i
= 0;
2067 first
.span
= page
->text
;
2068 for (span
= page
->text
; span
; span
= span
->next
) {
2069 for (i
= 0; i
< span
->len
; ++i
) {
2070 b
= &span
->text
[i
].bbox
;
2073 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
2078 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
2083 if (0 && selected
) {
2084 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
2085 glColor3ub (128, 128, 128);
2086 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
2087 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
2092 if (y1
< y0
|| x1
< x0
) {
2095 if (first
.span
== last
.span
) {
2100 for (span
= first
.span
; span
&& span
!= last
.span
;
2101 span
= span
->next
) {
2114 first
.span
= last
.span
;
2120 page
->fmark
= first
;
2123 unlock ("ml_seltext");
2126 CAMLreturn (Val_unit
);
2129 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
2134 for (i
= a
; i
<= b
; ++i
) {
2135 len
= runetochar (buf
, &span
->text
[i
].c
);
2136 ret
= fwrite (buf
, len
, 1, f
);
2139 fprintf (stderr
, "failed to write %d bytes ret=%d: %s\n",
2140 len
, ret
, strerror (errno
));
2147 CAMLprim value
ml_copysel (value command_v
, value ptr_v
)
2153 char *s
= String_val (ptr_v
);
2154 char *command
= String_val (command_v
);
2156 if (trylock ("ml_copysel")) {
2160 page
= parse_pointer ("ml_sopysel", s
);
2162 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
2163 fprintf (stderr
, "nothing to copy\n");
2167 f
= popen (command
, "w");
2169 fprintf (stderr
, "failed to open sel pipe: %s\n",
2174 for (span
= page
->fmark
.span
;
2175 span
&& span
!= page
->lmark
.span
->next
;
2176 span
= span
->next
) {
2177 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
2178 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
2179 if (pipespan (f
, span
, a
, b
)) {
2183 if (putc ('\n', f
) == EOF
) {
2184 fprintf (stderr
, "failed break line on sel pipe: %s\n",
2190 page
->lmark
.span
= NULL
;
2191 page
->fmark
.span
= NULL
;
2195 int ret
= pclose (f
);
2197 if (errno
!= ECHILD
) {
2198 fprintf (stderr
, "failed to close sel pipe: %s\n",
2204 unlock ("ml_copysel");
2207 CAMLreturn (Val_unit
);
2210 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
2212 CAMLparam1 (pagedimno_v
);
2214 int pagedimno
= Int_val (pagedimno_v
);
2217 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2218 if (trylock ("ml_getpdimrect")) {
2219 box
= fz_empty_rect
;
2222 box
= state
.pagedims
[pagedimno
].mediabox
;
2223 unlock ("ml_getpdimrect");
2226 Store_double_field (ret_v
, 0, box
.x0
);
2227 Store_double_field (ret_v
, 1, box
.x1
);
2228 Store_double_field (ret_v
, 2, box
.y0
);
2229 Store_double_field (ret_v
, 3, box
.y1
);
2234 static double getmaxw (void)
2240 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2243 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2244 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2247 maxw
= MAX (w
, maxw
);
2252 CAMLprim value
ml_getmaxw (value unit_v
)
2254 CAMLparam1 (unit_v
);
2258 if (trylock ("ml_getmaxw")) {
2262 unlock ("ml_getmaxw");
2264 ret_v
= caml_copy_double (maxw
);
2268 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2270 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2274 double maxw
= 0.0, maxh
= 0.0;
2276 double winw
= Int_val (winw_v
);
2277 double winh
= Int_val (winh_v
);
2278 double dw
= Int_val (dw_v
);
2279 double pw
= 1.0, ph
= 1.0, num
, den
;
2281 if (trylock ("ml_zoom_for_height")) {
2285 if (state
.proportional
) {
2289 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2290 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2292 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2293 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2294 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2295 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2300 if (state
.proportional
) {
2302 scaledh
= h
* scale
;
2309 if (scaledh
> maxh
) {
2316 num
= (winh
* pw
) + (ph
* dw
);
2320 unlock ("ml_zoom_for_height");
2322 ret_v
= caml_copy_double (zoom
);
2328 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2330 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2332 int pt
= Int_val(pt_v
);
2333 int x
= Int_val (x_v
);
2334 int y
= Int_val (y_v
);
2337 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2338 ret_v
= caml_copy_double (w
);
2342 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2344 CAMLparam2 (pt_v
, string_v
);
2346 int pt
= Int_val (pt_v
);
2349 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2350 ret_v
= caml_copy_double (w
);
2354 CAMLprim value
ml_getpagebox (value opaque_v
)
2356 CAMLparam1 (opaque_v
);
2360 char *s
= String_val (opaque_v
);
2361 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2363 ret_v
= caml_alloc_tuple (4);
2364 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2365 dev
->hints
|= FZ_IGNORE_SHADE
;
2367 switch (page
->type
) {
2369 pdf_run_page (state
.u
.pdf
, page
->u
.pdfpage
, dev
, pagectm (page
), NULL
);
2373 xps_run_page (state
.u
.xps
, page
->u
.xpspage
, dev
, pagectm (page
), NULL
);
2377 bbox
= fz_infinite_bbox
;
2381 fz_free_device (dev
);
2382 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2383 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2384 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2385 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2390 CAMLprim value
ml_setaalevel (value level_v
)
2392 CAMLparam1 (level_v
);
2394 state
.aalevel
= Int_val (level_v
);
2395 CAMLreturn (Val_unit
);
2399 #include <X11/Xlib.h>
2405 GLXDrawable drawable
;
2408 #include "keysym2ucs.c"
2410 CAMLprim value
ml_keysymtoutf8 (value keysym_v
)
2412 CAMLparam1 (keysym_v
);
2414 KeySym keysym
= Int_val (keysym_v
);
2419 rune
= keysym2ucs (keysym
);
2420 len
= runetochar (buf
, &rune
);
2422 str_v
= caml_copy_string (buf
);
2426 CAMLprim value
ml_glx (value win_v
)
2430 XVisualInfo
*visual
;
2431 int attributes
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
2433 glx
.dpy
= XOpenDisplay (NULL
);
2435 caml_failwith ("XOpenDisplay failed");
2438 screen
= DefaultScreen (glx
.dpy
);
2439 visual
= glXChooseVisual (glx
.dpy
, screen
, attributes
);
2441 XCloseDisplay (glx
.dpy
);
2443 caml_failwith ("glXChooseVisual");
2446 glx
.ctx
= glXCreateContext (glx
.dpy
, visual
, NULL
, True
);
2448 XCloseDisplay (glx
.dpy
);
2451 caml_failwith ("glXCreateContext");
2455 if (!glXMakeCurrent (glx
.dpy
, Int_val (win_v
), glx
.ctx
)) {
2456 glXDestroyContext (glx
.dpy
, glx
.ctx
);
2457 XCloseDisplay (glx
.dpy
);
2460 caml_failwith ("glXMakeCurrent");
2462 glx
.drawable
= Int_val (win_v
);
2464 CAMLreturn (Val_unit
);
2467 CAMLprim value
ml_swapb (value unit_v
)
2469 CAMLparam1 (unit_v
);
2471 glXSwapBuffers (glx
.dpy
, glx
.drawable
);
2472 CAMLreturn (Val_unit
);
2475 enum { piunknown
, pilinux
, piosx
, pisun
, pifreebsd
,
2476 pidragonflybsd
, piopenbsd
, pinetbsd
, picygwin
};
2478 CAMLprim value
ml_platform (value unit_v
)
2480 CAMLparam1 (unit_v
);
2481 int platid
= piunknown
;
2483 #if defined __linux__
2485 #elif defined __CYGWIN__
2487 #elif defined __DragonFly__
2488 platid
= pidragonflybsd
;
2489 #elif defined __FreeBSD__
2491 #elif defined __OpenBSD__
2493 #elif defined __NetBSD__
2495 #elif defined __sun__
2497 #elif defined __APPLE__
2500 CAMLreturn (Val_int (platid
));
2503 CAMLprim value
ml_cloexec (value fd_v
)
2506 int fd
= Int_val (fd_v
);
2508 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
2509 uerror ("fcntl", Nothing
);
2511 CAMLreturn (Val_unit
);
2514 CAMLprim value
ml_init (value pipe_v
, value params_v
)
2516 CAMLparam2 (pipe_v
, params_v
);
2517 CAMLlocal2 (trim_v
, fuzz_v
);
2523 struct sigaction sa
;
2525 state
.cr
= Int_val (Field (pipe_v
, 0));
2526 state
.cw
= Int_val (Field (pipe_v
, 1));
2527 state
.rotate
= Int_val (Field (params_v
, 0));
2528 state
.proportional
= Bool_val (Field (params_v
, 1));
2529 trim_v
= Field (params_v
, 2);
2530 texcount
= Int_val (Field (params_v
, 3));
2531 state
.sliceheight
= Int_val (Field (params_v
, 4));
2532 mustoresize
= Int_val (Field (params_v
, 5));
2533 colorspace
= Int_val (Field (params_v
, 6));
2534 fontpath
= String_val (Field (params_v
, 7));
2536 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
2537 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2539 fuzz_v
= Field (trim_v
, 1);
2540 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2541 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2542 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2543 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2545 set_tex_params (colorspace
);
2548 state
.face
= load_font (fontpath
);
2552 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2554 state
.face
= load_builtin_font (base
, len
);
2556 if (!state
.face
) _exit (1);
2558 realloctexts (texcount
);
2560 sa
.sa_handler
= SIG_DFL
;
2561 if (sigemptyset (&sa
.sa_mask
)) {
2562 err (1, "sigemptyset");
2564 sa
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
| SA_NOCLDWAIT
;
2565 if (sigaction (SIGCHLD
, &sa
, NULL
)) {
2566 err (1, "sigaction");
2569 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2571 errx (1, "pthread_create: %s", strerror (ret
));
2574 CAMLreturn (Val_unit
);