From 219bf54cdd5c7f4c28a582d56c0a6faed3bb0f7a Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 17 May 2022 17:37:02 -0400 Subject: [PATCH] improve font stripping --- build.rs | 80 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/build.rs b/build.rs index 41d4fc4..5c3449c 100644 --- a/build.rs +++ b/build.rs @@ -15,6 +15,43 @@ use run_script::ScriptOptions; include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/data.rs")); +fn font_stripper(from: &str, out: &str, unicodes: &[&str]) -> Result, String> { + let new_path = [&env::var("OUT_DIR").unwrap(), out].concat(); + let unicodes_formatted = unicodes + .iter() + .map(|u| format!("U+{}", u)) + .collect::>() + .join(","); + + let script_result = run_script::run( + &format!( + " + pyftsubset {}/assets/{} --unicodes={} + mv {}/assets/{} {} + ", + env!("CARGO_MANIFEST_DIR"), + from, + unicodes_formatted, + env!("CARGO_MANIFEST_DIR"), + from.replace(".ttf", ".subset.ttf"), + new_path + ), + &(vec![]), + &ScriptOptions::new(), + ); + + if let Ok((_, _, error)) = script_result { + if error.is_empty() { + return Ok(std::fs::read(new_path).unwrap()); + } else { + return Err(error); + } + } else if let Err(error) = script_result { + return Err(error.to_string()); + } + unreachable!() +} + fn main() { // rebuild if new commit or contents of `assets` folder changed println!("cargo:rerun-if-changed=.git/logs/HEAD"); @@ -24,40 +61,6 @@ fn main() { // let font_hack = FontData::from_static(include_bytes!("assets/Hack-Regular.ttf")); let font_ubuntu_light = FontData::from_static(include_bytes!("assets/Ubuntu-Light.ttf")); - let new_noto_path = [&env::var("OUT_DIR").unwrap(), "/noto-emoji.ttf"].concat(); - let new_emoji_icon_path = [&env::var("OUT_DIR").unwrap(), "/emoji-icon.ttf"].concat(); - - let (_code, _output, error) = run_script::run( - &format!( - " - pyftsubset {}/assets/NotoEmoji-Regular.ttf --unicodes=U+1F31E,U+1F319,U+2716 - mv {}/assets/NotoEmoji-Regular.subset.ttf {} - ", - env!("CARGO_MANIFEST_DIR"), - env!("CARGO_MANIFEST_DIR"), - new_noto_path - ), - &(vec![]), - &ScriptOptions::new(), - ) - .unwrap(); - assert_eq!(error, String::new()); - - let (_code, _output, error) = run_script::run( - &format!( - " - pyftsubset {}/assets/emoji-icon-font.ttf --unicodes=U+2699 - mv {}/assets/emoji-icon-font.subset.ttf {} - ", - env!("CARGO_MANIFEST_DIR"), - env!("CARGO_MANIFEST_DIR"), - new_emoji_icon_path - ), - &(vec![]), - &ScriptOptions::new(), - ) - .unwrap(); - assert_eq!(error, String::new()); let fonts = FontDefinitions { font_data: BTreeMap::from([ @@ -66,13 +69,18 @@ fn main() { ( "NotoEmoji-Regular".to_owned(), FontData::from_owned( - std::fs::read(new_noto_path).expect("unable to read noto emoji font"), + font_stripper( + "NotoEmoji-Regular.ttf", + "noto-emoji.ttf", + &["1F31E", "1F319", "2716"], + ) + .unwrap(), ), ), ( "emoji-icon-font".to_owned(), FontData::from_owned( - std::fs::read(new_emoji_icon_path).expect("unable to read emoji icon font"), + font_stripper("emoji-icon-font.ttf", "emoji-icon.ttf", &["2699"]).unwrap(), ) .tweak(FontTweak { scale: 0.8,