Fixed Text rendering

This commit is contained in:
Michel Fedde 2021-03-29 15:29:03 +02:00
parent a4cab567b3
commit f7dc2e24dc
3 changed files with 19 additions and 19 deletions

View file

@ -32,6 +32,8 @@ namespace SM.Base.Drawing.Text
/// </summary> /// </summary>
public float FontSize = 12; public float FontSize = 12;
public float SpaceWidth { get; private set; }
/// <summary> /// <summary>
/// The font style. /// The font style.
/// <para>Default: <see cref="System.Drawing.FontStyle.Regular" /></para> /// <para>Default: <see cref="System.Drawing.FontStyle.Regular" /></para>
@ -43,11 +45,6 @@ namespace SM.Base.Drawing.Text
/// </summary> /// </summary>
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>(); public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
/// <summary>
/// Allows a font wide spacing option.
/// </summary>
public float Spacing = 1;
/// <summary> /// <summary>
/// Generates a font from a font family from the specified path. /// Generates a font from a font family from the specified path.
/// </summary> /// </summary>
@ -92,7 +89,7 @@ namespace SM.Base.Drawing.Text
foreach (var c in CharSet) foreach (var c in CharSet)
{ {
var s = c.ToString(); var s = c.ToString();
var size = g.MeasureString(s, f); var size = g.MeasureString(s, f, 0, StringFormat.GenericTypographic);
try try
{ {
charParams.Add(c, new[] {size.Width, Width}); charParams.Add(c, new[] {size.Width, Width});
@ -105,6 +102,8 @@ namespace SM.Base.Drawing.Text
if (Height < size.Height) Height = (int) size.Height; if (Height < size.Height) Height = (int) size.Height;
Width += (int) size.Width; Width += (int) size.Width;
} }
SpaceWidth = g.MeasureString("_", f, 0, StringFormat.GenericTypographic).Width;
} }
map = new Bitmap(Width, Height); map = new Bitmap(Width, Height);
@ -112,7 +111,7 @@ namespace SM.Base.Drawing.Text
{ {
foreach (var keyValuePair in charParams) 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; var normalizedWidth = keyValuePair.Value[0] / Width;
CharParameter parameter; CharParameter parameter;
@ -124,7 +123,7 @@ namespace SM.Base.Drawing.Text
X = (int) keyValuePair.Value[1] 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);
} }
} }
} }

View file

@ -42,12 +42,6 @@ namespace SM.Base.Drawing.Text
/// </summary> /// </summary>
public float Spacing = 1; public float Spacing = 1;
/// <summary>
/// Calculates the actual spacing for the object.
/// </summary>
public float ActualSpacing => Spacing * Font.Spacing;
/// <summary> /// <summary>
/// The font. /// The font.
/// </summary> /// </summary>
@ -120,7 +114,7 @@ namespace SM.Base.Drawing.Text
{ {
if (_text[i] == ' ') if (_text[i] == ' ')
{ {
x += Font.FontSize * ActualSpacing; x += Font.SpaceWidth * Spacing;
continue; continue;
} }
@ -151,7 +145,7 @@ namespace SM.Base.Drawing.Text
new Vector2(parameter.NormalizedWidth, 1), 0) new Vector2(parameter.NormalizedWidth, 1), 0)
}; };
x += parameter.Width * ActualSpacing; x += parameter.Width * Spacing;
_last = parameter; _last = parameter;
} }

View file

@ -1,8 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using OpenTK; using OpenTK;
using SM.Base.Animation; using SM.Base.Animation;
using SM.Base.Controls; using SM.Base.Controls;
using SM.Base.Drawing.Text;
using SM.Base.Time; using SM.Base.Time;
using SM.Base.Window; using SM.Base.Window;
using SM2D; using SM2D;
@ -22,15 +24,20 @@ namespace SM_TEST
private static InterpolationProcess interpolation; private static InterpolationProcess interpolation;
static void Main(string[] args) static void Main(string[] args)
{ {
Font font = new Font(@"C:\Windows\Fonts\Arial.ttf")
{
FontSize = 20,
CharSet = new List<char>(){'H', 'i', 'I', ','}
};
window = new GLWindow(1280, 720, "0ms", WindowFlags.Window, VSyncMode.Off); window = new GLWindow(1280, 720, "0ms", WindowFlags.Window, VSyncMode.Off);
window.ApplySetup(new Window2DSetup()); window.ApplySetup(new Window2DSetup());
window.SetRenderPipeline(new TestRenderPipeline()); window.SetRenderPipeline(new TestRenderPipeline());
window.SetScene(scene = new Scene()); window.SetScene(scene = new Scene());
DrawObject2D testObj = new DrawObject2D(); DrawText text = new DrawText(font, "Hi, HI");
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(text);
scene.Objects.Add(testObj);
window.UpdateFrame += WindowOnUpdateFrame; window.UpdateFrame += WindowOnUpdateFrame;
window.RenderFrame += Window_RenderFrame; window.RenderFrame += Window_RenderFrame;