Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
SvgResource.cs
1using System.Drawing;
2using SkiaSharp;
3using Svg.Skia;
4
5namespace Gondwana.Drawing;
6
10public sealed class SvgResource : IDisposable
11{
12 private readonly SKPicture _picture;
13 private readonly object _cacheLock = new();
14 private readonly Dictionary<(int Width, int Height), SKBitmap> _cachedBitmaps = new();
15 private bool _disposed;
16
17 private SvgResource(SKPicture picture, SizeF intrinsicSize)
18 {
19 _picture = picture;
20 IntrinsicSize = intrinsicSize;
21 }
22
28 public static SvgResource Load(string path)
29 {
30 ArgumentException.ThrowIfNullOrWhiteSpace(path);
31 using var stream = File.OpenRead(path);
32 return Load(stream);
33 }
34
35 internal static SvgResource Load(Stream stream)
36 {
37 ArgumentNullException.ThrowIfNull(stream);
38
39 var svg = new SKSvg();
40 svg.Load(stream);
41
42 if (svg.Picture is null)
43 throw new InvalidOperationException("Failed to parse SVG content.");
44
45 var cull = svg.Picture.CullRect;
46
47 var intrinsic = new SizeF(
48 cull.Width > 0 ? cull.Width : 1f,
49 cull.Height > 0 ? cull.Height : 1f);
50
51 return new SvgResource(svg.Picture, intrinsic);
52 }
53
57 public SizeF IntrinsicSize { get; }
58
69 public SKBitmap Rasterize(int width, int height)
70 {
71 ThrowIfDisposed();
72
73 if (width <= 0)
74 throw new ArgumentOutOfRangeException(nameof(width), "Width must be greater than 0.");
75
76 if (height <= 0)
77 throw new ArgumentOutOfRangeException(nameof(height), "Height must be greater than 0.");
78
79 lock (_cacheLock)
80 {
81 if (_cachedBitmaps.TryGetValue((width, height), out var cachedBitmap))
82 return cachedBitmap;
83
84 var bitmap = new SKBitmap(
85 width,
86 height,
87 SKColorType.Bgra8888,
88 SKAlphaType.Premul);
89
90 using var canvas = new SKCanvas(bitmap);
91 canvas.Clear(SKColors.Transparent);
92
93 var src = _picture.CullRect;
94
95 if (src.Width <= 0 || src.Height <= 0)
96 {
97 src = new SKRect(
98 0,
99 0,
100 Math.Max(1f, IntrinsicSize.Width),
101 Math.Max(1f, IntrinsicSize.Height));
102 }
103
104 canvas.Scale(width / src.Width, height / src.Height);
105 canvas.DrawPicture(_picture);
106 canvas.Flush();
107
108 _cachedBitmaps[(width, height)] = bitmap;
109 return bitmap;
110 }
111 }
112
118 public SKBitmap Rasterize(float scale = 1.0f)
119 {
120 ThrowIfDisposed();
121
122 if (scale <= 0f)
123 throw new ArgumentOutOfRangeException(nameof(scale), "Scale must be greater than 0.");
124
125 int width = Math.Max(1, (int)MathF.Round(IntrinsicSize.Width * scale));
126 int height = Math.Max(1, (int)MathF.Round(IntrinsicSize.Height * scale));
127 return Rasterize(width, height);
128 }
129
131 public void Dispose()
132 {
133 if (_disposed)
134 return;
135
136 lock (_cacheLock)
137 {
138 foreach (var bitmap in _cachedBitmaps.Values)
139 bitmap.Dispose();
140
141 _cachedBitmaps.Clear();
142 _picture.Dispose();
143 _disposed = true;
144 }
145 }
146
147 private void ThrowIfDisposed()
148 {
149 ObjectDisposedException.ThrowIf(_disposed, this);
150 }
151}
Represents a loaded SVG asset that can be rasterized into cached bitmaps at requested sizes.
SKBitmap Rasterize(int width, int height)
Rasterizes this SVG to an explicit pixel size.
SKBitmap Rasterize(float scale=1.0f)
Rasterizes this SVG to its intrinsic size multiplied by a scale factor.
SizeF IntrinsicSize
Gets the intrinsic (unscaled) SVG size in pixels.
static SvgResource Load(string path)
Loads an SVG resource from a file path.