diff --git a/samples/AvaloniaSvgSkiaSample/MainWindow.axaml b/samples/AvaloniaSvgSkiaSample/MainWindow.axaml index d9d83f41..871ab1c3 100644 --- a/samples/AvaloniaSvgSkiaSample/MainWindow.axaml +++ b/samples/AvaloniaSvgSkiaSample/MainWindow.axaml @@ -140,9 +140,16 @@ UniformToFill - + - + + + + + + diff --git a/src/Avalonia.Svg.Skia/Svg.cs b/src/Avalonia.Svg.Skia/Svg.cs index e61521a8..0539456b 100644 --- a/src/Avalonia.Svg.Skia/Svg.cs +++ b/src/Avalonia.Svg.Skia/Svg.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Text; using Avalonia.Controls; using Avalonia.Logging; using Avalonia.Media; @@ -246,38 +248,61 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang if (change.Property == PathProperty) { - var path = change.GetNewValue(); var css = GetCss(this); var currentCss = GetCurrentCss(this); var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); + var path = change.GetNewValue(); LoadFromPath(path, parameters); InvalidateVisual(); } if (change.Property == CssProperty) { - var path = Path; var css = change.GetNewValue(); var currentCss = GetCurrentCss(this); var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); - LoadFromPath(path, parameters); + var path = Path; + var source = Source; + + if (path is { }) + { + LoadFromPath(path, parameters); + } + else if (source is { }) + { + LoadFromSource(source, parameters); + } + InvalidateVisual(); } if (change.Property == CurrentCssProperty) { - var path = Path; var css = GetCss(this); var currentCss = change.GetNewValue(); var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); - LoadFromPath(path, parameters); + var path = Path; + var source = Source; + + if (path is { }) + { + LoadFromPath(path, parameters); + } + else if (source is { }) + { + LoadFromSource(source, parameters); + } + InvalidateVisual(); } if (change.Property == SourceProperty) { var source = change.GetNewValue(); - LoadFromSource(source); + var css = GetCss(this); + var currentCss = GetCurrentCss(this); + var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); + LoadFromSource(source, parameters); InvalidateVisual(); } @@ -333,7 +358,7 @@ private void LoadFromPath(string? path, SvgParameters? parameters = null) } } - private void LoadFromSource(string? source) + private void LoadFromSource(string? source, SvgParameters? parameters = null) { if (source is null) { @@ -345,7 +370,9 @@ private void LoadFromSource(string? source) try { - _svg = SvgSource.LoadFromSvg(source); + var bytes = Encoding.UTF8.GetBytes(source); + using var ms = new MemoryStream(bytes); + _svg = SvgSource.LoadFromStream(ms, parameters); } catch (Exception e) { diff --git a/src/Avalonia.Svg/Svg.cs b/src/Avalonia.Svg/Svg.cs index 693412a2..33a6e1cf 100644 --- a/src/Avalonia.Svg/Svg.cs +++ b/src/Avalonia.Svg/Svg.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Text; using Avalonia.Controls; using Avalonia.Media; using Avalonia.Metadata; @@ -239,28 +241,51 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang if (change.Property == CssProperty) { - var path = Path; var css = change.GetNewValue(); var currentCss = GetCurrentCss(this); var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); - LoadFromPath(path, parameters); + var path = Path; + var source = Source; + + if (path is { }) + { + LoadFromPath(path, parameters); + } + else if (source is { }) + { + LoadFromSource(source, parameters); + } + InvalidateVisual(); } if (change.Property == CurrentCssProperty) { - var path = Path; var css = GetCss(this); var currentCss = change.GetNewValue(); var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); - LoadFromPath(path, parameters); + var path = Path; + var source = Source; + + if (path is { }) + { + LoadFromPath(path, parameters); + } + else if (source is { }) + { + LoadFromSource(source, parameters); + } + InvalidateVisual(); } if (change.Property == SourceProperty) { + var css = GetCss(this); + var currentCss = GetCurrentCss(this); + var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss)); var source = change.GetNewValue(); - LoadFromSource(source); + LoadFromSource(source, parameters); InvalidateVisual(); } } @@ -280,14 +305,16 @@ private void LoadFromPath(string? path, SvgParameters? parameters = null) } } - private void LoadFromSource(string? source) + private void LoadFromSource(string? source, SvgParameters? parameters = null) { _picture = default; _avaloniaPicture?.Dispose(); if (source is not null) { - _picture = SvgSource.LoadPictureFromSvg(source); + var bytes = Encoding.UTF8.GetBytes(source); + using var ms = new MemoryStream(bytes); + _picture = SvgSource.LoadPicture(ms, parameters); if (_picture is { }) { _avaloniaPicture = AvaloniaPicture.Record(_picture); diff --git a/src/Avalonia.Svg/SvgSource.cs b/src/Avalonia.Svg/SvgSource.cs index 77c46db5..03f038aa 100644 --- a/src/Avalonia.Svg/SvgSource.cs +++ b/src/Avalonia.Svg/SvgSource.cs @@ -116,7 +116,7 @@ public static SvgSource Load(Stream stream, SvgParameters? parameters = null) /// /// The svg source. /// The svg picture. - public static SKPicture? LoadPictureFromSvg(string source) + public static SKPicture? LoadPictureFromSvg(string source, SvgParameters? parameters = null) { var document = SM.SvgExtensions.FromSvg(source); return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default;