1 /* lots of code c&p-ed directly from mupdf */
3 #define WIN32_LEAN_AND_MEAN
6 #define fionread_arg long
14 #pragma warning (disable:4244)
15 #pragma warning (disable:4996)
16 #pragma warning (disable:4995)
24 static void __declspec (noreturn
) err (int exitcode
, const char *fmt
, ...)
31 vfprintf (stderr
, fmt
, ap
);
33 fprintf (stderr
, ": %s\n", strerror (errno
));
36 static void __declspec (noreturn
) errx (int exitcode
, const char *fmt
, ...)
41 vfprintf (stderr
, fmt
, ap
);
46 static void __declspec (noreturn
) sockerr (int exitcode
, const char *fmt
, ...)
51 vfprintf (stderr
, fmt
, ap
);
53 fprintf (stderr
, ": wsaerror 0x%x\n", WSAGetLastError ());
59 #define fionread_arg int
60 #define ioctlsocket ioctl
76 #include <sys/types.h>
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
82 #include <OpenGL/gl.h>
87 #ifndef GL_TEXTURE_RECTANGLE_ARB
88 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
91 #include <caml/fail.h>
92 #include <caml/alloc.h>
93 #include <caml/memory.h>
94 #include <caml/unixsupport.h>
100 #define lprintf printf
106 #include FT_FREETYPE_H
109 #define ARSERT(cond) for (;;) { \
111 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
136 struct pagedim pagedim
;
141 struct slice slices
[];
144 #if !defined _WIN32 && !defined __APPLE__
152 struct pagedim
*pagedims
;
156 fz_glyph_cache
*cache
;
184 static CRITICAL_SECTION critsec
;
186 static void lock (void *unused
)
189 EnterCriticalSection (&critsec
);
192 static void unlock (void *unused
)
195 LeaveCriticalSection (&critsec
);
198 static int trylock (void *unused
)
200 return TryEnterCriticalSection (&critsec
) == 0;
203 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
205 static void lock (const char *cap
)
207 int ret
= pthread_mutex_lock (&mutex
);
209 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
213 static void unlock (const char *cap
)
215 int ret
= pthread_mutex_unlock (&mutex
);
217 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
221 static int trylock (const char *cap
)
223 int ret
= pthread_mutex_trylock (&mutex
);
225 if (ret
&& ret
!= EBUSY
) {
226 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
232 static void *parse_pointer (const char *cap
, const char *s
)
237 ret
= sscanf (s
, "%p", &ptr
);
239 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
244 static int hasdata (int sock
)
248 ret
= ioctlsocket (sock
, FIONREAD
, &avail
);
249 if (ret
) sockerr (1, "hasdata: FIONREAD error ret=%d", ret
);
253 static double now (void)
257 if (gettimeofday (&tv
, NULL
)) {
258 err (1, "gettimeofday");
260 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
263 static void readdata (int fd
, char *p
, int size
)
267 n
= recv (fd
, p
, size
, 0);
269 if (!n
) errx (1, "EOF while reading");
270 sockerr (1, "recv (req %d, ret " FMT_ss
")", size
, n
);
274 static void writedata (int fd
, char *p
, int size
)
279 buf
[0] = (size
>> 24) & 0xff;
280 buf
[1] = (size
>> 16) & 0xff;
281 buf
[2] = (size
>> 8) & 0xff;
282 buf
[3] = (size
>> 0) & 0xff;
284 n
= send (fd
, buf
, 4, 0);
286 if (!n
) errx (1, "EOF while writing length");
287 sockerr (1, "send " FMT_ss
, n
);
290 n
= send (fd
, p
, size
, 0);
292 if (!n
) errx (1, "EOF while writing data");
293 sockerr (1, "send (req %d, ret " FMT_ss
")", size
, n
);
299 __attribute__ ((format (printf
, 2, 3)))
301 printd (int fd
, const char *fmt
, ...)
309 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
312 len
= vsnprintf (buf
, size
, fmt
, ap
);
315 if (len
> -1 && len
< size
) {
316 writedata (fd
, buf
, len
);
326 buf
= realloc (buf
, size
);
331 static void die (fz_error error
)
333 fz_catch (error
, "aborting");
335 pdf_free_xref (state
.xref
);
339 static void openxref (char *filename
, char *password
)
344 for (i
= 0; i
< state
.texcount
; ++i
) {
345 state
.texowners
[i
].slice
= NULL
;
349 fz_free_glyph_cache (state
.cache
);
352 state
.cache
= fz_new_glyph_cache ();
354 errx (1, "fz_newglyph_cache failed");
358 if (state
.xref
->store
) {
359 pdf_free_store (state
.xref
->store
);
360 state
.xref
->store
= NULL
;
362 pdf_free_xref (state
.xref
);
366 if (state
.pagedims
) {
367 free (state
.pagedims
);
368 state
.pagedims
= NULL
;
370 state
.pagedimcount
= 0;
372 error
= pdf_open_xref (&state
.xref
, filename
, password
);
377 error
= pdf_load_page_tree (state
.xref
);
382 state
.pagecount
= pdf_count_pages (state
.xref
);
385 static void pdfinfo (void)
389 printd (state
.sock
, "i version PDF-%d.%d\n",
390 state
.xref
->version
/ 10, state
.xref
->version
% 10);
398 f
= open_memstream (&buf
, &size
);
399 obj
= fz_dict_gets (state
.xref
->trailer
, "Info");
400 fz_fprint_obj (f
, fz_resolve_indirect (obj
), 0);
402 printd (state
.sock
, "i %.*s", size
, buf
);
408 static int readlen (int fd
)
413 n
= recv (fd
, p
, 4, 0);
415 if (!n
) errx (1, "EOF while reading length");
416 sockerr (1, "recv " FMT_ss
, n
);
419 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
422 static void unlinkpage (struct page
*page
)
426 for (i
= 0; i
< page
->slicecount
; ++i
) {
427 struct slice
*s
= &page
->slices
[i
];
428 if (s
->texindex
!= -1) {
429 if (state
.texowners
[s
->texindex
].slice
== s
) {
430 state
.texowners
[s
->texindex
].slice
= NULL
;
431 ARSERT (state
.texowners
[s
->texindex
].w
== s
->w
);
432 ARSERT (state
.texowners
[s
->texindex
].h
>= s
->h
);
438 static void freepage (struct page
*page
)
440 fz_drop_pixmap (page
->pixmap
);
445 fz_free_text_span (page
->text
);
447 if (page
->drawpage
) {
448 pdf_free_page (page
->drawpage
);
454 static void subdivide (struct page
*p
)
457 int h
= p
->pixmap
->h
;
458 int th
= MIN (h
, state
.sliceheight
);
460 for (i
= 0; i
< p
->slicecount
; ++i
) {
461 struct slice
*s
= &p
->slices
[i
];
469 static int compatpdims (struct pagedim
*p1
, struct pagedim
*p2
)
471 return p1
->rotate
== p2
->rotate
472 && !memcmp (&p1
->bbox
, &p2
->bbox
, sizeof (p1
->bbox
))
473 && !memcmp (&p1
->ctm
, &p2
->ctm
, sizeof (p1
->ctm
));
479 static int cacheline32bytes
;
481 static void __attribute__ ((constructor
)) clcheck (void)
483 char **envp
= environ
;
488 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
490 cacheline32bytes
= auxv
[1] == 32;
496 static void __attribute__ ((optimize ("O"))) clearpixmap (fz_pixmap
*pixmap
)
498 if (cacheline32bytes
) {
499 intptr_t a1
, a2
, diff
;
500 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
501 vector
unsigned char v
= vec_splat_u8 (-1);
502 vector
unsigned char *p
;
504 a1
= a2
= (intptr_t) pixmap
->samples
;
505 a2
= (a1
+ 31) & ~31;
510 while (a1
!= a2
) *(char *) a1
++ = 0xff;
511 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
512 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
514 vec_st (v
, i
+ 16, p
);
516 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
518 else fz_clear_pixmap_with_color (pixmap
, 0xff);
521 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
524 static void *render (int pageno
, int pindex
)
528 struct page
*page
= NULL
;
532 struct pagedim
*pagedim
;
535 printd (state
.sock
, "V rendering %d", pageno
);
537 pagedim
= &state
.pagedims
[pindex
];
538 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
539 + state
.sliceheight
- 1) / state
.sliceheight
;
540 slicecount
+= slicecount
== 0;
543 if (compatpdims (&state
.pig
->pagedim
, pagedim
)) {
546 fz_free_text_span (page
->text
);
549 if (page
->drawpage
) {
550 pdf_free_page (page
->drawpage
);
551 page
->drawpage
= NULL
;
555 freepage (state
.pig
);
559 page
= calloc (sizeof (*page
)
560 + (slicecount
* sizeof (struct slice
)), 1);
562 err (1, "calloc page %d\n", pageno
);
564 page
->pixmap
= fz_new_pixmap_with_rect (fz_device_rgb
, pagedim
->bbox
);
567 page
->slicecount
= slicecount
;
569 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
- 1);
573 clearpixmap (page
->pixmap
);
575 idev
= fz_new_draw_device (state
.cache
, page
->pixmap
);
577 die (fz_throw ("fz_newdrawdevice failed"));
578 error
= pdf_run_page (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
580 die (fz_rethrow (error
, "pdf_runpage failed"));
581 fz_free_device (idev
);
583 page
->drawpage
= drawpage
;
584 page
->pagedim
= *pagedim
;
585 page
->pageno
= pageno
;
589 pdf_age_store (state
.xref
->store
, 3);
591 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
596 static void initpdims (void)
602 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
606 fz_obj
*obj
, *pageobj
;
608 pageobj
= state
.xref
->page_objs
[pageno
];
610 obj
= fz_dict_gets (pageobj
, "CropBox");
611 if (!fz_is_array (obj
)) {
612 obj
= fz_dict_gets (pageobj
, "MediaBox");
613 if (!fz_is_array (obj
)) {
614 die (fz_throw ("cannot find page bounds %d (%d Rd)",
615 fz_to_num (pageobj
), fz_to_gen (pageobj
)));
618 box
= pdf_to_rect (obj
);
620 obj
= fz_dict_gets (pageobj
, "Rotate");
621 if (fz_is_int (obj
)) {
622 rotate
= fz_to_int (obj
);
627 rotate
+= state
.rotate
;
629 p
= &state
.pagedims
[state
.pagedimcount
- 1];
630 if ((state
.pagedimcount
== 0)
631 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
634 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
635 state
.pagedims
= realloc (state
.pagedims
, size
);
636 if (!state
.pagedims
) {
637 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
638 size
, state
.pagedimcount
+ 1);
640 p
= &state
.pagedims
[state
.pagedimcount
++];
647 printd (state
.sock
, "T Processed %d pages in %f seconds",
648 state
.pagecount
, end
- start
);
651 static void layout (void)
656 double zoom
, w
, maxw
= 0;
657 struct pagedim
*p
= state
.pagedims
;
659 if (state
.proportional
) {
660 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
661 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
662 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
663 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
664 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
667 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
668 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
669 box2
= fz_transform_rect (ctm
, box
);
670 w
= box2
.x1
- box2
.x0
;
671 maxw
= MAX (w
, maxw
);
676 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
677 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
678 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
679 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
680 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
683 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
684 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
685 box2
= fz_transform_rect (ctm
, box
);
686 w
= box2
.x1
- box2
.x0
;
688 if (state
.proportional
) {
689 double scale
= w
/ maxw
;
690 zoom
= (state
.w
/ w
) * scale
;
696 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
697 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
698 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
699 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
700 p
->bbox
= fz_round_rect (fz_transform_rect (ctm
, box
));
701 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
702 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
705 while (p
-- != state
.pagedims
) {
706 printd (state
.sock
, "l %d %d %d %d",
707 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
,
712 static void recurse_outline (pdf_outline
*outline
, int level
)
720 if (!outline
->link
) goto next
;
722 obj
= outline
->link
->dest
;
723 if (fz_is_indirect (obj
)) {
724 obj
= fz_resolve_indirect (obj
);
726 if (fz_is_array (obj
)) {
729 obj2
= fz_array_get (obj
, 0);
730 if (fz_is_int (obj2
)) {
731 pageno
= fz_to_int (obj2
);
734 pageno
= pdf_find_page_number (state
.xref
, obj2
);
737 if (fz_array_len (obj
) > 3) {
741 xo
= fz_array_get (obj
, 2);
742 yo
= fz_array_get (obj
, 3);
743 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
744 struct pagedim
*pagedim
= state
.pagedims
;
746 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
747 if (state
.pagedims
[i
].pageno
> pageno
)
749 pagedim
= &state
.pagedims
[i
];
751 p
.x
= fz_to_int (xo
);
752 p
.y
= fz_to_int (yo
);
753 p
= fz_transform_point (pagedim
->ctm
, p
);
754 h
= pagedim
->bbox
.y1
- pagedim
->bbox
.y0
;
760 pageno
= pdf_find_page_number (state
.xref
, outline
->link
->dest
);
763 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
765 printd (state
.sock
, "o %d %d %d %d %s",
766 level
, pageno
, top
, h
, outline
->title
);
769 if (outline
->child
) {
770 recurse_outline (outline
->child
, level
+ 1);
772 outline
= outline
->next
;
776 static void process_outline (void)
778 pdf_outline
*outline
;
780 if (!state
.needoutline
) return;
782 state
.needoutline
= 0;
783 outline
= pdf_load_outline (state
.xref
);
785 recurse_outline (outline
, 0);
786 pdf_free_outline (outline
);
790 static int comparespans (const void *l
, const void *r
)
792 fz_text_span
const *const*ls
= l
;
793 fz_text_span
const *const*rs
= r
;
794 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
797 /* wishful thinking function */
798 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
808 fz_text_span
*text
, *span
, **pspan
;
809 struct pagedim
*pdim
, *pdimprev
;
816 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
818 pdf_age_store (state
.xref
->store
, 3);
820 if (hasdata (state
.sock
)) {
821 printd (state
.sock
, "T attention requested aborting search at %d",
826 printd (state
.sock
, "T searching in page %d", pageno
);
830 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
831 pdim
= &state
.pagedims
[i
];
832 if (pdim
->pageno
== pageno
) {
835 if (pdim
->pageno
> pageno
) {
844 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
);
848 ctm
= fz_rotate (pdim
->rotate
);
850 text
= fz_new_text_span ();
851 tdev
= fz_new_text_device (text
);
852 error
= pdf_run_page (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
853 if (error
) die (error
);
854 fz_free_device (tdev
);
857 for (span
= text
; span
; span
= span
->next
) {
860 pspan
= malloc (sizeof (void *) * nspans
);
862 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
864 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
867 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
869 j
= forward
? 0 : nspans
- 1;
874 j
+= forward
? 1 : -1;
876 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
878 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
883 if (span
->text
[i
].bbox
.y0
> y
- 1) {
887 if (span
->text
[i
].c
< 256) {
888 *p
++ = span
->text
[i
].c
;
899 ret
= regexec (re
, buf
, 1, &rm
, 0);
901 if (ret
!= REG_NOMATCH
) {
904 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
906 "T regexec error `%.*s'",
908 fz_free_text_span (text
);
909 pdf_free_page (drawpage
);
917 fz_point p1
, p2
, p3
, p4
;
919 xoff
= pdim
->left
- pdim
->bbox
.x0
;
920 yoff
= -pdim
->bbox
.y0
;
922 sb
= &span
->text
[rm
.rm_so
].bbox
;
923 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
934 p1
= fz_transform_point (ctm
, p1
);
935 p2
= fz_transform_point (ctm
, p2
);
936 p3
= fz_transform_point (ctm
, p3
);
937 p4
= fz_transform_point (ctm
, p4
);
940 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
942 p1
.x
+ xoff
, p1
.y
+ yoff
,
943 p2
.x
+ xoff
, p2
.y
+ yoff
,
944 p3
.x
+ xoff
, p3
.y
+ yoff
,
945 p4
.x
+ xoff
, p4
.y
+ yoff
);
947 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
948 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
952 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
954 p1
.x
+ xoff
, p1
.y
+ yoff
,
955 p2
.x
+ xoff
, p2
.y
+ yoff
,
956 p3
.x
+ xoff
, p3
.y
+ yoff
,
957 p4
.x
+ xoff
, p4
.y
+ yoff
);
970 fz_free_text_span (text
);
971 pdf_free_page (drawpage
);
976 printd (state
.sock
, "T no matches %f sec", end
- start
);
978 printd (state
.sock
, "D");
987 mainloop (void *unused
)
990 int len
, ret
, oldlen
= 0;
993 len
= readlen (state
.sock
);
995 errx (1, "readlen returned 0");
998 if (oldlen
< len
+ 1) {
999 p
= realloc (p
, len
+ 1);
1001 err (1, "realloc %d failed", len
+ 1);
1005 readdata (state
.sock
, p
, len
);
1008 if (!strncmp ("open", p
, 4)) {
1012 char *filename
= p
+ 5;
1014 filenamelen
= strlen (filename
);
1015 password
= filename
+ filenamelen
+ 1;
1017 openxref (filename
, password
);
1020 obj
= fz_dict_gets (state
.xref
->trailer
, "Info");
1024 obj
= fz_dict_gets (obj
, "Title");
1025 s
= pdf_to_utf8 (obj
);
1027 printd (state
.sock
, "t %s", s
);
1032 state
.needoutline
= 1;
1034 else if (!strncmp ("free", p
, 4)) {
1037 ret
= sscanf (p
+ 4, " %p", &ptr
);
1039 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
1044 else if (!strncmp ("search", p
, 6)) {
1045 int icase
, pageno
, y
, ret
, len2
, forward
;
1049 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1050 &icase
, &pageno
, &y
, &forward
, &len2
);
1052 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1055 pattern
= p
+ 6 + len2
;
1056 ret
= regcomp (&re
, pattern
,
1057 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1062 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1063 printd (state
.sock
, "T regcomp failed `%.*s'",
1064 (int) size
, errbuf
);
1067 search (&re
, pageno
, y
, forward
);
1071 else if (!strncmp ("geometry", p
, 8)) {
1074 printd (state
.sock
, "c");
1075 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1077 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1085 for (i
= 0; i
< state
.texcount
; ++i
) {
1086 state
.texowners
[i
].slice
= NULL
;
1091 unlock ("geometry");
1092 printd (state
.sock
, "C %d", state
.pagecount
);
1094 else if (!strncmp ("reinit", p
, 6)) {
1098 printd (state
.sock
, "c");
1099 ret
= sscanf (p
+ 6, " %f %d", &rotate
, &proportional
);
1101 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
1104 state
.rotate
= rotate
;
1105 state
.proportional
= proportional
;
1106 state
.pagedimcount
= 0;
1107 free (state
.pagedims
);
1108 state
.pagedims
= NULL
;
1113 freepage (state
.pig
);
1117 printd (state
.sock
, "C %d", state
.pagecount
);
1119 else if (!strncmp ("render", p
, 6)) {
1120 int pageno
, pindex
, w
, h
, ret
;
1123 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
1125 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1129 page
= render (pageno
, pindex
);
1132 printd (state
.sock
, "r %d %d %d %d %d %d %p",
1141 else if (!strncmp ("info", p
, 4)) {
1144 else if (!strncmp ("interrupt", p
, 9)) {
1145 printd (state
.sock
, "V interrupted");
1148 errx (1, "unknown command %.*s", len
, p
);
1154 static void showsel (struct page
*page
, int oy
)
1159 struct mark first
, last
;
1161 first
= page
->fmark
;
1164 if (!first
.span
|| !last
.span
) return;
1166 glEnable (GL_BLEND
);
1167 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1168 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1170 ox
= -page
->pixmap
->x
+ page
->pagedim
.left
;
1171 oy
= -page
->pixmap
->y
+ oy
;
1172 for (span
= first
.span
; span
; span
= span
->next
) {
1175 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1180 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1181 j
= MIN (first
.i
, last
.i
);
1182 k
= MAX (first
.i
, last
.i
);
1184 else if (span
== first
.span
) {
1187 else if (span
== last
.span
) {
1191 for (i
= j
; i
<= k
; ++i
) {
1192 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1194 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1201 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1203 if (span
== last
.span
) break;
1205 glDisable (GL_BLEND
);
1208 static void highlightlinks (struct page
*page
, int yoff
)
1213 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1214 glEnable (GL_LINE_STIPPLE
);
1215 glLineStipple (0.5, 0xcccc);
1217 xoff
= page
->pagedim
.left
- page
->pixmap
->x
;
1218 yoff
-= page
->pixmap
->y
;
1221 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1222 fz_point p1
, p2
, p3
, p4
;
1223 fz_matrix ctm
= page
->pagedim
.ctm
;
1225 p1
.x
= link
->rect
.x0
;
1226 p1
.y
= link
->rect
.y0
;
1228 p2
.x
= link
->rect
.x1
;
1229 p2
.y
= link
->rect
.y0
;
1231 p3
.x
= link
->rect
.x1
;
1232 p3
.y
= link
->rect
.y1
;
1234 p4
.x
= link
->rect
.x0
;
1235 p4
.y
= link
->rect
.y1
;
1237 p1
= fz_transform_point (ctm
, p1
);
1238 p2
= fz_transform_point (ctm
, p2
);
1239 p3
= fz_transform_point (ctm
, p3
);
1240 p4
= fz_transform_point (ctm
, p4
);
1242 switch (link
->kind
) {
1243 case PDF_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1244 case PDF_LINK_URI
: glColor3ub (0, 0, 255); break;
1245 default: glColor3ub (0, 0, 0); break;
1248 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1249 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1250 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1251 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1255 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1256 glDisable (GL_LINE_STIPPLE
);
1259 static void upload2 (struct page
*page
, int slicenum
, const char *cap
)
1264 struct slice
*slice
= &page
->slices
[slicenum
];
1266 w
= page
->pixmap
->w
;
1268 ARSERT (w
== slice
->w
);
1269 if (slice
->texindex
!= -1
1270 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1271 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1275 int index
= (state
.texindex
++ % state
.texcount
);
1278 for (i
= 0; i
< slicenum
; ++i
) {
1279 offset
+= w
* page
->slices
[i
].h
* 4;
1282 if (state
.texowners
[index
].w
== slice
->w
) {
1283 if (state
.texowners
[index
].h
>= slice
->h
) {
1287 state
.texowners
[index
].h
= slice
->h
;
1291 state
.texowners
[index
].h
= slice
->h
;
1294 state
.texowners
[index
].slice
= slice
;
1295 state
.texowners
[index
].w
= slice
->w
;
1296 slice
->texindex
= index
;
1298 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1302 GLenum err
= glGetError ();
1303 if (err
!= GL_NO_ERROR
) {
1304 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1308 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1316 page
->pixmap
->samples
+ offset
1319 GLenum err
= glGetError ();
1320 if (err
!= GL_NO_ERROR
) {
1321 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1327 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1335 page
->pixmap
->samples
+ offset
1342 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1343 subimage
? "sub" : "img",
1344 page
->pageno
, slicenum
,
1346 state
.texids
[slice
->texindex
],
1351 CAMLprim value
ml_draw (value args_v
, value ptr_v
)
1353 CAMLparam2 (args_v
, ptr_v
);
1354 int dispy
= Int_val (Field (args_v
, 0));
1355 int w
= Int_val (Field (args_v
, 1));
1356 int h
= Int_val (Field (args_v
, 2));
1357 int py
= Int_val (Field (args_v
, 3));
1358 int hlinks
= Bool_val (Field (args_v
, 4));
1359 char *s
= String_val (ptr_v
);
1364 int yoff
= dispy
- py
;
1366 ret
= sscanf (s
, "%p", &ptr
);
1368 errx (1, "cannot parse pointer `%s'", s
);
1372 w
= page
->pixmap
->w
;
1374 ARSERT (h
>= 0 && "ml_draw wrong h");
1375 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1377 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1379 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1380 struct slice
*slice
= &page
->slices
[slicenum
];
1381 if (slice
->h
> py
) {
1387 h
= MIN (state
.h
, h
);
1390 struct slice
*slice
= &page
->slices
[slicenum
];
1392 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1394 th
= MIN (h
, slice
->h
- py
);
1395 upload2 (page
, slicenum
, "upload");
1397 left
= page
->pagedim
.left
;
1400 glTexCoord2i (0, py
);
1401 glVertex2i (left
, dispy
);
1403 glTexCoord2i (w
, py
);
1404 glVertex2i (left
+w
, dispy
);
1406 glTexCoord2i (w
, py
+th
);
1407 glVertex2i (left
+w
, dispy
+ th
);
1409 glTexCoord2i (0, py
+th
);
1410 glVertex2i (left
, dispy
+ th
);
1420 showsel (page
, yoff
);
1421 if (hlinks
) highlightlinks (page
, yoff
);
1422 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1424 CAMLreturn (Val_unit
);
1427 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1433 p
.x
= x
+ page
->pixmap
->x
;
1434 p
.y
= y
+ page
->pixmap
->y
;
1436 ctm
= fz_invert_matrix (page
->pagedim
.ctm
);
1437 p
= fz_transform_point (ctm
, p
);
1439 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1440 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1441 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1449 static void ensuretext (struct page
*page
)
1455 page
->text
= fz_new_text_span ();
1456 tdev
= fz_new_text_device (page
->text
);
1457 error
= pdf_run_page (state
.xref
, page
->drawpage
, tdev
,
1459 if (error
) die (error
);
1460 fz_free_device (tdev
);
1464 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1466 CAMLparam3 (ptr_v
, x_v
, y_v
);
1467 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1470 char *s
= String_val (ptr_v
);
1472 ret_v
= Val_int (0);
1473 if (trylock ("ml_whatsunder")) {
1477 page
= parse_pointer ("ml_whatsunder", s
);
1478 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1480 switch (link
->kind
) {
1491 if (fz_is_array (link
->dest
)) {
1492 obj
= fz_array_get (link
->dest
, 0);
1493 if (fz_is_indirect (obj
)) {
1494 pageno
= pdf_find_page_number (state
.xref
, obj
);
1496 else if (fz_is_int (obj
)) {
1497 pageno
= fz_to_int (obj
);
1500 if (fz_array_len (link
->dest
) > 3) {
1503 xo
= fz_array_get (link
->dest
, 2);
1504 yo
= fz_array_get (link
->dest
, 3);
1505 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
1506 p
.x
= fz_to_int (xo
);
1507 p
.y
= fz_to_int (yo
);
1508 p
= fz_transform_point (page
->pagedim
.ctm
, p
);
1513 pageno
= pdf_find_page_number (state
.xref
, link
->dest
);
1515 tup_v
= caml_alloc_tuple (2);
1516 ret_v
= caml_alloc_small (1, 1);
1517 Field (tup_v
, 0) = Val_int (pageno
);
1518 Field (tup_v
, 1) = Val_int (p
.y
);
1519 Field (ret_v
, 0) = tup_v
;
1524 str_v
= caml_copy_string (fz_to_str_buf (link
->dest
));
1525 ret_v
= caml_alloc_small (1, 0);
1526 Field (ret_v
, 0) = str_v
;
1530 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1539 x
= Int_val (x_v
) + page
->pixmap
->x
;
1540 y
= Int_val (y_v
) + page
->pixmap
->y
;
1542 for (span
= page
->text
; span
; span
= span
->next
) {
1543 for (i
= 0; i
< span
->len
; ++i
) {
1545 b
= &span
->text
[i
].bbox
;
1546 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1548 span
->font
&& span
->font
->name
1550 : "Span has no font name"
1552 #ifdef FT_FREETYPE_H
1553 FT_FaceRec
*face
= span
->font
->ft_face
;
1554 if (face
&& face
->family_name
) {
1556 char *n1
= face
->family_name
;
1557 size_t l1
= strlen (n1
);
1558 size_t l2
= strlen (n2
);
1560 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
1561 s
= malloc (l1
+ l2
+ 2);
1565 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
1566 str_v
= caml_copy_string (s
);
1572 str_v
= caml_copy_string (n2
);
1575 str_v
= caml_copy_string (n2
);
1577 ret_v
= caml_alloc_small (1, 2);
1578 Field (ret_v
, 0) = str_v
;
1585 unlock ("ml_whatsunder");
1591 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
, value oy_v
)
1593 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1597 struct mark first
, last
;
1598 int i
, x0
, x1
, y0
, y1
, oy
, left
;
1599 char *s
= String_val (ptr_v
);
1601 if (trylock ("ml_seltext")) {
1605 page
= parse_pointer ("ml_seltext", s
);
1608 oy
= Int_val (oy_v
);
1609 x0
= Int_val (Field (rect_v
, 0));
1610 y0
= Int_val (Field (rect_v
, 1));
1611 x1
= Int_val (Field (rect_v
, 2));
1612 y1
= Int_val (Field (rect_v
, 3));
1614 left
= page
->pagedim
.left
;
1616 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1617 glColor3ub (128, 128, 128);
1618 glRecti (x0
, y0
, x1
, y1
);
1619 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1622 x0
+= page
->pixmap
->x
- left
;
1623 y0
+= page
->pixmap
->y
- oy
;
1624 x1
+= page
->pixmap
->x
- left
;
1625 y1
+= page
->pixmap
->y
- oy
;
1630 last
.i
= first
.i
= 0;
1631 first
.span
= page
->text
;
1632 for (span
= page
->text
; span
; span
= span
->next
) {
1633 for (i
= 0; i
< span
->len
; ++i
) {
1634 b
= &span
->text
[i
].bbox
;
1637 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1642 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1647 if (0 && selected
) {
1648 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1649 glColor3ub (128, 128, 128);
1650 glRecti (b
->x0
+left
, b
->y0
, b
->x1
+left
, b
->y1
);
1651 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1656 if (y1
< y0
|| x1
< x0
) {
1659 if (first
.span
== last
.span
) {
1664 for (span
= first
.span
; span
&& span
!= last
.span
;
1665 span
= span
->next
) {
1678 first
.span
= last
.span
;
1684 page
->fmark
= first
;
1687 unlock ("ml_seltext");
1690 CAMLreturn (Val_unit
);
1693 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
1698 for (i
= a
; i
<= b
; ++i
) {
1699 len
= runetochar (buf
, &span
->text
[i
].c
);
1700 ret
= fwrite (buf
, len
, 1, f
);
1703 printd (state
.sock
, "T failed to write %d bytes ret=%d: %s",
1704 len
, ret
, strerror (errno
));
1711 CAMLprim value
ml_copysel (value ptr_v
)
1716 char *s
= String_val (ptr_v
);
1718 if (trylock ("ml_copysel")) {
1725 if (state
.xselpipe
) {
1726 int ret
= pclose (state
.xselpipe
);
1728 printd (state
.sock
, "T failed to close xsel pipe `%s'",
1731 state
.xselpipe
= NULL
;
1734 printf ("========================================\n");
1740 page
= parse_pointer ("ml_sopysel", s
);
1742 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1743 printd (state
.sock
, "T nothing to copy");
1749 if (!state
.xselpipe
) {
1750 state
.xselpipe
= popen ("xsel -i", "w");
1751 if (!state
.xselpipe
) {
1752 printd (state
.sock
, "T failed to open xsel pipe `%s'",
1764 for (span
= page
->fmark
.span
;
1765 span
&& span
!= page
->lmark
.span
->next
;
1766 span
= span
->next
) {
1767 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1768 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1769 if (pipespan (f
, span
, a
, b
)) {
1773 if (putc ('\n', f
) == EOF
) {
1774 printd (state
.sock
, "T failed break line on xsel pipe `%s'",
1780 page
->lmark
.span
= NULL
;
1781 page
->fmark
.span
= NULL
;
1785 unlock ("ml_copysel");
1788 CAMLreturn (Val_unit
);
1791 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
1793 CAMLparam1 (pagedimno_v
);
1795 int pagedimno
= Int_val (pagedimno_v
);
1798 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1799 if (trylock ("ml_getpdimrect")) {
1800 box
= fz_empty_rect
;
1803 box
= state
.pagedims
[pagedimno
].box
;
1804 unlock ("ml_getpdimrect");
1807 Store_double_field (ret_v
, 0, box
.x0
);
1808 Store_double_field (ret_v
, 1, box
.x1
);
1809 Store_double_field (ret_v
, 2, box
.y0
);
1810 Store_double_field (ret_v
, 3, box
.y1
);
1815 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
1817 CAMLparam3 (winw_v
, winh_v
, dw_v
);
1821 double maxw
= 0.0, maxh
= 0.0;
1823 double winw
= Int_val (winw_v
);
1824 double winh
= Int_val (winh_v
);
1825 double dw
= Int_val (dw_v
);
1826 double pw
= 1.0, ph
= 1.0, num
, den
;
1828 if (trylock ("ml_zoom_for_height")) {
1832 if (state
.proportional
) {
1833 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1836 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1837 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1840 maxw
= MAX (w
, maxw
);
1844 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1845 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
1847 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1848 y0
= MIN (p
->box
.y0
, p
->box
.y1
);
1849 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1850 y1
= MAX (p
->box
.y0
, p
->box
.y1
);
1855 if (state
.proportional
) {
1857 scaledh
= h
* scale
;
1864 if (scaledh
> maxh
) {
1871 num
= (winh
* pw
) + (ph
* dw
);
1875 unlock ("ml_zoom_for_height");
1877 ret_v
= caml_copy_double (zoom
);
1881 CAMLprim value
ml_init (value sock_v
, value params_v
)
1886 CAMLparam2 (sock_v
, params_v
);
1888 state
.rotate
= Int_val (Field (params_v
, 0));
1889 state
.proportional
= Bool_val (Field (params_v
, 1));
1890 state
.texcount
= Int_val (Field (params_v
, 2));
1891 state
.sliceheight
= Int_val (Field (params_v
, 3));
1892 state
.texform
= GL_RGBA
;
1893 state
.texty
= GL_UNSIGNED_BYTE
;
1896 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1897 if (!state
.texids
) {
1898 err (1, "calloc texids " FMT_s
,
1899 state
.texcount
* sizeof (*state
.texids
));
1902 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
1903 if (!state
.texowners
) {
1904 err (1, "calloc texowners " FMT_s
,
1905 state
.texcount
* sizeof (*state
.texowners
));
1908 glGenTextures (state
.texcount
, state
.texids
);
1911 state
.sock
= Socket_val (sock_v
);
1913 state
.sock
= Int_val (sock_v
);
1917 InitializeCriticalSection (&critsec
);
1918 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
1919 if (state
.thread
== INVALID_HANDLE_VALUE
) {
1920 errx (1, "CreateThread failed: %lx", GetLastError ());
1923 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
1925 errx (1, "pthread_create: %s", strerror (ret
));
1929 CAMLreturn (Val_unit
);