4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 type params
= angle
* proportional
* texcount
* sliceheight
20 and proportional
= bool
21 and interpagespace
= int
27 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
28 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext
: string -> (int * int * int * int) -> int -> unit =
31 external copysel
: string -> unit = "ml_copysel";;
32 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
33 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
34 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
38 | Msel
of (mpos
* mpos
)
52 type textentry
= (char
* string * onhist
* onkey
* ondone
)
53 and onkey
= string -> int -> te
54 and ondone
= string -> unit
55 and histcancel
= unit -> unit
56 and onhist
= ((histcmd
-> string) * histcancel
) option
57 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
62 | TEswitch
of textentry
66 { store
= Array.create n v
73 let cbcap b
= Array.length b
.store
;;
78 b
.wc
<- (b
.wc
+ 1) mod cap;
80 b
.len
<- min
(b
.len
+ 1) cap;
83 let cbempty b
= b
.len
= 0;;
85 let cbgetg b circular dir
=
89 let rc = b
.rc + dir
in
101 else max
0 (min
rc (b
.len
-1))
107 let cbget b
= cbgetg b
false;;
108 let cbgetc b
= cbgetg b
true;;
111 let rc = b
.wc
- b
.len
in
112 let rc = if rc < 0 then cbcap b
+ rc else rc in
116 let cbdecr b
= b
.len
<- b
.len
- 1;;
131 { mutable scrollw
: int
132 ; mutable scrollh
: int
133 ; mutable icase
: bool
134 ; mutable preload
: bool
135 ; mutable pagebias
: int
136 ; mutable verbose
: bool
137 ; mutable scrollincr
: int
138 ; mutable maxhfit
: bool
139 ; mutable crophack
: bool
140 ; mutable autoscroll
: bool
141 ; mutable showall
: bool
142 ; mutable hlinks
: bool
143 ; mutable underinfo
: bool
144 ; mutable interpagespace
: interpagespace
145 ; mutable zoom
: float
146 ; mutable presentation
: bool
147 ; mutable angle
: angle
150 ; mutable savebmarks
: bool
151 ; mutable proportional
: proportional
152 ; mutable memlimit
: int
153 ; mutable texcount
: texcount
154 ; mutable sliceheight
: sliceheight
158 type outline
= string * int * int * float;;
160 | Oarray
of outline array
161 | Olist
of outline list
162 | Onarrow
of string * outline array
* outline array
165 type rect
= (float * float * float * float * float * float * float * float);;
167 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
170 { mutable csock
: Unix.file_descr
171 ; mutable ssock
: Unix.file_descr
176 ; mutable layout
: layout list
177 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
178 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
179 ; mutable pagecount
: int
180 ; pagecache
: string circbuf
181 ; mutable rendering
: bool
182 ; mutable mstate
: mstate
183 ; mutable searchpattern
: string
184 ; mutable rects
: (pageno
* recttype
* rect
) list
185 ; mutable rects1
: (pageno
* recttype
* rect
) list
186 ; mutable text
: string
187 ; mutable fullscreen
: (width
* height
) option
188 ; mutable birdseye
: (conf
* leftx
* pageno
* pageno
) option
189 ; mutable textentry
: textentry
option
190 ; mutable outlines
: outlines
191 ; mutable outline
: (bool * int * int * outline array
* string) option
192 ; mutable bookmarks
: outline list
193 ; mutable path
: string
194 ; mutable password
: string
195 ; mutable invalidated
: int
196 ; mutable colorscale
: float
197 ; mutable memused
: int
199 ; mutable throttle
: layout list
option
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
};;
247 ; pagemap
= Hashtbl.create
10
248 ; pagecache
= cbnew 100 ""
260 ; outlines
= Olist
[]
267 { 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
, state.gen
))
502 with Not_found
-> None
505 let cache pageno opaque
=
506 Hashtbl.replace
state.pagemap
507 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) 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 layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
549 let memleft = conf.memlimit
- state.memused
in
552 let opaque = cbpeek state.pagecache
in
553 match findpageforopaque opaque with
554 | Some
((n, _
, _
, _
, _
), size
) ->
555 memleft + size
>= 0 && not
(pagevisible state.layout n)
562 let presentation = conf.presentation in
563 let interpagespace = conf.interpagespace in
564 let maxy = state.maxy in
565 conf.presentation <- false;
566 conf.interpagespace <- 0;
567 state.maxy <- calcheight ();
569 match state.layout with
571 | l
:: _
-> getpagey l
.pageno
573 let y = if y < conf.winh
then 0 else y - conf.winh
in
574 let pages = layout y (conf.winh
*3) in
575 List.iter
render pages;
576 conf.presentation <- presentation;
577 conf.interpagespace <- interpagespace;
583 let y = min
state.maxy y in
584 let pages = layout y conf.winh
in
585 let ready = loadlayout pages in
591 state.layout <- pages;
592 state.throttle
<- None
;
593 Glut.postRedisplay
();
596 state.throttle
<- Some
pages;
601 state.layout <- pages;
602 state.throttle
<- None
;
603 Glut.postRedisplay
();
605 begin match state.birdseye
with
606 | Some
(conf, leftx
, pageno
, hooverpageno
) ->
607 if not
(pagevisible pages pageno
)
609 match state.layout with
612 state.birdseye
<- Some
(conf, leftx
, l
.pageno
, hooverpageno
)
619 let gotoy_and_clear_text y =
621 if not
conf.verbose
then state.text
<- "";
625 cbput state.hists
.nav
(yratio state.y);
629 let y = cbgetc state.hists
.nav ~
-1 in
630 truncate
(y *. float state.maxy)
634 let y, h
= getpageyh n in
636 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
639 let gotopage1 n top
=
640 let y = getpagey n in
642 gotoy_and_clear_text (y + top
);
650 state.invalidated
<- state.invalidated
+ 1;
654 let c = c *. state.colorscale
in
659 let maxy = calcheight () in
661 match state.birdseye
with
663 begin match state.layout with
665 let rely = yratio state.y in
666 truncate
(float maxy *. rely)
668 | l
:: _
-> getpagey l
.pageno
671 | Some
(conf, leftx
, pageno
, hooverpageno
) ->
672 let rely = yratio state.y in
673 truncate
(float maxy *. rely)
680 GlMat.mode `projection
;
681 GlMat.load_identity
();
682 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
683 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
684 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
688 GlMat.mode `projection
;
689 GlMat.load_identity
();
690 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
691 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
692 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
697 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
701 GlMat.mode `modelview
;
702 GlMat.load_identity
();
703 GlClear.color
(scalecolor 1.0);
704 GlClear.clear
[`color
];
707 wcmd "geometry" [`i
w; `i h
];
711 GlDraw.color
(0.0, 0.0, 0.0);
713 (0.0, float (conf.winh
- 18))
714 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
716 let font = Glut.BITMAP_8_BY_13
in
717 GlDraw.color
(1.0, 1.0, 1.0);
718 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
719 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
720 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
724 let len = String.length
state.text
in
725 match state.textentry
with
727 if len > 0 then showtext ' '
state.text
729 | Some
(c, text
, _
, _
, _
) ->
733 text ^
" [" ^
state.text ^
"]"
743 state.text
<- Printf.sprintf
"%c%s" c s;
744 Glut.postRedisplay
();
758 state.rects
<- state.rects1
;
759 Glut.postRedisplay
()
762 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
763 state.pagecount
<- n;
764 state.invalidated
<- state.invalidated
- 1;
765 if state.invalidated
= 0
769 let s = Scanf.sscanf
cmd "t %n"
770 (fun n -> String.sub
cmd n (String.length
cmd - n))
772 Glut.setWindowTitle
s
775 let s = Scanf.sscanf
cmd "T %n"
776 (fun n -> String.sub
cmd n (String.length
cmd - n))
778 if state.textentry
= None
785 Glut.postRedisplay
();
791 let s = Scanf.sscanf
cmd "V %n"
792 (fun n -> String.sub
cmd n (String.length
cmd - n))
798 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
799 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
800 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
801 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
803 let y = (getpagey pageno) + truncate y0
in
806 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
809 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
810 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
811 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
812 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
815 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
818 let n, w, h
, r
, l
, s, p
=
819 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
820 (fun n w h r l
s p
->
821 (n-1, w, h
, r
, l
!= 0, s, p
))
824 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
825 state.memused
<- state.memused
+ s;
828 match state.throttle
with
829 | None
-> state.layout
830 | Some
layout -> layout
834 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
837 let evictedopaque = cbpeek state.pagecache
in
838 match findpageforopaque evictedopaque with
839 | None
-> failwith
"bug in gc"
840 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
841 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
843 wcmd "free" [`
s evictedopaque];
844 state.memused
<- state.memused
- evictedsize
;
845 Hashtbl.remove
state.pagemap k
;
846 cbdecr state.pagecache
;
853 cbput state.pagecache p
;
854 state.rendering
<- false;
856 begin match state.throttle
with
858 if pagevisible state.layout n
861 let allvisible = loadlayout state.layout in
862 if allvisible then preload ();
869 let y = getpagey l
.pageno + l
.pagey in
874 let (n, w, h
, x
) as pdim
=
875 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
877 state.pdims
<- pdim
:: state.pdims
880 let (l
, n, t
, h
, pos
) =
881 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
883 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
885 let l = String.length
s in
886 let b = Buffer.create
(String.length
s) in
893 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
896 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
905 let outline = (s, l, n, float t
/. float h
) in
907 match state.outlines with
908 | Olist
outlines -> Olist
(outline :: outlines)
909 | Oarray _
-> Olist
[outline]
910 | Onarrow _
-> Olist
[outline]
912 state.outlines <- outlines
915 log "unknown cmd `%S'" cmd
918 let now = Unix.gettimeofday
;;
922 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
927 let y = state.y + conf.scrollincr
in
928 let y = if y >= state.maxy then 0 else y in
934 let cmd = readcmd state.csock
in
943 let action = function
944 | HCprev
-> cbget cb ~
-1
945 | HCnext
-> cbget cb
1
946 | HCfirst
-> cbget cb ~
-(cb
.rc)
947 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
948 and cancel
() = cb
.rc <- rc
952 let search pattern forward
=
953 if String.length pattern
> 0
956 match state.layout with
959 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
962 let b = makecmd "search"
963 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
965 Buffer.add_char
b '
,'
;
966 Buffer.add_string
b pattern
;
967 Buffer.add_char
b '
\000'
;
970 writecmd state.csock
cmd;
973 let intentry text key
=
974 let c = Char.unsafe_chr key
in
977 let s = "x" in s.[0] <- c;
978 let text = text ^
s in
982 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
987 let b = Buffer.create
(String.length
s + 1) in
988 Buffer.add_string
b s;
993 let textentry text key
=
994 let c = Char.unsafe_chr key
in
996 | _
when key
>= 32 && key
< 127 ->
997 let text = addchar text c in
1001 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1005 let reinit angle proportional
=
1006 conf.angle
<- angle
;
1007 conf.proportional
<- proportional
;
1009 wcmd "reinit" [`i angle
; `
b proportional
];
1012 let optentry text key
=
1013 let btos b = if b then "on" else "off" in
1014 let c = Char.unsafe_chr key
in
1018 try conf.scrollincr
<- int_of_string
s with exc
->
1019 state.text <- Printf.sprintf
"bad integer `%s': %s"
1020 s (Printexc.to_string exc
)
1022 TEswitch
('#'
, "", None
, intentry, ondone)
1027 Some
(int_of_string
s)
1029 state.text <- Printf.sprintf
"bad integer `%s': %s"
1030 s (Printexc.to_string exc
);
1033 | Some angle
-> reinit angle
conf.proportional
1036 TEswitch
('^'
, "", None
, intentry, ondone)
1039 conf.icase
<- not
conf.icase
;
1040 TEdone
("case insensitive search " ^
(btos conf.icase
))
1043 conf.preload <- not
conf.preload;
1045 TEdone
("preload " ^
(btos conf.preload))
1048 conf.verbose
<- not
conf.verbose
;
1049 TEdone
("verbose " ^
(btos conf.verbose
))
1052 conf.maxhfit
<- not
conf.maxhfit
;
1053 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1054 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1057 conf.crophack
<- not
conf.crophack
;
1058 TEdone
("crophack " ^
btos conf.crophack
)
1061 conf.showall
<- not
conf.showall
;
1062 TEdone
("showall " ^
btos conf.showall
)
1065 conf.underinfo
<- not
conf.underinfo
;
1066 TEdone
("underinfo " ^
btos conf.underinfo
)
1069 conf.savebmarks
<- not
conf.savebmarks
;
1070 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1076 match state.layout with
1081 conf.interpagespace <- int_of_string
s;
1082 state.maxy <- calcheight ();
1083 let y = getpagey pageno in
1086 state.text <- Printf.sprintf
"bad integer `%s': %s"
1087 s (Printexc.to_string exc
)
1089 TEswitch
('
%'
, "", None
, intentry, ondone)
1092 reinit conf.angle
(not
conf.proportional
);
1093 TEdone
("proprortional display " ^
btos conf.proportional
)
1096 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1100 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1102 let enterselector allowdel
outlines errmsg msg
=
1103 if Array.length
outlines = 0
1105 showtext ' ' errmsg
;
1109 Glut.setCursor
Glut.CURSOR_INHERIT
;
1111 match state.layout with
1113 | {pageno=pageno} :: rest
-> pageno
1117 if n = Array.length
outlines
1120 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1121 if outlinepageno
>= pageno then n else loop (n+1)
1126 Some
(allowdel
, active,
1127 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1128 Glut.postRedisplay
();
1132 let enteroutlinemode () =
1134 match state.outlines with
1137 let a = Array.of_list
(List.rev
l) in
1138 state.outlines <- Oarray
a;
1140 | Onarrow
(pat
, a, b) ->
1141 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1143 enterselector false outlines "Document has no outline" msg
;
1146 let enterbookmarkmode () =
1147 let bookmarks = Array.of_list
state.bookmarks in
1148 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1151 let quickbookmark ?title
() =
1152 match state.layout with
1158 let sec = Unix.gettimeofday
() in
1159 let tm = Unix.localtime
sec in
1160 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1164 (tm.Unix.tm_year
+ 1900)
1167 | Some
title -> title
1170 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1174 state.fullscreen
<- None
;
1175 Glut.reshapeWindow
w h
;
1178 let writeopen path password
=
1179 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1182 let opendoc path password
=
1185 state.password
<- password
;
1186 state.gen
<- state.gen
+ 1;
1188 writeopen path password
;
1189 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1190 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1194 let zoom = 76.0 /. float state.w in
1195 let birdseyepageno =
1196 match state.layout with
1198 | l :: _
-> l.pageno
1201 Some
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1);
1203 conf.presentation <- false;
1204 conf.interpagespace <- 10;
1205 conf.hlinks
<- false;
1207 state.mstate
<- Mnone
;
1208 conf.showall
<- false;
1209 Glut.setCursor
Glut.CURSOR_INHERIT
;
1212 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1219 let birdseyeoff (c, leftx
, pageno, _
) =
1220 state.birdseye
<- None
;
1221 conf.zoom <- c.zoom;
1222 conf.presentation <- c.presentation;
1223 conf.interpagespace <- c.interpagespace;
1224 conf.showall
<- c.showall
;
1225 conf.hlinks
<- c.hlinks
;
1227 state.maxy <- calcheight ();
1228 state.y <- getpagey pageno;
1231 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1236 let togglebirdseye () =
1237 match state.birdseye
with
1238 | None
-> birdseyeon ()
1239 | Some vals
-> birdseyeoff vals
1242 let viewkeyboard ~key ~x ~
y =
1244 state.textentry <- te
;
1247 Glut.postRedisplay
()
1249 match state.textentry with
1251 let c = Char.chr key
in
1257 let y = getnav () in
1258 gotoy_and_clear_text y
1261 begin match state.birdseye
with
1265 reshape conf.winw
conf.winh
;
1274 Glut.postRedisplay
()
1277 let ondone isforw
s =
1278 cbput state.hists
.pat
s;
1279 state.searchpattern
<- s;
1282 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1283 textentry, ondone (c ='
/'
)))
1285 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1286 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1287 conf.zoom <- min
2.2 (conf.zoom +. incr);
1288 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1289 reshape conf.winw
conf.winh
1294 try int_of_string
s with exc
->
1295 state.text <- Printf.sprintf
"bad integer `%s': %s"
1296 s (Printexc.to_string exc
);
1302 state.text <- "page bias is now " ^ string_of_int
n;
1305 enttext (Some
('
+'
, "", None
, intentry, ondone))
1307 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1308 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1309 conf.zoom <- max
0.01 (conf.zoom -. decr);
1310 if conf.zoom <= 1.0 then state.x
<- 0;
1311 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1312 reshape conf.winw
conf.winh
;
1318 enttext (Some
('
-'
, "", None
, optentry, ondone))
1320 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1323 state.text <- "zoom is 100%";
1324 reshape conf.winw
conf.winh
1326 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1327 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1332 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1333 reshape conf.winw
conf.winh
;
1336 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1338 reshape conf.winw
conf.winh
;
1343 try int_of_string
s with exc
->
1344 state.text <- Printf.sprintf
"bad integer `%s': %s"
1345 s (Printexc.to_string exc
);
1351 cbput state.hists
.pag
(string_of_int
n);
1352 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1355 let pageentry text key
=
1356 match Char.unsafe_chr key
with
1357 | 'g'
-> TEdone
text
1358 | _
-> intentry text key
1360 let text = "x" in text.[0] <- c;
1361 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1365 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1366 reshape conf.winw
conf.winh
;
1369 conf.hlinks
<- not
conf.hlinks
;
1370 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1371 Glut.postRedisplay
()
1374 conf.autoscroll
<- not
conf.autoscroll
1377 conf.presentation <- not
conf.presentation;
1378 showtext ' '
("presentation mode " ^
1379 if conf.presentation then "on" else "off");
1383 begin match state.fullscreen
with
1385 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1388 state.fullscreen
<- None
;
1393 gotoy_and_clear_text 0
1396 search state.searchpattern
true
1399 search state.searchpattern
false
1402 begin match state.layout with
1405 gotoy_and_clear_text (getpagey l.pageno)
1409 begin match List.rev
state.layout with
1412 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1413 gotoy_and_clear_text (getpagey pageno)
1417 begin match state.layout with
1420 let pageno = max
0 (l.pageno-1) in
1421 gotoy_and_clear_text (getpagey pageno)
1426 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1428 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1430 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1434 else (100. *. (float state.y /. float maxy)) in
1437 Printf.sprintf
"Page %d of %d %.2f%%"
1438 (fn+1) state.pagecount
percent
1441 "Pages %d-%d of %d %.2f%%"
1442 (fn+1) (ln
+1) state.pagecount
percent
1447 begin match state.layout with
1450 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1451 Glut.postRedisplay
();
1455 enterbookmarkmode ()
1459 match state.layout with
1462 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1466 enttext (Some
('~'
, "", None
, textentry, ondone))
1470 showtext ' '
"Quick bookmark added";
1473 begin match state.layout with
1475 let rect = getpdimrect
l.pagedimno
in
1479 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1480 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1482 (truncate
(rect.(1) -. rect.(0)),
1483 truncate
(rect.(3) -. rect.(0)))
1487 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1489 Glut.postRedisplay
();
1495 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1500 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1501 Glut.postRedisplay
()
1503 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1504 | 'j'
-> gotoy (clamp conf.scrollincr
)
1506 | '
r'
-> opendoc state.path
state.password
1509 vlog "huh? %d %c" key
(Char.chr key
);
1512 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1513 let len = String.length
text in
1516 state.textentry <- None
;
1517 Glut.postRedisplay
();
1520 let s = String.sub
text 0 (len - 1) in
1521 enttext (Some
(c, s, opthist
, onkey
, ondone))
1524 | Some
(c, text, onhist, onkey
, ondone) ->
1525 begin match Char.unsafe_chr key
with
1528 state.textentry <- None
;
1529 Glut.postRedisplay
()
1532 begin match onhist with
1534 | Some
(_
, onhistcancel
) -> onhistcancel
()
1536 state.textentry <- None
;
1537 Glut.postRedisplay
()
1540 begin match onkey
text key
with
1542 state.textentry <- None
;
1544 Glut.postRedisplay
()
1547 enttext (Some
(c, text, onhist, onkey
, ondone));
1550 state.textentry <- None
;
1551 Glut.postRedisplay
()
1554 state.textentry <- Some te
;
1555 Glut.postRedisplay
()
1560 let narrow outlines pattern
=
1561 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1565 let rec fold accu n =
1569 let (s, _
, _
, _
) as o
= outlines.(n) in
1571 if (try ignore
(Str.search_forward re
s 0); true
1572 with Not_found
-> false)
1578 let matched = fold [] (Array.length
outlines - 1) in
1579 if matched = [] then None
else Some
(Array.of_list
matched)
1582 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1583 let search active pattern
incr =
1586 if n = Array.length
outlines || n = -1
1589 let (s, _
, _
, _
) = outlines.(n) in
1591 (try ignore
(Str.search_forward re
s 0); true
1592 with Not_found
-> false)
1594 else loop (n + incr)
1599 let re = Str.regexp_case_fold pattern
in
1605 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1608 if String.length qsearch
= 0
1611 state.outline <- None
;
1612 Glut.postRedisplay
();
1616 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1617 Glut.postRedisplay
();
1621 let incr = if key
= 18 then -1 else 1 in
1623 match search (active + incr) qsearch
incr with
1625 state.text <- qsearch ^
" [not found]";
1628 state.text <- qsearch
;
1629 active, firstof active
1631 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1632 Glut.postRedisplay
();
1635 let len = String.length qsearch
in
1642 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1645 let qsearch = String.sub
qsearch 0 (len - 1) in
1647 match search active qsearch ~
-1 with
1649 state.text <- qsearch ^
" [not found]";
1652 state.text <- qsearch;
1653 active, firstof active
1655 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1657 Glut.postRedisplay
()
1660 if active < Array.length
outlines
1662 let (_
, _
, n, t
) = outlines.(active) in
1666 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1667 state.outline <- None
;
1668 Glut.postRedisplay
();
1670 | _
when key
>= 32 && key
< 127 ->
1671 let pattern = addchar qsearch (Char.chr key
) in
1673 match search active pattern 1 with
1675 state.text <- pattern ^
" [not found]";
1678 state.text <- pattern;
1679 active, firstof active
1681 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1682 Glut.postRedisplay
()
1684 | 14 when not allowdel
-> (* ctrl-n *)
1685 if String.length
qsearch > 0
1687 let optoutlines = narrow outlines qsearch in
1688 begin match optoutlines with
1689 | None
-> state.text <- "can't narrow"
1691 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1692 match state.outlines with
1695 state.outlines <- Onarrow
(qsearch, outlines, a)
1696 | Onarrow
(pat
, a, b) ->
1697 state.outlines <- Onarrow
(qsearch, outlines, b)
1700 Glut.postRedisplay
()
1702 | 21 when not allowdel
-> (* ctrl-u *)
1704 match state.outlines with
1707 let a = Array.of_list
(List.rev
l) in
1708 state.outlines <- Oarray
a;
1710 | Onarrow
(pat
, a, b) ->
1711 state.outlines <- Oarray
b;
1715 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1716 Glut.postRedisplay
()
1720 Some
(allowdel
, active, firstof active, outlines, qsearch);
1721 Glut.postRedisplay
()
1723 | 127 when allowdel
->
1724 let len = Array.length
outlines - 1 in
1727 state.outline <- None
;
1728 state.bookmarks <- [];
1731 let bookmarks = Array.init
len
1733 let i = if i >= active then i + 1 else i in
1744 Glut.postRedisplay
()
1746 | _
-> log "unknown key %d" key
1749 let keyboard ~key ~x ~
y =
1754 match state.outline with
1755 | None
-> viewkeyboard ~key ~x ~
y
1756 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1759 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1762 let pageno = max
0 (pageno - 1) in
1764 let rec loop = function
1767 if l.pageno = pageno
1774 then gotopage1 pageno 0
1775 else Glut.postRedisplay
();
1776 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
)
1779 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1780 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1781 if not
(pagevisible state.layout pageno)
1783 begin match List.rev
state.layout with
1784 | [] -> gotopage pageno 0.0
1786 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1788 else Glut.postRedisplay
();
1790 | Glut.KEY_PAGE_UP
->
1791 begin match state.layout with
1793 if l.pageno = pageno
1795 match layout (state.y - conf.winh
) conf.winh
with
1796 | [] -> gotoy (clamp (-conf.winh
))
1798 let pageno = max
0 (l.pageno - 1) in
1799 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1803 let pageno = max
0 (l.pageno - 1) in
1804 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1807 | [] -> gotoy (clamp (-conf.winh
))
1809 | Glut.KEY_PAGE_DOWN
->
1810 begin match List.rev
state.layout with
1812 let pageno = min
l.pageno (state.pagecount
- 1) in
1813 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1814 gotoy (clamp (l.pagedispy
+ l.pageh
))
1815 | [] -> gotoy (clamp conf.winh
)
1819 state.birdseye
<- Some
(conf, leftx
, 0, hooverpageno
);
1822 let pageno = state.pagecount
- 1 in
1823 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1824 if not
(pagevisible state.layout pageno)
1825 then gotopage pageno 0.0
1826 else Glut.postRedisplay
();
1830 let special ~key ~x ~
y =
1831 match state.outline, state.birdseye
with
1832 | None
, _
when key
= Glut.KEY_F9
->
1834 reshape conf.winw
conf.winh
;
1836 | None
, (Some vals
) ->
1837 birdseyespecial key x
y vals
1840 begin match state.textentry with
1844 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1845 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1846 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1847 | Glut.KEY_PAGE_UP
->
1848 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1850 match state.layout with
1852 | l :: _
-> state.y - l.pagey
1855 | Glut.KEY_PAGE_DOWN
->
1856 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1858 match List.rev
state.layout with
1860 | l :: _
-> getpagey l.pageno
1863 | Glut.KEY_HOME
-> addnav (); 0
1866 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1868 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1869 state.x
<- state.x
- 10;
1871 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1872 state.x
<- state.x
+ 10;
1877 gotoy_and_clear_text y
1879 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1882 | Glut.KEY_UP
-> action HCprev
1883 | Glut.KEY_DOWN
-> action HCnext
1884 | Glut.KEY_HOME
-> action HCfirst
1885 | Glut.KEY_END
-> action HClast
1888 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1889 Glut.postRedisplay
()
1894 | Some
(allowdel
, active, first
, outlines, qsearch), _
->
1895 let maxrows = maxoutlinerows () in
1896 let calcfirst first
active =
1899 let rows = active - first
in
1900 if rows > maxrows then active - maxrows else first
1904 let active = active + incr in
1905 let active = max
0 (min
active (Array.length
outlines - 1)) in
1906 let first = calcfirst first active in
1907 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1908 Glut.postRedisplay
()
1910 let updownlevel incr =
1911 let len = Array.length
outlines in
1912 let (_
, curlevel
, _
, _
) = outlines.(active) in
1914 if i = len then i-1 else if i = -1 then 0 else
1915 let (_
, l, _
, _
) = outlines.(i) in
1916 if l != curlevel
then i else flow (i+incr)
1918 let active = flow active in
1919 let first = calcfirst first active in
1920 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1921 Glut.postRedisplay
()
1924 | Glut.KEY_UP
-> navigate ~
-1
1925 | Glut.KEY_DOWN
-> navigate 1
1926 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1927 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1929 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1930 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1933 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1934 Glut.postRedisplay
()
1937 let active = Array.length
outlines - 1 in
1938 let first = max
0 (active - maxrows) in
1939 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1940 Glut.postRedisplay
()
1945 let drawplaceholder l =
1946 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1948 (float l.pagex
, float l.pagedispy
)
1949 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1951 let x = float (if margin < 0 then -margin else l.pagex
)
1952 and y = float (l.pagedispy
+ 13) in
1953 let font = Glut.BITMAP_8_BY_13
in
1954 GlDraw.color
(0.0, 0.0, 0.0);
1955 GlPix.raster_pos ~
x ~
y ();
1956 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1957 ("Loading " ^ string_of_int
(l.pageno + 1));
1960 let now () = Unix.gettimeofday
();;
1963 if state.textentry = None
1965 match state.birdseye
with
1966 | None
-> GlDraw.color
(scalecolor 1.0);
1967 | Some
(_
, _
, pageno, hooverpageno
) ->
1969 if l.pageno = pageno
1972 if l.pageno = hooverpageno
1977 GlDraw.color (scalecolor color);
1979 else GlDraw.color (scalecolor 0.4);
1980 begin match getopaque l.pageno with
1981 | Some
(opaque, _
) when validopaque opaque ->
1983 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1987 vlog "draw %d %f sec" l.pageno d;
1995 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1996 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1997 let sh = float conf.winh
/. sh in
1998 let sh = max
sh (float conf.scrollh
) in
2001 if state.y = state.maxy
2003 else float y /. float maxy
2005 let position = (float conf.winh
-. sh) *. percent in
2008 if position +. sh > float conf.winh
2009 then float conf.winh
-. sh
2015 let scrollindicator () =
2016 GlDraw.color (0.64 , 0.64, 0.64);
2018 (float (conf.winw
- conf.scrollw
), 0.)
2019 (float conf.winw
, float conf.winh
)
2021 GlDraw.color (0.0, 0.0, 0.0);
2023 let position, sh = scrollph state.y in
2025 (float (conf.winw
- conf.scrollw
), position)
2026 (float conf.winw
, position +. sh)
2030 let showsel margin =
2031 match state.mstate
with
2032 | Mnone
| Mscroll _
| Mpan _
->
2035 | Msel
((x0
, y0
), (x1
, y1
)) ->
2036 let rec loop = function
2038 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2039 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2041 match getopaque l.pageno with
2042 | Some
(opaque, _
) when validopaque opaque ->
2043 let oy = -l.pagey + l.pagedispy
in
2045 (x0
- margin - state.x, y0
,
2046 x1
- margin - state.x, y1
) oy;
2056 let panx = float state.x in
2058 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2059 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2061 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2063 if l.pageno = pageno
2065 let d = float (l.pagedispy
- l.pagey) in
2066 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2067 GlDraw.begins `quads
;
2069 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2070 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2071 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2072 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2082 let showoutline = function
2084 | Some
(allowdel
, active, first, outlines, qsearch) ->
2086 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2087 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2088 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2091 GlDraw.color (1., 1., 1.);
2092 let font = Glut.BITMAP_9_BY_15
in
2093 let draw_string x y s =
2094 GlPix.raster_pos ~
x ~
y ();
2095 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2098 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2101 let (s, l, _
, _
) = outlines.(row
) in
2102 let y = (row
- first) * 16 in
2107 GlDraw.polygon_mode `both `line
;
2108 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2109 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2110 GlDraw.rect (0., float (y + 1))
2111 (float (conf.winw
- 1), float (y + 18));
2112 GlDraw.polygon_mode `both `fill
;
2114 GlDraw.color (1., 1., 1.);
2116 draw_string (float x) (float (y + 16)) s;
2124 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2125 GlDraw.viewport
margin 0 state.w conf.winh
;
2127 GlClear.color (scalecolor 0.5);
2128 GlClear.clear
[`
color];
2131 let x = float state.x in
2132 GlMat.translate ~
x ();
2136 Gl.enable `scissor_test
;
2137 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2139 List.iter
drawpage state.layout;
2142 Gl.disable `scissor_test
2146 let x = -.float state.x in
2147 GlMat.translate ~
x ();
2151 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2154 showoutline state.outline;
2156 Glut.swapBuffers
();
2160 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2161 let x = x - margin - state.x in
2162 let rec f = function
2164 begin match getopaque l.pageno with
2165 | Some
(opaque, _
) when validopaque opaque ->
2166 let y = y - l.pagedispy
in
2169 let y = l.pagey + y in
2170 let x = x - l.pagex
in
2171 match whatsunder
opaque x y with
2184 let mouse ~button ~bstate ~
x ~
y =
2186 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2194 let incr = incr * 2 in
2195 let y = clamp incr in
2196 gotoy_and_clear_text y
2198 | Glut.LEFT_BUTTON
when state.outline = None
2199 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2200 if bstate
= Glut.DOWN
2202 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2203 state.mstate
<- Mpan
(x, y)
2206 state.mstate
<- Mnone
2209 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2210 if bstate
= Glut.DOWN
2212 let position, sh = scrollph state.y in
2213 if y > truncate
position && y < truncate
(position +. sh)
2215 state.mstate
<- Mscroll
2217 let percent = float y /. float conf.winh
in
2218 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2220 state.mstate
<- Mscroll
2222 state.mstate
<- Mnone
2224 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2225 begin match state.birdseye
with
2226 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2227 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2228 let rec loop = function
2231 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2232 && x > margin && x < margin + l.pagew
2234 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2235 reshape conf.winw
conf.winh
;
2240 | None
-> () (* impossible *)
2243 | Glut.LEFT_BUTTON
when state.outline = None
->
2244 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2245 begin match dest with
2246 | Ulinkgoto
(pageno, top
) ->
2249 gotopage1 pageno top
2254 | Unone
when bstate
= Glut.DOWN
->
2255 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2256 state.mstate
<- Mpan
(x, y);
2258 | Unone
| Utext _
->
2259 if bstate
= Glut.DOWN
2261 if conf.angle
mod 360 = 0
2263 state.mstate
<- Msel
((x, y), (x, y));
2264 Glut.postRedisplay
()
2268 match state.mstate
with
2272 state.mstate
<- Mnone
2275 Glut.setCursor
Glut.CURSOR_INHERIT
;
2276 state.mstate
<- Mnone
2278 | Msel
((x0
, y0
), (x1
, y1
)) ->
2280 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2281 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2283 match getopaque l.pageno with
2284 | Some
(opaque, _
) when validopaque opaque ->
2288 List.iter
f state.layout;
2289 copysel
""; (* ugly *)
2290 Glut.setCursor
Glut.CURSOR_INHERIT
;
2291 state.mstate
<- Mnone
;
2298 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2301 if state.outline = None
2303 match state.mstate
with
2309 state.mstate
<- Mpan
(x, y);
2310 if conf.zoom > 1.0 then state.x <- state.x + dx;
2312 gotoy_and_clear_text y
2315 state.mstate
<- Msel
(a, (x, y));
2316 Glut.postRedisplay
()
2319 let y = min
conf.winh
(max
0 y) in
2320 let percent = float y /. float conf.winh
in
2321 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2322 gotoy_and_clear_text y
2326 match state.birdseye
with
2327 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2328 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2329 let rec loop = function
2331 if hooverpageno
!= -1
2333 state.birdseye
<- Some
(conf, leftx
, pageno, -1);
2334 Glut.postRedisplay
();
2337 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2338 && x > margin && x < margin + l.pagew
2340 state.birdseye
<- Some
(conf, leftx
, pageno, l.pageno);
2341 Glut.postRedisplay
();
2348 if state.outline = None
2350 match state.mstate
with
2352 begin match getunder x y with
2353 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2355 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2356 Glut.setCursor
Glut.CURSOR_INFO
2357 | Ulinkgoto
(page
, y) ->
2359 then showtext 'p'
("age: " ^ string_of_int page
);
2360 Glut.setCursor
Glut.CURSOR_INFO
2362 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2363 Glut.setCursor
Glut.CURSOR_TEXT
2366 | Mpan _
| Msel _
| Mscroll
->
2377 match Sys.os_type
with
2378 | "Win32" -> Sys.getenv
"HOMEPATH"
2379 | _
-> Sys.getenv
"HOME"
2382 ("Can not determine home directory location: " ^
2383 Printexc.to_string exn
);
2387 let config_of c attrs
=
2391 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2392 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2393 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2394 | "preload" -> { c with preload = bool_of_string v
}
2395 | "page-bias" -> { c with pagebias
= int_of_string v
}
2396 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2397 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2398 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2399 | "throttle" -> { c with showall
= bool_of_string v
}
2400 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2401 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2402 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2404 let zoom = float_of_string v
/. 100. in
2405 let zoom = max
0.01 (min
2.2 zoom) in
2406 { c with zoom = zoom }
2407 | "presentation" -> { c with presentation = bool_of_string v
}
2408 | "rotation-angle" -> { c with angle
= int_of_string v
}
2409 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2410 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2411 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2412 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2413 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2414 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2415 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2418 prerr_endline
("Error processing attribute (`" ^
2419 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2422 let rec fold c = function
2425 let c = apply c k v
in
2431 let bookmark_of attrs
=
2432 let rec fold title page
rely = function
2433 | ("title", v
) :: rest
-> fold v page
rely rest
2434 | ("page", v
) :: rest
-> fold title v
rely rest
2435 | ("rely", v
) :: rest
-> fold title page v rest
2436 | _
:: rest
-> fold title page
rely rest
2437 | [] -> title, page
, rely
2439 fold "invalid" "0" "0" attrs
2442 let setconf dst src
=
2443 dst
.scrollw
<- src
.scrollw
;
2444 dst
.scrollh
<- src
.scrollh
;
2445 dst
.icase
<- src
.icase
;
2446 dst
.preload <- src
.preload;
2447 dst
.pagebias
<- src
.pagebias
;
2448 dst
.verbose
<- src
.verbose
;
2449 dst
.scrollincr
<- src
.scrollincr
;
2450 dst
.maxhfit
<- src
.maxhfit
;
2451 dst
.crophack
<- src
.crophack
;
2452 dst
.autoscroll
<- src
.autoscroll
;
2453 dst
.showall
<- src
.showall
;
2454 dst
.hlinks
<- src
.hlinks
;
2455 dst
.underinfo
<- src
.underinfo
;
2456 dst
.interpagespace <- src
.interpagespace;
2457 dst
.zoom <- src
.zoom;
2458 dst
.presentation <- src
.presentation;
2459 dst
.angle
<- src
.angle
;
2460 dst
.winw
<- src
.winw
;
2461 dst
.winh
<- src
.winh
;
2462 dst
.savebmarks
<- src
.savebmarks
;
2463 dst
.memlimit
<- src
.memlimit
;
2464 dst
.proportional
<- src
.proportional
;
2465 dst
.texcount
<- src
.texcount
;
2466 dst
.sliceheight
<- src
.sliceheight
;
2470 let l = String.length
s in
2471 let b = Buffer.create
l in
2477 let h = Hashtbl.create
10 in
2478 let dc = { defconf with angle
= defconf.angle
} in
2479 let rec toplevel v t spos epos
=
2481 | Vdata
| Vcdata
| Vend
-> v
2482 | Vopen
("llppconfig", attrs
, closed
) ->
2485 else { v
with f = llppconfig
}
2487 error
"unexpected subelement at top level" s spos
2488 | Vclose tag
-> error
"unexpected close at top level" s spos
2490 and llppconfig v t spos epos
=
2492 | Vdata
| Vcdata
| Vend
-> v
2493 | Vopen
("defaults", attrs
, closed
) ->
2494 let c = config_of dc attrs
in
2498 else { v
with f = skip
"defaults" (fun () -> v
) }
2500 | Vopen
("doc", attrs
, closed
) ->
2503 List.assoc
"path" attrs
2504 with Not_found
-> error
"doc is missing path attribute" s spos
2506 let path = unent pathent in
2507 let c = config_of dc attrs
in
2510 float_of_string
(List.assoc
"rely" attrs
)
2514 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2519 int_of_string
(List.assoc
"pan" attrs
)
2523 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2527 then (Hashtbl.add
h path (c, [], x, y); v
)
2528 else { v
with f = doc
path x y c [] }
2530 | Vopen
(tag
, _
, closed
) ->
2531 error
"unexpected subelement in llppconfig" s spos
2533 | Vclose
"llppconfig" -> { v
with f = toplevel }
2534 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2536 and doc
path x y c bookmarks v t spos epos
=
2538 | Vdata
| Vcdata
-> v
2539 | Vend
-> error
"unexpected end of input in doc" s spos
2540 | Vopen
("bookmarks", attrs
, closed
) ->
2541 { v
with f = pbookmarks
path x y c bookmarks }
2543 | Vopen
(tag
, _
, _
) ->
2544 error
"unexpected subelement in doc" s spos
2547 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2548 { v
with f = llppconfig
}
2550 | Vclose tag
-> error
"unexpected close in doc" s spos
2552 and pbookmarks
path x y c bookmarks v t spos epos
=
2554 | Vdata
| Vcdata
-> v
2555 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2556 | Vopen
("item", attrs
, closed
) ->
2557 let titleent, spage
, srely
= bookmark_of attrs
in
2562 dolog "Failed to convert page %S to integer: %s"
2563 spage
(Printexc.to_string exn
);
2568 float_of_string srely
2570 dolog "Failed to convert rely %S to real: %s"
2571 srely
(Printexc.to_string exn
);
2574 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2576 then { v
with f = pbookmarks
path x y c bookmarks }
2579 { v
with f = skip
"item" f }
2582 error
"unexpected subelement in bookmarks" s spos
2584 | Vclose
"bookmarks" ->
2585 { v
with f = doc
path x y c bookmarks }
2587 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2589 and skip tag
f v t spos epos
=
2591 | Vdata
| Vcdata
-> v
2593 error
("unexpected end of input in skipped " ^ tag
) s spos
2594 | Vopen
(tag'
, _
, closed
) ->
2598 let f'
() = { v
with f = skip tag
f } in
2599 { v
with f = skip tag'
f'
}
2603 else error
("unexpected close in skipped " ^ tag
) s spos
2606 parse
{ f = toplevel; accu = () } s;
2612 let len = in_channel_length ic
in
2613 let s = String.create
len in
2614 really_input ic
s 0 len;
2617 | Parse_error
(msg
, s, pos
) ->
2618 let subs = subs s pos
in
2619 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2620 failwith
("parse error: " ^
s)
2623 failwith
("config load error: " ^
Printexc.to_string exn
)
2629 let dir = Filename.concat
home ".config" in
2630 if Sys.is_directory
dir then dir else home
2633 Filename.concat
dir "llpp.conf"
2637 if Sys.file_exists
path
2640 (try Some
(open_in_bin
path)
2643 ("Error opening configuation file `" ^
path ^
"': " ^
2644 Printexc.to_string exn
);
2653 ("Error loading configuation from `" ^
path ^
"': " ^
2654 Printexc.to_string exn
);
2660 f (Hashtbl.create
0, defconf)
2665 let pc, pb
, px
, py =
2667 Hashtbl.find
h state.path
2668 with Not_found
-> dc, [], 0, 0.0
2672 state.bookmarks <- pb
;
2674 cbput state.hists
.nav
py;
2679 let add_attrs bb always
dc c =
2682 then Printf.bprintf bb
"\n %s='%b'" s a
2685 then Printf.bprintf bb
"\n %s='%d'" s a
2688 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2692 then dc.winw
, dc.winh
2694 match state.fullscreen
with
2696 | None
-> c.winw
, c.winh
2698 let zoom, presentation, interpagespace, showall
=
2700 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2702 match state.birdseye
with
2703 | Some
(bc
, _
, _
, _
) ->
2704 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2705 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2707 oi
"width" w dc.winw
;
2708 oi
"height" h dc.winh
;
2709 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2710 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2711 ob "case-insensitive-search" c.icase
dc.icase
;
2712 ob "preload" c.preload dc.preload;
2713 oi
"page-bias" c.pagebias
dc.pagebias
;
2714 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2715 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2716 ob "crop-hack" c.crophack
dc.crophack
;
2717 ob "throttle" showall
dc.showall
;
2718 ob "highlight-links" c.hlinks
dc.hlinks
;
2719 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2720 oi
"vertical-margin" interpagespace dc.interpagespace;
2721 oz
"zoom" zoom dc.zoom;
2722 ob "presentation" presentation dc.presentation;
2723 oi
"rotation-angle" c.angle
dc.angle
;
2724 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2725 ob "proportional-display" c.proportional
dc.proportional
;
2726 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2727 oi
"texcount" c.texcount
dc.texcount
;
2728 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2732 let bb = Buffer.create
32768 in
2734 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2735 add_attrs bb true dc dc;
2736 Buffer.add_string
bb "/>\n";
2738 let adddoc path x y c bookmarks =
2739 if bookmarks == [] && c = dc && y = 0.0
2742 Printf.bprintf
bb "<doc path='%s'"
2743 (enent
path 0 (String.length
path));
2746 then Printf.bprintf
bb " rely='%f'" y;
2749 then Printf.bprintf
bb " pan='%d'" x;
2751 add_attrs bb false dc c;
2753 begin match bookmarks with
2754 | [] -> Buffer.add_string
bb "/>\n"
2756 Buffer.add_string
bb ">\n<bookmarks>\n";
2757 List.iter
(fun (title, _level
, page, rely) ->
2759 "<item title='%s' page='%d' rely='%f'/>\n"
2760 (enent
title 0 (String.length
title))
2764 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2770 match state.birdseye
with
2771 | Some
(_
, x, _
, _
) -> x
2774 adddoc state.path x (yratio state.y) conf
2775 (if conf.savebmarks
then state.bookmarks else []);
2777 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2778 if path <> state.path
2780 adddoc path x y c bookmarks
2782 Buffer.add_string
bb "</llppconfig>";
2785 if Buffer.length
bb > 0
2788 let tmp = path ^
".tmp" in
2789 let oc = open_out_bin
tmp in
2790 Buffer.output_buffer
oc bb;
2792 Sys.rename
tmp path;
2795 ("error while saving configuration: " ^
Printexc.to_string exn
)
2801 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2802 (fun s -> state.path <- s)
2803 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2806 if String.length
state.path = 0
2807 then (prerr_endline
"filename missing"; exit
1)
2809 if Filename.is_relative
state.path
2810 then Filename.concat
(Sys.getcwd
()) state.path
2818 let _ = Glut.init
Sys.argv
in
2819 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2820 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2821 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2824 if Sys.os_type
= "Unix"
2826 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2828 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2829 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2830 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2831 Unix.bind
sock addr;
2833 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2834 Unix.connect
csock addr;
2835 let ssock, _ = Unix.accept
sock in
2838 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2839 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2843 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2847 let () = Glut.displayFunc
display in
2848 let () = Glut.reshapeFunc
reshape in
2849 let () = Glut.keyboardFunc
keyboard in
2850 let () = Glut.specialFunc
special in
2851 let () = Glut.idleFunc
(Some
idle) in
2852 let () = Glut.mouseFunc
mouse in
2853 let () = Glut.motionFunc
motion in
2854 let () = Glut.passiveMotionFunc
pmotion in
2856 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2857 state.csock <- csock;
2858 state.ssock <- ssock;
2859 state.text <- "Opening " ^
path;
2860 writeopen state.path state.password
;
2864 let rec handlelablglutbug () =
2867 with Glut.BadEnum
"key in special_of_int" ->
2868 showtext '
!'
" LablGlut bug: special key not recognized";
2869 handlelablglutbug ()
2871 handlelablglutbug ();