1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-09-11 13:15:35 +03:00

Add polyline and polycurve

This commit is contained in:
Alexandra Novikova 2016-09-13 20:00:39 +07:00
parent 4ec5c6299a
commit e328232fa3
2 changed files with 80 additions and 48 deletions

View File

@ -339,17 +339,33 @@ class ShapeRenderer: NodeRenderer {
// TODO: think about this
for part in path.segments {
let data = part.data
var data = part.data
switch part.type {
case .M:
M(data[0], y: data[1])
data.removeRange(Range(start: 0, end: 2))
while data.count >= 2 {
L(data[0], y: data[1])
data.removeRange(Range(start: 0, end: 2))
}
case .m:
m(data[0], y: data[1])
data.removeRange(Range(start: 0, end: 2))
while data.count >= 2 {
l(data[0], y: data[1])
data.removeRange(Range(start: 0, end: 2))
}
case .L:
L(data[0], y: data[1])
while data.count >= 2 {
L(data[0], y: data[1])
data.removeRange(Range(start: 0, end: 2))
}
case .l:
l(data[0], y: data[1])
case .H:
while data.count >= 2 {
l(data[0], y: data[1])
data.removeRange(Range(start: 0, end: 2))
}
case .H:
H(data[0])
case .h:
h(data[0])
@ -358,13 +374,25 @@ class ShapeRenderer: NodeRenderer {
case .v:
v(data[0])
case .C:
C(data[0], y1: data[1], x2: data[2], y2: data[3], x: data[4], y: data[5])
while data.count >= 6 {
C(data[0], y1: data[1], x2: data[2], y2: data[3], x: data[4], y: data[5])
data.removeRange(Range(start: 0, end: 6))
}
case .c:
c(data[0], y1: data[1], x2: data[2], y2: data[3], x: data[4], y: data[5])
while data.count >= 6 {
c(data[0], y1: data[1], x2: data[2], y2: data[3], x: data[4], y: data[5])
data.removeRange(Range(start: 0, end: 6))
}
case .S:
S(data[0], y2: data[1], x: data[2], y: data[3])
while data.count >= 4 {
S(data[0], y2: data[1], x: data[2], y: data[3])
data.removeRange(Range(start: 0, end: 4))
}
case .s:
s(data[0], y2: data[1], x: data[2], y: data[3])
while data.count >= 4 {
s(data[0], y2: data[1], x: data[2], y: data[3])
data.removeRange(Range(start: 0, end: 4))
}
case .A:
let flags = numToBools(data[3])
A(data[0], ry: data[1], angle: data[2], largeArc: flags[0], sweep: flags[1], x: data[4], y: data[5])

View File

@ -896,26 +896,32 @@ public class SVGParser {
switch command.type {
case .MoveTo:
if separatedValues.count < 2 {
return .None
}
var data = [Double]()
separatedValues.forEach { value in
if let double = Double(value) {
data.append(double)
}
}
if data.count < 2 {
return .None
}
guard let x = Double(separatedValues[0]), y = Double(separatedValues[1]) else {
return .None
}
return PathSegment(type: command.absolute ? .M : .m, data: [x, y])
return PathSegment(type: command.absolute ? .M : .m, data: data)
case .LineTo:
if separatedValues.count < 2 {
return .None
}
var data = [Double]()
separatedValues.forEach { value in
if let double = Double(value) {
data.append(double)
}
}
if data.count < 2 {
return .None
}
guard let x = Double(separatedValues[0]), y = Double(separatedValues[1]) else {
return .None
}
return PathSegment(type: command.absolute ? .L : .l, data: [x, y])
return PathSegment(type: command.absolute ? .L : .l, data: data)
case .LineH:
if separatedValues.count < 1 {
@ -940,34 +946,32 @@ public class SVGParser {
return PathSegment(type: command.absolute ? .V : .v, data: [y])
case .CurveTo:
if separatedValues.count < 6 {
return .None
}
var data = [Double]()
separatedValues.forEach { value in
if let double = Double(value) {
data.append(double)
}
}
if data.count < 6 {
return .None
}
guard let x1 = Double(separatedValues[0]),
y1 = Double(separatedValues[1]),
x2 = Double(separatedValues[2]),
y2 = Double(separatedValues[3]),
x = Double(separatedValues[4]),
y = Double(separatedValues[5]) else {
return .None
}
return PathSegment(type: command.absolute ? .C : .c, data: [x1, y1, x2, y2, x, y])
return PathSegment(type: command.absolute ? .C : .c, data: data)
case .SmoothCurveTo:
if separatedValues.count < 4 {
return .None
}
var data = [Double]()
separatedValues.forEach { value in
if let double = Double(value) {
data.append(double)
}
}
if data.count < 4 {
return .None
}
guard let x2 = Double(separatedValues[0]),
y2 = Double(separatedValues[1]),
x = Double(separatedValues[2]),
y = Double(separatedValues[3]) else {
return .None
}
return PathSegment(type: command.absolute ? .S : .s, data: [x2, y2, x, y])
return PathSegment(type: command.absolute ? .S : .s, data: data)
case .ClosePath:
return PathSegment(type: .Z)