Render display math in the route-D preview

This commit is contained in:
gloamxun committed 2026-10-20 21:57:12 +08:00
1 parent 15880c884d
commit 63593e7ff3
4 files changed
+48 -3

No files matched your search

+27
View File
@@ -7,6 +7,8 @@
use std::cell::RefCell;
use std::fmt::Write as _;
use texrra_tex_core::cmr10::Cmr10Metrics;
use texrra_tex_core::math::parse_math;
use texrra_tex_core::pdf::display_lists_to_pdf;
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem};
@@ -78,6 +80,7 @@ struct SourceParagraph {
text: String,
font: ParagraphFont,
line_height: i64,
math: bool,
}
fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
@@ -105,6 +108,21 @@ fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
/// a full LaTeX reader; it exists so the live endpoint can show headings and
/// emphasis while the semantic layer is built out.
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some(math) = raw
.strip_prefix("$$")
.and_then(|text| text.strip_suffix("$$"))
.or_else(|| {
raw.strip_prefix("\\[")
.and_then(|text| text.strip_suffix("\\]"))
})
{
return SourceParagraph {
text: math.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
math: true,
};
}
let candidates = [
("\\section", ParagraphFont::Bold, 18),
("\\textbf", ParagraphFont::Bold, 12),
@@ -116,6 +134,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
text: text.to_string(),
font,
line_height,
math: false,
};
}
}
@@ -123,6 +142,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
text: raw.to_string(),
font: ParagraphFont::Roman,
line_height: 12,
math: false,
}
}
@@ -131,6 +151,13 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
.iter()
.enumerate()
.map(|(index, paragraph)| {
if paragraph.math {
return Unit::math(
index as u64,
parse_math(&paragraph.text),
&Cmr10Metrics::default(),
);
}
let style = ParagraphStyle {
line_width: 480 * SCALE,
left_skip: 0,