1 |
f1f822e9
|
taeseongkim
|
using SharpVectors.Converters;
|
2 |
|
|
using SharpVectors.Dom.Utils;
|
3 |
a74e3cbc
|
ljiyeon
|
using System;
|
4 |
|
|
using System.Collections.Generic;
|
5 |
f1f822e9
|
taeseongkim
|
using System.ComponentModel;
|
6 |
a74e3cbc
|
ljiyeon
|
using System.Globalization;
|
7 |
f1f822e9
|
taeseongkim
|
using System.IO;
|
8 |
a74e3cbc
|
ljiyeon
|
using System.Linq;
|
9 |
|
|
using System.Text;
|
10 |
|
|
using System.Threading.Tasks;
|
11 |
|
|
using System.Windows.Controls;
|
12 |
|
|
using System.Windows.Data;
|
13 |
f1f822e9
|
taeseongkim
|
using System.Windows.Markup;
|
14 |
a74e3cbc
|
ljiyeon
|
using System.Windows.Media;
|
15 |
|
|
using System.Windows.Media.Imaging;
|
16 |
|
|
|
17 |
|
|
namespace KCOM.Common.Converter
|
18 |
|
|
{
|
19 |
f1f822e9
|
taeseongkim
|
public class SvgImageConverter : SvgImageBase, IValueConverter
|
20 |
a74e3cbc
|
ljiyeon
|
{
|
21 |
f1f822e9
|
taeseongkim
|
#region Private Fields
|
22 |
|
|
|
23 |
|
|
private Uri _baseUri;
|
24 |
|
|
private readonly UriTypeConverter _uriConverter;
|
25 |
|
|
|
26 |
|
|
#endregion
|
27 |
|
|
|
28 |
|
|
public SvgImageConverter()
|
29 |
|
|
{
|
30 |
|
|
_uriConverter = new UriTypeConverter();
|
31 |
|
|
}
|
32 |
f65e6c02
|
taeseongkim
|
|
33 |
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
34 |
a74e3cbc
|
ljiyeon
|
{
|
35 |
f1f822e9
|
taeseongkim
|
try
|
36 |
f65e6c02
|
taeseongkim
|
{
|
37 |
f1f822e9
|
taeseongkim
|
if (string.IsNullOrWhiteSpace(_appName))
|
38 |
|
|
{
|
39 |
|
|
if (this.IsDesignMode() || LicenseManager.UsageMode == LicenseUsageMode.Designtime)
|
40 |
|
|
{
|
41 |
|
|
this.GetAppName();
|
42 |
|
|
}
|
43 |
|
|
}
|
44 |
f65e6c02
|
taeseongkim
|
|
45 |
f1f822e9
|
taeseongkim
|
Uri inputUri = null;
|
46 |
|
|
if (parameter != null)
|
47 |
|
|
{
|
48 |
|
|
inputUri = this.ResolveUri(parameter.ToString());
|
49 |
|
|
}
|
50 |
|
|
else if (value != null)
|
51 |
|
|
{
|
52 |
|
|
inputUri = _uriConverter.ConvertFrom(value) as Uri;
|
53 |
|
|
if (inputUri == null)
|
54 |
|
|
{
|
55 |
|
|
inputUri = this.ResolveUri(value.ToString());
|
56 |
|
|
}
|
57 |
|
|
else if (!inputUri.IsAbsoluteUri)
|
58 |
|
|
{
|
59 |
|
|
inputUri = this.ResolveUri(value.ToString());
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
a74e3cbc
|
ljiyeon
|
|
63 |
f1f822e9
|
taeseongkim
|
if (inputUri == null)
|
64 |
a74e3cbc
|
ljiyeon
|
{
|
65 |
f1f822e9
|
taeseongkim
|
return null;
|
66 |
|
|
}
|
67 |
|
|
var svgSource = inputUri.IsAbsoluteUri ? inputUri : new Uri(_baseUri, inputUri);
|
68 |
|
|
|
69 |
|
|
if (svgSource.ToString().EndsWith(".svg"))
|
70 |
|
|
{
|
71 |
|
|
return this.GetImage(svgSource);
|
72 |
|
|
}
|
73 |
|
|
else
|
74 |
|
|
{
|
75 |
|
|
return new BitmapImage(new Uri(value.ToString()));
|
76 |
a74e3cbc
|
ljiyeon
|
}
|
77 |
|
|
|
78 |
|
|
}
|
79 |
f1f822e9
|
taeseongkim
|
catch
|
80 |
a74e3cbc
|
ljiyeon
|
{
|
81 |
f1f822e9
|
taeseongkim
|
//throw; #82
|
82 |
a74e3cbc
|
ljiyeon
|
}
|
83 |
f1f822e9
|
taeseongkim
|
return null;
|
84 |
a74e3cbc
|
ljiyeon
|
}
|
85 |
f65e6c02
|
taeseongkim
|
|
86 |
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
87 |
a74e3cbc
|
ljiyeon
|
{
|
88 |
|
|
throw new NotImplementedException();
|
89 |
|
|
}
|
90 |
f1f822e9
|
taeseongkim
|
|
91 |
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
92 |
|
|
{
|
93 |
|
|
var uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext;
|
94 |
|
|
if (uriContext != null)
|
95 |
|
|
{
|
96 |
|
|
_baseUri = uriContext.BaseUri;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
return this;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
#region Private Methods
|
104 |
|
|
|
105 |
|
|
/// <summary>
|
106 |
|
|
/// Converts the SVG source file to <see cref="Uri"/>
|
107 |
|
|
/// </summary>
|
108 |
|
|
/// <param name="inputParameter">
|
109 |
|
|
/// Object that can provide services for the markup extension.
|
110 |
|
|
/// </param>
|
111 |
|
|
/// <returns>
|
112 |
|
|
/// Returns the valid <see cref="Uri"/> of the SVG source path if
|
113 |
|
|
/// successful; otherwise, it returns <see langword="null"/>.
|
114 |
|
|
/// </returns>
|
115 |
|
|
private Uri ResolveUri(string inputParameter)
|
116 |
|
|
{
|
117 |
|
|
if (string.IsNullOrWhiteSpace(inputParameter))
|
118 |
|
|
{
|
119 |
|
|
return null;
|
120 |
|
|
}
|
121 |
|
|
if (string.IsNullOrWhiteSpace(_appName))
|
122 |
|
|
{
|
123 |
|
|
this.GetAppName();
|
124 |
|
|
}
|
125 |
|
|
var comparer = StringComparison.OrdinalIgnoreCase;
|
126 |
|
|
|
127 |
|
|
Uri svgSource;
|
128 |
|
|
if (Uri.TryCreate(inputParameter, UriKind.RelativeOrAbsolute, out svgSource))
|
129 |
|
|
{
|
130 |
|
|
if (svgSource.IsAbsoluteUri)
|
131 |
|
|
{
|
132 |
|
|
return svgSource;
|
133 |
|
|
}
|
134 |
|
|
// Try getting a local file in the same directory....
|
135 |
|
|
string svgPath = inputParameter;
|
136 |
|
|
if (inputParameter[0] == '\\' || inputParameter[0] == '/')
|
137 |
|
|
{
|
138 |
|
|
svgPath = inputParameter.Substring(1);
|
139 |
|
|
}
|
140 |
|
|
svgPath = svgPath.Replace('/', '\\');
|
141 |
|
|
|
142 |
|
|
var assembly = this.GetExecutingAssembly();
|
143 |
|
|
if (assembly != null)
|
144 |
|
|
{
|
145 |
|
|
string localFile = PathUtils.Combine(assembly, svgPath);
|
146 |
|
|
|
147 |
|
|
if (File.Exists(localFile))
|
148 |
|
|
{
|
149 |
|
|
return new Uri(localFile);
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
// Try getting it as resource file...
|
154 |
|
|
var inputUri = _uriConverter.ConvertFrom(inputParameter) as Uri;
|
155 |
|
|
if (inputUri != null)
|
156 |
|
|
{
|
157 |
|
|
if (inputUri.IsAbsoluteUri)
|
158 |
|
|
{
|
159 |
|
|
return inputUri;
|
160 |
|
|
}
|
161 |
|
|
if (_baseUri != null)
|
162 |
|
|
{
|
163 |
|
|
var validUri = new Uri(_baseUri, inputUri);
|
164 |
|
|
if (validUri.IsAbsoluteUri)
|
165 |
|
|
{
|
166 |
|
|
if (validUri.IsFile && File.Exists(validUri.LocalPath))
|
167 |
|
|
{
|
168 |
|
|
return validUri;
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
string asmName = _appName;
|
175 |
|
|
if (string.IsNullOrWhiteSpace(asmName) && assembly != null)
|
176 |
|
|
{
|
177 |
|
|
// It should not be the SharpVectors.Converters.Wpf.dll, which contains this extension...
|
178 |
|
|
string tempName = assembly.GetName().Name;
|
179 |
|
|
if (!string.Equals("SharpVectors.Converters.Wpf", tempName, comparer)
|
180 |
|
|
&& !string.Equals("WpfSurface", tempName, comparer))
|
181 |
|
|
{
|
182 |
|
|
asmName = tempName;
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
svgPath = inputParameter;
|
187 |
|
|
if (inputParameter.StartsWith("/", comparer))
|
188 |
|
|
{
|
189 |
|
|
svgPath = svgPath.TrimStart('/');
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
// A little hack to display preview in design mode
|
193 |
|
|
if (this.IsDesignMode() && !string.IsNullOrWhiteSpace(_appName))
|
194 |
|
|
{
|
195 |
|
|
//string uriDesign = string.Format("/{0};component/{1}", _appName, svgPath);
|
196 |
|
|
//return new Uri(uriDesign, UriKind.Relative);
|
197 |
|
|
|
198 |
|
|
// The relative path is not working with the Converter...
|
199 |
|
|
string uriDesign = string.Format("pack://application:,,,/{0};component/{1}",
|
200 |
|
|
_appName, svgPath);
|
201 |
|
|
|
202 |
|
|
return new Uri(uriDesign);
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
string uriString = string.Format("pack://application:,,,/{0};component/{1}",
|
206 |
|
|
asmName, svgPath);
|
207 |
|
|
|
208 |
|
|
return new Uri(uriString);
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
return null;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
#endregion
|
215 |
a74e3cbc
|
ljiyeon
|
}
|
216 |
|
|
} |