Skip to content

Path.TryParseSvgPath starts a command after Z at the wrong point #443

Backlog

Description

@sebastienros

Note

Fixed by #442. With that PR the program below prints (10, 10) (15, 10) and (10, 10) (30, 30) for the open subpaths of the first two inputs.

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp.Drawing
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp.Drawing version

main at commit a4e94b8

Other ImageSharp packages and versions

SixLabors.ImageSharp 5.0.0-alpha.0.38, SixLabors.Fonts 3.2.1, SixLabors.PolygonClipper 2.0.0-alpha.0.2

Environment (Operating system, version and so on)

macOS 15.8.3 (arm64)

.NET Framework version

.NET 10 (net10.0)

Description

In SVG path data, a drawing command that follows a close path (Z or z) without a move command starts a new subpath at the start point of the closed subpath. Path.TryParseSvgPath starts it at the last point of the closed subpath instead. For a relative command the end point is computed from the right point, so the new segment connects the wrong start to the right end.

Explanation

In Path.TryParseSvgPath (Path.cs), the Z case (line 542) is:

case 'Z':
    _ = builder.CloseFigure();
    c = first;
    break;

c, the current point of the parser, is set to the start point of the subpath, so relative coordinates of the next command are right. But PathBuilder.CloseFigure (PathBuilder.cs, line 724) only closes the figure and starts a new one; the current point of the builder stays at the last point. The next LineTo (PathBuilder.LineTo calls AddLine(this.currentPoint, point), line 133) therefore starts at the last point of the closed figure.

The SVG specification (paths, "closepath"): "If a closepath is followed immediately by any other command, then the next subpath starts at the same initial point as the current subpath."

Suggested fix

Move the builder to the start point after the close:

case 'Z':
    _ = builder.CloseFigure().MoveTo(first);
    c = first;
    break;

A following M replaces that start point, so Z M x y is not changed. When nothing follows the Z, the builder keeps no empty figure: in a separate check on main, new PathBuilder() with two lines, CloseFigure().MoveTo(start), and Build() gave one subpath.

#442 makes this change in the Z case of Path.TryParseSvgPath.

Notes

  • Found by the resvg conformance suite of the SVG work.
  • Not checked: other path producers of the library that call CloseFigure and then draw on.

Steps to Reproduce

Project file (Repro.csproj). It references a Release build of main; the path is a placeholder.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="SixLabors.ImageSharp.Drawing" HintPath="path/to/SixLabors.ImageSharp.Drawing.dll" />
    <PackageReference Include="SixLabors.ImageSharp" Version="5.0.0-alpha.0.38" />
    <PackageReference Include="SixLabors.Fonts" Version="3.2.1" />
    <PackageReference Include="SixLabors.PolygonClipper" Version="2.0.0-alpha.0.2" />
  </ItemGroup>
</Project>

Program.cs:

using SixLabors.ImageSharp.Drawing;
using Path = SixLabors.ImageSharp.Drawing.Path;

// After "Z" the current point is the start point of the closed subpath, (10, 10) here.
// A drawing command that follows without a move command starts a new subpath at that point.
foreach (string data in new[] { "M 10 10 L 20 10 L 20 20 Z l 5 0", "M 10 10 L 20 10 L 20 20 Z L 30 30", "M 10 10 L 20 10 L 20 20 Z M 10 10 l 5 0" })
{
    _ = Path.TryParseSvgPath(data, out IPath? path);
    Console.WriteLine(data);
    foreach (ISimplePath subpath in path!.Flatten())
    {
        Console.WriteLine($"    {(subpath.IsClosed ? "closed" : "open  ")} {string.Join(" ", subpath.Points.ToArray().Select(p => $"({p.X}, {p.Y})"))}");
    }
}

Run with dotnet run -c Release. A Debug build of main gives the same output.

Observed output:

M 10 10 L 20 10 L 20 20 Z l 5 0
    closed (10, 10) (20, 10) (20, 20)
    open   (20, 20) (15, 10)
M 10 10 L 20 10 L 20 20 Z L 30 30
    closed (10, 10) (20, 10) (20, 20)
    open   (20, 20) (30, 30)
M 10 10 L 20 10 L 20 20 Z M 10 10 l 5 0
    closed (10, 10) (20, 10) (20, 20)
    open   (10, 10) (15, 10)

Expected: the open subpath of the first input is (10, 10) (15, 10), like the third input, which has an explicit move command; the open subpath of the second input is (10, 10) (30, 30).

Images

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions