use f_office_units::Length;
use f_office_warnings::{Name, Warnings};
use f_office_xml::{Document, Limits};
use super::{ShadowOffset, effects, picture_effects};
use crate::{A, Feature, Warning};
fn features_of(
props: &str,
read: fn(&Document, f_office_xml::NodeId, &mut Warnings<Warning>),
) -> Vec<Feature> {
let doc = Document::parse(
format!(r#"<a:spPr xmlns:a="{A}">{props}</a:spPr>"#).as_bytes(),
&Limits::default(),
)
.unwrap();
let mut warnings = Warnings::new();
read(&doc, doc.root().unwrap(), &mut warnings);
warnings
.iter()
.map(|(warning, _)| match warning {
Warning::FeatureNotSupported { feature } => *feature,
other => panic!("{other}"),
})
.collect()
}
#[test]
fn warns_once_of_each_effect_and_of_a_3d_look_and_not_of_a_flat_or_empty_one() {
let cases: [(&str, &[Feature]); 9] = [
("<a:effectLst/>", &[]),
(
r#"<a:effectLst><a:outerShdw blurRad="40000"/><a:innerShdw/><a:glow rad="1"/><a:softEdge rad="1"/><a:reflection/><a:blur/></a:effectLst>"#,
&[
Feature::Shadow,
Feature::Glow,
Feature::SoftEdge,
Feature::Reflection,
Feature::OtherEffect,
],
),
("<a:effectDag/>", &[]),
(
"<a:effectDag><a:cont/></a:effectDag>",
&[Feature::OtherEffect],
),
(
r#"<a:scene3d><a:camera prst="orthographicFront"><a:rot lat="0" lon="0" rev="0"/></a:camera><a:lightRig rig="threePt" dir="t"/></a:scene3d><a:sp3d extrusionH="0"><a:bevelT w="0" h="0"/></a:sp3d>"#,
&[],
),
(
r#"<a:scene3d><a:camera prst="perspectiveFront"/><a:lightRig rig="threePt" dir="t"/></a:scene3d>"#,
&[Feature::ThreeD],
),
(
r#"<a:scene3d><a:camera prst="orthographicFront"><a:rot lat="0" lon="1200000" rev="0"/></a:camera><a:lightRig rig="threePt" dir="t"/></a:scene3d>"#,
&[Feature::ThreeD],
),
("<a:sp3d><a:bevelT/></a:sp3d>", &[Feature::ThreeD]),
(r#"<a:sp3d contourW="12700"/>"#, &[Feature::ThreeD]),
];
for (props, want) in cases {
let read = |doc: &Document, props, warnings: &mut Warnings<Warning>| {
effects(doc, props, warnings);
};
assert_eq!(features_of(props, read), want, "{props}");
}
}
#[test]
fn warns_of_a_3d_value_that_does_not_read_and_counts_it_as_zero() {
let warned = |props: &str| {
let doc = Document::parse(
format!(r#"<a:spPr xmlns:a="{A}">{props}</a:spPr>"#).as_bytes(),
&Limits::default(),
)
.unwrap();
let mut warnings = Warnings::new();
effects(&doc, doc.root().unwrap(), &mut warnings);
warnings
.iter()
.map(|(warning, _)| warning.clone())
.collect::<Vec<_>>()
};
let invalid = |element: &str, attribute: &str| Warning::InvalidValue {
element: Name::new(element),
attribute: Name::new(attribute),
};
let three_d = Warning::FeatureNotSupported {
feature: Feature::ThreeD,
};
assert_eq!(
warned(r#"<a:sp3d><a:bevelT w="x" h="0"/></a:sp3d>"#),
[invalid("bevelT", "w")]
);
assert_eq!(
warned(r#"<a:sp3d><a:bevelT w="x"/></a:sp3d>"#),
[invalid("bevelT", "w"), three_d]
);
assert_eq!(
warned(r#"<a:sp3d extrusionH="1.5"/>"#),
[invalid("sp3d", "extrusionH")]
);
assert_eq!(
warned(
r#"<a:scene3d><a:camera prst="orthographicFront"><a:rot lat="x"/></a:camera></a:scene3d>"#
),
[invalid("rot", "lat")]
);
}
#[test]
fn reads_supported_picture_color_effects_and_warns_only_of_unsupported_ones() {
let cases: [(&str, &[Feature], usize); 8] = [
(
r#"<a:alphaModFix amt="50000"/><a:grayscl/><a:extLst/>"#,
&[],
1,
),
(r#"<a:lum bright="0" contrast="0%"/>"#, &[], 0),
(r#"<a:lum bright="20000"/>"#, &[], 1),
(
r#"<a:biLevel thresh="50000"/><a:duotone><a:srgbClr val="000000"/><a:srgbClr val="FFFFFF"/></a:duotone>"#,
&[],
2,
),
(
r#"<a:clrChange><a:clrFrom><a:srgbClr val="FFFFFF"/></a:clrFrom><a:clrTo><a:srgbClr val="000000"/></a:clrTo></a:clrChange>"#,
&[],
1,
),
(
r#"<a:duotone/><a:biLevel thresh="50000"/>"#,
&[Feature::PictureEffect],
1,
),
(
"<a:clrChange><a:clrFrom/><a:clrTo/></a:clrChange>",
&[Feature::PictureEffect],
0,
),
(
r#"<a:tint hue="0" amt="50000"/>"#,
&[Feature::PictureEffect],
0,
),
];
for (blip, want, count) in cases {
let doc = Document::parse(
format!(r#"<a:blip xmlns:a="{A}">{blip}</a:blip>"#).as_bytes(),
&Limits::default(),
)
.unwrap();
let mut warnings = Warnings::new();
let parsed = picture_effects(&doc, doc.root().unwrap(), &mut warnings);
let got: Vec<Feature> = warnings
.iter()
.map(|(warning, _)| match warning {
Warning::FeatureNotSupported { feature } => *feature,
other => panic!("{other}"),
})
.collect();
assert_eq!(got, want, "{blip}");
assert_eq!(parsed.len(), count, "{blip}");
}
}
fn shadow_of(props: &str) -> (Option<super::OuterShadow>, Vec<Feature>) {
let doc = Document::parse(
format!(r#"<a:spPr xmlns:a="{A}">{props}</a:spPr>"#).as_bytes(),
&Limits::default(),
)
.unwrap();
let mut warnings = Warnings::new();
let shadow = effects(&doc, doc.root().unwrap(), &mut warnings);
let features = warnings
.iter()
.map(|(warning, _)| match warning {
Warning::FeatureNotSupported { feature } => *feature,
other => panic!("{other}"),
})
.collect();
(shadow, features)
}
#[test]
fn gives_an_outer_shadow_of_the_shape_s_size_and_warns_of_the_other_effects() {
let (shadow, features) = shadow_of(
r#"<a:effectLst><a:outerShdw blurRad="50800" dist="38100" dir="2700000" algn="tl" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="43000"/></a:srgbClr></a:outerShdw><a:glow rad="1"><a:srgbClr val="FF0000"/></a:glow></a:effectLst>"#,
);
let shadow = shadow.unwrap();
assert_eq!(features, [Feature::Glow]);
assert_eq!(shadow.blur, Length::from_emus(50_800).unwrap());
assert_eq!(shadow.distance, Length::from_emus(38_100).unwrap());
assert_eq!(shadow.direction, 2_700_000);
assert!(!shadow.turns_with_shape());
let part = Length::from_units(53_882);
assert_eq!(
shadow.offset(),
ShadowOffset {
across: part,
down: part
}
);
}
#[test]
fn moves_a_shadow_along_its_direction_clockwise_from_the_right() {
let offset = |direction: i64| {
let props = format!(
r#"<a:effectLst><a:outerShdw dist="12700" dir="{direction}"><a:prstClr val="black"/></a:outerShdw></a:effectLst>"#
);
shadow_of(&props).0.unwrap().offset()
};
let (zero, point) = (Length::ZERO, Length::from_units(Length::UNITS_PER_POINT));
let at = |across: Length, down: Length| ShadowOffset { across, down };
assert_eq!(offset(0), at(point, zero));
assert_eq!(offset(5_400_000), at(zero, point));
assert_eq!(offset(16_200_000), at(zero, zero - point));
}
#[test]
fn warns_of_a_scaled_or_skewed_shadow_or_one_without_a_color() {
for shadow in [
r#"<a:outerShdw sx="90000"><a:prstClr val="black"/></a:outerShdw>"#,
r#"<a:outerShdw kx="600000"><a:prstClr val="black"/></a:outerShdw>"#,
r#"<a:outerShdw blurRad="40000"/>"#,
] {
let (found, features) = shadow_of(&format!("<a:effectLst>{shadow}</a:effectLst>"));
assert_eq!((found, features), (None, vec![Feature::Shadow]), "{shadow}");
}
}
fn parse_shape_reflection(props: &str) -> (Option<super::Reflection>, Vec<Feature>) {
let doc = Document::parse(
format!(r#"<a:spPr xmlns:a="{A}">{props}</a:spPr>"#).as_bytes(),
&Limits::default(),
)
.unwrap();
let mut warnings = Warnings::new();
let (_, _, refl) = super::shape_effects_of(&doc, doc.root().unwrap(), &mut warnings);
let features = warnings
.iter()
.map(|(w, _)| match w {
Warning::FeatureNotSupported { feature } => *feature,
other => panic!("{other}"),
})
.collect();
(refl, features)
}
#[test]
fn reads_supported_downward_reflection() {
let (refl, features) = parse_shape_reflection(
r#"<a:effectLst><a:reflection blurRad="12700" stA="38000" endPos="28000" dist="5000" dir="5400000" sy="-100000" algn="bl" rotWithShape="0"/></a:effectLst>"#,
);
assert_eq!(features, []);
let refl = refl.unwrap();
assert_eq!(
(
refl.blur,
refl.distance,
refl.start_alpha,
refl.start_pos,
refl.end_alpha,
refl.end_pos,
),
(
Length::from_emus(12_700).unwrap(),
Length::from_emus(5_000).unwrap(),
38_000,
0,
0,
28_000,
)
);
let empty = parse_shape_reflection(
r#"<a:effectLst><a:reflection stA="0" endA="0" sy="-100000" algn="bl"/></a:effectLst>"#,
);
assert_eq!(empty, (None, vec![]));
}
#[test]
fn warns_on_unsupported_reflection() {
for xml in [
r#"<a:reflection sy="100000" algn="bl"/>"#,
r#"<a:reflection sy="-100000" kx="600000" algn="bl"/>"#,
r#"<a:reflection sy="-100000" dist="12700" dir="0" algn="bl"/>"#,
] {
let got = parse_shape_reflection(&format!("<a:effectLst>{xml}</a:effectLst>"));
assert_eq!(got, (None, vec![Feature::Reflection]), "{xml}");
}
}