From fca4fa1aa253335bb7b210c5c951531287dcf89e Mon Sep 17 00:00:00 2001 From: kimci86 Date: Mon, 24 Jun 2024 22:08:51 +0200 Subject: [PATCH] Rename Font::loadFromFile into Font::openFromFile Similar renaming for Font::loadFromMemory and Font::loadFromStream. The goal is to better express the need to keep the source available, similar to Music::openFromFile for example. --- doc/mainpage.hpp | 2 +- examples/android/app/src/main/jni/main.cpp | 2 +- examples/cocoa/CocoaAppDelegate.mm | 2 +- examples/island/Island.cpp | 2 +- examples/joystick/Joystick.cpp | 4 +- examples/opengl/OpenGL.cpp | 2 +- examples/raw_input/RawInput.cpp | 4 +- examples/shader/Shader.cpp | 4 +- examples/sound_effects/SoundEffects.cpp | 4 +- examples/tennis/Tennis.cpp | 4 +- include/SFML/Graphics/Font.hpp | 46 +++++++++---------- include/SFML/Graphics/RenderWindow.hpp | 2 +- include/SFML/Graphics/Text.hpp | 4 +- src/SFML/Graphics/Font.cpp | 8 ++-- test/Graphics/Font.test.cpp | 24 +++++----- test/Graphics/Text.test.cpp | 4 +- .../SFML/SFML App.xctemplate/main.cpp | 2 +- .../SFML/SFML CLT.xctemplate/main.cpp | 2 +- 18 files changed, 59 insertions(+), 63 deletions(-) diff --git a/doc/mainpage.hpp b/doc/mainpage.hpp index b2599e1bb..561deb458 100644 --- a/doc/mainpage.hpp +++ b/doc/mainpage.hpp @@ -25,7 +25,7 @@ /// sf::Sprite sprite(texture); /// /// // Create a graphical text to display -/// const auto font = sf::Font::loadFromFile("arial.ttf").value(); +/// const auto font = sf::Font::openFromFile("arial.ttf").value(); /// sf::Text text(font, "Hello SFML", 50); /// /// // Load a music to play diff --git a/examples/android/app/src/main/jni/main.cpp b/examples/android/app/src/main/jni/main.cpp index e79609595..e66403138 100644 --- a/examples/android/app/src/main/jni/main.cpp +++ b/examples/android/app/src/main/jni/main.cpp @@ -92,7 +92,7 @@ int main(int argc, char* argv[]) image.setPosition(sf::Vector2f(screen.size) / 2.f); image.setOrigin(sf::Vector2f(texture.getSize()) / 2.f); - const auto font = sf::Font::loadFromFile("tuffy.ttf").value(); + const auto font = sf::Font::openFromFile("tuffy.ttf").value(); sf::Text text(font, "Tap anywhere to move the logo.", 64); text.setFillColor(sf::Color::Black); diff --git a/examples/cocoa/CocoaAppDelegate.mm b/examples/cocoa/CocoaAppDelegate.mm index cc56d55fe..27463b520 100644 --- a/examples/cocoa/CocoaAppDelegate.mm +++ b/examples/cocoa/CocoaAppDelegate.mm @@ -52,7 +52,7 @@ struct SFMLmainWindow std::filesystem::path resPath{[[[NSBundle mainBundle] resourcePath] tostdstring]}; sf::RenderWindow renderWindow; - sf::Font font{sf::Font::loadFromFile(resPath / "tuffy.ttf").value()}; + sf::Font font{sf::Font::openFromFile(resPath / "tuffy.ttf").value()}; sf::Text text{font}; sf::Texture logo{sf::Texture::loadFromFile(resPath / "logo.png").value()}; sf::Sprite sprite{logo}; diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index 819bd8689..09df89cc6 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -91,7 +91,7 @@ int main() sf::RenderWindow window(sf::VideoMode({windowWidth, windowHeight}), "SFML Island", sf::Style::Titlebar | sf::Style::Close); window.setVerticalSyncEnabled(true); - const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value(); + const auto font = sf::Font::openFromFile("resources/tuffy.ttf").value(); // Create all of our graphics resources sf::Text hudText(font); diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 09ce372a6..36a572512 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -93,8 +93,8 @@ int main() sf::RenderWindow window(sf::VideoMode({400, 775}), "Joystick", sf::Style::Close); window.setVerticalSyncEnabled(true); - // Load the text font - const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value(); + // Open the text font + const auto font = sf::Font::openFromFile("resources/tuffy.ttf").value(); // Set up our string conversion parameters sstr.precision(2); diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index 9841c06a4..f3d451da2 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -62,7 +62,7 @@ int main() const sf::Sprite background(backgroundTexture); // Create some text to draw on top of our OpenGL object - const auto font = sf::Font::loadFromFile(resourcesDir() / "tuffy.ttf").value(); + const auto font = sf::Font::openFromFile(resourcesDir() / "tuffy.ttf").value(); sf::Text text(font, "SFML / OpenGL demo"); sf::Text sRgbInstructions(font, "Press space to toggle sRGB conversion"); diff --git a/examples/raw_input/RawInput.cpp b/examples/raw_input/RawInput.cpp index 29d38a424..114a84965 100644 --- a/examples/raw_input/RawInput.cpp +++ b/examples/raw_input/RawInput.cpp @@ -18,8 +18,8 @@ int main() sf::RenderWindow window(sf::VideoMode({800u, 600u}), "SFML Raw Mouse Input", sf::Style::Titlebar | sf::Style::Close); window.setVerticalSyncEnabled(true); - // Load the application font and pass it to the Effect class - const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value(); + // Open the application font and pass it to the Effect class + const auto font = sf::Font::openFromFile("resources/tuffy.ttf").value(); // Create the mouse position text sf::Text mousePosition(font, "", 20); diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index b618be327..7f2190a1d 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -393,8 +393,8 @@ int main() sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML Shader", sf::Style::Titlebar | sf::Style::Close); window.setVerticalSyncEnabled(true); - // Load the application font - const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value(); + // Open the application font + const auto font = sf::Font::openFromFile("resources/tuffy.ttf").value(); // Create the effects std::optional pixelateEffect = tryLoadPixelate(); diff --git a/examples/sound_effects/SoundEffects.cpp b/examples/sound_effects/SoundEffects.cpp index 260ba7505..9ac4fadb0 100644 --- a/examples/sound_effects/SoundEffects.cpp +++ b/examples/sound_effects/SoundEffects.cpp @@ -1063,8 +1063,8 @@ int main() sf::Style::Titlebar | sf::Style::Close); window.setVerticalSyncEnabled(true); - // Load the application font and pass it to the Effect class - const auto font = sf::Font::loadFromFile(resourcesDir() / "tuffy.ttf").value(); + // Open the application font and pass it to the Effect class + const auto font = sf::Font::openFromFile(resourcesDir() / "tuffy.ttf").value(); Effect::setFont(font); // Create the effects diff --git a/examples/tennis/Tennis.cpp b/examples/tennis/Tennis.cpp index 42f55b45d..3e71dafa5 100644 --- a/examples/tennis/Tennis.cpp +++ b/examples/tennis/Tennis.cpp @@ -81,8 +81,8 @@ int main() ball.setFillColor(sf::Color::White); ball.setOrigin({ballRadius / 2.f, ballRadius / 2.f}); - // Load the text font - const auto font = sf::Font::loadFromFile(resourcesDir() / "tuffy.ttf").value(); + // Open the text font + const auto font = sf::Font::openFromFile(resourcesDir() / "tuffy.ttf").value(); // Initialize the pause message sf::Text pauseMessage(font); diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 09c5ca922..d615a7f93 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -74,7 +74,7 @@ public: }; //////////////////////////////////////////////////////////// - /// \brief Load the font from a file + /// \brief Open the font from a file /// /// The supported font formats are: TrueType, Type 1, CFF, /// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42. @@ -84,59 +84,55 @@ public: /// /// \warning SFML cannot preload all the font data in this /// function, so the file has to remain accessible until - /// the sf::Font object loads a new font or is destroyed. + /// the sf::Font object is destroyed. /// /// \param filename Path of the font file to load /// - /// \return Font if loading succeeded, `std::nullopt` if it failed + /// \return Font if opening succeeded, `std::nullopt` if it failed /// - /// \see loadFromMemory, loadFromStream + /// \see openFromMemory, openFromStream /// //////////////////////////////////////////////////////////// - [[nodiscard]] static std::optional loadFromFile(const std::filesystem::path& filename); + [[nodiscard]] static std::optional openFromFile(const std::filesystem::path& filename); //////////////////////////////////////////////////////////// - /// \brief Load the font from a file in memory + /// \brief Open the font from a file in memory /// /// The supported font formats are: TrueType, Type 1, CFF, /// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42. /// /// \warning SFML cannot preload all the font data in this /// function, so the buffer pointed by \a data has to remain - /// valid until the sf::Font object loads a new font or - /// is destroyed. + /// valid until the sf::Font object is destroyed. /// /// \param data Pointer to the file data in memory /// \param sizeInBytes Size of the data to load, in bytes /// - /// \return Font if loading succeeded, `std::nullopt` if it failed + /// \return Font if opening succeeded, `std::nullopt` if it failed /// - /// \see loadFromFile, loadFromStream + /// \see openFromFile, openFromStream /// //////////////////////////////////////////////////////////// - [[nodiscard]] static std::optional loadFromMemory(const void* data, std::size_t sizeInBytes); + [[nodiscard]] static std::optional openFromMemory(const void* data, std::size_t sizeInBytes); //////////////////////////////////////////////////////////// - /// \brief Load the font from a custom stream + /// \brief Open the font from a custom stream /// /// The supported font formats are: TrueType, Type 1, CFF, /// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42. - /// Warning: SFML cannot preload all the font data in this - /// function, so the contents of \a stream have to remain - /// valid as long as the font is used. /// /// \warning SFML cannot preload all the font data in this /// function, so the stream has to remain accessible until - /// the sf::Font object loads a new font or is destroyed. + /// the sf::Font object is destroyed. /// /// \param stream Source stream to read from /// - /// \return Font if loading succeeded, `std::nullopt` if it failed + /// \return Font if opening succeeded, `std::nullopt` if it failed /// - /// \see loadFromFile, loadFromMemory + /// \see openFromFile, openFromMemory /// //////////////////////////////////////////////////////////// - [[nodiscard]] static std::optional loadFromStream(InputStream& stream); + [[nodiscard]] static std::optional openFromStream(InputStream& stream); //////////////////////////////////////////////////////////// /// \brief Get the font information @@ -401,11 +397,11 @@ private: /// \class sf::Font /// \ingroup graphics /// -/// Fonts can be loaded from a file, from memory or from a custom +/// Fonts can be opened from a file, from memory or from a custom /// stream, and supports the most common types of fonts. See -/// the loadFromFile function for the complete list of supported formats. +/// the openFromFile function for the complete list of supported formats. /// -/// Once it is loaded, a sf::Font instance provides three +/// Once it is opened, a sf::Font instance provides three /// types of information about the font: /// \li Global metrics, such as the line spacing /// \li Per-glyph metrics, such as bounding box or kerning @@ -433,8 +429,8 @@ private: /// /// Usage example: /// \code -/// // Load a new font -/// const auto font = sf::Font::loadFromFile("arial.ttf").value(); +/// // Open a new font +/// const auto font = sf::Font::openFromFile("arial.ttf").value(); /// /// // Create a text which uses our font /// sf::Text text1(font); @@ -447,7 +443,7 @@ private: /// text2.setStyle(sf::Text::Italic); /// \endcode /// -/// Apart from loading font files, and passing them to instances +/// Apart from opening font files, and passing them to instances /// of sf::Text, you should normally not have to deal directly /// with this class. However, it may be useful to access the /// font metrics or rasterized glyphs for advanced usage. diff --git a/include/SFML/Graphics/RenderWindow.hpp b/include/SFML/Graphics/RenderWindow.hpp index 7f39e0f83..53ad44116 100644 --- a/include/SFML/Graphics/RenderWindow.hpp +++ b/include/SFML/Graphics/RenderWindow.hpp @@ -268,7 +268,7 @@ private: /// // Create a sprite and a text to display /// const auto texture = sf::Texture::loadFromFile("circle.png").value(); /// sf::Sprite sprite(texture); -/// const auto font = sf::Font::loadFromFile("arial.ttf").value(); +/// const auto font = sf::Font::openFromFile("arial.ttf").value(); /// sf::Text text(font); /// ... /// diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index a20b4cf63..7c1541665 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -468,8 +468,8 @@ private: /// /// Usage example: /// \code -/// // Load a font -/// const auto font = sf::Font::loadFromFile("arial.ttf").value(); +/// // Open a font +/// const auto font = sf::Font::openFromFile("arial.ttf").value(); /// /// // Create a text /// sf::Text text(font, "hello"); diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 83758aa5c..f1783a616 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -129,7 +129,7 @@ Font::Font(std::shared_ptr&& fontHandles, std::string&& familyName) //////////////////////////////////////////////////////////// -std::optional Font::loadFromFile(const std::filesystem::path& filename) +std::optional Font::openFromFile(const std::filesystem::path& filename) { #ifndef SFML_SYSTEM_ANDROID @@ -173,7 +173,7 @@ std::optional Font::loadFromFile(const std::filesystem::path& filename) #else auto stream = std::make_shared(filename); - auto font = loadFromStream(*stream); + auto font = openFromStream(*stream); if (font) font->m_stream = std::move(stream); return font; @@ -183,7 +183,7 @@ std::optional Font::loadFromFile(const std::filesystem::path& filename) //////////////////////////////////////////////////////////// -std::optional Font::loadFromMemory(const void* data, std::size_t sizeInBytes) +std::optional Font::openFromMemory(const void* data, std::size_t sizeInBytes) { auto fontHandles = std::make_shared(); @@ -228,7 +228,7 @@ std::optional Font::loadFromMemory(const void* data, std::size_t sizeInByt //////////////////////////////////////////////////////////// -std::optional Font::loadFromStream(InputStream& stream) +std::optional Font::openFromStream(InputStream& stream) { auto fontHandles = std::make_shared(); diff --git a/test/Graphics/Font.test.cpp b/test/Graphics/Font.test.cpp index 76d36ac2d..f5f5d71de 100644 --- a/test/Graphics/Font.test.cpp +++ b/test/Graphics/Font.test.cpp @@ -22,16 +22,16 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) STATIC_CHECK(std::is_move_assignable_v); } - SECTION("loadFromFile()") + SECTION("openFromFile()") { SECTION("Invalid filename") { - CHECK(!sf::Font::loadFromFile("does/not/exist.ttf")); + CHECK(!sf::Font::openFromFile("does/not/exist.ttf")); } - SECTION("Successful load") + SECTION("Valid file") { - const auto font = sf::Font::loadFromFile("Graphics/tuffy.ttf").value(); + const auto font = sf::Font::openFromFile("Graphics/tuffy.ttf").value(); CHECK(font.getInfo().family == "Tuffy"); const auto& glyph = font.getGlyph(0x45, 16, false); CHECK(glyph.advance == 9); @@ -56,19 +56,19 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) } } - SECTION("loadFromMemory()") + SECTION("openFromMemory()") { SECTION("Invalid data and size") { - CHECK(!sf::Font::loadFromMemory(nullptr, 1)); + CHECK(!sf::Font::openFromMemory(nullptr, 1)); const std::byte testByte{0xCD}; - CHECK(!sf::Font::loadFromMemory(&testByte, 0)); + CHECK(!sf::Font::openFromMemory(&testByte, 0)); } - SECTION("Successful load") + SECTION("Valid data") { const auto memory = loadIntoMemory("Graphics/tuffy.ttf"); - const auto font = sf::Font::loadFromMemory(memory.data(), memory.size()).value(); + const auto font = sf::Font::openFromMemory(memory.data(), memory.size()).value(); CHECK(font.getInfo().family == "Tuffy"); const auto& glyph = font.getGlyph(0x45, 16, false); CHECK(glyph.advance == 9); @@ -93,10 +93,10 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) } } - SECTION("loadFromStream()") + SECTION("openFromStream()") { auto stream = sf::FileInputStream::open("Graphics/tuffy.ttf").value(); - const auto font = sf::Font::loadFromStream(stream).value(); + const auto font = sf::Font::openFromStream(stream).value(); CHECK(font.getInfo().family == "Tuffy"); const auto& glyph = font.getGlyph(0x45, 16, false); CHECK(glyph.advance == 9); @@ -122,7 +122,7 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) SECTION("Set/get smooth") { - auto font = sf::Font::loadFromFile("Graphics/tuffy.ttf").value(); + auto font = sf::Font::openFromFile("Graphics/tuffy.ttf").value(); font.setSmooth(false); CHECK(!font.isSmooth()); } diff --git a/test/Graphics/Text.test.cpp b/test/Graphics/Text.test.cpp index 72b3c5b71..8b95e44b0 100644 --- a/test/Graphics/Text.test.cpp +++ b/test/Graphics/Text.test.cpp @@ -21,7 +21,7 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) STATIC_CHECK(std::is_nothrow_move_assignable_v); } - const auto font = sf::Font::loadFromFile("Graphics/tuffy.ttf").value(); + const auto font = sf::Font::openFromFile("Graphics/tuffy.ttf").value(); SECTION("Construction") { @@ -87,7 +87,7 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) SECTION("Set/get font") { sf::Text text(font); - const auto otherFont = sf::Font::loadFromFile("Graphics/tuffy.ttf").value(); + const auto otherFont = sf::Font::openFromFile("Graphics/tuffy.ttf").value(); text.setFont(otherFont); CHECK(&text.getFont() == &otherFont); } diff --git a/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp b/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp index 46ea92422..dac6a84db 100644 --- a/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp +++ b/tools/xcode/templates/SFML/SFML App.xctemplate/main.cpp @@ -34,7 +34,7 @@ int main() sf::Sprite sprite(texture); // Create a graphical text to display - const auto font = sf::Font::loadFromFile(resourcePath() / "tuffy.ttf").value(); + const auto font = sf::Font::openFromFile(resourcePath() / "tuffy.ttf").value(); sf::Text text(font, "Hello SFML", 50); text.setFillColor(sf::Color::Black); diff --git a/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp b/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp index 06319e3be..045a00b79 100644 --- a/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp +++ b/tools/xcode/templates/SFML/SFML CLT.xctemplate/main.cpp @@ -32,7 +32,7 @@ int main() sf::Sprite sprite(texture); // Create a graphical text to display - const auto font = sf::Font::loadFromFile("tuffy.ttf").value(); + const auto font = sf::Font::openFromFile("tuffy.ttf").value(); sf::Text text(font, "Hello SFML", 50); text.setFillColor(sf::Color::Black);