very strange issue

This commit is contained in:
Ryan Oldenburg 2022-01-25 17:04:43 -06:00
parent d65fd68161
commit 12636e4e9f
6 changed files with 42 additions and 11 deletions

View file

@ -560,6 +560,48 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
except:
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*(
root: XmlNode, width = 0, height = 0
): Image {.raises: [PixieError].} =
@ -600,14 +642,3 @@ proc decodeSvg*(
raise e
except:
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)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 280 KiB

After

Width:  |  Height:  |  Size: 280 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 644 KiB

After

Width:  |  Height:  |  Size: 674 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 610 KiB

After

Width:  |  Height:  |  Size: 629 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 519 KiB

After

Width:  |  Height:  |  Size: 519 KiB