Files
MonoGame.Extended/tests/MonoGame.Extended.Tests/BitmapFonts/BitmapFontTests.cs
T
Christopher WhitleyandGitHub 8850875fb4 Texture Atlas Refactor (#890)
* Moved `TextureRegion2D` and `NinePatchRegion2D` into Graphics namespace
These are used for more than just texture atlas related things and are primarly for working with `Texture2D` instances and creating sub textures from them.  Moving them to Graphics namespace where `Texture2D` lives in MonoGame

* Dropped the `2D` suffix from `TextureRegion2D` and `NinePatchRegion2D`

* Moved `RectanglExtensions` to project root
This moves the extensions for `Rectangle` to the same relative namespace location as `Rectangle` is in MonoGame.

* Renamed to `Rectangle.Extensions.cs`

* Separate `Rectangle` from `RectangleF` extensions

* Added `GetRelativeRectangle` method

* Updated documentation

* Added `GetRelativeRectangle` method

* Updated documentation

* Top-level namespace

* Remove whitespace

* Move properties up

* Top-level namespace

* Don't recreate structs for `Bounds` and Size`
By having the property return a new struct, this allocates on the stack during situations like a loop during draw.

* Size should be `Size` not `SizeF` value
Textures deal in pixels (ints not floats)

* Removed setter for `Texture` property
Texture should not be set after region is created

* Added UV properties

* Refactor constructors

* Added documentation

* Added license header

* Make it an actual extension method

* Added `GetSubRegion` extension methods for `TextureRegion`

* Update documentation about ObjectDisposedException

* Move const values to top

* Resolve errors from`TextureRegion` refactor

* Update unit tests for TextureRegion refactor

* Started NinePatch rework

* Refactored NinePatch

* Update for texture region constructor change

* Remove ninepatch from atlas

* Moved `TextureAtlasExtensions` into `SpriteBatch.Extensions`
These are extension methods for the sprite batch

* Updated to use new `DrawTextureRegion` method after rename

* Renamed to NinePatchJsonConverter

* Removed `GetRegion<T>`
TextureAtlas no longer contains nine patch regions, so this method isn't needed

* Update after `NinePatchRegion2DJsonConverter` name change

* Resolve errors from NinePatch refactor

* TextureAtlas rework

* Renamed `TextureRegion` to `Texture2DRegion`

* Moved ContentReaders to new Content\ContentReaders directory

* Moved TextureAtlasJsonConvert to Serialization directory

* Adjusted name from `Texture*` to `Texture2D*`

* Update runtimereader and runtimettype strings
2024-06-15 01:37:18 -04:00

98 lines
3.3 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.Graphics;
using MonoGame.Extended.TextureAtlases;
using Xunit;
namespace MonoGame.Extended.Tests.BitmapFonts
{
public class BitmapFontTests
{
[Fact]
public void BitmapFont_Constructor_Test()
{
var font = CreateTestFont();
Assert.Equal("Impact", font.Face);
Assert.Equal(22, font.LineHeight);
}
[Fact]
public void BitmapFont_MeasureString_SingleWord_Test()
{
var font = CreateTestFont();
var size = font.MeasureString("fox");
Assert.Equal(40, size.Width);
Assert.Equal(font.LineHeight, size.Height);
}
[Fact]
public void BitmapFont_MeasureString_WithLetterSpacing_Test()
{
var font = CreateTestFont();
font.LetterSpacing = 3;
var size = font.MeasureString("fox");
Assert.Equal(46, size.Width);
Assert.Equal(size.Height, font.LineHeight);
}
[Fact]
public void BitmapFont_MeasureString_MultipleLines_Test()
{
var font = CreateTestFont();
//var size = font.MeasureString("box fox\nbox of fox");
var size = font.MeasureString("box fox\nbox of fox");
Assert.Equal(123, size.Width);
Assert.Equal(size.Height, font.LineHeight * 2);
}
[Fact]
public void BitmapFont_MeasureString_EmptyString_Test()
{
var font = CreateTestFont();
var size = font.MeasureString(string.Empty);
Assert.Equal(0, size.Width);
Assert.Equal(0, size.Height);
}
// Test added for issue #695
// https://github.com/craftworkgames/MonoGame.Extended/issues/695
//
// Issue claims measure string does not account for space at the end of string.
[Fact]
public void BitmapFont_MeasureString_SpaceAtEnd_Test()
{
var font = CreateTestFont();
var noSpaceAtEnd = font.MeasureString("bfox");
var spaceAtEnd = font.MeasureString("bfox ");
Assert.NotEqual(noSpaceAtEnd, spaceAtEnd);
}
private static BitmapFont CreateTestFont()
{
var textureRegion = new Texture2DRegion("Test Font", x: 219, y: 61, width: 16, height: 18);
var regions = new[]
{
// extracted from 'Impact' font. 'x' is particularly interesting because it has a negative x offset
new BitmapFontCharacter(textureRegion: textureRegion, character: ' ', xOffset: 0, yOffset: 0, xAdvance: 6),
new BitmapFontCharacter(textureRegion: textureRegion, character: 'b', xOffset: 0, yOffset: 7, xAdvance: 17),
new BitmapFontCharacter(textureRegion: textureRegion, character: 'f', xOffset: 0, yOffset: 7, xAdvance: 9),
new BitmapFontCharacter(textureRegion: textureRegion, character: 'o', xOffset: 0, yOffset: 11, xAdvance: 16),
new BitmapFontCharacter(textureRegion: textureRegion, character: 'x', xOffset: -1, yOffset: 11, xAdvance: 13),
};
var font = new BitmapFont("Impact", size: 32, lineHeight: 22, regions);
return font;
}
}
}