From a10cbe067203b25d308ecb74e874f0725fd923d6 Mon Sep 17 00:00:00 2001 From: treeform Date: Sat, 16 Nov 2024 14:45:07 -0800 Subject: [PATCH] Fix parseSvg function to accept XmlNode as root parameter --- src/pixie/fileformats/svg.nim | 17 +++++++++++++---- tests/test_svg.nim | 11 ++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/pixie/fileformats/svg.nim b/src/pixie/fileformats/svg.nim index dd1d167e..ac01734b 100644 --- a/src/pixie/fileformats/svg.nim +++ b/src/pixie/fileformats/svg.nim @@ -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() @@ -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() @@ -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: @@ -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) diff --git a/tests/test_svg.nim b/tests/test_svg.nim index 69501191..13b22bd8 100644 --- a/tests/test_svg.nim +++ b/tests/test_svg.nim @@ -1,4 +1,4 @@ -import pixie, pixie/fileformats/svg, strformat, xrays +import pixie, pixie/fileformats/svg, strformat, xrays, xmlparser, xmltree const files = [ "line01", @@ -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 + )