mirror of
https://github.com/exyte/Macaw.git
synced 2024-11-10 13:16:41 +03:00
Merge pull request #327 from f3dm76/task/strokeOffset
Fix #186: Support stroke-dashoffset in SVG parser, renderer, and seri…
This commit is contained in:
commit
e4d3a0f7b4
@ -7,12 +7,14 @@ open class Stroke {
|
||||
open let cap: LineCap
|
||||
open let join: LineJoin
|
||||
open let dashes: [Double]
|
||||
open let offset: Double
|
||||
|
||||
public init(fill: Fill = Color.black, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) {
|
||||
public init(fill: Fill = Color.black, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = [], offset: Double = 0.0) {
|
||||
self.fill = fill
|
||||
self.width = width
|
||||
self.cap = cap
|
||||
self.join = join
|
||||
self.dashes = dashes
|
||||
self.offset = offset
|
||||
}
|
||||
}
|
||||
|
@ -167,14 +167,9 @@ class ShapeRenderer: NodeRenderer {
|
||||
ctx!.setLineWidth(CGFloat(stroke.width))
|
||||
ctx!.setLineJoin(RenderUtils.mapLineJoin(stroke.join))
|
||||
ctx!.setLineCap(RenderUtils.mapLineCap(stroke.cap))
|
||||
let dashes = stroke.dashes
|
||||
if !dashes.isEmpty {
|
||||
var floatDashes = [CGFloat]()
|
||||
dashes.forEach { dash in
|
||||
floatDashes.append(CGFloat(dash))
|
||||
}
|
||||
|
||||
ctx?.setLineDash(phase: 0.0, lengths: floatDashes)
|
||||
if !stroke.dashes.isEmpty {
|
||||
ctx?.setLineDash(phase: CGFloat(stroke.offset),
|
||||
lengths: stroke.dashes.map{ CGFloat($0) })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ open class SVGParser {
|
||||
return SVGParser(text).parse()
|
||||
}
|
||||
|
||||
let availableStyleAttributes = ["stroke", "stroke-width", "stroke-opacity", "stroke-dasharray", "stroke-linecap", "stroke-linejoin",
|
||||
let availableStyleAttributes = ["stroke", "stroke-width", "stroke-opacity", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin",
|
||||
"fill", "text-anchor", "clip-path", "fill-opacity",
|
||||
"stop-color", "stop-opacity",
|
||||
"font-family", "font-size",
|
||||
@ -620,7 +620,8 @@ open class SVGParser {
|
||||
width: getStrokeWidth(styleParts),
|
||||
cap: getStrokeCap(styleParts),
|
||||
join: getStrokeJoin(styleParts),
|
||||
dashes: getStrokeDashes(styleParts))
|
||||
dashes: getStrokeDashes(styleParts),
|
||||
offset: getStrokeOffset(styleParts))
|
||||
}
|
||||
|
||||
return .none
|
||||
@ -684,6 +685,13 @@ open class SVGParser {
|
||||
return dashes
|
||||
}
|
||||
|
||||
fileprivate func getStrokeOffset(_ styleParts: [String: String]) -> Double {
|
||||
if let strokeOffset = styleParts["stroke-dashoffset"], let offset = Double(strokeOffset) { // TODO use doubleFromString once it's merged
|
||||
return offset
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
fileprivate func getTag(_ element: SWXMLHash.XMLElement) -> [String] {
|
||||
let id = element.allAttributes["id"]?.text
|
||||
return id != nil ? [id!] : []
|
||||
|
@ -247,6 +247,15 @@ open class SVGSerializer {
|
||||
result += " stroke-linejoin=\"\(strokeJoin)\""
|
||||
}
|
||||
}
|
||||
if let strokeDashes = stroke?.dashes, strokeDashes.count > 0 {
|
||||
let dashes = strokeDashes.map{ String($0) }.joined(separator: ",")
|
||||
result += " stroke-dasharray=\"\(dashes)\""
|
||||
}
|
||||
if let strokeOffset = stroke?.offset {
|
||||
if strokeOffset != 0 {
|
||||
result += " stroke-dashoffset=\"\(strokeOffset)\""
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user