Skip to content

Commit

Permalink
fix Path.prototype.split for relative subpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
ppvg committed Nov 30, 2013
1 parent 517070f commit af0aa90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Path.prototype.copy = function () {
return new Path(newContent)
}

Path.prototype.split = function() {
Path.prototype.split = function () {
var subpaths = [[]]
var start
function currentPath() { return subpaths[subpaths.length - 1] }
Expand All @@ -93,11 +93,20 @@ Path.prototype.split = function() {
start = command
} else {
if (command.type === 'M') {
start = command
if (command.relative) {
start = { type: 'M', relative: false,
x: command.x + start.x,
y: command.y + start.y }
} else {
start = clone(command)
}
push(start)
} else if (currentPath().length === 0) {
push({ type: 'M', relative: false, x: start.x, y: start.y })
push(clone(command))
} else {
push(clone(command))
}
push(clone(command))
}
})
if (currentPath().length === 0) subpaths.pop()
Expand Down
24 changes: 24 additions & 0 deletions test/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ test('explicit subpath', function (t) {
t.end()
})

test('relative subpath', function (t) {
var path = new Path([
{ type: 'M', relative:false, x:50, y:50 },
{ type: 'L', relative:false, x:100, y:100 },
{ type: 'Z' },
{ type: 'M', relative:true, x:50, y:50 },
{ type: 'L', relative:false, x:200, y:200 }
])
var subpaths = path.split()
t.same(subpaths.length, 2)
t.same(subpaths[0].content,
[
{ type: 'M', relative:false, x:50, y:50 },
{ type: 'L', relative:false, x:100, y:100 },
{ type: 'Z' }
])
t.same(subpaths[1].content,
[
{ type: 'M', relative:false, x:100, y:100 },
{ type: 'L', relative:false, x:200, y:200 }
])
t.end()
})

test('subpath without closepath', function (t) {
var path = new Path([
{ type: 'M', relative:false, x:0, y:0 },
Expand Down

0 comments on commit af0aa90

Please sign in to comment.