Skip to content

Commit

Permalink
Support avar2 in user_to_normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Aug 30, 2024
1 parent e5e1e0c commit a16885f
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion read-fonts/src/tables/fvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ include!("../../generated/generated_fvar.rs");
#[path = "./instance_record.rs"]
mod instance_record;

use super::avar::Avar;
use super::{
avar::Avar,
variations::{DeltaSetIndex, FloatItemDeltaTarget},
};
pub use instance_record::InstanceRecord;

impl<'a> Fvar<'a> {
Expand Down Expand Up @@ -71,6 +74,35 @@ impl<'a> Fvar<'a> {
}
}
}
if avar.is_none() || avar.map(|a| a.version()) == Some(MajorMinor::new(1, 0)) {
return;
}
let avar = avar.unwrap();
let var_store = avar.var_store();
let var_index_map = avar.axis_index_map();
let mut new_coords = normalized_coords.to_vec();
for (i, v) in normalized_coords.iter().enumerate() {
let var_index = if let Some(Ok(ref map)) = var_index_map {
map.get(i as u32).ok()
} else {
Some(DeltaSetIndex {
outer: 0,
inner: i as u16,
})
};
if var_index.is_none() {
continue;
}
if let Some(Ok(varstore)) = var_store.as_ref() {
if let Ok(delta) =
varstore.compute_float_delta(var_index.unwrap(), normalized_coords)
{
new_coords[i] = F2Dot14::from_f32((*v).apply_float_delta(delta))
.clamp(F2Dot14::MIN, F2Dot14::MAX);
}
}
}
normalized_coords.copy_from_slice(&new_coords);
}
}

Expand Down Expand Up @@ -205,4 +237,31 @@ mod tests {
assert_eq!(normalized_coords[0], F2Dot14::from_f32(normalized));
}
}

#[test]
fn avar2() {
let font = FontRef::new(font_test_data::AVAR2_CHECKER).unwrap();
let avar = font.avar().ok();
let fvar = font.fvar().unwrap();
let avar_axis = Tag::new(b"AVAR");
let avwk_axis = Tag::new(b"AVWK");
let mut normalized_coords = [F2Dot14::default(); 2];
let cases = [
((100.0, 0.0), (1.0, 1.0)),
((50.0, 0.0), (0.5, 0.5)),
((0.0, 50.0), (0.0, 0.5)),
];
for (user, expected) in cases {
fvar.user_to_normalized(
avar.as_ref(),
[
(avar_axis, Fixed::from_f64(user.0)),
(avwk_axis, Fixed::from_f64(user.1)),
],
&mut normalized_coords,
);
assert_eq!(normalized_coords[0], F2Dot14::from_f32(expected.0));
assert_eq!(normalized_coords[1], F2Dot14::from_f32(expected.1));
}
}
}

0 comments on commit a16885f

Please sign in to comment.