use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use base64::Engine;
use super::{
escape_css_font_name, find_font_file, font_data_uri_format, plan_svg_font_file_lookup,
svg_outline_font_data, FontEmbedMode, SvgRenderer,
};
pub(super) fn append_missing_serif_font_css(
css: &mut String,
renderer: &SvgRenderer,
font_paths: &[PathBuf],
embedded_fonts: &HashMap<String, Vec<u8>>,
) {
const FALLBACK: &str = "Haansoft Batang";
if renderer.font_embed_mode == FontEmbedMode::None
|| renderer.font_codepoints().contains_key(FALLBACK)
{
return;
}
let mut missing = HashSet::new();
for (family, chars) in renderer.font_codepoints() {
if !crate::renderer::generic_fallback(family).ends_with(",serif") {
continue;
}
let bytes = embedded_fonts.get(family).cloned().or_else(|| {
find_font_file(&plan_svg_font_file_lookup(family, font_paths, false))
.and_then(|path| std::fs::read(path).ok())
});
let Some(bytes) = bytes else { continue };
let Ok(face) = ttf_parser::Face::parse(&bytes, 0) else {
continue;
};
missing.extend(
chars
.iter()
.copied()
.filter(|ch| !ch.is_whitespace() && face.glyph_index(*ch).is_none()),
);
}
if missing.is_empty() {
return;
}
let Some(path) = find_font_file(&plan_svg_font_file_lookup(FALLBACK, font_paths, false)) else {
return;
};
let Ok(bytes) = std::fs::read(path) else {
return;
};
let Ok(face) = ttf_parser::Face::parse(&bytes, 0) else {
return;
};
missing.retain(|ch| face.glyph_index(*ch).is_some());
if missing.is_empty() {
return;
}
let bytes = svg_outline_font_data(&bytes, &missing);
let (mime, format) = font_data_uri_format(&bytes);
let b64 = base64::engine::general_purpose::STANDARD.encode(&bytes);
css.push_str(&format!(
"@font-face {{ font-family: \"{}\"; src: url(\"data:{};base64,{}\") format(\"{}\"); }}\n",
escape_css_font_name(FALLBACK),
mime,
b64,
format,
));
}