Refactor
[llpp.git] / link.c
blob28d583b27c6081ad8a8ef43c9c10ca589227ce0b
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 #include <regex.h>
16 #include <ctype.h>
17 #include <stdarg.h>
18 #include <limits.h>
20 #ifdef __APPLE__
21 #include <OpenGL/gl.h>
22 #else
23 #include <GL/gl.h>
24 #endif
26 #include <caml/fail.h>
27 #include <caml/alloc.h>
28 #include <caml/memory.h>
29 #include <caml/unixsupport.h>
31 #include <fitz.h>
32 #include <mupdf.h>
33 #include <muxps.h>
34 #include <mucbz.h>
36 #include FT_FREETYPE_H
38 #define PIGGYBACK
40 #if defined __GNUC__
41 #define NORETURN __attribute__ ((noreturn))
42 #define UNUSED __attribute__ ((unused))
43 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
44 #define GCC_FMT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
45 #else
46 #define NORETURN
47 #define UNUSED
48 #define OPTIMIZE(n)
49 #define GCC_FMT_ATTR(a, b)
50 #endif
52 #define FMT_s "zu"
54 #define FMT_ptr "p"
55 #define FMT_ptr_cast(p) (p)
56 #define FMT_ptr_cast2(p) (p)
58 static void NORETURN GCC_FMT_ATTR (2, 3)
59 err (int exitcode, const char *fmt, ...)
61 va_list ap;
62 int savederrno;
64 savederrno = errno;
65 va_start (ap, fmt);
66 vfprintf (stderr, fmt, ap);
67 va_end (ap);
68 fprintf (stderr, ": %s\n", strerror (savederrno));
69 fflush (stderr);
70 _exit (exitcode);
73 static void NORETURN GCC_FMT_ATTR (2, 3)
74 errx (int exitcode, const char *fmt, ...)
76 va_list ap;
78 va_start (ap, fmt);
79 vfprintf (stderr, fmt, ap);
80 va_end (ap);
81 fputc ('\n', stderr);
82 fflush (stderr);
83 _exit (exitcode);
86 #ifndef GL_TEXTURE_RECTANGLE_ARB
87 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
88 #endif
90 #ifndef GL_BGRA
91 #define GL_BGRA 0x80E1
92 #endif
94 #ifndef GL_UNSIGNED_INT_8_8_8_8
95 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
96 #endif
98 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
99 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
100 #endif
102 #if 0
103 #define lprintf printf
104 #else
105 #define lprintf(...)
106 #endif
108 #define ARSERT(cond) for (;;) { \
109 if (!(cond)) { \
110 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
112 break; \
115 struct slice {
116 int h;
117 int texindex;
120 struct tile {
121 int x, y, w, h;
122 int slicecount;
123 int sliceheight;
124 fz_pixmap *pixmap;
125 struct slice slices[1];
128 struct pagedim {
129 int pageno;
130 int rotate;
131 int left;
132 int tctmready;
133 fz_bbox bounds;
134 fz_rect pagebox;
135 fz_rect mediabox;
136 fz_matrix ctm, zoomctm, lctm, tctm;
139 enum { DPDF, DXPS, DCBZ };
141 struct page {
142 int gen;
143 int type;
144 int pageno;
145 int pdimno;
146 fz_text_span *text;
147 union {
148 void *ptr;
149 pdf_page *pdfpage;
150 xps_page *xpspage;
151 cbz_page *cbzpage;
152 } u;
153 fz_display_list *dlist;
154 struct mark {
155 int i;
156 fz_text_span *span;
157 } fmark, lmark;
158 void (*freepage) (void *);
161 struct {
162 int type;
163 int sliceheight;
164 struct pagedim *pagedims;
165 int pagecount;
166 int pagedimcount;
167 union {
168 pdf_document *pdf;
169 xps_document *xps;
170 cbz_document *cbz;
171 } u;
172 fz_context *ctx;
173 fz_glyph_cache *cache;
174 int w, h;
176 int texindex;
177 int texcount;
178 GLuint *texids;
180 GLenum texiform;
181 GLenum texform;
182 GLenum texty;
184 fz_colorspace *colorspace;
186 struct {
187 int w, h;
188 struct slice *slice;
189 } *texowners;
191 int rotate;
192 int proportional;
193 int trimmargins;
194 int needoutline;
195 int gen;
196 int aalevel;
198 int trimanew;
199 fz_bbox trimfuzz;
200 fz_pixmap *pig;
202 pthread_t thread;
203 int cr, cw;
204 FT_Face face;
206 void (*closedoc) (void);
207 void (*freepage) (void *);
208 } state;
210 static void UNUSED debug_rect (const char *cap, fz_rect r)
212 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap, r.x0, r.y0, r.x1, r.y1);
215 static void UNUSED debug_bbox (const char *cap, fz_bbox r)
217 printf ("%s(bbox) %d,%d,%d,%d\n", cap, r.x0, r.y0, r.x1, r.y1);
220 static void UNUSED debug_matrix (const char *cap, fz_matrix m)
222 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap,
223 m.a, m.b, m.c, m.d, m.e, m.f);
226 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
228 static void lock (const char *cap)
230 int ret = pthread_mutex_lock (&mutex);
231 if (ret) {
232 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
236 static void unlock (const char *cap)
238 int ret = pthread_mutex_unlock (&mutex);
239 if (ret) {
240 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
244 static int trylock (const char *cap)
246 int ret = pthread_mutex_trylock (&mutex);
248 if (ret && ret != EBUSY) {
249 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
251 return ret == EBUSY;
254 static void *parse_pointer (const char *cap, const char *s)
256 int ret;
257 void *ptr;
259 ret = sscanf (s, "%" FMT_ptr, FMT_ptr_cast (&ptr));
260 if (ret != 1) {
261 errx (1, "%s: cannot parse pointer in `%s'", cap, s);
263 return ptr;
266 static double now (void)
268 struct timeval tv;
270 if (gettimeofday (&tv, NULL)) {
271 err (1, "gettimeofday");
273 return tv.tv_sec + tv.tv_usec*1e-6;
276 static int hasdata (void)
278 int ret, avail;
279 ret = ioctl (state.cr, FIONREAD, &avail);
280 if (ret) err (1, "hasdata: FIONREAD error ret=%d", ret);
281 return avail > 0;
284 CAMLprim value ml_hasdata (value fd_v)
286 CAMLparam1 (fd_v);
287 int ret, avail;
289 ret = ioctl (Int_val (fd_v), FIONREAD, &avail);
290 if (ret) uerror ("ioctl (FIONREAD)", Nothing);
291 CAMLreturn (Val_bool (avail > 0));
294 static void readdata (void *p, int size)
296 ssize_t n;
298 n = read (state.cr, p, size);
299 if (n - size) {
300 if (!n) errx (1, "EOF while reading");
301 err (1, "read (req %d, ret %zd)", size, n);
305 static void writedata (char *p, int size)
307 char buf[4];
308 ssize_t n;
310 buf[0] = (size >> 24) & 0xff;
311 buf[1] = (size >> 16) & 0xff;
312 buf[2] = (size >> 8) & 0xff;
313 buf[3] = (size >> 0) & 0xff;
315 n = write (state.cw, buf, 4);
316 if (n != 4) {
317 if (!n) errx (1, "EOF while writing length");
318 err (1, "write %zd", n);
321 n = write (state.cw, p, size);
322 if (n - size) {
323 if (!n) errx (1, "EOF while writing data");
324 err (1, "write (req %d, ret %zd)", size, n);
328 static int readlen (void)
330 unsigned char p[4];
332 readdata (p, 4);
333 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
336 static void GCC_FMT_ATTR (1, 2) printd (const char *fmt, ...)
338 int size = 200, len;
339 va_list ap;
340 char *buf;
342 buf = malloc (size);
343 for (;;) {
344 if (!buf) err (errno, "malloc for temp buf (%d bytes) failed", size);
346 va_start (ap, fmt);
347 len = vsnprintf (buf, size, fmt, ap);
348 va_end (ap);
350 if (len > -1 && len < size) {
351 writedata (buf, len);
352 break;
355 if (len > -1) {
356 size = len + 1;
358 else {
359 size *= 2;
361 buf = realloc (buf, size);
363 free (buf);
366 static void closepdf (void)
368 if (state.u.pdf) {
369 pdf_close_document (state.u.pdf);
370 state.u.pdf = NULL;
374 static void closexps (void)
376 if (state.u.xps) {
377 xps_close_document (state.u.xps);
378 state.u.xps = NULL;
382 static void closecbz (void)
384 if (state.u.cbz) {
385 cbz_close_document (state.u.cbz);
386 state.u.cbz = NULL;
390 static void freepdfpage (void *ptr)
392 pdf_free_page (state.u.pdf, ptr);
395 static void freexpspage (void *ptr)
397 xps_free_page (state.u.xps, ptr);
400 static void freecbzpage (void *ptr)
402 cbz_free_page (state.u.cbz, ptr);
405 static void openxref (char *filename, char *password)
407 int i, len;
409 for (i = 0; i < state.texcount; ++i) {
410 state.texowners[i].w = -1;
411 state.texowners[i].slice = NULL;
414 if (state.closedoc) state.closedoc ();
416 len = strlen (filename);
418 state.type = DPDF;
419 if (len > 4) {
420 char ext[4];
422 ext[0] = tolower (filename[len-3]);
423 ext[1] = tolower (filename[len-2]);
424 ext[2] = tolower (filename[len-1]);
426 /**/ if (ext[0] == 'x' && ext[1] == 'p' && ext[2] == 's') {
427 state.type = DXPS;
429 else if (ext[0] == 'c' && ext[1] == 'b' && ext[2] == 'z') {
430 state.type = DCBZ;
434 if (state.pagedims) {
435 free (state.pagedims);
436 state.pagedims = NULL;
438 state.pagedimcount = 0;
440 fz_set_aa_level (state.ctx, state.aalevel);
441 switch (state.type) {
442 case DPDF:
443 state.u.pdf = pdf_open_document (state.ctx, filename);
444 if (pdf_needs_password (state.u.pdf)) {
445 int okay = pdf_authenticate_password (state.u.pdf, password);
446 if (!okay) {
447 errx (1, "invalid password");
450 state.pagecount = pdf_count_pages (state.u.pdf);
451 state.closedoc = closepdf;
452 state.freepage = freepdfpage;
453 break;
455 case DXPS:
456 state.u.xps = xps_open_document (state.ctx, filename);
457 state.pagecount = xps_count_pages (state.u.xps);
458 state.closedoc = closexps;
459 state.freepage = freexpspage;
460 break;
462 case DCBZ:
463 state.u.cbz = cbz_open_document (state.ctx, filename);
464 state.pagecount = cbz_count_pages (state.u.cbz);
465 state.closedoc = closecbz;
466 state.freepage = freecbzpage;
467 break;
471 static void pdfinfo (void)
473 if (state.type == DPDF) {
474 fz_obj *infoobj;
476 printd ("info PDF version\t%d.%d",
477 state.u.pdf->version / 10, state.u.pdf->version % 10);
479 infoobj = fz_dict_gets (state.u.pdf->trailer, "Info");
480 if (infoobj) {
481 int i;
482 char *s;
483 char *items[] = { "Title", "Author", "Creator",
484 "Producer", "CreationDate" };
486 for (i = 0; i < sizeof (items) / sizeof (*items); ++i) {
487 fz_obj *obj = fz_dict_gets (infoobj, items[i]);
488 s = pdf_to_utf8 (state.ctx, obj);
489 if (*s) {
490 if (i == 0) {
491 printd ("title %s", s);
493 printd ("info %s\t%s", items[i], s);
495 fz_free (state.ctx, s);
498 printd ("infoend");
502 static void unlinktile (struct tile *tile)
504 int i;
506 for (i = 0; i < tile->slicecount; ++i) {
507 struct slice *s = &tile->slices[i];
509 if (s->texindex != -1) {
510 if (state.texowners[s->texindex].slice == s) {
511 state.texowners[s->texindex].slice = NULL;
517 static void freepage (struct page *page)
519 if (page->text) {
520 fz_free_text_span (state.ctx, page->text);
522 page->freepage (page->u.ptr);
523 fz_free_display_list (state.ctx, page->dlist);
524 free (page);
527 static void freetile (struct tile *tile)
529 unlinktile (tile);
530 #ifndef PIGGYBACK
531 fz_drop_pixmap (state.ctx, tile->pixmap);
532 #else
533 if (state.pig) {
534 fz_drop_pixmap (state.ctx, state.pig);
536 state.pig = tile->pixmap;
537 #endif
538 free (tile);
541 #ifdef __ALTIVEC__
542 #include <altivec.h>
544 static int cacheline32bytes;
545 extern char **environ;
547 static void __attribute__ ((constructor)) clcheck (void)
549 char **envp = environ;
550 unsigned long *auxv;
552 while (*envp++);
554 for (auxv = (unsigned long *) envp; *auxv != 0; auxv += 2) {
555 if (*auxv == 19) {
556 cacheline32bytes = auxv[1] == 32;
557 return;
562 static void OPTIMIZE (3) clearpixmap (fz_pixmap *pixmap)
564 if (cacheline32bytes) {
565 intptr_t a1, a2, diff;
566 size_t sizea, i, size = pixmap->w * pixmap->h * pixmap->n;
567 vector unsigned char v = vec_splat_u8 (-1);
568 vector unsigned char *p;
570 a1 = a2 = (intptr_t) pixmap->samples;
571 a2 = (a1 + 31) & ~31;
572 diff = a2 - a1;
573 sizea = size - diff;
574 p = (void *) a2;
576 while (a1 != a2) *(char *) a1++ = 0xff;
577 for (i = 0; i < (sizea & ~31); i += 32) {
578 __asm volatile ("dcbz %0, %1"::"b"(a2),"r"(i));
579 vec_st (v, i, p);
580 vec_st (v, i + 16, p);
582 while (i < sizea) *((char *) a1 + i++) = 0xff;
584 else fz_clear_pixmap_with_value (state.ctx, pixmap, 0xff);
586 #else
587 #define clearpixmap(p) fz_clear_pixmap_with_value (state.ctx, p, 0xff)
588 #endif
590 static fz_matrix trimctm (pdf_page *page, int pindex)
592 fz_matrix ctm;
593 struct pagedim *pdim = &state.pagedims[pindex];
595 if (!pdim->tctmready) {
596 if (state.trimmargins) {
597 fz_rect realbox;
599 ctm = fz_concat (fz_rotate (-pdim->rotate), fz_scale (1, -1));
600 realbox = fz_transform_rect (ctm, pdim->mediabox);
601 ctm = fz_concat (ctm, fz_translate (-realbox.x0, -realbox.y0));
602 ctm = fz_concat (fz_invert_matrix (page->ctm), ctm);
604 else {
605 ctm = fz_identity;
607 pdim->tctm = ctm;
608 pdim->tctmready = 1;
610 return pdim->tctm;
613 static fz_matrix pagectm (struct page *page)
615 if (page->type == DPDF) {
616 return fz_concat (trimctm (page->u.pdfpage, page->pdimno),
617 state.pagedims[page->pdimno].ctm);
619 else {
620 fz_matrix ctm;
621 struct pagedim *pdim = &state.pagedims[page->pdimno];
623 ctm = state.pagedims[page->pdimno].ctm;
624 ctm = fz_concat (fz_translate (-pdim->mediabox.x0,
625 -pdim->mediabox.y0), ctm);
626 return ctm;
630 static void *loadpage (int pageno, int pindex)
632 fz_device *dev;
633 struct page *page = NULL;
635 page = calloc (sizeof (struct page), 1);
636 if (!page) {
637 err (1, "calloc page %d", pageno);
640 page->dlist = fz_new_display_list (state.ctx);
641 dev = fz_new_list_device (state.ctx, page->dlist);
642 switch (state.type) {
643 case DPDF:
644 page->u.pdfpage = pdf_load_page (state.u.pdf, pageno);
645 pdf_run_page (state.u.pdf, page->u.pdfpage, dev, fz_identity, NULL);
646 page->freepage = freepdfpage;
647 break;
649 case DXPS:
650 page->u.xpspage = xps_load_page (state.u.xps, pageno);
651 xps_run_page (state.u.xps, page->u.xpspage, dev, fz_identity, NULL);
652 page->freepage = freexpspage;
653 break;
655 case DCBZ:
656 page->u.cbzpage = cbz_load_page (state.u.cbz, pageno);
657 cbz_run_page (state.u.cbz, page->u.cbzpage, dev, fz_identity, NULL);
658 page->freepage = freecbzpage;
659 break;
661 fz_free_device (dev);
663 page->pdimno = pindex;
664 page->pageno = pageno;
665 page->gen = state.gen;
666 page->type = state.type;
668 return page;
671 static struct tile *alloctile (int h)
673 int i;
674 int slicecount;
675 size_t tilesize;
676 struct tile *tile;
678 slicecount = (h + state.sliceheight - 1) / state.sliceheight;
679 tilesize = sizeof (*tile) + ((slicecount - 1) * sizeof (struct slice));
680 tile = calloc (tilesize, 1);
681 if (!tile) {
682 err (1, "can not allocate tile (%" FMT_s " bytes)", tilesize);
684 for (i = 0; i < slicecount; ++i) {
685 int sh = MIN (h, state.sliceheight);
686 tile->slices[i].h = sh;
687 tile->slices[i].texindex = -1;
688 h -= sh;
690 tile->slicecount = slicecount;
691 tile->sliceheight = state.sliceheight;
692 return tile;
695 static struct tile *rendertile (struct page *page, int x, int y, int w, int h)
697 fz_bbox bbox;
698 fz_device *dev;
699 struct tile *tile;
700 struct pagedim *pdim;
702 tile = alloctile (h);
703 pdim = &state.pagedims[page->pdimno];
705 bbox = pdim->bounds;
706 bbox.x0 += x;
707 bbox.y0 += y;
708 bbox.x1 = bbox.x0 + w;
709 bbox.y1 = bbox.y0 + h;
711 if (state.pig) {
712 if (state.pig->w == w
713 && state.pig->h == h
714 && state.pig->colorspace == state.colorspace) {
715 tile->pixmap = state.pig;
716 tile->pixmap->x = bbox.x0;
717 tile->pixmap->y = bbox.y0;
719 else {
720 fz_drop_pixmap (state.ctx, state.pig);
722 state.pig = NULL;
724 if (!tile->pixmap) {
725 tile->pixmap =
726 fz_new_pixmap_with_rect (state.ctx, state.colorspace, bbox);
729 tile->w = w;
730 tile->h = h;
731 clearpixmap (tile->pixmap);
732 dev = fz_new_draw_device (state.ctx, tile->pixmap);
733 fz_run_display_list (page->dlist, dev, pagectm (page), bbox, NULL);
734 fz_free_device (dev);
736 return tile;
739 static void initpdims (void)
741 int pageno;
742 double start, end;
744 start = now ();
745 for (pageno = 0; pageno < state.pagecount; ++pageno) {
746 int rotate;
747 fz_obj *pageobj;
748 struct pagedim *p;
749 fz_rect mediabox;
751 switch (state.type) {
752 case DPDF:
753 pageobj = state.u.pdf->page_objs[pageno];
755 if (state.trimmargins) {
756 fz_obj *obj;
757 pdf_page *page;
759 page = pdf_load_page (state.u.pdf, pageno);
760 obj = fz_dict_gets (pageobj, "llpp.TrimBox");
761 if (state.trimanew || !obj) {
762 fz_rect rect;
763 fz_bbox bbox;
764 fz_matrix ctm;
765 fz_device *dev;
767 dev = fz_new_bbox_device (state.ctx, &bbox);
768 dev->hints |= FZ_IGNORE_SHADE;
769 ctm = fz_invert_matrix (page->ctm);
770 pdf_run_page (state.u.pdf, page, dev, fz_identity, NULL);
771 fz_free_device (dev);
773 rect.x0 = bbox.x0 + state.trimfuzz.x0;
774 rect.x1 = bbox.x1 + state.trimfuzz.x1;
775 rect.y0 = bbox.y0 + state.trimfuzz.y0;
776 rect.y1 = bbox.y1 + state.trimfuzz.y1;
777 rect = fz_transform_rect (ctm, rect);
778 rect = fz_intersect_rect (rect, page->mediabox);
780 if (fz_is_empty_rect (rect)) {
781 mediabox = page->mediabox;
783 else {
784 mediabox = rect;
787 obj = fz_new_array (state.ctx, 4);
788 fz_array_push (obj, fz_new_real (state.ctx, mediabox.x0));
789 fz_array_push (obj, fz_new_real (state.ctx, mediabox.y0));
790 fz_array_push (obj, fz_new_real (state.ctx, mediabox.x1));
791 fz_array_push (obj, fz_new_real (state.ctx, mediabox.y1));
792 fz_dict_puts (pageobj, "llpp.TrimBox", obj);
794 else {
795 mediabox.x0 = fz_to_real (fz_array_get (obj, 0));
796 mediabox.y0 = fz_to_real (fz_array_get (obj, 1));
797 mediabox.x1 = fz_to_real (fz_array_get (obj, 2));
798 mediabox.y1 = fz_to_real (fz_array_get (obj, 3));
801 rotate = page->rotate;
802 pdf_free_page (state.u.pdf, page);
804 printd ("progress %f Trimming %d",
805 (double) (pageno + 1) / state.pagecount,
806 pageno + 1);
808 else {
809 fz_rect cropbox;
811 mediabox = pdf_to_rect (state.ctx,
812 fz_dict_gets (pageobj, "MediaBox"));
813 if (fz_is_empty_rect (mediabox)) {
814 fprintf (stderr, "cannot find page size for page %d\n",
815 pageno+1);
816 mediabox.x0 = 0;
817 mediabox.y0 = 0;
818 mediabox.x1 = 612;
819 mediabox.y1 = 792;
822 cropbox = pdf_to_rect (state.ctx,
823 fz_dict_gets (pageobj, "CropBox"));
824 if (!fz_is_empty_rect (cropbox)) {
825 mediabox = fz_intersect_rect (mediabox, cropbox);
827 rotate = fz_to_int (fz_dict_gets (pageobj, "Rotate"));
829 break;
831 case DXPS:
833 xps_page *page;
835 page = xps_load_page (state.u.xps, pageno);
836 mediabox = xps_bound_page (state.u.xps, page);
837 rotate = 0;
838 if (state.trimmargins) {
839 fz_rect rect;
840 fz_bbox bbox;
841 fz_device *dev;
843 dev = fz_new_bbox_device (state.ctx, &bbox);
844 dev->hints |= FZ_IGNORE_SHADE;
845 xps_run_page (state.u.xps, page, dev, fz_identity, NULL);
846 fz_free_device (dev);
848 rect.x0 = bbox.x0 + state.trimfuzz.x0;
849 rect.x1 = bbox.x1 + state.trimfuzz.x1;
850 rect.y0 = bbox.y0 + state.trimfuzz.y0;
851 rect.y1 = bbox.y1 + state.trimfuzz.y1;
852 rect = fz_intersect_rect (rect, mediabox);
854 if (!fz_is_empty_rect (rect)) {
855 mediabox = rect;
858 xps_free_page (state.u.xps, page);
859 printd ("progress %f loading %d",
860 (double) (pageno + 1) / state.pagecount,
861 pageno + 1);
863 break;
865 case DCBZ:
867 rotate = 0;
868 if (state.trimmargins) {
869 cbz_page *page;
871 page = cbz_load_page (state.u.cbz, pageno);
872 mediabox = cbz_bound_page (state.u.cbz, page);
873 cbz_free_page (state.u.cbz, page);
874 printd ("progress %f Trimming %d",
875 (double) (pageno + 1) / state.pagecount,
876 pageno + 1);
878 else {
879 mediabox.x0 = mediabox.y0 = 0;
880 mediabox.x1 = 900;
881 mediabox.y1 = 900;
884 break;
886 default:
887 ARSERT (0 && state.type);
890 if (state.pagedimcount == 0
891 || (p = &state.pagedims[state.pagedimcount-1], p->rotate != rotate)
892 || memcmp (&p->mediabox, &mediabox, sizeof (mediabox))) {
893 size_t size;
895 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
896 state.pagedims = realloc (state.pagedims, size);
897 if (!state.pagedims) {
898 err (1, "realloc pagedims to %" FMT_s " (%d elems)",
899 size, state.pagedimcount + 1);
902 p = &state.pagedims[state.pagedimcount++];
903 p->rotate = rotate;
904 p->mediabox = mediabox;
905 p->pageno = pageno;
908 end = now ();
909 if (state.trimmargins) {
910 printd ("progress 1 Trimmed %d pages in %f seconds",
911 state.pagecount, end - start);
913 else {
914 printd ("vmsg Processed %d pages in %f seconds",
915 state.pagecount, end - start);
917 state.trimanew = 0;
920 static void layout (void)
922 int pindex;
923 fz_rect box;
924 fz_matrix ctm;
925 double zoom, w, maxw = 0;
926 struct pagedim *p = state.pagedims;
928 if (state.proportional) {
929 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
930 box = fz_transform_rect (fz_rotate (p->rotate + state.rotate),
931 p->mediabox);
932 w = box.x1 - box.x0;
933 maxw = MAX (w, maxw);
937 p = state.pagedims;
938 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
939 fz_bbox bbox;
941 ctm = fz_rotate (state.rotate);
942 box = fz_transform_rect (fz_rotate (p->rotate + state.rotate),
943 p->mediabox);
944 w = box.x1 - box.x0;
946 if (state.proportional) {
947 double scale = w / maxw;
948 zoom = (state.w / w) * scale;
950 else {
951 zoom = state.w / w;
954 p->zoomctm = fz_scale (zoom, zoom);
955 ctm = fz_concat (p->zoomctm, ctm);
957 p->pagebox = fz_transform_rect (fz_rotate (p->rotate), p->mediabox);
958 p->pagebox.x1 -= p->pagebox.x0;
959 p->pagebox.y1 -= p->pagebox.y0;
960 p->pagebox.x0 = 0;
961 p->pagebox.y0 = 0;
962 bbox = fz_round_rect (fz_transform_rect (ctm, p->pagebox));
964 p->bounds = bbox;
965 p->left = state.proportional ? ((maxw - w) * zoom) / 2.0 : 0;
966 p->ctm = ctm;
968 ctm = fz_identity;
969 ctm = fz_concat (ctm, fz_translate (0, -p->mediabox.y1));
970 ctm = fz_concat (ctm, fz_scale (zoom, -zoom));
971 ctm = fz_concat (ctm, fz_rotate (p->rotate + state.rotate));
972 p->lctm = ctm;
974 p->tctmready = 0;
977 while (p-- != state.pagedims) {
978 int w = p->bounds.x1;
979 int h = p->bounds.y1;
981 printd ("pdim %d %d %d %d", p->pageno, w, h, p->left);
985 static void recurse_outline (fz_outline *outline, int level)
987 while (outline) {
988 fz_link_dest *dest;
989 int i, top = 0;
990 struct pagedim *pdim = state.pagedims;
992 dest = &outline->dest;
993 for (i = 0; i < state.pagedimcount; ++i) {
994 if (state.pagedims[i].pageno > dest->ld.gotor.page)
995 break;
996 pdim = &state.pagedims[i];
998 if (dest->ld.gotor.flags & fz_link_flag_t_valid) {
999 fz_point p;
1000 p.x = 0;
1001 p.y = dest->ld.gotor.lt.y;
1002 p = fz_transform_point (pdim->lctm, p);
1003 top = p.y;
1005 if (dest->ld.gotor.page >= 0 && dest->ld.gotor.page < 1<<30) {
1006 int h;
1007 double y0, y1;
1009 y0 = MIN (pdim->bounds.y0, pdim->bounds.y1);
1010 y1 = MAX (pdim->bounds.y0, pdim->bounds.y1);
1011 h = y1 - y0;
1012 printd ("o %d %d %d %d %s",
1013 level, dest->ld.gotor.page, top, h, outline->title);
1015 if (outline->down) {
1016 recurse_outline (outline->down, level + 1);
1018 outline = outline->next;
1022 static void process_outline (void)
1024 fz_outline *outline;
1026 if (!state.needoutline) return;
1028 state.needoutline = 0;
1029 switch (state.type) {
1030 case DPDF:
1031 outline = pdf_load_outline (state.u.pdf);
1032 break;
1033 case DXPS:
1034 outline = xps_load_outline (state.u.xps);
1035 break;
1036 default:
1037 outline = NULL;
1038 break;
1040 if (outline) {
1041 recurse_outline (outline, 0);
1042 fz_free_outline (state.ctx, outline);
1046 static int comparespans (const void *l, const void *r)
1048 fz_text_span const *const*ls = l;
1049 fz_text_span const *const*rs = r;
1050 return (*ls)->text->bbox.y0 - (*rs)->text->bbox.y0;
1053 /* wishful thinking function */
1054 static void search (regex_t *re, int pageno, int y, int forward)
1056 int i, j;
1057 int ret;
1058 char *p;
1059 char buf[256];
1060 fz_matrix ctm;
1061 fz_device *tdev;
1062 union { void *ptr; pdf_page *pdfpage; xps_page *xpspage; } u;
1063 fz_text_span *text, *span, **pspan;
1064 struct pagedim *pdim, *pdimprev;
1065 int stop = 0;
1066 int niters = 0;
1067 int nspans;
1068 double start, end;
1070 if (!(state.type == DPDF || state.type == DXPS))
1071 return;
1073 start = now ();
1074 while (pageno >= 0 && pageno < state.pagecount && !stop) {
1075 if (niters++ == 5) {
1076 niters = 0;
1077 if (hasdata ()) {
1078 printd ("progress 1 attention requested aborting search at %d",
1079 pageno);
1080 stop = 1;
1082 else {
1083 printd ("progress %f searching in page %d",
1084 (double) (pageno + 1) / state.pagecount,
1085 pageno);
1088 pdimprev = NULL;
1089 for (i = 0; i < state.pagedimcount; ++i) {
1090 pdim = &state.pagedims[i];
1091 if (pdim->pageno == pageno) {
1092 goto found;
1094 if (pdim->pageno > pageno) {
1095 pdim = pdimprev;
1096 goto found;
1098 pdimprev = pdim;
1100 pdim = pdimprev;
1101 found:
1103 text = fz_new_text_span (state.ctx);
1104 tdev = fz_new_text_device (state.ctx, text);
1106 switch (state.type) {
1107 case DPDF:
1108 u.pdfpage = pdf_load_page (state.u.pdf, pageno);
1109 pdf_run_page (state.u.pdf, u.pdfpage, tdev, fz_identity, NULL);
1110 break;
1112 case DXPS:
1113 u.xpspage = xps_load_page (state.u.xps, pageno);
1114 xps_run_page (state.u.xps, u.xpspage, tdev, fz_identity, NULL);
1115 break;
1117 default:
1118 ARSERT (0 && state.type);
1121 fz_free_device (tdev);
1123 nspans = 0;
1124 for (span = text; span; span = span->next) {
1125 nspans++;
1127 pspan = malloc (sizeof (void *) * nspans);
1128 if (!pspan) {
1129 err (1, "malloc span pointers %" FMT_s, sizeof (void *) * nspans);
1131 for (i = 0, span = text; span; span = span->next, ++i) {
1132 pspan[i] = span;
1134 qsort (pspan, nspans, sizeof (fz_text_span *), comparespans);
1136 j = forward ? 0 : nspans - 1;
1137 while (nspans--) {
1138 regmatch_t rm;
1140 span = pspan[j];
1141 j += forward ? 1 : -1;
1142 p = buf;
1143 for (i = 0; i < span->len; ++i) {
1144 int len;
1145 char cbuf[4];
1147 if (forward) {
1148 if (span->text[i].bbox.y0 < y + 1) {
1149 continue;
1152 else {
1153 if (span->text[i].bbox.y0 > y - 1) {
1154 continue;
1157 len = runetochar (cbuf, &span->text[i].c);
1158 if (sizeof (buf) - 1 - (p - buf) > len) {
1159 int k;
1160 for (k = 0; k < len; ++k)
1161 *p++ = cbuf[k];
1163 else {
1164 break;
1167 if (p == buf) {
1168 continue;
1170 *p++ = 0;
1172 ret = regexec (re, buf, 1, &rm, 0);
1173 if (ret) {
1174 if (ret != REG_NOMATCH) {
1175 size_t size;
1176 char errbuf[80];
1177 size = regerror (ret, re, errbuf, sizeof (errbuf));
1178 printd ("msg regexec error `%.*s'",
1179 (int) size, errbuf);
1180 fz_free_text_span (state.ctx, text);
1181 state.freepage (u.ptr);
1182 free (pspan);
1183 return;
1186 else {
1187 fz_bbox *sb, *eb;
1188 fz_point p1, p2, p3, p4;
1189 int a, b, c, l;
1191 l = span->len;
1192 for (a = 0, c = 0; c < rm.rm_so && a < l; a++) {
1193 c += runelen (span->text[a].c);
1195 for (b = a; c < rm.rm_eo - 1 && b < l; b++) {
1196 c += runelen (span->text[b].c);
1199 if (runelen (span->text[b].c) > 1) {
1200 b = MAX (0, b-1);
1202 sb = &span->text[MIN (a, l-1)].bbox;
1203 eb = &span->text[MIN (b, l-1)].bbox;
1205 p1.x = sb->x0;
1206 p1.y = sb->y0;
1207 p2.x = eb->x1;
1208 p2.y = sb->y0;
1209 p3.x = eb->x1;
1210 p3.y = eb->y1;
1211 p4.x = sb->x0;
1212 p4.y = eb->y1;
1214 switch (state.type) {
1215 case DPDF:
1216 trimctm (u.pdfpage, pdim - state.pagedims);
1217 ctm = fz_concat (pdim->tctm, pdim->zoomctm);
1218 break;
1220 case DXPS:
1221 ctm = pdim->ctm;
1222 break;
1225 p1 = fz_transform_point (ctm, p1);
1226 p2 = fz_transform_point (ctm, p2);
1227 p3 = fz_transform_point (ctm, p3);
1228 p4 = fz_transform_point (ctm, p4);
1230 if (!stop) {
1231 printd ("firstmatch %d %d %f %f %f %f %f %f %f %f",
1232 pageno, 1,
1233 p1.x, p1.y,
1234 p2.x, p2.y,
1235 p3.x, p3.y,
1236 p4.x, p4.y);
1238 printd ("progress 1 found at %d `%.*s' in %f sec",
1239 pageno, (int) (rm.rm_eo - rm.rm_so), &buf[rm.rm_so],
1240 now () - start);
1242 else {
1243 printd ("match %d %d %f %f %f %f %f %f %f %f",
1244 pageno, 2,
1245 p1.x, p1.y,
1246 p2.x, p2.y,
1247 p3.x, p3.y,
1248 p4.x, p4.y);
1250 stop = 1;
1253 if (forward) {
1254 pageno += 1;
1255 y = 0;
1257 else {
1258 pageno -= 1;
1259 y = INT_MAX;
1261 fz_free_text_span (state.ctx, text);
1262 state.freepage (u.ptr);
1263 free (pspan);
1265 end = now ();
1266 if (!stop) {
1267 printd ("progress 1 no matches %f sec", end - start);
1269 printd ("clearrects");
1272 static void set_tex_params (int colorspace)
1274 switch (colorspace) {
1275 case 0:
1276 state.texiform = GL_RGBA8;
1277 state.texform = GL_RGBA;
1278 state.texty = GL_UNSIGNED_BYTE;
1279 state.colorspace = fz_device_rgb;
1280 break;
1281 case 1:
1282 state.texiform = GL_RGBA8;
1283 state.texform = GL_BGRA;
1284 state.texty = fz_is_big_endian ()
1285 ? GL_UNSIGNED_INT_8_8_8_8
1286 : GL_UNSIGNED_INT_8_8_8_8_REV;
1287 state.colorspace = fz_device_bgr;
1288 break;
1289 case 2:
1290 state.texiform = GL_LUMINANCE_ALPHA;
1291 state.texform = GL_LUMINANCE_ALPHA;
1292 state.texty = GL_UNSIGNED_BYTE;
1293 state.colorspace = fz_device_gray;
1294 break;
1295 default:
1296 errx (1, "invalid colorspce %d", colorspace);
1300 static void realloctexts (int texcount)
1302 size_t size;
1304 if (texcount == state.texcount) return;
1306 if (texcount < state.texcount) {
1307 glDeleteTextures (state.texcount - texcount,
1308 state.texids + texcount);
1311 size = texcount * sizeof (*state.texids);
1312 state.texids = realloc (state.texids, size);
1313 if (!state.texids) {
1314 err (1, "realloc texids %" FMT_s, size);
1317 size = texcount * sizeof (*state.texowners);
1318 state.texowners = realloc (state.texowners, size);
1319 if (!state.texowners) {
1320 err (1, "realloc texowners %" FMT_s, size);
1322 if (texcount > state.texcount) {
1323 int i;
1325 glGenTextures (texcount - state.texcount,
1326 state.texids + state.texcount);
1327 for (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 void * mainloop (void *unused)
1338 char *p = NULL;
1339 int len, ret, oldlen = 0;
1341 for (;;) {
1342 len = readlen ();
1343 if (len == 0) {
1344 errx (1, "readlen returned 0");
1347 if (oldlen < len + 1) {
1348 p = realloc (p, len + 1);
1349 if (!p) {
1350 err (1, "realloc %d failed", len + 1);
1352 oldlen = len + 1;
1354 readdata (p, len);
1355 p[len] = 0;
1357 if (!strncmp ("open", p, 4)) {
1358 size_t filenamelen;
1359 char *password;
1360 char *filename = p + 5;
1362 filenamelen = strlen (filename);
1363 password = filename + filenamelen + 1;
1365 openxref (filename, password);
1366 pdfinfo ();
1367 initpdims ();
1368 printd ("msg Opened %s (press h/F1 to get help)", filename);
1369 state.needoutline = 1;
1371 else if (!strncmp ("cs", p, 2)) {
1372 int i, colorspace;
1374 ret = sscanf (p + 2, " %d", &colorspace);
1375 if (ret != 1) {
1376 errx (1, "malformed cs `%.*s' ret=%d", len, p, ret);
1378 lock ("cs");
1379 set_tex_params (colorspace);
1380 for (i = 0; i < state.texcount; ++i) {
1381 state.texowners[i].w = -1;
1382 state.texowners[i].slice = NULL;
1384 unlock ("cs");
1386 else if (!strncmp ("freepage", p, 8)) {
1387 void *ptr;
1389 ret = sscanf (p + 8, " %" FMT_ptr, FMT_ptr_cast (&ptr));
1390 if (ret != 1) {
1391 errx (1, "malformed freepage `%.*s' ret=%d", len, p, ret);
1393 freepage (ptr);
1395 else if (!strncmp ("freetile", p, 8)) {
1396 void *ptr;
1398 ret = sscanf (p + 8, " %" FMT_ptr, FMT_ptr_cast (&ptr));
1399 if (ret != 1) {
1400 errx (1, "malformed freetile `%.*s' ret=%d", len, p, ret);
1402 freetile (ptr);
1404 else if (!strncmp ("search", p, 6)) {
1405 int icase, pageno, y, ret, len2, forward;
1406 char *pattern;
1407 regex_t re;
1409 ret = sscanf (p + 6, " %d %d %d %d,%n",
1410 &icase, &pageno, &y, &forward, &len2);
1411 if (ret != 4) {
1412 errx (1, "malformed search `%s' ret=%d", p, ret);
1415 pattern = p + 6 + len2;
1416 ret = regcomp (&re, pattern,
1417 REG_EXTENDED | (icase ? REG_ICASE : 0));
1418 if (ret) {
1419 char errbuf[80];
1420 size_t size;
1422 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1423 printd ("msg regcomp failed `%.*s'", (int) size, errbuf);
1425 else {
1426 search (&re, pageno, y, forward);
1427 regfree (&re);
1430 else if (!strncmp ("geometry", p, 8)) {
1431 int w, h;
1433 printd ("clear");
1434 ret = sscanf (p + 8, " %d %d", &w, &h);
1435 if (ret != 2) {
1436 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1439 lock ("geometry");
1440 state.h = h;
1441 if (w != state.w) {
1442 int i;
1443 state.w = w;
1444 for (i = 0; i < state.texcount; ++i) {
1445 state.texowners[i].slice = NULL;
1448 layout ();
1449 process_outline ();
1450 state.gen++;
1451 unlock ("geometry");
1452 printd ("continue %d", state.pagecount);
1454 else if (!strncmp ("reqlayout", p, 9)) {
1455 int rotate, proportional;
1457 printd ("clear");
1458 ret = sscanf (p + 9, " %d %d", &rotate, &proportional);
1459 if (ret != 2) {
1460 errx (1, "bad reqlayout line `%.*s' ret=%d", len, p, ret);
1462 lock ("reqlayout");
1463 state.rotate = rotate;
1464 state.proportional = proportional;
1465 layout ();
1466 unlock ("reqlayout");
1467 printd ("continue %d", state.pagecount);
1469 else if (!strncmp ("page", p, 4)) {
1470 double a, b;
1471 struct page *page;
1472 int pageno, pindex, ret;
1474 ret = sscanf (p + 4, " %d %d", &pageno, &pindex);
1475 if (ret != 2) {
1476 errx (1, "bad render line `%.*s' ret=%d", len, p, ret);
1479 lock ("page");
1480 a = now ();
1481 page = loadpage (pageno, pindex);
1482 b = now ();
1483 unlock ("page");
1485 printd ("page %" FMT_ptr " %f", FMT_ptr_cast2 (page), b - a);
1487 else if (!strncmp ("tile", p, 4)) {
1488 int x, y, w, h, ret;
1489 struct page *page;
1490 struct tile *tile;
1491 double a, b;
1493 ret = sscanf (p + 4, " %" FMT_ptr " %d %d %d %d",
1494 FMT_ptr_cast (&page), &x, &y, &w, &h);
1495 if (ret != 5) {
1496 errx (1, "bad tile line `%.*s' ret=%d", len, p, ret);
1499 lock ("tile");
1500 a = now ();
1501 tile = rendertile (page, x, y, w, h);
1502 b = now ();
1503 unlock ("tile");
1505 printd ("tile %d %d %" FMT_ptr " %u %f",
1506 x, y,
1507 FMT_ptr_cast2 (tile),
1508 tile->w * tile->h * tile->pixmap->n,
1509 b - a);
1511 else if (!strncmp ("settrim", p, 7)) {
1512 int trimmargins;
1513 fz_bbox fuzz;
1515 ret = sscanf (p + 7, " %d %d %d %d %d", &trimmargins,
1516 &fuzz.x0, &fuzz.y0, &fuzz.x1, &fuzz.y1);
1517 if (ret != 5) {
1518 errx (1, "malformed settrim `%.*s' ret=%d", len, p, ret);
1520 printd ("clear");
1521 lock ("settrim");
1522 state.trimmargins = trimmargins;
1523 state.needoutline = 1;
1524 if (memcmp (&fuzz, &state.trimfuzz, sizeof (fuzz))) {
1525 state.trimanew = 1;
1526 state.trimfuzz = fuzz;
1528 state.pagedimcount = 0;
1529 free (state.pagedims);
1530 state.pagedims = NULL;
1531 initpdims ();
1532 layout ();
1533 process_outline ();
1534 unlock ("settrim");
1535 printd ("continue %d", state.pagecount);
1537 else if (!strncmp ("sliceh", p, 6)) {
1538 int h;
1540 ret = sscanf (p + 6, " %d", &h);
1541 if (ret != 1) {
1542 errx (1, "malformed sliceh `%.*s' ret=%d", len, p, ret);
1544 if (h != state.sliceheight) {
1545 int i;
1547 state.sliceheight = h;
1548 for (i = 0; i < state.texcount; ++i) {
1549 state.texowners[i].w = -1;
1550 state.texowners[i].h = -1;
1551 state.texowners[i].slice = NULL;
1555 else if (!strncmp ("interrupt", p, 9)) {
1556 printd ("vmsg interrupted");
1558 else if (!strncmp ("quit", p, 4)) {
1559 return 0;
1561 else {
1562 errx (1, "unknown command %.*s", len, p);
1565 return 0;
1568 CAMLprim value ml_realloctexts (value texcount_v)
1570 CAMLparam1 (texcount_v);
1571 int ok;
1573 if (trylock ("ml_realloctexts")) {
1574 ok = 0;
1575 goto done;
1577 realloctexts (Int_val (texcount_v));
1578 ok = 1;
1579 unlock ("ml_realloctexts");
1581 done:
1582 CAMLreturn (Val_bool (ok));
1585 static void showsel (struct page *page, int ox, int oy)
1587 fz_bbox bbox;
1588 fz_text_span *span;
1589 struct mark first, last;
1591 first = page->fmark;
1592 last = page->lmark;
1594 if (!first.span || !last.span) return;
1596 glEnable (GL_BLEND);
1597 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1598 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1600 for (span = first.span; span; span = span->next) {
1601 int i, j, k;
1603 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0;
1605 j = 0;
1606 k = span->len - 1;
1608 if (span == page->fmark.span && span == page->lmark.span) {
1609 j = MIN (first.i, last.i);
1610 k = MAX (first.i, last.i);
1612 else if (span == first.span) {
1613 j = first.i;
1615 else if (span == last.span) {
1616 k = last.i;
1619 for (i = j; i <= k; ++i) {
1620 bbox = fz_union_bbox (bbox, span->text[i].bbox);
1622 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1623 bbox.x0,
1624 bbox.y0,
1625 bbox.x1,
1626 bbox.y1,
1627 oy, ox);
1629 glRecti (bbox.x0 + ox, bbox.y0 + oy, bbox.x1 + ox, bbox.y1 + oy);
1631 if (span == last.span) break;
1633 glDisable (GL_BLEND);
1636 static void highlightlinks (struct page *page, int xoff, int yoff)
1638 fz_matrix ctm;
1639 fz_link *link, *links;
1641 switch (page->type) {
1642 case DPDF:
1643 links = page->u.pdfpage->links;
1644 break;
1646 case DXPS:
1647 links = page->u.xpspage->links;
1648 break;
1650 default:
1651 return;
1654 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1655 glEnable (GL_LINE_STIPPLE);
1656 glLineStipple (0.5, 0xcccc);
1658 ctm = fz_concat (pagectm (page), fz_translate (xoff, yoff));
1660 glBegin (GL_QUADS);
1661 for (link = links; link; link = link->next) {
1662 fz_point p1, p2, p3, p4;
1664 p1.x = link->rect.x0;
1665 p1.y = link->rect.y0;
1667 p2.x = link->rect.x1;
1668 p2.y = link->rect.y0;
1670 p3.x = link->rect.x1;
1671 p3.y = link->rect.y1;
1673 p4.x = link->rect.x0;
1674 p4.y = link->rect.y1;
1676 p1 = fz_transform_point (ctm, p1);
1677 p2 = fz_transform_point (ctm, p2);
1678 p3 = fz_transform_point (ctm, p3);
1679 p4 = fz_transform_point (ctm, p4);
1681 switch (link->dest.kind) {
1682 case FZ_LINK_GOTO: glColor3ub (255, 0, 0); break;
1683 case FZ_LINK_URI: glColor3ub (0, 0, 255); break;
1684 default: glColor3ub (0, 0, 0); break;
1687 glVertex2f (p1.x, p1.y);
1688 glVertex2f (p2.x, p2.y);
1689 glVertex2f (p3.x, p3.y);
1690 glVertex2f (p4.x, p4.y);
1692 glEnd ();
1694 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1695 glDisable (GL_LINE_STIPPLE);
1698 static void uploadslice (struct tile *tile, struct slice *slice)
1700 int offset;
1701 struct slice *slice1;
1703 offset = 0;
1704 for (slice1 = tile->slices; slice != slice1; slice1++) {
1705 offset += slice1->h * tile->w * tile->pixmap->n;
1707 if (slice->texindex != -1 && slice->texindex < state.texcount
1708 && state.texowners[slice->texindex].slice == slice) {
1709 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
1711 else {
1712 int subimage = 0;
1713 int texindex = state.texindex++ % state.texcount;
1715 if (state.texowners[texindex].w == tile->w) {
1716 if (state.texowners[texindex].h >= slice->h) {
1717 subimage = 1;
1719 else {
1720 state.texowners[texindex].h = slice->h;
1723 else {
1724 state.texowners[texindex].h = slice->h;
1727 state.texowners[texindex].w = tile->w;
1728 state.texowners[texindex].slice = slice;
1729 slice->texindex = texindex;
1731 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[texindex]);
1732 if (subimage) {
1733 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
1737 tile->w,
1738 slice->h,
1739 state.texform,
1740 state.texty,
1741 tile->pixmap->samples+offset
1744 else {
1745 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
1747 state.texiform,
1748 tile->w,
1749 slice->h,
1751 state.texform,
1752 state.texty,
1753 tile->pixmap->samples+offset
1759 CAMLprim value ml_drawtile (value args_v, value ptr_v)
1761 CAMLparam2 (args_v, ptr_v);
1762 int dispx = Int_val (Field (args_v, 0));
1763 int dispy = Int_val (Field (args_v, 1));
1764 int dispw = Int_val (Field (args_v, 2));
1765 int disph = Int_val (Field (args_v, 3));
1766 int tilex = Int_val (Field (args_v, 4));
1767 int tiley = Int_val (Field (args_v, 5));
1768 char *s = String_val (ptr_v);
1769 struct tile *tile = parse_pointer ("ml_drawtile", s);
1771 glEnable (GL_TEXTURE_RECTANGLE_ARB);
1773 int slicey, firstslice;
1774 struct slice *slice;
1776 firstslice = tiley / tile->sliceheight;
1777 slice = &tile->slices[firstslice];
1778 slicey = tiley % tile->sliceheight;
1780 while (disph > 0) {
1781 int dh;
1783 dh = slice->h - slicey;
1784 dh = MIN (disph, dh);
1785 uploadslice (tile, slice);
1787 glBegin (GL_QUADS);
1789 glTexCoord2i (tilex, slicey);
1790 glVertex2i (dispx, dispy);
1792 glTexCoord2i (tilex+dispw, slicey);
1793 glVertex2i (dispx+dispw, dispy);
1795 glTexCoord2i (tilex+dispw, slicey+dh);
1796 glVertex2i (dispx+dispw, dispy+dh);
1798 glTexCoord2i (tilex, slicey+dh);
1799 glVertex2i (dispx, dispy+dh);
1801 glEnd ();
1803 dispy += dh;
1804 disph -= dh;
1805 slice++;
1806 ARSERT (!(slice - tile->slices >= tile->slicecount && disph > 0));
1807 slicey = 0;
1810 glDisable (GL_TEXTURE_RECTANGLE_ARB);
1811 CAMLreturn (Val_unit);
1814 CAMLprim value ml_postprocess (value ptr_v, value hlinks_v,
1815 value xoff_v, value yoff_v)
1817 CAMLparam4 (ptr_v, hlinks_v, xoff_v, yoff_v);
1818 int xoff = Int_val (xoff_v);
1819 int yoff = Int_val (yoff_v);
1820 char *s = String_val (ptr_v);
1821 struct page *page = parse_pointer ("ml_postprocess", s);
1823 if (Bool_val (hlinks_v)) highlightlinks (page, xoff, yoff);
1825 if (trylock ("ml_postprocess")) {
1826 goto done;
1828 showsel (page, xoff, yoff);
1829 unlock ("ml_postprocess");
1831 done:
1832 CAMLreturn (Val_unit);
1835 static fz_link *getlink (struct page *page, int x, int y)
1837 fz_point p;
1838 fz_matrix ctm;
1839 fz_link *link, *links;
1841 switch (page->type) {
1842 case DPDF:
1843 links = page->u.pdfpage->links;
1844 break;
1846 case DXPS:
1847 links = page->u.xpspage->links;
1848 break;
1850 default:
1851 return NULL;
1853 p.x = x;
1854 p.y = y;
1856 ctm = fz_concat (trimctm (page->u.pdfpage, page->pdimno),
1857 state.pagedims[page->pdimno].ctm);
1858 ctm = fz_invert_matrix (ctm);
1859 p = fz_transform_point (ctm, p);
1861 for (link = links; link; link = link->next) {
1862 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
1863 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
1864 return link;
1868 return NULL;
1871 static void droptext (struct page *page)
1873 if (page->text) {
1874 fz_free_text_span (state.ctx, page->text);
1875 page->fmark.i = -1;
1876 page->lmark.i = -1;
1877 page->fmark.span = NULL;
1878 page->lmark.span = NULL;
1879 page->text = NULL;
1883 static void ensuretext (struct page *page)
1885 if (state.gen != page->gen) {
1886 droptext (page);
1887 page->gen = state.gen;
1889 if (!page->text) {
1890 fz_device *tdev;
1892 page->text = fz_new_text_span (state.ctx);
1893 tdev = fz_new_text_device (state.ctx, page->text);
1894 fz_run_display_list (page->dlist,
1895 tdev,
1896 pagectm (page),
1897 fz_infinite_bbox, NULL);
1898 fz_free_device (tdev);
1902 CAMLprim value ml_whatsunder (value ptr_v, value x_v, value y_v)
1904 CAMLparam3 (ptr_v, x_v, y_v);
1905 CAMLlocal3 (ret_v, tup_v, str_v);
1906 int pageno;
1907 fz_link *link;
1908 struct page *page;
1909 char *s = String_val (ptr_v);
1910 int x = Int_val (x_v), y = Int_val (y_v);
1911 struct pagedim *pdim;
1913 ret_v = Val_int (0);
1914 if (trylock ("ml_whatsunder")) {
1915 goto done;
1918 page = parse_pointer ("ml_whatsunder", s);
1919 pdim = &state.pagedims[page->pdimno];
1920 link = getlink (page, x, y);
1921 if (link) {
1922 switch (link->dest.kind) {
1923 case FZ_LINK_GOTO:
1925 fz_point p;
1927 pageno = link->dest.ld.gotor.page;
1928 p.x = 0;
1929 p.y = 0;
1931 if (link->dest.ld.gotor.flags & fz_link_flag_t_valid) {
1932 p.y = link->dest.ld.gotor.lt.y;
1933 p = fz_transform_point (pdim->lctm, p);
1935 tup_v = caml_alloc_tuple (2);
1936 ret_v = caml_alloc_small (1, 1);
1937 Field (tup_v, 0) = Val_int (pageno);
1938 Field (tup_v, 1) = Val_int (p.y);
1939 Field (ret_v, 0) = tup_v;
1941 break;
1943 case FZ_LINK_URI:
1944 str_v = caml_copy_string (link->dest.ld.uri.uri);
1945 ret_v = caml_alloc_small (1, 0);
1946 Field (ret_v, 0) = str_v;
1947 break;
1949 case FZ_LINK_LAUNCH:
1950 str_v = caml_copy_string (link->dest.ld.launch.file_spec);
1951 ret_v = caml_alloc_small (1, 4);
1952 Field (ret_v, 0) = str_v;
1953 break;
1955 case FZ_LINK_NAMED:
1956 str_v = caml_copy_string (link->dest.ld.named.named);
1957 ret_v = caml_alloc_small (1, 5);
1958 Field (ret_v, 0) = str_v;
1959 break;
1961 case FZ_LINK_GOTOR:
1962 str_v = caml_copy_string (link->dest.ld.gotor.file_spec);
1963 pageno = link->dest.ld.gotor.page;
1964 tup_v = caml_alloc_tuple (2);
1965 ret_v = caml_alloc_small (1, 6);
1966 Field (tup_v, 0) = str_v;
1967 Field (tup_v, 1) = Val_int (pageno);
1968 Field (ret_v, 0) = tup_v;
1969 break;
1971 default:
1973 char buf[80];
1975 snprintf (buf, sizeof (buf),
1976 "unhandled link kind %d", link->dest.kind);
1977 str_v = caml_copy_string (buf);
1978 ret_v = caml_alloc_small (1, 3);
1979 Field (ret_v, 0) = str_v;
1981 break;
1984 else {
1985 int i;
1986 fz_text_span *span;
1988 ensuretext (page);
1989 for (span = page->text; span; span = span->next) {
1990 for (i = 0; i < span->len; ++i) {
1991 fz_bbox *b;
1992 b = &span->text[i].bbox;
1993 if ((x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1)) {
1994 const char *n2 =
1995 span->font && span->font->name
1996 ? span->font->name
1997 : "Span has no font name"
1999 FT_FaceRec *face = span->font->ft_face;
2000 if (face && face->family_name) {
2001 char *s;
2002 char *n1 = face->family_name;
2003 size_t l1 = strlen (n1);
2004 size_t l2 = strlen (n2);
2006 if (l1 != l2 || memcmp (n1, n2, l1)) {
2007 s = malloc (l1 + l2 + 2);
2008 if (s) {
2009 memcpy (s, n2, l2);
2010 s[l2] = '=';
2011 memcpy (s + l2 + 1, n1, l1 + 1);
2012 str_v = caml_copy_string (s);
2013 free (s);
2017 if (str_v == 0) {
2018 str_v = caml_copy_string (n2);
2020 ret_v = caml_alloc_small (1, 2);
2021 Field (ret_v, 0) = str_v;
2022 goto unlock;
2027 unlock:
2028 unlock ("ml_whatsunder");
2030 done:
2031 CAMLreturn (ret_v);
2034 CAMLprim value ml_seltext (value ptr_v, value rect_v)
2036 CAMLparam2 (ptr_v, rect_v);
2037 fz_bbox *b;
2038 struct page *page;
2039 fz_text_span *span;
2040 struct mark first, last;
2041 int i, x0, x1, y0, y1;
2042 char *s = String_val (ptr_v);
2044 if (trylock ("ml_seltext")) {
2045 goto done;
2048 page = parse_pointer ("ml_seltext", s);
2049 ensuretext (page);
2051 x0 = Int_val (Field (rect_v, 0));
2052 y0 = Int_val (Field (rect_v, 1));
2053 x1 = Int_val (Field (rect_v, 2));
2054 y1 = Int_val (Field (rect_v, 3));
2056 if (0) {
2057 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
2058 glColor3ub (128, 128, 128);
2059 glRecti (x0, y0, x1, y1);
2060 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
2063 first.span = NULL;
2064 last.span = NULL;
2066 last.i = first.i = 0;
2067 first.span = page->text;
2068 for (span = page->text; span; span = span->next) {
2069 for (i = 0; i < span->len; ++i) {
2070 b = &span->text[i].bbox;
2071 int selected = 0;
2073 if (x0 >= b->x0 && x0 <= b->x1 && y0 >= b->y0 && y0 <= b->y1) {
2074 first.i = i;
2075 first.span = span;
2076 selected = 1;
2078 if (x1 >= b->x0 && x1 <= b->x1 && y1 >= b->y0 && y1 <= b->y1) {
2079 last.i = i;
2080 last.span = span;
2081 selected = 1;
2083 if (0 && selected) {
2084 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
2085 glColor3ub (128, 128, 128);
2086 glRecti (b->x0, b->y0, b->x1, b->y1);
2087 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
2092 if (y1 < y0 || x1 < x0) {
2093 int swap = 0;
2095 if (first.span == last.span) {
2096 swap = 1;
2098 else {
2099 if (y1 < y0) {
2100 for (span = first.span; span && span != last.span;
2101 span = span->next) {
2102 if (span->eol) {
2103 swap = 1;
2104 break;
2110 if (swap) {
2111 i = first.i;
2112 span = first.span;
2113 first.i = last.i;
2114 first.span = last.span;
2115 last.i = i;
2116 last.span = span;
2120 page->fmark = first;
2121 page->lmark = last;
2123 unlock ("ml_seltext");
2125 done:
2126 CAMLreturn (Val_unit);
2129 static int pipespan (FILE *f, fz_text_span *span, int a, int b)
2131 char buf[4];
2132 int i, len, ret;
2134 for (i = a; i <= b; ++i) {
2135 len = runetochar (buf, &span->text[i].c);
2136 ret = fwrite (buf, len, 1, f);
2138 if (ret != 1) {
2139 fprintf (stderr, "failed to write %d bytes ret=%d: %s\n",
2140 len, ret, strerror (errno));
2141 return -1;
2144 return 0;
2147 CAMLprim value ml_copysel (value command_v, value ptr_v)
2149 CAMLparam1 (ptr_v);
2150 FILE *f;
2151 struct page *page;
2152 fz_text_span *span;
2153 char *s = String_val (ptr_v);
2154 char *command = String_val (command_v);
2156 if (trylock ("ml_copysel")) {
2157 goto done;
2160 page = parse_pointer ("ml_sopysel", s);
2162 if (!page->fmark.span || !page->lmark.span) {
2163 fprintf (stderr, "nothing to copy\n");
2164 goto unlock;
2167 f = popen (command, "w");
2168 if (!f) {
2169 fprintf (stderr, "failed to open sel pipe: %s\n",
2170 strerror (errno));
2171 f = stdout;
2174 for (span = page->fmark.span;
2175 span && span != page->lmark.span->next;
2176 span = span->next) {
2177 int a = span == page->fmark.span ? page->fmark.i : 0;
2178 int b = span == page->lmark.span ? page->lmark.i : span->len - 1;
2179 if (pipespan (f, span, a, b)) {
2180 goto close;
2182 if (span->eol) {
2183 if (putc ('\n', f) == EOF) {
2184 fprintf (stderr, "failed break line on sel pipe: %s\n",
2185 strerror (errno));
2186 goto close;
2190 page->lmark.span = NULL;
2191 page->fmark.span = NULL;
2193 close:
2194 if (f != stdout) {
2195 int ret = pclose (f);
2196 if (ret == -1) {
2197 if (errno != ECHILD) {
2198 fprintf (stderr, "failed to close sel pipe: %s\n",
2199 strerror (errno));
2203 unlock:
2204 unlock ("ml_copysel");
2206 done:
2207 CAMLreturn (Val_unit);
2210 CAMLprim value ml_getpdimrect (value pagedimno_v)
2212 CAMLparam1 (pagedimno_v);
2213 CAMLlocal1 (ret_v);
2214 int pagedimno = Int_val (pagedimno_v);
2215 fz_rect box;
2217 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
2218 if (trylock ("ml_getpdimrect")) {
2219 box = fz_empty_rect;
2221 else {
2222 box = state.pagedims[pagedimno].mediabox;
2223 unlock ("ml_getpdimrect");
2226 Store_double_field (ret_v, 0, box.x0);
2227 Store_double_field (ret_v, 1, box.x1);
2228 Store_double_field (ret_v, 2, box.y0);
2229 Store_double_field (ret_v, 3, box.y1);
2231 CAMLreturn (ret_v);
2234 static double getmaxw (void)
2236 int i;
2237 struct pagedim *p;
2238 double maxw = 0.0;
2240 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
2241 double x0, x1, w;
2243 x0 = MIN (p->mediabox.x0, p->mediabox.x1);
2244 x1 = MAX (p->mediabox.x0, p->mediabox.x1);
2246 w = x1 - x0;
2247 maxw = MAX (w, maxw);
2249 return maxw;
2252 CAMLprim value ml_getmaxw (value unit_v)
2254 CAMLparam1 (unit_v);
2255 CAMLlocal1 (ret_v);
2256 double maxw = 0.0;
2258 if (trylock ("ml_getmaxw")) {
2259 goto done;
2261 maxw = getmaxw ();
2262 unlock ("ml_getmaxw");
2263 done:
2264 ret_v = caml_copy_double (maxw);
2265 CAMLreturn (ret_v);
2268 CAMLprim value ml_zoom_for_height (value winw_v, value winh_v, value dw_v)
2270 CAMLparam3 (winw_v, winh_v, dw_v);
2271 CAMLlocal1 (ret_v);
2272 int i;
2273 double zoom = 1.0;
2274 double maxw = 0.0, maxh = 0.0;
2275 struct pagedim *p;
2276 double winw = Int_val (winw_v);
2277 double winh = Int_val (winh_v);
2278 double dw = Int_val (dw_v);
2279 double pw = 1.0, ph = 1.0, num, den;
2281 if (trylock ("ml_zoom_for_height")) {
2282 goto done;
2285 if (state.proportional) {
2286 maxw = getmaxw ();
2289 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
2290 double x0, x1, y0, y1, w, h, scaledh, scale;
2292 x0 = MIN (p->mediabox.x0, p->mediabox.x1);
2293 x1 = MAX (p->mediabox.x0, p->mediabox.x1);
2294 y0 = MIN (p->mediabox.y0, p->mediabox.y1);
2295 y1 = MAX (p->mediabox.y0, p->mediabox.y1);
2297 w = x1 - x0;
2298 h = y1 - y0;
2300 if (state.proportional) {
2301 scale = w / maxw;
2302 scaledh = h * scale;
2304 else {
2305 scale = 1.0;
2306 scaledh = h;
2309 if (scaledh > maxh) {
2310 maxh = scaledh;
2311 ph = scaledh;
2312 pw = w * scale;
2316 num = (winh * pw) + (ph * dw);
2317 den = ph * winw;
2318 zoom = num / den;
2320 unlock ("ml_zoom_for_height");
2321 done:
2322 ret_v = caml_copy_double (zoom);
2323 CAMLreturn (ret_v);
2326 #include "glfont.c"
2328 CAMLprim value ml_draw_string (value pt_v, value x_v, value y_v, value string_v)
2330 CAMLparam4 (pt_v, x_v, y_v, string_v);
2331 CAMLlocal1 (ret_v);
2332 int pt = Int_val(pt_v);
2333 int x = Int_val (x_v);
2334 int y = Int_val (y_v);
2335 double w;
2337 w = draw_string (state.face, pt, x, y, String_val (string_v));
2338 ret_v = caml_copy_double (w);
2339 CAMLreturn (ret_v);
2342 CAMLprim value ml_measure_string (value pt_v, value string_v)
2344 CAMLparam2 (pt_v, string_v);
2345 CAMLlocal1 (ret_v);
2346 int pt = Int_val (pt_v);
2347 double w;
2349 w = measure_string (state.face, pt, String_val (string_v));
2350 ret_v = caml_copy_double (w);
2351 CAMLreturn (ret_v);
2354 CAMLprim value ml_getpagebox (value opaque_v)
2356 CAMLparam1 (opaque_v);
2357 CAMLlocal1 (ret_v);
2358 fz_bbox bbox;
2359 fz_device *dev;
2360 char *s = String_val (opaque_v);
2361 struct page *page = parse_pointer ("ml_getpagebox", s);
2363 ret_v = caml_alloc_tuple (4);
2364 dev = fz_new_bbox_device (state.ctx, &bbox);
2365 dev->hints |= FZ_IGNORE_SHADE;
2367 switch (page->type) {
2368 case DPDF:
2369 pdf_run_page (state.u.pdf, page->u.pdfpage, dev, pagectm (page), NULL);
2370 break;
2372 case DXPS:
2373 xps_run_page (state.u.xps, page->u.xpspage, dev, pagectm (page), NULL);
2374 break;
2376 default:
2377 bbox = fz_infinite_bbox;
2378 break;
2381 fz_free_device (dev);
2382 Field (ret_v, 0) = Val_int (bbox.x0);
2383 Field (ret_v, 1) = Val_int (bbox.y0);
2384 Field (ret_v, 2) = Val_int (bbox.x1);
2385 Field (ret_v, 3) = Val_int (bbox.y1);
2387 CAMLreturn (ret_v);
2390 CAMLprim value ml_setaalevel (value level_v)
2392 CAMLparam1 (level_v);
2394 state.aalevel = Int_val (level_v);
2395 CAMLreturn (Val_unit);
2398 #undef pixel
2399 #include <X11/Xlib.h>
2400 #include <GL/glx.h>
2402 static struct {
2403 Display *dpy;
2404 GLXContext ctx;
2405 GLXDrawable drawable;
2406 } glx;
2408 #include "keysym2ucs.c"
2410 CAMLprim value ml_keysymtoutf8 (value keysym_v)
2412 CAMLparam1 (keysym_v);
2413 CAMLlocal1 (str_v);
2414 KeySym keysym = Int_val (keysym_v);
2415 Rune rune;
2416 int len;
2417 char buf[5];
2419 rune = keysym2ucs (keysym);
2420 len = runetochar (buf, &rune);
2421 buf[len] = 0;
2422 str_v = caml_copy_string (buf);
2423 CAMLreturn (str_v);
2426 CAMLprim value ml_glx (value win_v)
2428 CAMLparam1 (win_v);
2429 int screen;
2430 XVisualInfo *visual;
2431 int attributes[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
2433 glx.dpy = XOpenDisplay (NULL);
2434 if (!glx.dpy) {
2435 caml_failwith ("XOpenDisplay failed");
2438 screen = DefaultScreen (glx.dpy);
2439 visual = glXChooseVisual (glx.dpy, screen, attributes);
2440 if (!visual) {
2441 XCloseDisplay (glx.dpy);
2442 glx.dpy = NULL;
2443 caml_failwith ("glXChooseVisual");
2446 glx.ctx = glXCreateContext (glx.dpy, visual, NULL, True);
2447 if (!glx.ctx) {
2448 XCloseDisplay (glx.dpy);
2449 XFree (visual);
2450 glx.dpy = NULL;
2451 caml_failwith ("glXCreateContext");
2454 XFree (visual);
2455 if (!glXMakeCurrent (glx.dpy, Int_val (win_v), glx.ctx)) {
2456 glXDestroyContext (glx.dpy, glx.ctx);
2457 XCloseDisplay (glx.dpy);
2458 glx.dpy = NULL;
2459 glx.ctx = NULL;
2460 caml_failwith ("glXMakeCurrent");
2462 glx.drawable = Int_val (win_v);
2464 CAMLreturn (Val_unit);
2467 CAMLprim value ml_swapb (value unit_v)
2469 CAMLparam1 (unit_v);
2470 glXSwapBuffers (glx.dpy, glx.drawable);
2471 CAMLreturn (Val_unit);
2474 enum { piunknown, pilinux, piosx, pisun, pifreebsd,
2475 pidragonflybsd, piopenbsd, pinetbsd, picygwin };
2477 CAMLprim value ml_platform (value unit_v)
2479 CAMLparam1 (unit_v);
2480 int platid = piunknown;
2482 #if defined __linux__
2483 platid = pilinux;
2484 #elif defined __CYGWIN__
2485 platid = picygwin;
2486 #elif defined __DragonFly__
2487 platid = pidragonflybsd;
2488 #elif defined __FreeBSD__
2489 platid = pifreebsd;
2490 #elif defined __OpenBSD__
2491 platid = piopenbsd;
2492 #elif defined __NetBSD__
2493 platid = pinetbsd;
2494 #elif defined __sun__
2495 platid = pisun;
2496 #elif defined __APPLE__
2497 platid = piosx;
2498 #endif
2499 CAMLreturn (Val_int (platid));
2502 CAMLprim value ml_cloexec (value fd_v)
2504 CAMLparam1 (fd_v);
2505 int fd = Int_val (fd_v);
2507 if (fcntl (fd, F_SETFD, FD_CLOEXEC, 1)) {
2508 uerror ("fcntl", Nothing);
2510 CAMLreturn (Val_unit);
2513 CAMLprim value ml_init (value pipe_v, value params_v)
2515 CAMLparam2 (pipe_v, params_v);
2516 CAMLlocal2 (trim_v, fuzz_v);
2517 int ret;
2518 int texcount;
2519 char *fontpath;
2520 int colorspace;
2521 int mustoresize;
2522 struct sigaction sa;
2524 state.cr = Int_val (Field (pipe_v, 0));
2525 state.cw = Int_val (Field (pipe_v, 1));
2526 state.rotate = Int_val (Field (params_v, 0));
2527 state.proportional = Bool_val (Field (params_v, 1));
2528 trim_v = Field (params_v, 2);
2529 texcount = Int_val (Field (params_v, 3));
2530 state.sliceheight = Int_val (Field (params_v, 4));
2531 mustoresize = Int_val (Field (params_v, 5));
2532 colorspace = Int_val (Field (params_v, 6));
2533 fontpath = String_val (Field (params_v, 7));
2535 state.ctx = fz_new_context (NULL, NULL, mustoresize);
2536 state.trimmargins = Bool_val (Field (trim_v, 0));
2538 fuzz_v = Field (trim_v, 1);
2539 state.trimfuzz.x0 = Int_val (Field (fuzz_v, 0));
2540 state.trimfuzz.y0 = Int_val (Field (fuzz_v, 1));
2541 state.trimfuzz.x1 = Int_val (Field (fuzz_v, 2));
2542 state.trimfuzz.y1 = Int_val (Field (fuzz_v, 3));
2544 set_tex_params (colorspace);
2546 if (*fontpath) {
2547 state.face = load_font (fontpath);
2549 else {
2550 unsigned int len;
2551 void *base = pdf_find_substitute_font (0, 0, 0, 0, &len);
2553 state.face = load_builtin_font (base, len);
2555 if (!state.face) _exit (1);
2557 realloctexts (texcount);
2559 sa.sa_handler = SIG_DFL;
2560 if (sigemptyset (&sa.sa_mask)) {
2561 err (1, "sigemptyset");
2563 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP | SA_NOCLDWAIT;
2564 if (sigaction (SIGCHLD, &sa, NULL)) {
2565 err (1, "sigaction");
2568 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
2569 if (ret) {
2570 errx (1, "pthread_create: %s", strerror (ret));
2573 CAMLreturn (Val_unit);