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

Add proper fill color handling for SVG shapes and masks

This commit is contained in:
shipinev 2016-12-08 16:21:57 +07:00
parent e480eb4dc5
commit 4979390a35
2 changed files with 22 additions and 9 deletions

View File

@ -111,6 +111,7 @@
57FCD2771D76EA4600CC0FB6 /* Macaw.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 57FCD26C1D76EA4600CC0FB6 /* Macaw.framework */; };
57FCD27C1D76EA4600CC0FB6 /* MacawTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57FCD27B1D76EA4600CC0FB6 /* MacawTests.swift */; };
662808C41DE6BC9900A61B95 /* CGFloat+Double.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662808C31DE6BC9900A61B95 /* CGFloat+Double.swift */; };
6637F0B41DF95D9D009E28F9 /* SVGConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6637F0B31DF95D9D009E28F9 /* SVGConstants.swift */; };
66EA4AE41DE5AE030011818C /* SVGView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66EA4AE31DE5AE030011818C /* SVGView.swift */; };
/* End PBXBuildFile section */
@ -244,6 +245,7 @@
57FCD27B1D76EA4600CC0FB6 /* MacawTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacawTests.swift; sourceTree = "<group>"; };
57FCD27D1D76EA4600CC0FB6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
662808C31DE6BC9900A61B95 /* CGFloat+Double.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CGFloat+Double.swift"; sourceTree = "<group>"; };
6637F0B31DF95D9D009E28F9 /* SVGConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVGConstants.swift; sourceTree = "<group>"; };
66EA4AE31DE5AE030011818C /* SVGView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVGView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -467,6 +469,7 @@
38A997E31D91888700AC4545 /* svg */ = {
isa = PBXGroup;
children = (
6637F0B31DF95D9D009E28F9 /* SVGConstants.swift */,
38A997E41D91888700AC4545 /* SVGParser.swift */,
38A997E51D91888700AC4545 /* SVGParserRegexHelper.swift */,
66EA4AE31DE5AE030011818C /* SVGView.swift */,
@ -765,6 +768,7 @@
38A998061D91888700AC4545 /* GroupDisposable.swift in Sources */,
57653CA51DBA674200F6B64D /* ContentsAnimation.swift in Sources */,
38A998391D91888700AC4545 /* RenderContext.swift in Sources */,
6637F0B41DF95D9D009E28F9 /* SVGConstants.swift in Sources */,
38A997FA1D91888700AC4545 /* PathFunctions.swift in Sources */,
38A998261D91888700AC4545 /* Path.swift in Sources */,
38A997F61D91888700AC4545 /* Interpolable.swift in Sources */,

View File

@ -59,7 +59,7 @@ open class SVGParser {
fileprivate var nodes = [Node]()
fileprivate var defNodes = [String: Node]()
fileprivate var defFills = [String: Fill]()
fileprivate var defMasks = [String: Locus]()
fileprivate var defMasks = [String: Shape]()
fileprivate enum PathCommandType {
case moveTo
@ -347,6 +347,9 @@ open class SVGParser {
if let fillOpacity = styleParts["fill-opacity"] {
opacity = Double(fillOpacity.replacingOccurrences(of: " ", with: "")) ?? 1
}
if let defaultColor = SVGConstants.colorList[fillColor] {
return Color(val: defaultColor)
}
if fillColor.hasPrefix("url") {
let index = fillColor.characters.index(fillColor.startIndex, offsetBy: 4)
let id = fillColor.substring(from: index)
@ -651,7 +654,7 @@ open class SVGParser {
let attributes = getStyleAttributes([:], element: element)
return Text(text: text, font: getFont(attributes, fontName: fontName, fontSize: fontSize),
fill: getFillColor(attributes) ?? fill ?? Color.black, baseline: .alphabetic,
fill: fill ?? getFillColor(attributes) ?? Color.black, baseline: .alphabetic,
place: pos, opacity: getOpacity(attributes) ?? opacity)
}
@ -701,14 +704,15 @@ open class SVGParser {
if let color = fill {
shape.fill = color
}
if let line = stroke {
shape.stroke = line
}
if let line = stroke {
shape.stroke = line
}
if let maskIdenitifierMatcher = SVGParserRegexHelper.getMaskIdenitifierMatcher() {
let maskString = element.attributes["mask"] ?? ""
let fullRange = NSMakeRange(0, maskString.characters.count)
if let match = maskIdenitifierMatcher.firstMatch(in: maskString, options: .reportCompletion, range: fullRange), let maskReferenceNode = self.defMasks[(maskString as NSString).substring(with: match.rangeAt(1))] {
shape.clip = maskReferenceNode
shape.clip = maskReferenceNode.form
shape.fill = .none
}
}
return shape
@ -722,7 +726,7 @@ open class SVGParser {
return node
}
fileprivate func parseMask(_ mask: XMLIndexer) -> Locus? {
fileprivate func parseMask(_ mask: XMLIndexer) -> Shape? {
guard let element = mask.element else {
return .none
}
@ -737,10 +741,15 @@ open class SVGParser {
guard let shape = node as? Shape else {
return .none
}
let maskShape: Shape
if let circle = shape.form as? Circle {
return circle.arc(shift: 0, extent: degreesToRadians(360))
maskShape = Shape(form: circle.arc(shift: 0, extent: degreesToRadians(360)))
} else {
maskShape = Shape(form: shape.form)
}
return shape.form
let maskStyleAttributes = getStyleAttributes([:], element: element)
maskShape.fill = getFillColor(maskStyleAttributes)
return maskShape
}
fileprivate func parseLinearGradient(_ gradient: XMLIndexer) -> Fill? {