Move Length::to_em to EM::from_length

This commit is contained in:
Malo 2025-06-30 19:26:21 +01:00
parent b1d9519fe7
commit 545e083bf3
5 changed files with 20 additions and 21 deletions

View File

@ -261,7 +261,7 @@ impl<'a> ShapedText<'a> {
adjustability_right * justification_ratio;
if shaped.is_justifiable() {
justification_right +=
Em::from_length(extra_justification, glyph_size)
Em::from_abs(extra_justification, glyph_size)
}
frame.size_mut().x += justification_left.at(glyph_size)
@ -1074,9 +1074,8 @@ fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, font: Font) {
/// Apply tracking and spacing to the shaped glyphs.
fn track_and_space(ctx: &mut ShapingContext) {
let tracking = Em::from_length(TextElem::tracking_in(ctx.styles), ctx.size);
let spacing =
TextElem::spacing_in(ctx.styles).map(|abs| Em::from_length(abs, ctx.size));
let tracking = Em::from_abs(TextElem::tracking_in(ctx.styles), ctx.size);
let spacing = TextElem::spacing_in(ctx.styles).map(|abs| Em::from_abs(abs, ctx.size));
let mut glyphs = ctx.glyphs.iter_mut().peekable();
while let Some(glyph) = glyphs.next() {

View File

@ -215,7 +215,7 @@ impl MathFragment {
&glyph.item.font,
GlyphId(glyph.item.glyphs[glyph_index].id),
corner,
Em::from_length(height, glyph.item.size),
Em::from_abs(height, glyph.item.size),
)
.unwrap_or_default()
.at(glyph.item.size)
@ -767,8 +767,8 @@ fn assemble(
advance += ratio * (max_overlap - min_overlap);
}
let (x, y) = match axis {
Axis::X => (Em::from_length(advance, base.item.size), Em::zero()),
Axis::Y => (Em::zero(), Em::from_length(advance, base.item.size)),
Axis::X => (Em::from_abs(advance, base.item.size), Em::zero()),
Axis::Y => (Em::zero(), Em::from_abs(advance, base.item.size)),
};
glyphs.push(Glyph {
id: part.glyph_id.0,

View File

@ -6,7 +6,7 @@ use ecow::EcoString;
use typst_utils::{Numeric, Scalar};
use crate::foundations::{cast, repr, Repr, Resolve, StyleChain, Value};
use crate::layout::Abs;
use crate::layout::{Abs, Length};
use crate::text::TextElem;
/// A length that is relative to the font size.
@ -26,18 +26,18 @@ impl Em {
Self(Scalar::ONE)
}
/// Create a font-relative length.
/// Creates a font-relative length.
pub const fn new(em: f64) -> Self {
Self(Scalar::new(em))
}
/// Create an em length from font units at the given units per em.
/// Creates an em length from font units at the given units per em.
pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self {
Self(Scalar::new(units.into() / units_per_em))
}
/// Create an em length from a length at the given font size.
pub fn from_length(length: Abs, font_size: Abs) -> Self {
/// Creates an em length from an absolute length at the given font size.
pub fn from_abs(length: Abs, font_size: Abs) -> Self {
let result = length / font_size;
if result.is_finite() {
Self(Scalar::new(result))
@ -46,6 +46,11 @@ impl Em {
}
}
/// Creates an em length from a length at the given font size.
pub fn from_length(length: Length, font_size: Abs) -> Em {
length.em + Self::from_abs(length.abs, font_size)
}
/// The number of em units.
pub const fn get(self) -> f64 {
(self.0).get()
@ -56,7 +61,7 @@ impl Em {
Self::new(self.get().abs())
}
/// Convert to an absolute length at the given font size.
/// Converts to an absolute length at the given font size.
pub fn at(self, font_size: Abs) -> Abs {
let resolved = font_size * self.get();
if resolved.is_finite() {

View File

@ -76,11 +76,6 @@ impl Length {
self.abs + self.em.at(font_size)
}
/// Expresses this length in em's of the provided outer text size.
pub fn to_em(&self, font_size: Abs) -> Em {
self.em + Em::from_length(self.abs, font_size)
}
/// Fails with an error if the length has a non-zero font-relative part.
fn ensure_that_em_is_zero(&self, span: Span, unit: &str) -> SourceResult<()> {
if self.em == Em::zero() {

View File

@ -177,11 +177,11 @@ fn show_script(
size: Smart<TextSize>,
kind: ScriptKind,
) -> SourceResult<Content> {
let outer_text_size = TextElem::size_in(styles);
let font_size = TextElem::size_in(styles);
Ok(body.styled(TextElem::set_shift_settings(Some(ShiftSettings {
typographic,
shift: baseline.map(|l| -l.to_em(outer_text_size)),
size: size.map(|t| t.0.to_em(outer_text_size)),
shift: baseline.map(|l| -Em::from_length(l, font_size)),
size: size.map(|t| Em::from_length(t.0, font_size)),
kind,
}))))
}