From a76c14b243ae848d1487b333752c2e73aea2de32 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 19 Jul 2021 23:10:27 +0200 Subject: [PATCH] Adjusted GenericStashFont line height calculations to result in the same values as GenericSpriteFont --- CHANGELOG.md | 4 ++++ MLEM.Extended/Font/GenericStashFont.cs | 17 +++++++++++--- Sandbox/Content/Fonts/TestFont.spritefont | 2 +- Sandbox/GameImpl.cs | 28 ++++++++++++++++++++--- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b27cc4e..7297e1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,10 @@ Improvements Fixes - Fixed a crash if a paragraph has a link formatting code, but no font +### MLEM.Extended +Improvements +- Adjusted GenericStashFont line height calculations to result in the same values as GenericSpriteFont + ### MLEM.Data Additions - Added the ability to specify a coordinate offset in data texture atlases diff --git a/MLEM.Extended/Font/GenericStashFont.cs b/MLEM.Extended/Font/GenericStashFont.cs index f4c3bc0..1b497ee 100644 --- a/MLEM.Extended/Font/GenericStashFont.cs +++ b/MLEM.Extended/Font/GenericStashFont.cs @@ -29,9 +29,7 @@ namespace MLEM.Extended.Font { /// An italic version of the font public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) { this.Font = font; - // SpriteFontBase provides no line height, so we measure the height of a new line for most fonts - // This doesn't work with static sprite fonts, but their size is always the one we calculate here - this.LineHeight = font is StaticSpriteFont s ? s.FontSize + s.LineSpacing : font.MeasureString("\n").Y; + this.LineHeight = CalculateLineHeight(font); this.Bold = bold != null ? new GenericStashFont(bold) : this; this.Italic = italic != null ? new GenericStashFont(italic) : this; } @@ -51,5 +49,18 @@ namespace MLEM.Extended.Font { return this.Font.MeasureString(c.ToCachedString()); } + 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 { + // Y (min y) just stores the glyph's Y offset, whereas Y2 (max y) stores the glyph's height + // since we technically want line spacing rather than line height, we calculate it like this + var bounds = new Bounds(); + font.TextBounds(" ", Vector2.Zero, ref bounds); + return bounds.Y2 + (bounds.Y2 - bounds.Y); + } + } + } } \ No newline at end of file diff --git a/Sandbox/Content/Fonts/TestFont.spritefont b/Sandbox/Content/Fonts/TestFont.spritefont index 54ab9ac..b44bd5d 100644 --- a/Sandbox/Content/Fonts/TestFont.spritefont +++ b/Sandbox/Content/Fonts/TestFont.spritefont @@ -11,7 +11,7 @@ with. - Arial + Cadman_Roman.otf