use fontspector_hotfix::{Testable, apply_hotfixes};
use crate::GftoolsError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IncludeSourceFixes {
Yes,
#[default]
No,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Interactive {
Yes,
#[default]
No,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FixFvarTable {
#[default]
Yes,
No,
}
pub fn fix_font(
font_path: &str,
output_path: &str,
include_source_fixes: IncludeSourceFixes,
interactive: Interactive,
fix_fvar_table: FixFvarTable,
) -> Result<(), GftoolsError> {
let mut font = Testable::new(font_path).expect("Failed to load font");
let mut check_ids = vec![
"integer_ppem_if_hinted",
"googlefonts/gasp",
"googlefonts/metadata/valid_nameid25",
"googlefonts/color_fonts",
"empty_glyph_on_gid1_for_colrv0",
"opentype/caret_slope",
];
if let IncludeSourceFixes::Yes = include_source_fixes {
check_ids.extend([
"unwanted_tables",
"googlefonts/font_names",
"googlefonts/fstype",
"googlefonts/use_typo_metrics",
"opentype/fsselection",
"opentype/mac_style",
"googlefonts/weightclass",
"opentype/italic_angle",
]);
}
if let FixFvarTable::Yes = fix_fvar_table {
check_ids.push("googlefonts/fvar_instances");
}
let check_ids: Vec<String> = check_ids.into_iter().map(String::from).collect();
apply_hotfixes(
&mut font,
&check_ids,
matches!(interactive, Interactive::Yes),
)
.map_err(|_| GftoolsError::Misc("Failed to apply hotfixes".to_string()))?;
std::fs::write(output_path, &font.contents)?;
Ok(())
}