fix(simulator): stop sub-pixel arcs crashing the cut trail paint

GDI+ throws "Out of memory" stroking a figure that is only an arc a few
hundredths of a pixel long with a round-join or dashed pen. Small etched
text zoomed out has such arcs, and playback adds them to the trail one at
a time, so the paint threw and took the app down.

An arc bulging under a tenth of a pixel from its chord now goes into the
path as the chord, and a path GDI+ still rejects is left undrawn instead
of failing the paint.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Fable 5.2 committed 2026-12-02 20:04:24 -05:00
1 parent 1839e6641b
commit 4cc43578f3
2 files changed
+72 -8

No files matched your search

@@ -89,6 +89,50 @@ public class CutSimulationOverlayTests
}
});
// Small etched text zoomed out: each arc is a few hundredths of a pixel long, and playback
// adds them to the trail one at a time. GDI+ throws "Out of memory" stroking such an arc
// on its own.
[Theory]
[InlineData(0.3f)]
[InlineData(1f)]
[InlineData(3f)]
public void SubPixelArcsDrawnOneAtATimeDoNotFailThePaint(float scale) => RunSta(() =>
{
var plate = new Plate(48, 96);
plate.Parts.Add(new Part(new Drawing("dot", TinyArcs(0.03)), new Vector(5, 5)));
plate.Parts.Add(new Part(new Drawing("wide", TinyArcs(1)), new Vector(8, 5)));
using var view = new PlateView { Plate = plate };
view.ZoomToPoint(new Vector(0, 0), scale / view.ViewScale, false);
var sim = CutSimulation.Build(plate, Parameters);
view.CutSimulation.Load(sim);
Assert.Contains(sim.Steps, s => s.IsArc);
using var bitmap = new Bitmap(400, 400);
using var graphics = Graphics.FromImage(bitmap);
graphics.TranslateTransform(20.4f, 380.2f);
foreach (var step in sim.Steps)
{
view.CutSimulation.Time = step.StartTime + step.Duration;
view.CutSimulation.Draw(graphics);
}
});
// A closed loop of arcs of mixed sweeps, 1 to 20 degrees, on one circle.
private static Program TinyArcs(double radius)
{
var program = new Program();
program.MoveTo(radius, 0);
var angle = 0.0;
var sweeps = new[] { 1.0, 3.0, 20.0 };
for (var i = 0; angle < 360; i++)
{
angle = System.Math.Min(360, angle + sweeps[i % sweeps.Length]);
var radians = angle * System.Math.PI / 180;
program.ArcTo(radius * System.Math.Cos(radians), radius * System.Math.Sin(radians), 0, 0, RotationType.CCW);
}
return program;
}
[Fact]
public void RetimingKeepsTheTorchWhereItWas() => RunSta(() =>
{
+28 -8
View File
@@ -521,15 +521,26 @@ internal sealed class CutSimulationOverlay : IDisposable
}
graphics.SmoothingMode = smoothing;
if (travel.PointCount > 0)
graphics.DrawPath(pens.Rapid, travel);
if (cuts.PointCount > 0)
Stroke(graphics, pens.Rapid, travel);
Stroke(graphics, pens.Glow, cuts);
Stroke(graphics, pens.Cut, cuts);
Stroke(graphics, pens.Scribe, scribes);
}
// GDI+ reports a figure it cannot stroke as "Out of memory". AddStep keeps out the one kind
// known to do that; anything else it rejects is left undrawn rather than failing the paint.
private static void Stroke(Graphics graphics, Pen pen, GraphicsPath path)
{
if (path.PointCount == 0)
return;
try
{
graphics.DrawPath(pens.Glow, cuts);
graphics.DrawPath(pens.Cut, cuts);
graphics.DrawPath(pen, path);
}
catch (Exception e) when (e is OutOfMemoryException or ArgumentException or OverflowException)
{
Debug.WriteLine($"Cut simulation trail not drawn: {e.Message}");
}
if (scribes.PointCount > 0)
graphics.DrawPath(pens.Scribe, scribes);
}
// A dashed pen's cost grows with the length it strokes, so a rapid is trimmed to the clip:
@@ -545,6 +556,15 @@ internal sealed class CutSimulationOverlay : IDisposable
pen.DashOffset = 0;
}
// An arc bulging less than this many pixels from its chord is drawn as the chord. It looks the
// same, and GDI+ fails ("Out of memory") stroking a figure that is only an arc a few
// hundredths of a pixel long: small text zoomed out, drawn a step at a time during playback.
internal const float MinArcSag = 0.1f;
// How far the arc bulges from its chord; always less than its length.
private static double ArcSag(float radius, double sweep) =>
radius * (1 - System.Math.Cos(System.Math.Min(System.Math.Abs(sweep), System.Math.Tau) / 2));
// Consecutive moves join into one figure; arcs go in as GDI+ arcs, which flatten to the size
// they are drawn at.
private void AddStep(GraphicsPath path, CutSimulationStep step, int index, ref int last)
@@ -557,7 +577,7 @@ internal sealed class CutSimulationOverlay : IDisposable
{
var center = view.PointWorldToGraph(step.Center);
var radius = view.LengthWorldToGui(step.Radius);
if (radius > 0.01f)
if (ArcSag(radius, step.Sweep) >= MinArcSag)
{
// Graph y points down, so world angles flip sign.
path.AddArc(center.X - radius, center.Y - radius, radius * 2, radius * 2,