From 790e1f783ab852b6b148fffecb605c27e00749f7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 18 Apr 2016 16:49:53 +1000 Subject: [PATCH] Make GDI backend draw text consistently with default.png backend. --- .../2D/Drawing/CanvasDrawer2D.Text.cs | 4 ++ ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs | 47 ++++++++++++------- ClassicalSharp/2D/Screens/FilesScreen.cs | 2 +- ClassicalSharp/2D/Screens/FpsScreen.cs | 4 +- .../2D/Screens/Menu/MenuOptionsScreen.cs | 3 +- ClassicalSharp/2D/Screens/Menu/MenuScreen.cs | 3 +- Launcher2/LauncherWindow.Background.cs | 6 +-- Launcher2/LauncherWindow.cs | 2 +- 8 files changed, 42 insertions(+), 29 deletions(-) diff --git a/ClassicalSharp/2D/Drawing/CanvasDrawer2D.Text.cs b/ClassicalSharp/2D/Drawing/CanvasDrawer2D.Text.cs index 1c914ee5e..c7ef31a2d 100644 --- a/ClassicalSharp/2D/Drawing/CanvasDrawer2D.Text.cs +++ b/ClassicalSharp/2D/Drawing/CanvasDrawer2D.Text.cs @@ -66,6 +66,10 @@ namespace ClassicalSharp { return Size.Ceiling( total ); } + int PtToPx( int point ) { + return (int)Math.Ceiling( (float)point / 72 * 96 ); // TODO: not sure if even right, non 96 dpi? + } + public override Size MeasureBitmappedSize( ref DrawTextArgs args ) { return MeasureBitmappedSizeImpl( ref args ); } diff --git a/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs b/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs index 73c1eb4af..c5568579e 100644 --- a/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs +++ b/ClassicalSharp/2D/Drawing/IDrawer2D.TextMC.cs @@ -66,7 +66,7 @@ namespace ClassicalSharp { int xMul = args.Font.Style == FontStyle.Italic ? 1 : 0; int runCount = 0, lastY = -1; string text = args.Text; - float point = args.Font.Size; + int point = Utils.Floor( args.Font.Size ); byte* coordsPtr = stackalloc byte[256]; for( int i = 0; i < text.Length; i++ ) { @@ -100,23 +100,25 @@ namespace ClassicalSharp { } void DrawRun( FastBitmap dst, int x, int y, int xMul, string text, - int runCount, byte* coords, float point, FastColour col ) { + int runCount, byte* coords, int point, FastColour col ) { if( runCount == 0 ) return; int srcY = (coords[0] >> 4) * boxSize; - int srcHeight = boxSize, dstHeight = PtToPx( point, srcHeight ); + int textHeight = AdjTextSize( point ), cellHeight = CellSize( textHeight ); + int padding = (cellHeight - textHeight) / 2; int startX = x; + ushort* dstWidths = stackalloc ushort[runCount]; for( int i = 0; i < runCount; i++ ) dstWidths[i] = (ushort)PtToPx( point, widths[coords[i]] ); - for( int yy = 0; yy < dstHeight; yy++ ) { - int fontY = srcY + yy * srcHeight / dstHeight; + for( int yy = 0; yy < textHeight; yy++ ) { + int fontY = srcY + yy * boxSize / textHeight; int* fontRow = fontPixels.GetRowPtr( fontY ); - int dstY = y + yy; + int dstY = y + (yy + padding); if( dstY >= dst.Height ) return; int* dstRow = dst.GetRowPtr( dstY ); - int xOffset = xMul * ((dstHeight - 1 - yy) / italicSize); + int xOffset = xMul * ((textHeight - 1 - yy) / italicSize); for( int i = 0; i < runCount; i++ ) { int srcX = (coords[i] & 0x0F) * boxSize; int srcWidth = widths[coords[i]], dstWidth = dstWidths[i]; @@ -141,17 +143,17 @@ namespace ClassicalSharp { } void DrawUnderline( FastBitmap dst, int x, int yOffset, ref DrawTextArgs args, bool shadowCol ) { - int height = PtToPx( args.Font.Size, boxSize ); + int point = Utils.Floor( args.Font.Size ); + int height = PtToPx( point, boxSize ); int offset = ShadowOffset( args.Font.Size ); - int col = FastColour.White.ToArgb(); - float point = args.Font.Size; + int col = FastColour.White.ToArgb(); string text = args.Text; if( args.UseShadow ) height += offset; int startX = x; for( int yy = height - offset; yy < height; yy++ ) { - int* dstRow = dst.GetRowPtr( yy + yOffset ); + int* dstRow = dst.GetRowPtr( yy + yOffset ); for( int i = 0; i < text.Length; i++ ) { char c = text[i]; @@ -175,8 +177,9 @@ namespace ClassicalSharp { protected Size MeasureBitmappedSizeImpl( ref DrawTextArgs args ) { if( String.IsNullOrEmpty( args.Text ) ) return Size.Empty; - float point = args.Font.Size; - Size total = new Size( 0, PtToPx( point, boxSize ) ); + int textHeight = AdjTextSize( Utils.Floor( args.Font.Size ) ); + Size total = new Size( 0, CellSize( textHeight ) ); + int point = Utils.Floor( args.Font.Size ); for( int i = 0; i < args.Text.Length; i++ ) { char c = args.Text[i]; @@ -209,18 +212,26 @@ namespace ClassicalSharp { return (int)'?'; } - protected static int ShadowOffset( float fontSize ) { + protected int ShadowOffset( float fontSize ) { if( fontSize < 9.9f ) return 1; if( fontSize < 24.9f ) return 2; return 3; } - protected int PtToPx( int point ) { - return (int)Math.Ceiling( (float)point / 72 * 96 ); // TODO: non 96 dpi? + protected int PtToPx( int point, int value ) { + return Utils.CeilDiv( value * point, boxSize ); } - protected int PtToPx( float point, float value ) { - return (int)Math.Ceiling( (value / boxSize) * point / 72f * 96f ); + /// Rounds the given font size up to the nearest whole + /// multiple of the size of a character in default.png. + protected int AdjTextSize( int point ) { + return Utils.CeilDiv( point, boxSize ) * boxSize; + } + + /// Returns the height of the bounding box that + /// contains both the given font size in addition to padding. + protected static int CellSize( int point ) { + return Utils.CeilDiv( point * 3, 2 ); } protected void DisposeBitmappedText() { diff --git a/ClassicalSharp/2D/Screens/FilesScreen.cs b/ClassicalSharp/2D/Screens/FilesScreen.cs index c3b9c7df4..4f6f27243 100644 --- a/ClassicalSharp/2D/Screens/FilesScreen.cs +++ b/ClassicalSharp/2D/Screens/FilesScreen.cs @@ -19,7 +19,7 @@ namespace ClassicalSharp.Gui { protected string titleText; public override void Init() { - textFont = new Font( game.FontName, 14, FontStyle.Bold ); + textFont = new Font( game.FontName, 16, FontStyle.Bold ); arrowFont = new Font( game.FontName, 18, FontStyle.Bold ); titleFont = new Font( game.FontName, 16, FontStyle.Bold ); title = TextWidget.Create( game, 0, -130, titleText, Anchor.Centre, Anchor.Centre, titleFont ); diff --git a/ClassicalSharp/2D/Screens/FpsScreen.cs b/ClassicalSharp/2D/Screens/FpsScreen.cs index 90229c518..4f493eb75 100644 --- a/ClassicalSharp/2D/Screens/FpsScreen.cs +++ b/ClassicalSharp/2D/Screens/FpsScreen.cs @@ -82,8 +82,8 @@ namespace ClassicalSharp.Gui { string Q( int value ) { return value == 1 ? "" : "s"; } public override void Init() { - font = new Font( game.FontName, 13 ); - posFont = new Font( game.FontName, 12, FontStyle.Italic ); + font = new Font( game.FontName, 14 ); + posFont = new Font( game.FontName, 13, FontStyle.Italic ); game.Events.ChatFontChanged += ChatFontChanged; fpsTextWidget = new ChatTextWidget( game, font ); diff --git a/ClassicalSharp/2D/Screens/Menu/MenuOptionsScreen.cs b/ClassicalSharp/2D/Screens/Menu/MenuOptionsScreen.cs index 32420e2f8..abc8a4dc7 100644 --- a/ClassicalSharp/2D/Screens/Menu/MenuOptionsScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/MenuOptionsScreen.cs @@ -40,8 +40,7 @@ namespace ClassicalSharp.Gui { public override void Init() { base.Init(); regularFont = new Font( game.FontName, 16, FontStyle.Regular ); - int size = game.Drawer2D.UseBitmappedChat ? 11 : 12; - extendedHelpFont = new Font( game.FontName, size, FontStyle.Regular ); + extendedHelpFont = new Font( game.FontName, 13, FontStyle.Regular ); game.Keyboard.KeyRepeat = true; } diff --git a/ClassicalSharp/2D/Screens/Menu/MenuScreen.cs b/ClassicalSharp/2D/Screens/Menu/MenuScreen.cs index c7f036b64..f82318496 100644 --- a/ClassicalSharp/2D/Screens/Menu/MenuScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/MenuScreen.cs @@ -24,8 +24,7 @@ namespace ClassicalSharp.Gui { } public override void Init() { - int size = game.Drawer2D.UseBitmappedChat ? 13 : 16; - titleFont = new Font( game.FontName, size, FontStyle.Bold ); + titleFont = new Font( game.FontName, 16, FontStyle.Bold ); } public override void Dispose() { diff --git a/Launcher2/LauncherWindow.Background.cs b/Launcher2/LauncherWindow.Background.cs index 246c268c7..5ae7e070e 100644 --- a/Launcher2/LauncherWindow.Background.cs +++ b/Launcher2/LauncherWindow.Background.cs @@ -96,15 +96,15 @@ namespace Launcher { using( IDrawer2D drawer = Drawer ) { drawer.SetBitmap( Framebuffer ); - drawer.UseBitmappedChat = useBitmappedFont; + drawer.UseBitmappedChat = useBitmappedFont || ClassicBackground; DrawTextArgs args = new DrawTextArgs( "&eClassical&fSharp", logoFont, false ); Size size = drawer.MeasureChatSize( ref args ); int xStart = Width / 2 - size.Width / 2; args.Text = "&0Classical&0Sharp"; - drawer.DrawChatText( ref args, xStart + 4, 8 + 4 ); + drawer.DrawChatText( ref args, xStart + 3, 3 ); args.Text = "&eClassical&fSharp"; - drawer.DrawChatText( ref args, xStart, 8 ); + drawer.DrawChatText( ref args, xStart, 0 ); drawer.UseBitmappedChat = false; } } diff --git a/Launcher2/LauncherWindow.cs b/Launcher2/LauncherWindow.cs index 689b91c33..5671d7165 100644 --- a/Launcher2/LauncherWindow.cs +++ b/Launcher2/LauncherWindow.cs @@ -70,7 +70,7 @@ namespace Launcher { Window.WindowStateChanged += Resize; Window.Keyboard.KeyDown += KeyDown; LoadFont(); - logoFont = new Font( FontName, 24, FontStyle.Regular ); + logoFont = new Font( FontName, 32, FontStyle.Regular ); string path = Assembly.GetExecutingAssembly().Location; Window.Icon = Icon.ExtractAssociatedIcon( path ); //Minimised = Window.WindowState == WindowState.Minimized;