Refactor
[llpp.git] / link.c
blob3395b148454f92ca936c417f555cd8f9d226a486
1 /* lots of code c&p-ed directly from mupdf */
2 #ifdef _WIN32
3 #define WIN32_LEAN_AND_MEAN
4 #include <windows.h>
5 #include <winsock2.h>
6 #define fionread_arg long
7 #define ssize_t int
8 #define FMT_ss "%d"
9 #ifdef _WIN64
10 #define FMT_s "%i64u"
11 #else
12 #define FMT_s "%u"
13 #endif
14 #pragma warning (disable:4244)
15 #pragma warning (disable:4996)
16 #pragma warning (disable:4995)
17 #endif
19 #ifdef _MSC_VER
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 static void __declspec (noreturn) err (int exitcode, const char *fmt, ...)
26 va_list ap;
27 int errcode;
29 errcode = errno;
30 va_start (ap, fmt);
31 vfprintf (stderr, fmt, ap);
32 va_end (ap);
33 fprintf (stderr, ": %s\n", strerror (errno));
34 exit (exitcode);
36 static void __declspec (noreturn) errx (int exitcode, const char *fmt, ...)
38 va_list ap;
40 va_start (ap, fmt);
41 vfprintf (stderr, fmt, ap);
42 va_end (ap);
43 fputc ('\n', stderr);
44 exit (exitcode);
46 static void __declspec (noreturn) sockerr (int exitcode, const char *fmt, ...)
48 va_list ap;
50 va_start (ap, fmt);
51 vfprintf (stderr, fmt, ap);
52 va_end (ap);
53 fprintf (stderr, ": wsaerror 0x%x\n", WSAGetLastError ());
54 exit (exitcode);
56 #else
57 #define FMT_ss "%zd"
58 #define FMT_s "%zu"
59 #define fionread_arg int
60 #define ioctlsocket ioctl
61 #define _GNU_SOURCE
62 #include <err.h>
63 #define sockerr err
64 #endif
65 #include <regex.h>
66 #include <errno.h>
67 #include <ctype.h>
68 #include <stdio.h>
69 #include <stdarg.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <limits.h>
73 #ifndef _WIN32
74 #include <pthread.h>
75 #include <sys/time.h>
76 #include <sys/types.h>
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
79 #endif
81 #ifdef __APPLE__
82 #include <OpenGL/gl.h>
83 #else
84 #include <GL/gl.h>
85 #endif
87 #ifndef GL_TEXTURE_RECTANGLE_ARB
88 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
89 #endif
91 #include <caml/fail.h>
92 #include <caml/alloc.h>
93 #include <caml/memory.h>
94 #include <caml/unixsupport.h>
96 #include <fitz.h>
97 #include <mupdf.h>
99 #if 0
100 #define lprintf printf
101 #else
102 #define lprintf(...)
103 #endif
105 #ifdef FT_FREETYPE_H
106 #include FT_FREETYPE_H
107 #endif
109 #define ARSERT(cond) for (;;) { \
110 if (!(cond)) { \
111 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
113 break; \
116 struct slice {
117 int texindex;
118 int w, h;
121 struct pagedim {
122 int pageno;
123 int rotate;
124 int left;
125 fz_rect box;
126 fz_bbox bbox;
127 fz_matrix ctm, ctm1;
130 struct page {
131 int pageno;
132 int slicecount;
133 fz_text_span *text;
134 fz_pixmap *pixmap;
135 pdf_page *drawpage;
136 struct pagedim pagedim;
137 struct mark {
138 int i;
139 fz_text_span *span;
140 } fmark, lmark;
141 struct slice slices[];
144 #if !defined _WIN32 && !defined __APPLE__
145 #define USE_XSEL
146 #endif
148 struct {
149 int sock;
150 int sliceheight;
151 struct page *pig;
152 struct pagedim *pagedims;
153 int pagecount;
154 int pagedimcount;
155 pdf_xref *xref;
156 fz_glyph_cache *cache;
157 int w, h;
159 int texindex;
160 int texcount;
161 GLuint *texids;
163 GLenum texform;
164 GLenum texty;
166 struct {
167 int w, h;
168 struct slice *slice;
169 } *texowners;
171 int rotate;
172 int proportional;
173 int needoutline;
175 #ifdef _WIN32
176 HANDLE thread;
177 #else
178 pthread_t thread;
179 #endif
180 FILE *xselpipe;
181 } state;
183 #ifdef _WIN32
184 static CRITICAL_SECTION critsec;
186 static void lock (void *unused)
188 (void) unused;
189 EnterCriticalSection (&critsec);
192 static void unlock (void *unused)
194 (void) unused;
195 LeaveCriticalSection (&critsec);
198 static int trylock (void *unused)
200 return TryEnterCriticalSection (&critsec) == 0;
202 #else
203 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
205 static void lock (const char *cap)
207 int ret = pthread_mutex_lock (&mutex);
208 if (ret) {
209 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
213 static void unlock (const char *cap)
215 int ret = pthread_mutex_unlock (&mutex);
216 if (ret) {
217 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
221 static int trylock (const char *cap)
223 int ret = pthread_mutex_trylock (&mutex);
225 if (ret && ret != EBUSY) {
226 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
228 return ret == EBUSY;
230 #endif
232 static void *parse_pointer (const char *cap, const char *s)
234 int ret;
235 void *ptr;
237 ret = sscanf (s, "%p", &ptr);
238 if (ret != 1) {
239 errx (1, "%s: cannot parse pointer in `%s'", cap, s);
241 return ptr;
244 static int hasdata (int sock)
246 int ret;
247 fionread_arg avail;
248 ret = ioctlsocket (sock, FIONREAD, &avail);
249 if (ret) sockerr (1, "hasdata: FIONREAD error ret=%d", ret);
250 return avail > 0;
253 static double now (void)
255 struct timeval tv;
257 if (gettimeofday (&tv, NULL)) {
258 err (1, "gettimeofday");
260 return tv.tv_sec + tv.tv_usec*1e-6;
263 static void readdata (int fd, char *p, int size)
265 ssize_t n;
267 n = recv (fd, p, size, 0);
268 if (n - size) {
269 if (!n) errx (1, "EOF while reading");
270 sockerr (1, "recv (req %d, ret " FMT_ss ")", size, n);
274 static void writedata (int fd, char *p, int size)
276 char buf[4];
277 ssize_t n;
279 buf[0] = (size >> 24) & 0xff;
280 buf[1] = (size >> 16) & 0xff;
281 buf[2] = (size >> 8) & 0xff;
282 buf[3] = (size >> 0) & 0xff;
284 n = send (fd, buf, 4, 0);
285 if (n != 4) {
286 if (!n) errx (1, "EOF while writing length");
287 sockerr (1, "send " FMT_ss, n);
290 n = send (fd, p, size, 0);
291 if (n - size) {
292 if (!n) errx (1, "EOF while writing data");
293 sockerr (1, "send (req %d, ret " FMT_ss ")", size, n);
297 static void
298 #ifdef __GNUC__
299 __attribute__ ((format (printf, 2, 3)))
300 #endif
301 printd (int fd, const char *fmt, ...)
303 int size = 200, len;
304 va_list ap;
305 char *buf;
307 buf = malloc (size);
308 for (;;) {
309 if (!buf) err (errno, "malloc for temp buf (%d bytes) failed", size);
311 va_start (ap, fmt);
312 len = vsnprintf (buf, size, fmt, ap);
313 va_end (ap);
315 if (len > -1 && len < size) {
316 writedata (fd, buf, len);
317 break;
320 if (len > -1) {
321 size = len + 1;
323 else {
324 size *= 2;
326 buf = realloc (buf, size);
328 free (buf);
331 static void die (fz_error error)
333 fz_catch (error, "aborting");
334 if (state.xref)
335 pdf_free_xref (state.xref);
336 exit (1);
339 static void openxref (char *filename, char *password)
341 int i;
342 fz_error error;
344 for (i = 0; i < state.texcount; ++i) {
345 state.texowners[i].slice = NULL;
348 if (state.cache) {
349 fz_free_glyph_cache (state.cache);
352 state.cache = fz_new_glyph_cache ();
353 if (!state.cache) {
354 errx (1, "fz_newglyph_cache failed");
357 if (state.xref) {
358 if (state.xref->store) {
359 pdf_free_store (state.xref->store);
360 state.xref->store = NULL;
362 pdf_free_xref (state.xref);
363 state.xref = NULL;
366 if (state.pagedims) {
367 free (state.pagedims);
368 state.pagedims = NULL;
370 state.pagedimcount = 0;
372 error = pdf_open_xref (&state.xref, filename, password);
373 if (error) {
374 die (error);
377 error = pdf_load_page_tree (state.xref);
378 if (error) {
379 die (error);
382 state.pagecount = pdf_count_pages (state.xref);
385 static void pdfinfo (void)
387 printd (state.sock, "i PDF version %d.%d\n",
388 state.xref->version / 10, state.xref->version % 10);
390 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
392 FILE *f;
393 char *buf;
394 size_t size;
395 fz_obj *obj;
397 f = open_memstream (&buf, &size);
398 if (f) {
399 obj = fz_dict_gets (state.xref->trailer, "Info");
400 fz_fprint_obj (f, fz_resolve_indirect (obj), 0);
401 if (fclose (f)) err (1, "fclose on memstream failed");
402 printd (state.sock, "i %.*s", size, buf);
403 free (buf);
405 else {
406 printd (state.sock, "i error opening memstream: %s\n",
407 strerror (errno));
410 #endif
413 static int readlen (int fd)
415 ssize_t n;
416 unsigned char p[4];
418 n = recv (fd, p, 4, 0);
419 if (n != 4) {
420 if (!n) errx (1, "EOF while reading length");
421 sockerr (1, "recv " FMT_ss, n);
424 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
427 static void unlinkpage (struct page *page)
429 int i;
431 for (i = 0; i < page->slicecount; ++i) {
432 struct slice *s = &page->slices[i];
433 if (s->texindex != -1) {
434 if (state.texowners[s->texindex].slice == s) {
435 state.texowners[s->texindex].slice = NULL;
436 ARSERT (state.texowners[s->texindex].w == s->w);
437 ARSERT (state.texowners[s->texindex].h >= s->h);
443 static void freepage (struct page *page)
445 fz_drop_pixmap (page->pixmap);
447 unlinkpage (page);
449 if (page->text) {
450 fz_free_text_span (page->text);
452 if (page->drawpage) {
453 pdf_free_page (page->drawpage);
456 free (page);
459 static void subdivide (struct page *p)
461 int i;
462 int h = p->pixmap->h;
463 int th = MIN (h, state.sliceheight);
465 for (i = 0; i < p->slicecount; ++i) {
466 struct slice *s = &p->slices[i];
467 s->texindex = -1;
468 s->h = MIN (th, h);
469 s->w = p->pixmap->w;
470 h -= th;
474 static int compatpdims (struct pagedim *p1, struct pagedim *p2)
476 return p1->rotate == p2->rotate
477 && !memcmp (&p1->bbox, &p2->bbox, sizeof (p1->bbox))
478 && !memcmp (&p1->ctm, &p2->ctm, sizeof (p1->ctm));
481 #ifdef __ALTIVEC__
482 #include <altivec.h>
484 static int cacheline32bytes;
486 static void __attribute__ ((constructor)) clcheck (void)
488 char **envp = environ;
489 unsigned long *auxv;
491 while (*envp++);
493 for (auxv = (unsigned long *) envp; *auxv != 0; auxv += 2) {
494 if (*auxv == 19) {
495 cacheline32bytes = auxv[1] == 32;
496 return;
501 static void __attribute__ ((optimize ("O"))) clearpixmap (fz_pixmap *pixmap)
503 if (cacheline32bytes) {
504 intptr_t a1, a2, diff;
505 size_t sizea, i, size = pixmap->w * pixmap->h * pixmap->n;
506 vector unsigned char v = vec_splat_u8 (-1);
507 vector unsigned char *p;
509 a1 = a2 = (intptr_t) pixmap->samples;
510 a2 = (a1 + 31) & ~31;
511 diff = a2 - a1;
512 sizea = size - diff;
513 p = (void *) a2;
515 while (a1 != a2) *(char *) a1++ = 0xff;
516 for (i = 0; i < (sizea & ~31); i += 32) {
517 __asm volatile ("dcbz %0, %1"::"b"(a2),"r"(i));
518 vec_st (v, i, p);
519 vec_st (v, i + 16, p);
521 while (i < sizea) *((char *) a1 + i++) = 0xff;
523 else fz_clear_pixmap_with_color (pixmap, 0xff);
525 #else
526 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
527 #endif
529 static void *render (int pageno, int pindex)
531 fz_error error;
532 int slicecount;
533 struct page *page = NULL;
534 double start, end;
535 pdf_page *drawpage;
536 fz_device *idev;
537 struct pagedim *pagedim;
539 start = now ();
540 printd (state.sock, "V rendering %d", pageno);
542 pagedim = &state.pagedims[pindex];
543 slicecount = (pagedim->bbox.y1 - pagedim->bbox.y0
544 + state.sliceheight - 1) / state.sliceheight;
545 slicecount += slicecount == 0;
547 if (state.pig) {
548 if (compatpdims (&state.pig->pagedim, pagedim)) {
549 page = state.pig;
550 if (page->text) {
551 fz_free_text_span (page->text);
552 page->text = NULL;
554 if (page->drawpage) {
555 pdf_free_page (page->drawpage);
556 page->drawpage = NULL;
559 else {
560 freepage (state.pig);
563 if (!page) {
564 page = calloc (sizeof (*page)
565 + (slicecount * sizeof (struct slice)), 1);
566 if (!page) {
567 err (1, "calloc page %d\n", pageno);
569 page->pixmap = fz_new_pixmap_with_rect (fz_device_rgb, pagedim->bbox);
572 page->slicecount = slicecount;
574 error = pdf_load_page (&drawpage, state.xref, pageno - 1);
575 if (error)
576 die (error);
578 clearpixmap (page->pixmap);
580 idev = fz_new_draw_device (state.cache, page->pixmap);
581 if (!idev)
582 die (fz_throw ("fz_newdrawdevice failed"));
583 error = pdf_run_page (state.xref, drawpage, idev, pagedim->ctm);
584 if (error)
585 die (fz_rethrow (error, "pdf_runpage failed"));
586 fz_free_device (idev);
588 page->drawpage = drawpage;
589 page->pagedim = *pagedim;
590 page->pageno = pageno;
591 subdivide (page);
592 end = now ();
594 pdf_age_store (state.xref->store, 3);
596 printd (state.sock, "V rendering %d took %f sec", pageno, end - start);
597 state.pig = NULL;
598 return page;
601 static void initpdims (void)
603 int pageno;
604 double start, end;
606 start = now ();
607 for (pageno = 0; pageno < state.pagecount; ++pageno) {
608 int rotate;
609 fz_rect box;
610 struct pagedim *p;
611 fz_obj *obj, *pageobj;
613 pageobj = state.xref->page_objs[pageno];
615 obj = fz_dict_gets (pageobj, "CropBox");
616 if (!fz_is_array (obj)) {
617 obj = fz_dict_gets (pageobj, "MediaBox");
618 if (!fz_is_array (obj)) {
619 die (fz_throw ("cannot find page bounds %d (%d Rd)",
620 fz_to_num (pageobj), fz_to_gen (pageobj)));
623 box = pdf_to_rect (obj);
625 obj = fz_dict_gets (pageobj, "Rotate");
626 if (fz_is_int (obj)) {
627 rotate = fz_to_int (obj);
629 else {
630 rotate = 0;
632 rotate += state.rotate;
634 p = &state.pagedims[state.pagedimcount - 1];
635 if ((state.pagedimcount == 0)
636 || (p->rotate != rotate || memcmp (&p->box, &box, sizeof (box)))) {
637 size_t size;
639 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
640 state.pagedims = realloc (state.pagedims, size);
641 if (!state.pagedims) {
642 err (1, "realloc pagedims to " FMT_s " (%d elems)",
643 size, state.pagedimcount + 1);
645 p = &state.pagedims[state.pagedimcount++];
646 p->rotate = rotate;
647 p->box = box;
648 p->pageno = pageno;
651 end = now ();
652 printd (state.sock, "T Processed %d pages in %f seconds",
653 state.pagecount, end - start);
656 static void layout (void)
658 int pindex;
659 fz_matrix ctm;
660 fz_rect box, box2;
661 double zoom, w, maxw = 0;
662 struct pagedim *p = state.pagedims;
664 if (state.proportional) {
665 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
666 box.x0 = MIN (p->box.x0, p->box.x1);
667 box.y0 = MIN (p->box.y0, p->box.y1);
668 box.x1 = MAX (p->box.x0, p->box.x1);
669 box.y1 = MAX (p->box.y0, p->box.y1);
671 ctm = fz_identity;
672 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
673 ctm = fz_concat (ctm, fz_rotate (p->rotate));
674 box2 = fz_transform_rect (ctm, box);
675 w = box2.x1 - box2.x0;
676 maxw = MAX (w, maxw);
680 p = state.pagedims;
681 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
682 box.x0 = MIN (p->box.x0, p->box.x1);
683 box.y0 = MIN (p->box.y0, p->box.y1);
684 box.x1 = MAX (p->box.x0, p->box.x1);
685 box.y1 = MAX (p->box.y0, p->box.y1);
687 ctm = fz_identity;
688 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
689 ctm = fz_concat (ctm, fz_rotate (p->rotate));
690 box2 = fz_transform_rect (ctm, box);
691 w = box2.x1 - box2.x0;
693 if (state.proportional) {
694 double scale = w / maxw;
695 zoom = (state.w / w) * scale;
697 else {
698 zoom = state.w / w;
700 ctm = fz_identity;
701 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
702 ctm = fz_concat (ctm, fz_scale (zoom, -zoom));
703 memcpy (&p->ctm1, &ctm, sizeof (ctm));
704 ctm = fz_concat (ctm, fz_rotate (p->rotate));
705 p->bbox = fz_round_rect (fz_transform_rect (ctm, box));
706 p->left = state.proportional ? ((maxw - w) * zoom) / 2.0 : 0;
707 memcpy (&p->ctm, &ctm, sizeof (ctm));
710 while (p-- != state.pagedims) {
711 printd (state.sock, "l %d %d %d %d",
712 p->pageno, p->bbox.x1 - p->bbox.x0, p->bbox.y1 - p->bbox.y0,
713 p->left);
717 static void recurse_outline (pdf_outline *outline, int level)
719 while (outline) {
720 int i;
721 fz_obj *obj;
722 int pageno = -1;
723 int top = 0, h = 0;
725 if (!outline->link) goto next;
727 obj = outline->link->dest;
728 if (fz_is_indirect (obj)) {
729 obj = fz_resolve_indirect (obj);
731 if (fz_is_array (obj)) {
732 fz_obj *obj2;
734 obj2 = fz_array_get (obj, 0);
735 if (fz_is_int (obj2)) {
736 pageno = fz_to_int (obj2);
738 else {
739 pageno = pdf_find_page_number (state.xref, obj2);
742 if (fz_array_len (obj) > 3) {
743 fz_point p;
744 fz_obj *xo, *yo;
746 xo = fz_array_get (obj, 2);
747 yo = fz_array_get (obj, 3);
748 if (!fz_is_null (xo) && !fz_is_null (yo)) {
749 struct pagedim *pagedim = state.pagedims;
751 for (i = 0; i < state.pagedimcount; ++i) {
752 if (state.pagedims[i].pageno > pageno)
753 break;
754 pagedim = &state.pagedims[i];
756 p.x = fz_to_int (xo);
757 p.y = fz_to_int (yo);
758 p = fz_transform_point (pagedim->ctm, p);
759 h = pagedim->bbox.y1 - pagedim->bbox.y0;
760 top = p.y;
764 else {
765 pageno = pdf_find_page_number (state.xref, outline->link->dest);
768 lprintf ("%*c%s %d\n", level, ' ', outline->title, pageno);
769 if (pageno > 0) {
770 printd (state.sock, "o %d %d %d %d %s",
771 level, pageno, top, h, outline->title);
773 next:
774 if (outline->child) {
775 recurse_outline (outline->child, level + 1);
777 outline = outline->next;
781 static void process_outline (void)
783 pdf_outline *outline;
785 if (!state.needoutline) return;
787 state.needoutline = 0;
788 outline = pdf_load_outline (state.xref);
789 if (outline) {
790 recurse_outline (outline, 0);
791 pdf_free_outline (outline);
795 static int comparespans (const void *l, const void *r)
797 fz_text_span const *const*ls = l;
798 fz_text_span const *const*rs = r;
799 return (*ls)->text->bbox.y0 - (*rs)->text->bbox.y0;
802 /* wishful thinking function */
803 static void search (regex_t *re, int pageno, int y, int forward)
805 int i, j;
806 int ret;
807 char *p;
808 char buf[256];
809 fz_matrix ctm;
810 fz_error error;
811 fz_device *tdev;
812 pdf_page *drawpage;
813 fz_text_span *text, *span, **pspan;
814 struct pagedim *pdim, *pdimprev;
815 int stop = 0;
816 int niters = 0;
817 int nspans;
818 double start, end;
820 start = now ();
821 while (pageno >= 0 && pageno < state.pagecount && !stop) {
822 if (niters++ == 5) {
823 pdf_age_store (state.xref->store, 3);
824 niters = 0;
825 if (hasdata (state.sock)) {
826 printd (state.sock, "T attention requested aborting search at %d",
827 pageno);
828 stop = 1;
830 else {
831 printd (state.sock, "T searching in page %d", pageno);
834 pdimprev = NULL;
835 for (i = 0; i < state.pagedimcount; ++i) {
836 pdim = &state.pagedims[i];
837 if (pdim->pageno == pageno) {
838 goto found;
840 if (pdim->pageno > pageno) {
841 pdim = pdimprev;
842 goto found;
844 pdimprev = pdim;
846 pdim = pdimprev;
847 found:
849 error = pdf_load_page (&drawpage, state.xref, pageno);
850 if (error)
851 die (error);
853 ctm = fz_rotate (pdim->rotate);
855 text = fz_new_text_span ();
856 tdev = fz_new_text_device (text);
857 error = pdf_run_page (state.xref, drawpage, tdev, pdim->ctm1);
858 if (error) die (error);
859 fz_free_device (tdev);
861 nspans = 0;
862 for (span = text; span; span = span->next) {
863 nspans++;
865 pspan = malloc (sizeof (void *) * nspans);
866 if (!pspan) {
867 err (1, "malloc span pointers " FMT_s, sizeof (void *) * nspans);
869 for (i = 0, span = text; span; span = span->next, ++i) {
870 pspan[i] = span;
872 qsort (pspan, nspans, sizeof (fz_text_span *), comparespans);
874 j = forward ? 0 : nspans - 1;
875 while (nspans--) {
876 regmatch_t rm;
878 span = pspan[j];
879 j += forward ? 1 : -1;
880 p = buf;
881 for (i = 0; i < MIN (span->len, (int) sizeof (buf) - 1); ++i) {
882 if (forward) {
883 if (span->text[i].bbox.y0 < y + 1) {
884 continue;
887 else {
888 if (span->text[i].bbox.y0 > y - 1) {
889 continue;
892 if (span->text[i].c < 256) {
893 *p++ = span->text[i].c;
895 else {
896 *p++ = '?';
899 if (p == buf) {
900 continue;
902 *p++ = 0;
904 ret = regexec (re, buf, 1, &rm, 0);
905 if (ret) {
906 if (ret != REG_NOMATCH) {
907 size_t size;
908 char errbuf[80];
909 size = regerror (ret, re, errbuf, sizeof (errbuf));
910 printd (state.sock,
911 "T regexec error `%.*s'",
912 (int) size, errbuf);
913 fz_free_text_span (text);
914 pdf_free_page (drawpage);
915 free (pspan);
916 return;
919 else {
920 int xoff, yoff;
921 fz_bbox *sb, *eb;
922 fz_point p1, p2, p3, p4;
924 xoff = pdim->left - pdim->bbox.x0;
925 yoff = -pdim->bbox.y0;
927 sb = &span->text[rm.rm_so].bbox;
928 eb = &span->text[rm.rm_eo - 1].bbox;
930 p1.x = sb->x0;
931 p1.y = sb->y0;
932 p2.x = eb->x1;
933 p2.y = sb->y0;
934 p3.x = eb->x1;
935 p3.y = eb->y1;
936 p4.x = sb->x0;
937 p4.y = eb->y1;
939 p1 = fz_transform_point (ctm, p1);
940 p2 = fz_transform_point (ctm, p2);
941 p3 = fz_transform_point (ctm, p3);
942 p4 = fz_transform_point (ctm, p4);
944 if (!stop) {
945 printd (state.sock, "F %d %d %f %f %f %f %f %f %f %f",
946 pageno, 1,
947 p1.x + xoff, p1.y + yoff,
948 p2.x + xoff, p2.y + yoff,
949 p3.x + xoff, p3.y + yoff,
950 p4.x + xoff, p4.y + yoff);
952 printd (state.sock, "T found at %d `%.*s' in %f sec",
953 pageno, rm.rm_eo - rm.rm_so, &buf[rm.rm_so],
954 now () - start);
956 else {
957 printd (state.sock, "R %d %d %f %f %f %f %f %f %f %f",
958 pageno, 2,
959 p1.x + xoff, p1.y + yoff,
960 p2.x + xoff, p2.y + yoff,
961 p3.x + xoff, p3.y + yoff,
962 p4.x + xoff, p4.y + yoff);
964 stop = 1;
967 if (forward) {
968 pageno += 1;
969 y = 0;
971 else {
972 pageno -= 1;
973 y = INT_MAX;
975 fz_free_text_span (text);
976 pdf_free_page (drawpage);
977 free (pspan);
979 end = now ();
980 if (!stop) {
981 printd (state.sock, "T no matches %f sec", end - start);
983 printd (state.sock, "D");
986 static
987 #ifdef _WIN32
988 DWORD _stdcall
989 #else
990 void *
991 #endif
992 mainloop (void *unused)
994 char *p = NULL;
995 int len, ret, oldlen = 0;
997 for (;;) {
998 len = readlen (state.sock);
999 if (len == 0) {
1000 errx (1, "readlen returned 0");
1003 if (oldlen < len + 1) {
1004 p = realloc (p, len + 1);
1005 if (!p) {
1006 err (1, "realloc %d failed", len + 1);
1008 oldlen = len + 1;
1010 readdata (state.sock, p, len);
1011 p[len] = 0;
1013 if (!strncmp ("open", p, 4)) {
1014 fz_obj *obj;
1015 size_t filenamelen;
1016 char *password;
1017 char *filename = p + 5;
1019 filenamelen = strlen (filename);
1020 password = filename + filenamelen + 1;
1022 openxref (filename, password);
1023 initpdims ();
1025 obj = fz_dict_gets (state.xref->trailer, "Info");
1026 if (obj) {
1027 char *s;
1029 obj = fz_dict_gets (obj, "Title");
1030 s = pdf_to_utf8 (obj);
1031 if (*s) {
1032 printd (state.sock, "t %s", s);
1034 fz_free (s);
1037 state.needoutline = 1;
1039 else if (!strncmp ("free", p, 4)) {
1040 void *ptr;
1042 ret = sscanf (p + 4, " %p", &ptr);
1043 if (ret != 1) {
1044 errx (1, "malformed free `%.*s' ret=%d", len, p, ret);
1046 unlinkpage (ptr);
1047 state.pig = ptr;
1049 else if (!strncmp ("search", p, 6)) {
1050 int icase, pageno, y, ret, len2, forward;
1051 char *pattern;
1052 regex_t re;
1054 ret = sscanf (p + 6, " %d %d %d %d,%n",
1055 &icase, &pageno, &y, &forward, &len2);
1056 if (ret != 4) {
1057 errx (1, "malformed search `%s' ret=%d", p, ret);
1060 pattern = p + 6 + len2;
1061 ret = regcomp (&re, pattern,
1062 REG_EXTENDED | (icase ? REG_ICASE : 0));
1063 if (ret) {
1064 char errbuf[80];
1065 size_t size;
1067 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1068 printd (state.sock, "T regcomp failed `%.*s'",
1069 (int) size, errbuf);
1071 else {
1072 search (&re, pageno, y, forward);
1073 regfree (&re);
1076 else if (!strncmp ("geometry", p, 8)) {
1077 int w, h;
1079 printd (state.sock, "c");
1080 ret = sscanf (p + 8, " %d %d", &w, &h);
1081 if (ret != 2) {
1082 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1085 lock ("geometry");
1086 state.h = h;
1087 if (w != state.w) {
1088 int i;
1089 state.w = w;
1090 for (i = 0; i < state.texcount; ++i) {
1091 state.texowners[i].slice = NULL;
1094 layout ();
1095 process_outline ();
1096 unlock ("geometry");
1097 printd (state.sock, "C %d", state.pagecount);
1099 else if (!strncmp ("reinit", p, 6)) {
1100 float rotate;
1101 int proportional;
1103 printd (state.sock, "c");
1104 ret = sscanf (p + 6, " %f %d", &rotate, &proportional);
1105 if (ret != 2) {
1106 errx (1, "bad rotate line `%.*s' ret=%d", len, p, ret);
1108 lock ("reinit");
1109 state.rotate = rotate;
1110 state.proportional = proportional;
1111 state.pagedimcount = 0;
1112 free (state.pagedims);
1113 state.pagedims = NULL;
1114 initpdims ();
1115 layout ();
1116 process_outline ();
1117 if (state.pig) {
1118 freepage (state.pig);
1119 state.pig = NULL;
1121 unlock ("reinit");
1122 printd (state.sock, "C %d", state.pagecount);
1124 else if (!strncmp ("render", p, 6)) {
1125 int pageno, pindex, w, h, ret;
1126 struct page *page;
1128 ret = sscanf (p + 6, " %d %d %d %d", &pageno, &pindex, &w, &h);
1129 if (ret != 4) {
1130 errx (1, "bad render line `%.*s' ret=%d", len, p, ret);
1133 lock ("render");
1134 page = render (pageno, pindex);
1135 unlock ("render");
1137 printd (state.sock, "r %d %d %d %d %d %d %p",
1138 pageno,
1139 state.w,
1140 state.h,
1141 state.rotate,
1142 state.proportional,
1143 w * h * 4,
1144 page);
1146 else if (!strncmp ("info", p, 4)) {
1147 pdfinfo ();
1149 else if (!strncmp ("interrupt", p, 9)) {
1150 printd (state.sock, "V interrupted");
1152 else {
1153 errx (1, "unknown command %.*s", len, p);
1156 return 0;
1159 static void showsel (struct page *page, int oy)
1161 int ox;
1162 fz_bbox bbox;
1163 fz_text_span *span;
1164 struct mark first, last;
1166 first = page->fmark;
1167 last = page->lmark;
1169 if (!first.span || !last.span) return;
1171 glEnable (GL_BLEND);
1172 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1173 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1175 ox = -page->pixmap->x + page->pagedim.left;
1176 oy = -page->pixmap->y + oy;
1177 for (span = first.span; span; span = span->next) {
1178 int i, j, k;
1180 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0;
1182 j = 0;
1183 k = span->len - 1;
1185 if (span == page->fmark.span && span == page->lmark.span) {
1186 j = MIN (first.i, last.i);
1187 k = MAX (first.i, last.i);
1189 else if (span == first.span) {
1190 j = first.i;
1192 else if (span == last.span) {
1193 k = last.i;
1196 for (i = j; i <= k; ++i) {
1197 bbox = fz_union_bbox (bbox, span->text[i].bbox);
1199 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1200 bbox.x0,
1201 bbox.y0,
1202 bbox.x1,
1203 bbox.y1,
1204 oy, ox);
1206 glRecti (bbox.x0 + ox, bbox.y0 + oy, bbox.x1 + ox, bbox.y1 + oy);
1208 if (span == last.span) break;
1210 glDisable (GL_BLEND);
1213 static void highlightlinks (struct page *page, int yoff)
1215 pdf_link *link;
1216 int xoff;
1218 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1219 glEnable (GL_LINE_STIPPLE);
1220 glLineStipple (0.5, 0xcccc);
1222 xoff = page->pagedim.left - page->pixmap->x;
1223 yoff -= page->pixmap->y;
1225 glBegin (GL_QUADS);
1226 for (link = page->drawpage->links; link; link = link->next) {
1227 fz_point p1, p2, p3, p4;
1228 fz_matrix ctm = page->pagedim.ctm;
1230 p1.x = link->rect.x0;
1231 p1.y = link->rect.y0;
1233 p2.x = link->rect.x1;
1234 p2.y = link->rect.y0;
1236 p3.x = link->rect.x1;
1237 p3.y = link->rect.y1;
1239 p4.x = link->rect.x0;
1240 p4.y = link->rect.y1;
1242 p1 = fz_transform_point (ctm, p1);
1243 p2 = fz_transform_point (ctm, p2);
1244 p3 = fz_transform_point (ctm, p3);
1245 p4 = fz_transform_point (ctm, p4);
1247 switch (link->kind) {
1248 case PDF_LINK_GOTO: glColor3ub (255, 0, 0); break;
1249 case PDF_LINK_URI: glColor3ub (0, 0, 255); break;
1250 default: glColor3ub (0, 0, 0); break;
1253 glVertex2f (p1.x + xoff, p1.y + yoff);
1254 glVertex2f (p2.x + xoff, p2.y + yoff);
1255 glVertex2f (p3.x + xoff, p3.y + yoff);
1256 glVertex2f (p4.x + xoff, p4.y + yoff);
1258 glEnd ();
1260 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1261 glDisable (GL_LINE_STIPPLE);
1264 static void upload2 (struct page *page, int slicenum, const char *cap)
1266 int i;
1267 int w;
1268 double start, end;
1269 struct slice *slice = &page->slices[slicenum];
1271 w = page->pixmap->w;
1273 ARSERT (w == slice->w);
1274 if (slice->texindex != -1
1275 && state.texowners[slice->texindex].slice == slice) {
1276 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
1278 else {
1279 int subimage = 0;
1280 int index = (state.texindex++ % state.texcount);
1281 size_t offset = 0;
1283 for (i = 0; i < slicenum; ++i) {
1284 offset += w * page->slices[i].h * 4;
1287 if (state.texowners[index].w == slice->w) {
1288 if (state.texowners[index].h >= slice->h ) {
1289 subimage = 1;
1291 else {
1292 state.texowners[index].h = slice->h;
1295 else {
1296 state.texowners[index].h = slice->h;
1299 state.texowners[index].slice = slice;
1300 state.texowners[index].w = slice->w;
1301 slice->texindex = index;
1303 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
1304 start = now ();
1305 if (subimage) {
1307 GLenum err = glGetError ();
1308 if (err != GL_NO_ERROR) {
1309 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w, slice->h, err);
1310 abort ();
1313 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
1318 slice->h,
1319 state.texform,
1320 state.texty,
1321 page->pixmap->samples + offset
1324 GLenum err = glGetError ();
1325 if (err != GL_NO_ERROR) {
1326 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w, slice->h, err);
1327 abort ();
1331 else {
1332 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
1334 GL_RGBA8,
1336 slice->h,
1338 state.texform,
1339 state.texty,
1340 page->pixmap->samples + offset
1344 end = now ();
1345 (void) start;
1346 (void) end;
1347 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1348 subimage ? "sub" : "img",
1349 page->pageno, slicenum,
1350 slice->w, slice->h,
1351 state.texids[slice->texindex],
1352 end - start);
1356 CAMLprim value ml_draw (value args_v, value ptr_v)
1358 CAMLparam2 (args_v, ptr_v);
1359 int dispy = Int_val (Field (args_v, 0));
1360 int w = Int_val (Field (args_v, 1));
1361 int h = Int_val (Field (args_v, 2));
1362 int py = Int_val (Field (args_v, 3));
1363 int hlinks = Bool_val (Field (args_v, 4));
1364 char *s = String_val (ptr_v);
1365 int ret;
1366 void *ptr;
1367 struct page *page;
1368 int slicenum = 0;
1369 int yoff = dispy - py;
1371 ret = sscanf (s, "%p", &ptr);
1372 if (ret != 1) {
1373 errx (1, "cannot parse pointer `%s'", s);
1375 page = ptr;
1377 w = page->pixmap->w;
1379 ARSERT (h >= 0 && "ml_draw wrong h");
1380 ARSERT (py <= page->pixmap->h && "ml_draw wrong py");
1382 glEnable (GL_TEXTURE_RECTANGLE_ARB);
1384 for (slicenum = 0; slicenum < page->slicecount; ++slicenum) {
1385 struct slice *slice = &page->slices[slicenum];
1386 if (slice->h > py) {
1387 break;
1389 py -= slice->h;
1392 h = MIN (state.h, h);
1393 while (h) {
1394 int th, left;
1395 struct slice *slice = &page->slices[slicenum];
1397 ARSERT (slicenum < page->slicecount && "ml_draw wrong slicenum");
1399 th = MIN (h, slice->h - py);
1400 upload2 (page, slicenum, "upload");
1402 left = page->pagedim.left;
1403 glBegin (GL_QUADS);
1405 glTexCoord2i (0, py);
1406 glVertex2i (left, dispy);
1408 glTexCoord2i (w, py);
1409 glVertex2i (left+w, dispy);
1411 glTexCoord2i (w, py+th);
1412 glVertex2i (left+w, dispy + th);
1414 glTexCoord2i (0, py+th);
1415 glVertex2i (left, dispy + th);
1417 glEnd ();
1419 h -= th;
1420 py = 0;
1421 dispy += th;
1422 slicenum += 1;
1425 showsel (page, yoff);
1426 if (hlinks) highlightlinks (page, yoff);
1427 glDisable (GL_TEXTURE_RECTANGLE_ARB);
1429 CAMLreturn (Val_unit);
1432 static pdf_link *getlink (struct page *page, int x, int y)
1434 fz_point p;
1435 fz_matrix ctm;
1436 pdf_link *link;
1438 p.x = x + page->pixmap->x;
1439 p.y = y + page->pixmap->y;
1441 ctm = fz_invert_matrix (page->pagedim.ctm);
1442 p = fz_transform_point (ctm, p);
1444 for (link = page->drawpage->links; link; link = link->next) {
1445 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
1446 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
1447 return link;
1451 return NULL;
1454 static void ensuretext (struct page *page)
1456 if (!page->text) {
1457 fz_error error;
1458 fz_device *tdev;
1460 page->text = fz_new_text_span ();
1461 tdev = fz_new_text_device (page->text);
1462 error = pdf_run_page (state.xref, page->drawpage, tdev,
1463 page->pagedim.ctm);
1464 if (error) die (error);
1465 fz_free_device (tdev);
1469 CAMLprim value ml_whatsunder (value ptr_v, value x_v, value y_v)
1471 CAMLparam3 (ptr_v, x_v, y_v);
1472 CAMLlocal3 (ret_v, tup_v, str_v);
1473 pdf_link *link;
1474 struct page *page;
1475 char *s = String_val (ptr_v);
1477 ret_v = Val_int (0);
1478 if (trylock ("ml_whatsunder")) {
1479 goto done;
1482 page = parse_pointer ("ml_whatsunder", s);
1483 link = getlink (page, Int_val (x_v), Int_val (y_v));
1484 if (link) {
1485 switch (link->kind) {
1486 case PDF_LINK_GOTO:
1488 int pageno;
1489 fz_point p;
1490 fz_obj *obj;
1492 pageno = -1;
1493 p.x = 0;
1494 p.y = 0;
1496 if (fz_is_array (link->dest)) {
1497 obj = fz_array_get (link->dest, 0);
1498 if (fz_is_indirect (obj)) {
1499 pageno = pdf_find_page_number (state.xref, obj);
1501 else if (fz_is_int (obj)) {
1502 pageno = fz_to_int (obj);
1505 if (fz_array_len (link->dest) > 3) {
1506 fz_obj *xo, *yo;
1508 xo = fz_array_get (link->dest, 2);
1509 yo = fz_array_get (link->dest, 3);
1510 if (!fz_is_null (xo) && !fz_is_null (yo)) {
1511 p.x = fz_to_int (xo);
1512 p.y = fz_to_int (yo);
1513 p = fz_transform_point (page->pagedim.ctm, p);
1517 else {
1518 pageno = pdf_find_page_number (state.xref, link->dest);
1520 tup_v = caml_alloc_tuple (2);
1521 ret_v = caml_alloc_small (1, 1);
1522 Field (tup_v, 0) = Val_int (pageno);
1523 Field (tup_v, 1) = Val_int (p.y);
1524 Field (ret_v, 0) = tup_v;
1526 break;
1528 case PDF_LINK_URI:
1529 str_v = caml_copy_string (fz_to_str_buf (link->dest));
1530 ret_v = caml_alloc_small (1, 0);
1531 Field (ret_v, 0) = str_v;
1532 break;
1534 default:
1535 printd (state.sock, "T unhandled link kind %d", link->kind);
1536 break;
1539 else {
1540 int i, x, y;
1541 fz_text_span *span;
1543 ensuretext (page);
1544 x = Int_val (x_v) + page->pixmap->x;
1545 y = Int_val (y_v) + page->pixmap->y;
1547 for (span = page->text; span; span = span->next) {
1548 for (i = 0; i < span->len; ++i) {
1549 fz_bbox *b;
1550 b = &span->text[i].bbox;
1551 if ((x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1)) {
1552 const char *n2 =
1553 span->font && span->font->name
1554 ? span->font->name
1555 : "Span has no font name"
1557 #ifdef FT_FREETYPE_H
1558 FT_FaceRec *face = span->font->ft_face;
1559 if (face && face->family_name) {
1560 char *s;
1561 char *n1 = face->family_name;
1562 size_t l1 = strlen (n1);
1563 size_t l2 = strlen (n2);
1565 if (l1 != l2 || memcmp (n1, n2, l1)) {
1566 s = malloc (l1 + l2 + 2);
1567 if (s) {
1568 memcpy (s, n2, l2);
1569 s[l2] = '=';
1570 memcpy (s + l2 + 1, n1, l1 + 1);
1571 str_v = caml_copy_string (s);
1572 free (s);
1576 if (str_v == 0) {
1577 str_v = caml_copy_string (n2);
1579 #else
1580 str_v = caml_copy_string (n2);
1581 #endif
1582 ret_v = caml_alloc_small (1, 2);
1583 Field (ret_v, 0) = str_v;
1584 goto unlock;
1589 unlock:
1590 unlock ("ml_whatsunder");
1592 done:
1593 CAMLreturn (ret_v);
1596 CAMLprim value ml_seltext (value ptr_v, value rect_v, value oy_v)
1598 CAMLparam4 (ptr_v, rect_v, oy_v, rect_v);
1599 fz_bbox *b;
1600 struct page *page;
1601 fz_text_span *span;
1602 struct mark first, last;
1603 int i, x0, x1, y0, y1, oy, left;
1604 char *s = String_val (ptr_v);
1606 if (trylock ("ml_seltext")) {
1607 goto done;
1610 page = parse_pointer ("ml_seltext", s);
1611 ensuretext (page);
1613 oy = Int_val (oy_v);
1614 x0 = Int_val (Field (rect_v, 0));
1615 y0 = Int_val (Field (rect_v, 1));
1616 x1 = Int_val (Field (rect_v, 2));
1617 y1 = Int_val (Field (rect_v, 3));
1619 left = page->pagedim.left;
1620 if (0) {
1621 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1622 glColor3ub (128, 128, 128);
1623 glRecti (x0, y0, x1, y1);
1624 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1627 x0 += page->pixmap->x - left;
1628 y0 += page->pixmap->y - oy;
1629 x1 += page->pixmap->x - left;
1630 y1 += page->pixmap->y - oy;
1632 first.span = NULL;
1633 last.span = NULL;
1635 last.i = first.i = 0;
1636 first.span = page->text;
1637 for (span = page->text; span; span = span->next) {
1638 for (i = 0; i < span->len; ++i) {
1639 b = &span->text[i].bbox;
1640 int selected = 0;
1642 if (x0 >= b->x0 && x0 <= b->x1 && y0 >= b->y0 && y0 <= b->y1) {
1643 first.i = i;
1644 first.span = span;
1645 selected = 1;
1647 if (x1 >= b->x0 && x1 <= b->x1 && y1 >= b->y0 && y1 <= b->y1) {
1648 last.i = i;
1649 last.span = span;
1650 selected = 1;
1652 if (0 && selected) {
1653 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1654 glColor3ub (128, 128, 128);
1655 glRecti (b->x0+left, b->y0, b->x1+left, b->y1);
1656 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1661 if (y1 < y0 || x1 < x0) {
1662 int swap = 0;
1664 if (first.span == last.span) {
1665 swap = 1;
1667 else {
1668 if (y1 < y0) {
1669 for (span = first.span; span && span != last.span;
1670 span = span->next) {
1671 if (span->eol) {
1672 swap = 1;
1673 break;
1679 if (swap) {
1680 i = first.i;
1681 span = first.span;
1682 first.i = last.i;
1683 first.span = last.span;
1684 last.i = i;
1685 last.span = span;
1689 page->fmark = first;
1690 page->lmark = last;
1692 unlock ("ml_seltext");
1694 done:
1695 CAMLreturn (Val_unit);
1698 static int pipespan (FILE *f, fz_text_span *span, int a, int b)
1700 char buf[4];
1701 int i, len, ret;
1703 for (i = a; i <= b; ++i) {
1704 len = runetochar (buf, &span->text[i].c);
1705 ret = fwrite (buf, len, 1, f);
1707 if (ret != 1) {
1708 printd (state.sock, "T failed to write %d bytes ret=%d: %s",
1709 len, ret, strerror (errno));
1710 return -1;
1713 return 0;
1716 CAMLprim value ml_copysel (value ptr_v)
1718 CAMLparam1 (ptr_v);
1719 FILE *f;
1720 struct page *page;
1721 char *s = String_val (ptr_v);
1723 if (trylock ("ml_copysel")) {
1724 goto done;
1727 if (!*s) {
1728 close:
1729 #ifdef USE_XSEL
1730 if (state.xselpipe) {
1731 int ret = pclose (state.xselpipe);
1732 if (ret) {
1733 printd (state.sock, "T failed to close xsel pipe `%s'",
1734 strerror (errno));
1736 state.xselpipe = NULL;
1738 #else
1739 printf ("========================================\n");
1740 #endif
1742 else {
1743 fz_text_span *span;
1745 page = parse_pointer ("ml_sopysel", s);
1747 if (!page->fmark.span || !page->lmark.span) {
1748 printd (state.sock, "T nothing to copy");
1749 goto unlock;
1752 f = stdout;
1753 #ifdef USE_XSEL
1754 if (!state.xselpipe) {
1755 state.xselpipe = popen ("xsel -i", "w");
1756 if (!state.xselpipe) {
1757 printd (state.sock, "T failed to open xsel pipe `%s'",
1758 strerror (errno));
1760 else {
1761 f = state.xselpipe;
1764 else {
1765 f = state.xselpipe;
1767 #endif
1769 for (span = page->fmark.span;
1770 span && span != page->lmark.span->next;
1771 span = span->next) {
1772 int a = span == page->fmark.span ? page->fmark.i : 0;
1773 int b = span == page->lmark.span ? page->lmark.i : span->len - 1;
1774 if (pipespan (f, span, a, b)) {
1775 goto close;
1777 if (span->eol) {
1778 if (putc ('\n', f) == EOF) {
1779 printd (state.sock, "T failed break line on xsel pipe `%s'",
1780 strerror (errno));
1781 goto close;
1785 page->lmark.span = NULL;
1786 page->fmark.span = NULL;
1789 unlock:
1790 unlock ("ml_copysel");
1792 done:
1793 CAMLreturn (Val_unit);
1796 CAMLprim value ml_getpdimrect (value pagedimno_v)
1798 CAMLparam1 (pagedimno_v);
1799 CAMLlocal1 (ret_v);
1800 int pagedimno = Int_val (pagedimno_v);
1801 fz_rect box;
1803 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
1804 if (trylock ("ml_getpdimrect")) {
1805 box = fz_empty_rect;
1807 else {
1808 box = state.pagedims[pagedimno].box;
1809 unlock ("ml_getpdimrect");
1812 Store_double_field (ret_v, 0, box.x0);
1813 Store_double_field (ret_v, 1, box.x1);
1814 Store_double_field (ret_v, 2, box.y0);
1815 Store_double_field (ret_v, 3, box.y1);
1817 CAMLreturn (ret_v);
1820 CAMLprim value ml_zoom_for_height (value winw_v, value winh_v, value dw_v)
1822 CAMLparam3 (winw_v, winh_v, dw_v);
1823 CAMLlocal1 (ret_v);
1824 int i;
1825 double zoom = 1.0;
1826 double maxw = 0.0, maxh = 0.0;
1827 struct pagedim *p;
1828 double winw = Int_val (winw_v);
1829 double winh = Int_val (winh_v);
1830 double dw = Int_val (dw_v);
1831 double pw = 1.0, ph = 1.0, num, den;
1833 if (trylock ("ml_zoom_for_height")) {
1834 goto done;
1837 if (state.proportional) {
1838 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
1839 double x0, x1, w;
1841 x0 = MIN (p->box.x0, p->box.x1);
1842 x1 = MAX (p->box.x0, p->box.x1);
1844 w = x1 - x0;
1845 maxw = MAX (w, maxw);
1849 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
1850 double x0, x1, y0, y1, w, h, scaledh, scale;
1852 x0 = MIN (p->box.x0, p->box.x1);
1853 y0 = MIN (p->box.y0, p->box.y1);
1854 x1 = MAX (p->box.x0, p->box.x1);
1855 y1 = MAX (p->box.y0, p->box.y1);
1857 w = x1 - x0;
1858 h = y1 - y0;
1860 if (state.proportional) {
1861 scale = w / maxw;
1862 scaledh = h * scale;
1864 else {
1865 scale = 1.0;
1866 scaledh = h;
1869 if (scaledh > maxh) {
1870 maxh = scaledh;
1871 ph = scaledh;
1872 pw = w * scale;
1876 num = (winh * pw) + (ph * dw);
1877 den = ph * winw;
1878 zoom = num / den;
1880 unlock ("ml_zoom_for_height");
1881 done:
1882 ret_v = caml_copy_double (zoom);
1883 CAMLreturn (ret_v);
1886 CAMLprim value ml_init (value sock_v, value params_v)
1888 #ifndef _WIN32
1889 int ret;
1890 #endif
1891 CAMLparam2 (sock_v, params_v);
1893 state.rotate = Int_val (Field (params_v, 0));
1894 state.proportional = Bool_val (Field (params_v, 1));
1895 state.texcount = Int_val (Field (params_v, 2));
1896 state.sliceheight = Int_val (Field (params_v, 3));
1897 state.texform = GL_RGBA;
1898 state.texty = GL_UNSIGNED_BYTE;
1900 fz_accelerate ();
1901 state.texids = calloc (state.texcount * sizeof (*state.texids), 1);
1902 if (!state.texids) {
1903 err (1, "calloc texids " FMT_s,
1904 state.texcount * sizeof (*state.texids));
1907 state.texowners = calloc (state.texcount * sizeof (*state.texowners), 1);
1908 if (!state.texowners) {
1909 err (1, "calloc texowners " FMT_s,
1910 state.texcount * sizeof (*state.texowners));
1913 glGenTextures (state.texcount, state.texids);
1915 #ifdef _WIN32
1916 state.sock = Socket_val (sock_v);
1917 #else
1918 state.sock = Int_val (sock_v);
1919 #endif
1921 #ifdef _WIN32
1922 InitializeCriticalSection (&critsec);
1923 state.thread = CreateThread (NULL, 0, mainloop, NULL, 0, NULL);
1924 if (state.thread == INVALID_HANDLE_VALUE) {
1925 errx (1, "CreateThread failed: %lx", GetLastError ());
1927 #else
1928 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
1929 if (ret) {
1930 errx (1, "pthread_create: %s", strerror (ret));
1932 #endif
1934 CAMLreturn (Val_unit);