Keep styled runs across incremental paragraph edits

This commit is contained in:
gloamxun committed 2026-10-24 02:18:21 +08:00
1 parent 4cf64a4d55
commit 36f2aa12d7
4 files changed
+127 -11

No files matched your search

+105 -7
View File
@@ -844,6 +844,9 @@ pub struct Unit {
/// editable through a plain source string.
#[serde(default)]
pub paragraph_style: Option<ParagraphStyle>,
/// Inline styled runs for a paragraph unit, when the paragraph mixes faces.
#[serde(default)]
pub paragraph_runs: Option<Vec<(ParagraphFont, String)>>,
/// Extra vertical glue before this unit (TeX `\vskip`/`\smallskip`).
#[serde(default)]
pub space_before: i64,
@@ -966,6 +969,7 @@ impl Unit {
id,
source: "halign".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -1258,6 +1262,7 @@ impl Unit {
source,
items: vec![VerticalItem::Paragraph(paragraph)],
paragraph_style: Some(style),
paragraph_runs: Some(runs),
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -1280,6 +1285,7 @@ impl Unit {
source,
items: vec![VerticalItem::Paragraph(paragraph)],
paragraph_style: Some(style),
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -1332,6 +1338,7 @@ impl Unit {
source: String::new(),
items,
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -3560,13 +3567,42 @@ impl Engine {
_ => None,
})
.unwrap_or(ParagraphFont::Roman);
let space_before = next.units[unit_index].space_before;
let space_stretch = next.units[unit_index].space_stretch;
let space_shrink = next.units[unit_index].space_shrink;
next.units[unit_index] = Unit::paragraph_with_style_and_face(unit_id, source, style, font);
next.units[unit_index].space_before = space_before;
next.units[unit_index].space_stretch = space_stretch;
next.units[unit_index].space_shrink = space_shrink;
let replacement = Unit::paragraph_with_style_and_face(unit_id, source, style, font);
self.replace_unit_and_replay(next, unit_index, replacement);
}
/// Replace a paragraph that carries inline styled runs, then replay the
/// pages from its checkpoint.
///
/// [`Self::edit`] cannot preserve runs: a plain source string has already
/// lost where each face begins and ends.
pub fn edit_runs(
&mut self,
document: &Document,
unit_id: UnitId,
source: impl Into<String>,
runs: Vec<(ParagraphFont, String)>,
) {
let Some(unit_index) = document.units.iter().position(|unit| unit.id == unit_id) else {
self.layout = Self::layout(document).layout;
self.layout.recomputed_pages = self.layout.pages.len();
self.layout.changed_pages = (0..self.layout.pages.len()).collect();
return;
};
let mut next = document.clone();
let Some(style) = next.units[unit_index].paragraph_style.clone() else {
next.units[unit_index].source = source.into();
return;
};
let replacement = Unit::paragraph_with_style_and_runs(unit_id, source, style, runs);
self.replace_unit_and_replay(next, unit_index, replacement);
}
fn replace_unit_and_replay(&mut self, mut next: Document, unit_index: usize, mut unit: Unit) {
unit.space_before = next.units[unit_index].space_before;
unit.space_stretch = next.units[unit_index].space_stretch;
unit.space_shrink = next.units[unit_index].space_shrink;
next.units[unit_index] = unit;
if self.layout.checkpoints.is_empty() {
if !self.relayout_paged_incremental(&next, unit_index) {
self.replace_with_paged(&next);
@@ -4129,6 +4165,7 @@ mod tests {
id: 7,
source: "footnote layout".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -4463,6 +4500,7 @@ mod tests {
id: index,
source: String::new(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -4487,6 +4525,7 @@ mod tests {
id,
source: "boxes".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -4539,6 +4578,7 @@ mod tests {
id: 0,
source: "penalty".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -4605,6 +4645,7 @@ mod tests {
id: 0,
source: text.to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -4666,6 +4707,7 @@ mod tests {
id: 0,
source: "float".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -4702,6 +4744,7 @@ mod tests {
id: 0,
source: "float".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -5410,6 +5453,7 @@ mod tests {
id: 0,
source: "bottom float".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -5448,6 +5492,7 @@ mod tests {
id: 0,
source: "tall bottom float".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -5483,6 +5528,7 @@ mod tests {
id: 0,
source: "top fraction".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -7509,6 +7555,7 @@ b & 2 \\
id: 0,
source: "top float".to_owned(),
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -8008,6 +8055,7 @@ b & 2 \\
depth: 30,
}],
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -8161,6 +8209,7 @@ b & 2 \\
depth: 300_000,
}],
paragraph_style: None,
paragraph_runs: None,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
@@ -8752,3 +8801,52 @@ b & 2 \\
);
}
}
#[cfg(test)]
mod runs_edit_tests {
use super::*;
fn bold_glyphs(engine: &Engine) -> usize {
engine
.pages()
.iter()
.flat_map(|page| page.display_list().items)
.filter(|item| {
matches!(
item,
crate::render::DisplayItem::Text {
font: crate::render::FontFace::Bold,
..
}
)
})
.count()
}
#[test]
fn editing_runs_preserves_mixed_faces() {
let style = ParagraphStyle::default();
let unit = Unit::paragraph_with_style_and_runs(
1,
"a \\textbf{b}",
style,
vec![
(ParagraphFont::Roman, "a ".to_string()),
(ParagraphFont::Bold, "b".to_string()),
],
);
let document = Document::new(2_000, vec![unit]);
let mut engine = Engine::layout(&document);
assert!(bold_glyphs(&engine) > 0);
engine.edit_runs(
&document,
1,
"a \\textbf{cc}",
vec![
(ParagraphFont::Roman, "a ".to_string()),
(ParagraphFont::Bold, "cc".to_string()),
],
);
assert!(bold_glyphs(&engine) > 0);
}
}