mirror of
https://github.com/typst/typst
synced 2025-05-16 01:55:28 +08:00
Added to-absolute
method to length. (#3117)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
parent
28a5069f95
commit
3aeb150c95
@ -5,7 +5,7 @@ use std::ops::{Add, Div, Mul, Neg};
|
|||||||
use ecow::{eco_format, EcoString};
|
use ecow::{eco_format, EcoString};
|
||||||
|
|
||||||
use crate::diag::{At, Hint, SourceResult};
|
use crate::diag::{At, Hint, SourceResult};
|
||||||
use crate::foundations::{func, scope, ty, Repr, Resolve, StyleChain};
|
use crate::foundations::{func, scope, ty, Repr, Resolve, StyleChain, Styles};
|
||||||
use crate::layout::{Abs, Em};
|
use crate::layout::{Abs, Em};
|
||||||
use crate::syntax::Span;
|
use crate::syntax::Span;
|
||||||
use crate::util::Numeric;
|
use crate::util::Numeric;
|
||||||
@ -132,6 +132,38 @@ impl Length {
|
|||||||
self.ensure_that_em_is_zero(span, "inches")?;
|
self.ensure_that_em_is_zero(span, "inches")?;
|
||||||
Ok(self.abs.to_inches())
|
Ok(self.abs.to_inches())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve this length to an absolute length.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #set text(size: 12pt)
|
||||||
|
/// #style(styles => [
|
||||||
|
/// #(6pt).to-absolute(styles) \
|
||||||
|
/// #(6pt + 10em).to-absolute(styles) \
|
||||||
|
/// #(10em).to-absolute(styles)
|
||||||
|
/// ])
|
||||||
|
///
|
||||||
|
/// #set text(size: 6pt)
|
||||||
|
/// #style(styles => [
|
||||||
|
/// #(6pt).to-absolute(styles) \
|
||||||
|
/// #(6pt + 10em).to-absolute(styles) \
|
||||||
|
/// #(10em).to-absolute(styles)
|
||||||
|
/// ])
|
||||||
|
/// ```
|
||||||
|
#[func]
|
||||||
|
pub fn to_absolute(
|
||||||
|
&self,
|
||||||
|
/// The styles to resolve the length with.
|
||||||
|
///
|
||||||
|
/// Since a length can use font-relative em units, resolving it to an
|
||||||
|
/// absolute length requires knowledge of the font size. This size is
|
||||||
|
/// provided through these styles. You can obtain the styles using
|
||||||
|
/// the [`style`]($style) function.
|
||||||
|
styles: Styles,
|
||||||
|
) -> Length {
|
||||||
|
let styles = StyleChain::new(&styles);
|
||||||
|
self.resolve(styles).into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Length {
|
impl Debug for Length {
|
||||||
|
@ -81,6 +81,23 @@
|
|||||||
#test(5em.abs.cm(), 0.0)
|
#test(5em.abs.cm(), 0.0)
|
||||||
#test((5em + 6in).abs.inches(), 6.0)
|
#test((5em + 6in).abs.inches(), 6.0)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test length `to-absolute` method.
|
||||||
|
|
||||||
|
#set text(size: 12pt)
|
||||||
|
#style(styles => {
|
||||||
|
test((6pt).to-absolute(styles), 6pt)
|
||||||
|
test((6pt + 10em).to-absolute(styles), 126pt)
|
||||||
|
test((10em).to-absolute(styles), 120pt)
|
||||||
|
})
|
||||||
|
|
||||||
|
#set text(size: 64pt)
|
||||||
|
#style(styles => {
|
||||||
|
test((6pt).to-absolute(styles), 6pt)
|
||||||
|
test((6pt + 10em).to-absolute(styles), 646pt)
|
||||||
|
test((10em).to-absolute(styles), 640pt)
|
||||||
|
})
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 2-21 cannot convert a length with non-zero em units (`-6pt + 10.5em`) to pt
|
// Error: 2-21 cannot convert a length with non-zero em units (`-6pt + 10.5em`) to pt
|
||||||
// Hint: 2-21 use `length.abs.pt()` instead to ignore its em component
|
// Hint: 2-21 use `length.abs.pt()` instead to ignore its em component
|
||||||
|
Loading…
x
Reference in New Issue
Block a user