forked from DebugST/STTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSvgDocument.static.cs
More file actions
135 lines (119 loc) · 4.77 KB
/
SvgDocument.static.cs
File metadata and controls
135 lines (119 loc) · 4.77 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.IO;
using System.Xml;
namespace ST.Library.Drawing.SvgRender
{
partial class SvgDocument
{
private static Type m_type_svg_element = typeof(SvgElement);
private static Regex m_reg_style_width = new Regex(@"\bwidth\s*:\s*(\.\d+|\d+(?:\.\d+))");
private static Regex m_reg_style_height = new Regex(@"\bheight\s*:\s*(\.\d+|\d+(?:\.\d+))");
private static Regex m_reg_number = new Regex(@"(-?(\.\d+|\d(?:\.\d+)?)+)");
private static Dictionary<string, Type> m_dic_target_type = new Dictionary<string, Type>();
static SvgDocument() {
var asm = Assembly.GetExecutingAssembly();
SvgDocument.RegisterType(asm);
}
internal static SvgElement CreateElement(string strTargetName) {
if (!m_dic_target_type.ContainsKey(strTargetName)) {
return null;
}
var obj = Activator.CreateInstance(m_dic_target_type[strTargetName]);
return (SvgElement)obj;
}
public static string[] GetTargetNames() {
return m_dic_target_type.Keys.ToArray();
}
public static bool UnRegisterType(string strTargetName) {
if (!m_dic_target_type.ContainsKey(strTargetName)) {
return false;
}
m_dic_target_type.Remove(strTargetName);
return true;
}
public static bool RegisterType(string strTargetName, Type type) {
return SvgDocument.RegisterType(strTargetName, type, true);
}
public static bool RegisterType(string strTargetName, Type type, bool bOverride) {
if (type.IsAbstract) {
throw new ArgumentException("Can not retister a abstract class!");
}
if (!(type.IsSubclassOf(m_type_svg_element))) {
throw new ArgumentException("The type is not a SvgElement!");
}
if (!m_dic_target_type.ContainsKey(strTargetName)) {
m_dic_target_type.Add(strTargetName, type);
return true;
}
if (!bOverride) {
return false;
}
m_dic_target_type[strTargetName] = type;
return true;
}
public static int RegisterType() {
var asm = Assembly.GetCallingAssembly();
return SvgDocument.RegisterType(asm, true);
}
public static int RegisterType(bool bOverride) {
var asm = Assembly.GetCallingAssembly();
return SvgDocument.RegisterType(asm, bOverride);
}
public static int RegisterType(string strFile) {
return SvgDocument.RegisterType(strFile, true);
}
public static int RegisterType(string strFile, bool bOverride) {
var asm = Assembly.LoadFrom(Path.GetFullPath(strFile));
return SvgDocument.RegisterType(asm, bOverride);
}
public static int RegisterType(Assembly asm) {
return SvgDocument.RegisterType(asm, true);
}
public static int RegisterType(Assembly asm, bool bOverride) {
int nCounter = 0;
var types = asm.GetTypes();
foreach (var t in types) {
if (t.IsAbstract) {
continue;
}
var attrs = t.GetCustomAttributes(false);
foreach (var a in attrs) {
if (!(a is SvgElementAttribute)) {
continue;
}
if (!t.IsSubclassOf(m_type_svg_element)) {
continue;
}
if (SvgDocument.RegisterType(((SvgElementAttribute)a).TargetName, t, bOverride)) {
nCounter++;
}
}
}
return nCounter;
}
//========================================================
public static SvgDocument FromXmlFile(string strFile) {
return SvgDocument.FromXml(File.ReadAllText(strFile));
}
public static SvgDocument FromXml(string strXml) {
XmlDocument xml = new XmlDocument();
xml.LoadXml(strXml);
return SvgDocument.FromXmlDocument(xml);
}
public static SvgDocument FromXmlDocument(XmlDocument xml) {
// TODO: comment element
var doc = xml.DocumentElement;
if (doc == null || doc.Name != "svg") {
throw new ArgumentException("Invalid svg document");
}
SvgDocument svg = new SvgDocument();
svg.InitElement(svg, null, doc);
return svg;
}
}
}