Change default slice height to make text scroll smoother on this machine
[llpp.git] / link.c
blob8ffbba6bc519dc5f4d658ec414751bdc875ea4b3
1 /* lot's of code c&p-ed directly from mupdf */
3 #define _GNU_SOURCE
4 #include <err.h>
5 #include <regex.h>
6 #include <errno.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <pthread.h>
13 #include <sys/poll.h>
14 #include <sys/time.h>
16 /* fugly as hell and GCC specific but... */
17 #ifdef _BIG_ENDIAN
18 #define GL_GLEXT_PROTOTYPES
19 #endif
21 #include <GL/gl.h>
22 #include <GL/glext.h>
24 #include <caml/fail.h>
25 #include <caml/alloc.h>
26 #include <caml/memory.h>
27 #include <caml/unixsupport.h>
29 #include <fitz.h>
30 #include <mupdf.h>
32 #if 0
33 #define lprintf printf
34 #else
35 #define lprintf(...)
36 #endif
38 #define ARSERT(cond) for (;;) { \
39 if (!(cond)) { \
40 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
41 } \
42 break; \
45 struct slice {
46 int texindex;
47 int w, h;
50 struct page {
51 int pageno;
52 int slicecount;
53 fz_textspan *text;
54 fz_pixmap *pixmap;
55 pdf_page *drawpage;
56 struct pagedim *pagedim;
57 struct page *prev;
58 struct slice slices[];
61 struct pagedim {
62 int pageno;
63 int rotate;
64 fz_rect box;
65 fz_bbox bbox;
66 fz_matrix ctm;
69 struct {
70 int sock;
71 int sliceheight;
72 pthread_t thread;
73 struct page *pages;
74 struct pagedim *pagedims;
75 int pagecount;
76 int pagedimcount;
77 pdf_xref *xref;
78 fz_glyphcache *cache;
79 int w, h;
81 int useatifs;
83 int texindex;
84 int texcount;
85 GLuint *texids;
87 GLenum texform;
88 GLenum texty;
90 int lotsamemory;
92 int *pagetbl;
93 struct {
94 int w, h;
95 struct slice *slice;
96 } *texowners;
97 } state;
99 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
101 static void lock (const char *cap)
103 int ret = pthread_mutex_lock (&mutex);
104 if (ret) {
105 errx (1, "%s: pthread_mutex_lock: %s", cap, strerror (ret));
109 static void unlock (const char *cap)
111 int ret = pthread_mutex_unlock (&mutex);
112 if (ret) {
113 errx (1, "%s: pthread_mutex_unlock: %s", cap, strerror (ret));
117 static int trylock (const char *cap)
119 int ret = pthread_mutex_trylock (&mutex);
121 if (ret && ret != EBUSY) {
122 errx (1, "%s: pthread_mutex_trylock: %s", cap, strerror (ret));
124 return ret == EBUSY;
127 static void *parse_pointer (const char *cap, const char *s)
129 int ret;
130 void *ptr;
132 ret = sscanf (s, "%p", &ptr);
133 if (ret != 1) {
134 errx (1, "%s: cannot parse pointer in `%s'", cap, s);
136 return ptr;
139 static int hasdata (int sock)
141 int ret;
142 struct pollfd pfd;
144 pfd.fd = sock;
145 pfd.events = POLLIN;
146 ret = poll (&pfd, 1, 0);
147 if (ret == 0) {
148 return 0;
150 if (ret != 1) {
151 err (1, "poll");
153 return pfd.revents & POLLIN;
156 static double now (void)
158 struct timeval tv;
160 if (gettimeofday (&tv, NULL)) {
161 err (1, "gettimeofday");
163 return tv.tv_sec + tv.tv_usec*1e-6;
166 static void readdata (int fd, char *p, int size)
168 ssize_t n;
170 n = read (fd, p, size);
171 if (n - size) {
172 err (1, "read (req %d, ret %zd)", size, n);
176 static void writedata (int fd, char *p, int size)
178 char buf[4];
179 ssize_t n;
181 buf[0] = (size >> 24) & 0xff;
182 buf[1] = (size >> 16) & 0xff;
183 buf[2] = (size >> 8) & 0xff;
184 buf[3] = (size >> 0) & 0xff;
186 n = write (fd, buf, 4);
187 if (n != 4) {
188 err (1, "write %zd", n);
191 n = write (fd, p, size);
192 if (n - size) {
193 err (1, "write (req %d, ret %zd)", size, n);
197 static void __attribute__ ((format (printf, 2, 3)))
198 printd (int fd, const char *fmt, ...)
200 int len;
201 va_list ap;
202 char buf[200];
204 va_start (ap, fmt);
205 len = vsnprintf (buf, sizeof (buf), fmt, ap);
206 va_end (ap);
207 writedata (fd, buf, len);
210 static void die (fz_error error)
212 fz_catch (error, "aborting");
213 if (state.xref)
214 pdf_closexref (state.xref);
215 exit (1);
218 static void openxref (char *filename)
220 int fd;
221 fz_error error;
222 fz_stream *file;
224 fd = open (filename, O_BINARY | O_RDONLY, 0666);
225 if (fd < 0)
226 die (fz_throw ("cannot open file '%s'", filename));
228 file = fz_openfile (fd);
229 state.xref = pdf_openxref (file);
230 if (!state.xref)
231 die (fz_throw ("cannot open PDF file '%s'", filename));
232 fz_dropstream (file);
234 if (pdf_needspassword (state.xref)) {
235 die (fz_throw ("password protected"));
238 error = pdf_loadpagetree (state.xref);
239 if (error) {
240 die (fz_throw ("cannot load page tree"));
243 state.pagecount = pdf_getpagecount (state.xref);
244 state.pagetbl = stat_alloc (state.pagecount * sizeof (*state.pagetbl));
247 static int readlen (int fd)
249 ssize_t n;
250 char p[4];
252 n = read (fd, p, 4);
253 if (n != 4) {
254 err (1, "read %zd", n);
257 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
260 static void freepage (struct page *page)
262 int i;
263 struct page *p;
265 fz_droppixmap (page->pixmap);
266 for (p = state.pages; p; p = p->prev) {
267 if (p->prev == page) {
268 p->prev = page->prev;
269 break;
272 for (i = 0; i < page->slicecount; ++i) {
273 struct slice *s = &page->slices[i];
274 if (s->texindex != -1) {
275 if (state.texowners[s->texindex].slice == s) {
276 state.texowners[s->texindex].slice = NULL;
277 ARSERT (state.texowners[s->texindex].w == s->w);
278 ARSERT (state.texowners[s->texindex].h >= s->h);
282 if (page->text) {
283 fz_freetextspan (page->text);
285 if (page->drawpage) {
286 pdf_freepage (page->drawpage);
289 free (page);
292 static void subdivide (struct page *p)
294 int i;
295 int h = p->pixmap->h;
296 int th = MIN (h, state.sliceheight);
298 for (i = 0; i < p->slicecount; ++i) {
299 struct slice *s = &p->slices[i];
300 s->texindex = -1;
301 s->h = MIN (th, h);
302 s->w = p->pixmap->w;
303 h -= th;
307 static void *render (int pageno, int pindex)
309 fz_error error;
310 int slicecount;
311 fz_obj *pageobj;
312 struct page *page;
313 double start, end;
314 pdf_page *drawpage;
315 fz_displaylist *list;
316 fz_device *idev, *mdev;
317 struct pagedim *pagedim;
319 start = now ();
320 /* printd (state.sock, "T rendering %d", pageno); */
321 pdf_flushxref (state.xref, 0);
323 pagedim = &state.pagedims[pindex];
324 slicecount = (pagedim->bbox.y1 - pagedim->bbox.y0
325 + state.sliceheight - 1) / state.sliceheight;
326 slicecount += slicecount == 0;
328 page = calloc (sizeof (*page)
329 + (slicecount * sizeof (struct slice)), 1);
330 if (!page) {
331 err (1, "calloc page %d\n", pageno);
333 page->slicecount = slicecount;
334 page->prev = state.pages;
335 state.pages = page;
337 pageobj = pdf_getpageobject (state.xref, pageno);
338 if (!pageobj)
339 die (fz_throw ("cannot retrieve info from page %d", pageno));
341 error = pdf_loadpage (&drawpage, state.xref, pageobj);
342 if (error)
343 die (error);
345 page->pixmap = fz_newpixmapwithrect (pdf_devicergb, pagedim->bbox);
346 if (error)
347 die (error);
348 fz_clearpixmap (page->pixmap, 0xFF);
350 list = fz_newdisplaylist ();
351 if (!list)
352 die (fz_throw ("fz_newdisplaylist failed"));
354 mdev = fz_newlistdevice (list);
355 error = pdf_runcontentstream (mdev, fz_identity (), state.xref,
356 drawpage->resources,
357 drawpage->contents);
358 if (error)
359 die (error);
360 fz_freedevice (mdev);
362 idev = fz_newdrawdevice (state.cache, page->pixmap);
363 if (!idev)
364 die (fz_throw ("fz_newdrawdevice failed"));
365 fz_executedisplaylist (list, idev, pagedim->ctm);
366 fz_freedevice (idev);
368 fz_freedisplaylist (list);
370 page->drawpage = drawpage;
371 page->pagedim = pagedim;
372 page->pageno = pageno;
373 subdivide (page);
374 end = now ();
376 if (!state.lotsamemory) {
377 pdf_agestoreditems (state.xref->store);
378 pdf_evictageditems (state.xref->store);
381 /* printd (state.sock, "T rendering %d took %f sec", pageno, end - start); */
382 return page;
385 static void initpdims (void)
387 int pageno;
388 double start, end;
390 start = now ();
391 for (pageno = 0; pageno < state.pagecount; ++pageno) {
392 int rotate;
393 fz_rect box;
394 struct pagedim *p;
395 fz_obj *obj, *pageobj;
397 pageobj = pdf_getpageobject (state.xref, pageno + 1);
399 obj = fz_dictgets (pageobj, "CropBox");
400 if (!fz_isarray (obj)) {
401 obj = fz_dictgets (pageobj, "MediaBox");
402 if (!fz_isarray (obj)) {
403 die (fz_throw ("cannot find page bounds %d (%d Rd)",
404 fz_tonum (pageobj), fz_togen (pageobj)));
407 box = pdf_torect (obj);
409 obj = fz_dictgets (pageobj, "Rotate");
410 if (fz_isint (obj)) {
411 rotate = fz_toint (obj);
413 else {
414 rotate = 0;
417 p = &state.pagedims[state.pagedimcount - 1];
418 if ((state.pagedimcount == 0)
419 || (p->rotate != rotate || memcmp (&p->box, &box, sizeof (box)))) {
420 size_t size;
422 size = (state.pagedimcount + 1) * sizeof (*state.pagedims);
423 state.pagedims = realloc (state.pagedims, size);
424 if (!state.pagedims) {
425 err (1, "realloc pagedims to %zu (%d elems)",
426 size, state.pagedimcount + 1);
428 p = &state.pagedims[state.pagedimcount++];
429 p->rotate = rotate;
430 p->box = box;
431 p->pageno = pageno;
434 end = now ();
435 printd (state.sock, "T Processed %d pages in %f seconds",
436 state.pagecount, end - start);
439 static void layout (void)
441 int pindex;
442 fz_matrix ctm;
443 fz_rect box, box2;
444 double zoom, w;
445 struct pagedim *p = state.pagedims;
447 pindex = 0;
448 for (pindex = 0; pindex < state.pagedimcount; ++pindex, ++p) {
449 box.x0 = MIN (p->box.x0, p->box.x1);
450 box.y0 = MIN (p->box.y0, p->box.y1);
451 box.x1 = MAX (p->box.x0, p->box.x1);
452 box.y1 = MAX (p->box.y0, p->box.y1);
454 ctm = fz_identity ();
455 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
456 ctm = fz_concat (ctm, fz_rotate (p->rotate));
457 box2 = fz_transformrect (ctm, box);
458 w = box2.x1 - box2.x0;
460 zoom = (state.w / w);
461 ctm = fz_identity ();
462 ctm = fz_concat (ctm, fz_translate (0, -box.y1));
463 ctm = fz_concat (ctm, fz_scale (zoom, -zoom));
464 ctm = fz_concat (ctm, fz_rotate (p->rotate));
465 p->bbox = fz_roundrect (fz_transformrect (ctm, box));
466 memcpy (&p->ctm, &ctm, sizeof (ctm));
469 while (p-- != state.pagedims) {
470 printd (state.sock, "l %d %d %d",
471 p->pageno, p->bbox.x1 - p->bbox.x0, p->bbox.y1 - p->bbox.y0);
475 static void recurse_outline (pdf_outline *outline, int level)
477 while (outline) {
478 int i, num;
479 fz_obj *obj;
480 int top = 0;
481 int pageno = -1;
483 if (!outline->link) goto next;
485 obj = outline->link->dest;
486 if (fz_isindirect (obj)) {
487 num = fz_tonum (obj);
489 for (i = 0; i < state.pagecount; ++i) {
490 if (state.pagetbl[i] == num) {
491 pageno = i;
492 break;
496 else if (fz_isarray (obj)) {
497 fz_obj *obj2;
499 obj2 = fz_arrayget (obj, 0);
500 if (fz_isint (obj2)) {
501 pageno = fz_toint (obj2);
503 else {
504 num = fz_tonum (obj2);
505 for (i = 0; i < state.pagecount; ++i) {
506 if (state.pagetbl[i] == num) {
507 pageno = i;
508 break;
513 if (fz_arraylen (obj) > 3) {
514 fz_point p;
515 struct pagedim *pagedim = state.pagedims;
517 for (i = 0; i < state.pagedimcount; ++i) {
518 if (state.pagedims[i].pageno > pageno)
519 break;
520 pagedim = &state.pagedims[i];
523 p.x = fz_toint (fz_arrayget (obj, 2));
524 p.y = fz_toint (fz_arrayget (obj, 3));
525 p = fz_transformpoint (pagedim->ctm, p);
526 top = p.y;
530 lprintf ("%*c%s %d\n", level, ' ', outline->title, pageno);
531 printd (state.sock, "o %d %d %d %s",
532 level, pageno, top, outline->title);
533 next:
534 if (outline->child) {
535 recurse_outline (outline->child, level + 1);
537 outline = outline->next;
541 static void process_outline (void)
543 pdf_outline *outline;
545 outline = pdf_loadoutline (state.xref);
546 if (outline) {
547 recurse_outline (outline, 0);
548 pdf_freeoutline (outline);
552 static int comparespans (const void *l, const void *r)
554 fz_textspan *const*ls = l;
555 fz_textspan *const*rs = r;
557 return (*ls)->text->bbox.y0 - (*rs)->text->bbox.y0;
560 /* wishful thinking function */
561 static void search (regex_t *re, int pageno, int y, int forward)
563 int i, j;
564 int ret;
565 char *p;
566 char buf[256];
567 fz_error error;
568 fz_obj *pageobj;
569 fz_device *tdev;
570 pdf_page *drawpage;
571 fz_textspan *text, *span, **pspan;
572 struct pagedim *pdim, *pdimprev;
573 int stop = 0;
574 int niters = 0;
575 int nspans;
576 double start, end;
578 start = now ();
579 while (pageno >= 0 && pageno < state.pagecount && !stop) {
580 if (niters++ == 5) {
581 if (!state.lotsamemory) {
582 pdf_agestoreditems (state.xref->store);
583 pdf_evictageditems (state.xref->store);
585 niters = 0;
586 if (hasdata (state.sock)) {
587 printd (state.sock, "T attention requested aborting search at %d",
588 pageno);
589 stop = 1;
591 else {
592 printd (state.sock, "T searching in page %d", pageno);
595 pdimprev = NULL;
596 for (i = 0; i < state.pagedimcount; ++i) {
597 pdim = &state.pagedims[i];
598 if (pdim->pageno == pageno) {
599 goto found;
601 if (pdim->pageno > pageno) {
602 pdim = pdimprev;
603 goto found;
605 pdimprev = pdim;
607 pdim = pdimprev;
608 found:
610 pageobj = pdf_getpageobject (state.xref, pageno + 1);
611 if (!pageobj)
612 die (fz_throw ("cannot retrieve info from page %d", pageno));
614 error = pdf_loadpage (&drawpage, state.xref, pageobj);
615 if (error)
616 die (error);
618 text = fz_newtextspan ();
619 tdev = fz_newtextdevice (text);
620 error = pdf_runcontentstream (tdev, pdim->ctm, state.xref,
621 drawpage->resources,
622 drawpage->contents);
623 if (error) die (error);
624 fz_freedevice (tdev);
626 nspans = 0;
627 for (span = text; span; span = span->next) {
628 nspans++;
630 pspan = malloc (sizeof (void *) * nspans);
631 if (!pspan) {
632 err (1, "malloc span pointers %zu", sizeof (void *) * nspans);
634 for (i = 0, span = text; span; span = span->next, ++i) {
635 pspan[i] = span;
637 qsort (pspan, nspans, sizeof (fz_textspan *), comparespans);
639 j = forward ? 0 : nspans - 1;
640 while (nspans--) {
641 regmatch_t rm;
643 span = pspan[j];
644 j += forward ? 1 : -1;
645 p = buf;
646 /* XXX: spans are not sorted "visually" */
647 for (i = 0; i < MIN (span->len, sizeof (buf) - 1); ++i) {
648 if (forward) {
649 if (span->text[i].bbox.y0 < y + 1) {
650 continue;
653 else {
654 if (span->text[i].bbox.y0 > y - 1) {
655 continue;
658 if (span->text[i].c < 256) {
659 *p++ = span->text[i].c;
661 else {
662 *p++ = '?';
665 if (p == buf) {
666 continue;
668 *p++ = 0;
670 ret = regexec (re, buf, 1, &rm, 0);
671 if (ret) {
672 if (ret != REG_NOMATCH) {
673 size_t size;
674 char errbuf[80];
675 size = regerror (ret, re, errbuf, sizeof (errbuf));
676 printd (state.sock,
677 "T regexec error `%.*s'",
678 (int) size, errbuf);
679 fz_freetextspan (text);
680 pdf_freepage (drawpage);
681 free (pspan);
682 return;
685 else {
686 fz_rect r;
688 r.x0 = span->text[rm.rm_so].bbox.x0 - pdim->bbox.x0;
689 r.y0 = span->text[rm.rm_so].bbox.y0;
690 r.x1 = span->text[rm.rm_eo - 1].bbox.x1 - pdim->bbox.x0;
691 r.y1 = span->text[rm.rm_eo - 1].bbox.y1;
693 if (!stop) {
694 printd (state.sock, "F %d %d %f %f %f %f",
695 pageno, 1,
696 r.x0, r.y0,
697 r.x1, r.y1);
699 else {
700 printd (state.sock, "R %d %d %f %f %f %f",
701 pageno, 2,
702 r.x0, r.y0,
703 r.x1, r.y1);
705 printd (state.sock, "T found at %d `%.*s' %f in %f sec",
706 pageno, rm.rm_eo - rm.rm_so, &buf[rm.rm_so],
707 span->text[0].bbox.y0 - drawpage->mediabox.y0,
708 now () - start);
709 stop = 1;
712 if (forward) {
713 pageno += 1;
714 y = 0;
716 else {
717 pageno -= 1;
718 y = INT_MAX;
720 fz_freetextspan (text);
721 pdf_freepage (drawpage);
722 free (pspan);
724 end = now ();
725 if (!stop) {
726 printd (state.sock, "T no matches %f sec", end - start);
728 printd (state.sock, "D");
731 static void *mainloop (void *unused)
733 char *p = NULL;
734 int len, ret, oldlen = 0;
736 for (;;) {
737 len = readlen (state.sock);
738 if (len == 0) {
739 errx (1, "readlen returned 0");
742 if (oldlen < len + 1) {
743 p = realloc (p, len + 1);
744 if (!p) {
745 err (1, "realloc %d failed", len + 1);
747 oldlen = len + 1;
749 readdata (state.sock, p, len);
750 p[len] = 0;
752 if (!strncmp ("open", p, 4)) {
753 fz_obj *obj;
754 char *filename = p + 5;
756 openxref (filename);
757 initpdims ();
759 obj = fz_dictgets (state.xref->trailer, "Info");
760 if (obj) {
761 obj = fz_dictgets (obj, "Title");
762 if (obj) {
763 printd (state.sock, "t %s", pdf_toutf8 (obj));
767 else if (!strncmp ("free", p, 4)) {
768 void *ptr;
770 ret = sscanf (p + 4, " %p", &ptr);
771 if (ret != 1) {
772 errx (1, "malformed free `%.*s' ret=%d", len, p, ret);
774 lock ("free");
775 freepage (ptr);
776 unlock ("free");
777 printd (state.sock, "d");
779 else if (!strncmp ("search", p, 6)) {
780 int icase, pageno, y, ret, len2, forward;
781 char *pattern;
782 regex_t re;
784 ret = sscanf (p + 6, " %d %d %d %d,%n",
785 &icase, &pageno, &y, &forward, &len2);
786 if (ret != 4) {
787 errx (1, "malformed search `%s' ret=%d", p, ret);
790 pattern = p + 6 + len2;
791 ret = regcomp (&re, pattern,
792 REG_EXTENDED | (icase ? REG_ICASE : 0));
793 if (ret) {
794 char errbuf[80];
795 size_t size;
797 size = regerror (ret, &re, errbuf, sizeof (errbuf));
798 printd (state.sock, "T regcomp failed `%.*s'",
799 (int) size, errbuf);
801 else {
802 search (&re, pageno, y, forward);
803 regfree (&re);
806 else if (!strncmp ("geometry", p, 8)) {
807 int w, h;
809 printd (state.sock, "c");
810 ret = sscanf (p + 8, " %d %d", &w, &h);
811 if (ret != 2) {
812 errx (1, "malformed geometry `%.*s' ret=%d", len, p, ret);
814 state.h = h;
815 if (w != state.w) {
816 int i;
817 state.w = w;
818 for (i = 0; i < state.texcount; ++i) {
819 state.texowners[i].slice = NULL;
822 lock ("geometry");
823 layout ();
824 process_outline ();
825 unlock ("geometry");
826 printd (state.sock, "C %d", state.pagecount);
828 else if (!strncmp ("render", p, 6)) {
829 int pageno, pindex, w, h, ret;
830 struct page *page;
832 ret = sscanf (p + 6, " %d %d %d %d", &pageno, &pindex, &w, &h);
833 if (ret != 4) {
834 errx (1, "bad render line `%.*s' ret=%d", len, p, ret);
837 page = render (pageno, pindex);
838 printd (state.sock, "r %d %d %d %p\n",
839 pageno,
840 state.w,
841 state.h,
842 page);
844 else {
845 errx (1, "unknown command %.*s", len, p);
848 return NULL;
851 static void upload2 (struct page *page, int slicenum, const char *cap)
853 int i;
854 int w, h;
855 double start, end;
856 struct slice *slice = &page->slices[slicenum];
858 w = page->pixmap->w;
859 h = page->pixmap->h;
861 ARSERT (w == slice->w);
862 if (slice->texindex != -1
863 && state.texowners[slice->texindex].slice == slice) {
864 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
866 else {
867 int subimage = 0;
868 int index = (state.texindex++ % state.texcount);
869 size_t offset = 0;
871 for (i = 0; i < slicenum; ++i) {
872 offset += w * page->slices[i].h * 4;
875 if (state.texowners[index].w == slice->w) {
876 if (state.texowners[index].h >= slice->h ) {
877 subimage = 1;
879 else {
880 state.texowners[index].h = slice->h;
883 else {
884 state.texowners[index].h = slice->h;
887 state.texowners[index].slice = slice;
888 state.texowners[index].w = slice->w;
889 slice->texindex = index;
891 glBindTexture (GL_TEXTURE_RECTANGLE_ARB, state.texids[slice->texindex]);
892 start = now ();
893 if (subimage) {
895 GLenum err = glGetError ();
896 if (err != GL_NO_ERROR) {
897 printf ("\e[0;31mERROR1 %d %d %#x\e[0m\n", w, slice->h, err);
898 abort ();
901 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB,
906 slice->h,
907 state.texform,
908 state.texty,
909 page->pixmap->samples + offset
912 GLenum err = glGetError ();
913 if (err != GL_NO_ERROR) {
914 printf ("\e[0;31mERROR %d %d %#x\e[0m\n", w, slice->h, err);
915 abort ();
919 else {
920 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB,
922 GL_RGBA8,
924 slice->h,
926 state.texform,
927 state.texty,
928 page->pixmap->samples + offset
932 end = now ();
933 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
934 subimage ? "sub" : "img",
935 page->pageno, slicenum,
936 slice->w, slice->h,
937 state.texids[slice->texindex],
938 end - start);
942 CAMLprim value ml_draw (value dispy_v, value w_v, value h_v,
943 value py_v, value ptr_v)
945 CAMLparam5 (dispy_v, w_v, h_v, py_v, ptr_v);
946 int dispy = Int_val (dispy_v);
947 int w = Int_val (w_v);
948 int h = Int_val (h_v);
949 int py = Int_val (py_v);
950 char *s = String_val (ptr_v);
951 int ret;
952 void *ptr;
953 struct page *page;
954 int slicenum = 0;
956 if (trylock ("ml_draw")) {
957 goto done;
960 ret = sscanf (s, "%p", &ptr);
961 if (ret != 1) {
962 errx (1, "cannot parse pointer `%s'", s);
964 page = ptr;
966 w = page->pixmap->w;
968 ARSERT (h >= 0 && "ml_draw wrong h");
970 glEnable (GL_TEXTURE_RECTANGLE_ARB);
971 if (state.useatifs) {
972 glEnable (GL_FRAGMENT_SHADER_ATI);
975 for (slicenum = 0; slicenum < page->slicecount; ++slicenum) {
976 struct slice *slice = &page->slices[slicenum];
977 if (slice->h > py) {
978 break;
980 py -= slice->h;
983 h = MIN (state.h, h);
984 while (h) {
985 int th;
986 struct slice *slice = &page->slices[slicenum];
988 ARSERT (slicenum < page->slicecount && "ml_draw wrong slicenum");
990 th = MIN (h, slice->h - py);
991 upload2 (page, slicenum, "upload");
993 glBegin (GL_QUADS);
995 glTexCoord2i (0, py);
996 glVertex2i (0, dispy);
998 glTexCoord2i (w, py);
999 glVertex2i (w, dispy);
1001 glTexCoord2i (w, py+th);
1002 glVertex2i (w, dispy + th);
1004 glTexCoord2i (0, py+th);
1005 glVertex2i (0, dispy + th);
1007 glEnd ();
1009 h -= th;
1010 py = 0;
1011 dispy += th;
1012 slicenum += 1;
1015 glDisable (GL_TEXTURE_RECTANGLE_ARB);
1016 if (state.useatifs) {
1017 glDisable (GL_FRAGMENT_SHADER_ATI);
1020 unlock ("ml_draw");
1021 done:
1022 CAMLreturn (Val_unit);
1025 static pdf_link *getlink (struct page *page, int x, int y)
1027 fz_point p;
1028 fz_matrix ctm;
1029 pdf_link *link;
1031 p.x = x;
1032 p.y = y;
1034 ctm = fz_invertmatrix (page->pagedim->ctm);
1035 p = fz_transformpoint (ctm, p);
1037 for (link = page->drawpage->links; link; link = link->next) {
1038 if (p.x >= link->rect.x0 && p.x <= link->rect.x1) {
1039 if (p.y >= link->rect.y0 && p.y <= link->rect.y1) {
1040 if (link->kind == PDF_LGOTO) {
1041 return link;
1046 return NULL;
1049 CAMLprim value ml_checklink (value ptr_v, value x_v, value y_v)
1051 CAMLparam3 (ptr_v, x_v, y_v);
1052 char *s = String_val (ptr_v);
1053 int ret;
1055 if (trylock ("ml_checklink")) {
1056 ret = 0;
1058 else {
1059 ret = NULL != getlink (parse_pointer ("ml_checklink", s),
1060 Int_val (x_v), Int_val (y_v));
1061 unlock ("ml_checklink");
1063 CAMLreturn (Val_bool (ret));
1066 CAMLprim value ml_getlink (value ptr_v, value x_v, value y_v)
1068 CAMLparam3 (ptr_v, x_v, y_v);
1069 CAMLlocal2 (ret_v, tup_v);
1070 pdf_link *link;
1071 struct page *page;
1072 char *s = String_val (ptr_v);
1074 if (trylock ("ml_getlink")) {
1075 ret_v = Val_int (0);
1076 goto done;
1079 page = parse_pointer ("ml_getlink", s);
1081 link = getlink (page, Int_val (x_v), Int_val (y_v));
1082 if (link) {
1083 int pageno;
1084 fz_point p;
1085 fz_obj *obj;
1087 pageno = -1;
1088 obj = fz_arrayget (link->dest, 0);
1089 if (fz_isindirect (obj)) {
1090 pageno = pdf_findpageobject (state.xref, obj) - 1;
1092 else if (fz_isint (obj)) {
1093 pageno = fz_toint (obj);
1096 if (fz_arraylen (link->dest) > 3) {
1097 p.x = fz_toint (fz_arrayget (link->dest, 2));
1098 p.y = fz_toint (fz_arrayget (link->dest, 3));
1099 p = fz_transformpoint (page->pagedim->ctm, p);
1101 else {
1102 p.x = 0.0;
1103 p.y = 0.0;
1106 tup_v = caml_alloc_tuple (2);
1107 ret_v = caml_alloc_small (1, 1);
1108 Field (tup_v, 0) = Val_int (pageno);
1109 Field (tup_v, 1) = Val_int (p.y);
1110 Field (ret_v, 0) = tup_v;
1112 else {
1113 ret_v = Val_int (0);
1115 unlock ("ml_getlink");
1117 done:
1118 CAMLreturn (ret_v);
1121 CAMLprim value ml_gettext (value ptr_v, value rect_v, value oy_v, value rectsel_v)
1123 CAMLparam4 (ptr_v, rect_v, oy_v, rect_v);
1124 fz_matrix ctm;
1125 fz_point p1, p2;
1126 struct page *page;
1127 fz_textspan *span;
1128 char *s = String_val (ptr_v);
1129 int rectsel = Bool_val (rectsel_v);
1130 int i, bx0, bx1, by0, by1, x0, x1, y0, y1, oy;
1132 /* stop GCC from complaining about uninitialized variables */
1133 int rx0 = rx0, rx1 = rx1, ry0 = ry0, ry1 = ry1;
1135 if (trylock ("ml_gettext")) {
1136 goto done;
1139 page = parse_pointer ("ml_gettext", s);
1141 oy = Int_val (oy_v);
1142 p1.x = Int_val (Field (rect_v, 0));
1143 p1.y = Int_val (Field (rect_v, 1));
1144 p2.x = Int_val (Field (rect_v, 2));
1145 p2.y = Int_val (Field (rect_v, 3));
1147 if (0) {
1148 glEnable (GL_BLEND);
1149 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1150 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1151 glColor4f (0, 0, 0, 0.2);
1152 glRecti (p1.x, p1.y, p2.x, p2.y);
1153 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1154 glDisable (GL_BLEND);
1157 ctm = page->pagedim->ctm;
1158 if (!page->text) {
1159 fz_error error;
1160 fz_device *tdev;
1162 page->text = fz_newtextspan ();
1163 tdev = fz_newtextdevice (page->text);
1164 error = pdf_runcontentstream (tdev, page->pagedim->ctm, state.xref,
1165 page->drawpage->resources,
1166 page->drawpage->contents);
1167 if (error) die (error);
1168 fz_freedevice (tdev);
1171 printf ("\ec");
1173 printf ("BBox %f %f %f %f\n", p1.x, p1.y, p2.x, p2.y);
1174 p1.x += page->pixmap->x;
1175 p1.y += page->pixmap->y;
1176 p2.x += page->pixmap->x;
1177 p2.y += page->pixmap->y;
1178 x0 = p1.x;
1179 y0 = p1.y;
1180 x1 = p2.x;
1181 y1 = p2.y;
1182 printf ("BBox %d %d %d %d %d %d\n", x0, y0, x1, y1, oy, page->pageno);
1184 for (span = page->text; span; span = span->next) {
1185 int seen = 0;
1187 /* fz_debugtextspanxml (span); */
1188 for (i = 0; i < span->len; ++i) {
1189 long c;
1191 bx0 = span->text[i].bbox.x0;
1192 bx1 = span->text[i].bbox.x1;
1193 by0 = span->text[i].bbox.y0 + oy;
1194 by1 = span->text[i].bbox.y1 + oy;
1196 if ((bx1 >= x0 && bx0 <= x1 && by1 >= y0 && by0 <= y1)) {
1197 if (!seen) {
1198 rx0 = bx0 - page->pixmap->x;
1199 rx1 = bx1 - page->pixmap->x;
1200 ry0 = by0;
1201 ry1 = by1;
1204 seen = 1;
1205 c = span->text[i].c;
1206 if (c < 256) {
1207 if ((isprint (c) && !isspace (c))) {
1208 if (!rectsel) {
1209 bx0 -= page->pixmap->x;
1210 bx1 -= page->pixmap->x;
1211 glEnable (GL_BLEND);
1212 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1213 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1214 glColor4f (0.5, 0.5, 0.0, 0.6);
1215 glRecti (bx0, by0, bx1, by1);
1216 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1217 glDisable (GL_BLEND);
1219 if (isprint (c) || c ==' ') {
1220 rx1 = bx1;
1221 ry1 = by1;
1224 putc (c, stdout);
1226 else {
1227 putc ('?', stdout);
1232 if (rectsel) {
1233 if (seen) {
1234 glEnable (GL_BLEND);
1235 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1236 glBlendFunc (GL_DST_ALPHA, GL_SRC_ALPHA);
1237 glColor4f (0.5, 0.5, 0.0, 0.6);
1238 glRecti (rx0, ry0, rx1, ry1);
1239 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1240 glDisable (GL_BLEND);
1244 if (seen && span->eol) {
1245 x0 = page->pixmap->x;
1246 putc ('\n', stdout);
1249 unlock ("ml_gettext");
1251 done:
1252 CAMLreturn (Val_unit);
1255 CAMLprim value ml_getpagewh (value pagedimno_v)
1257 CAMLparam1 (pagedimno_v);
1258 CAMLlocal1 (ret_v);
1259 int pagedimno = Int_val (pagedimno_v);
1261 ret_v = caml_alloc_small (4 * Double_wosize, Double_array_tag);
1262 Store_double_field (ret_v, 0, state.pagedims[pagedimno].box.x0);
1263 Store_double_field (ret_v, 1, state.pagedims[pagedimno].box.x1);
1264 Store_double_field (ret_v, 2, state.pagedims[pagedimno].box.y0);
1265 Store_double_field (ret_v, 3, state.pagedims[pagedimno].box.y1);
1266 CAMLreturn (ret_v);
1269 static void initgl (void)
1271 #ifdef _BIG_ENDIAN
1272 if (strstr ((char *) glGetString (GL_EXTENSIONS),
1273 "GL_ATI_fragment_shader")) {
1274 /* Here, with MESA, rv280, powerpc32: BGRA(rev) is slow while
1275 ABGR is fast, so fix things in the shader */
1276 state.texform = GL_ABGR_EXT;
1277 state.texty = GL_UNSIGNED_INT_8_8_8_8;
1279 glBindFragmentShaderATI (1);
1280 glBeginFragmentShaderATI ();
1282 glSampleMapATI (GL_REG_0_ATI, GL_TEXTURE0_ARB, GL_SWIZZLE_STR_ATI);
1284 glColorFragmentOp1ATI (GL_MOV_ATI,
1285 GL_REG_1_ATI, GL_RED_BIT_ATI, GL_NONE,
1286 GL_REG_0_ATI, GL_BLUE, GL_NONE);
1287 glColorFragmentOp1ATI (GL_MOV_ATI,
1288 GL_REG_1_ATI, GL_BLUE_BIT_ATI, GL_NONE,
1289 GL_REG_0_ATI, GL_RED, GL_NONE);
1290 glColorFragmentOp1ATI (
1291 GL_MOV_ATI,
1292 GL_REG_0_ATI, GL_RED_BIT_ATI | GL_BLUE_BIT_ATI, GL_NONE,
1293 GL_REG_1_ATI, GL_NONE, GL_NONE
1296 glEndFragmentShaderATI ();
1297 state.useatifs = 1;
1299 else {
1300 state.texform = GL_BGRA_EXT;
1301 state.texty = GL_UNSIGNED_INT_8_8_8_8_REV;
1303 #else
1304 state.texform = GL_BGRA_EXT;
1305 state.texty = GL_UNSIGNED_INT_8_8_8_8;
1306 #endif
1309 CAMLprim value ml_init (value sock_v)
1311 int ret;
1312 CAMLparam1 (sock_v);
1314 state.texcount = 256;
1315 state.sliceheight = 16;
1317 state.texids = calloc (state.texcount * sizeof (*state.texids), 1);
1318 if (!state.texids) {
1319 err (1, "calloc texids %zu", state.texcount * sizeof (*state.texids));
1322 state.texowners = calloc (state.texcount * sizeof (*state.texowners), 1);
1323 if (!state.texowners) {
1324 err (1, "calloc texowners %zu",
1325 state.texcount * sizeof (*state.texowners));
1328 glGenTextures (state.texcount, state.texids);
1330 state.sock = Int_val (sock_v);
1331 initgl ();
1333 state.cache = fz_newglyphcache ();
1334 if (!state.cache) {
1335 errx (1, "fz_newglyphcache failed");
1338 ret = pthread_create (&state.thread, NULL, mainloop, NULL);
1339 if (ret) {
1340 errx (1, "pthread_create: %s", strerror (errno));
1343 CAMLreturn (Val_unit);