Skip to content

Commit 0fdd4ac

Browse files
authored
Allows SVGRenderer to export the result from memory
Fixes #120
1 parent ba39eff commit 0fdd4ac

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Sources/SVGRenderer/SVGRenderer.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,20 @@ public class SVGRenderer: Renderer{
231231
public func drawOutput(fileName name: String) throws {
232232
try savePlotImage(fileName: name)
233233
}
234+
235+
/// Returns the content of the SVG generated by the renderer
236+
public var svg: String {
237+
// Build the document.
238+
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">"#
239+
+ "\n" + #"<rect width="100%" height="100%" fill="white"/>"#
240+
let font = #"<defs><style>@import url("https://fonts.googleapis.com/css?family=\#(fontFamily)");</style></defs>"#
241+
let image = header + "\n" + font + "\n" + lines.joined(separator: "\n") + "\n</svg>"
242+
return image
243+
}
234244

235245
func savePlotImage(fileName name: String) throws {
236246
// Build the document.
237-
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">"#
238-
+ "\n" + #"<rect width="100%" height="100%" fill="white"/>"#
239-
let font = #"<defs><style>@import url("https://fonts.googleapis.com/css?family=\#(fontFamily)");</style></defs>"#
240-
let image = header + "\n" + font + "\n" + lines.joined(separator: "\n") + "\n</svg>"
247+
let image = self.svg
241248

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

0 commit comments

Comments
 (0)