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__); \
92 #define ML(d) extern value ml_##d; value ml_##d
93 #define ML0(d) extern void ml_##d; void ml_##d
106 struct slice slices
[1];
117 fz_matrix ctm
, zoomctm
, tctm
;
121 enum { SLINK
, SANNOT
} tag
;
142 fz_display_list
*dlist
;
145 struct slink
*slinks
;
147 struct annot
*annots
;
148 fz_stext_char
*fmark
, *lmark
;
151 enum { FitWidth
, FitProportional
, FitPage
};
155 struct pagedim
*pagedims
;
170 fz_colorspace
*colorspace
;
201 void (*glBindBufferARB
) (GLenum
, GLuint
);
202 GLboolean (*glUnmapBufferARB
) (GLenum
);
203 void *(*glMapBufferARB
) (GLenum
, GLenum
);
204 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
205 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
206 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
208 GLfloat texcoords
[8];
209 GLfloat vertices
[16];
220 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
222 static void lock (const char *cap
)
224 int ret
= pthread_mutex_lock (&mutex
);
226 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
230 static void unlock (const char *cap
)
232 int ret
= pthread_mutex_unlock (&mutex
);
234 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
238 static int trylock (const char *cap
)
240 int ret
= pthread_mutex_trylock (&mutex
);
241 if (ret
&& ret
!= EBUSY
) {
242 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
247 static int hasdata (void)
250 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
251 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
255 ML (hasdata (value fd_v
))
260 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
261 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
262 CAMLreturn (Val_bool (avail
> 0));
265 static void readdata (int fd
, void *p
, int size
)
270 n
= read (fd
, p
, size
);
272 if (n
< 0 && errno
== EINTR
) goto again
;
273 if (!n
) errx (1, "EOF while reading");
274 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
278 static void writedata (int fd
, char *p
, int size
)
281 uint32_t size4
= size
;
282 struct iovec iov
[2] = {
283 { .iov_base
= &size4
, .iov_len
= 4 },
284 { .iov_base
= p
, .iov_len
= size
}
288 n
= writev (fd
, iov
, 2);
289 if (n
< 0 && errno
== EINTR
) goto again
;
291 if (!n
) errx (1, "EOF while writing data");
292 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
296 static int readlen (int fd
)
299 readdata (fd
, &u
, 4);
303 ML0 (wcmd (value fd_v
, value bytes_v
, value len_v
))
305 CAMLparam3 (fd_v
, bytes_v
, len_v
);
306 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
310 ML (rcmd (value fd_v
))
313 CAMLlocal1 (strdata_v
);
314 int fd
= Int_val (fd_v
);
315 int len
= readlen (fd
);
316 strdata_v
= caml_alloc_string (len
);
317 readdata (fd
, Bytes_val (strdata_v
), len
);
318 CAMLreturn (strdata_v
);
321 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
324 int size
= sizeof (fbuf
), len
;
330 len
= vsnprintf (buf
, size
, fmt
, ap
);
334 if (len
< size
- 4) {
335 writedata (state
.csock
, buf
, len
);
341 err (1, "vsnprintf for `%s' failed", fmt
);
343 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
344 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
346 if (buf
!= fbuf
) free (buf
);
349 static void closedoc (void)
352 fz_drop_document (state
.ctx
, state
.doc
);
357 static int openxref (char *filename
, char *password
, int layouth
)
359 for (int i
= 0; i
< state
.texcount
; ++i
) {
360 state
.texowners
[i
].w
= -1;
361 state
.texowners
[i
].slice
= NULL
;
367 if (state
.pagedims
) {
368 free (state
.pagedims
);
369 state
.pagedims
= NULL
;
371 state
.pagedimcount
= 0;
373 fz_set_aa_level (state
.ctx
, state
.aalevel
);
374 state
.doc
= fz_open_document (state
.ctx
, filename
);
375 if (fz_needs_password (state
.ctx
, state
.doc
)) {
376 if (password
&& !*password
) {
381 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
383 printd ("pass fail");
389 fz_layout_document (state
.ctx
, state
.doc
, 460, layouth
, 12);
390 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
394 static void docinfo (void)
396 struct { char *tag
; char *name
; } metatbl
[] = {
397 { FZ_META_INFO_TITLE
, "Title" },
398 { FZ_META_INFO_AUTHOR
, "Author" },
399 { FZ_META_FORMAT
, "Format" },
400 { FZ_META_ENCRYPTION
, "Encryption" },
401 { "info:Creator", "Creator" },
402 { "info:Producer", "Producer" },
403 { "info:CreationDate", "Creation date" },
408 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
411 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
412 metatbl
[i
].tag
, buf
, len
);
415 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
418 buf
= realloc (buf
, need
+ 1);
419 if (!buf
) err (1, "docinfo realloc %d", need
+ 1);
430 static void unlinktile (struct tile
*tile
)
432 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
433 struct slice
*s
= &tile
->slices
[i
];
435 if (s
->texindex
!= -1) {
436 if (state
.texowners
[s
->texindex
].slice
== s
) {
437 state
.texowners
[s
->texindex
].slice
= NULL
;
443 static void freepage (struct page
*page
)
447 fz_drop_stext_page (state
.ctx
, page
->text
);
452 fz_drop_display_list (state
.ctx
, page
->dlist
);
453 fz_drop_page (state
.ctx
, page
->fzpage
);
457 static void freetile (struct tile
*tile
)
462 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
463 #else /* piggyback */
465 fz_drop_pixmap (state
.ctx
, state
.pig
);
467 state
.pig
= tile
->pixmap
;
472 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
477 static void trimctm (pdf_page
*page
, int pindex
)
479 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
482 if (!pdim
->tctmready
) {
483 fz_rect realbox
, mediabox
;
484 fz_matrix page_ctm
, ctm
;
486 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
487 realbox
= fz_transform_rect (pdim
->mediabox
, ctm
);
488 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
489 pdim
->tctm
= fz_concat (
490 fz_invert_matrix (page_ctm
),
491 fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
)));
496 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
499 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
501 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
502 if (pdf_specifics (state
.ctx
, state
.doc
)) {
503 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
504 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
507 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
),
513 static fz_matrix
pagectm (struct page
*page
)
515 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
518 static void *loadpage (int pageno
, int pindex
)
523 page
= calloc (sizeof (struct page
), 1);
525 err (1, "calloc page %d", pageno
);
528 page
->dlist
= fz_new_display_list (state
.ctx
, fz_infinite_rect
);
529 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
531 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
532 fz_run_page (state
.ctx
, page
->fzpage
, dev
, fz_identity
, NULL
);
534 fz_catch (state
.ctx
) {
537 fz_close_device (state
.ctx
, dev
);
538 fz_drop_device (state
.ctx
, dev
);
540 page
->pdimno
= pindex
;
541 page
->pageno
= pageno
;
542 page
->sgen
= state
.gen
;
543 page
->agen
= state
.gen
;
544 page
->tgen
= state
.gen
;
548 static struct tile
*alloctile (int h
)
554 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
555 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
556 tile
= calloc (tilesize
, 1);
558 err (1, "cannot allocate tile (%zu bytes)", tilesize
);
560 for (int i
= 0; i
< slicecount
; ++i
) {
561 int sh
= fz_mini (h
, state
.sliceheight
);
562 tile
->slices
[i
].h
= sh
;
563 tile
->slices
[i
].texindex
= -1;
566 tile
->slicecount
= slicecount
;
567 tile
->sliceheight
= state
.sliceheight
;
571 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
578 struct pagedim
*pdim
;
580 tile
= alloctile (h
);
581 pdim
= &state
.pagedims
[page
->pdimno
];
586 bbox
.x1
= bbox
.x0
+ w
;
587 bbox
.y1
= bbox
.y0
+ h
;
590 if (state
.pig
->w
== w
592 && state
.pig
->colorspace
== state
.colorspace
) {
593 tile
->pixmap
= state
.pig
;
594 tile
->pixmap
->x
= bbox
.x0
;
595 tile
->pixmap
->y
= bbox
.y0
;
598 fz_drop_pixmap (state
.ctx
, state
.pig
);
605 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
606 bbox
, NULL
, 1, pbo
->ptr
);
611 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
,
618 fz_fill_pixmap_with_color (state
.ctx
, tile
->pixmap
,
619 fz_device_rgb (state
.ctx
),
621 fz_default_color_params
);
623 dev
= fz_new_draw_device (state
.ctx
, fz_identity
, tile
->pixmap
);
624 ctm
= pagectm (page
);
625 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, ctm
,
626 fz_rect_from_irect (bbox
), NULL
);
627 fz_close_device (state
.ctx
, dev
);
628 fz_drop_device (state
.ctx
, dev
);
633 static void initpdims (void)
636 int pageno
, trim
, show
;
637 int trimw
= 0, cxcount
;
638 struct pagedim
*p
= NULL
;
639 fz_context
*ctx
= state
.ctx
;
640 fz_rect rootmediabox
= fz_empty_rect
;
641 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
648 if (state
.trimmargins
&& state
.trimcachepath
) {
649 trimf
= fopen (state
.trimcachepath
, "rb");
651 trimf
= fopen (state
.trimcachepath
, "wb");
656 cxcount
= state
.pagecount
;
659 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
660 "Root/Pages/MediaBox");
661 rootmediabox
= pdf_to_rect (ctx
, obj
);
662 pdf_load_page_tree (ctx
, pdf
);
665 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
667 fz_rect mediabox
= fz_empty_rect
;
671 pdf_obj
*pageobj
= NULL
;
674 if (pdf
->rev_page_map
) {
675 for (int i
= 0; i
< pdf
->rev_page_count
; ++i
) {
676 if (pdf
->rev_page_map
[i
].page
== pageno
) {
677 pageobj
= pdf_get_xref_entry (
678 ctx
, pdf
, pdf
->rev_page_map
[i
].object
685 pageobj
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
688 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
690 if (state
.trimmargins
) {
695 page
= pdf_load_page (ctx
, pdf
, pageno
);
696 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
697 trim
= state
.trimanew
|| !obj
;
701 fz_matrix ctm
, page_ctm
;
703 dev
= fz_new_bbox_device (ctx
, &rect
);
704 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
705 ctm
= fz_invert_matrix (page_ctm
);
706 pdf_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
707 fz_close_device (ctx
, dev
);
708 fz_drop_device (ctx
, dev
);
710 rect
.x0
+= state
.trimfuzz
.x0
;
711 rect
.x1
+= state
.trimfuzz
.x1
;
712 rect
.y0
+= state
.trimfuzz
.y0
;
713 rect
.y1
+= state
.trimfuzz
.y1
;
714 rect
= fz_transform_rect (rect
, ctm
);
715 rect
= fz_intersect_rect (rect
, mediabox
);
717 if (!fz_is_empty_rect (rect
)) {
721 obj
= pdf_new_array (ctx
, pdf
, 4);
722 pdf_array_push_real (ctx
, obj
, mediabox
.x0
);
723 pdf_array_push_real (ctx
, obj
, mediabox
.y0
);
724 pdf_array_push_real (ctx
, obj
, mediabox
.x1
);
725 pdf_array_push_real (ctx
, obj
, mediabox
.y1
);
726 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
729 mediabox
.x0
= pdf_array_get_real (ctx
, obj
, 0);
730 mediabox
.y0
= pdf_array_get_real (ctx
, obj
, 1);
731 mediabox
.x1
= pdf_array_get_real (ctx
, obj
, 2);
732 mediabox
.y1
= pdf_array_get_real (ctx
, obj
, 3);
735 fz_drop_page (ctx
, &page
->super
);
736 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
738 printd ("progress %f Trimming %d",
739 (double) (pageno
+ 1) / state
.pagecount
,
744 printd ("emsg failed to load page %d", pageno
);
752 pdf_to_rect (ctx
, pdf_dict_get_inheritable (
758 if (fz_is_empty_rect (mediabox
)) {
767 pdf_to_rect (ctx
, pdf_dict_gets (ctx
, pageobj
, "CropBox"));
768 if (!fz_is_empty_rect (cropbox
)) {
773 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
778 if (fz_is_empty_rect (rootmediabox
)) {
779 printd ("emsg cannot find page size for page %d",
783 mediabox
= rootmediabox
;
790 if (state
.trimmargins
&& trimw
) {
794 page
= fz_load_page (ctx
, state
.doc
, pageno
);
795 mediabox
= fz_bound_page (ctx
, page
);
796 if (state
.trimmargins
) {
800 dev
= fz_new_bbox_device (ctx
, &rect
);
801 fz_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
802 fz_close_device (ctx
, dev
);
803 fz_drop_device (ctx
, dev
);
805 rect
.x0
+= state
.trimfuzz
.x0
;
806 rect
.x1
+= state
.trimfuzz
.x1
;
807 rect
.y0
+= state
.trimfuzz
.y0
;
808 rect
.y1
+= state
.trimfuzz
.y1
;
809 rect
= fz_intersect_rect (rect
, mediabox
);
811 if (!fz_is_empty_rect (rect
)) {
815 fz_drop_page (ctx
, page
);
820 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
822 err (1, "fwrite trim mediabox");
828 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
830 err (1, "fread trim mediabox %d", pageno
);
836 page
= fz_load_page (ctx
, state
.doc
, pageno
);
837 mediabox
= fz_bound_page (ctx
, page
);
838 fz_drop_page (ctx
, page
);
840 show
= !state
.trimmargins
&& pageno
% 20 == 0;
842 printd ("progress %f Gathering dimensions %d",
843 (double) (pageno
) / state
.pagecount
,
848 printd ("emsg failed to load page %d", pageno
);
854 if (!p
|| p
->rotate
!= rotate
855 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
858 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
859 state
.pagedims
= realloc (state
.pagedims
, size
);
860 if (!state
.pagedims
) {
861 err (1, "realloc pagedims to %zu (%d elems)",
862 size
, state
.pagedimcount
+ 1);
865 p
= &state
.pagedims
[state
.pagedimcount
++];
867 p
->mediabox
= mediabox
;
873 if (fclose (trimf
)) {
879 static void layout (void)
884 struct pagedim
*p
= NULL
;
885 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
887 if (state
.pagedimcount
== 0) return;
889 switch (state
.fitmodel
) {
890 case FitProportional
:
891 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
894 p
= &state
.pagedims
[pindex
];
895 box
= fz_transform_rect (p
->mediabox
,
896 fz_rotate (p
->rotate
+ state
.rotate
));
898 x0
= fz_min (box
.x0
, box
.x1
);
899 x1
= fz_max (box
.x0
, box
.x1
);
902 maxw
= fz_max (w
, maxw
);
903 zoom
= state
.w
/ maxw
;
915 ARSERT (0 && state
.fitmodel
);
918 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
919 p
= &state
.pagedims
[pindex
];
920 ctm
= fz_rotate (state
.rotate
);
921 box
= fz_transform_rect (p
->mediabox
,
922 fz_rotate (p
->rotate
+ state
.rotate
));
924 switch (state
.fitmodel
) {
925 case FitProportional
:
926 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
934 zoom
= fz_min (zw
, zh
);
935 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
944 p
->zoomctm
= fz_scale (zoom
, zoom
);
945 ctm
= fz_concat (p
->zoomctm
, ctm
);
947 p
->pagebox
= p
->mediabox
;
948 p
->pagebox
= fz_transform_rect (p
->pagebox
, fz_rotate (p
->rotate
));
949 p
->pagebox
.x1
-= p
->pagebox
.x0
;
950 p
->pagebox
.y1
-= p
->pagebox
.y0
;
953 p
->bounds
= fz_round_rect (fz_transform_rect (p
->pagebox
, ctm
));
956 ctm
= fz_concat (fz_translate (0, -p
->mediabox
.y1
),
957 fz_scale (zoom
, -zoom
));
962 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
963 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
964 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
965 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
966 int boundw
= x1
- x0
;
967 int boundh
= y1
- y0
;
969 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
970 } while (p
-- != state
.pagedims
);
973 static struct pagedim
*pdimofpageno (int pageno
)
975 struct pagedim
*pdim
= state
.pagedims
;
977 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
978 if (state
.pagedims
[i
].pageno
> pageno
)
980 pdim
= &state
.pagedims
[i
];
985 static void recurse_outline (fz_outline
*outline
, int level
)
988 if (outline
->page
>= 0) {
989 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
990 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
991 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
992 p
= fz_transform_point (p
, pdim
->ctm
);
993 printd ("o %d %d %d %d %s",
994 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
997 printd ("on %d %s", level
, outline
->title
);
1000 recurse_outline (outline
->down
, level
+ 1);
1002 outline
= outline
->next
;
1006 static void process_outline (void)
1008 fz_outline
*outline
;
1010 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1012 state
.needoutline
= 0;
1013 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1015 recurse_outline (outline
, 0);
1016 fz_drop_outline (state
.ctx
, outline
);
1020 static char *strofline (fz_stext_line
*line
)
1025 size_t size
= 0, cap
= 80;
1027 p
= malloc (cap
+ 1);
1028 if (!p
) return NULL
;
1030 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1031 int n
= fz_runetochar (utf8
, ch
->c
);
1032 if (size
+ n
> cap
) {
1034 p
= realloc (p
, cap
+ 1);
1035 if (!p
) return NULL
;
1038 memcpy (p
+ size
, utf8
, n
);
1045 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1046 int stop
, int pageno
, double start
)
1052 p
= strofline (line
);
1055 ret
= regexec (re
, p
, 1, &rm
, 0);
1058 if (ret
!= REG_NOMATCH
) {
1061 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1062 printd ("msg regexec error `%.*s'",
1063 (int) size
, errbuf
);
1073 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1074 o
+= fz_runelen (ch
->c
);
1080 for (;ch
; ch
= ch
->next
) {
1081 o
+= fz_runelen (ch
->c
);
1082 if (o
> rm
.rm_eo
) break;
1087 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1094 printd ("progress 1 found at %d `%.*s' in %f sec",
1095 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1099 printd ("match %d %d %f %f %f %f %f %f %f %f",
1111 /* wishful thinking function */
1112 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1115 fz_stext_page
*text
;
1116 struct pagedim
*pdim
;
1117 int stop
= 0, niters
= 0;
1120 fz_stext_block
*block
;
1123 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1124 if (niters
++ == 5) {
1127 printd ("progress 1 attention requested aborting search at %d",
1132 printd ("progress %f searching in page %d",
1133 (double) (pageno
+ 1) / state
.pagecount
,
1137 pdim
= pdimofpageno (pageno
);
1138 text
= fz_new_stext_page (state
.ctx
, pdim
->mediabox
);
1139 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1141 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1142 fz_run_page (state
.ctx
, page
, tdev
, pagectm1 (page
, pdim
), NULL
);
1144 fz_close_device (state
.ctx
, tdev
);
1145 fz_drop_device (state
.ctx
, tdev
);
1148 for (block
= text
->first_block
; block
; block
= block
->next
) {
1149 fz_stext_line
*line
;
1151 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1152 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1153 if (line
->bbox
.y0
< y
+ 1) continue;
1155 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1157 case 1: stop
= 1; break;
1158 case -1: stop
= 1; goto endloop
;
1164 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1165 fz_stext_line
*line
;
1167 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1168 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1169 if (line
->bbox
.y0
< y
+ 1) continue;
1171 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1173 case 1: stop
= 1; break;
1174 case -1: stop
= 1; goto endloop
;
1189 fz_drop_stext_page (state
.ctx
, text
);
1190 fz_drop_page (state
.ctx
, page
);
1194 printd ("progress 1 no matches %f sec", end
- start
);
1196 printd ("clearrects");
1199 static void set_tex_params (int colorspace
)
1201 switch (colorspace
) {
1203 state
.texiform
= GL_RGBA8
;
1204 state
.texform
= GL_RGBA
;
1205 state
.texty
= GL_UNSIGNED_BYTE
;
1206 state
.colorspace
= fz_device_rgb (state
.ctx
);
1209 state
.texiform
= GL_LUMINANCE_ALPHA
;
1210 state
.texform
= GL_LUMINANCE_ALPHA
;
1211 state
.texty
= GL_UNSIGNED_BYTE
;
1212 state
.colorspace
= fz_device_gray (state
.ctx
);
1215 errx (1, "invalid colorspce %d", colorspace
);
1219 static void realloctexts (int texcount
)
1223 if (texcount
== state
.texcount
) return;
1225 if (texcount
< state
.texcount
) {
1226 glDeleteTextures (state
.texcount
- texcount
,
1227 state
.texids
+ texcount
);
1230 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1231 state
.texids
= realloc (state
.texids
, size
);
1232 if (!state
.texids
) {
1233 err (1, "realloc texs %zu", size
);
1236 state
.texowners
= (void *) (state
.texids
+ texcount
);
1237 if (texcount
> state
.texcount
) {
1238 glGenTextures (texcount
- state
.texcount
,
1239 state
.texids
+ state
.texcount
);
1240 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1241 state
.texowners
[i
].w
= -1;
1242 state
.texowners
[i
].slice
= NULL
;
1245 state
.texcount
= texcount
;
1249 static char *mbtoutf8 (char *s
)
1259 len
= mbstowcs (NULL
, s
, strlen (s
));
1260 if (len
== 0 || len
== (size_t) -1) {
1262 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1266 tmp
= calloc (len
, sizeof (wchar_t));
1268 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1269 len
, sizeof (wchar_t), errno
, strerror (errno
));
1273 ret
= mbstowcs (tmp
, s
, len
);
1274 if (ret
== (size_t) -1) {
1275 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1276 len
, errno
, strerror (errno
));
1282 for (i
= 0; i
< ret
; ++i
) {
1283 len
+= fz_runelen (tmp
[i
]);
1286 p
= r
= malloc (len
+ 1);
1288 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1293 for (i
= 0; i
< ret
; ++i
) {
1294 p
+= fz_runetochar (p
, tmp
[i
]);
1301 ML (mbtoutf8 (value s_v
))
1313 ret_v
= caml_copy_string (r
);
1319 static void * mainloop (void UNUSED_ATTR
*unused
)
1322 int len
, ret
, oldlen
= 0;
1327 len
= readlen (state
.csock
);
1329 errx (1, "readlen returned 0");
1332 if (oldlen
< len
+ 1) {
1333 p
= realloc (p
, len
+ 1);
1335 err (1, "realloc %d failed", len
+ 1);
1339 readdata (state
.csock
, p
, len
);
1342 if (!strncmp ("open", p
, 4)) {
1343 int off
, usedoccss
, ok
= 0, layouth
;
1350 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1352 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1355 filename
= p
+ 5 + off
;
1356 filenamelen
= strlen (filename
);
1357 password
= filename
+ filenamelen
+ 1;
1359 if (password
[strlen (password
) + 1]) {
1360 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1364 fz_set_use_document_css (state
.ctx
, usedoccss
);
1365 fz_try (state
.ctx
) {
1366 ok
= openxref (filename
, password
, layouth
);
1368 fz_catch (state
.ctx
) {
1369 utf8filename
= mbtoutf8 (filename
);
1370 printd ("emsg Error loading %s: %s",
1371 utf8filename
, fz_caught_message (state
.ctx
));
1372 if (utf8filename
!= filename
) {
1373 free (utf8filename
);
1381 state
.needoutline
= ok
;
1383 else if (!strncmp ("cs", p
, 2)) {
1386 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1388 errx (1, "malformed cs `%.*s' ret=%d", len
, p
, ret
);
1391 set_tex_params (colorspace
);
1392 for (i
= 0; i
< state
.texcount
; ++i
) {
1393 state
.texowners
[i
].w
= -1;
1394 state
.texowners
[i
].slice
= NULL
;
1398 else if (!strncmp ("freepage", p
, 8)) {
1401 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1403 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1407 unlock ("freepage");
1409 else if (!strncmp ("freetile", p
, 8)) {
1412 ret
= sscanf (p
+ 8, " %" SCNxPTR
, (uintptr_t *) &ptr
);
1414 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1418 unlock ("freetile");
1420 else if (!strncmp ("search", p
, 6)) {
1421 int icase
, pageno
, y
, len2
, forward
;
1425 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1426 &icase
, &pageno
, &y
, &forward
, &len2
);
1428 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1431 pattern
= p
+ 6 + len2
;
1432 ret
= regcomp (&re
, pattern
,
1433 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1438 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1439 printd ("msg regcomp failed `%.*s'", (int) size
, errbuf
);
1443 search (&re
, pageno
, y
, forward
);
1448 else if (!strncmp ("geometry", p
, 8)) {
1452 ret
= sscanf (p
+ 8, " %d %d %d", &w
, &h
, &fitmodel
);
1454 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1461 for (int i
= 0; i
< state
.texcount
; ++i
) {
1462 state
.texowners
[i
].slice
= NULL
;
1465 state
.fitmodel
= fitmodel
;
1470 unlock ("geometry");
1471 printd ("continue %d", state
.pagecount
);
1473 else if (!strncmp ("reqlayout", p
, 9)) {
1480 ret
= sscanf (p
+ 9, " %d %d %d %n",
1481 &rotate
, &fitmodel
, &h
, &off
);
1483 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1486 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1487 if (state
.rotate
!= rotate
|| state
.fitmodel
!= fitmodel
) {
1490 state
.rotate
= rotate
;
1491 state
.fitmodel
= fitmodel
;
1496 nameddest
= p
+ 9 + off
;
1497 if (pdf
&& nameddest
&& *nameddest
) {
1499 struct pagedim
*pdim
;
1500 int pageno
= pdf_lookup_anchor (state
.ctx
, pdf
, nameddest
,
1502 pdim
= pdimofpageno (pageno
);
1503 xy
= fz_transform_point (xy
, pdim
->ctm
);
1504 printd ("a %d %d %d", pageno
, (int) xy
.x
, (int) xy
.y
);
1508 unlock ("reqlayout");
1509 printd ("continue %d", state
.pagecount
);
1511 else if (!strncmp ("page", p
, 4)) {
1516 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1518 errx (1, "bad page line `%.*s' ret=%d", len
, p
, ret
);
1523 page
= loadpage (pageno
, pindex
);
1527 printd ("page %" PRIxPTR
" %f", (uintptr_t) page
, b
- a
);
1529 else if (!strncmp ("tile", p
, 4)) {
1536 ret
= sscanf (p
+ 4, " %" SCNxPTR
" %d %d %d %d %" SCNxPTR
,
1537 (uintptr_t *) &page
, &x
, &y
, &w
, &h
,
1538 (uintptr_t *) &data
);
1540 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1545 tile
= rendertile (page
, x
, y
, w
, h
, data
);
1549 printd ("tile %d %d %" PRIxPTR
" %u %f",
1550 x
, y
, (uintptr_t) (tile
),
1551 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1554 else if (!strncmp ("trimset", p
, 7)) {
1558 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1559 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1561 errx (1, "malformed trimset `%.*s' ret=%d", len
, p
, ret
);
1564 state
.trimmargins
= trimmargins
;
1565 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1567 state
.trimfuzz
= fuzz
;
1571 else if (!strncmp ("settrim", p
, 7)) {
1575 ret
= sscanf (p
+ 7, " %d %d %d %d %d",
1576 &trimmargins
, &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1578 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1582 state
.trimmargins
= trimmargins
;
1583 state
.needoutline
= 1;
1584 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1586 state
.trimfuzz
= fuzz
;
1588 state
.pagedimcount
= 0;
1589 free (state
.pagedims
);
1590 state
.pagedims
= NULL
;
1595 printd ("continue %d", state
.pagecount
);
1597 else if (!strncmp ("sliceh", p
, 6)) {
1600 ret
= sscanf (p
+ 6, " %d", &h
);
1602 errx (1, "malformed sliceh `%.*s' ret=%d", len
, p
, ret
);
1604 if (h
!= state
.sliceheight
) {
1605 state
.sliceheight
= h
;
1606 for (int i
= 0; i
< state
.texcount
; ++i
) {
1607 state
.texowners
[i
].w
= -1;
1608 state
.texowners
[i
].h
= -1;
1609 state
.texowners
[i
].slice
= NULL
;
1613 else if (!strncmp ("interrupt", p
, 9)) {
1614 printd ("vmsg interrupted");
1617 errx (1, "unknown command %.*s", len
, p
);
1623 ML (isexternallink (value uri_v
))
1626 int ext
= fz_is_external_link (state
.ctx
, String_val (uri_v
));
1627 CAMLreturn (Val_bool (ext
));
1630 ML (uritolocation (value uri_v
))
1636 struct pagedim
*pdim
;
1638 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, String_val (uri_v
),
1640 pdim
= pdimofpageno (pageno
);
1641 xy
= fz_transform_point (xy
, pdim
->ctm
);
1642 ret_v
= caml_alloc_tuple (3);
1643 Field (ret_v
, 0) = Val_int (pageno
);
1644 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1645 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1649 ML (realloctexts (value texcount_v
))
1651 CAMLparam1 (texcount_v
);
1654 if (trylock (__func__
)) {
1658 realloctexts (Int_val (texcount_v
));
1663 CAMLreturn (Val_bool (ok
));
1666 static void recti (int x0
, int y0
, int x1
, int y1
)
1668 GLfloat
*v
= state
.vertices
;
1670 glVertexPointer (2, GL_FLOAT
, 0, v
);
1671 v
[0] = x0
; v
[1] = y0
;
1672 v
[2] = x1
; v
[3] = y0
;
1673 v
[4] = x0
; v
[5] = y1
;
1674 v
[6] = x1
; v
[7] = y1
;
1675 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1678 static void showsel (struct page
*page
, int ox
, int oy
)
1682 fz_stext_block
*block
;
1684 unsigned char selcolor
[] = {15,15,15,140};
1686 if (!page
->fmark
|| !page
->lmark
) return;
1688 glEnable (GL_BLEND
);
1689 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1690 glColor4ubv (selcolor
);
1692 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1693 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1695 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1696 fz_stext_line
*line
;
1698 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1699 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1702 rect
= fz_empty_rect
;
1703 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1705 if (ch
== page
->fmark
) seen
= 1;
1706 r
= fz_rect_from_quad (ch
->quad
);
1707 if (seen
) rect
= fz_union_rect (rect
, r
);
1708 if (ch
== page
->lmark
) {
1709 bbox
= fz_round_rect (rect
);
1710 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1711 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1715 bbox
= fz_round_rect (rect
);
1716 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1717 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1721 glDisable (GL_BLEND
);
1724 #pragma GCC diagnostic push
1725 #pragma GCC diagnostic ignored "-Wconversion"
1727 #pragma GCC diagnostic pop
1729 static void stipplerect (fz_matrix m
,
1737 p1
= fz_transform_point (p1
, m
);
1738 p2
= fz_transform_point (p2
, m
);
1739 p3
= fz_transform_point (p3
, m
);
1740 p4
= fz_transform_point (p4
, m
);
1746 t
= hypotf (w
, h
) * .25f
;
1750 s
= hypotf (w
, h
) * .25f
;
1752 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1753 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1755 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
1756 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
1758 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
1759 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
1761 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
1762 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
1764 glDrawArrays (GL_LINES
, 0, 8);
1767 static void solidrect (fz_matrix m
,
1774 p1
= fz_transform_point (p1
, m
);
1775 p2
= fz_transform_point (p2
, m
);
1776 p3
= fz_transform_point (p3
, m
);
1777 p4
= fz_transform_point (p4
, m
);
1778 vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1779 vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1781 vertices
[4] = p3
.x
; vertices
[5] = p3
.y
;
1782 vertices
[6] = p4
.x
; vertices
[7] = p4
.y
;
1783 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
1786 static void ensurelinks (struct page
*page
)
1789 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
1792 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1796 GLfloat
*texcoords
= state
.texcoords
;
1797 GLfloat
*vertices
= state
.vertices
;
1801 glEnable (GL_TEXTURE_1D
);
1802 glEnable (GL_BLEND
);
1803 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1804 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
1806 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1807 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1808 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1810 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
1811 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
1813 for (link
= page
->links
; link
; link
= link
->next
) {
1814 fz_point p1
, p2
, p3
, p4
;
1816 p1
.x
= link
->rect
.x0
;
1817 p1
.y
= link
->rect
.y0
;
1819 p2
.x
= link
->rect
.x1
;
1820 p2
.y
= link
->rect
.y0
;
1822 p3
.x
= link
->rect
.x1
;
1823 p3
.y
= link
->rect
.y1
;
1825 p4
.x
= link
->rect
.x0
;
1826 p4
.y
= link
->rect
.y1
;
1828 /* TODO: different colours for different schemes */
1829 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
1830 else glColor3ub (255, 0, 0);
1832 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1835 for (int i
= 0; i
< page
->annotcount
; ++i
) {
1836 fz_point p1
, p2
, p3
, p4
;
1837 struct annot
*annot
= &page
->annots
[i
];
1839 p1
.x
= annot
->bbox
.x0
;
1840 p1
.y
= annot
->bbox
.y0
;
1842 p2
.x
= annot
->bbox
.x1
;
1843 p2
.y
= annot
->bbox
.y0
;
1845 p3
.x
= annot
->bbox
.x1
;
1846 p3
.y
= annot
->bbox
.y1
;
1848 p4
.x
= annot
->bbox
.x0
;
1849 p4
.y
= annot
->bbox
.y1
;
1851 glColor3ub (0, 0, 128);
1852 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1855 glDisable (GL_BLEND
);
1856 glDisable (GL_TEXTURE_1D
);
1859 static int compareslinks (const void *l
, const void *r
)
1861 struct slink
const *ls
= l
;
1862 struct slink
const *rs
= r
;
1863 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1864 return ls
->bbox
.x0
- rs
->bbox
.x0
;
1866 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1869 static void droptext (struct page
*page
)
1872 fz_drop_stext_page (state
.ctx
, page
->text
);
1879 static void dropannots (struct page
*page
)
1882 free (page
->annots
);
1883 page
->annots
= NULL
;
1884 page
->annotcount
= 0;
1888 static void ensureannots (struct page
*page
)
1891 size_t annotsize
= sizeof (*page
->annots
);
1896 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1899 pdfpage
= pdf_page_from_fz_page (state
.ctx
, page
->fzpage
);
1900 if (state
.gen
!= page
->agen
) {
1902 page
->agen
= state
.gen
;
1904 if (page
->annots
) return;
1906 for (annot
= pdf_first_annot (state
.ctx
, pdfpage
);
1908 annot
= pdf_next_annot (state
.ctx
, annot
)) {
1913 page
->annotcount
= count
;
1914 page
->annots
= calloc (count
, annotsize
);
1915 if (!page
->annots
) {
1916 err (1, "calloc annots %d", count
);
1919 for (annot
= pdf_first_annot (state
.ctx
, pdfpage
), i
= 0;
1921 annot
= pdf_next_annot (state
.ctx
, annot
), i
++) {
1924 rect
= pdf_bound_annot (state
.ctx
, annot
);
1925 page
->annots
[i
].annot
= annot
;
1926 page
->annots
[i
].bbox
= fz_round_rect (rect
);
1931 static void dropslinks (struct page
*page
)
1934 free (page
->slinks
);
1935 page
->slinks
= NULL
;
1936 page
->slinkcount
= 0;
1939 fz_drop_link (state
.ctx
, page
->links
);
1944 static void ensureslinks (struct page
*page
)
1948 size_t slinksize
= sizeof (*page
->slinks
);
1951 ensureannots (page
);
1952 if (state
.gen
!= page
->sgen
) {
1954 page
->sgen
= state
.gen
;
1956 if (page
->slinks
) return;
1959 ctm
= pagectm (page
);
1961 count
= page
->annotcount
;
1962 for (link
= page
->links
; link
; link
= link
->next
) {
1968 page
->slinkcount
= count
;
1969 page
->slinks
= calloc (count
, slinksize
);
1970 if (!page
->slinks
) {
1971 err (1, "calloc slinks %d", count
);
1974 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
1978 rect
= fz_transform_rect (rect
, ctm
);
1979 page
->slinks
[i
].tag
= SLINK
;
1980 page
->slinks
[i
].u
.link
= link
;
1981 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
1983 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
1985 rect
= pdf_bound_annot (state
.ctx
, page
->annots
[j
].annot
);
1986 rect
= fz_transform_rect (rect
, ctm
);
1987 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
1989 page
->slinks
[i
].tag
= SANNOT
;
1990 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
1992 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
1996 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
1997 int noff
, const char *targ
,
1998 mlsize_t tlen
, int hfsize
)
2001 struct slink
*slink
;
2002 float x0
, y0
, x1
, y1
, w
;
2004 ensureslinks (page
);
2005 glColor3ub (0xc3, 0xb0, 0x91);
2006 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2007 fmt_linkn (buf
, i
+ noff
);
2008 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2009 slink
= &page
->slinks
[i
];
2011 x0
= slink
->bbox
.x0
+ xoff
- 5;
2012 y1
= slink
->bbox
.y0
+ yoff
- 5;
2013 y0
= y1
+ 10 + hfsize
;
2014 w
= measure_string (state
.face
, hfsize
, buf
);
2016 recti ((int) x0
, (int) y0
, (int) x1
, (int) y1
);
2020 glEnable (GL_BLEND
);
2021 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2022 glEnable (GL_TEXTURE_2D
);
2023 glColor3ub (0, 0, 0);
2024 for (int i
= 0; i
< page
->slinkcount
; ++i
) {
2025 fmt_linkn (buf
, i
+ noff
);
2026 if (!tlen
|| !strncmp (targ
, buf
, tlen
)) {
2027 slink
= &page
->slinks
[i
];
2029 x0
= slink
->bbox
.x0
+ xoff
;
2030 y0
= slink
->bbox
.y0
+ yoff
+ hfsize
;
2031 draw_string (state
.face
, hfsize
, x0
, y0
, buf
);
2034 glDisable (GL_TEXTURE_2D
);
2035 glDisable (GL_BLEND
);
2038 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
2041 struct slice
*slice1
;
2042 unsigned char *texdata
;
2045 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
2046 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
2048 if (slice
->texindex
!= -1 && slice
->texindex
< state
.texcount
2049 && state
.texowners
[slice
->texindex
].slice
== slice
) {
2050 glBindTexture (TEXT_TYPE
, state
.texids
[slice
->texindex
]);
2054 int texindex
= state
.texindex
++ % state
.texcount
;
2056 if (state
.texowners
[texindex
].w
== tile
->w
) {
2057 if (state
.texowners
[texindex
].h
>= slice
->h
) {
2061 state
.texowners
[texindex
].h
= slice
->h
;
2065 state
.texowners
[texindex
].h
= slice
->h
;
2068 state
.texowners
[texindex
].w
= tile
->w
;
2069 state
.texowners
[texindex
].slice
= slice
;
2070 slice
->texindex
= texindex
;
2072 glBindTexture (TEXT_TYPE
, state
.texids
[texindex
]);
2073 #if TEXT_TYPE == GL_TEXTURE_2D
2074 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
2075 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
2076 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
2077 glTexParameteri (TEXT_TYPE
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
2080 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
2084 texdata
= tile
->pixmap
->samples
;
2087 glTexSubImage2D (TEXT_TYPE
,
2099 glTexImage2D (TEXT_TYPE
,
2111 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
2116 ML0 (begintiles (void))
2118 glEnable (TEXT_TYPE
);
2119 glTexCoordPointer (2, GL_FLOAT
, 0, state
.texcoords
);
2120 glVertexPointer (2, GL_FLOAT
, 0, state
.vertices
);
2123 ML0 (endtiles (void))
2125 glDisable (TEXT_TYPE
);
2128 ML0 (drawtile (value args_v
, value ptr_v
))
2130 CAMLparam2 (args_v
, ptr_v
);
2131 int dispx
= Int_val (Field (args_v
, 0));
2132 int dispy
= Int_val (Field (args_v
, 1));
2133 int dispw
= Int_val (Field (args_v
, 2));
2134 int disph
= Int_val (Field (args_v
, 3));
2135 int tilex
= Int_val (Field (args_v
, 4));
2136 int tiley
= Int_val (Field (args_v
, 5));
2137 const char *s
= String_val (ptr_v
);
2138 struct tile
*tile
= parse_pointer (__func__
, s
);
2139 int slicey
, firstslice
;
2140 struct slice
*slice
;
2141 GLfloat
*texcoords
= state
.texcoords
;
2142 GLfloat
*vertices
= state
.vertices
;
2144 firstslice
= tiley
/ tile
->sliceheight
;
2145 slice
= &tile
->slices
[firstslice
];
2146 slicey
= tiley
% tile
->sliceheight
;
2151 dh
= slice
->h
- slicey
;
2152 dh
= fz_mini (disph
, dh
);
2153 uploadslice (tile
, slice
);
2155 texcoords
[0] = tilex
; texcoords
[1] = slicey
;
2156 texcoords
[2] = tilex
+dispw
; texcoords
[3] = slicey
;
2157 texcoords
[4] = tilex
; texcoords
[5] = slicey
+dh
;
2158 texcoords
[6] = tilex
+dispw
; texcoords
[7] = slicey
+dh
;
2160 vertices
[0] = dispx
; vertices
[1] = dispy
;
2161 vertices
[2] = dispx
+dispw
; vertices
[3] = dispy
;
2162 vertices
[4] = dispx
; vertices
[5] = dispy
+dh
;
2163 vertices
[6] = dispx
+dispw
; vertices
[7] = dispy
+dh
;
2165 #if TEXT_TYPE == GL_TEXTURE_2D
2166 for (int i
= 0; i
< 8; ++i
) {
2167 texcoords
[i
] /= ((i
& 1) == 0 ? tile
->w
: slice
->h
);
2171 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
2175 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
2181 static void drawprect (struct page
*page
, int xoff
, int yoff
, value rects_v
)
2184 fz_point p1
, p2
, p3
, p4
;
2185 GLfloat
*vertices
= state
.vertices
;
2187 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
2188 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
2189 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
2191 glEnable (GL_BLEND
);
2192 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
2195 Double_array_field (rects_v
, 0),
2196 Double_array_field (rects_v
, 1),
2197 Double_array_field (rects_v
, 2),
2198 Double_array_field (rects_v
, 3)
2200 p1
.x
= (float) Double_array_field (rects_v
, 4);
2201 p1
.y
= (float) Double_array_field (rects_v
, 5);
2203 p2
.x
= (float) Double_array_field (rects_v
, 6);
2207 p3
.y
= (float) Double_array_field (rects_v
, 7);
2211 solidrect (ctm
, p1
, p2
, p3
, p4
, vertices
);
2212 glDisable (GL_BLEND
);
2215 ML (postprocess (value ptr_v
, value hlinks_v
,
2216 value xoff_v
, value yoff_v
,
2219 CAMLparam5 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
, li_v
);
2220 int xoff
= Int_val (xoff_v
);
2221 int yoff
= Int_val (yoff_v
);
2222 int noff
= Int_val (Field (li_v
, 0));
2223 const char *targ
= String_val (Field (li_v
, 1));
2224 mlsize_t tlen
= caml_string_length (Field (li_v
, 1));
2225 int hfsize
= Int_val (Field (li_v
, 2));
2226 const char *s
= String_val (ptr_v
);
2227 int hlmask
= Int_val (hlinks_v
);
2228 struct page
*page
= parse_pointer (__func__
, s
);
2230 if (!page
->fzpage
) {
2231 /* deal with loadpage failed pages */
2235 if (trylock (__func__
)) {
2240 ensureannots (page
);
2241 if (hlmask
& 1) highlightlinks (page
, xoff
, yoff
);
2243 highlightslinks (page
, xoff
, yoff
, noff
, targ
, tlen
, hfsize
);
2244 noff
= page
->slinkcount
;
2246 if (page
->tgen
== state
.gen
) {
2247 showsel (page
, xoff
, yoff
);
2252 CAMLreturn (Val_int (noff
));
2255 ML0 (drawprect (value ptr_v
, value xoff_v
, value yoff_v
, value rects_v
))
2257 CAMLparam4 (ptr_v
, xoff_v
, yoff_v
, rects_v
);
2258 int xoff
= Int_val (xoff_v
);
2259 int yoff
= Int_val (yoff_v
);
2260 const char *s
= String_val (ptr_v
);
2261 struct page
*page
= parse_pointer (__func__
, s
);
2263 drawprect (page
, xoff
, yoff
, rects_v
);
2267 static struct annot
*getannot (struct page
*page
, int x
, int y
)
2271 const fz_matrix
*tctm
;
2272 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2274 if (!page
->annots
) return NULL
;
2277 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
2278 tctm
= &state
.pagedims
[page
->pdimno
].tctm
;
2281 tctm
= &fz_identity
;
2287 ctm
= fz_concat (*tctm
, state
.pagedims
[page
->pdimno
].ctm
);
2288 ctm
= fz_invert_matrix (ctm
);
2289 p
= fz_transform_point (p
, ctm
);
2292 for (int i
= 0; i
< page
->annotcount
; ++i
) {
2293 struct annot
*a
= &page
->annots
[i
];
2296 rect
= pdf_bound_annot (state
.ctx
, a
->annot
);
2297 if (p
.x
>= rect
.x0
&& p
.x
<= rect
.x1
) {
2298 if (p
.y
>= rect
.y0
&& p
.y
<= rect
.y1
)
2306 static fz_link
*getlink (struct page
*page
, int x
, int y
)
2311 ensureslinks (page
);
2316 p
= fz_transform_point (p
, fz_invert_matrix (pagectm (page
)));
2318 for (link
= page
->links
; link
; link
= link
->next
) {
2319 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
2320 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
2328 static void ensuretext (struct page
*page
)
2330 if (state
.gen
!= page
->tgen
) {
2332 page
->tgen
= state
.gen
;
2337 page
->text
= fz_new_stext_page (state
.ctx
,
2338 state
.pagedims
[page
->pdimno
].mediabox
);
2339 tdev
= fz_new_stext_device (state
.ctx
, page
->text
, 0);
2340 fz_run_display_list (state
.ctx
, page
->dlist
,
2341 tdev
, pagectm (page
), fz_infinite_rect
, NULL
);
2342 fz_close_device (state
.ctx
, tdev
);
2343 fz_drop_device (state
.ctx
, tdev
);
2347 ML (find_page_with_links (value start_page_v
, value dir_v
))
2349 CAMLparam2 (start_page_v
, dir_v
);
2351 int i
, dir
= Int_val (dir_v
);
2352 int start_page
= Int_val (start_page_v
);
2353 int end_page
= dir
> 0 ? state
.pagecount
: -1;
2357 ret_v
= Val_int (0);
2359 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2360 for (i
= start_page
+ dir
; i
!= end_page
; i
+= dir
) {
2365 pdf_page
*page
= NULL
;
2368 fz_try (state
.ctx
) {
2369 page
= pdf_load_page (state
.ctx
, pdf
, i
);
2370 found
= !!page
->links
|| !!page
->annots
;
2372 fz_catch (state
.ctx
) {
2376 fz_drop_page (state
.ctx
, &page
->super
);
2380 fz_page
*page
= fz_load_page (state
.ctx
, state
.doc
, i
);
2381 fz_link
*link
= fz_load_links (state
.ctx
, page
);
2383 fz_drop_link (state
.ctx
, link
);
2384 fz_drop_page (state
.ctx
, page
);
2388 ret_v
= caml_alloc_small (1, 1);
2389 Field (ret_v
, 0) = Val_int (i
);
2398 enum { dir_first
, dir_last
};
2399 enum { dir_first_visible
, dir_left
, dir_right
, dir_down
, dir_up
};
2401 ML (findlink (value ptr_v
, value dir_v
))
2403 CAMLparam2 (ptr_v
, dir_v
);
2404 CAMLlocal2 (ret_v
, pos_v
);
2406 int dirtag
, i
, slinkindex
;
2407 struct slink
*found
= NULL
,*slink
;
2408 const char *s
= String_val (ptr_v
);
2410 page
= parse_pointer (__func__
, s
);
2411 ret_v
= Val_int (0);
2413 ensureslinks (page
);
2415 if (Is_block (dir_v
)) {
2416 dirtag
= Tag_val (dir_v
);
2418 case dir_first_visible
:
2420 int x0
, y0
, dir
, first_index
, last_index
;
2422 pos_v
= Field (dir_v
, 0);
2423 x0
= Int_val (Field (pos_v
, 0));
2424 y0
= Int_val (Field (pos_v
, 1));
2425 dir
= Int_val (Field (pos_v
, 2));
2430 last_index
= page
->slinkcount
;
2433 first_index
= page
->slinkcount
- 1;
2437 for (i
= first_index
; i
!= last_index
; i
+= dir
) {
2438 slink
= &page
->slinks
[i
];
2439 if (slink
->bbox
.y0
>= y0
&& slink
->bbox
.x0
>= x0
) {
2448 slinkindex
= Int_val (Field (dir_v
, 0));
2449 found
= &page
->slinks
[slinkindex
];
2450 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2451 slink
= &page
->slinks
[i
];
2452 if (slink
->bbox
.x0
< found
->bbox
.x0
) {
2460 slinkindex
= Int_val (Field (dir_v
, 0));
2461 found
= &page
->slinks
[slinkindex
];
2462 for (i
= slinkindex
+ 1; i
< page
->slinkcount
; ++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
.y0
>= found
->bbox
.y0
) {
2484 slinkindex
= Int_val (Field (dir_v
, 0));
2485 found
= &page
->slinks
[slinkindex
];
2486 for (i
= slinkindex
- 1; i
>= 0; --i
) {
2487 slink
= &page
->slinks
[i
];
2488 if (slink
->bbox
.y0
<= found
->bbox
.y0
) {
2497 dirtag
= Int_val (dir_v
);
2500 found
= page
->slinks
;
2505 found
= page
->slinks
+ (page
->slinkcount
- 1);
2511 ret_v
= caml_alloc_small (2, 1);
2512 Field (ret_v
, 0) = Val_int (found
- page
->slinks
);
2519 enum { uuri
, utext
, uannot
};
2521 ML (getlink (value ptr_v
, value n_v
))
2523 CAMLparam2 (ptr_v
, n_v
);
2524 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2527 const char *s
= String_val (ptr_v
);
2528 struct slink
*slink
;
2530 ret_v
= Val_int (0);
2531 page
= parse_pointer (__func__
, s
);
2534 ensureslinks (page
);
2535 slink
= &page
->slinks
[Int_val (n_v
)];
2536 if (slink
->tag
== SLINK
) {
2537 link
= slink
->u
.link
;
2538 str_v
= caml_copy_string (link
->uri
);
2539 ret_v
= caml_alloc_small (1, uuri
);
2540 Field (ret_v
, 0) = str_v
;
2543 ret_v
= caml_alloc_small (1, uannot
);
2544 tup_v
= caml_alloc_tuple (2);
2545 Field (ret_v
, 0) = tup_v
;
2546 Field (tup_v
, 0) = ptr_v
;
2547 Field (tup_v
, 1) = n_v
;
2554 ML (getannotcontents (value ptr_v
, value n_v
))
2556 CAMLparam2 (ptr_v
, n_v
);
2559 const char *contents
= "";
2562 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
2564 const char *s
= String_val (ptr_v
);
2566 struct slink
*slink
;
2568 page
= parse_pointer (__func__
, s
);
2569 slink
= &page
->slinks
[Int_val (n_v
)];
2570 contents
= pdf_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
);
2573 ret_v
= caml_copy_string (contents
);
2577 ML (getlinkcount (value ptr_v
))
2581 const char *s
= String_val (ptr_v
);
2583 page
= parse_pointer (__func__
, s
);
2584 CAMLreturn (Val_int (page
->slinkcount
));
2587 ML (getlinkrect (value ptr_v
, value n_v
))
2589 CAMLparam2 (ptr_v
, n_v
);
2592 struct slink
*slink
;
2593 const char *s
= String_val (ptr_v
);
2595 page
= parse_pointer (__func__
, s
);
2596 ret_v
= caml_alloc_tuple (4);
2598 ensureslinks (page
);
2600 slink
= &page
->slinks
[Int_val (n_v
)];
2601 Field (ret_v
, 0) = Val_int (slink
->bbox
.x0
);
2602 Field (ret_v
, 1) = Val_int (slink
->bbox
.y0
);
2603 Field (ret_v
, 2) = Val_int (slink
->bbox
.x1
);
2604 Field (ret_v
, 3) = Val_int (slink
->bbox
.y1
);
2609 ML (whatsunder (value ptr_v
, value x_v
, value y_v
))
2611 CAMLparam3 (ptr_v
, x_v
, y_v
);
2612 CAMLlocal4 (ret_v
, tup_v
, str_v
, gr_v
);
2614 struct annot
*annot
;
2616 const char *ptr
= String_val (ptr_v
);
2617 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2618 struct pagedim
*pdim
;
2620 ret_v
= Val_int (0);
2621 if (trylock (__func__
)) {
2625 page
= parse_pointer (__func__
, ptr
);
2626 pdim
= &state
.pagedims
[page
->pdimno
];
2627 x
+= pdim
->bounds
.x0
;
2628 y
+= pdim
->bounds
.y0
;
2631 annot
= getannot (page
, x
, y
);
2635 ensureslinks (page
);
2636 for (i
= 0; i
< page
->slinkcount
; ++i
) {
2637 if (page
->slinks
[i
].tag
== SANNOT
2638 && page
->slinks
[i
].u
.annot
== annot
->annot
) {
2643 ret_v
= caml_alloc_small (1, uannot
);
2644 tup_v
= caml_alloc_tuple (2);
2645 Field (ret_v
, 0) = tup_v
;
2646 Field (tup_v
, 0) = ptr_v
;
2647 Field (tup_v
, 1) = Val_int (n
);
2652 link
= getlink (page
, x
, y
);
2654 str_v
= caml_copy_string (link
->uri
);
2655 ret_v
= caml_alloc_small (1, uuri
);
2656 Field (ret_v
, 0) = str_v
;
2660 fz_stext_block
*block
;
2664 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2665 fz_stext_line
*line
;
2667 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2669 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2672 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2676 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2679 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2680 fz_quad
*q
= &ch
->quad
;
2682 if (x
>= q
->ul
.x
&& x
<= q
->ur
.x
2683 && y
>= q
->ul
.y
&& y
<= q
->ll
.y
) {
2684 const char *n2
= fz_font_name (state
.ctx
, ch
->font
);
2685 FT_FaceRec
*face
= fz_font_ft_face (state
.ctx
,
2688 if (!n2
) n2
= "<unknown font>";
2690 if (face
&& face
->family_name
) {
2692 char *n1
= face
->family_name
;
2693 size_t l1
= strlen (n1
);
2694 size_t l2
= strlen (n2
);
2696 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
2697 s
= malloc (l1
+ l2
+ 2);
2701 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
2702 str_v
= caml_copy_string (s
);
2707 if (str_v
== Val_unit
) {
2708 str_v
= caml_copy_string (n2
);
2710 ret_v
= caml_alloc_small (1, utext
);
2711 Field (ret_v
, 0) = str_v
;
2725 enum { mark_page
, mark_block
, mark_line
, mark_word
};
2727 ML0 (clearmark (value ptr_v
))
2730 const char *s
= String_val (ptr_v
);
2733 if (trylock (__func__
)) {
2737 page
= parse_pointer (__func__
, s
);
2746 static int uninteresting (int c
)
2748 return isspace (c
) || ispunct (c
);
2751 ML (markunder (value ptr_v
, value x_v
, value y_v
, value mark_v
))
2753 CAMLparam4 (ptr_v
, x_v
, y_v
, mark_v
);
2757 fz_stext_line
*line
;
2758 fz_stext_block
*block
;
2759 struct pagedim
*pdim
;
2760 int mark
= Int_val (mark_v
);
2761 const char *s
= String_val (ptr_v
);
2762 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2764 ret_v
= Val_bool (0);
2765 if (trylock (__func__
)) {
2769 page
= parse_pointer (__func__
, s
);
2770 pdim
= &state
.pagedims
[page
->pdimno
];
2774 if (mark
== mark_page
) {
2775 page
->fmark
= page
->text
->first_block
->u
.t
.first_line
->first_char
;
2776 page
->lmark
= page
->text
->last_block
->u
.t
.last_line
->last_char
;
2777 ret_v
= Val_bool (1);
2781 x
+= pdim
->bounds
.x0
;
2782 y
+= pdim
->bounds
.y0
;
2784 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2785 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2787 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2790 if (mark
== mark_block
) {
2791 page
->fmark
= block
->u
.t
.first_line
->first_char
;
2792 page
->lmark
= block
->u
.t
.last_line
->last_char
;
2793 ret_v
= Val_bool (1);
2797 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2801 if (!(x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
))
2804 if (mark
== mark_line
) {
2805 page
->fmark
= line
->first_char
;
2806 page
->lmark
= line
->last_char
;
2807 ret_v
= Val_bool (1);
2811 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2812 fz_stext_char
*ch2
, *first
= NULL
, *last
= NULL
;
2813 fz_quad
*q
= &ch
->quad
;
2814 if (x
>= q
->ul
.x
&& x
<= q
->ur
.x
2815 && y
>= q
->ul
.y
&& y
<= q
->ll
.y
) {
2816 for (ch2
= line
->first_char
; ch2
!= ch
; ch2
= ch2
->next
) {
2817 if (uninteresting (ch2
->c
)) first
= NULL
;
2818 else if (!first
) first
= ch2
;
2820 for (ch2
= ch
; ch2
; ch2
= ch2
->next
) {
2821 if (uninteresting (ch2
->c
)) break;
2825 page
->fmark
= first
;
2827 ret_v
= Val_bool (1);
2834 if (!Bool_val (ret_v
)) {
2844 ML (rectofblock (value ptr_v
, value x_v
, value y_v
))
2846 CAMLparam3 (ptr_v
, x_v
, y_v
);
2847 CAMLlocal2 (ret_v
, res_v
);
2850 struct pagedim
*pdim
;
2851 fz_stext_block
*block
;
2852 const char *s
= String_val (ptr_v
);
2853 int x
= Int_val (x_v
), y
= Int_val (y_v
);
2855 ret_v
= Val_int (0);
2856 if (trylock (__func__
)) {
2860 page
= parse_pointer (__func__
, s
);
2861 pdim
= &state
.pagedims
[page
->pdimno
];
2862 x
+= pdim
->bounds
.x0
;
2863 y
+= pdim
->bounds
.y0
;
2867 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2868 switch (block
->type
) {
2869 case FZ_STEXT_BLOCK_TEXT
:
2873 case FZ_STEXT_BLOCK_IMAGE
:
2881 if (x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)
2886 res_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
2887 ret_v
= caml_alloc_small (1, 1);
2888 Store_double_field (res_v
, 0, (double) b
->x0
);
2889 Store_double_field (res_v
, 1, (double) b
->x1
);
2890 Store_double_field (res_v
, 2, (double) b
->y0
);
2891 Store_double_field (res_v
, 3, (double) b
->y1
);
2892 Field (ret_v
, 0) = res_v
;
2900 ML0 (seltext (value ptr_v
, value rect_v
))
2902 CAMLparam2 (ptr_v
, rect_v
);
2904 struct pagedim
*pdim
;
2905 const char *s
= String_val (ptr_v
);
2908 fz_stext_line
*line
;
2909 fz_stext_block
*block
;
2910 fz_stext_char
*fc
, *lc
;
2912 if (trylock (__func__
)) {
2916 page
= parse_pointer (__func__
, s
);
2919 pdim
= &state
.pagedims
[page
->pdimno
];
2920 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
2921 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
2922 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
2923 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
2936 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
2937 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
2938 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
2939 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
2940 fz_quad q
= ch
->quad
;
2941 if (x0
>= q
.ul
.x
&& x0
<= q
.ur
.x
2942 && y0
>= q
.ul
.y
&& y0
<= q
.ll
.y
) {
2945 if (x1
>= q
.ul
.x
&& x1
<= q
.ur
.x
2946 && y1
>= q
.ul
.y
&& y1
<= q
.ll
.y
) {
2952 if (x1
< x0
&& fc
== lc
) {
2969 static int pipechar (FILE *f
, fz_stext_char
*ch
)
2975 len
= fz_runetochar (buf
, ch
->c
);
2976 ret
= fwrite (buf
, len
, 1, f
);
2978 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
2979 len
, ret
, errno
, strerror (errno
));
2985 ML (spawn (value command_v
, value fds_v
))
2987 CAMLparam2 (command_v
, fds_v
);
2988 CAMLlocal2 (l_v
, tup_v
);
2990 pid_t pid
= (pid_t
) -1;
2992 value earg_v
= Nothing
;
2993 posix_spawnattr_t attr
;
2994 posix_spawn_file_actions_t fa
;
2995 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
2997 argv
[2] = &Byte (command_v
, 0);
2998 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
2999 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3002 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3003 msg
= "posix_spawnattr_init";
3007 #ifdef POSIX_SPAWN_USEVFORK
3008 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3009 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3014 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3017 tup_v
= Field (l_v
, 0);
3018 fd1
= Int_val (Field (tup_v
, 0));
3019 fd2
= Int_val (Field (tup_v
, 1));
3021 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3022 msg
= "posix_spawn_file_actions_addclose";
3028 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3029 msg
= "posix_spawn_file_actions_adddup2";
3036 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3037 msg
= "posix_spawn";
3042 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3043 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3047 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3048 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3049 ret1
, strerror (ret1
));
3053 unix_error (ret
, msg
, earg_v
);
3055 CAMLreturn (Val_int (pid
));
3058 ML (hassel (value ptr_v
))
3063 const char *s
= String_val (ptr_v
);
3065 ret_v
= Val_bool (0);
3066 if (trylock (__func__
)) {
3070 page
= parse_pointer (__func__
, s
);
3071 ret_v
= Val_bool (page
->fmark
&& page
->lmark
);
3077 ML0 (copysel (value fd_v
, value ptr_v
))
3079 CAMLparam2 (fd_v
, ptr_v
);
3083 fz_stext_line
*line
;
3084 fz_stext_block
*block
;
3085 int fd
= Int_val (fd_v
);
3086 const char *s
= String_val (ptr_v
);
3088 if (trylock (__func__
)) {
3092 page
= parse_pointer (__func__
, s
);
3094 if (!page
->fmark
|| !page
->lmark
) {
3095 printd ("emsg nothing to copy on page %d", page
->pageno
);
3099 f
= fdopen (fd
, "w");
3101 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3102 fd
, errno
, strerror (errno
));
3106 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3107 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3108 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3110 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3111 if (seen
|| ch
== page
->fmark
) {
3114 if (ch
== page
->lmark
) goto close
;
3115 } while ((ch
= ch
->next
));
3120 if (seen
) fputc ('\n', f
);
3125 int ret
= fclose (f
);
3128 if (errno
!= ECHILD
) {
3129 printd ("emsg failed to close sel pipe: %d:%s",
3130 errno
, strerror (errno
));
3140 printd ("emsg failed to close sel pipe: %d:%s",
3141 errno
, strerror (errno
));
3147 ML (getpdimrect (value pagedimno_v
))
3149 CAMLparam1 (pagedimno_v
);
3151 int pagedimno
= Int_val (pagedimno_v
);
3154 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3155 if (trylock (__func__
)) {
3156 box
= fz_empty_rect
;
3159 box
= state
.pagedims
[pagedimno
].mediabox
;
3163 Store_double_field (ret_v
, 0, (double) box
.x0
);
3164 Store_double_field (ret_v
, 1, (double) box
.x1
);
3165 Store_double_field (ret_v
, 2, (double) box
.y0
);
3166 Store_double_field (ret_v
, 3, (double) box
.y1
);
3171 ML (zoom_for_height (value winw_v
, value winh_v
, value dw_v
, value cols_v
))
3173 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3179 float winw
= Int_val (winw_v
);
3180 float winh
= Int_val (winh_v
);
3181 float dw
= Int_val (dw_v
);
3182 float cols
= Int_val (cols_v
);
3183 float pw
= 1.0, ph
= 1.0;
3185 if (trylock (__func__
)) {
3189 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3190 float w
= p
->pagebox
.x1
/ cols
;
3191 float h
= p
->pagebox
.y1
;
3195 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3197 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3200 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3203 ret_v
= caml_copy_double ((double) zoom
);
3207 ML (getmaxw (value unit_v
))
3209 CAMLparam1 (unit_v
);
3215 if (trylock (__func__
)) {
3219 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3220 float w
= p
->pagebox
.x1
;
3221 maxw
= fz_max (maxw
, w
);
3226 ret_v
= caml_copy_double ((double) maxw
);
3230 ML (draw_string (value pt_v
, value x_v
, value y_v
, value string_v
))
3232 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3234 int pt
= Int_val(pt_v
);
3235 int x
= Int_val (x_v
);
3236 int y
= Int_val (y_v
);
3239 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
3240 ret_v
= caml_copy_double (w
);
3244 ML (measure_string (value pt_v
, value string_v
))
3246 CAMLparam2 (pt_v
, string_v
);
3248 int pt
= Int_val (pt_v
);
3251 w
= (double) measure_string (state
.face
, pt
, String_val (string_v
));
3252 ret_v
= caml_copy_double (w
);
3256 ML (getpagebox (value opaque_v
))
3258 CAMLparam1 (opaque_v
);
3263 const char *s
= String_val (opaque_v
);
3264 struct page
*page
= parse_pointer (__func__
, s
);
3266 ret_v
= caml_alloc_tuple (4);
3267 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3269 fz_run_page (state
.ctx
, page
->fzpage
, dev
, pagectm (page
), NULL
);
3271 fz_close_device (state
.ctx
, dev
);
3272 fz_drop_device (state
.ctx
, dev
);
3273 bbox
= fz_round_rect (rect
);
3274 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3275 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3276 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3277 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3282 ML0 (setaalevel (value level_v
))
3284 CAMLparam1 (level_v
);
3286 state
.aalevel
= Int_val (level_v
);
3290 ML0 (setpapercolor (value rgba_v
))
3292 CAMLparam1 (rgba_v
);
3294 state
.papercolor
[0] = (float) Double_val (Field (rgba_v
, 0));
3295 state
.papercolor
[1] = (float) Double_val (Field (rgba_v
, 1));
3296 state
.papercolor
[2] = (float) Double_val (Field (rgba_v
, 2));
3297 state
.papercolor
[3] = (float) Double_val (Field (rgba_v
, 3));
3301 value
ml_keysymtoutf8 (value keysym_v
);
3303 value
ml_keysymtoutf8 (value keysym_v
)
3305 CAMLparam1 (keysym_v
);
3307 unsigned short keysym
= Int_val (keysym_v
);
3309 extern long keysym2ucs (unsigned short);
3313 rune
= (Rune
) keysym2ucs (keysym
);
3314 len
= fz_runetochar (buf
, rune
);
3316 str_v
= caml_copy_string (buf
);
3320 value
ml_keysymtoutf8 (value keysym_v
)
3322 CAMLparam1 (keysym_v
);
3324 long ucs
= Long_val (keysym_v
);
3328 len
= fz_runetochar (buf
, (int) ucs
);
3330 str_v
= caml_copy_string (buf
);
3335 enum { piunknown
, pilinux
, pimacos
, pibsd
};
3337 ML (platform (value unit_v
))
3339 CAMLparam1 (unit_v
);
3340 CAMLlocal2 (tup_v
, arr_v
);
3341 int platid
= piunknown
;
3344 #if defined __linux__
3346 #elif defined __DragonFly__ || defined __FreeBSD__
3347 || defined __OpenBSD__
|| defined __NetBSD__
3349 #elif defined __APPLE__
3352 if (uname (&buf
)) err (1, "uname");
3354 tup_v
= caml_alloc_tuple (2);
3356 char const *sar
[] = {
3363 arr_v
= caml_copy_string_array (sar
);
3365 Field (tup_v
, 0) = Val_int (platid
);
3366 Field (tup_v
, 1) = arr_v
;
3370 ML0 (cloexec (value fd_v
))
3373 int fd
= Int_val (fd_v
);
3375 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3376 uerror ("fcntl", Nothing
);
3381 ML (getpbo (value w_v
, value h_v
, value cs_v
))
3383 CAMLparam2 (w_v
, h_v
);
3386 int w
= Int_val (w_v
);
3387 int h
= Int_val (h_v
);
3388 int cs
= Int_val (cs_v
);
3390 if (state
.bo_usable
) {
3391 pbo
= calloc (sizeof (*pbo
), 1);
3393 err (1, "calloc pbo");
3405 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3408 state
.glGenBuffersARB (1, &pbo
->id
);
3409 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3410 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3411 NULL
, GL_STREAM_DRAW
);
3412 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3414 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3416 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3417 state
.glDeleteBuffersARB (1, &pbo
->id
);
3419 ret_v
= caml_copy_string ("0");
3425 res
= snprintf (NULL
, 0, "%" PRIxPTR
, (uintptr_t) pbo
);
3427 err (1, "snprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3431 err (1, "malloc %d bytes failed", res
+1);
3433 res
= sprintf (s
, "%" PRIxPTR
, (uintptr_t) pbo
);
3435 err (1, "sprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3437 ret_v
= caml_copy_string (s
);
3442 ret_v
= caml_copy_string ("0");
3447 ML0 (freepbo (value s_v
))
3450 const char *s
= String_val (s_v
);
3451 struct tile
*tile
= parse_pointer (__func__
, s
);
3454 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3456 tile
->pbo
->ptr
= NULL
;
3457 tile
->pbo
->size
= -1;
3462 ML0 (unmappbo (value s_v
))
3465 const char *s
= String_val (s_v
);
3466 struct tile
*tile
= parse_pointer (__func__
, s
);
3469 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3470 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3471 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3473 tile
->pbo
->ptr
= NULL
;
3474 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3479 static void setuppbo (void)
3481 extern void (*wsigladdr (const char *name
)) (void);
3482 #pragma GCC diagnostic push
3484 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3486 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3487 state
.bo_usable
= GPA (glBindBufferARB
)
3488 && GPA (glUnmapBufferARB
)
3489 && GPA (glMapBufferARB
)
3490 && GPA (glBufferDataARB
)
3491 && GPA (glGenBuffersARB
)
3492 && GPA (glDeleteBuffersARB
);
3494 #pragma GCC diagnostic pop
3497 ML (bo_usable (void))
3499 return Val_bool (state
.bo_usable
);
3502 ML (unproject (value ptr_v
, value x_v
, value y_v
))
3504 CAMLparam3 (ptr_v
, x_v
, y_v
);
3505 CAMLlocal2 (ret_v
, tup_v
);
3507 const char *s
= String_val (ptr_v
);
3508 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3509 struct pagedim
*pdim
;
3512 page
= parse_pointer (__func__
, s
);
3513 pdim
= &state
.pagedims
[page
->pdimno
];
3515 ret_v
= Val_int (0);
3516 if (trylock (__func__
)) {
3520 p
.x
= x
+ pdim
->bounds
.x0
;
3521 p
.y
= y
+ pdim
->bounds
.y0
;
3523 p
= fz_transform_point (p
, fz_invert_matrix (fz_concat (pdim
->tctm
,
3526 tup_v
= caml_alloc_tuple (2);
3527 ret_v
= caml_alloc_small (1, 1);
3528 Field (tup_v
, 0) = Val_int (p
.x
);
3529 Field (tup_v
, 1) = Val_int (p
.y
);
3530 Field (ret_v
, 0) = tup_v
;
3537 ML (project (value ptr_v
, value pageno_v
, value pdimno_v
, value x_v
, value y_v
))
3539 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
3542 const char *s
= String_val (ptr_v
);
3543 int pageno
= Int_val (pageno_v
);
3544 int pdimno
= Int_val (pdimno_v
);
3545 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
3546 struct pagedim
*pdim
;
3550 ret_v
= Val_int (0);
3554 page
= loadpage (pageno
, pdimno
);
3557 page
= parse_pointer (__func__
, s
);
3559 pdim
= &state
.pagedims
[pdimno
];
3561 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3562 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3563 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3568 p
.x
= x
+ pdim
->bounds
.x0
;
3569 p
.y
= y
+ pdim
->bounds
.y0
;
3571 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
3572 p
= fz_transform_point (p
, ctm
);
3574 ret_v
= caml_alloc_tuple (2);
3575 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
3576 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
3585 ML0 (addannot (value ptr_v
, value x_v
, value y_v
, value contents_v
))
3587 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
3588 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3594 const char *s
= String_val (ptr_v
);
3596 page
= parse_pointer (__func__
, s
);
3597 annot
= pdf_create_annot (state
.ctx
,
3598 pdf_page_from_fz_page (state
.ctx
,
3601 r
.x0
= Int_val (x_v
) - 10;
3602 r
.y0
= Int_val (y_v
) - 10;
3605 pdf_set_annot_contents (state
.ctx
, annot
, String_val (contents_v
));
3606 pdf_set_annot_rect (state
.ctx
, annot
, r
);
3613 ML0 (delannot (value ptr_v
, value n_v
))
3615 CAMLparam2 (ptr_v
, n_v
);
3616 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3620 const char *s
= String_val (ptr_v
);
3621 struct slink
*slink
;
3623 page
= parse_pointer (__func__
, s
);
3624 slink
= &page
->slinks
[Int_val (n_v
)];
3625 pdf_delete_annot (state
.ctx
,
3626 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
3627 (pdf_annot
*) slink
->u
.annot
);
3633 ML0 (modannot (value ptr_v
, value n_v
, value str_v
))
3635 CAMLparam3 (ptr_v
, n_v
, str_v
);
3636 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3640 const char *s
= String_val (ptr_v
);
3641 struct slink
*slink
;
3643 page
= parse_pointer (__func__
, s
);
3644 slink
= &page
->slinks
[Int_val (n_v
)];
3645 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
3646 String_val (str_v
));
3652 ML (hasunsavedchanges (void))
3654 return Val_bool (state
.dirty
);
3657 ML0 (savedoc (value path_v
))
3659 CAMLparam1 (path_v
);
3660 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3663 pdf_save_document (state
.ctx
, pdf
, String_val (path_v
), NULL
);
3668 static void makestippletex (void)
3670 const char pixels
[] = "\xff\xff\0\0";
3671 glGenTextures (1, &state
.stid
);
3672 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3673 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3674 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3687 ML (fz_version (void))
3689 return caml_copy_string (FZ_VERSION
);
3692 ML (llpp_version (void))
3694 extern char llpp_version
[];
3695 return caml_copy_string (llpp_version
);
3698 static void diag_callback (void *user
, const char *message
)
3700 printd ("emsg %s %s", (char *) user
, message
);
3703 ML0 (init (value csock_v
, value params_v
))
3705 CAMLparam2 (csock_v
, params_v
);
3706 CAMLlocal2 (trim_v
, fuzz_v
);
3709 const char *fontpath
;
3713 state
.csock
= Int_val (csock_v
);
3714 state
.rotate
= Int_val (Field (params_v
, 0));
3715 state
.fitmodel
= Int_val (Field (params_v
, 1));
3716 trim_v
= Field (params_v
, 2);
3717 texcount
= Int_val (Field (params_v
, 3));
3718 state
.sliceheight
= Int_val (Field (params_v
, 4));
3719 mustoresize
= Int_val (Field (params_v
, 5));
3720 colorspace
= Int_val (Field (params_v
, 6));
3721 fontpath
= String_val (Field (params_v
, 7));
3726 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3727 if (setlocale (LC_CTYPE
, "")) {
3728 const char *cset
= nl_langinfo (CODESET
);
3729 state
.utf8cs
= !strcmp (cset
, "UTF-8");
3732 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
3736 if (caml_string_length (Field (params_v
, 8)) > 0) {
3737 state
.trimcachepath
= ystrdup (String_val (Field (params_v
, 8)));
3739 if (!state
.trimcachepath
) {
3740 printd ("emsg failed to strdup trimcachepath: %d:%s",
3741 errno
, strerror (errno
));
3745 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3746 fz_register_document_handlers (state
.ctx
);
3747 fz_set_error_callback (state
.ctx
, diag_callback
, "[e]");
3748 fz_set_warning_callback (state
.ctx
, diag_callback
, "[w]");
3750 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3751 fuzz_v
= Field (trim_v
, 1);
3752 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3753 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3754 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3755 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3757 set_tex_params (colorspace
);
3760 state
.face
= load_font (fontpath
);
3764 const unsigned char *data
;
3766 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
3767 state
.face
= load_builtin_font (data
, len
);
3769 if (!state
.face
) _exit (1);
3771 realloctexts (texcount
);
3775 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3777 errx (1, "pthread_create: %s", strerror (ret
));
3784 static void UNUSED_ATTR NO_OPTIMIZE_ATTR
refmacs (void) {}