mirror of https://github.com/mosra/magnum.git
60 changed files with 3605 additions and 498 deletions
@ -0,0 +1,38 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "Math/Geometry/Rectangle.h" |
||||
|
||||
namespace Corrade { namespace Utility { |
||||
|
||||
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||
template struct ConfigurationValue<Magnum::Math::Geometry::Rectangle<Magnum::Float>>; |
||||
template struct ConfigurationValue<Magnum::Math::Geometry::Rectangle<Magnum::Int>>; |
||||
template struct ConfigurationValue<Magnum::Math::Geometry::Rectangle<Magnum::UnsignedInt>>; |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
template struct ConfigurationValue<Magnum::Math::Geometry::Rectangle<Magnum::Double>>; |
||||
#endif |
||||
#endif |
||||
|
||||
}} |
||||
@ -0,0 +1,56 @@
|
||||
#ifndef Magnum_Test_AbstractOpenGLTester_h |
||||
#define Magnum_Test_AbstractOpenGLTester_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <TestSuite/Tester.h> |
||||
|
||||
#include "Renderer.h" |
||||
|
||||
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_DESKTOP_GLES) |
||||
#include "Platform/WindowlessGlxApplication.h" |
||||
#else |
||||
#error Cannot run OpenGL tests on this platform |
||||
#endif |
||||
|
||||
namespace Magnum { namespace Test { |
||||
|
||||
class AbstractOpenGLTester: public TestSuite::Tester, public Platform::WindowlessApplication { |
||||
public: |
||||
explicit AbstractOpenGLTester(): Platform::WindowlessApplication({zero, nullptr}) {} |
||||
|
||||
using TestSuite::Tester::exec; |
||||
int exec() override { return TestSuite::Tester::exec(); } |
||||
|
||||
private: |
||||
static int zero; |
||||
}; |
||||
|
||||
int AbstractOpenGLTester::zero = 0; |
||||
|
||||
#define MAGNUM_VERIFY_NO_ERROR() CORRADE_COMPARE(Renderer::error(), Renderer::Error::NoError) |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,265 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <Containers/Array.h> |
||||
|
||||
#include "Buffer.h" |
||||
#include "Context.h" |
||||
#include "Extensions.h" |
||||
#include "Test/AbstractOpenGLTester.h" |
||||
|
||||
namespace Magnum { namespace Test { |
||||
|
||||
class BufferGLTest: public AbstractOpenGLTester { |
||||
public: |
||||
explicit BufferGLTest(); |
||||
|
||||
void construct(); |
||||
void data(); |
||||
void map(); |
||||
void mapRange(); |
||||
void mapRangeExplicitFlush(); |
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
void copy(); |
||||
#endif |
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
void invalidate(); |
||||
#endif |
||||
}; |
||||
|
||||
BufferGLTest::BufferGLTest() { |
||||
addTests({&BufferGLTest::construct, |
||||
&BufferGLTest::data, |
||||
&BufferGLTest::map, |
||||
&BufferGLTest::mapRange, |
||||
&BufferGLTest::mapRangeExplicitFlush, |
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
&BufferGLTest::copy, |
||||
#endif |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
&BufferGLTest::invalidate |
||||
#endif |
||||
}); |
||||
} |
||||
|
||||
void BufferGLTest::construct() { |
||||
Buffer buffer; |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
CORRADE_COMPARE(buffer.targetHint(), Buffer::Target::Array); |
||||
|
||||
CORRADE_COMPARE(buffer.size(), 0); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
} |
||||
|
||||
void BufferGLTest::data() { |
||||
Buffer buffer; |
||||
|
||||
constexpr Int data[] = {2, 7, 5, 13, 25}; |
||||
buffer.setData(5*4, data, Buffer::Usage::StaticDraw); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
CORRADE_COMPARE(buffer.size(), 5*4); |
||||
|
||||
/** @todo How to verify the contents in ES? */ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
const Containers::Array<Int> contents = buffer.data<Int>(); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
CORRADE_COMPARE(contents.size(), 5); |
||||
CORRADE_COMPARE(contents[0], 2); |
||||
CORRADE_COMPARE(contents[1], 7); |
||||
CORRADE_COMPARE(contents[2], 5); |
||||
CORRADE_COMPARE(contents[3], 13); |
||||
CORRADE_COMPARE(contents[4], 25); |
||||
#endif |
||||
|
||||
constexpr Int subData[] = {125, 3, 15}; |
||||
buffer.setSubData(4, 3*4, subData); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
CORRADE_COMPARE(buffer.size(), 5*4); |
||||
|
||||
/** @todo How to verify the contents in ES? */ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
const Containers::Array<Int> subContents = buffer.subData<Int>(4, 3); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
CORRADE_COMPARE(subContents.size(), 3); |
||||
CORRADE_COMPARE(subContents[0], 125); |
||||
CORRADE_COMPARE(subContents[1], 3); |
||||
CORRADE_COMPARE(subContents[2], 15); |
||||
#endif |
||||
} |
||||
|
||||
#ifndef MAGNUM_TARGET_GLES3 |
||||
void BufferGLTest::map() { |
||||
#ifdef MAGNUM_TARGET_GLES2 |
||||
if(!Context::current()->isExtensionSupported<Extensions::GL::OES::mapbuffer>()) |
||||
CORRADE_SKIP(Extensions::GL::OES::mapbuffer::string() + std::string(" is not supported")); |
||||
#endif |
||||
Buffer buffer; |
||||
|
||||
constexpr char data[] = {2, 7, 5, 13, 25}; |
||||
buffer.setData(data, Buffer::Usage::StaticDraw); |
||||
|
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
char* contents = reinterpret_cast<char*>(buffer.map(Buffer::MapAccess::ReadWrite)); |
||||
#else |
||||
char* contents = reinterpret_cast<char*>(buffer.map(Buffer::MapAccess::WriteOnly)); |
||||
#endif |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
CORRADE_VERIFY(contents); |
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
CORRADE_COMPARE(contents[2], 5); |
||||
#endif |
||||
contents[3] = 107; |
||||
|
||||
CORRADE_VERIFY(buffer.unmap()); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
/** @todo How to verify the contents in ES? */ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
Containers::Array<char> changedContents = buffer.data<char>(); |
||||
CORRADE_COMPARE(changedContents.size(), 5); |
||||
CORRADE_COMPARE(changedContents[3], 107); |
||||
#endif |
||||
} |
||||
#endif |
||||
|
||||
void BufferGLTest::mapRange() { |
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::map_buffer_range>()) |
||||
CORRADE_SKIP(Extensions::GL::ARB::map_buffer_range::string() + std::string(" is not supported")); |
||||
#else |
||||
if(!Context::current()->isExtensionSupported<Extensions::GL::EXT::map_buffer_range>()) |
||||
CORRADE_SKIP(Extensions::GL::EXT::map_buffer_range::string() + std::string(" is not supported")); |
||||
#endif |
||||
|
||||
constexpr char data[] = {2, 7, 5, 13, 25}; |
||||
Buffer buffer; |
||||
buffer.setData(data, Buffer::Usage::StaticDraw); |
||||
|
||||
char* contents = reinterpret_cast<char*>(buffer.map(1, 4, Buffer::MapFlag::Read|Buffer::MapFlag::Write)); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
CORRADE_VERIFY(contents); |
||||
CORRADE_COMPARE(contents[2], 13); |
||||
contents[3] = 107; |
||||
|
||||
CORRADE_VERIFY(buffer.unmap()); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
/** @todo How to verify the contents in ES? */ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
Containers::Array<char> changedContents = buffer.data<char>(); |
||||
CORRADE_COMPARE(changedContents.size(), 5); |
||||
CORRADE_COMPARE(changedContents[4], 107); |
||||
#endif |
||||
} |
||||
|
||||
void BufferGLTest::mapRangeExplicitFlush() { |
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::map_buffer_range>()) |
||||
CORRADE_SKIP(Extensions::GL::ARB::map_buffer_range::string() + std::string(" is not supported")); |
||||
#else |
||||
if(!Context::current()->isExtensionSupported<Extensions::GL::EXT::map_buffer_range>()) |
||||
CORRADE_SKIP(Extensions::GL::EXT::map_buffer_range::string() + std::string(" is not supported")); |
||||
#endif |
||||
|
||||
constexpr char data[] = {2, 7, 5, 13, 25}; |
||||
Buffer buffer; |
||||
buffer.setData(data, Buffer::Usage::StaticDraw); |
||||
|
||||
/* Map, set byte, don't flush and unmap */ |
||||
char* contents = reinterpret_cast<char*>(buffer.map(1, 4, Buffer::MapFlag::Write|Buffer::MapFlag::FlushExplicit)); |
||||
CORRADE_VERIFY(contents); |
||||
contents[2] = 99; |
||||
CORRADE_VERIFY(buffer.unmap()); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
/* Unflushed range _might_ not be changed, thus nothing to test */ |
||||
|
||||
/* Map, set byte, flush and unmap */ |
||||
contents = reinterpret_cast<char*>(buffer.map(1, 4, Buffer::MapFlag::Write|Buffer::MapFlag::FlushExplicit)); |
||||
CORRADE_VERIFY(contents); |
||||
contents[3] = 107; |
||||
buffer.flushMappedRange(3, 1); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
CORRADE_VERIFY(buffer.unmap()); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
/* Flushed range should be changed */ |
||||
/** @todo How to verify the contents in ES? */ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
Containers::Array<char> changedContents = buffer.data<char>(); |
||||
CORRADE_COMPARE(changedContents.size(), 5); |
||||
CORRADE_COMPARE(changedContents[4], 107); |
||||
#endif |
||||
} |
||||
|
||||
#ifndef MAGNUM_TARGET_GLES2 |
||||
void BufferGLTest::copy() { |
||||
Buffer buffer1; |
||||
constexpr char data[] = {2, 7, 5, 13, 25}; |
||||
buffer1.setData(data, Buffer::Usage::StaticDraw); |
||||
|
||||
Buffer buffer2; |
||||
buffer2.setData(5, nullptr, Buffer::Usage::StaticDraw); |
||||
|
||||
Buffer::copy(&buffer1, &buffer2, 1, 2, 3); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
/** @todo How to verify the contents in ES? */ |
||||
#ifndef MAGNUM_TARGET_GLES |
||||
const Containers::Array<char> subContents = buffer2.subData<char>(2, 3); |
||||
CORRADE_COMPARE(subContents.size(), 3); |
||||
CORRADE_COMPARE(subContents[0], 7); |
||||
CORRADE_COMPARE(subContents[1], 5); |
||||
CORRADE_COMPARE(subContents[2], 13); |
||||
#endif |
||||
} |
||||
#endif |
||||
|
||||
#ifndef MAGNUM_TARGET_GLES |
||||
void BufferGLTest::invalidate() { |
||||
if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::invalidate_subdata>()) |
||||
CORRADE_SKIP(Extensions::GL::ARB::invalidate_subdata::string() + std::string(" is not supported")); |
||||
|
||||
Buffer buffer; |
||||
constexpr char data[] = {2, 7, 5, 13, 25}; |
||||
buffer.setData(data, Buffer::Usage::StaticDraw); |
||||
|
||||
/* Just test that no errors are emitted */ |
||||
|
||||
buffer.invalidateSubData(3, 2); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
buffer.invalidateData(); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
} |
||||
#endif |
||||
|
||||
}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Test::BufferGLTest) |
||||
@ -0,0 +1,85 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <TestSuite/Tester.h> |
||||
|
||||
#include "Image.h" |
||||
#include "ImageFormat.h" |
||||
|
||||
namespace Magnum { namespace Test { |
||||
|
||||
class ImageTest: public TestSuite::Tester { |
||||
public: |
||||
explicit ImageTest(); |
||||
|
||||
void moveConstructor(); |
||||
void moveAssignment(); |
||||
void toReference(); |
||||
}; |
||||
|
||||
ImageTest::ImageTest() { |
||||
addTests({&ImageTest::moveConstructor, |
||||
&ImageTest::moveAssignment, |
||||
&ImageTest::toReference}); |
||||
} |
||||
|
||||
void ImageTest::moveConstructor() { |
||||
unsigned char* data = new unsigned char[3]; |
||||
Image2D a(ImageFormat::Red, ImageType::UnsignedByte, {1, 3}, data); |
||||
|
||||
Image2D b(std::move(a)); |
||||
CORRADE_VERIFY(!a.data()); |
||||
CORRADE_COMPARE(b.format(), ImageFormat::Red); |
||||
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte); |
||||
CORRADE_COMPARE(b.size(), Vector2i(1, 3)); |
||||
CORRADE_VERIFY(b.data() == data); |
||||
} |
||||
|
||||
void ImageTest::moveAssignment() { |
||||
unsigned char* data = new unsigned char[3]; |
||||
Image2D a(ImageFormat::Red, ImageType::UnsignedByte, {1, 3}, data); |
||||
|
||||
Image2D b(ImageFormat::Red, ImageType::UnsignedByte); |
||||
b = std::move(a); |
||||
CORRADE_VERIFY(!a.data()); |
||||
CORRADE_COMPARE(b.format(), ImageFormat::Red); |
||||
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte); |
||||
CORRADE_COMPARE(b.size(), Vector2i(1, 3)); |
||||
CORRADE_VERIFY(b.data() == data); |
||||
} |
||||
|
||||
void ImageTest::toReference() { |
||||
unsigned char* data = new unsigned char[3]; |
||||
Image2D a(ImageFormat::Red, ImageType::UnsignedByte, {1, 3}, data); |
||||
|
||||
ImageReference2D b = a; |
||||
CORRADE_COMPARE(b.format(), ImageFormat::Red); |
||||
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte); |
||||
CORRADE_COMPARE(b.size(), Vector2i(1, 3)); |
||||
CORRADE_VERIFY(b.data() == data); |
||||
} |
||||
|
||||
}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Test::ImageTest) |
||||
@ -0,0 +1,228 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "AbstractFontConverter.h" |
||||
|
||||
#include <algorithm> |
||||
#include <fstream> |
||||
#include <Containers/Array.h> |
||||
#include <Utility/Assert.h> |
||||
#include <Utility/Unicode.h> |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
AbstractFontConverter::AbstractFontConverter() = default; |
||||
|
||||
AbstractFontConverter::AbstractFontConverter(PluginManager::AbstractManager* manager, std::string plugin): PluginManager::AbstractPlugin(manager, std::move(plugin)) {} |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::exportFontToData(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::string& characters) const { |
||||
CORRADE_ASSERT(features() >= (Feature::ExportFont|Feature::ConvertData), |
||||
"Text::AbstractFontConverter::exportFontToData(): feature not supported", {}); |
||||
|
||||
return doExportFontToData(font, cache, filename, uniqueUnicode(characters)); |
||||
} |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::doExportFontToData(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::u32string& characters) const { |
||||
CORRADE_ASSERT(!(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::exportFontToData(): feature advertised but not implemented", {}); |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> out; |
||||
out.emplace_back(filename, std::move(doExportFontToSingleData(font, cache, characters))); |
||||
return std::move(out); |
||||
} |
||||
|
||||
Containers::Array<unsigned char> AbstractFontConverter::exportFontToSingleData(AbstractFont* const font, GlyphCache* const cache, const std::string& characters) const { |
||||
CORRADE_ASSERT(features() >= (Feature::ExportFont|Feature::ConvertData), |
||||
"Text::AbstractFontConverter::exportFontToSingleData(): feature not supported", nullptr); |
||||
CORRADE_ASSERT(!(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::exportFontToSingleData(): the format is not single-file", nullptr); |
||||
|
||||
return doExportFontToSingleData(font, cache, uniqueUnicode(characters)); |
||||
} |
||||
|
||||
Containers::Array<unsigned char> AbstractFontConverter::doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string&) const { |
||||
CORRADE_ASSERT(false, |
||||
"Text::AbstractFontConverter::exportFontToSingleData(): feature advertised but not implemented", nullptr); |
||||
} |
||||
|
||||
bool AbstractFontConverter::exportFontToFile(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::string& characters) const { |
||||
CORRADE_ASSERT(features() & Feature::ExportFont, |
||||
"Text::AbstractFontConverter::exportFontToFile(): feature not supported", false); |
||||
|
||||
return doExportFontToFile(font, cache, filename, uniqueUnicode(characters)); |
||||
} |
||||
|
||||
bool AbstractFontConverter::doExportFontToFile(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::u32string& characters) const { |
||||
CORRADE_ASSERT(features() & Feature::ConvertData, |
||||
"Text::AbstractFontConverter::exportFontToFile(): not implemented", false); |
||||
|
||||
/* Export all data */ |
||||
const auto data = doExportFontToData(font, cache, filename, characters); |
||||
for(const auto& d: data) { |
||||
/* Open file */ |
||||
std::ofstream out(d.first.data(), std::ios::binary); |
||||
if(!out.good()) { |
||||
Error() << "Text::AbstractFontConverter::exportFontToFile(): cannot write to file" << d.first; |
||||
return false; |
||||
} |
||||
|
||||
/* Write data, close */ |
||||
out.write(reinterpret_cast<const char*>(d.second.begin()), d.second.size()); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::exportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const { |
||||
CORRADE_ASSERT(features() >= (Feature::ExportGlyphCache|Feature::ConvertData), |
||||
"Text::AbstractFontConverter::exportGlyphCacheToData(): feature not supported", {}); |
||||
|
||||
return doExportGlyphCacheToData(cache, filename); |
||||
} |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::doExportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const { |
||||
CORRADE_ASSERT(!(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::exportGlyphCacheToData(): feature advertised but not implemented", {}); |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> out; |
||||
out.emplace_back(filename, std::move(doExportGlyphCacheToSingleData(cache))); |
||||
return std::move(out); |
||||
} |
||||
|
||||
Containers::Array<unsigned char> AbstractFontConverter::exportGlyphCacheToSingleData(GlyphCache* cache) const { |
||||
CORRADE_ASSERT(features() >= (Feature::ExportGlyphCache|Feature::ConvertData), |
||||
"Text::AbstractFontConverter::exportGlyphCacheToSingleData(): feature not supported", nullptr); |
||||
CORRADE_ASSERT(!(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::exportGlyphCacheToSingleData(): the format is not single-file", nullptr); |
||||
|
||||
return doExportGlyphCacheToSingleData(cache); |
||||
} |
||||
|
||||
Containers::Array<unsigned char> AbstractFontConverter::doExportGlyphCacheToSingleData(GlyphCache*) const { |
||||
CORRADE_ASSERT(false, |
||||
"Text::AbstractFontConverter::exportGlyphCacheToSingleData(): feature advertised but not implemented", nullptr); |
||||
} |
||||
|
||||
bool AbstractFontConverter::exportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const { |
||||
CORRADE_ASSERT(features() & Feature::ExportGlyphCache, |
||||
"Text::AbstractFontConverter::exportGlyphCacheToFile(): feature not supported", false); |
||||
|
||||
return doExportGlyphCacheToFile(cache, filename); |
||||
} |
||||
|
||||
bool AbstractFontConverter::doExportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const { |
||||
CORRADE_ASSERT(features() & Feature::ConvertData, |
||||
"Text::AbstractFontConverter::exportGlyphCacheToFile(): not implemented", false); |
||||
|
||||
/* Export all data */ |
||||
const auto data = doExportGlyphCacheToData(cache, filename); |
||||
for(const auto& d: data) { |
||||
/* Open file */ |
||||
std::ofstream out(d.first.data(), std::ios::binary); |
||||
if(!out.good()) { |
||||
Error() << "Text::AbstractFontConverter::exportGlyphCacheToFile(): cannot write to file" << d.first; |
||||
return false; |
||||
} |
||||
|
||||
/* Write data, close */ |
||||
out.write(reinterpret_cast<const char*>(d.second.begin()), d.second.size()); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
GlyphCache* AbstractFontConverter::importGlyphCacheFromData(const std::vector<std::pair<std::string, Containers::ArrayReference<const unsigned char>>>& data) const { |
||||
CORRADE_ASSERT(features() >= (Feature::ImportGlyphCache|Feature::ConvertData), |
||||
"Text::AbstractFontConverter::importGlyphCacheFromData(): feature not supported", nullptr); |
||||
CORRADE_ASSERT(!data.empty(), |
||||
"Text::AbstractFontConverter::importGlyphCacheFromData(): no data passed", nullptr); |
||||
|
||||
return doImportGlyphCacheFromData(data); |
||||
} |
||||
|
||||
GlyphCache* AbstractFontConverter::doImportGlyphCacheFromData(const std::vector<std::pair<std::string, Containers::ArrayReference<const unsigned char>>>& data) const { |
||||
CORRADE_ASSERT(!(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::importGlyphCacheFromData(): feature advertised but not implemented", nullptr); |
||||
CORRADE_ASSERT(data.size() == 1, |
||||
"Text::AbstractFontConverter::importGlyphCacheFromData(): expected just one file for single-file format", nullptr); |
||||
|
||||
return doImportGlyphCacheFromSingleData(data[0].second); |
||||
} |
||||
|
||||
GlyphCache* AbstractFontConverter::importGlyphCacheFromSingleData(Containers::ArrayReference<const unsigned char> data) const { |
||||
CORRADE_ASSERT(features() >= (Feature::ImportGlyphCache|Feature::ConvertData), |
||||
"Text::AbstractFontConverter::importGlyphCacheFromSingleData(): feature not supported", nullptr); |
||||
CORRADE_ASSERT(!(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::importGlyphCacheFromSingleData(): the format is not single-file", nullptr); |
||||
|
||||
return doImportGlyphCacheFromSingleData(data); |
||||
} |
||||
|
||||
GlyphCache* AbstractFontConverter::doImportGlyphCacheFromSingleData(Containers::ArrayReference<const unsigned char>) const { |
||||
CORRADE_ASSERT(false, |
||||
"Text::AbstractFontConverter::importGlyphCacheFromSingleData(): feature advertised but not implemented", nullptr); |
||||
} |
||||
|
||||
GlyphCache* AbstractFontConverter::importGlyphCacheFromFile(const std::string& filename) const { |
||||
CORRADE_ASSERT(features() & Feature::ImportGlyphCache, |
||||
"Text::AbstractFontConverter::importGlyphCacheFromFile(): feature not supported", nullptr); |
||||
|
||||
return doImportGlyphCacheFromFile(filename); |
||||
} |
||||
|
||||
GlyphCache* AbstractFontConverter::doImportGlyphCacheFromFile(const std::string& filename) const { |
||||
CORRADE_ASSERT(features() & Feature::ConvertData && !(features() & Feature::MultiFile), |
||||
"Text::AbstractFontConverter::importGlyphCacheFromFile(): not implemented", nullptr); |
||||
|
||||
/* Open file */ |
||||
std::ifstream in(filename.data(), std::ios::binary); |
||||
if(!in.good()) { |
||||
Error() << "Trade::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file" << filename; |
||||
return nullptr; |
||||
} |
||||
|
||||
/* Create array to hold file contents */ |
||||
in.seekg(0, std::ios::end); |
||||
Containers::Array<unsigned char> data(in.tellg()); |
||||
|
||||
/* Read data, close */ |
||||
in.seekg(0, std::ios::beg); |
||||
in.read(reinterpret_cast<char*>(data.begin()), data.size()); |
||||
in.close(); |
||||
|
||||
return doImportGlyphCacheFromSingleData(data); |
||||
} |
||||
|
||||
std::u32string AbstractFontConverter::uniqueUnicode(const std::string& characters) { |
||||
/* Convert UTF-8 to UTF-32 */ |
||||
std::u32string result = Utility::Unicode::utf32(characters); |
||||
|
||||
/* Remove duplicate glyphs */ |
||||
std::sort(result.begin(), result.end()); |
||||
result.erase(std::unique(result.begin(), result.end()), result.end()); |
||||
|
||||
return std::move(result); |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,317 @@
|
||||
#ifndef Magnum_Text_AbstractFontConverter_h |
||||
#define Magnum_Text_AbstractFontConverter_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class Magnum::Text::AbstractFontConverter |
||||
*/ |
||||
|
||||
#include <PluginManager/AbstractPlugin.h> |
||||
|
||||
#include "Magnum.h" |
||||
#include "Text/Text.h" |
||||
#include "Text/magnumTextVisibility.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
/**
|
||||
@brief Base for font converter plugins |
||||
|
||||
Provides functionality for converting arbitrary font to different format. |
||||
|
||||
@section AbstractFontConverter-subclassing Subclassing |
||||
|
||||
Plugin implements doFeatures() and one or more of `exportTo*()` / `importFrom*()` |
||||
functions based on what features are supported. Characters passed to font |
||||
exporting functions are converted to list of unique UTF-32 characters. |
||||
|
||||
You don't need to do most of the redundant sanity checks, these things are |
||||
checked by the implementation: |
||||
|
||||
- Functions `doExportFontTo*()` are called only if @ref Feature "Feature::ExportFont" |
||||
is supported, functions `doExportGlyphCacheTo*()` are called only if |
||||
@ref Feature "Feature::ExportGlyphCache" is supported. |
||||
- Functions `doImportGlyphCacheFrom*()` are called only if |
||||
@ref Feature "Feature::ImportGlyphCache" is supported. |
||||
- Functions `doExport*To*Data()` and `doImport*From*Data()` are called only |
||||
if @ref Feature "Feature::ConvertData" is supported. |
||||
- Function `doImport*FromData()` is called only if there is at least one data |
||||
array passed. |
||||
*/ |
||||
class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPlugin { |
||||
CORRADE_PLUGIN_INTERFACE("cz.mosra.magnum.Text.AbstractFontConverter/0.1") |
||||
|
||||
public: |
||||
/**
|
||||
* @brief Features supported by this converter |
||||
* |
||||
* @see Features, features() |
||||
*/ |
||||
enum class Feature: UnsignedByte { |
||||
/**
|
||||
* Exporting font using exportToFile() or exportToData() |
||||
* @see @ref Feature "Feature::ConvertData" |
||||
*/ |
||||
ExportFont = 1 << 0, |
||||
|
||||
/**
|
||||
* Export glyph cache using exportToFile() or exportToData() |
||||
* @see @ref Feature "Feature::ConvertData" |
||||
*/ |
||||
ExportGlyphCache = 1 << 1, |
||||
|
||||
/**
|
||||
* Import glyph cache using importFromFile() or importFromData() |
||||
* @see @ref Feature "Feature::ConvertData" |
||||
*/ |
||||
ImportGlyphCache = 1 << 2, |
||||
|
||||
/** Convert from/to data using exportToData() or importFromData() */ |
||||
ConvertData = 1 << 4, |
||||
|
||||
/**
|
||||
* The format is multi-file, thus exportToSingleData() and |
||||
* importFromSingleData() convenience functions cannot be used. |
||||
*/ |
||||
MultiFile = 1 << 5 |
||||
}; |
||||
|
||||
/**
|
||||
* @brief Features supported by this converter |
||||
* |
||||
* @see features() |
||||
*/ |
||||
typedef Containers::EnumSet<Feature, UnsignedByte> Features; |
||||
|
||||
/** @brief Default constructor */ |
||||
explicit AbstractFontConverter(); |
||||
|
||||
/** @brief Plugin manager constructor */ |
||||
explicit AbstractFontConverter(PluginManager::AbstractManager* manager, std::string plugin); |
||||
|
||||
/** @brief Features supported by this converter */ |
||||
Features features() const { return doFeatures(); } |
||||
|
||||
/**
|
||||
* @brief Export font to raw data |
||||
* @param font Opened font |
||||
* @param cache Populated glyph cache |
||||
* @param filename Output filename |
||||
* @param characters Characters to export |
||||
* |
||||
* Available only if @ref Feature "Feature::ConvertData" and |
||||
* @ref Feature "Feature::ExportFont" is supported. Returns pairs of |
||||
* filename and data on success, empty vector otherwise. All data will |
||||
* be sharing common basename derived from @p filename. If the plugin |
||||
* doesn't have @ref Feature "Feature::MultiFile", only one pair is |
||||
* returned, thus using exportFontToSingleData() might be more convenient |
||||
* in that case. |
||||
* @see features(), exportFontToFile(), exportGlyphCacheToData() |
||||
*/ |
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> exportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::string& characters) const; |
||||
|
||||
/**
|
||||
* @brief Export font to single raw data |
||||
* |
||||
* Available only if @ref Feature "Feature::ConvertData" and |
||||
* @ref Feature "Feature::ExportFont" is supported and the plugin |
||||
* doesn't have @ref Feature "Feature::MultiFile". Returns data on |
||||
* success, zero-sized array otherwise. See exportFontToData() for |
||||
* more information. |
||||
* @see features(), exportFontToFile(), importFromSingleData() |
||||
*/ |
||||
Containers::Array<unsigned char> exportFontToSingleData(AbstractFont* font, GlyphCache* cache, const std::string& characters) const; |
||||
|
||||
/**
|
||||
* @brief Export font to file |
||||
* |
||||
* Available only if @ref Feature "Feature::ExportFont" is supported. |
||||
* If the plugin has @ref Feature "Feature::MultiFile", the function |
||||
* will create more than one file in given path, all sharing common |
||||
* basename derived from @p filename. Returns `true` on success, |
||||
* `false` otherwise. See exportFontToData() for more information. |
||||
* @see features(), exportFontToData(), exportGlyphCacheToFile() |
||||
*/ |
||||
bool exportFontToFile(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::string& characters) const; |
||||
|
||||
/**
|
||||
* @brief Export glyph cache to raw data |
||||
* @param cache Populated glyph cache |
||||
* @param filename Output filename |
||||
* |
||||
* Available only if @ref Feature "Feature::ConvertData" and |
||||
* @ref Feature "Feature::ExportGlyphCache" is supported. Returns pairs |
||||
* of filename and data on success, empty vector otherwise. All data |
||||
* will be sharing common basename derived from @p filename. If the |
||||
* plugin doesn't have @ref Feature "Feature::MultiFile", only one pair |
||||
* is returned, thus using exportGlyphCacheToSingleData() might be more |
||||
* convenient in that case. |
||||
* |
||||
* All glyphs from given cache will be exported. If you want to export |
||||
* smaller subset, fill the cache with less characters. |
||||
* @see features(), exportGlyphCacheToFile(), exportFontToData() |
||||
*/ |
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> exportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const; |
||||
|
||||
/**
|
||||
* @brief Export glyph cache to single raw data |
||||
* |
||||
* Available only if @ref Feature "Feature::ConvertData" and |
||||
* @ref Feature "Feature::ExportGlyphCache" is supported and the plugin |
||||
* doesn't have @ref Feature "Feature::MultiFile". Returns data on |
||||
* success, zero-sized array otherwise. See exportGlyphCacheToData() |
||||
* for more information. |
||||
* @see features(), exportGlyphCacheToFile(), importGlyphCacheFromSingleData() |
||||
*/ |
||||
Containers::Array<unsigned char> exportGlyphCacheToSingleData(GlyphCache* cache) const; |
||||
|
||||
/**
|
||||
* @brief Export glyph cache to file |
||||
* |
||||
* Available only if @ref Feature "Feature::ExportGlyphCache" is |
||||
* supported. If the plugin has @ref Feature "Feature::MultiFile", the |
||||
* function will create more than one file in given path, all sharing |
||||
* common basename derived from @p filename. Returns `true` on success, |
||||
* `false` otherwise. |
||||
* @see features(), exportGlyphCacheToData(), exportFontToFile() |
||||
*/ |
||||
bool exportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const; |
||||
|
||||
/**
|
||||
* @brief Import glyph cache from raw data |
||||
* @param data Pairs of filename and file data |
||||
* |
||||
* Available only if @ref Feature "Feature::ConvertData" and |
||||
* @ref Feature "Feature::ImportGlyphCache" is supported. Returns |
||||
* imported cache on success, `nullptr` otherwise. If the plugin |
||||
* doesn't have @ref Feature "Feature::MultiFile", only one file is |
||||
* needed, thus using convertToSingleData() might be more convenient in |
||||
* that case. |
||||
* @see features(), importFromFile(), exportToData() |
||||
*/ |
||||
GlyphCache* importGlyphCacheFromData(const std::vector<std::pair<std::string, Containers::ArrayReference<const unsigned char>>>& data) const; |
||||
|
||||
/**
|
||||
* @brief Import glyph cache from single raw data |
||||
* |
||||
* Available only if @ref Feature "Feature::ConvertData" and |
||||
* @ref Feature "Feature::ImportGlyphCache" is supported and the plugin |
||||
* doesn't have @ref Feature "Feature::MultiFile". Returns imported |
||||
* cache on success, `nullptr` otherwise. See importFromData() for |
||||
* multi-file conversion. |
||||
* @see features(), importFromFile(), exportToSingleData() |
||||
*/ |
||||
GlyphCache* importGlyphCacheFromSingleData(Containers::ArrayReference<const unsigned char> data) const; |
||||
|
||||
/**
|
||||
* @brief Import glyph cache from file |
||||
* |
||||
* Available only if @ref Feature "Feature::ImportGlyphCache" is |
||||
* supported. If the plugin has @ref Feature "Feature::MultiFile", the |
||||
* function will use additional files in given path, all sharing common |
||||
* basename derived from @p filename. Returns imported cache on |
||||
* success, `nullptr` otherwise. |
||||
* @see features(), importFromData(), exportToFile() |
||||
*/ |
||||
GlyphCache* importGlyphCacheFromFile(const std::string& filename) const; |
||||
|
||||
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||
private: |
||||
#else |
||||
protected: |
||||
#endif |
||||
/** @brief Implementation for features() */ |
||||
virtual Features doFeatures() const = 0; |
||||
|
||||
/**
|
||||
* @brief Implementation for exportFontToData() |
||||
* |
||||
* If the plugin doesn't have @ref Feature "Feature::MultiFile", |
||||
* default implementation calls doExportFontToSingleData(). |
||||
*/ |
||||
virtual std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::u32string& characters) const; |
||||
|
||||
/** @brief Implementation for exportFontToSingleData() */ |
||||
virtual Containers::Array<unsigned char> doExportFontToSingleData(AbstractFont* font, GlyphCache* cache, const std::u32string& characters) const; |
||||
|
||||
/**
|
||||
* @brief Implementation for exportFontToFile() |
||||
* |
||||
* If @ref Feature "Feature::ConvertData" is supported, default |
||||
* implementation calls doExportFontToData() and saves the result to |
||||
* given file(s). |
||||
*/ |
||||
virtual bool doExportFontToFile(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::u32string& characters) const; |
||||
|
||||
/**
|
||||
* @brief Implementation for exportGlyphCacheToData() |
||||
* |
||||
* If the plugin doesn't have @ref Feature "Feature::MultiFile", |
||||
* default implementation calls doExportGlyphCacheToSingleData(). |
||||
*/ |
||||
virtual std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const; |
||||
|
||||
/** @brief Implementation for exportGlyphCacheToSingleData() */ |
||||
virtual Containers::Array<unsigned char> doExportGlyphCacheToSingleData(GlyphCache* cache) const; |
||||
|
||||
/**
|
||||
* @brief Implementation for exportGlyphCacheToFile() |
||||
* |
||||
* If @ref Feature "Feature::ConvertData" is supported, default |
||||
* implementation calls doExportGlyphCacheToData() and saves the result |
||||
* to given file(s). |
||||
*/ |
||||
virtual bool doExportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const; |
||||
|
||||
/**
|
||||
* @brief Implementation for importGlyphCacheFromData() |
||||
* |
||||
* If the plugin doesn't have @ref Feature "Feature::MultiFile", |
||||
* default implementation calls doImportGlyphCacheFromSingleData(). |
||||
*/ |
||||
virtual GlyphCache* doImportGlyphCacheFromData(const std::vector<std::pair<std::string, Containers::ArrayReference<const unsigned char>>>& data) const; |
||||
|
||||
/** @brief Implementation for importGlyphCacheFromSingleData() */ |
||||
virtual GlyphCache* doImportGlyphCacheFromSingleData(Containers::ArrayReference<const unsigned char> data) const; |
||||
|
||||
/**
|
||||
* @brief Implementation for importGlyphCacheFromFile() |
||||
* |
||||
* If @ref Feature "Feature::ConvertData" is supported and the plugin |
||||
* doesn't have @ref Feature "Feature::MultiFile", default |
||||
* implementation opens the file and calls doImportGlyphCacheFromSingleData() |
||||
* with its contents. |
||||
*/ |
||||
virtual GlyphCache* doImportGlyphCacheFromFile(const std::string& filename) const; |
||||
|
||||
private: |
||||
MAGNUM_TEXT_LOCAL static std::u32string uniqueUnicode(const std::string& characters); |
||||
}; |
||||
|
||||
CORRADE_ENUMSET_OPERATORS(AbstractFontConverter::Features) |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,229 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <Containers/Array.h> |
||||
#include <TestSuite/Tester.h> |
||||
#include <TestSuite/Compare/FileToString.h> |
||||
#include <Utility/Directory.h> |
||||
|
||||
#include "Text/AbstractFontConverter.h" |
||||
|
||||
#include "testConfigure.h" |
||||
|
||||
namespace Magnum { namespace Text { namespace Test { |
||||
|
||||
class AbstractFontConverterTest: public TestSuite::Tester { |
||||
public: |
||||
explicit AbstractFontConverterTest(); |
||||
|
||||
void convertGlyphs(); |
||||
|
||||
void exportFontToSingleData(); |
||||
void exportFontToFile(); |
||||
|
||||
void exportGlyphCacheToSingleData(); |
||||
void exportGlyphCacheToFile(); |
||||
|
||||
void importGlyphCacheFromSingleData(); |
||||
void importGlyphCacheFromFile(); |
||||
}; |
||||
|
||||
AbstractFontConverterTest::AbstractFontConverterTest() { |
||||
addTests({&AbstractFontConverterTest::convertGlyphs, |
||||
|
||||
&AbstractFontConverterTest::exportFontToSingleData, |
||||
&AbstractFontConverterTest::exportFontToFile, |
||||
|
||||
&AbstractFontConverterTest::exportGlyphCacheToSingleData, |
||||
&AbstractFontConverterTest::exportGlyphCacheToFile, |
||||
|
||||
&AbstractFontConverterTest::importGlyphCacheFromSingleData, |
||||
&AbstractFontConverterTest::importGlyphCacheFromFile}); |
||||
} |
||||
|
||||
void AbstractFontConverterTest::convertGlyphs() { |
||||
class GlyphExporter: public AbstractFontConverter { |
||||
public: |
||||
GlyphExporter(std::u32string& characters): characters(characters) {} |
||||
|
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont; } |
||||
|
||||
Containers::Array<unsigned char> doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string& characters) const override { |
||||
this->characters = characters; |
||||
return {}; |
||||
} |
||||
|
||||
std::u32string& characters; |
||||
}; |
||||
|
||||
std::u32string characters; |
||||
GlyphExporter exporter(characters); |
||||
exporter.exportFontToSingleData(nullptr, nullptr, "abC01a0 "); |
||||
CORRADE_COMPARE(characters, (std::u32string{ |
||||
U' ', U'0', U'1', U'C', U'a', U'b'})); |
||||
} |
||||
|
||||
void AbstractFontConverterTest::exportFontToSingleData() { |
||||
class SingleDataExporter: public Text::AbstractFontConverter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont; } |
||||
|
||||
Containers::Array<unsigned char> doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string&) const override { |
||||
Containers::Array<unsigned char> data(1); |
||||
data[0] = 0xee; |
||||
return std::move(data); |
||||
} |
||||
}; |
||||
|
||||
/* doExportFontToData() should call doExportFontToSingleData() */ |
||||
SingleDataExporter exporter; |
||||
auto ret = exporter.exportFontToData(nullptr, nullptr, "font.out", {}); |
||||
CORRADE_COMPARE(ret.size(), 1); |
||||
CORRADE_COMPARE(ret[0].first, "font.out"); |
||||
CORRADE_COMPARE(ret[0].second.size(), 1); |
||||
CORRADE_COMPARE(ret[0].second[0], 0xee); |
||||
} |
||||
|
||||
void AbstractFontConverterTest::exportFontToFile() { |
||||
class DataExporter: public Text::AbstractFontConverter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont|Feature::MultiFile; } |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportFontToData(AbstractFont*, GlyphCache*, const std::string& filename, const std::u32string&) const override { |
||||
Containers::Array<unsigned char> file(1); |
||||
file[0] = 0xf0; |
||||
|
||||
Containers::Array<unsigned char> data(2); |
||||
data[0] = 0xfe; |
||||
data[1] = 0xed; |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> out; |
||||
out.emplace_back(filename, std::move(file)); |
||||
out.emplace_back(filename + ".data", std::move(data)); |
||||
return std::move(out); |
||||
} |
||||
}; |
||||
|
||||
/* Remove previous files */ |
||||
Utility::Directory::rm(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out")); |
||||
Utility::Directory::rm(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out.data")); |
||||
|
||||
/* doExportToFile() should call doExportToData() */ |
||||
DataExporter exporter; |
||||
bool exported = exporter.exportFontToFile(nullptr, nullptr, Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out"), {}); |
||||
CORRADE_VERIFY(exported); |
||||
CORRADE_COMPARE_AS(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out"), |
||||
"\xf0", TestSuite::Compare::FileToString); |
||||
CORRADE_COMPARE_AS(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out.data"), |
||||
"\xfe\xed", TestSuite::Compare::FileToString); |
||||
} |
||||
|
||||
void AbstractFontConverterTest::exportGlyphCacheToSingleData() { |
||||
class SingleDataExporter: public Text::AbstractFontConverter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData|Feature::ExportGlyphCache; } |
||||
|
||||
Containers::Array<unsigned char> doExportGlyphCacheToSingleData(GlyphCache*) const override { |
||||
Containers::Array<unsigned char> data(1); |
||||
data[0] = 0xee; |
||||
return std::move(data); |
||||
} |
||||
}; |
||||
|
||||
/* doExportGlyphCacheToData() should call doExportGlyphCacheToSingleData() */ |
||||
SingleDataExporter exporter; |
||||
auto ret = exporter.exportGlyphCacheToData(nullptr, "font.out"); |
||||
CORRADE_COMPARE(ret.size(), 1); |
||||
CORRADE_COMPARE(ret[0].first, "font.out"); |
||||
CORRADE_COMPARE(ret[0].second.size(), 1); |
||||
CORRADE_COMPARE(ret[0].second[0], 0xee); |
||||
} |
||||
|
||||
void AbstractFontConverterTest::exportGlyphCacheToFile() { |
||||
class DataExporter: public Text::AbstractFontConverter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData|Feature::ExportGlyphCache|Feature::MultiFile; } |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportGlyphCacheToData(GlyphCache*, const std::string& filename) const override { |
||||
Containers::Array<unsigned char> file(1); |
||||
file[0] = 0xf0; |
||||
|
||||
Containers::Array<unsigned char> data(2); |
||||
data[0] = 0xfe; |
||||
data[1] = 0xed; |
||||
|
||||
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> out; |
||||
out.emplace_back(filename, std::move(file)); |
||||
out.emplace_back(filename + ".data", std::move(data)); |
||||
return std::move(out); |
||||
} |
||||
}; |
||||
|
||||
/* Remove previous files */ |
||||
Utility::Directory::rm(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "glyphcache.out")); |
||||
Utility::Directory::rm(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "glyphcache.out.data")); |
||||
|
||||
/* doExportGlyphCacheToFile() should call doExportGlyphCacheToData() */ |
||||
DataExporter exporter; |
||||
bool exported = exporter.exportGlyphCacheToFile(nullptr, Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "glyphcache.out")); |
||||
CORRADE_VERIFY(exported); |
||||
CORRADE_COMPARE_AS(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "glyphcache.out"), |
||||
"\xf0", TestSuite::Compare::FileToString); |
||||
CORRADE_COMPARE_AS(Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "glyphcache.out.data"), |
||||
"\xfe\xed", TestSuite::Compare::FileToString); |
||||
} |
||||
|
||||
namespace { |
||||
|
||||
class SingleGlyphCacheDataImporter: public Text::AbstractFontConverter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData|Feature::ImportGlyphCache; } |
||||
|
||||
GlyphCache* doImportGlyphCacheFromSingleData(const Containers::ArrayReference<const unsigned char> data) const override { |
||||
if(data.size() == 1 && data[0] == 0xa5) return reinterpret_cast<GlyphCache*>(0xdeadbeef); |
||||
return nullptr; |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
void AbstractFontConverterTest::importGlyphCacheFromSingleData() { |
||||
/* doImportFromData() should call doImportFromSingleData() */ |
||||
SingleGlyphCacheDataImporter importer; |
||||
const unsigned char data[] = {0xa5}; |
||||
GlyphCache* cache = importer.importGlyphCacheFromData({{{}, data}}); |
||||
CORRADE_COMPARE(cache, reinterpret_cast<GlyphCache*>(0xdeadbeef)); |
||||
} |
||||
|
||||
void AbstractFontConverterTest::importGlyphCacheFromFile() { |
||||
/* doImportFromFile() should call doImportFromSingleData() */ |
||||
SingleGlyphCacheDataImporter importer; |
||||
GlyphCache* cache = importer.importGlyphCacheFromFile(Utility::Directory::join(TEXT_TEST_DIR, "data.bin")); |
||||
CORRADE_COMPARE(cache, reinterpret_cast<GlyphCache*>(0xdeadbeef)); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Text::Test::AbstractFontConverterTest) |
||||
@ -0,0 +1,94 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <Containers/Array.h> |
||||
#include <TestSuite/Tester.h> |
||||
#include <Utility/Directory.h> |
||||
|
||||
#include "Text/AbstractFont.h" |
||||
|
||||
#include "testConfigure.h" |
||||
|
||||
namespace Magnum { namespace Text { namespace Test { |
||||
|
||||
class AbstractFontTest: public TestSuite::Tester { |
||||
public: |
||||
explicit AbstractFontTest(); |
||||
|
||||
void openSingleData(); |
||||
void openFile(); |
||||
}; |
||||
|
||||
AbstractFontTest::AbstractFontTest() { |
||||
addTests({&AbstractFontTest::openSingleData, |
||||
&AbstractFontTest::openFile}); |
||||
} |
||||
|
||||
namespace { |
||||
|
||||
class SingleDataFont: public Text::AbstractFont { |
||||
public: |
||||
explicit SingleDataFont(): opened(false) {} |
||||
|
||||
Features doFeatures() const override { return Feature::OpenData; } |
||||
bool doIsOpened() const override { return opened; } |
||||
void doClose() override {} |
||||
|
||||
void doOpenSingleData(const Containers::ArrayReference<const unsigned char> data, Float) override { |
||||
opened = (data.size() == 1 && data[0] == 0xa5); |
||||
} |
||||
|
||||
UnsignedInt doGlyphId(char32_t) override { return 0; } |
||||
|
||||
Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } |
||||
|
||||
AbstractLayouter* doLayout(const GlyphCache*, Float, const std::string&) { |
||||
return nullptr; |
||||
} |
||||
|
||||
bool opened; |
||||
}; |
||||
|
||||
} |
||||
|
||||
void AbstractFontTest::openSingleData() { |
||||
/* doOpenData() should call doOpenSingleData() */ |
||||
SingleDataFont font; |
||||
const unsigned char data[] = {0xa5}; |
||||
CORRADE_VERIFY(!font.isOpened()); |
||||
font.openData({{{}, data}}, 3.0f); |
||||
CORRADE_VERIFY(font.isOpened()); |
||||
} |
||||
|
||||
void AbstractFontTest::openFile() { |
||||
/* doOpenFile() should call doOpenSingleData() */ |
||||
SingleDataFont font; |
||||
CORRADE_VERIFY(!font.isOpened()); |
||||
font.openFile(Utility::Directory::join(TEXT_TEST_DIR, "data.bin"), 3.0f); |
||||
CORRADE_VERIFY(font.isOpened()); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Text::Test::AbstractFontTest) |
||||
@ -0,0 +1,36 @@
|
||||
# |
||||
# This file is part of Magnum. |
||||
# |
||||
# Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
# |
||||
# 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. |
||||
# |
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/testConfigure.h.cmake |
||||
${CMAKE_CURRENT_BINARY_DIR}/testConfigure.h) |
||||
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR}) |
||||
|
||||
corrade_add_test(TextAbstractFontTest AbstractFontTest.cpp LIBRARIES Magnum MagnumText) |
||||
corrade_add_test(TextAbstractFontConverterTest AbstractFontConverterTest.cpp LIBRARIES Magnum MagnumText) |
||||
|
||||
if(BUILD_GL_TESTS) |
||||
corrade_add_test(TextGlyphCacheGLTest GlyphCacheGLTest.cpp LIBRARIES MagnumText ${GL_TEST_LIBRARIES}) |
||||
corrade_add_test(TextRendererGLTest TextRendererGLTest.cpp LIBRARIES MagnumText ${GL_TEST_LIBRARIES}) |
||||
endif() |
||||
@ -0,0 +1,92 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "Test/AbstractOpenGLTester.h" |
||||
#include "Text/GlyphCache.h" |
||||
|
||||
namespace Magnum { namespace Text { namespace Test { |
||||
|
||||
class GlyphCacheGLTest: public Magnum::Test::AbstractOpenGLTester { |
||||
public: |
||||
explicit GlyphCacheGLTest(); |
||||
|
||||
void initialize(); |
||||
void access(); |
||||
void reserve(); |
||||
}; |
||||
|
||||
GlyphCacheGLTest::GlyphCacheGLTest() { |
||||
addTests({&GlyphCacheGLTest::initialize, |
||||
&GlyphCacheGLTest::access, |
||||
&GlyphCacheGLTest::reserve}); |
||||
} |
||||
|
||||
void GlyphCacheGLTest::initialize() { |
||||
Text::GlyphCache cache({1024, 2048}); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
CORRADE_COMPARE(cache.texture()->imageSize(0), Vector2i(1024, 2048)); |
||||
} |
||||
|
||||
void GlyphCacheGLTest::access() { |
||||
Text::GlyphCache cache(Vector2i(236)); |
||||
Vector2i position; |
||||
Rectanglei rectangle; |
||||
|
||||
/* Default "Not Found" glyph */ |
||||
CORRADE_COMPARE(cache.glyphCount(), 1); |
||||
std::tie(position, rectangle) = cache[0]; |
||||
CORRADE_COMPARE(position, Vector2i(0, 0)); |
||||
CORRADE_COMPARE(rectangle, Rectanglei({0, 0}, {0, 0})); |
||||
|
||||
/* Overwrite "Not Found" glyph */ |
||||
cache.insert(0, {3, 5}, {{10, 10}, {23, 45}}); |
||||
CORRADE_COMPARE(cache.glyphCount(), 1); |
||||
std::tie(position, rectangle) = cache[0]; |
||||
CORRADE_COMPARE(position, Vector2i(3, 5)); |
||||
CORRADE_COMPARE(rectangle, Rectanglei({10, 10}, {23, 45})); |
||||
|
||||
/* Querying available glyph */ |
||||
cache.insert(25, {3, 4}, {{15, 30}, {45, 35}}); |
||||
CORRADE_COMPARE(cache.glyphCount(), 2); |
||||
std::tie(position, rectangle) = cache[25]; |
||||
CORRADE_COMPARE(position, Vector2i(3, 4)); |
||||
CORRADE_COMPARE(rectangle, Rectanglei({15, 30}, {45, 35})); |
||||
|
||||
/* Querying not available glyph falls back to "Not Found" */ |
||||
std::tie(position, rectangle) = cache[42]; |
||||
CORRADE_COMPARE(position, Vector2i(3, 5)); |
||||
CORRADE_COMPARE(rectangle, Rectanglei({10, 10}, {23, 45})); |
||||
} |
||||
|
||||
void GlyphCacheGLTest::reserve() { |
||||
Text::GlyphCache cache(Vector2i(236)); |
||||
|
||||
/* Verify that this works for "empty" cache */ |
||||
CORRADE_VERIFY(!cache.reserve({{5, 3}}).empty()); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Text::Test::GlyphCacheGLTest) |
||||
@ -0,0 +1,247 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "Test/AbstractOpenGLTester.h" |
||||
#include "Text/AbstractFont.h" |
||||
#include "Text/TextRenderer.h" |
||||
|
||||
namespace Magnum { namespace Text { namespace Test { |
||||
|
||||
class TextRendererGLTest: public Magnum::Test::AbstractOpenGLTester { |
||||
public: |
||||
explicit TextRendererGLTest(); |
||||
|
||||
void renderData(); |
||||
void renderMesh(); |
||||
void mutableText(); |
||||
}; |
||||
|
||||
TextRendererGLTest::TextRendererGLTest() { |
||||
addTests({&TextRendererGLTest::renderData, |
||||
&TextRendererGLTest::renderMesh, |
||||
&TextRendererGLTest::mutableText}); |
||||
} |
||||
|
||||
namespace { |
||||
|
||||
class TestLayouter: public Text::AbstractLayouter { |
||||
public: |
||||
explicit TestLayouter(Float size, std::size_t glyphCount): _size(size) { |
||||
_glyphCount = glyphCount; |
||||
} |
||||
|
||||
std::tuple<Rectangle, Rectangle, Vector2> renderGlyph(UnsignedInt i) override { |
||||
return std::make_tuple( |
||||
Rectangle({}, Vector2(3.0f, 2.0f)*((i+1)*_size)), |
||||
Rectangle::fromSize({i*6.0f, 0.0f}, {6.0f, 10.0f}), |
||||
(Vector2::xAxis((i+1)*3.0f)+Vector2(1.0f, -1.0f))*_size |
||||
); |
||||
} |
||||
|
||||
private: |
||||
Float _size; |
||||
}; |
||||
|
||||
class TestFont: public Text::AbstractFont { |
||||
public: |
||||
Features doFeatures() const override { return Feature::OpenData; } |
||||
|
||||
bool doIsOpened() const override { return true; } |
||||
void doClose() override {} |
||||
|
||||
UnsignedInt doGlyphId(char32_t) override { return 0; } |
||||
Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } |
||||
|
||||
AbstractLayouter* doLayout(const GlyphCache*, Float size, const std::string& text) override { |
||||
return new TestLayouter(size, text.size()); |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
void TextRendererGLTest::renderData() { |
||||
TestFont font; |
||||
std::vector<Vector2> positions; |
||||
std::vector<Vector2> textureCoordinates; |
||||
std::vector<UnsignedInt> indices; |
||||
Rectangle bounds; |
||||
std::tie(positions, textureCoordinates, indices, bounds) = Text::AbstractTextRenderer::render(&font, nullptr, 0.25f, "abc"); |
||||
|
||||
/* Three glyphs, three quads -> 12 vertices, 18 indices */ |
||||
CORRADE_COMPARE(positions.size(), 12); |
||||
CORRADE_COMPARE(textureCoordinates.size(), 12); |
||||
CORRADE_COMPARE(indices.size(), 18); |
||||
|
||||
/* Vertex positions and texture coordinates
|
||||
0---2 |
||||
| | |
||||
| | |
||||
| | |
||||
1---3 */ |
||||
|
||||
/* Vertex positions
|
||||
+---+ |
||||
+-+ | | |
||||
a |b| | c | |
||||
+-+ | | |
||||
+---+ */ |
||||
CORRADE_COMPARE(positions, (std::vector<Vector2>{ |
||||
{0.0f, 0.5f}, |
||||
{0.0f, 0.0f}, |
||||
{0.75f, 0.5f}, |
||||
{0.75f, 0.0f}, |
||||
|
||||
{1.0f, 0.75f}, |
||||
{1.0f, -0.25f}, |
||||
{2.5f, 0.75f}, |
||||
{2.5f, -0.25f}, |
||||
|
||||
{2.75f, 1.0f}, |
||||
{2.75f, -0.5f}, |
||||
{5.0f, 1.0f}, |
||||
{5.0f, -0.5f} |
||||
})); |
||||
|
||||
/* Texture coordinates
|
||||
+-+ +-+ +-+ |
||||
|a| |b| |c| |
||||
+-+ +-+ +-+ */ |
||||
CORRADE_COMPARE(textureCoordinates, (std::vector<Vector2>{ |
||||
{0.0f, 10.0f}, |
||||
{0.0f, 0.0f}, |
||||
{6.0f, 10.0f}, |
||||
{6.0f, 0.0f}, |
||||
|
||||
{ 6.0f, 10.0f}, |
||||
{ 6.0f, 0.0f}, |
||||
{12.0f, 10.0f}, |
||||
{12.0f, 0.0f}, |
||||
|
||||
{12.0f, 10.0f}, |
||||
{12.0f, 0.0f}, |
||||
{18.0f, 10.0f}, |
||||
{18.0f, 0.0f} |
||||
})); |
||||
|
||||
/* Indices
|
||||
0---2 0---2 5 |
||||
| | | / /| |
||||
| | | / / | |
||||
| | |/ / | |
||||
1---3 1 3---4 */ |
||||
CORRADE_COMPARE(indices, (std::vector<UnsignedInt>{ |
||||
0, 1, 2, 1, 3, 2, |
||||
4, 5, 6, 5, 7, 6, |
||||
8, 9, 10, 9, 11, 10 |
||||
})); |
||||
|
||||
/* Bounds */ |
||||
CORRADE_COMPARE(bounds, Rectangle({0.0f, -0.5f}, {5.0f, 1.0f})); |
||||
} |
||||
|
||||
void TextRendererGLTest::renderMesh() { |
||||
TestFont font; |
||||
Mesh mesh; |
||||
Buffer vertexBuffer, indexBuffer; |
||||
Rectangle bounds; |
||||
std::tie(mesh, bounds) = Text::TextRenderer3D::render(&font, nullptr, 0.25f, "abc", &vertexBuffer, &indexBuffer, Buffer::Usage::StaticDraw); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
|
||||
/* Vertex buffer contents */ |
||||
Containers::Array<Float> vertices = vertexBuffer.data<Float>(); |
||||
CORRADE_COMPARE(std::vector<Float>(vertices.begin(), vertices.end()), (std::vector<Float>{ |
||||
0.0f, 0.5f, 0.0f, 10.0f, |
||||
0.0f, 0.0f, 0.0f, 0.0f, |
||||
0.75f, 0.5f, 6.0f, 10.0f, |
||||
0.75f, 0.0f, 6.0f, 0.0f, |
||||
|
||||
1.0f, 0.75f, 6.0f, 10.0f, |
||||
1.0f, -0.25f, 6.0f, 0.0f, |
||||
2.5f, 0.75f, 12.0f, 10.0f, |
||||
2.5f, -0.25f, 12.0f, 0.0f, |
||||
|
||||
2.75f, 1.0f, 12.0f, 10.0f, |
||||
2.75f, -0.5f, 12.0f, 0.0f, |
||||
5.0f, 1.0f, 18.0f, 10.0f, |
||||
5.0f, -0.5f, 18.0f, 0.0f |
||||
})); |
||||
|
||||
Containers::Array<UnsignedByte> indices = indexBuffer.data<UnsignedByte>(); |
||||
CORRADE_COMPARE(std::vector<UnsignedByte>(indices.begin(), indices.end()), (std::vector<UnsignedByte>{ |
||||
0, 1, 2, 1, 3, 2, |
||||
4, 5, 6, 5, 7, 6, |
||||
8, 9, 10, 9, 11, 10 |
||||
})); |
||||
|
||||
/* Bounds */ |
||||
CORRADE_COMPARE(bounds, Rectangle({0.0f, -0.5f}, {5.0f, 1.0f})); |
||||
} |
||||
|
||||
void TextRendererGLTest::mutableText() { |
||||
TestFont font; |
||||
Text::TextRenderer2D renderer(&font, nullptr, 0.25f); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
CORRADE_COMPARE(renderer.capacity(), 0); |
||||
CORRADE_COMPARE(renderer.rectangle(), Rectangle()); |
||||
|
||||
/* Reserve some capacity */ |
||||
renderer.reserve(4, Buffer::Usage::StaticDraw, Buffer::Usage::StaticDraw); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
CORRADE_COMPARE(renderer.capacity(), 4); |
||||
Containers::Array<UnsignedByte> indices = renderer.indexBuffer()->data<UnsignedByte>(); |
||||
CORRADE_COMPARE(std::vector<UnsignedByte>(indices.begin(), indices.end()), (std::vector<UnsignedByte>{ |
||||
0, 1, 2, 1, 3, 2, |
||||
4, 5, 6, 5, 7, 6, |
||||
8, 9, 10, 9, 11, 10, |
||||
12, 13, 14, 13, 15, 14 |
||||
})); |
||||
|
||||
/* Render text */ |
||||
renderer.render("abc"); |
||||
MAGNUM_VERIFY_NO_ERROR(); |
||||
Containers::Array<Float> vertices = renderer.vertexBuffer()->subData<Float>(0, 48); |
||||
CORRADE_COMPARE(std::vector<Float>(vertices.begin(), vertices.end()), (std::vector<Float>{ |
||||
0.0f, 0.5f, 0.0f, 10.0f, |
||||
0.0f, 0.0f, 0.0f, 0.0f, |
||||
0.75f, 0.5f, 6.0f, 10.0f, |
||||
0.75f, 0.0f, 6.0f, 0.0f, |
||||
|
||||
1.0f, 0.75f, 6.0f, 10.0f, |
||||
1.0f, -0.25f, 6.0f, 0.0f, |
||||
2.5f, 0.75f, 12.0f, 10.0f, |
||||
2.5f, -0.25f, 12.0f, 0.0f, |
||||
|
||||
2.75f, 1.0f, 12.0f, 10.0f, |
||||
2.75f, -0.5f, 12.0f, 0.0f, |
||||
5.0f, 1.0f, 18.0f, 10.0f, |
||||
5.0f, -0.5f, 18.0f, 0.0f |
||||
})); |
||||
|
||||
/* Updated bounds */ |
||||
CORRADE_COMPARE(renderer.rectangle(), Rectangle({0.0f, -0.5f}, {5.0f, 1.0f})); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Text::Test::TextRendererGLTest) |
||||
@ -0,0 +1,26 @@
|
||||
/* |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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. |
||||
*/ |
||||
|
||||
#define TEXT_TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}" |
||||
#define TEXT_TEST_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}" |
||||
@ -0,0 +1,75 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <Containers/Array.h> |
||||
#include <TestSuite/Tester.h> |
||||
#include <TestSuite/Compare/FileToString.h> |
||||
#include <Utility/Directory.h> |
||||
|
||||
#include "Image.h" |
||||
#include "ImageFormat.h" |
||||
#include "Trade/AbstractImageConverter.h" |
||||
|
||||
#include "testConfigure.h" |
||||
|
||||
namespace Magnum { namespace Trade { namespace Test { |
||||
|
||||
class AbstractImageConverterTest: public TestSuite::Tester { |
||||
public: |
||||
explicit AbstractImageConverterTest(); |
||||
|
||||
void exportToFile(); |
||||
}; |
||||
|
||||
AbstractImageConverterTest::AbstractImageConverterTest() { |
||||
addTests({&AbstractImageConverterTest::exportToFile}); |
||||
} |
||||
|
||||
void AbstractImageConverterTest::exportToFile() { |
||||
class DataExporter: public Trade::AbstractImageConverter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::ConvertData; } |
||||
|
||||
Containers::Array<unsigned char> doExportToData(const Image2D* image) const override { |
||||
Containers::Array<unsigned char> out(2); |
||||
out[0] = image->size().x(); |
||||
out[1] = image->size().y(); |
||||
return out; |
||||
}; |
||||
}; |
||||
|
||||
/* Remove previous file */ |
||||
Utility::Directory::rm(Utility::Directory::join(TRADE_TEST_OUTPUT_DIR, "image.out")); |
||||
|
||||
/* doExportToFile() should call doExportToData() */ |
||||
DataExporter exporter; |
||||
Image2D image(ImageFormat::RGBA, ImageType::UnsignedByte, {0xfe, 0xed}, nullptr); |
||||
CORRADE_VERIFY(exporter.exportToFile(&image, Utility::Directory::join(TRADE_TEST_OUTPUT_DIR, "image.out"))); |
||||
CORRADE_COMPARE_AS(Utility::Directory::join(TRADE_TEST_OUTPUT_DIR, "image.out"), |
||||
"\xFE\xED", TestSuite::Compare::FileToString); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Trade::Test::AbstractImageConverterTest) |
||||
@ -0,0 +1,69 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <Containers/Array.h> |
||||
#include <TestSuite/Tester.h> |
||||
#include <Utility/Directory.h> |
||||
|
||||
#include "Trade/AbstractImporter.h" |
||||
|
||||
#include "testConfigure.h" |
||||
|
||||
namespace Magnum { namespace Trade { namespace Test { |
||||
|
||||
class AbstractImporterTest: public TestSuite::Tester { |
||||
public: |
||||
explicit AbstractImporterTest(); |
||||
|
||||
void openFile(); |
||||
}; |
||||
|
||||
AbstractImporterTest::AbstractImporterTest() { |
||||
addTests({&AbstractImporterTest::openFile}); |
||||
} |
||||
|
||||
void AbstractImporterTest::openFile() { |
||||
class DataImporter: public Trade::AbstractImporter { |
||||
private: |
||||
Features doFeatures() const override { return Feature::OpenData; } |
||||
bool doIsOpened() const override { return opened; } |
||||
void doClose() override {} |
||||
|
||||
void doOpenData(Containers::ArrayReference<const unsigned char> data) override { |
||||
opened = (data.size() == 1 && data[0] == 0xa5); |
||||
} |
||||
|
||||
bool opened; |
||||
}; |
||||
|
||||
/* doOpenFile() should call doOpenData() */ |
||||
DataImporter importer; |
||||
CORRADE_VERIFY(!importer.isOpened()); |
||||
importer.openFile(Utility::Directory::join(TRADE_TEST_DIR, "file.bin")); |
||||
CORRADE_VERIFY(importer.isOpened()); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Trade::Test::AbstractImporterTest) |
||||
@ -0,0 +1,85 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <TestSuite/Tester.h> |
||||
|
||||
#include "ImageFormat.h" |
||||
#include "Trade/ImageData.h" |
||||
|
||||
namespace Magnum { namespace Trade { namespace Test { |
||||
|
||||
class ImageDataTest: public TestSuite::Tester { |
||||
public: |
||||
explicit ImageDataTest(); |
||||
|
||||
void moveConstructor(); |
||||
void moveAssignment(); |
||||
void toReference(); |
||||
}; |
||||
|
||||
ImageDataTest::ImageDataTest() { |
||||
addTests({&ImageDataTest::moveConstructor, |
||||
&ImageDataTest::moveAssignment, |
||||
&ImageDataTest::toReference}); |
||||
} |
||||
|
||||
void ImageDataTest::moveConstructor() { |
||||
unsigned char* data = new unsigned char[3]; |
||||
Trade::ImageData2D a(ImageFormat::Red, ImageType::UnsignedByte, {1, 3}, data); |
||||
|
||||
Trade::ImageData2D b(std::move(a)); |
||||
CORRADE_VERIFY(!a.data()); |
||||
CORRADE_COMPARE(b.format(), ImageFormat::Red); |
||||
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte); |
||||
CORRADE_COMPARE(b.size(), Vector2i(1, 3)); |
||||
CORRADE_VERIFY(b.data() == data); |
||||
} |
||||
|
||||
void ImageDataTest::moveAssignment() { |
||||
unsigned char* data = new unsigned char[3]; |
||||
Trade::ImageData2D a(ImageFormat::Red, ImageType::UnsignedByte, {1, 3}, data); |
||||
|
||||
Trade::ImageData2D b(ImageFormat::Red, ImageType::UnsignedByte, {}, nullptr); |
||||
b = std::move(a); |
||||
CORRADE_VERIFY(!a.data()); |
||||
CORRADE_COMPARE(b.format(), ImageFormat::Red); |
||||
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte); |
||||
CORRADE_COMPARE(b.size(), Vector2i(1, 3)); |
||||
CORRADE_VERIFY(b.data() == data); |
||||
} |
||||
|
||||
void ImageDataTest::toReference() { |
||||
unsigned char* data = new unsigned char[3]; |
||||
Trade::ImageData2D a(ImageFormat::Red, ImageType::UnsignedByte, {1, 3}, data); |
||||
|
||||
ImageReference2D b = a; |
||||
CORRADE_COMPARE(b.format(), ImageFormat::Red); |
||||
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte); |
||||
CORRADE_COMPARE(b.size(), Vector2i(1, 3)); |
||||
CORRADE_COMPARE(b.data(), data); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Trade::Test::ImageDataTest) |
||||
@ -0,0 +1,26 @@
|
||||
/* |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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. |
||||
*/ |
||||
|
||||
#define TRADE_TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}" |
||||
#define TRADE_TEST_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}" |
||||
Loading…
Reference in new issue