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)
28 #elif defined __GNUC__
29 #define NORETURN __attribute__ ((noreturn))
30 #define UNUSED __attribute__ ((unused))
31 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
39 static void __declspec (noreturn
) sockerr (int exitcode
, const char *fmt
, ...)
44 vfprintf (stderr
, fmt
, ap
);
46 fprintf (stderr
, ": wsaerror 0x%x\n", WSAGetLastError ());
52 #define fionread_arg int
53 #define ioctlsocket ioctl
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <sys/ioctl.h>
71 static void NORETURN
err (int exitcode
, const char *fmt
, ...)
78 vfprintf (stderr
, fmt
, ap
);
80 fprintf (stderr
, ": %s\n", strerror (savederrno
));
85 static void NORETURN
errx (int exitcode
, const char *fmt
, ...)
90 vfprintf (stderr
, fmt
, ap
);
98 #include <OpenGL/gl.h>
103 #ifndef GL_TEXTURE_RECTANGLE_ARB
104 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
107 #include <caml/fail.h>
108 #include <caml/alloc.h>
109 #include <caml/memory.h>
110 #include <caml/unixsupport.h>
115 #include FT_FREETYPE_H
118 #define lprintf printf
123 #define ARSERT(cond) for (;;) { \
125 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
139 struct block
*blocks
;
158 struct pagedim pagedim
;
163 struct slice
*slices
;
166 #if !defined _WIN32 && !defined __APPLE__
172 int sliceheight
, blockwidth
;
174 struct pagedim
*pagedims
;
178 fz_glyph_cache
*cache
;
208 static CRITICAL_SECTION critsec
;
210 static void lock (void *unused
)
213 EnterCriticalSection (&critsec
);
216 static void unlock (void *unused
)
219 LeaveCriticalSection (&critsec
);
222 static int trylock (void *unused
)
224 return TryEnterCriticalSection (&critsec
) == 0;
227 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
229 static void lock (const char *cap
)
231 int ret
= pthread_mutex_lock (&mutex
);
233 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
237 static void unlock (const char *cap
)
239 int ret
= pthread_mutex_unlock (&mutex
);
241 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
245 static int trylock (const char *cap
)
247 int ret
= pthread_mutex_trylock (&mutex
);
249 if (ret
&& ret
!= EBUSY
) {
250 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
256 static void *parse_pointer (const char *cap
, const char *s
)
261 ret
= sscanf (s
, "%p", &ptr
);
263 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
268 static int hasdata (int sock
)
272 ret
= ioctlsocket (sock
, FIONREAD
, &avail
);
273 if (ret
) sockerr (1, "hasdata: FIONREAD error ret=%d", ret
);
277 static double now (void)
281 if (gettimeofday (&tv
, NULL
)) {
282 err (1, "gettimeofday");
284 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
287 static void readdata (int fd
, char *p
, int size
)
291 n
= recv (fd
, p
, size
, 0);
293 if (!n
) errx (1, "EOF while reading");
294 sockerr (1, "recv (req %d, ret " FMT_ss
")", size
, n
);
298 static void writedata (int fd
, char *p
, int size
)
303 buf
[0] = (size
>> 24) & 0xff;
304 buf
[1] = (size
>> 16) & 0xff;
305 buf
[2] = (size
>> 8) & 0xff;
306 buf
[3] = (size
>> 0) & 0xff;
308 n
= send (fd
, buf
, 4, 0);
310 if (!n
) errx (1, "EOF while writing length");
311 sockerr (1, "send " FMT_ss
, n
);
314 n
= send (fd
, p
, size
, 0);
316 if (!n
) errx (1, "EOF while writing data");
317 sockerr (1, "send (req %d, ret " FMT_ss
")", size
, n
);
323 __attribute__ ((format (printf
, 2, 3)))
325 printd (int fd
, const char *fmt
, ...)
333 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
336 len
= vsnprintf (buf
, size
, fmt
, ap
);
339 if (len
> -1 && len
< size
) {
340 writedata (fd
, buf
, len
);
350 buf
= realloc (buf
, size
);
355 static void die (fz_error error
)
357 fz_catch (error
, "aborting");
359 pdf_free_xref (state
.xref
);
363 static void openxref (char *filename
, char *password
)
368 for (i
= 0; i
< state
.texcount
; ++i
) {
369 state
.texowners
[i
].block
= NULL
;
373 fz_free_glyph_cache (state
.cache
);
376 state
.cache
= fz_new_glyph_cache ();
378 errx (1, "fz_newglyph_cache failed");
382 if (state
.xref
->store
) {
383 pdf_free_store (state
.xref
->store
);
384 state
.xref
->store
= NULL
;
386 pdf_free_xref (state
.xref
);
390 if (state
.pagedims
) {
391 free (state
.pagedims
);
392 state
.pagedims
= NULL
;
394 state
.pagedimcount
= 0;
396 error
= pdf_open_xref (&state
.xref
, filename
, password
);
401 error
= pdf_load_page_tree (state
.xref
);
406 state
.pagecount
= pdf_count_pages (state
.xref
);
409 static void pdfinfo (void)
413 printd (state
.sock
, "i PDF version\t%d.%d",
414 state
.xref
->version
/ 10, state
.xref
->version
% 10);
416 infoobj
= fz_dict_gets (state
.xref
->trailer
, "Info");
420 char *items
[] = { "Title", "Creator", "Producer", "CreationDate" };
422 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
423 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
424 s
= pdf_to_utf8 (obj
);
427 printd (state
.sock
, "t %s", s
);
429 printd (state
.sock
, "i %s\t%s", items
[i
], s
);
433 printd (state
.sock
, "ie");
437 static int readlen (int fd
)
442 n
= recv (fd
, p
, 4, 0);
444 if (!n
) errx (1, "EOF while reading length");
445 sockerr (1, "recv " FMT_ss
, n
);
448 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
451 static void unlinkpage (struct page
*page
)
455 for (i
= 0; i
< page
->slicecount
; ++i
) {
457 struct slice
*s
= &page
->slices
[i
];
459 for (j
= 0; j
< page
->blockcount
; ++j
) {
460 struct block
*b
= &s
->blocks
[j
];
462 if (b
->texindex
!= -1) {
463 if (state
.texowners
[b
->texindex
].block
== b
) {
464 state
.texowners
[b
->texindex
].block
= NULL
;
471 static void freepage (struct page
*page
)
475 fz_drop_pixmap (page
->pixmap
);
480 fz_free_text_span (page
->text
);
482 if (page
->drawpage
) {
483 pdf_free_page (page
->drawpage
);
485 for (i
= 0; i
< page
->slicecount
; ++i
) {
486 free (page
->slices
[i
].blocks
);
492 static void subdivide (struct page
*p
, int blockcount
)
495 int w
= p
->pixmap
->w
;
496 int h
= p
->pixmap
->h
;
498 int tw
= MIN (w
, state
.blockwidth
);
499 int th
= MIN (h
, state
.sliceheight
);
501 for (i
= 0; i
< p
->slicecount
; ++i
) {
503 struct slice
*s
= &p
->slices
[i
];
507 s
->blocks
= calloc (sizeof (struct block
), p
->blockcount
);
509 err (1, "calloc blocks page %d slice %d, %d",
510 p
->pageno
, i
, p
->blockcount
);
517 for (j
= 0; j
< blockcount
; ++j
) {
518 struct block
*b
= &s
->blocks
[j
];
528 offset
+= s
->h
* p
->pixmap
->w
* 4;
533 static int compatpdims (struct pagedim
*p1
, struct pagedim
*p2
)
535 return p1
->rotate
== p2
->rotate
536 && !memcmp (&p1
->bbox
, &p2
->bbox
, sizeof (p1
->bbox
))
537 && !memcmp (&p1
->ctm
, &p2
->ctm
, sizeof (p1
->ctm
));
543 static int cacheline32bytes
;
544 extern char **environ
;
546 static void __attribute__ ((constructor
)) clcheck (void)
548 char **envp
= environ
;
553 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
555 cacheline32bytes
= auxv
[1] == 32;
561 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
563 if (cacheline32bytes
) {
564 intptr_t a1
, a2
, diff
;
565 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
566 vector
unsigned char v
= vec_splat_u8 (-1);
567 vector
unsigned char *p
;
569 a1
= a2
= (intptr_t) pixmap
->samples
;
570 a2
= (a1
+ 31) & ~31;
575 while (a1
!= a2
) *(char *) a1
++ = 0xff;
576 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
577 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
579 vec_st (v
, i
+ 16, p
);
581 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
583 else fz_clear_pixmap_with_color (pixmap
, 0xff);
586 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
589 static void *render (int pageno
, int pindex
)
595 struct pagedim
*pagedim
;
596 struct page
*page
= NULL
;
597 int slicecount
, blockcount
;
600 printd (state
.sock
, "V rendering %d", pageno
);
602 pagedim
= &state
.pagedims
[pindex
];
603 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
604 + state
.sliceheight
- 1) / state
.sliceheight
;
605 slicecount
+= slicecount
== 0;
607 blockcount
= (pagedim
->bbox
.x1
- pagedim
->bbox
.x0
608 + state
.blockwidth
- 1) / state
.blockwidth
;
609 blockcount
+= blockcount
== 0;
612 if (compatpdims (&state
.pig
->pagedim
, pagedim
)) {
615 fz_free_text_span (page
->text
);
618 if (page
->drawpage
) {
619 pdf_free_page (page
->drawpage
);
620 page
->drawpage
= NULL
;
624 freepage (state
.pig
);
628 page
= calloc (sizeof (struct page
), 1);
630 err (1, "calloc page %d", pageno
);
632 page
->slices
= calloc (sizeof (struct slice
), slicecount
);
634 err (1, "calloc slices %d %d", pageno
, slicecount
);
636 page
->pixmap
= fz_new_pixmap_with_rect (fz_device_rgb
, pagedim
->bbox
);
639 page
->slicecount
= slicecount
;
640 page
->blockcount
= blockcount
;
642 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
- 1);
646 clearpixmap (page
->pixmap
);
648 idev
= fz_new_draw_device (state
.cache
, page
->pixmap
);
650 die (fz_throw ("fz_newdrawdevice failed"));
651 error
= pdf_run_page (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
653 die (fz_rethrow (error
, "pdf_runpage failed"));
654 fz_free_device (idev
);
656 page
->drawpage
= drawpage
;
657 page
->pagedim
= *pagedim
;
658 page
->pageno
= pageno
;
659 subdivide (page
, blockcount
);
662 pdf_age_store (state
.xref
->store
, 3);
664 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
669 static void initpdims (void)
675 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
679 fz_obj
*obj
, *pageobj
;
681 pageobj
= state
.xref
->page_objs
[pageno
];
683 obj
= fz_dict_gets (pageobj
, "CropBox");
684 if (!fz_is_array (obj
)) {
685 obj
= fz_dict_gets (pageobj
, "MediaBox");
686 if (!fz_is_array (obj
)) {
687 die (fz_throw ("cannot find page bounds %d (%d Rd)",
688 fz_to_num (pageobj
), fz_to_gen (pageobj
)));
691 box
= pdf_to_rect (obj
);
693 obj
= fz_dict_gets (pageobj
, "Rotate");
694 if (fz_is_int (obj
)) {
695 rotate
= fz_to_int (obj
);
700 rotate
+= state
.rotate
;
702 p
= &state
.pagedims
[state
.pagedimcount
- 1];
703 if ((state
.pagedimcount
== 0)
704 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
707 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
708 state
.pagedims
= realloc (state
.pagedims
, size
);
709 if (!state
.pagedims
) {
710 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
711 size
, state
.pagedimcount
+ 1);
713 p
= &state
.pagedims
[state
.pagedimcount
++];
720 printd (state
.sock
, "T Processed %d pages in %f seconds",
721 state
.pagecount
, end
- start
);
724 static void layout (void)
729 double zoom
, w
, maxw
= 0;
730 struct pagedim
*p
= state
.pagedims
;
732 if (state
.proportional
) {
733 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
734 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
735 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
736 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
737 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
740 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
741 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
742 box2
= fz_transform_rect (ctm
, box
);
743 w
= box2
.x1
- box2
.x0
;
744 maxw
= MAX (w
, maxw
);
749 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
750 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
751 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
752 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
753 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
756 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
757 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
758 box2
= fz_transform_rect (ctm
, box
);
759 w
= box2
.x1
- box2
.x0
;
761 if (state
.proportional
) {
762 double scale
= w
/ maxw
;
763 zoom
= (state
.w
/ w
) * scale
;
769 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
770 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
771 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
772 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
773 p
->bbox
= fz_round_rect (fz_transform_rect (ctm
, box
));
774 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
775 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
778 while (p
-- != state
.pagedims
) {
779 printd (state
.sock
, "l %d %d %d %d",
780 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
,
785 static void recurse_outline (pdf_outline
*outline
, int level
)
793 if (!outline
->link
) goto next
;
795 obj
= outline
->link
->dest
;
796 if (fz_is_indirect (obj
)) {
797 obj
= fz_resolve_indirect (obj
);
799 if (fz_is_array (obj
)) {
802 obj2
= fz_array_get (obj
, 0);
803 if (fz_is_int (obj2
)) {
804 pageno
= fz_to_int (obj2
);
807 pageno
= pdf_find_page_number (state
.xref
, obj2
);
810 if (fz_array_len (obj
) > 3) {
814 xo
= fz_array_get (obj
, 2);
815 yo
= fz_array_get (obj
, 3);
816 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
817 struct pagedim
*pagedim
= state
.pagedims
;
819 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
820 if (state
.pagedims
[i
].pageno
> pageno
)
822 pagedim
= &state
.pagedims
[i
];
824 p
.x
= fz_to_int (xo
);
825 p
.y
= fz_to_int (yo
);
826 p
= fz_transform_point (pagedim
->ctm
, p
);
827 h
= pagedim
->bbox
.y1
- pagedim
->bbox
.y0
;
833 pageno
= pdf_find_page_number (state
.xref
, outline
->link
->dest
);
836 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
838 printd (state
.sock
, "o %d %d %d %d %s",
839 level
, pageno
, top
, h
, outline
->title
);
842 if (outline
->child
) {
843 recurse_outline (outline
->child
, level
+ 1);
845 outline
= outline
->next
;
849 static void process_outline (void)
851 pdf_outline
*outline
;
853 if (!state
.needoutline
) return;
855 state
.needoutline
= 0;
856 outline
= pdf_load_outline (state
.xref
);
858 recurse_outline (outline
, 0);
859 pdf_free_outline (outline
);
863 static int comparespans (const void *l
, const void *r
)
865 fz_text_span
const *const*ls
= l
;
866 fz_text_span
const *const*rs
= r
;
867 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
870 /* wishful thinking function */
871 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
881 fz_text_span
*text
, *span
, **pspan
;
882 struct pagedim
*pdim
, *pdimprev
;
889 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
891 pdf_age_store (state
.xref
->store
, 3);
893 if (hasdata (state
.sock
)) {
894 printd (state
.sock
, "T attention requested aborting search at %d",
899 printd (state
.sock
, "T searching in page %d", pageno
);
903 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
904 pdim
= &state
.pagedims
[i
];
905 if (pdim
->pageno
== pageno
) {
908 if (pdim
->pageno
> pageno
) {
917 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
);
921 ctm
= fz_rotate (pdim
->rotate
);
923 text
= fz_new_text_span ();
924 tdev
= fz_new_text_device (text
);
925 error
= pdf_run_page (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
926 if (error
) die (error
);
927 fz_free_device (tdev
);
930 for (span
= text
; span
; span
= span
->next
) {
933 pspan
= malloc (sizeof (void *) * nspans
);
935 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
937 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
940 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
942 j
= forward
? 0 : nspans
- 1;
947 j
+= forward
? 1 : -1;
949 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
951 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
956 if (span
->text
[i
].bbox
.y0
> y
- 1) {
960 if (span
->text
[i
].c
< 256) {
961 *p
++ = span
->text
[i
].c
;
972 ret
= regexec (re
, buf
, 1, &rm
, 0);
974 if (ret
!= REG_NOMATCH
) {
977 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
979 "T regexec error `%.*s'",
981 fz_free_text_span (text
);
982 pdf_free_page (drawpage
);
990 fz_point p1
, p2
, p3
, p4
;
992 xoff
= pdim
->left
- pdim
->bbox
.x0
;
993 yoff
= -pdim
->bbox
.y0
;
995 sb
= &span
->text
[rm
.rm_so
].bbox
;
996 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1007 p1
= fz_transform_point (ctm
, p1
);
1008 p2
= fz_transform_point (ctm
, p2
);
1009 p3
= fz_transform_point (ctm
, p3
);
1010 p4
= fz_transform_point (ctm
, p4
);
1013 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
1015 p1
.x
+ xoff
, p1
.y
+ yoff
,
1016 p2
.x
+ xoff
, p2
.y
+ yoff
,
1017 p3
.x
+ xoff
, p3
.y
+ yoff
,
1018 p4
.x
+ xoff
, p4
.y
+ yoff
);
1020 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
1021 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
1025 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
1027 p1
.x
+ xoff
, p1
.y
+ yoff
,
1028 p2
.x
+ xoff
, p2
.y
+ yoff
,
1029 p3
.x
+ xoff
, p3
.y
+ yoff
,
1030 p4
.x
+ xoff
, p4
.y
+ yoff
);
1043 fz_free_text_span (text
);
1044 pdf_free_page (drawpage
);
1049 printd (state
.sock
, "T no matches %f sec", end
- start
);
1051 printd (state
.sock
, "D");
1060 mainloop (void *unused
)
1063 int len
, ret
, oldlen
= 0;
1066 len
= readlen (state
.sock
);
1068 errx (1, "readlen returned 0");
1071 if (oldlen
< len
+ 1) {
1072 p
= realloc (p
, len
+ 1);
1074 err (1, "realloc %d failed", len
+ 1);
1078 readdata (state
.sock
, p
, len
);
1081 if (!strncmp ("open", p
, 4)) {
1084 char *filename
= p
+ 5;
1086 filenamelen
= strlen (filename
);
1087 password
= filename
+ filenamelen
+ 1;
1089 openxref (filename
, password
);
1092 state
.needoutline
= 1;
1094 else if (!strncmp ("free", p
, 4)) {
1097 ret
= sscanf (p
+ 4, " %p", &ptr
);
1099 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
1103 freepage (state
.pig
);
1107 else if (!strncmp ("search", p
, 6)) {
1108 int icase
, pageno
, y
, ret
, len2
, forward
;
1112 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1113 &icase
, &pageno
, &y
, &forward
, &len2
);
1115 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1118 pattern
= p
+ 6 + len2
;
1119 ret
= regcomp (&re
, pattern
,
1120 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1125 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1126 printd (state
.sock
, "T regcomp failed `%.*s'",
1127 (int) size
, errbuf
);
1130 search (&re
, pageno
, y
, forward
);
1134 else if (!strncmp ("geometry", p
, 8)) {
1137 printd (state
.sock
, "c");
1138 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1140 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1148 for (i
= 0; i
< state
.texcount
; ++i
) {
1149 state
.texowners
[i
].block
= NULL
;
1154 unlock ("geometry");
1155 printd (state
.sock
, "C %d", state
.pagecount
);
1157 else if (!strncmp ("reinit", p
, 6)) {
1161 printd (state
.sock
, "c");
1162 ret
= sscanf (p
+ 6, " %f %d", &rotate
, &proportional
);
1164 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
1167 state
.rotate
= rotate
;
1168 state
.proportional
= proportional
;
1169 state
.pagedimcount
= 0;
1170 free (state
.pagedims
);
1171 state
.pagedims
= NULL
;
1176 freepage (state
.pig
);
1180 printd (state
.sock
, "C %d", state
.pagecount
);
1182 else if (!strncmp ("render", p
, 6)) {
1183 int pageno
, pindex
, w
, h
, ret
;
1186 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
1188 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1192 page
= render (pageno
, pindex
);
1195 printd (state
.sock
, "r %d %d %d %d %d %d %p",
1204 else if (!strncmp ("interrupt", p
, 9)) {
1205 printd (state
.sock
, "V interrupted");
1208 errx (1, "unknown command %.*s", len
, p
);
1214 static void showsel (struct page
*page
, int oy
)
1219 struct mark first
, last
;
1221 first
= page
->fmark
;
1224 if (!first
.span
|| !last
.span
) return;
1226 glEnable (GL_BLEND
);
1227 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1228 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1230 ox
= -page
->pixmap
->x
+ page
->pagedim
.left
;
1231 oy
= -page
->pixmap
->y
+ oy
;
1232 for (span
= first
.span
; span
; span
= span
->next
) {
1235 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1240 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1241 j
= MIN (first
.i
, last
.i
);
1242 k
= MAX (first
.i
, last
.i
);
1244 else if (span
== first
.span
) {
1247 else if (span
== last
.span
) {
1251 for (i
= j
; i
<= k
; ++i
) {
1252 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1254 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1261 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1263 if (span
== last
.span
) break;
1265 glDisable (GL_BLEND
);
1268 static void highlightlinks (struct page
*page
, int yoff
)
1273 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1274 glEnable (GL_LINE_STIPPLE
);
1275 glLineStipple (0.5, 0xcccc);
1277 xoff
= page
->pagedim
.left
- page
->pixmap
->x
;
1278 yoff
-= page
->pixmap
->y
;
1281 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1282 fz_point p1
, p2
, p3
, p4
;
1283 fz_matrix ctm
= page
->pagedim
.ctm
;
1285 p1
.x
= link
->rect
.x0
;
1286 p1
.y
= link
->rect
.y0
;
1288 p2
.x
= link
->rect
.x1
;
1289 p2
.y
= link
->rect
.y0
;
1291 p3
.x
= link
->rect
.x1
;
1292 p3
.y
= link
->rect
.y1
;
1294 p4
.x
= link
->rect
.x0
;
1295 p4
.y
= link
->rect
.y1
;
1297 p1
= fz_transform_point (ctm
, p1
);
1298 p2
= fz_transform_point (ctm
, p2
);
1299 p3
= fz_transform_point (ctm
, p3
);
1300 p4
= fz_transform_point (ctm
, p4
);
1302 switch (link
->kind
) {
1303 case PDF_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1304 case PDF_LINK_URI
: glColor3ub (0, 0, 255); break;
1305 default: glColor3ub (0, 0, 0); break;
1308 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1309 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1310 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1311 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1315 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1316 glDisable (GL_LINE_STIPPLE
);
1319 static void upload (struct page
*page
, int slicenum
, int blocknum
)
1322 struct slice
*slice
= &page
->slices
[slicenum
];
1323 struct block
*block
= &slice
->blocks
[blocknum
];
1325 if (block
->texindex
!= -1
1326 && state
.texowners
[block
->texindex
].block
== block
) {
1327 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[block
->texindex
]);
1331 int index
= (state
.texindex
++ % state
.texcount
);
1333 if (state
.texowners
[index
].w
== block
->w
) {
1334 if (state
.texowners
[index
].h
>= slice
->h
) {
1338 state
.texowners
[index
].h
= slice
->h
;
1342 state
.texowners
[index
].h
= slice
->h
;
1345 state
.texowners
[index
].w
= block
->w
;
1346 state
.texowners
[index
].block
= block
;
1347 block
->texindex
= index
;
1349 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[block
->texindex
]);
1350 glPixelStorei (GL_UNPACK_ROW_LENGTH
, page
->pixmap
->w
);
1354 GLenum err
= glGetError ();
1355 if (err
!= GL_NO_ERROR
) {
1356 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n",
1357 block
->w
, slice
->h
, err
);
1362 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1370 page
->pixmap
->samples
+ block
->offset
1374 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1382 page
->pixmap
->samples
+ block
->offset
1386 GLenum err
= glGetError ();
1387 if (err
!= GL_NO_ERROR
) {
1388 printf ("\033[0;31mERROR %d %d %#x\033[0m\n",
1389 block
->w
, slice
->h
, err
);
1397 lprintf ("%s[%d] slice=%d,%d(%d,%d) tex=%d,%d offset=%d %f sec\n",
1398 subimage
? "sub" : "img",
1399 page
->pageno
, slicenum
, blocknum
,
1402 state
.texids
[block
->texindex
],
1408 CAMLprim value
ml_draw (value args_v
, value ptr_v
)
1410 CAMLparam2 (args_v
, ptr_v
);
1411 int dispy
= Int_val (Field (args_v
, 0));
1412 int h
= Int_val (Field (args_v
, 1));
1413 int py
= Int_val (Field (args_v
, 2));
1414 int hlinks
= Bool_val (Field (args_v
, 3));
1415 char *s
= String_val (ptr_v
);
1420 int yoff
= dispy
- py
;
1421 struct slice
*slice
;
1423 ret
= sscanf (s
, "%p", &ptr
);
1425 errx (1, "cannot parse pointer `%s'", s
);
1429 ARSERT (h
>= 0 && "ml_draw wrong h");
1430 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1432 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1434 for (slicenum
= 0, slice
= page
->slices
;
1435 slicenum
< page
->slicecount
; ++slicenum
, ++slice
) {
1436 if (slice
->h
> py
) {
1441 h
= MIN (state
.h
, h
);
1444 int th
, left
, blocknum
;
1446 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1448 th
= MIN (h
, slice
->h
- py
);
1449 left
= page
->pagedim
.left
;
1451 for (blocknum
= 0; blocknum
< page
->blockcount
; ++blocknum
) {
1452 struct block
*block
= &slice
->blocks
[blocknum
];
1454 upload (page
, slicenum
, blocknum
);
1457 glTexCoord2i (0, py
);
1458 glVertex2i (left
, dispy
);
1460 glTexCoord2i (block
->w
, py
);
1461 glVertex2i (left
+ block
->w
, dispy
);
1463 glTexCoord2i (block
->w
, py
+ th
);
1464 glVertex2i (left
+ block
->w
, dispy
+ th
);
1466 glTexCoord2i (0, py
+ th
);
1467 glVertex2i (left
, dispy
+ th
);
1479 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1481 showsel (page
, yoff
);
1482 if (hlinks
) highlightlinks (page
, yoff
);
1484 CAMLreturn (Val_unit
);
1487 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1493 p
.x
= x
+ page
->pixmap
->x
;
1494 p
.y
= y
+ page
->pixmap
->y
;
1496 ctm
= fz_invert_matrix (page
->pagedim
.ctm
);
1497 p
= fz_transform_point (ctm
, p
);
1499 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1500 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1501 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1509 static void ensuretext (struct page
*page
)
1515 page
->text
= fz_new_text_span ();
1516 tdev
= fz_new_text_device (page
->text
);
1517 error
= pdf_run_page (state
.xref
, page
->drawpage
, tdev
,
1519 if (error
) die (error
);
1520 fz_free_device (tdev
);
1524 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1526 CAMLparam3 (ptr_v
, x_v
, y_v
);
1527 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1530 char *s
= String_val (ptr_v
);
1532 ret_v
= Val_int (0);
1533 if (trylock ("ml_whatsunder")) {
1537 page
= parse_pointer ("ml_whatsunder", s
);
1538 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1540 switch (link
->kind
) {
1551 if (fz_is_array (link
->dest
)) {
1552 obj
= fz_array_get (link
->dest
, 0);
1553 if (fz_is_indirect (obj
)) {
1554 pageno
= pdf_find_page_number (state
.xref
, obj
);
1556 else if (fz_is_int (obj
)) {
1557 pageno
= fz_to_int (obj
);
1560 if (fz_array_len (link
->dest
) > 3) {
1563 xo
= fz_array_get (link
->dest
, 2);
1564 yo
= fz_array_get (link
->dest
, 3);
1565 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
1566 p
.x
= fz_to_int (xo
);
1567 p
.y
= fz_to_int (yo
);
1568 p
= fz_transform_point (page
->pagedim
.ctm
, p
);
1573 pageno
= pdf_find_page_number (state
.xref
, link
->dest
);
1575 tup_v
= caml_alloc_tuple (2);
1576 ret_v
= caml_alloc_small (1, 1);
1577 Field (tup_v
, 0) = Val_int (pageno
);
1578 Field (tup_v
, 1) = Val_int (p
.y
);
1579 Field (ret_v
, 0) = tup_v
;
1584 str_v
= caml_copy_string (fz_to_str_buf (link
->dest
));
1585 ret_v
= caml_alloc_small (1, 0);
1586 Field (ret_v
, 0) = str_v
;
1590 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1599 x
= Int_val (x_v
) + page
->pixmap
->x
;
1600 y
= Int_val (y_v
) + page
->pixmap
->y
;
1602 for (span
= page
->text
; span
; span
= span
->next
) {
1603 for (i
= 0; i
< span
->len
; ++i
) {
1605 b
= &span
->text
[i
].bbox
;
1606 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1608 span
->font
&& span
->font
->name
1610 : "Span has no font name"
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
);
1633 ret_v
= caml_alloc_small (1, 2);
1634 Field (ret_v
, 0) = str_v
;
1641 unlock ("ml_whatsunder");
1647 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
, value oy_v
)
1649 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1653 struct mark first
, last
;
1654 int i
, x0
, x1
, y0
, y1
, oy
, left
;
1655 char *s
= String_val (ptr_v
);
1657 if (trylock ("ml_seltext")) {
1661 page
= parse_pointer ("ml_seltext", s
);
1664 oy
= Int_val (oy_v
);
1665 x0
= Int_val (Field (rect_v
, 0));
1666 y0
= Int_val (Field (rect_v
, 1));
1667 x1
= Int_val (Field (rect_v
, 2));
1668 y1
= Int_val (Field (rect_v
, 3));
1670 left
= page
->pagedim
.left
;
1672 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1673 glColor3ub (128, 128, 128);
1674 glRecti (x0
, y0
, x1
, y1
);
1675 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1678 x0
+= page
->pixmap
->x
- left
;
1679 y0
+= page
->pixmap
->y
- oy
;
1680 x1
+= page
->pixmap
->x
- left
;
1681 y1
+= page
->pixmap
->y
- oy
;
1686 last
.i
= first
.i
= 0;
1687 first
.span
= page
->text
;
1688 for (span
= page
->text
; span
; span
= span
->next
) {
1689 for (i
= 0; i
< span
->len
; ++i
) {
1690 b
= &span
->text
[i
].bbox
;
1693 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1698 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1703 if (0 && selected
) {
1704 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1705 glColor3ub (128, 128, 128);
1706 glRecti (b
->x0
+left
, b
->y0
, b
->x1
+left
, b
->y1
);
1707 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1712 if (y1
< y0
|| x1
< x0
) {
1715 if (first
.span
== last
.span
) {
1720 for (span
= first
.span
; span
&& span
!= last
.span
;
1721 span
= span
->next
) {
1734 first
.span
= last
.span
;
1740 page
->fmark
= first
;
1743 unlock ("ml_seltext");
1746 CAMLreturn (Val_unit
);
1749 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
1754 for (i
= a
; i
<= b
; ++i
) {
1755 len
= runetochar (buf
, &span
->text
[i
].c
);
1756 ret
= fwrite (buf
, len
, 1, f
);
1759 printd (state
.sock
, "T failed to write %d bytes ret=%d: %s",
1760 len
, ret
, strerror (errno
));
1767 CAMLprim value
ml_copysel (value ptr_v
)
1772 char *s
= String_val (ptr_v
);
1774 if (trylock ("ml_copysel")) {
1781 if (state
.xselpipe
) {
1782 int ret
= pclose (state
.xselpipe
);
1784 printd (state
.sock
, "T failed to close xsel pipe `%s'",
1787 state
.xselpipe
= NULL
;
1790 printf ("========================================\n");
1796 page
= parse_pointer ("ml_sopysel", s
);
1798 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1799 printd (state
.sock
, "T nothing to copy");
1805 if (!state
.xselpipe
) {
1806 state
.xselpipe
= popen ("xsel -i", "w");
1807 if (!state
.xselpipe
) {
1808 printd (state
.sock
, "T failed to open xsel pipe `%s'",
1820 for (span
= page
->fmark
.span
;
1821 span
&& span
!= page
->lmark
.span
->next
;
1822 span
= span
->next
) {
1823 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1824 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1825 if (pipespan (f
, span
, a
, b
)) {
1829 if (putc ('\n', f
) == EOF
) {
1830 printd (state
.sock
, "T failed break line on xsel pipe `%s'",
1836 page
->lmark
.span
= NULL
;
1837 page
->fmark
.span
= NULL
;
1841 unlock ("ml_copysel");
1844 CAMLreturn (Val_unit
);
1847 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
1849 CAMLparam1 (pagedimno_v
);
1851 int pagedimno
= Int_val (pagedimno_v
);
1854 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1855 if (trylock ("ml_getpdimrect")) {
1856 box
= fz_empty_rect
;
1859 box
= state
.pagedims
[pagedimno
].box
;
1860 unlock ("ml_getpdimrect");
1863 Store_double_field (ret_v
, 0, box
.x0
);
1864 Store_double_field (ret_v
, 1, box
.x1
);
1865 Store_double_field (ret_v
, 2, box
.y0
);
1866 Store_double_field (ret_v
, 3, box
.y1
);
1871 static double getmaxw (void)
1877 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1880 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1881 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1884 maxw
= MAX (w
, maxw
);
1889 CAMLprim value
ml_getmaxw (value unit_v
)
1891 CAMLparam1 (unit_v
);
1895 if (trylock ("ml_getmaxw")) {
1899 unlock ("ml_getmaxw");
1901 ret_v
= caml_copy_double (maxw
);
1905 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
1907 CAMLparam3 (winw_v
, winh_v
, dw_v
);
1911 double maxw
= 0.0, maxh
= 0.0;
1913 double winw
= Int_val (winw_v
);
1914 double winh
= Int_val (winh_v
);
1915 double dw
= Int_val (dw_v
);
1916 double pw
= 1.0, ph
= 1.0, num
, den
;
1918 if (trylock ("ml_zoom_for_height")) {
1922 if (state
.proportional
) {
1926 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1927 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
1929 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1930 y0
= MIN (p
->box
.y0
, p
->box
.y1
);
1931 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1932 y1
= MAX (p
->box
.y0
, p
->box
.y1
);
1937 if (state
.proportional
) {
1939 scaledh
= h
* scale
;
1946 if (scaledh
> maxh
) {
1953 num
= (winh
* pw
) + (ph
* dw
);
1957 unlock ("ml_zoom_for_height");
1959 ret_v
= caml_copy_double (zoom
);
1965 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
1967 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
1969 int pt
= Int_val(pt_v
);
1970 int x
= Int_val (x_v
);
1971 int y
= Int_val (y_v
);
1974 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
1975 ret_v
= caml_copy_double (w
);
1979 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
1981 CAMLparam2 (pt_v
, string_v
);
1983 int pt
= Int_val (pt_v
);
1986 w
= measure_string (state
.face
, pt
, String_val (string_v
));
1987 ret_v
= caml_copy_double (w
);
1991 CAMLprim value
ml_init (value sock_v
, value params_v
)
1993 CAMLparam2 (sock_v
, params_v
);
1999 state
.rotate
= Int_val (Field (params_v
, 0));
2000 state
.proportional
= Bool_val (Field (params_v
, 1));
2001 state
.texcount
= Int_val (Field (params_v
, 2));
2002 state
.sliceheight
= Int_val (Field (params_v
, 3));
2003 state
.blockwidth
= Int_val (Field (params_v
, 4));
2004 fontpath
= String_val (Field (params_v
, 5));
2005 state
.texform
= GL_RGBA
;
2006 state
.texty
= GL_UNSIGNED_BYTE
;
2009 state
.face
= load_font (fontpath
);
2013 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2015 state
.face
= load_builtin_font (base
, len
);
2017 if (!state
.face
) _exit (1);
2020 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
2021 if (!state
.texids
) {
2022 err (1, "calloc texids " FMT_s
,
2023 state
.texcount
* sizeof (*state
.texids
));
2026 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
2027 if (!state
.texowners
) {
2028 err (1, "calloc texowners " FMT_s
,
2029 state
.texcount
* sizeof (*state
.texowners
));
2032 glGenTextures (state
.texcount
, state
.texids
);
2035 state
.sock
= Socket_val (sock_v
);
2037 state
.sock
= Int_val (sock_v
);
2041 InitializeCriticalSection (&critsec
);
2042 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2043 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2044 errx (1, "CreateThread failed: %lx", GetLastError ());
2047 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2049 errx (1, "pthread_create: %s", strerror (ret
));
2053 CAMLreturn (Val_unit
);