Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GenerateReferencesCLI/cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ struct cli: ParsableCommand {
static let customRefs: [String] = [
"viewport-01",
"viewport-02",
"graph-01",
]

mutating func run() throws {
Expand Down
22 changes: 15 additions & 7 deletions Source/Parser/SVG/SVGParserBasics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension SVGHelper {
}
}
if let color = parseColor(colorString, style) {
return color.opacity(parseOpacity(style, "fill-opacity", alternativeKeys: ["opacity"]))
return color.opacity(color.opacity * parseOpacity(style, "fill-opacity", alternativeKeys: ["opacity"]))
}

return .none
Expand All @@ -112,7 +112,7 @@ extension SVGHelper {
} else if let defaultColor = SVGColor.by(name: normalized) {
return defaultColor
} else if normalized.hasPrefix("rgb") {
return parseRGBNotation(colorString: normalized)
return parseRGBANotation(colorString: normalized)
} else {
return createColorFromHex(normalized)
}
Expand All @@ -127,11 +127,12 @@ extension SVGHelper {
let x = Array(cleanedHexString)
cleanedHexString = "\(x[0])\(x[0])\(x[1])\(x[1])\(x[2])\(x[2])"
}
return SVGColor(hex: cleanedHexString)
return SVGColor(hex: cleanedHexString).opacity(1.0)
}

static func parseRGBNotation(colorString: String) -> SVGColor {
let from = colorString.index(colorString.startIndex, offsetBy: 4)
static func parseRGBANotation(colorString: String) -> SVGColor {
let fromIndex = colorString.hasPrefix("rgba") ? 5 : 4
let from = colorString.index(colorString.startIndex, offsetBy: fromIndex)
let inPercentage = colorString.contains("%")
let sp = String(colorString.suffix(from: from))
.replacingOccurrences(of: "%", with: "")
Expand All @@ -141,19 +142,26 @@ extension SVGHelper {
var red = 0.0
var green = 0.0
var blue = 0.0
if x.count == 3 {
var alpha = 1.0 // Default to fully opaque, always from 0 to 1 in CSS
if x.count >= 3 {
if let r = Double(x[0]), let g = Double(x[1]), let b = Double(x[2]) {
blue = b
green = g
red = r
}
}
if x.count == 4, let a = Double(x[3]) {
alpha = a
}
if inPercentage {
red *= 2.55
green *= 2.55
blue *= 2.55
if x.count == 4 {
alpha *= 0.01
}
}
return SVGColor(r: Int(round(red)), g: Int(round(green)), b: Int(round(blue)))
return SVGColor(r: Int(round(red)), g: Int(round(green)), b: Int(round(blue))).opacity(min(max(alpha, 0.0), 1.0))
}

static private func parseIdFromUrl(_ urlString: String) -> String? {
Expand Down
8 changes: 8 additions & 0 deletions Tests/SVGViewTests/SVGCustomTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ class SVGCustomTests: BaseTestCase {
return "Custom"
}

func testGraph01() {
compareToReference("graph-01")
}

func testViewport01() {
compareToReference("viewport-01")
}

func testViewport02() {
compareToReference("viewport-02")
}

}
78 changes: 78 additions & 0 deletions Tests/SVGViewTests/w3c/Custom/refs/graph-01.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
SVGViewport {
width: "2000",
height: "2000",
scaling: "none",
contents: [
SVGRect { width: 2000, height: 2000, fill: "#F4F4F4" },
SVGRect { width: 2000, height: 2000, rx: 10, ry: 10, fill: "white" },
SVGRect { x: 900, y: 900, width: 200, height: 200, rx: 50, ry: 50, fill: "#E82127" },
SVGText {
text: "Tesla RoboTaxi",
font: { name: "Arial, sans-serif", size: 20, weight: "700" },
fill: "white",
transform: [1, 0, 0, 1, 917.34375, 1008]
},
SVGRect { x: 1425, y: 925, width: 150, height: 150, rx: 50, ry: 50, fill: "black" },
SVGText {
text: "Technology",
font: { name: "Arial, sans-serif", weight: "400" },
fill: "white",
transform: [1, 0, 0, 1, 1455.1875, 1006.5]
},
SVGRect { x: 1000, y: 1000, width: 500, height: 2, fill: "#888888" },
SVGRect {
x: 1709.796875,
y: 800,
width: 104,
height: 104,
rx: 50,
ry: 50,
fill: "white",
stroke: { fill: "#E82127", width: 2 }
},
SVGText {
text: "Full Self-Driving",
font: { name: "Arial, sans-serif", size: 12, weight: "400" },
fill: "black",
transform: [1, 0, 0, 1, 1714.921875, 850]
},
SVGText {
text: "(FSD)",
font: { name: "Arial, sans-serif", size: 12, weight: "400" },
fill: "black",
transform: [1, 0, 0, 1, 1714.921875, 864]
},
SVGRect {
x: 1500,
y: 999.9999971679687,
width: 300,
height: 2,
fill: "#888888",
transform: [0.866025, -0.5, 0.5, 0.866025, 0, 0]
},
SVGRect {
x: 1735.3125,
y: 857.28125,
width: 104,
height: 104,
rx: 50,
ry: 50,
fill: "white",
stroke: { fill: "#E82127", width: 2 }
},
SVGText {
text: "Computer Vision",
font: { name: "Arial, sans-serif", size: 12, weight: "400" },
fill: "black",
transform: [1, 0, 0, 1, 1737.671875, 914.28125]
},
SVGRect {
x: 1500.0000570390625,
y: 1000.0000290703125,
width: 300,
height: 2,
fill: "#888888",
transform: [0.951057, -0.309017, 0.309017, 0.951057, 0, 0]
}
]
}
16 changes: 16 additions & 0 deletions Tests/SVGViewTests/w3c/Custom/svg/graph-01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.