Fix realize in math

This commit is contained in:
Laurenz 2023-04-19 10:38:03 +02:00
parent d10b53df0b
commit f8966f9e86
6 changed files with 43 additions and 9 deletions

View File

@ -148,6 +148,7 @@ pub struct GlyphFragment {
pub font_size: Abs,
pub class: Option<MathClass>,
pub span: Span,
pub meta: Vec<Meta>,
}
impl GlyphFragment {
@ -194,6 +195,7 @@ impl GlyphFragment {
_ => unicode_math_class::class(c),
},
span,
meta: MetaElem::data_in(ctx.styles()),
}
}
@ -231,6 +233,7 @@ impl GlyphFragment {
};
let size = Size::new(self.width, self.ascent + self.descent);
let mut frame = Frame::new(size);
frame.meta_iter(self.meta);
frame.set_baseline(self.ascent);
frame.push(Point::with_y(self.ascent), FrameItem::Text(item));
frame
@ -273,8 +276,9 @@ pub struct FrameFragment {
}
impl FrameFragment {
pub fn new(ctx: &MathContext, frame: Frame) -> Self {
pub fn new(ctx: &MathContext, mut frame: Frame) -> Self {
let base_ascent = frame.ascent();
frame.meta(ctx.styles(), false);
Self {
frame,
font_size: ctx.size,

View File

@ -344,9 +344,18 @@ impl LayoutMath for EquationElem {
impl LayoutMath for Content {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
// Directly layout the body of nested equations instead of handling it
// like a normal equation so that things like this work:
// ```
// #let my = $pi$
// $ my r^2 $
// ```
if let Some(elem) = self.to::<EquationElem>() {
return elem.layout_math(ctx);
}
if let Some(realized) = ctx.realize(self)? {
realized.layout_math(ctx)?;
return Ok(());
return realized.layout_math(ctx);
}
if let Some(children) = self.to_sequence() {

View File

@ -162,6 +162,7 @@ fn assemble(
let mut frame = Frame::new(size);
let mut offset = Abs::zero();
frame.set_baseline(baseline);
frame.meta_iter(base.meta);
for (fragment, advance) in selected {
let pos = if horizontal {

View File

@ -273,13 +273,18 @@ impl Frame {
/// Attach the metadata from this style chain to the frame.
pub fn meta(&mut self, styles: StyleChain, force: bool) {
if force || !self.is_empty() {
for meta in MetaElem::data_in(styles) {
if matches!(meta, Meta::Hide) {
self.clear();
break;
}
self.prepend(Point::zero(), FrameItem::Meta(meta, self.size));
self.meta_iter(MetaElem::data_in(styles));
}
}
/// Attach metadata from an iterator.
pub fn meta_iter(&mut self, iter: impl IntoIterator<Item = Meta>) {
for meta in iter {
if matches!(meta, Meta::Hide) {
self.clear();
break;
}
self.prepend(Point::zero(), FrameItem::Meta(meta, self.size));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

@ -0,0 +1,15 @@
// Test that content in math can be realized without breaking
// nested equations.
---
#let my = $pi$
#let f1 = box(baseline: 10pt, [f])
#let f2 = style(sty => f1)
#show math.vec: [nope]
$ pi a $
$ my a $
$ 1 + sqrt(x/2) + sqrt(#hide($x/2$)) $
$ a x #link("url", $+ b$) $
$ f f1 f2 $
$ vec(1,2) * 2 $