mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-04 11:46:53 -06:00
Merge pull request #3833
With AutoCraft's conflict resolution d46b3593.
This commit is contained in:
@@ -149,6 +149,8 @@ pub(crate) fn env_hash(doc: &Document, cache: &mut LayoutCache, opts: &LayoutOpt
|
||||
let outline = (opts.view == ViewMode::Outline).then(|| opts.outline.as_ref().is_some_and(|o| o.plain));
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
styles.hash(&mut h);
|
||||
// The compatibility mode changes paragraph layout too (widow control in table cells).
|
||||
doc.settings.compat_mode.hash(&mut h);
|
||||
opts.show_hidden.hash(&mut h);
|
||||
opts.hide_deleted.hash(&mut h);
|
||||
opts.hide_marked.hash(&mut h);
|
||||
|
||||
@@ -101,6 +101,11 @@ fn is_special(c: char) -> bool {
|
||||
fn paragraph_props(p: &Paragraph, env: &ParaEnv) -> ResolvedPara {
|
||||
let doc = env.doc;
|
||||
let mut rp = doc.styles.resolve_para_in(&p.props, env.table.map(|t| &t.para));
|
||||
// Before Word 2013 layout (compatibility mode 15) a row split across pages divides its cells'
|
||||
// text at any line: widow/orphan control doesn't apply in a table (measured in modes 12, 14).
|
||||
if env.table.is_some() && doc.settings.compat_mode < wordcraft_doc::COMPAT_MODE_CURRENT {
|
||||
rp.widow_control = false;
|
||||
}
|
||||
if let Some(spacing) = env.read_line_spacing.filter(|s| s.is_finite() && (1.0..=2.0).contains(s)) {
|
||||
rp.line_spacing = wordcraft_doc::props::LineSpacing::Multiple(spacing);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,8 @@ pub(crate) fn footer_table_wraps(ctx: &mut Ctx, pb: &PageBuilder) -> Vec<WrapAre
|
||||
/// The room a table in the text needs to start on a page: its repeated header rows and the least
|
||||
/// room its first row can start in (whole, or the part of it that splits off). A paragraph kept
|
||||
/// with the table goes over with it when that much is not left, as in Word.
|
||||
/// Before Word 2013 layout (compatibility mode 15) the header rows may stay alone at the foot of a
|
||||
/// page, so the paragraph keeps with them only.
|
||||
pub(crate) fn table_start_need(ctx: &mut Ctx, t: &wordcraft_doc::Table, block: usize, width: f32) -> f32 {
|
||||
// A look ahead: list and equation numbers in the cells don't count yet.
|
||||
let (counters, eq_count) = (ctx.counters.clone(), ctx.eq_count);
|
||||
@@ -80,6 +82,10 @@ pub(crate) fn table_start_need(ctx: &mut Ctx, t: &wordcraft_doc::Table, block: u
|
||||
(ctx.counters, ctx.eq_count) = (counters, eq_count);
|
||||
let headers = t.rows.iter().take_while(|r| r.props.header).count();
|
||||
let head: f32 = tl.rows.iter().take(headers).map(|r| r.height).sum();
|
||||
if headers > 0 && ctx.doc.settings.compat_mode < wordcraft_doc::COMPAT_MODE_CURRENT {
|
||||
// With the border below the last of them, as `place_table` places them.
|
||||
return head + tl.rows.get(headers - 1).map_or(0.0, |r| r.edge);
|
||||
}
|
||||
let (Some(row), Some(laid)) = (t.rows.get(headers), tl.rows.get(headers)) else { return head };
|
||||
// ponytail: bisection assumes starting a row gets no harder with more room.
|
||||
let (mut lo, mut hi) = (0.0f32, laid.height + laid.edge);
|
||||
@@ -259,9 +265,11 @@ pub(crate) fn place_table(ctx: &mut Ctx, pb: &mut PageBuilder, t: &wordcraft_doc
|
||||
t.rows.get(ri).is_some_and(|r| r.props.height_rule == HeightRule::AtLeast && r.props.height.is_some_and(|h| h > room + 0.01))
|
||||
};
|
||||
// Like Word, repeated header rows don't stay alone at the bottom of a page: when the first row
|
||||
// after them can't start under them there, the table starts on the next page.
|
||||
// after them can't start under them there, the table starts on the next page. Before Word
|
||||
// 2013 layout (compatibility mode 15) they do (measured in modes 12 and 14).
|
||||
let first = header_rows.len();
|
||||
if !header_rows.is_empty()
|
||||
&& ctx.doc.settings.compat_mode >= wordcraft_doc::COMPAT_MODE_CURRENT
|
||||
&& !pb.at_top()
|
||||
&& let Some(row) = tl.rows.get(first)
|
||||
{
|
||||
|
||||
@@ -18375,6 +18375,95 @@ fn a_caption_kept_with_a_table_counts_its_space_after() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Before Word 2013 layout (compatibility mode 15, measured in Word for Mac in modes 12 and 14) a
|
||||
/// row split across pages divides its cells' text at any line, the repeated header row may stay
|
||||
/// alone at the foot of a page, and a caption kept with the table keeps with the header row only.
|
||||
/// The caption's space before grows a point at a time under lines exactly 12 pt high, so it lands
|
||||
/// at every offset from the foot of the page, whatever the font's metrics.
|
||||
#[test]
|
||||
fn before_word_2013_layout_a_kept_caption_goes_with_the_header_row_only() {
|
||||
let (mut one_line, mut header_alone) = (false, false);
|
||||
// 44 lines end 120 pt above the bottom margin.
|
||||
let fill = 44;
|
||||
for push in 0..120 {
|
||||
let mut d = Document::from_text(&format!("{}Caption", "filler\n".repeat(fill)));
|
||||
d.settings.compat_mode = wordcraft_doc::LEGACY_COMPAT_MODE;
|
||||
d.format_paragraphs(&Pos::body(0, 0), &Pos::body(fill - 1, 0), &|p| {
|
||||
p.line_spacing = Some(wordcraft_doc::props::LineSpacing::Exactly(12.0));
|
||||
p.space_before = Some(0.0);
|
||||
p.space_after = Some(0.0);
|
||||
})
|
||||
.unwrap();
|
||||
// (No space after the caption: the table starts right under it.)
|
||||
d.format_paragraphs(&Pos::body(fill, 0), &Pos::body(fill, 0), &|p| {
|
||||
p.keep_next = Some(true);
|
||||
p.space_before = Some(push as f32);
|
||||
p.space_after = Some(0.0);
|
||||
})
|
||||
.unwrap();
|
||||
let mut t = Table::new(2, 1, 468.0);
|
||||
t.rows[0].props.header = true;
|
||||
t.rows[0].cells[0].blocks = vec![wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text("Header", Default::default()))];
|
||||
t.rows[1].cells[0].blocks =
|
||||
vec![wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text(&"first row words go on and on ".repeat(60), Default::default()))];
|
||||
d.insert_block(StoryRef::Body, &Path::top(fill + 1), wordcraft_doc::Block::Table(t)).unwrap();
|
||||
let l = lay(&d);
|
||||
let page = l
|
||||
.pages
|
||||
.iter()
|
||||
.find(|p| p.items.iter().any(|it| matches!(it, Placed::Lines { story: StoryRef::Body, path, .. } if *path == Path::top(fill))))
|
||||
.unwrap();
|
||||
let rows: Vec<usize> = page.items.iter().filter_map(|it| if let Placed::Cell { row, .. } = it { Some(*row) } else { None }).collect();
|
||||
let lines: usize = page
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|it| match it {
|
||||
Placed::Lines { path, l0, l1, .. } if path.0.len() > 1 && path.0.get(1) == Some(&1) => Some(l1 - l0),
|
||||
_ => None,
|
||||
})
|
||||
.sum();
|
||||
assert!(rows.contains(&0), "{push} pt before the caption: the caption is alone on its page");
|
||||
one_line |= lines == 1;
|
||||
header_alone |= !rows.contains(&1);
|
||||
}
|
||||
assert!(one_line && header_alone, "one line of the first row: {one_line}, the header row alone: {header_alone}");
|
||||
}
|
||||
|
||||
/// Before Word 2013 layout a cell's text drops widow/orphan control whatever the table's style:
|
||||
/// a table with no style at all (no `w:tblStyle`) is still a table.
|
||||
#[test]
|
||||
fn before_word_2013_layout_cells_of_a_table_without_a_style_drop_widow_control() {
|
||||
for (mode, styled) in
|
||||
[(wordcraft_doc::LEGACY_COMPAT_MODE, true), (wordcraft_doc::LEGACY_COMPAT_MODE, false), (wordcraft_doc::COMPAT_MODE_CURRENT, false)]
|
||||
{
|
||||
let mut d = Document::from_text("before");
|
||||
d.settings.compat_mode = mode;
|
||||
let mut t = Table::new(1, 1, 468.0);
|
||||
if !styled {
|
||||
t.props.style = None;
|
||||
}
|
||||
t.rows[0].cells[0].blocks = vec![wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text("cell", Default::default()))];
|
||||
d.insert_block(StoryRef::Body, &Path::top(1), wordcraft_doc::Block::Table(t)).unwrap();
|
||||
let widow = cell_para(&lay(&d), 0, 0).rp.widow_control;
|
||||
assert_eq!(widow, mode == wordcraft_doc::COMPAT_MODE_CURRENT, "compatibility mode {mode}, table style {styled}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A layout cache reused after the compatibility mode changes lays the cells' paragraphs out
|
||||
/// again: their widow control depends on the mode.
|
||||
#[test]
|
||||
fn a_reused_layout_cache_follows_a_change_of_compatibility_mode() {
|
||||
let mut d = Document::from_text("before");
|
||||
let mut t = Table::new(1, 1, 468.0);
|
||||
t.rows[0].cells[0].blocks = vec![wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text("cell", Default::default()))];
|
||||
d.insert_block(StoryRef::Body, &Path::top(1), wordcraft_doc::Block::Table(t)).unwrap();
|
||||
let mut cache = LayoutCache::new();
|
||||
d.settings.compat_mode = wordcraft_doc::LEGACY_COMPAT_MODE;
|
||||
assert!(!cell_para(&layout(&d, &mut cache, &LayoutOptions::default()), 0, 0).rp.widow_control);
|
||||
d.settings.compat_mode = wordcraft_doc::COMPAT_MODE_CURRENT;
|
||||
assert!(cell_para(&layout(&d, &mut cache, &LayoutOptions::default()), 0, 0).rp.widow_control);
|
||||
}
|
||||
|
||||
/// A header paragraph beside a Square float on its last lines only: those lines are shorter than
|
||||
/// the first. A click on the first line past the short last line's end is still on the header's
|
||||
/// text, so a Behind Text picture under it is not grabbed there (#5601 review).
|
||||
|
||||
Reference in New Issue
Block a user