Skip to content

coenttb/swift-svg-printer

swift-svg-printer

CI Development Status

Renders SVG types to strings and bytes for cross-platform SVG generation.

Overview

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.

Features

  • 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

Installation

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")
        ]
    )
]

Quick Start

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"/>

Usage Examples

Pretty Printing

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)

Attribute Management

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"

Related Packages

Dependencies

Used By

  • swift-svg: A Swift package for type-safe SVG generation.

Third-Party Dependencies

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the Apache 2.0 License. See LICENSE for details.

About

A Swift package for rendering SVG to String or bytes.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Languages