1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Globalization;
6
using System.Reflection;
8
using System.Windows.Controls;
9
using System.Windows.Media;
10
using System.Windows.Media.TextFormatting;
12
namespace ICSharpCode.AvalonEdit.Utils
15
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
17
static class TextFormatterFactory
20
readonly static DependencyProperty TextFormattingModeProperty;
22
static TextFormatterFactory()
24
Assembly presentationFramework = typeof(FrameworkElement).Assembly;
25
Type textOptionsType = presentationFramework.GetType("System.Windows.Media.TextOptions", false);
26
if (textOptionsType != null) {
27
TextFormattingModeProperty = textOptionsType.GetField("TextFormattingModeProperty").GetValue(null) as DependencyProperty;
33
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
35
public static TextFormatter Create(DependencyObject owner)
38
throw new ArgumentNullException("owner");
40
return TextFormatter.Create(TextOptions.GetTextFormattingMode(owner));
42
if (TextFormattingModeProperty != null) {
43
object formattingMode = owner.GetValue(TextFormattingModeProperty);
44
return (TextFormatter)typeof(TextFormatter).InvokeMember(
46
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
48
new object[] { formattingMode },
49
CultureInfo.InvariantCulture);
51
return TextFormatter.Create();
57
/// Returns whether the specified dependency property affects the text formatter creation.
58
/// Controls should re-create their text formatter for such property changes.
60
public static bool PropertyChangeAffectsTextFormatter(DependencyProperty dp)
63
return dp == TextOptions.TextFormattingModeProperty;
65
return dp == TextFormattingModeProperty && TextFormattingModeProperty != null;
70
/// Creates formatted text.
72
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
73
/// <param name="text">The text.</param>
74
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
75
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
76
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
77
/// <returns>A FormattedText object using the specified settings.</returns>
78
public static FormattedText CreateFormattedText(FrameworkElement element, string text, Typeface typeface, double? emSize, Brush foreground)
81
throw new ArgumentNullException("element");
83
throw new ArgumentNullException("text");
85
typeface = element.CreateTypeface();
87
emSize = TextBlock.GetFontSize(element);
88
if (foreground == null)
89
foreground = TextBlock.GetForeground(element);
91
return new FormattedText(
93
CultureInfo.CurrentCulture,
94
FlowDirection.LeftToRight,
99
TextOptions.GetTextFormattingMode(element)
102
if (TextFormattingModeProperty != null) {
103
object formattingMode = element.GetValue(TextFormattingModeProperty);
104
return (FormattedText)Activator.CreateInstance(
105
typeof(FormattedText),
107
CultureInfo.CurrentCulture,
108
FlowDirection.LeftToRight,
116
return new FormattedText(
118
CultureInfo.CurrentCulture,
119
FlowDirection.LeftToRight,