very strange issue
|
@ -560,6 +560,48 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
|
||||||
except:
|
except:
|
||||||
raise currentExceptionAsPixieError()
|
raise currentExceptionAsPixieError()
|
||||||
|
|
||||||
|
proc decodeSvg*(
|
||||||
|
data: string, width = 0, height = 0
|
||||||
|
): Image {.raises: [PixieError].} =
|
||||||
|
## Render SVG XML and return the image. Defaults to the SVG's view box size.
|
||||||
|
try:
|
||||||
|
let root = parseXml(data)
|
||||||
|
if root.tag != "svg":
|
||||||
|
failInvalid()
|
||||||
|
|
||||||
|
let
|
||||||
|
viewBox = root.attr("viewBox")
|
||||||
|
box = viewBox.split(" ")
|
||||||
|
viewBoxMinX = parseInt(box[0])
|
||||||
|
viewBoxMinY = parseInt(box[1])
|
||||||
|
viewBoxWidth = parseInt(box[2])
|
||||||
|
viewBoxHeight = parseInt(box[3])
|
||||||
|
|
||||||
|
var rootCtx = initCtx()
|
||||||
|
rootCtx = decodeCtx(rootCtx, root)
|
||||||
|
|
||||||
|
if viewBoxMinX != 0 or viewBoxMinY != 0:
|
||||||
|
let viewBoxMin = vec2(-viewBoxMinX.float32, -viewBoxMinY.float32)
|
||||||
|
rootCtx.transform = rootCtx.transform * translate(viewBoxMin)
|
||||||
|
|
||||||
|
if width == 0 and height == 0: # Default to the view box size
|
||||||
|
result = newImage(viewBoxWidth, viewBoxHeight)
|
||||||
|
else:
|
||||||
|
result = newImage(width, height)
|
||||||
|
|
||||||
|
let
|
||||||
|
scaleX = width.float32 / viewBoxWidth.float32
|
||||||
|
scaleY = height.float32 / viewBoxHeight.float32
|
||||||
|
rootCtx.transform = rootCtx.transform * scale(vec2(scaleX, scaleY))
|
||||||
|
|
||||||
|
var ctxStack = @[rootCtx]
|
||||||
|
for node in root:
|
||||||
|
result.draw(node, ctxStack)
|
||||||
|
except PixieError as e:
|
||||||
|
raise e
|
||||||
|
except:
|
||||||
|
raise newException(PixieError, "Unable to load SVG")
|
||||||
|
|
||||||
proc decodeSvg*(
|
proc decodeSvg*(
|
||||||
root: XmlNode, width = 0, height = 0
|
root: XmlNode, width = 0, height = 0
|
||||||
): Image {.raises: [PixieError].} =
|
): Image {.raises: [PixieError].} =
|
||||||
|
@ -600,14 +642,3 @@ proc decodeSvg*(
|
||||||
raise e
|
raise e
|
||||||
except:
|
except:
|
||||||
raise newException(PixieError, "Unable to load SVG")
|
raise newException(PixieError, "Unable to load SVG")
|
||||||
|
|
||||||
proc decodeSvg*(
|
|
||||||
data: string, width = 0, height = 0
|
|
||||||
): Image {.raises: [PixieError].} =
|
|
||||||
## Render SVG data and return the image. Defaults to the SVG's view box size.
|
|
||||||
let root =
|
|
||||||
try:
|
|
||||||
parseXml(data)
|
|
||||||
except:
|
|
||||||
raise currentExceptionAsPixieError()
|
|
||||||
decodeSvg(root)
|
|
||||||
|
|
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 280 KiB |
Before Width: | Height: | Size: 644 KiB After Width: | Height: | Size: 674 KiB |
Before Width: | Height: | Size: 1.4 MiB After Width: | Height: | Size: 1.4 MiB |
Before Width: | Height: | Size: 610 KiB After Width: | Height: | Size: 629 KiB |
Before Width: | Height: | Size: 519 KiB After Width: | Height: | Size: 519 KiB |