Skip to content

Commit 5301fca

Browse files
Merge pull request #524 from reduckted/bugfix/font-size
Calculated the correct font size for use in WPF.
2 parents cc88202 + ace0bd8 commit 5301fca

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

src/toolkit/Community.VisualStudio.Toolkit.Shared/FontsAndColors/BaseFontAndColorCategory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ int IVsFontAndColorEvents.OnFontChanged(ref Guid rguidCategory, FontInfo[] pInfo
246246
// only want to handle the changes for this category.
247247
if (rguidCategory.Equals(_categoryGuid))
248248
{
249-
EmitChange((x) => x.SetFont(ref pInfo[0]));
249+
EmitChange((x) => x.SetFont(ref pLOGFONT[0], ref pInfo[0]));
250250
}
251251
return VSConstants.S_OK;
252252
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/FontsAndColors/ConfiguredFont.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using System;
2+
using System.ComponentModel;
23
using System.Windows.Media;
34
using Microsoft.VisualStudio.PlatformUI;
45
using Microsoft.VisualStudio.Shell.Interop;
@@ -22,14 +23,16 @@ public class ConfiguredFont : ObservableObject
2223
private FontFamily? _family;
2324
private bool _hasFamily;
2425
private string _familyName;
26+
private int _pointSize;
2527
private double _size;
2628
private byte _characterSet;
2729

28-
internal ConfiguredFont(ref FontInfo info)
30+
internal ConfiguredFont(ref LOGFONTW logfont, ref FontInfo fontInfo)
2931
{
30-
_familyName = info.bstrFaceName;
31-
_size = info.wPointSize;
32-
_characterSet = info.iCharSet;
32+
_familyName = fontInfo.bstrFaceName;
33+
_pointSize = fontInfo.wPointSize;
34+
_size = CalculateFontSize(ref logfont);
35+
_characterSet = fontInfo.iCharSet;
3336
}
3437

3538
/// <summary>
@@ -61,7 +64,14 @@ public FontFamily? Family
6164
public string FamilyName => _familyName;
6265

6366
/// <summary>
64-
/// The font size.
67+
/// The font size, in points. This is the value specified on the <i>Fonts and Colors</i> options page.
68+
/// </summary>
69+
public int PointSize => _pointSize;
70+
71+
/// <summary>
72+
/// The font size, for use in WPF. This is the font size that can be used in WPF controls.
73+
/// For example, the value can be used directly in the
74+
/// <see cref="System.Windows.Documents.TextElement.FontSize"/> property.
6575
/// </summary>
6676
public double Size => _size;
6777

@@ -70,17 +80,19 @@ public FontFamily? Family
7080
/// </summary>
7181
public byte CharacterSet => _characterSet;
7282

73-
internal bool Update(ref FontInfo info)
83+
internal bool Update(ref LOGFONTW logfont, ref FontInfo info)
7484
{
7585
bool changed = false;
7686
string oldFaceName = _familyName;
87+
int oldPointSize = _pointSize;
7788
double oldSize = _size;
7889
byte oldCharacterSet = _characterSet;
7990

8091
// Update all of the fields first so that
8192
// everything is set before we raise the events.
8293
_familyName = info.bstrFaceName;
83-
_size = info.wPointSize;
94+
_pointSize = info.wPointSize;
95+
_size = CalculateFontSize(ref logfont);
8496
_characterSet = info.iCharSet;
8597

8698
if (!string.Equals(oldFaceName, _familyName))
@@ -92,6 +104,12 @@ internal bool Update(ref FontInfo info)
92104
NotifyPropertyChanged(nameof(FamilyName));
93105
}
94106

107+
if (oldPointSize != _pointSize)
108+
{
109+
changed = true;
110+
NotifyPropertyChanged(nameof(PointSize));
111+
}
112+
95113
if (oldSize != _size)
96114
{
97115
changed = true;
@@ -106,5 +124,18 @@ internal bool Update(ref FontInfo info)
106124

107125
return changed;
108126
}
127+
128+
private static double CalculateFontSize(ref LOGFONTW logfont)
129+
{
130+
return Math.Abs(logfont.lfHeight) * 96.0 /
131+
#if VS14
132+
// `DpiAwareness` does not exist in VS 14, so default
133+
// to 96.0, which is the standard system DPI.
134+
96.0
135+
#else
136+
Microsoft.VisualStudio.Utilities.DpiAwareness.SystemDpiY
137+
#endif
138+
;
139+
}
109140
}
110141
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/FontsAndColors/ConfiguredFontAndColorSet.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ namespace Community.VisualStudio.Toolkit
1414

1515
internal ConfiguredFontAndColorSet(
1616
T category,
17-
ref FontInfo font,
17+
ref LOGFONTW logfont,
18+
ref FontInfo fontInfo,
1819
Dictionary<ColorDefinition, ConfiguredColor> colors,
1920
Action<IFontAndColorChangeListener> onDispose
2021
)
2122
{
2223
Category = category;
23-
Font = new ConfiguredFont(ref font);
24+
Font = new ConfiguredFont(ref logfont, ref fontInfo);
2425
_colors = colors;
2526
_onDispose = onDispose;
2627
}
@@ -64,9 +65,9 @@ public ConfiguredColor GetColor(ColorDefinition definition)
6465
/// </summary>
6566
public event EventHandler<ConfiguredColorChangedEventArgs>? ColorChanged;
6667

67-
void IFontAndColorChangeListener.SetFont(ref FontInfo info)
68+
void IFontAndColorChangeListener.SetFont(ref LOGFONTW logfont, ref FontInfo info)
6869
{
69-
if (Font.Update(ref info))
70+
if (Font.Update(ref logfont, ref info))
7071
{
7172
FontChanged?.Invoke(this, EventArgs.Empty);
7273
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/FontsAndColors/FontsAndColors.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ internal FontsAndColors()
3737
T category = BaseFontAndColorCategory<T>.Instance;
3838

3939
FontInfo[] fontInfo = new FontInfo[1];
40-
ErrorHandler.ThrowOnFailure(storage.GetFont(null, fontInfo));
40+
LOGFONTW[] logfont = new LOGFONTW[1];
41+
ErrorHandler.ThrowOnFailure(storage.GetFont(logfont, fontInfo));
4142

4243
ConfiguredFontAndColorSet<T> set = new(
4344
category,
45+
ref logfont[0],
4446
ref fontInfo[0],
4547
await GetColorsAsync(category, categoryGuid, storage),
4648
category.UnregisterChangeListener

src/toolkit/Community.VisualStudio.Toolkit.Shared/FontsAndColors/IFontAndColorChangeListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Community.VisualStudio.Toolkit
44
{
55
internal interface IFontAndColorChangeListener
66
{
7-
void SetFont(ref FontInfo info);
7+
void SetFont(ref LOGFONTW logfont, ref FontInfo info);
88

99
void SetColor(ColorDefinition definition, uint background, uint foreground, FontStyle fontStyle);
1010
}

0 commit comments

Comments
 (0)