diff --git a/crates/typst-library/src/text/font/mod.rs b/crates/typst-library/src/text/font/mod.rs index ba984f8e4..c32a05723 100644 --- a/crates/typst-library/src/text/font/mod.rs +++ b/crates/typst-library/src/text/font/mod.rs @@ -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, + /// Metrics for superscripts, if provided by the font. + pub superscript: Option, } impl FontMetrics { @@ -254,25 +254,19 @@ impl FontMetrics { thickness: underline.thickness, }; - let subscript = match ttf.subscript_metrics() { - None => ScriptMetrics::default_subscript(), - Some(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 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 { - 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 = 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 { diff --git a/crates/typst-library/src/text/shift.rs b/crates/typst-library/src/text/shift.rs index acbddea1c..220668935 100644 --- a/crates/typst-library/src/text/shift.rs +++ b/crates/typst-library/src/text/shift.rs @@ -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.