Skip to content

Commit

Permalink
Use new gvar coordinates type with upstream intermediates optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoolean committed Feb 11, 2025
1 parent 1c27696 commit 2cac39e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ members = [
"otl-normalizer",
"fontc_crater",
]

[patch.crates-io]
write-fonts = { git = "https://github.com/daltonmaag/fontations.git", branch = "conditional-gvar-intermediates" }
skrifa = { git = "https://github.com/daltonmaag/fontations.git", branch = "conditional-gvar-intermediates" }
read-fonts = { git = "https://github.com/daltonmaag/fontations.git", branch = "conditional-gvar-intermediates" }
font-types = { git = "https://github.com/daltonmaag/fontations.git", branch = "conditional-gvar-intermediates" }
8 changes: 2 additions & 6 deletions fontbe/src/gvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ impl Work<Context, AnyWorkId, Error> for GvarWork {
mod tests {
use fontir::ir::GlyphOrder;
use write_fonts::{
tables::{
gvar::{GlyphDelta, GlyphDeltas},
variations::Tuple,
},
tables::gvar::{AxisCoordinates, GlyphDelta, GlyphDeltas},
types::F2Dot14,
};

Expand All @@ -110,9 +107,8 @@ mod tests {
v if v == glyph_without_var => Vec::new(),
// At the maximum extent (normalized pos 1.0) of our axis, add +1, +1
v if v == glyph_with_var => vec![GlyphDeltas::new(
Tuple::new(vec![F2Dot14::from_f32(1.0)]),
vec![AxisCoordinates::new(F2Dot14::from_f32(1.0), None)],
vec![GlyphDelta::new(1, 1, false)],
None,
)],
v => panic!("unexpected {v}"),
}
Expand Down
48 changes: 8 additions & 40 deletions fontbe/src/orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use fontir::{
},
variations::VariationRegion,
};
use log::trace;

use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -59,10 +58,9 @@ use write_fonts::{
os2::Os2,
post::Post,
stat::Stat,
variations::Tuple,
vhea::Vhea,
},
types::{F2Dot14, GlyphId16, Tag},
types::{GlyphId16, Tag},
validate::Validate,
FontWrite,
};
Expand Down Expand Up @@ -385,9 +383,13 @@ impl GvarFragment {
return None;
}

let tuple_builder = TupleBuilder::new(region, axis_order);
let (min, peak, max) = tuple_builder.build();
Some(GlyphDeltas::new(peak, deltas.clone(), Some((min, max))))
let coordinates = axis_order
.iter()
.map(|axis| *region.get(axis).unwrap())
.map(Into::into)
.collect();

Some(GlyphDeltas::new(coordinates, deltas.clone()))
})
.collect()
}
Expand All @@ -409,40 +411,6 @@ impl Persistable for GvarFragment {
}
}

/// <https://learn.microsoft.com/en-us/typography/opentype/spec/otvaroverview#variation-data>
#[derive(Debug, Default)]
struct TupleBuilder {
axes: Vec<Tag>,
min: Vec<F2Dot14>,
peak: Vec<F2Dot14>,
max: Vec<F2Dot14>,
}

impl TupleBuilder {
fn new(region: &VariationRegion, axis_order: &[Tag]) -> Self {
let mut builder = TupleBuilder::default();
for tag in axis_order {
let tent = region.get(tag).unwrap();
builder.axes.push(*tag);
builder.min.push(F2Dot14::from_f32(tent.min.to_f64() as _));
builder
.peak
.push(F2Dot14::from_f32(tent.peak.to_f64() as _));
builder.max.push(F2Dot14::from_f32(tent.max.to_f64() as _));
}
trace!("{builder:?}");
builder
}

fn build(self) -> (Tuple, Tuple, Tuple) {
(
Tuple::new(self.min),
Tuple::new(self.peak),
Tuple::new(self.max),
)
}
}

/// Marks, ready to feed to fea-rs in the form it expects
#[derive(Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct FeaRsMarks {
Expand Down
16 changes: 13 additions & 3 deletions fontir/src/variations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use write_fonts::{
tables::variations::RegionAxisCoordinates,
tables::{gvar, variations::RegionAxisCoordinates},
types::{F2Dot14, Tag},
};

Expand Down Expand Up @@ -590,7 +590,7 @@ impl VariationRegion {
///
/// Visualize as a tent of influence, starting at min, peaking at peak,
/// and dropping off to zero at max.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub struct Tent {
pub min: NormalizedCoord,
pub peak: NormalizedCoord,
Expand Down Expand Up @@ -677,6 +677,16 @@ impl From<(f64, f64, f64)> for Tent {
}
}

impl From<Tent> for gvar::AxisCoordinates {
fn from(val: Tent) -> Self {
let Tent { peak, min, max } = val;
gvar::AxisCoordinates::new(
peak.into(),
Some(gvar::Intermediate::explicit(min.into(), max.into())),
)
}
}

/// Split space into regions.
///
/// VariationModel::_locationsToRegions in Python.
Expand Down Expand Up @@ -756,7 +766,7 @@ fn master_influence(axis_order: &[Tag], regions: &[VariationRegion]) -> Vec<Vari
continue;
}
let prev_peak = prev_region.axis_tents[tag].peak;
let mut axis_region = region.axis_tents[tag].clone();
let mut axis_region = region.axis_tents[tag];
let ratio;
match prev_peak.cmp(&axis_region.peak) {
Ordering::Less => {
Expand Down

0 comments on commit 2cac39e

Please sign in to comment.