Skip to content
Open
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
6 changes: 3 additions & 3 deletions DOM/Sources/DOM.SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ package extension DOM {
final class SVG: GraphicsElement, ContainerElement {
package var x: Coordinate?
package var y: Coordinate?
package var width: Length
package var height: Length
package var width: Coordinate
package var height: Coordinate
package var viewBox: ViewBox?

package var childElements = [GraphicsElement]()

package var styles = [StyleSheet]()
package var defs = Defs()

package init(x: Coordinate? = nil, y: Coordinate? = nil, width: Length, height: Length) {
package init(x: Coordinate? = nil, y: Coordinate? = nil, width: Coordinate, height: Coordinate) {
self.x = x
self.y = y
self.width = width
Expand Down
2 changes: 1 addition & 1 deletion DOM/Sources/Parser.XML.SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ package extension XMLParser {
throw XMLParser.Error.unresolvableDimension(reason: makeUnresolvedReason(attribute: "height", raw: heightRaw, hasViewBox: viewBox != nil))
}

let svg = DOM.SVG(width: DOM.Length(w), height: DOM.Length(h))
let svg = DOM.SVG(width: w, height: h)
svg.x = try att.parseCoordinate("x")
svg.y = try att.parseCoordinate("y")
svg.childElements = try parseGraphicsElements(e.children)
Expand Down
12 changes: 10 additions & 2 deletions DOM/Tests/Parser.SVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ struct ParserSVGTests {
#expect(parsed != expected2)
}

@Test
func svgWithFractionalDimensions() throws {
let node = XML.Element(name: "svg", attributes: ["width": "5.8", "height": "5.8"])
let parser = DOMXMLParser()
let parsed = try parser.parseSVG(node)
#expect(Float(parsed.width) == 5.8)
#expect(Float(parsed.height) == 5.8)
}
@Test
func svgWithUnits() throws {
let node = XML.Element(name: "svg", attributes: ["width": "10cm", "height": "2in"])
let parser = DOMXMLParser()

let parsed = try parser.parseSVG(node)
// 10cm = 10 * 37.795 = 377.95 → truncated to 377
#expect(parsed.width == 377)
// 10cm = 10 * 37.795 = 377.95
#expect(parsed.width == 377.95)
// 2in = 2 * 96 = 192
#expect(parsed.height == 192)
}
Expand Down
4 changes: 2 additions & 2 deletions SwiftDraw/Sources/Formatter/XML.Formatter.SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ extension XML.Formatter {
element.attributes["viewBox"] = makeViewBox(svg.viewBox)

if svg.viewBox != .init(x: 0, y: 0, width: DOM.Coordinate(svg.width), height: DOM.Coordinate(svg.height)) {
element.attributes["width"] = formatter.formatLength(svg.width)
element.attributes["height"] = formatter.formatLength(svg.height)
element.attributes["width"] = formatter.format(svg.width)
element.attributes["height"] = formatter.format(svg.height)
}

try element.children.append(contentsOf: makeStyles(svg.styles))
Expand Down
4 changes: 2 additions & 2 deletions SwiftDraw/Sources/LayerTree/LayerTree.Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ extension LayerTree {
x: DOM.Coordinate?,
y: DOM.Coordinate?,
viewBox: DOM.SVG.ViewBox?,
width: DOM.Length,
height: DOM.Length
width: DOM.Coordinate,
height: DOM.Coordinate
) -> [LayerTree.Transform] {
let position = LayerTree.Transform.translate(tx: x ?? 0, ty: y ?? 0)
let viewBox = viewBox ?? DOM.SVG.ViewBox(x: 0, y: 0, width: .init(width), height: .init(height))
Expand Down
3 changes: 1 addition & 2 deletions SwiftDraw/Sources/SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ extension SVG {
}

init(dom: DOM.SVG, options: Options) {
self.size = CGSize(width: dom.width, height: dom.height)

self.size = CGSize(width: CGFloat(dom.width), height: CGFloat(dom.height))
//To create the draw commands;
// - XML is parsed into DOM.SVG
// - DOM.SVG is converted into a LayerTree
Expand Down
4 changes: 2 additions & 2 deletions SwiftDraw/Tests/LayerTree/LayerTree.BuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ extension LayerTree.Builder {

static func makeTransform(
viewBox: DOM.SVG.ViewBox?,
width: DOM.Length,
height: DOM.Length
width: DOM.Coordinate,
height: DOM.Coordinate
) -> [LayerTree.Transform] {
makeTransform(
x: nil,
Expand Down
Loading