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
94 #define FUSV(v) ((void *) String_val (v))
107 struct slice slices
[1];
118 fz_matrix ctm
, zoomctm
, tctm
;
122 enum { SLINK
, SANNOT
} tag
;
143 fz_display_list
*dlist
;
146 struct slink
*slinks
;
148 struct annot
*annots
;
149 fz_stext_char
*fmark
, *lmark
;
152 enum { FitWidth
, FitProportional
, FitPage
};
156 struct pagedim
*pagedims
;
171 fz_colorspace
*colorspace
;
202 void (*glBindBufferARB
) (GLenum
, GLuint
);
203 GLboolean (*glUnmapBufferARB
) (GLenum
);
204 void *(*glMapBufferARB
) (GLenum
, GLenum
);
205 void (*glBufferDataARB
) (GLenum
, GLsizei
, void *, GLenum
);
206 void (*glGenBuffersARB
) (GLsizei
, GLuint
*);
207 void (*glDeleteBuffersARB
) (GLsizei
, GLuint
*);
209 GLfloat texcoords
[8];
210 GLfloat vertices
[16];
221 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
223 static void lock (const char *cap
)
225 int ret
= pthread_mutex_lock (&mutex
);
227 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
231 static void unlock (const char *cap
)
233 int ret
= pthread_mutex_unlock (&mutex
);
235 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
239 static int trylock (const char *cap
)
241 int ret
= pthread_mutex_trylock (&mutex
);
242 if (ret
&& ret
!= EBUSY
) {
243 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
248 static int hasdata (void)
251 ret
= ioctl (state
.csock
, FIONREAD
, &avail
);
252 if (ret
) err (1, "hasdata: FIONREAD error ret=%d", ret
);
256 ML (hasdata (value fd_v
))
261 ret
= ioctl (Int_val (fd_v
), FIONREAD
, &avail
);
262 if (ret
) uerror ("ioctl (FIONREAD)", Nothing
);
263 CAMLreturn (Val_bool (avail
> 0));
266 static void readdata (int fd
, void *p
, int size
)
271 n
= read (fd
, p
, size
);
273 if (n
< 0 && errno
== EINTR
) goto again
;
274 if (!n
) errx (1, "EOF while reading");
275 errx (1, "read (fd %d, req %d, ret %zd)", fd
, size
, n
);
279 static void writedata (int fd
, char *p
, int size
)
282 uint32_t size4
= size
;
283 struct iovec iov
[2] = {
284 { .iov_base
= &size4
, .iov_len
= 4 },
285 { .iov_base
= p
, .iov_len
= size
}
289 n
= writev (fd
, iov
, 2);
290 if (n
< 0 && errno
== EINTR
) goto again
;
292 if (!n
) errx (1, "EOF while writing data");
293 err (1, "writev (fd %d, req %d, ret %zd)", fd
, size
+ 4, n
);
297 static int readlen (int fd
)
300 readdata (fd
, &u
, 4);
304 ML0 (wcmd (value fd_v
, value bytes_v
, value len_v
))
306 CAMLparam3 (fd_v
, bytes_v
, len_v
);
307 writedata (Int_val (fd_v
), &Byte (bytes_v
, 0), Int_val (len_v
));
311 ML (rcmd (value fd_v
))
314 CAMLlocal1 (strdata_v
);
315 int fd
= Int_val (fd_v
);
316 int len
= readlen (fd
);
317 strdata_v
= caml_alloc_string (len
);
318 readdata (fd
, FUSV (strdata_v
), len
);
319 CAMLreturn (strdata_v
);
322 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt
, ...)
325 int size
= sizeof (fbuf
), len
;
331 len
= vsnprintf (buf
, size
, fmt
, ap
);
335 if (len
< size
- 4) {
336 writedata (state
.csock
, buf
, len
);
342 err (1, "vsnprintf for `%s' failed", fmt
);
344 buf
= realloc (buf
== fbuf
? NULL
: buf
, size
);
345 if (!buf
) err (1, "realloc for temp buf (%d bytes) failed", size
);
347 if (buf
!= fbuf
) free (buf
);
350 static void closedoc (void)
353 fz_drop_document (state
.ctx
, state
.doc
);
358 static int openxref (char *filename
, char *password
, int layouth
)
360 for (int i
= 0; i
< state
.texcount
; ++i
) {
361 state
.texowners
[i
].w
= -1;
362 state
.texowners
[i
].slice
= NULL
;
368 if (state
.pagedims
) {
369 free (state
.pagedims
);
370 state
.pagedims
= NULL
;
372 state
.pagedimcount
= 0;
374 fz_set_aa_level (state
.ctx
, state
.aalevel
);
375 state
.doc
= fz_open_document (state
.ctx
, filename
);
376 if (fz_needs_password (state
.ctx
, state
.doc
)) {
377 if (password
&& !*password
) {
382 int ok
= fz_authenticate_password (state
.ctx
, state
.doc
, password
);
384 printd ("pass fail");
390 fz_layout_document (state
.ctx
, state
.doc
, 460, layouth
, 12);
391 state
.pagecount
= fz_count_pages (state
.ctx
, state
.doc
);
395 static void docinfo (void)
397 struct { char *tag
; char *name
; } metatbl
[] = {
398 { FZ_META_INFO_TITLE
, "Title" },
399 { FZ_META_INFO_AUTHOR
, "Author" },
400 { FZ_META_FORMAT
, "Format" },
401 { FZ_META_ENCRYPTION
, "Encryption" },
402 { "info:Creator", "Creator" },
403 { "info:Producer", "Producer" },
404 { "info:CreationDate", "Creation date" },
409 for (size_t i
= 0; i
< sizeof (metatbl
) / sizeof (metatbl
[1]); ++i
) {
412 need
= fz_lookup_metadata (state
.ctx
, state
.doc
,
413 metatbl
[i
].tag
, buf
, len
);
416 printd ("info %s\t%s", metatbl
[i
].name
, buf
);
419 buf
= realloc (buf
, need
+ 1);
420 if (!buf
) err (1, "docinfo realloc %d", need
+ 1);
431 static void unlinktile (struct tile
*tile
)
433 for (int i
= 0; i
< tile
->slicecount
; ++i
) {
434 struct slice
*s
= &tile
->slices
[i
];
436 if (s
->texindex
!= -1) {
437 if (state
.texowners
[s
->texindex
].slice
== s
) {
438 state
.texowners
[s
->texindex
].slice
= NULL
;
444 static void freepage (struct page
*page
)
448 fz_drop_stext_page (state
.ctx
, page
->text
);
453 fz_drop_display_list (state
.ctx
, page
->dlist
);
454 fz_drop_page (state
.ctx
, page
->fzpage
);
458 static void freetile (struct tile
*tile
)
463 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
464 #else /* piggyback */
466 fz_drop_pixmap (state
.ctx
, state
.pig
);
468 state
.pig
= tile
->pixmap
;
473 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
478 static void trimctm (pdf_page
*page
, int pindex
)
480 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
483 if (!pdim
->tctmready
) {
484 fz_rect realbox
, mediabox
;
485 fz_matrix page_ctm
, ctm
;
487 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
488 realbox
= fz_transform_rect (pdim
->mediabox
, ctm
);
489 pdf_page_transform (state
.ctx
, page
, &mediabox
, &page_ctm
);
490 pdim
->tctm
= fz_concat (
491 fz_invert_matrix (page_ctm
),
492 fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
)));
497 static fz_matrix
pagectm1 (fz_page
*fzpage
, struct pagedim
*pdim
)
500 ptrdiff_t pdimno
= pdim
- state
.pagedims
;
502 ARSERT (pdim
- state
.pagedims
< INT_MAX
);
503 if (pdf_specifics (state
.ctx
, state
.doc
)) {
504 trimctm (pdf_page_from_fz_page (state
.ctx
, fzpage
), (int) pdimno
);
505 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
508 ctm
= fz_concat (fz_translate (-pdim
->mediabox
.x0
, -pdim
->mediabox
.y0
),
514 static fz_matrix
pagectm (struct page
*page
)
516 return pagectm1 (page
->fzpage
, &state
.pagedims
[page
->pdimno
]);
519 static void *loadpage (int pageno
, int pindex
)
524 page
= calloc (sizeof (struct page
), 1);
526 err (1, "calloc page %d", pageno
);
529 page
->dlist
= fz_new_display_list (state
.ctx
, fz_infinite_rect
);
530 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
532 page
->fzpage
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
533 fz_run_page (state
.ctx
, page
->fzpage
, dev
, fz_identity
, NULL
);
535 fz_catch (state
.ctx
) {
538 fz_close_device (state
.ctx
, dev
);
539 fz_drop_device (state
.ctx
, dev
);
541 page
->pdimno
= pindex
;
542 page
->pageno
= pageno
;
543 page
->sgen
= state
.gen
;
544 page
->agen
= state
.gen
;
545 page
->tgen
= state
.gen
;
549 static struct tile
*alloctile (int h
)
555 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
556 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
557 tile
= calloc (tilesize
, 1);
559 err (1, "cannot allocate tile (%zu bytes)", tilesize
);
561 for (int i
= 0; i
< slicecount
; ++i
) {
562 int sh
= fz_mini (h
, state
.sliceheight
);
563 tile
->slices
[i
].h
= sh
;
564 tile
->slices
[i
].texindex
= -1;
567 tile
->slicecount
= slicecount
;
568 tile
->sliceheight
= state
.sliceheight
;
572 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
,
579 struct pagedim
*pdim
;
581 tile
= alloctile (h
);
582 pdim
= &state
.pagedims
[page
->pdimno
];
587 bbox
.x1
= bbox
.x0
+ w
;
588 bbox
.y1
= bbox
.y0
+ h
;
591 if (state
.pig
->w
== w
593 && state
.pig
->colorspace
== state
.colorspace
) {
594 tile
->pixmap
= state
.pig
;
595 tile
->pixmap
->x
= bbox
.x0
;
596 tile
->pixmap
->y
= bbox
.y0
;
599 fz_drop_pixmap (state
.ctx
, state
.pig
);
606 fz_new_pixmap_with_bbox_and_data (state
.ctx
, state
.colorspace
,
607 bbox
, NULL
, 1, pbo
->ptr
);
612 fz_new_pixmap_with_bbox (state
.ctx
, state
.colorspace
, bbox
,
619 fz_fill_pixmap_with_color (state
.ctx
, tile
->pixmap
,
620 fz_device_rgb (state
.ctx
),
622 fz_default_color_params
);
624 dev
= fz_new_draw_device (state
.ctx
, fz_identity
, tile
->pixmap
);
625 ctm
= pagectm (page
);
626 fz_run_display_list (state
.ctx
, page
->dlist
, dev
, ctm
,
627 fz_rect_from_irect (bbox
), NULL
);
628 fz_close_device (state
.ctx
, dev
);
629 fz_drop_device (state
.ctx
, dev
);
634 static void initpdims (void)
637 int pageno
, trim
, show
;
638 int trimw
= 0, cxcount
;
639 struct pagedim
*p
= NULL
;
640 fz_context
*ctx
= state
.ctx
;
641 fz_rect rootmediabox
= fz_empty_rect
;
642 pdf_document
*pdf
= pdf_specifics (ctx
, state
.doc
);
649 if (state
.trimmargins
&& state
.trimcachepath
) {
650 trimf
= fopen (state
.trimcachepath
, "rb");
652 trimf
= fopen (state
.trimcachepath
, "wb");
657 cxcount
= state
.pagecount
;
660 obj
= pdf_dict_getp (ctx
, pdf_trailer (ctx
, pdf
),
661 "Root/Pages/MediaBox");
662 rootmediabox
= pdf_to_rect (ctx
, obj
);
663 pdf_load_page_tree (ctx
, pdf
);
666 for (pageno
= 0; pageno
< cxcount
; ++pageno
) {
668 fz_rect mediabox
= fz_empty_rect
;
672 pdf_obj
*pageobj
= NULL
;
675 if (pdf
->rev_page_map
) {
676 for (int i
= 0; i
< pdf
->rev_page_count
; ++i
) {
677 if (pdf
->rev_page_map
[i
].page
== pageno
) {
678 pageobj
= pdf_get_xref_entry (
679 ctx
, pdf
, pdf
->rev_page_map
[i
].object
686 pageobj
= pdf_lookup_page_obj (ctx
, pdf
, pageno
);
689 rotate
= pdf_to_int (ctx
, pdf_dict_gets (ctx
, pageobj
, "Rotate"));
691 if (state
.trimmargins
) {
696 page
= pdf_load_page (ctx
, pdf
, pageno
);
697 obj
= pdf_dict_gets (ctx
, pageobj
, "llpp.TrimBox");
698 trim
= state
.trimanew
|| !obj
;
702 fz_matrix ctm
, page_ctm
;
704 dev
= fz_new_bbox_device (ctx
, &rect
);
705 pdf_page_transform (ctx
, page
, &mediabox
, &page_ctm
);
706 ctm
= fz_invert_matrix (page_ctm
);
707 pdf_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
708 fz_close_device (ctx
, dev
);
709 fz_drop_device (ctx
, dev
);
711 rect
.x0
+= state
.trimfuzz
.x0
;
712 rect
.x1
+= state
.trimfuzz
.x1
;
713 rect
.y0
+= state
.trimfuzz
.y0
;
714 rect
.y1
+= state
.trimfuzz
.y1
;
715 rect
= fz_transform_rect (rect
, ctm
);
716 rect
= fz_intersect_rect (rect
, mediabox
);
718 if (!fz_is_empty_rect (rect
)) {
722 obj
= pdf_new_array (ctx
, pdf
, 4);
723 pdf_array_push_real (ctx
, obj
, mediabox
.x0
);
724 pdf_array_push_real (ctx
, obj
, mediabox
.y0
);
725 pdf_array_push_real (ctx
, obj
, mediabox
.x1
);
726 pdf_array_push_real (ctx
, obj
, mediabox
.y1
);
727 pdf_dict_puts (ctx
, pageobj
, "llpp.TrimBox", obj
);
730 mediabox
.x0
= pdf_array_get_real (ctx
, obj
, 0);
731 mediabox
.y0
= pdf_array_get_real (ctx
, obj
, 1);
732 mediabox
.x1
= pdf_array_get_real (ctx
, obj
, 2);
733 mediabox
.y1
= pdf_array_get_real (ctx
, obj
, 3);
736 fz_drop_page (ctx
, &page
->super
);
737 show
= trim
? pageno
% 5 == 0 : pageno
% 20 == 0;
739 printd ("progress %f Trimming %d",
740 (double) (pageno
+ 1) / state
.pagecount
,
745 printd ("emsg failed to load page %d", pageno
);
753 pdf_to_rect (ctx
, pdf_dict_get_inheritable (
759 if (fz_is_empty_rect (mediabox
)) {
768 pdf_to_rect (ctx
, pdf_dict_gets (ctx
, pageobj
, "CropBox"));
769 if (!fz_is_empty_rect (cropbox
)) {
774 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
779 if (fz_is_empty_rect (rootmediabox
)) {
780 printd ("emsg cannot find page size for page %d",
784 mediabox
= rootmediabox
;
791 if (state
.trimmargins
&& trimw
) {
795 page
= fz_load_page (ctx
, state
.doc
, pageno
);
796 mediabox
= fz_bound_page (ctx
, page
);
797 if (state
.trimmargins
) {
801 dev
= fz_new_bbox_device (ctx
, &rect
);
802 fz_run_page (ctx
, page
, dev
, fz_identity
, NULL
);
803 fz_close_device (ctx
, dev
);
804 fz_drop_device (ctx
, dev
);
806 rect
.x0
+= state
.trimfuzz
.x0
;
807 rect
.x1
+= state
.trimfuzz
.x1
;
808 rect
.y0
+= state
.trimfuzz
.y0
;
809 rect
.y1
+= state
.trimfuzz
.y1
;
810 rect
= fz_intersect_rect (rect
, mediabox
);
812 if (!fz_is_empty_rect (rect
)) {
816 fz_drop_page (ctx
, page
);
821 size_t n
= fwrite (&mediabox
, sizeof (mediabox
), 1, trimf
);
823 err (1, "fwrite trim mediabox");
829 size_t n
= fread (&mediabox
, sizeof (mediabox
), 1, trimf
);
831 err (1, "fread trim mediabox %d", pageno
);
837 page
= fz_load_page (ctx
, state
.doc
, pageno
);
838 mediabox
= fz_bound_page (ctx
, page
);
839 fz_drop_page (ctx
, page
);
841 show
= !state
.trimmargins
&& pageno
% 20 == 0;
843 printd ("progress %f Gathering dimensions %d",
844 (double) (pageno
) / state
.pagecount
,
849 printd ("emsg failed to load page %d", pageno
);
855 if (!p
|| p
->rotate
!= rotate
856 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
859 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
860 state
.pagedims
= realloc (state
.pagedims
, size
);
861 if (!state
.pagedims
) {
862 err (1, "realloc pagedims to %zu (%d elems)",
863 size
, state
.pagedimcount
+ 1);
866 p
= &state
.pagedims
[state
.pagedimcount
++];
868 p
->mediabox
= mediabox
;
874 if (fclose (trimf
)) {
880 static void layout (void)
885 struct pagedim
*p
= NULL
;
886 float zw
, w
, maxw
= 0.0, zoom
= 1.0;
888 if (state
.pagedimcount
== 0) return;
890 switch (state
.fitmodel
) {
891 case FitProportional
:
892 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
895 p
= &state
.pagedims
[pindex
];
896 box
= fz_transform_rect (p
->mediabox
,
897 fz_rotate (p
->rotate
+ state
.rotate
));
899 x0
= fz_min (box
.x0
, box
.x1
);
900 x1
= fz_max (box
.x0
, box
.x1
);
903 maxw
= fz_max (w
, maxw
);
904 zoom
= state
.w
/ maxw
;
916 ARSERT (0 && state
.fitmodel
);
919 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
) {
920 p
= &state
.pagedims
[pindex
];
921 ctm
= fz_rotate (state
.rotate
);
922 box
= fz_transform_rect (p
->mediabox
,
923 fz_rotate (p
->rotate
+ state
.rotate
));
925 switch (state
.fitmodel
) {
926 case FitProportional
:
927 p
->left
= (int) (((maxw
- w
) * zoom
) / 2.f
);
935 zoom
= fz_min (zw
, zh
);
936 p
->left
= (int) ((maxw
- (w
* zoom
)) / 2.f
);
945 p
->zoomctm
= fz_scale (zoom
, zoom
);
946 ctm
= fz_concat (p
->zoomctm
, ctm
);
948 p
->pagebox
= p
->mediabox
;
949 p
->pagebox
= fz_transform_rect (p
->pagebox
, fz_rotate (p
->rotate
));
950 p
->pagebox
.x1
-= p
->pagebox
.x0
;
951 p
->pagebox
.y1
-= p
->pagebox
.y0
;
954 p
->bounds
= fz_round_rect (fz_transform_rect (p
->pagebox
, ctm
));
957 ctm
= fz_concat (fz_translate (0, -p
->mediabox
.y1
),
958 fz_scale (zoom
, -zoom
));
963 int x0
= fz_mini (p
->bounds
.x0
, p
->bounds
.x1
);
964 int y0
= fz_mini (p
->bounds
.y0
, p
->bounds
.y1
);
965 int x1
= fz_maxi (p
->bounds
.x0
, p
->bounds
.x1
);
966 int y1
= fz_maxi (p
->bounds
.y0
, p
->bounds
.y1
);
967 int boundw
= x1
- x0
;
968 int boundh
= y1
- y0
;
970 printd ("pdim %d %d %d %d", p
->pageno
, boundw
, boundh
, p
->left
);
971 } while (p
-- != state
.pagedims
);
974 static struct pagedim
*pdimofpageno (int pageno
)
976 struct pagedim
*pdim
= state
.pagedims
;
978 for (int i
= 0; i
< state
.pagedimcount
; ++i
) {
979 if (state
.pagedims
[i
].pageno
> pageno
)
981 pdim
= &state
.pagedims
[i
];
986 static void recurse_outline (fz_outline
*outline
, int level
)
989 if (outline
->page
>= 0) {
990 fz_point p
= {.x
= outline
->x
, .y
= outline
->y
};
991 struct pagedim
*pdim
= pdimofpageno (outline
->page
);
992 int h
= fz_maxi (fz_absi (pdim
->bounds
.y1
- pdim
->bounds
.y0
), 0);
993 p
= fz_transform_point (p
, pdim
->ctm
);
994 printd ("o %d %d %d %d %s",
995 level
, outline
->page
, (int) p
.y
, h
, outline
->title
);
998 printd ("on %d %s", level
, outline
->title
);
1000 if (outline
->down
) {
1001 recurse_outline (outline
->down
, level
+ 1);
1003 outline
= outline
->next
;
1007 static void process_outline (void)
1009 fz_outline
*outline
;
1011 if (!state
.needoutline
|| !state
.pagedimcount
) return;
1013 state
.needoutline
= 0;
1014 outline
= fz_load_outline (state
.ctx
, state
.doc
);
1016 recurse_outline (outline
, 0);
1017 fz_drop_outline (state
.ctx
, outline
);
1021 static char *strofline (fz_stext_line
*line
)
1026 size_t size
= 0, cap
= 80;
1028 p
= malloc (cap
+ 1);
1029 if (!p
) return NULL
;
1031 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1032 int n
= fz_runetochar (utf8
, ch
->c
);
1033 if (size
+ n
> cap
) {
1035 p
= realloc (p
, cap
+ 1);
1036 if (!p
) return NULL
;
1039 memcpy (p
+ size
, utf8
, n
);
1046 static int matchline (regex_t
*re
, fz_stext_line
*line
,
1047 int stop
, int pageno
, double start
)
1053 p
= strofline (line
);
1056 ret
= regexec (re
, p
, 1, &rm
, 0);
1059 if (ret
!= REG_NOMATCH
) {
1062 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1063 printd ("msg regexec error `%.*s'",
1064 (int) size
, errbuf
);
1074 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1075 o
+= fz_runelen (ch
->c
);
1081 for (;ch
; ch
= ch
->next
) {
1082 o
+= fz_runelen (ch
->c
);
1083 if (o
> rm
.rm_eo
) break;
1088 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1095 printd ("progress 1 found at %d `%.*s' in %f sec",
1096 pageno
+ 1, (int) (rm
.rm_eo
- rm
.rm_so
), &p
[rm
.rm_so
],
1100 printd ("match %d %d %f %f %f %f %f %f %f %f",
1112 /* wishful thinking function */
1113 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
1116 fz_stext_page
*text
;
1117 struct pagedim
*pdim
;
1118 int stop
= 0, niters
= 0;
1121 fz_stext_block
*block
;
1124 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
1125 if (niters
++ == 5) {
1128 printd ("progress 1 attention requested aborting search at %d",
1133 printd ("progress %f searching in page %d",
1134 (double) (pageno
+ 1) / state
.pagecount
,
1138 pdim
= pdimofpageno (pageno
);
1139 text
= fz_new_stext_page (state
.ctx
, pdim
->mediabox
);
1140 tdev
= fz_new_stext_device (state
.ctx
, text
, 0);
1142 page
= fz_load_page (state
.ctx
, state
.doc
, pageno
);
1143 fz_run_page (state
.ctx
, page
, tdev
, pagectm1 (page
, pdim
), NULL
);
1145 fz_close_device (state
.ctx
, tdev
);
1146 fz_drop_device (state
.ctx
, tdev
);
1149 for (block
= text
->first_block
; block
; block
= block
->next
) {
1150 fz_stext_line
*line
;
1152 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1153 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1154 if (line
->bbox
.y0
< y
+ 1) continue;
1156 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1158 case 1: stop
= 1; break;
1159 case -1: stop
= 1; goto endloop
;
1165 for (block
= text
->last_block
; block
; block
= block
->prev
) {
1166 fz_stext_line
*line
;
1168 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1169 for (line
= block
->u
.t
.last_line
; line
; line
= line
->prev
) {
1170 if (line
->bbox
.y0
< y
+ 1) continue;
1172 switch (matchline (re
, line
, stop
, pageno
, start
)) {
1174 case 1: stop
= 1; break;
1175 case -1: stop
= 1; goto endloop
;
1190 fz_drop_stext_page (state
.ctx
, text
);
1191 fz_drop_page (state
.ctx
, page
);
1195 printd ("progress 1 no matches %f sec", end
- start
);
1197 printd ("clearrects");
1200 static void set_tex_params (int colorspace
)
1202 switch (colorspace
) {
1204 state
.texiform
= GL_RGBA8
;
1205 state
.texform
= GL_RGBA
;
1206 state
.texty
= GL_UNSIGNED_BYTE
;
1207 state
.colorspace
= fz_device_rgb (state
.ctx
);
1210 state
.texiform
= GL_LUMINANCE_ALPHA
;
1211 state
.texform
= GL_LUMINANCE_ALPHA
;
1212 state
.texty
= GL_UNSIGNED_BYTE
;
1213 state
.colorspace
= fz_device_gray (state
.ctx
);
1216 errx (1, "invalid colorspce %d", colorspace
);
1220 static void realloctexts (int texcount
)
1224 if (texcount
== state
.texcount
) return;
1226 if (texcount
< state
.texcount
) {
1227 glDeleteTextures (state
.texcount
- texcount
,
1228 state
.texids
+ texcount
);
1231 size
= texcount
* (sizeof (*state
.texids
) + sizeof (*state
.texowners
));
1232 state
.texids
= realloc (state
.texids
, size
);
1233 if (!state
.texids
) {
1234 err (1, "realloc texs %zu", size
);
1237 state
.texowners
= (void *) (state
.texids
+ texcount
);
1238 if (texcount
> state
.texcount
) {
1239 glGenTextures (texcount
- state
.texcount
,
1240 state
.texids
+ state
.texcount
);
1241 for (int i
= state
.texcount
; i
< texcount
; ++i
) {
1242 state
.texowners
[i
].w
= -1;
1243 state
.texowners
[i
].slice
= NULL
;
1246 state
.texcount
= texcount
;
1250 static char *mbtoutf8 (char *s
)
1260 len
= mbstowcs (NULL
, s
, strlen (s
));
1261 if (len
== 0 || len
== (size_t) -1) {
1263 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno
, strerror (errno
));
1267 tmp
= calloc (len
, sizeof (wchar_t));
1269 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1270 len
, sizeof (wchar_t), errno
, strerror (errno
));
1274 ret
= mbstowcs (tmp
, s
, len
);
1275 if (ret
== (size_t) -1) {
1276 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1277 len
, errno
, strerror (errno
));
1283 for (i
= 0; i
< ret
; ++i
) {
1284 len
+= fz_runelen (tmp
[i
]);
1287 p
= r
= malloc (len
+ 1);
1289 printd ("emsg mbtoutf8: malloc(%zu)", len
);
1294 for (i
= 0; i
< ret
; ++i
) {
1295 p
+= fz_runetochar (p
, tmp
[i
]);
1302 ML (mbtoutf8 (value s_v
))
1314 ret_v
= caml_copy_string (r
);
1320 static void * mainloop (void UNUSED_ATTR
*unused
)
1323 int len
, ret
, oldlen
= 0;
1328 len
= readlen (state
.csock
);
1330 errx (1, "readlen returned 0");
1333 if (oldlen
< len
+ 1) {
1334 p
= realloc (p
, len
+ 1);
1336 err (1, "realloc %d failed", len
+ 1);
1340 readdata (state
.csock
, p
, len
);
1343 if (!strncmp ("open", p
, 4)) {
1344 int off
, usedoccss
, ok
= 0, layouth
;
1351 ret
= sscanf (p
+ 5, " %d %d %n", &usedoccss
, &layouth
, &off
);
1353 errx (1, "malformed open `%.*s' ret=%d", len
, p
, ret
);
1356 filename
= p
+ 5 + off
;
1357 filenamelen
= strlen (filename
);
1358 password
= filename
+ filenamelen
+ 1;
1360 if (password
[strlen (password
) + 1]) {
1361 fz_set_user_css (state
.ctx
, password
+ strlen (password
) + 1);
1365 fz_set_use_document_css (state
.ctx
, usedoccss
);
1366 fz_try (state
.ctx
) {
1367 ok
= openxref (filename
, password
, layouth
);
1369 fz_catch (state
.ctx
) {
1370 utf8filename
= mbtoutf8 (filename
);
1371 printd ("emsg Error loading %s: %s",
1372 utf8filename
, fz_caught_message (state
.ctx
));
1373 if (utf8filename
!= filename
) {
1374 free (utf8filename
);
1382 state
.needoutline
= ok
;
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 ML (isexternallink (value uri_v
))
1627 int ext
= fz_is_external_link (state
.ctx
, FUSV (uri_v
));
1628 CAMLreturn (Val_bool (ext
));
1631 ML (uritolocation (value uri_v
))
1637 struct pagedim
*pdim
;
1639 pageno
= fz_resolve_link (state
.ctx
, state
.doc
, FUSV (uri_v
),
1641 pdim
= pdimofpageno (pageno
);
1642 xy
= fz_transform_point (xy
, pdim
->ctm
);
1643 ret_v
= caml_alloc_tuple (3);
1644 Field (ret_v
, 0) = Val_int (pageno
);
1645 Field (ret_v
, 1) = caml_copy_double ((double) xy
.x
);
1646 Field (ret_v
, 2) = caml_copy_double ((double) xy
.y
);
1650 ML (realloctexts (value texcount_v
))
1652 CAMLparam1 (texcount_v
);
1655 if (trylock (__func__
)) {
1659 realloctexts (Int_val (texcount_v
));
1664 CAMLreturn (Val_bool (ok
));
1667 static void recti (int x0
, int y0
, int x1
, int y1
)
1669 GLfloat
*v
= state
.vertices
;
1671 glVertexPointer (2, GL_FLOAT
, 0, v
);
1672 v
[0] = x0
; v
[1] = y0
;
1673 v
[2] = x1
; v
[3] = y0
;
1674 v
[4] = x0
; v
[5] = y1
;
1675 v
[6] = x1
; v
[7] = y1
;
1676 glDrawArrays (GL_TRIANGLE_STRIP
, 0, 4);
1679 static void showsel (struct page
*page
, int ox
, int oy
)
1683 fz_stext_block
*block
;
1685 unsigned char selcolor
[] = {15,15,15,140};
1687 if (!page
->fmark
|| !page
->lmark
) return;
1689 glEnable (GL_BLEND
);
1690 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1691 glColor4ubv (selcolor
);
1693 ox
+= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1694 oy
+= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1696 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
1697 fz_stext_line
*line
;
1699 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
1700 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
1703 rect
= fz_empty_rect
;
1704 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
1706 if (ch
== page
->fmark
) seen
= 1;
1707 r
= fz_rect_from_quad (ch
->quad
);
1708 if (seen
) rect
= fz_union_rect (rect
, r
);
1709 if (ch
== page
->lmark
) {
1710 bbox
= fz_round_rect (rect
);
1711 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1712 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1716 bbox
= fz_round_rect (rect
);
1717 recti (bbox
.x0
+ ox
, bbox
.y0
+ oy
,
1718 bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1722 glDisable (GL_BLEND
);
1725 #pragma GCC diagnostic push
1726 #pragma GCC diagnostic ignored "-Wconversion"
1728 #pragma GCC diagnostic pop
1730 static void stipplerect (fz_matrix m
,
1738 p1
= fz_transform_point (p1
, m
);
1739 p2
= fz_transform_point (p2
, m
);
1740 p3
= fz_transform_point (p3
, m
);
1741 p4
= fz_transform_point (p4
, m
);
1747 t
= hypotf (w
, h
) * .25f
;
1751 s
= hypotf (w
, h
) * .25f
;
1753 texcoords
[0] = 0; vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1754 texcoords
[1] = t
; vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1756 texcoords
[2] = 0; vertices
[4] = p2
.x
; vertices
[5] = p2
.y
;
1757 texcoords
[3] = s
; vertices
[6] = p3
.x
; vertices
[7] = p3
.y
;
1759 texcoords
[4] = 0; vertices
[8] = p3
.x
; vertices
[9] = p3
.y
;
1760 texcoords
[5] = t
; vertices
[10] = p4
.x
; vertices
[11] = p4
.y
;
1762 texcoords
[6] = 0; vertices
[12] = p4
.x
; vertices
[13] = p4
.y
;
1763 texcoords
[7] = s
; vertices
[14] = p1
.x
; vertices
[15] = p1
.y
;
1765 glDrawArrays (GL_LINES
, 0, 8);
1768 static void solidrect (fz_matrix m
,
1775 p1
= fz_transform_point (p1
, m
);
1776 p2
= fz_transform_point (p2
, m
);
1777 p3
= fz_transform_point (p3
, m
);
1778 p4
= fz_transform_point (p4
, m
);
1779 vertices
[0] = p1
.x
; vertices
[1] = p1
.y
;
1780 vertices
[2] = p2
.x
; vertices
[3] = p2
.y
;
1782 vertices
[4] = p3
.x
; vertices
[5] = p3
.y
;
1783 vertices
[6] = p4
.x
; vertices
[7] = p4
.y
;
1784 glDrawArrays (GL_TRIANGLE_FAN
, 0, 4);
1787 static void ensurelinks (struct page
*page
)
1790 page
->links
= fz_load_links (state
.ctx
, page
->fzpage
);
1793 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1797 GLfloat
*texcoords
= state
.texcoords
;
1798 GLfloat
*vertices
= state
.vertices
;
1802 glEnable (GL_TEXTURE_1D
);
1803 glEnable (GL_BLEND
);
1804 glBlendFunc (GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1805 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
1807 xoff
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1808 yoff
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1809 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1811 glTexCoordPointer (1, GL_FLOAT
, 0, texcoords
);
1812 glVertexPointer (2, GL_FLOAT
, 0, vertices
);
1814 for (link
= page
->links
; link
; link
= link
->next
) {
1815 fz_point p1
, p2
, p3
, p4
;
1817 p1
.x
= link
->rect
.x0
;
1818 p1
.y
= link
->rect
.y0
;
1820 p2
.x
= link
->rect
.x1
;
1821 p2
.y
= link
->rect
.y0
;
1823 p3
.x
= link
->rect
.x1
;
1824 p3
.y
= link
->rect
.y1
;
1826 p4
.x
= link
->rect
.x0
;
1827 p4
.y
= link
->rect
.y1
;
1829 /* TODO: different colours for different schemes */
1830 if (fz_is_external_link (state
.ctx
, link
->uri
)) glColor3ub (0, 0, 255);
1831 else glColor3ub (255, 0, 0);
1833 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1836 for (int i
= 0; i
< page
->annotcount
; ++i
) {
1837 fz_point p1
, p2
, p3
, p4
;
1838 struct annot
*annot
= &page
->annots
[i
];
1840 p1
.x
= annot
->bbox
.x0
;
1841 p1
.y
= annot
->bbox
.y0
;
1843 p2
.x
= annot
->bbox
.x1
;
1844 p2
.y
= annot
->bbox
.y0
;
1846 p3
.x
= annot
->bbox
.x1
;
1847 p3
.y
= annot
->bbox
.y1
;
1849 p4
.x
= annot
->bbox
.x0
;
1850 p4
.y
= annot
->bbox
.y1
;
1852 glColor3ub (0, 0, 128);
1853 stipplerect (ctm
, p1
, p2
, p3
, p4
, texcoords
, vertices
);
1856 glDisable (GL_BLEND
);
1857 glDisable (GL_TEXTURE_1D
);
1860 static int compareslinks (const void *l
, const void *r
)
1862 struct slink
const *ls
= l
;
1863 struct slink
const *rs
= r
;
1864 if (ls
->bbox
.y0
== rs
->bbox
.y0
) {
1865 return ls
->bbox
.x0
- rs
->bbox
.x0
;
1867 return ls
->bbox
.y0
- rs
->bbox
.y0
;
1870 static void droptext (struct page
*page
)
1873 fz_drop_stext_page (state
.ctx
, page
->text
);
1880 static void dropannots (struct page
*page
)
1883 free (page
->annots
);
1884 page
->annots
= NULL
;
1885 page
->annotcount
= 0;
1889 static void ensureannots (struct page
*page
)
1892 size_t annotsize
= sizeof (*page
->annots
);
1897 pdf
= pdf_specifics (state
.ctx
, state
.doc
);
1900 pdfpage
= pdf_page_from_fz_page (state
.ctx
, page
->fzpage
);
1901 if (state
.gen
!= page
->agen
) {
1903 page
->agen
= state
.gen
;
1905 if (page
->annots
) return;
1907 for (annot
= pdf_first_annot (state
.ctx
, pdfpage
);
1909 annot
= pdf_next_annot (state
.ctx
, annot
)) {
1914 page
->annotcount
= count
;
1915 page
->annots
= calloc (count
, annotsize
);
1916 if (!page
->annots
) {
1917 err (1, "calloc annots %d", count
);
1920 for (annot
= pdf_first_annot (state
.ctx
, pdfpage
), i
= 0;
1922 annot
= pdf_next_annot (state
.ctx
, annot
), i
++) {
1925 rect
= pdf_bound_annot (state
.ctx
, annot
);
1926 page
->annots
[i
].annot
= annot
;
1927 page
->annots
[i
].bbox
= fz_round_rect (rect
);
1932 static void dropslinks (struct page
*page
)
1935 free (page
->slinks
);
1936 page
->slinks
= NULL
;
1937 page
->slinkcount
= 0;
1940 fz_drop_link (state
.ctx
, page
->links
);
1945 static void ensureslinks (struct page
*page
)
1949 size_t slinksize
= sizeof (*page
->slinks
);
1952 ensureannots (page
);
1953 if (state
.gen
!= page
->sgen
) {
1955 page
->sgen
= state
.gen
;
1957 if (page
->slinks
) return;
1960 ctm
= pagectm (page
);
1962 count
= page
->annotcount
;
1963 for (link
= page
->links
; link
; link
= link
->next
) {
1969 page
->slinkcount
= count
;
1970 page
->slinks
= calloc (count
, slinksize
);
1971 if (!page
->slinks
) {
1972 err (1, "calloc slinks %d", count
);
1975 for (i
= 0, link
= page
->links
; link
; ++i
, link
= link
->next
) {
1979 rect
= fz_transform_rect (rect
, ctm
);
1980 page
->slinks
[i
].tag
= SLINK
;
1981 page
->slinks
[i
].u
.link
= link
;
1982 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
1984 for (j
= 0; j
< page
->annotcount
; ++j
, ++i
) {
1986 rect
= pdf_bound_annot (state
.ctx
, page
->annots
[j
].annot
);
1987 rect
= fz_transform_rect (rect
, ctm
);
1988 page
->slinks
[i
].bbox
= fz_round_rect (rect
);
1990 page
->slinks
[i
].tag
= SANNOT
;
1991 page
->slinks
[i
].u
.annot
= page
->annots
[j
].annot
;
1993 qsort (page
->slinks
, count
, slinksize
, compareslinks
);
1997 static void highlightslinks (struct page
*page
, int xoff
, int yoff
,
1998 int noff
, char *targ
, 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 char *s
= FUSV (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 char *targ
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *ptr
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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 char *s
= FUSV (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] = FUSV (command_v
);
2999 if ((ret
= posix_spawn_file_actions_init (&fa
)) != 0) {
3000 unix_error (ret
, "posix_spawn_file_actions_init", Nothing
);
3003 if ((ret
= posix_spawnattr_init (&attr
)) != 0) {
3004 msg
= "posix_spawnattr_init";
3008 #ifdef POSIX_SPAWN_USEVFORK
3009 if ((ret
= posix_spawnattr_setflags (&attr
, POSIX_SPAWN_USEVFORK
)) != 0) {
3010 msg
= "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3015 for (l_v
= fds_v
; l_v
!= Val_int (0); l_v
= Field (l_v
, 1)) {
3018 tup_v
= Field (l_v
, 0);
3019 fd1
= Int_val (Field (tup_v
, 0));
3020 fd2
= Int_val (Field (tup_v
, 1));
3022 if ((ret
= posix_spawn_file_actions_addclose (&fa
, fd1
)) != 0) {
3023 msg
= "posix_spawn_file_actions_addclose";
3029 if ((ret
= posix_spawn_file_actions_adddup2 (&fa
, fd1
, fd2
)) != 0) {
3030 msg
= "posix_spawn_file_actions_adddup2";
3037 if ((ret
= posix_spawn (&pid
, "/bin/sh", &fa
, &attr
, argv
, environ
))) {
3038 msg
= "posix_spawn";
3043 if ((ret1
= posix_spawnattr_destroy (&attr
)) != 0) {
3044 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1
, strerror (ret1
));
3048 if ((ret1
= posix_spawn_file_actions_destroy (&fa
)) != 0) {
3049 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3050 ret1
, strerror (ret1
));
3054 unix_error (ret
, msg
, earg_v
);
3056 CAMLreturn (Val_int (pid
));
3059 ML (hassel (value ptr_v
))
3064 char *s
= FUSV (ptr_v
);
3066 ret_v
= Val_bool (0);
3067 if (trylock (__func__
)) {
3071 page
= parse_pointer (__func__
, s
);
3072 ret_v
= Val_bool (page
->fmark
&& page
->lmark
);
3078 ML0 (copysel (value fd_v
, value ptr_v
))
3080 CAMLparam2 (fd_v
, ptr_v
);
3084 fz_stext_line
*line
;
3085 fz_stext_block
*block
;
3086 int fd
= Int_val (fd_v
);
3087 char *s
= FUSV (ptr_v
);
3089 if (trylock (__func__
)) {
3093 page
= parse_pointer (__func__
, s
);
3095 if (!page
->fmark
|| !page
->lmark
) {
3096 printd ("emsg nothing to copy on page %d", page
->pageno
);
3100 f
= fdopen (fd
, "w");
3102 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3103 fd
, errno
, strerror (errno
));
3107 for (block
= page
->text
->first_block
; block
; block
= block
->next
) {
3108 if (block
->type
!= FZ_STEXT_BLOCK_TEXT
) continue;
3109 for (line
= block
->u
.t
.first_line
; line
; line
= line
->next
) {
3111 for (ch
= line
->first_char
; ch
; ch
= ch
->next
) {
3112 if (seen
|| ch
== page
->fmark
) {
3115 if (ch
== page
->lmark
) goto close
;
3116 } while ((ch
= ch
->next
));
3121 if (seen
) fputc ('\n', f
);
3126 int ret
= fclose (f
);
3129 if (errno
!= ECHILD
) {
3130 printd ("emsg failed to close sel pipe: %d:%s",
3131 errno
, strerror (errno
));
3141 printd ("emsg failed to close sel pipe: %d:%s",
3142 errno
, strerror (errno
));
3148 ML (getpdimrect (value pagedimno_v
))
3150 CAMLparam1 (pagedimno_v
);
3152 int pagedimno
= Int_val (pagedimno_v
);
3155 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
3156 if (trylock (__func__
)) {
3157 box
= fz_empty_rect
;
3160 box
= state
.pagedims
[pagedimno
].mediabox
;
3164 Store_double_field (ret_v
, 0, (double) box
.x0
);
3165 Store_double_field (ret_v
, 1, (double) box
.x1
);
3166 Store_double_field (ret_v
, 2, (double) box
.y0
);
3167 Store_double_field (ret_v
, 3, (double) box
.y1
);
3172 ML (zoom_for_height (value winw_v
, value winh_v
, value dw_v
, value cols_v
))
3174 CAMLparam4 (winw_v
, winh_v
, dw_v
, cols_v
);
3180 float winw
= Int_val (winw_v
);
3181 float winh
= Int_val (winh_v
);
3182 float dw
= Int_val (dw_v
);
3183 float cols
= Int_val (cols_v
);
3184 float pw
= 1.0, ph
= 1.0;
3186 if (trylock (__func__
)) {
3190 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3191 float w
= p
->pagebox
.x1
/ cols
;
3192 float h
= p
->pagebox
.y1
;
3196 if (state
.fitmodel
!= FitProportional
) pw
= w
;
3198 if ((state
.fitmodel
== FitProportional
) && w
> pw
) pw
= w
;
3201 zoom
= (((winh
/ ph
) * pw
) + dw
) / winw
;
3204 ret_v
= caml_copy_double ((double) zoom
);
3208 ML (getmaxw (value unit_v
))
3210 CAMLparam1 (unit_v
);
3216 if (trylock (__func__
)) {
3220 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
3221 float w
= p
->pagebox
.x1
;
3222 maxw
= fz_max (maxw
, w
);
3227 ret_v
= caml_copy_double ((double) maxw
);
3231 ML (draw_string (value pt_v
, value x_v
, value y_v
, value string_v
))
3233 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
3235 int pt
= Int_val(pt_v
);
3236 int x
= Int_val (x_v
);
3237 int y
= Int_val (y_v
);
3240 w
= draw_string (state
.face
, pt
, x
, y
, FUSV (string_v
));
3241 ret_v
= caml_copy_double (w
);
3245 ML (measure_string (value pt_v
, value string_v
))
3247 CAMLparam2 (pt_v
, string_v
);
3249 int pt
= Int_val (pt_v
);
3252 w
= (double) measure_string (state
.face
, pt
, FUSV (string_v
));
3253 ret_v
= caml_copy_double (w
);
3257 ML (getpagebox (value opaque_v
))
3259 CAMLparam1 (opaque_v
);
3264 char *s
= FUSV (opaque_v
);
3265 struct page
*page
= parse_pointer (__func__
, s
);
3267 ret_v
= caml_alloc_tuple (4);
3268 dev
= fz_new_bbox_device (state
.ctx
, &rect
);
3270 fz_run_page (state
.ctx
, page
->fzpage
, dev
, pagectm (page
), NULL
);
3272 fz_close_device (state
.ctx
, dev
);
3273 fz_drop_device (state
.ctx
, dev
);
3274 bbox
= fz_round_rect (rect
);
3275 Field (ret_v
, 0) = Val_int (bbox
.x0
);
3276 Field (ret_v
, 1) = Val_int (bbox
.y0
);
3277 Field (ret_v
, 2) = Val_int (bbox
.x1
);
3278 Field (ret_v
, 3) = Val_int (bbox
.y1
);
3283 ML0 (setaalevel (value level_v
))
3285 CAMLparam1 (level_v
);
3287 state
.aalevel
= Int_val (level_v
);
3291 ML0 (setpapercolor (value rgba_v
))
3293 CAMLparam1 (rgba_v
);
3295 state
.papercolor
[0] = (float) Double_val (Field (rgba_v
, 0));
3296 state
.papercolor
[1] = (float) Double_val (Field (rgba_v
, 1));
3297 state
.papercolor
[2] = (float) Double_val (Field (rgba_v
, 2));
3298 state
.papercolor
[3] = (float) Double_val (Field (rgba_v
, 3));
3302 value
ml_keysymtoutf8 (value keysym_v
);
3304 value
ml_keysymtoutf8 (value keysym_v
)
3306 CAMLparam1 (keysym_v
);
3308 KeySym keysym
= Int_val (keysym_v
);
3310 extern long keysym2ucs (KeySym
);
3314 rune
= (Rune
) keysym2ucs (keysym
);
3315 len
= fz_runetochar (buf
, rune
);
3317 str_v
= caml_copy_string (buf
);
3321 value
ml_keysymtoutf8 (value keysym_v
)
3323 CAMLparam1 (keysym_v
);
3325 long ucs
= Long_val (keysym_v
);
3329 len
= fz_runetochar (buf
, (int) ucs
);
3331 str_v
= caml_copy_string (buf
);
3336 enum { piunknown
, pilinux
, pimacos
, pibsd
};
3338 ML (platform (value unit_v
))
3340 CAMLparam1 (unit_v
);
3341 CAMLlocal2 (tup_v
, arr_v
);
3342 int platid
= piunknown
;
3345 #if defined __linux__
3347 #elif defined __DragonFly__ || defined __FreeBSD__
3348 || defined __OpenBSD__
|| defined __NetBSD__
3350 #elif defined __APPLE__
3353 if (uname (&buf
)) err (1, "uname");
3355 tup_v
= caml_alloc_tuple (2);
3357 char const *sar
[] = {
3364 arr_v
= caml_copy_string_array (sar
);
3366 Field (tup_v
, 0) = Val_int (platid
);
3367 Field (tup_v
, 1) = arr_v
;
3371 ML0 (cloexec (value fd_v
))
3374 int fd
= Int_val (fd_v
);
3376 if (fcntl (fd
, F_SETFD
, FD_CLOEXEC
, 1)) {
3377 uerror ("fcntl", Nothing
);
3382 ML (getpbo (value w_v
, value h_v
, value cs_v
))
3384 CAMLparam2 (w_v
, h_v
);
3387 int w
= Int_val (w_v
);
3388 int h
= Int_val (h_v
);
3389 int cs
= Int_val (cs_v
);
3391 if (state
.bo_usable
) {
3392 pbo
= calloc (sizeof (*pbo
), 1);
3394 err (1, "calloc pbo");
3406 errx (1, "%s: invalid colorspace %d", __func__
, cs
);
3409 state
.glGenBuffersARB (1, &pbo
->id
);
3410 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, pbo
->id
);
3411 state
.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB
, (GLsizei
) pbo
->size
,
3412 NULL
, GL_STREAM_DRAW
);
3413 pbo
->ptr
= state
.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
,
3415 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3417 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3418 state
.glDeleteBuffersARB (1, &pbo
->id
);
3420 ret_v
= caml_copy_string ("0");
3426 res
= snprintf (NULL
, 0, "%" PRIxPTR
, (uintptr_t) pbo
);
3428 err (1, "snprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3432 err (1, "malloc %d bytes failed", res
+1);
3434 res
= sprintf (s
, "%" PRIxPTR
, (uintptr_t) pbo
);
3436 err (1, "sprintf %" PRIxPTR
" failed", (uintptr_t) pbo
);
3438 ret_v
= caml_copy_string (s
);
3443 ret_v
= caml_copy_string ("0");
3448 ML0 (freepbo (value s_v
))
3451 char *s
= FUSV (s_v
);
3452 struct tile
*tile
= parse_pointer (__func__
, s
);
3455 state
.glDeleteBuffersARB (1, &tile
->pbo
->id
);
3457 tile
->pbo
->ptr
= NULL
;
3458 tile
->pbo
->size
= -1;
3463 ML0 (unmappbo (value s_v
))
3466 char *s
= FUSV (s_v
);
3467 struct tile
*tile
= parse_pointer (__func__
, s
);
3470 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, tile
->pbo
->id
);
3471 if (state
.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
) == GL_FALSE
) {
3472 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3474 tile
->pbo
->ptr
= NULL
;
3475 state
.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
3480 static void setuppbo (void)
3482 extern void (*wsigladdr (const char *name
)) (void);
3483 #pragma GCC diagnostic push
3485 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3487 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3488 state
.bo_usable
= GPA (glBindBufferARB
)
3489 && GPA (glUnmapBufferARB
)
3490 && GPA (glMapBufferARB
)
3491 && GPA (glBufferDataARB
)
3492 && GPA (glGenBuffersARB
)
3493 && GPA (glDeleteBuffersARB
);
3495 #pragma GCC diagnostic pop
3498 ML (bo_usable (void))
3500 return Val_bool (state
.bo_usable
);
3503 ML (unproject (value ptr_v
, value x_v
, value y_v
))
3505 CAMLparam3 (ptr_v
, x_v
, y_v
);
3506 CAMLlocal2 (ret_v
, tup_v
);
3508 char *s
= FUSV (ptr_v
);
3509 int x
= Int_val (x_v
), y
= Int_val (y_v
);
3510 struct pagedim
*pdim
;
3513 page
= parse_pointer (__func__
, s
);
3514 pdim
= &state
.pagedims
[page
->pdimno
];
3516 ret_v
= Val_int (0);
3517 if (trylock (__func__
)) {
3521 p
.x
= x
+ pdim
->bounds
.x0
;
3522 p
.y
= y
+ pdim
->bounds
.y0
;
3524 p
= fz_transform_point (p
, fz_invert_matrix (fz_concat (pdim
->tctm
,
3527 tup_v
= caml_alloc_tuple (2);
3528 ret_v
= caml_alloc_small (1, 1);
3529 Field (tup_v
, 0) = Val_int (p
.x
);
3530 Field (tup_v
, 1) = Val_int (p
.y
);
3531 Field (ret_v
, 0) = tup_v
;
3538 ML (project (value ptr_v
, value pageno_v
, value pdimno_v
, value x_v
, value y_v
))
3540 CAMLparam5 (ptr_v
, pageno_v
, pdimno_v
, x_v
, y_v
);
3543 char *s
= FUSV (ptr_v
);
3544 int pageno
= Int_val (pageno_v
);
3545 int pdimno
= Int_val (pdimno_v
);
3546 float x
= (float) Double_val (x_v
), y
= (float) Double_val (y_v
);
3547 struct pagedim
*pdim
;
3551 ret_v
= Val_int (0);
3555 page
= loadpage (pageno
, pdimno
);
3558 page
= parse_pointer (__func__
, s
);
3560 pdim
= &state
.pagedims
[pdimno
];
3562 if (pdf_specifics (state
.ctx
, state
.doc
)) {
3563 trimctm (pdf_page_from_fz_page (state
.ctx
, page
->fzpage
), page
->pdimno
);
3564 ctm
= state
.pagedims
[page
->pdimno
].tctm
;
3569 p
.x
= x
+ pdim
->bounds
.x0
;
3570 p
.y
= y
+ pdim
->bounds
.y0
;
3572 ctm
= fz_concat (pdim
->tctm
, pdim
->ctm
);
3573 p
= fz_transform_point (p
, ctm
);
3575 ret_v
= caml_alloc_tuple (2);
3576 Field (ret_v
, 0) = caml_copy_double ((double) p
.x
);
3577 Field (ret_v
, 1) = caml_copy_double ((double) p
.y
);
3586 ML0 (addannot (value ptr_v
, value x_v
, value y_v
, value contents_v
))
3588 CAMLparam4 (ptr_v
, x_v
, y_v
, contents_v
);
3589 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3595 char *s
= FUSV (ptr_v
);
3597 page
= parse_pointer (__func__
, s
);
3598 annot
= pdf_create_annot (state
.ctx
,
3599 pdf_page_from_fz_page (state
.ctx
,
3602 r
.x0
= Int_val (x_v
) - 10;
3603 r
.y0
= Int_val (y_v
) - 10;
3606 pdf_set_annot_contents (state
.ctx
, annot
, FUSV (contents_v
));
3607 pdf_set_annot_rect (state
.ctx
, annot
, r
);
3614 ML0 (delannot (value ptr_v
, value n_v
))
3616 CAMLparam2 (ptr_v
, n_v
);
3617 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3621 char *s
= FUSV (ptr_v
);
3622 struct slink
*slink
;
3624 page
= parse_pointer (__func__
, s
);
3625 slink
= &page
->slinks
[Int_val (n_v
)];
3626 pdf_delete_annot (state
.ctx
,
3627 pdf_page_from_fz_page (state
.ctx
, page
->fzpage
),
3628 (pdf_annot
*) slink
->u
.annot
);
3634 ML0 (modannot (value ptr_v
, value n_v
, value str_v
))
3636 CAMLparam3 (ptr_v
, n_v
, str_v
);
3637 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3641 char *s
= FUSV (ptr_v
);
3642 struct slink
*slink
;
3644 page
= parse_pointer (__func__
, s
);
3645 slink
= &page
->slinks
[Int_val (n_v
)];
3646 pdf_set_annot_contents (state
.ctx
, (pdf_annot
*) slink
->u
.annot
,
3653 ML (hasunsavedchanges (void))
3655 return Val_bool (state
.dirty
);
3658 ML0 (savedoc (value path_v
))
3660 CAMLparam1 (path_v
);
3661 pdf_document
*pdf
= pdf_specifics (state
.ctx
, state
.doc
);
3664 pdf_save_document (state
.ctx
, pdf
, FUSV (path_v
), NULL
);
3669 static void makestippletex (void)
3671 const char pixels
[] = "\xff\xff\0\0";
3672 glGenTextures (1, &state
.stid
);
3673 glBindTexture (GL_TEXTURE_1D
, state
.stid
);
3674 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
3675 glTexParameteri (GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
3688 ML (fz_version (void))
3690 return caml_copy_string (FZ_VERSION
);
3693 ML (llpp_version (void))
3695 extern char llpp_version
[];
3696 return caml_copy_string (llpp_version
);
3699 static void diag_callback (void *user
, const char *message
)
3701 printd ("emsg %s %s", (char *) user
, message
);
3704 ML0 (init (value csock_v
, value params_v
))
3706 CAMLparam2 (csock_v
, params_v
);
3707 CAMLlocal2 (trim_v
, fuzz_v
);
3714 state
.csock
= Int_val (csock_v
);
3715 state
.rotate
= Int_val (Field (params_v
, 0));
3716 state
.fitmodel
= Int_val (Field (params_v
, 1));
3717 trim_v
= Field (params_v
, 2);
3718 texcount
= Int_val (Field (params_v
, 3));
3719 state
.sliceheight
= Int_val (Field (params_v
, 4));
3720 mustoresize
= Int_val (Field (params_v
, 5));
3721 colorspace
= Int_val (Field (params_v
, 6));
3722 fontpath
= FUSV (Field (params_v
, 7));
3727 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3728 if (setlocale (LC_CTYPE
, "")) {
3729 const char *cset
= nl_langinfo (CODESET
);
3730 state
.utf8cs
= !strcmp (cset
, "UTF-8");
3733 printd ("emsg setlocale: %d:%s", errno
, strerror (errno
));
3737 if (caml_string_length (Field (params_v
, 8)) > 0) {
3738 state
.trimcachepath
= ystrdup (FUSV (Field (params_v
, 8)));
3740 if (!state
.trimcachepath
) {
3741 printd ("emsg failed to strdup trimcachepath: %d:%s",
3742 errno
, strerror (errno
));
3746 state
.ctx
= fz_new_context (NULL
, NULL
, mustoresize
);
3747 fz_register_document_handlers (state
.ctx
);
3748 fz_set_error_callback (state
.ctx
, diag_callback
, "[e]");
3749 fz_set_warning_callback (state
.ctx
, diag_callback
, "[w]");
3751 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
3752 fuzz_v
= Field (trim_v
, 1);
3753 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
3754 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
3755 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
3756 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
3758 set_tex_params (colorspace
);
3761 state
.face
= load_font (fontpath
);
3765 const unsigned char *data
;
3767 data
= pdf_lookup_substitute_font (state
.ctx
, 0, 0, 0, 0, &len
);
3768 state
.face
= load_builtin_font (data
, len
);
3770 if (!state
.face
) _exit (1);
3772 realloctexts (texcount
);
3776 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
3778 errx (1, "pthread_create: %s", strerror (ret
));
3785 static void UNUSED_ATTR NO_OPTIMIZE_ATTR
refmacs (void) {}