Skip to content

Commit

Permalink
Merge pull request #524 from reduckted/bugfix/font-size
Browse files Browse the repository at this point in the history
Calculated the correct font size for use in WPF.
  • Loading branch information
madskristensen authored Oct 31, 2024
2 parents cc88202 + ace0bd8 commit 5301fca
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ int IVsFontAndColorEvents.OnFontChanged(ref Guid rguidCategory, FontInfo[] pInfo
// only want to handle the changes for this category.
if (rguidCategory.Equals(_categoryGuid))
{
EmitChange((x) => x.SetFont(ref pInfo[0]));
EmitChange((x) => x.SetFont(ref pLOGFONT[0], ref pInfo[0]));
}
return VSConstants.S_OK;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Windows.Media;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell.Interop;
Expand All @@ -22,14 +23,16 @@ public class ConfiguredFont : ObservableObject
private FontFamily? _family;
private bool _hasFamily;
private string _familyName;
private int _pointSize;
private double _size;
private byte _characterSet;

internal ConfiguredFont(ref FontInfo info)
internal ConfiguredFont(ref LOGFONTW logfont, ref FontInfo fontInfo)
{
_familyName = info.bstrFaceName;
_size = info.wPointSize;
_characterSet = info.iCharSet;
_familyName = fontInfo.bstrFaceName;
_pointSize = fontInfo.wPointSize;
_size = CalculateFontSize(ref logfont);
_characterSet = fontInfo.iCharSet;
}

/// <summary>
Expand Down Expand Up @@ -61,7 +64,14 @@ public FontFamily? Family
public string FamilyName => _familyName;

/// <summary>
/// The font size.
/// The font size, in points. This is the value specified on the <i>Fonts and Colors</i> options page.
/// </summary>
public int PointSize => _pointSize;

/// <summary>
/// The font size, for use in WPF. This is the font size that can be used in WPF controls.
/// For example, the value can be used directly in the
/// <see cref="System.Windows.Documents.TextElement.FontSize"/> property.
/// </summary>
public double Size => _size;

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

internal bool Update(ref FontInfo info)
internal bool Update(ref LOGFONTW logfont, ref FontInfo info)
{
bool changed = false;
string oldFaceName = _familyName;
int oldPointSize = _pointSize;
double oldSize = _size;
byte oldCharacterSet = _characterSet;

// Update all of the fields first so that
// everything is set before we raise the events.
_familyName = info.bstrFaceName;
_size = info.wPointSize;
_pointSize = info.wPointSize;
_size = CalculateFontSize(ref logfont);
_characterSet = info.iCharSet;

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

if (oldPointSize != _pointSize)
{
changed = true;
NotifyPropertyChanged(nameof(PointSize));
}

if (oldSize != _size)
{
changed = true;
Expand All @@ -106,5 +124,18 @@ internal bool Update(ref FontInfo info)

return changed;
}

private static double CalculateFontSize(ref LOGFONTW logfont)
{
return Math.Abs(logfont.lfHeight) * 96.0 /
#if VS14
// `DpiAwareness` does not exist in VS 14, so default
// to 96.0, which is the standard system DPI.
96.0
#else
Microsoft.VisualStudio.Utilities.DpiAwareness.SystemDpiY
#endif
;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ namespace Community.VisualStudio.Toolkit

internal ConfiguredFontAndColorSet(
T category,
ref FontInfo font,
ref LOGFONTW logfont,
ref FontInfo fontInfo,
Dictionary<ColorDefinition, ConfiguredColor> colors,
Action<IFontAndColorChangeListener> onDispose
)
{
Category = category;
Font = new ConfiguredFont(ref font);
Font = new ConfiguredFont(ref logfont, ref fontInfo);
_colors = colors;
_onDispose = onDispose;
}
Expand Down Expand Up @@ -64,9 +65,9 @@ public ConfiguredColor GetColor(ColorDefinition definition)
/// </summary>
public event EventHandler<ConfiguredColorChangedEventArgs>? ColorChanged;

void IFontAndColorChangeListener.SetFont(ref FontInfo info)
void IFontAndColorChangeListener.SetFont(ref LOGFONTW logfont, ref FontInfo info)
{
if (Font.Update(ref info))
if (Font.Update(ref logfont, ref info))
{
FontChanged?.Invoke(this, EventArgs.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ internal FontsAndColors()
T category = BaseFontAndColorCategory<T>.Instance;

FontInfo[] fontInfo = new FontInfo[1];
ErrorHandler.ThrowOnFailure(storage.GetFont(null, fontInfo));
LOGFONTW[] logfont = new LOGFONTW[1];
ErrorHandler.ThrowOnFailure(storage.GetFont(logfont, fontInfo));

ConfiguredFontAndColorSet<T> set = new(
category,
ref logfont[0],
ref fontInfo[0],
await GetColorsAsync(category, categoryGuid, storage),
category.UnregisterChangeListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Community.VisualStudio.Toolkit
{
internal interface IFontAndColorChangeListener
{
void SetFont(ref FontInfo info);
void SetFont(ref LOGFONTW logfont, ref FontInfo info);

void SetColor(ColorDefinition definition, uint background, uint foreground, FontStyle fontStyle);
}
Expand Down

0 comments on commit 5301fca

Please sign in to comment.