-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUseElement.cs
More file actions
99 lines (83 loc) · 2.76 KB
/
UseElement.cs
File metadata and controls
99 lines (83 loc) · 2.76 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
#region copyright LGPL nanoLogika
// Copyright 2023, nanoLogika GmbH.
// All rights reserved.
// This source code is licensed under the "LGPL v3 or any later version" license.
// See LICENSE file in the project root for full license information.
#endregion
using System.Xml.Linq;
namespace SvgElements {
/// <summary>
/// Represents SVG <i>use</i> element.
/// </summary>
public class UseElement : SvgElementBase<UseElement> {
private string _groupId;
/// <summary>
/// Gets or sets an optional x-coordinate of the location for the
/// content referenced by this <see cref="UseElement"/>. Default
/// is x = 0.
/// </summary>
public double? X { get; set; }
/// <summary>
/// Gets or sets an optional y-coordinate of the location for the
/// content referenced by this <see cref="UseElement"/>. Default
/// is y = 0.
/// </summary>
public double? Y { get; set; }
/// <summary>
/// Gets or sets the the ID of the SVG group to be
/// referenced and returns this element.
/// </summary>
/// <value>
/// A string containing the ID of the SVG group as defined by the
/// <i>id</i>-Attribute.
/// </value>
public string GroupId {
get {
return _groupId;
}
set {
_groupId = value;
if (!_groupId.StartsWith("#")) {
_groupId = $"#{_groupId}";
}
}
}
/// <summary>
/// Sets the location (<see cref="X"/>, <see cref="Y"/>) for the
/// content referenced by this <see cref="UseElement"/> and returns
/// this element.
/// </summary>
/// <param name="x">The x-coordinate of the location.</param>
/// <param name="y">The y-coordinate of the location.</param>
/// <returns>This <see cref="UseElement"/>.</returns>
public UseElement WithXY(double x, double y) {
X = x;
Y = y;
return this;
}
/// <summary>
/// Sets the <see cref="GroupId"/>, i.e. the ID of the SVG group to be
/// referenced and returns this element.
/// </summary>
/// <param name="groupId">The ID of a SVG group as defined by the
/// <i>id</i>-Attribute.</param>
/// <returns>This <see cref="UseElement"/>.</returns>
public UseElement WithGroupId(string groupId) {
GroupId = groupId;
return this;
}
/// <inheritdoc />
public override XElement GetXml() {
if (string.IsNullOrEmpty(_groupId)) {
throw new InvalidOperationException("UseElement.BlockName must be specified.");
}
XElement xElement = new XElement("use");
AddAttribute(xElement, "x", Cd(X.GetValueOrDefault()), X.HasValue);
AddAttribute(xElement, "y", Cd(Y.GetValueOrDefault()), Y.HasValue);
AddTransform(xElement);
xElement.Add(new XAttribute("href", _groupId));
xElement.Value = string.Empty;
return xElement;
}
}
}