2022-07-25 14:36:05 +08:00
|
|
|
#include <SFML/Graphics/Font.hpp>
|
2024-02-08 21:28:57 +08:00
|
|
|
#include <SFML/Graphics/Texture.hpp>
|
2022-07-25 14:36:05 +08:00
|
|
|
|
2023-11-17 01:03:29 +08:00
|
|
|
// Other 1st party headers
|
|
|
|
#include <SFML/System/FileInputStream.hpp>
|
|
|
|
|
2023-07-07 12:10:29 +08:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
|
|
|
|
#include <GraphicsUtil.hpp>
|
2024-02-08 21:28:57 +08:00
|
|
|
#include <WindowUtil.hpp>
|
2023-11-17 01:03:29 +08:00
|
|
|
#include <fstream>
|
2022-07-25 14:36:05 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
2023-07-07 12:10:29 +08:00
|
|
|
TEST_CASE("[Graphics] sf::Font", runDisplayTests())
|
|
|
|
{
|
|
|
|
SECTION("Type traits")
|
|
|
|
{
|
|
|
|
STATIC_CHECK(std::is_copy_constructible_v<sf::Font>);
|
|
|
|
STATIC_CHECK(std::is_copy_assignable_v<sf::Font>);
|
2023-11-18 02:54:57 +08:00
|
|
|
STATIC_CHECK(std::is_move_constructible_v<sf::Font>);
|
|
|
|
STATIC_CHECK(std::is_move_assignable_v<sf::Font>);
|
2023-07-07 12:10:29 +08:00
|
|
|
}
|
|
|
|
|
2024-07-06 09:38:32 +08:00
|
|
|
SECTION("Construction")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Default constructor")
|
2023-11-17 01:03:29 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
const sf::Font font;
|
|
|
|
CHECK(font.getInfo().family.empty());
|
|
|
|
CHECK(!font.hasGlyph(0));
|
|
|
|
CHECK(font.getLineSpacing(0) == 0);
|
|
|
|
CHECK(font.getUnderlinePosition(0) == 0);
|
|
|
|
CHECK(font.getUnderlineThickness(0) == 0);
|
|
|
|
CHECK(font.isSmooth());
|
2023-11-17 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("File")
|
2023-11-17 01:03:29 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Invalid filename")
|
|
|
|
{
|
|
|
|
CHECK_THROWS_AS(sf::Font("does/not/exist.ttf"), std::runtime_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Successful load")
|
|
|
|
{
|
|
|
|
const sf::Font font("Graphics/tuffy.ttf");
|
|
|
|
CHECK(font.getInfo().family == "Tuffy");
|
|
|
|
const auto& glyph = font.getGlyph(0x45, 16, false);
|
|
|
|
CHECK(glyph.advance == 9);
|
|
|
|
CHECK(glyph.lsbDelta == 9);
|
|
|
|
CHECK(glyph.rsbDelta == 16);
|
|
|
|
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
|
|
|
|
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
|
|
|
|
CHECK(font.hasGlyph(0x41));
|
|
|
|
CHECK(font.hasGlyph(0xC0));
|
|
|
|
CHECK(font.getKerning(0x41, 0x42, 12) == -1);
|
|
|
|
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
|
|
|
|
CHECK(font.getLineSpacing(24) == 30);
|
|
|
|
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f));
|
|
|
|
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f));
|
|
|
|
const auto& texture = font.getTexture(10);
|
|
|
|
CHECK(texture.getSize() == sf::Vector2u(128, 128));
|
|
|
|
CHECK(texture.isSmooth());
|
|
|
|
CHECK(!texture.isSrgb());
|
|
|
|
CHECK(!texture.isRepeated());
|
|
|
|
CHECK(texture.getNativeHandle() != 0);
|
|
|
|
CHECK(font.isSmooth());
|
|
|
|
}
|
2023-11-17 01:03:29 +08:00
|
|
|
}
|
2024-07-06 09:38:32 +08:00
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Memory")
|
2023-11-17 01:03:29 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Invalid data and size")
|
|
|
|
{
|
|
|
|
CHECK_THROWS_AS(sf::Font(nullptr, 1), std::runtime_error);
|
|
|
|
const std::byte testByte{0xCD};
|
|
|
|
CHECK_THROWS_AS(sf::Font(&testByte, 0), std::runtime_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Successful load")
|
|
|
|
{
|
|
|
|
const auto memory = loadIntoMemory("Graphics/tuffy.ttf");
|
|
|
|
const sf::Font font(memory.data(), memory.size());
|
|
|
|
CHECK(font.getInfo().family == "Tuffy");
|
|
|
|
const auto& glyph = font.getGlyph(0x45, 16, false);
|
|
|
|
CHECK(glyph.advance == 9);
|
|
|
|
CHECK(glyph.lsbDelta == 9);
|
|
|
|
CHECK(glyph.rsbDelta == 16);
|
|
|
|
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
|
|
|
|
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
|
|
|
|
CHECK(font.hasGlyph(0x41));
|
|
|
|
CHECK(font.hasGlyph(0xC0));
|
|
|
|
CHECK(font.getKerning(0x41, 0x42, 12) == -1);
|
|
|
|
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
|
|
|
|
CHECK(font.getLineSpacing(24) == 30);
|
|
|
|
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f));
|
|
|
|
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f));
|
|
|
|
const auto& texture = font.getTexture(10);
|
|
|
|
CHECK(texture.getSize() == sf::Vector2u(128, 128));
|
|
|
|
CHECK(texture.isSmooth());
|
|
|
|
CHECK(!texture.isSrgb());
|
|
|
|
CHECK(!texture.isRepeated());
|
|
|
|
CHECK(texture.getNativeHandle() != 0);
|
|
|
|
CHECK(font.isSmooth());
|
|
|
|
}
|
2023-11-17 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Stream")
|
2023-11-17 01:03:29 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::FileInputStream stream("Graphics/tuffy.ttf");
|
|
|
|
const sf::Font font(stream);
|
2023-11-17 01:03:29 +08:00
|
|
|
CHECK(font.getInfo().family == "Tuffy");
|
|
|
|
const auto& glyph = font.getGlyph(0x45, 16, false);
|
|
|
|
CHECK(glyph.advance == 9);
|
|
|
|
CHECK(glyph.lsbDelta == 9);
|
|
|
|
CHECK(glyph.rsbDelta == 16);
|
|
|
|
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
|
|
|
|
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
|
|
|
|
CHECK(font.hasGlyph(0x41));
|
|
|
|
CHECK(font.hasGlyph(0xC0));
|
|
|
|
CHECK(font.getKerning(0x41, 0x42, 12) == -1);
|
|
|
|
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
|
|
|
|
CHECK(font.getLineSpacing(24) == 30);
|
|
|
|
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f));
|
|
|
|
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f));
|
|
|
|
const auto& texture = font.getTexture(10);
|
|
|
|
CHECK(texture.getSize() == sf::Vector2u(128, 128));
|
|
|
|
CHECK(texture.isSmooth());
|
|
|
|
CHECK(!texture.isSrgb());
|
|
|
|
CHECK(!texture.isRepeated());
|
|
|
|
CHECK(texture.getNativeHandle() != 0);
|
|
|
|
CHECK(font.isSmooth());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("openFromFile()")
|
2023-11-17 01:03:29 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::Font font;
|
2024-07-06 09:38:32 +08:00
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Invalid filename")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
CHECK(!font.openFromFile("does/not/exist.ttf"));
|
2024-07-06 09:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Successful load")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
REQUIRE(font.openFromFile("Graphics/tuffy.ttf"));
|
2024-07-06 09:38:32 +08:00
|
|
|
CHECK(font.getInfo().family == "Tuffy");
|
|
|
|
const auto& glyph = font.getGlyph(0x45, 16, false);
|
|
|
|
CHECK(glyph.advance == 9);
|
|
|
|
CHECK(glyph.lsbDelta == 9);
|
|
|
|
CHECK(glyph.rsbDelta == 16);
|
|
|
|
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
|
|
|
|
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
|
|
|
|
CHECK(font.hasGlyph(0x41));
|
|
|
|
CHECK(font.hasGlyph(0xC0));
|
|
|
|
CHECK(font.getKerning(0x41, 0x42, 12) == -1);
|
|
|
|
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
|
|
|
|
CHECK(font.getLineSpacing(24) == 30);
|
|
|
|
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f));
|
|
|
|
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f));
|
|
|
|
const auto& texture = font.getTexture(10);
|
|
|
|
CHECK(texture.getSize() == sf::Vector2u(128, 128));
|
|
|
|
CHECK(texture.isSmooth());
|
|
|
|
CHECK(!texture.isSrgb());
|
|
|
|
CHECK(!texture.isRepeated());
|
|
|
|
CHECK(texture.getNativeHandle() != 0);
|
|
|
|
CHECK(font.isSmooth());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("openFromMemory()")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::Font font;
|
|
|
|
|
|
|
|
SECTION("Invalid data and size")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
CHECK(!font.openFromMemory(nullptr, 1));
|
|
|
|
const std::byte testByte{0xCD};
|
|
|
|
CHECK(!font.openFromMemory(&testByte, 0));
|
2024-07-06 09:38:32 +08:00
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Successful load")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
const auto memory = loadIntoMemory("Graphics/tuffy.ttf");
|
|
|
|
REQUIRE(font.openFromMemory(memory.data(), memory.size()));
|
2024-07-06 09:38:32 +08:00
|
|
|
CHECK(font.getInfo().family == "Tuffy");
|
|
|
|
const auto& glyph = font.getGlyph(0x45, 16, false);
|
|
|
|
CHECK(glyph.advance == 9);
|
|
|
|
CHECK(glyph.lsbDelta == 9);
|
|
|
|
CHECK(glyph.rsbDelta == 16);
|
|
|
|
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
|
|
|
|
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
|
|
|
|
CHECK(font.hasGlyph(0x41));
|
|
|
|
CHECK(font.hasGlyph(0xC0));
|
|
|
|
CHECK(font.getKerning(0x41, 0x42, 12) == -1);
|
|
|
|
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
|
|
|
|
CHECK(font.getLineSpacing(24) == 30);
|
|
|
|
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f));
|
|
|
|
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f));
|
|
|
|
const auto& texture = font.getTexture(10);
|
|
|
|
CHECK(texture.getSize() == sf::Vector2u(128, 128));
|
|
|
|
CHECK(texture.isSmooth());
|
|
|
|
CHECK(!texture.isSrgb());
|
|
|
|
CHECK(!texture.isRepeated());
|
|
|
|
CHECK(texture.getNativeHandle() != 0);
|
|
|
|
CHECK(font.isSmooth());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("openFromStream()")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::Font font;
|
|
|
|
sf::FileInputStream stream;
|
|
|
|
|
|
|
|
SECTION("Invalid stream")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
CHECK(!font.openFromStream(stream));
|
2024-07-06 09:38:32 +08:00
|
|
|
}
|
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
SECTION("Successful load")
|
2024-07-06 09:38:32 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
REQUIRE(stream.open("Graphics/tuffy.ttf"));
|
|
|
|
REQUIRE(font.openFromStream(stream));
|
2024-07-06 09:38:32 +08:00
|
|
|
CHECK(font.getInfo().family == "Tuffy");
|
|
|
|
const auto& glyph = font.getGlyph(0x45, 16, false);
|
|
|
|
CHECK(glyph.advance == 9);
|
|
|
|
CHECK(glyph.lsbDelta == 9);
|
|
|
|
CHECK(glyph.rsbDelta == 16);
|
|
|
|
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
|
|
|
|
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
|
|
|
|
CHECK(font.hasGlyph(0x41));
|
|
|
|
CHECK(font.hasGlyph(0xC0));
|
|
|
|
CHECK(font.getKerning(0x41, 0x42, 12) == -1);
|
|
|
|
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
|
|
|
|
CHECK(font.getLineSpacing(24) == 30);
|
|
|
|
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f));
|
|
|
|
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f));
|
|
|
|
const auto& texture = font.getTexture(10);
|
|
|
|
CHECK(texture.getSize() == sf::Vector2u(128, 128));
|
|
|
|
CHECK(texture.isSmooth());
|
|
|
|
CHECK(!texture.isSrgb());
|
|
|
|
CHECK(!texture.isRepeated());
|
|
|
|
CHECK(texture.getNativeHandle() != 0);
|
|
|
|
CHECK(font.isSmooth());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:10:29 +08:00
|
|
|
SECTION("Set/get smooth")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::Font font("Graphics/tuffy.ttf");
|
2023-07-07 12:10:29 +08:00
|
|
|
font.setSmooth(false);
|
|
|
|
CHECK(!font.isSmooth());
|
|
|
|
}
|
|
|
|
}
|