mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-26 10:48:38 +00:00
Layout: Word 2003's indent rules start a line right after a float before the indent (#5939) (#5940) (#6905)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: SenuraMalaka <19330762+SenuraMalaka@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
SenuraMalaka
parent
abc04c1b41
commit
224bfdb2a0
@@ -137,6 +137,12 @@ pub struct Settings {
|
||||
/// Keep contextual paragraph spacing inside legacy table cells (`allowSpaceOfSameStyleInTable`).
|
||||
/// Word ignores the layout override in compatibility mode 15, but the flag still round-trips.
|
||||
pub allow_space_of_same_style_in_table: bool,
|
||||
/// `w:doNotSuppressIndentation` (a `.doc`'s `Copts.fWW11IndentRules`): Word 2003's indent
|
||||
/// rules beside floating objects. An unnumbered paragraph's line starts right after a floating
|
||||
/// object before it on the line, its own indent ignored (ECMA-376 Part 4 §14.8.3.12). Word 2013's
|
||||
/// layout (compatibility mode 15) does not use it.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub do_not_suppress_indentation: bool,
|
||||
/// Drawing grid spacing in points, across and down (View › Gridlines); ECMA-376's default is 1/8 inch.
|
||||
pub grid_h: f32,
|
||||
pub grid_v: f32,
|
||||
@@ -319,6 +325,7 @@ impl Default for Settings {
|
||||
sum_paragraph_spacing: false,
|
||||
do_not_expand_shift_return: false,
|
||||
allow_space_of_same_style_in_table: false,
|
||||
do_not_suppress_indentation: false,
|
||||
grid_h: DEFAULT_GRID,
|
||||
grid_v: DEFAULT_GRID,
|
||||
math: None,
|
||||
|
||||
@@ -27,6 +27,9 @@ struct Breaker<'e> {
|
||||
/// Word 2013+ (compatibility mode 15) fits more text on a justified line by shrinking its
|
||||
/// spaces (`space_room`); never on lines with tabs.
|
||||
shrink_spaces: bool,
|
||||
/// Word 2003's indent rules (`w:doNotSuppressIndentation`) before Word 2013's layout, for an
|
||||
/// unnumbered paragraph: a row starts right after a floating object before the indent.
|
||||
tight_indent: bool,
|
||||
/// Word stretches a justified line ending in a manual line break unless the document says not to.
|
||||
expand_break: bool,
|
||||
/// Hyphenation Options: the zone, and how many lines in a row may end with a hyphen.
|
||||
@@ -190,6 +193,13 @@ impl<'e> Breaker<'e> {
|
||||
hanging_at: if rp.indent_first < 0.0 { Some(rp.indent_left) } else { None },
|
||||
tab_origin: if settings.odf.is_some_and(|o| o.tabs_from_indent) { rp.indent_left } else { 0.0 },
|
||||
shrink_spaces: rp.align == Align::Justify && settings.compat_mode >= wordcraft_doc::COMPAT_MODE_CURRENT,
|
||||
tight_indent: settings.do_not_suppress_indentation
|
||||
&& settings.compat_mode < wordcraft_doc::COMPAT_MODE_CURRENT
|
||||
&& level.is_none()
|
||||
&& pl.label.is_none()
|
||||
// A drop cap sits at the indent: starting its lines at a float's edge would put
|
||||
// them over the letter.
|
||||
&& pl.drop_cap.is_none(),
|
||||
expand_break: !settings.do_not_expand_shift_return,
|
||||
zone: Some(settings.hyphenation_zone).filter(|z| z.is_finite() && *z >= 0.0).unwrap_or(HYPHENATION_ZONE).min(1584.0),
|
||||
hyph_limit: settings.consecutive_hyphen_limit as usize,
|
||||
@@ -236,9 +246,12 @@ impl<'e> Breaker<'e> {
|
||||
let mut guard = 0;
|
||||
loop {
|
||||
let lo = self.row_left(pl);
|
||||
match row_spans(self.exclusions, self.top, self.est_h, self.table_h, lo, self.base_right).map(|mut spans| (spans.pop_front(), spans)) {
|
||||
let row = (self.top, self.est_h, self.table_h);
|
||||
match row_spans(self.exclusions, row, (lo, self.base_right), self.tight_indent).map(|mut spans| (spans.pop_front(), spans)) {
|
||||
Ok((Some((a, b)), rest)) => {
|
||||
let narrowed = !rest.is_empty() || a > lo || b < self.base_right;
|
||||
// Under Word 2003's indent rules a row can start left of the indent too.
|
||||
let moved = if self.tight_indent { (a - lo).abs() > 0.01 } else { a > lo };
|
||||
let narrowed = !rest.is_empty() || moved || b < self.base_right;
|
||||
self.row_rest = rest;
|
||||
self.row_first = self.lines.len();
|
||||
return (a, b.max(a + MIN_LINE_WIDTH), narrowed);
|
||||
@@ -1064,6 +1077,23 @@ impl Scan {
|
||||
/// paragraph beside a floating table in a 21.3pt gap but not in an 18.6pt one.
|
||||
const MIN_SPAN: f32 = 20.0;
|
||||
|
||||
/// Where a row starts under Word 2003's indent rules: right after the floating objects that end
|
||||
/// at or before the indent `lo` on this row and let text past their right side (their outline's
|
||||
/// right edge for Tight and Through wrapping), never left of the paragraph's own left edge (0) or
|
||||
/// right of `lo`; `lo` if there are none.
|
||||
fn tight_row_start(exclusions: &[Exclusion], (top, h, table_h): (f32, f32, f32), lo: f32) -> f32 {
|
||||
exclusions
|
||||
.iter()
|
||||
.filter(|e| !e.top_bottom && e.side != WrapText::Left && e.right <= lo && e.intersects_row(top, h, table_h, f32::NEG_INFINITY, lo))
|
||||
.filter_map(|e| match &e.contour {
|
||||
None => Some(e.right),
|
||||
Some(c) => c.occupied(top - e.top, top + h - e.top).into_iter().map(|(_, b)| b + e.left).reduce(f32::max),
|
||||
})
|
||||
.reduce(f32::max)
|
||||
// A float out in the left margin doesn't pull the text into the margin after it.
|
||||
.map_or(lo, |edge| edge.max(0.0).min(lo))
|
||||
}
|
||||
|
||||
/// How far below a line's top its baseline sits. Multiple spacing at or above single keeps the
|
||||
/// text at the top of the line and puts the extra leading below the baseline; below single it
|
||||
/// shrinks the whole box, ascent included (Word's output for docxide-pdf's fixtures: 1.15-spaced
|
||||
@@ -1078,8 +1108,17 @@ fn baseline_offset(spacing: LineSpacing, asc: f32, height: f32, desc: f32) -> f3
|
||||
|
||||
/// The spans of a row at `top` (about `h` tall) between `lo` and `hi` that text can use around
|
||||
/// the exclusions, left to right (Square wrapping uses both sides of an object). `Err(y)`: none
|
||||
/// here, try again at `y` (below what's in the way).
|
||||
fn row_spans(exclusions: &[Exclusion], top: f32, h: f32, table_h: f32, lo: f32, hi: f32) -> Result<VecDeque<(f32, f32)>, f32> {
|
||||
/// here, try again at `y` (below what's in the way). With `tight_indent` (Word 2003's indent
|
||||
/// rules, `w:doNotSuppressIndentation`) a row starts right after a floating object that ends
|
||||
/// before `lo`, the indent ignored (ECMA-376 Part 4 §14.8.3.12: a paragraph indented 1" tightly
|
||||
/// wraps an object at 0.25").
|
||||
fn row_spans(
|
||||
exclusions: &[Exclusion],
|
||||
(top, h, table_h): (f32, f32, f32),
|
||||
(lo, hi): (f32, f32),
|
||||
tight_indent: bool,
|
||||
) -> Result<VecDeque<(f32, f32)>, f32> {
|
||||
let lo = if tight_indent { tight_row_start(exclusions, (top, h, table_h), lo) } else { lo };
|
||||
let here: Vec<&Exclusion> = exclusions.iter().filter(|e| e.intersects_row(top, h, table_h, lo, hi)).collect();
|
||||
if let Some(below) = here.iter().filter(|e| e.top_bottom).map(|e| e.bottom).reduce(f32::max) {
|
||||
return Err(below);
|
||||
|
||||
@@ -3809,6 +3809,113 @@ fn text_wraps_around_square_float() {
|
||||
assert!(l.pages[0].items.iter().any(|i| matches!(i, Placed::Shape { .. })));
|
||||
}
|
||||
|
||||
/// The left edges of a paragraph's lines beside a 144 × 100 pt square-wrapped rectangle at the
|
||||
/// column's top left (9 pt from text), and of its first line below the rectangle, in
|
||||
/// compatibility `mode` with Word 2003's indent rules on or off. The paragraph is indented
|
||||
/// 216 pt; `float` adjusts the object, `para` the paragraph, and `numbered` makes it a list item.
|
||||
fn word_2003_indent_lefts(
|
||||
mode: u32,
|
||||
rules: bool,
|
||||
float: &dyn Fn(&mut wordcraft_doc::para::Float),
|
||||
numbered: bool,
|
||||
para: &dyn Fn(&mut ParaProps),
|
||||
) -> (Vec<f32>, Option<f32>) {
|
||||
let mut d = Document::from_text(&"Words beside the picture here. ".repeat(20));
|
||||
d.settings.compat_mode = mode;
|
||||
d.settings.do_not_suppress_indentation = rules;
|
||||
let num = numbered.then(|| Arc::make_mut(&mut d.numbering).add_list(wordcraft_doc::ListKind::Numbered));
|
||||
d.format_paragraphs(&Pos::body(0, 0), &Pos::body(0, 0), &|p| {
|
||||
p.indent_left = Some(216.0);
|
||||
p.indent_first = Some(0.0);
|
||||
p.numbering = num.map(|num| wordcraft_doc::props::NumRef { num, level: 0 });
|
||||
para(p);
|
||||
})
|
||||
.unwrap();
|
||||
let mut f = wordcraft_doc::para::Float {
|
||||
wrap: wordcraft_doc::para::Wrap::Square,
|
||||
h_rel: wordcraft_doc::para::Anchor::Column,
|
||||
v_rel: wordcraft_doc::para::Anchor::Paragraph,
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
dist: 9.0,
|
||||
..Default::default()
|
||||
};
|
||||
float(&mut f);
|
||||
let shape = InlineObject::Shape {
|
||||
kind: wordcraft_doc::para::ShapeKind::Rectangle,
|
||||
w: 144.0,
|
||||
h: 100.0,
|
||||
fill: None,
|
||||
stroke: None,
|
||||
stroke_width: 1.0,
|
||||
float: f,
|
||||
story: None,
|
||||
freeform: None,
|
||||
effects: Default::default(),
|
||||
extra: Default::default(),
|
||||
};
|
||||
// After the first word, so a drop cap takes the paragraph's first letter.
|
||||
d.insert_object(&Pos::body(0, 6), shape, &Default::default()).unwrap();
|
||||
let l = lay(&d);
|
||||
let Placed::Lines { para, .. } = l.pages[0].items.iter().find(|i| matches!(i, Placed::Lines { story: StoryRef::Body, .. })).unwrap() else {
|
||||
panic!()
|
||||
};
|
||||
let beside = para.lines.iter().filter(|ln| ln.top < 100.0).map(|ln| ln.left).collect();
|
||||
let below = para.lines.iter().find(|ln| ln.top > 120.0).map(|ln| ln.left);
|
||||
(beside, below)
|
||||
}
|
||||
|
||||
/// Word 2003's indent rules (`w:doNotSuppressIndentation`, a `.doc`'s `Copts.fWW11IndentRules`): an
|
||||
/// unnumbered paragraph indented past a floating object on its left starts its lines right after
|
||||
/// the object, its indent ignored (ECMA-376 Part 4 §14.8.3.12). In a Word 2003-mode `.doc` saved
|
||||
/// by Word, empty paragraphs indented 216 pt sit right after a text box ending 165 pt into the
|
||||
/// column, in the 71 pt gap before a second box where the indent would leave under 20 pt. Below
|
||||
/// the object, and in Word 2013's layout, the indent applies.
|
||||
#[test]
|
||||
fn word_2003_indent_rules_start_lines_right_after_a_float_before_the_indent() {
|
||||
let lefts = |mode: u32, rules: bool| word_2003_indent_lefts(mode, rules, &|_| {}, false, &|_| {});
|
||||
for mode in [11, 12, 14] {
|
||||
let (beside, below) = lefts(mode, true);
|
||||
assert!(
|
||||
!beside.is_empty() && beside.iter().all(|x| (x - 153.0).abs() < 0.01),
|
||||
"mode {mode}: lines start after the object and its 9 pt distance: {beside:?}"
|
||||
);
|
||||
assert_eq!(below, Some(216.0), "mode {mode}: below the object the indent applies");
|
||||
}
|
||||
assert_eq!(lefts(11, false).0[0], 216.0, "without the option the indent applies beside the object");
|
||||
assert_eq!(lefts(15, true).0[0], 216.0, "Word 2013's layout keeps the indent");
|
||||
}
|
||||
|
||||
/// Under Word 2003's indent rules a line starts after a Tight or Through-wrapped object's outline
|
||||
/// where the line meets it, not after the outline's widest point: here a right triangle, its
|
||||
/// point at the top left, 144 pt wide (and 9 pt from text) only at its foot.
|
||||
#[test]
|
||||
fn word_2003_indent_rules_follow_a_tight_objects_outline() {
|
||||
use wordcraft_doc::para::Wrap;
|
||||
use wordcraft_doc::wrap::WrapPolygon;
|
||||
let poly = WrapPolygon::new(true, [(0, 0), (0, 21_600), (21_600, 21_600)]).map(Arc::new);
|
||||
for wrap in [Wrap::Tight, Wrap::Through] {
|
||||
let (beside, _) = word_2003_indent_lefts(11, true, &|f| (f.wrap, f.wrap_polygon) = (wrap, poly.clone()), false, &|_| {});
|
||||
assert!(beside.len() > 2 && beside[0] < 60.0, "{wrap:?}: the first line starts near the point: {beside:?}");
|
||||
assert!(beside.windows(2).all(|w| w[0] < w[1]) && beside.iter().all(|&x| x <= 153.0 + 0.01), "{wrap:?}: each line further out: {beside:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Word 2003's indent rules leave the indent alone for a numbered paragraph (its number sits at
|
||||
/// the indent) and for one with a drop cap (the letter does), and never start a line left of the
|
||||
/// paragraph's own edge after an object out in the margin.
|
||||
#[test]
|
||||
fn word_2003_indent_rules_keep_the_indent_where_they_do_not_apply() {
|
||||
let (numbered, _) = word_2003_indent_lefts(11, true, &|_| {}, true, &|_| {});
|
||||
assert!(numbered.iter().all(|&x| x >= 216.0 - 0.01), "a numbered paragraph keeps its indent: {numbered:?}");
|
||||
let (dropped, _) = word_2003_indent_lefts(11, true, &|_| {}, false, &|p| p.drop_cap = Some(3));
|
||||
assert!(dropped.iter().all(|&x| x >= 216.0 - 0.01), "lines beside a drop cap stay right of it: {dropped:?}");
|
||||
// An object in the left margin, its right side and distance from text 44 pt short of the column.
|
||||
let (margin, _) = word_2003_indent_lefts(11, true, &|f| f.x = -197.0, false, &|_| {});
|
||||
assert!(margin.iter().all(|&x| x >= -0.01), "no line starts in the margin: {margin:?}");
|
||||
assert!((margin[0] - 0.0).abs() < 0.01, "the line starts at the paragraph's edge: {margin:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lines_arrowheads_sit_at_its_ends() {
|
||||
// A line runs from its frame's top left to its bottom right (DrawingML's `line`, as it is
|
||||
|
||||
+3
-2
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user