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 #if __GNUC__ > 4 && __GNUC_MINOR__ > 3
561 #define OPTIMIZE __attribute__ ((optimize ("O3")))
566 static void clearpixmap (fz_pixmap
*pixmap
)
568 if (cacheline32bytes
) {
569 intptr_t a1
, a2
, diff
;
570 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
571 vector
unsigned char v
= vec_splat_u8 (-1);
572 vector
unsigned char *p
;
574 a1
= a2
= (intptr_t) pixmap
->samples
;
575 a2
= (a1
+ 31) & ~31;
580 while (a1
!= a2
) *(char *) a1
++ = 0xff;
581 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
582 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
584 vec_st (v
, i
+ 16, p
);
586 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
588 else fz_clear_pixmap_with_color (pixmap
, 0xff);
591 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
594 static void *render (int pageno
, int pindex
)
600 struct pagedim
*pagedim
;
601 struct page
*page
= NULL
;
602 int slicecount
, blockcount
;
605 printd (state
.sock
, "V rendering %d", pageno
);
607 pagedim
= &state
.pagedims
[pindex
];
608 slicecount
= (pagedim
->bbox
.y1
- pagedim
->bbox
.y0
609 + state
.sliceheight
- 1) / state
.sliceheight
;
610 slicecount
+= slicecount
== 0;
612 blockcount
= (pagedim
->bbox
.x1
- pagedim
->bbox
.x0
613 + state
.blockwidth
- 1) / state
.blockwidth
;
614 blockcount
+= blockcount
== 0;
617 if (compatpdims (&state
.pig
->pagedim
, pagedim
)) {
620 fz_free_text_span (page
->text
);
623 if (page
->drawpage
) {
624 pdf_free_page (page
->drawpage
);
625 page
->drawpage
= NULL
;
629 freepage (state
.pig
);
633 page
= calloc (sizeof (struct page
), 1);
635 err (1, "calloc page %d", pageno
);
637 page
->slices
= calloc (sizeof (struct slice
), slicecount
);
639 err (1, "calloc slices %d %d", pageno
, slicecount
);
641 page
->pixmap
= fz_new_pixmap_with_rect (fz_device_rgb
, pagedim
->bbox
);
644 page
->slicecount
= slicecount
;
645 page
->blockcount
= blockcount
;
647 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
- 1);
651 clearpixmap (page
->pixmap
);
653 idev
= fz_new_draw_device (state
.cache
, page
->pixmap
);
655 die (fz_throw ("fz_newdrawdevice failed"));
656 error
= pdf_run_page (state
.xref
, drawpage
, idev
, pagedim
->ctm
);
658 die (fz_rethrow (error
, "pdf_runpage failed"));
659 fz_free_device (idev
);
661 page
->drawpage
= drawpage
;
662 page
->pagedim
= *pagedim
;
663 page
->pageno
= pageno
;
664 subdivide (page
, blockcount
);
667 pdf_age_store (state
.xref
->store
, 3);
669 printd (state
.sock
, "V rendering %d took %f sec", pageno
, end
- start
);
674 static void initpdims (void)
680 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
684 fz_obj
*obj
, *pageobj
;
686 pageobj
= state
.xref
->page_objs
[pageno
];
688 obj
= fz_dict_gets (pageobj
, "CropBox");
689 if (!fz_is_array (obj
)) {
690 obj
= fz_dict_gets (pageobj
, "MediaBox");
691 if (!fz_is_array (obj
)) {
692 die (fz_throw ("cannot find page bounds %d (%d Rd)",
693 fz_to_num (pageobj
), fz_to_gen (pageobj
)));
696 box
= pdf_to_rect (obj
);
698 obj
= fz_dict_gets (pageobj
, "Rotate");
699 if (fz_is_int (obj
)) {
700 rotate
= fz_to_int (obj
);
705 rotate
+= state
.rotate
;
707 p
= &state
.pagedims
[state
.pagedimcount
- 1];
708 if ((state
.pagedimcount
== 0)
709 || (p
->rotate
!= rotate
|| memcmp (&p
->box
, &box
, sizeof (box
)))) {
712 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
713 state
.pagedims
= realloc (state
.pagedims
, size
);
714 if (!state
.pagedims
) {
715 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
716 size
, state
.pagedimcount
+ 1);
718 p
= &state
.pagedims
[state
.pagedimcount
++];
725 printd (state
.sock
, "T Processed %d pages in %f seconds",
726 state
.pagecount
, end
- start
);
729 static void layout (void)
734 double zoom
, w
, maxw
= 0;
735 struct pagedim
*p
= state
.pagedims
;
737 if (state
.proportional
) {
738 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
739 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
740 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
741 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
742 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
745 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
746 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
747 box2
= fz_transform_rect (ctm
, box
);
748 w
= box2
.x1
- box2
.x0
;
749 maxw
= MAX (w
, maxw
);
754 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
755 box
.x0
= MIN (p
->box
.x0
, p
->box
.x1
);
756 box
.y0
= MIN (p
->box
.y0
, p
->box
.y1
);
757 box
.x1
= MAX (p
->box
.x0
, p
->box
.x1
);
758 box
.y1
= MAX (p
->box
.y0
, p
->box
.y1
);
761 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
762 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
763 box2
= fz_transform_rect (ctm
, box
);
764 w
= box2
.x1
- box2
.x0
;
766 if (state
.proportional
) {
767 double scale
= w
/ maxw
;
768 zoom
= (state
.w
/ w
) * scale
;
774 ctm
= fz_concat (ctm
, fz_translate (0, -box
.y1
));
775 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
776 memcpy (&p
->ctm1
, &ctm
, sizeof (ctm
));
777 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
));
778 p
->bbox
= fz_round_rect (fz_transform_rect (ctm
, box
));
779 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
780 memcpy (&p
->ctm
, &ctm
, sizeof (ctm
));
783 while (p
-- != state
.pagedims
) {
784 printd (state
.sock
, "l %d %d %d %d",
785 p
->pageno
, p
->bbox
.x1
- p
->bbox
.x0
, p
->bbox
.y1
- p
->bbox
.y0
,
790 static void recurse_outline (pdf_outline
*outline
, int level
)
798 if (!outline
->link
) goto next
;
800 obj
= outline
->link
->dest
;
801 if (fz_is_indirect (obj
)) {
802 obj
= fz_resolve_indirect (obj
);
804 if (fz_is_array (obj
)) {
807 obj2
= fz_array_get (obj
, 0);
808 if (fz_is_int (obj2
)) {
809 pageno
= fz_to_int (obj2
);
812 pageno
= pdf_find_page_number (state
.xref
, obj2
);
815 if (fz_array_len (obj
) > 3) {
819 xo
= fz_array_get (obj
, 2);
820 yo
= fz_array_get (obj
, 3);
821 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
822 struct pagedim
*pagedim
= state
.pagedims
;
824 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
825 if (state
.pagedims
[i
].pageno
> pageno
)
827 pagedim
= &state
.pagedims
[i
];
829 p
.x
= fz_to_int (xo
);
830 p
.y
= fz_to_int (yo
);
831 p
= fz_transform_point (pagedim
->ctm
, p
);
832 h
= pagedim
->bbox
.y1
- pagedim
->bbox
.y0
;
838 pageno
= pdf_find_page_number (state
.xref
, outline
->link
->dest
);
841 lprintf ("%*c%s %d\n", level
, ' ', outline
->title
, pageno
);
843 printd (state
.sock
, "o %d %d %d %d %s",
844 level
, pageno
, top
, h
, outline
->title
);
847 if (outline
->child
) {
848 recurse_outline (outline
->child
, level
+ 1);
850 outline
= outline
->next
;
854 static void process_outline (void)
856 pdf_outline
*outline
;
858 if (!state
.needoutline
) return;
860 state
.needoutline
= 0;
861 outline
= pdf_load_outline (state
.xref
);
863 recurse_outline (outline
, 0);
864 pdf_free_outline (outline
);
868 static int comparespans (const void *l
, const void *r
)
870 fz_text_span
const *const*ls
= l
;
871 fz_text_span
const *const*rs
= r
;
872 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
875 /* wishful thinking function */
876 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
886 fz_text_span
*text
, *span
, **pspan
;
887 struct pagedim
*pdim
, *pdimprev
;
894 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
896 pdf_age_store (state
.xref
->store
, 3);
898 if (hasdata (state
.sock
)) {
899 printd (state
.sock
, "T attention requested aborting search at %d",
904 printd (state
.sock
, "T searching in page %d", pageno
);
908 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
909 pdim
= &state
.pagedims
[i
];
910 if (pdim
->pageno
== pageno
) {
913 if (pdim
->pageno
> pageno
) {
922 error
= pdf_load_page (&drawpage
, state
.xref
, pageno
);
926 ctm
= fz_rotate (pdim
->rotate
);
928 text
= fz_new_text_span ();
929 tdev
= fz_new_text_device (text
);
930 error
= pdf_run_page (state
.xref
, drawpage
, tdev
, pdim
->ctm1
);
931 if (error
) die (error
);
932 fz_free_device (tdev
);
935 for (span
= text
; span
; span
= span
->next
) {
938 pspan
= malloc (sizeof (void *) * nspans
);
940 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
942 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
945 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
947 j
= forward
? 0 : nspans
- 1;
952 j
+= forward
? 1 : -1;
954 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
956 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
961 if (span
->text
[i
].bbox
.y0
> y
- 1) {
965 if (span
->text
[i
].c
< 256) {
966 *p
++ = span
->text
[i
].c
;
977 ret
= regexec (re
, buf
, 1, &rm
, 0);
979 if (ret
!= REG_NOMATCH
) {
982 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
984 "T regexec error `%.*s'",
986 fz_free_text_span (text
);
987 pdf_free_page (drawpage
);
995 fz_point p1
, p2
, p3
, p4
;
997 xoff
= pdim
->left
- pdim
->bbox
.x0
;
998 yoff
= -pdim
->bbox
.y0
;
1000 sb
= &span
->text
[rm
.rm_so
].bbox
;
1001 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1012 p1
= fz_transform_point (ctm
, p1
);
1013 p2
= fz_transform_point (ctm
, p2
);
1014 p3
= fz_transform_point (ctm
, p3
);
1015 p4
= fz_transform_point (ctm
, p4
);
1018 printd (state
.sock
, "F %d %d %f %f %f %f %f %f %f %f",
1020 p1
.x
+ xoff
, p1
.y
+ yoff
,
1021 p2
.x
+ xoff
, p2
.y
+ yoff
,
1022 p3
.x
+ xoff
, p3
.y
+ yoff
,
1023 p4
.x
+ xoff
, p4
.y
+ yoff
);
1025 printd (state
.sock
, "T found at %d `%.*s' in %f sec",
1026 pageno
, rm
.rm_eo
- rm
.rm_so
, &buf
[rm
.rm_so
],
1030 printd (state
.sock
, "R %d %d %f %f %f %f %f %f %f %f",
1032 p1
.x
+ xoff
, p1
.y
+ yoff
,
1033 p2
.x
+ xoff
, p2
.y
+ yoff
,
1034 p3
.x
+ xoff
, p3
.y
+ yoff
,
1035 p4
.x
+ xoff
, p4
.y
+ yoff
);
1048 fz_free_text_span (text
);
1049 pdf_free_page (drawpage
);
1054 printd (state
.sock
, "T no matches %f sec", end
- start
);
1056 printd (state
.sock
, "D");
1065 mainloop (void *unused
)
1068 int len
, ret
, oldlen
= 0;
1071 len
= readlen (state
.sock
);
1073 errx (1, "readlen returned 0");
1076 if (oldlen
< len
+ 1) {
1077 p
= realloc (p
, len
+ 1);
1079 err (1, "realloc %d failed", len
+ 1);
1083 readdata (state
.sock
, p
, len
);
1086 if (!strncmp ("open", p
, 4)) {
1089 char *filename
= p
+ 5;
1091 filenamelen
= strlen (filename
);
1092 password
= filename
+ filenamelen
+ 1;
1094 openxref (filename
, password
);
1097 state
.needoutline
= 1;
1099 else if (!strncmp ("free", p
, 4)) {
1102 ret
= sscanf (p
+ 4, " %p", &ptr
);
1104 errx (1, "malformed free `%.*s' ret=%d", len
, p
, ret
);
1109 else if (!strncmp ("search", p
, 6)) {
1110 int icase
, pageno
, y
, ret
, len2
, forward
;
1114 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1115 &icase
, &pageno
, &y
, &forward
, &len2
);
1117 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1120 pattern
= p
+ 6 + len2
;
1121 ret
= regcomp (&re
, pattern
,
1122 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1127 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1128 printd (state
.sock
, "T regcomp failed `%.*s'",
1129 (int) size
, errbuf
);
1132 search (&re
, pageno
, y
, forward
);
1136 else if (!strncmp ("geometry", p
, 8)) {
1139 printd (state
.sock
, "c");
1140 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1142 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1150 for (i
= 0; i
< state
.texcount
; ++i
) {
1151 state
.texowners
[i
].block
= NULL
;
1156 unlock ("geometry");
1157 printd (state
.sock
, "C %d", state
.pagecount
);
1159 else if (!strncmp ("reinit", p
, 6)) {
1163 printd (state
.sock
, "c");
1164 ret
= sscanf (p
+ 6, " %f %d", &rotate
, &proportional
);
1166 errx (1, "bad rotate line `%.*s' ret=%d", len
, p
, ret
);
1169 state
.rotate
= rotate
;
1170 state
.proportional
= proportional
;
1171 state
.pagedimcount
= 0;
1172 free (state
.pagedims
);
1173 state
.pagedims
= NULL
;
1178 freepage (state
.pig
);
1182 printd (state
.sock
, "C %d", state
.pagecount
);
1184 else if (!strncmp ("render", p
, 6)) {
1185 int pageno
, pindex
, w
, h
, ret
;
1188 ret
= sscanf (p
+ 6, " %d %d %d %d", &pageno
, &pindex
, &w
, &h
);
1190 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1194 page
= render (pageno
, pindex
);
1197 printd (state
.sock
, "r %d %d %d %d %d %d %p",
1206 else if (!strncmp ("interrupt", p
, 9)) {
1207 printd (state
.sock
, "V interrupted");
1210 errx (1, "unknown command %.*s", len
, p
);
1216 static void showsel (struct page
*page
, int oy
)
1221 struct mark first
, last
;
1223 first
= page
->fmark
;
1226 if (!first
.span
|| !last
.span
) return;
1228 glEnable (GL_BLEND
);
1229 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1230 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1232 ox
= -page
->pixmap
->x
+ page
->pagedim
.left
;
1233 oy
= -page
->pixmap
->y
+ oy
;
1234 for (span
= first
.span
; span
; span
= span
->next
) {
1237 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1242 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1243 j
= MIN (first
.i
, last
.i
);
1244 k
= MAX (first
.i
, last
.i
);
1246 else if (span
== first
.span
) {
1249 else if (span
== last
.span
) {
1253 for (i
= j
; i
<= k
; ++i
) {
1254 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1256 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1263 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1265 if (span
== last
.span
) break;
1267 glDisable (GL_BLEND
);
1270 static void highlightlinks (struct page
*page
, int yoff
)
1275 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1276 glEnable (GL_LINE_STIPPLE
);
1277 glLineStipple (0.5, 0xcccc);
1279 xoff
= page
->pagedim
.left
- page
->pixmap
->x
;
1280 yoff
-= page
->pixmap
->y
;
1283 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1284 fz_point p1
, p2
, p3
, p4
;
1285 fz_matrix ctm
= page
->pagedim
.ctm
;
1287 p1
.x
= link
->rect
.x0
;
1288 p1
.y
= link
->rect
.y0
;
1290 p2
.x
= link
->rect
.x1
;
1291 p2
.y
= link
->rect
.y0
;
1293 p3
.x
= link
->rect
.x1
;
1294 p3
.y
= link
->rect
.y1
;
1296 p4
.x
= link
->rect
.x0
;
1297 p4
.y
= link
->rect
.y1
;
1299 p1
= fz_transform_point (ctm
, p1
);
1300 p2
= fz_transform_point (ctm
, p2
);
1301 p3
= fz_transform_point (ctm
, p3
);
1302 p4
= fz_transform_point (ctm
, p4
);
1304 switch (link
->kind
) {
1305 case PDF_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1306 case PDF_LINK_URI
: glColor3ub (0, 0, 255); break;
1307 default: glColor3ub (0, 0, 0); break;
1310 glVertex2f (p1
.x
+ xoff
, p1
.y
+ yoff
);
1311 glVertex2f (p2
.x
+ xoff
, p2
.y
+ yoff
);
1312 glVertex2f (p3
.x
+ xoff
, p3
.y
+ yoff
);
1313 glVertex2f (p4
.x
+ xoff
, p4
.y
+ yoff
);
1317 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1318 glDisable (GL_LINE_STIPPLE
);
1321 static void upload (struct page
*page
, int slicenum
, int blocknum
)
1324 struct slice
*slice
= &page
->slices
[slicenum
];
1325 struct block
*block
= &slice
->blocks
[blocknum
];
1327 if (block
->texindex
!= -1
1328 && state
.texowners
[block
->texindex
].block
== block
) {
1329 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[block
->texindex
]);
1333 int index
= (state
.texindex
++ % state
.texcount
);
1335 if (state
.texowners
[index
].w
== block
->w
) {
1336 if (state
.texowners
[index
].h
>= slice
->h
) {
1340 state
.texowners
[index
].h
= slice
->h
;
1344 state
.texowners
[index
].h
= slice
->h
;
1347 state
.texowners
[index
].w
= block
->w
;
1348 state
.texowners
[index
].block
= block
;
1349 block
->texindex
= index
;
1351 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[block
->texindex
]);
1352 glPixelStorei (GL_UNPACK_ROW_LENGTH
, page
->pixmap
->w
);
1356 GLenum err
= glGetError ();
1357 if (err
!= GL_NO_ERROR
) {
1358 printf ("\033[0;31mERROR1 %d %d %#x\033[0m\n",
1359 block
->w
, slice
->h
, err
);
1364 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1372 page
->pixmap
->samples
+ block
->offset
1376 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1384 page
->pixmap
->samples
+ block
->offset
1388 GLenum err
= glGetError ();
1389 if (err
!= GL_NO_ERROR
) {
1390 printf ("\033[0;31mERROR %d %d %#x\033[0m\n",
1391 block
->w
, slice
->h
, err
);
1399 lprintf ("%s[%d] slice=%d,%d(%d,%d) tex=%d,%d offset=%d %f sec\n",
1400 subimage
? "sub" : "img",
1401 page
->pageno
, slicenum
, blocknum
,
1404 state
.texids
[block
->texindex
],
1410 CAMLprim value
ml_draw (value args_v
, value ptr_v
)
1412 CAMLparam2 (args_v
, ptr_v
);
1413 int dispy
= Int_val (Field (args_v
, 0));
1414 int h
= Int_val (Field (args_v
, 1));
1415 int py
= Int_val (Field (args_v
, 2));
1416 int hlinks
= Bool_val (Field (args_v
, 3));
1417 char *s
= String_val (ptr_v
);
1422 int yoff
= dispy
- py
;
1423 struct slice
*slice
;
1425 ret
= sscanf (s
, "%p", &ptr
);
1427 errx (1, "cannot parse pointer `%s'", s
);
1431 ARSERT (h
>= 0 && "ml_draw wrong h");
1432 ARSERT (py
<= page
->pixmap
->h
&& "ml_draw wrong py");
1434 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1436 for (slicenum
= 0, slice
= page
->slices
;
1437 slicenum
< page
->slicecount
; ++slicenum
, ++slice
) {
1438 if (slice
->h
> py
) {
1443 h
= MIN (state
.h
, h
);
1446 int th
, left
, blocknum
;
1448 ARSERT (slicenum
< page
->slicecount
&& "ml_draw wrong slicenum");
1450 th
= MIN (h
, slice
->h
- py
);
1451 left
= page
->pagedim
.left
;
1453 for (blocknum
= 0; blocknum
< page
->blockcount
; ++blocknum
) {
1454 struct block
*block
= &slice
->blocks
[blocknum
];
1456 upload (page
, slicenum
, blocknum
);
1459 glTexCoord2i (0, py
);
1460 glVertex2i (left
, dispy
);
1462 glTexCoord2i (block
->w
, py
);
1463 glVertex2i (left
+ block
->w
, dispy
);
1465 glTexCoord2i (block
->w
, py
+ th
);
1466 glVertex2i (left
+ block
->w
, dispy
+ th
);
1468 glTexCoord2i (0, py
+ th
);
1469 glVertex2i (left
, dispy
+ th
);
1481 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1483 showsel (page
, yoff
);
1484 if (hlinks
) highlightlinks (page
, yoff
);
1486 CAMLreturn (Val_unit
);
1489 static pdf_link
*getlink (struct page
*page
, int x
, int y
)
1495 p
.x
= x
+ page
->pixmap
->x
;
1496 p
.y
= y
+ page
->pixmap
->y
;
1498 ctm
= fz_invert_matrix (page
->pagedim
.ctm
);
1499 p
= fz_transform_point (ctm
, p
);
1501 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1502 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1503 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1511 static void ensuretext (struct page
*page
)
1517 page
->text
= fz_new_text_span ();
1518 tdev
= fz_new_text_device (page
->text
);
1519 error
= pdf_run_page (state
.xref
, page
->drawpage
, tdev
,
1521 if (error
) die (error
);
1522 fz_free_device (tdev
);
1526 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1528 CAMLparam3 (ptr_v
, x_v
, y_v
);
1529 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1532 char *s
= String_val (ptr_v
);
1534 ret_v
= Val_int (0);
1535 if (trylock ("ml_whatsunder")) {
1539 page
= parse_pointer ("ml_whatsunder", s
);
1540 link
= getlink (page
, Int_val (x_v
), Int_val (y_v
));
1542 switch (link
->kind
) {
1553 if (fz_is_array (link
->dest
)) {
1554 obj
= fz_array_get (link
->dest
, 0);
1555 if (fz_is_indirect (obj
)) {
1556 pageno
= pdf_find_page_number (state
.xref
, obj
);
1558 else if (fz_is_int (obj
)) {
1559 pageno
= fz_to_int (obj
);
1562 if (fz_array_len (link
->dest
) > 3) {
1565 xo
= fz_array_get (link
->dest
, 2);
1566 yo
= fz_array_get (link
->dest
, 3);
1567 if (!fz_is_null (xo
) && !fz_is_null (yo
)) {
1568 p
.x
= fz_to_int (xo
);
1569 p
.y
= fz_to_int (yo
);
1570 p
= fz_transform_point (page
->pagedim
.ctm
, p
);
1575 pageno
= pdf_find_page_number (state
.xref
, link
->dest
);
1577 tup_v
= caml_alloc_tuple (2);
1578 ret_v
= caml_alloc_small (1, 1);
1579 Field (tup_v
, 0) = Val_int (pageno
);
1580 Field (tup_v
, 1) = Val_int (p
.y
);
1581 Field (ret_v
, 0) = tup_v
;
1586 str_v
= caml_copy_string (fz_to_str_buf (link
->dest
));
1587 ret_v
= caml_alloc_small (1, 0);
1588 Field (ret_v
, 0) = str_v
;
1592 printd (state
.sock
, "T unhandled link kind %d", link
->kind
);
1601 x
= Int_val (x_v
) + page
->pixmap
->x
;
1602 y
= Int_val (y_v
) + page
->pixmap
->y
;
1604 for (span
= page
->text
; span
; span
= span
->next
) {
1605 for (i
= 0; i
< span
->len
; ++i
) {
1607 b
= &span
->text
[i
].bbox
;
1608 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1610 span
->font
&& span
->font
->name
1612 : "Span has no font name"
1614 #ifdef FT_FREETYPE_H
1615 FT_FaceRec
*face
= span
->font
->ft_face
;
1616 if (face
&& face
->family_name
) {
1618 char *n1
= face
->family_name
;
1619 size_t l1
= strlen (n1
);
1620 size_t l2
= strlen (n2
);
1622 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
1623 s
= malloc (l1
+ l2
+ 2);
1627 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
1628 str_v
= caml_copy_string (s
);
1634 str_v
= caml_copy_string (n2
);
1637 str_v
= caml_copy_string (n2
);
1639 ret_v
= caml_alloc_small (1, 2);
1640 Field (ret_v
, 0) = str_v
;
1647 unlock ("ml_whatsunder");
1653 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
, value oy_v
)
1655 CAMLparam4 (ptr_v
, rect_v
, oy_v
, rect_v
);
1659 struct mark first
, last
;
1660 int i
, x0
, x1
, y0
, y1
, oy
, left
;
1661 char *s
= String_val (ptr_v
);
1663 if (trylock ("ml_seltext")) {
1667 page
= parse_pointer ("ml_seltext", s
);
1670 oy
= Int_val (oy_v
);
1671 x0
= Int_val (Field (rect_v
, 0));
1672 y0
= Int_val (Field (rect_v
, 1));
1673 x1
= Int_val (Field (rect_v
, 2));
1674 y1
= Int_val (Field (rect_v
, 3));
1676 left
= page
->pagedim
.left
;
1678 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1679 glColor3ub (128, 128, 128);
1680 glRecti (x0
, y0
, x1
, y1
);
1681 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1684 x0
+= page
->pixmap
->x
- left
;
1685 y0
+= page
->pixmap
->y
- oy
;
1686 x1
+= page
->pixmap
->x
- left
;
1687 y1
+= page
->pixmap
->y
- oy
;
1692 last
.i
= first
.i
= 0;
1693 first
.span
= page
->text
;
1694 for (span
= page
->text
; span
; span
= span
->next
) {
1695 for (i
= 0; i
< span
->len
; ++i
) {
1696 b
= &span
->text
[i
].bbox
;
1699 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1704 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1709 if (0 && selected
) {
1710 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1711 glColor3ub (128, 128, 128);
1712 glRecti (b
->x0
+left
, b
->y0
, b
->x1
+left
, b
->y1
);
1713 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1718 if (y1
< y0
|| x1
< x0
) {
1721 if (first
.span
== last
.span
) {
1726 for (span
= first
.span
; span
&& span
!= last
.span
;
1727 span
= span
->next
) {
1740 first
.span
= last
.span
;
1746 page
->fmark
= first
;
1749 unlock ("ml_seltext");
1752 CAMLreturn (Val_unit
);
1755 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
1760 for (i
= a
; i
<= b
; ++i
) {
1761 len
= runetochar (buf
, &span
->text
[i
].c
);
1762 ret
= fwrite (buf
, len
, 1, f
);
1765 printd (state
.sock
, "T failed to write %d bytes ret=%d: %s",
1766 len
, ret
, strerror (errno
));
1773 CAMLprim value
ml_copysel (value ptr_v
)
1778 char *s
= String_val (ptr_v
);
1780 if (trylock ("ml_copysel")) {
1787 if (state
.xselpipe
) {
1788 int ret
= pclose (state
.xselpipe
);
1790 printd (state
.sock
, "T failed to close xsel pipe `%s'",
1793 state
.xselpipe
= NULL
;
1796 printf ("========================================\n");
1802 page
= parse_pointer ("ml_sopysel", s
);
1804 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1805 printd (state
.sock
, "T nothing to copy");
1811 if (!state
.xselpipe
) {
1812 state
.xselpipe
= popen ("xsel -i", "w");
1813 if (!state
.xselpipe
) {
1814 printd (state
.sock
, "T failed to open xsel pipe `%s'",
1826 for (span
= page
->fmark
.span
;
1827 span
&& span
!= page
->lmark
.span
->next
;
1828 span
= span
->next
) {
1829 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1830 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1831 if (pipespan (f
, span
, a
, b
)) {
1835 if (putc ('\n', f
) == EOF
) {
1836 printd (state
.sock
, "T failed break line on xsel pipe `%s'",
1842 page
->lmark
.span
= NULL
;
1843 page
->fmark
.span
= NULL
;
1847 unlock ("ml_copysel");
1850 CAMLreturn (Val_unit
);
1853 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
1855 CAMLparam1 (pagedimno_v
);
1857 int pagedimno
= Int_val (pagedimno_v
);
1860 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1861 if (trylock ("ml_getpdimrect")) {
1862 box
= fz_empty_rect
;
1865 box
= state
.pagedims
[pagedimno
].box
;
1866 unlock ("ml_getpdimrect");
1869 Store_double_field (ret_v
, 0, box
.x0
);
1870 Store_double_field (ret_v
, 1, box
.x1
);
1871 Store_double_field (ret_v
, 2, box
.y0
);
1872 Store_double_field (ret_v
, 3, box
.y1
);
1877 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
1879 CAMLparam3 (winw_v
, winh_v
, dw_v
);
1883 double maxw
= 0.0, maxh
= 0.0;
1885 double winw
= Int_val (winw_v
);
1886 double winh
= Int_val (winh_v
);
1887 double dw
= Int_val (dw_v
);
1888 double pw
= 1.0, ph
= 1.0, num
, den
;
1890 if (trylock ("ml_zoom_for_height")) {
1894 if (state
.proportional
) {
1895 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1898 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1899 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1902 maxw
= MAX (w
, maxw
);
1906 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1907 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
1909 x0
= MIN (p
->box
.x0
, p
->box
.x1
);
1910 y0
= MIN (p
->box
.y0
, p
->box
.y1
);
1911 x1
= MAX (p
->box
.x0
, p
->box
.x1
);
1912 y1
= MAX (p
->box
.y0
, p
->box
.y1
);
1917 if (state
.proportional
) {
1919 scaledh
= h
* scale
;
1926 if (scaledh
> maxh
) {
1933 num
= (winh
* pw
) + (ph
* dw
);
1937 unlock ("ml_zoom_for_height");
1939 ret_v
= caml_copy_double (zoom
);
1945 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
1947 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
1948 int pt
= Int_val(pt_v
);
1949 int x
= Int_val (x_v
);
1950 int y
= Int_val (y_v
);
1953 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
1954 CAMLreturn (caml_copy_double (w
));
1957 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
1959 CAMLparam2 (pt_v
, string_v
);
1961 int pt
= Int_val (pt_v
);
1964 w
= measure_string (state
.face
, pt
, String_val (string_v
));
1965 CAMLreturn (caml_copy_double (w
));
1968 CAMLprim value
ml_init (value sock_v
, value params_v
)
1970 CAMLparam2 (sock_v
, params_v
);
1976 state
.rotate
= Int_val (Field (params_v
, 0));
1977 state
.proportional
= Bool_val (Field (params_v
, 1));
1978 state
.texcount
= Int_val (Field (params_v
, 2));
1979 state
.sliceheight
= Int_val (Field (params_v
, 3));
1980 state
.blockwidth
= Int_val (Field (params_v
, 4));
1981 fontpath
= String_val (Field (params_v
, 5));
1982 state
.texform
= GL_RGBA
;
1983 state
.texty
= GL_UNSIGNED_BYTE
;
1986 state
.face
= load_font (fontpath
);
1990 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
1992 state
.face
= load_builtin_font (base
, len
);
1994 if (!state
.face
) _exit (1);
1997 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
1998 if (!state
.texids
) {
1999 err (1, "calloc texids " FMT_s
,
2000 state
.texcount
* sizeof (*state
.texids
));
2003 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
2004 if (!state
.texowners
) {
2005 err (1, "calloc texowners " FMT_s
,
2006 state
.texcount
* sizeof (*state
.texowners
));
2009 glGenTextures (state
.texcount
, state
.texids
);
2012 state
.sock
= Socket_val (sock_v
);
2014 state
.sock
= Int_val (sock_v
);
2018 InitializeCriticalSection (&critsec
);
2019 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2020 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2021 errx (1, "CreateThread failed: %lx", GetLastError ());
2024 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2026 errx (1, "pthread_create: %s", strerror (ret
));
2030 CAMLreturn (Val_unit
);