f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Floating tables in a table cell, floating tables that follow each other, and the text after a
//! floating table.

use f_office_units::Length;

use crate::layout::testing::{
    NORMAL, filler, find, find_all, lay_out, on_base_with, p, pass_with, passes_mode, table,
};
use crate::numbering::Numbering;
use crate::quirks::FloatingTableBreaks;

/// A floating table 3000 twips wide that hangs on the text, with the attributes `placement`
/// of its `w:tblpPr` and one row for each of `labels`.
fn floating(placement: &str, labels: &[&str]) -> String {
    let properties =
        format!(r#"<w:tblpPr w:vertAnchor="text"{placement}/><w:tblW w:w="3000" w:type="dxa"/>"#);
    let cells: Vec<String> = labels.iter().map(|label| p(label)).collect();
    let rows: Vec<(&str, [&str; 1])> = cells.iter().map(|cell| ("", [cell.as_str()])).collect();
    let rows: Vec<(&str, &[&str])> = rows
        .iter()
        .map(|(props, cell)| (*props, &cell[..]))
        .collect();
    table(&properties, &[3000], &rows)
}

/// A table of one cell 9000 twips wide that holds `blocks`, and then the paragraph "z".
fn in_a_cell(blocks: &str) -> String {
    let outer = table(
        r#"<w:tblW w:w="9000" w:type="dxa"/>"#,
        &[9000],
        &[("", &[blocks])],
    );
    outer + &p("z")
}

const RIGHT: &str = r#" w:tblpXSpec="right""#;

#[test]
fn puts_two_floating_tables_of_a_cell_side_by_side() {
    // The second table stands at the cell's right edge on the baseline of the first, and the
    // row is as tall as with the first table alone.
    let both = floating("", &["l"]) + &floating(RIGHT, &["r"]) + &p("");
    let list = lay_out(&in_a_cell(&both), NORMAL, None);
    let (l, r) = (find(&list, 'l'), find(&list, 'r'));
    assert_eq!(l.2, r.2);
    assert!(r.1 - l.1 > 5000 * Length::UNITS_PER_TWIP);
    let alone = floating("", &["l"]) + &p("");
    let one = lay_out(&in_a_cell(&alone), NORMAL, None);
    assert_eq!(find(&list, 'z'), find(&one, 'z'));
    // An alignment down does nothing for a table that hangs on the text.
    let centered = floating("", &["l", "m", "n"])
        + &floating(r#" w:tblpXSpec="right" w:tblpYSpec="center""#, &["r"])
        + &p("");
    let list = lay_out(&in_a_cell(&centered), NORMAL, None);
    assert_eq!(find(&list, 'l').2, find(&list, 'r').2);
}

#[test]
fn floats_a_table_of_a_cell_that_hangs_on_the_text_down_whatever_its_anchor_across() {
    // Across from the margin or the page, the table stands where its twin across from the text
    // stands: offset from the cell's text column, with the paragraph beside it.
    let blocks = |across: &str| {
        p("a") + &floating(&format!(r#"{across} w:tblpX="1440" w:tblpY="200""#), &["l"]) + &p("b")
    };
    let text = lay_out(&in_a_cell(&blocks("")), NORMAL, None);
    let (l, b) = (find(&text, 'l'), find(&text, 'b'));
    assert!(b.1 < l.1);
    for across in [r#" w:horzAnchor="margin""#, r#" w:horzAnchor="page""#] {
        let list = lay_out(&in_a_cell(&blocks(across)), NORMAL, None);
        assert_eq!(find(&list, 'l'), l, "{across}");
        assert_eq!(find(&list, 'b'), b, "{across}");
    }
    // With a negative offset down it stays in the flow, below the paragraph before it.
    let raised = p("a") + &floating(r#" w:horzAnchor="margin" w:tblpY="-165""#, &["l"]) + &p("b");
    let list = lay_out(&in_a_cell(&raised), NORMAL, None);
    let (a, l, b) = (find(&list, 'a'), find(&list, 'l'), find(&list, 'b'));
    assert!(a.2 < l.2 && l.2 < b.2);
}

#[test]
fn lays_out_a_table_of_a_cell_without_room_beside_it_as_a_table_in_the_flow() {
    // A page holds 64 lines. The row cannot split but is taller than the page, so it splits:
    // its first cell has 150 lines and its second 80, then a table as wide as the cell that
    // hangs on the text, and "b". The table leaves no room beside it, so it stays in the flow
    // of the cell and breaks between the row's parts as the same table without `w:tblpPr`.
    let inner = |placement: &str| {
        let rows: Vec<String> = (0..20).map(|_| p("l")).collect();
        let rows: Vec<(&str, [&str; 1])> = rows.iter().map(|cell| ("", [cell.as_str()])).collect();
        let rows: Vec<(&str, &[&str])> = rows
            .iter()
            .map(|(props, cell)| (*props, &cell[..]))
            .collect();
        table(
            &format!(r#"{placement}<w:tblW w:w="6000" w:type="dxa"/>"#),
            &[6000],
            &rows,
        )
    };
    let lay = |placement: &str| {
        let second = filler(80) + &inner(placement) + &p("b");
        let rows: &[(&str, &[&str])] = &[("<w:cantSplit/>", &[&(filler(149) + &p("a")), &second])];
        let body = table(r#"<w:tblW w:w="9360" w:type="dxa"/>"#, &[3000, 6360], rows);
        let target = on_base_with(|p| p.with_variant(FloatingTableBreaks::AcrossPages));
        pass_with(&body, (NORMAL, None), None, Numbering::default(), target).list
    };
    let flow = lay("");
    let placed = lay(r#"<w:tblpPr w:vertAnchor="text" w:horzAnchor="margin" w:tblpY="0"/>"#);
    assert_eq!(placed.pages().len(), flow.pages().len());
    assert_eq!(find(&placed, 'a'), find(&flow, 'a'));
    assert_eq!(find(&placed, 'b'), find(&flow, 'b'));
    assert_eq!(find_all(&placed, 'l'), find_all(&flow, 'l'));
}

#[test]
fn flows_the_text_of_a_cell_beside_its_floating_table_and_grows_the_row() {
    // The paragraph starts on the baseline of the table's first row, right of the table, and
    // the row holds the three rows of the table.
    let blocks = floating("", &["l", "m", "n"]) + &p("b");
    let list = lay_out(&in_a_cell(&blocks), NORMAL, None);
    let (l, n, b, z) = (
        find(&list, 'l'),
        find(&list, 'n'),
        find(&list, 'b'),
        find(&list, 'z'),
    );
    assert_eq!(l.2, b.2);
    assert!(b.1 - l.1 >= 2800 * Length::UNITS_PER_TWIP);
    assert!(z.2 > n.2);
    // A table as wide as the cell stays in the flow, and the paragraph stands below it.
    let wide = table(
        r#"<w:tblpPr w:vertAnchor="text"/><w:tblW w:w="9000" w:type="dxa"/>"#,
        &[9000],
        &[("", &[&p("l")])],
    );
    let list = lay_out(&in_a_cell(&(wide + &p("b"))), NORMAL, None);
    assert!(find(&list, 'b').2 > find(&list, 'l').2);
}

#[test]
fn joins_floating_tables_with_the_same_properties_into_one_table() {
    // Two floating tables that follow each other directly: the second goes on below the
    // first as its next row, in a cell and in the body.
    let two = floating("", &["l"]) + &floating("", &["r"]) + &p("");
    let one = floating("", &["l", "r"]) + &p("");
    for wrap in [in_a_cell, |blocks: &str| blocks.to_owned() + &p("z")] {
        let list = lay_out(&wrap(&two), NORMAL, None);
        let joined = lay_out(&wrap(&one), NORMAL, None);
        for ch in ['l', 'r', 'z'] {
            assert_eq!(find_all(&list, ch), find_all(&joined, ch));
        }
        assert!(find(&list, 'r').2 > find(&list, 'l').2);
    }
    // A table with another placement does not go on the first one.
    let apart = floating("", &["l"]) + &floating(RIGHT, &["r"]) + &p("");
    let list = lay_out(&apart, NORMAL, None);
    assert_eq!(find(&list, 'l').2, find(&list, 'r').2);
}

/// The paragraph "a" with 10 pt after.
fn spaced_a() -> String {
    r#"<w:p><w:pPr><w:spacing w:before="0" w:after="200"/></w:pPr><w:r><w:t>a</w:t></w:r></w:p>"#
        .to_owned()
}

#[test]
fn keeps_the_bottom_distance_below_a_table_on_the_text_without_room_beside_it() {
    // A table as wide as the text stays in the flow, and the paragraph after it starts
    // `w:bottomFromText` below its bottom, where a space before of that size would put it, to
    // a device unit.
    let lay = |bottom: u32, after: &str| {
        let wide = table(
            &format!(
                r#"<w:tblpPr w:bottomFromText="{bottom}" w:vertAnchor="text"/><w:tblW w:w="9360" w:type="dxa"/>"#
            ),
            &[9360],
            &[("", &[&p("l")])],
        );
        find(&lay_out(&(p("t") + &wide + after), NORMAL, None), 'b')
    };
    let spaced_b =
        r#"<w:p><w:pPr><w:spacing w:before="200"/></w:pPr><w:r><w:t>b</w:t></w:r></w:p>"#;
    let (kept, spaced) = (lay(200, &p("b")), lay(0, spaced_b));
    assert!(kept.2.abs_diff(spaced.2) <= 1, "{kept:?} {spaced:?}");
    assert!(kept.2 > lay(0, &p("b")).2);
}

#[test]
fn keeps_the_space_after_the_paragraph_before_a_table_with_room_beside_it() {
    // Beside a narrow floating table, the paragraph after it starts below the space after of
    // the paragraph before the table, as without the table.
    let with_table = lay_out(
        &(spaced_a() + &floating("", &["l"]) + &p("b")),
        NORMAL,
        None,
    );
    let without = lay_out(&(spaced_a() + &p("b")), NORMAL, None);
    assert_eq!(find(&with_table, 'b').2, find(&without, 'b').2);
}

fn anchored_drawing(x_emu: i64, w_emu: i64, h_emu: i64, dist_b_emu: i64, wrap: &str) -> String {
    use crate::ns::{A, WP, WPS};
    format!(
        r#"<w:p><w:r><w:drawing><wp:anchor xmlns:wp="{WP}" distT="0" distB="{dist_b_emu}" distL="114300" distR="114300" simplePos="0" relativeHeight="1" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1"><wp:simplePos x="0" y="0"/><wp:positionH relativeFrom="margin"><wp:posOffset>{x_emu}</wp:posOffset></wp:positionH><wp:positionV relativeFrom="paragraph"><wp:posOffset>0</wp:posOffset></wp:positionV><wp:extent cx="{w_emu}" cy="{h_emu}"/><wp:effectExtent l="0" t="0" r="0" b="0"/>{wrap}<wp:docPr id="1" name="B"/><a:graphic xmlns:a="{A}"><a:graphicData><wps:wsp xmlns:wps="{WPS}"><wps:spPr><a:prstGeom prst="rect"/></wps:spPr><wps:bodyPr/></wps:wsp></a:graphicData></a:graphic></wp:anchor></w:drawing></w:r><w:r><w:t>a</w:t></w:r></w:p>"#
    )
}

#[test]
fn inline_table_clears_or_wraps_beside_floating_drawings() {
    let sq = r#"<wp:wrapSquare wrapText="bothSides"/>"#;
    let none = "<wp:wrapNone/>";
    let wide_tbl = table("", &[9360], &[("", &[&p("t")])]);
    let narrow_tbl = table("", &[4000], &[("", &[&p("t")])]);

    // 1. Wide inline table overlapping a 40 pt wrapSquare box moves down below the box (+ distB).
    let base_none = lay_out(
        &(anchored_drawing(0, 1_524_000, 508_000, 0, none) + &wide_tbl),
        NORMAL,
        None,
    );
    let below_sq = lay_out(
        &(anchored_drawing(0, 1_524_000, 508_000, 0, sq) + &wide_tbl),
        NORMAL,
        None,
    );
    let below_sq_db = lay_out(
        &(anchored_drawing(0, 1_524_000, 508_000, 127_000, sq) + &wide_tbl),
        NORMAL,
        None,
    );
    let t_none = find(&base_none, 't');
    let t_sq = find(&below_sq, 't');
    let t_sq_db = find(&below_sq_db, 't');
    // 40 pt box vs 10 pt anchor line pushes table down 30 pt (~125 dots); +10 pt distB adds ~42 dots.
    assert_eq!(t_sq.1, t_none.1);
    assert!(
        (t_sq.2 - t_none.2 - 125).abs() <= 1,
        "{t_sq:?} vs {t_none:?}"
    );
    assert!(
        (t_sq_db.2 - t_sq.2 - 42).abs() <= 1,
        "{t_sq_db:?} vs {t_sq:?}"
    );

    // 2. Narrow inline table (200 pt) on the left does NOT move for a right-side wrapSquare box.
    let right_sq = lay_out(
        &(anchored_drawing(4_318_000, 1_524_000, 508_000, 0, sq) + &narrow_tbl),
        NORMAL,
        None,
    );
    let narrow_none = lay_out(
        &(anchored_drawing(0, 1_524_000, 508_000, 0, none) + &narrow_tbl),
        NORMAL,
        None,
    );
    assert_eq!(find(&right_sq, 't'), find(&narrow_none, 't'));

    // 3. Narrow inline table (200 pt) overlapping a left-side 120 pt wrapSquare box shifts right
    //    beside the box (same y, x shifted by 120 + 9 = 129 pt).
    let beside = lay_out(
        &(anchored_drawing(0, 1_524_000, 508_000, 0, sq) + &narrow_tbl),
        NORMAL,
        None,
    );
    let t_beside = find(&beside, 't');
    let t_narrow = find(&narrow_none, 't');
    assert_eq!(t_beside.2, t_narrow.2);
    assert!(t_beside.1 > t_narrow.1, "{t_beside:?} vs {t_narrow:?}");

    // 4. A wrap wholly in the left margin does not push a table with negative tblInd down.
    let neg_ind = table(
        r#"<w:tblInd w:w="-120" w:type="dxa"/>"#,
        &[9360],
        &[("", &[&p("t")])],
    );
    let margin_sq = lay_out(
        &(anchored_drawing(-635_000, 495_300, 508_000, 0, sq) + &neg_ind),
        NORMAL,
        None,
    );
    let margin_none = lay_out(
        &(anchored_drawing(-635_000, 495_300, 508_000, 0, none) + &neg_ind),
        NORMAL,
        None,
    );
    assert_eq!(find(&margin_sq, 't'), find(&margin_none, 't'));
}

#[test]
fn inline_table_clears_right_wraps_and_polygon_bounds() {
    let sq = r#"<wp:wrapSquare wrapText="bothSides"/>"#;
    let none = "<wp:wrapNone/>";
    let wide_tbl = table("", &[9360], &[("", &[&p("t")])]);

    // Right-aligned narrow table beside a right-side wrap stays at the same y inside [col_left, sq.left].
    let right_tbl = table(r#"<w:jc w:val="right"/>"#, &[4000], &[("", &[&p("t")])]);
    let rgt_sq = lay_out(
        &(anchored_drawing(4_318_000, 1_524_000, 508_000, 0, sq) + &right_tbl),
        NORMAL,
        None,
    );
    let rgt_none = lay_out(
        &(anchored_drawing(0, 1_524_000, 508_000, 0, none) + &right_tbl),
        NORMAL,
        None,
    );
    let t_rgt_sq = find(&rgt_sq, 't');
    let t_rgt_none = find(&rgt_none, 't');
    assert_eq!(t_rgt_sq.2, t_rgt_none.2);
    assert!(t_rgt_sq.1 < t_rgt_none.1, "{t_rgt_sq:?} vs {t_rgt_none:?}");

    // A wrapTight half-height polygon (0..30 pt inside a 120 pt box) does not push a table
    // starting below the polygon's bottom down to the box's bottom.
    let half_poly = r#"<wp:wrapTight wrapText="bothSides"><wp:wrapPolygon edited="0"><wp:start x="0" y="0"/><wp:lineTo x="0" y="5400"/><wp:lineTo x="21600" y="5400"/><wp:lineTo x="21600" y="0"/><wp:lineTo x="0" y="0"/></wp:wrapPolygon></wp:wrapTight>"#;
    let body_poly =
        anchored_drawing(0, 1_524_000, 1_524_000, 0, half_poly) + &p("b") + &p("c") + &wide_tbl;
    let body_ctrl =
        anchored_drawing(0, 1_524_000, 1_524_000, 0, none) + &p("b") + &p("c") + &wide_tbl;
    let t_poly = find(&lay_out(&body_poly, NORMAL, None), 't');
    let t_ctrl = find(&lay_out(&body_ctrl, NORMAL, None), 't');
    assert_eq!(t_poly.2, t_ctrl.2);
}

#[test]
fn places_fitting_paragraphs_above_a_full_width_text_anchored_floating_table() {
    let lay = |body: &str| {
        let target = on_base_with(|p| p.with_variant(FloatingTableBreaks::AcrossPages));
        pass_with(body, (NORMAL, None), None, Numbering::default(), target).list
    };
    let wide = |tblp_y: i32, top_from: u32, bottom_from: u32| {
        table(
            &format!(
                r#"<w:tblpPr w:vertAnchor="text" w:horzAnchor="margin" w:tblpX="0" w:tblpY="{tblp_y}" w:topFromText="{top_from}" w:bottomFromText="{bottom_from}"/><w:tblW w:w="9360" w:type="dxa"/>"#
            ),
            &[9360],
            &[("", &[&p("r")])],
        )
    };

    // 1. With 15 pt above (`tblpY=300`), 10 pt "a" fits above the table and "b" drops below it.
    let fit = lay(&(p("t") + &wide(300, 0, 0) + &p("a") + &p("b")));
    let (t, a, r, b) = (
        find(&fit, 't'),
        find(&fit, 'a'),
        find(&fit, 'r'),
        find(&fit, 'b'),
    );
    assert!(
        t.2 < a.2 && a.2 < r.2 && r.2 < b.2,
        "{t:?} {a:?} {r:?} {b:?}"
    );

    // 2. When `topFromText` leaves only 5 pt above, "a" drops below the table.
    let blocked = lay(&(p("t") + &wide(300, 200, 0) + &p("a")));
    assert!(find(&blocked, 'r').2 < find(&blocked, 'a').2);

    // 3. The paragraph before the table consumes its `after` above the table, so a following
    //    paragraph that drops below keeps its full `before` rather than collapsing against it.
    let b_before =
        r#"<w:p><w:pPr><w:spacing w:before="240"/></w:pPr><w:r><w:t>b</w:t></w:r></w:p>"#;
    let dropped_after = lay(&(spaced_a() + &wide(300, 0, 0) + b_before));
    let dropped_plain = lay(&(spaced_a() + &wide(300, 0, 0) + &p("b")));
    let r_after = find(&dropped_after, 'r');
    let b_after = find(&dropped_after, 'b');
    let b_plain = find(&dropped_plain, 'b');
    assert!(r_after.2 < b_after.2, "{r_after:?} {b_after:?}");
    assert!(
        b_after.2 > b_plain.2,
        "before kept below table: {b_after:?} vs {b_plain:?}"
    );

    // 4. Inside a table cell, a full-width floating table with room above behaves the same way.
    let inner = table(
        r#"<w:tblpPr w:vertAnchor="text" w:horzAnchor="text" w:tblpX="0" w:tblpY="300"/><w:tblW w:w="8784" w:type="dxa"/>"#,
        &[8784],
        &[("", &[&p("r")])],
    );
    let in_cell = lay(&in_a_cell(&(p("t") + &inner + &p("a") + &p("b"))));
    assert!(find(&in_cell, 'a').2 < find(&in_cell, 'r').2);
    assert!(find(&in_cell, 'r').2 < find(&in_cell, 'b').2);
}

#[test]
fn clears_and_stacks_tables_after_a_full_width_text_anchored_floating_table() {
    let lay = |body: &str| {
        let target = on_base_with(|p| p.with_variant(FloatingTableBreaks::AcrossPages));
        pass_with(body, (NORMAL, None), None, Numbering::default(), target).list
    };
    let wide = table(
        r#"<w:tblpPr w:vertAnchor="text" w:horzAnchor="margin" w:tblpX="0" w:tblpY="300"/><w:tblW w:w="9360" w:type="dxa"/>"#,
        &[9360],
        &[("", &[&p("r")])],
    );

    // A following inline table that is taller than the room above drops below the floating table.
    let tall_inline = table("", &[9360], &[("", &[&p("i")]), ("", &[&p("j")])]);
    let after_tall = lay(&(p("t") + &wide + &tall_inline));
    assert!(find(&after_tall, 'r').2 < find(&after_tall, 'i').2);

    // A second full-width floating table stacks below the first while text still fits above it.
    let second_wide = table(
        r#"<w:tblpPr w:vertAnchor="text" w:horzAnchor="margin" w:tblpX="0" w:tblpY="300"/><w:tblW w:w="9360" w:type="dxa"/>"#,
        &[4680, 4680],
        &[("", &[&p("s"), &p("")])],
    );
    let stacked = lay(&(p("t") + &wide + &second_wide + &p("a")));
    assert!(find(&stacked, 'a').2 < find(&stacked, 'r').2);
    assert!(find(&stacked, 'r').2 < find(&stacked, 's').2);
}

#[test]
fn mode_14_narrow_floating_table_breaks_by_bottom_only_for_text_anchor() {
    let lay = |body: &str| {
        let target = on_base_with(|p| p.with_variant(FloatingTableBreaks::AcrossPages));
        pass_with(body, (NORMAL, Some(14)), None, Numbering::default(), target).list
    };
    let narrow = |tblp: &str| {
        table(
            &format!(
                r#"<w:tblpPr w:horzAnchor="margin" w:tblpXSpec="right" {tblp}/><w:tblW w:w="3000" w:type="dxa"/>"#
            ),
            &[3000],
            &[("", &[&p("a")]), ("", &[&p("b")]), ("", &[&p("c")])],
        )
    };
    // Body runs 1440..14400 twips; 63 lines reach 14040 twips (360 twips left).
    // With 200-twip rows: row "a" ends at 14240, row "b" starts at 14240 (< 14400) and ends at
    // 14440 (> 14400).
    let text_anchor = lay(&(filler(63) + &narrow(r#"w:vertAnchor="text" w:tblpY="0""#) + &p("z")));
    assert_eq!(find(&text_anchor, 'a').0, 0);
    assert_eq!(find(&text_anchor, 'b').0, 1);
    assert_eq!(find(&text_anchor, 'z').0, 1);

    // Anchored to page or margin at the same y (14040 twips from page / 12600 from margin),
    // mode 14 keeps row "b" on page 1 because its top lies above 14400.
    let page_anchor = lay(&(p("t") + &narrow(r#"w:vertAnchor="page" w:tblpY="14040""#) + &p("z")));
    assert_eq!(find(&page_anchor, 'b').0, 0);
    assert_eq!(find(&page_anchor, 'c').0, 1);

    let margin_anchor =
        lay(&(p("t") + &narrow(r#"w:vertAnchor="margin" w:tblpY="12600""#) + &p("z")));
    assert_eq!(find(&margin_anchor, 'b').0, 0);
    assert_eq!(find(&margin_anchor, 'c').0, 1);
}

#[test]
fn page_anchored_floating_table_wraps_preceding_text_and_moves_on_overflow() {
    let full_width = table(
        r#"<w:tblpPr w:vertAnchor="page" w:horzAnchor="margin" w:tblpX="0" w:tblpY="2296"/><w:tblW w:w="9360" w:type="dxa"/>"#,
        &[9360],
        &[
            (r#"<w:trHeight w:val="400" w:hRule="exact"/>"#, &[&p("r")]),
            (r#"<w:trHeight w:val="400" w:hRule="exact"/>"#, &[&p("s")]),
            (r#"<w:trHeight w:val="400" w:hRule="exact"/>"#, &[&p("t")]),
        ],
    );

    // A 60 pt line starting at the 72 pt top margin reaches 132 pt > tblpY (114.8 pt), so it
    // collides with the table's early wrap and wraps below it on page 1.
    let tall_a = r#"<w:p><w:pPr><w:spacing w:line="1200" w:lineRule="exact"/></w:pPr><w:r><w:t>a</w:t></w:r></w:p>"#;
    let fits = passes_mode(&(String::from(tall_a) + &full_width + &p("z")), Some(14)).list;
    let (fits_a, fits_r, fits_t) = (find(&fits, 'a'), find(&fits, 'r'), find(&fits, 't'));
    assert_eq!((fits_r.0, fits_a.0, fits_a.2 > fits_t.2), (0, 0, true));

    // When pushing preceding text below the table overflows page 1, the table moves to page 2 at
    // its page-relative position while the preceding text stays untouched on page 1.
    let overflow = passes_mode(
        &(String::from(tall_a) + &filler(56) + &p("b") + &full_width + &p("z")),
        Some(14),
    )
    .list;
    let (ov_a, ov_b, ov_r, ov_z) = (
        find(&overflow, 'a'),
        find(&overflow, 'b'),
        find(&overflow, 'r'),
        find(&overflow, 'z'),
    );
    assert_eq!(
        (
            ov_a.0,
            ov_b.0,
            ov_a.2 < fits_a.2,
            ov_r.0,
            ov_r.2,
            ov_z.0,
            ov_z.2 < ov_r.2,
        ),
        (0, 0, true, 1, fits_r.2, 1, true),
    );
}

#[test]
fn turned_cell_in_page_anchored_floating_table_ignores_outer_wraps() {
    let turned_cell = format!(
        r#"<w:tc><w:tcPr><w:tcW w:w="1000" w:type="dxa"/><w:textDirection w:val="btLr"/></w:tcPr>{}</w:tc>"#,
        p("v")
    );
    let upright_cell = format!(
        r#"<w:tc><w:tcPr><w:tcW w:w="8000" w:type="dxa"/></w:tcPr>{}</w:tc>"#,
        p("u")
    );
    let floating = format!(
        r#"<w:tbl><w:tblPr><w:tblpPr w:vertAnchor="page" w:horzAnchor="margin" w:tblpX="0" w:tblpY="1440"/><w:tblW w:w="9000" w:type="dxa"/></w:tblPr><w:tblGrid><w:gridCol w:w="1000"/><w:gridCol w:w="8000"/></w:tblGrid><w:tr><w:trPr><w:trHeight w:val="1200" w:hRule="exact"/></w:trPr>{turned_cell}{upright_cell}</w:tr></w:tbl>"#
    );
    let list = passes_mode(&floating, Some(15)).list;
    let (v, u) = (find(&list, 'v'), find(&list, 'u'));
    assert_eq!((v.0, u.0, v.1 < u.1), (0, 0, true));
}