From 1d38262388f64019af634c3b571c1552691ceb10 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 4 Jun 2020 22:18:53 +0200 Subject: [PATCH] only use nbsp in image codes if the font supports it --- MLEM.Extended/Font/GenericBitmapFont.cs | 5 +++++ MLEM.Ui/Style/UntexturedStyle.cs | 4 ++++ MLEM/Font/GenericFont.cs | 7 +++++++ MLEM/Font/GenericSpriteFont.cs | 5 +++++ MLEM/Formatting/Codes/ImageCode.cs | 4 +++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/MLEM.Extended/Font/GenericBitmapFont.cs b/MLEM.Extended/Font/GenericBitmapFont.cs index 7e35f43..42ee2f8 100644 --- a/MLEM.Extended/Font/GenericBitmapFont.cs +++ b/MLEM.Extended/Font/GenericBitmapFont.cs @@ -78,6 +78,11 @@ namespace MLEM.Extended.Font { batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth); } + /// + public override bool HasCharacter(char c) { + return this.Font.GetCharacterRegion(c) != null; + } + // this fixes an issue with BitmapFonts where, if only given a single character, // only the width of the character itself (disregarding spacing) is returned private bool SingleCharacterWidthFix(string text, out Vector2 size) { diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index e689cfc..a4054ff 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -74,6 +74,10 @@ namespace MLEM.Ui.Style { public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { } + public override bool HasCharacter(char c) { + return false; + } + } } diff --git a/MLEM/Font/GenericFont.cs b/MLEM/Font/GenericFont.cs index de07a0d..7b6a480 100644 --- a/MLEM/Font/GenericFont.cs +++ b/MLEM/Font/GenericFont.cs @@ -48,6 +48,13 @@ 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); + /// + /// Returns whether this generic font supports the given character + /// + /// The character + /// Whether this generic font supports the character + public abstract bool HasCharacter(char c); + /// /// Draws a string with the given text alignment. /// diff --git a/MLEM/Font/GenericSpriteFont.cs b/MLEM/Font/GenericSpriteFont.cs index 1ded08f..8a4e766 100644 --- a/MLEM/Font/GenericSpriteFont.cs +++ b/MLEM/Font/GenericSpriteFont.cs @@ -72,5 +72,10 @@ namespace MLEM.Font { batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth); } + /// + public override bool HasCharacter(char c) { + return this.Font.Characters.Contains(c); + } + } } \ No newline at end of file diff --git a/MLEM/Formatting/Codes/ImageCode.cs b/MLEM/Formatting/Codes/ImageCode.cs index 801d3c5..884c3d6 100644 --- a/MLEM/Formatting/Codes/ImageCode.cs +++ b/MLEM/Formatting/Codes/ImageCode.cs @@ -29,7 +29,9 @@ namespace MLEM.Formatting.Codes { /// public override string GetReplacementString(GenericFont font) { if (this.replacement == null) { - this.replacement = font.GetWidthString(font.LineHeight); + // use non-breaking space so that the image won't be line-splitted + var strg = font.GetWidthString(font.LineHeight, font.HasCharacter('\u00A0') ? '\u00A0' : ' '); + this.replacement = strg.Remove(strg.Length - 1) + ' '; this.gapSize = font.MeasureString(this.replacement).X; } return this.replacement;