Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic changing of font in Windows console on CJK codepages #771

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions PSReadLine/PlatformWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ private static bool OnBreak(ConsoleBreakSignal signal)
return false;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct CONSOLE_FONT_INFO_EX
{
internal int cbSize;
internal int nFont;
internal short FontWidth;
internal short FontHeight;
internal FontFamily FontFamily;
internal uint FontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
internal string FontFace;
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool GetCurrentConsoleFontEx(IntPtr consoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFO_EX consoleFontInfo);

[Flags()]
internal enum FontFamily : uint
{
TMPF_FIXED_PITCH = 0x01
}

internal static bool IsUsingRasterFont()
{
CONSOLE_FONT_INFO_EX fontInfo = new CONSOLE_FONT_INFO_EX();
fontInfo.cbSize = Marshal.SizeOf(fontInfo);
var handle = _outputHandle.Value.DangerousGetHandle();
bool result = GetCurrentConsoleFontEx(handle, false, ref fontInfo);
// If this bit is set the font is a variable pitch font.
// If this bit is clear the font is a fixed pitch font.
// Note very carefully that those meanings are the opposite of what the constant name implies.
return !fontInfo.FontFamily.HasFlag(FontFamily.TMPF_FIXED_PITCH);
}


private static PSConsoleReadLine _singleton;
internal static IConsole OneTimeInit(PSConsoleReadLine singleton)
{
Expand Down Expand Up @@ -333,13 +368,18 @@ private static bool SetConsoleOutputVirtualTerminalProcessing()
&& SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}

internal static bool IsConsoleInput()
{
var handle = GetStdHandle((uint)StandardHandleId.Input);
return GetFileType(handle) == FILE_TYPE_CHAR;
}

private static bool IsHandleRedirected(bool stdin)
{
var handle = GetStdHandle((uint)(stdin ? StandardHandleId.Input : StandardHandleId.Output));

// If handle is not to a character device, we must be redirected:
int fileType = GetFileType(handle);
if ((fileType & FILE_TYPE_CHAR) != FILE_TYPE_CHAR)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the documentation, GetFileType() returns a single value and is not flags.

if (GetFileType(handle) != FILE_TYPE_CHAR)
return true;

// Char device - if GetConsoleMode succeeds, we are NOT redirected.
Expand Down
19 changes: 16 additions & 3 deletions PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
private IConsole _console;
private ICharMap _charMap;
private Encoding _initialOutputEncoding;

private bool _skipOutputEncodingChange;
private EngineIntrinsics _engineIntrinsics;
private Thread _readKeyThread;
private AutoResetEvent _readKeyWaitHandle;
Expand Down Expand Up @@ -531,7 +531,10 @@ T CallPossibleExternalApplication<T>(Func<T> func)
}
finally
{
_console.OutputEncoding = Encoding.UTF8;
if (!_skipOutputEncodingChange)
{
_console.OutputEncoding = Encoding.UTF8;
}
}
}

Expand Down Expand Up @@ -647,7 +650,17 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
_statusIsErrorMessage = false;

_initialOutputEncoding = _console.OutputEncoding;
_console.OutputEncoding = Encoding.UTF8;

// Don't change the OutputEncoding if already UTF8, no console, or using raster font on Windows
_skipOutputEncodingChange = _initialOutputEncoding == Encoding.UTF8
|| (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
&& PlatformWindows.IsConsoleInput()
&& PlatformWindows.IsUsingRasterFont());

if (!_skipOutputEncodingChange) {
_console.OutputEncoding = Encoding.UTF8;
}

_lastRenderTime = Stopwatch.StartNew();

_killCommandCount = 0;
Expand Down