Don't confuse imenu
[llpp.git] / link.c
blob58958f90ac3cde045e95de3478e3a38c6ca24ebe
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 "-Wdouble-promotion"
11 #endif
13 extern char **environ;
15 #include <errno.h>
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <signal.h>
22 #include <math.h>
23 #include <wchar.h>
24 #include <locale.h>
25 #include <langinfo.h>
27 #include <unistd.h>
28 #include <pthread.h>
29 #include <sys/uio.h>
30 #include <sys/time.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/utsname.h>
37 #include <spawn.h>
39 #include <regex.h>
40 #include <stdarg.h>
41 #include <limits.h>
42 #include <inttypes.h>
44 #ifdef CIDER
45 #include <OpenGL/gl.h>
46 #else
47 #include <GL/gl.h>
48 #endif
50 #pragma GCC diagnostic push
51 #ifdef __clang__
52 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
53 #endif
54 #pragma GCC diagnostic ignored "-Wpedantic"
55 #define CAML_NAME_SPACE
56 #include <caml/fail.h>
57 #include <caml/alloc.h>
58 #include <caml/memory.h>
59 #include <caml/unixsupport.h>
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wfloat-equal"
63 #include <mupdf/fitz.h>
64 #include <mupdf/pdf.h>
65 #pragma GCC diagnostic pop
67 #include <ft2build.h>
68 #include FT_FREETYPE_H
69 #pragma GCC diagnostic pop
71 #include "cutils.h"
73 #ifdef USE_NPOT
74 #define TEXT_TYPE GL_TEXTURE_2D
75 #else
76 #define TEXT_TYPE GL_TEXTURE_RECTANGLE_ARB
77 #endif
79 #if 0
80 #define lprintf printf
81 #else
82 #define lprintf(...)
83 #endif
85 #define ARSERT(cond) for (;;) { \
86 if (!(cond)) { \
87 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
88 } \
89 break; \
92 struct slice {
93 int h;
94 int texindex;
97 struct tile {
98 int w, h;
99 int slicecount;
100 int sliceheight;
101 struct bo *pbo;
102 fz_pixmap *pixmap;
103 struct slice slices[1];
106 struct pagedim {
107 int pageno;
108 int rotate;
109 int left;
110 int tctmready;
111 fz_irect bounds;
112 fz_rect pagebox;
113 fz_rect mediabox;
114 fz_matrix ctm, zoomctm, tctm;
117 struct slink {
118 enum { SLINK, SANNOT } tag;
119 fz_irect bbox;
120 union {
121 fz_link *link;
122 fz_annot *annot;
123 } u;
126 struct annot {
127 fz_irect bbox;
128 fz_annot *annot;
131 struct page {
132 int tgen;
133 int sgen;
134 int agen;
135 int pageno;
136 int pdimno;
137 fz_stext_page *text;
138 fz_page *fzpage;
139 fz_display_list *dlist;
140 fz_link *links;
141 int slinkcount;
142 struct slink *slinks;
143 int annotcount;
144 struct annot *annots;
145 fz_stext_char *fmark, *lmark;
148 enum { FitWidth, FitProportional, FitPage };
150 static struct {
151 int sliceheight;
152 struct pagedim *pagedims;
153 int pagecount;
154 int pagedimcount;
155 fz_document *doc;
156 fz_context *ctx;
157 int w, h;
159 int texindex;
160 int texcount;
161 GLuint *texids;
163 GLenum texiform;
164 GLenum texform;
165 GLenum texty;
167 fz_colorspace *colorspace;
169 struct {
170 int w, h;
171 struct slice *slice;
172 } *texowners;
174 int rotate;
175 int fitmodel;
176 int trimmargins;
177 int needoutline;
178 int gen;
179 int aalevel;
181 int trimanew;
182 fz_irect trimfuzz;
183 fz_pixmap *pig;
185 pthread_t thread;
186 int csock;
187 FT_Face face;
189 char *trimcachepath;
190 int dirty;
192 GLuint stid;
194 int bo_usable;
195 GLuint boid;
197 void (*glBindBufferARB) (GLenum, GLuint);
198 GLboolean (*glUnmapBufferARB) (GLenum);
199 void *(*glMapBufferARB) (GLenum, GLenum);
200 void (*glBufferDataARB) (GLenum, GLsizei, void *, GLenum);
201 void (*glGenBuffersARB) (GLsizei, GLuint *);
202 void (*glDeleteBuffersARB) (GLsizei, GLuint *);
204 GLfloat texcoords[8];
205 GLfloat vertices[16];
207 #ifdef CACHE_PAGEREFS
208 struct {
209 int idx;
210 int count;
211 pdf_obj **objs;
212 pdf_document *pdf;
213 } pdflut;
214 #endif
215 int utf8cs;
216 } state;
218 struct bo {
219 GLuint id;
220 void *ptr;
221 size_t size;
224 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
226 static void lock (const char *cap)
228 int ret = pthread_mutex_lock (&mutex);
229 if (ret) {
230 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
234 static void unlock (const char *cap)
236 int ret = pthread_mutex_unlock (&mutex);
237 if (ret) {
238 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
242 static int trylock (const char *cap)
244 int ret = pthread_mutex_trylock (&mutex);
245 if (ret && ret != EBUSY) {
246 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
248 return ret == EBUSY;
251 static int hasdata (void)
253 int ret, avail;
254 ret = ioctl (state.csock, FIONREAD, &avail);
255 if (ret) err (1, "hasdata: FIONREAD error ret=%d", ret);
256 return avail > 0;
259 value ml_hasdata (value fd_v);
260 value ml_hasdata (value fd_v)
262 CAMLparam1 (fd_v);
263 int ret, avail;
265 ret = ioctl (Int_val (fd_v), FIONREAD, &avail);
266 if (ret) uerror ("ioctl (FIONREAD)", Nothing);
267 CAMLreturn (Val_bool (avail > 0));
270 static void readdata (int fd, void *p, int size)
272 ssize_t n;
274 again:
275 n = read (fd, p, size);
276 if (n - size) {
277 if (n < 0 && errno == EINTR) goto again;
278 if (!n) errx (1, "EOF while reading");
279 errx (1, "read (fd %d, req %d, ret %zd)", fd, size, n);
283 static void writedata (int fd, char *p, int size)
285 ssize_t n;
286 uint32_t size4 = size;
287 struct iovec iov[2] = {
288 { .iov_base = &size4, .iov_len = 4 },
289 { .iov_base = p, .iov_len = size }
292 again:
293 n = writev (fd, iov, 2);
294 if (n < 0 && errno == EINTR) goto again;
295 if (n - size - 4) {
296 if (!n) errx (1, "EOF while writing data");
297 err (1, "writev (fd %d, req %d, ret %zd)", fd, size + 4, n);
301 static int readlen (int fd)
303 uint32_t u;
304 readdata (fd, &u, 4);
305 return u;
308 void ml_wcmd (value fd_v, value bytes_v, value len_v);
309 void ml_wcmd (value fd_v, value bytes_v, value len_v)
311 CAMLparam3 (fd_v, bytes_v, len_v);
312 writedata (Int_val (fd_v), &Byte (bytes_v, 0), Int_val (len_v));
313 CAMLreturn0;
316 value ml_rcmd (value fd_v);
317 value ml_rcmd (value fd_v)
319 CAMLparam1 (fd_v);
320 CAMLlocal1 (strdata_v);
321 int fd = Int_val (fd_v);
322 int len = readlen (fd);
323 strdata_v = caml_alloc_string (len);
324 readdata (fd, String_val (strdata_v), len);
325 CAMLreturn (strdata_v);
328 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt, ...)
330 char fbuf[64];
331 int size = sizeof (fbuf), len;
332 va_list ap;
333 char *buf = fbuf;
335 for (;;) {
336 va_start (ap, fmt);
337 len = vsnprintf (buf, size, fmt, ap);
338 va_end (ap);
340 if (len > -1) {
341 if (len < size - 4) {
342 writedata (state.csock, buf, len);
343 break;
345 else size = len + 5;
347 else {
348 err (1, "vsnprintf for `%s' failed", fmt);
350 buf = realloc (buf == fbuf ? NULL : buf, size);
351 if (!buf) err (1, "realloc for temp buf (%d bytes) failed", size);
353 if (buf != fbuf) free (buf);
356 static void closedoc (void)
358 #ifdef CACHE_PAGEREFS
359 if (state.pdflut.objs) {
360 for (int i = 0; i < state.pdflut.count; ++i) {
361 pdf_drop_obj (state.ctx, state.pdflut.objs[i]);
363 free (state.pdflut.objs);
364 state.pdflut.objs = NULL;
365 state.pdflut.idx = 0;
367 #endif
368 if (state.doc) {
369 fz_drop_document (state.ctx, state.doc);
370 state.doc = NULL;
374 static int openxref (char *filename, char *password, int layouth)
376 for (int i = 0; i < state.texcount; ++i) {
377 state.texowners[i].w = -1;
378 state.texowners[i].slice = NULL;
381 closedoc ();
383 state.dirty = 0;
384 if (state.pagedims) {
385 free (state.pagedims);
386 state.pagedims = NULL;
388 state.pagedimcount = 0;
390 fz_set_aa_level (state.ctx, state.aalevel);
391 state.doc = fz_open_document (state.ctx, filename);
392 if (fz_needs_password (state.ctx, state.doc)) {
393 if (password && !*password) {
394 printd ("pass");
395 return 0;
397 else {
398 int ok = fz_authenticate_password (state.ctx, state.doc, password);
399 if (!ok) {
400 printd ("pass fail");
401 return 0;
405 if (layouth >= 0)
406 fz_layout_document (state.ctx, state.doc, 460, layouth, 12);
407 state.pagecount = fz_count_pages (state.ctx, state.doc);
408 return 1;
411 static void docinfo (void)
413 struct { char *tag; char *name; } metatbl[] = {
414 { FZ_META_INFO_TITLE, "Title" },
415 { FZ_META_INFO_AUTHOR, "Author" },
416 { FZ_META_FORMAT, "Format" },
417 { FZ_META_ENCRYPTION, "Encryption" },
418 { "info:Creator", "Creator" },
419 { "info:Producer", "Producer" },
420 { "info:CreationDate", "Creation date" },
422 int len = 0;
423 char *buf = NULL;
425 for (size_t i = 0; i < sizeof (metatbl) / sizeof (metatbl[1]); ++i) {
426 int need;
427 again:
428 need = fz_lookup_metadata (state.ctx, state.doc,
429 metatbl[i].tag, buf, len);
430 if (need > 0) {
431 if (need <= len) {
432 printd ("info %s\t%s", metatbl[i].name, buf);
434 else {
435 buf = realloc (buf, need + 1);
436 if (!buf) err (1, "docinfo realloc %d", need + 1);
437 len = need + 1;
438 goto again;
442 free (buf);
444 printd ("infoend");
447 static void unlinktile (struct tile *tile)
449 for (int i = 0; i < tile->slicecount; ++i) {
450 struct slice *s = &tile->slices[i];
452 if (s->texindex != -1) {
453 if (state.texowners[s->texindex].slice == s) {
454 state.texowners[s->texindex].slice = NULL;
460 static void freepage (struct page *page)
462 if (!page) return;
463 if (page->text) {
464 fz_drop_stext_page (state.ctx, page->text);
466 if (page->slinks) {
467 free (page->slinks);
469 fz_drop_display_list (state.ctx, page->dlist);
470 fz_drop_page (state.ctx, page->fzpage);
471 free (page);
474 static void freetile (struct tile *tile)
476 unlinktile (tile);
477 if (!tile->pbo) {
478 #if 0
479 fz_drop_pixmap (state.ctx, tile->pixmap);
480 #else /* piggyback */
481 if (state.pig) {
482 fz_drop_pixmap (state.ctx, state.pig);
484 state.pig = tile->pixmap;
485 #endif
487 else {
488 free (tile->pbo);
489 fz_drop_pixmap (state.ctx, tile->pixmap);
491 free (tile);
494 static void trimctm (pdf_page *page, int pindex)
496 struct pagedim *pdim = &state.pagedims[pindex];
498 if (!page) return;
499 if (!pdim->tctmready) {
500 fz_rect realbox, mediabox;
501 fz_matrix page_ctm, ctm;
503 ctm = fz_concat (fz_rotate (-pdim->rotate), fz_scale (1, -1));
504 realbox = fz_transform_rect (pdim->mediabox, ctm);
505 pdf_page_transform (state.ctx, page, &mediabox, &page_ctm);
506 pdim->tctm = fz_concat (
507 fz_invert_matrix (page_ctm),
508 fz_concat (ctm, fz_translate (-realbox.x0, -realbox.y0)));
509 pdim->tctmready = 1;
513 static fz_matrix pagectm1 (fz_page *fzpage, struct pagedim *pdim)
515 fz_matrix ctm;
516 ptrdiff_t pdimno = pdim - state.pagedims;
518 ARSERT (pdim - state.pagedims < INT_MAX);
519 if (pdf_specifics (state.ctx, state.doc)) {
520 trimctm (pdf_page_from_fz_page (state.ctx, fzpage), (int) pdimno);
521 ctm = fz_concat (pdim->tctm, pdim->ctm);
523 else {
524 ctm = fz_concat (fz_translate (-pdim->mediabox.x0, -pdim->mediabox.y0),
525 pdim->ctm);
527 return ctm;
530 static fz_matrix pagectm (struct page *page)
532 return pagectm1 (page->fzpage, &state.pagedims[page->pdimno]);
535 static void *loadpage (int pageno, int pindex)
537 fz_device *dev;
538 struct page *page;
540 page = calloc (sizeof (struct page), 1);
541 if (!page) {
542 err (1, "calloc page %d", pageno);
545 page->dlist = fz_new_display_list (state.ctx, fz_infinite_rect);
546 dev = fz_new_list_device (state.ctx, page->dlist);
547 fz_try (state.ctx) {
548 page->fzpage = fz_load_page (state.ctx, state.doc, pageno);
549 fz_run_page (state.ctx, page->fzpage, dev, fz_identity, NULL);
551 fz_catch (state.ctx) {
552 page->fzpage = NULL;
554 fz_close_device (state.ctx, dev);
555 fz_drop_device (state.ctx, dev);
557 page->pdimno = pindex;
558 page->pageno = pageno;
559 page->sgen = state.gen;
560 page->agen = state.gen;
561 page->tgen = state.gen;
562 return page;
565 static struct tile *alloctile (int h)
567 int slicecount;
568 size_t tilesize;
569 struct tile *tile;
571 slicecount = (h + state.sliceheight - 1) / state.sliceheight;
572 tilesize = sizeof (*tile) + ((slicecount - 1) * sizeof (struct slice));
573 tile = calloc (tilesize, 1);
574 if (!tile) {
575 err (1, "cannot allocate tile (%zu bytes)", tilesize);
577 for (int i = 0; i < slicecount; ++i) {
578 int sh = fz_mini (h, state.sliceheight);
579 tile->slices[i].h = sh;
580 tile->slices[i].texindex = -1;
581 h -= sh;
583 tile->slicecount = slicecount;
584 tile->sliceheight = state.sliceheight;
585 return tile;
588 static struct tile *rendertile (struct page *page, int x, int y, int w, int h,
589 struct bo *pbo)
591 fz_irect bbox;
592 fz_matrix ctm;
593 fz_device *dev;
594 struct tile *tile;
595 struct pagedim *pdim;
597 tile = alloctile (h);
598 pdim = &state.pagedims[page->pdimno];
600 bbox = pdim->bounds;
601 bbox.x0 += x;
602 bbox.y0 += y;
603 bbox.x1 = bbox.x0 + w;
604 bbox.y1 = bbox.y0 + h;
606 if (state.pig) {
607 if (state.pig->w == w
608 && state.pig->h == h
609 && state.pig->colorspace == state.colorspace) {
610 tile->pixmap = state.pig;
611 tile->pixmap->x = bbox.x0;
612 tile->pixmap->y = bbox.y0;
614 else {
615 fz_drop_pixmap (state.ctx, state.pig);
617 state.pig = NULL;
619 if (!tile->pixmap) {
620 if (pbo) {
621 tile->pixmap =
622 fz_new_pixmap_with_bbox_and_data (state.ctx, state.colorspace,
623 bbox, NULL, 1, pbo->ptr);
624 tile->pbo = pbo;
626 else {
627 tile->pixmap =
628 fz_new_pixmap_with_bbox (state.ctx, state.colorspace, bbox,
629 NULL, 1);
633 tile->w = w;
634 tile->h = h;
635 fz_clear_pixmap_with_value (state.ctx, tile->pixmap, 0xff);
637 dev = fz_new_draw_device (state.ctx, fz_identity, tile->pixmap);
638 ctm = pagectm (page);
639 fz_run_display_list (state.ctx, page->dlist, dev, ctm,
640 fz_rect_from_irect (bbox), NULL);
641 fz_close_device (state.ctx, dev);
642 fz_drop_device (state.ctx, dev);
644 return tile;
647 #ifdef CACHE_PAGEREFS
648 /* modified mupdf/source/pdf/pdf-page.c:pdf_lookup_page_loc_imp
649 thanks to Robin Watts */
650 static void
651 pdf_collect_pages(pdf_document *doc, pdf_obj *node)
653 fz_context *ctx = state.ctx; /* doc->ctx; */
654 pdf_obj *kids;
655 int len;
657 if (state.pdflut.idx == state.pagecount) return;
659 kids = pdf_dict_gets (ctx, node, "Kids");
660 len = pdf_array_len (ctx, kids);
662 if (len == 0)
663 fz_throw (ctx, FZ_ERROR_GENERIC, "malformed pages tree");
665 if (pdf_mark_obj (ctx, node))
666 fz_throw (ctx, FZ_ERROR_GENERIC, "cycle in page tree");
667 for (int i = 0; i < len; i++) {
668 pdf_obj *kid = pdf_array_get (ctx, kids, i);
669 const char *type = pdf_to_name (ctx, pdf_dict_gets (ctx, kid, "Type"));
670 if (*type
671 ? !strcmp (type, "Pages")
672 : pdf_dict_gets (ctx, kid, "Kids")
673 && !pdf_dict_gets (ctx, kid, "MediaBox")) {
674 pdf_collect_pages (doc, kid);
676 else {
677 if (*type
678 ? strcmp (type, "Page") != 0
679 : !pdf_dict_gets (ctx, kid, "MediaBox"))
680 fz_warn (ctx, "non-page object in page tree (%s)", type);
681 state.pdflut.objs[state.pdflut.idx++] = pdf_keep_obj (ctx, kid);
684 pdf_unmark_obj (ctx, node);
687 static void
688 pdf_load_page_objs (pdf_document *doc)
690 pdf_obj *root = pdf_dict_gets (state.ctx,
691 pdf_trailer (state.ctx, doc), "Root");
692 pdf_obj *node = pdf_dict_gets (state.ctx, root, "Pages");
694 if (!node)
695 fz_throw (state.ctx, FZ_ERROR_GENERIC, "cannot find page tree");
697 state.pdflut.idx = 0;
698 pdf_collect_pages (doc, node);
700 #endif
702 static void initpdims (void)
704 double start, end;
705 FILE *trimf = NULL;
706 fz_rect rootmediabox = fz_empty_rect;
707 int pageno, trim, show;
708 int trimw = 0, cxcount;
709 fz_context *ctx = state.ctx;
710 pdf_document *pdf = pdf_specifics (ctx, state.doc);
712 fz_var (trimw);
713 fz_var (trimf);
714 fz_var (cxcount);
715 start = now ();
717 if (state.trimmargins && state.trimcachepath) {
718 trimf = fopen (state.trimcachepath, "rb");
719 if (!trimf) {
720 trimf = fopen (state.trimcachepath, "wb");
721 trimw = 1;
725 if (state.trimmargins || pdf)
726 cxcount = state.pagecount;
727 else
728 cxcount = fz_mini (state.pagecount, 1);
730 if (pdf) {
731 pdf_obj *obj;
732 obj = pdf_dict_getp (ctx, pdf_trailer (ctx, pdf),
733 "Root/Pages/MediaBox");
734 rootmediabox = pdf_to_rect (ctx, obj);
737 #ifdef CACHE_PAGEREFS
738 if (pdf && (!state.pdflut.objs || state.pdflut.pdf != pdf)) {
739 state.pdflut.objs = calloc (sizeof (*state.pdflut.objs), cxcount);
740 if (!state.pdflut.objs) {
741 err (1, "malloc pageobjs %zu %d %zu failed",
742 sizeof (*state.pdflut.objs), cxcount,
743 sizeof (*state.pdflut.objs) * cxcount);
745 state.pdflut.count = cxcount;
746 pdf_load_page_objs (pdf);
747 state.pdflut.pdf = pdf;
749 #endif
751 for (pageno = 0; pageno < cxcount; ++pageno) {
752 int rotate = 0;
753 struct pagedim *p;
754 fz_rect mediabox = fz_empty_rect;
756 fz_var (rotate);
757 if (pdf) {
758 pdf_obj *pageref, *pageobj;
760 #ifdef CACHE_PAGEREFS
761 pageref = state.pdflut.objs[pageno];
762 #else
763 pageref = pdf_lookup_page_obj (ctx, pdf, pageno);
764 #endif
765 pageobj = pdf_resolve_indirect (ctx, pageref);
766 rotate = pdf_to_int (ctx, pdf_dict_gets (ctx, pageobj, "Rotate"));
768 if (state.trimmargins) {
769 pdf_obj *obj;
770 pdf_page *page;
772 fz_try (ctx) {
773 page = pdf_load_page (ctx, pdf, pageno);
774 obj = pdf_dict_gets (ctx, pageobj, "llpp.TrimBox");
775 trim = state.trimanew || !obj;
776 if (trim) {
777 fz_rect rect;
778 fz_device *dev;
779 fz_matrix ctm, page_ctm;
781 dev = fz_new_bbox_device (ctx, &rect);
782 pdf_page_transform (ctx, page, &mediabox, &page_ctm);
783 ctm = fz_invert_matrix (page_ctm);
784 pdf_run_page (ctx, page, dev, fz_identity, NULL);
785 fz_close_device (ctx, dev);
786 fz_drop_device (ctx, dev);
788 rect.x0 += state.trimfuzz.x0;
789 rect.x1 += state.trimfuzz.x1;
790 rect.y0 += state.trimfuzz.y0;
791 rect.y1 += state.trimfuzz.y1;
792 rect = fz_transform_rect (rect, ctm);
793 rect = fz_intersect_rect (rect, mediabox);
795 if (!fz_is_empty_rect (rect)) {
796 mediabox = rect;
799 obj = pdf_new_array (ctx, pdf, 4);
800 pdf_array_push (ctx, obj,
801 pdf_new_real (ctx, mediabox.x0));
802 pdf_array_push (ctx, obj,
803 pdf_new_real (ctx, mediabox.y0));
804 pdf_array_push (ctx, obj,
805 pdf_new_real (ctx, mediabox.x1));
806 pdf_array_push (ctx, obj,
807 pdf_new_real (ctx, mediabox.y1));
808 pdf_dict_puts (ctx, pageobj, "llpp.TrimBox", obj);
810 else {
811 mediabox.x0 = pdf_to_real (ctx,
812 pdf_array_get (ctx, obj, 0));
813 mediabox.y0 = pdf_to_real (ctx,
814 pdf_array_get (ctx, obj, 1));
815 mediabox.x1 = pdf_to_real (ctx,
816 pdf_array_get (ctx, obj, 2));
817 mediabox.y1 = pdf_to_real (ctx,
818 pdf_array_get (ctx, obj, 3));
821 fz_drop_page (ctx, &page->super);
822 show = trim ? pageno % 5 == 0 : pageno % 20 == 0;
823 if (show) {
824 printd ("progress %f Trimming %d",
825 (double) (pageno + 1) / state.pagecount,
826 pageno + 1);
829 fz_catch (ctx) {
830 printd ("emsg failed to load page %d", pageno);
833 else {
834 int empty = 0;
835 fz_rect cropbox;
837 mediabox =
838 pdf_to_rect (ctx, pdf_dict_gets (ctx, pageobj, "MediaBox"));
839 if (fz_is_empty_rect (mediabox)) {
840 mediabox.x0 = 0;
841 mediabox.y0 = 0;
842 mediabox.x1 = 612;
843 mediabox.y1 = 792;
844 empty = 1;
847 cropbox =
848 pdf_to_rect (ctx, pdf_dict_gets (ctx, pageobj, "CropBox"));
849 if (!fz_is_empty_rect (cropbox)) {
850 if (empty) {
851 mediabox = cropbox;
853 else {
854 mediabox = fz_intersect_rect (mediabox, cropbox);
857 else {
858 if (empty) {
859 if (fz_is_empty_rect (rootmediabox)) {
860 printd ("emsg cannot find page size for page %d",
861 pageno);
863 else {
864 mediabox = rootmediabox;
870 else {
871 if (state.trimmargins && trimw) {
872 fz_page *page;
874 fz_try (ctx) {
875 page = fz_load_page (ctx, state.doc, pageno);
876 mediabox = fz_bound_page (ctx, page);
877 if (state.trimmargins) {
878 fz_rect rect;
879 fz_device *dev;
881 dev = fz_new_bbox_device (ctx, &rect);
882 fz_run_page (ctx, page, dev, fz_identity, NULL);
883 fz_close_device (ctx, dev);
884 fz_drop_device (ctx, dev);
886 rect.x0 += state.trimfuzz.x0;
887 rect.x1 += state.trimfuzz.x1;
888 rect.y0 += state.trimfuzz.y0;
889 rect.y1 += state.trimfuzz.y1;
890 rect = fz_intersect_rect (rect, mediabox);
892 if (!fz_is_empty_rect (rect)) {
893 mediabox = rect;
896 fz_drop_page (ctx, page);
898 fz_catch (ctx) {
900 if (trimf) {
901 size_t n = fwrite (&mediabox, sizeof (mediabox), 1, trimf);
902 if (n - 1) {
903 err (1, "fwrite trim mediabox");
907 else {
908 if (trimf) {
909 size_t n = fread (&mediabox, sizeof (mediabox), 1, trimf);
910 if (n - 1) {
911 err (1, "fread trim mediabox %d", pageno);
914 else {
915 fz_page *page;
916 fz_try (ctx) {
917 page = fz_load_page (ctx, state.doc, pageno);
918 mediabox = fz_bound_page (ctx, page);
919 fz_drop_page (ctx, page);
921 show = !state.trimmargins && pageno % 20 == 0;
922 if (show) {
923 printd ("progress %f Gathering dimensions %d",
924 (double) (pageno) / state.pagecount,
925 pageno);
928 fz_catch (ctx) {
929 printd ("emsg failed to load page %d", pageno);
935 if (state.pagedimcount == 0
936 || ((void) (p = &state.pagedims[state.pagedimcount-1])
937 , p->rotate != rotate)
938 || memcmp (&p->mediabox, &mediabox, sizeof (mediabox))) {
939 size_t size;
941 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
942 state.pagedims = realloc (state.pagedims, size);
943 if (!state.pagedims) {
944 err (1, "realloc pagedims to %zu (%d elems)",
945 size, state.pagedimcount + 1);
948 p = &state.pagedims[state.pagedimcount++];
949 p->rotate = rotate;
950 p->mediabox = mediabox;
951 p->pageno = pageno;
954 end = now ();
955 printd ("progress 1 %s %d pages in %f seconds",
956 state.trimmargins ? "Trimmed" : "Processed",
957 state.pagecount, end - start);
958 state.trimanew = 0;
959 if (trimf) {
960 if (fclose (trimf)) {
961 err (1, "fclose");
966 static void layout (void)
968 int pindex;
969 fz_rect box;
970 fz_matrix ctm;
971 struct pagedim *p = NULL;
972 float zw, w, maxw = 0.0, zoom = 1.0;
974 if (state.pagedimcount == 0) return;
976 switch (state.fitmodel) {
977 case FitProportional:
978 for (pindex = 0; pindex < state.pagedimcount; ++pindex) {
979 float x0, x1;
981 p = &state.pagedims[pindex];
982 box = fz_transform_rect (p->mediabox,
983 fz_rotate (p->rotate + state.rotate));
985 x0 = fz_min (box.x0, box.x1);
986 x1 = fz_max (box.x0, box.x1);
988 w = x1 - x0;
989 maxw = fz_max (w, maxw);
990 zoom = state.w / maxw;
992 break;
994 case FitPage:
995 maxw = state.w;
996 break;
998 case FitWidth:
999 break;
1001 default:
1002 ARSERT (0 && state.fitmodel);
1005 for (pindex = 0; pindex < state.pagedimcount; ++pindex) {
1006 p = &state.pagedims[pindex];
1007 ctm = fz_rotate (state.rotate);
1008 box = fz_transform_rect (p->mediabox,
1009 fz_rotate (p->rotate + state.rotate));
1010 w = box.x1 - box.x0;
1011 switch (state.fitmodel) {
1012 case FitProportional:
1013 p->left = (int) (((maxw - w) * zoom) / 2.f);
1014 break;
1015 case FitPage:
1017 float zh, h;
1018 zw = maxw / w;
1019 h = box.y1 - box.y0;
1020 zh = state.h / h;
1021 zoom = fz_min (zw, zh);
1022 p->left = (int) ((maxw - (w * zoom)) / 2.f);
1024 break;
1025 case FitWidth:
1026 p->left = 0;
1027 zoom = state.w / w;
1028 break;
1031 p->zoomctm = fz_scale (zoom, zoom);
1032 ctm = fz_concat (p->zoomctm, ctm);
1034 p->pagebox = p->mediabox;
1035 p->pagebox = fz_transform_rect (p->pagebox, fz_rotate (p->rotate));
1036 p->pagebox.x1 -= p->pagebox.x0;
1037 p->pagebox.y1 -= p->pagebox.y0;
1038 p->pagebox.x0 = 0;
1039 p->pagebox.y0 = 0;
1040 p->bounds = fz_round_rect (fz_transform_rect (p->pagebox, ctm));
1041 p->ctm = ctm;
1043 ctm = fz_concat (fz_translate (0, -p->mediabox.y1),
1044 fz_scale (zoom, -zoom));
1045 p->tctmready = 0;
1048 do {
1049 int x0 = fz_mini (p->bounds.x0, p->bounds.x1);
1050 int y0 = fz_mini (p->bounds.y0, p->bounds.y1);
1051 int x1 = fz_maxi (p->bounds.x0, p->bounds.x1);
1052 int y1 = fz_maxi (p->bounds.y0, p->bounds.y1);
1053 int boundw = x1 - x0;
1054 int boundh = y1 - y0;
1056 printd ("pdim %d %d %d %d", p->pageno, boundw, boundh, p->left);
1057 } while (p-- != state.pagedims);
1060 static struct pagedim *pdimofpageno (int pageno)
1062 struct pagedim *pdim = state.pagedims;
1064 for (int i = 0; i < state.pagedimcount; ++i) {
1065 if (state.pagedims[i].pageno > pageno)
1066 break;
1067 pdim = &state.pagedims[i];
1069 return pdim;
1072 static void recurse_outline (fz_outline *outline, int level)
1074 while (outline) {
1075 if (outline->page >= 0) {
1076 fz_point p = {.x = outline->x, .y = outline->y};
1077 struct pagedim *pdim = pdimofpageno (outline->page);
1078 int h = fz_maxi (fz_absi (pdim->bounds.y1 - pdim->bounds.y0), 0);
1079 p = fz_transform_point (p, pdim->ctm);
1080 printd ("o %d %d %d %d %s",
1081 level, outline->page, (int) p.y, h, outline->title);
1083 else {
1084 printd ("on %d %s", level, outline->title);
1086 if (outline->down) {
1087 recurse_outline (outline->down, level + 1);
1089 outline = outline->next;
1093 static void process_outline (void)
1095 fz_outline *outline;
1097 if (!state.needoutline || !state.pagedimcount) return;
1099 state.needoutline = 0;
1100 outline = fz_load_outline (state.ctx, state.doc);
1101 if (outline) {
1102 recurse_outline (outline, 0);
1103 fz_drop_outline (state.ctx, outline);
1107 static char *strofline (fz_stext_line *line)
1109 char *p;
1110 char utf8[10];
1111 fz_stext_char *ch;
1112 size_t size = 0, cap = 80;
1114 p = malloc (cap + 1);
1115 if (!p) return NULL;
1117 for (ch = line->first_char; ch; ch = ch->next) {
1118 int n = fz_runetochar (utf8, ch->c);
1119 if (size + n > cap) {
1120 cap *= 2;
1121 p = realloc (p, cap + 1);
1122 if (!p) return NULL;
1125 memcpy (p + size, utf8, n);
1126 size += n;
1128 p[size] = 0;
1129 return p;
1132 static int matchline (regex_t *re, fz_stext_line *line,
1133 int stop, int pageno, double start)
1135 int ret;
1136 char *p;
1137 regmatch_t rm;
1139 p = strofline (line);
1140 if (!p) return -1;
1142 ret = regexec (re, p, 1, &rm, 0);
1143 if (ret) {
1144 free (p);
1145 if (ret != REG_NOMATCH) {
1146 size_t size;
1147 char errbuf[80];
1148 size = regerror (ret, re, errbuf, sizeof (errbuf));
1149 printd ("msg regexec error `%.*s'",
1150 (int) size, errbuf);
1151 return -1;
1153 return 0;
1155 else {
1156 fz_quad s, e;
1157 fz_stext_char *ch;
1158 int o = 0;
1160 for (ch = line->first_char; ch; ch = ch->next) {
1161 o += fz_runelen (ch->c);
1162 if (o > rm.rm_so) {
1163 s = ch->quad;
1164 break;
1167 for (;ch; ch = ch->next) {
1168 o += fz_runelen (ch->c);
1169 if (o > rm.rm_eo) break;
1171 e = ch->quad;
1173 if (!stop) {
1174 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1175 pageno, 1,
1176 s.ul.x, s.ul.y,
1177 e.ur.x, s.ul.y,
1178 e.lr.x, e.lr.y,
1179 s.ul.x, e.lr.y);
1181 printd ("progress 1 found at %d `%.*s' in %f sec",
1182 pageno + 1, (int) (rm.rm_eo - rm.rm_so), &p[rm.rm_so],
1183 now () - start);
1185 else {
1186 printd ("match %d %d %f %f %f %f %f %f %f %f",
1187 pageno, 2,
1188 s.ul.x, s.ul.y,
1189 e.ur.x, s.ul.y,
1190 e.lr.x, e.lr.y,
1191 s.ul.x, e.lr.y);
1193 free (p);
1194 return 1;
1198 /* wishful thinking function */
1199 static void search (regex_t *re, int pageno, int y, int forward)
1201 fz_device *tdev;
1202 fz_stext_page *text;
1203 struct pagedim *pdim;
1204 int stop = 0, niters = 0;
1205 double start, end;
1206 fz_page *page;
1207 fz_stext_block *block;
1209 start = now ();
1210 while (pageno >= 0 && pageno < state.pagecount && !stop) {
1211 if (niters++ == 5) {
1212 niters = 0;
1213 if (hasdata ()) {
1214 printd ("progress 1 attention requested aborting search at %d",
1215 pageno);
1216 stop = 1;
1218 else {
1219 printd ("progress %f searching in page %d",
1220 (double) (pageno + 1) / state.pagecount,
1221 pageno);
1224 pdim = pdimofpageno (pageno);
1225 text = fz_new_stext_page (state.ctx, pdim->mediabox);
1226 tdev = fz_new_stext_device (state.ctx, text, 0);
1228 page = fz_load_page (state.ctx, state.doc, pageno);
1229 fz_run_page (state.ctx, page, tdev, pagectm1 (page, pdim), NULL);
1231 fz_close_device (state.ctx, tdev);
1232 fz_drop_device (state.ctx, tdev);
1234 if (forward) {
1235 for (block = text->first_block; block; block = block->next) {
1236 fz_stext_line *line;
1238 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1239 for (line = block->u.t.first_line; line; line = line->next) {
1240 if (line->bbox.y0 < y + 1) continue;
1242 switch (matchline (re, line, stop, pageno, start)) {
1243 case 0: break;
1244 case 1: stop = 1; break;
1245 case -1: stop = 1; goto endloop;
1250 else {
1251 for (block = text->last_block; block; block = block->prev) {
1252 fz_stext_line *line;
1254 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1255 for (line = block->u.t.last_line; line; line = line->prev) {
1256 if (line->bbox.y0 < y + 1) continue;
1258 switch (matchline (re, line, stop, pageno, start)) {
1259 case 0: break;
1260 case 1: stop = 1; break;
1261 case -1: stop = 1; goto endloop;
1267 if (forward) {
1268 pageno += 1;
1269 y = 0;
1271 else {
1272 pageno -= 1;
1273 y = INT_MAX;
1275 endloop:
1276 fz_drop_stext_page (state.ctx, text);
1277 fz_drop_page (state.ctx, page);
1279 end = now ();
1280 if (!stop) {
1281 printd ("progress 1 no matches %f sec", end - start);
1283 printd ("clearrects");
1286 static void set_tex_params (int colorspace)
1288 switch (colorspace) {
1289 case 0:
1290 state.texiform = GL_RGBA8;
1291 state.texform = GL_RGBA;
1292 state.texty = GL_UNSIGNED_BYTE;
1293 state.colorspace = fz_device_rgb (state.ctx);
1294 break;
1295 case 1:
1296 state.texiform = GL_LUMINANCE_ALPHA;
1297 state.texform = GL_LUMINANCE_ALPHA;
1298 state.texty = GL_UNSIGNED_BYTE;
1299 state.colorspace = fz_device_gray (state.ctx);
1300 break;
1301 default:
1302 errx (1, "invalid colorspce %d", colorspace);
1306 static void realloctexts (int texcount)
1308 size_t size;
1310 if (texcount == state.texcount) return;
1312 if (texcount < state.texcount) {
1313 glDeleteTextures (state.texcount - texcount,
1314 state.texids + texcount);
1317 size = texcount * (sizeof (*state.texids) + sizeof (*state.texowners));
1318 state.texids = realloc (state.texids, size);
1319 if (!state.texids) {
1320 err (1, "realloc texs %zu", size);
1323 state.texowners = (void *) (state.texids + texcount);
1324 if (texcount > state.texcount) {
1325 glGenTextures (texcount - state.texcount,
1326 state.texids + state.texcount);
1327 for (int i = state.texcount; i < texcount; ++i) {
1328 state.texowners[i].w = -1;
1329 state.texowners[i].slice = NULL;
1332 state.texcount = texcount;
1333 state.texindex = 0;
1336 static char *mbtoutf8 (char *s)
1338 char *p, *r;
1339 wchar_t *tmp;
1340 size_t i, ret, len;
1342 if (state.utf8cs) {
1343 return s;
1346 len = mbstowcs (NULL, s, strlen (s));
1347 if (len == 0 || len == (size_t) -1) {
1348 if (len)
1349 printd ("emsg mbtoutf8: mbstowcs: %d:%s", errno, strerror (errno));
1350 return s;
1353 tmp = calloc (len, sizeof (wchar_t));
1354 if (!tmp) {
1355 printd ("emsg mbtoutf8: calloc(%zu, %zu): %d:%s",
1356 len, sizeof (wchar_t), errno, strerror (errno));
1357 return s;
1360 ret = mbstowcs (tmp, s, len);
1361 if (ret == (size_t) -1) {
1362 printd ("emsg mbtoutf8: mbswcs %zu characters failed: %d:%s",
1363 len, errno, strerror (errno));
1364 free (tmp);
1365 return s;
1368 len = 0;
1369 for (i = 0; i < ret; ++i) {
1370 len += fz_runelen (tmp[i]);
1373 p = r = malloc (len + 1);
1374 if (!r) {
1375 printd ("emsg mbtoutf8: malloc(%zu)", len);
1376 free (tmp);
1377 return s;
1380 for (i = 0; i < ret; ++i) {
1381 p += fz_runetochar (p, tmp[i]);
1383 *p = 0;
1384 free (tmp);
1385 return r;
1388 value ml_mbtoutf8 (value s_v);
1389 value ml_mbtoutf8 (value s_v)
1391 CAMLparam1 (s_v);
1392 CAMLlocal1 (ret_v);
1393 char *s, *r;
1395 s = String_val (s_v);
1396 r = mbtoutf8 (s);
1397 if (r == s) {
1398 ret_v = s_v;
1400 else {
1401 ret_v = caml_copy_string (r);
1402 free (r);
1404 CAMLreturn (ret_v);
1407 static void * mainloop (void UNUSED_ATTR *unused)
1409 char *p = NULL;
1410 int len, ret, oldlen = 0;
1412 fz_var (p);
1413 fz_var (oldlen);
1414 for (;;) {
1415 len = readlen (state.csock);
1416 if (len == 0) {
1417 errx (1, "readlen returned 0");
1420 if (oldlen < len + 1) {
1421 p = realloc (p, len + 1);
1422 if (!p) {
1423 err (1, "realloc %d failed", len + 1);
1425 oldlen = len + 1;
1427 readdata (state.csock, p, len);
1428 p[len] = 0;
1430 if (!strncmp ("open", p, 4)) {
1431 int off, usedoccss, ok = 0, layouth;
1432 char *password;
1433 char *filename;
1434 char *utf8filename;
1435 size_t filenamelen;
1437 fz_var (ok);
1438 ret = sscanf (p + 5, " %d %d %n", &usedoccss, &layouth, &off);
1439 if (ret != 2) {
1440 errx (1, "malformed open `%.*s' ret=%d", len, p, ret);
1443 filename = p + 5 + off;
1444 filenamelen = strlen (filename);
1445 password = filename + filenamelen + 1;
1447 if (password[strlen (password) + 1]) {
1448 fz_set_user_css (state.ctx, password + strlen (password) + 1);
1451 lock ("open");
1452 fz_set_use_document_css (state.ctx, usedoccss);
1453 fz_try (state.ctx) {
1454 ok = openxref (filename, password, layouth);
1456 fz_catch (state.ctx) {
1457 utf8filename = mbtoutf8 (filename);
1458 printd ("msg Could not open %s", utf8filename);
1459 if (utf8filename != filename) {
1460 free (utf8filename);
1463 if (ok) {
1464 docinfo ();
1465 initpdims ();
1467 unlock ("open");
1469 if (ok) {
1470 utf8filename = mbtoutf8 (filename);
1471 printd ("msg Opened %s (press h/F1 to get help)", utf8filename);
1472 if (utf8filename != filename) {
1473 free (utf8filename);
1475 state.needoutline = 1;
1478 else if (!strncmp ("cs", p, 2)) {
1479 int i, colorspace;
1481 ret = sscanf (p + 2, " %d", &colorspace);
1482 if (ret != 1) {
1483 errx (1, "malformed cs `%.*s' ret=%d", len, p, ret);
1485 lock ("cs");
1486 set_tex_params (colorspace);
1487 for (i = 0; i < state.texcount; ++i) {
1488 state.texowners[i].w = -1;
1489 state.texowners[i].slice = NULL;
1491 unlock ("cs");
1493 else if (!strncmp ("freepage", p, 8)) {
1494 void *ptr;
1496 ret = sscanf (p + 8, " %" SCNxPTR, (uintptr_t *) &ptr);
1497 if (ret != 1) {
1498 errx (1, "malformed freepage `%.*s' ret=%d", len, p, ret);
1500 lock ("freepage");
1501 freepage (ptr);
1502 unlock ("freepage");
1504 else if (!strncmp ("freetile", p, 8)) {
1505 void *ptr;
1507 ret = sscanf (p + 8, " %" SCNxPTR, (uintptr_t *) &ptr);
1508 if (ret != 1) {
1509 errx (1, "malformed freetile `%.*s' ret=%d", len, p, ret);
1511 lock ("freetile");
1512 freetile (ptr);
1513 unlock ("freetile");
1515 else if (!strncmp ("search", p, 6)) {
1516 int icase, pageno, y, len2, forward;
1517 char *pattern;
1518 regex_t re;
1520 ret = sscanf (p + 6, " %d %d %d %d,%n",
1521 &icase, &pageno, &y, &forward, &len2);
1522 if (ret != 4) {
1523 errx (1, "malformed search `%s' ret=%d", p, ret);
1526 pattern = p + 6 + len2;
1527 ret = regcomp (&re, pattern,
1528 REG_EXTENDED | (icase ? REG_ICASE : 0));
1529 if (ret) {
1530 char errbuf[80];
1531 size_t size;
1533 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1534 printd ("msg regcomp failed `%.*s'", (int) size, errbuf);
1536 else {
1537 search (&re, pageno, y, forward);
1538 regfree (&re);
1541 else if (!strncmp ("geometry", p, 8)) {
1542 int w, h, fitmodel;
1544 printd ("clear");
1545 ret = sscanf (p + 8, " %d %d %d", &w, &h, &fitmodel);
1546 if (ret != 3) {
1547 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1550 lock ("geometry");
1551 state.h = h;
1552 if (w != state.w) {
1553 state.w = w;
1554 for (int i = 0; i < state.texcount; ++i) {
1555 state.texowners[i].slice = NULL;
1558 state.fitmodel = fitmodel;
1559 layout ();
1560 process_outline ();
1562 state.gen++;
1563 unlock ("geometry");
1564 printd ("continue %d", state.pagecount);
1566 else if (!strncmp ("reqlayout", p, 9)) {
1567 char *nameddest;
1568 int rotate, off, h;
1569 int fitmodel;
1570 pdf_document *pdf;
1572 printd ("clear");
1573 ret = sscanf (p + 9, " %d %d %d %n",
1574 &rotate, &fitmodel, &h, &off);
1575 if (ret != 3) {
1576 errx (1, "bad reqlayout line `%.*s' ret=%d", len, p, ret);
1578 lock ("reqlayout");
1579 pdf = pdf_specifics (state.ctx, state.doc);
1580 if (state.rotate != rotate || state.fitmodel != fitmodel) {
1581 state.gen += 1;
1583 state.rotate = rotate;
1584 state.fitmodel = fitmodel;
1585 state.h = h;
1586 layout ();
1587 process_outline ();
1589 nameddest = p + 9 + off;
1590 if (pdf && nameddest && *nameddest) {
1591 fz_point xy;
1592 struct pagedim *pdim;
1593 int pageno = pdf_lookup_anchor (state.ctx, pdf, nameddest,
1594 &xy.x, &xy.y);
1595 pdim = pdimofpageno (pageno);
1596 xy = fz_transform_point (xy, pdim->ctm);
1597 printd ("a %d %d %d", pageno, (int) xy.x, (int) xy.y);
1600 state.gen++;
1601 unlock ("reqlayout");
1602 printd ("continue %d", state.pagecount);
1604 else if (!strncmp ("page", p, 4)) {
1605 double a, b;
1606 struct page *page;
1607 int pageno, pindex;
1609 ret = sscanf (p + 4, " %d %d", &pageno, &pindex);
1610 if (ret != 2) {
1611 errx (1, "bad page line `%.*s' ret=%d", len, p, ret);
1614 lock ("page");
1615 a = now ();
1616 page = loadpage (pageno, pindex);
1617 b = now ();
1618 unlock ("page");
1620 printd ("page %" PRIxPTR " %f", (uintptr_t) page, b - a);
1622 else if (!strncmp ("tile", p, 4)) {
1623 int x, y, w, h;
1624 struct page *page;
1625 struct tile *tile;
1626 double a, b;
1627 void *data;
1629 ret = sscanf (p + 4, " %" SCNxPTR " %d %d %d %d %" SCNxPTR,
1630 (uintptr_t *) &page, &x, &y, &w, &h,
1631 (uintptr_t *) &data);
1632 if (ret != 6) {
1633 errx (1, "bad tile line `%.*s' ret=%d", len, p, ret);
1636 lock ("tile");
1637 a = now ();
1638 tile = rendertile (page, x, y, w, h, data);
1639 b = now ();
1640 unlock ("tile");
1642 printd ("tile %d %d %" PRIxPTR " %u %f",
1643 x, y, (uintptr_t) (tile),
1644 tile->w * tile->h * tile->pixmap->n,
1645 b - a);
1647 else if (!strncmp ("trimset", p, 7)) {
1648 fz_irect fuzz;
1649 int trimmargins;
1651 ret = sscanf (p + 7, " %d %d %d %d %d",
1652 &trimmargins, &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1653 if (ret != 5) {
1654 errx (1, "malformed trimset `%.*s' ret=%d", len, p, ret);
1656 lock ("trimset");
1657 state.trimmargins = trimmargins;
1658 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1659 state.trimanew = 1;
1660 state.trimfuzz = fuzz;
1662 unlock ("trimset");
1664 else if (!strncmp ("settrim", p, 7)) {
1665 fz_irect fuzz;
1666 int trimmargins;
1668 ret = sscanf (p + 7, " %d %d %d %d %d",
1669 &trimmargins, &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1670 if (ret != 5) {
1671 errx (1, "malformed settrim `%.*s' ret=%d", len, p, ret);
1673 printd ("clear");
1674 lock ("settrim");
1675 state.trimmargins = trimmargins;
1676 state.needoutline = 1;
1677 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1678 state.trimanew = 1;
1679 state.trimfuzz = fuzz;
1681 state.pagedimcount = 0;
1682 free (state.pagedims);
1683 state.pagedims = NULL;
1684 initpdims ();
1685 layout ();
1686 process_outline ();
1687 unlock ("settrim");
1688 printd ("continue %d", state.pagecount);
1690 else if (!strncmp ("sliceh", p, 6)) {
1691 int h;
1693 ret = sscanf (p + 6, " %d", &h);
1694 if (ret != 1) {
1695 errx (1, "malformed sliceh `%.*s' ret=%d", len, p, ret);
1697 if (h != state.sliceheight) {
1698 state.sliceheight = h;
1699 for (int i = 0; i < state.texcount; ++i) {
1700 state.texowners[i].w = -1;
1701 state.texowners[i].h = -1;
1702 state.texowners[i].slice = NULL;
1706 else if (!strncmp ("interrupt", p, 9)) {
1707 printd ("vmsg interrupted");
1709 else {
1710 errx (1, "unknown command %.*s", len, p);
1713 return 0;
1716 value ml_isexternallink (value uri_v);
1717 value ml_isexternallink (value uri_v)
1719 CAMLparam1 (uri_v);
1720 int ext = fz_is_external_link (state.ctx, String_val (uri_v));
1721 CAMLreturn (Val_bool (ext));
1724 value ml_uritolocation (value uri_v);
1725 value ml_uritolocation (value uri_v)
1727 CAMLparam1 (uri_v);
1728 CAMLlocal1 (ret_v);
1729 int pageno;
1730 fz_point xy;
1731 struct pagedim *pdim;
1733 pageno = fz_resolve_link (state.ctx, state.doc, String_val (uri_v),
1734 &xy.x, &xy.y);
1735 pdim = pdimofpageno (pageno);
1736 xy = fz_transform_point (xy, pdim->ctm);
1737 ret_v = caml_alloc_tuple (3);
1738 Field (ret_v, 0) = Val_int (pageno);
1739 Field (ret_v, 1) = caml_copy_double ((double) xy.x);
1740 Field (ret_v, 2) = caml_copy_double ((double) xy.y);
1741 CAMLreturn (ret_v);
1744 value ml_realloctexts (value texcount_v);
1745 value ml_realloctexts (value texcount_v)
1747 CAMLparam1 (texcount_v);
1748 int ok;
1750 if (trylock (__func__)) {
1751 ok = 0;
1752 goto done;
1754 realloctexts (Int_val (texcount_v));
1755 ok = 1;
1756 unlock (__func__);
1758 done:
1759 CAMLreturn (Val_bool (ok));
1762 static void recti (int x0, int y0, int x1, int y1)
1764 GLfloat *v = state.vertices;
1766 glVertexPointer (2, GL_FLOAT, 0, v);
1767 v[0] = x0; v[1] = y0;
1768 v[2] = x1; v[3] = y0;
1769 v[4] = x0; v[5] = y1;
1770 v[6] = x1; v[7] = y1;
1771 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1774 static void showsel (struct page *page, int ox, int oy)
1776 fz_irect bbox;
1777 fz_rect rect;
1778 fz_stext_block *block;
1779 int seen = 0;
1780 unsigned char selcolor[] = {15,15,15,140};
1782 if (!page->fmark || !page->lmark) return;
1784 glEnable (GL_BLEND);
1785 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1786 glColor4ubv (selcolor);
1788 ox += state.pagedims[page->pdimno].bounds.x0;
1789 oy += state.pagedims[page->pdimno].bounds.y0;
1791 for (block = page->text->first_block; block; block = block->next) {
1792 fz_stext_line *line;
1794 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
1795 for (line = block->u.t.first_line; line; line = line->next) {
1796 fz_stext_char *ch;
1798 rect = fz_empty_rect;
1799 for (ch = line->first_char; ch; ch = ch->next) {
1800 fz_rect r;
1801 if (ch == page->fmark) seen = 1;
1802 r = fz_rect_from_quad (ch->quad);
1803 if (seen) rect = fz_union_rect (rect, r);
1804 if (ch == page->lmark) {
1805 bbox = fz_round_rect (rect);
1806 recti (bbox.x0 + ox, bbox.y0 + oy,
1807 bbox.x1 + ox, bbox.y1 + oy);
1808 goto done;
1811 bbox = fz_round_rect (rect);
1812 recti (bbox.x0 + ox, bbox.y0 + oy,
1813 bbox.x1 + ox, bbox.y1 + oy);
1816 done:
1817 glDisable (GL_BLEND);
1820 #pragma GCC diagnostic push
1821 #pragma GCC diagnostic ignored "-Wconversion"
1822 #include "glfont.c"
1823 #pragma GCC diagnostic pop
1825 static void stipplerect (fz_matrix m,
1826 fz_point p1,
1827 fz_point p2,
1828 fz_point p3,
1829 fz_point p4,
1830 GLfloat *texcoords,
1831 GLfloat *vertices)
1833 p1 = fz_transform_point (p1, m);
1834 p2 = fz_transform_point (p2, m);
1835 p3 = fz_transform_point (p3, m);
1836 p4 = fz_transform_point (p4, m);
1838 float w, h, s, t;
1840 w = p2.x - p1.x;
1841 h = p2.y - p1.y;
1842 t = hypotf (w, h) * .25f;
1844 w = p3.x - p2.x;
1845 h = p3.y - p2.y;
1846 s = hypotf (w, h) * .25f;
1848 texcoords[0] = 0; vertices[0] = p1.x; vertices[1] = p1.y;
1849 texcoords[1] = t; vertices[2] = p2.x; vertices[3] = p2.y;
1851 texcoords[2] = 0; vertices[4] = p2.x; vertices[5] = p2.y;
1852 texcoords[3] = s; vertices[6] = p3.x; vertices[7] = p3.y;
1854 texcoords[4] = 0; vertices[8] = p3.x; vertices[9] = p3.y;
1855 texcoords[5] = t; vertices[10] = p4.x; vertices[11] = p4.y;
1857 texcoords[6] = 0; vertices[12] = p4.x; vertices[13] = p4.y;
1858 texcoords[7] = s; vertices[14] = p1.x; vertices[15] = p1.y;
1860 glDrawArrays (GL_LINES, 0, 8);
1863 static void solidrect (fz_matrix m,
1864 fz_point p1,
1865 fz_point p2,
1866 fz_point p3,
1867 fz_point p4,
1868 GLfloat *vertices)
1870 p1 = fz_transform_point (p1, m);
1871 p2 = fz_transform_point (p2, m);
1872 p3 = fz_transform_point (p3, m);
1873 p4 = fz_transform_point (p4, m);
1874 vertices[0] = p1.x; vertices[1] = p1.y;
1875 vertices[2] = p2.x; vertices[3] = p2.y;
1877 vertices[4] = p3.x; vertices[5] = p3.y;
1878 vertices[6] = p4.x; vertices[7] = p4.y;
1879 glDrawArrays (GL_TRIANGLE_FAN, 0, 4);
1882 static void ensurelinks (struct page *page)
1884 if (!page->links)
1885 page->links = fz_load_links (state.ctx, page->fzpage);
1888 static void highlightlinks (struct page *page, int xoff, int yoff)
1890 fz_matrix ctm;
1891 fz_link *link;
1892 GLfloat *texcoords = state.texcoords;
1893 GLfloat *vertices = state.vertices;
1895 ensurelinks (page);
1897 glEnable (GL_TEXTURE_1D);
1898 glEnable (GL_BLEND);
1899 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1900 glBindTexture (GL_TEXTURE_1D, state.stid);
1902 xoff -= state.pagedims[page->pdimno].bounds.x0;
1903 yoff -= state.pagedims[page->pdimno].bounds.y0;
1904 ctm = fz_concat (pagectm (page), fz_translate (xoff, yoff));
1906 glTexCoordPointer (1, GL_FLOAT, 0, texcoords);
1907 glVertexPointer (2, GL_FLOAT, 0, vertices);
1909 for (link = page->links; link; link = link->next) {
1910 fz_point p1, p2, p3, p4;
1912 p1.x = link->rect.x0;
1913 p1.y = link->rect.y0;
1915 p2.x = link->rect.x1;
1916 p2.y = link->rect.y0;
1918 p3.x = link->rect.x1;
1919 p3.y = link->rect.y1;
1921 p4.x = link->rect.x0;
1922 p4.y = link->rect.y1;
1924 /* TODO: different colours for different schemes */
1925 if (fz_is_external_link (state.ctx, link->uri)) glColor3ub (0, 0, 255);
1926 else glColor3ub (255, 0, 0);
1928 stipplerect (ctm, p1, p2, p3, p4, texcoords, vertices);
1931 for (int i = 0; i < page->annotcount; ++i) {
1932 fz_point p1, p2, p3, p4;
1933 struct annot *annot = &page->annots[i];
1935 p1.x = annot->bbox.x0;
1936 p1.y = annot->bbox.y0;
1938 p2.x = annot->bbox.x1;
1939 p2.y = annot->bbox.y0;
1941 p3.x = annot->bbox.x1;
1942 p3.y = annot->bbox.y1;
1944 p4.x = annot->bbox.x0;
1945 p4.y = annot->bbox.y1;
1947 glColor3ub (0, 0, 128);
1948 stipplerect (ctm, p1, p2, p3, p4, texcoords, vertices);
1951 glDisable (GL_BLEND);
1952 glDisable (GL_TEXTURE_1D);
1955 static int compareslinks (const void *l, const void *r)
1957 struct slink const *ls = l;
1958 struct slink const *rs = r;
1959 if (ls->bbox.y0 == rs->bbox.y0) {
1960 return rs->bbox.x0 - rs->bbox.x0;
1962 return ls->bbox.y0 - rs->bbox.y0;
1965 static void droptext (struct page *page)
1967 if (page->text) {
1968 fz_drop_stext_page (state.ctx, page->text);
1969 page->fmark = NULL;
1970 page->lmark = NULL;
1971 page->text = NULL;
1975 static void dropannots (struct page *page)
1977 if (page->annots) {
1978 free (page->annots);
1979 page->annots = NULL;
1980 page->annotcount = 0;
1984 static void ensureannots (struct page *page)
1986 int i, count = 0;
1987 size_t annotsize = sizeof (*page->annots);
1988 fz_annot *annot;
1990 if (state.gen != page->agen) {
1991 dropannots (page);
1992 page->agen = state.gen;
1994 if (page->annots) return;
1996 for (annot = fz_first_annot (state.ctx, page->fzpage);
1997 annot;
1998 annot = fz_next_annot (state.ctx, annot)) {
1999 count++;
2002 if (count > 0) {
2003 page->annotcount = count;
2004 page->annots = calloc (count, annotsize);
2005 if (!page->annots) {
2006 err (1, "calloc annots %d", count);
2009 for (annot = fz_first_annot (state.ctx, page->fzpage), i = 0;
2010 annot;
2011 annot = fz_next_annot (state.ctx, annot), i++) {
2012 fz_rect rect;
2014 rect = fz_bound_annot (state.ctx, annot);
2015 page->annots[i].annot = annot;
2016 page->annots[i].bbox = fz_round_rect (rect);
2021 static void dropslinks (struct page *page)
2023 if (page->slinks) {
2024 free (page->slinks);
2025 page->slinks = NULL;
2026 page->slinkcount = 0;
2028 if (page->links) {
2029 fz_drop_link (state.ctx, page->links);
2030 page->links = NULL;
2034 static void ensureslinks (struct page *page)
2036 fz_matrix ctm;
2037 int i, count;
2038 size_t slinksize = sizeof (*page->slinks);
2039 fz_link *link;
2041 ensureannots (page);
2042 if (state.gen != page->sgen) {
2043 dropslinks (page);
2044 page->sgen = state.gen;
2046 if (page->slinks) return;
2048 ensurelinks (page);
2049 ctm = pagectm (page);
2051 count = page->annotcount;
2052 for (link = page->links; link; link = link->next) {
2053 count++;
2055 if (count > 0) {
2056 int j;
2058 page->slinkcount = count;
2059 page->slinks = calloc (count, slinksize);
2060 if (!page->slinks) {
2061 err (1, "calloc slinks %d", count);
2064 for (i = 0, link = page->links; link; ++i, link = link->next) {
2065 fz_rect rect;
2067 rect = link->rect;
2068 rect = fz_transform_rect (rect, ctm);
2069 page->slinks[i].tag = SLINK;
2070 page->slinks[i].u.link = link;
2071 page->slinks[i].bbox = fz_round_rect (rect);
2073 for (j = 0; j < page->annotcount; ++j, ++i) {
2074 fz_rect rect;
2075 rect = fz_bound_annot (state.ctx, page->annots[j].annot);
2076 rect = fz_transform_rect (rect, ctm);
2077 page->slinks[i].bbox = fz_round_rect (rect);
2079 page->slinks[i].tag = SANNOT;
2080 page->slinks[i].u.annot = page->annots[j].annot;
2082 qsort (page->slinks, count, slinksize, compareslinks);
2086 static void highlightslinks (struct page *page, int xoff, int yoff,
2087 int noff, char *targ, mlsize_t tlen, int hfsize)
2089 char buf[40];
2090 struct slink *slink;
2091 float x0, y0, x1, y1, w;
2093 ensureslinks (page);
2094 glColor3ub (0xc3, 0xb0, 0x91);
2095 for (int i = 0; i < page->slinkcount; ++i) {
2096 fmt_linkn (buf, i + noff);
2097 if (!tlen || !strncmp (targ, buf, tlen)) {
2098 slink = &page->slinks[i];
2100 x0 = slink->bbox.x0 + xoff - 5;
2101 y1 = slink->bbox.y0 + yoff - 5;
2102 y0 = y1 + 10 + hfsize;
2103 w = measure_string (state.face, hfsize, buf);
2104 x1 = x0 + w + 10;
2105 recti ((int) x0, (int) y0, (int) x1, (int) y1);
2109 glEnable (GL_BLEND);
2110 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2111 glEnable (GL_TEXTURE_2D);
2112 glColor3ub (0, 0, 0);
2113 for (int i = 0; i < page->slinkcount; ++i) {
2114 fmt_linkn (buf, i + noff);
2115 if (!tlen || !strncmp (targ, buf, tlen)) {
2116 slink = &page->slinks[i];
2118 x0 = slink->bbox.x0 + xoff;
2119 y0 = slink->bbox.y0 + yoff + hfsize;
2120 draw_string (state.face, hfsize, x0, y0, buf);
2123 glDisable (GL_TEXTURE_2D);
2124 glDisable (GL_BLEND);
2127 static void uploadslice (struct tile *tile, struct slice *slice)
2129 int offset;
2130 struct slice *slice1;
2131 unsigned char *texdata;
2133 offset = 0;
2134 for (slice1 = tile->slices; slice != slice1; slice1++) {
2135 offset += slice1->h * tile->w * tile->pixmap->n;
2137 if (slice->texindex != -1 && slice->texindex < state.texcount
2138 && state.texowners[slice->texindex].slice == slice) {
2139 glBindTexture (TEXT_TYPE, state.texids[slice->texindex]);
2141 else {
2142 int subimage = 0;
2143 int texindex = state.texindex++ % state.texcount;
2145 if (state.texowners[texindex].w == tile->w) {
2146 if (state.texowners[texindex].h >= slice->h) {
2147 subimage = 1;
2149 else {
2150 state.texowners[texindex].h = slice->h;
2153 else {
2154 state.texowners[texindex].h = slice->h;
2157 state.texowners[texindex].w = tile->w;
2158 state.texowners[texindex].slice = slice;
2159 slice->texindex = texindex;
2161 glBindTexture (TEXT_TYPE, state.texids[texindex]);
2162 #if TEXT_TYPE == GL_TEXTURE_2D
2163 glTexParameteri (TEXT_TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2164 glTexParameteri (TEXT_TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2165 glTexParameteri (TEXT_TYPE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2166 glTexParameteri (TEXT_TYPE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2167 #endif
2168 if (tile->pbo) {
2169 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, tile->pbo->id);
2170 texdata = 0;
2172 else {
2173 texdata = tile->pixmap->samples;
2175 if (subimage) {
2176 glTexSubImage2D (TEXT_TYPE,
2180 tile->w,
2181 slice->h,
2182 state.texform,
2183 state.texty,
2184 texdata+offset
2187 else {
2188 glTexImage2D (TEXT_TYPE,
2190 state.texiform,
2191 tile->w,
2192 slice->h,
2194 state.texform,
2195 state.texty,
2196 texdata+offset
2199 if (tile->pbo) {
2200 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
2205 void ml_begintiles (void);
2206 void ml_begintiles (void)
2208 glEnable (TEXT_TYPE);
2209 glTexCoordPointer (2, GL_FLOAT, 0, state.texcoords);
2210 glVertexPointer (2, GL_FLOAT, 0, state.vertices);
2213 void ml_endtiles (void);
2214 void ml_endtiles (void)
2216 glDisable (TEXT_TYPE);
2219 void ml_drawtile (value args_v, value ptr_v);
2220 void ml_drawtile (value args_v, value ptr_v)
2222 CAMLparam2 (args_v, ptr_v);
2223 int dispx = Int_val (Field (args_v, 0));
2224 int dispy = Int_val (Field (args_v, 1));
2225 int dispw = Int_val (Field (args_v, 2));
2226 int disph = Int_val (Field (args_v, 3));
2227 int tilex = Int_val (Field (args_v, 4));
2228 int tiley = Int_val (Field (args_v, 5));
2229 char *s = String_val (ptr_v);
2230 struct tile *tile = parse_pointer (__func__, s);
2231 int slicey, firstslice;
2232 struct slice *slice;
2233 GLfloat *texcoords = state.texcoords;
2234 GLfloat *vertices = state.vertices;
2236 firstslice = tiley / tile->sliceheight;
2237 slice = &tile->slices[firstslice];
2238 slicey = tiley % tile->sliceheight;
2240 while (disph > 0) {
2241 int dh;
2243 dh = slice->h - slicey;
2244 dh = fz_mini (disph, dh);
2245 uploadslice (tile, slice);
2247 texcoords[0] = tilex; texcoords[1] = slicey;
2248 texcoords[2] = tilex+dispw; texcoords[3] = slicey;
2249 texcoords[4] = tilex; texcoords[5] = slicey+dh;
2250 texcoords[6] = tilex+dispw; texcoords[7] = slicey+dh;
2252 vertices[0] = dispx; vertices[1] = dispy;
2253 vertices[2] = dispx+dispw; vertices[3] = dispy;
2254 vertices[4] = dispx; vertices[5] = dispy+dh;
2255 vertices[6] = dispx+dispw; vertices[7] = dispy+dh;
2257 #if TEXT_TYPE == GL_TEXTURE_2D
2258 for (int i = 0; i < 8; ++i) {
2259 texcoords[i] /= ((i & 1) == 0 ? tile->w : slice->h);
2261 #endif
2263 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
2264 dispy += dh;
2265 disph -= dh;
2266 slice++;
2267 ARSERT (!(slice - tile->slices >= tile->slicecount && disph > 0));
2268 slicey = 0;
2270 CAMLreturn0;
2273 static void drawprect (struct page *page, int xoff, int yoff, value rects_v)
2275 fz_matrix ctm;
2276 fz_point p1, p2, p3, p4;
2277 GLfloat *vertices = state.vertices;
2278 double *v = (double *) rects_v;
2280 xoff -= state.pagedims[page->pdimno].bounds.x0;
2281 yoff -= state.pagedims[page->pdimno].bounds.y0;
2282 ctm = fz_concat (pagectm (page), fz_translate (xoff, yoff));
2284 glEnable (GL_BLEND);
2285 glVertexPointer (2, GL_FLOAT, 0, vertices);
2287 glColor4dv (v);
2288 p1.x = (float) v[4];
2289 p1.y = (float) v[5];
2291 p2.x = (float) v[6];
2292 p2.y = (float) v[5];
2294 p3.x = (float) v[6];
2295 p3.y = (float) v[7];
2297 p4.x = (float) v[4];
2298 p4.y = (float) v[7];
2299 solidrect (ctm, p1, p2, p3, p4, vertices);
2300 glDisable (GL_BLEND);
2303 value ml_postprocess (value ptr_v, value hlinks_v,
2304 value xoff_v, value yoff_v,
2305 value li_v);
2306 value ml_postprocess (value ptr_v, value hlinks_v,
2307 value xoff_v, value yoff_v,
2308 value li_v)
2310 CAMLparam5 (ptr_v, hlinks_v, xoff_v, yoff_v, li_v);
2311 int xoff = Int_val (xoff_v);
2312 int yoff = Int_val (yoff_v);
2313 int noff = Int_val (Field (li_v, 0));
2314 char *targ = String_val (Field (li_v, 1));
2315 mlsize_t tlen = caml_string_length (Field (li_v, 1));
2316 int hfsize = Int_val (Field (li_v, 2));
2317 char *s = String_val (ptr_v);
2318 int hlmask = Int_val (hlinks_v);
2319 struct page *page = parse_pointer (__func__, s);
2321 if (!page->fzpage) {
2322 /* deal with loadpage failed pages */
2323 goto done;
2326 if (trylock (__func__)) {
2327 noff = -1;
2328 goto done;
2331 ensureannots (page);
2332 if (hlmask & 1) highlightlinks (page, xoff, yoff);
2333 if (hlmask & 2) {
2334 highlightslinks (page, xoff, yoff, noff, targ, tlen, hfsize);
2335 noff = page->slinkcount;
2337 if (page->tgen == state.gen) {
2338 showsel (page, xoff, yoff);
2340 unlock (__func__);
2342 done:
2343 CAMLreturn (Val_int (noff));
2346 void ml_drawprect (value ptr_v, value xoff_v, value yoff_v, value rects_v);
2347 void ml_drawprect (value ptr_v, value xoff_v, value yoff_v, value rects_v)
2349 CAMLparam4 (ptr_v, xoff_v, yoff_v, rects_v);
2350 int xoff = Int_val (xoff_v);
2351 int yoff = Int_val (yoff_v);
2352 char *s = String_val (ptr_v);
2353 struct page *page = parse_pointer (__func__, s);
2355 drawprect (page, xoff, yoff, rects_v);
2356 CAMLreturn0;
2359 static struct annot *getannot (struct page *page, int x, int y)
2361 fz_point p;
2362 fz_matrix ctm;
2363 const fz_matrix *tctm;
2364 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
2366 if (!page->annots) return NULL;
2368 if (pdf) {
2369 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
2370 tctm = &state.pagedims[page->pdimno].tctm;
2372 else {
2373 tctm = &fz_identity;
2376 p.x = x;
2377 p.y = y;
2379 ctm = fz_concat (*tctm, state.pagedims[page->pdimno].ctm);
2380 ctm = fz_invert_matrix (ctm);
2381 p = fz_transform_point (p, ctm);
2383 if (pdf) {
2384 for (int i = 0; i < page->annotcount; ++i) {
2385 struct annot *a = &page->annots[i];
2386 fz_rect rect;
2388 rect = fz_bound_annot (state.ctx, a->annot);
2389 if (p.x >= rect.x0 && p.x <= rect.x1) {
2390 if (p.y >= rect.y0 && p.y <= rect.y1)
2391 return a;
2395 return NULL;
2398 static fz_link *getlink (struct page *page, int x, int y)
2400 fz_point p;
2401 fz_link *link;
2403 ensureslinks (page);
2405 p.x = x;
2406 p.y = y;
2408 p = fz_transform_point (p, fz_invert_matrix (pagectm (page)));
2410 for (link = page->links; link; link = link->next) {
2411 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
2412 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
2413 return link;
2417 return NULL;
2420 static void ensuretext (struct page *page)
2422 if (state.gen != page->tgen) {
2423 droptext (page);
2424 page->tgen = state.gen;
2426 if (!page->text) {
2427 fz_device *tdev;
2429 page->text = fz_new_stext_page (state.ctx,
2430 state.pagedims[page->pdimno].mediabox);
2431 tdev = fz_new_stext_device (state.ctx, page->text, 0);
2432 fz_run_display_list (state.ctx, page->dlist,
2433 tdev, pagectm (page), fz_infinite_rect, NULL);
2434 fz_close_device (state.ctx, tdev);
2435 fz_drop_device (state.ctx, tdev);
2439 value ml_find_page_with_links (value start_page_v, value dir_v);
2440 value ml_find_page_with_links (value start_page_v, value dir_v)
2442 CAMLparam2 (start_page_v, dir_v);
2443 CAMLlocal1 (ret_v);
2444 int i, dir = Int_val (dir_v);
2445 int start_page = Int_val (start_page_v);
2446 int end_page = dir > 0 ? state.pagecount : -1;
2447 pdf_document *pdf;
2449 fz_var (end_page);
2450 ret_v = Val_int (0);
2451 lock (__func__);
2452 pdf = pdf_specifics (state.ctx, state.doc);
2453 for (i = start_page + dir; i != end_page; i += dir) {
2454 int found;
2456 fz_var (found);
2457 if (pdf) {
2458 pdf_page *page = NULL;
2460 fz_var (page);
2461 fz_try (state.ctx) {
2462 page = pdf_load_page (state.ctx, pdf, i);
2463 found = !!page->links || !!page->annots;
2465 fz_catch (state.ctx) {
2466 found = 0;
2468 if (page) {
2469 fz_drop_page (state.ctx, &page->super);
2472 else {
2473 fz_page *page = fz_load_page (state.ctx, state.doc, i);
2474 fz_link *link = fz_load_links (state.ctx, page);
2475 found = !!link;
2476 fz_drop_link (state.ctx, link);
2477 fz_drop_page (state.ctx, page);
2480 if (found) {
2481 ret_v = caml_alloc_small (1, 1);
2482 Field (ret_v, 0) = Val_int (i);
2483 goto unlock;
2486 unlock:
2487 unlock (__func__);
2488 CAMLreturn (ret_v);
2491 enum { dir_first, dir_last };
2492 enum { dir_first_visible, dir_left, dir_right, dir_down, dir_up };
2494 value ml_findlink (value ptr_v, value dir_v);
2495 value ml_findlink (value ptr_v, value dir_v)
2497 CAMLparam2 (ptr_v, dir_v);
2498 CAMLlocal2 (ret_v, pos_v);
2499 struct page *page;
2500 int dirtag, i, slinkindex;
2501 struct slink *found = NULL ,*slink;
2502 char *s = String_val (ptr_v);
2504 page = parse_pointer (__func__, s);
2505 ret_v = Val_int (0);
2506 lock (__func__);
2507 ensureslinks (page);
2509 if (Is_block (dir_v)) {
2510 dirtag = Tag_val (dir_v);
2511 switch (dirtag) {
2512 case dir_first_visible:
2514 int x0, y0, dir, first_index, last_index;
2516 pos_v = Field (dir_v, 0);
2517 x0 = Int_val (Field (pos_v, 0));
2518 y0 = Int_val (Field (pos_v, 1));
2519 dir = Int_val (Field (pos_v, 2));
2521 if (dir >= 0) {
2522 dir = 1;
2523 first_index = 0;
2524 last_index = page->slinkcount;
2526 else {
2527 first_index = page->slinkcount - 1;
2528 last_index = -1;
2531 for (i = first_index; i != last_index; i += dir) {
2532 slink = &page->slinks[i];
2533 if (slink->bbox.y0 >= y0 && slink->bbox.x0 >= x0) {
2534 found = slink;
2535 break;
2539 break;
2541 case dir_left:
2542 slinkindex = Int_val (Field (dir_v, 0));
2543 found = &page->slinks[slinkindex];
2544 for (i = slinkindex - 1; i >= 0; --i) {
2545 slink = &page->slinks[i];
2546 if (slink->bbox.x0 < found->bbox.x0) {
2547 found = slink;
2548 break;
2551 break;
2553 case dir_right:
2554 slinkindex = Int_val (Field (dir_v, 0));
2555 found = &page->slinks[slinkindex];
2556 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2557 slink = &page->slinks[i];
2558 if (slink->bbox.x0 > found->bbox.x0) {
2559 found = slink;
2560 break;
2563 break;
2565 case dir_down:
2566 slinkindex = Int_val (Field (dir_v, 0));
2567 found = &page->slinks[slinkindex];
2568 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2569 slink = &page->slinks[i];
2570 if (slink->bbox.y0 >= found->bbox.y0) {
2571 found = slink;
2572 break;
2575 break;
2577 case dir_up:
2578 slinkindex = Int_val (Field (dir_v, 0));
2579 found = &page->slinks[slinkindex];
2580 for (i = slinkindex - 1; i >= 0; --i) {
2581 slink = &page->slinks[i];
2582 if (slink->bbox.y0 <= found->bbox.y0) {
2583 found = slink;
2584 break;
2587 break;
2590 else {
2591 dirtag = Int_val (dir_v);
2592 switch (dirtag) {
2593 case dir_first:
2594 found = page->slinks;
2595 break;
2597 case dir_last:
2598 if (page->slinks) {
2599 found = page->slinks + (page->slinkcount - 1);
2601 break;
2604 if (found) {
2605 ret_v = caml_alloc_small (2, 1);
2606 Field (ret_v, 0) = Val_int (found - page->slinks);
2609 unlock (__func__);
2610 CAMLreturn (ret_v);
2613 enum { uuri, utext, uannot };
2615 value ml_getlink (value ptr_v, value n_v);
2616 value ml_getlink (value ptr_v, value n_v)
2618 CAMLparam2 (ptr_v, n_v);
2619 CAMLlocal4 (ret_v, tup_v, str_v, gr_v);
2620 fz_link *link;
2621 struct page *page;
2622 char *s = String_val (ptr_v);
2623 struct slink *slink;
2625 ret_v = Val_int (0);
2626 page = parse_pointer (__func__, s);
2628 lock (__func__);
2629 ensureslinks (page);
2630 slink = &page->slinks[Int_val (n_v)];
2631 if (slink->tag == SLINK) {
2632 link = slink->u.link;
2633 str_v = caml_copy_string (link->uri);
2634 ret_v = caml_alloc_small (1, uuri);
2635 Field (ret_v, 0) = str_v;
2637 else {
2638 ret_v = caml_alloc_small (1, uannot);
2639 tup_v = caml_alloc_tuple (2);
2640 Field (ret_v, 0) = tup_v;
2641 Field (tup_v, 0) = ptr_v;
2642 Field (tup_v, 1) = n_v;
2644 unlock (__func__);
2646 CAMLreturn (ret_v);
2649 value ml_getannotcontents (value ptr_v, value n_v);
2650 value ml_getannotcontents (value ptr_v, value n_v)
2652 CAMLparam2 (ptr_v, n_v);
2653 CAMLlocal1 (ret_v);
2654 pdf_document *pdf;
2655 const char *contents = "";
2657 lock (__func__);
2658 pdf = pdf_specifics (state.ctx, state.doc);
2659 if (pdf) {
2660 char *s = String_val (ptr_v);
2661 struct page *page;
2662 struct slink *slink;
2664 page = parse_pointer (__func__, s);
2665 slink = &page->slinks[Int_val (n_v)];
2666 contents = pdf_annot_contents (state.ctx, (pdf_annot *) slink->u.annot);
2668 unlock (__func__);
2669 ret_v = caml_copy_string (contents);
2670 CAMLreturn (ret_v);
2673 value ml_getlinkcount (value ptr_v);
2674 value ml_getlinkcount (value ptr_v)
2676 CAMLparam1 (ptr_v);
2677 struct page *page;
2678 char *s = String_val (ptr_v);
2680 page = parse_pointer (__func__, s);
2681 CAMLreturn (Val_int (page->slinkcount));
2684 value ml_getlinkrect (value ptr_v, value n_v);
2685 value ml_getlinkrect (value ptr_v, value n_v)
2687 CAMLparam2 (ptr_v, n_v);
2688 CAMLlocal1 (ret_v);
2689 struct page *page;
2690 struct slink *slink;
2691 char *s = String_val (ptr_v);
2693 page = parse_pointer (__func__, s);
2694 ret_v = caml_alloc_tuple (4);
2695 lock (__func__);
2696 ensureslinks (page);
2698 slink = &page->slinks[Int_val (n_v)];
2699 Field (ret_v, 0) = Val_int (slink->bbox.x0);
2700 Field (ret_v, 1) = Val_int (slink->bbox.y0);
2701 Field (ret_v, 2) = Val_int (slink->bbox.x1);
2702 Field (ret_v, 3) = Val_int (slink->bbox.y1);
2703 unlock (__func__);
2704 CAMLreturn (ret_v);
2707 value ml_whatsunder (value ptr_v, value x_v, value y_v);
2708 value ml_whatsunder (value ptr_v, value x_v, value y_v)
2710 CAMLparam3 (ptr_v, x_v, y_v);
2711 CAMLlocal4 (ret_v, tup_v, str_v, gr_v);
2712 fz_link *link;
2713 struct annot *annot;
2714 struct page *page;
2715 char *ptr = String_val (ptr_v);
2716 int x = Int_val (x_v), y = Int_val (y_v);
2717 struct pagedim *pdim;
2719 ret_v = Val_int (0);
2720 if (trylock (__func__)) {
2721 goto done;
2724 page = parse_pointer (__func__, ptr);
2725 pdim = &state.pagedims[page->pdimno];
2726 x += pdim->bounds.x0;
2727 y += pdim->bounds.y0;
2730 annot = getannot (page, x, y);
2731 if (annot) {
2732 int i, n = -1;
2734 ensureslinks (page);
2735 for (i = 0; i < page->slinkcount; ++i) {
2736 if (page->slinks[i].tag == SANNOT
2737 && page->slinks[i].u.annot == annot->annot) {
2738 n = i;
2739 break;
2742 ret_v = caml_alloc_small (1, uannot);
2743 tup_v = caml_alloc_tuple (2);
2744 Field (ret_v, 0) = tup_v;
2745 Field (tup_v, 0) = ptr_v;
2746 Field (tup_v, 1) = Val_int (n);
2747 goto unlock;
2751 link = getlink (page, x, y);
2752 if (link) {
2753 str_v = caml_copy_string (link->uri);
2754 ret_v = caml_alloc_small (1, uuri);
2755 Field (ret_v, 0) = str_v;
2757 else {
2758 fz_rect *b;
2759 fz_stext_block *block;
2761 ensuretext (page);
2763 for (block = page->text->first_block; block; block = block->next) {
2764 fz_stext_line *line;
2766 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
2767 b = &block->bbox;
2768 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2769 continue;
2771 for (line = block->u.t.first_line; line; line = line->next) {
2772 fz_stext_char *ch;
2774 b = &line->bbox;
2775 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2776 continue;
2778 for (ch = line->first_char; ch; ch = ch->next) {
2779 fz_quad *q = &ch->quad;
2781 if (x >= q->ul.x && x <= q->ur.x
2782 && y >= q->ul.y && y <= q->ll.y) {
2783 const char *n2 = fz_font_name (state.ctx, ch->font);
2784 FT_FaceRec *face = fz_font_ft_face (state.ctx,
2785 ch->font);
2787 if (!n2) n2 = "<unknown font>";
2789 if (face && face->family_name) {
2790 char *s;
2791 char *n1 = face->family_name;
2792 size_t l1 = strlen (n1);
2793 size_t l2 = strlen (n2);
2795 if (l1 != l2 || memcmp (n1, n2, l1)) {
2796 s = malloc (l1 + l2 + 2);
2797 if (s) {
2798 memcpy (s, n2, l2);
2799 s[l2] = '=';
2800 memcpy (s + l2 + 1, n1, l1 + 1);
2801 str_v = caml_copy_string (s);
2802 free (s);
2806 if (str_v == Val_unit) {
2807 str_v = caml_copy_string (n2);
2809 ret_v = caml_alloc_small (1, utext);
2810 Field (ret_v, 0) = str_v;
2811 goto unlock;
2817 unlock:
2818 unlock (__func__);
2820 done:
2821 CAMLreturn (ret_v);
2824 enum { mark_page, mark_block, mark_line, mark_word };
2826 void ml_clearmark (value ptr_v);
2827 void ml_clearmark (value ptr_v)
2829 CAMLparam1 (ptr_v);
2830 char *s = String_val (ptr_v);
2831 struct page *page;
2833 if (trylock (__func__)) {
2834 goto done;
2837 page = parse_pointer (__func__, s);
2838 page->fmark = NULL;
2839 page->lmark = NULL;
2841 unlock (__func__);
2842 done:
2843 CAMLreturn0;
2846 static int uninteresting (int c)
2848 return isspace (c) || ispunct (c);
2851 value ml_markunder (value ptr_v, value x_v, value y_v, value mark_v);
2852 value ml_markunder (value ptr_v, value x_v, value y_v, value mark_v)
2854 CAMLparam4 (ptr_v, x_v, y_v, mark_v);
2855 CAMLlocal1 (ret_v);
2856 fz_rect *b;
2857 struct page *page;
2858 fz_stext_line *line;
2859 fz_stext_block *block;
2860 struct pagedim *pdim;
2861 int mark = Int_val (mark_v);
2862 char *s = String_val (ptr_v);
2863 int x = Int_val (x_v), y = Int_val (y_v);
2865 ret_v = Val_bool (0);
2866 if (trylock (__func__)) {
2867 goto done;
2870 page = parse_pointer (__func__, s);
2871 pdim = &state.pagedims[page->pdimno];
2873 ensuretext (page);
2875 if (mark == mark_page) {
2876 page->fmark = page->text->first_block->u.t.first_line->first_char;
2877 page->lmark = page->text->last_block->u.t.last_line->last_char;
2878 ret_v = Val_bool (1);
2879 goto unlock;
2882 x += pdim->bounds.x0;
2883 y += pdim->bounds.y0;
2885 for (block = page->text->first_block; block; block = block->next) {
2886 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
2887 b = &block->bbox;
2888 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2889 continue;
2891 if (mark == mark_block) {
2892 page->fmark = block->u.t.first_line->first_char;
2893 page->lmark = block->u.t.last_line->last_char;
2894 ret_v = Val_bool (1);
2895 goto unlock;
2898 for (line = block->u.t.first_line; line; line = line->next) {
2899 fz_stext_char *ch;
2901 b = &line->bbox;
2902 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2903 continue;
2905 if (mark == mark_line) {
2906 page->fmark = line->first_char;
2907 page->lmark = line->last_char;
2908 ret_v = Val_bool (1);
2909 goto unlock;
2912 for (ch = line->first_char; ch; ch = ch->next) {
2913 fz_stext_char *ch2, *first = NULL, *last = NULL;
2914 fz_quad *q = &ch->quad;
2915 if (x >= q->ul.x && x <= q->ur.x
2916 && y >= q->ul.y && y <= q->ll.y) {
2917 for (ch2 = line->first_char; ch2 != ch; ch2 = ch2->next) {
2918 if (uninteresting (ch2->c)) first = NULL;
2919 else if (!first) first = ch2;
2921 for (ch2 = ch; ch2; ch2 = ch2->next) {
2922 if (uninteresting (ch2->c)) break;
2923 last = ch2;
2926 page->fmark = first;
2927 page->lmark = last;
2928 ret_v = Val_bool (1);
2929 goto unlock;
2934 unlock:
2935 if (!Bool_val (ret_v)) {
2936 page->fmark = NULL;
2937 page->lmark = NULL;
2939 unlock (__func__);
2941 done:
2942 CAMLreturn (ret_v);
2945 value ml_rectofblock (value ptr_v, value x_v, value y_v);
2946 value ml_rectofblock (value ptr_v, value x_v, value y_v)
2948 CAMLparam3 (ptr_v, x_v, y_v);
2949 CAMLlocal2 (ret_v, res_v);
2950 fz_rect *b = NULL;
2951 struct page *page;
2952 struct pagedim *pdim;
2953 fz_stext_block *block;
2954 char *s = String_val (ptr_v);
2955 int x = Int_val (x_v), y = Int_val (y_v);
2957 ret_v = Val_int (0);
2958 if (trylock (__func__)) {
2959 goto done;
2962 page = parse_pointer (__func__, s);
2963 pdim = &state.pagedims[page->pdimno];
2964 x += pdim->bounds.x0;
2965 y += pdim->bounds.y0;
2967 ensuretext (page);
2969 for (block = page->text->first_block; block; block = block->next) {
2970 switch (block->type) {
2971 case FZ_STEXT_BLOCK_TEXT:
2972 b = &block->bbox;
2973 break;
2975 case FZ_STEXT_BLOCK_IMAGE:
2976 b = &block->bbox;
2977 break;
2979 default:
2980 continue;
2983 if (x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1)
2984 break;
2985 b = NULL;
2987 if (b) {
2988 res_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
2989 ret_v = caml_alloc_small (1, 1);
2990 Store_double_field (res_v, 0, (double) b->x0);
2991 Store_double_field (res_v, 1, (double) b->x1);
2992 Store_double_field (res_v, 2, (double) b->y0);
2993 Store_double_field (res_v, 3, (double) b->y1);
2994 Field (ret_v, 0) = res_v;
2996 unlock (__func__);
2998 done:
2999 CAMLreturn (ret_v);
3002 void ml_seltext (value ptr_v, value rect_v);
3003 void ml_seltext (value ptr_v, value rect_v)
3005 CAMLparam2 (ptr_v, rect_v);
3006 struct page *page;
3007 struct pagedim *pdim;
3008 char *s = String_val (ptr_v);
3009 int x0, x1, y0, y1;
3010 fz_stext_char *ch;
3011 fz_stext_line *line;
3012 fz_stext_block *block;
3013 fz_stext_char *fc, *lc;
3015 if (trylock (__func__)) {
3016 goto done;
3019 page = parse_pointer (__func__, s);
3020 ensuretext (page);
3022 pdim = &state.pagedims[page->pdimno];
3023 x0 = Int_val (Field (rect_v, 0)) + pdim->bounds.x0;
3024 y0 = Int_val (Field (rect_v, 1)) + pdim->bounds.y0;
3025 x1 = Int_val (Field (rect_v, 2)) + pdim->bounds.x0;
3026 y1 = Int_val (Field (rect_v, 3)) + pdim->bounds.y0;
3028 if (y0 > y1) {
3029 int t = y0;
3030 y0 = y1;
3031 y1 = t;
3032 x0 = x1;
3033 x1 = t;
3036 fc = page->fmark;
3037 lc = page->lmark;
3039 for (block = page->text->first_block; block; block = block->next) {
3040 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
3041 for (line = block->u.t.first_line; line; line = line->next) {
3042 for (ch = line->first_char; ch; ch = ch->next) {
3043 fz_quad q = ch->quad;
3044 if (x0 >= q.ul.x && x0 <= q.ur.x
3045 && y0 >= q.ul.y && y0 <= q.ll.y) {
3046 fc = ch;
3048 if (x1 >= q.ul.x && x1 <= q.ur.x
3049 && y1 >= q.ul.y && y1 <= q.ll.y) {
3050 lc = ch;
3055 if (x1 < x0 && fc == lc) {
3056 fz_stext_char *t;
3058 t = fc;
3059 fc = lc;
3060 lc = t;
3063 page->fmark = fc;
3064 page->lmark = lc;
3066 unlock (__func__);
3068 done:
3069 CAMLreturn0;
3072 static int pipechar (FILE *f, fz_stext_char *ch)
3074 char buf[4];
3075 int len;
3076 size_t ret;
3078 len = fz_runetochar (buf, ch->c);
3079 ret = fwrite (buf, len, 1, f);
3080 if (ret != 1) {
3081 printd ("emsg failed to write %d bytes ret=%zu: %d:%s",
3082 len, ret, errno, strerror (errno));
3083 return -1;
3085 return 0;
3088 value ml_spawn (value command_v, value fds_v);
3089 value ml_spawn (value command_v, value fds_v)
3091 CAMLparam2 (command_v, fds_v);
3092 CAMLlocal2 (l_v, tup_v);
3093 int ret, ret1;
3094 pid_t pid = (pid_t) -1;
3095 char *msg = NULL;
3096 value earg_v = Nothing;
3097 posix_spawnattr_t attr;
3098 posix_spawn_file_actions_t fa;
3099 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
3101 argv[2] = String_val (command_v);
3103 if ((ret = posix_spawn_file_actions_init (&fa)) != 0) {
3104 unix_error (ret, "posix_spawn_file_actions_init", Nothing);
3107 if ((ret = posix_spawnattr_init (&attr)) != 0) {
3108 msg = "posix_spawnattr_init";
3109 goto fail1;
3112 #ifdef POSIX_SPAWN_USEVFORK
3113 if ((ret = posix_spawnattr_setflags (&attr, POSIX_SPAWN_USEVFORK)) != 0) {
3114 msg = "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
3115 goto fail;
3117 #endif
3119 for (l_v = fds_v; l_v != Val_int (0); l_v = Field (l_v, 1)) {
3120 int fd1, fd2;
3122 tup_v = Field (l_v, 0);
3123 fd1 = Int_val (Field (tup_v, 0));
3124 fd2 = Int_val (Field (tup_v, 1));
3125 if (fd2 < 0) {
3126 if ((ret = posix_spawn_file_actions_addclose (&fa, fd1)) != 0) {
3127 msg = "posix_spawn_file_actions_addclose";
3128 earg_v = tup_v;
3129 goto fail;
3132 else {
3133 if ((ret = posix_spawn_file_actions_adddup2 (&fa, fd1, fd2)) != 0) {
3134 msg = "posix_spawn_file_actions_adddup2";
3135 earg_v = tup_v;
3136 goto fail;
3141 if ((ret = posix_spawn (&pid, "/bin/sh", &fa, &attr, argv, environ))) {
3142 msg = "posix_spawn";
3143 goto fail;
3146 fail:
3147 if ((ret1 = posix_spawnattr_destroy (&attr)) != 0) {
3148 printd ("emsg posix_spawnattr_destroy: %d:%s", ret1, strerror (ret1));
3151 fail1:
3152 if ((ret1 = posix_spawn_file_actions_destroy (&fa)) != 0) {
3153 printd ("emsg posix_spawn_file_actions_destroy: %d:%s",
3154 ret1, strerror (ret1));
3157 if (msg)
3158 unix_error (ret, msg, earg_v);
3160 CAMLreturn (Val_int (pid));
3163 value ml_hassel (value ptr_v);
3164 value ml_hassel (value ptr_v)
3166 CAMLparam1 (ptr_v);
3167 CAMLlocal1 (ret_v);
3168 struct page *page;
3169 char *s = String_val (ptr_v);
3171 ret_v = Val_bool (0);
3172 if (trylock (__func__)) {
3173 goto done;
3176 page = parse_pointer (__func__, s);
3177 ret_v = Val_bool (page->fmark && page->lmark);
3178 unlock (__func__);
3179 done:
3180 CAMLreturn (ret_v);
3183 void ml_copysel (value fd_v, value ptr_v);
3184 void ml_copysel (value fd_v, value ptr_v)
3186 CAMLparam2 (fd_v, ptr_v);
3187 FILE *f;
3188 int seen = 0;
3189 struct page *page;
3190 fz_stext_line *line;
3191 fz_stext_block *block;
3192 int fd = Int_val (fd_v);
3193 char *s = String_val (ptr_v);
3195 if (trylock (__func__)) {
3196 goto done;
3199 page = parse_pointer (__func__, s);
3201 if (!page->fmark || !page->lmark) {
3202 printd ("emsg nothing to copy on page %d", page->pageno);
3203 goto unlock;
3206 f = fdopen (fd, "w");
3207 if (!f) {
3208 printd ("emsg failed to fdopen sel pipe (from fd %d): %d:%s",
3209 fd, errno, strerror (errno));
3210 f = stdout;
3213 for (block = page->text->first_block; block; block = block->next) {
3214 if (block->type != FZ_STEXT_BLOCK_TEXT) continue;
3215 for (line = block->u.t.first_line; line; line = line->next) {
3216 fz_stext_char *ch;
3217 for (ch = line->first_char; ch; ch = ch->next) {
3218 if (seen || ch == page->fmark) {
3219 do {
3220 pipechar (f, ch);
3221 if (ch == page->lmark) goto close;
3222 } while ((ch = ch->next));
3223 seen = 1;
3224 break;
3227 if (seen) fputc ('\n', f);
3230 close:
3231 if (f != stdout) {
3232 int ret = fclose (f);
3233 fd = -1;
3234 if (ret == -1) {
3235 if (errno != ECHILD) {
3236 printd ("emsg failed to close sel pipe: %d:%s",
3237 errno, strerror (errno));
3241 unlock:
3242 unlock (__func__);
3244 done:
3245 if (fd >= 0) {
3246 if (close (fd)) {
3247 printd ("emsg failed to close sel pipe: %d:%s",
3248 errno, strerror (errno));
3251 CAMLreturn0;
3254 value ml_getpdimrect (value pagedimno_v);
3255 value ml_getpdimrect (value pagedimno_v)
3257 CAMLparam1 (pagedimno_v);
3258 CAMLlocal1 (ret_v);
3259 int pagedimno = Int_val (pagedimno_v);
3260 fz_rect box;
3262 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
3263 if (trylock (__func__)) {
3264 box = fz_empty_rect;
3266 else {
3267 box = state.pagedims[pagedimno].mediabox;
3268 unlock (__func__);
3271 Store_double_field (ret_v, 0, (double) box.x0);
3272 Store_double_field (ret_v, 1, (double) box.x1);
3273 Store_double_field (ret_v, 2, (double) box.y0);
3274 Store_double_field (ret_v, 3, (double) box.y1);
3276 CAMLreturn (ret_v);
3279 value ml_zoom_for_height (value winw_v, value winh_v,
3280 value dw_v, value cols_v);
3281 value ml_zoom_for_height (value winw_v, value winh_v,
3282 value dw_v, value cols_v)
3284 CAMLparam4 (winw_v, winh_v, dw_v, cols_v);
3285 CAMLlocal1 (ret_v);
3286 int i;
3287 float zoom = -1.;
3288 float maxh = 0.0;
3289 struct pagedim *p;
3290 float winw = Int_val (winw_v);
3291 float winh = Int_val (winh_v);
3292 float dw = Int_val (dw_v);
3293 float cols = Int_val (cols_v);
3294 float pw = 1.0, ph = 1.0;
3296 if (trylock (__func__)) {
3297 goto done;
3300 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3301 float w = p->pagebox.x1 / cols;
3302 float h = p->pagebox.y1;
3303 if (h > maxh) {
3304 maxh = h;
3305 ph = h;
3306 if (state.fitmodel != FitProportional) pw = w;
3308 if ((state.fitmodel == FitProportional) && w > pw) pw = w;
3311 zoom = (((winh / ph) * pw) + dw) / winw;
3312 unlock (__func__);
3313 done:
3314 ret_v = caml_copy_double ((double) zoom);
3315 CAMLreturn (ret_v);
3318 value ml_getmaxw (value unit_v);
3319 value ml_getmaxw (value unit_v)
3321 CAMLparam1 (unit_v);
3322 CAMLlocal1 (ret_v);
3323 int i;
3324 float maxw = -1.;
3325 struct pagedim *p;
3327 if (trylock (__func__)) {
3328 goto done;
3331 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3332 float w = p->pagebox.x1;
3333 maxw = fz_max (maxw, w);
3336 unlock (__func__);
3337 done:
3338 ret_v = caml_copy_double ((double) maxw);
3339 CAMLreturn (ret_v);
3342 value ml_draw_string (value pt_v, value x_v,
3343 value y_v, value string_v);
3344 value ml_draw_string (value pt_v, value x_v,
3345 value y_v, value string_v)
3347 CAMLparam4 (pt_v, x_v, y_v, string_v);
3348 CAMLlocal1 (ret_v);
3349 int pt = Int_val(pt_v);
3350 int x = Int_val (x_v);
3351 int y = Int_val (y_v);
3352 float w;
3354 w = draw_string (state.face, pt, x, y, String_val (string_v));
3355 ret_v = caml_copy_double (w);
3356 CAMLreturn (ret_v);
3359 value ml_measure_string (value pt_v, value string_v);
3360 value ml_measure_string (value pt_v, value string_v)
3362 CAMLparam2 (pt_v, string_v);
3363 CAMLlocal1 (ret_v);
3364 int pt = Int_val (pt_v);
3365 double w;
3367 w = (double) measure_string (state.face, pt, String_val (string_v));
3368 ret_v = caml_copy_double (w);
3369 CAMLreturn (ret_v);
3372 value ml_getpagebox (value opaque_v);
3373 value ml_getpagebox (value opaque_v)
3375 CAMLparam1 (opaque_v);
3376 CAMLlocal1 (ret_v);
3377 fz_rect rect;
3378 fz_irect bbox;
3379 fz_device *dev;
3380 char *s = String_val (opaque_v);
3381 struct page *page = parse_pointer (__func__, s);
3383 ret_v = caml_alloc_tuple (4);
3384 dev = fz_new_bbox_device (state.ctx, &rect);
3386 fz_run_page (state.ctx, page->fzpage, dev, pagectm (page), NULL);
3388 fz_close_device (state.ctx, dev);
3389 fz_drop_device (state.ctx, dev);
3390 bbox = fz_round_rect (rect);
3391 Field (ret_v, 0) = Val_int (bbox.x0);
3392 Field (ret_v, 1) = Val_int (bbox.y0);
3393 Field (ret_v, 2) = Val_int (bbox.x1);
3394 Field (ret_v, 3) = Val_int (bbox.y1);
3396 CAMLreturn (ret_v);
3399 void ml_setaalevel (value level_v);
3400 void ml_setaalevel (value level_v)
3402 CAMLparam1 (level_v);
3404 state.aalevel = Int_val (level_v);
3405 CAMLreturn0;
3408 value ml_keysymtoutf8 (value keysym_v);
3409 #ifndef CIDER
3410 value ml_keysymtoutf8 (value keysym_v)
3412 CAMLparam1 (keysym_v);
3413 CAMLlocal1 (str_v);
3414 KeySym keysym = Int_val (keysym_v);
3415 Rune rune;
3416 extern long keysym2ucs (KeySym);
3417 int len;
3418 char buf[5];
3420 rune = (Rune) keysym2ucs (keysym);
3421 len = fz_runetochar (buf, rune);
3422 buf[len] = 0;
3423 str_v = caml_copy_string (buf);
3424 CAMLreturn (str_v);
3426 #else
3427 value ml_keysymtoutf8 (value keysym_v)
3429 CAMLparam1 (keysym_v);
3430 CAMLlocal1 (str_v);
3431 long ucs_v = Long_val (keysym_v);
3432 int len;
3433 char buf[5];
3435 len = fz_runetochar (buf, (int) ucs_v);
3436 buf[len] = 0;
3437 str_v = caml_copy_string (buf);
3438 CAMLreturn (str_v);
3440 #endif
3442 enum { piunknown, pilinux, pimacos, pibsd };
3444 value ml_platform (value unit_v);
3445 value ml_platform (value unit_v)
3447 CAMLparam1 (unit_v);
3448 CAMLlocal2 (tup_v, arr_v);
3449 int platid = piunknown;
3450 struct utsname buf;
3452 #if defined __linux__
3453 platid = pilinux;
3454 #elif defined __DragonFly__ || defined __FreeBSD__
3455 || defined __OpenBSD__ || defined __NetBSD__
3456 platid = pibsd;
3457 #elif defined __APPLE__
3458 platid = pimacos;
3459 #endif
3460 if (uname (&buf)) err (1, "uname");
3462 tup_v = caml_alloc_tuple (2);
3464 char const *sar[] = {
3465 buf.sysname,
3466 buf.release,
3467 buf.version,
3468 buf.machine,
3469 NULL
3471 arr_v = caml_copy_string_array (sar);
3473 Field (tup_v, 0) = Val_int (platid);
3474 Field (tup_v, 1) = arr_v;
3475 CAMLreturn (tup_v);
3478 void ml_cloexec (value fd_v);
3479 void ml_cloexec (value fd_v)
3481 CAMLparam1 (fd_v);
3482 int fd = Int_val (fd_v);
3484 if (fcntl (fd, F_SETFD, FD_CLOEXEC, 1)) {
3485 uerror ("fcntl", Nothing);
3487 CAMLreturn0;
3490 value ml_getpbo (value w_v, value h_v, value cs_v);
3491 value ml_getpbo (value w_v, value h_v, value cs_v)
3493 CAMLparam2 (w_v, h_v);
3494 CAMLlocal1 (ret_v);
3495 struct bo *pbo;
3496 int w = Int_val (w_v);
3497 int h = Int_val (h_v);
3498 int cs = Int_val (cs_v);
3500 if (state.bo_usable) {
3501 pbo = calloc (sizeof (*pbo), 1);
3502 if (!pbo) {
3503 err (1, "calloc pbo");
3506 switch (cs) {
3507 case 0:
3508 case 1:
3509 pbo->size = w*h*4;
3510 break;
3511 case 2:
3512 pbo->size = w*h*2;
3513 break;
3514 default:
3515 errx (1, "%s: invalid colorspace %d", __func__, cs);
3518 state.glGenBuffersARB (1, &pbo->id);
3519 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, pbo->id);
3520 state.glBufferDataARB (GL_PIXEL_UNPACK_BUFFER_ARB, (GLsizei) pbo->size,
3521 NULL, GL_STREAM_DRAW);
3522 pbo->ptr = state.glMapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB,
3523 GL_READ_WRITE);
3524 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
3525 if (!pbo->ptr) {
3526 printd ("emsg glMapBufferARB failed: %#x", glGetError ());
3527 state.glDeleteBuffersARB (1, &pbo->id);
3528 free (pbo);
3529 ret_v = caml_copy_string ("0");
3531 else {
3532 int res;
3533 char *s;
3535 res = snprintf (NULL, 0, "%" PRIxPTR, (uintptr_t) pbo);
3536 if (res < 0) {
3537 err (1, "snprintf %" PRIxPTR " failed", (uintptr_t) pbo);
3539 s = malloc (res+1);
3540 if (!s) {
3541 err (1, "malloc %d bytes failed", res+1);
3543 res = sprintf (s, "%" PRIxPTR, (uintptr_t) pbo);
3544 if (res < 0) {
3545 err (1, "sprintf %" PRIxPTR " failed", (uintptr_t) pbo);
3547 ret_v = caml_copy_string (s);
3548 free (s);
3551 else {
3552 ret_v = caml_copy_string ("0");
3554 CAMLreturn (ret_v);
3557 void ml_freepbo (value s_v);
3558 void ml_freepbo (value s_v)
3560 CAMLparam1 (s_v);
3561 char *s = String_val (s_v);
3562 struct tile *tile = parse_pointer (__func__, s);
3564 if (tile->pbo) {
3565 state.glDeleteBuffersARB (1, &tile->pbo->id);
3566 tile->pbo->id = -1;
3567 tile->pbo->ptr = NULL;
3568 tile->pbo->size = -1;
3570 CAMLreturn0;
3573 void ml_unmappbo (value s_v);
3574 void ml_unmappbo (value s_v)
3576 CAMLparam1 (s_v);
3577 char *s = String_val (s_v);
3578 struct tile *tile = parse_pointer (__func__, s);
3580 if (tile->pbo) {
3581 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, tile->pbo->id);
3582 if (state.glUnmapBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB) == GL_FALSE) {
3583 errx (1, "glUnmapBufferARB failed: %#x\n", glGetError ());
3585 tile->pbo->ptr = NULL;
3586 state.glBindBufferARB (GL_PIXEL_UNPACK_BUFFER_ARB, 0);
3588 CAMLreturn0;
3591 static void setuppbo (void)
3593 extern void (*wsigladdr (const char *name)) (void);
3594 #pragma GCC diagnostic push
3595 #ifdef __clang__
3596 #pragma GCC diagnostic ignored "-Wbad-function-cast"
3597 #endif
3598 #define GPA(n) (*(uintptr_t *) &state.n = (uintptr_t) wsigladdr (#n))
3599 state.bo_usable = GPA (glBindBufferARB)
3600 && GPA (glUnmapBufferARB)
3601 && GPA (glMapBufferARB)
3602 && GPA (glBufferDataARB)
3603 && GPA (glGenBuffersARB)
3604 && GPA (glDeleteBuffersARB);
3605 #undef GPA
3606 #pragma GCC diagnostic pop
3609 value ml_bo_usable (void);
3610 value ml_bo_usable (void)
3612 return Val_bool (state.bo_usable);
3615 value ml_unproject (value ptr_v, value x_v, value y_v);
3616 value ml_unproject (value ptr_v, value x_v, value y_v)
3618 CAMLparam3 (ptr_v, x_v, y_v);
3619 CAMLlocal2 (ret_v, tup_v);
3620 struct page *page;
3621 char *s = String_val (ptr_v);
3622 int x = Int_val (x_v), y = Int_val (y_v);
3623 struct pagedim *pdim;
3624 fz_point p;
3626 page = parse_pointer (__func__, s);
3627 pdim = &state.pagedims[page->pdimno];
3629 ret_v = Val_int (0);
3630 if (trylock (__func__)) {
3631 goto done;
3634 p.x = x + pdim->bounds.x0;
3635 p.y = y + pdim->bounds.y0;
3637 p = fz_transform_point (p, fz_invert_matrix (fz_concat (pdim->tctm,
3638 pdim->ctm)));
3640 tup_v = caml_alloc_tuple (2);
3641 ret_v = caml_alloc_small (1, 1);
3642 Field (tup_v, 0) = Val_int (p.x);
3643 Field (tup_v, 1) = Val_int (p.y);
3644 Field (ret_v, 0) = tup_v;
3646 unlock (__func__);
3647 done:
3648 CAMLreturn (ret_v);
3651 value ml_project (value ptr_v, value pageno_v, value pdimno_v,
3652 value x_v, value y_v);
3653 value ml_project (value ptr_v, value pageno_v, value pdimno_v,
3654 value x_v, value y_v)
3656 CAMLparam5 (ptr_v, pageno_v, pdimno_v, x_v, y_v);
3657 CAMLlocal1 (ret_v);
3658 struct page *page;
3659 char *s = String_val (ptr_v);
3660 int pageno = Int_val (pageno_v);
3661 int pdimno = Int_val (pdimno_v);
3662 float x = (float) Double_val (x_v), y = (float) Double_val (y_v);
3663 struct pagedim *pdim;
3664 fz_point p;
3665 fz_matrix ctm;
3667 ret_v = Val_int (0);
3668 lock (__func__);
3670 if (!*s) {
3671 page = loadpage (pageno, pdimno);
3673 else {
3674 page = parse_pointer (__func__, s);
3676 pdim = &state.pagedims[pdimno];
3678 if (pdf_specifics (state.ctx, state.doc)) {
3679 trimctm (pdf_page_from_fz_page (state.ctx, page->fzpage), page->pdimno);
3680 ctm = state.pagedims[page->pdimno].tctm;
3682 else {
3683 ctm = fz_identity;
3685 p.x = x + pdim->bounds.x0;
3686 p.y = y + pdim->bounds.y0;
3688 ctm = fz_concat (pdim->tctm, pdim->ctm);
3689 p = fz_transform_point (p, ctm);
3691 ret_v = caml_alloc_tuple (2);
3692 Field (ret_v, 0) = caml_copy_double ((double) p.x);
3693 Field (ret_v, 1) = caml_copy_double ((double) p.y);
3695 if (!*s) {
3696 freepage (page);
3698 unlock (__func__);
3699 CAMLreturn (ret_v);
3702 void ml_addannot (value ptr_v, value x_v, value y_v, value contents_v);
3703 void ml_addannot (value ptr_v, value x_v, value y_v, value contents_v)
3705 CAMLparam4 (ptr_v, x_v, y_v, contents_v);
3706 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3708 if (pdf) {
3709 pdf_annot *annot;
3710 struct page *page;
3711 fz_point p;
3712 char *s = String_val (ptr_v);
3714 page = parse_pointer (__func__, s);
3715 annot = pdf_create_annot (state.ctx,
3716 pdf_page_from_fz_page (state.ctx,
3717 page->fzpage),
3718 PDF_ANNOT_TEXT);
3719 p.x = Int_val (x_v);
3720 p.y = Int_val (y_v);
3721 pdf_set_annot_contents (state.ctx, annot, String_val (contents_v));
3722 pdf_set_text_annot_position (state.ctx, annot, p);
3723 state.dirty = 1;
3725 CAMLreturn0;
3728 void ml_delannot (value ptr_v, value n_v);
3729 void ml_delannot (value ptr_v, value n_v)
3731 CAMLparam2 (ptr_v, n_v);
3732 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3734 if (pdf) {
3735 struct page *page;
3736 char *s = String_val (ptr_v);
3737 struct slink *slink;
3739 page = parse_pointer (__func__, s);
3740 slink = &page->slinks[Int_val (n_v)];
3741 pdf_delete_annot (state.ctx,
3742 pdf_page_from_fz_page (state.ctx, page->fzpage),
3743 (pdf_annot *) slink->u.annot);
3744 state.dirty = 1;
3746 CAMLreturn0;
3749 void ml_modannot (value ptr_v, value n_v, value str_v);
3750 void ml_modannot (value ptr_v, value n_v, value str_v)
3752 CAMLparam3 (ptr_v, n_v, str_v);
3753 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3755 if (pdf) {
3756 struct page *page;
3757 char *s = String_val (ptr_v);
3758 struct slink *slink;
3760 page = parse_pointer (__func__, s);
3761 slink = &page->slinks[Int_val (n_v)];
3762 pdf_set_annot_contents (state.ctx, (pdf_annot *) slink->u.annot,
3763 String_val (str_v));
3764 state.dirty = 1;
3766 CAMLreturn0;
3769 value ml_hasunsavedchanges (void);
3770 value ml_hasunsavedchanges (void)
3772 return Val_bool (state.dirty);
3775 void ml_savedoc (value path_v);
3776 void ml_savedoc (value path_v)
3778 CAMLparam1 (path_v);
3779 pdf_document *pdf = pdf_specifics (state.ctx, state.doc);
3781 if (pdf) {
3782 pdf_save_document (state.ctx, pdf, String_val (path_v), NULL);
3784 CAMLreturn0;
3787 static void makestippletex (void)
3789 const char pixels[] = "\xff\xff\0\0";
3790 glGenTextures (1, &state.stid);
3791 glBindTexture (GL_TEXTURE_1D, state.stid);
3792 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3793 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3794 glTexImage1D (
3795 GL_TEXTURE_1D,
3797 GL_ALPHA,
3800 GL_ALPHA,
3801 GL_UNSIGNED_BYTE,
3802 pixels
3806 value ml_fz_version (void);
3807 value ml_fz_version (void)
3809 return caml_copy_string (FZ_VERSION);
3812 value ml_llpp_version (void);
3813 value ml_llpp_version (void)
3815 extern char llpp_version[];
3816 return caml_copy_string (llpp_version);
3819 void ml_init (value csock_v, value params_v);
3820 void ml_init (value csock_v, value params_v)
3822 CAMLparam2 (csock_v, params_v);
3823 CAMLlocal2 (trim_v, fuzz_v);
3824 int ret;
3825 int texcount;
3826 char *fontpath;
3827 int colorspace;
3828 int mustoresize;
3830 state.csock = Int_val (csock_v);
3831 state.rotate = Int_val (Field (params_v, 0));
3832 state.fitmodel = Int_val (Field (params_v, 1));
3833 trim_v = Field (params_v, 2);
3834 texcount = Int_val (Field (params_v, 3));
3835 state.sliceheight = Int_val (Field (params_v, 4));
3836 mustoresize = Int_val (Field (params_v, 5));
3837 colorspace = Int_val (Field (params_v, 6));
3838 fontpath = String_val (Field (params_v, 7));
3840 #ifdef CIDER
3841 state.utf8cs = 1;
3842 #else
3843 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html */
3844 if (setlocale (LC_CTYPE, "")) {
3845 const char *cset = nl_langinfo (CODESET);
3846 state.utf8cs = !strcmp (cset, "UTF-8");
3848 else {
3849 printd ("emsg setlocale: %d:%s", errno, strerror (errno));
3851 #endif
3853 if (caml_string_length (Field (params_v, 8)) > 0) {
3854 state.trimcachepath = ystrdup (String_val (Field (params_v, 8)));
3856 if (!state.trimcachepath) {
3857 printd ("emsg failed to strdup trimcachepath: %d:%s",
3858 errno, strerror (errno));
3862 state.ctx = fz_new_context (NULL, NULL, mustoresize);
3863 fz_register_document_handlers (state.ctx);
3865 state.trimmargins = Bool_val (Field (trim_v, 0));
3866 fuzz_v = Field (trim_v, 1);
3867 state.trimfuzz.x0 = Int_val (Field (fuzz_v, 0));
3868 state.trimfuzz.y0 = Int_val (Field (fuzz_v, 1));
3869 state.trimfuzz.x1 = Int_val (Field (fuzz_v, 2));
3870 state.trimfuzz.y1 = Int_val (Field (fuzz_v, 3));
3872 set_tex_params (colorspace);
3874 if (*fontpath) {
3875 state.face = load_font (fontpath);
3877 else {
3878 int len;
3879 const unsigned char *data;
3881 data = pdf_lookup_substitute_font (state.ctx, 0, 0, 0, 0, &len);
3882 state.face = load_builtin_font (data, len);
3884 if (!state.face) _exit (1);
3886 realloctexts (texcount);
3887 setuppbo ();
3888 makestippletex ();
3890 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
3891 if (ret) {
3892 errx (1, "pthread_create: %s", strerror (ret));
3895 CAMLreturn0;
3898 #if FIXME || !FIXME
3899 static void UNUSED_ATTR NO_OPTIMIZE_ATTR refmacs (void) {}
3900 #endif