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

[breaking change] made the one em string be determined automatically for each font

This commit is contained in:
Ellpeck 2019-12-26 19:05:26 +01:00
parent f4db1be464
commit e0b83f6644
3 changed files with 20 additions and 13 deletions

View file

@ -60,8 +60,8 @@ namespace MLEM.Ui.Elements {
var size = base.CalcActualSize(parentArea); var size = base.CalcActualSize(parentArea);
var sc = this.TextScale * this.Scale; var sc = this.TextScale * this.Scale;
this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.Width, sc); this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(this.RegularFont.Value), size.X - this.ScaledPadding.Width, sc);
this.codeLocations = this.text.GetFormattingCodes(); this.codeLocations = this.text.GetFormattingCodes(this.RegularFont.Value);
var textDims = this.RegularFont.Value.MeasureString(this.splitText) * sc; 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); return new Vector2(this.AutoAdjustWidth ? textDims.X + this.ScaledPadding.Width : size.X, textDims.Y + this.ScaledPadding.Height);

View file

@ -1,4 +1,5 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Font;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Formatting { namespace MLEM.Formatting {
@ -30,8 +31,8 @@ namespace MLEM.Formatting {
this.CodeType = Type.Animation; this.CodeType = Type.Animation;
} }
public string GetReplacementString() { public string GetReplacementString(IGenericFont font) {
return this.CodeType == Type.Icon ? TextFormatting.OneEmString : string.Empty; return this.CodeType == Type.Icon ? TextFormatting.GetOneEmString(font) : string.Empty;
} }
public enum Type { public enum Type {

View file

@ -11,13 +11,9 @@ namespace MLEM.Formatting {
public static class TextFormatting { public static class TextFormatting {
public static readonly Dictionary<string, FormattingCode> FormattingCodes = new Dictionary<string, FormattingCode>(); public static readonly Dictionary<string, FormattingCode> FormattingCodes = new Dictionary<string, FormattingCode>();
private static readonly Dictionary<IGenericFont, string> OneEmStrings = new Dictionary<IGenericFont, string>();
private static Regex formatRegex; 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() { static TextFormatting() {
SetFormatIndicators('[', ']'); SetFormatIndicators('[', ']');
@ -47,14 +43,24 @@ namespace MLEM.Formatting {
formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}"); 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 => { return formatRegex.Replace(s, match => {
var code = FromMatch(match); var code = FromMatch(match);
return code != null ? code.GetReplacementString() : string.Empty; return code != null ? code.GetReplacementString(font) : string.Empty;
}); });
} }
public static Dictionary<int, FormattingCode> GetFormattingCodes(this string s) { public static Dictionary<int, FormattingCode> GetFormattingCodes(this string s, IGenericFont font) {
var codes = new Dictionary<int, FormattingCode>(); var codes = new Dictionary<int, FormattingCode>();
var codeLengths = 0; var codeLengths = 0;
foreach (Match match in formatRegex.Matches(s)) { foreach (Match match in formatRegex.Matches(s)) {
@ -62,7 +68,7 @@ namespace MLEM.Formatting {
if (code == null) if (code == null)
continue; continue;
codes[match.Index - codeLengths] = code; codes[match.Index - codeLengths] = code;
codeLengths += match.Length - code.GetReplacementString().Length; codeLengths += match.Length - code.GetReplacementString(font).Length;
} }
return codes; return codes;
} }