From bb357407150e505c7337826be9de34d3c5124fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20S=CC=8Colte=CC=81s?= Date: Fri, 26 Jan 2024 19:32:02 +0100 Subject: [PATCH] Refactor --- src/Avalonia.Svg.Skia/SvgImage.cs | 8 +-- src/Svg.Skia/SKSvg.Model.cs | 93 +++++++++++++++++-------------- 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/Avalonia.Svg.Skia/SvgImage.cs b/src/Avalonia.Svg.Skia/SvgImage.cs index 4c26370f..d683f637 100644 --- a/src/Avalonia.Svg.Skia/SvgImage.cs +++ b/src/Avalonia.Svg.Skia/SvgImage.cs @@ -103,9 +103,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang if (change.Property == StyleProperty) { - var style = string.Concat(Style, ' ', CurrentStyle); + var style = string.Concat(change.GetNewValue(), ' ', CurrentStyle); - if (Source?.Parameters?.Style != style) + if (Source?.Style != style) { Source?.ReLoad(new SvgParameters(null, style)); } @@ -113,9 +113,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang if (change.Property == CurrentStyleProperty) { - var style = string.Concat(Style, ' ', CurrentStyle); + var style = string.Concat(Style, ' ', change.GetNewValue()); - if (Source?.Parameters?.Style != style) + if (Source?.Style != style) { Source?.ReLoad(new SvgParameters(null, style)); } diff --git a/src/Svg.Skia/SKSvg.Model.cs b/src/Svg.Skia/SKSvg.Model.cs index 82b44846..e7d8bf8b 100644 --- a/src/Svg.Skia/SKSvg.Model.cs +++ b/src/Svg.Skia/SKSvg.Model.cs @@ -77,6 +77,10 @@ public static void Draw(SkiaSharp.SKCanvas skCanvas, string path, SkiaModel skia } } + private SvgParameters? _originalParameters; + private string? _originalPath; + private System.IO.Stream? _originalStream; + public SKSvgSettings Settings { get; } public IAssetLoader AssetLoader { get; } @@ -89,11 +93,9 @@ public static void Draw(SkiaSharp.SKCanvas skCanvas, string path, SkiaModel skia public SkiaSharp.SKPicture? Picture { get; private set; } - public SvgParameters? Parameters { get; set; } - - private string? Path { get; set; } + public string? Style => _originalParameters?.Style; - private System.IO.Stream? Stream { get; set; } + public Dictionary? Entities => _originalParameters?.Entities; public SKSvg() { @@ -105,39 +107,29 @@ public SKSvg() public SkiaSharp.SKPicture? Load(System.IO.Stream stream, SvgParameters? parameters = null) { Reset(); - if (Stream != stream) - { - Stream?.Dispose(); - Stream = new System.IO.MemoryStream(); - stream.CopyTo(Stream); - } - Path = null; - Parameters = parameters; - Stream.Position = 0; - var svgDocument = SvgExtensions.Open(Stream, parameters); - if (svgDocument is { }) + + if (_originalStream != stream) { - Model = SvgExtensions.ToModel(svgDocument, AssetLoader, out var drawable, out _); - Drawable = drawable; - Picture = SkiaModel.ToSKPicture(Model); - return Picture; + _originalStream?.Dispose(); + _originalStream = new System.IO.MemoryStream(); + stream.CopyTo(_originalStream); } - return null; - } - public SkiaSharp.SKPicture? ReLoad(SvgParameters? parameters) - { - Reset(); + _originalPath = null; + _originalParameters = parameters; + _originalStream.Position = 0; - Parameters = parameters; - - if (Stream == null) + var svgDocument = SvgExtensions.Open(_originalStream, parameters); + if (svgDocument is null) { - return Load(Path, parameters); + return null; } - Stream.Position = 0; - return Load(Stream, parameters); + Model = SvgExtensions.ToModel(svgDocument, AssetLoader, out var drawable, out _); + Drawable = drawable; + Picture = SkiaModel.ToSKPicture(Model); + + return Picture; } public SkiaSharp.SKPicture? Load(System.IO.Stream stream) => Load(stream, null); @@ -145,19 +137,22 @@ public SKSvg() public SkiaSharp.SKPicture? Load(string path, SvgParameters? parameters = null) { Reset(); - Path = path; - Parameters = parameters; - Stream?.Dispose(); - Stream = null; + + _originalPath = path; + _originalStream?.Dispose(); + _originalStream = null; + var svgDocument = SvgExtensions.Open(path, parameters); - if (svgDocument is { }) + if (svgDocument is null) { - Model = SvgExtensions.ToModel(svgDocument, AssetLoader, out var drawable, out _); - Drawable = drawable; - Picture = SkiaModel.ToSKPicture(Model); - return Picture; + return null; } - return null; + + Model = SvgExtensions.ToModel(svgDocument, AssetLoader, out var drawable, out _); + Drawable = drawable; + Picture = SkiaModel.ToSKPicture(Model); + + return Picture; } public SkiaSharp.SKPicture? Load(string path) => Load(path, null); @@ -176,6 +171,22 @@ public SKSvg() return null; } + public SkiaSharp.SKPicture? ReLoad(SvgParameters? parameters) + { + Reset(); + + _originalParameters = parameters; + + if (_originalStream == null) + { + return Load(_originalPath, parameters); + } + + _originalStream.Position = 0; + + return Load(_originalStream, parameters); + } + public SkiaSharp.SKPicture? FromSvg(string svg) { Reset(); @@ -234,6 +245,6 @@ private void Reset() public void Dispose() { Reset(); - Stream?.Dispose(); + _originalStream?.Dispose(); } }