From 7cb5b424295a698172eb02708fa9ee2f45d513fb Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 21 Oct 2026 09:33:22 +0800 Subject: [PATCH] Preserve paragraph font faces across edits --- crates/texrra-tex-core/src/lib.rs | 51 ++++++++++++++++++++++++++----- docs/TEX_CORE.md | 4 ++- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 88362fe..2642702 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -449,21 +449,28 @@ impl Unit { /// Build a paragraph unit with an explicit font face. pub fn paragraph_with_face(id: UnitId, source: impl Into, font: ParagraphFont) -> Self { - let mut unit = Self::paragraph_with_style(id, source, ParagraphStyle::default()); - if let Some(VerticalItem::Paragraph(paragraph)) = unit.items.first_mut() { - paragraph.font = font; - } - unit + Self::paragraph_with_style_and_face(id, source, ParagraphStyle::default(), font) } pub fn paragraph_with_style( id: UnitId, source: impl Into, style: ParagraphStyle, + ) -> Self { + Self::paragraph_with_style_and_face(id, source, style, ParagraphFont::Roman) + } + + /// Build a paragraph unit with both an explicit style and font face. + pub fn paragraph_with_style_and_face( + id: UnitId, + source: impl Into, + style: ParagraphStyle, + font: ParagraphFont, ) -> Self { let source = source.into(); - let paragraph = + let mut paragraph = layout_paragraph_with_font(&source, &style, &crate::cmr10::Cmr10Metrics::new()); + paragraph.font = font; Self { id, source, @@ -1170,7 +1177,15 @@ impl Engine { next.units[unit_index].source = source; return; }; - next.units[unit_index] = Unit::paragraph_with_style(unit_id, source, style); + let font = next.units[unit_index] + .items + .iter() + .find_map(|item| match item { + VerticalItem::Paragraph(paragraph) => Some(paragraph.font), + _ => None, + }) + .unwrap_or(ParagraphFont::Roman); + next.units[unit_index] = Unit::paragraph_with_style_and_face(unit_id, source, style, font); self.relayout_from(&next, unit_index); } @@ -1543,6 +1558,28 @@ mod tests { assert_eq!(engine.checkpoints()[1].unit_index, 1); } + #[test] + fn editing_a_paragraph_preserves_its_font_face() { + let document = Document::new( + 51, + vec![Unit::paragraph_with_face( + 1, + "bold text", + ParagraphFont::Bold, + )], + ); + let mut engine = Engine::layout(&document); + engine.edit(&document, 1, "edited bold text"); + let list = engine.pages()[0].display_list(); + assert!(list.items.iter().any(|item| matches!( + item, + crate::render::DisplayItem::Text { + font: crate::render::FontFace::Bold, + .. + } + ))); + } + #[test] fn edit_on_second_page_only_rebuilds_second_page() { let mut document = document(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index bac3f42..def2f73 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -270,7 +270,9 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits. -The crate currently has 165 unit tests plus five route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 262 tests (165 route-D unit, 5 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path. +Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans for both display-list painting and PDF face selection. `Engine::edit` preserves the face when reflowing an edited paragraph. + +The crate currently has 166 unit tests plus five route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 263 tests (166 route-D unit, 5 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path. ### M0 incremental demo