1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-09 19:18:44 +02:00

Adjusted GenericStashFont line height calculations to result in the same values as GenericSpriteFont

This commit is contained in:
Ell 2021-07-19 23:10:27 +02:00
parent abac738123
commit a76c14b243
4 changed files with 44 additions and 7 deletions

View file

@ -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

View file

@ -29,9 +29,7 @@ namespace MLEM.Extended.Font {
/// <param name="italic">An italic version of the font</param>
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);
}
}
}
}

View file

@ -11,7 +11,7 @@ with.
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Arial</FontName>
<FontName>Cadman_Roman.otf</FontName>
<!--
Size is a float value, measured in points. Modify this value to change

View file

@ -8,9 +8,11 @@ using Microsoft.Xna.Framework.Input;
using MLEM.Cameras;
using MLEM.Data;
using MLEM.Data.Content;
using MLEM.Extended.Extensions;
using MLEM.Extended.Font;
using MLEM.Extended.Tiled;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Input;
@ -20,6 +22,7 @@ using MLEM.Textures;
using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using MonoGame.Extended;
using MonoGame.Extended.Tiled;
using Group = MLEM.Ui.Elements.Group;
@ -74,9 +77,10 @@ namespace Sandbox {
//var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
//var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular"));
var font = new GenericStashFont(system.GetFont(32));
var spriteFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = font,
TextScale = 0.1F,
TextScale = 0.5F,
PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4)
};
@ -84,6 +88,14 @@ namespace Sandbox {
this.UiSystem.AutoScaleWithScreen = true;
this.UiSystem.GlobalScale = 5;
/*this.OnDraw += (g, time) => {
const string strg = "This is a test string\nto test things\n\nMany things are being tested, like the ability\nfor this font to agree\n\nwith newlines";
this.SpriteBatch.Begin();
spriteFont.DrawString(this.SpriteBatch, strg, new Vector2(600, 100), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
font.DrawString(this.SpriteBatch, strg, new Vector2(600, 100), Color.White, 0, Vector2.Zero, 2, SpriteEffects.None, 0);
this.SpriteBatch.End();
};*/
var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true};
panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10)));
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
@ -129,7 +141,7 @@ namespace Sandbox {
Console.WriteLine("The res is " + res);
var gradient = this.SpriteBatch.GenerateGradientTexture(Color.Green, Color.Red, Color.Blue, Color.Yellow);
this.OnDraw += (game, time) => {
/*this.OnDraw += (game, time) => {
this.SpriteBatch.Begin();
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(640 - 4, 360 - 4, 8, 8), Color.Green);
@ -141,7 +153,7 @@ namespace Sandbox {
this.SpriteBatch.Draw(gradient, new Rectangle(300, 100, 200, 200), Color.White);
this.SpriteBatch.End();
};
};*/
var sc = 4;
var formatter = new TextFormatter();
@ -218,6 +230,16 @@ namespace Sandbox {
CanBeMoused = false
});
}
var par = loadPanel.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is another\ntest string\n\nwith many lines\nand many more!"));
par.OnUpdated = (e, time) => {
GenericFont newFont = Input.IsModifierKeyDown(ModifierKey.Shift) ? spriteFont : font;
if (newFont != par.RegularFont.Value) {
par.TextScaleMultiplier = newFont == font ? 1 : 0.5F;
par.RegularFont = newFont;
par.ForceUpdateArea();
}
};
par.OnDrawn = (e, time, batch, a) => batch.DrawRectangle(e.DisplayArea.ToExtended(), Color.Red);
this.UiSystem.Add("Load", loadGroup);
}