Return reference from ScriptKind::read_metric

This commit is contained in:
Malo 2025-06-22 15:20:15 +01:00
parent f4a2ed59a6
commit 328a119703
2 changed files with 21 additions and 18 deletions

View File

@ -429,8 +429,8 @@ pub fn apply_shift<'a>(
.select(family.as_str(), variant(styles)) .select(family.as_str(), variant(styles))
.and_then(|id| world.font(id)) .and_then(|id| world.font(id))
}) })
.map_or(scripts.kind.default_metrics(), |f| { .map_or(*scripts.kind.default_metrics(), |f| {
scripts.kind.read_metrics(f.metrics()) *scripts.kind.read_metrics(f.metrics())
}); });
baseline -= scripts.shift.unwrap_or(font_metrics.vertical_offset).resolve(styles); baseline -= scripts.shift.unwrap_or(font_metrics.vertical_offset).resolve(styles);
compensation += font_metrics.horizontal_offset.resolve(styles); compensation += font_metrics.horizontal_offset.resolve(styles);

View File

@ -195,28 +195,18 @@ impl ScriptKind {
/// ///
/// This can be used as a last resort if neither the user nor the font /// This can be used as a last resort if neither the user nor the font
/// provided those metrics. /// provided those metrics.
pub const fn default_metrics(self) -> ScriptMetrics { pub fn default_metrics(self) -> &'static ScriptMetrics {
match self { match self {
Self::Sub => ScriptMetrics { Self::Sub => &DEFAULT_SUBSCRIPT_METRICS,
width: Em::new(0.6), Self::Super => &DEFAULT_SUPERSCRIPT_METRICS,
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(-0.2),
},
Self::Super => ScriptMetrics {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(0.5),
},
} }
} }
/// Reads the script metrics from the font table for to this script kind. /// Reads the script metrics from the font table for to this script kind.
pub fn read_metrics(self, font_metrics: &FontMetrics) -> ScriptMetrics { pub fn read_metrics(self, font_metrics: &FontMetrics) -> &ScriptMetrics {
match self { match self {
Self::Sub => font_metrics.subscript, Self::Sub => font_metrics.subscript.as_ref(),
Self::Super => font_metrics.superscript, Self::Super => font_metrics.superscript.as_ref(),
} }
.unwrap_or(self.default_metrics()) .unwrap_or(self.default_metrics())
} }
@ -229,3 +219,16 @@ impl ScriptKind {
} }
} }
} }
static DEFAULT_SUBSCRIPT_METRICS: ScriptMetrics = ScriptMetrics {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(-0.2),
};
static DEFAULT_SUPERSCRIPT_METRICS: ScriptMetrics = ScriptMetrics {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(0.5),
};