diff options
author | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2020-08-12 16:55:40 +0200 |
---|---|---|
committer | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2020-08-12 16:55:40 +0200 |
commit | 4a48dd2d5dacb9a0ba8fdc357d56eb13e20680c4 (patch) | |
tree | 7ff902f76a0db3b494898f9d853abb687918caa3 | |
parent | 442b9b9ebb2f4ac38c3c048e27fcbb3876040429 (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 dd2c6dc..a4125ba 100644 --- a/cairomm/context.cc +++ b/cairomm/context.cc @@ -701,6 +701,21 @@ RefPtr<const Pattern> Context::get_source() const return RefPtr<const Pattern>::cast_const (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 RefPtr<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 93ab2ce..c8c1c43 100644 --- a/cairomm/context.h +++ b/cairomm/context.h @@ -1300,10 +1300,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 d2c359b..4d0a959 100644 --- a/tests/test-context.cc +++ b/tests/test-context.cc @@ -84,6 +84,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::FORMAT_ARGB32, 1, 1); cr->set_source (solid_pattern); { @@ -101,6 +102,13 @@ BOOST_AUTO_TEST_CASE(test_source) auto retrieved_solid2 = Cairo::RefPtr<const Cairo::SolidPattern>::cast_dynamic(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); @@ -127,6 +135,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 = @@ -139,6 +148,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::FILTER_NEAREST); + BOOST_CHECK_EQUAL (Cairo::FILTER_NEAREST, surface_pattern->get_filter()); + } } BOOST_AUTO_TEST_CASE(test_tolerance) |