fz_fprint_obj does the line termination for us
[llpp/slowscroll.git] / link.c
blob763e77f1d214514871f27f957aa03a1e1d2c99a2
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 fz_obj *obj;
389 printd (state.sock, "i version PDF-%d.%d\n",
390 state.xref->version / 10, state.xref->version % 10);
392 #ifdef _GNU_SOURCE
394 FILE *f;
395 char *buf;
396 size_t size;
398 f = open_memstream (&buf, &size);
399 obj = fz_dict_gets (state.xref->trailer, "Info");
400 fz_fprint_obj (f, fz_resolve_indirect (obj), 0);
401 fclose (f);
402 printd (state.sock, "i %.*s", size, buf);
403 free (buf);
405 #endif
408 static int readlen (int fd)
410 ssize_t n;
411 unsigned char p[4];
413 n = recv (fd, p, 4, 0);
414 if (n != 4) {
415 if (!n) errx (1, "EOF while reading length");
416 sockerr (1, "recv " FMT_ss, n);
419 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
422 static void unlinkpage (struct page *page)
424 int i;
426 for (i = 0; i < page->slicecount; ++i) {
427 struct slice *s = &page->slices[i];
428 if (s->texindex != -1) {
429 if (state.texowners[s->texindex].slice == s) {
430 state.texowners[s->texindex].slice = NULL;
431 ARSERT (state.texowners[s->texindex].w == s->w);
432 ARSERT (state.texowners[s->texindex].h >= s->h);
438 static void freepage (struct page *page)
440 fz_drop_pixmap (page->pixmap);
442 unlinkpage (page);
444 if (page->text) {
445 fz_free_text_span (page->text);
447 if (page->drawpage) {
448 pdf_free_page (page->drawpage);
451 free (page);
454 static void subdivide (struct page *p)
456 int i;
457 int h = p->pixmap->h;
458 int th = MIN (h, state.sliceheight);
460 for (i = 0; i < p->slicecount; ++i) {
461 struct slice *s = &p->slices[i];
462 s->texindex = -1;
463 s->h = MIN (th, h);
464 s->w = p->pixmap->w;
465 h -= th;
469 static int compatpdims (struct pagedim *p1, struct pagedim *p2)
471 return p1->rotate == p2->rotate
472 && !memcmp (&p1->bbox, &p2->bbox, sizeof (p1->bbox))
473 && !memcmp (&p1->ctm, &p2->ctm, sizeof (p1->ctm));
476 #ifdef __ALTIVEC__
477 #include <altivec.h>
479 static int cacheline32bytes;
481 static void __attribute__ ((constructor)) clcheck (void)
483 char **envp = environ;
484 unsigned long *auxv;
486 while (*envp++);
488 for (auxv = (unsigned long *) envp; *auxv != 0; auxv += 2) {
489 if (*auxv == 19) {
490 cacheline32bytes = auxv[1] == 32;
491 return;
496 static void __attribute__ ((optimize ("O"))) clearpixmap (fz_pixmap *pixmap)
498 if (cacheline32bytes) {
499 intptr_t a1, a2, diff;
500 size_t sizea, i, size = pixmap->w * pixmap->h * pixmap->n;
501 vector unsigned char v = vec_splat_u8 (-1);
502 vector unsigned char *p;
504 a1 = a2 = (intptr_t) pixmap->samples;
505 a2 = (a1 + 31) & ~31;
506 diff = a2 - a1;
507 sizea = size - diff;
508 p = (void *) a2;
510 while (a1 != a2) *(char *) a1++ = 0xff;
511 for (i = 0; i < (sizea & ~31); i += 32) {
512 __asm volatile ("dcbz %0, %1"::"b"(a2),"r"(i));
513 vec_st (v, i, p);
514 vec_st (v, i + 16, p);
516 while (i < sizea) *((char *) a1 + i++) = 0xff;
518 else fz_clear_pixmap_with_color (pixmap, 0xff);
520 #else
521 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
522 #endif
524 static void *render (int pageno, int pindex)
526 fz_error error;
527 int slicecount;
528 struct page *page = NULL;
529 double start, end;
530 pdf_page *drawpage;
531 fz_device *idev;
532 struct pagedim *pagedim;
534 start = now ();
535 printd (state.sock, "V rendering %d", pageno);
537 pagedim = &state.pagedims[pindex];
538 slicecount = (pagedim->bbox.y1 - pagedim->bbox.y0
539 + state.sliceheight - 1) / state.sliceheight;
540 slicecount += slicecount == 0;
542 if (state.pig) {
543 if (compatpdims (&state.pig->pagedim, pagedim)) {
544 page = state.pig;
545 if (page->text) {
546 fz_free_text_span (page->text);
547 page->text = NULL;
549 if (page->drawpage) {
550 pdf_free_page (page->drawpage);
551 page->drawpage = NULL;
554 else {
555 freepage (state.pig);
558 if (!page) {
559 page = calloc (sizeof (*page)
560 + (slicecount * sizeof (struct slice)), 1);
561 if (!page) {
562 err (1, "calloc page %d\n", pageno);
564 page->pixmap = fz_new_pixmap_with_rect (fz_device_rgb, pagedim->bbox);
567 page->slicecount = slicecount;
569 error = pdf_load_page (&drawpage, state.xref, pageno - 1);
570 if (error)
571 die (error);
573 clearpixmap (page->pixmap);
575 idev = fz_new_draw_device (state.cache, page->pixmap);
576 if (!idev)
577 die (fz_throw ("fz_newdrawdevice failed"));
578 error = pdf_run_page (state.xref, drawpage, idev, pagedim->ctm);
579 if (error)
580 die (fz_rethrow (error, "pdf_runpage failed"));
581 fz_free_device (idev);
583 page->drawpage = drawpage;
584 page->pagedim = *pagedim;
585 page->pageno = pageno;
586 subdivide (page);
587 end = now ();
589 pdf_age_store (state.xref->store, 3);
591 printd (state.sock, "V rendering %d took %f sec", pageno, end - start);
592 state.pig = NULL;
593 return page;
596 static void initpdims (void)
598 int pageno;
599 double start, end;
601 start = now ();
602 for (pageno = 0; pageno < state.pagecount; ++pageno) {
603 int rotate;
604 fz_rect box;
605 struct pagedim *p;
606 fz_obj *obj, *pageobj;
608 pageobj = state.xref->page_objs[pageno];
610 obj = fz_dict_gets (pageobj, "CropBox");
611 if (!fz_is_array (obj)) {
612 obj = fz_dict_gets (pageobj, "MediaBox");
613 if (!fz_is_array (obj)) {
614 die (fz_throw ("cannot find page bounds %d (%d Rd)",
615 fz_to_num (pageobj), fz_to_gen (pageobj)));
618 box = pdf_to_rect (obj);
620 obj = fz_dict_gets (pageobj, "Rotate");
621 if (fz_is_int (obj)) {
622 rotate = fz_to_int (obj);
624 else {
625 rotate = 0;
627 rotate += state.rotate;
629 p = &state.pagedims[state.pagedimcount - 1];
630 if ((state.pagedimcount == 0)
631 || (p->rotate != rotate || memcmp (&p->box, &box, sizeof (box)))) {
632 size_t size;
634 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
635 state.pagedims = realloc (state.pagedims, size);
636 if (!state.pagedims) {
637 err (1, "realloc pagedims to " FMT_s " (%d elems)",
638 size, state.pagedimcount + 1);
640 p = &state.pagedims[state.pagedimcount++];
641 p->rotate = rotate;
642 p->box = box;
643 p->pageno = pageno;
646 end = now ();
647 printd (state.sock, "T Processed %d pages in %f seconds",
648 state.pagecount, end - start);
651 static void layout (void)
653 int pindex;
654 fz_matrix ctm;
655 fz_rect box, box2;
656 double zoom, w, maxw = 0;
657 struct pagedim *p = state.pagedims;
659 if (state.proportional) {
660 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
661 box.x0 = MIN (p->box.x0, p->box.x1);
662 box.y0 = MIN (p->box.y0, p->box.y1);
663 box.x1 = MAX (p->box.x0, p->box.x1);
664 box.y1 = MAX (p->box.y0, p->box.y1);
666 ctm = fz_identity;
667 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
668 ctm = fz_concat (ctm, fz_rotate (p->rotate));
669 box2 = fz_transform_rect (ctm, box);
670 w = box2.x1 - box2.x0;
671 maxw = MAX (w, maxw);
675 p = state.pagedims;
676 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
677 box.x0 = MIN (p->box.x0, p->box.x1);
678 box.y0 = MIN (p->box.y0, p->box.y1);
679 box.x1 = MAX (p->box.x0, p->box.x1);
680 box.y1 = MAX (p->box.y0, p->box.y1);
682 ctm = fz_identity;
683 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
684 ctm = fz_concat (ctm, fz_rotate (p->rotate));
685 box2 = fz_transform_rect (ctm, box);
686 w = box2.x1 - box2.x0;
688 if (state.proportional) {
689 double scale = w / maxw;
690 zoom = (state.w / w) * scale;
692 else {
693 zoom = state.w / w;
695 ctm = fz_identity;
696 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
697 ctm = fz_concat (ctm, fz_scale (zoom, -zoom));
698 memcpy (&p->ctm1, &ctm, sizeof (ctm));
699 ctm = fz_concat (ctm, fz_rotate (p->rotate));
700 p->bbox = fz_round_rect (fz_transform_rect (ctm, box));
701 p->left = state.proportional ? ((maxw - w) * zoom) / 2.0 : 0;
702 memcpy (&p->ctm, &ctm, sizeof (ctm));
705 while (p-- != state.pagedims) {
706 printd (state.sock, "l %d %d %d %d",
707 p->pageno, p->bbox.x1 - p->bbox.x0, p->bbox.y1 - p->bbox.y0,
708 p->left);
712 static void recurse_outline (pdf_outline *outline, int level)
714 while (outline) {
715 int i;
716 fz_obj *obj;
717 int pageno = -1;
718 int top = 0, h = 0;
720 if (!outline->link) goto next;
722 obj = outline->link->dest;
723 if (fz_is_indirect (obj)) {
724 obj = fz_resolve_indirect (obj);
726 if (fz_is_array (obj)) {
727 fz_obj *obj2;
729 obj2 = fz_array_get (obj, 0);
730 if (fz_is_int (obj2)) {
731 pageno = fz_to_int (obj2);
733 else {
734 pageno = pdf_find_page_number (state.xref, obj2);
737 if (fz_array_len (obj) > 3) {
738 fz_point p;
739 fz_obj *xo, *yo;
741 xo = fz_array_get (obj, 2);
742 yo = fz_array_get (obj, 3);
743 if (!fz_is_null (xo) && !fz_is_null (yo)) {
744 struct pagedim *pagedim = state.pagedims;
746 for (i = 0; i < state.pagedimcount; ++i) {
747 if (state.pagedims[i].pageno > pageno)
748 break;
749 pagedim = &state.pagedims[i];
751 p.x = fz_to_int (xo);
752 p.y = fz_to_int (yo);
753 p = fz_transform_point (pagedim->ctm, p);
754 h = pagedim->bbox.y1 - pagedim->bbox.y0;
755 top = p.y;
759 else {
760 pageno = pdf_find_page_number (state.xref, outline->link->dest);
763 lprintf ("%*c%s %d\n", level, ' ', outline->title, pageno);
764 if (pageno > 0) {
765 printd (state.sock, "o %d %d %d %d %s",
766 level, pageno, top, h, outline->title);
768 next:
769 if (outline->child) {
770 recurse_outline (outline->child, level + 1);
772 outline = outline->next;
776 static void process_outline (void)
778 pdf_outline *outline;
780 if (!state.needoutline) return;
782 state.needoutline = 0;
783 outline = pdf_load_outline (state.xref);
784 if (outline) {
785 recurse_outline (outline, 0);
786 pdf_free_outline (outline);
790 static int comparespans (const void *l, const void *r)
792 fz_text_span const *const*ls = l;
793 fz_text_span const *const*rs = r;
794 return (*ls)->text->bbox.y0 - (*rs)->text->bbox.y0;
797 /* wishful thinking function */
798 static void search (regex_t *re, int pageno, int y, int forward)
800 int i, j;
801 int ret;
802 char *p;
803 char buf[256];
804 fz_matrix ctm;
805 fz_error error;
806 fz_device *tdev;
807 pdf_page *drawpage;
808 fz_text_span *text, *span, **pspan;
809 struct pagedim *pdim, *pdimprev;
810 int stop = 0;
811 int niters = 0;
812 int nspans;
813 double start, end;
815 start = now ();
816 while (pageno >= 0 && pageno < state.pagecount && !stop) {
817 if (niters++ == 5) {
818 pdf_age_store (state.xref->store, 3);
819 niters = 0;
820 if (hasdata (state.sock)) {
821 printd (state.sock, "T attention requested aborting search at %d",
822 pageno);
823 stop = 1;
825 else {
826 printd (state.sock, "T searching in page %d", pageno);
829 pdimprev = NULL;
830 for (i = 0; i < state.pagedimcount; ++i) {
831 pdim = &state.pagedims[i];
832 if (pdim->pageno == pageno) {
833 goto found;
835 if (pdim->pageno > pageno) {
836 pdim = pdimprev;
837 goto found;
839 pdimprev = pdim;
841 pdim = pdimprev;
842 found:
844 error = pdf_load_page (&drawpage, state.xref, pageno);
845 if (error)
846 die (error);
848 ctm = fz_rotate (pdim->rotate);
850 text = fz_new_text_span ();
851 tdev = fz_new_text_device (text);
852 error = pdf_run_page (state.xref, drawpage, tdev, pdim->ctm1);
853 if (error) die (error);
854 fz_free_device (tdev);
856 nspans = 0;
857 for (span = text; span; span = span->next) {
858 nspans++;
860 pspan = malloc (sizeof (void *) * nspans);
861 if (!pspan) {
862 err (1, "malloc span pointers " FMT_s, sizeof (void *) * nspans);
864 for (i = 0, span = text; span; span = span->next, ++i) {
865 pspan[i] = span;
867 qsort (pspan, nspans, sizeof (fz_text_span *), comparespans);
869 j = forward ? 0 : nspans - 1;
870 while (nspans--) {
871 regmatch_t rm;
873 span = pspan[j];
874 j += forward ? 1 : -1;
875 p = buf;
876 for (i = 0; i < MIN (span->len, (int) sizeof (buf) - 1); ++i) {
877 if (forward) {
878 if (span->text[i].bbox.y0 < y + 1) {
879 continue;
882 else {
883 if (span->text[i].bbox.y0 > y - 1) {
884 continue;
887 if (span->text[i].c < 256) {
888 *p++ = span->text[i].c;
890 else {
891 *p++ = '?';
894 if (p == buf) {
895 continue;
897 *p++ = 0;
899 ret = regexec (re, buf, 1, &rm, 0);
900 if (ret) {
901 if (ret != REG_NOMATCH) {
902 size_t size;
903 char errbuf[80];
904 size = regerror (ret, re, errbuf, sizeof (errbuf));
905 printd (state.sock,
906 "T regexec error `%.*s'",
907 (int) size, errbuf);
908 fz_free_text_span (text);
909 pdf_free_page (drawpage);
910 free (pspan);
911 return;
914 else {
915 int xoff, yoff;
916 fz_bbox *sb, *eb;
917 fz_point p1, p2, p3, p4;
919 xoff = pdim->left - pdim->bbox.x0;
920 yoff = -pdim->bbox.y0;
922 sb = &span->text[rm.rm_so].bbox;
923 eb = &span->text[rm.rm_eo - 1].bbox;
925 p1.x = sb->x0;
926 p1.y = sb->y0;
927 p2.x = eb->x1;
928 p2.y = sb->y0;
929 p3.x = eb->x1;
930 p3.y = eb->y1;
931 p4.x = sb->x0;
932 p4.y = eb->y1;
934 p1 = fz_transform_point (ctm, p1);
935 p2 = fz_transform_point (ctm, p2);
936 p3 = fz_transform_point (ctm, p3);
937 p4 = fz_transform_point (ctm, p4);
939 if (!stop) {
940 printd (state.sock, "F %d %d %f %f %f %f %f %f %f %f",
941 pageno, 1,
942 p1.x + xoff, p1.y + yoff,
943 p2.x + xoff, p2.y + yoff,
944 p3.x + xoff, p3.y + yoff,
945 p4.x + xoff, p4.y + yoff);
947 printd (state.sock, "T found at %d `%.*s' in %f sec",
948 pageno, rm.rm_eo - rm.rm_so, &buf[rm.rm_so],
949 now () - start);
951 else {
952 printd (state.sock, "R %d %d %f %f %f %f %f %f %f %f",
953 pageno, 2,
954 p1.x + xoff, p1.y + yoff,
955 p2.x + xoff, p2.y + yoff,
956 p3.x + xoff, p3.y + yoff,
957 p4.x + xoff, p4.y + yoff);
959 stop = 1;
962 if (forward) {
963 pageno += 1;
964 y = 0;
966 else {
967 pageno -= 1;
968 y = INT_MAX;
970 fz_free_text_span (text);
971 pdf_free_page (drawpage);
972 free (pspan);
974 end = now ();
975 if (!stop) {
976 printd (state.sock, "T no matches %f sec", end - start);
978 printd (state.sock, "D");
981 static
982 #ifdef _WIN32
983 DWORD _stdcall
984 #else
985 void *
986 #endif
987 mainloop (void *unused)
989 char *p = NULL;
990 int len, ret, oldlen = 0;
992 for (;;) {
993 len = readlen (state.sock);
994 if (len == 0) {
995 errx (1, "readlen returned 0");
998 if (oldlen < len + 1) {
999 p = realloc (p, len + 1);
1000 if (!p) {
1001 err (1, "realloc %d failed", len + 1);
1003 oldlen = len + 1;
1005 readdata (state.sock, p, len);
1006 p[len] = 0;
1008 if (!strncmp ("open", p, 4)) {
1009 fz_obj *obj;
1010 size_t filenamelen;
1011 char *password;
1012 char *filename = p + 5;
1014 filenamelen = strlen (filename);
1015 password = filename + filenamelen + 1;
1017 openxref (filename, password);
1018 initpdims ();
1020 obj = fz_dict_gets (state.xref->trailer, "Info");
1021 if (obj) {
1022 char *s;
1024 obj = fz_dict_gets (obj, "Title");
1025 s = pdf_to_utf8 (obj);
1026 if (*s) {
1027 printd (state.sock, "t %s", s);
1029 fz_free (s);
1032 state.needoutline = 1;
1034 else if (!strncmp ("free", p, 4)) {
1035 void *ptr;
1037 ret = sscanf (p + 4, " %p", &ptr);
1038 if (ret != 1) {
1039 errx (1, "malformed free `%.*s' ret=%d", len, p, ret);
1041 unlinkpage (ptr);
1042 state.pig = ptr;
1044 else if (!strncmp ("search", p, 6)) {
1045 int icase, pageno, y, ret, len2, forward;
1046 char *pattern;
1047 regex_t re;
1049 ret = sscanf (p + 6, " %d %d %d %d,%n",
1050 &icase, &pageno, &y, &forward, &len2);
1051 if (ret != 4) {
1052 errx (1, "malformed search `%s' ret=%d", p, ret);
1055 pattern = p + 6 + len2;
1056 ret = regcomp (&re, pattern,
1057 REG_EXTENDED | (icase ? REG_ICASE : 0));
1058 if (ret) {
1059 char errbuf[80];
1060 size_t size;
1062 size = regerror (ret, &re, errbuf, sizeof (errbuf));
1063 printd (state.sock, "T regcomp failed `%.*s'",
1064 (int) size, errbuf);
1066 else {
1067 search (&re, pageno, y, forward);
1068 regfree (&re);
1071 else if (!strncmp ("geometry", p, 8)) {
1072 int w, h;
1074 printd (state.sock, "c");
1075 ret = sscanf (p + 8, " %d %d", &w, &h);
1076 if (ret != 2) {
1077 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
1080 lock ("geometry");
1081 state.h = h;
1082 if (w != state.w) {
1083 int i;
1084 state.w = w;
1085 for (i = 0; i < state.texcount; ++i) {
1086 state.texowners[i].slice = NULL;
1089 layout ();
1090 process_outline ();
1091 unlock ("geometry");
1092 printd (state.sock, "C %d", state.pagecount);
1094 else if (!strncmp ("reinit", p, 6)) {
1095 float rotate;
1096 int proportional;
1098 printd (state.sock, "c");
1099 ret = sscanf (p + 6, " %f %d", &rotate, &proportional);
1100 if (ret != 2) {
1101 errx (1, "bad rotate line `%.*s' ret=%d", len, p, ret);
1103 lock ("reinit");
1104 state.rotate = rotate;
1105 state.proportional = proportional;
1106 state.pagedimcount = 0;
1107 free (state.pagedims);
1108 state.pagedims = NULL;
1109 initpdims ();
1110 layout ();
1111 process_outline ();
1112 if (state.pig) {
1113 freepage (state.pig);
1114 state.pig = NULL;
1116 unlock ("reinit");
1117 printd (state.sock, "C %d", state.pagecount);
1119 else if (!strncmp ("render", p, 6)) {
1120 int pageno, pindex, w, h, ret;
1121 struct page *page;
1123 ret = sscanf (p + 6, " %d %d %d %d", &pageno, &pindex, &w, &h);
1124 if (ret != 4) {
1125 errx (1, "bad render line `%.*s' ret=%d", len, p, ret);
1128 lock ("render");
1129 page = render (pageno, pindex);
1130 unlock ("render");
1132 printd (state.sock, "r %d %d %d %d %d %d %p",
1133 pageno,
1134 state.w,
1135 state.h,
1136 state.rotate,
1137 state.proportional,
1138 w * h * 4,
1139 page);
1141 else if (!strncmp ("info", p, 4)) {
1142 pdfinfo ();
1144 else if (!strncmp ("interrupt", p, 9)) {
1145 printd (state.sock, "V interrupted");
1147 else {
1148 errx (1, "unknown command %.*s", len, p);
1151 return 0;
1154 static void showsel (struct page *page, int oy)
1156 int ox;
1157 fz_bbox bbox;
1158 fz_text_span *span;
1159 struct mark first, last;
1161 first = page->fmark;
1162 last = page->lmark;
1164 if (!first.span || !last.span) return;
1166 glEnable (GL_BLEND);
1167 glBlendFunc (GL_SRC_ALPHA, GL_SRC_ALPHA);
1168 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1170 ox = -page->pixmap->x + page->pagedim.left;
1171 oy = -page->pixmap->y + oy;
1172 for (span = first.span; span; span = span->next) {
1173 int i, j, k;
1175 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0;
1177 j = 0;
1178 k = span->len - 1;
1180 if (span == page->fmark.span && span == page->lmark.span) {
1181 j = MIN (first.i, last.i);
1182 k = MAX (first.i, last.i);
1184 else if (span == first.span) {
1185 j = first.i;
1187 else if (span == last.span) {
1188 k = last.i;
1191 for (i = j; i <= k; ++i) {
1192 bbox = fz_union_bbox (bbox, span->text[i].bbox);
1194 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1195 bbox.x0,
1196 bbox.y0,
1197 bbox.x1,
1198 bbox.y1,
1199 oy, ox);
1201 glRecti (bbox.x0 + ox, bbox.y0 + oy, bbox.x1 + ox, bbox.y1 + oy);
1203 if (span == last.span) break;
1205 glDisable (GL_BLEND);
1208 static void highlightlinks (struct page *page, int yoff)
1210 pdf_link *link;
1211 int xoff;
1213 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1214 glEnable (GL_LINE_STIPPLE);
1215 glLineStipple (0.5, 0xcccc);
1217 xoff = page->pagedim.left - page->pixmap->x;
1218 yoff -= page->pixmap->y;
1220 glBegin (GL_QUADS);
1221 for (link = page->drawpage->links; link; link = link->next) {
1222 fz_point p1, p2, p3, p4;
1223 fz_matrix ctm = page->pagedim.ctm;
1225 p1.x = link->rect.x0;
1226 p1.y = link->rect.y0;
1228 p2.x = link->rect.x1;
1229 p2.y = link->rect.y0;
1231 p3.x = link->rect.x1;
1232 p3.y = link->rect.y1;
1234 p4.x = link->rect.x0;
1235 p4.y = link->rect.y1;
1237 p1 = fz_transform_point (ctm, p1);
1238 p2 = fz_transform_point (ctm, p2);
1239 p3 = fz_transform_point (ctm, p3);
1240 p4 = fz_transform_point (ctm, p4);
1242 switch (link->kind) {
1243 case PDF_LINK_GOTO: glColor3ub (255, 0, 0); break;
1244 case PDF_LINK_URI: glColor3ub (0, 0, 255); break;
1245 default: glColor3ub (0, 0, 0); break;
1248 glVertex2f (p1.x + xoff, p1.y + yoff);
1249 glVertex2f (p2.x + xoff, p2.y + yoff);
1250 glVertex2f (p3.x + xoff, p3.y + yoff);
1251 glVertex2f (p4.x + xoff, p4.y + yoff);
1253 glEnd ();
1255 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1256 glDisable (GL_LINE_STIPPLE);
1259 static void upload2 (struct page *page, int slicenum, const char *cap)
1261 int i;
1262 int w;
1263 double start, end;
1264 struct slice *slice = &page->slices[slicenum];
1266 w = page->pixmap->w;
1268 ARSERT (w == slice->w);
1269 if (slice->texindex != -1
1270 && state.texowners[slice->texindex].slice == slice) {
1271 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
1273 else {
1274 int subimage = 0;
1275 int index = (state.texindex++ % state.texcount);
1276 size_t offset = 0;
1278 for (i = 0; i < slicenum; ++i) {
1279 offset += w * page->slices[i].h * 4;
1282 if (state.texowners[index].w == slice->w) {
1283 if (state.texowners[index].h >= slice->h ) {
1284 subimage = 1;
1286 else {
1287 state.texowners[index].h = slice->h;
1290 else {
1291 state.texowners[index].h = slice->h;
1294 state.texowners[index].slice = slice;
1295 state.texowners[index].w = slice->w;
1296 slice->texindex = index;
1298 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
1299 start = now ();
1300 if (subimage) {
1302 GLenum err = glGetError ();
1303 if (err != GL_NO_ERROR) {
1304 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w, slice->h, err);
1305 abort ();
1308 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
1313 slice->h,
1314 state.texform,
1315 state.texty,
1316 page->pixmap->samples + offset
1319 GLenum err = glGetError ();
1320 if (err != GL_NO_ERROR) {
1321 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w, slice->h, err);
1322 abort ();
1326 else {
1327 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
1329 GL_RGBA8,
1331 slice->h,
1333 state.texform,
1334 state.texty,
1335 page->pixmap->samples + offset
1339 end = now ();
1340 (void) start;
1341 (void) end;
1342 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1343 subimage ? "sub" : "img",
1344 page->pageno, slicenum,
1345 slice->w, slice->h,
1346 state.texids[slice->texindex],
1347 end - start);
1351 CAMLprim value ml_draw (value args_v, value ptr_v)
1353 CAMLparam2 (args_v, ptr_v);
1354 int dispy = Int_val (Field (args_v, 0));
1355 int w = Int_val (Field (args_v, 1));
1356 int h = Int_val (Field (args_v, 2));
1357 int py = Int_val (Field (args_v, 3));
1358 int hlinks = Bool_val (Field (args_v, 4));
1359 char *s = String_val (ptr_v);
1360 int ret;
1361 void *ptr;
1362 struct page *page;
1363 int slicenum = 0;
1364 int yoff = dispy - py;
1366 ret = sscanf (s, "%p", &ptr);
1367 if (ret != 1) {
1368 errx (1, "cannot parse pointer `%s'", s);
1370 page = ptr;
1372 w = page->pixmap->w;
1374 ARSERT (h >= 0 && "ml_draw wrong h");
1375 ARSERT (py <= page->pixmap->h && "ml_draw wrong py");
1377 glEnable (GL_TEXTURE_RECTANGLE_ARB);
1379 for (slicenum = 0; slicenum < page->slicecount; ++slicenum) {
1380 struct slice *slice = &page->slices[slicenum];
1381 if (slice->h > py) {
1382 break;
1384 py -= slice->h;
1387 h = MIN (state.h, h);
1388 while (h) {
1389 int th, left;
1390 struct slice *slice = &page->slices[slicenum];
1392 ARSERT (slicenum < page->slicecount && "ml_draw wrong slicenum");
1394 th = MIN (h, slice->h - py);
1395 upload2 (page, slicenum, "upload");
1397 left = page->pagedim.left;
1398 glBegin (GL_QUADS);
1400 glTexCoord2i (0, py);
1401 glVertex2i (left, dispy);
1403 glTexCoord2i (w, py);
1404 glVertex2i (left+w, dispy);
1406 glTexCoord2i (w, py+th);
1407 glVertex2i (left+w, dispy + th);
1409 glTexCoord2i (0, py+th);
1410 glVertex2i (left, dispy + th);
1412 glEnd ();
1414 h -= th;
1415 py = 0;
1416 dispy += th;
1417 slicenum += 1;
1420 showsel (page, yoff);
1421 if (hlinks) highlightlinks (page, yoff);
1422 glDisable (GL_TEXTURE_RECTANGLE_ARB);
1424 CAMLreturn (Val_unit);
1427 static pdf_link *getlink (struct page *page, int x, int y)
1429 fz_point p;
1430 fz_matrix ctm;
1431 pdf_link *link;
1433 p.x = x + page->pixmap->x;
1434 p.y = y + page->pixmap->y;
1436 ctm = fz_invert_matrix (page->pagedim.ctm);
1437 p = fz_transform_point (ctm, p);
1439 for (link = page->drawpage->links; link; link = link->next) {
1440 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
1441 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
1442 return link;
1446 return NULL;
1449 static void ensuretext (struct page *page)
1451 if (!page->text) {
1452 fz_error error;
1453 fz_device *tdev;
1455 page->text = fz_new_text_span ();
1456 tdev = fz_new_text_device (page->text);
1457 error = pdf_run_page (state.xref, page->drawpage, tdev,
1458 page->pagedim.ctm);
1459 if (error) die (error);
1460 fz_free_device (tdev);
1464 CAMLprim value ml_whatsunder (value ptr_v, value x_v, value y_v)
1466 CAMLparam3 (ptr_v, x_v, y_v);
1467 CAMLlocal3 (ret_v, tup_v, str_v);
1468 pdf_link *link;
1469 struct page *page;
1470 char *s = String_val (ptr_v);
1472 ret_v = Val_int (0);
1473 if (trylock ("ml_whatsunder")) {
1474 goto done;
1477 page = parse_pointer ("ml_whatsunder", s);
1478 link = getlink (page, Int_val (x_v), Int_val (y_v));
1479 if (link) {
1480 switch (link->kind) {
1481 case PDF_LINK_GOTO:
1483 int pageno;
1484 fz_point p;
1485 fz_obj *obj;
1487 pageno = -1;
1488 p.x = 0;
1489 p.y = 0;
1491 if (fz_is_array (link->dest)) {
1492 obj = fz_array_get (link->dest, 0);
1493 if (fz_is_indirect (obj)) {
1494 pageno = pdf_find_page_number (state.xref, obj);
1496 else if (fz_is_int (obj)) {
1497 pageno = fz_to_int (obj);
1500 if (fz_array_len (link->dest) > 3) {
1501 fz_obj *xo, *yo;
1503 xo = fz_array_get (link->dest, 2);
1504 yo = fz_array_get (link->dest, 3);
1505 if (!fz_is_null (xo) && !fz_is_null (yo)) {
1506 p.x = fz_to_int (xo);
1507 p.y = fz_to_int (yo);
1508 p = fz_transform_point (page->pagedim.ctm, p);
1512 else {
1513 pageno = pdf_find_page_number (state.xref, link->dest);
1515 tup_v = caml_alloc_tuple (2);
1516 ret_v = caml_alloc_small (1, 1);
1517 Field (tup_v, 0) = Val_int (pageno);
1518 Field (tup_v, 1) = Val_int (p.y);
1519 Field (ret_v, 0) = tup_v;
1521 break;
1523 case PDF_LINK_URI:
1524 str_v = caml_copy_string (fz_to_str_buf (link->dest));
1525 ret_v = caml_alloc_small (1, 0);
1526 Field (ret_v, 0) = str_v;
1527 break;
1529 default:
1530 printd (state.sock, "T unhandled link kind %d", link->kind);
1531 break;
1534 else {
1535 int i, x, y;
1536 fz_text_span *span;
1538 ensuretext (page);
1539 x = Int_val (x_v) + page->pixmap->x;
1540 y = Int_val (y_v) + page->pixmap->y;
1542 for (span = page->text; span; span = span->next) {
1543 for (i = 0; i < span->len; ++i) {
1544 fz_bbox *b;
1545 b = &span->text[i].bbox;
1546 if ((x >= b->x0 && x <= b->x1 && y >= b->y0 && y <= b->y1)) {
1547 const char *n2 =
1548 span->font && span->font->name
1549 ? span->font->name
1550 : "Span has no font name"
1552 #ifdef FT_FREETYPE_H
1553 FT_FaceRec *face = span->font->ft_face;
1554 if (face && face->family_name) {
1555 char *s;
1556 char *n1 = face->family_name;
1557 size_t l1 = strlen (n1);
1558 size_t l2 = strlen (n2);
1560 if (l1 != l2 || memcmp (n1, n2, l1)) {
1561 s = malloc (l1 + l2 + 2);
1562 if (s) {
1563 memcpy (s, n2, l2);
1564 s[l2] = '=';
1565 memcpy (s + l2 + 1, n1, l1 + 1);
1566 str_v = caml_copy_string (s);
1567 free (s);
1571 if (str_v == 0) {
1572 str_v = caml_copy_string (n2);
1574 #else
1575 str_v = caml_copy_string (n2);
1576 #endif
1577 ret_v = caml_alloc_small (1, 2);
1578 Field (ret_v, 0) = str_v;
1579 goto unlock;
1584 unlock:
1585 unlock ("ml_whatsunder");
1587 done:
1588 CAMLreturn (ret_v);
1591 CAMLprim value ml_seltext (value ptr_v, value rect_v, value oy_v)
1593 CAMLparam4 (ptr_v, rect_v, oy_v, rect_v);
1594 fz_bbox *b;
1595 struct page *page;
1596 fz_text_span *span;
1597 struct mark first, last;
1598 int i, x0, x1, y0, y1, oy, left;
1599 char *s = String_val (ptr_v);
1601 if (trylock ("ml_seltext")) {
1602 goto done;
1605 page = parse_pointer ("ml_seltext", s);
1606 ensuretext (page);
1608 oy = Int_val (oy_v);
1609 x0 = Int_val (Field (rect_v, 0));
1610 y0 = Int_val (Field (rect_v, 1));
1611 x1 = Int_val (Field (rect_v, 2));
1612 y1 = Int_val (Field (rect_v, 3));
1614 left = page->pagedim.left;
1615 if (0) {
1616 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1617 glColor3ub (128, 128, 128);
1618 glRecti (x0, y0, x1, y1);
1619 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1622 x0 += page->pixmap->x - left;
1623 y0 += page->pixmap->y - oy;
1624 x1 += page->pixmap->x - left;
1625 y1 += page->pixmap->y - oy;
1627 first.span = NULL;
1628 last.span = NULL;
1630 last.i = first.i = 0;
1631 first.span = page->text;
1632 for (span = page->text; span; span = span->next) {
1633 for (i = 0; i < span->len; ++i) {
1634 b = &span->text[i].bbox;
1635 int selected = 0;
1637 if (x0 >= b->x0 && x0 <= b->x1 && y0 >= b->y0 && y0 <= b->y1) {
1638 first.i = i;
1639 first.span = span;
1640 selected = 1;
1642 if (x1 >= b->x0 && x1 <= b->x1 && y1 >= b->y0 && y1 <= b->y1) {
1643 last.i = i;
1644 last.span = span;
1645 selected = 1;
1647 if (0 && selected) {
1648 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1649 glColor3ub (128, 128, 128);
1650 glRecti (b->x0+left, b->y0, b->x1+left, b->y1);
1651 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1656 if (y1 < y0 || x1 < x0) {
1657 int swap = 0;
1659 if (first.span == last.span) {
1660 swap = 1;
1662 else {
1663 if (y1 < y0) {
1664 for (span = first.span; span && span != last.span;
1665 span = span->next) {
1666 if (span->eol) {
1667 swap = 1;
1668 break;
1674 if (swap) {
1675 i = first.i;
1676 span = first.span;
1677 first.i = last.i;
1678 first.span = last.span;
1679 last.i = i;
1680 last.span = span;
1684 page->fmark = first;
1685 page->lmark = last;
1687 unlock ("ml_seltext");
1689 done:
1690 CAMLreturn (Val_unit);
1693 static int pipespan (FILE *f, fz_text_span *span, int a, int b)
1695 char buf[4];
1696 int i, len, ret;
1698 for (i = a; i <= b; ++i) {
1699 len = runetochar (buf, &span->text[i].c);
1700 ret = fwrite (buf, len, 1, f);
1702 if (ret != 1) {
1703 printd (state.sock, "T failed to write %d bytes ret=%d: %s",
1704 len, ret, strerror (errno));
1705 return -1;
1708 return 0;
1711 CAMLprim value ml_copysel (value ptr_v)
1713 CAMLparam1 (ptr_v);
1714 FILE *f;
1715 struct page *page;
1716 char *s = String_val (ptr_v);
1718 if (trylock ("ml_copysel")) {
1719 goto done;
1722 if (!*s) {
1723 close:
1724 #ifdef USE_XSEL
1725 if (state.xselpipe) {
1726 int ret = pclose (state.xselpipe);
1727 if (ret) {
1728 printd (state.sock, "T failed to close xsel pipe `%s'",
1729 strerror (errno));
1731 state.xselpipe = NULL;
1733 #else
1734 printf ("========================================\n");
1735 #endif
1737 else {
1738 fz_text_span *span;
1740 page = parse_pointer ("ml_sopysel", s);
1742 if (!page->fmark.span || !page->lmark.span) {
1743 printd (state.sock, "T nothing to copy");
1744 goto unlock;
1747 f = stdout;
1748 #ifdef USE_XSEL
1749 if (!state.xselpipe) {
1750 state.xselpipe = popen ("xsel -i", "w");
1751 if (!state.xselpipe) {
1752 printd (state.sock, "T failed to open xsel pipe `%s'",
1753 strerror (errno));
1755 else {
1756 f = state.xselpipe;
1759 else {
1760 f = state.xselpipe;
1762 #endif
1764 for (span = page->fmark.span;
1765 span && span != page->lmark.span->next;
1766 span = span->next) {
1767 int a = span == page->fmark.span ? page->fmark.i : 0;
1768 int b = span == page->lmark.span ? page->lmark.i : span->len - 1;
1769 if (pipespan (f, span, a, b)) {
1770 goto close;
1772 if (span->eol) {
1773 if (putc ('\n', f) == EOF) {
1774 printd (state.sock, "T failed break line on xsel pipe `%s'",
1775 strerror (errno));
1776 goto close;
1780 page->lmark.span = NULL;
1781 page->fmark.span = NULL;
1784 unlock:
1785 unlock ("ml_copysel");
1787 done:
1788 CAMLreturn (Val_unit);
1791 CAMLprim value ml_getpdimrect (value pagedimno_v)
1793 CAMLparam1 (pagedimno_v);
1794 CAMLlocal1 (ret_v);
1795 int pagedimno = Int_val (pagedimno_v);
1796 fz_rect box;
1798 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
1799 if (trylock ("ml_getpdimrect")) {
1800 box = fz_empty_rect;
1802 else {
1803 box = state.pagedims[pagedimno].box;
1804 unlock ("ml_getpdimrect");
1807 Store_double_field (ret_v, 0, box.x0);
1808 Store_double_field (ret_v, 1, box.x1);
1809 Store_double_field (ret_v, 2, box.y0);
1810 Store_double_field (ret_v, 3, box.y1);
1812 CAMLreturn (ret_v);
1815 CAMLprim value ml_zoom_for_height (value winw_v, value winh_v, value dw_v)
1817 CAMLparam3 (winw_v, winh_v, dw_v);
1818 CAMLlocal1 (ret_v);
1819 int i;
1820 double zoom = 1.0;
1821 double maxw = 0.0, maxh = 0.0;
1822 struct pagedim *p;
1823 double winw = Int_val (winw_v);
1824 double winh = Int_val (winh_v);
1825 double dw = Int_val (dw_v);
1826 double pw = 1.0, ph = 1.0, num, den;
1828 if (trylock ("ml_zoom_for_height")) {
1829 goto done;
1832 if (state.proportional) {
1833 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
1834 double x0, x1, w;
1836 x0 = MIN (p->box.x0, p->box.x1);
1837 x1 = MAX (p->box.x0, p->box.x1);
1839 w = x1 - x0;
1840 maxw = MAX (w, maxw);
1844 for (i = 0, p = state.pagedims; i < state.pagedimcount; ++i, ++p) {
1845 double x0, x1, y0, y1, w, h, scaledh, scale;
1847 x0 = MIN (p->box.x0, p->box.x1);
1848 y0 = MIN (p->box.y0, p->box.y1);
1849 x1 = MAX (p->box.x0, p->box.x1);
1850 y1 = MAX (p->box.y0, p->box.y1);
1852 w = x1 - x0;
1853 h = y1 - y0;
1855 if (state.proportional) {
1856 scale = w / maxw;
1857 scaledh = h * scale;
1859 else {
1860 scale = 1.0;
1861 scaledh = h;
1864 if (scaledh > maxh) {
1865 maxh = scaledh;
1866 ph = scaledh;
1867 pw = w * scale;
1871 num = (winh * pw) + (ph * dw);
1872 den = ph * winw;
1873 zoom = num / den;
1875 unlock ("ml_zoom_for_height");
1876 done:
1877 ret_v = caml_copy_double (zoom);
1878 CAMLreturn (ret_v);
1881 CAMLprim value ml_init (value sock_v, value params_v)
1883 #ifndef _WIN32
1884 int ret;
1885 #endif
1886 CAMLparam2 (sock_v, params_v);
1888 state.rotate = Int_val (Field (params_v, 0));
1889 state.proportional = Bool_val (Field (params_v, 1));
1890 state.texcount = Int_val (Field (params_v, 2));
1891 state.sliceheight = Int_val (Field (params_v, 3));
1892 state.texform = GL_RGBA;
1893 state.texty = GL_UNSIGNED_BYTE;
1895 fz_accelerate ();
1896 state.texids = calloc (state.texcount * sizeof (*state.texids), 1);
1897 if (!state.texids) {
1898 err (1, "calloc texids " FMT_s,
1899 state.texcount * sizeof (*state.texids));
1902 state.texowners = calloc (state.texcount * sizeof (*state.texowners), 1);
1903 if (!state.texowners) {
1904 err (1, "calloc texowners " FMT_s,
1905 state.texcount * sizeof (*state.texowners));
1908 glGenTextures (state.texcount, state.texids);
1910 #ifdef _WIN32
1911 state.sock = Socket_val (sock_v);
1912 #else
1913 state.sock = Int_val (sock_v);
1914 #endif
1916 #ifdef _WIN32
1917 InitializeCriticalSection (&critsec);
1918 state.thread = CreateThread (NULL, 0, mainloop, NULL, 0, NULL);
1919 if (state.thread == INVALID_HANDLE_VALUE) {
1920 errx (1, "CreateThread failed: %lx", GetLastError ());
1922 #else
1923 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
1924 if (ret) {
1925 errx (1, "pthread_create: %s", strerror (ret));
1927 #endif
1929 CAMLreturn (Val_unit);