Skip to content

0.2.0 - Typed Geometry

Choose a tag to compare

@coenttb coenttb released this 15 Dec 21:04

swift-svg 0.2.0

This release introduces compile-time type safety for SVG coordinates, dimensions, and angles through typed geometry from swift-standards.

Highlights

Type-Safe Coordinates

SVG elements now use phantom-typed coordinates that prevent mixing incompatible values:

let x: W3C_SVG2.X = 100
let y: W3C_SVG2.Y = 200

circle(cx: x, cy: y, r: 40)  // ✓ Compiles
circle(cx: y, cy: x, r: 40)  // ✗ Compile error - can't swap X and Y!

Ergonomic API

Thanks to ExpressibleByIntegerLiteral and ExpressibleByFloatLiteral, your existing code continues to work unchanged:

// These all still work seamlessly
svg(viewBox: .init(minX: 0, minY: 0, width: 100, height: 100)) {
    circle(cx: 50, cy: 50, r: 40)
        .fill("blue")
    
    rect(x: 10, y: 10, width: 80, height: 80)
        .stroke("black")
    
    polygon(coordinates: [(50, 10), (90, 90), (10, 90)])
        .fill("green")
}

Path Data Preservation

Path data strings are now preserved exactly as provided:

// Input is preserved exactly - no reformatting
path(d: "M12 2L2 7l10 5 10-5-10-5z")
// Output: d="M12 2L2 7l10 5 10-5-10-5z"

New Typed Aliases

  • W3C_SVG2.X / W3C_SVG2.Y - Coordinates
  • W3C_SVG2.Width / W3C_SVG2.Height - Dimensions
  • W3C_SVG2.Dx / W3C_SVG2.Dy - Displacements
  • W3C_SVG2.Radius - Magnitudes
  • W3C_SVG2.Degrees - Angles

Updated Types

  • ViewBox: minX, minY, width, height are now typed
  • Transform: translate uses Dx/Dy, rotate/skew use Degrees
  • Polygon/Polyline: init(coordinates:) takes typed coordinate tuples

Dependencies

  • swift-svg-standard 0.2.0
  • swift-svg-rendering 0.2.0
  • swift-w3c-svg 0.2.0

Full Changelog: 0.1.0...0.2.0