diff --git a/crates/ppt-binary/README.md b/crates/ppt-binary/README.md index 069a5bdb0..43bca4c0a 100644 --- a/crates/ppt-binary/README.md +++ b/crates/ppt-binary/README.md @@ -68,10 +68,13 @@ counter-clockwise, DrawingML clockwise, so each is negated. A shape's geometry h as drawn, quarter-turns and mirror included, so its angle and flips come from there (a box turned by an odd number of quarter-turns is turned back); the rectangle, rounded rectangle and ellipse take the angle the user set. A shape that draws nothing but text is a text box; a shape's text is -read as a text box's is, its text rectangle measured from the edges of the shape's unturned box (a -balloon's, its body). PowerPoint 95 keeps the text of a shape turned before it was typed upright; -the text's own angle isn't read yet, so that text turns with its shape. Freeforms read as custom -paths through their points, in their box, with +read as a text box's is. PowerPoint 95 lays it out in the box the geometry holds, turned by the +text's own angle, so the text of a shape turned before it was typed stays upright: such text is +`upright`, at its own angle, with its insets on that box's edges (a balloon's measured from its +body). A preset with a text rectangle of its own, such as an arrow's shaft, applies the insets +inside that rectangle, where PowerPoint 95 measures them from the whole box, so such text can sit +off from where PowerPoint 95 puts it. Freeforms read as custom paths through their points, in +their box, with their format's fill, line and shadow, and open ones with their arrowheads. Groups read as groups of their members, which keep their places on the slide (PowerPoint 95 can't turn or flip a group), nested up to 32 deep. Pictures and notes come later. diff --git a/crates/ppt-binary/src/ppt95.rs b/crates/ppt-binary/src/ppt95.rs index 18a0efe8d..537c0f878 100644 --- a/crates/ppt-binary/src/ppt95.rs +++ b/crates/ppt-binary/src/ppt95.rs @@ -448,21 +448,29 @@ fn object(r: &Rec, env: &Env, scheme: &Scheme, size: Size) -> Option { }); let container = r.child(rt::TEXT); let mut text = container.and_then(|c| text::text_body(&c, env, scheme, Some(sides), ph.is_none())); - // The text rectangle is measured from the edges of the box as the geometry holds it; the - // shape's are those of the unturned box. - if let Some(t) = text.as_mut() { - unturn_insets(&mut t.body, shape::quarter_turns(&xfrm, drawn)); + // PowerPoint 95 lays a shape's text out in the box the geometry holds, turned by the text's own + // angle (4003 +16, the geometry's own angle in every probe): quarter-turns folded into that box + // don't turn the text. When the shape's box was turned back, the text is upright on the slide, + // at that angle, with its insets on the held box's edges by name, as PowerPoint lays out + // `upright` text (checked in PowerPoint 16 against PowerPoint 95's renderings). + let quarters = shape::quarter_turns(&xfrm, drawn); + if let Some(t) = text.as_mut().filter(|_| quarters != 0) { + let angle = container.and_then(|c| text::angle(&c)).unwrap_or(drawn); + t.body.upright = Some(true); + t.body.rot = Some(shape::clockwise(i64::from(angle))).filter(|r| *r != 0.0); } let ruler = container.and_then(|c| text::ruler_slot(&c)); let (mut geom, mut look) = (None, Look::none()); if !placeholder { let height = xfrm.h; geom = code.map(|c| shape::geometry(c, g.i32(4), &mut xfrm)); - // A balloon's box is its body, above its tail: the text's bottom edge stays where it was. - if let Some(b) = text.as_mut().map(|t| &mut t.body) - && let Some(inset) = b.inset_b - { - b.inset_b = Some((inset - (height - xfrm.h)).max(0.0)); + // A balloon's box is its body, above its tail: the text's edge there stays where it was. The + // cut is the shape's bottom, which is the held box's edge `quarters` on from the bottom. + if let Some(b) = text.as_mut().map(|t| &mut t.body).filter(|_| height != xfrm.h) { + let inset = [&mut b.inset_l, &mut b.inset_t, &mut b.inset_r, &mut b.inset_b].into_iter().nth((3 + quarters) % 4); + if let Some(inset) = inset { + *inset = inset.map(|v| (v - (height - xfrm.h)).max(0.0)); + } } if let Some(f) = format { look = shape::look(&f, scheme); @@ -471,15 +479,6 @@ fn object(r: &Rec, env: &Env, scheme: &Scheme, size: Size) -> Option { Some(Object { placeholder, ph, xfrm, text, ruler, geom, look, kind: Kind::Shape }) } -/// Insets measured from the edges of a box `quarters` quarter-turns clockwise from the shape's -/// unturned one, as insets from the unturned box's edges. Left, top, right and bottom are in -/// clockwise order, so the unturned box's edge `i` is the turned box's edge `i + quarters`. -fn unturn_insets(b: &mut craft_pptx::text::BodyProps, quarters: usize) { - let turned = [b.inset_l, b.inset_t, b.inset_r, b.inset_b]; - let edge = |i: usize| turned[(i + quarters) % 4]; - [b.inset_l, b.inset_t, b.inset_r, b.inset_b] = [edge(0), edge(1), edge(2), edge(3)]; -} - /// An object as a shape: a placeholder of a known type; a text box when it draws nothing but its /// text; else a shape or line that draws something. One that draws nothing is left out. A /// placeholder of an unknown type keeps its text as a text box. diff --git a/crates/ppt-binary/src/ppt95/shape.rs b/crates/ppt-binary/src/ppt95/shape.rs index f64efff32..b2ef866df 100644 --- a/crates/ppt-binary/src/ppt95/shape.rs +++ b/crates/ppt-binary/src/ppt95/shape.rs @@ -155,7 +155,7 @@ pub(super) fn quarter_turns(xfrm: &Xfrm, drawn: i32) -> usize { } /// A counter-clockwise angle in sixteenths of a degree as DrawingML's clockwise degrees, 0 to 360. -fn clockwise(ccw: i64) -> f64 { +pub(super) fn clockwise(ccw: i64) -> f64 { ((-ccw).rem_euclid(TURN) as f64) / 16.0 } diff --git a/crates/ppt-binary/src/ppt95/tests.rs b/crates/ppt-binary/src/ppt95/tests.rs index f0870a3ac..891340064 100644 --- a/crates/ppt-binary/src/ppt95/tests.rs +++ b/crates/ppt-binary/src/ppt95/tests.rs @@ -1601,42 +1601,65 @@ fn hostile_angles_never_panic() { /// The text of a drawn shape and of a freeform keeps its anchoring, wrap, spacing and justified /// alignment, as a text box's does. -/// The text rectangle is measured from the edges of the box the geometry holds. A shape turned a -/// quarter before its text was typed holds its box turned (as `rot90thentext` and -/// `rot90arrowtext` showed), so its insets move to the unturned box's edges; a balloon's box is its -/// body, so its text's bottom inset is measured from the body's bottom (as `balloontext` showed). +/// PowerPoint 95 lays a shape's text out in the box the geometry holds, turned by the text's own +/// angle (4003 +16): a shape turned a quarter before its text was typed holds its box turned and +/// draws its text upright (`wrap-before`, `rot90arrowtext`), one turned on to 120° after that draws +/// it at the 30° its geometry holds (`wrap-mix`), and one turned after its text was typed turns +/// its text with it (`wrap-after`). The insets stay on the held box's edges, as PowerPoint lays +/// out `upright` text. A balloon's box is its body, so the inset at its bottom is measured from +/// the body (`balloontext`). #[test] -fn text_insets_follow_the_unturned_box() { - // A text rectangle `insets` (left, top, right, bottom, in master units) inside `bx`. - let text_in = |[l, t, r, b]: [i32; 4], [il, it, ir, ib]: [i32; 4]| { - text(b"Hi", &[plain(3)], &[para_run(3, false, 0, 0, 0)], 1, [l + il, t + it, r - ir, b - ib], BODY) +fn text_in_a_turned_box_keeps_its_own_angle() { + // A text rectangle `insets` (left, top, right, bottom, in master units) inside `bx`, drawn at + // `angle` (sixteenths of a degree, counter-clockwise). + let text_in = |[l, t, r, b]: [i32; 4], [il, it, ir, ib]: [i32; 4], angle: i32| { + let mut t = text(b"Hi", &[plain(3)], &[para_run(3, false, 0, 0, 0)], 1, [l + il, t + it, r - ir, b - ib], BODY); + let at = t.windows(4).position(|w| w == 4003u32.to_le_bytes()).unwrap() + 16 + 16; + t[at..at + 4].copy_from_slice(&angle.to_le_bytes()); + t }; - let shape = |code: i32, bx: [i32; 4], turn: i32, insets: [i32; 4]| { - container(rt::SHAPE, 0x13, &[coded_geometry(code as u32, 1800, bx, 0), format(Format { turn, ..PLAIN }), text_in(bx, insets)]) + let shape = |code: i32, bx: [i32; 4], drawn: i32, turn: i32, insets: [i32; 4]| { + container(rt::SHAPE, 0x13, &[coded_geometry(code as u32, 1800, bx, drawn), format(Format { turn, ..PLAIN }), text_in(bx, insets, drawn)]) }; - let tall = [-288, -576, 288, 576]; + let (tall, wide) = ([-288, -576, 288, 576], [-576, -288, 576, 288]); let p = deck_of( vec![ - // A rectangle turned a quarter counter-clockwise, its box held turned. - shape(-3, tall, 1440, [66, 37, 66, 37]), - // An up arrow: a right arrow turned a quarter, its head at the turned box's top. - shape(9 | 0x1000, tall, 0, [209, 329, 208, 37]), - // Unturned: the insets stay where they are. - shape(-3, [-576, -288, 576, 288], 0, [66, 37, 66, 37]), + // A rectangle turned a quarter counter-clockwise before its text: its box held turned. + shape(-3, tall, 0, 1440, [66, 37, 66, 37]), + // The same turned on to 120°: the geometry holds the 30° left over. + shape(-3, tall, 480, 1920, [66, 37, 66, 37]), + // An up arrow: a right arrow turned a quarter, its head at the held box's top. + shape(9 | 0x1000, tall, 0, 0, [209, 329, 208, 37]), + // Turned after its text: the geometry holds the box unturned and the whole angle. + shape(-3, wide, 1440, 1440, [66, 37, 66, 37]), // A balloon in a 144 pt square: its body is the top 120 pt. - shape(13, [-576, -576, 576, 576], 0, [121, 91, 121, 307]), + shape(13, [-576, -576, 576, 576], 0, 0, [121, 91, 121, 307]), ], &[], ); - let insets: Vec<_> = p.slides[0] + let got: Vec<_> = p.slides[0] .shapes .iter() .map(|s| { let b = &body_of(s).body; - (b.inset_l.unwrap(), b.inset_t.unwrap(), b.inset_r.unwrap(), b.inset_b.unwrap()) + (s.xfrm.unwrap().rot, b.upright, b.rot, (b.inset_l.unwrap(), b.inset_t.unwrap(), b.inset_r.unwrap(), b.inset_b.unwrap())) }) .collect(); - assert_eq!(insets, [(4.625, 8.25, 4.625, 8.25), (4.625, 26.125, 41.125, 26.0), (8.25, 4.625, 8.25, 4.625), (15.125, 11.375, 15.125, 14.375)]); + assert_eq!( + got, + [ + (270.0, Some(true), None, (8.25, 4.625, 8.25, 4.625)), + (240.0, Some(true), Some(330.0), (8.25, 4.625, 8.25, 4.625)), + (270.0, Some(true), None, (26.125, 41.125, 26.0, 4.625)), + (270.0, None, None, (8.25, 4.625, 8.25, 4.625)), + (0.0, None, None, (15.125, 11.375, 15.125, 14.375)), + ] + ); + // A balloon turned a quarter counter-clockwise: its body is cut from the shape's bottom, the + // held box's right, where its tail is. + let p = deck_of(vec![shape(13 | 0x1000, [-576, -576, 576, 576], 0, 0, [91, 121, 307, 121])], &[]); + let b = &body_of(&p.slides[0].shapes[0]).body; + assert_eq!((b.upright, b.inset_l, b.inset_t, b.inset_r, b.inset_b), (Some(true), Some(11.375), Some(15.125), Some(14.375), Some(15.125))); } #[test] @@ -2019,8 +2042,8 @@ fn check_text_extras(name: &str, p: &Presentation) { } } -/// The text rectangles of the PowerPoint 95 samples with text in a turned shape or a balloon; false -/// for any other sample. +/// The text of the PowerPoint 95 samples with text in a turned shape or a balloon: its insets and +/// whether it is drawn upright; false for any other sample. fn check_text_frame_sample(name: &str, shapes: &[Shape]) -> bool { // AddShape(n, 1440, 1440, 2880, 1440) in twips. let wide = (72.0, 72.0, 144.0, 72.0); @@ -2028,23 +2051,33 @@ fn check_text_frame_sample(name: &str, shapes: &[Shape]) -> bool { let b = &body_of(s).body; (b.inset_l.unwrap(), b.inset_t.unwrap(), b.inset_r.unwrap(), b.inset_b.unwrap()) }; + let upright = |s: &Shape| (body_of(s).body.upright, body_of(s).body.rot); match name { // A rectangle with text, then the same turned 90° after its text was typed: PowerPoint 95 // keeps the box unturned and the text turns with it (4003 +16), insets 66 × 37 units. "recttext" | "rot90text" => { assert_eq!((boxed(&shapes[0]), insets(&shapes[0])), (wide, (8.25, 4.625, 8.25, 4.625)), "{name}"); } - // Turned 90° before its text was typed: the box and the text rectangle are held turned - // (and the text upright), so the insets move to the unturned box's edges, the turned - // box's left becoming the top. + // Turned 90° before its text was typed: the box and the text rectangle are held turned and + // the text is drawn upright, its insets on the held box's edges. "rot90thentext" => { let x = shapes[0].xfrm.unwrap(); assert_eq!((x.rot, x.w, x.h), (270.0, 144.0, 72.0)); - assert_eq!(insets(&shapes[0]), (4.625, 8.25, 4.625, 8.25)); + assert_eq!((upright(&shapes[0]), insets(&shapes[0])), ((Some(true), None), (8.25, 4.625, 8.25, 4.625))); } - // An up arrow with text: a right arrow turned a quarter, whose head (the turned box's top, - // 329 units in) is the unturned box's right. - "rot90arrowtext" => assert_eq!(insets(&shapes[0]), (4.625, 26.125, 41.125, 26.0)), + // An up arrow with text: a right arrow turned a quarter, its head at the held box's top + // (329 units in), its text upright. + "rot90arrowtext" | "wrap-arrow" => { + assert_eq!((upright(&shapes[0]), insets(&shapes[0])), ((Some(true), None), (26.125, 41.125, 26.0, 4.625)), "{name}"); + } + // Wrapping text (the Text Anchor dialog's word wrap): drawn upright in the held box when + // turned before it was typed, turned with the shape when turned after, and at the 30° the + // geometry holds when turned a quarter, typed, then turned on to 120° (`rotmix2` likewise). + // Turned 30°, typed, then on to 90° (`rotmix`), the geometry holds the box unturned and the + // whole angle, and the text turns with the shape. + "wrap-none" | "wrap-after" | "rotmix" | "rot30text" => assert_eq!(upright(&shapes[0]), (None, None), "{name}"), + "wrap-before" => assert_eq!(upright(&shapes[0]), (Some(true), None)), + "wrap-mix" | "rotmix2" => assert_eq!(upright(&shapes[0]), (Some(true), Some(330.0)), "{name}"), // A balloon's box is its body (the square's top 120 of 144 pt); its text's bottom edge, // 307 units above the square's, is 14.375 pt above the body's. "balloontext" => { diff --git a/crates/ppt-binary/src/ppt95/text.rs b/crates/ppt-binary/src/ppt95/text.rs index 3e31e8b71..4e79ac8ac 100644 --- a/crates/ppt-binary/src/ppt95/text.rs +++ b/crates/ppt-binary/src/ppt95/text.rs @@ -147,6 +147,13 @@ pub(super) fn master_style(env: &Env, scheme: &Scheme, ty: usize, ruler: Option< Some(ls) } +/// The angle a text container's text is drawn at (4003 +16), counter-clockwise in sixteenths of a +/// degree: the geometry's own angle in every probe, whatever quarter-turns its box holds. +pub(super) fn angle(container: &Rec) -> Option { + let body = container.children().find(|r| r.kind == rt::TEXT_BODY || r.kind == rt::MASTER_TEXT_BODY)?; + body.child(rt::TEXT_ATOM)?.i32(16) +} + /// The ruler slot a text container's body names. pub(super) fn ruler_slot(container: &Rec) -> Option { let body = container.children().find(|r| r.kind == rt::TEXT_BODY || r.kind == rt::MASTER_TEXT_BODY)?;