diff --git a/CHANGELOG.md b/CHANGELOG.md index b667234..dcbe089 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Improvements - Multi-target net452, making MLEM compatible with MonoGame for consoles - Allow retrieving the cost of a calculated path when using AStar - **Drastically improved StaticSpriteBatch batching performance** +- **Made GenericFont and TokenizedString support UTF-32 characters like emoji** Fixes - Fixed TokenizedString handling trailing spaces incorrectly in the last line of non-left aligned text @@ -68,6 +69,7 @@ Fixes ## MLEM.Extended Improvements - Multi-target net452, making MLEM compatible with MonoGame for consoles +- **Made GenericBitmapFont and GenericStashFont support UTF-32 characters like emoji** ## MLEM.Startup Improvements diff --git a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs index 39f2d1c..87877f2 100644 --- a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs +++ b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Runtime.Serialization; using MLEM.Misc; using Newtonsoft.Json; diff --git a/MLEM.Extended/Font/GenericBitmapFont.cs b/MLEM.Extended/Font/GenericBitmapFont.cs index 50595dd..d1ff2c4 100644 --- a/MLEM.Extended/Font/GenericBitmapFont.cs +++ b/MLEM.Extended/Font/GenericBitmapFont.cs @@ -32,14 +32,14 @@ namespace MLEM.Extended.Font { } /// - protected override float MeasureChar(char c) { - var region = this.Font.GetCharacterRegion(c); + protected override float MeasureCharacter(int codePoint) { + var region = this.Font.GetCharacterRegion(codePoint); return region != null ? new Vector2(region.XAdvance, region.Height).X : 0; } /// - protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) { - batch.DrawString(this.Font, cString, position, color, rotation, Vector2.Zero, scale, effects, layerDepth); + protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) { + batch.DrawString(this.Font, character, position, color, rotation, Vector2.Zero, scale, effects, layerDepth); } } diff --git a/MLEM.Extended/Font/GenericStashFont.cs b/MLEM.Extended/Font/GenericStashFont.cs index cb7f741..b84e5e3 100644 --- a/MLEM.Extended/Font/GenericStashFont.cs +++ b/MLEM.Extended/Font/GenericStashFont.cs @@ -1,7 +1,6 @@ using FontStashSharp; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Font; namespace MLEM.Extended.Font { @@ -33,13 +32,13 @@ namespace MLEM.Extended.Font { } /// - protected override float MeasureChar(char c) { - return this.Font.MeasureString(c.ToCachedString()).X; + protected override float MeasureCharacter(int codePoint) { + return this.Font.MeasureString(char.ConvertFromUtf32(codePoint)).X; } /// - protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) { - this.Font.DrawText(batch, cString, position, color, scale, rotation, Vector2.Zero, layerDepth); + protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) { + this.Font.DrawText(batch, character, position, color, scale, rotation, Vector2.Zero, layerDepth); } } diff --git a/MLEM/Extensions/CharExtensions.cs b/MLEM/Extensions/CharExtensions.cs index 137aaf3..4b550b8 100644 --- a/MLEM/Extensions/CharExtensions.cs +++ b/MLEM/Extensions/CharExtensions.cs @@ -1,9 +1,11 @@ +using System; using System.Collections.Generic; namespace MLEM.Extensions { /// /// A set of extensions for dealing with /// + [Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching.")] public static class CharExtensions { private static readonly Dictionary Cache = new Dictionary(); @@ -14,6 +16,7 @@ namespace MLEM.Extensions { /// /// The character to turn into a string /// A string representing the character + [Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching.")] public static string ToCachedString(this char c) { if (!CharExtensions.Cache.TryGetValue(c, out var ret)) { ret = c.ToString(); diff --git a/MLEM/Font/CodePointSource.cs b/MLEM/Font/CodePointSource.cs new file mode 100644 index 0000000..93be007 --- /dev/null +++ b/MLEM/Font/CodePointSource.cs @@ -0,0 +1,84 @@ +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace MLEM.Font { + /// + /// A code point source is a wrapper around a or that allows retrieving UTF-32 code points at a given index using . Additionally, it allows enumerating every code point in the underlying or . + /// + public readonly struct CodePointSource : IEnumerable { + + private readonly string strg; + private readonly StringBuilder builder; + private char this[int index] => this.strg?[index] ?? this.builder[index]; + + /// + /// The length of this code point, in characters. + /// Note that this is not representative of the amount of code points in this source. + /// + public int Length => this.strg?.Length ?? this.builder.Length; + + /// + /// Creates a new code point source from the given . + /// + /// The whose code points to inspect. + public CodePointSource(string strg) { + this.strg = strg; + this.builder = null; + } + + /// + /// Creates a new code point source from the given . + /// + /// The whose code points to inspect. + public CodePointSource(StringBuilder builder) { + this.strg = null; + this.builder = builder; + } + + /// + /// Returns the code point at the given in this code point source's underlying string, where the index is measured in characters and not code points. + /// The resulting code point will either be a single cast to an , at which point the returned length will be 1, or a UTF-32 character made up of two values, at which point the returned length will be 2. + /// + /// The index at which to return the code point, which is measured in characters. + /// Whether the represents a low surrogate. If this is , the represents a high surrogate and the low surrogate will be looked for in the following character. If this is , the represents a low surrogate and the high surrogate will be looked for in the previous character. + /// + public (int CodePoint, int Length) GetCodePoint(int index, bool indexLowSurrogate = false) { + var curr = this[index]; + if (indexLowSurrogate) { + if (index > 0) { + var high = this[index - 1]; + if (char.IsSurrogatePair(high, curr)) + return (char.ConvertToUtf32(high, curr), 2); + } + } else { + if (index < this.Length - 1) { + var low = this[index + 1]; + if (char.IsSurrogatePair(curr, low)) + return (char.ConvertToUtf32(curr, low), 2); + } + } + return (curr, 1); + } + + /// Returns an enumerator that iterates through the collection. + /// A that can be used to iterate through the collection. + /// 1 + public IEnumerator GetEnumerator() { + var index = 0; + while (index < this.Length) { + var (codePoint, length) = this.GetCodePoint(index); + yield return codePoint; + index += length; + } + } + + /// Returns an enumerator that iterates through a collection. + /// An object that can be used to iterate through the collection. + /// 2 + IEnumerator IEnumerable.GetEnumerator() { + return this.GetEnumerator(); + } + + } +} diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index 28c4748..626d2d6 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Misc; namespace MLEM.Font { @@ -50,35 +49,36 @@ namespace MLEM.Font { public abstract float LineHeight { get; } /// - /// Measures the width of the given character with the default scale for use in . + /// Measures the width of the given code point 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. /// - /// The character whose width to calculate + /// The code point whose width to calculate /// The width of the given character with the default scale - protected abstract float MeasureChar(char c); + protected abstract float MeasureCharacter(int codePoint); /// - /// Draws the given character with the given data for use in . + /// Draws the given code point with the given data for use in . /// Note that this method is only called internally. /// /// The sprite batch to draw with. - /// A string representation of the character which will be drawn. + /// The code point which will be drawn. + /// A string representation of the character which will be drawn. /// The drawing location on screen. /// A color mask. /// A rotation of this character. /// A scaling of this character. /// Modificators for drawing. Can be combined. /// A depth of the layer of this character. - protected abstract void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth); + protected abstract void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth); /// public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { - this.DrawString(batch, new CharSource(text), position, color, rotation, origin, scale, effects, layerDepth); + this.DrawString(batch, new CodePointSource(text), position, color, rotation, origin, scale, effects, layerDepth); } /// public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { - this.DrawString(batch, new CharSource(text), position, color, rotation, origin, scale, effects, layerDepth); + this.DrawString(batch, new CodePointSource(text), position, color, rotation, origin, scale, effects, layerDepth); } /// @@ -103,19 +103,19 @@ namespace MLEM.Font { /// /// Measures the width of the given string when drawn with this font's underlying font. - /// This method uses internally to calculate the size of known characters and calculates additional characters like , and . + /// This method uses internally to calculate the size of known characters and calculates additional characters like , and . /// If the text contains newline characters (\n), the size returned will represent a rectangle that encompasses the width of the longest line and the string's full height. /// /// The text whose size to calculate /// 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 this.MeasureString(new CharSource(text), ignoreTrailingSpaces, null); + return this.MeasureString(new CodePointSource(text), ignoreTrailingSpaces, null); } /// public Vector2 MeasureString(StringBuilder text, bool ignoreTrailingSpaces = false) { - return this.MeasureString(new CharSource(text), ignoreTrailingSpaces, null); + return this.MeasureString(new CodePointSource(text), ignoreTrailingSpaces, null); } /// @@ -129,12 +129,12 @@ 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 this.TruncateString(new CharSource(text), width, scale, fromBack, ellipsis, null).ToString(); + return this.TruncateString(new CodePointSource(text), width, scale, fromBack, ellipsis, null).ToString(); } /// public StringBuilder TruncateString(StringBuilder text, float width, float scale, bool fromBack = false, string ellipsis = "") { - return this.TruncateString(new CharSource(text), width, scale, fromBack, ellipsis, null); + return this.TruncateString(new CodePointSource(text), width, scale, fromBack, ellipsis, null); } /// @@ -165,22 +165,24 @@ 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 this.SplitStringSeparate(new CharSource(text), width, scale, null); + return this.SplitStringSeparate(new CodePointSource(text), width, scale, null); } /// public IEnumerable SplitStringSeparate(StringBuilder text, float width, float scale) { - return this.SplitStringSeparate(new CharSource(text), width, scale, null); + return this.SplitStringSeparate(new CodePointSource(text), width, scale, null); } - internal Vector2 MeasureString(CharSource text, bool ignoreTrailingSpaces, Func fontFunction) { + internal Vector2 MeasureString(CodePointSource text, bool ignoreTrailingSpaces, Func fontFunction) { var size = Vector2.Zero; if (text.Length <= 0) return size; var xOffset = 0F; - for (var i = 0; i < text.Length; i++) { - var font = fontFunction?.Invoke(i) ?? this; - switch (text[i]) { + var index = 0; + while (index < text.Length) { + var font = fontFunction?.Invoke(index) ?? this; + var (codePoint, length) = text.GetCodePoint(index); + switch (codePoint) { case '\n': xOffset = 0; size.Y += this.LineHeight; @@ -189,74 +191,80 @@ namespace MLEM.Font { xOffset += this.LineHeight; break; case GenericFont.Nbsp: - xOffset += font.MeasureChar(' '); + xOffset += font.MeasureCharacter(' '); break; case GenericFont.Zwsp: // don't add width for a zero-width space break; case ' ': - if (ignoreTrailingSpaces && GenericFont.IsTrailingSpace(text, i)) { + if (ignoreTrailingSpaces && GenericFont.IsTrailingSpace(text, index)) { // if this is a trailing space, we can skip remaining spaces too - i = text.Length - 1; + index = text.Length - 1; break; } - xOffset += font.MeasureChar(' '); + xOffset += font.MeasureCharacter(' '); break; default: - xOffset += font.MeasureChar(text[i]); + xOffset += font.MeasureCharacter(codePoint); break; } // increase x size if this line is the longest if (xOffset > size.X) size.X = xOffset; + index += length; } // include the last line's height too! size.Y += this.LineHeight; return size; } - internal StringBuilder TruncateString(CharSource text, float width, float scale, bool fromBack, string ellipsis, Func fontFunction) { + internal StringBuilder TruncateString(CodePointSource text, float width, float scale, bool fromBack, string ellipsis, Func fontFunction) { var total = new StringBuilder(); - for (var i = 0; i < text.Length; i++) { + var index = 0; + while (index < text.Length) { + var innerIndex = fromBack ? text.Length - 1 - index : index; + var (codePoint, length) = text.GetCodePoint(innerIndex, fromBack); if (fromBack) { - total.Insert(0, text[text.Length - 1 - i]); + total.Insert(0, char.ConvertFromUtf32(codePoint)); } else { - total.Append(text[i]); + total.Append(char.ConvertFromUtf32(codePoint)); } - var font = fontFunction?.Invoke(i) ?? this; - if (font.MeasureString(total + ellipsis).X * scale >= width) { + if (this.MeasureString(new CodePointSource(total + ellipsis), false, fontFunction).X * scale >= width) { if (fromBack) { - return total.Remove(0, 1).Insert(0, ellipsis); + return total.Remove(0, length).Insert(0, ellipsis); } else { - return total.Remove(total.Length - 1, 1).Append(ellipsis); + return total.Remove(total.Length - length, length).Append(ellipsis); } } + index += length; } return total; } - internal IEnumerable SplitStringSeparate(CharSource text, float width, float scale, Func fontFunction) { + internal IEnumerable SplitStringSeparate(CodePointSource text, float width, float scale, Func fontFunction) { var currWidth = 0F; var lastSpaceIndex = -1; var widthSinceLastSpace = 0F; var curr = new StringBuilder(); - for (var i = 0; i < text.Length; i++) { - var c = text[i]; - if (c == '\n') { + var index = 0; + while (index < text.Length) { + var (codePoint, length) = text.GetCodePoint(index); + if (codePoint == '\n') { // fake split at pre-defined new lines - curr.Append(c); + curr.Append('\n'); lastSpaceIndex = -1; widthSinceLastSpace = 0; currWidth = 0; } else { - var font = fontFunction?.Invoke(i) ?? this; - var cWidth = font.MeasureString(c.ToCachedString()).X * scale; - if (c == ' ' || c == GenericFont.Emsp || c == GenericFont.Zwsp) { + var font = fontFunction?.Invoke(index) ?? this; + var character = char.ConvertFromUtf32(codePoint); + var charWidth = font.MeasureString(character).X * scale; + if (codePoint == ' ' || codePoint == GenericFont.Emsp || codePoint == GenericFont.Zwsp) { // remember the location of this (breaking!) space lastSpaceIndex = curr.Length; widthSinceLastSpace = 0; - } else if (currWidth + cWidth >= width) { + } else if (currWidth + charWidth >= width) { // check if this line contains a space if (lastSpaceIndex < 0) { // if there is no last space, the word is longer than a line so we split here @@ -275,16 +283,17 @@ namespace MLEM.Font { } // add current character - currWidth += cWidth; - widthSinceLastSpace += cWidth; - curr.Append(c); + currWidth += charWidth; + widthSinceLastSpace += charWidth; + curr.Append(character); } + index += length; } if (curr.Length > 0) yield return curr.ToString(); } - private void DrawString(SpriteBatch batch, CharSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { + private void DrawString(SpriteBatch batch, CodePointSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { var (flipX, flipY) = (0F, 0F); var flippedV = (effects & SpriteEffects.FlipVertically) != 0; var flippedH = (effects & SpriteEffects.FlipHorizontally) != 0; @@ -318,56 +327,39 @@ namespace MLEM.Font { } var offset = Vector2.Zero; - for (var i = 0; i < text.Length; i++) { - var c = text[i]; - if (c == '\n') { + var index = 0; + while (index < text.Length) { + var (codePoint, length) = text.GetCodePoint(index); + if (codePoint == '\n') { offset.X = 0; offset.Y += this.LineHeight; - continue; + } else { + var character = char.ConvertFromUtf32(codePoint); + var charSize = this.MeasureString(character); + + var charPos = offset; + if (flippedH) + charPos.X += charSize.X; + if (flippedV) + charPos.Y += charSize.Y - this.LineHeight; + Vector2.Transform(ref charPos, ref trans, out charPos); + + this.DrawCharacter(batch, codePoint, character, charPos, color, rotation, scale, effects, layerDepth); + offset.X += charSize.X; } - - var cString = c.ToCachedString(); - var cSize = this.MeasureString(cString); - - var charPos = offset; - if (flippedH) - charPos.X += cSize.X; - if (flippedV) - charPos.Y += cSize.Y - this.LineHeight; - Vector2.Transform(ref charPos, ref trans, out charPos); - - this.DrawChar(batch, cString, charPos, color, rotation, scale, effects, layerDepth); - offset.X += cSize.X; + index += length; } } - private static bool IsTrailingSpace(CharSource s, int index) { - for (var i = index + 1; i < s.Length; i++) { - if (s[i] != ' ') + private static bool IsTrailingSpace(CodePointSource s, int index) { + while (index < s.Length) { + var (codePoint, length) = s.GetCodePoint(index); + if (codePoint != ' ') return false; + index += length; } return true; } - internal readonly struct CharSource { - - private readonly string strg; - private readonly StringBuilder builder; - - public int Length => this.strg?.Length ?? this.builder.Length; - public char this[int index] => this.strg?[index] ?? this.builder[index]; - - public CharSource(string strg) { - this.strg = strg; - this.builder = null; - } - - public CharSource(StringBuilder builder) { - this.strg = null; - this.builder = builder; - } - - } - } } diff --git a/MLEM/Font/GenericSpriteFont.cs b/MLEM/Font/GenericSpriteFont.cs index abfa44d..7d50f7e 100644 --- a/MLEM/Font/GenericSpriteFont.cs +++ b/MLEM/Font/GenericSpriteFont.cs @@ -1,6 +1,5 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; #if !FNA using System.Linq; @@ -35,13 +34,13 @@ namespace MLEM.Font { } /// - protected override float MeasureChar(char c) { - return this.Font.MeasureString(c.ToCachedString()).X; + protected override float MeasureCharacter(int codePoint) { + return this.Font.MeasureString(char.ConvertFromUtf32(codePoint)).X; } /// - protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) { - batch.DrawString(this.Font, cString, position, color, rotation, Vector2.Zero, scale, effects, layerDepth); + protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) { + batch.DrawString(this.Font, character, position, color, rotation, Vector2.Zero, scale, effects, layerDepth); } private static SpriteFont SetDefaults(SpriteFont font) { diff --git a/MLEM/Formatting/Codes/Code.cs b/MLEM/Formatting/Codes/Code.cs index 2ca18ce..261c703 100644 --- a/MLEM/Formatting/Codes/Code.cs +++ b/MLEM/Formatting/Codes/Code.cs @@ -85,7 +85,7 @@ namespace MLEM.Formatting.Codes { } /// - public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { return false; } diff --git a/MLEM/Formatting/Codes/ImageCode.cs b/MLEM/Formatting/Codes/ImageCode.cs index 66c600b..6e8dffb 100644 --- a/MLEM/Formatting/Codes/ImageCode.cs +++ b/MLEM/Formatting/Codes/ImageCode.cs @@ -27,7 +27,7 @@ namespace MLEM.Formatting.Codes { /// public override string GetReplacementString(GenericFont font) { - return GenericFont.Emsp.ToCachedString(); + return GenericFont.Emsp.ToString(); } /// @@ -42,7 +42,7 @@ namespace MLEM.Formatting.Codes { } /// - public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { // we don't want to draw the first (space) character (in case it is set to a missing character in FNA) return indexInToken == 0; } diff --git a/MLEM/Formatting/Codes/LinkCode.cs b/MLEM/Formatting/Codes/LinkCode.cs index 4e1a0b7..e8118ac 100644 --- a/MLEM/Formatting/Codes/LinkCode.cs +++ b/MLEM/Formatting/Codes/LinkCode.cs @@ -35,9 +35,9 @@ namespace MLEM.Formatting.Codes { } /// - public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { // since we inherit from UnderlineCode, we can just call base if selected - return this.IsSelected() && base.DrawCharacter(time, batch, c, cString, token, indexInToken, ref pos, font, ref color, ref scale, depth); + return this.IsSelected() && base.DrawCharacter(time, batch, codePoint, character, token, indexInToken, ref pos, font, ref color, ref scale, depth); } } diff --git a/MLEM/Formatting/Codes/ShadowCode.cs b/MLEM/Formatting/Codes/ShadowCode.cs index 942bf1a..217af03 100644 --- a/MLEM/Formatting/Codes/ShadowCode.cs +++ b/MLEM/Formatting/Codes/ShadowCode.cs @@ -18,8 +18,8 @@ namespace MLEM.Formatting.Codes { } /// - public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { - font.DrawString(batch, cString, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth); + public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + font.DrawString(batch, character, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth); // we return false since we still want regular drawing to occur return false; } diff --git a/MLEM/Formatting/Codes/UnderlineCode.cs b/MLEM/Formatting/Codes/UnderlineCode.cs index 4d20a54..c5fa08f 100644 --- a/MLEM/Formatting/Codes/UnderlineCode.cs +++ b/MLEM/Formatting/Codes/UnderlineCode.cs @@ -19,11 +19,11 @@ namespace MLEM.Formatting.Codes { } /// - public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { // don't underline spaces at the end of lines - if (c == ' ' && token.DisplayString.Length > indexInToken + 1 && token.DisplayString[indexInToken + 1] == '\n') + if (codePoint == ' ' && token.DisplayString.Length > indexInToken + 1 && token.DisplayString[indexInToken + 1] == '\n') return false; - var size = font.MeasureString(cString) * scale; + var size = font.MeasureString(character) * scale; var t = size.Y * this.thickness; batch.Draw(batch.GetBlankTexture(), new RectangleF(pos.X, pos.Y + this.yOffset * size.Y - t, size.X, t), color); return false; diff --git a/MLEM/Formatting/Codes/WobblyCode.cs b/MLEM/Formatting/Codes/WobblyCode.cs index 8c0ffad..4d02d21 100644 --- a/MLEM/Formatting/Codes/WobblyCode.cs +++ b/MLEM/Formatting/Codes/WobblyCode.cs @@ -28,7 +28,7 @@ namespace MLEM.Formatting.Codes { } /// - public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { var offset = new Vector2(0, (float) Math.Sin(token.Index + indexInToken + this.TimeIntoAnimation.TotalSeconds * this.modifier) * font.LineHeight * this.heightModifier * scale); pos += offset; // we return false since we still want regular drawing to occur, we just changed the position diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index 6899197..cc1e9c6 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -59,8 +59,8 @@ namespace MLEM.Formatting { this.Codes.Add(new Regex(@""), (f, m, r) => new SimpleEndCode(m, r, m.Groups[1].Value)); // macros - this.Macros.Add(new Regex("~"), (f, m, r) => GenericFont.Nbsp.ToCachedString()); - this.Macros.Add(new Regex(""), (f, m, r) => '\n'.ToCachedString()); + this.Macros.Add(new Regex("~"), (f, m, r) => GenericFont.Nbsp.ToString()); + this.Macros.Add(new Regex(""), (f, m, r) => '\n'.ToString()); } /// diff --git a/MLEM/Formatting/Token.cs b/MLEM/Formatting/Token.cs index f468088..ca2409a 100644 --- a/MLEM/Formatting/Token.cs +++ b/MLEM/Formatting/Token.cs @@ -97,26 +97,26 @@ namespace MLEM.Formatting { } /// - /// Draws a given character using this token's formatting options. + /// Draws a given code point using this token's formatting options. /// /// The time /// The sprite batch to use - /// The character to draw - /// A single-character string that contains the character to draw + /// The code point of the character to draw + /// The string representation of the character to draw /// The index within this token that the character is at /// The position to draw the token at /// The font to use to draw /// The color to draw with /// The scale to draw at /// The depth to draw at - public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) { + public void DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) { foreach (var code in this.AppliedCodes) { - if (code.DrawCharacter(time, batch, c, cString, this, indexInToken, ref pos, font, ref color, ref scale, depth)) + if (code.DrawCharacter(time, batch, codePoint, character, this, indexInToken, ref pos, font, ref color, ref scale, depth)) return; } // if no code drew, we have to do it ourselves - font.DrawString(batch, cString, pos, color, 0, Vector2.Zero, scale, SpriteEffects.None, depth); + font.DrawString(batch, character, pos, color, 0, Vector2.Zero, scale, SpriteEffects.None, depth); } /// diff --git a/MLEM/Formatting/TokenizedString.cs b/MLEM/Formatting/TokenizedString.cs index fb70db9..c4b1a4e 100644 --- a/MLEM/Formatting/TokenizedString.cs +++ b/MLEM/Formatting/TokenizedString.cs @@ -4,11 +4,9 @@ using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Font; using MLEM.Formatting.Codes; using MLEM.Misc; -using static MLEM.Font.GenericFont; namespace MLEM.Formatting { /// @@ -65,7 +63,7 @@ namespace MLEM.Formatting { /// The text alignment that should be used for width calculations public void Split(GenericFont font, float width, float scale, TextAlignment alignment = TextAlignment.Left) { // a split string has the same character count as the input string but with newline characters added - this.modifiedString = string.Join("\n", font.SplitStringSeparate(new CharSource(this.String), width, scale, i => this.GetFontForIndex(font, i))); + this.modifiedString = string.Join("\n", font.SplitStringSeparate(new CodePointSource(this.String), width, scale, i => this.GetFontForIndex(font, i))); this.StoreModifiedSubstrings(font, alignment); } @@ -80,7 +78,7 @@ namespace MLEM.Formatting { /// The characters to add to the end of the string if it is too long /// The text alignment that should be used for width calculations public void Truncate(GenericFont font, float width, float scale, string ellipsis = "", TextAlignment alignment = TextAlignment.Left) { - this.modifiedString = font.TruncateString(new CharSource(this.String), width, scale, false, ellipsis, i => this.GetFontForIndex(font, i)).ToString(); + this.modifiedString = font.TruncateString(new CodePointSource(this.String), width, scale, false, ellipsis, i => this.GetFontForIndex(font, i)).ToString(); this.StoreModifiedSubstrings(font, alignment); } @@ -122,7 +120,7 @@ namespace MLEM.Formatting { /// public Vector2 Measure(GenericFont font) { - return font.MeasureString(new CharSource(this.DisplayString), false, i => this.GetFontForIndex(font, i)); + return font.MeasureString(new CodePointSource(this.DisplayString), false, i => this.GetFontForIndex(font, i)); } /// @@ -162,14 +160,18 @@ namespace MLEM.Formatting { var indexInToken = 0; for (var l = 0; l < token.SplitDisplayString.Length; l++) { - foreach (var c in token.SplitDisplayString[l]) { - var cString = c.ToCachedString(); + var charIndex = 0; + var line = new CodePointSource(token.SplitDisplayString[l]); + while (charIndex < line.Length) { + var (codePoint, length) = line.GetCodePoint(charIndex); + var character = char.ConvertFromUtf32(codePoint); if (indexInToken == 0) token.DrawSelf(time, batch, pos + innerOffset, drawFont, color, scale, depth); - token.DrawCharacter(time, batch, c, cString, indexInToken, pos + innerOffset, drawFont, drawColor, scale, depth); + token.DrawCharacter(time, batch, codePoint, character, indexInToken, pos + innerOffset, drawFont, drawColor, scale, depth); - innerOffset.X += drawFont.MeasureString(cString).X * scale; + innerOffset.X += drawFont.MeasureString(character).X * scale; + charIndex += length; indexInToken++; } diff --git a/MLEM/Input/TextInput.cs b/MLEM/Input/TextInput.cs index 228d51d..443c230 100644 --- a/MLEM/Input/TextInput.cs +++ b/MLEM/Input/TextInput.cs @@ -417,9 +417,10 @@ namespace MLEM.Input { private bool FilterText(ref string text, bool removeMismatching) { if (removeMismatching) { var result = new StringBuilder(); - foreach (var c in text) { - if (this.InputRule(this, c.ToCachedString())) - result.Append(c); + foreach (var codePoint in new CodePointSource(text)) { + var character = char.ConvertFromUtf32(codePoint); + if (this.InputRule(this, character)) + result.Append(character); } text = result.ToString(); } else if (!this.InputRule(this, text)) diff --git a/MLEM/Misc/GenericDataHolder.cs b/MLEM/Misc/GenericDataHolder.cs index b224b55..fd5e19e 100644 --- a/MLEM/Misc/GenericDataHolder.cs +++ b/MLEM/Misc/GenericDataHolder.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Runtime.Serialization; diff --git a/Sandbox/Content/Content.mgcb b/Sandbox/Content/Content.mgcb index a23980d..8c80f5f 100644 --- a/Sandbox/Content/Content.mgcb +++ b/Sandbox/Content/Content.mgcb @@ -17,6 +17,9 @@ #begin Fonts/Cadman_Roman.otf /copy:Fonts/Cadman_Roman.otf +#begin Fonts/Symbola-Emoji.ttf +/copy:Fonts/Symbola-Emoji.ttf + #begin Fonts/Regular.fnt /importer:BitmapFontImporter /processor:BitmapFontProcessor diff --git a/Sandbox/Content/Fonts/Symbola-Emoji.ttf b/Sandbox/Content/Fonts/Symbola-Emoji.ttf new file mode 100644 index 0000000..c4abce8 Binary files /dev/null and b/Sandbox/Content/Fonts/Symbola-Emoji.ttf differ diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 7314feb..82d15d2 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -11,11 +11,9 @@ using MLEM.Data; using MLEM.Data.Content; using MLEM.Extended.Font; using MLEM.Extensions; -using MLEM.Font; using MLEM.Formatting; using MLEM.Formatting.Codes; using MLEM.Graphics; -using MLEM.Input; using MLEM.Misc; using MLEM.Startup; using MLEM.Textures; @@ -67,10 +65,13 @@ public class GameImpl : MlemGame { var system = new FontSystem(); system.AddFont(File.ReadAllBytes("Content/Fonts/Cadman_Roman.otf")); + system.AddFont(File.ReadAllBytes("Content/Fonts/Symbola-Emoji.ttf")); //var font = new GenericSpriteFont(LoadContent("Fonts/TestFont")); //var font = new GenericBitmapFont(LoadContent("Fonts/Regular")); var font = new GenericStashFont(system.GetFont(32)); + /* var spriteFont = new GenericSpriteFont(MlemGame.LoadContent("Fonts/TestFont")); + */ this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) { Font = font, TextScale = 0.5F, @@ -147,7 +148,7 @@ public class GameImpl : MlemGame { formatter.Macros.Add(new Regex(""), (_, _, _) => ""); formatter.Macros.Add(new Regex(""), (_, _, _) => " blue"); formatter.Macros.Add(new Regex(""), (_, _, _) => ""); - const string strg = "This \nstring \nis\n split \n weirdly \n . "; + const string strg = "This is a string containing 💡 emoji"; //var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; //var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; //var strg = "This is a test of the underlined formatting code!"; @@ -256,43 +257,43 @@ public class GameImpl : MlemGame { }).SetData("Ref", "Main"); this.UiSystem.Add("SpillTest", spillPanel);*/ - var regularFont = spriteFont.Font; - var genericFont = spriteFont; + /* var regularFont = spriteFont.Font; + var genericFont = spriteFont; - var index = 0; - var pos = new Vector2(100, 20); - var scale = 1F; - var origin = Vector2.Zero; - var rotation = 0F; - var effects = SpriteEffects.None; + var index = 0; + var pos = new Vector2(100, 20); + var scale = 1F; + var origin = Vector2.Zero; + var rotation = 0F; + var effects = SpriteEffects.None; - this.OnDraw += (_, _) => { - const string testString = "This is a\ntest string\n\twith long lines.\nLet's write some more stuff. Let's\r\nsplit lines weirdly."; - if (MlemGame.Input.IsKeyPressed(Keys.I)) { - index++; - if (index == 1) { - scale = 2; - } else if (index == 2) { - origin = new Vector2(15, 15); - } else if (index == 3) { - rotation = 0.25F; - } else if (index == 4) { - effects = SpriteEffects.FlipHorizontally; - } else if (index == 5) { - effects = SpriteEffects.FlipVertically; - } else if (index == 6) { - effects = SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically; - } - } + this.OnDraw += (_, _) => { + const string testString = "This is a\ntest string\n\twith long lines.\nLet's write some more stuff. Let's\r\nsplit lines weirdly."; + if (MlemGame.Input.IsKeyPressed(Keys.I)) { + index++; + if (index == 1) { + scale = 2; + } else if (index == 2) { + origin = new Vector2(15, 15); + } else if (index == 3) { + rotation = 0.25F; + } else if (index == 4) { + effects = SpriteEffects.FlipHorizontally; + } else if (index == 5) { + effects = SpriteEffects.FlipVertically; + } else if (index == 6) { + effects = SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically; + } + } - /*this.SpriteBatch.Begin(); - if (MlemGame.Input.IsKeyDown(Keys.LeftShift)) { - this.SpriteBatch.DrawString(regularFont, testString, pos, Color.Red, rotation, origin, scale, effects, 0); - } else { - genericFont.DrawString(this.SpriteBatch, testString, pos, Color.Green, rotation, origin, scale, effects, 0); - } - this.SpriteBatch.End();*/ - }; + this.SpriteBatch.Begin(); + if (MlemGame.Input.IsKeyDown(Keys.LeftShift)) { + this.SpriteBatch.DrawString(regularFont, testString, pos, Color.Red, rotation, origin, scale, effects, 0); + } else { + genericFont.DrawString(this.SpriteBatch, testString, pos, Color.Green, rotation, origin, scale, effects, 0); + } + this.SpriteBatch.End(); + };*/ /*var viewport = new BoxingViewportAdapter(this.Window, this.GraphicsDevice, 1280, 720); var newPanel = new Panel(Anchor.TopLeft, new Vector2(200, 100), new Vector2(10, 10)); diff --git a/Sandbox/Sandbox.csproj b/Sandbox/Sandbox.csproj index b7ba249..896bff7 100644 --- a/Sandbox/Sandbox.csproj +++ b/Sandbox/Sandbox.csproj @@ -23,6 +23,10 @@ + + + + diff --git a/Tests/FontTests.cs b/Tests/FontTests.cs index e233294..013a2cb 100644 --- a/Tests/FontTests.cs +++ b/Tests/FontTests.cs @@ -2,7 +2,6 @@ using System; using System.Text.RegularExpressions; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Font; using MLEM.Formatting; using MLEM.Formatting.Codes; @@ -150,8 +149,8 @@ namespace Tests { CompareSizes($"This is a very simple{GenericFont.Emsp}test string"); CompareSizes($"This is a very simple{GenericFont.Zwsp}test string"); - Assert.AreEqual(new Vector2(this.font.LineHeight, this.font.LineHeight), this.font.MeasureString(GenericFont.Emsp.ToCachedString())); - Assert.AreEqual(new Vector2(0, this.font.LineHeight), this.font.MeasureString(GenericFont.Zwsp.ToCachedString())); + Assert.AreEqual(new Vector2(this.font.LineHeight, this.font.LineHeight), this.font.MeasureString(GenericFont.Emsp.ToString())); + Assert.AreEqual(new Vector2(0, this.font.LineHeight), this.font.MeasureString(GenericFont.Zwsp.ToString())); } }