From 6c318b915f06ee601fdb67f86bf0bfab81d34678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Wed, 23 Feb 2022 13:20:20 +0100 Subject: [PATCH] Tests/Utility: Add font loading test --- tests/Engine/Utility/FontLoading.cpp | 107 +++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/Engine/Utility/FontLoading.cpp diff --git a/tests/Engine/Utility/FontLoading.cpp b/tests/Engine/Utility/FontLoading.cpp new file mode 100644 index 000000000..c55872039 --- /dev/null +++ b/tests/Engine/Utility/FontLoading.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include + +std::filesystem::path GetResourceDir(); + +SCENARIO("Fonts", "[Utility][Font]") +{ + WHEN("Loading default font") + { + std::shared_ptr font = Nz::Font::GetDefault(); + + CHECK(font->GetFamilyName() == "Open Sans"); + CHECK(font->GetStyleName() == "Regular"); + + for (std::size_t i = 0; i < 3; ++i) + { + WHEN("Inspecting size info") + { + const auto& sizeInfo24 = font->GetSizeInfo(24); + CHECK(sizeInfo24.lineHeight == 33); + CHECK(sizeInfo24.spaceAdvance == 6); + CHECK(sizeInfo24.underlinePosition == Approx(-2.40625f)); + CHECK(sizeInfo24.underlineThickness == Approx(1.20312f)); + + const auto& sizeInfo72 = font->GetSizeInfo(72); + CHECK(sizeInfo72.lineHeight == 98); + CHECK(sizeInfo72.spaceAdvance == 19); + CHECK(sizeInfo72.underlinePosition == Approx(-7.20312f)); + CHECK(sizeInfo72.underlineThickness == Approx(3.59375f)); + } + + WHEN("Retrieving kerning") + { + struct Test + { + unsigned int characterSize; + char firstChar; + char secondChar; + int expectedKerning; + }; + + std::array tests{ + { + Test { 16, 'A', 'A', 0 }, + Test { 24, 'A', 'A', 0 }, + Test { 32, 'A', 'A', 0 }, + Test { 48, 'A', 'A', 0 }, + + Test { 16, 'A', 'M', 0 }, + Test { 24, 'A', 'M', 0 }, + Test { 32, 'A', 'M', 0 }, + Test { 48, 'A', 'M', 0 }, + + Test { 16, 'A', 'T', -1 }, + Test { 24, 'A', 'T', -2 }, + Test { 32, 'A', 'T', -2 }, + Test { 48, 'A', 'T', -3 }, + + Test { 16, 'A', 'V', 0 }, + Test { 24, 'A', 'V', -1 }, + Test { 32, 'A', 'V', -1 }, + Test { 48, 'A', 'V', -2 }, + } + }; + + for (const Test& test : tests) + { + INFO("between " << test.firstChar << " and " << test.secondChar << " at character size " << test.characterSize); + CHECK(font->GetKerning(test.characterSize, test.firstChar, test.secondChar) == test.expectedKerning); + } + } + + WHEN("Retrieving a glyph existing in the font") + { + const auto& glyph = font->GetGlyph(72, Nz::TextStyle_Regular, 0.f, 'L'); + REQUIRE(glyph.valid); + CHECK(glyph.advance == 37); + CHECK(glyph.aabb.IsValid()); + CHECK(glyph.atlasRect.IsValid()); + CHECK(glyph.fauxOutlineThickness == 0.f); + CHECK_FALSE(glyph.requireFauxBold); + CHECK_FALSE(glyph.requireFauxItalic); + } + + WHEN("Retrieving a glyph existing in the font with italic") + { + const auto& glyph = font->GetGlyph(72, Nz::TextStyle::Italic, 0.f, 'y'); + REQUIRE(glyph.valid); + CHECK(glyph.advance == 36); + CHECK(glyph.aabb.IsValid()); + CHECK(glyph.atlasRect.IsValid()); + CHECK(glyph.fauxOutlineThickness == 0.f); + CHECK_FALSE(glyph.requireFauxBold); + CHECK(glyph.requireFauxItalic); + } + + if (i == 1) + { + font->ClearGlyphCache(); + font->ClearKerningCache(); + font->ClearSizeInfoCache(); + } + } + } +}