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
158 type outline
= string * int * int * float;;
160 | Oarray
of outline array
161 | Olist
of outline list
162 | Onarrow
of string * outline array
* outline array
165 type rect
= (float * float * float * float * float * float * float * float);;
167 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
170 { mutable csock
: Unix.file_descr
171 ; mutable ssock
: Unix.file_descr
176 ; mutable layout
: layout list
177 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
178 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
179 ; mutable pagecount
: int
180 ; pagecache
: string circbuf
181 ; mutable rendering
: bool
182 ; mutable mstate
: mstate
183 ; mutable searchpattern
: string
184 ; mutable rects
: (pageno
* recttype
* rect
) list
185 ; mutable rects1
: (pageno
* recttype
* rect
) list
186 ; mutable text
: string
187 ; mutable fullscreen
: (width
* height
) option
188 ; mutable birdseye
: (conf
* leftx
* pageno
) option
189 ; mutable textentry
: textentry
option
190 ; mutable outlines
: outlines
191 ; mutable outline
: (bool * int * int * outline array
* string) option
192 ; mutable bookmarks
: outline list
193 ; mutable path
: string
194 ; mutable password
: string
195 ; mutable invalidated
: int
196 ; mutable colorscale
: float
197 ; mutable memused
: int
198 ; mutable birdseyepageno
: pageno
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
238 let conf = { defconf with angle
= defconf.angle
};;
248 ; pagemap
= Hashtbl.create
10
249 ; pagecache
= cbnew 100 ""
261 ; outlines
= Olist
[]
268 { nav
= cbnew 100 0.0
274 ; birdseyepageno
= -1
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)
563 let rely = yratio state.y in
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 ();
570 let y = truncate
(float state.maxy *. rely) in
571 let y = if y < conf.winh
then 0 else y - conf.winh
in
572 let pages = layout y (conf.winh
*3) in
573 List.iter
render pages;
574 conf.presentation <- presentation;
575 conf.interpagespace <- interpagespace;
581 let y = min
state.maxy y in
582 let pages = layout y conf.winh
in
583 let ready = loadlayout pages in
589 state.layout <- pages;
590 state.throttle
<- None
;
591 Glut.postRedisplay
();
594 state.throttle
<- Some
pages;
599 state.layout <- pages;
600 state.throttle
<- None
;
601 Glut.postRedisplay
();
603 if state.birdseye
<> None
605 if not
(pagevisible pages state.birdseyepageno
)
607 match state.layout with
609 | l
:: _
-> state.birdseyepageno
<- l
.pageno
614 let gotoy_and_clear_text y =
616 if not
conf.verbose
then state.text
<- "";
620 cbput state.hists
.nav
(yratio state.y);
624 let y = cbgetc state.hists
.nav ~
-1 in
625 truncate
(y *. float state.maxy)
629 let y, h
= getpageyh n in
631 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
634 let gotopage1 n top
=
635 let y = getpagey n in
637 gotoy_and_clear_text (y + top
);
645 state.invalidated
<- state.invalidated
+ 1;
649 let c = c *. state.colorscale
in
655 if state.birdseyepageno
= -1 && state.birdseye
= None
657 match state.layout with
659 let rely = yratio state.y in
660 state.maxy <- calcheight ();
661 truncate
(float state.maxy *. rely)
664 state.maxy <- calcheight ();
667 let y = getpagey state.birdseyepageno
in
668 state.birdseyepageno
<- -1;
676 GlMat.mode `projection
;
677 GlMat.load_identity
();
678 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
679 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
680 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
684 GlMat.mode `projection
;
685 GlMat.load_identity
();
686 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
687 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
688 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
693 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
697 GlMat.mode `modelview
;
698 GlMat.load_identity
();
699 GlClear.color
(scalecolor 1.0);
700 GlClear.clear
[`color
];
703 wcmd "geometry" [`i
w; `i h
];
707 GlDraw.color
(0.0, 0.0, 0.0);
709 (0.0, float (conf.winh
- 18))
710 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
712 let font = Glut.BITMAP_8_BY_13
in
713 GlDraw.color
(1.0, 1.0, 1.0);
714 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
715 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
716 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
720 let len = String.length
state.text
in
721 match state.textentry
with
723 if len > 0 then showtext ' '
state.text
725 | Some
(c, text
, _
, _
, _
) ->
729 text ^
" [" ^
state.text ^
"]"
739 state.text
<- Printf.sprintf
"%c%s" c s;
740 Glut.postRedisplay
();
754 state.rects
<- state.rects1
;
755 Glut.postRedisplay
()
758 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
759 state.pagecount
<- n;
760 state.invalidated
<- state.invalidated
- 1;
761 if state.invalidated
= 0
765 let s = Scanf.sscanf
cmd "t %n"
766 (fun n -> String.sub
cmd n (String.length
cmd - n))
768 Glut.setWindowTitle
s
771 let s = Scanf.sscanf
cmd "T %n"
772 (fun n -> String.sub
cmd n (String.length
cmd - n))
774 if state.textentry
= None
781 Glut.postRedisplay
();
787 let s = Scanf.sscanf
cmd "V %n"
788 (fun n -> String.sub
cmd n (String.length
cmd - n))
794 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
795 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
796 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
797 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
799 let y = (getpagey pageno) + truncate y0
in
802 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
805 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
806 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
807 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
808 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
811 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
814 let n, w, h
, r
, l
, s, p
=
815 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
816 (fun n w h r l
s p
->
817 (n-1, w, h
, r
, l
!= 0, s, p
))
820 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
821 state.memused
<- state.memused
+ s;
824 match state.throttle
with
825 | None
-> state.layout
826 | Some
layout -> layout
830 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
833 let evictedopaque = cbpeek state.pagecache
in
834 match findpageforopaque evictedopaque with
835 | None
-> failwith
"bug in gc"
836 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
837 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
839 wcmd "free" [`
s evictedopaque];
840 state.memused
<- state.memused
- evictedsize
;
841 Hashtbl.remove
state.pagemap k
;
842 cbdecr state.pagecache
;
849 cbput state.pagecache p
;
850 state.rendering
<- false;
852 begin match state.throttle
with
854 if pagevisible state.layout n
857 let allvisible = loadlayout state.layout in
858 if allvisible then preload ();
865 let y = getpagey l
.pageno + l
.pagey in
870 let (n, w, h
, x
) as pdim
=
871 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
873 state.pdims
<- pdim
:: state.pdims
876 let (l
, n, t
, h
, pos
) =
877 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
879 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
881 let l = String.length
s in
882 let b = Buffer.create
(String.length
s) in
889 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
892 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
901 let outline = (s, l, n, float t
/. float h
) in
903 match state.outlines with
904 | Olist
outlines -> Olist
(outline :: outlines)
905 | Oarray _
-> Olist
[outline]
906 | Onarrow _
-> Olist
[outline]
908 state.outlines <- outlines
911 log "unknown cmd `%S'" cmd
914 let now = Unix.gettimeofday
;;
918 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
923 let y = state.y + conf.scrollincr
in
924 let y = if y >= state.maxy then 0 else y in
930 let cmd = readcmd state.csock
in
939 let action = function
940 | HCprev
-> cbget cb ~
-1
941 | HCnext
-> cbget cb
1
942 | HCfirst
-> cbget cb ~
-(cb
.rc)
943 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
944 and cancel
() = cb
.rc <- rc
948 let search pattern forward
=
949 if String.length pattern
> 0
952 match state.layout with
955 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
958 let b = makecmd "search"
959 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
961 Buffer.add_char
b '
,'
;
962 Buffer.add_string
b pattern
;
963 Buffer.add_char
b '
\000'
;
966 writecmd state.csock
cmd;
969 let intentry text key
=
970 let c = Char.unsafe_chr key
in
973 let s = "x" in s.[0] <- c;
974 let text = text ^
s in
978 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
983 let b = Buffer.create
(String.length
s + 1) in
984 Buffer.add_string
b s;
989 let textentry text key
=
990 let c = Char.unsafe_chr key
in
992 | _
when key
>= 32 && key
< 127 ->
993 let text = addchar text c in
997 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1001 let reinit angle proportional
=
1002 conf.angle
<- angle
;
1003 conf.proportional
<- proportional
;
1005 wcmd "reinit" [`i angle
; `
b proportional
];
1008 let optentry text key
=
1009 let btos b = if b then "on" else "off" in
1010 let c = Char.unsafe_chr key
in
1014 try conf.scrollincr
<- int_of_string
s with exc
->
1015 state.text <- Printf.sprintf
"bad integer `%s': %s"
1016 s (Printexc.to_string exc
)
1018 TEswitch
('#'
, "", None
, intentry, ondone)
1023 Some
(int_of_string
s)
1025 state.text <- Printf.sprintf
"bad integer `%s': %s"
1026 s (Printexc.to_string exc
);
1029 | Some angle
-> reinit angle
conf.proportional
1032 TEswitch
('^'
, "", None
, intentry, ondone)
1035 conf.icase
<- not
conf.icase
;
1036 TEdone
("case insensitive search " ^
(btos conf.icase
))
1039 conf.preload <- not
conf.preload;
1041 TEdone
("preload " ^
(btos conf.preload))
1044 conf.verbose
<- not
conf.verbose
;
1045 TEdone
("verbose " ^
(btos conf.verbose
))
1048 conf.maxhfit
<- not
conf.maxhfit
;
1049 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1050 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1053 conf.crophack
<- not
conf.crophack
;
1054 TEdone
("crophack " ^
btos conf.crophack
)
1057 conf.showall
<- not
conf.showall
;
1058 TEdone
("showall " ^
btos conf.showall
)
1061 conf.underinfo
<- not
conf.underinfo
;
1062 TEdone
("underinfo " ^
btos conf.underinfo
)
1065 conf.savebmarks
<- not
conf.savebmarks
;
1066 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1072 match state.layout with
1077 conf.interpagespace <- int_of_string
s;
1078 state.maxy <- calcheight ();
1079 let y = getpagey pageno in
1082 state.text <- Printf.sprintf
"bad integer `%s': %s"
1083 s (Printexc.to_string exc
)
1085 TEswitch
('
%'
, "", None
, intentry, ondone)
1088 reinit conf.angle
(not
conf.proportional
);
1089 TEdone
("proprortional display " ^
btos conf.proportional
)
1092 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1096 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1098 let enterselector allowdel
outlines errmsg msg
=
1099 if Array.length
outlines = 0
1101 showtext ' ' errmsg
;
1105 Glut.setCursor
Glut.CURSOR_INHERIT
;
1107 match state.layout with
1109 | {pageno=pageno} :: rest
-> pageno
1113 if n = Array.length
outlines
1116 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1117 if outlinepageno
>= pageno then n else loop (n+1)
1122 Some
(allowdel
, active,
1123 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1124 Glut.postRedisplay
();
1128 let enteroutlinemode () =
1130 match state.outlines with
1133 let a = Array.of_list
(List.rev
l) in
1134 state.outlines <- Oarray
a;
1136 | Onarrow
(pat
, a, b) ->
1137 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1139 enterselector false outlines "Document has no outline" msg
;
1142 let enterbookmarkmode () =
1143 let bookmarks = Array.of_list
state.bookmarks in
1144 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1147 let quickbookmark ?title
() =
1148 match state.layout with
1154 let sec = Unix.gettimeofday
() in
1155 let tm = Unix.localtime
sec in
1156 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1160 (tm.Unix.tm_year
+ 1900)
1163 | Some
title -> title
1166 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1170 state.fullscreen
<- None
;
1171 Glut.reshapeWindow
w h
;
1174 let writeopen path password
=
1175 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1178 let opendoc path password
=
1181 state.password
<- password
;
1182 state.gen
<- state.gen
+ 1;
1184 writeopen path password
;
1185 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1186 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1189 let birdseyeoff (c, leftx
, _
) =
1190 state.birdseye
<- None
;
1191 conf.zoom
<- c.zoom
;
1192 conf.presentation <- c.presentation;
1193 conf.interpagespace <- c.interpagespace;
1194 conf.showall
<- c.showall
;
1195 conf.hlinks
<- c.hlinks
;
1199 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1204 let viewkeyboard ~key ~x ~
y =
1206 state.textentry <- te
;
1209 Glut.postRedisplay
()
1211 match state.textentry with
1213 let c = Char.chr key
in
1219 let y = getnav () in
1220 gotoy_and_clear_text y
1223 begin match state.birdseye
with
1227 reshape conf.winw
conf.winh
;
1236 Glut.postRedisplay
()
1239 let ondone isforw
s =
1240 cbput state.hists
.pat
s;
1241 state.searchpattern
<- s;
1244 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1245 textentry, ondone (c ='
/'
)))
1247 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1248 let incr = if conf.zoom
+. 0.01 > 0.1 then 0.1 else 0.01 in
1249 conf.zoom
<- min
2.2 (conf.zoom
+. incr);
1250 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1251 reshape conf.winw
conf.winh
1256 try int_of_string
s with exc
->
1257 state.text <- Printf.sprintf
"bad integer `%s': %s"
1258 s (Printexc.to_string exc
);
1264 state.text <- "page bias is now " ^ string_of_int
n;
1267 enttext (Some
('
+'
, "", None
, intentry, ondone))
1269 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1270 let decr = if conf.zoom
-. 0.1 < 0.1 then 0.01 else 0.1 in
1271 conf.zoom
<- max
0.01 (conf.zoom
-. decr);
1272 if conf.zoom
<= 1.0 then state.x
<- 0;
1273 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1274 reshape conf.winw
conf.winh
;
1280 enttext (Some
('
-'
, "", None
, optentry, ondone))
1282 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1285 state.text <- "zoom is 100%";
1286 reshape conf.winw
conf.winh
1288 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1289 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1294 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1295 reshape conf.winw
conf.winh
;
1298 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1299 begin match state.birdseye
with
1301 let zoom = 76.0 /. float state.w in
1302 let birdseyepageno =
1303 match state.layout with
1305 | l :: _
-> l.pageno
1307 state.birdseyepageno <- birdseyepageno;
1309 Some
({ conf with zoom = conf.zoom }, state.x
, -1);
1311 conf.presentation <- false;
1312 conf.interpagespace <- 10;
1313 conf.hlinks
<- false;
1315 state.mstate
<- Mnone
;
1316 conf.showall
<- false;
1317 Glut.setCursor
Glut.CURSOR_INHERIT
;
1320 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1329 reshape conf.winw
conf.winh
1334 try int_of_string
s with exc
->
1335 state.text <- Printf.sprintf
"bad integer `%s': %s"
1336 s (Printexc.to_string exc
);
1342 cbput state.hists
.pag
(string_of_int
n);
1343 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1346 let pageentry text key
=
1347 match Char.unsafe_chr key
with
1348 | 'g'
-> TEdone
text
1349 | _
-> intentry text key
1351 let text = "x" in text.[0] <- c;
1352 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1356 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1357 reshape conf.winw
conf.winh
;
1360 conf.hlinks
<- not
conf.hlinks
;
1361 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1362 Glut.postRedisplay
()
1365 conf.autoscroll
<- not
conf.autoscroll
1368 conf.presentation <- not
conf.presentation;
1369 showtext ' '
("presentation mode " ^
1370 if conf.presentation then "on" else "off");
1374 begin match state.fullscreen
with
1376 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1379 state.fullscreen
<- None
;
1384 gotoy_and_clear_text 0
1387 search state.searchpattern
true
1390 search state.searchpattern
false
1393 begin match state.layout with
1396 gotoy_and_clear_text (getpagey l.pageno)
1400 begin match List.rev
state.layout with
1403 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1404 gotoy_and_clear_text (getpagey pageno)
1408 begin match state.layout with
1411 let pageno = max
0 (l.pageno-1) in
1412 gotoy_and_clear_text (getpagey pageno)
1417 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1419 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1421 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1425 else (100. *. (float state.y /. float maxy)) in
1428 Printf.sprintf
"Page %d of %d %.2f%%"
1429 (fn+1) state.pagecount
percent
1432 "Pages %d-%d of %d %.2f%%"
1433 (fn+1) (ln
+1) state.pagecount
percent
1438 begin match state.layout with
1441 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1442 Glut.postRedisplay
();
1446 enterbookmarkmode ()
1450 match state.layout with
1453 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1457 enttext (Some
('~'
, "", None
, textentry, ondone))
1461 showtext ' '
"Quick bookmark added";
1464 begin match state.layout with
1466 let rect = getpdimrect
l.pagedimno
in
1470 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1471 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1473 (truncate
(rect.(1) -. rect.(0)),
1474 truncate
(rect.(3) -. rect.(0)))
1478 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1480 Glut.postRedisplay
();
1486 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1491 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1492 Glut.postRedisplay
()
1494 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1495 | 'j'
-> gotoy (clamp conf.scrollincr
)
1497 | '
r'
-> opendoc state.path
state.password
1500 vlog "huh? %d %c" key
(Char.chr key
);
1503 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1504 let len = String.length
text in
1507 state.textentry <- None
;
1508 Glut.postRedisplay
();
1511 let s = String.sub
text 0 (len - 1) in
1512 enttext (Some
(c, s, opthist
, onkey
, ondone))
1515 | Some
(c, text, onhist, onkey
, ondone) ->
1516 begin match Char.unsafe_chr key
with
1519 state.textentry <- None
;
1520 Glut.postRedisplay
()
1523 begin match onhist with
1525 | Some
(_
, onhistcancel
) -> onhistcancel
()
1527 state.textentry <- None
;
1528 Glut.postRedisplay
()
1531 begin match onkey
text key
with
1533 state.textentry <- None
;
1535 Glut.postRedisplay
()
1538 enttext (Some
(c, text, onhist, onkey
, ondone));
1541 state.textentry <- None
;
1542 Glut.postRedisplay
()
1545 state.textentry <- Some te
;
1546 Glut.postRedisplay
()
1551 let narrow outlines pattern
=
1552 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1556 let rec fold accu n =
1560 let (s, _
, _
, _
) as o
= outlines.(n) in
1562 if (try ignore
(Str.search_forward re
s 0); true
1563 with Not_found
-> false)
1569 let matched = fold [] (Array.length
outlines - 1) in
1570 if matched = [] then None
else Some
(Array.of_list
matched)
1573 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1574 let search active pattern
incr =
1577 if n = Array.length
outlines || n = -1
1580 let (s, _
, _
, _
) = outlines.(n) in
1582 (try ignore
(Str.search_forward re
s 0); true
1583 with Not_found
-> false)
1585 else loop (n + incr)
1590 let re = Str.regexp_case_fold pattern
in
1596 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1599 if String.length qsearch
= 0
1602 state.outline <- None
;
1603 Glut.postRedisplay
();
1607 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1608 Glut.postRedisplay
();
1612 let incr = if key
= 18 then -1 else 1 in
1614 match search (active + incr) qsearch
incr with
1616 state.text <- qsearch ^
" [not found]";
1619 state.text <- qsearch
;
1620 active, firstof active
1622 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1623 Glut.postRedisplay
();
1626 let len = String.length qsearch
in
1633 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1636 let qsearch = String.sub
qsearch 0 (len - 1) in
1638 match search active qsearch ~
-1 with
1640 state.text <- qsearch ^
" [not found]";
1643 state.text <- qsearch;
1644 active, firstof active
1646 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1648 Glut.postRedisplay
()
1651 if active < Array.length
outlines
1653 let (_
, _
, n, t
) = outlines.(active) in
1657 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1658 state.outline <- None
;
1659 Glut.postRedisplay
();
1661 | _
when key
>= 32 && key
< 127 ->
1662 let pattern = addchar qsearch (Char.chr key
) in
1664 match search active pattern 1 with
1666 state.text <- pattern ^
" [not found]";
1669 state.text <- pattern;
1670 active, firstof active
1672 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1673 Glut.postRedisplay
()
1675 | 14 when not allowdel
-> (* ctrl-n *)
1676 if String.length
qsearch > 0
1678 let optoutlines = narrow outlines qsearch in
1679 begin match optoutlines with
1680 | None
-> state.text <- "can't narrow"
1682 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1683 match state.outlines with
1686 state.outlines <- Onarrow
(qsearch, outlines, a)
1687 | Onarrow
(pat
, a, b) ->
1688 state.outlines <- Onarrow
(qsearch, outlines, b)
1691 Glut.postRedisplay
()
1693 | 21 when not allowdel
-> (* ctrl-u *)
1695 match state.outlines with
1698 let a = Array.of_list
(List.rev
l) in
1699 state.outlines <- Oarray
a;
1701 | Onarrow
(pat
, a, b) ->
1702 state.outlines <- Oarray
b;
1706 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1707 Glut.postRedisplay
()
1711 Some
(allowdel
, active, firstof active, outlines, qsearch);
1712 Glut.postRedisplay
()
1714 | 127 when allowdel
->
1715 let len = Array.length
outlines - 1 in
1718 state.outline <- None
;
1719 state.bookmarks <- [];
1722 let bookmarks = Array.init
len
1724 let i = if i >= active then i + 1 else i in
1735 Glut.postRedisplay
()
1737 | _
-> log "unknown key %d" key
1740 let keyboard ~key ~x ~
y =
1745 match state.outline with
1746 | None
-> viewkeyboard ~key ~x ~
y
1747 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1750 let special ~key ~x ~
y =
1751 match state.outline with
1752 | None
when state.birdseye
<> None
->
1753 begin match key
with
1755 let pageno = max
0 (state.birdseyepageno - 1) in
1756 state.birdseyepageno <- pageno;
1757 if not
(pagevisible state.layout pageno)
1758 then gotopage pageno 0.0
1759 else Glut.postRedisplay
();
1762 let pageno = min
(state.pagecount
- 1) (state.birdseyepageno + 1) in
1763 state.birdseyepageno <- pageno;
1764 if not
(pagevisible state.layout pageno)
1766 begin match List.rev
state.layout with
1767 | [] -> gotopage pageno 0.0
1769 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1771 else Glut.postRedisplay
();
1773 | Glut.KEY_PAGE_UP
->
1774 begin match state.layout with
1776 if l.pageno = state.birdseyepageno
1778 match layout (state.y - conf.winh
) conf.winh
with
1779 | [] -> gotoy (clamp (-conf.winh
))
1781 state.birdseyepageno <- max
0 (l.pageno - 1);
1782 gotopage state.birdseyepageno 0.0
1785 state.birdseyepageno <- max
0 (l.pageno - 1);
1786 gotopage state.birdseyepageno 0.0
1788 | [] -> gotoy (clamp (-conf.winh
))
1790 | Glut.KEY_PAGE_DOWN
->
1791 begin match List.rev
state.layout with
1793 state.birdseyepageno <- min
(state.pagecount
- 1) (l.pageno + 1);
1794 gotoy (clamp (l.pagedispy
+ l.pageh
))
1795 | [] -> gotoy (clamp conf.winh
)
1799 state.birdseyepageno <- 0;
1802 state.birdseyepageno <- state.pagecount
- 1;
1803 if not
(pagevisible state.layout state.birdseyepageno)
1805 gotopage state.birdseyepageno 0.0
1807 Glut.postRedisplay
()
1813 begin match state.textentry with
1817 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1818 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1819 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1820 | Glut.KEY_PAGE_UP
->
1821 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1823 match state.layout with
1825 | l :: _
-> state.y - l.pagey
1828 | Glut.KEY_PAGE_DOWN
->
1829 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1831 match List.rev
state.layout with
1833 | l :: _
-> getpagey l.pageno
1836 | Glut.KEY_HOME
-> addnav (); 0
1839 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1841 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1842 state.x
<- state.x
- 10;
1844 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1845 state.x
<- state.x
+ 10;
1850 gotoy_and_clear_text y
1852 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1855 | Glut.KEY_UP
-> action HCprev
1856 | Glut.KEY_DOWN
-> action HCnext
1857 | Glut.KEY_HOME
-> action HCfirst
1858 | Glut.KEY_END
-> action HClast
1861 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1862 Glut.postRedisplay
()
1867 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1868 let maxrows = maxoutlinerows () in
1869 let calcfirst first
active =
1872 let rows = active - first
in
1873 if rows > maxrows then active - maxrows else first
1877 let active = active + incr in
1878 let active = max
0 (min
active (Array.length
outlines - 1)) in
1879 let first = calcfirst first active in
1880 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1881 Glut.postRedisplay
()
1883 let updownlevel incr =
1884 let len = Array.length
outlines in
1885 let (_
, curlevel
, _
, _
) = outlines.(active) in
1887 if i = len then i-1 else if i = -1 then 0 else
1888 let (_
, l, _
, _
) = outlines.(i) in
1889 if l != curlevel
then i else flow (i+incr)
1891 let active = flow active in
1892 let first = calcfirst first active in
1893 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1894 Glut.postRedisplay
()
1897 | Glut.KEY_UP
-> navigate ~
-1
1898 | Glut.KEY_DOWN
-> navigate 1
1899 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1900 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1902 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1903 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1906 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1907 Glut.postRedisplay
()
1910 let active = Array.length
outlines - 1 in
1911 let first = max
0 (active - maxrows) in
1912 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1913 Glut.postRedisplay
()
1918 let drawplaceholder l =
1919 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1921 (float l.pagex
, float l.pagedispy
)
1922 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1924 let x = float (if margin < 0 then -margin else 0)
1925 and y = float (l.pagedispy
+ 13) in
1926 let font = Glut.BITMAP_8_BY_13
in
1927 GlDraw.color
(0.0, 0.0, 0.0);
1928 GlPix.raster_pos ~
x ~
y ();
1929 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1930 ("Loading " ^ string_of_int
(l.pageno + 1));
1933 let now () = Unix.gettimeofday
();;
1936 if state.textentry = None
1938 match state.birdseye
with
1939 | None
-> GlDraw.color
(scalecolor 1.0);
1940 | Some
(_
, _
, hooverpageno
) ->
1942 if l.pageno = state.birdseyepageno
1945 if l.pageno = hooverpageno
1950 GlDraw.color (scalecolor color);
1952 else GlDraw.color (scalecolor 0.4);
1953 begin match getopaque l.pageno with
1954 | Some
(opaque, _
) when validopaque opaque ->
1956 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1960 vlog "draw %d %f sec" l.pageno d;
1968 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1969 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1970 let sh = float conf.winh
/. sh in
1971 let sh = max
sh (float conf.scrollh
) in
1974 if state.y = state.maxy
1976 else float y /. float maxy
1978 let position = (float conf.winh
-. sh) *. percent in
1981 if position +. sh > float conf.winh
1982 then float conf.winh
-. sh
1988 let scrollindicator () =
1989 GlDraw.color (0.64 , 0.64, 0.64);
1991 (float (conf.winw
- conf.scrollw
), 0.)
1992 (float conf.winw
, float conf.winh
)
1994 GlDraw.color (0.0, 0.0, 0.0);
1996 let position, sh = scrollph state.y in
1998 (float (conf.winw
- conf.scrollw
), position)
1999 (float conf.winw
, position +. sh)
2003 let showsel margin =
2004 match state.mstate
with
2005 | Mnone
| Mscroll _
| Mpan _
->
2008 | Msel
((x0
, y0
), (x1
, y1
)) ->
2009 let rec loop = function
2011 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2012 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2014 match getopaque l.pageno with
2015 | Some
(opaque, _
) when validopaque opaque ->
2016 let oy = -l.pagey + l.pagedispy
in
2018 (x0
- margin - state.x, y0
,
2019 x1
- margin - state.x, y1
) oy;
2029 let panx = float state.x in
2031 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2032 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2034 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2036 if l.pageno = pageno
2038 let d = float (l.pagedispy
- l.pagey) in
2039 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2040 GlDraw.begins `quads
;
2042 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2043 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2044 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2045 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2055 let showoutline = function
2057 | Some
(allowdel
, active, first, outlines, qsearch) ->
2059 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2060 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2061 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2064 GlDraw.color (1., 1., 1.);
2065 let font = Glut.BITMAP_9_BY_15
in
2066 let draw_string x y s =
2067 GlPix.raster_pos ~
x ~
y ();
2068 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2071 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2074 let (s, l, _
, _
) = outlines.(row
) in
2075 let y = (row
- first) * 16 in
2080 GlDraw.polygon_mode `both `line
;
2081 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2082 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2083 GlDraw.rect (0., float (y + 1))
2084 (float (conf.winw
- 1), float (y + 18));
2085 GlDraw.polygon_mode `both `fill
;
2087 GlDraw.color (1., 1., 1.);
2089 draw_string (float x) (float (y + 16)) s;
2097 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2098 GlDraw.viewport
margin 0 state.w conf.winh
;
2100 GlClear.color (scalecolor 0.5);
2101 GlClear.clear
[`
color];
2104 let x = float state.x in
2105 GlMat.translate ~
x ();
2109 Gl.enable `scissor_test
;
2110 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2112 List.iter
drawpage state.layout;
2115 Gl.disable `scissor_test
2119 let x = -.float state.x in
2120 GlMat.translate ~
x ();
2124 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2127 showoutline state.outline;
2129 Glut.swapBuffers
();
2133 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2134 let x = x - margin - state.x in
2135 let rec f = function
2137 begin match getopaque l.pageno with
2138 | Some
(opaque, _
) when validopaque opaque ->
2139 let y = y - l.pagedispy
in
2142 let y = l.pagey + y in
2143 let x = x - l.pagex
in
2144 match whatsunder
opaque x y with
2157 let mouse ~button ~bstate ~
x ~
y =
2159 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2167 let incr = incr * 2 in
2168 let y = clamp incr in
2169 gotoy_and_clear_text y
2171 | Glut.LEFT_BUTTON
when state.outline = None
2172 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2173 if bstate
= Glut.DOWN
2175 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2176 state.mstate
<- Mpan
(x, y)
2179 state.mstate
<- Mnone
2182 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2183 if bstate
= Glut.DOWN
2185 let position, sh = scrollph state.y in
2186 if y > truncate
position && y < truncate
(position +. sh)
2188 state.mstate
<- Mscroll
2190 let percent = float y /. float conf.winh
in
2191 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2193 state.mstate
<- Mscroll
2195 state.mstate
<- Mnone
2197 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2198 begin match state.birdseye
with
2200 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2201 let rec loop = function
2204 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2205 && x > margin && x < margin + l.pagew
2207 state.birdseyepageno <- l.pageno;
2209 reshape conf.winw
conf.winh
;
2214 | None
-> () (* impossible *)
2217 | Glut.LEFT_BUTTON
when state.outline = None
->
2218 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2219 begin match dest with
2220 | Ulinkgoto
(pageno, top
) ->
2223 gotopage1 pageno top
2228 | Unone
when bstate
= Glut.DOWN
->
2229 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2230 state.mstate
<- Mpan
(x, y);
2232 | Unone
| Utext _
->
2233 if bstate
= Glut.DOWN
2235 if conf.angle
mod 360 = 0
2237 state.mstate
<- Msel
((x, y), (x, y));
2238 Glut.postRedisplay
()
2242 match state.mstate
with
2246 state.mstate
<- Mnone
2249 Glut.setCursor
Glut.CURSOR_INHERIT
;
2250 state.mstate
<- Mnone
2252 | Msel
((x0
, y0
), (x1
, y1
)) ->
2254 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2255 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2257 match getopaque l.pageno with
2258 | Some
(opaque, _
) when validopaque opaque ->
2262 List.iter
f state.layout;
2263 copysel
""; (* ugly *)
2264 Glut.setCursor
Glut.CURSOR_INHERIT
;
2265 state.mstate
<- Mnone
;
2272 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2275 if state.outline = None
2277 match state.mstate
with
2283 state.mstate
<- Mpan
(x, y);
2284 if conf.zoom > 1.0 then state.x <- state.x + dx;
2286 gotoy_and_clear_text y
2289 state.mstate
<- Msel
(a, (x, y));
2290 Glut.postRedisplay
()
2293 let y = min
conf.winh
(max
0 y) in
2294 let percent = float y /. float conf.winh
in
2295 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2296 gotoy_and_clear_text y
2300 match state.birdseye
with
2301 | Some
(conf, leftx
, hooverpageno
) ->
2302 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2303 let rec loop = function
2305 if hooverpageno
!= -1
2307 state.birdseye
<- Some
(conf, leftx
, -1);
2308 Glut.postRedisplay
();
2311 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2312 && x > margin && x < margin + l.pagew
2314 state.birdseye
<- Some
(conf, leftx
, l.pageno);
2315 Glut.postRedisplay
();
2322 if state.outline = None
2324 match state.mstate
with
2326 begin match getunder x y with
2327 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2329 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2330 Glut.setCursor
Glut.CURSOR_INFO
2331 | Ulinkgoto
(page
, y) ->
2333 then showtext 'p'
("age: " ^ string_of_int page
);
2334 Glut.setCursor
Glut.CURSOR_INFO
2336 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2337 Glut.setCursor
Glut.CURSOR_TEXT
2340 | Mpan _
| Msel _
| Mscroll
->
2351 match Sys.os_type
with
2352 | "Win32" -> Sys.getenv
"HOMEPATH"
2353 | _
-> Sys.getenv
"HOME"
2356 ("Can not determine home directory location: " ^
2357 Printexc.to_string exn
);
2361 let config_of c attrs
=
2365 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2366 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2367 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2368 | "preload" -> { c with preload = bool_of_string v
}
2369 | "page-bias" -> { c with pagebias
= int_of_string v
}
2370 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2371 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2372 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2373 | "throttle" -> { c with showall
= bool_of_string v
}
2374 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2375 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2376 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2378 let zoom = float_of_string v
/. 100. in
2379 let zoom = max
0.01 (min
2.2 zoom) in
2380 { c with zoom = zoom }
2381 | "presentation" -> { c with presentation = bool_of_string v
}
2382 | "rotation-angle" -> { c with angle
= int_of_string v
}
2383 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2384 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2385 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2386 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2387 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2388 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2389 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2392 prerr_endline
("Error processing attribute (`" ^
2393 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2396 let rec fold c = function
2399 let c = apply c k v
in
2405 let bookmark_of attrs
=
2406 let rec fold title page
rely = function
2407 | ("title", v
) :: rest
-> fold v page
rely rest
2408 | ("page", v
) :: rest
-> fold title v
rely rest
2409 | ("rely", v
) :: rest
-> fold title page v rest
2410 | _
:: rest
-> fold title page
rely rest
2411 | [] -> title, page
, rely
2413 fold "invalid" "0" "0" attrs
2416 let setconf dst src
=
2417 dst
.scrollw
<- src
.scrollw
;
2418 dst
.scrollh
<- src
.scrollh
;
2419 dst
.icase
<- src
.icase
;
2420 dst
.preload <- src
.preload;
2421 dst
.pagebias
<- src
.pagebias
;
2422 dst
.verbose
<- src
.verbose
;
2423 dst
.scrollincr
<- src
.scrollincr
;
2424 dst
.maxhfit
<- src
.maxhfit
;
2425 dst
.crophack
<- src
.crophack
;
2426 dst
.autoscroll
<- src
.autoscroll
;
2427 dst
.showall
<- src
.showall
;
2428 dst
.hlinks
<- src
.hlinks
;
2429 dst
.underinfo
<- src
.underinfo
;
2430 dst
.interpagespace <- src
.interpagespace;
2431 dst
.zoom <- src
.zoom;
2432 dst
.presentation <- src
.presentation;
2433 dst
.angle
<- src
.angle
;
2434 dst
.winw
<- src
.winw
;
2435 dst
.winh
<- src
.winh
;
2436 dst
.savebmarks
<- src
.savebmarks
;
2437 dst
.memlimit
<- src
.memlimit
;
2438 dst
.proportional
<- src
.proportional
;
2439 dst
.texcount
<- src
.texcount
;
2440 dst
.sliceheight
<- src
.sliceheight
;
2444 let l = String.length
s in
2445 let b = Buffer.create
l in
2451 let h = Hashtbl.create
10 in
2452 let dc = { defconf with angle
= defconf.angle
} in
2453 let rec toplevel v t spos epos
=
2455 | Vdata
| Vcdata
| Vend
-> v
2456 | Vopen
("llppconfig", attrs
, closed
) ->
2459 else { v
with f = llppconfig
}
2461 error
"unexpected subelement at top level" s spos
2462 | Vclose tag
-> error
"unexpected close at top level" s spos
2464 and llppconfig v t spos epos
=
2466 | Vdata
| Vcdata
| Vend
-> v
2467 | Vopen
("defaults", attrs
, closed
) ->
2468 let c = config_of dc attrs
in
2472 else { v
with f = skip
"defaults" (fun () -> v
) }
2474 | Vopen
("doc", attrs
, closed
) ->
2477 List.assoc
"path" attrs
2478 with Not_found
-> error
"doc is missing path attribute" s spos
2480 let path = unent pathent in
2481 let c = config_of dc attrs
in
2484 float_of_string
(List.assoc
"rely" attrs
)
2488 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2493 int_of_string
(List.assoc
"pan" attrs
)
2497 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2501 then (Hashtbl.add
h path (c, [], x, y); v
)
2502 else { v
with f = doc
path x y c [] }
2504 | Vopen
(tag
, _
, closed
) ->
2505 error
"unexpected subelement in llppconfig" s spos
2507 | Vclose
"llppconfig" -> { v
with f = toplevel }
2508 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2510 and doc
path x y c bookmarks v t spos epos
=
2512 | Vdata
| Vcdata
-> v
2513 | Vend
-> error
"unexpected end of input in doc" s spos
2514 | Vopen
("bookmarks", attrs
, closed
) ->
2515 { v
with f = pbookmarks
path x y c bookmarks }
2517 | Vopen
(tag
, _
, _
) ->
2518 error
"unexpected subelement in doc" s spos
2521 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2522 { v
with f = llppconfig
}
2524 | Vclose tag
-> error
"unexpected close in doc" s spos
2526 and pbookmarks
path x y c bookmarks v t spos epos
=
2528 | Vdata
| Vcdata
-> v
2529 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2530 | Vopen
("item", attrs
, closed
) ->
2531 let titleent, spage
, srely
= bookmark_of attrs
in
2536 dolog "Failed to convert page %S to integer: %s"
2537 spage
(Printexc.to_string exn
);
2542 float_of_string srely
2544 dolog "Failed to convert rely %S to real: %s"
2545 srely
(Printexc.to_string exn
);
2548 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2550 then { v
with f = pbookmarks
path x y c bookmarks }
2553 { v
with f = skip
"item" f }
2556 error
"unexpected subelement in bookmarks" s spos
2558 | Vclose
"bookmarks" ->
2559 { v
with f = doc
path x y c bookmarks }
2561 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2563 and skip tag
f v t spos epos
=
2565 | Vdata
| Vcdata
-> v
2567 error
("unexpected end of input in skipped " ^ tag
) s spos
2568 | Vopen
(tag'
, _
, closed
) ->
2572 let f'
() = { v
with f = skip tag
f } in
2573 { v
with f = skip tag'
f'
}
2577 else error
("unexpected close in skipped " ^ tag
) s spos
2580 parse
{ f = toplevel; accu = () } s;
2586 let len = in_channel_length ic
in
2587 let s = String.create
len in
2588 really_input ic
s 0 len;
2591 | Parse_error
(msg
, s, pos
) ->
2592 let subs = subs s pos
in
2593 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2594 failwith
("parse error: " ^
s)
2597 failwith
("config load error: " ^
Printexc.to_string exn
)
2603 let dir = Filename.concat
home ".config" in
2604 if Sys.is_directory
dir then dir else home
2607 Filename.concat
dir "llpp.conf"
2611 if Sys.file_exists
path
2614 (try Some
(open_in_bin
path)
2617 ("Error opening configuation file `" ^
path ^
"': " ^
2618 Printexc.to_string exn
);
2627 ("Error loading configuation from `" ^
path ^
"': " ^
2628 Printexc.to_string exn
);
2634 f (Hashtbl.create
0, defconf)
2639 let pc, pb
, px
, py =
2641 Hashtbl.find
h state.path
2642 with Not_found
-> dc, [], 0, 0.0
2646 state.bookmarks <- pb
;
2648 cbput state.hists
.nav
py;
2653 let add_attrs bb always
dc c =
2656 then Printf.bprintf bb
"\n %s='%b'" s a
2659 then Printf.bprintf bb
"\n %s='%d'" s a
2662 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2666 then dc.winw
, dc.winh
2668 match state.fullscreen
with
2670 | None
-> c.winw
, c.winh
2672 let zoom, presentation, interpagespace, showall
=
2674 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2676 match state.birdseye
with
2677 | Some
(bc
, _
, _
) ->
2678 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2679 | None
-> c.zoom, c.presentation, c.interpagespace, c.showall
2681 oi
"width" w dc.winw
;
2682 oi
"height" h dc.winh
;
2683 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2684 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2685 ob "case-insensitive-search" c.icase
dc.icase
;
2686 ob "preload" c.preload dc.preload;
2687 oi
"page-bias" c.pagebias
dc.pagebias
;
2688 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2689 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2690 ob "crop-hack" c.crophack
dc.crophack
;
2691 ob "throttle" showall
dc.showall
;
2692 ob "highlight-links" c.hlinks
dc.hlinks
;
2693 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2694 oi
"vertical-margin" interpagespace dc.interpagespace;
2695 oz
"zoom" zoom dc.zoom;
2696 ob "presentation" presentation dc.presentation;
2697 oi
"rotation-angle" c.angle
dc.angle
;
2698 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2699 ob "proportional-display" c.proportional
dc.proportional
;
2700 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2701 oi
"texcount" c.texcount
dc.texcount
;
2702 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2706 let bb = Buffer.create
32768 in
2708 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2709 add_attrs bb true dc dc;
2710 Buffer.add_string
bb "/>\n";
2712 let adddoc path x y c bookmarks =
2713 if bookmarks == [] && c = dc && y = 0.0
2716 Printf.bprintf
bb "<doc path='%s'"
2717 (enent
path 0 (String.length
path));
2720 then Printf.bprintf
bb " rely='%f'" y;
2723 then Printf.bprintf
bb " pan='%d'" x;
2725 add_attrs bb false dc c;
2727 begin match bookmarks with
2728 | [] -> Buffer.add_string
bb "/>\n"
2730 Buffer.add_string
bb ">\n<bookmarks>\n";
2731 List.iter
(fun (title, _level
, page, rely) ->
2733 "<item title='%s' page='%d' rely='%f'/>\n"
2734 (enent
title 0 (String.length
title))
2738 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2744 match state.birdseye
with
2745 | Some
(_
, x, _
) -> x
2748 adddoc state.path x (yratio state.y) conf
2749 (if conf.savebmarks
then state.bookmarks else []);
2751 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2752 if path <> state.path
2754 adddoc path x y c bookmarks
2756 Buffer.add_string
bb "</llppconfig>";
2759 if Buffer.length
bb > 0
2762 let tmp = path ^
".tmp" in
2763 let oc = open_out_bin
tmp in
2764 Buffer.output_buffer
oc bb;
2766 Sys.rename
tmp path;
2769 ("error while saving configuration: " ^
Printexc.to_string exn
)
2775 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2776 (fun s -> state.path <- s)
2777 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2780 if String.length
state.path = 0
2781 then (prerr_endline
"filename missing"; exit
1)
2783 if Filename.is_relative
state.path
2784 then Filename.concat
(Sys.getcwd
()) state.path
2792 let _ = Glut.init
Sys.argv
in
2793 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2794 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2795 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2798 if Sys.os_type
= "Unix"
2800 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2802 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2803 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2804 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2805 Unix.bind
sock addr;
2807 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2808 Unix.connect
csock addr;
2809 let ssock, _ = Unix.accept
sock in
2812 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2813 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2817 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2821 let () = Glut.displayFunc
display in
2822 let () = Glut.reshapeFunc
reshape in
2823 let () = Glut.keyboardFunc
keyboard in
2824 let () = Glut.specialFunc
special in
2825 let () = Glut.idleFunc
(Some
idle) in
2826 let () = Glut.mouseFunc
mouse in
2827 let () = Glut.motionFunc
motion in
2828 let () = Glut.passiveMotionFunc
pmotion in
2830 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2831 state.csock <- csock;
2832 state.ssock <- ssock;
2833 state.text <- "Opening " ^
path;
2834 writeopen state.path state.password
;
2838 let rec handlelablglutbug () =
2841 with Glut.BadEnum
"key in special_of_int" ->
2842 showtext '
!'
" LablGlut bug: special key not recognized";
2843 handlelablglutbug ()
2845 handlelablglutbug ();