Browse Source

Text: add cache-related assertions to the AbstractRenderer constructor.

So it's nicer to users and doesn't fire only later during .render().
pull/674/head
Vladimír Vondruš 1 year ago
parent
commit
eb0c2a3755
  1. 8
      src/Magnum/Text/Renderer.cpp
  2. 3
      src/Magnum/Text/Renderer.h
  3. 55
      src/Magnum/Text/Test/RendererGLTest.cpp

8
src/Magnum/Text/Renderer.cpp

@ -1974,6 +1974,14 @@ AbstractRenderer::AbstractRenderer(AbstractFont& font, const AbstractGlyphCache&
}
#endif
/* These two are done in renderVerticesInternal() as well, but it makes
sense to verify the same here already, not only once .render() is
called */
CORRADE_ASSERT(cache.size().z() == 1,
"Text::AbstractRenderer: array glyph caches are not supported", );
CORRADE_ASSERT(cache.findFont(font),
"Text::AbstractRenderer: font not found among" << cache.fontCount() << "fonts in passed glyph cache", );
/* Vertex buffer configuration depends on dimension count, done in subclass */
_mesh.setPrimitive(MeshPrimitive::Triangles);
}

3
src/Magnum/Text/Renderer.h

@ -1664,6 +1664,9 @@ template<UnsignedInt dimensions> class MAGNUM_TEXT_EXPORT BasicRenderer: public
* @param cache Glyph cache
* @param size Font size
* @param alignment Text alignment
*
* Expects that @p font is present in @p cache and that @p cache isn't
* an array.
*/
explicit BasicRenderer(AbstractFont& font, const AbstractGlyphCache& cache, Float size, Alignment alignment = Alignment::LineLeft);
BasicRenderer(AbstractFont&, AbstractGlyphCache&&, Float, Alignment alignment = Alignment::LineLeft) = delete; /**< @overload */

55
src/Magnum/Text/Test/RendererGLTest.cpp

@ -81,6 +81,9 @@ struct RendererGLTest: GL::OpenGLTester {
void renderMeshIndexType();
void mutableText();
void arrayGlyphCache();
void fontNotFoundInCache();
private:
PluginManager::Manager<Trade::AbstractImporter> _manager{"nonexistent"};
@ -219,7 +222,10 @@ RendererGLTest::RendererGLTest() {
addTests({&RendererGLTest::renderMesh,
&RendererGLTest::renderMeshIndexType,
&RendererGLTest::mutableText});
&RendererGLTest::mutableText,
&RendererGLTest::arrayGlyphCache,
&RendererGLTest::fontNotFoundInCache});
/* Load the plugins directly from the build tree. Otherwise they're either
static and already loaded or not present in the build tree */
@ -1069,6 +1075,53 @@ void RendererGLTest::mutableText() {
#endif
}
void RendererGLTest::arrayGlyphCache() {
CORRADE_SKIP_IF_NO_ASSERT();
TestFont font;
font.openFile({}, 0.5f);
struct: AbstractGlyphCache {
using AbstractGlyphCache::AbstractGlyphCache;
GlyphCacheFeatures doFeatures() const override { return {}; }
} cache{PixelFormat::R8Unorm, {100, 100, 3}};
GL::Buffer buffer;
Containers::String out;
Error redirectError{&out};
Renderer2D::render(font, cache, 1.0f, "", buffer, buffer, GL::BufferUsage::StaticDraw);
Renderer2D{font, cache, 1.0f};
CORRADE_COMPARE(out,
"Text::Renderer: array glyph caches are not supported\n"
"Text::AbstractRenderer: array glyph caches are not supported\n");
}
void RendererGLTest::fontNotFoundInCache() {
CORRADE_SKIP_IF_NO_ASSERT();
TestFont font;
font.openFile({}, 0.5f);
struct: AbstractGlyphCache {
using AbstractGlyphCache::AbstractGlyphCache;
GlyphCacheFeatures doFeatures() const override { return {}; }
} cache{PixelFormat::R8Unorm, {100, 100}};
cache.addFont(34);
cache.addFont(25);
GL::Buffer buffer;
Containers::String out;
Error redirectError{&out};
Renderer2D::render(font, cache, 1.0f, "", buffer, buffer, GL::BufferUsage::StaticDraw);
Renderer2D{font, cache, 1.0f};
CORRADE_COMPARE(out,
"Text::Renderer: font not found among 2 fonts in passed glyph cache\n"
"Text::AbstractRenderer: font not found among 2 fonts in passed glyph cache\n");
}
}}}}
CORRADE_TEST_MAIN(Magnum::Text::Test::RendererGLTest)

Loading…
Cancel
Save