Handle default metrics in shift.rs

This commit is contained in:
Malo 2025-06-22 15:13:45 +01:00
parent 3fd2570533
commit f4a2ed59a6
2 changed files with 30 additions and 53 deletions

View File

@ -215,10 +215,10 @@ pub struct FontMetrics {
pub underline: LineMetrics,
/// Recommended metrics for an overline.
pub overline: LineMetrics,
/// Metrics for subscripts.
pub subscript: ScriptMetrics,
/// Metrics for superscripts.
pub superscript: ScriptMetrics,
/// Metrics for subscripts, if provided by the font.
pub subscript: Option<ScriptMetrics>,
/// Metrics for superscripts, if provided by the font.
pub superscript: Option<ScriptMetrics>,
}
impl FontMetrics {
@ -254,25 +254,19 @@ impl FontMetrics {
thickness: underline.thickness,
};
let subscript = match ttf.subscript_metrics() {
None => ScriptMetrics::default_subscript(),
Some(metrics) => ScriptMetrics {
let subscript = ttf.subscript_metrics().map(|metrics| ScriptMetrics {
width: to_em(metrics.x_size),
height: to_em(metrics.y_size),
horizontal_offset: to_em(metrics.x_offset),
vertical_offset: -to_em(metrics.y_offset),
},
};
});
let superscript = match ttf.superscript_metrics() {
None => ScriptMetrics::default_superscript(),
Some(metrics) => ScriptMetrics {
let superscript = ttf.superscript_metrics().map(|metrics| ScriptMetrics {
width: to_em(metrics.x_size),
height: to_em(metrics.y_size),
horizontal_offset: to_em(metrics.x_offset),
vertical_offset: to_em(metrics.y_offset),
},
};
});
Self {
units_per_em,
@ -328,34 +322,6 @@ pub struct ScriptMetrics {
pub vertical_offset: Em,
}
impl ScriptMetrics {
/// Creates default script metrics with the specified vertical offset.
pub const fn default_with_vertical_offset(offset: Em) -> Self {
Self {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: offset,
}
}
/// Returns the default metrics for subscripts.
///
/// This can be used as a last resort if neither the user nor the font
/// provided those metrics.
pub const fn default_subscript() -> Self {
Self::default_with_vertical_offset(Em::new(-0.2))
}
/// Returns the default metrics for superscripts.
///
/// This can be used as a last resort if neither the user nor the font
/// provided those metrics.
pub const fn default_superscript() -> Self {
Self::default_with_vertical_offset(Em::new(0.5))
}
}
/// Identifies a vertical metric of a font.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum VerticalFontMetric {

View File

@ -197,17 +197,28 @@ impl ScriptKind {
/// provided those metrics.
pub const fn default_metrics(self) -> ScriptMetrics {
match self {
Self::Sub => ScriptMetrics::default_subscript(),
Self::Super => ScriptMetrics::default_superscript(),
Self::Sub => ScriptMetrics {
width: Em::new(0.6),
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.
pub const fn read_metrics(self, font_metrics: &FontMetrics) -> ScriptMetrics {
pub fn read_metrics(self, font_metrics: &FontMetrics) -> ScriptMetrics {
match self {
Self::Sub => font_metrics.subscript,
Self::Super => font_metrics.superscript,
}
.unwrap_or(self.default_metrics())
}
/// The corresponding OpenType feature.