4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
10 type params
= angle
* proportional
* texcount
* sliceheight
19 and proportional
= bool
20 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
155 ; mutable thumbw
: width
159 type outline
= string * int * int * float;;
161 | Oarray
of outline array
162 | Olist
of outline list
163 | Onarrow
of string * outline array
* outline array
166 type rect
= (float * float * float * float * float * float * float * float);;
168 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
170 type anchor
= pageno
* top
;;
173 | Birdseye
of (conf
* leftx
* pageno
* pageno
)
174 | Outline
of (bool * int * int * outline array
* string)
175 | Textentry
of (textentry
* mode
)
179 let isbirdseye = function Birdseye _
-> true | _
-> false;;
180 let istextentry = function Textentry _
-> true | _
-> false;;
183 { mutable csock
: Unix.file_descr
184 ; mutable ssock
: Unix.file_descr
188 ; mutable anchor
: anchor
190 ; mutable layout
: layout list
191 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
192 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
193 ; mutable pagecount
: int
194 ; pagecache
: string circbuf
195 ; mutable rendering
: bool
196 ; mutable mstate
: mstate
197 ; mutable searchpattern
: string
198 ; mutable rects
: (pageno
* recttype
* rect
) list
199 ; mutable rects1
: (pageno
* recttype
* rect
) list
200 ; mutable text
: string
201 ; mutable fullscreen
: (width
* height
) option
202 ; mutable mode
: mode
203 ; mutable outlines
: outlines
204 ; mutable bookmarks
: outline list
205 ; mutable path
: string
206 ; mutable password
: string
207 ; mutable invalidated
: int
208 ; mutable colorscale
: float
209 ; mutable memused
: int
211 ; mutable throttle
: layout list
option
215 { pat
: string circbuf
216 ; pag
: string circbuf
217 ; nav
: anchor circbuf
237 ; presentation
= false
242 ; proportional
= true
243 ; memlimit
= 32*1024*1024
250 let conf = { defconf with angle
= defconf.angle
};;
261 ; pagemap
= Hashtbl.create
10
262 ; pagecache
= cbnew 100 ""
273 ; outlines
= Olist
[]
279 { nav
= cbnew 100 (0, 0.0)
293 Printf.kprintf prerr_endline fmt
295 Printf.kprintf ignore fmt
299 let len = String.length s
in
301 let b = Buffer.create
n in
302 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
303 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
304 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
305 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
306 Buffer.add_string
b s
;
307 let s'
= Buffer.contents
b in
308 let n'
= Unix.write fd
s'
0 n in
309 if n'
!= n then failwith
"write failed";
314 let n = Unix.read fd
s 0 4 in
315 if n != 4 then failwith
"incomplete read(len)";
317 lor (Char.code
s.[0] lsl 24)
318 lor (Char.code
s.[1] lsl 16)
319 lor (Char.code
s.[2] lsl 8)
320 lor (Char.code
s.[3] lsl 0)
322 let s = String.create
len in
323 let n = Unix.read fd
s 0 len in
324 if n != len then failwith
"incomplete read(data)";
329 let b = Buffer.create
10 in
330 Buffer.add_string
b s;
331 let rec combine = function
334 Buffer.add_char
b ' '
;
337 | `
b b -> if b then "1" else "0"
339 | `i i
-> string_of_int i
340 | `f f
-> string_of_float f
341 | `I f
-> string_of_int
(truncate f
)
343 Buffer.add_string
b s;
350 let cmd = Buffer.contents
(makecmd s l
) in
351 writecmd state.csock
cmd;
357 let d = conf.winh
- h
in
364 let rec f pn ph pi fh l
=
366 | (n, _
, h
, _
) :: rest
->
367 let ips = calcips h
in
372 if isbirdseye state.mode
&& pn
= 0
377 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
382 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
386 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
389 let fh = f 0 0 0 0 state.pdims
in
393 let getpageyh pageno
=
394 let rec f pn ph pi y l
=
396 | (n, _
, h
, _
) :: rest
->
397 let ips = calcips h
in
400 let h = if n = pageno
then h else ph
in
401 if conf.presentation
&& n = pageno
403 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
405 y
+ (pageno
- pn
) * (ph
+ pi
), h
407 let y = y + (if conf.presentation
then pi
else 0) in
408 let y = y + (n - pn
) * (ph
+ pi
) in
412 y + (pageno
- pn
) * (ph
+ pi
), ph
414 f 0 0 0 0 state.pdims
417 let getpagey pageno
= fst
(getpageyh pageno
);;
420 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
421 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
423 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
424 let ips = calcips h in
426 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
430 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
432 prev
, pdims
, pdimno
, 0
434 let dy = dy + yinc in
435 let py = py + yinc in
436 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
441 if py + h <= vy - yinc
443 let py = py + h + ips in
444 let dy = max
0 (py - y) in
454 let pagey = vy - py in
455 let pagevh = h - pagey in
456 let pagevh = min
(sh
- dy) pagevh in
457 let off = if yinc > 0 then py - vy else 0 in
458 let py = py + h + ips in
464 ; pagedispy
= dy + off
465 ; pagey = pagey + off
466 ; pagevh = pagevh - off
470 let accu = e :: accu in
477 ~cacheleft
:(cacheleft
-1)
480 if state.invalidated
= 0
490 ~cacheleft
:(cbcap state.pagecache
)
500 let y = state.y + incr
in
502 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
506 let getopaque pageno
=
507 try Some
(Hashtbl.find
state.pagemap
508 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
509 with Not_found
-> None
512 let cache pageno opaque
=
513 Hashtbl.replace
state.pagemap
514 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
517 let validopaque opaque
= String.length opaque
> 0;;
520 match getopaque l
.pageno
with
521 | None
when not
state.rendering
->
522 state.rendering
<- true;
523 cache l
.pageno
("", -1);
524 wcmd "render" [`i
(l
.pageno
+ 1)
532 let loadlayout layout =
533 let rec f all
= function
535 begin match getopaque l
.pageno
with
536 | None
-> render l
; f false ls
537 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
541 f (layout <> []) layout;
544 let findpageforopaque opaque
=
546 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
550 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
556 let memleft = conf.memlimit
- state.memused
in
559 let opaque = cbpeek state.pagecache
in
560 match findpageforopaque opaque with
561 | Some
((n, _
, _
, _
, _
), size
) ->
562 memleft + size
>= 0 && not
(pagevisible state.layout n)
569 let presentation = conf.presentation in
570 let interpagespace = conf.interpagespace in
571 let maxy = state.maxy in
572 conf.presentation <- false;
573 conf.interpagespace <- 0;
574 state.maxy <- calcheight ();
576 match state.layout with
578 | l
:: _
-> getpagey l
.pageno
580 let y = if y < conf.winh
then 0 else y - conf.winh
in
581 let pages = layout y (conf.winh
*3) in
582 List.iter
render pages;
583 conf.presentation <- presentation;
584 conf.interpagespace <- interpagespace;
590 let y = min
state.maxy y in
591 let pages = layout y conf.winh
in
592 let ready = loadlayout pages in
598 state.layout <- pages;
599 state.throttle
<- None
;
600 Glut.postRedisplay
();
603 state.throttle
<- Some
pages;
608 state.layout <- pages;
609 state.throttle
<- None
;
610 Glut.postRedisplay
();
612 begin match state.mode
with
613 | Birdseye
(conf, leftx
, pageno
, hooverpageno
) ->
614 if not
(pagevisible pages pageno
)
616 match state.layout with
619 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
)
626 let gotoy_and_clear_text y =
628 if not
conf.verbose
then state.text
<- "";
631 let emptyanchor = (0, 0.0);;
634 match state.layout with
636 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
639 let getanchory (n, top
) =
640 let y, h = getpageyh n in
641 y + (truncate
(top
*. float h));
644 let gotoanchor anchor
=
645 gotoy (getanchory anchor
);
649 cbput state.hists
.nav
(getanchor ());
653 let anchor = cbgetc state.hists
.nav ~
-1 in
657 let gotopagenonav n top
=
658 let y, h = getpageyh n in
659 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
662 let gotopage1nonav n top
=
663 let y, h = getpageyh n in
665 gotoy_and_clear_text (y + top
);
669 let y, h = getpageyh n in
671 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
674 let gotopage1 n top
=
675 let y = getpagey n in
677 gotoy_and_clear_text (y + top
);
685 state.invalidated
<- state.invalidated
+ 1;
689 let c = c *. state.colorscale
in
694 state.maxy <- calcheight ();
695 match state.mode
with
696 | Birdseye
(_
, _
, pageno
, _
) ->
697 let y, h = getpageyh pageno
in
698 let top = (conf.winh
- h) / 2 in
699 gotoy (max
0 (y - top))
700 | _
-> gotoanchor state.anchor
704 GlMat.mode `projection
;
705 GlMat.load_identity
();
706 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
707 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
708 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
711 GlMat.translate ~x
:(float state.x
) ();
716 GlMat.mode `projection
;
717 GlMat.load_identity
();
718 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
719 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
720 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
724 if state.invalidated
= 0
725 then state.anchor <- getanchor ();
728 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
732 GlMat.mode `modelview
;
733 GlMat.load_identity
();
734 GlClear.color
(scalecolor 1.0);
735 GlClear.clear
[`color
];
738 wcmd "geometry" [`i
w; `i
h];
742 GlDraw.color
(0.0, 0.0, 0.0);
744 (0.0, float (conf.winh
- 18))
745 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
747 let font = Glut.BITMAP_8_BY_13
in
748 GlDraw.color
(1.0, 1.0, 1.0);
749 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
750 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
751 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
755 let len = String.length
state.text
in
756 match state.mode
with
757 | Textentry
((c, text
, _
, _
, _
), _
) ->
761 text ^
" [" ^
state.text ^
"]"
768 if len > 0 then showtext ' '
state.text
774 state.text
<- Printf.sprintf
"%c%s" c s;
775 Glut.postRedisplay
();
789 state.rects
<- state.rects1
;
790 Glut.postRedisplay
()
793 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
794 state.pagecount
<- n;
795 state.invalidated
<- state.invalidated
- 1;
796 if state.invalidated
= 0
800 let s = Scanf.sscanf
cmd "t %n"
801 (fun n -> String.sub
cmd n (String.length
cmd - n))
803 Glut.setWindowTitle
s
806 let s = Scanf.sscanf
cmd "T %n"
807 (fun n -> String.sub
cmd n (String.length
cmd - n))
809 if istextentry state.mode
816 Glut.postRedisplay
();
822 let s = Scanf.sscanf
cmd "V %n"
823 (fun n -> String.sub
cmd n (String.length
cmd - n))
829 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
830 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
831 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
832 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
834 let y = (getpagey pageno) + truncate y0
in
837 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
840 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
841 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
842 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
843 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
846 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
849 let n, w, h, r
, l
, s, p
=
850 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
851 (fun n w h r l
s p
->
852 (n-1, w, h, r
, l
!= 0, s, p
))
855 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
856 state.memused
<- state.memused
+ s;
859 match state.throttle
with
860 | None
-> state.layout
861 | Some
layout -> layout
865 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
868 let evictedopaque = cbpeek state.pagecache
in
869 match findpageforopaque evictedopaque with
870 | None
-> failwith
"bug in gc"
871 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
872 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
874 wcmd "free" [`
s evictedopaque];
875 state.memused
<- state.memused
- evictedsize
;
876 Hashtbl.remove
state.pagemap k
;
877 cbdecr state.pagecache
;
884 cbput state.pagecache p
;
885 state.rendering
<- false;
887 begin match state.throttle
with
889 if pagevisible state.layout n
892 let allvisible = loadlayout state.layout in
893 if allvisible then preload ();
900 let y = getpagey l
.pageno + l
.pagey in
905 let (n, w, h, x
) as pdim
=
906 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
908 state.pdims
<- pdim
:: state.pdims
911 let (l
, n, t
, h, pos
) =
912 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
914 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
916 let l = String.length
s in
917 let b = Buffer.create
(String.length
s) in
924 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
927 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
936 let outline = (s, l, n, float t
/. float h) in
938 match state.outlines with
939 | Olist
outlines -> Olist
(outline :: outlines)
940 | Oarray _
-> Olist
[outline]
941 | Onarrow _
-> Olist
[outline]
943 state.outlines <- outlines
946 dolog "unknown cmd `%S'" cmd
949 let now = Unix.gettimeofday
;;
953 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
956 if conf.autoscroll
&& conf.scrollincr
!= 0
958 let y = state.y + conf.scrollincr
in
959 let y = if y >= state.maxy then 0 else y in
965 let cmd = readcmd state.csock
in
974 let action = function
975 | HCprev
-> cbget cb ~
-1
976 | HCnext
-> cbget cb
1
977 | HCfirst
-> cbget cb ~
-(cb
.rc)
978 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
979 and cancel
() = cb
.rc <- rc
983 let search pattern forward
=
984 if String.length pattern
> 0
987 match state.layout with
990 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
993 let b = makecmd "search"
994 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
996 Buffer.add_char
b '
,'
;
997 Buffer.add_string
b pattern
;
998 Buffer.add_char
b '
\000'
;
1001 writecmd state.csock
cmd;
1004 let intentry text key
=
1005 let c = Char.unsafe_chr key
in
1008 let s = "x" in s.[0] <- c;
1009 let text = text ^
s in
1013 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1018 let b = Buffer.create
(String.length
s + 1) in
1019 Buffer.add_string
b s;
1020 Buffer.add_char
b c;
1024 let textentry text key
=
1025 let c = Char.unsafe_chr key
in
1027 | _
when key
>= 32 && key
< 127 ->
1028 let text = addchar text c in
1032 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1036 let reinit angle proportional
=
1037 conf.angle
<- angle
;
1038 conf.proportional
<- proportional
;
1040 wcmd "reinit" [`i angle
; `
b proportional
];
1043 let optentry text key
=
1044 let btos b = if b then "on" else "off" in
1045 let c = Char.unsafe_chr key
in
1049 try conf.scrollincr
<- int_of_string
s with exc
->
1050 state.text <- Printf.sprintf
"bad integer `%s': %s"
1051 s (Printexc.to_string exc
)
1053 TEswitch
('#'
, "", None
, intentry, ondone)
1058 Some
(int_of_string
s)
1060 state.text <- Printf.sprintf
"bad integer `%s': %s"
1061 s (Printexc.to_string exc
);
1064 | Some angle
-> reinit angle
conf.proportional
1067 TEswitch
('^'
, "", None
, intentry, ondone)
1070 conf.icase
<- not
conf.icase
;
1071 TEdone
("case insensitive search " ^
(btos conf.icase
))
1074 conf.preload <- not
conf.preload;
1076 TEdone
("preload " ^
(btos conf.preload))
1079 conf.verbose
<- not
conf.verbose
;
1080 TEdone
("verbose " ^
(btos conf.verbose
))
1083 conf.maxhfit
<- not
conf.maxhfit
;
1084 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1085 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1088 conf.crophack
<- not
conf.crophack
;
1089 TEdone
("crophack " ^
btos conf.crophack
)
1092 conf.showall
<- not
conf.showall
;
1093 TEdone
("showall " ^
btos conf.showall
)
1096 conf.underinfo
<- not
conf.underinfo
;
1097 TEdone
("underinfo " ^
btos conf.underinfo
)
1100 conf.savebmarks
<- not
conf.savebmarks
;
1101 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1107 match state.layout with
1112 conf.interpagespace <- int_of_string
s;
1113 state.maxy <- calcheight ();
1114 let y = getpagey pageno in
1117 state.text <- Printf.sprintf
"bad integer `%s': %s"
1118 s (Printexc.to_string exc
)
1120 TEswitch
('
%'
, "", None
, intentry, ondone)
1123 reinit conf.angle
(not
conf.proportional
);
1124 TEdone
("proprortional display " ^
btos conf.proportional
)
1127 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1131 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1133 let enterselector allowdel
outlines errmsg msg
=
1134 if Array.length
outlines = 0
1136 showtext ' ' errmsg
;
1140 Glut.setCursor
Glut.CURSOR_INHERIT
;
1142 match state.layout with
1144 | {pageno=pageno} :: rest
-> pageno
1148 if n = Array.length
outlines
1151 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1152 if outlinepageno
>= pageno then n else loop (n+1)
1156 state.mode
<- Outline
1157 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "");
1158 Glut.postRedisplay
();
1162 let enteroutlinemode () =
1164 match state.outlines with
1167 let a = Array.of_list
(List.rev
l) in
1168 state.outlines <- Oarray
a;
1170 | Onarrow
(pat
, a, b) ->
1171 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1173 enterselector false outlines "Document has no outline" msg
;
1176 let enterbookmarkmode () =
1177 let bookmarks = Array.of_list
state.bookmarks in
1178 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1181 let quickbookmark ?title
() =
1182 match state.layout with
1188 let sec = Unix.gettimeofday
() in
1189 let tm = Unix.localtime
sec in
1190 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1194 (tm.Unix.tm_year
+ 1900)
1197 | Some
title -> title
1200 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1204 state.fullscreen
<- None
;
1205 Glut.reshapeWindow
w h;
1208 let writeopen path password
=
1209 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1212 let opendoc path password
=
1215 state.password
<- password
;
1216 state.gen
<- state.gen
+ 1;
1218 writeopen path password
;
1219 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1220 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1224 let zoom = float conf.thumbw
/. float conf.winw
in
1225 let (birdseyepageno
, _
) as anchor = getanchor () in
1227 Birdseye
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno
, -1);
1229 conf.presentation <- false;
1230 conf.interpagespace <- 10;
1231 conf.hlinks
<- false;
1233 state.mstate
<- Mnone
;
1234 conf.showall
<- false;
1235 Glut.setCursor
Glut.CURSOR_INHERIT
;
1238 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1245 let birdseyeoff (c, leftx
, pageno, _
) =
1247 conf.zoom <- c.zoom;
1248 conf.presentation <- c.presentation;
1249 conf.interpagespace <- c.interpagespace;
1250 conf.showall
<- c.showall
;
1251 conf.hlinks
<- c.hlinks
;
1255 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1260 let togglebirdseye () =
1261 match state.mode
with
1262 | Birdseye vals
-> birdseyeoff vals
1263 | View
| Outline _
-> birdseyeon ()
1267 let viewkeyboard ~key ~x ~
y =
1269 state.mode
<- Textentry
(te
, state.mode
);
1272 Glut.postRedisplay
()
1274 let c = Char.chr key
in
1280 let y = getnav () in
1281 gotoy_and_clear_text y
1289 Glut.postRedisplay
()
1292 let ondone isforw
s =
1293 cbput state.hists
.pat
s;
1294 state.searchpattern
<- s;
1297 enttext (c, "", Some
(onhist state.hists
.pat
),
1298 textentry, ondone (c ='
/'
))
1300 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1301 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1302 conf.zoom <- min
2.2 (conf.zoom +. incr);
1303 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1304 reshape conf.winw
conf.winh
1309 try int_of_string
s with exc
->
1310 state.text <- Printf.sprintf
"bad integer `%s': %s"
1311 s (Printexc.to_string exc
);
1317 state.text <- "page bias is now " ^ string_of_int
n;
1320 enttext ('
+'
, "", None
, intentry, ondone)
1322 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1323 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1324 conf.zoom <- max
0.01 (conf.zoom -. decr);
1325 if conf.zoom <= 1.0 then state.x
<- 0;
1326 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1327 reshape conf.winw
conf.winh
;
1333 enttext ('
-'
, "", None
, optentry, ondone)
1335 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1338 state.text <- "zoom is 100%";
1339 reshape conf.winw
conf.winh
1341 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1342 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1347 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1348 reshape conf.winw
conf.winh
;
1351 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1353 reshape conf.winw
conf.winh
;
1358 try int_of_string
s with exc
->
1359 state.text <- Printf.sprintf
"bad integer `%s': %s"
1360 s (Printexc.to_string exc
);
1366 cbput state.hists
.pag
(string_of_int
n);
1367 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1370 let pageentry text key
=
1371 match Char.unsafe_chr key
with
1372 | 'g'
-> TEdone
text
1373 | _
-> intentry text key
1375 let text = "x" in text.[0] <- c;
1376 enttext ('
:'
, text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1379 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1380 reshape conf.winw
conf.winh
;
1383 conf.hlinks
<- not
conf.hlinks
;
1384 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1385 Glut.postRedisplay
()
1388 conf.autoscroll
<- not
conf.autoscroll
1391 conf.presentation <- not
conf.presentation;
1392 showtext ' '
("presentation mode " ^
1393 if conf.presentation then "on" else "off");
1397 begin match state.fullscreen
with
1399 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1402 state.fullscreen
<- None
;
1407 gotoy_and_clear_text 0
1410 search state.searchpattern
true
1413 search state.searchpattern
false
1416 begin match state.layout with
1419 gotoy_and_clear_text (getpagey l.pageno)
1423 begin match List.rev
state.layout with
1426 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1427 gotoy_and_clear_text (getpagey pageno)
1431 begin match state.layout with
1434 let pageno = max
0 (l.pageno-1) in
1435 gotoy_and_clear_text (getpagey pageno)
1440 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1442 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1444 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1448 else (100. *. (float state.y /. float maxy)) in
1451 Printf.sprintf
"Page %d of %d %.2f%%"
1452 (fn+1) state.pagecount
percent
1455 "Pages %d-%d of %d %.2f%%"
1456 (fn+1) (ln
+1) state.pagecount
percent
1461 begin match state.layout with
1464 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1465 Glut.postRedisplay
();
1469 enterbookmarkmode ()
1473 match state.layout with
1476 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1480 enttext ('~'
, "", None
, textentry, ondone)
1484 showtext ' '
"Quick bookmark added";
1487 begin match state.layout with
1489 let rect = getpdimrect
l.pagedimno
in
1493 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1494 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1496 (truncate
(rect.(1) -. rect.(0)),
1497 truncate
(rect.(3) -. rect.(0)))
1501 doreshape (w + conf.scrollw
) (h + conf.interpagespace)
1503 Glut.postRedisplay
();
1509 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1514 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1515 Glut.postRedisplay
()
1517 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1518 | 'j'
-> gotoy (clamp conf.scrollincr
)
1520 | '
r'
-> opendoc state.path
state.password
1523 vlog "huh? %d %c" key
(Char.chr key
);
1526 let textentrykeyboard ~key ~x ~
y ((c, text, opthist
, onkey
, ondone), mode
) =
1528 state.mode
<- Textentry
(te
, mode
);
1531 Glut.postRedisplay
()
1533 match Char.unsafe_chr key
with
1535 let len = String.length
text in
1539 Glut.postRedisplay
();
1542 let s = String.sub
text 0 (len - 1) in
1543 enttext (c, s, opthist
, onkey
, ondone)
1549 Glut.postRedisplay
()
1552 begin match opthist
with
1554 | Some
(_
, onhistcancel
) -> onhistcancel
()
1557 Glut.postRedisplay
()
1560 begin match onkey
text key
with
1564 Glut.postRedisplay
()
1567 enttext (c, text, opthist
, onkey
, ondone);
1571 Glut.postRedisplay
()
1574 state.mode
<- Textentry
(te
, mode
);
1575 Glut.postRedisplay
()
1579 let birdseyekeyboard ~key ~x ~
y ((c, leftx
, pageno, hooverpageno
) as beye
) =
1583 reshape conf.winw
conf.winh
1586 let y, h = getpageyh pageno in
1587 let top = (conf.winh
- h) / 2 in
1588 gotoy (max
0 (y - top))
1593 reshape conf.winw
conf.winh
;
1594 state.anchor <- (pageno, 0.0);
1597 viewkeyboard ~key ~x ~
y
1600 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1601 let narrow outlines pattern
=
1602 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1606 let rec fold accu n =
1610 let (s, _
, _
, _
) as o
= outlines.(n) in
1612 if (try ignore
(Str.search_forward re
s 0); true
1613 with Not_found
-> false)
1619 let matched = fold [] (Array.length
outlines - 1) in
1620 if matched = [] then None
else Some
(Array.of_list
matched)
1622 let search active pattern
incr =
1625 if n = Array.length
outlines || n = -1
1628 let (s, _
, _
, _
) = outlines.(n) in
1630 (try ignore
(Str.search_forward re
s 0); true
1631 with Not_found
-> false)
1633 else loop (n + incr)
1638 let re = Str.regexp_case_fold pattern
in
1644 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1647 if String.length qsearch
= 0
1651 Glut.postRedisplay
();
1655 state.mode
<- Outline
(allowdel
, active, first
, outlines, "");
1656 Glut.postRedisplay
();
1660 let incr = if key
= 18 then -1 else 1 in
1662 match search (active + incr) qsearch
incr with
1664 state.text <- qsearch ^
" [not found]";
1667 state.text <- qsearch
;
1668 active, firstof active
1670 state.mode
<- Outline
(allowdel
, active, first
, outlines, qsearch
);
1671 Glut.postRedisplay
();
1674 let len = String.length qsearch
in
1681 state.mode
<- Outline
(allowdel
, active, first
, outlines, "");
1684 let qsearch = String.sub
qsearch 0 (len - 1) in
1686 match search active qsearch ~
-1 with
1688 state.text <- qsearch ^
" [not found]";
1691 state.text <- qsearch;
1692 active, firstof active
1694 state.mode
<- Outline
(allowdel
, active, first
, outlines, qsearch);
1696 Glut.postRedisplay
()
1699 if active < Array.length
outlines
1701 let (_
, _
, n, t
) = outlines.(active) in
1705 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1707 Glut.postRedisplay
();
1709 | _
when key
>= 32 && key
< 127 ->
1710 let pattern = addchar qsearch (Char.chr key
) in
1712 match search active pattern 1 with
1714 state.text <- pattern ^
" [not found]";
1717 state.text <- pattern;
1718 active, firstof active
1720 state.mode
<- Outline
(allowdel
, active, first
, outlines, pattern);
1721 Glut.postRedisplay
()
1723 | 14 when not allowdel
-> (* ctrl-n *)
1724 if String.length
qsearch > 0
1726 let optoutlines = narrow outlines qsearch in
1727 begin match optoutlines with
1728 | None
-> state.text <- "can't narrow"
1730 state.mode
<- Outline
(allowdel
, 0, 0, outlines, qsearch);
1731 match state.outlines with
1734 state.outlines <- Onarrow
(qsearch, outlines, a)
1735 | Onarrow
(pat
, a, b) ->
1736 state.outlines <- Onarrow
(qsearch, outlines, b)
1739 Glut.postRedisplay
()
1741 | 21 when not allowdel
-> (* ctrl-u *)
1743 match state.outlines with
1746 let a = Array.of_list
(List.rev
l) in
1747 state.outlines <- Oarray
a;
1749 | Onarrow
(pat
, a, b) ->
1750 state.outlines <- Oarray
b;
1754 state.mode
<- Outline
(allowdel
, 0, 0, outline, qsearch);
1755 Glut.postRedisplay
()
1758 state.mode
<- Outline
1759 (allowdel
, active, firstof active, outlines, qsearch);
1760 Glut.postRedisplay
()
1762 | 127 when allowdel
->
1763 let len = Array.length
outlines - 1 in
1767 state.bookmarks <- [];
1770 let bookmarks = Array.init
len
1772 let i = if i >= active then i + 1 else i in
1784 Glut.postRedisplay
()
1786 | _
-> dolog "unknown key %d" key
1789 let keyboard ~key ~x ~
y =
1794 match state.mode
with
1795 | Outline
outline -> outlinekeyboard ~key ~x ~
y outline
1796 | Textentry
textentry -> textentrykeyboard ~key ~x ~
y textentry
1797 | Birdseye birdseye
-> birdseyekeyboard ~key ~x ~
y birdseye
1798 | View
-> viewkeyboard ~key ~x ~
y
1801 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1804 let pageno = max
0 (pageno - 1) in
1805 let rec loop = function
1806 | [] -> gotopage1nonav pageno 0
1807 | l :: _
when l.pageno = pageno ->
1808 if l.pagedispy
>= 0 && l.pagey = 0
1809 then Glut.postRedisplay
()
1810 else gotopage1nonav pageno 0
1811 | _
:: rest
-> loop rest
1814 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
)
1817 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1818 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
);
1819 let rec loop = function
1821 let y, h = getpageyh pageno in
1822 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1824 | l :: rest
when l.pageno = pageno ->
1825 if l.pagevh != l.pageh
1826 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1827 else Glut.postRedisplay
()
1828 | l :: rest
-> loop rest
1832 | Glut.KEY_PAGE_UP
->
1833 begin match state.layout with
1837 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1838 gotopage1nonav l.pageno 0;
1841 let layout = layout (state.y-conf.winh
) conf.winh
in
1843 | [] -> gotoy (clamp (-conf.winh
))
1845 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1846 gotopage1nonav l.pageno 0
1849 | [] -> gotoy (clamp (-conf.winh
))
1852 | Glut.KEY_PAGE_DOWN
->
1853 begin match List.rev
state.layout with
1855 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1856 gotoy (clamp (l.pagedispy
+ l.pageh
))
1857 | [] -> gotoy (clamp conf.winh
)
1861 state.mode
<- Birdseye
(conf, leftx
, 0, hooverpageno
);
1865 let pageno = state.pagecount
- 1 in
1866 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
);
1867 if not
(pagevisible state.layout pageno)
1870 match List.rev
state.pdims
with
1872 | (_
, _
, h, _
) :: _
-> h
1874 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1875 else Glut.postRedisplay
();
1879 let special ~key ~x ~
y =
1880 match state.mode
with
1881 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
1883 reshape conf.winw
conf.winh
;
1886 birdseyespecial key x
y vals
1888 | View
| Textentry _
->
1889 begin match state.mode
with
1893 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1894 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1895 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1896 | Glut.KEY_PAGE_UP
->
1897 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1899 match state.layout with
1901 | l :: _
-> state.y - l.pagey
1904 | Glut.KEY_PAGE_DOWN
->
1905 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1907 match List.rev
state.layout with
1909 | l :: _
-> getpagey l.pageno
1912 | Glut.KEY_HOME
-> addnav (); 0
1915 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1917 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1918 state.x
<- state.x
- 10;
1920 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1921 state.x
<- state.x
+ 10;
1926 gotoy_and_clear_text y
1929 ((c, s, (Some
(action, _
) as onhist), onkey
, ondone), mode
) ->
1932 | Glut.KEY_UP
-> action HCprev
1933 | Glut.KEY_DOWN
-> action HCnext
1934 | Glut.KEY_HOME
-> action HCfirst
1935 | Glut.KEY_END
-> action HClast
1938 state.mode
<- Textentry
((c, s, onhist, onkey
, ondone), mode
);
1939 Glut.postRedisplay
()
1944 | Outline
(allowdel
, active, first
, outlines, qsearch) ->
1945 let maxrows = maxoutlinerows () in
1946 let calcfirst first
active =
1949 let rows = active - first
in
1950 if rows > maxrows then active - maxrows else first
1954 let active = active + incr in
1955 let active = max
0 (min
active (Array.length
outlines - 1)) in
1956 let first = calcfirst first active in
1957 state.mode
<- Outline
(allowdel
, active, first, outlines, qsearch);
1958 Glut.postRedisplay
()
1960 let updownlevel incr =
1961 let len = Array.length
outlines in
1962 let (_
, curlevel
, _
, _
) = outlines.(active) in
1964 if i = len then i-1 else if i = -1 then 0 else
1965 let (_
, l, _
, _
) = outlines.(i) in
1966 if l != curlevel
then i else flow (i+incr)
1968 let active = flow active in
1969 let first = calcfirst first active in
1970 state.mode
<- Outline
(allowdel
, active, first, outlines, qsearch);
1971 Glut.postRedisplay
()
1974 | Glut.KEY_UP
-> navigate ~
-1
1975 | Glut.KEY_DOWN
-> navigate 1
1976 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1977 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1979 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1980 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1983 state.mode
<- Outline
(allowdel
, 0, 0, outlines, qsearch);
1984 Glut.postRedisplay
()
1987 let active = Array.length
outlines - 1 in
1988 let first = max
0 (active - maxrows) in
1989 state.mode
<- Outline
(allowdel
, active, first, outlines, qsearch);
1990 Glut.postRedisplay
()
1995 let drawplaceholder l =
1996 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1998 (float l.pagex
, float l.pagedispy
)
1999 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
2001 let x = float (if margin < 0 then -margin else l.pagex
)
2002 and y = float (l.pagedispy
+ 13) in
2003 let font = Glut.BITMAP_8_BY_13
in
2004 GlDraw.color
(0.0, 0.0, 0.0);
2005 GlPix.raster_pos ~
x ~
y ();
2006 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
2007 ("Loading " ^ string_of_int
(l.pageno + 1));
2010 let now () = Unix.gettimeofday
();;
2014 match state.mode
with
2015 | Textentry _
-> scalecolor 0.4
2016 | View
| Outline _
-> scalecolor 1.0
2017 | Birdseye
(_
, _
, pageno, hooverpageno
) ->
2018 if l.pageno = pageno
2021 if l.pageno = hooverpageno
2027 begin match getopaque l.pageno with
2028 | Some
(opaque, _
) when validopaque opaque ->
2030 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2034 vlog "draw %d %f sec" l.pageno d;
2042 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2043 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2044 let sh = float conf.winh
/. sh in
2045 let sh = max
sh (float conf.scrollh
) in
2048 if state.y = state.maxy
2050 else float y /. float maxy
2052 let position = (float conf.winh
-. sh) *. percent in
2055 if position +. sh > float conf.winh
2056 then float conf.winh
-. sh
2062 let scrollindicator () =
2063 GlDraw.color (0.64 , 0.64, 0.64);
2065 (float (conf.winw
- conf.scrollw
), 0.)
2066 (float conf.winw
, float conf.winh
)
2068 GlDraw.color (0.0, 0.0, 0.0);
2070 let position, sh = scrollph state.y in
2072 (float (conf.winw
- conf.scrollw
), position)
2073 (float conf.winw
, position +. sh)
2077 let showsel margin =
2078 match state.mstate
with
2079 | Mnone
| Mscroll _
| Mpan _
->
2082 | Msel
((x0
, y0
), (x1
, y1
)) ->
2083 let rec loop = function
2085 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2086 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2088 match getopaque l.pageno with
2089 | Some
(opaque, _
) when validopaque opaque ->
2090 let oy = -l.pagey + l.pagedispy
in
2092 (x0
- margin - state.x, y0
,
2093 x1
- margin - state.x, y1
) oy;
2103 let panx = float state.x in
2105 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2106 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2108 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2110 if l.pageno = pageno
2112 let d = float (l.pagedispy
- l.pagey) in
2113 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2114 GlDraw.begins `quads
;
2116 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2117 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2118 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2119 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2129 let showoutline () =
2130 match state.mode
with
2131 | Outline
(allowdel
, active, first, outlines, qsearch) ->
2133 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2134 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2135 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2138 GlDraw.color (1., 1., 1.);
2139 let font = Glut.BITMAP_9_BY_15
in
2140 let draw_string x y s =
2141 GlPix.raster_pos ~
x ~
y ();
2142 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2145 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2148 let (s, l, _
, _
) = outlines.(row
) in
2149 let y = (row
- first) * 16 in
2154 GlDraw.polygon_mode `both `line
;
2155 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2156 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2157 GlDraw.rect (0., float (y + 1))
2158 (float (conf.winw
- 1), float (y + 18));
2159 GlDraw.polygon_mode `both `fill
;
2161 GlDraw.color (1., 1., 1.);
2163 draw_string (float x) (float (y + 16)) s;
2173 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2174 GlDraw.viewport
margin 0 state.w conf.winh
;
2176 GlClear.color (scalecolor 0.5);
2177 GlClear.clear
[`
color];
2180 Gl.enable `scissor_test
;
2181 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2183 List.iter
drawpage state.layout;
2186 Gl.disable `scissor_test
2190 let x = -.float state.x in
2191 GlMat.translate ~
x ();
2195 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2200 Glut.swapBuffers
();
2204 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2205 let x = x - margin - state.x in
2206 let rec f = function
2208 begin match getopaque l.pageno with
2209 | Some
(opaque, _
) when validopaque opaque ->
2210 let y = y - l.pagedispy
in
2213 let y = l.pagey + y in
2214 let x = x - l.pagex
in
2215 match whatsunder
opaque x y with
2228 let viewmouse button bstate
x y =
2230 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2238 let incr = incr * 2 in
2239 let y = clamp incr in
2240 gotoy_and_clear_text y
2242 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2243 if bstate
= Glut.DOWN
2245 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2246 state.mstate
<- Mpan
(x, y)
2249 state.mstate
<- Mnone
2251 | Glut.LEFT_BUTTON
when x > conf.winw
- conf.scrollw
->
2252 if bstate
= Glut.DOWN
2254 let position, sh = scrollph state.y in
2255 if y > truncate
position && y < truncate
(position +. sh)
2257 state.mstate
<- Mscroll
2259 let percent = float y /. float conf.winh
in
2260 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2262 state.mstate
<- Mscroll
2264 state.mstate
<- Mnone
2266 | Glut.LEFT_BUTTON
->
2267 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2268 begin match dest with
2269 | Ulinkgoto
(pageno, top) ->
2272 gotopage1 pageno top
2277 | Unone
when bstate
= Glut.DOWN
->
2278 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2279 state.mstate
<- Mpan
(x, y);
2281 | Unone
| Utext _
->
2282 if bstate
= Glut.DOWN
2284 if conf.angle
mod 360 = 0
2286 state.mstate
<- Msel
((x, y), (x, y));
2287 Glut.postRedisplay
()
2291 match state.mstate
with
2295 state.mstate
<- Mnone
2298 Glut.setCursor
Glut.CURSOR_INHERIT
;
2299 state.mstate
<- Mnone
2301 | Msel
((x0
, y0
), (x1
, y1
)) ->
2303 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2304 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2306 match getopaque l.pageno with
2307 | Some
(opaque, _
) when validopaque opaque ->
2311 List.iter
f state.layout;
2312 copysel
""; (* ugly *)
2313 Glut.setCursor
Glut.CURSOR_INHERIT
;
2314 state.mstate
<- Mnone
;
2321 let birdseyemouse button bstate
x y (conf, leftx
, pageno, hooverpageno
) =
2323 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
2324 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2325 let rec loop = function
2328 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2329 && x > margin && x < margin + l.pagew
2331 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2332 reshape conf.winw
conf.winh
;
2333 state.anchor <- (l.pageno, 0.0);
2341 let mouse bstate button
x y =
2342 match state.mode
with
2343 | View
-> viewmouse button bstate
x y
2344 | Birdseye beye
-> birdseyemouse button bstate
x y beye
2349 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
2352 match state.mode
with
2355 match state.mstate
with
2361 state.mstate
<- Mpan
(x, y);
2362 if conf.zoom > 1.0 then state.x <- state.x + dx;
2364 gotoy_and_clear_text y
2367 state.mstate
<- Msel
(a, (x, y));
2368 Glut.postRedisplay
()
2371 let y = min
conf.winh
(max
0 y) in
2372 let percent = float y /. float conf.winh
in
2373 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2374 gotoy_and_clear_text y
2378 match state.mode
with
2379 | Birdseye
(conf, leftx
, pageno, hooverpageno
) ->
2380 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2381 let rec loop = function
2383 if hooverpageno
!= -1
2385 state.mode
<- Birdseye
(conf, leftx
, pageno, -1);
2386 Glut.postRedisplay
();
2389 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2390 && x > margin && x < margin + l.pagew
2392 state.mode
<- Birdseye
(conf, leftx
, pageno, l.pageno);
2393 Glut.postRedisplay
();
2401 match state.mstate
with
2403 begin match getunder x y with
2404 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2406 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2407 Glut.setCursor
Glut.CURSOR_INFO
2408 | Ulinkgoto
(page
, y) ->
2410 then showtext 'p'
("age: " ^ string_of_int page
);
2411 Glut.setCursor
Glut.CURSOR_INFO
2413 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2414 Glut.setCursor
Glut.CURSOR_TEXT
2417 | Mpan _
| Msel _
| Mscroll
->
2428 match Sys.os_type
with
2429 | "Win32" -> Sys.getenv
"HOMEPATH"
2430 | _
-> Sys.getenv
"HOME"
2433 ("Can not determine home directory location: " ^
2434 Printexc.to_string exn
);
2438 let config_of c attrs
=
2442 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2443 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2444 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2445 | "preload" -> { c with preload = bool_of_string v
}
2446 | "page-bias" -> { c with pagebias
= int_of_string v
}
2447 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2448 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2449 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2450 | "throttle" -> { c with showall
= bool_of_string v
}
2451 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2452 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2453 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2455 let zoom = float_of_string v
/. 100. in
2456 let zoom = max
0.01 (min
2.2 zoom) in
2457 { c with zoom = zoom }
2458 | "presentation" -> { c with presentation = bool_of_string v
}
2459 | "rotation-angle" -> { c with angle
= int_of_string v
}
2460 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2461 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2462 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2463 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2464 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2465 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2466 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2467 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2470 prerr_endline
("Error processing attribute (`" ^
2471 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2474 let rec fold c = function
2477 let c = apply c k v
in
2483 let bookmark_of attrs
=
2484 let rec fold title page rely
= function
2485 | ("title", v
) :: rest
-> fold v page rely rest
2486 | ("page", v
) :: rest
-> fold title v rely rest
2487 | ("rely", v
) :: rest
-> fold title page v rest
2488 | _
:: rest
-> fold title page rely rest
2489 | [] -> title, page
, rely
2491 fold "invalid" "0" "0" attrs
2494 let setconf dst src
=
2495 dst
.scrollw
<- src
.scrollw
;
2496 dst
.scrollh
<- src
.scrollh
;
2497 dst
.icase
<- src
.icase
;
2498 dst
.preload <- src
.preload;
2499 dst
.pagebias
<- src
.pagebias
;
2500 dst
.verbose
<- src
.verbose
;
2501 dst
.scrollincr
<- src
.scrollincr
;
2502 dst
.maxhfit
<- src
.maxhfit
;
2503 dst
.crophack
<- src
.crophack
;
2504 dst
.autoscroll
<- src
.autoscroll
;
2505 dst
.showall
<- src
.showall
;
2506 dst
.hlinks
<- src
.hlinks
;
2507 dst
.underinfo
<- src
.underinfo
;
2508 dst
.interpagespace <- src
.interpagespace;
2509 dst
.zoom <- src
.zoom;
2510 dst
.presentation <- src
.presentation;
2511 dst
.angle
<- src
.angle
;
2512 dst
.winw
<- src
.winw
;
2513 dst
.winh
<- src
.winh
;
2514 dst
.savebmarks
<- src
.savebmarks
;
2515 dst
.memlimit
<- src
.memlimit
;
2516 dst
.proportional
<- src
.proportional
;
2517 dst
.texcount
<- src
.texcount
;
2518 dst
.sliceheight
<- src
.sliceheight
;
2519 dst
.thumbw
<- src
.thumbw
;
2523 let l = String.length
s in
2524 let b = Buffer.create
l in
2530 let h = Hashtbl.create
10 in
2531 let dc = { defconf with angle
= defconf.angle
} in
2532 let rec toplevel v t spos epos
=
2534 | Vdata
| Vcdata
| Vend
-> v
2535 | Vopen
("llppconfig", attrs
, closed
) ->
2538 else { v
with f = llppconfig
}
2540 error
"unexpected subelement at top level" s spos
2541 | Vclose tag
-> error
"unexpected close at top level" s spos
2543 and llppconfig v t spos epos
=
2545 | Vdata
| Vcdata
| Vend
-> v
2546 | Vopen
("defaults", attrs
, closed
) ->
2547 let c = config_of dc attrs
in
2551 else { v
with f = skip
"defaults" (fun () -> v
) }
2553 | Vopen
("doc", attrs
, closed
) ->
2556 List.assoc
"path" attrs
2557 with Not_found
-> error
"doc is missing path attribute" s spos
2559 let path = unent pathent in
2560 let c = config_of dc attrs
in
2561 let pageno, rely
, x =
2565 dolog "error accessing %s (%S) at postion %d:\n %s"
2566 n v spos
(Printexc.to_string exn
);
2569 let rec fold pageno rely
x = function
2570 | [] -> pageno, rely
, x
2571 | ("rely", v
) :: rest
->
2572 fold pageno (safef float_of_string
"rely" v
0.0) x rest
2573 | ("page", v
) :: rest
->
2574 fold (safef int_of_string
"page" v
0) rely
x rest
2575 | ("x", v
) :: rest
->
2576 fold pageno rely
(safef int_of_string
"x" v
0) rest
2578 fold pageno rely
x rest
2582 let anchor = (pageno, rely
) in
2584 then (Hashtbl.add
h path (c, [], x, anchor); v
)
2585 else { v
with f = doc
path x anchor c [] }
2587 | Vopen
(tag
, _
, closed
) ->
2588 error
"unexpected subelement in llppconfig" s spos
2590 | Vclose
"llppconfig" -> { v
with f = toplevel }
2591 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2593 and doc
path x anchor c bookmarks v t spos epos
=
2595 | Vdata
| Vcdata
-> v
2596 | Vend
-> error
"unexpected end of input in doc" s spos
2597 | Vopen
("bookmarks", attrs
, closed
) ->
2598 { v
with f = pbookmarks
path x anchor c bookmarks }
2600 | Vopen
(tag
, _
, _
) ->
2601 error
"unexpected subelement in doc" s spos
2604 Hashtbl.add
h path (c, List.rev
bookmarks, x, anchor);
2605 { v
with f = llppconfig
}
2607 | Vclose tag
-> error
"unexpected close in doc" s spos
2609 and pbookmarks
path x anchor c bookmarks v t spos epos
=
2611 | Vdata
| Vcdata
-> v
2612 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2613 | Vopen
("item", attrs
, closed
) ->
2614 let titleent, spage
, srely
= bookmark_of attrs
in
2619 dolog "Failed to convert page %S to integer: %s"
2620 spage
(Printexc.to_string exn
);
2625 float_of_string srely
2627 dolog "Failed to convert rely %S to real: %s"
2628 srely
(Printexc.to_string exn
);
2631 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2633 then { v
with f = pbookmarks
path x anchor c bookmarks }
2636 { v
with f = skip
"item" f }
2639 error
"unexpected subelement in bookmarks" s spos
2641 | Vclose
"bookmarks" ->
2642 { v
with f = doc
path x anchor c bookmarks }
2644 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2646 and skip tag
f v t spos epos
=
2648 | Vdata
| Vcdata
-> v
2650 error
("unexpected end of input in skipped " ^ tag
) s spos
2651 | Vopen
(tag'
, _
, closed
) ->
2655 let f'
() = { v
with f = skip tag
f } in
2656 { v
with f = skip tag'
f'
}
2660 else error
("unexpected close in skipped " ^ tag
) s spos
2663 parse
{ f = toplevel; accu = () } s;
2669 let len = in_channel_length ic
in
2670 let s = String.create
len in
2671 really_input ic
s 0 len;
2674 | Parse_error
(msg
, s, pos
) ->
2675 let subs = subs s pos
in
2676 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2677 failwith
("parse error: " ^
s)
2680 failwith
("config load error: " ^
Printexc.to_string exn
)
2686 let dir = Filename.concat
home ".config" in
2687 if Sys.is_directory
dir then dir else home
2690 Filename.concat
dir "llpp.conf"
2694 if Sys.file_exists
path
2697 (try Some
(open_in_bin
path)
2700 ("Error opening configuation file `" ^
path ^
"': " ^
2701 Printexc.to_string exn
);
2710 ("Error loading configuation from `" ^
path ^
"': " ^
2711 Printexc.to_string exn
);
2717 f (Hashtbl.create
0, defconf)
2722 let pc, pb
, px
, pa
=
2724 Hashtbl.find
h (Filename.basename
state.path)
2725 with Not_found
-> dc, [], 0, (0, 0.0)
2729 state.bookmarks <- pb
;
2731 cbput state.hists
.nav pa
;
2736 let add_attrs bb always
dc c =
2739 then Printf.bprintf bb
"\n %s='%b'" s a
2742 then Printf.bprintf bb
"\n %s='%d'" s a
2745 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2749 then dc.winw
, dc.winh
2751 match state.fullscreen
with
2753 | None
-> c.winw
, c.winh
2755 let zoom, presentation, interpagespace, showall
=
2757 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2759 match state.mode
with
2760 | Birdseye
(bc
, _
, _
, _
) ->
2761 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2762 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2764 oi
"width" w dc.winw
;
2765 oi
"height" h dc.winh
;
2766 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2767 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2768 ob "case-insensitive-search" c.icase
dc.icase
;
2769 ob "preload" c.preload dc.preload;
2770 oi
"page-bias" c.pagebias
dc.pagebias
;
2771 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2772 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2773 ob "crop-hack" c.crophack
dc.crophack
;
2774 ob "throttle" showall
dc.showall
;
2775 ob "highlight-links" c.hlinks
dc.hlinks
;
2776 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2777 oi
"vertical-margin" interpagespace dc.interpagespace;
2778 oz
"zoom" zoom dc.zoom;
2779 ob "presentation" presentation dc.presentation;
2780 oi
"rotation-angle" c.angle
dc.angle
;
2781 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2782 ob "proportional-display" c.proportional
dc.proportional
;
2783 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2784 oi
"texcount" c.texcount
dc.texcount
;
2785 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2786 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2790 let bb = Buffer.create
32768 in
2792 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2793 add_attrs bb true dc dc;
2794 Buffer.add_string
bb "/>\n";
2796 let adddoc path x anchor c bookmarks =
2797 if bookmarks == [] && c = dc && anchor = emptyanchor
2800 Printf.bprintf
bb "<doc path='%s'"
2801 (enent
path 0 (String.length
path));
2803 if anchor <> emptyanchor
2805 let n, y = anchor in
2806 Printf.bprintf
bb " page='%d'" n;
2807 Printf.bprintf
bb " rely='%f'" y;
2811 then Printf.bprintf
bb " pan='%d'" x;
2813 add_attrs bb false dc c;
2815 begin match bookmarks with
2816 | [] -> Buffer.add_string
bb "/>\n"
2818 Buffer.add_string
bb ">\n<bookmarks>\n";
2819 List.iter
(fun (title, _level
, page, rely) ->
2821 "<item title='%s' page='%d' rely='%f'/>\n"
2822 (enent
title 0 (String.length
title))
2826 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2832 match state.mode
with
2833 | Birdseye
(_
, x, _
, _
) -> x
2836 let basename = Filename.basename state.path in
2837 adddoc basename x (getanchor ()) conf
2838 (if conf.savebmarks
then state.bookmarks else []);
2840 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2842 then adddoc path x y c bookmarks
2844 Buffer.add_string
bb "</llppconfig>";
2847 if Buffer.length
bb > 0
2850 let tmp = path ^
".tmp" in
2851 let oc = open_out_bin
tmp in
2852 Buffer.output_buffer
oc bb;
2854 Sys.rename
tmp path;
2857 ("error while saving configuration: " ^
Printexc.to_string exn
)
2863 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2864 (fun s -> state.path <- s)
2865 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2867 if String.length
state.path = 0
2868 then (prerr_endline
"filename missing"; exit
1);
2872 let _ = Glut.init
Sys.argv
in
2873 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2874 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2875 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
2878 if Sys.os_type
= "Unix"
2880 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2882 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2883 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2884 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2885 Unix.bind
sock addr;
2887 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2888 Unix.connect
csock addr;
2889 let ssock, _ = Unix.accept
sock in
2892 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2893 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2897 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2901 let () = Glut.displayFunc
display in
2902 let () = Glut.reshapeFunc
reshape in
2903 let () = Glut.keyboardFunc
keyboard in
2904 let () = Glut.specialFunc
special in
2905 let () = Glut.idleFunc
(Some
idle) in
2906 let () = Glut.mouseFunc
mouse in
2907 let () = Glut.motionFunc
motion in
2908 let () = Glut.passiveMotionFunc
pmotion in
2910 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2911 state.csock <- csock;
2912 state.ssock <- ssock;
2913 state.text <- "Opening " ^
state.path;
2914 writeopen state.path state.password
;
2918 let rec handlelablglutbug () =
2921 with Glut.BadEnum
"key in special_of_int" ->
2922 showtext '
!'
" LablGlut bug: special key not recognized";
2923 handlelablglutbug ()
2925 handlelablglutbug ();