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, pub underline: LineMetrics,
/// Recommended metrics for an overline. /// Recommended metrics for an overline.
pub overline: LineMetrics, pub overline: LineMetrics,
/// Metrics for subscripts. /// Metrics for subscripts, if provided by the font.
pub subscript: ScriptMetrics, pub subscript: Option<ScriptMetrics>,
/// Metrics for superscripts. /// Metrics for superscripts, if provided by the font.
pub superscript: ScriptMetrics, pub superscript: Option<ScriptMetrics>,
} }
impl FontMetrics { impl FontMetrics {
@ -254,25 +254,19 @@ impl FontMetrics {
thickness: underline.thickness, thickness: underline.thickness,
}; };
let subscript = match ttf.subscript_metrics() { let subscript = ttf.subscript_metrics().map(|metrics| ScriptMetrics {
None => ScriptMetrics::default_subscript(),
Some(metrics) => ScriptMetrics {
width: to_em(metrics.x_size), width: to_em(metrics.x_size),
height: to_em(metrics.y_size), height: to_em(metrics.y_size),
horizontal_offset: to_em(metrics.x_offset), horizontal_offset: to_em(metrics.x_offset),
vertical_offset: -to_em(metrics.y_offset), vertical_offset: -to_em(metrics.y_offset),
}, });
};
let superscript = match ttf.superscript_metrics() { let superscript = ttf.superscript_metrics().map(|metrics| ScriptMetrics {
None => ScriptMetrics::default_superscript(),
Some(metrics) => ScriptMetrics {
width: to_em(metrics.x_size), width: to_em(metrics.x_size),
height: to_em(metrics.y_size), height: to_em(metrics.y_size),
horizontal_offset: to_em(metrics.x_offset), horizontal_offset: to_em(metrics.x_offset),
vertical_offset: to_em(metrics.y_offset), vertical_offset: to_em(metrics.y_offset),
}, });
};
Self { Self {
units_per_em, units_per_em,
@ -328,34 +322,6 @@ pub struct ScriptMetrics {
pub vertical_offset: Em, 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. /// Identifies a vertical metric of a font.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum VerticalFontMetric { pub enum VerticalFontMetric {

View File

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