Repository navigation
Description
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
DEBUGandRELEASEmode - I have searched open and closed issues to ensure it has not already been reported
ImageSharp.Drawing version
main at commit a4e94b8 (the same GenerateDashes code is in v3.1.3)
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
A dash pattern with a zero-length dash (for example [0, 2], a common way to draw a dotted line with round caps) draws nothing. SplitPathExtensions.GenerateDashes creates the zero-length dashes and then drops them, so neither Draw(Pen, IPath) nor Fill(Pen, IPath) gets anything to stroke. The stroker itself can draw a point: a zero-length subpath with a round cap draws a dot.
Explanation
SplitPathExtensions.GenerateDashes (SplitPathExtensions.cs) walks each flattened subpath. When the current pattern element has length 0, targetLength is 0, and the walker splits the segment at t = targetLength / segLen = 0 (line 199). The "on" span is then the two equal points current and split, and it is passed to FlushBuffer.
FlushBuffer (line 247) keeps a span only if its first and last points differ (line 251):
// Spans whose first and last points coincide have no drawable extent
// (or would form a degenerate loop), so they are dropped.
if (buffer.Count >= 2 && buffer[0] != buffer[^1])For butt caps a zero-length span has no extent. For round and square caps it does: the cap of each end adds half the width around the point. Both stroke routes get the dashes from this method:
Draw(Pen, IPath):DrawingCanvasBatcher<TPixel>callsGenerateDashes(Processing/DrawingCanvasBatcher{TPixel}.cs, line 768).Fill(Pen, IPath)andGenerateOutlinewith a pattern:OutlinePathExtensions.cs, line 159.
When every dash of the pattern has zero length, GenerateDashes returns Path.Empty and nothing is drawn.
Suggested fix
- In
GenerateDashes, keep zero-length "on" spans when the pen has round or square caps.GenerateDashesdoes not know the cap, so either keep them always (as a subpath of two equal points) and let the stroker decide, or add an overload that takes the cap.FlushBuffershould still drop a closed span whose points coincide, if that is the degenerate loop the comment means. - The stroker must then draw such a subpath by its cap: a circle for round caps and a square for square caps. For square caps the square should be turned to the direction of the path at that point, as SVG requires. A two-point subpath with equal points has no direction, so
GenerateDashesshould keep the direction, for example by emitting the dash as a very short segment along the path direction, or by recording the direction with the point. For butt caps a zero-length dash should draw nothing. Today a zero-length subpath with butt caps draws an axis-aligned square of the stroke width, which a comment inDefaultRasterizer.StrokeLinearizer.EmitPointStrokeContourdescribes as intended, so a kept zero-length dash must not take that route with butt caps.
DefaultRasterizer.StrokeLinearizer.EmitPointStrokeContour already draws a circle for a point with a round cap, so step 1 alone should fix the round-cap case for Draw (an assumption; not tried). In a separate check on main, Fill(Pen, IPath) (which uses PolygonStroker of SixLabors.PolygonClipper) also drew a disc for a zero-length subpath with a round cap and a 20 x 20 square with a square cap, so that route also handles points.
The SVG work does not change this code; the two resvg tests painting/stroke-dasharray/0-n-with-round-caps and 0-n-with-square-caps fail in the SVG work for this reason.
Notes
- Found by the resvg conformance suite of the SVG work.
- Not run: the orientation of square dots on a diagonal path; it depends on the fix.
- Not run on v3.1.3: its
SplitPathExtensions.cshas the same split (line 199) and the same check inFlushBuffer(line 251).
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 Path = SixLabors.ImageSharp.Drawing.Path;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
// A dash pattern of zero-length dashes with 40 pixel gaps (pattern in multiples of the width 20).
// With round or square caps each zero-length dash should draw a dot of the cap shape.
IPath square = new RectanglePolygon(40, 40, 120, 120);
float[] pattern = [0F, 2F];
IPath dashes = square.GenerateDashes(20, pattern);
Console.WriteLine($"GenerateDashes: {dashes.Flatten().Count()} subpaths");
foreach (LineCap cap in new[] { LineCap.Round, LineCap.Square })
{
PatternPen pen = new(new PenOptions(Color.Black, 20, pattern) { StrokeOptions = new StrokeOptions { LineCap = cap } });
using Image<Rgba32> drawn = Render(canvas => canvas.Draw(pen, square));
using Image<Rgba32> filled = Render(canvas => canvas.Fill(pen, square));
Console.WriteLine($"{cap,-6} caps: Draw covers {Covered(drawn)} pixels, Fill(Pen, IPath) covers {Covered(filled)} pixels");
drawn.SaveAsPng($"zero-dash-{cap}.png");
}
// For comparison: the stroker draws a dot for a zero-length subpath with a round cap.
SolidPen roundPen = new(new PenOptions(Color.Black, 20, null) { StrokeOptions = new StrokeOptions { LineCap = LineCap.Round } });
using Image<Rgba32> dot = Render(canvas => canvas.Draw(roundPen, new Path(new LinearLineSegment(new PointF(100, 100), new PointF(100, 100)))));
Console.WriteLine($"zero-length subpath, round cap: Draw covers {Covered(dot)} pixels");
static int Covered(Image<Rgba32> image)
{
int count = 0;
image.ProcessPixelRows(rows =>
{
for (int y = 0; y < rows.Height; y++)
{
foreach (Rgba32 p in rows.GetRowSpan(y))
{
count += p.R < 255 ? 1 : 0;
}
}
});
return count;
}
static Image<Rgba32> Render(CanvasAction draw)
{
Image<Rgba32> image = new(200, 200, Color.White.ToPixel<Rgba32>());
image.Mutate(c => c.Paint(draw));
return image;
}Run with dotnet run -c Release. A Debug build of main gives the same output and byte-identical PNGs.
Observed output:
GenerateDashes: 0 subpaths
Round caps: Draw covers 0 pixels, Fill(Pen, IPath) covers 0 pixels
Square caps: Draw covers 0 pixels, Fill(Pen, IPath) covers 0 pixels
zero-length subpath, round cap: Draw covers 338 pixels
The images zero-dash-Round.png and zero-dash-Square.png are white.
Expected: 12 dots along the square (perimeter 480, one dot every 40 pixels): circles of diameter 20 with round caps (about 12 x 330 pixels) and squares of 20 x 20 with square caps. The reference image of the resvg test painting/stroke-dasharray/0-n-with-round-caps.svg (the same square, stroke-dasharray="0 40", round caps) shows these 12 dots; the SVG specification asks for them. Other graphics models (PDF, the HTML canvas) are assumed to do the same; that was not checked. With butt caps nothing is drawn, as now.
Images
Aren't these the best images you have seen in a bug report?

Activity