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)
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 if state.birdseye
<> None
609 if not
(pagevisible pages state.birdseyepageno
)
611 match state.layout with
613 | l
:: _
-> state.birdseyepageno
<- l
.pageno
618 let gotoy_and_clear_text y =
620 if not
conf.verbose
then state.text
<- "";
624 cbput state.hists
.nav
(yratio state.y);
628 let y = cbgetc state.hists
.nav ~
-1 in
629 truncate
(y *. float state.maxy)
633 let y, h
= getpageyh n in
635 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
638 let gotopage1 n top
=
639 let y = getpagey n in
641 gotoy_and_clear_text (y + top
);
649 state.invalidated
<- state.invalidated
+ 1;
653 let c = c *. state.colorscale
in
658 let maxy = calcheight () in
660 if state.birdseyepageno
= -1 && state.birdseye
= None
662 match state.layout with
664 let rely = yratio state.y in
665 truncate
(float maxy *. rely)
670 let y = getpagey state.birdseyepageno
in
671 state.birdseyepageno
<- -1;
680 GlMat.mode `projection
;
681 GlMat.load_identity
();
682 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
683 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
684 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
688 GlMat.mode `projection
;
689 GlMat.load_identity
();
690 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
691 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
692 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
697 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
701 GlMat.mode `modelview
;
702 GlMat.load_identity
();
703 GlClear.color
(scalecolor 1.0);
704 GlClear.clear
[`color
];
707 wcmd "geometry" [`i
w; `i h
];
711 GlDraw.color
(0.0, 0.0, 0.0);
713 (0.0, float (conf.winh
- 18))
714 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
716 let font = Glut.BITMAP_8_BY_13
in
717 GlDraw.color
(1.0, 1.0, 1.0);
718 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
719 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
720 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
724 let len = String.length
state.text
in
725 match state.textentry
with
727 if len > 0 then showtext ' '
state.text
729 | Some
(c, text
, _
, _
, _
) ->
733 text ^
" [" ^
state.text ^
"]"
743 state.text
<- Printf.sprintf
"%c%s" c s;
744 Glut.postRedisplay
();
758 state.rects
<- state.rects1
;
759 Glut.postRedisplay
()
762 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
763 state.pagecount
<- n;
764 state.invalidated
<- state.invalidated
- 1;
765 if state.invalidated
= 0
769 let s = Scanf.sscanf
cmd "t %n"
770 (fun n -> String.sub
cmd n (String.length
cmd - n))
772 Glut.setWindowTitle
s
775 let s = Scanf.sscanf
cmd "T %n"
776 (fun n -> String.sub
cmd n (String.length
cmd - n))
778 if state.textentry
= None
785 Glut.postRedisplay
();
791 let s = Scanf.sscanf
cmd "V %n"
792 (fun n -> String.sub
cmd n (String.length
cmd - n))
798 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
799 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
800 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
801 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
803 let y = (getpagey pageno) + truncate y0
in
806 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
809 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
810 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
811 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
812 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
815 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
818 let n, w, h
, r
, l
, s, p
=
819 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
820 (fun n w h r l
s p
->
821 (n-1, w, h
, r
, l
!= 0, s, p
))
824 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
825 state.memused
<- state.memused
+ s;
828 match state.throttle
with
829 | None
-> state.layout
830 | Some
layout -> layout
834 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
837 let evictedopaque = cbpeek state.pagecache
in
838 match findpageforopaque evictedopaque with
839 | None
-> failwith
"bug in gc"
840 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
841 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
843 wcmd "free" [`
s evictedopaque];
844 state.memused
<- state.memused
- evictedsize
;
845 Hashtbl.remove
state.pagemap k
;
846 cbdecr state.pagecache
;
853 cbput state.pagecache p
;
854 state.rendering
<- false;
856 begin match state.throttle
with
858 if pagevisible state.layout n
861 let allvisible = loadlayout state.layout in
862 if allvisible then preload ();
869 let y = getpagey l
.pageno + l
.pagey in
874 let (n, w, h
, x
) as pdim
=
875 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
877 state.pdims
<- pdim
:: state.pdims
880 let (l
, n, t
, h
, pos
) =
881 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
883 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
885 let l = String.length
s in
886 let b = Buffer.create
(String.length
s) in
893 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
896 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
905 let outline = (s, l, n, float t
/. float h
) in
907 match state.outlines with
908 | Olist
outlines -> Olist
(outline :: outlines)
909 | Oarray _
-> Olist
[outline]
910 | Onarrow _
-> Olist
[outline]
912 state.outlines <- outlines
915 log "unknown cmd `%S'" cmd
918 let now = Unix.gettimeofday
;;
922 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
927 let y = state.y + conf.scrollincr
in
928 let y = if y >= state.maxy then 0 else y in
934 let cmd = readcmd state.csock
in
943 let action = function
944 | HCprev
-> cbget cb ~
-1
945 | HCnext
-> cbget cb
1
946 | HCfirst
-> cbget cb ~
-(cb
.rc)
947 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
948 and cancel
() = cb
.rc <- rc
952 let search pattern forward
=
953 if String.length pattern
> 0
956 match state.layout with
959 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
962 let b = makecmd "search"
963 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
965 Buffer.add_char
b '
,'
;
966 Buffer.add_string
b pattern
;
967 Buffer.add_char
b '
\000'
;
970 writecmd state.csock
cmd;
973 let intentry text key
=
974 let c = Char.unsafe_chr key
in
977 let s = "x" in s.[0] <- c;
978 let text = text ^
s in
982 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
987 let b = Buffer.create
(String.length
s + 1) in
988 Buffer.add_string
b s;
993 let textentry text key
=
994 let c = Char.unsafe_chr key
in
996 | _
when key
>= 32 && key
< 127 ->
997 let text = addchar text c in
1001 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1005 let reinit angle proportional
=
1006 conf.angle
<- angle
;
1007 conf.proportional
<- proportional
;
1009 wcmd "reinit" [`i angle
; `
b proportional
];
1012 let optentry text key
=
1013 let btos b = if b then "on" else "off" in
1014 let c = Char.unsafe_chr key
in
1018 try conf.scrollincr
<- int_of_string
s with exc
->
1019 state.text <- Printf.sprintf
"bad integer `%s': %s"
1020 s (Printexc.to_string exc
)
1022 TEswitch
('#'
, "", None
, intentry, ondone)
1027 Some
(int_of_string
s)
1029 state.text <- Printf.sprintf
"bad integer `%s': %s"
1030 s (Printexc.to_string exc
);
1033 | Some angle
-> reinit angle
conf.proportional
1036 TEswitch
('^'
, "", None
, intentry, ondone)
1039 conf.icase
<- not
conf.icase
;
1040 TEdone
("case insensitive search " ^
(btos conf.icase
))
1043 conf.preload <- not
conf.preload;
1045 TEdone
("preload " ^
(btos conf.preload))
1048 conf.verbose
<- not
conf.verbose
;
1049 TEdone
("verbose " ^
(btos conf.verbose
))
1052 conf.maxhfit
<- not
conf.maxhfit
;
1053 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1054 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1057 conf.crophack
<- not
conf.crophack
;
1058 TEdone
("crophack " ^
btos conf.crophack
)
1061 conf.showall
<- not
conf.showall
;
1062 TEdone
("showall " ^
btos conf.showall
)
1065 conf.underinfo
<- not
conf.underinfo
;
1066 TEdone
("underinfo " ^
btos conf.underinfo
)
1069 conf.savebmarks
<- not
conf.savebmarks
;
1070 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1076 match state.layout with
1081 conf.interpagespace <- int_of_string
s;
1082 state.maxy <- calcheight ();
1083 let y = getpagey pageno in
1086 state.text <- Printf.sprintf
"bad integer `%s': %s"
1087 s (Printexc.to_string exc
)
1089 TEswitch
('
%'
, "", None
, intentry, ondone)
1092 reinit conf.angle
(not
conf.proportional
);
1093 TEdone
("proprortional display " ^
btos conf.proportional
)
1096 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1100 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1102 let enterselector allowdel
outlines errmsg msg
=
1103 if Array.length
outlines = 0
1105 showtext ' ' errmsg
;
1109 Glut.setCursor
Glut.CURSOR_INHERIT
;
1111 match state.layout with
1113 | {pageno=pageno} :: rest
-> pageno
1117 if n = Array.length
outlines
1120 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1121 if outlinepageno
>= pageno then n else loop (n+1)
1126 Some
(allowdel
, active,
1127 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1128 Glut.postRedisplay
();
1132 let enteroutlinemode () =
1134 match state.outlines with
1137 let a = Array.of_list
(List.rev
l) in
1138 state.outlines <- Oarray
a;
1140 | Onarrow
(pat
, a, b) ->
1141 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1143 enterselector false outlines "Document has no outline" msg
;
1146 let enterbookmarkmode () =
1147 let bookmarks = Array.of_list
state.bookmarks in
1148 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1151 let quickbookmark ?title
() =
1152 match state.layout with
1158 let sec = Unix.gettimeofday
() in
1159 let tm = Unix.localtime
sec in
1160 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1164 (tm.Unix.tm_year
+ 1900)
1167 | Some
title -> title
1170 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1174 state.fullscreen
<- None
;
1175 Glut.reshapeWindow
w h
;
1178 let writeopen path password
=
1179 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1182 let opendoc path password
=
1185 state.password
<- password
;
1186 state.gen
<- state.gen
+ 1;
1188 writeopen path password
;
1189 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1190 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1193 let birdseyeoff (c, leftx
, _
) =
1194 state.birdseye
<- None
;
1195 conf.zoom
<- c.zoom
;
1196 conf.presentation <- c.presentation;
1197 conf.interpagespace <- c.interpagespace;
1198 conf.showall
<- c.showall
;
1199 conf.hlinks
<- c.hlinks
;
1203 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1208 let viewkeyboard ~key ~x ~
y =
1210 state.textentry <- te
;
1213 Glut.postRedisplay
()
1215 match state.textentry with
1217 let c = Char.chr key
in
1223 let y = getnav () in
1224 gotoy_and_clear_text y
1227 begin match state.birdseye
with
1231 reshape conf.winw
conf.winh
;
1240 Glut.postRedisplay
()
1243 let ondone isforw
s =
1244 cbput state.hists
.pat
s;
1245 state.searchpattern
<- s;
1248 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1249 textentry, ondone (c ='
/'
)))
1251 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1252 let incr = if conf.zoom
+. 0.01 > 0.1 then 0.1 else 0.01 in
1253 conf.zoom
<- min
2.2 (conf.zoom
+. incr);
1254 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1255 reshape conf.winw
conf.winh
1260 try int_of_string
s with exc
->
1261 state.text <- Printf.sprintf
"bad integer `%s': %s"
1262 s (Printexc.to_string exc
);
1268 state.text <- "page bias is now " ^ string_of_int
n;
1271 enttext (Some
('
+'
, "", None
, intentry, ondone))
1273 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1274 let decr = if conf.zoom
-. 0.1 < 0.1 then 0.01 else 0.1 in
1275 conf.zoom
<- max
0.01 (conf.zoom
-. decr);
1276 if conf.zoom
<= 1.0 then state.x
<- 0;
1277 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1278 reshape conf.winw
conf.winh
;
1284 enttext (Some
('
-'
, "", None
, optentry, ondone))
1286 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1289 state.text <- "zoom is 100%";
1290 reshape conf.winw
conf.winh
1292 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1293 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1298 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1299 reshape conf.winw
conf.winh
;
1302 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1303 begin match state.birdseye
with
1305 let zoom = 76.0 /. float state.w in
1306 let birdseyepageno =
1307 match state.layout with
1309 | l :: _
-> l.pageno
1311 state.birdseyepageno <- birdseyepageno;
1313 Some
({ conf with zoom = conf.zoom }, state.x
, -1);
1315 conf.presentation <- false;
1316 conf.interpagespace <- 10;
1317 conf.hlinks
<- false;
1319 state.mstate
<- Mnone
;
1320 conf.showall
<- false;
1321 Glut.setCursor
Glut.CURSOR_INHERIT
;
1324 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1333 reshape conf.winw
conf.winh
;
1338 try int_of_string
s with exc
->
1339 state.text <- Printf.sprintf
"bad integer `%s': %s"
1340 s (Printexc.to_string exc
);
1346 cbput state.hists
.pag
(string_of_int
n);
1347 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1350 let pageentry text key
=
1351 match Char.unsafe_chr key
with
1352 | 'g'
-> TEdone
text
1353 | _
-> intentry text key
1355 let text = "x" in text.[0] <- c;
1356 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1360 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1361 reshape conf.winw
conf.winh
;
1364 conf.hlinks
<- not
conf.hlinks
;
1365 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1366 Glut.postRedisplay
()
1369 conf.autoscroll
<- not
conf.autoscroll
1372 conf.presentation <- not
conf.presentation;
1373 showtext ' '
("presentation mode " ^
1374 if conf.presentation then "on" else "off");
1378 begin match state.fullscreen
with
1380 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1383 state.fullscreen
<- None
;
1388 gotoy_and_clear_text 0
1391 search state.searchpattern
true
1394 search state.searchpattern
false
1397 begin match state.layout with
1400 gotoy_and_clear_text (getpagey l.pageno)
1404 begin match List.rev
state.layout with
1407 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1408 gotoy_and_clear_text (getpagey pageno)
1412 begin match state.layout with
1415 let pageno = max
0 (l.pageno-1) in
1416 gotoy_and_clear_text (getpagey pageno)
1421 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1423 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1425 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1429 else (100. *. (float state.y /. float maxy)) in
1432 Printf.sprintf
"Page %d of %d %.2f%%"
1433 (fn+1) state.pagecount
percent
1436 "Pages %d-%d of %d %.2f%%"
1437 (fn+1) (ln
+1) state.pagecount
percent
1442 begin match state.layout with
1445 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1446 Glut.postRedisplay
();
1450 enterbookmarkmode ()
1454 match state.layout with
1457 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1461 enttext (Some
('~'
, "", None
, textentry, ondone))
1465 showtext ' '
"Quick bookmark added";
1468 begin match state.layout with
1470 let rect = getpdimrect
l.pagedimno
in
1474 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1475 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1477 (truncate
(rect.(1) -. rect.(0)),
1478 truncate
(rect.(3) -. rect.(0)))
1482 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1484 Glut.postRedisplay
();
1490 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1495 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1496 Glut.postRedisplay
()
1498 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1499 | 'j'
-> gotoy (clamp conf.scrollincr
)
1501 | '
r'
-> opendoc state.path
state.password
1504 vlog "huh? %d %c" key
(Char.chr key
);
1507 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1508 let len = String.length
text in
1511 state.textentry <- None
;
1512 Glut.postRedisplay
();
1515 let s = String.sub
text 0 (len - 1) in
1516 enttext (Some
(c, s, opthist
, onkey
, ondone))
1519 | Some
(c, text, onhist, onkey
, ondone) ->
1520 begin match Char.unsafe_chr key
with
1523 state.textentry <- None
;
1524 Glut.postRedisplay
()
1527 begin match onhist with
1529 | Some
(_
, onhistcancel
) -> onhistcancel
()
1531 state.textentry <- None
;
1532 Glut.postRedisplay
()
1535 begin match onkey
text key
with
1537 state.textentry <- None
;
1539 Glut.postRedisplay
()
1542 enttext (Some
(c, text, onhist, onkey
, ondone));
1545 state.textentry <- None
;
1546 Glut.postRedisplay
()
1549 state.textentry <- Some te
;
1550 Glut.postRedisplay
()
1555 let narrow outlines pattern
=
1556 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1560 let rec fold accu n =
1564 let (s, _
, _
, _
) as o
= outlines.(n) in
1566 if (try ignore
(Str.search_forward re
s 0); true
1567 with Not_found
-> false)
1573 let matched = fold [] (Array.length
outlines - 1) in
1574 if matched = [] then None
else Some
(Array.of_list
matched)
1577 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1578 let search active pattern
incr =
1581 if n = Array.length
outlines || n = -1
1584 let (s, _
, _
, _
) = outlines.(n) in
1586 (try ignore
(Str.search_forward re
s 0); true
1587 with Not_found
-> false)
1589 else loop (n + incr)
1594 let re = Str.regexp_case_fold pattern
in
1600 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1603 if String.length qsearch
= 0
1606 state.outline <- None
;
1607 Glut.postRedisplay
();
1611 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1612 Glut.postRedisplay
();
1616 let incr = if key
= 18 then -1 else 1 in
1618 match search (active + incr) qsearch
incr with
1620 state.text <- qsearch ^
" [not found]";
1623 state.text <- qsearch
;
1624 active, firstof active
1626 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1627 Glut.postRedisplay
();
1630 let len = String.length qsearch
in
1637 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1640 let qsearch = String.sub
qsearch 0 (len - 1) in
1642 match search active qsearch ~
-1 with
1644 state.text <- qsearch ^
" [not found]";
1647 state.text <- qsearch;
1648 active, firstof active
1650 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1652 Glut.postRedisplay
()
1655 if active < Array.length
outlines
1657 let (_
, _
, n, t
) = outlines.(active) in
1661 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1662 state.outline <- None
;
1663 Glut.postRedisplay
();
1665 | _
when key
>= 32 && key
< 127 ->
1666 let pattern = addchar qsearch (Char.chr key
) in
1668 match search active pattern 1 with
1670 state.text <- pattern ^
" [not found]";
1673 state.text <- pattern;
1674 active, firstof active
1676 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1677 Glut.postRedisplay
()
1679 | 14 when not allowdel
-> (* ctrl-n *)
1680 if String.length
qsearch > 0
1682 let optoutlines = narrow outlines qsearch in
1683 begin match optoutlines with
1684 | None
-> state.text <- "can't narrow"
1686 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1687 match state.outlines with
1690 state.outlines <- Onarrow
(qsearch, outlines, a)
1691 | Onarrow
(pat
, a, b) ->
1692 state.outlines <- Onarrow
(qsearch, outlines, b)
1695 Glut.postRedisplay
()
1697 | 21 when not allowdel
-> (* ctrl-u *)
1699 match state.outlines with
1702 let a = Array.of_list
(List.rev
l) in
1703 state.outlines <- Oarray
a;
1705 | Onarrow
(pat
, a, b) ->
1706 state.outlines <- Oarray
b;
1710 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1711 Glut.postRedisplay
()
1715 Some
(allowdel
, active, firstof active, outlines, qsearch);
1716 Glut.postRedisplay
()
1718 | 127 when allowdel
->
1719 let len = Array.length
outlines - 1 in
1722 state.outline <- None
;
1723 state.bookmarks <- [];
1726 let bookmarks = Array.init
len
1728 let i = if i >= active then i + 1 else i in
1739 Glut.postRedisplay
()
1741 | _
-> log "unknown key %d" key
1744 let keyboard ~key ~x ~
y =
1749 match state.outline with
1750 | None
-> viewkeyboard ~key ~x ~
y
1751 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1754 let special ~key ~x ~
y =
1755 match state.outline with
1756 | None
when state.birdseye
<> None
->
1757 begin match key
with
1759 let pageno = max
0 (state.birdseyepageno - 1) in
1760 state.birdseyepageno <- pageno;
1761 if not
(pagevisible state.layout pageno)
1762 then gotopage pageno 0.0
1763 else Glut.postRedisplay
();
1766 let pageno = min
(state.pagecount
- 1) (state.birdseyepageno + 1) in
1767 state.birdseyepageno <- pageno;
1768 if not
(pagevisible state.layout pageno)
1770 begin match List.rev
state.layout with
1771 | [] -> gotopage pageno 0.0
1773 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1775 else Glut.postRedisplay
();
1777 | Glut.KEY_PAGE_UP
->
1778 begin match state.layout with
1780 if l.pageno = state.birdseyepageno
1782 match layout (state.y - conf.winh
) conf.winh
with
1783 | [] -> gotoy (clamp (-conf.winh
))
1785 state.birdseyepageno <- max
0 (l.pageno - 1);
1786 gotopage state.birdseyepageno 0.0
1789 state.birdseyepageno <- max
0 (l.pageno - 1);
1790 gotopage state.birdseyepageno 0.0
1792 | [] -> gotoy (clamp (-conf.winh
))
1794 | Glut.KEY_PAGE_DOWN
->
1795 begin match List.rev
state.layout with
1797 state.birdseyepageno <- min
(state.pagecount
- 1) (l.pageno + 1);
1798 gotoy (clamp (l.pagedispy
+ l.pageh
))
1799 | [] -> gotoy (clamp conf.winh
)
1803 state.birdseyepageno <- 0;
1806 state.birdseyepageno <- state.pagecount
- 1;
1807 if not
(pagevisible state.layout state.birdseyepageno)
1809 gotopage state.birdseyepageno 0.0
1811 Glut.postRedisplay
()
1817 begin match state.textentry with
1821 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1822 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1823 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1824 | Glut.KEY_PAGE_UP
->
1825 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1827 match state.layout with
1829 | l :: _
-> state.y - l.pagey
1832 | Glut.KEY_PAGE_DOWN
->
1833 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1835 match List.rev
state.layout with
1837 | l :: _
-> getpagey l.pageno
1840 | Glut.KEY_HOME
-> addnav (); 0
1843 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1845 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1846 state.x
<- state.x
- 10;
1848 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1849 state.x
<- state.x
+ 10;
1854 gotoy_and_clear_text y
1856 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1859 | Glut.KEY_UP
-> action HCprev
1860 | Glut.KEY_DOWN
-> action HCnext
1861 | Glut.KEY_HOME
-> action HCfirst
1862 | Glut.KEY_END
-> action HClast
1865 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1866 Glut.postRedisplay
()
1871 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1872 let maxrows = maxoutlinerows () in
1873 let calcfirst first
active =
1876 let rows = active - first
in
1877 if rows > maxrows then active - maxrows else first
1881 let active = active + incr in
1882 let active = max
0 (min
active (Array.length
outlines - 1)) in
1883 let first = calcfirst first active in
1884 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1885 Glut.postRedisplay
()
1887 let updownlevel incr =
1888 let len = Array.length
outlines in
1889 let (_
, curlevel
, _
, _
) = outlines.(active) in
1891 if i = len then i-1 else if i = -1 then 0 else
1892 let (_
, l, _
, _
) = outlines.(i) in
1893 if l != curlevel
then i else flow (i+incr)
1895 let active = flow active in
1896 let first = calcfirst first active in
1897 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1898 Glut.postRedisplay
()
1901 | Glut.KEY_UP
-> navigate ~
-1
1902 | Glut.KEY_DOWN
-> navigate 1
1903 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1904 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1906 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1907 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1910 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1911 Glut.postRedisplay
()
1914 let active = Array.length
outlines - 1 in
1915 let first = max
0 (active - maxrows) in
1916 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1917 Glut.postRedisplay
()
1922 let drawplaceholder l =
1923 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1925 (float l.pagex
, float l.pagedispy
)
1926 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1928 let x = float (if margin < 0 then -margin else l.pagex
)
1929 and y = float (l.pagedispy
+ 13) in
1930 let font = Glut.BITMAP_8_BY_13
in
1931 GlDraw.color
(0.0, 0.0, 0.0);
1932 GlPix.raster_pos ~
x ~
y ();
1933 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1934 ("Loading " ^ string_of_int
(l.pageno + 1));
1937 let now () = Unix.gettimeofday
();;
1940 if state.textentry = None
1942 match state.birdseye
with
1943 | None
-> GlDraw.color
(scalecolor 1.0);
1944 | Some
(_
, _
, hooverpageno
) ->
1946 if l.pageno = state.birdseyepageno
1949 if l.pageno = hooverpageno
1954 GlDraw.color (scalecolor color);
1956 else GlDraw.color (scalecolor 0.4);
1957 begin match getopaque l.pageno with
1958 | Some
(opaque, _
) when validopaque opaque ->
1960 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1964 vlog "draw %d %f sec" l.pageno d;
1972 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1973 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1974 let sh = float conf.winh
/. sh in
1975 let sh = max
sh (float conf.scrollh
) in
1978 if state.y = state.maxy
1980 else float y /. float maxy
1982 let position = (float conf.winh
-. sh) *. percent in
1985 if position +. sh > float conf.winh
1986 then float conf.winh
-. sh
1992 let scrollindicator () =
1993 GlDraw.color (0.64 , 0.64, 0.64);
1995 (float (conf.winw
- conf.scrollw
), 0.)
1996 (float conf.winw
, float conf.winh
)
1998 GlDraw.color (0.0, 0.0, 0.0);
2000 let position, sh = scrollph state.y in
2002 (float (conf.winw
- conf.scrollw
), position)
2003 (float conf.winw
, position +. sh)
2007 let showsel margin =
2008 match state.mstate
with
2009 | Mnone
| Mscroll _
| Mpan _
->
2012 | Msel
((x0
, y0
), (x1
, y1
)) ->
2013 let rec loop = function
2015 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2016 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2018 match getopaque l.pageno with
2019 | Some
(opaque, _
) when validopaque opaque ->
2020 let oy = -l.pagey + l.pagedispy
in
2022 (x0
- margin - state.x, y0
,
2023 x1
- margin - state.x, y1
) oy;
2033 let panx = float state.x in
2035 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2036 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2038 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2040 if l.pageno = pageno
2042 let d = float (l.pagedispy
- l.pagey) in
2043 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2044 GlDraw.begins `quads
;
2046 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2047 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2048 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2049 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2059 let showoutline = function
2061 | Some
(allowdel
, active, first, outlines, qsearch) ->
2063 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2064 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2065 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2068 GlDraw.color (1., 1., 1.);
2069 let font = Glut.BITMAP_9_BY_15
in
2070 let draw_string x y s =
2071 GlPix.raster_pos ~
x ~
y ();
2072 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2075 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2078 let (s, l, _
, _
) = outlines.(row
) in
2079 let y = (row
- first) * 16 in
2084 GlDraw.polygon_mode `both `line
;
2085 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2086 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2087 GlDraw.rect (0., float (y + 1))
2088 (float (conf.winw
- 1), float (y + 18));
2089 GlDraw.polygon_mode `both `fill
;
2091 GlDraw.color (1., 1., 1.);
2093 draw_string (float x) (float (y + 16)) s;
2101 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2102 GlDraw.viewport
margin 0 state.w conf.winh
;
2104 GlClear.color (scalecolor 0.5);
2105 GlClear.clear
[`
color];
2108 let x = float state.x in
2109 GlMat.translate ~
x ();
2113 Gl.enable `scissor_test
;
2114 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2116 List.iter
drawpage state.layout;
2119 Gl.disable `scissor_test
2123 let x = -.float state.x in
2124 GlMat.translate ~
x ();
2128 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2131 showoutline state.outline;
2133 Glut.swapBuffers
();
2137 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2138 let x = x - margin - state.x in
2139 let rec f = function
2141 begin match getopaque l.pageno with
2142 | Some
(opaque, _
) when validopaque opaque ->
2143 let y = y - l.pagedispy
in
2146 let y = l.pagey + y in
2147 let x = x - l.pagex
in
2148 match whatsunder
opaque x y with
2161 let mouse ~button ~bstate ~
x ~
y =
2163 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2171 let incr = incr * 2 in
2172 let y = clamp incr in
2173 gotoy_and_clear_text y
2175 | Glut.LEFT_BUTTON
when state.outline = None
2176 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2177 if bstate
= Glut.DOWN
2179 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2180 state.mstate
<- Mpan
(x, y)
2183 state.mstate
<- Mnone
2186 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2187 if bstate
= Glut.DOWN
2189 let position, sh = scrollph state.y in
2190 if y > truncate
position && y < truncate
(position +. sh)
2192 state.mstate
<- Mscroll
2194 let percent = float y /. float conf.winh
in
2195 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2197 state.mstate
<- Mscroll
2199 state.mstate
<- Mnone
2201 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2202 begin match state.birdseye
with
2204 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2205 let rec loop = function
2208 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2209 && x > margin && x < margin + l.pagew
2211 state.birdseyepageno <- l.pageno;
2213 reshape conf.winw
conf.winh
;
2218 | None
-> () (* impossible *)
2221 | Glut.LEFT_BUTTON
when state.outline = None
->
2222 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2223 begin match dest with
2224 | Ulinkgoto
(pageno, top
) ->
2227 gotopage1 pageno top
2232 | Unone
when bstate
= Glut.DOWN
->
2233 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2234 state.mstate
<- Mpan
(x, y);
2236 | Unone
| Utext _
->
2237 if bstate
= Glut.DOWN
2239 if conf.angle
mod 360 = 0
2241 state.mstate
<- Msel
((x, y), (x, y));
2242 Glut.postRedisplay
()
2246 match state.mstate
with
2250 state.mstate
<- Mnone
2253 Glut.setCursor
Glut.CURSOR_INHERIT
;
2254 state.mstate
<- Mnone
2256 | Msel
((x0
, y0
), (x1
, y1
)) ->
2258 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2259 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2261 match getopaque l.pageno with
2262 | Some
(opaque, _
) when validopaque opaque ->
2266 List.iter
f state.layout;
2267 copysel
""; (* ugly *)
2268 Glut.setCursor
Glut.CURSOR_INHERIT
;
2269 state.mstate
<- Mnone
;
2276 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2279 if state.outline = None
2281 match state.mstate
with
2287 state.mstate
<- Mpan
(x, y);
2288 if conf.zoom > 1.0 then state.x <- state.x + dx;
2290 gotoy_and_clear_text y
2293 state.mstate
<- Msel
(a, (x, y));
2294 Glut.postRedisplay
()
2297 let y = min
conf.winh
(max
0 y) in
2298 let percent = float y /. float conf.winh
in
2299 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2300 gotoy_and_clear_text y
2304 match state.birdseye
with
2305 | Some
(conf, leftx
, hooverpageno
) ->
2306 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2307 let rec loop = function
2309 if hooverpageno
!= -1
2311 state.birdseye
<- Some
(conf, leftx
, -1);
2312 Glut.postRedisplay
();
2315 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2316 && x > margin && x < margin + l.pagew
2318 state.birdseye
<- Some
(conf, leftx
, l.pageno);
2319 Glut.postRedisplay
();
2326 if state.outline = None
2328 match state.mstate
with
2330 begin match getunder x y with
2331 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2333 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2334 Glut.setCursor
Glut.CURSOR_INFO
2335 | Ulinkgoto
(page
, y) ->
2337 then showtext 'p'
("age: " ^ string_of_int page
);
2338 Glut.setCursor
Glut.CURSOR_INFO
2340 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2341 Glut.setCursor
Glut.CURSOR_TEXT
2344 | Mpan _
| Msel _
| Mscroll
->
2355 match Sys.os_type
with
2356 | "Win32" -> Sys.getenv
"HOMEPATH"
2357 | _
-> Sys.getenv
"HOME"
2360 ("Can not determine home directory location: " ^
2361 Printexc.to_string exn
);
2365 let config_of c attrs
=
2369 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2370 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2371 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2372 | "preload" -> { c with preload = bool_of_string v
}
2373 | "page-bias" -> { c with pagebias
= int_of_string v
}
2374 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2375 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2376 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2377 | "throttle" -> { c with showall
= bool_of_string v
}
2378 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2379 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2380 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2382 let zoom = float_of_string v
/. 100. in
2383 let zoom = max
0.01 (min
2.2 zoom) in
2384 { c with zoom = zoom }
2385 | "presentation" -> { c with presentation = bool_of_string v
}
2386 | "rotation-angle" -> { c with angle
= int_of_string v
}
2387 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2388 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2389 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2390 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2391 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2392 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2393 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2396 prerr_endline
("Error processing attribute (`" ^
2397 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2400 let rec fold c = function
2403 let c = apply c k v
in
2409 let bookmark_of attrs
=
2410 let rec fold title page
rely = function
2411 | ("title", v
) :: rest
-> fold v page
rely rest
2412 | ("page", v
) :: rest
-> fold title v
rely rest
2413 | ("rely", v
) :: rest
-> fold title page v rest
2414 | _
:: rest
-> fold title page
rely rest
2415 | [] -> title, page
, rely
2417 fold "invalid" "0" "0" attrs
2420 let setconf dst src
=
2421 dst
.scrollw
<- src
.scrollw
;
2422 dst
.scrollh
<- src
.scrollh
;
2423 dst
.icase
<- src
.icase
;
2424 dst
.preload <- src
.preload;
2425 dst
.pagebias
<- src
.pagebias
;
2426 dst
.verbose
<- src
.verbose
;
2427 dst
.scrollincr
<- src
.scrollincr
;
2428 dst
.maxhfit
<- src
.maxhfit
;
2429 dst
.crophack
<- src
.crophack
;
2430 dst
.autoscroll
<- src
.autoscroll
;
2431 dst
.showall
<- src
.showall
;
2432 dst
.hlinks
<- src
.hlinks
;
2433 dst
.underinfo
<- src
.underinfo
;
2434 dst
.interpagespace <- src
.interpagespace;
2435 dst
.zoom <- src
.zoom;
2436 dst
.presentation <- src
.presentation;
2437 dst
.angle
<- src
.angle
;
2438 dst
.winw
<- src
.winw
;
2439 dst
.winh
<- src
.winh
;
2440 dst
.savebmarks
<- src
.savebmarks
;
2441 dst
.memlimit
<- src
.memlimit
;
2442 dst
.proportional
<- src
.proportional
;
2443 dst
.texcount
<- src
.texcount
;
2444 dst
.sliceheight
<- src
.sliceheight
;
2448 let l = String.length
s in
2449 let b = Buffer.create
l in
2455 let h = Hashtbl.create
10 in
2456 let dc = { defconf with angle
= defconf.angle
} in
2457 let rec toplevel v t spos epos
=
2459 | Vdata
| Vcdata
| Vend
-> v
2460 | Vopen
("llppconfig", attrs
, closed
) ->
2463 else { v
with f = llppconfig
}
2465 error
"unexpected subelement at top level" s spos
2466 | Vclose tag
-> error
"unexpected close at top level" s spos
2468 and llppconfig v t spos epos
=
2470 | Vdata
| Vcdata
| Vend
-> v
2471 | Vopen
("defaults", attrs
, closed
) ->
2472 let c = config_of dc attrs
in
2476 else { v
with f = skip
"defaults" (fun () -> v
) }
2478 | Vopen
("doc", attrs
, closed
) ->
2481 List.assoc
"path" attrs
2482 with Not_found
-> error
"doc is missing path attribute" s spos
2484 let path = unent pathent in
2485 let c = config_of dc attrs
in
2488 float_of_string
(List.assoc
"rely" attrs
)
2492 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2497 int_of_string
(List.assoc
"pan" attrs
)
2501 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2505 then (Hashtbl.add
h path (c, [], x, y); v
)
2506 else { v
with f = doc
path x y c [] }
2508 | Vopen
(tag
, _
, closed
) ->
2509 error
"unexpected subelement in llppconfig" s spos
2511 | Vclose
"llppconfig" -> { v
with f = toplevel }
2512 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2514 and doc
path x y c bookmarks v t spos epos
=
2516 | Vdata
| Vcdata
-> v
2517 | Vend
-> error
"unexpected end of input in doc" s spos
2518 | Vopen
("bookmarks", attrs
, closed
) ->
2519 { v
with f = pbookmarks
path x y c bookmarks }
2521 | Vopen
(tag
, _
, _
) ->
2522 error
"unexpected subelement in doc" s spos
2525 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2526 { v
with f = llppconfig
}
2528 | Vclose tag
-> error
"unexpected close in doc" s spos
2530 and pbookmarks
path x y c bookmarks v t spos epos
=
2532 | Vdata
| Vcdata
-> v
2533 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2534 | Vopen
("item", attrs
, closed
) ->
2535 let titleent, spage
, srely
= bookmark_of attrs
in
2540 dolog "Failed to convert page %S to integer: %s"
2541 spage
(Printexc.to_string exn
);
2546 float_of_string srely
2548 dolog "Failed to convert rely %S to real: %s"
2549 srely
(Printexc.to_string exn
);
2552 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2554 then { v
with f = pbookmarks
path x y c bookmarks }
2557 { v
with f = skip
"item" f }
2560 error
"unexpected subelement in bookmarks" s spos
2562 | Vclose
"bookmarks" ->
2563 { v
with f = doc
path x y c bookmarks }
2565 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2567 and skip tag
f v t spos epos
=
2569 | Vdata
| Vcdata
-> v
2571 error
("unexpected end of input in skipped " ^ tag
) s spos
2572 | Vopen
(tag'
, _
, closed
) ->
2576 let f'
() = { v
with f = skip tag
f } in
2577 { v
with f = skip tag'
f'
}
2581 else error
("unexpected close in skipped " ^ tag
) s spos
2584 parse
{ f = toplevel; accu = () } s;
2590 let len = in_channel_length ic
in
2591 let s = String.create
len in
2592 really_input ic
s 0 len;
2595 | Parse_error
(msg
, s, pos
) ->
2596 let subs = subs s pos
in
2597 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2598 failwith
("parse error: " ^
s)
2601 failwith
("config load error: " ^
Printexc.to_string exn
)
2607 let dir = Filename.concat
home ".config" in
2608 if Sys.is_directory
dir then dir else home
2611 Filename.concat
dir "llpp.conf"
2615 if Sys.file_exists
path
2618 (try Some
(open_in_bin
path)
2621 ("Error opening configuation file `" ^
path ^
"': " ^
2622 Printexc.to_string exn
);
2631 ("Error loading configuation from `" ^
path ^
"': " ^
2632 Printexc.to_string exn
);
2638 f (Hashtbl.create
0, defconf)
2643 let pc, pb
, px
, py =
2645 Hashtbl.find
h state.path
2646 with Not_found
-> dc, [], 0, 0.0
2650 state.bookmarks <- pb
;
2652 cbput state.hists
.nav
py;
2657 let add_attrs bb always
dc c =
2660 then Printf.bprintf bb
"\n %s='%b'" s a
2663 then Printf.bprintf bb
"\n %s='%d'" s a
2666 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2670 then dc.winw
, dc.winh
2672 match state.fullscreen
with
2674 | None
-> c.winw
, c.winh
2676 let zoom, presentation, interpagespace, showall
=
2678 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2680 match state.birdseye
with
2681 | Some
(bc
, _
, _
) ->
2682 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2683 | None
-> c.zoom, c.presentation, c.interpagespace, c.showall
2685 oi
"width" w dc.winw
;
2686 oi
"height" h dc.winh
;
2687 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2688 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2689 ob "case-insensitive-search" c.icase
dc.icase
;
2690 ob "preload" c.preload dc.preload;
2691 oi
"page-bias" c.pagebias
dc.pagebias
;
2692 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2693 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2694 ob "crop-hack" c.crophack
dc.crophack
;
2695 ob "throttle" showall
dc.showall
;
2696 ob "highlight-links" c.hlinks
dc.hlinks
;
2697 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2698 oi
"vertical-margin" interpagespace dc.interpagespace;
2699 oz
"zoom" zoom dc.zoom;
2700 ob "presentation" presentation dc.presentation;
2701 oi
"rotation-angle" c.angle
dc.angle
;
2702 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2703 ob "proportional-display" c.proportional
dc.proportional
;
2704 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2705 oi
"texcount" c.texcount
dc.texcount
;
2706 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2710 let bb = Buffer.create
32768 in
2712 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2713 add_attrs bb true dc dc;
2714 Buffer.add_string
bb "/>\n";
2716 let adddoc path x y c bookmarks =
2717 if bookmarks == [] && c = dc && y = 0.0
2720 Printf.bprintf
bb "<doc path='%s'"
2721 (enent
path 0 (String.length
path));
2724 then Printf.bprintf
bb " rely='%f'" y;
2727 then Printf.bprintf
bb " pan='%d'" x;
2729 add_attrs bb false dc c;
2731 begin match bookmarks with
2732 | [] -> Buffer.add_string
bb "/>\n"
2734 Buffer.add_string
bb ">\n<bookmarks>\n";
2735 List.iter
(fun (title, _level
, page, rely) ->
2737 "<item title='%s' page='%d' rely='%f'/>\n"
2738 (enent
title 0 (String.length
title))
2742 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2748 match state.birdseye
with
2749 | Some
(_
, x, _
) -> x
2752 adddoc state.path x (yratio state.y) conf
2753 (if conf.savebmarks
then state.bookmarks else []);
2755 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2756 if path <> state.path
2758 adddoc path x y c bookmarks
2760 Buffer.add_string
bb "</llppconfig>";
2763 if Buffer.length
bb > 0
2766 let tmp = path ^
".tmp" in
2767 let oc = open_out_bin
tmp in
2768 Buffer.output_buffer
oc bb;
2770 Sys.rename
tmp path;
2773 ("error while saving configuration: " ^
Printexc.to_string exn
)
2779 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2780 (fun s -> state.path <- s)
2781 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2784 if String.length
state.path = 0
2785 then (prerr_endline
"filename missing"; exit
1)
2787 if Filename.is_relative
state.path
2788 then Filename.concat
(Sys.getcwd
()) state.path
2796 let _ = Glut.init
Sys.argv
in
2797 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2798 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2799 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2802 if Sys.os_type
= "Unix"
2804 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2806 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2807 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2808 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2809 Unix.bind
sock addr;
2811 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2812 Unix.connect
csock addr;
2813 let ssock, _ = Unix.accept
sock in
2816 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2817 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2821 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2825 let () = Glut.displayFunc
display in
2826 let () = Glut.reshapeFunc
reshape in
2827 let () = Glut.keyboardFunc
keyboard in
2828 let () = Glut.specialFunc
special in
2829 let () = Glut.idleFunc
(Some
idle) in
2830 let () = Glut.mouseFunc
mouse in
2831 let () = Glut.motionFunc
motion in
2832 let () = Glut.passiveMotionFunc
pmotion in
2834 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2835 state.csock <- csock;
2836 state.ssock <- ssock;
2837 state.text <- "Opening " ^
path;
2838 writeopen state.path state.password
;
2842 let rec handlelablglutbug () =
2845 with Glut.BadEnum
"key in special_of_int" ->
2846 showtext '
!'
" LablGlut bug: special key not recognized";
2847 handlelablglutbug ()
2849 handlelablglutbug ();