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
28 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
29 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext
: string -> (int * int * int * int) -> int -> unit =
32 external copysel
: string -> unit = "ml_copysel";;
33 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
34 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
35 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
39 | Msel
of (mpos
* mpos
)
53 type textentry
= (char
* string * onhist
* onkey
* ondone
)
54 and onkey
= string -> int -> te
55 and ondone
= string -> unit
56 and histcancel
= unit -> unit
57 and onhist
= ((histcmd
-> string) * histcancel
) option
58 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
63 | TEswitch
of textentry
67 { store
= Array.create n v
74 let cbcap b
= Array.length b
.store
;;
79 b
.wc
<- (b
.wc
+ 1) mod cap;
81 b
.len
<- min
(b
.len
+ 1) cap;
84 let cbempty b
= b
.len
= 0;;
86 let cbgetg b circular dir
=
90 let rc = b
.rc + dir
in
102 else max
0 (min
rc (b
.len
-1))
108 let cbget b
= cbgetg b
false;;
109 let cbgetc b
= cbgetg b
true;;
112 let rc = b
.wc
- b
.len
in
113 let rc = if rc < 0 then cbcap b
+ rc else rc in
117 let cbdecr b
= b
.len
<- b
.len
- 1;;
132 { mutable scrollw
: int
133 ; mutable scrollh
: int
134 ; mutable icase
: bool
135 ; mutable preload
: bool
136 ; mutable pagebias
: int
137 ; mutable verbose
: bool
138 ; mutable scrollincr
: int
139 ; mutable maxhfit
: bool
140 ; mutable crophack
: bool
141 ; mutable autoscroll
: bool
142 ; mutable showall
: bool
143 ; mutable hlinks
: bool
144 ; mutable underinfo
: bool
145 ; mutable interpagespace
: interpagespace
146 ; mutable zoom
: float
147 ; mutable presentation
: bool
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
156 ; mutable thumbw
: width
160 type outline
= string * int * int * float;;
162 | Oarray
of outline array
163 | Olist
of outline list
164 | Onarrow
of string * outline array
* outline array
167 type rect
= (float * float * float * float * float * float * float * float);;
169 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
171 type anchor
= pageno
* top
;;
174 | Birdseye
of (conf
* leftx
* pageno
* pageno
)
175 | Outline
of (bool * int * int * outline array
* string)
176 | Textentry
of textentry
180 let isbirdseye = function Birdseye _
-> true | _
-> false;;
181 let istextentry = function Textentry _
-> true | _
-> false;;
182 let isoutline = function Outline _
-> true | _
-> false;;
185 { mutable csock
: Unix.file_descr
186 ; mutable ssock
: Unix.file_descr
190 ; mutable anchor
: anchor
192 ; mutable layout
: layout list
193 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
194 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
195 ; mutable pagecount
: int
196 ; pagecache
: string circbuf
197 ; mutable rendering
: bool
198 ; mutable mstate
: mstate
199 ; mutable searchpattern
: string
200 ; mutable rects
: (pageno
* recttype
* rect
) list
201 ; mutable rects1
: (pageno
* recttype
* rect
) list
202 ; mutable text
: string
203 ; mutable fullscreen
: (width
* height
) option
204 ; mutable mode
: mode
205 ; mutable outlines
: outlines
206 ; mutable bookmarks
: outline list
207 ; mutable path
: string
208 ; mutable password
: string
209 ; mutable invalidated
: int
210 ; mutable colorscale
: float
211 ; mutable memused
: int
213 ; mutable throttle
: layout list
option
217 { pat
: string circbuf
218 ; pag
: string circbuf
219 ; nav
: anchor circbuf
239 ; presentation
= false
244 ; proportional
= true
245 ; memlimit
= 32*1024*1024
252 let conf = { defconf with angle
= defconf.angle
};;
263 ; pagemap
= Hashtbl.create
10
264 ; pagecache
= cbnew 100 ""
275 ; outlines
= Olist
[]
281 { nav
= cbnew 100 (0, 0.0)
295 Printf.kprintf prerr_endline fmt
297 Printf.kprintf ignore fmt
301 let len = String.length s
in
303 let b = Buffer.create
n in
304 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
305 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
306 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
307 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
308 Buffer.add_string
b s
;
309 let s'
= Buffer.contents
b in
310 let n'
= Unix.write fd
s'
0 n in
311 if n'
!= n then failwith
"write failed";
316 let n = Unix.read fd
s 0 4 in
317 if n != 4 then failwith
"incomplete read(len)";
319 lor (Char.code
s.[0] lsl 24)
320 lor (Char.code
s.[1] lsl 16)
321 lor (Char.code
s.[2] lsl 8)
322 lor (Char.code
s.[3] lsl 0)
324 let s = String.create
len in
325 let n = Unix.read fd
s 0 len in
326 if n != len then failwith
"incomplete read(data)";
331 let b = Buffer.create
10 in
332 Buffer.add_string
b s;
333 let rec combine = function
336 Buffer.add_char
b ' '
;
339 | `
b b -> if b then "1" else "0"
341 | `i i
-> string_of_int i
342 | `f f
-> string_of_float f
343 | `I f
-> string_of_int
(truncate f
)
345 Buffer.add_string
b s;
352 let cmd = Buffer.contents
(makecmd s l
) in
353 writecmd state.csock
cmd;
359 let d = conf.winh
- h
in
366 let rec f pn ph pi fh l
=
368 | (n, _
, h
, _
) :: rest
->
369 let ips = calcips h
in
374 if isbirdseye state.mode
&& pn
= 0
379 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
384 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
388 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
391 let fh = f 0 0 0 0 state.pdims
in
395 let getpageyh pageno
=
396 let rec f pn ph pi y l
=
398 | (n, _
, h
, _
) :: rest
->
399 let ips = calcips h
in
402 let h = if n = pageno
then h else ph
in
403 if conf.presentation
&& n = pageno
405 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
407 y
+ (pageno
- pn
) * (ph
+ pi
), h
409 let y = y + (if conf.presentation
then pi
else 0) in
410 let y = y + (n - pn
) * (ph
+ pi
) in
414 y + (pageno
- pn
) * (ph
+ pi
), ph
416 f 0 0 0 0 state.pdims
419 let getpagey pageno
= fst
(getpageyh pageno
);;
422 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
423 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
425 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
426 let ips = calcips h in
428 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
432 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
434 prev
, pdims
, pdimno
, 0
436 let dy = dy + yinc in
437 let py = py + yinc in
438 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
443 if py + h <= vy - yinc
445 let py = py + h + ips in
446 let dy = max
0 (py - y) in
456 let pagey = vy - py in
457 let pagevh = h - pagey in
458 let pagevh = min
(sh
- dy) pagevh in
459 let off = if yinc > 0 then py - vy else 0 in
460 let py = py + h + ips in
466 ; pagedispy
= dy + off
467 ; pagey = pagey + off
468 ; pagevh = pagevh - off
472 let accu = e :: accu in
479 ~cacheleft
:(cacheleft
-1)
482 if state.invalidated
= 0
492 ~cacheleft
:(cbcap state.pagecache
)
502 let y = state.y + incr
in
504 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
508 let getopaque pageno
=
509 try Some
(Hashtbl.find
state.pagemap
510 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
511 with Not_found
-> None
514 let cache pageno opaque
=
515 Hashtbl.replace
state.pagemap
516 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
519 let validopaque opaque
= String.length opaque
> 0;;
522 match getopaque l
.pageno
with
523 | None
when not
state.rendering
->
524 state.rendering
<- true;
525 cache l
.pageno
("", -1);
526 wcmd "render" [`i
(l
.pageno
+ 1)
534 let loadlayout layout =
535 let rec f all
= function
537 begin match getopaque l
.pageno
with
538 | None
-> render l
; f false ls
539 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
543 f (layout <> []) layout;
546 let findpageforopaque opaque
=
548 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
552 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
558 let memleft = conf.memlimit
- state.memused
in
561 let opaque = cbpeek state.pagecache
in
562 match findpageforopaque opaque with
563 | Some
((n, _
, _
, _
, _
), size
) ->
564 memleft + size
>= 0 && not
(pagevisible state.layout n)
571 let presentation = conf.presentation in
572 let interpagespace = conf.interpagespace in
573 let maxy = state.maxy in
574 conf.presentation <- false;
575 conf.interpagespace <- 0;
576 state.maxy <- calcheight ();
578 match state.layout with
580 | l
:: _
-> getpagey l
.pageno
582 let y = if y < conf.winh
then 0 else y - conf.winh
in
583 let pages = layout y (conf.winh
*3) in
584 List.iter
render pages;
585 conf.presentation <- presentation;
586 conf.interpagespace <- interpagespace;
592 let y = min
state.maxy y in
593 let pages = layout y conf.winh
in
594 let ready = loadlayout pages in
600 state.layout <- pages;
601 state.throttle
<- None
;
602 Glut.postRedisplay
();
605 state.throttle
<- Some
pages;
610 state.layout <- pages;
611 state.throttle
<- None
;
612 Glut.postRedisplay
();
614 begin match state.mode
with
615 | Birdseye
(conf, leftx
, pageno
, hooverpageno
) ->
616 if not
(pagevisible pages pageno
)
618 match state.layout with
621 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
)
628 let gotoy_and_clear_text y =
630 if not
conf.verbose
then state.text
<- "";
633 let emptyanchor = (0, 0.0);;
636 match state.layout with
638 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
641 let getanchory (n, top
) =
642 let y, h = getpageyh n in
643 y + (truncate
(top
*. float h));
646 let gotoanchor anchor
=
647 gotoy (getanchory anchor
);
651 cbput state.hists
.nav
(getanchor ());
655 let anchor = cbgetc state.hists
.nav ~
-1 in
659 let gotopagenonav n top
=
660 let y, h = getpageyh n in
661 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
664 let gotopage1nonav n top
=
665 let y, h = getpageyh n in
667 gotoy_and_clear_text (y + top
);
671 let y, h = getpageyh n in
673 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
676 let gotopage1 n top
=
677 let y = getpagey n in
679 gotoy_and_clear_text (y + top
);
687 state.invalidated
<- state.invalidated
+ 1;
691 let c = c *. state.colorscale
in
696 state.maxy <- calcheight ();
697 match state.mode
with
698 | Birdseye
(_
, _
, pageno
, _
) ->
699 let y, h = getpageyh pageno
in
700 let top = (conf.winh
- h) / 2 in
701 gotoy (max
0 (y - top))
702 | _
-> gotoanchor state.anchor
706 GlMat.mode `projection
;
707 GlMat.load_identity
();
708 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
709 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
710 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
714 GlMat.mode `projection
;
715 GlMat.load_identity
();
716 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
717 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
718 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
722 if state.invalidated
= 0
723 then state.anchor <- getanchor ();
726 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
730 GlMat.mode `modelview
;
731 GlMat.load_identity
();
732 GlClear.color
(scalecolor 1.0);
733 GlClear.clear
[`color
];
736 wcmd "geometry" [`i
w; `i
h];
740 GlDraw.color
(0.0, 0.0, 0.0);
742 (0.0, float (conf.winh
- 18))
743 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
745 let font = Glut.BITMAP_8_BY_13
in
746 GlDraw.color
(1.0, 1.0, 1.0);
747 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
748 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
749 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
753 let len = String.length
state.text
in
754 match state.mode
with
755 | Textentry
(c, text
, _
, _
, _
) ->
759 text ^
" [" ^
state.text ^
"]"
766 if len > 0 then showtext ' '
state.text
772 state.text
<- Printf.sprintf
"%c%s" c s;
773 Glut.postRedisplay
();
787 state.rects
<- state.rects1
;
788 Glut.postRedisplay
()
791 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
792 state.pagecount
<- n;
793 state.invalidated
<- state.invalidated
- 1;
794 if state.invalidated
= 0
798 let s = Scanf.sscanf
cmd "t %n"
799 (fun n -> String.sub
cmd n (String.length
cmd - n))
801 Glut.setWindowTitle
s
804 let s = Scanf.sscanf
cmd "T %n"
805 (fun n -> String.sub
cmd n (String.length
cmd - n))
807 if istextentry state.mode
814 Glut.postRedisplay
();
820 let s = Scanf.sscanf
cmd "V %n"
821 (fun n -> String.sub
cmd n (String.length
cmd - n))
827 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
828 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
829 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
830 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
832 let y = (getpagey pageno) + truncate y0
in
835 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
838 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
839 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
840 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
841 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
844 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
847 let n, w, h, r
, l
, s, p
=
848 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
849 (fun n w h r l
s p
->
850 (n-1, w, h, r
, l
!= 0, s, p
))
853 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
854 state.memused
<- state.memused
+ s;
857 match state.throttle
with
858 | None
-> state.layout
859 | Some
layout -> layout
863 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
866 let evictedopaque = cbpeek state.pagecache
in
867 match findpageforopaque evictedopaque with
868 | None
-> failwith
"bug in gc"
869 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
870 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
872 wcmd "free" [`
s evictedopaque];
873 state.memused
<- state.memused
- evictedsize
;
874 Hashtbl.remove
state.pagemap k
;
875 cbdecr state.pagecache
;
882 cbput state.pagecache p
;
883 state.rendering
<- false;
885 begin match state.throttle
with
887 if pagevisible state.layout n
890 let allvisible = loadlayout state.layout in
891 if allvisible then preload ();
898 let y = getpagey l
.pageno + l
.pagey in
903 let (n, w, h, x
) as pdim
=
904 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
906 state.pdims
<- pdim
:: state.pdims
909 let (l
, n, t
, h, pos
) =
910 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
912 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
914 let l = String.length
s in
915 let b = Buffer.create
(String.length
s) in
922 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
925 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
934 let outline = (s, l, n, float t
/. float h) in
936 match state.outlines with
937 | Olist
outlines -> Olist
(outline :: outlines)
938 | Oarray _
-> Olist
[outline]
939 | Onarrow _
-> Olist
[outline]
941 state.outlines <- outlines
944 log "unknown cmd `%S'" cmd
947 let now = Unix.gettimeofday
;;
951 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
954 if conf.autoscroll
&& conf.scrollincr
!= 0
956 let y = state.y + conf.scrollincr
in
957 let y = if y >= state.maxy then 0 else y in
963 let cmd = readcmd state.csock
in
972 let action = function
973 | HCprev
-> cbget cb ~
-1
974 | HCnext
-> cbget cb
1
975 | HCfirst
-> cbget cb ~
-(cb
.rc)
976 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
977 and cancel
() = cb
.rc <- rc
981 let search pattern forward
=
982 if String.length pattern
> 0
985 match state.layout with
988 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
991 let b = makecmd "search"
992 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
994 Buffer.add_char
b '
,'
;
995 Buffer.add_string
b pattern
;
996 Buffer.add_char
b '
\000'
;
999 writecmd state.csock
cmd;
1002 let intentry text key
=
1003 let c = Char.unsafe_chr key
in
1006 let s = "x" in s.[0] <- c;
1007 let text = text ^
s in
1011 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1016 let b = Buffer.create
(String.length
s + 1) in
1017 Buffer.add_string
b s;
1018 Buffer.add_char
b c;
1022 let textentry text key
=
1023 let c = Char.unsafe_chr key
in
1025 | _
when key
>= 32 && key
< 127 ->
1026 let text = addchar text c in
1030 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1034 let reinit angle proportional
=
1035 conf.angle
<- angle
;
1036 conf.proportional
<- proportional
;
1038 wcmd "reinit" [`i angle
; `
b proportional
];
1041 let optentry text key
=
1042 let btos b = if b then "on" else "off" in
1043 let c = Char.unsafe_chr key
in
1047 try conf.scrollincr
<- int_of_string
s with exc
->
1048 state.text <- Printf.sprintf
"bad integer `%s': %s"
1049 s (Printexc.to_string exc
)
1051 TEswitch
('#'
, "", None
, intentry, ondone)
1056 Some
(int_of_string
s)
1058 state.text <- Printf.sprintf
"bad integer `%s': %s"
1059 s (Printexc.to_string exc
);
1062 | Some angle
-> reinit angle
conf.proportional
1065 TEswitch
('^'
, "", None
, intentry, ondone)
1068 conf.icase
<- not
conf.icase
;
1069 TEdone
("case insensitive search " ^
(btos conf.icase
))
1072 conf.preload <- not
conf.preload;
1074 TEdone
("preload " ^
(btos conf.preload))
1077 conf.verbose
<- not
conf.verbose
;
1078 TEdone
("verbose " ^
(btos conf.verbose
))
1081 conf.maxhfit
<- not
conf.maxhfit
;
1082 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1083 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1086 conf.crophack
<- not
conf.crophack
;
1087 TEdone
("crophack " ^
btos conf.crophack
)
1090 conf.showall
<- not
conf.showall
;
1091 TEdone
("showall " ^
btos conf.showall
)
1094 conf.underinfo
<- not
conf.underinfo
;
1095 TEdone
("underinfo " ^
btos conf.underinfo
)
1098 conf.savebmarks
<- not
conf.savebmarks
;
1099 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1105 match state.layout with
1110 conf.interpagespace <- int_of_string
s;
1111 state.maxy <- calcheight ();
1112 let y = getpagey pageno in
1115 state.text <- Printf.sprintf
"bad integer `%s': %s"
1116 s (Printexc.to_string exc
)
1118 TEswitch
('
%'
, "", None
, intentry, ondone)
1121 reinit conf.angle
(not
conf.proportional
);
1122 TEdone
("proprortional display " ^
btos conf.proportional
)
1125 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1129 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1131 let enterselector allowdel
outlines errmsg msg
=
1132 if Array.length
outlines = 0
1134 showtext ' ' errmsg
;
1138 Glut.setCursor
Glut.CURSOR_INHERIT
;
1140 match state.layout with
1142 | {pageno=pageno} :: rest
-> pageno
1146 if n = Array.length
outlines
1149 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1150 if outlinepageno
>= pageno then n else loop (n+1)
1154 state.mode
<- Outline
1155 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "");
1156 Glut.postRedisplay
();
1160 let enteroutlinemode () =
1162 match state.outlines with
1165 let a = Array.of_list
(List.rev
l) in
1166 state.outlines <- Oarray
a;
1168 | Onarrow
(pat
, a, b) ->
1169 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1171 enterselector false outlines "Document has no outline" msg
;
1174 let enterbookmarkmode () =
1175 let bookmarks = Array.of_list
state.bookmarks in
1176 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1179 let quickbookmark ?title
() =
1180 match state.layout with
1186 let sec = Unix.gettimeofday
() in
1187 let tm = Unix.localtime
sec in
1188 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1192 (tm.Unix.tm_year
+ 1900)
1195 | Some
title -> title
1198 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1202 state.fullscreen
<- None
;
1203 Glut.reshapeWindow
w h;
1206 let writeopen path password
=
1207 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1210 let opendoc path password
=
1213 state.password
<- password
;
1214 state.gen
<- state.gen
+ 1;
1216 writeopen path password
;
1217 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1218 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1222 let zoom = float conf.thumbw
/. float conf.winw
in
1223 let (birdseyepageno
, _
) as anchor = getanchor () in
1225 Birdseye
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno
, -1);
1227 conf.presentation <- false;
1228 conf.interpagespace <- 10;
1229 conf.hlinks
<- false;
1231 state.mstate
<- Mnone
;
1232 conf.showall
<- false;
1233 Glut.setCursor
Glut.CURSOR_INHERIT
;
1236 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1243 let birdseyeoff (c, leftx
, pageno, _
) =
1245 conf.zoom <- c.zoom;
1246 conf.presentation <- c.presentation;
1247 conf.interpagespace <- c.interpagespace;
1248 conf.showall
<- c.showall
;
1249 conf.hlinks
<- c.hlinks
;
1253 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1258 let togglebirdseye () =
1259 match state.mode
with
1260 | Birdseye vals
-> birdseyeoff vals
1261 | View
| Outline _
-> birdseyeon ()
1265 let viewkeyboard ~key ~x ~
y =
1267 state.mode
<- Textentry te
;
1270 Glut.postRedisplay
()
1272 let c = Char.chr key
in
1278 let y = getnav () in
1279 gotoy_and_clear_text y
1287 Glut.postRedisplay
()
1290 let ondone isforw
s =
1291 cbput state.hists
.pat
s;
1292 state.searchpattern
<- s;
1295 enttext (c, "", Some
(onhist state.hists
.pat
),
1296 textentry, ondone (c ='
/'
))
1298 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1299 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1300 conf.zoom <- min
2.2 (conf.zoom +. incr);
1301 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1302 reshape conf.winw
conf.winh
1307 try int_of_string
s with exc
->
1308 state.text <- Printf.sprintf
"bad integer `%s': %s"
1309 s (Printexc.to_string exc
);
1315 state.text <- "page bias is now " ^ string_of_int
n;
1318 enttext ('
+'
, "", None
, intentry, ondone)
1320 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1321 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1322 conf.zoom <- max
0.01 (conf.zoom -. decr);
1323 if conf.zoom <= 1.0 then state.x
<- 0;
1324 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1325 reshape conf.winw
conf.winh
;
1331 enttext ('
-'
, "", None
, optentry, ondone)
1333 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1336 state.text <- "zoom is 100%";
1337 reshape conf.winw
conf.winh
1339 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1340 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1345 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1346 reshape conf.winw
conf.winh
;
1349 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1351 reshape conf.winw
conf.winh
;
1356 try int_of_string
s with exc
->
1357 state.text <- Printf.sprintf
"bad integer `%s': %s"
1358 s (Printexc.to_string exc
);
1364 cbput state.hists
.pag
(string_of_int
n);
1365 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1368 let pageentry text key
=
1369 match Char.unsafe_chr key
with
1370 | 'g'
-> TEdone
text
1371 | _
-> intentry text key
1373 let text = "x" in text.[0] <- c;
1374 enttext ('
:'
, text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1377 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1378 reshape conf.winw
conf.winh
;
1381 conf.hlinks
<- not
conf.hlinks
;
1382 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1383 Glut.postRedisplay
()
1386 conf.autoscroll
<- not
conf.autoscroll
1389 conf.presentation <- not
conf.presentation;
1390 showtext ' '
("presentation mode " ^
1391 if conf.presentation then "on" else "off");
1395 begin match state.fullscreen
with
1397 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1400 state.fullscreen
<- None
;
1405 gotoy_and_clear_text 0
1408 search state.searchpattern
true
1411 search state.searchpattern
false
1414 begin match state.layout with
1417 gotoy_and_clear_text (getpagey l.pageno)
1421 begin match List.rev
state.layout with
1424 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1425 gotoy_and_clear_text (getpagey pageno)
1429 begin match state.layout with
1432 let pageno = max
0 (l.pageno-1) in
1433 gotoy_and_clear_text (getpagey pageno)
1438 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1440 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1442 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1446 else (100. *. (float state.y /. float maxy)) in
1449 Printf.sprintf
"Page %d of %d %.2f%%"
1450 (fn+1) state.pagecount
percent
1453 "Pages %d-%d of %d %.2f%%"
1454 (fn+1) (ln
+1) state.pagecount
percent
1459 begin match state.layout with
1462 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1463 Glut.postRedisplay
();
1467 enterbookmarkmode ()
1471 match state.layout with
1474 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1478 enttext ('~'
, "", None
, textentry, ondone)
1482 showtext ' '
"Quick bookmark added";
1485 begin match state.layout with
1487 let rect = getpdimrect
l.pagedimno
in
1491 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1492 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1494 (truncate
(rect.(1) -. rect.(0)),
1495 truncate
(rect.(3) -. rect.(0)))
1499 doreshape (w + conf.scrollw
) (h + conf.interpagespace)
1501 Glut.postRedisplay
();
1507 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1512 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1513 Glut.postRedisplay
()
1515 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1516 | 'j'
-> gotoy (clamp conf.scrollincr
)
1518 | '
r'
-> opendoc state.path
state.password
1521 vlog "huh? %d %c" key
(Char.chr key
);
1524 let textentrykeyboard ~key ~x ~
y (c, text, opthist
, onkey
, ondone) =
1526 state.mode
<- Textentry te
;
1529 Glut.postRedisplay
()
1531 match Char.unsafe_chr key
with
1533 let len = String.length
text in
1537 Glut.postRedisplay
();
1540 let s = String.sub
text 0 (len - 1) in
1541 enttext (c, s, opthist
, onkey
, ondone)
1547 Glut.postRedisplay
()
1550 begin match opthist
with
1552 | Some
(_
, onhistcancel
) -> onhistcancel
()
1555 Glut.postRedisplay
()
1558 begin match onkey
text key
with
1562 Glut.postRedisplay
()
1565 enttext (c, text, opthist
, onkey
, ondone);
1569 Glut.postRedisplay
()
1572 state.mode
<- Textentry te
;
1573 Glut.postRedisplay
()
1577 let birdseyekeyboard ~key ~x ~
y ((c, leftx
, pageno, hooverpageno
) as beye
) =
1581 reshape conf.winw
conf.winh
1584 let y, h = getpageyh pageno in
1585 let top = (conf.winh
- h) / 2 in
1586 gotoy (max
0 (y - top))
1591 reshape conf.winw
conf.winh
;
1592 state.anchor <- (pageno, 0.0);
1595 viewkeyboard ~key ~x ~
y
1598 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1599 let narrow outlines pattern
=
1600 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1604 let rec fold accu n =
1608 let (s, _
, _
, _
) as o
= outlines.(n) in
1610 if (try ignore
(Str.search_forward re
s 0); true
1611 with Not_found
-> false)
1617 let matched = fold [] (Array.length
outlines - 1) in
1618 if matched = [] then None
else Some
(Array.of_list
matched)
1620 let search active pattern
incr =
1623 if n = Array.length
outlines || n = -1
1626 let (s, _
, _
, _
) = outlines.(n) in
1628 (try ignore
(Str.search_forward re
s 0); true
1629 with Not_found
-> false)
1631 else loop (n + incr)
1636 let re = Str.regexp_case_fold pattern
in
1642 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1645 if String.length qsearch
= 0
1649 Glut.postRedisplay
();
1653 state.mode
<- Outline
(allowdel
, active, first
, outlines, "");
1654 Glut.postRedisplay
();
1658 let incr = if key
= 18 then -1 else 1 in
1660 match search (active + incr) qsearch
incr with
1662 state.text <- qsearch ^
" [not found]";
1665 state.text <- qsearch
;
1666 active, firstof active
1668 state.mode
<- Outline
(allowdel
, active, first
, outlines, qsearch
);
1669 Glut.postRedisplay
();
1672 let len = String.length qsearch
in
1679 state.mode
<- Outline
(allowdel
, active, first
, outlines, "");
1682 let qsearch = String.sub
qsearch 0 (len - 1) in
1684 match search active qsearch ~
-1 with
1686 state.text <- qsearch ^
" [not found]";
1689 state.text <- qsearch;
1690 active, firstof active
1692 state.mode
<- Outline
(allowdel
, active, first
, outlines, qsearch);
1694 Glut.postRedisplay
()
1697 if active < Array.length
outlines
1699 let (_
, _
, n, t
) = outlines.(active) in
1703 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1705 Glut.postRedisplay
();
1707 | _
when key
>= 32 && key
< 127 ->
1708 let pattern = addchar qsearch (Char.chr key
) in
1710 match search active pattern 1 with
1712 state.text <- pattern ^
" [not found]";
1715 state.text <- pattern;
1716 active, firstof active
1718 state.mode
<- Outline
(allowdel
, active, first
, outlines, pattern);
1719 Glut.postRedisplay
()
1721 | 14 when not allowdel
-> (* ctrl-n *)
1722 if String.length
qsearch > 0
1724 let optoutlines = narrow outlines qsearch in
1725 begin match optoutlines with
1726 | None
-> state.text <- "can't narrow"
1728 state.mode
<- Outline
(allowdel
, 0, 0, outlines, qsearch);
1729 match state.outlines with
1732 state.outlines <- Onarrow
(qsearch, outlines, a)
1733 | Onarrow
(pat
, a, b) ->
1734 state.outlines <- Onarrow
(qsearch, outlines, b)
1737 Glut.postRedisplay
()
1739 | 21 when not allowdel
-> (* ctrl-u *)
1741 match state.outlines with
1744 let a = Array.of_list
(List.rev
l) in
1745 state.outlines <- Oarray
a;
1747 | Onarrow
(pat
, a, b) ->
1748 state.outlines <- Oarray
b;
1752 state.mode
<- Outline
(allowdel
, 0, 0, outline, qsearch);
1753 Glut.postRedisplay
()
1756 state.mode
<- Outline
1757 (allowdel
, active, firstof active, outlines, qsearch);
1758 Glut.postRedisplay
()
1760 | 127 when allowdel
->
1761 let len = Array.length
outlines - 1 in
1765 state.bookmarks <- [];
1768 let bookmarks = Array.init
len
1770 let i = if i >= active then i + 1 else i in
1782 Glut.postRedisplay
()
1784 | _
-> log "unknown key %d" key
1787 let keyboard ~key ~x ~
y =
1792 match state.mode
with
1793 | Outline
outline -> outlinekeyboard ~key ~x ~
y outline
1794 | Textentry
textentry -> textentrykeyboard ~key ~x ~
y textentry
1795 | Birdseye birdseye
-> birdseyekeyboard ~key ~x ~
y birdseye
1796 | View
-> viewkeyboard ~key ~x ~
y
1799 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1802 let pageno = max
0 (pageno - 1) in
1803 let rec loop = function
1804 | [] -> gotopage1nonav pageno 0
1805 | l :: _
when l.pageno = pageno ->
1806 if l.pagedispy
>= 0 && l.pagey = 0
1807 then Glut.postRedisplay
()
1808 else gotopage1nonav pageno 0
1809 | _
:: rest
-> loop rest
1812 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
)
1815 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1816 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
);
1817 let rec loop = function
1819 let y, h = getpageyh pageno in
1820 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1822 | l :: rest
when l.pageno = pageno ->
1823 if l.pagevh != l.pageh
1824 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1825 else Glut.postRedisplay
()
1826 | l :: rest
-> loop rest
1830 | Glut.KEY_PAGE_UP
->
1831 begin match state.layout with
1835 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1836 gotopage1nonav l.pageno 0;
1839 let layout = layout (state.y-conf.winh
) conf.winh
in
1841 | [] -> gotoy (clamp (-conf.winh
))
1843 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1844 gotopage1nonav l.pageno 0
1847 | [] -> gotoy (clamp (-conf.winh
))
1850 | Glut.KEY_PAGE_DOWN
->
1851 begin match List.rev
state.layout with
1853 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1854 gotoy (clamp (l.pagedispy
+ l.pageh
))
1855 | [] -> gotoy (clamp conf.winh
)
1859 state.mode
<- Birdseye
(conf, leftx
, 0, hooverpageno
);
1863 let pageno = state.pagecount
- 1 in
1864 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
);
1865 if not
(pagevisible state.layout pageno)
1868 match List.rev
state.pdims
with
1870 | (_
, _
, h, _
) :: _
-> h
1872 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1873 else Glut.postRedisplay
();
1877 let special ~key ~x ~
y =
1878 match state.mode
with
1879 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
1881 reshape conf.winw
conf.winh
;
1884 birdseyespecial key x
y vals
1886 | View
| Textentry _
->
1887 begin match state.mode
with
1891 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1892 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1893 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1894 | Glut.KEY_PAGE_UP
->
1895 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1897 match state.layout with
1899 | l :: _
-> state.y - l.pagey
1902 | Glut.KEY_PAGE_DOWN
->
1903 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1905 match List.rev
state.layout with
1907 | l :: _
-> getpagey l.pageno
1910 | Glut.KEY_HOME
-> addnav (); 0
1913 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1915 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1916 state.x
<- state.x
- 10;
1918 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1919 state.x
<- state.x
+ 10;
1924 gotoy_and_clear_text y
1926 | Textentry
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1929 | Glut.KEY_UP
-> action HCprev
1930 | Glut.KEY_DOWN
-> action HCnext
1931 | Glut.KEY_HOME
-> action HCfirst
1932 | Glut.KEY_END
-> action HClast
1935 state.mode
<- Textentry
(c, s, onhist, onkey
, ondone);
1936 Glut.postRedisplay
()
1941 | Outline
(allowdel
, active, first
, outlines, qsearch) ->
1942 let maxrows = maxoutlinerows () in
1943 let calcfirst first
active =
1946 let rows = active - first
in
1947 if rows > maxrows then active - maxrows else first
1951 let active = active + incr in
1952 let active = max
0 (min
active (Array.length
outlines - 1)) in
1953 let first = calcfirst first active in
1954 state.mode
<- Outline
(allowdel
, active, first, outlines, qsearch);
1955 Glut.postRedisplay
()
1957 let updownlevel incr =
1958 let len = Array.length
outlines in
1959 let (_
, curlevel
, _
, _
) = outlines.(active) in
1961 if i = len then i-1 else if i = -1 then 0 else
1962 let (_
, l, _
, _
) = outlines.(i) in
1963 if l != curlevel
then i else flow (i+incr)
1965 let active = flow active in
1966 let first = calcfirst first active in
1967 state.mode
<- Outline
(allowdel
, active, first, outlines, qsearch);
1968 Glut.postRedisplay
()
1971 | Glut.KEY_UP
-> navigate ~
-1
1972 | Glut.KEY_DOWN
-> navigate 1
1973 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1974 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1976 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1977 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1980 state.mode
<- Outline
(allowdel
, 0, 0, outlines, qsearch);
1981 Glut.postRedisplay
()
1984 let active = Array.length
outlines - 1 in
1985 let first = max
0 (active - maxrows) in
1986 state.mode
<- Outline
(allowdel
, active, first, outlines, qsearch);
1987 Glut.postRedisplay
()
1992 let drawplaceholder l =
1993 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1995 (float l.pagex
, float l.pagedispy
)
1996 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1998 let x = float (if margin < 0 then -margin else l.pagex
)
1999 and y = float (l.pagedispy
+ 13) in
2000 let font = Glut.BITMAP_8_BY_13
in
2001 GlDraw.color
(0.0, 0.0, 0.0);
2002 GlPix.raster_pos ~
x ~
y ();
2003 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
2004 ("Loading " ^ string_of_int
(l.pageno + 1));
2007 let now () = Unix.gettimeofday
();;
2011 match state.mode
with
2012 | Textentry _
-> scalecolor 0.4
2013 | View
| Outline _
-> scalecolor 1.0
2014 | Birdseye
(_
, _
, pageno, hooverpageno
) ->
2015 if l.pageno = pageno
2018 if l.pageno = hooverpageno
2024 begin match getopaque l.pageno with
2025 | Some
(opaque, _
) when validopaque opaque ->
2027 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2031 vlog "draw %d %f sec" l.pageno d;
2039 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2040 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2041 let sh = float conf.winh
/. sh in
2042 let sh = max
sh (float conf.scrollh
) in
2045 if state.y = state.maxy
2047 else float y /. float maxy
2049 let position = (float conf.winh
-. sh) *. percent in
2052 if position +. sh > float conf.winh
2053 then float conf.winh
-. sh
2059 let scrollindicator () =
2060 GlDraw.color (0.64 , 0.64, 0.64);
2062 (float (conf.winw
- conf.scrollw
), 0.)
2063 (float conf.winw
, float conf.winh
)
2065 GlDraw.color (0.0, 0.0, 0.0);
2067 let position, sh = scrollph state.y in
2069 (float (conf.winw
- conf.scrollw
), position)
2070 (float conf.winw
, position +. sh)
2074 let showsel margin =
2075 match state.mstate
with
2076 | Mnone
| Mscroll _
| Mpan _
->
2079 | Msel
((x0
, y0
), (x1
, y1
)) ->
2080 let rec loop = function
2082 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2083 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2085 match getopaque l.pageno with
2086 | Some
(opaque, _
) when validopaque opaque ->
2087 let oy = -l.pagey + l.pagedispy
in
2089 (x0
- margin - state.x, y0
,
2090 x1
- margin - state.x, y1
) oy;
2100 let panx = float state.x in
2102 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2103 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2105 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2107 if l.pageno = pageno
2109 let d = float (l.pagedispy
- l.pagey) in
2110 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2111 GlDraw.begins `quads
;
2113 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2114 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2115 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2116 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2126 let showoutline = function
2127 | Outline
(allowdel
, active, first, outlines, qsearch) ->
2129 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2130 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2131 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2134 GlDraw.color (1., 1., 1.);
2135 let font = Glut.BITMAP_9_BY_15
in
2136 let draw_string x y s =
2137 GlPix.raster_pos ~
x ~
y ();
2138 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2141 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2144 let (s, l, _
, _
) = outlines.(row
) in
2145 let y = (row
- first) * 16 in
2150 GlDraw.polygon_mode `both `line
;
2151 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2152 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2153 GlDraw.rect (0., float (y + 1))
2154 (float (conf.winw
- 1), float (y + 18));
2155 GlDraw.polygon_mode `both `fill
;
2157 GlDraw.color (1., 1., 1.);
2159 draw_string (float x) (float (y + 16)) s;
2169 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2170 GlDraw.viewport
margin 0 state.w conf.winh
;
2172 GlClear.color (scalecolor 0.5);
2173 GlClear.clear
[`
color];
2176 let x = float state.x in
2177 GlMat.translate ~
x ();
2181 Gl.enable `scissor_test
;
2182 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2184 List.iter
drawpage state.layout;
2187 Gl.disable `scissor_test
2191 let x = -.float state.x in
2192 GlMat.translate ~
x ();
2196 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2199 showoutline state.mode
;
2201 Glut.swapBuffers
();
2205 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2206 let x = x - margin - state.x in
2207 let rec f = function
2209 begin match getopaque l.pageno with
2210 | Some
(opaque, _
) when validopaque opaque ->
2211 let y = y - l.pagedispy
in
2214 let y = l.pagey + y in
2215 let x = x - l.pagex
in
2216 match whatsunder
opaque x y with
2229 let mouse ~button ~bstate ~
x ~
y =
2231 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2239 let incr = incr * 2 in
2240 let y = clamp incr in
2241 gotoy_and_clear_text y
2243 | Glut.LEFT_BUTTON
when not
(isoutline state.mode
)
2244 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2245 if bstate
= Glut.DOWN
2247 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2248 state.mstate
<- Mpan
(x, y)
2251 state.mstate
<- Mnone
2254 when not
(isoutline state.mode
) && x > conf.winw
- conf.scrollw
->
2255 if bstate
= Glut.DOWN
2257 let position, sh = scrollph state.y in
2258 if y > truncate
position && y < truncate
(position +. sh)
2260 state.mstate
<- Mscroll
2262 let percent = float y /. float conf.winh
in
2263 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2265 state.mstate
<- Mscroll
2267 state.mstate
<- Mnone
2270 when not
(isoutline state.mode
) && (isbirdseye state.mode
) ->
2271 begin match state.mode
with
2272 | Birdseye
(conf, leftx
, pageno, hooverpageno
) ->
2273 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2274 let rec loop = function
2277 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2278 && x > margin && x < margin + l.pagew
2280 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2281 reshape conf.winw
conf.winh
;
2282 state.anchor <- (l.pageno, 0.0);
2287 | _
-> () (* impossible *)
2290 | Glut.LEFT_BUTTON
when not
(isoutline state.mode
) ->
2291 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2292 begin match dest with
2293 | Ulinkgoto
(pageno, top) ->
2296 gotopage1 pageno top
2301 | Unone
when bstate
= Glut.DOWN
->
2302 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2303 state.mstate
<- Mpan
(x, y);
2305 | Unone
| Utext _
->
2306 if bstate
= Glut.DOWN
2308 if conf.angle
mod 360 = 0
2310 state.mstate
<- Msel
((x, y), (x, y));
2311 Glut.postRedisplay
()
2315 match state.mstate
with
2319 state.mstate
<- Mnone
2322 Glut.setCursor
Glut.CURSOR_INHERIT
;
2323 state.mstate
<- Mnone
2325 | Msel
((x0
, y0
), (x1
, y1
)) ->
2327 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2328 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2330 match getopaque l.pageno with
2331 | Some
(opaque, _
) when validopaque opaque ->
2335 List.iter
f state.layout;
2336 copysel
""; (* ugly *)
2337 Glut.setCursor
Glut.CURSOR_INHERIT
;
2338 state.mstate
<- Mnone
;
2345 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2348 match state.mode
with
2351 match state.mstate
with
2357 state.mstate
<- Mpan
(x, y);
2358 if conf.zoom > 1.0 then state.x <- state.x + dx;
2360 gotoy_and_clear_text y
2363 state.mstate
<- Msel
(a, (x, y));
2364 Glut.postRedisplay
()
2367 let y = min
conf.winh
(max
0 y) in
2368 let percent = float y /. float conf.winh
in
2369 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2370 gotoy_and_clear_text y
2374 match state.mode
with
2375 | Birdseye
(conf, leftx
, pageno, hooverpageno
) ->
2376 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2377 let rec loop = function
2379 if hooverpageno
!= -1
2381 state.mode
<- Birdseye
(conf, leftx
, pageno, -1);
2382 Glut.postRedisplay
();
2385 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2386 && x > margin && x < margin + l.pagew
2388 state.mode
<- Birdseye
(conf, leftx
, pageno, l.pageno);
2389 Glut.postRedisplay
();
2397 match state.mstate
with
2399 begin match getunder x y with
2400 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2402 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2403 Glut.setCursor
Glut.CURSOR_INFO
2404 | Ulinkgoto
(page
, y) ->
2406 then showtext 'p'
("age: " ^ string_of_int page
);
2407 Glut.setCursor
Glut.CURSOR_INFO
2409 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2410 Glut.setCursor
Glut.CURSOR_TEXT
2413 | Mpan _
| Msel _
| Mscroll
->
2424 match Sys.os_type
with
2425 | "Win32" -> Sys.getenv
"HOMEPATH"
2426 | _
-> Sys.getenv
"HOME"
2429 ("Can not determine home directory location: " ^
2430 Printexc.to_string exn
);
2434 let config_of c attrs
=
2438 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2439 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2440 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2441 | "preload" -> { c with preload = bool_of_string v
}
2442 | "page-bias" -> { c with pagebias
= int_of_string v
}
2443 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2444 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2445 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2446 | "throttle" -> { c with showall
= bool_of_string v
}
2447 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2448 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2449 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2451 let zoom = float_of_string v
/. 100. in
2452 let zoom = max
0.01 (min
2.2 zoom) in
2453 { c with zoom = zoom }
2454 | "presentation" -> { c with presentation = bool_of_string v
}
2455 | "rotation-angle" -> { c with angle
= int_of_string v
}
2456 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2457 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2458 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2459 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2460 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2461 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2462 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2463 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2466 prerr_endline
("Error processing attribute (`" ^
2467 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2470 let rec fold c = function
2473 let c = apply c k v
in
2479 let bookmark_of attrs
=
2480 let rec fold title page rely
= function
2481 | ("title", v
) :: rest
-> fold v page rely rest
2482 | ("page", v
) :: rest
-> fold title v rely rest
2483 | ("rely", v
) :: rest
-> fold title page v rest
2484 | _
:: rest
-> fold title page rely rest
2485 | [] -> title, page
, rely
2487 fold "invalid" "0" "0" attrs
2490 let setconf dst src
=
2491 dst
.scrollw
<- src
.scrollw
;
2492 dst
.scrollh
<- src
.scrollh
;
2493 dst
.icase
<- src
.icase
;
2494 dst
.preload <- src
.preload;
2495 dst
.pagebias
<- src
.pagebias
;
2496 dst
.verbose
<- src
.verbose
;
2497 dst
.scrollincr
<- src
.scrollincr
;
2498 dst
.maxhfit
<- src
.maxhfit
;
2499 dst
.crophack
<- src
.crophack
;
2500 dst
.autoscroll
<- src
.autoscroll
;
2501 dst
.showall
<- src
.showall
;
2502 dst
.hlinks
<- src
.hlinks
;
2503 dst
.underinfo
<- src
.underinfo
;
2504 dst
.interpagespace <- src
.interpagespace;
2505 dst
.zoom <- src
.zoom;
2506 dst
.presentation <- src
.presentation;
2507 dst
.angle
<- src
.angle
;
2508 dst
.winw
<- src
.winw
;
2509 dst
.winh
<- src
.winh
;
2510 dst
.savebmarks
<- src
.savebmarks
;
2511 dst
.memlimit
<- src
.memlimit
;
2512 dst
.proportional
<- src
.proportional
;
2513 dst
.texcount
<- src
.texcount
;
2514 dst
.sliceheight
<- src
.sliceheight
;
2515 dst
.thumbw
<- src
.thumbw
;
2519 let l = String.length
s in
2520 let b = Buffer.create
l in
2526 let h = Hashtbl.create
10 in
2527 let dc = { defconf with angle
= defconf.angle
} in
2528 let rec toplevel v t spos epos
=
2530 | Vdata
| Vcdata
| Vend
-> v
2531 | Vopen
("llppconfig", attrs
, closed
) ->
2534 else { v
with f = llppconfig
}
2536 error
"unexpected subelement at top level" s spos
2537 | Vclose tag
-> error
"unexpected close at top level" s spos
2539 and llppconfig v t spos epos
=
2541 | Vdata
| Vcdata
| Vend
-> v
2542 | Vopen
("defaults", attrs
, closed
) ->
2543 let c = config_of dc attrs
in
2547 else { v
with f = skip
"defaults" (fun () -> v
) }
2549 | Vopen
("doc", attrs
, closed
) ->
2552 List.assoc
"path" attrs
2553 with Not_found
-> error
"doc is missing path attribute" s spos
2555 let path = unent pathent in
2556 let c = config_of dc attrs
in
2557 let pageno, rely
, x =
2561 dolog "error accessing %s (%S) at postion %d:\n %s"
2562 n v spos
(Printexc.to_string exn
);
2565 let rec fold pageno rely
x = function
2566 | [] -> pageno, rely
, x
2567 | ("rely", v
) :: rest
->
2568 fold pageno (safef float_of_string
"rely" v
0.0) x rest
2569 | ("page", v
) :: rest
->
2570 fold (safef int_of_string
"page" v
0) rely
x rest
2571 | ("x", v
) :: rest
->
2572 fold pageno rely
(safef int_of_string
"x" v
0) rest
2574 fold pageno rely
x rest
2578 let anchor = (pageno, rely
) in
2580 then (Hashtbl.add
h path (c, [], x, anchor); v
)
2581 else { v
with f = doc
path x anchor c [] }
2583 | Vopen
(tag
, _
, closed
) ->
2584 error
"unexpected subelement in llppconfig" s spos
2586 | Vclose
"llppconfig" -> { v
with f = toplevel }
2587 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2589 and doc
path x anchor c bookmarks v t spos epos
=
2591 | Vdata
| Vcdata
-> v
2592 | Vend
-> error
"unexpected end of input in doc" s spos
2593 | Vopen
("bookmarks", attrs
, closed
) ->
2594 { v
with f = pbookmarks
path x anchor c bookmarks }
2596 | Vopen
(tag
, _
, _
) ->
2597 error
"unexpected subelement in doc" s spos
2600 Hashtbl.add
h path (c, List.rev
bookmarks, x, anchor);
2601 { v
with f = llppconfig
}
2603 | Vclose tag
-> error
"unexpected close in doc" s spos
2605 and pbookmarks
path x anchor c bookmarks v t spos epos
=
2607 | Vdata
| Vcdata
-> v
2608 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2609 | Vopen
("item", attrs
, closed
) ->
2610 let titleent, spage
, srely
= bookmark_of attrs
in
2615 dolog "Failed to convert page %S to integer: %s"
2616 spage
(Printexc.to_string exn
);
2621 float_of_string srely
2623 dolog "Failed to convert rely %S to real: %s"
2624 srely
(Printexc.to_string exn
);
2627 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2629 then { v
with f = pbookmarks
path x anchor c bookmarks }
2632 { v
with f = skip
"item" f }
2635 error
"unexpected subelement in bookmarks" s spos
2637 | Vclose
"bookmarks" ->
2638 { v
with f = doc
path x anchor c bookmarks }
2640 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2642 and skip tag
f v t spos epos
=
2644 | Vdata
| Vcdata
-> v
2646 error
("unexpected end of input in skipped " ^ tag
) s spos
2647 | Vopen
(tag'
, _
, closed
) ->
2651 let f'
() = { v
with f = skip tag
f } in
2652 { v
with f = skip tag'
f'
}
2656 else error
("unexpected close in skipped " ^ tag
) s spos
2659 parse
{ f = toplevel; accu = () } s;
2665 let len = in_channel_length ic
in
2666 let s = String.create
len in
2667 really_input ic
s 0 len;
2670 | Parse_error
(msg
, s, pos
) ->
2671 let subs = subs s pos
in
2672 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2673 failwith
("parse error: " ^
s)
2676 failwith
("config load error: " ^
Printexc.to_string exn
)
2682 let dir = Filename.concat
home ".config" in
2683 if Sys.is_directory
dir then dir else home
2686 Filename.concat
dir "llpp.conf"
2690 if Sys.file_exists
path
2693 (try Some
(open_in_bin
path)
2696 ("Error opening configuation file `" ^
path ^
"': " ^
2697 Printexc.to_string exn
);
2706 ("Error loading configuation from `" ^
path ^
"': " ^
2707 Printexc.to_string exn
);
2713 f (Hashtbl.create
0, defconf)
2718 let pc, pb
, px
, pa
=
2720 Hashtbl.find
h (Filename.basename
state.path)
2721 with Not_found
-> dc, [], 0, (0, 0.0)
2725 state.bookmarks <- pb
;
2727 cbput state.hists
.nav pa
;
2732 let add_attrs bb always
dc c =
2735 then Printf.bprintf bb
"\n %s='%b'" s a
2738 then Printf.bprintf bb
"\n %s='%d'" s a
2741 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2745 then dc.winw
, dc.winh
2747 match state.fullscreen
with
2749 | None
-> c.winw
, c.winh
2751 let zoom, presentation, interpagespace, showall
=
2753 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2755 match state.mode
with
2756 | Birdseye
(bc
, _
, _
, _
) ->
2757 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2758 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2760 oi
"width" w dc.winw
;
2761 oi
"height" h dc.winh
;
2762 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2763 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2764 ob "case-insensitive-search" c.icase
dc.icase
;
2765 ob "preload" c.preload dc.preload;
2766 oi
"page-bias" c.pagebias
dc.pagebias
;
2767 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2768 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2769 ob "crop-hack" c.crophack
dc.crophack
;
2770 ob "throttle" showall
dc.showall
;
2771 ob "highlight-links" c.hlinks
dc.hlinks
;
2772 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2773 oi
"vertical-margin" interpagespace dc.interpagespace;
2774 oz
"zoom" zoom dc.zoom;
2775 ob "presentation" presentation dc.presentation;
2776 oi
"rotation-angle" c.angle
dc.angle
;
2777 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2778 ob "proportional-display" c.proportional
dc.proportional
;
2779 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2780 oi
"texcount" c.texcount
dc.texcount
;
2781 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2782 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2786 let bb = Buffer.create
32768 in
2788 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2789 add_attrs bb true dc dc;
2790 Buffer.add_string
bb "/>\n";
2792 let adddoc path x anchor c bookmarks =
2793 if bookmarks == [] && c = dc && anchor = emptyanchor
2796 Printf.bprintf
bb "<doc path='%s'"
2797 (enent
path 0 (String.length
path));
2799 if anchor <> emptyanchor
2801 let n, y = anchor in
2802 Printf.bprintf
bb " page='%d'" n;
2803 Printf.bprintf
bb " rely='%f'" y;
2807 then Printf.bprintf
bb " pan='%d'" x;
2809 add_attrs bb false dc c;
2811 begin match bookmarks with
2812 | [] -> Buffer.add_string
bb "/>\n"
2814 Buffer.add_string
bb ">\n<bookmarks>\n";
2815 List.iter
(fun (title, _level
, page, rely) ->
2817 "<item title='%s' page='%d' rely='%f'/>\n"
2818 (enent
title 0 (String.length
title))
2822 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2828 match state.mode
with
2829 | Birdseye
(_
, x, _
, _
) -> x
2832 let basename = Filename.basename state.path in
2833 adddoc basename x (getanchor ()) conf
2834 (if conf.savebmarks
then state.bookmarks else []);
2836 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2838 then adddoc path x y c bookmarks
2840 Buffer.add_string
bb "</llppconfig>";
2843 if Buffer.length
bb > 0
2846 let tmp = path ^
".tmp" in
2847 let oc = open_out_bin
tmp in
2848 Buffer.output_buffer
oc bb;
2850 Sys.rename
tmp path;
2853 ("error while saving configuration: " ^
Printexc.to_string exn
)
2859 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2860 (fun s -> state.path <- s)
2861 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2863 if String.length
state.path = 0
2864 then (prerr_endline
"filename missing"; exit
1);
2868 let _ = Glut.init
Sys.argv
in
2869 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2870 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2871 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
2874 if Sys.os_type
= "Unix"
2876 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2878 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2879 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2880 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2881 Unix.bind
sock addr;
2883 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2884 Unix.connect
csock addr;
2885 let ssock, _ = Unix.accept
sock in
2888 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2889 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2893 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2897 let () = Glut.displayFunc
display in
2898 let () = Glut.reshapeFunc
reshape in
2899 let () = Glut.keyboardFunc
keyboard in
2900 let () = Glut.specialFunc
special in
2901 let () = Glut.idleFunc
(Some
idle) in
2902 let () = Glut.mouseFunc
mouse in
2903 let () = Glut.motionFunc
motion in
2904 let () = Glut.passiveMotionFunc
pmotion in
2906 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2907 state.csock <- csock;
2908 state.ssock <- ssock;
2909 state.text <- "Opening " ^
state.path;
2910 writeopen state.path state.password
;
2914 let rec handlelablglutbug () =
2917 with Glut.BadEnum
"key in special_of_int" ->
2918 showtext '
!'
" LablGlut bug: special key not recognized";
2919 handlelablglutbug ()
2921 handlelablglutbug ();