Skip to content

Commit

Permalink
fix(Geometries): Relative line drawing (#14013)
Browse files Browse the repository at this point in the history
* test: Add invalid draw test

* fix: Geometry parsing

* test: Refactoring the test for address `h` and `v` command

* fix: Parser of `h` command

* fix: parser `v` command

* test: Add case `M50,50z l -5,-5`

* test: Add PathMarkupParserTests

* test: Removed render tests
  • Loading branch information
workgroupengineering authored and maxkatz6 committed Jan 17, 2024
1 parent 366a935 commit 906e0cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
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);
}
}
}

0 comments on commit 906e0cc

Please sign in to comment.