-
Notifications
You must be signed in to change notification settings - Fork 472
/
SvgTextBase.cs
350 lines (313 loc) · 12.2 KB
/
SvgTextBase.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
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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Svg
{
public abstract partial class SvgTextBase : SvgVisualElement
{
private SvgUnitCollection _x = new SvgUnitCollection();
private SvgUnitCollection _y = new SvgUnitCollection();
private SvgUnitCollection _dy = new SvgUnitCollection();
private SvgUnitCollection _dx = new SvgUnitCollection();
private string _rotate;
private List<float> _rotations = new List<float>();
public SvgTextBase()
{
NotifyCollectionChangedEventHandler xHandler = null;
_x.CollectionChanged += xHandler = (s, e) => { Attributes["x"] = s; _x.CollectionChanged -= xHandler; };
NotifyCollectionChangedEventHandler dxHandler = null;
_dx.CollectionChanged += dxHandler = (s, e) => { Attributes["dx"] = s; _dx.CollectionChanged -= dxHandler; };
NotifyCollectionChangedEventHandler yHandler = null;
_y.CollectionChanged += yHandler = (s, e) => { Attributes["y"] = s; _y.CollectionChanged -= yHandler; };
NotifyCollectionChangedEventHandler dyHandler = null;
_dy.CollectionChanged += dyHandler = (s, e) => { Attributes["dy"] = s; _dy.CollectionChanged -= dyHandler; };
_x.CollectionChanged += OnCoordinateChanged;
_dx.CollectionChanged += OnCoordinateChanged;
_y.CollectionChanged += OnCoordinateChanged;
_dy.CollectionChanged += OnCoordinateChanged;
}
/// <summary>
/// Gets or sets the text to be rendered.
/// </summary>
public virtual string Text
{
get { return Content; }
set
{
Nodes.Clear();
Children.Clear();
if (value != null)
{
Nodes.Add(new SvgContentNode { Content = value });
}
Content = value;
IsPathDirty = true;
}
}
public override XmlSpaceHandling SpaceHandling
{
set { base.SpaceHandling = value; IsPathDirty = true; }
}
/// <summary>
/// Gets or sets the X.
/// </summary>
/// <value>The X.</value>
[SvgAttribute("x")]
public virtual SvgUnitCollection X
{
get { return _x; }
set
{
if (_x != value)
{
if (_x != null) _x.CollectionChanged -= OnCoordinateChanged;
_x = value;
if (_x != null) _x.CollectionChanged += OnCoordinateChanged;
IsPathDirty = true;
}
Attributes["x"] = value;
}
}
/// <summary>
/// Gets or sets the dX.
/// </summary>
/// <value>The dX.</value>
[SvgAttribute("dx")]
public virtual SvgUnitCollection Dx
{
get { return _dx; }
set
{
if (_dx != value)
{
if (_dx != null) _dx.CollectionChanged -= OnCoordinateChanged;
_dx = value;
if (_dx != null) _dx.CollectionChanged += OnCoordinateChanged;
IsPathDirty = true;
}
Attributes["dx"] = value;
}
}
/// <summary>
/// Gets or sets the Y.
/// </summary>
/// <value>The Y.</value>
[SvgAttribute("y")]
public virtual SvgUnitCollection Y
{
get { return _y; }
set
{
if (_y != value)
{
if (_y != null) _y.CollectionChanged -= OnCoordinateChanged;
_y = value;
if (_y != null) _y.CollectionChanged += OnCoordinateChanged;
IsPathDirty = true;
}
Attributes["y"] = value;
}
}
/// <summary>
/// Gets or sets the dY.
/// </summary>
/// <value>The dY.</value>
[SvgAttribute("dy")]
public virtual SvgUnitCollection Dy
{
get { return _dy; }
set
{
if (_dy != value)
{
if (_dy != null) _dy.CollectionChanged -= OnCoordinateChanged;
_dy = value;
if (_dy != null) _dy.CollectionChanged += OnCoordinateChanged;
IsPathDirty = true;
}
Attributes["dy"] = value;
}
}
private void OnCoordinateChanged(object sender, NotifyCollectionChangedEventArgs args)
{
IsPathDirty = true;
}
/// <summary>
/// Gets or sets the rotate.
/// </summary>
/// <value>The rotate.</value>
[SvgAttribute("rotate")]
public virtual string Rotate
{
get { return _rotate; }
set
{
if (_rotate != value)
{
_rotate = value;
_rotations.Clear();
_rotations.AddRange(from r in _rotate.Split(new char[] { ',', ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries)
select float.Parse(r, NumberStyles.Any, CultureInfo.InvariantCulture));
IsPathDirty = true;
}
Attributes["rotate"] = value;
}
}
/// <summary>
/// The pre-calculated length of the text
/// </summary>
[SvgAttribute("textLength")]
public virtual SvgUnit TextLength
{
get { return GetAttribute("textLength", true, SvgUnit.None); }
set { Attributes["textLength"] = value; IsPathDirty = true; }
}
/// <summary>
/// Gets or sets the text anchor.
/// </summary>
/// <value>The text anchor.</value>
[SvgAttribute("lengthAdjust")]
public virtual SvgTextLengthAdjust LengthAdjust
{
get { return GetAttribute("lengthAdjust", true, SvgTextLengthAdjust.Spacing); }
set { Attributes["lengthAdjust"] = value; IsPathDirty = true; }
}
/// <summary>
/// Specifies spacing behavior between text characters.
/// </summary>
[SvgAttribute("letter-spacing")]
public virtual SvgUnit LetterSpacing
{
get { return GetAttribute("letter-spacing", true, SvgUnit.None); }
set { Attributes["letter-spacing"] = value; IsPathDirty = true; }
}
/// <summary>
/// Specifies spacing behavior between words.
/// </summary>
[SvgAttribute("word-spacing")]
public virtual SvgUnit WordSpacing
{
get { return GetAttribute("word-spacing", true, SvgUnit.None); }
set { Attributes["word-spacing"] = value; IsPathDirty = true; }
}
/// <summary>
/// Gets or sets the fill.
/// </summary>
/// <remarks>
/// <para>Unlike other <see cref="SvgVisualElement"/>s, <see cref="SvgText"/> has a default fill of black rather than transparent.</para>
/// </remarks>
/// <value>The fill.</value>
public override SvgPaintServer Fill
{
get { return GetAttribute<SvgPaintServer>("fill", true, new SvgColourServer(System.Drawing.Color.Black)); }
set { Attributes["fill"] = value; }
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
public override string ToString()
{
return this.Text;
}
private static readonly Regex MultipleSpaces = new Regex(@" {2,}", RegexOptions.Compiled);
/// <summary>
/// Prepare the text according to the whitespace handling rules and text transformations. <see href="http://www.w3.org/TR/SVG/text.html">SVG Spec</see>.
/// </summary>
/// <param name="value">Text to be prepared</param>
/// <returns>Prepared text</returns>
protected string PrepareText(string value)
{
value = ApplyTransformation(value);
value = new StringBuilder(value).Replace("\r\n", " ").Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' ').ToString();
return this.SpaceHandling == XmlSpaceHandling.Preserve ? value : MultipleSpaces.Replace(value.Trim(), " ");
}
private string ApplyTransformation(string value)
{
switch (this.TextTransformation)
{
case SvgTextTransformation.Capitalize:
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
case SvgTextTransformation.Uppercase:
return value.ToUpper();
case SvgTextTransformation.Lowercase:
return value.ToLower();
}
return value;
}
[SvgAttribute("onchange")]
public event EventHandler<StringArg> Change;
//change
protected void OnChange(string newString, string sessionID)
{
RaiseChange(this, new StringArg { s = newString, SessionID = sessionID });
}
protected void RaiseChange(object sender, StringArg s)
{
var handler = Change;
if (handler != null)
{
handler(sender, s);
}
}
#if Net4
public override void RegisterEvents(ISvgEventCaller caller)
{
//register basic events
base.RegisterEvents(caller);
//add change event for text
caller.RegisterAction<string, string>(this.ID + "/onchange", OnChange);
}
public override void UnregisterEvents(ISvgEventCaller caller)
{
//unregister base events
base.UnregisterEvents(caller);
//unregister change event
caller.UnregisterAction(this.ID + "/onchange");
}
#endif
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as SvgTextBase;
if (newObj.Attributes.ContainsKey("x"))
{
newObj._x = (SvgUnitCollection)newObj.Attributes["x"];
if (newObj._x != null)
newObj._x.CollectionChanged += newObj.OnCoordinateChanged;
}
if (newObj.Attributes.ContainsKey("y"))
{
newObj._y = (SvgUnitCollection)newObj.Attributes["y"];
if (newObj._y != null)
newObj._y.CollectionChanged += newObj.OnCoordinateChanged;
}
if (newObj.Attributes.ContainsKey("dx"))
{
newObj._dx = (SvgUnitCollection)newObj.Attributes["dx"];
if (newObj._dx != null)
newObj._dx.CollectionChanged += newObj.OnCoordinateChanged;
}
if (newObj.Attributes.ContainsKey("dy"))
{
newObj._dy = (SvgUnitCollection)newObj.Attributes["dy"];
if (newObj._dy != null)
newObj._dy.CollectionChanged += newObj.OnCoordinateChanged;
}
newObj._rotate = _rotate;
foreach (var rotation in _rotations)
newObj._rotations.Add(rotation);
return newObj;
}
/// <summary>Empty text elements are not legal - only write this element if it has children.</summary>
public override bool ShouldWriteElement()
{
return (this.HasChildren() || this.Nodes.Count > 0);
}
}
}