use f_office_docx::testing::{self, WordParts as _};
use f_office_paint::{Item, Page};
use super::{Document, Options};
use crate::test_fonts::{CARLITO_FONTS, CARLITO_RUN, carlito};
const POINT: i64 = super::Length::UNITS_PER_POINT;
const DOT: i64 = super::Length::UNITS_PER_DOT;
const SECTION: &str = r#"<w:sectPr><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/></w:sectPr>"#;
fn docx(body: &str, comments: Option<&str>) -> Vec<u8> {
package(body, comments).zip().unwrap()
}
fn package(body: &str, comments: Option<&str>) -> testing::Builder {
let package = testing::docx(&format!("{body}{SECTION}"));
match comments {
Some(comments) => package
.word_part("rId1", "comments", "comments.xml", "comments", comments)
.word_part(
"rId2",
"styles",
"styles.xml",
"styles",
&format!(
r#"<w:style w:type="paragraph" w:styleId="BalloonText"><w:rPr>{CARLITO_FONTS}<w:sz w:val="16"/></w:rPr></w:style>"#
),
),
None => package,
}
}
fn runs(page: &Page) -> Vec<(String, i64, i64)> {
page.items()
.iter()
.filter_map(|item| match item {
Item::Glyphs(run) => run
.glyphs
.first()
.map(|g| (run.text.clone(), g.x.units(), g.y.units())),
Item::Rect(_) | Item::Path(_) | Item::Gradient(_) | Item::Image(_) | Item::Group(_) => {
None
}
})
.collect()
}
fn paragraph(text: &str, reference: Option<&str>) -> String {
let reference = reference.map_or(String::new(), |id| {
format!(r#"<w:r><w:commentReference w:id="{id}"/></w:r>"#)
});
format!("<w:p><w:r>{CARLITO_RUN}<w:t>{text}</w:t></w:r>{reference}</w:p>")
}
fn comment(id: &str, text: &str) -> String {
format!(
r#"<w:comment w:id="{id}" w:initials="AB"><w:p><w:r><w:annotationRef/></w:r><w:r>{CARLITO_RUN}<w:t>{text}</w:t></w:r></w:p></w:comment>"#
)
}
#[test]
fn prints_a_page_without_comment_references_at_full_size() {
let body = paragraph("plain", None);
let document = Document::open(
&docx(&body, Some(&comment("0", "unused"))),
&carlito(),
&Options::base(),
)
.unwrap()
.value;
let (_, x, _) = runs(&document.pages()[0])[0].clone();
assert_eq!(x, 72 * POINT);
}
fn two_comments() -> Page {
let body = paragraph("text", Some("0")) + ¶graph("more", Some("1"));
let comments = comment("0", "first") + &comment("1", "second");
let document = Document::open(&docx(&body, Some(&comments)), &carlito(), &Options::base())
.unwrap()
.value;
document.pages()[0].clone()
}
#[test]
fn prints_a_page_with_comments_smaller_beside_the_markup_area() {
let page = two_comments();
let body_x = runs(&page).iter().find(|r| r.0 == "text").unwrap().1;
assert_eq!(body_x, 4 * DOT + 72 * POINT * 224 / 300);
assert!(matches!(&page.items()[0], Item::Rect(r) if r.color.r == 242));
}
#[test]
fn puts_each_comment_in_a_balloon_with_its_label() {
let page = two_comments();
let runs = runs(&page);
let labels: Vec<&(String, i64, i64)> = runs
.iter()
.filter(|r| r.0.starts_with("Commented"))
.collect();
assert_eq!(labels.len(), 2, "{runs:?}");
assert_eq!(labels[0].0, "Commented [AB1]: ");
assert_eq!(labels[1].0, "Commented [AB2]: ");
assert_eq!(labels[0].1, 4 * DOT + (540 + 36) * POINT * 224 / 300);
assert!(labels[1].2 > labels[0].2);
let size = page.items().iter().find_map(|item| match item {
Item::Glyphs(run) if run.text == "first" => Some(run.size.units()),
_ => None,
});
assert_eq!(size, Some(33 * DOT * 224 / 300));
}
#[test]
fn hides_a_comment_whose_range_ends_outside_a_paragraph() {
let body = format!(
r#"{}<w:commentRangeEnd w:id="0"/>{}"#,
paragraph("text", None),
paragraph("more", Some("0")) + ¶graph("last", Some("1"))
);
let comments = comment("0", "hidden") + &comment("1", "shown");
let document = Document::open(&docx(&body, Some(&comments)), &carlito(), &Options::base())
.unwrap()
.value;
let labels: Vec<String> = runs(&document.pages()[0])
.into_iter()
.map(|r| r.0)
.filter(|t| t.starts_with("Commented"))
.collect();
assert_eq!(labels, ["Commented [AB2]: "]);
}
#[test]
fn draws_the_picture_of_a_comment_from_the_comments_part_relationships() {
use super::docx::{Drawing, Reason, Warning as DocxWarning};
let body = paragraph("text", Some("0"));
let comment = format!(
r#"<w:comment w:id="0" w:initials="AB">{}</w:comment>"#,
super::picture_tests::picture_paragraph("rId1", "")
);
let open = |linked: bool| {
let mut package = package(&body, Some(&comment));
if linked {
package = package
.relationship("word/comments.xml", "rId1", "image", "media/comment.png")
.part("word/media/comment.png", super::picture_tests::black_png());
}
let open = Document::open(&package.zip().unwrap(), &carlito(), &Options::base()).unwrap();
let page = &open.value.pages()[0];
let images = page
.items()
.iter()
.filter(|item| matches!(item, Item::Image(_)))
.count();
let skipped: Vec<_> = open
.warnings
.iter()
.filter_map(|(warning, _)| match warning {
super::Warning::Docx(DocxWarning::DrawingSkipped {
drawing: Drawing::Picture,
part,
reason,
}) => Some((part.to_string(), *reason)),
_ => None,
})
.collect();
(images, skipped)
};
assert_eq!(open(true), (1, Vec::new()));
assert_eq!(
open(false),
(0, vec![("word/comments.xml".to_owned(), Reason::Unlinked)])
);
}