-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISEFontSmoothing.psm1
212 lines (178 loc) · 7.72 KB
/
ISEFontSmoothing.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<#
Copyright 2017 George Chakhidze
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>
$Local:ISEFontSmoothingCSharpCode = @'
namespace ISEFontSmoothing
{
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
// There is no dependency on Visual Studio, - these namespaces are part of Microsoft.PowerShell.Editor assembly:
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
public static class ISEFontSmoother
{
public static void InjectTextEditorFactoryService()
{
Type editorImportsType = Type.GetType("Microsoft.Windows.PowerShell.Gui.Internal.EditorImports, Microsoft.PowerShell.GPowerShell, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL", true);
FieldInfo valueField = editorImportsType.GetField("value", BindingFlags.Static | BindingFlags.NonPublic);
FieldInfo textEditorFactoryServiceField = editorImportsType.GetField("textEditorFactoryService", BindingFlags.NonPublic | BindingFlags.Instance);
object editorImportsObject = valueField.GetValue(null);
object existingFactory = textEditorFactoryServiceField.GetValue(editorImportsObject);
FontSmoothingTextEditorFactoryService fontSmoother = new FontSmoothingTextEditorFactoryService(existingFactory as ITextEditorFactoryService);
textEditorFactoryServiceField.SetValue(editorImportsObject, fontSmoother);
}
}
internal static class FontSmoother
{
public static void Smooth(DependencyObject element)
{
if (element != null)
{
TextOptions.SetTextFormattingMode(element, TextFormattingMode.Ideal);
TextOptions.SetTextRenderingMode(element, TextRenderingMode.Auto);
TextOptions.SetTextHintingMode(element, TextHintingMode.Fixed);
}
}
}
internal sealed class ISEFontSmoothingTextViewMonitor
{
private IWpfTextView textView;
public ISEFontSmoothingTextViewMonitor(IWpfTextView textView_)
{
this.textView = textView_;
this.textView.LayoutChanged += new EventHandler<TextViewLayoutChangedEventArgs>(this.OnTextViewLayoutChanged);
this.textView.Closed += new EventHandler(this.OnTextViewClosed);
}
private void Update()
{
FontSmoother.Smooth(this.textView.VisualElement);
}
private void OnTextViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
this.Update();
}
private void OnTextViewClosed(object sender, EventArgs e)
{
this.textView.LayoutChanged -= new EventHandler<TextViewLayoutChangedEventArgs>(this.OnTextViewLayoutChanged);
this.textView.Closed -= new EventHandler(this.OnTextViewClosed);
this.textView = null;
}
}
[TextViewRole("DOCUMENT")]
[ContentType("powershell"), Export(typeof(IWpfTextViewCreationListener))]
internal sealed class ISEFontSmoothingTextViewsManager : IWpfTextViewCreationListener
{
public void TextViewCreated(IWpfTextView textView)
{
new ISEFontSmoothingTextViewMonitor(textView);
}
}
internal sealed class FontSmoothingTextEditorFactoryService : ITextEditorFactoryService
{
private readonly ITextEditorFactoryService original;
private readonly ISEFontSmoothingTextViewsManager smoother;
public FontSmoothingTextEditorFactoryService(ITextEditorFactoryService existing)
{
if (existing == null)
{
throw new ArgumentNullException("existing");
}
this.original = existing;
this.smoother = new ISEFontSmoothingTextViewsManager();
}
public ITextViewRoleSet AllPredefinedRoles
{
get { return this.original.AllPredefinedRoles; }
}
public IWpfTextView CreateTextView()
{
IWpfTextView view = this.original.CreateTextView();
this.smoother.TextViewCreated(view);
return view;
}
public IWpfTextView CreateTextView(ITextBuffer textBuffer)
{
IWpfTextView view = this.original.CreateTextView(textBuffer);
this.smoother.TextViewCreated(view);
return view;
}
public IWpfTextView CreateTextView(ITextBuffer textBuffer, ITextViewRoleSet roles)
{
IWpfTextView view = this.original.CreateTextView(textBuffer, roles);
this.smoother.TextViewCreated(view);
return view;
}
public IWpfTextView CreateTextView(ITextBuffer textBuffer, ITextViewRoleSet roles, IEditorOptions parentOptions)
{
IWpfTextView view = this.original.CreateTextView(textBuffer, roles, parentOptions);
this.smoother.TextViewCreated(view);
return view;
}
public IWpfTextView CreateTextView(ITextDataModel dataModel, ITextViewRoleSet roles, IEditorOptions parentOptions)
{
IWpfTextView view = this.original.CreateTextView(dataModel, roles, parentOptions);
this.smoother.TextViewCreated(view);
return view;
}
public IWpfTextView CreateTextView(ITextViewModel viewModel, ITextViewRoleSet roles, IEditorOptions parentOptions)
{
IWpfTextView view = this.original.CreateTextView(viewModel, roles, parentOptions);
this.smoother.TextViewCreated(view);
return view;
}
public IWpfTextViewHost CreateTextViewHost(IWpfTextView wpfTextView, bool setFocus)
{
return this.original.CreateTextViewHost(wpfTextView, setFocus);
}
public ITextViewRoleSet CreateTextViewRoleSet(params string[] roles)
{
return this.original.CreateTextViewRoleSet(roles);
}
public ITextViewRoleSet CreateTextViewRoleSet(IEnumerable<string> roles)
{
return this.original.CreateTextViewRoleSet(roles);
}
public ITextViewRoleSet DefaultRoles
{
get { return this.original.DefaultRoles; }
}
public ITextViewRoleSet NoRoles
{
get { return this.original.NoRoles; }
}
public event EventHandler<TextViewCreatedEventArgs> TextViewCreated
{
add { this.original.TextViewCreated += value; }
remove { this.original.TextViewCreated -= value; }
}
}
}
'@
$Local:ISEFontSmoothingReferences = @(
'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089',
'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089',
'Microsoft.PowerShell.Editor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35',
'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35',
'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35',
'System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089',
'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35',
'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
if (-not ([System.Management.Automation.PSTypeName]'ISEFontSmoothing.ISEFontSmoother').Type)
{
Add-Type -TypeDefinition $Local:ISEFontSmoothingCSharpCode -ReferencedAssemblies $Local:ISEFontSmoothingReferences
}
[ISEFontSmoothing.ISEFontSmoother]::InjectTextEditorFactoryService()