-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSvgElementBase.cs
More file actions
388 lines (310 loc) · 12.8 KB
/
SvgElementBase.cs
File metadata and controls
388 lines (310 loc) · 12.8 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#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.Globalization;
using System.Text;
using System.Xml.Linq;
using SvgElements.Transform;
namespace SvgElements {
/// <summary>
/// Base class for all classes that represent SVG elements.
/// </summary>
public abstract class SvgElementBase {
internal TransformAttribute _ta = new TransformAttribute();
// TODO: Parser for reading transform from string value
private string _transform;
/// <summary>
/// Returns an XML-element built from the properties of this SVG-element
/// object.
/// </summary>
/// <returns>
/// The <see cref="XElement"/> built from the properties of this
/// SVG-element.
/// </returns>
public abstract XElement GetXml();
/// <summary>
/// Formats a double value with a decimal dot as needed in numerical attribute
/// of a SVG element.
/// </summary>
/// <param name="val"></param>
/// <param name="places"></param>
/// <returns>A string containing the formatted double value.</returns>
public static string Cd(double val, int places = 6) {
return Math.Round(val, places).ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets or sets an optional value for the <i>id</i> attribute.
/// </summary>
public string ID { get; set; }
/// <summary>
/// Gets or sets an optional value for the <i>class</i> attribute.
/// </summary>
public string Class { get; set; }
/// <summary>
/// Gets or sets a value indicating that a comment associated with this
/// <see cref="SvgElementBase"/> is to be displayed.
/// </summary>
/// <value><b>true</b>, when the comment is to be displayed; otherwise, <b>false</b>.</value>
public static bool CommentsEnabled { get; set; } = false;
/// <summary>
/// Gets or sets an optional string value for a comment to be placed in
/// this element.
/// </summary>
public string Comment { get; set; }
/// <summary>
/// Gets or sets an optional value for the <i>stroke</i> attribute.
/// </summary>
public string Stroke { get; set; }
/// <summary>
/// Gets or sets an optional value for the <i>stroke-width</i> attribute.
/// </summary>
public double? StrokeWidth { get; set; } = 0;
/// <summary>
/// Gets or sets an optional value for the <i>stroke-dasharray</i> attribute.
/// </summary>
public string StrokeDashArray { get; set; } = null;
/// <summary>
/// Gets or sets an optional value for the <i>fill</i> attribute.
/// </summary>
public string Fill { get; set; }
/// <summary>
/// Gets the value of the <i>transform</i> attribute, built from the specified clauses.
/// </summary>
public string Transform {
get {
if (!string.IsNullOrEmpty(_transform))
{
return _transform;
}
else
{
return _ta.ToString();
}
}
set {
_transform = value;
_ta.Clear();
}
}
}
/// <summary>
/// Base class for all classes that represent SVG elements.
/// </summary>
public abstract class SvgElementBase<T> : SvgElementBase {
/// <summary>
/// Sets the value for the <i>id</i> attribute
/// and returns this element.
/// </summary>
/// <param name="id"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithID(string id) {
ID = id;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the value for the <i>class</i> attribute
/// and returns this element.
/// </summary>
/// <param name="className"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithClass(string className) {
Class = className;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the value for the <i>stroke</i> attribute
/// and returns this element.
/// </summary>
/// <param name="stroke"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithStroke(string stroke) {
Stroke = stroke;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the values for the <i>stroke-width</i> attribute
/// and returns this element.
/// </summary>
/// <param name="strokeWidth"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithStrokeWidth(double? strokeWidth) {
StrokeWidth = strokeWidth;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the value for the <i>stroke-dasharray</i> attribute
/// and returns this element.
/// </summary>
/// <param name="strokeDashArray"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithStrokeDashArray(string strokeDashArray) {
StrokeDashArray = strokeDashArray;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the fill property with an fill attribute value containg a
/// color name or Hex RGB-value and returns this element.
/// </summary>
/// <param name="fill"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithFill(string fill) {
Fill = fill;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the fill property with a fill attribute value containg a URL
/// expression with the passed fill URL and returns this element.
/// </summary>
/// <param name="fillUrl"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithFillURL(string fillUrl) {
Fill = $"url({fillUrl})";
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Adds a scale clause with individual scale factors for x and y
/// to the transform attribute and returns this element.
/// </summary>
/// <param name="x">Scale factor for x.</param>
/// <param name="y">Scale factor for y.</param>
/// <returns></returns>
public T AddScale(double x, double y) {
_ta.AddScale(x, y);
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Adds a scale clause with equal scale factors for x and y
/// to the transform attribute and returns this element.
/// </summary>
/// <param name="xy">Scale factor for x and y.</param>
/// <returns></returns>
public T AddScale(double xy) {
_ta.AddScale(xy);
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Adds a rotate clause to the transform attribute and returns this
/// element.
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public T AddRotate(double a) {
_ta.AddRotate(a);
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Adds a rotate clause to the transform attribute and returns this
/// element.
/// </summary>
/// <param name="a"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public T AddRotate(double a, double x, double y) {
_ta.AddRotate(a, x, y);
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Adds a transalate clause to the transform attribute and returns this
/// element.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public T AddTranslate(double x, double y) {
_ta.AddTranslate(x, y);
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets a value indicating that a transform atrribute provides a inversion of
/// the y-coordinate.
/// </summary>
/// <returns></returns>
public T ReverseY(bool value = true) {
_ta.ReverseY = value;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Sets the <see cref="Comment"/> property
/// and return this element.
/// </summary>
/// <param name="comment"></param>
/// <returns>This <see cref="T"/>.</returns>
public T WithComment(string comment) {
Comment = comment;
return (T)Convert.ChangeType(this, typeof(T));
}
/// <summary>
/// Returns an XML-element built from the properties of this SVG-element
/// object as string. If the <see cref="Comment"/> property contains a
/// value and comments are enbled a XML Comment element is placed before
/// the XML-element.
/// </summary>
/// <returns>A string containing <see cref="XElement"/> built from the properties of this
/// SVG-element preceeded by a <see cref="XComment"/> element.
/// </returns>
public override string ToString() {
StringBuilder sb = new StringBuilder();
if (CommentsEnabled && !string.IsNullOrEmpty(Comment)) {
sb.AppendLine(new XComment(Comment).ToString());
}
sb.Append(GetXml());
return replaceSpecialCharacters(sb.ToString());
}
private string replaceSpecialCharacters(string value) {
return value.Replace(">", ">").Replace("<", "<");
}
#region Internal methods to add optional attributes
internal void AddID(XElement xElement) {
if (!string.IsNullOrEmpty(ID)) {
xElement.Add(new XAttribute("id", ID));
}
}
internal void AddClass(XElement xElement) {
if (!string.IsNullOrEmpty(Class)) {
xElement.Add(new XAttribute("class", Class));
}
}
internal void AddTransform(XElement xElement) {
if (!string.IsNullOrEmpty(Transform)) {
xElement.Add(new XAttribute("transform", Transform));
}
}
internal void AddStroke(XElement xElement) {
if (!string.IsNullOrEmpty(Stroke)) {
xElement.Add(new XAttribute("stroke", Stroke));
}
if (StrokeWidth != null && StrokeWidth > 0) {
xElement.Add(new XAttribute("stroke-width", Math.Round(StrokeWidth.GetValueOrDefault(), 4)));
}
}
internal void AddStrokeDashArray(XElement xElement) {
if (!string.IsNullOrEmpty(StrokeDashArray)) {
xElement.Add(new XAttribute("stroke-dasharray", StrokeDashArray));
}
}
internal void AddFill(XElement xElement) {
if (!string.IsNullOrEmpty(Fill)) {
xElement.Add(new XAttribute("fill", Fill));
}
}
/// <summary>
/// Adds an attribute with specified name and value if the <paramref name="specified"/>
/// parameter is true and the specified value is not null.
/// </summary>
/// <param name="xElement">The element the attribute is to added to.</param>
/// <param name="name">The name of the attributre.</param>
/// <param name="value">The value for the attribute.</param>
/// <param name="specified"><b>true</b>, if the attribute is to be added; otherwise; <b>false</b>.</param>
internal static void AddAttribute(XElement xElement, string name, object value, bool specified) {
if (value == null || !specified) {
return;
}
xElement.Add(new XAttribute(name, value.ToString()));
}
#endregion
}
}