diff --git a/src/pixie/fileformats/svg.nim b/src/pixie/fileformats/svg.nim index 54f345c..99ad575 100644 --- a/src/pixie/fileformats/svg.nim +++ b/src/pixie/fileformats/svg.nim @@ -143,6 +143,9 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx = ty = parseFloat(components[1]) result.transform = result.transform * translate(vec2(tx, ty)) elif f.startsWith("rotate("): + # let + # values = f[7 .. ^2].split(" ") + # angle = parseFloat(values[0]) * -PI / 180 let angle = parseFloat(f[7 .. ^2]) * -PI / 180 result.transform = result.transform * rotationMat3(angle) else: @@ -198,11 +201,18 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) = points = node.attr("points") var vecs: seq[Vec2] - for pair in points.split(" "): - let parts = pair.split(",") - if parts.len != 2: + if points.contains(","): + for pair in points.split(" "): + let parts = pair.split(",") + if parts.len != 2: + failInvalid() + vecs.add(vec2(parseFloat(parts[0]), parseFloat(parts[1]))) + else: + let points = points.split(" ") + if points.len mod 2 != 0: failInvalid() - vecs.add(vec2(parseFloat(parts[0]), parseFloat(parts[1]))) + for i in countup(0, points.len - 2, 2): + vecs.add(vec2(parseFloat(points[i]), parseFloat(points[i + 1]))) if vecs.len == 0: failInvalid() @@ -295,8 +305,8 @@ proc decodeSvg*(data: string, width = 0, height = 0): Image = viewBox = root.attr("viewBox") box = viewBox.split(" ") - if parseInt(box[0]) != 0 or parseInt(box[1]) != 0: - failInvalid() + # if parseInt(box[0]) != 0 or parseInt(box[1]) != 0: + # failInvalid() let viewBoxWidth = parseInt(box[2]) @@ -319,5 +329,5 @@ proc decodeSvg*(data: string, width = 0, height = 0): Image = result.toStraightAlpha() except PixieError as e: raise e - except: - raise newException(PixieError, "Unable to load SVG") + # except: + # raise newException(PixieError, "Unable to load SVG") diff --git a/tests/images/svg/flat-color-icons.png b/tests/images/svg/flat-color-icons.png new file mode 100644 index 0000000..f4ba9f4 Binary files /dev/null and b/tests/images/svg/flat-color-icons.png differ diff --git a/tests/images/svg/ionicons.png b/tests/images/svg/ionicons.png new file mode 100644 index 0000000..e5ff3a1 Binary files /dev/null and b/tests/images/svg/ionicons.png differ diff --git a/tests/images/svg/twbs-icons.png b/tests/images/svg/twbs-icons.png index 88df646..98ad662 100644 Binary files a/tests/images/svg/twbs-icons.png and b/tests/images/svg/twbs-icons.png differ diff --git a/tests/megatest.nim b/tests/megatest.nim index 8405e4e..0d001e6 100644 --- a/tests/megatest.nim +++ b/tests/megatest.nim @@ -1,34 +1,68 @@ -import os, pixie, pixie/fileformats/svg, strformat +import cligen, os, pixie, pixie/fileformats/svg, strformat # Clone https://github.com/twbs/icons # Check out commit f364cb14dfc0703b9e3ef10c8b490a71dfef1e9d +# Clone https://github.com/icons8/flat-color-icons +# Check out commit 8eccbbbd8b2af1d2c9593e7cfba5ecb0d68ee378 + +# Clone https://github.com/ionic-team/ionicons +# Check out commit 0d7f507677f8d317ce6882729ffecf46e215e01a + +# Clone https://github.com/tabler/tabler-icons +# Check out commit ccf2784b57e42a2b2221963f92146fd7b249b5b7 + +# Clone https://github.com/simple-icons/simple-icons +# Check out commit 355454cb6caa02aba70638631c557d4e06205710 + +type IconSet = object + name: string + path: string + const - iconsPath = "../icons/icons/*" + iconSets = [ + IconSet(name: "twbs-icons", path: "../icons/icons/*"), + IconSet(name: "flat-color-icons", path: "../flat-color-icons/svg/*"), + IconSet(name: "ionicons", path: "../ionicons/src/svg/*"), + # IconSet(name: "tabler-icons", path: "../tabler-icons/icons/*"), + # IconSet(name: "simple-icons", path: "../simple-icons/icons/*") + ] width = 32 height = 32 -var images: seq[(string, Image)] +proc renderIconSet(index: int) = + let iconSet = iconSets[index] + + var images: seq[(string, Image)] + + for filePath in walkFiles(iconSet.path): + let + (_, name, _) = splitFile(filePath) + image = decodeSvg(readFile(filePath), width, height) + + images.add((name, image)) -for path in walkFiles(iconsPath): let - (_, name, _) = splitFile(path) - image = decodeSvg(readFile(path), width, height) + columns = 50 + rows = (images.len + columns - 1) div columns + rendered = newImage((width + 4) * columns, (height + 4) * rows) - images.add((name, image)) + for i in 0 ..< rows: + for j in 0 ..< max(images.len - i * columns, 0): + let (_, icon) = images[i * columns + j] + rendered.draw( + icon, + vec2(((width + 4) * j + 2).float32, ((height + 4) * i + 2).float32), + bmOverwrite + ) -let - columns = 50 - rows = (images.len + columns - 1) div columns - rendered = newImage((width + 4) * columns, (height + 4) * rows) + rendered.writeFile(&"tests/images/svg/{iconSet.name}.png") -for i in 0 ..< rows: - for j in 0 ..< max(images.len - i * columns, 0): - let (_, icon) = images[i * columns + j] - rendered.draw( - icon, - vec2(((width + 4) * j + 2).float32, ((height + 4) * i + 2).float32), - bmOverwrite - ) +proc main(index = -1) = + if index >= 0: + renderIconSet(index) + else: + for i in 0 ..< iconSets.len: + renderIconSet(i) -rendered.writeFile(&"tests/images/svg/twbs-icons.png") +dispatch(main)