Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[skrifa] cleanup phantom deltas #1313

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
66 changes: 41 additions & 25 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,16 +749,13 @@ 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.
match self.outlines.var_metrics {
AvailableVarMetrics::Missing => {
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 => {
self.phantom[0].x += F26Dot6::from_bits(deltas[count - 4].x.to_i32());
}
_ => {}
}
self.outlines.var_metrics.phantom_deltas(
&mut self.phantom,
deltas,
|phantom, delta| {
phantom.x += F26Dot6::from_bits(delta.x.to_i32());
},
);
have_deltas = true;
}
self.component_delta_count += count;
Expand Down Expand Up @@ -1172,16 +1169,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 +1289,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 +1364,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 +1401,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
Loading