forked from ghennequin/llpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ml
6670 lines (6138 loc) · 188 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;;
exception Quit;;
external init : Unix.file_descr -> params -> unit = "ml_init";;
external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
external hassel : opaque -> bool = "ml_hassel";;
external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
external getpdimrect : int -> float array = "ml_getpdimrect";;
external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
external clearmark : opaque -> unit = "ml_clearmark";;
external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
external getmaxw : unit -> float = "ml_getmaxw";;
external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
external measurestr : int -> string -> float = "ml_measure_string";;
external postprocess :
opaque -> int -> int -> int -> (int * string * int) -> int
= "ml_postprocess";;
external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
external setaalevel : int -> unit = "ml_setaalevel";;
external realloctexts : int -> bool = "ml_realloctexts";;
external findlink : opaque -> linkdir -> link = "ml_findlink";;
external getlink : opaque -> int -> under = "ml_getlink";;
external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
external getlinkcount : opaque -> int = "ml_getlinkcount";;
external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
external freepbo : opaque -> unit = "ml_freepbo";;
external unmappbo : opaque -> unit = "ml_unmappbo";;
external bousable : unit -> bool = "ml_bo_usable";;
external unproject : opaque -> int -> int -> (int * int) option
= "ml_unproject";;
external project : opaque -> int -> int -> float -> float -> (float * float)
= "ml_project";;
external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
external rectofblock : opaque -> int -> int -> float array option
= "ml_rectofblock";;
external begintiles : unit -> unit = "ml_begintiles";;
external endtiles : unit -> unit = "ml_endtiles";;
external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
external savedoc : string -> unit = "ml_savedoc";;
external getannotcontents : opaque -> slinkindex -> string
= "ml_getannotcontents";;
external drawprect : opaque -> int -> int -> float array -> unit =
"ml_drawprect";;
external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
external rcmd : Unix.file_descr -> string = "ml_rcmd";;
let selfexec = ref E.s;;
let opengl_has_pbo = ref false;;
let drawstring size x y s =
Gl.enable `blend;
Gl.enable `texture_2d;
GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
ignore (drawstr size x y s);
Gl.disable `blend;
Gl.disable `texture_2d;
;;
let drawstring1 size x y s =
drawstr size x y s;
;;
let drawstring2 size x y fmt =
Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
;;
let _debugl l =
dolog "l %d dim=%d {" l.pageno l.pagedimno;
dolog " WxH %dx%d" l.pagew l.pageh;
dolog " vWxH %dx%d" l.pagevw l.pagevh;
dolog " pagex,y %d,%d" l.pagex l.pagey;
dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
dolog " column %d" l.pagecol;
dolog "}";
;;
let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
dolog "rect {";
dolog " x0,y0=(% f, % f)" x0 y0;
dolog " x1,y1=(% f, % f)" x1 y1;
dolog " x2,y2=(% f, % f)" x2 y2;
dolog " x3,y3=(% f, % f)" x3 y3;
dolog "}";
;;
let isbirdseye = function
| Birdseye _ -> true
| Textentry _
| View
| LinkNav _ -> false
;;
let istextentry = function
| Textentry _ -> true
| Birdseye _
| View
| LinkNav _ -> false
;;
let wtmode = ref false;;
let cxack = ref false;;
let pgscale h = truncate (float h *. conf.pgscale);;
let hscrollh () =
if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
&& (state.w > state.winw))
then conf.scrollbw
else 0
;;
let vscrollw () =
if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
&& (state.maxy > state.winh))
then conf.scrollbw
else 0
;;
let vscrollhit x =
if conf.leftscroll
then x < vscrollw ()
else x > state.winw - vscrollw ()
;;
let setfontsize n =
fstate.fontsize <- n;
fstate.wwidth <- measurestr fstate.fontsize "w";
fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
;;
let vlog fmt =
if conf.verbose
then dolog fmt
else Printf.kprintf ignore fmt
;;
let launchpath () =
if emptystr conf.pathlauncher
then dolog "%s" state.path
else (
let command = Str.global_replace percentsre state.path conf.pathlauncher in
match spawn command [] with
| _pid -> ()
| (exception exn) ->
dolog "failed to execute `%s': %s" command @@ exntos exn
);
;;
module G =
struct
let postRedisplay who =
vlog "redisplay for [%S]" who;
state.redisplay <- true;
;;
end;;
let getopaque pageno =
try Some (Hashtbl.find state.pagemap (pageno, state.gen))
with Not_found -> None
;;
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
| l :: rest ->
begin match getopaque l.pageno with
| Some 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
| _ ->
f rest
end
| [] -> d
in
f state.layout
;;
let getunder x y =
let g opaque l px py =
if state.bzoom
then (
match 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
state.rects <- [l.pageno, color, rect];
G.postRedisplay "getunder";
| _otherwise -> ()
);
let under = 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 unproject opaque x y with
| Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
| None -> None
in
onppundermouse g x y None;
;;
let showtext c s =
state.text <- Printf.sprintf "%c%s" c s;
G.postRedisplay "showtext";
;;
let impmsg fmt =
Format.ksprintf (fun s -> showtext '!' s) fmt;
;;
let pipesel opaque cmd =
if hassel opaque
then
match Unix.pipe () with
| (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
| (r, w) ->
let doclose what fd =
Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
in
let pid =
try spawn cmd [r, 0; w, -1]
with exn ->
dolog "cannot execute %S: %s" cmd @@ exntos exn;
-1
in
if pid > 0
then (
copysel w opaque;
G.postRedisplay "pipesel";
)
else doclose "pipesel pipe/w" w;
doclose "pipesel pipe/r" r;
;;
let paxunder x y =
let g opaque l px py =
if markunder opaque px py conf.paxmark
then (
Some (fun () ->
match getopaque l.pageno with
| None -> ()
| Some opaque -> pipesel opaque conf.paxcmd
)
)
else None
in
G.postRedisplay "paxunder";
if conf.paxmark = Mark_page
then
List.iter (fun l ->
match getopaque l.pageno with
| None -> ()
| Some opaque -> clearmark opaque) state.layout;
state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
;;
let selstring s =
match Unix.pipe () with
| (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
| (r, w) ->
let clo cap fd =
Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
in
let pid =
try spawn conf.selcmd [r, 0; w, -1]
with exn ->
impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
-1
in
if pid > 0
then (
try
let l = String.length s in
let bytes = Bytes.unsafe_of_string s in
let n = tempfailureretry (Unix.write w bytes 0) l in
if n != l
then impmsg "failed to write %d characters to sel pipe, wrote %d"
l n
with exn ->
impmsg "failed to write to sel pipe: %s" @@ exntos exn
)
else dolog "%s" s;
clo "selstring pipe/r" r;
clo "selstring pipe/w" w;
;;
let undertext ?(nopath=false) = function
| Unone -> "none"
| Ulinkuri s -> s
| Ulinkgoto (pageno, _) ->
if nopath
then "page " ^ string_of_int (pageno+1)
else Printf.sprintf "%s: page %d" state.path (pageno+1)
| Utext s -> "font: " ^ s
| Uunexpected s -> "unexpected: " ^ s
| Ulaunch s -> "launch: " ^ s
| Unamed s -> "named: " ^ s
| Uremote (filename, pageno) ->
Printf.sprintf "%s: page %d" filename (pageno+1)
| Uremotedest (filename, destname) ->
Printf.sprintf "%s: destination %S" filename destname
| Uannotation (opaque, slinkindex) ->
"annotation: " ^ getannotcontents 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
| Ulinkgoto (pageno, _) ->
if conf.underinfo
then showtext 'p' ("age: " ^ string_of_int (pageno+1));
Wsi.setcursor Wsi.CURSOR_INFO
| Utext s ->
if conf.underinfo then showtext 'f' ("ont: " ^ s);
Wsi.setcursor Wsi.CURSOR_TEXT
| Uunexpected s ->
if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
Wsi.setcursor Wsi.CURSOR_INHERIT
| Ulaunch s ->
if conf.underinfo then showtext 'l' ("aunch: " ^ s);
Wsi.setcursor Wsi.CURSOR_INHERIT
| Unamed s ->
if conf.underinfo then showtext 'n' ("amed: " ^ s);
Wsi.setcursor Wsi.CURSOR_INHERIT
| Uremote (filename, pageno) ->
if conf.underinfo then showtext 'r'
(Printf.sprintf "emote: %s (%d)" filename (pageno+1));
Wsi.setcursor Wsi.CURSOR_INFO
| Uremotedest (filename, destname) ->
if conf.underinfo then showtext 'r'
(Printf.sprintf "emote destination: %s (%S)" filename destname);
Wsi.setcursor Wsi.CURSOR_INFO
| Uannotation _ ->
if conf.underinfo then showtext 'a' "nnotation";
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 =
if key >= 32 && key < 127
then
let c = Char.chr key in
match c with
| '0' .. '9' ->
addchar text c
| 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
addchar text @@ asciilower c
| _ ->
state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
text
else (
state.text <- Printf.sprintf "invalid key %d" key;
text
)
in
TEcont text
;;
let wcmd fmt =
let b = Buffer.create 16 in
Buffer.add_string b "llll";
Printf.kbprintf
(fun b ->
let b = Buffer.to_bytes b in
wcmd state.ss b @@ Bytes.length b
) b fmt
;;
let nogeomcmds cmds =
match cmds with
| s, [] -> emptystr s
| _ -> false
;;
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 = state.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 = state.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
let e =
{ pageno = n
; pagedimno = pdimno
; pagew = w
; pageh = h
; pagex = pagex
; pagey = pagey
; pagevw = pagevw
; pagevh = pagevh
; pagedispx = pagedispx
; pagedispy = pagedispy
; pagecol = 0
}
in
e :: 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
let e =
{ pageno = n/columns
; pagedimno = pdimno
; pagew = pagew
; pageh = pageh
; pagex = pagex
; pagey = pagey
; pagevw = pagevw
; pagevh = pagevh
; pagedispx = pagedispx
; pagedispy = pagedispy
; pagecol = n mod columns
}
in
e :: accu
else
accu
else
accu
in
fold accu (n+1)
in
List.rev (fold [] 0)
;;
let layout x y sw sh =
if nogeomcmds state.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 clamp incr =
let y = state.y + incr in
let y = max 0 y in
let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
y;
;;
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 ()
else (
let dh = conf.tileh - y0 in
let dh = min h dh in
let rec colloop col x0 dispx w =
if w = 0
then ()
else (
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, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
in
try Some (Hashtbl.find state.tilemap key)
with Not_found -> None
;;
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 state.tilemap key (opaque, size, elapsed)
;;
let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
GlArray.vertex `two state.vraw;
GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
;;
let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
let filledrect x0 y0 x1 y1 =
GlArray.disable `texture_coord;
filledrect1 x0 y0 x1 y1;
GlArray.enable `texture_coord;
;;
let linerect x0 y0 x1 y1 =
GlArray.disable `texture_coord;
Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
GlArray.vertex `two state.vraw;
GlArray.draw_arrays `line_loop ~first:0 ~count:4;
GlArray.enable `texture_coord;
;;
let drawtiles l color =
GlDraw.color color;
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
if conf.invert
then GlTex.env (`mode `blend);
drawtile params opaque;
if conf.invert
then GlTex.env (`mode `modulate);
if conf.debug
then (
endtiles ();
let s = Printf.sprintf
"%d[%d,%d] %f sec"
l.pageno col row t
in
let w = measurestr fstate.fontsize s in
GlDraw.color (0.0, 0.0, 0.0);
filledrect (float (x-2))
(float (y-2))
(float (x+2) +. w)
(float (y + fstate.fontsize + 2));
GlDraw.color color;
drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
begintiles ();
);
| None ->
endtiles ();
let w =
let lw = state.winw - x in
min lw w
and h =
let lh = state.winh - y in
min lh h
in
if conf.invert
then GlTex.env (`mode `blend);
begin match state.checkerstexid with
| Some id ->
Gl.enable `texture_2d;
GlTex.bind_texture ~target:`texture_2d id;
let x0 = float x
and y0 = float y
and x1 = float (x+w)
and y1 = float (y+h) in
let tw = float w /. 16.0
and th = float h /. 16.0 in
let tx0 = float tilex /. 16.0
and ty0 = float tiley /. 16.0 in
let tx1 = tx0 +. tw
and ty1 = ty0 +. th in
Raw.sets_float state.vraw ~pos:0
[| x0; y0; x0; y1; x1; y0; x1; y1 |];
Raw.sets_float state.traw ~pos:0
[| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
GlArray.vertex `two state.vraw;
GlArray.tex_coord `two state.traw;
GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
Gl.disable `texture_2d;
| None ->
GlDraw.color (1.0, 1.0, 1.0);
filledrect (float x) (float y) (float (x+w)) (float (y+h));
end;
if conf.invert
then GlTex.env (`mode `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
drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
);
GlDraw.color color;
begintiles ();
in
itertiles l f;
endtiles ();
;;
let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
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 state.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
let pbo =
if conf.usepbo
then getpbo w h conf.colorspace
else ~< "0"
in
wcmd "tile %s %d %d %d %d %s"
(~> p) x y w h (~> pbo);
state.currently <-
Tiling (
l, p, conf.colorspace, conf.angle,
state.gen, col, row, conf.tilew, conf.tileh
);
in
itertiles l f;
else
loop rest
| [] -> ()
in
if nogeomcmds state.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 state.currently != Idle
then ()
else
match pages with
| l :: rest ->
begin match getopaque l.pageno with
| None ->
wcmd "page %d %d" l.pageno l.pagedimno;
state.currently <- Loading (l, state.gen);
| Some opaque ->
tilepage l.pageno opaque pages;
loop rest
end;
| _ -> ()
in
if nogeomcmds state.geomcmds
then loop pages
;;
let preload pages =
load pages;
if conf.preload && state.currently = Idle
then load (preloadlayout state.x state.y state.winw state.winh);
;;
let layoutready layout =
let rec fold all ls =
all && match ls with
| l :: rest ->
let seen = ref false in
let allvisible = ref true in
let foo col row _ _ _ _ _ _ =
seen := true;
allvisible := !allvisible &&
begin match gettileopaque l col row with
| Some _ -> true
| None -> false
end
in
itertiles l foo;
fold (!seen && !allvisible) rest
| [] -> true
in
let alltilesvisible = fold true layout in
alltilesvisible;
;;
let gotoxy x y =
let y = bound y 0 state.maxy in
let y, layout, proceed =
match conf.maxwait with
| Some time when state.ghyll == noghyll ->
begin match state.throttle with
| None ->
let layout = layout x y state.winw state.winh in
let ready = layoutready layout in
if not ready
then (
load layout;
state.throttle <- Some (layout, y, now ());
)
else G.postRedisplay "gotoxy showall (None)";
y, layout, ready
| Some (_, _, started) ->
let dt = now () -. started in
if dt > time
then (
state.throttle <- None;
let layout = layout x y state.winw state.winh in
load layout;
G.postRedisplay "maxwait";
y, layout, true
)
else -1, [], false
end
| _ ->
let layout = layout x y state.winw state.winh in
if not !wtmode || layoutready layout
then G.postRedisplay "gotoxy ready";
y, layout, true
in
if proceed
then (
state.x <- x;
state.y <- y;
state.layout <- layout;
begin match state.mode with
| LinkNav ln ->
begin match ln with
| Ltexact (pageno, linkno) ->
let rec loop = function
| [] ->
state.mode <- LinkNav (Ltgendir 0)
| l :: _ when l.pageno = pageno ->
begin match getopaque pageno with
| None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
| Some opaque ->
let x0, y0, x1, y1 = getlinkrect opaque linkno in
if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
&& y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
then state.mode <- LinkNav (Ltgendir 0)
end
| _ :: rest -> loop rest
in
loop layout
| Ltnotready _ | Ltgendir _ -> ()
end
| Birdseye _
| Textentry _
| View -> ()
end;
begin match state.mode with
| Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
if not (pagevisible layout pageno)
then (
match state.layout with
| [] -> ()
| l :: _ ->
state.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
| None -> Ltnotready (l.pageno, dir)
| Some opaque ->
let link =
let ld =
if dir = 0
then LDfirstvisible (l.pagex, l.pagey, dir)
else (
if dir > 0 then LDfirst else LDlast
)
in
findlink opaque ld
in
match link with
| Lnotfound -> loop rest
| Lfound n ->
showlinktype (getlink opaque n);
Ltexact (l.pageno, n)
in
loop state.layout
in
state.mode <- LinkNav linknav
| Ltexact _ -> ()
end
| Textentry _
| View -> ()
end;
preload layout;
);
state.ghyll <- noghyll;
if conf.updatecurs
then (
let mx, my = state.mpos in
updateunder mx my;
);
;;
let conttiling pageno opaque =
tilepage pageno opaque
(if conf.preload
then preloadlayout state.x state.y state.winw state.winh
else state.layout)
;;
let gotoxy_and_clear_text x y =
if not conf.verbose then state.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 gotoanchor anchor =