Renders SVG types to strings and bytes for cross-platform SVG generation.
swift-svg-printer provides the rendering layer for the SVG type system, converting swift-svg-types elements into SVG markup as strings or byte arrays.
- Renders to
ContiguousArray<UInt8>for memory efficiency - String conversion via
render()method - Attribute escaping and formatting for valid SVG output
- Configurable indentation and pretty-printing
- Dependencies: swift-svg-types, swift-collections, swift-dependencies
Add swift-svg-printer to your Package.swift:
dependencies: [
.package(url: "https://github.com/coenttb/swift-svg-printer", from: "0.1.0")
],
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "SVGPrinter", package: "swift-svg-printer")
]
)
]import SVGPrinter
// Create a printer
var printer = SVGPrinter()
// Build SVG markup
printer.append("<circle cx=\"50\" cy=\"50\" r=\"40\"/>")
// Get result as string
let svgString = String(decoding: printer.bytes, as: UTF8.self)
// Output: <circle cx="50" cy="50" r="40"/>import SVGPrinter
var printer = SVGPrinter(.pretty)
printer.append("<svg>")
printer.appendNewlineIfNeeded()
printer.indent()
printer.appendIndentationIfNeeded()
printer.append("<circle cx=\"50\" cy=\"50\" r=\"40\"/>")
printer.outdent()
printer.appendNewlineIfNeeded()
printer.append("</svg>")
let output = String(decoding: printer.bytes, as: UTF8.self)import SVGPrinter
import OrderedCollections
var printer = SVGPrinter()
printer.attributes["fill"] = "red"
printer.attributes["stroke"] = "black"
// Attributes can be accessed and managed
let fillColor = printer.attributes["fill"] // "red"- swift-svg-types: A Swift package with foundational types for SVG.
- swift-svg: A Swift package for type-safe SVG generation.
- pointfreeco/swift-dependencies: A dependency management library for controlling dependencies in Swift.
- apple/swift-collections: Commonly used data structures for Swift.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the Apache 2.0 License. See LICENSE for details.