~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Utils/TextFormatterFactory.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Globalization;
 
6
using System.Reflection;
 
7
using System.Windows;
 
8
using System.Windows.Controls;
 
9
using System.Windows.Media;
 
10
using System.Windows.Media.TextFormatting;
 
11
 
 
12
namespace ICSharpCode.AvalonEdit.Utils
 
13
{
 
14
        /// <summary>
 
15
        /// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
 
16
        /// </summary>
 
17
        static class TextFormatterFactory
 
18
        {
 
19
                #if !DOTNET4
 
20
                readonly static DependencyProperty TextFormattingModeProperty;
 
21
                
 
22
                static TextFormatterFactory()
 
23
                {
 
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;
 
28
                        }
 
29
                }
 
30
                #endif
 
31
                
 
32
                /// <summary>
 
33
                /// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
 
34
                /// </summary>
 
35
                public static TextFormatter Create(DependencyObject owner)
 
36
                {
 
37
                        if (owner == null)
 
38
                                throw new ArgumentNullException("owner");
 
39
                        #if DOTNET4
 
40
                        return TextFormatter.Create(TextOptions.GetTextFormattingMode(owner));
 
41
                        #else
 
42
                        if (TextFormattingModeProperty != null) {
 
43
                                object formattingMode = owner.GetValue(TextFormattingModeProperty);
 
44
                                return (TextFormatter)typeof(TextFormatter).InvokeMember(
 
45
                                        "Create",
 
46
                                        BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
 
47
                                        null, null,
 
48
                                        new object[] { formattingMode },
 
49
                                        CultureInfo.InvariantCulture);
 
50
                        } else {
 
51
                                return TextFormatter.Create();
 
52
                        }
 
53
                        #endif
 
54
                }
 
55
                
 
56
                /// <summary>
 
57
                /// Returns whether the specified dependency property affects the text formatter creation.
 
58
                /// Controls should re-create their text formatter for such property changes.
 
59
                /// </summary>
 
60
                public static bool PropertyChangeAffectsTextFormatter(DependencyProperty dp)
 
61
                {
 
62
                        #if DOTNET4
 
63
                        return dp == TextOptions.TextFormattingModeProperty;
 
64
                        #else
 
65
                        return dp == TextFormattingModeProperty && TextFormattingModeProperty != null;
 
66
                        #endif
 
67
                }
 
68
                
 
69
                /// <summary>
 
70
                /// Creates formatted text.
 
71
                /// </summary>
 
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)
 
79
                {
 
80
                        if (element == null)
 
81
                                throw new ArgumentNullException("element");
 
82
                        if (text == null)
 
83
                                throw new ArgumentNullException("text");
 
84
                        if (typeface == null)
 
85
                                typeface = element.CreateTypeface();
 
86
                        if (emSize == null)
 
87
                                emSize = TextBlock.GetFontSize(element);
 
88
                        if (foreground == null)
 
89
                                foreground = TextBlock.GetForeground(element);
 
90
                        #if DOTNET4
 
91
                        return new FormattedText(
 
92
                                text,
 
93
                                CultureInfo.CurrentCulture,
 
94
                                FlowDirection.LeftToRight,
 
95
                                typeface,
 
96
                                emSize.Value,
 
97
                                foreground,
 
98
                                null,
 
99
                                TextOptions.GetTextFormattingMode(element)
 
100
                        );
 
101
                        #else
 
102
                        if (TextFormattingModeProperty != null) {
 
103
                                object formattingMode = element.GetValue(TextFormattingModeProperty);
 
104
                                return (FormattedText)Activator.CreateInstance(
 
105
                                        typeof(FormattedText),
 
106
                                        text,
 
107
                                        CultureInfo.CurrentCulture,
 
108
                                        FlowDirection.LeftToRight,
 
109
                                        typeface,
 
110
                                        emSize,
 
111
                                        foreground,
 
112
                                        null,
 
113
                                        formattingMode
 
114
                                );
 
115
                        } else {
 
116
                                return new FormattedText(
 
117
                                        text,
 
118
                                        CultureInfo.CurrentCulture,
 
119
                                        FlowDirection.LeftToRight,
 
120
                                        typeface,
 
121
                                        emSize.Value,
 
122
                                        foreground
 
123
                                );
 
124
                        }
 
125
                        #endif
 
126
                }
 
127
        }
 
128
}