Skip to content

Commit

Permalink
Add feature incrementally export SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyvan committed Dec 29, 2023
1 parent 4594985 commit cf4f100
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Sources/geometrize/SVGExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ public struct SVGExporter {
/// A hook that an SVG exporter should use to augment shape styling produced by the getSvgShapeData method.
private let svg_style_hook = "::svg_style_hook::" // swiftlint:disable:this identifier_name

// TODO: get rid of this
public enum RotatedEllipseSVGExportMode {
/// Export as a translated, rotated and scaled svg <ellipse>. OpenFL's SVG library can't handle this
case ellipseItem
/// Export as a <polygon>, OpenFL's SVG library can handle this, but it looks quite ugly
case polygon
}

// TODO: get rid of this
/// Represents the options that can be set for the SVG
public struct ExportOptions {
/// Technique to use when exporting rotated ellipses
let rotatedEllipseExportMode: RotatedEllipseSVGExportMode
/// Id to tag the exported SVG shapes with
var itemId: Int
var itemId: Int // TODO: it is not nice how it is used
public init(rotatedEllipseExportMode: RotatedEllipseSVGExportMode = .ellipseItem, itemId: Int = 0) {
self.rotatedEllipseExportMode = rotatedEllipseExportMode
self.itemId = itemId
Expand Down Expand Up @@ -85,12 +87,14 @@ public struct SVGExporter {
/// - width The width of the SVG image.
/// - height The height of the SVG image.
/// - options additional options used by the exporter.
/// - updateMarker place where new elements should be inserted. Better if this will be correct XML comment.
/// - Returns: A string representing the SVG image.
public func export(
data: [ShapeResult],
width: Int,
height: Int,
options: ExportOptions = ExportOptions()
options: ExportOptions = ExportOptions(),
updateMarker: String?
) -> String {
var str = """
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Expand All @@ -103,11 +107,22 @@ public struct SVGExporter {
str += singleShapeData(color: shapeResult.color, shape: shapeResult.shape, options: options)
}

if let updateMarker {
str += updateMarker
}

str += "</svg>"

return str
}

public func export(
shapeResult: ShapeResult,
options: ExportOptions = ExportOptions()
) -> String {
singleShapeData(color: shapeResult.color, shape: shapeResult.shape, options: options)
}

private func shapeData(rectangle r: Rectangle) -> String {
"<rect x=\"\(min(r.x1, r.x2))\" y=\"\(min(r.y1, r.y2))\" width=\"\(max(r.x1, r.x2) - min(r.x1, r.x2))\" height=\"\(max(r.y1, r.y2) - min(r.y1, r.y2))\" \(svg_style_hook) />"
}
Expand Down

0 comments on commit cf4f100

Please sign in to comment.