Skip to content

Commit a878b36

Browse files
authored
Fix Raster font check (#825)
The original fix in #771 resulted in essentially never setting the UTF8 output encoding because it only tested the fixed width bit on the font family, but every console font is fixed width. The proper fix appears to be testing that all 4 lower bits in FontFamily are not set. Fix #823
1 parent 3d246d9 commit a878b36

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

PSReadLine/PlatformWindows.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,28 @@ private struct CONSOLE_FONT_INFO_EX
110110
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
111111
private static extern bool GetCurrentConsoleFontEx(IntPtr consoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFO_EX consoleFontInfo);
112112

113-
[Flags()]
113+
[Flags]
114114
internal enum FontFamily : uint
115115
{
116-
TMPF_FIXED_PITCH = 0x01
116+
// If this bit is set the font is a variable pitch font.
117+
// If this bit is clear the font is a fixed pitch font.
118+
TMPF_FIXED_PITCH = 0x01,
119+
TMPF_VECTOR = 0x02,
120+
TMPF_TRUETYPE = 0x04,
121+
TMPF_DEVICE = 0x08,
122+
LOWORDER_BITS = TMPF_FIXED_PITCH | TMPF_VECTOR | TMPF_TRUETYPE | TMPF_DEVICE,
117123
}
118124

119125
internal static bool IsUsingRasterFont()
120126
{
121-
CONSOLE_FONT_INFO_EX fontInfo = new CONSOLE_FONT_INFO_EX();
122-
fontInfo.cbSize = Marshal.SizeOf(fontInfo);
123127
var handle = _outputHandle.Value.DangerousGetHandle();
128+
var fontInfo = new CONSOLE_FONT_INFO_EX { cbSize = Marshal.SizeOf(typeof(CONSOLE_FONT_INFO_EX)) };
124129
bool result = GetCurrentConsoleFontEx(handle, false, ref fontInfo);
125-
// If this bit is set the font is a variable pitch font.
126-
// If this bit is clear the font is a fixed pitch font.
127-
// Note very carefully that those meanings are the opposite of what the constant name implies.
128-
return !fontInfo.FontFamily.HasFlag(FontFamily.TMPF_FIXED_PITCH);
130+
// From https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/ns-wingdi-tagtextmetrica
131+
// tmPitchAndFamily - A monospace bitmap font has all of these low-order bits clear;
132+
return result && (fontInfo.FontFamily & FontFamily.LOWORDER_BITS) == 0;
129133
}
130134

131-
132135
private static PSConsoleReadLine _singleton;
133136
internal static IConsole OneTimeInit(PSConsoleReadLine singleton)
134137
{

0 commit comments

Comments
 (0)