diff --git a/src/eepp/graphics/text.cpp b/src/eepp/graphics/text.cpp index bcc28d8ce..735330db4 100644 --- a/src/eepp/graphics/text.cpp +++ b/src/eepp/graphics/text.cpp @@ -656,7 +656,7 @@ Sizef Text::draw( const StringType& string, const Vector2f& pos, Font* font, Flo ( textDrawHints & TextHints::AllAscii ) != 0 ) ); Float kerning = 0; Float width = 0; - Float height = font->getFontHeight( fontSize ); + Float height = font->getLineSpacing( fontSize ); Sizef size{ 0, height }; size_t ssize = string.size(); BatchRenderer* BR = GlobalBatchRenderer::instance(); diff --git a/src/tests/unit_tests/fontrendering.cpp b/src/tests/unit_tests/fontrendering.cpp index a91528392..315059985 100644 --- a/src/tests/unit_tests/fontrendering.cpp +++ b/src/tests/unit_tests/fontrendering.cpp @@ -1,3 +1,4 @@ +#include "eepp/ui/uithememanager.hpp" #include "utest.hpp" #include @@ -387,3 +388,84 @@ UTEST( FontRendering, textEditBengaliTest ) { SceneManager::instance()->draw(); compareImages( utest_state, utest_result, app.getWindow(), "eepp-textedit-bengali" ); } + +UTEST( FontRendering, textSizes ) { + auto win = Engine::instance()->createWindow( + WindowSettings( 1024, 650, "eepp - Text Sizes", WindowStyle::Default, + WindowBackend::Default, 32, {}, 1, false, true ) ); + + ASSERT_TRUE_MSG( win->isOpen(), "Failed to create Window" ); + + FontTrueType* font = FontTrueType::New( "NotoSans-Regular" ); + font->loadFromFile( "../assets/fonts/NotoSans-Regular.ttf" ); + + FontStyleConfig config; + config.Font = font; + config.CharacterSize = 12; + config.Style = 0; + + String txt( "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n" + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n" + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n" + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n" + "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n" + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum." ); + + const auto runTest = [&]() { + Sizef size = Text::draw( txt, Vector2f::Zero, config ); + EXPECT_EQ( 445, size.getWidth() ); + EXPECT_EQ( 96, size.getHeight() ); + EXPECT_EQ( 445, Text::getTextWidth( txt, config ) ); + + Vector2i startPos{ 120, 7 }; + EXPECT_EQ( 19, Text::findCharacterFromPos( startPos, true, config.Font, + config.CharacterSize, txt, 0 ) ); + EXPECT_EQ( 19, Text::findCharacterFromPos( startPos, false, config.Font, + config.CharacterSize, txt, 0 ) ); + + Vector2i middlePos{ 120, 64 }; + EXPECT_EQ( 242, Text::findCharacterFromPos( middlePos, true, config.Font, + config.CharacterSize, txt, 0 ) ); + EXPECT_EQ( 242, Text::findCharacterFromPos( middlePos, false, config.Font, + config.CharacterSize, txt, 0 ) ); + + Vector2i endPos{ 120, 103 }; + EXPECT_EQ( 395, Text::findCharacterFromPos( endPos, true, config.Font, config.CharacterSize, + txt, 0 ) ); + EXPECT_EQ( -1, Text::findCharacterFromPos( endPos, false, config.Font, config.CharacterSize, + txt, 0 ) ); + + EXPECT_EQ( 18ul, Text::findLastCharPosWithinLength( txt, 120, config ) ); + Vector2f pos = Text::findCharacterPos( 19, config.Font, config.CharacterSize, txt, 0 ); + EXPECT_EQ( 120, pos.x ); + EXPECT_EQ( 0, pos.y ); + + Text text; + text.setStyleConfig( config ); + text.setString( txt ); + EXPECT_EQ( 445, text.getTextWidth() ); + EXPECT_EQ( 96, text.getTextHeight() ); + EXPECT_EQ( 446, text.getLocalBounds().getWidth() ); + EXPECT_EQ( 93, text.getLocalBounds().getHeight() ); + EXPECT_EQ( 19, text.findCharacterFromPos( startPos, true ) ); + EXPECT_EQ( 19, text.findCharacterFromPos( startPos, false ) ); + EXPECT_EQ( 242, text.findCharacterFromPos( middlePos, true ) ); + EXPECT_EQ( 242, text.findCharacterFromPos( middlePos, false ) ); + EXPECT_EQ( 395, text.findCharacterFromPos( endPos ) ); + EXPECT_EQ( -1, text.findCharacterFromPos( endPos, false ) ); + pos = text.findCharacterPos( 19 ); + EXPECT_EQ( 120, pos.x ); + EXPECT_EQ( 0, pos.y ); + }; + + UTEST_PRINT_STEP( "Text Shaper disabled" ); + runTest(); + + UTEST_PRINT_STEP( "Text Shaper enabled" ); + { + BoolScopedOp op( Text::TextShaperEnabled, true ); + runTest(); + } + + Engine::destroySingleton(); +}