Skip to content

Commit

Permalink
Merge pull request #212 from wieslawsoltes/206
Browse files Browse the repository at this point in the history
206
  • Loading branch information
wieslawsoltes authored Mar 14, 2024
2 parents 61990af + 4d77050 commit fae8793
Show file tree
Hide file tree
Showing 13 changed files with 2,340 additions and 22 deletions.
7 changes: 7 additions & 0 deletions Svg.Skia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Svg", "src\Avaloni
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaSvgSkiaStylingSample", "samples\AvaloniaSvgSkiaStylingSample\AvaloniaSvgSkiaStylingSample.csproj", "{8A938DC2-1634-4387-BAB3-69F871D54FB5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avalonia.Svg.Skia.UiTests", "tests\Avalonia.Svg.Skia.UiTests\Avalonia.Svg.Skia.UiTests.csproj", "{CDAEEF97-F162-468B-9424-6496253C2BFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -207,6 +209,10 @@ Global
{8A938DC2-1634-4387-BAB3-69F871D54FB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A938DC2-1634-4387-BAB3-69F871D54FB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A938DC2-1634-4387-BAB3-69F871D54FB5}.Release|Any CPU.Build.0 = Release|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -238,6 +244,7 @@ Global
{3049C672-8A3F-4FE4-9973-515B8323B546} = {4C42912C-9F8C-43D9-A4B5-4427F7EC8F18}
{B742F260-0EC6-4805-AE9F-987818CE3CF4} = {4C42912C-9F8C-43D9-A4B5-4427F7EC8F18}
{8A938DC2-1634-4387-BAB3-69F871D54FB5} = {B65D5B3A-77BE-4AFF-B502-A136B9C932F8}
{CDAEEF97-F162-468B-9424-6496253C2BFE} = {7863AE7D-FF68-45BF-BA68-6FA0E5604CB7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {12D5E557-A27B-4FB2-83A3-4AC75B04B22C}
Expand Down
22 changes: 16 additions & 6 deletions src/Avalonia.Svg.Skia/SvgCustomDrawOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,41 @@ public void Dispose()

public Rect Bounds { get; }

public bool HitTest(Point p) => false;
public bool HitTest(Point p) => true;

public bool Equals(ICustomDrawOperation? other) => false;

public void Render(ImmediateDrawingContext context)
{
if (_svg?.Picture is null)
if (_svg == null)
{
return;
}

var leaseFeature = context.TryGetFeature<ISkiaSharpApiLeaseFeature>();
if (leaseFeature is null)
{
return;
}

using var lease = leaseFeature.Lease();
var canvas = lease?.SkCanvas;
if (canvas is null)
{
return;
}

canvas.Save();
canvas.DrawPicture(_svg.Picture);
canvas.Restore();
lock (_svg.Sync)
{
var picture = _svg.Picture;
if (picture is null)
{
return;
}

canvas.Save();
canvas.DrawPicture(picture);
canvas.Restore();
}
}
}
39 changes: 24 additions & 15 deletions src/Svg.Skia/SKSvg.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public static void Draw(SkiaSharp.SKCanvas skCanvas, string path, SkiaModel skia
private string? _originalPath;
private System.IO.Stream? _originalStream;

public object Sync { get; } = new ();

public SKSvgSettings Settings { get; }

public IAssetLoader AssetLoader { get; }
Expand Down Expand Up @@ -181,23 +183,26 @@ public SKSvg()

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

Reset();
Reset();

_originalParameters = parameters;
_originalParameters = parameters;

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

Check warning on line 199 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 199 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 199 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)'.
}

_originalStream.Position = 0;
_originalStream.Position = 0;

return Load(_originalStream, parameters);
return Load(_originalStream, parameters);
}
}

public SkiaSharp.SKPicture? FromSvg(string svg)
Expand Down Expand Up @@ -246,10 +251,14 @@ public bool Save(string path, SkiaSharp.SKColor background, SkiaSharp.SKEncodedI

private void Reset()
{
Model = null;
Drawable = null;
Picture?.Dispose();
Picture = null;
lock (Sync)
{
Model = null;
Drawable = null;

Picture?.Dispose();
Picture = null;
}
}

public void Dispose()
Expand Down
8 changes: 8 additions & 0 deletions tests/Avalonia.Svg.Skia.UiTests/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Avalonia.Svg.Skia.UiTests.App"
RequestedThemeVariant="Light">
<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
23 changes: 23 additions & 0 deletions tests/Avalonia.Svg.Skia.UiTests/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

namespace Avalonia.Svg.Skia.UiTests;

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new Window();
}

base.OnFrameworkInitializationCompleted();
}
}
31 changes: 31 additions & 0 deletions tests/Avalonia.Svg.Skia.UiTests/Assets/Icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fae8793

Please sign in to comment.