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
* mode
)
180 let isbirdseye = function Birdseye _
-> true | _
-> false;;
181 let istextentry = function Textentry _
-> true | _
-> false;;
184 { mutable csock
: Unix.file_descr
185 ; mutable ssock
: Unix.file_descr
189 ; mutable anchor
: anchor
191 ; mutable layout
: layout list
192 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
193 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
194 ; mutable pagecount
: int
195 ; pagecache
: string circbuf
196 ; mutable rendering
: bool
197 ; mutable mstate
: mstate
198 ; mutable searchpattern
: string
199 ; mutable rects
: (pageno
* recttype
* rect
) list
200 ; mutable rects1
: (pageno
* recttype
* rect
) list
201 ; mutable text
: string
202 ; mutable fullscreen
: (width
* height
) option
203 ; mutable mode
: mode
204 ; mutable outlines
: outlines
205 ; mutable bookmarks
: outline list
206 ; mutable path
: string
207 ; mutable password
: string
208 ; mutable invalidated
: int
209 ; mutable colorscale
: float
210 ; mutable memused
: int
212 ; mutable throttle
: layout list
option
216 { pat
: string circbuf
217 ; pag
: string circbuf
218 ; nav
: anchor circbuf
238 ; presentation
= false
243 ; proportional
= true
244 ; memlimit
= 32*1024*1024
251 let conf = { defconf with angle
= defconf.angle
};;
262 ; pagemap
= Hashtbl.create
10
263 ; pagecache
= cbnew 100 ""
274 ; outlines
= Olist
[]
280 { nav
= cbnew 100 (0, 0.0)
294 Printf.kprintf prerr_endline fmt
296 Printf.kprintf ignore fmt
300 let len = String.length s
in
302 let b = Buffer.create
n in
303 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
304 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
305 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
306 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
307 Buffer.add_string
b s
;
308 let s'
= Buffer.contents
b in
309 let n'
= Unix.write fd
s'
0 n in
310 if n'
!= n then failwith
"write failed";
315 let n = Unix.read fd
s 0 4 in
316 if n != 4 then failwith
"incomplete read(len)";
318 lor (Char.code
s.[0] lsl 24)
319 lor (Char.code
s.[1] lsl 16)
320 lor (Char.code
s.[2] lsl 8)
321 lor (Char.code
s.[3] lsl 0)
323 let s = String.create
len in
324 let n = Unix.read fd
s 0 len in
325 if n != len then failwith
"incomplete read(data)";
330 let b = Buffer.create
10 in
331 Buffer.add_string
b s;
332 let rec combine = function
335 Buffer.add_char
b ' '
;
338 | `
b b -> if b then "1" else "0"
340 | `i i
-> string_of_int i
341 | `f f
-> string_of_float f
342 | `I f
-> string_of_int
(truncate f
)
344 Buffer.add_string
b s;
351 let cmd = Buffer.contents
(makecmd s l
) in
352 writecmd state.csock
cmd;
358 let d = conf.winh
- h
in
365 let rec f pn ph pi fh l
=
367 | (n, _
, h
, _
) :: rest
->
368 let ips = calcips h
in
373 if isbirdseye state.mode
&& pn
= 0
378 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
383 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
387 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
390 let fh = f 0 0 0 0 state.pdims
in
394 let getpageyh pageno
=
395 let rec f pn ph pi y l
=
397 | (n, _
, h
, _
) :: rest
->
398 let ips = calcips h
in
401 let h = if n = pageno
then h else ph
in
402 if conf.presentation
&& n = pageno
404 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
406 y
+ (pageno
- pn
) * (ph
+ pi
), h
408 let y = y + (if conf.presentation
then pi
else 0) in
409 let y = y + (n - pn
) * (ph
+ pi
) in
413 y + (pageno
- pn
) * (ph
+ pi
), ph
415 f 0 0 0 0 state.pdims
418 let getpagey pageno
= fst
(getpageyh pageno
);;
421 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
422 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
424 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
425 let ips = calcips h in
427 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
431 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
433 prev
, pdims
, pdimno
, 0
435 let dy = dy + yinc in
436 let py = py + yinc in
437 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
442 if py + h <= vy - yinc
444 let py = py + h + ips in
445 let dy = max
0 (py - y) in
455 let pagey = vy - py in
456 let pagevh = h - pagey in
457 let pagevh = min
(sh
- dy) pagevh in
458 let off = if yinc > 0 then py - vy else 0 in
459 let py = py + h + ips in
465 ; pagedispy
= dy + off
466 ; pagey = pagey + off
467 ; pagevh = pagevh - off
471 let accu = e :: accu in
478 ~cacheleft
:(cacheleft
-1)
481 if state.invalidated
= 0
491 ~cacheleft
:(cbcap state.pagecache
)
501 let y = state.y + incr
in
503 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
507 let getopaque pageno
=
508 try Some
(Hashtbl.find
state.pagemap
509 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
510 with Not_found
-> None
513 let cache pageno opaque
=
514 Hashtbl.replace
state.pagemap
515 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
518 let validopaque opaque
= String.length opaque
> 0;;
521 match getopaque l
.pageno
with
522 | None
when not
state.rendering
->
523 state.rendering
<- true;
524 cache l
.pageno
("", -1);
525 wcmd "render" [`i
(l
.pageno
+ 1)
533 let loadlayout layout =
534 let rec f all
= function
536 begin match getopaque l
.pageno
with
537 | None
-> render l
; f false ls
538 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
542 f (layout <> []) layout;
545 let findpageforopaque opaque
=
547 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
551 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
557 let memleft = conf.memlimit
- state.memused
in
560 let opaque = cbpeek state.pagecache
in
561 match findpageforopaque opaque with
562 | Some
((n, _
, _
, _
, _
), size
) ->
563 memleft + size
>= 0 && not
(pagevisible state.layout n)
570 let presentation = conf.presentation in
571 let interpagespace = conf.interpagespace in
572 let maxy = state.maxy in
573 conf.presentation <- false;
574 conf.interpagespace <- 0;
575 state.maxy <- calcheight ();
577 match state.layout with
579 | l
:: _
-> getpagey l
.pageno
581 let y = if y < conf.winh
then 0 else y - conf.winh
in
582 let pages = layout y (conf.winh
*3) in
583 List.iter
render pages;
584 conf.presentation <- presentation;
585 conf.interpagespace <- interpagespace;
591 let y = min
state.maxy y in
592 let pages = layout y conf.winh
in
593 let ready = loadlayout pages in
599 state.layout <- pages;
600 state.throttle
<- None
;
601 Glut.postRedisplay
();
604 state.throttle
<- Some
pages;
609 state.layout <- pages;
610 state.throttle
<- None
;
611 Glut.postRedisplay
();
613 begin match state.mode
with
614 | Birdseye
(conf, leftx
, pageno
, hooverpageno
) ->
615 if not
(pagevisible pages pageno
)
617 match state.layout with
620 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
)
627 let gotoy_and_clear_text y =
629 if not
conf.verbose
then state.text
<- "";
632 let emptyanchor = (0, 0.0);;
635 match state.layout with
637 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
640 let getanchory (n, top
) =
641 let y, h = getpageyh n in
642 y + (truncate
(top
*. float h));
645 let gotoanchor anchor
=
646 gotoy (getanchory anchor
);
650 cbput state.hists
.nav
(getanchor ());
654 let anchor = cbgetc state.hists
.nav ~
-1 in
658 let gotopagenonav n top
=
659 let y, h = getpageyh n in
660 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
663 let gotopage1nonav n top
=
664 let y, h = getpageyh n in
666 gotoy_and_clear_text (y + top
);
670 let y, h = getpageyh n in
672 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
675 let gotopage1 n top
=
676 let y = getpagey n in
678 gotoy_and_clear_text (y + top
);
686 state.invalidated
<- state.invalidated
+ 1;
690 let c = c *. state.colorscale
in
695 state.maxy <- calcheight ();
696 match state.mode
with
697 | Birdseye
(_
, _
, pageno
, _
) ->
698 let y, h = getpageyh pageno
in
699 let top = (conf.winh
- h) / 2 in
700 gotoy (max
0 (y - top))
701 | _
-> gotoanchor state.anchor
705 GlMat.mode `projection
;
706 GlMat.load_identity
();
707 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
708 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
709 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
713 GlMat.mode `projection
;
714 GlMat.load_identity
();
715 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
716 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
717 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
721 if state.invalidated
= 0
722 then state.anchor <- getanchor ();
725 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
729 GlMat.mode `modelview
;
730 GlMat.load_identity
();
731 GlClear.color
(scalecolor 1.0);
732 GlClear.clear
[`color
];
735 wcmd "geometry" [`i
w; `i
h];
739 GlDraw.color
(0.0, 0.0, 0.0);
741 (0.0, float (conf.winh
- 18))
742 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
744 let font = Glut.BITMAP_8_BY_13
in
745 GlDraw.color
(1.0, 1.0, 1.0);
746 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
747 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
748 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
752 let len = String.length
state.text
in
753 match state.mode
with
754 | Textentry
((c, text
, _
, _
, _
), _
) ->
758 text ^
" [" ^
state.text ^
"]"
765 if len > 0 then showtext ' '
state.text
771 state.text
<- Printf.sprintf
"%c%s" c s;
772 Glut.postRedisplay
();
786 state.rects
<- state.rects1
;
787 Glut.postRedisplay
()
790 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
791 state.pagecount
<- n;
792 state.invalidated
<- state.invalidated
- 1;
793 if state.invalidated
= 0
797 let s = Scanf.sscanf
cmd "t %n"
798 (fun n -> String.sub
cmd n (String.length
cmd - n))
800 Glut.setWindowTitle
s
803 let s = Scanf.sscanf
cmd "T %n"
804 (fun n -> String.sub
cmd n (String.length
cmd - n))
806 if istextentry state.mode
813 Glut.postRedisplay
();
819 let s = Scanf.sscanf
cmd "V %n"
820 (fun n -> String.sub
cmd n (String.length
cmd - n))
826 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
827 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
828 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
829 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
831 let y = (getpagey pageno) + truncate y0
in
834 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
837 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
838 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
839 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
840 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
843 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
846 let n, w, h, r
, l
, s, p
=
847 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
848 (fun n w h r l
s p
->
849 (n-1, w, h, r
, l
!= 0, s, p
))
852 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
853 state.memused
<- state.memused
+ s;
856 match state.throttle
with
857 | None
-> state.layout
858 | Some
layout -> layout
862 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
865 let evictedopaque = cbpeek state.pagecache
in
866 match findpageforopaque evictedopaque with
867 | None
-> failwith
"bug in gc"
868 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
869 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
871 wcmd "free" [`
s evictedopaque];
872 state.memused
<- state.memused
- evictedsize
;
873 Hashtbl.remove
state.pagemap k
;
874 cbdecr state.pagecache
;
881 cbput state.pagecache p
;
882 state.rendering
<- false;
884 begin match state.throttle
with
886 if pagevisible state.layout n
889 let allvisible = loadlayout state.layout in
890 if allvisible then preload ();
897 let y = getpagey l
.pageno + l
.pagey in
902 let (n, w, h, x
) as pdim
=
903 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
905 state.pdims
<- pdim
:: state.pdims
908 let (l
, n, t
, h, pos
) =
909 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
911 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
913 let l = String.length
s in
914 let b = Buffer.create
(String.length
s) in
921 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
924 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
933 let outline = (s, l, n, float t
/. float h) in
935 match state.outlines with
936 | Olist
outlines -> Olist
(outline :: outlines)
937 | Oarray _
-> Olist
[outline]
938 | Onarrow _
-> Olist
[outline]
940 state.outlines <- outlines
943 log "unknown cmd `%S'" cmd
946 let now = Unix.gettimeofday
;;
950 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
953 if conf.autoscroll
&& conf.scrollincr
!= 0
955 let y = state.y + conf.scrollincr
in
956 let y = if y >= state.maxy then 0 else y in
962 let cmd = readcmd state.csock
in
971 let action = function
972 | HCprev
-> cbget cb ~
-1
973 | HCnext
-> cbget cb
1
974 | HCfirst
-> cbget cb ~
-(cb
.rc)
975 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
976 and cancel
() = cb
.rc <- rc
980 let search pattern forward
=
981 if String.length pattern
> 0
984 match state.layout with
987 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
990 let b = makecmd "search"
991 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
993 Buffer.add_char
b '
,'
;
994 Buffer.add_string
b pattern
;
995 Buffer.add_char
b '
\000'
;
998 writecmd state.csock
cmd;
1001 let intentry text key
=
1002 let c = Char.unsafe_chr key
in
1005 let s = "x" in s.[0] <- c;
1006 let text = text ^
s in
1010 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1015 let b = Buffer.create
(String.length
s + 1) in
1016 Buffer.add_string
b s;
1017 Buffer.add_char
b c;
1021 let textentry text key
=
1022 let c = Char.unsafe_chr key
in
1024 | _
when key
>= 32 && key
< 127 ->
1025 let text = addchar text c in
1029 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1033 let reinit angle proportional
=
1034 conf.angle
<- angle
;
1035 conf.proportional
<- proportional
;
1037 wcmd "reinit" [`i angle
; `
b proportional
];
1040 let optentry text key
=
1041 let btos b = if b then "on" else "off" in
1042 let c = Char.unsafe_chr key
in
1046 try conf.scrollincr
<- int_of_string
s with exc
->
1047 state.text <- Printf.sprintf
"bad integer `%s': %s"
1048 s (Printexc.to_string exc
)
1050 TEswitch
('#'
, "", None
, intentry, ondone)
1055 Some
(int_of_string
s)
1057 state.text <- Printf.sprintf
"bad integer `%s': %s"
1058 s (Printexc.to_string exc
);
1061 | Some angle
-> reinit angle
conf.proportional
1064 TEswitch
('^'
, "", None
, intentry, ondone)
1067 conf.icase
<- not
conf.icase
;
1068 TEdone
("case insensitive search " ^
(btos conf.icase
))
1071 conf.preload <- not
conf.preload;
1073 TEdone
("preload " ^
(btos conf.preload))
1076 conf.verbose
<- not
conf.verbose
;
1077 TEdone
("verbose " ^
(btos conf.verbose
))
1080 conf.maxhfit
<- not
conf.maxhfit
;
1081 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1082 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1085 conf.crophack
<- not
conf.crophack
;
1086 TEdone
("crophack " ^
btos conf.crophack
)
1089 conf.showall
<- not
conf.showall
;
1090 TEdone
("showall " ^
btos conf.showall
)
1093 conf.underinfo
<- not
conf.underinfo
;
1094 TEdone
("underinfo " ^
btos conf.underinfo
)
1097 conf.savebmarks
<- not
conf.savebmarks
;
1098 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1104 match state.layout with
1109 conf.interpagespace <- int_of_string
s;
1110 state.maxy <- calcheight ();
1111 let y = getpagey pageno in
1114 state.text <- Printf.sprintf
"bad integer `%s': %s"
1115 s (Printexc.to_string exc
)
1117 TEswitch
('
%'
, "", None
, intentry, ondone)
1120 reinit conf.angle
(not
conf.proportional
);
1121 TEdone
("proprortional display " ^
btos conf.proportional
)
1124 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1128 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1130 let enterselector allowdel
outlines errmsg msg
=
1131 if Array.length
outlines = 0
1133 showtext ' ' errmsg
;
1137 Glut.setCursor
Glut.CURSOR_INHERIT
;
1139 match state.layout with
1141 | {pageno=pageno} :: rest
-> pageno
1145 if n = Array.length
outlines
1148 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1149 if outlinepageno
>= pageno then n else loop (n+1)
1153 state.mode
<- Outline
1154 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "");
1155 Glut.postRedisplay
();
1159 let enteroutlinemode () =
1161 match state.outlines with
1164 let a = Array.of_list
(List.rev
l) in
1165 state.outlines <- Oarray
a;
1167 | Onarrow
(pat
, a, b) ->
1168 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1170 enterselector false outlines "Document has no outline" msg
;
1173 let enterbookmarkmode () =
1174 let bookmarks = Array.of_list
state.bookmarks in
1175 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1178 let quickbookmark ?title
() =
1179 match state.layout with
1185 let sec = Unix.gettimeofday
() in
1186 let tm = Unix.localtime
sec in
1187 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1191 (tm.Unix.tm_year
+ 1900)
1194 | Some
title -> title
1197 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1201 state.fullscreen
<- None
;
1202 Glut.reshapeWindow
w h;
1205 let writeopen path password
=
1206 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1209 let opendoc path password
=
1212 state.password
<- password
;
1213 state.gen
<- state.gen
+ 1;
1215 writeopen path password
;
1216 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1217 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1221 let zoom = float conf.thumbw
/. float conf.winw
in
1222 let (birdseyepageno
, _
) as anchor = getanchor () in
1224 Birdseye
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno
, -1);
1226 conf.presentation <- false;
1227 conf.interpagespace <- 10;
1228 conf.hlinks
<- false;
1230 state.mstate
<- Mnone
;
1231 conf.showall
<- false;
1232 Glut.setCursor
Glut.CURSOR_INHERIT
;
1235 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1242 let birdseyeoff (c, leftx
, pageno, _
) =
1244 conf.zoom <- c.zoom;
1245 conf.presentation <- c.presentation;
1246 conf.interpagespace <- c.interpagespace;
1247 conf.showall
<- c.showall
;
1248 conf.hlinks
<- c.hlinks
;
1252 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1257 let togglebirdseye () =
1258 match state.mode
with
1259 | Birdseye vals
-> birdseyeoff vals
1260 | View
| Outline _
-> birdseyeon ()
1264 let viewkeyboard ~key ~x ~
y =
1266 state.mode
<- Textentry
(te
, state.mode
);
1269 Glut.postRedisplay
()
1271 let c = Char.chr key
in
1277 let y = getnav () in
1278 gotoy_and_clear_text y
1286 Glut.postRedisplay
()
1289 let ondone isforw
s =
1290 cbput state.hists
.pat
s;
1291 state.searchpattern
<- s;
1294 enttext (c, "", Some
(onhist state.hists
.pat
),
1295 textentry, ondone (c ='
/'
))
1297 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1298 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1299 conf.zoom <- min
2.2 (conf.zoom +. incr);
1300 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1301 reshape conf.winw
conf.winh
1306 try int_of_string
s with exc
->
1307 state.text <- Printf.sprintf
"bad integer `%s': %s"
1308 s (Printexc.to_string exc
);
1314 state.text <- "page bias is now " ^ string_of_int
n;
1317 enttext ('
+'
, "", None
, intentry, ondone)
1319 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1320 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1321 conf.zoom <- max
0.01 (conf.zoom -. decr);
1322 if conf.zoom <= 1.0 then state.x
<- 0;
1323 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1324 reshape conf.winw
conf.winh
;
1330 enttext ('
-'
, "", None
, optentry, ondone)
1332 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1335 state.text <- "zoom is 100%";
1336 reshape conf.winw
conf.winh
1338 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1339 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1344 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1345 reshape conf.winw
conf.winh
;
1348 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1350 reshape conf.winw
conf.winh
;
1355 try int_of_string
s with exc
->
1356 state.text <- Printf.sprintf
"bad integer `%s': %s"
1357 s (Printexc.to_string exc
);
1363 cbput state.hists
.pag
(string_of_int
n);
1364 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1367 let pageentry text key
=
1368 match Char.unsafe_chr key
with
1369 | 'g'
-> TEdone
text
1370 | _
-> intentry text key
1372 let text = "x" in text.[0] <- c;
1373 enttext ('
:'
, text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1376 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1377 reshape conf.winw
conf.winh
;
1380 conf.hlinks
<- not
conf.hlinks
;
1381 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1382 Glut.postRedisplay
()
1385 conf.autoscroll
<- not
conf.autoscroll
1388 conf.presentation <- not
conf.presentation;
1389 showtext ' '
("presentation mode " ^
1390 if conf.presentation then "on" else "off");
1394 begin match state.fullscreen
with
1396 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1399 state.fullscreen
<- None
;
1404 gotoy_and_clear_text 0
1407 search state.searchpattern
true
1410 search state.searchpattern
false
1413 begin match state.layout with
1416 gotoy_and_clear_text (getpagey l.pageno)
1420 begin match List.rev
state.layout with
1423 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1424 gotoy_and_clear_text (getpagey pageno)
1428 begin match state.layout with
1431 let pageno = max
0 (l.pageno-1) in
1432 gotoy_and_clear_text (getpagey pageno)
1437 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1439 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1441 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1445 else (100. *. (float state.y /. float maxy)) in
1448 Printf.sprintf
"Page %d of %d %.2f%%"
1449 (fn+1) state.pagecount
percent
1452 "Pages %d-%d of %d %.2f%%"
1453 (fn+1) (ln
+1) state.pagecount
percent
1458 begin match state.layout with
1461 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1462 Glut.postRedisplay
();
1466 enterbookmarkmode ()
1470 match state.layout with
1473 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1477 enttext ('~'
, "", None
, textentry, ondone)
1481 showtext ' '
"Quick bookmark added";
1484 begin match state.layout with
1486 let rect = getpdimrect
l.pagedimno
in
1490 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1491 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1493 (truncate
(rect.(1) -. rect.(0)),
1494 truncate
(rect.(3) -. rect.(0)))
1498 doreshape (w + conf.scrollw
) (h + conf.interpagespace)
1500 Glut.postRedisplay
();
1506 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1511 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1512 Glut.postRedisplay
()
1514 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1515 | 'j'
-> gotoy (clamp conf.scrollincr
)
1517 | '
r'
-> opendoc state.path
state.password
1520 vlog "huh? %d %c" key
(Char.chr key
);
1523 let textentrykeyboard ~key ~x ~
y ((c, text, opthist
, onkey
, ondone), mode
) =
1525 state.mode
<- Textentry
(te
, mode
);
1528 Glut.postRedisplay
()
1530 match Char.unsafe_chr key
with
1532 let len = String.length
text in
1536 Glut.postRedisplay
();
1539 let s = String.sub
text 0 (len - 1) in
1540 enttext (c, s, opthist
, onkey
, ondone)
1546 Glut.postRedisplay
()
1549 begin match opthist
with
1551 | Some
(_
, onhistcancel
) -> onhistcancel
()
1554 Glut.postRedisplay
()
1557 begin match onkey
text key
with
1561 Glut.postRedisplay
()
1564 enttext (c, text, opthist
, onkey
, ondone);
1568 Glut.postRedisplay
()
1571 state.mode
<- Textentry
(te
, mode
);
1572 Glut.postRedisplay
()
1576 let birdseyekeyboard ~key ~x ~
y ((c, leftx
, pageno, hooverpageno
) as beye
) =
1580 reshape conf.winw
conf.winh
1583 let y, h = getpageyh pageno in
1584 let top = (conf.winh
- h) / 2 in
1585 gotoy (max
0 (y - top))
1590 reshape conf.winw
conf.winh
;
1591 state.anchor <- (pageno, 0.0);
1594 viewkeyboard ~key ~x ~
y
1597 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1598 let narrow outlines pattern
=
1599 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1603 let rec fold accu n =
1607 let (s, _
, _
, _
) as o
= outlines.(n) in
1609 if (try ignore
(Str.search_forward re
s 0); true
1610 with Not_found
-> false)
1616 let matched = fold [] (Array.length
outlines - 1) in
1617 if matched = [] then None
else Some
(Array.of_list
matched)
1619 let search active pattern
incr =
1622 if n = Array.length
outlines || n = -1
1625 let (s, _
, _
, _
) = outlines.(n) in
1627 (try ignore
(Str.search_forward re
s 0); true
1628 with Not_found
-> false)
1630 else loop (n + incr)
1635 let re = Str.regexp_case_fold pattern
in
1641 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1644 if String.length qsearch
= 0
1648 Glut.postRedisplay
();
1652 state.mode
<- Outline
(allowdel
, active, first
, outlines, "");
1653 Glut.postRedisplay
();
1657 let incr = if key
= 18 then -1 else 1 in
1659 match search (active + incr) qsearch
incr with
1661 state.text <- qsearch ^
" [not found]";
1664 state.text <- qsearch
;
1665 active, firstof active
1667 state.mode
<- Outline
(allowdel
, active, first
, outlines, qsearch
);
1668 Glut.postRedisplay
();
1671 let len = String.length qsearch
in
1678 state.mode
<- Outline
(allowdel
, active, first
, outlines, "");
1681 let qsearch = String.sub
qsearch 0 (len - 1) in
1683 match search active qsearch ~
-1 with
1685 state.text <- qsearch ^
" [not found]";
1688 state.text <- qsearch;
1689 active, firstof active
1691 state.mode
<- Outline
(allowdel
, active, first
, outlines, qsearch);
1693 Glut.postRedisplay
()
1696 if active < Array.length
outlines
1698 let (_
, _
, n, t
) = outlines.(active) in
1702 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1704 Glut.postRedisplay
();
1706 | _
when key
>= 32 && key
< 127 ->
1707 let pattern = addchar qsearch (Char.chr key
) in
1709 match search active pattern 1 with
1711 state.text <- pattern ^
" [not found]";
1714 state.text <- pattern;
1715 active, firstof active
1717 state.mode
<- Outline
(allowdel
, active, first
, outlines, pattern);
1718 Glut.postRedisplay
()
1720 | 14 when not allowdel
-> (* ctrl-n *)
1721 if String.length
qsearch > 0
1723 let optoutlines = narrow outlines qsearch in
1724 begin match optoutlines with
1725 | None
-> state.text <- "can't narrow"
1727 state.mode
<- Outline
(allowdel
, 0, 0, outlines, qsearch);
1728 match state.outlines with
1731 state.outlines <- Onarrow
(qsearch, outlines, a)
1732 | Onarrow
(pat
, a, b) ->
1733 state.outlines <- Onarrow
(qsearch, outlines, b)
1736 Glut.postRedisplay
()
1738 | 21 when not allowdel
-> (* ctrl-u *)
1740 match state.outlines with
1743 let a = Array.of_list
(List.rev
l) in
1744 state.outlines <- Oarray
a;
1746 | Onarrow
(pat
, a, b) ->
1747 state.outlines <- Oarray
b;
1751 state.mode
<- Outline
(allowdel
, 0, 0, outline, qsearch);
1752 Glut.postRedisplay
()
1755 state.mode
<- Outline
1756 (allowdel
, active, firstof active, outlines, qsearch);
1757 Glut.postRedisplay
()
1759 | 127 when allowdel
->
1760 let len = Array.length
outlines - 1 in
1764 state.bookmarks <- [];
1767 let bookmarks = Array.init
len
1769 let i = if i >= active then i + 1 else i in
1781 Glut.postRedisplay
()
1783 | _
-> log "unknown key %d" key
1786 let keyboard ~key ~x ~
y =
1791 match state.mode
with
1792 | Outline
outline -> outlinekeyboard ~key ~x ~
y outline
1793 | Textentry
textentry -> textentrykeyboard ~key ~x ~
y textentry
1794 | Birdseye birdseye
-> birdseyekeyboard ~key ~x ~
y birdseye
1795 | View
-> viewkeyboard ~key ~x ~
y
1798 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1801 let pageno = max
0 (pageno - 1) in
1802 let rec loop = function
1803 | [] -> gotopage1nonav pageno 0
1804 | l :: _
when l.pageno = pageno ->
1805 if l.pagedispy
>= 0 && l.pagey = 0
1806 then Glut.postRedisplay
()
1807 else gotopage1nonav pageno 0
1808 | _
:: rest
-> loop rest
1811 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
)
1814 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1815 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
);
1816 let rec loop = function
1818 let y, h = getpageyh pageno in
1819 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1821 | l :: rest
when l.pageno = pageno ->
1822 if l.pagevh != l.pageh
1823 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1824 else Glut.postRedisplay
()
1825 | l :: rest
-> loop rest
1829 | Glut.KEY_PAGE_UP
->
1830 begin match state.layout with
1834 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1835 gotopage1nonav l.pageno 0;
1838 let layout = layout (state.y-conf.winh
) conf.winh
in
1840 | [] -> gotoy (clamp (-conf.winh
))
1842 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1843 gotopage1nonav l.pageno 0
1846 | [] -> gotoy (clamp (-conf.winh
))
1849 | Glut.KEY_PAGE_DOWN
->
1850 begin match List.rev
state.layout with
1852 state.mode
<- Birdseye
(conf, leftx
, l.pageno, hooverpageno
);
1853 gotoy (clamp (l.pagedispy
+ l.pageh
))
1854 | [] -> gotoy (clamp conf.winh
)
1858 state.mode
<- Birdseye
(conf, leftx
, 0, hooverpageno
);
1862 let pageno = state.pagecount
- 1 in
1863 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
);
1864 if not
(pagevisible state.layout pageno)
1867 match List.rev
state.pdims
with
1869 | (_
, _
, h, _
) :: _
-> h
1871 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1872 else Glut.postRedisplay
();
1876 let special ~key ~x ~
y =
1877 match state.mode
with
1878 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
1880 reshape conf.winw
conf.winh
;
1883 birdseyespecial key x
y vals
1885 | View
| Textentry _
->
1886 begin match state.mode
with
1890 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1891 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1892 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1893 | Glut.KEY_PAGE_UP
->
1894 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1896 match state.layout with
1898 | l :: _
-> state.y - l.pagey
1901 | Glut.KEY_PAGE_DOWN
->
1902 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1904 match List.rev
state.layout with
1906 | l :: _
-> getpagey l.pageno
1909 | Glut.KEY_HOME
-> addnav (); 0
1912 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1914 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1915 state.x
<- state.x
- 10;
1917 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1918 state.x
<- state.x
+ 10;
1923 gotoy_and_clear_text y
1926 ((c, s, (Some
(action, _
) as onhist), onkey
, ondone), mode
) ->
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), mode
);
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 viewmouse 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 Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2244 if bstate
= Glut.DOWN
2246 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2247 state.mstate
<- Mpan
(x, y)
2250 state.mstate
<- Mnone
2252 | Glut.LEFT_BUTTON
when x > conf.winw
- conf.scrollw
->
2253 if bstate
= Glut.DOWN
2255 let position, sh = scrollph state.y in
2256 if y > truncate
position && y < truncate
(position +. sh)
2258 state.mstate
<- Mscroll
2260 let percent = float y /. float conf.winh
in
2261 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2263 state.mstate
<- Mscroll
2265 state.mstate
<- Mnone
2267 | Glut.LEFT_BUTTON
->
2268 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2269 begin match dest with
2270 | Ulinkgoto
(pageno, top) ->
2273 gotopage1 pageno top
2278 | Unone
when bstate
= Glut.DOWN
->
2279 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2280 state.mstate
<- Mpan
(x, y);
2282 | Unone
| Utext _
->
2283 if bstate
= Glut.DOWN
2285 if conf.angle
mod 360 = 0
2287 state.mstate
<- Msel
((x, y), (x, y));
2288 Glut.postRedisplay
()
2292 match state.mstate
with
2296 state.mstate
<- Mnone
2299 Glut.setCursor
Glut.CURSOR_INHERIT
;
2300 state.mstate
<- Mnone
2302 | Msel
((x0
, y0
), (x1
, y1
)) ->
2304 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2305 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2307 match getopaque l.pageno with
2308 | Some
(opaque, _
) when validopaque opaque ->
2312 List.iter
f state.layout;
2313 copysel
""; (* ugly *)
2314 Glut.setCursor
Glut.CURSOR_INHERIT
;
2315 state.mstate
<- Mnone
;
2322 let birdseyemouse button bstate
x y (conf, leftx
, pageno, hooverpageno
) =
2324 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
2325 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2326 let rec loop = function
2329 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2330 && x > margin && x < margin + l.pagew
2332 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2333 reshape conf.winw
conf.winh
;
2334 state.anchor <- (l.pageno, 0.0);
2342 let mouse bstate button
x y =
2343 match state.mode
with
2344 | View
-> viewmouse button bstate
x y
2345 | Birdseye beye
-> birdseyemouse button bstate
x y beye
2350 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
2353 match state.mode
with
2356 match state.mstate
with
2362 state.mstate
<- Mpan
(x, y);
2363 if conf.zoom > 1.0 then state.x <- state.x + dx;
2365 gotoy_and_clear_text y
2368 state.mstate
<- Msel
(a, (x, y));
2369 Glut.postRedisplay
()
2372 let y = min
conf.winh
(max
0 y) in
2373 let percent = float y /. float conf.winh
in
2374 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2375 gotoy_and_clear_text y
2379 match state.mode
with
2380 | Birdseye
(conf, leftx
, pageno, hooverpageno
) ->
2381 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2382 let rec loop = function
2384 if hooverpageno
!= -1
2386 state.mode
<- Birdseye
(conf, leftx
, pageno, -1);
2387 Glut.postRedisplay
();
2390 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2391 && x > margin && x < margin + l.pagew
2393 state.mode
<- Birdseye
(conf, leftx
, pageno, l.pageno);
2394 Glut.postRedisplay
();
2402 match state.mstate
with
2404 begin match getunder x y with
2405 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2407 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2408 Glut.setCursor
Glut.CURSOR_INFO
2409 | Ulinkgoto
(page
, y) ->
2411 then showtext 'p'
("age: " ^ string_of_int page
);
2412 Glut.setCursor
Glut.CURSOR_INFO
2414 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2415 Glut.setCursor
Glut.CURSOR_TEXT
2418 | Mpan _
| Msel _
| Mscroll
->
2429 match Sys.os_type
with
2430 | "Win32" -> Sys.getenv
"HOMEPATH"
2431 | _
-> Sys.getenv
"HOME"
2434 ("Can not determine home directory location: " ^
2435 Printexc.to_string exn
);
2439 let config_of c attrs
=
2443 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2444 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2445 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2446 | "preload" -> { c with preload = bool_of_string v
}
2447 | "page-bias" -> { c with pagebias
= int_of_string v
}
2448 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2449 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2450 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2451 | "throttle" -> { c with showall
= bool_of_string v
}
2452 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2453 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2454 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2456 let zoom = float_of_string v
/. 100. in
2457 let zoom = max
0.01 (min
2.2 zoom) in
2458 { c with zoom = zoom }
2459 | "presentation" -> { c with presentation = bool_of_string v
}
2460 | "rotation-angle" -> { c with angle
= int_of_string v
}
2461 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2462 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2463 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2464 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2465 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2466 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2467 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2468 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2471 prerr_endline
("Error processing attribute (`" ^
2472 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2475 let rec fold c = function
2478 let c = apply c k v
in
2484 let bookmark_of attrs
=
2485 let rec fold title page rely
= function
2486 | ("title", v
) :: rest
-> fold v page rely rest
2487 | ("page", v
) :: rest
-> fold title v rely rest
2488 | ("rely", v
) :: rest
-> fold title page v rest
2489 | _
:: rest
-> fold title page rely rest
2490 | [] -> title, page
, rely
2492 fold "invalid" "0" "0" attrs
2495 let setconf dst src
=
2496 dst
.scrollw
<- src
.scrollw
;
2497 dst
.scrollh
<- src
.scrollh
;
2498 dst
.icase
<- src
.icase
;
2499 dst
.preload <- src
.preload;
2500 dst
.pagebias
<- src
.pagebias
;
2501 dst
.verbose
<- src
.verbose
;
2502 dst
.scrollincr
<- src
.scrollincr
;
2503 dst
.maxhfit
<- src
.maxhfit
;
2504 dst
.crophack
<- src
.crophack
;
2505 dst
.autoscroll
<- src
.autoscroll
;
2506 dst
.showall
<- src
.showall
;
2507 dst
.hlinks
<- src
.hlinks
;
2508 dst
.underinfo
<- src
.underinfo
;
2509 dst
.interpagespace <- src
.interpagespace;
2510 dst
.zoom <- src
.zoom;
2511 dst
.presentation <- src
.presentation;
2512 dst
.angle
<- src
.angle
;
2513 dst
.winw
<- src
.winw
;
2514 dst
.winh
<- src
.winh
;
2515 dst
.savebmarks
<- src
.savebmarks
;
2516 dst
.memlimit
<- src
.memlimit
;
2517 dst
.proportional
<- src
.proportional
;
2518 dst
.texcount
<- src
.texcount
;
2519 dst
.sliceheight
<- src
.sliceheight
;
2520 dst
.thumbw
<- src
.thumbw
;
2524 let l = String.length
s in
2525 let b = Buffer.create
l in
2531 let h = Hashtbl.create
10 in
2532 let dc = { defconf with angle
= defconf.angle
} in
2533 let rec toplevel v t spos epos
=
2535 | Vdata
| Vcdata
| Vend
-> v
2536 | Vopen
("llppconfig", attrs
, closed
) ->
2539 else { v
with f = llppconfig
}
2541 error
"unexpected subelement at top level" s spos
2542 | Vclose tag
-> error
"unexpected close at top level" s spos
2544 and llppconfig v t spos epos
=
2546 | Vdata
| Vcdata
| Vend
-> v
2547 | Vopen
("defaults", attrs
, closed
) ->
2548 let c = config_of dc attrs
in
2552 else { v
with f = skip
"defaults" (fun () -> v
) }
2554 | Vopen
("doc", attrs
, closed
) ->
2557 List.assoc
"path" attrs
2558 with Not_found
-> error
"doc is missing path attribute" s spos
2560 let path = unent pathent in
2561 let c = config_of dc attrs
in
2562 let pageno, rely
, x =
2566 dolog "error accessing %s (%S) at postion %d:\n %s"
2567 n v spos
(Printexc.to_string exn
);
2570 let rec fold pageno rely
x = function
2571 | [] -> pageno, rely
, x
2572 | ("rely", v
) :: rest
->
2573 fold pageno (safef float_of_string
"rely" v
0.0) x rest
2574 | ("page", v
) :: rest
->
2575 fold (safef int_of_string
"page" v
0) rely
x rest
2576 | ("x", v
) :: rest
->
2577 fold pageno rely
(safef int_of_string
"x" v
0) rest
2579 fold pageno rely
x rest
2583 let anchor = (pageno, rely
) in
2585 then (Hashtbl.add
h path (c, [], x, anchor); v
)
2586 else { v
with f = doc
path x anchor c [] }
2588 | Vopen
(tag
, _
, closed
) ->
2589 error
"unexpected subelement in llppconfig" s spos
2591 | Vclose
"llppconfig" -> { v
with f = toplevel }
2592 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2594 and doc
path x anchor c bookmarks v t spos epos
=
2596 | Vdata
| Vcdata
-> v
2597 | Vend
-> error
"unexpected end of input in doc" s spos
2598 | Vopen
("bookmarks", attrs
, closed
) ->
2599 { v
with f = pbookmarks
path x anchor c bookmarks }
2601 | Vopen
(tag
, _
, _
) ->
2602 error
"unexpected subelement in doc" s spos
2605 Hashtbl.add
h path (c, List.rev
bookmarks, x, anchor);
2606 { v
with f = llppconfig
}
2608 | Vclose tag
-> error
"unexpected close in doc" s spos
2610 and pbookmarks
path x anchor c bookmarks v t spos epos
=
2612 | Vdata
| Vcdata
-> v
2613 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2614 | Vopen
("item", attrs
, closed
) ->
2615 let titleent, spage
, srely
= bookmark_of attrs
in
2620 dolog "Failed to convert page %S to integer: %s"
2621 spage
(Printexc.to_string exn
);
2626 float_of_string srely
2628 dolog "Failed to convert rely %S to real: %s"
2629 srely
(Printexc.to_string exn
);
2632 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2634 then { v
with f = pbookmarks
path x anchor c bookmarks }
2637 { v
with f = skip
"item" f }
2640 error
"unexpected subelement in bookmarks" s spos
2642 | Vclose
"bookmarks" ->
2643 { v
with f = doc
path x anchor c bookmarks }
2645 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2647 and skip tag
f v t spos epos
=
2649 | Vdata
| Vcdata
-> v
2651 error
("unexpected end of input in skipped " ^ tag
) s spos
2652 | Vopen
(tag'
, _
, closed
) ->
2656 let f'
() = { v
with f = skip tag
f } in
2657 { v
with f = skip tag'
f'
}
2661 else error
("unexpected close in skipped " ^ tag
) s spos
2664 parse
{ f = toplevel; accu = () } s;
2670 let len = in_channel_length ic
in
2671 let s = String.create
len in
2672 really_input ic
s 0 len;
2675 | Parse_error
(msg
, s, pos
) ->
2676 let subs = subs s pos
in
2677 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2678 failwith
("parse error: " ^
s)
2681 failwith
("config load error: " ^
Printexc.to_string exn
)
2687 let dir = Filename.concat
home ".config" in
2688 if Sys.is_directory
dir then dir else home
2691 Filename.concat
dir "llpp.conf"
2695 if Sys.file_exists
path
2698 (try Some
(open_in_bin
path)
2701 ("Error opening configuation file `" ^
path ^
"': " ^
2702 Printexc.to_string exn
);
2711 ("Error loading configuation from `" ^
path ^
"': " ^
2712 Printexc.to_string exn
);
2718 f (Hashtbl.create
0, defconf)
2723 let pc, pb
, px
, pa
=
2725 Hashtbl.find
h (Filename.basename
state.path)
2726 with Not_found
-> dc, [], 0, (0, 0.0)
2730 state.bookmarks <- pb
;
2732 cbput state.hists
.nav pa
;
2737 let add_attrs bb always
dc c =
2740 then Printf.bprintf bb
"\n %s='%b'" s a
2743 then Printf.bprintf bb
"\n %s='%d'" s a
2746 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2750 then dc.winw
, dc.winh
2752 match state.fullscreen
with
2754 | None
-> c.winw
, c.winh
2756 let zoom, presentation, interpagespace, showall
=
2758 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2760 match state.mode
with
2761 | Birdseye
(bc
, _
, _
, _
) ->
2762 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2763 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2765 oi
"width" w dc.winw
;
2766 oi
"height" h dc.winh
;
2767 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2768 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2769 ob "case-insensitive-search" c.icase
dc.icase
;
2770 ob "preload" c.preload dc.preload;
2771 oi
"page-bias" c.pagebias
dc.pagebias
;
2772 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2773 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2774 ob "crop-hack" c.crophack
dc.crophack
;
2775 ob "throttle" showall
dc.showall
;
2776 ob "highlight-links" c.hlinks
dc.hlinks
;
2777 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2778 oi
"vertical-margin" interpagespace dc.interpagespace;
2779 oz
"zoom" zoom dc.zoom;
2780 ob "presentation" presentation dc.presentation;
2781 oi
"rotation-angle" c.angle
dc.angle
;
2782 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2783 ob "proportional-display" c.proportional
dc.proportional
;
2784 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2785 oi
"texcount" c.texcount
dc.texcount
;
2786 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2787 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2791 let bb = Buffer.create
32768 in
2793 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2794 add_attrs bb true dc dc;
2795 Buffer.add_string
bb "/>\n";
2797 let adddoc path x anchor c bookmarks =
2798 if bookmarks == [] && c = dc && anchor = emptyanchor
2801 Printf.bprintf
bb "<doc path='%s'"
2802 (enent
path 0 (String.length
path));
2804 if anchor <> emptyanchor
2806 let n, y = anchor in
2807 Printf.bprintf
bb " page='%d'" n;
2808 Printf.bprintf
bb " rely='%f'" y;
2812 then Printf.bprintf
bb " pan='%d'" x;
2814 add_attrs bb false dc c;
2816 begin match bookmarks with
2817 | [] -> Buffer.add_string
bb "/>\n"
2819 Buffer.add_string
bb ">\n<bookmarks>\n";
2820 List.iter
(fun (title, _level
, page, rely) ->
2822 "<item title='%s' page='%d' rely='%f'/>\n"
2823 (enent
title 0 (String.length
title))
2827 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2833 match state.mode
with
2834 | Birdseye
(_
, x, _
, _
) -> x
2837 let basename = Filename.basename state.path in
2838 adddoc basename x (getanchor ()) conf
2839 (if conf.savebmarks
then state.bookmarks else []);
2841 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2843 then adddoc path x y c bookmarks
2845 Buffer.add_string
bb "</llppconfig>";
2848 if Buffer.length
bb > 0
2851 let tmp = path ^
".tmp" in
2852 let oc = open_out_bin
tmp in
2853 Buffer.output_buffer
oc bb;
2855 Sys.rename
tmp path;
2858 ("error while saving configuration: " ^
Printexc.to_string exn
)
2864 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2865 (fun s -> state.path <- s)
2866 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2868 if String.length
state.path = 0
2869 then (prerr_endline
"filename missing"; exit
1);
2873 let _ = Glut.init
Sys.argv
in
2874 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2875 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2876 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
2879 if Sys.os_type
= "Unix"
2881 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2883 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2884 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2885 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2886 Unix.bind
sock addr;
2888 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2889 Unix.connect
csock addr;
2890 let ssock, _ = Unix.accept
sock in
2893 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2894 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2898 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2902 let () = Glut.displayFunc
display in
2903 let () = Glut.reshapeFunc
reshape in
2904 let () = Glut.keyboardFunc
keyboard in
2905 let () = Glut.specialFunc
special in
2906 let () = Glut.idleFunc
(Some
idle) in
2907 let () = Glut.mouseFunc
mouse in
2908 let () = Glut.motionFunc
motion in
2909 let () = Glut.passiveMotionFunc
pmotion in
2911 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2912 state.csock <- csock;
2913 state.ssock <- ssock;
2914 state.text <- "Opening " ^
state.path;
2915 writeopen state.path state.password
;
2919 let rec handlelablglutbug () =
2922 with Glut.BadEnum
"key in special_of_int" ->
2923 showtext '
!'
" LablGlut bug: special key not recognized";
2924 handlelablglutbug ()
2926 handlelablglutbug ();