Cosmetics
[llpp.git] / link.c
blob1877edcc28054a412d05ad3750f13a6e72ea397a
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_RGBA8;
1344 state.texform = GL_BGRA;
1345 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1346 state.texty = GL_UNSIGNED_INT_8_8_8_8_REV;
1347 #else
1348 state.texty = GL_UNSIGNED_INT_8_8_8_8;
1349 #endif
1350 state.colorspace = fz_device_bgr (state.ctx);
1351 break;
1352 case 2:
1353 state.texiform = GL_LUMINANCE_ALPHA;
1354 state.texform = GL_LUMINANCE_ALPHA;
1355 state.texty = GL_UNSIGNED_BYTE;
1356 state.colorspace = fz_device_gray (state.ctx);
1357 break;
1358 default:
1359 errx (1, "invalid colorspce %d", colorspace);
1363 static void realloctexts (int texcount)
1365 size_t size;
1367 if (texcount == state.texcount) return;
1369 if (texcount < state.texcount) {
1370 glDeleteTextures (state.texcount - texcount,
1371 state.texids + texcount);
1374 size = texcount * (sizeof (*state.texids) + sizeof (*state.texowners));
1375 state.texids = realloc (state.texids, size);
1376 if (!state.texids) {
1377 err (1, "realloc texs %zu", size);
1380 state.texowners = (void *) (state.texids + texcount);
1381 if (texcount > state.texcount) {
1382 glGenTextures (texcount - state.texcount,
1383 state.texids + state.texcount);
1384 for (int i = state.texcount; i < texcount; ++i) {
1385 state.texowners[i].w = -1;
1386 state.texowners[i].slice = NULL;
1389 state.texcount = texcount;
1390 state.texindex = 0;
1393 static char *mbtoutf8 (char *s)
1395 char *p, *r;
1396 wchar_t *tmp;
1397 size_t i, ret, len;
1399 if (state.utf8cs) {
1400 return s;
1403 len = mbstowcs (NULL, s, strlen (s));
1404 if (len == 0) {
1405 return s;
1407 else {
1408 if (len == (size_t) -1) {
1409 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno, strerror (errno));
1410 return s;
1414 tmp = calloc (len, sizeof (wchar_t));
1415 if (!tmp) {
1416 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1417 len, sizeof (wchar_t), errno, strerror (errno));
1418 return s;
1421 ret = mbstowcs (tmp, s, len);
1422 if (ret == (size_t) -1) {
1423 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1424 len, errno, strerror (errno));
1425 free (tmp);
1426 return s;
1429 len = 0;
1430 for (i = 0; i < ret; ++i) {
1431 len += fz_runelen (tmp[i]);
1434 p = r = malloc (len + 1);
1435 if (!r) {
1436 printd ("emsg mbtoutf8: malloc(%zu)", len);
1437 free (tmp);
1438 return s;
1441 for (i = 0; i < ret; ++i) {
1442 p += fz_runetochar (p, tmp[i]);
1444 *p = 0;
1445 free (tmp);
1446 return r;
1449 CAMLprim value ml_mbtoutf8 (value s_v)
1451 CAMLparam1 (s_v);
1452 CAMLlocal1 (ret_v);
1453 char *s, *r;
1455 s = String_val (s_v);
1456 r = mbtoutf8 (s);
1457 if (r == s) {
1458 ret_v = s_v;
1460 else {
1461 ret_v = caml_copy_string (r);
1462 free (r);
1464 CAMLreturn (ret_v);
1467 static void * mainloop (void UNUSED_ATTR *unused)
1469 char *p = NULL;
1470 int len, ret, oldlen = 0;
1472 fz_var (p);
1473 fz_var (oldlen);
1474 for (;;) {
1475 len = readlen (state.csock);
1476 if (len == 0) {
1477 errx (1, "readlen returned 0");
1480 if (oldlen < len + 1) {
1481 p = realloc (p, len + 1);
1482 if (!p) {
1483 err (1, "realloc %d failed", len + 1);
1485 oldlen = len + 1;
1487 readdata (state.csock, p, len);
1488 p[len] = 0;
1490 if (!strncmp ("open", p, 4)) {
1491 int off, usedoccss, ok = 0, layouth;
1492 char *password;
1493 char *filename;
1494 char *utf8filename;
1495 size_t filenamelen;
1497 fz_var (ok);
1498 ret = sscanf (p + 5, " %d %d %n", &usedoccss, &layouth, &off);
1499 if (ret != 2) {
1500 errx (1, "malformed open `%.*s' ret=%d", len, p, ret);
1503 filename = p + 5 + off;
1504 filenamelen = strlen (filename);
1505 password = filename + filenamelen + 1;
1507 if (password[strlen (password) + 1]) {
1508 fz_set_user_css (state.ctx, password + strlen (password) + 1);
1511 lock ("open");
1512 fz_set_use_document_css (state.ctx, usedoccss);
1513 fz_try (state.ctx) {
1514 ok = openxref (filename, password, layouth);
1516 fz_catch (state.ctx) {
1517 utf8filename = mbtoutf8 (filename);
1518 printd ("msg Could not open %s", utf8filename);
1519 if (utf8filename != filename) {
1520 free (utf8filename);
1523 if (ok) {
1524 docinfo ();
1525 initpdims ();
1527 unlock ("open");
1529 if (ok) {
1530 utf8filename = mbtoutf8 (filename);
1531 printd ("msg Opened %s (press h/F1 to get help)", utf8filename);
1532 if (utf8filename != filename) {
1533 free (utf8filename);
1535 state.needoutline = 1;
1538 else if (!strncmp ("cs", p, 2)) {
1539 int i, colorspace;
1541 ret = sscanf (p + 2, " %d", &colorspace);
1542 if (ret != 1) {
1543 errx (1, "malformed cs `%.*s' ret=%d", len, p, ret);
1545 lock ("cs");
1546 set_tex_params (colorspace);
1547 for (i = 0; i < state.texcount; ++i) {
1548 state.texowners[i].w = -1;
1549 state.texowners[i].slice = NULL;
1551 unlock ("cs");
1553 else if (!strncmp ("freepage", p, 8)) {
1554 void *ptr;
1556 ret = sscanf (p + 8, " %" SCNxPTR, (uintptr_t *) &ptr);
1557 if (ret != 1) {
1558 errx (1, "malformed freepage `%.*s' ret=%d", len, p, ret);
1560 lock ("freepage");
1561 freepage (ptr);
1562 unlock ("freepage");
1564 else if (!strncmp ("freetile", p, 8)) {
1565 void *ptr;
1567 ret = sscanf (p + 8, " %" SCNxPTR, (uintptr_t *) &ptr);
1568 if (ret != 1) {
1569 errx (1, "malformed freetile `%.*s' ret=%d", len, p, ret);
1571 lock ("freetile");
1572 freetile (ptr);
1573 unlock ("freetile");
1575 else if (!strncmp ("search", p, 6)) {
1576 int icase, pageno, y, len2, forward;
1577 char *pattern;
1578 regex_t re;
1580 ret = sscanf (p + 6, " %d %d %d %d,%n",
1581 &icase, &pageno, &y, &forward, &len2);
1582 if (ret != 4) {
1583 errx (1, "malformed search `%s' ret=%d", p, ret);
1586 pattern = p + 6 + len2;
1587 ret = regcomp (&re, pattern,
1588 REG_EXTENDED | (icase ? REG_ICASE : 0));
1589 if (ret) {
1590 char errbuf[80];
1591 size_t size;
1593 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1594 printd ("msg regcomp failed `%.*s'", (int) size, errbuf);
1596 else {
1597 search (&re, pageno, y, forward);
1598 regfree (&re);
1601 else if (!strncmp ("geometry", p, 8)) {
1602 int w, h, fitmodel;
1604 printd ("clear");
1605 ret = sscanf (p + 8, " %d %d %d", &w, &h, &fitmodel);
1606 if (ret != 3) {
1607 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1610 lock ("geometry");
1611 state.h = h;
1612 if (w != state.w) {
1613 state.w = w;
1614 for (int i = 0; i < state.texcount; ++i) {
1615 state.texowners[i].slice = NULL;
1618 state.fitmodel = fitmodel;
1619 layout ();
1620 process_outline ();
1622 state.gen++;
1623 unlock ("geometry");
1624 printd ("continue %d", state.pagecount);
1626 else if (!strncmp ("reqlayout", p, 9)) {
1627 char *nameddest;
1628 int rotate, off, h;
1629 int fitmodel;
1630 pdf_document *pdf;
1632 printd ("clear");
1633 ret = sscanf (p + 9, " %d %d %d %n",
1634 &rotate, &fitmodel, &h, &off);
1635 if (ret != 3) {
1636 errx (1, "bad reqlayout line `%.*s' ret=%d", len, p, ret);
1638 lock ("reqlayout");
1639 pdf = pdf_specifics (state.ctx, state.doc);
1640 if (state.rotate != rotate || state.fitmodel != fitmodel) {
1641 state.gen += 1;
1643 state.rotate = rotate;
1644 state.fitmodel = fitmodel;
1645 state.h = h;
1646 layout ();
1647 process_outline ();
1649 nameddest = p + 9 + off;
1650 if (pdf && nameddest && *nameddest) {
1651 fz_point xy;
1652 struct pagedim *pdim;
1653 int pageno = pdf_lookup_anchor (state.ctx, pdf, nameddest,
1654 &xy.x, &xy.y);
1655 pdim = pdimofpageno (pageno);
1656 fz_transform_point (&xy, &pdim->ctm);
1657 printd ("a %d %d %d", pageno, (int) xy.x, (int) xy.y);
1660 state.gen++;
1661 unlock ("reqlayout");
1662 printd ("continue %d", state.pagecount);
1664 else if (!strncmp ("page", p, 4)) {
1665 double a, b;
1666 struct page *page;
1667 int pageno, pindex;
1669 ret = sscanf (p + 4, " %d %d", &pageno, &pindex);
1670 if (ret != 2) {
1671 errx (1, "bad page line `%.*s' ret=%d", len, p, ret);
1674 lock ("page");
1675 a = now ();
1676 page = loadpage (pageno, pindex);
1677 b = now ();
1678 unlock ("page");
1680 printd ("page %" PRIxPTR " %f", (uintptr_t) page, b - a);
1682 else if (!strncmp ("tile", p, 4)) {
1683 int x, y, w, h;
1684 struct page *page;
1685 struct tile *tile;
1686 double a, b;
1687 void *data;
1689 ret = sscanf (p + 4, " %" SCNxPTR " %d %d %d %d %" SCNxPTR,
1690 (uintptr_t *) &page, &x, &y, &w, &h,
1691 (uintptr_t *) &data);
1692 if (ret != 6) {
1693 errx (1, "bad tile line `%.*s' ret=%d", len, p, ret);
1696 lock ("tile");
1697 a = now ();
1698 tile = rendertile (page, x, y, w, h, data);
1699 b = now ();
1700 unlock ("tile");
1702 printd ("tile %d %d %" PRIxPTR " %u %f",
1703 x, y, (uintptr_t) (tile),
1704 tile->w * tile->h * tile->pixmap->n,
1705 b - a);
1707 else if (!strncmp ("trimset", p, 7)) {
1708 fz_irect fuzz;
1709 int trimmargins;
1711 ret = sscanf (p + 7, " %d %d %d %d %d",
1712 &trimmargins, &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1713 if (ret != 5) {
1714 errx (1, "malformed trimset `%.*s' ret=%d", len, p, ret);
1716 lock ("trimset");
1717 state.trimmargins = trimmargins;
1718 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1719 state.trimanew = 1;
1720 state.trimfuzz = fuzz;
1722 unlock ("trimset");
1724 else if (!strncmp ("settrim", p, 7)) {
1725 fz_irect fuzz;
1726 int trimmargins;
1728 ret = sscanf (p + 7, " %d %d %d %d %d",
1729 &trimmargins, &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1730 if (ret != 5) {
1731 errx (1, "malformed settrim `%.*s' ret=%d", len, p, ret);
1733 printd ("clear");
1734 lock ("settrim");
1735 state.trimmargins = trimmargins;
1736 state.needoutline = 1;
1737 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1738 state.trimanew = 1;
1739 state.trimfuzz = fuzz;
1741 state.pagedimcount = 0;
1742 free (state.pagedims);
1743 state.pagedims = NULL;
1744 initpdims ();
1745 layout ();
1746 process_outline ();
1747 unlock ("settrim");
1748 printd ("continue %d", state.pagecount);
1750 else if (!strncmp ("sliceh", p, 6)) {
1751 int h;
1753 ret = sscanf (p + 6, " %d", &h);
1754 if (ret != 1) {
1755 errx (1, "malformed sliceh `%.*s' ret=%d", len, p, ret);
1757 if (h != state.sliceheight) {
1758 state.sliceheight = h;
1759 for (int i = 0; i < state.texcount; ++i) {
1760 state.texowners[i].w = -1;
1761 state.texowners[i].h = -1;
1762 state.texowners[i].slice = NULL;
1766 else if (!strncmp ("interrupt", p, 9)) {
1767 printd ("vmsg interrupted");
1769 else {
1770 errx (1, "unknown command %.*s", len, p);
1773 return 0;
1776 CAMLprim value ml_isexternallink (value uri_v)
1778 CAMLparam1 (uri_v);
1779 int ext = fz_is_external_link (state.ctx, String_val (uri_v));
1780 CAMLreturn (Val_bool (ext));
1783 CAMLprim value ml_uritolocation (value uri_v)
1785 CAMLparam1 (uri_v);
1786 CAMLlocal1 (ret_v);
1787 int pageno;
1788 fz_point xy;
1789 struct pagedim *pdim;
1791 pageno = fz_resolve_link (state.ctx, state.doc, String_val (uri_v),
1792 &xy.x, &xy.y);
1793 pdim = pdimofpageno (pageno);
1794 fz_transform_point (&xy, &pdim->ctm);
1795 ret_v = caml_alloc_tuple (3);
1796 Field (ret_v, 0) = Val_int (pageno);
1797 Field (ret_v, 1) = caml_copy_double ((double) xy.x);
1798 Field (ret_v, 2) = caml_copy_double ((double) xy.y);
1799 CAMLreturn (ret_v);
1802 CAMLprim value ml_realloctexts (value texcount_v)
1804 CAMLparam1 (texcount_v);
1805 int ok;
1807 if (trylock (__func__)) {
1808 ok = 0;
1809 goto done;
1811 realloctexts (Int_val (texcount_v));
1812 ok = 1;
1813 unlock (__func__);
1815 done:
1816 CAMLreturn (Val_bool (ok));
1819 static void recti (int x0, int y0, int x1, int y1)
1821 GLfloat *v = state.vertices;
1823 glVertexPointer (2, GL_FLOAT, 0, v);
1824 v[0] = x0; v[1] = y0;
1825 v[2] = x1; v[3] = y0;
1826 v[4] = x0; v[5] = y1;
1827 v[6] = x1; v[7] = y1;
1828 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1831 static void showsel (struct page *page, int ox, int oy)
1833 fz_irect bbox;
1834 fz_rect rect;
1835 fz_stext_block *block;
1836 int seen = 0;
1837 unsigned char selcolor[] = {15,15,15,140};
1839 if (!page->fmark || !page->lmark) return;
1841 glEnable (GL_BLEND);
1842 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1843 glColor4ubv (selcolor);
1845 ox += state.pagedims[page->pdimno].bounds.x0;
1846 oy += state.pagedims[page->pdimno].bounds.y0;
1848 for (block = page->text->first_block; block; block = block->next) {
1849 fz_stext_line *line;
1851 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1852 for (line = block->u.t.first_line; line; line = line->next) {
1853 fz_stext_char *ch;
1855 rect = fz_empty_rect;
1856 for (ch = line->first_char; ch; ch = ch->next) {
1857 if (ch == page->fmark) seen = 1;
1858 if (seen) fz_union_rect (&rect, &ch->bbox);
1859 if (ch == page->lmark) {
1860 fz_round_rect (&bbox, &rect);
1861 recti (bbox.x0 + ox, bbox.y0 + oy,
1862 bbox.x1 + ox, bbox.y1 + oy);
1863 goto done;
1866 fz_round_rect (&bbox, &rect);
1867 recti (bbox.x0 + ox, bbox.y0 + oy,
1868 bbox.x1 + ox, bbox.y1 + oy);
1871 done:
1872 glDisable (GL_BLEND);
1875 #pragma GCC diagnostic push
1876 #pragma GCC diagnostic ignored "-Wconversion"
1877 #include "glfont.c"
1878 #pragma GCC diagnostic pop
1880 static void stipplerect (fz_matrix *m,
1881 fz_point *p1,
1882 fz_point *p2,
1883 fz_point *p3,
1884 fz_point *p4,
1885 GLfloat *texcoords,
1886 GLfloat *vertices)
1888 fz_transform_point (p1, m);
1889 fz_transform_point (p2, m);
1890 fz_transform_point (p3, m);
1891 fz_transform_point (p4, m);
1893 float w, h, s, t;
1895 w = p2->x - p1->x;
1896 h = p2->y - p1->y;
1897 t = hypotf (w, h) * .25f;
1899 w = p3->x - p2->x;
1900 h = p3->y - p2->y;
1901 s = hypotf (w, h) * .25f;
1903 texcoords[0] = 0; vertices[0] = p1->x; vertices[1] = p1->y;
1904 texcoords[1] = t; vertices[2] = p2->x; vertices[3] = p2->y;
1906 texcoords[2] = 0; vertices[4] = p2->x; vertices[5] = p2->y;
1907 texcoords[3] = s; vertices[6] = p3->x; vertices[7] = p3->y;
1909 texcoords[4] = 0; vertices[8] = p3->x; vertices[9] = p3->y;
1910 texcoords[5] = t; vertices[10] = p4->x; vertices[11] = p4->y;
1912 texcoords[6] = 0; vertices[12] = p4->x; vertices[13] = p4->y;
1913 texcoords[7] = s; vertices[14] = p1->x; vertices[15] = p1->y;
1915 glDrawArrays (GL_LINES, 0, 8);
1918 static void solidrect (fz_matrix *m,
1919 fz_point *p1,
1920 fz_point *p2,
1921 fz_point *p3,
1922 fz_point *p4,
1923 GLfloat *vertices)
1925 fz_transform_point (p1, m);
1926 fz_transform_point (p2, m);
1927 fz_transform_point (p3, m);
1928 fz_transform_point (p4, m);
1929 vertices[0] = p1->x; vertices[1] = p1->y;
1930 vertices[2] = p2->x; vertices[3] = p2->y;
1932 vertices[4] = p3->x; vertices[5] = p3->y;
1933 vertices[6] = p4->x; vertices[7] = p4->y;
1934 glDrawArrays (GL_TRIANGLE_FAN, 0, 4);
1937 static void ensurelinks (struct page *page)
1939 if (!page->links)
1940 page->links = fz_load_links (state.ctx, page->fzpage);
1943 static void highlightlinks (struct page *page, int xoff, int yoff)
1945 fz_matrix ctm, tm, pm;
1946 fz_link *link;
1947 GLfloat *texcoords = state.texcoords;
1948 GLfloat *vertices = state.vertices;
1950 ensurelinks (page);
1952 glEnable (GL_TEXTURE_1D);
1953 glEnable (GL_BLEND);
1954 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1955 glBindTexture (GL_TEXTURE_1D, state.stid);
1957 xoff -= state.pagedims[page->pdimno].bounds.x0;
1958 yoff -= state.pagedims[page->pdimno].bounds.y0;
1959 fz_translate (&tm, xoff, yoff);
1960 pm = pagectm (page);
1961 fz_concat (&ctm, &pm, &tm);
1963 glTexCoordPointer (1, GL_FLOAT, 0, texcoords);
1964 glVertexPointer (2, GL_FLOAT, 0, vertices);
1966 for (link = page->links; link; link = link->next) {
1967 fz_point p1, p2, p3, p4;
1969 p1.x = link->rect.x0;
1970 p1.y = link->rect.y0;
1972 p2.x = link->rect.x1;
1973 p2.y = link->rect.y0;
1975 p3.x = link->rect.x1;
1976 p3.y = link->rect.y1;
1978 p4.x = link->rect.x0;
1979 p4.y = link->rect.y1;
1981 /* TODO: different colours for different schemes */
1982 if (fz_is_external_link (state.ctx, link->uri)) glColor3ub (0, 0, 255);
1983 else glColor3ub (255, 0, 0);
1985 stipplerect (&ctm, &p1, &p2, &p3, &p4, texcoords, vertices);
1988 for (int i = 0; i < page->annotcount; ++i) {
1989 fz_point p1, p2, p3, p4;
1990 struct annot *annot = &page->annots[i];
1992 p1.x = annot->bbox.x0;
1993 p1.y = annot->bbox.y0;
1995 p2.x = annot->bbox.x1;
1996 p2.y = annot->bbox.y0;
1998 p3.x = annot->bbox.x1;
1999 p3.y = annot->bbox.y1;
2001 p4.x = annot->bbox.x0;
2002 p4.y = annot->bbox.y1;
2004 glColor3ub (0, 0, 128);
2005 stipplerect (&ctm, &p1, &p2, &p3, &p4, texcoords, vertices);
2008 glDisable (GL_BLEND);
2009 glDisable (GL_TEXTURE_1D);
2012 static int compareslinks (const void *l, const void *r)
2014 struct slink const *ls = l;
2015 struct slink const *rs = r;
2016 if (ls->bbox.y0 == rs->bbox.y0) {
2017 return rs->bbox.x0 - rs->bbox.x0;
2019 return ls->bbox.y0 - rs->bbox.y0;
2022 static void droptext (struct page *page)
2024 if (page->text) {
2025 fz_drop_stext_page (state.ctx, page->text);
2026 page->fmark = NULL;
2027 page->lmark = NULL;
2028 page->text = NULL;
2032 static void dropannots (struct page *page)
2034 if (page->annots) {
2035 free (page->annots);
2036 page->annots = NULL;
2037 page->annotcount = 0;
2041 static void ensureannots (struct page *page)
2043 int i, count = 0;
2044 size_t annotsize = sizeof (*page->annots);
2045 fz_annot *annot;
2047 if (state.gen != page->agen) {
2048 dropannots (page);
2049 page->agen = state.gen;
2051 if (page->annots) return;
2053 for (annot = fz_first_annot (state.ctx, page->fzpage);
2054 annot;
2055 annot = fz_next_annot (state.ctx, annot)) {
2056 count++;
2059 if (count > 0) {
2060 page->annotcount = count;
2061 page->annots = calloc (count, annotsize);
2062 if (!page->annots) {
2063 err (1, "calloc annots %d", count);
2066 for (annot = fz_first_annot (state.ctx, page->fzpage), i = 0;
2067 annot;
2068 annot = fz_next_annot (state.ctx, annot), i++) {
2069 fz_rect rect;
2071 fz_bound_annot (state.ctx, annot, &rect);
2072 page->annots[i].annot = annot;
2073 fz_round_rect (&page->annots[i].bbox, &rect);
2078 static void dropslinks (struct page *page)
2080 if (page->slinks) {
2081 free (page->slinks);
2082 page->slinks = NULL;
2083 page->slinkcount = 0;
2085 if (page->links) {
2086 fz_drop_link (state.ctx, page->links);
2087 page->links = NULL;
2091 static void ensureslinks (struct page *page)
2093 fz_matrix ctm;
2094 int i, count;
2095 size_t slinksize = sizeof (*page->slinks);
2096 fz_link *link;
2098 ensureannots (page);
2099 if (state.gen != page->sgen) {
2100 dropslinks (page);
2101 page->sgen = state.gen;
2103 if (page->slinks) return;
2105 ensurelinks (page);
2106 ctm = pagectm (page);
2108 count = page->annotcount;
2109 for (link = page->links; link; link = link->next) {
2110 count++;
2112 if (count > 0) {
2113 int j;
2115 page->slinkcount = count;
2116 page->slinks = calloc (count, slinksize);
2117 if (!page->slinks) {
2118 err (1, "calloc slinks %d", count);
2121 for (i = 0, link = page->links; link; ++i, link = link->next) {
2122 fz_rect rect;
2124 rect = link->rect;
2125 fz_transform_rect (&rect, &ctm);
2126 page->slinks[i].tag = SLINK;
2127 page->slinks[i].u.link = link;
2128 fz_round_rect (&page->slinks[i].bbox, &rect);
2130 for (j = 0; j < page->annotcount; ++j, ++i) {
2131 fz_rect rect;
2132 fz_bound_annot (state.ctx, page->annots[j].annot, &rect);
2133 fz_transform_rect (&rect, &ctm);
2134 fz_round_rect (&page->slinks[i].bbox, &rect);
2136 page->slinks[i].tag = SANNOT;
2137 page->slinks[i].u.annot = page->annots[j].annot;
2139 qsort (page->slinks, count, slinksize, compareslinks);
2143 static void highlightslinks (struct page *page, int xoff, int yoff,
2144 int noff, char *targ, mlsize_t tlen, int hfsize)
2146 char buf[40];
2147 struct slink *slink;
2148 float x0, y0, x1, y1, w;
2150 ensureslinks (page);
2151 glColor3ub (0xc3, 0xb0, 0x91);
2152 for (int i = 0; i < page->slinkcount; ++i) {
2153 fmt_linkn (buf, i + noff);
2154 if (!tlen || !strncmp (targ, buf, tlen)) {
2155 slink = &page->slinks[i];
2157 x0 = slink->bbox.x0 + xoff - 5;
2158 y1 = slink->bbox.y0 + yoff - 5;
2159 y0 = y1 + 10 + hfsize;
2160 w = measure_string (state.face, hfsize, buf);
2161 x1 = x0 + w + 10;
2162 recti ((int) x0, (int) y0, (int) x1, (int) y1);
2166 glEnable (GL_BLEND);
2167 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2168 glEnable (GL_TEXTURE_2D);
2169 glColor3ub (0, 0, 0);
2170 for (int i = 0; i < page->slinkcount; ++i) {
2171 fmt_linkn (buf, i + noff);
2172 if (!tlen || !strncmp (targ, buf, tlen)) {
2173 slink = &page->slinks[i];
2175 x0 = slink->bbox.x0 + xoff;
2176 y0 = slink->bbox.y0 + yoff + hfsize;
2177 draw_string (state.face, hfsize, x0, y0, buf);
2180 glDisable (GL_TEXTURE_2D);
2181 glDisable (GL_BLEND);
2184 static void uploadslice (struct tile *tile, struct slice *slice)
2186 int offset;
2187 struct slice *slice1;
2188 unsigned char *texdata;
2190 offset = 0;
2191 for (slice1 = tile->slices; slice != slice1; slice1++) {
2192 offset += slice1->h * tile->w * tile->pixmap->n;
2194 if (slice->texindex != -1 && slice->texindex < state.texcount
2195 && state.texowners[slice->texindex].slice == slice) {
2196 glBindTexture (TEXT_TYPE, state.texids[slice->texindex]);
2198 else {
2199 int subimage = 0;
2200 int texindex = state.texindex++ % state.texcount;
2202 if (state.texowners[texindex].w == tile->w) {
2203 if (state.texowners[texindex].h >= slice->h) {
2204 subimage = 1;
2206 else {
2207 state.texowners[texindex].h = slice->h;
2210 else {
2211 state.texowners[texindex].h = slice->h;
2214 state.texowners[texindex].w = tile->w;
2215 state.texowners[texindex].slice = slice;
2216 slice->texindex = texindex;
2218 glBindTexture (TEXT_TYPE, state.texids[texindex]);
2219 #if TEXT_TYPE == GL_TEXTURE_2D
2220 glTexParameteri (TEXT_TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2221 glTexParameteri (TEXT_TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2222 glTexParameteri (TEXT_TYPE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2223 glTexParameteri (TEXT_TYPE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2224 #endif
2225 if (tile->pbo) {
2226 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, tile->pbo->id);
2227 texdata = 0;
2229 else {
2230 texdata = tile->pixmap->samples;
2232 if (subimage) {
2233 glTexSubImage2D (TEXT_TYPE,
2237 tile->w,
2238 slice->h,
2239 state.texform,
2240 state.texty,
2241 texdata+offset
2244 else {
2245 glTexImage2D (TEXT_TYPE,
2247 state.texiform,
2248 tile->w,
2249 slice->h,
2251 state.texform,
2252 state.texty,
2253 texdata+offset
2256 if (tile->pbo) {
2257 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
2262 CAMLprim void ml_begintiles (void)
2264 glEnable (TEXT_TYPE);
2265 glTexCoordPointer (2, GL_FLOAT, 0, state.texcoords);
2266 glVertexPointer (2, GL_FLOAT, 0, state.vertices);
2269 CAMLprim void ml_endtiles (void)
2271 glDisable (TEXT_TYPE);
2274 CAMLprim void ml_drawtile (value args_v, value ptr_v)
2276 CAMLparam2 (args_v, ptr_v);
2277 int dispx = Int_val (Field (args_v, 0));
2278 int dispy = Int_val (Field (args_v, 1));
2279 int dispw = Int_val (Field (args_v, 2));
2280 int disph = Int_val (Field (args_v, 3));
2281 int tilex = Int_val (Field (args_v, 4));
2282 int tiley = Int_val (Field (args_v, 5));
2283 char *s = String_val (ptr_v);
2284 struct tile *tile = parse_pointer (__func__, s);
2285 int slicey, firstslice;
2286 struct slice *slice;
2287 GLfloat *texcoords = state.texcoords;
2288 GLfloat *vertices = state.vertices;
2290 firstslice = tiley / tile->sliceheight;
2291 slice = &tile->slices[firstslice];
2292 slicey = tiley % tile->sliceheight;
2294 while (disph > 0) {
2295 int dh;
2297 dh = slice->h - slicey;
2298 dh = fz_mini (disph, dh);
2299 uploadslice (tile, slice);
2301 texcoords[0] = tilex; texcoords[1] = slicey;
2302 texcoords[2] = tilex+dispw; texcoords[3] = slicey;
2303 texcoords[4] = tilex; texcoords[5] = slicey+dh;
2304 texcoords[6] = tilex+dispw; texcoords[7] = slicey+dh;
2306 vertices[0] = dispx; vertices[1] = dispy;
2307 vertices[2] = dispx+dispw; vertices[3] = dispy;
2308 vertices[4] = dispx; vertices[5] = dispy+dh;
2309 vertices[6] = dispx+dispw; vertices[7] = dispy+dh;
2311 #if TEXT_TYPE == GL_TEXTURE_2D
2312 for (int i = 0; i < 8; ++i) {
2313 texcoords[i] /= ((i & 1) == 0 ? tile->w : slice->h);
2315 #endif
2317 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
2318 dispy += dh;
2319 disph -= dh;
2320 slice++;
2321 ARSERT (!(slice - tile->slices >= tile->slicecount && disph > 0));
2322 slicey = 0;
2324 CAMLreturn0;
2327 static void drawprect (struct page *page, int xoff, int yoff, value rects_v)
2329 fz_matrix ctm, tm, pm;
2330 fz_point p1, p2, p3, p4;
2331 GLfloat *vertices = state.vertices;
2332 double *v = (double *) rects_v;
2334 xoff -= state.pagedims[page->pdimno].bounds.x0;
2335 yoff -= state.pagedims[page->pdimno].bounds.y0;
2336 fz_translate (&tm, xoff, yoff);
2337 pm = pagectm (page);
2338 fz_concat (&ctm, &pm, &tm);
2340 glEnable (GL_BLEND);
2341 glVertexPointer (2, GL_FLOAT, 0, vertices);
2343 glColor4dv (v);
2344 p1.x = (float) v[4];
2345 p1.y = (float) v[5];
2347 p2.x = (float) v[6];
2348 p2.y = (float) v[5];
2350 p3.x = (float) v[6];
2351 p3.y = (float) v[7];
2353 p4.x = (float) v[4];
2354 p4.y = (float) v[7];
2355 solidrect (&ctm, &p1, &p2, &p3, &p4, vertices);
2356 glDisable (GL_BLEND);
2359 CAMLprim value ml_postprocess (value ptr_v, value hlinks_v,
2360 value xoff_v, value yoff_v,
2361 value li_v)
2363 CAMLparam5 (ptr_v, hlinks_v, xoff_v, yoff_v, li_v);
2364 int xoff = Int_val (xoff_v);
2365 int yoff = Int_val (yoff_v);
2366 int noff = Int_val (Field (li_v, 0));
2367 char *targ = String_val (Field (li_v, 1));
2368 mlsize_t tlen = caml_string_length (Field (li_v, 1));
2369 int hfsize = Int_val (Field (li_v, 2));
2370 char *s = String_val (ptr_v);
2371 int hlmask = Int_val (hlinks_v);
2372 struct page *page = parse_pointer (__func__, s);
2374 if (!page->fzpage) {
2375 /* deal with loadpage failed pages */
2376 goto done;
2379 if (trylock (__func__)) {
2380 noff = -1;
2381 goto done;
2384 ensureannots (page);
2385 if (hlmask & 1) highlightlinks (page, xoff, yoff);
2386 if (hlmask & 2) {
2387 highlightslinks (page, xoff, yoff, noff, targ, tlen, hfsize);
2388 noff = page->slinkcount;
2390 if (page->tgen == state.gen) {
2391 showsel (page, xoff, yoff);
2393 unlock (__func__);
2395 done:
2396 CAMLreturn (Val_int (noff));
2399 CAMLprim void ml_drawprect (value ptr_v, value xoff_v, value yoff_v,
2400 value rects_v)
2402 CAMLparam4 (ptr_v, xoff_v, yoff_v, rects_v);
2403 int xoff = Int_val (xoff_v);
2404 int yoff = Int_val (yoff_v);
2405 char *s = String_val (ptr_v);
2406 struct page *page = parse_pointer (__func__, s);
2408 drawprect (page, xoff, yoff, rects_v);
2409 CAMLreturn0;
2412 static struct annot *getannot (struct page *page, int x, int y)
2414 fz_point p;
2415 fz_matrix ctm;
2416 const fz_matrix *tctm;
2417 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
2419 if (!page->annots) return NULL;
2421 if (pdf) {
2422 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
2423 tctm = &state.pagedims[page->pdimno].tctm;
2425 else {
2426 tctm = &fz_identity;
2429 p.x = x;
2430 p.y = y;
2432 fz_concat (&ctm, tctm, &state.pagedims[page->pdimno].ctm);
2433 fz_invert_matrix (&ctm, &ctm);
2434 fz_transform_point (&p, &ctm);
2436 if (pdf) {
2437 for (int i = 0; i < page->annotcount; ++i) {
2438 struct annot *a = &page->annots[i];
2439 fz_rect rect;
2441 fz_bound_annot (state.ctx, a->annot, &rect);
2442 if (p.x >= rect.x0 && p.x <= rect.x1) {
2443 if (p.y >= rect.y0 && p.y <= rect.y1)
2444 return a;
2448 return NULL;
2451 static fz_link *getlink (struct page *page, int x, int y)
2453 fz_point p;
2454 fz_matrix ctm;
2455 fz_link *link;
2457 ensureslinks (page);
2459 p.x = x;
2460 p.y = y;
2462 ctm = pagectm (page);
2463 fz_invert_matrix (&ctm, &ctm);
2464 fz_transform_point (&p, &ctm);
2466 for (link = page->links; link; link = link->next) {
2467 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
2468 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
2469 return link;
2473 return NULL;
2476 static void ensuretext (struct page *page)
2478 if (state.gen != page->tgen) {
2479 droptext (page);
2480 page->tgen = state.gen;
2482 if (!page->text) {
2483 fz_matrix ctm;
2484 fz_device *tdev;
2486 page->text = fz_new_stext_page (state.ctx,
2487 &state.pagedims[page->pdimno].mediabox);
2488 tdev = fz_new_stext_device (state.ctx, page->text, 0);
2489 ctm = pagectm (page);
2490 fz_run_display_list (state.ctx, page->dlist,
2491 tdev, &ctm, &fz_infinite_rect, NULL);
2492 fz_close_device (state.ctx, tdev);
2493 fz_drop_device (state.ctx, tdev);
2497 CAMLprim value ml_find_page_with_links (value start_page_v, value dir_v)
2499 CAMLparam2 (start_page_v, dir_v);
2500 CAMLlocal1 (ret_v);
2501 int i, dir = Int_val (dir_v);
2502 int start_page = Int_val (start_page_v);
2503 int end_page = dir > 0 ? state.pagecount : -1;
2504 pdf_document *pdf;
2506 fz_var (end_page);
2507 ret_v = Val_int (0);
2508 lock (__func__);
2509 pdf = pdf_specifics (state.ctx, state.doc);
2510 for (i = start_page + dir; i != end_page; i += dir) {
2511 int found;
2513 fz_var (found);
2514 if (pdf) {
2515 pdf_page *page = NULL;
2517 fz_var (page);
2518 fz_try (state.ctx) {
2519 page = pdf_load_page (state.ctx, pdf, i);
2520 found = !!page->links || !!page->annots;
2522 fz_catch (state.ctx) {
2523 found = 0;
2525 if (page) {
2526 fz_drop_page (state.ctx, &page->super);
2529 else {
2530 fz_page *page = fz_load_page (state.ctx, state.doc, i);
2531 fz_link *link = fz_load_links (state.ctx, page);
2532 found = !!link;
2533 fz_drop_link (state.ctx, link);
2534 fz_drop_page (state.ctx, page);
2537 if (found) {
2538 ret_v = caml_alloc_small (1, 1);
2539 Field (ret_v, 0) = Val_int (i);
2540 goto unlock;
2543 unlock:
2544 unlock (__func__);
2545 CAMLreturn (ret_v);
2548 enum { dir_first, dir_last };
2549 enum { dir_first_visible, dir_left, dir_right, dir_down, dir_up };
2551 CAMLprim value ml_findlink (value ptr_v, value dir_v)
2553 CAMLparam2 (ptr_v, dir_v);
2554 CAMLlocal2 (ret_v, pos_v);
2555 struct page *page;
2556 int dirtag, i, slinkindex;
2557 struct slink *found = NULL ,*slink;
2558 char *s = String_val (ptr_v);
2560 page = parse_pointer (__func__, s);
2561 ret_v = Val_int (0);
2562 lock (__func__);
2563 ensureslinks (page);
2565 if (Is_block (dir_v)) {
2566 dirtag = Tag_val (dir_v);
2567 switch (dirtag) {
2568 case dir_first_visible:
2570 int x0, y0, dir, first_index, last_index;
2572 pos_v = Field (dir_v, 0);
2573 x0 = Int_val (Field (pos_v, 0));
2574 y0 = Int_val (Field (pos_v, 1));
2575 dir = Int_val (Field (pos_v, 2));
2577 if (dir >= 0) {
2578 dir = 1;
2579 first_index = 0;
2580 last_index = page->slinkcount;
2582 else {
2583 first_index = page->slinkcount - 1;
2584 last_index = -1;
2587 for (i = first_index; i != last_index; i += dir) {
2588 slink = &page->slinks[i];
2589 if (slink->bbox.y0 >= y0 && slink->bbox.x0 >= x0) {
2590 found = slink;
2591 break;
2595 break;
2597 case dir_left:
2598 slinkindex = Int_val (Field (dir_v, 0));
2599 found = &page->slinks[slinkindex];
2600 for (i = slinkindex - 1; i >= 0; --i) {
2601 slink = &page->slinks[i];
2602 if (slink->bbox.x0 < found->bbox.x0) {
2603 found = slink;
2604 break;
2607 break;
2609 case dir_right:
2610 slinkindex = Int_val (Field (dir_v, 0));
2611 found = &page->slinks[slinkindex];
2612 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2613 slink = &page->slinks[i];
2614 if (slink->bbox.x0 > found->bbox.x0) {
2615 found = slink;
2616 break;
2619 break;
2621 case dir_down:
2622 slinkindex = Int_val (Field (dir_v, 0));
2623 found = &page->slinks[slinkindex];
2624 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2625 slink = &page->slinks[i];
2626 if (slink->bbox.y0 >= found->bbox.y0) {
2627 found = slink;
2628 break;
2631 break;
2633 case dir_up:
2634 slinkindex = Int_val (Field (dir_v, 0));
2635 found = &page->slinks[slinkindex];
2636 for (i = slinkindex - 1; i >= 0; --i) {
2637 slink = &page->slinks[i];
2638 if (slink->bbox.y0 <= found->bbox.y0) {
2639 found = slink;
2640 break;
2643 break;
2646 else {
2647 dirtag = Int_val (dir_v);
2648 switch (dirtag) {
2649 case dir_first:
2650 found = page->slinks;
2651 break;
2653 case dir_last:
2654 if (page->slinks) {
2655 found = page->slinks + (page->slinkcount - 1);
2657 break;
2660 if (found) {
2661 ret_v = caml_alloc_small (2, 1);
2662 Field (ret_v, 0) = Val_int (found - page->slinks);
2665 unlock (__func__);
2666 CAMLreturn (ret_v);
2669 enum { uuri, utext, uannot };
2671 CAMLprim value ml_getlink (value ptr_v, value n_v)
2673 CAMLparam2 (ptr_v, n_v);
2674 CAMLlocal4 (ret_v, tup_v, str_v, gr_v);
2675 fz_link *link;
2676 struct page *page;
2677 char *s = String_val (ptr_v);
2678 struct slink *slink;
2680 ret_v = Val_int (0);
2681 page = parse_pointer (__func__, s);
2683 lock (__func__);
2684 ensureslinks (page);
2685 slink = &page->slinks[Int_val (n_v)];
2686 if (slink->tag == SLINK) {
2687 link = slink->u.link;
2688 str_v = caml_copy_string (link->uri);
2689 ret_v = caml_alloc_small (1, uuri);
2690 Field (ret_v, 0) = str_v;
2692 else {
2693 ret_v = caml_alloc_small (1, uannot);
2694 tup_v = caml_alloc_tuple (2);
2695 Field (ret_v, 0) = tup_v;
2696 Field (tup_v, 0) = ptr_v;
2697 Field (tup_v, 1) = n_v;
2699 unlock (__func__);
2701 CAMLreturn (ret_v);
2704 CAMLprim value ml_getannotcontents (value ptr_v, value n_v)
2706 CAMLparam2 (ptr_v, n_v);
2707 CAMLlocal1 (ret_v);
2708 pdf_document *pdf;
2709 char *contents = NULL;
2711 lock (__func__);
2712 pdf = pdf_specifics (state.ctx, state.doc);
2713 if (pdf) {
2714 char *s = String_val (ptr_v);
2715 struct page *page;
2716 struct slink *slink;
2718 page = parse_pointer (__func__, s);
2719 slink = &page->slinks[Int_val (n_v)];
2720 contents = pdf_copy_annot_contents (state.ctx,
2721 (pdf_annot *) slink->u.annot);
2723 unlock (__func__);
2724 if (contents) {
2725 ret_v = caml_copy_string (contents);
2726 fz_free (state.ctx, contents);
2728 else {
2729 ret_v = caml_copy_string ("");
2731 CAMLreturn (ret_v);
2734 CAMLprim value ml_getlinkcount (value ptr_v)
2736 CAMLparam1 (ptr_v);
2737 struct page *page;
2738 char *s = String_val (ptr_v);
2740 page = parse_pointer (__func__, s);
2741 CAMLreturn (Val_int (page->slinkcount));
2744 CAMLprim value ml_getlinkrect (value ptr_v, value n_v)
2746 CAMLparam2 (ptr_v, n_v);
2747 CAMLlocal1 (ret_v);
2748 struct page *page;
2749 struct slink *slink;
2750 char *s = String_val (ptr_v);
2752 page = parse_pointer (__func__, s);
2753 ret_v = caml_alloc_tuple (4);
2754 lock (__func__);
2755 ensureslinks (page);
2757 slink = &page->slinks[Int_val (n_v)];
2758 Field (ret_v, 0) = Val_int (slink->bbox.x0);
2759 Field (ret_v, 1) = Val_int (slink->bbox.y0);
2760 Field (ret_v, 2) = Val_int (slink->bbox.x1);
2761 Field (ret_v, 3) = Val_int (slink->bbox.y1);
2762 unlock (__func__);
2763 CAMLreturn (ret_v);
2766 CAMLprim value ml_whatsunder (value ptr_v, value x_v, value y_v)
2768 CAMLparam3 (ptr_v, x_v, y_v);
2769 CAMLlocal4 (ret_v, tup_v, str_v, gr_v);
2770 fz_link *link;
2771 struct annot *annot;
2772 struct page *page;
2773 char *ptr = String_val (ptr_v);
2774 int x = Int_val (x_v), y = Int_val (y_v);
2775 struct pagedim *pdim;
2777 ret_v = Val_int (0);
2778 if (trylock (__func__)) {
2779 goto done;
2782 page = parse_pointer (__func__, ptr);
2783 pdim = &state.pagedims[page->pdimno];
2784 x += pdim->bounds.x0;
2785 y += pdim->bounds.y0;
2788 annot = getannot (page, x, y);
2789 if (annot) {
2790 int i, n = -1;
2792 ensureslinks (page);
2793 for (i = 0; i < page->slinkcount; ++i) {
2794 if (page->slinks[i].tag == SANNOT
2795 && page->slinks[i].u.annot == annot->annot) {
2796 n = i;
2797 break;
2800 ret_v = caml_alloc_small (1, uannot);
2801 tup_v = caml_alloc_tuple (2);
2802 Field (ret_v, 0) = tup_v;
2803 Field (tup_v, 0) = ptr_v;
2804 Field (tup_v, 1) = Val_int (n);
2805 goto unlock;
2809 link = getlink (page, x, y);
2810 if (link) {
2811 str_v = caml_copy_string (link->uri);
2812 ret_v = caml_alloc_small (1, uuri);
2813 Field (ret_v, 0) = str_v;
2815 else {
2816 fz_rect *b;
2817 fz_stext_block *block;
2819 ensuretext (page);
2821 for (block = page->text->first_block; block; block = block->next) {
2822 fz_stext_line *line;
2824 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
2825 b = &block->bbox;
2826 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2827 continue;
2829 for (line = block->u.t.first_line; line; line = line->next) {
2830 fz_stext_char *ch;
2832 b = &line->bbox;
2833 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2834 continue;
2836 for (ch = line->first_char; ch; ch = ch->next) {
2837 b = &ch->bbox;
2839 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1) {
2840 const char *n2 = fz_font_name (state.ctx, ch->font);
2841 FT_FaceRec *face = fz_font_ft_face (state.ctx,
2842 ch->font);
2844 if (!n2) n2 = "<unknown font>";
2846 if (face && face->family_name) {
2847 char *s;
2848 char *n1 = face->family_name;
2849 size_t l1 = strlen (n1);
2850 size_t l2 = strlen (n2);
2852 if (l1 != l2 || memcmp (n1, n2, l1)) {
2853 s = malloc (l1 + l2 + 2);
2854 if (s) {
2855 memcpy (s, n2, l2);
2856 s[l2] = '=';
2857 memcpy (s + l2 + 1, n1, l1 + 1);
2858 str_v = caml_copy_string (s);
2859 free (s);
2863 if (str_v == Val_unit) {
2864 str_v = caml_copy_string (n2);
2866 ret_v = caml_alloc_small (1, utext);
2867 Field (ret_v, 0) = str_v;
2868 goto unlock;
2874 unlock:
2875 unlock (__func__);
2877 done:
2878 CAMLreturn (ret_v);
2881 enum { mark_page, mark_block, mark_line, mark_word };
2883 CAMLprim void ml_clearmark (value ptr_v)
2885 CAMLparam1 (ptr_v);
2886 char *s = String_val (ptr_v);
2887 struct page *page;
2889 if (trylock (__func__)) {
2890 goto done;
2893 page = parse_pointer (__func__, s);
2894 page->fmark = NULL;
2895 page->lmark = NULL;
2897 unlock (__func__);
2898 done:
2899 CAMLreturn0;
2902 static int uninteresting (int c)
2904 return c == ' ' || c == '\n' || c == '\t' || c == '\n' || c == '\r'
2905 || ispunct (c);
2908 CAMLprim value ml_markunder (value ptr_v, value x_v, value y_v, value mark_v)
2910 CAMLparam4 (ptr_v, x_v, y_v, mark_v);
2911 CAMLlocal1 (ret_v);
2912 fz_rect *b;
2913 struct page *page;
2914 fz_stext_line *line;
2915 fz_stext_block *block;
2916 struct pagedim *pdim;
2917 int mark = Int_val (mark_v);
2918 char *s = String_val (ptr_v);
2919 int x = Int_val (x_v), y = Int_val (y_v);
2921 ret_v = Val_bool (0);
2922 if (trylock (__func__)) {
2923 goto done;
2926 page = parse_pointer (__func__, s);
2927 pdim = &state.pagedims[page->pdimno];
2929 ensuretext (page);
2931 if (mark == mark_page) {
2932 page->fmark = page->text->first_block->u.t.first_line->first_char;
2933 page->lmark = page->text->last_block->u.t.last_line->last_char;
2934 ret_v = Val_bool (1);
2935 goto unlock;
2938 x += pdim->bounds.x0;
2939 y += pdim->bounds.y0;
2941 for (block = page->text->first_block; block; block = block->next) {
2942 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
2943 b = &block->bbox;
2944 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2945 continue;
2947 if (mark == mark_block) {
2948 page->fmark = block->u.t.first_line->first_char;
2949 page->lmark = block->u.t.last_line->last_char;
2950 ret_v = Val_bool (1);
2951 goto unlock;
2954 for (line = block->u.t.first_line; line; line = line->next) {
2955 fz_stext_char *ch;
2957 b = &line->bbox;
2958 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2959 continue;
2961 if (mark == mark_line) {
2962 page->fmark = line->first_char;
2963 page->lmark = line->last_char;
2964 ret_v = Val_bool (1);
2965 goto unlock;
2968 for (ch = line->first_char; ch; ch = ch->next) {
2969 fz_stext_char *ch2, *first = NULL, *last = NULL;
2970 b = &ch->bbox;
2971 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1) {
2972 for (ch2 = line->first_char; ch2 != ch; ch2 = ch2->next) {
2973 if (uninteresting (ch2->c)) first = NULL;
2974 else if (!first) first = ch2;
2976 for (ch2 = ch; ch2; ch2 = ch2->next) {
2977 if (uninteresting (ch2->c)) break;
2978 last = ch2;
2981 page->fmark = first;
2982 page->lmark = last;
2983 ret_v = Val_bool (1);
2984 goto unlock;
2989 unlock:
2990 if (!Bool_val (ret_v)) {
2991 page->fmark = NULL;
2992 page->lmark = NULL;
2994 unlock (__func__);
2996 done:
2997 CAMLreturn (ret_v);
3000 CAMLprim value ml_rectofblock (value ptr_v, value x_v, value y_v)
3002 CAMLparam3 (ptr_v, x_v, y_v);
3003 CAMLlocal2 (ret_v, res_v);
3004 fz_rect *b = NULL;
3005 struct page *page;
3006 struct pagedim *pdim;
3007 fz_stext_block *block;
3008 char *s = String_val (ptr_v);
3009 int x = Int_val (x_v), y = Int_val (y_v);
3011 ret_v = Val_int (0);
3012 if (trylock (__func__)) {
3013 goto done;
3016 page = parse_pointer (__func__, s);
3017 pdim = &state.pagedims[page->pdimno];
3018 x += pdim->bounds.x0;
3019 y += pdim->bounds.y0;
3021 ensuretext (page);
3023 for (block = page->text->first_block; block; block = block->next) {
3024 switch (block->type) {
3025 case FZ_STEXT_BLOCK_TEXT:
3026 b = &block->bbox;
3027 break;
3029 case FZ_STEXT_BLOCK_IMAGE:
3030 b = &block->bbox;
3031 break;
3033 default:
3034 continue;
3037 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1)
3038 break;
3039 b = NULL;
3041 if (b) {
3042 res_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
3043 ret_v = caml_alloc_small (1, 1);
3044 Store_double_field (res_v, 0, (double) b->x0);
3045 Store_double_field (res_v, 1, (double) b->x1);
3046 Store_double_field (res_v, 2, (double) b->y0);
3047 Store_double_field (res_v, 3, (double) b->y1);
3048 Field (ret_v, 0) = res_v;
3050 unlock (__func__);
3052 done:
3053 CAMLreturn (ret_v);
3056 CAMLprim void ml_seltext (value ptr_v, value rect_v)
3058 CAMLparam2 (ptr_v, rect_v);
3059 fz_rect b;
3060 struct page *page;
3061 struct pagedim *pdim;
3062 char *s = String_val (ptr_v);
3063 int x0, x1, y0, y1;
3064 fz_stext_char *ch;
3065 fz_stext_line *line;
3066 fz_stext_block *block;
3067 fz_stext_char *fc, *lc;
3069 if (trylock (__func__)) {
3070 goto done;
3073 page = parse_pointer (__func__, s);
3074 ensuretext (page);
3076 pdim = &state.pagedims[page->pdimno];
3077 x0 = Int_val (Field (rect_v, 0)) + pdim->bounds.x0;
3078 y0 = Int_val (Field (rect_v, 1)) + pdim->bounds.y0;
3079 x1 = Int_val (Field (rect_v, 2)) + pdim->bounds.x0;
3080 y1 = Int_val (Field (rect_v, 3)) + pdim->bounds.y0;
3082 if (y0 > y1) {
3083 int t = y0;
3084 y0 = y1;
3085 y1 = t;
3086 x0 = x1;
3087 x1 = t;
3090 fc = page->fmark;
3091 lc = page->lmark;
3093 for (block = page->text->first_block; block; block = block->next) {
3094 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
3095 for (line = block->u.t.first_line; line; line = line->next) {
3096 for (ch = line->first_char; ch; ch = ch->next) {
3097 b = ch->bbox;
3098 if (x0 >= b.x0 && x0 <= b.x1 && y0 >= b.y0 && y0 <= b.y1) {
3099 fc = ch;
3101 if (x1 >= b.x0 && x1 <= b.x1 && y1 >= b.y0 && y1 <= b.y1) {
3102 lc = ch;
3107 if (x1 < x0 && fc == lc) {
3108 fz_stext_char *t;
3110 t = fc;
3111 fc = lc;
3112 lc = t;
3115 page->fmark = fc;
3116 page->lmark = lc;
3118 unlock (__func__);
3120 done:
3121 CAMLreturn0;
3124 static int pipechar (FILE *f, fz_stext_char *ch)
3126 char buf[4];
3127 int len;
3128 size_t ret;
3130 len = fz_runetochar (buf, ch->c);
3131 ret = fwrite (buf, len, 1, f);
3132 if (ret != 1) {
3133 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3134 len, ret, errno, strerror (errno));
3135 return -1;
3137 return 0;
3140 CAMLprim value ml_spawn (value command_v, value fds_v)
3142 CAMLparam2 (command_v, fds_v);
3143 CAMLlocal2 (l_v, tup_v);
3144 int ret, ret1;
3145 pid_t pid = (pid_t) -1;
3146 char *msg = NULL;
3147 value earg_v = Nothing;
3148 posix_spawnattr_t attr;
3149 posix_spawn_file_actions_t fa;
3150 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
3152 argv[2] = String_val (command_v);
3154 if ((ret = posix_spawn_file_actions_init (&fa)) != 0) {
3155 unix_error (ret, "posix_spawn_file_actions_init", Nothing);
3158 if ((ret = posix_spawnattr_init (&attr)) != 0) {
3159 msg = "posix_spawnattr_init";
3160 goto fail1;
3163 #ifdef POSIX_SPAWN_USEVFORK
3164 if ((ret = posix_spawnattr_setflags (&attr, POSIX_SPAWN_USEVFORK)) != 0) {
3165 msg = "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3166 goto fail;
3168 #endif
3170 for (l_v = fds_v; l_v != Val_int (0); l_v = Field (l_v, 1)) {
3171 int fd1, fd2;
3173 tup_v = Field (l_v, 0);
3174 fd1 = Int_val (Field (tup_v, 0));
3175 fd2 = Int_val (Field (tup_v, 1));
3176 if (fd2 < 0) {
3177 if ((ret = posix_spawn_file_actions_addclose (&fa, fd1)) != 0) {
3178 msg = "posix_spawn_file_actions_addclose";
3179 earg_v = tup_v;
3180 goto fail;
3183 else {
3184 if ((ret = posix_spawn_file_actions_adddup2 (&fa, fd1, fd2)) != 0) {
3185 msg = "posix_spawn_file_actions_adddup2";
3186 earg_v = tup_v;
3187 goto fail;
3192 if ((ret = posix_spawn (&pid, "/bin/sh", &fa, &attr, argv, environ))) {
3193 msg = "posix_spawn";
3194 goto fail;
3197 fail:
3198 if ((ret1 = posix_spawnattr_destroy (&attr)) != 0) {
3199 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1, strerror (ret1));
3202 fail1:
3203 if ((ret1 = posix_spawn_file_actions_destroy (&fa)) != 0) {
3204 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3205 ret1, strerror (ret1));
3208 if (msg)
3209 unix_error (ret, msg, earg_v);
3211 CAMLreturn (Val_int (pid));
3214 CAMLprim value ml_hassel (value ptr_v)
3216 CAMLparam1 (ptr_v);
3217 CAMLlocal1 (ret_v);
3218 struct page *page;
3219 char *s = String_val (ptr_v);
3221 ret_v = Val_bool (0);
3222 if (trylock (__func__)) {
3223 goto done;
3226 page = parse_pointer (__func__, s);
3227 ret_v = Val_bool (page->fmark && page->lmark);
3228 unlock (__func__);
3229 done:
3230 CAMLreturn (ret_v);
3233 CAMLprim void ml_copysel (value fd_v, value ptr_v)
3235 CAMLparam2 (fd_v, ptr_v);
3236 FILE *f;
3237 int seen = 0;
3238 struct page *page;
3239 fz_stext_line *line;
3240 fz_stext_block *block;
3241 int fd = Int_val (fd_v);
3242 char *s = String_val (ptr_v);
3244 if (trylock (__func__)) {
3245 goto done;
3248 page = parse_pointer (__func__, s);
3250 if (!page->fmark || !page->lmark) {
3251 printd ("emsg nothing to copy on page %d", page->pageno);
3252 goto unlock;
3255 f = fdopen (fd, "w");
3256 if (!f) {
3257 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3258 fd, errno, strerror (errno));
3259 f = stdout;
3262 for (block = page->text->first_block; block; block = block->next) {
3263 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
3264 for (line = block->u.t.first_line; line; line = line->next) {
3265 fz_stext_char *ch;
3266 for (ch = line->first_char; ch; ch = ch->next) {
3267 if (seen || ch == page->fmark) {
3268 do {
3269 pipechar (f, ch);
3270 if (ch == page->lmark) goto close;
3271 } while ((ch = ch->next));
3272 seen = 1;
3273 break;
3276 if (seen) fputc ('\n', f);
3279 close:
3280 if (f != stdout) {
3281 int ret = fclose (f);
3282 fd = -1;
3283 if (ret == -1) {
3284 if (errno != ECHILD) {
3285 printd ("emsg failed to close sel pipe: %d:%s",
3286 errno, strerror (errno));
3290 unlock:
3291 unlock (__func__);
3293 done:
3294 if (fd >= 0) {
3295 if (close (fd)) {
3296 printd ("emsg failed to close sel pipe: %d:%s",
3297 errno, strerror (errno));
3300 CAMLreturn0;
3303 CAMLprim value ml_getpdimrect (value pagedimno_v)
3305 CAMLparam1 (pagedimno_v);
3306 CAMLlocal1 (ret_v);
3307 int pagedimno = Int_val (pagedimno_v);
3308 fz_rect box;
3310 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
3311 if (trylock (__func__)) {
3312 box = fz_empty_rect;
3314 else {
3315 box = state.pagedims[pagedimno].mediabox;
3316 unlock (__func__);
3319 Store_double_field (ret_v, 0, (double) box.x0);
3320 Store_double_field (ret_v, 1, (double) box.x1);
3321 Store_double_field (ret_v, 2, (double) box.y0);
3322 Store_double_field (ret_v, 3, (double) box.y1);
3324 CAMLreturn (ret_v);
3327 CAMLprim value ml_zoom_for_height (value winw_v, value winh_v,
3328 value dw_v, value cols_v)
3330 CAMLparam4 (winw_v, winh_v, dw_v, cols_v);
3331 CAMLlocal1 (ret_v);
3332 int i;
3333 float zoom = -1.;
3334 float maxh = 0.0;
3335 struct pagedim *p;
3336 float winw = Int_val (winw_v);
3337 float winh = Int_val (winh_v);
3338 float dw = Int_val (dw_v);
3339 float cols = Int_val (cols_v);
3340 float pw = 1.0, ph = 1.0;
3342 if (trylock (__func__)) {
3343 goto done;
3346 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3347 float w = p->pagebox.x1 / cols;
3348 float h = p->pagebox.y1;
3349 if (h > maxh) {
3350 maxh = h;
3351 ph = h;
3352 if (state.fitmodel != FitProportional) pw = w;
3354 if ((state.fitmodel == FitProportional) && w > pw) pw = w;
3357 zoom = (((winh / ph) * pw) + dw) / winw;
3358 unlock (__func__);
3359 done:
3360 ret_v = caml_copy_double ((double) zoom);
3361 CAMLreturn (ret_v);
3364 CAMLprim value ml_getmaxw (value unit_v)
3366 CAMLparam1 (unit_v);
3367 CAMLlocal1 (ret_v);
3368 int i;
3369 float maxw = -1.;
3370 struct pagedim *p;
3372 if (trylock (__func__)) {
3373 goto done;
3376 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3377 float w = p->pagebox.x1;
3378 maxw = fz_max (maxw, w);
3381 unlock (__func__);
3382 done:
3383 ret_v = caml_copy_double ((double) maxw);
3384 CAMLreturn (ret_v);
3387 CAMLprim value ml_draw_string (value pt_v, value x_v, value y_v, value string_v)
3389 CAMLparam4 (pt_v, x_v, y_v, string_v);
3390 CAMLlocal1 (ret_v);
3391 int pt = Int_val(pt_v);
3392 int x = Int_val (x_v);
3393 int y = Int_val (y_v);
3394 double w;
3396 w = (double) draw_string (state.face, pt, x, y, String_val (string_v));
3397 ret_v = caml_copy_double (w);
3398 CAMLreturn (ret_v);
3401 CAMLprim value ml_measure_string (value pt_v, value string_v)
3403 CAMLparam2 (pt_v, string_v);
3404 CAMLlocal1 (ret_v);
3405 int pt = Int_val (pt_v);
3406 double w;
3408 w = (double) measure_string (state.face, pt, String_val (string_v));
3409 ret_v = caml_copy_double (w);
3410 CAMLreturn (ret_v);
3413 CAMLprim value ml_getpagebox (value opaque_v)
3415 CAMLparam1 (opaque_v);
3416 CAMLlocal1 (ret_v);
3417 fz_rect rect;
3418 fz_irect bbox;
3419 fz_matrix ctm;
3420 fz_device *dev;
3421 char *s = String_val (opaque_v);
3422 struct page *page = parse_pointer (__func__, s);
3424 ret_v = caml_alloc_tuple (4);
3425 dev = fz_new_bbox_device (state.ctx, &rect);
3427 ctm = pagectm (page);
3428 fz_run_page (state.ctx, page->fzpage, dev, &ctm, NULL);
3430 fz_close_device (state.ctx, dev);
3431 fz_drop_device (state.ctx, dev);
3432 fz_round_rect (&bbox, &rect);
3433 Field (ret_v, 0) = Val_int (bbox.x0);
3434 Field (ret_v, 1) = Val_int (bbox.y0);
3435 Field (ret_v, 2) = Val_int (bbox.x1);
3436 Field (ret_v, 3) = Val_int (bbox.y1);
3438 CAMLreturn (ret_v);
3441 CAMLprim void ml_setaalevel (value level_v)
3443 CAMLparam1 (level_v);
3445 state.aalevel = Int_val (level_v);
3446 CAMLreturn0;
3449 #ifndef CIDER
3450 CAMLprim value ml_keysymtoutf8 (value keysym_v)
3452 CAMLparam1 (keysym_v);
3453 CAMLlocal1 (str_v);
3454 KeySym keysym = Int_val (keysym_v);
3455 Rune rune;
3456 extern long keysym2ucs (KeySym);
3457 int len;
3458 char buf[5];
3460 rune = (Rune) keysym2ucs (keysym);
3461 len = fz_runetochar (buf, rune);
3462 buf[len] = 0;
3463 str_v = caml_copy_string (buf);
3464 CAMLreturn (str_v);
3466 #else
3467 CAMLprim value ml_keysymtoutf8 (value keysym_v)
3469 CAMLparam1 (keysym_v);
3470 CAMLlocal1 (str_v);
3471 long ucs_v = Long_val (keysym_v);
3472 int len;
3473 char buf[5];
3475 len = fz_runetochar (buf, (int) ucs_v);
3476 buf[len] = 0;
3477 str_v = caml_copy_string (buf);
3478 CAMLreturn (str_v);
3480 #endif
3482 enum { piunknown, pilinux, pimacos, pibsd };
3484 CAMLprim value ml_platform (value unit_v)
3486 CAMLparam1 (unit_v);
3487 CAMLlocal2 (tup_v, arr_v);
3488 int platid = piunknown;
3489 struct utsname buf;
3491 #if defined __linux__
3492 platid = pilinux;
3493 #elif defined __DragonFly__ || defined __FreeBSD__
3494 || defined __OpenBSD__ || defined __NetBSD__
3495 platid = pibsd;
3496 #elif defined __APPLE__
3497 platid = pimacos;
3498 #endif
3499 if (uname (&buf)) err (1, "uname");
3501 tup_v = caml_alloc_tuple (2);
3503 char const *sar[] = {
3504 buf.sysname,
3505 buf.release,
3506 buf.version,
3507 buf.machine,
3508 NULL
3510 arr_v = caml_copy_string_array (sar);
3512 Field (tup_v, 0) = Val_int (platid);
3513 Field (tup_v, 1) = arr_v;
3514 CAMLreturn (tup_v);
3517 CAMLprim void ml_cloexec (value fd_v)
3519 CAMLparam1 (fd_v);
3520 int fd = Int_val (fd_v);
3522 if (fcntl (fd, F_SETFD, FD_CLOEXEC, 1)) {
3523 uerror ("fcntl", Nothing);
3525 CAMLreturn0;
3528 CAMLprim value ml_getpbo (value w_v, value h_v, value cs_v)
3530 CAMLparam2 (w_v, h_v);
3531 CAMLlocal1 (ret_v);
3532 struct bo *pbo;
3533 int w = Int_val (w_v);
3534 int h = Int_val (h_v);
3535 int cs = Int_val (cs_v);
3537 if (state.bo_usable) {
3538 pbo = calloc (sizeof (*pbo), 1);
3539 if (!pbo) {
3540 err (1, "calloc pbo");
3543 switch (cs) {
3544 case 0:
3545 case 1:
3546 pbo->size = w*h*4;
3547 break;
3548 case 2:
3549 pbo->size = w*h*2;
3550 break;
3551 default:
3552 errx (1, "%s: invalid colorspace %d", __func__, cs);
3555 state.glGenBuffersARB (1, &pbo->id);
3556 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, pbo->id);
3557 state.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB, (GLsizei) pbo->size,
3558 NULL, GL_STREAM_DRAW);
3559 pbo->ptr = state.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB,
3560 GL_READ_WRITE);
3561 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
3562 if (!pbo->ptr) {
3563 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3564 state.glDeleteBuffersARB (1, &pbo->id);
3565 free (pbo);
3566 ret_v = caml_copy_string ("0");
3568 else {
3569 int res;
3570 char *s;
3572 res = snprintf (NULL, 0, "%" PRIxPTR, (uintptr_t) pbo);
3573 if (res < 0) {
3574 err (1, "snprintf %" PRIxPTR " failed", (uintptr_t) pbo);
3576 s = malloc (res+1);
3577 if (!s) {
3578 err (1, "malloc %d bytes failed", res+1);
3580 res = sprintf (s, "%" PRIxPTR, (uintptr_t) pbo);
3581 if (res < 0) {
3582 err (1, "sprintf %" PRIxPTR " failed", (uintptr_t) pbo);
3584 ret_v = caml_copy_string (s);
3585 free (s);
3588 else {
3589 ret_v = caml_copy_string ("0");
3591 CAMLreturn (ret_v);
3594 CAMLprim void ml_freepbo (value s_v)
3596 CAMLparam1 (s_v);
3597 char *s = String_val (s_v);
3598 struct tile *tile = parse_pointer (__func__, s);
3600 if (tile->pbo) {
3601 state.glDeleteBuffersARB (1, &tile->pbo->id);
3602 tile->pbo->id = -1;
3603 tile->pbo->ptr = NULL;
3604 tile->pbo->size = -1;
3606 CAMLreturn0;
3609 CAMLprim void ml_unmappbo (value s_v)
3611 CAMLparam1 (s_v);
3612 char *s = String_val (s_v);
3613 struct tile *tile = parse_pointer (__func__, s);
3615 if (tile->pbo) {
3616 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, tile->pbo->id);
3617 if (state.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) == GL_FALSE) {
3618 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3620 tile->pbo->ptr = NULL;
3621 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
3623 CAMLreturn0;
3626 static void setuppbo (void)
3628 extern void (*wsigladdr (const char *name)) (void);
3629 #pragma GCC diagnostic push
3630 #ifdef __clang__
3631 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3632 #endif
3633 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3634 state.bo_usable = GPA (glBindBufferARB)
3635 && GPA (glUnmapBufferARB)
3636 && GPA (glMapBufferARB)
3637 && GPA (glBufferDataARB)
3638 && GPA (glGenBuffersARB)
3639 && GPA (glDeleteBuffersARB);
3640 #undef GPA
3641 #pragma GCC diagnostic pop
3644 CAMLprim value ml_bo_usable (value unit_v)
3646 CAMLparam1 (unit_v);
3647 CAMLreturn (Val_bool (state.bo_usable));
3650 CAMLprim value ml_unproject (value ptr_v, value x_v, value y_v)
3652 CAMLparam3 (ptr_v, x_v, y_v);
3653 CAMLlocal2 (ret_v, tup_v);
3654 struct page *page;
3655 char *s = String_val (ptr_v);
3656 int x = Int_val (x_v), y = Int_val (y_v);
3657 struct pagedim *pdim;
3658 fz_point p;
3659 fz_matrix ctm;
3661 page = parse_pointer (__func__, s);
3662 pdim = &state.pagedims[page->pdimno];
3664 ret_v = Val_int (0);
3665 if (trylock (__func__)) {
3666 goto done;
3669 if (pdf_specifics (state.ctx, state.doc)) {
3670 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
3671 ctm = state.pagedims[page->pdimno].tctm;
3673 else {
3674 ctm = fz_identity;
3676 p.x = x + pdim->bounds.x0;
3677 p.y = y + pdim->bounds.y0;
3679 fz_concat (&ctm, &pdim->tctm, &pdim->ctm);
3680 fz_invert_matrix (&ctm, &ctm);
3681 fz_transform_point (&p, &ctm);
3683 tup_v = caml_alloc_tuple (2);
3684 ret_v = caml_alloc_small (1, 1);
3685 Field (tup_v, 0) = Val_int (p.x);
3686 Field (tup_v, 1) = Val_int (p.y);
3687 Field (ret_v, 0) = tup_v;
3689 unlock (__func__);
3690 done:
3691 CAMLreturn (ret_v);
3694 CAMLprim value ml_project (value ptr_v, value pageno_v, value pdimno_v,
3695 value x_v, value y_v)
3697 CAMLparam5 (ptr_v, pageno_v, pdimno_v, x_v, y_v);
3698 CAMLlocal1 (ret_v);
3699 struct page *page;
3700 char *s = String_val (ptr_v);
3701 int pageno = Int_val (pageno_v);
3702 int pdimno = Int_val (pdimno_v);
3703 float x = (float) Double_val (x_v), y = (float) Double_val (y_v);
3704 struct pagedim *pdim;
3705 fz_point p;
3706 fz_matrix ctm;
3708 ret_v = Val_int (0);
3709 lock (__func__);
3711 if (!*s) {
3712 page = loadpage (pageno, pdimno);
3714 else {
3715 page = parse_pointer (__func__, s);
3717 pdim = &state.pagedims[pdimno];
3719 if (pdf_specifics (state.ctx, state.doc)) {
3720 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
3721 ctm = state.pagedims[page->pdimno].tctm;
3723 else {
3724 ctm = fz_identity;
3726 p.x = x + pdim->bounds.x0;
3727 p.y = y + pdim->bounds.y0;
3729 fz_concat (&ctm, &pdim->tctm, &pdim->ctm);
3730 fz_transform_point (&p, &ctm);
3732 ret_v = caml_alloc_tuple (2);
3733 Field (ret_v, 0) = caml_copy_double ((double) p.x);
3734 Field (ret_v, 1) = caml_copy_double ((double) p.y);
3736 if (!*s) {
3737 freepage (page);
3739 unlock (__func__);
3740 CAMLreturn (ret_v);
3743 CAMLprim void ml_addannot (value ptr_v, value x_v, value y_v,
3744 value contents_v)
3746 CAMLparam4 (ptr_v, x_v, y_v, contents_v);
3747 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3749 if (pdf) {
3750 pdf_annot *annot;
3751 struct page *page;
3752 fz_point p;
3753 char *s = String_val (ptr_v);
3755 page = parse_pointer (__func__, s);
3756 annot = pdf_create_annot (state.ctx,
3757 pdf_page_from_fz_page (state.ctx,
3758 page->fzpage),
3759 PDF_ANNOT_TEXT);
3760 p.x = Int_val (x_v);
3761 p.y = Int_val (y_v);
3762 pdf_set_annot_contents (state.ctx, annot, String_val (contents_v));
3763 pdf_set_text_annot_position (state.ctx, annot, p);
3764 state.dirty = 1;
3766 CAMLreturn0;
3769 CAMLprim void ml_delannot (value ptr_v, value n_v)
3771 CAMLparam2 (ptr_v, n_v);
3772 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3774 if (pdf) {
3775 struct page *page;
3776 char *s = String_val (ptr_v);
3777 struct slink *slink;
3779 page = parse_pointer (__func__, s);
3780 slink = &page->slinks[Int_val (n_v)];
3781 pdf_delete_annot (state.ctx,
3782 pdf_page_from_fz_page (state.ctx, page->fzpage),
3783 (pdf_annot *) slink->u.annot);
3784 state.dirty = 1;
3786 CAMLreturn0;
3789 CAMLprim void ml_modannot (value ptr_v, value n_v, value str_v)
3791 CAMLparam3 (ptr_v, n_v, str_v);
3792 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3794 if (pdf) {
3795 struct page *page;
3796 char *s = String_val (ptr_v);
3797 struct slink *slink;
3799 page = parse_pointer (__func__, s);
3800 slink = &page->slinks[Int_val (n_v)];
3801 pdf_set_annot_contents (state.ctx, (pdf_annot *) slink->u.annot,
3802 String_val (str_v));
3803 state.dirty = 1;
3805 CAMLreturn0;
3808 CAMLprim value ml_hasunsavedchanges (value unit_v)
3810 CAMLparam1 (unit_v);
3811 CAMLreturn (Val_bool (state.dirty));
3814 CAMLprim void ml_savedoc (value path_v)
3816 CAMLparam1 (path_v);
3817 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3819 if (pdf) {
3820 pdf_save_document (state.ctx, pdf, String_val (path_v), NULL);
3822 CAMLreturn0;
3825 static void makestippletex (void)
3827 const char pixels[] = "\xff\xff\0\0";
3828 glGenTextures (1, &state.stid);
3829 glBindTexture (GL_TEXTURE_1D, state.stid);
3830 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3831 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3832 glTexImage1D (
3833 GL_TEXTURE_1D,
3835 GL_ALPHA,
3838 GL_ALPHA,
3839 GL_UNSIGNED_BYTE,
3840 pixels
3844 CAMLprim value ml_fz_version (void)
3846 return caml_copy_string (FZ_VERSION);
3849 CAMLprim value ml_llpp_version (void)
3851 extern char llpp_version[];
3852 return caml_copy_string (llpp_version);
3855 CAMLprim void ml_init (value csock_v, value params_v)
3857 CAMLparam2 (csock_v, params_v);
3858 CAMLlocal2 (trim_v, fuzz_v);
3859 int ret;
3860 int texcount;
3861 char *fontpath;
3862 int colorspace;
3863 int mustoresize;
3865 state.csock = Int_val (csock_v);
3866 state.rotate = Int_val (Field (params_v, 0));
3867 state.fitmodel = Int_val (Field (params_v, 1));
3868 trim_v = Field (params_v, 2);
3869 texcount = Int_val (Field (params_v, 3));
3870 state.sliceheight = Int_val (Field (params_v, 4));
3871 mustoresize = Int_val (Field (params_v, 5));
3872 colorspace = Int_val (Field (params_v, 6));
3873 fontpath = String_val (Field (params_v, 7));
3875 #ifdef CIDER
3876 state.utf8cs = 1;
3877 #else
3878 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3879 if (setlocale (LC_CTYPE, "")) {
3880 const char *cset = nl_langinfo (CODESET);
3881 state.utf8cs = !strcmp (cset, "UTF-8");
3883 else {
3884 printd ("emsg setlocale: %d:%s", errno, strerror (errno));
3886 #endif
3888 if (caml_string_length (Field (params_v, 8)) > 0) {
3889 state.trimcachepath = ystrdup (String_val (Field (params_v, 8)));
3891 if (!state.trimcachepath) {
3892 printd ("emsg failed to strdup trimcachepath: %d:%s",
3893 errno, strerror (errno));
3897 state.ctx = fz_new_context (NULL, NULL, mustoresize);
3898 fz_register_document_handlers (state.ctx);
3900 state.trimmargins = Bool_val (Field (trim_v, 0));
3901 fuzz_v = Field (trim_v, 1);
3902 state.trimfuzz.x0 = Int_val (Field (fuzz_v, 0));
3903 state.trimfuzz.y0 = Int_val (Field (fuzz_v, 1));
3904 state.trimfuzz.x1 = Int_val (Field (fuzz_v, 2));
3905 state.trimfuzz.y1 = Int_val (Field (fuzz_v, 3));
3907 set_tex_params (colorspace);
3909 if (*fontpath) {
3910 state.face = load_font (fontpath);
3912 else {
3913 int len;
3914 const unsigned char *data;
3916 data = pdf_lookup_substitute_font (state.ctx, 0, 0, 0, 0, &len);
3917 state.face = load_builtin_font (data, len);
3919 if (!state.face) _exit (1);
3921 realloctexts (texcount);
3922 setuppbo ();
3923 makestippletex ();
3925 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
3926 if (ret) {
3927 errx (1, "pthread_create: %s", strerror (ret));
3930 CAMLreturn0;
3933 #if FIXME || !FIXME
3934 static void UNUSED_ATTR NO_OPTIMIZE_ATTR refmacs (void) {}
3935 #endif