mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-21 01:47:44 +03:00
Merge pull request #322 from f3dm76/task/units
Fix #185: Implement px m Stroke-dasharray
This commit is contained in:
commit
8ce50eaba2
@ -681,7 +681,7 @@ open class SVGParser {
|
|||||||
characterSet.insert(",")
|
characterSet.insert(",")
|
||||||
let separatedValues = strokeDashes.components(separatedBy: characterSet)
|
let separatedValues = strokeDashes.components(separatedBy: characterSet)
|
||||||
separatedValues.forEach { value in
|
separatedValues.forEach { value in
|
||||||
if let doubleValue = Double(value) {
|
if let doubleValue = doubleFromString(value) {
|
||||||
dashes.append(doubleValue)
|
dashes.append(doubleValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1208,10 +1208,31 @@ open class SVGParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func getDoubleValue(_ element: SWXMLHash.XMLElement, attribute: String) -> Double? {
|
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 .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? {
|
fileprivate func getDoubleValueFromPercentage(_ element: SWXMLHash.XMLElement, attribute: String) -> Double? {
|
||||||
|
@ -6,11 +6,13 @@ class SVGParserRegexHelper {
|
|||||||
fileprivate static let transformPattern = "\\-?\\d+\\.?\\d*e?\\-?\\d*"
|
fileprivate static let transformPattern = "\\-?\\d+\\.?\\d*e?\\-?\\d*"
|
||||||
fileprivate static let textElementPattern = "<text.*?>((?s:.*))<\\/text>"
|
fileprivate static let textElementPattern = "<text.*?>((?s:.*))<\\/text>"
|
||||||
fileprivate static let maskIdenitifierPattern = "url\\(#((?s:.*))\\)"
|
fileprivate static let maskIdenitifierPattern = "url\\(#((?s:.*))\\)"
|
||||||
|
fileprivate static let unitsIdenitifierPattern = "([a-zA-Z]+)$"
|
||||||
|
|
||||||
fileprivate static var transformMatcher: NSRegularExpression?
|
fileprivate static var transformMatcher: NSRegularExpression?
|
||||||
fileprivate static var transformAttributeMatcher: NSRegularExpression?
|
fileprivate static var transformAttributeMatcher: NSRegularExpression?
|
||||||
fileprivate static var textElementMatcher: NSRegularExpression?
|
fileprivate static var textElementMatcher: NSRegularExpression?
|
||||||
fileprivate static var maskIdenitifierMatcher: NSRegularExpression?
|
fileprivate static var maskIdenitifierMatcher: NSRegularExpression?
|
||||||
|
fileprivate static var unitsMatcher: NSRegularExpression?
|
||||||
|
|
||||||
class func getTransformAttributeMatcher() -> NSRegularExpression? {
|
class func getTransformAttributeMatcher() -> NSRegularExpression? {
|
||||||
if self.transformAttributeMatcher == nil {
|
if self.transformAttributeMatcher == nil {
|
||||||
@ -55,5 +57,16 @@ class SVGParserRegexHelper {
|
|||||||
}
|
}
|
||||||
return self.maskIdenitifierMatcher
|
return self.maskIdenitifierMatcher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class func getUnitsIdenitifierMatcher() -> NSRegularExpression? {
|
||||||
|
if unitsMatcher == nil {
|
||||||
|
do {
|
||||||
|
unitsMatcher = try NSRegularExpression(pattern: unitsIdenitifierPattern, options: .caseInsensitive)
|
||||||
|
} catch {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return unitsMatcher
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user