mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
6.6 KiB
153 lines
6.6 KiB
|
13 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
7 years ago
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
|
||
|
12 years ago
|
Vladimír Vondruš <mosra@centrum.cz>
|
||
|
13 years ago
|
|
||
|
|
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.
|
||
|
|
*/
|
||
|
|
|
||
|
7 years ago
|
#include <sstream>
|
||
|
|
#include <Corrade/Containers/Optional.h>
|
||
|
13 years ago
|
#include <Corrade/Utility/Directory.h>
|
||
|
7 years ago
|
#include <Corrade/TestSuite/Tester.h>
|
||
|
13 years ago
|
#include <Corrade/TestSuite/Compare/File.h>
|
||
|
13 years ago
|
|
||
|
7 years ago
|
#include "Magnum/Image.h"
|
||
|
11 years ago
|
#include "Magnum/PixelFormat.h"
|
||
|
7 years ago
|
#include "Magnum/Text/AbstractGlyphCache.h"
|
||
|
13 years ago
|
#include "Magnum/Text/AbstractFont.h"
|
||
|
8 years ago
|
#include "Magnum/Text/AbstractFontConverter.h"
|
||
|
|
#include "Magnum/Trade/AbstractImageConverter.h"
|
||
|
|
#include "Magnum/Trade/AbstractImporter.h"
|
||
|
13 years ago
|
#include "Magnum/Trade/ImageData.h"
|
||
|
13 years ago
|
|
||
|
13 years ago
|
#include "configure.h"
|
||
|
13 years ago
|
|
||
|
7 years ago
|
namespace Magnum { namespace Text { namespace Test { namespace {
|
||
|
13 years ago
|
|
||
|
7 years ago
|
struct MagnumFontConverterTest: TestSuite::Tester {
|
||
|
|
explicit MagnumFontConverterTest();
|
||
|
13 years ago
|
|
||
|
9 years ago
|
void exportFont();
|
||
|
8 years ago
|
|
||
|
|
/* Explicitly forbid system-wide plugin dependencies */
|
||
|
|
PluginManager::Manager<Trade::AbstractImageConverter> _imageConverterManager{"nonexistent"};
|
||
|
|
PluginManager::Manager<AbstractFontConverter> _fontConverterManager{"nonexistent"};
|
||
|
|
PluginManager::Manager<Trade::AbstractImporter> _importerManager{"nonexistent"};
|
||
|
13 years ago
|
};
|
||
|
|
|
||
|
7 years ago
|
MagnumFontConverterTest::MagnumFontConverterTest() {
|
||
|
|
addTests({&MagnumFontConverterTest::exportFont});
|
||
|
8 years ago
|
|
||
|
|
/* Load the plugins directly from the build tree. Otherwise they are static
|
||
|
|
and already loaded. */
|
||
|
|
#if defined(TGAIMAGECONVERTER_PLUGIN_FILENAME) && defined(MAGNUMFONTCONVERTER_PLUGIN_FILENAME)
|
||
|
|
CORRADE_INTERNAL_ASSERT(_imageConverterManager.load(TGAIMAGECONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded);
|
||
|
|
CORRADE_INTERNAL_ASSERT(_fontConverterManager.load(MAGNUMFONTCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded);
|
||
|
|
#endif
|
||
|
|
/* Optional plugins that don't have to be here */
|
||
|
|
#ifdef TGAIMPORTER_PLUGIN_FILENAME
|
||
|
|
CORRADE_INTERNAL_ASSERT(_importerManager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded);
|
||
|
|
#endif
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
7 years ago
|
void MagnumFontConverterTest::exportFont() {
|
||
|
13 years ago
|
/* Remove previously created files */
|
||
|
|
Utility::Directory::rm(Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.conf"));
|
||
|
|
Utility::Directory::rm(Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.tga"));
|
||
|
|
|
||
|
13 years ago
|
/* Fake font with fake cache */
|
||
|
|
class FakeFont: public Text::AbstractFont {
|
||
|
|
public:
|
||
|
13 years ago
|
explicit FakeFont(): _opened(false) {}
|
||
|
13 years ago
|
|
||
|
|
private:
|
||
|
13 years ago
|
void doClose() { _opened = false; }
|
||
|
|
bool doIsOpened() const { return _opened; }
|
||
|
10 years ago
|
Metrics doOpenFile(const std::string&, Float) {
|
||
|
13 years ago
|
_opened = true;
|
||
|
10 years ago
|
return {16.0f, 25.0f, -10.0f, 39.7333f};
|
||
|
13 years ago
|
}
|
||
|
13 years ago
|
Features doFeatures() const { return {}; }
|
||
|
7 years ago
|
Containers::Pointer<AbstractLayouter> doLayout(const AbstractGlyphCache&, Float, const std::string&) { return nullptr; }
|
||
|
13 years ago
|
|
||
|
|
UnsignedInt doGlyphId(const char32_t character) {
|
||
|
|
switch(character) {
|
||
|
|
case 'W': return 2;
|
||
|
|
case 'e': return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector2 doGlyphAdvance(const UnsignedInt glyph) {
|
||
|
|
switch(glyph) {
|
||
|
|
case 0: return {8, 0};
|
||
|
|
case 1: return {12, 0};
|
||
|
|
case 2: return {23, 0};
|
||
|
|
}
|
||
|
|
|
||
|
10 years ago
|
CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */
|
||
|
13 years ago
|
}
|
||
|
13 years ago
|
|
||
|
|
bool _opened;
|
||
|
13 years ago
|
} font;
|
||
|
13 years ago
|
font.openFile({}, {});
|
||
|
13 years ago
|
|
||
|
|
/* Create fake cache */
|
||
|
7 years ago
|
struct MyCache: AbstractGlyphCache {
|
||
|
|
explicit MyCache(): AbstractGlyphCache{Vector2i{1536}, Vector2i{24}} {}
|
||
|
|
|
||
|
|
GlyphCacheFeatures doFeatures() const override { return GlyphCacheFeature::ImageDownload; }
|
||
|
|
void doSetImage(const Vector2i&, const ImageView2D&) override {}
|
||
|
|
Image2D doImage() override {
|
||
|
|
return Image2D{PixelFormat::R8Unorm, Vector2i{256}, Containers::Array<char>{Containers::ValueInit, 256*256}};
|
||
|
|
}
|
||
|
|
} cache;
|
||
|
13 years ago
|
cache.insert(font.glyphId(U'W'), {25, 34}, {{0, 8}, {16, 128}});
|
||
|
|
cache.insert(font.glyphId(U'e'), {25, 12}, {{16, 4}, {64, 32}});
|
||
|
|
|
||
|
|
/* Convert the file */
|
||
|
7 years ago
|
Containers::Pointer<AbstractFontConverter> converter = _fontConverterManager.instantiate("MagnumFontConverter");
|
||
|
8 years ago
|
converter->exportFontToFile(font, cache, Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font"), "Wave");
|
||
|
13 years ago
|
|
||
|
|
/* Verify font parameters */
|
||
|
|
/** @todo This might behave differently elsewhere due to unspecified order of glyphs in cache */
|
||
|
|
CORRADE_COMPARE_AS(Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.conf"),
|
||
|
|
Utility::Directory::join(MAGNUMFONT_TEST_DIR, "font.conf"),
|
||
|
|
TestSuite::Compare::File);
|
||
|
|
|
||
|
8 years ago
|
if(!(_importerManager.loadState("TgaImporter") & PluginManager::LoadState::Loaded))
|
||
|
|
CORRADE_SKIP("TgaImporter plugin not enabled, not testing glyph cache contents");
|
||
|
|
|
||
|
7 years ago
|
/* Verify font image, no need to test image contents, as the image is
|
||
|
|
garbage anyway */
|
||
|
7 years ago
|
Containers::Pointer<Trade::AbstractImporter> importer = _importerManager.instantiate("TgaImporter");
|
||
|
8 years ago
|
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(MAGNUMFONTCONVERTER_TEST_WRITE_DIR, "font.tga")));
|
||
|
|
Containers::Optional<Trade::ImageData2D> image = importer->image2D(0);
|
||
|
13 years ago
|
CORRADE_VERIFY(image);
|
||
|
13 years ago
|
CORRADE_COMPARE(image->size(), Vector2i(256));
|
||
|
8 years ago
|
CORRADE_COMPARE(image->format(), PixelFormat::R8Unorm);
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
7 years ago
|
}}}}
|
||
|
13 years ago
|
|
||
|
7 years ago
|
CORRADE_TEST_MAIN(Magnum::Text::Test::MagnumFontConverterTest)
|