Skip to content

Commit

Permalink
Merge d3d5619 into 30bec80
Browse files Browse the repository at this point in the history
  • Loading branch information
madig authored Dec 23, 2020
2 parents 30bec80 + d3d5619 commit e49acab
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
64 changes: 64 additions & 0 deletions examples/normalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! A little tool for normalizing UFOs.
//!
//! It will scrub layer and lib data of a UFO in an opinionated way, as done
//! in the Cantarell font project, to show a real-world script.
//!
//! Call like `cargo run --release --example normalize some.ufo another.ufo`.

fn main() {
for arg in std::env::args().skip(1) {
let mut ufo = match norad::Ufo::load(&arg) {
Ok(v) => v,
Err(e) => {
eprintln!("Loading UFO failed: {}", e);
std::process::exit(1);
}
};

// Prune all non-foreground layers.
ufo.layers.retain(|l| l.name == "public.default");

// Prune the foreground layer's lib.
let default_layer = ufo.get_default_layer_mut().unwrap();
default_layer.info.lib.as_mut().and_then(|lib| {
Some(lib.retain(|k, &mut _| {
k.starts_with("public.") || k.starts_with("com.schriftgestaltung.layerId")
}))
});

// Prune all glyphs' libs.
for glyph in default_layer.iter_contents_mut() {
glyph.lib.as_mut().and_then(|lib| {
Some(lib.retain(|k, &mut _| {
(k.starts_with("public.")
|| k.starts_with("com.schriftgestaltung.")
|| k == "com.schriftgestaltung.componentsAlignment")
&& k != "public.markColor"
}))
});
}

// Prune the UFO lib.
ufo.lib.as_mut().and_then(|lib| {
Some(lib.retain(|k, &mut _| {
k.starts_with("public.")
|| k.starts_with("com.github.googlei18n.ufo2ft.")
|| k == "com.schriftgestaltung.appVersion"
|| k == "com.schriftgestaltung.fontMasterID"
|| k == "com.schriftgestaltung.customParameter.GSFont.disablesLastChange"
|| k == "com.schriftgestaltung.customParameter.GSFontMaster.paramArea"
|| k == "com.schriftgestaltung.customParameter.GSFontMaster.paramDepth"
|| k == "com.schriftgestaltung.customParameter.GSFontMaster.paramOver"
}))
});

ufo.meta.creator = "org.linebender.norad".to_string();
match ufo.save(arg) {
Err(e) => {
eprintln!("Saving UFO failed: {}", e);
std::process::exit(1);
}
_ => {}
};
}
}
4 changes: 3 additions & 1 deletion src/glyph/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl Glyph {
}

if let Some(lib) = self.lib.as_ref() {
write_lib_section(lib, &mut writer)?;
if !lib.is_empty() {
write_lib_section(lib, &mut writer)?;
}
}

writer.write_event(Event::End(BytesEnd::borrowed(b"glyph")))?;
Expand Down
6 changes: 5 additions & 1 deletion src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static LAYER_INFO_FILE: &str = "layerinfo.plist";
pub struct Layer {
pub(crate) glyphs: BTreeMap<GlyphName, Arc<Glyph>>,
contents: BTreeMap<GlyphName, PathBuf>,
info: LayerInfo,
pub info: LayerInfo,
}

impl Layer {
Expand Down Expand Up @@ -149,6 +149,10 @@ impl Layer {
self.glyphs.values().map(Arc::clone)
}

pub fn iter_contents_mut(&mut self) -> impl Iterator<Item = &mut Glyph> {
self.glyphs.values_mut().map(Arc::make_mut)
}

#[cfg(test)]
pub fn get_path(&self, name: &str) -> Option<&Path> {
self.contents.get(name).map(PathBuf::as_path)
Expand Down

0 comments on commit e49acab

Please sign in to comment.