From 9937209f498e66f140cca666c0b8906e4f8bcf6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 5 Oct 2024 16:04:34 +0200 Subject: [PATCH] python: adapt to Text library changes. This is a breaking change, yes. --- src/python/magnum/test/test_text_gl.py | 17 +++++++++++++---- src/python/magnum/text.cpp | 26 +++++++++++++------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/python/magnum/test/test_text_gl.py b/src/python/magnum/test/test_text_gl.py index 134861d..a2e2d2d 100644 --- a/src/python/magnum/test/test_text_gl.py +++ b/src/python/magnum/test/test_text_gl.py @@ -37,11 +37,14 @@ import magnum from magnum import * from magnum import gl, text -class GlyphCache(GLTestCase): +class GlyphCacheGL(GLTestCase): def test(self): - cache = text.GlyphCache((128, 128), (2, 2)) + cache = text.GlyphCacheGL(PixelFormat.R8_UNORM, (128, 128), (2, 2)) + self.assertEqual(cache.format, PixelFormat.R8_UNORM) + self.assertEqual(cache.processed_format, PixelFormat.R8_UNORM) self.assertEqual(cache.size, (128, 128, 1)) + self.assertEqual(cache.processed_size, (128, 128, 1)) self.assertEqual(cache.padding, (2, 2)) cache_refcount = sys.getrefcount(cache) @@ -55,10 +58,16 @@ class GlyphCache(GLTestCase): del texture self.assertEqual(sys.getrefcount(cache), cache_refcount) + def test_implicit_padding(self): + # Ensure the default is correct to avoid artifacts + cache = text.GlyphCacheGL(PixelFormat.R8_UNORM, (128, 128)) + self.assertEqual(cache.padding, (1, 1)) + class DistanceFieldGlyphCache(GLTestCase): def test(self): - cache = text.DistanceFieldGlyphCache((1024, 1024), (128, 128), 2) + cache = text.DistanceFieldGlyphCacheGL((1024, 1024), (128, 128), 2) + self.assertEqual(cache.size, (1024, 1024, 1)) self.assertEqual(cache.size, (1024, 1024, 1)) self.assertEqual(cache.padding, (2, 2)) @@ -68,7 +77,7 @@ class Renderer2D(GLTestCase): font = text.FontManager().load_and_instantiate('StbTrueTypeFont') font.open_file(os.path.join(os.path.dirname(__file__), 'Oxygen.ttf'), 16.0) - cache = text.GlyphCache((128, 128)) + cache = text.GlyphCacheGL(PixelFormat.R8_UNORM, (128, 128)) font.fill_glyph_cache(cache, "hello") renderer = text.Renderer2D(font, cache, 1.0) diff --git a/src/python/magnum/text.cpp b/src/python/magnum/text.cpp index db5e87b..db13bde 100644 --- a/src/python/magnum/text.cpp +++ b/src/python/magnum/text.cpp @@ -28,7 +28,7 @@ #include /** @todo drop once we have our string casters */ #include #include -#include +#include #include #include "corrade/pluginmanager.h" @@ -91,26 +91,26 @@ void text(py::module_& m) { py::class_ abstractGlyphCache{m, "AbstractGlyphCache", "Base for glyph caches"}; abstractGlyphCache /** @todo features */ + .def_property_readonly("format", &Text::AbstractGlyphCache::format, "Glyph cache format") + .def_property_readonly("processed_format", &Text::AbstractGlyphCache::processedFormat, "Processed glyph cache format") .def_property_readonly("size", &Text::AbstractGlyphCache::size, "Glyph cache texture size") + .def_property_readonly("processed_size", &Text::AbstractGlyphCache::processedSize, "Processed glyph cache texture size") .def_property_readonly("padding", &Text::AbstractGlyphCache::padding, "Glyph padding") - /** @todo glyph iteration and population */ + /** @todo font / glyph iteration and population */ + /** @todo image, processedImage, setProcessedImage, once needed for + anything */ ; - py::class_ glyphCache{m, "GlyphCache", "Glyph cache"}; + py::class_ glyphCache{m, "GlyphCacheGL", "OpenGL glyph cache"}; glyphCache - .def(py::init(), "Constructor", py::arg("internal_format"), py::arg("original_size"), py::arg("size"), py::arg("padding")) - .def(py::init(), "Constructor", py::arg("internal_format"), py::arg("size"), py::arg("padding") = Vector2i{}) - .def(py::init(), "Constructor", py::arg("original_size"), py::arg("size"), py::arg("padding")) - .def(py::init(), "Constructor", py::arg("size"), py::arg("padding") = Vector2i{}) + .def(py::init(), "Constructor", py::arg("format"), py::arg("size"), py::arg("padding") = Vector2i{1}) /* The default behavior when returning a reference seems to be that it increfs the originating instance and decrefs it again after the variable gets deleted. This is verified in test_text_gl.py to be extra sure. */ - .def_property_readonly("texture", &Text::GlyphCache::texture, "Cache texture"); - py::class_ distanceFieldGlyphCache{m, "DistanceFieldGlyphCache", "Glyph cache with distance field rendering"}; + .def_property_readonly("texture", &Text::GlyphCacheGL::texture, "Cache texture"); + py::class_ distanceFieldGlyphCache{m, "DistanceFieldGlyphCacheGL", "OpenGL glyph cache with distance field rendering"}; distanceFieldGlyphCache - .def(py::init(), "Constructor", py::arg("original_size"), py::arg("size"), py::arg("radius")) - /** @todo setDistanceFieldImage, once needed for anything */ - ; + .def(py::init(), "Constructor", py::arg("size"), py::arg("processed_size"), py::arg("radius")); /* Font */ py::class_, PluginManager::AbstractPlugin> abstractFont{m, "AbstractFont", "Interface for font plugins"}; @@ -186,7 +186,7 @@ void text(py::module_& m) { currently */ py::class_ renderer2D{m, "Renderer2D", "2D text renderer"}; renderer2D - .def(py::init(), "Constructor", py::arg("font"), py::arg("cache"), py::arg("size"), py::arg("alignment") = Text::Alignment::LineLeft) + .def(py::init(), "Constructor", py::arg("font"), py::arg("cache"), py::arg("size"), py::arg("alignment") = Text::Alignment::LineLeft) .def_property_readonly("capacity", &Text::Renderer2D::capacity, "Capacity for rendered glyphs") .def_property_readonly("rectangle", &Text::Renderer2D::rectangle, "Rectangle spanning the rendered text") /** @todo are the buffers useful for anything? */