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 presentation
= bool
22 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
: zoom
147 ; mutable presentation
: presentation
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
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);;
169 { mutable csock
: Unix.file_descr
170 ; mutable ssock
: Unix.file_descr
176 ; mutable layout
: layout list
178 ((pageno
* width
* angle
* proportional
), (opaque
* pixmapsize
)) Hashtbl.t
179 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
180 ; mutable pagecount
: int
181 ; pagecache
: string circbuf
182 ; mutable rendering
: bool
183 ; mutable mstate
: mstate
184 ; mutable searchpattern
: string
185 ; mutable rects
: (pageno
* recttype
* rect
) list
186 ; mutable rects1
: (pageno
* recttype
* rect
) list
187 ; mutable text
: string
188 ; mutable fullscreen
: (width
* height
) option
189 ; mutable birdseye
: (zoom
* leftx
* presentation
* interpagespace
) option
190 ; mutable textentry
: textentry
option
191 ; mutable outlines
: outlines
192 ; mutable outline
: (bool * int * int * outline array
* string) option
193 ; mutable bookmarks
: outline list
194 ; mutable path
: string
195 ; mutable password
: string
196 ; mutable invalidated
: int
197 ; mutable colorscale
: float
198 ; mutable memused
: int
199 ; mutable birdseyepageno
: pageno
203 { pat
: string circbuf
204 ; pag
: string circbuf
205 ; nav
: float circbuf
225 ; presentation
= false
230 ; proportional
= true
231 ; memlimit
= 32*1024*1024
237 let conf = { defconf with angle
= defconf.angle
};;
248 ; pagemap
= Hashtbl.create
10
249 ; pagecache
= cbnew 100 ""
261 ; outlines
= Olist
[]
268 { nav
= cbnew 100 0.0
281 Printf.kprintf prerr_endline fmt
283 Printf.kprintf ignore fmt
287 let len = String.length s
in
289 let b = Buffer.create
n in
290 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
291 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
292 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
293 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
294 Buffer.add_string
b s
;
295 let s'
= Buffer.contents
b in
296 let n'
= Unix.write fd
s'
0 n in
297 if n'
!= n then failwith
"write failed";
302 let n = Unix.read fd
s 0 4 in
303 if n != 4 then failwith
"incomplete read(len)";
305 lor (Char.code
s.[0] lsl 24)
306 lor (Char.code
s.[1] lsl 16)
307 lor (Char.code
s.[2] lsl 8)
308 lor (Char.code
s.[3] lsl 0)
310 let s = String.create
len in
311 let n = Unix.read fd
s 0 len in
312 if n != len then failwith
"incomplete read(data)";
319 else float y
/. float state.maxy
323 let b = Buffer.create
10 in
324 Buffer.add_string
b s;
325 let rec combine = function
328 Buffer.add_char
b ' '
;
331 | `
b b -> if b then "1" else "0"
333 | `i i
-> string_of_int i
334 | `f f
-> string_of_float f
335 | `I f
-> string_of_int
(truncate f
)
337 Buffer.add_string
b s;
344 let cmd = Buffer.contents
(makecmd s l
) in
345 writecmd state.csock
cmd;
351 let d = conf.winh
- h
in
358 let rec f pn ph pi fh l
=
360 | (n, _
, h
, _
) :: rest
->
361 let ips = calcips h
in
366 if state.birdseye
<> None
&& pn
= 0
371 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
376 if conf.presentation
|| (state.birdseye
<> None
&& pn
= 0)
380 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
383 let fh = f 0 0 0 0 state.pdims
in
387 let getpageyh pageno
=
388 let rec f pn ph pi y l
=
390 | (n, _
, h
, _
) :: rest
->
391 let ips = calcips h
in
394 if conf.presentation
&& n = pageno
396 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
398 y
+ (pageno
- pn
) * (ph
+ pi
), h
400 let y = y + (if conf.presentation
then pi
else 0) in
401 let y = y + (n - pn
) * (ph
+ pi
) in
405 y + (pageno
- pn
) * (ph
+ pi
), ph
407 f 0 0 0 0 state.pdims
410 let getpagey pageno
= fst
(getpageyh pageno
);;
413 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
414 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
416 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
417 let ips = calcips h
in
419 if conf.presentation
|| (state.birdseye
<> None
&& pageno
= 0)
423 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
425 prev
, pdims
, pdimno
, 0
427 let dy = dy + yinc in
428 let py = py + yinc in
429 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
434 if py + h
<= vy - yinc
436 let py = py + h
+ ips in
437 let dy = max
0 (py - y) in
447 let pagey = vy - py in
448 let pagevh = h
- pagey in
449 let pagevh = min
(sh
- dy) pagevh in
450 let off = if yinc > 0 then py - vy else 0 in
451 let py = py + h
+ ips in
457 ; pagedispy
= dy + off
458 ; pagey = pagey + off
459 ; pagevh = pagevh - off
463 let accu = e :: accu in
470 ~cacheleft
:(cacheleft
-1)
473 if state.invalidated
= 0
483 ~cacheleft
:(cbcap state.pagecache
)
493 let y = state.y + incr
in
495 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
499 let getopaque pageno
=
500 try Some
(Hashtbl.find
state.pagemap
501 (pageno
, state.w
, conf.angle
, conf.proportional
))
502 with Not_found
-> None
505 let cache pageno opaque
=
506 Hashtbl.replace
state.pagemap
507 (pageno
, state.w
, conf.angle
, conf.proportional
) opaque
510 let validopaque opaque
= String.length opaque
> 0;;
513 match getopaque l
.pageno
with
514 | None
when not
state.rendering
->
515 state.rendering
<- true;
516 cache l
.pageno
("", -1);
517 wcmd "render" [`i
(l
.pageno
+ 1)
525 let loadlayout layout =
526 let rec f all
= function
528 begin match getopaque l
.pageno
with
529 | None
-> render l
; f false ls
530 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
534 f (layout <> []) layout;
537 let findpageforopaque opaque
=
539 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
543 let pagevisible n = List.exists
(fun l
-> l
.pageno
= n) state.layout;;
549 let opaque = cbpeek state.pagecache
in
550 match findpageforopaque opaque with
551 | Some
((n, _
, _
, _
), size
) ->
552 not
(pagevisible n) && state.memused
- size
<= conf.memlimit
557 let rely = yratio state.y in
558 let presentation = conf.presentation in
559 let interpagespace = conf.interpagespace in
560 let maxy = state.maxy in
561 conf.presentation <- false;
562 conf.interpagespace <- 0;
563 state.maxy <- calcheight ();
564 let y = truncate
(float state.maxy *. rely) in
565 let y = if y < conf.winh
then 0 else y - conf.winh
in
566 let pages = layout y (conf.winh
*3) in
567 List.iter
render pages;
568 conf.presentation <- presentation;
569 conf.interpagespace <- interpagespace;
575 let y = min
state.maxy y in
576 let pages = layout y conf.winh
in
577 let ready = loadlayout pages in
578 state.ty
<- yratio y;
583 state.layout <- pages;
585 Glut.postRedisplay
();
589 state.layout <- pages;
591 Glut.postRedisplay
();
593 if state.birdseye
<> None
595 if not
(pagevisible state.birdseyepageno
)
597 match state.layout with
599 | l
:: _
-> state.birdseyepageno
<- l
.pageno
604 let gotoy_and_clear_text y =
606 if not
conf.verbose
then state.text
<- "";
610 cbput state.hists
.nav
(yratio state.y);
614 let y = cbgetc state.hists
.nav ~
-1 in
615 truncate
(y *. float state.maxy)
619 let y, h
= getpageyh n in
621 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
624 let gotopage1 n top
=
625 let y = getpagey n in
627 gotoy_and_clear_text (y + top
);
635 state.invalidated
<- state.invalidated
+ 1;
639 let c = c *. state.colorscale
in
645 match state.layout with
647 let rely = yratio state.y in
648 state.maxy <- calcheight ();
649 truncate
(float state.maxy *. rely)
652 state.maxy <- calcheight ();
659 GlMat.mode `projection
;
660 GlMat.load_identity
();
661 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
662 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
663 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
667 GlMat.mode `projection
;
668 GlMat.load_identity
();
669 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
670 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
671 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
676 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
680 GlMat.mode `modelview
;
681 GlMat.load_identity
();
682 GlClear.color
(scalecolor 1.0);
683 GlClear.clear
[`color
];
686 wcmd "geometry" [`i
w; `i h
];
690 GlDraw.color
(0.0, 0.0, 0.0);
692 (0.0, float (conf.winh
- 18))
693 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
695 let font = Glut.BITMAP_8_BY_13
in
696 GlDraw.color
(1.0, 1.0, 1.0);
697 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
698 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
699 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
703 let len = String.length
state.text
in
704 match state.textentry
with
706 if len > 0 then showtext ' '
state.text
708 | Some
(c, text
, _
, _
, _
) ->
712 text ^
" [" ^
state.text ^
"]"
722 state.text
<- Printf.sprintf
"%c%s" c s;
723 Glut.postRedisplay
();
737 state.rects
<- state.rects1
;
738 Glut.postRedisplay
()
741 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
742 state.pagecount
<- n;
743 state.invalidated
<- state.invalidated
- 1;
744 if state.invalidated
= 0
748 let s = Scanf.sscanf
cmd "t %n"
749 (fun n -> String.sub
cmd n (String.length
cmd - n))
751 Glut.setWindowTitle
s
754 let s = Scanf.sscanf
cmd "T %n"
755 (fun n -> String.sub
cmd n (String.length
cmd - n))
757 if state.textentry
= None
764 Glut.postRedisplay
();
770 let s = Scanf.sscanf
cmd "V %n"
771 (fun n -> String.sub
cmd n (String.length
cmd - n))
777 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
778 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
779 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
780 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
782 let y = (getpagey pageno) + truncate y0
in
785 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
788 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
789 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
790 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
791 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
794 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
797 let n, w, h
, r
, l
, s, p
=
798 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
799 (fun n w h r l
s p
->
800 (n-1, w, h
, r
, l
!= 0, s, p
))
803 Hashtbl.replace
state.pagemap
(n, w, r
, l
) (p
, s);
804 state.memused
<- state.memused
+ s;
807 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
810 let evictedopaque = cbpeek state.pagecache
in
811 match findpageforopaque evictedopaque with
812 | None
-> failwith
"bug in gc"
813 | Some
((evictedn
, _
, _
, _
) as k
, evictedsize
) ->
814 if not
(pagevisible evictedn
)
816 wcmd "free" [`
s evictedopaque];
817 state.memused
<- state.memused
- evictedsize
;
818 Hashtbl.remove
state.pagemap k
;
819 cbdecr state.pagecache
;
826 cbput state.pagecache p
;
827 state.rendering
<- false;
830 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
834 else (ignore
(loadlayout state.layout); preload ())
838 let (n, w, h
, x
) as pdim
=
839 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
841 state.pdims
<- pdim
:: state.pdims
844 let (l
, n, t
, h
, pos
) =
845 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
847 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
849 let l = String.length
s in
850 let b = Buffer.create
(String.length
s) in
857 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
860 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
869 let outline = (s, l, n, float t
/. float h
) in
871 match state.outlines with
872 | Olist
outlines -> Olist
(outline :: outlines)
873 | Oarray _
-> Olist
[outline]
874 | Onarrow _
-> Olist
[outline]
876 state.outlines <- outlines
879 log "unknown cmd `%S'" cmd
882 let now = Unix.gettimeofday
;;
886 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
891 let y = state.y + conf.scrollincr
in
892 let y = if y >= state.maxy then 0 else y in
898 let cmd = readcmd state.csock
in
907 let action = function
908 | HCprev
-> cbget cb ~
-1
909 | HCnext
-> cbget cb
1
910 | HCfirst
-> cbget cb ~
-(cb
.rc)
911 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
912 and cancel
() = cb
.rc <- rc
916 let search pattern forward
=
917 if String.length pattern
> 0
920 match state.layout with
923 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
926 let b = makecmd "search"
927 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
929 Buffer.add_char
b '
,'
;
930 Buffer.add_string
b pattern
;
931 Buffer.add_char
b '
\000'
;
934 writecmd state.csock
cmd;
937 let intentry text key
=
938 let c = Char.unsafe_chr key
in
941 let s = "x" in s.[0] <- c;
942 let text = text ^
s in
946 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
951 let b = Buffer.create
(String.length
s + 1) in
952 Buffer.add_string
b s;
957 let textentry text key
=
958 let c = Char.unsafe_chr key
in
960 | _
when key
>= 32 && key
< 127 ->
961 let text = addchar text c in
965 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
969 let reinit angle proportional
=
971 conf.proportional
<- proportional
;
973 wcmd "reinit" [`i angle
; `
b proportional
];
976 let optentry text key
=
977 let btos b = if b then "on" else "off" in
978 let c = Char.unsafe_chr key
in
982 try conf.scrollincr
<- int_of_string
s with exc
->
983 state.text <- Printf.sprintf
"bad integer `%s': %s"
984 s (Printexc.to_string exc
)
986 TEswitch
('#'
, "", None
, intentry, ondone)
991 Some
(int_of_string
s)
993 state.text <- Printf.sprintf
"bad integer `%s': %s"
994 s (Printexc.to_string exc
);
997 | Some angle
-> reinit angle
conf.proportional
1000 TEswitch
('^'
, "", None
, intentry, ondone)
1003 conf.icase
<- not
conf.icase
;
1004 TEdone
("case insensitive search " ^
(btos conf.icase
))
1007 conf.preload <- not
conf.preload;
1009 TEdone
("preload " ^
(btos conf.preload))
1012 conf.verbose
<- not
conf.verbose
;
1013 TEdone
("verbose " ^
(btos conf.verbose
))
1016 conf.maxhfit
<- not
conf.maxhfit
;
1017 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1018 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1021 conf.crophack
<- not
conf.crophack
;
1022 TEdone
("crophack " ^
btos conf.crophack
)
1025 conf.showall
<- not
conf.showall
;
1026 TEdone
("showall " ^
btos conf.showall
)
1029 conf.underinfo
<- not
conf.underinfo
;
1030 TEdone
("underinfo " ^
btos conf.underinfo
)
1033 conf.savebmarks
<- not
conf.savebmarks
;
1034 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1039 conf.interpagespace <- int_of_string
s;
1040 let rely = yratio state.y in
1041 state.maxy <- calcheight ();
1042 gotoy (truncate
(float state.maxy *. rely));
1044 state.text <- Printf.sprintf
"bad integer `%s': %s"
1045 s (Printexc.to_string exc
)
1047 TEswitch
('
%'
, "", None
, intentry, ondone)
1050 reinit conf.angle
(not
conf.proportional
);
1051 TEdone
("proprortional display " ^
btos conf.proportional
)
1054 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1058 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1060 let enterselector allowdel
outlines errmsg msg
=
1061 if Array.length
outlines = 0
1063 showtext ' ' errmsg
;
1067 Glut.setCursor
Glut.CURSOR_INHERIT
;
1069 match state.layout with
1071 | {pageno=pageno} :: rest
-> pageno
1075 if n = Array.length
outlines
1078 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1079 if outlinepageno
>= pageno then n else loop (n+1)
1084 Some
(allowdel
, active,
1085 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1086 Glut.postRedisplay
();
1090 let enteroutlinemode () =
1092 match state.outlines with
1095 let a = Array.of_list
(List.rev
l) in
1096 state.outlines <- Oarray
a;
1098 | Onarrow
(pat
, a, b) ->
1099 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1101 enterselector false outlines "Document has no outline" msg
;
1104 let enterbookmarkmode () =
1105 let bookmarks = Array.of_list
state.bookmarks in
1106 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1109 let quickbookmark ?title
() =
1110 match state.layout with
1116 let sec = Unix.gettimeofday
() in
1117 let tm = Unix.localtime
sec in
1118 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1122 (tm.Unix.tm_year
+ 1900)
1125 | Some
title -> title
1128 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1132 state.fullscreen
<- None
;
1133 Glut.reshapeWindow
w h
;
1136 let writeopen path password
=
1137 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1140 let opendoc path password
=
1143 state.password
<- password
;
1144 Hashtbl.clear
state.pagemap
;
1146 writeopen path password
;
1147 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1148 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1151 let birdseyeoff (zoom
, x
, presentation, interpagespace) =
1152 state.birdseye
<- None
;
1154 conf.presentation <- presentation;
1155 conf.interpagespace <- interpagespace;
1157 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1161 let viewkeyboard ~key ~x ~
y =
1163 state.textentry <- te
;
1166 Glut.postRedisplay
()
1168 match state.textentry with
1170 let c = Char.chr key
in
1176 let y = getnav () in
1177 gotoy_and_clear_text y
1180 begin match state.birdseye
with
1183 let y = getpagey state.birdseyepageno
in
1186 reshape conf.winw
conf.winh
;
1195 Glut.postRedisplay
()
1198 let ondone isforw
s =
1199 cbput state.hists
.pat
s;
1200 state.searchpattern
<- s;
1203 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1204 textentry, ondone (c ='
/'
)))
1206 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1207 let incr = if conf.zoom
+. 0.01 > 0.1 then 0.1 else 0.01 in
1208 conf.zoom
<- min
2.2 (conf.zoom
+. incr);
1209 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1210 reshape conf.winw
conf.winh
1215 try int_of_string
s with exc
->
1216 state.text <- Printf.sprintf
"bad integer `%s': %s"
1217 s (Printexc.to_string exc
);
1223 state.text <- "page bias is now " ^ string_of_int
n;
1226 enttext (Some
('
+'
, "", None
, intentry, ondone))
1228 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1229 let decr = if conf.zoom
-. 0.1 < 0.1 then 0.01 else 0.1 in
1230 conf.zoom
<- max
0.01 (conf.zoom
-. decr);
1231 if conf.zoom
<= 1.0 then state.x
<- 0;
1232 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1233 reshape conf.winw
conf.winh
;
1239 enttext (Some
('
-'
, "", None
, optentry, ondone))
1241 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1244 state.text <- "zoom is 100%";
1245 reshape conf.winw
conf.winh
1247 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1248 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1253 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1254 reshape conf.winw
conf.winh
;
1257 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1258 begin match state.birdseye
with
1260 let zoom = 50.0 /. float state.w in
1261 state.birdseye
<- Some
(
1265 conf.interpagespace;
1268 conf.presentation <- false;
1269 conf.interpagespace <- 10;
1271 state.mstate
<- Mnone
;
1272 Glut.setCursor
Glut.CURSOR_INHERIT
;
1273 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1279 reshape conf.winw
conf.winh
1284 try int_of_string
s with exc
->
1285 state.text <- Printf.sprintf
"bad integer `%s': %s"
1286 s (Printexc.to_string exc
);
1292 cbput state.hists
.pag
(string_of_int
n);
1293 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1296 let pageentry text key
=
1297 match Char.unsafe_chr key
with
1298 | 'g'
-> TEdone
text
1299 | _
-> intentry text key
1301 let text = "x" in text.[0] <- c;
1302 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1306 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1307 reshape conf.winw
conf.winh
;
1310 conf.hlinks
<- not
conf.hlinks
;
1311 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1312 Glut.postRedisplay
()
1315 conf.autoscroll
<- not
conf.autoscroll
1318 conf.presentation <- not
conf.presentation;
1319 showtext ' '
("presentation mode " ^
1320 if conf.presentation then "on" else "off");
1324 begin match state.fullscreen
with
1326 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1329 state.fullscreen
<- None
;
1334 gotoy_and_clear_text 0
1337 search state.searchpattern
true
1340 search state.searchpattern
false
1343 begin match state.layout with
1346 gotoy_and_clear_text (getpagey l.pageno)
1350 begin match List.rev
state.layout with
1353 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1354 gotoy_and_clear_text (getpagey pageno)
1358 begin match state.layout with
1361 let pageno = max
0 (l.pageno-1) in
1362 gotoy_and_clear_text (getpagey pageno)
1367 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1369 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1371 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1375 else (100. *. (float state.y /. float maxy)) in
1378 Printf.sprintf
"Page %d of %d %.2f%%"
1379 (fn+1) state.pagecount
percent
1382 "Pages %d-%d of %d %.2f%%"
1383 (fn+1) (ln
+1) state.pagecount
percent
1388 begin match state.layout with
1391 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1392 Glut.postRedisplay
();
1396 enterbookmarkmode ()
1400 match state.layout with
1403 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1407 enttext (Some
('~'
, "", None
, textentry, ondone))
1411 showtext ' '
"Quick bookmark added";
1414 begin match state.layout with
1416 let rect = getpdimrect
l.pagedimno
in
1420 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1421 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1423 (truncate
(rect.(1) -. rect.(0)),
1424 truncate
(rect.(3) -. rect.(0)))
1428 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1430 Glut.postRedisplay
();
1436 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1441 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1442 Glut.postRedisplay
()
1444 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1445 | 'j'
-> gotoy (clamp conf.scrollincr
)
1447 | '
r'
-> opendoc state.path
state.password
1450 vlog "huh? %d %c" key
(Char.chr key
);
1453 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1454 let len = String.length
text in
1457 state.textentry <- None
;
1458 Glut.postRedisplay
();
1461 let s = String.sub
text 0 (len - 1) in
1462 enttext (Some
(c, s, opthist
, onkey
, ondone))
1465 | Some
(c, text, onhist, onkey
, ondone) ->
1466 begin match Char.unsafe_chr key
with
1469 state.textentry <- None
;
1470 Glut.postRedisplay
()
1473 begin match onhist with
1475 | Some
(_
, onhistcancel
) -> onhistcancel
()
1477 state.textentry <- None
;
1478 Glut.postRedisplay
()
1481 begin match onkey
text key
with
1483 state.textentry <- None
;
1485 Glut.postRedisplay
()
1488 enttext (Some
(c, text, onhist, onkey
, ondone));
1491 state.textentry <- None
;
1492 Glut.postRedisplay
()
1495 state.textentry <- Some te
;
1496 Glut.postRedisplay
()
1501 let narrow outlines pattern
=
1502 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1506 let rec fold accu n =
1510 let (s, _
, _
, _
) as o
= outlines.(n) in
1512 if (try ignore
(Str.search_forward re
s 0); true
1513 with Not_found
-> false)
1519 let matched = fold [] (Array.length
outlines - 1) in
1520 if matched = [] then None
else Some
(Array.of_list
matched)
1523 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1524 let search active pattern
incr =
1527 if n = Array.length
outlines || n = -1
1530 let (s, _
, _
, _
) = outlines.(n) in
1532 (try ignore
(Str.search_forward re
s 0); true
1533 with Not_found
-> false)
1535 else loop (n + incr)
1540 let re = Str.regexp_case_fold pattern
in
1546 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1549 if String.length qsearch
= 0
1552 state.outline <- None
;
1553 Glut.postRedisplay
();
1557 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1558 Glut.postRedisplay
();
1562 let incr = if key
= 18 then -1 else 1 in
1564 match search (active + incr) qsearch
incr with
1566 state.text <- qsearch ^
" [not found]";
1569 state.text <- qsearch
;
1570 active, firstof active
1572 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1573 Glut.postRedisplay
();
1576 let len = String.length qsearch
in
1583 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1586 let qsearch = String.sub
qsearch 0 (len - 1) in
1588 match search active qsearch ~
-1 with
1590 state.text <- qsearch ^
" [not found]";
1593 state.text <- qsearch;
1594 active, firstof active
1596 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1598 Glut.postRedisplay
()
1601 if active < Array.length
outlines
1603 let (_
, _
, n, t
) = outlines.(active) in
1607 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1608 state.outline <- None
;
1609 Glut.postRedisplay
();
1611 | _
when key
>= 32 && key
< 127 ->
1612 let pattern = addchar qsearch (Char.chr key
) in
1614 match search active pattern 1 with
1616 state.text <- pattern ^
" [not found]";
1619 state.text <- pattern;
1620 active, firstof active
1622 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1623 Glut.postRedisplay
()
1625 | 14 when not allowdel
-> (* ctrl-n *)
1626 if String.length
qsearch > 0
1628 let optoutlines = narrow outlines qsearch in
1629 begin match optoutlines with
1630 | None
-> state.text <- "can't narrow"
1632 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1633 match state.outlines with
1636 state.outlines <- Onarrow
(qsearch, outlines, a)
1637 | Onarrow
(pat
, a, b) ->
1638 state.outlines <- Onarrow
(qsearch, outlines, b)
1641 Glut.postRedisplay
()
1643 | 21 when not allowdel
-> (* ctrl-u *)
1645 match state.outlines with
1648 let a = Array.of_list
(List.rev
l) in
1649 state.outlines <- Oarray
a;
1651 | Onarrow
(pat
, a, b) ->
1652 state.outlines <- Oarray
b;
1656 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1657 Glut.postRedisplay
()
1661 Some
(allowdel
, active, firstof active, outlines, qsearch);
1662 Glut.postRedisplay
()
1664 | 127 when allowdel
->
1665 let len = Array.length
outlines - 1 in
1668 state.outline <- None
;
1669 state.bookmarks <- [];
1672 let bookmarks = Array.init
len
1674 let i = if i >= active then i + 1 else i in
1685 Glut.postRedisplay
()
1687 | _
-> log "unknown key %d" key
1690 let keyboard ~key ~x ~
y =
1695 match state.outline with
1696 | None
-> viewkeyboard ~key ~x ~
y
1697 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1700 let special ~key ~x ~
y =
1701 match state.outline with
1702 | None
when state.birdseye
<> None
->
1703 begin match key
with
1705 let pageno = max
0 (state.birdseyepageno
- 1) in
1706 state.birdseyepageno
<- pageno;
1707 if not
(pagevisible pageno)
1708 then gotopage pageno 0.0
1709 else Glut.postRedisplay
();
1712 let pageno = min
(state.pagecount
- 1) (state.birdseyepageno
+ 1) in
1713 state.birdseyepageno
<- pageno;
1714 if not
(pagevisible pageno)
1716 begin match List.rev
state.layout with
1717 | [] -> gotopage pageno 0.0
1719 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1721 else Glut.postRedisplay
();
1723 | Glut.KEY_PAGE_UP
->
1724 begin match state.layout with
1726 if l.pageno = state.birdseyepageno
1727 then gotoy (clamp (-conf.winh
))
1729 state.birdseyepageno
<- max
0 (l.pageno - 1);
1730 gotopage state.birdseyepageno
0.0
1732 | [] -> gotoy (clamp (-conf.winh
))
1734 | Glut.KEY_PAGE_DOWN
->
1735 begin match List.rev
state.layout with
1737 state.birdseyepageno
<- min
(state.pagecount
- 1) (l.pageno + 1);
1738 gotoy (clamp (l.pagedispy
+ conf.interpagespace + l.pageh
))
1739 | [] -> gotoy (clamp conf.winh
)
1743 state.birdseyepageno
<- 0;
1746 state.birdseyepageno
<- state.pagecount
- 1;
1747 if not
(pagevisible state.birdseyepageno
)
1749 gotopage state.birdseyepageno
0.0
1751 Glut.postRedisplay
()
1757 begin match state.textentry with
1761 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1762 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1763 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1764 | Glut.KEY_PAGE_UP
->
1765 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1767 match state.layout with
1769 | l :: _
-> state.y - l.pagey
1772 | Glut.KEY_PAGE_DOWN
->
1773 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1775 match List.rev
state.layout with
1777 | l :: _
-> getpagey l.pageno
1780 | Glut.KEY_HOME
-> addnav (); 0
1783 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1785 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1786 state.x
<- state.x
- 10;
1788 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1789 state.x
<- state.x
+ 10;
1794 gotoy_and_clear_text y
1796 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1799 | Glut.KEY_UP
-> action HCprev
1800 | Glut.KEY_DOWN
-> action HCnext
1801 | Glut.KEY_HOME
-> action HCfirst
1802 | Glut.KEY_END
-> action HClast
1805 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1806 Glut.postRedisplay
()
1811 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1812 let maxrows = maxoutlinerows () in
1813 let calcfirst first
active =
1816 let rows = active - first
in
1817 if rows > maxrows then active - maxrows else first
1821 let active = active + incr in
1822 let active = max
0 (min
active (Array.length
outlines - 1)) in
1823 let first = calcfirst first active in
1824 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1825 Glut.postRedisplay
()
1827 let updownlevel incr =
1828 let len = Array.length
outlines in
1829 let (_
, curlevel
, _
, _
) = outlines.(active) in
1831 if i = len then i-1 else if i = -1 then 0 else
1832 let (_
, l, _
, _
) = outlines.(i) in
1833 if l != curlevel
then i else flow (i+incr)
1835 let active = flow active in
1836 let first = calcfirst first active in
1837 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1838 Glut.postRedisplay
()
1841 | Glut.KEY_UP
-> navigate ~
-1
1842 | Glut.KEY_DOWN
-> navigate 1
1843 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1844 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1846 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1847 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1850 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1851 Glut.postRedisplay
()
1854 let active = Array.length
outlines - 1 in
1855 let first = max
0 (active - maxrows) in
1856 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1857 Glut.postRedisplay
()
1862 let drawplaceholder l =
1863 GlDraw.color
(scalecolor 1.0);
1865 (float l.pagex
, float l.pagedispy
)
1866 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1868 let x = float l.pagex
1869 and y = float (l.pagedispy
+ 13) in
1870 let font = Glut.BITMAP_8_BY_13
in
1871 GlDraw.color
(0.0, 0.0, 0.0);
1872 GlPix.raster_pos ~
x ~
y ();
1873 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1874 ("Loading " ^ string_of_int
(l.pageno + 1));
1877 let now () = Unix.gettimeofday
();;
1880 begin match getopaque l.pageno with
1881 | Some
(opaque, _
) when validopaque opaque ->
1882 if state.textentry = None
1883 then GlDraw.color
(scalecolor 1.0)
1884 else GlDraw.color
(scalecolor 0.4);
1886 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1890 vlog "draw %d %f sec" l.pageno d;
1895 if state.birdseye
<> None
&& state.birdseyepageno
= l.pageno
1897 GlDraw.polygon_mode `both `line
;
1898 GlDraw.line_width
4.0;
1899 GlDraw.color
(0.8, 0.0, 0.0);
1901 (float (l.pagex
- 1), float (l.pagedispy
- 1))
1902 (float (l.pagew
+ l.pagex
+ 1), float (l.pagedispy
+ l.pagevh + 1))
1904 GlDraw.line_width
1.0;
1905 GlDraw.polygon_mode `both `fill
;
1910 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1911 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1912 let sh = float conf.winh
/. sh in
1913 let sh = max
sh (float conf.scrollh
) in
1916 if state.y = state.maxy
1918 else float y /. float maxy
1920 let position = (float conf.winh
-. sh) *. percent in
1923 if position +. sh > float conf.winh
1924 then float conf.winh
-. sh
1930 let scrollindicator () =
1931 GlDraw.color
(0.64 , 0.64, 0.64);
1933 (float (conf.winw
- conf.scrollw
), 0.)
1934 (float conf.winw
, float conf.winh
)
1936 GlDraw.color
(0.0, 0.0, 0.0);
1938 let position, sh = scrollph state.y in
1940 (float (conf.winw
- conf.scrollw
), position)
1941 (float conf.winw
, position +. sh)
1945 let showsel margin
=
1946 match state.mstate
with
1947 | Mnone
| Mscroll _
| Mpan _
->
1950 | Msel
((x0
, y0
), (x1
, y1
)) ->
1951 let rec loop = function
1953 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1954 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1956 match getopaque l.pageno with
1957 | Some
(opaque, _
) when validopaque opaque ->
1958 let oy = -l.pagey + l.pagedispy
in
1960 (x0
- margin
- state.x, y0
,
1961 x1
- margin
- state.x, y1
) oy;
1971 let panx = float state.x in
1973 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1974 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1976 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1978 if l.pageno = pageno
1980 let d = float (l.pagedispy
- l.pagey) in
1981 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1982 GlDraw.begins `quads
;
1984 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1985 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1986 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1987 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1997 let showoutline = function
1999 | Some
(allowdel
, active, first, outlines, qsearch) ->
2001 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2002 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
2003 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2006 GlDraw.color
(1., 1., 1.);
2007 let font = Glut.BITMAP_9_BY_15
in
2008 let draw_string x y s =
2009 GlPix.raster_pos ~
x ~
y ();
2010 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2013 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2016 let (s, l, _
, _
) = outlines.(row
) in
2017 let y = (row
- first) * 16 in
2022 GlDraw.polygon_mode `both `line
;
2023 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2024 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
2025 GlDraw.rect (0., float (y + 1))
2026 (float (conf.winw
- 1), float (y + 18));
2027 GlDraw.polygon_mode `both `fill
;
2029 GlDraw.color
(1., 1., 1.);
2031 draw_string (float x) (float (y + 16)) s;
2039 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2040 GlDraw.viewport
margin 0 state.w conf.winh
;
2042 if state.birdseye
<> None
2044 GlClear.color
(0.5, 0.5, 0.55)
2046 GlClear.color
(scalecolor 0.5)
2048 GlClear.clear
[`color
];
2051 let x = float state.x in
2052 GlMat.translate ~
x ();
2056 Gl.enable `scissor_test
;
2057 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2059 List.iter
drawpage state.layout;
2062 Gl.disable `scissor_test
2066 let x = -.float state.x in
2067 GlMat.translate ~
x ();
2071 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2074 showoutline state.outline;
2076 Glut.swapBuffers
();
2080 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2081 let x = x - margin - state.x in
2082 let rec f = function
2084 begin match getopaque l.pageno with
2085 | Some
(opaque, _
) when validopaque opaque ->
2086 let y = y - l.pagedispy
in
2089 let y = l.pagey + y in
2090 let x = x - l.pagex
in
2091 match whatsunder
opaque x y with
2104 let mouse ~button ~bstate ~
x ~
y =
2106 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2114 let incr = incr * 2 in
2115 let y = clamp incr in
2116 gotoy_and_clear_text y
2118 | Glut.LEFT_BUTTON
when state.outline = None
2119 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2120 if bstate
= Glut.DOWN
2122 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2123 state.mstate
<- Mpan
(x, y)
2126 state.mstate
<- Mnone
2129 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2130 if bstate
= Glut.DOWN
2132 let position, sh = scrollph state.y in
2133 if y > truncate
position && y < truncate
(position +. sh)
2135 state.mstate
<- Mscroll
2137 let percent = float y /. float conf.winh
in
2138 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2140 state.mstate
<- Mscroll
2142 state.mstate
<- Mnone
2144 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2145 begin match state.birdseye
with
2147 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2148 let rec loop = function
2151 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2152 && x > margin && x < margin + l.pagew
2154 let y = getpagey l.pageno in
2157 reshape conf.winw
conf.winh
;
2162 | None
-> () (* impossible *)
2165 | Glut.LEFT_BUTTON
when state.outline = None
->
2166 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2167 begin match dest with
2168 | Ulinkgoto
(pageno, top
) ->
2171 gotopage1 pageno top
2176 | Unone
when bstate
= Glut.DOWN
->
2177 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2178 state.mstate
<- Mpan
(x, y);
2180 | Unone
| Utext _
->
2181 if bstate
= Glut.DOWN
2183 if conf.angle
mod 360 = 0
2185 state.mstate
<- Msel
((x, y), (x, y));
2186 Glut.postRedisplay
()
2190 match state.mstate
with
2194 state.mstate
<- Mnone
2197 Glut.setCursor
Glut.CURSOR_INHERIT
;
2198 state.mstate
<- Mnone
2200 | Msel
((x0
, y0
), (x1
, y1
)) ->
2202 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2203 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2205 match getopaque l.pageno with
2206 | Some
(opaque, _
) when validopaque opaque ->
2210 List.iter
f state.layout;
2211 copysel
""; (* ugly *)
2212 Glut.setCursor
Glut.CURSOR_INHERIT
;
2213 state.mstate
<- Mnone
;
2220 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2223 if state.outline = None
2225 match state.mstate
with
2231 state.mstate
<- Mpan
(x, y);
2232 if conf.zoom > 1.0 then state.x <- state.x + dx;
2234 gotoy_and_clear_text y
2237 state.mstate
<- Msel
(a, (x, y));
2238 Glut.postRedisplay
()
2241 let y = min
conf.winh
(max
0 y) in
2242 let percent = float y /. float conf.winh
in
2243 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2244 gotoy_and_clear_text y
2248 if state.outline = None
&& state.birdseye
= None
2250 match state.mstate
with
2252 begin match getunder x y with
2253 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2255 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2256 Glut.setCursor
Glut.CURSOR_INFO
2257 | Ulinkgoto
(page
, y) ->
2258 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2259 Glut.setCursor
Glut.CURSOR_INFO
2261 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2262 Glut.setCursor
Glut.CURSOR_TEXT
2265 | Mpan _
| Msel _
| Mscroll
->
2275 match Sys.os_type
with
2276 | "Win32" -> Sys.getenv
"HOMEPATH"
2277 | _
-> Sys.getenv
"HOME"
2280 ("Can not determine home directory location: " ^
2281 Printexc.to_string exn
);
2285 let config_of c attrs
=
2289 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2290 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2291 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2292 | "preload" -> { c with preload = bool_of_string v
}
2293 | "page-bias" -> { c with pagebias
= int_of_string v
}
2294 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2295 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2296 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2297 | "throttle" -> { c with showall
= bool_of_string v
}
2298 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2299 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2300 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2302 let zoom = float_of_string v
/. 100. in
2303 let zoom = max
0.01 (min
2.2 zoom) in
2304 { c with zoom = zoom }
2305 | "presentation" -> { c with presentation = bool_of_string v
}
2306 | "rotation-angle" -> { c with angle
= int_of_string v
}
2307 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2308 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2309 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2310 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2311 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2312 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2313 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2316 prerr_endline
("Error processing attribute (`" ^
2317 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2320 let rec fold c = function
2323 let c = apply c k v
in
2329 let bookmark_of attrs
=
2330 let rec fold title page
rely = function
2331 | ("title", v
) :: rest
-> fold v page
rely rest
2332 | ("page", v
) :: rest
-> fold title v
rely rest
2333 | ("rely", v
) :: rest
-> fold title page v rest
2334 | _
:: rest
-> fold title page
rely rest
2335 | [] -> title, page
, rely
2337 fold "invalid" "0" "0" attrs
2340 let setconf dst src
=
2341 dst
.scrollw
<- src
.scrollw
;
2342 dst
.scrollh
<- src
.scrollh
;
2343 dst
.icase
<- src
.icase
;
2344 dst
.preload <- src
.preload;
2345 dst
.pagebias
<- src
.pagebias
;
2346 dst
.verbose
<- src
.verbose
;
2347 dst
.scrollincr
<- src
.scrollincr
;
2348 dst
.maxhfit
<- src
.maxhfit
;
2349 dst
.crophack
<- src
.crophack
;
2350 dst
.autoscroll
<- src
.autoscroll
;
2351 dst
.showall
<- src
.showall
;
2352 dst
.hlinks
<- src
.hlinks
;
2353 dst
.underinfo
<- src
.underinfo
;
2354 dst
.interpagespace <- src
.interpagespace;
2355 dst
.zoom <- src
.zoom;
2356 dst
.presentation <- src
.presentation;
2357 dst
.angle
<- src
.angle
;
2358 dst
.winw
<- src
.winw
;
2359 dst
.winh
<- src
.winh
;
2360 dst
.savebmarks
<- src
.savebmarks
;
2361 dst
.memlimit
<- src
.memlimit
;
2362 dst
.proportional
<- src
.proportional
;
2363 dst
.texcount
<- src
.texcount
;
2364 dst
.sliceheight
<- src
.sliceheight
;
2368 let l = String.length
s in
2369 let b = Buffer.create
l in
2375 let h = Hashtbl.create
10 in
2376 let dc = { defconf with angle
= defconf.angle
} in
2377 let rec toplevel v t spos epos
=
2379 | Vdata
| Vcdata
| Vend
-> v
2380 | Vopen
("llppconfig", attrs
, closed
) ->
2383 else { v
with f = llppconfig
}
2385 error
"unexpected subelement at top level" s spos
2386 | Vclose tag
-> error
"unexpected close at top level" s spos
2388 and llppconfig v t spos epos
=
2390 | Vdata
| Vcdata
| Vend
-> v
2391 | Vopen
("defaults", attrs
, closed
) ->
2392 let c = config_of dc attrs
in
2396 else { v
with f = skip
"defaults" (fun () -> v
) }
2398 | Vopen
("doc", attrs
, closed
) ->
2401 List.assoc
"path" attrs
2402 with Not_found
-> error
"doc is missing path attribute" s spos
2404 let path = unent pathent in
2405 let c = config_of dc attrs
in
2408 float_of_string
(List.assoc
"rely" attrs
)
2412 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2417 int_of_string
(List.assoc
"pan" attrs
)
2421 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2425 then (Hashtbl.add
h path (c, [], x, y); v
)
2426 else { v
with f = doc
path x y c [] }
2428 | Vopen
(tag
, _
, closed
) ->
2429 error
"unexpected subelement in llppconfig" s spos
2431 | Vclose
"llppconfig" -> { v
with f = toplevel }
2432 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2434 and doc
path x y c bookmarks v t spos epos
=
2436 | Vdata
| Vcdata
-> v
2437 | Vend
-> error
"unexpected end of input in doc" s spos
2438 | Vopen
("bookmarks", attrs
, closed
) ->
2439 { v
with f = pbookmarks
path x y c bookmarks }
2441 | Vopen
(tag
, _
, _
) ->
2442 error
"unexpected subelement in doc" s spos
2445 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2446 { v
with f = llppconfig
}
2448 | Vclose tag
-> error
"unexpected close in doc" s spos
2450 and pbookmarks
path x y c bookmarks v t spos epos
=
2452 | Vdata
| Vcdata
-> v
2453 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2454 | Vopen
("item", attrs
, closed
) ->
2455 let titleent, spage
, srely
= bookmark_of attrs
in
2460 dolog "Failed to convert page %S to integer: %s"
2461 spage
(Printexc.to_string exn
);
2466 float_of_string srely
2468 dolog "Failed to convert rely %S to real: %s"
2469 srely
(Printexc.to_string exn
);
2472 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2474 then { v
with f = pbookmarks
path x y c bookmarks }
2477 { v
with f = skip
"item" f }
2480 error
"unexpected subelement in bookmarks" s spos
2482 | Vclose
"bookmarks" ->
2483 { v
with f = doc
path x y c bookmarks }
2485 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2487 and skip tag
f v t spos epos
=
2489 | Vdata
| Vcdata
-> v
2491 error
("unexpected end of input in skipped " ^ tag
) s spos
2492 | Vopen
(tag'
, _
, closed
) ->
2496 let f'
() = { v
with f = skip tag
f } in
2497 { v
with f = skip tag'
f'
}
2501 else error
("unexpected close in skipped " ^ tag
) s spos
2504 parse
{ f = toplevel; accu = () } s;
2510 let len = in_channel_length ic
in
2511 let s = String.create
len in
2512 really_input ic
s 0 len;
2515 | Parse_error
(msg
, s, pos
) ->
2516 let subs = subs s pos
in
2517 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2518 failwith
("parse error: " ^
s)
2521 failwith
("config load error: " ^
Printexc.to_string exn
)
2527 let dir = Filename.concat
home ".config" in
2528 if Sys.is_directory
dir then dir else home
2531 Filename.concat
dir "llpp.conf"
2535 if Sys.file_exists
path
2538 (try Some
(open_in_bin
path)
2541 ("Error opening configuation file `" ^
path ^
"': " ^
2542 Printexc.to_string exn
);
2551 ("Error loading configuation from `" ^
path ^
"': " ^
2552 Printexc.to_string exn
);
2558 f (Hashtbl.create
0, defconf)
2563 let pc, pb
, px
, py =
2565 Hashtbl.find
h state.path
2566 with Not_found
-> dc, [], 0, 0.0
2570 state.bookmarks <- pb
;
2572 cbput state.hists
.nav
py;
2577 let add_attrs bb always
dc c =
2580 then Printf.bprintf bb
"\n %s='%b'" s a
2583 then Printf.bprintf bb
"\n %s='%d'" s a
2586 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2590 then dc.winw
, dc.winh
2592 match state.fullscreen
with
2594 | None
-> c.winw
, c.winh
2596 let zoom, presentation, interpagespace =
2598 then dc.zoom, dc.presentation, dc.interpagespace
2600 match state.birdseye
with
2601 | Some
(zoom, _
, presentation, interpagespace) ->
2602 (zoom, presentation, interpagespace)
2603 | None
-> c.zoom, c.presentation, c.interpagespace
2605 oi
"width" w dc.winw
;
2606 oi
"height" h dc.winh
;
2607 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2608 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2609 ob "case-insensitive-search" c.icase
dc.icase
;
2610 ob "preload" c.preload dc.preload;
2611 oi
"page-bias" c.pagebias
dc.pagebias
;
2612 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2613 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2614 ob "crop-hack" c.crophack
dc.crophack
;
2615 ob "throttle" c.showall
dc.showall
;
2616 ob "highlight-links" c.hlinks
dc.hlinks
;
2617 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2618 oi
"vertical-margin" interpagespace dc.interpagespace;
2619 oz
"zoom" zoom dc.zoom;
2620 ob "presentation" presentation dc.presentation;
2621 oi
"rotation-angle" c.angle
dc.angle
;
2622 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2623 ob "proportional-display" c.proportional
dc.proportional
;
2624 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2625 oi
"texcount" c.texcount
dc.texcount
;
2626 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2630 let bb = Buffer.create
32768 in
2632 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2633 add_attrs bb true dc dc;
2634 Buffer.add_string
bb "/>\n";
2636 let adddoc path x y c bookmarks =
2637 if bookmarks == [] && c = dc && y = 0.0
2640 Printf.bprintf
bb "<doc path='%s'"
2641 (enent
path 0 (String.length
path));
2644 then Printf.bprintf
bb " rely='%f'" y;
2647 then Printf.bprintf
bb " pan='%d'" x;
2649 add_attrs bb false dc c;
2651 begin match bookmarks with
2652 | [] -> Buffer.add_string
bb "/>\n"
2654 Buffer.add_string
bb ">\n<bookmarks>\n";
2655 List.iter
(fun (title, _level
, page, rely) ->
2657 "<item title='%s' page='%d' rely='%f'/>\n"
2658 (enent
title 0 (String.length
title))
2662 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2668 match state.birdseye
with
2669 | Some
(_
, x, _
, _
) -> x
2672 adddoc state.path x (yratio state.y) conf
2673 (if conf.savebmarks
then state.bookmarks else []);
2675 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2676 if path <> state.path
2678 adddoc path x y c bookmarks
2680 Buffer.add_string
bb "</llppconfig>";
2683 if Buffer.length
bb > 0
2686 let tmp = path ^
".tmp" in
2687 let oc = open_out_bin
tmp in
2688 Buffer.output_buffer
oc bb;
2690 Sys.rename
tmp path;
2693 ("error while saving configuration: " ^
Printexc.to_string exn
)
2699 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2700 (fun s -> state.path <- s)
2701 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2704 if String.length
state.path = 0
2705 then (prerr_endline
"filename missing"; exit
1)
2707 if Filename.is_relative
state.path
2708 then Filename.concat
(Sys.getcwd
()) state.path
2716 let _ = Glut.init
Sys.argv
in
2717 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2718 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2719 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2722 if Sys.os_type
= "Unix"
2724 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2726 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2727 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2728 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2729 Unix.bind
sock addr;
2731 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2732 Unix.connect
csock addr;
2733 let ssock, _ = Unix.accept
sock in
2736 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2737 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2741 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2745 let () = Glut.displayFunc
display in
2746 let () = Glut.reshapeFunc
reshape in
2747 let () = Glut.keyboardFunc
keyboard in
2748 let () = Glut.specialFunc
special in
2749 let () = Glut.idleFunc
(Some
idle) in
2750 let () = Glut.mouseFunc
mouse in
2751 let () = Glut.motionFunc
motion in
2752 let () = Glut.passiveMotionFunc
pmotion in
2754 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2755 state.csock <- csock;
2756 state.ssock <- ssock;
2757 state.text <- "Opening " ^
path;
2758 writeopen state.path state.password
;
2762 let rec handlelablglutbug () =
2765 with Glut.BadEnum
"key in special_of_int" ->
2766 showtext '
!'
" LablGlut bug: special key not recognized";
2767 handlelablglutbug ()
2769 handlelablglutbug ();