Skip to content

Commit

Permalink
[skrifa] cleanup phantom deltas
Browse files Browse the repository at this point in the history
Additional code cleanup suggested after merging #1312
  • Loading branch information
dfrg committed Jan 9, 2025
1 parent 89eaec0 commit e29ed13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
12 changes: 6 additions & 6 deletions skrifa/src/outline/glyf/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use super::PHANTOM_POINT_COUNT;
#[derive(Copy, Clone, Debug)]
pub(super) enum AvailableVarMetrics {
/// Full variable metrics with advances and side-bearings.
Present,
All,
/// Variable advances but not side-bearings.
MissingSideBearings,
Advances,
/// No variable metrics table.
Missing,
None,
}

/// Compute a set of deltas for the component offsets of a composite glyph.
Expand Down Expand Up @@ -87,11 +87,11 @@ where
// content of the HVAR table.
let actual_len = match var_metrics {
// Modify LSB and advance
AvailableVarMetrics::Missing => points.len() - 2,
AvailableVarMetrics::None => points.len() - 2,
// Modify only LSB
AvailableVarMetrics::MissingSideBearings => points.len() - 3,
AvailableVarMetrics::Advances => points.len() - 3,
// Modify nothing
AvailableVarMetrics::Present => points.len() - 4,
AvailableVarMetrics::All => points.len() - PHANTOM_POINT_COUNT,
};
let deltas = &mut deltas[..actual_len];
compute_deltas_for_glyph(gvar, glyph_id, coords, deltas, |scalar, tuple, deltas| {
Expand Down
60 changes: 43 additions & 17 deletions skrifa/src/outline/glyf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ impl<'a> Outlines<'a> {
let var_metrics = match glyph_metrics.hvar.as_ref() {
Some(hvar) => {
if hvar.lsb_mapping().is_some() {
AvailableVarMetrics::Present
AvailableVarMetrics::All
} else {
AvailableVarMetrics::MissingSideBearings
AvailableVarMetrics::Advances
}
}
None => AvailableVarMetrics::Missing,
None => AvailableVarMetrics::None,
};
let (
glyph_count,
Expand Down Expand Up @@ -749,12 +749,19 @@ impl Scaler for FreeTypeScaler<'_> {
.ok_or(InsufficientMemory)?;
if deltas::composite_glyph(gvar, glyph_id, self.coords, &mut deltas[..]).is_ok() {
// Apply selective deltas to phantom points.
self.outlines.var_metrics.phantom_deltas(
&mut self.phantom,
deltas,
|phantom, delta| {
phantom.x += F26Dot6::from_bits(delta.x.to_i32());
},
);
match self.outlines.var_metrics {
AvailableVarMetrics::Missing => {
AvailableVarMetrics::None => {
self.phantom[0].x += F26Dot6::from_bits(deltas[count - 4].x.to_i32());
self.phantom[1].x += F26Dot6::from_bits(deltas[count - 3].x.to_i32());
}
AvailableVarMetrics::MissingSideBearings => {
AvailableVarMetrics::Advances => {
self.phantom[0].x += F26Dot6::from_bits(deltas[count - 4].x.to_i32());
}
_ => {}
Expand Down Expand Up @@ -1172,16 +1179,13 @@ impl Scaler for HarfBuzzScaler<'_> {
.ok_or(InsufficientMemory)?;
if deltas::composite_glyph(gvar, glyph_id, self.coords, &mut deltas[..]).is_ok() {
// Apply selective deltas to phantom points.
match self.outlines.var_metrics {
AvailableVarMetrics::Missing => {
self.phantom[0].x += deltas[count - 4].x;
self.phantom[1].x += deltas[count - 3].x;
}
AvailableVarMetrics::MissingSideBearings => {
self.phantom[0].x += deltas[count - 4].x;
}
_ => {}
}
self.outlines.var_metrics.phantom_deltas(
&mut self.phantom,
deltas,
|phantom, delta| {
phantom.x += delta.x;
},
);
have_deltas = true;
}
self.component_delta_count += count;
Expand Down Expand Up @@ -1295,6 +1299,28 @@ fn map_point(transform: [f32; 6], p: Point<f32>) -> Point<f32> {
}
}

impl AvailableVarMetrics {
/// Calls `f` for each combination of phantom point and its associated
/// delta based on the available metrics present in `self`.
fn phantom_deltas<P, D>(
self,
phantom: &mut [Point<P>; 4],
deltas: &[Point<D>],
f: impl Fn(&mut Point<P>, &Point<D>),
) {
match self {
Self::None => {
f(&mut phantom[0], &deltas[deltas.len() - 4]);
f(&mut phantom[1], &deltas[deltas.len() - 3]);
}
Self::Advances => {
f(&mut phantom[0], &deltas[deltas.len() - 4]);
}
Self::All => {}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1348,7 +1374,7 @@ mod tests {
let advance_hvar = scaled.adjusted_advance_width();
// Set HVAR to None and mark var metrics as missing so we pull deltas from gvar
outlines.glyph_metrics.hvar = None;
outlines.var_metrics = AvailableVarMetrics::Missing;
outlines.var_metrics = AvailableVarMetrics::None;
let scaler =
FreeTypeScaler::unhinted(&outlines, &outline, &mut buf, ppem, &coords).unwrap();
let scaled = scaler.scale(&outline.glyph, gid).unwrap();
Expand Down Expand Up @@ -1385,7 +1411,7 @@ mod tests {
let advance_hvar = scaled.adjusted_advance_width();
// Set HVAR to None and mark var metrics as missing so we pull deltas from gvar
outlines.glyph_metrics.hvar = None;
outlines.var_metrics = AvailableVarMetrics::Missing;
outlines.var_metrics = AvailableVarMetrics::None;
let scaler =
FreeTypeScaler::unhinted(&outlines, &outline, &mut buf, ppem, &coords).unwrap();
let scaled = scaler.scale(&outline.glyph, gid).unwrap();
Expand Down

0 comments on commit e29ed13

Please sign in to comment.