From e0b83f6644153948f6c4d088875d6f2e10bb8749 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 26 Dec 2019 19:05:26 +0100 Subject: [PATCH] [breaking change] made the one em string be determined automatically for each font --- MLEM.Ui/Elements/Paragraph.cs | 4 ++-- MLEM/Formatting/FormattingCode.cs | 5 +++-- MLEM/Formatting/TextFormatting.cs | 24 +++++++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 16e5db3..0bc6364 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -60,8 +60,8 @@ namespace MLEM.Ui.Elements { var size = base.CalcActualSize(parentArea); var sc = this.TextScale * this.Scale; - this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.Width, sc); - this.codeLocations = this.text.GetFormattingCodes(); + this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(this.RegularFont.Value), size.X - this.ScaledPadding.Width, sc); + this.codeLocations = this.text.GetFormattingCodes(this.RegularFont.Value); var textDims = this.RegularFont.Value.MeasureString(this.splitText) * sc; return new Vector2(this.AutoAdjustWidth ? textDims.X + this.ScaledPadding.Width : size.X, textDims.Y + this.ScaledPadding.Height); diff --git a/MLEM/Formatting/FormattingCode.cs b/MLEM/Formatting/FormattingCode.cs index c5a7838..33c60f9 100644 --- a/MLEM/Formatting/FormattingCode.cs +++ b/MLEM/Formatting/FormattingCode.cs @@ -1,4 +1,5 @@ using Microsoft.Xna.Framework; +using MLEM.Font; using MLEM.Textures; namespace MLEM.Formatting { @@ -30,8 +31,8 @@ namespace MLEM.Formatting { this.CodeType = Type.Animation; } - public string GetReplacementString() { - return this.CodeType == Type.Icon ? TextFormatting.OneEmString : string.Empty; + public string GetReplacementString(IGenericFont font) { + return this.CodeType == Type.Icon ? TextFormatting.GetOneEmString(font) : string.Empty; } public enum Type { diff --git a/MLEM/Formatting/TextFormatting.cs b/MLEM/Formatting/TextFormatting.cs index 03bb56e..dcd6d31 100644 --- a/MLEM/Formatting/TextFormatting.cs +++ b/MLEM/Formatting/TextFormatting.cs @@ -11,13 +11,9 @@ namespace MLEM.Formatting { public static class TextFormatting { public static readonly Dictionary FormattingCodes = new Dictionary(); + private static readonly Dictionary OneEmStrings = new Dictionary(); private static Regex formatRegex; - // Unicode suggests that a space should be 1/4em in length, so this string should be - // 1em in length for most fonts. If it's not for the used font, the user can just - // change the value of this string to something that is in fact 1em long - public static string OneEmString = " "; - static TextFormatting() { SetFormatIndicators('[', ']'); @@ -47,14 +43,24 @@ namespace MLEM.Formatting { formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}"); } - public static string RemoveFormatting(this string s) { + public static string GetOneEmString(IGenericFont font) { + if (!OneEmStrings.TryGetValue(font, out var strg)) { + strg = " "; + while (font.MeasureString(strg + ' ').X < font.LineHeight) + strg += ' '; + OneEmStrings[font] = strg; + } + return strg; + } + + public static string RemoveFormatting(this string s, IGenericFont font) { return formatRegex.Replace(s, match => { var code = FromMatch(match); - return code != null ? code.GetReplacementString() : string.Empty; + return code != null ? code.GetReplacementString(font) : string.Empty; }); } - public static Dictionary GetFormattingCodes(this string s) { + public static Dictionary GetFormattingCodes(this string s, IGenericFont font) { var codes = new Dictionary(); var codeLengths = 0; foreach (Match match in formatRegex.Matches(s)) { @@ -62,7 +68,7 @@ namespace MLEM.Formatting { if (code == null) continue; codes[match.Index - codeLengths] = code; - codeLengths += match.Length - code.GetReplacementString().Length; + codeLengths += match.Length - code.GetReplacementString(font).Length; } return codes; }