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
177 ; mutable layout
: layout list
178 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
179 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
180 ; mutable pagecount
: int
181 ; pagecache
: string circbuf
182 ; mutable rendering
: bool
183 ; mutable mstate
: mstate
184 ; mutable searchpattern
: string
185 ; mutable rects
: (pageno
* recttype
* rect
) list
186 ; mutable rects1
: (pageno
* recttype
* rect
) list
187 ; mutable text
: string
188 ; mutable fullscreen
: (width
* height
) option
189 ; mutable birdseye
: (conf
* leftx
) option
190 ; mutable textentry
: textentry
option
191 ; mutable outlines
: outlines
192 ; mutable outline
: (bool * int * int * outline array
* string) option
193 ; mutable bookmarks
: outline list
194 ; mutable path
: string
195 ; mutable password
: string
196 ; mutable invalidated
: int
197 ; mutable colorscale
: float
198 ; mutable memused
: int
199 ; mutable birdseyepageno
: pageno
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
};;
249 ; pagemap
= Hashtbl.create
10
250 ; pagecache
= cbnew 100 ""
262 ; outlines
= Olist
[]
269 { nav
= cbnew 100 0.0
283 Printf.kprintf prerr_endline fmt
285 Printf.kprintf ignore fmt
289 let len = String.length s
in
291 let b = Buffer.create
n in
292 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
293 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
294 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
295 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
296 Buffer.add_string
b s
;
297 let s'
= Buffer.contents
b in
298 let n'
= Unix.write fd
s'
0 n in
299 if n'
!= n then failwith
"write failed";
304 let n = Unix.read fd
s 0 4 in
305 if n != 4 then failwith
"incomplete read(len)";
307 lor (Char.code
s.[0] lsl 24)
308 lor (Char.code
s.[1] lsl 16)
309 lor (Char.code
s.[2] lsl 8)
310 lor (Char.code
s.[3] lsl 0)
312 let s = String.create
len in
313 let n = Unix.read fd
s 0 len in
314 if n != len then failwith
"incomplete read(data)";
321 else float y
/. float state.maxy
325 let b = Buffer.create
10 in
326 Buffer.add_string
b s;
327 let rec combine = function
330 Buffer.add_char
b ' '
;
333 | `
b b -> if b then "1" else "0"
335 | `i i
-> string_of_int i
336 | `f f
-> string_of_float f
337 | `I f
-> string_of_int
(truncate f
)
339 Buffer.add_string
b s;
346 let cmd = Buffer.contents
(makecmd s l
) in
347 writecmd state.csock
cmd;
353 let d = conf.winh
- h
in
360 let rec f pn ph pi fh l
=
362 | (n, _
, h
, _
) :: rest
->
363 let ips = calcips h
in
368 if state.birdseye
<> None
&& pn
= 0
373 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
378 if conf.presentation
|| (state.birdseye
<> None
&& pn
= 0)
382 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
385 let fh = f 0 0 0 0 state.pdims
in
389 let getpageyh pageno
=
390 let rec f pn ph pi y l
=
392 | (n, _
, h
, _
) :: rest
->
393 let ips = calcips h
in
396 if conf.presentation
&& n = pageno
398 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
400 y
+ (pageno
- pn
) * (ph
+ pi
), h
402 let y = y + (if conf.presentation
then pi
else 0) in
403 let y = y + (n - pn
) * (ph
+ pi
) in
407 y + (pageno
- pn
) * (ph
+ pi
), ph
409 f 0 0 0 0 state.pdims
412 let getpagey pageno
= fst
(getpageyh pageno
);;
415 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
416 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
418 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
419 let ips = calcips h
in
421 if conf.presentation
|| (state.birdseye
<> None
&& pageno
= 0)
425 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
427 prev
, pdims
, pdimno
, 0
429 let dy = dy + yinc in
430 let py = py + yinc in
431 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
436 if py + h
<= vy - yinc
438 let py = py + h
+ ips in
439 let dy = max
0 (py - y) in
449 let pagey = vy - py in
450 let pagevh = h
- pagey in
451 let pagevh = min
(sh
- dy) pagevh in
452 let off = if yinc > 0 then py - vy else 0 in
453 let py = py + h
+ ips in
459 ; pagedispy
= dy + off
460 ; pagey = pagey + off
461 ; pagevh = pagevh - off
465 let accu = e :: accu in
472 ~cacheleft
:(cacheleft
-1)
475 if state.invalidated
= 0
485 ~cacheleft
:(cbcap state.pagecache
)
495 let y = state.y + incr
in
497 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
501 let getopaque pageno
=
502 try Some
(Hashtbl.find
state.pagemap
503 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
504 with Not_found
-> None
507 let cache pageno opaque
=
508 Hashtbl.replace
state.pagemap
509 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
512 let validopaque opaque
= String.length opaque
> 0;;
515 match getopaque l
.pageno
with
516 | None
when not
state.rendering
->
517 state.rendering
<- true;
518 cache l
.pageno
("", -1);
519 wcmd "render" [`i
(l
.pageno
+ 1)
527 let loadlayout layout =
528 let rec f all
= function
530 begin match getopaque l
.pageno
with
531 | None
-> render l
; f false ls
532 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
536 f (layout <> []) layout;
539 let findpageforopaque opaque
=
541 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
545 let pagevisible n = List.exists
(fun l
-> l
.pageno
= n) state.layout;;
551 let opaque = cbpeek state.pagecache
in
552 match findpageforopaque opaque with
553 | Some
((n, _
, _
, _
, _
), size
) ->
554 not
(pagevisible n) && state.memused
- size
<= conf.memlimit
559 let rely = yratio state.y in
560 let presentation = conf.presentation in
561 let interpagespace = conf.interpagespace in
562 let maxy = state.maxy in
563 conf.presentation <- false;
564 conf.interpagespace <- 0;
565 state.maxy <- calcheight ();
566 let y = truncate
(float state.maxy *. rely) in
567 let y = if y < conf.winh
then 0 else y - conf.winh
in
568 let pages = layout y (conf.winh
*3) in
569 List.iter
render pages;
570 conf.presentation <- presentation;
571 conf.interpagespace <- interpagespace;
577 let y = min
state.maxy y in
578 let pages = layout y conf.winh
in
579 let ready = loadlayout pages in
580 state.ty
<- yratio y;
581 state.layout <- pages;
587 Glut.postRedisplay
();
592 Glut.postRedisplay
();
594 if state.birdseye
<> None
596 if not
(pagevisible state.birdseyepageno
)
598 match state.layout with
600 | l
:: _
-> state.birdseyepageno
<- l
.pageno
605 let gotoy_and_clear_text y =
607 if not
conf.verbose
then state.text
<- "";
611 cbput state.hists
.nav
(yratio state.y);
615 let y = cbgetc state.hists
.nav ~
-1 in
616 truncate
(y *. float state.maxy)
620 let y, h
= getpageyh n in
622 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
625 let gotopage1 n top
=
626 let y = getpagey n in
628 gotoy_and_clear_text (y + top
);
636 state.invalidated
<- state.invalidated
+ 1;
640 let c = c *. state.colorscale
in
646 match state.layout with
648 let rely = yratio state.y in
649 state.maxy <- calcheight ();
650 truncate
(float state.maxy *. rely)
653 state.maxy <- calcheight ();
660 GlMat.mode `projection
;
661 GlMat.load_identity
();
662 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
663 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
664 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
668 GlMat.mode `projection
;
669 GlMat.load_identity
();
670 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
671 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
672 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
677 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
681 GlMat.mode `modelview
;
682 GlMat.load_identity
();
683 GlClear.color
(scalecolor 1.0);
684 GlClear.clear
[`color
];
687 wcmd "geometry" [`i
w; `i h
];
691 GlDraw.color
(0.0, 0.0, 0.0);
693 (0.0, float (conf.winh
- 18))
694 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
696 let font = Glut.BITMAP_8_BY_13
in
697 GlDraw.color
(1.0, 1.0, 1.0);
698 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
699 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
700 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
704 let len = String.length
state.text
in
705 match state.textentry
with
707 if len > 0 then showtext ' '
state.text
709 | Some
(c, text
, _
, _
, _
) ->
713 text ^
" [" ^
state.text ^
"]"
723 state.text
<- Printf.sprintf
"%c%s" c s;
724 Glut.postRedisplay
();
738 state.rects
<- state.rects1
;
739 Glut.postRedisplay
()
742 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
743 state.pagecount
<- n;
744 state.invalidated
<- state.invalidated
- 1;
745 if state.invalidated
= 0
749 let s = Scanf.sscanf
cmd "t %n"
750 (fun n -> String.sub
cmd n (String.length
cmd - n))
752 Glut.setWindowTitle
s
755 let s = Scanf.sscanf
cmd "T %n"
756 (fun n -> String.sub
cmd n (String.length
cmd - n))
758 if state.textentry
= None
765 Glut.postRedisplay
();
771 let s = Scanf.sscanf
cmd "V %n"
772 (fun n -> String.sub
cmd n (String.length
cmd - n))
778 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
779 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
780 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
781 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
783 let y = (getpagey pageno) + truncate y0
in
786 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
789 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
790 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
791 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
792 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
795 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
798 let n, w, h
, r
, l
, s, p
=
799 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
800 (fun n w h r l
s p
->
801 (n-1, w, h
, r
, l
!= 0, s, p
))
804 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
805 state.memused
<- state.memused
+ s;
808 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
811 let evictedopaque = cbpeek state.pagecache
in
812 match findpageforopaque evictedopaque with
813 | None
-> failwith
"bug in gc"
814 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
815 if state.gen
!= gen
|| not
(pagevisible evictedn
)
817 wcmd "free" [`
s evictedopaque];
818 state.memused
<- state.memused
- evictedsize
;
819 Hashtbl.remove
state.pagemap k
;
820 cbdecr state.pagecache
;
827 cbput state.pagecache p
;
828 state.rendering
<- false;
831 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
835 else (ignore
(loadlayout state.layout); preload ())
839 let (n, w, h
, x
) as pdim
=
840 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
842 state.pdims
<- pdim
:: state.pdims
845 let (l
, n, t
, h
, pos
) =
846 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
848 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
850 let l = String.length
s in
851 let b = Buffer.create
(String.length
s) in
858 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
861 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
870 let outline = (s, l, n, float t
/. float h
) in
872 match state.outlines with
873 | Olist
outlines -> Olist
(outline :: outlines)
874 | Oarray _
-> Olist
[outline]
875 | Onarrow _
-> Olist
[outline]
877 state.outlines <- outlines
880 log "unknown cmd `%S'" cmd
883 let now = Unix.gettimeofday
;;
887 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
892 let y = state.y + conf.scrollincr
in
893 let y = if y >= state.maxy then 0 else y in
899 let cmd = readcmd state.csock
in
908 let action = function
909 | HCprev
-> cbget cb ~
-1
910 | HCnext
-> cbget cb
1
911 | HCfirst
-> cbget cb ~
-(cb
.rc)
912 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
913 and cancel
() = cb
.rc <- rc
917 let search pattern forward
=
918 if String.length pattern
> 0
921 match state.layout with
924 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
927 let b = makecmd "search"
928 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
930 Buffer.add_char
b '
,'
;
931 Buffer.add_string
b pattern
;
932 Buffer.add_char
b '
\000'
;
935 writecmd state.csock
cmd;
938 let intentry text key
=
939 let c = Char.unsafe_chr key
in
942 let s = "x" in s.[0] <- c;
943 let text = text ^
s in
947 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
952 let b = Buffer.create
(String.length
s + 1) in
953 Buffer.add_string
b s;
958 let textentry text key
=
959 let c = Char.unsafe_chr key
in
961 | _
when key
>= 32 && key
< 127 ->
962 let text = addchar text c in
966 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
970 let reinit angle proportional
=
972 conf.proportional
<- proportional
;
974 wcmd "reinit" [`i angle
; `
b proportional
];
977 let optentry text key
=
978 let btos b = if b then "on" else "off" in
979 let c = Char.unsafe_chr key
in
983 try conf.scrollincr
<- int_of_string
s with exc
->
984 state.text <- Printf.sprintf
"bad integer `%s': %s"
985 s (Printexc.to_string exc
)
987 TEswitch
('#'
, "", None
, intentry, ondone)
992 Some
(int_of_string
s)
994 state.text <- Printf.sprintf
"bad integer `%s': %s"
995 s (Printexc.to_string exc
);
998 | Some angle
-> reinit angle
conf.proportional
1001 TEswitch
('^'
, "", None
, intentry, ondone)
1004 conf.icase
<- not
conf.icase
;
1005 TEdone
("case insensitive search " ^
(btos conf.icase
))
1008 conf.preload <- not
conf.preload;
1010 TEdone
("preload " ^
(btos conf.preload))
1013 conf.verbose
<- not
conf.verbose
;
1014 TEdone
("verbose " ^
(btos conf.verbose
))
1017 conf.maxhfit
<- not
conf.maxhfit
;
1018 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1019 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1022 conf.crophack
<- not
conf.crophack
;
1023 TEdone
("crophack " ^
btos conf.crophack
)
1026 conf.showall
<- not
conf.showall
;
1027 TEdone
("showall " ^
btos conf.showall
)
1030 conf.underinfo
<- not
conf.underinfo
;
1031 TEdone
("underinfo " ^
btos conf.underinfo
)
1034 conf.savebmarks
<- not
conf.savebmarks
;
1035 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1040 conf.interpagespace <- int_of_string
s;
1041 let rely = yratio state.y in
1042 state.maxy <- calcheight ();
1043 gotoy (truncate
(float state.maxy *. rely));
1045 state.text <- Printf.sprintf
"bad integer `%s': %s"
1046 s (Printexc.to_string exc
)
1048 TEswitch
('
%'
, "", None
, intentry, ondone)
1051 reinit conf.angle
(not
conf.proportional
);
1052 TEdone
("proprortional display " ^
btos conf.proportional
)
1055 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1059 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1061 let enterselector allowdel
outlines errmsg msg
=
1062 if Array.length
outlines = 0
1064 showtext ' ' errmsg
;
1068 Glut.setCursor
Glut.CURSOR_INHERIT
;
1070 match state.layout with
1072 | {pageno=pageno} :: rest
-> pageno
1076 if n = Array.length
outlines
1079 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1080 if outlinepageno
>= pageno then n else loop (n+1)
1085 Some
(allowdel
, active,
1086 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1087 Glut.postRedisplay
();
1091 let enteroutlinemode () =
1093 match state.outlines with
1096 let a = Array.of_list
(List.rev
l) in
1097 state.outlines <- Oarray
a;
1099 | Onarrow
(pat
, a, b) ->
1100 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1102 enterselector false outlines "Document has no outline" msg
;
1105 let enterbookmarkmode () =
1106 let bookmarks = Array.of_list
state.bookmarks in
1107 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1110 let quickbookmark ?title
() =
1111 match state.layout with
1117 let sec = Unix.gettimeofday
() in
1118 let tm = Unix.localtime
sec in
1119 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1123 (tm.Unix.tm_year
+ 1900)
1126 | Some
title -> title
1129 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1133 state.fullscreen
<- None
;
1134 Glut.reshapeWindow
w h
;
1137 let writeopen path password
=
1138 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1141 let opendoc path password
=
1144 state.password
<- password
;
1145 state.gen
<- state.gen
+ 1;
1147 writeopen path password
;
1148 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1149 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1152 let birdseyeoff (c, leftx
) =
1153 state.birdseye
<- None
;
1154 conf.zoom
<- c.zoom
;
1155 conf.presentation <- c.presentation;
1156 conf.interpagespace <- c.interpagespace;
1157 conf.showall
<- c.showall
;
1158 conf.hlinks
<- c.hlinks
;
1160 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1164 let viewkeyboard ~key ~x ~
y =
1166 state.textentry <- te
;
1169 Glut.postRedisplay
()
1171 match state.textentry with
1173 let c = Char.chr key
in
1179 let y = getnav () in
1180 gotoy_and_clear_text y
1183 begin match state.birdseye
with
1186 let y = getpagey state.birdseyepageno
in
1189 reshape conf.winw
conf.winh
;
1198 Glut.postRedisplay
()
1201 let ondone isforw
s =
1202 cbput state.hists
.pat
s;
1203 state.searchpattern
<- s;
1206 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1207 textentry, ondone (c ='
/'
)))
1209 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1210 let incr = if conf.zoom
+. 0.01 > 0.1 then 0.1 else 0.01 in
1211 conf.zoom
<- min
2.2 (conf.zoom
+. incr);
1212 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1213 reshape conf.winw
conf.winh
1218 try int_of_string
s with exc
->
1219 state.text <- Printf.sprintf
"bad integer `%s': %s"
1220 s (Printexc.to_string exc
);
1226 state.text <- "page bias is now " ^ string_of_int
n;
1229 enttext (Some
('
+'
, "", None
, intentry, ondone))
1231 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1232 let decr = if conf.zoom
-. 0.1 < 0.1 then 0.01 else 0.1 in
1233 conf.zoom
<- max
0.01 (conf.zoom
-. decr);
1234 if conf.zoom
<= 1.0 then state.x
<- 0;
1235 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1236 reshape conf.winw
conf.winh
;
1242 enttext (Some
('
-'
, "", None
, optentry, ondone))
1244 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1247 state.text <- "zoom is 100%";
1248 reshape conf.winw
conf.winh
1250 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1251 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1256 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1257 reshape conf.winw
conf.winh
;
1260 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1261 begin match state.birdseye
with
1263 let zoom = 76.0 /. float state.w in
1264 state.birdseye
<- Some
({ conf with zoom = conf.zoom }, state.x
);
1266 conf.presentation <- false;
1267 conf.interpagespace <- 10;
1268 conf.hlinks
<- false;
1270 state.mstate
<- Mnone
;
1271 conf.showall
<- false;
1272 Glut.setCursor
Glut.CURSOR_INHERIT
;
1273 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1279 reshape conf.winw
conf.winh
1284 try int_of_string
s with exc
->
1285 state.text <- Printf.sprintf
"bad integer `%s': %s"
1286 s (Printexc.to_string exc
);
1292 cbput state.hists
.pag
(string_of_int
n);
1293 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1296 let pageentry text key
=
1297 match Char.unsafe_chr key
with
1298 | 'g'
-> TEdone
text
1299 | _
-> intentry text key
1301 let text = "x" in text.[0] <- c;
1302 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1306 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1307 reshape conf.winw
conf.winh
;
1310 conf.hlinks
<- not
conf.hlinks
;
1311 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1312 Glut.postRedisplay
()
1315 conf.autoscroll
<- not
conf.autoscroll
1318 conf.presentation <- not
conf.presentation;
1319 showtext ' '
("presentation mode " ^
1320 if conf.presentation then "on" else "off");
1324 begin match state.fullscreen
with
1326 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1329 state.fullscreen
<- None
;
1334 gotoy_and_clear_text 0
1337 search state.searchpattern
true
1340 search state.searchpattern
false
1343 begin match state.layout with
1346 gotoy_and_clear_text (getpagey l.pageno)
1350 begin match List.rev
state.layout with
1353 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1354 gotoy_and_clear_text (getpagey pageno)
1358 begin match state.layout with
1361 let pageno = max
0 (l.pageno-1) in
1362 gotoy_and_clear_text (getpagey pageno)
1367 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1369 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1371 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1375 else (100. *. (float state.y /. float maxy)) in
1378 Printf.sprintf
"Page %d of %d %.2f%%"
1379 (fn+1) state.pagecount
percent
1382 "Pages %d-%d of %d %.2f%%"
1383 (fn+1) (ln
+1) state.pagecount
percent
1388 begin match state.layout with
1391 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1392 Glut.postRedisplay
();
1396 enterbookmarkmode ()
1400 match state.layout with
1403 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1407 enttext (Some
('~'
, "", None
, textentry, ondone))
1411 showtext ' '
"Quick bookmark added";
1414 begin match state.layout with
1416 let rect = getpdimrect
l.pagedimno
in
1420 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1421 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1423 (truncate
(rect.(1) -. rect.(0)),
1424 truncate
(rect.(3) -. rect.(0)))
1428 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1430 Glut.postRedisplay
();
1436 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1441 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1442 Glut.postRedisplay
()
1444 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1445 | 'j'
-> gotoy (clamp conf.scrollincr
)
1447 | '
r'
-> opendoc state.path
state.password
1450 vlog "huh? %d %c" key
(Char.chr key
);
1453 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1454 let len = String.length
text in
1457 state.textentry <- None
;
1458 Glut.postRedisplay
();
1461 let s = String.sub
text 0 (len - 1) in
1462 enttext (Some
(c, s, opthist
, onkey
, ondone))
1465 | Some
(c, text, onhist, onkey
, ondone) ->
1466 begin match Char.unsafe_chr key
with
1469 state.textentry <- None
;
1470 Glut.postRedisplay
()
1473 begin match onhist with
1475 | Some
(_
, onhistcancel
) -> onhistcancel
()
1477 state.textentry <- None
;
1478 Glut.postRedisplay
()
1481 begin match onkey
text key
with
1483 state.textentry <- None
;
1485 Glut.postRedisplay
()
1488 enttext (Some
(c, text, onhist, onkey
, ondone));
1491 state.textentry <- None
;
1492 Glut.postRedisplay
()
1495 state.textentry <- Some te
;
1496 Glut.postRedisplay
()
1501 let narrow outlines pattern
=
1502 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1506 let rec fold accu n =
1510 let (s, _
, _
, _
) as o
= outlines.(n) in
1512 if (try ignore
(Str.search_forward re
s 0); true
1513 with Not_found
-> false)
1519 let matched = fold [] (Array.length
outlines - 1) in
1520 if matched = [] then None
else Some
(Array.of_list
matched)
1523 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1524 let search active pattern
incr =
1527 if n = Array.length
outlines || n = -1
1530 let (s, _
, _
, _
) = outlines.(n) in
1532 (try ignore
(Str.search_forward re
s 0); true
1533 with Not_found
-> false)
1535 else loop (n + incr)
1540 let re = Str.regexp_case_fold pattern
in
1546 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1549 if String.length qsearch
= 0
1552 state.outline <- None
;
1553 Glut.postRedisplay
();
1557 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1558 Glut.postRedisplay
();
1562 let incr = if key
= 18 then -1 else 1 in
1564 match search (active + incr) qsearch
incr with
1566 state.text <- qsearch ^
" [not found]";
1569 state.text <- qsearch
;
1570 active, firstof active
1572 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1573 Glut.postRedisplay
();
1576 let len = String.length qsearch
in
1583 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1586 let qsearch = String.sub
qsearch 0 (len - 1) in
1588 match search active qsearch ~
-1 with
1590 state.text <- qsearch ^
" [not found]";
1593 state.text <- qsearch;
1594 active, firstof active
1596 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1598 Glut.postRedisplay
()
1601 if active < Array.length
outlines
1603 let (_
, _
, n, t
) = outlines.(active) in
1607 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1608 state.outline <- None
;
1609 Glut.postRedisplay
();
1611 | _
when key
>= 32 && key
< 127 ->
1612 let pattern = addchar qsearch (Char.chr key
) in
1614 match search active pattern 1 with
1616 state.text <- pattern ^
" [not found]";
1619 state.text <- pattern;
1620 active, firstof active
1622 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1623 Glut.postRedisplay
()
1625 | 14 when not allowdel
-> (* ctrl-n *)
1626 if String.length
qsearch > 0
1628 let optoutlines = narrow outlines qsearch in
1629 begin match optoutlines with
1630 | None
-> state.text <- "can't narrow"
1632 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1633 match state.outlines with
1636 state.outlines <- Onarrow
(qsearch, outlines, a)
1637 | Onarrow
(pat
, a, b) ->
1638 state.outlines <- Onarrow
(qsearch, outlines, b)
1641 Glut.postRedisplay
()
1643 | 21 when not allowdel
-> (* ctrl-u *)
1645 match state.outlines with
1648 let a = Array.of_list
(List.rev
l) in
1649 state.outlines <- Oarray
a;
1651 | Onarrow
(pat
, a, b) ->
1652 state.outlines <- Oarray
b;
1656 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1657 Glut.postRedisplay
()
1661 Some
(allowdel
, active, firstof active, outlines, qsearch);
1662 Glut.postRedisplay
()
1664 | 127 when allowdel
->
1665 let len = Array.length
outlines - 1 in
1668 state.outline <- None
;
1669 state.bookmarks <- [];
1672 let bookmarks = Array.init
len
1674 let i = if i >= active then i + 1 else i in
1685 Glut.postRedisplay
()
1687 | _
-> log "unknown key %d" key
1690 let keyboard ~key ~x ~
y =
1695 match state.outline with
1696 | None
-> viewkeyboard ~key ~x ~
y
1697 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1700 let special ~key ~x ~
y =
1701 match state.outline with
1702 | None
when state.birdseye
<> None
->
1703 begin match key
with
1705 let pageno = max
0 (state.birdseyepageno
- 1) in
1706 state.birdseyepageno
<- pageno;
1707 if not
(pagevisible pageno)
1708 then gotopage pageno 0.0
1709 else Glut.postRedisplay
();
1712 let pageno = min
(state.pagecount
- 1) (state.birdseyepageno
+ 1) in
1713 state.birdseyepageno
<- pageno;
1714 if not
(pagevisible pageno)
1716 begin match List.rev
state.layout with
1717 | [] -> gotopage pageno 0.0
1719 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1721 else Glut.postRedisplay
();
1723 | Glut.KEY_PAGE_UP
->
1724 begin match state.layout with
1726 if l.pageno = state.birdseyepageno
1728 match layout (state.y - conf.winh
) conf.winh
with
1729 | [] -> gotoy (clamp (-conf.winh
))
1731 state.birdseyepageno
<- max
0 (l.pageno - 1);
1732 gotopage state.birdseyepageno
0.0
1735 state.birdseyepageno
<- max
0 (l.pageno - 1);
1736 gotopage state.birdseyepageno
0.0
1738 | [] -> gotoy (clamp (-conf.winh
))
1740 | Glut.KEY_PAGE_DOWN
->
1741 begin match List.rev
state.layout with
1743 state.birdseyepageno
<- min
(state.pagecount
- 1) (l.pageno + 1);
1744 gotoy (clamp (l.pagedispy
+ conf.interpagespace + l.pageh
))
1745 | [] -> gotoy (clamp conf.winh
)
1749 state.birdseyepageno
<- 0;
1752 state.birdseyepageno
<- state.pagecount
- 1;
1753 if not
(pagevisible state.birdseyepageno
)
1755 gotopage state.birdseyepageno
0.0
1757 Glut.postRedisplay
()
1763 begin match state.textentry with
1767 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1768 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1769 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1770 | Glut.KEY_PAGE_UP
->
1771 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1773 match state.layout with
1775 | l :: _
-> state.y - l.pagey
1778 | Glut.KEY_PAGE_DOWN
->
1779 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1781 match List.rev
state.layout with
1783 | l :: _
-> getpagey l.pageno
1786 | Glut.KEY_HOME
-> addnav (); 0
1789 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1791 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1792 state.x
<- state.x
- 10;
1794 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1795 state.x
<- state.x
+ 10;
1800 gotoy_and_clear_text y
1802 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1805 | Glut.KEY_UP
-> action HCprev
1806 | Glut.KEY_DOWN
-> action HCnext
1807 | Glut.KEY_HOME
-> action HCfirst
1808 | Glut.KEY_END
-> action HClast
1811 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1812 Glut.postRedisplay
()
1817 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1818 let maxrows = maxoutlinerows () in
1819 let calcfirst first
active =
1822 let rows = active - first
in
1823 if rows > maxrows then active - maxrows else first
1827 let active = active + incr in
1828 let active = max
0 (min
active (Array.length
outlines - 1)) in
1829 let first = calcfirst first active in
1830 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1831 Glut.postRedisplay
()
1833 let updownlevel incr =
1834 let len = Array.length
outlines in
1835 let (_
, curlevel
, _
, _
) = outlines.(active) in
1837 if i = len then i-1 else if i = -1 then 0 else
1838 let (_
, l, _
, _
) = outlines.(i) in
1839 if l != curlevel
then i else flow (i+incr)
1841 let active = flow active in
1842 let first = calcfirst first active in
1843 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1844 Glut.postRedisplay
()
1847 | Glut.KEY_UP
-> navigate ~
-1
1848 | Glut.KEY_DOWN
-> navigate 1
1849 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1850 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1852 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1853 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1856 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1857 Glut.postRedisplay
()
1860 let active = Array.length
outlines - 1 in
1861 let first = max
0 (active - maxrows) in
1862 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1863 Glut.postRedisplay
()
1868 let drawplaceholder l =
1869 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1870 GlDraw.color
(scalecolor 1.0);
1872 (float l.pagex
, float l.pagedispy
)
1873 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1875 let x = float (if margin < 0 then -margin else 0)
1876 and y = float (l.pagedispy
+ 13) in
1877 let font = Glut.BITMAP_8_BY_13
in
1878 GlDraw.color
(0.0, 0.0, 0.0);
1879 GlPix.raster_pos ~
x ~
y ();
1880 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1881 ("Loading " ^ string_of_int
(l.pageno + 1));
1884 let now () = Unix.gettimeofday
();;
1887 begin match getopaque l.pageno with
1888 | Some
(opaque, _
) when validopaque opaque ->
1889 if state.textentry = None
1890 then GlDraw.color
(scalecolor 1.0)
1891 else GlDraw.color
(scalecolor 0.4);
1893 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1897 vlog "draw %d %f sec" l.pageno d;
1902 if state.birdseye
<> None
&& state.birdseyepageno
= l.pageno
1904 GlDraw.polygon_mode `both `line
;
1905 GlDraw.line_width
4.0;
1906 GlDraw.color
(0.8, 0.0, 0.0);
1908 (float (l.pagex
- 1), float (l.pagedispy
- 1))
1909 (float (l.pagew
+ l.pagex
+ 1), float (l.pagedispy
+ l.pagevh + 1))
1911 GlDraw.line_width
1.0;
1912 GlDraw.polygon_mode `both `fill
;
1917 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1918 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1919 let sh = float conf.winh
/. sh in
1920 let sh = max
sh (float conf.scrollh
) in
1923 if state.y = state.maxy
1925 else float y /. float maxy
1927 let position = (float conf.winh
-. sh) *. percent in
1930 if position +. sh > float conf.winh
1931 then float conf.winh
-. sh
1937 let scrollindicator () =
1938 GlDraw.color
(0.64 , 0.64, 0.64);
1940 (float (conf.winw
- conf.scrollw
), 0.)
1941 (float conf.winw
, float conf.winh
)
1943 GlDraw.color
(0.0, 0.0, 0.0);
1945 let position, sh = scrollph state.y in
1947 (float (conf.winw
- conf.scrollw
), position)
1948 (float conf.winw
, position +. sh)
1952 let showsel margin =
1953 match state.mstate
with
1954 | Mnone
| Mscroll _
| Mpan _
->
1957 | Msel
((x0
, y0
), (x1
, y1
)) ->
1958 let rec loop = function
1960 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1961 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1963 match getopaque l.pageno with
1964 | Some
(opaque, _
) when validopaque opaque ->
1965 let oy = -l.pagey + l.pagedispy
in
1967 (x0
- margin - state.x, y0
,
1968 x1
- margin - state.x, y1
) oy;
1978 let panx = float state.x in
1980 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1981 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1983 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1985 if l.pageno = pageno
1987 let d = float (l.pagedispy
- l.pagey) in
1988 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1989 GlDraw.begins `quads
;
1991 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1992 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1993 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1994 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2004 let showoutline = function
2006 | Some
(allowdel
, active, first, outlines, qsearch) ->
2008 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2009 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
2010 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2013 GlDraw.color
(1., 1., 1.);
2014 let font = Glut.BITMAP_9_BY_15
in
2015 let draw_string x y s =
2016 GlPix.raster_pos ~
x ~
y ();
2017 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2020 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2023 let (s, l, _
, _
) = outlines.(row
) in
2024 let y = (row
- first) * 16 in
2029 GlDraw.polygon_mode `both `line
;
2030 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2031 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
2032 GlDraw.rect (0., float (y + 1))
2033 (float (conf.winw
- 1), float (y + 18));
2034 GlDraw.polygon_mode `both `fill
;
2036 GlDraw.color
(1., 1., 1.);
2038 draw_string (float x) (float (y + 16)) s;
2046 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2047 GlDraw.viewport
margin 0 state.w conf.winh
;
2049 if state.birdseye
<> None
2051 GlClear.color
(0.5, 0.5, 0.55)
2053 GlClear.color
(scalecolor 0.5)
2055 GlClear.clear
[`color
];
2058 let x = float state.x in
2059 GlMat.translate ~
x ();
2063 Gl.enable `scissor_test
;
2064 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2066 List.iter
drawpage state.layout;
2069 Gl.disable `scissor_test
2073 let x = -.float state.x in
2074 GlMat.translate ~
x ();
2078 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2081 showoutline state.outline;
2083 Glut.swapBuffers
();
2087 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2088 let x = x - margin - state.x in
2089 let rec f = function
2091 begin match getopaque l.pageno with
2092 | Some
(opaque, _
) when validopaque opaque ->
2093 let y = y - l.pagedispy
in
2096 let y = l.pagey + y in
2097 let x = x - l.pagex
in
2098 match whatsunder
opaque x y with
2111 let mouse ~button ~bstate ~
x ~
y =
2113 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2121 let incr = incr * 2 in
2122 let y = clamp incr in
2123 gotoy_and_clear_text y
2125 | Glut.LEFT_BUTTON
when state.outline = None
2126 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2127 if bstate
= Glut.DOWN
2129 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2130 state.mstate
<- Mpan
(x, y)
2133 state.mstate
<- Mnone
2136 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2137 if bstate
= Glut.DOWN
2139 let position, sh = scrollph state.y in
2140 if y > truncate
position && y < truncate
(position +. sh)
2142 state.mstate
<- Mscroll
2144 let percent = float y /. float conf.winh
in
2145 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2147 state.mstate
<- Mscroll
2149 state.mstate
<- Mnone
2151 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2152 begin match state.birdseye
with
2154 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2155 let rec loop = function
2158 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2159 && x > margin && x < margin + l.pagew
2161 let y = getpagey l.pageno in
2164 reshape conf.winw
conf.winh
;
2169 | None
-> () (* impossible *)
2172 | Glut.LEFT_BUTTON
when state.outline = None
->
2173 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2174 begin match dest with
2175 | Ulinkgoto
(pageno, top
) ->
2178 gotopage1 pageno top
2183 | Unone
when bstate
= Glut.DOWN
->
2184 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2185 state.mstate
<- Mpan
(x, y);
2187 | Unone
| Utext _
->
2188 if bstate
= Glut.DOWN
2190 if conf.angle
mod 360 = 0
2192 state.mstate
<- Msel
((x, y), (x, y));
2193 Glut.postRedisplay
()
2197 match state.mstate
with
2201 state.mstate
<- Mnone
2204 Glut.setCursor
Glut.CURSOR_INHERIT
;
2205 state.mstate
<- Mnone
2207 | Msel
((x0
, y0
), (x1
, y1
)) ->
2209 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2210 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2212 match getopaque l.pageno with
2213 | Some
(opaque, _
) when validopaque opaque ->
2217 List.iter
f state.layout;
2218 copysel
""; (* ugly *)
2219 Glut.setCursor
Glut.CURSOR_INHERIT
;
2220 state.mstate
<- Mnone
;
2227 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2230 if state.outline = None
2232 match state.mstate
with
2238 state.mstate
<- Mpan
(x, y);
2239 if conf.zoom > 1.0 then state.x <- state.x + dx;
2241 gotoy_and_clear_text y
2244 state.mstate
<- Msel
(a, (x, y));
2245 Glut.postRedisplay
()
2248 let y = min
conf.winh
(max
0 y) in
2249 let percent = float y /. float conf.winh
in
2250 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2251 gotoy_and_clear_text y
2255 if state.outline = None
&& state.birdseye
= None
2257 match state.mstate
with
2259 begin match getunder x y with
2260 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2262 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2263 Glut.setCursor
Glut.CURSOR_INFO
2264 | Ulinkgoto
(page
, y) ->
2265 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2266 Glut.setCursor
Glut.CURSOR_INFO
2268 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2269 Glut.setCursor
Glut.CURSOR_TEXT
2272 | Mpan _
| Msel _
| Mscroll
->
2282 match Sys.os_type
with
2283 | "Win32" -> Sys.getenv
"HOMEPATH"
2284 | _
-> Sys.getenv
"HOME"
2287 ("Can not determine home directory location: " ^
2288 Printexc.to_string exn
);
2292 let config_of c attrs
=
2296 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2297 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2298 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2299 | "preload" -> { c with preload = bool_of_string v
}
2300 | "page-bias" -> { c with pagebias
= int_of_string v
}
2301 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2302 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2303 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2304 | "throttle" -> { c with showall
= bool_of_string v
}
2305 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2306 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2307 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2309 let zoom = float_of_string v
/. 100. in
2310 let zoom = max
0.01 (min
2.2 zoom) in
2311 { c with zoom = zoom }
2312 | "presentation" -> { c with presentation = bool_of_string v
}
2313 | "rotation-angle" -> { c with angle
= int_of_string v
}
2314 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2315 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2316 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2317 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2318 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2319 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2320 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2323 prerr_endline
("Error processing attribute (`" ^
2324 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2327 let rec fold c = function
2330 let c = apply c k v
in
2336 let bookmark_of attrs
=
2337 let rec fold title page
rely = function
2338 | ("title", v
) :: rest
-> fold v page
rely rest
2339 | ("page", v
) :: rest
-> fold title v
rely rest
2340 | ("rely", v
) :: rest
-> fold title page v rest
2341 | _
:: rest
-> fold title page
rely rest
2342 | [] -> title, page
, rely
2344 fold "invalid" "0" "0" attrs
2347 let setconf dst src
=
2348 dst
.scrollw
<- src
.scrollw
;
2349 dst
.scrollh
<- src
.scrollh
;
2350 dst
.icase
<- src
.icase
;
2351 dst
.preload <- src
.preload;
2352 dst
.pagebias
<- src
.pagebias
;
2353 dst
.verbose
<- src
.verbose
;
2354 dst
.scrollincr
<- src
.scrollincr
;
2355 dst
.maxhfit
<- src
.maxhfit
;
2356 dst
.crophack
<- src
.crophack
;
2357 dst
.autoscroll
<- src
.autoscroll
;
2358 dst
.showall
<- src
.showall
;
2359 dst
.hlinks
<- src
.hlinks
;
2360 dst
.underinfo
<- src
.underinfo
;
2361 dst
.interpagespace <- src
.interpagespace;
2362 dst
.zoom <- src
.zoom;
2363 dst
.presentation <- src
.presentation;
2364 dst
.angle
<- src
.angle
;
2365 dst
.winw
<- src
.winw
;
2366 dst
.winh
<- src
.winh
;
2367 dst
.savebmarks
<- src
.savebmarks
;
2368 dst
.memlimit
<- src
.memlimit
;
2369 dst
.proportional
<- src
.proportional
;
2370 dst
.texcount
<- src
.texcount
;
2371 dst
.sliceheight
<- src
.sliceheight
;
2375 let l = String.length
s in
2376 let b = Buffer.create
l in
2382 let h = Hashtbl.create
10 in
2383 let dc = { defconf with angle
= defconf.angle
} in
2384 let rec toplevel v t spos epos
=
2386 | Vdata
| Vcdata
| Vend
-> v
2387 | Vopen
("llppconfig", attrs
, closed
) ->
2390 else { v
with f = llppconfig
}
2392 error
"unexpected subelement at top level" s spos
2393 | Vclose tag
-> error
"unexpected close at top level" s spos
2395 and llppconfig v t spos epos
=
2397 | Vdata
| Vcdata
| Vend
-> v
2398 | Vopen
("defaults", attrs
, closed
) ->
2399 let c = config_of dc attrs
in
2403 else { v
with f = skip
"defaults" (fun () -> v
) }
2405 | Vopen
("doc", attrs
, closed
) ->
2408 List.assoc
"path" attrs
2409 with Not_found
-> error
"doc is missing path attribute" s spos
2411 let path = unent pathent in
2412 let c = config_of dc attrs
in
2415 float_of_string
(List.assoc
"rely" attrs
)
2419 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2424 int_of_string
(List.assoc
"pan" attrs
)
2428 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2432 then (Hashtbl.add
h path (c, [], x, y); v
)
2433 else { v
with f = doc
path x y c [] }
2435 | Vopen
(tag
, _
, closed
) ->
2436 error
"unexpected subelement in llppconfig" s spos
2438 | Vclose
"llppconfig" -> { v
with f = toplevel }
2439 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2441 and doc
path x y c bookmarks v t spos epos
=
2443 | Vdata
| Vcdata
-> v
2444 | Vend
-> error
"unexpected end of input in doc" s spos
2445 | Vopen
("bookmarks", attrs
, closed
) ->
2446 { v
with f = pbookmarks
path x y c bookmarks }
2448 | Vopen
(tag
, _
, _
) ->
2449 error
"unexpected subelement in doc" s spos
2452 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2453 { v
with f = llppconfig
}
2455 | Vclose tag
-> error
"unexpected close in doc" s spos
2457 and pbookmarks
path x y c bookmarks v t spos epos
=
2459 | Vdata
| Vcdata
-> v
2460 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2461 | Vopen
("item", attrs
, closed
) ->
2462 let titleent, spage
, srely
= bookmark_of attrs
in
2467 dolog "Failed to convert page %S to integer: %s"
2468 spage
(Printexc.to_string exn
);
2473 float_of_string srely
2475 dolog "Failed to convert rely %S to real: %s"
2476 srely
(Printexc.to_string exn
);
2479 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2481 then { v
with f = pbookmarks
path x y c bookmarks }
2484 { v
with f = skip
"item" f }
2487 error
"unexpected subelement in bookmarks" s spos
2489 | Vclose
"bookmarks" ->
2490 { v
with f = doc
path x y c bookmarks }
2492 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2494 and skip tag
f v t spos epos
=
2496 | Vdata
| Vcdata
-> v
2498 error
("unexpected end of input in skipped " ^ tag
) s spos
2499 | Vopen
(tag'
, _
, closed
) ->
2503 let f'
() = { v
with f = skip tag
f } in
2504 { v
with f = skip tag'
f'
}
2508 else error
("unexpected close in skipped " ^ tag
) s spos
2511 parse
{ f = toplevel; accu = () } s;
2517 let len = in_channel_length ic
in
2518 let s = String.create
len in
2519 really_input ic
s 0 len;
2522 | Parse_error
(msg
, s, pos
) ->
2523 let subs = subs s pos
in
2524 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2525 failwith
("parse error: " ^
s)
2528 failwith
("config load error: " ^
Printexc.to_string exn
)
2534 let dir = Filename.concat
home ".config" in
2535 if Sys.is_directory
dir then dir else home
2538 Filename.concat
dir "llpp.conf"
2542 if Sys.file_exists
path
2545 (try Some
(open_in_bin
path)
2548 ("Error opening configuation file `" ^
path ^
"': " ^
2549 Printexc.to_string exn
);
2558 ("Error loading configuation from `" ^
path ^
"': " ^
2559 Printexc.to_string exn
);
2565 f (Hashtbl.create
0, defconf)
2570 let pc, pb
, px
, py =
2572 Hashtbl.find
h state.path
2573 with Not_found
-> dc, [], 0, 0.0
2577 state.bookmarks <- pb
;
2579 cbput state.hists
.nav
py;
2584 let add_attrs bb always
dc c =
2587 then Printf.bprintf bb
"\n %s='%b'" s a
2590 then Printf.bprintf bb
"\n %s='%d'" s a
2593 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2597 then dc.winw
, dc.winh
2599 match state.fullscreen
with
2601 | None
-> c.winw
, c.winh
2603 let zoom, presentation, interpagespace, showall
=
2605 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2607 match state.birdseye
with
2609 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2610 | None
-> c.zoom, c.presentation, c.interpagespace, c.showall
2612 oi
"width" w dc.winw
;
2613 oi
"height" h dc.winh
;
2614 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2615 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2616 ob "case-insensitive-search" c.icase
dc.icase
;
2617 ob "preload" c.preload dc.preload;
2618 oi
"page-bias" c.pagebias
dc.pagebias
;
2619 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2620 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2621 ob "crop-hack" c.crophack
dc.crophack
;
2622 ob "throttle" showall
dc.showall
;
2623 ob "highlight-links" c.hlinks
dc.hlinks
;
2624 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2625 oi
"vertical-margin" interpagespace dc.interpagespace;
2626 oz
"zoom" zoom dc.zoom;
2627 ob "presentation" presentation dc.presentation;
2628 oi
"rotation-angle" c.angle
dc.angle
;
2629 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2630 ob "proportional-display" c.proportional
dc.proportional
;
2631 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2632 oi
"texcount" c.texcount
dc.texcount
;
2633 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2637 let bb = Buffer.create
32768 in
2639 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2640 add_attrs bb true dc dc;
2641 Buffer.add_string
bb "/>\n";
2643 let adddoc path x y c bookmarks =
2644 if bookmarks == [] && c = dc && y = 0.0
2647 Printf.bprintf
bb "<doc path='%s'"
2648 (enent
path 0 (String.length
path));
2651 then Printf.bprintf
bb " rely='%f'" y;
2654 then Printf.bprintf
bb " pan='%d'" x;
2656 add_attrs bb false dc c;
2658 begin match bookmarks with
2659 | [] -> Buffer.add_string
bb "/>\n"
2661 Buffer.add_string
bb ">\n<bookmarks>\n";
2662 List.iter
(fun (title, _level
, page, rely) ->
2664 "<item title='%s' page='%d' rely='%f'/>\n"
2665 (enent
title 0 (String.length
title))
2669 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2675 match state.birdseye
with
2679 adddoc state.path x (yratio state.y) conf
2680 (if conf.savebmarks
then state.bookmarks else []);
2682 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2683 if path <> state.path
2685 adddoc path x y c bookmarks
2687 Buffer.add_string
bb "</llppconfig>";
2690 if Buffer.length
bb > 0
2693 let tmp = path ^
".tmp" in
2694 let oc = open_out_bin
tmp in
2695 Buffer.output_buffer
oc bb;
2697 Sys.rename
tmp path;
2700 ("error while saving configuration: " ^
Printexc.to_string exn
)
2706 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2707 (fun s -> state.path <- s)
2708 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2711 if String.length
state.path = 0
2712 then (prerr_endline
"filename missing"; exit
1)
2714 if Filename.is_relative
state.path
2715 then Filename.concat
(Sys.getcwd
()) state.path
2723 let _ = Glut.init
Sys.argv
in
2724 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2725 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2726 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2729 if Sys.os_type
= "Unix"
2731 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2733 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2734 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2735 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2736 Unix.bind
sock addr;
2738 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2739 Unix.connect
csock addr;
2740 let ssock, _ = Unix.accept
sock in
2743 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2744 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2748 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2752 let () = Glut.displayFunc
display in
2753 let () = Glut.reshapeFunc
reshape in
2754 let () = Glut.keyboardFunc
keyboard in
2755 let () = Glut.specialFunc
special in
2756 let () = Glut.idleFunc
(Some
idle) in
2757 let () = Glut.mouseFunc
mouse in
2758 let () = Glut.motionFunc
motion in
2759 let () = Glut.passiveMotionFunc
pmotion in
2761 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2762 state.csock <- csock;
2763 state.ssock <- ssock;
2764 state.text <- "Opening " ^
path;
2765 writeopen state.path state.password
;
2769 let rec handlelablglutbug () =
2772 with Glut.BadEnum
"key in special_of_int" ->
2773 showtext '
!'
" LablGlut bug: special key not recognized";
2774 handlelablglutbug ()
2776 handlelablglutbug ();