1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-11-10 13:16:41 +03:00

Merge pull request #322 from f3dm76/task/units

Fix #185: Implement px m Stroke-dasharray
This commit is contained in:
Yuri Strot 2018-04-23 20:04:04 +07:00 committed by GitHub
commit 8ce50eaba2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View File

@ -681,7 +681,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)
}
}
@ -1208,10 +1208,31 @@ 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
}
guard let matcher = SVGParserRegexHelper.getUnitsIdenitifierMatcher() else { return .none }
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)
}
}
return .none
}
fileprivate func getDoubleValueFromPercentage(_ element: SWXMLHash.XMLElement, attribute: String) -> Double? {

View File

@ -6,11 +6,13 @@ class SVGParserRegexHelper {
fileprivate static let transformPattern = "\\-?\\d+\\.?\\d*e?\\-?\\d*"
fileprivate static let textElementPattern = "<text.*?>((?s:.*))<\\/text>"
fileprivate static let maskIdenitifierPattern = "url\\(#((?s:.*))\\)"
fileprivate static let unitsIdenitifierPattern = "([a-zA-Z]+)$"
fileprivate static var transformMatcher: NSRegularExpression?
fileprivate static var transformAttributeMatcher: NSRegularExpression?
fileprivate static var textElementMatcher: NSRegularExpression?
fileprivate static var maskIdenitifierMatcher: NSRegularExpression?
fileprivate static var unitsMatcher: NSRegularExpression?
class func getTransformAttributeMatcher() -> NSRegularExpression? {
if self.transformAttributeMatcher == nil {
@ -55,5 +57,16 @@ class SVGParserRegexHelper {
}
return self.maskIdenitifierMatcher
}
class func getUnitsIdenitifierMatcher() -> NSRegularExpression? {
if unitsMatcher == nil {
do {
unitsMatcher = try NSRegularExpression(pattern: unitsIdenitifierPattern, options: .caseInsensitive)
} catch {
}
}
return unitsMatcher
}
}