4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 type params
= angle
* proportional
* texcount
* sliceheight
20 and proportional
= bool
21 and interpagespace
= int
27 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
28 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext
: string -> (int * int * int * int) -> int -> unit =
31 external copysel
: string -> unit = "ml_copysel";;
32 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
33 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
34 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
38 | Msel
of (mpos
* mpos
)
52 type textentry
= (char
* string * onhist
* onkey
* ondone
)
53 and onkey
= string -> int -> te
54 and ondone
= string -> unit
55 and histcancel
= unit -> unit
56 and onhist
= ((histcmd
-> string) * histcancel
) option
57 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
62 | TEswitch
of textentry
66 { store
= Array.create n v
73 let cbcap b
= Array.length b
.store
;;
78 b
.wc
<- (b
.wc
+ 1) mod cap;
80 b
.len
<- min
(b
.len
+ 1) cap;
83 let cbempty b
= b
.len
= 0;;
85 let cbgetg b circular dir
=
89 let rc = b
.rc + dir
in
101 else max
0 (min
rc (b
.len
-1))
107 let cbget b
= cbgetg b
false;;
108 let cbgetc b
= cbgetg b
true;;
111 let rc = b
.wc
- b
.len
in
112 let rc = if rc < 0 then cbcap b
+ rc else rc in
116 let cbdecr b
= b
.len
<- b
.len
- 1;;
131 { mutable scrollw
: int
132 ; mutable scrollh
: int
133 ; mutable icase
: bool
134 ; mutable preload
: bool
135 ; mutable pagebias
: int
136 ; mutable verbose
: bool
137 ; mutable scrollincr
: int
138 ; mutable maxhfit
: bool
139 ; mutable crophack
: bool
140 ; mutable autoscroll
: bool
141 ; mutable showall
: bool
142 ; mutable hlinks
: bool
143 ; mutable underinfo
: bool
144 ; mutable interpagespace
: interpagespace
145 ; mutable zoom
: float
146 ; mutable presentation
: bool
147 ; mutable angle
: angle
150 ; mutable savebmarks
: bool
151 ; mutable proportional
: proportional
152 ; mutable memlimit
: int
153 ; mutable texcount
: texcount
154 ; mutable sliceheight
: sliceheight
155 ; mutable thumbw
: width
159 type outline
= string * int * int * float;;
161 | Oarray
of outline array
162 | Olist
of outline list
163 | Onarrow
of string * outline array
* outline array
166 type rect
= (float * float * float * float * float * float * float * float);;
168 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
171 { mutable csock
: Unix.file_descr
172 ; mutable ssock
: Unix.file_descr
177 ; mutable layout
: layout list
178 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
179 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
180 ; mutable pagecount
: int
181 ; pagecache
: string circbuf
182 ; mutable rendering
: bool
183 ; mutable mstate
: mstate
184 ; mutable searchpattern
: string
185 ; mutable rects
: (pageno
* recttype
* rect
) list
186 ; mutable rects1
: (pageno
* recttype
* rect
) list
187 ; mutable text
: string
188 ; mutable fullscreen
: (width
* height
) option
189 ; mutable birdseye
: (conf
* leftx
* pageno
* pageno
) option
190 ; mutable textentry
: textentry
option
191 ; mutable outlines
: outlines
192 ; mutable outline
: (bool * int * int * outline array
* string) option
193 ; mutable bookmarks
: outline list
194 ; mutable path
: string
195 ; mutable password
: string
196 ; mutable invalidated
: int
197 ; mutable colorscale
: float
198 ; mutable memused
: int
200 ; mutable throttle
: layout list
option
204 { pat
: string circbuf
205 ; pag
: string circbuf
206 ; nav
: float circbuf
226 ; presentation
= false
231 ; proportional
= true
232 ; memlimit
= 32*1024*1024
239 let conf = { defconf with angle
= defconf.angle
};;
249 ; pagemap
= Hashtbl.create
10
250 ; pagecache
= cbnew 100 ""
262 ; outlines
= Olist
[]
269 { nav
= cbnew 100 0.0
283 Printf.kprintf prerr_endline fmt
285 Printf.kprintf ignore fmt
289 let len = String.length s
in
291 let b = Buffer.create
n in
292 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
293 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
294 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
295 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
296 Buffer.add_string
b s
;
297 let s'
= Buffer.contents
b in
298 let n'
= Unix.write fd
s'
0 n in
299 if n'
!= n then failwith
"write failed";
304 let n = Unix.read fd
s 0 4 in
305 if n != 4 then failwith
"incomplete read(len)";
307 lor (Char.code
s.[0] lsl 24)
308 lor (Char.code
s.[1] lsl 16)
309 lor (Char.code
s.[2] lsl 8)
310 lor (Char.code
s.[3] lsl 0)
312 let s = String.create
len in
313 let n = Unix.read fd
s 0 len in
314 if n != len then failwith
"incomplete read(data)";
321 else float y
/. float state.maxy
325 let b = Buffer.create
10 in
326 Buffer.add_string
b s;
327 let rec combine = function
330 Buffer.add_char
b ' '
;
333 | `
b b -> if b then "1" else "0"
335 | `i i
-> string_of_int i
336 | `f f
-> string_of_float f
337 | `I f
-> string_of_int
(truncate f
)
339 Buffer.add_string
b s;
346 let cmd = Buffer.contents
(makecmd s l
) in
347 writecmd state.csock
cmd;
353 let d = conf.winh
- h
in
360 let rec f pn ph pi fh l
=
362 | (n, _
, h
, _
) :: rest
->
363 let ips = calcips h
in
368 if state.birdseye
<> None
&& pn
= 0
373 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
378 if conf.presentation
|| (state.birdseye
<> None
&& pn
= 0)
382 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
385 let fh = f 0 0 0 0 state.pdims
in
389 let getpageyh pageno
=
390 let rec f pn ph pi y l
=
392 | (n, _
, h
, _
) :: rest
->
393 let ips = calcips h
in
396 if conf.presentation
&& n = pageno
398 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
400 y
+ (pageno
- pn
) * (ph
+ pi
), h
402 let y = y + (if conf.presentation
then pi
else 0) in
403 let y = y + (n - pn
) * (ph
+ pi
) in
407 y + (pageno
- pn
) * (ph
+ pi
), ph
409 f 0 0 0 0 state.pdims
412 let getpagey pageno
= fst
(getpageyh pageno
);;
415 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
416 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
418 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
419 let ips = calcips h
in
421 if conf.presentation
|| (state.birdseye
<> None
&& pageno
= 0)
425 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
427 prev
, pdims
, pdimno
, 0
429 let dy = dy + yinc in
430 let py = py + yinc in
431 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
436 if py + h
<= vy - yinc
438 let py = py + h
+ ips in
439 let dy = max
0 (py - y) in
449 let pagey = vy - py in
450 let pagevh = h
- pagey in
451 let pagevh = min
(sh
- dy) pagevh in
452 let off = if yinc > 0 then py - vy else 0 in
453 let py = py + h
+ ips in
459 ; pagedispy
= dy + off
460 ; pagey = pagey + off
461 ; pagevh = pagevh - off
465 let accu = e :: accu in
472 ~cacheleft
:(cacheleft
-1)
475 if state.invalidated
= 0
485 ~cacheleft
:(cbcap state.pagecache
)
495 let y = state.y + incr
in
497 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
501 let getopaque pageno
=
502 try Some
(Hashtbl.find
state.pagemap
503 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
504 with Not_found
-> None
507 let cache pageno opaque
=
508 Hashtbl.replace
state.pagemap
509 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
512 let validopaque opaque
= String.length opaque
> 0;;
515 match getopaque l
.pageno
with
516 | None
when not
state.rendering
->
517 state.rendering
<- true;
518 cache l
.pageno
("", -1);
519 wcmd "render" [`i
(l
.pageno
+ 1)
527 let loadlayout layout =
528 let rec f all
= function
530 begin match getopaque l
.pageno
with
531 | None
-> render l
; f false ls
532 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
536 f (layout <> []) layout;
539 let findpageforopaque opaque
=
541 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
545 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
551 let memleft = conf.memlimit
- state.memused
in
554 let opaque = cbpeek state.pagecache
in
555 match findpageforopaque opaque with
556 | Some
((n, _
, _
, _
, _
), size
) ->
557 memleft + size
>= 0 && not
(pagevisible state.layout n)
564 let presentation = conf.presentation in
565 let interpagespace = conf.interpagespace in
566 let maxy = state.maxy in
567 conf.presentation <- false;
568 conf.interpagespace <- 0;
569 state.maxy <- calcheight ();
571 match state.layout with
573 | l
:: _
-> getpagey l
.pageno
575 let y = if y < conf.winh
then 0 else y - conf.winh
in
576 let pages = layout y (conf.winh
*3) in
577 List.iter
render pages;
578 conf.presentation <- presentation;
579 conf.interpagespace <- interpagespace;
585 let y = min
state.maxy y in
586 let pages = layout y conf.winh
in
587 let ready = loadlayout pages in
593 state.layout <- pages;
594 state.throttle
<- None
;
595 Glut.postRedisplay
();
598 state.throttle
<- Some
pages;
603 state.layout <- pages;
604 state.throttle
<- None
;
605 Glut.postRedisplay
();
607 begin match state.birdseye
with
608 | Some
(conf, leftx
, pageno
, hooverpageno
) ->
609 if not
(pagevisible pages pageno
)
611 match state.layout with
614 state.birdseye
<- Some
(conf, leftx
, l
.pageno
, hooverpageno
)
621 let gotoy_and_clear_text y =
623 if not
conf.verbose
then state.text
<- "";
627 cbput state.hists
.nav
(yratio state.y);
631 let y = cbgetc state.hists
.nav ~
-1 in
632 truncate
(y *. float state.maxy)
636 let y, h
= getpageyh n in
638 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
641 let gotopage1 n top
=
642 let y = getpagey n in
644 gotoy_and_clear_text (y + top
);
652 state.invalidated
<- state.invalidated
+ 1;
656 let c = c *. state.colorscale
in
661 let maxy = calcheight () in
663 match state.birdseye
with
665 begin match state.layout with
667 let rely = yratio state.y in
668 truncate
(float maxy *. rely)
670 | l
:: _
-> getpagey l
.pageno
673 | Some
(conf, leftx
, pageno
, hooverpageno
) ->
674 let rely = yratio state.y in
675 truncate
(float maxy *. rely)
682 GlMat.mode `projection
;
683 GlMat.load_identity
();
684 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
685 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
686 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
690 GlMat.mode `projection
;
691 GlMat.load_identity
();
692 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
693 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
694 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
699 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
703 GlMat.mode `modelview
;
704 GlMat.load_identity
();
705 GlClear.color
(scalecolor 1.0);
706 GlClear.clear
[`color
];
709 wcmd "geometry" [`i
w; `i h
];
713 GlDraw.color
(0.0, 0.0, 0.0);
715 (0.0, float (conf.winh
- 18))
716 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
718 let font = Glut.BITMAP_8_BY_13
in
719 GlDraw.color
(1.0, 1.0, 1.0);
720 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
721 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
722 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
726 let len = String.length
state.text
in
727 match state.textentry
with
729 if len > 0 then showtext ' '
state.text
731 | Some
(c, text
, _
, _
, _
) ->
735 text ^
" [" ^
state.text ^
"]"
745 state.text
<- Printf.sprintf
"%c%s" c s;
746 Glut.postRedisplay
();
760 state.rects
<- state.rects1
;
761 Glut.postRedisplay
()
764 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
765 state.pagecount
<- n;
766 state.invalidated
<- state.invalidated
- 1;
767 if state.invalidated
= 0
771 let s = Scanf.sscanf
cmd "t %n"
772 (fun n -> String.sub
cmd n (String.length
cmd - n))
774 Glut.setWindowTitle
s
777 let s = Scanf.sscanf
cmd "T %n"
778 (fun n -> String.sub
cmd n (String.length
cmd - n))
780 if state.textentry
= None
787 Glut.postRedisplay
();
793 let s = Scanf.sscanf
cmd "V %n"
794 (fun n -> String.sub
cmd n (String.length
cmd - n))
800 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
801 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
802 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
803 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
805 let y = (getpagey pageno) + truncate y0
in
808 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
811 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
812 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
813 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
814 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
817 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
820 let n, w, h
, r
, l
, s, p
=
821 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
822 (fun n w h r l
s p
->
823 (n-1, w, h
, r
, l
!= 0, s, p
))
826 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
827 state.memused
<- state.memused
+ s;
830 match state.throttle
with
831 | None
-> state.layout
832 | Some
layout -> layout
836 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
839 let evictedopaque = cbpeek state.pagecache
in
840 match findpageforopaque evictedopaque with
841 | None
-> failwith
"bug in gc"
842 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
843 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
845 wcmd "free" [`
s evictedopaque];
846 state.memused
<- state.memused
- evictedsize
;
847 Hashtbl.remove
state.pagemap k
;
848 cbdecr state.pagecache
;
855 cbput state.pagecache p
;
856 state.rendering
<- false;
858 begin match state.throttle
with
860 if pagevisible state.layout n
863 let allvisible = loadlayout state.layout in
864 if allvisible then preload ();
871 let y = getpagey l
.pageno + l
.pagey in
876 let (n, w, h
, x
) as pdim
=
877 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
879 state.pdims
<- pdim
:: state.pdims
882 let (l
, n, t
, h
, pos
) =
883 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
885 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
887 let l = String.length
s in
888 let b = Buffer.create
(String.length
s) in
895 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
898 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
907 let outline = (s, l, n, float t
/. float h
) in
909 match state.outlines with
910 | Olist
outlines -> Olist
(outline :: outlines)
911 | Oarray _
-> Olist
[outline]
912 | Onarrow _
-> Olist
[outline]
914 state.outlines <- outlines
917 log "unknown cmd `%S'" cmd
920 let now = Unix.gettimeofday
;;
924 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
929 let y = state.y + conf.scrollincr
in
930 let y = if y >= state.maxy then 0 else y in
936 let cmd = readcmd state.csock
in
945 let action = function
946 | HCprev
-> cbget cb ~
-1
947 | HCnext
-> cbget cb
1
948 | HCfirst
-> cbget cb ~
-(cb
.rc)
949 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
950 and cancel
() = cb
.rc <- rc
954 let search pattern forward
=
955 if String.length pattern
> 0
958 match state.layout with
961 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
964 let b = makecmd "search"
965 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
967 Buffer.add_char
b '
,'
;
968 Buffer.add_string
b pattern
;
969 Buffer.add_char
b '
\000'
;
972 writecmd state.csock
cmd;
975 let intentry text key
=
976 let c = Char.unsafe_chr key
in
979 let s = "x" in s.[0] <- c;
980 let text = text ^
s in
984 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
989 let b = Buffer.create
(String.length
s + 1) in
990 Buffer.add_string
b s;
995 let textentry text key
=
996 let c = Char.unsafe_chr key
in
998 | _
when key
>= 32 && key
< 127 ->
999 let text = addchar text c in
1003 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1007 let reinit angle proportional
=
1008 conf.angle
<- angle
;
1009 conf.proportional
<- proportional
;
1011 wcmd "reinit" [`i angle
; `
b proportional
];
1014 let optentry text key
=
1015 let btos b = if b then "on" else "off" in
1016 let c = Char.unsafe_chr key
in
1020 try conf.scrollincr
<- int_of_string
s with exc
->
1021 state.text <- Printf.sprintf
"bad integer `%s': %s"
1022 s (Printexc.to_string exc
)
1024 TEswitch
('#'
, "", None
, intentry, ondone)
1029 Some
(int_of_string
s)
1031 state.text <- Printf.sprintf
"bad integer `%s': %s"
1032 s (Printexc.to_string exc
);
1035 | Some angle
-> reinit angle
conf.proportional
1038 TEswitch
('^'
, "", None
, intentry, ondone)
1041 conf.icase
<- not
conf.icase
;
1042 TEdone
("case insensitive search " ^
(btos conf.icase
))
1045 conf.preload <- not
conf.preload;
1047 TEdone
("preload " ^
(btos conf.preload))
1050 conf.verbose
<- not
conf.verbose
;
1051 TEdone
("verbose " ^
(btos conf.verbose
))
1054 conf.maxhfit
<- not
conf.maxhfit
;
1055 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1056 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1059 conf.crophack
<- not
conf.crophack
;
1060 TEdone
("crophack " ^
btos conf.crophack
)
1063 conf.showall
<- not
conf.showall
;
1064 TEdone
("showall " ^
btos conf.showall
)
1067 conf.underinfo
<- not
conf.underinfo
;
1068 TEdone
("underinfo " ^
btos conf.underinfo
)
1071 conf.savebmarks
<- not
conf.savebmarks
;
1072 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1078 match state.layout with
1083 conf.interpagespace <- int_of_string
s;
1084 state.maxy <- calcheight ();
1085 let y = getpagey pageno in
1088 state.text <- Printf.sprintf
"bad integer `%s': %s"
1089 s (Printexc.to_string exc
)
1091 TEswitch
('
%'
, "", None
, intentry, ondone)
1094 reinit conf.angle
(not
conf.proportional
);
1095 TEdone
("proprortional display " ^
btos conf.proportional
)
1098 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1102 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1104 let enterselector allowdel
outlines errmsg msg
=
1105 if Array.length
outlines = 0
1107 showtext ' ' errmsg
;
1111 Glut.setCursor
Glut.CURSOR_INHERIT
;
1113 match state.layout with
1115 | {pageno=pageno} :: rest
-> pageno
1119 if n = Array.length
outlines
1122 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1123 if outlinepageno
>= pageno then n else loop (n+1)
1128 Some
(allowdel
, active,
1129 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1130 Glut.postRedisplay
();
1134 let enteroutlinemode () =
1136 match state.outlines with
1139 let a = Array.of_list
(List.rev
l) in
1140 state.outlines <- Oarray
a;
1142 | Onarrow
(pat
, a, b) ->
1143 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1145 enterselector false outlines "Document has no outline" msg
;
1148 let enterbookmarkmode () =
1149 let bookmarks = Array.of_list
state.bookmarks in
1150 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1153 let quickbookmark ?title
() =
1154 match state.layout with
1160 let sec = Unix.gettimeofday
() in
1161 let tm = Unix.localtime
sec in
1162 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1166 (tm.Unix.tm_year
+ 1900)
1169 | Some
title -> title
1172 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1176 state.fullscreen
<- None
;
1177 Glut.reshapeWindow
w h
;
1180 let writeopen path password
=
1181 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1184 let opendoc path password
=
1187 state.password
<- password
;
1188 state.gen
<- state.gen
+ 1;
1190 writeopen path password
;
1191 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1192 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1196 let zoom = float conf.thumbw
/. float conf.winw
in
1197 let birdseyepageno =
1198 match state.layout with
1200 | l :: _
-> l.pageno
1203 Some
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1);
1205 conf.presentation <- false;
1206 conf.interpagespace <- 10;
1207 conf.hlinks
<- false;
1209 state.mstate
<- Mnone
;
1210 conf.showall
<- false;
1211 Glut.setCursor
Glut.CURSOR_INHERIT
;
1214 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1221 let birdseyeoff (c, leftx
, pageno, _
) =
1222 state.birdseye
<- None
;
1223 conf.zoom <- c.zoom;
1224 conf.presentation <- c.presentation;
1225 conf.interpagespace <- c.interpagespace;
1226 conf.showall
<- c.showall
;
1227 conf.hlinks
<- c.hlinks
;
1229 state.maxy <- calcheight ();
1230 state.y <- getpagey pageno;
1233 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1238 let togglebirdseye () =
1239 match state.birdseye
with
1240 | None
-> birdseyeon ()
1241 | Some vals
-> birdseyeoff vals
1244 let viewkeyboard ~key ~x ~
y =
1246 state.textentry <- te
;
1249 Glut.postRedisplay
()
1251 match state.textentry with
1253 let c = Char.chr key
in
1259 let y = getnav () in
1260 gotoy_and_clear_text y
1263 begin match state.birdseye
with
1267 reshape conf.winw
conf.winh
;
1276 Glut.postRedisplay
()
1279 let ondone isforw
s =
1280 cbput state.hists
.pat
s;
1281 state.searchpattern
<- s;
1284 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1285 textentry, ondone (c ='
/'
)))
1287 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1288 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1289 conf.zoom <- min
2.2 (conf.zoom +. incr);
1290 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1291 reshape conf.winw
conf.winh
1296 try int_of_string
s with exc
->
1297 state.text <- Printf.sprintf
"bad integer `%s': %s"
1298 s (Printexc.to_string exc
);
1304 state.text <- "page bias is now " ^ string_of_int
n;
1307 enttext (Some
('
+'
, "", None
, intentry, ondone))
1309 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1310 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1311 conf.zoom <- max
0.01 (conf.zoom -. decr);
1312 if conf.zoom <= 1.0 then state.x
<- 0;
1313 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1314 reshape conf.winw
conf.winh
;
1320 enttext (Some
('
-'
, "", None
, optentry, ondone))
1322 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1325 state.text <- "zoom is 100%";
1326 reshape conf.winw
conf.winh
1328 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1329 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1334 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1335 reshape conf.winw
conf.winh
;
1338 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1340 reshape conf.winw
conf.winh
;
1345 try int_of_string
s with exc
->
1346 state.text <- Printf.sprintf
"bad integer `%s': %s"
1347 s (Printexc.to_string exc
);
1353 cbput state.hists
.pag
(string_of_int
n);
1354 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1357 let pageentry text key
=
1358 match Char.unsafe_chr key
with
1359 | 'g'
-> TEdone
text
1360 | _
-> intentry text key
1362 let text = "x" in text.[0] <- c;
1363 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1367 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1368 reshape conf.winw
conf.winh
;
1371 conf.hlinks
<- not
conf.hlinks
;
1372 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1373 Glut.postRedisplay
()
1376 conf.autoscroll
<- not
conf.autoscroll
1379 conf.presentation <- not
conf.presentation;
1380 showtext ' '
("presentation mode " ^
1381 if conf.presentation then "on" else "off");
1385 begin match state.fullscreen
with
1387 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1390 state.fullscreen
<- None
;
1395 gotoy_and_clear_text 0
1398 search state.searchpattern
true
1401 search state.searchpattern
false
1404 begin match state.layout with
1407 gotoy_and_clear_text (getpagey l.pageno)
1411 begin match List.rev
state.layout with
1414 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1415 gotoy_and_clear_text (getpagey pageno)
1419 begin match state.layout with
1422 let pageno = max
0 (l.pageno-1) in
1423 gotoy_and_clear_text (getpagey pageno)
1428 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1430 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1432 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1436 else (100. *. (float state.y /. float maxy)) in
1439 Printf.sprintf
"Page %d of %d %.2f%%"
1440 (fn+1) state.pagecount
percent
1443 "Pages %d-%d of %d %.2f%%"
1444 (fn+1) (ln
+1) state.pagecount
percent
1449 begin match state.layout with
1452 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1453 Glut.postRedisplay
();
1457 enterbookmarkmode ()
1461 match state.layout with
1464 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1468 enttext (Some
('~'
, "", None
, textentry, ondone))
1472 showtext ' '
"Quick bookmark added";
1475 begin match state.layout with
1477 let rect = getpdimrect
l.pagedimno
in
1481 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1482 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1484 (truncate
(rect.(1) -. rect.(0)),
1485 truncate
(rect.(3) -. rect.(0)))
1489 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1491 Glut.postRedisplay
();
1497 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1502 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1503 Glut.postRedisplay
()
1505 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1506 | 'j'
-> gotoy (clamp conf.scrollincr
)
1508 | '
r'
-> opendoc state.path
state.password
1511 vlog "huh? %d %c" key
(Char.chr key
);
1514 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1515 let len = String.length
text in
1518 state.textentry <- None
;
1519 Glut.postRedisplay
();
1522 let s = String.sub
text 0 (len - 1) in
1523 enttext (Some
(c, s, opthist
, onkey
, ondone))
1526 | Some
(c, text, onhist, onkey
, ondone) ->
1527 begin match Char.unsafe_chr key
with
1530 state.textentry <- None
;
1531 Glut.postRedisplay
()
1534 begin match onhist with
1536 | Some
(_
, onhistcancel
) -> onhistcancel
()
1538 state.textentry <- None
;
1539 Glut.postRedisplay
()
1542 begin match onkey
text key
with
1544 state.textentry <- None
;
1546 Glut.postRedisplay
()
1549 enttext (Some
(c, text, onhist, onkey
, ondone));
1552 state.textentry <- None
;
1553 Glut.postRedisplay
()
1556 state.textentry <- Some te
;
1557 Glut.postRedisplay
()
1562 let narrow outlines pattern
=
1563 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1567 let rec fold accu n =
1571 let (s, _
, _
, _
) as o
= outlines.(n) in
1573 if (try ignore
(Str.search_forward re
s 0); true
1574 with Not_found
-> false)
1580 let matched = fold [] (Array.length
outlines - 1) in
1581 if matched = [] then None
else Some
(Array.of_list
matched)
1584 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1585 let search active pattern
incr =
1588 if n = Array.length
outlines || n = -1
1591 let (s, _
, _
, _
) = outlines.(n) in
1593 (try ignore
(Str.search_forward re
s 0); true
1594 with Not_found
-> false)
1596 else loop (n + incr)
1601 let re = Str.regexp_case_fold pattern
in
1607 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1610 if String.length qsearch
= 0
1613 state.outline <- None
;
1614 Glut.postRedisplay
();
1618 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1619 Glut.postRedisplay
();
1623 let incr = if key
= 18 then -1 else 1 in
1625 match search (active + incr) qsearch
incr with
1627 state.text <- qsearch ^
" [not found]";
1630 state.text <- qsearch
;
1631 active, firstof active
1633 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1634 Glut.postRedisplay
();
1637 let len = String.length qsearch
in
1644 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1647 let qsearch = String.sub
qsearch 0 (len - 1) in
1649 match search active qsearch ~
-1 with
1651 state.text <- qsearch ^
" [not found]";
1654 state.text <- qsearch;
1655 active, firstof active
1657 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1659 Glut.postRedisplay
()
1662 if active < Array.length
outlines
1664 let (_
, _
, n, t
) = outlines.(active) in
1668 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1669 state.outline <- None
;
1670 Glut.postRedisplay
();
1672 | _
when key
>= 32 && key
< 127 ->
1673 let pattern = addchar qsearch (Char.chr key
) in
1675 match search active pattern 1 with
1677 state.text <- pattern ^
" [not found]";
1680 state.text <- pattern;
1681 active, firstof active
1683 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1684 Glut.postRedisplay
()
1686 | 14 when not allowdel
-> (* ctrl-n *)
1687 if String.length
qsearch > 0
1689 let optoutlines = narrow outlines qsearch in
1690 begin match optoutlines with
1691 | None
-> state.text <- "can't narrow"
1693 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1694 match state.outlines with
1697 state.outlines <- Onarrow
(qsearch, outlines, a)
1698 | Onarrow
(pat
, a, b) ->
1699 state.outlines <- Onarrow
(qsearch, outlines, b)
1702 Glut.postRedisplay
()
1704 | 21 when not allowdel
-> (* ctrl-u *)
1706 match state.outlines with
1709 let a = Array.of_list
(List.rev
l) in
1710 state.outlines <- Oarray
a;
1712 | Onarrow
(pat
, a, b) ->
1713 state.outlines <- Oarray
b;
1717 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1718 Glut.postRedisplay
()
1722 Some
(allowdel
, active, firstof active, outlines, qsearch);
1723 Glut.postRedisplay
()
1725 | 127 when allowdel
->
1726 let len = Array.length
outlines - 1 in
1729 state.outline <- None
;
1730 state.bookmarks <- [];
1733 let bookmarks = Array.init
len
1735 let i = if i >= active then i + 1 else i in
1746 Glut.postRedisplay
()
1748 | _
-> log "unknown key %d" key
1751 let keyboard ~key ~x ~
y =
1756 match state.outline with
1757 | None
-> viewkeyboard ~key ~x ~
y
1758 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1761 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1764 let pageno = max
0 (pageno - 1) in
1766 let rec loop = function
1769 if l.pageno = pageno
1776 then gotopage1 pageno 0
1777 else Glut.postRedisplay
();
1778 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
)
1781 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1782 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1783 if not
(pagevisible state.layout pageno)
1785 begin match List.rev
state.layout with
1786 | [] -> gotopage pageno 0.0
1788 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1790 else Glut.postRedisplay
();
1792 | Glut.KEY_PAGE_UP
->
1793 begin match state.layout with
1795 if l.pageno = pageno
1797 match layout (state.y - conf.winh
) conf.winh
with
1798 | [] -> gotoy (clamp (-conf.winh
))
1800 let pageno = max
0 (l.pageno - 1) in
1801 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1805 let pageno = max
0 (l.pageno - 1) in
1806 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1809 | [] -> gotoy (clamp (-conf.winh
))
1811 | Glut.KEY_PAGE_DOWN
->
1812 begin match List.rev
state.layout with
1814 let pageno = min
l.pageno (state.pagecount
- 1) in
1815 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1816 gotoy (clamp (l.pagedispy
+ l.pageh
))
1817 | [] -> gotoy (clamp conf.winh
)
1821 state.birdseye
<- Some
(conf, leftx
, 0, hooverpageno
);
1824 let pageno = state.pagecount
- 1 in
1825 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1826 if not
(pagevisible state.layout pageno)
1829 match List.rev
state.pdims
with
1831 | (_
, _
, h, _
) :: _
-> h
1833 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1834 else Glut.postRedisplay
();
1838 let special ~key ~x ~
y =
1839 match state.outline, state.birdseye
with
1840 | None
, _
when key
= Glut.KEY_F9
->
1842 reshape conf.winw
conf.winh
;
1844 | None
, (Some vals
) ->
1845 birdseyespecial key x
y vals
1848 begin match state.textentry with
1852 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1853 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1854 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1855 | Glut.KEY_PAGE_UP
->
1856 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1858 match state.layout with
1860 | l :: _
-> state.y - l.pagey
1863 | Glut.KEY_PAGE_DOWN
->
1864 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1866 match List.rev
state.layout with
1868 | l :: _
-> getpagey l.pageno
1871 | Glut.KEY_HOME
-> addnav (); 0
1874 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1876 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1877 state.x
<- state.x
- 10;
1879 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1880 state.x
<- state.x
+ 10;
1885 gotoy_and_clear_text y
1887 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1890 | Glut.KEY_UP
-> action HCprev
1891 | Glut.KEY_DOWN
-> action HCnext
1892 | Glut.KEY_HOME
-> action HCfirst
1893 | Glut.KEY_END
-> action HClast
1896 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1897 Glut.postRedisplay
()
1902 | Some
(allowdel
, active, first
, outlines, qsearch), _
->
1903 let maxrows = maxoutlinerows () in
1904 let calcfirst first
active =
1907 let rows = active - first
in
1908 if rows > maxrows then active - maxrows else first
1912 let active = active + incr in
1913 let active = max
0 (min
active (Array.length
outlines - 1)) in
1914 let first = calcfirst first active in
1915 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1916 Glut.postRedisplay
()
1918 let updownlevel incr =
1919 let len = Array.length
outlines in
1920 let (_
, curlevel
, _
, _
) = outlines.(active) in
1922 if i = len then i-1 else if i = -1 then 0 else
1923 let (_
, l, _
, _
) = outlines.(i) in
1924 if l != curlevel
then i else flow (i+incr)
1926 let active = flow active in
1927 let first = calcfirst first active in
1928 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1929 Glut.postRedisplay
()
1932 | Glut.KEY_UP
-> navigate ~
-1
1933 | Glut.KEY_DOWN
-> navigate 1
1934 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1935 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1937 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1938 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1941 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1942 Glut.postRedisplay
()
1945 let active = Array.length
outlines - 1 in
1946 let first = max
0 (active - maxrows) in
1947 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1948 Glut.postRedisplay
()
1953 let drawplaceholder l =
1954 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1956 (float l.pagex
, float l.pagedispy
)
1957 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1959 let x = float (if margin < 0 then -margin else l.pagex
)
1960 and y = float (l.pagedispy
+ 13) in
1961 let font = Glut.BITMAP_8_BY_13
in
1962 GlDraw.color
(0.0, 0.0, 0.0);
1963 GlPix.raster_pos ~
x ~
y ();
1964 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1965 ("Loading " ^ string_of_int
(l.pageno + 1));
1968 let now () = Unix.gettimeofday
();;
1971 if state.textentry = None
1973 match state.birdseye
with
1974 | None
-> GlDraw.color
(scalecolor 1.0);
1975 | Some
(_
, _
, pageno, hooverpageno
) ->
1977 if l.pageno = pageno
1980 if l.pageno = hooverpageno
1985 GlDraw.color (scalecolor color);
1987 else GlDraw.color (scalecolor 0.4);
1988 begin match getopaque l.pageno with
1989 | Some
(opaque, _
) when validopaque opaque ->
1991 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1995 vlog "draw %d %f sec" l.pageno d;
2003 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2004 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2005 let sh = float conf.winh
/. sh in
2006 let sh = max
sh (float conf.scrollh
) in
2009 if state.y = state.maxy
2011 else float y /. float maxy
2013 let position = (float conf.winh
-. sh) *. percent in
2016 if position +. sh > float conf.winh
2017 then float conf.winh
-. sh
2023 let scrollindicator () =
2024 GlDraw.color (0.64 , 0.64, 0.64);
2026 (float (conf.winw
- conf.scrollw
), 0.)
2027 (float conf.winw
, float conf.winh
)
2029 GlDraw.color (0.0, 0.0, 0.0);
2031 let position, sh = scrollph state.y in
2033 (float (conf.winw
- conf.scrollw
), position)
2034 (float conf.winw
, position +. sh)
2038 let showsel margin =
2039 match state.mstate
with
2040 | Mnone
| Mscroll _
| Mpan _
->
2043 | Msel
((x0
, y0
), (x1
, y1
)) ->
2044 let rec loop = function
2046 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2047 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2049 match getopaque l.pageno with
2050 | Some
(opaque, _
) when validopaque opaque ->
2051 let oy = -l.pagey + l.pagedispy
in
2053 (x0
- margin - state.x, y0
,
2054 x1
- margin - state.x, y1
) oy;
2064 let panx = float state.x in
2066 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2067 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2069 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2071 if l.pageno = pageno
2073 let d = float (l.pagedispy
- l.pagey) in
2074 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2075 GlDraw.begins `quads
;
2077 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2078 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2079 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2080 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2090 let showoutline = function
2092 | Some
(allowdel
, active, first, outlines, qsearch) ->
2094 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2095 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2096 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2099 GlDraw.color (1., 1., 1.);
2100 let font = Glut.BITMAP_9_BY_15
in
2101 let draw_string x y s =
2102 GlPix.raster_pos ~
x ~
y ();
2103 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2106 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2109 let (s, l, _
, _
) = outlines.(row
) in
2110 let y = (row
- first) * 16 in
2115 GlDraw.polygon_mode `both `line
;
2116 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2117 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2118 GlDraw.rect (0., float (y + 1))
2119 (float (conf.winw
- 1), float (y + 18));
2120 GlDraw.polygon_mode `both `fill
;
2122 GlDraw.color (1., 1., 1.);
2124 draw_string (float x) (float (y + 16)) s;
2132 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2133 GlDraw.viewport
margin 0 state.w conf.winh
;
2135 GlClear.color (scalecolor 0.5);
2136 GlClear.clear
[`
color];
2139 let x = float state.x in
2140 GlMat.translate ~
x ();
2144 Gl.enable `scissor_test
;
2145 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2147 List.iter
drawpage state.layout;
2150 Gl.disable `scissor_test
2154 let x = -.float state.x in
2155 GlMat.translate ~
x ();
2159 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2162 showoutline state.outline;
2164 Glut.swapBuffers
();
2168 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2169 let x = x - margin - state.x in
2170 let rec f = function
2172 begin match getopaque l.pageno with
2173 | Some
(opaque, _
) when validopaque opaque ->
2174 let y = y - l.pagedispy
in
2177 let y = l.pagey + y in
2178 let x = x - l.pagex
in
2179 match whatsunder
opaque x y with
2192 let mouse ~button ~bstate ~
x ~
y =
2194 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2202 let incr = incr * 2 in
2203 let y = clamp incr in
2204 gotoy_and_clear_text y
2206 | Glut.LEFT_BUTTON
when state.outline = None
2207 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2208 if bstate
= Glut.DOWN
2210 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2211 state.mstate
<- Mpan
(x, y)
2214 state.mstate
<- Mnone
2217 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2218 if bstate
= Glut.DOWN
2220 let position, sh = scrollph state.y in
2221 if y > truncate
position && y < truncate
(position +. sh)
2223 state.mstate
<- Mscroll
2225 let percent = float y /. float conf.winh
in
2226 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2228 state.mstate
<- Mscroll
2230 state.mstate
<- Mnone
2232 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2233 begin match state.birdseye
with
2234 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2235 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2236 let rec loop = function
2239 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2240 && x > margin && x < margin + l.pagew
2242 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2243 reshape conf.winw
conf.winh
;
2248 | None
-> () (* impossible *)
2251 | Glut.LEFT_BUTTON
when state.outline = None
->
2252 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2253 begin match dest with
2254 | Ulinkgoto
(pageno, top
) ->
2257 gotopage1 pageno top
2262 | Unone
when bstate
= Glut.DOWN
->
2263 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2264 state.mstate
<- Mpan
(x, y);
2266 | Unone
| Utext _
->
2267 if bstate
= Glut.DOWN
2269 if conf.angle
mod 360 = 0
2271 state.mstate
<- Msel
((x, y), (x, y));
2272 Glut.postRedisplay
()
2276 match state.mstate
with
2280 state.mstate
<- Mnone
2283 Glut.setCursor
Glut.CURSOR_INHERIT
;
2284 state.mstate
<- Mnone
2286 | Msel
((x0
, y0
), (x1
, y1
)) ->
2288 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2289 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2291 match getopaque l.pageno with
2292 | Some
(opaque, _
) when validopaque opaque ->
2296 List.iter
f state.layout;
2297 copysel
""; (* ugly *)
2298 Glut.setCursor
Glut.CURSOR_INHERIT
;
2299 state.mstate
<- Mnone
;
2306 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2309 if state.outline = None
2311 match state.mstate
with
2317 state.mstate
<- Mpan
(x, y);
2318 if conf.zoom > 1.0 then state.x <- state.x + dx;
2320 gotoy_and_clear_text y
2323 state.mstate
<- Msel
(a, (x, y));
2324 Glut.postRedisplay
()
2327 let y = min
conf.winh
(max
0 y) in
2328 let percent = float y /. float conf.winh
in
2329 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2330 gotoy_and_clear_text y
2334 match state.birdseye
with
2335 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2336 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2337 let rec loop = function
2339 if hooverpageno
!= -1
2341 state.birdseye
<- Some
(conf, leftx
, pageno, -1);
2342 Glut.postRedisplay
();
2345 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2346 && x > margin && x < margin + l.pagew
2348 state.birdseye
<- Some
(conf, leftx
, pageno, l.pageno);
2349 Glut.postRedisplay
();
2356 if state.outline = None
2358 match state.mstate
with
2360 begin match getunder x y with
2361 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2363 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2364 Glut.setCursor
Glut.CURSOR_INFO
2365 | Ulinkgoto
(page
, y) ->
2367 then showtext 'p'
("age: " ^ string_of_int page
);
2368 Glut.setCursor
Glut.CURSOR_INFO
2370 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2371 Glut.setCursor
Glut.CURSOR_TEXT
2374 | Mpan _
| Msel _
| Mscroll
->
2385 match Sys.os_type
with
2386 | "Win32" -> Sys.getenv
"HOMEPATH"
2387 | _
-> Sys.getenv
"HOME"
2390 ("Can not determine home directory location: " ^
2391 Printexc.to_string exn
);
2395 let config_of c attrs
=
2399 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2400 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2401 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2402 | "preload" -> { c with preload = bool_of_string v
}
2403 | "page-bias" -> { c with pagebias
= int_of_string v
}
2404 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2405 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2406 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2407 | "throttle" -> { c with showall
= bool_of_string v
}
2408 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2409 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2410 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2412 let zoom = float_of_string v
/. 100. in
2413 let zoom = max
0.01 (min
2.2 zoom) in
2414 { c with zoom = zoom }
2415 | "presentation" -> { c with presentation = bool_of_string v
}
2416 | "rotation-angle" -> { c with angle
= int_of_string v
}
2417 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2418 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2419 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2420 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2421 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2422 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2423 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2424 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2427 prerr_endline
("Error processing attribute (`" ^
2428 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2431 let rec fold c = function
2434 let c = apply c k v
in
2440 let bookmark_of attrs
=
2441 let rec fold title page
rely = function
2442 | ("title", v
) :: rest
-> fold v page
rely rest
2443 | ("page", v
) :: rest
-> fold title v
rely rest
2444 | ("rely", v
) :: rest
-> fold title page v rest
2445 | _
:: rest
-> fold title page
rely rest
2446 | [] -> title, page
, rely
2448 fold "invalid" "0" "0" attrs
2451 let setconf dst src
=
2452 dst
.scrollw
<- src
.scrollw
;
2453 dst
.scrollh
<- src
.scrollh
;
2454 dst
.icase
<- src
.icase
;
2455 dst
.preload <- src
.preload;
2456 dst
.pagebias
<- src
.pagebias
;
2457 dst
.verbose
<- src
.verbose
;
2458 dst
.scrollincr
<- src
.scrollincr
;
2459 dst
.maxhfit
<- src
.maxhfit
;
2460 dst
.crophack
<- src
.crophack
;
2461 dst
.autoscroll
<- src
.autoscroll
;
2462 dst
.showall
<- src
.showall
;
2463 dst
.hlinks
<- src
.hlinks
;
2464 dst
.underinfo
<- src
.underinfo
;
2465 dst
.interpagespace <- src
.interpagespace;
2466 dst
.zoom <- src
.zoom;
2467 dst
.presentation <- src
.presentation;
2468 dst
.angle
<- src
.angle
;
2469 dst
.winw
<- src
.winw
;
2470 dst
.winh
<- src
.winh
;
2471 dst
.savebmarks
<- src
.savebmarks
;
2472 dst
.memlimit
<- src
.memlimit
;
2473 dst
.proportional
<- src
.proportional
;
2474 dst
.texcount
<- src
.texcount
;
2475 dst
.sliceheight
<- src
.sliceheight
;
2476 dst
.thumbw
<- src
.thumbw
;
2480 let l = String.length
s in
2481 let b = Buffer.create
l in
2487 let h = Hashtbl.create
10 in
2488 let dc = { defconf with angle
= defconf.angle
} in
2489 let rec toplevel v t spos epos
=
2491 | Vdata
| Vcdata
| Vend
-> v
2492 | Vopen
("llppconfig", attrs
, closed
) ->
2495 else { v
with f = llppconfig
}
2497 error
"unexpected subelement at top level" s spos
2498 | Vclose tag
-> error
"unexpected close at top level" s spos
2500 and llppconfig v t spos epos
=
2502 | Vdata
| Vcdata
| Vend
-> v
2503 | Vopen
("defaults", attrs
, closed
) ->
2504 let c = config_of dc attrs
in
2508 else { v
with f = skip
"defaults" (fun () -> v
) }
2510 | Vopen
("doc", attrs
, closed
) ->
2513 List.assoc
"path" attrs
2514 with Not_found
-> error
"doc is missing path attribute" s spos
2516 let path = unent pathent in
2517 let c = config_of dc attrs
in
2520 float_of_string
(List.assoc
"rely" attrs
)
2524 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2529 int_of_string
(List.assoc
"pan" attrs
)
2533 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2537 then (Hashtbl.add
h path (c, [], x, y); v
)
2538 else { v
with f = doc
path x y c [] }
2540 | Vopen
(tag
, _
, closed
) ->
2541 error
"unexpected subelement in llppconfig" s spos
2543 | Vclose
"llppconfig" -> { v
with f = toplevel }
2544 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2546 and doc
path x y c bookmarks v t spos epos
=
2548 | Vdata
| Vcdata
-> v
2549 | Vend
-> error
"unexpected end of input in doc" s spos
2550 | Vopen
("bookmarks", attrs
, closed
) ->
2551 { v
with f = pbookmarks
path x y c bookmarks }
2553 | Vopen
(tag
, _
, _
) ->
2554 error
"unexpected subelement in doc" s spos
2557 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2558 { v
with f = llppconfig
}
2560 | Vclose tag
-> error
"unexpected close in doc" s spos
2562 and pbookmarks
path x y c bookmarks v t spos epos
=
2564 | Vdata
| Vcdata
-> v
2565 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2566 | Vopen
("item", attrs
, closed
) ->
2567 let titleent, spage
, srely
= bookmark_of attrs
in
2572 dolog "Failed to convert page %S to integer: %s"
2573 spage
(Printexc.to_string exn
);
2578 float_of_string srely
2580 dolog "Failed to convert rely %S to real: %s"
2581 srely
(Printexc.to_string exn
);
2584 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2586 then { v
with f = pbookmarks
path x y c bookmarks }
2589 { v
with f = skip
"item" f }
2592 error
"unexpected subelement in bookmarks" s spos
2594 | Vclose
"bookmarks" ->
2595 { v
with f = doc
path x y c bookmarks }
2597 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2599 and skip tag
f v t spos epos
=
2601 | Vdata
| Vcdata
-> v
2603 error
("unexpected end of input in skipped " ^ tag
) s spos
2604 | Vopen
(tag'
, _
, closed
) ->
2608 let f'
() = { v
with f = skip tag
f } in
2609 { v
with f = skip tag'
f'
}
2613 else error
("unexpected close in skipped " ^ tag
) s spos
2616 parse
{ f = toplevel; accu = () } s;
2622 let len = in_channel_length ic
in
2623 let s = String.create
len in
2624 really_input ic
s 0 len;
2627 | Parse_error
(msg
, s, pos
) ->
2628 let subs = subs s pos
in
2629 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2630 failwith
("parse error: " ^
s)
2633 failwith
("config load error: " ^
Printexc.to_string exn
)
2639 let dir = Filename.concat
home ".config" in
2640 if Sys.is_directory
dir then dir else home
2643 Filename.concat
dir "llpp.conf"
2647 if Sys.file_exists
path
2650 (try Some
(open_in_bin
path)
2653 ("Error opening configuation file `" ^
path ^
"': " ^
2654 Printexc.to_string exn
);
2663 ("Error loading configuation from `" ^
path ^
"': " ^
2664 Printexc.to_string exn
);
2670 f (Hashtbl.create
0, defconf)
2675 let pc, pb
, px
, py =
2677 Hashtbl.find
h state.path
2678 with Not_found
-> dc, [], 0, 0.0
2682 state.bookmarks <- pb
;
2684 cbput state.hists
.nav
py;
2689 let add_attrs bb always
dc c =
2692 then Printf.bprintf bb
"\n %s='%b'" s a
2695 then Printf.bprintf bb
"\n %s='%d'" s a
2698 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2702 then dc.winw
, dc.winh
2704 match state.fullscreen
with
2706 | None
-> c.winw
, c.winh
2708 let zoom, presentation, interpagespace, showall
=
2710 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2712 match state.birdseye
with
2713 | Some
(bc
, _
, _
, _
) ->
2714 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2715 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2717 oi
"width" w dc.winw
;
2718 oi
"height" h dc.winh
;
2719 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2720 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2721 ob "case-insensitive-search" c.icase
dc.icase
;
2722 ob "preload" c.preload dc.preload;
2723 oi
"page-bias" c.pagebias
dc.pagebias
;
2724 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2725 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2726 ob "crop-hack" c.crophack
dc.crophack
;
2727 ob "throttle" showall
dc.showall
;
2728 ob "highlight-links" c.hlinks
dc.hlinks
;
2729 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2730 oi
"vertical-margin" interpagespace dc.interpagespace;
2731 oz
"zoom" zoom dc.zoom;
2732 ob "presentation" presentation dc.presentation;
2733 oi
"rotation-angle" c.angle
dc.angle
;
2734 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2735 ob "proportional-display" c.proportional
dc.proportional
;
2736 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2737 oi
"texcount" c.texcount
dc.texcount
;
2738 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2739 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2743 let bb = Buffer.create
32768 in
2745 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2746 add_attrs bb true dc dc;
2747 Buffer.add_string
bb "/>\n";
2749 let adddoc path x y c bookmarks =
2750 if bookmarks == [] && c = dc && y = 0.0
2753 Printf.bprintf
bb "<doc path='%s'"
2754 (enent
path 0 (String.length
path));
2757 then Printf.bprintf
bb " rely='%f'" y;
2760 then Printf.bprintf
bb " pan='%d'" x;
2762 add_attrs bb false dc c;
2764 begin match bookmarks with
2765 | [] -> Buffer.add_string
bb "/>\n"
2767 Buffer.add_string
bb ">\n<bookmarks>\n";
2768 List.iter
(fun (title, _level
, page, rely) ->
2770 "<item title='%s' page='%d' rely='%f'/>\n"
2771 (enent
title 0 (String.length
title))
2775 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2781 match state.birdseye
with
2782 | Some
(_
, x, _
, _
) -> x
2785 adddoc state.path x (yratio state.y) conf
2786 (if conf.savebmarks
then state.bookmarks else []);
2788 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2789 if path <> state.path
2791 adddoc path x y c bookmarks
2793 Buffer.add_string
bb "</llppconfig>";
2796 if Buffer.length
bb > 0
2799 let tmp = path ^
".tmp" in
2800 let oc = open_out_bin
tmp in
2801 Buffer.output_buffer
oc bb;
2803 Sys.rename
tmp path;
2806 ("error while saving configuration: " ^
Printexc.to_string exn
)
2812 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2813 (fun s -> state.path <- s)
2814 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2817 if String.length
state.path = 0
2818 then (prerr_endline
"filename missing"; exit
1)
2820 if Filename.is_relative
state.path
2821 then Filename.concat
(Sys.getcwd
()) state.path
2829 let _ = Glut.init
Sys.argv
in
2830 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2831 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2832 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2835 if Sys.os_type
= "Unix"
2837 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2839 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2840 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2841 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2842 Unix.bind
sock addr;
2844 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2845 Unix.connect
csock addr;
2846 let ssock, _ = Unix.accept
sock in
2849 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2850 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2854 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2858 let () = Glut.displayFunc
display in
2859 let () = Glut.reshapeFunc
reshape in
2860 let () = Glut.keyboardFunc
keyboard in
2861 let () = Glut.specialFunc
special in
2862 let () = Glut.idleFunc
(Some
idle) in
2863 let () = Glut.mouseFunc
mouse in
2864 let () = Glut.motionFunc
motion in
2865 let () = Glut.passiveMotionFunc
pmotion in
2867 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2868 state.csock <- csock;
2869 state.ssock <- ssock;
2870 state.text <- "Opening " ^
path;
2871 writeopen state.path state.password
;
2875 let rec handlelablglutbug () =
2878 with Glut.BadEnum
"key in special_of_int" ->
2879 showtext '
!'
" LablGlut bug: special key not recognized";
2880 handlelablglutbug ()
2882 handlelablglutbug ();