Merge pull request #1134 from pick2510/fix/3d-in-scaled-groups

This commit is contained in:
Brandon Thomas
2026-10-23 17:10:42 -04:00
3 changed files with 101 additions and 2 deletions
+24 -2
View File
@@ -843,6 +843,15 @@ impl Renderer {
if depth > 0 && view.determinant().abs() > 1e-12 && view.is_finite() { (view, view.inverse() * m) } else { (m, Affine::IDENTITY) }
}
/// Shape-space units per point: a group's child coordinates can scale a shape's box (a legacy
/// 1360-unit child space makes one unit hundreds of EMUs), but 3-D sizes kept in points
/// (contour width, extrusion depth, bevel) stay points, as PowerPoint draws them. 1 outside a
/// group.
fn per_point(&self, m: Affine, depth: usize) -> f64 {
let k = scale_of(self.line_space(m, depth).1);
if k.is_finite() && k > 1e-12 { 1.0 / k } else { 1.0 }
}
/// The size and transform a shape's text is set in: inside a group whose child coordinates
/// scale the shape, the shape's size in slide points and `m` with that scale taken out, so
/// insets and font sizes stay in points; otherwise `w`, `h` and `m` themselves.
@@ -1049,6 +1058,17 @@ impl Renderer {
ctx.push_layer(None, None, None, None, None);
let _ = r;
}
// Shape-space units per point for the 3-D sizes, measured without the camera's turn (which
// `shape_at` puts in `m`): its foreshortening is not a child-coordinate scale.
let pt = {
let turn = effects
.as_ref()
.and_then(|e| e.raw3d.as_deref())
.and_then(camera::from_raw)
.and_then(|c| camera::affine(&c, w, h))
.filter(|t| t.determinant().abs() > 1e-9);
self.per_point(turn.map_or(m, |t| m * t.inverse()), depth)
};
// A 3-D extrusion under a turned camera: the sides, swept behind the face.
if let Some(raw) = effects.as_ref().and_then(|e| e.raw3d.as_deref())
&& let Some(ex) = extrude::from_raw(raw)
@@ -1062,7 +1082,8 @@ impl Renderer {
};
let pre = m * turn.inverse();
let lit = light::Lighting::from_raw(raw);
extrude::draw(ctx, &outline, w, h, &cam, ex.depth, pre, turn, extrude::side_color(rctx, &ex, face), &lit);
let d = ex.depth * pt;
extrude::draw(ctx, &outline, w, h, &cam, d, pre, turn, extrude::side_color(rctx, &ex, face), &lit);
}
// A 3-D contour: a band around the outline, behind the face (and in front of any sides).
if let Some(raw) = effects.as_ref().and_then(|e| e.raw3d.as_deref())
@@ -1073,7 +1094,7 @@ impl Renderer {
_ => None,
};
let col = c.color.as_ref().map(|k| rctx.color(k, face)).or(face).unwrap_or(Rgba::rgb(0xff, 0xff, 0xff));
extrude::draw_contour(ctx, &outline, m, c.width, col, &light::Lighting::from_raw(raw));
extrude::draw_contour(ctx, &outline, m, c.width * pt, col, &light::Lighting::from_raw(raw));
}
// Fill.
match &s.kind {
@@ -1168,6 +1189,7 @@ impl Renderer {
};
// A 3-D bevel: the face and a band inside the outline lit as surfaces.
if let Some(bv) = effects.as_ref().and_then(|e| e.raw3d.as_deref()).and_then(bevel::from_raw) {
let bv = bevel::Bevel { w: bv.w * pt, h: bv.h * pt, ..bv };
let solid = match &fill {
Some(Fill::Solid { color: c }) => Some(rctx.color(c, fill_ph)),
_ => None,
+75
View File
@@ -2281,6 +2281,81 @@ fn extrusions_show_their_sides_under_a_turned_camera() {
assert_eq!(ink(&flat, 0, 960), 0);
}
#[test]
fn group_child_scale_does_not_scale_3d_depth_contours_or_bevels() {
// A legacy group child space (here 100 points per unit) scales the shapes' boxes, but
// extrusion depths, contour widths and bevels are points: a 1 pt contour on a small extruded
// cube in such a group was drawn hundreds of points wide, a band across the whole slide.
let mut deck = extruded_deck();
let bevelled = Shape {
id: ShapeId(704),
xfrm: Some(Xfrm::new(40.0, 340.0, 170.0, 130.0)),
geom: Geom::preset("rect"),
style: Some(ShapeStyle::accent(deckcraft_color::SchemeSlot::Accent1)),
effects: Some(deckcraft_model::Effects {
raw3d: Some(r#"<a:scene3d><a:camera prst="orthographicFront"/><a:lightRig rig="threePt" dir="t"/></a:scene3d><a:sp3d><a:bevelT w="254000" h="254000"/></a:sp3d>"#.into()),
..Default::default()
}),
..Default::default()
};
std::sync::Arc::make_mut(&mut deck.slides[0]).shapes.push(bevelled);
let shapes = deck.slides[0].shapes.clone();
let grouped = |k: f64| {
let children = shapes
.iter()
.map(|s| {
let x = s.xfrm.unwrap_or_default();
Shape { xfrm: Some(Xfrm::new(x.x / k, x.y / k, x.w / k, x.h / k)), ..s.clone() }
})
.collect();
let group = Shape {
id: ShapeId(10),
xfrm: Some(Xfrm::new(0.0, 0.0, 960.0, 540.0)),
kind: ShapeKind::Group { children, child: Xfrm::new(0.0, 0.0, 960.0 / k, 540.0 / k) },
..Default::default()
};
render_slide(&deck_with(vec![group]), 0, &RenderOpts { scale: 1.0, ..Default::default() })
};
let reference = grouped(1.0);
let ink = |img: &Image| img.pixels.as_chunks::<4>().0.iter().filter(|p| **p != [255, 255, 255, 255]).count();
let drawn = ink(&reference);
assert!(drawn > 20_000, "the reference draws the shapes: {drawn}");
let scaled = grouped(100.0);
let (n, d) = (ink(&scaled), differing(&reference, &scaled));
assert!(n.abs_diff(drawn) * 20 < drawn, "{n} inked pixels against {drawn}");
assert!(d * 20 < drawn, "{d} differing values against {drawn} inked pixels");
}
#[test]
fn a_turned_camera_in_a_one_to_one_group_draws_its_3d_sizes_as_outside_one() {
// The camera's turn is part of the shape's transform; its foreshortening is not a group scale,
// so a 1:1 group must not change extrusion depths, contours or bevels under a turned camera.
let mut deck = extruded_deck();
let bevelled = Shape {
id: ShapeId(705),
xfrm: Some(Xfrm::new(40.0, 340.0, 170.0, 130.0)),
geom: Geom::preset("rect"),
style: Some(ShapeStyle::accent(deckcraft_color::SchemeSlot::Accent1)),
effects: Some(deckcraft_model::Effects {
raw3d: Some(r#"<a:scene3d><a:camera prst="isometricOffAxis1Right"/><a:lightRig rig="threePt" dir="t"/></a:scene3d><a:sp3d extrusionH="254000" contourW="38100"><a:bevelT w="254000" h="254000"/></a:sp3d>"#.into()),
..Default::default()
}),
..Default::default()
};
std::sync::Arc::make_mut(&mut deck.slides[0]).shapes.push(bevelled);
let opts = RenderOpts { scale: 1.0, ..Default::default() };
let alone = render_slide(&deck, 0, &opts);
let group = Shape {
id: ShapeId(10),
xfrm: Some(Xfrm::new(0.0, 0.0, 960.0, 540.0)),
kind: ShapeKind::Group { children: deck.slides[0].shapes.clone(), child: Xfrm::new(0.0, 0.0, 960.0, 540.0) },
..Default::default()
};
let grouped = render_slide(&deck_with(vec![group]), 0, &opts);
let d = differing(&alone, &grouped);
assert!(d < 200, "{d} values differ between the shapes alone and in a 1:1 group");
}
#[test]
fn table_rows_below_the_slide_are_not_laid_out_and_the_slide_looks_the_same() {
// A fuzzed deck's 200 x 200 table of long text took 70 s to draw, most of it off the slide.