forked from svg-net/SVG
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSvgPatternServer.cs
283 lines (252 loc) · 11.2 KB
/
SvgPatternServer.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using Svg.Transforms;
namespace Svg
{
/// <summary>
/// A pattern is used to fill or stroke an object using a pre-defined graphic object which can be replicated ("tiled") at fixed intervals in x and y to cover the areas to be painted.
/// </summary>
[SvgElement("pattern")]
public sealed class SvgPatternServer : SvgPaintServer, ISvgViewPort
{
private SvgUnit _x = SvgUnit.None;
private SvgUnit _y = SvgUnit.None;
private SvgUnit _width = SvgUnit.None;
private SvgUnit _height = SvgUnit.None;
private SvgCoordinateUnits? _patternUnits;
private SvgCoordinateUnits? _patternContentUnits;
private SvgViewBox _viewBox;
/// <summary>
/// Gets or sets the X-axis location of the pattern.
/// </summary>
[SvgAttribute("x")]
public SvgUnit X
{
get { return _x; }
set { _x = value; Attributes["x"] = value; }
}
/// <summary>
/// Gets or sets the Y-axis location of the pattern.
/// </summary>
[SvgAttribute("y")]
public SvgUnit Y
{
get { return _y; }
set { _y = value; Attributes["y"] = value; }
}
/// <summary>
/// Gets or sets the width of the pattern.
/// </summary>
[SvgAttribute("width")]
public SvgUnit Width
{
get { return _width; }
set { _width = value; Attributes["width"] = value; }
}
/// <summary>
/// Gets or sets the height of the pattern.
/// </summary>
[SvgAttribute("height")]
public SvgUnit Height
{
get { return _height; }
set { _height = value; Attributes["height"] = value; }
}
/// <summary>
/// Gets or sets the width of the pattern.
/// </summary>
[SvgAttribute("patternUnits")]
public SvgCoordinateUnits PatternUnits
{
get { return _patternUnits ?? SvgCoordinateUnits.ObjectBoundingBox; }
set { _patternUnits = value; Attributes["patternUnits"] = value; }
}
/// <summary>
/// Gets or sets the width of the pattern.
/// </summary>
[SvgAttribute("patternContentUnits")]
public SvgCoordinateUnits PatternContentUnits
{
get { return _patternContentUnits ?? SvgCoordinateUnits.UserSpaceOnUse; }
set { _patternContentUnits = value; Attributes["patternContentUnits"] = value; }
}
/// <summary>
/// Specifies a supplemental transformation which is applied on top of any
/// transformations necessary to create a new pattern coordinate system.
/// </summary>
[SvgAttribute("viewBox")]
public SvgViewBox ViewBox
{
get { return _viewBox; }
set { _viewBox = value; Attributes["viewBox"] = value; }
}
/// <summary>
/// Gets or sets another gradient fill from which to inherit the stops from.
/// </summary>
[SvgAttribute("href", SvgAttributeAttribute.XLinkNamespace)]
public SvgDeferredPaintServer InheritGradient
{
get { return GetAttribute<SvgDeferredPaintServer>("href", false); }
set { Attributes["href"] = value; }
}
[SvgAttribute("overflow")]
public SvgOverflow Overflow
{
get { return GetAttribute<SvgOverflow>("overflow", false); }
set { Attributes["overflow"] = value; }
}
/// <summary>
/// Gets or sets the aspect of the viewport.
/// </summary>
/// <value></value>
[SvgAttribute("preserveAspectRatio")]
public SvgAspectRatio AspectRatio
{
get { return GetAttribute("preserveAspectRatio", false, new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid)); }
set { Attributes["preserveAspectRatio"] = value; }
}
[SvgAttribute("patternTransform")]
public SvgTransformCollection PatternTransform
{
get { return GetAttribute<SvgTransformCollection>("patternTransform", false); }
set { Attributes["patternTransform"] = value; }
}
private Matrix EffectivePatternTransform
{
get
{
var transform = new Matrix();
if (PatternTransform != null)
using (var matrix = PatternTransform.GetMatrix())
transform.Multiply(matrix);
return transform;
}
}
/// <summary>
/// Check if <see cref="PatternUnits"/> property value was set.
/// </summary>
/// <returns>True if <see cref="PatternUnits"/> has value.</returns>
public bool HasPatternUnits()
{
return _patternUnits.HasValue;
}
/// <summary>
/// Check if <see cref="PatternContentUnits"/> property value was set.
/// </summary>
/// <returns>True if <see cref="PatternContentUnits"/> has value.</returns>
public bool HasPatternContentUnits()
{
return _patternContentUnits.HasValue;
}
/// <summary>
/// Gets a <see cref="Brush"/> representing the current paint server.
/// </summary>
/// <param name="renderingElement">The owner <see cref="SvgVisualElement"/>.</param>
/// <param name="renderer">The renderer object.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="forStroke">Not used.</param>
public override Brush GetBrush(SvgVisualElement renderingElement, ISvgRenderer renderer, float opacity, bool forStroke = false)
{
var chain = new List<SvgPatternServer>();
var curr = this;
do
{
chain.Add(curr);
curr = SvgDeferredPaintServer.TryGet<SvgPatternServer>(curr.InheritGradient, renderingElement);
} while (curr != null);
var firstChildren = chain.Where(p => p.Children.Count > 0).FirstOrDefault();
if (firstChildren == null)
return null;
var firstX = chain.Where(p => p.X != null && p.X != SvgUnit.None).FirstOrDefault();
var firstY = chain.Where(p => p.Y != null && p.Y != SvgUnit.None).FirstOrDefault();
var firstWidth = chain.Where(p => p.Width != null && p.Width != SvgUnit.None).FirstOrDefault();
var firstHeight = chain.Where(p => p.Height != null && p.Height != SvgUnit.None).FirstOrDefault();
if (firstWidth == null || firstHeight == null)
return null;
var firstPatternUnit = chain.Where(p => p._patternUnits.HasValue).FirstOrDefault();
var firstPatternContentUnit = chain.Where(p => p._patternContentUnits.HasValue).FirstOrDefault();
var firstViewBox = chain.Where(p => p.ViewBox != null && p.ViewBox != SvgViewBox.Empty).FirstOrDefault();
var xUnit = firstX == null ? new SvgUnit(0f) : firstX.X;
var yUnit = firstY == null ? new SvgUnit(0f) : firstY.Y;
var widthUnit = firstWidth.Width;
var heightUnit = firstHeight.Height;
var patternUnits = firstPatternUnit == null ? SvgCoordinateUnits.ObjectBoundingBox : firstPatternUnit.PatternUnits;
var patternContentUnits = firstPatternContentUnit == null ? SvgCoordinateUnits.UserSpaceOnUse : firstPatternContentUnit.PatternContentUnits;
var viewBox = firstViewBox == null ? SvgViewBox.Empty : firstViewBox.ViewBox;
var isPatternObjectBoundingBox = patternUnits == SvgCoordinateUnits.ObjectBoundingBox;
try
{
if (isPatternObjectBoundingBox)
renderer.SetBoundable(renderingElement);
var x = xUnit.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this);
var y = yUnit.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);
var width = widthUnit.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this);
var height = heightUnit.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);
if (isPatternObjectBoundingBox)
{
var bounds = renderer.GetBoundable().Bounds; // Boundary without stroke is expect...
if (xUnit.Type != SvgUnitType.Percentage)
x *= bounds.Width;
if (yUnit.Type != SvgUnitType.Percentage)
y *= bounds.Height;
if (widthUnit.Type != SvgUnitType.Percentage)
width *= bounds.Width;
if (heightUnit.Type != SvgUnitType.Percentage)
height *= bounds.Height;
x += bounds.X;
y += bounds.Y;
}
var tile = new Bitmap((int)Math.Ceiling(width), (int)Math.Ceiling(height));
using (var tileRenderer = SvgRenderer.FromImage(tile))
{
tileRenderer.SetBoundable(renderingElement);
if (viewBox != SvgViewBox.Empty)
{
var bounds = tileRenderer.GetBoundable().Bounds;
tileRenderer.ScaleTransform(width / viewBox.Width, height / viewBox.Height);
}
else if (patternContentUnits == SvgCoordinateUnits.ObjectBoundingBox)
{
var bounds = tileRenderer.GetBoundable().Bounds;
tileRenderer.ScaleTransform(bounds.Width, bounds.Height);
}
foreach (var child in firstChildren.Children)
child.RenderElement(tileRenderer);
}
using (var transform = EffectivePatternTransform)
{
var textureBrush = new TextureBrush(tile, new RectangleF(0f, 0f, width, height))
{
Transform = transform
};
textureBrush.TranslateTransform(x, y);
return textureBrush;
}
}
finally
{
if (isPatternObjectBoundingBox)
renderer.PopBoundable();
}
}
public override SvgElement DeepCopy()
{
return DeepCopy<SvgPatternServer>();
}
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as SvgPatternServer;
newObj._x = _x;
newObj._y = _y;
newObj._width = _width;
newObj._height = _height;
newObj._patternUnits = _patternUnits;
newObj._patternContentUnits = _patternContentUnits;
newObj._viewBox = _viewBox;
return newObj;
}
}
}