浏览代码

Added ConsoleDriver.GetFont and Windows' impl.

Charlie Kindel 5 年之前
父节点
当前提交
bf35d8c448

+ 5 - 2
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -17,7 +17,6 @@ namespace Terminal.Gui {
 	/// This is the Curses driver for the gui.cs/Terminal framework.
 	/// </summary>
 	internal class CursesDriver : ConsoleDriver {
-#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
 		public override int Cols => Curses.Cols;
 		public override int Rows => Curses.Lines;
 
@@ -694,6 +693,11 @@ namespace Terminal.Gui {
 			//mouseGrabbed = false;
 			//Curses.mouseinterval (lastMouseInterval);
 		}
+
+		public override ConsoleFont GetFont ()
+		{
+			return null;
+		}
 	}
 
 	internal static class Platform {
@@ -755,7 +759,6 @@ namespace Terminal.Gui {
 			killpg (0, signal);
 			return true;
 		}
-#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
 	}
 
 }

+ 9 - 0
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -464,6 +464,15 @@ namespace Terminal.Gui {
 		{
 		}
 
+		/// <summary>
+		/// 
+		/// </summary>
+		/// <returns></returns>
+		public override ConsoleFont GetFont ()
+		{
+			return null;
+		}
+
 		AutoResetEvent keyReady = new AutoResetEvent (false);
 		AutoResetEvent waitForProbe = new AutoResetEvent (false);
 		ConsoleKeyInfo? windowsKeyResult = null;

+ 5 - 0
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -381,6 +381,11 @@ namespace Terminal.Gui {
 		{
 		}
 
+		public override ConsoleFont GetFont ()
+		{
+			return null;
+		}
+
 		//
 		// These are for the .NET driver, but running natively on Windows, wont run
 		// on the Mono emulation

+ 44 - 1
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -120,6 +120,27 @@ namespace Terminal.Gui {
 			}
 		}
 
+		public ConsoleDriver.ConsoleFont GetFont ()
+		{
+			ConsoleDriver.ConsoleFont cf = null;
+			CONSOLE_FONT_INFO_EX fontInfoEx = new CONSOLE_FONT_INFO_EX ();
+			fontInfoEx.cbSize = Marshal.SizeOf (typeof (CONSOLE_FONT_INFO_EX));
+
+			if (GetCurrentConsoleFontEx(OutputHandle, false, ref fontInfoEx)) {
+				cf = new ConsoleDriver.ConsoleFont ();
+				cf.FaceName = fontInfoEx.FaceName;
+				cf.Size = new Size (fontInfoEx.FontWidth, fontInfoEx.FontHeight);
+				cf.Weight = fontInfoEx.FontWeight;
+			}
+
+			var err = Marshal.GetLastWin32Error ();
+			if (err != 0)
+				throw new System.ComponentModel.Win32Exception (err);
+
+			return cf;
+
+		}
+
 		[Flags]
 		public enum ConsoleModes : uint {
 			EnableProcessedInput = 1,
@@ -420,6 +441,24 @@ namespace Terminal.Gui {
 				return v;
 			}
 		}
+
+		[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Unicode)]
+		public struct CONSOLE_FONT_INFO_EX {
+			public int cbSize;
+			public int FontIndex;
+			public short FontWidth;
+			public short FontHeight;
+			public int FontFamily;
+			public int FontWeight;
+			[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
+			public string FaceName;
+		}
+
+		[DllImport ("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
+		extern static bool GetCurrentConsoleFontEx (
+			IntPtr hConsoleOutput, 
+			bool bMaximumWindow, 
+			[In,Out] ref CONSOLE_FONT_INFO_EX lpConsoleCurrentFont);
 	}
 
 	internal class WindowsDriver : ConsoleDriver, IMainLoopDriver {
@@ -1265,8 +1304,12 @@ namespace Terminal.Gui {
 		public override void CookMouse ()
 		{
 		}
-		#endregion
+		public override ConsoleFont GetFont ()
+		{
+			return winConsole.GetFont ();
 
+		}
+		#endregion
 	}
 
 }

+ 27 - 0
Terminal.Gui/Core/ConsoleDriver.cs

@@ -8,6 +8,7 @@
 using NStack;
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Runtime.CompilerServices;
 
@@ -888,6 +889,12 @@ namespace Terminal.Gui {
 		/// </summary>
 		public abstract void CookMouse ();
 
+		/// <summary>
+		/// Gets the current font set for the console.
+		/// </summary>
+		/// <returns>The Font for the current console. <c>null</c> if the console driver does not support querying the font.</returns>
+		public abstract ConsoleFont GetFont ();
+
 		/// <summary>
 		/// Horizontal line character.
 		/// </summary>
@@ -1025,5 +1032,25 @@ namespace Terminal.Gui {
 		/// <param name="back">Background.</param>
 		/// <returns></returns>
 		public abstract Attribute MakeAttribute (Color fore, Color back);
+
+		/// <summary>
+		/// Contains information for a console font.
+		/// </summary>
+		public class ConsoleFont {
+			/// <summary>
+			/// The name of the typeface (such as Courier or Arial).
+			/// </summary>
+			public string FaceName;
+
+			/// <summary>
+			/// The font weight. The weight can range from 100 to 1000, in multiples of 100. For example, the normal weight is 400, while 700 is bold.
+			/// </summary>
+			public int Weight;
+
+			/// <summary>
+			/// Contains the width and height of each character in the font, in logical units. The X member contains the width, while the Y member contains the height.
+			/// </summary>
+			public Size Size;
+		}
 	}
 }

+ 5 - 1
Terminal.Gui/Views/StatusBar.cs

@@ -181,7 +181,11 @@ namespace Terminal.Gui {
 					}
 					Driver.AddRune (title [n]);
 				}
-				Driver.AddRune (' ');
+				if (i + 1 < Items.Length) {
+					Driver.AddRune (' ');
+					Driver.AddRune (Driver.VLine);
+					Driver.AddRune (' ');
+				}
 			}
 		}
 

+ 7 - 1
UICatalog/UICatalog.cs

@@ -266,6 +266,11 @@ namespace UICatalog {
 			_numlock = new StatusItem (Key.CharMask, "Numlock", null);
 			_scrolllock = new StatusItem (Key.CharMask, "Scrolllock", null);
 
+			ConsoleDriver.ConsoleFont consoleFont = Application.Driver.GetFont ();
+			StatusItem font = new StatusItem (Key.Unknown, $"Console driver does not support querying font.", null); ;
+			if (consoleFont != null) {
+				font = new StatusItem (Key.Unknown, $"Console Font: {consoleFont.FaceName}, {consoleFont.Weight}, {consoleFont.Size.Width}x{consoleFont.Size.Height}", null);
+			}
 			_statusBar = new StatusBar (new StatusItem [] {
 				new StatusItem(Key.ControlQ, "~CTRL-Q~ Quit", () => {
 					if (_runningScenario is null){
@@ -278,7 +283,8 @@ namespace UICatalog {
 				}),
 				_capslock,
 				_numlock,
-				_scrolllock
+				_scrolllock,
+				font
 			});
 
 			SetColorScheme ();