another iconset + fixes

This commit is contained in:
Ryan Oldenburg 2021-02-23 17:28:18 -06:00
parent 2dfe06ca60
commit 380d284a66
3 changed files with 30 additions and 19 deletions

View file

@ -14,6 +14,7 @@ type Ctx = object
strokeLineCap: LineCap strokeLineCap: LineCap
strokeLineJoin: LineJoin strokeLineJoin: LineJoin
transform: Mat3 transform: Mat3
shouldStroke: bool
template failInvalid() = template failInvalid() =
raise newException(PixieError, "Invalid SVG data") raise newException(PixieError, "Invalid SVG data")
@ -24,7 +25,8 @@ proc attrOrDefault(node: XmlNode, name, default: string): string =
result = default result = default
proc initCtx(): Ctx = proc initCtx(): Ctx =
result.fill = parseHtmlColor("black").rgba.toPremultipliedAlpha() result.fill = parseHtmlColor("black").rgba
result.stroke = parseHtmlColor("black").rgba
result.strokeWidth = 1 result.strokeWidth = 1
result.transform = mat3() result.transform = mat3()
@ -81,12 +83,15 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
else: else:
result.fill = parseHtmlColor(fill).rgba.toPremultipliedAlpha() result.fill = parseHtmlColor(fill).rgba.toPremultipliedAlpha()
if stroke == "" or fill == "currentColor": if stroke == "":
discard # Inherit discard # Inherit
elif stroke == "currentColor":
result.shouldStroke = true
elif stroke == "none": elif stroke == "none":
result.stroke = ColorRGBA() result.stroke = ColorRGBA()
else: else:
result.stroke = parseHtmlColor(stroke).rgba.toPremultipliedAlpha() result.stroke = parseHtmlColor(stroke).rgba.toPremultipliedAlpha()
result.shouldStroke = true
if strokeWidth == "": if strokeWidth == "":
discard # Inherit discard # Inherit
@ -94,6 +99,10 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
if strokeWidth.endsWith("px"): if strokeWidth.endsWith("px"):
strokeWidth = strokeWidth[0 .. ^3] strokeWidth = strokeWidth[0 .. ^3]
result.strokeWidth = parseFloat(strokeWidth) result.strokeWidth = parseFloat(strokeWidth)
result.shouldStroke = true
if result.stroke == ColorRGBA() or result.strokeWidth <= 0:
result.shouldStroke = false
if strokeLineCap == "": if strokeLineCap == "":
discard # Inherit discard # Inherit
@ -165,14 +174,20 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
let let
components = f[10 .. ^2].split(" ") components = f[10 .. ^2].split(" ")
tx = parseFloat(components[0]) tx = parseFloat(components[0])
ty = parseFloat(components[1]) ty = if components[1].len == 0: 0.0 else: parseFloat(components[1])
result.transform = result.transform * translate(vec2(tx, ty)) result.transform = result.transform * translate(vec2(tx, ty))
elif f.startsWith("rotate("): elif f.startsWith("rotate("):
# let let
# values = f[7 .. ^2].split(" ") values = f[7 .. ^2].split(" ")
# angle = parseFloat(values[0]) * -PI / 180 angle = parseFloat(values[0]) * -PI / 180
let angle = parseFloat(f[7 .. ^2]) * -PI / 180 var cx, cy: float32
result.transform = result.transform * rotationMat3(angle) if values.len > 1:
cx = parseFloat(values[1])
if values.len > 2:
cy = parseFloat(values[2])
let center = vec2(cx, cy)
result.transform = result.transform *
translate(center) * rotationMat3(angle) * translate(-center)
else: else:
failInvalidTransform(transform) failInvalidTransform(transform)
@ -199,7 +214,7 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
path = parsePath(d) path = parsePath(d)
if ctx.fill != ColorRGBA(): if ctx.fill != ColorRGBA():
img.fillPath(path, ctx.fill, ctx.transform, ctx.fillRule) img.fillPath(path, ctx.fill, ctx.transform, ctx.fillRule)
if ctx.stroke != ColorRGBA() and ctx.strokeWidth > 0: if ctx.shouldStroke:
img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth) img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth)
of "line": of "line":
@ -217,7 +232,7 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
if ctx.fill != ColorRGBA(): if ctx.fill != ColorRGBA():
img.fillPath(path, ctx.fill, ctx.transform) img.fillPath(path, ctx.fill, ctx.transform)
if ctx.stroke != ColorRGBA() and ctx.strokeWidth > 0: if ctx.shouldStroke:
img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth) img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth)
of "polyline", "polygon": of "polyline", "polygon":
@ -253,7 +268,7 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
if ctx.fill != ColorRGBA(): if ctx.fill != ColorRGBA():
img.fillPath(path, ctx.fill, ctx.transform) img.fillPath(path, ctx.fill, ctx.transform)
if ctx.stroke != ColorRGBA() and ctx.strokeWidth > 0: if ctx.shouldStroke:
img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth) img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth)
of "rect": of "rect":
@ -291,7 +306,7 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
if ctx.fill != ColorRGBA(): if ctx.fill != ColorRGBA():
img.fillPath(path, ctx.fill, ctx.transform) img.fillPath(path, ctx.fill, ctx.transform)
if ctx.stroke != ColorRGBA() and ctx.strokeWidth > 0: if ctx.shouldStroke:
img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth) img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth)
of "circle", "ellipse": of "circle", "ellipse":
@ -313,7 +328,7 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
if ctx.fill != ColorRGBA(): if ctx.fill != ColorRGBA():
img.fillPath(path, ctx.fill, ctx.transform) img.fillPath(path, ctx.fill, ctx.transform)
if ctx.stroke != ColorRGBA() and ctx.strokeWidth > 0: if ctx.shouldStroke:
img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth) img.strokePath(path, ctx.stroke, ctx.transform, ctx.strokeWidth)
else: else:
@ -329,15 +344,11 @@ proc decodeSvg*(data: string, width = 0, height = 0): Image =
let let
viewBox = root.attr("viewBox") viewBox = root.attr("viewBox")
box = viewBox.split(" ") box = viewBox.split(" ")
# if parseInt(box[0]) != 0 or parseInt(box[1]) != 0:
# failInvalid()
let
viewBoxWidth = parseInt(box[2]) viewBoxWidth = parseInt(box[2])
viewBoxHeight = parseInt(box[3]) viewBoxHeight = parseInt(box[3])
var rootCtx = initCtx() var rootCtx = initCtx()
rootCtx = decodeCtx(rootCtx, root)
if width == 0 and height == 0: # Default to the view box size if width == 0 and height == 0: # Default to the view box size
result = newImage(viewBoxWidth, viewBoxHeight) result = newImage(viewBoxWidth, viewBoxHeight)
else: else:

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 KiB

View file

@ -24,7 +24,7 @@ const
IconSet(name: "twbs-icons", path: "../icons/icons/*"), IconSet(name: "twbs-icons", path: "../icons/icons/*"),
IconSet(name: "flat-color-icons", path: "../flat-color-icons/svg/*"), IconSet(name: "flat-color-icons", path: "../flat-color-icons/svg/*"),
IconSet(name: "ionicons", path: "../ionicons/src/svg/*"), IconSet(name: "ionicons", path: "../ionicons/src/svg/*"),
# IconSet(name: "tabler-icons", path: "../tabler-icons/icons/*"), IconSet(name: "tabler-icons", path: "../tabler-icons/icons/*"),
IconSet(name: "simple-icons", path: "../simple-icons/icons/*") IconSet(name: "simple-icons", path: "../simple-icons/icons/*")
] ]
width = 32 width = 32