A few math fixes

This commit is contained in:
Laurenz 2022-12-09 21:22:23 +01:00
parent cd089b6194
commit c38d55614a
5 changed files with 29 additions and 10 deletions

View File

@ -61,7 +61,8 @@ impl Layout for MathNode {
) -> SourceResult<Fragment> {
let mut t = Texifier::new(styles);
self.texify(&mut t)?;
layout_tex(vt, &t.finish(), self.display, styles)
Ok(layout_tex(vt, &t.finish(), self.display, styles)
.unwrap_or(Fragment::frame(Frame::new(Size::zero()))))
}
}

View File

@ -14,7 +14,7 @@ pub fn layout_tex(
tex: &str,
display: bool,
styles: StyleChain,
) -> SourceResult<Fragment> {
) -> Result<Fragment, &'static str> {
// Load the font.
let variant = variant(styles);
let world = vt.world();
@ -27,11 +27,13 @@ pub fn layout_tex(
}
// Prepare the font context.
let font = font.expect("failed to find suitable math font");
let ctx = font
let Some(font) = font else { return Err("failed to find suitable math font") };
let Some(ctx) = font
.math()
.map(|math| FontContext::new(font.ttf(), math))
.expect("failed to create font context");
else {
return Err("failed to create math font context");
};
// Layout the formula.
let em = styles.get(TextNode::SIZE);
@ -45,7 +47,7 @@ pub fn layout_tex(
Error::Layout(LayoutError::Font(err)) => err.to_string(),
})
else {
panic!("failed to layout with rex: {tex}");
return Err("failed to layout math");
};
// Determine the metrics.

View File

@ -405,7 +405,15 @@ impl SyntaxKind {
Self::EnumNumbering(_) => "enumeration item numbering",
Self::DescItem => "description list item",
Self::Math => "math formula",
Self::Atom(_) => "math atom",
Self::Atom(s) => match s.as_str() {
"(" => "opening paren",
")" => "closing paren",
"{" => "opening brace",
"}" => "closing brace",
"[" => "opening bracket",
"]" => "closing bracket",
_ => "math atom",
},
Self::Script => "script",
Self::Frac => "fraction",
Self::AlignPoint => "alignment point",

View File

@ -338,9 +338,13 @@ impl<'s> Parser<'s> {
Some(SyntaxKind::RightParen) => self.inside(Group::Paren),
Some(SyntaxKind::Star) => self.inside(Group::Strong),
Some(SyntaxKind::Underscore) => self.inside(Group::Emph),
Some(SyntaxKind::Dollar) => {
self.groups.last().map(|group| group.kind) == Some(Group::Math)
}
Some(SyntaxKind::Dollar) => self
.groups
.iter()
.rev()
.skip_while(|group| matches!(group.kind, Group::MathRow(..)))
.next()
.map_or(false, |group| group.kind == Group::Math),
Some(SyntaxKind::Semicolon) => self.inside(Group::Expr),
Some(SyntaxKind::From) => self.inside(Group::Imports),
Some(SyntaxKind::Atom(s)) => match s.as_str() {

View File

@ -22,3 +22,7 @@ $ 1 + 2 = #{1 + 2} $
$ A sub:eq:not B $
```
<table>
---
// Error: 8 expected closing paren
$ sum_( $