From c166443abe8551c705d45a2175d8b6303dda615b Mon Sep 17 00:00:00 2001 From: Tirondzo Date: Mon, 22 Mar 2021 02:44:47 +0300 Subject: [PATCH] Add ctx2d.setTransform(mat) overload Based on currentTransform setter --- src/CanvasRenderingContext2d.cc | 45 +++++++++++++++++++++++++-------- test/canvas.test.js | 6 +++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index d6960dd8b..98d0372b5 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -1775,6 +1775,22 @@ get_current_transform(Context2d *context) { return Nan::NewInstance(context->_DOMMatrix.Get(iso), argc, argv).ToLocalChecked(); } +/* + * Helper for get/set transform. + */ + +void parse_matrix_from_object(cairo_matrix_t &matrix, Local mat) { + cairo_matrix_init(&matrix, + Nan::To(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), + Nan::To(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), + Nan::To(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), + Nan::To(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), + Nan::To(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), + Nan::To(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0) + ); +} + + /* * Get current transform. */ @@ -1802,14 +1818,7 @@ NAN_SETTER(Context2d::SetCurrentTransform) { #endif cairo_matrix_t matrix; - cairo_matrix_init(&matrix, - Nan::To(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), - Nan::To(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), - Nan::To(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), - Nan::To(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), - Nan::To(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0), - Nan::To(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0) - ); + parse_matrix_from_object(matrix, mat); cairo_transform(context->context(), &matrix); } @@ -2279,8 +2288,24 @@ NAN_METHOD(Context2d::ResetTransform) { NAN_METHOD(Context2d::SetTransform) { Context2d *context = Nan::ObjectWrap::Unwrap(info.This()); - cairo_identity_matrix(context->context()); - Context2d::Transform(info); + if (info.Length() == 1) { + Local mat = Nan::To(info[0]).ToLocalChecked(); + + #if NODE_MAJOR_VERSION >= 8 + Local ctx = Nan::GetCurrentContext(); + if (!mat->InstanceOf(ctx, _DOMMatrix.Get(Isolate::GetCurrent())).ToChecked()) { + return Nan::ThrowTypeError("Expected DOMMatrix"); + } + #endif + + cairo_matrix_t matrix; + parse_matrix_from_object(matrix, mat); + + cairo_set_matrix(context->context(), &matrix); + } else { + cairo_identity_matrix(context->context()); + Context2d::Transform(info); + } } /* diff --git a/test/canvas.test.js b/test/canvas.test.js index c3be7d274..0aa3dc6e3 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -993,6 +993,12 @@ describe('Canvas', function () { assert.equal(mat3.d, 0.3); assert.deepEqual(ctx.currentTransform, ctx.getTransform()); + + ctx.setTransform(ctx.getTransform()); + assert.deepEqual(mat3, ctx.getTransform()); + + ctx.setTransform(mat3.a, mat3.b, mat3.c, mat3.d, mat3.e, mat3.f); + assert.deepEqual(mat3, ctx.getTransform()); }); it('Context2d#createImageData(ImageData)', function () {