svg no more asserts

This commit is contained in:
Ryan Oldenburg 2020-12-13 02:21:18 -06:00
parent db5b1f4cb4
commit 6e55ecc7d4

View file

@ -5,72 +5,78 @@ import chroma, pixie/images, pixie/common, pixie/paths, vmath, xmlparser, xmltre
const svgSignature* = "<?xml"
template failInvalid() =
raise newException(PixieError, "Invalid SVG data")
proc draw(img: Image, matStack: var seq[Mat3], xml: XmlNode) =
case xml.tag:
of "g":
let
fill = xml.attr("fill")
stroke = xml.attr("stroke")
strokeWidth = xml.attr("stroke-width")
transform = xml.attr("transform")
if xml.tag != "g":
raise newException(PixieError, "Unsupported SVG tag: " & xml.tag & ".")
if transform != "":
if transform.startsWith("matrix("):
let arr = transform[7..^2].split(",")
assert arr.len == 6
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
let (bounds, fillImg) =
fillPathBounds(d, fillColor, mat = matStack[^1])
img.draw(fillImg, bounds.xy)
if stroke != "none" and stroke != "":
let strokeColor = parseHtmlColor(stroke).rgba
let strokeWidth =
if strokeWidth == "": 1.0 # Default stroke width is 1px
else: parseFloat(strokeWidth)
let (bounds, strokeImg) =
strokePathBounds(d, strokeColor, strokeWidth, mat = matStack[^1])
img.draw(strokeImg, bounds.xy)
else:
img.draw(matStack, child)
if transform != "":
discard matStack.pop()
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 tag: " & xml.tag & ".")
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:
var xml = parseXml(data)
assert xml.tag == "svg"
var viewBox = xml.attr "viewBox"
let box = viewBox.split(" ")
assert parseInt(box[0]) == 0
assert parseInt(box[1]) == 0
let w = parseInt(box[2])
let h = parseInt(box[3])
result = newImage(w, h)
let xml = parseXml(data)
if xml.tag != "svg":
failInvalid()
let
viewBox = xml.attr("viewBox")
box = viewBox.split(" ")
if parseInt(box[0]) != 0 or parseInt(box[1]) != 0:
failInvalid()
let
width = parseInt(box[2])
height = parseInt(box[3])
result = newImage(width, height)
var matStack = @[mat3()]
for n in xml:
result.draw(matStack, n)