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)
387 printd (state
.sock
, "i PDF version %d.%d\n",
388 state
.xref
->version
/ 10, state
.xref
->version
% 10);
390 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
397 f
= open_memstream (&buf
, &size
);
399 obj
= fz_dict_gets (state
.xref
->trailer
, "Info");
400 fz_fprint_obj (f
, fz_resolve_indirect (obj
), 0);
401 if (fclose (f
)) err (1, "fclose on memstream failed");
402 printd (state
.sock
, "i %.*s", size
, buf
);
406 printd (state
.sock
, "i error opening memstream: %s\n",
413 static int readlen (int fd
)
418 n
= recv (fd
, p
, 4, 0);
420 if (!n
) errx (1, "EOF while reading length");
421 sockerr (1, "recv " FMT_ss
, n
);
424 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
427 static void unlinkpage (struct page
*page
)
431 for (i
= 0; i
< page
->slicecount
; ++i
) {
432 struct slice
*s
= &page
->slices
[i
];
433 if (s
->texindex
!= -1) {
434 if (state
.texowners
[s
->texindex
].slice
== s
) {
435 state
.texowners
[s
->texindex
].slice
= NULL
;
436 ARSERT (state
.texowners
[s
->texindex
].w
== s
->w
);
437 ARSERT (state
.texowners
[s
->texindex
].h
>= s
->h
);
443 static void freepage (struct page
*page
)
445 fz_drop_pixmap (page
->pixmap
);
450 fz_free_text_span (page
->text
);
452 if (page
->drawpage
) {
453 pdf_free_page (page
->drawpage
);
459 static void subdivide (struct page
*p
)
462 int h
= p
->pixmap
->h
;
463 int th
= MIN (h
, state
.sliceheight
);
465 for (i
= 0; i
< p
->slicecount
; ++i
) {
466 struct slice
*s
= &p
->slices
[i
];
474 static int compatpdims (struct pagedim
*p1
, struct pagedim
*p2
)
476 return p1
->rotate
== p2
->rotate
477 && !memcmp (&p1
->bbox
, &p2
->bbox
, sizeof (p1
->bbox
))
478 && !memcmp (&p1
->ctm
, &p2
->ctm
, sizeof (p1
->ctm
));
484 static int cacheline32bytes
;
486 static void __attribute__ ((constructor
)) clcheck (void)
488 char **envp
= environ
;
493 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
495 cacheline32bytes
= auxv
[1] == 32;
501 static void __attribute__ ((optimize ("O"))) clearpixmap (fz_pixmap
*pixmap
)
503 if (cacheline32bytes
) {
504 intptr_t a1
, a2
, diff
;
505 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
506 vector
unsigned char v
= vec_splat_u8 (-1);
507 vector
unsigned char *p
;
509 a1
= a2
= (intptr_t) pixmap
->samples
;
510 a2
= (a1
+ 31) & ~31;
515 while (a1
!= a2
) *(char *) a1
++ = 0xff;
516 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
517 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
519 vec_st (v
, i
+ 16, p
);
521 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
523 else fz_clear_pixmap_with_color (pixmap
, 0xff);
526 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
529 static void *render (int pageno
, int pindex
)
533 struct page
*page
= NULL
;
537 struct pagedim
*pagedim
;
540 printd (state
.sock
, "V rendering %d", pageno
);
542 pagedim
= &state
.pagedims
[pindex
];
543 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
544 + state
.sliceheight
- 1) / state
.sliceheight
;
545 slicecount
+= slicecount
== 0;
548 if (compatpdims (&state
.pig
->pagedim
, pagedim
)) {
551 fz_free_text_span (page
->text
);
554 if (page
->drawpage
) {
555 pdf_free_page (page
->drawpage
);
556 page
->drawpage
= NULL
;
560 freepage (state
.pig
);
564 page
= calloc (sizeof (*page
)
565 + (slicecount
* sizeof (struct slice
)), 1);
567 err (1, "calloc page %d\n", pageno
);
569 page
->pixmap
= fz_new_pixmap_with_rect (fz_device_rgb
, pagedim
->bbox
);
572 page
->slicecount
= slicecount
;
574 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
- 1);
578 clearpixmap (page
->pixmap
);
580 idev
= fz_new_draw_device (state
.cache
, page
->pixmap
);
582 die (fz_throw ("fz_newdrawdevice failed"));
583 error
= pdf_run_page (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
585 die (fz_rethrow (error
, "pdf_runpage failed"));
586 fz_free_device (idev
);
588 page
->drawpage
= drawpage
;
589 page
->pagedim
= *pagedim
;
590 page
->pageno
= pageno
;
594 pdf_age_store (state
.xref
->store
, 3);
596 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
601 static void initpdims (void)
607 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
611 fz_obj
*obj
, *pageobj
;
613 pageobj
= state
.xref
->page_objs
[pageno
];
615 obj
= fz_dict_gets (pageobj
, "CropBox");
616 if (!fz_is_array (obj
)) {
617 obj
= fz_dict_gets (pageobj
, "MediaBox");
618 if (!fz_is_array (obj
)) {
619 die (fz_throw ("cannot find page bounds %d (%d Rd)",
620 fz_to_num (pageobj
), fz_to_gen (pageobj
)));
623 box
= pdf_to_rect (obj
);
625 obj
= fz_dict_gets (pageobj
, "Rotate");
626 if (fz_is_int (obj
)) {
627 rotate
= fz_to_int (obj
);
632 rotate
+= state
.rotate
;
634 p
= &state
.pagedims
[state
.pagedimcount
- 1];
635 if ((state
.pagedimcount
== 0)
636 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
639 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
640 state
.pagedims
= realloc (state
.pagedims
, size
);
641 if (!state
.pagedims
) {
642 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
643 size
, state
.pagedimcount
+ 1);
645 p
= &state
.pagedims
[state
.pagedimcount
++];
652 printd (state
.sock
, "T Processed %d pages in %f seconds",
653 state
.pagecount
, end
- start
);
656 static void layout (void)
661 double zoom
, w
, maxw
= 0;
662 struct pagedim
*p
= state
.pagedims
;
664 if (state
.proportional
) {
665 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
666 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
667 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
668 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
669 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
672 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
673 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
674 box2
= fz_transform_rect (ctm
, box
);
675 w
= box2
.x1
- box2
.x0
;
676 maxw
= MAX (w
, maxw
);
681 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
682 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
683 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
684 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
685 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
688 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
689 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
690 box2
= fz_transform_rect (ctm
, box
);
691 w
= box2
.x1
- box2
.x0
;
693 if (state
.proportional
) {
694 double scale
= w
/ maxw
;
695 zoom
= (state
.w
/ w
) * scale
;
701 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
702 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
703 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
704 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
705 p
->bbox
= fz_round_rect (fz_transform_rect (ctm
, box
));
706 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
707 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
710 while (p
-- != state
.pagedims
) {
711 printd (state
.sock
, "l %d %d %d %d",
712 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
,
717 static void recurse_outline (pdf_outline
*outline
, int level
)
725 if (!outline
->link
) goto next
;
727 obj
= outline
->link
->dest
;
728 if (fz_is_indirect (obj
)) {
729 obj
= fz_resolve_indirect (obj
);
731 if (fz_is_array (obj
)) {
734 obj2
= fz_array_get (obj
, 0);
735 if (fz_is_int (obj2
)) {
736 pageno
= fz_to_int (obj2
);
739 pageno
= pdf_find_page_number (state
.xref
, obj2
);
742 if (fz_array_len (obj
) > 3) {
746 xo
= fz_array_get (obj
, 2);
747 yo
= fz_array_get (obj
, 3);
748 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
749 struct pagedim
*pagedim
= state
.pagedims
;
751 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
752 if (state
.pagedims
[i
].pageno
> pageno
)
754 pagedim
= &state
.pagedims
[i
];
756 p
.x
= fz_to_int (xo
);
757 p
.y
= fz_to_int (yo
);
758 p
= fz_transform_point (pagedim
->ctm
, p
);
759 h
= pagedim
->bbox
.y1
- pagedim
->bbox
.y0
;
765 pageno
= pdf_find_page_number (state
.xref
, outline
->link
->dest
);
768 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
770 printd (state
.sock
, "o %d %d %d %d %s",
771 level
, pageno
, top
, h
, outline
->title
);
774 if (outline
->child
) {
775 recurse_outline (outline
->child
, level
+ 1);
777 outline
= outline
->next
;
781 static void process_outline (void)
783 pdf_outline
*outline
;
785 if (!state
.needoutline
) return;
787 state
.needoutline
= 0;
788 outline
= pdf_load_outline (state
.xref
);
790 recurse_outline (outline
, 0);
791 pdf_free_outline (outline
);
795 static int comparespans (const void *l
, const void *r
)
797 fz_text_span
const *const*ls
= l
;
798 fz_text_span
const *const*rs
= r
;
799 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
802 /* wishful thinking function */
803 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
813 fz_text_span
*text
, *span
, **pspan
;
814 struct pagedim
*pdim
, *pdimprev
;
821 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
823 pdf_age_store (state
.xref
->store
, 3);
825 if (hasdata (state
.sock
)) {
826 printd (state
.sock
, "T attention requested aborting search at %d",
831 printd (state
.sock
, "T searching in page %d", pageno
);
835 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
836 pdim
= &state
.pagedims
[i
];
837 if (pdim
->pageno
== pageno
) {
840 if (pdim
->pageno
> pageno
) {
849 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
);
853 ctm
= fz_rotate (pdim
->rotate
);
855 text
= fz_new_text_span ();
856 tdev
= fz_new_text_device (text
);
857 error
= pdf_run_page (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
858 if (error
) die (error
);
859 fz_free_device (tdev
);
862 for (span
= text
; span
; span
= span
->next
) {
865 pspan
= malloc (sizeof (void *) * nspans
);
867 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
869 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
872 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
874 j
= forward
? 0 : nspans
- 1;
879 j
+= forward
? 1 : -1;
881 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
883 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
888 if (span
->text
[i
].bbox
.y0
> y
- 1) {
892 if (span
->text
[i
].c
< 256) {
893 *p
++ = span
->text
[i
].c
;
904 ret
= regexec (re
, buf
, 1, &rm
, 0);
906 if (ret
!= REG_NOMATCH
) {
909 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
911 "T regexec error `%.*s'",
913 fz_free_text_span (text
);
914 pdf_free_page (drawpage
);
922 fz_point p1
, p2
, p3
, p4
;
924 xoff
= pdim
->left
- pdim
->bbox
.x0
;
925 yoff
= -pdim
->bbox
.y0
;
927 sb
= &span
->text
[rm
.rm_so
].bbox
;
928 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
939 p1
= fz_transform_point (ctm
, p1
);
940 p2
= fz_transform_point (ctm
, p2
);
941 p3
= fz_transform_point (ctm
, p3
);
942 p4
= fz_transform_point (ctm
, p4
);
945 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
947 p1
.x
+ xoff
, p1
.y
+ yoff
,
948 p2
.x
+ xoff
, p2
.y
+ yoff
,
949 p3
.x
+ xoff
, p3
.y
+ yoff
,
950 p4
.x
+ xoff
, p4
.y
+ yoff
);
952 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
953 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
957 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
959 p1
.x
+ xoff
, p1
.y
+ yoff
,
960 p2
.x
+ xoff
, p2
.y
+ yoff
,
961 p3
.x
+ xoff
, p3
.y
+ yoff
,
962 p4
.x
+ xoff
, p4
.y
+ yoff
);
975 fz_free_text_span (text
);
976 pdf_free_page (drawpage
);
981 printd (state
.sock
, "T no matches %f sec", end
- start
);
983 printd (state
.sock
, "D");
992 mainloop (void *unused
)
995 int len
, ret
, oldlen
= 0;
998 len
= readlen (state
.sock
);
1000 errx (1, "readlen returned 0");
1003 if (oldlen
< len
+ 1) {
1004 p
= realloc (p
, len
+ 1);
1006 err (1, "realloc %d failed", len
+ 1);
1010 readdata (state
.sock
, p
, len
);
1013 if (!strncmp ("open", p
, 4)) {
1017 char *filename
= p
+ 5;
1019 filenamelen
= strlen (filename
);
1020 password
= filename
+ filenamelen
+ 1;
1022 openxref (filename
, password
);
1025 obj
= fz_dict_gets (state
.xref
->trailer
, "Info");
1029 obj
= fz_dict_gets (obj
, "Title");
1030 s
= pdf_to_utf8 (obj
);
1032 printd (state
.sock
, "t %s", s
);
1037 state
.needoutline
= 1;
1039 else if (!strncmp ("free", p
, 4)) {
1042 ret
= sscanf (p
+ 4, " %p", &ptr
);
1044 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
1049 else if (!strncmp ("search", p
, 6)) {
1050 int icase
, pageno
, y
, ret
, len2
, forward
;
1054 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1055 &icase
, &pageno
, &y
, &forward
, &len2
);
1057 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1060 pattern
= p
+ 6 + len2
;
1061 ret
= regcomp (&re
, pattern
,
1062 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1067 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1068 printd (state
.sock
, "T regcomp failed `%.*s'",
1069 (int) size
, errbuf
);
1072 search (&re
, pageno
, y
, forward
);
1076 else if (!strncmp ("geometry", p
, 8)) {
1079 printd (state
.sock
, "c");
1080 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1082 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1090 for (i
= 0; i
< state
.texcount
; ++i
) {
1091 state
.texowners
[i
].slice
= NULL
;
1096 unlock ("geometry");
1097 printd (state
.sock
, "C %d", state
.pagecount
);
1099 else if (!strncmp ("reinit", p
, 6)) {
1103 printd (state
.sock
, "c");
1104 ret
= sscanf (p
+ 6, " %f %d", &rotate
, &proportional
);
1106 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
1109 state
.rotate
= rotate
;
1110 state
.proportional
= proportional
;
1111 state
.pagedimcount
= 0;
1112 free (state
.pagedims
);
1113 state
.pagedims
= NULL
;
1118 freepage (state
.pig
);
1122 printd (state
.sock
, "C %d", state
.pagecount
);
1124 else if (!strncmp ("render", p
, 6)) {
1125 int pageno
, pindex
, w
, h
, ret
;
1128 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
1130 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1134 page
= render (pageno
, pindex
);
1137 printd (state
.sock
, "r %d %d %d %d %d %d %p",
1146 else if (!strncmp ("info", p
, 4)) {
1149 else if (!strncmp ("interrupt", p
, 9)) {
1150 printd (state
.sock
, "V interrupted");
1153 errx (1, "unknown command %.*s", len
, p
);
1159 static void showsel (struct page
*page
, int oy
)
1164 struct mark first
, last
;
1166 first
= page
->fmark
;
1169 if (!first
.span
|| !last
.span
) return;
1171 glEnable (GL_BLEND
);
1172 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1173 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1175 ox
= -page
->pixmap
->x
+ page
->pagedim
.left
;
1176 oy
= -page
->pixmap
->y
+ oy
;
1177 for (span
= first
.span
; span
; span
= span
->next
) {
1180 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1185 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1186 j
= MIN (first
.i
, last
.i
);
1187 k
= MAX (first
.i
, last
.i
);
1189 else if (span
== first
.span
) {
1192 else if (span
== last
.span
) {
1196 for (i
= j
; i
<= k
; ++i
) {
1197 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1199 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1206 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1208 if (span
== last
.span
) break;
1210 glDisable (GL_BLEND
);
1213 static void highlightlinks (struct page
*page
, int yoff
)
1218 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1219 glEnable (GL_LINE_STIPPLE
);
1220 glLineStipple (0.5, 0xcccc);
1222 xoff
= page
->pagedim
.left
- page
->pixmap
->x
;
1223 yoff
-= page
->pixmap
->y
;
1226 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1227 fz_point p1
, p2
, p3
, p4
;
1228 fz_matrix ctm
= page
->pagedim
.ctm
;
1230 p1
.x
= link
->rect
.x0
;
1231 p1
.y
= link
->rect
.y0
;
1233 p2
.x
= link
->rect
.x1
;
1234 p2
.y
= link
->rect
.y0
;
1236 p3
.x
= link
->rect
.x1
;
1237 p3
.y
= link
->rect
.y1
;
1239 p4
.x
= link
->rect
.x0
;
1240 p4
.y
= link
->rect
.y1
;
1242 p1
= fz_transform_point (ctm
, p1
);
1243 p2
= fz_transform_point (ctm
, p2
);
1244 p3
= fz_transform_point (ctm
, p3
);
1245 p4
= fz_transform_point (ctm
, p4
);
1247 switch (link
->kind
) {
1248 case PDF_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1249 case PDF_LINK_URI
: glColor3ub (0, 0, 255); break;
1250 default: glColor3ub (0, 0, 0); break;
1253 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1254 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1255 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1256 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1260 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1261 glDisable (GL_LINE_STIPPLE
);
1264 static void upload2 (struct page
*page
, int slicenum
, const char *cap
)
1269 struct slice
*slice
= &page
->slices
[slicenum
];
1271 w
= page
->pixmap
->w
;
1273 ARSERT (w
== slice
->w
);
1274 if (slice
->texindex
!= -1
1275 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1276 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1280 int index
= (state
.texindex
++ % state
.texcount
);
1283 for (i
= 0; i
< slicenum
; ++i
) {
1284 offset
+= w
* page
->slices
[i
].h
* 4;
1287 if (state
.texowners
[index
].w
== slice
->w
) {
1288 if (state
.texowners
[index
].h
>= slice
->h
) {
1292 state
.texowners
[index
].h
= slice
->h
;
1296 state
.texowners
[index
].h
= slice
->h
;
1299 state
.texowners
[index
].slice
= slice
;
1300 state
.texowners
[index
].w
= slice
->w
;
1301 slice
->texindex
= index
;
1303 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1307 GLenum err
= glGetError ();
1308 if (err
!= GL_NO_ERROR
) {
1309 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1313 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1321 page
->pixmap
->samples
+ offset
1324 GLenum err
= glGetError ();
1325 if (err
!= GL_NO_ERROR
) {
1326 printf ("\033[0;31mERROR %d %d %#x\033[0m\n", w
, slice
->h
, err
);
1332 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1340 page
->pixmap
->samples
+ offset
1347 lprintf ("%s[%d] slice=%d(%d,%d) texid=%d %f sec\n",
1348 subimage
? "sub" : "img",
1349 page
->pageno
, slicenum
,
1351 state
.texids
[slice
->texindex
],
1356 CAMLprim value
ml_draw (value args_v
, value ptr_v
)
1358 CAMLparam2 (args_v
, ptr_v
);
1359 int dispy
= Int_val (Field (args_v
, 0));
1360 int w
= Int_val (Field (args_v
, 1));
1361 int h
= Int_val (Field (args_v
, 2));
1362 int py
= Int_val (Field (args_v
, 3));
1363 int hlinks
= Bool_val (Field (args_v
, 4));
1364 char *s
= String_val (ptr_v
);
1369 int yoff
= dispy
- py
;
1371 ret
= sscanf (s
, "%p", &ptr
);
1373 errx (1, "cannot parse pointer `%s'", s
);
1377 w
= page
->pixmap
->w
;
1379 ARSERT (h
>= 0 && "ml_draw wrong h");
1380 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1382 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1384 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1385 struct slice
*slice
= &page
->slices
[slicenum
];
1386 if (slice
->h
> py
) {
1392 h
= MIN (state
.h
, h
);
1395 struct slice
*slice
= &page
->slices
[slicenum
];
1397 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1399 th
= MIN (h
, slice
->h
- py
);
1400 upload2 (page
, slicenum
, "upload");
1402 left
= page
->pagedim
.left
;
1405 glTexCoord2i (0, py
);
1406 glVertex2i (left
, dispy
);
1408 glTexCoord2i (w
, py
);
1409 glVertex2i (left
+w
, dispy
);
1411 glTexCoord2i (w
, py
+th
);
1412 glVertex2i (left
+w
, dispy
+ th
);
1414 glTexCoord2i (0, py
+th
);
1415 glVertex2i (left
, dispy
+ th
);
1425 showsel (page
, yoff
);
1426 if (hlinks
) highlightlinks (page
, yoff
);
1427 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1429 CAMLreturn (Val_unit
);
1432 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1438 p
.x
= x
+ page
->pixmap
->x
;
1439 p
.y
= y
+ page
->pixmap
->y
;
1441 ctm
= fz_invert_matrix (page
->pagedim
.ctm
);
1442 p
= fz_transform_point (ctm
, p
);
1444 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1445 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1446 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1454 static void ensuretext (struct page
*page
)
1460 page
->text
= fz_new_text_span ();
1461 tdev
= fz_new_text_device (page
->text
);
1462 error
= pdf_run_page (state
.xref
, page
->drawpage
, tdev
,
1464 if (error
) die (error
);
1465 fz_free_device (tdev
);
1469 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1471 CAMLparam3 (ptr_v
, x_v
, y_v
);
1472 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1475 char *s
= String_val (ptr_v
);
1477 ret_v
= Val_int (0);
1478 if (trylock ("ml_whatsunder")) {
1482 page
= parse_pointer ("ml_whatsunder", s
);
1483 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1485 switch (link
->kind
) {
1496 if (fz_is_array (link
->dest
)) {
1497 obj
= fz_array_get (link
->dest
, 0);
1498 if (fz_is_indirect (obj
)) {
1499 pageno
= pdf_find_page_number (state
.xref
, obj
);
1501 else if (fz_is_int (obj
)) {
1502 pageno
= fz_to_int (obj
);
1505 if (fz_array_len (link
->dest
) > 3) {
1508 xo
= fz_array_get (link
->dest
, 2);
1509 yo
= fz_array_get (link
->dest
, 3);
1510 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
1511 p
.x
= fz_to_int (xo
);
1512 p
.y
= fz_to_int (yo
);
1513 p
= fz_transform_point (page
->pagedim
.ctm
, p
);
1518 pageno
= pdf_find_page_number (state
.xref
, link
->dest
);
1520 tup_v
= caml_alloc_tuple (2);
1521 ret_v
= caml_alloc_small (1, 1);
1522 Field (tup_v
, 0) = Val_int (pageno
);
1523 Field (tup_v
, 1) = Val_int (p
.y
);
1524 Field (ret_v
, 0) = tup_v
;
1529 str_v
= caml_copy_string (fz_to_str_buf (link
->dest
));
1530 ret_v
= caml_alloc_small (1, 0);
1531 Field (ret_v
, 0) = str_v
;
1535 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1544 x
= Int_val (x_v
) + page
->pixmap
->x
;
1545 y
= Int_val (y_v
) + page
->pixmap
->y
;
1547 for (span
= page
->text
; span
; span
= span
->next
) {
1548 for (i
= 0; i
< span
->len
; ++i
) {
1550 b
= &span
->text
[i
].bbox
;
1551 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1553 span
->font
&& span
->font
->name
1555 : "Span has no font name"
1557 #ifdef FT_FREETYPE_H
1558 FT_FaceRec
*face
= span
->font
->ft_face
;
1559 if (face
&& face
->family_name
) {
1561 char *n1
= face
->family_name
;
1562 size_t l1
= strlen (n1
);
1563 size_t l2
= strlen (n2
);
1565 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
1566 s
= malloc (l1
+ l2
+ 2);
1570 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
1571 str_v
= caml_copy_string (s
);
1577 str_v
= caml_copy_string (n2
);
1580 str_v
= caml_copy_string (n2
);
1582 ret_v
= caml_alloc_small (1, 2);
1583 Field (ret_v
, 0) = str_v
;
1590 unlock ("ml_whatsunder");
1596 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
, value oy_v
)
1598 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1602 struct mark first
, last
;
1603 int i
, x0
, x1
, y0
, y1
, oy
, left
;
1604 char *s
= String_val (ptr_v
);
1606 if (trylock ("ml_seltext")) {
1610 page
= parse_pointer ("ml_seltext", s
);
1613 oy
= Int_val (oy_v
);
1614 x0
= Int_val (Field (rect_v
, 0));
1615 y0
= Int_val (Field (rect_v
, 1));
1616 x1
= Int_val (Field (rect_v
, 2));
1617 y1
= Int_val (Field (rect_v
, 3));
1619 left
= page
->pagedim
.left
;
1621 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1622 glColor3ub (128, 128, 128);
1623 glRecti (x0
, y0
, x1
, y1
);
1624 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1627 x0
+= page
->pixmap
->x
- left
;
1628 y0
+= page
->pixmap
->y
- oy
;
1629 x1
+= page
->pixmap
->x
- left
;
1630 y1
+= page
->pixmap
->y
- oy
;
1635 last
.i
= first
.i
= 0;
1636 first
.span
= page
->text
;
1637 for (span
= page
->text
; span
; span
= span
->next
) {
1638 for (i
= 0; i
< span
->len
; ++i
) {
1639 b
= &span
->text
[i
].bbox
;
1642 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1647 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1652 if (0 && selected
) {
1653 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1654 glColor3ub (128, 128, 128);
1655 glRecti (b
->x0
+left
, b
->y0
, b
->x1
+left
, b
->y1
);
1656 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1661 if (y1
< y0
|| x1
< x0
) {
1664 if (first
.span
== last
.span
) {
1669 for (span
= first
.span
; span
&& span
!= last
.span
;
1670 span
= span
->next
) {
1683 first
.span
= last
.span
;
1689 page
->fmark
= first
;
1692 unlock ("ml_seltext");
1695 CAMLreturn (Val_unit
);
1698 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
1703 for (i
= a
; i
<= b
; ++i
) {
1704 len
= runetochar (buf
, &span
->text
[i
].c
);
1705 ret
= fwrite (buf
, len
, 1, f
);
1708 printd (state
.sock
, "T failed to write %d bytes ret=%d: %s",
1709 len
, ret
, strerror (errno
));
1716 CAMLprim value
ml_copysel (value ptr_v
)
1721 char *s
= String_val (ptr_v
);
1723 if (trylock ("ml_copysel")) {
1730 if (state
.xselpipe
) {
1731 int ret
= pclose (state
.xselpipe
);
1733 printd (state
.sock
, "T failed to close xsel pipe `%s'",
1736 state
.xselpipe
= NULL
;
1739 printf ("========================================\n");
1745 page
= parse_pointer ("ml_sopysel", s
);
1747 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1748 printd (state
.sock
, "T nothing to copy");
1754 if (!state
.xselpipe
) {
1755 state
.xselpipe
= popen ("xsel -i", "w");
1756 if (!state
.xselpipe
) {
1757 printd (state
.sock
, "T failed to open xsel pipe `%s'",
1769 for (span
= page
->fmark
.span
;
1770 span
&& span
!= page
->lmark
.span
->next
;
1771 span
= span
->next
) {
1772 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1773 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1774 if (pipespan (f
, span
, a
, b
)) {
1778 if (putc ('\n', f
) == EOF
) {
1779 printd (state
.sock
, "T failed break line on xsel pipe `%s'",
1785 page
->lmark
.span
= NULL
;
1786 page
->fmark
.span
= NULL
;
1790 unlock ("ml_copysel");
1793 CAMLreturn (Val_unit
);
1796 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
1798 CAMLparam1 (pagedimno_v
);
1800 int pagedimno
= Int_val (pagedimno_v
);
1803 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1804 if (trylock ("ml_getpdimrect")) {
1805 box
= fz_empty_rect
;
1808 box
= state
.pagedims
[pagedimno
].box
;
1809 unlock ("ml_getpdimrect");
1812 Store_double_field (ret_v
, 0, box
.x0
);
1813 Store_double_field (ret_v
, 1, box
.x1
);
1814 Store_double_field (ret_v
, 2, box
.y0
);
1815 Store_double_field (ret_v
, 3, box
.y1
);
1820 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
1822 CAMLparam3 (winw_v
, winh_v
, dw_v
);
1826 double maxw
= 0.0, maxh
= 0.0;
1828 double winw
= Int_val (winw_v
);
1829 double winh
= Int_val (winh_v
);
1830 double dw
= Int_val (dw_v
);
1831 double pw
= 1.0, ph
= 1.0, num
, den
;
1833 if (trylock ("ml_zoom_for_height")) {
1837 if (state
.proportional
) {
1838 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1841 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1842 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1845 maxw
= MAX (w
, maxw
);
1849 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1850 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
1852 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1853 y0
= MIN (p
->box
.y0
, p
->box
.y1
);
1854 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1855 y1
= MAX (p
->box
.y0
, p
->box
.y1
);
1860 if (state
.proportional
) {
1862 scaledh
= h
* scale
;
1869 if (scaledh
> maxh
) {
1876 num
= (winh
* pw
) + (ph
* dw
);
1880 unlock ("ml_zoom_for_height");
1882 ret_v
= caml_copy_double (zoom
);
1886 CAMLprim value
ml_init (value sock_v
, value params_v
)
1891 CAMLparam2 (sock_v
, params_v
);
1893 state
.rotate
= Int_val (Field (params_v
, 0));
1894 state
.proportional
= Bool_val (Field (params_v
, 1));
1895 state
.texcount
= Int_val (Field (params_v
, 2));
1896 state
.sliceheight
= Int_val (Field (params_v
, 3));
1897 state
.texform
= GL_RGBA
;
1898 state
.texty
= GL_UNSIGNED_BYTE
;
1901 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1902 if (!state
.texids
) {
1903 err (1, "calloc texids " FMT_s
,
1904 state
.texcount
* sizeof (*state
.texids
));
1907 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
1908 if (!state
.texowners
) {
1909 err (1, "calloc texowners " FMT_s
,
1910 state
.texcount
* sizeof (*state
.texowners
));
1913 glGenTextures (state
.texcount
, state
.texids
);
1916 state
.sock
= Socket_val (sock_v
);
1918 state
.sock
= Int_val (sock_v
);
1922 InitializeCriticalSection (&critsec
);
1923 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
1924 if (state
.thread
== INVALID_HANDLE_VALUE
) {
1925 errx (1, "CreateThread failed: %lx", GetLastError ());
1928 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
1930 errx (1, "pthread_create: %s", strerror (ret
));
1934 CAMLreturn (Val_unit
);