1 /* lot's of code c&p-ed directly from mupdf */
3 #define WIN32_LEAN_AND_MEAN
7 #pragma warning (disable:4244)
8 #pragma warning (disable:4996)
9 #pragma warning (disable:4995)
17 static void __declspec (noreturn
) err (int exitcode
, const char *fmt
, ...)
24 vfprintf (stderr
, fmt
, ap
);
26 fprintf (stderr
, ": %s\n", strerror (errno
));
29 static void __declspec (noreturn
) errx (int exitcode
, const char *fmt
, ...)
36 vfprintf (stderr
, fmt
, ap
);
41 static void __declspec (noreturn
) sockerr (int exitcode
, const char *fmt
, ...)
48 vfprintf (stderr
, fmt
, ap
);
50 fprintf (stderr
, ": wsaerror %lx\n", WSAGetLastError ());
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <sys/select.h>
75 #include <OpenGL/gl.h>
80 #ifndef GL_TEXTURE_RECTANGLE_ARB
81 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
84 #include <caml/fail.h>
85 #include <caml/alloc.h>
86 #include <caml/memory.h>
87 #include <caml/unixsupport.h>
93 #define lprintf printf
98 #define ARSERT(cond) for (;;) { \
100 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
116 struct pagedim
*pagedim
;
118 struct slice slices
[];
133 struct pagedim
*pagedims
;
137 fz_glyphcache
*cache
;
165 static CRITICAL_SECTION critsec
;
167 static void lock (void *unused
)
170 EnterCriticalSection (&critsec
);
173 static void unlock (void *unused
)
176 LeaveCriticalSection (&critsec
);
179 static int trylock (void *unused
)
181 return TryEnterCriticalSection (&critsec
) == 0;
184 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
186 static void lock (const char *cap
)
188 int ret
= pthread_mutex_lock (&mutex
);
190 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
194 static void unlock (const char *cap
)
196 int ret
= pthread_mutex_unlock (&mutex
);
198 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
202 static int trylock (const char *cap
)
204 int ret
= pthread_mutex_trylock (&mutex
);
206 if (ret
&& ret
!= EBUSY
) {
207 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
213 static void *parse_pointer (const char *cap
, const char *s
)
218 ret
= sscanf (s
, "%p", &ptr
);
220 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
225 static int hasdata (int sock
)
234 n
= select (sock
+ 1, &s
, NULL
, NULL
, &tv
);
235 if (n
== 0) return 0;
236 if (n
== 1) return 1;
237 sockerr (1, "hasdata: select error ret=%d", n
);
240 static double now (void)
244 if (gettimeofday (&tv
, NULL
)) {
245 err (1, "gettimeofday");
247 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
250 static void readdata (int fd
, char *p
, int size
)
254 n
= recv (fd
, p
, size
, 0);
256 if (!n
) errx (1, "EOF while reading");
257 sockerr (1, "recv (req %d, ret %zd)", size
, n
);
261 static void writedata (int fd
, char *p
, int size
)
266 buf
[0] = (size
>> 24) & 0xff;
267 buf
[1] = (size
>> 16) & 0xff;
268 buf
[2] = (size
>> 8) & 0xff;
269 buf
[3] = (size
>> 0) & 0xff;
271 n
= send (fd
, buf
, 4, 0);
273 if (!n
) errx (1, "EOF while writing length");
274 sockerr (1, "send %zd", n
);
277 n
= send (fd
, p
, size
, 0);
279 if (!n
) errx (1, "EOF while writing data");
280 sockerr (1, "send (req %d, ret %zd)", size
, n
);
286 __attribute__ ((format (printf
, 2, 3)))
288 printd (int fd
, const char *fmt
, ...)
295 len
= vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
297 writedata (fd
, buf
, len
);
300 static void die (fz_error error
)
302 fz_catch (error
, "aborting");
304 pdf_freexref (state
.xref
);
308 static void openxref (char *filename
)
314 fd
= open (filename
, O_BINARY
| O_RDONLY
, 0666);
316 die (fz_throw ("cannot open file '%s'", filename
));
318 file
= fz_openfile (fd
);
319 error
= pdf_openxrefwithstream (&state
.xref
, file
, NULL
);
321 die (fz_rethrow(error
, "cannot open document '%s'", filename
));
322 fz_dropstream (file
);
324 if (pdf_needspassword (state
.xref
)) {
325 die (fz_throw ("password protected"));
328 error
= pdf_loadpagetree (state
.xref
);
330 die (fz_throw ("cannot load page tree"));
333 state
.pagecount
= pdf_getpagecount (state
.xref
);
334 state
.pagetbl
= stat_alloc (state
.pagecount
* sizeof (*state
.pagetbl
));
337 static int readlen (int fd
)
342 n
= recv (fd
, p
, 4, 0);
344 if (!n
) errx (1, "EOF while reading length");
345 sockerr (1, "recv %zd", n
);
348 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
351 static void freepage (struct page
*page
)
356 fz_droppixmap (page
->pixmap
);
357 for (p
= state
.pages
; p
; p
= p
->prev
) {
358 if (p
->prev
== page
) {
359 p
->prev
= page
->prev
;
363 for (i
= 0; i
< page
->slicecount
; ++i
) {
364 struct slice
*s
= &page
->slices
[i
];
365 if (s
->texindex
!= -1) {
366 if (state
.texowners
[s
->texindex
].slice
== s
) {
367 state
.texowners
[s
->texindex
].slice
= NULL
;
368 ARSERT (state
.texowners
[s
->texindex
].w
== s
->w
);
369 ARSERT (state
.texowners
[s
->texindex
].h
>= s
->h
);
374 fz_freetextspan (page
->text
);
376 if (page
->drawpage
) {
377 pdf_freepage (page
->drawpage
);
383 static void subdivide (struct page
*p
)
386 int h
= p
->pixmap
->h
;
387 int th
= MIN (h
, state
.sliceheight
);
389 for (i
= 0; i
< p
->slicecount
; ++i
) {
390 struct slice
*s
= &p
->slices
[i
];
398 static void *render (int pageno
, int pindex
)
407 struct pagedim
*pagedim
;
410 printd (state
.sock
, "V rendering %d", pageno
);
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);
420 err (1, "calloc page %d\n", pageno
);
422 page
->slicecount
= slicecount
;
423 page
->prev
= state
.pages
;
426 pageobj
= pdf_getpageobject (state
.xref
, pageno
);
428 die (fz_throw ("cannot retrieve info from page %d", pageno
));
430 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
434 page
->pixmap
= fz_newpixmapwithrect (fz_devicergb
, pagedim
->bbox
);
435 fz_clearpixmap (page
->pixmap
, 0xFF);
437 idev
= fz_newdrawdevice (state
.cache
, page
->pixmap
);
439 die (fz_throw ("fz_newdrawdevice failed"));
440 error
= pdf_runpage (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
441 fz_freedevice (idev
);
443 page
->drawpage
= drawpage
;
444 page
->pagedim
= pagedim
;
445 page
->pageno
= pageno
;
449 if (!state
.lotsamemory
) {
450 pdf_agestore(state
.xref
->store
, 3);
453 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
457 static void initpdims (void)
463 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
467 fz_obj
*obj
, *pageobj
;
469 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
471 obj
= fz_dictgets (pageobj
, "CropBox");
472 if (!fz_isarray (obj
)) {
473 obj
= fz_dictgets (pageobj
, "MediaBox");
474 if (!fz_isarray (obj
)) {
475 die (fz_throw ("cannot find page bounds %d (%d Rd)",
476 fz_tonum (pageobj
), fz_togen (pageobj
)));
479 box
= pdf_torect (obj
);
481 obj
= fz_dictgets (pageobj
, "Rotate");
482 if (fz_isint (obj
)) {
483 rotate
= fz_toint (obj
);
488 rotate
+= state
.rotate
;
490 state
.pagetbl
[pageno
] = fz_tonum (state
.xref
->pagerefs
[pageno
]);
491 p
= &state
.pagedims
[state
.pagedimcount
- 1];
492 if ((state
.pagedimcount
== 0)
493 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
496 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
497 state
.pagedims
= realloc (state
.pagedims
, size
);
498 if (!state
.pagedims
) {
499 err (1, "realloc pagedims to %zu (%d elems)",
500 size
, state
.pagedimcount
+ 1);
502 p
= &state
.pagedims
[state
.pagedimcount
++];
509 printd (state
.sock
, "T Processed %d pages in %f seconds",
510 state
.pagecount
, end
- start
);
513 static void layout (void)
519 struct pagedim
*p
= state
.pagedims
;
522 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
523 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
524 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
525 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
526 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
529 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
530 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
531 box2
= fz_transformrect (ctm
, box
);
532 w
= box2
.x1
- box2
.x0
;
534 zoom
= (state
.w
/ w
);
536 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
537 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
538 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
539 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
540 p
->bbox
= fz_roundrect (fz_transformrect (ctm
, box
));
541 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
544 while (p
-- != state
.pagedims
) {
545 printd (state
.sock
, "l %d %d %d",
546 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
);
550 static void recurse_outline (pdf_outline
*outline
, int level
)
558 if (!outline
->link
) goto next
;
560 obj
= outline
->link
->dest
;
561 if (fz_isindirect (obj
)) {
562 num
= fz_tonum (obj
);
564 for (i
= 0; i
< state
.pagecount
; ++i
) {
565 if (state
.pagetbl
[i
] == num
) {
571 else if (fz_isarray (obj
)) {
574 obj2
= fz_arrayget (obj
, 0);
575 if (fz_isint (obj2
)) {
576 pageno
= fz_toint (obj2
);
579 num
= fz_tonum (obj2
);
580 for (i
= 0; i
< state
.pagecount
; ++i
) {
581 if (state
.pagetbl
[i
] == num
) {
588 if (fz_arraylen (obj
) > 3) {
590 struct pagedim
*pagedim
= state
.pagedims
;
592 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
593 if (state
.pagedims
[i
].pageno
> pageno
)
595 pagedim
= &state
.pagedims
[i
];
598 p
.x
= fz_toint (fz_arrayget (obj
, 2));
599 p
.y
= fz_toint (fz_arrayget (obj
, 3));
600 p
= fz_transformpoint (pagedim
->ctm
, p
);
605 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
606 printd (state
.sock
, "o %d %d %d %s",
607 level
, pageno
, top
, outline
->title
);
609 if (outline
->child
) {
610 recurse_outline (outline
->child
, level
+ 1);
612 outline
= outline
->next
;
616 static void process_outline (void)
618 pdf_outline
*outline
;
620 outline
= pdf_loadoutline (state
.xref
);
622 recurse_outline (outline
, 0);
623 pdf_freeoutline (outline
);
627 static int comparespans (const void *l
, const void *r
)
629 fz_textspan
const *const*ls
= l
;
630 fz_textspan
const *const*rs
= r
;
631 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
634 /* wishful thinking function */
635 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
646 fz_textspan
*text
, *span
, **pspan
;
647 struct pagedim
*pdim
, *pdimprev
;
654 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
656 if (!state
.lotsamemory
) {
657 pdf_agestore(state
.xref
->store
, 3);
660 if (hasdata (state
.sock
)) {
661 printd (state
.sock
, "T attention requested aborting search at %d",
666 printd (state
.sock
, "T searching in page %d", pageno
);
670 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
671 pdim
= &state
.pagedims
[i
];
672 if (pdim
->pageno
== pageno
) {
675 if (pdim
->pageno
> pageno
) {
684 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
686 die (fz_throw ("cannot retrieve info from page %d", pageno
));
688 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
692 ctm
= fz_rotate (pdim
->rotate
);
694 text
= fz_newtextspan ();
695 tdev
= fz_newtextdevice (text
);
696 error
= pdf_runpage (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
697 if (error
) die (error
);
698 fz_freedevice (tdev
);
701 for (span
= text
; span
; span
= span
->next
) {
704 pspan
= malloc (sizeof (void *) * nspans
);
706 err (1, "malloc span pointers %zu", sizeof (void *) * nspans
);
708 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
711 qsort (pspan
, nspans
, sizeof (fz_textspan
*), comparespans
);
713 j
= forward
? 0 : nspans
- 1;
718 j
+= forward
? 1 : -1;
720 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
722 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
727 if (span
->text
[i
].bbox
.y0
> y
- 1) {
731 if (span
->text
[i
].c
< 256) {
732 *p
++ = span
->text
[i
].c
;
743 ret
= regexec (re
, buf
, 1, &rm
, 0);
745 if (ret
!= REG_NOMATCH
) {
748 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
750 "T regexec error `%.*s'",
752 fz_freetextspan (text
);
753 pdf_freepage (drawpage
);
761 fz_point p1
, p2
, p3
, p4
;
763 xoff
= -pdim
->bbox
.x0
;
764 yoff
= -pdim
->bbox
.y0
;
766 sb
= &span
->text
[rm
.rm_so
].bbox
;
767 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
778 p1
= fz_transformpoint (ctm
, p1
);
779 p2
= fz_transformpoint (ctm
, p2
);
780 p3
= fz_transformpoint (ctm
, p3
);
781 p4
= fz_transformpoint (ctm
, p4
);
784 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
786 p1
.x
+ xoff
, p1
.y
+ yoff
,
787 p2
.x
+ xoff
, p2
.y
+ yoff
,
788 p3
.x
+ xoff
, p3
.y
+ yoff
,
789 p4
.x
+ xoff
, p4
.y
+ yoff
);
791 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
792 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
796 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
798 p1
.x
+ xoff
, p1
.y
+ yoff
,
799 p2
.x
+ xoff
, p2
.y
+ yoff
,
800 p3
.x
+ xoff
, p3
.y
+ yoff
,
801 p4
.x
+ xoff
, p4
.y
+ yoff
);
814 fz_freetextspan (text
);
815 pdf_freepage (drawpage
);
820 printd (state
.sock
, "T no matches %f sec", end
- start
);
822 printd (state
.sock
, "D");
831 mainloop (void *unused
)
834 int len
, ret
, oldlen
= 0;
837 len
= readlen (state
.sock
);
839 errx (1, "readlen returned 0");
842 if (oldlen
< len
+ 1) {
843 p
= realloc (p
, len
+ 1);
845 err (1, "realloc %d failed", len
+ 1);
849 readdata (state
.sock
, p
, len
);
852 if (!strncmp ("open", p
, 4)) {
854 char *filename
= p
+ 5;
859 obj
= fz_dictgets (state
.xref
->trailer
, "Info");
861 obj
= fz_dictgets (obj
, "Title");
863 printd (state
.sock
, "t %s", pdf_toutf8 (obj
));
867 else if (!strncmp ("free", p
, 4)) {
870 ret
= sscanf (p
+ 4, " %p", &ptr
);
872 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
877 printd (state
.sock
, "d");
879 else if (!strncmp ("search", p
, 6)) {
880 int icase
, pageno
, y
, ret
, len2
, forward
;
884 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
885 &icase
, &pageno
, &y
, &forward
, &len2
);
887 errx (1, "malformed search `%s' ret=%d", p
, ret
);
890 pattern
= p
+ 6 + len2
;
891 ret
= regcomp (&re
, pattern
,
892 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
897 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
898 printd (state
.sock
, "T regcomp failed `%.*s'",
902 search (&re
, pageno
, y
, forward
);
906 else if (!strncmp ("geometry", p
, 8)) {
909 printd (state
.sock
, "c");
910 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
912 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
918 for (i
= 0; i
< state
.texcount
; ++i
) {
919 state
.texowners
[i
].slice
= NULL
;
926 printd (state
.sock
, "C %d", state
.pagecount
);
928 else if (!strncmp ("rotate", p
, 6)) {
931 printd (state
.sock
, "c");
932 ret
= sscanf (p
+ 6, " %f", &rotate
);
934 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
936 state
.rotate
= rotate
;
938 state
.pagedimcount
= 0;
939 free (state
.pagedims
);
940 state
.pagedims
= NULL
;
945 printd (state
.sock
, "C %d", state
.pagecount
);
947 else if (!strncmp ("render", p
, 6)) {
948 int pageno
, pindex
, w
, h
, ret
;
951 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
953 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
956 page
= render (pageno
, pindex
);
957 printd (state
.sock
, "r %d %d %d %d %p\n",
964 else if (!strncmp ("interrupt", p
, 8)) {
965 printd (state
.sock
, "V interrupted");
968 errx (1, "unknown command %.*s", len
, p
);
974 static void upload2 (struct page
*page
, int slicenum
, const char *cap
)
979 struct slice
*slice
= &page
->slices
[slicenum
];
984 ARSERT (w
== slice
->w
);
985 if (slice
->texindex
!= -1
986 && state
.texowners
[slice
->texindex
].slice
== slice
) {
987 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
991 int index
= (state
.texindex
++ % state
.texcount
);
994 for (i
= 0; i
< slicenum
; ++i
) {
995 offset
+= w
* page
->slices
[i
].h
* 4;
998 if (state
.texowners
[index
].w
== slice
->w
) {
999 if (state
.texowners
[index
].h
>= slice
->h
) {
1003 state
.texowners
[index
].h
= slice
->h
;
1007 state
.texowners
[index
].h
= slice
->h
;
1010 state
.texowners
[index
].slice
= slice
;
1011 state
.texowners
[index
].w
= slice
->w
;
1012 slice
->texindex
= index
;
1014 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1018 GLenum err
= glGetError ();
1019 if (err
!= GL_NO_ERROR
) {
1020 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1024 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1032 page
->pixmap
->samples
+ offset
1035 GLenum err
= glGetError ();
1036 if (err
!= GL_NO_ERROR
) {
1037 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1043 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1051 page
->pixmap
->samples
+ offset
1056 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1057 subimage
? "sub" : "img",
1058 page
->pageno
, slicenum
,
1060 state
.texids
[slice
->texindex
],
1065 CAMLprim value
ml_draw (value dispy_v
, value w_v
, value h_v
,
1066 value py_v
, value ptr_v
)
1068 CAMLparam5 (dispy_v
, w_v
, h_v
, py_v
, ptr_v
);
1069 int dispy
= Int_val (dispy_v
);
1070 int w
= Int_val (w_v
);
1071 int h
= Int_val (h_v
);
1072 int py
= Int_val (py_v
);
1073 char *s
= String_val (ptr_v
);
1079 if (trylock ("ml_draw")) {
1083 ret
= sscanf (s
, "%p", &ptr
);
1085 errx (1, "cannot parse pointer `%s'", s
);
1089 w
= page
->pixmap
->w
;
1091 ARSERT (h
>= 0 && "ml_draw wrong h");
1092 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1094 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1096 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1097 struct slice
*slice
= &page
->slices
[slicenum
];
1098 if (slice
->h
> py
) {
1104 h
= MIN (state
.h
, h
);
1107 struct slice
*slice
= &page
->slices
[slicenum
];
1109 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1111 th
= MIN (h
, slice
->h
- py
);
1112 upload2 (page
, slicenum
, "upload");
1116 glTexCoord2i (0, py
);
1117 glVertex2i (0, dispy
);
1119 glTexCoord2i (w
, py
);
1120 glVertex2i (w
, dispy
);
1122 glTexCoord2i (w
, py
+th
);
1123 glVertex2i (w
, dispy
+ th
);
1125 glTexCoord2i (0, py
+th
);
1126 glVertex2i (0, dispy
+ th
);
1136 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1140 CAMLreturn (Val_unit
);
1143 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1149 p
.x
= x
+ page
->pixmap
->x
;
1150 p
.y
= y
+ page
->pixmap
->y
;
1152 ctm
= fz_invertmatrix (page
->pagedim
->ctm
);
1153 p
= fz_transformpoint (ctm
, p
);
1155 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1156 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1157 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1165 CAMLprim value
ml_highlightlinks (value ptr_v
, value yoff_v
)
1167 CAMLparam2 (ptr_v
, yoff_v
);
1170 int xoff
, yoff
= Int_val (yoff_v
);
1171 const char *s
= String_val (ptr_v
);
1173 if (trylock ("ml_highlightlinks")) {
1177 page
= parse_pointer ("ml_highlightlinks", s
);
1179 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1180 glEnable (GL_LINE_STIPPLE
);
1181 glLineStipple (0.5, 0xcccc);
1183 xoff
= -page
->pixmap
->x
;
1184 yoff
-= page
->pixmap
->y
;
1187 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1188 fz_point p1
, p2
, p3
, p4
;
1189 fz_matrix ctm
= page
->pagedim
->ctm
;
1191 p1
.x
= link
->rect
.x0
;
1192 p1
.y
= link
->rect
.y0
;
1194 p2
.x
= link
->rect
.x1
;
1195 p2
.y
= link
->rect
.y0
;
1197 p3
.x
= link
->rect
.x1
;
1198 p3
.y
= link
->rect
.y1
;
1200 p4
.x
= link
->rect
.x0
;
1201 p4
.y
= link
->rect
.y1
;
1203 p1
= fz_transformpoint (ctm
, p1
);
1204 p2
= fz_transformpoint (ctm
, p2
);
1205 p3
= fz_transformpoint (ctm
, p3
);
1206 p4
= fz_transformpoint (ctm
, p4
);
1208 switch (link
->kind
) {
1209 case PDF_LGOTO
: glColor3ub (255, 0, 0); break;
1210 case PDF_LURI
: glColor3ub (0, 0, 255); break;
1211 default: glColor3ub (0, 0, 0); break;
1214 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1215 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1216 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1217 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1221 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1222 glDisable (GL_LINE_STIPPLE
);
1223 unlock ("ml_highlightlinks");
1226 CAMLreturn (Val_unit
);
1229 CAMLprim value
ml_checklink (value ptr_v
, value x_v
, value y_v
)
1231 CAMLparam3 (ptr_v
, x_v
, y_v
);
1232 char *s
= String_val (ptr_v
);
1235 if (trylock ("ml_checklink")) {
1241 link
= getlink (parse_pointer ("ml_checklink", s
),
1242 Int_val (x_v
), Int_val (y_v
));
1248 if (link
->kind
== PDF_LURI
) {
1249 printd (state
.sock
, "T %s", fz_tostrbuf (link
->dest
));
1252 unlock ("ml_checklink");
1254 CAMLreturn (Val_bool (ret
));
1257 CAMLprim value
ml_getlink (value ptr_v
, value x_v
, value y_v
)
1259 CAMLparam3 (ptr_v
, x_v
, y_v
);
1260 CAMLlocal2 (ret_v
, tup_v
);
1263 char *s
= String_val (ptr_v
);
1265 ret_v
= Val_int (0);
1266 if (trylock ("ml_getlink")) {
1267 ret_v
= Val_int (0);
1271 page
= parse_pointer ("ml_getlink", s
);
1273 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1275 switch (link
->kind
) {
1283 obj
= fz_arrayget (link
->dest
, 0);
1284 if (fz_isindirect (obj
)) {
1285 pageno
= pdf_findpageobject (state
.xref
, obj
) - 1;
1287 else if (fz_isint (obj
)) {
1288 pageno
= fz_toint (obj
);
1291 if (fz_arraylen (link
->dest
) > 3) {
1292 p
.x
= fz_toint (fz_arrayget (link
->dest
, 2));
1293 p
.y
= fz_toint (fz_arrayget (link
->dest
, 3));
1294 p
= fz_transformpoint (page
->pagedim
->ctm
, p
);
1301 tup_v
= caml_alloc_tuple (2);
1302 ret_v
= caml_alloc_small (1, 1);
1303 Field (tup_v
, 0) = Val_int (pageno
);
1304 Field (tup_v
, 1) = Val_int (p
.y
);
1305 Field (ret_v
, 0) = tup_v
;
1310 printf ("%s\n", fz_tostrbuf (link
->dest
));
1311 tup_v
= caml_alloc_tuple (2);
1312 ret_v
= caml_alloc_small (1, 1);
1313 Field (tup_v
, 0) = Val_int (-1);
1314 Field (tup_v
, 1) = Val_int (0);
1315 Field (ret_v
, 0) = tup_v
;
1319 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1323 unlock ("ml_getlink");
1329 CAMLprim value
ml_gettext (value ptr_v
, value rect_v
, value oy_v
, value rectsel_v
)
1331 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1336 char *s
= String_val (ptr_v
);
1337 int rectsel
= Bool_val (rectsel_v
);
1338 int i
, bx0
, bx1
, by0
, by1
, x0
, x1
, y0
, y1
, oy
;
1340 /* stop GCC from complaining about uninitialized variables */
1342 int rx0
= rx0
, rx1
= rx1
, ry0
= ry0
, ry1
= ry1
;
1344 int rx0
, rx1
, ry0
, ry1
;
1347 if (trylock ("ml_gettext")) {
1351 page
= parse_pointer ("ml_gettext", s
);
1353 oy
= Int_val (oy_v
);
1354 p1
.x
= Int_val (Field (rect_v
, 0));
1355 p1
.y
= Int_val (Field (rect_v
, 1));
1356 p2
.x
= Int_val (Field (rect_v
, 2));
1357 p2
.y
= Int_val (Field (rect_v
, 3));
1360 glEnable (GL_BLEND
);
1361 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1362 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1363 glColor4f (0.0f
, 0.0f
, 0.0f
, 0.2f
);
1364 glRecti (p1
.x
, p1
.y
, p2
.x
, p2
.y
);
1365 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1366 glDisable (GL_BLEND
);
1369 ctm
= page
->pagedim
->ctm
;
1374 page
->text
= fz_newtextspan ();
1375 tdev
= fz_newtextdevice (page
->text
);
1376 error
= pdf_runpage (state
.xref
, page
->drawpage
, tdev
,
1377 page
->pagedim
->ctm
);
1378 if (error
) die (error
);
1379 fz_freedevice (tdev
);
1384 printf ("BBox %f %f %f %f\n", p1
.x
, p1
.y
, p2
.x
, p2
.y
);
1385 p1
.x
+= page
->pixmap
->x
;
1386 p1
.y
+= page
->pixmap
->y
;
1387 p2
.x
+= page
->pixmap
->x
;
1388 p2
.y
+= page
->pixmap
->y
;
1393 printf ("BBox %d %d %d %d %d %d\n", x0
, y0
, x1
, y1
, oy
, page
->pageno
);
1395 for (span
= page
->text
; span
; span
= span
->next
) {
1398 /* fz_debugtextspanxml (span); */
1399 for (i
= 0; i
< span
->len
; ++i
) {
1402 bx0
= span
->text
[i
].bbox
.x0
;
1403 bx1
= span
->text
[i
].bbox
.x1
;
1404 by0
= span
->text
[i
].bbox
.y0
+ oy
;
1405 by1
= span
->text
[i
].bbox
.y1
+ oy
;
1407 if ((bx1
>= x0
&& bx0
<= x1
&& by1
>= y0
&& by0
<= y1
)) {
1409 rx0
= bx0
- page
->pixmap
->x
;
1410 rx1
= bx1
- page
->pixmap
->x
;
1416 c
= span
->text
[i
].c
;
1418 if ((isprint (c
) && !isspace (c
))) {
1420 bx0
-= page
->pixmap
->x
;
1421 bx1
-= page
->pixmap
->x
;
1422 glEnable (GL_BLEND
);
1423 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1424 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1425 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1426 glRecti (bx0
, by0
, bx1
, by1
);
1427 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1428 glDisable (GL_BLEND
);
1430 if (isprint (c
) || c
==' ') {
1445 glEnable (GL_BLEND
);
1446 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1447 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1448 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1449 glRecti (rx0
, ry0
, rx1
, ry1
);
1450 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1451 glDisable (GL_BLEND
);
1455 if (seen
&& span
->eol
) {
1456 x0
= page
->pixmap
->x
;
1457 putc ('\n', stdout
);
1460 unlock ("ml_gettext");
1463 CAMLreturn (Val_unit
);
1466 CAMLprim value
ml_getpagewh (value pagedimno_v
)
1468 CAMLparam1 (pagedimno_v
);
1470 int pagedimno
= Int_val (pagedimno_v
);
1472 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1473 Store_double_field (ret_v
, 0, state
.pagedims
[pagedimno
].box
.x0
);
1474 Store_double_field (ret_v
, 1, state
.pagedims
[pagedimno
].box
.x1
);
1475 Store_double_field (ret_v
, 2, state
.pagedims
[pagedimno
].box
.y0
);
1476 Store_double_field (ret_v
, 3, state
.pagedims
[pagedimno
].box
.y1
);
1480 CAMLprim value
ml_init (value sock_v
)
1485 CAMLparam1 (sock_v
);
1487 state
.texcount
= 256;
1488 state
.sliceheight
= 24;
1489 state
.texform
= GL_RGBA
;
1490 state
.texty
= GL_UNSIGNED_BYTE
;
1493 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1494 if (!state
.texids
) {
1495 err (1, "calloc texids %zu", state
.texcount
* sizeof (*state
.texids
));
1498 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
1499 if (!state
.texowners
) {
1500 err (1, "calloc texowners %zu",
1501 state
.texcount
* sizeof (*state
.texowners
));
1504 glGenTextures (state
.texcount
, state
.texids
);
1507 state
.sock
= Socket_val (sock_v
);
1509 state
.sock
= Int_val (sock_v
);
1512 state
.cache
= fz_newglyphcache ();
1514 errx (1, "fz_newglyphcache failed");
1518 InitializeCriticalSection (&critsec
);
1519 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
1520 if (state
.thread
== INVALID_HANDLE_VALUE
) {
1521 errx (1, "CreateThread failed: %lx", GetLastError ());
1524 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
1526 errx (1, "pthread_create: %s", strerror (errno
));
1530 CAMLreturn (Val_unit
);