diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index fb4064b4..7e586c2d 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -667,7 +667,7 @@ open class SVGParser { characterSet.insert(",") let separatedValues = strokeDashes.components(separatedBy: characterSet) separatedValues.forEach { value in - if let doubleValue = Double(value) { + if let doubleValue = doubleFromString(value) { dashes.append(doubleValue) } } @@ -1189,10 +1189,35 @@ open class SVGParser { } fileprivate func getDoubleValue(_ element: SWXMLHash.XMLElement, attribute: String) -> Double? { - guard let attributeValue = element.allAttributes[attribute]?.text, let doubleValue = Double(attributeValue) else { + guard let attributeValue = element.allAttributes[attribute]?.text else { return .none } - return doubleValue + return doubleFromString(attributeValue) + } + + fileprivate func doubleFromString(_ string: String) -> Double? { + if let doubleValue = Double(string) { + return doubleValue + } + do { + let matcher = try NSRegularExpression(pattern: "([a-zA-Z]+)", options: .caseInsensitive) + let fullRange = NSRange(location: 0, length: string.count) + if let match = matcher.firstMatch(in: string, options: .reportCompletion, range: fullRange) { + + let unitString = (string as NSString).substring(with: match.range(at: 1)) + let numberString = String(string.dropLast(unitString.count)) + switch unitString { + case "px" : + return Double(numberString) + default: + print("SVG parsing error. Unit \(unitString) not supported") + return Double(numberString) + } + } + } catch { + print(error.localizedDescription) + } + return .none } fileprivate func getDoubleValueFromPercentage(_ element: SWXMLHash.XMLElement, attribute: String) -> Double? {