Preserve paragraph font faces across edits
This commit is contained in:
1 parent
763ae99e8e
commit
7cb5b42429
2 files changed
+47
-8
No files matched your search
@@ -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<String>, 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<String>,
|
||||
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<String>,
|
||||
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();
|
||||
|
||||
+3
-1
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user