Skip to content

Commit

Permalink
mvar: add test for sparse global metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
anthrotype committed Dec 18, 2023
1 parent 0c50599 commit 98698fe
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions fontbe/src/mvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ mod tests {
tables::{mvar as read_mvar, variations::ItemVariationData},
FontData, FontRead,
},
types::F2Dot14,
};

use super::*;
Expand Down Expand Up @@ -267,4 +268,62 @@ mod tests {
// hence no MVAR needed
assert!(builder.build().is_none());
}

struct MvarReader<'a> {
mvar: read_mvar::Mvar<'a>,
}

impl<'a> MvarReader<'a> {
fn new(mvar: read_mvar::Mvar<'a>) -> Self {
Self { mvar }
}

fn metric_delta(&self, mvar_tag: &str, coords: &[f32]) -> f64 {
let mvar_tag = Tag::from_str(mvar_tag).unwrap();
let coords: Vec<F2Dot14> = coords
.iter()
.map(|coord| F2Dot14::from_f32(*coord))
.collect();
self.mvar.metric_delta(mvar_tag, &coords).unwrap().to_f64()
}
}

#[test]
fn sparse_global_metrics() {
let regular = NormalizedLocation::for_pos(&[("wght", 0.0)]);
let medium = NormalizedLocation::for_pos(&[("wght", 0.5)]);
let bold = NormalizedLocation::for_pos(&[("wght", 1.0)]);
let mut builder = new_mvar_builder(
vec![&regular, &medium, &bold],
vec![axis("wght", 400.0, 400.0, 700.0)],
);
// 'xhgt' defines a value for all three locations
add_sources(
&mut builder,
"xhgt",
&[(&regular, 500.0), (&medium, 530.0), (&bold, 550.0)],
);
// 'strs' is sparse: defines a value for regular and bold, not medium
add_sources(&mut builder, "strs", &[(&regular, 50.0), (&bold, 100.0)]);

let Some(mvar) = builder.build() else {
panic!("no MVAR?!");
};

let bytes = dump_table(&mvar).unwrap();
let mvar = read_mvar::Mvar::read(FontData::new(&bytes)).unwrap();

assert_eq!(mvar.value_records().len(), 2);
assert_eq!(mvar.value_records()[0].value_tag(), Tag::new(b"strs"));
assert_eq!(mvar.value_records()[1].value_tag(), Tag::new(b"xhgt"));

let mvar = MvarReader::new(mvar);
assert_eq!(mvar.metric_delta("xhgt", &[0.0]), 0.0);
assert_eq!(mvar.metric_delta("xhgt", &[0.5]), 30.0); // not 25.0
assert_eq!(mvar.metric_delta("xhgt", &[1.0]), 50.0);

assert_eq!(mvar.metric_delta("strs", &[0.0]), 0.0);
assert_eq!(mvar.metric_delta("strs", &[0.5]), 25.0); // interpolated
assert_eq!(mvar.metric_delta("strs", &[1.0]), 50.0);
}
}

0 comments on commit 98698fe

Please sign in to comment.