-
Notifications
You must be signed in to change notification settings - Fork 135
/
SvgImageExtension.cs
254 lines (223 loc) · 8.46 KB
/
SvgImageExtension.cs
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
using System;
using System.IO;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Markup;
using SharpVectors.Dom.Utils;
namespace SharpVectors.Converters
{
/// <summary>
/// This implements a markup extension that enables the creation
/// of <see cref="DrawingImage"/> from SVG sources.
/// </summary>
/// <remarks>
/// The SVG source file can be:
/// <list type="bullet">
/// <item>
/// <description>From the web</description>
/// </item>
/// <item>
/// <description>From the local computer (relative or absolute paths)</description>
/// </item>
/// <item>
/// <description>From the resources.</description>
/// </item>
/// </list>
/// <para>
/// The rendering settings are provided as properties for customizations.
/// </para>
/// </remarks>
[MarkupExtensionReturnType(typeof(DrawingImage))]
public sealed class SvgImageExtension : SvgImageBase
{
#region Private Fields
private string _svgPath;
#endregion
#region Constructors and Destructor
/// <overloads>
/// Initializes a new instance of the <see cref="SvgImageExtension"/> class.
/// </overloads>
/// <summary>
/// Initializes a new instance of the <see cref="SvgImageExtension"/>
/// class with the default parameters.
/// </summary>
public SvgImageExtension()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SvgImageExtension"/>
/// class with the specified SVG file path.
/// </summary>
/// <param name="svgPath"></param>
public SvgImageExtension(string svgPath)
: this()
{
_svgPath = svgPath;
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the SVG source file.
/// </summary>
/// <value>
/// A string specifying the path of the SVG source file.
/// The default is <see langword="null"/>.
/// </value>
public string Source
{
get {
return _svgPath;
}
set {
_svgPath = value;
if (string.IsNullOrWhiteSpace(_appName))
{
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()) ||
LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
this.GetAppName();
}
}
}
}
#endregion
#region Public Methods
/// <summary>
/// Performs the conversion of a valid SVG source file to the
/// <see cref="DrawingImage"/> that is set as the value of the target
/// property for this markup extension.
/// </summary>
/// <param name="serviceProvider">
/// Object that can provide services for the markup extension.
/// </param>
/// <returns>
/// This returns <see cref="DrawingImage"/> if successful; otherwise, it
/// returns <see langword="null"/>.
/// </returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
try
{
if (string.IsNullOrWhiteSpace(_appName))
{
if (this.IsDesignMode() || LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
this.GetAppName();
}
}
Uri svgSource = this.ResolveUri(serviceProvider);
if (svgSource == null)
{
return null;
}
return this.GetImage(svgSource);
}
catch
{
//throw; #82
}
return null;
}
#endregion
#region Private Methods
/// <summary>
/// Converts the SVG source file to <see cref="Uri"/>
/// </summary>
/// <param name="serviceProvider">
/// Object that can provide services for the markup extension.
/// </param>
/// <returns>
/// Returns the valid <see cref="Uri"/> of the SVG source path if
/// successful; otherwise, it returns <see langword="null"/>.
/// </returns>
private Uri ResolveUri(IServiceProvider serviceProvider)
{
if (string.IsNullOrWhiteSpace(_svgPath))
{
return null;
}
if (string.IsNullOrWhiteSpace(_appName))
{
this.GetAppName();
}
var comparer = StringComparison.OrdinalIgnoreCase;
Uri svgSource;
if (Uri.TryCreate(_svgPath, UriKind.RelativeOrAbsolute, out svgSource))
{
if (svgSource.IsAbsoluteUri)
{
return svgSource;
}
// Try getting a local file in the same directory....
string svgPath = _svgPath;
if (_svgPath[0] == '\\' || _svgPath[0] == '/')
{
svgPath = _svgPath.Substring(1);
}
svgPath = svgPath.Replace('/', '\\');
var assembly = this.GetExecutingAssembly();
if (assembly != null)
{
string localFile = PathUtils.Combine(assembly, svgPath);
if (File.Exists(localFile))
{
return new Uri(localFile);
}
}
// Try getting it as resource file...
IUriContext uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext;
if (uriContext != null && uriContext.BaseUri != null)
{
var contextUri = new Uri(uriContext.BaseUri, svgSource);
if (contextUri.IsAbsoluteUri)
{
if (contextUri.IsFile && File.Exists(contextUri.LocalPath))
{
return contextUri;
}
}
if (uriContext is DependencyObject)
{
var baseUri = System.Windows.Navigation.BaseUriHelper.GetBaseUri((DependencyObject)uriContext);
if (baseUri != null && baseUri != uriContext.BaseUri)
{
contextUri = new Uri(baseUri, svgSource);
if (contextUri.IsFile && File.Exists(contextUri.LocalPath))
{
return contextUri;
}
}
}
}
string asmName = _appName;
if (string.IsNullOrWhiteSpace(asmName) && assembly != null)
{
// It should not be the SharpVectors.Converters.Wpf.dll, which contains this extension...
string tempName = assembly.GetName().Name;
if (!string.Equals("SharpVectors.Converters.Wpf", tempName, comparer)
&& !string.Equals("WpfSurface", tempName, comparer))
{
asmName = tempName;
}
}
svgPath = _svgPath;
if (_svgPath.StartsWith("/", comparer))
{
svgPath = svgPath.TrimStart('/');
}
// A little hack to display preview in design mode
if (this.IsDesignMode(serviceProvider) && !string.IsNullOrWhiteSpace(_appName))
{
string uriDesign = string.Format("/{0};component/{1}", _appName, svgPath);
return new Uri(uriDesign, UriKind.Relative);
}
string uriString = string.Format("pack://application:,,,/{0};component/{1}",
asmName, svgPath);
return new Uri(uriString);
}
return null;
}
#endregion
}
}