Extract None case outside of compute_synthesized_shift

This commit is contained in:
Malo 2025-06-24 19:25:58 +01:00
parent 7195f97b9b
commit 899157af24

View File

@ -804,8 +804,11 @@ fn shape_segment<'a>(
// text extraction. // text extraction.
buffer.set_flags(BufferFlags::REMOVE_DEFAULT_IGNORABLES); buffer.set_flags(BufferFlags::REMOVE_DEFAULT_IGNORABLES);
let (script_shift, script_compensation, scale) = let (script_shift, script_compensation, scale) = ctx
compute_synthesized_shift(ctx, text, &font); .shift_settings
.map_or((Em::zero(), Em::zero(), Em::one()), |settings| {
compute_synthesized_shift(ctx, text, &font, settings)
});
// Prepare the shape plan. This plan depends on direction, script, language, // Prepare the shape plan. This plan depends on direction, script, language,
// and features, but is independent from the text and can thus be memoized. // and features, but is independent from the text and can thus be memoized.
@ -964,46 +967,46 @@ fn compute_synthesized_shift(
ctx: &mut ShapingContext, ctx: &mut ShapingContext,
text: &str, text: &str,
font: &Font, font: &Font,
settings: ShiftSettings,
) -> (Em, Em, Em) { ) -> (Em, Em, Em) {
match ctx.shift_settings { settings
None => (Em::zero(), Em::zero(), Em::one()), .typographic
Some(settings) => settings .then(|| {
.typographic // If typographic scripts are enabled (i.e., we want to use the
.then(|| { // OpenType feature instead of synthesizing if possible), we add
// If typographic scripts are enabled (i.e., we want to use the // "subs"/"sups" to the feature list if supported by the font.
// OpenType feature instead of synthesizing if possible), we add // In case of a problem, we just early exit
// "subs"/"sups" to the feature list if supported by the font. let gsub = font.rusty().tables().gsub?;
// In case of a problem, we just early exit let subtable_index =
let gsub = font.rusty().tables().gsub?; gsub.features.find(settings.kind.feature())?.lookup_indices.get(0)?;
let subtable_index = let coverage = gsub
gsub.features.find(settings.kind.feature())?.lookup_indices.get(0)?; .lookups
let coverage = gsub .get(subtable_index)?
.lookups .subtables
.get(subtable_index)? .get::<SubstitutionSubtable>(0)?
.subtables .coverage();
.get::<SubstitutionSubtable>(0)? text.chars()
.coverage(); .all(|c| {
text.chars() font.rusty().glyph_index(c).is_some_and(|i| coverage.contains(i))
.all(|c| { })
font.rusty().glyph_index(c).is_some_and(|i| coverage.contains(i)) .then(|| {
}) ctx.features.push(Feature::new(settings.kind.feature(), 1, ..));
.then(|| { // If we can use the OpenType feature, we can keep the text
ctx.features.push(Feature::new(settings.kind.feature(), 1, ..)); // as is.
(Em::zero(), Em::zero(), Em::one()) (Em::zero(), Em::zero(), Em::one())
}) })
}) })
// Reunite the cases where `typographic` is `false` or where using // Reunite the cases where `typographic` is `false` or where using the
// the OpenType feature would not work. // OpenType feature would not work.
.flatten() .flatten()
.unwrap_or_else(|| { .unwrap_or_else(|| {
let script_metrics = settings.kind.read_metrics(font.metrics()); let script_metrics = settings.kind.read_metrics(font.metrics());
( (
settings.shift.unwrap_or(script_metrics.vertical_offset), settings.shift.unwrap_or(script_metrics.vertical_offset),
script_metrics.horizontal_offset, script_metrics.horizontal_offset,
settings.size.unwrap_or(script_metrics.height), settings.size.unwrap_or(script_metrics.height),
) )
}), })
}
} }
/// Create a shape plan. /// Create a shape plan.