Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/TextFormatterFactory.cs @ 15377

Last change on this file since 15377 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.2 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Globalization;
21using System.Reflection;
22using System.Windows;
23using System.Windows.Controls;
24using System.Windows.Media;
25using System.Windows.Media.TextFormatting;
26
27namespace ICSharpCode.AvalonEdit.Utils
28{
29  /// <summary>
30  /// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
31  /// </summary>
32  static class TextFormatterFactory
33  {
34    #if !DOTNET4
35    readonly static DependencyProperty TextFormattingModeProperty;
36   
37    static TextFormatterFactory()
38    {
39      Assembly presentationFramework = typeof(FrameworkElement).Assembly;
40      Type textOptionsType = presentationFramework.GetType("System.Windows.Media.TextOptions", false);
41      if (textOptionsType != null) {
42        TextFormattingModeProperty = textOptionsType.GetField("TextFormattingModeProperty").GetValue(null) as DependencyProperty;
43      }
44    }
45    #endif
46   
47    /// <summary>
48    /// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
49    /// </summary>
50    public static TextFormatter Create(DependencyObject owner)
51    {
52      if (owner == null)
53        throw new ArgumentNullException("owner");
54      #if DOTNET4
55      return TextFormatter.Create(TextOptions.GetTextFormattingMode(owner));
56      #else
57      if (TextFormattingModeProperty != null) {
58        object formattingMode = owner.GetValue(TextFormattingModeProperty);
59        return (TextFormatter)typeof(TextFormatter).InvokeMember(
60          "Create",
61          BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
62          null, null,
63          new object[] { formattingMode },
64          CultureInfo.InvariantCulture);
65      } else {
66        return TextFormatter.Create();
67      }
68      #endif
69    }
70   
71    /// <summary>
72    /// Returns whether the specified dependency property affects the text formatter creation.
73    /// Controls should re-create their text formatter for such property changes.
74    /// </summary>
75    public static bool PropertyChangeAffectsTextFormatter(DependencyProperty dp)
76    {
77      #if DOTNET4
78      return dp == TextOptions.TextFormattingModeProperty;
79      #else
80      return dp == TextFormattingModeProperty && TextFormattingModeProperty != null;
81      #endif
82    }
83   
84    /// <summary>
85    /// Creates formatted text.
86    /// </summary>
87    /// <param name="element">The owner element. The text formatter setting are read from this element.</param>
88    /// <param name="text">The text.</param>
89    /// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
90    /// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
91    /// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
92    /// <returns>A FormattedText object using the specified settings.</returns>
93    public static FormattedText CreateFormattedText(FrameworkElement element, string text, Typeface typeface, double? emSize, Brush foreground)
94    {
95      if (element == null)
96        throw new ArgumentNullException("element");
97      if (text == null)
98        throw new ArgumentNullException("text");
99      if (typeface == null)
100        typeface = element.CreateTypeface();
101      if (emSize == null)
102        emSize = TextBlock.GetFontSize(element);
103      if (foreground == null)
104        foreground = TextBlock.GetForeground(element);
105      #if DOTNET4
106      return new FormattedText(
107        text,
108        CultureInfo.CurrentCulture,
109        FlowDirection.LeftToRight,
110        typeface,
111        emSize.Value,
112        foreground,
113        null,
114        TextOptions.GetTextFormattingMode(element)
115      );
116      #else
117      if (TextFormattingModeProperty != null) {
118        object formattingMode = element.GetValue(TextFormattingModeProperty);
119        return (FormattedText)Activator.CreateInstance(
120          typeof(FormattedText),
121          text,
122          CultureInfo.CurrentCulture,
123          FlowDirection.LeftToRight,
124          typeface,
125          emSize,
126          foreground,
127          null,
128          formattingMode
129        );
130      } else {
131        return new FormattedText(
132          text,
133          CultureInfo.CurrentCulture,
134          FlowDirection.LeftToRight,
135          typeface,
136          emSize.Value,
137          foreground
138        );
139      }
140      #endif
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.