Prints out all path commands it can parse.

This commit is contained in:
treeform 2020-12-02 23:35:23 -08:00
parent 3ae54dad41
commit 4ae59d51da
2 changed files with 56 additions and 12 deletions

View file

@ -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)

View file

@ -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)