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 external init
: Unix.file_descr
-> unit = "ml_init";;
12 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext
: string -> (int * int * int * int) -> int -> unit =
15 external copysel
: string -> unit = "ml_copysel";;
16 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
17 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
18 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
22 | Msel
of (mpos
* mpos
)
36 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
37 and onkey
= string -> int -> te
38 and ondone
= string -> unit
39 and onhist
= histcmd
-> string
40 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
45 | TEswitch
of textentry
49 { store
= Array.create n v
56 let cblen b
= Array.length b
.store
;;
61 b
.wc
<- (b
.wc
+ 1) mod len;
62 b
.len <- min
(b
.len + 1) len;
65 let cbpeekw b
= b
.store
.(b
.wc
);;
71 let rc = b
.rc + dir
in
72 let rc = if rc = -1 then b
.len - 1 else rc in
73 let rc = if rc = b
.len then 0 else rc in
84 Array.fill b
.store
0 (Array.length b
.store
) v
;
100 { mutable scrollw
: int
101 ; mutable scrollh
: int
102 ; mutable icase
: bool
103 ; mutable preload
: bool
104 ; mutable pagebias
: int
105 ; mutable verbose
: bool
106 ; mutable scrollincr
: int
107 ; mutable maxhfit
: bool
108 ; mutable crophack
: bool
109 ; mutable autoscroll
: bool
110 ; mutable showall
: bool
111 ; mutable hlinks
: bool
112 ; mutable underinfo
: bool
113 ; mutable interpagespace
: int
114 ; mutable zoom
: float
115 ; mutable presentation
: bool
116 ; mutable angle
: int
119 ; mutable savebmarks
: bool
120 ; mutable proportional
: bool
129 and proportional
= bool
134 type outline
= string * int * int * float;;
136 | Oarray
of outline array
137 | Olist
of outline list
138 | Onarrow
of string * outline array
* outline array
141 type rect
= (float * float * float * float * float * float * float * float);;
144 { mutable csock
: Unix.file_descr
145 ; mutable ssock
: Unix.file_descr
151 ; mutable layout
: layout list
152 ; pagemap
: ((pageno
* width
* angle
* proportional
), opaque
) Hashtbl.t
153 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
154 ; mutable pagecount
: int
155 ; pagecache
: string circbuf
156 ; mutable rendering
: bool
157 ; mutable mstate
: mstate
158 ; mutable searchpattern
: string
159 ; mutable rects
: (pageno
* recttype
* rect
) list
160 ; mutable rects1
: (pageno
* recttype
* rect
) list
161 ; mutable text
: string
162 ; mutable fullscreen
: (width
* height
) option
163 ; mutable textentry
: textentry
option
164 ; mutable outlines
: outlines
165 ; mutable outline
: (bool * int * int * outline array
* string) option
166 ; mutable bookmarks
: outline list
167 ; mutable path
: string
168 ; mutable password
: string
169 ; mutable invalidated
: int
170 ; mutable colorscale
: float
174 { pat
: string circbuf
175 ; pag
: string circbuf
176 ; nav
: float circbuf
196 ; presentation
= false
201 ; proportional
= true
205 let conf = { defconf with angle
= defconf.angle
};;
216 ; pagemap
= Hashtbl.create
10
217 ; pagecache
= cbnew 10 ""
228 ; outlines
= Olist
[]
235 { nav
= cbnew 100 0.0
246 Printf.kprintf prerr_endline fmt
248 Printf.kprintf ignore fmt
252 let len = String.length s
in
254 let b = Buffer.create
n in
255 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
256 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
257 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
258 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
259 Buffer.add_string
b s
;
260 let s'
= Buffer.contents
b in
261 let n'
= Unix.write fd
s'
0 n in
262 if n'
!= n then failwith
"write failed";
267 let n = Unix.read fd
s 0 4 in
268 if n != 4 then failwith
"incomplete read(len)";
270 lor (Char.code
s.[0] lsl 24)
271 lor (Char.code
s.[1] lsl 16)
272 lor (Char.code
s.[2] lsl 8)
273 lor (Char.code
s.[3] lsl 0)
275 let s = String.create
len in
276 let n = Unix.read fd
s 0 len in
277 if n != len then failwith
"incomplete read(data)";
284 else float y
/. float state.maxy
288 let b = Buffer.create
10 in
289 Buffer.add_string
b s;
290 let rec combine = function
293 Buffer.add_char
b ' '
;
296 | `
b b -> if b then "1" else "0"
298 | `i i
-> string_of_int i
299 | `f f
-> string_of_float f
300 | `I f
-> string_of_int
(truncate f
)
302 Buffer.add_string
b s;
309 let cmd = Buffer.contents
(makecmd s l
) in
310 writecmd state.csock
cmd;
316 let d = conf.winh
- h
in
323 let rec f pn ph pi fh l
=
325 | (n, _
, h
, _
) :: rest
->
326 let ips = calcips h
in
332 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
341 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
344 let fh = f 0 0 0 0 state.pdims
in
348 let getpageyh pageno
=
349 let rec f pn ph pi y l
=
351 | (n, _
, h
, _
) :: rest
->
352 let ips = calcips h
in
355 if conf.presentation
&& n = pageno
357 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
359 y
+ (pageno
- pn
) * (ph
+ pi
), h
361 let y = y + (if conf.presentation
then pi
else 0) in
362 let y = y + (n - pn
) * (ph
+ pi
) in
366 y + (pageno
- pn
) * (ph
+ pi
), ph
368 f 0 0 0 0 state.pdims
371 let getpagey pageno
= fst
(getpageyh pageno
);;
374 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
375 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
377 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
378 let ips = calcips h
in
379 let yinc = if conf.presentation
then ips else 0 in
380 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
382 prev
, pdims
, pdimno
, 0
384 let dy = dy + yinc in
385 let py = py + yinc in
386 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
391 if py + h
<= vy - yinc
393 let py = py + h
+ ips in
394 let dy = max
0 (py - y) in
404 let pagey = vy - py in
405 let pagevh = h
- pagey in
406 let pagevh = min
(sh
- dy) pagevh in
407 let off = if yinc > 0 then py - vy else 0
409 let py = py + h
+ ips in
415 ; pagedispy
= dy + off
416 ; pagey = pagey + off
417 ; pagevh = pagevh - off
421 let accu = e :: accu in
428 ~cacheleft
:(cacheleft
-1)
431 if state.invalidated
= 0
441 ~cacheleft
:(cblen state.pagecache
)
451 let y = state.y + incr
in
453 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
457 let getopaque pageno
=
458 try Some
(Hashtbl.find
state.pagemap
459 (pageno
+ 1, state.w
, conf.angle
, conf.proportional
))
460 with Not_found
-> None
463 let cache pageno opaque
=
464 Hashtbl.replace
state.pagemap
465 (pageno
+ 1, state.w
, conf.angle
, conf.proportional
) opaque
468 let validopaque opaque
= String.length opaque
> 0;;
471 match getopaque l
.pageno
with
472 | None
when not
state.rendering
->
473 state.rendering
<- true;
475 wcmd "render" [`i
(l
.pageno
+ 1)
483 let loadlayout layout =
484 let rec f all
= function
486 begin match getopaque l
.pageno
with
487 | None
-> render l
; f false ls
488 | Some opaque
-> f (all
&& validopaque opaque
) ls
492 f (layout <> []) layout;
499 let evictedopaque = cbpeekw state.pagecache
in
500 List.exists
(fun l
->
501 match getopaque l
.pageno
with
502 | Some opaque
when validopaque opaque
->
503 evictedopaque = opaque
507 if not
evictedvisible
509 let rely = yratio state.y in
510 let presentation = conf.presentation in
511 let interpagespace = conf.interpagespace in
512 let maxy = state.maxy in
513 conf.presentation <- false;
514 conf.interpagespace <- 0;
515 state.maxy <- calcheight ();
516 let y = truncate
(float state.maxy *. rely) in
517 let y = if y < conf.winh
then 0 else y - conf.winh
in
518 let pages = layout y (conf.winh
*3) in
519 List.iter
render pages;
520 conf.presentation <- presentation;
521 conf.interpagespace <- interpagespace;
527 let y = min
state.maxy y in
528 let pages = layout y conf.winh
in
529 let ready = loadlayout pages in
530 state.ty
<- yratio y;
535 state.layout <- pages;
537 Glut.postRedisplay
();
541 state.layout <- pages;
543 Glut.postRedisplay
();
548 let gotoy_and_clear_text y =
550 if not
conf.verbose
then state.text
<- "";
554 cbput state.hists
.nav
(yratio state.y);
555 cbrfollowlen state.hists
.nav
;
559 let y = cbget state.hists
.nav ~
-1 in
560 truncate
(y *. float state.maxy)
564 let y, h
= getpageyh n in
566 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
569 let gotopage1 n top
=
570 let y = getpagey n in
572 gotoy_and_clear_text (y + top
);
580 state.invalidated
<- state.invalidated
+ 1;
584 let c = c *. state.colorscale
in
590 match state.layout with
592 let rely = yratio state.y in
593 state.maxy <- calcheight ();
594 truncate
(float state.maxy *. rely)
597 state.maxy <- calcheight ();
604 GlMat.mode `projection
;
605 GlMat.load_identity
();
606 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
607 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
608 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
612 GlMat.mode `projection
;
613 GlMat.load_identity
();
614 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
615 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
616 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
621 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
624 GlMat.mode `modelview
;
625 GlMat.load_identity
();
626 GlClear.color
(scalecolor 1.0);
627 GlClear.clear
[`color
];
630 wcmd "geometry" [`i
w; `i h
];
634 GlDraw.color
(0.0, 0.0, 0.0);
636 (0.0, float (conf.winh
- 18))
637 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
639 let font = Glut.BITMAP_8_BY_13
in
640 GlDraw.color
(1.0, 1.0, 1.0);
641 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
642 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
643 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
647 let len = String.length
state.text
in
648 match state.textentry
with
650 if len > 0 then showtext ' '
state.text
652 | Some
(c, text
, _
, _
, _
) ->
656 text ^
" [" ^
state.text ^
"]"
666 state.text
<- Printf.sprintf
"%c%s" c s;
667 Glut.postRedisplay
();
681 state.rects
<- state.rects1
;
682 Glut.postRedisplay
()
685 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
686 state.pagecount
<- n;
687 state.invalidated
<- state.invalidated
- 1;
688 if state.invalidated
= 0
692 let s = Scanf.sscanf
cmd "t %n"
693 (fun n -> String.sub
cmd n (String.length
cmd - n))
695 Glut.setWindowTitle
s
698 let s = Scanf.sscanf
cmd "T %n"
699 (fun n -> String.sub
cmd n (String.length
cmd - n))
701 if state.textentry
= None
708 Glut.postRedisplay
();
714 let s = Scanf.sscanf
cmd "V %n"
715 (fun n -> String.sub
cmd n (String.length
cmd - n))
721 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
722 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
723 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
724 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
726 let y = (getpagey pageno) + truncate y0
in
729 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
732 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
733 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
734 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
735 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
738 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
741 let n, w, h
, r
, l
, p
=
742 Scanf.sscanf
cmd "r %d %d %d %d %d %s"
743 (fun n w h r l p
-> (n, w, h
, r
, l
!= 0, p
))
745 Hashtbl.replace
state.pagemap
(n, w, r
, l
) p
;
746 let opaque = cbpeekw state.pagecache
in
747 if validopaque opaque
751 (fun k v a
-> if v
= opaque then k else a
)
752 state.pagemap
(-1, -1, -1, false)
754 wcmd "free" [`
s opaque];
755 Hashtbl.remove
state.pagemap
k
757 cbput state.pagecache p
;
758 state.rendering
<- false;
760 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
762 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
765 else (ignore
(loadlayout state.layout); preload ())
769 let (n, w, h
, x
) as pdim
=
770 Scanf.sscanf
cmd "l %d %d %d %d" (fun n w h x
-> n, w, h
, x
)
772 state.pdims
<- pdim
:: state.pdims
775 let (l
, n, t
, h
, pos
) =
776 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
778 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
780 let l = String.length
s in
781 let b = Buffer.create
(String.length
s) in
788 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
791 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
800 let outline = (s, l, n, float t
/. float h
) in
802 match state.outlines with
803 | Olist
outlines -> Olist
(outline :: outlines)
804 | Oarray _
-> Olist
[outline]
805 | Onarrow _
-> Olist
[outline]
807 state.outlines <- outlines
810 log "unknown cmd `%S'" cmd
813 let now = Unix.gettimeofday
;;
817 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
822 let y = state.y + conf.scrollincr
in
823 let y = if y >= state.maxy then 0 else y in
829 let cmd = readcmd state.csock
in
836 let onhist cb
= function
837 | HCprev
-> cbget cb ~
-1
838 | HCnext
-> cbget cb
1
839 | HCfirst
-> cbget cb ~
-(cb
.rc)
840 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
843 let search pattern forward
=
844 if String.length pattern
> 0
847 match state.layout with
850 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
853 let b = makecmd "search"
854 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
856 Buffer.add_char
b '
,'
;
857 Buffer.add_string
b pattern
;
858 Buffer.add_char
b '
\000'
;
861 writecmd state.csock
cmd;
864 let intentry text key
=
865 let c = Char.unsafe_chr key
in
868 let s = "x" in s.[0] <- c;
869 let text = text ^
s in
873 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
878 let b = Buffer.create
(String.length
s + 1) in
879 Buffer.add_string
b s;
884 let textentry text key
=
885 let c = Char.unsafe_chr key
in
887 | _
when key
>= 32 && key
< 127 ->
888 let text = addchar text c in
892 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
896 let reinit angle proportional
=
898 conf.proportional
<- proportional
;
900 wcmd "reinit" [`i angle
; `
b proportional
];
903 let optentry text key
=
904 let btos b = if b then "on" else "off" in
905 let c = Char.unsafe_chr key
in
909 try conf.scrollincr
<- int_of_string
s with exc
->
910 state.text <- Printf.sprintf
"bad integer `%s': %s"
911 s (Printexc.to_string exc
)
913 TEswitch
('#'
, "", None
, intentry, ondone)
918 Some
(int_of_string
s)
920 state.text <- Printf.sprintf
"bad integer `%s': %s"
921 s (Printexc.to_string exc
);
924 | Some angle
-> reinit angle
conf.proportional
927 TEswitch
('^'
, "", None
, intentry, ondone)
930 conf.icase
<- not
conf.icase
;
931 TEdone
("case insensitive search " ^
(btos conf.icase
))
934 conf.preload <- not
conf.preload;
936 TEdone
("preload " ^
(btos conf.preload))
939 conf.verbose
<- not
conf.verbose
;
940 TEdone
("verbose " ^
(btos conf.verbose
))
943 conf.maxhfit
<- not
conf.maxhfit
;
944 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
945 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
948 conf.crophack
<- not
conf.crophack
;
949 TEdone
("crophack " ^
btos conf.crophack
)
952 conf.showall
<- not
conf.showall
;
953 TEdone
("showall " ^
btos conf.showall
)
956 conf.underinfo
<- not
conf.underinfo
;
957 TEdone
("underinfo " ^
btos conf.underinfo
)
960 conf.savebmarks
<- not
conf.savebmarks
;
961 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
966 conf.interpagespace <- int_of_string
s;
967 let rely = yratio state.y in
968 state.maxy <- calcheight ();
969 gotoy (truncate
(float state.maxy *. rely));
971 state.text <- Printf.sprintf
"bad integer `%s': %s"
972 s (Printexc.to_string exc
)
974 TEswitch
('
%'
, "", None
, intentry, ondone)
977 reinit conf.angle
(not
conf.proportional
);
978 TEdone
("proprortional display " ^
btos conf.proportional
)
981 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
985 let maxoutlinerows () = (conf.winh
- 31) / 16;;
987 let enterselector allowdel
outlines errmsg msg
=
988 if Array.length
outlines = 0
994 Glut.setCursor
Glut.CURSOR_INHERIT
;
996 match state.layout with
998 | {pageno=pageno} :: rest
-> pageno
1002 if n = Array.length
outlines
1005 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1006 if outlinepageno
>= pageno then n else loop (n+1)
1011 Some
(allowdel
, active,
1012 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1013 Glut.postRedisplay
();
1017 let enteroutlinemode () =
1019 match state.outlines with
1022 let a = Array.of_list
(List.rev
l) in
1023 state.outlines <- Oarray
a;
1025 | Onarrow
(pat
, a, b) ->
1026 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1028 enterselector false outlines "Document has no outline" msg
;
1031 let enterbookmarkmode () =
1032 let bookmarks = Array.of_list
state.bookmarks in
1033 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1036 let quickbookmark ?title
() =
1037 match state.layout with
1043 let sec = Unix.gettimeofday
() in
1044 let tm = Unix.localtime
sec in
1045 Printf.sprintf
"Quick (page %d) visited (%d/%d/%d %d:%d)"
1049 (tm.Unix.tm_year
+ 1900)
1052 | Some
title -> title
1055 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1059 state.fullscreen
<- None
;
1060 Glut.reshapeWindow
w h
;
1063 let writeopen path password
=
1064 writecmd state.csock
1065 ("open " ^ path ^
"\000"
1066 ^
state.password ^
"\000"
1067 ^ string_of_int
conf.angle ^
" "
1068 ^
(if conf.proportional
then "1" else "0"))
1072 let opendoc path password
=
1075 state.password
<- password
;
1076 Hashtbl.clear
state.pagemap
;
1078 writeopen path password
;
1079 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1080 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1083 let viewkeyboard ~key ~x ~
y =
1085 state.textentry <- te
;
1088 Glut.postRedisplay
()
1090 match state.textentry with
1092 let c = Char.chr key
in
1098 let y = getnav () in
1099 gotoy_and_clear_text y
1107 Glut.postRedisplay
()
1110 let ondone isforw
s =
1111 cbput state.hists
.pat
s;
1112 cbrfollowlen state.hists
.pat
;
1113 state.searchpattern
<- s;
1116 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1117 textentry, ondone (c ='
/'
)))
1119 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1120 conf.zoom
<- min
2.2 (conf.zoom
+. 0.1);
1121 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1122 reshape conf.winw
conf.winh
1127 try int_of_string
s with exc
->
1128 state.text <- Printf.sprintf
"bad integer `%s': %s"
1129 s (Printexc.to_string exc
);
1135 state.text <- "page bias is now " ^ string_of_int
n;
1138 enttext (Some
('
+'
, "", None
, intentry, ondone))
1140 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1141 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1142 if conf.zoom
<= 1.0 then state.x
<- 0;
1143 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1144 reshape conf.winw
conf.winh
;
1150 enttext (Some
('
-'
, "", None
, optentry, ondone))
1152 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1155 state.text <- "zoom is 100%";
1156 reshape conf.winw
conf.winh
1158 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1159 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1164 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1165 reshape conf.winw
conf.winh
;
1171 try int_of_string
s with exc
->
1172 state.text <- Printf.sprintf
"bad integer `%s': %s"
1173 s (Printexc.to_string exc
);
1179 cbput state.hists
.pag
(string_of_int
n);
1180 cbrfollowlen state.hists
.pag
;
1181 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1184 let pageentry text key
=
1185 match Char.unsafe_chr key
with
1186 | 'g'
-> TEdone
text
1187 | _
-> intentry text key
1189 let text = "x" in text.[0] <- c;
1190 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1194 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1195 reshape conf.winw
conf.winh
;
1198 conf.hlinks
<- not
conf.hlinks
;
1199 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1200 Glut.postRedisplay
()
1203 conf.autoscroll
<- not
conf.autoscroll
1206 conf.presentation <- not
conf.presentation;
1207 showtext ' '
("presentation mode " ^
1208 if conf.presentation then "on" else "off");
1212 begin match state.fullscreen
with
1214 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1217 state.fullscreen
<- None
;
1222 gotoy_and_clear_text 0
1225 search state.searchpattern
true
1228 search state.searchpattern
false
1231 begin match state.layout with
1234 gotoy_and_clear_text (getpagey l.pageno)
1238 begin match List.rev
state.layout with
1241 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1242 gotoy_and_clear_text (getpagey pageno)
1246 begin match state.layout with
1249 let pageno = max
0 (l.pageno-1) in
1250 gotoy_and_clear_text (getpagey pageno)
1255 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1257 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1259 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1263 else (100. *. (float state.y /. float maxy)) in
1266 Printf.sprintf
"Page %d of %d %.2f%%"
1267 (fn+1) state.pagecount
percent
1270 "Pages %d-%d of %d %.2f%%"
1271 (fn+1) (ln
+1) state.pagecount
percent
1276 begin match state.layout with
1279 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1280 Glut.postRedisplay
();
1284 enterbookmarkmode ()
1288 match state.layout with
1291 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1295 enttext (Some
('~'
, "", None
, textentry, ondone))
1299 showtext ' '
"Quick bookmark added";
1302 begin match state.layout with
1304 let rect = getpdimrect
l.pagedimno
in
1308 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1309 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1311 (truncate
(rect.(1) -. rect.(0)),
1312 truncate
(rect.(3) -. rect.(0)))
1316 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1318 Glut.postRedisplay
();
1324 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1329 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1330 Glut.postRedisplay
()
1332 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1333 | 'j'
-> gotoy (clamp conf.scrollincr
)
1335 | '
r'
-> opendoc state.path
state.password
1338 vlog "huh? %d %c" key
(Char.chr key
);
1341 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1342 let len = String.length
text in
1345 state.textentry <- None
;
1346 Glut.postRedisplay
();
1349 let s = String.sub
text 0 (len - 1) in
1350 enttext (Some
(c, s, onhist, onkey
, ondone))
1353 | Some
(c, text, onhist, onkey
, ondone) ->
1354 begin match Char.unsafe_chr key
with
1357 state.textentry <- None
;
1358 Glut.postRedisplay
()
1361 state.textentry <- None
;
1362 Glut.postRedisplay
()
1365 begin match onkey
text key
with
1367 state.textentry <- None
;
1369 Glut.postRedisplay
()
1372 enttext (Some
(c, text, onhist, onkey
, ondone));
1375 state.textentry <- None
;
1376 Glut.postRedisplay
()
1379 state.textentry <- Some te
;
1380 Glut.postRedisplay
()
1385 let narrow outlines pattern
=
1386 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1390 let rec fold accu n =
1394 let (s, _
, _
, _
) as o
= outlines.(n) in
1396 if (try ignore
(Str.search_forward re
s 0); true
1397 with Not_found
-> false)
1403 let matched = fold [] (Array.length
outlines - 1) in
1404 if matched = [] then None
else Some
(Array.of_list
matched)
1407 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1408 let search active pattern incr
=
1411 if n = Array.length
outlines || n = -1
1414 let (s, _
, _
, _
) = outlines.(n) in
1416 (try ignore
(Str.search_forward re
s 0); true
1417 with Not_found
-> false)
1419 else loop (n + incr
)
1424 let re = Str.regexp_case_fold pattern
in
1430 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1433 if String.length qsearch
= 0
1436 state.outline <- None
;
1437 Glut.postRedisplay
();
1441 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1442 Glut.postRedisplay
();
1446 let incr = if key
= 18 then -1 else 1 in
1448 match search (active + incr) qsearch
incr with
1450 state.text <- qsearch ^
" [not found]";
1453 state.text <- qsearch
;
1454 active, firstof active
1456 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1457 Glut.postRedisplay
();
1460 let len = String.length qsearch
in
1467 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1470 let qsearch = String.sub
qsearch 0 (len - 1) in
1472 match search active qsearch ~
-1 with
1474 state.text <- qsearch ^
" [not found]";
1477 state.text <- qsearch;
1478 active, firstof active
1480 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1482 Glut.postRedisplay
()
1485 if active < Array.length
outlines
1487 let (_
, _
, n, t
) = outlines.(active) in
1491 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1492 state.outline <- None
;
1493 Glut.postRedisplay
();
1495 | _
when key
>= 32 && key
< 127 ->
1496 let pattern = addchar qsearch (Char.chr key
) in
1498 match search active pattern 1 with
1500 state.text <- pattern ^
" [not found]";
1503 state.text <- pattern;
1504 active, firstof active
1506 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1507 Glut.postRedisplay
()
1509 | 14 when not allowdel
-> (* ctrl-n *)
1510 if String.length
qsearch > 0
1512 let optoutlines = narrow outlines qsearch in
1513 begin match optoutlines with
1514 | None
-> state.text <- "can't narrow"
1516 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1517 match state.outlines with
1520 state.outlines <- Onarrow
(qsearch, outlines, a)
1521 | Onarrow
(pat
, a, b) ->
1522 state.outlines <- Onarrow
(qsearch, outlines, b)
1525 Glut.postRedisplay
()
1527 | 21 when not allowdel
-> (* ctrl-u *)
1529 match state.outlines with
1532 let a = Array.of_list
(List.rev
l) in
1533 state.outlines <- Oarray
a;
1535 | Onarrow
(pat
, a, b) ->
1536 state.outlines <- Oarray
b;
1540 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1541 Glut.postRedisplay
()
1545 Some
(allowdel
, active, firstof active, outlines, qsearch);
1546 Glut.postRedisplay
()
1548 | 127 when allowdel
->
1549 let len = Array.length
outlines - 1 in
1552 state.outline <- None
;
1553 state.bookmarks <- [];
1556 let bookmarks = Array.init
len
1558 let i = if i >= active then i + 1 else i in
1569 Glut.postRedisplay
()
1571 | _
-> log "unknown key %d" key
1574 let keyboard ~key ~x ~
y =
1579 match state.outline with
1580 | None
-> viewkeyboard ~key ~x ~
y
1581 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1584 let special ~key ~x ~
y =
1585 match state.outline with
1587 begin match state.textentry with
1591 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1592 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1593 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1594 | Glut.KEY_PAGE_UP
->
1595 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1597 match state.layout with
1599 | l :: _
-> state.y - l.pagey
1602 | Glut.KEY_PAGE_DOWN
->
1603 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1605 match List.rev
state.layout with
1607 | l :: _
-> getpagey l.pageno
1610 | Glut.KEY_HOME
-> addnav (); 0
1613 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1615 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1616 state.x
<- state.x
- 10;
1618 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1619 state.x
<- state.x
+ 10;
1624 gotoy_and_clear_text y
1626 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1629 | Glut.KEY_UP
-> onhist HCprev
1630 | Glut.KEY_DOWN
-> onhist HCnext
1631 | Glut.KEY_HOME
-> onhist HCfirst
1632 | Glut.KEY_END
-> onhist HClast
1635 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1636 Glut.postRedisplay
()
1641 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1642 let maxrows = maxoutlinerows () in
1643 let calcfirst first
active =
1646 let rows = active - first
in
1647 if rows > maxrows then active - maxrows else first
1651 let active = active + incr in
1652 let active = max
0 (min
active (Array.length
outlines - 1)) in
1653 let first = calcfirst first active in
1654 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1655 Glut.postRedisplay
()
1657 let updownlevel incr =
1658 let len = Array.length
outlines in
1659 let (_
, curlevel
, _
, _
) = outlines.(active) in
1661 if i = len then i-1 else if i = -1 then 0 else
1662 let (_
, l, _
, _
) = outlines.(i) in
1663 if l != curlevel
then i else flow (i+incr)
1665 let active = flow active in
1666 let first = calcfirst first active in
1667 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1668 Glut.postRedisplay
()
1671 | Glut.KEY_UP
-> navigate ~
-1
1672 | Glut.KEY_DOWN
-> navigate 1
1673 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1674 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1676 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1677 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1680 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1681 Glut.postRedisplay
()
1684 let active = Array.length
outlines - 1 in
1685 let first = max
0 (active - maxrows) in
1686 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1687 Glut.postRedisplay
()
1692 let drawplaceholder l =
1693 GlDraw.color
(scalecolor 1.0);
1695 (float l.pagex
, float l.pagedispy
)
1696 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1698 let x = float l.pagex
1699 and y = float (l.pagedispy
+ 13) in
1700 let font = Glut.BITMAP_8_BY_13
in
1701 GlDraw.color
(0.0, 0.0, 0.0);
1702 GlPix.raster_pos ~
x ~
y ();
1703 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1704 ("Loading " ^ string_of_int
(l.pageno + 1));
1707 let now () = Unix.gettimeofday
();;
1710 begin match getopaque l.pageno with
1711 | Some
opaque when validopaque opaque ->
1712 if state.textentry = None
1713 then GlDraw.color
(scalecolor 1.0)
1714 else GlDraw.color
(scalecolor 0.4);
1716 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1720 vlog "draw %d %f sec" l.pageno d;
1725 l.pagedispy
+ l.pagevh;
1729 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1730 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1731 let sh = float conf.winh
/. sh in
1732 let sh = max
sh (float conf.scrollh
) in
1735 if state.y = state.maxy
1737 else float y /. float maxy
1739 let position = (float conf.winh
-. sh) *. percent in
1742 if position +. sh > float conf.winh
1743 then float conf.winh
-. sh
1749 let scrollindicator () =
1750 GlDraw.color
(0.64 , 0.64, 0.64);
1752 (float (conf.winw
- conf.scrollw
), 0.)
1753 (float conf.winw
, float conf.winh
)
1755 GlDraw.color
(0.0, 0.0, 0.0);
1757 let position, sh = scrollph state.y in
1759 (float (conf.winw
- conf.scrollw
), position)
1760 (float conf.winw
, position +. sh)
1764 let showsel margin
=
1765 match state.mstate
with
1766 | Mnone
| Mscroll _
| Mpan _
->
1769 | Msel
((x0
, y0
), (x1
, y1
)) ->
1770 let rec loop = function
1772 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1773 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1775 match getopaque l.pageno with
1776 | Some
opaque when validopaque opaque ->
1777 let oy = -l.pagey + l.pagedispy
in
1779 (x0
- margin
- state.x, y0
,
1780 x1
- margin
- state.x, y1
) oy;
1790 let panx = float state.x in
1792 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1793 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1795 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1797 if l.pageno = pageno
1799 let d = float (l.pagedispy
- l.pagey) in
1800 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1801 GlDraw.begins `quads
;
1803 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1804 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1805 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1806 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1816 let showoutline = function
1818 | Some
(allowdel
, active, first, outlines, qsearch) ->
1820 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1821 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1822 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
1825 GlDraw.color
(1., 1., 1.);
1826 let font = Glut.BITMAP_9_BY_15
in
1827 let draw_string x y s =
1828 GlPix.raster_pos ~
x ~
y ();
1829 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1832 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
1835 let (s, l, _
, _
) = outlines.(row
) in
1836 let y = (row
- first) * 16 in
1841 GlDraw.polygon_mode `both `line
;
1842 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1843 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1844 GlDraw.rect (0., float (y + 1))
1845 (float (conf.winw
- 1), float (y + 18));
1846 GlDraw.polygon_mode `both `fill
;
1848 GlDraw.color
(1., 1., 1.);
1850 draw_string (float x) (float (y + 16)) s;
1858 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1859 GlDraw.viewport
margin 0 state.w conf.winh
;
1861 GlClear.color
(scalecolor 0.5);
1862 GlClear.clear
[`color
];
1865 let x = float state.x in
1866 GlMat.translate ~
x ();
1870 Gl.enable `scissor_test
;
1871 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
1873 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1876 Gl.disable `scissor_test
1880 let x = -.float state.x in
1881 GlMat.translate ~
x ();
1885 GlDraw.viewport
0 0 conf.winw
conf.winh
;
1888 showoutline state.outline;
1890 Glut.swapBuffers
();
1894 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1895 let x = x - margin - state.x in
1896 let rec f = function
1898 begin match getopaque l.pageno with
1899 | Some
opaque when validopaque opaque ->
1900 let y = y - l.pagedispy
in
1903 let y = l.pagey + y in
1904 let x = x - l.pagex
in
1905 match whatsunder
opaque x y with
1918 let mouse ~button ~bstate ~
x ~
y =
1920 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1928 let incr = incr * 2 in
1929 let y = clamp incr in
1930 gotoy_and_clear_text y
1932 | Glut.LEFT_BUTTON
when state.outline = None
1933 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1934 if bstate
= Glut.DOWN
1936 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1937 state.mstate
<- Mpan
(x, y)
1940 state.mstate
<- Mnone
1943 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
1944 if bstate
= Glut.DOWN
1946 let position, sh = scrollph state.y in
1947 if y > truncate
position && y < truncate
(position +. sh)
1949 state.mstate
<- Mscroll
1951 let percent = float y /. float conf.winh
in
1952 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
1954 state.mstate
<- Mscroll
1956 state.mstate
<- Mnone
1958 | Glut.LEFT_BUTTON
when state.outline = None
->
1959 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1960 begin match dest with
1961 | Ulinkgoto
(pageno, top
) ->
1964 gotopage1 pageno top
1969 | Unone
when bstate
= Glut.DOWN
->
1970 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1971 state.mstate
<- Mpan
(x, y);
1973 | Unone
| Utext _
->
1974 if bstate
= Glut.DOWN
1976 if conf.angle
mod 360 = 0
1978 state.mstate
<- Msel
((x, y), (x, y));
1979 Glut.postRedisplay
()
1983 match state.mstate
with
1987 state.mstate
<- Mnone
1990 Glut.setCursor
Glut.CURSOR_INHERIT
;
1991 state.mstate
<- Mnone
1993 | Msel
((x0
, y0
), (x1
, y1
)) ->
1995 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1996 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1998 match getopaque l.pageno with
1999 | Some
opaque when validopaque opaque ->
2003 List.iter
f state.layout;
2004 copysel
""; (* ugly *)
2005 Glut.setCursor
Glut.CURSOR_INHERIT
;
2006 state.mstate
<- Mnone
;
2013 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2016 if state.outline = None
2018 match state.mstate
with
2024 state.mstate
<- Mpan
(x, y);
2025 if conf.zoom > 1.0 then state.x <- state.x + dx;
2027 gotoy_and_clear_text y
2030 state.mstate
<- Msel
(a, (x, y));
2031 Glut.postRedisplay
()
2034 let y = min
conf.winh
(max
0 y) in
2035 let percent = float y /. float conf.winh
in
2036 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2037 gotoy_and_clear_text y
2041 if state.outline = None
2043 match state.mstate
with
2045 begin match getunder x y with
2046 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2048 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2049 Glut.setCursor
Glut.CURSOR_INFO
2050 | Ulinkgoto
(page
, y) ->
2051 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2052 Glut.setCursor
Glut.CURSOR_INFO
2054 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2055 Glut.setCursor
Glut.CURSOR_TEXT
2058 | Mpan _
| Msel _
| Mscroll
->
2068 match Sys.os_type
with
2069 | "Win32" -> Sys.getenv
"HOMEPATH"
2070 | _
-> Sys.getenv
"HOME"
2073 ("Can not determine home directory location: " ^
2074 Printexc.to_string exn
);
2078 let config_of c attrs
=
2082 | "scroll-bar-width" -> { c with scrollw
= int_of_string v
}
2083 | "scroll-handle-height" -> { c with scrollh
= int_of_string v
}
2084 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2085 | "preload" -> { c with preload = bool_of_string v
}
2086 | "page-bias" -> { c with pagebias
= int_of_string v
}
2087 | "scroll-step" -> { c with scrollincr
= int_of_string v
}
2088 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2089 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2090 | "throttle" -> { c with showall
= bool_of_string v
}
2091 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2092 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2093 | "vertical-margin" -> { c with interpagespace = int_of_string v
}
2095 let zoom = float_of_string v
/. 100. in
2096 let zoom = max
0.1 (min
2.2 zoom) in
2097 { c with zoom = zoom }
2098 | "presentation" -> { c with presentation = bool_of_string v
}
2099 | "rotation-angle" -> { c with angle
= int_of_string v
}
2100 | "width" -> { c with winw
= int_of_string v
}
2101 | "height" -> { c with winh
= int_of_string v
}
2102 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2103 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2106 prerr_endline
("Error processing attribute (`" ^
2107 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2110 let rec fold c = function
2113 let c = apply c k v
in
2119 let bookmark_of attrs
=
2120 let rec fold title page
rely = function
2121 | ("title", v
) :: rest
-> fold v page
rely rest
2122 | ("page", v
) :: rest
-> fold title v
rely rest
2123 | ("rely", v
) :: rest
-> fold title page v rest
2124 | _
:: rest
-> fold title page
rely rest
2125 | [] -> title, page
, rely
2127 fold "invalid" "0" "0" attrs
2130 let setconf dst src
=
2131 dst
.scrollw
<- src
.scrollw
;
2132 dst
.scrollh
<- src
.scrollh
;
2133 dst
.icase
<- src
.icase
;
2134 dst
.preload <- src
.preload;
2135 dst
.pagebias
<- src
.pagebias
;
2136 dst
.verbose
<- src
.verbose
;
2137 dst
.scrollincr
<- src
.scrollincr
;
2138 dst
.maxhfit
<- src
.maxhfit
;
2139 dst
.crophack
<- src
.crophack
;
2140 dst
.autoscroll
<- src
.autoscroll
;
2141 dst
.showall
<- src
.showall
;
2142 dst
.hlinks
<- src
.hlinks
;
2143 dst
.underinfo
<- src
.underinfo
;
2144 dst
.interpagespace <- src
.interpagespace;
2145 dst
.zoom <- src
.zoom;
2146 dst
.presentation <- src
.presentation;
2147 dst
.angle
<- src
.angle
;
2148 dst
.winw
<- src
.winw
;
2149 dst
.winh
<- src
.winh
;
2150 dst
.savebmarks
<- src
.savebmarks
;
2151 dst
.proportional
<- src
.proportional
;
2155 let l = String.length
s in
2156 let b = Buffer.create
l in
2162 let h = Hashtbl.create
10 in
2163 let dc = { defconf with angle
= defconf.angle
} in
2164 let rec toplevel v t spos epos
=
2166 | Vdata
| Vcdata
| Vend
-> v
2167 | Vopen
("llppconfig", attrs
, closed
) ->
2170 else { v
with f = llppconfig
}
2172 error
"unexpected subelement at top level" s spos
2173 | Vclose tag
-> error
"unexpected close at toplevel" s spos
2175 and llppconfig v t spos epos
=
2177 | Vdata
| Vcdata
| Vend
-> v
2178 | Vopen
("defaults", attrs
, closed
) ->
2179 let c = config_of dc attrs
in
2183 else { v
with f = skip
"defaults" (fun () -> v
) }
2185 | Vopen
("doc", attrs
, closed
) ->
2188 List.assoc
"path" attrs
2189 with Not_found
-> error
"doc is missing path attribute" s spos
2191 let path = unent pathent in
2192 let c = config_of dc attrs
in
2195 float_of_string
(List.assoc
"rely" attrs
)
2199 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2204 int_of_string
(List.assoc
"pan" attrs
)
2208 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2212 then (Hashtbl.add
h path (c, [], x, y); v
)
2213 else { v
with f = doc
path x y c [] }
2215 | Vopen
(tag
, _
, closed
) ->
2216 error
"unexpected subelement in llppconfig" s spos
2218 | Vclose
"llppconfig" -> { v
with f = toplevel }
2219 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2221 and doc
path x y c bookmarks v t spos epos
=
2223 | Vdata
| Vcdata
-> v
2224 | Vend
-> error
"unexpected end of input in doc" s spos
2225 | Vopen
("bookmarks", attrs
, closed
) ->
2226 { v
with f = pbookmarks
path x y c bookmarks }
2228 | Vopen
(tag
, _
, _
) ->
2229 error
"unexpected subelement in doc" s spos
2232 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2233 { v
with f = llppconfig
}
2235 | Vclose tag
-> error
"unexpected close in doc" s spos
2237 and pbookmarks
path x y c bookmarks v t spos epos
=
2239 | Vdata
| Vcdata
-> v
2240 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2241 | Vopen
("item", attrs
, closed
) ->
2242 let titleent, spage
, srely
= bookmark_of attrs
in
2247 dolog "Failed to convert page %S to integer: %s"
2248 spage
(Printexc.to_string exn
);
2253 float_of_string srely
2255 dolog "Failed to convert rely %S to real: %s"
2256 srely
(Printexc.to_string exn
);
2259 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2261 then { v
with f = pbookmarks
path x y c bookmarks }
2264 { v
with f = skip
"item" f }
2267 error
"unexpected subelement in bookmarks" s spos
2269 | Vclose
"bookmarks" ->
2270 { v
with f = doc
path x y c bookmarks }
2272 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2274 and skip tag
f v t spos epos
=
2276 | Vdata
| Vcdata
-> v
2278 error
("unexpected end of input in skipped " ^ tag
) s spos
2279 | Vopen
(tag'
, _
, closed
) ->
2283 let f'
() = { v
with f = skip tag
f } in
2284 { v
with f = skip tag'
f'
}
2288 else error
("unexpected close in skipped " ^ tag
) s spos
2291 parse
{ f = toplevel; accu = () } s;
2297 let len = in_channel_length ic
in
2298 let s = String.create
len in
2299 really_input ic
s 0 len;
2302 | Parse_error
(msg
, s, pos
) ->
2303 let subs = subs s pos
in
2304 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2305 failwith
("parse error: " ^
s)
2308 failwith
("config load error: " ^
Printexc.to_string exn
)
2314 let dir = Filename.concat
home ".config" in
2315 if Sys.is_directory
dir then dir else home
2318 Filename.concat
dir "llpp.conf"
2322 if Sys.file_exists
path
2325 (try Some
(open_in_bin
path)
2328 ("Error opening configuation file `" ^
path ^
"': " ^
2329 Printexc.to_string exn
);
2338 ("Error loading configuation from `" ^
path ^
"': " ^
2339 Printexc.to_string exn
);
2345 f (Hashtbl.create
0, defconf)
2350 let pc, pb
, px
, py =
2352 Hashtbl.find
h state.path
2353 with Not_found
-> dc, [], 0, 0.0
2357 state.bookmarks <- pb
;
2359 cbput state.hists
.nav
py;
2360 cbrfollowlen state.hists
.nav
;
2365 let add_attrs bb always
dc c =
2368 then Printf.bprintf bb
"\n %s='%b'" s a
2371 then Printf.bprintf bb
"\n %s='%d'" s a
2374 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2378 then dc.winw
, dc.winh
2380 match state.fullscreen
with
2382 | None
-> c.winw
, c.winh
2384 oi
"width" w dc.winw
;
2385 oi
"height" h dc.winh
;
2386 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2387 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2388 ob "case-insensitive-search" c.icase
dc.icase
;
2389 ob "preload" c.preload dc.preload;
2390 oi
"page-bias" c.pagebias
dc.pagebias
;
2391 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2392 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2393 ob "crop-hack" c.crophack
dc.crophack
;
2394 ob "throttle" c.showall
dc.showall
;
2395 ob "highlight-links" c.hlinks
dc.hlinks
;
2396 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2397 oi
"vertical-margin" c.interpagespace dc.interpagespace;
2398 oz
"zoom" c.zoom dc.zoom;
2399 ob "presentation" c.presentation dc.presentation;
2400 oi
"rotation-angle" c.angle
dc.angle
;
2401 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2402 ob "proportional-display" c.proportional
dc.proportional
;
2406 let bb = Buffer.create
32768 in
2408 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2409 add_attrs bb true dc dc;
2410 Buffer.add_string
bb "/>\n";
2412 let adddoc path x y c bookmarks =
2413 if bookmarks == [] && c = dc && y = 0.0
2416 Printf.bprintf
bb "<doc path='%s'"
2417 (enent
path 0 (String.length
path));
2420 then Printf.bprintf
bb " rely='%f'" y;
2423 then Printf.bprintf
bb " pan='%d'" x;
2425 add_attrs bb false dc c;
2427 begin match bookmarks with
2428 | [] -> Buffer.add_string
bb "/>\n"
2430 Buffer.add_string
bb ">\n<bookmarks>\n";
2431 List.iter
(fun (title, _level
, page, rely) ->
2433 "<item title='%s' page='%d' rely='%f'/>\n"
2434 (enent
title 0 (String.length
title))
2438 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2443 adddoc state.path state.x (yratio state.y) conf
2444 (if conf.savebmarks
then state.bookmarks else []);
2446 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2447 if path <> state.path
2449 adddoc path x y c bookmarks
2451 Buffer.add_string
bb "</llppconfig>";
2454 if Buffer.length
bb > 0
2457 let tmp = path ^
".tmp" in
2458 let oc = open_out_bin
tmp in
2459 Buffer.output_buffer
oc bb;
2461 Sys.rename
tmp path;
2464 ("error while saving configuration: " ^
Printexc.to_string exn
)
2470 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2471 (fun s -> state.path <- s)
2472 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2475 if String.length
state.path = 0
2476 then (prerr_endline
"filename missing"; exit
1)
2478 if Filename.is_relative
state.path
2479 then Filename.concat
(Sys.getcwd
()) state.path
2487 let _ = Glut.init
Sys.argv
in
2488 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2489 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2490 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2493 if Sys.os_type
= "Unix"
2495 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2497 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2498 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2499 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2500 Unix.bind
sock addr;
2502 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2503 Unix.connect
csock addr;
2504 let ssock, _ = Unix.accept
sock in
2507 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2508 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2512 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2516 let () = Glut.displayFunc
display in
2517 let () = Glut.reshapeFunc
reshape in
2518 let () = Glut.keyboardFunc
keyboard in
2519 let () = Glut.specialFunc
special in
2520 let () = Glut.idleFunc
(Some
idle) in
2521 let () = Glut.mouseFunc
mouse in
2522 let () = Glut.motionFunc
motion in
2523 let () = Glut.passiveMotionFunc
pmotion in
2526 state.csock <- csock;
2527 state.ssock <- ssock;
2528 state.text <- "Opening " ^
path;
2529 writeopen state.path state.password
;
2533 let rec handlelablglutbug () =
2536 with Glut.BadEnum
"key in special_of_int" ->
2537 showtext '
!'
" LablGlut bug: special key not recognized";
2538 handlelablglutbug ()
2540 handlelablglutbug ();