4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 type params
= angle
* proportional
* texcount
* sliceheight
20 and proportional
= bool
21 and interpagespace
= int
28 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
29 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext
: string -> (int * int * int * int) -> int -> unit =
32 external copysel
: string -> unit = "ml_copysel";;
33 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
34 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
35 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
39 | Msel
of (mpos
* mpos
)
53 type textentry
= (char
* string * onhist
* onkey
* ondone
)
54 and onkey
= string -> int -> te
55 and ondone
= string -> unit
56 and histcancel
= unit -> unit
57 and onhist
= ((histcmd
-> string) * histcancel
) option
58 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
63 | TEswitch
of textentry
67 { store
= Array.create n v
74 let cbcap b
= Array.length b
.store
;;
79 b
.wc
<- (b
.wc
+ 1) mod cap;
81 b
.len
<- min
(b
.len
+ 1) cap;
84 let cbempty b
= b
.len
= 0;;
86 let cbgetg b circular dir
=
90 let rc = b
.rc + dir
in
102 else max
0 (min
rc (b
.len
-1))
108 let cbget b
= cbgetg b
false;;
109 let cbgetc b
= cbgetg b
true;;
112 let rc = b
.wc
- b
.len
in
113 let rc = if rc < 0 then cbcap b
+ rc else rc in
117 let cbdecr b
= b
.len
<- b
.len
- 1;;
132 { mutable scrollw
: int
133 ; mutable scrollh
: int
134 ; mutable icase
: bool
135 ; mutable preload
: bool
136 ; mutable pagebias
: int
137 ; mutable verbose
: bool
138 ; mutable scrollincr
: int
139 ; mutable maxhfit
: bool
140 ; mutable crophack
: bool
141 ; mutable autoscroll
: bool
142 ; mutable showall
: bool
143 ; mutable hlinks
: bool
144 ; mutable underinfo
: bool
145 ; mutable interpagespace
: interpagespace
146 ; mutable zoom
: float
147 ; mutable presentation
: bool
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
156 ; mutable thumbw
: width
160 type outline
= string * int * int * float;;
162 | Oarray
of outline array
163 | Olist
of outline list
164 | Onarrow
of string * outline array
* outline array
167 type rect
= (float * float * float * float * float * float * float * float);;
169 type pagemapkey
= (pageno
* width
* angle
* proportional
* gen
);;
171 type anchor
= pageno
* top
;;
174 { mutable csock
: Unix.file_descr
175 ; mutable ssock
: Unix.file_descr
179 ; mutable anchor
: anchor
181 ; mutable layout
: layout list
182 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
183 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
184 ; mutable pagecount
: int
185 ; pagecache
: string circbuf
186 ; mutable rendering
: bool
187 ; mutable mstate
: mstate
188 ; mutable searchpattern
: string
189 ; mutable rects
: (pageno
* recttype
* rect
) list
190 ; mutable rects1
: (pageno
* recttype
* rect
) list
191 ; mutable text
: string
192 ; mutable fullscreen
: (width
* height
) option
193 ; mutable birdseye
: (conf
* leftx
* pageno
* pageno
) option
194 ; mutable textentry
: textentry
option
195 ; mutable outlines
: outlines
196 ; mutable outline
: (bool * int * int * outline array
* string) option
197 ; mutable bookmarks
: outline list
198 ; mutable path
: string
199 ; mutable password
: string
200 ; mutable invalidated
: int
201 ; mutable colorscale
: float
202 ; mutable memused
: int
204 ; mutable throttle
: layout list
option
208 { pat
: string circbuf
209 ; pag
: string circbuf
210 ; nav
: anchor circbuf
230 ; presentation
= false
235 ; proportional
= true
236 ; memlimit
= 32*1024*1024
243 let conf = { defconf with angle
= defconf.angle
};;
254 ; pagemap
= Hashtbl.create
10
255 ; pagecache
= cbnew 100 ""
267 ; outlines
= Olist
[]
274 { nav
= cbnew 100 (0, 0.0)
288 Printf.kprintf prerr_endline fmt
290 Printf.kprintf ignore fmt
294 let len = String.length s
in
296 let b = Buffer.create
n in
297 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
298 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
299 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
300 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
301 Buffer.add_string
b s
;
302 let s'
= Buffer.contents
b in
303 let n'
= Unix.write fd
s'
0 n in
304 if n'
!= n then failwith
"write failed";
309 let n = Unix.read fd
s 0 4 in
310 if n != 4 then failwith
"incomplete read(len)";
312 lor (Char.code
s.[0] lsl 24)
313 lor (Char.code
s.[1] lsl 16)
314 lor (Char.code
s.[2] lsl 8)
315 lor (Char.code
s.[3] lsl 0)
317 let s = String.create
len in
318 let n = Unix.read fd
s 0 len in
319 if n != len then failwith
"incomplete read(data)";
324 let b = Buffer.create
10 in
325 Buffer.add_string
b s;
326 let rec combine = function
329 Buffer.add_char
b ' '
;
332 | `
b b -> if b then "1" else "0"
334 | `i i
-> string_of_int i
335 | `f f
-> string_of_float f
336 | `I f
-> string_of_int
(truncate f
)
338 Buffer.add_string
b s;
345 let cmd = Buffer.contents
(makecmd s l
) in
346 writecmd state.csock
cmd;
352 let d = conf.winh
- h
in
359 let rec f pn ph pi fh l
=
361 | (n, _
, h
, _
) :: rest
->
362 let ips = calcips h
in
367 if state.birdseye
<> None
&& pn
= 0
372 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
377 if conf.presentation
|| (state.birdseye
<> None
&& pn
= 0)
381 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
384 let fh = f 0 0 0 0 state.pdims
in
388 let getpageyh pageno
=
389 let rec f pn ph pi y l
=
391 | (n, _
, h
, _
) :: rest
->
392 let ips = calcips h
in
395 if conf.presentation
&& n = pageno
397 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
399 y
+ (pageno
- pn
) * (ph
+ pi
), h
401 let y = y + (if conf.presentation
then pi
else 0) in
402 let y = y + (n - pn
) * (ph
+ pi
) in
406 y + (pageno
- pn
) * (ph
+ pi
), ph
408 f 0 0 0 0 state.pdims
411 let getpagey pageno
= fst
(getpageyh pageno
);;
414 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
415 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
417 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
418 let ips = calcips h
in
420 if conf.presentation
|| (state.birdseye
<> None
&& pageno
= 0)
424 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
426 prev
, pdims
, pdimno
, 0
428 let dy = dy + yinc in
429 let py = py + yinc in
430 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
435 if py + h
<= vy - yinc
437 let py = py + h
+ ips in
438 let dy = max
0 (py - y) in
448 let pagey = vy - py in
449 let pagevh = h
- pagey in
450 let pagevh = min
(sh
- dy) pagevh in
451 let off = if yinc > 0 then py - vy else 0 in
452 let py = py + h
+ ips in
458 ; pagedispy
= dy + off
459 ; pagey = pagey + off
460 ; pagevh = pagevh - off
464 let accu = e :: accu in
471 ~cacheleft
:(cacheleft
-1)
474 if state.invalidated
= 0
484 ~cacheleft
:(cbcap state.pagecache
)
494 let y = state.y + incr
in
496 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
500 let getopaque pageno
=
501 try Some
(Hashtbl.find
state.pagemap
502 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
503 with Not_found
-> None
506 let cache pageno opaque
=
507 Hashtbl.replace
state.pagemap
508 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
511 let validopaque opaque
= String.length opaque
> 0;;
514 match getopaque l
.pageno
with
515 | None
when not
state.rendering
->
516 state.rendering
<- true;
517 cache l
.pageno
("", -1);
518 wcmd "render" [`i
(l
.pageno
+ 1)
526 let loadlayout layout =
527 let rec f all
= function
529 begin match getopaque l
.pageno
with
530 | None
-> render l
; f false ls
531 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
535 f (layout <> []) layout;
538 let findpageforopaque opaque
=
540 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
544 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
550 let memleft = conf.memlimit
- state.memused
in
553 let opaque = cbpeek state.pagecache
in
554 match findpageforopaque opaque with
555 | Some
((n, _
, _
, _
, _
), size
) ->
556 memleft + size
>= 0 && not
(pagevisible state.layout n)
563 let presentation = conf.presentation in
564 let interpagespace = conf.interpagespace in
565 let maxy = state.maxy in
566 conf.presentation <- false;
567 conf.interpagespace <- 0;
568 state.maxy <- calcheight ();
570 match state.layout with
572 | l
:: _
-> getpagey l
.pageno
574 let y = if y < conf.winh
then 0 else y - conf.winh
in
575 let pages = layout y (conf.winh
*3) in
576 List.iter
render pages;
577 conf.presentation <- presentation;
578 conf.interpagespace <- interpagespace;
584 let y = min
state.maxy y in
585 let pages = layout y conf.winh
in
586 let ready = loadlayout pages in
592 state.layout <- pages;
593 state.throttle
<- None
;
594 Glut.postRedisplay
();
597 state.throttle
<- Some
pages;
602 state.layout <- pages;
603 state.throttle
<- None
;
604 Glut.postRedisplay
();
606 begin match state.birdseye
with
607 | Some
(conf, leftx
, pageno
, hooverpageno
) ->
608 if not
(pagevisible pages pageno
)
610 match state.layout with
613 state.birdseye
<- Some
(conf, leftx
, l
.pageno
, hooverpageno
)
620 let gotoy_and_clear_text y =
622 if not
conf.verbose
then state.text
<- "";
625 let emptyanchor = (0, 0.0);;
628 match state.layout with
630 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
633 let getanchory (n, top
) =
634 let y, h
= getpageyh n in
635 y + (truncate
(top
*. float h
));
638 let gotoanchor anchor
=
639 gotoy (getanchory anchor
);
643 cbput state.hists
.nav
(getanchor ());
647 let anchor = cbgetc state.hists
.nav ~
-1 in
652 let y, h
= getpageyh n in
654 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
657 let gotopage1 n top
=
658 let y = getpagey n in
660 gotoy_and_clear_text (y + top
);
668 state.invalidated
<- state.invalidated
+ 1;
672 let c = c *. state.colorscale
in
677 state.maxy <- calcheight ();
678 gotoanchor state.anchor;
682 GlMat.mode `projection
;
683 GlMat.load_identity
();
684 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
685 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
686 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
690 GlMat.mode `projection
;
691 GlMat.load_identity
();
692 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
693 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
694 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
698 if state.invalidated
= 0
699 then state.anchor <- getanchor ();
702 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
706 GlMat.mode `modelview
;
707 GlMat.load_identity
();
708 GlClear.color
(scalecolor 1.0);
709 GlClear.clear
[`color
];
712 wcmd "geometry" [`i
w; `i h
];
716 GlDraw.color
(0.0, 0.0, 0.0);
718 (0.0, float (conf.winh
- 18))
719 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
721 let font = Glut.BITMAP_8_BY_13
in
722 GlDraw.color
(1.0, 1.0, 1.0);
723 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
724 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
725 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
729 let len = String.length
state.text
in
730 match state.textentry
with
732 if len > 0 then showtext ' '
state.text
734 | Some
(c, text
, _
, _
, _
) ->
738 text ^
" [" ^
state.text ^
"]"
748 state.text
<- Printf.sprintf
"%c%s" c s;
749 Glut.postRedisplay
();
763 state.rects
<- state.rects1
;
764 Glut.postRedisplay
()
767 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
768 state.pagecount
<- n;
769 state.invalidated
<- state.invalidated
- 1;
770 if state.invalidated
= 0
774 let s = Scanf.sscanf
cmd "t %n"
775 (fun n -> String.sub
cmd n (String.length
cmd - n))
777 Glut.setWindowTitle
s
780 let s = Scanf.sscanf
cmd "T %n"
781 (fun n -> String.sub
cmd n (String.length
cmd - n))
783 if state.textentry
= None
790 Glut.postRedisplay
();
796 let s = Scanf.sscanf
cmd "V %n"
797 (fun n -> String.sub
cmd n (String.length
cmd - n))
803 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
804 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
805 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
806 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
808 let y = (getpagey pageno) + truncate y0
in
811 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
814 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
815 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
816 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
817 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
820 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
823 let n, w, h
, r
, l
, s, p
=
824 Scanf.sscanf
cmd "r %u %u %u %u %d %u %s"
825 (fun n w h r l
s p
->
826 (n-1, w, h
, r
, l
!= 0, s, p
))
829 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
830 state.memused
<- state.memused
+ s;
833 match state.throttle
with
834 | None
-> state.layout
835 | Some
layout -> layout
839 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
842 let evictedopaque = cbpeek state.pagecache
in
843 match findpageforopaque evictedopaque with
844 | None
-> failwith
"bug in gc"
845 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
846 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
848 wcmd "free" [`
s evictedopaque];
849 state.memused
<- state.memused
- evictedsize
;
850 Hashtbl.remove
state.pagemap k
;
851 cbdecr state.pagecache
;
858 cbput state.pagecache p
;
859 state.rendering
<- false;
861 begin match state.throttle
with
863 if pagevisible state.layout n
866 let allvisible = loadlayout state.layout in
867 if allvisible then preload ();
874 let y = getpagey l
.pageno + l
.pagey in
879 let (n, w, h
, x
) as pdim
=
880 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h
, x
)
882 state.pdims
<- pdim
:: state.pdims
885 let (l
, n, t
, h
, pos
) =
886 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
888 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
890 let l = String.length
s in
891 let b = Buffer.create
(String.length
s) in
898 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
901 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
910 let outline = (s, l, n, float t
/. float h
) in
912 match state.outlines with
913 | Olist
outlines -> Olist
(outline :: outlines)
914 | Oarray _
-> Olist
[outline]
915 | Onarrow _
-> Olist
[outline]
917 state.outlines <- outlines
920 log "unknown cmd `%S'" cmd
923 let now = Unix.gettimeofday
;;
927 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
932 let y = state.y + conf.scrollincr
in
933 let y = if y >= state.maxy then 0 else y in
939 let cmd = readcmd state.csock
in
948 let action = function
949 | HCprev
-> cbget cb ~
-1
950 | HCnext
-> cbget cb
1
951 | HCfirst
-> cbget cb ~
-(cb
.rc)
952 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
953 and cancel
() = cb
.rc <- rc
957 let search pattern forward
=
958 if String.length pattern
> 0
961 match state.layout with
964 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
967 let b = makecmd "search"
968 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
970 Buffer.add_char
b '
,'
;
971 Buffer.add_string
b pattern
;
972 Buffer.add_char
b '
\000'
;
975 writecmd state.csock
cmd;
978 let intentry text key
=
979 let c = Char.unsafe_chr key
in
982 let s = "x" in s.[0] <- c;
983 let text = text ^
s in
987 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
992 let b = Buffer.create
(String.length
s + 1) in
993 Buffer.add_string
b s;
998 let textentry text key
=
999 let c = Char.unsafe_chr key
in
1001 | _
when key
>= 32 && key
< 127 ->
1002 let text = addchar text c in
1006 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1010 let reinit angle proportional
=
1011 conf.angle
<- angle
;
1012 conf.proportional
<- proportional
;
1014 wcmd "reinit" [`i angle
; `
b proportional
];
1017 let optentry text key
=
1018 let btos b = if b then "on" else "off" in
1019 let c = Char.unsafe_chr key
in
1023 try conf.scrollincr
<- int_of_string
s with exc
->
1024 state.text <- Printf.sprintf
"bad integer `%s': %s"
1025 s (Printexc.to_string exc
)
1027 TEswitch
('#'
, "", None
, intentry, ondone)
1032 Some
(int_of_string
s)
1034 state.text <- Printf.sprintf
"bad integer `%s': %s"
1035 s (Printexc.to_string exc
);
1038 | Some angle
-> reinit angle
conf.proportional
1041 TEswitch
('^'
, "", None
, intentry, ondone)
1044 conf.icase
<- not
conf.icase
;
1045 TEdone
("case insensitive search " ^
(btos conf.icase
))
1048 conf.preload <- not
conf.preload;
1050 TEdone
("preload " ^
(btos conf.preload))
1053 conf.verbose
<- not
conf.verbose
;
1054 TEdone
("verbose " ^
(btos conf.verbose
))
1057 conf.maxhfit
<- not
conf.maxhfit
;
1058 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1059 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1062 conf.crophack
<- not
conf.crophack
;
1063 TEdone
("crophack " ^
btos conf.crophack
)
1066 conf.showall
<- not
conf.showall
;
1067 TEdone
("showall " ^
btos conf.showall
)
1070 conf.underinfo
<- not
conf.underinfo
;
1071 TEdone
("underinfo " ^
btos conf.underinfo
)
1074 conf.savebmarks
<- not
conf.savebmarks
;
1075 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1081 match state.layout with
1086 conf.interpagespace <- int_of_string
s;
1087 state.maxy <- calcheight ();
1088 let y = getpagey pageno in
1091 state.text <- Printf.sprintf
"bad integer `%s': %s"
1092 s (Printexc.to_string exc
)
1094 TEswitch
('
%'
, "", None
, intentry, ondone)
1097 reinit conf.angle
(not
conf.proportional
);
1098 TEdone
("proprortional display " ^
btos conf.proportional
)
1101 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1105 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1107 let enterselector allowdel
outlines errmsg msg
=
1108 if Array.length
outlines = 0
1110 showtext ' ' errmsg
;
1114 Glut.setCursor
Glut.CURSOR_INHERIT
;
1116 match state.layout with
1118 | {pageno=pageno} :: rest
-> pageno
1122 if n = Array.length
outlines
1125 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1126 if outlinepageno
>= pageno then n else loop (n+1)
1131 Some
(allowdel
, active,
1132 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1133 Glut.postRedisplay
();
1137 let enteroutlinemode () =
1139 match state.outlines with
1142 let a = Array.of_list
(List.rev
l) in
1143 state.outlines <- Oarray
a;
1145 | Onarrow
(pat
, a, b) ->
1146 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1148 enterselector false outlines "Document has no outline" msg
;
1151 let enterbookmarkmode () =
1152 let bookmarks = Array.of_list
state.bookmarks in
1153 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1156 let quickbookmark ?title
() =
1157 match state.layout with
1163 let sec = Unix.gettimeofday
() in
1164 let tm = Unix.localtime
sec in
1165 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1169 (tm.Unix.tm_year
+ 1900)
1172 | Some
title -> title
1175 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1179 state.fullscreen
<- None
;
1180 Glut.reshapeWindow
w h
;
1183 let writeopen path password
=
1184 writecmd state.csock
("open " ^ path ^
"\000" ^
state.password ^
"\000");
1187 let opendoc path password
=
1190 state.password
<- password
;
1191 state.gen
<- state.gen
+ 1;
1193 writeopen path password
;
1194 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1195 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1199 let zoom = float conf.thumbw
/. float conf.winw
in
1200 let birdseyepageno =
1201 match state.layout with
1203 | l :: _
-> l.pageno
1206 Some
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1);
1208 conf.presentation <- false;
1209 conf.interpagespace <- 10;
1210 conf.hlinks
<- false;
1212 state.mstate
<- Mnone
;
1213 conf.showall
<- false;
1214 Glut.setCursor
Glut.CURSOR_INHERIT
;
1217 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1224 let birdseyeoff (c, leftx
, pageno, _
) =
1225 state.birdseye
<- None
;
1226 conf.zoom <- c.zoom;
1227 conf.presentation <- c.presentation;
1228 conf.interpagespace <- c.interpagespace;
1229 conf.showall
<- c.showall
;
1230 conf.hlinks
<- c.hlinks
;
1234 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1239 let togglebirdseye () =
1240 match state.birdseye
with
1241 | None
-> birdseyeon ()
1242 | Some vals
-> birdseyeoff vals
1245 let viewkeyboard ~key ~x ~
y =
1247 state.textentry <- te
;
1250 Glut.postRedisplay
()
1252 match state.textentry with
1254 let c = Char.chr key
in
1260 let y = getnav () in
1261 gotoy_and_clear_text y
1264 begin match state.birdseye
with
1266 | Some
((_
, _
, pageno, _
) as vals
) ->
1268 reshape conf.winw
conf.winh
;
1269 state.anchor <- (pageno, 0.0);
1278 Glut.postRedisplay
()
1281 let ondone isforw
s =
1282 cbput state.hists
.pat
s;
1283 state.searchpattern
<- s;
1286 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1287 textentry, ondone (c ='
/'
)))
1289 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1290 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1291 conf.zoom <- min
2.2 (conf.zoom +. incr);
1292 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1293 reshape conf.winw
conf.winh
1298 try int_of_string
s with exc
->
1299 state.text <- Printf.sprintf
"bad integer `%s': %s"
1300 s (Printexc.to_string exc
);
1306 state.text <- "page bias is now " ^ string_of_int
n;
1309 enttext (Some
('
+'
, "", None
, intentry, ondone))
1311 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1312 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1313 conf.zoom <- max
0.01 (conf.zoom -. decr);
1314 if conf.zoom <= 1.0 then state.x
<- 0;
1315 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1316 reshape conf.winw
conf.winh
;
1322 enttext (Some
('
-'
, "", None
, optentry, ondone))
1324 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1327 state.text <- "zoom is 100%";
1328 reshape conf.winw
conf.winh
1330 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1331 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1336 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1337 reshape conf.winw
conf.winh
;
1340 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1342 reshape conf.winw
conf.winh
;
1347 try int_of_string
s with exc
->
1348 state.text <- Printf.sprintf
"bad integer `%s': %s"
1349 s (Printexc.to_string exc
);
1355 cbput state.hists
.pag
(string_of_int
n);
1356 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1359 let pageentry text key
=
1360 match Char.unsafe_chr key
with
1361 | 'g'
-> TEdone
text
1362 | _
-> intentry text key
1364 let text = "x" in text.[0] <- c;
1365 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1369 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1370 reshape conf.winw
conf.winh
;
1373 conf.hlinks
<- not
conf.hlinks
;
1374 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1375 Glut.postRedisplay
()
1378 conf.autoscroll
<- not
conf.autoscroll
1381 conf.presentation <- not
conf.presentation;
1382 showtext ' '
("presentation mode " ^
1383 if conf.presentation then "on" else "off");
1387 begin match state.fullscreen
with
1389 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1392 state.fullscreen
<- None
;
1397 gotoy_and_clear_text 0
1400 search state.searchpattern
true
1403 search state.searchpattern
false
1406 begin match state.layout with
1409 gotoy_and_clear_text (getpagey l.pageno)
1413 begin match List.rev
state.layout with
1416 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1417 gotoy_and_clear_text (getpagey pageno)
1421 begin match state.layout with
1424 let pageno = max
0 (l.pageno-1) in
1425 gotoy_and_clear_text (getpagey pageno)
1430 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1432 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1434 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1438 else (100. *. (float state.y /. float maxy)) in
1441 Printf.sprintf
"Page %d of %d %.2f%%"
1442 (fn+1) state.pagecount
percent
1445 "Pages %d-%d of %d %.2f%%"
1446 (fn+1) (ln
+1) state.pagecount
percent
1451 begin match state.layout with
1454 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1455 Glut.postRedisplay
();
1459 enterbookmarkmode ()
1463 match state.layout with
1466 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1470 enttext (Some
('~'
, "", None
, textentry, ondone))
1474 showtext ' '
"Quick bookmark added";
1477 begin match state.layout with
1479 let rect = getpdimrect
l.pagedimno
in
1483 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1484 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1486 (truncate
(rect.(1) -. rect.(0)),
1487 truncate
(rect.(3) -. rect.(0)))
1491 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1493 Glut.postRedisplay
();
1499 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1504 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1505 Glut.postRedisplay
()
1507 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1508 | 'j'
-> gotoy (clamp conf.scrollincr
)
1510 | '
r'
-> opendoc state.path
state.password
1513 vlog "huh? %d %c" key
(Char.chr key
);
1516 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1517 let len = String.length
text in
1520 state.textentry <- None
;
1521 Glut.postRedisplay
();
1524 let s = String.sub
text 0 (len - 1) in
1525 enttext (Some
(c, s, opthist
, onkey
, ondone))
1528 | Some
(c, text, onhist, onkey
, ondone) ->
1529 begin match Char.unsafe_chr key
with
1532 state.textentry <- None
;
1533 Glut.postRedisplay
()
1536 begin match onhist with
1538 | Some
(_
, onhistcancel
) -> onhistcancel
()
1540 state.textentry <- None
;
1541 Glut.postRedisplay
()
1544 begin match onkey
text key
with
1546 state.textentry <- None
;
1548 Glut.postRedisplay
()
1551 enttext (Some
(c, text, onhist, onkey
, ondone));
1554 state.textentry <- None
;
1555 Glut.postRedisplay
()
1558 state.textentry <- Some te
;
1559 Glut.postRedisplay
()
1564 let narrow outlines pattern
=
1565 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1569 let rec fold accu n =
1573 let (s, _
, _
, _
) as o
= outlines.(n) in
1575 if (try ignore
(Str.search_forward re
s 0); true
1576 with Not_found
-> false)
1582 let matched = fold [] (Array.length
outlines - 1) in
1583 if matched = [] then None
else Some
(Array.of_list
matched)
1586 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1587 let search active pattern
incr =
1590 if n = Array.length
outlines || n = -1
1593 let (s, _
, _
, _
) = outlines.(n) in
1595 (try ignore
(Str.search_forward re
s 0); true
1596 with Not_found
-> false)
1598 else loop (n + incr)
1603 let re = Str.regexp_case_fold pattern
in
1609 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1612 if String.length qsearch
= 0
1615 state.outline <- None
;
1616 Glut.postRedisplay
();
1620 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1621 Glut.postRedisplay
();
1625 let incr = if key
= 18 then -1 else 1 in
1627 match search (active + incr) qsearch
incr with
1629 state.text <- qsearch ^
" [not found]";
1632 state.text <- qsearch
;
1633 active, firstof active
1635 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1636 Glut.postRedisplay
();
1639 let len = String.length qsearch
in
1646 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1649 let qsearch = String.sub
qsearch 0 (len - 1) in
1651 match search active qsearch ~
-1 with
1653 state.text <- qsearch ^
" [not found]";
1656 state.text <- qsearch;
1657 active, firstof active
1659 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1661 Glut.postRedisplay
()
1664 if active < Array.length
outlines
1666 let (_
, _
, n, t
) = outlines.(active) in
1670 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1671 state.outline <- None
;
1672 Glut.postRedisplay
();
1674 | _
when key
>= 32 && key
< 127 ->
1675 let pattern = addchar qsearch (Char.chr key
) in
1677 match search active pattern 1 with
1679 state.text <- pattern ^
" [not found]";
1682 state.text <- pattern;
1683 active, firstof active
1685 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1686 Glut.postRedisplay
()
1688 | 14 when not allowdel
-> (* ctrl-n *)
1689 if String.length
qsearch > 0
1691 let optoutlines = narrow outlines qsearch in
1692 begin match optoutlines with
1693 | None
-> state.text <- "can't narrow"
1695 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1696 match state.outlines with
1699 state.outlines <- Onarrow
(qsearch, outlines, a)
1700 | Onarrow
(pat
, a, b) ->
1701 state.outlines <- Onarrow
(qsearch, outlines, b)
1704 Glut.postRedisplay
()
1706 | 21 when not allowdel
-> (* ctrl-u *)
1708 match state.outlines with
1711 let a = Array.of_list
(List.rev
l) in
1712 state.outlines <- Oarray
a;
1714 | Onarrow
(pat
, a, b) ->
1715 state.outlines <- Oarray
b;
1719 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1720 Glut.postRedisplay
()
1724 Some
(allowdel
, active, firstof active, outlines, qsearch);
1725 Glut.postRedisplay
()
1727 | 127 when allowdel
->
1728 let len = Array.length
outlines - 1 in
1731 state.outline <- None
;
1732 state.bookmarks <- [];
1735 let bookmarks = Array.init
len
1737 let i = if i >= active then i + 1 else i in
1748 Glut.postRedisplay
()
1750 | _
-> log "unknown key %d" key
1753 let keyboard ~key ~x ~
y =
1758 match state.outline with
1759 | None
-> viewkeyboard ~key ~x ~
y
1760 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1763 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1766 let pageno = max
0 (pageno - 1) in
1768 let rec loop = function
1771 if l.pageno = pageno
1778 then gotopage1 pageno 0
1779 else Glut.postRedisplay
();
1780 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
)
1783 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1784 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1785 let rec loop = function
1786 | [] -> gotopage1 pageno 0
1788 if l.pageno = pageno
1790 if l.pagevh != l.pageh
1791 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1792 else Glut.postRedisplay
()
1798 | Glut.KEY_PAGE_UP
->
1799 begin match state.layout with
1801 if l.pageno = pageno
1803 match layout (state.y - conf.winh
) conf.winh
with
1804 | [] -> gotoy (clamp (-conf.winh
))
1806 let pageno = max
0 (l.pageno - 1) in
1807 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1811 let pageno = max
0 (l.pageno - 1) in
1812 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1815 | [] -> gotoy (clamp (-conf.winh
))
1817 | Glut.KEY_PAGE_DOWN
->
1818 begin match List.rev
state.layout with
1820 let pageno = min
l.pageno (state.pagecount
- 1) in
1821 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1822 gotoy (clamp (l.pagedispy
+ l.pageh
))
1823 | [] -> gotoy (clamp conf.winh
)
1827 state.birdseye
<- Some
(conf, leftx
, 0, hooverpageno
);
1830 let pageno = state.pagecount
- 1 in
1831 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1832 if not
(pagevisible state.layout pageno)
1835 match List.rev
state.pdims
with
1837 | (_
, _
, h, _
) :: _
-> h
1839 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1840 else Glut.postRedisplay
();
1844 let special ~key ~x ~
y =
1845 match state.outline, state.birdseye
with
1846 | None
, _
when key
= Glut.KEY_F9
->
1848 reshape conf.winw
conf.winh
;
1850 | None
, (Some vals
) ->
1851 birdseyespecial key x
y vals
1854 begin match state.textentry with
1858 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1859 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1860 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1861 | Glut.KEY_PAGE_UP
->
1862 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1864 match state.layout with
1866 | l :: _
-> state.y - l.pagey
1869 | Glut.KEY_PAGE_DOWN
->
1870 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1872 match List.rev
state.layout with
1874 | l :: _
-> getpagey l.pageno
1877 | Glut.KEY_HOME
-> addnav (); 0
1880 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1882 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1883 state.x
<- state.x
- 10;
1885 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1886 state.x
<- state.x
+ 10;
1891 gotoy_and_clear_text y
1893 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1896 | Glut.KEY_UP
-> action HCprev
1897 | Glut.KEY_DOWN
-> action HCnext
1898 | Glut.KEY_HOME
-> action HCfirst
1899 | Glut.KEY_END
-> action HClast
1902 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1903 Glut.postRedisplay
()
1908 | Some
(allowdel
, active, first
, outlines, qsearch), _
->
1909 let maxrows = maxoutlinerows () in
1910 let calcfirst first
active =
1913 let rows = active - first
in
1914 if rows > maxrows then active - maxrows else first
1918 let active = active + incr in
1919 let active = max
0 (min
active (Array.length
outlines - 1)) in
1920 let first = calcfirst first active in
1921 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1922 Glut.postRedisplay
()
1924 let updownlevel incr =
1925 let len = Array.length
outlines in
1926 let (_
, curlevel
, _
, _
) = outlines.(active) in
1928 if i = len then i-1 else if i = -1 then 0 else
1929 let (_
, l, _
, _
) = outlines.(i) in
1930 if l != curlevel
then i else flow (i+incr)
1932 let active = flow active in
1933 let first = calcfirst first active in
1934 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1935 Glut.postRedisplay
()
1938 | Glut.KEY_UP
-> navigate ~
-1
1939 | Glut.KEY_DOWN
-> navigate 1
1940 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1941 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1943 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1944 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1947 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1948 Glut.postRedisplay
()
1951 let active = Array.length
outlines - 1 in
1952 let first = max
0 (active - maxrows) in
1953 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1954 Glut.postRedisplay
()
1959 let drawplaceholder l =
1960 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1962 (float l.pagex
, float l.pagedispy
)
1963 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1965 let x = float (if margin < 0 then -margin else l.pagex
)
1966 and y = float (l.pagedispy
+ 13) in
1967 let font = Glut.BITMAP_8_BY_13
in
1968 GlDraw.color
(0.0, 0.0, 0.0);
1969 GlPix.raster_pos ~
x ~
y ();
1970 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1971 ("Loading " ^ string_of_int
(l.pageno + 1));
1974 let now () = Unix.gettimeofday
();;
1977 if state.textentry = None
1979 match state.birdseye
with
1980 | None
-> GlDraw.color
(scalecolor 1.0);
1981 | Some
(_
, _
, pageno, hooverpageno
) ->
1983 if l.pageno = pageno
1986 if l.pageno = hooverpageno
1991 GlDraw.color (scalecolor color);
1993 else GlDraw.color (scalecolor 0.4);
1994 begin match getopaque l.pageno with
1995 | Some
(opaque, _
) when validopaque opaque ->
1997 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2001 vlog "draw %d %f sec" l.pageno d;
2009 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2010 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2011 let sh = float conf.winh
/. sh in
2012 let sh = max
sh (float conf.scrollh
) in
2015 if state.y = state.maxy
2017 else float y /. float maxy
2019 let position = (float conf.winh
-. sh) *. percent in
2022 if position +. sh > float conf.winh
2023 then float conf.winh
-. sh
2029 let scrollindicator () =
2030 GlDraw.color (0.64 , 0.64, 0.64);
2032 (float (conf.winw
- conf.scrollw
), 0.)
2033 (float conf.winw
, float conf.winh
)
2035 GlDraw.color (0.0, 0.0, 0.0);
2037 let position, sh = scrollph state.y in
2039 (float (conf.winw
- conf.scrollw
), position)
2040 (float conf.winw
, position +. sh)
2044 let showsel margin =
2045 match state.mstate
with
2046 | Mnone
| Mscroll _
| Mpan _
->
2049 | Msel
((x0
, y0
), (x1
, y1
)) ->
2050 let rec loop = function
2052 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2053 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2055 match getopaque l.pageno with
2056 | Some
(opaque, _
) when validopaque opaque ->
2057 let oy = -l.pagey + l.pagedispy
in
2059 (x0
- margin - state.x, y0
,
2060 x1
- margin - state.x, y1
) oy;
2070 let panx = float state.x in
2072 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2073 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2075 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2077 if l.pageno = pageno
2079 let d = float (l.pagedispy
- l.pagey) in
2080 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2081 GlDraw.begins `quads
;
2083 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2084 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2085 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2086 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2096 let showoutline = function
2098 | Some
(allowdel
, active, first, outlines, qsearch) ->
2100 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2101 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2102 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2105 GlDraw.color (1., 1., 1.);
2106 let font = Glut.BITMAP_9_BY_15
in
2107 let draw_string x y s =
2108 GlPix.raster_pos ~
x ~
y ();
2109 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2112 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2115 let (s, l, _
, _
) = outlines.(row
) in
2116 let y = (row
- first) * 16 in
2121 GlDraw.polygon_mode `both `line
;
2122 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2123 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2124 GlDraw.rect (0., float (y + 1))
2125 (float (conf.winw
- 1), float (y + 18));
2126 GlDraw.polygon_mode `both `fill
;
2128 GlDraw.color (1., 1., 1.);
2130 draw_string (float x) (float (y + 16)) s;
2138 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2139 GlDraw.viewport
margin 0 state.w conf.winh
;
2141 GlClear.color (scalecolor 0.5);
2142 GlClear.clear
[`
color];
2145 let x = float state.x in
2146 GlMat.translate ~
x ();
2150 Gl.enable `scissor_test
;
2151 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2153 List.iter
drawpage state.layout;
2156 Gl.disable `scissor_test
2160 let x = -.float state.x in
2161 GlMat.translate ~
x ();
2165 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2168 showoutline state.outline;
2170 Glut.swapBuffers
();
2174 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2175 let x = x - margin - state.x in
2176 let rec f = function
2178 begin match getopaque l.pageno with
2179 | Some
(opaque, _
) when validopaque opaque ->
2180 let y = y - l.pagedispy
in
2183 let y = l.pagey + y in
2184 let x = x - l.pagex
in
2185 match whatsunder
opaque x y with
2198 let mouse ~button ~bstate ~
x ~
y =
2200 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2208 let incr = incr * 2 in
2209 let y = clamp incr in
2210 gotoy_and_clear_text y
2212 | Glut.LEFT_BUTTON
when state.outline = None
2213 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2214 if bstate
= Glut.DOWN
2216 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2217 state.mstate
<- Mpan
(x, y)
2220 state.mstate
<- Mnone
2223 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2224 if bstate
= Glut.DOWN
2226 let position, sh = scrollph state.y in
2227 if y > truncate
position && y < truncate
(position +. sh)
2229 state.mstate
<- Mscroll
2231 let percent = float y /. float conf.winh
in
2232 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2234 state.mstate
<- Mscroll
2236 state.mstate
<- Mnone
2238 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2239 begin match state.birdseye
with
2240 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2241 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2242 let rec loop = function
2245 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2246 && x > margin && x < margin + l.pagew
2248 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2249 reshape conf.winw
conf.winh
;
2250 state.anchor <- (l.pageno, 0.0);
2255 | None
-> () (* impossible *)
2258 | Glut.LEFT_BUTTON
when state.outline = None
->
2259 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2260 begin match dest with
2261 | Ulinkgoto
(pageno, top
) ->
2264 gotopage1 pageno top
2269 | Unone
when bstate
= Glut.DOWN
->
2270 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2271 state.mstate
<- Mpan
(x, y);
2273 | Unone
| Utext _
->
2274 if bstate
= Glut.DOWN
2276 if conf.angle
mod 360 = 0
2278 state.mstate
<- Msel
((x, y), (x, y));
2279 Glut.postRedisplay
()
2283 match state.mstate
with
2287 state.mstate
<- Mnone
2290 Glut.setCursor
Glut.CURSOR_INHERIT
;
2291 state.mstate
<- Mnone
2293 | Msel
((x0
, y0
), (x1
, y1
)) ->
2295 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2296 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2298 match getopaque l.pageno with
2299 | Some
(opaque, _
) when validopaque opaque ->
2303 List.iter
f state.layout;
2304 copysel
""; (* ugly *)
2305 Glut.setCursor
Glut.CURSOR_INHERIT
;
2306 state.mstate
<- Mnone
;
2313 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2316 if state.outline = None
2318 match state.mstate
with
2324 state.mstate
<- Mpan
(x, y);
2325 if conf.zoom > 1.0 then state.x <- state.x + dx;
2327 gotoy_and_clear_text y
2330 state.mstate
<- Msel
(a, (x, y));
2331 Glut.postRedisplay
()
2334 let y = min
conf.winh
(max
0 y) in
2335 let percent = float y /. float conf.winh
in
2336 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2337 gotoy_and_clear_text y
2341 match state.birdseye
with
2342 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2343 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2344 let rec loop = function
2346 if hooverpageno
!= -1
2348 state.birdseye
<- Some
(conf, leftx
, pageno, -1);
2349 Glut.postRedisplay
();
2352 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2353 && x > margin && x < margin + l.pagew
2355 state.birdseye
<- Some
(conf, leftx
, pageno, l.pageno);
2356 Glut.postRedisplay
();
2363 if state.outline = None
2365 match state.mstate
with
2367 begin match getunder x y with
2368 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2370 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2371 Glut.setCursor
Glut.CURSOR_INFO
2372 | Ulinkgoto
(page
, y) ->
2374 then showtext 'p'
("age: " ^ string_of_int page
);
2375 Glut.setCursor
Glut.CURSOR_INFO
2377 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2378 Glut.setCursor
Glut.CURSOR_TEXT
2381 | Mpan _
| Msel _
| Mscroll
->
2392 match Sys.os_type
with
2393 | "Win32" -> Sys.getenv
"HOMEPATH"
2394 | _
-> Sys.getenv
"HOME"
2397 ("Can not determine home directory location: " ^
2398 Printexc.to_string exn
);
2402 let config_of c attrs
=
2406 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2407 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2408 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2409 | "preload" -> { c with preload = bool_of_string v
}
2410 | "page-bias" -> { c with pagebias
= int_of_string v
}
2411 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2412 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2413 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2414 | "throttle" -> { c with showall
= bool_of_string v
}
2415 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2416 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2417 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2419 let zoom = float_of_string v
/. 100. in
2420 let zoom = max
0.01 (min
2.2 zoom) in
2421 { c with zoom = zoom }
2422 | "presentation" -> { c with presentation = bool_of_string v
}
2423 | "rotation-angle" -> { c with angle
= int_of_string v
}
2424 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2425 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2426 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2427 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2428 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2429 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2430 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2431 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2434 prerr_endline
("Error processing attribute (`" ^
2435 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2438 let rec fold c = function
2441 let c = apply c k v
in
2447 let bookmark_of attrs
=
2448 let rec fold title page rely
= function
2449 | ("title", v
) :: rest
-> fold v page rely rest
2450 | ("page", v
) :: rest
-> fold title v rely rest
2451 | ("rely", v
) :: rest
-> fold title page v rest
2452 | _
:: rest
-> fold title page rely rest
2453 | [] -> title, page
, rely
2455 fold "invalid" "0" "0" attrs
2458 let setconf dst src
=
2459 dst
.scrollw
<- src
.scrollw
;
2460 dst
.scrollh
<- src
.scrollh
;
2461 dst
.icase
<- src
.icase
;
2462 dst
.preload <- src
.preload;
2463 dst
.pagebias
<- src
.pagebias
;
2464 dst
.verbose
<- src
.verbose
;
2465 dst
.scrollincr
<- src
.scrollincr
;
2466 dst
.maxhfit
<- src
.maxhfit
;
2467 dst
.crophack
<- src
.crophack
;
2468 dst
.autoscroll
<- src
.autoscroll
;
2469 dst
.showall
<- src
.showall
;
2470 dst
.hlinks
<- src
.hlinks
;
2471 dst
.underinfo
<- src
.underinfo
;
2472 dst
.interpagespace <- src
.interpagespace;
2473 dst
.zoom <- src
.zoom;
2474 dst
.presentation <- src
.presentation;
2475 dst
.angle
<- src
.angle
;
2476 dst
.winw
<- src
.winw
;
2477 dst
.winh
<- src
.winh
;
2478 dst
.savebmarks
<- src
.savebmarks
;
2479 dst
.memlimit
<- src
.memlimit
;
2480 dst
.proportional
<- src
.proportional
;
2481 dst
.texcount
<- src
.texcount
;
2482 dst
.sliceheight
<- src
.sliceheight
;
2483 dst
.thumbw
<- src
.thumbw
;
2487 let l = String.length
s in
2488 let b = Buffer.create
l in
2494 let h = Hashtbl.create
10 in
2495 let dc = { defconf with angle
= defconf.angle
} in
2496 let rec toplevel v t spos epos
=
2498 | Vdata
| Vcdata
| Vend
-> v
2499 | Vopen
("llppconfig", attrs
, closed
) ->
2502 else { v
with f = llppconfig
}
2504 error
"unexpected subelement at top level" s spos
2505 | Vclose tag
-> error
"unexpected close at top level" s spos
2507 and llppconfig v t spos epos
=
2509 | Vdata
| Vcdata
| Vend
-> v
2510 | Vopen
("defaults", attrs
, closed
) ->
2511 let c = config_of dc attrs
in
2515 else { v
with f = skip
"defaults" (fun () -> v
) }
2517 | Vopen
("doc", attrs
, closed
) ->
2520 List.assoc
"path" attrs
2521 with Not_found
-> error
"doc is missing path attribute" s spos
2523 let path = unent pathent in
2524 let c = config_of dc attrs
in
2525 let pageno, rely
, x =
2529 dolog "error accessing %s (%S) at postion %d:\n %s"
2530 n v spos
(Printexc.to_string exn
);
2533 let rec fold pageno rely
x = function
2534 | [] -> pageno, rely
, x
2535 | ("rely", v
) :: rest
->
2536 fold pageno (safef float_of_string
"rely" v
0.0) x rest
2537 | ("page", v
) :: rest
->
2538 fold (safef int_of_string
"page" v
0) rely
x rest
2539 | ("x", v
) :: rest
->
2540 fold pageno rely
(safef int_of_string
"x" v
0) rest
2542 fold pageno rely
x rest
2546 let anchor = (pageno, rely
) in
2548 then (Hashtbl.add
h path (c, [], x, anchor); v
)
2549 else { v
with f = doc
path x anchor c [] }
2551 | Vopen
(tag
, _
, closed
) ->
2552 error
"unexpected subelement in llppconfig" s spos
2554 | Vclose
"llppconfig" -> { v
with f = toplevel }
2555 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2557 and doc
path x anchor c bookmarks v t spos epos
=
2559 | Vdata
| Vcdata
-> v
2560 | Vend
-> error
"unexpected end of input in doc" s spos
2561 | Vopen
("bookmarks", attrs
, closed
) ->
2562 { v
with f = pbookmarks
path x anchor c bookmarks }
2564 | Vopen
(tag
, _
, _
) ->
2565 error
"unexpected subelement in doc" s spos
2568 Hashtbl.add
h path (c, List.rev
bookmarks, x, anchor);
2569 { v
with f = llppconfig
}
2571 | Vclose tag
-> error
"unexpected close in doc" s spos
2573 and pbookmarks
path x anchor c bookmarks v t spos epos
=
2575 | Vdata
| Vcdata
-> v
2576 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2577 | Vopen
("item", attrs
, closed
) ->
2578 let titleent, spage
, srely
= bookmark_of attrs
in
2583 dolog "Failed to convert page %S to integer: %s"
2584 spage
(Printexc.to_string exn
);
2589 float_of_string srely
2591 dolog "Failed to convert rely %S to real: %s"
2592 srely
(Printexc.to_string exn
);
2595 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2597 then { v
with f = pbookmarks
path x anchor c bookmarks }
2600 { v
with f = skip
"item" f }
2603 error
"unexpected subelement in bookmarks" s spos
2605 | Vclose
"bookmarks" ->
2606 { v
with f = doc
path x anchor c bookmarks }
2608 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2610 and skip tag
f v t spos epos
=
2612 | Vdata
| Vcdata
-> v
2614 error
("unexpected end of input in skipped " ^ tag
) s spos
2615 | Vopen
(tag'
, _
, closed
) ->
2619 let f'
() = { v
with f = skip tag
f } in
2620 { v
with f = skip tag'
f'
}
2624 else error
("unexpected close in skipped " ^ tag
) s spos
2627 parse
{ f = toplevel; accu = () } s;
2633 let len = in_channel_length ic
in
2634 let s = String.create
len in
2635 really_input ic
s 0 len;
2638 | Parse_error
(msg
, s, pos
) ->
2639 let subs = subs s pos
in
2640 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2641 failwith
("parse error: " ^
s)
2644 failwith
("config load error: " ^
Printexc.to_string exn
)
2650 let dir = Filename.concat
home ".config" in
2651 if Sys.is_directory
dir then dir else home
2654 Filename.concat
dir "llpp.conf"
2658 if Sys.file_exists
path
2661 (try Some
(open_in_bin
path)
2664 ("Error opening configuation file `" ^
path ^
"': " ^
2665 Printexc.to_string exn
);
2674 ("Error loading configuation from `" ^
path ^
"': " ^
2675 Printexc.to_string exn
);
2681 f (Hashtbl.create
0, defconf)
2686 let pc, pb
, px
, pa
=
2688 Hashtbl.find
h state.path
2689 with Not_found
-> dc, [], 0, (0, 0.0)
2693 state.bookmarks <- pb
;
2695 cbput state.hists
.nav pa
;
2700 let add_attrs bb always
dc c =
2703 then Printf.bprintf bb
"\n %s='%b'" s a
2706 then Printf.bprintf bb
"\n %s='%d'" s a
2709 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2713 then dc.winw
, dc.winh
2715 match state.fullscreen
with
2717 | None
-> c.winw
, c.winh
2719 let zoom, presentation, interpagespace, showall
=
2721 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2723 match state.birdseye
with
2724 | Some
(bc
, _
, _
, _
) ->
2725 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2726 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2728 oi
"width" w dc.winw
;
2729 oi
"height" h dc.winh
;
2730 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2731 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2732 ob "case-insensitive-search" c.icase
dc.icase
;
2733 ob "preload" c.preload dc.preload;
2734 oi
"page-bias" c.pagebias
dc.pagebias
;
2735 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2736 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2737 ob "crop-hack" c.crophack
dc.crophack
;
2738 ob "throttle" showall
dc.showall
;
2739 ob "highlight-links" c.hlinks
dc.hlinks
;
2740 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2741 oi
"vertical-margin" interpagespace dc.interpagespace;
2742 oz
"zoom" zoom dc.zoom;
2743 ob "presentation" presentation dc.presentation;
2744 oi
"rotation-angle" c.angle
dc.angle
;
2745 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2746 ob "proportional-display" c.proportional
dc.proportional
;
2747 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2748 oi
"texcount" c.texcount
dc.texcount
;
2749 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2750 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2754 let bb = Buffer.create
32768 in
2756 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2757 add_attrs bb true dc dc;
2758 Buffer.add_string
bb "/>\n";
2760 let adddoc path x anchor c bookmarks =
2761 if bookmarks == [] && c = dc && anchor == emptyanchor
2764 Printf.bprintf
bb "<doc path='%s'"
2765 (enent
path 0 (String.length
path));
2767 if anchor != emptyanchor
2769 let n, y = anchor in
2770 Printf.bprintf
bb " page='%d'" n;
2771 Printf.bprintf
bb " rely='%f'" y;
2775 then Printf.bprintf
bb " pan='%d'" x;
2777 add_attrs bb false dc c;
2779 begin match bookmarks with
2780 | [] -> Buffer.add_string
bb "/>\n"
2782 Buffer.add_string
bb ">\n<bookmarks>\n";
2783 List.iter
(fun (title, _level
, page, rely) ->
2785 "<item title='%s' page='%d' rely='%f'/>\n"
2786 (enent
title 0 (String.length
title))
2790 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2796 match state.birdseye
with
2797 | Some
(_
, x, _
, _
) -> x
2800 adddoc state.path x (getanchor ()) conf
2801 (if conf.savebmarks
then state.bookmarks else []);
2803 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2804 if path <> state.path
2806 adddoc path x y c bookmarks
2808 Buffer.add_string
bb "</llppconfig>";
2811 if Buffer.length
bb > 0
2814 let tmp = path ^
".tmp" in
2815 let oc = open_out_bin
tmp in
2816 Buffer.output_buffer
oc bb;
2818 Sys.rename
tmp path;
2821 ("error while saving configuration: " ^
Printexc.to_string exn
)
2827 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2828 (fun s -> state.path <- s)
2829 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2832 if String.length
state.path = 0
2833 then (prerr_endline
"filename missing"; exit
1)
2835 if Filename.is_relative
state.path
2836 then Filename.concat
(Sys.getcwd
()) state.path
2844 let _ = Glut.init
Sys.argv
in
2845 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2846 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2847 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2850 if Sys.os_type
= "Unix"
2852 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2854 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2855 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2856 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2857 Unix.bind
sock addr;
2859 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2860 Unix.connect
csock addr;
2861 let ssock, _ = Unix.accept
sock in
2864 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2865 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2869 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2873 let () = Glut.displayFunc
display in
2874 let () = Glut.reshapeFunc
reshape in
2875 let () = Glut.keyboardFunc
keyboard in
2876 let () = Glut.specialFunc
special in
2877 let () = Glut.idleFunc
(Some
idle) in
2878 let () = Glut.mouseFunc
mouse in
2879 let () = Glut.motionFunc
motion in
2880 let () = Glut.passiveMotionFunc
pmotion in
2882 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2883 state.csock <- csock;
2884 state.ssock <- ssock;
2885 state.text <- "Opening " ^
path;
2886 writeopen state.path state.password
;
2890 let rec handlelablglutbug () =
2893 with Glut.BadEnum
"key in special_of_int" ->
2894 showtext '
!'
" LablGlut bug: special key not recognized";
2895 handlelablglutbug ()
2897 handlelablglutbug ();