Skip to content

Commit

Permalink
Fixed possible corrupted text when changing the font scale with the S…
Browse files Browse the repository at this point in the history
…FML backend
  • Loading branch information
texus committed Dec 19, 2024
1 parent 9320e00 commit fd59d6a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TGUI 1.7 (TBD)
---------------

- New widgets: GrowHorizontalLayout and GrowVerticalLayout
- Fixed some issues with font scale in SFML font backend


TGUI 1.6.1 (8 October 2024)
Expand Down
4 changes: 4 additions & 0 deletions include/TGUI/Backend/Font/SFML-Graphics/BackendFontSFML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ TGUI_MODULE_EXPORT namespace tgui
// from breaking since sf::Font does the same.
std::map<unsigned int, std::shared_ptr<BackendTexture>> m_textures;
std::map<unsigned int, unsigned int> m_textureVersions;

// We use a single version that is unique across all text sizes. Otherwise switching from size by changing the font scale
// can result in the same version being accidentally returned and the text not realizing that the texture changed.
unsigned int m_lastTextureVersion = 0;
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/Backend/Font/SFML-Graphics/BackendFontSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ namespace tgui
}
m_textures[scaledTextSize] = texture;

textureVersion = ++m_textureVersions[scaledTextSize];
textureVersion = ++m_lastTextureVersion;
m_textureVersions[scaledTextSize] = textureVersion;
return texture;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,28 @@ TEST_CASE("[Widget]")

TEST_DRAW("OriginScaleRotation.png")
}

SECTION("Font scale")
{
auto button = tgui::Button::create();
button->setSize(150, 50);
button->setText("Scaling");
button->setTextSize(32);
button->setPosition(5, 10);

TEST_DRAW_INIT(400, 175, button)

auto oldView = gui.getView();
gui.setAbsoluteView({0, 0, 160, 70});

TEST_DRAW("FontScale_Unscaled.png")

tgui::getBackend()->setFontScale(2.5f);
TEST_DRAW("FontScale_Scaled.png")

tgui::getBackend()->setFontScale(1);
gui.setAbsoluteView(oldView.getRect());
}
}

SECTION("Bug Fixes")
Expand Down
Binary file added tests/expected/FontScale_Scaled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/expected/FontScale_Unscaled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fd59d6a

Please sign in to comment.