7
7
8
8
extern sk_sp<skia::textlayout::FontCollection> fontCollection;
9
9
10
+ void free_style (Style* style) {
11
+ if (style->shader != nullptr ) {
12
+ style->shader .~sk_sp ();
13
+ }
14
+ }
15
+
16
+ void free_font (Font* font) {
17
+ free (font->family );
18
+ free (font);
19
+ }
20
+
21
+ Style clone_style (Style* style) {
22
+ Style new_style;
23
+ new_style.type = style->type ;
24
+ if (style->type == kStyleColor ) {
25
+ new_style.color = style->color ;
26
+ } else if (style->type == kStyleShader ) {
27
+ new_style.shader = sk_sp (style->shader );
28
+ }
29
+ return new_style;
30
+ }
31
+
10
32
sk_context_state* create_default_state () {
11
33
sk_context_state* state = new sk_context_state ();
12
34
state->paint = new SkPaint ();
@@ -53,10 +75,8 @@ sk_context_state* clone_context_state(sk_context_state* state) {
53
75
new_state->lineDash = state->lineDash ;
54
76
new_state->globalAlpha = state->globalAlpha ;
55
77
new_state->lineDashOffset = state->lineDashOffset ;
56
- new_state->fillStyle = state->fillStyle ;
57
- if (new_state->fillStyle .shader ) new_state->fillStyle .shader = sk_sp (new_state->fillStyle .shader );
58
- new_state->strokeStyle = state->strokeStyle ;
59
- if (new_state->strokeStyle .shader ) new_state->strokeStyle .shader = sk_sp (new_state->strokeStyle .shader );
78
+ new_state->fillStyle = clone_style (&state->fillStyle );
79
+ new_state->strokeStyle = clone_style (&state->strokeStyle );
60
80
new_state->shadowColor = state->shadowColor ;
61
81
new_state->transform = new SkMatrix (*state->transform );
62
82
new_state->imageSmoothingEnabled = state->imageSmoothingEnabled ;
@@ -78,12 +98,12 @@ sk_context_state* clone_context_state(sk_context_state* state) {
78
98
}
79
99
80
100
void free_context_state (sk_context_state* state) {
81
- if ( state->fillStyle . shader ) state-> fillStyle . shader . ~sk_sp ( );
82
- if ( state->strokeStyle . shader ) state-> strokeStyle . shader . ~sk_sp ( );
101
+ free_style (& state->fillStyle );
102
+ free_style (& state->strokeStyle );
83
103
if (state->filter .get () != nullptr ) state->filter .~sk_sp ();
84
104
delete state->paint ;
85
105
delete state->transform ;
86
- free (state->font );
106
+ free_font (state->font );
87
107
}
88
108
89
109
// Utility
@@ -215,6 +235,7 @@ void applyShadowOffsetMatrix(sk_context* context) {
215
235
shadowOffset->preTranslate (shadowOffsetX, shadowOffsetY);
216
236
canvas->concat (*shadowOffset);
217
237
canvas->concat (cts);
238
+ delete shadowOffset;
218
239
}
219
240
220
241
extern " C" {
@@ -242,8 +263,10 @@ extern "C" {
242
263
applyShadowOffsetMatrix (context);
243
264
canvas->drawRect (rect, *shadowPaint);
244
265
canvas->restore ();
266
+ delete shadowPaint;
245
267
}
246
268
canvas->drawRect (rect, *fillPaint);
269
+ delete fillPaint;
247
270
}
248
271
249
272
// Context.strokeRect()
@@ -257,8 +280,10 @@ extern "C" {
257
280
applyShadowOffsetMatrix (context);
258
281
canvas->drawRect (rect, *shadowPaint);
259
282
canvas->restore ();
283
+ delete shadowPaint;
260
284
}
261
285
canvas->drawRect (rect, *strokePaint);
286
+ delete strokePaint;
262
287
}
263
288
264
289
// / Drawing text
@@ -446,10 +471,11 @@ extern "C" {
446
471
shadowPaint
447
472
);
448
473
context->canvas ->restore ();
474
+ delete shadowPaint;
449
475
if (res == 0 ) return 0 ;
450
476
}
451
477
}
452
- return sk_context_text_base (
478
+ auto res = sk_context_text_base (
453
479
context,
454
480
text,
455
481
textLen,
@@ -460,6 +486,8 @@ extern "C" {
460
486
out_metrics,
461
487
paint
462
488
);
489
+ delete paint;
490
+ return res;
463
491
}
464
492
465
493
// Context.fillText() implementation in JS using sk_context_test
@@ -575,7 +603,7 @@ extern "C" {
575
603
int variant,
576
604
int stretch
577
605
) {
578
- free (context->state ->font );
606
+ free_font (context->state ->font );
579
607
context->state ->font = new Font ();
580
608
context->state ->font ->family = strdup (family);
581
609
context->state ->font ->size = size;
@@ -657,6 +685,7 @@ extern "C" {
657
685
int sk_context_set_fill_style (sk_context* context, char * style) {
658
686
auto color = CSSColorParser::parse (std::string (style));
659
687
if (color) {
688
+ free_style (&context->state ->fillStyle );
660
689
auto val = color.value ();
661
690
context->state ->fillStyle = Style ();
662
691
context->state ->fillStyle .type = kStyleColor ;
@@ -667,12 +696,14 @@ extern "C" {
667
696
}
668
697
669
698
void sk_context_set_fill_style_gradient (sk_context* context, sk_gradient* gradient) {
699
+ free_style (&context->state ->fillStyle );
670
700
context->state ->fillStyle = Style ();
671
701
context->state ->fillStyle .type = kStyleShader ;
672
702
context->state ->fillStyle .shader = sk_gradient_to_shader (gradient, context->state ->transform );
673
703
}
674
704
675
705
void sk_context_set_fill_style_pattern (sk_context* context, sk_pattern* pattern) {
706
+ free_style (&context->state ->fillStyle );
676
707
context->state ->fillStyle = Style ();
677
708
context->state ->fillStyle .type = kStyleShader ;
678
709
context->state ->fillStyle .shader = sk_pattern_to_shader (pattern);
@@ -684,6 +715,7 @@ extern "C" {
684
715
int sk_context_set_stroke_style (sk_context* context, char * style) {
685
716
auto color = CSSColorParser::parse (std::string (style));
686
717
if (color) {
718
+ free_style (&context->state ->strokeStyle );
687
719
auto val = color.value ();
688
720
context->state ->strokeStyle = Style ();
689
721
context->state ->strokeStyle .type = kStyleColor ;
@@ -694,12 +726,14 @@ extern "C" {
694
726
}
695
727
696
728
void sk_context_set_stroke_style_gradient (sk_context* context, sk_gradient* gradient) {
729
+ free_style (&context->state ->strokeStyle );
697
730
context->state ->strokeStyle = Style ();
698
731
context->state ->strokeStyle .type = kStyleShader ;
699
732
context->state ->strokeStyle .shader = sk_gradient_to_shader (gradient, context->state ->transform );
700
733
}
701
734
702
735
void sk_context_set_stroke_style_pattern (sk_context* context, sk_pattern* pattern) {
736
+ free_style (&context->state ->strokeStyle );
703
737
context->state ->strokeStyle = Style ();
704
738
context->state ->strokeStyle .type = kStyleShader ;
705
739
context->state ->strokeStyle .shader = sk_pattern_to_shader (pattern);
@@ -831,8 +865,10 @@ extern "C" {
831
865
applyShadowOffsetMatrix (context);
832
866
canvas->drawPath (*path, *shadowPaint);
833
867
canvas->restore ();
868
+ delete shadowPaint;
834
869
}
835
870
canvas->drawPath (*path, *paint);
871
+ delete paint;
836
872
}
837
873
838
874
// Context.stroke()
@@ -846,8 +882,10 @@ extern "C" {
846
882
applyShadowOffsetMatrix (context);
847
883
canvas->drawPath (*path, *shadowPaint);
848
884
canvas->restore ();
885
+ delete shadowPaint;
849
886
}
850
887
canvas->drawPath (*path, *strokePaint);
888
+ delete strokePaint;
851
889
}
852
890
853
891
// TODO?: Context.drawFocusIfNeeded() (should we support it?)
@@ -1080,6 +1118,7 @@ extern "C" {
1080
1118
shadowPaint,
1081
1119
SkCanvas::kFast_SrcRectConstraint
1082
1120
);
1121
+ delete shadowPaint;
1083
1122
}
1084
1123
1085
1124
context->canvas ->drawImageRect (
0 commit comments