mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Implement mid() for middle delimiters in lr() (#2760)
This commit is contained in:
parent
c354f00f79
commit
34862b7b27
@ -297,6 +297,7 @@ impl GlyphFragment {
|
|||||||
span: self.span,
|
span: self.span,
|
||||||
limits: self.limits,
|
limits: self.limits,
|
||||||
frame: self.into_frame(),
|
frame: self.into_frame(),
|
||||||
|
mid_stretched: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,6 +361,7 @@ pub struct VariantFragment {
|
|||||||
pub class: Option<MathClass>,
|
pub class: Option<MathClass>,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub limits: Limits,
|
pub limits: Limits,
|
||||||
|
pub mid_stretched: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VariantFragment {
|
impl VariantFragment {
|
||||||
|
@ -57,6 +57,7 @@ impl LayoutMath for LrElem {
|
|||||||
.resolve(ctx.styles())
|
.resolve(ctx.styles())
|
||||||
.relative_to(2.0 * max_extent);
|
.relative_to(2.0 * max_extent);
|
||||||
|
|
||||||
|
// Scale up fragments at both ends.
|
||||||
match fragments.as_mut_slice() {
|
match fragments.as_mut_slice() {
|
||||||
[one] => scale(ctx, one, height, None),
|
[one] => scale(ctx, one, height, None),
|
||||||
[first, .., last] => {
|
[first, .., last] => {
|
||||||
@ -66,12 +67,58 @@ impl LayoutMath for LrElem {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle MathFragment::Variant fragments that should be scaled up.
|
||||||
|
for fragment in &mut fragments {
|
||||||
|
if let MathFragment::Variant(ref mut variant) = fragment {
|
||||||
|
if variant.mid_stretched == Some(false) {
|
||||||
|
variant.mid_stretched = Some(true);
|
||||||
|
scale(ctx, fragment, height, Some(MathClass::Large));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.extend(fragments);
|
ctx.extend(fragments);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Scales contents vertically to the nearest surrounding lr() group.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// $ { x mid(|) sum_(i=1)^oo phi_i (x) < 1 } $
|
||||||
|
/// ```
|
||||||
|
#[elem(LayoutMath)]
|
||||||
|
pub struct MidElem {
|
||||||
|
/// The content to be scaled.
|
||||||
|
#[required]
|
||||||
|
pub body: Content,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LayoutMath for MidElem {
|
||||||
|
#[tracing::instrument(skip(ctx))]
|
||||||
|
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
|
||||||
|
let mut fragments = ctx.layout_fragments(self.body())?;
|
||||||
|
|
||||||
|
for fragment in &mut fragments {
|
||||||
|
match fragment {
|
||||||
|
MathFragment::Glyph(glyph) => {
|
||||||
|
let mut new = glyph.clone().into_variant();
|
||||||
|
new.mid_stretched = Some(false);
|
||||||
|
*fragment = MathFragment::Variant(new);
|
||||||
|
}
|
||||||
|
MathFragment::Variant(variant) => {
|
||||||
|
variant.mid_stretched = Some(false);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.extend(fragments);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Scale a math fragment to a height.
|
/// Scale a math fragment to a height.
|
||||||
fn scale(
|
fn scale(
|
||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
|
@ -161,6 +161,7 @@ pub fn module() -> Module {
|
|||||||
math.define_elem::<EquationElem>();
|
math.define_elem::<EquationElem>();
|
||||||
math.define_elem::<TextElem>();
|
math.define_elem::<TextElem>();
|
||||||
math.define_elem::<LrElem>();
|
math.define_elem::<LrElem>();
|
||||||
|
math.define_elem::<MidElem>();
|
||||||
math.define_elem::<AttachElem>();
|
math.define_elem::<AttachElem>();
|
||||||
math.define_elem::<ScriptsElem>();
|
math.define_elem::<ScriptsElem>();
|
||||||
math.define_elem::<LimitsElem>();
|
math.define_elem::<LimitsElem>();
|
||||||
|
@ -187,6 +187,7 @@ fn assemble(
|
|||||||
class: base.class,
|
class: base.class,
|
||||||
span: base.span,
|
span: base.span,
|
||||||
limits: base.limits,
|
limits: base.limits,
|
||||||
|
mid_stretched: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 29 KiB |
@ -43,3 +43,10 @@ $ lr(
|
|||||||
text("(", fill: #green) a/b
|
text("(", fill: #green) a/b
|
||||||
text(")", fill: #blue)
|
text(")", fill: #blue)
|
||||||
) $
|
) $
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test middle functions
|
||||||
|
$ { x mid(|) sum_(i=1)^oo phi_i (x) < 1 } \
|
||||||
|
{ integral |x| dif x
|
||||||
|
mid(bar.v.double)
|
||||||
|
floor(hat(A) mid(|) { x mid(|) y } mid(|) A) } $
|
||||||
|
Loading…
x
Reference in New Issue
Block a user