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 gotopage state.birdseyepageno
0.0
1746 begin match state.textentry with
1750 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1751 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1752 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1753 | Glut.KEY_PAGE_UP
->
1754 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1756 match state.layout with
1758 | l :: _
-> state.y - l.pagey
1761 | Glut.KEY_PAGE_DOWN
->
1762 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1764 match List.rev
state.layout with
1766 | l :: _
-> getpagey l.pageno
1769 | Glut.KEY_HOME
-> addnav (); 0
1772 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1774 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1775 state.x
<- state.x
- 10;
1777 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1778 state.x
<- state.x
+ 10;
1783 gotoy_and_clear_text y
1785 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1788 | Glut.KEY_UP
-> action HCprev
1789 | Glut.KEY_DOWN
-> action HCnext
1790 | Glut.KEY_HOME
-> action HCfirst
1791 | Glut.KEY_END
-> action HClast
1794 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1795 Glut.postRedisplay
()
1800 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1801 let maxrows = maxoutlinerows () in
1802 let calcfirst first
active =
1805 let rows = active - first
in
1806 if rows > maxrows then active - maxrows else first
1810 let active = active + incr in
1811 let active = max
0 (min
active (Array.length
outlines - 1)) in
1812 let first = calcfirst first active in
1813 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1814 Glut.postRedisplay
()
1816 let updownlevel incr =
1817 let len = Array.length
outlines in
1818 let (_
, curlevel
, _
, _
) = outlines.(active) in
1820 if i = len then i-1 else if i = -1 then 0 else
1821 let (_
, l, _
, _
) = outlines.(i) in
1822 if l != curlevel
then i else flow (i+incr)
1824 let active = flow active in
1825 let first = calcfirst first active in
1826 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1827 Glut.postRedisplay
()
1830 | Glut.KEY_UP
-> navigate ~
-1
1831 | Glut.KEY_DOWN
-> navigate 1
1832 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1833 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1835 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1836 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1839 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1840 Glut.postRedisplay
()
1843 let active = Array.length
outlines - 1 in
1844 let first = max
0 (active - maxrows) in
1845 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1846 Glut.postRedisplay
()
1851 let drawplaceholder l =
1852 GlDraw.color
(scalecolor 1.0);
1854 (float l.pagex
, float l.pagedispy
)
1855 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1857 let x = float l.pagex
1858 and y = float (l.pagedispy
+ 13) in
1859 let font = Glut.BITMAP_8_BY_13
in
1860 GlDraw.color
(0.0, 0.0, 0.0);
1861 GlPix.raster_pos ~
x ~
y ();
1862 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1863 ("Loading " ^ string_of_int
(l.pageno + 1));
1866 let now () = Unix.gettimeofday
();;
1869 begin match getopaque l.pageno with
1870 | Some
(opaque, _
) when validopaque opaque ->
1871 if state.textentry = None
1872 then GlDraw.color
(scalecolor 1.0)
1873 else GlDraw.color
(scalecolor 0.4);
1875 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1879 vlog "draw %d %f sec" l.pageno d;
1884 if state.birdseye
<> None
&& state.birdseyepageno
= l.pageno
1886 GlDraw.polygon_mode `both `line
;
1887 GlDraw.line_width
4.0;
1888 GlDraw.color
(0.8, 0.0, 0.0);
1890 (float (l.pagex
- 1), float (l.pagedispy
- 1))
1891 (float (l.pagew
+ l.pagex
+ 1), float (l.pagedispy
+ l.pagevh + 1))
1893 GlDraw.line_width
1.0;
1894 GlDraw.polygon_mode `both `fill
;
1899 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1900 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1901 let sh = float conf.winh
/. sh in
1902 let sh = max
sh (float conf.scrollh
) in
1905 if state.y = state.maxy
1907 else float y /. float maxy
1909 let position = (float conf.winh
-. sh) *. percent in
1912 if position +. sh > float conf.winh
1913 then float conf.winh
-. sh
1919 let scrollindicator () =
1920 GlDraw.color
(0.64 , 0.64, 0.64);
1922 (float (conf.winw
- conf.scrollw
), 0.)
1923 (float conf.winw
, float conf.winh
)
1925 GlDraw.color
(0.0, 0.0, 0.0);
1927 let position, sh = scrollph state.y in
1929 (float (conf.winw
- conf.scrollw
), position)
1930 (float conf.winw
, position +. sh)
1934 let showsel margin
=
1935 match state.mstate
with
1936 | Mnone
| Mscroll _
| Mpan _
->
1939 | Msel
((x0
, y0
), (x1
, y1
)) ->
1940 let rec loop = function
1942 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1943 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1945 match getopaque l.pageno with
1946 | Some
(opaque, _
) when validopaque opaque ->
1947 let oy = -l.pagey + l.pagedispy
in
1949 (x0
- margin
- state.x, y0
,
1950 x1
- margin
- state.x, y1
) oy;
1960 let panx = float state.x in
1962 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1963 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1965 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1967 if l.pageno = pageno
1969 let d = float (l.pagedispy
- l.pagey) in
1970 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1971 GlDraw.begins `quads
;
1973 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1974 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1975 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1976 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1986 let showoutline = function
1988 | Some
(allowdel
, active, first, outlines, qsearch) ->
1990 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1991 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1992 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
1995 GlDraw.color
(1., 1., 1.);
1996 let font = Glut.BITMAP_9_BY_15
in
1997 let draw_string x y s =
1998 GlPix.raster_pos ~
x ~
y ();
1999 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2002 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2005 let (s, l, _
, _
) = outlines.(row
) in
2006 let y = (row
- first) * 16 in
2011 GlDraw.polygon_mode `both `line
;
2012 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2013 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
2014 GlDraw.rect (0., float (y + 1))
2015 (float (conf.winw
- 1), float (y + 18));
2016 GlDraw.polygon_mode `both `fill
;
2018 GlDraw.color
(1., 1., 1.);
2020 draw_string (float x) (float (y + 16)) s;
2028 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2029 GlDraw.viewport
margin 0 state.w conf.winh
;
2031 if state.birdseye
<> None
2033 GlClear.color
(0.5, 0.5, 0.55)
2035 GlClear.color
(scalecolor 0.5)
2037 GlClear.clear
[`color
];
2040 let x = float state.x in
2041 GlMat.translate ~
x ();
2045 Gl.enable `scissor_test
;
2046 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2048 List.iter
drawpage state.layout;
2051 Gl.disable `scissor_test
2055 let x = -.float state.x in
2056 GlMat.translate ~
x ();
2060 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2063 showoutline state.outline;
2065 Glut.swapBuffers
();
2069 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2070 let x = x - margin - state.x in
2071 let rec f = function
2073 begin match getopaque l.pageno with
2074 | Some
(opaque, _
) when validopaque opaque ->
2075 let y = y - l.pagedispy
in
2078 let y = l.pagey + y in
2079 let x = x - l.pagex
in
2080 match whatsunder
opaque x y with
2093 let mouse ~button ~bstate ~
x ~
y =
2095 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2103 let incr = incr * 2 in
2104 let y = clamp incr in
2105 gotoy_and_clear_text y
2107 | Glut.LEFT_BUTTON
when state.outline = None
2108 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2109 if bstate
= Glut.DOWN
2111 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2112 state.mstate
<- Mpan
(x, y)
2115 state.mstate
<- Mnone
2118 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2119 if bstate
= Glut.DOWN
2121 let position, sh = scrollph state.y in
2122 if y > truncate
position && y < truncate
(position +. sh)
2124 state.mstate
<- Mscroll
2126 let percent = float y /. float conf.winh
in
2127 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2129 state.mstate
<- Mscroll
2131 state.mstate
<- Mnone
2133 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2134 begin match state.birdseye
with
2136 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2137 let rec loop = function
2140 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2141 && x > margin && x < margin + l.pagew
2143 let y = getpagey l.pageno in
2146 reshape conf.winw
conf.winh
;
2151 | None
-> () (* impossible *)
2154 | Glut.LEFT_BUTTON
when state.outline = None
->
2155 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2156 begin match dest with
2157 | Ulinkgoto
(pageno, top
) ->
2160 gotopage1 pageno top
2165 | Unone
when bstate
= Glut.DOWN
->
2166 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2167 state.mstate
<- Mpan
(x, y);
2169 | Unone
| Utext _
->
2170 if bstate
= Glut.DOWN
2172 if conf.angle
mod 360 = 0
2174 state.mstate
<- Msel
((x, y), (x, y));
2175 Glut.postRedisplay
()
2179 match state.mstate
with
2183 state.mstate
<- Mnone
2186 Glut.setCursor
Glut.CURSOR_INHERIT
;
2187 state.mstate
<- Mnone
2189 | Msel
((x0
, y0
), (x1
, y1
)) ->
2191 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2192 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2194 match getopaque l.pageno with
2195 | Some
(opaque, _
) when validopaque opaque ->
2199 List.iter
f state.layout;
2200 copysel
""; (* ugly *)
2201 Glut.setCursor
Glut.CURSOR_INHERIT
;
2202 state.mstate
<- Mnone
;
2209 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2212 if state.outline = None
2214 match state.mstate
with
2220 state.mstate
<- Mpan
(x, y);
2221 if conf.zoom > 1.0 then state.x <- state.x + dx;
2223 gotoy_and_clear_text y
2226 state.mstate
<- Msel
(a, (x, y));
2227 Glut.postRedisplay
()
2230 let y = min
conf.winh
(max
0 y) in
2231 let percent = float y /. float conf.winh
in
2232 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2233 gotoy_and_clear_text y
2237 if state.outline = None
&& state.birdseye
= None
2239 match state.mstate
with
2241 begin match getunder x y with
2242 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2244 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2245 Glut.setCursor
Glut.CURSOR_INFO
2246 | Ulinkgoto
(page
, y) ->
2247 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2248 Glut.setCursor
Glut.CURSOR_INFO
2250 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2251 Glut.setCursor
Glut.CURSOR_TEXT
2254 | Mpan _
| Msel _
| Mscroll
->
2264 match Sys.os_type
with
2265 | "Win32" -> Sys.getenv
"HOMEPATH"
2266 | _
-> Sys.getenv
"HOME"
2269 ("Can not determine home directory location: " ^
2270 Printexc.to_string exn
);
2274 let config_of c attrs
=
2278 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2279 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2280 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2281 | "preload" -> { c with preload = bool_of_string v
}
2282 | "page-bias" -> { c with pagebias
= int_of_string v
}
2283 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2284 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2285 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2286 | "throttle" -> { c with showall
= bool_of_string v
}
2287 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2288 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2289 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2291 let zoom = float_of_string v
/. 100. in
2292 let zoom = max
0.01 (min
2.2 zoom) in
2293 { c with zoom = zoom }
2294 | "presentation" -> { c with presentation = bool_of_string v
}
2295 | "rotation-angle" -> { c with angle
= int_of_string v
}
2296 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2297 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2298 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2299 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2300 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2301 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2302 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2305 prerr_endline
("Error processing attribute (`" ^
2306 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2309 let rec fold c = function
2312 let c = apply c k v
in
2318 let bookmark_of attrs
=
2319 let rec fold title page
rely = function
2320 | ("title", v
) :: rest
-> fold v page
rely rest
2321 | ("page", v
) :: rest
-> fold title v
rely rest
2322 | ("rely", v
) :: rest
-> fold title page v rest
2323 | _
:: rest
-> fold title page
rely rest
2324 | [] -> title, page
, rely
2326 fold "invalid" "0" "0" attrs
2329 let setconf dst src
=
2330 dst
.scrollw
<- src
.scrollw
;
2331 dst
.scrollh
<- src
.scrollh
;
2332 dst
.icase
<- src
.icase
;
2333 dst
.preload <- src
.preload;
2334 dst
.pagebias
<- src
.pagebias
;
2335 dst
.verbose
<- src
.verbose
;
2336 dst
.scrollincr
<- src
.scrollincr
;
2337 dst
.maxhfit
<- src
.maxhfit
;
2338 dst
.crophack
<- src
.crophack
;
2339 dst
.autoscroll
<- src
.autoscroll
;
2340 dst
.showall
<- src
.showall
;
2341 dst
.hlinks
<- src
.hlinks
;
2342 dst
.underinfo
<- src
.underinfo
;
2343 dst
.interpagespace <- src
.interpagespace;
2344 dst
.zoom <- src
.zoom;
2345 dst
.presentation <- src
.presentation;
2346 dst
.angle
<- src
.angle
;
2347 dst
.winw
<- src
.winw
;
2348 dst
.winh
<- src
.winh
;
2349 dst
.savebmarks
<- src
.savebmarks
;
2350 dst
.memlimit
<- src
.memlimit
;
2351 dst
.proportional
<- src
.proportional
;
2352 dst
.texcount
<- src
.texcount
;
2353 dst
.sliceheight
<- src
.sliceheight
;
2357 let l = String.length
s in
2358 let b = Buffer.create
l in
2364 let h = Hashtbl.create
10 in
2365 let dc = { defconf with angle
= defconf.angle
} in
2366 let rec toplevel v t spos epos
=
2368 | Vdata
| Vcdata
| Vend
-> v
2369 | Vopen
("llppconfig", attrs
, closed
) ->
2372 else { v
with f = llppconfig
}
2374 error
"unexpected subelement at top level" s spos
2375 | Vclose tag
-> error
"unexpected close at top level" s spos
2377 and llppconfig v t spos epos
=
2379 | Vdata
| Vcdata
| Vend
-> v
2380 | Vopen
("defaults", attrs
, closed
) ->
2381 let c = config_of dc attrs
in
2385 else { v
with f = skip
"defaults" (fun () -> v
) }
2387 | Vopen
("doc", attrs
, closed
) ->
2390 List.assoc
"path" attrs
2391 with Not_found
-> error
"doc is missing path attribute" s spos
2393 let path = unent pathent in
2394 let c = config_of dc attrs
in
2397 float_of_string
(List.assoc
"rely" attrs
)
2401 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2406 int_of_string
(List.assoc
"pan" attrs
)
2410 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2414 then (Hashtbl.add
h path (c, [], x, y); v
)
2415 else { v
with f = doc
path x y c [] }
2417 | Vopen
(tag
, _
, closed
) ->
2418 error
"unexpected subelement in llppconfig" s spos
2420 | Vclose
"llppconfig" -> { v
with f = toplevel }
2421 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2423 and doc
path x y c bookmarks v t spos epos
=
2425 | Vdata
| Vcdata
-> v
2426 | Vend
-> error
"unexpected end of input in doc" s spos
2427 | Vopen
("bookmarks", attrs
, closed
) ->
2428 { v
with f = pbookmarks
path x y c bookmarks }
2430 | Vopen
(tag
, _
, _
) ->
2431 error
"unexpected subelement in doc" s spos
2434 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2435 { v
with f = llppconfig
}
2437 | Vclose tag
-> error
"unexpected close in doc" s spos
2439 and pbookmarks
path x y c bookmarks v t spos epos
=
2441 | Vdata
| Vcdata
-> v
2442 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2443 | Vopen
("item", attrs
, closed
) ->
2444 let titleent, spage
, srely
= bookmark_of attrs
in
2449 dolog "Failed to convert page %S to integer: %s"
2450 spage
(Printexc.to_string exn
);
2455 float_of_string srely
2457 dolog "Failed to convert rely %S to real: %s"
2458 srely
(Printexc.to_string exn
);
2461 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2463 then { v
with f = pbookmarks
path x y c bookmarks }
2466 { v
with f = skip
"item" f }
2469 error
"unexpected subelement in bookmarks" s spos
2471 | Vclose
"bookmarks" ->
2472 { v
with f = doc
path x y c bookmarks }
2474 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2476 and skip tag
f v t spos epos
=
2478 | Vdata
| Vcdata
-> v
2480 error
("unexpected end of input in skipped " ^ tag
) s spos
2481 | Vopen
(tag'
, _
, closed
) ->
2485 let f'
() = { v
with f = skip tag
f } in
2486 { v
with f = skip tag'
f'
}
2490 else error
("unexpected close in skipped " ^ tag
) s spos
2493 parse
{ f = toplevel; accu = () } s;
2499 let len = in_channel_length ic
in
2500 let s = String.create
len in
2501 really_input ic
s 0 len;
2504 | Parse_error
(msg
, s, pos
) ->
2505 let subs = subs s pos
in
2506 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2507 failwith
("parse error: " ^
s)
2510 failwith
("config load error: " ^
Printexc.to_string exn
)
2516 let dir = Filename.concat
home ".config" in
2517 if Sys.is_directory
dir then dir else home
2520 Filename.concat
dir "llpp.conf"
2524 if Sys.file_exists
path
2527 (try Some
(open_in_bin
path)
2530 ("Error opening configuation file `" ^
path ^
"': " ^
2531 Printexc.to_string exn
);
2540 ("Error loading configuation from `" ^
path ^
"': " ^
2541 Printexc.to_string exn
);
2547 f (Hashtbl.create
0, defconf)
2552 let pc, pb
, px
, py =
2554 Hashtbl.find
h state.path
2555 with Not_found
-> dc, [], 0, 0.0
2559 state.bookmarks <- pb
;
2561 cbput state.hists
.nav
py;
2566 let add_attrs bb always
dc c =
2569 then Printf.bprintf bb
"\n %s='%b'" s a
2572 then Printf.bprintf bb
"\n %s='%d'" s a
2575 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2579 then dc.winw
, dc.winh
2581 match state.fullscreen
with
2583 | None
-> c.winw
, c.winh
2585 let zoom, presentation, interpagespace =
2587 then dc.zoom, dc.presentation, dc.interpagespace
2589 match state.birdseye
with
2590 | Some
(zoom, _
, presentation, interpagespace) ->
2591 (zoom, presentation, interpagespace)
2592 | None
-> c.zoom, c.presentation, c.interpagespace
2594 oi
"width" w dc.winw
;
2595 oi
"height" h dc.winh
;
2596 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2597 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2598 ob "case-insensitive-search" c.icase
dc.icase
;
2599 ob "preload" c.preload dc.preload;
2600 oi
"page-bias" c.pagebias
dc.pagebias
;
2601 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2602 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2603 ob "crop-hack" c.crophack
dc.crophack
;
2604 ob "throttle" c.showall
dc.showall
;
2605 ob "highlight-links" c.hlinks
dc.hlinks
;
2606 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2607 oi
"vertical-margin" interpagespace dc.interpagespace;
2608 oz
"zoom" zoom dc.zoom;
2609 ob "presentation" presentation dc.presentation;
2610 oi
"rotation-angle" c.angle
dc.angle
;
2611 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2612 ob "proportional-display" c.proportional
dc.proportional
;
2613 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2614 oi
"texcount" c.texcount
dc.texcount
;
2615 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2619 let bb = Buffer.create
32768 in
2621 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2622 add_attrs bb true dc dc;
2623 Buffer.add_string
bb "/>\n";
2625 let adddoc path x y c bookmarks =
2626 if bookmarks == [] && c = dc && y = 0.0
2629 Printf.bprintf
bb "<doc path='%s'"
2630 (enent
path 0 (String.length
path));
2633 then Printf.bprintf
bb " rely='%f'" y;
2636 then Printf.bprintf
bb " pan='%d'" x;
2638 add_attrs bb false dc c;
2640 begin match bookmarks with
2641 | [] -> Buffer.add_string
bb "/>\n"
2643 Buffer.add_string
bb ">\n<bookmarks>\n";
2644 List.iter
(fun (title, _level
, page, rely) ->
2646 "<item title='%s' page='%d' rely='%f'/>\n"
2647 (enent
title 0 (String.length
title))
2651 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2657 match state.birdseye
with
2658 | Some
(_
, x, _
, _
) -> x
2661 adddoc state.path x (yratio state.y) conf
2662 (if conf.savebmarks
then state.bookmarks else []);
2664 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2665 if path <> state.path
2667 adddoc path x y c bookmarks
2669 Buffer.add_string
bb "</llppconfig>";
2672 if Buffer.length
bb > 0
2675 let tmp = path ^
".tmp" in
2676 let oc = open_out_bin
tmp in
2677 Buffer.output_buffer
oc bb;
2679 Sys.rename
tmp path;
2682 ("error while saving configuration: " ^
Printexc.to_string exn
)
2688 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2689 (fun s -> state.path <- s)
2690 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2693 if String.length
state.path = 0
2694 then (prerr_endline
"filename missing"; exit
1)
2696 if Filename.is_relative
state.path
2697 then Filename.concat
(Sys.getcwd
()) state.path
2705 let _ = Glut.init
Sys.argv
in
2706 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2707 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2708 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2711 if Sys.os_type
= "Unix"
2713 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2715 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2716 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2717 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2718 Unix.bind
sock addr;
2720 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2721 Unix.connect
csock addr;
2722 let ssock, _ = Unix.accept
sock in
2725 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2726 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2730 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2734 let () = Glut.displayFunc
display in
2735 let () = Glut.reshapeFunc
reshape in
2736 let () = Glut.keyboardFunc
keyboard in
2737 let () = Glut.specialFunc
special in
2738 let () = Glut.idleFunc
(Some
idle) in
2739 let () = Glut.mouseFunc
mouse in
2740 let () = Glut.motionFunc
motion in
2741 let () = Glut.passiveMotionFunc
pmotion in
2743 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2744 state.csock <- csock;
2745 state.ssock <- ssock;
2746 state.text <- "Opening " ^
path;
2747 writeopen state.path state.password
;
2751 let rec handlelablglutbug () =
2754 with Glut.BadEnum
"key in special_of_int" ->
2755 showtext '
!'
" LablGlut bug: special key not recognized";
2756 handlelablglutbug ()
2758 handlelablglutbug ();