fix(reports): draw arcs as Bezier curves to stop an out-of-memory crash

PDFsharp 6.2.4's XGraphicsPath.AddArc loops until memory runs out on some
arcs ending on a quadrant boundary within rounding, which nested part
geometry produces. Nest reports and part printouts now build arcs from
Bezier curves themselves.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Sonnet 5.5 committed 2026-11-28 14:40:14 -05:00
1 parent 17e6e65e1c
commit bf9b0df816
2 files changed
+56 -5

No files matched your search

+25 -5
View File
@@ -765,15 +765,35 @@ public static class NestPdfWriter
path.AddLine(fit.Point(segment.Start), fit.Point(segment.End));
continue;
}
var center = fit.Point(segment.Center);
var radius = segment.Radius * fit.Scale;
var box = new XRect(center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
// Page Y points down, so model angles and sweeps change sign.
path.AddArc(box, -Degrees(segment.StartAngle), -Degrees(segment.SweepAngle));
AddArc(path, fit.Point(segment.Center), segment.Radius * fit.Scale,
-segment.StartAngle, -segment.SweepAngle);
}
}
private static double Degrees(double radians) => radians * 180 / System.Math.PI;
/// <summary>
/// Adds an arc as Bezier curves of at most a quarter turn each. Never use PDFsharp's AddArc:
/// in 6.2.4 some arcs ending on a quadrant boundary, within rounding, loop until memory
/// runs out. Angles are in page radians.
/// </summary>
private static void AddArc(XGraphicsPath path, XPoint center, double radius, double start, double sweep)
{
// The margin keeps a quarter turn that is a hair long from becoming two curves.
var count = System.Math.Max(1, (int)System.Math.Ceiling(System.Math.Abs(sweep) / (System.Math.PI / 2) - 1e-9));
var step = sweep / count;
var handle = 4.0 / 3 * System.Math.Tan(step / 4) * radius;
for (var index = 0; index < count; index++)
{
var (sin0, cos0) = System.Math.SinCos(start + index * step);
var (sin1, cos1) = System.Math.SinCos(start + (index + 1) * step);
var from = new XPoint(center.X + radius * cos0, center.Y + radius * sin0);
var to = new XPoint(center.X + radius * cos1, center.Y + radius * sin1);
path.AddBezier(from,
new XPoint(from.X - handle * sin0, from.Y + handle * cos0),
new XPoint(to.X + handle * sin1, to.Y - handle * cos1),
to);
}
}
private static XFont BodyFont(bool bold) =>
new(ReportFonts.Family, BodyFontSize, bold ? XFontStyleEx.Bold : XFontStyleEx.Regular);
@@ -0,0 +1,31 @@
using System.Collections.Immutable;
using OpenNest.Reporting;
using PdfSharp.Drawing;
namespace OpenNest.Tests.Reporting;
public sealed class NestPdfWriterArcTests
{
// Start and sweep, in model degrees, of arcs ending on a quadrant boundary within rounding.
// PDFsharp's own AddArc runs out of memory on these.
[Theory]
[InlineData(180.00000000000003, 180)]
[InlineData(270.0000000000001, 89.9999999999999)]
[InlineData(359.999, 0.001)]
[InlineData(360, 1e-14)]
[InlineData(0, 360)]
[InlineData(90, -360)]
public void AddContour_DrawsArcsEndingOnAQuadrantBoundary(double start, double sweep)
{
var from = start * System.Math.PI / 180;
var span = sweep * System.Math.PI / 180;
ReportPoint At(double angle) => new(10 * System.Math.Cos(angle), 10 * System.Math.Sin(angle));
var contour = new ReportContour(ImmutableArray.Create(
new ReportSegment(At(from), At(from + span), new ReportPoint(0, 0), 10, from, span)), false);
var fit = Fit.Create(new ReportBounds(-10, -10, 10, 10), new XRect(0, 0, 100, 100));
var exception = Record.Exception(() => NestPdfWriter.AddContour(new XGraphicsPath(), contour, fit));
Assert.Null(exception);
}
}