forked from criticic/llpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ml
4897 lines (4578 loc) · 145 KB
/
main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Utils
open Config
open Uiutils
module U = struct
let dopen = '\023'
let cs = '\024'
let freepage = '\025'
let freetile = '\026'
let search = '\027'
let geometry = '\028'
let reqlayout = '\029'
let page = '\030'
let tile = '\031'
let trimset = '\032'
let settrim = '\033'
let sliceh = '\034'
let interrupt = '\035'
let pgscale h = truncate (float h *. conf.pgscale)
let nogeomcmds = function | s, [] -> emptystr s | _ -> false
let maxy () = !S.maxy - if conf.maxhfit then !S.winh else 0
let scalecolor c = let c = c *. conf.colorscale in (c, c, c)
let panbound x = bound x (- !S.w) !S.winw
let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout
let add_to_y_and_clamp inc = bound (!S.y + inc) 0 @@ maxy ()
end
let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
dolog {|rect {
x0,y0=(% f, % f)
x1,y1=(% f, % f)
x2,y2=(% f, % f)
x3,y3=(% f, % f)
}|} x0 y0 x1 y1 x2 y2 x3 y3
let hscrollh () =
if ((conf.scrollb land scrollbhv != 0) && (!S.w > !S.winw))
|| !S.uioh#alwaysscrolly
then conf.scrollbw
else 0
let setfontsize n =
fstate.fontsize <- n;
fstate.wwidth <- Ffi.measurestr fstate.fontsize "w";
fstate.maxrows <- (!S.winh - fstate.fontsize - 1) / (fstate.fontsize + 1)
let showtext c s =
S.text := Printf.sprintf "%c%s" c s;
Glutils.postRedisplay "showtext"
let adderrmsg src msg =
Buffer.add_string S.errmsgs msg;
S.newerrmsgs := true;
Glutils.postRedisplay src
let settextfmt fmt = Printf.ksprintf (fun s -> S.text := s) fmt
let impmsg fmt = Printf.ksprintf (fun s -> showtext '!' s) fmt
let adderrfmt src fmt = Printf.ksprintf (fun s -> adderrmsg src s) fmt
let launchpath () =
if emptystr conf.pathlauncher
then adderrmsg "path launcher" "command set"
else
let n =
match !S.layout with
| l :: _ -> string_of_int l.pageno
| _ -> E.s
in
let cmd = Str.global_replace Re.percents !S.path conf.pathlauncher in
let cmd =
if nonemptystr n
then Str.global_replace Re.percentp n cmd
else cmd
in
match spawn cmd [] with
| exception exn ->
adderrfmt "spawn" "failed to execute `%s': %s" cmd @@ exntos exn
| _pid -> ()
let getopaque pageno = Hashtbl.find S.pagemap (pageno, !S.gen)
let pagetranslatepoint l x y =
let dy = y - l.pagedispy in
let y = dy + l.pagey in
let dx = x - l.pagedispx in
let x = dx + l.pagex in
(x, y)
let onppundermouse g x y d =
let rec f = function
| [] -> d
| l :: rest ->
match getopaque l.pageno with
| exception Not_found -> f rest
| opaque ->
let x0 = l.pagedispx in
let x1 = x0 + l.pagevw in
let y0 = l.pagedispy in
let y1 = y0 + l.pagevh in
if y >= y0 && y <= y1 && x >= x0 && x <= x1
then
let px, py = pagetranslatepoint l x y in
match g opaque l px py with
| Some res -> res
| None -> f rest
else f rest
in
f !S.layout
let getunder x y =
let g opaque l px py =
if !S.bzoom
then (
match Ffi.rectofblock opaque px py with
| Some [|x0;x1;y0;y1|] ->
let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
S.rects := [l.pageno, color, rect];
Glutils.postRedisplay "getunder";
| _ -> ()
);
let under = Ffi.whatsunder opaque px py in
if under = Unone then None else Some under
in
onppundermouse g x y Unone
let unproject x y =
let g opaque l x y =
match Ffi.unproject opaque x y with
| Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
| None -> None
in
onppundermouse g x y None
let pipesel opaque cmd =
if Ffi.hassel opaque
then
pipef ~closew:false "pipesel"
(fun w ->
Ffi.copysel w opaque;
Glutils.postRedisplay "pipesel"
) cmd
let paxunder x y =
let g opaque l px py =
if Ffi.markunder opaque px py conf.paxmark
then
Some (fun () ->
match getopaque l.pageno with
| exception Not_found -> ()
| opaque -> pipesel opaque conf.paxcmd
)
else None
in
Glutils.postRedisplay "paxunder";
if conf.paxmark = MarkPage
then
List.iter (fun l ->
match getopaque l.pageno with
| exception Not_found -> ()
| opaque -> Ffi.clearmark opaque) !S.layout;
S.roamf := onppundermouse g x y (fun () -> impmsg "whoopsie daisy")
let undertext = function
| Unone -> "none"
| Ulinkuri s -> s
| Utext s -> "font: " ^ s
| Utextannot (opaque, slinkindex) ->
"text annotation: " ^ Ffi.gettextannot opaque slinkindex
| Ufileannot (opaque, slinkindex) ->
"file annotation: " ^ Ffi.getfileannot opaque slinkindex
let updateunder x y =
match getunder x y with
| Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
| Ulinkuri uri ->
if conf.underinfo then showtext 'u' ("ri: " ^ uri);
Wsi.setcursor Wsi.CURSOR_INFO
| Utext s ->
if conf.underinfo then showtext 'f' ("ont: " ^ s);
Wsi.setcursor Wsi.CURSOR_TEXT
| Utextannot _ ->
if conf.underinfo then showtext 't' "ext annotation";
Wsi.setcursor Wsi.CURSOR_INFO
| Ufileannot _ ->
if conf.underinfo then showtext 'f' "ile annotation";
Wsi.setcursor Wsi.CURSOR_INFO
let showlinktype under =
if conf.underinfo && under != Unone
then showtext ' ' @@ undertext under
let intentry_with_suffix text key =
let text =
match [@warning "-fragile-match"] key with
| Keys.Ascii ('0'..'9' as c) -> addchar text c
| Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
addchar text @@ Char.lowercase_ascii c
| _ ->
S.text := "invalid key";
text
in
TEcont text
let wcmd cmd fmt =
let b = Buffer.create 16 in
Printf.kbprintf
(fun b ->
Buffer.add_char b cmd;
let b = Buffer.to_bytes b in
Ffi.wcmd !S.ss b @@ Bytes.length b
) b fmt
let wcmd1 cmd opaque =
let s = Opaque.to_string opaque in
let l = String.length s in
let b = Bytes.create (l+1) in
Bytes.set b l cmd;
Bytes.blit_string s 0 b 0 l;
Ffi.wcmd !S.ss b @@ l + 1
let layoutN ((columns, coverA, coverB), b) x y sw sh =
let rec fold accu n =
if n = Array.length b
then accu
else
let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
if (vy - y) > sh
&& (n = coverA - 1
|| n = !S.pagecount - coverB
|| (n - coverA) mod columns = columns - 1)
then accu
else
let accu =
if vy + h > y
then
let pagey = max 0 (y - vy) in
let pagedispy = if pagey > 0 then 0 else vy - y in
let pagedispx, pagex =
let pdx =
if n = coverA - 1 || n = !S.pagecount - coverB
then x + (sw - w) / 2
else dx + xoff + x
in
if pdx < 0
then 0, -pdx
else pdx, 0
in
let pagevw =
let vw = sw - pagedispx in
let pw = w - pagex in
min vw pw
in
let pagevh = min (h - pagey) (sh - pagedispy) in
if pagevw > 0 && pagevh > 0
then
{ pageno = n
; pagecol = 0 ; pagedimno = pdimno ; pagew = w ; pageh = h
; pagex ; pagey ; pagevw ; pagevh ; pagedispx ; pagedispy
} :: accu
else accu
else accu
in
fold accu (n+1)
in
if Array.length b = 0
then []
else List.rev (fold [] (page_of_y y))
let layoutS (columns, b) x y sw sh =
let rec fold accu n =
if n = Array.length b
then accu
else
let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
if (vy - y) > sh
then accu
else
let accu =
if vy + pageh > y
then
let x = xoff + x in
let pagey = max 0 (y - vy) in
let pagedispy = if pagey > 0 then 0 else vy - y in
let pagedispx, pagex =
if px = 0
then (
if x < 0
then 0, -x
else x, 0
)
else (
let px = px - x in
if px < 0
then -px, 0
else 0, px
)
in
let pagecolw = pagew/columns in
let pagedispx =
if pagecolw < sw
then pagedispx + ((sw - pagecolw) / 2)
else pagedispx
in
let pagevw =
let vw = sw - pagedispx in
let pw = pagew - pagex in
min vw pw
in
let pagevw = min pagevw pagecolw in
let pagevh = min (pageh - pagey) (sh - pagedispy) in
if pagevw > 0 && pagevh > 0
then
{ pageno = n/columns
; pagedimno = pdimno
; pagecol = n mod columns
; pagew ; pageh ; pagex ; pagey ; pagedispx ; pagedispy
; pagevw ; pagevh
} :: accu
else accu
else accu
in
fold accu (n+1)
in
List.rev (fold [] 0)
let layout x y sw sh =
if U.nogeomcmds !S.geomcmds
then
match conf.columns with
| Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
| Cmulti c -> layoutN c x y sw sh
| Csplit s -> layoutS s x y sw sh
else []
let itertiles l f =
let tilex = l.pagex mod conf.tilew in
let tiley = l.pagey mod conf.tileh in
let col = l.pagex / conf.tilew in
let row = l.pagey / conf.tileh in
let rec rowloop row y0 dispy h =
if h != 0
then
let dh = conf.tileh - y0 in
let dh = min h dh in
let rec colloop col x0 dispx w =
if w != 0
then
let dw = conf.tilew - x0 in
let dw = min w dw in
f col row dispx dispy x0 y0 dw dh;
colloop (col+1) 0 (dispx+dw) (w-dw)
in
colloop col tilex l.pagedispx l.pagevw;
rowloop (row+1) 0 (dispy+dh) (h-dh)
in
if l.pagevw > 0 && l.pagevh > 0
then rowloop row tiley l.pagedispy l.pagevh
let gettileopaque l col row =
let key = l.pageno, !S.gen, conf.colorspace,
conf.angle, l.pagew, l.pageh, col, row in
Hashtbl.find_opt S.tilemap key
let puttileopaque l col row gen colorspace angle opaque size elapsed =
let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
Hashtbl.add S.tilemap key (opaque, size, elapsed)
let drawtiles l color =
let texe e = if conf.invert then GlTex.env (`mode e) in
GlDraw.color color;
Ffi.begintiles ();
let f col row x y tilex tiley w h =
match gettileopaque l col row with
| Some (opaque, _, t) ->
let params = x, y, w, h, tilex, tiley in
texe `blend;
Ffi.drawtile params opaque;
texe `modulate;
if conf.debug
then (
Ffi.endtiles ();
let s = Printf.sprintf "%d[%d,%d] %f sec" l.pageno col row t in
let w = Ffi.measurestr fstate.fontsize s in
GlDraw.color (0.0, 0.0, 0.0);
Glutils.filledrect
(float (x-2))
(float (y-2))
(float (x+2) +. w)
(float (y + fstate.fontsize + 2));
GlDraw.color color;
Glutils.drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
Ffi.begintiles ();
);
| None ->
Ffi.endtiles ();
let w = let lw = !S.winw - x in min lw w
and h = let lh = !S.winh - y in min lh h in
texe `blend;
let c = if conf.invert then 0.2 else 0.8 in
GlDraw.color (c, c, c);
Glutils.filledrect (float x) (float y) (float (x+w)) (float (y+h));
texe `modulate;
if w > 128 && h > fstate.fontsize + 10
then (
let c = if conf.invert then 1.0 else 0.0 in
GlDraw.color (c, c, c);
let c, r =
if conf.verbose
then (col*conf.tilew, row*conf.tileh)
else col, row
in
Glutils.drawstringf fstate.fontsize x y
"Loading %d [%d,%d]" l.pageno c r;
);
GlDraw.color color;
Ffi.begintiles ();
in
itertiles l f;
Ffi.endtiles ()
let tilevisible1 l x y =
let ax0 = l.pagex
and ax1 = l.pagex + l.pagevw
and ay0 = l.pagey
and ay1 = l.pagey + l.pagevh in
let bx0 = x
and by0 = y in
let bx1 = min (bx0 + conf.tilew) l.pagew
and by1 = min (by0 + conf.tileh) l.pageh in
let rx0 = max ax0 bx0
and ry0 = max ay0 by0
and rx1 = min ax1 bx1
and ry1 = min ay1 by1 in
let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
nonemptyintersection
let tilevisible layout n x y =
let rec findpageinlayout m = function
| l :: rest when l.pageno = n ->
tilevisible1 l x y || (
match conf.columns with
| Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
| Csplit _ | Csingle _ | Cmulti _ -> false
)
| _ :: rest -> findpageinlayout 0 rest
| [] -> false
in
findpageinlayout 0 layout
let tileready l x y =
tilevisible1 l x y &&
gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
let tilepage n p layout =
let rec loop = function
| l :: rest ->
if l.pageno = n
then
let f col row _ _ _ _ _ _ =
if !S.currently = Idle
then
match gettileopaque l col row with
| Some _ -> ()
| None ->
let x = col*conf.tilew
and y = row*conf.tileh in
let w =
let w = l.pagew - x in
min w conf.tilew
in
let h =
let h = l.pageh - y in
min h conf.tileh
in
wcmd U.tile "%s %d %d %d %d" (Opaque.to_string p) x y w h;
S.currently :=
Tiling (
l, p, conf.colorspace, conf.angle,
!S.gen, col, row, conf.tilew, conf.tileh
);
in
itertiles l f;
else loop rest
| [] -> ()
in
if U.nogeomcmds !S.geomcmds
then loop layout
let preloadlayout x y sw sh =
let y = if y < sh then 0 else y - sh in
let x = min 0 (x + sw) in
let h = sh*3 in
let w = sw*3 in
layout x y w h
let load pages =
let rec loop pages =
if !S.currently = Idle
then
match pages with
| l :: rest ->
begin match getopaque l.pageno with
| exception Not_found ->
wcmd U.page "%d %d" l.pageno l.pagedimno;
S.currently := Loading (l, !S.gen);
| opaque ->
tilepage l.pageno opaque pages;
loop rest
end
| _ -> ()
in
if U.nogeomcmds !S.geomcmds
then loop pages
let preload pages =
load pages;
if conf.preload && !S.currently = Idle
then load (preloadlayout !S.x !S.y !S.winw !S.winh)
let alltilesrendered layout =
let exception E in
let rec fold ls =
match ls with
| [] -> true
| l :: rest ->
let foo col row _ _ _ _ _ _ =
match gettileopaque l col row with
| Some _ -> ()
| None -> raise E
in
match itertiles l foo with
| () -> fold rest
| exception E -> false
in
fold layout
let gotoxy x y =
let y = bound y 0 !S.maxy in
let y, layout =
let layout = layout x y !S.winw !S.winh in
Glutils.postRedisplay "gotoxy ready";
y, layout
in
S.x := x;
S.y := y;
S.layout := layout;
begin match !S.mode with
| LinkNav ln ->
begin match ln with
| Ltexact (pageno, linkno) ->
let rec loop = function
| [] ->
S.lnava := Some (pageno, linkno);
S.mode := LinkNav (Ltgendir 0)
| l :: _ when l.pageno = pageno ->
begin match getopaque pageno with
| exception Not_found ->
S.mode := LinkNav (Ltnotready (pageno, 0))
| opaque ->
let x0, y0, x1, y1 = Ffi.getlinkrect opaque linkno in
if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
&& y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
then S.mode := LinkNav (Ltgendir 0)
end
| _ :: rest -> loop rest
in
loop layout
| Ltnotready _ | Ltgendir _ -> ()
end
| Birdseye _ | Textentry _ | View -> ()
end;
begin match !S.mode with
| Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
if not (U.pagevisible layout pageno)
then (
match !S.layout with
| [] -> ()
| l :: _ ->
S.mode := Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
)
| LinkNav lt ->
begin match lt with
| Ltnotready (_, dir)
| Ltgendir dir ->
let linknav =
let rec loop = function
| [] -> lt
| l :: rest ->
match getopaque l.pageno with
| exception Not_found -> Ltnotready (l.pageno, dir)
| opaque ->
let link =
let ld =
if dir = 0
then LDfirstvisible (l.pagex, l.pagey, dir)
else if dir > 0 then LDfirst else LDlast
in
Ffi.findlink opaque ld
in
match link with
| Lnotfound -> loop rest
| Lfound n ->
showlinktype (Ffi.getlink opaque n);
Ltexact (l.pageno, n)
in
loop !S.layout
in
S.mode := LinkNav linknav
| Ltexact _ -> ()
end
| Textentry _ | View -> ()
end;
preload layout;
if conf.updatecurs
then (
let mx, my = !S.mpos in
updateunder mx my;
)
let conttiling pageno opaque =
tilepage pageno opaque
(if conf.preload
then preloadlayout !S.x !S.y !S.winw !S.winh
else !S.layout)
let gotoxy x y =
if not conf.verbose then S.text := E.s;
gotoxy x y
let getanchory (n, top, dtop) =
let y, h = getpageyh n in
if conf.presentation
then
let ips = calcips h in
y + truncate (top*.float h -. dtop*.float ips) + ips;
else y + truncate (top*.float h -. dtop*.float conf.interpagespace)
let addnav () = S.nav := { past = getanchor () :: !S.nav.past; future = []; }
let gotopage n top =
let y, h = getpageyh n in
let y = y + (truncate (top *. float h)) in
gotoxy !S.x y
let gotopage1 n top =
let y = getpagey n in
let y = y + top in
gotoxy !S.x y
let invalidate s f =
Glutils.redisplay := false;
S.layout := [];
S.pdims := [];
S.rects := [];
S.rects1 := [];
match !S.geomcmds with
| ps, [] when emptystr ps ->
f ();
S.geomcmds := s, [];
| ps, [] -> S.geomcmds := ps, [s, f];
| ps, (s', _) :: rest when s' = s -> S.geomcmds := ps, ((s, f) :: rest);
| ps, cmds -> S.geomcmds := ps, ((s, f) :: cmds)
let flushpages () =
Hashtbl.iter (fun _ opaque -> wcmd1 U.freepage opaque) S.pagemap;
Hashtbl.clear S.pagemap
let flushtiles () =
if not (Queue.is_empty S.tilelru)
then (
Queue.iter (fun (k, p, s) ->
wcmd1 U.freetile p;
S.memused := !S.memused - s;
Hashtbl.remove S.tilemap k;
) S.tilelru;
!S.uioh#infochanged Memused;
Queue.clear S.tilelru;
);
load !S.layout
let stateh h =
let h = truncate (float h*.conf.zoom) in
let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
h - d
let fillhelp () =
S.help :=
let sl = keystostrlist conf in
let rec loop accu =
function | [] -> accu
| s :: rest -> loop ((s, 0, None) :: accu) rest
in Help.makehelp conf.urilauncher
@ (("", 0, None) :: loop [] sl) |> Array.of_list
let titlify path =
if emptystr path
then path
else
(if emptystr !S.origin then path else !S.origin)
|> Filename.basename |> Ffi.mbtoutf8
let settitle title =
conf.title <- title;
if not !S.ignoredoctitlte
then Wsi.settitle @@ title ^ " - llpp"
let opendoc path mimetype password =
S.path := path;
S.mimetype := mimetype;
S.password := password;
S.gen := !S.gen + 1;
S.docinfo := [];
S.outlines := [||];
flushpages ();
Ffi.setaalevel conf.aalevel;
Ffi.setpapercolor conf.papercolor;
Ffi.setdcf conf.dcf;
settitle @@ titlify path;
wcmd U.dopen "%d %d %d %d %s\000%s\000%s\000%s\000"
(btod conf.usedoccss) conf.rlw conf.rlh conf.rlem
path mimetype password conf.css;
invalidate "reqlayout"
(fun () ->
wcmd U.reqlayout " %d %d %d %s\000"
conf.angle (FMTE.to_int conf.fitmodel)
(stateh !S.winh) !S.nameddest
);
fillhelp ()
let reload () =
S.anchor := getanchor ();
S.reload := Some (!S.x, !S.y, now ());
opendoc !S.path !S.mimetype !S.password
let docolumns columns =
match columns with
| Csingle _ ->
let a = Array.make !S.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
let rec loop pageno pdimno pdim y ph pdims =
if pageno != !S.pagecount
then
let pdimno, ((_, w, h, xoff) as pdim), pdims =
match pdims with
| ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
pdimno+1, pdim, rest
| _ ->
pdimno, pdim, pdims
in
let x = max 0 (((!S.winw - w) / 2) - xoff) in
let y =
y + (if conf.presentation
then (if pageno = 0 then calcips h else calcips ph + calcips h)
else (if pageno = 0 then 0 else conf.interpagespace))
in
a.(pageno) <- (pdimno, x, y, pdim);
loop (pageno+1) pdimno pdim (y + h) h pdims
in
loop 0 ~-1 (-1,-1,-1,-1) 0 0 !S.pdims;
conf.columns <- Csingle a;
| Cmulti ((columns, coverA, coverB), _) ->
let a = Array.make !S.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
let rec loop pageno pdimno pdim x y rowh pdims =
let rec fixrow m =
if m >= pageno
then
let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
if h < rowh
then a.(m) <- (pdimno, x, y + (rowh - h) / 2, pdim);
fixrow (m+1)
in
if pageno = !S.pagecount
then fixrow (((pageno - 1) / columns) * columns)
else
let pdimno, ((_, w, h, xoff) as pdim), pdims =
match pdims with
| ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
pdimno+1, pdim, rest
| _ -> pdimno, pdim, pdims
in
let x, y, rowh' =
if pageno = coverA - 1 || pageno = !S.pagecount - coverB
then (
let x = (!S.winw - w) / 2 in
let ips =
if conf.presentation then calcips h else conf.interpagespace in
x, y + ips + rowh, h
)
else (
if (pageno - coverA) mod columns = 0
then (
let x = max 0 (!S.winw - !S.w) / 2 in
let y =
if conf.presentation
then
let ips = calcips h in
y + (if pageno = 0 then 0 else calcips rowh + ips)
else y + (if pageno = 0 then 0 else conf.interpagespace)
in
x, y + rowh, h
)
else x, y, max rowh h
)
in
let y =
if pageno > 1 && (pageno - coverA) mod columns = 0
then (
let y =
if pageno = columns && conf.presentation
then (
let ips = calcips rowh in
for i = 0 to pred columns
do
let (pdimno, x, y, pdim) = a.(i) in
a.(i) <- (pdimno, x, y+ips, pdim)
done;
y+ips;
)
else y
in
fixrow (pageno - columns);
y
)
else y
in
a.(pageno) <- (pdimno, x, y, pdim);
let x = x + w + xoff*2 + conf.interpagespace in
loop (pageno+1) pdimno pdim x y rowh' pdims
in
loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 !S.pdims;
conf.columns <- Cmulti ((columns, coverA, coverB), a);
| Csplit (c, _) ->
let a = Array.make (!S.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
let rec loop pageno pdimno pdim y pdims =
if pageno != !S.pagecount
then
let pdimno, ((_, w, h, _) as pdim), pdims =
match pdims with
| ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
pdimno+1, pdim, rest
| _ -> pdimno, pdim, pdims
in
let cw = w / c in
let rec loop1 n x y =
if n = c then y else (
a.(pageno*c + n) <- (pdimno, x, y, pdim);
loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
)
in
let y = loop1 0 0 y in
loop (pageno+1) pdimno pdim y pdims
in
loop 0 ~-1 (-1,-1,-1,-1) 0 !S.pdims;
conf.columns <- Csplit (c, a)
let represent () =
docolumns conf.columns;
S.maxy := calcheight ();
if !S.reprf == noreprf
then (
match !S.mode with
| Birdseye (_, _, pageno, _, _) ->
let y, h = getpageyh pageno in
let top = (!S.winh - h) / 2 in
gotoxy !S.x (max 0 (y - top))
| Textentry _ | View | LinkNav _ ->
let y = getanchory !S.anchor in
let y = min y (!S.maxy - !S.winh) in
gotoxy !S.x y;
)
else (
!S.reprf ();
S.reprf := noreprf;
)
let reshape ?(firsttime=false) w h =
GlDraw.viewport ~x:0 ~y:0 ~w ~h;
if not firsttime && U.nogeomcmds !S.geomcmds
then S.anchor := getanchor ();
S.winw := w;
let w = truncate (float w *. conf.zoom) in
let w = max w 2 in
S.winh := h;
setfontsize fstate.fontsize;
GlMat.mode `modelview;
GlMat.load_identity ();
GlMat.mode `projection;
GlMat.load_identity ();
GlMat.rotate ~x:1.0 ~angle:180.0 ();
GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
GlMat.scale3 (2.0 /. float !S.winw, 2.0 /. float !S.winh, 1.0);
let relx =
if conf.zoom <= 1.0
then 0.0
else float !S.x /. float !S.w
in
invalidate "geometry"
(fun () ->
S.w := w;
if not firsttime
then S.x := truncate (relx *. float w);
let w =
match conf.columns with
| Csingle _ -> w
| Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
| Csplit (c, _) -> w * c
in
wcmd U.geometry "%d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
)
let gctilesnotinlayout layout =
let len = Queue.length S.tilelru in
let rec loop qpos =
if !S.memused > conf.memlimit
then (
if qpos < len
then
let (k, p, s) as lruitem = Queue.pop S.tilelru in
let n, gen, colorspace, angle, pagew, pageh, col, row = k in
let (_, pw, ph, _) = getpagedim n in
if gen = !S.gen
&& colorspace = conf.colorspace
&& angle = conf.angle
&& pagew = pw
&& pageh = ph
&& (
let x = col*conf.tilew and y = row*conf.tileh in
tilevisible layout n x y
)
then Queue.push lruitem S.tilelru
else (
wcmd1 U.freetile p;
S.memused := !S.memused - s;
!S.uioh#infochanged Memused;
Hashtbl.remove S.tilemap k;
);
loop (qpos+1)
)
in
loop 0
let onpagerect pageno f =
let b =
match conf.columns with
| Cmulti (_, b) -> b
| Csingle b -> b
| Csplit (_, b) -> b
in
if pageno >= 0 && pageno < Array.length b
then
let (_, _, _, (_, w, h, _)) = b.(pageno) in
f w h
let gotopagexy1 pageno x y =
let _,w1,h1,leftx = getpagedim pageno in
let top = y /. (float h1) in
let left = x /. (float w1) in
let py, w, h = getpageywh pageno in
let wh = !S.winh in
let x = left *. (float w) in
let x = leftx + !S.x + truncate x in
let sx =
if x < 0 || x >= !S.winw
then !S.x - x
else !S.x
in
let pdy = truncate (top *. float h) in
let y' = py + pdy in
let dy = y' - !S.y in
let sy =
if x != !S.x || not (dy > 0 && dy < wh)
then (
if conf.presentation
then
if abs (py - y') > wh
then y'
else py
else y';
)
else !S.y
in
if !S.x != sx || !S.y != sy
then gotoxy sx sy
else gotoxy !S.x !S.y
let gotopagexy pageno x y =