#![cfg(feature = "html")]
use std::collections::BTreeMap;
use printpdf::*;
fn page(note: bool) -> String {
let footnote = if note {
r#"<span style="float: footnote; font-size: 9pt;">The note of this page.</span>"#
} else {
""
};
format!(
r#"<html><head><style>
::footnote-call {{ content: none; }}
::footnote-marker {{ content: none; }}
</style></head><body style="font-size: 12pt;">
<p>A short paragraph at the top of the page{footnote} and nothing else below it.</p>
</body></html>"#
)
}
fn lowest_baseline(html: &str) -> (usize, f32) {
let mut warnings = Vec::new();
let doc = PdfDocument::from_html(
html,
&BTreeMap::new(),
&BTreeMap::new(),
&GeneratePdfOptions::default(),
&mut warnings,
)
.expect("the page renders");
let lowest = doc.pages[0]
.ops
.iter()
.filter_map(|op| match op {
Op::SetTextMatrix { matrix } => Some(matrix.as_array()[5]),
_ => None,
})
.fold(f32::MAX, f32::min);
(doc.pages.len(), lowest)
}
#[test]
fn a_footnote_is_printed_at_the_foot_of_the_page_of_its_reference() {
let (pages, plain) = lowest_baseline(&page(false));
assert_eq!(pages, 1);
assert!(plain > 420.0, "a short paragraph stands in the upper half of the page, lowest baseline at {plain}pt");
let (pages, with_note) = lowest_baseline(&page(true));
assert_eq!(pages, 1, "the note is on the page of its reference, not on one of its own");
assert!(
with_note < 150.0,
"the note is printed at the foot of the page, but the lowest text is at {with_note}pt from the bottom"
);
}