From 9d3b0a30ad424ce64f0fd5de98c491b39449c9f7 Mon Sep 17 00:00:00 2001 From: Karim Naaji Date: Fri, 6 Mar 2020 11:36:55 -0800 Subject: [PATCH] Fix corner case with dash-array 0-length collapsing (#9385) 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 --- src/render/line_atlas.js | 16 +++++----- test/unit/render/line_atlas.test.js | 48 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 test/unit/render/line_atlas.test.js diff --git a/src/render/line_atlas.js b/src/render/line_atlas.js index ddf42269878..c755ea8990e 100644 --- a/src/render/line_atlas.js +++ b/src/render/line_atlas.js @@ -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 = { diff --git a/test/unit/render/line_atlas.test.js b/test/unit/render/line_atlas.test.js new file mode 100644 index 00000000000..b1c2bdef938 --- /dev/null +++ b/test/unit/render/line_atlas.test.js @@ -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(); +});