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
625 GlMat.mode `modelview
;
626 GlMat.load_identity
();
627 GlClear.color
(scalecolor 1.0);
628 GlClear.clear
[`color
];
631 wcmd "geometry" [`i
w; `i h
];
635 GlDraw.color
(0.0, 0.0, 0.0);
637 (0.0, float (conf.winh
- 18))
638 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
640 let font = Glut.BITMAP_8_BY_13
in
641 GlDraw.color
(1.0, 1.0, 1.0);
642 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
643 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
644 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
648 let len = String.length
state.text
in
649 match state.textentry
with
651 if len > 0 then showtext ' '
state.text
653 | Some
(c, text
, _
, _
, _
) ->
657 text ^
" [" ^
state.text ^
"]"
667 state.text
<- Printf.sprintf
"%c%s" c s;
668 Glut.postRedisplay
();
682 state.rects
<- state.rects1
;
683 Glut.postRedisplay
()
686 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
687 state.pagecount
<- n;
688 state.invalidated
<- state.invalidated
- 1;
689 if state.invalidated
= 0
693 let s = Scanf.sscanf
cmd "t %n"
694 (fun n -> String.sub
cmd n (String.length
cmd - n))
696 Glut.setWindowTitle
s
699 let s = Scanf.sscanf
cmd "T %n"
700 (fun n -> String.sub
cmd n (String.length
cmd - n))
702 if state.textentry
= None
709 Glut.postRedisplay
();
715 let s = Scanf.sscanf
cmd "V %n"
716 (fun n -> String.sub
cmd n (String.length
cmd - n))
722 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
723 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
724 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
725 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
727 let y = (getpagey pageno) + truncate y0
in
730 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
733 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
734 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
735 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
736 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
739 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
742 let n, w, h
, r
, l
, p
=
743 Scanf.sscanf
cmd "r %d %d %d %d %d %s"
744 (fun n w h r l p
-> (n, w, h
, r
, l
!= 0, p
))
746 Hashtbl.replace
state.pagemap
(n, w, r
, l
) p
;
747 let opaque = cbpeekw state.pagecache
in
748 if validopaque opaque
752 (fun k v a
-> if v
= opaque then k else a
)
753 state.pagemap
(-1, -1, -1, false)
755 wcmd "free" [`
s opaque];
756 Hashtbl.remove
state.pagemap
k
758 cbput state.pagecache p
;
759 state.rendering
<- false;
761 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
763 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
766 else (ignore
(loadlayout state.layout); preload ())
770 let (n, w, h
, x
) as pdim
=
771 Scanf.sscanf
cmd "l %d %d %d %d" (fun n w h x
-> n, w, h
, x
)
773 state.pdims
<- pdim
:: state.pdims
776 let (l
, n, t
, h
, pos
) =
777 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
779 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
781 let l = String.length
s in
782 let b = Buffer.create
(String.length
s) in
789 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
792 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
801 let outline = (s, l, n, float t
/. float h
) in
803 match state.outlines with
804 | Olist
outlines -> Olist
(outline :: outlines)
805 | Oarray _
-> Olist
[outline]
806 | Onarrow _
-> Olist
[outline]
808 state.outlines <- outlines
811 log "unknown cmd `%S'" cmd
814 let now = Unix.gettimeofday
;;
818 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
823 let y = state.y + conf.scrollincr
in
824 let y = if y >= state.maxy then 0 else y in
830 let cmd = readcmd state.csock
in
837 let onhist cb
= function
838 | HCprev
-> cbget cb ~
-1
839 | HCnext
-> cbget cb
1
840 | HCfirst
-> cbget cb ~
-(cb
.rc)
841 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
844 let search pattern forward
=
845 if String.length pattern
> 0
848 match state.layout with
851 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
854 let b = makecmd "search"
855 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
857 Buffer.add_char
b '
,'
;
858 Buffer.add_string
b pattern
;
859 Buffer.add_char
b '
\000'
;
862 writecmd state.csock
cmd;
865 let intentry text key
=
866 let c = Char.unsafe_chr key
in
869 let s = "x" in s.[0] <- c;
870 let text = text ^
s in
874 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
879 let b = Buffer.create
(String.length
s + 1) in
880 Buffer.add_string
b s;
885 let textentry text key
=
886 let c = Char.unsafe_chr key
in
888 | _
when key
>= 32 && key
< 127 ->
889 let text = addchar text c in
893 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
897 let reinit angle proportional
=
899 conf.proportional
<- proportional
;
901 wcmd "reinit" [`i angle
; `
b proportional
];
904 let optentry text key
=
905 let btos b = if b then "on" else "off" in
906 let c = Char.unsafe_chr key
in
910 try conf.scrollincr
<- int_of_string
s with exc
->
911 state.text <- Printf.sprintf
"bad integer `%s': %s"
912 s (Printexc.to_string exc
)
914 TEswitch
('#'
, "", None
, intentry, ondone)
919 Some
(int_of_string
s)
921 state.text <- Printf.sprintf
"bad integer `%s': %s"
922 s (Printexc.to_string exc
);
925 | Some angle
-> reinit angle
conf.proportional
928 TEswitch
('^'
, "", None
, intentry, ondone)
931 conf.icase
<- not
conf.icase
;
932 TEdone
("case insensitive search " ^
(btos conf.icase
))
935 conf.preload <- not
conf.preload;
937 TEdone
("preload " ^
(btos conf.preload))
940 conf.verbose
<- not
conf.verbose
;
941 TEdone
("verbose " ^
(btos conf.verbose
))
944 conf.maxhfit
<- not
conf.maxhfit
;
945 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
946 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
949 conf.crophack
<- not
conf.crophack
;
950 TEdone
("crophack " ^
btos conf.crophack
)
953 conf.showall
<- not
conf.showall
;
954 TEdone
("showall " ^
btos conf.showall
)
957 conf.underinfo
<- not
conf.underinfo
;
958 TEdone
("underinfo " ^
btos conf.underinfo
)
961 conf.savebmarks
<- not
conf.savebmarks
;
962 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
967 conf.interpagespace <- int_of_string
s;
968 let rely = yratio state.y in
969 state.maxy <- calcheight ();
970 gotoy (truncate
(float state.maxy *. rely));
972 state.text <- Printf.sprintf
"bad integer `%s': %s"
973 s (Printexc.to_string exc
)
975 TEswitch
('
%'
, "", None
, intentry, ondone)
978 reinit conf.angle
(not
conf.proportional
);
979 TEdone
("proprortional display " ^
btos conf.proportional
)
982 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
986 let maxoutlinerows () = (conf.winh
- 31) / 16;;
988 let enterselector allowdel
outlines errmsg msg
=
989 if Array.length
outlines = 0
995 Glut.setCursor
Glut.CURSOR_INHERIT
;
997 match state.layout with
999 | {pageno=pageno} :: rest
-> pageno
1003 if n = Array.length
outlines
1006 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
1007 if outlinepageno
>= pageno then n else loop (n+1)
1012 Some
(allowdel
, active,
1013 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1014 Glut.postRedisplay
();
1018 let enteroutlinemode () =
1020 match state.outlines with
1023 let a = Array.of_list
(List.rev
l) in
1024 state.outlines <- Oarray
a;
1026 | Onarrow
(pat
, a, b) ->
1027 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1029 enterselector false outlines "Document has no outline" msg
;
1032 let enterbookmarkmode () =
1033 let bookmarks = Array.of_list
state.bookmarks in
1034 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1037 let quickbookmark ?title
() =
1038 match state.layout with
1044 let sec = Unix.gettimeofday
() in
1045 let tm = Unix.localtime
sec in
1046 Printf.sprintf
"Quick (page %d) visited (%d/%d/%d %d:%d)"
1050 (tm.Unix.tm_year
+ 1900)
1053 | Some
title -> title
1056 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1060 state.fullscreen
<- None
;
1061 Glut.reshapeWindow
w h
;
1064 let writeopen path password
=
1065 writecmd state.csock
1066 ("open " ^ path ^
"\000"
1067 ^
state.password ^
"\000"
1068 ^ string_of_int
conf.angle ^
" "
1069 ^
(if conf.proportional
then "1" else "0"))
1073 let opendoc path password
=
1076 state.password
<- password
;
1077 Hashtbl.clear
state.pagemap
;
1079 writeopen path password
;
1080 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1081 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1084 let viewkeyboard ~key ~x ~
y =
1086 state.textentry <- te
;
1089 Glut.postRedisplay
()
1091 match state.textentry with
1093 let c = Char.chr key
in
1099 let y = getnav () in
1100 gotoy_and_clear_text y
1108 Glut.postRedisplay
()
1111 let ondone isforw
s =
1112 cbput state.hists
.pat
s;
1113 cbrfollowlen state.hists
.pat
;
1114 state.searchpattern
<- s;
1117 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1118 textentry, ondone (c ='
/'
)))
1120 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1121 conf.zoom
<- min
2.2 (conf.zoom
+. 0.1);
1122 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1123 reshape conf.winw
conf.winh
1128 try int_of_string
s with exc
->
1129 state.text <- Printf.sprintf
"bad integer `%s': %s"
1130 s (Printexc.to_string exc
);
1136 state.text <- "page bias is now " ^ string_of_int
n;
1139 enttext (Some
('
+'
, "", None
, intentry, ondone))
1141 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1142 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1143 if conf.zoom
<= 1.0 then state.x
<- 0;
1144 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1145 reshape conf.winw
conf.winh
;
1151 enttext (Some
('
-'
, "", None
, optentry, ondone))
1153 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1156 state.text <- "zoom is 100%";
1157 reshape conf.winw
conf.winh
1159 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1160 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1165 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1166 reshape conf.winw
conf.winh
;
1172 try int_of_string
s with exc
->
1173 state.text <- Printf.sprintf
"bad integer `%s': %s"
1174 s (Printexc.to_string exc
);
1180 cbput state.hists
.pag
(string_of_int
n);
1181 cbrfollowlen state.hists
.pag
;
1182 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1185 let pageentry text key
=
1186 match Char.unsafe_chr key
with
1187 | 'g'
-> TEdone
text
1188 | _
-> intentry text key
1190 let text = "x" in text.[0] <- c;
1191 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1195 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1196 reshape conf.winw
conf.winh
;
1199 conf.hlinks
<- not
conf.hlinks
;
1200 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1201 Glut.postRedisplay
()
1204 conf.autoscroll
<- not
conf.autoscroll
1207 conf.presentation <- not
conf.presentation;
1208 showtext ' '
("presentation mode " ^
1209 if conf.presentation then "on" else "off");
1213 begin match state.fullscreen
with
1215 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1218 state.fullscreen
<- None
;
1223 gotoy_and_clear_text 0
1226 search state.searchpattern
true
1229 search state.searchpattern
false
1232 begin match state.layout with
1235 gotoy_and_clear_text (getpagey l.pageno)
1239 begin match List.rev
state.layout with
1242 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1243 gotoy_and_clear_text (getpagey pageno)
1247 begin match state.layout with
1250 let pageno = max
0 (l.pageno-1) in
1251 gotoy_and_clear_text (getpagey pageno)
1256 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1258 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1260 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1264 else (100. *. (float state.y /. float maxy)) in
1267 Printf.sprintf
"Page %d of %d %.2f%%"
1268 (fn+1) state.pagecount
percent
1271 "Pages %d-%d of %d %.2f%%"
1272 (fn+1) (ln
+1) state.pagecount
percent
1277 begin match state.layout with
1280 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1281 Glut.postRedisplay
();
1285 enterbookmarkmode ()
1289 match state.layout with
1292 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1296 enttext (Some
('~'
, "", None
, textentry, ondone))
1300 showtext ' '
"Quick bookmark added";
1303 begin match state.layout with
1305 let rect = getpdimrect
l.pagedimno
in
1309 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1310 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1312 (truncate
(rect.(1) -. rect.(0)),
1313 truncate
(rect.(3) -. rect.(0)))
1317 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1319 Glut.postRedisplay
();
1325 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1330 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1331 Glut.postRedisplay
()
1333 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1334 | 'j'
-> gotoy (clamp conf.scrollincr
)
1336 | '
r'
-> opendoc state.path
state.password
1339 vlog "huh? %d %c" key
(Char.chr key
);
1342 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1343 let len = String.length
text in
1346 state.textentry <- None
;
1347 Glut.postRedisplay
();
1350 let s = String.sub
text 0 (len - 1) in
1351 enttext (Some
(c, s, onhist, onkey
, ondone))
1354 | Some
(c, text, onhist, onkey
, ondone) ->
1355 begin match Char.unsafe_chr key
with
1358 state.textentry <- None
;
1359 Glut.postRedisplay
()
1362 state.textentry <- None
;
1363 Glut.postRedisplay
()
1366 begin match onkey
text key
with
1368 state.textentry <- None
;
1370 Glut.postRedisplay
()
1373 enttext (Some
(c, text, onhist, onkey
, ondone));
1376 state.textentry <- None
;
1377 Glut.postRedisplay
()
1380 state.textentry <- Some te
;
1381 Glut.postRedisplay
()
1386 let narrow outlines pattern
=
1387 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1391 let rec fold accu n =
1395 let (s, _
, _
, _
) as o
= outlines.(n) in
1397 if (try ignore
(Str.search_forward re
s 0); true
1398 with Not_found
-> false)
1404 let matched = fold [] (Array.length
outlines - 1) in
1405 if matched = [] then None
else Some
(Array.of_list
matched)
1408 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1409 let search active pattern incr
=
1412 if n = Array.length
outlines || n = -1
1415 let (s, _
, _
, _
) = outlines.(n) in
1417 (try ignore
(Str.search_forward re
s 0); true
1418 with Not_found
-> false)
1420 else loop (n + incr
)
1425 let re = Str.regexp_case_fold pattern
in
1431 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1434 if String.length qsearch
= 0
1437 state.outline <- None
;
1438 Glut.postRedisplay
();
1442 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1443 Glut.postRedisplay
();
1447 let incr = if key
= 18 then -1 else 1 in
1449 match search (active + incr) qsearch
incr with
1451 state.text <- qsearch ^
" [not found]";
1454 state.text <- qsearch
;
1455 active, firstof active
1457 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1458 Glut.postRedisplay
();
1461 let len = String.length qsearch
in
1468 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1471 let qsearch = String.sub
qsearch 0 (len - 1) in
1473 match search active qsearch ~
-1 with
1475 state.text <- qsearch ^
" [not found]";
1478 state.text <- qsearch;
1479 active, firstof active
1481 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1483 Glut.postRedisplay
()
1486 if active < Array.length
outlines
1488 let (_
, _
, n, t
) = outlines.(active) in
1492 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1493 state.outline <- None
;
1494 Glut.postRedisplay
();
1496 | _
when key
>= 32 && key
< 127 ->
1497 let pattern = addchar qsearch (Char.chr key
) in
1499 match search active pattern 1 with
1501 state.text <- pattern ^
" [not found]";
1504 state.text <- pattern;
1505 active, firstof active
1507 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1508 Glut.postRedisplay
()
1510 | 14 when not allowdel
-> (* ctrl-n *)
1511 if String.length
qsearch > 0
1513 let optoutlines = narrow outlines qsearch in
1514 begin match optoutlines with
1515 | None
-> state.text <- "can't narrow"
1517 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1518 match state.outlines with
1521 state.outlines <- Onarrow
(qsearch, outlines, a)
1522 | Onarrow
(pat
, a, b) ->
1523 state.outlines <- Onarrow
(qsearch, outlines, b)
1526 Glut.postRedisplay
()
1528 | 21 when not allowdel
-> (* ctrl-u *)
1530 match state.outlines with
1533 let a = Array.of_list
(List.rev
l) in
1534 state.outlines <- Oarray
a;
1536 | Onarrow
(pat
, a, b) ->
1537 state.outlines <- Oarray
b;
1541 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1542 Glut.postRedisplay
()
1546 Some
(allowdel
, active, firstof active, outlines, qsearch);
1547 Glut.postRedisplay
()
1549 | 127 when allowdel
->
1550 let len = Array.length
outlines - 1 in
1553 state.outline <- None
;
1554 state.bookmarks <- [];
1557 let bookmarks = Array.init
len
1559 let i = if i >= active then i + 1 else i in
1570 Glut.postRedisplay
()
1572 | _
-> log "unknown key %d" key
1575 let keyboard ~key ~x ~
y =
1580 match state.outline with
1581 | None
-> viewkeyboard ~key ~x ~
y
1582 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1585 let special ~key ~x ~
y =
1586 match state.outline with
1588 begin match state.textentry with
1592 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1593 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1594 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1595 | Glut.KEY_PAGE_UP
->
1596 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1598 match state.layout with
1600 | l :: _
-> state.y - l.pagey
1603 | Glut.KEY_PAGE_DOWN
->
1604 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1606 match List.rev
state.layout with
1608 | l :: _
-> getpagey l.pageno
1611 | Glut.KEY_HOME
-> addnav (); 0
1614 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1616 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1617 state.x
<- state.x
- 10;
1619 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1620 state.x
<- state.x
+ 10;
1625 gotoy_and_clear_text y
1627 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1630 | Glut.KEY_UP
-> onhist HCprev
1631 | Glut.KEY_DOWN
-> onhist HCnext
1632 | Glut.KEY_HOME
-> onhist HCfirst
1633 | Glut.KEY_END
-> onhist HClast
1636 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1637 Glut.postRedisplay
()
1642 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1643 let maxrows = maxoutlinerows () in
1644 let calcfirst first
active =
1647 let rows = active - first
in
1648 if rows > maxrows then active - maxrows else first
1652 let active = active + incr in
1653 let active = max
0 (min
active (Array.length
outlines - 1)) in
1654 let first = calcfirst first active in
1655 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1656 Glut.postRedisplay
()
1658 let updownlevel incr =
1659 let len = Array.length
outlines in
1660 let (_
, curlevel
, _
, _
) = outlines.(active) in
1662 if i = len then i-1 else if i = -1 then 0 else
1663 let (_
, l, _
, _
) = outlines.(i) in
1664 if l != curlevel
then i else flow (i+incr)
1666 let active = flow active in
1667 let first = calcfirst first active in
1668 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1669 Glut.postRedisplay
()
1672 | Glut.KEY_UP
-> navigate ~
-1
1673 | Glut.KEY_DOWN
-> navigate 1
1674 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1675 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1677 | Glut.KEY_RIGHT
when not allowdel
-> updownlevel 1
1678 | Glut.KEY_LEFT
when not allowdel
-> updownlevel ~
-1
1681 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1682 Glut.postRedisplay
()
1685 let active = Array.length
outlines - 1 in
1686 let first = max
0 (active - maxrows) in
1687 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1688 Glut.postRedisplay
()
1693 let drawplaceholder l =
1694 GlDraw.color
(scalecolor 1.0);
1696 (float l.pagex
, float l.pagedispy
)
1697 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1699 let x = float l.pagex
1700 and y = float (l.pagedispy
+ 13) in
1701 let font = Glut.BITMAP_8_BY_13
in
1702 GlDraw.color
(0.0, 0.0, 0.0);
1703 GlPix.raster_pos ~
x ~
y ();
1704 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1705 ("Loading " ^ string_of_int
(l.pageno + 1));
1708 let now () = Unix.gettimeofday
();;
1711 begin match getopaque l.pageno with
1712 | Some
opaque when validopaque opaque ->
1713 if state.textentry = None
1714 then GlDraw.color
(scalecolor 1.0)
1715 else GlDraw.color
(scalecolor 0.4);
1717 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1721 vlog "draw %d %f sec" l.pageno d;
1726 l.pagedispy
+ l.pagevh;
1730 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1731 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1732 let sh = float conf.winh
/. sh in
1733 let sh = max
sh (float conf.scrollh
) in
1736 if state.y = state.maxy
1738 else float y /. float maxy
1740 let position = (float conf.winh
-. sh) *. percent in
1743 if position +. sh > float conf.winh
1744 then float conf.winh
-. sh
1750 let scrollindicator () =
1751 GlDraw.color
(0.64 , 0.64, 0.64);
1753 (float (conf.winw
- conf.scrollw
), 0.)
1754 (float conf.winw
, float conf.winh
)
1756 GlDraw.color
(0.0, 0.0, 0.0);
1758 let position, sh = scrollph state.y in
1760 (float (conf.winw
- conf.scrollw
), position)
1761 (float conf.winw
, position +. sh)
1765 let showsel margin
=
1766 match state.mstate
with
1767 | Mnone
| Mscroll _
| Mpan _
->
1770 | Msel
((x0
, y0
), (x1
, y1
)) ->
1771 let rec loop = function
1773 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1774 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1776 match getopaque l.pageno with
1777 | Some
opaque when validopaque opaque ->
1778 let oy = -l.pagey + l.pagedispy
in
1780 (x0
- margin
- state.x, y0
,
1781 x1
- margin
- state.x, y1
) oy;
1791 let panx = float state.x in
1793 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1794 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1796 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1798 if l.pageno = pageno
1800 let d = float (l.pagedispy
- l.pagey) in
1801 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1802 GlDraw.begins `quads
;
1804 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1805 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1806 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1807 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1817 let showoutline = function
1819 | Some
(allowdel
, active, first, outlines, qsearch) ->
1821 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1822 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1823 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
1826 GlDraw.color
(1., 1., 1.);
1827 let font = Glut.BITMAP_9_BY_15
in
1828 let draw_string x y s =
1829 GlPix.raster_pos ~
x ~
y ();
1830 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1833 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
1836 let (s, l, _
, _
) = outlines.(row
) in
1837 let y = (row
- first) * 16 in
1842 GlDraw.polygon_mode `both `line
;
1843 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1844 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1845 GlDraw.rect (0., float (y + 1))
1846 (float (conf.winw
- 1), float (y + 18));
1847 GlDraw.polygon_mode `both `fill
;
1849 GlDraw.color
(1., 1., 1.);
1851 draw_string (float x) (float (y + 16)) s;
1859 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1860 GlDraw.viewport
margin 0 state.w conf.winh
;
1862 GlClear.color
(scalecolor 0.5);
1863 GlClear.clear
[`color
];
1866 let x = float state.x in
1867 GlMat.translate ~
x ();
1871 Gl.enable `scissor_test
;
1872 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
1874 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1877 Gl.disable `scissor_test
1881 let x = -.float state.x in
1882 GlMat.translate ~
x ();
1886 GlDraw.viewport
0 0 conf.winw
conf.winh
;
1889 showoutline state.outline;
1891 Glut.swapBuffers
();
1895 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1896 let x = x - margin - state.x in
1897 let rec f = function
1899 begin match getopaque l.pageno with
1900 | Some
opaque when validopaque opaque ->
1901 let y = y - l.pagedispy
in
1904 let y = l.pagey + y in
1905 let x = x - l.pagex
in
1906 match whatsunder
opaque x y with
1919 let mouse ~button ~bstate ~
x ~
y =
1921 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1929 let incr = incr * 2 in
1930 let y = clamp incr in
1931 gotoy_and_clear_text y
1933 | Glut.LEFT_BUTTON
when state.outline = None
1934 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1935 if bstate
= Glut.DOWN
1937 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1938 state.mstate
<- Mpan
(x, y)
1941 state.mstate
<- Mnone
1944 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
1945 if bstate
= Glut.DOWN
1947 let position, sh = scrollph state.y in
1948 if y > truncate
position && y < truncate
(position +. sh)
1950 state.mstate
<- Mscroll
1952 let percent = float y /. float conf.winh
in
1953 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
1955 state.mstate
<- Mscroll
1957 state.mstate
<- Mnone
1959 | Glut.LEFT_BUTTON
when state.outline = None
->
1960 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1961 begin match dest with
1962 | Ulinkgoto
(pageno, top
) ->
1965 gotopage1 pageno top
1970 | Unone
when bstate
= Glut.DOWN
->
1971 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1972 state.mstate
<- Mpan
(x, y);
1974 | Unone
| Utext _
->
1975 if bstate
= Glut.DOWN
1977 if conf.angle
mod 360 = 0
1979 state.mstate
<- Msel
((x, y), (x, y));
1980 Glut.postRedisplay
()
1984 match state.mstate
with
1988 state.mstate
<- Mnone
1991 Glut.setCursor
Glut.CURSOR_INHERIT
;
1992 state.mstate
<- Mnone
1994 | Msel
((x0
, y0
), (x1
, y1
)) ->
1996 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1997 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1999 match getopaque l.pageno with
2000 | Some
opaque when validopaque opaque ->
2004 List.iter
f state.layout;
2005 copysel
""; (* ugly *)
2006 Glut.setCursor
Glut.CURSOR_INHERIT
;
2007 state.mstate
<- Mnone
;
2014 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
2017 if state.outline = None
2019 match state.mstate
with
2025 state.mstate
<- Mpan
(x, y);
2026 if conf.zoom > 1.0 then state.x <- state.x + dx;
2028 gotoy_and_clear_text y
2031 state.mstate
<- Msel
(a, (x, y));
2032 Glut.postRedisplay
()
2035 let y = min
conf.winh
(max
0 y) in
2036 let percent = float y /. float conf.winh
in
2037 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2038 gotoy_and_clear_text y
2042 if state.outline = None
2044 match state.mstate
with
2046 begin match getunder x y with
2047 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2049 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2050 Glut.setCursor
Glut.CURSOR_INFO
2051 | Ulinkgoto
(page
, y) ->
2052 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2053 Glut.setCursor
Glut.CURSOR_INFO
2055 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2056 Glut.setCursor
Glut.CURSOR_TEXT
2059 | Mpan _
| Msel _
| Mscroll
->
2069 match Sys.os_type
with
2070 | "Win32" -> Sys.getenv
"HOMEPATH"
2071 | _
-> Sys.getenv
"HOME"
2074 ("Can not determine home directory location: " ^
2075 Printexc.to_string exn
);
2079 let config_of c attrs
=
2083 | "scroll-bar-width" -> { c with scrollw
= int_of_string v
}
2084 | "scroll-handle-height" -> { c with scrollh
= int_of_string v
}
2085 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2086 | "preload" -> { c with preload = bool_of_string v
}
2087 | "page-bias" -> { c with pagebias
= int_of_string v
}
2088 | "scroll-step" -> { c with scrollincr
= int_of_string v
}
2089 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2090 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2091 | "throttle" -> { c with showall
= bool_of_string v
}
2092 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2093 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2094 | "vertical-margin" -> { c with interpagespace = int_of_string v
}
2096 let zoom = float_of_string v
/. 100. in
2097 let zoom = max
0.1 (min
2.2 zoom) in
2098 { c with zoom = zoom }
2099 | "presentation" -> { c with presentation = bool_of_string v
}
2100 | "rotation-angle" -> { c with angle
= int_of_string v
}
2101 | "width" -> { c with winw
= int_of_string v
}
2102 | "height" -> { c with winh
= int_of_string v
}
2103 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2104 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2107 prerr_endline
("Error processing attribute (`" ^
2108 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2111 let rec fold c = function
2114 let c = apply c k v
in
2120 let bookmark_of attrs
=
2121 let rec fold title page
rely = function
2122 | ("title", v
) :: rest
-> fold v page
rely rest
2123 | ("page", v
) :: rest
-> fold title v
rely rest
2124 | ("rely", v
) :: rest
-> fold title page v rest
2125 | _
:: rest
-> fold title page
rely rest
2126 | [] -> title, page
, rely
2128 fold "invalid" "0" "0" attrs
2131 let setconf dst src
=
2132 dst
.scrollw
<- src
.scrollw
;
2133 dst
.scrollh
<- src
.scrollh
;
2134 dst
.icase
<- src
.icase
;
2135 dst
.preload <- src
.preload;
2136 dst
.pagebias
<- src
.pagebias
;
2137 dst
.verbose
<- src
.verbose
;
2138 dst
.scrollincr
<- src
.scrollincr
;
2139 dst
.maxhfit
<- src
.maxhfit
;
2140 dst
.crophack
<- src
.crophack
;
2141 dst
.autoscroll
<- src
.autoscroll
;
2142 dst
.showall
<- src
.showall
;
2143 dst
.hlinks
<- src
.hlinks
;
2144 dst
.underinfo
<- src
.underinfo
;
2145 dst
.interpagespace <- src
.interpagespace;
2146 dst
.zoom <- src
.zoom;
2147 dst
.presentation <- src
.presentation;
2148 dst
.angle
<- src
.angle
;
2149 dst
.winw
<- src
.winw
;
2150 dst
.winh
<- src
.winh
;
2151 dst
.savebmarks
<- src
.savebmarks
;
2152 dst
.proportional
<- src
.proportional
;
2156 let l = String.length
s in
2157 let b = Buffer.create
l in
2163 let h = Hashtbl.create
10 in
2164 let dc = { defconf with angle
= defconf.angle
} in
2165 let rec toplevel v t spos epos
=
2167 | Vdata
| Vcdata
| Vend
-> v
2168 | Vopen
("llppconfig", attrs
, closed
) ->
2171 else { v
with f = llppconfig
}
2173 error
"unexpected subelement at top level" s spos
2174 | Vclose tag
-> error
"unexpected close at toplevel" s spos
2176 and llppconfig v t spos epos
=
2178 | Vdata
| Vcdata
| Vend
-> v
2179 | Vopen
("defaults", attrs
, closed
) ->
2180 let c = config_of dc attrs
in
2184 else { v
with f = skip
"defaults" (fun () -> v
) }
2186 | Vopen
("doc", attrs
, closed
) ->
2189 List.assoc
"path" attrs
2190 with Not_found
-> error
"doc is missing path attribute" s spos
2192 let path = unent pathent in
2193 let c = config_of dc attrs
in
2196 float_of_string
(List.assoc
"rely" attrs
)
2200 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2205 int_of_string
(List.assoc
"pan" attrs
)
2209 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2213 then (Hashtbl.add
h path (c, [], x, y); v
)
2214 else { v
with f = doc
path x y c [] }
2216 | Vopen
(tag
, _
, closed
) ->
2217 error
"unexpected subelement in llppconfig" s spos
2219 | Vclose
"llppconfig" -> { v
with f = toplevel }
2220 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2222 and doc
path x y c bookmarks v t spos epos
=
2224 | Vdata
| Vcdata
-> v
2225 | Vend
-> error
"unexpected end of input in doc" s spos
2226 | Vopen
("bookmarks", attrs
, closed
) ->
2227 { v
with f = pbookmarks
path x y c bookmarks }
2229 | Vopen
(tag
, _
, _
) ->
2230 error
"unexpected subelement in doc" s spos
2233 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2234 { v
with f = llppconfig
}
2236 | Vclose tag
-> error
"unexpected close in doc" s spos
2238 and pbookmarks
path x y c bookmarks v t spos epos
=
2240 | Vdata
| Vcdata
-> v
2241 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2242 | Vopen
("item", attrs
, closed
) ->
2243 let titleent, spage
, srely
= bookmark_of attrs
in
2248 dolog "Failed to convert page %S to integer: %s"
2249 spage
(Printexc.to_string exn
);
2254 float_of_string srely
2256 dolog "Failed to convert rely %S to real: %s"
2257 srely
(Printexc.to_string exn
);
2260 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2262 then { v
with f = pbookmarks
path x y c bookmarks }
2265 { v
with f = skip
"item" f }
2268 error
"unexpected subelement in bookmarks" s spos
2270 | Vclose
"bookmarks" ->
2271 { v
with f = doc
path x y c bookmarks }
2273 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2275 and skip tag
f v t spos epos
=
2277 | Vdata
| Vcdata
-> v
2279 error
("unexpected end of input in skipped " ^ tag
) s spos
2280 | Vopen
(tag'
, _
, closed
) ->
2284 let f'
() = { v
with f = skip tag
f } in
2285 { v
with f = skip tag'
f'
}
2289 else error
("unexpected close in skipped " ^ tag
) s spos
2292 parse
{ f = toplevel; accu = () } s;
2298 let len = in_channel_length ic
in
2299 let s = String.create
len in
2300 really_input ic
s 0 len;
2303 | Parse_error
(msg
, s, pos
) ->
2304 let subs = subs s pos
in
2305 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2306 failwith
("parse error: " ^
s)
2309 failwith
("config load error: " ^
Printexc.to_string exn
)
2315 let dir = Filename.concat
home ".config" in
2316 if Sys.is_directory
dir then dir else home
2319 Filename.concat
dir "llpp.conf"
2323 if Sys.file_exists
path
2326 (try Some
(open_in_bin
path)
2329 ("Error opening configuation file `" ^
path ^
"': " ^
2330 Printexc.to_string exn
);
2339 ("Error loading configuation from `" ^
path ^
"': " ^
2340 Printexc.to_string exn
);
2346 f (Hashtbl.create
0, defconf)
2351 let pc, pb
, px
, py =
2353 Hashtbl.find
h state.path
2354 with Not_found
-> dc, [], 0, 0.0
2358 state.bookmarks <- pb
;
2360 cbput state.hists
.nav
py;
2361 cbrfollowlen state.hists
.nav
;
2366 let add_attrs bb always
dc c =
2369 then Printf.bprintf bb
"\n %s='%b'" s a
2372 then Printf.bprintf bb
"\n %s='%d'" s a
2375 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2379 then dc.winw
, dc.winh
2381 match state.fullscreen
with
2383 | None
-> c.winw
, c.winh
2385 oi
"width" w dc.winw
;
2386 oi
"height" h dc.winh
;
2387 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2388 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2389 ob "case-insensitive-search" c.icase
dc.icase
;
2390 ob "preload" c.preload dc.preload;
2391 oi
"page-bias" c.pagebias
dc.pagebias
;
2392 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2393 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2394 ob "crop-hack" c.crophack
dc.crophack
;
2395 ob "throttle" c.showall
dc.showall
;
2396 ob "highlight-links" c.hlinks
dc.hlinks
;
2397 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2398 oi
"vertical-margin" c.interpagespace dc.interpagespace;
2399 oz
"zoom" c.zoom dc.zoom;
2400 ob "presentation" c.presentation dc.presentation;
2401 oi
"rotation-angle" c.angle
dc.angle
;
2402 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2403 ob "proportional-display" c.proportional
dc.proportional
;
2407 let bb = Buffer.create
32768 in
2409 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2410 add_attrs bb true dc dc;
2411 Buffer.add_string
bb "/>\n";
2413 let adddoc path x y c bookmarks =
2414 if bookmarks == [] && c = dc && y = 0.0
2417 Printf.bprintf
bb "<doc path='%s'"
2418 (enent
path 0 (String.length
path));
2421 then Printf.bprintf
bb " rely='%f'" y;
2424 then Printf.bprintf
bb " pan='%d'" x;
2426 add_attrs bb false dc c;
2428 begin match bookmarks with
2429 | [] -> Buffer.add_string
bb "/>\n"
2431 Buffer.add_string
bb ">\n<bookmarks>\n";
2432 List.iter
(fun (title, _level
, page, rely) ->
2434 "<item title='%s' page='%d' rely='%f'/>\n"
2435 (enent
title 0 (String.length
title))
2439 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2444 adddoc state.path state.x (yratio state.y) conf
2445 (if conf.savebmarks
then state.bookmarks else []);
2447 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2448 if path <> state.path
2450 adddoc path x y c bookmarks
2452 Buffer.add_string
bb "</llppconfig>";
2455 if Buffer.length
bb > 0
2458 let tmp = path ^
".tmp" in
2459 let oc = open_out_bin
tmp in
2460 Buffer.output_buffer
oc bb;
2462 Sys.rename
tmp path;
2465 ("error while saving configuration: " ^
Printexc.to_string exn
)
2471 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2472 (fun s -> state.path <- s)
2473 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2476 if String.length
state.path = 0
2477 then (prerr_endline
"filename missing"; exit
1)
2479 if Filename.is_relative
state.path
2480 then Filename.concat
(Sys.getcwd
()) state.path
2488 let _ = Glut.init
Sys.argv
in
2489 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2490 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2491 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2494 if Sys.os_type
= "Unix"
2496 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2498 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2499 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2500 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2501 Unix.bind
sock addr;
2503 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2504 Unix.connect
csock addr;
2505 let ssock, _ = Unix.accept
sock in
2508 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2509 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2513 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2517 let () = Glut.displayFunc
display in
2518 let () = Glut.reshapeFunc
reshape in
2519 let () = Glut.keyboardFunc
keyboard in
2520 let () = Glut.specialFunc
special in
2521 let () = Glut.idleFunc
(Some
idle) in
2522 let () = Glut.mouseFunc
mouse in
2523 let () = Glut.motionFunc
motion in
2524 let () = Glut.passiveMotionFunc
pmotion in
2527 state.csock <- csock;
2528 state.ssock <- ssock;
2529 state.text <- "Opening " ^
path;
2530 writeopen state.path state.password
;
2534 let rec handlelablglutbug () =
2537 with Glut.BadEnum
"key in special_of_int" ->
2538 showtext '
!'
" LablGlut bug: special key not recognized";
2539 handlelablglutbug ()
2541 handlelablglutbug ();