From 866dad49abd41fe76f3b2da8faffdffcd14f93b6 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 19 Aug 2021 21:43:17 +0200 Subject: [PATCH] Use FontStashSharp's built-in LineHeight property for GenericStashFont --- CHANGELOG.md | 2 +- MLEM.Extended/Font/GenericStashFont.cs | 30 ++++---------------------- MLEM.Extended/MLEM.Extended.csproj | 2 +- Sandbox/Sandbox.csproj | 2 +- 4 files changed, 7 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00357fe..d952736 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,7 @@ Fixes ### MLEM.Extended Improvements -- Adjusted GenericStashFont line height calculations to result in values closer to GenericSpriteFont and added a constructor parameter to set a custom line height +- Use FontStashSharp's built-in LineHeight property for GenericStashFont ### MLEM.Data Additions diff --git a/MLEM.Extended/Font/GenericStashFont.cs b/MLEM.Extended/Font/GenericStashFont.cs index b3e6b1a..42d9d41 100644 --- a/MLEM.Extended/Font/GenericStashFont.cs +++ b/MLEM.Extended/Font/GenericStashFont.cs @@ -1,4 +1,3 @@ -using System.Linq; using System.Text; using FontStashSharp; using Microsoft.Xna.Framework; @@ -19,27 +18,19 @@ namespace MLEM.Extended.Font { /// public override GenericFont Italic { get; } /// - public override float LineHeight { get; } + public override float LineHeight => this.Font.LineHeight; /// /// Creates a new generic font using . /// Optionally, a bold and italic version of the font can be supplied. /// - /// - /// doesn't expose a text-independent line height (https://github.com/rds1983/FontStashSharp/blob/main/src/FontStashSharp/DynamicSpriteFont.cs#L130). - /// Since exposes , there is somewhat of an incompatibility between the two. - /// Because of this, uses a heuristic to determine a text-independent line height based on the tallest character out of a set of predetermined characters (spaces, numbers and uppercase and lowercase A through Z). - /// Because this heuristic is just that, and because it excludes non-latin characters, the desired line height can be specified using , overriding the default heuristic. - /// /// The font to wrap /// A bold version of the font /// An italic version of the font - /// The line height that should be used for instead of the heuristic described in the remarks - public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null, float? lineHeight = null) { + public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) { this.Font = font; - this.LineHeight = lineHeight ?? CalculateLineHeight(font); - this.Bold = bold != null ? new GenericStashFont(bold, null, null, lineHeight) : this; - this.Italic = italic != null ? new GenericStashFont(italic, null, null, lineHeight) : this; + this.Bold = bold != null ? new GenericStashFont(bold) : this; + this.Italic = italic != null ? new GenericStashFont(italic) : this; } /// @@ -57,18 +48,5 @@ namespace MLEM.Extended.Font { return this.Font.MeasureString(c.ToCachedString()).X; } - private static float CalculateLineHeight(SpriteFontBase font) { - if (font is StaticSpriteFont s) { - // this is the same calculation used internally by StaticSpriteFont - return s.FontSize + s.LineSpacing; - } else { - // use a heuristic to determine the text-independent line heights as described in the constructor remarks - return new[] {' ', '\n', OneEmSpace, Zwsp, Nbsp} - .Concat(Enumerable.Range('a', 'z' - 'a' + 1).SelectMany(c => new[] {(char) c, char.ToUpper((char) c)})) - .Concat(Enumerable.Range('0', '9' - '0' + 1).Select(c => (char) c)) - .Select(c => font.MeasureString(c.ToString()).Y).Max(); - } - } - } } \ No newline at end of file diff --git a/MLEM.Extended/MLEM.Extended.csproj b/MLEM.Extended/MLEM.Extended.csproj index 430df91..8039c4a 100644 --- a/MLEM.Extended/MLEM.Extended.csproj +++ b/MLEM.Extended/MLEM.Extended.csproj @@ -24,7 +24,7 @@ all - + all diff --git a/Sandbox/Sandbox.csproj b/Sandbox/Sandbox.csproj index d35b0cc..ee984be 100644 --- a/Sandbox/Sandbox.csproj +++ b/Sandbox/Sandbox.csproj @@ -18,7 +18,7 @@ - +