Skip to content

Commit

Permalink
Make setLineDash able to handle full zeroed dashes (Automattic#1060)
Browse files Browse the repository at this point in the history
* added fix and test

Fixes Automattic#1055
  • Loading branch information
asturur authored and zbjornson committed Dec 21, 2017
1 parent 7c9ec58 commit 727cb2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2193,20 +2193,26 @@ NAN_METHOD(Context2d::SetLineDash) {
if (!info[0]->IsArray()) return;
Local<Array> dash = Local<Array>::Cast(info[0]);
uint32_t dashes = dash->Length() & 1 ? dash->Length() * 2 : dash->Length();

uint32_t zero_dashes = 0;
std::vector<double> a(dashes);
for (uint32_t i=0; i<dashes; i++) {
Local<Value> d = dash->Get(i % dash->Length());
if (!d->IsNumber()) return;
a[i] = d->NumberValue();
if (a[i] == 0) zero_dashes++;
if (a[i] < 0 || isnan(a[i]) || isinf(a[i])) return;
}

Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
cairo_t *ctx = context->context();
double offset;
cairo_get_dash(ctx, NULL, &offset);
cairo_set_dash(ctx, a.data(), dashes, offset);
if (zero_dashes == dashes) {
std::vector<double> b(0);
cairo_set_dash(ctx, b.data(), 0, offset);
} else {
cairo_set_dash(ctx, a.data(), dashes, offset);
}
}

/*
Expand Down
5 changes: 4 additions & 1 deletion test/public/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ tests['putImageData() png data 3'] = function (ctx, done) {

tests['setLineDash'] = function (ctx) {
ctx.setLineDash([10, 5, 25, 15])
ctx.lineWidth = 17
ctx.lineWidth = 14

var y = 5
var line = function (lineDash, color) {
Expand All @@ -2096,6 +2096,9 @@ tests['setLineDash'] = function (ctx) {
a.push(20)
return a
})(), 'orange')
line([0, 0], 'purple') // should be full
line([0, 0, 3, 0], 'orange') // should be full
line([0, 3, 0, 0], 'green') // should be empty
}

tests['lineDashOffset'] = function (ctx) {
Expand Down

0 comments on commit 727cb2c

Please sign in to comment.