1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Collections.Generic;
|
---|
5 |
|
---|
6 | using System.Windows;
|
---|
7 | using System.Windows.Media;
|
---|
8 |
|
---|
9 | namespace SharpVectors.Renderers.Wpf
|
---|
10 | {
|
---|
11 | public sealed class WpfDrawingContext : DependencyObject, IEnumerable<DrawingGroup>
|
---|
12 | {
|
---|
13 | #region Private Fields
|
---|
14 |
|
---|
15 | private bool _renderingClip;
|
---|
16 | private bool _isFragment;
|
---|
17 |
|
---|
18 | private object _tag;
|
---|
19 | private DrawingGroup _rootDrawing;
|
---|
20 | private DrawingGroup _linkDrawing;
|
---|
21 |
|
---|
22 | private WpfDrawingSettings _settings;
|
---|
23 |
|
---|
24 | private WpfLinkVisitor _linkVisitor;
|
---|
25 | private WpfFontFamilyVisitor _fontFamilyVisitor;
|
---|
26 | private WpfEmbeddedImageVisitor _imageVisitor;
|
---|
27 |
|
---|
28 | private Stack<DrawingGroup> _drawStack;
|
---|
29 |
|
---|
30 | private HashSet<string> _registeredIds;
|
---|
31 |
|
---|
32 | #endregion
|
---|
33 |
|
---|
34 | #region Constructors and Destructor
|
---|
35 |
|
---|
36 | public WpfDrawingContext(bool isFragment)
|
---|
37 | {
|
---|
38 | _isFragment = isFragment;
|
---|
39 | _drawStack = new Stack<DrawingGroup>();
|
---|
40 | _registeredIds = new HashSet<string>(
|
---|
41 | StringComparer.OrdinalIgnoreCase);
|
---|
42 |
|
---|
43 | _settings = new WpfDrawingSettings();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public WpfDrawingContext(bool isFragment, WpfDrawingSettings settings)
|
---|
47 | : this(isFragment)
|
---|
48 | {
|
---|
49 | if (settings != null)
|
---|
50 | {
|
---|
51 | _settings = settings;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Public Properties
|
---|
58 |
|
---|
59 | // Summary:
|
---|
60 | // Gets the number of elements contained in the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
61 | //
|
---|
62 | // Returns:
|
---|
63 | // The number of elements contained in the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
64 | public int Count
|
---|
65 | {
|
---|
66 | get
|
---|
67 | {
|
---|
68 | if (_drawStack != null)
|
---|
69 | {
|
---|
70 | return _drawStack.Count;
|
---|
71 | }
|
---|
72 |
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public bool RenderingClipRegion
|
---|
78 | {
|
---|
79 | get
|
---|
80 | {
|
---|
81 | return _renderingClip;
|
---|
82 | }
|
---|
83 | set
|
---|
84 | {
|
---|
85 | _renderingClip = value;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public DrawingGroup Root
|
---|
90 | {
|
---|
91 | get
|
---|
92 | {
|
---|
93 | return _rootDrawing;
|
---|
94 | }
|
---|
95 | internal set
|
---|
96 | {
|
---|
97 | if (value != null)
|
---|
98 | {
|
---|
99 | _rootDrawing = value;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public DrawingGroup Links
|
---|
105 | {
|
---|
106 | get
|
---|
107 | {
|
---|
108 | return _linkDrawing;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public bool IsFragment
|
---|
113 | {
|
---|
114 | get
|
---|
115 | {
|
---|
116 | return _isFragment;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public object Tag
|
---|
121 | {
|
---|
122 | get
|
---|
123 | {
|
---|
124 | return _tag;
|
---|
125 | }
|
---|
126 | set
|
---|
127 | {
|
---|
128 | _tag = value;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public WpfDrawingSettings Settings
|
---|
133 | {
|
---|
134 | get
|
---|
135 | {
|
---|
136 | return _settings;
|
---|
137 | }
|
---|
138 | set
|
---|
139 | {
|
---|
140 | if (value != null)
|
---|
141 | {
|
---|
142 | _settings = value;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public WpfLinkVisitor LinkVisitor
|
---|
148 | {
|
---|
149 | get
|
---|
150 | {
|
---|
151 | return _linkVisitor;
|
---|
152 | }
|
---|
153 | set
|
---|
154 | {
|
---|
155 | _linkVisitor = value;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public WpfEmbeddedImageVisitor ImageVisitor
|
---|
160 | {
|
---|
161 | get
|
---|
162 | {
|
---|
163 | return _imageVisitor;
|
---|
164 | }
|
---|
165 | set
|
---|
166 | {
|
---|
167 | _imageVisitor = value;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | public WpfFontFamilyVisitor FontFamilyVisitor
|
---|
172 | {
|
---|
173 | get
|
---|
174 | {
|
---|
175 | return _fontFamilyVisitor;
|
---|
176 | }
|
---|
177 | set
|
---|
178 | {
|
---|
179 | _fontFamilyVisitor = value;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | #endregion
|
---|
184 |
|
---|
185 | #region Internal Properties
|
---|
186 |
|
---|
187 | internal bool OptimizePath
|
---|
188 | {
|
---|
189 | get
|
---|
190 | {
|
---|
191 | return _settings.OptimizePath;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | internal bool TextAsGeometry
|
---|
196 | {
|
---|
197 | get
|
---|
198 | {
|
---|
199 | return _settings.TextAsGeometry;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | internal bool IncludeRuntime
|
---|
204 | {
|
---|
205 | get
|
---|
206 | {
|
---|
207 | return _settings.IncludeRuntime;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | internal CultureInfo CultureInfo
|
---|
212 | {
|
---|
213 | get
|
---|
214 | {
|
---|
215 | return _settings.CultureInfo;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | internal CultureInfo EnglishCultureInfo
|
---|
220 | {
|
---|
221 | get
|
---|
222 | {
|
---|
223 | return _settings.NeutralCultureInfo;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | internal string DefaultFontName
|
---|
228 | {
|
---|
229 | get
|
---|
230 | {
|
---|
231 | return _settings.DefaultFontName;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | #endregion
|
---|
236 |
|
---|
237 | #region Public Methods
|
---|
238 |
|
---|
239 | // Summary:
|
---|
240 | // Removes all objects from the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
241 | public void Clear()
|
---|
242 | {
|
---|
243 | if (_drawStack != null)
|
---|
244 | {
|
---|
245 | _drawStack.Clear();
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | //
|
---|
250 | // Summary:
|
---|
251 | // Determines whether an element is in the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
252 | //
|
---|
253 | // Parameters:
|
---|
254 | // item:
|
---|
255 | // The object to locate in the System.Collections.Generic.Stack<DrawingGroup>. The value
|
---|
256 | // can be null for reference types.
|
---|
257 | //
|
---|
258 | // Returns:
|
---|
259 | // true if item is found in the System.Collections.Generic.Stack<DrawingGroup>; otherwise,
|
---|
260 | // false.
|
---|
261 | public bool Contains(DrawingGroup item)
|
---|
262 | {
|
---|
263 | if (_drawStack != null)
|
---|
264 | {
|
---|
265 | return _drawStack.Contains(item);
|
---|
266 | }
|
---|
267 |
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 |
|
---|
271 | //
|
---|
272 | // Summary:
|
---|
273 | // Copies the System.Collections.Generic.Stack<DrawingGroup> to an existing one-dimensional
|
---|
274 | // System.Array, starting at the specified array index.
|
---|
275 | //
|
---|
276 | // Parameters:
|
---|
277 | // array:
|
---|
278 | // The one-dimensional System.Array that is the destination of the elements
|
---|
279 | // copied from System.Collections.Generic.Stack<DrawingGroup>. The System.Array must have
|
---|
280 | // zero-based indexing.
|
---|
281 | //
|
---|
282 | // arrayIndex:
|
---|
283 | // The zero-based index in array at which copying begins.
|
---|
284 | //
|
---|
285 | // Exceptions:
|
---|
286 | // System.ArgumentNullException:
|
---|
287 | // array is null.
|
---|
288 | //
|
---|
289 | // System.ArgumentOutOfRangeException:
|
---|
290 | // arrayIndex is less than zero.
|
---|
291 | //
|
---|
292 | // System.ArgumentException:
|
---|
293 | // arrayIndex is equal to or greater than the length of array. -or- The number
|
---|
294 | // of elements in the source System.Collections.Generic.Stack<DrawingGroup> is greater
|
---|
295 | // than the available space from arrayIndex to the end of the destination array.
|
---|
296 | public void CopyTo(DrawingGroup[] array, int arrayIndex)
|
---|
297 | {
|
---|
298 | if (_drawStack != null)
|
---|
299 | {
|
---|
300 | _drawStack.CopyTo(array, arrayIndex);
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | //
|
---|
305 | // Summary:
|
---|
306 | // Returns the object at the top of the System.Collections.Generic.Stack<DrawingGroup>
|
---|
307 | // without removing it.
|
---|
308 | //
|
---|
309 | // Returns:
|
---|
310 | // The object at the top of the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
311 | //
|
---|
312 | // Exceptions:
|
---|
313 | // System.InvalidOperationException:
|
---|
314 | // The System.Collections.Generic.Stack<DrawingGroup> is empty.
|
---|
315 | public DrawingGroup Peek()
|
---|
316 | {
|
---|
317 | if (_drawStack != null)
|
---|
318 | {
|
---|
319 | return _drawStack.Peek();
|
---|
320 | }
|
---|
321 |
|
---|
322 | return null;
|
---|
323 | }
|
---|
324 |
|
---|
325 | //
|
---|
326 | // Summary:
|
---|
327 | // Removes and returns the object at the top of the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
328 | //
|
---|
329 | // Returns:
|
---|
330 | // The object removed from the top of the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
331 | //
|
---|
332 | // Exceptions:
|
---|
333 | // System.InvalidOperationException:
|
---|
334 | // The System.Collections.Generic.Stack<DrawingGroup> is empty.
|
---|
335 | public DrawingGroup Pop()
|
---|
336 | {
|
---|
337 | if (_drawStack != null)
|
---|
338 | {
|
---|
339 | return _drawStack.Pop();
|
---|
340 | }
|
---|
341 |
|
---|
342 | return null;
|
---|
343 | }
|
---|
344 |
|
---|
345 | //
|
---|
346 | // Summary:
|
---|
347 | // Inserts an object at the top of the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
348 | //
|
---|
349 | // Parameters:
|
---|
350 | // item:
|
---|
351 | // The object to push onto the System.Collections.Generic.Stack<DrawingGroup>. The value
|
---|
352 | // can be null for reference types.
|
---|
353 | public void Push(DrawingGroup item)
|
---|
354 | {
|
---|
355 | if (_drawStack != null && item != null)
|
---|
356 | {
|
---|
357 | _drawStack.Push(item);
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | //
|
---|
362 | // Summary:
|
---|
363 | // Copies the System.Collections.Generic.Stack<DrawingGroup> to a new array.
|
---|
364 | //
|
---|
365 | // Returns:
|
---|
366 | // A new array containing copies of the elements of the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
367 | public DrawingGroup[] ToArray()
|
---|
368 | {
|
---|
369 | if (_drawStack != null)
|
---|
370 | {
|
---|
371 | return _drawStack.ToArray();
|
---|
372 | }
|
---|
373 |
|
---|
374 | return new DrawingGroup[0];
|
---|
375 | }
|
---|
376 |
|
---|
377 | //
|
---|
378 | // Summary:
|
---|
379 | // Sets the capacity to the actual number of elements in the System.Collections.Generic.Stack<DrawingGroup>,
|
---|
380 | // if that number is less than 90 percent of current capacity.
|
---|
381 | public void TrimExcess()
|
---|
382 | {
|
---|
383 | if (_drawStack != null)
|
---|
384 | {
|
---|
385 | _drawStack.TrimExcess();
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | public void Initialize(WpfLinkVisitor linkVisitor, WpfFontFamilyVisitor fontFamilyVisitor,
|
---|
390 | WpfEmbeddedImageVisitor imageVisitor)
|
---|
391 | {
|
---|
392 | _linkVisitor = linkVisitor;
|
---|
393 | _fontFamilyVisitor = fontFamilyVisitor;
|
---|
394 | _imageVisitor = imageVisitor;
|
---|
395 |
|
---|
396 | _rootDrawing = new DrawingGroup();
|
---|
397 |
|
---|
398 | this.Push(_rootDrawing);
|
---|
399 |
|
---|
400 | if (_linkVisitor != null && _linkVisitor.Aggregates)
|
---|
401 | {
|
---|
402 | _linkDrawing = new DrawingGroup();
|
---|
403 |
|
---|
404 | string groupId = _linkVisitor.AggregatedLayerName;
|
---|
405 | if (!String.IsNullOrEmpty(groupId))
|
---|
406 | {
|
---|
407 | _linkDrawing.SetValue(FrameworkElement.NameProperty, groupId);
|
---|
408 | }
|
---|
409 |
|
---|
410 | linkVisitor.Initialize(_linkDrawing, this);
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | public void Uninitialize()
|
---|
415 | {
|
---|
416 | }
|
---|
417 |
|
---|
418 | public void BeginDrawing()
|
---|
419 | {
|
---|
420 | }
|
---|
421 |
|
---|
422 | public void EndDrawing()
|
---|
423 | {
|
---|
424 | if (_rootDrawing != null && _linkDrawing != null)
|
---|
425 | {
|
---|
426 | if (_linkDrawing.Children.Count != 0)
|
---|
427 | {
|
---|
428 | _rootDrawing.Children.Add(_linkDrawing);
|
---|
429 | }
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | public bool IsRegisteredId(string elementId)
|
---|
434 | {
|
---|
435 | if (String.IsNullOrEmpty(elementId))
|
---|
436 | {
|
---|
437 | return false;
|
---|
438 | }
|
---|
439 |
|
---|
440 | if (_registeredIds != null && _registeredIds.Count != 0)
|
---|
441 | {
|
---|
442 | return _registeredIds.Contains(elementId);
|
---|
443 | }
|
---|
444 |
|
---|
445 | return false;
|
---|
446 | }
|
---|
447 |
|
---|
448 | public void RegisterId(string elementId)
|
---|
449 | {
|
---|
450 | if (String.IsNullOrEmpty(elementId))
|
---|
451 | {
|
---|
452 | return;
|
---|
453 | }
|
---|
454 |
|
---|
455 | if (_registeredIds != null)
|
---|
456 | {
|
---|
457 | _registeredIds.Add(elementId);
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | public void UnRegisterId(string elementId)
|
---|
462 | {
|
---|
463 | if (String.IsNullOrEmpty(elementId))
|
---|
464 | {
|
---|
465 | return;
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (_registeredIds != null)
|
---|
469 | {
|
---|
470 | _registeredIds.Remove(elementId);
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | #endregion
|
---|
475 |
|
---|
476 | #region IEnumerable Members
|
---|
477 |
|
---|
478 | //
|
---|
479 | // Summary:
|
---|
480 | // Returns an enumerator for the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
481 | //
|
---|
482 | // Returns:
|
---|
483 | // An System.Collections.Generic.Stack<DrawingGroup>.Enumerator for the System.Collections.Generic.Stack<DrawingGroup>.
|
---|
484 | public IEnumerator<DrawingGroup> GetEnumerator()
|
---|
485 | {
|
---|
486 | if (_drawStack != null)
|
---|
487 | {
|
---|
488 | return _drawStack.GetEnumerator();
|
---|
489 | }
|
---|
490 |
|
---|
491 | return null;
|
---|
492 | }
|
---|
493 |
|
---|
494 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
495 | {
|
---|
496 | if (_drawStack != null)
|
---|
497 | {
|
---|
498 | return _drawStack.GetEnumerator();
|
---|
499 | }
|
---|
500 |
|
---|
501 | return null;
|
---|
502 | }
|
---|
503 |
|
---|
504 | #endregion
|
---|
505 | }
|
---|
506 | }
|
---|