use super::{
font_local_aliases, font_local_bold_aliases, known_bold_font_filenames, known_font_filenames,
korean_gothic_substitute,
};
fn original_font_files(font_name: &str, bold: bool) -> Vec<FontFileName> {
if bold || !matches!(font_name, "휴먼명조" | "HumanMyeongJo") {
return Vec::new();
}
let mut files = vec!["HMKMM.TTF".to_string()];
for name in ["휴먼명조", "HumanMyeongJo"] {
for ext in ["ttf", "otf", "ttc", "TTF"] {
files.push(format!("{name}.{ext}"));
}
}
files
.into_iter()
.filter_map(FontFileName::from_document_candidate)
.collect()
}
#[derive(Clone, Debug)]
pub(super) struct FontFileName(String);
impl FontFileName {
pub(super) fn from_document_candidate(candidate: String) -> Option<Self> {
use std::path::Component;
if candidate.contains(['/', '\\', '\0']) {
return None;
}
let mut components = std::path::Path::new(&candidate).components();
(matches!(components.next(), Some(Component::Normal(_))) && components.next().is_none())
.then_some(Self(candidate))
}
fn as_path(&self) -> &std::path::Path {
std::path::Path::new(&self.0)
}
}
pub(super) struct FontFileLookupPlan {
original_candidates: Vec<FontFileName>,
candidates: Vec<FontFileName>,
pub(super) search_dirs: Vec<std::path::PathBuf>,
custom_dirs: Vec<std::path::PathBuf>,
families: Vec<String>,
bold: bool,
}
pub(super) fn plan_svg_font_file_lookup(
font_name: &str,
extra_paths: &[std::path::PathBuf],
bold: bool,
) -> FontFileLookupPlan {
plan_svg_font_file_lookup_with_fallback(font_name, extra_paths, bold, true)
}
pub(super) fn plan_svg_font_file_lookup_with_fallback(
font_name: &str,
extra_paths: &[std::path::PathBuf],
bold: bool,
allow_generic_substitute: bool,
) -> FontFileLookupPlan {
let original_candidates = original_font_files(font_name, bold);
let candidates = {
let known_files = if bold {
known_bold_font_filenames(font_name)
} else {
known_font_filenames(font_name)
};
let mut files: Vec<String> = known_files.iter().map(|s| s.to_string()).collect();
let aliases = if bold {
font_local_bold_aliases(font_name)
} else {
font_local_aliases(font_name)
};
let mut names = vec![font_name.to_string()];
for a in &aliases {
names.push(a.to_string());
}
for name in &names {
let clean = name.replace(' ', "");
files.push(format!("{}.ttf", name));
files.push(format!("{}.otf", name));
files.push(format!("{}.ttc", name));
files.push(format!("{}.TTF", name));
if clean != *name {
files.push(format!("{}.ttf", clean));
files.push(format!("{}.otf", clean));
files.push(format!("{}.ttc", clean));
}
}
if !bold && allow_generic_substitute {
if let Some(sub) = korean_gothic_substitute(font_name) {
files.push(sub.to_string());
}
}
files
.into_iter()
.filter_map(FontFileName::from_document_candidate)
.collect()
};
FontFileLookupPlan {
original_candidates,
candidates,
search_dirs: crate::renderer::font_paths::search_dirs(extra_paths),
custom_dirs: crate::renderer::font_paths::custom_font_dirs(extra_paths),
families: std::iter::once(font_name.to_string())
.chain(
font_local_aliases(font_name)
.into_iter()
.map(str::to_string),
)
.collect(),
bold,
}
}
pub(super) fn find_font_file(plan: &FontFileLookupPlan) -> Option<std::path::PathBuf> {
for dir in &plan.search_dirs {
if !dir.exists() {
continue;
}
for candidate in &plan.original_candidates {
let path = dir.join(candidate.as_path());
if path.is_file() {
return Some(path);
}
}
}
for dir in &plan.custom_dirs {
for candidate in &plan.candidates {
let path = dir.join(candidate.as_path());
if path.is_file() {
return Some(path);
}
}
}
if let Some(path) = super::system_fonts::supplied_family_file_for_chars(
&plan.custom_dirs,
&plan.families,
plan.bold,
&std::collections::HashSet::new(),
) {
return Some(path);
}
if let Some(path) = super::system_fonts::family_file(&plan.families, plan.bold) {
return Some(path);
}
for dir in &plan.search_dirs {
for candidate in &plan.candidates {
let path = dir.join(candidate.as_path());
if path.is_file() {
return Some(path);
}
}
}
None
}
pub(super) fn find_font_file_for_chars(
plan: &FontFileLookupPlan,
chars: &std::collections::HashSet<char>,
) -> Option<std::path::PathBuf> {
let preferred = find_font_file(plan)?;
let covered = std::fs::read(&preferred).ok().is_some_and(|bytes| {
ttf_parser::Face::parse(&bytes, 0).is_ok_and(|face| {
chars
.iter()
.filter(|ch| !ch.is_whitespace() && !ch.is_control())
.all(|ch| face.glyph_index(*ch).is_some_and(|glyph| glyph.0 != 0))
})
});
if covered {
return Some(preferred);
}
super::system_fonts::supplied_family_file_for_chars(
&plan.custom_dirs,
&plan.families,
plan.bold,
chars,
)
.or_else(|| super::system_fonts::family_file_for_chars(&plan.families, plan.bold, chars))
.or(Some(preferred))
}