diff --git a/crates/ppt-binary/README.md b/crates/ppt-binary/README.md index 4ec03ae78..662f66ea8 100644 --- a/crates/ppt-binary/README.md +++ b/crates/ppt-binary/README.md @@ -49,11 +49,14 @@ underline and colour (an RGB or a colour scheme slot). The text is Windows-1252. placeholders set where the default master's placeholders go, and its date, footer and slide number those of every layout. Shapes and lines read too: PowerPoint 95's twelve shapes (rectangle, rounded rectangle, ellipse, diamond, the two triangles, parallelogram, trapezoid, hexagon, -octagon, plus and five-point star) as DrawingML presets with their adjust values, each with its -fill and line colour (or none), its shadow, its angle and a horizontal flip, and lines from their -start to their end. A shape that draws nothing but text is a text box. Line widths and styles, -vertical flips and shadow offsets are not read yet (lines draw 1 pt wide, as PowerPoint 95 drew -them, and shadows fall 6 pt right and down); groups and freeforms are skipped. Pictures, notes and the master's text styles come later. +octagon, plus and five-point star) as DrawingML presets with their adjust values (the trapezoid, +wide at the top in PowerPoint 95, as its own outline), each with its fill (or none), its line +(or none) with its colour, its width (the six line styles, 1 to 10 pt) and its dots or dashes, +its shadow and the shadow's colour, its angle and its horizontal and vertical flips; and lines +from their start to their end, with their arrowheads. A shape turned by whole quarter-turns +keeps its unturned box. A shape that draws nothing but text is a text box. Shadow offsets are +not read yet (shadows fall 6 pt right and down, as in every file PowerPoint 95 wrote); groups and +freeforms are skipped. Pictures, notes and the master's text styles 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 5756cf051..66635868f 100644 --- a/crates/ppt-binary/src/ppt95.rs +++ b/crates/ppt-binary/src/ppt95.rs @@ -12,10 +12,11 @@ //! A page's object list (3000) holds shapes (3008), placeholders (3010, which add a 3011 atom: the //! type, the size, then the index) and lines (3014). A shape or placeholder has its geometry //! (3009): the shape code and its adjust value, the box as left, top, right and bottom in master -//! units (576 per inch) measured from the slide's centre, then the angle in sixteenths of a degree. -//! A turned shape keeps its unturned box. A line holds 3015: its start and end points, in the same -//! units. Shapes and lines have a format (3005) with their fill, line, shadow and horizontal flip -//! ([`shape`]). A shape that draws nothing but its text is a text box. +//! units (576 per inch) measured from the slide's centre, then what is left of the angle after +//! whole quarter-turns, in sixteenths of a degree. A line holds 3015: its start and end points, in +//! the same units, and 3007: its arrowheads. Shapes and lines have a format (3005) with their fill, +//! line width, dashes and colour, shadow, angle and flips ([`shape`]); a box turned by an odd number +//! of quarter-turns is turned back. A shape that draws nothing but its text is a text box. //! //! PowerPoint 95 numbers placeholders its own way: on the master 1 title, 2 body, 6 date, 7 slide //! number and 8 footer; on slides 0x0C title, 0x0D body, 0x0F centred title and 0x10 subtitle. A @@ -23,10 +24,9 @@ //! caller's default master's, and the footer row places every layout's, as the PowerPoint 97 path //! does. //! -//! Not read yet: line widths and styles, vertical flips and shadow offsets (no probe changed them); -//! groups and freeforms, which are skipped; pictures; notes; and the master's text styles, so the -//! levels of the default master's styles stay as they are (the slide text carries its own -//! formatting in full). +//! Not read yet: shadow offsets (no probe could move one); groups and freeforms, which are +//! skipped; pictures; notes; and the master's text styles, so the levels of the default master's +//! styles stay as they are (the slide text carries its own formatting in full). use std::collections::{HashMap, HashSet}; use std::sync::Arc; @@ -64,6 +64,7 @@ mod rt { pub const PLACEHOLDER: u32 = 3010; pub const PLACEHOLDER_ATOM: u32 = 3011; pub const LINE: u32 = 3014; + pub const ARROWHEADS: u32 = 3007; pub const LINE_ATOM: u32 = 3015; pub const TEXT: u32 = 4001; } @@ -258,7 +259,7 @@ fn xfrm_of([l, t, right, b]: [i32; 4], size: Size) -> Xfrm { } /// A line: the box its ends span, flipped so that it runs from its start to its end, drawn as its -/// format says (in the scheme's text and lines colour without one). +/// format says (1 pt in the scheme's text and lines colour without one), with its arrowheads. fn line(r: &Rec, scheme: &Scheme, size: Size) -> Option { let a = r.child(rt::LINE_ATOM)?; let (x1, y1, x2, y2) = (a.i32(0)?, a.i32(4)?, a.i32(8)?, a.i32(12)?); @@ -266,10 +267,13 @@ fn line(r: &Rec, scheme: &Scheme, size: Size) -> Option { (xfrm.flip_h, xfrm.flip_v) = (x2 < x1, y2 < y1); let mut look = match r.child(rt::FORMAT) { Some(f) => shape::look(&f, scheme), - None => Look { line: shape::line_of(scheme[1]), ..Look::none() }, + None => Look { line: shape::line_of(scheme[1], None), ..Look::none() }, }; // A line has no inside to fill, and its direction is in its ends. - (look.fill, look.flip_h) = (Fill::None, false); + (look.fill, look.flip_h, look.flip_v) = (Fill::None, false, false); + if let Some(heads) = r.child(rt::ARROWHEADS).filter(|_| look.line != Line::none()) { + shape::arrowheads(&mut look.line, &heads); + } let geom = Some(Geom::Preset { name: "line".into(), adj: vec![] }); Some(Object { placeholder: false, ph: None, xfrm, text: None, geom, look, line: true }) } @@ -283,8 +287,10 @@ fn object(r: &Rec, env: &Env, scheme: &Scheme, size: Size) -> Option { let g = r.child(rt::GEOMETRY)?; let sides = [g.i32(12)?, g.i32(16)?, g.i32(20)?, g.i32(24)?]; let mut xfrm = xfrm_of(sides, size); - // The box is the unturned one; the angle is in sixteenths of a degree. - xfrm.rot = g.i32(28).map_or(0.0, |v| (f64::from(v) / 16.0).rem_euclid(360.0)); + let format = r.child(rt::FORMAT); + // The angle the user set is the format's; the geometry's is what quarter-turns leave of it. + let drawn = g.i32(28).unwrap_or(0); + shape::orient(&mut xfrm, drawn, format.and_then(|f| f.i32(48)).unwrap_or(drawn)); let placeholder = r.kind == rt::PLACEHOLDER; let ph = r.child(rt::PLACEHOLDER_ATOM).filter(|_| placeholder).and_then(|a| { let size = match a.u8(1) { @@ -297,16 +303,13 @@ fn object(r: &Rec, env: &Env, scheme: &Scheme, size: Size) -> Option { let text = r.child(rt::TEXT).and_then(|c| text::text_body(&c, env, scheme, Some(sides), ph.is_none())); let (mut geom, mut look) = (None, Look::none()); if !placeholder { - // The shape code is the low byte: PowerPoint 95 sets bits above it on some shapes (0x60 on - // the trapezoid). - if let Some((preset, upside_down)) = g.u8(0).and_then(|c| shape::preset(c as i8, g.i32(4), xfrm.w, xfrm.h)) { - geom = Some(preset); - xfrm.flip_v = upside_down; - } - if let Some(f) = r.child(rt::FORMAT) { + // The shape code is the low byte: the bits above it orient the outline as drawn (0x60 on a + // new trapezoid), which the format's angle and flips already say. + geom = g.u8(0).and_then(|c| shape::preset(c as i8, g.i32(4), xfrm.w, xfrm.h)); + if let Some(f) = format { look = shape::look(&f, scheme); } - xfrm.flip_h = look.flip_h; + (xfrm.flip_h, xfrm.flip_v) = (look.flip_h, look.flip_v); } Some(Object { placeholder, ph, xfrm, text, geom, look, line: false }) } diff --git a/crates/ppt-binary/src/ppt95/shape.rs b/crates/ppt-binary/src/ppt95/shape.rs index 51d343d70..ebe4e0c66 100644 --- a/crates/ppt-binary/src/ppt95/shape.rs +++ b/crates/ppt-binary/src/ppt95/shape.rs @@ -1,29 +1,37 @@ //! PowerPoint 95 shapes: the shape code of a geometry record (3009) as a DrawingML preset, and a -//! format record (3005) as fill, line and shadow, learned from files PowerPoint 95 wrote and the -//! slides it rendered from them. +//! format record (3005) as fill, line, shadow, angle and flips, learned from files PowerPoint 95 +//! wrote and the slides it rendered from them. //! //! The shape code is the [MS-ODRAW] shape type less 4, so the [`geom`] table names its preset; the //! adjust value after it is a coordinate in the shape's 21600-unit square, as OfficeArt's are. //! -//! A format record holds the line (+0 0xFF for none, +4 its colour), the fill (+12 0xFF for none, -//! +20 its colour) and the shadow (+24 its colour, +28 0x00 when on and 0xFF when off), then the -//! angle at +48 and a horizontal flip at +52. Each colour is R, G, B and an index: 0xFE for the RGB -//! itself, else a slot of the colour scheme. +//! A format record holds the line (+0 0xFF for none, +1 its dash style, +3 its style less one, +//! which sets its width, +4 its colour), the fill (+12 0xFF for none, +20 its colour) and the +//! shadow (+28 0x00 when on and 0xFF when off, +30 its colour), then the angle the user set at +48 +//! (sixteenths of a degree) and the flips at +52 (bit 1 horizontal, bit 2 vertical). Each colour is +//! R, G, B and an index: 0xFE for the RGB itself, else a slot of the colour scheme. +//! +//! The geometry record holds the outline as drawn: whole quarter-turns of the angle are folded into +//! orientation bits above its shape code, its box and its own angle, which is only what is left +//! over. So the angle comes from the format, and the box is turned back by the quarter-turns +//! between the two ([`orient`]). A line's 3007 atom holds its arrowheads ([`arrowheads`]). -use craft_pptx::style::Shadow; -use craft_pptx::{ColorRef, Fill, Geom, Line, Rgba}; +use craft_pptx::style::{Dash, LineEnd, Shadow}; +use craft_pptx::{ColorRef, CustomPath, Fill, Geom, Line, Rgba, Xfrm}; use super::{RGB_GIVEN, Rec}; use crate::convert::Scheme; use crate::geom; -/// The width of every line PowerPoint 95 drew in the probes' renderings, in points (8 master -/// units); no probe set another, so the format's width field is not known yet. -const LINE_WIDTH: f64 = 1.0; -/// How far a shadow falls right and down, in points. The probes turned shadows on and off but -/// never moved one, so the offset is not read: this is the 48 units (6 pt) every probe's format -/// holds at +36 and +40, where the offset most likely is. +/// The widths of line styles 1 to 6 (the format holds the style less one), in points: the pens +/// PowerPoint 95 drew them with were 8, 16, 32, 48, 64 and 80 master units wide. +const LINE_WIDTHS: [f64; 6] = [1.0, 2.0, 4.0, 6.0, 8.0, 10.0]; +/// How far a shadow falls right and down, in points. The offset can't be set through the object +/// model, so no probe moved it: this is the 48 units (6 pt) every probe's format holds at +36 and +/// +40, where the offset most likely is, and they are not read. const SHADOW_OFFSET: f64 = 6.0; +/// A whole turn in sixteenths of a degree. +const TURN: i64 = 360 * 16; /// How a shape is drawn. #[derive(Clone, Debug, PartialEq)] @@ -32,12 +40,13 @@ pub(super) struct Look { pub line: Line, pub shadow: Option, pub flip_h: bool, + pub flip_v: bool, } impl Look { /// Nothing drawn: what a shape with no format record shows. pub(super) fn none() -> Look { - Look { fill: Fill::None, line: Line::none(), shadow: None, flip_h: false } + Look { fill: Fill::None, line: Line::none(), shadow: None, flip_h: false, flip_v: false } } /// Whether the shape draws anything of its own (its text aside). @@ -52,16 +61,41 @@ pub(super) fn color(format: &Rec, at: usize, scheme: &Scheme) -> Option { if index == RGB_GIVEN { Some(Rgba::rgb(r, g, b)) } else { scheme.get(usize::from(index)).copied() } } -/// A line of `color` as PowerPoint 95 draws it. -pub(super) fn line_of(color: Rgba) -> Line { - Line::solid(ColorRef::rgb(color), LINE_WIDTH) +/// A line of `color` in the style and dashes of `format`: 1 pt and solid without one, or for a +/// style or dash value no probe showed. +pub(super) fn line_of(color: Rgba, format: Option<&Rec>) -> Line { + let width = format.and_then(|f| f.u8(3)).and_then(|s| LINE_WIDTHS.get(usize::from(s))).copied().unwrap_or(1.0); + let mut line = Line::solid(ColorRef::rgb(color), width); + let dash = match format.and_then(|f| f.u8(1)) { + Some(1) => Some("sysDot"), + Some(2) => Some("dash"), + Some(3) => Some("lgDash"), + Some(4) => Some("dashDot"), + _ => None, + }; + line.dash = dash.map(|name| Dash::Preset { name: name.into() }); + line } -/// A format record as fill, line, shadow and flip. A part whose flag or colour is cut off or names -/// no scheme slot is not drawn. +/// A line's arrowheads from its 3007 atom: +0 the start's, +1 the end's. 4 is a small swept-back +/// head, 1 a longer and wider one; 0, and any value no probe showed, is none. +pub(super) fn arrowheads(line: &mut Line, atom: &Rec) { + let end = |v: Option| { + let (kind, size) = match v? { + 4 => ("stealth", "med"), + 1 => ("triangle", "lg"), + _ => return None, + }; + Some(LineEnd { kind: kind.into(), w: size.into(), len: size.into() }) + }; + (line.head, line.tail) = (end(atom.u8(0)), end(atom.u8(1))); +} + +/// A format record as fill, line, shadow and flips (the angle is read with the box, [`orient`]). A part whose flag or colour is cut off +/// or names no scheme slot is not drawn. pub(super) fn look(format: &Rec, scheme: &Scheme) -> Look { let on = |flag: usize, at: usize| format.u8(flag).filter(|&f| f != 0xFF).and_then(|_| color(format, at, scheme)); - let shadow = format.u8(28).filter(|&f| f == 0).and_then(|_| color(format, 24, scheme)).map(|c| Shadow { + let shadow = format.u8(28).filter(|&f| f == 0).and_then(|_| color(format, 30, scheme)).map(|c| Shadow { color: ColorRef::rgb(c), blur: 0.0, dist: SHADOW_OFFSET.hypot(SHADOW_OFFSET), @@ -74,25 +108,54 @@ pub(super) fn look(format: &Rec, scheme: &Scheme) -> Look { align: String::new(), rotate_with_shape: false, }); + let flips = format.u8(52).unwrap_or(0); Look { fill: on(12, 20).map_or(Fill::None, |c| Fill::solid(ColorRef::rgb(c))), - line: on(0, 4).map_or_else(Line::none, line_of), + line: on(0, 4).map_or_else(Line::none, |c| line_of(c, Some(format))), shadow, - flip_h: format.u8(52).is_some_and(|f| f != 0), + flip_h: flips & 1 != 0, + flip_v: flips & 2 != 0, } } -/// The preset for a shape code with its adjust value, in a box `w` × `h` points, and whether it is -/// drawn upside down from the preset. `None` for a code no probe showed. +/// Give `xfrm`, the geometry record's box, the angle `turn` the user set, given the angle `drawn` +/// the geometry record holds (both in sixteenths of a degree). The quarter-turns between them are +/// folded into the drawn box: after an odd number of them it is the turned box, so its width and +/// height are swapped back about its centre. +pub(super) fn orient(xfrm: &mut Xfrm, drawn: i32, turn: i32) { + let quarter = TURN / 4; + let quarters = (i64::from(turn) - i64::from(drawn) + quarter / 2).div_euclid(quarter).rem_euclid(4); + if quarters % 2 == 1 { + let (cx, cy) = (xfrm.x + xfrm.w / 2.0, xfrm.y + xfrm.h / 2.0); + (xfrm.w, xfrm.h) = (xfrm.h, xfrm.w); + (xfrm.x, xfrm.y) = (cx - xfrm.w / 2.0, cy - xfrm.h / 2.0); + } + xfrm.rot = (i64::from(turn).rem_euclid(TURN) as f64) / 16.0; +} + +/// PowerPoint 95's trapezoid: wide at the top, each bottom corner inset by the adjust value (0 to +/// half the side, a quarter by default) in the shape's 21600-unit square. The same outline the +/// PowerPoint 97 path gives the trapezoid (storytold/craft-libs#2492), to be shared once that lands. +fn trapezoid(adjust: Option) -> Geom { + let a = adjust.unwrap_or(5400).clamp(0, 10800); + let d = format!("M 0 0 L {a} 21600 L {} 21600 L 21600 0 Z", 21600 - a); + Geom::Custom { paths: vec![CustomPath { w: 21600.0, h: 21600.0, d, ..Default::default() }] } +} + +/// The geometry for a shape code with its adjust value, in a box `w` × `h` points: a preset, or +/// the trapezoid's own outline. `None` for a code no probe showed. /// /// An OfficeArt adjust value is a coordinate in the shape's 21600-unit square, stretched to the box /// on each axis; a DrawingML one is a 100000 fraction of the width (the triangle's apex) or of the -/// shorter side. PowerPoint 95's trapezoid is wide at the top, DrawingML's at the bottom. -pub(super) fn preset(code: i8, adjust: Option, w: f64, h: f64) -> Option<(Geom, bool)> { +/// shorter side. +pub(super) fn preset(code: i8, adjust: Option, w: f64, h: f64) -> Option { if !(-3..=8).contains(&code) { return None; } let kind = u16::try_from(i16::from(code) + 4).ok()?; + if kind == 8 { + return Some(trapezoid(adjust)); + } let name = geom::preset_name(kind)?; let fraction = |v: i32, max: f64| f64::from(v).clamp(0.0, max) / 21600.0 * 100_000.0; // A horizontal inset: a share of the width, as a share of the shorter side. @@ -102,9 +165,9 @@ pub(super) fn preset(code: i8, adjust: Option, w: f64, h: f64) -> Option<(G 2 => Some(geom::round_rect_adjust(v)), 5 => Some(fraction(v, 21600.0)), 7 => Some(across(v, 21600.0)), - 8 | 9 => Some(across(v, 10800.0)), + 9 => Some(across(v, 10800.0)), 10 | 11 => Some(fraction(v, 10800.0)), _ => None, }); - Some((Geom::Preset { name: name.into(), adj: adj.into_iter().collect() }, kind == 8)) + Some(Geom::Preset { name: name.into(), adj: adj.into_iter().collect() }) } diff --git a/crates/ppt-binary/src/ppt95/tests.rs b/crates/ppt-binary/src/ppt95/tests.rs index 2fed4b413..ae9dedcac 100644 --- a/crates/ppt-binary/src/ppt95/tests.rs +++ b/crates/ppt-binary/src/ppt95/tests.rs @@ -714,46 +714,73 @@ fn coded_geometry(code: u32, adjust: i32, bx: [i32; 4], rot: i32) -> Vec { } /// How a test shape is drawn: line and fill colours (R, G, B, then 0xFE or a scheme slot; `None` -/// for none), the shadow's colour when it has one, and the horizontal flip. +/// for none), the shadow's colour when it has one, the flips (bit 1 horizontal, bit 2 vertical), +/// the line's style byte (its style less one) and dash byte, and the angle the user set. #[derive(Clone, Copy)] struct Format { line: Option<[u8; 4]>, fill: Option<[u8; 4]>, shadow: Option<[u8; 4]>, - flip_h: bool, + flips: u8, + style: u8, + dash: u8, + turn: i32, } -/// PowerPoint 95's defaults: lines in slot 1, fills in slot 4, no shadow. -const PLAIN: Format = Format { line: Some([0, 0, 0, 1]), fill: Some([0, 0, 0, 4]), shadow: None, flip_h: false }; +/// PowerPoint 95's defaults: lines in slot 1, fills in slot 4, no shadow (its colour in slot 2). +const PLAIN: Format = Format { line: Some([0, 0, 0, 1]), fill: Some([0, 0, 0, 4]), shadow: None, flips: 0, style: 0, dash: 0, turn: 0 }; fn format(f: Format) -> Vec { let mut b = vec![0u8; 56]; b[0] = if f.line.is_some() { 0 } else { 0xFF }; + b[1] = f.dash; + b[3] = f.style; b[4..8].copy_from_slice(&f.line.unwrap_or([0, 0, 0, 1])); b[12] = if f.fill.is_some() { 0 } else { 0xFF }; b[13] = 1; b[20..24].copy_from_slice(&f.fill.unwrap_or([0, 0, 0, 4])); - b[24..28].copy_from_slice(&f.shadow.unwrap_or([0, 0, 0, 1])); + // +24 stays slot 1 whatever the shadow is, as PowerPoint 95 writes it. + b[24..28].copy_from_slice(&[0, 0, 0, 1]); b[28] = if f.shadow.is_some() { 0 } else { 0xFF }; b[29] = 1; + b[30..34].copy_from_slice(&f.shadow.unwrap_or([0, 0, 0, 2])); b[36] = 48; b[40] = 48; - b[52] = u8::from(f.flip_h); + b[48..52].copy_from_slice(&f.turn.to_le_bytes()); + b[52] = f.flips; rec(rt::FORMAT, 1, 0, &b) } -/// A shape of `code` with its adjust value, box, angle and format. +/// A shape of `code` with its adjust value, box, angle and format, its angle less than a quarter +/// turn, so the geometry and the format agree. fn autoshape(code: i32, adjust: i32, bx: [i32; 4], rot: i32, f: Format) -> Vec { - container(rt::SHAPE, 0x13, &[coded_geometry(code as u32, adjust, bx, rot), format(f)]) + drawn_shape(code, adjust, bx, rot, Format { turn: rot, ..f }) +} + +/// A shape whose geometry holds `drawn`, the box and angle as drawn, and whose format holds the +/// angle the user set. +fn drawn_shape(code: i32, adjust: i32, drawn: [i32; 4], rot: i32, f: Format) -> Vec { + container(rt::SHAPE, 0x13, &[coded_geometry(code as u32, adjust, drawn, rot), format(f)]) } /// A line from (x1, y1) to (x2, y2), with its format when given. fn line_95(ends: [i32; 4], f: Option) -> Vec { - let mut kids = vec![rec(rt::LINE_ATOM, 0, 0, &ends.iter().flat_map(|v| v.to_le_bytes()).collect::>()), rec(3007, 0, 0, &[0, 0])]; + arrow_line(ends, [0, 0], f) +} + +/// A line with its arrowhead atom (start, end). +fn arrow_line(ends: [i32; 4], heads: [u8; 2], f: Option) -> Vec { + let mut kids = vec![rec(rt::LINE_ATOM, 0, 0, &ends.iter().flat_map(|v| v.to_le_bytes()).collect::>()), rec(rt::ARROWHEADS, 0, 0, &heads)]; kids.extend(f.map(format)); container(rt::LINE, 0x13, &kids) } +/// PowerPoint 95's trapezoid outline with bottom corners inset by `a`. +fn trapezoid_95(a: i32) -> Geom { + let d = format!("M 0 0 L {a} 21600 L {} 21600 L 21600 0 Z", 21600 - a); + Geom::Custom { paths: vec![craft_pptx::CustomPath { w: 21600.0, h: 21600.0, d, ..Default::default() }] } +} + fn preset_of(s: &Shape) -> (&str, &[f64]) { match &s.geom { Geom::Preset { name, adj } => (name, adj), @@ -766,9 +793,9 @@ fn rgb(r: u8, g: u8, b: u8) -> ColorRef { } /// Each shape code PowerPoint 95 wrote is its preset, with its adjust value in DrawingML terms (the -/// probes' adjust values are DrawingML's defaults on a square). The trapezoid, wide at the top in -/// PowerPoint 95, is DrawingML's turned upside down; the bits above its code's low byte don't -/// change the code. +/// probes' adjust values are DrawingML's defaults on a square), except the trapezoid, which has its +/// own outline ([`trapezoids_are_wide_at_the_top`]); the bits above its code's low byte don't change +/// the code. #[test] fn shape_codes_are_presets_with_their_adjust_values() { let sq = [0, 0, 1152, 1152]; @@ -780,7 +807,7 @@ fn shape_codes_are_presets_with_their_adjust_values() { (1, 10800, "triangle", &[50_000.0]), (2, 0, "rtTriangle", &[]), (3, 5400, "parallelogram", &[25_000.0]), - (0x6004, 5400, "trapezoid", &[25_000.0]), + (0x6004, 5400, "custom", &[]), (5, 5400, "hexagon", &[25_000.0]), (6, 6326, "octagon", &[6326.0 / 21600.0 * 100_000.0]), (7, 5400, "plus", &[25_000.0]), @@ -792,9 +819,9 @@ fn shape_codes_are_presets_with_their_adjust_values() { for (s, (code, _, name, adj)) in shapes.iter().zip(cases) { assert_eq!(preset_of(s), (name, adj), "code {code}"); assert_eq!(boxed(s), (360.0, 270.0, 144.0, 144.0)); - assert_eq!(s.xfrm.unwrap().flip_v, name == "trapezoid", "{name}"); + assert!(!s.xfrm.unwrap().flip_v, "{name}"); assert!(!s.text_box && s.ph.is_none()); - assert!(craft_drawingml::preset::build(name, 144.0, 144.0, adj).is_some()); + assert!(name == "custom" || craft_drawingml::preset::build(name, 144.0, 144.0, adj).is_some()); } // An inset across a box twice as wide as it is tall is twice the share of its shorter side. let p = deck_of(vec![autoshape(3, 5400, [0, 0, 1152, 576], 0, PLAIN), autoshape(-2, 99_999, sq, 0, PLAIN)], &[]); @@ -825,7 +852,7 @@ fn shapes_keep_their_fill_line_and_shadow() { assert_eq!(s[3].line, s[0].line); assert_eq!(s[4].line, Some(craft_pptx::Line::none())); let sh = s[5].effects.as_ref().and_then(|e| e.outer_shadow.clone()).unwrap(); - assert_eq!((sh.color, sh.dir, sh.blur), (rgb(0x80, 0x80, 0x80), 45.0, 0.0)); + assert_eq!((sh.color, sh.dir, sh.blur), (rgb(0x80, 0x80, 0x80), 45.0, 0.0), "the shadow's colour is at +30, not +24"); assert!((sh.dist - 6.0 * 2f64.sqrt()).abs() < 1e-9); // A slot of the slide's own scheme. @@ -851,7 +878,7 @@ fn shapes_keep_their_fill_line_and_shadow() { #[test] fn shapes_keep_their_angle_and_flip() { let bx = [-2304, -1584, -1152, -1008]; - let p = deck_of(vec![autoshape(-3, 0, bx, 480, PLAIN), autoshape(2, 0, bx, 0, Format { flip_h: true, ..PLAIN })], &[]); + let p = deck_of(vec![autoshape(-3, 0, bx, 480, PLAIN), autoshape(2, 0, bx, 0, Format { flips: 1, ..PLAIN })], &[]); let x: Vec<_> = p.slides[0].shapes.iter().map(|s| s.xfrm.unwrap()).collect(); assert_eq!((x[0].x, x[0].y, x[0].w, x[0].h, x[0].rot, x[0].flip_h), (72.0, 72.0, 144.0, 72.0, 30.0, false)); assert_eq!((x[1].rot, x[1].flip_h, x[1].flip_v), (0.0, true, false)); @@ -915,7 +942,15 @@ fn hostile_shapes_never_panic() { objects.push(container(rt::SHAPE, 0x13, &[cut(coded_geometry(1, 10800, bx, 0), keep), format(PLAIN)])); objects.push(container(rt::LINE, 0x13, &[cut(rec(rt::LINE_ATOM, 0, 0, &le32(&[0, 0, 8, 8])), keep.min(16))])); } - let huge = Format { line: Some([0, 0, 0, 0xFD]), fill: Some([9, 9, 9, 200]), shadow: Some([1, 1, 1, 8]), flip_h: true }; + let huge = Format { + line: Some([0, 0, 0, 0xFD]), + fill: Some([9, 9, 9, 200]), + shadow: Some([1, 1, 1, 8]), + flips: 0xFF, + style: 0xFF, + dash: 0xFF, + turn: i32::MIN, + }; objects.push(autoshape(5, i32::MAX, [i32::MIN, i32::MIN, i32::MAX, i32::MAX], i32::MIN, huge)); objects.push(autoshape(3, i32::MIN, [0, 0, 0, 0], i32::MAX, PLAIN)); objects.push(line_95([i32::MIN, i32::MAX, i32::MAX, i32::MIN], Some(huge))); @@ -929,8 +964,10 @@ fn hostile_shapes_never_panic() { } let mut s = SlideSpec::new(0x100, 0); - s.objects = - vec![autoshape(4, 5400, bx, 480, Format { shadow: Some([0, 0, 0, 2]), flip_h: true, ..PLAIN }), line_95([0, 0, 576, 288], Some(PLAIN))]; + s.objects = vec![ + autoshape(4, 5400, bx, 480, Format { shadow: Some([0, 0, 0, 2]), flips: 3, ..PLAIN }), + arrow_line([0, 0, 576, 288], [4, 1], Some(PLAIN)), + ]; let stream = stream_of(document_atom(5760, 4320, 1), &[slide(&s)], &[master_with(MASTER, DEFAULT, [0; 4], &[autoshape(-1, 0, bx, 0, PLAIN)])]); for at in (0..stream.len() - 4).step_by(4) { for v in [0u32, 1, 0xFE, 0x7FFF_FFFF, 0xFFFF_FFFF] { @@ -944,9 +981,160 @@ fn hostile_shapes_never_panic() { } } -/// The presets of the object model's shape types 1 to 12, as PowerPoint 95 rendered them. +/// The angle is the one the format holds. The geometry holds the outline as drawn, whole +/// quarter-turns folded into its box: after an odd number of them its box is the turned one, so it +/// is turned back about its centre. Less than a quarter turn, or a half turn, leaves it as it is. +#[test] +fn quarter_turns_are_turned_back() { + // A 2 in × 1 in rectangle at 1 in, 1 in; turned a quarter, it is drawn 1 in × 2 in about the + // same centre (as the rot90wide probe). + let (wide, tall) = ([-2304, -1584, -1152, -1008], [-2016, -1872, -1440, -720]); + let turned = |drawn: [i32; 4], drawn_rot: i32, turn: i32| drawn_shape(-3, 0, drawn, drawn_rot, Format { turn, ..PLAIN }); + let p = deck_of( + vec![ + turned(wide, 480, 480), + turned(tall, 0, 1440), + turned(wide, 0, 2880), + turned(tall, 0, 4320), + turned(tall, 0, -1440), + turned(wide, 480, 3360), + turned(tall, 480, 1920), + drawn_shape(2, 0, wide, 1920, Format { turn: 1920, ..PLAIN }), + turned(wide, 0, 480), + ], + &[], + ); + let got: Vec<_> = p.slides[0].shapes.iter().map(|s| (boxed(s), s.xfrm.unwrap().rot)).collect(); + let flat = (72.0, 72.0, 144.0, 72.0); + assert_eq!( + got, + [(flat, 30.0), (flat, 90.0), (flat, 180.0), (flat, 270.0), (flat, 270.0), (flat, 210.0), (flat, 120.0), (flat, 120.0), (flat, 30.0)] + ); +} + +/// The flips are the format's +52: bit 1 horizontal, bit 2 vertical. The orientation bits above +/// the geometry's shape code don't flip it. +#[test] +fn flips_come_from_the_format() { + let bx = [0, 0, 576, 576]; + let cases = [(2, 1), (2, 2), (2, 3), (0x1002, 0), (0x3002, 2), (1, 0xFC)]; + let p = deck_of(cases.iter().map(|&(code, flips)| autoshape(code, 0, bx, 0, Format { flips, ..PLAIN })).collect(), &[]); + let flips: Vec<_> = p.slides[0].shapes.iter().map(|s| (s.xfrm.unwrap().flip_h, s.xfrm.unwrap().flip_v)).collect(); + assert_eq!(flips, [(true, false), (false, true), (true, true), (false, false), (false, true), (false, false)]); +} + +/// PowerPoint 95's trapezoid is wide at the top: its own outline, its bottom corners inset by the +/// adjust value (up to half the side). A flip the user set flips it as any other shape. +#[test] +fn trapezoids_are_wide_at_the_top() { + let sq = [0, 0, 1152, 1152]; + let p = deck_of( + vec![ + autoshape(0x6004, 5400, sq, 0, PLAIN), + autoshape(0x6004, 2700, sq, 0, PLAIN), + autoshape(0x6004, 20_000, sq, 0, PLAIN), + autoshape(0x6004, -5, sq, 0, PLAIN), + autoshape(4, 5400, sq, 0, Format { flips: 2, ..PLAIN }), + ], + &[], + ); + let s = &p.slides[0].shapes; + let got: Vec<_> = s.iter().map(|s| (s.geom.clone(), s.xfrm.unwrap().flip_v)).collect(); + let want = [(5400, false), (2700, false), (10800, false), (0, false), (5400, true)].map(|(a, v)| (trapezoid_95(a), v)); + assert_eq!(got, want); + assert_eq!(boxed(&s[0]), (360.0, 270.0, 144.0, 144.0)); +} + +/// The line's style sets its width, 1 to 10 pt; its dash style dots or dashes it. Values no probe +/// showed are a 1 pt solid line. +#[test] +fn lines_keep_their_width_and_dashes() { + let bx = [0, 0, 576, 576]; + let styles = [0u8, 1, 2, 3, 4, 5, 6, 0xFF]; + let p = deck_of(styles.iter().map(|&style| autoshape(-3, 0, bx, 0, Format { style, ..PLAIN })).collect(), &[]); + let widths: Vec<_> = p.slides[0].shapes.iter().map(|s| s.line.as_ref().and_then(|l| l.width)).collect(); + assert_eq!(widths, [1.0, 2.0, 4.0, 6.0, 8.0, 10.0, 1.0, 1.0].map(Some)); + + let dashes = [0u8, 1, 2, 3, 4, 5, 0xFF]; + let mut objects: Vec<_> = dashes.iter().map(|&dash| autoshape(-3, 0, bx, 0, Format { dash, ..PLAIN })).collect(); + objects.push(line_95([0, 0, 576, 0], Some(Format { dash: 2, style: 3, ..PLAIN }))); + let p = deck_of(objects, &[]); + let got: Vec<_> = p.slides[0].shapes.iter().map(|s| s.line.as_ref().and_then(|l| l.dash.clone())).collect(); + let preset = |n: &str| Some(craft_pptx::style::Dash::Preset { name: n.into() }); + assert_eq!(got, [None, preset("sysDot"), preset("dash"), preset("lgDash"), preset("dashDot"), None, None, preset("dash")]); + let line = p.slides[0].shapes[7].line.clone().unwrap(); + assert_eq!((line.width, line.fill), (Some(6.0), Some(Fill::solid(rgb(0, 0, 0))))); +} + +/// A shadow is in its own colour at +30: an RGB, or a slot of the scheme (slot 2, the shadow +/// colour, by default). +#[test] +fn shadows_keep_their_colour() { + let bx = [0, 0, 576, 576]; + let mut spec = SlideSpec::new(0x100, 16); + spec.scheme[2] = 0x445566; + spec.objects = vec![ + autoshape(-3, 0, bx, 0, Format { shadow: Some([0xFF, 0, 0, 0xFE]), ..PLAIN }), + autoshape(-3, 0, bx, 0, Format { shadow: Some([0, 0, 0, 2]), ..PLAIN }), + autoshape(-3, 0, bx, 0, Format { shadow: Some([0, 0, 0, 200]), fill: None, line: None, ..PLAIN }), + ]; + let p = deck(&[spec]); + let s = &p.slides[0].shapes; + let colours: Vec<_> = s.iter().map(|s| s.effects.as_ref().and_then(|e| e.outer_shadow.as_ref()).map(|sh| sh.color.clone())).collect(); + assert_eq!(colours, [Some(rgb(0xFF, 0, 0)), Some(rgb(0x44, 0x55, 0x66))], "a shadow of no slot draws nothing"); +} + +/// A line's 3007 atom gives its start (DrawingML's head) and end (tail) arrowheads: 4 a small +/// swept-back head, 1 a larger one; other values none. A cut atom keeps what is there. +#[test] +fn lines_keep_their_arrowheads() { + let ends = [0, 0, 1152, 0]; + let cases: [[u8; 2]; 7] = [[0, 0], [0, 4], [4, 0], [4, 4], [0, 1], [1, 9], [7, 0xFF]]; + let mut objects: Vec<_> = cases.iter().map(|&h| arrow_line(ends, h, Some(PLAIN))).collect(); + objects.push(arrow_line(ends, [4, 4], None)); + objects.push(container(rt::LINE, 0x13, &[rec(rt::LINE_ATOM, 0, 0, &le32(&[0, 0, 8, 8])), rec(rt::ARROWHEADS, 0, 0, &[4]), format(PLAIN)])); + let p = deck_of(objects, &[]); + let end = |kind: &str, size: &str| Some(craft_pptx::style::LineEnd { kind: kind.into(), w: size.into(), len: size.into() }); + let (none, stealth, big) = (None, end("stealth", "med"), end("triangle", "lg")); + let got: Vec<_> = p.slides[0].shapes.iter().map(|s| s.line.as_ref().map(|l| (l.head.clone(), l.tail.clone())).unwrap()).collect(); + assert_eq!( + got, + [ + (none.clone(), none.clone()), + (none.clone(), stealth.clone()), + (stealth.clone(), none.clone()), + (stealth.clone(), stealth.clone()), + (none.clone(), big.clone()), + (big, none.clone()), + (none.clone(), none.clone()), + (stealth.clone(), stealth.clone()), + (stealth, none), + ] + ); +} + +/// Hostile angles, flips and line bytes never panic, and every angle is finite and in 0..360. +#[test] +fn hostile_angles_never_panic() { + let bx = [i32::MIN, i32::MIN, i32::MAX, i32::MAX]; + let mut objects = vec![]; + for (drawn, turn) in [(i32::MIN, i32::MAX), (i32::MAX, i32::MIN), (0, i32::MIN), (i32::MAX, i32::MAX), (-1, 1)] { + objects.push(drawn_shape(8, i32::MAX, bx, drawn, Format { turn, flips: 0xFF, style: 0x80, dash: 0x80, ..PLAIN })); + } + objects.push(arrow_line([i32::MIN, 0, i32::MAX, 0], [0xFF, 0x80], Some(Format { style: 0xFF, dash: 0xFF, ..PLAIN }))); + let p = deck_of(objects, &[]); + assert_eq!(p.slides[0].shapes.len(), 6); + for s in &p.slides[0].shapes { + let x = s.xfrm.unwrap(); + assert!([x.x, x.y, x.w, x.h].iter().all(|v| v.is_finite())); + assert!((0.0..360.0).contains(&x.rot), "{}", x.rot); + } +} + +/// The presets of the object model's shape types 1 to 12, as PowerPoint 95 rendered them ("custom" for +/// the trapezoid, which has its own outline). const SHAPE_PRESETS: [&str; 12] = - ["rect", "roundRect", "ellipse", "diamond", "triangle", "rtTriangle", "parallelogram", "trapezoid", "hexagon", "octagon", "plus", "star5"]; + ["rect", "roundRect", "ellipse", "diamond", "triangle", "rtTriangle", "parallelogram", "custom", "hexagon", "octagon", "plus", "star5"]; /// What one PowerPoint 95 sample shows, by its case name ([`powerpoint_95_samples`]). fn check_sample(name: &str, p: &Presentation) { @@ -1016,6 +1204,11 @@ fn check_sample(name: &str, p: &Presentation) { /// What one PowerPoint 95 shape sample shows, by its case name. fn check_shape_sample(name: &str, p: &Presentation) { let Some(shapes) = p.slides.first().map(|s| &s.shapes) else { return }; + let flips = |s: &Shape| (s.xfrm.unwrap().flip_h, s.xfrm.unwrap().flip_v); + // AddShape(n, 5760, 3960, 2880, 2880) in twips: 2 in square at 4 in, 2.75 in. + let square = (288.0, 198.0, 144.0, 144.0); + // AddShape(n, 1440, 1440, 2880, 1440) in twips. + let wide = (72.0, 72.0, 144.0, 72.0); match name { "base" => assert!(shapes.is_empty()), "shapes" => { @@ -1026,15 +1219,57 @@ fn check_shape_sample(name: &str, p: &Presentation) { let n: usize = name[5..].parse().unwrap(); let s = &shapes[0]; assert_eq!((shapes.len(), preset_of(s).0), (1, SHAPE_PRESETS[n - 1]), "{name}"); - assert_eq!(s.xfrm.unwrap().flip_v, n == 8, "{name}"); - // AddShape(n, 5760, 3960, 2880, 2880) in twips: 2 in square at 4 in, 2.75 in. - assert_eq!(boxed(s), (288.0, 198.0, 144.0, 144.0), "{name}"); + if n == 8 { + // Wide at the top, as PowerPoint 95 drew it, with no flip of its own. + assert_eq!(s.geom, trapezoid_95(5400)); + } + assert_eq!(flips(s), (false, false), "{name}"); + assert_eq!(boxed(s), square, "{name}"); } - "rect" | "fill" | "fill2" | "linecolor" | "nofill" | "shadow" | "rotate" | "flip" => { + "vflip" => { + // AddShape(5) is the isosceles triangle. + let s = &shapes[0]; + assert_eq!((shapes.len(), preset_of(s).0, boxed(s), flips(s)), (1, "triangle", wide, (false, true))); + } + "rectvflip" | "rthflip" | "rtvflip" | "rtboth" | "rtrot" | "trapvflip" | "traphflip" => { + let s = &shapes[0]; + let kind = match name { + "rectvflip" => "rect", + "trapvflip" | "traphflip" => "custom", + _ => "rtTriangle", + }; + assert_eq!((shapes.len(), preset_of(s).0, boxed(s)), (1, kind, square), "{name}"); + if kind == "custom" { + assert_eq!(s.geom, trapezoid_95(5400)); + } + let want = match name { + "rthflip" | "traphflip" => (true, false), + "rtboth" => (true, true), + "rtrot" => (false, false), + _ => (false, true), + }; + assert_eq!(flips(s), want, "{name}"); + assert_eq!(s.xfrm.unwrap().rot, if name == "rtrot" { 90.0 } else { 0.0 }, "{name}"); + } + "rot120wide" => { + let s = &shapes[0]; + assert_eq!((shapes.len(), preset_of(s).0, boxed(s), s.xfrm.unwrap().rot), (1, "rtTriangle", wide, 120.0)); + } + _ => check_drawn_sample(name, shapes), + } +} + +/// What one PowerPoint 95 sample of a rectangle's or line's look shows, by its case name. +fn check_drawn_sample(name: &str, shapes: &[Shape]) { + let flips = |s: &Shape| (s.xfrm.unwrap().flip_h, s.xfrm.unwrap().flip_v); + let black = rgb(0, 0, 0); + // AddShape(n, 1440, 1440, 2880, 1440) in twips. + let wide = (72.0, 72.0, 144.0, 72.0); + match name { + "rect" | "fill" | "fill2" | "linecolor" | "nofill" | "shadow" | "rotate" | "flip" | "lineoff" | "shadowcolor" | "rot90wide" => { let s = &shapes[0]; assert_eq!((shapes.len(), preset_of(s).0), (1, "rect"), "{name}"); - // AddShape(1, 1440, 1440, 2880, 1440) in twips. - assert_eq!(boxed(s), (72.0, 72.0, 144.0, 72.0), "{name}"); + assert_eq!(boxed(s), wide, "{name}"); let fill = match name { "fill" => Fill::solid(rgb(0xFF, 0, 0)), "fill2" => Fill::solid(rgb(0, 0xFF, 0)), @@ -1042,19 +1277,70 @@ fn check_shape_sample(name: &str, p: &Presentation) { _ => Fill::solid(rgb(0, 0xCC, 0x99)), }; assert_eq!(s.fill, Some(fill), "{name}"); - let line = if name == "linecolor" { rgb(0, 0, 0xFF) } else { rgb(0, 0, 0) }; - assert_eq!(s.line, Some(craft_pptx::Line::solid(line, 1.0)), "{name}"); - assert_eq!(s.effects.is_some(), name == "shadow", "{name}"); - let x = s.xfrm.unwrap(); - assert_eq!((x.rot, x.flip_h, x.flip_v), (if name == "rotate" { 30.0 } else { 0.0 }, name == "flip", false), "{name}"); + let line = match name { + "lineoff" => craft_pptx::Line::none(), + "linecolor" => craft_pptx::Line::solid(rgb(0, 0, 0xFF), 1.0), + _ => craft_pptx::Line::solid(black.clone(), 1.0), + }; + assert_eq!(s.line, Some(line), "{name}"); + let shadow = s.effects.as_ref().and_then(|e| e.outer_shadow.as_ref()).map(|sh| sh.color.clone()); + let want = match name { + // The scheme's shadow colour, and the red the object model set (BGR 255). + "shadow" => Some(rgb(0x96, 0x96, 0x96)), + "shadowcolor" => Some(rgb(0xFF, 0, 0)), + _ => None, + }; + assert_eq!(shadow, want, "{name}"); + let rot = match name { + "rotate" => 30.0, + "rot90wide" => 90.0, + _ => 0.0, + }; + assert_eq!((s.xfrm.unwrap().rot, flips(s)), (rot, (name == "flip", false)), "{name}"); + } + _ if name.starts_with("lstyle") || name.starts_with("dash") => { + let s = &shapes[0]; + assert_eq!((shapes.len(), preset_of(s).0, boxed(s)), (1, "rect", wide), "{name}"); + let line = s.line.clone().unwrap(); + let (width, dash) = match name { + "lstyle2" => (2.0, None), + "lstyle3" => (4.0, None), + "lstyle4" => (6.0, None), + "lstyle5" => (8.0, None), + "lstyle6" => (10.0, None), + "dash1" => (1.0, Some("sysDot")), + "dash2" => (1.0, Some("dash")), + "dash3" => (1.0, Some("lgDash")), + "dash4" => (1.0, Some("dashDot")), + _ => return, + }; + let dash = dash.map(|n| craft_pptx::style::Dash::Preset { name: n.into() }); + assert_eq!((line.width, line.dash, line.fill), (Some(width), dash, Some(Fill::solid(black))), "{name}"); } "line" => { let s = &shapes[0]; assert_eq!((shapes.len(), preset_of(s).0), (1, "line")); // AddLine(1440, 1440, 4320, 2880): from 1 in, 1 in to 4 in, 3 in. assert_eq!(boxed(s), (72.0, 72.0, 216.0, 144.0)); - assert_eq!((s.xfrm.unwrap().flip_h, s.xfrm.unwrap().flip_v), (false, false)); - assert_eq!(s.line, Some(craft_pptx::Line::solid(rgb(0, 0, 0), 1.0))); + assert_eq!(flips(s), (false, false)); + assert_eq!(s.line, Some(craft_pptx::Line::solid(black, 1.0))); + } + "linebase" | "arrow1" | "arrow2" | "arrow3" | "arrow4" => { + let s = &shapes[0]; + // AddLine(1440, 1440, 4320, 0): 3 in across from 1 in, 1 in. + assert_eq!((shapes.len(), preset_of(s).0, boxed(s), flips(s)), (1, "line", (72.0, 72.0, 216.0, 0.0), (false, false)), "{name}"); + let end = |kind: &str, size: &str| Some(craft_pptx::style::LineEnd { kind: kind.into(), w: size.into(), len: size.into() }); + let stealth = end("stealth", "med"); + let want = match name { + "arrow1" => (None, stealth), + "arrow2" => (stealth, None), + "arrow3" => (stealth.clone(), stealth), + "arrow4" => (None, end("triangle", "lg")), + _ => (None, None), + }; + let line = s.line.clone().unwrap(); + assert_eq!((line.head, line.tail), want, "{name}"); + assert_eq!((line.width, line.fill), (Some(1.0), Some(Fill::solid(black))), "{name}"); } _ => {} }