1 /* lot's of code c&p-ed directly from mupdf */
3 #define WIN32_LEAN_AND_MEAN
13 #pragma warning (disable:4244)
14 #pragma warning (disable:4996)
15 #pragma warning (disable:4995)
23 static void __declspec (noreturn
) err (int exitcode
, const char *fmt
, ...)
30 vfprintf (stderr
, fmt
, ap
);
32 fprintf (stderr
, ": %s\n", strerror (errno
));
35 static void __declspec (noreturn
) errx (int exitcode
, const char *fmt
, ...)
40 vfprintf (stderr
, fmt
, ap
);
45 static void __declspec (noreturn
) sockerr (int exitcode
, const char *fmt
, ...)
50 vfprintf (stderr
, fmt
, ap
);
52 fprintf (stderr
, ": wsaerror 0x%x\n", WSAGetLastError ());
73 #include <sys/types.h>
74 #include <sys/socket.h>
75 #include <sys/select.h>
79 #include <OpenGL/gl.h>
84 #ifndef GL_TEXTURE_RECTANGLE_ARB
85 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
88 #include <caml/fail.h>
89 #include <caml/alloc.h>
90 #include <caml/memory.h>
91 #include <caml/unixsupport.h>
97 #define lprintf printf
102 #define ARSERT(cond) for (;;) { \
104 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
120 struct pagedim
*pagedim
;
126 struct slice slices
[];
137 #if !defined _WIN32 && !defined __APPLE__
145 struct pagedim
*pagedims
;
149 fz_glyphcache
*cache
;
177 static CRITICAL_SECTION critsec
;
179 static void lock (void *unused
)
182 EnterCriticalSection (&critsec
);
185 static void unlock (void *unused
)
188 LeaveCriticalSection (&critsec
);
191 static int trylock (void *unused
)
193 return TryEnterCriticalSection (&critsec
) == 0;
196 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
198 static void lock (const char *cap
)
200 int ret
= pthread_mutex_lock (&mutex
);
202 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
206 static void unlock (const char *cap
)
208 int ret
= pthread_mutex_unlock (&mutex
);
210 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
214 static int trylock (const char *cap
)
216 int ret
= pthread_mutex_trylock (&mutex
);
218 if (ret
&& ret
!= EBUSY
) {
219 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
225 static void *parse_pointer (const char *cap
, const char *s
)
230 ret
= sscanf (s
, "%p", &ptr
);
232 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
237 static int hasdata (int sock
)
246 n
= select (sock
+ 1, &s
, NULL
, NULL
, &tv
);
247 if (n
== 0) return 0;
248 if (n
== 1) return 1;
249 sockerr (1, "hasdata: select error ret=%d", n
);
252 static double now (void)
256 if (gettimeofday (&tv
, NULL
)) {
257 err (1, "gettimeofday");
259 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
262 static void readdata (int fd
, char *p
, int size
)
266 n
= recv (fd
, p
, size
, 0);
268 if (!n
) errx (1, "EOF while reading");
269 sockerr (1, "recv (req %d, ret " FMT_ss
")", size
, n
);
273 static void writedata (int fd
, char *p
, int size
)
278 buf
[0] = (size
>> 24) & 0xff;
279 buf
[1] = (size
>> 16) & 0xff;
280 buf
[2] = (size
>> 8) & 0xff;
281 buf
[3] = (size
>> 0) & 0xff;
283 n
= send (fd
, buf
, 4, 0);
285 if (!n
) errx (1, "EOF while writing length");
286 sockerr (1, "send " FMT_ss
, n
);
289 n
= send (fd
, p
, size
, 0);
291 if (!n
) errx (1, "EOF while writing data");
292 sockerr (1, "send (req %d, ret " FMT_ss
")", size
, n
);
298 __attribute__ ((format (printf
, 2, 3)))
300 printd (int fd
, const char *fmt
, ...)
307 len
= vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
309 writedata (fd
, buf
, len
);
312 static void die (fz_error error
)
314 fz_catch (error
, "aborting");
316 pdf_freexref (state
.xref
);
320 static void openxref (char *filename
)
326 fd
= open (filename
, O_BINARY
| O_RDONLY
, 0666);
328 die (fz_throw ("cannot open file '%s'", filename
));
330 file
= fz_openfile (fd
);
331 error
= pdf_openxrefwithstream (&state
.xref
, file
, NULL
);
333 die (fz_rethrow(error
, "cannot open document '%s'", filename
));
336 if (pdf_needspassword (state
.xref
)) {
337 die (fz_throw ("password protected"));
340 error
= pdf_loadpagetree (state
.xref
);
342 die (fz_throw ("cannot load page tree"));
345 state
.pagecount
= pdf_getpagecount (state
.xref
);
348 static int readlen (int fd
)
353 n
= recv (fd
, p
, 4, 0);
355 if (!n
) errx (1, "EOF while reading length");
356 sockerr (1, "recv " FMT_ss
, n
);
359 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
362 static void freepage (struct page
*page
)
367 fz_droppixmap (page
->pixmap
);
368 for (p
= state
.pages
; p
; p
= p
->prev
) {
369 if (p
->prev
== page
) {
370 p
->prev
= page
->prev
;
374 for (i
= 0; i
< page
->slicecount
; ++i
) {
375 struct slice
*s
= &page
->slices
[i
];
376 if (s
->texindex
!= -1) {
377 if (state
.texowners
[s
->texindex
].slice
== s
) {
378 state
.texowners
[s
->texindex
].slice
= NULL
;
379 ARSERT (state
.texowners
[s
->texindex
].w
== s
->w
);
380 ARSERT (state
.texowners
[s
->texindex
].h
>= s
->h
);
385 fz_freetextspan (page
->text
);
387 if (page
->drawpage
) {
388 pdf_freepage (page
->drawpage
);
394 static void subdivide (struct page
*p
)
397 int h
= p
->pixmap
->h
;
398 int th
= MIN (h
, state
.sliceheight
);
400 for (i
= 0; i
< p
->slicecount
; ++i
) {
401 struct slice
*s
= &p
->slices
[i
];
409 static void *render (int pageno
, int pindex
)
418 struct pagedim
*pagedim
;
421 printd (state
.sock
, "V rendering %d", pageno
);
423 pagedim
= &state
.pagedims
[pindex
];
424 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
425 + state
.sliceheight
- 1) / state
.sliceheight
;
426 slicecount
+= slicecount
== 0;
428 page
= calloc (sizeof (*page
)
429 + (slicecount
* sizeof (struct slice
)), 1);
431 err (1, "calloc page %d\n", pageno
);
433 page
->slicecount
= slicecount
;
434 page
->prev
= state
.pages
;
437 pageobj
= pdf_getpageobject (state
.xref
, pageno
);
439 die (fz_throw ("cannot retrieve info from page %d", pageno
));
441 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
445 page
->pixmap
= fz_newpixmapwithrect (fz_devicergb
, pagedim
->bbox
);
446 fz_clearpixmap (page
->pixmap
, 0xFF);
448 idev
= fz_newdrawdevice (state
.cache
, page
->pixmap
);
450 die (fz_throw ("fz_newdrawdevice failed"));
451 error
= pdf_runpage (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
453 die (fz_rethrow (error
, "pdf_runpage failed"));
454 fz_freedevice (idev
);
456 page
->drawpage
= drawpage
;
457 page
->pagedim
= pagedim
;
458 page
->pageno
= pageno
;
462 if (!state
.lotsamemory
) {
463 pdf_agestore(state
.xref
->store
, 3);
466 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
470 static void initpdims (void)
476 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
480 fz_obj
*obj
, *pageobj
;
482 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
484 obj
= fz_dictgets (pageobj
, "CropBox");
485 if (!fz_isarray (obj
)) {
486 obj
= fz_dictgets (pageobj
, "MediaBox");
487 if (!fz_isarray (obj
)) {
488 die (fz_throw ("cannot find page bounds %d (%d Rd)",
489 fz_tonum (pageobj
), fz_togen (pageobj
)));
492 box
= pdf_torect (obj
);
494 obj
= fz_dictgets (pageobj
, "Rotate");
495 if (fz_isint (obj
)) {
496 rotate
= fz_toint (obj
);
501 rotate
+= state
.rotate
;
503 p
= &state
.pagedims
[state
.pagedimcount
- 1];
504 if ((state
.pagedimcount
== 0)
505 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
508 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
509 state
.pagedims
= realloc (state
.pagedims
, size
);
510 if (!state
.pagedims
) {
511 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
512 size
, state
.pagedimcount
+ 1);
514 p
= &state
.pagedims
[state
.pagedimcount
++];
521 printd (state
.sock
, "T Processed %d pages in %f seconds",
522 state
.pagecount
, end
- start
);
525 static void layout (void)
531 struct pagedim
*p
= state
.pagedims
;
534 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
535 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
536 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
537 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
538 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
541 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
542 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
543 box2
= fz_transformrect (ctm
, box
);
544 w
= box2
.x1
- box2
.x0
;
546 zoom
= (state
.w
/ w
);
548 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
549 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
550 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
551 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
552 p
->bbox
= fz_roundrect (fz_transformrect (ctm
, box
));
553 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
556 while (p
-- != state
.pagedims
) {
557 printd (state
.sock
, "l %d %d %d",
558 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
);
562 static void recurse_outline (pdf_outline
*outline
, int level
)
570 if (!outline
->link
) goto next
;
572 obj
= outline
->link
->dest
;
573 if (fz_isindirect (obj
)) {
574 obj
= fz_resolveindirect (obj
);
576 if (fz_isarray (obj
)) {
579 obj2
= fz_arrayget (obj
, 0);
580 if (fz_isint (obj2
)) {
581 pageno
= fz_toint (obj2
);
584 pageno
= pdf_findpageobject (state
.xref
, obj2
) - 1;
587 if (fz_arraylen (obj
) > 3) {
589 struct pagedim
*pagedim
= state
.pagedims
;
591 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
592 if (state
.pagedims
[i
].pageno
> pageno
)
594 pagedim
= &state
.pagedims
[i
];
597 p
.x
= fz_toint (fz_arrayget (obj
, 2));
598 p
.y
= fz_toint (fz_arrayget (obj
, 3));
599 p
= fz_transformpoint (pagedim
->ctm
, p
);
604 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
605 printd (state
.sock
, "o %d %d %d %s",
606 level
, pageno
, top
, outline
->title
);
608 if (outline
->child
) {
609 recurse_outline (outline
->child
, level
+ 1);
611 outline
= outline
->next
;
615 static void process_outline (void)
617 pdf_outline
*outline
;
619 outline
= pdf_loadoutline (state
.xref
);
621 recurse_outline (outline
, 0);
622 pdf_freeoutline (outline
);
626 static int comparespans (const void *l
, const void *r
)
628 fz_textspan
const *const*ls
= l
;
629 fz_textspan
const *const*rs
= r
;
630 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
633 /* wishful thinking function */
634 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
645 fz_textspan
*text
, *span
, **pspan
;
646 struct pagedim
*pdim
, *pdimprev
;
653 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
655 if (!state
.lotsamemory
) {
656 pdf_agestore(state
.xref
->store
, 3);
659 if (hasdata (state
.sock
)) {
660 printd (state
.sock
, "T attention requested aborting search at %d",
665 printd (state
.sock
, "T searching in page %d", pageno
);
669 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
670 pdim
= &state
.pagedims
[i
];
671 if (pdim
->pageno
== pageno
) {
674 if (pdim
->pageno
> pageno
) {
683 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
685 die (fz_throw ("cannot retrieve info from page %d", pageno
));
687 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
691 ctm
= fz_rotate (pdim
->rotate
);
693 text
= fz_newtextspan ();
694 tdev
= fz_newtextdevice (text
);
695 error
= pdf_runpage (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
696 if (error
) die (error
);
697 fz_freedevice (tdev
);
700 for (span
= text
; span
; span
= span
->next
) {
703 pspan
= malloc (sizeof (void *) * nspans
);
705 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
707 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
710 qsort (pspan
, nspans
, sizeof (fz_textspan
*), comparespans
);
712 j
= forward
? 0 : nspans
- 1;
717 j
+= forward
? 1 : -1;
719 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
721 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
726 if (span
->text
[i
].bbox
.y0
> y
- 1) {
730 if (span
->text
[i
].c
< 256) {
731 *p
++ = span
->text
[i
].c
;
742 ret
= regexec (re
, buf
, 1, &rm
, 0);
744 if (ret
!= REG_NOMATCH
) {
747 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
749 "T regexec error `%.*s'",
751 fz_freetextspan (text
);
752 pdf_freepage (drawpage
);
760 fz_point p1
, p2
, p3
, p4
;
762 xoff
= -pdim
->bbox
.x0
;
763 yoff
= -pdim
->bbox
.y0
;
765 sb
= &span
->text
[rm
.rm_so
].bbox
;
766 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
777 p1
= fz_transformpoint (ctm
, p1
);
778 p2
= fz_transformpoint (ctm
, p2
);
779 p3
= fz_transformpoint (ctm
, p3
);
780 p4
= fz_transformpoint (ctm
, p4
);
783 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
785 p1
.x
+ xoff
, p1
.y
+ yoff
,
786 p2
.x
+ xoff
, p2
.y
+ yoff
,
787 p3
.x
+ xoff
, p3
.y
+ yoff
,
788 p4
.x
+ xoff
, p4
.y
+ yoff
);
790 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
791 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
795 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
797 p1
.x
+ xoff
, p1
.y
+ yoff
,
798 p2
.x
+ xoff
, p2
.y
+ yoff
,
799 p3
.x
+ xoff
, p3
.y
+ yoff
,
800 p4
.x
+ xoff
, p4
.y
+ yoff
);
813 fz_freetextspan (text
);
814 pdf_freepage (drawpage
);
819 printd (state
.sock
, "T no matches %f sec", end
- start
);
821 printd (state
.sock
, "D");
830 mainloop (void *unused
)
833 int len
, ret
, oldlen
= 0;
836 len
= readlen (state
.sock
);
838 errx (1, "readlen returned 0");
841 if (oldlen
< len
+ 1) {
842 p
= realloc (p
, len
+ 1);
844 err (1, "realloc %d failed", len
+ 1);
848 readdata (state
.sock
, p
, len
);
851 if (!strncmp ("open", p
, 4)) {
853 char *filename
= p
+ 5;
858 obj
= fz_dictgets (state
.xref
->trailer
, "Info");
860 obj
= fz_dictgets (obj
, "Title");
862 printd (state
.sock
, "t %s", pdf_toutf8 (obj
));
866 else if (!strncmp ("free", p
, 4)) {
869 ret
= sscanf (p
+ 4, " %p", &ptr
);
871 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
877 else if (!strncmp ("search", p
, 6)) {
878 int icase
, pageno
, y
, ret
, len2
, forward
;
882 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
883 &icase
, &pageno
, &y
, &forward
, &len2
);
885 errx (1, "malformed search `%s' ret=%d", p
, ret
);
888 pattern
= p
+ 6 + len2
;
889 ret
= regcomp (&re
, pattern
,
890 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
895 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
896 printd (state
.sock
, "T regcomp failed `%.*s'",
900 search (&re
, pageno
, y
, forward
);
904 else if (!strncmp ("geometry", p
, 8)) {
907 printd (state
.sock
, "c");
908 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
910 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
918 for (i
= 0; i
< state
.texcount
; ++i
) {
919 state
.texowners
[i
].slice
= NULL
;
925 printd (state
.sock
, "C %d", state
.pagecount
);
927 else if (!strncmp ("rotate", p
, 6)) {
930 printd (state
.sock
, "c");
931 ret
= sscanf (p
+ 6, " %f", &rotate
);
933 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
936 state
.rotate
= rotate
;
937 state
.pagedimcount
= 0;
938 free (state
.pagedims
);
939 state
.pagedims
= NULL
;
944 printd (state
.sock
, "C %d", state
.pagecount
);
946 else if (!strncmp ("render", p
, 6)) {
947 int pageno
, pindex
, w
, h
, ret
;
950 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
952 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
956 page
= render (pageno
, pindex
);
959 printd (state
.sock
, "r %d %d %d %d %p\n",
966 else if (!strncmp ("interrupt", p
, 8)) {
967 printd (state
.sock
, "V interrupted");
970 errx (1, "unknown command %.*s", len
, p
);
976 static void showsel (struct page
*page
, int oy
)
981 struct mark first
, last
;
986 if (!first
.span
|| !last
.span
) return;
989 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
990 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
992 ox
= -page
->pixmap
->x
;
993 oy
= -page
->pixmap
->y
+ oy
;
994 for (span
= first
.span
; span
; span
= span
->next
) {
997 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1002 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1003 j
= MIN (first
.i
, last
.i
);
1004 k
= MAX (first
.i
, last
.i
);
1006 else if (span
== first
.span
) {
1009 else if (span
== last
.span
) {
1013 for (i
= j
; i
<= k
; ++i
) {
1014 bbox
= fz_unionbbox (bbox
, span
->text
[i
].bbox
);
1016 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1023 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1025 if (span
== last
.span
) break;
1027 glDisable (GL_BLEND
);
1030 static void upload2 (struct page
*page
, int slicenum
, const char *cap
)
1035 struct slice
*slice
= &page
->slices
[slicenum
];
1037 w
= page
->pixmap
->w
;
1038 h
= page
->pixmap
->h
;
1040 ARSERT (w
== slice
->w
);
1041 if (slice
->texindex
!= -1
1042 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1043 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1047 int index
= (state
.texindex
++ % state
.texcount
);
1050 for (i
= 0; i
< slicenum
; ++i
) {
1051 offset
+= w
* page
->slices
[i
].h
* 4;
1054 if (state
.texowners
[index
].w
== slice
->w
) {
1055 if (state
.texowners
[index
].h
>= slice
->h
) {
1059 state
.texowners
[index
].h
= slice
->h
;
1063 state
.texowners
[index
].h
= slice
->h
;
1066 state
.texowners
[index
].slice
= slice
;
1067 state
.texowners
[index
].w
= slice
->w
;
1068 slice
->texindex
= index
;
1070 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1074 GLenum err
= glGetError ();
1075 if (err
!= GL_NO_ERROR
) {
1076 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1080 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1088 page
->pixmap
->samples
+ offset
1091 GLenum err
= glGetError ();
1092 if (err
!= GL_NO_ERROR
) {
1093 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1099 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1107 page
->pixmap
->samples
+ offset
1112 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1113 subimage
? "sub" : "img",
1114 page
->pageno
, slicenum
,
1116 state
.texids
[slice
->texindex
],
1121 CAMLprim value
ml_draw (value dispy_v
, value w_v
, value h_v
,
1122 value py_v
, value ptr_v
)
1124 CAMLparam5 (dispy_v
, w_v
, h_v
, py_v
, ptr_v
);
1125 int dispy
= Int_val (dispy_v
);
1126 int w
= Int_val (w_v
);
1127 int h
= Int_val (h_v
);
1128 int py
= Int_val (py_v
);
1129 char *s
= String_val (ptr_v
);
1135 ret
= sscanf (s
, "%p", &ptr
);
1137 errx (1, "cannot parse pointer `%s'", s
);
1141 w
= page
->pixmap
->w
;
1143 ARSERT (h
>= 0 && "ml_draw wrong h");
1144 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1146 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1148 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1149 struct slice
*slice
= &page
->slices
[slicenum
];
1150 if (slice
->h
> py
) {
1156 h
= MIN (state
.h
, h
);
1159 struct slice
*slice
= &page
->slices
[slicenum
];
1161 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1163 th
= MIN (h
, slice
->h
- py
);
1164 upload2 (page
, slicenum
, "upload");
1168 glTexCoord2i (0, py
);
1169 glVertex2i (0, dispy
);
1171 glTexCoord2i (w
, py
);
1172 glVertex2i (w
, dispy
);
1174 glTexCoord2i (w
, py
+th
);
1175 glVertex2i (w
, dispy
+ th
);
1177 glTexCoord2i (0, py
+th
);
1178 glVertex2i (0, dispy
+ th
);
1188 showsel (page
, Int_val (dispy_v
) - Int_val (py_v
));
1189 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1191 CAMLreturn (Val_unit
);
1194 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1200 p
.x
= x
+ page
->pixmap
->x
;
1201 p
.y
= y
+ page
->pixmap
->y
;
1203 ctm
= fz_invertmatrix (page
->pagedim
->ctm
);
1204 p
= fz_transformpoint (ctm
, p
);
1206 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1207 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1208 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1216 CAMLprim value
ml_highlightlinks (value ptr_v
, value yoff_v
)
1218 CAMLparam2 (ptr_v
, yoff_v
);
1221 int xoff
, yoff
= Int_val (yoff_v
);
1222 const char *s
= String_val (ptr_v
);
1224 if (trylock ("ml_highlightlinks")) {
1228 page
= parse_pointer ("ml_highlightlinks", s
);
1230 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1231 glEnable (GL_LINE_STIPPLE
);
1232 glLineStipple (0.5, 0xcccc);
1234 xoff
= -page
->pixmap
->x
;
1235 yoff
-= page
->pixmap
->y
;
1238 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1239 fz_point p1
, p2
, p3
, p4
;
1240 fz_matrix ctm
= page
->pagedim
->ctm
;
1242 p1
.x
= link
->rect
.x0
;
1243 p1
.y
= link
->rect
.y0
;
1245 p2
.x
= link
->rect
.x1
;
1246 p2
.y
= link
->rect
.y0
;
1248 p3
.x
= link
->rect
.x1
;
1249 p3
.y
= link
->rect
.y1
;
1251 p4
.x
= link
->rect
.x0
;
1252 p4
.y
= link
->rect
.y1
;
1254 p1
= fz_transformpoint (ctm
, p1
);
1255 p2
= fz_transformpoint (ctm
, p2
);
1256 p3
= fz_transformpoint (ctm
, p3
);
1257 p4
= fz_transformpoint (ctm
, p4
);
1259 switch (link
->kind
) {
1260 case PDF_LGOTO
: glColor3ub (255, 0, 0); break;
1261 case PDF_LURI
: glColor3ub (0, 0, 255); break;
1262 default: glColor3ub (0, 0, 0); break;
1265 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1266 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1267 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1268 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1272 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1273 glDisable (GL_LINE_STIPPLE
);
1274 unlock ("ml_highlightlinks");
1277 CAMLreturn (Val_unit
);
1280 static void ensuretext (struct page
*page
)
1286 page
->text
= fz_newtextspan ();
1287 tdev
= fz_newtextdevice (page
->text
);
1288 error
= pdf_runpage (state
.xref
, page
->drawpage
, tdev
,
1289 page
->pagedim
->ctm
);
1290 if (error
) die (error
);
1291 fz_freedevice (tdev
);
1295 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1297 CAMLparam3 (ptr_v
, x_v
, y_v
);
1298 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1301 char *s
= String_val (ptr_v
);
1303 ret_v
= Val_int (0);
1304 if (trylock ("ml_whatsunder")) {
1308 page
= parse_pointer ("ml_whatsunder", s
);
1309 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1311 switch (link
->kind
) {
1319 obj
= fz_arrayget (link
->dest
, 0);
1320 if (fz_isindirect (obj
)) {
1321 pageno
= pdf_findpageobject (state
.xref
, obj
) - 1;
1323 else if (fz_isint (obj
)) {
1324 pageno
= fz_toint (obj
);
1329 if (fz_arraylen (link
->dest
) > 3) {
1332 xo
= fz_arrayget (link
->dest
, 2);
1333 yo
= fz_arrayget (link
->dest
, 3);
1334 if (!fz_isnull (xo
) && !fz_isnull (yo
)) {
1335 p
.x
= fz_toint (xo
);
1336 p
.y
= fz_toint (yo
);
1337 p
= fz_transformpoint (page
->pagedim
->ctm
, p
);
1341 tup_v
= caml_alloc_tuple (2);
1342 ret_v
= caml_alloc_small (1, 1);
1343 Field (tup_v
, 0) = Val_int (pageno
);
1344 Field (tup_v
, 1) = Val_int (p
.y
);
1345 Field (ret_v
, 0) = tup_v
;
1350 str_v
= caml_copy_string (fz_tostrbuf (link
->dest
));
1351 ret_v
= caml_alloc_small (1, 0);
1352 Field (ret_v
, 0) = str_v
;
1356 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1365 x
= Int_val (x_v
) + page
->pixmap
->x
;
1366 y
= Int_val (y_v
) + page
->pixmap
->y
;
1368 for (span
= page
->text
; span
; span
= span
->next
) {
1369 for (i
= 0; i
< span
->len
; ++i
) {
1371 b
= &span
->text
[i
].bbox
;
1372 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1373 str_v
= caml_copy_string (span
->font
->name
);
1374 ret_v
= caml_alloc_small (1, 2);
1375 Field (ret_v
, 0) = str_v
;
1382 unlock ("ml_whatsunder");
1388 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
, value oy_v
)
1390 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1394 struct mark first
, last
;
1395 int i
, x0
, x1
, y0
, y1
, oy
;
1396 char *s
= String_val (ptr_v
);
1398 if (trylock ("ml_seltext")) {
1402 page
= parse_pointer ("ml_seltext", s
);
1405 oy
= Int_val (oy_v
);
1406 x0
= Int_val (Field (rect_v
, 0));
1407 y0
= Int_val (Field (rect_v
, 1));
1408 x1
= Int_val (Field (rect_v
, 2));
1409 y1
= Int_val (Field (rect_v
, 3));
1412 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1413 glColor3ub (128, 128, 128);
1414 glRecti (x0
, y0
, x1
, y1
);
1415 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1418 x0
+= page
->pixmap
->x
;
1419 y0
+= page
->pixmap
->y
- oy
;
1420 x1
+= page
->pixmap
->x
;
1421 y1
+= page
->pixmap
->y
- oy
;
1426 last
.i
= first
.i
= 0;
1427 first
.span
= page
->text
;
1428 for (span
= page
->text
; span
; span
= span
->next
) {
1429 for (i
= 0; i
< span
->len
; ++i
) {
1430 b
= &span
->text
[i
].bbox
;
1431 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1435 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1442 if (y1
< y0
|| x1
< x0
) {
1445 if (first
.span
== last
.span
) {
1450 for (span
= first
.span
; span
&& span
!= last
.span
;
1451 span
= span
->next
) {
1464 first
.span
= last
.span
;
1470 page
->fmark
= first
;
1473 unlock ("ml_seltext");
1476 CAMLreturn (Val_unit
);
1479 static int pipespan (FILE *f
, fz_textspan
*span
, int a
, int b
)
1484 for (i
= a
; i
<= b
; ++i
) {
1485 len
= runetochar (buf
, &span
->text
[i
].c
);
1486 ret
= fwrite (buf
, len
, 1, f
);
1489 printd (state
.sock
, "T failed to write %d bytes ret=%d: %s",
1490 len
, ret
, strerror (errno
));
1497 CAMLprim value
ml_copysel (value ptr_v
)
1502 char *s
= String_val (ptr_v
);
1504 if (trylock ("ml_copysel")) {
1511 if (state
.xselpipe
) {
1512 int ret
= pclose (state
.xselpipe
);
1514 printd (state
.sock
, "T failed to close xsel pipe `%s'",
1517 state
.xselpipe
= NULL
;
1520 printf ("========================================\n");
1526 page
= parse_pointer ("ml_sopysel", s
);
1528 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1529 printd (state
.sock
, "T nothing to copy");
1535 if (!state
.xselpipe
) {
1536 state
.xselpipe
= popen ("xsel -i", "w");
1537 if (!state
.xselpipe
) {
1538 printd (state
.sock
, "T failed to open xsel pipe `%s'",
1550 for (span
= page
->fmark
.span
;
1551 span
&& span
!= page
->lmark
.span
->next
;
1552 span
= span
->next
) {
1553 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1554 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1555 if (pipespan (f
, span
, a
, b
)) {
1559 if (putc ('\n', f
) == EOF
) {
1560 printd (state
.sock
, "T failed break line on xsel pipe `%s'",
1566 page
->lmark
.span
= NULL
;
1567 page
->fmark
.span
= NULL
;
1571 unlock ("ml_copysel");
1574 CAMLreturn (Val_unit
);
1577 CAMLprim value
ml_getpagewh (value pagedimno_v
)
1579 CAMLparam1 (pagedimno_v
);
1581 int pagedimno
= Int_val (pagedimno_v
);
1583 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1584 Store_double_field (ret_v
, 0, state
.pagedims
[pagedimno
].box
.x0
);
1585 Store_double_field (ret_v
, 1, state
.pagedims
[pagedimno
].box
.x1
);
1586 Store_double_field (ret_v
, 2, state
.pagedims
[pagedimno
].box
.y0
);
1587 Store_double_field (ret_v
, 3, state
.pagedims
[pagedimno
].box
.y1
);
1591 CAMLprim value
ml_init (value sock_v
)
1596 CAMLparam1 (sock_v
);
1598 state
.texcount
= 256;
1599 state
.sliceheight
= 24;
1600 state
.texform
= GL_RGBA
;
1601 state
.texty
= GL_UNSIGNED_BYTE
;
1604 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1605 if (!state
.texids
) {
1606 err (1, "calloc texids " FMT_s
,
1607 state
.texcount
* sizeof (*state
.texids
));
1610 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
1611 if (!state
.texowners
) {
1612 err (1, "calloc texowners " FMT_s
,
1613 state
.texcount
* sizeof (*state
.texowners
));
1616 glGenTextures (state
.texcount
, state
.texids
);
1619 state
.sock
= Socket_val (sock_v
);
1621 state
.sock
= Int_val (sock_v
);
1624 state
.cache
= fz_newglyphcache ();
1626 errx (1, "fz_newglyphcache failed");
1630 InitializeCriticalSection (&critsec
);
1631 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
1632 if (state
.thread
== INVALID_HANDLE_VALUE
) {
1633 errx (1, "CreateThread failed: %lx", GetLastError ());
1636 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
1638 errx (1, "pthread_create: %s", strerror (errno
));
1642 CAMLreturn (Val_unit
);