use crate::font::language::Language;
use crate::table::position::Position;
use std::path::Path;
pub struct TexFont {
pub font_family: String,
pub command: String,
pub language: Language,
}
impl TexFont {
#[allow(clippy::too_many_arguments)]
pub fn new<P: AsRef<Path>>(
path: P,
regular: &str,
italic: &Option<String>,
bold: &Option<String>,
bold_italic: &Option<String>,
position: Position,
language: Language,
used_languages: &mut Vec<Language>,
) -> Self {
const STYLES: [&str; 3] = ["ItalicFont", "BoldFont", "BoldItalicFont"];
let name = match &language {
Language::English => format!("{position}font"),
other => {
if used_languages.contains(&language) {
format!("{position}font")
} else {
format!("{other}font")
}
}
};
let mut font_family = format!(
"\\newfontfamily\\{}[Path={}/, Ligatures=TeX",
&name,
&path.as_ref().to_str().unwrap().replace("\\", "/")
);
let styles = [italic, bold, bold_italic]
.iter()
.zip(STYLES)
.filter_map(|(f, s)| f.as_ref().map(|f| format!("{}={}", s, f)))
.collect::<Vec<String>>()
.join(", ");
if !styles.is_empty() {
font_family.push_str(", ");
font_family.push_str(&styles);
}
font_family.push_str(&format!("]{{{}}}", regular));
let command = match &language {
Language::English => format!("\\{name}"),
other => {
if used_languages.contains(&language) {
format!("\\{other}font\\text{other}{{\\{name}")
} else {
format!("\\{name}\\text{other}{{")
}
}
};
if !used_languages.contains(&language) {
used_languages.push(language);
};
Self {
command,
font_family,
language,
}
}
}