diff --git a/glyphs-reader/src/font.rs b/glyphs-reader/src/font.rs index a3f29e6c..d5df88c6 100644 --- a/glyphs-reader/src/font.rs +++ b/glyphs-reader/src/font.rs @@ -62,12 +62,12 @@ pub struct Font { // master id => { (name or class, name or class) => adjustment } pub kerning_ltr: Kerning, - pub custom_parameters: FontCustomParameters, + pub custom_parameters: CustomParameters, } /// Custom parameter options that can be set on a glyphs font #[derive(Clone, Debug, PartialEq, Hash, Default)] -pub struct FontCustomParameters { +pub struct CustomParameters { pub use_typo_metrics: Option, pub fs_type: Option, pub has_wws_names: Option, @@ -502,8 +502,8 @@ impl PlistParamsExt for Plist { impl RawCustomParameters { ////convert into the parsed params for a top-level font - fn to_font_params(&self) -> Result { - let mut params = FontCustomParameters::default(); + fn cook(&self) -> Result { + let mut params = CustomParameters::default(); let mut virtual_masters = Vec::>>::new(); // PANOSE custom parameter is accessible under a short name and a long name: // https://github.com/googlefonts/glyphsLib/blob/050ef62c/Lib/glyphsLib/builder/custom_params.py#L322-L323 @@ -591,14 +591,6 @@ impl RawCustomParameters { (item.disabled != Some(true)).then_some(&item.value) } - fn int(&self, name: &str) -> Option { - self.get(name).and_then(Plist::as_i64) - } - - fn float(&self, name: &str) -> Option> { - self.get(name).and_then(Plist::as_f64).map(OrderedFloat) - } - fn string(&self, name: &str) -> Option<&str> { self.get(name).and_then(Plist::as_str) } @@ -863,26 +855,7 @@ pub struct FontMaster { pub axes_values: Vec>, metric_values: BTreeMap, pub number_values: BTreeMap>, - pub typo_ascender: Option, - pub typo_descender: Option, - pub typo_line_gap: Option, - pub win_ascent: Option, - pub win_descent: Option, - pub hhea_ascender: Option, - pub hhea_descender: Option, - pub hhea_line_gap: Option, - pub underline_thickness: Option>, - pub underline_position: Option>, - pub strikeout_position: Option, - pub strikeout_size: Option, - pub subscript_x_offset: Option, - pub subscript_x_size: Option, - pub subscript_y_offset: Option, - pub subscript_y_size: Option, - pub superscript_x_offset: Option, - pub superscript_x_size: Option, - pub superscript_y_offset: Option, - pub superscript_y_size: Option, + pub custom_parameters: CustomParameters, } impl FontMaster { @@ -2291,7 +2264,7 @@ impl TryFrom for Font { let radix = if from.is_v2() { 16 } else { 10 }; - let mut custom_parameters = from.custom_parameters.to_font_params()?; + let mut custom_parameters = from.custom_parameters.cook()?; let glyph_order = make_glyph_order(&from.glyphs, custom_parameters.glyph_order.take()); let axes = from.axes.clone(); let instances: Vec<_> = from @@ -2372,47 +2345,31 @@ impl TryFrom for Font { let masters = from .font_master .into_iter() - .map(|m| FontMaster { - id: m.id, - name: m.name.unwrap_or_default(), - axes_values: m.axes_values, - metric_values: m - .metric_values - .into_iter() - .enumerate() - .filter_map(|(idx, value)| { - metric_names.get(&idx).map(|name| (name.clone(), value)) - }) - .filter(|(_, metric)| !metric.is_empty()) - .collect(), - number_values: from - .numbers - .iter() - .zip(m.number_values.iter()) - .map(|(k, v)| (k.name.clone(), *v)) - .collect(), - typo_ascender: m.custom_parameters.int("typoAscender"), - typo_descender: m.custom_parameters.int("typoDescender"), - typo_line_gap: m.custom_parameters.int("typoLineGap"), - win_ascent: m.custom_parameters.int("winAscent"), - win_descent: m.custom_parameters.int("winDescent"), - hhea_ascender: m.custom_parameters.int("hheaAscender"), - hhea_descender: m.custom_parameters.int("hheaDescender"), - hhea_line_gap: m.custom_parameters.int("hheaLineGap"), - underline_thickness: m.custom_parameters.float("underlineThickness"), - underline_position: m.custom_parameters.float("underlinePosition"), - strikeout_position: m.custom_parameters.int("strikeoutPosition"), - strikeout_size: m.custom_parameters.int("strikeoutSize"), - subscript_x_offset: m.custom_parameters.int("subscriptXOffset"), - subscript_x_size: m.custom_parameters.int("subscriptXSize"), - subscript_y_offset: m.custom_parameters.int("subscriptYOffset"), - subscript_y_size: m.custom_parameters.int("subscriptYSize"), - superscript_x_offset: m.custom_parameters.int("superscriptXOffset"), - superscript_x_size: m.custom_parameters.int("superscriptXSize"), - superscript_y_offset: m.custom_parameters.int("superscriptYOffset"), - superscript_y_size: m.custom_parameters.int("superscriptYSize"), + .map(|m| { + let custom_parameters = m.custom_parameters.cook()?; + Ok(FontMaster { + id: m.id, + name: m.name.unwrap_or_default(), + axes_values: m.axes_values, + metric_values: m + .metric_values + .into_iter() + .enumerate() + .filter_map(|(idx, value)| { + metric_names.get(&idx).map(|name| (name.clone(), value)) + }) + .filter(|(_, metric)| !metric.is_empty()) + .collect(), + number_values: from + .numbers + .iter() + .zip(m.number_values.iter()) + .map(|(k, v)| (k.name.clone(), *v)) + .collect(), + custom_parameters, + }) }) - .collect(); + .collect::>()?; let virtual_masters = custom_parameters.virtual_masters.take().unwrap_or_default(); Ok(Font { @@ -3249,8 +3206,9 @@ mod tests { #[test] fn read_typo_whatsits() { let font = Font::load(&glyphs2_dir().join("WghtVar_OS2.glyphs")).unwrap(); - assert_eq!(Some(1193), font.default_master().typo_ascender); - assert_eq!(Some(-289), font.default_master().typo_descender); + let master = font.default_master(); + assert_eq!(Some(1193), master.custom_parameters.typo_ascender); + assert_eq!(Some(-289), master.custom_parameters.typo_descender); } #[test] diff --git a/glyphs-reader/src/lib.rs b/glyphs-reader/src/lib.rs index 26f16572..ee5a8b8f 100644 --- a/glyphs-reader/src/lib.rs +++ b/glyphs-reader/src/lib.rs @@ -9,7 +9,7 @@ mod plist; mod propagate_anchors; pub use font::{ - Axis, Component, FeatureSnippet, Font, FontCustomParameters, FontMaster, Glyph, InstanceType, + Axis, Component, CustomParameters, FeatureSnippet, Font, FontMaster, Glyph, InstanceType, Layer, Node, NodeType, Path, Shape, }; pub use plist::Plist; diff --git a/glyphs2fontir/src/source.rs b/glyphs2fontir/src/source.rs index d3bfce9f..e531464b 100644 --- a/glyphs2fontir/src/source.rs +++ b/glyphs2fontir/src/source.rs @@ -27,7 +27,7 @@ use fontir::{ }; use glyphs_reader::{ glyphdata::{Category, Subcategory}, - Font, FontCustomParameters, InstanceType, + CustomParameters, Font, InstanceType, }; use ordered_float::OrderedFloat; use smol_str::SmolStr; @@ -124,7 +124,7 @@ impl GlyphsIrSource { version_minor: Default::default(), date: None, kerning_ltr: font.kerning_ltr.clone(), - custom_parameters: FontCustomParameters { + custom_parameters: CustomParameters { unicode_range_bits: None, codepage_range_bits: None, panose: None, @@ -556,6 +556,7 @@ impl Work for GlobalMetricWork { GlobalMetric::Os2WinDescent, pos.clone(), master + .custom_parameters .win_descent .or(font.custom_parameters.win_descent) .map(|v| v.abs() as f64), @@ -567,6 +568,7 @@ impl Work for GlobalMetricWork { set_metric!( $variant, master + .custom_parameters .$field_name .or(font.custom_parameters.$field_name) .map(|v| v as f64) @@ -577,6 +579,7 @@ impl Work for GlobalMetricWork { set_metric!( $variant, master + .custom_parameters .$field_name .or(font.custom_parameters.$field_name) .or(Some($fallback.into()))