Repository navigation
Description
Note
Fixed by #442. With that PR the program below parses all four inputs to the points (10, 0) and (20, 0).
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
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
The number grammar of SVG path data allows an exponent (1e1, 2E+1, 100e-1). Path.TryParseSvgPath returns false for any path data that contains such a number. The number scanner ends a number at the first letter, and e is a letter, so the exponent is read as an unknown command.
Explanation
Path.TryReadScalar (Path.cs, line 675) scans the length of a number and then parses it with float.TryParse. It ends the number at:
- a separator,
- a second decimal point,
- a
+or-that does not followeorE(lines 701 to 710; this rule exists to keep the sign of an exponent in the number), - any letter:
else if (char.IsLetter(ch))(line 712).
The last rule runs for e and E too, so in 1e1 the number ends before e. The parser reads 1, then finds e where it expects a command or a number, and TryParseSvgPath returns false. The sign rule for exponents is never reached for a sign that follows e, because the scanner has already stopped at e.
SVG path data from many editors and tools contains exponents for very small or very large values (for example 1e-5).
Suggested fix
In TryReadScalar, accept e or E as part of the number when it follows at least one digit of the mantissa and is followed by a digit, or by + or - and a digit; accept at most one exponent; then continue with the digits of the exponent. Any other letter still ends the number. Keep the existing rules for separators, a second decimal point (0.5.6 is two numbers), and a sign that starts a new number (10-20). A decimal point after the exponent ends the number (1e1.5 is 1e1 and .5).
float.TryParse with NumberStyles.Float already accepts the exponent, so only the scanner needs the change. Non-finite results (1e999) stay rejected by TryParseFloat, so the existing test TryParseSvgPath_ReturnsFalseForMalformedPathData("M 10 10 L 1e999 20") keeps passing.
#442 (the SVG work) fixes this: TryFindScaler uses the number scanner of the SVG code (SvgNumberParser.TryReadNumber, in src/ImageSharp.Drawing/Svg/Parsing/), and PathTests.TryParseSvgPath_ReadsNumbersWithExponents covers M 1e1 0 L 2E1 1e-0, M1e1,0L2e+1,1, and M 100e-1 0 h 10 v 1. Without the SVG code, a change inside TryReadScalar as above is enough.
Notes
- Found when the SVG work started to use
TryParseSvgPathfor thedattribute. - Only the scanner of path data was checked. Other parsers of the library were not checked for the same rule.
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;
// Numbers with an exponent are valid in SVG path data ("1e1" is 10).
foreach (string data in new[] { "M 10 0 L 20 0", "M 1e1 0 L 20 0", "M1E1,0 L2e+1,0", "M 100e-1 0 L 20 0" })
{
bool parsed = Path.TryParseSvgPath(data, out IPath? path);
string points = parsed ? string.Join(" ", path!.Flatten().SelectMany(p => p.Points.ToArray()).Select(p => $"({p.X}, {p.Y})")) : "-";
Console.WriteLine($"{data,-20} parsed: {parsed,-5} points: {points}");
}Run with dotnet run -c Release. A Debug build gives the same output.
Observed output:
M 10 0 L 20 0 parsed: True points: (10, 0) (20, 0)
M 1e1 0 L 20 0 parsed: False points: -
M1E1,0 L2e+1,0 parsed: False points: -
M 100e-1 0 L 20 0 parsed: False points: -
Expected: all four inputs parse to the points (10, 0) and (20, 0).
Images
No response
Activity