1 | // TextOnPathBase.cs by Charles Petzold, September 2008
|
---|
2 | using System;
|
---|
3 |
|
---|
4 | using System.Windows;
|
---|
5 | using System.Windows.Controls;
|
---|
6 | using System.Windows.Documents;
|
---|
7 | using System.Windows.Media;
|
---|
8 |
|
---|
9 | namespace SharpVectors.Renderers.Texts
|
---|
10 | {
|
---|
11 | public abstract class WpfTextOnPathBase : DependencyObject
|
---|
12 | {
|
---|
13 | // Dependency properties
|
---|
14 | public static readonly DependencyProperty FontFamilyProperty =
|
---|
15 | TextElement.FontFamilyProperty.AddOwner(typeof(WpfTextOnPathBase),
|
---|
16 | new FrameworkPropertyMetadata(OnFontPropertyChanged));
|
---|
17 |
|
---|
18 | public static readonly DependencyProperty FontStyleProperty =
|
---|
19 | TextElement.FontStyleProperty.AddOwner(typeof(WpfTextOnPathBase),
|
---|
20 | new FrameworkPropertyMetadata(OnFontPropertyChanged));
|
---|
21 |
|
---|
22 | public static readonly DependencyProperty FontWeightProperty =
|
---|
23 | TextElement.FontWeightProperty.AddOwner(typeof(WpfTextOnPathBase),
|
---|
24 | new FrameworkPropertyMetadata(OnFontPropertyChanged));
|
---|
25 |
|
---|
26 | public static readonly DependencyProperty FontStretchProperty =
|
---|
27 | TextElement.FontStretchProperty.AddOwner(typeof(WpfTextOnPathBase),
|
---|
28 | new FrameworkPropertyMetadata(OnFontPropertyChanged));
|
---|
29 |
|
---|
30 | public static readonly DependencyProperty ForegroundProperty =
|
---|
31 | TextElement.ForegroundProperty.AddOwner(typeof(WpfTextOnPathBase),
|
---|
32 | new FrameworkPropertyMetadata(OnForegroundPropertyChanged));
|
---|
33 |
|
---|
34 | //public static readonly DependencyProperty TextProperty =
|
---|
35 | // TextBlock.TextProperty.AddOwner(typeof(TextOnPathBase),
|
---|
36 | // new FrameworkPropertyMetadata(OnTextPropertyChanged));
|
---|
37 |
|
---|
38 | //public static readonly DependencyProperty PathFigureProperty =
|
---|
39 | // DependencyProperty.Register("PathFigure",
|
---|
40 | // typeof(PathFigure),
|
---|
41 | // typeof(TextOnPathBase),
|
---|
42 | // new FrameworkPropertyMetadata(OnPathPropertyChanged));
|
---|
43 |
|
---|
44 | // Properties
|
---|
45 | public FontFamily FontFamily
|
---|
46 | {
|
---|
47 | set { SetValue(FontFamilyProperty, value); }
|
---|
48 | get { return (FontFamily)GetValue(FontFamilyProperty); }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public FontStyle FontStyle
|
---|
52 | {
|
---|
53 | set { SetValue(FontStyleProperty, value); }
|
---|
54 | get { return (FontStyle)GetValue(FontStyleProperty); }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public FontWeight FontWeight
|
---|
58 | {
|
---|
59 | set { SetValue(FontWeightProperty, value); }
|
---|
60 | get { return (FontWeight)GetValue(FontWeightProperty); }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public FontStretch FontStretch
|
---|
64 | {
|
---|
65 | set { SetValue(FontStretchProperty, value); }
|
---|
66 | get { return (FontStretch)GetValue(FontStretchProperty); }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public Brush Foreground
|
---|
70 | {
|
---|
71 | set { SetValue(ForegroundProperty, value); }
|
---|
72 | get { return (Brush)GetValue(ForegroundProperty); }
|
---|
73 | }
|
---|
74 |
|
---|
75 | //public string Text
|
---|
76 | //{
|
---|
77 | // set { SetValue(TextProperty, value); }
|
---|
78 | // get { return (string)GetValue(TextProperty); }
|
---|
79 | //}
|
---|
80 |
|
---|
81 | //public PathFigure PathFigure
|
---|
82 | //{
|
---|
83 | // set { SetValue(PathFigureProperty, value); }
|
---|
84 | // get { return (PathFigure)GetValue(PathFigureProperty); }
|
---|
85 | //}
|
---|
86 |
|
---|
87 | // Property changed handlers
|
---|
88 | static void OnFontPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
|
---|
89 | {
|
---|
90 | (obj as WpfTextOnPathBase).OnFontPropertyChanged(args);
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected abstract void OnFontPropertyChanged(DependencyPropertyChangedEventArgs args);
|
---|
94 |
|
---|
95 | static void OnForegroundPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
|
---|
96 | {
|
---|
97 | (obj as WpfTextOnPathBase).OnForegroundPropertyChanged(args);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected abstract void OnForegroundPropertyChanged(DependencyPropertyChangedEventArgs args);
|
---|
101 |
|
---|
102 | //static void OnTextPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
|
---|
103 | //{
|
---|
104 | // (obj as TextOnPathBase).OnTextPropertyChanged(args);
|
---|
105 | //}
|
---|
106 |
|
---|
107 | //protected abstract void OnTextPropertyChanged(DependencyPropertyChangedEventArgs args);
|
---|
108 |
|
---|
109 | //static void OnPathPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
|
---|
110 | //{
|
---|
111 | // (obj as TextOnPathBase).OnPathPropertyChanged(args);
|
---|
112 | //}
|
---|
113 |
|
---|
114 | //protected abstract void OnPathPropertyChanged(DependencyPropertyChangedEventArgs args);
|
---|
115 |
|
---|
116 | // Utility method
|
---|
117 | public static double GetPathFigureLength(PathFigure pathFigure)
|
---|
118 | {
|
---|
119 | if (pathFigure == null)
|
---|
120 | return 0;
|
---|
121 |
|
---|
122 | bool isAlreadyFlattened = true;
|
---|
123 |
|
---|
124 | foreach (PathSegment pathSegment in pathFigure.Segments)
|
---|
125 | {
|
---|
126 | if (!(pathSegment is PolyLineSegment) && !(pathSegment is LineSegment))
|
---|
127 | {
|
---|
128 | isAlreadyFlattened = false;
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | PathFigure pathFigureFlattened = isAlreadyFlattened ?
|
---|
134 | pathFigure : pathFigure.GetFlattenedPathFigure();
|
---|
135 |
|
---|
136 | double length = 0;
|
---|
137 | Point pt1 = pathFigureFlattened.StartPoint;
|
---|
138 |
|
---|
139 | foreach (PathSegment pathSegment in pathFigureFlattened.Segments)
|
---|
140 | {
|
---|
141 | if (pathSegment is LineSegment)
|
---|
142 | {
|
---|
143 | Point pt2 = (pathSegment as LineSegment).Point;
|
---|
144 | length += (pt2 - pt1).Length;
|
---|
145 | pt1 = pt2;
|
---|
146 | }
|
---|
147 | else if (pathSegment is PolyLineSegment)
|
---|
148 | {
|
---|
149 | PointCollection pointCollection = (pathSegment as PolyLineSegment).Points;
|
---|
150 | foreach (Point pt2 in pointCollection)
|
---|
151 | {
|
---|
152 | length += (pt2 - pt1).Length;
|
---|
153 | pt1 = pt2;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | return length;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|