From d74fcb7729c95f3c241e3ad45abdb9f88f2a1732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 5 Oct 2023 19:45:50 +0200 Subject: [PATCH] Text: port away from deprecated AbstractGlyphCache APIs. --- doc/snippets/MagnumText.cpp | 5 ++-- src/Magnum/Text/DistanceFieldGlyphCache.cpp | 2 +- .../Text/Test/AbstractFontConverterTest.cpp | 25 ++++++++++--------- src/Magnum/Text/Test/AbstractFontTest.cpp | 23 +++++++++-------- .../Test/DistanceFieldGlyphCacheGLTest.cpp | 2 +- src/Magnum/Text/Test/GlyphCacheGLTest.cpp | 17 ++++++++++--- 6 files changed, 43 insertions(+), 31 deletions(-) diff --git a/doc/snippets/MagnumText.cpp b/doc/snippets/MagnumText.cpp index 4dc9ea552..f47a74cb3 100644 --- a/doc/snippets/MagnumText.cpp +++ b/doc/snippets/MagnumText.cpp @@ -42,6 +42,7 @@ #include "Magnum/FileCallback.h" #include "Magnum/ImageView.h" +#include "Magnum/PixelFormat.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Range.h" @@ -174,7 +175,7 @@ struct: Text::AbstractGlyphCache { using Text::AbstractGlyphCache::AbstractGlyphCache; Text::GlyphCacheFeatures doFeatures() const override { return {}; } -} cache{Vector2i{256}}; +} cache{PixelFormat::R8Unorm, Vector2i{256}}; /* [AbstractGlyphCache-filling-images] */ Containers::ArrayView images = DOXYGEN_ELLIPSIS({}); /* [AbstractGlyphCache-filling-images] */ @@ -224,7 +225,7 @@ struct: Text::AbstractGlyphCache { using Text::AbstractGlyphCache::AbstractGlyphCache; Text::GlyphCacheFeatures doFeatures() const override { return {}; } -} cacheInstance{Vector2i{256}}; +} cacheInstance{PixelFormat::R8Unorm, Vector2i{256}}; /* [AbstractGlyphCache-querying] */ Containers::Pointer font = DOXYGEN_ELLIPSIS({}); Text::AbstractGlyphCache& cache = DOXYGEN_ELLIPSIS(cacheInstance); diff --git a/src/Magnum/Text/DistanceFieldGlyphCache.cpp b/src/Magnum/Text/DistanceFieldGlyphCache.cpp index 883df1d55..993e2ed68 100644 --- a/src/Magnum/Text/DistanceFieldGlyphCache.cpp +++ b/src/Magnum/Text/DistanceFieldGlyphCache.cpp @@ -77,7 +77,7 @@ void DistanceFieldGlyphCache::doSetImage(const Vector2i& offset, const ImageView .setMagnificationFilter(GL::SamplerFilter::Linear); /* Upload the input texture and create a distance field from it */ - const Vector2 scale = Vector2{_size}/Vector2{textureSize()}; + const Vector2 scale = Vector2{_size}/Vector2{size().xy()}; /* On ES2 without EXT_unpack_subimage and on WebGL 1 there's no possibility to upload just a slice of the input, upload the whole image instead by diff --git a/src/Magnum/Text/Test/AbstractFontConverterTest.cpp b/src/Magnum/Text/Test/AbstractFontConverterTest.cpp index a8addb1f2..a9b54ea63 100644 --- a/src/Magnum/Text/Test/AbstractFontConverterTest.cpp +++ b/src/Magnum/Text/Test/AbstractFontConverterTest.cpp @@ -34,7 +34,8 @@ #include #include -#include "Magnum/Math/Vector2.h" +#include "Magnum/PixelFormat.h" +#include "Magnum/Math/Vector3.h" #include "Magnum/Text/AbstractFont.h" #include "Magnum/Text/AbstractFontConverter.h" #include "Magnum/Text/AbstractGlyphCache.h" @@ -172,7 +173,7 @@ struct DummyGlyphCache: AbstractGlyphCache { GlyphCacheFeatures doFeatures() const override { return {}; } void doSetImage(const Vector2i&, const ImageView2D&) override {} -} dummyGlyphCache{{128, 128}}; +} dummyGlyphCache{PixelFormat::R8Unorm, {128, 128}}; void AbstractFontConverterTest::convertGlyphs() { std::u32string characters; @@ -803,7 +804,7 @@ void AbstractFontConverterTest::importGlyphCacheFromSingleData() { Containers::Pointer doImportGlyphCacheFromSingleData(const Containers::ArrayView data) const override { if(data.size() == 1 && data[0] == '\xa5') - return Containers::pointer(Vector2i{123, 345}); + return Containers::pointer(PixelFormat::R8Unorm, Vector2i{123, 345}); return nullptr; } } converter; @@ -811,7 +812,7 @@ void AbstractFontConverterTest::importGlyphCacheFromSingleData() { const char data[] = {'\xa5'}; Containers::Pointer cache = converter.importGlyphCacheFromSingleData(data); CORRADE_VERIFY(cache); - CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); + CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1})); } void AbstractFontConverterTest::importGlyphCacheFromSingleDataNotImplemented() { @@ -852,7 +853,7 @@ void AbstractFontConverterTest::importGlyphCacheFromData() { Containers::Pointer doImportGlyphCacheFromData(const std::vector>>& data) const override { if(data.size() == 2 && data[1].second.size() == 1 && data[1].second[0] == '\xa5') - return Containers::pointer(Vector2i{123, 345}); + return Containers::pointer(PixelFormat::R8Unorm, Vector2i{123, 345}); return nullptr; } } converter; @@ -860,7 +861,7 @@ void AbstractFontConverterTest::importGlyphCacheFromData() { const char data[] = {'\xa5'}; Containers::Pointer cache = converter.importGlyphCacheFromData({{}, {{}, data}}); CORRADE_VERIFY(cache); - CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); + CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1})); } void AbstractFontConverterTest::importGlyphCacheFromDataNoData() { @@ -903,7 +904,7 @@ void AbstractFontConverterTest::importGlyphCacheFromDataAsSingleData() { Containers::Pointer doImportGlyphCacheFromSingleData(const Containers::ArrayView data) const override { if(data.size() == 1 && data[0] == '\xa5') - return Containers::pointer(Vector2i{123, 345}); + return Containers::pointer(PixelFormat::R8Unorm, Vector2i{123, 345}); return nullptr; } } converter; @@ -911,7 +912,7 @@ void AbstractFontConverterTest::importGlyphCacheFromDataAsSingleData() { const char data[] = {'\xa5'}; Containers::Pointer cache = converter.importGlyphCacheFromData({{{}, data}}); CORRADE_VERIFY(cache); - CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); + CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1})); } void AbstractFontConverterTest::importGlyphCacheFromFile() { @@ -926,13 +927,13 @@ void AbstractFontConverterTest::importGlyphCacheFromFile() { CORRADE_COMPARE_AS(*data, Containers::arrayView({'\xa5'}), TestSuite::Compare::Container); - return Containers::pointer(Vector2i{123, 345}); + return Containers::pointer(PixelFormat::R8Unorm, Vector2i{123, 345}); } } converter; Containers::Pointer cache = converter.importGlyphCacheFromFile(Utility::Path::join(TEXT_TEST_DIR, "data.bin")); CORRADE_VERIFY(cache); - CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); + CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1})); } void AbstractFontConverterTest::importGlyphCacheFromFileNotImplemented() { @@ -958,7 +959,7 @@ void AbstractFontConverterTest::importGlyphCacheFromFileAsSingleData() { Containers::Pointer doImportGlyphCacheFromSingleData(const Containers::ArrayView data) const override { if(data.size() == 1 && data[0] == '\xa5') - return Containers::pointer(Vector2i{123, 345}); + return Containers::pointer(PixelFormat::R8Unorm, Vector2i{123, 345}); return nullptr; } } converter; @@ -966,7 +967,7 @@ void AbstractFontConverterTest::importGlyphCacheFromFileAsSingleData() { /* doImportFromFile() should call doImportFromSingleData() */ Containers::Pointer cache = converter.importGlyphCacheFromFile(Utility::Path::join(TEXT_TEST_DIR, "data.bin")); CORRADE_VERIFY(cache); - CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); + CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1})); } void AbstractFontConverterTest::importGlyphCacheFromFileAsSingleDataNotFound() { diff --git a/src/Magnum/Text/Test/AbstractFontTest.cpp b/src/Magnum/Text/Test/AbstractFontTest.cpp index ea4716c1a..9ebee5509 100644 --- a/src/Magnum/Text/Test/AbstractFontTest.cpp +++ b/src/Magnum/Text/Test/AbstractFontTest.cpp @@ -36,6 +36,7 @@ #include #include "Magnum/FileCallback.h" +#include "Magnum/PixelFormat.h" #include "Magnum/Math/Range.h" #include "Magnum/Math/Vector2.h" #include "Magnum/Text/AbstractFont.h" @@ -961,11 +962,11 @@ void AbstractFontTest::layout() { Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } Containers::Pointer doLayout(const AbstractGlyphCache& cache, Float size, Containers::StringView str) override { - return Containers::pointer(UnsignedInt(cache.textureSize().x()*str.size()*size)); + return Containers::pointer(UnsignedInt(cache.size().x()*str.size()*size)); } } font; - DummyGlyphCache cache{{100, 200}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 200}}; Containers::Pointer layouter = font.layout(cache, 0.25f, "hello"); CORRADE_COMPARE(layouter->glyphCount(), 100*5/4); } @@ -986,7 +987,7 @@ void AbstractFontTest::layoutNoFont() { std::ostringstream out; Error redirectError{&out}; - DummyGlyphCache cache{{100, 200}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 200}}; font.layout(cache, 0.25f, "hello"); CORRADE_COMPARE(out.str(), "Text::AbstractFont::layout(): no font opened\n"); } @@ -1003,7 +1004,7 @@ void AbstractFontTest::fillGlyphCache() { Containers::Pointer doLayout(const AbstractGlyphCache&, Float, Containers::StringView) override { return nullptr; } void doFillGlyphCache(AbstractGlyphCache& cache, Containers::ArrayView characters) override { - CORRADE_COMPARE(cache.textureSize(), (Vector2i{100, 100})); + CORRADE_COMPARE(cache.size(), (Vector3i{100, 100, 1})); CORRADE_COMPARE_AS(characters, Containers::arrayView({ 'h', 'e', 'l', 'o' }), TestSuite::Compare::Container); @@ -1016,7 +1017,7 @@ void AbstractFontTest::fillGlyphCache() { /* Capture correct function name */ CORRADE_VERIFY(true); - DummyGlyphCache cache{{100, 100}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 100}}; font.fillGlyphCache(cache, "helo"); CORRADE_VERIFY(font.called); @@ -1038,7 +1039,7 @@ void AbstractFontTest::fillGlyphCacheNotSupported() { std::ostringstream out; Error redirectError{&out}; - DummyGlyphCache cache{{100, 100}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 100}}; font.fillGlyphCache(cache, "hello"); CORRADE_COMPARE(out.str(), "Text::AbstractFont::fillGlyphCache(): feature not supported\n"); } @@ -1059,7 +1060,7 @@ void AbstractFontTest::fillGlyphCacheNotImplemented() { std::ostringstream out; Error redirectError{&out}; - DummyGlyphCache cache{{100, 100}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 100}}; font.fillGlyphCache(cache, "hello"); CORRADE_COMPARE(out.str(), "Text::AbstractFont::fillGlyphCache(): feature advertised but not implemented\n"); } @@ -1080,7 +1081,7 @@ void AbstractFontTest::fillGlyphCacheNoFont() { std::ostringstream out; Error redirectError{&out}; - DummyGlyphCache cache{{100, 100}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 100}}; font.fillGlyphCache(cache, "hello"); CORRADE_COMPARE(out.str(), "Text::AbstractFont::fillGlyphCache(): no font opened\n"); } @@ -1101,7 +1102,7 @@ void AbstractFontTest::fillGlyphCacheInvalidUtf8() { std::ostringstream out; Error redirectError{&out}; - DummyGlyphCache cache{{100, 100}}; + DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 100}}; font.fillGlyphCache(cache, "he\xffo"); CORRADE_COMPARE(out.str(), "Text::AbstractFont::fillGlyphCache(): not a valid UTF-8 string: he\xffo\n"); } @@ -1118,14 +1119,14 @@ void AbstractFontTest::createGlyphCache() { Containers::Pointer doLayout(const AbstractGlyphCache&, Float, Containers::StringView) override { return nullptr; } Containers::Pointer doCreateGlyphCache() override { - return Containers::pointer(Vector2i{123, 345}); + return Containers::pointer(PixelFormat::R8Unorm, Vector2i{123, 345}); } } font; Containers::Pointer cache = font.createGlyphCache(); CORRADE_VERIFY(cache); - CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345})); + CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1})); } void AbstractFontTest::createGlyphCacheNotSupported() { diff --git a/src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp b/src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp index 6f694ddd7..d9adc437b 100644 --- a/src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp +++ b/src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp @@ -100,7 +100,7 @@ void DistanceFieldGlyphCacheGLTest::initialize() { DistanceFieldGlyphCache cache{{1024, 2048}, {128, 256}, 16}; MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE(cache.textureSize(), (Vector2i{1024, 2048})); + CORRADE_COMPARE(cache.size(), (Vector3i{1024, 2048, 1})); CORRADE_COMPARE(cache.distanceFieldTextureSize(), (Vector2i{128, 256})); #ifndef MAGNUM_TARGET_GLES CORRADE_COMPARE(cache.texture().imageSize(0), (Vector2i{128, 256})); diff --git a/src/Magnum/Text/Test/GlyphCacheGLTest.cpp b/src/Magnum/Text/Test/GlyphCacheGLTest.cpp index 75b7797e7..d5b5fbe0e 100644 --- a/src/Magnum/Text/Test/GlyphCacheGLTest.cpp +++ b/src/Magnum/Text/Test/GlyphCacheGLTest.cpp @@ -24,6 +24,8 @@ */ #include +#include +#include #include "Magnum/Image.h" #include "Magnum/ImageView.h" @@ -34,6 +36,7 @@ #endif #include "Magnum/GL/OpenGLTester.h" #include "Magnum/GL/TextureFormat.h" +#include "Magnum/Math/Color.h" #include "Magnum/Math/Range.h" #include "Magnum/Text/GlyphCache.h" @@ -61,7 +64,7 @@ void GlyphCacheGLTest::initialize() { GlyphCache cache{{1024, 2048}}; MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE(cache.textureSize(), (Vector2i{1024, 2048})); + CORRADE_COMPARE(cache.size(), (Vector3i{1024, 2048, 1})); #ifndef MAGNUM_TARGET_GLES CORRADE_COMPARE(cache.texture().imageSize(0), (Vector2i{1024, 2048})); #endif @@ -77,7 +80,7 @@ void GlyphCacheGLTest::initializeCustomFormat() { {256, 512}}; MAGNUM_VERIFY_NO_GL_ERROR(); - CORRADE_COMPARE(cache.textureSize(), (Vector2i{256, 512})); + CORRADE_COMPARE(cache.size(), (Vector3i{256, 512, 1})); #ifndef MAGNUM_TARGET_GLES CORRADE_COMPARE(cache.texture().imageSize(0), (Vector2i{256, 512})); #endif @@ -104,7 +107,10 @@ const UnsignedByte ExpectedData[]{ void GlyphCacheGLTest::setImage() { GlyphCache cache{{16, 8}}; - cache.setImage({8, 4}, ImageView2D{PixelFormat::R8Unorm, {8, 4}, InputData}); + Utility::copy( + Containers::StridedArrayView2D{InputData, {4, 8}}, + cache.image().pixels()[0].sliceSize({4, 8}, {4, 8})); + cache.flushImage(Range2Di::fromSize({8, 4}, {8, 4})); MAGNUM_VERIFY_NO_GL_ERROR(); MutableImageView3D actual3 = cache.image(); @@ -148,7 +154,10 @@ void GlyphCacheGLTest::setImageCustomFormat() { #endif {4, 8}}; - cache.setImage({2, 4}, ImageView2D{PixelFormat::RGBA8Unorm, {2, 4}, InputData}); + Utility::copy( + Containers::StridedArrayView2D{Containers::arrayCast(Containers::arrayView(InputData)), {4, 2}}, + cache.image().pixels()[0].sliceSize({4, 2}, {4, 2})); + cache.flushImage(Range2Di::fromSize({2, 4}, {2, 4})); MAGNUM_VERIFY_NO_GL_ERROR(); MutableImageView3D actual3 = cache.image();