Skip to content

Commit

Permalink
Add ctx2d.setTransform(mat) overload
Browse files Browse the repository at this point in the history
Based on currentTransform setter
  • Loading branch information
Tirondzo authored and zbjornson committed Apr 20, 2021
1 parent 2f84eee commit c166443
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> mat) {
cairo_matrix_init(&matrix,
Nan::To<double>(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0)
);
}


/*
* Get current transform.
*/
Expand Down Expand Up @@ -1802,14 +1818,7 @@ NAN_SETTER(Context2d::SetCurrentTransform) {
#endif

cairo_matrix_t matrix;
cairo_matrix_init(&matrix,
Nan::To<double>(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0)
);
parse_matrix_from_object(matrix, mat);

cairo_transform(context->context(), &matrix);
}
Expand Down Expand Up @@ -2279,8 +2288,24 @@ NAN_METHOD(Context2d::ResetTransform) {

NAN_METHOD(Context2d::SetTransform) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
cairo_identity_matrix(context->context());
Context2d::Transform(info);
if (info.Length() == 1) {
Local<Object> mat = Nan::To<Object>(info[0]).ToLocalChecked();

#if NODE_MAJOR_VERSION >= 8
Local<Context> 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);
}
}

/*
Expand Down
6 changes: 6 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit c166443

Please sign in to comment.