OutlineText.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Documents;
  5. using System.Windows.Media;
  6. using HandyControl.Data;
  7. using HandyControl.Tools.Helper;
  8. namespace HandyControl.Controls;
  9. public class OutlineText : FrameworkElement
  10. {
  11. private Pen _pen;
  12. private FormattedText _formattedText;
  13. private Geometry _textGeometry;
  14. private PathGeometry _clipGeometry;
  15. static OutlineText()
  16. {
  17. SnapsToDevicePixelsProperty.OverrideMetadata(typeof(OutlineText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
  18. UseLayoutRoundingProperty.OverrideMetadata(typeof(OutlineText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
  19. }
  20. public static readonly DependencyProperty StrokePositionProperty = DependencyProperty.Register(
  21. nameof(StrokePosition), typeof(StrokePosition), typeof(OutlineText), new PropertyMetadata(default(StrokePosition)));
  22. public StrokePosition StrokePosition
  23. {
  24. get => (StrokePosition) GetValue(StrokePositionProperty);
  25. set => SetValue(StrokePositionProperty, value);
  26. }
  27. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  28. nameof(Text), typeof(string), typeof(OutlineText), new FrameworkPropertyMetadata(
  29. string.Empty,
  30. FrameworkPropertyMetadataOptions.AffectsMeasure |
  31. FrameworkPropertyMetadataOptions.AffectsRender, OnFormattedTextInvalidated));
  32. public string Text
  33. {
  34. get => (string) GetValue(TextProperty);
  35. set => SetValue(TextProperty, value);
  36. }
  37. public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
  38. nameof(TextAlignment), typeof(TextAlignment), typeof(OutlineText),
  39. new PropertyMetadata(default(TextAlignment), OnFormattedTextUpdated));
  40. public TextAlignment TextAlignment
  41. {
  42. get => (TextAlignment) GetValue(TextAlignmentProperty);
  43. set => SetValue(TextAlignmentProperty, value);
  44. }
  45. public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
  46. nameof(TextTrimming), typeof(TextTrimming), typeof(OutlineText),
  47. new PropertyMetadata(default(TextTrimming), OnFormattedTextInvalidated));
  48. public TextTrimming TextTrimming
  49. {
  50. get => (TextTrimming) GetValue(TextTrimmingProperty);
  51. set => SetValue(TextTrimmingProperty, value);
  52. }
  53. public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
  54. nameof(TextWrapping), typeof(TextWrapping), typeof(OutlineText),
  55. new PropertyMetadata(TextWrapping.NoWrap, OnFormattedTextInvalidated));
  56. public TextWrapping TextWrapping
  57. {
  58. get => (TextWrapping) GetValue(TextWrappingProperty);
  59. set => SetValue(TextWrappingProperty, value);
  60. }
  61. public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
  62. nameof(Fill), typeof(Brush), typeof(OutlineText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
  63. public Brush Fill
  64. {
  65. get => (Brush) GetValue(FillProperty);
  66. set => SetValue(FillProperty, value);
  67. }
  68. public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
  69. nameof(Stroke), typeof(Brush), typeof(OutlineText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
  70. public Brush Stroke
  71. {
  72. get => (Brush) GetValue(StrokeProperty);
  73. set => SetValue(StrokeProperty, value);
  74. }
  75. public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
  76. nameof(StrokeThickness), typeof(double), typeof(OutlineText), new PropertyMetadata(ValueBoxes.Double0Box, OnFormattedTextUpdated));
  77. public double StrokeThickness
  78. {
  79. get => (double) GetValue(StrokeThicknessProperty);
  80. set => SetValue(StrokeThicknessProperty, value);
  81. }
  82. public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
  83. typeof(OutlineText),
  84. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  85. public FontFamily FontFamily
  86. {
  87. get => (FontFamily) GetValue(FontFamilyProperty);
  88. set => SetValue(FontFamilyProperty, value);
  89. }
  90. public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
  91. typeof(OutlineText),
  92. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  93. [TypeConverter(typeof(FontSizeConverter))]
  94. public double FontSize
  95. {
  96. get => (double) GetValue(FontSizeProperty);
  97. set => SetValue(FontSizeProperty, value);
  98. }
  99. public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
  100. typeof(OutlineText),
  101. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  102. public FontStretch FontStretch
  103. {
  104. get => (FontStretch) GetValue(FontStretchProperty);
  105. set => SetValue(FontStretchProperty, value);
  106. }
  107. public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
  108. typeof(OutlineText),
  109. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  110. public FontStyle FontStyle
  111. {
  112. get => (FontStyle) GetValue(FontStyleProperty);
  113. set => SetValue(FontStyleProperty, value);
  114. }
  115. public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
  116. typeof(OutlineText),
  117. new FrameworkPropertyMetadata(OnFormattedTextUpdated));
  118. public FontWeight FontWeight
  119. {
  120. get => (FontWeight) GetValue(FontWeightProperty);
  121. set => SetValue(FontWeightProperty, value);
  122. }
  123. protected override void OnRender(DrawingContext drawingContext)
  124. {
  125. if (StrokeThickness > 0)
  126. {
  127. EnsureGeometry();
  128. drawingContext.DrawGeometry(Fill, null, _textGeometry);
  129. if (StrokePosition == StrokePosition.Outside)
  130. {
  131. drawingContext.PushClip(_clipGeometry);
  132. }
  133. else if (StrokePosition == StrokePosition.Inside)
  134. {
  135. drawingContext.PushClip(_textGeometry);
  136. }
  137. drawingContext.DrawGeometry(null, _pen, _textGeometry);
  138. if (StrokePosition == StrokePosition.Outside || StrokePosition == StrokePosition.Inside)
  139. {
  140. drawingContext.Pop();
  141. }
  142. }
  143. else
  144. {
  145. UpdateFormattedText();
  146. drawingContext.DrawText(_formattedText, new Point());
  147. }
  148. }
  149. private void UpdatePen()
  150. {
  151. _pen = new Pen(Stroke, StrokeThickness);
  152. if (StrokePosition == StrokePosition.Outside || StrokePosition == StrokePosition.Inside)
  153. {
  154. _pen.Thickness = StrokeThickness * 2;
  155. }
  156. }
  157. private void EnsureFormattedText()
  158. {
  159. if (_formattedText != null || Text == null)
  160. {
  161. return;
  162. }
  163. _formattedText = TextHelper.CreateFormattedText(Text, FlowDirection,
  164. new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize);
  165. UpdateFormattedText();
  166. }
  167. private void EnsureGeometry()
  168. {
  169. if (_textGeometry != null)
  170. {
  171. return;
  172. }
  173. EnsureFormattedText();
  174. _textGeometry = _formattedText.BuildGeometry(new Point(0, 0));
  175. if (StrokePosition == StrokePosition.Outside)
  176. {
  177. var geometry = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));
  178. _clipGeometry = Geometry.Combine(geometry, _textGeometry, GeometryCombineMode.Exclude, null);
  179. }
  180. }
  181. private void UpdateFormattedText()
  182. {
  183. if (_formattedText == null)
  184. {
  185. return;
  186. }
  187. _formattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
  188. _formattedText.TextAlignment = TextAlignment;
  189. _formattedText.Trimming = TextTrimming;
  190. _formattedText.SetFontSize(FontSize);
  191. _formattedText.SetFontStyle(FontStyle);
  192. _formattedText.SetFontWeight(FontWeight);
  193. _formattedText.SetFontFamily(FontFamily);
  194. _formattedText.SetFontStretch(FontStretch);
  195. _formattedText.SetForegroundBrush(Fill);
  196. }
  197. private static void OnFormattedTextUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
  198. {
  199. var outlinedTextBlock = (OutlineText) d;
  200. outlinedTextBlock.UpdateFormattedText();
  201. outlinedTextBlock._textGeometry = null;
  202. outlinedTextBlock.InvalidateMeasure();
  203. outlinedTextBlock.InvalidateVisual();
  204. }
  205. private static void OnFormattedTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
  206. {
  207. var outlinedTextBlock = (OutlineText) d;
  208. outlinedTextBlock._formattedText = null;
  209. outlinedTextBlock._textGeometry = null;
  210. outlinedTextBlock.InvalidateMeasure();
  211. outlinedTextBlock.InvalidateVisual();
  212. }
  213. protected override Size MeasureOverride(Size availableSize)
  214. {
  215. EnsureFormattedText();
  216. // constrain the formatted text according to the available size
  217. // the Math.Min call is important - without this constraint (which seems arbitrary, but is the maximum allowable text width), things blow up when availableSize is infinite in both directions
  218. // the Math.Max call is to ensure we don't hit zero, which will cause MaxTextHeight to throw
  219. _formattedText.MaxTextWidth = Math.Min(3579139, availableSize.Width);
  220. _formattedText.MaxTextHeight = Math.Max(0.0001d, availableSize.Height);
  221. UpdatePen();
  222. // return the desired size
  223. return new Size(_formattedText.Width, _formattedText.Height);
  224. }
  225. }