/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include /** @todo remove once AbstractFont is -free */ #include #include #include #include #include "Magnum/Image.h" #include "Magnum/ImageView.h" #include "Magnum/PixelFormat.h" #include "Magnum/DebugTools/CompareImage.h" #include "Magnum/Text/AbstractGlyphCache.h" #include "Magnum/Text/AbstractFont.h" #include "Magnum/Text/AbstractFontConverter.h" #include "Magnum/Trade/AbstractImageConverter.h" #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/ImageData.h" #include "configure.h" namespace Magnum { namespace Text { namespace Test { namespace { struct MagnumFontConverterTest: TestSuite::Tester { explicit MagnumFontConverterTest(); void exportFont(); void exportFontEmptyCache(); void exportFontNoGlyphCacheImageDownload(); /* Explicitly forbid system-wide plugin dependencies */ PluginManager::Manager _imageConverterManager{"nonexistent"}; PluginManager::Manager _fontConverterManager{"nonexistent"}; PluginManager::Manager _importerManager{"nonexistent"}; }; MagnumFontConverterTest::MagnumFontConverterTest() { addTests({&MagnumFontConverterTest::exportFont, &MagnumFontConverterTest::exportFontEmptyCache, &MagnumFontConverterTest::exportFontNoGlyphCacheImageDownload}); /* Load the plugins directly from the build tree. Otherwise they're either static and already loaded or not present in the build tree. */ _fontConverterManager.registerExternalManager(_imageConverterManager); #if defined(TGAIMAGECONVERTER_PLUGIN_FILENAME) && defined(MAGNUMFONTCONVERTER_PLUGIN_FILENAME) CORRADE_INTERNAL_ASSERT_OUTPUT(_imageConverterManager.load(TGAIMAGECONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); CORRADE_INTERNAL_ASSERT_OUTPUT(_fontConverterManager.load(MAGNUMFONTCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif /* Optional plugins that don't have to be here */ #ifdef ANYIMAGEIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT_OUTPUT(_importerManager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif #ifdef TGAIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT_OUTPUT(_importerManager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif /* Create the output directory if it doesn't exist yet */ CORRADE_INTERNAL_ASSERT_OUTPUT(Utility::Path::make(MAGNUMFONTCONVERTER_TEST_WRITE_DIR)); } class MyFont: public Text::AbstractFont { private: void doClose() override { _opened = false; } bool doIsOpened() const override { return _opened; } Properties doOpenFile(Containers::StringView, Float) override { _opened = true; return {16.0f, 25.0f, -10.0f, 39.7333f, 4}; } FontFeatures doFeatures() const override { return {}; } Containers::Pointer doLayout(const AbstractGlyphCache&, Float, Containers::StringView) override { return nullptr; } UnsignedInt doGlyphId(const char32_t character) override { switch(character) { case U'W': return 2; case U'e': return 1; /* MSVC (but not clang-cl) doesn't support UTF-8 in char32_t literals but it does it regular strings. Still a problem in MSVC 2022, what a trash fire, can't you just give up on those codepage insanities already, ffs?! */ #if defined(CORRADE_TARGET_MSVC) && !defined(CORRADE_TARGET_CLANG) case U'\u011B': #else case U'ě': #endif return 3; } return 0; } Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(const UnsignedInt glyph) override { switch(glyph) { case 0: return {8, 0}; case 1: case 3: /* e and ě have the same advance */ return {12, 0}; case 2: return {23, 0}; } CORRADE_INTERNAL_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ } bool _opened = false; }; void MagnumFontConverterTest::exportFont() { 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::Path::exists(confFilename)) CORRADE_VERIFY(Utility::Path::remove(confFilename)); if(Utility::Path::exists(tgaFilename)) CORRADE_VERIFY(Utility::Path::remove(tgaFilename)); MyFont font; font.openFile({}, {}); struct: AbstractGlyphCache { using AbstractGlyphCache::AbstractGlyphCache; GlyphCacheFeatures doFeatures() const override { return GlyphCacheFeature::ImageDownload; } void doSetImage(const Vector2i&, const ImageView2D&) override {} Image2D doImage() override { return Image2D{PixelFormat::R8Unorm, {8, 4}, Containers::Array{InPlaceInit, { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v' }}}; } } cache{{128, 64}, {16, 8}}; /* Override the not found glyph to be in bounds as well */ cache.insert(0, {}, {{16, 8}, {16, 8}}); cache.insert(font.glyphId(U'W'), {25, 34}, {{16, 12}, {24, 56}}); cache.insert(font.glyphId(U'e'), {25, 12}, {{36, 8}, {112, 40}}); /* ě has deliberately the same glyph data as e */ cache.insert(font.glyphId( /* MSVC (but not clang-cl) doesn't support UTF-8 in char32_t literals but it does it regular strings. Still a problem in MSVC 2022, what a trash fire, can't you just give up on those codepage insanities already, ffs?! */ #if defined(CORRADE_TARGET_MSVC) && !defined(CORRADE_TARGET_CLANG) U'\u011B' #else U'ě' #endif ), {25, 12}, {{36, 8}, {112, 40}}); /* Convert the file */ Containers::Pointer converter = _fontConverterManager.instantiate("MagnumFontConverter"); CORRADE_VERIFY(converter->exportFontToFile(font, cache, Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font"), "Waveě")); /* Verify font parameters */ CORRADE_COMPARE_AS(confFilename, Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.conf"), TestSuite::Compare::File); if(!(_importerManager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_importerManager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found, not testing glyph cache contents"); /* Verify font image */ CORRADE_COMPARE_WITH(tgaFilename, Utility::Path::join(MAGNUMFONT_TEST_DIR, "font.tga"), DebugTools::CompareImageFile{_importerManager}); } void MagnumFontConverterTest::exportFontEmptyCache() { Containers::String confFilename = Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font-empty-cache.conf"); Containers::String tgaFilename = Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font-empty-cache.tga"); /* Remove previously created files */ if(Utility::Path::exists(confFilename)) CORRADE_VERIFY(Utility::Path::remove(confFilename)); if(Utility::Path::exists(tgaFilename)) CORRADE_VERIFY(Utility::Path::remove(tgaFilename)); MyFont font; font.openFile({}, {}); struct: AbstractGlyphCache { using AbstractGlyphCache::AbstractGlyphCache; GlyphCacheFeatures doFeatures() const override { return GlyphCacheFeature::ImageDownload; } void doSetImage(const Vector2i&, const ImageView2D&) override {} Image2D doImage() override { return Image2D{PixelFormat::R8Unorm, {8, 4}, Containers::Array{ValueInit, 8*4}}; } } cache{{8, 4}}; /* Convert the file */ Containers::Pointer converter = _fontConverterManager.instantiate("MagnumFontConverter"); CORRADE_VERIFY(converter->exportFontToFile(font, cache, Utility::Path::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font-empty-cache"), "Wave")); /* Verify font parameters */ CORRADE_COMPARE_AS(confFilename, Utility::Path::join(MAGNUMFONTCONVERTER_TEST_DIR, "font-empty-cache.conf"), TestSuite::Compare::File); if(!(_importerManager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_importerManager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found, not testing glyph cache contents"); /* Verify font image */ CORRADE_COMPARE_WITH(tgaFilename, Utility::Path::join(MAGNUMFONTCONVERTER_TEST_DIR, "font-empty-cache.tga"), DebugTools::CompareImageFile{_importerManager}); } void MagnumFontConverterTest::exportFontNoGlyphCacheImageDownload() { struct: AbstractFont { /* Supports neither file nor data opening */ FontFeatures doFeatures() const override { return {}; } bool doIsOpened() const override { return false; } void doClose() override {} UnsignedInt doGlyphId(char32_t) override { return {}; } Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } Containers::Pointer doLayout(const AbstractGlyphCache&, Float, Containers::StringView) override { return nullptr; } } font; struct: AbstractGlyphCache { using AbstractGlyphCache::AbstractGlyphCache; GlyphCacheFeatures doFeatures() const override { return {}; } void doSetImage(const Vector2i&, const ImageView2D&) override {} } cache{{100, 100}}; Containers::Pointer converter = _fontConverterManager.instantiate("MagnumFontConverter"); std::ostringstream out; Error redirectError{&out}; 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"); } }}}} CORRADE_TEST_MAIN(Magnum::Text::Test::MagnumFontConverterTest)