From 4a48dd2d5dacb9a0ba8fdc357d56eb13e20680c4 Mon Sep 17 00:00:00 2001 From: Kjell Ahlstedt Date: Wed, 12 Aug 2020 16:55:40 +0200 Subject: Add Context::get_source_for_surface() Fixes #5 --- cairomm/context.cc | 15 +++++++++++++++ cairomm/context.h | 10 +++++++++- tests/test-context.cc | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) 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 Context::get_source() const return RefPtr::cast_const (get_pattern_wrapper (pattern)); } +RefPtr 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(new SurfacePattern(pattern, false /* does not have reference */)); +} + +RefPtr Context::get_source_for_surface() const +{ + return const_cast(this)->get_source_for_surface(); +} + double Context::get_tolerance() const { const auto result = cairo_get_tolerance(const_cast(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 get_source(); RefPtr 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 get_source_for_surface(); + RefPtr 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::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) -- cgit v1.2.3