summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Holder <thomas@thomas-holder.de>2022-01-18 08:04:51 +0100
committerThomas Holder <thomas@thomas-holder.de>2022-01-18 08:04:51 +0100
commit98e4cce3a1041d17148547bd75ecd617bcbe5916 (patch)
tree9453f6d8cf4d73b857e250f74169163d246b20bd
parent393cd568894d1f07858ed9945022b7964635f9f8 (diff)
Wrapping surface device scale functions
-rw-r--r--cairomm/surface.cc11
-rw-r--r--cairomm/surface.h19
-rw-r--r--tests/test-surface.cc11
3 files changed, 41 insertions, 0 deletions
diff --git a/cairomm/surface.cc b/cairomm/surface.cc
index d7b9708..8e7d076 100644
--- a/cairomm/surface.cc
+++ b/cairomm/surface.cc
@@ -162,6 +162,17 @@ void Surface::get_device_offset(double& x_offset, double& y_offset) const
cairo_surface_get_device_offset(const_cast<cobject*>(cobj()), &x_offset, &y_offset);
}
+void Surface::set_device_scale(double x_scale, double y_scale)
+{
+ cairo_surface_set_device_scale(cobj(), x_scale, y_scale);
+ check_object_status_and_throw_exception(*this);
+}
+
+void Surface::get_device_scale(double& x_scale, double& y_scale) const
+{
+ cairo_surface_get_device_scale(const_cast<cobject*>(cobj()), &x_scale, &y_scale);
+}
+
void Surface::set_fallback_resolution(double x_pixels_per_inch, double y_pixels_per_inch)
{
cairo_surface_set_fallback_resolution(cobj(), x_pixels_per_inch, y_pixels_per_inch);
diff --git a/cairomm/surface.h b/cairomm/surface.h
index 663bd57..5d5d5dc 100644
--- a/cairomm/surface.h
+++ b/cairomm/surface.h
@@ -431,6 +431,25 @@ public:
*/
void get_device_offset(double& x_offset, double& y_offset) const;
+ /** Sets a scale that is multiplied to the device coordinates determined by
+ * the CTM when drawing to surface. One common use for this is to render to
+ * very high resolution display devices at a scale factor, so that code that
+ * assumes 1 pixel will be a certain size will still work. Setting a
+ * transformation via cairo_translate() isn't sufficient to do this, since
+ * functions like Cairo::Context::device_to_user() will expose the hidden scale.
+ *
+ * Note that the scale affects drawing to the surface as well as using the
+ * surface in a source pattern.
+ *
+ * @param x_scale a scale factor in the X direction
+ * @param y_scale a scale factor in the Y direction
+ */
+ void set_device_scale(double x_scale, double y_scale);
+
+ /** Returns a previous device scale set by set_device_scale().
+ */
+ void get_device_scale(double& x_scale, double& y_scale) const;
+
/**
* Set the horizontal and vertical resolution for image fallbacks.
*
diff --git a/tests/test-surface.cc b/tests/test-surface.cc
index 50069c8..fa78048 100644
--- a/tests/test-surface.cc
+++ b/tests/test-surface.cc
@@ -89,6 +89,17 @@ BOOST_AUTO_TEST_CASE(test_content)
BOOST_CHECK_EQUAL(similar->get_content(), CONTENT_ALPHA);
}
+BOOST_AUTO_TEST_CASE(test_device_scale)
+{
+ auto surface = ImageSurface::create(Surface::Format::ARGB32, 1, 1);
+ const double new_x = 3, new_y = 5;
+ double x, y;
+ surface->set_device_scale(new_x, new_y);
+ surface->get_device_scale(x, y);
+ BOOST_CHECK_EQUAL(x, new_x);
+ BOOST_CHECK_EQUAL(y, new_y);
+}
+
BOOST_AUTO_TEST_CASE(test_fallback_resolution)
{
auto surface = ImageSurface::create(Surface::Format::ARGB32, 1, 1);