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
, _
) as anchor = getanchor () in
1201 state.anchor <- anchor;
1203 Some
({ conf with zoom = conf.zoom }, state.x
, birdseyepageno
, -1);
1205 conf.presentation <- false;
1206 conf.interpagespace <- 10;
1207 conf.hlinks
<- false;
1209 state.mstate
<- Mnone
;
1210 conf.showall
<- false;
1211 Glut.setCursor
Glut.CURSOR_INHERIT
;
1214 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1221 let birdseyeoff (c, leftx
, pageno, _
) =
1222 state.birdseye
<- None
;
1223 conf.zoom <- c.zoom;
1224 conf.presentation <- c.presentation;
1225 conf.interpagespace <- c.interpagespace;
1226 conf.showall
<- c.showall
;
1227 conf.hlinks
<- c.hlinks
;
1231 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1236 let togglebirdseye () =
1237 match state.birdseye
with
1238 | None
-> birdseyeon ()
1239 | Some vals
-> birdseyeoff vals
1242 let viewkeyboard ~key ~x ~
y =
1244 state.textentry <- te
;
1247 Glut.postRedisplay
()
1249 match state.textentry with
1251 let c = Char.chr key
in
1257 let y = getnav () in
1258 gotoy_and_clear_text y
1261 begin match state.birdseye
with
1263 | Some
((_
, _
, pageno, _
) as vals
) ->
1266 reshape conf.winw
conf.winh
;
1267 state.anchor <- (pageno, 0.0);
1276 Glut.postRedisplay
()
1279 let ondone isforw
s =
1280 cbput state.hists
.pat
s;
1281 state.searchpattern
<- s;
1284 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1285 textentry, ondone (c ='
/'
)))
1287 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1288 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1289 conf.zoom <- min
2.2 (conf.zoom +. incr);
1290 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1291 reshape conf.winw
conf.winh
1296 try int_of_string
s with exc
->
1297 state.text <- Printf.sprintf
"bad integer `%s': %s"
1298 s (Printexc.to_string exc
);
1304 state.text <- "page bias is now " ^ string_of_int
n;
1307 enttext (Some
('
+'
, "", None
, intentry, ondone))
1309 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1310 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1311 conf.zoom <- max
0.01 (conf.zoom -. decr);
1312 if conf.zoom <= 1.0 then state.x
<- 0;
1313 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1314 reshape conf.winw
conf.winh
;
1320 enttext (Some
('
-'
, "", None
, optentry, ondone))
1322 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1325 state.text <- "zoom is 100%";
1326 reshape conf.winw
conf.winh
1328 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1329 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1334 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1335 reshape conf.winw
conf.winh
;
1338 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1340 reshape conf.winw
conf.winh
;
1345 try int_of_string
s with exc
->
1346 state.text <- Printf.sprintf
"bad integer `%s': %s"
1347 s (Printexc.to_string exc
);
1353 cbput state.hists
.pag
(string_of_int
n);
1354 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1357 let pageentry text key
=
1358 match Char.unsafe_chr key
with
1359 | 'g'
-> TEdone
text
1360 | _
-> intentry text key
1362 let text = "x" in text.[0] <- c;
1363 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1367 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1368 reshape conf.winw
conf.winh
;
1371 conf.hlinks
<- not
conf.hlinks
;
1372 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1373 Glut.postRedisplay
()
1376 conf.autoscroll
<- not
conf.autoscroll
1379 conf.presentation <- not
conf.presentation;
1380 showtext ' '
("presentation mode " ^
1381 if conf.presentation then "on" else "off");
1385 begin match state.fullscreen
with
1387 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1390 state.fullscreen
<- None
;
1395 gotoy_and_clear_text 0
1398 search state.searchpattern
true
1401 search state.searchpattern
false
1404 begin match state.layout with
1407 gotoy_and_clear_text (getpagey l.pageno)
1411 begin match List.rev
state.layout with
1414 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1415 gotoy_and_clear_text (getpagey pageno)
1419 begin match state.layout with
1422 let pageno = max
0 (l.pageno-1) in
1423 gotoy_and_clear_text (getpagey pageno)
1428 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1430 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1432 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1436 else (100. *. (float state.y /. float maxy)) in
1439 Printf.sprintf
"Page %d of %d %.2f%%"
1440 (fn+1) state.pagecount
percent
1443 "Pages %d-%d of %d %.2f%%"
1444 (fn+1) (ln
+1) state.pagecount
percent
1449 begin match state.layout with
1452 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1453 Glut.postRedisplay
();
1457 enterbookmarkmode ()
1461 match state.layout with
1464 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1468 enttext (Some
('~'
, "", None
, textentry, ondone))
1472 showtext ' '
"Quick bookmark added";
1475 begin match state.layout with
1477 let rect = getpdimrect
l.pagedimno
in
1481 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1482 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1484 (truncate
(rect.(1) -. rect.(0)),
1485 truncate
(rect.(3) -. rect.(0)))
1489 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1491 Glut.postRedisplay
();
1497 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1502 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1503 Glut.postRedisplay
()
1505 | 'k'
-> gotoy (clamp (-conf.scrollincr
))
1506 | 'j'
-> gotoy (clamp conf.scrollincr
)
1508 | '
r'
-> opendoc state.path
state.password
1511 vlog "huh? %d %c" key
(Char.chr key
);
1514 | Some
(c, text, opthist
, onkey
, ondone) when key
= 8 ->
1515 let len = String.length
text in
1518 state.textentry <- None
;
1519 Glut.postRedisplay
();
1522 let s = String.sub
text 0 (len - 1) in
1523 enttext (Some
(c, s, opthist
, onkey
, ondone))
1526 | Some
(c, text, onhist, onkey
, ondone) ->
1527 begin match Char.unsafe_chr key
with
1530 state.textentry <- None
;
1531 Glut.postRedisplay
()
1534 begin match onhist with
1536 | Some
(_
, onhistcancel
) -> onhistcancel
()
1538 state.textentry <- None
;
1539 Glut.postRedisplay
()
1542 begin match onkey
text key
with
1544 state.textentry <- None
;
1546 Glut.postRedisplay
()
1549 enttext (Some
(c, text, onhist, onkey
, ondone));
1552 state.textentry <- None
;
1553 Glut.postRedisplay
()
1556 state.textentry <- Some te
;
1557 Glut.postRedisplay
()
1562 let narrow outlines pattern
=
1563 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1567 let rec fold accu n =
1571 let (s, _
, _
, _
) as o
= outlines.(n) in
1573 if (try ignore
(Str.search_forward re
s 0); true
1574 with Not_found
-> false)
1580 let matched = fold [] (Array.length
outlines - 1) in
1581 if matched = [] then None
else Some
(Array.of_list
matched)
1584 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1585 let search active pattern
incr =
1588 if n = Array.length
outlines || n = -1
1591 let (s, _
, _
, _
) = outlines.(n) in
1593 (try ignore
(Str.search_forward re
s 0); true
1594 with Not_found
-> false)
1596 else loop (n + incr)
1601 let re = Str.regexp_case_fold pattern
in
1607 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1610 if String.length qsearch
= 0
1613 state.outline <- None
;
1614 Glut.postRedisplay
();
1618 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1619 Glut.postRedisplay
();
1623 let incr = if key
= 18 then -1 else 1 in
1625 match search (active + incr) qsearch
incr with
1627 state.text <- qsearch ^
" [not found]";
1630 state.text <- qsearch
;
1631 active, firstof active
1633 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1634 Glut.postRedisplay
();
1637 let len = String.length qsearch
in
1644 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1647 let qsearch = String.sub
qsearch 0 (len - 1) in
1649 match search active qsearch ~
-1 with
1651 state.text <- qsearch ^
" [not found]";
1654 state.text <- qsearch;
1655 active, firstof active
1657 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1659 Glut.postRedisplay
()
1662 if active < Array.length
outlines
1664 let (_
, _
, n, t
) = outlines.(active) in
1668 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1669 state.outline <- None
;
1670 Glut.postRedisplay
();
1672 | _
when key
>= 32 && key
< 127 ->
1673 let pattern = addchar qsearch (Char.chr key
) in
1675 match search active pattern 1 with
1677 state.text <- pattern ^
" [not found]";
1680 state.text <- pattern;
1681 active, firstof active
1683 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1684 Glut.postRedisplay
()
1686 | 14 when not allowdel
-> (* ctrl-n *)
1687 if String.length
qsearch > 0
1689 let optoutlines = narrow outlines qsearch in
1690 begin match optoutlines with
1691 | None
-> state.text <- "can't narrow"
1693 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1694 match state.outlines with
1697 state.outlines <- Onarrow
(qsearch, outlines, a)
1698 | Onarrow
(pat
, a, b) ->
1699 state.outlines <- Onarrow
(qsearch, outlines, b)
1702 Glut.postRedisplay
()
1704 | 21 when not allowdel
-> (* ctrl-u *)
1706 match state.outlines with
1709 let a = Array.of_list
(List.rev
l) in
1710 state.outlines <- Oarray
a;
1712 | Onarrow
(pat
, a, b) ->
1713 state.outlines <- Oarray
b;
1717 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1718 Glut.postRedisplay
()
1722 Some
(allowdel
, active, firstof active, outlines, qsearch);
1723 Glut.postRedisplay
()
1725 | 127 when allowdel
->
1726 let len = Array.length
outlines - 1 in
1729 state.outline <- None
;
1730 state.bookmarks <- [];
1733 let bookmarks = Array.init
len
1735 let i = if i >= active then i + 1 else i in
1746 Glut.postRedisplay
()
1748 | _
-> log "unknown key %d" key
1751 let keyboard ~key ~x ~
y =
1756 match state.outline with
1757 | None
-> viewkeyboard ~key ~x ~
y
1758 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1761 let birdseyespecial key x
y (conf, leftx
, pageno, hooverpageno
) =
1764 let pageno = max
0 (pageno - 1) in
1766 let rec loop = function
1769 if l.pageno = pageno
1776 then gotopage1 pageno 0
1777 else Glut.postRedisplay
();
1778 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
)
1781 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1782 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1783 let rec loop = function
1784 | [] -> gotopage1 pageno 0
1786 if l.pageno = pageno
1788 if l.pagevh != l.pageh
1789 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1790 else Glut.postRedisplay
()
1796 | Glut.KEY_PAGE_UP
->
1797 begin match state.layout with
1799 if l.pageno = pageno
1801 match layout (state.y - conf.winh
) conf.winh
with
1802 | [] -> gotoy (clamp (-conf.winh
))
1804 let pageno = max
0 (l.pageno - 1) in
1805 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1809 let pageno = max
0 (l.pageno - 1) in
1810 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1813 | [] -> gotoy (clamp (-conf.winh
))
1815 | Glut.KEY_PAGE_DOWN
->
1816 begin match List.rev
state.layout with
1818 let pageno = min
l.pageno (state.pagecount
- 1) in
1819 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1820 gotoy (clamp (l.pagedispy
+ l.pageh
))
1821 | [] -> gotoy (clamp conf.winh
)
1825 state.birdseye
<- Some
(conf, leftx
, 0, hooverpageno
);
1828 let pageno = state.pagecount
- 1 in
1829 state.birdseye
<- Some
(conf, leftx
, pageno, hooverpageno
);
1830 if not
(pagevisible state.layout pageno)
1833 match List.rev
state.pdims
with
1835 | (_
, _
, h, _
) :: _
-> h
1837 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
1838 else Glut.postRedisplay
();
1842 let special ~key ~x ~
y =
1843 match state.outline, state.birdseye
with
1844 | None
, _
when key
= Glut.KEY_F9
->
1846 reshape conf.winw
conf.winh
;
1848 | None
, (Some vals
) ->
1849 birdseyespecial key x
y vals
1852 begin match state.textentry with
1856 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1857 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1858 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1859 | Glut.KEY_PAGE_UP
->
1860 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1862 match state.layout with
1864 | l :: _
-> state.y - l.pagey
1867 | Glut.KEY_PAGE_DOWN
->
1868 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1870 match List.rev
state.layout with
1872 | l :: _
-> getpagey l.pageno
1875 | Glut.KEY_HOME
-> addnav (); 0
1878 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1880 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1881 state.x
<- state.x
- 10;
1883 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1884 state.x
<- state.x
+ 10;
1889 gotoy_and_clear_text y
1891 | Some
(c, s, (Some
(action, _
) as onhist), onkey
, ondone) ->
1894 | Glut.KEY_UP
-> action HCprev
1895 | Glut.KEY_DOWN
-> action HCnext
1896 | Glut.KEY_HOME
-> action HCfirst
1897 | Glut.KEY_END
-> action HClast
1900 state.textentry <- Some
(c, s, onhist, onkey
, ondone);
1901 Glut.postRedisplay
()
1906 | Some
(allowdel
, active, first
, outlines, qsearch), _
->
1907 let maxrows = maxoutlinerows () in
1908 let calcfirst first
active =
1911 let rows = active - first
in
1912 if rows > maxrows then active - maxrows else first
1916 let active = active + incr in
1917 let active = max
0 (min
active (Array.length
outlines - 1)) in
1918 let first = calcfirst first active in
1919 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1920 Glut.postRedisplay
()
1922 let updownlevel incr =
1923 let len = Array.length
outlines in
1924 let (_
, curlevel
, _
, _
) = outlines.(active) in
1926 if i = len then i-1 else if i = -1 then 0 else
1927 let (_
, l, _
, _
) = outlines.(i) in
1928 if l != curlevel
then i else flow (i+incr)
1930 let active = flow active in
1931 let first = calcfirst first active in
1932 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1933 Glut.postRedisplay
()
1936 | Glut.KEY_UP
-> navigate ~
-1
1937 | Glut.KEY_DOWN
-> navigate 1
1938 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1939 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1941 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1942 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1945 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1946 Glut.postRedisplay
()
1949 let active = Array.length
outlines - 1 in
1950 let first = max
0 (active - maxrows) in
1951 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1952 Glut.postRedisplay
()
1957 let drawplaceholder l =
1958 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1960 (float l.pagex
, float l.pagedispy
)
1961 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1963 let x = float (if margin < 0 then -margin else l.pagex
)
1964 and y = float (l.pagedispy
+ 13) in
1965 let font = Glut.BITMAP_8_BY_13
in
1966 GlDraw.color
(0.0, 0.0, 0.0);
1967 GlPix.raster_pos ~
x ~
y ();
1968 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1969 ("Loading " ^ string_of_int
(l.pageno + 1));
1972 let now () = Unix.gettimeofday
();;
1975 if state.textentry = None
1977 match state.birdseye
with
1978 | None
-> GlDraw.color
(scalecolor 1.0);
1979 | Some
(_
, _
, pageno, hooverpageno
) ->
1981 if l.pageno = pageno
1984 if l.pageno = hooverpageno
1989 GlDraw.color (scalecolor color);
1991 else GlDraw.color (scalecolor 0.4);
1992 begin match getopaque l.pageno with
1993 | Some
(opaque, _
) when validopaque opaque ->
1995 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1999 vlog "draw %d %f sec" l.pageno d;
2007 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2008 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2009 let sh = float conf.winh
/. sh in
2010 let sh = max
sh (float conf.scrollh
) in
2013 if state.y = state.maxy
2015 else float y /. float maxy
2017 let position = (float conf.winh
-. sh) *. percent in
2020 if position +. sh > float conf.winh
2021 then float conf.winh
-. sh
2027 let scrollindicator () =
2028 GlDraw.color (0.64 , 0.64, 0.64);
2030 (float (conf.winw
- conf.scrollw
), 0.)
2031 (float conf.winw
, float conf.winh
)
2033 GlDraw.color (0.0, 0.0, 0.0);
2035 let position, sh = scrollph state.y in
2037 (float (conf.winw
- conf.scrollw
), position)
2038 (float conf.winw
, position +. sh)
2042 let showsel margin =
2043 match state.mstate
with
2044 | Mnone
| Mscroll _
| Mpan _
->
2047 | Msel
((x0
, y0
), (x1
, y1
)) ->
2048 let rec loop = function
2050 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2051 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2053 match getopaque l.pageno with
2054 | Some
(opaque, _
) when validopaque opaque ->
2055 let oy = -l.pagey + l.pagedispy
in
2057 (x0
- margin - state.x, y0
,
2058 x1
- margin - state.x, y1
) oy;
2068 let panx = float state.x in
2070 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2071 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2073 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2075 if l.pageno = pageno
2077 let d = float (l.pagedispy
- l.pagey) in
2078 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2079 GlDraw.begins `quads
;
2081 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2082 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2083 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2084 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2094 let showoutline = function
2096 | Some
(allowdel
, active, first, outlines, qsearch) ->
2098 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2099 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2100 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2103 GlDraw.color (1., 1., 1.);
2104 let font = Glut.BITMAP_9_BY_15
in
2105 let draw_string x y s =
2106 GlPix.raster_pos ~
x ~
y ();
2107 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2110 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
2113 let (s, l, _
, _
) = outlines.(row
) in
2114 let y = (row
- first) * 16 in
2119 GlDraw.polygon_mode `both `line
;
2120 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2121 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2122 GlDraw.rect (0., float (y + 1))
2123 (float (conf.winw
- 1), float (y + 18));
2124 GlDraw.polygon_mode `both `fill
;
2126 GlDraw.color (1., 1., 1.);
2128 draw_string (float x) (float (y + 16)) s;
2136 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2137 GlDraw.viewport
margin 0 state.w conf.winh
;
2139 GlClear.color (scalecolor 0.5);
2140 GlClear.clear
[`
color];
2143 let x = float state.x in
2144 GlMat.translate ~
x ();
2148 Gl.enable `scissor_test
;
2149 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2151 List.iter
drawpage state.layout;
2154 Gl.disable `scissor_test
2158 let x = -.float state.x in
2159 GlMat.translate ~
x ();
2163 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2166 showoutline state.outline;
2168 Glut.swapBuffers
();
2172 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2173 let x = x - margin - state.x in
2174 let rec f = function
2176 begin match getopaque l.pageno with
2177 | Some
(opaque, _
) when validopaque opaque ->
2178 let y = y - l.pagedispy
in
2181 let y = l.pagey + y in
2182 let x = x - l.pagex
in
2183 match whatsunder
opaque x y with
2196 let mouse ~button ~bstate ~
x ~
y =
2198 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2206 let incr = incr * 2 in
2207 let y = clamp incr in
2208 gotoy_and_clear_text y
2210 | Glut.LEFT_BUTTON
when state.outline = None
2211 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2212 if bstate
= Glut.DOWN
2214 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2215 state.mstate
<- Mpan
(x, y)
2218 state.mstate
<- Mnone
2221 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
2222 if bstate
= Glut.DOWN
2224 let position, sh = scrollph state.y in
2225 if y > truncate
position && y < truncate
(position +. sh)
2227 state.mstate
<- Mscroll
2229 let percent = float y /. float conf.winh
in
2230 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2232 state.mstate
<- Mscroll
2234 state.mstate
<- Mnone
2236 | Glut.LEFT_BUTTON
when state.outline = None
&& state.birdseye
<> None
->
2237 begin match state.birdseye
with
2238 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2239 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2240 let rec loop = function
2243 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2244 && x > margin && x < margin + l.pagew
2246 birdseyeoff (conf, leftx
, l.pageno, hooverpageno
);
2247 reshape conf.winw
conf.winh
;
2248 state.anchor <- (l.pageno, 0.0);
2253 | None
-> () (* impossible *)
2256 | Glut.LEFT_BUTTON
when state.outline = None
->
2257 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2258 begin match dest with
2259 | Ulinkgoto
(pageno, top
) ->
2262 gotopage1 pageno top
2267 | Unone
when bstate
= Glut.DOWN
->
2268 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2269 state.mstate
<- Mpan
(x, y);
2271 | Unone
| Utext _
->
2272 if bstate
= Glut.DOWN
2274 if conf.angle
mod 360 = 0
2276 state.mstate
<- Msel
((x, y), (x, y));
2277 Glut.postRedisplay
()
2281 match state.mstate
with
2285 state.mstate
<- Mnone
2288 Glut.setCursor
Glut.CURSOR_INHERIT
;
2289 state.mstate
<- Mnone
2291 | Msel
((x0
, y0
), (x1
, y1
)) ->
2293 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2294 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2296 match getopaque l.pageno with
2297 | Some
(opaque, _
) when validopaque opaque ->
2301 List.iter
f state.layout;
2302 copysel
""; (* ugly *)
2303 Glut.setCursor
Glut.CURSOR_INHERIT
;
2304 state.mstate
<- Mnone
;
2311 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2314 if state.outline = None
2316 match state.mstate
with
2322 state.mstate
<- Mpan
(x, y);
2323 if conf.zoom > 1.0 then state.x <- state.x + dx;
2325 gotoy_and_clear_text y
2328 state.mstate
<- Msel
(a, (x, y));
2329 Glut.postRedisplay
()
2332 let y = min
conf.winh
(max
0 y) in
2333 let percent = float y /. float conf.winh
in
2334 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2335 gotoy_and_clear_text y
2339 match state.birdseye
with
2340 | Some
(conf, leftx
, pageno, hooverpageno
) ->
2341 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2342 let rec loop = function
2344 if hooverpageno
!= -1
2346 state.birdseye
<- Some
(conf, leftx
, pageno, -1);
2347 Glut.postRedisplay
();
2350 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
2351 && x > margin && x < margin + l.pagew
2353 state.birdseye
<- Some
(conf, leftx
, pageno, l.pageno);
2354 Glut.postRedisplay
();
2361 if state.outline = None
2363 match state.mstate
with
2365 begin match getunder x y with
2366 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2368 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2369 Glut.setCursor
Glut.CURSOR_INFO
2370 | Ulinkgoto
(page
, y) ->
2372 then showtext 'p'
("age: " ^ string_of_int page
);
2373 Glut.setCursor
Glut.CURSOR_INFO
2375 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2376 Glut.setCursor
Glut.CURSOR_TEXT
2379 | Mpan _
| Msel _
| Mscroll
->
2390 match Sys.os_type
with
2391 | "Win32" -> Sys.getenv
"HOMEPATH"
2392 | _
-> Sys.getenv
"HOME"
2395 ("Can not determine home directory location: " ^
2396 Printexc.to_string exn
);
2400 let config_of c attrs
=
2404 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string v
) }
2405 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string v
) }
2406 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2407 | "preload" -> { c with preload = bool_of_string v
}
2408 | "page-bias" -> { c with pagebias
= int_of_string v
}
2409 | "scroll-step" -> { c with scrollincr
= max
1 (int_of_string v
) }
2410 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2411 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2412 | "throttle" -> { c with showall
= bool_of_string v
}
2413 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2414 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2415 | "vertical-margin" -> { c with interpagespace = max
0 (int_of_string v
) }
2417 let zoom = float_of_string v
/. 100. in
2418 let zoom = max
0.01 (min
2.2 zoom) in
2419 { c with zoom = zoom }
2420 | "presentation" -> { c with presentation = bool_of_string v
}
2421 | "rotation-angle" -> { c with angle
= int_of_string v
}
2422 | "width" -> { c with winw
= max
20 (int_of_string v
) }
2423 | "height" -> { c with winh
= max
20 (int_of_string v
) }
2424 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2425 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2426 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string v
) }
2427 | "tex-count" -> { c with texcount
= max
1 (int_of_string v
) }
2428 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string v
) }
2429 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string v
) }
2432 prerr_endline
("Error processing attribute (`" ^
2433 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2436 let rec fold c = function
2439 let c = apply c k v
in
2445 let bookmark_of attrs
=
2446 let rec fold title page rely
= function
2447 | ("title", v
) :: rest
-> fold v page rely rest
2448 | ("page", v
) :: rest
-> fold title v rely rest
2449 | ("rely", v
) :: rest
-> fold title page v rest
2450 | _
:: rest
-> fold title page rely rest
2451 | [] -> title, page
, rely
2453 fold "invalid" "0" "0" attrs
2456 let setconf dst src
=
2457 dst
.scrollw
<- src
.scrollw
;
2458 dst
.scrollh
<- src
.scrollh
;
2459 dst
.icase
<- src
.icase
;
2460 dst
.preload <- src
.preload;
2461 dst
.pagebias
<- src
.pagebias
;
2462 dst
.verbose
<- src
.verbose
;
2463 dst
.scrollincr
<- src
.scrollincr
;
2464 dst
.maxhfit
<- src
.maxhfit
;
2465 dst
.crophack
<- src
.crophack
;
2466 dst
.autoscroll
<- src
.autoscroll
;
2467 dst
.showall
<- src
.showall
;
2468 dst
.hlinks
<- src
.hlinks
;
2469 dst
.underinfo
<- src
.underinfo
;
2470 dst
.interpagespace <- src
.interpagespace;
2471 dst
.zoom <- src
.zoom;
2472 dst
.presentation <- src
.presentation;
2473 dst
.angle
<- src
.angle
;
2474 dst
.winw
<- src
.winw
;
2475 dst
.winh
<- src
.winh
;
2476 dst
.savebmarks
<- src
.savebmarks
;
2477 dst
.memlimit
<- src
.memlimit
;
2478 dst
.proportional
<- src
.proportional
;
2479 dst
.texcount
<- src
.texcount
;
2480 dst
.sliceheight
<- src
.sliceheight
;
2481 dst
.thumbw
<- src
.thumbw
;
2485 let l = String.length
s in
2486 let b = Buffer.create
l in
2492 let h = Hashtbl.create
10 in
2493 let dc = { defconf with angle
= defconf.angle
} in
2494 let rec toplevel v t spos epos
=
2496 | Vdata
| Vcdata
| Vend
-> v
2497 | Vopen
("llppconfig", attrs
, closed
) ->
2500 else { v
with f = llppconfig
}
2502 error
"unexpected subelement at top level" s spos
2503 | Vclose tag
-> error
"unexpected close at top level" s spos
2505 and llppconfig v t spos epos
=
2507 | Vdata
| Vcdata
| Vend
-> v
2508 | Vopen
("defaults", attrs
, closed
) ->
2509 let c = config_of dc attrs
in
2513 else { v
with f = skip
"defaults" (fun () -> v
) }
2515 | Vopen
("doc", attrs
, closed
) ->
2518 List.assoc
"path" attrs
2519 with Not_found
-> error
"doc is missing path attribute" s spos
2521 let path = unent pathent in
2522 let c = config_of dc attrs
in
2523 let pageno, rely
, x =
2527 dolog "error accessing %s (%S) at postion %d:\n %s"
2528 n v spos
(Printexc.to_string exn
);
2531 let rec fold pageno rely
x = function
2532 | [] -> pageno, rely
, x
2533 | ("rely", v
) :: rest
->
2534 fold pageno (safef float_of_string
"rely" v
0.0) x rest
2535 | ("page", v
) :: rest
->
2536 fold (safef int_of_string
"page" v
0) rely
x rest
2537 | ("x", v
) :: rest
->
2538 fold pageno rely
(safef int_of_string
"x" v
0) rest
2540 fold pageno rely
x rest
2544 let anchor = (pageno, rely
) in
2546 then (Hashtbl.add
h path (c, [], x, anchor); v
)
2547 else { v
with f = doc
path x anchor c [] }
2549 | Vopen
(tag
, _
, closed
) ->
2550 error
"unexpected subelement in llppconfig" s spos
2552 | Vclose
"llppconfig" -> { v
with f = toplevel }
2553 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2555 and doc
path x anchor c bookmarks v t spos epos
=
2557 | Vdata
| Vcdata
-> v
2558 | Vend
-> error
"unexpected end of input in doc" s spos
2559 | Vopen
("bookmarks", attrs
, closed
) ->
2560 { v
with f = pbookmarks
path x anchor c bookmarks }
2562 | Vopen
(tag
, _
, _
) ->
2563 error
"unexpected subelement in doc" s spos
2566 Hashtbl.add
h path (c, List.rev
bookmarks, x, anchor);
2567 { v
with f = llppconfig
}
2569 | Vclose tag
-> error
"unexpected close in doc" s spos
2571 and pbookmarks
path x anchor c bookmarks v t spos epos
=
2573 | Vdata
| Vcdata
-> v
2574 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2575 | Vopen
("item", attrs
, closed
) ->
2576 let titleent, spage
, srely
= bookmark_of attrs
in
2581 dolog "Failed to convert page %S to integer: %s"
2582 spage
(Printexc.to_string exn
);
2587 float_of_string srely
2589 dolog "Failed to convert rely %S to real: %s"
2590 srely
(Printexc.to_string exn
);
2593 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2595 then { v
with f = pbookmarks
path x anchor c bookmarks }
2598 { v
with f = skip
"item" f }
2601 error
"unexpected subelement in bookmarks" s spos
2603 | Vclose
"bookmarks" ->
2604 { v
with f = doc
path x anchor c bookmarks }
2606 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2608 and skip tag
f v t spos epos
=
2610 | Vdata
| Vcdata
-> v
2612 error
("unexpected end of input in skipped " ^ tag
) s spos
2613 | Vopen
(tag'
, _
, closed
) ->
2617 let f'
() = { v
with f = skip tag
f } in
2618 { v
with f = skip tag'
f'
}
2622 else error
("unexpected close in skipped " ^ tag
) s spos
2625 parse
{ f = toplevel; accu = () } s;
2631 let len = in_channel_length ic
in
2632 let s = String.create
len in
2633 really_input ic
s 0 len;
2636 | Parse_error
(msg
, s, pos
) ->
2637 let subs = subs s pos
in
2638 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2639 failwith
("parse error: " ^
s)
2642 failwith
("config load error: " ^
Printexc.to_string exn
)
2648 let dir = Filename.concat
home ".config" in
2649 if Sys.is_directory
dir then dir else home
2652 Filename.concat
dir "llpp.conf"
2656 if Sys.file_exists
path
2659 (try Some
(open_in_bin
path)
2662 ("Error opening configuation file `" ^
path ^
"': " ^
2663 Printexc.to_string exn
);
2672 ("Error loading configuation from `" ^
path ^
"': " ^
2673 Printexc.to_string exn
);
2679 f (Hashtbl.create
0, defconf)
2684 let pc, pb
, px
, pa
=
2686 Hashtbl.find
h state.path
2687 with Not_found
-> dc, [], 0, (0, 0.0)
2691 state.bookmarks <- pb
;
2693 cbput state.hists
.nav pa
;
2698 let add_attrs bb always
dc c =
2701 then Printf.bprintf bb
"\n %s='%b'" s a
2704 then Printf.bprintf bb
"\n %s='%d'" s a
2707 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2711 then dc.winw
, dc.winh
2713 match state.fullscreen
with
2715 | None
-> c.winw
, c.winh
2717 let zoom, presentation, interpagespace, showall
=
2719 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2721 match state.birdseye
with
2722 | Some
(bc
, _
, _
, _
) ->
2723 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
2724 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
2726 oi
"width" w dc.winw
;
2727 oi
"height" h dc.winh
;
2728 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2729 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2730 ob "case-insensitive-search" c.icase
dc.icase
;
2731 ob "preload" c.preload dc.preload;
2732 oi
"page-bias" c.pagebias
dc.pagebias
;
2733 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2734 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2735 ob "crop-hack" c.crophack
dc.crophack
;
2736 ob "throttle" showall
dc.showall
;
2737 ob "highlight-links" c.hlinks
dc.hlinks
;
2738 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2739 oi
"vertical-margin" interpagespace dc.interpagespace;
2740 oz
"zoom" zoom dc.zoom;
2741 ob "presentation" presentation dc.presentation;
2742 oi
"rotation-angle" c.angle
dc.angle
;
2743 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2744 ob "proportional-display" c.proportional
dc.proportional
;
2745 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
2746 oi
"texcount" c.texcount
dc.texcount
;
2747 oi
"slice-height" c.sliceheight
dc.sliceheight
;
2748 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
2752 let bb = Buffer.create
32768 in
2754 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2755 add_attrs bb true dc dc;
2756 Buffer.add_string
bb "/>\n";
2758 let adddoc path x anchor c bookmarks =
2759 if bookmarks == [] && c = dc && anchor = emptyanchor
2762 Printf.bprintf
bb "<doc path='%s'"
2763 (enent
path 0 (String.length
path));
2765 if anchor <> emptyanchor
2767 let n, y = anchor in
2768 Printf.bprintf
bb " page='%d'" n;
2769 Printf.bprintf
bb " rely='%f'" y;
2773 then Printf.bprintf
bb " pan='%d'" x;
2775 add_attrs bb false dc c;
2777 begin match bookmarks with
2778 | [] -> Buffer.add_string
bb "/>\n"
2780 Buffer.add_string
bb ">\n<bookmarks>\n";
2781 List.iter
(fun (title, _level
, page, rely) ->
2783 "<item title='%s' page='%d' rely='%f'/>\n"
2784 (enent
title 0 (String.length
title))
2788 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2794 match state.birdseye
with
2795 | Some
(_
, x, _
, _
) -> x
2798 adddoc state.path x (getanchor ()) conf
2799 (if conf.savebmarks
then state.bookmarks else []);
2801 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2802 if path <> state.path
2804 adddoc path x y c bookmarks
2806 Buffer.add_string
bb "</llppconfig>";
2809 if Buffer.length
bb > 0
2812 let tmp = path ^
".tmp" in
2813 let oc = open_out_bin
tmp in
2814 Buffer.output_buffer
oc bb;
2816 Sys.rename
tmp path;
2819 ("error while saving configuration: " ^
Printexc.to_string exn
)
2825 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2826 (fun s -> state.path <- s)
2827 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2830 if String.length
state.path = 0
2831 then (prerr_endline
"filename missing"; exit
1)
2833 if Filename.is_relative
state.path
2834 then Filename.concat
(Sys.getcwd
()) state.path
2842 let _ = Glut.init
Sys.argv
in
2843 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2844 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2845 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2848 if Sys.os_type
= "Unix"
2850 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2852 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2853 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2854 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2855 Unix.bind
sock addr;
2857 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2858 Unix.connect
csock addr;
2859 let ssock, _ = Unix.accept
sock in
2862 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2863 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2867 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2871 let () = Glut.displayFunc
display in
2872 let () = Glut.reshapeFunc
reshape in
2873 let () = Glut.keyboardFunc
keyboard in
2874 let () = Glut.specialFunc
special in
2875 let () = Glut.idleFunc
(Some
idle) in
2876 let () = Glut.mouseFunc
mouse in
2877 let () = Glut.motionFunc
motion in
2878 let () = Glut.passiveMotionFunc
pmotion in
2880 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
2881 state.csock <- csock;
2882 state.ssock <- ssock;
2883 state.text <- "Opening " ^
path;
2884 writeopen state.path state.password
;
2888 let rec handlelablglutbug () =
2891 with Glut.BadEnum
"key in special_of_int" ->
2892 showtext '
!'
" LablGlut bug: special key not recognized";
2893 handlelablglutbug ()
2895 handlelablglutbug ();