mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Avoid spaces around ignorant and invisible elements in math (#4348)
This commit is contained in:
parent
394ba50fa6
commit
59374f7370
@ -71,6 +71,13 @@ impl MathFragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_ignorant(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Frame(fragment) => fragment.ignorant,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn class(&self) -> MathClass {
|
pub fn class(&self) -> MathClass {
|
||||||
match self {
|
match self {
|
||||||
Self::Glyph(glyph) => glyph.class,
|
Self::Glyph(glyph) => glyph.class,
|
||||||
@ -441,6 +448,7 @@ pub struct FrameFragment {
|
|||||||
pub italics_correction: Abs,
|
pub italics_correction: Abs,
|
||||||
pub accent_attach: Abs,
|
pub accent_attach: Abs,
|
||||||
pub text_like: bool,
|
pub text_like: bool,
|
||||||
|
pub ignorant: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FrameFragment {
|
impl FrameFragment {
|
||||||
@ -459,6 +467,7 @@ impl FrameFragment {
|
|||||||
italics_correction: Abs::zero(),
|
italics_correction: Abs::zero(),
|
||||||
accent_attach,
|
accent_attach,
|
||||||
text_like: false,
|
text_like: false,
|
||||||
|
ignorant: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -489,6 +498,10 @@ impl FrameFragment {
|
|||||||
pub fn with_text_like(self, text_like: bool) -> Self {
|
pub fn with_text_like(self, text_like: bool) -> Self {
|
||||||
Self { text_like, ..self }
|
Self { text_like, ..self }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_ignorant(self, ignorant: bool) -> Self {
|
||||||
|
Self { ignorant, ..self }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -49,6 +49,7 @@ use crate::foundations::{
|
|||||||
};
|
};
|
||||||
use crate::introspection::TagElem;
|
use crate::introspection::TagElem;
|
||||||
use crate::layout::{BoxElem, Frame, FrameItem, HElem, Point, Size, Spacing, VAlignment};
|
use crate::layout::{BoxElem, Frame, FrameItem, HElem, Point, Size, Spacing, VAlignment};
|
||||||
|
use crate::realize::Behaviour;
|
||||||
use crate::realize::{process, BehavedBuilder};
|
use crate::realize::{process, BehavedBuilder};
|
||||||
use crate::text::{LinebreakElem, SpaceElem, TextElem};
|
use crate::text::{LinebreakElem, SpaceElem, TextElem};
|
||||||
|
|
||||||
@ -299,7 +300,7 @@ impl LayoutMath for Content {
|
|||||||
if let Some(elem) = self.to_packed::<TagElem>() {
|
if let Some(elem) = self.to_packed::<TagElem>() {
|
||||||
let mut frame = Frame::soft(Size::zero());
|
let mut frame = Frame::soft(Size::zero());
|
||||||
frame.push(Point::zero(), FrameItem::Tag(elem.tag.clone()));
|
frame.push(Point::zero(), FrameItem::Tag(elem.tag.clone()));
|
||||||
ctx.push(FrameFragment::new(ctx, styles, frame));
|
ctx.push(FrameFragment::new(ctx, styles, frame).with_ignorant(true));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +313,15 @@ impl LayoutMath for Content {
|
|||||||
let axis = scaled!(ctx, styles, axis_height);
|
let axis = scaled!(ctx, styles, axis_height);
|
||||||
frame.set_baseline(frame.height() / 2.0 + axis);
|
frame.set_baseline(frame.height() / 2.0 + axis);
|
||||||
}
|
}
|
||||||
ctx.push(FrameFragment::new(ctx, styles, frame).with_spaced(true));
|
|
||||||
|
ctx.push(
|
||||||
|
FrameFragment::new(ctx, styles, frame)
|
||||||
|
.with_spaced(true)
|
||||||
|
.with_ignorant(matches!(
|
||||||
|
self.behaviour(),
|
||||||
|
Behaviour::Invisible | Behaviour::Ignorant
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,8 @@ impl MathRun {
|
|||||||
fragment.set_class(MathClass::Binary);
|
fragment.set_class(MathClass::Binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert spacing between the last and this item.
|
// Insert spacing between the last and this non-ignorant item.
|
||||||
|
if !fragment.is_ignorant() {
|
||||||
if let Some(i) = last {
|
if let Some(i) = last {
|
||||||
if let Some(s) = spacing(&resolved[i], space.take(), &fragment) {
|
if let Some(s) = spacing(&resolved[i], space.take(), &fragment) {
|
||||||
resolved.insert(i + 1, s);
|
resolved.insert(i + 1, s);
|
||||||
@ -85,6 +86,8 @@ impl MathRun {
|
|||||||
}
|
}
|
||||||
|
|
||||||
last = Some(resolved.len());
|
last = Some(resolved.len());
|
||||||
|
}
|
||||||
|
|
||||||
resolved.push(fragment);
|
resolved.push(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
tests/ref/math-spacing-ignorant.png
Normal file
BIN
tests/ref/math-spacing-ignorant.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 686 B |
@ -49,6 +49,16 @@ $integral f(x) thin dif x$,
|
|||||||
// Both are weak, collide
|
// Both are weak, collide
|
||||||
$integral f(x) #h(0.166em, weak: true)dif x$
|
$integral f(x) #h(0.166em, weak: true)dif x$
|
||||||
|
|
||||||
|
--- math-spacing-ignorant ---
|
||||||
|
// Test spacing with ignorant elements
|
||||||
|
$#metadata(none) "text"$ \
|
||||||
|
$#place(dx: 5em)[Placed] "text"$ \
|
||||||
|
// Operator spacing
|
||||||
|
$#counter("test").update(3) + b$ \
|
||||||
|
$#place(dx: 5em)[a] + b$
|
||||||
|
// Validate that ignorant elements are layouted
|
||||||
|
#context test(counter("test").get(), (3,))
|
||||||
|
|
||||||
--- issue-1052-math-number-spacing ---
|
--- issue-1052-math-number-spacing ---
|
||||||
// Test spacing after numbers in math.
|
// Test spacing after numbers in math.
|
||||||
$
|
$
|
||||||
|
Loading…
x
Reference in New Issue
Block a user