@@ -15,15 +15,19 @@ namespace Uno.UI.Svg;
15
15
internal partial class SvgCanvas : SKXamlCanvas
16
16
{
17
17
private readonly SvgImageSource _svgImageSource ;
18
- private SKSvg ? _skSvg ;
18
+ private readonly SvgProvider _svgProvider ;
19
19
private readonly CompositeDisposable _disposables = new ( ) ;
20
+ private SKMatrix _currentScaleMatrix = default ;
20
21
21
- public SvgCanvas ( SvgImageSource svgImageSource )
22
+ public SvgCanvas ( SvgImageSource svgImageSource , SvgProvider svgProvider )
22
23
{
23
24
_svgImageSource = svgImageSource ;
25
+ _svgProvider = svgProvider ;
26
+
24
27
SizeChanged += SvgCanvas_SizeChanged ;
25
28
26
- _disposables . Add ( _svgImageSource . Subscribe ( OnSourceOpened ) ) ;
29
+ _svgProvider . SourceLoaded += SvgProviderSourceOpened ;
30
+ _disposables . Add ( ( ) => _svgProvider . SourceLoaded -= SvgProviderSourceOpened ) ;
27
31
_disposables . Add ( _svgImageSource . RegisterDisposablePropertyChangedCallback ( SvgImageSource . UriSourceProperty , SourcePropertyChanged ) ) ;
28
32
_disposables . Add ( _svgImageSource . RegisterDisposablePropertyChangedCallback ( SvgImageSource . RasterizePixelHeightProperty , SourcePropertyChanged ) ) ;
29
33
_disposables . Add ( _svgImageSource . RegisterDisposablePropertyChangedCallback ( SvgImageSource . RasterizePixelWidthProperty , SourcePropertyChanged ) ) ;
@@ -40,76 +44,76 @@ public SvgCanvas(SvgImageSource svgImageSource)
40
44
41
45
private void SvgCanvas_SizeChanged ( object sender , Windows . UI . Xaml . SizeChangedEventArgs args ) => Invalidate ( ) ;
42
46
43
- protected override Size MeasureOverride ( Size availableSize )
47
+ private void SvgProviderSourceOpened ( object sender , EventArgs e )
44
48
{
45
- if ( _skSvg ? . Picture != null )
46
- {
47
- // TODO:MZ: Handle case where SVG is rasterized
48
- var measuredSize = new Size ( _skSvg . Picture . CullRect . Width , _skSvg . Picture . CullRect . Height ) ;
49
- return measuredSize ;
50
- //Size ret;
51
-
52
- //if (
53
- // double.IsInfinity(availableSize.Width)
54
- // && double.IsInfinity(availableSize.Height)
55
- //)
56
- //{
57
- // ret = measuredSize;
58
- //}
59
- //else
60
- //{
61
- // ret = ImageSizeHelper.AdjustSize(availableSize, measuredSize);
62
- //}
63
-
64
- // Always making sure the ret size isn't bigger than the available size for an image with a fixed width or height
65
- //ret = new Size(
66
- // !Double.IsNaN(Width) && (ret.Width > availableSize.Width) ? availableSize.Width : ret.Width,
67
- // !Double.IsNaN(Height) && (ret.Height > availableSize.Height) ? availableSize.Height : ret.Height
68
- //);
69
-
70
- //if (this.Log().IsEnabled(LogLevel.Debug))
71
- //{
72
- // this.Log().LogDebug($"Measure {this} availableSize:{availableSize} measuredSize:{_lastMeasuredSize} ret:{ret} Stretch: {Stretch} Width:{Width} Height:{Height}");
73
- //}
74
- }
75
- else
76
- {
77
- return default ;
78
- }
49
+ InvalidateMeasure ( ) ;
50
+ Invalidate ( ) ;
79
51
}
80
52
53
+ //protected override Size MeasureOverride(Size availableSize)
54
+ //{
55
+ // if (_skSvg?.Picture != null)
56
+ // {
57
+ // // TODO:MZ: Handle case where SVG is rasterized
58
+ // var measuredSize = new Size(_skSvg.Picture.CullRect.Width, _skSvg.Picture.CullRect.Height);
59
+ // return measuredSize;
60
+ // //Size ret;
61
+
62
+ // //if (
63
+ // // double.IsInfinity(availableSize.Width)
64
+ // // && double.IsInfinity(availableSize.Height)
65
+ // //)
66
+ // //{
67
+ // // ret = measuredSize;
68
+ // //}
69
+ // //else
70
+ // //{
71
+ // // ret = ImageSizeHelper.AdjustSize(availableSize, measuredSize);
72
+ // //}
73
+
74
+ // // Always making sure the ret size isn't bigger than the available size for an image with a fixed width or height
75
+ // //ret = new Size(
76
+ // // !Double.IsNaN(Width) && (ret.Width > availableSize.Width) ? availableSize.Width : ret.Width,
77
+ // // !Double.IsNaN(Height) && (ret.Height > availableSize.Height) ? availableSize.Height : ret.Height
78
+ // //);
79
+
80
+ // //if (this.Log().IsEnabled(LogLevel.Debug))
81
+ // //{
82
+ // // this.Log().LogDebug($"Measure {this} availableSize:{availableSize} measuredSize:{_lastMeasuredSize} ret:{ret} Stretch: {Stretch} Width:{Width} Height:{Height}");
83
+ // //}
84
+ // }
85
+ // else
86
+ // {
87
+ // return default;
88
+ // }
89
+ //}
90
+
81
91
protected override Size ArrangeOverride ( Size finalSize )
82
92
{
83
- return base . ArrangeOverride ( finalSize ) ;
84
- }
93
+ finalSize = base . ArrangeOverride ( finalSize ) ;
85
94
86
- private void OnSourceOpened ( ImageData obj )
87
- {
88
- try
95
+ SKMatrix scaleMatrix = default ;
96
+ if ( _svgProvider . SkSvg ? . Picture ? . CullRect is { } rect )
89
97
{
90
- _skSvg = new SKSvg ( ) ;
91
- using var memoryStream = new MemoryStream ( obj . Data ) ;
92
- _skSvg . Load ( memoryStream ) ;
93
- InvalidateMeasure ( ) ;
94
- Invalidate ( ) ;
98
+ scaleMatrix = SKMatrix . CreateScale ( ( float ) finalSize . Width / rect . Width , ( float ) finalSize . Height / rect . Height ) ;
99
+
95
100
}
96
- catch ( Exception )
101
+
102
+ if ( scaleMatrix != _currentScaleMatrix )
97
103
{
98
- _svgImageSource . RaiseImageFailed ( SvgImageSourceLoadStatus . InvalidFormat ) ;
104
+ _currentScaleMatrix = scaleMatrix ;
105
+ Invalidate ( ) ;
99
106
}
107
+
108
+ return finalSize ;
100
109
}
101
110
111
+
102
112
protected override void OnPaintSurface ( SKPaintSurfaceEventArgs e )
103
113
{
104
- if ( _skSvg is not null )
114
+ if ( _svgProvider . SkSvg ? . Picture is { } picture )
105
115
{
106
- var scale = SKMatrix . CreateScale ( 0.5f , 0.5f ) ;
107
- var image = Parent as Image ;
108
- var width = ActualWidth ;
109
- var height = ActualHeight ;
110
- var fit = _skSvg . Picture ! . CullRect . AspectFit ( new SKSize ( ( float ) width , ( float ) height ) ) ;
111
- //scale = SKMatrix.CreateScale((float)fit.Width / (float)_skSvg.Picture!.CullRect.Width, (float)fit.Height / (float)_skSvg.Picture!.CullRect.Height);
112
- e . Surface . Canvas . DrawPicture ( _skSvg . Picture , ref scale ) ;
116
+ e . Surface . Canvas . DrawPicture ( picture , ref _currentScaleMatrix ) ;
113
117
}
114
118
if ( double . IsNaN ( _svgImageSource . RasterizePixelHeight ) && double . IsNaN ( _svgImageSource . RasterizePixelWidth ) )
115
119
{
0 commit comments