From a962262b6128207af61ad4bf8196431d0171b6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 1 Aug 2013 17:59:20 +0200 Subject: [PATCH] Reducing pointer chasings, part 3g: less pointer passing in Text. The testing is now slightly more sloppy due to inability to not pass any Font or GlyphCache object. But it is actually better from user point of view, as it is now impossible to do that by accident. --- src/Text/AbstractFont.cpp | 6 ++--- src/Text/AbstractFont.h | 8 +++---- src/Text/AbstractFontConverter.cpp | 24 +++++++++---------- src/Text/AbstractFontConverter.h | 24 +++++++++---------- src/Text/DistanceFieldGlyphCache.cpp | 4 ++-- src/Text/GlyphCache.h | 2 +- src/Text/Test/AbstractFontConverterTest.cpp | 20 ++++++++-------- src/Text/Test/AbstractFontTest.cpp | 2 +- src/Text/Test/GlyphCacheGLTest.cpp | 2 +- src/Text/Test/TextRendererGLTest.cpp | 12 +++++----- src/Text/TextRenderer.cpp | 24 +++++++++---------- src/Text/TextRenderer.h | 26 +++++++++++---------- 12 files changed, 78 insertions(+), 76 deletions(-) diff --git a/src/Text/AbstractFont.cpp b/src/Text/AbstractFont.cpp index 6646f47a0..52c9feaff 100644 --- a/src/Text/AbstractFont.cpp +++ b/src/Text/AbstractFont.cpp @@ -115,7 +115,7 @@ Vector2 AbstractFont::glyphAdvance(const UnsignedInt glyph) { return doGlyphAdvance(glyph); } -void AbstractFont::fillGlyphCache(GlyphCache* const cache, const std::string& characters) { +void AbstractFont::fillGlyphCache(GlyphCache& cache, const std::string& characters) { CORRADE_ASSERT(isOpened(), "Text::AbstractFont::createGlyphCache(): no font opened", ); CORRADE_ASSERT(!(features() & Feature::PreparedGlyphCache), @@ -124,7 +124,7 @@ void AbstractFont::fillGlyphCache(GlyphCache* const cache, const std::string& ch doFillGlyphCache(cache, Utility::Unicode::utf32(characters)); } -void AbstractFont::doFillGlyphCache(GlyphCache*, const std::u32string&) { +void AbstractFont::doFillGlyphCache(GlyphCache&, const std::u32string&) { CORRADE_ASSERT(false, "Text::AbstractFont::fillGlyphCache(): feature advertised but not implemented", ); } @@ -141,7 +141,7 @@ GlyphCache* AbstractFont::doCreateGlyphCache() { CORRADE_ASSERT(false, "Text::AbstractFont::createGlyphCache(): feature advertised but not implemented", nullptr); } -AbstractLayouter* AbstractFont::layout(const GlyphCache* const cache, const Float size, const std::string& text) { +AbstractLayouter* AbstractFont::layout(const GlyphCache& cache, const Float size, const std::string& text) { CORRADE_ASSERT(isOpened(), "Text::AbstractFont::layout(): no font opened", nullptr); return doLayout(cache, size, text); diff --git a/src/Text/AbstractFont.h b/src/Text/AbstractFont.h index cf6a814bd..bab9763cd 100644 --- a/src/Text/AbstractFont.h +++ b/src/Text/AbstractFont.h @@ -175,7 +175,7 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin { * @ref Feature "Feature::PreparedGlyphCache" do not support partial * glyph cache filling, use createGlyphCache() instead. */ - void fillGlyphCache(GlyphCache* cache, const std::string& characters); + void fillGlyphCache(GlyphCache& cache, const std::string& characters); /** * @brief Create glyph cache @@ -195,7 +195,7 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin { * * @see fillGlyphCache(), createGlyphCache() */ - AbstractLayouter* layout(const GlyphCache* cache, Float size, const std::string& text); + AbstractLayouter* layout(const GlyphCache& cache, Float size, const std::string& text); #ifdef DOXYGEN_GENERATING_OUTPUT private: @@ -251,7 +251,7 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin { * The string is converted from UTF-8 to UTF-32, unique characters are * *not* removed. */ - virtual void doFillGlyphCache(GlyphCache* cache, const std::u32string& characters); + virtual void doFillGlyphCache(GlyphCache& cache, const std::u32string& characters); /** * @brief Implementation for createGlyphCache() @@ -259,7 +259,7 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin { virtual GlyphCache* doCreateGlyphCache(); /** @brief Implementation for layout() */ - virtual AbstractLayouter* doLayout(const GlyphCache* cache, Float size, const std::string& text) = 0; + virtual AbstractLayouter* doLayout(const GlyphCache& cache, Float size, const std::string& text) = 0; }; CORRADE_ENUMSET_OPERATORS(AbstractFont::Features) diff --git a/src/Text/AbstractFontConverter.cpp b/src/Text/AbstractFontConverter.cpp index 94c626c31..8487a0f61 100644 --- a/src/Text/AbstractFontConverter.cpp +++ b/src/Text/AbstractFontConverter.cpp @@ -36,14 +36,14 @@ AbstractFontConverter::AbstractFontConverter() = default; AbstractFontConverter::AbstractFontConverter(PluginManager::AbstractManager* manager, std::string plugin): PluginManager::AbstractPlugin(manager, std::move(plugin)) {} -std::vector>> AbstractFontConverter::exportFontToData(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::string& characters) const { +std::vector>> AbstractFontConverter::exportFontToData(AbstractFont& font, GlyphCache& 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>> AbstractFontConverter::doExportFontToData(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::u32string& characters) const { +std::vector>> AbstractFontConverter::doExportFontToData(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::u32string& characters) const { CORRADE_ASSERT(!(features() & Feature::MultiFile), "Text::AbstractFontConverter::exportFontToData(): feature advertised but not implemented", {}); @@ -52,7 +52,7 @@ std::vector>> AbstractFo return std::move(out); } -Containers::Array AbstractFontConverter::exportFontToSingleData(AbstractFont* const font, GlyphCache* const cache, const std::string& characters) const { +Containers::Array AbstractFontConverter::exportFontToSingleData(AbstractFont& font, GlyphCache& 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), @@ -61,19 +61,19 @@ Containers::Array AbstractFontConverter::exportFontToSingleData(A return doExportFontToSingleData(font, cache, uniqueUnicode(characters)); } -Containers::Array AbstractFontConverter::doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string&) const { +Containers::Array 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 { +bool AbstractFontConverter::exportFontToFile(AbstractFont& font, GlyphCache& 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 { +bool AbstractFontConverter::doExportFontToFile(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::u32string& characters) const { CORRADE_ASSERT(features() & Feature::ConvertData, "Text::AbstractFontConverter::exportFontToFile(): not implemented", false); @@ -94,14 +94,14 @@ bool AbstractFontConverter::doExportFontToFile(AbstractFont* const font, GlyphCa return true; } -std::vector>> AbstractFontConverter::exportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const { +std::vector>> 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>> AbstractFontConverter::doExportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const { +std::vector>> AbstractFontConverter::doExportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const { CORRADE_ASSERT(!(features() & Feature::MultiFile), "Text::AbstractFontConverter::exportGlyphCacheToData(): feature advertised but not implemented", {}); @@ -110,7 +110,7 @@ std::vector>> AbstractFo return std::move(out); } -Containers::Array AbstractFontConverter::exportGlyphCacheToSingleData(GlyphCache* cache) const { +Containers::Array AbstractFontConverter::exportGlyphCacheToSingleData(GlyphCache& cache) const { CORRADE_ASSERT(features() >= (Feature::ExportGlyphCache|Feature::ConvertData), "Text::AbstractFontConverter::exportGlyphCacheToSingleData(): feature not supported", nullptr); CORRADE_ASSERT(!(features() & Feature::MultiFile), @@ -119,19 +119,19 @@ Containers::Array AbstractFontConverter::exportGlyphCacheToSingle return doExportGlyphCacheToSingleData(cache); } -Containers::Array AbstractFontConverter::doExportGlyphCacheToSingleData(GlyphCache*) const { +Containers::Array 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 { +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 { +bool AbstractFontConverter::doExportGlyphCacheToFile(GlyphCache& cache, const std::string& filename) const { CORRADE_ASSERT(features() & Feature::ConvertData, "Text::AbstractFontConverter::exportGlyphCacheToFile(): not implemented", false); diff --git a/src/Text/AbstractFontConverter.h b/src/Text/AbstractFontConverter.h index c4508a7c0..2ea3c0aad 100644 --- a/src/Text/AbstractFontConverter.h +++ b/src/Text/AbstractFontConverter.h @@ -130,7 +130,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * in that case. * @see features(), exportFontToFile(), exportGlyphCacheToData() */ - std::vector>> exportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::string& characters) const; + std::vector>> exportFontToData(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::string& characters) const; /** * @brief Export font to single raw data @@ -142,7 +142,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * more information. * @see features(), exportFontToFile(), importFromSingleData() */ - Containers::Array exportFontToSingleData(AbstractFont* font, GlyphCache* cache, const std::string& characters) const; + Containers::Array exportFontToSingleData(AbstractFont& font, GlyphCache& cache, const std::string& characters) const; /** * @brief Export font to file @@ -154,7 +154,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * `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; + bool exportFontToFile(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::string& characters) const; /** * @brief Export glyph cache to raw data @@ -173,7 +173,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * smaller subset, fill the cache with less characters. * @see features(), exportGlyphCacheToFile(), exportFontToData() */ - std::vector>> exportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const; + std::vector>> exportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const; /** * @brief Export glyph cache to single raw data @@ -185,7 +185,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * for more information. * @see features(), exportGlyphCacheToFile(), importGlyphCacheFromSingleData() */ - Containers::Array exportGlyphCacheToSingleData(GlyphCache* cache) const; + Containers::Array exportGlyphCacheToSingleData(GlyphCache& cache) const; /** * @brief Export glyph cache to file @@ -197,7 +197,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * `false` otherwise. * @see features(), exportGlyphCacheToData(), exportFontToFile() */ - bool exportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const; + bool exportGlyphCacheToFile(GlyphCache& cache, const std::string& filename) const; /** * @brief Import glyph cache from raw data @@ -251,10 +251,10 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * If the plugin doesn't have @ref Feature "Feature::MultiFile", * default implementation calls doExportFontToSingleData(). */ - virtual std::vector>> doExportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::u32string& characters) const; + virtual std::vector>> doExportFontToData(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::u32string& characters) const; /** @brief Implementation for exportFontToSingleData() */ - virtual Containers::Array doExportFontToSingleData(AbstractFont* font, GlyphCache* cache, const std::u32string& characters) const; + virtual Containers::Array doExportFontToSingleData(AbstractFont& font, GlyphCache& cache, const std::u32string& characters) const; /** * @brief Implementation for exportFontToFile() @@ -263,7 +263,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * 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; + virtual bool doExportFontToFile(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::u32string& characters) const; /** * @brief Implementation for exportGlyphCacheToData() @@ -271,10 +271,10 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * If the plugin doesn't have @ref Feature "Feature::MultiFile", * default implementation calls doExportGlyphCacheToSingleData(). */ - virtual std::vector>> doExportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const; + virtual std::vector>> doExportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const; /** @brief Implementation for exportGlyphCacheToSingleData() */ - virtual Containers::Array doExportGlyphCacheToSingleData(GlyphCache* cache) const; + virtual Containers::Array doExportGlyphCacheToSingleData(GlyphCache& cache) const; /** * @brief Implementation for exportGlyphCacheToFile() @@ -283,7 +283,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl * implementation calls doExportGlyphCacheToData() and saves the result * to given file(s). */ - virtual bool doExportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const; + virtual bool doExportGlyphCacheToFile(GlyphCache& cache, const std::string& filename) const; /** * @brief Implementation for importGlyphCacheFromData() diff --git a/src/Text/DistanceFieldGlyphCache.cpp b/src/Text/DistanceFieldGlyphCache.cpp index 18dd71ff1..476ee61b7 100644 --- a/src/Text/DistanceFieldGlyphCache.cpp +++ b/src/Text/DistanceFieldGlyphCache.cpp @@ -80,7 +80,7 @@ void DistanceFieldGlyphCache::setImage(const Vector2i& offset, const ImageRefere .setImage(0, internalFormat, image); /* Create distance field from input texture */ - TextureTools::distanceField(&input, texture(), Rectanglei::fromSize(offset*scale, image.size()*scale), radius, image.size()); + TextureTools::distanceField(input, texture(), Rectanglei::fromSize(offset*scale, image.size()*scale), radius, image.size()); } void DistanceFieldGlyphCache::setDistanceFieldImage(const Vector2i& offset, const ImageReference2D& image) { @@ -97,7 +97,7 @@ void DistanceFieldGlyphCache::setDistanceFieldImage(const Vector2i& offset, cons "Text::DistanceFieldGlyphCache::setDistanceFieldImage(): expected" << ImageFormat::RGB << "but got" << image.format(), ); #endif - texture()->setSubImage(0, offset, image); + texture().setSubImage(0, offset, image); } }} diff --git a/src/Text/GlyphCache.h b/src/Text/GlyphCache.h index e0d8a7f0c..e587bf410 100644 --- a/src/Text/GlyphCache.h +++ b/src/Text/GlyphCache.h @@ -114,7 +114,7 @@ class MAGNUM_TEXT_EXPORT GlyphCache { std::size_t glyphCount() const { return glyphs.size(); } /** @brief Cache texture */ - Texture2D* texture() { return &_texture; } + Texture2D& texture() { return _texture; } /** * @brief Parameters of given glyph diff --git a/src/Text/Test/AbstractFontConverterTest.cpp b/src/Text/Test/AbstractFontConverterTest.cpp index 5aedd2830..643f45242 100644 --- a/src/Text/Test/AbstractFontConverterTest.cpp +++ b/src/Text/Test/AbstractFontConverterTest.cpp @@ -70,7 +70,7 @@ void AbstractFontConverterTest::convertGlyphs() { private: Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont; } - Containers::Array doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string& characters) const override { + Containers::Array doExportFontToSingleData(AbstractFont&, GlyphCache&, const std::u32string& characters) const override { this->characters = characters; return nullptr; } @@ -80,7 +80,7 @@ void AbstractFontConverterTest::convertGlyphs() { std::u32string characters; GlyphExporter exporter(characters); - exporter.exportFontToSingleData(nullptr, nullptr, "abC01a0 "); + exporter.exportFontToSingleData(*static_cast(nullptr), *static_cast(nullptr), "abC01a0 "); CORRADE_COMPARE(characters, (std::u32string{ U' ', U'0', U'1', U'C', U'a', U'b'})); } @@ -90,7 +90,7 @@ void AbstractFontConverterTest::exportFontToSingleData() { private: Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont; } - Containers::Array doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string&) const override { + Containers::Array doExportFontToSingleData(AbstractFont&, GlyphCache&, const std::u32string&) const override { Containers::Array data(1); data[0] = 0xee; return std::move(data); @@ -99,7 +99,7 @@ void AbstractFontConverterTest::exportFontToSingleData() { /* doExportFontToData() should call doExportFontToSingleData() */ SingleDataExporter exporter; - auto ret = exporter.exportFontToData(nullptr, nullptr, "font.out", {}); + auto ret = exporter.exportFontToData(*static_cast(nullptr), *static_cast(nullptr), "font.out", {}); CORRADE_COMPARE(ret.size(), 1); CORRADE_COMPARE(ret[0].first, "font.out"); CORRADE_COMPARE(ret[0].second.size(), 1); @@ -111,7 +111,7 @@ void AbstractFontConverterTest::exportFontToFile() { private: Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont|Feature::MultiFile; } - std::vector>> doExportFontToData(AbstractFont*, GlyphCache*, const std::string& filename, const std::u32string&) const override { + std::vector>> doExportFontToData(AbstractFont&, GlyphCache&, const std::string& filename, const std::u32string&) const override { Containers::Array file(1); file[0] = 0xf0; @@ -132,7 +132,7 @@ void AbstractFontConverterTest::exportFontToFile() { /* doExportToFile() should call doExportToData() */ DataExporter exporter; - bool exported = exporter.exportFontToFile(nullptr, nullptr, Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "font.out"), {}); + bool exported = exporter.exportFontToFile(*static_cast(nullptr), *static_cast(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); @@ -145,7 +145,7 @@ void AbstractFontConverterTest::exportGlyphCacheToSingleData() { private: Features doFeatures() const override { return Feature::ConvertData|Feature::ExportGlyphCache; } - Containers::Array doExportGlyphCacheToSingleData(GlyphCache*) const override { + Containers::Array doExportGlyphCacheToSingleData(GlyphCache&) const override { Containers::Array data(1); data[0] = 0xee; return std::move(data); @@ -154,7 +154,7 @@ void AbstractFontConverterTest::exportGlyphCacheToSingleData() { /* doExportGlyphCacheToData() should call doExportGlyphCacheToSingleData() */ SingleDataExporter exporter; - auto ret = exporter.exportGlyphCacheToData(nullptr, "font.out"); + auto ret = exporter.exportGlyphCacheToData(*static_cast(nullptr), "font.out"); CORRADE_COMPARE(ret.size(), 1); CORRADE_COMPARE(ret[0].first, "font.out"); CORRADE_COMPARE(ret[0].second.size(), 1); @@ -166,7 +166,7 @@ void AbstractFontConverterTest::exportGlyphCacheToFile() { private: Features doFeatures() const override { return Feature::ConvertData|Feature::ExportGlyphCache|Feature::MultiFile; } - std::vector>> doExportGlyphCacheToData(GlyphCache*, const std::string& filename) const override { + std::vector>> doExportGlyphCacheToData(GlyphCache&, const std::string& filename) const override { Containers::Array file(1); file[0] = 0xf0; @@ -187,7 +187,7 @@ void AbstractFontConverterTest::exportGlyphCacheToFile() { /* doExportGlyphCacheToFile() should call doExportGlyphCacheToData() */ DataExporter exporter; - bool exported = exporter.exportGlyphCacheToFile(nullptr, Utility::Directory::join(TEXT_TEST_OUTPUT_DIR, "glyphcache.out")); + bool exported = exporter.exportGlyphCacheToFile(*static_cast(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); diff --git a/src/Text/Test/AbstractFontTest.cpp b/src/Text/Test/AbstractFontTest.cpp index ae5bf4932..1a927ea3e 100644 --- a/src/Text/Test/AbstractFontTest.cpp +++ b/src/Text/Test/AbstractFontTest.cpp @@ -63,7 +63,7 @@ class SingleDataFont: public Text::AbstractFont { Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } - AbstractLayouter* doLayout(const GlyphCache*, Float, const std::string&) { + AbstractLayouter* doLayout(const GlyphCache&, Float, const std::string&) override { return nullptr; } diff --git a/src/Text/Test/GlyphCacheGLTest.cpp b/src/Text/Test/GlyphCacheGLTest.cpp index 0e2b42874..61790a90e 100644 --- a/src/Text/Test/GlyphCacheGLTest.cpp +++ b/src/Text/Test/GlyphCacheGLTest.cpp @@ -47,7 +47,7 @@ void GlyphCacheGLTest::initialize() { MAGNUM_VERIFY_NO_ERROR(); #ifndef MAGNUM_TARGET_GLES - CORRADE_COMPARE(cache.texture()->imageSize(0), Vector2i(1024, 2048)); + CORRADE_COMPARE(cache.texture().imageSize(0), Vector2i(1024, 2048)); #endif } diff --git a/src/Text/Test/TextRendererGLTest.cpp b/src/Text/Test/TextRendererGLTest.cpp index fdfc9f5c8..13ed94b10 100644 --- a/src/Text/Test/TextRendererGLTest.cpp +++ b/src/Text/Test/TextRendererGLTest.cpp @@ -73,7 +73,7 @@ class TestFont: public Text::AbstractFont { UnsignedInt doGlyphId(char32_t) override { return 0; } Vector2 doGlyphAdvance(UnsignedInt) override { return {}; } - AbstractLayouter* doLayout(const GlyphCache*, Float size, const std::string& text) override { + AbstractLayouter* doLayout(const GlyphCache&, Float size, const std::string& text) override { return new TestLayouter(size, text.size()); } }; @@ -86,7 +86,7 @@ void TextRendererGLTest::renderData() { std::vector textureCoordinates; std::vector indices; Rectangle bounds; - std::tie(positions, textureCoordinates, indices, bounds) = Text::AbstractTextRenderer::render(&font, nullptr, 0.25f, "abc"); + std::tie(positions, textureCoordinates, indices, bounds) = Text::AbstractTextRenderer::render(font, *static_cast(nullptr), 0.25f, "abc"); /* Three glyphs, three quads -> 12 vertices, 18 indices */ CORRADE_COMPARE(positions.size(), 12); @@ -165,7 +165,7 @@ void TextRendererGLTest::renderMesh() { Mesh mesh; Buffer vertexBuffer, indexBuffer; Rectangle bounds; - std::tie(mesh, bounds) = Text::TextRenderer3D::render(&font, nullptr, 0.25f, "abc", &vertexBuffer, &indexBuffer, Buffer::Usage::StaticDraw); + std::tie(mesh, bounds) = Text::TextRenderer3D::render(font, *static_cast(nullptr), 0.25f, "abc", vertexBuffer, indexBuffer, Buffer::Usage::StaticDraw); MAGNUM_VERIFY_NO_ERROR(); /** @todo How to verify this on ES? */ @@ -203,7 +203,7 @@ void TextRendererGLTest::renderMesh() { void TextRendererGLTest::mutableText() { TestFont font; - Text::TextRenderer2D renderer(&font, nullptr, 0.25f); + Text::TextRenderer2D renderer(font, *static_cast(nullptr), 0.25f); MAGNUM_VERIFY_NO_ERROR(); CORRADE_COMPARE(renderer.capacity(), 0); CORRADE_COMPARE(renderer.rectangle(), Rectangle()); @@ -214,7 +214,7 @@ void TextRendererGLTest::mutableText() { CORRADE_COMPARE(renderer.capacity(), 4); /** @todo How to verify this on ES? */ #ifndef MAGNUM_TARGET_GLES - Containers::Array indices = renderer.indexBuffer()->data(); + Containers::Array indices = renderer.indexBuffer().data(); CORRADE_COMPARE(std::vector(indices.begin(), indices.end()), (std::vector{ 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6, @@ -228,7 +228,7 @@ void TextRendererGLTest::mutableText() { MAGNUM_VERIFY_NO_ERROR(); /** @todo How to verify this on ES? */ #ifndef MAGNUM_TARGET_GLES - Containers::Array vertices = renderer.vertexBuffer()->subData(0, 48); + Containers::Array vertices = renderer.vertexBuffer().subData(0, 48); CORRADE_COMPARE(std::vector(vertices.begin(), vertices.end()), (std::vector{ 0.0f, 0.5f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, diff --git a/src/Text/TextRenderer.cpp b/src/Text/TextRenderer.cpp index 12d977741..b263e6587 100644 --- a/src/Text/TextRenderer.cpp +++ b/src/Text/TextRenderer.cpp @@ -60,8 +60,8 @@ struct Vertex { } -std::tuple, std::vector, std::vector, Rectangle> AbstractTextRenderer::render(AbstractFont* const font, const GlyphCache* const cache, Float size, const std::string& text) { - AbstractLayouter* const layouter = font->layout(cache, size, text); +std::tuple, std::vector, std::vector, Rectangle> AbstractTextRenderer::render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text) { + AbstractLayouter* const layouter = font.layout(cache, size, text); const UnsignedInt vertexCount = layouter->glyphCount()*4; /* Output data */ @@ -119,8 +119,8 @@ std::tuple, std::vector, std::vector, return std::make_tuple(std::move(positions), std::move(texcoords), std::move(indices), rectangle); } -std::tuple AbstractTextRenderer::render(AbstractFont* const font, const GlyphCache* const cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage) { - AbstractLayouter* const layouter = font->layout(cache, size, text); +std::tuple AbstractTextRenderer::render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text, Buffer& vertexBuffer, Buffer& indexBuffer, Buffer::Usage usage) { + AbstractLayouter* const layouter = font.layout(cache, size, text); const UnsignedInt vertexCount = layouter->glyphCount()*4; const UnsignedInt indexCount = layouter->glyphCount()*6; @@ -158,7 +158,7 @@ std::tuple AbstractTextRenderer::render(AbstractFont* const fon /* Advance cursor position to next character */ cursorPosition += advance; } - vertexBuffer->setData(vertices, usage); + vertexBuffer.setData(vertices, usage); /* Fill index buffer */ Mesh::IndexType indexType; @@ -180,7 +180,7 @@ std::tuple AbstractTextRenderer::render(AbstractFont* const fon indices = new char[indicesSize]; createIndices(indices, layouter->glyphCount()); } - indexBuffer->setData(indicesSize, indices, usage); + indexBuffer.setData(indicesSize, indices, usage); delete indices; /* Configure mesh except for vertex buffer (depends on dimension count, done @@ -194,7 +194,7 @@ std::tuple AbstractTextRenderer::render(AbstractFont* const fon return std::make_tuple(std::move(mesh), rectangle); } -template std::tuple TextRenderer::render(AbstractFont* const font, const GlyphCache* const cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage) { +template std::tuple TextRenderer::render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text, Buffer& vertexBuffer, Buffer& indexBuffer, Buffer::Usage usage) { /* Finalize mesh configuration and return the result */ auto r = AbstractTextRenderer::render(font, cache, size, text, vertexBuffer, indexBuffer, usage); Mesh& mesh = std::get<0>(r); @@ -240,7 +240,7 @@ void AbstractTextRenderer::bufferUnmapImplementationDefault(Buffer& buffer) buffer.unmap(); } -AbstractTextRenderer::AbstractTextRenderer(AbstractFont* const font, const GlyphCache* const cache, Float size): _vertexBuffer(Buffer::Target::Array), _indexBuffer(Buffer::Target::ElementArray), font(font), cache(cache), size(size), _capacity(0) { +AbstractTextRenderer::AbstractTextRenderer(AbstractFont& font, const GlyphCache& cache, Float size): _vertexBuffer(Buffer::Target::Array), _indexBuffer(Buffer::Target::ElementArray), font(font), cache(cache), size(size), _capacity(0) { #ifndef MAGNUM_TARGET_GLES MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::map_buffer_range); #elif defined(MAGNUM_TARGET_GLES2) @@ -264,9 +264,9 @@ AbstractTextRenderer::AbstractTextRenderer(AbstractFont* const font, const Glyph AbstractTextRenderer::~AbstractTextRenderer() {} -template TextRenderer::TextRenderer(AbstractFont* const font, const GlyphCache* const cache, const Float size): AbstractTextRenderer(font, cache, size) { +template TextRenderer::TextRenderer(AbstractFont& font, const GlyphCache& cache, const Float size): AbstractTextRenderer(font, cache, size) { /* Finalize mesh configuration */ - _mesh.addInterleavedVertexBuffer(&_vertexBuffer, 0, + _mesh.addInterleavedVertexBuffer(_vertexBuffer, 0, typename Shaders::AbstractVector::Position(Shaders::AbstractVector::Position::Components::Two), typename Shaders::AbstractVector::TextureCoordinates()); } @@ -296,7 +296,7 @@ void AbstractTextRenderer::reserve(const uint32_t glyphCount, const Buffer::Usag } _indexBuffer.setData(indicesSize, nullptr, indexBufferUsage); _mesh.setIndexCount(0) - .setIndexBuffer(&_indexBuffer, 0, indexType, 0, vertexCount); + .setIndexBuffer(_indexBuffer, 0, indexType, 0, vertexCount); /* Map buffer for filling */ void* const indices = bufferMapImplementation(_indexBuffer, indicesSize); @@ -313,7 +313,7 @@ void AbstractTextRenderer::reserve(const uint32_t glyphCount, const Buffer::Usag } void AbstractTextRenderer::render(const std::string& text) { - AbstractLayouter* layouter = font->layout(cache, size, text); + AbstractLayouter* layouter = font.layout(cache, size, text); CORRADE_ASSERT(layouter->glyphCount() <= _capacity, "Text::TextRenderer::render(): capacity" << _capacity << "too small to render" << layouter->glyphCount() << "glyphs", ); diff --git a/src/Text/TextRenderer.h b/src/Text/TextRenderer.h index 7e03902ba..02c05da66 100644 --- a/src/Text/TextRenderer.h +++ b/src/Text/TextRenderer.h @@ -60,7 +60,7 @@ class MAGNUM_TEXT_EXPORT AbstractTextRenderer { * Returns tuple with vertex positions, texture coordinates, indices * and rectangle spanning the rendered text. */ - static std::tuple, std::vector, std::vector, Rectangle> render(AbstractFont* font, const GlyphCache* cache, Float size, const std::string& text); + static std::tuple, std::vector, std::vector, Rectangle> render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text); /** * @brief Capacity for rendered glyphs @@ -73,13 +73,13 @@ class MAGNUM_TEXT_EXPORT AbstractTextRenderer { Rectangle rectangle() const { return _rectangle; } /** @brief Vertex buffer */ - Buffer* vertexBuffer() { return &_vertexBuffer; } + Buffer& vertexBuffer() { return _vertexBuffer; } /** @brief Index buffer */ - Buffer* indexBuffer() { return &_indexBuffer; } + Buffer& indexBuffer() { return _indexBuffer; } /** @brief Mesh */ - Mesh* mesh() { return &_mesh; } + Mesh& mesh() { return _mesh; } /** * @brief Reserve capacity for rendered glyphs @@ -115,7 +115,8 @@ class MAGNUM_TEXT_EXPORT AbstractTextRenderer { * @param cache Glyph cache * @param size Font size */ - explicit AbstractTextRenderer(AbstractFont* font, const GlyphCache* cache, Float size); + explicit AbstractTextRenderer(AbstractFont& font, const GlyphCache& cache, Float size); + AbstractTextRenderer(AbstractFont&, GlyphCache&&, Float) = delete; /**< @overload */ ~AbstractTextRenderer(); @@ -124,14 +125,14 @@ class MAGNUM_TEXT_EXPORT AbstractTextRenderer { #else private: #endif - static std::tuple MAGNUM_LOCAL render(AbstractFont* font, const GlyphCache* cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage); + static std::tuple MAGNUM_LOCAL render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text, Buffer& vertexBuffer, Buffer& indexBuffer, Buffer::Usage usage); Mesh _mesh; Buffer _vertexBuffer, _indexBuffer; private: - AbstractFont* const font; - const GlyphCache* const cache; + AbstractFont& font; + const GlyphCache& cache; Float size; UnsignedInt _capacity; Rectangle _rectangle; @@ -178,7 +179,7 @@ Mesh mesh; // Render the text Rectangle rectangle; -std::tie(mesh, rectangle) = Text::TextRenderer2D::render(font, cache, 0.15f, +std::tie(mesh, rectangle) = Text::TextRenderer2D::render(*font, cache, 0.15f, "Hello World!", vertexBuffer, indexBuffer, Buffer::Usage::StaticDraw); // Draw white text centered on the screen @@ -201,7 +202,7 @@ Text::GlyphCache cache; Shaders::VectorShader2D shader; // Initialize renderer and reserve memory for enough glyphs -Text::TextRenderer2D renderer(font, cache, 0.15f); +Text::TextRenderer2D renderer(*font, cache, 0.15f); renderer.reserve(32, Buffer::Usage::DynamicDraw, Buffer::Usage::StaticDraw); // Update the text occasionally @@ -239,7 +240,7 @@ template class MAGNUM_TEXT_EXPORT TextRenderer: public A * Returns mesh prepared for use with Shaders::AbstractVectorShader * subclasses and rectangle spanning the rendered text. */ - static std::tuple render(AbstractFont* const font, const GlyphCache* const cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage); + static std::tuple render(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text, Buffer& vertexBuffer, Buffer& indexBuffer, Buffer::Usage usage); /** * @brief Constructor @@ -247,7 +248,8 @@ template class MAGNUM_TEXT_EXPORT TextRenderer: public A * @param cache Glyph cache * @param size Font size */ - explicit TextRenderer(AbstractFont* const font, const GlyphCache* const cache, Float size); + explicit TextRenderer(AbstractFont& font, const GlyphCache& cache, Float size); + TextRenderer(AbstractFont&, GlyphCache&&, Float) = delete; /**< @overload */ using AbstractTextRenderer::render; };