1 /* lots of code c&p-ed directly from mupdf */
10 #define WIN32_LEAN_AND_MEAN
13 #define fionread_arg long
24 #pragma warning (disable:4244)
25 #pragma warning (disable:4996)
26 #pragma warning (disable:4995)
27 #define NORETURN __declspec (noreturn)
30 #elif defined __GNUC__
31 #define NORETURN __attribute__ ((noreturn))
32 #define UNUSED __attribute__ ((unused))
33 #define OPTIMIZE(n) __attribute__ ((optimize ("O"#n)))
41 /* some versions of MingW have non idempotent %p */
43 #define FMT_ptr PRIxPTR
44 #define FMT_ptr_cast(p) ((intptr_t *) (p))
45 #define FMT_ptr_cast2(p) ((intptr_t) (p))
48 #define FMT_ptr_cast(p) (p)
49 #define FMT_ptr_cast2(p) (p)
53 static void __declspec (noreturn
) sockerr (int exitcode
, const char *fmt
, ...)
58 vfprintf (stderr
, fmt
, ap
);
60 fprintf (stderr
, ": wsaerror 0x%x\n", WSAGetLastError ());
66 #define fionread_arg int
67 #define ioctlsocket ioctl
80 #include <sys/types.h>
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
85 static void NORETURN
err (int exitcode
, const char *fmt
, ...)
92 vfprintf (stderr
, fmt
, ap
);
94 fprintf (stderr
, ": %s\n", strerror (savederrno
));
99 static void NORETURN
errx (int exitcode
, const char *fmt
, ...)
104 vfprintf (stderr
, fmt
, ap
);
106 fputc ('\n', stderr
);
112 #include <OpenGL/gl.h>
117 #ifndef GL_TEXTURE_RECTANGLE_ARB
118 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
122 #define GL_BGRA 0x80E1
125 #ifndef GL_UNSIGNED_INT_8_8_8_8
126 #define GL_UNSIGNED_INT_8_8_8_8 0x8035
129 #ifndef GL_UNSIGNED_INT_8_8_8_8_REV
130 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
133 #include <caml/fail.h>
134 #include <caml/alloc.h>
135 #include <caml/memory.h>
136 #include <caml/unixsupport.h>
141 #include FT_FREETYPE_H
144 #define lprintf printf
149 #define ARSERT(cond) for (;;) { \
151 errx (1, "%s:%d " #cond, __FILE__, __LINE__); \
165 struct slice slices
[1];
176 fz_matrix ctm
, zoomctm
, lctm
, tctm
;
185 fz_display_list
*dlist
;
192 #if !defined _WIN32 && !defined __APPLE__
199 struct pagedim
*pagedims
;
204 fz_glyph_cache
*cache
;
215 fz_colorspace
*colorspace
;
243 static void UNUSED
debug_rect (const char *cap
, fz_rect r
)
245 printf ("%s(rect) %.2f,%.2f,%.2f,%.2f\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
248 static void UNUSED
debug_bbox (const char *cap
, fz_bbox r
)
250 printf ("%s(bbox) %d,%d,%d,%d\n", cap
, r
.x0
, r
.y0
, r
.x1
, r
.y1
);
253 static void UNUSED
debug_matrix (const char *cap
, fz_matrix m
)
255 printf ("%s(matrix) %.2f,%.2f,%.2f,%.2f %.2f %.2f\n", cap
,
256 m
.a
, m
.b
, m
.c
, m
.d
, m
.e
, m
.f
);
260 static CRITICAL_SECTION critsec
;
262 static void lock (void *unused
)
265 EnterCriticalSection (&critsec
);
268 static void unlock (void *unused
)
271 LeaveCriticalSection (&critsec
);
274 static int trylock (void *unused
)
276 return TryEnterCriticalSection (&critsec
) == 0;
279 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
281 static void lock (const char *cap
)
283 int ret
= pthread_mutex_lock (&mutex
);
285 errx (1, "%s: pthread_mutex_lock: %s", cap
, strerror (ret
));
289 static void unlock (const char *cap
)
291 int ret
= pthread_mutex_unlock (&mutex
);
293 errx (1, "%s: pthread_mutex_unlock: %s", cap
, strerror (ret
));
297 static int trylock (const char *cap
)
299 int ret
= pthread_mutex_trylock (&mutex
);
301 if (ret
&& ret
!= EBUSY
) {
302 errx (1, "%s: pthread_mutex_trylock: %s", cap
, strerror (ret
));
308 static void *parse_pointer (const char *cap
, const char *s
)
313 ret
= sscanf (s
, "%" FMT_ptr
, FMT_ptr_cast (&ptr
));
315 errx (1, "%s: cannot parse pointer in `%s'", cap
, s
);
320 static int hasdata (int sock
)
324 ret
= ioctlsocket (sock
, FIONREAD
, &avail
);
325 if (ret
) sockerr (1, "hasdata: FIONREAD error ret=%d", ret
);
329 static double now (void)
335 GetSystemTimeAsFileTime (&ft
);
336 tmp
= ft
.dwHighDateTime
;
338 tmp
|= ft
.dwLowDateTime
;
343 if (gettimeofday (&tv
, NULL
)) {
344 err (1, "gettimeofday");
346 return tv
.tv_sec
+ tv
.tv_usec
*1e-6;
350 static void readdata (int fd
, char *p
, int size
)
354 n
= recv (fd
, p
, size
, 0);
356 if (!n
) errx (1, "EOF while reading");
357 sockerr (1, "recv (req %d, ret " FMT_ss
")", size
, n
);
361 static void writedata (int fd
, char *p
, int size
)
366 buf
[0] = (size
>> 24) & 0xff;
367 buf
[1] = (size
>> 16) & 0xff;
368 buf
[2] = (size
>> 8) & 0xff;
369 buf
[3] = (size
>> 0) & 0xff;
371 n
= send (fd
, buf
, 4, 0);
373 if (!n
) errx (1, "EOF while writing length");
374 sockerr (1, "send " FMT_ss
, n
);
377 n
= send (fd
, p
, size
, 0);
379 if (!n
) errx (1, "EOF while writing data");
380 sockerr (1, "send (req %d, ret " FMT_ss
")", size
, n
);
386 __attribute__ ((format (printf
, 2, 3)))
388 printd (int fd
, const char *fmt
, ...)
396 if (!buf
) err (errno
, "malloc for temp buf (%d bytes) failed", size
);
399 len
= vsnprintf (buf
, size
, fmt
, ap
);
402 if (len
> -1 && len
< size
) {
403 writedata (fd
, buf
, len
);
413 buf
= realloc (buf
, size
);
418 static void openxref (char *filename
, char *password
)
422 for (i
= 0; i
< state
.texcount
; ++i
) {
423 state
.texowners
[i
].w
= -1;
424 state
.texowners
[i
].slice
= NULL
;
428 pdf_free_xref (state
.xref
);
432 if (state
.pagedims
) {
433 free (state
.pagedims
);
434 state
.pagedims
= NULL
;
436 state
.pagedimcount
= 0;
438 fz_set_aa_level (state
.ctx
, state
.aalevel
);
439 state
.xref
= pdf_open_xref (state
.ctx
, filename
);
440 if (pdf_needs_password (state
.xref
)) {
441 int okay
= pdf_authenticate_password (state
.xref
, password
);
443 errx (1, "invalid password");
446 state
.pagecount
= pdf_count_pages (state
.xref
);
449 static void pdfinfo (void)
453 printd (state
.sock
, "info PDF version\t%d.%d",
454 state
.xref
->version
/ 10, state
.xref
->version
% 10);
456 infoobj
= fz_dict_gets (state
.xref
->trailer
, "Info");
460 char *items
[] = { "Title", "Author", "Creator",
461 "Producer", "CreationDate" };
463 for (i
= 0; i
< sizeof (items
) / sizeof (*items
); ++i
) {
464 fz_obj
*obj
= fz_dict_gets (infoobj
, items
[i
]);
465 s
= pdf_to_utf8 (state
.ctx
, obj
);
468 printd (state
.sock
, "title %s", s
);
470 printd (state
.sock
, "info %s\t%s", items
[i
], s
);
472 fz_free (state
.ctx
, s
);
475 printd (state
.sock
, "info Pages\t%d", state
.pagecount
);
476 printd (state
.sock
, "info Dimensions\t%d", state
.pagedimcount
);
477 printd (state
.sock
, "infoend");
480 static int readlen (int fd
)
485 n
= recv (fd
, p
, 4, 0);
487 if (!n
) errx (1, "EOF while reading length");
488 sockerr (1, "recv " FMT_ss
, n
);
491 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
494 static void unlinktile (struct tile
*tile
)
498 for (i
= 0; i
< tile
->slicecount
; ++i
) {
499 struct slice
*s
= &tile
->slices
[i
];
501 if (s
->texindex
!= -1) {
502 if (state
.texowners
[s
->texindex
].slice
== s
) {
503 state
.texowners
[s
->texindex
].slice
= NULL
;
509 static void freepage (struct page
*page
)
512 fz_free_text_span (state
.ctx
, page
->text
);
514 pdf_free_page (state
.ctx
, page
->drawpage
);
515 fz_free_display_list (state
.ctx
, page
->dlist
);
519 static void freetile (struct tile
*tile
)
523 fz_drop_pixmap (state
.ctx
, tile
->pixmap
);
526 fz_drop_pixmap (state
.ctx
, state
.pig
);
528 state
.pig
= tile
->pixmap
;
536 static int cacheline32bytes
;
537 extern char **environ
;
539 static void __attribute__ ((constructor
)) clcheck (void)
541 char **envp
= environ
;
546 for (auxv
= (unsigned long *) envp
; *auxv
!= 0; auxv
+= 2) {
548 cacheline32bytes
= auxv
[1] == 32;
554 static void OPTIMIZE (3) clearpixmap (fz_pixmap
*pixmap
)
556 if (cacheline32bytes
) {
557 intptr_t a1
, a2
, diff
;
558 size_t sizea
, i
, size
= pixmap
->w
* pixmap
->h
* pixmap
->n
;
559 vector
unsigned char v
= vec_splat_u8 (-1);
560 vector
unsigned char *p
;
562 a1
= a2
= (intptr_t) pixmap
->samples
;
563 a2
= (a1
+ 31) & ~31;
568 while (a1
!= a2
) *(char *) a1
++ = 0xff;
569 for (i
= 0; i
< (sizea
& ~31); i
+= 32) {
570 __asm
volatile ("dcbz %0, %1"::"b"(a2
),"r"(i
));
572 vec_st (v
, i
+ 16, p
);
574 while (i
< sizea
) *((char *) a1
+ i
++) = 0xff;
576 else fz_clear_pixmap_with_color (pixmap
, 0xff);
579 #define clearpixmap(p) fz_clear_pixmap_with_color (p, 0xff)
582 static fz_matrix
trimctm (pdf_page
*page
, int pindex
)
585 struct pagedim
*pdim
= &state
.pagedims
[pindex
];
587 if (!pdim
->tctmready
) {
588 if (state
.trimmargins
) {
591 ctm
= fz_concat (fz_rotate (-pdim
->rotate
), fz_scale (1, -1));
592 realbox
= fz_transform_rect (ctm
, pdim
->mediabox
);
593 ctm
= fz_concat (ctm
, fz_translate (-realbox
.x0
, -realbox
.y0
));
594 ctm
= fz_concat (fz_invert_matrix (page
->ctm
), ctm
);
605 static fz_matrix
pagectm (struct page
*page
)
607 return fz_concat (trimctm (page
->drawpage
, page
->pdimno
),
608 state
.pagedims
[page
->pdimno
].ctm
);
611 static void *loadpage (int pageno
, int pindex
)
614 struct page
*page
= NULL
;
616 page
= calloc (sizeof (struct page
), 1);
618 err (1, "calloc page %d", pageno
);
621 page
->drawpage
= pdf_load_page (state
.xref
, pageno
);
622 page
->dlist
= fz_new_display_list (state
.ctx
);
623 dev
= fz_new_list_device (state
.ctx
, page
->dlist
);
624 pdf_run_page (state
.xref
, page
->drawpage
, dev
, fz_identity
, NULL
);
625 fz_free_device (dev
);
627 page
->pdimno
= pindex
;
628 page
->pageno
= pageno
;
629 page
->gen
= state
.gen
;
634 static struct tile
*alloctile (int h
)
641 slicecount
= (h
+ state
.sliceheight
- 1) / state
.sliceheight
;
642 tilesize
= sizeof (*tile
) + ((slicecount
- 1) * sizeof (struct slice
));
643 tile
= calloc (tilesize
, 1);
645 err (1, "can not allocate tile (" FMT_s
" bytes)", tilesize
);
647 for (i
= 0; i
< slicecount
; ++i
) {
648 int sh
= MIN (h
, state
.sliceheight
);
649 tile
->slices
[i
].h
= sh
;
650 tile
->slices
[i
].texindex
= -1;
653 tile
->slicecount
= slicecount
;
657 static struct tile
*rendertile (struct page
*page
, int x
, int y
, int w
, int h
)
662 struct pagedim
*pdim
;
664 tile
= alloctile (h
);
665 pdim
= &state
.pagedims
[page
->pdimno
];
670 bbox
.x1
= bbox
.x0
+ w
;
671 bbox
.y1
= bbox
.y0
+ h
;
674 if (state
.pig
->w
== w
676 && state
.pig
->colorspace
== state
.colorspace
) {
677 tile
->pixmap
= state
.pig
;
678 tile
->pixmap
->x
= bbox
.x0
;
679 tile
->pixmap
->y
= bbox
.y0
;
682 fz_drop_pixmap (state
.ctx
, state
.pig
);
688 fz_new_pixmap_with_rect (state
.ctx
, state
.colorspace
, bbox
);
693 clearpixmap (tile
->pixmap
);
694 dev
= fz_new_draw_device (state
.ctx
, tile
->pixmap
);
695 fz_execute_display_list (page
->dlist
, dev
, pagectm (page
), bbox
, NULL
);
696 fz_free_device (dev
);
701 static void initpdims (void)
707 for (pageno
= 0; pageno
< state
.pagecount
; ++pageno
) {
711 fz_rect mediabox
, cropbox
;
713 pageobj
= state
.xref
->page_objs
[pageno
];
715 if (state
.trimmargins
) {
719 page
= pdf_load_page (state
.xref
, pageno
);
720 obj
= fz_dict_gets (pageobj
, "llpp.TrimBox");
721 if (state
.trimanew
|| !obj
) {
727 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
728 dev
->hints
|= FZ_IGNORE_SHADE
;
729 ctm
= fz_invert_matrix (page
->ctm
);
730 pdf_run_page (state
.xref
, page
, dev
, fz_identity
, NULL
);
731 fz_free_device (dev
);
733 rect
.x0
= bbox
.x0
+ state
.trimfuzz
.x0
;
734 rect
.x1
= bbox
.x1
+ state
.trimfuzz
.x1
;
735 rect
.y0
= bbox
.y0
+ state
.trimfuzz
.y0
;
736 rect
.y1
= bbox
.y1
+ state
.trimfuzz
.y1
;
737 rect
= fz_transform_rect (ctm
, rect
);
738 rect
= fz_intersect_rect (rect
, page
->mediabox
);
740 if (fz_is_empty_rect (rect
)) {
741 mediabox
= page
->mediabox
;
747 obj
= fz_new_array (state
.ctx
, 4);
748 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x0
));
749 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y0
));
750 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.x1
));
751 fz_array_push (obj
, fz_new_real (state
.ctx
, mediabox
.y1
));
752 fz_dict_puts (pageobj
, "llpp.TrimBox", obj
);
755 mediabox
.x0
= fz_to_real (fz_array_get (obj
, 0));
756 mediabox
.y0
= fz_to_real (fz_array_get (obj
, 1));
757 mediabox
.x1
= fz_to_real (fz_array_get (obj
, 2));
758 mediabox
.y1
= fz_to_real (fz_array_get (obj
, 3));
761 rotate
= page
->rotate
;
762 pdf_free_page (state
.ctx
, page
);
764 printd (state
.sock
, "progress %f Trimming %d",
765 (double) (pageno
+ 1) / state
.pagecount
,
769 mediabox
= pdf_to_rect (state
.ctx
, fz_dict_gets (pageobj
, "MediaBox"));
770 if (fz_is_empty_rect (mediabox
)) {
771 fprintf (stderr
, "cannot find page size for page %d\n", pageno
+1);
778 cropbox
= pdf_to_rect (state
.ctx
, fz_dict_gets (pageobj
, "CropBox"));
779 if (!fz_is_empty_rect (cropbox
)) {
780 mediabox
= fz_intersect_rect (mediabox
, cropbox
);
782 rotate
= fz_to_int (fz_dict_gets (pageobj
, "Rotate"));
785 if (state
.pagedimcount
== 0
786 || (p
= &state
.pagedims
[state
.pagedimcount
-1], p
->rotate
!= rotate
)
787 || memcmp (&p
->mediabox
, &mediabox
, sizeof (mediabox
))) {
790 size
= (state
.pagedimcount
+ 1) * sizeof (*state
.pagedims
);
791 state
.pagedims
= realloc (state
.pagedims
, size
);
792 if (!state
.pagedims
) {
793 err (1, "realloc pagedims to " FMT_s
" (%d elems)",
794 size
, state
.pagedimcount
+ 1);
797 p
= &state
.pagedims
[state
.pagedimcount
++];
799 p
->mediabox
= mediabox
;
804 if (state
.trimmargins
) {
805 printd (state
.sock
, "progress 1 Trimmed %d pages in %f seconds",
806 state
.pagecount
, end
- start
);
809 printd (state
.sock
, "vmsg Processed %d pages in %f seconds",
810 state
.pagecount
, end
- start
);
815 static void layout (void)
820 double zoom
, w
, maxw
= 0;
821 struct pagedim
*p
= state
.pagedims
;
823 if (state
.proportional
) {
824 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
825 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
828 maxw
= MAX (w
, maxw
);
833 for (pindex
= 0; pindex
< state
.pagedimcount
; ++pindex
, ++p
) {
836 ctm
= fz_rotate (state
.rotate
);
837 box
= fz_transform_rect (fz_rotate (p
->rotate
+ state
.rotate
),
841 if (state
.proportional
) {
842 double scale
= w
/ maxw
;
843 zoom
= (state
.w
/ w
) * scale
;
849 p
->zoomctm
= fz_scale (zoom
, zoom
);
850 ctm
= fz_concat (p
->zoomctm
, ctm
);
852 p
->pagebox
= fz_transform_rect (fz_rotate (p
->rotate
), p
->mediabox
);
853 p
->pagebox
.x1
-= p
->pagebox
.x0
;
854 p
->pagebox
.y1
-= p
->pagebox
.y0
;
857 bbox
= fz_round_rect (fz_transform_rect (ctm
, p
->pagebox
));
860 p
->left
= state
.proportional
? ((maxw
- w
) * zoom
) / 2.0 : 0;
864 ctm
= fz_concat (ctm
, fz_translate (0, -p
->mediabox
.y1
));
865 ctm
= fz_concat (ctm
, fz_scale (zoom
, -zoom
));
866 ctm
= fz_concat (ctm
, fz_rotate (p
->rotate
+ state
.rotate
));
872 while (p
-- != state
.pagedims
) {
873 int w
= p
->bounds
.x1
- p
->bounds
.x0
;
874 int h
= p
->bounds
.y1
- p
->bounds
.y0
;
876 printd (state
.sock
, "pdim %d %d %d %d", p
->pageno
, w
, h
, p
->left
);
880 static void recurse_outline (fz_outline
*outline
, int level
)
885 struct pagedim
*pdim
= state
.pagedims
;
887 dest
= &outline
->dest
;
888 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
889 if (state
.pagedims
[i
].pageno
> dest
->ld
.gotor
.page
)
891 pdim
= &state
.pagedims
[i
];
893 if (dest
->ld
.gotor
.flags
& fz_link_flag_t_valid
) {
896 p
.y
= dest
->ld
.gotor
.lt
.y
;
897 p
= fz_transform_point (pdim
->lctm
, p
);
900 if (dest
->ld
.gotor
.page
>= 0 && dest
->ld
.gotor
.page
< 1<<30) {
904 y0
= MIN (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
905 y1
= MAX (pdim
->bounds
.y0
, pdim
->bounds
.y1
);
907 printd (state
.sock
, "o %d %d %d %d %s",
908 level
, dest
->ld
.gotor
.page
, top
, h
, outline
->title
);
911 recurse_outline (outline
->down
, level
+ 1);
913 outline
= outline
->next
;
917 static void process_outline (void)
921 if (!state
.needoutline
) return;
923 state
.needoutline
= 0;
924 outline
= pdf_load_outline (state
.xref
);
926 recurse_outline (outline
, 0);
927 fz_free_outline (outline
);
931 static int comparespans (const void *l
, const void *r
)
933 fz_text_span
const *const*ls
= l
;
934 fz_text_span
const *const*rs
= r
;
935 return (*ls
)->text
->bbox
.y0
- (*rs
)->text
->bbox
.y0
;
938 /* wishful thinking function */
939 static void search (regex_t
*re
, int pageno
, int y
, int forward
)
948 fz_text_span
*text
, *span
, **pspan
;
949 struct pagedim
*pdim
, *pdimprev
;
956 while (pageno
>= 0 && pageno
< state
.pagecount
&& !stop
) {
959 if (hasdata (state
.sock
)) {
961 "progress 1 attention requested aborting search at %d",
966 printd (state
.sock
, "progress %f searching in page %d",
967 (double) (pageno
+ 1) / state
.pagecount
,
972 for (i
= 0; i
< state
.pagedimcount
; ++i
) {
973 pdim
= &state
.pagedims
[i
];
974 if (pdim
->pageno
== pageno
) {
977 if (pdim
->pageno
> pageno
) {
986 drawpage
= pdf_load_page (state
.xref
, pageno
);
988 text
= fz_new_text_span (state
.ctx
);
989 tdev
= fz_new_text_device (state
.ctx
, text
);
990 pdf_run_page (state
.xref
, drawpage
, tdev
, fz_identity
, NULL
);
991 fz_free_device (tdev
);
994 for (span
= text
; span
; span
= span
->next
) {
997 pspan
= malloc (sizeof (void *) * nspans
);
999 err (1, "malloc span pointers " FMT_s
, sizeof (void *) * nspans
);
1001 for (i
= 0, span
= text
; span
; span
= span
->next
, ++i
) {
1004 qsort (pspan
, nspans
, sizeof (fz_text_span
*), comparespans
);
1006 j
= forward
? 0 : nspans
- 1;
1011 j
+= forward
? 1 : -1;
1013 for (i
= 0; i
< MIN (span
->len
, (int) sizeof (buf
) - 1); ++i
) {
1015 if (span
->text
[i
].bbox
.y0
< y
+ 1) {
1020 if (span
->text
[i
].bbox
.y0
> y
- 1) {
1024 if (span
->text
[i
].c
< 256) {
1025 *p
++ = span
->text
[i
].c
;
1036 ret
= regexec (re
, buf
, 1, &rm
, 0);
1038 if (ret
!= REG_NOMATCH
) {
1041 size
= regerror (ret
, re
, errbuf
, sizeof (errbuf
));
1043 "msg regexec error `%.*s'",
1044 (int) size
, errbuf
);
1045 fz_free_text_span (state
.ctx
, text
);
1046 pdf_free_page (state
.ctx
, drawpage
);
1053 fz_point p1
, p2
, p3
, p4
;
1055 sb
= &span
->text
[rm
.rm_so
].bbox
;
1056 eb
= &span
->text
[rm
.rm_eo
- 1].bbox
;
1067 trimctm (drawpage
, pdim
- state
.pagedims
);
1068 ctm
= fz_concat (pdim
->tctm
, pdim
->zoomctm
);
1070 p1
= fz_transform_point (ctm
, p1
);
1071 p2
= fz_transform_point (ctm
, p2
);
1072 p3
= fz_transform_point (ctm
, p3
);
1073 p4
= fz_transform_point (ctm
, p4
);
1077 "firstmatch %d %d %f %f %f %f %f %f %f %f",
1085 "progress 1 found at %d `%.*s' in %f sec",
1086 pageno
, (int) (rm
.rm_eo
- rm
.rm_so
), &buf
[rm
.rm_so
],
1091 "match %d %d %f %f %f %f %f %f %f %f",
1109 fz_free_text_span (state
.ctx
, text
);
1110 pdf_free_page (state
.ctx
, drawpage
);
1115 printd (state
.sock
, "progress 1 no matches %f sec", end
- start
);
1117 printd (state
.sock
, "clearrects");
1120 static void set_tex_params (int colorspace
)
1122 switch (colorspace
) {
1124 state
.texiform
= GL_RGBA8
;
1125 state
.texform
= GL_RGBA
;
1126 state
.texty
= GL_UNSIGNED_BYTE
;
1127 state
.colorspace
= fz_device_rgb
;
1130 state
.texiform
= GL_RGBA8
;
1131 state
.texform
= GL_BGRA
;
1132 state
.texty
= fz_is_big_endian ()
1133 ? GL_UNSIGNED_INT_8_8_8_8
1134 : GL_UNSIGNED_INT_8_8_8_8_REV
;
1135 state
.colorspace
= fz_device_bgr
;
1138 state
.texiform
= GL_LUMINANCE_ALPHA
;
1139 state
.texform
= GL_LUMINANCE_ALPHA
;
1140 state
.texty
= GL_UNSIGNED_BYTE
;
1141 state
.colorspace
= fz_device_gray
;
1144 errx (1, "invalid colorspce %d", colorspace
);
1154 mainloop (void *unused
)
1157 int len
, ret
, oldlen
= 0;
1160 len
= readlen (state
.sock
);
1162 errx (1, "readlen returned 0");
1165 if (oldlen
< len
+ 1) {
1166 p
= realloc (p
, len
+ 1);
1168 err (1, "realloc %d failed", len
+ 1);
1172 readdata (state
.sock
, p
, len
);
1175 if (!strncmp ("open", p
, 4)) {
1178 char *filename
= p
+ 5;
1180 filenamelen
= strlen (filename
);
1181 password
= filename
+ filenamelen
+ 1;
1183 openxref (filename
, password
);
1184 printd (state
.sock
, "msg Opened %s (press h/F1 to get help)",
1188 state
.needoutline
= 1;
1190 else if (!strncmp ("cs", p
, 2)) {
1193 ret
= sscanf (p
+ 2, " %d", &colorspace
);
1195 errx (1, "malformed aa `%.*s' ret=%d", len
, p
, ret
);
1198 set_tex_params (colorspace
);
1199 for (i
= 0; i
< state
.texcount
; ++i
) {
1200 state
.texowners
[i
].w
= -1;
1201 state
.texowners
[i
].slice
= NULL
;
1205 else if (!strncmp ("freepage", p
, 8)) {
1208 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1210 errx (1, "malformed freepage `%.*s' ret=%d", len
, p
, ret
);
1214 else if (!strncmp ("freetile", p
, 8)) {
1217 ret
= sscanf (p
+ 8, " %" FMT_ptr
, FMT_ptr_cast (&ptr
));
1219 errx (1, "malformed freetile `%.*s' ret=%d", len
, p
, ret
);
1223 else if (!strncmp ("search", p
, 6)) {
1224 int icase
, pageno
, y
, ret
, len2
, forward
;
1228 ret
= sscanf (p
+ 6, " %d %d %d %d,%n",
1229 &icase
, &pageno
, &y
, &forward
, &len2
);
1231 errx (1, "malformed search `%s' ret=%d", p
, ret
);
1234 pattern
= p
+ 6 + len2
;
1235 ret
= regcomp (&re
, pattern
,
1236 REG_EXTENDED
| (icase
? REG_ICASE
: 0));
1241 size
= regerror (ret
, &re
, errbuf
, sizeof (errbuf
));
1242 printd (state
.sock
, "msg regcomp failed `%.*s'",
1243 (int) size
, errbuf
);
1246 search (&re
, pageno
, y
, forward
);
1250 else if (!strncmp ("geometry", p
, 8)) {
1253 printd (state
.sock
, "clear");
1254 ret
= sscanf (p
+ 8, " %d %d", &w
, &h
);
1256 errx (1, "malformed geometry `%.*s' ret=%d", len
, p
, ret
);
1264 for (i
= 0; i
< state
.texcount
; ++i
) {
1265 state
.texowners
[i
].slice
= NULL
;
1271 unlock ("geometry");
1272 printd (state
.sock
, "continue %d", state
.pagecount
);
1274 else if (!strncmp ("reqlayout", p
, 9)) {
1275 int rotate
, proportional
;
1277 printd (state
.sock
, "clear");
1278 ret
= sscanf (p
+ 9, " %d %d", &rotate
, &proportional
);
1280 errx (1, "bad reqlayout line `%.*s' ret=%d", len
, p
, ret
);
1283 state
.rotate
= rotate
;
1284 state
.proportional
= proportional
;
1286 unlock ("reqlayout");
1287 printd (state
.sock
, "continue %d", state
.pagecount
);
1289 else if (!strncmp ("page", p
, 4)) {
1292 int pageno
, pindex
, ret
;
1294 ret
= sscanf (p
+ 4, " %d %d", &pageno
, &pindex
);
1296 errx (1, "bad render line `%.*s' ret=%d", len
, p
, ret
);
1301 page
= loadpage (pageno
, pindex
);
1305 printd (state
.sock
, "page %" FMT_ptr
" %f",
1306 FMT_ptr_cast2 (page
), b
- a
);
1308 else if (!strncmp ("tile", p
, 4)) {
1309 int x
, y
, w
, h
, ret
;
1314 ret
= sscanf (p
+ 4, " %" FMT_ptr
" %d %d %d %d",
1315 FMT_ptr_cast (&page
), &x
, &y
, &w
, &h
);
1317 errx (1, "bad tile line `%.*s' ret=%d", len
, p
, ret
);
1322 tile
= rendertile (page
, x
, y
, w
, h
);
1326 printd (state
.sock
, "tile %d %d %" FMT_ptr
" %u %f",
1328 FMT_ptr_cast2 (tile
),
1329 tile
->w
* tile
->h
* tile
->pixmap
->n
,
1332 else if (!strncmp ("settrim", p
, 7)) {
1336 ret
= sscanf (p
+ 7, " %d %d %d %d %d", &trimmargins
,
1337 &fuzz
.x0
, &fuzz
.y0
, &fuzz
.x1
, &fuzz
.y1
);
1339 errx (1, "malformed settrim `%.*s' ret=%d", len
, p
, ret
);
1341 printd (state
.sock
, "clear");
1343 state
.trimmargins
= trimmargins
;
1344 state
.needoutline
= 1;
1345 if (memcmp (&fuzz
, &state
.trimfuzz
, sizeof (fuzz
))) {
1347 state
.trimfuzz
= fuzz
;
1349 state
.pagedimcount
= 0;
1350 free (state
.pagedims
);
1351 state
.pagedims
= NULL
;
1356 printd (state
.sock
, "continue %d", state
.pagecount
);
1358 else if (!strncmp ("interrupt", p
, 9)) {
1359 printd (state
.sock
, "vmsg interrupted");
1361 else if (!strncmp ("quit", p
, 4)) {
1365 errx (1, "unknown command %.*s", len
, p
);
1371 static void showsel (struct page
*page
, int ox
, int oy
)
1375 struct mark first
, last
;
1377 first
= page
->fmark
;
1380 if (!first
.span
|| !last
.span
) return;
1382 glEnable (GL_BLEND
);
1383 glBlendFunc (GL_SRC_ALPHA
, GL_SRC_ALPHA
);
1384 glColor4f (0.5f
, 0.5f
, 0.0f
, 0.6f
);
1386 ox
-= state
.pagedims
[page
->pdimno
].bounds
.x0
;
1387 oy
-= state
.pagedims
[page
->pdimno
].bounds
.y0
;
1388 for (span
= first
.span
; span
; span
= span
->next
) {
1391 bbox
.x0
= bbox
.y0
= bbox
.x1
= bbox
.y1
= 0;
1396 if (span
== page
->fmark
.span
&& span
== page
->lmark
.span
) {
1397 j
= MIN (first
.i
, last
.i
);
1398 k
= MAX (first
.i
, last
.i
);
1400 else if (span
== first
.span
) {
1403 else if (span
== last
.span
) {
1407 for (i
= j
; i
<= k
; ++i
) {
1408 bbox
= fz_union_bbox (bbox
, span
->text
[i
].bbox
);
1410 lprintf ("%d %d %d %d oy=%d ox=%d\n",
1417 glRecti (bbox
.x0
+ ox
, bbox
.y0
+ oy
, bbox
.x1
+ ox
, bbox
.y1
+ oy
);
1419 if (span
== last
.span
) break;
1421 glDisable (GL_BLEND
);
1424 static void highlightlinks (struct page
*page
, int xoff
, int yoff
)
1429 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1430 glEnable (GL_LINE_STIPPLE
);
1431 glLineStipple (0.5, 0xcccc);
1433 ctm
= fz_concat (pagectm (page
), fz_translate (xoff
, yoff
));
1436 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1437 fz_point p1
, p2
, p3
, p4
;
1439 p1
.x
= link
->rect
.x0
;
1440 p1
.y
= link
->rect
.y0
;
1442 p2
.x
= link
->rect
.x1
;
1443 p2
.y
= link
->rect
.y0
;
1445 p3
.x
= link
->rect
.x1
;
1446 p3
.y
= link
->rect
.y1
;
1448 p4
.x
= link
->rect
.x0
;
1449 p4
.y
= link
->rect
.y1
;
1451 p1
= fz_transform_point (ctm
, p1
);
1452 p2
= fz_transform_point (ctm
, p2
);
1453 p3
= fz_transform_point (ctm
, p3
);
1454 p4
= fz_transform_point (ctm
, p4
);
1456 switch (link
->dest
.kind
) {
1457 case FZ_LINK_GOTO
: glColor3ub (255, 0, 0); break;
1458 case FZ_LINK_URI
: glColor3ub (0, 0, 255); break;
1459 default: glColor3ub (0, 0, 0); break;
1462 glVertex2f (p1
.x
, p1
.y
);
1463 glVertex2f (p2
.x
, p2
.y
);
1464 glVertex2f (p3
.x
, p3
.y
);
1465 glVertex2f (p4
.x
, p4
.y
);
1469 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1470 glDisable (GL_LINE_STIPPLE
);
1473 static void uploadslice (struct tile
*tile
, struct slice
*slice
)
1476 struct slice
*slice1
;
1479 for (slice1
= tile
->slices
; slice
!= slice1
; slice1
++) {
1480 offset
+= slice1
->h
* tile
->w
* tile
->pixmap
->n
;
1482 if (slice
->texindex
!= -1
1483 && state
.texowners
[slice
->texindex
].slice
== slice
) {
1484 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[slice
->texindex
]);
1488 int texindex
= state
.texindex
++ % state
.texcount
;
1490 if (state
.texowners
[texindex
].w
== tile
->w
) {
1491 if (state
.texowners
[texindex
].h
>= slice
->h
) {
1495 state
.texowners
[texindex
].h
= slice
->h
;
1499 state
.texowners
[texindex
].h
= slice
->h
;
1502 state
.texowners
[texindex
].w
= tile
->w
;
1503 state
.texowners
[texindex
].slice
= slice
;
1504 slice
->texindex
= texindex
;
1506 glBindTexture (GL_TEXTURE_RECTANGLE_ARB
, state
.texids
[texindex
]);
1508 glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1516 tile
->pixmap
->samples
+offset
1520 glTexImage2D (GL_TEXTURE_RECTANGLE_ARB
,
1528 tile
->pixmap
->samples
+offset
1534 CAMLprim value
ml_drawtile (value args_v
, value ptr_v
)
1536 CAMLparam2 (args_v
, ptr_v
);
1537 int dispx
= Int_val (Field (args_v
, 0));
1538 int dispy
= Int_val (Field (args_v
, 1));
1539 int dispw
= Int_val (Field (args_v
, 2));
1540 int disph
= Int_val (Field (args_v
, 3));
1541 int tilex
= Int_val (Field (args_v
, 4));
1542 int tiley
= Int_val (Field (args_v
, 5));
1543 char *s
= String_val (ptr_v
);
1544 struct tile
*tile
= parse_pointer ("ml_drawtile", s
);
1546 glEnable (GL_TEXTURE_RECTANGLE_ARB
);
1548 int slicey
, firstslice
;
1549 struct slice
*slice
;
1551 firstslice
= tiley
/ state
.sliceheight
;
1552 slice
= &tile
->slices
[firstslice
];
1553 slicey
= tiley
% state
.sliceheight
;
1558 dh
= slice
->h
- slicey
;
1559 dh
= MIN (disph
, dh
);
1560 uploadslice (tile
, slice
);
1564 glTexCoord2i (tilex
, slicey
);
1565 glVertex2i (dispx
, dispy
);
1567 glTexCoord2i (tilex
+dispw
, slicey
);
1568 glVertex2i (dispx
+dispw
, dispy
);
1570 glTexCoord2i (tilex
+dispw
, slicey
+dh
);
1571 glVertex2i (dispx
+dispw
, dispy
+dh
);
1573 glTexCoord2i (tilex
, slicey
+dh
);
1574 glVertex2i (dispx
, dispy
+dh
);
1581 ARSERT (!(slice
- tile
->slices
>= tile
->slicecount
&& disph
> 0));
1585 glDisable (GL_TEXTURE_RECTANGLE_ARB
);
1586 CAMLreturn (Val_unit
);
1589 CAMLprim value
ml_postprocess (value ptr_v
, value hlinks_v
,
1590 value xoff_v
, value yoff_v
)
1592 CAMLparam4 (ptr_v
, hlinks_v
, xoff_v
, yoff_v
);
1593 int xoff
= Int_val (xoff_v
);
1594 int yoff
= Int_val (yoff_v
);
1595 char *s
= String_val (ptr_v
);
1596 struct page
*page
= parse_pointer ("ml_postprocess", s
);
1598 if (Bool_val (hlinks_v
)) highlightlinks (page
, xoff
, yoff
);
1600 if (trylock ("ml_postprocess")) {
1603 showsel (page
, xoff
, yoff
);
1604 unlock ("ml_postprocess");
1607 CAMLreturn (Val_unit
);
1610 static fz_link
*getlink (struct page
*page
, int x
, int y
)
1619 ctm
= fz_concat (trimctm (page
->drawpage
, page
->pdimno
),
1620 state
.pagedims
[page
->pdimno
].ctm
);
1621 ctm
= fz_invert_matrix (ctm
);
1622 p
= fz_transform_point (ctm
, p
);
1624 for (link
= page
->drawpage
->links
; link
; link
= link
->next
) {
1625 if (p
.x
>= link
->rect
.x0
&& p
.x
<= link
->rect
.x1
) {
1626 if (p
.y
>= link
->rect
.y0
&& p
.y
<= link
->rect
.y1
) {
1634 static void droptext (struct page
*page
)
1637 fz_free_text_span (state
.ctx
, page
->text
);
1640 page
->fmark
.span
= NULL
;
1641 page
->lmark
.span
= NULL
;
1646 static void ensuretext (struct page
*page
)
1648 if (state
.gen
!= page
->gen
) {
1650 page
->gen
= state
.gen
;
1655 page
->text
= fz_new_text_span (state
.ctx
);
1656 tdev
= fz_new_text_device (state
.ctx
, page
->text
);
1657 fz_execute_display_list (page
->dlist
,
1660 fz_infinite_bbox
, NULL
);
1661 fz_free_device (tdev
);
1665 CAMLprim value
ml_whatsunder (value ptr_v
, value x_v
, value y_v
)
1667 CAMLparam3 (ptr_v
, x_v
, y_v
);
1668 CAMLlocal3 (ret_v
, tup_v
, str_v
);
1671 char *s
= String_val (ptr_v
);
1672 int x
= Int_val (x_v
), y
= Int_val (y_v
);
1673 struct pagedim
*pdim
;
1675 ret_v
= Val_int (0);
1676 if (trylock ("ml_whatsunder")) {
1680 page
= parse_pointer ("ml_whatsunder", s
);
1681 pdim
= &state
.pagedims
[page
->pdimno
];
1682 x
+= pdim
->bounds
.x0
;
1683 y
+= pdim
->bounds
.y0
;
1684 link
= getlink (page
, x
, y
);
1686 switch (link
->dest
.kind
) {
1692 pageno
= link
->dest
.ld
.gotor
.page
;
1696 if (link
->dest
.ld
.gotor
.flags
& fz_link_flag_t_valid
) {
1697 p
.y
= link
->dest
.ld
.gotor
.lt
.y
;
1698 p
= fz_transform_point (pdim
->lctm
, p
);
1700 tup_v
= caml_alloc_tuple (2);
1701 ret_v
= caml_alloc_small (1, 1);
1702 Field (tup_v
, 0) = Val_int (pageno
);
1703 Field (tup_v
, 1) = Val_int (p
.y
);
1704 Field (ret_v
, 0) = tup_v
;
1709 str_v
= caml_copy_string (link
->dest
.ld
.uri
.uri
);
1710 ret_v
= caml_alloc_small (1, 0);
1711 Field (ret_v
, 0) = str_v
;
1715 printd (state
.sock
, "msg unhandled link kind %d", link
->dest
.kind
);
1724 for (span
= page
->text
; span
; span
= span
->next
) {
1725 for (i
= 0; i
< span
->len
; ++i
) {
1727 b
= &span
->text
[i
].bbox
;
1728 if ((x
>= b
->x0
&& x
<= b
->x1
&& y
>= b
->y0
&& y
<= b
->y1
)) {
1730 span
->font
&& span
->font
->name
1732 : "Span has no font name"
1734 FT_FaceRec
*face
= span
->font
->ft_face
;
1735 if (face
&& face
->family_name
) {
1737 char *n1
= face
->family_name
;
1738 size_t l1
= strlen (n1
);
1739 size_t l2
= strlen (n2
);
1741 if (l1
!= l2
|| memcmp (n1
, n2
, l1
)) {
1742 s
= malloc (l1
+ l2
+ 2);
1746 memcpy (s
+ l2
+ 1, n1
, l1
+ 1);
1747 str_v
= caml_copy_string (s
);
1753 str_v
= caml_copy_string (n2
);
1755 ret_v
= caml_alloc_small (1, 2);
1756 Field (ret_v
, 0) = str_v
;
1763 unlock ("ml_whatsunder");
1769 CAMLprim value
ml_seltext (value ptr_v
, value rect_v
)
1771 CAMLparam2 (ptr_v
, rect_v
);
1775 struct mark first
, last
;
1776 int i
, x0
, x1
, y0
, y1
;
1777 struct pagedim
*pdim
;
1778 char *s
= String_val (ptr_v
);
1780 if (trylock ("ml_seltext")) {
1784 page
= parse_pointer ("ml_seltext", s
);
1787 pdim
= &state
.pagedims
[page
->pdimno
];
1789 x0
= Int_val (Field (rect_v
, 0)) + pdim
->bounds
.x0
;
1790 y0
= Int_val (Field (rect_v
, 1)) + pdim
->bounds
.y0
;
1791 x1
= Int_val (Field (rect_v
, 2)) + pdim
->bounds
.x0
;
1792 y1
= Int_val (Field (rect_v
, 3)) + pdim
->bounds
.y0
;
1795 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1796 glColor3ub (128, 128, 128);
1797 glRecti (x0
, y0
, x1
, y1
);
1798 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1804 last
.i
= first
.i
= 0;
1805 first
.span
= page
->text
;
1806 for (span
= page
->text
; span
; span
= span
->next
) {
1807 for (i
= 0; i
< span
->len
; ++i
) {
1808 b
= &span
->text
[i
].bbox
;
1811 if (x0
>= b
->x0
&& x0
<= b
->x1
&& y0
>= b
->y0
&& y0
<= b
->y1
) {
1816 if (x1
>= b
->x0
&& x1
<= b
->x1
&& y1
>= b
->y0
&& y1
<= b
->y1
) {
1821 if (0 && selected
) {
1822 glPolygonMode (GL_FRONT_AND_BACK
, GL_LINE
);
1823 glColor3ub (128, 128, 128);
1824 glRecti (b
->x0
, b
->y0
, b
->x1
, b
->y1
);
1825 glPolygonMode (GL_FRONT_AND_BACK
, GL_FILL
);
1830 if (y1
< y0
|| x1
< x0
) {
1833 if (first
.span
== last
.span
) {
1838 for (span
= first
.span
; span
&& span
!= last
.span
;
1839 span
= span
->next
) {
1852 first
.span
= last
.span
;
1858 page
->fmark
= first
;
1861 unlock ("ml_seltext");
1864 CAMLreturn (Val_unit
);
1867 static int pipespan (FILE *f
, fz_text_span
*span
, int a
, int b
)
1872 for (i
= a
; i
<= b
; ++i
) {
1873 len
= runetochar (buf
, &span
->text
[i
].c
);
1874 ret
= fwrite (buf
, len
, 1, f
);
1877 printd (state
.sock
, "msg failed to write %d bytes ret=%d: %s",
1878 len
, ret
, strerror (errno
));
1885 CAMLprim value
ml_copysel (value ptr_v
)
1890 char *s
= String_val (ptr_v
);
1892 if (trylock ("ml_copysel")) {
1899 if (state
.xselpipe
) {
1900 int ret
= pclose (state
.xselpipe
);
1902 printd (state
.sock
, "msg failed to close xsel pipe `%s'",
1905 state
.xselpipe
= NULL
;
1908 printf ("========================================\n");
1914 page
= parse_pointer ("ml_sopysel", s
);
1916 if (!page
->fmark
.span
|| !page
->lmark
.span
) {
1917 printd (state
.sock
, "msg nothing to copy");
1923 if (!state
.xselpipe
) {
1924 state
.xselpipe
= popen ("xsel -i", "w");
1925 if (!state
.xselpipe
) {
1926 printd (state
.sock
, "msg failed to open xsel pipe `%s'",
1938 for (span
= page
->fmark
.span
;
1939 span
&& span
!= page
->lmark
.span
->next
;
1940 span
= span
->next
) {
1941 int a
= span
== page
->fmark
.span
? page
->fmark
.i
: 0;
1942 int b
= span
== page
->lmark
.span
? page
->lmark
.i
: span
->len
- 1;
1943 if (pipespan (f
, span
, a
, b
)) {
1947 if (putc ('\n', f
) == EOF
) {
1949 "msg failed break line on xsel pipe `%s'",
1955 page
->lmark
.span
= NULL
;
1956 page
->fmark
.span
= NULL
;
1960 unlock ("ml_copysel");
1963 CAMLreturn (Val_unit
);
1966 CAMLprim value
ml_getpdimrect (value pagedimno_v
)
1968 CAMLparam1 (pagedimno_v
);
1970 int pagedimno
= Int_val (pagedimno_v
);
1973 ret_v
= caml_alloc_small (4 * Double_wosize
, Double_array_tag
);
1974 if (trylock ("ml_getpdimrect")) {
1975 box
= fz_empty_rect
;
1978 box
= state
.pagedims
[pagedimno
].mediabox
;
1979 unlock ("ml_getpdimrect");
1982 Store_double_field (ret_v
, 0, box
.x0
);
1983 Store_double_field (ret_v
, 1, box
.x1
);
1984 Store_double_field (ret_v
, 2, box
.y0
);
1985 Store_double_field (ret_v
, 3, box
.y1
);
1990 static double getmaxw (void)
1996 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
1999 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2000 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2003 maxw
= MAX (w
, maxw
);
2008 CAMLprim value
ml_getmaxw (value unit_v
)
2010 CAMLparam1 (unit_v
);
2014 if (trylock ("ml_getmaxw")) {
2018 unlock ("ml_getmaxw");
2020 ret_v
= caml_copy_double (maxw
);
2024 CAMLprim value
ml_zoom_for_height (value winw_v
, value winh_v
, value dw_v
)
2026 CAMLparam3 (winw_v
, winh_v
, dw_v
);
2030 double maxw
= 0.0, maxh
= 0.0;
2032 double winw
= Int_val (winw_v
);
2033 double winh
= Int_val (winh_v
);
2034 double dw
= Int_val (dw_v
);
2035 double pw
= 1.0, ph
= 1.0, num
, den
;
2037 if (trylock ("ml_zoom_for_height")) {
2041 if (state
.proportional
) {
2045 for (i
= 0, p
= state
.pagedims
; i
< state
.pagedimcount
; ++i
, ++p
) {
2046 double x0
, x1
, y0
, y1
, w
, h
, scaledh
, scale
;
2048 x0
= MIN (p
->mediabox
.x0
, p
->mediabox
.x1
);
2049 x1
= MAX (p
->mediabox
.x0
, p
->mediabox
.x1
);
2050 y0
= MIN (p
->mediabox
.y0
, p
->mediabox
.y1
);
2051 y1
= MAX (p
->mediabox
.y0
, p
->mediabox
.y1
);
2056 if (state
.proportional
) {
2058 scaledh
= h
* scale
;
2065 if (scaledh
> maxh
) {
2072 num
= (winh
* pw
) + (ph
* dw
);
2076 unlock ("ml_zoom_for_height");
2078 ret_v
= caml_copy_double (zoom
);
2084 CAMLprim value
ml_draw_string (value pt_v
, value x_v
, value y_v
, value string_v
)
2086 CAMLparam4 (pt_v
, x_v
, y_v
, string_v
);
2088 int pt
= Int_val(pt_v
);
2089 int x
= Int_val (x_v
);
2090 int y
= Int_val (y_v
);
2093 w
= draw_string (state
.face
, pt
, x
, y
, String_val (string_v
));
2094 ret_v
= caml_copy_double (w
);
2098 CAMLprim value
ml_measure_string (value pt_v
, value string_v
)
2100 CAMLparam2 (pt_v
, string_v
);
2102 int pt
= Int_val (pt_v
);
2105 w
= measure_string (state
.face
, pt
, String_val (string_v
));
2106 ret_v
= caml_copy_double (w
);
2110 CAMLprim value
ml_getpagebox (value opaque_v
)
2112 CAMLparam1 (opaque_v
);
2116 char *s
= String_val (opaque_v
);
2117 struct page
*page
= parse_pointer ("ml_getpagebox", s
);
2119 ret_v
= caml_alloc_tuple (4);
2120 dev
= fz_new_bbox_device (state
.ctx
, &bbox
);
2121 dev
->hints
|= FZ_IGNORE_SHADE
;
2122 pdf_run_page (state
.xref
, page
->drawpage
, dev
, pagectm (page
), NULL
);
2123 fz_free_device (dev
);
2125 Field (ret_v
, 0) = Val_int (bbox
.x0
);
2126 Field (ret_v
, 1) = Val_int (bbox
.y0
);
2127 Field (ret_v
, 2) = Val_int (bbox
.x1
);
2128 Field (ret_v
, 3) = Val_int (bbox
.y1
);
2133 CAMLprim value
ml_setaalevel (value level_v
)
2135 CAMLparam1 (level_v
);
2137 state
.aalevel
= Int_val (level_v
);
2138 CAMLreturn (Val_unit
);
2141 #if !defined _WIN32 && !defined __APPLE__
2144 #include <X11/Xlib.h>
2145 #include <X11/Xutil.h>
2148 static void set_wm_class (int hack
)
2156 display
= getenv ("DISPLAY");
2157 dpy
= XOpenDisplay (display
);
2159 fprintf (stderr
, "XOpenDisplay `%s' failed\n",
2160 display
? display
: "null");
2163 hint
.res_name
= "llpp";
2164 hint
.res_class
= "llpp";
2165 win
= glXGetCurrentDrawable ();
2167 fprintf (stderr
, "glXGetCurrentDrawable returned None\n");
2168 XCloseDisplay (dpy
);
2171 XSetClassHint (dpy
, win
, &hint
);
2172 XCloseDisplay (dpy
);
2176 #define set_wm_class(h) (void) (h)
2179 enum { piunknown
, pilinux
, piwindows
, piosx
,
2180 pisun
, pifreebsd
, pidragonflybsd
,
2181 piopenbsd
, pimingw
, picygwin
};
2183 CAMLprim value
ml_platform (value unit_v
)
2185 CAMLparam1 (unit_v
);
2186 int platid
= piunknown
;
2188 #if defined __linux__
2190 #elif defined __CYGWIN__
2192 #elif defined __MINGW32__
2194 #elif defined _WIN32
2196 #elif defined __DragonFly__
2197 platid
= pidragonflybsd
;
2198 #elif defined __FreeBSD__
2200 #elif defined __OpenBSD__
2202 #elif defined __sun__
2204 #elif defined __APPLE__
2207 CAMLreturn (Val_int (platid
));
2210 CAMLprim value
ml_init (value sock_v
, value params_v
)
2212 CAMLparam2 (sock_v
, params_v
);
2213 CAMLlocal2 (trim_v
, fuzz_v
);
2221 state
.rotate
= Int_val (Field (params_v
, 0));
2222 state
.proportional
= Bool_val (Field (params_v
, 1));
2223 trim_v
= Field (params_v
, 2);
2224 state
.texcount
= Int_val (Field (params_v
, 3));
2225 state
.sliceheight
= Int_val (Field (params_v
, 4));
2226 state
.ctx
= fz_new_context (&fz_alloc_default
, Field (params_v
, 5));
2227 colorspace
= Int_val (Field (params_v
, 6));
2228 wmclasshack
= Bool_val (Field (params_v
, 7));
2229 fontpath
= String_val (Field (params_v
, 8));
2231 state
.trimmargins
= Bool_val (Field (trim_v
, 0));
2232 fuzz_v
= Field (trim_v
, 1);
2233 state
.trimfuzz
.x0
= Int_val (Field (fuzz_v
, 0));
2234 state
.trimfuzz
.y0
= Int_val (Field (fuzz_v
, 1));
2235 state
.trimfuzz
.x1
= Int_val (Field (fuzz_v
, 2));
2236 state
.trimfuzz
.y1
= Int_val (Field (fuzz_v
, 3));
2238 set_tex_params (colorspace
);
2239 set_wm_class (wmclasshack
);
2242 state
.face
= load_font (fontpath
);
2246 void *base
= pdf_find_substitute_font (0, 0, 0, 0, &len
);
2248 state
.face
= load_builtin_font (base
, len
);
2250 if (!state
.face
) _exit (1);
2253 state
.texids
= calloc (state
.texcount
* sizeof (*state
.texids
), 1);
2254 if (!state
.texids
) {
2255 err (1, "calloc texids " FMT_s
,
2256 state
.texcount
* sizeof (*state
.texids
));
2259 state
.texowners
= calloc (state
.texcount
* sizeof (*state
.texowners
), 1);
2260 if (!state
.texowners
) {
2261 err (1, "calloc texowners " FMT_s
,
2262 state
.texcount
* sizeof (*state
.texowners
));
2265 glGenTextures (state
.texcount
, state
.texids
);
2268 state
.sock
= Socket_val (sock_v
);
2270 state
.sock
= Int_val (sock_v
);
2274 InitializeCriticalSection (&critsec
);
2275 state
.thread
= CreateThread (NULL
, 0, mainloop
, NULL
, 0, NULL
);
2276 if (state
.thread
== INVALID_HANDLE_VALUE
) {
2277 errx (1, "CreateThread failed: %lx", GetLastError ());
2280 ret
= pthread_create (&state
.thread
, NULL
, mainloop
, NULL
);
2282 errx (1, "pthread_create: %s", strerror (ret
));
2286 CAMLreturn (Val_unit
);