Skip to content

Commit

Permalink
Update SKSvg.Model.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jan 27, 2024
1 parent 3ea0d0a commit e7cfddd
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/Svg.Skia/SKSvg.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Svg.Skia;

public class SKSvg : IDisposable
{
public static bool CacheOriginalStream { get; set; }

public static SKSvg CreateFromStream(System.IO.Stream stream, SvgParameters? parameters = null)
{
var skSvg = new SKSvg();
Expand Down Expand Up @@ -108,21 +110,34 @@ public SKSvg()
{
Reset();

if (_originalStream != stream)
SvgDocument? svgDocument;

if (CacheOriginalStream)
{
_originalStream?.Dispose();
_originalStream = new System.IO.MemoryStream();
stream.CopyTo(_originalStream);
if (_originalStream != stream)
{
_originalStream?.Dispose();
_originalStream = new System.IO.MemoryStream();
stream.CopyTo(_originalStream);
}

_originalPath = null;
_originalParameters = parameters;
_originalStream.Position = 0;

svgDocument = SvgExtensions.Open(_originalStream, parameters);
if (svgDocument is null)
{
return null;
}
}

_originalPath = null;
_originalParameters = parameters;
_originalStream.Position = 0;

var svgDocument = SvgExtensions.Open(_originalStream, parameters);
if (svgDocument is null)
else
{
return null;
svgDocument = SvgExtensions.Open(stream, parameters);
if (svgDocument is null)
{
return null;
}
}

Model = SvgExtensions.ToModel(svgDocument, AssetLoader, out var drawable, out _);
Expand Down Expand Up @@ -173,6 +188,11 @@ public SKSvg()

public SkiaSharp.SKPicture? ReLoad(SvgParameters? parameters)
{
if (!CacheOriginalStream)
{
throw new ArgumentException($"Enable {nameof(CacheOriginalStream)} feature toggle to enable reload feature.");
}

Reset();

_originalParameters = parameters;
Expand All @@ -183,7 +203,7 @@ public SKSvg()
}

_originalStream.Position = 0;

return Load(_originalStream, parameters);
}

Expand Down

0 comments on commit e7cfddd

Please sign in to comment.