#![cfg(feature = "html")]
use std::collections::BTreeMap;
use printpdf::*;
#[test]
fn multi_word_font_family_from_fonts_map_is_embedded() {
let mut fonts = BTreeMap::new();
fonts.insert(
"Roboto Medium".to_string(),
Base64OrRaw::Raw(std::fs::read("examples/assets/fonts/RobotoMedium.ttf").unwrap()),
);
let html = r#"<html><body>
<p style="font-family: 'Roboto Medium'; font-size: 24px;">Hello Probe Font</p>
</body></html>"#;
let mut warnings = Vec::new();
let doc = PdfDocument::from_html(
html,
&BTreeMap::new(),
&fonts,
&GeneratePdfOptions::default(),
&mut warnings,
)
.expect("from_html");
let bytes = doc.save(&PdfSaveOptions::default(), &mut warnings);
let parsed = lopdf::Document::load_mem(&bytes).expect("output parses");
let mut base_fonts = Vec::new();
for (_, obj) in parsed.objects.iter() {
if let lopdf::Object::Dictionary(d) = obj {
let is_font = d
.get(b"Type")
.and_then(|o| o.as_name())
.map(|n| n == b"Font")
.unwrap_or(false);
if is_font {
if let Ok(bf) = d.get(b"BaseFont").and_then(|o| o.as_name()) {
base_fonts.push(String::from_utf8_lossy(bf).to_string());
}
}
}
}
assert!(
base_fonts.iter().any(|f| f.contains("Roboto")),
"the supplied 'Roboto Medium' must be the embedded font, found: {base_fonts:?}"
);
}