From 64b9246aa50d5876f3683341e29cc2bb3cf54366 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 20 Jun 2020 12:12:34 +0200 Subject: [PATCH] clean up GenericFont changes --- MLEM.Extended/Font/GenericBitmapFont.cs | 2 +- MLEM.Ui/Style/UntexturedStyle.cs | 2 +- MLEM/Font/GenericFont.cs | 24 ++++++++++++------------ MLEM/Font/GenericSpriteFont.cs | 2 +- MLEM/Formatting/TokenizedString.cs | 5 +++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/MLEM.Extended/Font/GenericBitmapFont.cs b/MLEM.Extended/Font/GenericBitmapFont.cs index 9f3dc1d..d61bff7 100644 --- a/MLEM.Extended/Font/GenericBitmapFont.cs +++ b/MLEM.Extended/Font/GenericBitmapFont.cs @@ -33,7 +33,7 @@ namespace MLEM.Extended.Font { } /// - protected override Vector2 CalcCharSize(char c) { + protected override Vector2 MeasureChar(char c) { var region = this.Font.GetCharacterRegion(c); return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero; } diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index b0a7b72..790a7a8 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -46,7 +46,7 @@ namespace MLEM.Ui.Style { public override GenericFont Italic => this; public override float LineHeight => 1; - protected override Vector2 CalcCharSize(char c) { + protected override Vector2 MeasureChar(char c) { return Vector2.Zero; } diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index 148fd5d..994771e 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -49,7 +49,7 @@ namespace MLEM.Font { public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth); /// - protected abstract Vector2 CalcCharSize(char c); + protected abstract Vector2 MeasureChar(char c); /// /// Draws a string with the given text alignment. @@ -85,23 +85,23 @@ namespace MLEM.Font { this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth); } - /// - public Vector2 MeasureChar(char c) { - return c == OneEmSpace ? new Vector2(this.LineHeight) : this.CalcCharSize(c); - } - /// public Vector2 MeasureString(string text) { var size = Vector2.Zero; var xOffset = 0F; foreach (var c in text) { - if (c == '\n') { - xOffset = 0; - size.Y += this.LineHeight; - continue; + switch (c) { + case '\n': + xOffset = 0; + size.Y += this.LineHeight; + break; + case OneEmSpace: + xOffset += this.LineHeight; + break; + default: + xOffset += this.MeasureChar(c).X; + break; } - - xOffset += this.MeasureChar(c).X; // increase x size if this line is the longest if (xOffset > size.X) size.X = xOffset; diff --git a/MLEM/Font/GenericSpriteFont.cs b/MLEM/Font/GenericSpriteFont.cs index feb8916..15c43a8 100644 --- a/MLEM/Font/GenericSpriteFont.cs +++ b/MLEM/Font/GenericSpriteFont.cs @@ -31,7 +31,7 @@ namespace MLEM.Font { } /// - protected override Vector2 CalcCharSize(char c) { + protected override Vector2 MeasureChar(char c) { return this.Font.MeasureString(c.ToString()); } diff --git a/MLEM/Formatting/TokenizedString.cs b/MLEM/Formatting/TokenizedString.cs index 1f9c8da..5290811 100644 --- a/MLEM/Formatting/TokenizedString.cs +++ b/MLEM/Formatting/TokenizedString.cs @@ -124,8 +124,9 @@ namespace MLEM.Formatting { if (i == 0) token.DrawSelf(time, batch, pos + innerOffset, font, color, scale, depth); - token.DrawCharacter(time, batch, c, c.ToString(), i, pos + innerOffset, drawFont, drawColor, scale, depth); - innerOffset.X += font.MeasureChar(c).X * scale; + var cString = c.ToString(); + token.DrawCharacter(time, batch, c, cString, i, pos + innerOffset, drawFont, drawColor, scale, depth); + innerOffset.X += font.MeasureString(cString).X * scale; } } }