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.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1277 reshape conf.winw
conf.winh
1282 try int_of_string
s with exc
->
1283 state.text <- Printf.sprintf
"bad integer `%s': %s"
1284 s (Printexc.to_string exc
);
1290 cbput state.hists
.pag
(string_of_int
n);
1291 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1294 let pageentry text key
=
1295 match Char.unsafe_chr key
with
1296 | 'g'
-> TEdone
text
1297 | _
-> intentry text key
1299 let text = "x" in text.[0] <- c;
1300 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1304 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1305 reshape conf.winw
conf.winh
;
1308 conf.hlinks
<- not
conf.hlinks
;
1309 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1310 Glut.postRedisplay
()
1313 conf.autoscroll
<- not
conf.autoscroll
1316 conf.presentation <- not
conf.presentation;
1317 showtext ' '
("presentation mode " ^
1318 if conf.presentation then "on" else "off");
1322 begin match state.fullscreen
with
1324 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1327 state.fullscreen
<- None
;
1332 gotoy_and_clear_text 0
1335 search state.searchpattern
true
1338 search state.searchpattern
false
1341 begin match state.layout with
1344 gotoy_and_clear_text (getpagey l.pageno)
1348 begin match List.rev
state.layout with
1351 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1352 gotoy_and_clear_text (getpagey pageno)
1356 begin match state.layout with
1359 let pageno = max
0 (l.pageno-1) in
1360 gotoy_and_clear_text (getpagey pageno)
1365 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1367 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1369 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1373 else (100. *. (float state.y /. float maxy)) in
1376 Printf.sprintf
"Page %d of %d %.2f%%"
1377 (fn+1) state.pagecount
percent
1380 "Pages %d-%d of %d %.2f%%"
1381 (fn+1) (ln
+1) state.pagecount
percent
1386 begin match state.layout with
1389 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1390 Glut.postRedisplay
();
1394 enterbookmarkmode ()
1398 match state.layout with
1401 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1405 enttext (Some
('~'
, "", None
, textentry, ondone))
1409 showtext ' '
"Quick bookmark added";
1412 begin match state.layout with
1414 let rect = getpdimrect
l.pagedimno
in
1418 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1419 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1421 (truncate
(rect.(1) -. rect.(0)),
1422 truncate
(rect.(3) -. rect.(0)))
1426 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1428 Glut.postRedisplay
();
1434 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1439 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1440 Glut.postRedisplay
()
1442 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1443 | 'j'
-> gotoy (clamp conf.scrollincr
)
1445 | '
r'
-> opendoc state.path
state.password
1448 vlog "huh? %d %c" key
(Char.chr key
);
1451 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1452 let len = String.length
text in
1455 state.textentry <- None
;
1456 Glut.postRedisplay
();
1459 let s = String.sub
text 0 (len - 1) in
1460 enttext (Some
(c, s, opthist
, onkey
, ondone))
1463 | Some
(c, text, onhist, onkey
, ondone) ->
1464 begin match Char.unsafe_chr key
with
1467 state.textentry <- None
;
1468 Glut.postRedisplay
()
1471 begin match onhist with
1473 | Some
(_
, onhistcancel
) -> onhistcancel
()
1475 state.textentry <- None
;
1476 Glut.postRedisplay
()
1479 begin match onkey
text key
with
1481 state.textentry <- None
;
1483 Glut.postRedisplay
()
1486 enttext (Some
(c, text, onhist, onkey
, ondone));
1489 state.textentry <- None
;
1490 Glut.postRedisplay
()
1493 state.textentry <- Some te
;
1494 Glut.postRedisplay
()
1499 let narrow outlines pattern
=
1500 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1504 let rec fold accu n =
1508 let (s, _
, _
, _
) as o
= outlines.(n) in
1510 if (try ignore
(Str.search_forward re
s 0); true
1511 with Not_found
-> false)
1517 let matched = fold [] (Array.length
outlines - 1) in
1518 if matched = [] then None
else Some
(Array.of_list
matched)
1521 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1522 let search active pattern
incr =
1525 if n = Array.length
outlines || n = -1
1528 let (s, _
, _
, _
) = outlines.(n) in
1530 (try ignore
(Str.search_forward re
s 0); true
1531 with Not_found
-> false)
1533 else loop (n + incr)
1538 let re = Str.regexp_case_fold pattern
in
1544 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1547 if String.length qsearch
= 0
1550 state.outline <- None
;
1551 Glut.postRedisplay
();
1555 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1556 Glut.postRedisplay
();
1560 let incr = if key
= 18 then -1 else 1 in
1562 match search (active + incr) qsearch
incr with
1564 state.text <- qsearch ^
" [not found]";
1567 state.text <- qsearch
;
1568 active, firstof active
1570 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1571 Glut.postRedisplay
();
1574 let len = String.length qsearch
in
1581 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1584 let qsearch = String.sub
qsearch 0 (len - 1) in
1586 match search active qsearch ~
-1 with
1588 state.text <- qsearch ^
" [not found]";
1591 state.text <- qsearch;
1592 active, firstof active
1594 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1596 Glut.postRedisplay
()
1599 if active < Array.length
outlines
1601 let (_
, _
, n, t
) = outlines.(active) in
1605 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1606 state.outline <- None
;
1607 Glut.postRedisplay
();
1609 | _
when key
>= 32 && key
< 127 ->
1610 let pattern = addchar qsearch (Char.chr key
) in
1612 match search active pattern 1 with
1614 state.text <- pattern ^
" [not found]";
1617 state.text <- pattern;
1618 active, firstof active
1620 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1621 Glut.postRedisplay
()
1623 | 14 when not allowdel
-> (* ctrl-n *)
1624 if String.length
qsearch > 0
1626 let optoutlines = narrow outlines qsearch in
1627 begin match optoutlines with
1628 | None
-> state.text <- "can't narrow"
1630 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1631 match state.outlines with
1634 state.outlines <- Onarrow
(qsearch, outlines, a)
1635 | Onarrow
(pat
, a, b) ->
1636 state.outlines <- Onarrow
(qsearch, outlines, b)
1639 Glut.postRedisplay
()
1641 | 21 when not allowdel
-> (* ctrl-u *)
1643 match state.outlines with
1646 let a = Array.of_list
(List.rev
l) in
1647 state.outlines <- Oarray
a;
1649 | Onarrow
(pat
, a, b) ->
1650 state.outlines <- Oarray
b;
1654 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1655 Glut.postRedisplay
()
1659 Some
(allowdel
, active, firstof active, outlines, qsearch);
1660 Glut.postRedisplay
()
1662 | 127 when allowdel
->
1663 let len = Array.length
outlines - 1 in
1666 state.outline <- None
;
1667 state.bookmarks <- [];
1670 let bookmarks = Array.init
len
1672 let i = if i >= active then i + 1 else i in
1683 Glut.postRedisplay
()
1685 | _
-> log "unknown key %d" key
1688 let keyboard ~key ~x ~
y =
1693 match state.outline with
1694 | None
-> viewkeyboard ~key ~x ~
y
1695 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1698 let special ~key ~x ~
y =
1699 match state.outline with
1700 | None
when state.birdseye
<> None
->
1701 begin match key
with
1703 let pageno = max
0 (state.birdseyepageno
- 1) in
1704 state.birdseyepageno
<- pageno;
1705 if not
(pagevisible pageno)
1706 then gotopage pageno 0.0
1707 else Glut.postRedisplay
();
1710 let pageno = min
(state.pagecount
- 1) (state.birdseyepageno
+ 1) in
1711 state.birdseyepageno
<- pageno;
1712 if not
(pagevisible pageno)
1714 begin match List.rev
state.layout with
1715 | [] -> gotopage pageno 0.0
1717 gotoy (state.y + conf.interpagespace + l.pageh
*2 - l.pagevh)
1719 else Glut.postRedisplay
();
1721 | Glut.KEY_PAGE_UP
->
1722 begin match state.layout with
1724 state.birdseyepageno
<- max
0 (l.pageno - 1);
1725 gotopage state.birdseyepageno
0.0
1726 | [] -> gotoy (clamp (-conf.winh
))
1728 | Glut.KEY_PAGE_DOWN
->
1729 begin match List.rev
state.layout with
1731 state.birdseyepageno
<- min
(state.pagecount
- 1) (l.pageno + 1);
1732 gotoy (clamp (l.pagedispy
+ conf.interpagespace + l.pageh
))
1733 | [] -> gotoy (clamp conf.winh
)
1737 state.birdseyepageno
<- 0;
1740 state.birdseyepageno
<- state.pagecount
- 1;
1741 if not
(pagevisible state.birdseyepageno
)
1743 gotopage state.birdseyepageno
0.0
1745 Glut.postRedisplay
()
1751 begin match state.textentry with
1755 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1756 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1757 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1758 | Glut.KEY_PAGE_UP
->
1759 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1761 match state.layout with
1763 | l :: _
-> state.y - l.pagey
1766 | Glut.KEY_PAGE_DOWN
->
1767 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1769 match List.rev
state.layout with
1771 | l :: _
-> getpagey l.pageno
1774 | Glut.KEY_HOME
-> addnav (); 0
1777 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1779 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1780 state.x
<- state.x
- 10;
1782 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1783 state.x
<- state.x
+ 10;
1788 gotoy_and_clear_text y
1790 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1793 | Glut.KEY_UP
-> action HCprev
1794 | Glut.KEY_DOWN
-> action HCnext
1795 | Glut.KEY_HOME
-> action HCfirst
1796 | Glut.KEY_END
-> action HClast
1799 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1800 Glut.postRedisplay
()
1805 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1806 let maxrows = maxoutlinerows () in
1807 let calcfirst first
active =
1810 let rows = active - first
in
1811 if rows > maxrows then active - maxrows else first
1815 let active = active + incr in
1816 let active = max
0 (min
active (Array.length
outlines - 1)) in
1817 let first = calcfirst first active in
1818 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1819 Glut.postRedisplay
()
1821 let updownlevel incr =
1822 let len = Array.length
outlines in
1823 let (_
, curlevel
, _
, _
) = outlines.(active) in
1825 if i = len then i-1 else if i = -1 then 0 else
1826 let (_
, l, _
, _
) = outlines.(i) in
1827 if l != curlevel
then i else flow (i+incr)
1829 let active = flow active in
1830 let first = calcfirst first active in
1831 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1832 Glut.postRedisplay
()
1835 | Glut.KEY_UP
-> navigate ~
-1
1836 | Glut.KEY_DOWN
-> navigate 1
1837 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1838 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1840 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1841 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1844 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1845 Glut.postRedisplay
()
1848 let active = Array.length
outlines - 1 in
1849 let first = max
0 (active - maxrows) in
1850 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1851 Glut.postRedisplay
()
1856 let drawplaceholder l =
1857 GlDraw.color
(scalecolor 1.0);
1859 (float l.pagex
, float l.pagedispy
)
1860 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1862 let x = float l.pagex
1863 and y = float (l.pagedispy
+ 13) in
1864 let font = Glut.BITMAP_8_BY_13
in
1865 GlDraw.color
(0.0, 0.0, 0.0);
1866 GlPix.raster_pos ~
x ~
y ();
1867 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1868 ("Loading " ^ string_of_int
(l.pageno + 1));
1871 let now () = Unix.gettimeofday
();;
1874 begin match getopaque l.pageno with
1875 | Some
(opaque, _
) when validopaque opaque ->
1876 if state.textentry = None
1877 then GlDraw.color
(scalecolor 1.0)
1878 else GlDraw.color
(scalecolor 0.4);
1880 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1884 vlog "draw %d %f sec" l.pageno d;
1889 if state.birdseye
<> None
&& state.birdseyepageno
= l.pageno
1891 GlDraw.polygon_mode `both `line
;
1892 GlDraw.line_width
4.0;
1893 GlDraw.color
(0.8, 0.0, 0.0);
1895 (float (l.pagex
- 1), float (l.pagedispy
- 1))
1896 (float (l.pagew
+ l.pagex
+ 1), float (l.pagedispy
+ l.pagevh + 1))
1898 GlDraw.line_width
1.0;
1899 GlDraw.polygon_mode `both `fill
;
1904 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1905 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1906 let sh = float conf.winh
/. sh in
1907 let sh = max
sh (float conf.scrollh
) in
1910 if state.y = state.maxy
1912 else float y /. float maxy
1914 let position = (float conf.winh
-. sh) *. percent in
1917 if position +. sh > float conf.winh
1918 then float conf.winh
-. sh
1924 let scrollindicator () =
1925 GlDraw.color
(0.64 , 0.64, 0.64);
1927 (float (conf.winw
- conf.scrollw
), 0.)
1928 (float conf.winw
, float conf.winh
)
1930 GlDraw.color
(0.0, 0.0, 0.0);
1932 let position, sh = scrollph state.y in
1934 (float (conf.winw
- conf.scrollw
), position)
1935 (float conf.winw
, position +. sh)
1939 let showsel margin
=
1940 match state.mstate
with
1941 | Mnone
| Mscroll _
| Mpan _
->
1944 | Msel
((x0
, y0
), (x1
, y1
)) ->
1945 let rec loop = function
1947 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1948 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1950 match getopaque l.pageno with
1951 | Some
(opaque, _
) when validopaque opaque ->
1952 let oy = -l.pagey + l.pagedispy
in
1954 (x0
- margin
- state.x, y0
,
1955 x1
- margin
- state.x, y1
) oy;
1965 let panx = float state.x in
1967 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1968 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1970 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1972 if l.pageno = pageno
1974 let d = float (l.pagedispy
- l.pagey) in
1975 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1976 GlDraw.begins `quads
;
1978 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1979 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1980 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1981 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1991 let showoutline = function
1993 | Some
(allowdel
, active, first, outlines, qsearch) ->
1995 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1996 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1997 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2000 GlDraw.color
(1., 1., 1.);
2001 let font = Glut.BITMAP_9_BY_15
in
2002 let draw_string x y s =
2003 GlPix.raster_pos ~
x ~
y ();
2004 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2007 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2010 let (s, l, _
, _
) = outlines.(row
) in
2011 let y = (row
- first) * 16 in
2016 GlDraw.polygon_mode `both `line
;
2017 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2018 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
2019 GlDraw.rect (0., float (y + 1))
2020 (float (conf.winw
- 1), float (y + 18));
2021 GlDraw.polygon_mode `both `fill
;
2023 GlDraw.color
(1., 1., 1.);
2025 draw_string (float x) (float (y + 16)) s;
2033 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2034 GlDraw.viewport
margin 0 state.w conf.winh
;
2036 if state.birdseye
<> None
2038 GlClear.color
(0.5, 0.5, 0.55)
2040 GlClear.color
(scalecolor 0.5)
2042 GlClear.clear
[`color
];
2045 let x = float state.x in
2046 GlMat.translate ~
x ();
2050 Gl.enable `scissor_test
;
2051 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2053 List.iter
drawpage state.layout;
2056 Gl.disable `scissor_test
2060 let x = -.float state.x in
2061 GlMat.translate ~
x ();
2065 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2068 showoutline state.outline;
2070 Glut.swapBuffers
();
2074 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2075 let x = x - margin - state.x in
2076 let rec f = function
2078 begin match getopaque l.pageno with
2079 | Some
(opaque, _
) when validopaque opaque ->
2080 let y = y - l.pagedispy
in
2083 let y = l.pagey + y in
2084 let x = x - l.pagex
in
2085 match whatsunder
opaque x y with
2098 let mouse ~button ~bstate ~
x ~
y =
2100 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2108 let incr = incr * 2 in
2109 let y = clamp incr in
2110 gotoy_and_clear_text y
2112 | Glut.LEFT_BUTTON
when state.outline = None
2113 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2114 if bstate
= Glut.DOWN
2116 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2117 state.mstate
<- Mpan
(x, y)
2120 state.mstate
<- Mnone
2123 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2124 if bstate
= Glut.DOWN
2126 let position, sh = scrollph state.y in
2127 if y > truncate
position && y < truncate
(position +. sh)
2129 state.mstate
<- Mscroll
2131 let percent = float y /. float conf.winh
in
2132 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2134 state.mstate
<- Mscroll
2136 state.mstate
<- Mnone
2138 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2139 begin match state.birdseye
with
2141 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2142 let rec loop = function
2145 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2146 && x > margin && x < margin + l.pagew
2148 let y = getpagey l.pageno in
2151 reshape conf.winw
conf.winh
;
2156 | None
-> () (* impossible *)
2159 | Glut.LEFT_BUTTON
when state.outline = None
->
2160 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2161 begin match dest with
2162 | Ulinkgoto
(pageno, top
) ->
2165 gotopage1 pageno top
2170 | Unone
when bstate
= Glut.DOWN
->
2171 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2172 state.mstate
<- Mpan
(x, y);
2174 | Unone
| Utext _
->
2175 if bstate
= Glut.DOWN
2177 if conf.angle
mod 360 = 0
2179 state.mstate
<- Msel
((x, y), (x, y));
2180 Glut.postRedisplay
()
2184 match state.mstate
with
2188 state.mstate
<- Mnone
2191 Glut.setCursor
Glut.CURSOR_INHERIT
;
2192 state.mstate
<- Mnone
2194 | Msel
((x0
, y0
), (x1
, y1
)) ->
2196 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2197 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2199 match getopaque l.pageno with
2200 | Some
(opaque, _
) when validopaque opaque ->
2204 List.iter
f state.layout;
2205 copysel
""; (* ugly *)
2206 Glut.setCursor
Glut.CURSOR_INHERIT
;
2207 state.mstate
<- Mnone
;
2214 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2217 if state.outline = None
2219 match state.mstate
with
2225 state.mstate
<- Mpan
(x, y);
2226 if conf.zoom > 1.0 then state.x <- state.x + dx;
2228 gotoy_and_clear_text y
2231 state.mstate
<- Msel
(a, (x, y));
2232 Glut.postRedisplay
()
2235 let y = min
conf.winh
(max
0 y) in
2236 let percent = float y /. float conf.winh
in
2237 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2238 gotoy_and_clear_text y
2242 if state.outline = None
&& state.birdseye
= None
2244 match state.mstate
with
2246 begin match getunder x y with
2247 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2249 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2250 Glut.setCursor
Glut.CURSOR_INFO
2251 | Ulinkgoto
(page
, y) ->
2252 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2253 Glut.setCursor
Glut.CURSOR_INFO
2255 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2256 Glut.setCursor
Glut.CURSOR_TEXT
2259 | Mpan _
| Msel _
| Mscroll
->
2269 match Sys.os_type
with
2270 | "Win32" -> Sys.getenv
"HOMEPATH"
2271 | _
-> Sys.getenv
"HOME"
2274 ("Can not determine home directory location: " ^
2275 Printexc.to_string exn
);
2279 let config_of c attrs
=
2283 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2284 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2285 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2286 | "preload" -> { c with preload = bool_of_string v
}
2287 | "page-bias" -> { c with pagebias
= int_of_string v
}
2288 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2289 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2290 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2291 | "throttle" -> { c with showall
= bool_of_string v
}
2292 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2293 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2294 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2296 let zoom = float_of_string v
/. 100. in
2297 let zoom = max
0.01 (min
2.2 zoom) in
2298 { c with zoom = zoom }
2299 | "presentation" -> { c with presentation = bool_of_string v
}
2300 | "rotation-angle" -> { c with angle
= int_of_string v
}
2301 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2302 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2303 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2304 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2305 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2306 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2307 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2310 prerr_endline
("Error processing attribute (`" ^
2311 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2314 let rec fold c = function
2317 let c = apply c k v
in
2323 let bookmark_of attrs
=
2324 let rec fold title page
rely = function
2325 | ("title", v
) :: rest
-> fold v page
rely rest
2326 | ("page", v
) :: rest
-> fold title v
rely rest
2327 | ("rely", v
) :: rest
-> fold title page v rest
2328 | _
:: rest
-> fold title page
rely rest
2329 | [] -> title, page
, rely
2331 fold "invalid" "0" "0" attrs
2334 let setconf dst src
=
2335 dst
.scrollw
<- src
.scrollw
;
2336 dst
.scrollh
<- src
.scrollh
;
2337 dst
.icase
<- src
.icase
;
2338 dst
.preload <- src
.preload;
2339 dst
.pagebias
<- src
.pagebias
;
2340 dst
.verbose
<- src
.verbose
;
2341 dst
.scrollincr
<- src
.scrollincr
;
2342 dst
.maxhfit
<- src
.maxhfit
;
2343 dst
.crophack
<- src
.crophack
;
2344 dst
.autoscroll
<- src
.autoscroll
;
2345 dst
.showall
<- src
.showall
;
2346 dst
.hlinks
<- src
.hlinks
;
2347 dst
.underinfo
<- src
.underinfo
;
2348 dst
.interpagespace <- src
.interpagespace;
2349 dst
.zoom <- src
.zoom;
2350 dst
.presentation <- src
.presentation;
2351 dst
.angle
<- src
.angle
;
2352 dst
.winw
<- src
.winw
;
2353 dst
.winh
<- src
.winh
;
2354 dst
.savebmarks
<- src
.savebmarks
;
2355 dst
.memlimit
<- src
.memlimit
;
2356 dst
.proportional
<- src
.proportional
;
2357 dst
.texcount
<- src
.texcount
;
2358 dst
.sliceheight
<- src
.sliceheight
;
2362 let l = String.length
s in
2363 let b = Buffer.create
l in
2369 let h = Hashtbl.create
10 in
2370 let dc = { defconf with angle
= defconf.angle
} in
2371 let rec toplevel v t spos epos
=
2373 | Vdata
| Vcdata
| Vend
-> v
2374 | Vopen
("llppconfig", attrs
, closed
) ->
2377 else { v
with f = llppconfig
}
2379 error
"unexpected subelement at top level" s spos
2380 | Vclose tag
-> error
"unexpected close at top level" s spos
2382 and llppconfig v t spos epos
=
2384 | Vdata
| Vcdata
| Vend
-> v
2385 | Vopen
("defaults", attrs
, closed
) ->
2386 let c = config_of dc attrs
in
2390 else { v
with f = skip
"defaults" (fun () -> v
) }
2392 | Vopen
("doc", attrs
, closed
) ->
2395 List.assoc
"path" attrs
2396 with Not_found
-> error
"doc is missing path attribute" s spos
2398 let path = unent pathent in
2399 let c = config_of dc attrs
in
2402 float_of_string
(List.assoc
"rely" attrs
)
2406 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2411 int_of_string
(List.assoc
"pan" attrs
)
2415 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2419 then (Hashtbl.add
h path (c, [], x, y); v
)
2420 else { v
with f = doc
path x y c [] }
2422 | Vopen
(tag
, _
, closed
) ->
2423 error
"unexpected subelement in llppconfig" s spos
2425 | Vclose
"llppconfig" -> { v
with f = toplevel }
2426 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2428 and doc
path x y c bookmarks v t spos epos
=
2430 | Vdata
| Vcdata
-> v
2431 | Vend
-> error
"unexpected end of input in doc" s spos
2432 | Vopen
("bookmarks", attrs
, closed
) ->
2433 { v
with f = pbookmarks
path x y c bookmarks }
2435 | Vopen
(tag
, _
, _
) ->
2436 error
"unexpected subelement in doc" s spos
2439 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2440 { v
with f = llppconfig
}
2442 | Vclose tag
-> error
"unexpected close in doc" s spos
2444 and pbookmarks
path x y c bookmarks v t spos epos
=
2446 | Vdata
| Vcdata
-> v
2447 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2448 | Vopen
("item", attrs
, closed
) ->
2449 let titleent, spage
, srely
= bookmark_of attrs
in
2454 dolog "Failed to convert page %S to integer: %s"
2455 spage
(Printexc.to_string exn
);
2460 float_of_string srely
2462 dolog "Failed to convert rely %S to real: %s"
2463 srely
(Printexc.to_string exn
);
2466 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2468 then { v
with f = pbookmarks
path x y c bookmarks }
2471 { v
with f = skip
"item" f }
2474 error
"unexpected subelement in bookmarks" s spos
2476 | Vclose
"bookmarks" ->
2477 { v
with f = doc
path x y c bookmarks }
2479 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2481 and skip tag
f v t spos epos
=
2483 | Vdata
| Vcdata
-> v
2485 error
("unexpected end of input in skipped " ^ tag
) s spos
2486 | Vopen
(tag'
, _
, closed
) ->
2490 let f'
() = { v
with f = skip tag
f } in
2491 { v
with f = skip tag'
f'
}
2495 else error
("unexpected close in skipped " ^ tag
) s spos
2498 parse
{ f = toplevel; accu = () } s;
2504 let len = in_channel_length ic
in
2505 let s = String.create
len in
2506 really_input ic
s 0 len;
2509 | Parse_error
(msg
, s, pos
) ->
2510 let subs = subs s pos
in
2511 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2512 failwith
("parse error: " ^
s)
2515 failwith
("config load error: " ^
Printexc.to_string exn
)
2521 let dir = Filename.concat
home ".config" in
2522 if Sys.is_directory
dir then dir else home
2525 Filename.concat
dir "llpp.conf"
2529 if Sys.file_exists
path
2532 (try Some
(open_in_bin
path)
2535 ("Error opening configuation file `" ^
path ^
"': " ^
2536 Printexc.to_string exn
);
2545 ("Error loading configuation from `" ^
path ^
"': " ^
2546 Printexc.to_string exn
);
2552 f (Hashtbl.create
0, defconf)
2557 let pc, pb
, px
, py =
2559 Hashtbl.find
h state.path
2560 with Not_found
-> dc, [], 0, 0.0
2564 state.bookmarks <- pb
;
2566 cbput state.hists
.nav
py;
2571 let add_attrs bb always
dc c =
2574 then Printf.bprintf bb
"\n %s='%b'" s a
2577 then Printf.bprintf bb
"\n %s='%d'" s a
2580 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2584 then dc.winw
, dc.winh
2586 match state.fullscreen
with
2588 | None
-> c.winw
, c.winh
2590 let zoom, presentation, interpagespace =
2592 then dc.zoom, dc.presentation, dc.interpagespace
2594 match state.birdseye
with
2595 | Some
(zoom, _
, presentation, interpagespace) ->
2596 (zoom, presentation, interpagespace)
2597 | None
-> c.zoom, c.presentation, c.interpagespace
2599 oi
"width" w dc.winw
;
2600 oi
"height" h dc.winh
;
2601 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2602 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2603 ob "case-insensitive-search" c.icase
dc.icase
;
2604 ob "preload" c.preload dc.preload;
2605 oi
"page-bias" c.pagebias
dc.pagebias
;
2606 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2607 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2608 ob "crop-hack" c.crophack
dc.crophack
;
2609 ob "throttle" c.showall
dc.showall
;
2610 ob "highlight-links" c.hlinks
dc.hlinks
;
2611 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2612 oi
"vertical-margin" interpagespace dc.interpagespace;
2613 oz
"zoom" zoom dc.zoom;
2614 ob "presentation" presentation dc.presentation;
2615 oi
"rotation-angle" c.angle
dc.angle
;
2616 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2617 ob "proportional-display" c.proportional
dc.proportional
;
2618 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2619 oi
"texcount" c.texcount
dc.texcount
;
2620 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2624 let bb = Buffer.create
32768 in
2626 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2627 add_attrs bb true dc dc;
2628 Buffer.add_string
bb "/>\n";
2630 let adddoc path x y c bookmarks =
2631 if bookmarks == [] && c = dc && y = 0.0
2634 Printf.bprintf
bb "<doc path='%s'"
2635 (enent
path 0 (String.length
path));
2638 then Printf.bprintf
bb " rely='%f'" y;
2641 then Printf.bprintf
bb " pan='%d'" x;
2643 add_attrs bb false dc c;
2645 begin match bookmarks with
2646 | [] -> Buffer.add_string
bb "/>\n"
2648 Buffer.add_string
bb ">\n<bookmarks>\n";
2649 List.iter
(fun (title, _level
, page, rely) ->
2651 "<item title='%s' page='%d' rely='%f'/>\n"
2652 (enent
title 0 (String.length
title))
2656 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2662 match state.birdseye
with
2663 | Some
(_
, x, _
, _
) -> x
2666 adddoc state.path x (yratio state.y) conf
2667 (if conf.savebmarks
then state.bookmarks else []);
2669 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2670 if path <> state.path
2672 adddoc path x y c bookmarks
2674 Buffer.add_string
bb "</llppconfig>";
2677 if Buffer.length
bb > 0
2680 let tmp = path ^
".tmp" in
2681 let oc = open_out_bin
tmp in
2682 Buffer.output_buffer
oc bb;
2684 Sys.rename
tmp path;
2687 ("error while saving configuration: " ^
Printexc.to_string exn
)
2693 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2694 (fun s -> state.path <- s)
2695 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2698 if String.length
state.path = 0
2699 then (prerr_endline
"filename missing"; exit
1)
2701 if Filename.is_relative
state.path
2702 then Filename.concat
(Sys.getcwd
()) state.path
2710 let _ = Glut.init
Sys.argv
in
2711 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2712 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2713 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2716 if Sys.os_type
= "Unix"
2718 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2720 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2721 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2722 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2723 Unix.bind
sock addr;
2725 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2726 Unix.connect
csock addr;
2727 let ssock, _ = Unix.accept
sock in
2730 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2731 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2735 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2739 let () = Glut.displayFunc
display in
2740 let () = Glut.reshapeFunc
reshape in
2741 let () = Glut.keyboardFunc
keyboard in
2742 let () = Glut.specialFunc
special in
2743 let () = Glut.idleFunc
(Some
idle) in
2744 let () = Glut.mouseFunc
mouse in
2745 let () = Glut.motionFunc
motion in
2746 let () = Glut.passiveMotionFunc
pmotion in
2748 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2749 state.csock <- csock;
2750 state.ssock <- ssock;
2751 state.text <- "Opening " ^
path;
2752 writeopen state.path state.password
;
2756 let rec handlelablglutbug () =
2759 with Glut.BadEnum
"key in special_of_int" ->
2760 showtext '
!'
" LablGlut bug: special key not recognized";
2761 handlelablglutbug ()
2763 handlelablglutbug ();