Skip to content

Commit

Permalink
Add support for svg paths starting with m
Browse files Browse the repository at this point in the history
  • Loading branch information
ezwelty committed May 10, 2024
1 parent 4f1299b commit ab98d83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/glimpse/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,11 @@ def _from_path(cls, d: str = "") -> "Points":
# moveTo: m (dx,dy)+ | lineTo: l (dx,dy)+ | curveTo: T (dx,dy)+
elif cmd in ("m", "l", "t"):
for dx, dy in _chunks(params, 2):
xy.append((xy[-1][0] + dx, xy[-1][1] + dy))
if not xy:
# Treat first point as absolute
xy.append((dx, dy))
else:
xy.append((xy[-1][0] + dx, xy[-1][1] + dy))
# lineTo: H (x)+
elif cmd == "H":
for (x,) in _chunks(params, 1):
Expand Down
17 changes: 9 additions & 8 deletions tests/test_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ def test_parses_path_commands(
) -> None:
"""Parses all possible path commands."""
xo, yo = 1, 2
# Uppercase
fp = io.StringIO(f"<svg><path d='M {xo},{yo} {cmd}' /></svg>")
coords = glimpse.svg.read(fp)
assert coords["path"][1] == (dxy[0] or xo, dxy[1] or yo)
# Lowercase
fp = io.StringIO(f"<svg><path d='M {xo},{yo} {cmd.lower()}' /></svg>")
coords = glimpse.svg.read(fp)
assert coords["path"][1] == (xo + dxy[0], yo + dxy[1])
for start in ("M", "m"):
# Uppercase command
fp = io.StringIO(f"<svg><path d='{start} {xo},{yo} {cmd}' /></svg>")
coords = glimpse.svg.read(fp)
assert coords["path"][1] == (dxy[0] or xo, dxy[1] or yo)
# Lowercase command
fp = io.StringIO(f"<svg><path d='{start} {xo},{yo} {cmd.lower()}' /></svg>")
coords = glimpse.svg.read(fp)
assert coords["path"][1] == (xo + dxy[0], yo + dxy[1])


def test_errors_for_invalid_path_command() -> None:
Expand Down

0 comments on commit ab98d83

Please sign in to comment.