Make use of the preload argument
[llpp.git] / link.c
blob39d90157628f301bf02d11410d022eab742a5b5f
1 /* lots of code c&p-ed directly from mupdf */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <signal.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
15 #ifdef __CYGWIN__
16 #include <cygwin/socket.h> /* FIONREAD */
17 #else
18 #include <spawn.h>
19 #endif
21 #include <regex.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <limits.h>
26 #include <GL/gl.h>
28 #include <caml/fail.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/unixsupport.h>
33 #include <fitz.h>
34 #include <mupdf.h>
35 #include <mupdf-internal.h>
36 #include <muxps.h>
37 #include <muxps-internal.h>
38 #include <mucbz.h>
40 #include FT_FREETYPE_H
42 #define PIGGYBACK
44 #ifndef __USE_GNU
45 extern char **environ;
46 #endif
48 #define MIN(a,b) ((a) < (b) ? (a) : (b))
49 #define MAX(a,b) ((a) > (b) ? (a) : (b))
51 #if defined __GNUC__
52 #define NORETURN_ATTR __attribute__ ((noreturn))
53 #define UNUSED_ATTR __attribute__ ((unused))
54 #define OPTIMIZE_ATTR(n) __attribute__ ((optimize ("O"#n)))
55 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
56 #else
57 #define NORETURN_ATTR
58 #define UNUSED_ATTR
59 #define OPTIMIZE_ATTR(n)
60 #define GCC_FMT_ATTR(a, b)
61 #endif
63 #define FMT_s "zu"
65 #define FMT_ptr "p"
66 #define FMT_ptr_cast(p) (p)
67 #define FMT_ptr_cast2(p) (p)
69 static void NORETURN_ATTR GCC_FMT_ATTR (2, 3)
70 err (int exitcode, const char *fmt, ...)
72 va_list ap;
73 int savederrno;
75 savederrno = errno;
76 va_start (ap, fmt);
77 vfprintf (stderr, fmt, ap);
78 va_end (ap);
79 fprintf (stderr, ": %s\n", strerror (savederrno));
80 fflush (stderr);
81 _exit (exitcode);
84 static void NORETURN_ATTR GCC_FMT_ATTR (2, 3)
85 errx (int exitcode, const char *fmt, ...)
87 va_list ap;
89 va_start (ap, fmt);
90 vfprintf (stderr, fmt, ap);
91 va_end (ap);
92 fputc ('\n', stderr);
93 fflush (stderr);
94 _exit (exitcode);
97 #ifndef GL_TEXTURE_RECTANGLE_ARB
98 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
99 #endif
101 #ifndef GL_BGRA
102 #define GL_BGRA 0x80E1
103 #endif
105 #ifndef GL_UNSIGNED_INT_8_8_8_8
106 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
107 #endif
109 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
110 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
111 #endif
113 #if 0
114 #define lprintf printf
115 #else
116 #define lprintf(...)
117 #endif
119 #define ARSERT(cond) for (;;) { \
120 if (!(cond)) { \
121 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
123 break; \
126 struct slice {
127 int h;
128 int texindex;
131 struct tile {
132 int x, y, w, h;
133 int slicecount;
134 int sliceheight;
135 fz_pixmap *pixmap;
136 struct slice slices[1];
139 struct pagedim {
140 int pageno;
141 int rotate;
142 int left;
143 int tctmready;
144 fz_bbox bounds;
145 fz_rect pagebox;
146 fz_rect mediabox;
147 fz_matrix ctm, zoomctm, lctm, tctm;
150 struct slink {
151 fz_bbox bbox;
152 fz_link *link;
155 enum { DPDF, DXPS, DCBZ };
157 struct page {
158 int tgen;
159 int sgen;
160 int type;
161 int pageno;
162 int pdimno;
163 fz_text_page *text;
164 fz_text_sheet *sheet;
165 union {
166 void *ptr;
167 pdf_page *pdfpage;
168 xps_page *xpspage;
169 cbz_page *cbzpage;
170 } u;
171 fz_display_list *dlist;
172 int slinkcount;
173 struct slink *slinks;
174 struct mark {
175 int i;
176 fz_text_span *span;
177 } fmark, lmark;
178 void (*freepage) (void *);
181 struct {
182 int type;
183 int sliceheight;
184 struct pagedim *pagedims;
185 int pagecount;
186 int pagedimcount;
187 union {
188 pdf_document *pdf;
189 xps_document *xps;
190 cbz_document *cbz;
191 } u;
192 fz_context *ctx;
193 int w, h;
195 int texindex;
196 int texcount;
197 GLuint *texids;
199 GLenum texiform;
200 GLenum texform;
201 GLenum texty;
203 fz_colorspace *colorspace;
205 struct {
206 int w, h;
207 struct slice *slice;
208 } *texowners;
210 int rotate;
211 int proportional;
212 int trimmargins;
213 int needoutline;
214 int gen;
215 int aalevel;
217 int trimanew;
218 fz_bbox trimfuzz;
219 fz_pixmap *pig;
221 pthread_t thread;
222 int cr, cw;
223 FT_Face face;
225 void (*closedoc) (void);
226 void (*freepage) (void *);
228 char *trimcachepath;
229 } state;
231 static void UNUSED_ATTR debug_rect (const char *cap, fz_rect r)
233 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap, r.x0, r.y0, r.x1, r.y1);
236 static void UNUSED_ATTR debug_bbox (const char *cap, fz_bbox r)
238 printf ("%s(bbox) %d,%d,%d,%d\n", cap, r.x0, r.y0, r.x1, r.y1);
241 static void UNUSED_ATTR debug_matrix (const char *cap, fz_matrix m)
243 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap,
244 m.a, m.b, m.c, m.d, m.e, m.f);
247 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
249 static void lock (const char *cap)
251 int ret = pthread_mutex_lock (&mutex);
252 if (ret) {
253 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
257 static void unlock (const char *cap)
259 int ret = pthread_mutex_unlock (&mutex);
260 if (ret) {
261 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
265 static int trylock (const char *cap)
267 int ret = pthread_mutex_trylock (&mutex);
268 if (ret && ret != EBUSY) {
269 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
271 return ret == EBUSY;
274 static void *parse_pointer (const char *cap, const char *s)
276 int ret;
277 void *ptr;
279 ret = sscanf (s, "%" FMT_ptr, FMT_ptr_cast (&ptr));
280 if (ret != 1) {
281 errx (1, "%s: cannot parse pointer in `%s'", cap, s);
283 return ptr;
286 static double now (void)
288 struct timeval tv;
290 if (gettimeofday (&tv, NULL)) {
291 err (1, "gettimeofday");
293 return tv.tv_sec + tv.tv_usec*1e-6;
296 static int hasdata (void)
298 int ret, avail;
299 ret = ioctl (state.cr, FIONREAD, &avail);
300 if (ret) err (1, "hasdata: FIONREAD error ret=%d", ret);
301 return avail > 0;
304 CAMLprim value ml_hasdata (value fd_v)
306 CAMLparam1 (fd_v);
307 int ret, avail;
309 ret = ioctl (Int_val (fd_v), FIONREAD, &avail);
310 if (ret) uerror ("ioctl (FIONREAD)", Nothing);
311 CAMLreturn (Val_bool (avail > 0));
314 static void readdata (void *p, int size)
316 ssize_t n;
318 again:
319 n = read (state.cr, p, size);
320 if (n < 0) {
321 if (errno == EINTR) goto again;
322 err (1, "read (req %d, ret %zd)", size, n);
324 if (n - size) {
325 if (!n) errx (1, "EOF while reading");
326 errx (1, "read (req %d, ret %zd)", size, n);
330 static void writedata (char *p, int size)
332 char buf[4];
333 ssize_t n;
335 buf[0] = (size >> 24) & 0xff;
336 buf[1] = (size >> 16) & 0xff;
337 buf[2] = (size >> 8) & 0xff;
338 buf[3] = (size >> 0) & 0xff;
340 n = write (state.cw, buf, 4);
341 if (n != 4) {
342 if (!n) errx (1, "EOF while writing length");
343 err (1, "write %zd", n);
346 n = write (state.cw, p, size);
347 if (n - size) {
348 if (!n) errx (1, "EOF while writing data");
349 err (1, "write (req %d, ret %zd)", size, n);
353 static int readlen (void)
355 unsigned char p[4];
357 readdata (p, 4);
358 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
361 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt, ...)
363 int size = 200, len;
364 va_list ap;
365 char *buf;
367 buf = malloc (size);
368 for (;;) {
369 if (!buf) err (1, "malloc for temp buf (%d bytes) failed", size);
371 va_start (ap, fmt);
372 len = vsnprintf (buf, size, fmt, ap);
373 va_end (ap);
375 if (len > -1 && len < size) {
376 writedata (buf, len);
377 break;
380 if (len > -1) {
381 size = len + 1;
383 else {
384 size *= 2;
386 buf = realloc (buf, size);
388 free (buf);
391 static void closepdf (void)
393 if (state.u.pdf) {
394 pdf_close_document (state.u.pdf);
395 state.u.pdf = NULL;
399 static void closexps (void)
401 if (state.u.xps) {
402 xps_close_document (state.u.xps);
403 state.u.xps = NULL;
407 static void closecbz (void)
409 if (state.u.cbz) {
410 cbz_close_document (state.u.cbz);
411 state.u.cbz = NULL;
415 static void freepdfpage (void *ptr)
417 pdf_free_page (state.u.pdf, ptr);
420 static void freeemptypdfpage (void *ptr)
422 (void) ptr;
425 static void freexpspage (void *ptr)
427 xps_free_page (state.u.xps, ptr);
430 static void freecbzpage (void *ptr)
432 cbz_free_page (state.u.cbz, ptr);
435 static void openxref (char *filename, char *password)
437 int i, len;
439 for (i = 0; i < state.texcount; ++i) {
440 state.texowners[i].w = -1;
441 state.texowners[i].slice = NULL;
444 if (state.closedoc) state.closedoc ();
446 len = strlen (filename);
448 state.type = DPDF;
449 if (len > 4) {
450 char ext[4];
452 ext[0] = tolower ((int) filename[len-3]);
453 ext[1] = tolower ((int) filename[len-2]);
454 ext[2] = tolower ((int) filename[len-1]);
456 /**/ if (ext[0] == 'x' && ext[1] == 'p' && ext[2] == 's') {
457 state.type = DXPS;
459 else if (ext[0] == 'c' && ext[1] == 'b' && ext[2] == 'z') {
460 state.type = DCBZ;
464 if (state.pagedims) {
465 free (state.pagedims);
466 state.pagedims = NULL;
468 state.pagedimcount = 0;
470 fz_set_aa_level (state.ctx, state.aalevel);
471 switch (state.type) {
472 case DPDF:
473 state.u.pdf = pdf_open_document (state.ctx, filename);
474 if (pdf_needs_password (state.u.pdf)) {
475 int okay = pdf_authenticate_password (state.u.pdf, password);
476 if (!okay) {
477 errx (1, "invalid password");
480 state.pagecount = pdf_count_pages (state.u.pdf);
481 state.closedoc = closepdf;
482 state.freepage = freepdfpage;
483 break;
485 case DXPS:
486 state.u.xps = xps_open_document (state.ctx, filename);
487 state.pagecount = xps_count_pages (state.u.xps);
488 state.closedoc = closexps;
489 state.freepage = freexpspage;
490 break;
492 case DCBZ:
493 state.u.cbz = cbz_open_document (state.ctx, filename);
494 state.pagecount = cbz_count_pages (state.u.cbz);
495 state.closedoc = closecbz;
496 state.freepage = freecbzpage;
497 break;
501 static void pdfinfo (void)
503 if (state.type == DPDF) {
504 pdf_obj *infoobj;
506 printd ("info PDF version\t%d.%d",
507 state.u.pdf->version / 10, state.u.pdf->version % 10);
509 infoobj = pdf_dict_gets (state.u.pdf->trailer, "Info");
510 if (infoobj) {
511 int i;
512 char *s;
513 char *items[] = { "Title", "Author", "Creator",
514 "Producer", "CreationDate" };
516 for (i = 0; i < sizeof (items) / sizeof (*items); ++i) {
517 pdf_obj *obj = pdf_dict_gets (infoobj, items[i]);
518 s = pdf_to_utf8 (state.u.pdf, obj);
519 if (*s) {
520 if (i == 0) {
521 printd ("title %s", s);
523 printd ("info %s\t%s", items[i], s);
525 fz_free (state.ctx, s);
528 printd ("infoend");
532 static void unlinktile (struct tile *tile)
534 int i;
536 for (i = 0; i < tile->slicecount; ++i) {
537 struct slice *s = &tile->slices[i];
539 if (s->texindex != -1) {
540 if (state.texowners[s->texindex].slice == s) {
541 state.texowners[s->texindex].slice = NULL;
547 static void freepage (struct page *page)
549 if (page->text) {
550 fz_free_text_page (state.ctx, page->text);
552 if (page->sheet) {
553 fz_free_text_sheet (state.ctx, page->sheet);
555 if (page->slinks) {
556 free (page->slinks);
558 page->freepage (page->u.ptr);
559 fz_free_display_list (state.ctx, page->dlist);
560 free (page);
563 static void freetile (struct tile *tile)
565 unlinktile (tile);
566 #ifndef PIGGYBACK
567 fz_drop_pixmap (state.ctx, tile->pixmap);
568 #else
569 if (state.pig) {
570 fz_drop_pixmap (state.ctx, state.pig);
572 state.pig = tile->pixmap;
573 #endif
574 free (tile);
577 #ifdef __ALTIVEC__
578 #include <altivec.h>
580 static int cacheline32bytes;
582 static void __attribute__ ((constructor)) clcheck (void)
584 char **envp = environ;
585 unsigned long *auxv;
587 while (*envp++);
589 for (auxv = (unsigned long *) envp; *auxv != 0; auxv += 2) {
590 if (*auxv == 19) {
591 cacheline32bytes = auxv[1] == 32;
592 return;
597 static void OPTIMIZE_ATTR (3) clearpixmap (fz_pixmap *pixmap)
599 size_t size = pixmap->w * pixmap->h * pixmap->n;
600 if (cacheline32bytes && size > 32) {
601 intptr_t a1, a2, diff;
602 size_t sizea, i;
603 vector unsigned char v = vec_splat_u8 (-1);
604 vector unsigned char *p;
606 a1 = a2 = (intptr_t) pixmap->samples;
607 a2 = (a1 + 31) & ~31;
608 diff = a2 - a1;
609 sizea = size - diff;
610 p = (void *) a2;
612 while (a1 != a2) *(char *) a1++ = 0xff;
613 for (i = 0; i < (sizea & ~31); i += 32) {
614 __asm volatile ("dcbz %0, %1"::"b"(a2),"r"(i));
615 vec_st (v, i, p);
616 vec_st (v, i + 16, p);
618 while (i < sizea) *((char *) a1 + i++) = 0xff;
620 else fz_clear_pixmap_with_value (state.ctx, pixmap, 0xff);
622 #else
623 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
624 #endif
626 static fz_matrix trimctm (pdf_page *page, int pindex)
628 fz_matrix ctm;
629 struct pagedim *pdim = &state.pagedims[pindex];
631 if (!pdim->tctmready) {
632 if (state.trimmargins) {
633 fz_rect realbox;
635 ctm = fz_concat (fz_rotate (-pdim->rotate), fz_scale (1, -1));
636 realbox = fz_transform_rect (ctm, pdim->mediabox);
637 ctm = fz_concat (ctm, fz_translate (-realbox.x0, -realbox.y0));
638 ctm = fz_concat (fz_invert_matrix (page->ctm), ctm);
640 else {
641 ctm = fz_identity;
643 pdim->tctm = ctm;
644 pdim->tctmready = 1;
646 return pdim->tctm;
649 static fz_matrix pagectm (struct page *page)
651 if (page->type == DPDF) {
652 return fz_concat (trimctm (page->u.pdfpage, page->pdimno),
653 state.pagedims[page->pdimno].ctm);
655 else {
656 fz_matrix ctm;
657 struct pagedim *pdim = &state.pagedims[page->pdimno];
659 ctm = state.pagedims[page->pdimno].ctm;
660 ctm = fz_concat (fz_translate (-pdim->mediabox.x0,
661 -pdim->mediabox.y0), ctm);
662 return ctm;
666 static void *loadpage (int pageno, int pindex)
668 fz_device *dev;
669 struct page *page = NULL;
671 page = calloc (sizeof (struct page), 1);
672 if (!page) {
673 err (1, "calloc page %d", pageno);
676 page->dlist = fz_new_display_list (state.ctx);
677 dev = fz_new_list_device (state.ctx, page->dlist);
678 switch (state.type) {
679 case DPDF:
680 fz_try (state.ctx) {
681 page->u.pdfpage = pdf_load_page (state.u.pdf, pageno);
682 pdf_run_page (state.u.pdf, page->u.pdfpage, dev, fz_identity, NULL);
683 page->freepage = freepdfpage;
685 fz_catch (state.ctx) {
686 page->u.ptr = NULL;
687 page->freepage = freeemptypdfpage;
689 break;
691 case DXPS:
692 page->u.xpspage = xps_load_page (state.u.xps, pageno);
693 xps_run_page (state.u.xps, page->u.xpspage, dev, fz_identity, NULL);
694 page->freepage = freexpspage;
695 break;
697 case DCBZ:
698 page->u.cbzpage = cbz_load_page (state.u.cbz, pageno);
699 cbz_run_page (state.u.cbz, page->u.cbzpage, dev, fz_identity, NULL);
700 page->freepage = freecbzpage;
701 break;
703 fz_free_device (dev);
705 page->pdimno = pindex;
706 page->pageno = pageno;
707 page->sgen = state.gen;
708 page->tgen = state.gen;
709 page->type = state.type;
711 return page;
714 static struct tile *alloctile (int h)
716 int i;
717 int slicecount;
718 size_t tilesize;
719 struct tile *tile;
721 slicecount = (h + state.sliceheight - 1) / state.sliceheight;
722 tilesize = sizeof (*tile) + ((slicecount - 1) * sizeof (struct slice));
723 tile = calloc (tilesize, 1);
724 if (!tile) {
725 err (1, "can not allocate tile (%" FMT_s " bytes)", tilesize);
727 for (i = 0; i < slicecount; ++i) {
728 int sh = MIN (h, state.sliceheight);
729 tile->slices[i].h = sh;
730 tile->slices[i].texindex = -1;
731 h -= sh;
733 tile->slicecount = slicecount;
734 tile->sliceheight = state.sliceheight;
735 return tile;
738 #ifdef OBSCURED_OPT
739 struct obs {
740 int cured;
741 fz_bbox b;
744 static void obs_fill_image (fz_device *dev, fz_image *image, fz_matrix ctm,
745 float alpha)
747 struct obs *obs = dev->user;
749 if (!obs->cured && fabs (1.0 - alpha) < 1e6) {
750 fz_bbox b = fz_round_rect (fz_transform_rect (ctm, fz_unit_rect));
751 b = fz_intersect_bbox (b, obs->b);
752 obs->cured = b.x0 == obs->b.x0
753 && b.x1 == obs->b.x1
754 && b.y0 == obs->b.y0
755 && b.y1 == obs->b.y1;
759 static int obscured (struct page *page, fz_bbox bbox)
761 fz_device dev;
762 struct obs obs;
764 memset (&dev, 0, sizeof (dev));
765 memset (&obs, 0, sizeof (obs));
766 dev.hints = 0;
767 dev.flags = 0;
768 dev.user = &obs;
769 dev.ctx = state.ctx;
770 dev.fill_image = obs_fill_image;
771 obs.b = bbox;
772 fz_run_display_list (page->dlist, &dev, pagectm (page), bbox, NULL);
773 return obs.cured;
775 #define OBSCURED obscured
776 #else
777 #define OBSCURED(a, b) 0
778 #endif
780 static struct tile *rendertile (struct page *page, int x, int y, int w, int h)
782 fz_bbox bbox;
783 fz_device *dev;
784 struct tile *tile;
785 struct pagedim *pdim;
787 tile = alloctile (h);
788 pdim = &state.pagedims[page->pdimno];
790 bbox = pdim->bounds;
791 bbox.x0 += x;
792 bbox.y0 += y;
793 bbox.x1 = bbox.x0 + w;
794 bbox.y1 = bbox.y0 + h;
796 if (state.pig) {
797 if (state.pig->w == w
798 && state.pig->h == h
799 && state.pig->colorspace == state.colorspace) {
800 tile->pixmap = state.pig;
801 tile->pixmap->x = bbox.x0;
802 tile->pixmap->y = bbox.y0;
804 else {
805 fz_drop_pixmap (state.ctx, state.pig);
807 state.pig = NULL;
809 if (!tile->pixmap) {
810 tile->pixmap =
811 fz_new_pixmap_with_bbox (state.ctx, state.colorspace, bbox);
814 tile->w = w;
815 tile->h = h;
816 if (!page->u.ptr || ((w < 128 && h < 128) || !OBSCURED (page, bbox))) {
817 clearpixmap (tile->pixmap);
819 dev = fz_new_draw_device (state.ctx, tile->pixmap);
820 fz_run_display_list (page->dlist, dev, pagectm (page), bbox, NULL);
821 fz_free_device (dev);
823 return tile;
826 static void initpdims (void)
828 double start, end;
829 int pageno, trim, show;
830 FILE *trimf = NULL;
831 int trimw = 0;
833 start = now ();
835 if (state.trimmargins && state.trimcachepath) {
836 trimf = fopen (state.trimcachepath, "rb");
837 if (!trimf) {
838 trimf = fopen (state.trimcachepath, "wb");
839 trimw = 1;
842 for (pageno = 0; pageno < state.pagecount; ++pageno) {
843 int rotate = 0;
844 struct pagedim *p;
845 fz_rect mediabox;
847 switch (state.type) {
848 case DPDF: {
849 pdf_obj *pageobj = state.u.pdf->page_objs[pageno];
851 if (state.trimmargins) {
852 pdf_obj *obj;
853 pdf_page *page;
855 fz_try (state.ctx) {
856 page = pdf_load_page (state.u.pdf, pageno);
857 obj = pdf_dict_gets (pageobj, "llpp.TrimBox");
858 trim = state.trimanew || !obj;
859 if (trim) {
860 fz_rect rect;
861 fz_bbox bbox;
862 fz_matrix ctm;
863 fz_device *dev;
865 dev = fz_new_bbox_device (state.ctx, &bbox);
866 dev->hints |= FZ_IGNORE_SHADE;
867 ctm = fz_invert_matrix (page->ctm);
868 pdf_run_page (state.u.pdf, page, dev, fz_identity, NULL);
869 fz_free_device (dev);
871 rect.x0 = bbox.x0 + state.trimfuzz.x0;
872 rect.x1 = bbox.x1 + state.trimfuzz.x1;
873 rect.y0 = bbox.y0 + state.trimfuzz.y0;
874 rect.y1 = bbox.y1 + state.trimfuzz.y1;
875 rect = fz_transform_rect (ctm, rect);
876 rect = fz_intersect_rect (rect, page->mediabox);
878 if (fz_is_empty_rect (rect)) {
879 mediabox = page->mediabox;
881 else {
882 mediabox = rect;
885 obj = pdf_new_array (state.ctx, 4);
886 pdf_array_push (obj, pdf_new_real (state.ctx, mediabox.x0));
887 pdf_array_push (obj, pdf_new_real (state.ctx, mediabox.y0));
888 pdf_array_push (obj, pdf_new_real (state.ctx, mediabox.x1));
889 pdf_array_push (obj, pdf_new_real (state.ctx, mediabox.y1));
890 pdf_dict_puts (pageobj, "llpp.TrimBox", obj);
892 else {
893 mediabox.x0 = pdf_to_real (pdf_array_get (obj, 0));
894 mediabox.y0 = pdf_to_real (pdf_array_get (obj, 1));
895 mediabox.x1 = pdf_to_real (pdf_array_get (obj, 2));
896 mediabox.y1 = pdf_to_real (pdf_array_get (obj, 3));
899 rotate = page->rotate;
900 pdf_free_page (state.u.pdf, page);
902 show = trim ? pageno % 5 == 0 : pageno % 20 == 0;
903 if (show) {
904 printd ("progress %f Trimming %d",
905 (double) (pageno + 1) / state.pagecount,
906 pageno + 1);
909 fz_catch (state.ctx) {
912 else {
913 fz_rect cropbox;
915 mediabox = pdf_to_rect (state.ctx,
916 pdf_dict_gets (pageobj, "MediaBox"));
917 if (fz_is_empty_rect (mediabox)) {
918 fprintf (stderr, "cannot find page size for page %d\n",
919 pageno+1);
920 mediabox.x0 = 0;
921 mediabox.y0 = 0;
922 mediabox.x1 = 612;
923 mediabox.y1 = 792;
926 cropbox = pdf_to_rect (state.ctx,
927 pdf_dict_gets (pageobj, "CropBox"));
928 if (!fz_is_empty_rect (cropbox)) {
929 mediabox = fz_intersect_rect (mediabox, cropbox);
931 rotate = pdf_to_int (pdf_dict_gets (pageobj, "Rotate"));
933 break;
936 case DXPS:
938 xps_page *page;
940 page = xps_load_page (state.u.xps, pageno);
941 mediabox = xps_bound_page (state.u.xps, page);
942 rotate = 0;
943 if (state.trimmargins) {
944 fz_rect rect;
945 fz_bbox bbox;
946 fz_device *dev;
948 dev = fz_new_bbox_device (state.ctx, &bbox);
949 dev->hints |= FZ_IGNORE_SHADE;
950 xps_run_page (state.u.xps, page, dev, fz_identity, NULL);
951 fz_free_device (dev);
953 rect.x0 = bbox.x0 + state.trimfuzz.x0;
954 rect.x1 = bbox.x1 + state.trimfuzz.x1;
955 rect.y0 = bbox.y0 + state.trimfuzz.y0;
956 rect.y1 = bbox.y1 + state.trimfuzz.y1;
957 rect = fz_intersect_rect (rect, mediabox);
959 if (!fz_is_empty_rect (rect)) {
960 mediabox = rect;
963 xps_free_page (state.u.xps, page);
964 printd ("progress %f loading %d",
965 (double) (pageno + 1) / state.pagecount,
966 pageno + 1);
968 break;
970 case DCBZ:
972 rotate = 0;
973 if (state.trimmargins && trimw) {
974 cbz_page *page;
976 page = cbz_load_page (state.u.cbz, pageno);
977 mediabox = cbz_bound_page (state.u.cbz, page);
978 cbz_free_page (state.u.cbz, page);
979 printd ("progress %f Trimming %d",
980 (double) (pageno + 1) / state.pagecount,
981 pageno + 1);
982 if (trimf) {
983 int n = fwrite (&mediabox, sizeof (mediabox), 1, trimf);
984 if (n - 1) {
985 err (1, "fwrite trim mediabox");
989 else {
990 if (trimf) {
991 int n = fread (&mediabox, sizeof (mediabox), 1, trimf);
992 if (n - 1) {
993 err (1, "fread trim mediabox");
996 else {
997 mediabox.x0 = mediabox.y0 = 0;
998 mediabox.x1 = 900;
999 mediabox.y1 = 900;
1003 break;
1005 default:
1006 ARSERT (0 && state.type);
1009 if (state.pagedimcount == 0
1010 || (p = &state.pagedims[state.pagedimcount-1], p->rotate != rotate)
1011 || memcmp (&p->mediabox, &mediabox, sizeof (mediabox))) {
1012 size_t size;
1014 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
1015 state.pagedims = realloc (state.pagedims, size);
1016 if (!state.pagedims) {
1017 err (1, "realloc pagedims to %" FMT_s " (%d elems)",
1018 size, state.pagedimcount + 1);
1021 p = &state.pagedims[state.pagedimcount++];
1022 p->rotate = rotate;
1023 p->mediabox = mediabox;
1024 p->pageno = pageno;
1027 end = now ();
1028 if (state.trimmargins) {
1029 printd ("progress 1 Trimmed %d pages in %f seconds",
1030 state.pagecount, end - start);
1032 else {
1033 printd ("vmsg Processed %d pages in %f seconds",
1034 state.pagecount, end - start);
1036 state.trimanew = 0;
1037 if (trimf) {
1038 if (fclose (trimf)) {
1039 err (1, "fclose");
1044 static void layout (void)
1046 int pindex;
1047 fz_rect box;
1048 fz_matrix ctm;
1049 double zoom, w, maxw = 0;
1050 struct pagedim *p = state.pagedims;
1052 if (state.proportional) {
1053 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
1054 box = fz_transform_rect (fz_rotate (p->rotate + state.rotate),
1055 p->mediabox);
1056 w = box.x1 - box.x0;
1057 maxw = MAX (w, maxw);
1061 p = state.pagedims;
1062 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
1063 fz_bbox bbox;
1065 ctm = fz_rotate (state.rotate);
1066 box = fz_transform_rect (fz_rotate (p->rotate + state.rotate),
1067 p->mediabox);
1068 w = box.x1 - box.x0;
1070 if (state.proportional) {
1071 double scale = w / maxw;
1072 zoom = (state.w / w) * scale;
1074 else {
1075 zoom = state.w / w;
1078 p->zoomctm = fz_scale (zoom, zoom);
1079 ctm = fz_concat (p->zoomctm, ctm);
1081 p->pagebox = fz_transform_rect (fz_rotate (p->rotate), p->mediabox);
1082 p->pagebox.x1 -= p->pagebox.x0;
1083 p->pagebox.y1 -= p->pagebox.y0;
1084 p->pagebox.x0 = 0;
1085 p->pagebox.y0 = 0;
1086 bbox = fz_round_rect (fz_transform_rect (ctm, p->pagebox));
1088 p->bounds = bbox;
1089 p->left = state.proportional ? ((maxw - w) * zoom) / 2.0 : 0;
1090 p->ctm = ctm;
1092 ctm = fz_identity;
1093 ctm = fz_concat (ctm, fz_translate (0, -p->mediabox.y1));
1094 ctm = fz_concat (ctm, fz_scale (zoom, -zoom));
1095 ctm = fz_concat (ctm, fz_rotate (p->rotate + state.rotate));
1096 p->lctm = ctm;
1098 p->tctmready = 0;
1101 while (p-- != state.pagedims) {
1102 int x0 = MIN (p->bounds.x0, p->bounds.x1);
1103 int y0 = MIN (p->bounds.y0, p->bounds.y1);
1104 int x1 = MAX (p->bounds.x0, p->bounds.x1);
1105 int y1 = MAX (p->bounds.y0, p->bounds.y1);
1106 int w = x1 - x0;
1107 int h = y1 - y0;
1109 printd ("pdim %d %d %d %d", p->pageno, w, h, p->left);
1113 static void recurse_outline (fz_outline *outline, int level)
1115 while (outline) {
1116 fz_link_dest *dest;
1117 int i, top = 0;
1118 struct pagedim *pdim = state.pagedims;
1120 dest = &outline->dest;
1121 for (i = 0; i < state.pagedimcount; ++i) {
1122 if (state.pagedims[i].pageno > dest->ld.gotor.page)
1123 break;
1124 pdim = &state.pagedims[i];
1126 if (dest->ld.gotor.flags & fz_link_flag_t_valid) {
1127 fz_point p;
1128 p.x = 0;
1129 p.y = dest->ld.gotor.lt.y;
1130 p = fz_transform_point (pdim->lctm, p);
1131 top = p.y;
1133 if (dest->ld.gotor.page >= 0 && dest->ld.gotor.page < 1<<30) {
1134 int h;
1135 double y0, y1;
1137 y0 = MIN (pdim->bounds.y0, pdim->bounds.y1);
1138 y1 = MAX (pdim->bounds.y0, pdim->bounds.y1);
1139 h = y1 - y0;
1140 printd ("o %d %d %d %d %s",
1141 level, dest->ld.gotor.page, top, h, outline->title);
1143 if (outline->down) {
1144 recurse_outline (outline->down, level + 1);
1146 outline = outline->next;
1150 static void process_outline (void)
1152 fz_outline *outline;
1154 if (!state.needoutline) return;
1156 state.needoutline = 0;
1157 switch (state.type) {
1158 case DPDF:
1159 outline = pdf_load_outline (state.u.pdf);
1160 break;
1161 case DXPS:
1162 outline = xps_load_outline (state.u.xps);
1163 break;
1164 default:
1165 outline = NULL;
1166 break;
1168 if (outline) {
1169 recurse_outline (outline, 0);
1170 fz_free_outline (state.ctx, outline);
1174 static char *strofspan (fz_text_span *span)
1176 char *p;
1177 char utf8[10];
1178 fz_text_char *ch;
1179 size_t size = 0, cap = 80;
1181 p = malloc (cap + 1);
1182 if (!p) return NULL;
1184 for (ch = span->text; ch < span->text + span->len; ++ch) {
1185 int n = fz_runetochar (utf8, ch->c);
1186 if (size + n > cap) {
1187 cap *= 2;
1188 p = realloc (p, cap + 1);
1189 if (!p) return NULL;
1192 memcpy (p + size, utf8, n);
1193 size += n;
1195 p[size] = 0;
1196 return p;
1199 static int matchspan (regex_t *re, fz_text_span *span, fz_matrix ctm,
1200 int stop, int pageno, double start)
1202 int ret;
1203 char *p;
1204 regmatch_t rm;
1205 int a, b, c;
1206 fz_rect *sb, *eb;
1207 fz_point p1, p2, p3, p4;
1209 p = strofspan (span);
1210 if (!p) return -1;
1212 ret = regexec (re, p, 1, &rm, 0);
1213 if (ret) {
1214 free (p);
1215 if (ret != REG_NOMATCH) {
1216 size_t size;
1217 char errbuf[80];
1218 size = regerror (ret, re, errbuf, sizeof (errbuf));
1219 printd ("msg regexec error `%.*s'",
1220 (int) size, errbuf);
1221 return -1;
1223 return 0;
1225 else {
1226 int l = span->len;
1227 for (a = 0, c = 0; c < rm.rm_so && a < l; a++) {
1228 c += fz_runelen (span->text[a].c);
1230 for (b = a; c < rm.rm_eo - 1 && b < l; b++) {
1231 c += fz_runelen (span->text[b].c);
1234 if (fz_runelen (span->text[b].c) > 1) {
1235 b = MAX (0, b-1);
1237 sb = &span->text[MIN (a, l-1)].bbox;
1238 eb = &span->text[MIN (b, l-1)].bbox;
1240 p1.x = sb->x0;
1241 p1.y = sb->y0;
1242 p2.x = eb->x1;
1243 p2.y = sb->y0;
1244 p3.x = eb->x1;
1245 p3.y = eb->y1;
1246 p4.x = sb->x0;
1247 p4.y = eb->y1;
1249 if (!stop) {
1250 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1251 pageno, 1,
1252 p1.x, p1.y,
1253 p2.x, p2.y,
1254 p3.x, p3.y,
1255 p4.x, p4.y);
1257 printd ("progress 1 found at %d `%.*s' in %f sec",
1258 pageno + 1, (int) (rm.rm_eo - rm.rm_so), &p[rm.rm_so],
1259 now () - start);
1261 else {
1262 printd ("match %d %d %f %f %f %f %f %f %f %f",
1263 pageno, 2,
1264 p1.x, p1.y,
1265 p2.x, p2.y,
1266 p3.x, p3.y,
1267 p4.x, p4.y);
1269 free (p);
1270 return 1;
1274 static int compareblocks (const void *l, const void *r)
1276 fz_text_block const *ls = l;
1277 fz_text_block const* rs = r;
1278 return ls->bbox.y0 - rs->bbox.y0;
1281 /* wishful thinking function */
1282 static void search (regex_t *re, int pageno, int y, int forward)
1284 int i, j;
1285 fz_matrix ctm;
1286 fz_device *tdev;
1287 union { void *ptr; pdf_page *pdfpage; xps_page *xpspage; } u;
1288 fz_text_page *text;
1289 fz_text_sheet *sheet;
1290 struct pagedim *pdim, *pdimprev;
1291 int stop = 0, niters = 0;
1292 double start, end;
1294 if (!(state.type == DPDF || state.type == DXPS))
1295 return;
1297 start = now ();
1298 while (pageno >= 0 && pageno < state.pagecount && !stop) {
1299 if (niters++ == 5) {
1300 niters = 0;
1301 if (hasdata ()) {
1302 printd ("progress 1 attention requested aborting search at %d",
1303 pageno);
1304 stop = 1;
1306 else {
1307 printd ("progress %f searching in page %d",
1308 (double) (pageno + 1) / state.pagecount,
1309 pageno);
1312 pdimprev = NULL;
1313 for (i = 0; i < state.pagedimcount; ++i) {
1314 pdim = &state.pagedims[i];
1315 if (pdim->pageno == pageno) {
1316 goto found;
1318 if (pdim->pageno > pageno) {
1319 pdim = pdimprev;
1320 goto found;
1322 pdimprev = pdim;
1324 pdim = pdimprev;
1325 found:
1327 sheet = fz_new_text_sheet (state.ctx);
1328 text = fz_new_text_page (state.ctx, fz_infinite_rect);
1329 tdev = fz_new_text_device (state.ctx, sheet, text);
1331 switch (state.type) {
1332 case DPDF:
1333 u.ptr = NULL;
1334 fz_try (state.ctx) {
1335 u.pdfpage = pdf_load_page (state.u.pdf, pageno);
1336 trimctm (u.pdfpage, pdim - state.pagedims);
1337 ctm = fz_concat (pdim->tctm, pdim->zoomctm);
1338 pdf_run_page (state.u.pdf, u.pdfpage, tdev, ctm, NULL);
1340 fz_catch (state.ctx) {
1341 fz_free_device (tdev);
1342 u.ptr = NULL;
1343 goto nextiter;
1345 break;
1347 case DXPS:
1348 u.xpspage = xps_load_page (state.u.xps, pageno);
1349 ctm = pdim->ctm;
1350 xps_run_page (state.u.xps, u.xpspage, tdev, ctm, NULL);
1351 break;
1353 default:
1354 ARSERT (0 && state.type);
1357 qsort (text->blocks, text->len, sizeof (*text->blocks), compareblocks);
1358 fz_free_device (tdev);
1360 for (j = 0; j < text->len; ++j) {
1361 int k;
1362 fz_text_block *block;
1364 block = &text->blocks[forward ? j : text->len - 1 - j];
1366 for (k = 0; k < block->len; ++k) {
1367 fz_text_line *line;
1368 fz_text_span *span;
1370 if (forward) {
1371 line = &block->lines[k];
1372 if (line->bbox.y0 < y + 1) continue;
1374 else {
1375 line = &block->lines[block->len - 1 - k];
1376 if (line->bbox.y0 > y - 1) continue;
1379 for (span = line->spans;
1380 span < line->spans + line->len;
1381 ++span) {
1383 switch (matchspan (re, span, ctm, stop, pageno, start)) {
1384 case 0: break;
1385 case 1: stop = 1; break;
1386 case -1: stop = 1; goto endloop;
1391 nextiter:
1392 if (forward) {
1393 pageno += 1;
1394 y = 0;
1396 else {
1397 pageno -= 1;
1398 y = INT_MAX;
1400 endloop:
1401 fz_free_text_page (state.ctx, text);
1402 fz_free_text_sheet (state.ctx, sheet);
1403 if (u.ptr) {
1404 state.freepage (u.ptr);
1407 end = now ();
1408 if (!stop) {
1409 printd ("progress 1 no matches %f sec", end - start);
1411 printd ("clearrects");
1414 static void set_tex_params (int colorspace)
1416 union {
1417 unsigned char b;
1418 unsigned int s;
1419 } endianness = {1};
1421 switch (colorspace) {
1422 case 0:
1423 state.texiform = GL_RGBA8;
1424 state.texform = GL_RGBA;
1425 state.texty = GL_UNSIGNED_BYTE;
1426 state.colorspace = fz_device_rgb;
1427 break;
1428 case 1:
1429 state.texiform = GL_RGBA8;
1430 state.texform = GL_BGRA;
1431 state.texty = endianness.s > 1
1432 ? GL_UNSIGNED_INT_8_8_8_8
1433 : GL_UNSIGNED_INT_8_8_8_8_REV;
1434 state.colorspace = fz_device_bgr;
1435 break;
1436 case 2:
1437 state.texiform = GL_LUMINANCE_ALPHA;
1438 state.texform = GL_LUMINANCE_ALPHA;
1439 state.texty = GL_UNSIGNED_BYTE;
1440 state.colorspace = fz_device_gray;
1441 break;
1442 default:
1443 errx (1, "invalid colorspce %d", colorspace);
1447 static void realloctexts (int texcount)
1449 size_t size;
1451 if (texcount == state.texcount) return;
1453 if (texcount < state.texcount) {
1454 glDeleteTextures (state.texcount - texcount,
1455 state.texids + texcount);
1458 size = texcount * sizeof (*state.texids);
1459 state.texids = realloc (state.texids, size);
1460 if (!state.texids) {
1461 err (1, "realloc texids %" FMT_s, size);
1464 size = texcount * sizeof (*state.texowners);
1465 state.texowners = realloc (state.texowners, size);
1466 if (!state.texowners) {
1467 err (1, "realloc texowners %" FMT_s, size);
1469 if (texcount > state.texcount) {
1470 int i;
1472 glGenTextures (texcount - state.texcount,
1473 state.texids + state.texcount);
1474 for (i = state.texcount; i < texcount; ++i) {
1475 state.texowners[i].w = -1;
1476 state.texowners[i].slice = NULL;
1479 state.texcount = texcount;
1480 state.texindex = 0;
1483 static void * mainloop (void *unused)
1485 char *p = NULL;
1486 int len, ret, oldlen = 0;
1488 for (;;) {
1489 len = readlen ();
1490 if (len == 0) {
1491 errx (1, "readlen returned 0");
1494 if (oldlen < len + 1) {
1495 p = realloc (p, len + 1);
1496 if (!p) {
1497 err (1, "realloc %d failed", len + 1);
1499 oldlen = len + 1;
1501 readdata (p, len);
1502 p[len] = 0;
1504 if (!strncmp ("open", p, 4)) {
1505 size_t filenamelen;
1506 char *password;
1507 char *filename = p + 5;
1509 filenamelen = strlen (filename);
1510 password = filename + filenamelen + 1;
1512 openxref (filename, password);
1513 pdfinfo ();
1514 initpdims ();
1515 printd ("msg Opened %s (press h/F1 to get help)", filename);
1516 state.needoutline = 1;
1518 else if (!strncmp ("cs", p, 2)) {
1519 int i, colorspace;
1521 ret = sscanf (p + 2, " %d", &colorspace);
1522 if (ret != 1) {
1523 errx (1, "malformed cs `%.*s' ret=%d", len, p, ret);
1525 lock ("cs");
1526 set_tex_params (colorspace);
1527 for (i = 0; i < state.texcount; ++i) {
1528 state.texowners[i].w = -1;
1529 state.texowners[i].slice = NULL;
1531 unlock ("cs");
1533 else if (!strncmp ("freepage", p, 8)) {
1534 void *ptr;
1536 ret = sscanf (p + 8, " %" FMT_ptr, FMT_ptr_cast (&ptr));
1537 if (ret != 1) {
1538 errx (1, "malformed freepage `%.*s' ret=%d", len, p, ret);
1540 freepage (ptr);
1542 else if (!strncmp ("freetile", p, 8)) {
1543 void *ptr;
1545 ret = sscanf (p + 8, " %" FMT_ptr, FMT_ptr_cast (&ptr));
1546 if (ret != 1) {
1547 errx (1, "malformed freetile `%.*s' ret=%d", len, p, ret);
1549 freetile (ptr);
1551 else if (!strncmp ("search", p, 6)) {
1552 int icase, pageno, y, ret, len2, forward;
1553 char *pattern;
1554 regex_t re;
1556 ret = sscanf (p + 6, " %d %d %d %d,%n",
1557 &icase, &pageno, &y, &forward, &len2);
1558 if (ret != 4) {
1559 errx (1, "malformed search `%s' ret=%d", p, ret);
1562 pattern = p + 6 + len2;
1563 ret = regcomp (&re, pattern,
1564 REG_EXTENDED | (icase ? REG_ICASE : 0));
1565 if (ret) {
1566 char errbuf[80];
1567 size_t size;
1569 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1570 printd ("msg regcomp failed `%.*s'", (int) size, errbuf);
1572 else {
1573 search (&re, pageno, y, forward);
1574 regfree (&re);
1577 else if (!strncmp ("geometry", p, 8)) {
1578 int w, h;
1580 printd ("clear");
1581 ret = sscanf (p + 8, " %d %d", &w, &h);
1582 if (ret != 2) {
1583 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1586 lock ("geometry");
1587 state.h = h;
1588 if (w != state.w) {
1589 int i;
1590 state.w = w;
1591 for (i = 0; i < state.texcount; ++i) {
1592 state.texowners[i].slice = NULL;
1595 layout ();
1596 process_outline ();
1597 state.gen++;
1598 unlock ("geometry");
1599 printd ("continue %d", state.pagecount);
1601 else if (!strncmp ("reqlayout", p, 9)) {
1602 int rotate, proportional;
1604 printd ("clear");
1605 ret = sscanf (p + 9, " %d %d", &rotate, &proportional);
1606 if (ret != 2) {
1607 errx (1, "bad reqlayout line `%.*s' ret=%d", len, p, ret);
1609 lock ("reqlayout");
1610 if (state.rotate != rotate || state.proportional != proportional) {
1611 state.gen += 1;
1613 state.rotate = rotate;
1614 state.proportional = proportional;
1615 layout ();
1616 process_outline ();
1617 state.gen++;
1618 unlock ("reqlayout");
1619 printd ("continue %d", state.pagecount);
1621 else if (!strncmp ("page", p, 4)) {
1622 double a, b;
1623 struct page *page;
1624 int pageno, pindex, ret;
1626 ret = sscanf (p + 4, " %d %d", &pageno, &pindex);
1627 if (ret != 2) {
1628 errx (1, "bad render line `%.*s' ret=%d", len, p, ret);
1631 lock ("page");
1632 a = now ();
1633 page = loadpage (pageno, pindex);
1634 b = now ();
1635 unlock ("page");
1637 printd ("page %" FMT_ptr " %f", FMT_ptr_cast2 (page), b - a);
1639 else if (!strncmp ("tile", p, 4)) {
1640 int x, y, w, h, ret;
1641 struct page *page;
1642 struct tile *tile;
1643 double a, b;
1645 ret = sscanf (p + 4, " %" FMT_ptr " %d %d %d %d",
1646 FMT_ptr_cast (&page), &x, &y, &w, &h);
1647 if (ret != 5) {
1648 errx (1, "bad tile line `%.*s' ret=%d", len, p, ret);
1651 lock ("tile");
1652 a = now ();
1653 tile = rendertile (page, x, y, w, h);
1654 b = now ();
1655 unlock ("tile");
1657 printd ("tile %d %d %" FMT_ptr " %u %f",
1658 x, y,
1659 FMT_ptr_cast2 (tile),
1660 tile->w * tile->h * tile->pixmap->n,
1661 b - a);
1663 else if (!strncmp ("settrim", p, 7)) {
1664 int trimmargins;
1665 fz_bbox fuzz;
1667 ret = sscanf (p + 7, " %d %d %d %d %d", &trimmargins,
1668 &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1669 if (ret != 5) {
1670 errx (1, "malformed settrim `%.*s' ret=%d", len, p, ret);
1672 printd ("clear");
1673 lock ("settrim");
1674 state.trimmargins = trimmargins;
1675 state.needoutline = 1;
1676 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1677 state.trimanew = 1;
1678 state.trimfuzz = fuzz;
1680 state.pagedimcount = 0;
1681 free (state.pagedims);
1682 state.pagedims = NULL;
1683 initpdims ();
1684 layout ();
1685 process_outline ();
1686 unlock ("settrim");
1687 printd ("continue %d", state.pagecount);
1689 else if (!strncmp ("sliceh", p, 6)) {
1690 int h;
1692 ret = sscanf (p + 6, " %d", &h);
1693 if (ret != 1) {
1694 errx (1, "malformed sliceh `%.*s' ret=%d", len, p, ret);
1696 if (h != state.sliceheight) {
1697 int i;
1699 state.sliceheight = h;
1700 for (i = 0; i < state.texcount; ++i) {
1701 state.texowners[i].w = -1;
1702 state.texowners[i].h = -1;
1703 state.texowners[i].slice = NULL;
1707 else if (!strncmp ("interrupt", p, 9)) {
1708 printd ("vmsg interrupted");
1710 else {
1711 errx (1, "unknown command %.*s", len, p);
1714 return 0;
1717 CAMLprim value ml_realloctexts (value texcount_v)
1719 CAMLparam1 (texcount_v);
1720 int ok;
1722 if (trylock ("ml_realloctexts")) {
1723 ok = 0;
1724 goto done;
1726 realloctexts (Int_val (texcount_v));
1727 ok = 1;
1728 unlock ("ml_realloctexts");
1730 done:
1731 CAMLreturn (Val_bool (ok));
1734 static void showsel (struct page *page, int ox, int oy)
1736 int seen = 0;
1737 fz_bbox bbox;
1738 fz_text_line *line;
1739 fz_text_span *span;
1740 fz_text_block *block;
1741 struct mark first, last;
1743 first = page->fmark;
1744 last = page->lmark;
1746 if (!first.span || !last.span) return;
1748 glEnable (GL_BLEND);
1749 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1750 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1752 ox += state.pagedims[page->pdimno].bounds.x0;
1753 oy += state.pagedims[page->pdimno].bounds.y0;
1754 for (block = page->text->blocks;
1755 block < page->text->blocks + page->text->len;
1756 ++block) {
1757 for (line = block->lines;
1758 line < block->lines + block->len;
1759 ++line) {
1760 for (span = line->spans;
1761 span < line->spans + line->len;
1762 ++span) {
1763 int i, j, k;
1765 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0;
1767 j = 0;
1768 k = span->len - 1;
1770 if (span == page->fmark.span && span == page->lmark.span) {
1771 seen = 1;
1772 j = MIN (first.i, last.i);
1773 k = MAX (first.i, last.i);
1775 else if (span == first.span) {
1776 seen = 1;
1777 j = first.i;
1779 else if (span == last.span) {
1780 seen = 1;
1781 k = last.i;
1784 if (seen) {
1785 for (i = j; i <= k; ++i) {
1786 fz_bbox b = fz_round_rect (span->text[i].bbox);
1787 bbox = fz_union_bbox (bbox, b);
1789 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1790 bbox.x0,
1791 bbox.y0,
1792 bbox.x1,
1793 bbox.y1,
1794 oy, ox);
1796 glRecti (bbox.x0 + ox, bbox.y0 + oy,
1797 bbox.x1 + ox, bbox.y1 + oy);
1798 if (span == last.span) {
1799 goto done;
1805 done:
1806 glDisable (GL_BLEND);
1809 #include "glfont.c"
1811 static void highlightlinks (struct page *page, int xoff, int yoff)
1813 fz_matrix ctm;
1814 fz_link *link, *links;
1816 switch (page->type) {
1817 case DPDF:
1818 links = page->u.pdfpage->links;
1819 break;
1821 case DXPS:
1822 links = page->u.xpspage->links;
1823 break;
1825 default:
1826 return;
1829 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1830 glEnable (GL_LINE_STIPPLE);
1831 glLineStipple (0.5, 0xcccc);
1833 xoff -= state.pagedims[page->pdimno].bounds.x0;
1834 yoff -= state.pagedims[page->pdimno].bounds.y0;
1835 ctm = fz_concat (pagectm (page), fz_translate (xoff, yoff));
1837 glBegin (GL_QUADS);
1838 for (link = links; link; link = link->next) {
1839 fz_point p1, p2, p3, p4;
1841 p1.x = link->rect.x0;
1842 p1.y = link->rect.y0;
1844 p2.x = link->rect.x1;
1845 p2.y = link->rect.y0;
1847 p3.x = link->rect.x1;
1848 p3.y = link->rect.y1;
1850 p4.x = link->rect.x0;
1851 p4.y = link->rect.y1;
1853 p1 = fz_transform_point (ctm, p1);
1854 p2 = fz_transform_point (ctm, p2);
1855 p3 = fz_transform_point (ctm, p3);
1856 p4 = fz_transform_point (ctm, p4);
1858 switch (link->dest.kind) {
1859 case FZ_LINK_GOTO: glColor3ub (255, 0, 0); break;
1860 case FZ_LINK_URI: glColor3ub (0, 0, 255); break;
1861 default: glColor3ub (0, 0, 0); break;
1864 glVertex2f (p1.x, p1.y);
1865 glVertex2f (p2.x, p2.y);
1866 glVertex2f (p3.x, p3.y);
1867 glVertex2f (p4.x, p4.y);
1869 glEnd ();
1871 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1872 glDisable (GL_LINE_STIPPLE);
1875 static int compareslinks (const void *l, const void *r)
1877 struct slink const *ls = l;
1878 struct slink const *rs = r;
1879 if (ls->bbox.y0 == rs->bbox.y0) {
1880 return rs->bbox.x0 - rs->bbox.x0;
1882 return ls->bbox.y0 - rs->bbox.y0;
1885 static void droptext (struct page *page)
1887 if (page->text) {
1888 fz_free_text_page (state.ctx, page->text);
1889 page->fmark.i = -1;
1890 page->lmark.i = -1;
1891 page->fmark.span = NULL;
1892 page->lmark.span = NULL;
1893 page->text = NULL;
1895 if (page->sheet) {
1896 fz_free_text_sheet (state.ctx, page->sheet);
1900 static void dropslinks (struct page *page)
1902 if (page->slinks) {
1903 free (page->slinks);
1904 page->slinks = NULL;
1905 page->slinkcount = 0;
1909 static void ensureslinks (struct page *page)
1911 fz_matrix ctm;
1912 int i, count = 0;
1913 size_t slinksize = sizeof (*page->slinks);
1914 fz_link *link, *links;
1916 if (state.gen != page->sgen) {
1917 dropslinks (page);
1918 page->sgen = state.gen;
1920 if (page->slinks) return;
1922 switch (page->type) {
1923 case DPDF:
1924 links = page->u.pdfpage->links;
1925 ctm = fz_concat (trimctm (page->u.pdfpage, page->pdimno),
1926 state.pagedims[page->pdimno].ctm);
1927 break;
1929 case DXPS:
1930 links = page->u.xpspage->links;
1931 ctm = state.pagedims[page->pdimno].ctm;
1932 break;
1934 default:
1935 return;
1938 for (link = links; link; link = link->next) {
1939 count++;
1941 if (count > 0) {
1942 page->slinkcount = count;
1943 page->slinks = calloc (count, slinksize);
1944 if (!page->slinks) {
1945 err (1, "realloc slinks %d", count);
1948 for (i = 0, link = links; link; ++i, link = link->next) {
1949 page->slinks[i].link = link;
1950 page->slinks[i].bbox =
1951 fz_round_rect (fz_transform_rect (ctm, link->rect));
1953 qsort (page->slinks, count, slinksize, compareslinks);
1957 /* slightly tweaked fmt_ulong by D.J. Bernstein */
1958 static void fmt_linkn (char *s, unsigned int u)
1960 unsigned int len; unsigned int q;
1961 int zma = 'z' - 'a' + 1;
1962 len = 1; q = u;
1963 while (q > zma - 1) { ++len; q /= zma; }
1964 if (s) {
1965 s += len;
1966 do { *--s = 'a' + (u % zma) - (u < zma && len > 1); u /= zma; } while(u);
1967 /* handles u == 0 */
1969 s[len] = 0;
1972 static void highlightslinks (struct page *page, int xoff, int yoff,
1973 int noff, char *targ, int tlen, int hfsize)
1975 int i;
1976 char buf[40];
1977 struct slink *slink;
1978 double x0, y0, x1, y1, w;
1980 ensureslinks (page);
1981 glColor3ub (0xc3, 0xb0, 0x91);
1982 for (i = 0; i < page->slinkcount; ++i) {
1983 fmt_linkn (buf, i + noff);
1984 if (!tlen || !strncmp (targ, buf, tlen)) {
1985 slink = &page->slinks[i];
1987 x0 = slink->bbox.x0 + xoff - 5;
1988 y1 = slink->bbox.y0 + yoff - 5;
1989 y0 = y1 + 10 + hfsize;
1990 w = measure_string (state.face, hfsize, buf);
1991 x1 = x0 + w + 10;
1992 glRectd (x0, y0, x1, y1);
1996 glEnable (GL_BLEND);
1997 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1998 glEnable (GL_TEXTURE_2D);
1999 glColor3ub (0, 0, 0);
2000 for (i = 0; i < page->slinkcount; ++i) {
2001 fmt_linkn (buf, i + noff);
2002 if (!tlen || !strncmp (targ, buf, tlen)) {
2003 slink = &page->slinks[i];
2005 x0 = slink->bbox.x0 + xoff;
2006 y0 = slink->bbox.y0 + yoff + hfsize;
2007 draw_string (state.face, hfsize, x0, y0, buf);
2010 glDisable (GL_TEXTURE_2D);
2011 glDisable (GL_BLEND);
2015 static void uploadslice (struct tile *tile, struct slice *slice)
2017 int offset;
2018 struct slice *slice1;
2020 offset = 0;
2021 for (slice1 = tile->slices; slice != slice1; slice1++) {
2022 offset += slice1->h * tile->w * tile->pixmap->n;
2024 if (slice->texindex != -1 && slice->texindex < state.texcount
2025 && state.texowners[slice->texindex].slice == slice) {
2026 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
2028 else {
2029 int subimage = 0;
2030 int texindex = state.texindex++ % state.texcount;
2032 if (state.texowners[texindex].w == tile->w) {
2033 if (state.texowners[texindex].h >= slice->h) {
2034 subimage = 1;
2036 else {
2037 state.texowners[texindex].h = slice->h;
2040 else {
2041 state.texowners[texindex].h = slice->h;
2044 state.texowners[texindex].w = tile->w;
2045 state.texowners[texindex].slice = slice;
2046 slice->texindex = texindex;
2048 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[texindex]);
2049 if (subimage) {
2050 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
2054 tile->w,
2055 slice->h,
2056 state.texform,
2057 state.texty,
2058 tile->pixmap->samples+offset
2061 else {
2062 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
2064 state.texiform,
2065 tile->w,
2066 slice->h,
2068 state.texform,
2069 state.texty,
2070 tile->pixmap->samples+offset
2076 CAMLprim value ml_drawtile (value args_v, value ptr_v)
2078 CAMLparam2 (args_v, ptr_v);
2079 int dispx = Int_val (Field (args_v, 0));
2080 int dispy = Int_val (Field (args_v, 1));
2081 int dispw = Int_val (Field (args_v, 2));
2082 int disph = Int_val (Field (args_v, 3));
2083 int tilex = Int_val (Field (args_v, 4));
2084 int tiley = Int_val (Field (args_v, 5));
2085 char *s = String_val (ptr_v);
2086 struct tile *tile = parse_pointer ("ml_drawtile", s);
2088 glEnable (GL_TEXTURE_RECTANGLE_ARB);
2090 int slicey, firstslice;
2091 struct slice *slice;
2093 firstslice = tiley / tile->sliceheight;
2094 slice = &tile->slices[firstslice];
2095 slicey = tiley % tile->sliceheight;
2097 while (disph > 0) {
2098 int dh;
2100 dh = slice->h - slicey;
2101 dh = MIN (disph, dh);
2102 uploadslice (tile, slice);
2104 glBegin (GL_QUADS);
2106 glTexCoord2i (tilex, slicey);
2107 glVertex2i (dispx, dispy);
2109 glTexCoord2i (tilex+dispw, slicey);
2110 glVertex2i (dispx+dispw, dispy);
2112 glTexCoord2i (tilex+dispw, slicey+dh);
2113 glVertex2i (dispx+dispw, dispy+dh);
2115 glTexCoord2i (tilex, slicey+dh);
2116 glVertex2i (dispx, dispy+dh);
2118 glEnd ();
2120 dispy += dh;
2121 disph -= dh;
2122 slice++;
2123 ARSERT (!(slice - tile->slices >= tile->slicecount && disph > 0));
2124 slicey = 0;
2127 glDisable (GL_TEXTURE_RECTANGLE_ARB);
2128 CAMLreturn (Val_unit);
2131 CAMLprim value ml_postprocess (value ptr_v, value hlinks_v,
2132 value xoff_v, value yoff_v,
2133 value li_v)
2135 CAMLparam5 (ptr_v, hlinks_v, xoff_v, yoff_v, li_v);
2136 int xoff = Int_val (xoff_v);
2137 int yoff = Int_val (yoff_v);
2138 int noff = Int_val (Field (li_v, 0));
2139 char *targ = String_val (Field (li_v, 1));
2140 int tlen = caml_string_length (Field (li_v, 1));
2141 int hfsize = Int_val (Field (li_v, 2));
2142 char *s = String_val (ptr_v);
2143 int hlmask = Int_val (hlinks_v);
2144 struct page *page = parse_pointer ("ml_postprocess", s);
2146 if (!page->u.ptr) {
2147 /* deal with loadpage failed pages */
2148 goto done;
2151 if (hlmask & 1) highlightlinks (page, xoff, yoff);
2152 if (trylock ("ml_postprocess")) {
2153 noff = 0;
2154 goto done;
2156 if (hlmask & 2) {
2157 highlightslinks (page, xoff, yoff, noff, targ, tlen, hfsize);
2158 noff = page->slinkcount;
2160 showsel (page, xoff, yoff);
2161 unlock ("ml_postprocess");
2163 done:
2164 CAMLreturn (Val_int (noff));
2167 static fz_link *getlink (struct page *page, int x, int y)
2169 fz_point p;
2170 fz_matrix ctm;
2171 fz_link *link, *links;
2173 switch (page->type) {
2174 case DPDF:
2175 ctm = trimctm (page->u.pdfpage, page->pdimno);
2176 links = page->u.pdfpage->links;
2177 break;
2179 case DXPS:
2180 ctm = fz_identity;
2181 links = page->u.xpspage->links;
2182 break;
2184 default:
2185 return NULL;
2187 p.x = x;
2188 p.y = y;
2190 ctm = fz_concat (ctm, state.pagedims[page->pdimno].ctm);
2191 ctm = fz_invert_matrix (ctm);
2192 p = fz_transform_point (ctm, p);
2194 for (link = links; link; link = link->next) {
2195 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
2196 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
2197 return link;
2201 return NULL;
2204 static void ensuretext (struct page *page)
2206 if (state.gen != page->tgen) {
2207 droptext (page);
2208 page->tgen = state.gen;
2210 if (!page->text) {
2211 fz_device *tdev;
2213 page->text = fz_new_text_page (state.ctx, fz_infinite_rect);
2214 page->sheet = fz_new_text_sheet (state.ctx);
2215 tdev = fz_new_text_device (state.ctx, page->sheet, page->text);
2216 fz_run_display_list (page->dlist,
2217 tdev,
2218 pagectm (page),
2219 fz_infinite_bbox, NULL);
2220 qsort (page->text->blocks, page->text->len,
2221 sizeof (*page->text->blocks), compareblocks);
2222 fz_free_device (tdev);
2226 CAMLprim value ml_find_page_with_links (value start_page_v, value dir_v)
2228 CAMLparam2 (start_page_v, dir_v);
2229 CAMLlocal1 (ret_v);
2230 int i, dir = Int_val (dir_v);
2231 int start_page = Int_val (start_page_v);
2232 int end_page = dir > 0 ? state.pagecount : -1;
2234 ret_v = Val_int (0);
2235 if (!(state.type == DPDF || state.type == DXPS)) {
2236 goto done;
2239 lock ("ml_findpage_with_links");
2240 for (i = start_page + dir; i != end_page; i += dir) {
2241 int found;
2243 switch (state.type) {
2244 case DPDF:
2246 pdf_page *page = NULL;
2248 fz_try (state.ctx) {
2249 page = pdf_load_page (state.u.pdf, i);
2250 found = !!page->links;
2252 fz_catch (state.ctx) {
2253 found = 0;
2255 if (page) {
2256 freepdfpage (page);
2259 break;
2260 case DXPS:
2262 xps_page *page = xps_load_page (state.u.xps, i);
2263 found = !!page->links;
2264 freexpspage (page);
2266 break;
2268 default:
2269 ARSERT ("invalid document type");
2272 if (found) {
2273 ret_v = caml_alloc_small (1, 1);
2274 Field (ret_v, 0) = Val_int (i);
2275 goto unlock;
2278 unlock:
2279 unlock ("ml_findpage_with_links");
2281 done:
2282 CAMLreturn (ret_v);
2285 enum { dir_first, dir_last};
2286 enum { dir_first_visible, dir_left, dir_right, dir_down, dir_up };
2288 CAMLprim value ml_findlink (value ptr_v, value dir_v)
2290 CAMLparam2 (ptr_v, dir_v);
2291 CAMLlocal2 (ret_v, pos_v);
2292 struct page *page;
2293 int dirtag, i, slinkindex;
2294 struct slink *found = NULL ,*slink;
2295 char *s = String_val (ptr_v);
2297 page = parse_pointer ("ml_findlink", s);
2298 ret_v = Val_int (0);
2299 if (trylock ("ml_findlink")) {
2300 goto done;
2303 ensureslinks (page);
2305 if (Is_block (dir_v)) {
2306 dirtag = Tag_val (dir_v);
2307 switch (dirtag) {
2308 case dir_first_visible:
2310 int x0, y0, dir, first_index, last_index;
2312 pos_v = Field (dir_v, 0);
2313 x0 = Int_val (Field (pos_v, 0));
2314 y0 = Int_val (Field (pos_v, 1));
2315 dir = Int_val (Field (pos_v, 2));
2317 if (dir >= 0) {
2318 dir = 1;
2319 first_index = 0;
2320 last_index = page->slinkcount;
2322 else {
2323 first_index = page->slinkcount - 1;
2324 last_index = -1;
2327 for (i = first_index; i != last_index; i += dir) {
2328 slink = &page->slinks[i];
2329 if (slink->bbox.y0 >= y0 && slink->bbox.x0 >= x0) {
2330 found = slink;
2331 break;
2335 break;
2337 case dir_left:
2338 slinkindex = Int_val (Field (dir_v, 0));
2339 found = &page->slinks[slinkindex];
2340 for (i = slinkindex - 1; i >= 0; --i) {
2341 slink = &page->slinks[i];
2342 if (slink->bbox.x0 < found->bbox.x0) {
2343 found = slink;
2344 break;
2347 break;
2349 case dir_right:
2350 slinkindex = Int_val (Field (dir_v, 0));
2351 found = &page->slinks[slinkindex];
2352 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2353 slink = &page->slinks[i];
2354 if (slink->bbox.x0 > found->bbox.x0) {
2355 found = slink;
2356 break;
2359 break;
2361 case dir_down:
2362 slinkindex = Int_val (Field (dir_v, 0));
2363 found = &page->slinks[slinkindex];
2364 for (i = slinkindex + 1; i < page->slinkcount; ++i) {
2365 slink = &page->slinks[i];
2366 if (slink->bbox.y0 >= found->bbox.y0) {
2367 found = slink;
2368 break;
2371 break;
2373 case dir_up:
2374 slinkindex = Int_val (Field (dir_v, 0));
2375 found = &page->slinks[slinkindex];
2376 for (i = slinkindex - 1; i >= 0; --i) {
2377 slink = &page->slinks[i];
2378 if (slink->bbox.y0 <= found->bbox.y0) {
2379 found = slink;
2380 break;
2383 break;
2386 else {
2387 dirtag = Int_val (dir_v);
2388 switch (dirtag) {
2389 case dir_first:
2390 found = page->slinks;
2391 break;
2393 case dir_last:
2394 if (page->slinks) {
2395 found = page->slinks + (page->slinkcount - 1);
2397 break;
2400 if (found) {
2401 ret_v = caml_alloc_small (2, 1);
2402 Field (ret_v, 0) = Val_int (found - page->slinks);
2405 unlock ("ml_findlink");
2406 done:
2407 CAMLreturn (ret_v);
2410 enum { uuri, ugoto, utext, uunexpected, ulaunch, unamed, uremote };
2412 #define LINKTOVAL \
2414 int pageno; \
2416 switch (link->dest.kind) { \
2417 case FZ_LINK_GOTO: \
2419 fz_point p; \
2421 pageno = link->dest.ld.gotor.page; \
2422 p.x = 0; \
2423 p.y = 0; \
2425 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) { \
2426 p.y = link->dest.ld.gotor.lt.y; \
2427 p = fz_transform_point (pdim->lctm, p); \
2429 tup_v = caml_alloc_tuple (2); \
2430 ret_v = caml_alloc_small (1, ugoto); \
2431 Field (tup_v, 0) = Val_int (pageno); \
2432 Field (tup_v, 1) = Val_int (p.y); \
2433 Field (ret_v, 0) = tup_v; \
2435 break; \
2437 case FZ_LINK_URI: \
2438 str_v = caml_copy_string (link->dest.ld.uri.uri); \
2439 ret_v = caml_alloc_small (1, uuri); \
2440 Field (ret_v, 0) = str_v; \
2441 break; \
2443 case FZ_LINK_LAUNCH: \
2444 str_v = caml_copy_string (link->dest.ld.launch.file_spec); \
2445 ret_v = caml_alloc_small (1, ulaunch); \
2446 Field (ret_v, 0) = str_v; \
2447 break; \
2449 case FZ_LINK_NAMED: \
2450 str_v = caml_copy_string (link->dest.ld.named.named); \
2451 ret_v = caml_alloc_small (1, unamed); \
2452 Field (ret_v, 0) = str_v; \
2453 break; \
2455 case FZ_LINK_GOTOR: \
2456 str_v = caml_copy_string (link->dest.ld.gotor.file_spec); \
2457 pageno = link->dest.ld.gotor.page; \
2458 tup_v = caml_alloc_tuple (2); \
2459 ret_v = caml_alloc_small (1, uremote); \
2460 Field (tup_v, 0) = str_v; \
2461 Field (tup_v, 1) = Val_int (pageno); \
2462 Field (ret_v, 0) = tup_v; \
2463 break; \
2465 default: \
2467 char buf[80]; \
2469 snprintf (buf, sizeof (buf), \
2470 "unhandled link kind %d", link->dest.kind); \
2471 str_v = caml_copy_string (buf); \
2472 ret_v = caml_alloc_small (1, uunexpected); \
2473 Field (ret_v, 0) = str_v; \
2475 break; \
2479 CAMLprim value ml_getlink (value ptr_v, value n_v)
2481 CAMLparam2 (ptr_v, n_v);
2482 CAMLlocal3 (ret_v, tup_v, str_v);
2483 fz_link *link;
2484 struct page *page;
2485 struct pagedim *pdim;
2486 char *s = String_val (ptr_v);
2488 ret_v = Val_int (0);
2489 if (trylock ("ml_getlink")) {
2490 goto done;
2493 page = parse_pointer ("ml_getlink", s);
2494 ensureslinks (page);
2495 pdim = &state.pagedims[page->pdimno];
2496 link = page->slinks[Int_val (n_v)].link;
2497 LINKTOVAL;
2499 unlock ("ml_getlink");
2500 done:
2501 CAMLreturn (ret_v);
2504 CAMLprim value ml_getlinkcount (value ptr_v)
2506 CAMLparam1 (ptr_v);
2507 struct page *page;
2508 char *s = String_val (ptr_v);
2510 page = parse_pointer ("ml_getlinkcount", s);
2511 CAMLreturn (Val_int (page->slinkcount));
2514 CAMLprim value ml_getlinkrect (value ptr_v, value n_v)
2516 CAMLparam2 (ptr_v, n_v);
2517 CAMLlocal1 (ret_v);
2518 struct page *page;
2519 struct slink *slink;
2520 char *s = String_val (ptr_v);
2522 page = parse_pointer ("ml_getlinkrect", s);
2523 ret_v = caml_alloc_tuple (4);
2524 if (trylock ("ml_getlinkrect")) {
2525 Field (ret_v, 0) = Val_int (0);
2526 Field (ret_v, 1) = Val_int (0);
2527 Field (ret_v, 2) = Val_int (0);
2528 Field (ret_v, 3) = Val_int (0);
2529 goto done;
2531 ensureslinks (page);
2533 slink = &page->slinks[Int_val (n_v)];
2534 Field (ret_v, 0) = Val_int (slink->bbox.x0);
2535 Field (ret_v, 1) = Val_int (slink->bbox.y0);
2536 Field (ret_v, 2) = Val_int (slink->bbox.x1);
2537 Field (ret_v, 3) = Val_int (slink->bbox.y1);
2538 unlock ("ml_getlinkrect");
2540 done:
2541 CAMLreturn (ret_v);
2544 CAMLprim value ml_whatsunder (value ptr_v, value x_v, value y_v)
2546 CAMLparam3 (ptr_v, x_v, y_v);
2547 CAMLlocal3 (ret_v, tup_v, str_v);
2548 fz_link *link;
2549 struct page *page;
2550 char *s = String_val (ptr_v);
2551 int x = Int_val (x_v), y = Int_val (y_v);
2552 struct pagedim *pdim;
2554 ret_v = Val_int (0);
2555 if (trylock ("ml_whatsunder")) {
2556 goto done;
2559 page = parse_pointer ("ml_whatsunder", s);
2560 pdim = &state.pagedims[page->pdimno];
2561 x += pdim->bounds.x0;
2562 y += pdim->bounds.y0;
2563 link = getlink (page, x, y);
2564 if (link) {
2565 LINKTOVAL;
2567 else {
2568 fz_rect *b;
2569 fz_text_block *block;
2571 ensuretext (page);
2572 for (block = page->text->blocks;
2573 block < page->text->blocks + page->text->len;
2574 ++block) {
2575 fz_text_line *line;
2577 b = &block->bbox;
2578 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2579 continue;
2581 for (line = block->lines;
2582 line < block->lines + block->len;
2583 ++line) {
2584 fz_text_span *span;
2586 b = &line->bbox;
2587 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2588 continue;
2590 for (span = line->spans;
2591 span < line->spans + line->len;
2592 ++span) {
2593 fz_text_char *ch;
2595 b = &span->bbox;
2596 if (!(x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1))
2597 continue;
2599 for (ch = span->text; ch < span->text + span->len; ++ch) {
2600 b = &ch->bbox;
2602 if (x >= b->x0 && x <= b->x1
2603 && y >= b->y0 && y <= b->y1) {
2604 const char *n2 =
2605 span->style->font && span->style->font->name
2606 ? span->style->font->name
2607 : "Span has no font name"
2609 FT_FaceRec *face = span->style->font->ft_face;
2610 if (face && face->family_name) {
2611 char *s;
2612 char *n1 = face->family_name;
2613 size_t l1 = strlen (n1);
2614 size_t l2 = strlen (n2);
2616 if (l1 != l2 || memcmp (n1, n2, l1)) {
2617 s = malloc (l1 + l2 + 2);
2618 if (s) {
2619 memcpy (s, n2, l2);
2620 s[l2] = '=';
2621 memcpy (s + l2 + 1, n1, l1 + 1);
2622 str_v = caml_copy_string (s);
2623 free (s);
2627 if (str_v == 0) {
2628 str_v = caml_copy_string (n2);
2630 ret_v = caml_alloc_small (1, utext);
2631 Field (ret_v, 0) = str_v;
2632 goto unlock;
2639 unlock:
2640 unlock ("ml_whatsunder");
2642 done:
2643 CAMLreturn (ret_v);
2646 CAMLprim value ml_seltext (value ptr_v, value rect_v)
2648 CAMLparam2 (ptr_v, rect_v);
2649 fz_rect *b;
2650 struct page *page;
2651 struct pagedim *pdim;
2652 int i, x0, x1, y0, y1;
2653 char *s = String_val (ptr_v);
2654 int fi = 0, li = 0;
2655 fz_text_block *block;
2656 fz_text_span *span, *fspan, *lspan;
2657 fz_text_line *line, *fline = NULL, *lline = NULL;
2659 if (trylock ("ml_seltext")) {
2660 goto done;
2663 page = parse_pointer ("ml_seltext", s);
2664 ensuretext (page);
2666 pdim = &state.pagedims[page->pdimno];
2667 x0 = Int_val (Field (rect_v, 0)) + pdim->bounds.x0;;
2668 y0 = Int_val (Field (rect_v, 1)) + pdim->bounds.y0;
2669 x1 = Int_val (Field (rect_v, 2)) + pdim->bounds.x0;
2670 y1 = Int_val (Field (rect_v, 3)) + pdim->bounds.y0;
2672 if (0) {
2673 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
2674 glColor3ub (128, 128, 128);
2675 glRecti (x0, y0, x1, y1);
2676 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
2679 fspan = lspan = NULL;
2681 for (block = page->text->blocks;
2682 block < page->text->blocks + page->text->len;
2683 ++block) {
2684 for (line = block->lines;
2685 line < block->lines + block->len;
2686 ++line) {
2687 for (span = line->spans;
2688 span < line->spans + line->len;
2689 ++span) {
2690 for (i = 0; i < span->len; ++i) {
2691 b = &span->text[i].bbox;
2692 int selected = 0;
2694 if (x0 >= b->x0 && x0 <= b->x1
2695 && y0 >= b->y0 && y0 <= b->y1) {
2696 fspan = span;
2697 fline = line;
2698 fi = i;
2699 selected = 1;
2701 if (x1 >= b->x0 && x1 <= b->x1
2702 && y1 >= b->y0 && y1 <= b->y1) {
2703 lspan = span;
2704 lline = line;
2705 li = i;
2706 selected = 1;
2708 if (0 && selected) {
2709 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
2710 glColor3ub (128, 128, 128);
2711 glRecti (b->x0, b->y0, b->x1, b->y1);
2712 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
2718 if (y1 < y0 || x1 < x0) {
2719 int swap = 0;
2721 if (fspan == lspan) {
2722 swap = 1;
2724 else {
2725 if (y1 < y0) {
2726 if (fline != lline) {
2727 swap = 1;
2732 if (swap) {
2733 i = fi;
2734 span = fspan;
2736 fi = li;
2737 fspan = lspan;
2739 li = i;
2740 lspan = span;
2744 page->fmark.i = fi;
2745 page->fmark.span = fspan;
2747 page->lmark.i = li;
2748 page->lmark.span = lspan;
2750 unlock ("ml_seltext");
2752 done:
2753 CAMLreturn (Val_unit);
2756 static int UNUSED_ATTR pipespan (FILE *f, fz_text_span *span, int a, int b)
2758 char buf[4];
2759 int i, len, ret;
2761 for (i = a; i <= b; ++i) {
2762 len = fz_runetochar (buf, span->text[i].c);
2763 ret = fwrite (buf, len, 1, f);
2765 if (ret != 1) {
2766 fprintf (stderr, "failed to write %d bytes ret=%d: %s\n",
2767 len, ret, strerror (errno));
2768 return -1;
2771 return 0;
2774 #ifdef __CYGWIN__
2775 value ml_popen (value UNUSED_ATTR u1, value UNUSED_ATTR u2)
2777 caml_failwith ("ml_popen not implemented under Cygwin");
2779 #else
2780 CAMLprim value ml_popen (value command_v, value fds_v)
2782 CAMLparam2 (command_v, fds_v);
2783 CAMLlocal2 (l_v, tup_v);
2784 int ret;
2785 char *msg = NULL;
2786 value earg_v = Nothing;
2787 posix_spawnattr_t attr;
2788 posix_spawn_file_actions_t fa;
2789 char *argv[] = { "/bin/sh", "-c", String_val (command_v), NULL };
2791 if ((ret = posix_spawn_file_actions_init (&fa)) != 0) {
2792 unix_error (ret, "posix_spawn_file_actions_init", Nothing);
2795 if ((ret = posix_spawnattr_init (&attr)) != 0) {
2796 msg = "posix_spawnattr_init";
2797 goto fail1;
2800 #ifdef POSIX_SPAWN_USEVFORK
2801 if ((ret = posix_spawnattr_setflags (&attr, POSIX_SPAWN_USEVFORK)) != 0) {
2802 msg = "posix_spawnattr_setflags POSIX_SPAWN_USEVFORK";
2803 goto fail;
2805 #endif
2807 for (l_v = fds_v; l_v != Val_int (0); l_v = Field (l_v, 1)) {
2808 int fd1, fd2;
2810 tup_v = Field (l_v, 0);
2811 fd1 = Int_val (Field (tup_v, 0));
2812 fd2 = Int_val (Field (tup_v, 1));
2813 if (fd2 < 0) {
2814 if ((ret = posix_spawn_file_actions_addclose (&fa, fd1)) != 0) {
2815 msg = "posix_spawn_file_actions_addclose";
2816 earg_v = tup_v;
2817 goto fail;
2820 else {
2821 if ((ret = posix_spawn_file_actions_adddup2 (&fa, fd1, fd2)) != 0) {
2822 msg = "posix_spawn_file_actions_adddup2";
2823 earg_v = tup_v;
2824 goto fail;
2829 if ((ret = posix_spawn (NULL, "/bin/sh", &fa, &attr, argv, environ))) {
2830 msg = "posix_spawn";
2831 goto fail;
2834 fail:
2835 if ((ret = posix_spawnattr_destroy (&attr)) != 0) {
2836 fprintf (stderr, "posix_spawnattr_destroy: %s\n", strerror (ret));
2839 fail1:
2840 if ((ret = posix_spawn_file_actions_destroy (&fa)) != 0) {
2841 fprintf (stderr, "posix_spawn_file_actions_destroy: %s\n",
2842 strerror (ret));
2845 if (msg)
2846 unix_error (ret, msg, earg_v);
2848 CAMLreturn (Val_unit);
2850 #endif
2852 CAMLprim value ml_copysel (value fd_v, value ptr_v)
2854 CAMLparam1 (ptr_v);
2855 FILE *f;
2856 int seen = 0;
2857 struct page *page;
2858 fz_text_line *line;
2859 fz_text_span *span;
2860 fz_text_block *block;
2861 int fd = Int_val (fd_v);
2862 char *s = String_val (ptr_v);
2864 if (trylock ("ml_copysel")) {
2865 goto done;
2868 page = parse_pointer ("ml_sopysel", s);
2870 if (!page->fmark.span || !page->lmark.span) {
2871 fprintf (stderr, "nothing to copy\n");
2872 goto unlock;
2875 f = fdopen (fd, "w");
2876 if (!f) {
2877 fprintf (stderr, "failed to fopen sel pipe: %s\n",
2878 strerror (errno));
2879 f = stdout;
2882 for (block = page->text->blocks;
2883 block < page->text->blocks + page->text->len;
2884 ++block) {
2885 for (line = block->lines;
2886 line < block->lines + block->len;
2887 ++line) {
2888 for (span = line->spans;
2889 span < line->spans + line->len;
2890 ++span) {
2891 int a, b;
2893 seen |= span == page->fmark.span || span == page->lmark.span;
2894 a = span == page->fmark.span ? page->fmark.i : 0;
2895 b = span == page->lmark.span ? page->lmark.i : span->len - 1;
2897 if (seen) {
2898 if (pipespan (f, span, a, b)) {
2899 goto close;
2901 if (span == line->spans + line->len - 1) {
2902 if (putc ('\n', f) == EOF) {
2903 fprintf (stderr,
2904 "failed break line on sel pipe: %s\n",
2905 strerror (errno));
2906 goto close;
2909 if (span == page->lmark.span) {
2910 goto endloop;
2916 endloop:
2917 page->lmark.span = NULL;
2918 page->fmark.span = NULL;
2920 close:
2921 if (f != stdout) {
2922 int ret = fclose (f);
2923 fd = -1;
2924 if (ret == -1) {
2925 if (errno != ECHILD) {
2926 fprintf (stderr, "failed to close sel pipe: %s\n",
2927 strerror (errno));
2931 unlock:
2932 unlock ("ml_copysel");
2934 done:
2935 if (fd >= 0) {
2936 if (close (fd)) {
2937 fprintf (stderr, "failed to close sel pipe: %s\n",
2938 strerror (errno));
2941 CAMLreturn (Val_unit);
2944 CAMLprim value ml_getpdimrect (value pagedimno_v)
2946 CAMLparam1 (pagedimno_v);
2947 CAMLlocal1 (ret_v);
2948 int pagedimno = Int_val (pagedimno_v);
2949 fz_rect box;
2951 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
2952 if (trylock ("ml_getpdimrect")) {
2953 box = fz_empty_rect;
2955 else {
2956 box = state.pagedims[pagedimno].mediabox;
2957 unlock ("ml_getpdimrect");
2960 Store_double_field (ret_v, 0, box.x0);
2961 Store_double_field (ret_v, 1, box.x1);
2962 Store_double_field (ret_v, 2, box.y0);
2963 Store_double_field (ret_v, 3, box.y1);
2965 CAMLreturn (ret_v);
2968 static double getmaxw (void)
2970 int i;
2971 struct pagedim *p;
2972 double maxw = 0.0;
2974 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
2975 double x0, x1, w;
2977 x0 = MIN (p->mediabox.x0, p->mediabox.x1);
2978 x1 = MAX (p->mediabox.x0, p->mediabox.x1);
2980 w = x1 - x0;
2981 maxw = MAX (w, maxw);
2983 return maxw;
2986 CAMLprim value ml_getmaxw (value unit_v)
2988 CAMLparam1 (unit_v);
2989 CAMLlocal1 (ret_v);
2990 double maxw = 0.0;
2992 if (trylock ("ml_getmaxw")) {
2993 goto done;
2995 maxw = getmaxw ();
2996 unlock ("ml_getmaxw");
2997 done:
2998 ret_v = caml_copy_double (maxw);
2999 CAMLreturn (ret_v);
3002 CAMLprim value ml_zoom_for_height (value winw_v, value winh_v,
3003 value dw_v, value cols_v)
3005 CAMLparam3 (winw_v, winh_v, dw_v);
3006 CAMLlocal1 (ret_v);
3007 int i;
3008 double zoom = 1.0;
3009 double maxw = 0.0, maxh = 0.0;
3010 struct pagedim *p;
3011 double winw = Int_val (winw_v);
3012 double winh = Int_val (winh_v);
3013 double dw = Int_val (dw_v);
3014 double cols = Int_val (cols_v);
3015 double pw = 1.0, ph = 1.0, aspect;
3017 if (trylock ("ml_zoom_for_height")) {
3018 goto done;
3021 if (state.proportional) {
3022 maxw = getmaxw () / cols;
3025 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
3026 double x0, x1, y0, y1, w, h, scaledh, scale;
3027 fz_rect rect;
3029 rect = fz_transform_rect (fz_rotate (p->rotate + state.rotate),
3030 p->mediabox);
3031 x0 = MIN (rect.x0, rect.x1);
3032 x1 = MAX (rect.x0, rect.x1);
3033 y0 = MIN (rect.y0, rect.y1);
3034 y1 = MAX (rect.y0, rect.y1);
3036 w = (x1 - x0) / cols;
3037 h = y1 - y0;
3039 if (state.proportional) {
3040 scale = w / maxw;
3041 scaledh = h * scale;
3043 else {
3044 scale = 1.0;
3045 scaledh = h;
3048 if (scaledh > maxh) {
3049 maxh = scaledh;
3050 ph = scaledh;
3051 pw = w * scale;
3055 aspect = pw / ph;
3056 zoom = (winh * aspect + dw) / winw;
3058 unlock ("ml_zoom_for_height");
3059 done:
3060 ret_v = caml_copy_double (zoom);
3061 CAMLreturn (ret_v);
3064 CAMLprim value ml_draw_string (value pt_v, value x_v, value y_v, value string_v)
3066 CAMLparam4 (pt_v, x_v, y_v, string_v);
3067 CAMLlocal1 (ret_v);
3068 int pt = Int_val(pt_v);
3069 int x = Int_val (x_v);
3070 int y = Int_val (y_v);
3071 double w;
3073 w = draw_string (state.face, pt, x, y, String_val (string_v));
3074 ret_v = caml_copy_double (w);
3075 CAMLreturn (ret_v);
3078 CAMLprim value ml_measure_string (value pt_v, value string_v)
3080 CAMLparam2 (pt_v, string_v);
3081 CAMLlocal1 (ret_v);
3082 int pt = Int_val (pt_v);
3083 double w;
3085 w = measure_string (state.face, pt, String_val (string_v));
3086 ret_v = caml_copy_double (w);
3087 CAMLreturn (ret_v);
3090 CAMLprim value ml_getpagebox (value opaque_v)
3092 CAMLparam1 (opaque_v);
3093 CAMLlocal1 (ret_v);
3094 fz_bbox bbox;
3095 fz_device *dev;
3096 char *s = String_val (opaque_v);
3097 struct page *page = parse_pointer ("ml_getpagebox", s);
3099 ret_v = caml_alloc_tuple (4);
3100 dev = fz_new_bbox_device (state.ctx, &bbox);
3101 dev->hints |= FZ_IGNORE_SHADE;
3103 switch (page->type) {
3104 case DPDF:
3105 pdf_run_page (state.u.pdf, page->u.pdfpage, dev, pagectm (page), NULL);
3106 break;
3108 case DXPS:
3109 xps_run_page (state.u.xps, page->u.xpspage, dev, pagectm (page), NULL);
3110 break;
3112 default:
3113 bbox = fz_infinite_bbox;
3114 break;
3117 fz_free_device (dev);
3118 Field (ret_v, 0) = Val_int (bbox.x0);
3119 Field (ret_v, 1) = Val_int (bbox.y0);
3120 Field (ret_v, 2) = Val_int (bbox.x1);
3121 Field (ret_v, 3) = Val_int (bbox.y1);
3123 CAMLreturn (ret_v);
3126 CAMLprim value ml_setaalevel (value level_v)
3128 CAMLparam1 (level_v);
3130 state.aalevel = Int_val (level_v);
3131 CAMLreturn (Val_unit);
3134 #undef pixel
3135 #include <X11/Xlib.h>
3136 #include <GL/glx.h>
3138 static struct {
3139 Display *dpy;
3140 GLXContext ctx;
3141 GLXDrawable drawable;
3142 } glx;
3144 #include "keysym2ucs.c"
3146 CAMLprim value ml_keysymtoutf8 (value keysym_v)
3148 CAMLparam1 (keysym_v);
3149 CAMLlocal1 (str_v);
3150 KeySym keysym = Int_val (keysym_v);
3151 Rune rune;
3152 int len;
3153 char buf[5];
3155 rune = keysym2ucs (keysym);
3156 len = fz_runetochar (buf, rune);
3157 buf[len] = 0;
3158 str_v = caml_copy_string (buf);
3159 CAMLreturn (str_v);
3162 CAMLprim value ml_glx (value win_v)
3164 CAMLparam1 (win_v);
3165 XVisualInfo *visual;
3166 int screen, wid = Int_val (win_v);
3167 int attributes[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
3169 glx.dpy = XOpenDisplay (NULL);
3170 if (!glx.dpy) {
3171 caml_failwith ("XOpenDisplay failed");
3174 screen = DefaultScreen (glx.dpy);
3175 visual = glXChooseVisual (glx.dpy, screen, attributes);
3176 if (!visual) {
3177 XCloseDisplay (glx.dpy);
3178 glx.dpy = NULL;
3179 caml_failwith ("glXChooseVisual");
3182 glx.ctx = glXCreateContext (glx.dpy, visual, NULL, True);
3183 XFree (visual);
3184 if (!glx.ctx) {
3185 XCloseDisplay (glx.dpy);
3186 glx.dpy = NULL;
3187 caml_failwith ("glXCreateContext");
3190 if (!glXMakeCurrent (glx.dpy, wid, glx.ctx)) {
3191 glXDestroyContext (glx.dpy, glx.ctx);
3192 XCloseDisplay (glx.dpy);
3193 glx.dpy = NULL;
3194 glx.ctx = NULL;
3195 caml_failwith ("glXMakeCurrent");
3197 glx.drawable = wid;
3199 if (0) {
3200 int (*f) (int) =
3201 (int (*) (int)) glXGetProcAddress ((GLubyte *) "glXSwapIntervalMESA");
3202 printf ("sync %d\n", f (0));
3204 CAMLreturn (Val_unit);
3207 CAMLprim value ml_swapb (value unit_v)
3209 CAMLparam1 (unit_v);
3210 glXSwapBuffers (glx.dpy, glx.drawable);
3211 CAMLreturn (Val_unit);
3214 CAMLprim value ml_glxsync (value unit_v)
3216 CAMLparam1 (unit_v);
3217 if (glx.dpy && glx.ctx) {
3218 glXWaitX ();
3219 glXWaitGL ();
3221 CAMLreturn (Val_unit);
3224 enum { piunknown, pilinux, piosx, pisun, pifreebsd,
3225 pidragonflybsd, piopenbsd, pinetbsd, picygwin };
3227 CAMLprim value ml_platform (value unit_v)
3229 CAMLparam1 (unit_v);
3230 int platid = piunknown;
3232 #if defined __linux__
3233 platid = pilinux;
3234 #elif defined __CYGWIN__
3235 platid = picygwin;
3236 #elif defined __DragonFly__
3237 platid = pidragonflybsd;
3238 #elif defined __FreeBSD__
3239 platid = pifreebsd;
3240 #elif defined __OpenBSD__
3241 platid = piopenbsd;
3242 #elif defined __NetBSD__
3243 platid = pinetbsd;
3244 #elif defined __sun__
3245 platid = pisun;
3246 #elif defined __APPLE__
3247 platid = piosx;
3248 #endif
3249 CAMLreturn (Val_int (platid));
3252 CAMLprim value ml_cloexec (value fd_v)
3254 CAMLparam1 (fd_v);
3255 int fd = Int_val (fd_v);
3257 if (fcntl (fd, F_SETFD, FD_CLOEXEC, 1)) {
3258 uerror ("fcntl", Nothing);
3260 CAMLreturn (Val_unit);
3263 CAMLprim value ml_init (value pipe_v, value params_v)
3265 CAMLparam2 (pipe_v, params_v);
3266 CAMLlocal2 (trim_v, fuzz_v);
3267 int ret;
3268 int texcount;
3269 char *fontpath;
3270 int colorspace;
3271 int mustoresize;
3272 struct sigaction sa;
3274 state.cr = Int_val (Field (pipe_v, 0));
3275 state.cw = Int_val (Field (pipe_v, 1));
3276 state.rotate = Int_val (Field (params_v, 0));
3277 state.proportional = Bool_val (Field (params_v, 1));
3278 trim_v = Field (params_v, 2);
3279 texcount = Int_val (Field (params_v, 3));
3280 state.sliceheight = Int_val (Field (params_v, 4));
3281 mustoresize = Int_val (Field (params_v, 5));
3282 colorspace = Int_val (Field (params_v, 6));
3283 fontpath = String_val (Field (params_v, 7));
3284 state.trimcachepath = strdup (String_val (Field (params_v, 8)));
3286 state.ctx = fz_new_context (NULL, NULL, mustoresize);
3288 state.trimmargins = Bool_val (Field (trim_v, 0));
3289 fuzz_v = Field (trim_v, 1);
3290 state.trimfuzz.x0 = Int_val (Field (fuzz_v, 0));
3291 state.trimfuzz.y0 = Int_val (Field (fuzz_v, 1));
3292 state.trimfuzz.x1 = Int_val (Field (fuzz_v, 2));
3293 state.trimfuzz.y1 = Int_val (Field (fuzz_v, 3));
3295 set_tex_params (colorspace);
3297 if (*fontpath) {
3298 state.face = load_font (fontpath);
3300 else {
3301 unsigned int len;
3302 void *base = pdf_lookup_substitute_font (0, 0, 0, 0, &len);
3304 state.face = load_builtin_font (base, len);
3306 if (!state.face) _exit (1);
3308 realloctexts (texcount);
3310 #ifdef __CYGWIN__
3311 sa.sa_handler = SIG_IGN;
3312 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3313 #else
3314 sa.sa_handler = SIG_DFL;
3315 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP | SA_NOCLDWAIT;
3316 #endif
3317 if (sigemptyset (&sa.sa_mask)) {
3318 err (1, "sigemptyset");
3320 if (sigaction (SIGCHLD, &sa, NULL)) {
3321 err (1, "sigaction");
3324 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
3325 if (ret) {
3326 errx (1, "pthread_create: %s", strerror (ret));
3329 CAMLreturn (Val_unit);