Render: a gradient mesh past any buffer draws without flattening its outline

A damaged .ai can give a gradient mesh whose outline is enormous but finite.
draw_mesh_raster flattened that whole outline in device pixels (grown_outline),
and kurbo's flatten then asked for more segments than memory holds: a
"capacity overflow" panic (seen in import_fuzz's ai_written_meshes_never_panic).
An outline wider or taller than 2^24 device pixels now goes to the per-quad
drawing path the function already falls back to.
This commit is contained in:
bflatastic
2026-11-18 01:41:52 +07:00
parent 36b5a5920c
commit 82c9631b57
2 changed files with 26 additions and 1 deletions
+10 -1
View File
@@ -249,6 +249,10 @@ fn mesh_level(m: &GradientMesh, bounds_px: f64) -> usize {
/// of pixels per mesh and frame.
const MESH_RASTER_MAX_PIXELS: usize = 4 << 20;
/// The widest and tallest a gradient mesh's outline is in device pixels for [`draw_mesh_raster`]
/// to flatten it (about 16.7 million pixels, far past any buffer it fills).
const MESH_OUTLINE_MAX_EXTENT: f64 = (1u32 << 24) as f64;
/// Draw a tessellated gradient mesh as one image: its quads are filled into a pixel buffer of the
/// mesh's on-screen box by scanline (each pixel takes the quad its centre is in, so neighbouring
/// quads meet without seams or overlap, and is shaded between the quad's corner colours, so the
@@ -262,7 +266,12 @@ const MESH_RASTER_MAX_PIXELS: usize = 4 << 20;
fn draw_mesh_raster(ctx: &mut RenderContext, f: &Frame, m: &GradientMesh, quads: &[MeshQuad], alpha: f32) -> bool {
let outline = m.outline().to_bezpath();
let dev = f.view.transform_rect_bbox(vectorcraft_geom::Shape::bounding_box(&outline));
if ![dev.x0, dev.y0, dev.x1, dev.y1].iter().all(|v| v.is_finite()) {
// The outline is flattened whole in device pixels ([`grown_outline`]): one far past any
// buffer would make more segments than memory holds, so the caller draws the quads.
if ![dev.x0, dev.y0, dev.x1, dev.y1].iter().all(|v| v.is_finite())
|| dev.width() > MESH_OUTLINE_MAX_EXTENT
|| dev.height() > MESH_OUTLINE_MAX_EXTENT
{
return false;
}
let (cw, ch) = (f64::from(ctx.width()), f64::from(ctx.height()));
+16
View File
@@ -463,3 +463,19 @@ fn backdrop_drag_perf() {
);
}
}
/// A gradient mesh far larger than any buffer (a damaged file can give one) is drawn without
/// flattening its whole outline in device pixels, which would make more segments than memory holds.
#[test]
fn a_mesh_past_any_buffer_draws_without_flattening_its_outline() {
let side = 1e30;
let mut d = Document::new(595.0, 842.0);
let m = vectorcraft_doc::GradientMesh::from_surface(2, 2, &|u, v| vectorcraft_geom::Point::new(u * side, v * side + u * u * side), &|u, v| {
Color::rgb(u as f32, v as f32, 0.5)
});
let layer = d.layers[0].id;
let node = Node::new(d.alloc_id(), NodeKind::Mesh(m));
d.insert(Some(layer), 0, node).unwrap();
let image = Renderer::new().render(&d, 640, 900, view(1.0, (640, 900)), &RenderOptions::default());
assert_eq!(image.pixels.len(), 640 * 900 * 4);
}