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);