Cosmetics
[llpp.git] / link.c
blob783b12f56eee8ffb6ebdea756e344205cd542258
1 /* lots of code c&p-ed directly from mupdf */
2 #define FIXME 0
4 #ifdef __clang__
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 "-Wmissing-prototypes"
11 #pragma GCC diagnostic ignored "-Wdouble-promotion"
12 #endif
14 extern char **environ;
16 #include <errno.h>
17 #include <stdio.h>
18 #include <ctype.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <signal.h>
23 #include <math.h>
24 #include <wchar.h>
25 #include <locale.h>
26 #include <langinfo.h>
28 #include <unistd.h>
29 #include <pthread.h>
30 #include <sys/uio.h>
31 #include <sys/time.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/utsname.h>
38 #include <spawn.h>
40 #include <regex.h>
41 #include <stdarg.h>
42 #include <limits.h>
43 #include <inttypes.h>
45 #ifdef CIDER
46 #include <OpenGL/gl.h>
47 #else
48 #include <GL/gl.h>
49 #endif
51 #pragma GCC diagnostic push
52 #ifdef __clang__
53 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
54 #endif
55 #pragma GCC diagnostic ignored "-Wpedantic"
56 #define CAML_NAME_SPACE
57 #include <caml/fail.h>
58 #include <caml/alloc.h>
59 #include <caml/memory.h>
60 #include <caml/unixsupport.h>
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wfloat-equal"
64 #include <mupdf/fitz.h>
65 #include <mupdf/pdf.h>
66 #pragma GCC diagnostic pop
68 #include <ft2build.h>
69 #include FT_FREETYPE_H
70 #pragma GCC diagnostic pop
72 #include "cutils.h"
74 #ifdef USE_NPOT
75 #define TEXT_TYPE GL_TEXTURE_2D
76 #else
77 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
78 #endif
80 #if 0
81 #define lprintf printf
82 #else
83 #define lprintf(...)
84 #endif
86 #define ARSERT(cond) for (;;) { \
87 if (!(cond)) { \
88 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
89 } \
90 break; \
93 struct slice {
94 int h;
95 int texindex;
98 struct tile {
99 int w, h;
100 int slicecount;
101 int sliceheight;
102 struct bo *pbo;
103 fz_pixmap *pixmap;
104 struct slice slices[1];
107 struct pagedim {
108 int pageno;
109 int rotate;
110 int left;
111 int tctmready;
112 fz_irect bounds;
113 fz_rect pagebox;
114 fz_rect mediabox;
115 fz_matrix ctm, zoomctm, tctm;
118 struct slink {
119 enum { SLINK, SANNOT } tag;
120 fz_irect bbox;
121 union {
122 fz_link *link;
123 fz_annot *annot;
124 } u;
127 struct annot {
128 fz_irect bbox;
129 fz_annot *annot;
132 struct page {
133 int tgen;
134 int sgen;
135 int agen;
136 int pageno;
137 int pdimno;
138 fz_stext_page *text;
139 fz_page *fzpage;
140 fz_display_list *dlist;
141 fz_link *links;
142 int slinkcount;
143 struct slink *slinks;
144 int annotcount;
145 struct annot *annots;
146 fz_stext_char *fmark, *lmark;
149 enum { FitWidth, FitProportional, FitPage };
151 static struct {
152 int sliceheight;
153 struct pagedim *pagedims;
154 int pagecount;
155 int pagedimcount;
156 fz_document *doc;
157 fz_context *ctx;
158 int w, h;
160 int texindex;
161 int texcount;
162 GLuint *texids;
164 GLenum texiform;
165 GLenum texform;
166 GLenum texty;
168 fz_colorspace *colorspace;
170 struct {
171 int w, h;
172 struct slice *slice;
173 } *texowners;
175 int rotate;
176 int fitmodel;
177 int trimmargins;
178 int needoutline;
179 int gen;
180 int aalevel;
182 int trimanew;
183 fz_irect trimfuzz;
184 fz_pixmap *pig;
186 pthread_t thread;
187 int csock;
188 FT_Face face;
190 char *trimcachepath;
191 int dirty;
193 GLuint stid;
195 int bo_usable;
196 GLuint boid;
198 void (*glBindBufferARB) (GLenum, GLuint);
199 GLboolean (*glUnmapBufferARB) (GLenum);
200 void *(*glMapBufferARB) (GLenum, GLenum);
201 void (*glBufferDataARB) (GLenum, GLsizei, void *, GLenum);
202 void (*glGenBuffersARB) (GLsizei, GLuint *);
203 void (*glDeleteBuffersARB) (GLsizei, GLuint *);
205 GLfloat texcoords[8];
206 GLfloat vertices[16];
208 #ifdef CACHE_PAGEREFS
209 struct {
210 int idx;
211 int count;
212 pdf_obj **objs;
213 pdf_document *pdf;
214 } pdflut;
215 #endif
216 int utf8cs;
217 } state;
219 struct bo {
220 GLuint id;
221 void *ptr;
222 size_t size;
225 static void UNUSED_ATTR debug_rect (const char *cap, fz_rect r)
227 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap, r.x0, r.y0, r.x1, r.y1);
230 static void UNUSED_ATTR debug_bbox (const char *cap, fz_irect r)
232 printf ("%s(bbox) %d,%d,%d,%d\n", cap, r.x0, r.y0, r.x1, r.y1);
235 static void UNUSED_ATTR debug_matrix (const char *cap, fz_matrix m)
237 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap,
238 m.a, m.b, m.c, m.d, m.e, m.f);
241 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
243 static void lock (const char *cap)
245 int ret = pthread_mutex_lock (&mutex);
246 if (ret) {
247 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
251 static void unlock (const char *cap)
253 int ret = pthread_mutex_unlock (&mutex);
254 if (ret) {
255 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
259 static int trylock (const char *cap)
261 int ret = pthread_mutex_trylock (&mutex);
262 if (ret && ret != EBUSY) {
263 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
265 return ret == EBUSY;
268 static int hasdata (void)
270 int ret, avail;
271 ret = ioctl (state.csock, FIONREAD, &avail);
272 if (ret) err (1, "hasdata: FIONREAD error ret=%d", ret);
273 return avail > 0;
276 CAMLprim value ml_hasdata (value fd_v)
278 CAMLparam1 (fd_v);
279 int ret, avail;
281 ret = ioctl (Int_val (fd_v), FIONREAD, &avail);
282 if (ret) uerror ("ioctl (FIONREAD)", Nothing);
283 CAMLreturn (Val_bool (avail > 0));
286 static void readdata (int fd, void *p, int size)
288 ssize_t n;
290 again:
291 n = read (fd, p, size);
292 if (n - size) {
293 if (n < 0 && errno == EINTR) goto again;
294 if (!n) errx (1, "EOF while reading");
295 errx (1, "read (fd %d, req %d, ret %zd)", fd, size, n);
299 static void writedata (int fd, char *p, int size)
301 ssize_t n;
302 uint32_t size4 = size;
303 struct iovec iov[2] = {
304 { .iov_base = &size4, .iov_len = 4 },
305 { .iov_base = p, .iov_len = size }
308 again:
309 n = writev (fd, iov, 2);
310 if (n < 0 && errno == EINTR) goto again;
311 if (n - size - 4) {
312 if (!n) errx (1, "EOF while writing data");
313 err (1, "writev (fd %d, req %d, ret %zd)", fd, size + 4, n);
317 static int readlen (int fd)
319 uint32_t u;
320 readdata (fd, &u, 4);
321 return u;
324 CAMLprim void ml_wcmd (value fd_v, value bytes_v, value len_v)
326 CAMLparam3 (fd_v, bytes_v, len_v);
327 writedata (Int_val (fd_v), &Byte (bytes_v, 0), Int_val (len_v));
328 CAMLreturn0;
331 CAMLprim value ml_rcmd (value fd_v)
333 CAMLparam1 (fd_v);
334 CAMLlocal1 (strdata_v);
335 int fd = Int_val (fd_v);
336 int len = readlen (fd);
337 strdata_v = caml_alloc_string (len);
338 readdata (fd, String_val (strdata_v), len);
339 CAMLreturn (strdata_v);
342 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt, ...)
344 char fbuf[64];
345 int size = sizeof (fbuf), len;
346 va_list ap;
347 char *buf = fbuf;
349 for (;;) {
350 va_start (ap, fmt);
351 len = vsnprintf (buf, size, fmt, ap);
352 va_end (ap);
354 if (len > -1) {
355 if (len < size - 4) {
356 writedata (state.csock, buf, len);
357 break;
359 else size = len + 5;
361 else {
362 err (1, "vsnprintf for `%s' failed", fmt);
364 buf = realloc (buf == fbuf ? NULL : buf, size);
365 if (!buf) err (1, "realloc for temp buf (%d bytes) failed", size);
367 if (buf != fbuf) free (buf);
370 static void closedoc (void)
372 #ifdef CACHE_PAGEREFS
373 if (state.pdflut.objs) {
374 for (int i = 0; i < state.pdflut.count; ++i) {
375 pdf_drop_obj (state.ctx, state.pdflut.objs[i]);
377 free (state.pdflut.objs);
378 state.pdflut.objs = NULL;
379 state.pdflut.idx = 0;
381 #endif
382 if (state.doc) {
383 fz_drop_document (state.ctx, state.doc);
384 state.doc = NULL;
388 static int openxref (char *filename, char *password, int layouth)
390 for (int i = 0; i < state.texcount; ++i) {
391 state.texowners[i].w = -1;
392 state.texowners[i].slice = NULL;
395 closedoc ();
397 state.dirty = 0;
398 if (state.pagedims) {
399 free (state.pagedims);
400 state.pagedims = NULL;
402 state.pagedimcount = 0;
404 fz_set_aa_level (state.ctx, state.aalevel);
405 state.doc = fz_open_document (state.ctx, filename);
406 if (fz_needs_password (state.ctx, state.doc)) {
407 if (password && !*password) {
408 printd ("pass");
409 return 0;
411 else {
412 int ok = fz_authenticate_password (state.ctx, state.doc, password);
413 if (!ok) {
414 printd ("pass fail");
415 return 0;
419 if (layouth >= 0)
420 fz_layout_document (state.ctx, state.doc, 460, layouth, 12);
421 state.pagecount = fz_count_pages (state.ctx, state.doc);
422 return 1;
425 static void docinfo (void)
427 struct { char *tag; char *name; } metatbl[] = {
428 { FZ_META_INFO_TITLE, "Title" },
429 { FZ_META_INFO_AUTHOR, "Author" },
430 { FZ_META_FORMAT, "Format" },
431 { FZ_META_ENCRYPTION, "Encryption" },
432 { "info:Creator", "Creator" },
433 { "info:Producer", "Producer" },
434 { "info:CreationDate", "Creation date" },
436 int len = 0;
437 char *buf = NULL;
439 for (size_t i = 0; i < sizeof (metatbl) / sizeof (metatbl[1]); ++i) {
440 int need;
441 again:
442 need = fz_lookup_metadata (state.ctx, state.doc,
443 metatbl[i].tag, buf, len);
444 if (need > 0) {
445 if (need <= len) {
446 printd ("info %s\t%s", metatbl[i].name, buf);
448 else {
449 buf = realloc (buf, need + 1);
450 if (!buf) err (1, "docinfo realloc %d", need + 1);
451 len = need + 1;
452 goto again;
456 free (buf);
458 printd ("infoend");
461 static void unlinktile (struct tile *tile)
463 for (int i = 0; i < tile->slicecount; ++i) {
464 struct slice *s = &tile->slices[i];
466 if (s->texindex != -1) {
467 if (state.texowners[s->texindex].slice == s) {
468 state.texowners[s->texindex].slice = NULL;
474 static void freepage (struct page *page)
476 if (!page) return;
477 if (page->text) {
478 fz_drop_stext_page (state.ctx, page->text);
480 if (page->slinks) {
481 free (page->slinks);
483 fz_drop_display_list (state.ctx, page->dlist);
484 fz_drop_page (state.ctx, page->fzpage);
485 free (page);
488 static void freetile (struct tile *tile)
490 unlinktile (tile);
491 if (!tile->pbo) {
492 #if 0
493 fz_drop_pixmap (state.ctx, tile->pixmap);
494 #else /* piggyback */
495 if (state.pig) {
496 fz_drop_pixmap (state.ctx, state.pig);
498 state.pig = tile->pixmap;
499 #endif
501 else {
502 free (tile->pbo);
503 fz_drop_pixmap (state.ctx, tile->pixmap);
505 free (tile);
508 static void trimctm (pdf_page *page, int pindex)
510 fz_matrix ctm;
511 struct pagedim *pdim = &state.pagedims[pindex];
513 if (!page) return;
514 if (!pdim->tctmready) {
515 fz_rect realbox, mediabox;
516 fz_matrix rm, sm, tm, im, ctm1, page_ctm;
518 fz_rotate (&rm, -pdim->rotate);
519 fz_scale (&sm, 1, -1);
520 fz_concat (&ctm, &rm, &sm);
521 realbox = pdim->mediabox;
522 fz_transform_rect (&realbox, &ctm);
523 fz_translate (&tm, -realbox.x0, -realbox.y0);
524 fz_concat (&ctm1, &ctm, &tm);
525 pdf_page_transform (state.ctx, page, &mediabox, &page_ctm);
526 fz_invert_matrix (&im, &page_ctm);
527 fz_concat (&ctm, &im, &ctm1);
528 pdim->tctm = ctm;
529 pdim->tctmready = 1;
533 static fz_matrix pagectm1 (fz_page *fzpage, struct pagedim *pdim)
535 fz_matrix ctm, tm;
536 ptrdiff_t pdimno = pdim - state.pagedims;
538 ARSERT (pdim - state.pagedims < INT_MAX);
539 if (pdf_specifics (state.ctx, state.doc)) {
540 trimctm (pdf_page_from_fz_page (state.ctx, fzpage), (int) pdimno);
541 fz_concat (&ctm, &pdim->tctm, &pdim->ctm);
543 else {
544 fz_translate (&tm, -pdim->mediabox.x0, -pdim->mediabox.y0);
545 fz_concat (&ctm, &tm, &pdim->ctm);
547 return ctm;
550 static fz_matrix pagectm (struct page *page)
552 return pagectm1 (page->fzpage, &state.pagedims[page->pdimno]);
555 static void *loadpage (int pageno, int pindex)
557 fz_device *dev;
558 struct page *page;
560 page = calloc (sizeof (struct page), 1);
561 if (!page) {
562 err (1, "calloc page %d", pageno);
565 page->dlist = fz_new_display_list (state.ctx, NULL);
566 dev = fz_new_list_device (state.ctx, page->dlist);
567 fz_try (state.ctx) {
568 page->fzpage = fz_load_page (state.ctx, state.doc, pageno);
569 fz_run_page (state.ctx, page->fzpage, dev,
570 &fz_identity, NULL);
572 fz_catch (state.ctx) {
573 page->fzpage = NULL;
575 fz_close_device (state.ctx, dev);
576 fz_drop_device (state.ctx, dev);
578 page->pdimno = pindex;
579 page->pageno = pageno;
580 page->sgen = state.gen;
581 page->agen = state.gen;
582 page->tgen = state.gen;
583 return page;
586 static struct tile *alloctile (int h)
588 int slicecount;
589 size_t tilesize;
590 struct tile *tile;
592 slicecount = (h + state.sliceheight - 1) / state.sliceheight;
593 tilesize = sizeof (*tile) + ((slicecount - 1) * sizeof (struct slice));
594 tile = calloc (tilesize, 1);
595 if (!tile) {
596 err (1, "cannot allocate tile (%zu bytes)", tilesize);
598 for (int i = 0; i < slicecount; ++i) {
599 int sh = fz_mini (h, state.sliceheight);
600 tile->slices[i].h = sh;
601 tile->slices[i].texindex = -1;
602 h -= sh;
604 tile->slicecount = slicecount;
605 tile->sliceheight = state.sliceheight;
606 return tile;
609 static struct tile *rendertile (struct page *page, int x, int y, int w, int h,
610 struct bo *pbo)
612 fz_rect rect;
613 fz_irect bbox;
614 fz_matrix ctm;
615 fz_device *dev;
616 struct tile *tile;
617 struct pagedim *pdim;
619 tile = alloctile (h);
620 pdim = &state.pagedims[page->pdimno];
622 bbox = pdim->bounds;
623 bbox.x0 += x;
624 bbox.y0 += y;
625 bbox.x1 = bbox.x0 + w;
626 bbox.y1 = bbox.y0 + h;
628 if (state.pig) {
629 if (state.pig->w == w
630 && state.pig->h == h
631 && state.pig->colorspace == state.colorspace) {
632 tile->pixmap = state.pig;
633 tile->pixmap->x = bbox.x0;
634 tile->pixmap->y = bbox.y0;
636 else {
637 fz_drop_pixmap (state.ctx, state.pig);
639 state.pig = NULL;
641 if (!tile->pixmap) {
642 if (pbo) {
643 tile->pixmap =
644 fz_new_pixmap_with_bbox_and_data (state.ctx, state.colorspace,
645 &bbox, NULL, 1, pbo->ptr);
646 tile->pbo = pbo;
648 else {
649 tile->pixmap =
650 fz_new_pixmap_with_bbox (state.ctx, state.colorspace, &bbox,
651 NULL, 1);
655 tile->w = w;
656 tile->h = h;
657 fz_clear_pixmap_with_value (state.ctx, tile->pixmap, 0xff);
659 dev = fz_new_draw_device (state.ctx, NULL, tile->pixmap);
660 ctm = pagectm (page);
661 fz_rect_from_irect (&rect, &bbox);
662 fz_run_display_list (state.ctx, page->dlist, dev, &ctm, &rect, NULL);
663 fz_close_device (state.ctx, dev);
664 fz_drop_device (state.ctx, dev);
666 return tile;
669 #ifdef CACHE_PAGEREFS
670 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
671 thanks to Robin Watts */
672 static void
673 pdf_collect_pages(pdf_document *doc, pdf_obj *node)
675 fz_context *ctx = state.ctx; /* doc->ctx; */
676 pdf_obj *kids;
677 int len;
679 if (state.pdflut.idx == state.pagecount) return;
681 kids = pdf_dict_gets (ctx, node, "Kids");
682 len = pdf_array_len (ctx, kids);
684 if (len == 0)
685 fz_throw (ctx, FZ_ERROR_GENERIC, "malformed pages tree");
687 if (pdf_mark_obj (ctx, node))
688 fz_throw (ctx, FZ_ERROR_GENERIC, "cycle in page tree");
689 for (int i = 0; i < len; i++) {
690 pdf_obj *kid = pdf_array_get (ctx, kids, i);
691 const char *type = pdf_to_name (ctx, pdf_dict_gets (ctx, kid, "Type"));
692 if (*type
693 ? !strcmp (type, "Pages")
694 : pdf_dict_gets (ctx, kid, "Kids")
695 && !pdf_dict_gets (ctx, kid, "MediaBox")) {
696 pdf_collect_pages (doc, kid);
698 else {
699 if (*type
700 ? strcmp (type, "Page") != 0
701 : !pdf_dict_gets (ctx, kid, "MediaBox"))
702 fz_warn (ctx, "non-page object in page tree (%s)", type);
703 state.pdflut.objs[state.pdflut.idx++] = pdf_keep_obj (ctx, kid);
706 pdf_unmark_obj (ctx, node);
709 static void
710 pdf_load_page_objs (pdf_document *doc)
712 pdf_obj *root = pdf_dict_gets (state.ctx,
713 pdf_trailer (state.ctx, doc), "Root");
714 pdf_obj *node = pdf_dict_gets (state.ctx, root, "Pages");
716 if (!node)
717 fz_throw (state.ctx, FZ_ERROR_GENERIC, "cannot find page tree");
719 state.pdflut.idx = 0;
720 pdf_collect_pages (doc, node);
722 #endif
724 static void initpdims (void)
726 double start, end;
727 FILE *trimf = NULL;
728 fz_rect rootmediabox = fz_empty_rect;
729 int pageno, trim, show;
730 int trimw = 0, cxcount;
731 fz_context *ctx = state.ctx;
732 pdf_document *pdf = pdf_specifics (ctx, state.doc);
734 fz_var (trimw);
735 fz_var (trimf);
736 fz_var (cxcount);
737 start = now ();
739 if (state.trimmargins && state.trimcachepath) {
740 trimf = fopen (state.trimcachepath, "rb");
741 if (!trimf) {
742 trimf = fopen (state.trimcachepath, "wb");
743 trimw = 1;
747 if (state.trimmargins || pdf)
748 cxcount = state.pagecount;
749 else
750 cxcount = fz_mini (state.pagecount, 1);
752 if (pdf) {
753 pdf_obj *obj;
754 obj = pdf_dict_getp (ctx, pdf_trailer (ctx, pdf),
755 "Root/Pages/MediaBox");
756 pdf_to_rect (ctx, obj, &rootmediabox);
759 #ifdef CACHE_PAGEREFS
760 if (pdf && (!state.pdflut.objs || state.pdflut.pdf != pdf)) {
761 state.pdflut.objs = calloc (sizeof (*state.pdflut.objs), cxcount);
762 if (!state.pdflut.objs) {
763 err (1, "malloc pageobjs %zu %d %zu failed",
764 sizeof (*state.pdflut.objs), cxcount,
765 sizeof (*state.pdflut.objs) * cxcount);
767 state.pdflut.count = cxcount;
768 pdf_load_page_objs (pdf);
769 state.pdflut.pdf = pdf;
771 #endif
773 for (pageno = 0; pageno < cxcount; ++pageno) {
774 int rotate = 0;
775 struct pagedim *p;
776 fz_rect mediabox = fz_empty_rect;
778 fz_var (rotate);
779 if (pdf) {
780 pdf_obj *pageref, *pageobj;
782 #ifdef CACHE_PAGEREFS
783 pageref = state.pdflut.objs[pageno];
784 #else
785 pageref = pdf_lookup_page_obj (ctx, pdf, pageno);
786 #endif
787 pageobj = pdf_resolve_indirect (ctx, pageref);
788 rotate = pdf_to_int (ctx, pdf_dict_gets (ctx, pageobj, "Rotate"));
790 if (state.trimmargins) {
791 pdf_obj *obj;
792 pdf_page *page;
794 fz_try (ctx) {
795 page = pdf_load_page (ctx, pdf, pageno);
796 obj = pdf_dict_gets (ctx, pageobj, "llpp.TrimBox");
797 trim = state.trimanew || !obj;
798 if (trim) {
799 fz_rect rect;
800 fz_device *dev;
801 fz_matrix ctm, page_ctm;
803 dev = fz_new_bbox_device (ctx, &rect);
804 pdf_page_transform (ctx, page, &mediabox, &page_ctm);
805 fz_invert_matrix (&ctm, &page_ctm);
806 pdf_run_page (ctx, page, dev, &fz_identity, NULL);
807 fz_close_device (ctx, dev);
808 fz_drop_device (ctx, dev);
810 rect.x0 += state.trimfuzz.x0;
811 rect.x1 += state.trimfuzz.x1;
812 rect.y0 += state.trimfuzz.y0;
813 rect.y1 += state.trimfuzz.y1;
814 fz_transform_rect (&rect, &ctm);
815 fz_intersect_rect (&rect, &mediabox);
817 if (!fz_is_empty_rect (&rect)) {
818 mediabox = rect;
821 obj = pdf_new_array (ctx, pdf, 4);
822 pdf_array_push (ctx, obj,
823 pdf_new_real (ctx, mediabox.x0));
824 pdf_array_push (ctx, obj,
825 pdf_new_real (ctx, mediabox.y0));
826 pdf_array_push (ctx, obj,
827 pdf_new_real (ctx, mediabox.x1));
828 pdf_array_push (ctx, obj,
829 pdf_new_real (ctx, mediabox.y1));
830 pdf_dict_puts (ctx, pageobj, "llpp.TrimBox", obj);
832 else {
833 mediabox.x0 = pdf_to_real (ctx,
834 pdf_array_get (ctx, obj, 0));
835 mediabox.y0 = pdf_to_real (ctx,
836 pdf_array_get (ctx, obj, 1));
837 mediabox.x1 = pdf_to_real (ctx,
838 pdf_array_get (ctx, obj, 2));
839 mediabox.y1 = pdf_to_real (ctx,
840 pdf_array_get (ctx, obj, 3));
843 fz_drop_page (ctx, &page->super);
844 show = trim ? pageno % 5 == 0 : pageno % 20 == 0;
845 if (show) {
846 printd ("progress %f Trimming %d",
847 (double) (pageno + 1) / state.pagecount,
848 pageno + 1);
851 fz_catch (ctx) {
852 printd ("emsg failed to load page %d", pageno);
855 else {
856 int empty = 0;
857 fz_rect cropbox;
859 pdf_to_rect (ctx,
860 pdf_dict_gets (ctx, pageobj, "MediaBox"),
861 &mediabox);
862 if (fz_is_empty_rect (&mediabox)) {
863 mediabox.x0 = 0;
864 mediabox.y0 = 0;
865 mediabox.x1 = 612;
866 mediabox.y1 = 792;
867 empty = 1;
870 pdf_to_rect (ctx,
871 pdf_dict_gets (ctx, pageobj, "CropBox"),
872 &cropbox);
873 if (!fz_is_empty_rect (&cropbox)) {
874 if (empty) {
875 mediabox = cropbox;
877 else {
878 fz_intersect_rect (&mediabox, &cropbox);
881 else {
882 if (empty) {
883 if (fz_is_empty_rect (&rootmediabox)) {
884 printd ("emsg cannot find page size for page %d",
885 pageno);
887 else {
888 mediabox = rootmediabox;
894 else {
895 if (state.trimmargins && trimw) {
896 fz_page *page;
898 fz_try (ctx) {
899 page = fz_load_page (ctx, state.doc, pageno);
900 fz_bound_page (ctx, page, &mediabox);
901 if (state.trimmargins) {
902 fz_rect rect;
903 fz_device *dev;
905 dev = fz_new_bbox_device (ctx, &rect);
906 fz_run_page (ctx, page, dev, &fz_identity, NULL);
907 fz_close_device (ctx, dev);
908 fz_drop_device (ctx, dev);
910 rect.x0 += state.trimfuzz.x0;
911 rect.x1 += state.trimfuzz.x1;
912 rect.y0 += state.trimfuzz.y0;
913 rect.y1 += state.trimfuzz.y1;
914 fz_intersect_rect (&rect, &mediabox);
916 if (!fz_is_empty_rect (&rect)) {
917 mediabox = rect;
920 fz_drop_page (ctx, page);
922 fz_catch (ctx) {
924 if (trimf) {
925 size_t n = fwrite (&mediabox, sizeof (mediabox), 1, trimf);
926 if (n - 1) {
927 err (1, "fwrite trim mediabox");
931 else {
932 if (trimf) {
933 size_t n = fread (&mediabox, sizeof (mediabox), 1, trimf);
934 if (n - 1) {
935 err (1, "fread trim mediabox %d", pageno);
938 else {
939 fz_page *page;
940 fz_try (ctx) {
941 page = fz_load_page (ctx, state.doc, pageno);
942 fz_bound_page (ctx, page, &mediabox);
943 fz_drop_page (ctx, page);
945 show = !state.trimmargins && pageno % 20 == 0;
946 if (show) {
947 printd ("progress %f Gathering dimensions %d",
948 (double) (pageno) / state.pagecount,
949 pageno);
952 fz_catch (ctx) {
953 printd ("emsg failed to load page %d", pageno);
959 if (state.pagedimcount == 0
960 || ((void) (p = &state.pagedims[state.pagedimcount-1])
961 , p->rotate != rotate)
962 || memcmp (&p->mediabox, &mediabox, sizeof (mediabox))) {
963 size_t size;
965 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
966 state.pagedims = realloc (state.pagedims, size);
967 if (!state.pagedims) {
968 err (1, "realloc pagedims to %zu (%d elems)",
969 size, state.pagedimcount + 1);
972 p = &state.pagedims[state.pagedimcount++];
973 p->rotate = rotate;
974 p->mediabox = mediabox;
975 p->pageno = pageno;
978 end = now ();
979 printd ("progress 1 %s %d pages in %f seconds",
980 state.trimmargins ? "Trimmed" : "Processed",
981 state.pagecount, end - start);
982 state.trimanew = 0;
983 if (trimf) {
984 if (fclose (trimf)) {
985 err (1, "fclose");
990 static void layout (void)
992 int pindex;
993 fz_rect box;
994 fz_matrix ctm, rm;
995 struct pagedim *p = NULL;
996 float zw, w, maxw = 0.0, zoom = 1.0;
998 if (state.pagedimcount == 0) return;
1000 switch (state.fitmodel) {
1001 case FitProportional:
1002 for (pindex = 0; pindex < state.pagedimcount; ++pindex) {
1003 float x0, x1;
1005 p = &state.pagedims[pindex];
1006 fz_rotate (&rm, p->rotate + state.rotate);
1007 box = p->mediabox;
1008 fz_transform_rect (&box, &rm);
1010 x0 = fz_min (box.x0, box.x1);
1011 x1 = fz_max (box.x0, box.x1);
1013 w = x1 - x0;
1014 maxw = fz_max (w, maxw);
1015 zoom = state.w / maxw;
1017 break;
1019 case FitPage:
1020 maxw = state.w;
1021 break;
1023 case FitWidth:
1024 break;
1026 default:
1027 ARSERT (0 && state.fitmodel);
1030 for (pindex = 0; pindex < state.pagedimcount; ++pindex) {
1031 fz_rect rect;
1032 fz_matrix tm, sm;
1034 p = &state.pagedims[pindex];
1035 fz_rotate (&ctm, state.rotate);
1036 fz_rotate (&rm, p->rotate + state.rotate);
1037 box = p->mediabox;
1038 fz_transform_rect (&box, &rm);
1039 w = box.x1 - box.x0;
1040 switch (state.fitmodel) {
1041 case FitProportional:
1042 p->left = (int) (((maxw - w) * zoom) / 2.f);
1043 break;
1044 case FitPage:
1046 float zh, h;
1047 zw = maxw / w;
1048 h = box.y1 - box.y0;
1049 zh = state.h / h;
1050 zoom = fz_min (zw, zh);
1051 p->left = (int) ((maxw - (w * zoom)) / 2.f);
1053 break;
1054 case FitWidth:
1055 p->left = 0;
1056 zoom = state.w / w;
1057 break;
1060 fz_scale (&p->zoomctm, zoom, zoom);
1061 fz_concat (&ctm, &p->zoomctm, &ctm);
1063 fz_rotate (&rm, p->rotate);
1064 p->pagebox = p->mediabox;
1065 fz_transform_rect (&p->pagebox, &rm);
1066 p->pagebox.x1 -= p->pagebox.x0;
1067 p->pagebox.y1 -= p->pagebox.y0;
1068 p->pagebox.x0 = 0;
1069 p->pagebox.y0 = 0;
1070 rect = p->pagebox;
1071 fz_transform_rect (&rect, &ctm);
1072 fz_round_rect (&p->bounds, &rect);
1073 p->ctm = ctm;
1075 fz_translate (&tm, 0, -p->mediabox.y1);
1076 fz_scale (&sm, zoom, -zoom);
1077 fz_concat (&ctm, &tm, &sm);
1079 p->tctmready = 0;
1082 do {
1083 int x0 = fz_mini (p->bounds.x0, p->bounds.x1);
1084 int y0 = fz_mini (p->bounds.y0, p->bounds.y1);
1085 int x1 = fz_maxi (p->bounds.x0, p->bounds.x1);
1086 int y1 = fz_maxi (p->bounds.y0, p->bounds.y1);
1087 int boundw = x1 - x0;
1088 int boundh = y1 - y0;
1090 printd ("pdim %d %d %d %d", p->pageno, boundw, boundh, p->left);
1091 } while (p-- != state.pagedims);
1094 struct pagedim *pdimofpageno (int pageno)
1096 struct pagedim *pdim = state.pagedims;
1098 for (int i = 0; i < state.pagedimcount; ++i) {
1099 if (state.pagedims[i].pageno > pageno)
1100 break;
1101 pdim = &state.pagedims[i];
1103 return pdim;
1106 static void recurse_outline (fz_outline *outline, int level)
1108 while (outline) {
1109 if (outline->page >= 0) {
1110 fz_point p = {.x = outline->x, .y = outline->y};
1111 struct pagedim *pdim = pdimofpageno (outline->page);
1112 int h = fz_maxi (fz_absi (pdim->bounds.y1 - pdim->bounds.y0), 0);
1113 fz_transform_point (&p, &pdim->ctm);
1114 printd ("o %d %d %d %d %s",
1115 level, outline->page, (int) p.y, h, outline->title);
1117 else {
1118 printd ("on %d %s", level, outline->title);
1120 if (outline->down) {
1121 recurse_outline (outline->down, level + 1);
1123 outline = outline->next;
1127 static void process_outline (void)
1129 fz_outline *outline;
1131 if (!state.needoutline || !state.pagedimcount) return;
1133 state.needoutline = 0;
1134 outline = fz_load_outline (state.ctx, state.doc);
1135 if (outline) {
1136 recurse_outline (outline, 0);
1137 fz_drop_outline (state.ctx, outline);
1141 static char *strofline (fz_stext_line *line)
1143 char *p;
1144 char utf8[10];
1145 fz_stext_char *ch;
1146 size_t size = 0, cap = 80;
1148 p = malloc (cap + 1);
1149 if (!p) return NULL;
1151 for (ch = line->first_char; ch; ch = ch->next) {
1152 int n = fz_runetochar (utf8, ch->c);
1153 if (size + n > cap) {
1154 cap *= 2;
1155 p = realloc (p, cap + 1);
1156 if (!p) return NULL;
1159 memcpy (p + size, utf8, n);
1160 size += n;
1162 p[size] = 0;
1163 return p;
1166 static int matchline (regex_t *re, fz_stext_line *line,
1167 int stop, int pageno, double start)
1169 int ret;
1170 char *p;
1171 regmatch_t rm;
1173 p = strofline (line);
1174 if (!p) return -1;
1176 ret = regexec (re, p, 1, &rm, 0);
1177 if (ret) {
1178 free (p);
1179 if (ret != REG_NOMATCH) {
1180 size_t size;
1181 char errbuf[80];
1182 size = regerror (ret, re, errbuf, sizeof (errbuf));
1183 printd ("msg regexec error `%.*s'",
1184 (int) size, errbuf);
1185 return -1;
1187 return 0;
1189 else {
1190 fz_point p1, p2, p3, p4;
1191 fz_rect s = {0,0,0,0}, e;
1192 fz_stext_char *ch;
1193 int o = 0;
1195 for (ch = line->first_char; ch; ch = ch->next) {
1196 o += fz_runelen (ch->c);
1197 if (o > rm.rm_so) {
1198 s = ch->bbox;
1199 break;
1202 for (;ch; ch = ch->next) {
1203 o += fz_runelen (ch->c);
1204 if (o > rm.rm_eo) break;
1206 e = ch->bbox;
1208 p1.x = s.x0;
1209 p1.y = s.y0;
1210 p2.x = e.x1;
1211 p2.y = s.y0;
1212 p3.x = e.x1;
1213 p3.y = e.y1;
1214 p4.x = s.x0;
1215 p4.y = e.y1;
1217 if (!stop) {
1218 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1219 pageno, 1,
1220 p1.x, p1.y,
1221 p2.x, p2.y,
1222 p3.x, p3.y,
1223 p4.x, p4.y);
1225 printd ("progress 1 found at %d `%.*s' in %f sec",
1226 pageno + 1, (int) (rm.rm_eo - rm.rm_so), &p[rm.rm_so],
1227 now () - start);
1229 else {
1230 printd ("match %d %d %f %f %f %f %f %f %f %f",
1231 pageno, 2,
1232 p1.x, p1.y,
1233 p2.x, p2.y,
1234 p3.x, p3.y,
1235 p4.x, p4.y);
1237 free (p);
1238 return 1;
1242 /* wishful thinking function */
1243 static void search (regex_t *re, int pageno, int y, int forward)
1245 fz_device *tdev;
1246 fz_stext_page *text;
1247 struct pagedim *pdim;
1248 int stop = 0, niters = 0;
1249 double start, end;
1250 fz_page *page;
1251 fz_stext_block *block;
1253 start = now ();
1254 while (pageno >= 0 && pageno < state.pagecount && !stop) {
1255 if (niters++ == 5) {
1256 niters = 0;
1257 if (hasdata ()) {
1258 printd ("progress 1 attention requested aborting search at %d",
1259 pageno);
1260 stop = 1;
1262 else {
1263 printd ("progress %f searching in page %d",
1264 (double) (pageno + 1) / state.pagecount,
1265 pageno);
1268 pdim = pdimofpageno (pageno);
1269 text = fz_new_stext_page (state.ctx, &pdim->mediabox);
1270 tdev = fz_new_stext_device (state.ctx, text, 0);
1272 page = fz_load_page (state.ctx, state.doc, pageno);
1274 fz_matrix ctm = pagectm1 (page, pdim);
1275 fz_run_page (state.ctx, page, tdev, &ctm, NULL);
1278 fz_close_device (state.ctx, tdev);
1279 fz_drop_device (state.ctx, tdev);
1281 if (forward) {
1282 for (block = text->first_block; block; block = block->next) {
1283 fz_stext_line *line;
1285 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1286 for (line = block->u.t.first_line; line; line = line->next) {
1287 if (line->bbox.y0 < y + 1) continue;
1289 switch (matchline (re, line, stop, pageno, start)) {
1290 case 0: break;
1291 case 1: stop = 1; break;
1292 case -1: stop = 1; goto endloop;
1297 else {
1298 for (block = text->last_block; block; block = block->prev) {
1299 fz_stext_line *line;
1301 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1302 for (line = block->u.t.last_line; line; line = line->prev) {
1303 if (line->bbox.y0 < y + 1) continue;
1305 switch (matchline (re, line, stop, pageno, start)) {
1306 case 0: break;
1307 case 1: stop = 1; break;
1308 case -1: stop = 1; goto endloop;
1314 if (forward) {
1315 pageno += 1;
1316 y = 0;
1318 else {
1319 pageno -= 1;
1320 y = INT_MAX;
1322 endloop:
1323 fz_drop_stext_page (state.ctx, text);
1324 fz_drop_page (state.ctx, page);
1326 end = now ();
1327 if (!stop) {
1328 printd ("progress 1 no matches %f sec", end - start);
1330 printd ("clearrects");
1333 static void set_tex_params (int colorspace)
1335 switch (colorspace) {
1336 case 0:
1337 state.texiform = GL_RGBA8;
1338 state.texform = GL_RGBA;
1339 state.texty = GL_UNSIGNED_BYTE;
1340 state.colorspace = fz_device_rgb (state.ctx);
1341 break;
1342 case 1:
1343 state.texiform = GL_LUMINANCE_ALPHA;
1344 state.texform = GL_LUMINANCE_ALPHA;
1345 state.texty = GL_UNSIGNED_BYTE;
1346 state.colorspace = fz_device_gray (state.ctx);
1347 break;
1348 default:
1349 errx (1, "invalid colorspce %d", colorspace);
1353 static void realloctexts (int texcount)
1355 size_t size;
1357 if (texcount == state.texcount) return;
1359 if (texcount < state.texcount) {
1360 glDeleteTextures (state.texcount - texcount,
1361 state.texids + texcount);
1364 size = texcount * (sizeof (*state.texids) + sizeof (*state.texowners));
1365 state.texids = realloc (state.texids, size);
1366 if (!state.texids) {
1367 err (1, "realloc texs %zu", size);
1370 state.texowners = (void *) (state.texids + texcount);
1371 if (texcount > state.texcount) {
1372 glGenTextures (texcount - state.texcount,
1373 state.texids + state.texcount);
1374 for (int i = state.texcount; i < texcount; ++i) {
1375 state.texowners[i].w = -1;
1376 state.texowners[i].slice = NULL;
1379 state.texcount = texcount;
1380 state.texindex = 0;
1383 static char *mbtoutf8 (char *s)
1385 char *p, *r;
1386 wchar_t *tmp;
1387 size_t i, ret, len;
1389 if (state.utf8cs) {
1390 return s;
1393 len = mbstowcs (NULL, s, strlen (s));
1394 if (len == 0 || len == (size_t) -1) {
1395 if (len)
1396 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno, strerror (errno));
1397 return s;
1400 tmp = calloc (len, sizeof (wchar_t));
1401 if (!tmp) {
1402 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1403 len, sizeof (wchar_t), errno, strerror (errno));
1404 return s;
1407 ret = mbstowcs (tmp, s, len);
1408 if (ret == (size_t) -1) {
1409 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1410 len, errno, strerror (errno));
1411 free (tmp);
1412 return s;
1415 len = 0;
1416 for (i = 0; i < ret; ++i) {
1417 len += fz_runelen (tmp[i]);
1420 p = r = malloc (len + 1);
1421 if (!r) {
1422 printd ("emsg mbtoutf8: malloc(%zu)", len);
1423 free (tmp);
1424 return s;
1427 for (i = 0; i < ret; ++i) {
1428 p += fz_runetochar (p, tmp[i]);
1430 *p = 0;
1431 free (tmp);
1432 return r;
1435 CAMLprim value ml_mbtoutf8 (value s_v)
1437 CAMLparam1 (s_v);
1438 CAMLlocal1 (ret_v);
1439 char *s, *r;
1441 s = String_val (s_v);
1442 r = mbtoutf8 (s);
1443 if (r == s) {
1444 ret_v = s_v;
1446 else {
1447 ret_v = caml_copy_string (r);
1448 free (r);
1450 CAMLreturn (ret_v);
1453 static void * mainloop (void UNUSED_ATTR *unused)
1455 char *p = NULL;
1456 int len, ret, oldlen = 0;
1458 fz_var (p);
1459 fz_var (oldlen);
1460 for (;;) {
1461 len = readlen (state.csock);
1462 if (len == 0) {
1463 errx (1, "readlen returned 0");
1466 if (oldlen < len + 1) {
1467 p = realloc (p, len + 1);
1468 if (!p) {
1469 err (1, "realloc %d failed", len + 1);
1471 oldlen = len + 1;
1473 readdata (state.csock, p, len);
1474 p[len] = 0;
1476 if (!strncmp ("open", p, 4)) {
1477 int off, usedoccss, ok = 0, layouth;
1478 char *password;
1479 char *filename;
1480 char *utf8filename;
1481 size_t filenamelen;
1483 fz_var (ok);
1484 ret = sscanf (p + 5, " %d %d %n", &usedoccss, &layouth, &off);
1485 if (ret != 2) {
1486 errx (1, "malformed open `%.*s' ret=%d", len, p, ret);
1489 filename = p + 5 + off;
1490 filenamelen = strlen (filename);
1491 password = filename + filenamelen + 1;
1493 if (password[strlen (password) + 1]) {
1494 fz_set_user_css (state.ctx, password + strlen (password) + 1);
1497 lock ("open");
1498 fz_set_use_document_css (state.ctx, usedoccss);
1499 fz_try (state.ctx) {
1500 ok = openxref (filename, password, layouth);
1502 fz_catch (state.ctx) {
1503 utf8filename = mbtoutf8 (filename);
1504 printd ("msg Could not open %s", utf8filename);
1505 if (utf8filename != filename) {
1506 free (utf8filename);
1509 if (ok) {
1510 docinfo ();
1511 initpdims ();
1513 unlock ("open");
1515 if (ok) {
1516 utf8filename = mbtoutf8 (filename);
1517 printd ("msg Opened %s (press h/F1 to get help)", utf8filename);
1518 if (utf8filename != filename) {
1519 free (utf8filename);
1521 state.needoutline = 1;
1524 else if (!strncmp ("cs", p, 2)) {
1525 int i, colorspace;
1527 ret = sscanf (p + 2, " %d", &colorspace);
1528 if (ret != 1) {
1529 errx (1, "malformed cs `%.*s' ret=%d", len, p, ret);
1531 lock ("cs");
1532 set_tex_params (colorspace);
1533 for (i = 0; i < state.texcount; ++i) {
1534 state.texowners[i].w = -1;
1535 state.texowners[i].slice = NULL;
1537 unlock ("cs");
1539 else if (!strncmp ("freepage", p, 8)) {
1540 void *ptr;
1542 ret = sscanf (p + 8, " %" SCNxPTR, (uintptr_t *) &ptr);
1543 if (ret != 1) {
1544 errx (1, "malformed freepage `%.*s' ret=%d", len, p, ret);
1546 lock ("freepage");
1547 freepage (ptr);
1548 unlock ("freepage");
1550 else if (!strncmp ("freetile", p, 8)) {
1551 void *ptr;
1553 ret = sscanf (p + 8, " %" SCNxPTR, (uintptr_t *) &ptr);
1554 if (ret != 1) {
1555 errx (1, "malformed freetile `%.*s' ret=%d", len, p, ret);
1557 lock ("freetile");
1558 freetile (ptr);
1559 unlock ("freetile");
1561 else if (!strncmp ("search", p, 6)) {
1562 int icase, pageno, y, len2, forward;
1563 char *pattern;
1564 regex_t re;
1566 ret = sscanf (p + 6, " %d %d %d %d,%n",
1567 &icase, &pageno, &y, &forward, &len2);
1568 if (ret != 4) {
1569 errx (1, "malformed search `%s' ret=%d", p, ret);
1572 pattern = p + 6 + len2;
1573 ret = regcomp (&re, pattern,
1574 REG_EXTENDED | (icase ? REG_ICASE : 0));
1575 if (ret) {
1576 char errbuf[80];
1577 size_t size;
1579 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1580 printd ("msg regcomp failed `%.*s'", (int) size, errbuf);
1582 else {
1583 search (&re, pageno, y, forward);
1584 regfree (&re);
1587 else if (!strncmp ("geometry", p, 8)) {
1588 int w, h, fitmodel;
1590 printd ("clear");
1591 ret = sscanf (p + 8, " %d %d %d", &w, &h, &fitmodel);
1592 if (ret != 3) {
1593 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1596 lock ("geometry");
1597 state.h = h;
1598 if (w != state.w) {
1599 state.w = w;
1600 for (int i = 0; i < state.texcount; ++i) {
1601 state.texowners[i].slice = NULL;
1604 state.fitmodel = fitmodel;
1605 layout ();
1606 process_outline ();
1608 state.gen++;
1609 unlock ("geometry");
1610 printd ("continue %d", state.pagecount);
1612 else if (!strncmp ("reqlayout", p, 9)) {
1613 char *nameddest;
1614 int rotate, off, h;
1615 int fitmodel;
1616 pdf_document *pdf;
1618 printd ("clear");
1619 ret = sscanf (p + 9, " %d %d %d %n",
1620 &rotate, &fitmodel, &h, &off);
1621 if (ret != 3) {
1622 errx (1, "bad reqlayout line `%.*s' ret=%d", len, p, ret);
1624 lock ("reqlayout");
1625 pdf = pdf_specifics (state.ctx, state.doc);
1626 if (state.rotate != rotate || state.fitmodel != fitmodel) {
1627 state.gen += 1;
1629 state.rotate = rotate;
1630 state.fitmodel = fitmodel;
1631 state.h = h;
1632 layout ();
1633 process_outline ();
1635 nameddest = p + 9 + off;
1636 if (pdf && nameddest && *nameddest) {
1637 fz_point xy;
1638 struct pagedim *pdim;
1639 int pageno = pdf_lookup_anchor (state.ctx, pdf, nameddest,
1640 &xy.x, &xy.y);
1641 pdim = pdimofpageno (pageno);
1642 fz_transform_point (&xy, &pdim->ctm);
1643 printd ("a %d %d %d", pageno, (int) xy.x, (int) xy.y);
1646 state.gen++;
1647 unlock ("reqlayout");
1648 printd ("continue %d", state.pagecount);
1650 else if (!strncmp ("page", p, 4)) {
1651 double a, b;
1652 struct page *page;
1653 int pageno, pindex;
1655 ret = sscanf (p + 4, " %d %d", &pageno, &pindex);
1656 if (ret != 2) {
1657 errx (1, "bad page line `%.*s' ret=%d", len, p, ret);
1660 lock ("page");
1661 a = now ();
1662 page = loadpage (pageno, pindex);
1663 b = now ();
1664 unlock ("page");
1666 printd ("page %" PRIxPTR " %f", (uintptr_t) page, b - a);
1668 else if (!strncmp ("tile", p, 4)) {
1669 int x, y, w, h;
1670 struct page *page;
1671 struct tile *tile;
1672 double a, b;
1673 void *data;
1675 ret = sscanf (p + 4, " %" SCNxPTR " %d %d %d %d %" SCNxPTR,
1676 (uintptr_t *) &page, &x, &y, &w, &h,
1677 (uintptr_t *) &data);
1678 if (ret != 6) {
1679 errx (1, "bad tile line `%.*s' ret=%d", len, p, ret);
1682 lock ("tile");
1683 a = now ();
1684 tile = rendertile (page, x, y, w, h, data);
1685 b = now ();
1686 unlock ("tile");
1688 printd ("tile %d %d %" PRIxPTR " %u %f",
1689 x, y, (uintptr_t) (tile),
1690 tile->w * tile->h * tile->pixmap->n,
1691 b - a);
1693 else if (!strncmp ("trimset", p, 7)) {
1694 fz_irect fuzz;
1695 int trimmargins;
1697 ret = sscanf (p + 7, " %d %d %d %d %d",
1698 &trimmargins, &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1699 if (ret != 5) {
1700 errx (1, "malformed trimset `%.*s' ret=%d", len, p, ret);
1702 lock ("trimset");
1703 state.trimmargins = trimmargins;
1704 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1705 state.trimanew = 1;
1706 state.trimfuzz = fuzz;
1708 unlock ("trimset");
1710 else if (!strncmp ("settrim", p, 7)) {
1711 fz_irect fuzz;
1712 int trimmargins;
1714 ret = sscanf (p + 7, " %d %d %d %d %d",
1715 &trimmargins, &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1716 if (ret != 5) {
1717 errx (1, "malformed settrim `%.*s' ret=%d", len, p, ret);
1719 printd ("clear");
1720 lock ("settrim");
1721 state.trimmargins = trimmargins;
1722 state.needoutline = 1;
1723 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1724 state.trimanew = 1;
1725 state.trimfuzz = fuzz;
1727 state.pagedimcount = 0;
1728 free (state.pagedims);
1729 state.pagedims = NULL;
1730 initpdims ();
1731 layout ();
1732 process_outline ();
1733 unlock ("settrim");
1734 printd ("continue %d", state.pagecount);
1736 else if (!strncmp ("sliceh", p, 6)) {
1737 int h;
1739 ret = sscanf (p + 6, " %d", &h);
1740 if (ret != 1) {
1741 errx (1, "malformed sliceh `%.*s' ret=%d", len, p, ret);
1743 if (h != state.sliceheight) {
1744 state.sliceheight = h;
1745 for (int i = 0; i < state.texcount; ++i) {
1746 state.texowners[i].w = -1;
1747 state.texowners[i].h = -1;
1748 state.texowners[i].slice = NULL;
1752 else if (!strncmp ("interrupt", p, 9)) {
1753 printd ("vmsg interrupted");
1755 else {
1756 errx (1, "unknown command %.*s", len, p);
1759 return 0;
1762 CAMLprim value ml_isexternallink (value uri_v)
1764 CAMLparam1 (uri_v);
1765 int ext = fz_is_external_link (state.ctx, String_val (uri_v));
1766 CAMLreturn (Val_bool (ext));
1769 CAMLprim value ml_uritolocation (value uri_v)
1771 CAMLparam1 (uri_v);
1772 CAMLlocal1 (ret_v);
1773 int pageno;
1774 fz_point xy;
1775 struct pagedim *pdim;
1777 pageno = fz_resolve_link (state.ctx, state.doc, String_val (uri_v),
1778 &xy.x, &xy.y);
1779 pdim = pdimofpageno (pageno);
1780 fz_transform_point (&xy, &pdim->ctm);
1781 ret_v = caml_alloc_tuple (3);
1782 Field (ret_v, 0) = Val_int (pageno);
1783 Field (ret_v, 1) = caml_copy_double ((double) xy.x);
1784 Field (ret_v, 2) = caml_copy_double ((double) xy.y);
1785 CAMLreturn (ret_v);
1788 CAMLprim value ml_realloctexts (value texcount_v)
1790 CAMLparam1 (texcount_v);
1791 int ok;
1793 if (trylock (__func__)) {
1794 ok = 0;
1795 goto done;
1797 realloctexts (Int_val (texcount_v));
1798 ok = 1;
1799 unlock (__func__);
1801 done:
1802 CAMLreturn (Val_bool (ok));
1805 static void recti (int x0, int y0, int x1, int y1)
1807 GLfloat *v = state.vertices;
1809 glVertexPointer (2, GL_FLOAT, 0, v);
1810 v[0] = x0; v[1] = y0;
1811 v[2] = x1; v[3] = y0;
1812 v[4] = x0; v[5] = y1;
1813 v[6] = x1; v[7] = y1;
1814 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1817 static void showsel (struct page *page, int ox, int oy)
1819 fz_irect bbox;
1820 fz_rect rect;
1821 fz_stext_block *block;
1822 int seen = 0;
1823 unsigned char selcolor[] = {15,15,15,140};
1825 if (!page->fmark || !page->lmark) return;
1827 glEnable (GL_BLEND);
1828 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1829 glColor4ubv (selcolor);
1831 ox += state.pagedims[page->pdimno].bounds.x0;
1832 oy += state.pagedims[page->pdimno].bounds.y0;
1834 for (block = page->text->first_block; block; block = block->next) {
1835 fz_stext_line *line;
1837 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1838 for (line = block->u.t.first_line; line; line = line->next) {
1839 fz_stext_char *ch;
1841 rect = fz_empty_rect;
1842 for (ch = line->first_char; ch; ch = ch->next) {
1843 if (ch == page->fmark) seen = 1;
1844 if (seen) fz_union_rect (&rect, &ch->bbox);
1845 if (ch == page->lmark) {
1846 fz_round_rect (&bbox, &rect);
1847 recti (bbox.x0 + ox, bbox.y0 + oy,
1848 bbox.x1 + ox, bbox.y1 + oy);
1849 goto done;
1852 fz_round_rect (&bbox, &rect);
1853 recti (bbox.x0 + ox, bbox.y0 + oy,
1854 bbox.x1 + ox, bbox.y1 + oy);
1857 done:
1858 glDisable (GL_BLEND);
1861 #pragma GCC diagnostic push
1862 #pragma GCC diagnostic ignored "-Wconversion"
1863 #include "glfont.c"
1864 #pragma GCC diagnostic pop
1866 static void stipplerect (fz_matrix *m,
1867 fz_point *p1,
1868 fz_point *p2,
1869 fz_point *p3,
1870 fz_point *p4,
1871 GLfloat *texcoords,
1872 GLfloat *vertices)
1874 fz_transform_point (p1, m);
1875 fz_transform_point (p2, m);
1876 fz_transform_point (p3, m);
1877 fz_transform_point (p4, m);
1879 float w, h, s, t;
1881 w = p2->x - p1->x;
1882 h = p2->y - p1->y;
1883 t = hypotf (w, h) * .25f;
1885 w = p3->x - p2->x;
1886 h = p3->y - p2->y;
1887 s = hypotf (w, h) * .25f;
1889 texcoords[0] = 0; vertices[0] = p1->x; vertices[1] = p1->y;
1890 texcoords[1] = t; vertices[2] = p2->x; vertices[3] = p2->y;
1892 texcoords[2] = 0; vertices[4] = p2->x; vertices[5] = p2->y;
1893 texcoords[3] = s; vertices[6] = p3->x; vertices[7] = p3->y;
1895 texcoords[4] = 0; vertices[8] = p3->x; vertices[9] = p3->y;
1896 texcoords[5] = t; vertices[10] = p4->x; vertices[11] = p4->y;
1898 texcoords[6] = 0; vertices[12] = p4->x; vertices[13] = p4->y;
1899 texcoords[7] = s; vertices[14] = p1->x; vertices[15] = p1->y;
1901 glDrawArrays (GL_LINES, 0, 8);
1904 static void solidrect (fz_matrix *m,
1905 fz_point *p1,
1906 fz_point *p2,
1907 fz_point *p3,
1908 fz_point *p4,
1909 GLfloat *vertices)
1911 fz_transform_point (p1, m);
1912 fz_transform_point (p2, m);
1913 fz_transform_point (p3, m);
1914 fz_transform_point (p4, m);
1915 vertices[0] = p1->x; vertices[1] = p1->y;
1916 vertices[2] = p2->x; vertices[3] = p2->y;
1918 vertices[4] = p3->x; vertices[5] = p3->y;
1919 vertices[6] = p4->x; vertices[7] = p4->y;
1920 glDrawArrays (GL_TRIANGLE_FAN, 0, 4);
1923 static void ensurelinks (struct page *page)
1925 if (!page->links)
1926 page->links = fz_load_links (state.ctx, page->fzpage);
1929 static void highlightlinks (struct page *page, int xoff, int yoff)
1931 fz_matrix ctm, tm, pm;
1932 fz_link *link;
1933 GLfloat *texcoords = state.texcoords;
1934 GLfloat *vertices = state.vertices;
1936 ensurelinks (page);
1938 glEnable (GL_TEXTURE_1D);
1939 glEnable (GL_BLEND);
1940 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1941 glBindTexture (GL_TEXTURE_1D, state.stid);
1943 xoff -= state.pagedims[page->pdimno].bounds.x0;
1944 yoff -= state.pagedims[page->pdimno].bounds.y0;
1945 fz_translate (&tm, xoff, yoff);
1946 pm = pagectm (page);
1947 fz_concat (&ctm, &pm, &tm);
1949 glTexCoordPointer (1, GL_FLOAT, 0, texcoords);
1950 glVertexPointer (2, GL_FLOAT, 0, vertices);
1952 for (link = page->links; link; link = link->next) {
1953 fz_point p1, p2, p3, p4;
1955 p1.x = link->rect.x0;
1956 p1.y = link->rect.y0;
1958 p2.x = link->rect.x1;
1959 p2.y = link->rect.y0;
1961 p3.x = link->rect.x1;
1962 p3.y = link->rect.y1;
1964 p4.x = link->rect.x0;
1965 p4.y = link->rect.y1;
1967 /* TODO: different colours for different schemes */
1968 if (fz_is_external_link (state.ctx, link->uri)) glColor3ub (0, 0, 255);
1969 else glColor3ub (255, 0, 0);
1971 stipplerect (&ctm, &p1, &p2, &p3, &p4, texcoords, vertices);
1974 for (int i = 0; i < page->annotcount; ++i) {
1975 fz_point p1, p2, p3, p4;
1976 struct annot *annot = &page->annots[i];
1978 p1.x = annot->bbox.x0;
1979 p1.y = annot->bbox.y0;
1981 p2.x = annot->bbox.x1;
1982 p2.y = annot->bbox.y0;
1984 p3.x = annot->bbox.x1;
1985 p3.y = annot->bbox.y1;
1987 p4.x = annot->bbox.x0;
1988 p4.y = annot->bbox.y1;
1990 glColor3ub (0, 0, 128);
1991 stipplerect (&ctm, &p1, &p2, &p3, &p4, texcoords, vertices);
1994 glDisable (GL_BLEND);
1995 glDisable (GL_TEXTURE_1D);
1998 static int compareslinks (const void *l, const void *r)
2000 struct slink const *ls = l;
2001 struct slink const *rs = r;
2002 if (ls->bbox.y0 == rs->bbox.y0) {
2003 return rs->bbox.x0 - rs->bbox.x0;
2005 return ls->bbox.y0 - rs->bbox.y0;
2008 static void droptext (struct page *page)
2010 if (page->text) {
2011 fz_drop_stext_page (state.ctx, page->text);
2012 page->fmark = NULL;
2013 page->lmark = NULL;
2014 page->text = NULL;
2018 static void dropannots (struct page *page)
2020 if (page->annots) {
2021 free (page->annots);
2022 page->annots = NULL;
2023 page->annotcount = 0;
2027 static void ensureannots (struct page *page)
2029 int i, count = 0;
2030 size_t annotsize = sizeof (*page->annots);
2031 fz_annot *annot;
2033 if (state.gen != page->agen) {
2034 dropannots (page);
2035 page->agen = state.gen;
2037 if (page->annots) return;
2039 for (annot = fz_first_annot (state.ctx, page->fzpage);
2040 annot;
2041 annot = fz_next_annot (state.ctx, annot)) {
2042 count++;
2045 if (count > 0) {
2046 page->annotcount = count;
2047 page->annots = calloc (count, annotsize);
2048 if (!page->annots) {
2049 err (1, "calloc annots %d", count);
2052 for (annot = fz_first_annot (state.ctx, page->fzpage), i = 0;
2053 annot;
2054 annot = fz_next_annot (state.ctx, annot), i++) {
2055 fz_rect rect;
2057 fz_bound_annot (state.ctx, annot, &rect);
2058 page->annots[i].annot = annot;
2059 fz_round_rect (&page->annots[i].bbox, &rect);
2064 static void dropslinks (struct page *page)
2066 if (page->slinks) {
2067 free (page->slinks);
2068 page->slinks = NULL;
2069 page->slinkcount = 0;
2071 if (page->links) {
2072 fz_drop_link (state.ctx, page->links);
2073 page->links = NULL;
2077 static void ensureslinks (struct page *page)
2079 fz_matrix ctm;
2080 int i, count;
2081 size_t slinksize = sizeof (*page->slinks);
2082 fz_link *link;
2084 ensureannots (page);
2085 if (state.gen != page->sgen) {
2086 dropslinks (page);
2087 page->sgen = state.gen;
2089 if (page->slinks) return;
2091 ensurelinks (page);
2092 ctm = pagectm (page);
2094 count = page->annotcount;
2095 for (link = page->links; link; link = link->next) {
2096 count++;
2098 if (count > 0) {
2099 int j;
2101 page->slinkcount = count;
2102 page->slinks = calloc (count, slinksize);
2103 if (!page->slinks) {
2104 err (1, "calloc slinks %d", count);
2107 for (i = 0, link = page->links; link; ++i, link = link->next) {
2108 fz_rect rect;
2110 rect = link->rect;
2111 fz_transform_rect (&rect, &ctm);
2112 page->slinks[i].tag = SLINK;
2113 page->slinks[i].u.link = link;
2114 fz_round_rect (&page->slinks[i].bbox, &rect);
2116 for (j = 0; j < page->annotcount; ++j, ++i) {
2117 fz_rect rect;
2118 fz_bound_annot (state.ctx, page->annots[j].annot, &rect);
2119 fz_transform_rect (&rect, &ctm);
2120 fz_round_rect (&page->slinks[i].bbox, &rect);
2122 page->slinks[i].tag = SANNOT;
2123 page->slinks[i].u.annot = page->annots[j].annot;
2125 qsort (page->slinks, count, slinksize, compareslinks);
2129 static void highlightslinks (struct page *page, int xoff, int yoff,
2130 int noff, char *targ, mlsize_t tlen, int hfsize)
2132 char buf[40];
2133 struct slink *slink;
2134 float x0, y0, x1, y1, w;
2136 ensureslinks (page);
2137 glColor3ub (0xc3, 0xb0, 0x91);
2138 for (int i = 0; i < page->slinkcount; ++i) {
2139 fmt_linkn (buf, i + noff);
2140 if (!tlen || !strncmp (targ, buf, tlen)) {
2141 slink = &page->slinks[i];
2143 x0 = slink->bbox.x0 + xoff - 5;
2144 y1 = slink->bbox.y0 + yoff - 5;
2145 y0 = y1 + 10 + hfsize;
2146 w = measure_string (state.face, hfsize, buf);
2147 x1 = x0 + w + 10;
2148 recti ((int) x0, (int) y0, (int) x1, (int) y1);
2152 glEnable (GL_BLEND);
2153 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2154 glEnable (GL_TEXTURE_2D);
2155 glColor3ub (0, 0, 0);
2156 for (int i = 0; i < page->slinkcount; ++i) {
2157 fmt_linkn (buf, i + noff);
2158 if (!tlen || !strncmp (targ, buf, tlen)) {
2159 slink = &page->slinks[i];
2161 x0 = slink->bbox.x0 + xoff;
2162 y0 = slink->bbox.y0 + yoff + hfsize;
2163 draw_string (state.face, hfsize, x0, y0, buf);
2166 glDisable (GL_TEXTURE_2D);
2167 glDisable (GL_BLEND);
2170 static void uploadslice (struct tile *tile, struct slice *slice)
2172 int offset;
2173 struct slice *slice1;
2174 unsigned char *texdata;
2176 offset = 0;
2177 for (slice1 = tile->slices; slice != slice1; slice1++) {
2178 offset += slice1->h * tile->w * tile->pixmap->n;
2180 if (slice->texindex != -1 && slice->texindex < state.texcount
2181 && state.texowners[slice->texindex].slice == slice) {
2182 glBindTexture (TEXT_TYPE, state.texids[slice->texindex]);
2184 else {
2185 int subimage = 0;
2186 int texindex = state.texindex++ % state.texcount;
2188 if (state.texowners[texindex].w == tile->w) {
2189 if (state.texowners[texindex].h >= slice->h) {
2190 subimage = 1;
2192 else {
2193 state.texowners[texindex].h = slice->h;
2196 else {
2197 state.texowners[texindex].h = slice->h;
2200 state.texowners[texindex].w = tile->w;
2201 state.texowners[texindex].slice = slice;
2202 slice->texindex = texindex;
2204 glBindTexture (TEXT_TYPE, state.texids[texindex]);
2205 #if TEXT_TYPE == GL_TEXTURE_2D
2206 glTexParameteri (TEXT_TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2207 glTexParameteri (TEXT_TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2208 glTexParameteri (TEXT_TYPE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2209 glTexParameteri (TEXT_TYPE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2210 #endif
2211 if (tile->pbo) {
2212 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, tile->pbo->id);
2213 texdata = 0;
2215 else {
2216 texdata = tile->pixmap->samples;
2218 if (subimage) {
2219 glTexSubImage2D (TEXT_TYPE,
2223 tile->w,
2224 slice->h,
2225 state.texform,
2226 state.texty,
2227 texdata+offset
2230 else {
2231 glTexImage2D (TEXT_TYPE,
2233 state.texiform,
2234 tile->w,
2235 slice->h,
2237 state.texform,
2238 state.texty,
2239 texdata+offset
2242 if (tile->pbo) {
2243 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
2248 CAMLprim void ml_begintiles (void)
2250 glEnable (TEXT_TYPE);
2251 glTexCoordPointer (2, GL_FLOAT, 0, state.texcoords);
2252 glVertexPointer (2, GL_FLOAT, 0, state.vertices);
2255 CAMLprim void ml_endtiles (void)
2257 glDisable (TEXT_TYPE);
2260 CAMLprim void ml_drawtile (value args_v, value ptr_v)
2262 CAMLparam2 (args_v, ptr_v);
2263 int dispx = Int_val (Field (args_v, 0));
2264 int dispy = Int_val (Field (args_v, 1));
2265 int dispw = Int_val (Field (args_v, 2));
2266 int disph = Int_val (Field (args_v, 3));
2267 int tilex = Int_val (Field (args_v, 4));
2268 int tiley = Int_val (Field (args_v, 5));
2269 char *s = String_val (ptr_v);
2270 struct tile *tile = parse_pointer (__func__, s);
2271 int slicey, firstslice;
2272 struct slice *slice;
2273 GLfloat *texcoords = state.texcoords;
2274 GLfloat *vertices = state.vertices;
2276 firstslice = tiley / tile->sliceheight;
2277 slice = &tile->slices[firstslice];
2278 slicey = tiley % tile->sliceheight;
2280 while (disph > 0) {
2281 int dh;
2283 dh = slice->h - slicey;
2284 dh = fz_mini (disph, dh);
2285 uploadslice (tile, slice);
2287 texcoords[0] = tilex; texcoords[1] = slicey;
2288 texcoords[2] = tilex+dispw; texcoords[3] = slicey;
2289 texcoords[4] = tilex; texcoords[5] = slicey+dh;
2290 texcoords[6] = tilex+dispw; texcoords[7] = slicey+dh;
2292 vertices[0] = dispx; vertices[1] = dispy;
2293 vertices[2] = dispx+dispw; vertices[3] = dispy;
2294 vertices[4] = dispx; vertices[5] = dispy+dh;
2295 vertices[6] = dispx+dispw; vertices[7] = dispy+dh;
2297 #if TEXT_TYPE == GL_TEXTURE_2D
2298 for (int i = 0; i < 8; ++i) {
2299 texcoords[i] /= ((i & 1) == 0 ? tile->w : slice->h);
2301 #endif
2303 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
2304 dispy += dh;
2305 disph -= dh;
2306 slice++;
2307 ARSERT (!(slice - tile->slices >= tile->slicecount && disph > 0));
2308 slicey = 0;
2310 CAMLreturn0;
2313 static void drawprect (struct page *page, int xoff, int yoff, value rects_v)
2315 fz_matrix ctm, tm, pm;
2316 fz_point p1, p2, p3, p4;
2317 GLfloat *vertices = state.vertices;
2318 double *v = (double *) rects_v;
2320 xoff -= state.pagedims[page->pdimno].bounds.x0;
2321 yoff -= state.pagedims[page->pdimno].bounds.y0;
2322 fz_translate (&tm, xoff, yoff);
2323 pm = pagectm (page);
2324 fz_concat (&ctm, &pm, &tm);
2326 glEnable (GL_BLEND);
2327 glVertexPointer (2, GL_FLOAT, 0, vertices);
2329 glColor4dv (v);
2330 p1.x = (float) v[4];
2331 p1.y = (float) v[5];
2333 p2.x = (float) v[6];
2334 p2.y = (float) v[5];
2336 p3.x = (float) v[6];
2337 p3.y = (float) v[7];
2339 p4.x = (float) v[4];
2340 p4.y = (float) v[7];
2341 solidrect (&ctm, &p1, &p2, &p3, &p4, vertices);
2342 glDisable (GL_BLEND);
2345 CAMLprim value ml_postprocess (value ptr_v, value hlinks_v,
2346 value xoff_v, value yoff_v,
2347 value li_v)
2349 CAMLparam5 (ptr_v, hlinks_v, xoff_v, yoff_v, li_v);
2350 int xoff = Int_val (xoff_v);
2351 int yoff = Int_val (yoff_v);
2352 int noff = Int_val (Field (li_v, 0));
2353 char *targ = String_val (Field (li_v, 1));
2354 mlsize_t tlen = caml_string_length (Field (li_v, 1));
2355 int hfsize = Int_val (Field (li_v, 2));
2356 char *s = String_val (ptr_v);
2357 int hlmask = Int_val (hlinks_v);
2358 struct page *page = parse_pointer (__func__, s);
2360 if (!page->fzpage) {
2361 /* deal with loadpage failed pages */
2362 goto done;
2365 if (trylock (__func__)) {
2366 noff = -1;
2367 goto done;
2370 ensureannots (page);
2371 if (hlmask & 1) highlightlinks (page, xoff, yoff);
2372 if (hlmask & 2) {
2373 highlightslinks (page, xoff, yoff, noff, targ, tlen, hfsize);
2374 noff = page->slinkcount;
2376 if (page->tgen == state.gen) {
2377 showsel (page, xoff, yoff);
2379 unlock (__func__);
2381 done:
2382 CAMLreturn (Val_int (noff));
2385 CAMLprim void ml_drawprect (value ptr_v, value xoff_v, value yoff_v,
2386 value rects_v)
2388 CAMLparam4 (ptr_v, xoff_v, yoff_v, rects_v);
2389 int xoff = Int_val (xoff_v);
2390 int yoff = Int_val (yoff_v);
2391 char *s = String_val (ptr_v);
2392 struct page *page = parse_pointer (__func__, s);
2394 drawprect (page, xoff, yoff, rects_v);
2395 CAMLreturn0;
2398 static struct annot *getannot (struct page *page, int x, int y)
2400 fz_point p;
2401 fz_matrix ctm;
2402 const fz_matrix *tctm;
2403 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
2405 if (!page->annots) return NULL;
2407 if (pdf) {
2408 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
2409 tctm = &state.pagedims[page->pdimno].tctm;
2411 else {
2412 tctm = &fz_identity;
2415 p.x = x;
2416 p.y = y;
2418 fz_concat (&ctm, tctm, &state.pagedims[page->pdimno].ctm);
2419 fz_invert_matrix (&ctm, &ctm);
2420 fz_transform_point (&p, &ctm);
2422 if (pdf) {
2423 for (int i = 0; i < page->annotcount; ++i) {
2424 struct annot *a = &page->annots[i];
2425 fz_rect rect;
2427 fz_bound_annot (state.ctx, a->annot, &rect);
2428 if (p.x >= rect.x0 && p.x <= rect.x1) {
2429 if (p.y >= rect.y0 && p.y <= rect.y1)
2430 return a;
2434 return NULL;
2437 static fz_link *getlink (struct page *page, int x, int y)
2439 fz_point p;
2440 fz_matrix ctm;
2441 fz_link *link;
2443 ensureslinks (page);
2445 p.x = x;
2446 p.y = y;
2448 ctm = pagectm (page);
2449 fz_invert_matrix (&ctm, &ctm);
2450 fz_transform_point (&p, &ctm);
2452 for (link = page->links; link; link = link->next) {
2453 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
2454 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
2455 return link;
2459 return NULL;
2462 static void ensuretext (struct page *page)
2464 if (state.gen != page->tgen) {
2465 droptext (page);
2466 page->tgen = state.gen;
2468 if (!page->text) {
2469 fz_matrix ctm;
2470 fz_device *tdev;
2472 page->text = fz_new_stext_page (state.ctx,
2473 &state.pagedims[page->pdimno].mediabox);
2474 tdev = fz_new_stext_device (state.ctx, page->text, 0);
2475 ctm = pagectm (page);
2476 fz_run_display_list (state.ctx, page->dlist,
2477 tdev, &ctm, &fz_infinite_rect, NULL);
2478 fz_close_device (state.ctx, tdev);
2479 fz_drop_device (state.ctx, tdev);
2483 CAMLprim value ml_find_page_with_links (value start_page_v, value dir_v)
2485 CAMLparam2 (start_page_v, dir_v);
2486 CAMLlocal1 (ret_v);
2487 int i, dir = Int_val (dir_v);
2488 int start_page = Int_val (start_page_v);
2489 int end_page = dir > 0 ? state.pagecount : -1;
2490 pdf_document *pdf;
2492 fz_var (end_page);
2493 ret_v = Val_int (0);
2494 lock (__func__);
2495 pdf = pdf_specifics (state.ctx, state.doc);
2496 for (i = start_page + dir; i != end_page; i += dir) {
2497 int found;
2499 fz_var (found);
2500 if (pdf) {
2501 pdf_page *page = NULL;
2503 fz_var (page);
2504 fz_try (state.ctx) {
2505 page = pdf_load_page (state.ctx, pdf, i);
2506 found = !!page->links || !!page->annots;
2508 fz_catch (state.ctx) {
2509 found = 0;
2511 if (page) {
2512 fz_drop_page (state.ctx, &page->super);
2515 else {
2516 fz_page *page = fz_load_page (state.ctx, state.doc, i);
2517 fz_link *link = fz_load_links (state.ctx, page);
2518 found = !!link;
2519 fz_drop_link (state.ctx, link);
2520 fz_drop_page (state.ctx, page);
2523 if (found) {
2524 ret_v = caml_alloc_small (1, 1);
2525 Field (ret_v, 0) = Val_int (i);
2526 goto unlock;
2529 unlock:
2530 unlock (__func__);
2531 CAMLreturn (ret_v);
2534 enum { dir_first, dir_last };
2535 enum { dir_first_visible, dir_left, dir_right, dir_down, dir_up };
2537 CAMLprim value ml_findlink (value ptr_v, value dir_v)
2539 CAMLparam2 (ptr_v, dir_v);
2540 CAMLlocal2 (ret_v, pos_v);
2541 struct page *page;
2542 int dirtag, i, slinkindex;
2543 struct slink *found = NULL ,*slink;
2544 char *s = String_val (ptr_v);
2546 page = parse_pointer (__func__, s);
2547 ret_v = Val_int (0);
2548 lock (__func__);
2549 ensureslinks (page);
2551 if (Is_block (dir_v)) {
2552 dirtag = Tag_val (dir_v);
2553 switch (dirtag) {
2554 case dir_first_visible:
2556 int x0, y0, dir, first_index, last_index;
2558 pos_v = Field (dir_v, 0);
2559 x0 = Int_val (Field (pos_v, 0));
2560 y0 = Int_val (Field (pos_v, 1));
2561 dir = Int_val (Field (pos_v, 2));
2563 if (dir >= 0) {
2564 dir = 1;
2565 first_index = 0;
2566 last_index = page->slinkcount;
2568 else {
2569 first_index = page->slinkcount - 1;
2570 last_index = -1;
2573 for (i = first_index; i != last_index; i += dir) {
2574 slink = &page->slinks[i];
2575 if (slink->bbox.y0 >= y0 && slink->bbox.x0 >= x0) {
2576 found = slink;
2577 break;
2581 break;
2583 case dir_left:
2584 slinkindex = Int_val (Field (dir_v, 0));
2585 found = &page->slinks[slinkindex];
2586 for (i = slinkindex - 1; i >= 0; --i) {
2587 slink = &page->slinks[i];
2588 if (slink->bbox.x0 < found->bbox.x0) {
2589 found = slink;
2590 break;
2593 break;
2595 case dir_right:
2596 slinkindex = Int_val (Field (dir_v, 0));
2597 found = &page->slinks[slinkindex];
2598 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2599 slink = &page->slinks[i];
2600 if (slink->bbox.x0 > found->bbox.x0) {
2601 found = slink;
2602 break;
2605 break;
2607 case dir_down:
2608 slinkindex = Int_val (Field (dir_v, 0));
2609 found = &page->slinks[slinkindex];
2610 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2611 slink = &page->slinks[i];
2612 if (slink->bbox.y0 >= found->bbox.y0) {
2613 found = slink;
2614 break;
2617 break;
2619 case dir_up:
2620 slinkindex = Int_val (Field (dir_v, 0));
2621 found = &page->slinks[slinkindex];
2622 for (i = slinkindex - 1; i >= 0; --i) {
2623 slink = &page->slinks[i];
2624 if (slink->bbox.y0 <= found->bbox.y0) {
2625 found = slink;
2626 break;
2629 break;
2632 else {
2633 dirtag = Int_val (dir_v);
2634 switch (dirtag) {
2635 case dir_first:
2636 found = page->slinks;
2637 break;
2639 case dir_last:
2640 if (page->slinks) {
2641 found = page->slinks + (page->slinkcount - 1);
2643 break;
2646 if (found) {
2647 ret_v = caml_alloc_small (2, 1);
2648 Field (ret_v, 0) = Val_int (found - page->slinks);
2651 unlock (__func__);
2652 CAMLreturn (ret_v);
2655 enum { uuri, utext, uannot };
2657 CAMLprim value ml_getlink (value ptr_v, value n_v)
2659 CAMLparam2 (ptr_v, n_v);
2660 CAMLlocal4 (ret_v, tup_v, str_v, gr_v);
2661 fz_link *link;
2662 struct page *page;
2663 char *s = String_val (ptr_v);
2664 struct slink *slink;
2666 ret_v = Val_int (0);
2667 page = parse_pointer (__func__, s);
2669 lock (__func__);
2670 ensureslinks (page);
2671 slink = &page->slinks[Int_val (n_v)];
2672 if (slink->tag == SLINK) {
2673 link = slink->u.link;
2674 str_v = caml_copy_string (link->uri);
2675 ret_v = caml_alloc_small (1, uuri);
2676 Field (ret_v, 0) = str_v;
2678 else {
2679 ret_v = caml_alloc_small (1, uannot);
2680 tup_v = caml_alloc_tuple (2);
2681 Field (ret_v, 0) = tup_v;
2682 Field (tup_v, 0) = ptr_v;
2683 Field (tup_v, 1) = n_v;
2685 unlock (__func__);
2687 CAMLreturn (ret_v);
2690 CAMLprim value ml_getannotcontents (value ptr_v, value n_v)
2692 CAMLparam2 (ptr_v, n_v);
2693 CAMLlocal1 (ret_v);
2694 pdf_document *pdf;
2695 char *contents = NULL;
2697 lock (__func__);
2698 pdf = pdf_specifics (state.ctx, state.doc);
2699 if (pdf) {
2700 char *s = String_val (ptr_v);
2701 struct page *page;
2702 struct slink *slink;
2704 page = parse_pointer (__func__, s);
2705 slink = &page->slinks[Int_val (n_v)];
2706 contents = pdf_copy_annot_contents (state.ctx,
2707 (pdf_annot *) slink->u.annot);
2709 unlock (__func__);
2710 if (contents) {
2711 ret_v = caml_copy_string (contents);
2712 fz_free (state.ctx, contents);
2714 else {
2715 ret_v = caml_copy_string ("");
2717 CAMLreturn (ret_v);
2720 CAMLprim value ml_getlinkcount (value ptr_v)
2722 CAMLparam1 (ptr_v);
2723 struct page *page;
2724 char *s = String_val (ptr_v);
2726 page = parse_pointer (__func__, s);
2727 CAMLreturn (Val_int (page->slinkcount));
2730 CAMLprim value ml_getlinkrect (value ptr_v, value n_v)
2732 CAMLparam2 (ptr_v, n_v);
2733 CAMLlocal1 (ret_v);
2734 struct page *page;
2735 struct slink *slink;
2736 char *s = String_val (ptr_v);
2738 page = parse_pointer (__func__, s);
2739 ret_v = caml_alloc_tuple (4);
2740 lock (__func__);
2741 ensureslinks (page);
2743 slink = &page->slinks[Int_val (n_v)];
2744 Field (ret_v, 0) = Val_int (slink->bbox.x0);
2745 Field (ret_v, 1) = Val_int (slink->bbox.y0);
2746 Field (ret_v, 2) = Val_int (slink->bbox.x1);
2747 Field (ret_v, 3) = Val_int (slink->bbox.y1);
2748 unlock (__func__);
2749 CAMLreturn (ret_v);
2752 CAMLprim value ml_whatsunder (value ptr_v, value x_v, value y_v)
2754 CAMLparam3 (ptr_v, x_v, y_v);
2755 CAMLlocal4 (ret_v, tup_v, str_v, gr_v);
2756 fz_link *link;
2757 struct annot *annot;
2758 struct page *page;
2759 char *ptr = String_val (ptr_v);
2760 int x = Int_val (x_v), y = Int_val (y_v);
2761 struct pagedim *pdim;
2763 ret_v = Val_int (0);
2764 if (trylock (__func__)) {
2765 goto done;
2768 page = parse_pointer (__func__, ptr);
2769 pdim = &state.pagedims[page->pdimno];
2770 x += pdim->bounds.x0;
2771 y += pdim->bounds.y0;
2774 annot = getannot (page, x, y);
2775 if (annot) {
2776 int i, n = -1;
2778 ensureslinks (page);
2779 for (i = 0; i < page->slinkcount; ++i) {
2780 if (page->slinks[i].tag == SANNOT
2781 && page->slinks[i].u.annot == annot->annot) {
2782 n = i;
2783 break;
2786 ret_v = caml_alloc_small (1, uannot);
2787 tup_v = caml_alloc_tuple (2);
2788 Field (ret_v, 0) = tup_v;
2789 Field (tup_v, 0) = ptr_v;
2790 Field (tup_v, 1) = Val_int (n);
2791 goto unlock;
2795 link = getlink (page, x, y);
2796 if (link) {
2797 str_v = caml_copy_string (link->uri);
2798 ret_v = caml_alloc_small (1, uuri);
2799 Field (ret_v, 0) = str_v;
2801 else {
2802 fz_rect *b;
2803 fz_stext_block *block;
2805 ensuretext (page);
2807 for (block = page->text->first_block; block; block = block->next) {
2808 fz_stext_line *line;
2810 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
2811 b = &block->bbox;
2812 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2813 continue;
2815 for (line = block->u.t.first_line; line; line = line->next) {
2816 fz_stext_char *ch;
2818 b = &line->bbox;
2819 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2820 continue;
2822 for (ch = line->first_char; ch; ch = ch->next) {
2823 b = &ch->bbox;
2825 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1) {
2826 const char *n2 = fz_font_name (state.ctx, ch->font);
2827 FT_FaceRec *face = fz_font_ft_face (state.ctx,
2828 ch->font);
2830 if (!n2) n2 = "<unknown font>";
2832 if (face && face->family_name) {
2833 char *s;
2834 char *n1 = face->family_name;
2835 size_t l1 = strlen (n1);
2836 size_t l2 = strlen (n2);
2838 if (l1 != l2 || memcmp (n1, n2, l1)) {
2839 s = malloc (l1 + l2 + 2);
2840 if (s) {
2841 memcpy (s, n2, l2);
2842 s[l2] = '=';
2843 memcpy (s + l2 + 1, n1, l1 + 1);
2844 str_v = caml_copy_string (s);
2845 free (s);
2849 if (str_v == Val_unit) {
2850 str_v = caml_copy_string (n2);
2852 ret_v = caml_alloc_small (1, utext);
2853 Field (ret_v, 0) = str_v;
2854 goto unlock;
2860 unlock:
2861 unlock (__func__);
2863 done:
2864 CAMLreturn (ret_v);
2867 enum { mark_page, mark_block, mark_line, mark_word };
2869 CAMLprim void ml_clearmark (value ptr_v)
2871 CAMLparam1 (ptr_v);
2872 char *s = String_val (ptr_v);
2873 struct page *page;
2875 if (trylock (__func__)) {
2876 goto done;
2879 page = parse_pointer (__func__, s);
2880 page->fmark = NULL;
2881 page->lmark = NULL;
2883 unlock (__func__);
2884 done:
2885 CAMLreturn0;
2888 static int uninteresting (int c)
2890 return c == ' ' || c == '\n' || c == '\t' || c == '\n' || c == '\r'
2891 || ispunct (c);
2894 CAMLprim value ml_markunder (value ptr_v, value x_v, value y_v, value mark_v)
2896 CAMLparam4 (ptr_v, x_v, y_v, mark_v);
2897 CAMLlocal1 (ret_v);
2898 fz_rect *b;
2899 struct page *page;
2900 fz_stext_line *line;
2901 fz_stext_block *block;
2902 struct pagedim *pdim;
2903 int mark = Int_val (mark_v);
2904 char *s = String_val (ptr_v);
2905 int x = Int_val (x_v), y = Int_val (y_v);
2907 ret_v = Val_bool (0);
2908 if (trylock (__func__)) {
2909 goto done;
2912 page = parse_pointer (__func__, s);
2913 pdim = &state.pagedims[page->pdimno];
2915 ensuretext (page);
2917 if (mark == mark_page) {
2918 page->fmark = page->text->first_block->u.t.first_line->first_char;
2919 page->lmark = page->text->last_block->u.t.last_line->last_char;
2920 ret_v = Val_bool (1);
2921 goto unlock;
2924 x += pdim->bounds.x0;
2925 y += pdim->bounds.y0;
2927 for (block = page->text->first_block; block; block = block->next) {
2928 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
2929 b = &block->bbox;
2930 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2931 continue;
2933 if (mark == mark_block) {
2934 page->fmark = block->u.t.first_line->first_char;
2935 page->lmark = block->u.t.last_line->last_char;
2936 ret_v = Val_bool (1);
2937 goto unlock;
2940 for (line = block->u.t.first_line; line; line = line->next) {
2941 fz_stext_char *ch;
2943 b = &line->bbox;
2944 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2945 continue;
2947 if (mark == mark_line) {
2948 page->fmark = line->first_char;
2949 page->lmark = line->last_char;
2950 ret_v = Val_bool (1);
2951 goto unlock;
2954 for (ch = line->first_char; ch; ch = ch->next) {
2955 fz_stext_char *ch2, *first = NULL, *last = NULL;
2956 b = &ch->bbox;
2957 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1) {
2958 for (ch2 = line->first_char; ch2 != ch; ch2 = ch2->next) {
2959 if (uninteresting (ch2->c)) first = NULL;
2960 else if (!first) first = ch2;
2962 for (ch2 = ch; ch2; ch2 = ch2->next) {
2963 if (uninteresting (ch2->c)) break;
2964 last = ch2;
2967 page->fmark = first;
2968 page->lmark = last;
2969 ret_v = Val_bool (1);
2970 goto unlock;
2975 unlock:
2976 if (!Bool_val (ret_v)) {
2977 page->fmark = NULL;
2978 page->lmark = NULL;
2980 unlock (__func__);
2982 done:
2983 CAMLreturn (ret_v);
2986 CAMLprim value ml_rectofblock (value ptr_v, value x_v, value y_v)
2988 CAMLparam3 (ptr_v, x_v, y_v);
2989 CAMLlocal2 (ret_v, res_v);
2990 fz_rect *b = NULL;
2991 struct page *page;
2992 struct pagedim *pdim;
2993 fz_stext_block *block;
2994 char *s = String_val (ptr_v);
2995 int x = Int_val (x_v), y = Int_val (y_v);
2997 ret_v = Val_int (0);
2998 if (trylock (__func__)) {
2999 goto done;
3002 page = parse_pointer (__func__, s);
3003 pdim = &state.pagedims[page->pdimno];
3004 x += pdim->bounds.x0;
3005 y += pdim->bounds.y0;
3007 ensuretext (page);
3009 for (block = page->text->first_block; block; block = block->next) {
3010 switch (block->type) {
3011 case FZ_STEXT_BLOCK_TEXT:
3012 b = &block->bbox;
3013 break;
3015 case FZ_STEXT_BLOCK_IMAGE:
3016 b = &block->bbox;
3017 break;
3019 default:
3020 continue;
3023 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1)
3024 break;
3025 b = NULL;
3027 if (b) {
3028 res_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
3029 ret_v = caml_alloc_small (1, 1);
3030 Store_double_field (res_v, 0, (double) b->x0);
3031 Store_double_field (res_v, 1, (double) b->x1);
3032 Store_double_field (res_v, 2, (double) b->y0);
3033 Store_double_field (res_v, 3, (double) b->y1);
3034 Field (ret_v, 0) = res_v;
3036 unlock (__func__);
3038 done:
3039 CAMLreturn (ret_v);
3042 CAMLprim void ml_seltext (value ptr_v, value rect_v)
3044 CAMLparam2 (ptr_v, rect_v);
3045 fz_rect b;
3046 struct page *page;
3047 struct pagedim *pdim;
3048 char *s = String_val (ptr_v);
3049 int x0, x1, y0, y1;
3050 fz_stext_char *ch;
3051 fz_stext_line *line;
3052 fz_stext_block *block;
3053 fz_stext_char *fc, *lc;
3055 if (trylock (__func__)) {
3056 goto done;
3059 page = parse_pointer (__func__, s);
3060 ensuretext (page);
3062 pdim = &state.pagedims[page->pdimno];
3063 x0 = Int_val (Field (rect_v, 0)) + pdim->bounds.x0;
3064 y0 = Int_val (Field (rect_v, 1)) + pdim->bounds.y0;
3065 x1 = Int_val (Field (rect_v, 2)) + pdim->bounds.x0;
3066 y1 = Int_val (Field (rect_v, 3)) + pdim->bounds.y0;
3068 if (y0 > y1) {
3069 int t = y0;
3070 y0 = y1;
3071 y1 = t;
3072 x0 = x1;
3073 x1 = t;
3076 fc = page->fmark;
3077 lc = page->lmark;
3079 for (block = page->text->first_block; block; block = block->next) {
3080 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
3081 for (line = block->u.t.first_line; line; line = line->next) {
3082 for (ch = line->first_char; ch; ch = ch->next) {
3083 b = ch->bbox;
3084 if (x0 >= b.x0 && x0 <= b.x1 && y0 >= b.y0 && y0 <= b.y1) {
3085 fc = ch;
3087 if (x1 >= b.x0 && x1 <= b.x1 && y1 >= b.y0 && y1 <= b.y1) {
3088 lc = ch;
3093 if (x1 < x0 && fc == lc) {
3094 fz_stext_char *t;
3096 t = fc;
3097 fc = lc;
3098 lc = t;
3101 page->fmark = fc;
3102 page->lmark = lc;
3104 unlock (__func__);
3106 done:
3107 CAMLreturn0;
3110 static int pipechar (FILE *f, fz_stext_char *ch)
3112 char buf[4];
3113 int len;
3114 size_t ret;
3116 len = fz_runetochar (buf, ch->c);
3117 ret = fwrite (buf, len, 1, f);
3118 if (ret != 1) {
3119 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3120 len, ret, errno, strerror (errno));
3121 return -1;
3123 return 0;
3126 CAMLprim value ml_spawn (value command_v, value fds_v)
3128 CAMLparam2 (command_v, fds_v);
3129 CAMLlocal2 (l_v, tup_v);
3130 int ret, ret1;
3131 pid_t pid = (pid_t) -1;
3132 char *msg = NULL;
3133 value earg_v = Nothing;
3134 posix_spawnattr_t attr;
3135 posix_spawn_file_actions_t fa;
3136 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
3138 argv[2] = String_val (command_v);
3140 if ((ret = posix_spawn_file_actions_init (&fa)) != 0) {
3141 unix_error (ret, "posix_spawn_file_actions_init", Nothing);
3144 if ((ret = posix_spawnattr_init (&attr)) != 0) {
3145 msg = "posix_spawnattr_init";
3146 goto fail1;
3149 #ifdef POSIX_SPAWN_USEVFORK
3150 if ((ret = posix_spawnattr_setflags (&attr, POSIX_SPAWN_USEVFORK)) != 0) {
3151 msg = "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3152 goto fail;
3154 #endif
3156 for (l_v = fds_v; l_v != Val_int (0); l_v = Field (l_v, 1)) {
3157 int fd1, fd2;
3159 tup_v = Field (l_v, 0);
3160 fd1 = Int_val (Field (tup_v, 0));
3161 fd2 = Int_val (Field (tup_v, 1));
3162 if (fd2 < 0) {
3163 if ((ret = posix_spawn_file_actions_addclose (&fa, fd1)) != 0) {
3164 msg = "posix_spawn_file_actions_addclose";
3165 earg_v = tup_v;
3166 goto fail;
3169 else {
3170 if ((ret = posix_spawn_file_actions_adddup2 (&fa, fd1, fd2)) != 0) {
3171 msg = "posix_spawn_file_actions_adddup2";
3172 earg_v = tup_v;
3173 goto fail;
3178 if ((ret = posix_spawn (&pid, "/bin/sh", &fa, &attr, argv, environ))) {
3179 msg = "posix_spawn";
3180 goto fail;
3183 fail:
3184 if ((ret1 = posix_spawnattr_destroy (&attr)) != 0) {
3185 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1, strerror (ret1));
3188 fail1:
3189 if ((ret1 = posix_spawn_file_actions_destroy (&fa)) != 0) {
3190 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3191 ret1, strerror (ret1));
3194 if (msg)
3195 unix_error (ret, msg, earg_v);
3197 CAMLreturn (Val_int (pid));
3200 CAMLprim value ml_hassel (value ptr_v)
3202 CAMLparam1 (ptr_v);
3203 CAMLlocal1 (ret_v);
3204 struct page *page;
3205 char *s = String_val (ptr_v);
3207 ret_v = Val_bool (0);
3208 if (trylock (__func__)) {
3209 goto done;
3212 page = parse_pointer (__func__, s);
3213 ret_v = Val_bool (page->fmark && page->lmark);
3214 unlock (__func__);
3215 done:
3216 CAMLreturn (ret_v);
3219 CAMLprim void ml_copysel (value fd_v, value ptr_v)
3221 CAMLparam2 (fd_v, ptr_v);
3222 FILE *f;
3223 int seen = 0;
3224 struct page *page;
3225 fz_stext_line *line;
3226 fz_stext_block *block;
3227 int fd = Int_val (fd_v);
3228 char *s = String_val (ptr_v);
3230 if (trylock (__func__)) {
3231 goto done;
3234 page = parse_pointer (__func__, s);
3236 if (!page->fmark || !page->lmark) {
3237 printd ("emsg nothing to copy on page %d", page->pageno);
3238 goto unlock;
3241 f = fdopen (fd, "w");
3242 if (!f) {
3243 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3244 fd, errno, strerror (errno));
3245 f = stdout;
3248 for (block = page->text->first_block; block; block = block->next) {
3249 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
3250 for (line = block->u.t.first_line; line; line = line->next) {
3251 fz_stext_char *ch;
3252 for (ch = line->first_char; ch; ch = ch->next) {
3253 if (seen || ch == page->fmark) {
3254 do {
3255 pipechar (f, ch);
3256 if (ch == page->lmark) goto close;
3257 } while ((ch = ch->next));
3258 seen = 1;
3259 break;
3262 if (seen) fputc ('\n', f);
3265 close:
3266 if (f != stdout) {
3267 int ret = fclose (f);
3268 fd = -1;
3269 if (ret == -1) {
3270 if (errno != ECHILD) {
3271 printd ("emsg failed to close sel pipe: %d:%s",
3272 errno, strerror (errno));
3276 unlock:
3277 unlock (__func__);
3279 done:
3280 if (fd >= 0) {
3281 if (close (fd)) {
3282 printd ("emsg failed to close sel pipe: %d:%s",
3283 errno, strerror (errno));
3286 CAMLreturn0;
3289 CAMLprim value ml_getpdimrect (value pagedimno_v)
3291 CAMLparam1 (pagedimno_v);
3292 CAMLlocal1 (ret_v);
3293 int pagedimno = Int_val (pagedimno_v);
3294 fz_rect box;
3296 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
3297 if (trylock (__func__)) {
3298 box = fz_empty_rect;
3300 else {
3301 box = state.pagedims[pagedimno].mediabox;
3302 unlock (__func__);
3305 Store_double_field (ret_v, 0, (double) box.x0);
3306 Store_double_field (ret_v, 1, (double) box.x1);
3307 Store_double_field (ret_v, 2, (double) box.y0);
3308 Store_double_field (ret_v, 3, (double) box.y1);
3310 CAMLreturn (ret_v);
3313 CAMLprim value ml_zoom_for_height (value winw_v, value winh_v,
3314 value dw_v, value cols_v)
3316 CAMLparam4 (winw_v, winh_v, dw_v, cols_v);
3317 CAMLlocal1 (ret_v);
3318 int i;
3319 float zoom = -1.;
3320 float maxh = 0.0;
3321 struct pagedim *p;
3322 float winw = Int_val (winw_v);
3323 float winh = Int_val (winh_v);
3324 float dw = Int_val (dw_v);
3325 float cols = Int_val (cols_v);
3326 float pw = 1.0, ph = 1.0;
3328 if (trylock (__func__)) {
3329 goto done;
3332 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3333 float w = p->pagebox.x1 / cols;
3334 float h = p->pagebox.y1;
3335 if (h > maxh) {
3336 maxh = h;
3337 ph = h;
3338 if (state.fitmodel != FitProportional) pw = w;
3340 if ((state.fitmodel == FitProportional) && w > pw) pw = w;
3343 zoom = (((winh / ph) * pw) + dw) / winw;
3344 unlock (__func__);
3345 done:
3346 ret_v = caml_copy_double ((double) zoom);
3347 CAMLreturn (ret_v);
3350 CAMLprim value ml_getmaxw (value unit_v)
3352 CAMLparam1 (unit_v);
3353 CAMLlocal1 (ret_v);
3354 int i;
3355 float maxw = -1.;
3356 struct pagedim *p;
3358 if (trylock (__func__)) {
3359 goto done;
3362 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3363 float w = p->pagebox.x1;
3364 maxw = fz_max (maxw, w);
3367 unlock (__func__);
3368 done:
3369 ret_v = caml_copy_double ((double) maxw);
3370 CAMLreturn (ret_v);
3373 CAMLprim value ml_draw_string (value pt_v, value x_v, value y_v, value string_v)
3375 CAMLparam4 (pt_v, x_v, y_v, string_v);
3376 CAMLlocal1 (ret_v);
3377 int pt = Int_val(pt_v);
3378 int x = Int_val (x_v);
3379 int y = Int_val (y_v);
3380 double w;
3382 w = (double) draw_string (state.face, pt, x, y, String_val (string_v));
3383 ret_v = caml_copy_double (w);
3384 CAMLreturn (ret_v);
3387 CAMLprim value ml_measure_string (value pt_v, value string_v)
3389 CAMLparam2 (pt_v, string_v);
3390 CAMLlocal1 (ret_v);
3391 int pt = Int_val (pt_v);
3392 double w;
3394 w = (double) measure_string (state.face, pt, String_val (string_v));
3395 ret_v = caml_copy_double (w);
3396 CAMLreturn (ret_v);
3399 CAMLprim value ml_getpagebox (value opaque_v)
3401 CAMLparam1 (opaque_v);
3402 CAMLlocal1 (ret_v);
3403 fz_rect rect;
3404 fz_irect bbox;
3405 fz_matrix ctm;
3406 fz_device *dev;
3407 char *s = String_val (opaque_v);
3408 struct page *page = parse_pointer (__func__, s);
3410 ret_v = caml_alloc_tuple (4);
3411 dev = fz_new_bbox_device (state.ctx, &rect);
3413 ctm = pagectm (page);
3414 fz_run_page (state.ctx, page->fzpage, dev, &ctm, NULL);
3416 fz_close_device (state.ctx, dev);
3417 fz_drop_device (state.ctx, dev);
3418 fz_round_rect (&bbox, &rect);
3419 Field (ret_v, 0) = Val_int (bbox.x0);
3420 Field (ret_v, 1) = Val_int (bbox.y0);
3421 Field (ret_v, 2) = Val_int (bbox.x1);
3422 Field (ret_v, 3) = Val_int (bbox.y1);
3424 CAMLreturn (ret_v);
3427 CAMLprim void ml_setaalevel (value level_v)
3429 CAMLparam1 (level_v);
3431 state.aalevel = Int_val (level_v);
3432 CAMLreturn0;
3435 #ifndef CIDER
3436 CAMLprim value ml_keysymtoutf8 (value keysym_v)
3438 CAMLparam1 (keysym_v);
3439 CAMLlocal1 (str_v);
3440 KeySym keysym = Int_val (keysym_v);
3441 Rune rune;
3442 extern long keysym2ucs (KeySym);
3443 int len;
3444 char buf[5];
3446 rune = (Rune) keysym2ucs (keysym);
3447 len = fz_runetochar (buf, rune);
3448 buf[len] = 0;
3449 str_v = caml_copy_string (buf);
3450 CAMLreturn (str_v);
3452 #else
3453 CAMLprim value ml_keysymtoutf8 (value keysym_v)
3455 CAMLparam1 (keysym_v);
3456 CAMLlocal1 (str_v);
3457 long ucs_v = Long_val (keysym_v);
3458 int len;
3459 char buf[5];
3461 len = fz_runetochar (buf, (int) ucs_v);
3462 buf[len] = 0;
3463 str_v = caml_copy_string (buf);
3464 CAMLreturn (str_v);
3466 #endif
3468 enum { piunknown, pilinux, pimacos, pibsd };
3470 CAMLprim value ml_platform (value unit_v)
3472 CAMLparam1 (unit_v);
3473 CAMLlocal2 (tup_v, arr_v);
3474 int platid = piunknown;
3475 struct utsname buf;
3477 #if defined __linux__
3478 platid = pilinux;
3479 #elif defined __DragonFly__ || defined __FreeBSD__
3480 || defined __OpenBSD__ || defined __NetBSD__
3481 platid = pibsd;
3482 #elif defined __APPLE__
3483 platid = pimacos;
3484 #endif
3485 if (uname (&buf)) err (1, "uname");
3487 tup_v = caml_alloc_tuple (2);
3489 char const *sar[] = {
3490 buf.sysname,
3491 buf.release,
3492 buf.version,
3493 buf.machine,
3494 NULL
3496 arr_v = caml_copy_string_array (sar);
3498 Field (tup_v, 0) = Val_int (platid);
3499 Field (tup_v, 1) = arr_v;
3500 CAMLreturn (tup_v);
3503 CAMLprim void ml_cloexec (value fd_v)
3505 CAMLparam1 (fd_v);
3506 int fd = Int_val (fd_v);
3508 if (fcntl (fd, F_SETFD, FD_CLOEXEC, 1)) {
3509 uerror ("fcntl", Nothing);
3511 CAMLreturn0;
3514 CAMLprim value ml_getpbo (value w_v, value h_v, value cs_v)
3516 CAMLparam2 (w_v, h_v);
3517 CAMLlocal1 (ret_v);
3518 struct bo *pbo;
3519 int w = Int_val (w_v);
3520 int h = Int_val (h_v);
3521 int cs = Int_val (cs_v);
3523 if (state.bo_usable) {
3524 pbo = calloc (sizeof (*pbo), 1);
3525 if (!pbo) {
3526 err (1, "calloc pbo");
3529 switch (cs) {
3530 case 0:
3531 case 1:
3532 pbo->size = w*h*4;
3533 break;
3534 case 2:
3535 pbo->size = w*h*2;
3536 break;
3537 default:
3538 errx (1, "%s: invalid colorspace %d", __func__, cs);
3541 state.glGenBuffersARB (1, &pbo->id);
3542 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, pbo->id);
3543 state.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB, (GLsizei) pbo->size,
3544 NULL, GL_STREAM_DRAW);
3545 pbo->ptr = state.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB,
3546 GL_READ_WRITE);
3547 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
3548 if (!pbo->ptr) {
3549 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3550 state.glDeleteBuffersARB (1, &pbo->id);
3551 free (pbo);
3552 ret_v = caml_copy_string ("0");
3554 else {
3555 int res;
3556 char *s;
3558 res = snprintf (NULL, 0, "%" PRIxPTR, (uintptr_t) pbo);
3559 if (res < 0) {
3560 err (1, "snprintf %" PRIxPTR " failed", (uintptr_t) pbo);
3562 s = malloc (res+1);
3563 if (!s) {
3564 err (1, "malloc %d bytes failed", res+1);
3566 res = sprintf (s, "%" PRIxPTR, (uintptr_t) pbo);
3567 if (res < 0) {
3568 err (1, "sprintf %" PRIxPTR " failed", (uintptr_t) pbo);
3570 ret_v = caml_copy_string (s);
3571 free (s);
3574 else {
3575 ret_v = caml_copy_string ("0");
3577 CAMLreturn (ret_v);
3580 CAMLprim void ml_freepbo (value s_v)
3582 CAMLparam1 (s_v);
3583 char *s = String_val (s_v);
3584 struct tile *tile = parse_pointer (__func__, s);
3586 if (tile->pbo) {
3587 state.glDeleteBuffersARB (1, &tile->pbo->id);
3588 tile->pbo->id = -1;
3589 tile->pbo->ptr = NULL;
3590 tile->pbo->size = -1;
3592 CAMLreturn0;
3595 CAMLprim void ml_unmappbo (value s_v)
3597 CAMLparam1 (s_v);
3598 char *s = String_val (s_v);
3599 struct tile *tile = parse_pointer (__func__, s);
3601 if (tile->pbo) {
3602 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, tile->pbo->id);
3603 if (state.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) == GL_FALSE) {
3604 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3606 tile->pbo->ptr = NULL;
3607 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
3609 CAMLreturn0;
3612 static void setuppbo (void)
3614 extern void (*wsigladdr (const char *name)) (void);
3615 #pragma GCC diagnostic push
3616 #ifdef __clang__
3617 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3618 #endif
3619 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3620 state.bo_usable = GPA (glBindBufferARB)
3621 && GPA (glUnmapBufferARB)
3622 && GPA (glMapBufferARB)
3623 && GPA (glBufferDataARB)
3624 && GPA (glGenBuffersARB)
3625 && GPA (glDeleteBuffersARB);
3626 #undef GPA
3627 #pragma GCC diagnostic pop
3630 CAMLprim value ml_bo_usable (void)
3632 return Val_bool (state.bo_usable);
3635 CAMLprim value ml_unproject (value ptr_v, value x_v, value y_v)
3637 CAMLparam3 (ptr_v, x_v, y_v);
3638 CAMLlocal2 (ret_v, tup_v);
3639 struct page *page;
3640 char *s = String_val (ptr_v);
3641 int x = Int_val (x_v), y = Int_val (y_v);
3642 struct pagedim *pdim;
3643 fz_point p;
3644 fz_matrix ctm;
3646 page = parse_pointer (__func__, s);
3647 pdim = &state.pagedims[page->pdimno];
3649 ret_v = Val_int (0);
3650 if (trylock (__func__)) {
3651 goto done;
3654 if (pdf_specifics (state.ctx, state.doc)) {
3655 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
3656 ctm = state.pagedims[page->pdimno].tctm;
3658 else {
3659 ctm = fz_identity;
3661 p.x = x + pdim->bounds.x0;
3662 p.y = y + pdim->bounds.y0;
3664 fz_concat (&ctm, &pdim->tctm, &pdim->ctm);
3665 fz_invert_matrix (&ctm, &ctm);
3666 fz_transform_point (&p, &ctm);
3668 tup_v = caml_alloc_tuple (2);
3669 ret_v = caml_alloc_small (1, 1);
3670 Field (tup_v, 0) = Val_int (p.x);
3671 Field (tup_v, 1) = Val_int (p.y);
3672 Field (ret_v, 0) = tup_v;
3674 unlock (__func__);
3675 done:
3676 CAMLreturn (ret_v);
3679 CAMLprim value ml_project (value ptr_v, value pageno_v, value pdimno_v,
3680 value x_v, value y_v)
3682 CAMLparam5 (ptr_v, pageno_v, pdimno_v, x_v, y_v);
3683 CAMLlocal1 (ret_v);
3684 struct page *page;
3685 char *s = String_val (ptr_v);
3686 int pageno = Int_val (pageno_v);
3687 int pdimno = Int_val (pdimno_v);
3688 float x = (float) Double_val (x_v), y = (float) Double_val (y_v);
3689 struct pagedim *pdim;
3690 fz_point p;
3691 fz_matrix ctm;
3693 ret_v = Val_int (0);
3694 lock (__func__);
3696 if (!*s) {
3697 page = loadpage (pageno, pdimno);
3699 else {
3700 page = parse_pointer (__func__, s);
3702 pdim = &state.pagedims[pdimno];
3704 if (pdf_specifics (state.ctx, state.doc)) {
3705 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
3706 ctm = state.pagedims[page->pdimno].tctm;
3708 else {
3709 ctm = fz_identity;
3711 p.x = x + pdim->bounds.x0;
3712 p.y = y + pdim->bounds.y0;
3714 fz_concat (&ctm, &pdim->tctm, &pdim->ctm);
3715 fz_transform_point (&p, &ctm);
3717 ret_v = caml_alloc_tuple (2);
3718 Field (ret_v, 0) = caml_copy_double ((double) p.x);
3719 Field (ret_v, 1) = caml_copy_double ((double) p.y);
3721 if (!*s) {
3722 freepage (page);
3724 unlock (__func__);
3725 CAMLreturn (ret_v);
3728 CAMLprim void ml_addannot (value ptr_v, value x_v, value y_v,
3729 value contents_v)
3731 CAMLparam4 (ptr_v, x_v, y_v, contents_v);
3732 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3734 if (pdf) {
3735 pdf_annot *annot;
3736 struct page *page;
3737 fz_point p;
3738 char *s = String_val (ptr_v);
3740 page = parse_pointer (__func__, s);
3741 annot = pdf_create_annot (state.ctx,
3742 pdf_page_from_fz_page (state.ctx,
3743 page->fzpage),
3744 PDF_ANNOT_TEXT);
3745 p.x = Int_val (x_v);
3746 p.y = Int_val (y_v);
3747 pdf_set_annot_contents (state.ctx, annot, String_val (contents_v));
3748 pdf_set_text_annot_position (state.ctx, annot, p);
3749 state.dirty = 1;
3751 CAMLreturn0;
3754 CAMLprim void ml_delannot (value ptr_v, value n_v)
3756 CAMLparam2 (ptr_v, n_v);
3757 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3759 if (pdf) {
3760 struct page *page;
3761 char *s = String_val (ptr_v);
3762 struct slink *slink;
3764 page = parse_pointer (__func__, s);
3765 slink = &page->slinks[Int_val (n_v)];
3766 pdf_delete_annot (state.ctx,
3767 pdf_page_from_fz_page (state.ctx, page->fzpage),
3768 (pdf_annot *) slink->u.annot);
3769 state.dirty = 1;
3771 CAMLreturn0;
3774 CAMLprim void ml_modannot (value ptr_v, value n_v, value str_v)
3776 CAMLparam3 (ptr_v, n_v, str_v);
3777 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3779 if (pdf) {
3780 struct page *page;
3781 char *s = String_val (ptr_v);
3782 struct slink *slink;
3784 page = parse_pointer (__func__, s);
3785 slink = &page->slinks[Int_val (n_v)];
3786 pdf_set_annot_contents (state.ctx, (pdf_annot *) slink->u.annot,
3787 String_val (str_v));
3788 state.dirty = 1;
3790 CAMLreturn0;
3793 CAMLprim value ml_hasunsavedchanges (void)
3795 return Val_bool (state.dirty);
3798 CAMLprim void ml_savedoc (value path_v)
3800 CAMLparam1 (path_v);
3801 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3803 if (pdf) {
3804 pdf_save_document (state.ctx, pdf, String_val (path_v), NULL);
3806 CAMLreturn0;
3809 static void makestippletex (void)
3811 const char pixels[] = "\xff\xff\0\0";
3812 glGenTextures (1, &state.stid);
3813 glBindTexture (GL_TEXTURE_1D, state.stid);
3814 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3815 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3816 glTexImage1D (
3817 GL_TEXTURE_1D,
3819 GL_ALPHA,
3822 GL_ALPHA,
3823 GL_UNSIGNED_BYTE,
3824 pixels
3828 CAMLprim value ml_fz_version (void)
3830 return caml_copy_string (FZ_VERSION);
3833 CAMLprim value ml_llpp_version (void)
3835 extern char llpp_version[];
3836 return caml_copy_string (llpp_version);
3839 CAMLprim void ml_init (value csock_v, value params_v)
3841 CAMLparam2 (csock_v, params_v);
3842 CAMLlocal2 (trim_v, fuzz_v);
3843 int ret;
3844 int texcount;
3845 char *fontpath;
3846 int colorspace;
3847 int mustoresize;
3849 state.csock = Int_val (csock_v);
3850 state.rotate = Int_val (Field (params_v, 0));
3851 state.fitmodel = Int_val (Field (params_v, 1));
3852 trim_v = Field (params_v, 2);
3853 texcount = Int_val (Field (params_v, 3));
3854 state.sliceheight = Int_val (Field (params_v, 4));
3855 mustoresize = Int_val (Field (params_v, 5));
3856 colorspace = Int_val (Field (params_v, 6));
3857 fontpath = String_val (Field (params_v, 7));
3859 #ifdef CIDER
3860 state.utf8cs = 1;
3861 #else
3862 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3863 if (setlocale (LC_CTYPE, "")) {
3864 const char *cset = nl_langinfo (CODESET);
3865 state.utf8cs = !strcmp (cset, "UTF-8");
3867 else {
3868 printd ("emsg setlocale: %d:%s", errno, strerror (errno));
3870 #endif
3872 if (caml_string_length (Field (params_v, 8)) > 0) {
3873 state.trimcachepath = ystrdup (String_val (Field (params_v, 8)));
3875 if (!state.trimcachepath) {
3876 printd ("emsg failed to strdup trimcachepath: %d:%s",
3877 errno, strerror (errno));
3881 state.ctx = fz_new_context (NULL, NULL, mustoresize);
3882 fz_register_document_handlers (state.ctx);
3884 state.trimmargins = Bool_val (Field (trim_v, 0));
3885 fuzz_v = Field (trim_v, 1);
3886 state.trimfuzz.x0 = Int_val (Field (fuzz_v, 0));
3887 state.trimfuzz.y0 = Int_val (Field (fuzz_v, 1));
3888 state.trimfuzz.x1 = Int_val (Field (fuzz_v, 2));
3889 state.trimfuzz.y1 = Int_val (Field (fuzz_v, 3));
3891 set_tex_params (colorspace);
3893 if (*fontpath) {
3894 state.face = load_font (fontpath);
3896 else {
3897 int len;
3898 const unsigned char *data;
3900 data = pdf_lookup_substitute_font (state.ctx, 0, 0, 0, 0, &len);
3901 state.face = load_builtin_font (data, len);
3903 if (!state.face) _exit (1);
3905 realloctexts (texcount);
3906 setuppbo ();
3907 makestippletex ();
3909 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
3910 if (ret) {
3911 errx (1, "pthread_create: %s", strerror (ret));
3914 CAMLreturn0;
3917 #if FIXME || !FIXME
3918 static void UNUSED_ATTR NO_OPTIMIZE_ATTR refmacs (void) {}
3919 #endif