diff --git a/crates/ppt-binary/README.md b/crates/ppt-binary/README.md index 5972b2eb4..e6975d999 100644 --- a/crates/ppt-binary/README.md +++ b/crates/ppt-binary/README.md @@ -41,8 +41,9 @@ one feature per file, and the public [MS-PPT] numbering where the records keep i So far a PowerPoint 95 file imports with its slide size, its masters (each the caller's default master wearing the file's colour scheme, and its background when that isn't the scheme's), and its slides in order with their master, layout type and, when a slide doesn't follow the master's, -its own background: a solid colour, or none when the slide's background has no fill. The slides are empty: shapes, placeholders and their text, pictures and -notes come later. +its own background: a solid colour, or none when the slide's background has no fill. A slide also +says whether it shows the master's objects and whether it uses the master's colour scheme or its +own. The slides are empty: shapes, placeholders and their text, pictures and notes come later. The caller's `craft_pptx::Defaults` supplies the master each of the file's masters starts from: its layouts and placeholders. The file's masters then place and style the placeholders that diff --git a/crates/ppt-binary/src/ppt95.rs b/crates/ppt-binary/src/ppt95.rs index f44fd5002..36234cdeb 100644 --- a/crates/ppt-binary/src/ppt95.rs +++ b/crates/ppt-binary/src/ppt95.rs @@ -133,18 +133,25 @@ struct Page<'a> { master_id: u32, /// The [MS-PPT] `SlideLayoutType` number. layout: Option, + /// The slide box's flags (1005 +16, +17, +20): whether the slide shows the master's objects, + /// uses the master's colour scheme and uses the master's background. A missing box follows. + follow_objects: bool, + follow_scheme: bool, follow_background: bool, scheme: Option, } fn page(rec: Rec<'_>) -> Page<'_> { let atom = rec.child(rt::SLIDE_ATOM); + let flag = |at: usize| rec.child(rt::SLIDE_BOX).and_then(|b| b.u8(at)).is_none_or(|f| f != 0); Page { rec, id: atom.and_then(|a| a.u32(0)).unwrap_or(0), master_id: atom.and_then(|a| a.u32(4)).unwrap_or(0), layout: rec.child(rt::LAYOUT).and_then(|l| l.u32(0)), - follow_background: rec.child(rt::SLIDE_BOX).and_then(|b| b.u8(20)).is_none_or(|f| f != 0), + follow_objects: flag(16), + follow_scheme: flag(17), + follow_background: flag(20), scheme: rec.child(rt::SCHEME).and_then(|s| s.child(rt::COLOR_SCHEME)).map(|s| scheme_of(&s)), } } @@ -217,10 +224,12 @@ pub fn convert(document: &Rec, app: &dyn Defaults) -> Presentation { let Some((kinds, first)) = layouts.get(index) else { continue }; let kind: Option = s.layout.map(layout_kind); let Some(layout) = kind.and_then(|k| kinds.iter().find(|(lk, _)| *lk == k).map(|(_, id)| *id)).or(*first) else { continue }; - let scheme = s.scheme.unwrap_or(master_scheme); + // A slide following the master's scheme holds a copy of it; its own is used only when it + // doesn't follow. + let scheme = if s.follow_scheme { master_scheme } else { s.scheme.unwrap_or(master_scheme) }; let background = if s.follow_background { None } else { background(&s.rec, &scheme).map(|fill| Background::Fill { fill }) }; let Some(id) = p.alloc_id() else { break }; - p.slides.push(Arc::new(Slide { id: SlideId(id), layout, background, show_master_shapes: true, ..Default::default() })); + p.slides.push(Arc::new(Slide { id: SlideId(id), layout, background, show_master_shapes: s.follow_objects, ..Default::default() })); } p } diff --git a/crates/ppt-binary/src/ppt95/tests.rs b/crates/ppt-binary/src/ppt95/tests.rs index 530241648..e643a9f44 100644 --- a/crates/ppt-binary/src/ppt95/tests.rs +++ b/crates/ppt-binary/src/ppt95/tests.rs @@ -48,10 +48,11 @@ fn background_shape(fill: [u8; 4], filled: bool) -> Vec { container(rt::SHAPE, 0x1C, &[geometry, rec(rt::FORMAT, 1, 0, &format)]) } -/// The slide box: the slide rectangle, then whether it follows the master background. -fn slide_box(follow_background: bool) -> Vec { +/// The slide box: the slide rectangle, then whether it follows the master's objects, scheme and +/// background. +fn slide_box(objects: bool, scheme: bool, background: bool) -> Vec { let mut b = le32(&[(-2880i32) as u32, (-2160i32) as u32, 2880, 2160]); - b.extend_from_slice(&[1, 1, 0, 0, u8::from(follow_background), 0, 0, 0]); + b.extend_from_slice(&[u8::from(objects), u8::from(scheme), 0, 0, u8::from(background), 0, 0, 0]); rec(rt::SLIDE_BOX, 1, 0, &b) } @@ -59,6 +60,8 @@ struct SlideSpec { id: u32, master: u32, layout: u32, + follow_objects: bool, + follow_scheme: bool, follow_background: bool, fill: [u8; 4], filled: bool, @@ -67,7 +70,17 @@ struct SlideSpec { impl SlideSpec { fn new(id: u32, layout: u32) -> SlideSpec { - SlideSpec { id, master: MASTER, layout, follow_background: true, fill: [0, 0, 0, 0], filled: true, scheme: DEFAULT } + SlideSpec { + id, + master: MASTER, + layout, + follow_objects: true, + follow_scheme: true, + follow_background: true, + fill: [0, 0, 0, 0], + filled: true, + scheme: DEFAULT, + } } } @@ -77,7 +90,7 @@ fn slide(s: &SlideSpec) -> Vec { 0x10, &[ rec(rt::SLIDE_ATOM, 0, 0, &le32(&[s.id, s.master])), - slide_box(s.follow_background), + slide_box(s.follow_objects, s.follow_scheme, s.follow_background), rec(rt::LAYOUT, 1, 0, &le32(&[s.layout, 0, 0])), container(rt::SCHEME, 0x78, &[scheme_atom(0, s.scheme)]), background_shape(s.fill, s.filled), @@ -92,7 +105,7 @@ fn master(id: u32, scheme: [u32; 8], fill: [u8; 4]) -> Vec { 0x10, &[ rec(rt::SLIDE_ATOM, 0, 0, &le32(&[id, 0])), - slide_box(true), + slide_box(true, true, true), // A scheme preset, which isn't the master's scheme. scheme_atom(0x37, [0x0000FF, 0xFFFFFF, 0, 0xFFFF00, 0xFF9900, 0x00FFFF, 0xFF0033, 0x969696]), rec(rt::LAYOUT, 1, 0, &[1, 0, 0, 0, 1, 2, 6, 8, 7, 0, 0, 0]), @@ -247,6 +260,7 @@ fn a_slide_that_doesnt_follow_the_master_keeps_its_background() { // A scheme slot (4, fills) of the slide's own scheme. let mut slot = SlideSpec::new(0x101, 16); slot.follow_background = false; + slot.follow_scheme = false; slot.fill = [0, 0, 0, 4]; slot.scheme[4] = 0x123456; // Following the master, its own background shape is not used. @@ -262,6 +276,26 @@ fn a_slide_that_doesnt_follow_the_master_keeps_its_background() { assert_eq!(p.slides[3].background, Some(Background::Fill { fill: Fill::None })); } +/// The slide box's flags at +16 and +17 (seen in PowerPoint 95's files: turning the master's +/// objects off on a slide clears +16; giving one slide its own colour scheme clears +17): a slide +/// that follows the master's scheme takes the master's colours, not the copy it holds. +#[test] +fn slides_follow_the_masters_objects_and_scheme() { + let mut own = SlideSpec::new(0x100, 16); + (own.follow_objects, own.follow_scheme, own.follow_background) = (false, false, false); + own.fill = [0, 0, 0, 0]; + own.scheme[0] = 0x112233; + let mut follows = SlideSpec::new(0x101, 16); + follows.follow_background = false; + follows.fill = [0, 0, 0, 0]; + follows.scheme[0] = 0x445566; + let p = deck(&[own, follows]); + assert!(!p.slides[0].show_master_shapes); + assert!(p.slides[1].show_master_shapes); + assert_eq!(p.slides[0].background, solid(0x11, 0x22, 0x33), "its own scheme"); + assert_eq!(p.slides[1].background, solid(0xFF, 0xFF, 0xFF), "the master's scheme, not its copy"); +} + /// Damaged input never panics or grows without bound: every word set to hostile values, /// truncations, deep nesting and huge counts. #[test] @@ -335,6 +369,11 @@ fn powerpoint_95_samples() { "title" => assert_eq!(layout_kind(&p, 1), LayoutType::Title), "layout3" => assert_eq!(layout_kind(&p, 1), LayoutType::TwoContent), "background" => assert_eq!(p.slides[0].background, solid(0xFF, 0, 0)), + // FollowMasterObjects turned off on the slide. + "nomasterobj" => assert!(!p.slides[0].show_master_shapes), + // The Slide Color Scheme dialog's Apply (this slide only): the slide keeps its own scheme. + "schemeapply" => assert!(p.slides[0].show_master_shapes), + "base" => assert!(p.slides[0].show_master_shapes), _ => {} } seen += 1;