Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Geometries): Relative line drawing #14013

15 changes: 9 additions & 6 deletions src/Avalonia.Base/Media/PathMarkupParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void AddLine(ref ReadOnlySpan<char> span, bool relative)
{
ThrowIfDisposed();

_currentPoint = relative
var next = relative
? ReadRelativePoint(ref span, _currentPoint)
: ReadPoint(ref span);

Expand All @@ -268,14 +268,15 @@ private void AddLine(ref ReadOnlySpan<char> span, bool relative)
CreateFigure();
}

_geometryContext.LineTo(_currentPoint);
_geometryContext.LineTo(next);
_currentPoint = next;
}

private void AddHorizontalLine(ref ReadOnlySpan<char> span, bool relative)
{
ThrowIfDisposed();

_currentPoint = relative
var next = relative
? new Point(_currentPoint.X + ReadDouble(ref span), _currentPoint.Y)
: _currentPoint.WithX(ReadDouble(ref span));

Expand All @@ -284,14 +285,15 @@ private void AddHorizontalLine(ref ReadOnlySpan<char> span, bool relative)
CreateFigure();
}

_geometryContext.LineTo(_currentPoint);
_geometryContext.LineTo(next);
_currentPoint = next;
}

private void AddVerticalLine(ref ReadOnlySpan<char> span, bool relative)
{
ThrowIfDisposed();

_currentPoint = relative
var next = relative
? new Point(_currentPoint.X, _currentPoint.Y + ReadDouble(ref span))
: _currentPoint.WithY(ReadDouble(ref span));

Expand All @@ -300,7 +302,8 @@ private void AddVerticalLine(ref ReadOnlySpan<char> span, bool relative)
CreateFigure();
}

_geometryContext.LineTo(_currentPoint);
_geometryContext.LineTo(next);
_currentPoint = next;
}

private void AddCubicBezierCurve(ref ReadOnlySpan<char> span, bool relative)
Expand Down
19 changes: 19 additions & 0 deletions tests/Avalonia.Base.UnitTests/Media/PathMarkupParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,24 @@ public void Should_Parse_Flags_Without_Separator()
Assert.IsType<ArcSegment>(arcSegment);
}
}

[Fact]
public void Should_Handle_StartPoint_After_Empty_Figure()
{
var pathGeometry = new PathGeometry();
using var context = new PathGeometryContext(pathGeometry);
using var parser = new PathMarkupParser(context);
parser.Parse("M50,50z l -5,-5");

Assert.Equal(2, pathGeometry.Figures.Count);

var firstFigure = pathGeometry.Figures[0];

Assert.Equal(new Point(50, 50), firstFigure.StartPoint);

var secondFigure = pathGeometry.Figures[1];

Assert.Equal(new Point(50, 50), secondFigure.StartPoint);
}
}
}