Skip to content

Commit

Permalink
Fix corner case with dash-array 0-length collapsing (mapbox#9385)
Browse files Browse the repository at this point in the history
A dash array with values [0,0] results in a full collapse of values since it does not represent
any dashes, fix the behavior with early return and add some more unit test coverage for other
corner cases
  • Loading branch information
karimnaaji authored and mike-unearth committed Mar 18, 2020
1 parent 63ea338 commit 9d3b0a3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/render/line_atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ class LineAtlas {
let length = 0;
for (let i = 0; i < dasharray.length; i++) { length += dasharray[i]; }

const stretch = this.width / length;
const ranges = this.getDashRanges(dasharray, this.width, stretch);

if (round) {
this.addRoundDash(ranges, stretch, n);
} else {
this.addRegularDash(ranges);
if (length !== 0) {
const stretch = this.width / length;
const ranges = this.getDashRanges(dasharray, this.width, stretch);

if (round) {
this.addRoundDash(ranges, stretch, n);
} else {
this.addRegularDash(ranges);
}
}

const dashEntry = {
Expand Down
48 changes: 48 additions & 0 deletions test/unit/render/line_atlas.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {test} from '../../util/test';
import LineAtlas from '../../../src/render/line_atlas';

test('LineAtlas', (t) => {
const lineAtlas = new LineAtlas(64, 64);
t.test('round [0, 0]', (t) => {
const entry = lineAtlas.addDash([0, 0], true);
t.equal(entry.width, 0);
t.end();
});
t.test('round [1, 0]', (t) => {
const entry = lineAtlas.addDash([1, 0], true);
t.equal(entry.width, 1);
t.end();
});
t.test('round [0, 1]', (t) => {
const entry = lineAtlas.addDash([0, 1], true);
t.equal(entry.width, 1);
t.end();
});
t.test('odd round [1, 2, 1]', (t) => {
const entry = lineAtlas.addDash([1, 2, 1], true);
t.equal(entry.width, 4);
t.end();
});

t.test('regular [0, 0]', (t) => {
const entry = lineAtlas.addDash([0, 0], false);
t.equal(entry.width, 0);
t.end();
});
t.test('regular [1, 0]', (t) => {
const entry = lineAtlas.addDash([1, 0], false);
t.equal(entry.width, 1);
t.end();
});
t.test('regular [0, 1]', (t) => {
const entry = lineAtlas.addDash([0, 1], false);
t.equal(entry.width, 1);
t.end();
});
t.test('odd regular [1, 2, 1]', (t) => {
const entry = lineAtlas.addDash([1, 2, 1], false);
t.equal(entry.width, 4);
t.end();
});
t.end();
});

0 comments on commit 9d3b0a3

Please sign in to comment.