1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

Use FontStashSharp's built-in LineHeight property for GenericStashFont

This commit is contained in:
Ell 2021-08-19 21:43:17 +02:00
parent ff510c54c5
commit 866dad49ab
4 changed files with 7 additions and 29 deletions

View file

@ -38,7 +38,7 @@ Fixes
### MLEM.Extended ### MLEM.Extended
Improvements 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 ### MLEM.Data
Additions Additions

View file

@ -1,4 +1,3 @@
using System.Linq;
using System.Text; using System.Text;
using FontStashSharp; using FontStashSharp;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
@ -19,27 +18,19 @@ namespace MLEM.Extended.Font {
/// <inheritdoc /> /// <inheritdoc />
public override GenericFont Italic { get; } public override GenericFont Italic { get; }
/// <inheritdoc /> /// <inheritdoc />
public override float LineHeight { get; } public override float LineHeight => this.Font.LineHeight;
/// <summary> /// <summary>
/// Creates a new generic font using <see cref="SpriteFontBase"/>. /// Creates a new generic font using <see cref="SpriteFontBase"/>.
/// Optionally, a bold and italic version of the font can be supplied. /// Optionally, a bold and italic version of the font can be supplied.
/// </summary> /// </summary>
/// <remarks>
/// <see cref="DynamicSpriteFont"/> doesn't expose a text-independent line height (https://github.com/rds1983/FontStashSharp/blob/main/src/FontStashSharp/DynamicSpriteFont.cs#L130).
/// Since <see cref="GenericFont"/> exposes <see cref="LineHeight"/>, there is somewhat of an incompatibility between the two.
/// Because of this, <see cref="GenericStashFont"/> 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 <paramref name="lineHeight"/>, overriding the default heuristic.
/// </remarks>
/// <param name="font">The font to wrap</param> /// <param name="font">The font to wrap</param>
/// <param name="bold">A bold version of the font</param> /// <param name="bold">A bold version of the font</param>
/// <param name="italic">An italic version of the font</param> /// <param name="italic">An italic version of the font</param>
/// <param name="lineHeight">The line height that should be used for <see cref="LineHeight"/> instead of the heuristic described in the remarks</param> public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) {
public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null, float? lineHeight = null) {
this.Font = font; this.Font = font;
this.LineHeight = lineHeight ?? CalculateLineHeight(font); this.Bold = bold != null ? new GenericStashFont(bold) : this;
this.Bold = bold != null ? new GenericStashFont(bold, null, null, lineHeight) : this; this.Italic = italic != null ? new GenericStashFont(italic) : this;
this.Italic = italic != null ? new GenericStashFont(italic, null, null, lineHeight) : this;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -57,18 +48,5 @@ namespace MLEM.Extended.Font {
return this.Font.MeasureString(c.ToCachedString()).X; 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();
}
}
} }
} }

View file

@ -24,7 +24,7 @@
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0"> <PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.5"> <PackageReference Include="FontStashSharp.MonoGame" Version="1.0.3">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641"> <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">

View file

@ -18,7 +18,7 @@
<PackageReference Include="MonoGame.Extended.Content.Pipeline" Version="3.8.0" /> <PackageReference Include="MonoGame.Extended.Content.Pipeline" Version="3.8.0" />
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0" /> <PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" /> <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.5" /> <PackageReference Include="FontStashSharp.MonoGame" Version="1.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>