-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSvgElement.cs
More file actions
364 lines (341 loc) · 13.6 KB
/
SvgElement.cs
File metadata and controls
364 lines (341 loc) · 13.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace ST.Library.Drawing.SvgRender
{
public abstract class SvgElement : IDisposable
{
private static Regex m_reg_url_id = new Regex(@"url\((?:'|"")?#(.*?)(?:'|"")?\)");
private static Regex m_reg_style = new Regex(@"([a-zA-Z][a-zA-z0-9\-]*)\s?:\s*(.*?)(?:;|$)");
public abstract string TargetName { get; }
/// <summary>
/// Specifies whether children of the current element are allowed to draw.
/// Such as: [defs]
/// </summary>
public abstract bool AllowElementDraw { get; }
public SvgDocument Document { get; private set; }
public string OuterXml { get; private set; }
public string InnerXml { get; private set; }
public string InnerText { get; private set; }
public SvgElement DomParent { get; private set; }
public SvgElement CurrentParent {
get {
if (TempParent != null) {
return this.TempParent;
}
return this.DomParent;
}
}
private SvgElement TempParent { get; set; }
public SvgAttributes Attributes { get; private set; }
public SvgElementCollection Elements { get; private set; }
public SvgElement() {
this.Attributes = new SvgAttributes();
this.Elements = new SvgElementCollection(1);
}
public SvgElement GetElementByID(string strID) {
foreach (var v in this.Elements) {
if (v.Attributes["id"] == strID) {
return v;
}
}
foreach (var v in this.Elements) {
var ret = v.GetElementByID(strID);
if (ret != null) {
return ret;
}
}
return null;
}
public List<SvgElement> GetElementsByClass(string strClassName) {
List<SvgElement> lst = new List<SvgElement>();
foreach (var v in this.Elements) {
if (v.Attributes["class"] == strClassName) {
lst.Add(v);
}
foreach (var e in v.GetElementsByClass(strClassName)) {
lst.Add(e);
}
}
return lst;
}
public List<SvgElement> GetElementsByTarget(string strTargetName) {
List<SvgElement> lst = new List<SvgElement>();
foreach (var v in this.Elements) {
if (v.TargetName == strTargetName) {
lst.Add(v);
}
foreach (var e in v.GetElementsByTarget(strTargetName)) {
lst.Add(e);
}
}
return lst;
}
internal void InitElement(SvgDocument doc, SvgElement parent, XmlNode node) {
this.Document = doc;
this.DomParent = parent;
this.OuterXml = node.OuterXml;
this.InnerXml = node.InnerXml;
this.InnerText = node.InnerText;
string strStyle = null;
foreach (XmlAttribute attr in node.Attributes) {
if (attr.Value == "inherit") {
continue;
}
switch (attr.Name) { // TODO: ..
//case "id":
//case "class":
case "style":
strStyle = attr.Value;
continue;
}
this.Attributes.Set(attr.Name, attr.Value);
this.OnInitAttribute(attr.Name, attr.Value);
}
if (!string.IsNullOrEmpty(strStyle)) {
foreach (Match m in m_reg_style.Matches(strStyle)) {
this.Attributes.Set(m.Groups[1].Value, m.Groups[2].Value);
this.OnInitAttribute(m.Groups[1].Value, m.Groups[2].Value);
}
}
foreach (XmlNode n in node.ChildNodes) {
if (n.LocalName[0] == '#') {
continue;
}
var ele = SvgDocument.CreateElement(n.LocalName);
if (ele == null) {
continue;
}
ele.InitElement(doc, this, n);
this.Elements.Add(ele);
}
this.OnInitElementCompleted();
}
protected void DrawElement(Graphics g, SvgElement ele, SvgElement tempParent) {
ele.TempParent = tempParent;
ele.DrawElement(g);
ele.TempParent = null;
}
internal protected virtual void DrawElement(Graphics g) {
GraphicsPath gp = this.GetDrawPath();// this.GetPath();
if (gp != null) {
using (gp) {
//gp.FillMode = SvgAttributes.GetString(this, "fill-rule") == "evenodd" ? FillMode.Alternate : FillMode.Winding;
string strOrder = SvgAttributes.GetString(this, "paint-order");// this.Attributes["paint-order"];
if (string.IsNullOrEmpty(strOrder)) {
strOrder = string.Empty;
}
using (var matrix = SvgAttributes.GetTransform(this, "transform", true)) {
gp.Transform(matrix);
bool bStrokeDrawed = false, bFillDrawed = false, bMarkersDrawed = false;
foreach (var v in strOrder.Split(' ')) {
switch (v) {
case "stroke":
this.OnDrawStroke(g, gp);
bStrokeDrawed = true;
break;
case "fill":
this.OnDrawFill(g, gp);
bFillDrawed = true;
break;
case "markers":
this.OnDrawMarkers(g, gp);
bMarkersDrawed = true;
break;
}
}
if (!bFillDrawed) {
this.OnDrawFill(g, gp);
}
if (!bStrokeDrawed) {
this.OnDrawStroke(g, gp);
}
if (!bMarkersDrawed) {
this.OnDrawMarkers(g, gp);
}
}
}
}
if (!this.AllowElementDraw) {
return;
}
foreach (var v in this.Elements) {
v.DrawElement(g);
}
}
protected virtual Dictionary<string, string> InitDefaultAttrs() {
return null;
}
protected virtual void OnInitElementCompleted() { }
internal protected abstract void OnInitAttribute(string strName, string strValue);
internal protected GraphicsPath GetDrawPath(SvgElement ele) {
this.TempParent = ele;
var gp = this.GetDrawPath();
this.TempParent = null;
return gp;
}
internal protected GraphicsPath GetDrawPath() {
GraphicsPath gp = this.GetPath();
if (gp == null) {
return null;
}
gp.FillMode = SvgAttributes.GetString(this, "fill-rule") == "evenodd" ? FillMode.Alternate : FillMode.Winding;
return gp;
}
public abstract GraphicsPath GetPath();
internal protected abstract void Dispose();
protected virtual void OnDrawStroke(Graphics g, GraphicsPath gp) {
Pen p = this.GetPen();
if (p == null) return;
using (p) {
g.DrawPath(p, gp);
}
}
protected virtual void OnDrawFill(Graphics g, GraphicsPath gp) {
Brush brush = this.GetBrush();
if (brush == null) return;
using (brush) {
g.FillPath(brush, gp);
}
}
protected virtual void OnDrawMarkers(Graphics g, GraphicsPath gp) { }
public Pen GetPen() {
var p = this.GetPen(this, this.GetStrokeAlpha());
if (p == null) {
return null;
}
this.SetPen(p);
return p;
}
public virtual Pen GetPen(SvgElement ele, float fAlpha) {
string strTemp = SvgAttributes.GetString(this, "stroke");
if (string.IsNullOrEmpty(strTemp)) {
return null;
}
Pen p = null;
Match m = m_reg_url_id.Match(strTemp);
if (m.Success) {
SvgElement svg_obj = this.Document.GetElementByID(m.Groups[1].Value);
if (svg_obj == null) {
return null;
}
p = svg_obj.GetPen(ele, fAlpha);
} else {
Color clr = SvgAttributes.GetColor(this, "stroke");
p = new Pen(Color.FromArgb((int)(clr.A * fAlpha), clr));
}
p.Width = SvgAttributes.GetSize(this, "stroke-width");
return p;
}
protected float GetStrokeAlpha() {
float fOpacity = SvgAttributes.GetOpacity(this, "opacity", true);
float fStrokeOpacity = SvgAttributes.GetFloat(this, "stroke-opacity");
fOpacity *= fStrokeOpacity;
if (fOpacity < 0) {
fOpacity = 0;
} else if (fOpacity > 1) {
fOpacity = 1;
}
return fOpacity;
}
protected float GetFillAlpha() {
float fOpacity = SvgAttributes.GetOpacity(this, "opacity", true);
float fStrokeOpacity = SvgAttributes.GetFloat(this, "fill-opacity");
return fOpacity * fStrokeOpacity;
}
protected virtual void SetPen(Pen p) {
switch (SvgAttributes.GetEnum(this, "stroke-linecap", true, new string[] { "round", "square", "butt" }, "butt")) {
case "round":
p.StartCap = LineCap.Round;
p.EndCap = LineCap.Round;
break;
case "square":
p.StartCap = LineCap.Square;
p.EndCap = LineCap.Square;
break;
case "butt":
default:
p.StartCap = LineCap.NoAnchor;
p.EndCap = LineCap.NoAnchor;
break;
}
switch (SvgAttributes.GetEnum(this, "stroke-linejoin", true, new string[] { "miter-clip", "bevel", "round", "miter" }, "miter")) {
case "miter-clip":
p.LineJoin = LineJoin.MiterClipped;
break;
case "bevel":
p.LineJoin = LineJoin.Bevel;
break;
case "round":
p.LineJoin = LineJoin.Round;
p.DashCap = DashCap.Round;
break;
case "miter":
default:
p.LineJoin = LineJoin.Miter;
break;
}
p.MiterLimit = SvgAttributes.GetFloat(this, "stroke-miterlimit");// this.GetFloat("stroke-miterlimit");
p.DashOffset = SvgAttributes.GetFloat(this, "stroke-dashoffset") / p.Width;// this.GetFloat("stroke-dashoffset");
float[] arr = SvgAttributes.GetSizeArray(this, "stroke-dasharray", true);
if (arr == null) return;
int nErrorCount = 0;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] <= 0) {
nErrorCount++;
}
arr[i] = arr[i] / p.Width;
}
if (nErrorCount != 0) {
var temp = new float[arr.Length - nErrorCount];
for (int i = 0, j = 0; i < arr.Length; i++) {
if (arr[i] <= 0) continue;
temp[j++] = arr[i];
}
arr = temp;
}
if (arr != null) {
if (arr.Length % 2 != 0) {
float[] temp_arr = new float[arr.Length * 2];
for (int i = 0; i < temp_arr.Length; i++) {
temp_arr[i] = arr[i % arr.Length];
}
p.DashPattern = temp_arr;
} else {
p.DashPattern = arr;
}
}
}
public Brush GetBrush() {
return this.GetBrush(this, this.GetFillAlpha());
}
public virtual Brush GetBrush(SvgElement ele, float fAlpha) {
string strTemp = SvgAttributes.GetString(this, "fill");
if (string.IsNullOrEmpty(strTemp)) {
return null;
}
Brush brush = null;
Match m = m_reg_url_id.Match(strTemp);
if (m.Success) {
SvgElement svg_obj = this.Document.GetElementByID(m.Groups[1].Value);
if (svg_obj == null) {
return null;
}
brush = svg_obj.GetBrush(ele, fAlpha);
} else {
Color clr = SvgAttributes.GetColor(this, "fill");
clr = Color.FromArgb((int)(clr.A * fAlpha), clr);
brush = new SolidBrush(clr);
}
return brush;
}
void IDisposable.Dispose() {
this.Dispose();
}
}
}