From 4045ded6d8b831770b1c127146353b43baeb9768 Mon Sep 17 00:00:00 2001 From: Rod S Date: Tue, 26 Sep 2023 15:26:07 -0700 Subject: [PATCH 1/2] Filter out disabled features in glyphs files --- glyphs-reader/src/font.rs | 48 +++++++++++++++---- glyphs2fontir/src/toir.rs | 2 +- resources/testdata/glyphs2/Fea_Feature.glyphs | 5 ++ 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/glyphs-reader/src/font.rs b/glyphs-reader/src/font.rs index 727cc4635..fd8d380a5 100644 --- a/glyphs-reader/src/font.rs +++ b/glyphs-reader/src/font.rs @@ -70,11 +70,21 @@ pub struct Font { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct FeatureSnippet(String); +pub struct FeatureSnippet { + pub content: String, + pub disabled: bool, +} impl FeatureSnippet { - pub fn as_str(&self) -> &str { - self.0.as_str() + pub fn new(content: String, disabled: bool) -> Self { + FeatureSnippet { content, disabled } + } + + pub fn str_if_enabled(&self) -> Option<&str> { + match self.disabled { + true => None, + false => Some(&self.content), + } } } @@ -154,6 +164,7 @@ pub struct RawNameValue { #[derive(Clone, Debug, FromPlist, PartialEq, Eq, Hash)] pub struct RawFeature { pub automatic: Option, + pub disabled: Option, pub name: Option, pub tag: Option, pub code: String, @@ -1397,6 +1408,10 @@ impl RawFeature { ))), } } + + fn disabled(&self) -> bool { + self.disabled.map(|v| v == 1).unwrap_or_default() + } } // https://github.com/googlefonts/glyphsLib/blob/24b4d340e4c82948ba121dcfe563c1450a8e69c9/Lib/glyphsLib/builder/features.py#L90 @@ -1406,7 +1421,7 @@ fn prefix_to_feature(prefix: RawFeature) -> Result { None => "", }; let code = format!("# Prefix: {}\n{}{}", name, prefix.autostr(), prefix.code); - Ok(FeatureSnippet(code)) + Ok(FeatureSnippet::new(code, prefix.disabled())) } // https://github.com/googlefonts/glyphsLib/blob/24b4d340e4c82948ba121dcfe563c1450a8e69c9/Lib/glyphsLib/builder/features.py#L101 @@ -1419,7 +1434,7 @@ fn class_to_feature(feature: RawFeature) -> Result { name, feature.code ); - Ok(FeatureSnippet(code)) + Ok(FeatureSnippet::new(code, feature.disabled())) } // https://github.com/googlefonts/glyphsLib/blob/24b4d340e4c82948ba121dcfe563c1450a8e69c9/Lib/glyphsLib/builder/features.py#L113 @@ -1431,7 +1446,7 @@ fn raw_feature_to_feature(feature: RawFeature) -> Result feature.autostr(), feature.code ); - Ok(FeatureSnippet(code)) + Ok(FeatureSnippet::new(code, feature.disabled())) } /// @@ -2168,7 +2183,10 @@ mod tests { concat!("# automatic\n", "@Uppercase = [ A B C\n", "];",), concat!("@Lowercase = [ a b c\n", "];",), ], - font.features.iter().map(|f| f.as_str()).collect::>() + font.features + .iter() + .filter_map(|f| f.str_if_enabled()) + .collect::>() ) } @@ -2186,7 +2204,10 @@ mod tests { ), concat!("# Prefix: \n# automatic\nthanks for all the fish;",), ], - font.features.iter().map(|f| f.as_str()).collect::>() + font.features + .iter() + .filter_map(|f| f.str_if_enabled()) + .collect::>() ) } @@ -2212,7 +2233,10 @@ mod tests { "} ccmp;", ), ], - font.features.iter().map(|f| f.as_str()).collect::>() + font.features + .iter() + .filter_map(|f| f.str_if_enabled()) + .collect::>() ) } @@ -2225,7 +2249,10 @@ mod tests { "# Prefix: second\nmeh", "feature third {\nmeh\n} third;", ], - font.features.iter().map(|f| f.as_str()).collect::>() + font.features + .iter() + .filter_map(|f| f.str_if_enabled()) + .collect::>() ) } @@ -2235,6 +2262,7 @@ mod tests { name: None, tag: Some("aalt".to_string()), automatic: None, + disabled: None, code: "blah".to_string(), other_stuff: BTreeMap::new(), }; diff --git a/glyphs2fontir/src/toir.rs b/glyphs2fontir/src/toir.rs index 1f00ac7a8..bc1136ab2 100644 --- a/glyphs2fontir/src/toir.rs +++ b/glyphs2fontir/src/toir.rs @@ -117,7 +117,7 @@ pub(crate) fn to_ir_features(features: &[FeatureSnippet]) -> Result = features.iter().map(|f| f.as_str().to_string()).collect(); + let fea_snippets: Vec<_> = features.iter().filter_map(|f| f.str_if_enabled()).collect(); Ok(ir::Features::Memory { fea_content: fea_snippets.join("\n\n"), include_dir: None, diff --git a/resources/testdata/glyphs2/Fea_Feature.glyphs b/resources/testdata/glyphs2/Fea_Feature.glyphs index 9ceca3608..420cfe0dd 100644 --- a/resources/testdata/glyphs2/Fea_Feature.glyphs +++ b/resources/testdata/glyphs2/Fea_Feature.glyphs @@ -30,6 +30,11 @@ code = "lookup ccmp_Other_2 { etc;"; name = ccmp; +}, +{ +disabled = 1; +code = "IGNORE_ME"; +name = duck; } ); familyName = Fea; From f7d7f841c4501c71b1ae216f98102d0653c9e149 Mon Sep 17 00:00:00 2001 From: Rod S Date: Wed, 27 Sep 2023 12:12:38 -0700 Subject: [PATCH 2/2] Code review tweaks --- glyphs-reader/src/font.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/glyphs-reader/src/font.rs b/glyphs-reader/src/font.rs index fd8d380a5..be304b101 100644 --- a/glyphs-reader/src/font.rs +++ b/glyphs-reader/src/font.rs @@ -81,10 +81,7 @@ impl FeatureSnippet { } pub fn str_if_enabled(&self) -> Option<&str> { - match self.disabled { - true => None, - false => Some(&self.content), - } + (!self.disabled).then_some(&self.content) } } @@ -1410,7 +1407,7 @@ impl RawFeature { } fn disabled(&self) -> bool { - self.disabled.map(|v| v == 1).unwrap_or_default() + self.disabled == Some(1) } }