From f7dc2e24dc71044d3b468554161503658a8b379a Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Mon, 29 Mar 2021 15:29:03 +0200 Subject: [PATCH] Fixed Text rendering --- SMCode/SM.Base/Drawing/Text/Font.cs | 15 +++++++-------- SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs | 10 ++-------- SM_TEST/Program.cs | 13 ++++++++++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/SMCode/SM.Base/Drawing/Text/Font.cs b/SMCode/SM.Base/Drawing/Text/Font.cs index f763f1c..023726f 100644 --- a/SMCode/SM.Base/Drawing/Text/Font.cs +++ b/SMCode/SM.Base/Drawing/Text/Font.cs @@ -32,6 +32,8 @@ namespace SM.Base.Drawing.Text /// public float FontSize = 12; + public float SpaceWidth { get; private set; } + /// /// The font style. /// Default: @@ -43,11 +45,6 @@ namespace SM.Base.Drawing.Text /// public Dictionary Positions = new Dictionary(); - /// - /// Allows a font wide spacing option. - /// - public float Spacing = 1; - /// /// Generates a font from a font family from the specified path. /// @@ -92,7 +89,7 @@ namespace SM.Base.Drawing.Text foreach (var c in CharSet) { var s = c.ToString(); - var size = g.MeasureString(s, f); + var size = g.MeasureString(s, f, 0, StringFormat.GenericTypographic); try { charParams.Add(c, new[] {size.Width, Width}); @@ -105,6 +102,8 @@ namespace SM.Base.Drawing.Text if (Height < size.Height) Height = (int) size.Height; Width += (int) size.Width; } + + SpaceWidth = g.MeasureString("_", f, 0, StringFormat.GenericTypographic).Width; } map = new Bitmap(Width, Height); @@ -112,7 +111,7 @@ namespace SM.Base.Drawing.Text { foreach (var keyValuePair in charParams) { - var normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width; + var normalizedX = (keyValuePair.Value[1]) / Width; var normalizedWidth = keyValuePair.Value[0] / Width; CharParameter parameter; @@ -124,7 +123,7 @@ namespace SM.Base.Drawing.Text X = (int) keyValuePair.Value[1] }); - g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0); + g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0, StringFormat.GenericTypographic); } } } diff --git a/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs b/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs index ab886e7..23fdc16 100644 --- a/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs @@ -42,12 +42,6 @@ namespace SM.Base.Drawing.Text /// public float Spacing = 1; - - /// - /// Calculates the actual spacing for the object. - /// - public float ActualSpacing => Spacing * Font.Spacing; - /// /// The font. /// @@ -120,7 +114,7 @@ namespace SM.Base.Drawing.Text { if (_text[i] == ' ') { - x += Font.FontSize * ActualSpacing; + x += Font.SpaceWidth * Spacing; continue; } @@ -151,7 +145,7 @@ namespace SM.Base.Drawing.Text new Vector2(parameter.NormalizedWidth, 1), 0) }; - x += parameter.Width * ActualSpacing; + x += parameter.Width * Spacing; _last = parameter; } diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index a191b8c..3bd922d 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -1,8 +1,10 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using OpenTK; using SM.Base.Animation; using SM.Base.Controls; +using SM.Base.Drawing.Text; using SM.Base.Time; using SM.Base.Window; using SM2D; @@ -22,15 +24,20 @@ namespace SM_TEST private static InterpolationProcess interpolation; static void Main(string[] args) { + Font font = new Font(@"C:\Windows\Fonts\Arial.ttf") + { + FontSize = 20, + CharSet = new List(){'H', 'i', 'I', ','} + }; + window = new GLWindow(1280, 720, "0ms", WindowFlags.Window, VSyncMode.Off); window.ApplySetup(new Window2DSetup()); window.SetRenderPipeline(new TestRenderPipeline()); window.SetScene(scene = new Scene()); - DrawObject2D testObj = new DrawObject2D(); - interpolation = testObj.Transform.Position.Interpolate(TimeSpan.FromSeconds(5), new Vector2(300), new BezierCurve(Vector2.Zero,new Vector2(0.43f, 0), new Vector2(.5f, 1.5f), Vector2.One)); - scene.Objects.Add(testObj); + DrawText text = new DrawText(font, "Hi, HI"); + scene.Objects.Add(text); window.UpdateFrame += WindowOnUpdateFrame; window.RenderFrame += Window_RenderFrame;