agraff bindings
[llpp.git] / link.c
blob38b304deca919563d2ad7f328be197a59f78a980
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 {
948 errx (1, "unknown command %.*s", len, p);
951 return 0;
954 static void upload2 (struct page *page, int slicenum, const char *cap)
956 int i;
957 int w, h;
958 double start, end;
959 struct slice *slice = &page->slices[slicenum];
961 w = page->pixmap->w;
962 h = page->pixmap->h;
964 ARSERT (w == slice->w);
965 if (slice->texindex != -1
966 && state.texowners[slice->texindex].slice == slice) {
967 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
969 else {
970 int subimage = 0;
971 int index = (state.texindex++ % state.texcount);
972 size_t offset = 0;
974 for (i = 0; i < slicenum; ++i) {
975 offset += w * page->slices[i].h * 4;
978 if (state.texowners[index].w == slice->w) {
979 if (state.texowners[index].h >= slice->h ) {
980 subimage = 1;
982 else {
983 state.texowners[index].h = slice->h;
986 else {
987 state.texowners[index].h = slice->h;
990 state.texowners[index].slice = slice;
991 state.texowners[index].w = slice->w;
992 slice->texindex = index;
994 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
995 start = now ();
996 if (subimage) {
998 GLenum err = glGetError ();
999 if (err != GL_NO_ERROR) {
1000 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w, slice->h, err);
1001 abort ();
1004 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
1009 slice->h,
1010 state.texform,
1011 state.texty,
1012 page->pixmap->samples + offset
1015 GLenum err = glGetError ();
1016 if (err != GL_NO_ERROR) {
1017 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w, slice->h, err);
1018 abort ();
1022 else {
1023 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
1025 GL_RGBA8,
1027 slice->h,
1029 state.texform,
1030 state.texty,
1031 page->pixmap->samples + offset
1035 end = now ();
1036 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1037 subimage ? "sub" : "img",
1038 page->pageno, slicenum,
1039 slice->w, slice->h,
1040 state.texids[slice->texindex],
1041 end - start);
1045 CAMLprim value ml_draw (value dispy_v, value w_v, value h_v,
1046 value py_v, value ptr_v)
1048 CAMLparam5 (dispy_v, w_v, h_v, py_v, ptr_v);
1049 int dispy = Int_val (dispy_v);
1050 int w = Int_val (w_v);
1051 int h = Int_val (h_v);
1052 int py = Int_val (py_v);
1053 char *s = String_val (ptr_v);
1054 int ret;
1055 void *ptr;
1056 struct page *page;
1057 int slicenum = 0;
1059 if (trylock ("ml_draw")) {
1060 goto done;
1063 ret = sscanf (s, "%p", &ptr);
1064 if (ret != 1) {
1065 errx (1, "cannot parse pointer `%s'", s);
1067 page = ptr;
1069 w = page->pixmap->w;
1071 ARSERT (h >= 0 && "ml_draw wrong h");
1073 glEnable (GL_TEXTURE_RECTANGLE_ARB);
1075 for (slicenum = 0; slicenum < page->slicecount; ++slicenum) {
1076 struct slice *slice = &page->slices[slicenum];
1077 if (slice->h > py) {
1078 break;
1080 py -= slice->h;
1083 h = MIN (state.h, h);
1084 while (h) {
1085 int th;
1086 struct slice *slice = &page->slices[slicenum];
1088 ARSERT (slicenum < page->slicecount && "ml_draw wrong slicenum");
1090 th = MIN (h, slice->h - py);
1091 upload2 (page, slicenum, "upload");
1093 glBegin (GL_QUADS);
1095 glTexCoord2i (0, py);
1096 glVertex2i (0, dispy);
1098 glTexCoord2i (w, py);
1099 glVertex2i (w, dispy);
1101 glTexCoord2i (w, py+th);
1102 glVertex2i (w, dispy + th);
1104 glTexCoord2i (0, py+th);
1105 glVertex2i (0, dispy + th);
1107 glEnd ();
1109 h -= th;
1110 py = 0;
1111 dispy += th;
1112 slicenum += 1;
1115 glDisable (GL_TEXTURE_RECTANGLE_ARB);
1117 unlock ("ml_draw");
1118 done:
1119 CAMLreturn (Val_unit);
1122 static pdf_link *getlink (struct page *page, int x, int y)
1124 fz_point p;
1125 fz_matrix ctm;
1126 pdf_link *link;
1128 p.x = x + page->pixmap->x;
1129 p.y = y + page->pixmap->y;
1131 ctm = fz_invertmatrix (page->pagedim->ctm);
1132 p = fz_transformpoint (ctm, p);
1134 for (link = page->drawpage->links; link; link = link->next) {
1135 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
1136 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
1137 if (link->kind == PDF_LGOTO) {
1138 return link;
1143 return NULL;
1146 CAMLprim value ml_checklink (value ptr_v, value x_v, value y_v)
1148 CAMLparam3 (ptr_v, x_v, y_v);
1149 char *s = String_val (ptr_v);
1150 int ret;
1152 if (trylock ("ml_checklink")) {
1153 ret = 0;
1155 else {
1156 ret = NULL != getlink (parse_pointer ("ml_checklink", s),
1157 Int_val (x_v), Int_val (y_v));
1158 unlock ("ml_checklink");
1160 CAMLreturn (Val_bool (ret));
1163 CAMLprim value ml_getlink (value ptr_v, value x_v, value y_v)
1165 CAMLparam3 (ptr_v, x_v, y_v);
1166 CAMLlocal2 (ret_v, tup_v);
1167 pdf_link *link;
1168 struct page *page;
1169 char *s = String_val (ptr_v);
1171 if (trylock ("ml_getlink")) {
1172 ret_v = Val_int (0);
1173 goto done;
1176 page = parse_pointer ("ml_getlink", s);
1178 link = getlink (page, Int_val (x_v), Int_val (y_v));
1179 if (link) {
1180 int pageno;
1181 fz_point p;
1182 fz_obj *obj;
1184 pageno = -1;
1185 obj = fz_arrayget (link->dest, 0);
1186 if (fz_isindirect (obj)) {
1187 pageno = pdf_findpageobject (state.xref, obj) - 1;
1189 else if (fz_isint (obj)) {
1190 pageno = fz_toint (obj);
1193 if (fz_arraylen (link->dest) > 3) {
1194 p.x = fz_toint (fz_arrayget (link->dest, 2));
1195 p.y = fz_toint (fz_arrayget (link->dest, 3));
1196 p = fz_transformpoint (page->pagedim->ctm, p);
1198 else {
1199 p.x = 0.0;
1200 p.y = 0.0;
1203 tup_v = caml_alloc_tuple (2);
1204 ret_v = caml_alloc_small (1, 1);
1205 Field (tup_v, 0) = Val_int (pageno);
1206 Field (tup_v, 1) = Val_int (p.y);
1207 Field (ret_v, 0) = tup_v;
1209 else {
1210 ret_v = Val_int (0);
1212 unlock ("ml_getlink");
1214 done:
1215 CAMLreturn (ret_v);
1218 CAMLprim value ml_gettext (value ptr_v, value rect_v, value oy_v, value rectsel_v)
1220 CAMLparam4 (ptr_v, rect_v, oy_v, rect_v);
1221 fz_matrix ctm;
1222 fz_point p1, p2;
1223 struct page *page;
1224 fz_textspan *span;
1225 char *s = String_val (ptr_v);
1226 int rectsel = Bool_val (rectsel_v);
1227 int i, bx0, bx1, by0, by1, x0, x1, y0, y1, oy;
1229 /* stop GCC from complaining about uninitialized variables */
1230 #ifdef __GNUC__
1231 int rx0 = rx0, rx1 = rx1, ry0 = ry0, ry1 = ry1;
1232 #else
1233 int rx0, rx1, ry0, ry1;
1234 #endif
1236 if (trylock ("ml_gettext")) {
1237 goto done;
1240 page = parse_pointer ("ml_gettext", s);
1242 oy = Int_val (oy_v);
1243 p1.x = Int_val (Field (rect_v, 0));
1244 p1.y = Int_val (Field (rect_v, 1));
1245 p2.x = Int_val (Field (rect_v, 2));
1246 p2.y = Int_val (Field (rect_v, 3));
1248 if (0) {
1249 glEnable (GL_BLEND);
1250 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1251 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1252 glColor4f (0.0f, 0.0f, 0.0f, 0.2f);
1253 glRecti (p1.x, p1.y, p2.x, p2.y);
1254 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1255 glDisable (GL_BLEND);
1258 ctm = page->pagedim->ctm;
1259 if (!page->text) {
1260 fz_error error;
1261 fz_device *tdev;
1263 page->text = fz_newtextspan ();
1264 tdev = fz_newtextdevice (page->text);
1265 error = pdf_runcontentstream (tdev, page->pagedim->ctm, state.xref,
1266 page->drawpage->resources,
1267 page->drawpage->contents);
1268 if (error) die (error);
1269 fz_freedevice (tdev);
1272 printf ("\033c");
1274 printf ("BBox %f %f %f %f\n", p1.x, p1.y, p2.x, p2.y);
1275 p1.x += page->pixmap->x;
1276 p1.y += page->pixmap->y;
1277 p2.x += page->pixmap->x;
1278 p2.y += page->pixmap->y;
1279 x0 = p1.x;
1280 y0 = p1.y;
1281 x1 = p2.x;
1282 y1 = p2.y;
1283 printf ("BBox %d %d %d %d %d %d\n", x0, y0, x1, y1, oy, page->pageno);
1285 for (span = page->text; span; span = span->next) {
1286 int seen = 0;
1288 /* fz_debugtextspanxml (span); */
1289 for (i = 0; i < span->len; ++i) {
1290 long c;
1292 bx0 = span->text[i].bbox.x0;
1293 bx1 = span->text[i].bbox.x1;
1294 by0 = span->text[i].bbox.y0 + oy;
1295 by1 = span->text[i].bbox.y1 + oy;
1297 if ((bx1 >= x0 && bx0 <= x1 && by1 >= y0 && by0 <= y1)) {
1298 if (!seen) {
1299 rx0 = bx0 - page->pixmap->x;
1300 rx1 = bx1 - page->pixmap->x;
1301 ry0 = by0;
1302 ry1 = by1;
1305 seen = 1;
1306 c = span->text[i].c;
1307 if (c < 256) {
1308 if ((isprint (c) && !isspace (c))) {
1309 if (!rectsel) {
1310 bx0 -= page->pixmap->x;
1311 bx1 -= page->pixmap->x;
1312 glEnable (GL_BLEND);
1313 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1314 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1315 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1316 glRecti (bx0, by0, bx1, by1);
1317 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1318 glDisable (GL_BLEND);
1320 if (isprint (c) || c ==' ') {
1321 rx1 = bx1;
1322 ry1 = by1;
1325 putc (c, stdout);
1327 else {
1328 putc ('?', stdout);
1333 if (rectsel) {
1334 if (seen) {
1335 glEnable (GL_BLEND);
1336 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1337 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1338 glColor4f (0.5f, 0.5f, 0.0f, 0.6f);
1339 glRecti (rx0, ry0, rx1, ry1);
1340 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1341 glDisable (GL_BLEND);
1345 if (seen && span->eol) {
1346 x0 = page->pixmap->x;
1347 putc ('\n', stdout);
1350 unlock ("ml_gettext");
1352 done:
1353 CAMLreturn (Val_unit);
1356 CAMLprim value ml_getpagewh (value pagedimno_v)
1358 CAMLparam1 (pagedimno_v);
1359 CAMLlocal1 (ret_v);
1360 int pagedimno = Int_val (pagedimno_v);
1362 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
1363 Store_double_field (ret_v, 0, state.pagedims[pagedimno].box.x0);
1364 Store_double_field (ret_v, 1, state.pagedims[pagedimno].box.x1);
1365 Store_double_field (ret_v, 2, state.pagedims[pagedimno].box.y0);
1366 Store_double_field (ret_v, 3, state.pagedims[pagedimno].box.y1);
1367 CAMLreturn (ret_v);
1370 CAMLprim value ml_init (value sock_v)
1372 #ifndef _WIN32
1373 int ret;
1374 #endif
1375 CAMLparam1 (sock_v);
1377 state.texcount = 256;
1378 state.sliceheight = 24;
1379 state.texform = GL_RGBA;
1380 state.texty = GL_UNSIGNED_BYTE;
1382 state.texids = calloc (state.texcount * sizeof (*state.texids), 1);
1383 if (!state.texids) {
1384 err (1, "calloc texids %zu", state.texcount * sizeof (*state.texids));
1387 state.texowners = calloc (state.texcount * sizeof (*state.texowners), 1);
1388 if (!state.texowners) {
1389 err (1, "calloc texowners %zu",
1390 state.texcount * sizeof (*state.texowners));
1393 glGenTextures (state.texcount, state.texids);
1395 #ifdef _WIN32
1396 state.sock = Socket_val (sock_v);
1397 #else
1398 state.sock = Int_val (sock_v);
1399 #endif
1401 state.cache = fz_newglyphcache ();
1402 if (!state.cache) {
1403 errx (1, "fz_newglyphcache failed");
1406 #ifdef _WIN32
1407 InitializeCriticalSection (&critsec);
1408 state.thread = CreateThread (NULL, 0, mainloop, NULL, 0, NULL);
1409 if (state.thread == INVALID_HANDLE_VALUE) {
1410 errx (1, "CreateThread failed: %lx", GetLastError ());
1412 #else
1413 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
1414 if (ret) {
1415 errx (1, "pthread_create: %s", strerror (errno));
1417 #endif
1419 CAMLreturn (Val_unit);