From da66ec88e8643941e6270944b8d08378d32c0f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 12 Jun 2026 17:17:00 +0200 Subject: [PATCH] Text,MagnumFont: port away from deprecated AbstractFont APIs. --- doc/snippets/Text.cpp | 1 + .../Text/Test/AbstractFontConverterTest.cpp | 1 + src/Magnum/Text/Test/RendererGLTest.cpp | 10 +- src/Magnum/Text/Test/RendererTest.cpp | 160 +++++++++++++----- src/MagnumPlugins/MagnumFont/MagnumFont.cpp | 27 ++- src/MagnumPlugins/MagnumFont/MagnumFont.h | 5 +- .../MagnumFont/Test/MagnumFontTest.cpp | 11 ++ .../Test/MagnumFontConverterTest.cpp | 12 +- 8 files changed, 169 insertions(+), 58 deletions(-) diff --git a/doc/snippets/Text.cpp b/doc/snippets/Text.cpp index 4012a2c84..b0c4f06d6 100644 --- a/doc/snippets/Text.cpp +++ b/doc/snippets/Text.cpp @@ -80,6 +80,7 @@ struct MyFont: Text::AbstractFont { Text::FontFeatures doFeatures() const override { return {}; } bool doIsOpened() const override { return false; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } diff --git a/src/Magnum/Text/Test/AbstractFontConverterTest.cpp b/src/Magnum/Text/Test/AbstractFontConverterTest.cpp index ff43b04ae..303c476ad 100644 --- a/src/Magnum/Text/Test/AbstractFontConverterTest.cpp +++ b/src/Magnum/Text/Test/AbstractFontConverterTest.cpp @@ -225,6 +225,7 @@ struct DummyFont: AbstractFont { bool doIsOpened() const override { return false; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } diff --git a/src/Magnum/Text/Test/RendererGLTest.cpp b/src/Magnum/Text/Test/RendererGLTest.cpp index 50b4ca223..8ad764d5a 100644 --- a/src/Magnum/Text/Test/RendererGLTest.cpp +++ b/src/Magnum/Text/Test/RendererGLTest.cpp @@ -429,8 +429,11 @@ void RendererGLTest::renderClearReset() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances, ascent & descent is used to align the block. Line height is used for multi-line text which we don't test here, glyph count is overriden in addFont() @@ -765,8 +768,11 @@ void RendererGLTest::renderIndexTypeChanged() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* Compared to renderClearReset(), the line height is 0 so we can render the 256 glyph prefix on the same spot without having to adjust the cursor to place the next line correctly */ diff --git a/src/Magnum/Text/Test/RendererTest.cpp b/src/Magnum/Text/Test/RendererTest.cpp index 5f5b8deb3..3c15d9366 100644 --- a/src/Magnum/Text/Test/RendererTest.cpp +++ b/src/Magnum/Text/Test/RendererTest.cpp @@ -1863,10 +1863,14 @@ struct TestFont: AbstractFont { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float size, UnsignedInt) override { + _size = size; _opened = true; + } + + Properties doProperties() override { /* Line height isn't used for anything here so can be arbitrary */ - return {size, 4.5f, -2.5f, 10000.0f, 10}; + return {_size, 4.5f, -2.5f, 10000.0f, 10}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D& glyphs) override { @@ -1882,7 +1886,9 @@ struct TestFont: AbstractFont { ShapeDirection direction = ShapeDirection::Unspecified; - bool _opened = false; + private: + Float _size; + bool _opened = false; }; struct DummyGlyphCache: AbstractGlyphCache { @@ -2969,6 +2975,7 @@ void RendererTest::propertiesCoreRenderingInProgress() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -3092,6 +3099,7 @@ void RendererTest::propertiesRenderingInProgress() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -3145,8 +3153,11 @@ void RendererTest::glyphsForRuns() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { return {1.0f, 1.0f, -1.0f, 1.0f, 0}; } @@ -3158,7 +3169,7 @@ void RendererTest::glyphsForRuns() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile("", {}); glyphCache.addFont(1, &font); struct: AbstractShaper { @@ -3228,8 +3239,11 @@ void RendererTest::glyphsForRunsInvalid() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { return {1.0f, 1.0f, -1.0f, 1.0f, 0}; } @@ -3241,7 +3255,7 @@ void RendererTest::glyphsForRunsInvalid() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile("", {}); glyphCache.addFont(1, &font); struct: AbstractShaper { @@ -3299,13 +3313,16 @@ void RendererTest::allocateCore() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances, ascent & descent is used to align the block. Line height is used for multi-line text which we don't test here, glyph count is overriden in addFont() below. */ - return {size, 2.5f, -1.0f, 10000.0f, 0}; + return {1.0f, 2.5f, -1.0f, 10000.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -3316,7 +3333,7 @@ void RendererTest::allocateCore() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile("", {}); UnsignedInt fontId = glyphCache.addFont(23*2, &font); /* Add just the first few glyphs, in shuffled order to not have their IDs match the clusters */ @@ -3547,6 +3564,7 @@ void RendererTest::allocateCoreGlyphAllocator() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -3805,6 +3823,7 @@ void RendererTest::allocateCoreGlyphAllocatorInvalid() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -3906,6 +3925,7 @@ void RendererTest::allocateCoreRunAllocator() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -4122,6 +4142,7 @@ void RendererTest::allocateCoreRunAllocatorInvalid() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -4244,13 +4265,16 @@ template void RendererTest::allocate() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances, ascent & descent is used to align the block. Line height is used for multi-line text which we don't test here, glyph count is overriden in addFont() below. */ - return {size, 2.5f, -1.0f, 10000.0f, 0}; + return {1.0f, 2.5f, -1.0f, 10000.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -4261,7 +4285,7 @@ template void RendererTest::allocate() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile("", {}); UnsignedInt fontId = glyphCache.addFont(23*2, &font); /* Add just the first few glyphs, in shuffled order to not have their IDs match the clusters. Just the simplest possible sizes to verify that the @@ -4796,6 +4820,7 @@ void RendererTest::allocateIndexAllocator() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -5014,6 +5039,7 @@ void RendererTest::allocateIndexAllocatorInvalid() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -5133,6 +5159,7 @@ void RendererTest::allocateVertexAllocator() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -5386,6 +5413,7 @@ void RendererTest::allocateVertexAllocatorInvalid() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -5482,6 +5510,7 @@ void RendererTest::allocateVertexAllocatorNotEnoughStrideForArrayGlyphCache() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -5588,12 +5617,16 @@ void RendererTest::addSingleLine() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float size, UnsignedInt) override { + _size = size; _opened = true; + } + + Properties doProperties() override { /* The size is used to scale everything. Ascent, descent is used for the bounds rect. Line height isn't used for anything, glyph count is overriden in addFont() below. */ - return {size, 16.0f, -8.0f, 1000.0f, 0}; + return {_size, 16.0f, -8.0f, 1000.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -5602,6 +5635,7 @@ void RendererTest::addSingleLine() { Containers::Pointer doCreateShaper() override { return {}; } private: + Float _size; bool _opened = false; } font1, font2; /* Two fonts that do the same but each is opened with a different size */ @@ -5880,12 +5914,16 @@ void RendererTest::addMultipleLines() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float size, UnsignedInt) override { + _size = size; _opened = true; + } + + Properties doProperties() override { /* The size is used to scale everything. Ascent, descent, line height is used for the bounds rect. Glyph count is overriden in addFont() below. */ - return {size, 16.0f*size, -8.0f*size, 32.0f*size, 0}; + return {_size, 16.0f*_size, -8.0f*_size, 32.0f*_size, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -5894,6 +5932,7 @@ void RendererTest::addMultipleLines() { Containers::Pointer doCreateShaper() override { return {}; } private: + Float _size; bool _opened = false; } font1, font2; font1.openFile("", 1.0f); @@ -6128,11 +6167,14 @@ void RendererTest::addMultipleLinesAlign() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* Compared to the glyph bounds, which are from 0 to 2, this is shifted by one unit, thus by 0.5 in the output */ - return {size, 1.0f, -1.0f, 8.0f, 10}; + return {0.5f, 1.0f, -1.0f, 8.0f, 10}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D& glyphs) override { @@ -6146,7 +6188,7 @@ void RendererTest::addMultipleLinesAlign() { bool _opened = false; } font; - font.openFile({}, 0.5f); + font.openFile({}, {}); struct: AbstractShaper { using AbstractShaper::AbstractShaper; @@ -6237,6 +6279,7 @@ void RendererTest::addFontNotFoundInCache() { bool doIsOpened() const override { return true; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -6292,9 +6335,13 @@ void RendererTest::multipleBlocks() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float size, UnsignedInt) override { + _size = size; _opened = true; - return {size, 2.0f*size, -1.0f*size, 4.0f*size, 0}; + } + + Properties doProperties() override { + return {_size, 2.0f*_size, -1.0f*_size, 4.0f*_size, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D& glyphs) override { @@ -6306,7 +6353,9 @@ void RendererTest::multipleBlocks() { Containers::Pointer doCreateShaper() override { return {}; } - bool _opened = false; + private: + Float _size; + bool _opened = false; } font1, font2; /* Two fonts that do the same but each is opened with a different size */ font1.openFile("", 1.0f); @@ -6479,8 +6528,11 @@ void RendererTest::emptyLines() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { return {4.0f, 5.0f, -3.0f, 8.0f, 0}; } @@ -6492,17 +6544,21 @@ void RendererTest::emptyLines() { Containers::Pointer doCreateShaper() override { return {}; } - bool _opened = false; + private: + bool _opened = false; } font1; - font1.openFile({}, 0.0f); + font1.openFile({}, {}); struct: AbstractFont { FontFeatures doFeatures() const override { return {}; } bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { return {6.0f, 1.0f, -2.0f, 12.0f, 0}; } @@ -6514,9 +6570,10 @@ void RendererTest::emptyLines() { Containers::Pointer doCreateShaper() override { return {}; } - bool _opened = false; + private: + bool _opened = false; } font2; - font2.openFile({}, 0.0f); + font2.openFile({}, {}); struct: AbstractShaper { using AbstractShaper::AbstractShaper; @@ -6648,13 +6705,17 @@ template void RendererTest::indicesVertices() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float size, UnsignedInt) override { + _size = size; _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances, ascent & descent is used to align the block. Line height is used for multi-line text which we don't test here, glyph count is overriden in addFont() below. */ - return {size, 2.0f*size, -1.0f*size, 10000.0f, 0}; + return {_size, 2.0f*_size, -1.0f*_size, 10000.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -6663,6 +6724,7 @@ template void RendererTest::indicesVertices() { Containers::Pointer doCreateShaper() override { return {}; } private: + Float _size; bool _opened = false; } font1, font2; /* The same font open twice with a different size, and the same glyphs @@ -6909,14 +6971,17 @@ void RendererTest::clearResetCore() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances. Ascent & descent is used just for vertical rect size which isn't needed as we can check just that the horizontal size got reset. Line height is used to test that line advance is correctly reset as well. Glyph count is overriden in addFont() below. */ - return {size, 0.0f, 0.0f, 2.0f, 0}; + return {1.0f, 0.0f, 0.0f, 2.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -6927,7 +6992,7 @@ void RendererTest::clearResetCore() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile({}, {}); glyphCache.addFont(1, &font); struct: AbstractShaper { @@ -7090,12 +7155,15 @@ void RendererTest::clearResetCoreAllocators() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances, ascent, descent and line height is used for vertical alignment which we don't need and can stay zero. Glyph count is overriden in addFont() below. */ - return {size, 0.0f, 0.0f, 0.0f, 0}; + return {1.0f, 0.0f, 0.0f, 0.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -7106,7 +7174,7 @@ void RendererTest::clearResetCoreAllocators() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile({}, {}); glyphCache.addFont(1, &font); struct: AbstractShaper { @@ -7250,14 +7318,17 @@ void RendererTest::clearReset() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances. Ascent & descent is used just for vertical rect size which isn't needed as we can check just that the horizontal size got reset. Line height is used to test that line advance is correctly reset as well. Glyph count is overriden in addFont() below. */ - return {size, 0.0f, 0.0f, 2.0f, 0}; + return {1.0f, 0.0f, 0.0f, 2.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -7268,7 +7339,7 @@ void RendererTest::clearReset() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile("", {}); glyphCache.addFont(1, &font); struct: AbstractShaper { @@ -7435,12 +7506,15 @@ void RendererTest::clearResetAllocators() { bool doIsOpened() const override { return _opened; } void doClose() override { _opened = false; } - Properties doOpenFile(Containers::StringView, Float size) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { /* The size is used to scale advances, ascent, descent and line height is used for vertical alignment which we don't need and can stay zero. Glyph count is overriden in addFont() below. */ - return {size, 0.0f, 0.0f, 0.0f, 0}; + return {1.0f, 0.0f, 0.0f, 0.0f, 0}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} @@ -7451,7 +7525,7 @@ void RendererTest::clearResetAllocators() { private: bool _opened = false; } font; - font.openFile("", 1.0f); + font.openFile("", {}); glyphCache.addFont(1, &font); struct: AbstractShaper { diff --git a/src/MagnumPlugins/MagnumFont/MagnumFont.cpp b/src/MagnumPlugins/MagnumFont/MagnumFont.cpp index 7a4523cc9..5dfb0dc85 100644 --- a/src/MagnumPlugins/MagnumFont/MagnumFont.cpp +++ b/src/MagnumPlugins/MagnumFont/MagnumFont.cpp @@ -78,12 +78,17 @@ bool MagnumFont::doIsOpened() const { return _opened && _opened->image; } void MagnumFont::doClose() { _opened = nullptr; } -auto MagnumFont::doOpenData(const Containers::ArrayView data, const Float) -> Properties { +void MagnumFont::doOpenData(Containers::Array&& data, DataFlags, Float, UnsignedInt fontId) { + if(fontId != 0) { + Error{} << "Text::MagnumFont::openData(): cannot open font at index" << fontId << Debug::nospace << ", only one font is available"; + return; + } + if(!_opened) _opened.emplace(); if(!_opened->filePath && !fileCallback()) { Error{} << "Text::MagnumFont::openData(): the font can be opened only from the filesystem or if a file callback is present"; - return {}; + return; } /* Open the configuration file */ @@ -92,23 +97,25 @@ auto MagnumFont::doOpenData(const Containers::ArrayView data, const Utility::Configuration conf(in, Utility::Configuration::Flag::SkipComments); if(!conf.isValid() || conf.isEmpty()) { Error{} << "Text::MagnumFont::openData(): font file is not valid"; - return {}; + return; } /* Check version */ if(conf.value("version") != 1) { Error() << "Text::MagnumFont::openData(): unsupported file version, expected 1 but got" << conf.value("version"); - return {}; + return; } /* Open and load image file. Error messages should be printed by the TgaImporter already, no need to repeat them again. */ Trade::TgaImporter importer; importer.setFileCallback(fileCallback(), fileCallbackUserData()); - if(!importer.openFile(Utility::Path::join(_opened->filePath ? *_opened->filePath : "", conf.value("image")))) return {}; + if(!importer.openFile(Utility::Path::join(_opened->filePath ? *_opened->filePath : "", conf.value("image")))) + return; _opened->image = importer.image2D(0); - if(!_opened->image) return {}; + if(!_opened->image) + return; /* Everything okay, save the data internally */ _opened->conf = Utility::move(conf); @@ -129,19 +136,21 @@ auto MagnumFont::doOpenData(const Containers::ArrayView data, const CORRADE_INTERNAL_ASSERT(glyphId < _opened->glyphs.size()); _opened->glyphId.emplace(c->value("unicode"), glyphId); } +} +auto MagnumFont::doProperties() -> Properties { return {_opened->conf.value("fontSize"), _opened->conf.value("ascent"), _opened->conf.value("descent"), _opened->conf.value("lineHeight"), - UnsignedInt(glyphs.size())}; + UnsignedInt(_opened->glyphs.size())}; } -auto MagnumFont::doOpenFile(const Containers::StringView filename, const Float size) -> Properties { +void MagnumFont::doOpenFile(const Containers::StringView filename, const Float size, const UnsignedInt fontId) { _opened.emplace(); _opened->filePath.emplace(Utility::Path::path(filename)); - return AbstractFont::doOpenFile(filename, size); + return AbstractFont::doOpenFile(filename, size, fontId); } void MagnumFont::doGlyphIdsInto(const Containers::StridedArrayView1D& characters, const Containers::StridedArrayView1D& glyphs) { diff --git a/src/MagnumPlugins/MagnumFont/MagnumFont.h b/src/MagnumPlugins/MagnumFont/MagnumFont.h index 1afd3d128..707e16c6c 100644 --- a/src/MagnumPlugins/MagnumFont/MagnumFont.h +++ b/src/MagnumPlugins/MagnumFont/MagnumFont.h @@ -174,10 +174,11 @@ class MAGNUM_MAGNUMFONT_EXPORT MagnumFont: public AbstractFont { private: MAGNUM_MAGNUMFONT_LOCAL FontFeatures doFeatures() const override; MAGNUM_MAGNUMFONT_LOCAL bool doIsOpened() const override; - MAGNUM_MAGNUMFONT_LOCAL Properties doOpenData(Containers::ArrayView data, Float) override; - MAGNUM_MAGNUMFONT_LOCAL Properties doOpenFile(Containers::StringView filename, Float) override; + MAGNUM_MAGNUMFONT_LOCAL void doOpenData(Containers::Array&& data, DataFlags dataFlags, Float size, UnsignedInt fontId) override; + MAGNUM_MAGNUMFONT_LOCAL void doOpenFile(Containers::StringView filename, Float size, UnsignedInt fontId) override; MAGNUM_MAGNUMFONT_LOCAL void doClose() override; + MAGNUM_MAGNUMFONT_LOCAL Properties doProperties() override; MAGNUM_MAGNUMFONT_LOCAL void doGlyphIdsInto(const Containers::StridedArrayView1D& characters, const Containers::StridedArrayView1D& glyphs) override; MAGNUM_MAGNUMFONT_LOCAL Vector2 doGlyphSize(UnsignedInt glyph) override; MAGNUM_MAGNUMFONT_LOCAL Vector2 doGlyphAdvance(UnsignedInt glyph) override; diff --git a/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp b/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp index edab0454b..9cd3e303a 100644 --- a/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp +++ b/src/MagnumPlugins/MagnumFont/Test/MagnumFontTest.cpp @@ -51,6 +51,7 @@ struct MagnumFontTest: TestSuite::Tester { explicit MagnumFontTest(); void nonexistent(); + void nonZeroFontId(); void properties(); void shape(); @@ -79,6 +80,7 @@ const struct { MagnumFontTest::MagnumFontTest() { addTests({&MagnumFontTest::nonexistent, + &MagnumFontTest::nonZeroFontId, &MagnumFontTest::properties}); addInstancedTests({&MagnumFontTest::shape}, @@ -111,6 +113,15 @@ void MagnumFontTest::nonexistent() { TestSuite::Compare::StringHasSuffix); } +void MagnumFontTest::nonZeroFontId() { + Containers::Pointer font = _fontManager.instantiate("MagnumFont"); + + Containers::String out; + Error redirectError{&out}; + CORRADE_VERIFY(!font->openData({}, 0.0f, 1)); + CORRADE_COMPARE(out, "Text::MagnumFont::openData(): cannot open font at index 1, only one font is available\n"); +} + void MagnumFontTest::properties() { Containers::Pointer font = _fontManager.instantiate("MagnumFont"); diff --git a/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp b/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp index dc3c8cc52..742c30816 100644 --- a/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp +++ b/src/MagnumPlugins/MagnumFontConverter/Test/MagnumFontConverterTest.cpp @@ -108,8 +108,10 @@ class MyFont: public Text::AbstractFont { private: void doClose() override { _opened = false; } bool doIsOpened() const override { return _opened; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + Properties doProperties() override { return {16.0f, 25.0f, -10.0f, 39.7333f, 4}; } FontFeatures doFeatures() const override { return {}; } @@ -431,6 +433,7 @@ void MagnumFontConverterTest::exportFontImageProcessingGlyphCacheNoDownload() { bool doIsOpened() const override { return false; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -459,6 +462,7 @@ void MagnumFontConverterTest::exportFontArrayCache() { bool doIsOpened() const override { return false; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -489,6 +493,7 @@ void MagnumFontConverterTest::exportFontNotFoundInCache() { bool doIsOpened() const override { return false; } void doClose() override {} + Properties doProperties() override { return {}; } void doGlyphIdsInto(const Containers::StridedArrayView1D&, const Containers::StridedArrayView1D&) override {} Vector2 doGlyphSize(UnsignedInt) override { return {}; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } @@ -518,8 +523,11 @@ void MagnumFontConverterTest::exportFontImageConversionFailed() { FontFeatures doFeatures() const override { return {}; } void doClose() override { _opened = false; } bool doIsOpened() const override { return _opened; } - Properties doOpenFile(Containers::StringView, Float) override { + void doOpenFile(Containers::StringView, Float, UnsignedInt) override { _opened = true; + } + + Properties doProperties() override { return {16.0f, 25.0f, -10.0f, 39.7333f, 3}; }