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
;
122 struct slice slices
[];
137 struct pagedim
*pagedims
;
141 fz_glyphcache
*cache
;
169 static CRITICAL_SECTION critsec
;
171 static void lock (void *unused
)
174 EnterCriticalSection (&critsec
);
177 static void unlock (void *unused
)
180 LeaveCriticalSection (&critsec
);
183 static int trylock (void *unused
)
185 return TryEnterCriticalSection (&critsec
) == 0;
188 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
190 static void lock (const char *cap
)
192 int ret
= pthread_mutex_lock (&mutex
);
194 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
198 static void unlock (const char *cap
)
200 int ret
= pthread_mutex_unlock (&mutex
);
202 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
206 static int trylock (const char *cap
)
208 int ret
= pthread_mutex_trylock (&mutex
);
210 if (ret
&& ret
!= EBUSY
) {
211 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
217 static void *parse_pointer (const char *cap
, const char *s
)
222 ret
= sscanf (s
, "%p", &ptr
);
224 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
229 static int hasdata (int sock
)
238 n
= select (sock
+ 1, &s
, NULL
, NULL
, &tv
);
239 if (n
== 0) return 0;
240 if (n
== 1) return 1;
241 sockerr (1, "hasdata: select error ret=%d", n
);
244 static double now (void)
248 if (gettimeofday (&tv
, NULL
)) {
249 err (1, "gettimeofday");
251 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
254 static void readdata (int fd
, char *p
, int size
)
258 n
= recv (fd
, p
, size
, 0);
260 if (!n
) errx (1, "EOF while reading");
261 sockerr (1, "recv (req %d, ret " FMT_ss
")", size
, n
);
265 static void writedata (int fd
, char *p
, int size
)
270 buf
[0] = (size
>> 24) & 0xff;
271 buf
[1] = (size
>> 16) & 0xff;
272 buf
[2] = (size
>> 8) & 0xff;
273 buf
[3] = (size
>> 0) & 0xff;
275 n
= send (fd
, buf
, 4, 0);
277 if (!n
) errx (1, "EOF while writing length");
278 sockerr (1, "send " FMT_ss
, n
);
281 n
= send (fd
, p
, size
, 0);
283 if (!n
) errx (1, "EOF while writing data");
284 sockerr (1, "send (req %d, ret " FMT_ss
")", size
, n
);
290 __attribute__ ((format (printf
, 2, 3)))
292 printd (int fd
, const char *fmt
, ...)
299 len
= vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
301 writedata (fd
, buf
, len
);
304 static void die (fz_error error
)
306 fz_catch (error
, "aborting");
308 pdf_freexref (state
.xref
);
312 static void openxref (char *filename
)
318 fd
= open (filename
, O_BINARY
| O_RDONLY
, 0666);
320 die (fz_throw ("cannot open file '%s'", filename
));
322 file
= fz_openfile (fd
);
323 error
= pdf_openxrefwithstream (&state
.xref
, file
, NULL
);
325 die (fz_rethrow(error
, "cannot open document '%s'", filename
));
326 fz_dropstream (file
);
328 if (pdf_needspassword (state
.xref
)) {
329 die (fz_throw ("password protected"));
332 error
= pdf_loadpagetree (state
.xref
);
334 die (fz_throw ("cannot load page tree"));
337 state
.pagecount
= pdf_getpagecount (state
.xref
);
338 state
.pagetbl
= stat_alloc (state
.pagecount
* sizeof (*state
.pagetbl
));
341 static int readlen (int fd
)
346 n
= recv (fd
, p
, 4, 0);
348 if (!n
) errx (1, "EOF while reading length");
349 sockerr (1, "recv " FMT_ss
, n
);
352 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
355 static void freepage (struct page
*page
)
360 fz_droppixmap (page
->pixmap
);
361 for (p
= state
.pages
; p
; p
= p
->prev
) {
362 if (p
->prev
== page
) {
363 p
->prev
= page
->prev
;
367 for (i
= 0; i
< page
->slicecount
; ++i
) {
368 struct slice
*s
= &page
->slices
[i
];
369 if (s
->texindex
!= -1) {
370 if (state
.texowners
[s
->texindex
].slice
== s
) {
371 state
.texowners
[s
->texindex
].slice
= NULL
;
372 ARSERT (state
.texowners
[s
->texindex
].w
== s
->w
);
373 ARSERT (state
.texowners
[s
->texindex
].h
>= s
->h
);
378 fz_freetextspan (page
->text
);
380 if (page
->drawpage
) {
381 pdf_freepage (page
->drawpage
);
387 static void subdivide (struct page
*p
)
390 int h
= p
->pixmap
->h
;
391 int th
= MIN (h
, state
.sliceheight
);
393 for (i
= 0; i
< p
->slicecount
; ++i
) {
394 struct slice
*s
= &p
->slices
[i
];
402 static void *render (int pageno
, int pindex
)
411 struct pagedim
*pagedim
;
414 printd (state
.sock
, "V rendering %d", pageno
);
416 pagedim
= &state
.pagedims
[pindex
];
417 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
418 + state
.sliceheight
- 1) / state
.sliceheight
;
419 slicecount
+= slicecount
== 0;
421 page
= calloc (sizeof (*page
)
422 + (slicecount
* sizeof (struct slice
)), 1);
424 err (1, "calloc page %d\n", pageno
);
426 page
->slicecount
= slicecount
;
427 page
->prev
= state
.pages
;
430 pageobj
= pdf_getpageobject (state
.xref
, pageno
);
432 die (fz_throw ("cannot retrieve info from page %d", pageno
));
434 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
438 page
->pixmap
= fz_newpixmapwithrect (fz_devicergb
, pagedim
->bbox
);
439 fz_clearpixmap (page
->pixmap
, 0xFF);
441 idev
= fz_newdrawdevice (state
.cache
, page
->pixmap
);
443 die (fz_throw ("fz_newdrawdevice failed"));
444 error
= pdf_runpage (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
445 fz_freedevice (idev
);
447 page
->drawpage
= drawpage
;
448 page
->pagedim
= pagedim
;
449 page
->pageno
= pageno
;
453 if (!state
.lotsamemory
) {
454 pdf_agestore(state
.xref
->store
, 3);
457 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
461 static void initpdims (void)
467 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
471 fz_obj
*obj
, *pageobj
;
473 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
475 obj
= fz_dictgets (pageobj
, "CropBox");
476 if (!fz_isarray (obj
)) {
477 obj
= fz_dictgets (pageobj
, "MediaBox");
478 if (!fz_isarray (obj
)) {
479 die (fz_throw ("cannot find page bounds %d (%d Rd)",
480 fz_tonum (pageobj
), fz_togen (pageobj
)));
483 box
= pdf_torect (obj
);
485 obj
= fz_dictgets (pageobj
, "Rotate");
486 if (fz_isint (obj
)) {
487 rotate
= fz_toint (obj
);
492 rotate
+= state
.rotate
;
494 state
.pagetbl
[pageno
] =
495 fz_tonum (pdf_getpageref (state
.xref
, pageno
+ 1));
497 p
= &state
.pagedims
[state
.pagedimcount
- 1];
498 if ((state
.pagedimcount
== 0)
499 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
502 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
503 state
.pagedims
= realloc (state
.pagedims
, size
);
504 if (!state
.pagedims
) {
505 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
506 size
, state
.pagedimcount
+ 1);
508 p
= &state
.pagedims
[state
.pagedimcount
++];
515 printd (state
.sock
, "T Processed %d pages in %f seconds",
516 state
.pagecount
, end
- start
);
519 static void layout (void)
525 struct pagedim
*p
= state
.pagedims
;
528 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
529 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
530 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
531 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
532 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
535 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
536 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
537 box2
= fz_transformrect (ctm
, box
);
538 w
= box2
.x1
- box2
.x0
;
540 zoom
= (state
.w
/ w
);
542 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
543 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
544 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
545 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
546 p
->bbox
= fz_roundrect (fz_transformrect (ctm
, box
));
547 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
550 while (p
-- != state
.pagedims
) {
551 printd (state
.sock
, "l %d %d %d",
552 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
);
556 static void recurse_outline (pdf_outline
*outline
, int level
)
564 if (!outline
->link
) goto next
;
566 obj
= outline
->link
->dest
;
567 if (fz_isindirect (obj
)) {
568 num
= fz_tonum (obj
);
570 for (i
= 0; i
< state
.pagecount
; ++i
) {
571 if (state
.pagetbl
[i
] == num
) {
577 else if (fz_isarray (obj
)) {
580 obj2
= fz_arrayget (obj
, 0);
581 if (fz_isint (obj2
)) {
582 pageno
= fz_toint (obj2
);
585 num
= fz_tonum (obj2
);
586 for (i
= 0; i
< state
.pagecount
; ++i
) {
587 if (state
.pagetbl
[i
] == num
) {
594 if (fz_arraylen (obj
) > 3) {
596 struct pagedim
*pagedim
= state
.pagedims
;
598 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
599 if (state
.pagedims
[i
].pageno
> pageno
)
601 pagedim
= &state
.pagedims
[i
];
604 p
.x
= fz_toint (fz_arrayget (obj
, 2));
605 p
.y
= fz_toint (fz_arrayget (obj
, 3));
606 p
= fz_transformpoint (pagedim
->ctm
, p
);
611 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
612 printd (state
.sock
, "o %d %d %d %s",
613 level
, pageno
, top
, outline
->title
);
615 if (outline
->child
) {
616 recurse_outline (outline
->child
, level
+ 1);
618 outline
= outline
->next
;
622 static void process_outline (void)
624 pdf_outline
*outline
;
626 outline
= pdf_loadoutline (state
.xref
);
628 recurse_outline (outline
, 0);
629 pdf_freeoutline (outline
);
633 static int comparespans (const void *l
, const void *r
)
635 fz_textspan
const *const*ls
= l
;
636 fz_textspan
const *const*rs
= r
;
637 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
640 /* wishful thinking function */
641 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
652 fz_textspan
*text
, *span
, **pspan
;
653 struct pagedim
*pdim
, *pdimprev
;
660 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
662 if (!state
.lotsamemory
) {
663 pdf_agestore(state
.xref
->store
, 3);
666 if (hasdata (state
.sock
)) {
667 printd (state
.sock
, "T attention requested aborting search at %d",
672 printd (state
.sock
, "T searching in page %d", pageno
);
676 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
677 pdim
= &state
.pagedims
[i
];
678 if (pdim
->pageno
== pageno
) {
681 if (pdim
->pageno
> pageno
) {
690 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
692 die (fz_throw ("cannot retrieve info from page %d", pageno
));
694 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
698 ctm
= fz_rotate (pdim
->rotate
);
700 text
= fz_newtextspan ();
701 tdev
= fz_newtextdevice (text
);
702 error
= pdf_runpage (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
703 if (error
) die (error
);
704 fz_freedevice (tdev
);
707 for (span
= text
; span
; span
= span
->next
) {
710 pspan
= malloc (sizeof (void *) * nspans
);
712 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
714 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
717 qsort (pspan
, nspans
, sizeof (fz_textspan
*), comparespans
);
719 j
= forward
? 0 : nspans
- 1;
724 j
+= forward
? 1 : -1;
726 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
728 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
733 if (span
->text
[i
].bbox
.y0
> y
- 1) {
737 if (span
->text
[i
].c
< 256) {
738 *p
++ = span
->text
[i
].c
;
749 ret
= regexec (re
, buf
, 1, &rm
, 0);
751 if (ret
!= REG_NOMATCH
) {
754 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
756 "T regexec error `%.*s'",
758 fz_freetextspan (text
);
759 pdf_freepage (drawpage
);
767 fz_point p1
, p2
, p3
, p4
;
769 xoff
= -pdim
->bbox
.x0
;
770 yoff
= -pdim
->bbox
.y0
;
772 sb
= &span
->text
[rm
.rm_so
].bbox
;
773 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
784 p1
= fz_transformpoint (ctm
, p1
);
785 p2
= fz_transformpoint (ctm
, p2
);
786 p3
= fz_transformpoint (ctm
, p3
);
787 p4
= fz_transformpoint (ctm
, p4
);
790 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
792 p1
.x
+ xoff
, p1
.y
+ yoff
,
793 p2
.x
+ xoff
, p2
.y
+ yoff
,
794 p3
.x
+ xoff
, p3
.y
+ yoff
,
795 p4
.x
+ xoff
, p4
.y
+ yoff
);
797 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
798 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
802 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
804 p1
.x
+ xoff
, p1
.y
+ yoff
,
805 p2
.x
+ xoff
, p2
.y
+ yoff
,
806 p3
.x
+ xoff
, p3
.y
+ yoff
,
807 p4
.x
+ xoff
, p4
.y
+ yoff
);
820 fz_freetextspan (text
);
821 pdf_freepage (drawpage
);
826 printd (state
.sock
, "T no matches %f sec", end
- start
);
828 printd (state
.sock
, "D");
837 mainloop (void *unused
)
840 int len
, ret
, oldlen
= 0;
843 len
= readlen (state
.sock
);
845 errx (1, "readlen returned 0");
848 if (oldlen
< len
+ 1) {
849 p
= realloc (p
, len
+ 1);
851 err (1, "realloc %d failed", len
+ 1);
855 readdata (state
.sock
, p
, len
);
858 if (!strncmp ("open", p
, 4)) {
860 char *filename
= p
+ 5;
865 obj
= fz_dictgets (state
.xref
->trailer
, "Info");
867 obj
= fz_dictgets (obj
, "Title");
869 printd (state
.sock
, "t %s", pdf_toutf8 (obj
));
873 else if (!strncmp ("free", p
, 4)) {
876 ret
= sscanf (p
+ 4, " %p", &ptr
);
878 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
883 printd (state
.sock
, "d");
885 else if (!strncmp ("search", p
, 6)) {
886 int icase
, pageno
, y
, ret
, len2
, forward
;
890 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
891 &icase
, &pageno
, &y
, &forward
, &len2
);
893 errx (1, "malformed search `%s' ret=%d", p
, ret
);
896 pattern
= p
+ 6 + len2
;
897 ret
= regcomp (&re
, pattern
,
898 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
903 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
904 printd (state
.sock
, "T regcomp failed `%.*s'",
908 search (&re
, pageno
, y
, forward
);
912 else if (!strncmp ("geometry", p
, 8)) {
915 printd (state
.sock
, "c");
916 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
918 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
924 for (i
= 0; i
< state
.texcount
; ++i
) {
925 state
.texowners
[i
].slice
= NULL
;
932 printd (state
.sock
, "C %d", state
.pagecount
);
934 else if (!strncmp ("rotate", p
, 6)) {
937 printd (state
.sock
, "c");
938 ret
= sscanf (p
+ 6, " %f", &rotate
);
940 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
942 state
.rotate
= rotate
;
944 state
.pagedimcount
= 0;
945 free (state
.pagedims
);
946 state
.pagedims
= NULL
;
951 printd (state
.sock
, "C %d", state
.pagecount
);
953 else if (!strncmp ("render", p
, 6)) {
954 int pageno
, pindex
, w
, h
, ret
;
957 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
959 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
962 page
= render (pageno
, pindex
);
963 printd (state
.sock
, "r %d %d %d %d %p\n",
970 else if (!strncmp ("interrupt", p
, 8)) {
971 printd (state
.sock
, "V interrupted");
974 errx (1, "unknown command %.*s", len
, p
);
980 static void upload2 (struct page
*page
, int slicenum
, const char *cap
)
985 struct slice
*slice
= &page
->slices
[slicenum
];
990 ARSERT (w
== slice
->w
);
991 if (slice
->texindex
!= -1
992 && state
.texowners
[slice
->texindex
].slice
== slice
) {
993 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
997 int index
= (state
.texindex
++ % state
.texcount
);
1000 for (i
= 0; i
< slicenum
; ++i
) {
1001 offset
+= w
* page
->slices
[i
].h
* 4;
1004 if (state
.texowners
[index
].w
== slice
->w
) {
1005 if (state
.texowners
[index
].h
>= slice
->h
) {
1009 state
.texowners
[index
].h
= slice
->h
;
1013 state
.texowners
[index
].h
= slice
->h
;
1016 state
.texowners
[index
].slice
= slice
;
1017 state
.texowners
[index
].w
= slice
->w
;
1018 slice
->texindex
= index
;
1020 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1024 GLenum err
= glGetError ();
1025 if (err
!= GL_NO_ERROR
) {
1026 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1030 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1038 page
->pixmap
->samples
+ offset
1041 GLenum err
= glGetError ();
1042 if (err
!= GL_NO_ERROR
) {
1043 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1049 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1057 page
->pixmap
->samples
+ offset
1062 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1063 subimage
? "sub" : "img",
1064 page
->pageno
, slicenum
,
1066 state
.texids
[slice
->texindex
],
1071 CAMLprim value
ml_draw (value dispy_v
, value w_v
, value h_v
,
1072 value py_v
, value ptr_v
)
1074 CAMLparam5 (dispy_v
, w_v
, h_v
, py_v
, ptr_v
);
1075 int dispy
= Int_val (dispy_v
);
1076 int w
= Int_val (w_v
);
1077 int h
= Int_val (h_v
);
1078 int py
= Int_val (py_v
);
1079 char *s
= String_val (ptr_v
);
1085 if (trylock ("ml_draw")) {
1089 ret
= sscanf (s
, "%p", &ptr
);
1091 errx (1, "cannot parse pointer `%s'", s
);
1095 w
= page
->pixmap
->w
;
1097 ARSERT (h
>= 0 && "ml_draw wrong h");
1098 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1100 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1102 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1103 struct slice
*slice
= &page
->slices
[slicenum
];
1104 if (slice
->h
> py
) {
1110 h
= MIN (state
.h
, h
);
1113 struct slice
*slice
= &page
->slices
[slicenum
];
1115 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1117 th
= MIN (h
, slice
->h
- py
);
1118 upload2 (page
, slicenum
, "upload");
1122 glTexCoord2i (0, py
);
1123 glVertex2i (0, dispy
);
1125 glTexCoord2i (w
, py
);
1126 glVertex2i (w
, dispy
);
1128 glTexCoord2i (w
, py
+th
);
1129 glVertex2i (w
, dispy
+ th
);
1131 glTexCoord2i (0, py
+th
);
1132 glVertex2i (0, dispy
+ th
);
1142 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1146 CAMLreturn (Val_unit
);
1149 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1155 p
.x
= x
+ page
->pixmap
->x
;
1156 p
.y
= y
+ page
->pixmap
->y
;
1158 ctm
= fz_invertmatrix (page
->pagedim
->ctm
);
1159 p
= fz_transformpoint (ctm
, p
);
1161 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1162 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1163 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1171 CAMLprim value
ml_highlightlinks (value ptr_v
, value yoff_v
)
1173 CAMLparam2 (ptr_v
, yoff_v
);
1176 int xoff
, yoff
= Int_val (yoff_v
);
1177 const char *s
= String_val (ptr_v
);
1179 if (trylock ("ml_highlightlinks")) {
1183 page
= parse_pointer ("ml_highlightlinks", s
);
1185 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1186 glEnable (GL_LINE_STIPPLE
);
1187 glLineStipple (0.5, 0xcccc);
1189 xoff
= -page
->pixmap
->x
;
1190 yoff
-= page
->pixmap
->y
;
1193 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1194 fz_point p1
, p2
, p3
, p4
;
1195 fz_matrix ctm
= page
->pagedim
->ctm
;
1197 p1
.x
= link
->rect
.x0
;
1198 p1
.y
= link
->rect
.y0
;
1200 p2
.x
= link
->rect
.x1
;
1201 p2
.y
= link
->rect
.y0
;
1203 p3
.x
= link
->rect
.x1
;
1204 p3
.y
= link
->rect
.y1
;
1206 p4
.x
= link
->rect
.x0
;
1207 p4
.y
= link
->rect
.y1
;
1209 p1
= fz_transformpoint (ctm
, p1
);
1210 p2
= fz_transformpoint (ctm
, p2
);
1211 p3
= fz_transformpoint (ctm
, p3
);
1212 p4
= fz_transformpoint (ctm
, p4
);
1214 switch (link
->kind
) {
1215 case PDF_LGOTO
: glColor3ub (255, 0, 0); break;
1216 case PDF_LURI
: glColor3ub (0, 0, 255); break;
1217 default: glColor3ub (0, 0, 0); break;
1220 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1221 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1222 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1223 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1227 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1228 glDisable (GL_LINE_STIPPLE
);
1229 unlock ("ml_highlightlinks");
1232 CAMLreturn (Val_unit
);
1235 CAMLprim value
ml_getlink (value ptr_v
, value x_v
, value y_v
)
1237 CAMLparam3 (ptr_v
, x_v
, y_v
);
1238 CAMLlocal2 (ret_v
, tup_v
);
1241 char *s
= String_val (ptr_v
);
1243 ret_v
= Val_int (0);
1244 if (trylock ("ml_getlink")) {
1245 ret_v
= Val_int (0);
1249 page
= parse_pointer ("ml_getlink", s
);
1251 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1253 switch (link
->kind
) {
1261 obj
= fz_arrayget (link
->dest
, 0);
1262 if (fz_isindirect (obj
)) {
1263 pageno
= pdf_findpageobject (state
.xref
, obj
) - 1;
1265 else if (fz_isint (obj
)) {
1266 pageno
= fz_toint (obj
);
1269 if (fz_arraylen (link
->dest
) > 3) {
1270 p
.x
= fz_toint (fz_arrayget (link
->dest
, 2));
1271 p
.y
= fz_toint (fz_arrayget (link
->dest
, 3));
1272 p
= fz_transformpoint (page
->pagedim
->ctm
, p
);
1279 tup_v
= caml_alloc_tuple (2);
1280 ret_v
= caml_alloc_small (1, 1);
1281 Field (tup_v
, 0) = Val_int (pageno
);
1282 Field (tup_v
, 1) = Val_int (p
.y
);
1283 Field (ret_v
, 0) = tup_v
;
1288 ret_v
= caml_alloc_small (1, 0);
1289 Field (ret_v
, 0) = caml_copy_string (fz_tostrbuf (link
->dest
));
1293 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1297 unlock ("ml_getlink");
1303 CAMLprim value
ml_gettext (value ptr_v
, value rect_v
, value oy_v
, value rectsel_v
)
1305 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1310 char *s
= String_val (ptr_v
);
1311 int rectsel
= Bool_val (rectsel_v
);
1312 int i
, bx0
, bx1
, by0
, by1
, x0
, x1
, y0
, y1
, oy
;
1314 /* stop GCC from complaining about uninitialized variables */
1316 int rx0
= rx0
, rx1
= rx1
, ry0
= ry0
, ry1
= ry1
;
1318 int rx0
, rx1
, ry0
, ry1
;
1321 if (trylock ("ml_gettext")) {
1325 page
= parse_pointer ("ml_gettext", s
);
1327 oy
= Int_val (oy_v
);
1328 p1
.x
= Int_val (Field (rect_v
, 0));
1329 p1
.y
= Int_val (Field (rect_v
, 1));
1330 p2
.x
= Int_val (Field (rect_v
, 2));
1331 p2
.y
= Int_val (Field (rect_v
, 3));
1334 glEnable (GL_BLEND
);
1335 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1336 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1337 glColor4f (0.0f
, 0.0f
, 0.0f
, 0.2f
);
1338 glRecti (p1
.x
, p1
.y
, p2
.x
, p2
.y
);
1339 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1340 glDisable (GL_BLEND
);
1343 ctm
= page
->pagedim
->ctm
;
1348 page
->text
= fz_newtextspan ();
1349 tdev
= fz_newtextdevice (page
->text
);
1350 error
= pdf_runpage (state
.xref
, page
->drawpage
, tdev
,
1351 page
->pagedim
->ctm
);
1352 if (error
) die (error
);
1353 fz_freedevice (tdev
);
1358 printf ("BBox %f %f %f %f\n", p1
.x
, p1
.y
, p2
.x
, p2
.y
);
1359 p1
.x
+= page
->pixmap
->x
;
1360 p1
.y
+= page
->pixmap
->y
;
1361 p2
.x
+= page
->pixmap
->x
;
1362 p2
.y
+= page
->pixmap
->y
;
1367 printf ("BBox %d %d %d %d %d %d\n", x0
, y0
, x1
, y1
, oy
, page
->pageno
);
1369 for (span
= page
->text
; span
; span
= span
->next
) {
1372 /* fz_debugtextspanxml (span); */
1373 for (i
= 0; i
< span
->len
; ++i
) {
1376 bx0
= span
->text
[i
].bbox
.x0
;
1377 bx1
= span
->text
[i
].bbox
.x1
;
1378 by0
= span
->text
[i
].bbox
.y0
+ oy
;
1379 by1
= span
->text
[i
].bbox
.y1
+ oy
;
1381 if ((bx1
>= x0
&& bx0
<= x1
&& by1
>= y0
&& by0
<= y1
)) {
1383 rx0
= bx0
- page
->pixmap
->x
;
1384 rx1
= bx1
- page
->pixmap
->x
;
1390 c
= span
->text
[i
].c
;
1392 if ((isprint (c
) && !isspace (c
))) {
1394 bx0
-= page
->pixmap
->x
;
1395 bx1
-= page
->pixmap
->x
;
1396 glEnable (GL_BLEND
);
1397 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1398 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1399 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1400 glRecti (bx0
, by0
, bx1
, by1
);
1401 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1402 glDisable (GL_BLEND
);
1404 if (isprint (c
) || c
==' ') {
1419 glEnable (GL_BLEND
);
1420 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1421 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1422 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1423 glRecti (rx0
, ry0
, rx1
, ry1
);
1424 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1425 glDisable (GL_BLEND
);
1429 if (seen
&& span
->eol
) {
1430 x0
= page
->pixmap
->x
;
1431 putc ('\n', stdout
);
1434 unlock ("ml_gettext");
1437 CAMLreturn (Val_unit
);
1440 CAMLprim value
ml_getpagewh (value pagedimno_v
)
1442 CAMLparam1 (pagedimno_v
);
1444 int pagedimno
= Int_val (pagedimno_v
);
1446 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1447 Store_double_field (ret_v
, 0, state
.pagedims
[pagedimno
].box
.x0
);
1448 Store_double_field (ret_v
, 1, state
.pagedims
[pagedimno
].box
.x1
);
1449 Store_double_field (ret_v
, 2, state
.pagedims
[pagedimno
].box
.y0
);
1450 Store_double_field (ret_v
, 3, state
.pagedims
[pagedimno
].box
.y1
);
1454 CAMLprim value
ml_init (value sock_v
)
1459 CAMLparam1 (sock_v
);
1461 state
.texcount
= 256;
1462 state
.sliceheight
= 24;
1463 state
.texform
= GL_RGBA
;
1464 state
.texty
= GL_UNSIGNED_BYTE
;
1467 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1468 if (!state
.texids
) {
1469 err (1, "calloc texids " FMT_s
,
1470 state
.texcount
* sizeof (*state
.texids
));
1473 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
1474 if (!state
.texowners
) {
1475 err (1, "calloc texowners " FMT_s
,
1476 state
.texcount
* sizeof (*state
.texowners
));
1479 glGenTextures (state
.texcount
, state
.texids
);
1482 state
.sock
= Socket_val (sock_v
);
1484 state
.sock
= Int_val (sock_v
);
1487 state
.cache
= fz_newglyphcache ();
1489 errx (1, "fz_newglyphcache failed");
1493 InitializeCriticalSection (&critsec
);
1494 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
1495 if (state
.thread
== INVALID_HANDLE_VALUE
) {
1496 errx (1, "CreateThread failed: %lx", GetLastError ());
1499 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
1501 errx (1, "pthread_create: %s", strerror (errno
));
1505 CAMLreturn (Val_unit
);