Skip to content
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

Fix parseSvg function to accept XmlNode as root parameter #556

Merged
merged 1 commit into from
Nov 23, 2024
Merged
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
17 changes: 13 additions & 4 deletions src/pixie/fileformats/svg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,10 @@ proc parseSvgElement(
raise newException(PixieError, "Unsupported SVG tag: " & node.tag)

proc parseSvg*(
data: string | XmlNode, width = 0, height = 0
root: XmlNode, width = 0, height = 0
): Svg {.raises: [PixieError].} =
## Parse SVG XML. Defaults to the SVG's view box size.
try:
let root = parseXml(data)
if root.tag != "svg":
failInvalid()

Expand All @@ -521,7 +520,7 @@ proc parseSvg*(

if viewBoxMinX != 0 or viewBoxMinY != 0:
let viewBoxMin = vec2(-viewBoxMinX.float32, -viewBoxMinY.float32)
rootprops.transform = rootprops.transform * translate(viewBoxMin)
rootProps.transform = rootProps.transform * translate(viewBoxMin)

result = Svg()

Expand All @@ -535,7 +534,7 @@ proc parseSvg*(
let
scaleX = width.float32 / viewBoxWidth.float32
scaleY = height.float32 / viewBoxHeight.float32
rootprops.transform = rootprops.transform * scale(vec2(scaleX, scaleY))
rootProps.transform = rootProps.transform * scale(vec2(scaleX, scaleY))

var propertiesStack = @[rootProps]
for node in root.items:
Expand All @@ -545,6 +544,16 @@ proc parseSvg*(
except:
raise currentExceptionAsPixieError()

proc parseSvg*(data: string, width = 0, height = 0): Svg {.raises: [PixieError].} =
## Parse SVG data. Defaults to the SVG's view box size.
try:
let root = parseXml(data)
result = root.parseSvg(width, height)
except PixieError as e:
raise e
except:
raise currentExceptionAsPixieError()

proc newImage*(svg: Svg): Image {.raises: [PixieError].} =
## Render SVG and return the image.
result = newImage(svg.width, svg.height)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_svg.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pixie, pixie/fileformats/svg, strformat, xrays
import pixie, pixie/fileformats/svg, strformat, xrays, xmlparser, xmltree

const files = [
"line01",
Expand Down Expand Up @@ -29,3 +29,12 @@ block:
)
image = newImage(svg)
image.xray(&"tests/fileformats/svg/masters/accessibility-outline.png")

block:
# Test using XML node by itself, see: https://github.com/treeform/pixie/pull/533
let
xmlNode = parseXml(readFile("tests/fileformats/svg/accessibility-outline.svg"))
svg = parseSvg(
xmlNode,
512, 512
)
Loading