Browse Source

python: adapt to Text library changes.

This is a breaking change, yes.
next
Vladimír Vondruš 2 years ago
parent
commit
9937209f49
  1. 17
      src/python/magnum/test/test_text_gl.py
  2. 26
      src/python/magnum/text.cpp

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

26
src/python/magnum/text.cpp

@ -28,7 +28,7 @@
#include <Corrade/Containers/StringStl.h> /** @todo drop once we have our string casters */
#include <Magnum/ImageView.h>
#include <Magnum/Text/AbstractFont.h>
#include <Magnum/Text/DistanceFieldGlyphCache.h>
#include <Magnum/Text/DistanceFieldGlyphCacheGL.h>
#include <Magnum/Text/Renderer.h>
#include "corrade/pluginmanager.h"
@ -91,26 +91,26 @@ void text(py::module_& m) {
py::class_<Text::AbstractGlyphCache> 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_<Text::GlyphCache, Text::AbstractGlyphCache> glyphCache{m, "GlyphCache", "Glyph cache"};
py::class_<Text::GlyphCacheGL, Text::AbstractGlyphCache> glyphCache{m, "GlyphCacheGL", "OpenGL glyph cache"};
glyphCache
.def(py::init<GL::TextureFormat, const Vector2i&, const Vector2i&, const Vector2i&>(), "Constructor", py::arg("internal_format"), py::arg("original_size"), py::arg("size"), py::arg("padding"))
.def(py::init<GL::TextureFormat, const Vector2i&, const Vector2i&>(), "Constructor", py::arg("internal_format"), py::arg("size"), py::arg("padding") = Vector2i{})
.def(py::init<const Vector2i&, const Vector2i&, const Vector2i&>(), "Constructor", py::arg("original_size"), py::arg("size"), py::arg("padding"))
.def(py::init<const Vector2i&, const Vector2i&>(), "Constructor", py::arg("size"), py::arg("padding") = Vector2i{})
.def(py::init<PixelFormat, const Vector2i&, const Vector2i&>(), "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_<Text::DistanceFieldGlyphCache, Text::GlyphCache> distanceFieldGlyphCache{m, "DistanceFieldGlyphCache", "Glyph cache with distance field rendering"};
.def_property_readonly("texture", &Text::GlyphCacheGL::texture, "Cache texture");
py::class_<Text::DistanceFieldGlyphCacheGL, Text::GlyphCacheGL> distanceFieldGlyphCache{m, "DistanceFieldGlyphCacheGL", "OpenGL glyph cache with distance field rendering"};
distanceFieldGlyphCache
.def(py::init<const Vector2i&, const Vector2i&, UnsignedInt>(), "Constructor", py::arg("original_size"), py::arg("size"), py::arg("radius"))
/** @todo setDistanceFieldImage, once needed for anything */
;
.def(py::init<const Vector2i&, const Vector2i&, UnsignedInt>(), "Constructor", py::arg("size"), py::arg("processed_size"), py::arg("radius"));
/* Font */
py::class_<Text::AbstractFont, PluginManager::PyPluginHolder<Text::AbstractFont>, PluginManager::AbstractPlugin> abstractFont{m, "AbstractFont", "Interface for font plugins"};
@ -186,7 +186,7 @@ void text(py::module_& m) {
currently */
py::class_<Text::Renderer2D> renderer2D{m, "Renderer2D", "2D text renderer"};
renderer2D
.def(py::init<Text::AbstractFont&, const Text::GlyphCache&, Float, Text::Alignment>(), "Constructor", py::arg("font"), py::arg("cache"), py::arg("size"), py::arg("alignment") = Text::Alignment::LineLeft)
.def(py::init<Text::AbstractFont&, const Text::GlyphCacheGL&, Float, Text::Alignment>(), "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? */

Loading…
Cancel
Save