Source file ocamlCanvas.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
module V1 = struct
let valid_canvas_size (width, height) =
0 < width && width <= 32767 && 0 < height && height <= 32767
module Exception = struct
exception Not_initialized
exception File_not_found of string
exception Read_png_failed of string
exception Write_png_failed of string
let () =
Callback.register_exception "Not_initialized" Not_initialized;
Callback.register_exception "File_not_found" (File_not_found "");
Callback.register_exception "Read_png_failed" (Read_png_failed "");
Callback.register_exception "Write_png_failed" (Write_png_failed "")
end
module Const = struct
let pi = 3.14159265358979323846
let pi_2 = 1.57079632679489661923
let pi_4 = 0.785398163397448309616
let pi_8 = 0.392699081698724154808
let inv_pi = 0.318309886183790671538
let inv_pi_2 = 0.636619772367581343076
let inv_pi_4 = 1.27323954473516268615
let inv_pi_8 = 2.54647908947032537230
let e = 2.71828182845904523536
let inv_e = 0.367879441171442321596
let log2_e = 1.44269504088896340736
let log10_e = 0.434294481903251827651
let ln_2 = 0.693147180559945309417
let ln_10 = 2.30258509299404568402
end
module Vector = struct
type t = (float * float)
let zero =
(0.0, 0.0)
let unit =
(1.0, 1.0)
let add (x1, y1) (x2, y2) =
x1 +. x2, y1 +. y2
let sub (x1, y1) (x2, y2) =
x1 -. x2, y1 -. y2
let mul (x, y) k =
x *. k, y *. k
let dot (x1, y1) (x2, y2) =
x1 *. x2 +. y1 *. y2
let norm (x, y) =
sqrt (x *. x +. y *. y)
end
module Transform = struct
type t = {
a : float;
b : float;
c : float;
d : float;
e : float;
f : float;
}
let id = { a = 1.0; b = 0.0;
c = 0.0; d = 1.0;
e = 0.0; f = 0.0 }
let create (a, b, c, d, e, f) =
{ a; b; c; d; e; f }
let mul t1 t2 =
{ a = t1.a *. t2.a +. t1.c *. t2.b;
b = t1.b *. t2.a +. t1.d *. t2.b;
c = t1.a *. t2.c +. t1.c *. t2.d;
d = t1.b *. t2.c +. t1.d *. t2.d;
e = t1.e +. t1.a *. t2.e +. t1.c *. t2.f;
f = t1.f +. t1.b *. t2.e +. t1.d *. t2.f }
let translate t (x, y) =
{ t with
e = x *. t.a +. y *. t.c;
f = x *. t.b +. y *. t.d }
let scale t (x,y) =
{ t with
a = t.a *. x; b = t.b *. x;
c = t.c *. y; d = t.d *. y }
let shear t (x, y) =
{ t with
a = t.a +. t.c *. y; b = t.b +. t.d *. y;
c = t.c +. t.a *. x; d = t.d +. t.b *. x }
let rotate t a =
let cos_a = cos (-. a) in
let sin_a = sin (-. a) in
{ t with
a = t.a *. cos_a -. t.c *. sin_a;
b = t.b *. cos_a -. t.d *. sin_a;
c = t.c *. cos_a +. t.a *. sin_a;
d = t.d *. cos_a +. t.b *. sin_a }
let inverse t =
let det = t.a *. t.d -. t.b *. t.c in
if det = 0.0 then
invalid_arg "Transform.inverse: the matrix is not inversible";
let invdet = 1.0 /. det in
{ a = t.d *. invdet; b = -. t.b *. invdet;
c = -. t.c *. invdet; d = t.a *. invdet;
e = (t.c *. t.f -. t.d *. t.e) *. invdet;
f = (t.b *. t.e -. t.a *. t.f) *. invdet }
end
module Point = struct
type t = (float * float)
let of_ints (x, y) =
(float_of_int x, float_of_int y)
let sub (x1, y1) (x2, y2) =
x1 -. x2, y1 -. y2
let translate (x, y) ~by:(a, b) =
(x +. a, y +. b)
let rotate (x, y) ~around:(cx, cy) ~theta =
((x -. cx) *. (cos (-. theta)) +. (y -. cy) *. (sin (-. theta)) +. cx,
(y -. cy) *. (cos (-. theta)) -. (x -. cx) *. (sin (-. theta)) +. cy)
let transform (x, y) t =
let open Transform in
(x *. t.a +. y *. t.c +. t.e,
x *. t.b +. y *. t.d +. t.f)
let barycenter a (x1, y1) b (x2, y2) =
let sum_ab = a +. b in
if sum_ab = 0.0 then
invalid_arg "Point.barycenter: a + b must be non-nul";
(a *. x1 +. b *. x2) /. sum_ab,
(a *. y1 +. b *. y2) /. sum_ab
let distance (x1, y1) (x2, y2) =
sqrt ((x2 -. x1) ** 2.0 +. (y2 -. y1) ** 2.0)
end
module Color = struct
type t = Int32.t
let clip_8 i =
if i < 0 then 0
else if i > 0xFF then 0xFF
else i
let of_rgb r g b =
Int32.add 0xFF000000l
(Int32.of_int (clip_8 r lsl 16 + clip_8 g lsl 8 + clip_8 b))
let to_rgb c =
Int32.(to_int (logand (shift_right_logical c 16) 0xFFl)),
Int32.(to_int (logand (shift_right_logical c 8) 0xFFl)),
Int32.(to_int (logand c 0xFFl))
let of_argb a r g b =
Int32.add
(Int32.shift_left (Int32.of_int (clip_8 a)) 24)
(Int32.of_int (clip_8 r lsl 16 + clip_8 g lsl 8 + clip_8 b))
let to_argb c =
Int32.(to_int (shift_right_logical c 24)),
Int32.(to_int (logand (shift_right_logical c 16) 0xFFl)),
Int32.(to_int (logand (shift_right_logical c 8) 0xFFl)),
Int32.(to_int (logand c 0xFFl))
let of_int i =
if i < 0 then 0l
else if i > 0x00FFFFFF then 0xFFFFFFFFl
else Int32.add 0xFF000000l (Int32.of_int i)
let to_int c =
Int32.(to_int (logand c 0x00FFFFFFl))
let of_int32 i =
i
let to_int32 c =
c
module StringMap = Map.Make(String)
let colors = ref StringMap.empty
let define_color name c =
colors := StringMap.add (String.lowercase_ascii name) c !colors;
c
let transpBlack = define_color "transparent_black" 0x00000000l
let transpWhite = define_color "transparent_white" 0x00FFFFFFl
let black = define_color "black" 0xFF000000l
let white = define_color "white" 0xFFFFFFFFl
let blue = define_color "blue" 0xFF0000FFl
let cyan = define_color "cyan" 0xFF00FFFFl
let green = define_color "green" 0xFF008000l
let lime = define_color "lime" 0xFF00FF00l
let orange = define_color "orange" 0xFFFFA500l
let pink = define_color "pink" 0xFFFFC0CBl
let red = define_color "red" 0xFFFF0000l
let of_string s =
if String.length s < 1 then
black
else if s.[0] = '#' then
let s = "0x" ^ (String.sub s 1 (String.length s - 1)) in
of_int (int_of_string s)
else
try StringMap.find (String.lowercase_ascii s) !colors
with Not_found -> black
end
module Font = struct
type size = float
type slant =
| Roman
| Italic
| Oblique
type weight = int
let thin = 100
let = 200
let light = 300
let regular = 400
let medium = 500
let semiBold = 600
let bold = 700
let = 800
let black = 900
end
type canvas
type image_data =
(int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array3.t
module Pending = struct
type t =
| ImageData :
image_data React.event *
(?step:React.step -> image_data -> unit) * image_data -> t
| Canvas :
canvas React.event *
(?step:React.step -> canvas -> unit) * canvas -> t
let list : t list ref = ref []
let add_image_data event send_event c =
list := ImageData (event, send_event, c) :: !list
let add_canvas event send_event c =
list := Canvas (event, send_event, c) :: !list
let process () =
match !list with
| [] -> ()
| l ->
list := [];
List.iter (function
| ImageData (event, send_event, id) ->
send_event ?step:None id;
React.E.stop event
| Canvas (event, send_event, c) ->
send_event ?step:None c;
React.E.stop event
) (List.rev l)
end
module ImageData = struct
type t = image_data
let create (width, height) =
if not (valid_canvas_size (width, height)) then
invalid_arg "ImageData.create: invalid image dimensions";
let a = Bigarray.Array3.create Bigarray.int8_unsigned
Bigarray.c_layout height width 4 in
Bigarray.Array3.fill a 0;
a
external createFromPNG_internal : string -> (t -> unit) -> unit
= "ml_canvas_image_data_create_from_png"
let createFromPNG filename : t React.event =
let event, send_event = React.E.create () in
createFromPNG_internal filename
(fun id -> Pending.add_image_data event send_event id);
event
external getSize : t -> (int * int)
= "ml_canvas_image_data_get_size"
external fill : t -> Color.t -> unit
= "ml_canvas_image_data_fill"
external sub : t -> pos:(int * int) -> size:(int * int) -> t
= "ml_canvas_image_data_sub"
external blit :
dst:t -> dpos:(int * int) ->
src:t -> spos:(int * int) -> size:(int * int) -> unit
= "ml_canvas_image_data_blit"
external getPixel : t -> (int * int) -> Color.t
= "ml_canvas_image_data_get_pixel"
external putPixel : t -> (int * int) -> Color.t -> unit
= "ml_canvas_image_data_put_pixel"
external importPNG_internal :
t -> pos:(int * int) -> string -> (t -> unit) -> unit
= "ml_canvas_image_data_import_png"
let importPNG id ~pos filename : t React.event =
let event, send_event = React.E.create () in
importPNG_internal id ~pos filename
(fun id -> Pending.add_image_data event send_event id);
event
external exportPNG : t -> string -> unit
= "ml_canvas_image_data_export_png"
type t_repr = image_data
let of_bigarray (ba : t_repr) =
if not (valid_canvas_size (Bigarray.Array3.dim2 ba,
Bigarray.Array3.dim1 ba)) then
invalid_arg "ImageData.of_bigarray: invalid image dimensions";
if Bigarray.Array3.dim3 ba <> 4 then
invalid_arg "ImageData.of_bigarray: third dimension must be 4";
ba
external to_bigarray : t -> t_repr = "%identity"
end
module Gradient = struct
type t
external createLinear : pos1:Point.t -> pos2:Point.t -> t
= "ml_canvas_gradient_create_linear"
external createRadial :
center1:Point.t -> rad1:float -> center2:Point.t -> rad2:float -> t
= "ml_canvas_gradient_create_radial"
external createConic : center:Point.t -> angle:float -> t
= "ml_canvas_gradient_create_conic"
external addColorStop : t -> Color.t -> float -> unit
= "ml_canvas_gradient_add_color_stop"
end
module Pattern = struct
type t
type repeat =
| NoRepeat
| RepeatX
| RepeatY
| RepeatXY
external create : ImageData.t -> repeat -> t
= "ml_canvas_pattern_create"
end
module Path = struct
type t
external create : unit -> t
= "ml_canvas_path_create"
external close : t -> unit
= "ml_canvas_path_close"
external moveTo : t -> Point.t -> unit
= "ml_canvas_path_move_to"
external lineTo : t -> Point.t -> unit
= "ml_canvas_path_line_to"
external arc :
t -> center:Point.t -> radius:float ->
theta1:float -> theta2:float -> ccw:bool -> unit
= "ml_canvas_path_arc" "ml_canvas_path_arc_n"
external arcTo : t -> p1:Point.t -> p2:Point.t -> radius:float -> unit
= "ml_canvas_path_arc_to"
external quadraticCurveTo : t -> cp:Point.t -> p:Point.t -> unit
= "ml_canvas_path_quadratic_curve_to"
external bezierCurveTo :
t -> cp1:Point.t -> cp2:Point.t -> p:Point.t -> unit
= "ml_canvas_path_bezier_curve_to"
external rect : t -> pos:Point.t -> size:Vector.t -> unit
= "ml_canvas_path_rect"
external ellipse :
t -> center:Point.t -> radius:Vector.t ->
rotation:float -> theta1:float -> theta2:float -> ccw:bool -> unit
= "ml_canvas_path_ellipse" "ml_canvas_path_ellipse_n"
external add : t -> t -> unit
= "ml_canvas_path_add"
external addTransformed : t -> t -> Transform.t -> unit
= "ml_canvas_path_add_transformed"
end
module Join = struct
type t =
| Round
| Miter
| Bevel
end
module Cap = struct
type t =
| Butt
| Square
| Round
end
module Style = struct
type t =
| Color of Color.t
| Gradient of Gradient.t
| Pattern of Pattern.t
end
module CompositeOp = struct
type t =
| SourceOver
| SourceIn
| SourceOut
| SourceAtop
| DestinationOver
| DestinationIn
| DestinationOut
| DestinationAtop
| Lighter
| Copy
| XOR
| Multiply
| Screen
| Overlay
| Darken
| Lighten
| ColorDodge
| ColorBurn
| HardLight
| SoftLight
| Difference
| Exclusion
| Hue
| Saturation
| Color
| Luminosity
end
module Canvas = struct
type t = canvas
let () =
Callback.register "Hashtbl.hash" Hashtbl.hash
external compare : t -> t -> int
= "ml_canvas_compare"
external hash : t -> int
= "ml_canvas_hash"
external createOnscreen :
?autocommit:bool -> ?decorated:bool -> ?resizeable:bool ->
?minimize:bool -> ?maximize:bool -> ?close:bool -> ?title:string ->
?pos:(int * int) -> size:(int * int) -> unit -> t
= "ml_canvas_create_onscreen" "ml_canvas_create_onscreen_n"
external createOffscreen : size:(int * int) -> unit -> t
= "ml_canvas_create_offscreen"
external createOffscreenFromImageData : ImageData.t -> t
= "ml_canvas_create_offscreen_from_image_data"
external createOffscreenFromPNG_internal : string -> (t -> unit) -> unit
= "ml_canvas_create_offscreen_from_png"
let createOffscreenFromPNG filename : t React.event =
let event, send_event = React.E.create () in
createOffscreenFromPNG_internal filename
(fun c -> Pending.add_canvas event send_event c);
event
external show : t -> unit
= "ml_canvas_show"
external hide : t -> unit
= "ml_canvas_hide"
external close : t -> unit
= "ml_canvas_close"
external commit : t -> unit
= "ml_canvas_commit"
external getId : t -> int
= "ml_canvas_get_id"
external getSize : t -> (int * int)
= "ml_canvas_get_size"
external setSize : t -> (int * int) -> unit
= "ml_canvas_set_size"
external getPosition : t -> (int * int)
= "ml_canvas_get_position"
external setPosition : t -> (int * int) -> unit
= "ml_canvas_set_position"
external save : t -> unit
= "ml_canvas_save"
external restore : t -> unit
= "ml_canvas_restore"
external setTransform : t -> Transform.t -> unit
= "ml_canvas_set_transform"
external transform : t -> Transform.t -> unit
= "ml_canvas_transform"
external translate : t -> Vector.t -> unit
= "ml_canvas_translate"
external scale : t -> Vector.t -> unit
= "ml_canvas_scale"
external shear : t -> Vector.t -> unit
= "ml_canvas_shear"
external rotate : t -> float -> unit
= "ml_canvas_rotate"
external getLineWidth : t -> float
= "ml_canvas_get_line_width"
external setLineWidth : t -> float -> unit
= "ml_canvas_set_line_width"
external getLineJoin : t -> Join.t
= "ml_canvas_get_line_join"
external setLineJoin : t -> Join.t -> unit
= "ml_canvas_set_line_join"
external getLineCap : t -> Cap.t
= "ml_canvas_get_line_cap"
external setLineCap : t -> Cap.t -> unit
= "ml_canvas_set_line_cap"
external getMiterLimit : t -> float
= "ml_canvas_get_miter_limit"
external setMiterLimit : t -> float -> unit
= "ml_canvas_set_miter_limit"
external getLineDashOffset : t -> float
= "ml_canvas_get_line_dash_offset"
external setLineDashOffset : t -> float -> unit
= "ml_canvas_set_line_dash_offset"
external getLineDash : t -> float array
= "ml_canvas_get_line_dash"
external setLineDash : t -> float array -> unit
= "ml_canvas_set_line_dash"
external getStrokeColor : t -> Color.t
= "ml_canvas_get_stroke_color"
external setStrokeColor : t -> Color.t -> unit
= "ml_canvas_set_stroke_color"
external setStrokeGradient : t -> Gradient.t -> unit
= "ml_canvas_set_stroke_gradient"
external setStrokePattern : t -> Pattern.t -> unit
= "ml_canvas_set_stroke_pattern"
external getStrokeStyle : t -> Style.t
= "ml_canvas_get_stroke_style"
external setStrokeStyle : t -> Style.t -> unit
= "ml_canvas_set_stroke_style"
external getFillColor : t -> Color.t
= "ml_canvas_get_fill_color"
external setFillColor : t -> Color.t -> unit
= "ml_canvas_set_fill_color"
external setFillGradient : t -> Gradient.t -> unit
= "ml_canvas_set_fill_gradient"
external setFillPattern : t -> Pattern.t -> unit
= "ml_canvas_set_fill_pattern"
external getFillStyle : t -> Style.t
= "ml_canvas_get_fill_style"
external setFillStyle : t -> Style.t -> unit
= "ml_canvas_set_fill_style"
external getGlobalAlpha : t -> float
= "ml_canvas_get_global_alpha"
external setGlobalAlpha : t -> float -> unit
= "ml_canvas_set_global_alpha"
external getGlobalCompositeOperation : t -> CompositeOp.t
= "ml_canvas_get_global_composite_operation"
external setGlobalCompositeOperation : t -> CompositeOp.t -> unit
= "ml_canvas_set_global_composite_operation"
external getShadowColor : t -> Color.t
= "ml_canvas_get_shadow_color"
external setShadowColor : t -> Color.t -> unit
= "ml_canvas_set_shadow_color"
external getShadowBlur : t -> float
= "ml_canvas_get_shadow_blur"
external setShadowBlur : t -> float -> unit
= "ml_canvas_set_shadow_blur"
external getShadowOffset : t -> Vector.t
= "ml_canvas_get_shadow_offset"
external setShadowOffset : t -> Vector.t -> unit
= "ml_canvas_set_shadow_offset"
external setFont :
t -> string -> size:Font.size -> slant:Font.slant ->
weight:Font.weight -> unit
= "ml_canvas_set_font"
external clearPath : t -> unit
= "ml_canvas_clear_path"
external closePath : t -> unit
= "ml_canvas_close_path"
external moveTo : t -> Point.t -> unit
= "ml_canvas_move_to"
external lineTo : t -> Point.t -> unit
= "ml_canvas_line_to"
external arc :
t -> center:Point.t -> radius:float ->
theta1:float -> theta2:float -> ccw:bool -> unit
= "ml_canvas_arc" "ml_canvas_arc_n"
external arcTo : t -> p1:Point.t -> p2:Point.t -> radius:float -> unit
= "ml_canvas_arc_to"
external quadraticCurveTo : t -> cp:Point.t -> p:Point.t -> unit
= "ml_canvas_quadratic_curve_to"
external bezierCurveTo :
t -> cp1:Point.t -> cp2:Point.t -> p:Point.t -> unit
= "ml_canvas_bezier_curve_to"
external rect : t -> pos:Point.t -> size:Vector.t -> unit
= "ml_canvas_rect"
external ellipse :
t -> center:Point.t -> radius:Vector.t ->
rotation:float -> theta1:float -> theta2:float -> ccw:bool -> unit
= "ml_canvas_ellipse" "ml_canvas_ellipse_n"
external fill : t -> nonzero:bool -> unit
= "ml_canvas_fill"
external fillPath : t -> Path.t -> nonzero:bool -> unit
= "ml_canvas_fill_path"
external stroke : t -> unit
= "ml_canvas_stroke"
external strokePath : t -> Path.t -> unit
= "ml_canvas_stroke_path"
external clip : t -> nonzero:bool -> unit
= "ml_canvas_clip"
external clipPath : t -> Path.t -> nonzero:bool -> unit
= "ml_canvas_clip_path"
external fillRect : t -> pos:Point.t -> size:Vector.t -> unit
= "ml_canvas_fill_rect"
external strokeRect : t -> pos:Point.t -> size:Vector.t -> unit
= "ml_canvas_stroke_rect"
external fillText : t -> string -> Point.t -> unit
= "ml_canvas_fill_text"
external strokeText : t -> string -> Point.t -> unit
= "ml_canvas_stroke_text"
external blit :
dst:t -> dpos:(int * int) ->
src:t -> spos:(int * int) -> size:(int * int) -> unit
= "ml_canvas_blit"
external getPixel : t -> (int * int) -> Color.t
= "ml_canvas_get_pixel"
external putPixel : t -> (int * int) -> Color.t -> unit
= "ml_canvas_put_pixel"
external getImageData :
t -> pos:(int * int) -> size:(int * int) -> ImageData.t
= "ml_canvas_get_image_data"
external putImageData :
t -> dpos:(int * int) -> ImageData.t ->
spos:(int * int) -> size:(int * int) -> unit
= "ml_canvas_put_image_data"
external importPNG_internal :
t -> pos:(int * int) -> string -> (t -> unit) -> unit
= "ml_canvas_import_png"
let importPNG (c : t) ~pos filename : t React.event =
let event, send_event = React.E.create () in
importPNG_internal c ~pos filename
(fun c -> Pending.add_canvas event send_event c);
event
external exportPNG : t -> string -> unit
= "ml_canvas_export_png"
end
module Event = struct
type timestamp = Int64.t
type 'a canvas_event = {
canvas: Canvas.t;
timestamp: timestamp;
data: 'a;
}
type position = int * int
type size = int * int
type key =
| KeyEscape
| KeyF1
| KeyF2
| KeyF3
| KeyF4
| KeyF5
| KeyF6
| KeyF7
| KeyF8
| KeyF9
| KeyF10
| KeyF11
| KeyF12
| KeyPrintScreen
| KeyScrollLock
| KeyPause
| KeyGraveTilde
| Key1Exclamation
| Key2At
| Key3Number
| Key4Dollar
| Key5Percent
| Key6Caret
| Key7Ampersand
| Key8Asterisk
| Key9LParenthesis
| Key0RParenthesis
| KeyMinusUndersclre
| KeyEqualPlus
| KeyBackspace
| KeyTab
| KeyQ
| KeyW
| KeyE
| KeyR
| KeyT
| KeyY
| KeyU
| KeyI
| KeyO
| KeyP
| KeyLBracketCurly
| KeyRBracketCurly
| KeyBackslashPipe
| KeyCapsLock
| KeyA
| KeyS
| KeyD
| KeyF
| KeyG
| KeyH
| KeyJ
| KeyK
| KeyL
| KeySemicolonColon
| KeyQuoteDoublequote
| KeyNonUSNumberTilde
| KeyReturn
| KeyLShift
| KeyNonUSBackslashPipe
| KeyZ
| KeyX
| KeyC
| KeyV
| KeyB
| KeyN
| KeyM
| KeyCommaLess
| KeyPeriodGreater
| KeySlashQuestion
| KeyRShift
| KeyLControl
| KeyLMeta
| KeyLAlt
| KeySpacebar
| KeyRAlt
| KeyRMeta
| KeyRControl
| KeyInsert
| KeyHome
| KeyPageUp
| KeyDeleteForward
| KeyEend
| KeyPageDown
| KeyUpArrow
| KeyLeftArrow
| KeyDownArrow
| KeyRightArrow
| KeyPadNumlockClear
| KeyPadEquals
| KeyPadDivide
| KeyPadMultiply
| KeyPadMinus
| KeyPad7Home
| KeyPad8UpArrow
| KeyPad9PageUp
| KeyPadPlus
| KeyPad4LeftArrow
| KeyPad5
| KeyPad6RightArrow
| KeyPadComma
| KeyPad1End
| KeyPad2DownArrow
| KeyPad3PageDown
| KeyPad0Insert
| KeyPadDecimalDelete
| KeyPadEnter
| KeyF13
| KeyF14
| KeyF15
| KeyF16
| KeyF17
| KeyF18
| KeyF19
| KeyF20
| KeyF21
| KeyF22
| KeyF23
| KeyF24
| KeyInternational1
| KeyInternational2
| KeyInternational3
| KeyInternational4
| KeyInternational5
| KeyInternational6
| KeyInternational7
| KeyInternational8
| KeyInternational9
| KeyLang1
| KeyLang2
| KeyLang3
| KeyLang4
| KeyLand5
| KeyHelp
| KeyMute
| KeyVolumeUp
| KeyVolumeDown
type flags = {
flag_shift : bool;
flag_alt : bool;
flag_control : bool;
flag_meta : bool;
flag_capslock : bool;
flag_numlock : bool;
flag_dead : bool;
}
type key_data = {
key: key;
char: Uchar.t;
flags: flags;
}
type button =
| ButtonNone
| ButtonLeft
| ButtonMiddle
| ButtonRight
| ButtonWheelUp
| ButtonWheelDown
type button_data = {
position: position;
button: button;
}
type 'a ev = 'a React.event * (?step:React.step -> 'a -> unit)
let frame, send_frame =
(React.E.create () : unit canvas_event ev)
let focus_in, send_focus_in =
(React.E.create () : unit canvas_event ev)
let focus_out, send_focus_out =
(React.E.create () : unit canvas_event ev)
let resize, send_resize =
(React.E.create () : size canvas_event ev)
let move, send_move =
(React.E.create () : position canvas_event ev)
let close, send_close =
(React.E.create () : unit canvas_event ev)
let key_down, send_key_down =
(React.E.create () : key_data canvas_event ev)
let key_up, send_key_up =
(React.E.create () : key_data canvas_event ev)
let button_down, send_button_down =
(React.E.create () : button_data canvas_event ev)
let button_up, send_button_up =
(React.E.create () : button_data canvas_event ev)
let mouse_move, send_mouse_move =
(React.E.create () : position canvas_event ev)
let event_timestamp, set_event_timestamp =
React.S.create 0L
external int_of_key : key -> int
= "ml_canvas_int_of_key"
external key_of_int : int -> key
= "ml_canvas_key_of_int"
end
module InternalEvent = struct
[@@@warning "-37"]
open Event
type frame_cycle_event = {
timestamp: timestamp;
}
type frame_event = {
canvas: Canvas.t;
timestamp: timestamp;
}
type focus_direction =
| Out
| In
type canvas_focused_event = {
canvas: Canvas.t;
timestamp: timestamp;
focus: focus_direction;
}
type canvas_resized_event = {
canvas: Canvas.t;
timestamp: timestamp;
size: int * int;
}
type canvas_moved_event = {
canvas: Canvas.t;
timestamp: timestamp;
position: int * int;
}
type canvas_closed_event = {
canvas: Canvas.t;
timestamp: timestamp;
}
type state =
| Up
| Down
type key_action_event = {
canvas: Canvas.t;
timestamp: timestamp;
key: key;
char: Uchar.t;
flags: flags;
state: state;
}
type button_action_event = {
canvas: Canvas.t;
timestamp: timestamp;
position: int * int;
button: button;
state: state;
}
type mouse_move_event = {
canvas: Canvas.t;
timestamp: timestamp;
position: int * int;
}
type t =
| FrameCycle of frame_cycle_event
| Frame of frame_event
| CanvasFocused of canvas_focused_event
| CanvasResized of canvas_resized_event
| CanvasMoved of canvas_moved_event
| CanvasClosed of canvas_closed_event
| KeyAction of key_action_event
| ButtonAction of button_action_event
| MouseMove of mouse_move_event
end
module Backend = struct
external init : unit -> unit
= "ml_canvas_init"
external run_internal :
(InternalEvent.t -> unit) -> (unit -> 'dummy1) -> 'dummy2
= "ml_canvas_run"
external stop : unit -> unit
= "ml_canvas_stop"
external getCurrentTimestamp : unit -> Event.timestamp
= "ml_canvas_get_current_timestamp"
external getCanvas : int -> Canvas.t
= "ml_canvas_get_canvas"
let run k =
let open InternalEvent in
let open Event in
let h e =
(match e with
| FrameCycle { timestamp } ->
()
| Frame { canvas; timestamp } ->
let e = { canvas; timestamp; data = () } in
set_event_timestamp e.timestamp; send_frame e
| CanvasFocused { canvas; timestamp; focus = In } ->
let e = { canvas; timestamp; data = () } in
set_event_timestamp e.timestamp; send_focus_in e
| CanvasFocused { canvas; timestamp; focus = Out } ->
let e = { canvas; timestamp; data = () } in
set_event_timestamp e.timestamp; send_focus_out e
| CanvasResized { canvas; timestamp; size } ->
let e = { canvas; timestamp; data = size } in
set_event_timestamp e.timestamp; send_resize e
| CanvasMoved { canvas; timestamp; position } ->
let e = { canvas; timestamp; data = position } in
set_event_timestamp e.timestamp; send_move e
| CanvasClosed { canvas; timestamp } ->
let e = { canvas; timestamp; data = () } in
set_event_timestamp e.timestamp; send_close e
| KeyAction { canvas; timestamp; key; char; flags; state = Down } ->
let e = { canvas; timestamp; data = { key; char; flags } } in
set_event_timestamp e.timestamp; send_key_down e
| KeyAction { canvas; timestamp; key; char; flags; state = Up } ->
let e = { canvas; timestamp; data = { key; char; flags } } in
set_event_timestamp e.timestamp; send_key_up e
| ButtonAction { canvas; timestamp; position; button; state = Down } ->
let e = { canvas; timestamp; data = { position; button } } in
set_event_timestamp e.timestamp; send_button_down e
| ButtonAction { canvas; timestamp; position; button; state = Up } ->
let e = { canvas; timestamp; data = { position; button } } in
set_event_timestamp e.timestamp; send_button_up e
| MouseMove { canvas; timestamp; position } ->
let e = { canvas; timestamp; data = position } in
set_event_timestamp e.timestamp; send_mouse_move e);
Pending.process ()
in
run_internal h k
end
end