summaryrefslogtreecommitdiff
path: root/library/src/math
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-01-22 13:27:49 +0100
committerLaurenz <laurmaedje@gmail.com>2023-01-22 13:27:49 +0100
commitea378e89b4f2267bb85ec56c905111a6c73d4721 (patch)
tree41d72ea4e55ee86de0cc0a50b761c8d6c75c44a6 /library/src/math
parenta50cb588236a9258271d68b22b2c07fe71d19553 (diff)
Better math atoms
Diffstat (limited to 'library/src/math')
-rw-r--r--library/src/math/atom.rs47
1 files changed, 28 insertions, 19 deletions
diff --git a/library/src/math/atom.rs b/library/src/math/atom.rs
index 14246147..5b35d289 100644
--- a/library/src/math/atom.rs
+++ b/library/src/math/atom.rs
@@ -10,7 +10,7 @@ use super::*;
/// ## Category
/// math
#[func]
-#[capable(Texify)]
+#[capable(LayoutMath)]
#[derive(Debug, Hash)]
pub struct AtomNode(pub EcoString);
@@ -21,26 +21,35 @@ impl AtomNode {
}
}
-impl Texify for AtomNode {
- fn texify(&self, t: &mut Texifier) -> SourceResult<()> {
- let multi = self.0.graphemes(true).count() > 1;
- if multi {
- t.push_str("\\mathrm{");
- }
-
- for c in self.0.chars() {
- let supportive = c == '|';
- if supportive {
- t.support();
+impl LayoutMath for AtomNode {
+ fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
+ let mut chars = self.0.chars();
+ if let Some(glyph) = chars
+ .next()
+ .filter(|_| chars.next().is_none())
+ .and_then(|c| GlyphFragment::try_new(ctx, c))
+ {
+ // A single letter that is available in the math font.
+ if ctx.style.size == MathSize::Display
+ && glyph.class() == Some(MathClass::Large)
+ {
+ let height = scaled!(ctx, display_operator_min_height);
+ ctx.push(glyph.stretch_vertical(ctx, height, Abs::zero()));
+ } else {
+ ctx.push(glyph);
}
- t.push_escaped(c);
- if supportive {
- t.support();
+ } else if self.0.chars().all(|c| c.is_ascii_digit()) {
+ // A number that should respect math styling and can therefore
+ // not fall back to the normal text layout.
+ let mut vec = vec![];
+ for c in self.0.chars() {
+ vec.push(GlyphFragment::new(ctx, c).into());
}
- }
-
- if multi {
- t.push_str("}");
+ let frame = MathRow(vec).to_frame(ctx);
+ ctx.push(frame);
+ } else {
+ // Anything else is handled by Typst's standard text layout.
+ TextNode(self.0.clone()).pack().layout_math(ctx)?;
}
Ok(())