Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

+44 -3
View File
@@ -1,8 +1,11 @@
use serde::{Deserialize, Serialize};
/// A horizontal box in scaled points (1/65536 pt).
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct HorizontalBox {
/// Owned native TeX box geometry, painted without source re-parsing.
#[serde(default)]
pub native_box: Option<Box<crate::execution::NativeBox>>,
pub width: i64,
pub height: i64,
pub depth: i64,
@@ -17,6 +20,8 @@ pub struct HorizontalBox {
/// The inline formula this box paints, when it came from `$...$`.
#[serde(default)]
pub math: Option<Box<crate::math::MathAtom>>,
#[serde(default)]
pub math_geometry: Option<Box<crate::math_engine_tex::MathGeometry>>,
/// Link target when this glyph came from `\href`.
#[serde(default)]
pub link: Option<String>,
@@ -40,6 +45,8 @@ pub struct HorizontalBox {
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum InlineSegment {
Text(ParagraphFont, String),
/// A fixed horizontal kern, with no legal break inside it.
Kern(i64),
/// A run carrying its own type size, such as `{\small ...}`.
SizedText(ParagraphFont, i64, String),
/// A run raised or lowered like `\textsuperscript`.
@@ -751,6 +758,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
}
} else {
items.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: style.char_width,
height: style.line_height,
depth: 0,
@@ -758,6 +766,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
italic: 0,
font: None,
math: None,
math_geometry: None,
raise: 0,
color: None,
background: None,
@@ -1268,6 +1277,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} => {
if start + index + 1 == end {
line.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: *pre_break_width,
height: style.line_height,
depth: 0,
@@ -1275,6 +1285,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
italic: 0,
font: None,
math: None,
math_geometry: None,
raise: 0,
color: None,
background: None,
@@ -1284,6 +1295,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}));
} else if *no_break_width > 0 {
line.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: *no_break_width,
height: style.line_height,
depth: 0,
@@ -1291,6 +1303,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
italic: 0,
font: None,
math: None,
math_geometry: None,
raise: 0,
color: None,
background: None,
@@ -1656,6 +1669,7 @@ pub fn text_items_with_font(
previous_box = Some(items.len());
previous_character = Some(character);
items.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: font.width(slot),
height: font.height(character),
depth: font.depth(character),
@@ -1663,6 +1677,7 @@ pub fn text_items_with_font(
italic: font.italic(character),
font: None,
math: None,
math_geometry: None,
raise: 0,
color: None,
background: None,
@@ -1798,6 +1813,15 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
let mut seen_content = false;
for segment in segments {
match segment {
InlineSegment::Kern(width) => {
items.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: *width,
..HorizontalBox::default()
}));
previous = None;
seen_content = true;
}
InlineSegment::Text(font, text) => {
if text.is_empty() {
continue;
@@ -2036,6 +2060,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
for word in text.split_whitespace() {
if !first_word {
items.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: space_width,
height: 0,
depth: 0,
@@ -2043,6 +2068,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
italic: 0,
font: Some(*font),
math: None,
math_geometry: None,
raise: 0,
color: None,
background: None,
@@ -2126,8 +2152,14 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
order: 0,
});
}
let math = crate::math::layout_math(atom, &crate::cmr10::Cmr10Metrics::default());
let prepared = crate::math::prepare_math(
atom,
crate::math_engine_tex::MathStyle::Text,
&crate::cmr10::Cmr10Metrics::default(),
);
let math = prepared.metrics;
items.push(HorizontalItem::Box(HorizontalBox {
native_box: None,
width: math.width,
height: math.height,
depth: math.depth,
@@ -2135,6 +2167,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
italic: 0,
font: None,
math: Some(Box::new(atom.clone())),
math_geometry: prepared.geometry.map(Box::new),
size: None,
raise: 0,
color: None,
@@ -2672,7 +2705,13 @@ mod tests {
assert!(math.character.is_none());
assert_eq!(
math.width,
crate::math::layout_math(&atom, &crate::cmr10::Cmr10Metrics::default()).width
crate::math::prepare_math(
&atom,
crate::math_engine_tex::MathStyle::Text,
&crate::cmr10::Cmr10Metrics::default()
)
.metrics
.width
);
let glues = paragraph
.lines
@@ -2688,6 +2727,7 @@ mod tests {
fn split_at_keeps_each_part_last_line_descent() {
let boxed = |character: char, depth: i64| {
HorizontalItem::Box(HorizontalBox {
native_box: None,
width: 5 * 65_536,
height: 5 * 65_536,
depth,
@@ -2695,6 +2735,7 @@ mod tests {
italic: 0,
font: None,
math: None,
math_geometry: None,
raise: 0,
color: None,
background: None,