Browse Source

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.
pull/277/head
Vladimír Vondruš 13 years ago
parent
commit
a962262b61
  1. 6
      src/Text/AbstractFont.cpp
  2. 8
      src/Text/AbstractFont.h
  3. 24
      src/Text/AbstractFontConverter.cpp
  4. 24
      src/Text/AbstractFontConverter.h
  5. 4
      src/Text/DistanceFieldGlyphCache.cpp
  6. 2
      src/Text/GlyphCache.h
  7. 20
      src/Text/Test/AbstractFontConverterTest.cpp
  8. 2
      src/Text/Test/AbstractFontTest.cpp
  9. 2
      src/Text/Test/GlyphCacheGLTest.cpp
  10. 12
      src/Text/Test/TextRendererGLTest.cpp
  11. 24
      src/Text/TextRenderer.cpp
  12. 26
      src/Text/TextRenderer.h

6
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);

8
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)

24
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<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::exportFontToData(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::string& characters) const {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> 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<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::doExportFontToData(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::u32string& characters) const {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> 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<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFo
return std::move(out);
}
Containers::Array<unsigned char> AbstractFontConverter::exportFontToSingleData(AbstractFont* const font, GlyphCache* const cache, const std::string& characters) const {
Containers::Array<unsigned char> 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<unsigned char> AbstractFontConverter::exportFontToSingleData(A
return doExportFontToSingleData(font, cache, uniqueUnicode(characters));
}
Containers::Array<unsigned char> AbstractFontConverter::doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string&) const {
Containers::Array<unsigned char> AbstractFontConverter::doExportFontToSingleData(AbstractFont&, GlyphCache&, const std::u32string&) const {
CORRADE_ASSERT(false,
"Text::AbstractFontConverter::exportFontToSingleData(): feature advertised but not implemented", nullptr);
}
bool AbstractFontConverter::exportFontToFile(AbstractFont* const font, GlyphCache* const cache, const std::string& filename, const std::string& characters) const {
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<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::exportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::exportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const {
CORRADE_ASSERT(features() >= (Feature::ExportGlyphCache|Feature::ConvertData),
"Text::AbstractFontConverter::exportGlyphCacheToData(): feature not supported", {});
return doExportGlyphCacheToData(cache, filename);
}
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::doExportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFontConverter::doExportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const {
CORRADE_ASSERT(!(features() & Feature::MultiFile),
"Text::AbstractFontConverter::exportGlyphCacheToData(): feature advertised but not implemented", {});
@ -110,7 +110,7 @@ std::vector<std::pair<std::string, Containers::Array<unsigned char>>> AbstractFo
return std::move(out);
}
Containers::Array<unsigned char> AbstractFontConverter::exportGlyphCacheToSingleData(GlyphCache* cache) const {
Containers::Array<unsigned char> AbstractFontConverter::exportGlyphCacheToSingleData(GlyphCache& cache) const {
CORRADE_ASSERT(features() >= (Feature::ExportGlyphCache|Feature::ConvertData),
"Text::AbstractFontConverter::exportGlyphCacheToSingleData(): feature not supported", nullptr);
CORRADE_ASSERT(!(features() & Feature::MultiFile),
@ -119,19 +119,19 @@ Containers::Array<unsigned char> AbstractFontConverter::exportGlyphCacheToSingle
return doExportGlyphCacheToSingleData(cache);
}
Containers::Array<unsigned char> AbstractFontConverter::doExportGlyphCacheToSingleData(GlyphCache*) const {
Containers::Array<unsigned char> AbstractFontConverter::doExportGlyphCacheToSingleData(GlyphCache&) const {
CORRADE_ASSERT(false,
"Text::AbstractFontConverter::exportGlyphCacheToSingleData(): feature advertised but not implemented", nullptr);
}
bool AbstractFontConverter::exportGlyphCacheToFile(GlyphCache* cache, const std::string& filename) const {
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);

24
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<std::pair<std::string, Containers::Array<unsigned char>>> exportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::string& characters) const;
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> exportFontToData(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::string& characters) const;
/**
* @brief Export font to single raw data
@ -142,7 +142,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl
* more information.
* @see features(), exportFontToFile(), importFromSingleData()
*/
Containers::Array<unsigned char> exportFontToSingleData(AbstractFont* font, GlyphCache* cache, const std::string& characters) const;
Containers::Array<unsigned char> 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<std::pair<std::string, Containers::Array<unsigned char>>> exportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const;
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> exportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const;
/**
* @brief Export glyph cache to single raw data
@ -185,7 +185,7 @@ class MAGNUM_TEXT_EXPORT AbstractFontConverter: public PluginManager::AbstractPl
* for more information.
* @see features(), exportGlyphCacheToFile(), importGlyphCacheFromSingleData()
*/
Containers::Array<unsigned char> exportGlyphCacheToSingleData(GlyphCache* cache) const;
Containers::Array<unsigned char> 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<std::pair<std::string, Containers::Array<unsigned char>>> doExportFontToData(AbstractFont* font, GlyphCache* cache, const std::string& filename, const std::u32string& characters) const;
virtual std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportFontToData(AbstractFont& font, GlyphCache& cache, const std::string& filename, const std::u32string& characters) const;
/** @brief Implementation for exportFontToSingleData() */
virtual Containers::Array<unsigned char> doExportFontToSingleData(AbstractFont* font, GlyphCache* cache, const std::u32string& characters) const;
virtual Containers::Array<unsigned char> 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<std::pair<std::string, Containers::Array<unsigned char>>> doExportGlyphCacheToData(GlyphCache* cache, const std::string& filename) const;
virtual std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportGlyphCacheToData(GlyphCache& cache, const std::string& filename) const;
/** @brief Implementation for exportGlyphCacheToSingleData() */
virtual Containers::Array<unsigned char> doExportGlyphCacheToSingleData(GlyphCache* cache) const;
virtual Containers::Array<unsigned char> 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()

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

2
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

20
src/Text/Test/AbstractFontConverterTest.cpp

@ -70,7 +70,7 @@ void AbstractFontConverterTest::convertGlyphs() {
private:
Features doFeatures() const override { return Feature::ConvertData|Feature::ExportFont; }
Containers::Array<unsigned char> doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string& characters) const override {
Containers::Array<unsigned char> 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<AbstractFont*>(nullptr), *static_cast<GlyphCache*>(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<unsigned char> doExportFontToSingleData(AbstractFont*, GlyphCache*, const std::u32string&) const override {
Containers::Array<unsigned char> doExportFontToSingleData(AbstractFont&, GlyphCache&, const std::u32string&) const override {
Containers::Array<unsigned char> data(1);
data[0] = 0xee;
return std::move(data);
@ -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<AbstractFont*>(nullptr), *static_cast<GlyphCache*>(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<std::pair<std::string, Containers::Array<unsigned char>>> doExportFontToData(AbstractFont*, GlyphCache*, const std::string& filename, const std::u32string&) const override {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportFontToData(AbstractFont&, GlyphCache&, const std::string& filename, const std::u32string&) const override {
Containers::Array<unsigned char> file(1);
file[0] = 0xf0;
@ -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<AbstractFont*>(nullptr), *static_cast<GlyphCache*>(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<unsigned char> doExportGlyphCacheToSingleData(GlyphCache*) const override {
Containers::Array<unsigned char> doExportGlyphCacheToSingleData(GlyphCache&) const override {
Containers::Array<unsigned char> 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<GlyphCache*>(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<std::pair<std::string, Containers::Array<unsigned char>>> doExportGlyphCacheToData(GlyphCache*, const std::string& filename) const override {
std::vector<std::pair<std::string, Containers::Array<unsigned char>>> doExportGlyphCacheToData(GlyphCache&, const std::string& filename) const override {
Containers::Array<unsigned char> file(1);
file[0] = 0xf0;
@ -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<GlyphCache*>(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);

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

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

12
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<Vector2> textureCoordinates;
std::vector<UnsignedInt> 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<GlyphCache*>(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<GlyphCache*>(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<GlyphCache*>(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<UnsignedByte> indices = renderer.indexBuffer()->data<UnsignedByte>();
Containers::Array<UnsignedByte> indices = renderer.indexBuffer().data<UnsignedByte>();
CORRADE_COMPARE(std::vector<UnsignedByte>(indices.begin(), indices.end()), (std::vector<UnsignedByte>{
0, 1, 2, 1, 3, 2,
4, 5, 6, 5, 7, 6,
@ -228,7 +228,7 @@ void TextRendererGLTest::mutableText() {
MAGNUM_VERIFY_NO_ERROR();
/** @todo How to verify this on ES? */
#ifndef MAGNUM_TARGET_GLES
Containers::Array<Float> vertices = renderer.vertexBuffer()->subData<Float>(0, 48);
Containers::Array<Float> vertices = renderer.vertexBuffer().subData<Float>(0, 48);
CORRADE_COMPARE(std::vector<Float>(vertices.begin(), vertices.end()), (std::vector<Float>{
0.0f, 0.5f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f, 0.0f,

24
src/Text/TextRenderer.cpp

@ -60,8 +60,8 @@ struct Vertex {
}
std::tuple<std::vector<Vector2>, std::vector<Vector2>, std::vector<UnsignedInt>, 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<Vector2>, std::vector<Vector2>, std::vector<UnsignedInt>, 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<Vector2>, std::vector<Vector2>, std::vector<UnsignedInt>,
return std::make_tuple(std::move(positions), std::move(texcoords), std::move(indices), rectangle);
}
std::tuple<Mesh, Rectangle> 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<Mesh, Rectangle> 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<Mesh, Rectangle> 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<Mesh, Rectangle> AbstractTextRenderer::render(AbstractFont* const fon
indices = new char[indicesSize];
createIndices<UnsignedInt>(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<Mesh, Rectangle> AbstractTextRenderer::render(AbstractFont* const fon
return std::make_tuple(std::move(mesh), rectangle);
}
template<UnsignedInt dimensions> std::tuple<Mesh, Rectangle> TextRenderer<dimensions>::render(AbstractFont* const font, const GlyphCache* const cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage) {
template<UnsignedInt dimensions> std::tuple<Mesh, Rectangle> TextRenderer<dimensions>::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<UnsignedInt dimensions> TextRenderer<dimensions>::TextRenderer(AbstractFont* const font, const GlyphCache* const cache, const Float size): AbstractTextRenderer(font, cache, size) {
template<UnsignedInt dimensions> TextRenderer<dimensions>::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<dimensions>::Position(Shaders::AbstractVector<dimensions>::Position::Components::Two),
typename Shaders::AbstractVector<dimensions>::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", );

26
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<Vector2>, std::vector<Vector2>, std::vector<UnsignedInt>, Rectangle> render(AbstractFont* font, const GlyphCache* cache, Float size, const std::string& text);
static std::tuple<std::vector<Vector2>, std::vector<Vector2>, std::vector<UnsignedInt>, 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<Mesh, Rectangle> MAGNUM_LOCAL render(AbstractFont* font, const GlyphCache* cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage);
static std::tuple<Mesh, Rectangle> 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<UnsignedInt dimensions> 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<Mesh, Rectangle> render(AbstractFont* const font, const GlyphCache* const cache, Float size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage);
static std::tuple<Mesh, Rectangle> 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<UnsignedInt dimensions> 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;
};

Loading…
Cancel
Save