diff options
author | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2020-08-12 16:42:20 +0200 |
---|---|---|
committer | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2020-08-12 16:42:20 +0200 |
commit | 96e52ecd9fc1906a5d9f50a985f7c56d771f53fb (patch) | |
tree | 9344ed43208106d34cea54c4d8637adca87f063d | |
parent | 699c018a4c8b2d4e3bdf29a2bd5c642ef0b1f54f (diff) |
Add Context::get_source_for_surface()
Fixes #5
-rw-r--r-- | cairomm/context.cc | 15 | ||||
-rw-r--r-- | cairomm/context.h | 10 | ||||
-rw-r--r-- | tests/test-context.cc | 17 |
3 files changed, 41 insertions, 1 deletions
diff --git a/cairomm/context.cc b/cairomm/context.cc index 3eb73fd..25673e8 100644 --- a/cairomm/context.cc +++ b/cairomm/context.cc @@ -632,6 +632,21 @@ RefPtr<const Pattern> Context::get_source() const return get_pattern_wrapper(pattern); } +RefPtr<SurfacePattern> Context::get_source_for_surface() +{ + auto pattern = cairo_get_source(cobj()); + check_object_status_and_throw_exception(*this); + auto pattern_type = cairo_pattern_get_type(pattern); + if (pattern_type != CAIRO_PATTERN_TYPE_SURFACE) + return {}; + return make_refptr_for_instance<SurfacePattern>(new SurfacePattern(pattern, false /* does not have reference */)); +} + +RefPtr<const SurfacePattern> Context::get_source_for_surface() const +{ + return const_cast<Context*>(this)->get_source_for_surface(); +} + double Context::get_tolerance() const { const auto result = cairo_get_tolerance(const_cast<cobject*>(cobj())); diff --git a/cairomm/context.h b/cairomm/context.h index 501519e..6bf6bde 100644 --- a/cairomm/context.h +++ b/cairomm/context.h @@ -1384,10 +1384,18 @@ public: Operator get_operator() const; /// @{ - /** Gets the current source pattern for the Context + /** Gets the current source pattern for the %Context */ RefPtr<Pattern> get_source(); RefPtr<const Pattern> get_source() const; + + /** Gets the current source surface pattern for the %Context, if any. + * + * @returns The source pattern, if it is a surface pattern, + * else an empty RefPtr. + */ + RefPtr<SurfacePattern> get_source_for_surface(); + RefPtr<const SurfacePattern> get_source_for_surface() const; /// @} /** Gets the current tolerance value, as set by set_tolerance() diff --git a/tests/test-context.cc b/tests/test-context.cc index 8dad675..910eb3a 100644 --- a/tests/test-context.cc +++ b/tests/test-context.cc @@ -88,6 +88,7 @@ BOOST_AUTO_TEST_CASE(test_source) Cairo::SolidPattern::create_rgb (1.0, 0.5, 0.25); auto gradient_pattern = Cairo::LinearGradient::create (0.0, 0.0, 1.0, 1.0); + auto surface = Cairo::ImageSurface::create(Cairo::Surface::Format::ARGB32, 1, 1); cr->set_source (solid_pattern); { @@ -105,6 +106,13 @@ BOOST_AUTO_TEST_CASE(test_source) auto retrieved_solid2 = std::dynamic_pointer_cast<const Cairo::SolidPattern>(cr2->get_source ()); BOOST_REQUIRE (retrieved_solid2); + + // Check that get_source_for_surface() returns an empty RefPtr, + // when the source is not a surface. + auto surface_pattern = cr->get_source_for_surface(); + BOOST_CHECK(!surface_pattern); + auto surface_pattern2 = cr2->get_source_for_surface(); + BOOST_CHECK(!surface_pattern2); } cr->set_source (gradient_pattern); @@ -131,6 +139,7 @@ BOOST_AUTO_TEST_CASE(test_source) BOOST_CHECK_EQUAL (0.5, gx); BOOST_CHECK_EQUAL (0.25, bx); } + cr->set_source_rgba (0.1, 0.3, 0.5, 0.7); { auto solid = @@ -143,6 +152,14 @@ BOOST_AUTO_TEST_CASE(test_source) BOOST_CHECK_EQUAL (0.5, bx); BOOST_CHECK_EQUAL (0.7, ax); } + + cr->set_source (surface, 0.0, 0.0); + { + auto surface_pattern = cr->get_source_for_surface(); + BOOST_REQUIRE (surface_pattern); + surface_pattern->set_filter(Cairo::SurfacePattern::Filter::NEAREST); + BOOST_CHECK_EQUAL (to_int(Cairo::SurfacePattern::Filter::NEAREST), to_int(surface_pattern->get_filter())); + } } BOOST_AUTO_TEST_CASE(test_tolerance) |