Keep styled runs across incremental paragraph edits
This commit is contained in:
1 parent
4cf64a4d55
commit
36f2aa12d7
4 files changed
+127
-11
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
||||
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
||||
PDF opt-in with `?engine=route-d`). 525 workspace tests are green and no oracle
|
||||
PDF opt-in with `?engine=route-d`). 527 workspace tests are green and no oracle
|
||||
fixture is ignored.
|
||||
|
||||
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
|
||||
|
||||
+2
-2
@@ -353,7 +353,7 @@ M3 painter: `Page::display_list()` and `Halign::display_list()` produce absolute
|
||||
|
||||
PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open.
|
||||
|
||||
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright); true family/series/shape switching and Type1/CM outlines remain open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
|
||||
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright); styled runs are stored on the paragraph unit, so incremental edits rebuild them through `Engine::edit_runs` instead of flattening the paragraph back to one face; true family/series/shape switching and Type1/CM outlines remain open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
|
||||
|
||||
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
|
||||
|
||||
@@ -367,7 +367,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
|
||||
|
||||
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||
|
||||
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 525 tests (347 route-D unit, 33 engine binary unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 527 tests (348 route-D unit, 34 engine binary unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||
|
||||
### M0 incremental demo
|
||||
|
||||
|
||||
+19
-1
@@ -616,7 +616,13 @@ pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
|
||||
(next.clone(), engine)
|
||||
}
|
||||
};
|
||||
engine.edit(&next, unit as u64, paragraphs[unit].text.clone());
|
||||
let paragraph = ¶graphs[unit];
|
||||
match inline_runs(¶graph.text, paragraph.font) {
|
||||
Some(runs) => {
|
||||
engine.edit_runs(&next, unit as u64, paragraph.text.clone(), runs);
|
||||
}
|
||||
None => engine.edit(&next, unit as u64, paragraph.text.clone()),
|
||||
}
|
||||
let changed = engine.changed_pages().to_vec();
|
||||
let recomputed = engine.recomputed_pages();
|
||||
let json = render_json(&next, &engine, Some((&changed, recomputed)));
|
||||
@@ -2133,3 +2139,15 @@ mod inline_tests {
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod edit_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn paragraph_edits_keep_inline_runs() {
|
||||
let _ = preview_json("plain start").expect("preview");
|
||||
let json = edit_json("\\textbf{bold} start", 0).expect("edit");
|
||||
assert!(json.contains(r#"font-weight=\"bold\""#), "{json}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user