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 ());
68 #include <sys/types.h>
69 #include <sys/socket.h>
70 #include <sys/select.h>
74 #include <OpenGL/gl.h>
79 #ifndef GL_TEXTURE_RECTANGLE_ARB
80 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
83 #include <caml/fail.h>
84 #include <caml/alloc.h>
85 #include <caml/memory.h>
86 #include <caml/unixsupport.h>
92 #define lprintf printf
97 #define ARSERT(cond) for (;;) { \
99 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
115 struct pagedim
*pagedim
;
117 struct slice slices
[];
132 struct pagedim
*pagedims
;
136 fz_glyphcache
*cache
;
164 static CRITICAL_SECTION critsec
;
166 static void lock (void *unused
)
169 EnterCriticalSection (&critsec
);
172 static void unlock (void *unused
)
175 LeaveCriticalSection (&critsec
);
178 static int trylock (void *unused
)
180 return TryEnterCriticalSection (&critsec
) == 0;
183 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
185 static void lock (const char *cap
)
187 int ret
= pthread_mutex_lock (&mutex
);
189 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
193 static void unlock (const char *cap
)
195 int ret
= pthread_mutex_unlock (&mutex
);
197 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
201 static int trylock (const char *cap
)
203 int ret
= pthread_mutex_trylock (&mutex
);
205 if (ret
&& ret
!= EBUSY
) {
206 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
212 static void *parse_pointer (const char *cap
, const char *s
)
217 ret
= sscanf (s
, "%p", &ptr
);
219 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
224 static int hasdata (int sock
)
233 n
= select (sock
+ 1, &s
, NULL
, NULL
, &tv
);
234 if (n
== 0) return 0;
235 if (n
== 1) return 1;
236 sockerr (1, "hasdata: select error ret=%d", n
);
239 static double now (void)
243 if (gettimeofday (&tv
, NULL
)) {
244 err (1, "gettimeofday");
246 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
249 static void readdata (int fd
, char *p
, int size
)
253 n
= recv (fd
, p
, size
, 0);
255 if (!n
) errx (1, "EOF while reading");
256 sockerr (1, "recv (req %d, ret %zd)", size
, n
);
260 static void writedata (int fd
, char *p
, int size
)
265 buf
[0] = (size
>> 24) & 0xff;
266 buf
[1] = (size
>> 16) & 0xff;
267 buf
[2] = (size
>> 8) & 0xff;
268 buf
[3] = (size
>> 0) & 0xff;
270 n
= send (fd
, buf
, 4, 0);
272 if (!n
) errx (1, "EOF while writing length");
273 sockerr (1, "send %zd", n
);
276 n
= send (fd
, p
, size
, 0);
278 if (!n
) errx (1, "EOF while writing data");
279 sockerr (1, "send (req %d, ret %zd)", size
, n
);
285 __attribute__ ((format (printf
, 2, 3)))
287 printd (int fd
, const char *fmt
, ...)
294 len
= vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
296 writedata (fd
, buf
, len
);
299 static void die (fz_error error
)
301 fz_catch (error
, "aborting");
303 pdf_closexref (state
.xref
);
307 static void openxref (char *filename
)
313 fd
= open (filename
, O_BINARY
| O_RDONLY
, 0666);
315 die (fz_throw ("cannot open file '%s'", filename
));
317 file
= fz_openfile (fd
);
318 state
.xref
= pdf_openxref (file
);
320 die (fz_throw ("cannot open PDF file '%s'", filename
));
321 fz_dropstream (file
);
323 if (pdf_needspassword (state
.xref
)) {
324 die (fz_throw ("password protected"));
327 error
= pdf_loadpagetree (state
.xref
);
329 die (fz_throw ("cannot load page tree"));
332 state
.pagecount
= pdf_getpagecount (state
.xref
);
333 state
.pagetbl
= stat_alloc (state
.pagecount
* sizeof (*state
.pagetbl
));
336 static int readlen (int fd
)
341 n
= recv (fd
, p
, 4, 0);
343 if (!n
) errx (1, "EOF while reading length");
344 sockerr (1, "read %zd", n
);
347 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
350 static void freepage (struct page
*page
)
355 fz_droppixmap (page
->pixmap
);
356 for (p
= state
.pages
; p
; p
= p
->prev
) {
357 if (p
->prev
== page
) {
358 p
->prev
= page
->prev
;
362 for (i
= 0; i
< page
->slicecount
; ++i
) {
363 struct slice
*s
= &page
->slices
[i
];
364 if (s
->texindex
!= -1) {
365 if (state
.texowners
[s
->texindex
].slice
== s
) {
366 state
.texowners
[s
->texindex
].slice
= NULL
;
367 ARSERT (state
.texowners
[s
->texindex
].w
== s
->w
);
368 ARSERT (state
.texowners
[s
->texindex
].h
>= s
->h
);
373 fz_freetextspan (page
->text
);
375 if (page
->drawpage
) {
376 pdf_freepage (page
->drawpage
);
382 static void subdivide (struct page
*p
)
385 int h
= p
->pixmap
->h
;
386 int th
= MIN (h
, state
.sliceheight
);
388 for (i
= 0; i
< p
->slicecount
; ++i
) {
389 struct slice
*s
= &p
->slices
[i
];
397 static void *render (int pageno
, int pindex
)
406 struct pagedim
*pagedim
;
409 printd (state
.sock
, "V rendering %d", pageno
);
410 pdf_flushxref (state
.xref
, 0);
412 pagedim
= &state
.pagedims
[pindex
];
413 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
414 + state
.sliceheight
- 1) / state
.sliceheight
;
415 slicecount
+= slicecount
== 0;
417 page
= calloc (sizeof (*page
)
418 + (slicecount
* sizeof (struct slice
)), 1);
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 (pdf_devicergb
, pagedim
->bbox
);
437 fz_clearpixmap (page
->pixmap
, 0xFF);
439 idev
= fz_newdrawdevice (state
.cache
, page
->pixmap
);
441 die (fz_throw ("fz_newdrawdevice failed"));
442 error
= pdf_runcontentstream (idev
, pagedim
->ctm
, state
.xref
,
445 fz_freedevice (idev
);
447 page
->drawpage
= drawpage
;
448 page
->pagedim
= pagedim
;
449 page
->pageno
= pageno
;
453 if (!state
.lotsamemory
) {
454 pdf_agestoreditems (state
.xref
->store
);
455 pdf_evictageditems (state
.xref
->store
);
458 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
462 static void initpdims (void)
468 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
472 fz_obj
*obj
, *pageobj
;
474 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
476 obj
= fz_dictgets (pageobj
, "CropBox");
477 if (!fz_isarray (obj
)) {
478 obj
= fz_dictgets (pageobj
, "MediaBox");
479 if (!fz_isarray (obj
)) {
480 die (fz_throw ("cannot find page bounds %d (%d Rd)",
481 fz_tonum (pageobj
), fz_togen (pageobj
)));
484 box
= pdf_torect (obj
);
486 obj
= fz_dictgets (pageobj
, "Rotate");
487 if (fz_isint (obj
)) {
488 rotate
= fz_toint (obj
);
493 rotate
+= state
.rotate
;
495 state
.pagetbl
[pageno
] = fz_tonum (state
.xref
->pagerefs
[pageno
]);
496 p
= &state
.pagedims
[state
.pagedimcount
- 1];
497 if ((state
.pagedimcount
== 0)
498 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
501 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
502 state
.pagedims
= realloc (state
.pagedims
, size
);
503 if (!state
.pagedims
) {
504 err (1, "realloc pagedims to %zu (%d elems)",
505 size
, state
.pagedimcount
+ 1);
507 p
= &state
.pagedims
[state
.pagedimcount
++];
514 printd (state
.sock
, "T Processed %d pages in %f seconds",
515 state
.pagecount
, end
- start
);
518 static void layout (void)
524 struct pagedim
*p
= state
.pagedims
;
527 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
528 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
529 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
530 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
531 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
533 ctm
= fz_identity ();
534 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
535 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
536 box2
= fz_transformrect (ctm
, box
);
537 w
= box2
.x1
- box2
.x0
;
539 zoom
= (state
.w
/ w
);
540 ctm
= fz_identity ();
541 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
542 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
543 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
544 p
->bbox
= fz_roundrect (fz_transformrect (ctm
, box
));
545 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
548 while (p
-- != state
.pagedims
) {
549 printd (state
.sock
, "l %d %d %d",
550 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
);
554 static void recurse_outline (pdf_outline
*outline
, int level
)
562 if (!outline
->link
) goto next
;
564 obj
= outline
->link
->dest
;
565 if (fz_isindirect (obj
)) {
566 num
= fz_tonum (obj
);
568 for (i
= 0; i
< state
.pagecount
; ++i
) {
569 if (state
.pagetbl
[i
] == num
) {
575 else if (fz_isarray (obj
)) {
578 obj2
= fz_arrayget (obj
, 0);
579 if (fz_isint (obj2
)) {
580 pageno
= fz_toint (obj2
);
583 num
= fz_tonum (obj2
);
584 for (i
= 0; i
< state
.pagecount
; ++i
) {
585 if (state
.pagetbl
[i
] == num
) {
592 if (fz_arraylen (obj
) > 3) {
594 struct pagedim
*pagedim
= state
.pagedims
;
596 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
597 if (state
.pagedims
[i
].pageno
> pageno
)
599 pagedim
= &state
.pagedims
[i
];
602 p
.x
= fz_toint (fz_arrayget (obj
, 2));
603 p
.y
= fz_toint (fz_arrayget (obj
, 3));
604 p
= fz_transformpoint (pagedim
->ctm
, p
);
609 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
610 printd (state
.sock
, "o %d %d %d %s",
611 level
, pageno
, top
, outline
->title
);
613 if (outline
->child
) {
614 recurse_outline (outline
->child
, level
+ 1);
616 outline
= outline
->next
;
620 static void process_outline (void)
622 pdf_outline
*outline
;
624 outline
= pdf_loadoutline (state
.xref
);
626 recurse_outline (outline
, 0);
627 pdf_freeoutline (outline
);
631 static int comparespans (const void *l
, const void *r
)
633 fz_textspan
const *const*ls
= l
;
634 fz_textspan
const *const*rs
= r
;
635 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
638 /* wishful thinking function */
639 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
649 fz_textspan
*text
, *span
, **pspan
;
650 struct pagedim
*pdim
, *pdimprev
;
657 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
659 if (!state
.lotsamemory
) {
660 pdf_agestoreditems (state
.xref
->store
);
661 pdf_evictageditems (state
.xref
->store
);
664 if (hasdata (state
.sock
)) {
665 printd (state
.sock
, "T attention requested aborting search at %d",
670 printd (state
.sock
, "T searching in page %d", pageno
);
674 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
675 pdim
= &state
.pagedims
[i
];
676 if (pdim
->pageno
== pageno
) {
679 if (pdim
->pageno
> pageno
) {
688 pageobj
= pdf_getpageobject (state
.xref
, pageno
+ 1);
690 die (fz_throw ("cannot retrieve info from page %d", pageno
));
692 error
= pdf_loadpage (&drawpage
, state
.xref
, pageobj
);
696 text
= fz_newtextspan ();
697 tdev
= fz_newtextdevice (text
);
698 error
= pdf_runcontentstream (tdev
, pdim
->ctm
, state
.xref
,
701 if (error
) die (error
);
702 fz_freedevice (tdev
);
705 for (span
= text
; span
; span
= span
->next
) {
708 pspan
= malloc (sizeof (void *) * nspans
);
710 err (1, "malloc span pointers %zu", sizeof (void *) * nspans
);
712 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
715 qsort (pspan
, nspans
, sizeof (fz_textspan
*), comparespans
);
717 j
= forward
? 0 : nspans
- 1;
722 j
+= forward
? 1 : -1;
724 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
726 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
731 if (span
->text
[i
].bbox
.y0
> y
- 1) {
735 if (span
->text
[i
].c
< 256) {
736 *p
++ = span
->text
[i
].c
;
747 ret
= regexec (re
, buf
, 1, &rm
, 0);
749 if (ret
!= REG_NOMATCH
) {
752 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
754 "T regexec error `%.*s'",
756 fz_freetextspan (text
);
757 pdf_freepage (drawpage
);
765 r
.x0
= span
->text
[rm
.rm_so
].bbox
.x0
- pdim
->bbox
.x0
;
766 r
.y0
= span
->text
[rm
.rm_so
].bbox
.y0
;
767 r
.x1
= span
->text
[rm
.rm_eo
- 1].bbox
.x1
- pdim
->bbox
.x0
;
768 r
.y1
= span
->text
[rm
.rm_eo
- 1].bbox
.y1
;
771 printd (state
.sock
, "F %d %d %f %f %f %f",
777 printd (state
.sock
, "R %d %d %f %f %f %f",
782 printd (state
.sock
, "T found at %d `%.*s' %f in %f sec",
783 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
784 span
->text
[0].bbox
.y0
- drawpage
->mediabox
.y0
,
797 fz_freetextspan (text
);
798 pdf_freepage (drawpage
);
803 printd (state
.sock
, "T no matches %f sec", end
- start
);
805 printd (state
.sock
, "D");
814 mainloop (void *unused
)
817 int len
, ret
, oldlen
= 0;
820 len
= readlen (state
.sock
);
822 errx (1, "readlen returned 0");
825 if (oldlen
< len
+ 1) {
826 p
= realloc (p
, len
+ 1);
828 err (1, "realloc %d failed", len
+ 1);
832 readdata (state
.sock
, p
, len
);
835 if (!strncmp ("open", p
, 4)) {
837 char *filename
= p
+ 5;
842 obj
= fz_dictgets (state
.xref
->trailer
, "Info");
844 obj
= fz_dictgets (obj
, "Title");
846 printd (state
.sock
, "t %s", pdf_toutf8 (obj
));
850 else if (!strncmp ("free", p
, 4)) {
853 ret
= sscanf (p
+ 4, " %p", &ptr
);
855 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
860 printd (state
.sock
, "d");
862 else if (!strncmp ("search", p
, 6)) {
863 int icase
, pageno
, y
, ret
, len2
, forward
;
867 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
868 &icase
, &pageno
, &y
, &forward
, &len2
);
870 errx (1, "malformed search `%s' ret=%d", p
, ret
);
873 pattern
= p
+ 6 + len2
;
874 ret
= regcomp (&re
, pattern
,
875 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
880 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
881 printd (state
.sock
, "T regcomp failed `%.*s'",
885 search (&re
, pageno
, y
, forward
);
889 else if (!strncmp ("geometry", p
, 8)) {
892 printd (state
.sock
, "c");
893 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
895 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
901 for (i
= 0; i
< state
.texcount
; ++i
) {
902 state
.texowners
[i
].slice
= NULL
;
909 printd (state
.sock
, "C %d", state
.pagecount
);
911 else if (!strncmp ("rotate", p
, 6)) {
914 printd (state
.sock
, "c");
915 ret
= sscanf (p
+ 6, " %f", &rotate
);
917 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
919 state
.rotate
= rotate
;
921 state
.pagedimcount
= 0;
922 free (state
.pagedims
);
923 state
.pagedims
= NULL
;
928 printd (state
.sock
, "C %d", state
.pagecount
);
930 else if (!strncmp ("render", p
, 6)) {
931 int pageno
, pindex
, w
, h
, ret
;
934 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
936 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
939 page
= render (pageno
, pindex
);
940 printd (state
.sock
, "r %d %d %d %d %p\n",
947 else if (!strncmp ("interrupt", p
, 8)) {
948 printd (state
.sock
, "V interrupted");
951 errx (1, "unknown command %.*s", len
, p
);
957 static void upload2 (struct page
*page
, int slicenum
, const char *cap
)
962 struct slice
*slice
= &page
->slices
[slicenum
];
967 ARSERT (w
== slice
->w
);
968 if (slice
->texindex
!= -1
969 && state
.texowners
[slice
->texindex
].slice
== slice
) {
970 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
974 int index
= (state
.texindex
++ % state
.texcount
);
977 for (i
= 0; i
< slicenum
; ++i
) {
978 offset
+= w
* page
->slices
[i
].h
* 4;
981 if (state
.texowners
[index
].w
== slice
->w
) {
982 if (state
.texowners
[index
].h
>= slice
->h
) {
986 state
.texowners
[index
].h
= slice
->h
;
990 state
.texowners
[index
].h
= slice
->h
;
993 state
.texowners
[index
].slice
= slice
;
994 state
.texowners
[index
].w
= slice
->w
;
995 slice
->texindex
= index
;
997 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1001 GLenum err
= glGetError ();
1002 if (err
!= GL_NO_ERROR
) {
1003 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1007 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1015 page
->pixmap
->samples
+ offset
1018 GLenum err
= glGetError ();
1019 if (err
!= GL_NO_ERROR
) {
1020 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1026 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1034 page
->pixmap
->samples
+ offset
1039 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1040 subimage
? "sub" : "img",
1041 page
->pageno
, slicenum
,
1043 state
.texids
[slice
->texindex
],
1048 CAMLprim value
ml_draw (value dispy_v
, value w_v
, value h_v
,
1049 value py_v
, value ptr_v
)
1051 CAMLparam5 (dispy_v
, w_v
, h_v
, py_v
, ptr_v
);
1052 int dispy
= Int_val (dispy_v
);
1053 int w
= Int_val (w_v
);
1054 int h
= Int_val (h_v
);
1055 int py
= Int_val (py_v
);
1056 char *s
= String_val (ptr_v
);
1062 if (trylock ("ml_draw")) {
1066 ret
= sscanf (s
, "%p", &ptr
);
1068 errx (1, "cannot parse pointer `%s'", s
);
1072 w
= page
->pixmap
->w
;
1074 ARSERT (h
>= 0 && "ml_draw wrong h");
1076 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1078 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1079 struct slice
*slice
= &page
->slices
[slicenum
];
1080 if (slice
->h
> py
) {
1086 h
= MIN (state
.h
, h
);
1089 struct slice
*slice
= &page
->slices
[slicenum
];
1091 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1093 th
= MIN (h
, slice
->h
- py
);
1094 upload2 (page
, slicenum
, "upload");
1098 glTexCoord2i (0, py
);
1099 glVertex2i (0, dispy
);
1101 glTexCoord2i (w
, py
);
1102 glVertex2i (w
, dispy
);
1104 glTexCoord2i (w
, py
+th
);
1105 glVertex2i (w
, dispy
+ th
);
1107 glTexCoord2i (0, py
+th
);
1108 glVertex2i (0, dispy
+ th
);
1118 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1122 CAMLreturn (Val_unit
);
1125 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1131 p
.x
= x
+ page
->pixmap
->x
;
1132 p
.y
= y
+ page
->pixmap
->y
;
1134 ctm
= fz_invertmatrix (page
->pagedim
->ctm
);
1135 p
= fz_transformpoint (ctm
, p
);
1137 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1138 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1139 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1140 if (link
->kind
== PDF_LGOTO
) {
1149 CAMLprim value
ml_checklink (value ptr_v
, value x_v
, value y_v
)
1151 CAMLparam3 (ptr_v
, x_v
, y_v
);
1152 char *s
= String_val (ptr_v
);
1155 if (trylock ("ml_checklink")) {
1159 ret
= NULL
!= getlink (parse_pointer ("ml_checklink", s
),
1160 Int_val (x_v
), Int_val (y_v
));
1161 unlock ("ml_checklink");
1163 CAMLreturn (Val_bool (ret
));
1166 CAMLprim value
ml_getlink (value ptr_v
, value x_v
, value y_v
)
1168 CAMLparam3 (ptr_v
, x_v
, y_v
);
1169 CAMLlocal2 (ret_v
, tup_v
);
1172 char *s
= String_val (ptr_v
);
1174 if (trylock ("ml_getlink")) {
1175 ret_v
= Val_int (0);
1179 page
= parse_pointer ("ml_getlink", s
);
1181 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1188 obj
= fz_arrayget (link
->dest
, 0);
1189 if (fz_isindirect (obj
)) {
1190 pageno
= pdf_findpageobject (state
.xref
, obj
) - 1;
1192 else if (fz_isint (obj
)) {
1193 pageno
= fz_toint (obj
);
1196 if (fz_arraylen (link
->dest
) > 3) {
1197 p
.x
= fz_toint (fz_arrayget (link
->dest
, 2));
1198 p
.y
= fz_toint (fz_arrayget (link
->dest
, 3));
1199 p
= fz_transformpoint (page
->pagedim
->ctm
, p
);
1206 tup_v
= caml_alloc_tuple (2);
1207 ret_v
= caml_alloc_small (1, 1);
1208 Field (tup_v
, 0) = Val_int (pageno
);
1209 Field (tup_v
, 1) = Val_int (p
.y
);
1210 Field (ret_v
, 0) = tup_v
;
1213 ret_v
= Val_int (0);
1215 unlock ("ml_getlink");
1221 CAMLprim value
ml_gettext (value ptr_v
, value rect_v
, value oy_v
, value rectsel_v
)
1223 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1228 char *s
= String_val (ptr_v
);
1229 int rectsel
= Bool_val (rectsel_v
);
1230 int i
, bx0
, bx1
, by0
, by1
, x0
, x1
, y0
, y1
, oy
;
1232 /* stop GCC from complaining about uninitialized variables */
1234 int rx0
= rx0
, rx1
= rx1
, ry0
= ry0
, ry1
= ry1
;
1236 int rx0
, rx1
, ry0
, ry1
;
1239 if (trylock ("ml_gettext")) {
1243 page
= parse_pointer ("ml_gettext", s
);
1245 oy
= Int_val (oy_v
);
1246 p1
.x
= Int_val (Field (rect_v
, 0));
1247 p1
.y
= Int_val (Field (rect_v
, 1));
1248 p2
.x
= Int_val (Field (rect_v
, 2));
1249 p2
.y
= Int_val (Field (rect_v
, 3));
1252 glEnable (GL_BLEND
);
1253 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1254 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1255 glColor4f (0.0f
, 0.0f
, 0.0f
, 0.2f
);
1256 glRecti (p1
.x
, p1
.y
, p2
.x
, p2
.y
);
1257 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1258 glDisable (GL_BLEND
);
1261 ctm
= page
->pagedim
->ctm
;
1266 page
->text
= fz_newtextspan ();
1267 tdev
= fz_newtextdevice (page
->text
);
1268 error
= pdf_runcontentstream (tdev
, page
->pagedim
->ctm
, state
.xref
,
1269 page
->drawpage
->resources
,
1270 page
->drawpage
->contents
);
1271 if (error
) die (error
);
1272 fz_freedevice (tdev
);
1277 printf ("BBox %f %f %f %f\n", p1
.x
, p1
.y
, p2
.x
, p2
.y
);
1278 p1
.x
+= page
->pixmap
->x
;
1279 p1
.y
+= page
->pixmap
->y
;
1280 p2
.x
+= page
->pixmap
->x
;
1281 p2
.y
+= page
->pixmap
->y
;
1286 printf ("BBox %d %d %d %d %d %d\n", x0
, y0
, x1
, y1
, oy
, page
->pageno
);
1288 for (span
= page
->text
; span
; span
= span
->next
) {
1291 /* fz_debugtextspanxml (span); */
1292 for (i
= 0; i
< span
->len
; ++i
) {
1295 bx0
= span
->text
[i
].bbox
.x0
;
1296 bx1
= span
->text
[i
].bbox
.x1
;
1297 by0
= span
->text
[i
].bbox
.y0
+ oy
;
1298 by1
= span
->text
[i
].bbox
.y1
+ oy
;
1300 if ((bx1
>= x0
&& bx0
<= x1
&& by1
>= y0
&& by0
<= y1
)) {
1302 rx0
= bx0
- page
->pixmap
->x
;
1303 rx1
= bx1
- page
->pixmap
->x
;
1309 c
= span
->text
[i
].c
;
1311 if ((isprint (c
) && !isspace (c
))) {
1313 bx0
-= page
->pixmap
->x
;
1314 bx1
-= page
->pixmap
->x
;
1315 glEnable (GL_BLEND
);
1316 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1317 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1318 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1319 glRecti (bx0
, by0
, bx1
, by1
);
1320 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1321 glDisable (GL_BLEND
);
1323 if (isprint (c
) || c
==' ') {
1338 glEnable (GL_BLEND
);
1339 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1340 glBlendFunc (GL_DST_ALPHA
, GL_SRC_ALPHA
);
1341 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1342 glRecti (rx0
, ry0
, rx1
, ry1
);
1343 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1344 glDisable (GL_BLEND
);
1348 if (seen
&& span
->eol
) {
1349 x0
= page
->pixmap
->x
;
1350 putc ('\n', stdout
);
1353 unlock ("ml_gettext");
1356 CAMLreturn (Val_unit
);
1359 CAMLprim value
ml_getpagewh (value pagedimno_v
)
1361 CAMLparam1 (pagedimno_v
);
1363 int pagedimno
= Int_val (pagedimno_v
);
1365 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1366 Store_double_field (ret_v
, 0, state
.pagedims
[pagedimno
].box
.x0
);
1367 Store_double_field (ret_v
, 1, state
.pagedims
[pagedimno
].box
.x1
);
1368 Store_double_field (ret_v
, 2, state
.pagedims
[pagedimno
].box
.y0
);
1369 Store_double_field (ret_v
, 3, state
.pagedims
[pagedimno
].box
.y1
);
1373 CAMLprim value
ml_init (value sock_v
)
1378 CAMLparam1 (sock_v
);
1380 state
.texcount
= 256;
1381 state
.sliceheight
= 24;
1382 state
.texform
= GL_RGBA
;
1383 state
.texty
= GL_UNSIGNED_BYTE
;
1385 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1386 if (!state
.texids
) {
1387 err (1, "calloc texids %zu", state
.texcount
* sizeof (*state
.texids
));
1390 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
1391 if (!state
.texowners
) {
1392 err (1, "calloc texowners %zu",
1393 state
.texcount
* sizeof (*state
.texowners
));
1396 glGenTextures (state
.texcount
, state
.texids
);
1399 state
.sock
= Socket_val (sock_v
);
1401 state
.sock
= Int_val (sock_v
);
1404 state
.cache
= fz_newglyphcache ();
1406 errx (1, "fz_newglyphcache failed");
1410 InitializeCriticalSection (&critsec
);
1411 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
1412 if (state
.thread
== INVALID_HANDLE_VALUE
) {
1413 errx (1, "CreateThread failed: %lx", GetLastError ());
1416 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
1418 errx (1, "pthread_create: %s", strerror (errno
));
1422 CAMLreturn (Val_unit
);