forked from mstancombe/HTML-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageAdapter.cs
More file actions
100 lines (89 loc) · 2.82 KB
/
ImageAdapter.cs
File metadata and controls
100 lines (89 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using SkiaSharp;
using TheArtOfDev.HtmlRenderer.Adapters;
using Svg.Skia;
namespace TheArtOfDev.HtmlRenderer.SkiaSharp.Adapters
{
/// <summary>
/// Adapter for SkiaSharp Image object for core.
/// </summary>
internal sealed class ImageAdapter : RImage
{
/// <summary>
/// the underlying image. This may be either a bitmap (_image) or an svg (_svg).
/// </summary>
private SKImage? _image;
private readonly SKSvg? _svg;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object"/> class.
/// </summary>
public ImageAdapter(SKImage image)
{
_image = image;
}
public ImageAdapter(SKSvg svg)
{
_svg = svg;
}
/// <summary>
/// the underline SkiaSharp image.
/// </summary>
public SKImage Image
{
get
{
if (_image == null && _svg?.Picture != null)
{
//Render the image from the picture, as this is being used in a texture brush.
_image = SKImage.FromPicture(_svg.Picture, _svg.Picture.CullRect.Size.ToSizeI());
}
return _image;
}
}
public override double Width
{
get { return _image?.Width ?? _svg?.Picture?.CullRect.Width ?? 0; }
}
public override double Height
{
get { return _image?.Height ?? _svg?.Picture?.CullRect.Height ?? 0; }
}
public override void Dispose()
{
_image?.Dispose();
_svg?.Picture?.Dispose();
}
internal void DrawImage(SKCanvas canvas, SKRect dstRect, SKRect? srcRect = null)
{
if (_svg != null)
{
//TODO: support the overload that passes a source rect. Using Matrix overload perhaps?..
var matrix = SKMatrix.CreateTranslation(dstRect.Left, dstRect.Top);
matrix.ScaleX = dstRect.Width / _svg.Picture.CullRect.Width;
matrix.ScaleY = dstRect.Height / _svg.Picture.CullRect.Height;
canvas.DrawPicture(_svg.Picture, ref matrix);
}
else
{
if (srcRect != null)
{
canvas.DrawImage(_image, dstRect, srcRect.Value);
}
else
{
canvas.DrawImage(_image, dstRect);
}
}
}
}
}