From 4d148701501d2f099e78d9a770eff867a928b287 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Mon, 14 Dec 2020 13:11:14 -0600 Subject: [PATCH] case back --- src/pixie/fileformats/svg.nim | 99 ++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/src/pixie/fileformats/svg.nim b/src/pixie/fileformats/svg.nim index 13c8b65..6819ee3 100644 --- a/src/pixie/fileformats/svg.nim +++ b/src/pixie/fileformats/svg.nim @@ -9,57 +9,58 @@ template failInvalid() = raise newException(PixieError, "Invalid SVG data") proc draw(img: Image, matStack: var seq[Mat3], xml: XmlNode) = - if xml.tag != "g": + case xml.tag: + of "g": + let + fill = xml.attr("fill") + stroke = xml.attr("stroke") + strokeWidth = xml.attr("stroke-width") + transform = xml.attr("transform") + + if transform != "": + if transform.startsWith("matrix("): + let arr = transform[7..^2].split(",") + if arr.len != 6: + failInvalid() + var m = mat3() + m[0] = parseFloat(arr[0]) + m[1] = parseFloat(arr[1]) + m[3] = parseFloat(arr[2]) + m[4] = parseFloat(arr[3]) + m[6] = parseFloat(arr[4]) + m[7] = parseFloat(arr[5]) + matStack.add(matStack[^1] * m) + else: + raise newException( + PixieError, "Unsupported SVG transform: " & transform & ".") + + for child in xml: + if child.tag == "path": + let d = child.attr("d") + + if fill != "none" and fill != "": + let + fillColor = parseHtmlColor(fill).rgba + (bounds, fillImg) = fillPathBounds(d, fillColor, matStack[^1]) + img.draw(fillImg, bounds.xy) + + if stroke != "none" and stroke != "": + let + strokeColor = parseHtmlColor(stroke).rgba + strokeWidth = + if strokeWidth == "": 1.0 # Default stroke width is 1px + else: parseFloat(strokeWidth) + (bounds, strokeImg) = + strokePathBounds(d, strokeColor, strokeWidth, matStack[^1]) + img.draw(strokeImg, bounds.xy) + else: + img.draw(matStack, child) + + if transform != "": + discard matStack.pop() + else: raise newException(PixieError, "Unsupported SVG tag: " & xml.tag & ".") - let - fill = xml.attr("fill") - stroke = xml.attr("stroke") - strokeWidth = xml.attr("stroke-width") - transform = xml.attr("transform") - - if transform != "": - if transform.startsWith("matrix("): - let arr = transform[7..^2].split(",") - if arr.len != 6: - failInvalid() - var m = mat3() - m[0] = parseFloat(arr[0]) - m[1] = parseFloat(arr[1]) - m[3] = parseFloat(arr[2]) - m[4] = parseFloat(arr[3]) - m[6] = parseFloat(arr[4]) - m[7] = parseFloat(arr[5]) - matStack.add(matStack[^1] * m) - else: - raise newException( - PixieError, "Unsupported SVG transform: " & transform & ".") - - for child in xml: - if child.tag == "path": - let d = child.attr("d") - - if fill != "none" and fill != "": - let - fillColor = parseHtmlColor(fill).rgba - (bounds, fillImg) = fillPathBounds(d, fillColor, matStack[^1]) - img.draw(fillImg, bounds.xy) - - if stroke != "none" and stroke != "": - let - strokeColor = parseHtmlColor(stroke).rgba - strokeWidth = - if strokeWidth == "": 1.0 # Default stroke width is 1px - else: parseFloat(strokeWidth) - (bounds, strokeImg) = - strokePathBounds(d, strokeColor, strokeWidth, matStack[^1]) - img.draw(strokeImg, bounds.xy) - else: - img.draw(matStack, child) - - if transform != "": - discard matStack.pop() - proc decodeSvg*(data: string): Image = ## Render SVG file and return the image. try: