Skip to content

Commit

Permalink
[glyphs] Use same CustomParameters for font and master
Browse files Browse the repository at this point in the history
These overlap significantly, and this seems much cleaner than having
largely repetetive special cases for each.
  • Loading branch information
cmyr committed Nov 26, 2024
1 parent 41af04e commit 23146c9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 78 deletions.
108 changes: 33 additions & 75 deletions glyphs-reader/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
pub fs_type: Option<u16>,
pub has_wws_names: Option<bool>,
Expand Down Expand Up @@ -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<FontCustomParameters, Error> {
let mut params = FontCustomParameters::default();
fn cook(&self) -> Result<CustomParameters, Error> {
let mut params = CustomParameters::default();
let mut virtual_masters = Vec::<BTreeMap<String, OrderedFloat<f64>>>::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
Expand Down Expand Up @@ -591,14 +591,6 @@ impl RawCustomParameters {
(item.disabled != Some(true)).then_some(&item.value)
}

fn int(&self, name: &str) -> Option<i64> {
self.get(name).and_then(Plist::as_i64)
}

fn float(&self, name: &str) -> Option<OrderedFloat<f64>> {
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)
}
Expand Down Expand Up @@ -863,26 +855,7 @@ pub struct FontMaster {
pub axes_values: Vec<OrderedFloat<f64>>,
metric_values: BTreeMap<String, RawMetricValue>,
pub number_values: BTreeMap<SmolStr, OrderedFloat<f64>>,
pub typo_ascender: Option<i64>,
pub typo_descender: Option<i64>,
pub typo_line_gap: Option<i64>,
pub win_ascent: Option<i64>,
pub win_descent: Option<i64>,
pub hhea_ascender: Option<i64>,
pub hhea_descender: Option<i64>,
pub hhea_line_gap: Option<i64>,
pub underline_thickness: Option<OrderedFloat<f64>>,
pub underline_position: Option<OrderedFloat<f64>>,
pub strikeout_position: Option<i64>,
pub strikeout_size: Option<i64>,
pub subscript_x_offset: Option<i64>,
pub subscript_x_size: Option<i64>,
pub subscript_y_offset: Option<i64>,
pub subscript_y_size: Option<i64>,
pub superscript_x_offset: Option<i64>,
pub superscript_x_size: Option<i64>,
pub superscript_y_offset: Option<i64>,
pub superscript_y_size: Option<i64>,
pub custom_parameters: CustomParameters,
}

impl FontMaster {
Expand Down Expand Up @@ -2291,7 +2264,7 @@ impl TryFrom<RawFont> 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
Expand Down Expand Up @@ -2372,47 +2345,31 @@ impl TryFrom<RawFont> 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::<Result<_, Error>>()?;

let virtual_masters = custom_parameters.virtual_masters.take().unwrap_or_default();
Ok(Font {
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion glyphs-reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 5 additions & 2 deletions glyphs2fontir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -556,6 +556,7 @@ impl Work<Context, WorkId, Error> for GlobalMetricWork {
GlobalMetric::Os2WinDescent,
pos.clone(),
master
.custom_parameters
.win_descent
.or(font.custom_parameters.win_descent)
.map(|v| v.abs() as f64),
Expand All @@ -567,6 +568,7 @@ impl Work<Context, WorkId, Error> for GlobalMetricWork {
set_metric!(
$variant,
master
.custom_parameters
.$field_name
.or(font.custom_parameters.$field_name)
.map(|v| v as f64)
Expand All @@ -577,6 +579,7 @@ impl Work<Context, WorkId, Error> for GlobalMetricWork {
set_metric!(
$variant,
master
.custom_parameters
.$field_name
.or(font.custom_parameters.$field_name)
.or(Some($fallback.into()))
Expand Down

0 comments on commit 23146c9

Please sign in to comment.