forked from cursedtoxi/WebControls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSvgParser.cs
More file actions
190 lines (171 loc) · 4.6 KB
/
SvgParser.cs
File metadata and controls
190 lines (171 loc) · 4.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
namespace Aurigma.Svg
{
internal class SvgParser
{
public virtual void Parse(SvgDocument svgDocument, XmlReader xmlReader)
{
var xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlReader);
}
catch (XmlException ex)
{
throw new SvgParseException(Messages.CanNotLoadSvg, ex);
}
var xmlRoot = xmlDoc.DocumentElement;
if (xmlRoot.LocalName != svgDocument.LocalName)
{
throw new SvgParseException(Messages.IncorrectRootElement);
}
var attributes = xmlRoot.Attributes;
svgDocument.ID = GetStringAttribute(attributes, "id");
svgDocument.Height = GetFloatAttribute(attributes, "height");
svgDocument.Width = GetFloatAttribute(attributes, "width");
ParseChildNodes(svgDocument, xmlRoot.ChildNodes);
}
private void ParseChildNodes(SvgNode svgNode, XmlNodeList xmlNodeList)
{
foreach (XmlElement xmlNode in xmlNodeList)
{
SvgNode childNode = null;
switch (xmlNode.LocalName)
{
case "defs":
childNode = ParseDefsElement(xmlNode);
break;
case "g":
childNode = ParseGroupElement(xmlNode);
break;
case "image":
childNode = ParseImageElement(xmlNode);
break;
case "line":
childNode = ParseLineElement(xmlNode);
break;
default:
childNode = ParseUnknownElement(xmlNode);
break;
}
if (childNode != null)
{
svgNode.ChildNodes.Add(childNode);
}
}
}
public virtual SvgNode ParseUnknownElement(XmlElement xmlNode)
{
throw new NotImplementedException();
}
public virtual SvgLine ParseLineElement(XmlElement xmlNode)
{
throw new NotImplementedException();
}
public virtual SvgImage ParseImageElement(XmlElement xmlNode)
{
throw new NotImplementedException();
}
public virtual SvgGroup ParseGroupElement(XmlElement xmlNode)
{
throw new NotImplementedException();
}
public virtual SvgDefs ParseDefsElement(XmlElement xmlNode)
{
SvgDefs svgDefs = new SvgDefs();
FillElementProperties(svgDefs, xmlNode);
return svgDefs;
}
private void FillElementProperties(SvgElement svgElement, XmlElement xmlNode)
{
var attributes = xmlNode.Attributes;
svgElement.ID = GetStringAttribute(attributes, "id");
svgElement.Display = GetDisplayAttribute(attributes);
svgElement.Transform = GetTransformAttribute(attributes);
//foreach (XmlAttribute attr in attributes)
//{
// if (!string.IsNullOrEmpty(attr.Prefix) && svgDocument.ContainsCustomPrefix(attr.Prefix))
// {
// svgDocument.C
// }
//}
}
public virtual Matrix GetTransformAttribute(XmlAttributeCollection attributes,
string name = "transform", Matrix defaultValue = null)
{
var s = GetStringAttribute(attributes, name);
if (string.IsNullOrEmpty(s))
{
return defaultValue;
}
var rg = new Regex(string.Format(@"matrix\s*\({0},{0},{0},{0},{0},{0}\)", @"\s*([0-9\-+.eE]+)\s*"),
RegexOptions.CultureInvariant | RegexOptions.Compiled);
var match = rg.Match(s);
if (match == null || !match.Success)
{
return defaultValue;
}
float[] m = new float[6];
int i = 0;
for (i = 1; i < 7; i++)
{
if (!float.TryParse(match.Groups[i].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out m[i - 1]))
{
break;
}
}
if (i < 7)
{
return defaultValue;
}
return new Matrix(m[0], m[1], m[2], m[3], m[4], m[5]);
}
private Display GetDisplayAttribute(XmlAttributeCollection attributes,
string name = "display", Display defaultValue = Display.Inline)
{
var s = GetStringAttribute(attributes, name);
if (string.IsNullOrEmpty(s))
{
return defaultValue;
}
try
{
var value = (Display)Enum.Parse(typeof(Display), s, true);
if (Enum.IsDefined(typeof(Display), value))
{
return value;
}
}
catch
{
}
return defaultValue;
}
private float GetFloatAttribute(XmlAttributeCollection attributeCollection, string name, float defaultValue = 0)
{
float val;
var s = GetStringAttribute(attributeCollection, name);
if (!string.IsNullOrEmpty(s) && float.TryParse(s, NumberStyles.Float,
CultureInfo.InvariantCulture, out val))
{
return val;
}
return defaultValue;
}
private string GetStringAttribute(XmlAttributeCollection attributeCollection, string name, string defaultValue = "")
{
var attr = attributeCollection[name];
if (attr != null && attr.Value != null)
{
return attr.Value;
}
return defaultValue;
}
}
}