Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jan 26, 2024
1 parent 6090b70 commit bb35740
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
8 changes: 4 additions & 4 deletions src/Avalonia.Svg.Skia/SvgImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang

if (change.Property == StyleProperty)
{
var style = string.Concat(Style, ' ', CurrentStyle);
var style = string.Concat(change.GetNewValue<string>(), ' ', CurrentStyle);

if (Source?.Parameters?.Style != style)
if (Source?.Style != style)
{
Source?.ReLoad(new SvgParameters(null, style));
}
}

if (change.Property == CurrentStyleProperty)
{
var style = string.Concat(Style, ' ', CurrentStyle);
var style = string.Concat(Style, ' ', change.GetNewValue<string>());

if (Source?.Parameters?.Style != style)
if (Source?.Style != style)
{
Source?.ReLoad(new SvgParameters(null, style));
}
Expand Down
93 changes: 52 additions & 41 deletions src/Svg.Skia/SKSvg.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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<string, string>? Entities => _originalParameters?.Entities;

public SKSvg()
{
Expand All @@ -105,59 +107,52 @@ 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);

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);
Expand All @@ -176,6 +171,22 @@ public SKSvg()
return null;
}

public SkiaSharp.SKPicture? ReLoad(SvgParameters? parameters)
{
Reset();

_originalParameters = parameters;

if (_originalStream == null)
{
return Load(_originalPath, parameters);

Check warning on line 182 in src/Svg.Skia/SKSvg.Model.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'path' in 'SKPicture? SKSvg.Load(string path, SvgParameters? parameters = null)'.

Check warning on line 182 in src/Svg.Skia/SKSvg.Model.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'path' in 'SKPicture? SKSvg.Load(string path, SvgParameters? parameters = null)'.

Check warning on line 182 in src/Svg.Skia/SKSvg.Model.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Possible null reference argument for parameter 'path' in 'SKPicture? SKSvg.Load(string path, SvgParameters? parameters = null)'.

Check warning on line 182 in src/Svg.Skia/SKSvg.Model.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'path' in 'SKPicture? SKSvg.Load(string path, SvgParameters? parameters = null)'.

Check warning on line 182 in src/Svg.Skia/SKSvg.Model.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'path' in 'SKPicture? SKSvg.Load(string path, SvgParameters? parameters = null)'.

Check warning on line 182 in src/Svg.Skia/SKSvg.Model.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Possible null reference argument for parameter 'path' in 'SKPicture? SKSvg.Load(string path, SvgParameters? parameters = null)'.
}

_originalStream.Position = 0;

return Load(_originalStream, parameters);
}

public SkiaSharp.SKPicture? FromSvg(string svg)
{
Reset();
Expand Down Expand Up @@ -234,6 +245,6 @@ private void Reset()
public void Dispose()
{
Reset();
Stream?.Dispose();
_originalStream?.Dispose();
}
}

0 comments on commit bb35740

Please sign in to comment.