From 05c0a01b94e6dbcc1748fb2672d2d81a12e82825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 7 Mar 2022 19:30:28 +0100 Subject: [PATCH] Text: port to new Utility::Path, including plugins. Like in Trade, the unatomic exists() + read() pair (and silent failures if the file exists but can't be read) was replaced with just Path::read() that now returns an Optional. Besides that, not much worth mentioning. --- doc/snippets/MagnumText.cpp | 16 ++- src/Magnum/Text/AbstractFont.cpp | 12 +- src/Magnum/Text/AbstractFontConverter.cpp | 17 ++- .../Text/Test/AbstractFontConverterTest.cpp | 107 ++++++++++-------- src/Magnum/Text/Test/AbstractFontTest.cpp | 11 +- src/MagnumPlugins/MagnumFont/MagnumFont.cpp | 10 +- .../MagnumFont/Test/MagnumFontGLTest.cpp | 6 +- .../MagnumFont/Test/MagnumFontTest.cpp | 26 +++-- .../MagnumFontConverter.cpp | 5 +- .../Test/MagnumFontConverterTest.cpp | 25 ++-- 10 files changed, 140 insertions(+), 95 deletions(-) diff --git a/doc/snippets/MagnumText.cpp b/doc/snippets/MagnumText.cpp index 26feabb78..4e21fb97a 100644 --- a/doc/snippets/MagnumText.cpp +++ b/doc/snippets/MagnumText.cpp @@ -25,8 +25,10 @@ #include #include +#include +#include /** @todo remove once file callbacks are -free */ #include -#include +#include #include #include "Magnum/FileCallback.h" @@ -62,8 +64,8 @@ font->fillGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" Containers::Pointer font; /* [AbstractFont-usage-callbacks] */ struct Data { - std::unordered_map> files; + std::unordered_map>> files; } data; font->setFileCallback([](const std::string& filename, @@ -78,11 +80,13 @@ font->setFileCallback([](const std::string& filename, return {}; } - /* Load if not there yet */ + /* Load if not there yet. If the mapping fails, remember that to not + attempt to load the same file again next time. */ if(found == data.files.end()) found = data.files.emplace( - filename, Utility::Directory::mapRead(filename)).first; + filename, Utility::Path::mapRead(filename)).first; - return Containers::arrayView(found->second); + if(!found->second) return {}; + return Containers::arrayView(*found->second); }, data); font->openFile("magnum-font.conf", 13.0f); diff --git a/src/Magnum/Text/AbstractFont.cpp b/src/Magnum/Text/AbstractFont.cpp index 04da5b9bb..fed358e17 100644 --- a/src/Magnum/Text/AbstractFont.cpp +++ b/src/Magnum/Text/AbstractFont.cpp @@ -28,8 +28,10 @@ #include #include #include +#include +#include /** @todo remove once PluginManager is -free */ #include -#include +#include #include #include "Magnum/FileCallback.h" @@ -52,9 +54,10 @@ std::string AbstractFont::pluginInterface() { #ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT std::vector AbstractFont::pluginSearchPaths() { + const Containers::Optional libraryLocation = Utility::Path::libraryLocation(&pluginInterface); return PluginManager::implicitPluginSearchPaths( #ifndef MAGNUM_BUILD_STATIC - Utility::Directory::libraryLocation(&pluginInterface), + libraryLocation ? *libraryLocation : Containers::String{}, #else {}, #endif @@ -185,12 +188,13 @@ auto AbstractFont::doOpenFile(const std::string& filename, const Float size) -> /* Otherwise open the file directly */ } else { - if(!Utility::Directory::exists(filename)) { + const Containers::Optional> data = Utility::Path::read(filename); + if(!data) { Error() << "Text::AbstractFont::openFile(): cannot open file" << filename; return {}; } - metrics = doOpenData(Utility::Directory::read(filename), size); + metrics = doOpenData(*data, size); } return metrics; diff --git a/src/Magnum/Text/AbstractFontConverter.cpp b/src/Magnum/Text/AbstractFontConverter.cpp index 2392c74f0..b4c62694e 100644 --- a/src/Magnum/Text/AbstractFontConverter.cpp +++ b/src/Magnum/Text/AbstractFontConverter.cpp @@ -28,9 +28,12 @@ #include /* std::sort(), std::unique() */ #include #include +#include +#include +#include /** @todo remove once PluginManager and AbstractFontConverter is -free */ #include #include -#include +#include #include #include "Magnum/Text/AbstractGlyphCache.h" @@ -67,9 +70,10 @@ std::string AbstractFontConverter::pluginInterface() { #ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT std::vector AbstractFontConverter::pluginSearchPaths() { + const Containers::Optional libraryLocation = Utility::Path::libraryLocation(&pluginInterface); return PluginManager::implicitPluginSearchPaths( #ifndef MAGNUM_BUILD_STATIC - Utility::Directory::libraryLocation(&pluginInterface), + libraryLocation ? *libraryLocation : Containers::String{}, #else {}, #endif @@ -136,7 +140,7 @@ bool AbstractFontConverter::doExportFontToFile(AbstractFont& font, AbstractGlyph const auto data = doExportFontToData(font, cache, filename, characters); if(data.empty()) return false; - for(const auto& d: data) if(!Utility::Directory::write(d.first, d.second)) { + for(const auto& d: data) if(!Utility::Path::write(d.first, d.second)) { Error() << "Text::AbstractFontConverter::exportFontToFile(): cannot write to file" << d.first; return false; } @@ -189,7 +193,7 @@ bool AbstractFontConverter::doExportGlyphCacheToFile(AbstractGlyphCache& cache, const auto data = doExportGlyphCacheToData(cache, filename); if(data.empty()) return false; - for(const auto& d: data) if(!Utility::Directory::write(d.first, d.second)) { + for(const auto& d: data) if(!Utility::Path::write(d.first, d.second)) { Error() << "Text::AbstractFontConverter::exportGlyphCacheToFile(): cannot write to file" << d.first; return false; } @@ -240,12 +244,13 @@ Containers::Pointer AbstractFontConverter::doImportGlyphCach "Text::AbstractFontConverter::importGlyphCacheFromFile(): feature advertised but not implemented", {}); /* Open file */ - if(!Utility::Directory::exists(filename)) { + const Containers::Optional> data = Utility::Path::read(filename); + if(!data) { Error() << "Text::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file" << filename; return nullptr; } - return doImportGlyphCacheFromSingleData(Utility::Directory::read(filename)); + return doImportGlyphCacheFromSingleData(*data); } Debug& operator<<(Debug& debug, const FontConverterFeature value) { diff --git a/src/Magnum/Text/Test/AbstractFontConverterTest.cpp b/src/Magnum/Text/Test/AbstractFontConverterTest.cpp index ded762b5f..a5f3e4cfe 100644 --- a/src/Magnum/Text/Test/AbstractFontConverterTest.cpp +++ b/src/Magnum/Text/Test/AbstractFontConverterTest.cpp @@ -25,13 +25,14 @@ #include #include -#include +#include +#include /** @todo remove when AbstractFontConverter is -free */ #include #include #include #include #include -#include +#include #include "Magnum/Text/AbstractFont.h" #include "Magnum/Text/AbstractFontConverter.h" @@ -151,7 +152,7 @@ AbstractFontConverterTest::AbstractFontConverterTest() { &AbstractFontConverterTest::debugFeatures}); /* Create testing dir */ - Utility::Directory::mkpath(TEXT_TEST_OUTPUT_DIR); + Utility::Path::make(TEXT_TEST_OUTPUT_DIR); } struct DummyFont: AbstractFont { @@ -366,20 +367,22 @@ void AbstractFontConverterTest::exportFontToFile() { } bool doExportFontToFile(AbstractFont&, AbstractGlyphCache&, const std::string& filename, const std::u32string& characters) const override { - return - Utility::Directory::write(filename, Containers::arrayView({'\xf0'})) && - Utility::Directory::write(filename + ".dat", Containers::arrayView({'\xfe', char(characters.size())})); + CORRADE_VERIFY(Utility::Path::write(filename, Containers::arrayView({'\xf0'}))); + CORRADE_VERIFY(Utility::Path::write(filename + ".dat", Containers::arrayView({'\xfe', char(characters.size())}))); + return true; } } converter; - const std::string filename = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out"); - const std::string filename2 = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out.dat"); + Containers::String filename = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "font.out"); + Containers::String filename2 = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "font.out.dat"); /* Remove previous files, if any */ - if(Utility::Directory::exists(filename)) - CORRADE_VERIFY(Utility::Directory::rm(filename)); - if(Utility::Directory::exists(filename2)) - CORRADE_VERIFY(Utility::Directory::rm(filename2)); + if(Utility::Path::exists(filename)) + CORRADE_VERIFY(Utility::Path::remove(filename)); + if(Utility::Path::exists(filename2)) + CORRADE_VERIFY(Utility::Path::remove(filename2)); + + CORRADE_VERIFY(true); /* Capture correct function name first */ CORRADE_VERIFY(converter.exportFontToFile(dummyFont, dummyGlyphCache, filename, "eh")); CORRADE_COMPARE_AS(filename, "\xf0", @@ -436,14 +439,14 @@ void AbstractFontConverterTest::exportFontToFileThroughData() { } } converter; - const std::string filename = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out"); - const std::string filename2 = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out.dat"); + Containers::String filename = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "font.out"); + Containers::String filename2 = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "font.out.dat"); /* Remove previous files, if any */ - if(Utility::Directory::exists(filename)) - CORRADE_VERIFY(Utility::Directory::rm(filename)); - if(Utility::Directory::exists(filename2)) - CORRADE_VERIFY(Utility::Directory::rm(filename2)); + if(Utility::Path::exists(filename)) + CORRADE_VERIFY(Utility::Path::remove(filename)); + if(Utility::Path::exists(filename2)) + CORRADE_VERIFY(Utility::Path::remove(filename2)); /* doExportToFile() should call doExportToData() */ CORRADE_VERIFY(converter.exportFontToFile(dummyFont, dummyGlyphCache, filename, "awoo")); @@ -462,18 +465,18 @@ void AbstractFontConverterTest::exportFontToFileThroughDataFailed() { std::vector>> doExportFontToData(AbstractFont&, AbstractGlyphCache&, const std::string&, const std::u32string&) const override { return {}; } } converter; - const std::string filename = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out"); + Containers::String filename = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "font.out"); /* Remove previous file, if any */ - if(Utility::Directory::exists(filename)) - CORRADE_VERIFY(Utility::Directory::rm(filename)); + if(Utility::Path::exists(filename)) + CORRADE_VERIFY(Utility::Path::remove(filename)); /* Function should fail, no file should get written and no error output should be printed (the base implementation assumes the plugin does it) */ std::ostringstream out; Error redirectError{&out}; CORRADE_VERIFY(!converter.exportFontToFile(dummyFont, dummyGlyphCache, filename, {})); - CORRADE_VERIFY(!Utility::Directory::exists(filename)); + CORRADE_VERIFY(!Utility::Path::exists(filename)); CORRADE_COMPARE(out.str(), ""); } @@ -668,20 +671,22 @@ void AbstractFontConverterTest::exportGlyphCacheToFile() { } bool doExportGlyphCacheToFile(AbstractGlyphCache&, const std::string& filename) const override { - return - Utility::Directory::write(filename, Containers::arrayView({'\xf0'})) && - Utility::Directory::write(filename + ".dat", Containers::arrayView({'\xfe', '\xed'})); + CORRADE_VERIFY(Utility::Path::write(filename, Containers::arrayView({'\xf0'}))); + CORRADE_VERIFY(Utility::Path::write(filename + ".dat", Containers::arrayView({'\xfe', '\xed'}))); + return true; } } converter; - const std::string filename = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "cache.out"); - const std::string filename2 = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "cache.out.dat"); + Containers::String filename = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "cache.out"); + Containers::String filename2 = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "cache.out.dat"); /* Remove previous files, if any */ - if(Utility::Directory::exists(filename)) - CORRADE_VERIFY(Utility::Directory::rm(filename)); - if(Utility::Directory::exists(filename2)) - CORRADE_VERIFY(Utility::Directory::rm(filename2)); + if(Utility::Path::exists(filename)) + CORRADE_VERIFY(Utility::Path::remove(filename)); + if(Utility::Path::exists(filename2)) + CORRADE_VERIFY(Utility::Path::remove(filename2)); + + CORRADE_VERIFY(true); /* Capture correct function name first */ CORRADE_VERIFY(converter.exportGlyphCacheToFile(dummyGlyphCache, filename)); CORRADE_COMPARE_AS(filename, "\xf0", @@ -738,14 +743,14 @@ void AbstractFontConverterTest::exportGlyphCacheToFileThroughData() { } } converter; - const std::string filename = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "cache.out"); - const std::string filename2 = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "cache.out.dat"); + Containers::String filename = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "cache.out"); + Containers::String filename2 = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "cache.out.dat"); /* Remove previous files, if any */ - if(Utility::Directory::exists(filename)) - CORRADE_VERIFY(Utility::Directory::rm(filename)); - if(Utility::Directory::exists(filename2)) - CORRADE_VERIFY(Utility::Directory::rm(filename2)); + if(Utility::Path::exists(filename)) + CORRADE_VERIFY(Utility::Path::remove(filename)); + if(Utility::Path::exists(filename2)) + CORRADE_VERIFY(Utility::Path::remove(filename2)); /* doExportGlyphCacheToFile() should call doExportGlyphCacheToData() */ CORRADE_VERIFY(converter.exportGlyphCacheToFile(dummyGlyphCache, filename)); @@ -762,18 +767,17 @@ void AbstractFontConverterTest::exportGlyphCacheToFileThroughDataFailed() { std::vector>> doExportGlyphCacheToData(AbstractGlyphCache&, const std::string&) const override { return {}; } } converter; - const std::string filename = Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "cache.out"); - /* Remove previous file, if any */ - if(Utility::Directory::exists(filename)) - CORRADE_VERIFY(Utility::Directory::rm(filename)); + Containers::String filename = Utility::Path::join(TEXT_TEST_OUTPUT_DIR, "cache.out"); + if(Utility::Path::exists(filename)) + CORRADE_VERIFY(Utility::Path::remove(filename)); /* Function should fail, no file should get written and no error output should be printed (the base implementation assumes the plugin does it) */ std::ostringstream out; Error redirectError{&out}; CORRADE_VERIFY(!converter.exportGlyphCacheToFile(dummyGlyphCache, filename)); - CORRADE_VERIFY(!Utility::Directory::exists(filename)); + CORRADE_VERIFY(!Utility::Path::exists(filename)); CORRADE_COMPARE(out.str(), ""); } @@ -964,14 +968,16 @@ void AbstractFontConverterTest::importGlyphCacheFromFile() { } Containers::Pointer doImportGlyphCacheFromFile(const std::string& filename) const override { - Containers::Array data = Utility::Directory::read(filename); - if(data.size() == 1 && data[0] == '\xa5') - return Containers::pointer(new DummyGlyphCache{{123, 345}}); - return nullptr; + Containers::Optional> data = Utility::Path::read(filename); + CORRADE_VERIFY(data); + CORRADE_COMPARE_AS(*data, + Containers::arrayView({'\xa5'}), + TestSuite::Compare::Container); + return Containers::pointer(new DummyGlyphCache{{123, 345}}); } } converter; - Containers::Pointer cache = converter.importGlyphCacheFromFile(Utility::Directory::join(TEXT_TEST_DIR, "data.bin")); + Containers::Pointer cache = converter.importGlyphCacheFromFile(Utility::Path::join(TEXT_TEST_DIR, "data.bin")); CORRADE_VERIFY(cache); CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); } @@ -1024,7 +1030,7 @@ void AbstractFontConverterTest::importGlyphCacheFromFileAsSingleData() { } converter; /* doImportFromFile() should call doImportFromSingleData() */ - Containers::Pointer cache = converter.importGlyphCacheFromFile(Utility::Directory::join(TEXT_TEST_DIR, "data.bin")); + Containers::Pointer cache = converter.importGlyphCacheFromFile(Utility::Path::join(TEXT_TEST_DIR, "data.bin")); CORRADE_VERIFY(cache); CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); } @@ -1044,7 +1050,10 @@ void AbstractFontConverterTest::importGlyphCacheFromFileAsSingleDataNotFound() { std::ostringstream out; Error redirectError{&out}; CORRADE_VERIFY(!converter.importGlyphCacheFromFile("nonexistent.bin")); - CORRADE_COMPARE(out.str(), "Text::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file nonexistent.bin\n"); + /* There's an error message from Path::read() before */ + CORRADE_COMPARE_AS(out.str(), + "\nText::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file nonexistent.bin\n", + TestSuite::Compare::StringHasSuffix); } void AbstractFontConverterTest::debugFeature() { diff --git a/src/Magnum/Text/Test/AbstractFontTest.cpp b/src/Magnum/Text/Test/AbstractFontTest.cpp index cc4552591..9a2925b54 100644 --- a/src/Magnum/Text/Test/AbstractFontTest.cpp +++ b/src/Magnum/Text/Test/AbstractFontTest.cpp @@ -26,9 +26,11 @@ #include #include #include +#include /** @todo remove once AbstractFont is -free */ #include +#include #include -#include +#include #include "Magnum/FileCallback.h" #include "Magnum/Math/Vector2.h" @@ -223,7 +225,7 @@ void AbstractFontTest::openFileAsData() { /* doOpenFile() should call doOpenData() */ CORRADE_VERIFY(!font.isOpened()); - font.openFile(Utility::Directory::join(TEXT_TEST_DIR, "data.bin"), 13.0f); + font.openFile(Utility::Path::join(TEXT_TEST_DIR, "data.bin"), 13.0f); CORRADE_VERIFY(font.isOpened()); CORRADE_COMPARE(font.size(), 13.0f); CORRADE_COMPARE(font.ascent(), 1.0f); @@ -247,7 +249,10 @@ void AbstractFontTest::openFileAsDataNotFound() { std::ostringstream out; Error redirectError{&out}; CORRADE_VERIFY(!font.openFile("nonexistent.foo", 12.0f)); - CORRADE_COMPARE(out.str(), "Text::AbstractFont::openFile(): cannot open file nonexistent.foo\n"); + /* There's an error message from Path::read() before */ + CORRADE_COMPARE_AS(out.str(), + "\nText::AbstractFont::openFile(): cannot open file nonexistent.foo\n", + TestSuite::Compare::StringHasSuffix); } void AbstractFontTest::openFileNotImplemented() { diff --git a/src/MagnumPlugins/MagnumFont/MagnumFont.cpp b/src/MagnumPlugins/MagnumFont/MagnumFont.cpp index 69875ebfd..a8ccb84c1 100644 --- a/src/MagnumPlugins/MagnumFont/MagnumFont.cpp +++ b/src/MagnumPlugins/MagnumFont/MagnumFont.cpp @@ -28,8 +28,10 @@ #include #include #include +#include +#include /** @todo remove once AbstractFont is -free */ #include -#include +#include #include #include "Magnum/ImageView.h" @@ -48,7 +50,7 @@ struct MagnumFont::Data { Utility::Configuration conf; Containers::Optional image; - Containers::Optional filePath; + Containers::Optional filePath; std::unordered_map glyphId; std::vector glyphAdvance; }; @@ -108,7 +110,7 @@ auto MagnumFont::doOpenData(const Containers::ArrayView data, const TgaImporter already, no need to repeat them again. */ Trade::TgaImporter importer; importer.setFileCallback(fileCallback(), fileCallbackUserData()); - if(!importer.openFile(Utility::Directory::join(_opened->filePath ? *_opened->filePath : "", conf.value("image")))) return {}; + if(!importer.openFile(Utility::Path::join(_opened->filePath ? *_opened->filePath : "", conf.value("image")))) return {}; _opened->image = importer.image2D(0); if(!_opened->image) return {}; @@ -137,7 +139,7 @@ auto MagnumFont::doOpenData(const Containers::ArrayView data, const auto MagnumFont::doOpenFile(const std::string& filename, Float size) -> Metrics { _opened.emplace(); - _opened->filePath = Utility::Directory::path(filename); + _opened->filePath.emplace(Utility::Path::split(filename).first()); return AbstractFont::doOpenFile(filename, size); } diff --git a/src/MagnumPlugins/MagnumFont/Test/MagnumFontGLTest.cpp b/src/MagnumPlugins/MagnumFont/Test/MagnumFontGLTest.cpp index 055fe56ac..9790a4f9c 100644 --- a/src/MagnumPlugins/MagnumFont/Test/MagnumFontGLTest.cpp +++ b/src/MagnumPlugins/MagnumFont/Test/MagnumFontGLTest.cpp @@ -24,7 +24,9 @@ */ #include -#include +#include +#include /** @todo remove once AbstractFont is -free */ +#include #include "Magnum/GL/OpenGLTester.h" #include "Magnum/Text/AbstractFont.h" @@ -60,7 +62,7 @@ MagnumFontGLTest::MagnumFontGLTest() { void MagnumFontGLTest::createGlyphCache() { Containers::Pointer font = _fontManager.instantiate("MagnumFont"); - CORRADE_VERIFY(font->openFile(Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf"), 0.0f)); + CORRADE_VERIFY(font->openFile(Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf"), 0.0f)); /* Just testing that nothing crashes, asserts or errors */ Containers::Pointer cache = font->createGlyphCache(); diff --git a/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp b/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp index 1ac108466..96b4aee08 100644 --- a/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp +++ b/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp @@ -26,9 +26,11 @@ #include #include #include +#include /** @todo remove once AbstractFont is -free */ #include +#include #include -#include +#include #include "Magnum/FileCallback.h" #include "Magnum/Text/AbstractFont.h" @@ -77,13 +79,16 @@ void MagnumFontTest::nonexistent() { std::ostringstream out; Error redirectError{&out}; CORRADE_VERIFY(!font->openFile("nonexistent.conf", 0.0f)); - CORRADE_COMPARE(out.str(), "Text::AbstractFont::openFile(): cannot open file nonexistent.conf\n"); + /* There's an error message from Path::read() before */ + CORRADE_COMPARE_AS(out.str(), + "\nText::AbstractFont::openFile(): cannot open file nonexistent.conf\n", + TestSuite::Compare::StringHasSuffix); } void MagnumFontTest::properties() { Containers::Pointer font = _fontManager.instantiate("MagnumFont"); - CORRADE_VERIFY(font->openFile(Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf"), 0.0f)); + CORRADE_VERIFY(font->openFile(Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf"), 0.0f)); CORRADE_COMPARE(font->size(), 16.0f); CORRADE_COMPARE(font->ascent(), 25.0f); CORRADE_COMPARE(font->descent(), -10.0f); @@ -94,7 +99,7 @@ void MagnumFontTest::properties() { void MagnumFontTest::layout() { Containers::Pointer font = _fontManager.instantiate("MagnumFont"); - CORRADE_VERIFY(font->openFile(Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf"), 0.0f)); + CORRADE_VERIFY(font->openFile(Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf"), 0.0f)); /* Fill the cache with some fake glyphs */ struct DummyGlyphCache: AbstractGlyphCache { @@ -145,8 +150,13 @@ void MagnumFontTest::fileCallbackImage() { CORRADE_VERIFY(font->features() & FontFeature::FileCallback); std::unordered_map> files; - files["not/a/path/font.conf"] = Utility::Directory::read(Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf")); - files["not/a/path/font.tga"] = Utility::Directory::read(Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.tga")); + Containers::Optional> conf = Utility::Path::read(Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf")); + Containers::Optional> tga = + Utility::Path::read(Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.tga")); + CORRADE_VERIFY(conf); + CORRADE_VERIFY(tga); + files["not/a/path/font.conf"] = *std::move(conf); + files["not/a/path/font.tga"] = *std::move(tga); font->setFileCallback([](const std::string& filename, InputFileCallbackPolicy policy, std::unordered_map>& files) { Debug{} << "Loading" << filename << "with" << policy; @@ -171,7 +181,9 @@ void MagnumFontTest::fileCallbackImageNotFound() { std::ostringstream out; Error redirectError{&out}; - CORRADE_VERIFY(!font->openData(Utility::Directory::read(Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf")), 13.0f)); + Containers::Optional> conf = Utility::Path::read(Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf")); + CORRADE_VERIFY(conf); + CORRADE_VERIFY(!font->openData(*conf, 13.0f)); CORRADE_COMPARE(out.str(), "Trade::AbstractImporter::openFile(): cannot open file font.tga\n"); } diff --git a/src/MagnumPlugins/MagnumFontConverter/MagnumFontConverter.cpp b/src/MagnumPlugins/MagnumFontConverter/MagnumFontConverter.cpp index 2558f6201..c3a498e22 100644 --- a/src/MagnumPlugins/MagnumFontConverter/MagnumFontConverter.cpp +++ b/src/MagnumPlugins/MagnumFontConverter/MagnumFontConverter.cpp @@ -28,8 +28,9 @@ #include /* std::sort() */ #include #include +#include #include -#include +#include #include "Magnum/Image.h" #include "Magnum/ImageView.h" @@ -58,7 +59,7 @@ std::vector>> MagnumFontConverter Utility::Configuration configuration; configuration.setValue("version", 1); - configuration.setValue("image", Utility::Directory::filename(filename) + ".tga"); + configuration.setValue("image", Utility::Path::split(filename).second() + ".tga"); configuration.setValue("originalImageSize", cache.textureSize()); configuration.setValue("padding", cache.padding()); configuration.setValue("fontSize", font.size()); diff --git a/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp b/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp index 1641f672b..969a7ea78 100644 --- a/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp +++ b/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp @@ -25,10 +25,11 @@ #include #include +#include /** @todo remove once AbstractFont is -free */ #include #include #include -#include +#include #include "Magnum/Image.h" #include "Magnum/PixelFormat.h" @@ -72,17 +73,17 @@ MagnumFontConverterTest::MagnumFontConverterTest() { #endif /* Create the output directory if it doesn't exist yet */ - CORRADE_INTERNAL_ASSERT_OUTPUT(Utility::Directory::mkpath(MAGNUMFONTCONVERTER_TEST_WRITE_DIR)); + CORRADE_INTERNAL_ASSERT_OUTPUT(Utility::Path::make(MAGNUMFONTCONVERTER_TEST_WRITE_DIR)); } void MagnumFontConverterTest::exportFont() { - std::string confFilename = Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.conf"); - std::string tgaFilename = Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.tga"); + Containers::String confFilename = Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.conf"); + Containers::String tgaFilename = Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.tga"); /* Remove previously created files */ - if(Utility::Directory::exists(confFilename)) - CORRADE_VERIFY(Utility::Directory::rm(confFilename)); - if(Utility::Directory::exists(tgaFilename)) - CORRADE_VERIFY(Utility::Directory::rm(tgaFilename)); + if(Utility::Path::exists(confFilename)) + CORRADE_VERIFY(Utility::Path::remove(confFilename)); + if(Utility::Path::exists(tgaFilename)) + CORRADE_VERIFY(Utility::Path::remove(tgaFilename)); /* Fake font with fake cache */ class FakeFont: public Text::AbstractFont { @@ -137,12 +138,12 @@ void MagnumFontConverterTest::exportFont() { /* Convert the file */ Containers::Pointer converter = _fontConverterManager.instantiate("MagnumFontConverter"); - CORRADE_VERIFY(converter->exportFontToFile(font, cache, Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font"), "Wave")); + CORRADE_VERIFY(converter->exportFontToFile(font, cache, Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font"), "Wave")); /* Verify font parameters */ CORRADE_COMPARE_AS(confFilename, - Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf"), - TestSuite::Compare::File); + Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf"), + TestSuite::Compare::File); if(!(_importerManager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("TgaImporter plugin not enabled, not testing glyph cache contents"); @@ -182,7 +183,7 @@ void MagnumFontConverterTest::exportFontNoGlyphCacheImageDownload() { std::ostringstream out; Error redirectError{&out}; - CORRADE_VERIFY(!converter->exportFontToFile(font, cache, Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font"), "Wave")); + CORRADE_VERIFY(!converter->exportFontToFile(font, cache, Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font"), "Wave")); CORRADE_COMPARE(out.str(), "Text::MagnumFontConverter::exportFontToData(): passed glyph cache doesn't support image download\n"); }