1 /* lots of code c&p-ed directly from mupdf */
8 #define WIN32_LEAN_AND_MEAN
11 #define fionread_arg long
19 #pragma warning (disable:4244)
20 #pragma warning (disable:4996)
21 #pragma warning (disable:4995)
25 #define NORETURN __declspec (noreturn)
27 #elif defined __GNUC__
28 #define NORETURN __attribute__ ((noreturn))
29 #define UNUSED __attribute__ ((unused))
36 static void __declspec (noreturn
) sockerr (int exitcode
, const char *fmt
, ...)
41 vfprintf (stderr
, fmt
, ap
);
43 fprintf (stderr
, ": wsaerror 0x%x\n", WSAGetLastError ());
49 #define fionread_arg int
50 #define ioctlsocket ioctl
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <sys/ioctl.h>
68 static void NORETURN
err (int exitcode
, const char *fmt
, ...)
75 vfprintf (stderr
, fmt
, ap
);
77 fprintf (stderr
, ": %s\n", strerror (savederrno
));
82 static void NORETURN
errx (int exitcode
, const char *fmt
, ...)
87 vfprintf (stderr
, fmt
, ap
);
95 #include <OpenGL/gl.h>
100 #ifndef GL_TEXTURE_RECTANGLE_ARB
101 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
104 #include <caml/fail.h>
105 #include <caml/alloc.h>
106 #include <caml/memory.h>
107 #include <caml/unixsupport.h>
113 #define lprintf printf
119 #include FT_FREETYPE_H
122 #define ARSERT(cond) for (;;) { \
124 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
138 struct block
*blocks
;
157 struct pagedim pagedim
;
162 struct slice
*slices
;
165 #if !defined _WIN32 && !defined __APPLE__
171 int sliceheight
, blockwidth
;
173 struct pagedim
*pagedims
;
177 fz_glyph_cache
*cache
;
207 static CRITICAL_SECTION critsec
;
209 static void lock (void *unused
)
212 EnterCriticalSection (&critsec
);
215 static void unlock (void *unused
)
218 LeaveCriticalSection (&critsec
);
221 static int trylock (void *unused
)
223 return TryEnterCriticalSection (&critsec
) == 0;
226 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
228 static void lock (const char *cap
)
230 int ret
= pthread_mutex_lock (&mutex
);
232 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
236 static void unlock (const char *cap
)
238 int ret
= pthread_mutex_unlock (&mutex
);
240 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
244 static int trylock (const char *cap
)
246 int ret
= pthread_mutex_trylock (&mutex
);
248 if (ret
&& ret
!= EBUSY
) {
249 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
255 static void *parse_pointer (const char *cap
, const char *s
)
260 ret
= sscanf (s
, "%p", &ptr
);
262 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
267 static int hasdata (int sock
)
271 ret
= ioctlsocket (sock
, FIONREAD
, &avail
);
272 if (ret
) sockerr (1, "hasdata: FIONREAD error ret=%d", ret
);
276 static double now (void)
280 if (gettimeofday (&tv
, NULL
)) {
281 err (1, "gettimeofday");
283 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
286 static void readdata (int fd
, char *p
, int size
)
290 n
= recv (fd
, p
, size
, 0);
292 if (!n
) errx (1, "EOF while reading");
293 sockerr (1, "recv (req %d, ret " FMT_ss
")", size
, n
);
297 static void writedata (int fd
, char *p
, int size
)
302 buf
[0] = (size
>> 24) & 0xff;
303 buf
[1] = (size
>> 16) & 0xff;
304 buf
[2] = (size
>> 8) & 0xff;
305 buf
[3] = (size
>> 0) & 0xff;
307 n
= send (fd
, buf
, 4, 0);
309 if (!n
) errx (1, "EOF while writing length");
310 sockerr (1, "send " FMT_ss
, n
);
313 n
= send (fd
, p
, size
, 0);
315 if (!n
) errx (1, "EOF while writing data");
316 sockerr (1, "send (req %d, ret " FMT_ss
")", size
, n
);
322 __attribute__ ((format (printf
, 2, 3)))
324 printd (int fd
, const char *fmt
, ...)
332 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
335 len
= vsnprintf (buf
, size
, fmt
, ap
);
338 if (len
> -1 && len
< size
) {
339 writedata (fd
, buf
, len
);
349 buf
= realloc (buf
, size
);
354 static void die (fz_error error
)
356 fz_catch (error
, "aborting");
358 pdf_free_xref (state
.xref
);
362 static void openxref (char *filename
, char *password
)
367 for (i
= 0; i
< state
.texcount
; ++i
) {
368 state
.texowners
[i
].block
= NULL
;
372 fz_free_glyph_cache (state
.cache
);
375 state
.cache
= fz_new_glyph_cache ();
377 errx (1, "fz_newglyph_cache failed");
381 if (state
.xref
->store
) {
382 pdf_free_store (state
.xref
->store
);
383 state
.xref
->store
= NULL
;
385 pdf_free_xref (state
.xref
);
389 if (state
.pagedims
) {
390 free (state
.pagedims
);
391 state
.pagedims
= NULL
;
393 state
.pagedimcount
= 0;
395 error
= pdf_open_xref (&state
.xref
, filename
, password
);
400 error
= pdf_load_page_tree (state
.xref
);
405 state
.pagecount
= pdf_count_pages (state
.xref
);
408 static void pdfinfo (void)
412 printd (state
.sock
, "i PDF version\t%d.%d",
413 state
.xref
->version
/ 10, state
.xref
->version
% 10);
415 infoobj
= fz_dict_gets (state
.xref
->trailer
, "Info");
419 char *items
[] = { "Title", "Creator", "Producer", "CreationDate" };
421 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
422 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
423 s
= pdf_to_utf8 (obj
);
426 printd (state
.sock
, "t %s", s
);
428 printd (state
.sock
, "i %s\t%s", items
[i
], s
);
432 printd (state
.sock
, "ie");
436 static int readlen (int fd
)
441 n
= recv (fd
, p
, 4, 0);
443 if (!n
) errx (1, "EOF while reading length");
444 sockerr (1, "recv " FMT_ss
, n
);
447 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
450 static void unlinkpage (struct page
*page
)
454 for (i
= 0; i
< page
->slicecount
; ++i
) {
456 struct slice
*s
= &page
->slices
[i
];
458 for (j
= 0; j
< page
->blockcount
; ++j
) {
459 struct block
*b
= &s
->blocks
[j
];
461 if (b
->texindex
!= -1) {
462 if (state
.texowners
[b
->texindex
].block
== b
) {
463 state
.texowners
[b
->texindex
].block
= NULL
;
470 static void freepage (struct page
*page
)
474 fz_drop_pixmap (page
->pixmap
);
479 fz_free_text_span (page
->text
);
481 if (page
->drawpage
) {
482 pdf_free_page (page
->drawpage
);
484 for (i
= 0; i
< page
->slicecount
; ++i
) {
485 free (page
->slices
[i
].blocks
);
491 static void subdivide (struct page
*p
, int blockcount
)
494 int w
= p
->pixmap
->w
;
495 int h
= p
->pixmap
->h
;
497 int tw
= MIN (w
, state
.blockwidth
);
498 int th
= MIN (h
, state
.sliceheight
);
500 for (i
= 0; i
< p
->slicecount
; ++i
) {
502 struct slice
*s
= &p
->slices
[i
];
506 s
->blocks
= calloc (sizeof (struct block
), p
->blockcount
);
508 err (1, "calloc blocks page %d slice %d, %d",
509 p
->pageno
, i
, p
->blockcount
);
516 for (j
= 0; j
< blockcount
; ++j
) {
517 struct block
*b
= &s
->blocks
[j
];
527 offset
+= s
->h
* p
->pixmap
->w
* 4;
532 static int compatpdims (struct pagedim
*p1
, struct pagedim
*p2
)
534 return p1
->rotate
== p2
->rotate
535 && !memcmp (&p1
->bbox
, &p2
->bbox
, sizeof (p1
->bbox
))
536 && !memcmp (&p1
->ctm
, &p2
->ctm
, sizeof (p1
->ctm
));
542 static int cacheline32bytes
;
543 extern char **environ
;
545 static void __attribute__ ((constructor
)) clcheck (void)
547 char **envp
= environ
;
552 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
554 cacheline32bytes
= auxv
[1] == 32;
560 static void __attribute__ ((optimize ("O"))) clearpixmap (fz_pixmap
*pixmap
)
562 if (cacheline32bytes
) {
563 intptr_t a1
, a2
, diff
;
564 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
565 vector
unsigned char v
= vec_splat_u8 (-1);
566 vector
unsigned char *p
;
568 a1
= a2
= (intptr_t) pixmap
->samples
;
569 a2
= (a1
+ 31) & ~31;
574 while (a1
!= a2
) *(char *) a1
++ = 0xff;
575 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
576 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
578 vec_st (v
, i
+ 16, p
);
580 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
582 else fz_clear_pixmap_with_color (pixmap
, 0xff);
585 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
588 static void *render (int pageno
, int pindex
)
594 struct pagedim
*pagedim
;
595 struct page
*page
= NULL
;
596 int slicecount
, blockcount
;
599 printd (state
.sock
, "V rendering %d", pageno
);
601 pagedim
= &state
.pagedims
[pindex
];
602 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
603 + state
.sliceheight
- 1) / state
.sliceheight
;
604 slicecount
+= slicecount
== 0;
606 blockcount
= (pagedim
->bbox
.x1
- pagedim
->bbox
.x0
607 + state
.blockwidth
- 1) / state
.blockwidth
;
608 blockcount
+= blockcount
== 0;
611 if (compatpdims (&state
.pig
->pagedim
, pagedim
)) {
614 fz_free_text_span (page
->text
);
617 if (page
->drawpage
) {
618 pdf_free_page (page
->drawpage
);
619 page
->drawpage
= NULL
;
623 freepage (state
.pig
);
627 page
= calloc (sizeof (struct page
), 1);
629 err (1, "calloc page %d", pageno
);
631 page
->slices
= calloc (sizeof (struct slice
), slicecount
);
633 err (1, "calloc slices %d %d", pageno
, slicecount
);
635 page
->pixmap
= fz_new_pixmap_with_rect (fz_device_rgb
, pagedim
->bbox
);
638 page
->slicecount
= slicecount
;
639 page
->blockcount
= blockcount
;
641 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
- 1);
645 clearpixmap (page
->pixmap
);
647 idev
= fz_new_draw_device (state
.cache
, page
->pixmap
);
649 die (fz_throw ("fz_newdrawdevice failed"));
650 error
= pdf_run_page (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
652 die (fz_rethrow (error
, "pdf_runpage failed"));
653 fz_free_device (idev
);
655 page
->drawpage
= drawpage
;
656 page
->pagedim
= *pagedim
;
657 page
->pageno
= pageno
;
658 subdivide (page
, blockcount
);
661 pdf_age_store (state
.xref
->store
, 3);
663 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
668 static void initpdims (void)
674 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
678 fz_obj
*obj
, *pageobj
;
680 pageobj
= state
.xref
->page_objs
[pageno
];
682 obj
= fz_dict_gets (pageobj
, "CropBox");
683 if (!fz_is_array (obj
)) {
684 obj
= fz_dict_gets (pageobj
, "MediaBox");
685 if (!fz_is_array (obj
)) {
686 die (fz_throw ("cannot find page bounds %d (%d Rd)",
687 fz_to_num (pageobj
), fz_to_gen (pageobj
)));
690 box
= pdf_to_rect (obj
);
692 obj
= fz_dict_gets (pageobj
, "Rotate");
693 if (fz_is_int (obj
)) {
694 rotate
= fz_to_int (obj
);
699 rotate
+= state
.rotate
;
701 p
= &state
.pagedims
[state
.pagedimcount
- 1];
702 if ((state
.pagedimcount
== 0)
703 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
706 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
707 state
.pagedims
= realloc (state
.pagedims
, size
);
708 if (!state
.pagedims
) {
709 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
710 size
, state
.pagedimcount
+ 1);
712 p
= &state
.pagedims
[state
.pagedimcount
++];
719 printd (state
.sock
, "T Processed %d pages in %f seconds",
720 state
.pagecount
, end
- start
);
723 static void layout (void)
728 double zoom
, w
, maxw
= 0;
729 struct pagedim
*p
= state
.pagedims
;
731 if (state
.proportional
) {
732 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
733 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
734 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
735 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
736 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
739 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
740 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
741 box2
= fz_transform_rect (ctm
, box
);
742 w
= box2
.x1
- box2
.x0
;
743 maxw
= MAX (w
, maxw
);
748 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
749 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
750 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
751 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
752 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
755 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
756 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
757 box2
= fz_transform_rect (ctm
, box
);
758 w
= box2
.x1
- box2
.x0
;
760 if (state
.proportional
) {
761 double scale
= w
/ maxw
;
762 zoom
= (state
.w
/ w
) * scale
;
768 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
769 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
770 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
771 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
772 p
->bbox
= fz_round_rect (fz_transform_rect (ctm
, box
));
773 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
774 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
777 while (p
-- != state
.pagedims
) {
778 printd (state
.sock
, "l %d %d %d %d",
779 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
,
784 static void recurse_outline (pdf_outline
*outline
, int level
)
792 if (!outline
->link
) goto next
;
794 obj
= outline
->link
->dest
;
795 if (fz_is_indirect (obj
)) {
796 obj
= fz_resolve_indirect (obj
);
798 if (fz_is_array (obj
)) {
801 obj2
= fz_array_get (obj
, 0);
802 if (fz_is_int (obj2
)) {
803 pageno
= fz_to_int (obj2
);
806 pageno
= pdf_find_page_number (state
.xref
, obj2
);
809 if (fz_array_len (obj
) > 3) {
813 xo
= fz_array_get (obj
, 2);
814 yo
= fz_array_get (obj
, 3);
815 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
816 struct pagedim
*pagedim
= state
.pagedims
;
818 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
819 if (state
.pagedims
[i
].pageno
> pageno
)
821 pagedim
= &state
.pagedims
[i
];
823 p
.x
= fz_to_int (xo
);
824 p
.y
= fz_to_int (yo
);
825 p
= fz_transform_point (pagedim
->ctm
, p
);
826 h
= pagedim
->bbox
.y1
- pagedim
->bbox
.y0
;
832 pageno
= pdf_find_page_number (state
.xref
, outline
->link
->dest
);
835 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
837 printd (state
.sock
, "o %d %d %d %d %s",
838 level
, pageno
, top
, h
, outline
->title
);
841 if (outline
->child
) {
842 recurse_outline (outline
->child
, level
+ 1);
844 outline
= outline
->next
;
848 static void process_outline (void)
850 pdf_outline
*outline
;
852 if (!state
.needoutline
) return;
854 state
.needoutline
= 0;
855 outline
= pdf_load_outline (state
.xref
);
857 recurse_outline (outline
, 0);
858 pdf_free_outline (outline
);
862 static int comparespans (const void *l
, const void *r
)
864 fz_text_span
const *const*ls
= l
;
865 fz_text_span
const *const*rs
= r
;
866 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
869 /* wishful thinking function */
870 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
880 fz_text_span
*text
, *span
, **pspan
;
881 struct pagedim
*pdim
, *pdimprev
;
888 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
890 pdf_age_store (state
.xref
->store
, 3);
892 if (hasdata (state
.sock
)) {
893 printd (state
.sock
, "T attention requested aborting search at %d",
898 printd (state
.sock
, "T searching in page %d", pageno
);
902 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
903 pdim
= &state
.pagedims
[i
];
904 if (pdim
->pageno
== pageno
) {
907 if (pdim
->pageno
> pageno
) {
916 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
);
920 ctm
= fz_rotate (pdim
->rotate
);
922 text
= fz_new_text_span ();
923 tdev
= fz_new_text_device (text
);
924 error
= pdf_run_page (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
925 if (error
) die (error
);
926 fz_free_device (tdev
);
929 for (span
= text
; span
; span
= span
->next
) {
932 pspan
= malloc (sizeof (void *) * nspans
);
934 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
936 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
939 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
941 j
= forward
? 0 : nspans
- 1;
946 j
+= forward
? 1 : -1;
948 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
950 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
955 if (span
->text
[i
].bbox
.y0
> y
- 1) {
959 if (span
->text
[i
].c
< 256) {
960 *p
++ = span
->text
[i
].c
;
971 ret
= regexec (re
, buf
, 1, &rm
, 0);
973 if (ret
!= REG_NOMATCH
) {
976 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
978 "T regexec error `%.*s'",
980 fz_free_text_span (text
);
981 pdf_free_page (drawpage
);
989 fz_point p1
, p2
, p3
, p4
;
991 xoff
= pdim
->left
- pdim
->bbox
.x0
;
992 yoff
= -pdim
->bbox
.y0
;
994 sb
= &span
->text
[rm
.rm_so
].bbox
;
995 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1006 p1
= fz_transform_point (ctm
, p1
);
1007 p2
= fz_transform_point (ctm
, p2
);
1008 p3
= fz_transform_point (ctm
, p3
);
1009 p4
= fz_transform_point (ctm
, p4
);
1012 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
1014 p1
.x
+ xoff
, p1
.y
+ yoff
,
1015 p2
.x
+ xoff
, p2
.y
+ yoff
,
1016 p3
.x
+ xoff
, p3
.y
+ yoff
,
1017 p4
.x
+ xoff
, p4
.y
+ yoff
);
1019 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
1020 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
1024 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
1026 p1
.x
+ xoff
, p1
.y
+ yoff
,
1027 p2
.x
+ xoff
, p2
.y
+ yoff
,
1028 p3
.x
+ xoff
, p3
.y
+ yoff
,
1029 p4
.x
+ xoff
, p4
.y
+ yoff
);
1042 fz_free_text_span (text
);
1043 pdf_free_page (drawpage
);
1048 printd (state
.sock
, "T no matches %f sec", end
- start
);
1050 printd (state
.sock
, "D");
1059 mainloop (void *unused
)
1062 int len
, ret
, oldlen
= 0;
1065 len
= readlen (state
.sock
);
1067 errx (1, "readlen returned 0");
1070 if (oldlen
< len
+ 1) {
1071 p
= realloc (p
, len
+ 1);
1073 err (1, "realloc %d failed", len
+ 1);
1077 readdata (state
.sock
, p
, len
);
1080 if (!strncmp ("open", p
, 4)) {
1083 char *filename
= p
+ 5;
1085 filenamelen
= strlen (filename
);
1086 password
= filename
+ filenamelen
+ 1;
1088 openxref (filename
, password
);
1091 state
.needoutline
= 1;
1093 else if (!strncmp ("free", p
, 4)) {
1096 ret
= sscanf (p
+ 4, " %p", &ptr
);
1098 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
1103 else if (!strncmp ("search", p
, 6)) {
1104 int icase
, pageno
, y
, ret
, len2
, forward
;
1108 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1109 &icase
, &pageno
, &y
, &forward
, &len2
);
1111 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1114 pattern
= p
+ 6 + len2
;
1115 ret
= regcomp (&re
, pattern
,
1116 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1121 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1122 printd (state
.sock
, "T regcomp failed `%.*s'",
1123 (int) size
, errbuf
);
1126 search (&re
, pageno
, y
, forward
);
1130 else if (!strncmp ("geometry", p
, 8)) {
1133 printd (state
.sock
, "c");
1134 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1136 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1144 for (i
= 0; i
< state
.texcount
; ++i
) {
1145 state
.texowners
[i
].block
= NULL
;
1150 unlock ("geometry");
1151 printd (state
.sock
, "C %d", state
.pagecount
);
1153 else if (!strncmp ("reinit", p
, 6)) {
1157 printd (state
.sock
, "c");
1158 ret
= sscanf (p
+ 6, " %f %d", &rotate
, &proportional
);
1160 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
1163 state
.rotate
= rotate
;
1164 state
.proportional
= proportional
;
1165 state
.pagedimcount
= 0;
1166 free (state
.pagedims
);
1167 state
.pagedims
= NULL
;
1172 freepage (state
.pig
);
1176 printd (state
.sock
, "C %d", state
.pagecount
);
1178 else if (!strncmp ("render", p
, 6)) {
1179 int pageno
, pindex
, w
, h
, ret
;
1182 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
1184 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1188 page
= render (pageno
, pindex
);
1191 printd (state
.sock
, "r %d %d %d %d %d %d %p",
1200 else if (!strncmp ("interrupt", p
, 9)) {
1201 printd (state
.sock
, "V interrupted");
1204 errx (1, "unknown command %.*s", len
, p
);
1210 static void showsel (struct page
*page
, int oy
)
1215 struct mark first
, last
;
1217 first
= page
->fmark
;
1220 if (!first
.span
|| !last
.span
) return;
1222 glEnable (GL_BLEND
);
1223 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1224 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1226 ox
= -page
->pixmap
->x
+ page
->pagedim
.left
;
1227 oy
= -page
->pixmap
->y
+ oy
;
1228 for (span
= first
.span
; span
; span
= span
->next
) {
1231 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1236 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1237 j
= MIN (first
.i
, last
.i
);
1238 k
= MAX (first
.i
, last
.i
);
1240 else if (span
== first
.span
) {
1243 else if (span
== last
.span
) {
1247 for (i
= j
; i
<= k
; ++i
) {
1248 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1250 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1257 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1259 if (span
== last
.span
) break;
1261 glDisable (GL_BLEND
);
1264 static void highlightlinks (struct page
*page
, int yoff
)
1269 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1270 glEnable (GL_LINE_STIPPLE
);
1271 glLineStipple (0.5, 0xcccc);
1273 xoff
= page
->pagedim
.left
- page
->pixmap
->x
;
1274 yoff
-= page
->pixmap
->y
;
1277 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1278 fz_point p1
, p2
, p3
, p4
;
1279 fz_matrix ctm
= page
->pagedim
.ctm
;
1281 p1
.x
= link
->rect
.x0
;
1282 p1
.y
= link
->rect
.y0
;
1284 p2
.x
= link
->rect
.x1
;
1285 p2
.y
= link
->rect
.y0
;
1287 p3
.x
= link
->rect
.x1
;
1288 p3
.y
= link
->rect
.y1
;
1290 p4
.x
= link
->rect
.x0
;
1291 p4
.y
= link
->rect
.y1
;
1293 p1
= fz_transform_point (ctm
, p1
);
1294 p2
= fz_transform_point (ctm
, p2
);
1295 p3
= fz_transform_point (ctm
, p3
);
1296 p4
= fz_transform_point (ctm
, p4
);
1298 switch (link
->kind
) {
1299 case PDF_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1300 case PDF_LINK_URI
: glColor3ub (0, 0, 255); break;
1301 default: glColor3ub (0, 0, 0); break;
1304 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1305 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1306 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1307 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1311 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1312 glDisable (GL_LINE_STIPPLE
);
1315 static void upload (struct page
*page
, int slicenum
, int blocknum
)
1318 struct slice
*slice
= &page
->slices
[slicenum
];
1319 struct block
*block
= &slice
->blocks
[blocknum
];
1321 if (block
->texindex
!= -1
1322 && state
.texowners
[block
->texindex
].block
== block
) {
1323 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[block
->texindex
]);
1327 int index
= (state
.texindex
++ % state
.texcount
);
1329 if (state
.texowners
[index
].w
== block
->w
) {
1330 if (state
.texowners
[index
].h
>= slice
->h
) {
1334 state
.texowners
[index
].h
= slice
->h
;
1338 state
.texowners
[index
].h
= slice
->h
;
1341 state
.texowners
[index
].w
= block
->w
;
1342 state
.texowners
[index
].block
= block
;
1343 block
->texindex
= index
;
1345 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[block
->texindex
]);
1346 glPixelStorei (GL_UNPACK_ROW_LENGTH
, page
->pixmap
->w
);
1350 GLenum err
= glGetError ();
1351 if (err
!= GL_NO_ERROR
) {
1352 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n",
1353 block
->w
, slice
->h
, err
);
1358 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1366 page
->pixmap
->samples
+ block
->offset
1370 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1378 page
->pixmap
->samples
+ block
->offset
1382 GLenum err
= glGetError ();
1383 if (err
!= GL_NO_ERROR
) {
1384 printf ("\033[0;31mERROR %d %d %#x\033[0m\n",
1385 block
->w
, slice
->h
, err
);
1393 lprintf ("%s[%d] slice=%d,%d(%d,%d) tex=%d,%d offset=%d %f sec\n",
1394 subimage
? "sub" : "img",
1395 page
->pageno
, slicenum
, blocknum
,
1398 state
.texids
[block
->texindex
],
1404 CAMLprim value
ml_draw (value args_v
, value ptr_v
)
1406 CAMLparam2 (args_v
, ptr_v
);
1407 int dispy
= Int_val (Field (args_v
, 0));
1408 /* int w = Int_val (Field (args_v, 1)); */
1409 int h
= Int_val (Field (args_v
, 2));
1410 int py
= Int_val (Field (args_v
, 3));
1411 int hlinks
= Bool_val (Field (args_v
, 4));
1412 char *s
= String_val (ptr_v
);
1417 int yoff
= dispy
- py
;
1418 struct slice
*slice
;
1420 ret
= sscanf (s
, "%p", &ptr
);
1422 errx (1, "cannot parse pointer `%s'", s
);
1426 /* w = page->pixmap->w; */
1428 ARSERT (h
>= 0 && "ml_draw wrong h");
1429 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1431 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1433 for (slicenum
= 0; slicenum
< page
->slicecount
; ++slicenum
) {
1434 slice
= &page
->slices
[slicenum
];
1435 if (slice
->h
> py
) {
1440 h
= MIN (state
.h
, h
);
1443 int th
, left
, blocknum
;
1445 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1447 th
= MIN (h
, slice
->h
- py
);
1448 left
= page
->pagedim
.left
;
1450 for (blocknum
= 0; blocknum
< page
->blockcount
; ++blocknum
) {
1451 struct block
*block
= &slice
->blocks
[blocknum
];
1453 upload (page
, slicenum
, blocknum
);
1456 glTexCoord2i (0, py
);
1457 glVertex2i (left
, dispy
);
1459 glTexCoord2i (block
->w
, py
);
1460 glVertex2i (left
+ block
->w
, dispy
);
1462 glTexCoord2i (block
->w
, py
+ th
);
1463 glVertex2i (left
+ block
->w
, dispy
+ th
);
1465 glTexCoord2i (0, py
+ th
);
1466 glVertex2i (left
, dispy
+ th
);
1478 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1480 showsel (page
, yoff
);
1481 if (hlinks
) highlightlinks (page
, yoff
);
1483 CAMLreturn (Val_unit
);
1486 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1492 p
.x
= x
+ page
->pixmap
->x
;
1493 p
.y
= y
+ page
->pixmap
->y
;
1495 ctm
= fz_invert_matrix (page
->pagedim
.ctm
);
1496 p
= fz_transform_point (ctm
, p
);
1498 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1499 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1500 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1508 static void ensuretext (struct page
*page
)
1514 page
->text
= fz_new_text_span ();
1515 tdev
= fz_new_text_device (page
->text
);
1516 error
= pdf_run_page (state
.xref
, page
->drawpage
, tdev
,
1518 if (error
) die (error
);
1519 fz_free_device (tdev
);
1523 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1525 CAMLparam3 (ptr_v
, x_v
, y_v
);
1526 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1529 char *s
= String_val (ptr_v
);
1531 ret_v
= Val_int (0);
1532 if (trylock ("ml_whatsunder")) {
1536 page
= parse_pointer ("ml_whatsunder", s
);
1537 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1539 switch (link
->kind
) {
1550 if (fz_is_array (link
->dest
)) {
1551 obj
= fz_array_get (link
->dest
, 0);
1552 if (fz_is_indirect (obj
)) {
1553 pageno
= pdf_find_page_number (state
.xref
, obj
);
1555 else if (fz_is_int (obj
)) {
1556 pageno
= fz_to_int (obj
);
1559 if (fz_array_len (link
->dest
) > 3) {
1562 xo
= fz_array_get (link
->dest
, 2);
1563 yo
= fz_array_get (link
->dest
, 3);
1564 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
1565 p
.x
= fz_to_int (xo
);
1566 p
.y
= fz_to_int (yo
);
1567 p
= fz_transform_point (page
->pagedim
.ctm
, p
);
1572 pageno
= pdf_find_page_number (state
.xref
, link
->dest
);
1574 tup_v
= caml_alloc_tuple (2);
1575 ret_v
= caml_alloc_small (1, 1);
1576 Field (tup_v
, 0) = Val_int (pageno
);
1577 Field (tup_v
, 1) = Val_int (p
.y
);
1578 Field (ret_v
, 0) = tup_v
;
1583 str_v
= caml_copy_string (fz_to_str_buf (link
->dest
));
1584 ret_v
= caml_alloc_small (1, 0);
1585 Field (ret_v
, 0) = str_v
;
1589 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1598 x
= Int_val (x_v
) + page
->pixmap
->x
;
1599 y
= Int_val (y_v
) + page
->pixmap
->y
;
1601 for (span
= page
->text
; span
; span
= span
->next
) {
1602 for (i
= 0; i
< span
->len
; ++i
) {
1604 b
= &span
->text
[i
].bbox
;
1605 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1607 span
->font
&& span
->font
->name
1609 : "Span has no font name"
1611 #ifdef FT_FREETYPE_H
1612 FT_FaceRec
*face
= span
->font
->ft_face
;
1613 if (face
&& face
->family_name
) {
1615 char *n1
= face
->family_name
;
1616 size_t l1
= strlen (n1
);
1617 size_t l2
= strlen (n2
);
1619 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
1620 s
= malloc (l1
+ l2
+ 2);
1624 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
1625 str_v
= caml_copy_string (s
);
1631 str_v
= caml_copy_string (n2
);
1634 str_v
= caml_copy_string (n2
);
1636 ret_v
= caml_alloc_small (1, 2);
1637 Field (ret_v
, 0) = str_v
;
1644 unlock ("ml_whatsunder");
1650 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
, value oy_v
)
1652 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1656 struct mark first
, last
;
1657 int i
, x0
, x1
, y0
, y1
, oy
, left
;
1658 char *s
= String_val (ptr_v
);
1660 if (trylock ("ml_seltext")) {
1664 page
= parse_pointer ("ml_seltext", s
);
1667 oy
= Int_val (oy_v
);
1668 x0
= Int_val (Field (rect_v
, 0));
1669 y0
= Int_val (Field (rect_v
, 1));
1670 x1
= Int_val (Field (rect_v
, 2));
1671 y1
= Int_val (Field (rect_v
, 3));
1673 left
= page
->pagedim
.left
;
1675 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1676 glColor3ub (128, 128, 128);
1677 glRecti (x0
, y0
, x1
, y1
);
1678 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1681 x0
+= page
->pixmap
->x
- left
;
1682 y0
+= page
->pixmap
->y
- oy
;
1683 x1
+= page
->pixmap
->x
- left
;
1684 y1
+= page
->pixmap
->y
- oy
;
1689 last
.i
= first
.i
= 0;
1690 first
.span
= page
->text
;
1691 for (span
= page
->text
; span
; span
= span
->next
) {
1692 for (i
= 0; i
< span
->len
; ++i
) {
1693 b
= &span
->text
[i
].bbox
;
1696 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1701 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1706 if (0 && selected
) {
1707 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1708 glColor3ub (128, 128, 128);
1709 glRecti (b
->x0
+left
, b
->y0
, b
->x1
+left
, b
->y1
);
1710 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1715 if (y1
< y0
|| x1
< x0
) {
1718 if (first
.span
== last
.span
) {
1723 for (span
= first
.span
; span
&& span
!= last
.span
;
1724 span
= span
->next
) {
1737 first
.span
= last
.span
;
1743 page
->fmark
= first
;
1746 unlock ("ml_seltext");
1749 CAMLreturn (Val_unit
);
1752 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
1757 for (i
= a
; i
<= b
; ++i
) {
1758 len
= runetochar (buf
, &span
->text
[i
].c
);
1759 ret
= fwrite (buf
, len
, 1, f
);
1762 printd (state
.sock
, "T failed to write %d bytes ret=%d: %s",
1763 len
, ret
, strerror (errno
));
1770 CAMLprim value
ml_copysel (value ptr_v
)
1775 char *s
= String_val (ptr_v
);
1777 if (trylock ("ml_copysel")) {
1784 if (state
.xselpipe
) {
1785 int ret
= pclose (state
.xselpipe
);
1787 printd (state
.sock
, "T failed to close xsel pipe `%s'",
1790 state
.xselpipe
= NULL
;
1793 printf ("========================================\n");
1799 page
= parse_pointer ("ml_sopysel", s
);
1801 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1802 printd (state
.sock
, "T nothing to copy");
1808 if (!state
.xselpipe
) {
1809 state
.xselpipe
= popen ("xsel -i", "w");
1810 if (!state
.xselpipe
) {
1811 printd (state
.sock
, "T failed to open xsel pipe `%s'",
1823 for (span
= page
->fmark
.span
;
1824 span
&& span
!= page
->lmark
.span
->next
;
1825 span
= span
->next
) {
1826 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1827 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1828 if (pipespan (f
, span
, a
, b
)) {
1832 if (putc ('\n', f
) == EOF
) {
1833 printd (state
.sock
, "T failed break line on xsel pipe `%s'",
1839 page
->lmark
.span
= NULL
;
1840 page
->fmark
.span
= NULL
;
1844 unlock ("ml_copysel");
1847 CAMLreturn (Val_unit
);
1850 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
1852 CAMLparam1 (pagedimno_v
);
1854 int pagedimno
= Int_val (pagedimno_v
);
1857 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1858 if (trylock ("ml_getpdimrect")) {
1859 box
= fz_empty_rect
;
1862 box
= state
.pagedims
[pagedimno
].box
;
1863 unlock ("ml_getpdimrect");
1866 Store_double_field (ret_v
, 0, box
.x0
);
1867 Store_double_field (ret_v
, 1, box
.x1
);
1868 Store_double_field (ret_v
, 2, box
.y0
);
1869 Store_double_field (ret_v
, 3, box
.y1
);
1874 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
1876 CAMLparam3 (winw_v
, winh_v
, dw_v
);
1880 double maxw
= 0.0, maxh
= 0.0;
1882 double winw
= Int_val (winw_v
);
1883 double winh
= Int_val (winh_v
);
1884 double dw
= Int_val (dw_v
);
1885 double pw
= 1.0, ph
= 1.0, num
, den
;
1887 if (trylock ("ml_zoom_for_height")) {
1891 if (state
.proportional
) {
1892 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1895 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1896 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1899 maxw
= MAX (w
, maxw
);
1903 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1904 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
1906 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1907 y0
= MIN (p
->box
.y0
, p
->box
.y1
);
1908 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1909 y1
= MAX (p
->box
.y0
, p
->box
.y1
);
1914 if (state
.proportional
) {
1916 scaledh
= h
* scale
;
1923 if (scaledh
> maxh
) {
1930 num
= (winh
* pw
) + (ph
* dw
);
1934 unlock ("ml_zoom_for_height");
1936 ret_v
= caml_copy_double (zoom
);
1942 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
1944 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
1945 int pt
= Int_val(pt_v
);
1946 int x
= Int_val (x_v
);
1947 int y
= Int_val (y_v
);
1950 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
1951 CAMLreturn (caml_copy_double (w
));
1954 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
1956 CAMLparam2 (pt_v
, string_v
);
1958 int pt
= Int_val (pt_v
);
1961 w
= measure_string (state
.face
, pt
, String_val (string_v
));
1962 CAMLreturn (caml_copy_double (w
));
1965 CAMLprim value
ml_init (value sock_v
, value params_v
)
1967 CAMLparam2 (sock_v
, params_v
);
1973 state
.rotate
= Int_val (Field (params_v
, 0));
1974 state
.proportional
= Bool_val (Field (params_v
, 1));
1975 state
.texcount
= Int_val (Field (params_v
, 2));
1976 state
.sliceheight
= Int_val (Field (params_v
, 3));
1977 fontpath
= String_val (Field (params_v
, 4));
1978 state
.texform
= GL_RGBA
;
1979 state
.texty
= GL_UNSIGNED_BYTE
;
1980 state
.blockwidth
= 2048;
1981 state
.sliceheight
= 24;
1984 state
.face
= load_font (fontpath
);
1988 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
1990 state
.face
= load_builtin_font (base
, len
);
1992 if (!state
.face
) _exit (1);
1995 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1996 if (!state
.texids
) {
1997 err (1, "calloc texids " FMT_s
,
1998 state
.texcount
* sizeof (*state
.texids
));
2001 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
2002 if (!state
.texowners
) {
2003 err (1, "calloc texowners " FMT_s
,
2004 state
.texcount
* sizeof (*state
.texowners
));
2007 glGenTextures (state
.texcount
, state
.texids
);
2010 state
.sock
= Socket_val (sock_v
);
2012 state
.sock
= Int_val (sock_v
);
2016 InitializeCriticalSection (&critsec
);
2017 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2018 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2019 errx (1, "CreateThread failed: %lx", GetLastError ());
2022 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2024 errx (1, "pthread_create: %s", strerror (ret
));
2028 CAMLreturn (Val_unit
);