[gjs: 2/3] cairo: Add Surface.getDeviceScale and Surface.getDeviceOffset




commit 5be75c913da1c1a794719a0bd4cbaf70d4f7fb3b
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Aug 14 18:09:18 2021 -0700

    cairo: Add Surface.getDeviceScale and Surface.getDeviceOffset
    
    Simple wrappers for cairo_surface_get_device_scale() and
    cairo_surface_get_device_offset(), accompanied by tests.

 installed-tests/js/testCairo.js | 20 ++++++++++++++
 modules/cairo-surface.cpp       | 61 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 1 deletion(-)
---
diff --git a/installed-tests/js/testCairo.js b/installed-tests/js/testCairo.js
index 67a7dc43..5b55666f 100644
--- a/installed-tests/js/testCairo.js
+++ b/installed-tests/js/testCairo.js
@@ -244,6 +244,26 @@ describe('Cairo', function () {
             const pattern = new Cairo.SurfacePattern(surface);
             expect(() => new Cairo.Context(pattern)).toThrow();
         });
+
+        it('can access the device scale', function () {
+            let [x, y] = surface.getDeviceScale();
+            expect(x).toEqual(1);
+            expect(y).toEqual(1);
+            surface.setDeviceScale(1.2, 1.2);
+            [x, y] = surface.getDeviceScale();
+            expect(x).toEqual(1.2);
+            expect(y).toEqual(1.2);
+        });
+
+        it('can access the device offset', function () {
+            let [x, y] = surface.getDeviceOffset();
+            expect(x).toEqual(0);
+            expect(y).toEqual(0);
+            surface.setDeviceOffset(50, 50);
+            [x, y] = surface.getDeviceOffset();
+            expect(x).toEqual(50);
+            expect(y).toEqual(50);
+        });
     });
 
     describe('GI test suite', function () {
diff --git a/modules/cairo-surface.cpp b/modules/cairo-surface.cpp
index b0349ee2..d8b50483 100644
--- a/modules/cairo-surface.cpp
+++ b/modules/cairo-surface.cpp
@@ -8,6 +8,7 @@
 #include <girepository.h>
 #include <glib.h>
 
+#include <js/Array.h>
 #include <js/CallArgs.h>
 #include <js/Class.h>
 #include <js/PropertyDescriptor.h>  // for JSPROP_READONLY
@@ -15,6 +16,7 @@
 #include <js/RootingAPI.h>
 #include <js/TypeDecls.h>
 #include <js/Value.h>
+#include <js/ValueArray.h>
 #include <jsapi.h>  // for JS_GetPrivate, JS_GetClass, ...
 
 #include "gi/arg-inl.h"
@@ -105,6 +107,34 @@ static bool setDeviceOffset_func(JSContext* cx, unsigned argc, JS::Value* vp) {
     return true;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool getDeviceOffset_func(JSContext* cx, unsigned argc, JS::Value* vp) {
+    GJS_GET_THIS(cx, argc, vp, args, obj);
+
+    if (argc > 0) {
+        gjs_throw(cx, "Surface.getDeviceOffset() takes no arguments");
+        return false;
+    }
+
+    cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
+    if (!surface)
+        return false;
+
+    double x_offset, y_offset;
+    cairo_surface_get_device_offset(surface, &x_offset, &y_offset);
+    // cannot error
+
+    JS::RootedValueArray<2> elements(cx);
+    elements[0].setNumber(x_offset);
+    elements[1].setNumber(y_offset);
+    JS::RootedObject retval(cx, JS::NewArrayObject(cx, elements));
+    if (!retval)
+        return false;
+
+    args.rval().setObject(*retval);
+    return true;
+}
+
 GJS_JSAPI_RETURN_CONVENTION
 static bool setDeviceScale_func(JSContext* cx, unsigned argc, JS::Value* vp) {
     GJS_GET_THIS(cx, argc, vp, args, obj);
@@ -127,6 +157,34 @@ static bool setDeviceScale_func(JSContext* cx, unsigned argc, JS::Value* vp) {
     return true;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool getDeviceScale_func(JSContext* cx, unsigned argc, JS::Value* vp) {
+    GJS_GET_THIS(cx, argc, vp, args, obj);
+
+    if (argc > 0) {
+        gjs_throw(cx, "Surface.getDeviceScale() takes no arguments");
+        return false;
+    }
+
+    cairo_surface_t* surface = CairoSurface::for_js(cx, obj);
+    if (!surface)
+        return false;
+
+    double x_scale, y_scale;
+    cairo_surface_get_device_scale(surface, &x_scale, &y_scale);
+    // cannot error
+
+    JS::RootedValueArray<2> elements(cx);
+    elements[0].setNumber(x_scale);
+    elements[1].setNumber(y_scale);
+    JS::RootedObject retval(cx, JS::NewArrayObject(cx, elements));
+    if (!retval)
+        return false;
+
+    args.rval().setObject(*retval);
+    return true;
+}
+
 const JSFunctionSpec CairoSurface::proto_funcs[] = {
     // flush
     // getContent
@@ -135,8 +193,9 @@ const JSFunctionSpec CairoSurface::proto_funcs[] = {
     // markDirty
     // markDirtyRectangle
     JS_FN("setDeviceOffset", setDeviceOffset_func, 2, 0),
-    // getDeviceOffset
+    JS_FN("getDeviceOffset", getDeviceOffset_func, 0, 0),
     JS_FN("setDeviceScale", setDeviceScale_func, 2, 0),
+    JS_FN("getDeviceScale", getDeviceScale_func, 0, 0),
     // setFallbackResolution
     // getFallbackResolution
     // copyPage


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]