deno.land / x / skia_canvas@0.5.8 / src / context2d.ts

نووسراو ببینە
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
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
import { Canvas } from "./canvas.ts";import { DOMMatrix } from "./dommatrix.ts";import ffi, { cstr } from "./ffi.ts";import { FilterType, parseFilterString } from "./filter.ts";import { CanvasGradient } from "./gradient.ts";import { Image, ImageData } from "./image.ts";import { parseFont } from "./parse_font.ts";import { Path2D, type RoundRectRadii, roundRectRadiiArg } from "./path2d.ts";import { CanvasPattern, type CanvasPatternImage, type CanvasPatternRepeat,} from "./pattern.ts";
const { sk_context_clear_rect, sk_context_set_fill_style, sk_context_save, sk_context_restore, sk_context_fill_rect, sk_context_set_stroke_style, sk_context_stroke_rect, sk_context_begin_path, sk_context_close_path, sk_context_line_to, sk_context_move_to, sk_context_fill, sk_context_stroke, sk_context_get_global_alpha, sk_context_get_line_width, sk_context_get_miter_limit, sk_context_set_global_alpha, sk_context_set_line_width, sk_context_set_miter_limit, sk_context_set_shadow_color, sk_context_rect, sk_context_clip, sk_context_arc, sk_context_arc_to, sk_context_bezier_curve_to, sk_context_ellipse, sk_context_quadratic_curve_to, sk_context_reset_transform, sk_context_rotate, sk_context_scale, sk_context_set_transform, sk_context_transform, sk_context_translate, sk_context_get_line_cap, sk_context_get_line_dash_offset, sk_context_get_shadow_blur, sk_context_get_shadow_offset_x, sk_context_get_shadow_offset_y, sk_context_get_text_align, sk_context_get_text_baseline, sk_context_get_text_direction, sk_context_set_font, sk_context_set_line_cap, sk_context_set_line_dash_offset, sk_context_set_shadow_blur, sk_context_set_shadow_offset_x, sk_context_set_shadow_offset_y, sk_context_set_text_align, sk_context_set_text_baseline, sk_context_set_text_direction, sk_context_draw_image, sk_context_text, sk_context_get_line_join, sk_context_set_line_join, sk_context_set_line_dash, sk_context_is_point_in_path, sk_context_is_point_in_stroke, sk_context_get_global_composite_operation, sk_context_set_global_composite_operation, sk_context_get_image_smoothing_enabled, sk_context_set_image_smoothing_enabled, sk_context_round_rect, sk_context_get_transform, sk_context_get_image_smoothing_quality, sk_context_set_image_smoothing_quality, sk_context_put_image_data_dirty, sk_context_put_image_data, sk_gradient_create_conic, sk_gradient_create_linear, sk_gradient_create_radial, sk_context_set_fill_style_gradient, sk_context_set_stroke_style_gradient, sk_context_set_fill_style_pattern, sk_context_set_stroke_style_pattern, sk_context_filter_contrast, sk_context_filter_invert, sk_context_filter_brightness, sk_context_filter_blur, sk_context_filter_drop_shadow, sk_context_filter_grayscale, sk_context_filter_hue_rotate, sk_context_filter_opacity, sk_context_filter_reset, sk_context_filter_saturated, sk_context_filter_sepia, sk_context_get_word_spacing, sk_context_get_letter_spacing, sk_context_set_word_spacing, sk_context_set_letter_spacing, sk_context_set_font_stretch, sk_context_set_font_variant_caps,} = ffi;
export type FillRule = "nonzero" | "evenodd";
enum CLineCap { butt = 0, round = 1, square = 2,}
enum CLineJoin { miter = 0, round = 1, bevel = 2,}
enum CTextAlign { left = 0, center = 1, right = 2, start = 3, end = 4,}
enum CTextDirection { inherit = 0, ltr = 1, rtl = 2,}
enum CTextBaseline { top, hanging, middle, alphabetic, ideographic, bottom,}
enum CImageSmoothingQuality { low = 1, medium = 2, high = 3,}
// deno-fmt-ignoreconst CGlobalCompositeOperation = { ["source-over"]: 0, ["source-in"]: 1, ["source-out"]: 2, ["source-atop"]: 3, ["destination-over"]: 4, ["destination-in"]: 5, ["destination-out"]: 6, ["destination-atop"]: 7, ["xor"]: 8, ["copy"]: 10, // modulate (TODO: is this right) ["screen"]: 11, ["overlay"]: 12, ["darken"]: 13, ["lighten"]: 14, ["color-dodge"]: 15, ["color-burn"]: 16, ["hard-light"]: 17, ["soft-light"]: 18, ["difference"]: 19, ["exclusion"]: 20, ["multiply"]: 21, ["hue"]:22, ["saturation"] : 23, ["color"] : 24, ["luminosity"] : 25,} as const;
export type TextAlign = keyof typeof CTextAlign;export type TextDirection = keyof typeof CTextDirection;export type TextBaseline = keyof typeof CTextBaseline;export type LineCap = keyof typeof CLineCap;export type LineJoin = keyof typeof CLineJoin;export type CanvasImageSource = Canvas | Image;export type GlobalCompositeOperation = keyof typeof CGlobalCompositeOperation;export type ImageSmoothingQuality = keyof typeof CImageSmoothingQuality;
const METRICS = new Float32Array(10);const METRICS_PTR = Deno.UnsafePointer.of(METRICS);
export interface TextMetrics { width: number; actualBoundingBoxLeft: number; actualBoundingBoxRight: number; actualBoundingBoxAscent: number; actualBoundingBoxDescent: number; fontBoundingBoxAscent: number; fontBoundingBoxDescent: number; alphabeticBaseline: number; emHeightAscent: number; emHeightDescent: number; hangingBaseline: number; ideographicBaseline: number;}
export type Style = string | CanvasGradient | CanvasPattern;
const CFontStretch = { ["ultra-condensed"]: 1, ["50%"]: 1, ["extra-condensed"]: 2, ["62.5%"]: 2, ["condensed"]: 3, ["75%"]: 3, ["semi-condensed"]: 4, ["87.5%"]: 4, ["normal"]: 5, ["100%"]: 5, ["semi-expanded"]: 6, ["112.5%"]: 6, ["expanded"]: 7, ["125%"]: 7, ["extra-expanded"]: 8, ["150%"]: 8, ["ultra-expanded"]: 9, ["200%"]: 9,} as const;
const CFontVariantCaps = { ["normal"]: 0, ["small-caps"]: 1,} as const;
export type FontStretch = keyof typeof CFontStretch;export type FontVariantCaps = keyof typeof CFontVariantCaps;export type FontKerning = "auto" | "none" | "normal";export type TextRendering = | "auto" | "geometricPrecision" | "optimizeLegibility" | "optimizeSpeed";
const _canvas = Symbol("[[canvas]]");const _ptr = Symbol("[[ptr]]");const _fillStyle = Symbol("[[fillStyle]]");const _strokeStyle = Symbol("[[strokeStyle]]");const _shadowColor = Symbol("[[shadowColor]]");const _font = Symbol("[[font]]");const _fontStretch = Symbol("[[fontStretch]]");const _fontVariantCaps = Symbol("[[fontVariantCaps]]");const _lineDash = Symbol("[[lineDash]]");const _filter = Symbol("[[filter]]");
/** * @link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D */export class CanvasRenderingContext2D { /// Internal State
[_canvas]: Canvas; [_ptr]: Deno.PointerValue;
[_fillStyle]: Style = "black"; [_strokeStyle]: Style = "black"; [_shadowColor] = "black"; [_font] = "10px sans-serif"; [_fontStretch]: FontStretch = "normal"; [_fontVariantCaps]: FontVariantCaps = "normal"; [_lineDash]: number[] = []; [_filter] = "none";
/// For FFI interface get _unsafePointer(): Deno.PointerValue { return this[_ptr]; }
set _unsafePointer(ptr: Deno.PointerValue) { this[_ptr] = ptr; this[_fillStyle] = "black"; this[_strokeStyle] = "black"; this[_shadowColor] = "black"; this[_font] = "10px sans-serif"; this[_fontStretch] = "normal"; this[_fontVariantCaps] = "normal"; this[_lineDash] = []; this[_filter] = "none"; }
constructor(canvas: Canvas, ptr: Deno.PointerValue) { this[_canvas] = canvas; this[_ptr] = ptr; if (this[_ptr] === null) { throw new Error("Failed to create context"); } }
/// Drawing rectangles
clearRect(x: number, y: number, width: number, height: number) { sk_context_clear_rect(this[_ptr], x, y, width, height); }
fillRect(x: number, y: number, width: number, height: number) { sk_context_fill_rect(this[_ptr], x, y, width, height); }
strokeRect(x: number, y: number, width: number, height: number) { sk_context_stroke_rect(this[_ptr], x, y, width, height); }
/// Drawing text
fillText(text: string, x: number, y: number, maxWidth?: number) { const encoded = new TextEncoder().encode(text); if ( !sk_context_text( this[_ptr], encoded, encoded.byteLength, x, y, maxWidth ?? 100_000, 1, null, ) ) { throw new Error("failed to fill text"); } }
strokeText(text: string, x: number, y: number, maxWidth?: number) { const encoded = new TextEncoder().encode(text); if ( !sk_context_text( this[_ptr], encoded, encoded.byteLength, x, y, maxWidth ?? 100_000, 0, null, ) ) { throw new Error("failed to stroke text"); } }
measureText(text: string): TextMetrics { if (text.length === 0) { return { width: 0, actualBoundingBoxLeft: 0, actualBoundingBoxRight: 0, actualBoundingBoxAscent: 0, actualBoundingBoxDescent: 0, fontBoundingBoxAscent: 0, fontBoundingBoxDescent: 0, alphabeticBaseline: 0, emHeightAscent: 0, emHeightDescent: 0, ideographicBaseline: 0, hangingBaseline: 0, }; } const encoded = new TextEncoder().encode(text); if ( !sk_context_text( this[_ptr], encoded, encoded.byteLength, 0, 0, 100_000, 1, METRICS_PTR, ) ) { throw new Error("failed to measure text"); } return { width: METRICS[4], actualBoundingBoxLeft: METRICS[2], actualBoundingBoxRight: METRICS[3], actualBoundingBoxAscent: METRICS[0], actualBoundingBoxDescent: METRICS[1], fontBoundingBoxAscent: METRICS[5], fontBoundingBoxDescent: METRICS[6], alphabeticBaseline: METRICS[7], emHeightAscent: METRICS[5], emHeightDescent: METRICS[6], ideographicBaseline: METRICS[8], hangingBaseline: METRICS[9], }; }
/// Line styles
get lineWidth(): number { return sk_context_get_line_width(this[_ptr]); }
set lineWidth(value: number) { sk_context_set_line_width(this[_ptr], value); }
get lineCap(): LineCap { return CLineCap[sk_context_get_line_cap(this[_ptr])] as LineCap; }
set lineCap(value: LineCap) { sk_context_set_line_cap(this[_ptr], CLineCap[value]); }
get lineJoin(): LineJoin { return CLineJoin[sk_context_get_line_join(this[_ptr])] as LineJoin; }
set lineJoin(value: LineJoin) { sk_context_set_line_join(this[_ptr], CLineJoin[value]); }
get miterLimit(): number { return sk_context_get_miter_limit(this[_ptr]); }
set miterLimit(value: number) { sk_context_set_miter_limit(this[_ptr], value); }
getLineDash(): number[] { return this[_lineDash]; }
setLineDash(value: number[]) { this[_lineDash] = value; sk_context_set_line_dash(this[_ptr], new Float32Array(value), value.length); }
get lineDashOffset(): number { return sk_context_get_line_dash_offset(this[_ptr]); }
set lineDashOffset(value: number) { sk_context_set_line_dash_offset(this[_ptr], Number(value)); }
/// Text styles
set font(value: string) { const font = parseFont(value); if (font) { if ( sk_context_set_font( this[_ptr], font.size, cstr(font.family), font.weight, font.style, font.variant, font.stretch, ) ) { this[_font] = value; } } else { throw new Error("Invalid font"); } }
get font(): string { return this[_font]; }
get textAlign(): TextAlign { return CTextAlign[sk_context_get_text_align(this[_ptr])] as TextAlign; }
set textAlign(value: TextAlign) { sk_context_set_text_align(this[_ptr], CTextAlign[value]); }
get textBaseline(): TextBaseline { return CTextBaseline[ sk_context_get_text_baseline(this[_ptr]) ] as TextBaseline; }
set textBaseline(value: TextBaseline) { sk_context_set_text_baseline(this[_ptr], CTextBaseline[value]); }
get direction(): TextDirection { return CTextDirection[ sk_context_get_text_direction(this[_ptr]) ] as TextDirection; }
set direction(value: TextDirection) { sk_context_set_text_direction(this[_ptr], CTextDirection[value]); }
get letterSpacing(): string { return sk_context_get_letter_spacing(this[_ptr]) + "px"; }
set letterSpacing(value: number | string) { sk_context_set_letter_spacing( this[_ptr], typeof value === "number" ? value : parseFloat(value), ); }
get wordSpacing(): string { return sk_context_get_word_spacing(this[_ptr]) + "px"; }
set wordSpacing(value: number | string) { sk_context_set_word_spacing( this[_ptr], typeof value === "number" ? value : parseFloat(value), ); }
get fontKerning(): FontKerning { return "auto"; }
set fontKerning(value: FontKerning) { if (value !== "auto") { throw new Error("fontKerning only supports 'auto'"); } }
get fontStretch(): FontStretch { return this[_fontStretch]; }
set fontStretch(value: FontStretch) { const c = CFontStretch[value]; if (typeof c !== "number") { throw new Error("invalid fontStretch"); } this[_fontStretch] = value; sk_context_set_font_stretch(this[_ptr], c); }
get fontVariantCaps(): FontVariantCaps { return this[_fontVariantCaps]; }
set fontVariant(value: FontVariantCaps) { const c = CFontVariantCaps[value]; if (typeof c !== "number") { throw new Error("invalid fontVariantCaps"); } this[_fontVariantCaps] = value; sk_context_set_font_variant_caps(this[_ptr], c); }
get textRendering(): TextRendering { return "auto"; }
set textRendering(value: TextRendering) { if (value !== "auto") { throw new Error("textRendering only supports 'auto'"); } }
/// Fill and stroke styles
get fillStyle(): Style { return this[_fillStyle]; }
set fillStyle(value: Style) { if (typeof value === "string") { if (sk_context_set_fill_style(this[_ptr], cstr(value))) { this[_fillStyle] = value; } } else if ( typeof value === "object" && value !== null && value instanceof CanvasGradient ) { sk_context_set_fill_style_gradient(this[_ptr], value._unsafePointer); this[_fillStyle] = value; } else if ( typeof value === "object" && value !== null && value instanceof CanvasPattern ) { sk_context_set_fill_style_pattern(this[_ptr], value._unsafePointer); this[_fillStyle] = value; } else { throw new Error("Invalid fill style"); } }
get strokeStyle(): Style { return this[_strokeStyle]; }
set strokeStyle(value: Style) { if (typeof value === "string") { if (sk_context_set_stroke_style(this[_ptr], cstr(value))) { this[_strokeStyle] = value; } } else if ( typeof value === "object" && value !== null && value instanceof CanvasGradient ) { sk_context_set_stroke_style_gradient(this[_ptr], value._unsafePointer); this[_strokeStyle] = value; } else if ( typeof value === "object" && value !== null && value instanceof CanvasPattern ) { sk_context_set_stroke_style_pattern(this[_ptr], value._unsafePointer); this[_strokeStyle] = value; } else { throw new Error("Invalid stroke style"); } }
/// Gradients and patterns
createConicGradient( r: number, x: number, y: number, ): CanvasGradient { return new CanvasGradient(sk_gradient_create_conic(x, y, r)); }
createLinearGradient( x0: number, y0: number, x1: number, y1: number, ): CanvasGradient { return new CanvasGradient(sk_gradient_create_linear(x0, y0, x1, y1)); }
createRadialGradient( x0: number, y0: number, r0: number, x1: number, y1: number, r1: number, ): CanvasGradient { return new CanvasGradient( sk_gradient_create_radial(x0, y0, r0, x1, y1, r1), ); }
createPattern( image: CanvasPatternImage, repetition?: CanvasPatternRepeat, ): CanvasPattern { return new CanvasPattern(image, repetition || "repeat"); }
/// Shadows
get shadowBlur(): number { return sk_context_get_shadow_blur(this[_ptr]); }
set shadowBlur(value: number) { sk_context_set_shadow_blur(this[_ptr], value); }
get shadowColor(): string { return this[_shadowColor]; }
set shadowColor(value: string) { if (sk_context_set_shadow_color(this[_ptr], cstr(value))) { this[_shadowColor] = value; } }
get shadowOffsetX(): number { return sk_context_get_shadow_offset_x(this[_ptr]); }
set shadowOffsetX(value: number) { sk_context_set_shadow_offset_x(this[_ptr], value); }
get shadowOffsetY(): number { return sk_context_get_shadow_offset_y(this[_ptr]); }
set shadowOffsetY(value: number) { sk_context_set_shadow_offset_y(this[_ptr], value); }
/// Paths
beginPath() { sk_context_begin_path(this[_ptr]); }
closePath() { sk_context_close_path(this[_ptr]); }
moveTo(x: number, y: number) { sk_context_move_to(this[_ptr], x, y); }
lineTo(x: number, y: number) { sk_context_line_to(this[_ptr], x, y); }
bezierCurveTo( cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number, ) { sk_context_bezier_curve_to(this[_ptr], cp1x, cp1y, cp2x, cp2y, x, y); }
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) { sk_context_quadratic_curve_to(this[_ptr], cpx, cpy, x, y); }
arc( x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise: boolean, ) { sk_context_arc( this[_ptr], x, y, radius, startAngle, endAngle, anticlockwise ? 1 : 0, ); }
arcTo( x1: number, y1: number, x2: number, y2: number, radius: number, ) { sk_context_arc_to(this[_ptr], x1, y1, x2, y2, radius); }
ellipse( x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: boolean, ) { sk_context_ellipse( this[_ptr], x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise ? 1 : 0, ); }
rect(x: number, y: number, width: number, height: number) { sk_context_rect(this[_ptr], x, y, width, height); }
roundRect( x: number, y: number, width: number, height: number, r: RoundRectRadii, ) { sk_context_round_rect( this[_ptr], x, y, width, height, ...roundRectRadiiArg(r), ); }
/// Drawing paths
fill(): void; fill(path: Path2D): void; fill(rule: FillRule): void; fill(path: Path2D, rule: FillRule): void; fill(path?: Path2D | FillRule, rule?: FillRule) { const pathptr = typeof path === "object" && path !== null && path instanceof Path2D ? path._unsafePointer : null; const irule = (typeof path === "string" ? path : rule) ?? "nonzero"; sk_context_fill(this[_ptr], pathptr, irule === "evenodd" ? 1 : 0); }
stroke(path?: Path2D) { sk_context_stroke( this[_ptr], path ? path._unsafePointer : null, ); }
drawFocusIfNeeded() { throw new Error("Context.drawFocusIfNeeded() not implemented"); }
scrollPathIntoView() { throw new Error("Context.scrollPathIntoView() not implemented"); }
clip(): void; clip(path: Path2D): void; clip(fillRule: FillRule): void; clip(path: Path2D, fillRule: FillRule): void; clip( path?: Path2D | FillRule, fillRule?: FillRule, ) { const pathptr = typeof path === "object" && path !== null ? path._unsafePointer : null; const fillRuleStr = typeof path === "string" ? path : fillRule; const ifillRule = fillRuleStr === "evenodd" ? 1 : 0; sk_context_clip(this[_ptr], pathptr, ifillRule); }
isPointInPath( x: number, y: number, fillRule?: FillRule, ): boolean; isPointInPath( path: Path2D, x: number, y: number, fillRule?: FillRule, ): boolean; isPointInPath( path: Path2D | number, x: number, y?: number | FillRule, fillRule?: FillRule, ): boolean { const pathptr = typeof path === "object" && path !== null ? path._unsafePointer : null; const ifillRule = (typeof y === "string" ? y : fillRule) === "evenodd" ? 1 : 0; return sk_context_is_point_in_path( this[_ptr], typeof path === "number" ? path : x, typeof path === "number" ? x : y as number, pathptr, ifillRule, ) === 1; }
isPointInStroke( x: number, y: number, ): boolean; isPointInStroke( path: Path2D, x: number, y: number, ): boolean; isPointInStroke( path: Path2D | number, x: number, y?: number, ): boolean { const pathptr = typeof path === "object" && path !== null ? path._unsafePointer : null; return sk_context_is_point_in_stroke( this[_ptr], typeof path === "number" ? path : x, typeof path === "number" ? x : y as number, pathptr, ) === 1; }
/// Transformations
getTransform(): DOMMatrix { const f32 = new Float32Array(6); sk_context_get_transform(this[_ptr], f32); return new DOMMatrix(f32[0], f32[1], f32[2], f32[3], f32[4], f32[5]); }
rotate(angle: number) { sk_context_rotate(this[_ptr], angle); }
scale(x: number, y: number) { sk_context_scale(this[_ptr], x, y); }
translate(x: number, y: number) { sk_context_translate(this[_ptr], x, y); }
transform( a: number, b: number, c: number, d: number, e: number, f: number, ) { sk_context_transform(this[_ptr], a, b, c, d, e, f); }
setTransform( a: number, b: number, c: number, d: number, e: number, f: number, ): void; setTransform(matrix: DOMMatrix): void; setTransform( a: number | DOMMatrix, b?: number, c?: number, d?: number, e?: number, f?: number, ) { if (typeof a === "number") { sk_context_set_transform(this[_ptr], a, b!, c!, d!, e!, f!); } else { sk_context_set_transform(this[_ptr], a.a, a.b, a.c, a.d, a.e, a.f); } }
resetTransform() { sk_context_reset_transform(this[_ptr]); }
/// Compositing
get globalAlpha(): number { return sk_context_get_global_alpha(this[_ptr]); }
set globalAlpha(value: number) { sk_context_set_global_alpha(this[_ptr], value); }
get globalCompositeOperation(): GlobalCompositeOperation { const op = sk_context_get_global_composite_operation(this[_ptr]); return Object.entries(CGlobalCompositeOperation).find((e) => e[1] === op )![0] as GlobalCompositeOperation; }
set globalCompositeOperation(value: GlobalCompositeOperation) { sk_context_set_global_composite_operation( this[_ptr], CGlobalCompositeOperation[value], ); }
/// Drawing images
drawImage(image: CanvasImageSource, dx: number, dy: number): void; drawImage( image: CanvasImageSource, dx: number, dy: number, dw: number, dh: number, ): void; drawImage( image: CanvasImageSource, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number, ): void; drawImage( image: CanvasImageSource, adx: number, ady: number, adw?: number, adh?: number, asx?: number, asy?: number, asw?: number, ash?: number, ) { if (image instanceof Image && image._unsafePointer === null) { return; } const dx = asx ?? adx; const dy = asy ?? ady; const dw = asw ?? adw ?? image.width; const dh = ash ?? adh ?? image.height; const sx = asx === undefined ? 0 : adx; const sy = asy === undefined ? 0 : ady; const sw = asw === undefined ? image.width : adw ?? image.width; const sh = ash === undefined ? image.height : adh ?? image.height; sk_context_draw_image( this[_ptr], image instanceof Canvas ? image._unsafePointer : null, image instanceof Image ? image._unsafePointer : null, dx, dy, dw, dh, sx, sy, sw, sh, ); }
/// Pixel manipulation
createImageData(sw: number, sh: number): ImageData; createImageData(imagedata: ImageData): ImageData; createImageData(sw: number | ImageData, sh?: number) { if (typeof sw === "number") { return new ImageData(sw, sh!); } else { return new ImageData(sw.width, sw.height); } }
getImageData(sx: number, sy: number, sw: number, sh: number): ImageData { if (!(this[_canvas] instanceof Canvas)) { throw new Error("getImageData is only supported on Canvas"); } const data = new Uint8Array(sw * sh * 4); this[_canvas].readPixels(sx, sy, sw, sh, data, "srgb"); return new ImageData(data, sw, sh); }
putImageData( imagedata: ImageData, dx: number, dy: number, dirtyX?: number, dirtyY?: number, dirtyWidth?: number, dirtyHeight?: number, ) { if (dirtyX !== undefined) { dirtyX = dirtyX ?? 0; dirtyY = dirtyY ?? 0; dirtyWidth = dirtyWidth ?? imagedata.width; dirtyHeight = dirtyHeight ?? imagedata.height; if (dirtyWidth < 0) { dirtyX += dirtyWidth; dirtyWidth = Math.abs(dirtyWidth); } if (dirtyHeight < 0) { dirtyY += dirtyHeight; dirtyHeight = Math.abs(dirtyHeight); } if (dirtyX < 0) { dirtyWidth += dirtyX; dirtyX = 0; } if (dirtyY < 0) { dirtyHeight += dirtyY; dirtyY = 0; } if (dirtyWidth <= 0 || dirtyHeight <= 0) { return; } sk_context_put_image_data_dirty( this[_ptr], imagedata.width, imagedata.height, new Uint8Array(imagedata.data.buffer), imagedata.width * 4, imagedata.data.byteLength, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight, imagedata.colorSpace === "srgb" ? 0 : 1, ); } else { sk_context_put_image_data( this[_ptr], imagedata.width, imagedata.height, new Uint8Array(imagedata.data.buffer), imagedata.width * 4, dx, dy, ); } }
/// Image smoothing
get imageSmoothingEnabled(): boolean { return sk_context_get_image_smoothing_enabled(this[_ptr]) === 1; }
set imageSmoothingEnabled(value: boolean) { sk_context_set_image_smoothing_enabled(this[_ptr], value ? 1 : 0); }
get imageSmoothingQuality(): ImageSmoothingQuality { const quality = sk_context_get_image_smoothing_quality(this[_ptr]); return CImageSmoothingQuality[quality] as ImageSmoothingQuality; }
set imageSmoothingQuality(value: ImageSmoothingQuality) { sk_context_set_image_smoothing_quality( this[_ptr], CImageSmoothingQuality[value], ); }
/// The canvas state
save() { sk_context_save(this[_ptr]); }
restore() { sk_context_restore(this[_ptr]); }
get canvas(): Canvas { return this[_canvas]; }
getContextAttributes(): { alpha: boolean; desynchronized: boolean; } { return { alpha: true, desynchronized: false, }; }
reset() { throw new Error("TODO: Context.reset()"); }
isContextLost(): boolean { return false; }
/// Filters
get filter(): string { return this[_filter]; }
set filter(value: string) { if (value === "none" || value === "") { sk_context_filter_reset(this[_ptr]); this[_filter] = value; return; } const filters = parseFilterString(value); this[_filter] = value; for (const filter of filters) { switch (filter.type) { case FilterType.Blur: sk_context_filter_blur(this[_ptr], filter.value); break;
case FilterType.Brightness: sk_context_filter_brightness(this[_ptr], filter.value); break;
case FilterType.Contrast: sk_context_filter_contrast(this[_ptr], filter.value); break;
case FilterType.DropShadow: sk_context_filter_drop_shadow( this[_ptr], filter.dx, filter.dy, filter.radius, cstr(filter.color), ); break;
case FilterType.Grayscale: sk_context_filter_grayscale(this[_ptr], filter.value); break;
case FilterType.HueRotate: sk_context_filter_hue_rotate(this[_ptr], filter.value); break;
case FilterType.Invert: sk_context_filter_invert(this[_ptr], filter.value); break;
case FilterType.Opacity: sk_context_filter_opacity(this[_ptr], filter.value); break;
case FilterType.Saturate: sk_context_filter_saturated(this[_ptr], filter.value); break;
case FilterType.Sepia: sk_context_filter_sepia(this[_ptr], filter.value); break; } } }}
skia_canvas

Version Info

Tagged at
8 months ago