diff --git a/Velentr.Font.DevEnv/Game1.cs b/Velentr.Font.DevEnv/Game1.cs index d6a5d7c..9e772f8 100644 --- a/Velentr.Font.DevEnv/Game1.cs +++ b/Velentr.Font.DevEnv/Game1.cs @@ -51,6 +51,24 @@ protected override void Update(GameTime gameTime) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); + var size1a = font1.MeasureText("Hello World!"); + var size1b = font1.MakeText("Hello World!").Size; + var size1c = font1.MeasureText("Hello World!"); + + var size2a = font1.MeasureText("Hello\nWorld!"); + var size2b = font1.MakeText("Hello\nWorld!").Size; + var size2c = font1.MeasureText("Hello\nWorld!"); + + + + var size3a = font2.MeasureText("Hello World!"); + var size3b = font2.MakeText("Hello World!").Size; + var size3c = font2.MeasureText("Hello World!"); + + var size4a = font2.MeasureText("Hello\nWorld!"); + var size4b = font2.MakeText("Hello\nWorld!").Size; + var size4c = font2.MeasureText("Hello\nWorld!"); + base.Update(gameTime); } diff --git a/Velentr.Font.FNA/Velentr.Font.FNA.csproj b/Velentr.Font.FNA/Velentr.Font.FNA.csproj index 4f66f33..1a6d93e 100644 --- a/Velentr.Font.FNA/Velentr.Font.FNA.csproj +++ b/Velentr.Font.FNA/Velentr.Font.FNA.csproj @@ -10,7 +10,7 @@ https://github.com/vonderborch/Velentr.Font An alternative solution for Monogame/FNA/XNA-derived frameworks that utilizes SharpFont to draw text rather than the traditional SpriteFont approach. SharpFont, FNA, SpriteFont, Font - 1.2.6 + 1.2.7 true diff --git a/Velentr.Font.Monogame/Velentr.Font.Monogame.csproj b/Velentr.Font.Monogame/Velentr.Font.Monogame.csproj index 79122ef..d666ac4 100644 --- a/Velentr.Font.Monogame/Velentr.Font.Monogame.csproj +++ b/Velentr.Font.Monogame/Velentr.Font.Monogame.csproj @@ -10,7 +10,7 @@ https://github.com/vonderborch/Velentr.Font An alternative solution for Monogame/FNA/XNA-derived frameworks that utilizes SharpFont to draw text rather than the traditional SpriteFont approach. SharpFont, Monogame, SpriteFont, Font - 1.2.6 + 1.2.7 ..\release\Monogame\Velentr.Font.Monogame.xml diff --git a/Velentr.Font/Font.cs b/Velentr.Font/Font.cs index 62108a9..483804f 100644 --- a/Velentr.Font/Font.cs +++ b/Velentr.Font/Font.cs @@ -498,9 +498,9 @@ public Text MakeText(string text, bool applyMarkdown = false) /// /// The text. /// The size of the text. - public Vector2 MeasureText(StringBuilder text) + public Vector2 MeasureText(StringBuilder text, bool applyMarkdown = false) { - return MeasureText(text.ToString()); + return MeasureText(text.ToString(), applyMarkdown); } /// @@ -508,11 +508,19 @@ public Vector2 MeasureText(StringBuilder text) /// /// The text. /// The size of the text. - public Vector2 MeasureText(string text) + public Vector2 MeasureText(string text, bool applyMarkdown = false) { + // exit early if we've got the text in cache from making a Text object previously... + if (TextCache.TryGetItem(text, out var textResult)) + { + return textResult.Size; + } + + // otherwise, we need to calculate the size of the string... var finalSize = new Vector2(0, 0); var offsetX = 0; + var offsetY = 0; var underrun = 0; var finalCharacterIndex = text.Length - 1; @@ -530,6 +538,7 @@ public Vector2 MeasureText(string text) finalSize.X = Math.Max(offsetX, finalSize.X); offsetX = 0; underrun = 0; + offsetY += cachedCharacter.AdvanceY; if (i != finalCharacterIndex) { finalSize.Y += cachedCharacter.AdvanceY; @@ -542,7 +551,7 @@ public Vector2 MeasureText(string text) } // Markdown rules - if (text[i] == '[' && (i > 0 && text[i - 1] != '\\')) + if (applyMarkdown && text[i] == '[' && (i > 0 && text[i - 1] != '\\')) { var results = ApplyMarkdownCommands(text, Color.White, i); i = results.Item1; @@ -577,6 +586,10 @@ public Vector2 MeasureText(string text) } } } + else + { + finalSize.X = Math.Max(offsetX, finalSize.X); + } } return finalSize;