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
) 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
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 match state.layout with
657 let rely = yratio state.y in
658 state.maxy <- calcheight ();
659 truncate
(float state.maxy *. rely)
662 state.maxy <- calcheight ();
669 GlMat.mode `projection
;
670 GlMat.load_identity
();
671 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
672 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
673 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
677 GlMat.mode `projection
;
678 GlMat.load_identity
();
679 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
680 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
681 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
686 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
690 GlMat.mode `modelview
;
691 GlMat.load_identity
();
692 GlClear.color
(scalecolor 1.0);
693 GlClear.clear
[`color
];
696 wcmd "geometry" [`i
w; `i h
];
700 GlDraw.color
(0.0, 0.0, 0.0);
702 (0.0, float (conf.winh
- 18))
703 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
705 let font = Glut.BITMAP_8_BY_13
in
706 GlDraw.color
(1.0, 1.0, 1.0);
707 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
708 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
709 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
713 let len = String.length
state.text
in
714 match state.textentry
with
716 if len > 0 then showtext ' '
state.text
718 | Some
(c, text
, _
, _
, _
) ->
722 text ^
" [" ^
state.text ^
"]"
732 state.text
<- Printf.sprintf
"%c%s" c s;
733 Glut.postRedisplay
();
747 state.rects
<- state.rects1
;
748 Glut.postRedisplay
()
751 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
752 state.pagecount
<- n;
753 state.invalidated
<- state.invalidated
- 1;
754 if state.invalidated
= 0
758 let s = Scanf.sscanf
cmd "t %n"
759 (fun n -> String.sub
cmd n (String.length
cmd - n))
761 Glut.setWindowTitle
s
764 let s = Scanf.sscanf
cmd "T %n"
765 (fun n -> String.sub
cmd n (String.length
cmd - n))
767 if state.textentry
= None
774 Glut.postRedisplay
();
780 let s = Scanf.sscanf
cmd "V %n"
781 (fun n -> String.sub
cmd n (String.length
cmd - n))
787 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
788 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
789 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
790 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
792 let y = (getpagey pageno) + truncate y0
in
795 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
798 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
799 Scanf.sscanf
cmd "R %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
))
804 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
807 let n, w, h
, r
, l
, s, p
=
808 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
809 (fun n w h r l
s p
->
810 (n-1, w, h
, r
, l
!= 0, s, p
))
813 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
814 state.memused
<- state.memused
+ s;
817 match state.throttle
with
818 | None
-> state.layout
819 | Some
layout -> layout
823 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
826 let evictedopaque = cbpeek state.pagecache
in
827 match findpageforopaque evictedopaque with
828 | None
-> failwith
"bug in gc"
829 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
830 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
832 wcmd "free" [`
s evictedopaque];
833 state.memused
<- state.memused
- evictedsize
;
834 Hashtbl.remove
state.pagemap k
;
835 cbdecr state.pagecache
;
842 cbput state.pagecache p
;
843 state.rendering
<- false;
845 begin match state.throttle
with
847 if pagevisible state.layout n
850 let allvisible = loadlayout state.layout in
851 if allvisible then preload ();
858 let y = getpagey l
.pageno + l
.pagey in
863 let (n, w, h
, x
) as pdim
=
864 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
866 state.pdims
<- pdim
:: state.pdims
869 let (l
, n, t
, h
, pos
) =
870 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
872 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
874 let l = String.length
s in
875 let b = Buffer.create
(String.length
s) in
882 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
885 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
894 let outline = (s, l, n, float t
/. float h
) in
896 match state.outlines with
897 | Olist
outlines -> Olist
(outline :: outlines)
898 | Oarray _
-> Olist
[outline]
899 | Onarrow _
-> Olist
[outline]
901 state.outlines <- outlines
904 log "unknown cmd `%S'" cmd
907 let now = Unix.gettimeofday
;;
911 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
916 let y = state.y + conf.scrollincr
in
917 let y = if y >= state.maxy then 0 else y in
923 let cmd = readcmd state.csock
in
932 let action = function
933 | HCprev
-> cbget cb ~
-1
934 | HCnext
-> cbget cb
1
935 | HCfirst
-> cbget cb ~
-(cb
.rc)
936 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
937 and cancel
() = cb
.rc <- rc
941 let search pattern forward
=
942 if String.length pattern
> 0
945 match state.layout with
948 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
951 let b = makecmd "search"
952 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
954 Buffer.add_char
b '
,'
;
955 Buffer.add_string
b pattern
;
956 Buffer.add_char
b '
\000'
;
959 writecmd state.csock
cmd;
962 let intentry text key
=
963 let c = Char.unsafe_chr key
in
966 let s = "x" in s.[0] <- c;
967 let text = text ^
s in
971 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
976 let b = Buffer.create
(String.length
s + 1) in
977 Buffer.add_string
b s;
982 let textentry text key
=
983 let c = Char.unsafe_chr key
in
985 | _
when key
>= 32 && key
< 127 ->
986 let text = addchar text c in
990 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
994 let reinit angle proportional
=
996 conf.proportional
<- proportional
;
998 wcmd "reinit" [`i angle
; `
b proportional
];
1001 let optentry text key
=
1002 let btos b = if b then "on" else "off" in
1003 let c = Char.unsafe_chr key
in
1007 try conf.scrollincr
<- int_of_string
s with exc
->
1008 state.text <- Printf.sprintf
"bad integer `%s': %s"
1009 s (Printexc.to_string exc
)
1011 TEswitch
('#'
, "", None
, intentry, ondone)
1016 Some
(int_of_string
s)
1018 state.text <- Printf.sprintf
"bad integer `%s': %s"
1019 s (Printexc.to_string exc
);
1022 | Some angle
-> reinit angle
conf.proportional
1025 TEswitch
('^'
, "", None
, intentry, ondone)
1028 conf.icase
<- not
conf.icase
;
1029 TEdone
("case insensitive search " ^
(btos conf.icase
))
1032 conf.preload <- not
conf.preload;
1034 TEdone
("preload " ^
(btos conf.preload))
1037 conf.verbose
<- not
conf.verbose
;
1038 TEdone
("verbose " ^
(btos conf.verbose
))
1041 conf.maxhfit
<- not
conf.maxhfit
;
1042 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1043 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1046 conf.crophack
<- not
conf.crophack
;
1047 TEdone
("crophack " ^
btos conf.crophack
)
1050 conf.showall
<- not
conf.showall
;
1051 TEdone
("showall " ^
btos conf.showall
)
1054 conf.underinfo
<- not
conf.underinfo
;
1055 TEdone
("underinfo " ^
btos conf.underinfo
)
1058 conf.savebmarks
<- not
conf.savebmarks
;
1059 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1065 match state.layout with
1070 conf.interpagespace <- int_of_string
s;
1071 state.maxy <- calcheight ();
1072 let y = getpagey pageno in
1075 state.text <- Printf.sprintf
"bad integer `%s': %s"
1076 s (Printexc.to_string exc
)
1078 TEswitch
('
%'
, "", None
, intentry, ondone)
1081 reinit conf.angle
(not
conf.proportional
);
1082 TEdone
("proprortional display " ^
btos conf.proportional
)
1085 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1089 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1091 let enterselector allowdel
outlines errmsg msg
=
1092 if Array.length
outlines = 0
1094 showtext ' ' errmsg
;
1098 Glut.setCursor
Glut.CURSOR_INHERIT
;
1100 match state.layout with
1102 | {pageno=pageno} :: rest
-> pageno
1106 if n = Array.length
outlines
1109 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1110 if outlinepageno
>= pageno then n else loop (n+1)
1115 Some
(allowdel
, active,
1116 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1117 Glut.postRedisplay
();
1121 let enteroutlinemode () =
1123 match state.outlines with
1126 let a = Array.of_list
(List.rev
l) in
1127 state.outlines <- Oarray
a;
1129 | Onarrow
(pat
, a, b) ->
1130 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1132 enterselector false outlines "Document has no outline" msg
;
1135 let enterbookmarkmode () =
1136 let bookmarks = Array.of_list
state.bookmarks in
1137 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1140 let quickbookmark ?title
() =
1141 match state.layout with
1147 let sec = Unix.gettimeofday
() in
1148 let tm = Unix.localtime
sec in
1149 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1153 (tm.Unix.tm_year
+ 1900)
1156 | Some
title -> title
1159 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1163 state.fullscreen
<- None
;
1164 Glut.reshapeWindow
w h
;
1167 let writeopen path password
=
1168 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1171 let opendoc path password
=
1174 state.password
<- password
;
1175 state.gen
<- state.gen
+ 1;
1177 writeopen path password
;
1178 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1179 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1182 let birdseyeoff (c, leftx
) =
1183 state.birdseye
<- None
;
1184 conf.zoom
<- c.zoom
;
1185 conf.presentation <- c.presentation;
1186 conf.interpagespace <- c.interpagespace;
1187 conf.showall
<- c.showall
;
1188 conf.hlinks
<- c.hlinks
;
1190 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1194 let viewkeyboard ~key ~x ~
y =
1196 state.textentry <- te
;
1199 Glut.postRedisplay
()
1201 match state.textentry with
1203 let c = Char.chr key
in
1209 let y = getnav () in
1210 gotoy_and_clear_text y
1213 begin match state.birdseye
with
1216 let y = getpagey state.birdseyepageno
in
1219 reshape conf.winw
conf.winh
;
1228 Glut.postRedisplay
()
1231 let ondone isforw
s =
1232 cbput state.hists
.pat
s;
1233 state.searchpattern
<- s;
1236 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1237 textentry, ondone (c ='
/'
)))
1239 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1240 let incr = if conf.zoom
+. 0.01 > 0.1 then 0.1 else 0.01 in
1241 conf.zoom
<- min
2.2 (conf.zoom
+. incr);
1242 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1243 reshape conf.winw
conf.winh
1248 try int_of_string
s with exc
->
1249 state.text <- Printf.sprintf
"bad integer `%s': %s"
1250 s (Printexc.to_string exc
);
1256 state.text <- "page bias is now " ^ string_of_int
n;
1259 enttext (Some
('
+'
, "", None
, intentry, ondone))
1261 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1262 let decr = if conf.zoom
-. 0.1 < 0.1 then 0.01 else 0.1 in
1263 conf.zoom
<- max
0.01 (conf.zoom
-. decr);
1264 if conf.zoom
<= 1.0 then state.x
<- 0;
1265 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1266 reshape conf.winw
conf.winh
;
1272 enttext (Some
('
-'
, "", None
, optentry, ondone))
1274 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1277 state.text <- "zoom is 100%";
1278 reshape conf.winw
conf.winh
1280 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1281 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1286 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1287 reshape conf.winw
conf.winh
;
1290 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1291 begin match state.birdseye
with
1293 let zoom = 76.0 /. float state.w in
1294 state.birdseye
<- Some
({ conf with zoom = conf.zoom }, state.x
);
1296 conf.presentation <- false;
1297 conf.interpagespace <- 10;
1298 conf.hlinks
<- false;
1300 state.mstate
<- Mnone
;
1301 conf.showall
<- false;
1302 Glut.setCursor
Glut.CURSOR_INHERIT
;
1303 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1309 reshape conf.winw
conf.winh
1314 try int_of_string
s with exc
->
1315 state.text <- Printf.sprintf
"bad integer `%s': %s"
1316 s (Printexc.to_string exc
);
1322 cbput state.hists
.pag
(string_of_int
n);
1323 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1326 let pageentry text key
=
1327 match Char.unsafe_chr key
with
1328 | 'g'
-> TEdone
text
1329 | _
-> intentry text key
1331 let text = "x" in text.[0] <- c;
1332 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1336 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1337 reshape conf.winw
conf.winh
;
1340 conf.hlinks
<- not
conf.hlinks
;
1341 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1342 Glut.postRedisplay
()
1345 conf.autoscroll
<- not
conf.autoscroll
1348 conf.presentation <- not
conf.presentation;
1349 showtext ' '
("presentation mode " ^
1350 if conf.presentation then "on" else "off");
1354 begin match state.fullscreen
with
1356 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1359 state.fullscreen
<- None
;
1364 gotoy_and_clear_text 0
1367 search state.searchpattern
true
1370 search state.searchpattern
false
1373 begin match state.layout with
1376 gotoy_and_clear_text (getpagey l.pageno)
1380 begin match List.rev
state.layout with
1383 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1384 gotoy_and_clear_text (getpagey pageno)
1388 begin match state.layout with
1391 let pageno = max
0 (l.pageno-1) in
1392 gotoy_and_clear_text (getpagey pageno)
1397 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1399 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1401 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1405 else (100. *. (float state.y /. float maxy)) in
1408 Printf.sprintf
"Page %d of %d %.2f%%"
1409 (fn+1) state.pagecount
percent
1412 "Pages %d-%d of %d %.2f%%"
1413 (fn+1) (ln
+1) state.pagecount
percent
1418 begin match state.layout with
1421 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1422 Glut.postRedisplay
();
1426 enterbookmarkmode ()
1430 match state.layout with
1433 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1437 enttext (Some
('~'
, "", None
, textentry, ondone))
1441 showtext ' '
"Quick bookmark added";
1444 begin match state.layout with
1446 let rect = getpdimrect
l.pagedimno
in
1450 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1451 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1453 (truncate
(rect.(1) -. rect.(0)),
1454 truncate
(rect.(3) -. rect.(0)))
1458 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1460 Glut.postRedisplay
();
1466 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1471 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1472 Glut.postRedisplay
()
1474 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1475 | 'j'
-> gotoy (clamp conf.scrollincr
)
1477 | '
r'
-> opendoc state.path
state.password
1480 vlog "huh? %d %c" key
(Char.chr key
);
1483 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1484 let len = String.length
text in
1487 state.textentry <- None
;
1488 Glut.postRedisplay
();
1491 let s = String.sub
text 0 (len - 1) in
1492 enttext (Some
(c, s, opthist
, onkey
, ondone))
1495 | Some
(c, text, onhist, onkey
, ondone) ->
1496 begin match Char.unsafe_chr key
with
1499 state.textentry <- None
;
1500 Glut.postRedisplay
()
1503 begin match onhist with
1505 | Some
(_
, onhistcancel
) -> onhistcancel
()
1507 state.textentry <- None
;
1508 Glut.postRedisplay
()
1511 begin match onkey
text key
with
1513 state.textentry <- None
;
1515 Glut.postRedisplay
()
1518 enttext (Some
(c, text, onhist, onkey
, ondone));
1521 state.textentry <- None
;
1522 Glut.postRedisplay
()
1525 state.textentry <- Some te
;
1526 Glut.postRedisplay
()
1531 let narrow outlines pattern
=
1532 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1536 let rec fold accu n =
1540 let (s, _
, _
, _
) as o
= outlines.(n) in
1542 if (try ignore
(Str.search_forward re
s 0); true
1543 with Not_found
-> false)
1549 let matched = fold [] (Array.length
outlines - 1) in
1550 if matched = [] then None
else Some
(Array.of_list
matched)
1553 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1554 let search active pattern
incr =
1557 if n = Array.length
outlines || n = -1
1560 let (s, _
, _
, _
) = outlines.(n) in
1562 (try ignore
(Str.search_forward re
s 0); true
1563 with Not_found
-> false)
1565 else loop (n + incr)
1570 let re = Str.regexp_case_fold pattern
in
1576 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1579 if String.length qsearch
= 0
1582 state.outline <- None
;
1583 Glut.postRedisplay
();
1587 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1588 Glut.postRedisplay
();
1592 let incr = if key
= 18 then -1 else 1 in
1594 match search (active + incr) qsearch
incr with
1596 state.text <- qsearch ^
" [not found]";
1599 state.text <- qsearch
;
1600 active, firstof active
1602 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1603 Glut.postRedisplay
();
1606 let len = String.length qsearch
in
1613 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1616 let qsearch = String.sub
qsearch 0 (len - 1) in
1618 match search active qsearch ~
-1 with
1620 state.text <- qsearch ^
" [not found]";
1623 state.text <- qsearch;
1624 active, firstof active
1626 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1628 Glut.postRedisplay
()
1631 if active < Array.length
outlines
1633 let (_
, _
, n, t
) = outlines.(active) in
1637 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1638 state.outline <- None
;
1639 Glut.postRedisplay
();
1641 | _
when key
>= 32 && key
< 127 ->
1642 let pattern = addchar qsearch (Char.chr key
) in
1644 match search active pattern 1 with
1646 state.text <- pattern ^
" [not found]";
1649 state.text <- pattern;
1650 active, firstof active
1652 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1653 Glut.postRedisplay
()
1655 | 14 when not allowdel
-> (* ctrl-n *)
1656 if String.length
qsearch > 0
1658 let optoutlines = narrow outlines qsearch in
1659 begin match optoutlines with
1660 | None
-> state.text <- "can't narrow"
1662 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1663 match state.outlines with
1666 state.outlines <- Onarrow
(qsearch, outlines, a)
1667 | Onarrow
(pat
, a, b) ->
1668 state.outlines <- Onarrow
(qsearch, outlines, b)
1671 Glut.postRedisplay
()
1673 | 21 when not allowdel
-> (* ctrl-u *)
1675 match state.outlines with
1678 let a = Array.of_list
(List.rev
l) in
1679 state.outlines <- Oarray
a;
1681 | Onarrow
(pat
, a, b) ->
1682 state.outlines <- Oarray
b;
1686 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1687 Glut.postRedisplay
()
1691 Some
(allowdel
, active, firstof active, outlines, qsearch);
1692 Glut.postRedisplay
()
1694 | 127 when allowdel
->
1695 let len = Array.length
outlines - 1 in
1698 state.outline <- None
;
1699 state.bookmarks <- [];
1702 let bookmarks = Array.init
len
1704 let i = if i >= active then i + 1 else i in
1715 Glut.postRedisplay
()
1717 | _
-> log "unknown key %d" key
1720 let keyboard ~key ~x ~
y =
1725 match state.outline with
1726 | None
-> viewkeyboard ~key ~x ~
y
1727 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1730 let special ~key ~x ~
y =
1731 match state.outline with
1732 | None
when state.birdseye
<> None
->
1733 begin match key
with
1735 let pageno = max
0 (state.birdseyepageno
- 1) in
1736 state.birdseyepageno
<- pageno;
1737 if not
(pagevisible state.layout pageno)
1738 then gotopage pageno 0.0
1739 else Glut.postRedisplay
();
1742 let pageno = min
(state.pagecount
- 1) (state.birdseyepageno
+ 1) in
1743 state.birdseyepageno
<- pageno;
1744 if not
(pagevisible state.layout pageno)
1746 begin match List.rev
state.layout with
1747 | [] -> gotopage pageno 0.0
1749 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1751 else Glut.postRedisplay
();
1753 | Glut.KEY_PAGE_UP
->
1754 begin match state.layout with
1756 if l.pageno = state.birdseyepageno
1758 match layout (state.y - conf.winh
) conf.winh
with
1759 | [] -> gotoy (clamp (-conf.winh
))
1761 state.birdseyepageno
<- max
0 (l.pageno - 1);
1762 gotopage state.birdseyepageno
0.0
1765 state.birdseyepageno
<- max
0 (l.pageno - 1);
1766 gotopage state.birdseyepageno
0.0
1768 | [] -> gotoy (clamp (-conf.winh
))
1770 | Glut.KEY_PAGE_DOWN
->
1771 begin match List.rev
state.layout with
1773 state.birdseyepageno
<- min
(state.pagecount
- 1) (l.pageno + 1);
1774 gotoy (clamp (l.pagedispy
+ conf.interpagespace + l.pageh
))
1775 | [] -> gotoy (clamp conf.winh
)
1779 state.birdseyepageno
<- 0;
1782 state.birdseyepageno
<- state.pagecount
- 1;
1783 if not
(pagevisible state.layout state.birdseyepageno
)
1785 gotopage state.birdseyepageno
0.0
1787 Glut.postRedisplay
()
1793 begin match state.textentry with
1797 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1798 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1799 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1800 | Glut.KEY_PAGE_UP
->
1801 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1803 match state.layout with
1805 | l :: _
-> state.y - l.pagey
1808 | Glut.KEY_PAGE_DOWN
->
1809 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1811 match List.rev
state.layout with
1813 | l :: _
-> getpagey l.pageno
1816 | Glut.KEY_HOME
-> addnav (); 0
1819 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1821 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1822 state.x
<- state.x
- 10;
1824 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1825 state.x
<- state.x
+ 10;
1830 gotoy_and_clear_text y
1832 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1835 | Glut.KEY_UP
-> action HCprev
1836 | Glut.KEY_DOWN
-> action HCnext
1837 | Glut.KEY_HOME
-> action HCfirst
1838 | Glut.KEY_END
-> action HClast
1841 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1842 Glut.postRedisplay
()
1847 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1848 let maxrows = maxoutlinerows () in
1849 let calcfirst first
active =
1852 let rows = active - first
in
1853 if rows > maxrows then active - maxrows else first
1857 let active = active + incr in
1858 let active = max
0 (min
active (Array.length
outlines - 1)) in
1859 let first = calcfirst first active in
1860 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1861 Glut.postRedisplay
()
1863 let updownlevel incr =
1864 let len = Array.length
outlines in
1865 let (_
, curlevel
, _
, _
) = outlines.(active) in
1867 if i = len then i-1 else if i = -1 then 0 else
1868 let (_
, l, _
, _
) = outlines.(i) in
1869 if l != curlevel
then i else flow (i+incr)
1871 let active = flow active in
1872 let first = calcfirst first active in
1873 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1874 Glut.postRedisplay
()
1877 | Glut.KEY_UP
-> navigate ~
-1
1878 | Glut.KEY_DOWN
-> navigate 1
1879 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1880 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1882 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1883 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1886 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1887 Glut.postRedisplay
()
1890 let active = Array.length
outlines - 1 in
1891 let first = max
0 (active - maxrows) in
1892 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1893 Glut.postRedisplay
()
1898 let drawplaceholder l =
1899 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1900 GlDraw.color
(scalecolor 1.0);
1902 (float l.pagex
, float l.pagedispy
)
1903 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1905 let x = float (if margin < 0 then -margin else 0)
1906 and y = float (l.pagedispy
+ 13) in
1907 let font = Glut.BITMAP_8_BY_13
in
1908 GlDraw.color
(0.0, 0.0, 0.0);
1909 GlPix.raster_pos ~
x ~
y ();
1910 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1911 ("Loading " ^ string_of_int
(l.pageno + 1));
1914 let now () = Unix.gettimeofday
();;
1917 begin match getopaque l.pageno with
1918 | Some
(opaque, _
) when validopaque opaque ->
1919 if state.textentry = None
1920 then GlDraw.color
(scalecolor 1.0)
1921 else GlDraw.color
(scalecolor 0.4);
1923 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1927 vlog "draw %d %f sec" l.pageno d;
1932 if state.birdseye
<> None
&& state.birdseyepageno
= l.pageno
1934 GlDraw.polygon_mode `both `line
;
1935 GlDraw.line_width
4.0;
1936 GlDraw.color
(0.8, 0.0, 0.0);
1938 (float (l.pagex
- 1), float (l.pagedispy
- 1))
1939 (float (l.pagew
+ l.pagex
+ 1), float (l.pagedispy
+ l.pagevh + 1))
1941 GlDraw.line_width
1.0;
1942 GlDraw.polygon_mode `both `fill
;
1947 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1948 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1949 let sh = float conf.winh
/. sh in
1950 let sh = max
sh (float conf.scrollh
) in
1953 if state.y = state.maxy
1955 else float y /. float maxy
1957 let position = (float conf.winh
-. sh) *. percent in
1960 if position +. sh > float conf.winh
1961 then float conf.winh
-. sh
1967 let scrollindicator () =
1968 GlDraw.color
(0.64 , 0.64, 0.64);
1970 (float (conf.winw
- conf.scrollw
), 0.)
1971 (float conf.winw
, float conf.winh
)
1973 GlDraw.color
(0.0, 0.0, 0.0);
1975 let position, sh = scrollph state.y in
1977 (float (conf.winw
- conf.scrollw
), position)
1978 (float conf.winw
, position +. sh)
1982 let showsel margin =
1983 match state.mstate
with
1984 | Mnone
| Mscroll _
| Mpan _
->
1987 | Msel
((x0
, y0
), (x1
, y1
)) ->
1988 let rec loop = function
1990 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1991 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1993 match getopaque l.pageno with
1994 | Some
(opaque, _
) when validopaque opaque ->
1995 let oy = -l.pagey + l.pagedispy
in
1997 (x0
- margin - state.x, y0
,
1998 x1
- margin - state.x, y1
) oy;
2008 let panx = float state.x in
2010 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
2011 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2013 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2015 if l.pageno = pageno
2017 let d = float (l.pagedispy
- l.pagey) in
2018 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2019 GlDraw.begins `quads
;
2021 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2022 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2023 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2024 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2034 let showoutline = function
2036 | Some
(allowdel
, active, first, outlines, qsearch) ->
2038 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2039 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
2040 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2043 GlDraw.color
(1., 1., 1.);
2044 let font = Glut.BITMAP_9_BY_15
in
2045 let draw_string x y s =
2046 GlPix.raster_pos ~
x ~
y ();
2047 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2050 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2053 let (s, l, _
, _
) = outlines.(row
) in
2054 let y = (row
- first) * 16 in
2059 GlDraw.polygon_mode `both `line
;
2060 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2061 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
2062 GlDraw.rect (0., float (y + 1))
2063 (float (conf.winw
- 1), float (y + 18));
2064 GlDraw.polygon_mode `both `fill
;
2066 GlDraw.color
(1., 1., 1.);
2068 draw_string (float x) (float (y + 16)) s;
2076 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2077 GlDraw.viewport
margin 0 state.w conf.winh
;
2079 if state.birdseye
<> None
2081 GlClear.color
(0.5, 0.5, 0.55)
2083 GlClear.color
(scalecolor 0.5)
2085 GlClear.clear
[`color
];
2088 let x = float state.x in
2089 GlMat.translate ~
x ();
2093 Gl.enable `scissor_test
;
2094 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2096 List.iter
drawpage state.layout;
2099 Gl.disable `scissor_test
2103 let x = -.float state.x in
2104 GlMat.translate ~
x ();
2108 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2111 showoutline state.outline;
2113 Glut.swapBuffers
();
2117 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2118 let x = x - margin - state.x in
2119 let rec f = function
2121 begin match getopaque l.pageno with
2122 | Some
(opaque, _
) when validopaque opaque ->
2123 let y = y - l.pagedispy
in
2126 let y = l.pagey + y in
2127 let x = x - l.pagex
in
2128 match whatsunder
opaque x y with
2141 let mouse ~button ~bstate ~
x ~
y =
2143 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2151 let incr = incr * 2 in
2152 let y = clamp incr in
2153 gotoy_and_clear_text y
2155 | Glut.LEFT_BUTTON
when state.outline = None
2156 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2157 if bstate
= Glut.DOWN
2159 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2160 state.mstate
<- Mpan
(x, y)
2163 state.mstate
<- Mnone
2166 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2167 if bstate
= Glut.DOWN
2169 let position, sh = scrollph state.y in
2170 if y > truncate
position && y < truncate
(position +. sh)
2172 state.mstate
<- Mscroll
2174 let percent = float y /. float conf.winh
in
2175 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2177 state.mstate
<- Mscroll
2179 state.mstate
<- Mnone
2181 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2182 begin match state.birdseye
with
2184 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2185 let rec loop = function
2188 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2189 && x > margin && x < margin + l.pagew
2191 let y = getpagey l.pageno in
2194 reshape conf.winw
conf.winh
;
2199 | None
-> () (* impossible *)
2202 | Glut.LEFT_BUTTON
when state.outline = None
->
2203 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2204 begin match dest with
2205 | Ulinkgoto
(pageno, top
) ->
2208 gotopage1 pageno top
2213 | Unone
when bstate
= Glut.DOWN
->
2214 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2215 state.mstate
<- Mpan
(x, y);
2217 | Unone
| Utext _
->
2218 if bstate
= Glut.DOWN
2220 if conf.angle
mod 360 = 0
2222 state.mstate
<- Msel
((x, y), (x, y));
2223 Glut.postRedisplay
()
2227 match state.mstate
with
2231 state.mstate
<- Mnone
2234 Glut.setCursor
Glut.CURSOR_INHERIT
;
2235 state.mstate
<- Mnone
2237 | Msel
((x0
, y0
), (x1
, y1
)) ->
2239 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2240 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2242 match getopaque l.pageno with
2243 | Some
(opaque, _
) when validopaque opaque ->
2247 List.iter
f state.layout;
2248 copysel
""; (* ugly *)
2249 Glut.setCursor
Glut.CURSOR_INHERIT
;
2250 state.mstate
<- Mnone
;
2257 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2260 if state.outline = None
2262 match state.mstate
with
2268 state.mstate
<- Mpan
(x, y);
2269 if conf.zoom > 1.0 then state.x <- state.x + dx;
2271 gotoy_and_clear_text y
2274 state.mstate
<- Msel
(a, (x, y));
2275 Glut.postRedisplay
()
2278 let y = min
conf.winh
(max
0 y) in
2279 let percent = float y /. float conf.winh
in
2280 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2281 gotoy_and_clear_text y
2285 if state.outline = None
&& state.birdseye
= None
2287 match state.mstate
with
2289 begin match getunder x y with
2290 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2292 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2293 Glut.setCursor
Glut.CURSOR_INFO
2294 | Ulinkgoto
(page
, y) ->
2295 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2296 Glut.setCursor
Glut.CURSOR_INFO
2298 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2299 Glut.setCursor
Glut.CURSOR_TEXT
2302 | Mpan _
| Msel _
| Mscroll
->
2312 match Sys.os_type
with
2313 | "Win32" -> Sys.getenv
"HOMEPATH"
2314 | _
-> Sys.getenv
"HOME"
2317 ("Can not determine home directory location: " ^
2318 Printexc.to_string exn
);
2322 let config_of c attrs
=
2326 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2327 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2328 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2329 | "preload" -> { c with preload = bool_of_string v
}
2330 | "page-bias" -> { c with pagebias
= int_of_string v
}
2331 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2332 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2333 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2334 | "throttle" -> { c with showall
= bool_of_string v
}
2335 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2336 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2337 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2339 let zoom = float_of_string v
/. 100. in
2340 let zoom = max
0.01 (min
2.2 zoom) in
2341 { c with zoom = zoom }
2342 | "presentation" -> { c with presentation = bool_of_string v
}
2343 | "rotation-angle" -> { c with angle
= int_of_string v
}
2344 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2345 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2346 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2347 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2348 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2349 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2350 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2353 prerr_endline
("Error processing attribute (`" ^
2354 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2357 let rec fold c = function
2360 let c = apply c k v
in
2366 let bookmark_of attrs
=
2367 let rec fold title page
rely = function
2368 | ("title", v
) :: rest
-> fold v page
rely rest
2369 | ("page", v
) :: rest
-> fold title v
rely rest
2370 | ("rely", v
) :: rest
-> fold title page v rest
2371 | _
:: rest
-> fold title page
rely rest
2372 | [] -> title, page
, rely
2374 fold "invalid" "0" "0" attrs
2377 let setconf dst src
=
2378 dst
.scrollw
<- src
.scrollw
;
2379 dst
.scrollh
<- src
.scrollh
;
2380 dst
.icase
<- src
.icase
;
2381 dst
.preload <- src
.preload;
2382 dst
.pagebias
<- src
.pagebias
;
2383 dst
.verbose
<- src
.verbose
;
2384 dst
.scrollincr
<- src
.scrollincr
;
2385 dst
.maxhfit
<- src
.maxhfit
;
2386 dst
.crophack
<- src
.crophack
;
2387 dst
.autoscroll
<- src
.autoscroll
;
2388 dst
.showall
<- src
.showall
;
2389 dst
.hlinks
<- src
.hlinks
;
2390 dst
.underinfo
<- src
.underinfo
;
2391 dst
.interpagespace <- src
.interpagespace;
2392 dst
.zoom <- src
.zoom;
2393 dst
.presentation <- src
.presentation;
2394 dst
.angle
<- src
.angle
;
2395 dst
.winw
<- src
.winw
;
2396 dst
.winh
<- src
.winh
;
2397 dst
.savebmarks
<- src
.savebmarks
;
2398 dst
.memlimit
<- src
.memlimit
;
2399 dst
.proportional
<- src
.proportional
;
2400 dst
.texcount
<- src
.texcount
;
2401 dst
.sliceheight
<- src
.sliceheight
;
2405 let l = String.length
s in
2406 let b = Buffer.create
l in
2412 let h = Hashtbl.create
10 in
2413 let dc = { defconf with angle
= defconf.angle
} in
2414 let rec toplevel v t spos epos
=
2416 | Vdata
| Vcdata
| Vend
-> v
2417 | Vopen
("llppconfig", attrs
, closed
) ->
2420 else { v
with f = llppconfig
}
2422 error
"unexpected subelement at top level" s spos
2423 | Vclose tag
-> error
"unexpected close at top level" s spos
2425 and llppconfig v t spos epos
=
2427 | Vdata
| Vcdata
| Vend
-> v
2428 | Vopen
("defaults", attrs
, closed
) ->
2429 let c = config_of dc attrs
in
2433 else { v
with f = skip
"defaults" (fun () -> v
) }
2435 | Vopen
("doc", attrs
, closed
) ->
2438 List.assoc
"path" attrs
2439 with Not_found
-> error
"doc is missing path attribute" s spos
2441 let path = unent pathent in
2442 let c = config_of dc attrs
in
2445 float_of_string
(List.assoc
"rely" attrs
)
2449 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2454 int_of_string
(List.assoc
"pan" attrs
)
2458 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2462 then (Hashtbl.add
h path (c, [], x, y); v
)
2463 else { v
with f = doc
path x y c [] }
2465 | Vopen
(tag
, _
, closed
) ->
2466 error
"unexpected subelement in llppconfig" s spos
2468 | Vclose
"llppconfig" -> { v
with f = toplevel }
2469 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2471 and doc
path x y c bookmarks v t spos epos
=
2473 | Vdata
| Vcdata
-> v
2474 | Vend
-> error
"unexpected end of input in doc" s spos
2475 | Vopen
("bookmarks", attrs
, closed
) ->
2476 { v
with f = pbookmarks
path x y c bookmarks }
2478 | Vopen
(tag
, _
, _
) ->
2479 error
"unexpected subelement in doc" s spos
2482 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2483 { v
with f = llppconfig
}
2485 | Vclose tag
-> error
"unexpected close in doc" s spos
2487 and pbookmarks
path x y c bookmarks v t spos epos
=
2489 | Vdata
| Vcdata
-> v
2490 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2491 | Vopen
("item", attrs
, closed
) ->
2492 let titleent, spage
, srely
= bookmark_of attrs
in
2497 dolog "Failed to convert page %S to integer: %s"
2498 spage
(Printexc.to_string exn
);
2503 float_of_string srely
2505 dolog "Failed to convert rely %S to real: %s"
2506 srely
(Printexc.to_string exn
);
2509 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2511 then { v
with f = pbookmarks
path x y c bookmarks }
2514 { v
with f = skip
"item" f }
2517 error
"unexpected subelement in bookmarks" s spos
2519 | Vclose
"bookmarks" ->
2520 { v
with f = doc
path x y c bookmarks }
2522 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2524 and skip tag
f v t spos epos
=
2526 | Vdata
| Vcdata
-> v
2528 error
("unexpected end of input in skipped " ^ tag
) s spos
2529 | Vopen
(tag'
, _
, closed
) ->
2533 let f'
() = { v
with f = skip tag
f } in
2534 { v
with f = skip tag'
f'
}
2538 else error
("unexpected close in skipped " ^ tag
) s spos
2541 parse
{ f = toplevel; accu = () } s;
2547 let len = in_channel_length ic
in
2548 let s = String.create
len in
2549 really_input ic
s 0 len;
2552 | Parse_error
(msg
, s, pos
) ->
2553 let subs = subs s pos
in
2554 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2555 failwith
("parse error: " ^
s)
2558 failwith
("config load error: " ^
Printexc.to_string exn
)
2564 let dir = Filename.concat
home ".config" in
2565 if Sys.is_directory
dir then dir else home
2568 Filename.concat
dir "llpp.conf"
2572 if Sys.file_exists
path
2575 (try Some
(open_in_bin
path)
2578 ("Error opening configuation file `" ^
path ^
"': " ^
2579 Printexc.to_string exn
);
2588 ("Error loading configuation from `" ^
path ^
"': " ^
2589 Printexc.to_string exn
);
2595 f (Hashtbl.create
0, defconf)
2600 let pc, pb
, px
, py =
2602 Hashtbl.find
h state.path
2603 with Not_found
-> dc, [], 0, 0.0
2607 state.bookmarks <- pb
;
2609 cbput state.hists
.nav
py;
2614 let add_attrs bb always
dc c =
2617 then Printf.bprintf bb
"\n %s='%b'" s a
2620 then Printf.bprintf bb
"\n %s='%d'" s a
2623 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2627 then dc.winw
, dc.winh
2629 match state.fullscreen
with
2631 | None
-> c.winw
, c.winh
2633 let zoom, presentation, interpagespace, showall
=
2635 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2637 match state.birdseye
with
2639 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2640 | None
-> c.zoom, c.presentation, c.interpagespace, c.showall
2642 oi
"width" w dc.winw
;
2643 oi
"height" h dc.winh
;
2644 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2645 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2646 ob "case-insensitive-search" c.icase
dc.icase
;
2647 ob "preload" c.preload dc.preload;
2648 oi
"page-bias" c.pagebias
dc.pagebias
;
2649 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2650 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2651 ob "crop-hack" c.crophack
dc.crophack
;
2652 ob "throttle" showall
dc.showall
;
2653 ob "highlight-links" c.hlinks
dc.hlinks
;
2654 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2655 oi
"vertical-margin" interpagespace dc.interpagespace;
2656 oz
"zoom" zoom dc.zoom;
2657 ob "presentation" presentation dc.presentation;
2658 oi
"rotation-angle" c.angle
dc.angle
;
2659 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2660 ob "proportional-display" c.proportional
dc.proportional
;
2661 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2662 oi
"texcount" c.texcount
dc.texcount
;
2663 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2667 let bb = Buffer.create
32768 in
2669 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2670 add_attrs bb true dc dc;
2671 Buffer.add_string
bb "/>\n";
2673 let adddoc path x y c bookmarks =
2674 if bookmarks == [] && c = dc && y = 0.0
2677 Printf.bprintf
bb "<doc path='%s'"
2678 (enent
path 0 (String.length
path));
2681 then Printf.bprintf
bb " rely='%f'" y;
2684 then Printf.bprintf
bb " pan='%d'" x;
2686 add_attrs bb false dc c;
2688 begin match bookmarks with
2689 | [] -> Buffer.add_string
bb "/>\n"
2691 Buffer.add_string
bb ">\n<bookmarks>\n";
2692 List.iter
(fun (title, _level
, page, rely) ->
2694 "<item title='%s' page='%d' rely='%f'/>\n"
2695 (enent
title 0 (String.length
title))
2699 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2705 match state.birdseye
with
2709 adddoc state.path x (yratio state.y) conf
2710 (if conf.savebmarks
then state.bookmarks else []);
2712 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2713 if path <> state.path
2715 adddoc path x y c bookmarks
2717 Buffer.add_string
bb "</llppconfig>";
2720 if Buffer.length
bb > 0
2723 let tmp = path ^
".tmp" in
2724 let oc = open_out_bin
tmp in
2725 Buffer.output_buffer
oc bb;
2727 Sys.rename
tmp path;
2730 ("error while saving configuration: " ^
Printexc.to_string exn
)
2736 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2737 (fun s -> state.path <- s)
2738 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2741 if String.length
state.path = 0
2742 then (prerr_endline
"filename missing"; exit
1)
2744 if Filename.is_relative
state.path
2745 then Filename.concat
(Sys.getcwd
()) state.path
2753 let _ = Glut.init
Sys.argv
in
2754 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2755 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2756 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2759 if Sys.os_type
= "Unix"
2761 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2763 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2764 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2765 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2766 Unix.bind
sock addr;
2768 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2769 Unix.connect
csock addr;
2770 let ssock, _ = Unix.accept
sock in
2773 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2774 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2778 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2782 let () = Glut.displayFunc
display in
2783 let () = Glut.reshapeFunc
reshape in
2784 let () = Glut.keyboardFunc
keyboard in
2785 let () = Glut.specialFunc
special in
2786 let () = Glut.idleFunc
(Some
idle) in
2787 let () = Glut.mouseFunc
mouse in
2788 let () = Glut.motionFunc
motion in
2789 let () = Glut.passiveMotionFunc
pmotion in
2791 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2792 state.csock <- csock;
2793 state.ssock <- ssock;
2794 state.text <- "Opening " ^
path;
2795 writeopen state.path state.password
;
2799 let rec handlelablglutbug () =
2802 with Glut.BadEnum
"key in special_of_int" ->
2803 showtext '
!'
" LablGlut bug: special key not recognized";
2804 handlelablglutbug ()
2806 handlelablglutbug ();