Skip to content

Commit 2eaca8e

Browse files
authored
Merge pull request #556 from treeform/svg_xml_node
Fix parseSvg function to accept XmlNode as root parameter
2 parents 3b65ca5 + a10cbe0 commit 2eaca8e

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

src/pixie/fileformats/svg.nim

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,10 @@ proc parseSvgElement(
500500
raise newException(PixieError, "Unsupported SVG tag: " & node.tag)
501501

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

@@ -521,7 +520,7 @@ proc parseSvg*(
521520

522521
if viewBoxMinX != 0 or viewBoxMinY != 0:
523522
let viewBoxMin = vec2(-viewBoxMinX.float32, -viewBoxMinY.float32)
524-
rootprops.transform = rootprops.transform * translate(viewBoxMin)
523+
rootProps.transform = rootProps.transform * translate(viewBoxMin)
525524

526525
result = Svg()
527526

@@ -535,7 +534,7 @@ proc parseSvg*(
535534
let
536535
scaleX = width.float32 / viewBoxWidth.float32
537536
scaleY = height.float32 / viewBoxHeight.float32
538-
rootprops.transform = rootprops.transform * scale(vec2(scaleX, scaleY))
537+
rootProps.transform = rootProps.transform * scale(vec2(scaleX, scaleY))
539538

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

547+
proc parseSvg*(data: string, width = 0, height = 0): Svg {.raises: [PixieError].} =
548+
## Parse SVG data. Defaults to the SVG's view box size.
549+
try:
550+
let root = parseXml(data)
551+
result = root.parseSvg(width, height)
552+
except PixieError as e:
553+
raise e
554+
except:
555+
raise currentExceptionAsPixieError()
556+
548557
proc newImage*(svg: Svg): Image {.raises: [PixieError].} =
549558
## Render SVG and return the image.
550559
result = newImage(svg.width, svg.height)

tests/test_svg.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pixie, pixie/fileformats/svg, strformat, xrays
1+
import pixie, pixie/fileformats/svg, strformat, xrays, xmlparser, xmltree
22

33
const files = [
44
"line01",
@@ -29,3 +29,12 @@ block:
2929
)
3030
image = newImage(svg)
3131
image.xray(&"tests/fileformats/svg/masters/accessibility-outline.png")
32+
33+
block:
34+
# Test using XML node by itself, see: https://github.com/treeform/pixie/pull/533
35+
let
36+
xmlNode = parseXml(readFile("tests/fileformats/svg/accessibility-outline.svg"))
37+
svg = parseSvg(
38+
xmlNode,
39+
512, 512
40+
)

0 commit comments

Comments
 (0)