Skip to content

Commit

Permalink
Fixed MeasureText reporting no width
Browse files Browse the repository at this point in the history
Fixes Issue #16
  • Loading branch information
vonderborch committed Oct 6, 2021
1 parent fd60ebb commit 8b6cd2b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
18 changes: 18 additions & 0 deletions Velentr.Font.DevEnv/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion Velentr.Font.FNA/Velentr.Font.FNA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageProjectUrl>https://github.com/vonderborch/Velentr.Font</PackageProjectUrl>
<Description>An alternative solution for Monogame/FNA/XNA-derived frameworks that utilizes SharpFont to draw text rather than the traditional SpriteFont approach.</Description>
<PackageTags>SharpFont, FNA, SpriteFont, Font</PackageTags>
<Version>1.2.6</Version>
<Version>1.2.7</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
2 changes: 1 addition & 1 deletion Velentr.Font.Monogame/Velentr.Font.Monogame.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageProjectUrl>https://github.com/vonderborch/Velentr.Font</PackageProjectUrl>
<Description>An alternative solution for Monogame/FNA/XNA-derived frameworks that utilizes SharpFont to draw text rather than the traditional SpriteFont approach.</Description>
<PackageTags>SharpFont, Monogame, SpriteFont, Font</PackageTags>
<Version>1.2.6</Version>
<Version>1.2.7</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>..\release\Monogame\Velentr.Font.Monogame.xml</DocumentationFile>
Expand Down
21 changes: 17 additions & 4 deletions Velentr.Font/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,21 +498,29 @@ public Text MakeText(string text, bool applyMarkdown = false)
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The size of the text.</returns>
public Vector2 MeasureText(StringBuilder text)
public Vector2 MeasureText(StringBuilder text, bool applyMarkdown = false)
{
return MeasureText(text.ToString());
return MeasureText(text.ToString(), applyMarkdown);
}

/// <summary>
/// Measures the text.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The size of the text.</returns>
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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -577,6 +586,10 @@ public Vector2 MeasureText(string text)
}
}
}
else
{
finalSize.X = Math.Max(offsetX, finalSize.X);
}
}

return finalSize;
Expand Down

0 comments on commit 8b6cd2b

Please sign in to comment.