1 /* lots of code c&p-ed directly from mupdf */
5 #pragma GCC diagnostic error "-Weverything"
6 #pragma GCC diagnostic ignored "-Wpadded"
7 #pragma GCC diagnostic ignored "-Wsign-conversion"
8 #pragma GCC diagnostic ignored "-Wdocumentation-unknown-command"
9 #pragma GCC diagnostic ignored "-Wdocumentation"
10 #pragma GCC diagnostic ignored "-Wdouble-promotion"
13 extern char **environ
;
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/utsname.h>
45 #include <OpenGL/gl.h>
50 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
54 #pragma GCC diagnostic ignored "-Wpedantic"
55 #define CAML_NAME_SPACE
56 #include <caml/fail.h>
57 #include <caml/alloc.h>
58 #include <caml/memory.h>
59 #include <caml/unixsupport.h>
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wfloat-equal"
63 #include <mupdf/fitz.h>
64 #include <mupdf/pdf.h>
65 #pragma GCC diagnostic pop
68 #include FT_FREETYPE_H
69 #pragma GCC diagnostic pop
74 #define TEXT_TYPE GL_TEXTURE_2D
76 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
80 #define lprintf printf
85 #define ARSERT(cond) for (;;) { \
87 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
103 struct slice slices
[1];
114 fz_matrix ctm
, zoomctm
, tctm
;
118 enum { SLINK
, SANNOT
} tag
;
139 fz_display_list
*dlist
;
142 struct slink
*slinks
;
144 struct annot
*annots
;
145 fz_stext_char
*fmark
, *lmark
;
148 enum { FitWidth
, FitProportional
, FitPage
};
152 struct pagedim
*pagedims
;
167 fz_colorspace
*colorspace
;
197 void (*glBindBufferARB
) (GLenum
, GLuint
);
198 GLboolean (*glUnmapBufferARB
) (GLenum
);
199 void *(*glMapBufferARB
) (GLenum
, GLenum
);
200 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
201 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
202 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
204 GLfloat texcoords
[8];
205 GLfloat vertices
[16];
216 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
218 static void lock (const char *cap
)
220 int ret
= pthread_mutex_lock (&mutex
);
222 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
226 static void unlock (const char *cap
)
228 int ret
= pthread_mutex_unlock (&mutex
);
230 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
234 static int trylock (const char *cap
)
236 int ret
= pthread_mutex_trylock (&mutex
);
237 if (ret
&& ret
!= EBUSY
) {
238 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
243 static int hasdata (void)
246 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
247 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
251 value
ml_hasdata (value fd_v
);
252 value
ml_hasdata (value fd_v
)
257 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
258 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
259 CAMLreturn (Val_bool (avail
> 0));
262 static void readdata (int fd
, void *p
, int size
)
267 n
= read (fd
, p
, size
);
269 if (n
< 0 && errno
== EINTR
) goto again
;
270 if (!n
) errx (1, "EOF while reading");
271 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
275 static void writedata (int fd
, char *p
, int size
)
278 uint32_t size4
= size
;
279 struct iovec iov
[2] = {
280 { .iov_base
= &size4
, .iov_len
= 4 },
281 { .iov_base
= p
, .iov_len
= size
}
285 n
= writev (fd
, iov
, 2);
286 if (n
< 0 && errno
== EINTR
) goto again
;
288 if (!n
) errx (1, "EOF while writing data");
289 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
293 static int readlen (int fd
)
296 readdata (fd
, &u
, 4);
300 void ml_wcmd (value fd_v
, value bytes_v
, value len_v
);
301 void ml_wcmd (value fd_v
, value bytes_v
, value len_v
)
303 CAMLparam3 (fd_v
, bytes_v
, len_v
);
304 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
308 value
ml_rcmd (value fd_v
);
309 value
ml_rcmd (value fd_v
)
312 CAMLlocal1 (strdata_v
);
313 int fd
= Int_val (fd_v
);
314 int len
= readlen (fd
);
315 strdata_v
= caml_alloc_string (len
);
316 readdata (fd
, String_val (strdata_v
), len
);
317 CAMLreturn (strdata_v
);
320 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
323 int size
= sizeof (fbuf
), len
;
329 len
= vsnprintf (buf
, size
, fmt
, ap
);
333 if (len
< size
- 4) {
334 writedata (state
.csock
, buf
, len
);
340 err (1, "vsnprintf for `%s' failed", fmt
);
342 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
343 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
345 if (buf
!= fbuf
) free (buf
);
348 static void closedoc (void)
351 fz_drop_document (state
.ctx
, state
.doc
);
356 static int openxref (char *filename
, char *password
, int layouth
)
358 for (int i
= 0; i
< state
.texcount
; ++i
) {
359 state
.texowners
[i
].w
= -1;
360 state
.texowners
[i
].slice
= NULL
;
366 if (state
.pagedims
) {
367 free (state
.pagedims
);
368 state
.pagedims
= NULL
;
370 state
.pagedimcount
= 0;
372 fz_set_aa_level (state
.ctx
, state
.aalevel
);
373 state
.doc
= fz_open_document (state
.ctx
, filename
);
374 if (fz_needs_password (state
.ctx
, state
.doc
)) {
375 if (password
&& !*password
) {
380 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
382 printd ("pass fail");
388 fz_layout_document (state
.ctx
, state
.doc
, 460, layouth
, 12);
389 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
393 static void docinfo (void)
395 struct { char *tag
; char *name
; } metatbl
[] = {
396 { FZ_META_INFO_TITLE
, "Title" },
397 { FZ_META_INFO_AUTHOR
, "Author" },
398 { FZ_META_FORMAT
, "Format" },
399 { FZ_META_ENCRYPTION
, "Encryption" },
400 { "info:Creator", "Creator" },
401 { "info:Producer", "Producer" },
402 { "info:CreationDate", "Creation date" },
407 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
410 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
411 metatbl
[i
].tag
, buf
, len
);
414 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
417 buf
= realloc (buf
, need
+ 1);
418 if (!buf
) err (1, "docinfo realloc %d", need
+ 1);
429 static void unlinktile (struct tile
*tile
)
431 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
432 struct slice
*s
= &tile
->slices
[i
];
434 if (s
->texindex
!= -1) {
435 if (state
.texowners
[s
->texindex
].slice
== s
) {
436 state
.texowners
[s
->texindex
].slice
= NULL
;
442 static void freepage (struct page
*page
)
446 fz_drop_stext_page (state
.ctx
, page
->text
);
451 fz_drop_display_list (state
.ctx
, page
->dlist
);
452 fz_drop_page (state
.ctx
, page
->fzpage
);
456 static void freetile (struct tile
*tile
)
461 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
462 #else /* piggyback */
464 fz_drop_pixmap (state
.ctx
, state
.pig
);
466 state
.pig
= tile
->pixmap
;
471 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
476 static void trimctm (pdf_page
*page
, int pindex
)
478 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
481 if (!pdim
->tctmready
) {
482 fz_rect realbox
, mediabox
;
483 fz_matrix page_ctm
, ctm
;
485 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
486 realbox
= fz_transform_rect (pdim
->mediabox
, ctm
);
487 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
488 pdim
->tctm
= fz_concat (
489 fz_invert_matrix (page_ctm
),
490 fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
)));
495 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
498 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
500 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
501 if (pdf_specifics (state
.ctx
, state
.doc
)) {
502 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
503 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
506 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
),
512 static fz_matrix
pagectm (struct page
*page
)
514 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
517 static void *loadpage (int pageno
, int pindex
)
522 page
= calloc (sizeof (struct page
), 1);
524 err (1, "calloc page %d", pageno
);
527 page
->dlist
= fz_new_display_list (state
.ctx
, fz_infinite_rect
);
528 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
530 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
531 fz_run_page (state
.ctx
, page
->fzpage
, dev
, fz_identity
, NULL
);
533 fz_catch (state
.ctx
) {
536 fz_close_device (state
.ctx
, dev
);
537 fz_drop_device (state
.ctx
, dev
);
539 page
->pdimno
= pindex
;
540 page
->pageno
= pageno
;
541 page
->sgen
= state
.gen
;
542 page
->agen
= state
.gen
;
543 page
->tgen
= state
.gen
;
547 static struct tile
*alloctile (int h
)
553 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
554 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
555 tile
= calloc (tilesize
, 1);
557 err (1, "cannot allocate tile (%zu bytes)", tilesize
);
559 for (int i
= 0; i
< slicecount
; ++i
) {
560 int sh
= fz_mini (h
, state
.sliceheight
);
561 tile
->slices
[i
].h
= sh
;
562 tile
->slices
[i
].texindex
= -1;
565 tile
->slicecount
= slicecount
;
566 tile
->sliceheight
= state
.sliceheight
;
570 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
577 struct pagedim
*pdim
;
579 tile
= alloctile (h
);
580 pdim
= &state
.pagedims
[page
->pdimno
];
585 bbox
.x1
= bbox
.x0
+ w
;
586 bbox
.y1
= bbox
.y0
+ h
;
589 if (state
.pig
->w
== w
591 && state
.pig
->colorspace
== state
.colorspace
) {
592 tile
->pixmap
= state
.pig
;
593 tile
->pixmap
->x
= bbox
.x0
;
594 tile
->pixmap
->y
= bbox
.y0
;
597 fz_drop_pixmap (state
.ctx
, state
.pig
);
604 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
605 bbox
, NULL
, 1, pbo
->ptr
);
610 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
,
617 fz_clear_pixmap_with_value (state
.ctx
, tile
->pixmap
, 0xff);
619 dev
= fz_new_draw_device (state
.ctx
, fz_identity
, tile
->pixmap
);
620 ctm
= pagectm (page
);
621 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, ctm
,
622 fz_rect_from_irect (bbox
), NULL
);
623 fz_close_device (state
.ctx
, dev
);
624 fz_drop_device (state
.ctx
, dev
);
629 static void initpdims (void)
633 fz_rect rootmediabox
= fz_empty_rect
;
634 int pageno
, trim
, show
;
635 int trimw
= 0, cxcount
;
636 fz_context
*ctx
= state
.ctx
;
637 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
644 if (state
.trimmargins
&& state
.trimcachepath
) {
645 trimf
= fopen (state
.trimcachepath
, "rb");
647 trimf
= fopen (state
.trimcachepath
, "wb");
652 cxcount
= state
.pagecount
;
655 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
656 "Root/Pages/MediaBox");
657 rootmediabox
= pdf_to_rect (ctx
, obj
);
658 pdf_load_page_tree (state
.ctx
, pdf
);
661 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
664 fz_rect mediabox
= fz_empty_rect
;
668 pdf_obj
*pageobj
= pdf_get_xref_entry (
669 state
.ctx
, pdf
, pdf
->rev_page_map
[pageno
].object
672 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
674 if (state
.trimmargins
) {
679 page
= pdf_load_page (ctx
, pdf
, pageno
);
680 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
681 trim
= state
.trimanew
|| !obj
;
685 fz_matrix ctm
, page_ctm
;
687 dev
= fz_new_bbox_device (ctx
, &rect
);
688 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
689 ctm
= fz_invert_matrix (page_ctm
);
690 pdf_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
691 fz_close_device (ctx
, dev
);
692 fz_drop_device (ctx
, dev
);
694 rect
.x0
+= state
.trimfuzz
.x0
;
695 rect
.x1
+= state
.trimfuzz
.x1
;
696 rect
.y0
+= state
.trimfuzz
.y0
;
697 rect
.y1
+= state
.trimfuzz
.y1
;
698 rect
= fz_transform_rect (rect
, ctm
);
699 rect
= fz_intersect_rect (rect
, mediabox
);
701 if (!fz_is_empty_rect (rect
)) {
705 obj
= pdf_new_array (ctx
, pdf
, 4);
706 pdf_array_push (ctx
, obj
,
707 pdf_new_real (ctx
, mediabox
.x0
));
708 pdf_array_push (ctx
, obj
,
709 pdf_new_real (ctx
, mediabox
.y0
));
710 pdf_array_push (ctx
, obj
,
711 pdf_new_real (ctx
, mediabox
.x1
));
712 pdf_array_push (ctx
, obj
,
713 pdf_new_real (ctx
, mediabox
.y1
));
714 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
717 mediabox
.x0
= pdf_to_real (ctx
,
718 pdf_array_get (ctx
, obj
, 0));
719 mediabox
.y0
= pdf_to_real (ctx
,
720 pdf_array_get (ctx
, obj
, 1));
721 mediabox
.x1
= pdf_to_real (ctx
,
722 pdf_array_get (ctx
, obj
, 2));
723 mediabox
.y1
= pdf_to_real (ctx
,
724 pdf_array_get (ctx
, obj
, 3));
727 fz_drop_page (ctx
, &page
->super
);
728 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
730 printd ("progress %f Trimming %d",
731 (double) (pageno
+ 1) / state
.pagecount
,
736 printd ("emsg failed to load page %d", pageno
);
744 pdf_to_rect (ctx
, pdf_dict_gets (ctx
, pageobj
, "MediaBox"));
745 if (fz_is_empty_rect (mediabox
)) {
754 pdf_to_rect (ctx
, pdf_dict_gets (ctx
, pageobj
, "CropBox"));
755 if (!fz_is_empty_rect (cropbox
)) {
760 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
765 if (fz_is_empty_rect (rootmediabox
)) {
766 printd ("emsg cannot find page size for page %d",
770 mediabox
= rootmediabox
;
777 if (state
.trimmargins
&& trimw
) {
781 page
= fz_load_page (ctx
, state
.doc
, pageno
);
782 mediabox
= fz_bound_page (ctx
, page
);
783 if (state
.trimmargins
) {
787 dev
= fz_new_bbox_device (ctx
, &rect
);
788 fz_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
789 fz_close_device (ctx
, dev
);
790 fz_drop_device (ctx
, dev
);
792 rect
.x0
+= state
.trimfuzz
.x0
;
793 rect
.x1
+= state
.trimfuzz
.x1
;
794 rect
.y0
+= state
.trimfuzz
.y0
;
795 rect
.y1
+= state
.trimfuzz
.y1
;
796 rect
= fz_intersect_rect (rect
, mediabox
);
798 if (!fz_is_empty_rect (rect
)) {
802 fz_drop_page (ctx
, page
);
807 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
809 err (1, "fwrite trim mediabox");
815 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
817 err (1, "fread trim mediabox %d", pageno
);
823 page
= fz_load_page (ctx
, state
.doc
, pageno
);
824 mediabox
= fz_bound_page (ctx
, page
);
825 fz_drop_page (ctx
, page
);
827 show
= !state
.trimmargins
&& pageno
% 20 == 0;
829 printd ("progress %f Gathering dimensions %d",
830 (double) (pageno
) / state
.pagecount
,
835 printd ("emsg failed to load page %d", pageno
);
841 if (state
.pagedimcount
== 0
842 || ((void) (p
= &state
.pagedims
[state
.pagedimcount
-1])
843 , p
->rotate
!= rotate
)
844 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
847 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
848 state
.pagedims
= realloc (state
.pagedims
, size
);
849 if (!state
.pagedims
) {
850 err (1, "realloc pagedims to %zu (%d elems)",
851 size
, state
.pagedimcount
+ 1);
854 p
= &state
.pagedims
[state
.pagedimcount
++];
856 p
->mediabox
= mediabox
;
861 printd ("progress 1 %s %d pages in %f seconds",
862 state
.trimmargins
? "Trimmed" : "Processed",
863 state
.pagecount
, end
- start
);
866 if (fclose (trimf
)) {
872 static void layout (void)
877 struct pagedim
*p
= NULL
;
878 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
880 if (state
.pagedimcount
== 0) return;
882 switch (state
.fitmodel
) {
883 case FitProportional
:
884 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
887 p
= &state
.pagedims
[pindex
];
888 box
= fz_transform_rect (p
->mediabox
,
889 fz_rotate (p
->rotate
+ state
.rotate
));
891 x0
= fz_min (box
.x0
, box
.x1
);
892 x1
= fz_max (box
.x0
, box
.x1
);
895 maxw
= fz_max (w
, maxw
);
896 zoom
= state
.w
/ maxw
;
908 ARSERT (0 && state
.fitmodel
);
911 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
912 p
= &state
.pagedims
[pindex
];
913 ctm
= fz_rotate (state
.rotate
);
914 box
= fz_transform_rect (p
->mediabox
,
915 fz_rotate (p
->rotate
+ state
.rotate
));
917 switch (state
.fitmodel
) {
918 case FitProportional
:
919 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
927 zoom
= fz_min (zw
, zh
);
928 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
937 p
->zoomctm
= fz_scale (zoom
, zoom
);
938 ctm
= fz_concat (p
->zoomctm
, ctm
);
940 p
->pagebox
= p
->mediabox
;
941 p
->pagebox
= fz_transform_rect (p
->pagebox
, fz_rotate (p
->rotate
));
942 p
->pagebox
.x1
-= p
->pagebox
.x0
;
943 p
->pagebox
.y1
-= p
->pagebox
.y0
;
946 p
->bounds
= fz_round_rect (fz_transform_rect (p
->pagebox
, ctm
));
949 ctm
= fz_concat (fz_translate (0, -p
->mediabox
.y1
),
950 fz_scale (zoom
, -zoom
));
955 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
956 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
957 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
958 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
959 int boundw
= x1
- x0
;
960 int boundh
= y1
- y0
;
962 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
963 } while (p
-- != state
.pagedims
);
966 static struct pagedim
*pdimofpageno (int pageno
)
968 struct pagedim
*pdim
= state
.pagedims
;
970 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
971 if (state
.pagedims
[i
].pageno
> pageno
)
973 pdim
= &state
.pagedims
[i
];
978 static void recurse_outline (fz_outline
*outline
, int level
)
981 if (outline
->page
>= 0) {
982 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
983 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
984 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
985 p
= fz_transform_point (p
, pdim
->ctm
);
986 printd ("o %d %d %d %d %s",
987 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
990 printd ("on %d %s", level
, outline
->title
);
993 recurse_outline (outline
->down
, level
+ 1);
995 outline
= outline
->next
;
999 static void process_outline (void)
1001 fz_outline
*outline
;
1003 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1005 state
.needoutline
= 0;
1006 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1008 recurse_outline (outline
, 0);
1009 fz_drop_outline (state
.ctx
, outline
);
1013 static char *strofline (fz_stext_line
*line
)
1018 size_t size
= 0, cap
= 80;
1020 p
= malloc (cap
+ 1);
1021 if (!p
) return NULL
;
1023 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1024 int n
= fz_runetochar (utf8
, ch
->c
);
1025 if (size
+ n
> cap
) {
1027 p
= realloc (p
, cap
+ 1);
1028 if (!p
) return NULL
;
1031 memcpy (p
+ size
, utf8
, n
);
1038 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1039 int stop
, int pageno
, double start
)
1045 p
= strofline (line
);
1048 ret
= regexec (re
, p
, 1, &rm
, 0);
1051 if (ret
!= REG_NOMATCH
) {
1054 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1055 printd ("msg regexec error `%.*s'",
1056 (int) size
, errbuf
);
1066 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1067 o
+= fz_runelen (ch
->c
);
1073 for (;ch
; ch
= ch
->next
) {
1074 o
+= fz_runelen (ch
->c
);
1075 if (o
> rm
.rm_eo
) break;
1080 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1087 printd ("progress 1 found at %d `%.*s' in %f sec",
1088 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1092 printd ("match %d %d %f %f %f %f %f %f %f %f",
1104 /* wishful thinking function */
1105 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1108 fz_stext_page
*text
;
1109 struct pagedim
*pdim
;
1110 int stop
= 0, niters
= 0;
1113 fz_stext_block
*block
;
1116 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1117 if (niters
++ == 5) {
1120 printd ("progress 1 attention requested aborting search at %d",
1125 printd ("progress %f searching in page %d",
1126 (double) (pageno
+ 1) / state
.pagecount
,
1130 pdim
= pdimofpageno (pageno
);
1131 text
= fz_new_stext_page (state
.ctx
, pdim
->mediabox
);
1132 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1134 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1135 fz_run_page (state
.ctx
, page
, tdev
, pagectm1 (page
, pdim
), NULL
);
1137 fz_close_device (state
.ctx
, tdev
);
1138 fz_drop_device (state
.ctx
, tdev
);
1141 for (block
= text
->first_block
; block
; block
= block
->next
) {
1142 fz_stext_line
*line
;
1144 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1145 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1146 if (line
->bbox
.y0
< y
+ 1) continue;
1148 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1150 case 1: stop
= 1; break;
1151 case -1: stop
= 1; goto endloop
;
1157 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1158 fz_stext_line
*line
;
1160 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1161 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1162 if (line
->bbox
.y0
< y
+ 1) continue;
1164 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1166 case 1: stop
= 1; break;
1167 case -1: stop
= 1; goto endloop
;
1182 fz_drop_stext_page (state
.ctx
, text
);
1183 fz_drop_page (state
.ctx
, page
);
1187 printd ("progress 1 no matches %f sec", end
- start
);
1189 printd ("clearrects");
1192 static void set_tex_params (int colorspace
)
1194 switch (colorspace
) {
1196 state
.texiform
= GL_RGBA8
;
1197 state
.texform
= GL_RGBA
;
1198 state
.texty
= GL_UNSIGNED_BYTE
;
1199 state
.colorspace
= fz_device_rgb (state
.ctx
);
1202 state
.texiform
= GL_LUMINANCE_ALPHA
;
1203 state
.texform
= GL_LUMINANCE_ALPHA
;
1204 state
.texty
= GL_UNSIGNED_BYTE
;
1205 state
.colorspace
= fz_device_gray (state
.ctx
);
1208 errx (1, "invalid colorspce %d", colorspace
);
1212 static void realloctexts (int texcount
)
1216 if (texcount
== state
.texcount
) return;
1218 if (texcount
< state
.texcount
) {
1219 glDeleteTextures (state
.texcount
- texcount
,
1220 state
.texids
+ texcount
);
1223 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1224 state
.texids
= realloc (state
.texids
, size
);
1225 if (!state
.texids
) {
1226 err (1, "realloc texs %zu", size
);
1229 state
.texowners
= (void *) (state
.texids
+ texcount
);
1230 if (texcount
> state
.texcount
) {
1231 glGenTextures (texcount
- state
.texcount
,
1232 state
.texids
+ state
.texcount
);
1233 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1234 state
.texowners
[i
].w
= -1;
1235 state
.texowners
[i
].slice
= NULL
;
1238 state
.texcount
= texcount
;
1242 static char *mbtoutf8 (char *s
)
1252 len
= mbstowcs (NULL
, s
, strlen (s
));
1253 if (len
== 0 || len
== (size_t) -1) {
1255 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1259 tmp
= calloc (len
, sizeof (wchar_t));
1261 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1262 len
, sizeof (wchar_t), errno
, strerror (errno
));
1266 ret
= mbstowcs (tmp
, s
, len
);
1267 if (ret
== (size_t) -1) {
1268 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1269 len
, errno
, strerror (errno
));
1275 for (i
= 0; i
< ret
; ++i
) {
1276 len
+= fz_runelen (tmp
[i
]);
1279 p
= r
= malloc (len
+ 1);
1281 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1286 for (i
= 0; i
< ret
; ++i
) {
1287 p
+= fz_runetochar (p
, tmp
[i
]);
1294 value
ml_mbtoutf8 (value s_v
);
1295 value
ml_mbtoutf8 (value s_v
)
1301 s
= String_val (s_v
);
1307 ret_v
= caml_copy_string (r
);
1313 static void * mainloop (void UNUSED_ATTR
*unused
)
1316 int len
, ret
, oldlen
= 0;
1321 len
= readlen (state
.csock
);
1323 errx (1, "readlen returned 0");
1326 if (oldlen
< len
+ 1) {
1327 p
= realloc (p
, len
+ 1);
1329 err (1, "realloc %d failed", len
+ 1);
1333 readdata (state
.csock
, p
, len
);
1336 if (!strncmp ("open", p
, 4)) {
1337 int off
, usedoccss
, ok
= 0, layouth
;
1344 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1346 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1349 filename
= p
+ 5 + off
;
1350 filenamelen
= strlen (filename
);
1351 password
= filename
+ filenamelen
+ 1;
1353 if (password
[strlen (password
) + 1]) {
1354 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1358 fz_set_use_document_css (state
.ctx
, usedoccss
);
1359 fz_try (state
.ctx
) {
1360 ok
= openxref (filename
, password
, layouth
);
1362 fz_catch (state
.ctx
) {
1363 utf8filename
= mbtoutf8 (filename
);
1364 printd ("msg Could not open %s", utf8filename
);
1365 if (utf8filename
!= filename
) {
1366 free (utf8filename
);
1376 utf8filename
= mbtoutf8 (filename
);
1377 printd ("msg Opened %s (press h/F1 to get help)", utf8filename
);
1378 if (utf8filename
!= filename
) {
1379 free (utf8filename
);
1381 state
.needoutline
= 1;
1384 else if (!strncmp ("cs", p
, 2)) {
1387 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1389 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1392 set_tex_params (colorspace
);
1393 for (i
= 0; i
< state
.texcount
; ++i
) {
1394 state
.texowners
[i
].w
= -1;
1395 state
.texowners
[i
].slice
= NULL
;
1399 else if (!strncmp ("freepage", p
, 8)) {
1402 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1404 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1408 unlock ("freepage");
1410 else if (!strncmp ("freetile", p
, 8)) {
1413 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1415 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1419 unlock ("freetile");
1421 else if (!strncmp ("search", p
, 6)) {
1422 int icase
, pageno
, y
, len2
, forward
;
1426 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1427 &icase
, &pageno
, &y
, &forward
, &len2
);
1429 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1432 pattern
= p
+ 6 + len2
;
1433 ret
= regcomp (&re
, pattern
,
1434 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1439 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1440 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1444 search (&re
, pageno
, y
, forward
);
1449 else if (!strncmp ("geometry", p
, 8)) {
1453 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1455 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1462 for (int i
= 0; i
< state
.texcount
; ++i
) {
1463 state
.texowners
[i
].slice
= NULL
;
1466 state
.fitmodel
= fitmodel
;
1471 unlock ("geometry");
1472 printd ("continue %d", state
.pagecount
);
1474 else if (!strncmp ("reqlayout", p
, 9)) {
1481 ret
= sscanf (p
+ 9, " %d %d %d %n",
1482 &rotate
, &fitmodel
, &h
, &off
);
1484 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1487 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1488 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1491 state
.rotate
= rotate
;
1492 state
.fitmodel
= fitmodel
;
1497 nameddest
= p
+ 9 + off
;
1498 if (pdf
&& nameddest
&& *nameddest
) {
1500 struct pagedim
*pdim
;
1501 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1503 pdim
= pdimofpageno (pageno
);
1504 xy
= fz_transform_point (xy
, pdim
->ctm
);
1505 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1509 unlock ("reqlayout");
1510 printd ("continue %d", state
.pagecount
);
1512 else if (!strncmp ("page", p
, 4)) {
1517 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1519 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1524 page
= loadpage (pageno
, pindex
);
1528 printd ("page %" PRIxPTR
" %f", (uintptr_t) page
, b
- a
);
1530 else if (!strncmp ("tile", p
, 4)) {
1537 ret
= sscanf (p
+ 4, " %" SCNxPTR
" %d %d %d %d %" SCNxPTR
,
1538 (uintptr_t *) &page
, &x
, &y
, &w
, &h
,
1539 (uintptr_t *) &data
);
1541 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1546 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1550 printd ("tile %d %d %" PRIxPTR
" %u %f",
1551 x
, y
, (uintptr_t) (tile
),
1552 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1555 else if (!strncmp ("trimset", p
, 7)) {
1559 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1560 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1562 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1565 state
.trimmargins
= trimmargins
;
1566 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1568 state
.trimfuzz
= fuzz
;
1572 else if (!strncmp ("settrim", p
, 7)) {
1576 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1577 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1579 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1583 state
.trimmargins
= trimmargins
;
1584 state
.needoutline
= 1;
1585 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1587 state
.trimfuzz
= fuzz
;
1589 state
.pagedimcount
= 0;
1590 free (state
.pagedims
);
1591 state
.pagedims
= NULL
;
1596 printd ("continue %d", state
.pagecount
);
1598 else if (!strncmp ("sliceh", p
, 6)) {
1601 ret
= sscanf (p
+ 6, " %d", &h
);
1603 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1605 if (h
!= state
.sliceheight
) {
1606 state
.sliceheight
= h
;
1607 for (int i
= 0; i
< state
.texcount
; ++i
) {
1608 state
.texowners
[i
].w
= -1;
1609 state
.texowners
[i
].h
= -1;
1610 state
.texowners
[i
].slice
= NULL
;
1614 else if (!strncmp ("interrupt", p
, 9)) {
1615 printd ("vmsg interrupted");
1618 errx (1, "unknown command %.*s", len
, p
);
1624 value
ml_isexternallink (value uri_v
);
1625 value
ml_isexternallink (value uri_v
)
1628 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1629 CAMLreturn (Val_bool (ext
));
1632 value
ml_uritolocation (value uri_v
);
1633 value
ml_uritolocation (value uri_v
)
1639 struct pagedim
*pdim
;
1641 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1643 pdim
= pdimofpageno (pageno
);
1644 xy
= fz_transform_point (xy
, pdim
->ctm
);
1645 ret_v
= caml_alloc_tuple (3);
1646 Field (ret_v
, 0) = Val_int (pageno
);
1647 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1648 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1652 value
ml_realloctexts (value texcount_v
);
1653 value
ml_realloctexts (value texcount_v
)
1655 CAMLparam1 (texcount_v
);
1658 if (trylock (__func__
)) {
1662 realloctexts (Int_val (texcount_v
));
1667 CAMLreturn (Val_bool (ok
));
1670 static void recti (int x0
, int y0
, int x1
, int y1
)
1672 GLfloat
*v
= state
.vertices
;
1674 glVertexPointer (2, GL_FLOAT
, 0, v
);
1675 v
[0] = x0
; v
[1] = y0
;
1676 v
[2] = x1
; v
[3] = y0
;
1677 v
[4] = x0
; v
[5] = y1
;
1678 v
[6] = x1
; v
[7] = y1
;
1679 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1682 static void showsel (struct page
*page
, int ox
, int oy
)
1686 fz_stext_block
*block
;
1688 unsigned char selcolor
[] = {15,15,15,140};
1690 if (!page
->fmark
|| !page
->lmark
) return;
1692 glEnable (GL_BLEND
);
1693 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1694 glColor4ubv (selcolor
);
1696 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1697 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1699 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1700 fz_stext_line
*line
;
1702 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1703 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1706 rect
= fz_empty_rect
;
1707 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1709 if (ch
== page
->fmark
) seen
= 1;
1710 r
= fz_rect_from_quad (ch
->quad
);
1711 if (seen
) rect
= fz_union_rect (rect
, r
);
1712 if (ch
== page
->lmark
) {
1713 bbox
= fz_round_rect (rect
);
1714 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1715 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1719 bbox
= fz_round_rect (rect
);
1720 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1721 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1725 glDisable (GL_BLEND
);
1728 #pragma GCC diagnostic push
1729 #pragma GCC diagnostic ignored "-Wconversion"
1731 #pragma GCC diagnostic pop
1733 static void stipplerect (fz_matrix m
,
1741 p1
= fz_transform_point (p1
, m
);
1742 p2
= fz_transform_point (p2
, m
);
1743 p3
= fz_transform_point (p3
, m
);
1744 p4
= fz_transform_point (p4
, m
);
1750 t
= hypotf (w
, h
) * .25f
;
1754 s
= hypotf (w
, h
) * .25f
;
1756 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1757 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1759 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
1760 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
1762 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
1763 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
1765 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
1766 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
1768 glDrawArrays (GL_LINES
, 0, 8);
1771 static void solidrect (fz_matrix m
,
1778 p1
= fz_transform_point (p1
, m
);
1779 p2
= fz_transform_point (p2
, m
);
1780 p3
= fz_transform_point (p3
, m
);
1781 p4
= fz_transform_point (p4
, m
);
1782 vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1783 vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1785 vertices
[4] = p3
.x
; vertices
[5] = p3
.y
;
1786 vertices
[6] = p4
.x
; vertices
[7] = p4
.y
;
1787 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
1790 static void ensurelinks (struct page
*page
)
1793 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
1796 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1800 GLfloat
*texcoords
= state
.texcoords
;
1801 GLfloat
*vertices
= state
.vertices
;
1805 glEnable (GL_TEXTURE_1D
);
1806 glEnable (GL_BLEND
);
1807 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1808 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
1810 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1811 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1812 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1814 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
1815 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
1817 for (link
= page
->links
; link
; link
= link
->next
) {
1818 fz_point p1
, p2
, p3
, p4
;
1820 p1
.x
= link
->rect
.x0
;
1821 p1
.y
= link
->rect
.y0
;
1823 p2
.x
= link
->rect
.x1
;
1824 p2
.y
= link
->rect
.y0
;
1826 p3
.x
= link
->rect
.x1
;
1827 p3
.y
= link
->rect
.y1
;
1829 p4
.x
= link
->rect
.x0
;
1830 p4
.y
= link
->rect
.y1
;
1832 /* TODO: different colours for different schemes */
1833 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
1834 else glColor3ub (255, 0, 0);
1836 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1839 for (int i
= 0; i
< page
->annotcount
; ++i
) {
1840 fz_point p1
, p2
, p3
, p4
;
1841 struct annot
*annot
= &page
->annots
[i
];
1843 p1
.x
= annot
->bbox
.x0
;
1844 p1
.y
= annot
->bbox
.y0
;
1846 p2
.x
= annot
->bbox
.x1
;
1847 p2
.y
= annot
->bbox
.y0
;
1849 p3
.x
= annot
->bbox
.x1
;
1850 p3
.y
= annot
->bbox
.y1
;
1852 p4
.x
= annot
->bbox
.x0
;
1853 p4
.y
= annot
->bbox
.y1
;
1855 glColor3ub (0, 0, 128);
1856 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1859 glDisable (GL_BLEND
);
1860 glDisable (GL_TEXTURE_1D
);
1863 static int compareslinks (const void *l
, const void *r
)
1865 struct slink
const *ls
= l
;
1866 struct slink
const *rs
= r
;
1867 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1868 return rs
->bbox
.x0
- rs
->bbox
.x0
;
1870 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1873 static void droptext (struct page
*page
)
1876 fz_drop_stext_page (state
.ctx
, page
->text
);
1883 static void dropannots (struct page
*page
)
1886 free (page
->annots
);
1887 page
->annots
= NULL
;
1888 page
->annotcount
= 0;
1892 static void ensureannots (struct page
*page
)
1895 size_t annotsize
= sizeof (*page
->annots
);
1900 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1903 pdfpage
= pdf_page_from_fz_page (state
.ctx
, page
->fzpage
);
1904 if (state
.gen
!= page
->agen
) {
1906 page
->agen
= state
.gen
;
1908 if (page
->annots
) return;
1910 for (annot
= pdf_first_annot (state
.ctx
, pdfpage
);
1912 annot
= pdf_next_annot (state
.ctx
, annot
)) {
1917 page
->annotcount
= count
;
1918 page
->annots
= calloc (count
, annotsize
);
1919 if (!page
->annots
) {
1920 err (1, "calloc annots %d", count
);
1923 for (annot
= pdf_first_annot (state
.ctx
, pdfpage
), i
= 0;
1925 annot
= pdf_next_annot (state
.ctx
, annot
), i
++) {
1928 rect
= pdf_bound_annot (state
.ctx
, annot
);
1929 page
->annots
[i
].annot
= annot
;
1930 page
->annots
[i
].bbox
= fz_round_rect (rect
);
1935 static void dropslinks (struct page
*page
)
1938 free (page
->slinks
);
1939 page
->slinks
= NULL
;
1940 page
->slinkcount
= 0;
1943 fz_drop_link (state
.ctx
, page
->links
);
1948 static void ensureslinks (struct page
*page
)
1952 size_t slinksize
= sizeof (*page
->slinks
);
1955 ensureannots (page
);
1956 if (state
.gen
!= page
->sgen
) {
1958 page
->sgen
= state
.gen
;
1960 if (page
->slinks
) return;
1963 ctm
= pagectm (page
);
1965 count
= page
->annotcount
;
1966 for (link
= page
->links
; link
; link
= link
->next
) {
1972 page
->slinkcount
= count
;
1973 page
->slinks
= calloc (count
, slinksize
);
1974 if (!page
->slinks
) {
1975 err (1, "calloc slinks %d", count
);
1978 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
1982 rect
= fz_transform_rect (rect
, ctm
);
1983 page
->slinks
[i
].tag
= SLINK
;
1984 page
->slinks
[i
].u
.link
= link
;
1985 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
1987 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
1989 rect
= pdf_bound_annot (state
.ctx
, page
->annots
[j
].annot
);
1990 rect
= fz_transform_rect (rect
, ctm
);
1991 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
1993 page
->slinks
[i
].tag
= SANNOT
;
1994 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
1996 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
2000 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
2001 int noff
, char *targ
, mlsize_t tlen
, int hfsize
)
2004 struct slink
*slink
;
2005 float x0
, y0
, x1
, y1
, w
;
2007 ensureslinks (page
);
2008 glColor3ub (0xc3, 0xb0, 0x91);
2009 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2010 fmt_linkn (buf
, i
+ noff
);
2011 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2012 slink
= &page
->slinks
[i
];
2014 x0
= slink
->bbox
.x0
+ xoff
- 5;
2015 y1
= slink
->bbox
.y0
+ yoff
- 5;
2016 y0
= y1
+ 10 + hfsize
;
2017 w
= measure_string (state
.face
, hfsize
, buf
);
2019 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2023 glEnable (GL_BLEND
);
2024 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2025 glEnable (GL_TEXTURE_2D
);
2026 glColor3ub (0, 0, 0);
2027 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2028 fmt_linkn (buf
, i
+ noff
);
2029 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2030 slink
= &page
->slinks
[i
];
2032 x0
= slink
->bbox
.x0
+ xoff
;
2033 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2034 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2037 glDisable (GL_TEXTURE_2D
);
2038 glDisable (GL_BLEND
);
2041 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2044 struct slice
*slice1
;
2045 unsigned char *texdata
;
2048 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2049 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2051 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2052 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2053 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2057 int texindex
= state
.texindex
++ % state
.texcount
;
2059 if (state
.texowners
[texindex
].w
== tile
->w
) {
2060 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2064 state
.texowners
[texindex
].h
= slice
->h
;
2068 state
.texowners
[texindex
].h
= slice
->h
;
2071 state
.texowners
[texindex
].w
= tile
->w
;
2072 state
.texowners
[texindex
].slice
= slice
;
2073 slice
->texindex
= texindex
;
2075 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2076 #if TEXT_TYPE == GL_TEXTURE_2D
2077 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2078 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2079 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2080 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2083 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2087 texdata
= tile
->pixmap
->samples
;
2090 glTexSubImage2D (TEXT_TYPE
,
2102 glTexImage2D (TEXT_TYPE
,
2114 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2119 void ml_begintiles (void);
2120 void ml_begintiles (void)
2122 glEnable (TEXT_TYPE
);
2123 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2124 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2127 void ml_endtiles (void);
2128 void ml_endtiles (void)
2130 glDisable (TEXT_TYPE
);
2133 void ml_drawtile (value args_v
, value ptr_v
);
2134 void ml_drawtile (value args_v
, value ptr_v
)
2136 CAMLparam2 (args_v
, ptr_v
);
2137 int dispx
= Int_val (Field (args_v
, 0));
2138 int dispy
= Int_val (Field (args_v
, 1));
2139 int dispw
= Int_val (Field (args_v
, 2));
2140 int disph
= Int_val (Field (args_v
, 3));
2141 int tilex
= Int_val (Field (args_v
, 4));
2142 int tiley
= Int_val (Field (args_v
, 5));
2143 char *s
= String_val (ptr_v
);
2144 struct tile
*tile
= parse_pointer (__func__
, s
);
2145 int slicey
, firstslice
;
2146 struct slice
*slice
;
2147 GLfloat
*texcoords
= state
.texcoords
;
2148 GLfloat
*vertices
= state
.vertices
;
2150 firstslice
= tiley
/ tile
->sliceheight
;
2151 slice
= &tile
->slices
[firstslice
];
2152 slicey
= tiley
% tile
->sliceheight
;
2157 dh
= slice
->h
- slicey
;
2158 dh
= fz_mini (disph
, dh
);
2159 uploadslice (tile
, slice
);
2161 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2162 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2163 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2164 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2166 vertices
[0] = dispx
; vertices
[1] = dispy
;
2167 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2168 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2169 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2171 #if TEXT_TYPE == GL_TEXTURE_2D
2172 for (int i
= 0; i
< 8; ++i
) {
2173 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2177 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2181 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2187 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2190 fz_point p1
, p2
, p3
, p4
;
2191 GLfloat
*vertices
= state
.vertices
;
2193 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2194 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2195 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
2197 glEnable (GL_BLEND
);
2198 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2201 Double_array_field (rects_v
, 0),
2202 Double_array_field (rects_v
, 1),
2203 Double_array_field (rects_v
, 2),
2204 Double_array_field (rects_v
, 3)
2206 p1
.x
= (float) Double_array_field (rects_v
, 4);
2207 p1
.y
= (float) Double_array_field (rects_v
, 5);
2209 p2
.x
= (float) Double_array_field (rects_v
, 6);
2213 p3
.y
= (float) Double_array_field (rects_v
, 7);
2217 solidrect (ctm
, p1
, p2
, p3
, p4
, vertices
);
2218 glDisable (GL_BLEND
);
2221 value
ml_postprocess (value ptr_v
, value hlinks_v
,
2222 value xoff_v
, value yoff_v
,
2224 value
ml_postprocess (value ptr_v
, value hlinks_v
,
2225 value xoff_v
, value yoff_v
,
2228 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2229 int xoff
= Int_val (xoff_v
);
2230 int yoff
= Int_val (yoff_v
);
2231 int noff
= Int_val (Field (li_v
, 0));
2232 char *targ
= String_val (Field (li_v
, 1));
2233 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2234 int hfsize
= Int_val (Field (li_v
, 2));
2235 char *s
= String_val (ptr_v
);
2236 int hlmask
= Int_val (hlinks_v
);
2237 struct page
*page
= parse_pointer (__func__
, s
);
2239 if (!page
->fzpage
) {
2240 /* deal with loadpage failed pages */
2244 if (trylock (__func__
)) {
2249 ensureannots (page
);
2250 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2252 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2253 noff
= page
->slinkcount
;
2255 if (page
->tgen
== state
.gen
) {
2256 showsel (page
, xoff
, yoff
);
2261 CAMLreturn (Val_int (noff
));
2264 void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
, value rects_v
);
2265 void ml_drawprect (value ptr_v
, value xoff_v
, value yoff_v
, value rects_v
)
2267 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2268 int xoff
= Int_val (xoff_v
);
2269 int yoff
= Int_val (yoff_v
);
2270 char *s
= String_val (ptr_v
);
2271 struct page
*page
= parse_pointer (__func__
, s
);
2273 drawprect (page
, xoff
, yoff
, rects_v
);
2277 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2281 const fz_matrix
*tctm
;
2282 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2284 if (!page
->annots
) return NULL
;
2287 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2288 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2291 tctm
= &fz_identity
;
2297 ctm
= fz_concat (*tctm
, state
.pagedims
[page
->pdimno
].ctm
);
2298 ctm
= fz_invert_matrix (ctm
);
2299 p
= fz_transform_point (p
, ctm
);
2302 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2303 struct annot
*a
= &page
->annots
[i
];
2306 rect
= pdf_bound_annot (state
.ctx
, a
->annot
);
2307 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2308 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2316 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2321 ensureslinks (page
);
2326 p
= fz_transform_point (p
, fz_invert_matrix (pagectm (page
)));
2328 for (link
= page
->links
; link
; link
= link
->next
) {
2329 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2330 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2338 static void ensuretext (struct page
*page
)
2340 if (state
.gen
!= page
->tgen
) {
2342 page
->tgen
= state
.gen
;
2347 page
->text
= fz_new_stext_page (state
.ctx
,
2348 state
.pagedims
[page
->pdimno
].mediabox
);
2349 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2350 fz_run_display_list (state
.ctx
, page
->dlist
,
2351 tdev
, pagectm (page
), fz_infinite_rect
, NULL
);
2352 fz_close_device (state
.ctx
, tdev
);
2353 fz_drop_device (state
.ctx
, tdev
);
2357 value
ml_find_page_with_links (value start_page_v
, value dir_v
);
2358 value
ml_find_page_with_links (value start_page_v
, value dir_v
)
2360 CAMLparam2 (start_page_v
, dir_v
);
2362 int i
, dir
= Int_val (dir_v
);
2363 int start_page
= Int_val (start_page_v
);
2364 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2368 ret_v
= Val_int (0);
2370 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2371 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2376 pdf_page
*page
= NULL
;
2379 fz_try (state
.ctx
) {
2380 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2381 found
= !!page
->links
|| !!page
->annots
;
2383 fz_catch (state
.ctx
) {
2387 fz_drop_page (state
.ctx
, &page
->super
);
2391 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2392 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2394 fz_drop_link (state
.ctx
, link
);
2395 fz_drop_page (state
.ctx
, page
);
2399 ret_v
= caml_alloc_small (1, 1);
2400 Field (ret_v
, 0) = Val_int (i
);
2409 enum { dir_first
, dir_last
};
2410 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2412 value
ml_findlink (value ptr_v
, value dir_v
);
2413 value
ml_findlink (value ptr_v
, value dir_v
)
2415 CAMLparam2 (ptr_v
, dir_v
);
2416 CAMLlocal2 (ret_v
, pos_v
);
2418 int dirtag
, i
, slinkindex
;
2419 struct slink
*found
= NULL
,*slink
;
2420 char *s
= String_val (ptr_v
);
2422 page
= parse_pointer (__func__
, s
);
2423 ret_v
= Val_int (0);
2425 ensureslinks (page
);
2427 if (Is_block (dir_v
)) {
2428 dirtag
= Tag_val (dir_v
);
2430 case dir_first_visible
:
2432 int x0
, y0
, dir
, first_index
, last_index
;
2434 pos_v
= Field (dir_v
, 0);
2435 x0
= Int_val (Field (pos_v
, 0));
2436 y0
= Int_val (Field (pos_v
, 1));
2437 dir
= Int_val (Field (pos_v
, 2));
2442 last_index
= page
->slinkcount
;
2445 first_index
= page
->slinkcount
- 1;
2449 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2450 slink
= &page
->slinks
[i
];
2451 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2460 slinkindex
= Int_val (Field (dir_v
, 0));
2461 found
= &page
->slinks
[slinkindex
];
2462 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2463 slink
= &page
->slinks
[i
];
2464 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2472 slinkindex
= Int_val (Field (dir_v
, 0));
2473 found
= &page
->slinks
[slinkindex
];
2474 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2475 slink
= &page
->slinks
[i
];
2476 if (slink
->bbox
.x0
> found
->bbox
.x0
) {
2484 slinkindex
= Int_val (Field (dir_v
, 0));
2485 found
= &page
->slinks
[slinkindex
];
2486 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++i
) {
2487 slink
= &page
->slinks
[i
];
2488 if (slink
->bbox
.y0
>= found
->bbox
.y0
) {
2496 slinkindex
= Int_val (Field (dir_v
, 0));
2497 found
= &page
->slinks
[slinkindex
];
2498 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2499 slink
= &page
->slinks
[i
];
2500 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2509 dirtag
= Int_val (dir_v
);
2512 found
= page
->slinks
;
2517 found
= page
->slinks
+ (page
->slinkcount
- 1);
2523 ret_v
= caml_alloc_small (2, 1);
2524 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2531 enum { uuri
, utext
, uannot
};
2533 value
ml_getlink (value ptr_v
, value n_v
);
2534 value
ml_getlink (value ptr_v
, value n_v
)
2536 CAMLparam2 (ptr_v
, n_v
);
2537 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2540 char *s
= String_val (ptr_v
);
2541 struct slink
*slink
;
2543 ret_v
= Val_int (0);
2544 page
= parse_pointer (__func__
, s
);
2547 ensureslinks (page
);
2548 slink
= &page
->slinks
[Int_val (n_v
)];
2549 if (slink
->tag
== SLINK
) {
2550 link
= slink
->u
.link
;
2551 str_v
= caml_copy_string (link
->uri
);
2552 ret_v
= caml_alloc_small (1, uuri
);
2553 Field (ret_v
, 0) = str_v
;
2556 ret_v
= caml_alloc_small (1, uannot
);
2557 tup_v
= caml_alloc_tuple (2);
2558 Field (ret_v
, 0) = tup_v
;
2559 Field (tup_v
, 0) = ptr_v
;
2560 Field (tup_v
, 1) = n_v
;
2567 value
ml_getannotcontents (value ptr_v
, value n_v
);
2568 value
ml_getannotcontents (value ptr_v
, value n_v
)
2570 CAMLparam2 (ptr_v
, n_v
);
2573 const char *contents
= "";
2576 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2578 char *s
= String_val (ptr_v
);
2580 struct slink
*slink
;
2582 page
= parse_pointer (__func__
, s
);
2583 slink
= &page
->slinks
[Int_val (n_v
)];
2584 contents
= pdf_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
);
2587 ret_v
= caml_copy_string (contents
);
2591 value
ml_getlinkcount (value ptr_v
);
2592 value
ml_getlinkcount (value ptr_v
)
2596 char *s
= String_val (ptr_v
);
2598 page
= parse_pointer (__func__
, s
);
2599 CAMLreturn (Val_int (page
->slinkcount
));
2602 value
ml_getlinkrect (value ptr_v
, value n_v
);
2603 value
ml_getlinkrect (value ptr_v
, value n_v
)
2605 CAMLparam2 (ptr_v
, n_v
);
2608 struct slink
*slink
;
2609 char *s
= String_val (ptr_v
);
2611 page
= parse_pointer (__func__
, s
);
2612 ret_v
= caml_alloc_tuple (4);
2614 ensureslinks (page
);
2616 slink
= &page
->slinks
[Int_val (n_v
)];
2617 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2618 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2619 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2620 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2625 value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
);
2626 value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
2628 CAMLparam3 (ptr_v
, x_v
, y_v
);
2629 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2631 struct annot
*annot
;
2633 char *ptr
= String_val (ptr_v
);
2634 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2635 struct pagedim
*pdim
;
2637 ret_v
= Val_int (0);
2638 if (trylock (__func__
)) {
2642 page
= parse_pointer (__func__
, ptr
);
2643 pdim
= &state
.pagedims
[page
->pdimno
];
2644 x
+= pdim
->bounds
.x0
;
2645 y
+= pdim
->bounds
.y0
;
2648 annot
= getannot (page
, x
, y
);
2652 ensureslinks (page
);
2653 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2654 if (page
->slinks
[i
].tag
== SANNOT
2655 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2660 ret_v
= caml_alloc_small (1, uannot
);
2661 tup_v
= caml_alloc_tuple (2);
2662 Field (ret_v
, 0) = tup_v
;
2663 Field (tup_v
, 0) = ptr_v
;
2664 Field (tup_v
, 1) = Val_int (n
);
2669 link
= getlink (page
, x
, y
);
2671 str_v
= caml_copy_string (link
->uri
);
2672 ret_v
= caml_alloc_small (1, uuri
);
2673 Field (ret_v
, 0) = str_v
;
2677 fz_stext_block
*block
;
2681 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2682 fz_stext_line
*line
;
2684 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2686 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2689 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2693 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2696 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2697 fz_quad
*q
= &ch
->quad
;
2699 if (x
>= q
->ul
.x
&& x
<= q
->ur
.x
2700 && y
>= q
->ul
.y
&& y
<= q
->ll
.y
) {
2701 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
2702 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
2705 if (!n2
) n2
= "<unknown font>";
2707 if (face
&& face
->family_name
) {
2709 char *n1
= face
->family_name
;
2710 size_t l1
= strlen (n1
);
2711 size_t l2
= strlen (n2
);
2713 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2714 s
= malloc (l1
+ l2
+ 2);
2718 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2719 str_v
= caml_copy_string (s
);
2724 if (str_v
== Val_unit
) {
2725 str_v
= caml_copy_string (n2
);
2727 ret_v
= caml_alloc_small (1, utext
);
2728 Field (ret_v
, 0) = str_v
;
2742 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2744 void ml_clearmark (value ptr_v
);
2745 void ml_clearmark (value ptr_v
)
2748 char *s
= String_val (ptr_v
);
2751 if (trylock (__func__
)) {
2755 page
= parse_pointer (__func__
, s
);
2764 static int uninteresting (int c
)
2766 return isspace (c
) || ispunct (c
);
2769 value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
);
2770 value
ml_markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
)
2772 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2776 fz_stext_line
*line
;
2777 fz_stext_block
*block
;
2778 struct pagedim
*pdim
;
2779 int mark
= Int_val (mark_v
);
2780 char *s
= String_val (ptr_v
);
2781 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2783 ret_v
= Val_bool (0);
2784 if (trylock (__func__
)) {
2788 page
= parse_pointer (__func__
, s
);
2789 pdim
= &state
.pagedims
[page
->pdimno
];
2793 if (mark
== mark_page
) {
2794 page
->fmark
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
2795 page
->lmark
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
2796 ret_v
= Val_bool (1);
2800 x
+= pdim
->bounds
.x0
;
2801 y
+= pdim
->bounds
.y0
;
2803 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2804 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2806 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2809 if (mark
== mark_block
) {
2810 page
->fmark
= block
->u
.t
.first_line
->first_char
;
2811 page
->lmark
= block
->u
.t
.last_line
->last_char
;
2812 ret_v
= Val_bool (1);
2816 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2820 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2823 if (mark
== mark_line
) {
2824 page
->fmark
= line
->first_char
;
2825 page
->lmark
= line
->last_char
;
2826 ret_v
= Val_bool (1);
2830 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2831 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
2832 fz_quad
*q
= &ch
->quad
;
2833 if (x
>= q
->ul
.x
&& x
<= q
->ur
.x
2834 && y
>= q
->ul
.y
&& y
<= q
->ll
.y
) {
2835 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
2836 if (uninteresting (ch2
->c
)) first
= NULL
;
2837 else if (!first
) first
= ch2
;
2839 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
2840 if (uninteresting (ch2
->c
)) break;
2844 page
->fmark
= first
;
2846 ret_v
= Val_bool (1);
2853 if (!Bool_val (ret_v
)) {
2863 value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
);
2864 value
ml_rectofblock (value ptr_v
, value x_v
, value y_v
)
2866 CAMLparam3 (ptr_v
, x_v
, y_v
);
2867 CAMLlocal2 (ret_v
, res_v
);
2870 struct pagedim
*pdim
;
2871 fz_stext_block
*block
;
2872 char *s
= String_val (ptr_v
);
2873 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2875 ret_v
= Val_int (0);
2876 if (trylock (__func__
)) {
2880 page
= parse_pointer (__func__
, s
);
2881 pdim
= &state
.pagedims
[page
->pdimno
];
2882 x
+= pdim
->bounds
.x0
;
2883 y
+= pdim
->bounds
.y0
;
2887 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2888 switch (block
->type
) {
2889 case FZ_STEXT_BLOCK_TEXT
:
2893 case FZ_STEXT_BLOCK_IMAGE
:
2901 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
2906 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2907 ret_v
= caml_alloc_small (1, 1);
2908 Store_double_field (res_v
, 0, (double) b
->x0
);
2909 Store_double_field (res_v
, 1, (double) b
->x1
);
2910 Store_double_field (res_v
, 2, (double) b
->y0
);
2911 Store_double_field (res_v
, 3, (double) b
->y1
);
2912 Field (ret_v
, 0) = res_v
;
2920 void ml_seltext (value ptr_v
, value rect_v
);
2921 void ml_seltext (value ptr_v
, value rect_v
)
2923 CAMLparam2 (ptr_v
, rect_v
);
2925 struct pagedim
*pdim
;
2926 char *s
= String_val (ptr_v
);
2929 fz_stext_line
*line
;
2930 fz_stext_block
*block
;
2931 fz_stext_char
*fc
, *lc
;
2933 if (trylock (__func__
)) {
2937 page
= parse_pointer (__func__
, s
);
2940 pdim
= &state
.pagedims
[page
->pdimno
];
2941 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
2942 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2943 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2944 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2957 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2958 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2959 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2960 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2961 fz_quad q
= ch
->quad
;
2962 if (x0
>= q
.ul
.x
&& x0
<= q
.ur
.x
2963 && y0
>= q
.ul
.y
&& y0
<= q
.ll
.y
) {
2966 if (x1
>= q
.ul
.x
&& x1
<= q
.ur
.x
2967 && y1
>= q
.ul
.y
&& y1
<= q
.ll
.y
) {
2973 if (x1
< x0
&& fc
== lc
) {
2990 static int pipechar (FILE *f
, fz_stext_char
*ch
)
2996 len
= fz_runetochar (buf
, ch
->c
);
2997 ret
= fwrite (buf
, len
, 1, f
);
2999 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3000 len
, ret
, errno
, strerror (errno
));
3006 value
ml_spawn (value command_v
, value fds_v
);
3007 value
ml_spawn (value command_v
, value fds_v
)
3009 CAMLparam2 (command_v
, fds_v
);
3010 CAMLlocal2 (l_v
, tup_v
);
3012 pid_t pid
= (pid_t
) -1;
3014 value earg_v
= Nothing
;
3015 posix_spawnattr_t attr
;
3016 posix_spawn_file_actions_t fa
;
3017 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
3019 argv
[2] = String_val (command_v
);
3021 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3022 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3025 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3026 msg
= "posix_spawnattr_init";
3030 #ifdef POSIX_SPAWN_USEVFORK
3031 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3032 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3037 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3040 tup_v
= Field (l_v
, 0);
3041 fd1
= Int_val (Field (tup_v
, 0));
3042 fd2
= Int_val (Field (tup_v
, 1));
3044 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3045 msg
= "posix_spawn_file_actions_addclose";
3051 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3052 msg
= "posix_spawn_file_actions_adddup2";
3059 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3060 msg
= "posix_spawn";
3065 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3066 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3070 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3071 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3072 ret1
, strerror (ret1
));
3076 unix_error (ret
, msg
, earg_v
);
3078 CAMLreturn (Val_int (pid
));
3081 value
ml_hassel (value ptr_v
);
3082 value
ml_hassel (value ptr_v
)
3087 char *s
= String_val (ptr_v
);
3089 ret_v
= Val_bool (0);
3090 if (trylock (__func__
)) {
3094 page
= parse_pointer (__func__
, s
);
3095 ret_v
= Val_bool (page
->fmark
&& page
->lmark
);
3101 void ml_copysel (value fd_v
, value ptr_v
);
3102 void ml_copysel (value fd_v
, value ptr_v
)
3104 CAMLparam2 (fd_v
, ptr_v
);
3108 fz_stext_line
*line
;
3109 fz_stext_block
*block
;
3110 int fd
= Int_val (fd_v
);
3111 char *s
= String_val (ptr_v
);
3113 if (trylock (__func__
)) {
3117 page
= parse_pointer (__func__
, s
);
3119 if (!page
->fmark
|| !page
->lmark
) {
3120 printd ("emsg nothing to copy on page %d", page
->pageno
);
3124 f
= fdopen (fd
, "w");
3126 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3127 fd
, errno
, strerror (errno
));
3131 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3132 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3133 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3135 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3136 if (seen
|| ch
== page
->fmark
) {
3139 if (ch
== page
->lmark
) goto close
;
3140 } while ((ch
= ch
->next
));
3145 if (seen
) fputc ('\n', f
);
3150 int ret
= fclose (f
);
3153 if (errno
!= ECHILD
) {
3154 printd ("emsg failed to close sel pipe: %d:%s",
3155 errno
, strerror (errno
));
3165 printd ("emsg failed to close sel pipe: %d:%s",
3166 errno
, strerror (errno
));
3172 value
ml_getpdimrect (value pagedimno_v
);
3173 value
ml_getpdimrect (value pagedimno_v
)
3175 CAMLparam1 (pagedimno_v
);
3177 int pagedimno
= Int_val (pagedimno_v
);
3180 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3181 if (trylock (__func__
)) {
3182 box
= fz_empty_rect
;
3185 box
= state
.pagedims
[pagedimno
].mediabox
;
3189 Store_double_field (ret_v
, 0, (double) box
.x0
);
3190 Store_double_field (ret_v
, 1, (double) box
.x1
);
3191 Store_double_field (ret_v
, 2, (double) box
.y0
);
3192 Store_double_field (ret_v
, 3, (double) box
.y1
);
3197 value
ml_zoom_for_height (value winw_v
, value winh_v
,
3198 value dw_v
, value cols_v
);
3199 value
ml_zoom_for_height (value winw_v
, value winh_v
,
3200 value dw_v
, value cols_v
)
3202 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3208 float winw
= Int_val (winw_v
);
3209 float winh
= Int_val (winh_v
);
3210 float dw
= Int_val (dw_v
);
3211 float cols
= Int_val (cols_v
);
3212 float pw
= 1.0, ph
= 1.0;
3214 if (trylock (__func__
)) {
3218 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3219 float w
= p
->pagebox
.x1
/ cols
;
3220 float h
= p
->pagebox
.y1
;
3224 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3226 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3229 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3232 ret_v
= caml_copy_double ((double) zoom
);
3236 value
ml_getmaxw (value unit_v
);
3237 value
ml_getmaxw (value unit_v
)
3239 CAMLparam1 (unit_v
);
3245 if (trylock (__func__
)) {
3249 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3250 float w
= p
->pagebox
.x1
;
3251 maxw
= fz_max (maxw
, w
);
3256 ret_v
= caml_copy_double ((double) maxw
);
3260 value
ml_draw_string (value pt_v
, value x_v
,
3261 value y_v
, value string_v
);
3262 value
ml_draw_string (value pt_v
, value x_v
,
3263 value y_v
, value string_v
)
3265 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3267 int pt
= Int_val(pt_v
);
3268 int x
= Int_val (x_v
);
3269 int y
= Int_val (y_v
);
3272 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3273 ret_v
= caml_copy_double (w
);
3277 value
ml_measure_string (value pt_v
, value string_v
);
3278 value
ml_measure_string (value pt_v
, value string_v
)
3280 CAMLparam2 (pt_v
, string_v
);
3282 int pt
= Int_val (pt_v
);
3285 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3286 ret_v
= caml_copy_double (w
);
3290 value
ml_getpagebox (value opaque_v
);
3291 value
ml_getpagebox (value opaque_v
)
3293 CAMLparam1 (opaque_v
);
3298 char *s
= String_val (opaque_v
);
3299 struct page
*page
= parse_pointer (__func__
, s
);
3301 ret_v
= caml_alloc_tuple (4);
3302 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3304 fz_run_page (state
.ctx
, page
->fzpage
, dev
, pagectm (page
), NULL
);
3306 fz_close_device (state
.ctx
, dev
);
3307 fz_drop_device (state
.ctx
, dev
);
3308 bbox
= fz_round_rect (rect
);
3309 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3310 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3311 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3312 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3317 void ml_setaalevel (value level_v
);
3318 void ml_setaalevel (value level_v
)
3320 CAMLparam1 (level_v
);
3322 state
.aalevel
= Int_val (level_v
);
3326 value
ml_keysymtoutf8 (value keysym_v
);
3328 value
ml_keysymtoutf8 (value keysym_v
)
3330 CAMLparam1 (keysym_v
);
3332 KeySym keysym
= Int_val (keysym_v
);
3334 extern long keysym2ucs (KeySym
);
3338 rune
= (Rune
) keysym2ucs (keysym
);
3339 len
= fz_runetochar (buf
, rune
);
3341 str_v
= caml_copy_string (buf
);
3345 value
ml_keysymtoutf8 (value keysym_v
)
3347 CAMLparam1 (keysym_v
);
3349 long ucs
= Long_val (keysym_v
);
3353 len
= fz_runetochar (buf
, (int) ucs
);
3355 str_v
= caml_copy_string (buf
);
3360 enum { piunknown
, pilinux
, pimacos
, pibsd
};
3362 value
ml_platform (value unit_v
);
3363 value
ml_platform (value unit_v
)
3365 CAMLparam1 (unit_v
);
3366 CAMLlocal2 (tup_v
, arr_v
);
3367 int platid
= piunknown
;
3370 #if defined __linux__
3372 #elif defined __DragonFly__ || defined __FreeBSD__
3373 || defined __OpenBSD__
|| defined __NetBSD__
3375 #elif defined __APPLE__
3378 if (uname (&buf
)) err (1, "uname");
3380 tup_v
= caml_alloc_tuple (2);
3382 char const *sar
[] = {
3389 arr_v
= caml_copy_string_array (sar
);
3391 Field (tup_v
, 0) = Val_int (platid
);
3392 Field (tup_v
, 1) = arr_v
;
3396 void ml_cloexec (value fd_v
);
3397 void ml_cloexec (value fd_v
)
3400 int fd
= Int_val (fd_v
);
3402 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3403 uerror ("fcntl", Nothing
);
3408 value
ml_getpbo (value w_v
, value h_v
, value cs_v
);
3409 value
ml_getpbo (value w_v
, value h_v
, value cs_v
)
3411 CAMLparam2 (w_v
, h_v
);
3414 int w
= Int_val (w_v
);
3415 int h
= Int_val (h_v
);
3416 int cs
= Int_val (cs_v
);
3418 if (state
.bo_usable
) {
3419 pbo
= calloc (sizeof (*pbo
), 1);
3421 err (1, "calloc pbo");
3433 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3436 state
.glGenBuffersARB (1, &pbo
->id
);
3437 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3438 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3439 NULL
, GL_STREAM_DRAW
);
3440 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3442 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3444 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3445 state
.glDeleteBuffersARB (1, &pbo
->id
);
3447 ret_v
= caml_copy_string ("0");
3453 res
= snprintf (NULL
, 0, "%" PRIxPTR
, (uintptr_t) pbo
);
3455 err (1, "snprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3459 err (1, "malloc %d bytes failed", res
+1);
3461 res
= sprintf (s
, "%" PRIxPTR
, (uintptr_t) pbo
);
3463 err (1, "sprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3465 ret_v
= caml_copy_string (s
);
3470 ret_v
= caml_copy_string ("0");
3475 void ml_freepbo (value s_v
);
3476 void ml_freepbo (value s_v
)
3479 char *s
= String_val (s_v
);
3480 struct tile
*tile
= parse_pointer (__func__
, s
);
3483 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3485 tile
->pbo
->ptr
= NULL
;
3486 tile
->pbo
->size
= -1;
3491 void ml_unmappbo (value s_v
);
3492 void ml_unmappbo (value s_v
)
3495 char *s
= String_val (s_v
);
3496 struct tile
*tile
= parse_pointer (__func__
, s
);
3499 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3500 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3501 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3503 tile
->pbo
->ptr
= NULL
;
3504 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3509 static void setuppbo (void)
3511 extern void (*wsigladdr (const char *name
)) (void);
3512 #pragma GCC diagnostic push
3514 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3516 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3517 state
.bo_usable
= GPA (glBindBufferARB
)
3518 && GPA (glUnmapBufferARB
)
3519 && GPA (glMapBufferARB
)
3520 && GPA (glBufferDataARB
)
3521 && GPA (glGenBuffersARB
)
3522 && GPA (glDeleteBuffersARB
);
3524 #pragma GCC diagnostic pop
3527 value
ml_bo_usable (void);
3528 value
ml_bo_usable (void)
3530 return Val_bool (state
.bo_usable
);
3533 value
ml_unproject (value ptr_v
, value x_v
, value y_v
);
3534 value
ml_unproject (value ptr_v
, value x_v
, value y_v
)
3536 CAMLparam3 (ptr_v
, x_v
, y_v
);
3537 CAMLlocal2 (ret_v
, tup_v
);
3539 char *s
= String_val (ptr_v
);
3540 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3541 struct pagedim
*pdim
;
3544 page
= parse_pointer (__func__
, s
);
3545 pdim
= &state
.pagedims
[page
->pdimno
];
3547 ret_v
= Val_int (0);
3548 if (trylock (__func__
)) {
3552 p
.x
= x
+ pdim
->bounds
.x0
;
3553 p
.y
= y
+ pdim
->bounds
.y0
;
3555 p
= fz_transform_point (p
, fz_invert_matrix (fz_concat (pdim
->tctm
,
3558 tup_v
= caml_alloc_tuple (2);
3559 ret_v
= caml_alloc_small (1, 1);
3560 Field (tup_v
, 0) = Val_int (p
.x
);
3561 Field (tup_v
, 1) = Val_int (p
.y
);
3562 Field (ret_v
, 0) = tup_v
;
3569 value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
3570 value x_v
, value y_v
);
3571 value
ml_project (value ptr_v
, value pageno_v
, value pdimno_v
,
3572 value x_v
, value y_v
)
3574 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
3577 char *s
= String_val (ptr_v
);
3578 int pageno
= Int_val (pageno_v
);
3579 int pdimno
= Int_val (pdimno_v
);
3580 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
3581 struct pagedim
*pdim
;
3585 ret_v
= Val_int (0);
3589 page
= loadpage (pageno
, pdimno
);
3592 page
= parse_pointer (__func__
, s
);
3594 pdim
= &state
.pagedims
[pdimno
];
3596 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3597 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3598 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3603 p
.x
= x
+ pdim
->bounds
.x0
;
3604 p
.y
= y
+ pdim
->bounds
.y0
;
3606 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
3607 p
= fz_transform_point (p
, ctm
);
3609 ret_v
= caml_alloc_tuple (2);
3610 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
3611 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
3620 void ml_addannot (value ptr_v
, value x_v
, value y_v
, value contents_v
);
3621 void ml_addannot (value ptr_v
, value x_v
, value y_v
, value contents_v
)
3623 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
3624 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3630 char *s
= String_val (ptr_v
);
3632 page
= parse_pointer (__func__
, s
);
3633 annot
= pdf_create_annot (state
.ctx
,
3634 pdf_page_from_fz_page (state
.ctx
,
3637 r
.x0
= Int_val (x_v
) - 10;
3638 r
.y0
= Int_val (y_v
) - 10;
3641 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
3642 pdf_set_annot_rect (state
.ctx
, annot
, r
);
3649 void ml_delannot (value ptr_v
, value n_v
);
3650 void ml_delannot (value ptr_v
, value n_v
)
3652 CAMLparam2 (ptr_v
, n_v
);
3653 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3657 char *s
= String_val (ptr_v
);
3658 struct slink
*slink
;
3660 page
= parse_pointer (__func__
, s
);
3661 slink
= &page
->slinks
[Int_val (n_v
)];
3662 pdf_delete_annot (state
.ctx
,
3663 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
3664 (pdf_annot
*) slink
->u
.annot
);
3670 void ml_modannot (value ptr_v
, value n_v
, value str_v
);
3671 void ml_modannot (value ptr_v
, value n_v
, value str_v
)
3673 CAMLparam3 (ptr_v
, n_v
, str_v
);
3674 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3678 char *s
= String_val (ptr_v
);
3679 struct slink
*slink
;
3681 page
= parse_pointer (__func__
, s
);
3682 slink
= &page
->slinks
[Int_val (n_v
)];
3683 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
3684 String_val (str_v
));
3690 value
ml_hasunsavedchanges (void);
3691 value
ml_hasunsavedchanges (void)
3693 return Val_bool (state
.dirty
);
3696 void ml_savedoc (value path_v
);
3697 void ml_savedoc (value path_v
)
3699 CAMLparam1 (path_v
);
3700 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3703 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
3708 static void makestippletex (void)
3710 const char pixels
[] = "\xff\xff\0\0";
3711 glGenTextures (1, &state
.stid
);
3712 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3713 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3714 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3727 value
ml_fz_version (void);
3728 value
ml_fz_version (void)
3730 return caml_copy_string (FZ_VERSION
);
3733 value
ml_llpp_version (void);
3734 value
ml_llpp_version (void)
3736 extern char llpp_version
[];
3737 return caml_copy_string (llpp_version
);
3740 void ml_init (value csock_v
, value params_v
);
3741 void ml_init (value csock_v
, value params_v
)
3743 CAMLparam2 (csock_v
, params_v
);
3744 CAMLlocal2 (trim_v
, fuzz_v
);
3751 state
.csock
= Int_val (csock_v
);
3752 state
.rotate
= Int_val (Field (params_v
, 0));
3753 state
.fitmodel
= Int_val (Field (params_v
, 1));
3754 trim_v
= Field (params_v
, 2);
3755 texcount
= Int_val (Field (params_v
, 3));
3756 state
.sliceheight
= Int_val (Field (params_v
, 4));
3757 mustoresize
= Int_val (Field (params_v
, 5));
3758 colorspace
= Int_val (Field (params_v
, 6));
3759 fontpath
= String_val (Field (params_v
, 7));
3764 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3765 if (setlocale (LC_CTYPE
, "")) {
3766 const char *cset
= nl_langinfo (CODESET
);
3767 state
.utf8cs
= !strcmp (cset
, "UTF-8");
3770 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
3774 if (caml_string_length (Field (params_v
, 8)) > 0) {
3775 state
.trimcachepath
= ystrdup (String_val (Field (params_v
, 8)));
3777 if (!state
.trimcachepath
) {
3778 printd ("emsg failed to strdup trimcachepath: %d:%s",
3779 errno
, strerror (errno
));
3783 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3784 fz_register_document_handlers (state
.ctx
);
3786 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3787 fuzz_v
= Field (trim_v
, 1);
3788 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3789 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3790 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3791 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3793 set_tex_params (colorspace
);
3796 state
.face
= load_font (fontpath
);
3800 const unsigned char *data
;
3802 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
3803 state
.face
= load_builtin_font (data
, len
);
3805 if (!state
.face
) _exit (1);
3807 realloctexts (texcount
);
3811 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3813 errx (1, "pthread_create: %s", strerror (ret
));
3820 static void UNUSED_ATTR NO_OPTIMIZE_ATTR
refmacs (void) {}