Skip to content

Commit

Permalink
Merge pull request #556 from treeform/svg_xml_node
Browse files Browse the repository at this point in the history
Fix parseSvg function to accept XmlNode as root parameter
  • Loading branch information
treeform authored Nov 23, 2024
2 parents 3b65ca5 + a10cbe0 commit 2eaca8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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
)

0 comments on commit 2eaca8e

Please sign in to comment.