From 4ae59d51da1fdd2b31edf96460ee6984b20d452b Mon Sep 17 00:00:00 2001 From: treeform Date: Wed, 2 Dec 2020 23:35:23 -0800 Subject: [PATCH] Prints out all path commands it can parse. --- src/pixie/paths.nim | 37 +++++++++++++++++++++++++------------ tests/test_paths.nim | 31 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/pixie/paths.nim b/src/pixie/paths.nim index 6013c97..c95d824 100644 --- a/src/pixie/paths.nim +++ b/src/pixie/paths.nim @@ -48,7 +48,10 @@ proc parsePath*(path: string): Path = if command != Start: let num = commandNumbers(command) if num > 0: - assert numbers.len mod num == 0 + if numbers.len mod num != 0: + raise newException(PixieError, + "Could not parse path: " & $command & " has wrong number of prams," & + " got " & $numbers.len & " but expected " & $num & ".") for batch in 0 ..< numbers.len div num: result.commands.add PathCommand( kind: command, @@ -145,17 +148,27 @@ proc parsePath*(path: string): Path = proc `$`*(path: Path): string = for command in path.commands: - case command.kind: - of Move: - result.add "M" - of Arc: - result.add "A" - of Line: - result.add "L" - of End: - result.add "Z" - else: - result.add "?" + case command.kind + of Move: result.add "M" + of Line: result.add "L" + of HLine: result.add "H" + of VLine: result.add "V" + of Cubic: result.add "C" + of SCurve: result.add "S" + of Quad: result.add "Q" + of TQuad: result.add "T" + of Arc: result.add "A" + of RMove: result.add "m" + of RLine: result.add "l" + of RHLine: result.add "h" + of RVLine: result.add "v" + of RCubic: result.add "c" + of RSCurve: result.add "s" + of RQuad: result.add "q" + of RTQuad: result.add "t" + of RArc: result.add "a" + of End: result.add "Z" + of Start: result.add "?" for number in command.numbers: if floor(number) == number: result.add $(number.int) diff --git a/tests/test_paths.nim b/tests/test_paths.nim index ed3f6c9..2788a2c 100644 --- a/tests/test_paths.nim +++ b/tests/test_paths.nim @@ -1,5 +1,36 @@ import pixie, chroma +block: + let pathStr = """ + m 1 2 + l 3 4 + h 5 + v 6 + c 0 0 0 0 0 0 + q 1 1 1 1 + t 2 2 + a 7 7 7 7 7 7 7 + z + """ + let path = parsePath(pathStr) + doAssert $path == "m1 2 l3 4 h5 v6 c0 0 0 0 0 0 q1 1 1 1 t2 2 a7 7 7 7 7 7 7 Z" + +block: + let pathStr = """ + M 1 2 + L 3 4 + H 5 + V 6 + C 0 0 0 0 0 0 + Q 1 1 1 1 + T 2 2 + A 7 7 7 7 7 7 7 + z + """ + let path = parsePath(pathStr) + doAssert $path == "M1 2 L3 4 H5 V6 C0 0 0 0 0 0 Q1 1 1 1 T2 2 A7 7 7 7 7 7 7 Z" + + block: let pathStr = "M 0.1E-10 0.1e10 L2+2 L3-3 L0.1E+10-1" let path = parsePath(pathStr)