Skip to content
Merged
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
15 changes: 11 additions & 4 deletions Sources/SVGRenderer/SVGRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,20 @@ public class SVGRenderer: Renderer{
public func drawOutput(fileName name: String) throws {
try savePlotImage(fileName: name)
}

/// Returns the content of the SVG generated by the renderer
public var svg: String {
// Build the document.
let header = #"<svg height="\#(imageSize.height)" width="\#(imageSize.width)" version="4.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">"#
+ "\n" + #"<rect width="100%" height="100%" fill="white"/>"#
let font = #"<defs><style>@import url("https://fonts.googleapis.com/css?family=\#(fontFamily)");</style></defs>"#
let image = header + "\n" + font + "\n" + lines.joined(separator: "\n") + "\n</svg>"
return image
}

func savePlotImage(fileName name: String) throws {
// Build the document.
let header = #"<svg height="\#(imageSize.height)" width="\#(imageSize.width)" version="4.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">"#
+ "\n" + #"<rect width="100%" height="100%" fill="white"/>"#
let font = #"<defs><style>@import url("https://fonts.googleapis.com/css?family=\#(fontFamily)");</style></defs>"#
let image = header + "\n" + font + "\n" + lines.joined(separator: "\n") + "\n</svg>"
let image = self.svg

let url = URL(fileURLWithPath: "\(name).svg")
try image.write(to: url, atomically: true, encoding: .utf8)
Expand Down