Proper handling of T when textentry is active
[llpp.git] / link.c
blob82e62257e23b74535ac28540d9cd1aa5958468fc
1 /* lot's 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 ssize_t int
7 #pragma warning (disable:4244)
8 #pragma warning (disable:4996)
9 #pragma warning (disable:4995)
10 #endif
12 #ifdef _MSC_VER
13 #include <errno.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 static void __declspec (noreturn) err (int exitcode, const char *fmt, ...)
19 va_list ap;
20 int errcode;
22 errcode = errno;
23 va_start (ap, fmt);
24 vfprintf (stderr, fmt, ap);
25 va_end (ap);
26 fprintf (stderr, ": %s\n", strerror (errno));
27 exit (exitcode);
29 static void __declspec (noreturn) errx (int exitcode, const char *fmt, ...)
31 va_list ap;
32 int errcode;
34 errcode = errno;
35 va_start (ap, fmt);
36 vfprintf (stderr, fmt, ap);
37 va_end (ap);
38 fputc ('\n', stderr);
39 exit (exitcode);
41 static void __declspec (noreturn) sockerr (int exitcode, const char *fmt, ...)
43 va_list ap;
44 int errcode;
46 errcode = errno;
47 va_start (ap, fmt);
48 vfprintf (stderr, fmt, ap);
49 va_end (ap);
50 fprintf (stderr, ": wsaerror %lx\n", WSAGetLastError ());
51 exit (exitcode);
53 #else
54 #define _GNU_SOURCE
55 #include <err.h>
56 #define sockerr err
57 #endif
58 #include <regex.h>
59 #include <errno.h>
60 #include <ctype.h>
61 #include <stdio.h>
62 #include <stdarg.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #ifndef _WIN32
66 #include <pthread.h>
67 #include <sys/time.h>
68 #include <sys/types.h>
69 #include <sys/socket.h>
70 #include <sys/select.h>
71 #endif
73 #ifdef __APPLE__
74 #include <OpenGL/gl.h>
75 #else
76 #include <GL/gl.h>
77 #endif
79 #ifndef GL_TEXTURE_RECTANGLE_ARB
80 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
81 #endif
83 #include <caml/fail.h>
84 #include <caml/alloc.h>
85 #include <caml/memory.h>
86 #include <caml/unixsupport.h>
88 #include <fitz.h>
89 #include <mupdf.h>
91 #if 0
92 #define lprintf printf
93 #else
94 #define lprintf(...)
95 #endif
97 #define ARSERT(cond) for (;;) { \
98 if (!(cond)) { \
99 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
101 break; \
104 struct slice {
105 int texindex;
106 int w, h;
109 struct page {
110 int pageno;
111 int slicecount;
112 fz_textspan *text;
113 fz_pixmap *pixmap;
114 pdf_page *drawpage;
115 struct pagedim *pagedim;
116 struct page *prev;
117 struct slice slices[];
120 struct pagedim {
121 int pageno;
122 int rotate;
123 fz_rect box;
124 fz_bbox bbox;
125 fz_matrix ctm;
128 struct {
129 int sock;
130 int sliceheight;
131 struct page *pages;
132 struct pagedim *pagedims;
133 int pagecount;
134 int pagedimcount;
135 pdf_xref *xref;
136 fz_glyphcache *cache;
137 int w, h;
139 int texindex;
140 int texcount;
141 GLuint *texids;
143 GLenum texform;
144 GLenum texty;
146 int lotsamemory;
148 int *pagetbl;
149 struct {
150 int w, h;
151 struct slice *slice;
152 } *texowners;
154 int rotate;
156 #ifdef _WIN32
157 HANDLE thread;
158 #else
159 pthread_t thread;
160 #endif
161 } state;
163 #ifdef _WIN32
164 static CRITICAL_SECTION critsec;
166 static void lock (void *unused)
168 (void) unused;
169 EnterCriticalSection (&critsec);
172 static void unlock (void *unused)
174 (void) unused;
175 LeaveCriticalSection (&critsec);
178 static int trylock (void *unused)
180 return TryEnterCriticalSection (&critsec) == 0;
182 #else
183 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
185 static void lock (const char *cap)
187 int ret = pthread_mutex_lock (&mutex);
188 if (ret) {
189 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
193 static void unlock (const char *cap)
195 int ret = pthread_mutex_unlock (&mutex);
196 if (ret) {
197 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
201 static int trylock (const char *cap)
203 int ret = pthread_mutex_trylock (&mutex);
205 if (ret && ret != EBUSY) {
206 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
208 return ret == EBUSY;
210 #endif
212 static void *parse_pointer (const char *cap, const char *s)
214 int ret;
215 void *ptr;
217 ret = sscanf (s, "%p", &ptr);
218 if (ret != 1) {
219 errx (1, "%s: cannot parse pointer in `%s'", cap, s);
221 return ptr;
224 static int hasdata (int sock)
226 int n;
227 fd_set s;
228 struct timeval tv;
229 FD_ZERO (&s);
230 FD_SET (sock, &s);
231 tv.tv_sec = 0;
232 tv.tv_usec = 0;
233 n = select (sock + 1, &s, NULL, NULL, &tv);
234 if (n == 0) return 0;
235 if (n == 1) return 1;
236 sockerr (1, "hasdata: select error ret=%d", n);
239 static double now (void)
241 struct timeval tv;
243 if (gettimeofday (&tv, NULL)) {
244 err (1, "gettimeofday");
246 return tv.tv_sec + tv.tv_usec*1e-6;
249 static void readdata (int fd, char *p, int size)
251 ssize_t n;
253 n = recv (fd, p, size, 0);
254 if (n - size) {
255 if (!n) errx (1, "EOF while reading");
256 sockerr (1, "recv (req %d, ret %zd)", size, n);
260 static void writedata (int fd, char *p, int size)
262 char buf[4];
263 ssize_t n;
265 buf[0] = (size >> 24) & 0xff;
266 buf[1] = (size >> 16) & 0xff;
267 buf[2] = (size >> 8) & 0xff;
268 buf[3] = (size >> 0) & 0xff;
270 n = send (fd, buf, 4, 0);
271 if (n != 4) {
272 if (!n) errx (1, "EOF while writing length");
273 sockerr (1, "send %zd", n);
276 n = send (fd, p, size, 0);
277 if (n - size) {
278 if (!n) errx (1, "EOF while writing data");
279 sockerr (1, "send (req %d, ret %zd)", size, n);
283 static void
284 #ifdef __GNUC__
285 __attribute__ ((format (printf, 2, 3)))
286 #endif
287 printd (int fd, const char *fmt, ...)
289 int len;
290 va_list ap;
291 char buf[200];
293 va_start (ap, fmt);
294 len = vsnprintf (buf, sizeof (buf), fmt, ap);
295 va_end (ap);
296 writedata (fd, buf, len);
299 static void die (fz_error error)
301 fz_catch (error, "aborting");
302 if (state.xref)
303 pdf_closexref (state.xref);
304 exit (1);
307 static void openxref (char *filename)
309 int fd;
310 fz_error error;
311 fz_stream *file;
313 fd = open (filename, O_BINARY | O_RDONLY, 0666);
314 if (fd < 0)
315 die (fz_throw ("cannot open file '%s'", filename));
317 file = fz_openfile (fd);
318 state.xref = pdf_openxref (file);
319 if (!state.xref)
320 die (fz_throw ("cannot open PDF file '%s'", filename));
321 fz_dropstream (file);
323 if (pdf_needspassword (state.xref)) {
324 die (fz_throw ("password protected"));
327 error = pdf_loadpagetree (state.xref);
328 if (error) {
329 die (fz_throw ("cannot load page tree"));
332 state.pagecount = pdf_getpagecount (state.xref);
333 state.pagetbl = stat_alloc (state.pagecount * sizeof (*state.pagetbl));
336 static int readlen (int fd)
338 ssize_t n;
339 char p[4];
341 n = recv (fd, p, 4, 0);
342 if (n != 4) {
343 if (!n) errx (1, "EOF while reading length");
344 sockerr (1, "read %zd", n);
347 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
350 static void freepage (struct page *page)
352 int i;
353 struct page *p;
355 fz_droppixmap (page->pixmap);
356 for (p = state.pages; p; p = p->prev) {
357 if (p->prev == page) {
358 p->prev = page->prev;
359 break;
362 for (i = 0; i < page->slicecount; ++i) {
363 struct slice *s = &page->slices[i];
364 if (s->texindex != -1) {
365 if (state.texowners[s->texindex].slice == s) {
366 state.texowners[s->texindex].slice = NULL;
367 ARSERT (state.texowners[s->texindex].w == s->w);
368 ARSERT (state.texowners[s->texindex].h >= s->h);
372 if (page->text) {
373 fz_freetextspan (page->text);
375 if (page->drawpage) {
376 pdf_freepage (page->drawpage);
379 free (page);
382 static void subdivide (struct page *p)
384 int i;
385 int h = p->pixmap->h;
386 int th = MIN (h, state.sliceheight);
388 for (i = 0; i < p->slicecount; ++i) {
389 struct slice *s = &p->slices[i];
390 s->texindex = -1;
391 s->h = MIN (th, h);
392 s->w = p->pixmap->w;
393 h -= th;
397 static void *render (int pageno, int pindex)
399 fz_error error;
400 int slicecount;
401 fz_obj *pageobj;
402 struct page *page;
403 double start, end;
404 pdf_page *drawpage;
405 fz_device *idev;
406 struct pagedim *pagedim;
408 start = now ();
409 printd (state.sock, "V rendering %d", pageno);
410 pdf_flushxref (state.xref, 0);
412 pagedim = &state.pagedims[pindex];
413 slicecount = (pagedim->bbox.y1 - pagedim->bbox.y0
414 + state.sliceheight - 1) / state.sliceheight;
415 slicecount += slicecount == 0;
417 page = calloc (sizeof (*page)
418 + (slicecount * sizeof (struct slice)), 1);
419 if (!page) {
420 err (1, "calloc page %d\n", pageno);
422 page->slicecount = slicecount;
423 page->prev = state.pages;
424 state.pages = page;
426 pageobj = pdf_getpageobject (state.xref, pageno);
427 if (!pageobj)
428 die (fz_throw ("cannot retrieve info from page %d", pageno));
430 error = pdf_loadpage (&drawpage, state.xref, pageobj);
431 if (error)
432 die (error);
434 page->pixmap = fz_newpixmapwithrect (pdf_devicergb, pagedim->bbox);
435 if (error)
436 die (error);
437 fz_clearpixmap (page->pixmap, 0xFF);
439 idev = fz_newdrawdevice (state.cache, page->pixmap);
440 if (!idev)
441 die (fz_throw ("fz_newdrawdevice failed"));
442 error = pdf_runcontentstream (idev, pagedim->ctm, state.xref,
443 drawpage->resources,
444 drawpage->contents);
445 fz_freedevice (idev);
447 page->drawpage = drawpage;
448 page->pagedim = pagedim;
449 page->pageno = pageno;
450 subdivide (page);
451 end = now ();
453 if (!state.lotsamemory) {
454 pdf_agestoreditems (state.xref->store);
455 pdf_evictageditems (state.xref->store);
458 printd (state.sock, "V rendering %d took %f sec", pageno, end - start);
459 return page;
462 static void initpdims (void)
464 int pageno;
465 double start, end;
467 start = now ();
468 for (pageno = 0; pageno < state.pagecount; ++pageno) {
469 int rotate;
470 fz_rect box;
471 struct pagedim *p;
472 fz_obj *obj, *pageobj;
474 pageobj = pdf_getpageobject (state.xref, pageno + 1);
476 obj = fz_dictgets (pageobj, "CropBox");
477 if (!fz_isarray (obj)) {
478 obj = fz_dictgets (pageobj, "MediaBox");
479 if (!fz_isarray (obj)) {
480 die (fz_throw ("cannot find page bounds %d (%d Rd)",
481 fz_tonum (pageobj), fz_togen (pageobj)));
484 box = pdf_torect (obj);
486 obj = fz_dictgets (pageobj, "Rotate");
487 if (fz_isint (obj)) {
488 rotate = fz_toint (obj);
490 else {
491 rotate = 0;
493 rotate += state.rotate;
495 state.pagetbl[pageno] = fz_tonum (state.xref->pagerefs[pageno]);
496 p = &state.pagedims[state.pagedimcount - 1];
497 if ((state.pagedimcount == 0)
498 || (p->rotate != rotate || memcmp (&p->box, &box, sizeof (box)))) {
499 size_t size;
501 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
502 state.pagedims = realloc (state.pagedims, size);
503 if (!state.pagedims) {
504 err (1, "realloc pagedims to %zu (%d elems)",
505 size, state.pagedimcount + 1);
507 p = &state.pagedims[state.pagedimcount++];
508 p->rotate = rotate;
509 p->box = box;
510 p->pageno = pageno;
513 end = now ();
514 printd (state.sock, "T Processed %d pages in %f seconds",
515 state.pagecount, end - start);
518 static void layout (void)
520 int pindex;
521 fz_matrix ctm;
522 fz_rect box, box2;
523 double zoom, w;
524 struct pagedim *p = state.pagedims;
526 pindex = 0;
527 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
528 box.x0 = MIN (p->box.x0, p->box.x1);
529 box.y0 = MIN (p->box.y0, p->box.y1);
530 box.x1 = MAX (p->box.x0, p->box.x1);
531 box.y1 = MAX (p->box.y0, p->box.y1);
533 ctm = fz_identity ();
534 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
535 ctm = fz_concat (ctm, fz_rotate (p->rotate));
536 box2 = fz_transformrect (ctm, box);
537 w = box2.x1 - box2.x0;
539 zoom = (state.w / w);
540 ctm = fz_identity ();
541 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
542 ctm = fz_concat (ctm, fz_scale (zoom, -zoom));
543 ctm = fz_concat (ctm, fz_rotate (p->rotate));
544 p->bbox = fz_roundrect (fz_transformrect (ctm, box));
545 memcpy (&p->ctm, &ctm, sizeof (ctm));
548 while (p-- != state.pagedims) {
549 printd (state.sock, "l %d %d %d",
550 p->pageno, p->bbox.x1 - p->bbox.x0, p->bbox.y1 - p->bbox.y0);
554 static void recurse_outline (pdf_outline *outline, int level)
556 while (outline) {
557 int i, num;
558 fz_obj *obj;
559 int top = 0;
560 int pageno = -1;
562 if (!outline->link) goto next;
564 obj = outline->link->dest;
565 if (fz_isindirect (obj)) {
566 num = fz_tonum (obj);
568 for (i = 0; i < state.pagecount; ++i) {
569 if (state.pagetbl[i] == num) {
570 pageno = i;
571 break;
575 else if (fz_isarray (obj)) {
576 fz_obj *obj2;
578 obj2 = fz_arrayget (obj, 0);
579 if (fz_isint (obj2)) {
580 pageno = fz_toint (obj2);
582 else {
583 num = fz_tonum (obj2);
584 for (i = 0; i < state.pagecount; ++i) {
585 if (state.pagetbl[i] == num) {
586 pageno = i;
587 break;
592 if (fz_arraylen (obj) > 3) {
593 fz_point p;
594 struct pagedim *pagedim = state.pagedims;
596 for (i = 0; i < state.pagedimcount; ++i) {
597 if (state.pagedims[i].pageno > pageno)
598 break;
599 pagedim = &state.pagedims[i];
602 p.x = fz_toint (fz_arrayget (obj, 2));
603 p.y = fz_toint (fz_arrayget (obj, 3));
604 p = fz_transformpoint (pagedim->ctm, p);
605 top = p.y;
609 lprintf ("%*c%s %d\n", level, ' ', outline->title, pageno);
610 printd (state.sock, "o %d %d %d %s",
611 level, pageno, top, outline->title);
612 next:
613 if (outline->child) {
614 recurse_outline (outline->child, level + 1);
616 outline = outline->next;
620 static void process_outline (void)
622 pdf_outline *outline;
624 outline = pdf_loadoutline (state.xref);
625 if (outline) {
626 recurse_outline (outline, 0);
627 pdf_freeoutline (outline);
631 static int comparespans (const void *l, const void *r)
633 fz_textspan const *const*ls = l;
634 fz_textspan const *const*rs = r;
635 return (*ls)->text->bbox.y0 - (*rs)->text->bbox.y0;
638 /* wishful thinking function */
639 static void search (regex_t *re, int pageno, int y, int forward)
641 int i, j;
642 int ret;
643 char *p;
644 char buf[256];
645 fz_error error;
646 fz_obj *pageobj;
647 fz_device *tdev;
648 pdf_page *drawpage;
649 fz_textspan *text, *span, **pspan;
650 struct pagedim *pdim, *pdimprev;
651 int stop = 0;
652 int niters = 0;
653 int nspans;
654 double start, end;
656 start = now ();
657 while (pageno >= 0 && pageno < state.pagecount && !stop) {
658 if (niters++ == 5) {
659 if (!state.lotsamemory) {
660 pdf_agestoreditems (state.xref->store);
661 pdf_evictageditems (state.xref->store);
663 niters = 0;
664 if (hasdata (state.sock)) {
665 printd (state.sock, "T attention requested aborting search at %d",
666 pageno);
667 stop = 1;
669 else {
670 printd (state.sock, "T searching in page %d", pageno);
673 pdimprev = NULL;
674 for (i = 0; i < state.pagedimcount; ++i) {
675 pdim = &state.pagedims[i];
676 if (pdim->pageno == pageno) {
677 goto found;
679 if (pdim->pageno > pageno) {
680 pdim = pdimprev;
681 goto found;
683 pdimprev = pdim;
685 pdim = pdimprev;
686 found:
688 pageobj = pdf_getpageobject (state.xref, pageno + 1);
689 if (!pageobj)
690 die (fz_throw ("cannot retrieve info from page %d", pageno));
692 error = pdf_loadpage (&drawpage, state.xref, pageobj);
693 if (error)
694 die (error);
696 text = fz_newtextspan ();
697 tdev = fz_newtextdevice (text);
698 error = pdf_runcontentstream (tdev, pdim->ctm, state.xref,
699 drawpage->resources,
700 drawpage->contents);
701 if (error) die (error);
702 fz_freedevice (tdev);
704 nspans = 0;
705 for (span = text; span; span = span->next) {
706 nspans++;
708 pspan = malloc (sizeof (void *) * nspans);
709 if (!pspan) {
710 err (1, "malloc span pointers %zu", sizeof (void *) * nspans);
712 for (i = 0, span = text; span; span = span->next, ++i) {
713 pspan[i] = span;
715 qsort (pspan, nspans, sizeof (fz_textspan *), comparespans);
717 j = forward ? 0 : nspans - 1;
718 while (nspans--) {
719 regmatch_t rm;
721 span = pspan[j];
722 j += forward ? 1 : -1;
723 p = buf;
724 for (i = 0; i < MIN (span->len, (int) sizeof (buf) - 1); ++i) {
725 if (forward) {
726 if (span->text[i].bbox.y0 < y + 1) {
727 continue;
730 else {
731 if (span->text[i].bbox.y0 > y - 1) {
732 continue;
735 if (span->text[i].c < 256) {
736 *p++ = span->text[i].c;
738 else {
739 *p++ = '?';
742 if (p == buf) {
743 continue;
745 *p++ = 0;
747 ret = regexec (re, buf, 1, &rm, 0);
748 if (ret) {
749 if (ret != REG_NOMATCH) {
750 size_t size;
751 char errbuf[80];
752 size = regerror (ret, re, errbuf, sizeof (errbuf));
753 printd (state.sock,
754 "T regexec error `%.*s'",
755 (int) size, errbuf);
756 fz_freetextspan (text);
757 pdf_freepage (drawpage);
758 free (pspan);
759 return;
762 else {
763 fz_rect r;
765 r.x0 = span->text[rm.rm_so].bbox.x0 - pdim->bbox.x0;
766 r.y0 = span->text[rm.rm_so].bbox.y0;
767 r.x1 = span->text[rm.rm_eo - 1].bbox.x1 - pdim->bbox.x0;
768 r.y1 = span->text[rm.rm_eo - 1].bbox.y1;
770 if (!stop) {
771 printd (state.sock, "F %d %d %f %f %f %f",
772 pageno, 1,
773 r.x0, r.y0,
774 r.x1, r.y1);
776 else {
777 printd (state.sock, "R %d %d %f %f %f %f",
778 pageno, 2,
779 r.x0, r.y0,
780 r.x1, r.y1);
782 printd (state.sock, "T found at %d `%.*s' %f in %f sec",
783 pageno, rm.rm_eo - rm.rm_so, &buf[rm.rm_so],
784 span->text[0].bbox.y0 - drawpage->mediabox.y0,
785 now () - start);
786 stop = 1;
789 if (forward) {
790 pageno += 1;
791 y = 0;
793 else {
794 pageno -= 1;
795 y = INT_MAX;
797 fz_freetextspan (text);
798 pdf_freepage (drawpage);
799 free (pspan);
801 end = now ();
802 if (!stop) {
803 printd (state.sock, "T no matches %f sec", end - start);
805 printd (state.sock, "D");
808 static
809 #ifdef _WIN32
810 DWORD _stdcall
811 #else
812 void *
813 #endif
814 mainloop (void *unused)
816 char *p = NULL;
817 int len, ret, oldlen = 0;
819 for (;;) {
820 len = readlen (state.sock);
821 if (len == 0) {
822 errx (1, "readlen returned 0");
825 if (oldlen < len + 1) {
826 p = realloc (p, len + 1);
827 if (!p) {
828 err (1, "realloc %d failed", len + 1);
830 oldlen = len + 1;
832 readdata (state.sock, p, len);
833 p[len] = 0;
835 if (!strncmp ("open", p, 4)) {
836 fz_obj *obj;
837 char *filename = p + 5;
839 openxref (filename);
840 initpdims ();
842 obj = fz_dictgets (state.xref->trailer, "Info");
843 if (obj) {
844 obj = fz_dictgets (obj, "Title");
845 if (obj) {
846 printd (state.sock, "t %s", pdf_toutf8 (obj));
850 else if (!strncmp ("free", p, 4)) {
851 void *ptr;
853 ret = sscanf (p + 4, " %p", &ptr);
854 if (ret != 1) {
855 errx (1, "malformed free `%.*s' ret=%d", len, p, ret);
857 lock ("free");
858 freepage (ptr);
859 unlock ("free");
860 printd (state.sock, "d");
862 else if (!strncmp ("search", p, 6)) {
863 int icase, pageno, y, ret, len2, forward;
864 char *pattern;
865 regex_t re;
867 ret = sscanf (p + 6, " %d %d %d %d,%n",
868 &icase, &pageno, &y, &forward, &len2);
869 if (ret != 4) {
870 errx (1, "malformed search `%s' ret=%d", p, ret);
873 pattern = p + 6 + len2;
874 ret = regcomp (&re, pattern,
875 REG_EXTENDED | (icase ? REG_ICASE : 0));
876 if (ret) {
877 char errbuf[80];
878 size_t size;
880 size = regerror (ret, &re, errbuf, sizeof (errbuf));
881 printd (state.sock, "T regcomp failed `%.*s'",
882 (int) size, errbuf);
884 else {
885 search (&re, pageno, y, forward);
886 regfree (&re);
889 else if (!strncmp ("geometry", p, 8)) {
890 int w, h;
892 printd (state.sock, "c");
893 ret = sscanf (p + 8, " %d %d", &w, &h);
894 if (ret != 2) {
895 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
897 state.h = h;
898 if (w != state.w) {
899 int i;
900 state.w = w;
901 for (i = 0; i < state.texcount; ++i) {
902 state.texowners[i].slice = NULL;
905 lock ("geometry");
906 layout ();
907 process_outline ();
908 unlock ("geometry");
909 printd (state.sock, "C %d", state.pagecount);
911 else if (!strncmp ("rotate", p, 6)) {
912 float rotate;
914 printd (state.sock, "c");
915 ret = sscanf (p + 6, " %f", &rotate);
916 if (ret != 1) {
917 errx (1, "bad rotate line `%.*s' ret=%d", len, p, ret);
919 state.rotate = rotate;
920 lock ("rotate");
921 state.pagedimcount = 0;
922 free (state.pagedims);
923 state.pagedims = NULL;
924 initpdims ();
925 layout ();
926 process_outline ();
927 unlock ("rotate");
928 printd (state.sock, "C %d", state.pagecount);
930 else if (!strncmp ("render", p, 6)) {
931 int pageno, pindex, w, h, ret;
932 struct page *page;
934 ret = sscanf (p + 6, " %d %d %d %d", &pageno, &pindex, &w, &h);
935 if (ret != 4) {
936 errx (1, "bad render line `%.*s' ret=%d", len, p, ret);
939 page = render (pageno, pindex);
940 printd (state.sock, "r %d %d %d %d %p\n",
941 pageno,
942 state.w,
943 state.h,
944 state.rotate,
945 page);
947 else if (!strncmp ("interrupt", p, 8)) {
948 printd (state.sock, "V interrupted");
950 else {
951 errx (1, "unknown command %.*s", len, p);
954 return 0;
957 static void upload2 (struct page *page, int slicenum, const char *cap)
959 int i;
960 int w, h;
961 double start, end;
962 struct slice *slice = &page->slices[slicenum];
964 w = page->pixmap->w;
965 h = page->pixmap->h;
967 ARSERT (w == slice->w);
968 if (slice->texindex != -1
969 && state.texowners[slice->texindex].slice == slice) {
970 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
972 else {
973 int subimage = 0;
974 int index = (state.texindex++ % state.texcount);
975 size_t offset = 0;
977 for (i = 0; i < slicenum; ++i) {
978 offset += w * page->slices[i].h * 4;
981 if (state.texowners[index].w == slice->w) {
982 if (state.texowners[index].h >= slice->h ) {
983 subimage = 1;
985 else {
986 state.texowners[index].h = slice->h;
989 else {
990 state.texowners[index].h = slice->h;
993 state.texowners[index].slice = slice;
994 state.texowners[index].w = slice->w;
995 slice->texindex = index;
997 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
998 start = now ();
999 if (subimage) {
1001 GLenum err = glGetError ();
1002 if (err != GL_NO_ERROR) {
1003 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w, slice->h, err);
1004 abort ();
1007 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
1012 slice->h,
1013 state.texform,
1014 state.texty,
1015 page->pixmap->samples + offset
1018 GLenum err = glGetError ();
1019 if (err != GL_NO_ERROR) {
1020 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w, slice->h, err);
1021 abort ();
1025 else {
1026 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
1028 GL_RGBA8,
1030 slice->h,
1032 state.texform,
1033 state.texty,
1034 page->pixmap->samples + offset
1038 end = now ();
1039 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1040 subimage ? "sub" : "img",
1041 page->pageno, slicenum,
1042 slice->w, slice->h,
1043 state.texids[slice->texindex],
1044 end - start);
1048 CAMLprim value ml_draw (value dispy_v, value w_v, value h_v,
1049 value py_v, value ptr_v)
1051 CAMLparam5 (dispy_v, w_v, h_v, py_v, ptr_v);
1052 int dispy = Int_val (dispy_v);
1053 int w = Int_val (w_v);
1054 int h = Int_val (h_v);
1055 int py = Int_val (py_v);
1056 char *s = String_val (ptr_v);
1057 int ret;
1058 void *ptr;
1059 struct page *page;
1060 int slicenum = 0;
1062 if (trylock ("ml_draw")) {
1063 goto done;
1066 ret = sscanf (s, "%p", &ptr);
1067 if (ret != 1) {
1068 errx (1, "cannot parse pointer `%s'", s);
1070 page = ptr;
1072 w = page->pixmap->w;
1074 ARSERT (h >= 0 && "ml_draw wrong h");
1076 glEnable (GL_TEXTURE_RECTANGLE_ARB);
1078 for (slicenum = 0; slicenum < page->slicecount; ++slicenum) {
1079 struct slice *slice = &page->slices[slicenum];
1080 if (slice->h > py) {
1081 break;
1083 py -= slice->h;
1086 h = MIN (state.h, h);
1087 while (h) {
1088 int th;
1089 struct slice *slice = &page->slices[slicenum];
1091 ARSERT (slicenum < page->slicecount && "ml_draw wrong slicenum");
1093 th = MIN (h, slice->h - py);
1094 upload2 (page, slicenum, "upload");
1096 glBegin (GL_QUADS);
1098 glTexCoord2i (0, py);
1099 glVertex2i (0, dispy);
1101 glTexCoord2i (w, py);
1102 glVertex2i (w, dispy);
1104 glTexCoord2i (w, py+th);
1105 glVertex2i (w, dispy + th);
1107 glTexCoord2i (0, py+th);
1108 glVertex2i (0, dispy + th);
1110 glEnd ();
1112 h -= th;
1113 py = 0;
1114 dispy += th;
1115 slicenum += 1;
1118 glDisable (GL_TEXTURE_RECTANGLE_ARB);
1120 unlock ("ml_draw");
1121 done:
1122 CAMLreturn (Val_unit);
1125 static pdf_link *getlink (struct page *page, int x, int y)
1127 fz_point p;
1128 fz_matrix ctm;
1129 pdf_link *link;
1131 p.x = x + page->pixmap->x;
1132 p.y = y + page->pixmap->y;
1134 ctm = fz_invertmatrix (page->pagedim->ctm);
1135 p = fz_transformpoint (ctm, p);
1137 for (link = page->drawpage->links; link; link = link->next) {
1138 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
1139 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
1140 if (link->kind == PDF_LGOTO) {
1141 return link;
1146 return NULL;
1149 CAMLprim value ml_checklink (value ptr_v, value x_v, value y_v)
1151 CAMLparam3 (ptr_v, x_v, y_v);
1152 char *s = String_val (ptr_v);
1153 int ret;
1155 if (trylock ("ml_checklink")) {
1156 ret = 0;
1158 else {
1159 ret = NULL != getlink (parse_pointer ("ml_checklink", s),
1160 Int_val (x_v), Int_val (y_v));
1161 unlock ("ml_checklink");
1163 CAMLreturn (Val_bool (ret));
1166 CAMLprim value ml_getlink (value ptr_v, value x_v, value y_v)
1168 CAMLparam3 (ptr_v, x_v, y_v);
1169 CAMLlocal2 (ret_v, tup_v);
1170 pdf_link *link;
1171 struct page *page;
1172 char *s = String_val (ptr_v);
1174 if (trylock ("ml_getlink")) {
1175 ret_v = Val_int (0);
1176 goto done;
1179 page = parse_pointer ("ml_getlink", s);
1181 link = getlink (page, Int_val (x_v), Int_val (y_v));
1182 if (link) {
1183 int pageno;
1184 fz_point p;
1185 fz_obj *obj;
1187 pageno = -1;
1188 obj = fz_arrayget (link->dest, 0);
1189 if (fz_isindirect (obj)) {
1190 pageno = pdf_findpageobject (state.xref, obj) - 1;
1192 else if (fz_isint (obj)) {
1193 pageno = fz_toint (obj);
1196 if (fz_arraylen (link->dest) > 3) {
1197 p.x = fz_toint (fz_arrayget (link->dest, 2));
1198 p.y = fz_toint (fz_arrayget (link->dest, 3));
1199 p = fz_transformpoint (page->pagedim->ctm, p);
1201 else {
1202 p.x = 0.0;
1203 p.y = 0.0;
1206 tup_v = caml_alloc_tuple (2);
1207 ret_v = caml_alloc_small (1, 1);
1208 Field (tup_v, 0) = Val_int (pageno);
1209 Field (tup_v, 1) = Val_int (p.y);
1210 Field (ret_v, 0) = tup_v;
1212 else {
1213 ret_v = Val_int (0);
1215 unlock ("ml_getlink");
1217 done:
1218 CAMLreturn (ret_v);
1221 CAMLprim value ml_gettext (value ptr_v, value rect_v, value oy_v, value rectsel_v)
1223 CAMLparam4 (ptr_v, rect_v, oy_v, rect_v);
1224 fz_matrix ctm;
1225 fz_point p1, p2;
1226 struct page *page;
1227 fz_textspan *span;
1228 char *s = String_val (ptr_v);
1229 int rectsel = Bool_val (rectsel_v);
1230 int i, bx0, bx1, by0, by1, x0, x1, y0, y1, oy;
1232 /* stop GCC from complaining about uninitialized variables */
1233 #ifdef __GNUC__
1234 int rx0 = rx0, rx1 = rx1, ry0 = ry0, ry1 = ry1;
1235 #else
1236 int rx0, rx1, ry0, ry1;
1237 #endif
1239 if (trylock ("ml_gettext")) {
1240 goto done;
1243 page = parse_pointer ("ml_gettext", s);
1245 oy = Int_val (oy_v);
1246 p1.x = Int_val (Field (rect_v, 0));
1247 p1.y = Int_val (Field (rect_v, 1));
1248 p2.x = Int_val (Field (rect_v, 2));
1249 p2.y = Int_val (Field (rect_v, 3));
1251 if (0) {
1252 glEnable (GL_BLEND);
1253 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1254 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1255 glColor4f (0.0f, 0.0f, 0.0f, 0.2f);
1256 glRecti (p1.x, p1.y, p2.x, p2.y);
1257 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1258 glDisable (GL_BLEND);
1261 ctm = page->pagedim->ctm;
1262 if (!page->text) {
1263 fz_error error;
1264 fz_device *tdev;
1266 page->text = fz_newtextspan ();
1267 tdev = fz_newtextdevice (page->text);
1268 error = pdf_runcontentstream (tdev, page->pagedim->ctm, state.xref,
1269 page->drawpage->resources,
1270 page->drawpage->contents);
1271 if (error) die (error);
1272 fz_freedevice (tdev);
1275 printf ("\033c");
1277 printf ("BBox %f %f %f %f\n", p1.x, p1.y, p2.x, p2.y);
1278 p1.x += page->pixmap->x;
1279 p1.y += page->pixmap->y;
1280 p2.x += page->pixmap->x;
1281 p2.y += page->pixmap->y;
1282 x0 = p1.x;
1283 y0 = p1.y;
1284 x1 = p2.x;
1285 y1 = p2.y;
1286 printf ("BBox %d %d %d %d %d %d\n", x0, y0, x1, y1, oy, page->pageno);
1288 for (span = page->text; span; span = span->next) {
1289 int seen = 0;
1291 /* fz_debugtextspanxml (span); */
1292 for (i = 0; i < span->len; ++i) {
1293 long c;
1295 bx0 = span->text[i].bbox.x0;
1296 bx1 = span->text[i].bbox.x1;
1297 by0 = span->text[i].bbox.y0 + oy;
1298 by1 = span->text[i].bbox.y1 + oy;
1300 if ((bx1 >= x0 && bx0 <= x1 && by1 >= y0 && by0 <= y1)) {
1301 if (!seen) {
1302 rx0 = bx0 - page->pixmap->x;
1303 rx1 = bx1 - page->pixmap->x;
1304 ry0 = by0;
1305 ry1 = by1;
1308 seen = 1;
1309 c = span->text[i].c;
1310 if (c < 256) {
1311 if ((isprint (c) && !isspace (c))) {
1312 if (!rectsel) {
1313 bx0 -= page->pixmap->x;
1314 bx1 -= page->pixmap->x;
1315 glEnable (GL_BLEND);
1316 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1317 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1318 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1319 glRecti (bx0, by0, bx1, by1);
1320 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1321 glDisable (GL_BLEND);
1323 if (isprint (c) || c ==' ') {
1324 rx1 = bx1;
1325 ry1 = by1;
1328 putc (c, stdout);
1330 else {
1331 putc ('?', stdout);
1336 if (rectsel) {
1337 if (seen) {
1338 glEnable (GL_BLEND);
1339 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1340 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1341 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1342 glRecti (rx0, ry0, rx1, ry1);
1343 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1344 glDisable (GL_BLEND);
1348 if (seen && span->eol) {
1349 x0 = page->pixmap->x;
1350 putc ('\n', stdout);
1353 unlock ("ml_gettext");
1355 done:
1356 CAMLreturn (Val_unit);
1359 CAMLprim value ml_getpagewh (value pagedimno_v)
1361 CAMLparam1 (pagedimno_v);
1362 CAMLlocal1 (ret_v);
1363 int pagedimno = Int_val (pagedimno_v);
1365 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
1366 Store_double_field (ret_v, 0, state.pagedims[pagedimno].box.x0);
1367 Store_double_field (ret_v, 1, state.pagedims[pagedimno].box.x1);
1368 Store_double_field (ret_v, 2, state.pagedims[pagedimno].box.y0);
1369 Store_double_field (ret_v, 3, state.pagedims[pagedimno].box.y1);
1370 CAMLreturn (ret_v);
1373 CAMLprim value ml_init (value sock_v)
1375 #ifndef _WIN32
1376 int ret;
1377 #endif
1378 CAMLparam1 (sock_v);
1380 state.texcount = 256;
1381 state.sliceheight = 24;
1382 state.texform = GL_RGBA;
1383 state.texty = GL_UNSIGNED_BYTE;
1385 state.texids = calloc (state.texcount * sizeof (*state.texids), 1);
1386 if (!state.texids) {
1387 err (1, "calloc texids %zu", state.texcount * sizeof (*state.texids));
1390 state.texowners = calloc (state.texcount * sizeof (*state.texowners), 1);
1391 if (!state.texowners) {
1392 err (1, "calloc texowners %zu",
1393 state.texcount * sizeof (*state.texowners));
1396 glGenTextures (state.texcount, state.texids);
1398 #ifdef _WIN32
1399 state.sock = Socket_val (sock_v);
1400 #else
1401 state.sock = Int_val (sock_v);
1402 #endif
1404 state.cache = fz_newglyphcache ();
1405 if (!state.cache) {
1406 errx (1, "fz_newglyphcache failed");
1409 #ifdef _WIN32
1410 InitializeCriticalSection (&critsec);
1411 state.thread = CreateThread (NULL, 0, mainloop, NULL, 0, NULL);
1412 if (state.thread == INVALID_HANDLE_VALUE) {
1413 errx (1, "CreateThread failed: %lx", GetLastError ());
1415 #else
1416 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
1417 if (ret) {
1418 errx (1, "pthread_create: %s", strerror (errno));
1420 #endif
1422 CAMLreturn (Val_unit);