From 293602269be2b720947c5ac32ad10fc7ee9f2d19 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 27 Nov 2021 23:04:06 +0100 Subject: [PATCH] prevent unnecessary allocations in GenericFont --- MLEM/Font/GenericFont.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index 77d2884..bce5f7e 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -46,6 +46,15 @@ namespace MLEM.Font { /// public abstract float LineHeight { get; } + private readonly Func identity; + + /// + /// Creates a new generic font with the default settings + /// + public GenericFont() { + this.identity = i => this; + } + /// /// Measures the width of the given character with the default scale for use in . /// Note that this method does not support , and for most generic fonts, which is why should be used even for single characters. @@ -89,7 +98,7 @@ namespace MLEM.Font { /// Whether trailing whitespace should be ignored in the returned size, causing the end of each line to be effectively trimmed /// The size of the string when drawn with this font public Vector2 MeasureString(string text, bool ignoreTrailingSpaces = false) { - return MeasureString(i => this, text, ignoreTrailingSpaces); + return MeasureString(this.identity, text, ignoreTrailingSpaces); } /// @@ -103,7 +112,7 @@ namespace MLEM.Font { /// The characters to add to the end of the string if it is too long /// The truncated string, or the same string if it is shorter than the maximum width public string TruncateString(string text, float width, float scale, bool fromBack = false, string ellipsis = "") { - return TruncateString(i => this, text, width, scale, fromBack, ellipsis); + return TruncateString(this.identity, text, width, scale, fromBack, ellipsis); } /// @@ -129,7 +138,7 @@ namespace MLEM.Font { /// The scale to use for width measurements /// The split string as an enumerable of split sections public IEnumerable SplitStringSeparate(string text, float width, float scale) { - return SplitStringSeparate(i => this, text, width, scale); + return SplitStringSeparate(this.identity, text, width, scale); } internal static Vector2 MeasureString(Func fontFunction, string text, bool ignoreTrailingSpaces) {