Skip to content

Temporary fix for tspan #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion Source/Model/Nodes/SVGText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import Combine
#endif

public class SVGText: SVGNode {
public struct Shift: Codable {
public let dx: String?
public let dy: String?

public static let empty = Shift(dx: nil, dy: nil)
}

public enum Anchor: String, SerializableEnum {
case leading
case center
Expand All @@ -18,12 +25,18 @@ public class SVGText: SVGNode {
public var fill: SVGPaint?
public var stroke: SVGStroke?
public var textAnchor: Anchor = .leading
public var textAnchorString: String? = nil
public var contents: [SVGNode] = [] // Store tspan and text without tspan
public var shift: Shift? = nil
#else
@Published public var text: String
@Published public var font: SVGFont?
@Published public var fill: SVGPaint?
@Published public var stroke: SVGStroke?
@Published public var textAnchor: Anchor = .leading
@Published public var textAnchorString: String? = nil
@Published public var contents: [SVGNode] = []
@Published public var shift: Shift? = nil
#endif

public init(
Expand All @@ -33,16 +46,22 @@ public class SVGText: SVGNode {
stroke: SVGStroke? = nil,
textAnchor: Anchor = .leading,
transform: CGAffineTransform = .identity,
contents: [SVGNode] = [],
opaque: Bool = true,
opacity: Double = 1,
clip: SVGUserSpaceNode? = nil,
mask: SVGNode? = nil
mask: SVGNode? = nil,
shift: SVGText.Shift = .empty,
textAnchorString: String? = nil
) {
self.text = text
self.font = font
self.fill = fill
self.stroke = stroke
self.textAnchor = textAnchor
self.contents = contents
self.shift = shift
self.textAnchorString = textAnchorString
super.init(transform: transform, opaque: opaque, opacity: opacity, clip: clip, mask: mask)
}

Expand Down
14 changes: 12 additions & 2 deletions Source/Parser/SVG/Elements/SVGTextParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ class SVGTextParser: SVGBaseElementParser {
let y = SVGHelper.parseCGFloat(context.properties, "y")
let transform = CGAffineTransform(translationX: x, y: y)

let shift = SVGText.Shift(dx: context.properties["dx"], dy: context.properties["dy"])

if let textNode = context.element.contents.first as? XMLText {
let trimmed = textNode.text.trimmingCharacters(in: .whitespacesAndNewlines).processingWhitespaces()
return SVGText(text: trimmed, font: font, fill: SVGHelper.parseFill(context.styles, context.index), stroke: SVGHelper.parseStroke(context.styles, index: context.index), textAnchor: textAnchor, transform: transform)

return SVGText(text: trimmed, font: font, fill: SVGHelper.parseFill(context.styles, context.index), stroke: SVGHelper.parseStroke(context.styles, index: context.index), textAnchor: textAnchor, transform: transform, shift: shift, textAnchorString: context.style("text-anchor"))
}
return .none
return SVGGroup(contents: parseContents(context: context, delegate: delegate))
//return .none
}

func parseContents(context: SVGNodeContext, delegate: (XMLElement) -> SVGNode?) -> [SVGNode] {
return context.element.contents
.compactMap { $0 as? XMLElement }
.compactMap { delegate($0) }
}

private func parseTextAnchor(_ string: String?) -> SVGText.Anchor {
Expand Down
4 changes: 4 additions & 0 deletions Source/Parser/SVG/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public struct SVGParser {
"g": SVGGroupParser(),
"use": SVGUseParser(),
"text": SVGTextParser(),
"tspan": SVGTextParser(),
"image": SVGImageParser(),
"rect": SVGRectParser(),
"circle": SVGCircleParser(),
Expand All @@ -68,6 +69,9 @@ public struct SVGParser {
]

private static func parse(context: SVGNodeContext) -> SVGNode? {
if context.element.name == "tspan" {
// TODO: handle differently
}
return parsers[context.element.name]?.parse(context: context) {
parse(element: $0, parentContext: context)
}
Expand Down