Browse Source

Text: port away from deprecated AbstractGlyphCache APIs.

pull/168/head
Vladimír Vondruš 3 years ago
parent
commit
d74fcb7729
  1. 5
      doc/snippets/MagnumText.cpp
  2. 2
      src/Magnum/Text/DistanceFieldGlyphCache.cpp
  3. 25
      src/Magnum/Text/Test/AbstractFontConverterTest.cpp
  4. 23
      src/Magnum/Text/Test/AbstractFontTest.cpp
  5. 2
      src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp
  6. 17
      src/Magnum/Text/Test/GlyphCacheGLTest.cpp

5
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<const ImageView2D> 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<Text::AbstractFont> font = DOXYGEN_ELLIPSIS({});
Text::AbstractGlyphCache& cache = DOXYGEN_ELLIPSIS(cacheInstance);

2
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

25
src/Magnum/Text/Test/AbstractFontConverterTest.cpp

@ -34,7 +34,8 @@
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/Path.h>
#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<AbstractGlyphCache> doImportGlyphCacheFromSingleData(const Containers::ArrayView<const char> data) const override {
if(data.size() == 1 && data[0] == '\xa5')
return Containers::pointer<DummyGlyphCache>(Vector2i{123, 345});
return Containers::pointer<DummyGlyphCache>(PixelFormat::R8Unorm, Vector2i{123, 345});
return nullptr;
}
} converter;
@ -811,7 +812,7 @@ void AbstractFontConverterTest::importGlyphCacheFromSingleData() {
const char data[] = {'\xa5'};
Containers::Pointer<AbstractGlyphCache> 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<AbstractGlyphCache> doImportGlyphCacheFromData(const std::vector<std::pair<std::string, Containers::ArrayView<const char>>>& data) const override {
if(data.size() == 2 && data[1].second.size() == 1 && data[1].second[0] == '\xa5')
return Containers::pointer<DummyGlyphCache>(Vector2i{123, 345});
return Containers::pointer<DummyGlyphCache>(PixelFormat::R8Unorm, Vector2i{123, 345});
return nullptr;
}
} converter;
@ -860,7 +861,7 @@ void AbstractFontConverterTest::importGlyphCacheFromData() {
const char data[] = {'\xa5'};
Containers::Pointer<AbstractGlyphCache> 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<AbstractGlyphCache> doImportGlyphCacheFromSingleData(const Containers::ArrayView<const char> data) const override {
if(data.size() == 1 && data[0] == '\xa5')
return Containers::pointer<DummyGlyphCache>(Vector2i{123, 345});
return Containers::pointer<DummyGlyphCache>(PixelFormat::R8Unorm, Vector2i{123, 345});
return nullptr;
}
} converter;
@ -911,7 +912,7 @@ void AbstractFontConverterTest::importGlyphCacheFromDataAsSingleData() {
const char data[] = {'\xa5'};
Containers::Pointer<AbstractGlyphCache> 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<DummyGlyphCache>(Vector2i{123, 345});
return Containers::pointer<DummyGlyphCache>(PixelFormat::R8Unorm, Vector2i{123, 345});
}
} converter;
Containers::Pointer<AbstractGlyphCache> 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<AbstractGlyphCache> doImportGlyphCacheFromSingleData(const Containers::ArrayView<const char> data) const override {
if(data.size() == 1 && data[0] == '\xa5')
return Containers::pointer<DummyGlyphCache>(Vector2i{123, 345});
return Containers::pointer<DummyGlyphCache>(PixelFormat::R8Unorm, Vector2i{123, 345});
return nullptr;
}
} converter;
@ -966,7 +967,7 @@ void AbstractFontConverterTest::importGlyphCacheFromFileAsSingleData() {
/* doImportFromFile() should call doImportFromSingleData() */
Containers::Pointer<AbstractGlyphCache> 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() {

23
src/Magnum/Text/Test/AbstractFontTest.cpp

@ -36,6 +36,7 @@
#include <Corrade/Utility/Path.h>
#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<AbstractLayouter> doLayout(const AbstractGlyphCache& cache, Float size, Containers::StringView str) override {
return Containers::pointer<Layouter>(UnsignedInt(cache.textureSize().x()*str.size()*size));
return Containers::pointer<Layouter>(UnsignedInt(cache.size().x()*str.size()*size));
}
} font;
DummyGlyphCache cache{{100, 200}};
DummyGlyphCache cache{PixelFormat::R8Unorm, {100, 200}};
Containers::Pointer<AbstractLayouter> 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<AbstractLayouter> doLayout(const AbstractGlyphCache&, Float, Containers::StringView) override { return nullptr; }
void doFillGlyphCache(AbstractGlyphCache& cache, Containers::ArrayView<const char32_t> characters) override {
CORRADE_COMPARE(cache.textureSize(), (Vector2i{100, 100}));
CORRADE_COMPARE(cache.size(), (Vector3i{100, 100, 1}));
CORRADE_COMPARE_AS(characters, Containers::arrayView<char32_t>({
'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<AbstractLayouter> doLayout(const AbstractGlyphCache&, Float, Containers::StringView) override { return nullptr; }
Containers::Pointer<AbstractGlyphCache> doCreateGlyphCache() override {
return Containers::pointer<DummyGlyphCache>(Vector2i{123, 345});
return Containers::pointer<DummyGlyphCache>(PixelFormat::R8Unorm, Vector2i{123, 345});
}
} font;
Containers::Pointer<AbstractGlyphCache> cache = font.createGlyphCache();
CORRADE_VERIFY(cache);
CORRADE_COMPARE(cache->textureSize(), (Vector2i{123, 345}));
CORRADE_COMPARE(cache->size(), (Vector3i{123, 345, 1}));
}
void AbstractFontTest::createGlyphCacheNotSupported() {

2
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}));

17
src/Magnum/Text/Test/GlyphCacheGLTest.cpp

@ -24,6 +24,8 @@
*/
#include <Corrade/Containers/StringView.h>
#include <Corrade/Containers/StridedArrayView.h>
#include <Corrade/Utility/Algorithms.h>
#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<const UnsignedByte>{InputData, {4, 8}},
cache.image().pixels<UnsignedByte>()[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<const Color4ub>{Containers::arrayCast<const Color4ub>(Containers::arrayView(InputData)), {4, 2}},
cache.image().pixels<Color4ub>()[0].sliceSize({4, 2}, {4, 2}));
cache.flushImage(Range2Di::fromSize({2, 4}, {2, 4}));
MAGNUM_VERIFY_NO_GL_ERROR();
MutableImageView3D actual3 = cache.image();

Loading…
Cancel
Save