mirror of
https://github.com/typst/typst
synced 2025-08-21 18:24:14 +08:00
Factorize sub
's and super
's show rules
This commit is contained in:
parent
0a4d4ffa8d
commit
6ac77bc1d1
@ -62,16 +62,14 @@ pub struct SubElem {
|
|||||||
impl Show for Packed<SubElem> {
|
impl Show for Packed<SubElem> {
|
||||||
#[typst_macros::time(name = "sub", span = self.span())]
|
#[typst_macros::time(name = "sub", span = self.span())]
|
||||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
let outer_text_size = TextElem::size_in(styles);
|
show_script(
|
||||||
Ok(self
|
styles,
|
||||||
.body
|
self.body.clone(),
|
||||||
.clone()
|
self.typographic(styles),
|
||||||
.styled(TextElem::set_subperscript(Some(ScriptSettings {
|
self.baseline(styles),
|
||||||
typographic: self.typographic(styles),
|
self.size(styles),
|
||||||
shift: self.baseline(styles).map(|l| -l.to_em(outer_text_size)),
|
ScriptKind::Sub,
|
||||||
size: self.size(styles).map(|t| t.0.to_em(outer_text_size)),
|
)
|
||||||
kind: ScriptKind::Sub,
|
|
||||||
}))))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,19 +133,34 @@ pub struct SuperElem {
|
|||||||
impl Show for Packed<SuperElem> {
|
impl Show for Packed<SuperElem> {
|
||||||
#[typst_macros::time(name = "super", span = self.span())]
|
#[typst_macros::time(name = "super", span = self.span())]
|
||||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
let outer_text_size = TextElem::size_in(styles);
|
show_script(
|
||||||
Ok(self
|
styles,
|
||||||
.body
|
self.body.clone(),
|
||||||
.clone()
|
self.typographic(styles),
|
||||||
.styled(TextElem::set_subperscript(Some(ScriptSettings {
|
self.baseline(styles),
|
||||||
typographic: self.typographic(styles),
|
self.size(styles),
|
||||||
shift: self.baseline(styles).map(|l| -l.to_em(outer_text_size)),
|
ScriptKind::Super,
|
||||||
size: self.size(styles).map(|t| t.0.to_em(outer_text_size)),
|
)
|
||||||
kind: ScriptKind::Super,
|
|
||||||
}))))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn show_script(
|
||||||
|
styles: StyleChain,
|
||||||
|
body: Content,
|
||||||
|
typographic: bool,
|
||||||
|
baseline: Smart<Length>,
|
||||||
|
size: Smart<TextSize>,
|
||||||
|
kind: ScriptKind,
|
||||||
|
) -> SourceResult<Content> {
|
||||||
|
let outer_text_size = TextElem::size_in(styles);
|
||||||
|
Ok(body.styled(TextElem::set_subperscript(Some(ScriptSettings {
|
||||||
|
typographic,
|
||||||
|
shift: baseline.map(|l| -l.to_em(outer_text_size)),
|
||||||
|
size: size.map(|t| t.0.to_em(outer_text_size)),
|
||||||
|
kind,
|
||||||
|
}))))
|
||||||
|
}
|
||||||
|
|
||||||
/// Configuration values for sub- or superscript text.
|
/// Configuration values for sub- or superscript text.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
pub struct ScriptSettings {
|
pub struct ScriptSettings {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user