Add minipage and vbox block boxes

This commit is contained in:
gloamxun committed 2026-10-19 13:05:43 +08:00
1 parent 2552008d4b
commit 5fa301fdcd
7 files changed
+116 -20

No files matched your search

+48 -1
View File
@@ -375,6 +375,11 @@ fn consume_environment_preamble(
start_index
}
struct MinipageBuilder {
runs: Vec<Run>,
width: Option<common::Scaled>,
}
struct InlineBoxSpec {
runs: Vec<Run>,
width: Option<common::Scaled>,
@@ -414,6 +419,7 @@ struct Parser {
box_frame: bool,
box_width: Option<common::Scaled>,
inline_boxes: Vec<InlineBoxSpec>,
minipage: Option<MinipageBuilder>,
centered: Option<Vec<Run>>,
current_source: Option<SourceLocation>,
paragraph_source: Option<SourceLocation>,
@@ -454,6 +460,7 @@ impl Parser {
box_frame: true,
box_width: None,
inline_boxes: Vec::new(),
minipage: None,
centered: None,
current_source: None,
paragraph_source: None,
@@ -574,6 +581,7 @@ impl Parser {
self.append_text(&pending_text);
}
self.finish_heading();
self.finish_minipage();
self.flush_paragraph();
if let Some(candidate) = rich_candidate
&& candidate.end_index == expanded.items.len()
@@ -604,6 +612,7 @@ impl Parser {
&& self.framed_box.is_none()
&& self.centered.is_none()
&& self.inline_boxes.is_empty()
&& self.minipage.is_none()
&& !self.bold
&& !self.italic
&& !self.monospace
@@ -918,6 +927,13 @@ impl Parser {
if self.table.is_some() {
self.set_table_alignments(argument);
}
if let Some(minipage) = &mut self.minipage
&& minipage.width.is_none()
{
minipage.width = parse_dimension(argument.trim()).map(|width_mm| {
common::Scaled((width_mm / 25.4 * 72.0_f64 * 65_536.0).round() as i32)
});
}
}
expanded::FrontendEventKind::BeginEnvironment {
name: environment, ..
@@ -1025,6 +1041,24 @@ impl Parser {
});
}
fn finish_minipage(&mut self) {
let Some(mut minipage) = self.minipage.take() else {
return;
};
trim_run_end(&mut minipage.runs);
if minipage.runs.is_empty() {
return;
}
let width = minipage
.width
.unwrap_or_else(|| common::Scaled((160.0 / 25.4 * 72.0_f64 * 65_536.0).round() as i32));
self.push_block(Block::FramedBox {
runs: minipage.runs,
frame: false,
width: Some(width),
});
}
fn finish_heading(&mut self) {
if let Some(mut heading) = self.heading.take() {
trim_run_end(&mut heading.runs);
@@ -1255,6 +1289,7 @@ impl Parser {
fn target_runs(&mut self) -> &mut Vec<Run> {
let paragraph_target = self.inline_boxes.is_empty()
&& self.minipage.is_none()
&& self.centered.is_none()
&& self.framed_box.is_none()
&& self.metadata_capture.is_none()
@@ -1271,6 +1306,8 @@ impl Parser {
}
if let Some(spec) = self.inline_boxes.last_mut() {
&mut spec.runs
} else if let Some(minipage) = &mut self.minipage {
&mut minipage.runs
} else if let Some(runs) = &mut self.centered {
runs
} else if let Some(runs) = &mut self.framed_box {
@@ -1370,7 +1407,9 @@ impl Parser {
}
fn paragraph_break(&mut self) {
if !self.lists.is_empty() || self.table.is_some() {
if self.minipage.is_some() {
self.append_text(" ");
} else if !self.lists.is_empty() || self.table.is_some() {
self.append_text(" ");
} else {
self.flush_paragraph();
@@ -1476,6 +1515,14 @@ impl Parser {
self.push_block(Block::Centered { runs });
}
}
(true, "minipage") => {
self.flush_paragraph();
self.minipage = Some(MinipageBuilder {
runs: Vec::new(),
width: None,
});
}
(false, "minipage") => self.finish_minipage(),
(true, "thebibliography") => {
self.flush_paragraph();
self.bibliography = Some(BibliographyBuilder::default());