diff --git a/MLEM.Extended/Font/GenericBitmapFont.cs b/MLEM.Extended/Font/GenericBitmapFont.cs
index 2df049a..fb3adc9 100644
--- a/MLEM.Extended/Font/GenericBitmapFont.cs
+++ b/MLEM.Extended/Font/GenericBitmapFont.cs
@@ -21,10 +21,14 @@ namespace MLEM.Extended.Font {
}
public override Vector2 MeasureString(string text) {
+ if (text.Length == 1 && this.SingleCharacterWidthFix(text, out var size))
+ return size;
return this.Font.MeasureString(text);
}
public override Vector2 MeasureString(StringBuilder text) {
+ if (text.Length == 1 && this.SingleCharacterWidthFix(text.ToString(), out var size))
+ return size;
return this.Font.MeasureString(text);
}
@@ -52,5 +56,18 @@ namespace MLEM.Extended.Font {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
+ // 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) {
+ var codePoint = char.ConvertToUtf32(text, 0);
+ var region = this.Font.GetCharacterRegion(codePoint);
+ if (region != null) {
+ size = new Vector2(region.XAdvance, region.Height);
+ return true;
+ }
+ size = default;
+ return false;
+ }
+
}
}
\ No newline at end of file
diff --git a/Sandbox/Content/Content.mgcb b/Sandbox/Content/Content.mgcb
index f637798..b3ff1d0 100644
--- a/Sandbox/Content/Content.mgcb
+++ b/Sandbox/Content/Content.mgcb
@@ -14,6 +14,23 @@
#---------------------------------- Content ---------------------------------#
+#begin Fonts/Regular.fnt
+/importer:BitmapFontImporter
+/processor:BitmapFontProcessor
+/build:Fonts/Regular.fnt
+
+#begin Fonts/RegularTexture.png
+/importer:TextureImporter
+/processor:TextureProcessor
+/processorParam:ColorKeyColor=255,0,255,255
+/processorParam:ColorKeyEnabled=True
+/processorParam:GenerateMipmaps=False
+/processorParam:PremultiplyAlpha=True
+/processorParam:ResizeToPowerOfTwo=False
+/processorParam:MakeSquare=False
+/processorParam:TextureFormat=Color
+/build:Fonts/RegularTexture.png
+
#begin Fonts/TestFont.spritefont
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
diff --git a/Sandbox/Content/Fonts/Regular.fnt b/Sandbox/Content/Fonts/Regular.fnt
new file mode 100644
index 0000000..bee2d8c
--- /dev/null
+++ b/Sandbox/Content/Fonts/Regular.fnt
@@ -0,0 +1,330 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Sandbox/Content/Fonts/RegularTexture.png b/Sandbox/Content/Fonts/RegularTexture.png
new file mode 100644
index 0000000..2dbf170
Binary files /dev/null and b/Sandbox/Content/Fonts/RegularTexture.png differ
diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs
index 58951f3..12a27f6 100644
--- a/Sandbox/GameImpl.cs
+++ b/Sandbox/GameImpl.cs
@@ -8,6 +8,7 @@ using MLEM.Cameras;
using MLEM.Content;
using MLEM.Data;
using MLEM.Extended.Extensions;
+using MLEM.Extended.Font;
using MLEM.Extended.Tiled;
using MLEM.Extensions;
using MLEM.Font;
@@ -21,6 +22,7 @@ using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using MonoGame.Extended;
+using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.Tiled;
using Newtonsoft.Json.Linq;
using RectangleF = MonoGame.Extended.RectangleF;
@@ -60,7 +62,8 @@ namespace Sandbox {
};
var tex = this.rawContent.Load("Textures/Test");
- var font = new GenericSpriteFont(LoadContent("Fonts/TestFont"));
+ //var font = new GenericSpriteFont(LoadContent("Fonts/TestFont"));
+ var font = new GenericBitmapFont(LoadContent("Fonts/Regular"));
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = font,
TextScale = 0.1F,
@@ -116,23 +119,24 @@ namespace Sandbox {
var formatter = new TextFormatter();
formatter.AddImage("Test", new TextureRegion(tex, 0, 8, 24, 24));
- //var strg = "This is a formatted string with two bits of formatting! It also includesaverylongwordthatisformattedaswell. Additionally, it wobbles and has a shadow or a weird shadow. We like icons too! ";
+ var strg = "This is a formatted string with two bits of formatting! It also includesaverylongwordthatisformattedaswell. Additionally, it wobbles and has a shadow or a weird shadow. We like icons too! ";
//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!";
+ //var strg = "This is a test of the underlined formatting code!";
this.tokenized = formatter.Tokenize(font, strg);
- this.tokenized.Split(font, 400, 1);
+ this.tokenized.Split(font, 400, 5);
this.OnDraw += (g, time) => {
- this.SpriteBatch.Begin();
+ this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green);
- this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 1, 0);
+ font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, 5, SpriteEffects.None, 0);
+ this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0);
this.SpriteBatch.End();
};
this.OnUpdate += (g, time) => {
if (this.InputHandler.IsPressed(Keys.W)) {
this.tokenized = formatter.Tokenize(font, strg);
- this.tokenized.Split(font, this.InputHandler.IsModifierKeyDown(ModifierKey.Shift) ? 400 : 500, 1);
+ this.tokenized.Split(font, this.InputHandler.IsModifierKeyDown(ModifierKey.Shift) ? 400 : 500, 5);
}
this.tokenized.Update(time);
};