f-office-docx 0.2.2

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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! The compatibility mode of a document and the rules that depend on it.
//!
//! The setting `w:compatSetting w:name="compatibilityMode"` says which version of Word the
//! document's layout follows. [`CompatibilityMode`] has one variant for each class of modes
//! that a measured rule tells apart, and [`CompatibilityMode::read`] is the only code that
//! turns the setting's number, which the reader of numbers reads, into a class. [`ModeRules`] names what each rule does in the document's mode.
//! The settings build it once for a document, and layout asks it by the rule's name: no layout
//! code compares a mode.

use crate::params;

/// The compatibility mode of a document, as the classes that Word's rules tell apart. The
/// order of the variants is the order of the modes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum CompatibilityMode {
    /// A compatibility mode below 14, such as 11 and 12, and a document that names no mode.
    #[default]
    Below14,
    /// Compatibility mode 14.
    Mode14,
    /// Compatibility mode 15, and every mode above it.
    Mode15,
}

impl CompatibilityMode {
    /// The mode that the number of the setting `compatibilityMode` names. A document without
    /// the setting, a value that is not a number (which the reader of numbers reads as
    /// missing) and a number below 14 give [`Self::Below14`], and a number above 15 gives
    /// [`Self::Mode15`].
    ///
    /// Word gives a body the same lines without the setting and in the modes 11 and 12, and
    /// the same lines in mode 16 as in mode 15.
    #[must_use]
    pub fn read(number: Option<i64>) -> Self {
        match number {
            Some(number) if number >= i64::from(params::MODE_15_NUMBER) => Self::Mode15,
            Some(number) if number >= i64::from(params::MODE_14_NUMBER) => Self::Mode14,
            _ => Self::Below14,
        }
    }
}

/// Where the rules of a table's shape are asked.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum TableLevel {
    /// A table of the body, a header, a footer or a note.
    #[default]
    Outer,
    /// A table in a table cell.
    Nested,
}

/// What the rules that depend on the compatibility mode do in one document. Each method names
/// the behavior of one rule, and its comment names the parameter with the mode from which the
/// rule holds.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ModeRules {
    /// The document's mode.
    mode: CompatibilityMode,
    /// The level of the table whose shape asks.
    level: TableLevel,
    /// Whether the document turns the OpenType features setting on
    /// (`w:compatSetting w:name="enableOpenTypeFeatures"`).
    open_type_features: bool,
}

impl ModeRules {
    /// The rules of a document of the compatibility mode `mode`.
    #[must_use]
    pub fn of(mode: CompatibilityMode) -> Self {
        Self {
            mode,
            level: TableLevel::Outer,
            open_type_features: false,
        }
    }

    /// The rules of the same document with the OpenType features setting on or off
    /// (`w:compatSetting w:name="enableOpenTypeFeatures"`).
    #[must_use]
    pub fn with_open_type_features(self, on: bool) -> Self {
        Self {
            open_type_features: on,
            ..self
        }
    }

    /// The rules for the shape of a table: those of a table in a table cell when `nested`.
    /// Such a table takes the edge rule of compatibility mode 15 in every mode. The rule for a
    /// wide right-to-left table goes with it: the shape of a nested table had mode 15 for both
    /// rules before this type. A nested table gets only these two rules; every other rule keeps
    /// the document's mode.
    #[must_use]
    pub fn for_table(self, nested: bool) -> Self {
        let level = if nested {
            TableLevel::Nested
        } else {
            self.level
        };
        Self { level, ..self }
    }

    /// Whether the document's mode is `first` or a later one.
    fn holds_from(self, first: CompatibilityMode) -> bool {
        self.mode >= first
    }

    /// Whether automatic hyphenation keeps a word whole when the room that the line leaves for
    /// it is no wider than the hyphenation zone (before `HYPHENATION_MODE`).
    #[must_use]
    pub fn hyphenation_zone_holds(self) -> bool {
        !self.holds_from(params::HYPHENATION_MODE)
    }

    /// The fewest letters that automatic hyphenation leaves before and after a point
    /// (`HYPHEN_LEAST_BEFORE`, and `HYPHEN_LEAST_AFTER` from `HYPHENATION_MODE`,
    /// `OLDER_HYPHEN_LEAST_AFTER` before it).
    #[must_use]
    pub fn hyphen_least_letters(self) -> (usize, usize) {
        let after = if self.holds_from(params::HYPHENATION_MODE) {
            params::HYPHEN_LEAST_AFTER
        } else {
            params::OLDER_HYPHEN_LEAST_AFTER
        };
        (params::HYPHEN_LEAST_BEFORE, after)
    }

    /// Whether Word joins the standard ligatures that a run turns on (`LIGATURE_MODE`).
    #[must_use]
    pub fn joins_ligatures(self) -> bool {
        self.holds_from(params::LIGATURE_MODE)
    }

    /// Whether a joined space run keeps the words on both of its sides on one line
    /// (`JOINED_SPACE_MODE`).
    #[must_use]
    pub fn joins_space_runs(self) -> bool {
        self.holds_from(params::JOINED_SPACE_MODE)
    }

    /// Whether `w:wordWrap` turned off breaks inside words (`WORD_WRAP_MODE`).
    #[must_use]
    pub fn breaks_words_when_word_wrap_off(self) -> bool {
        self.holds_from(params::WORD_WRAP_MODE)
    }

    /// Whether the text after a tab to a right, center or decimal stop at or past the line's end
    /// ends at most at its limit where the target's quirk says so (`ALIGNED_TAB_PAST_END_MODE`).
    #[must_use]
    pub fn limits_text_past_the_line_end(self) -> bool {
        self.holds_from(params::ALIGNED_TAB_PAST_END_MODE)
    }

    /// Whether a tab to a custom left stop past the line's end starts the next line where the
    /// target's quirk says so (`LEFT_STOP_PAST_END_MODE`).
    #[must_use]
    pub fn moves_tab_to_left_stop_past_end(self) -> bool {
        self.holds_from(params::LEFT_STOP_PAST_END_MODE)
    }

    /// Whether default tab stops at negative offsets (`from < 0`) truncate `from / step` toward
    /// zero without adding 1 (`NEGATIVE_DEFAULT_TAB_MODE`), so exact negative multiples yield zero
    /// advance.
    #[must_use]
    pub(crate) fn truncates_negative_default_tabs(self) -> bool {
        self.holds_from(params::NEGATIVE_DEFAULT_TAB_MODE)
    }

    /// Whether a paragraph's first line comes after the first of its leading page breaks only,
    /// and not after all of them, where the target's quirk starts it after them
    /// (`LEADING_BREAK_MODE`).
    #[must_use]
    pub fn starts_after_first_leading_break(self) -> bool {
        self.holds_from(params::LEADING_BREAK_MODE)
    }

    /// The length of the text pieces of a paragraph, in characters (`TEXT_PIECE_MODE`).
    #[must_use]
    pub fn text_piece_length(self) -> usize {
        if self.holds_from(params::TEXT_PIECE_MODE) {
            params::TEXT_PIECE_LENGTH
        } else {
            params::OLD_MODE_TEXT_PIECE_LENGTH
        }
    }

    /// Whether Word reads the setting `w:doNotUseIndentAsNumberingTabStop`, which only the
    /// modes below `NUMBERING_INDENT_SETTING_MODE` do.
    #[must_use]
    pub fn reads_numbering_indent_setting(self) -> bool {
        !self.holds_from(params::NUMBERING_INDENT_SETTING_MODE)
    }

    /// Whether the outer edge of a table's left border, and not the text of its first cell,
    /// sits at the table indent (`EDGE_ALIGNED_MODE`). A nested table has this in every mode
    /// ([`Self::for_table`]).
    #[must_use]
    pub fn aligns_table_edge(self) -> bool {
        self.level == TableLevel::Nested || self.holds_from(params::EDGE_ALIGNED_MODE)
    }

    /// Whether justified lines shrink their spaces (`COMPRESSING_MODE`).
    #[must_use]
    pub fn shrinks_justified_spaces(self) -> bool {
        self.holds_from(params::COMPRESSING_MODE)
    }

    /// Whether the stretched spaces of a filled justified line take the shares that the quirk
    /// `StretchedSpaceShare` says (`WIDTH_SHARE_MODE`). In an earlier mode, each takes the same
    /// share.
    #[must_use]
    pub fn shares_stretched_spaces_by_quirk(self) -> bool {
        self.holds_from(params::WIDTH_SHARE_MODE)
    }

    /// Whether a justified line of a paragraph aligned `lowKashida` lengthens one joint of each
    /// of its last words that can take a kashida, where the quirk `KashidaJustification` says so
    /// (below `KASHIDA_WORDS_MODE`).
    #[must_use]
    pub fn lengthens_last_words(self) -> bool {
        !self.holds_from(params::KASHIDA_WORDS_MODE)
    }

    /// Whether a justified line may stretch a no-break space, where the quirk
    /// `NoBreakSpaceShare` says (below `NO_BREAK_SHARE_MODE`).
    #[must_use]
    pub fn may_stretch_no_break_spaces(self) -> bool {
        !self.holds_from(params::NO_BREAK_SHARE_MODE)
    }

    /// Whether a drawing in the body that text wraps around, placed from its paragraph, stays
    /// below the top margin (`MARGIN_KEEPING_MODE`).
    #[must_use]
    pub fn keeps_drawings_below_top_margin(self) -> bool {
        self.holds_from(params::MARGIN_KEEPING_MODE)
    }

    /// Whether a line that holds only a column break has the height of the break's run. If
    /// not, that line takes no room (`COLUMN_BREAK_LINE_MODE`).
    #[must_use]
    pub fn column_break_line_has_height(self) -> bool {
        self.holds_from(params::COLUMN_BREAK_LINE_MODE)
    }

    /// Whether a target whose multiple-spaced object-alone line scales the spacing above single
    /// spacing by its objects' runs (`ObjectLineMultiple::ObjectRun`) scales it so in this
    /// document: from `OBJECT_LINE_RUN_MODE`. Before it, the paragraph mark's line scales it.
    #[must_use]
    pub fn scales_object_lines_by_object_runs(self) -> bool {
        self.holds_from(params::OBJECT_LINE_RUN_MODE)
    }

    /// Whether a line beside a frame that the text flows around takes a piece as narrow as
    /// beside a drawing. If not, the piece must be an inch wide (`FRAME_PIECE_MODE`).
    #[must_use]
    pub fn frames_take_narrow_pieces(self) -> bool {
        self.holds_from(params::FRAME_PIECE_MODE)
    }

    /// Whether the band of a frame at the text that keeps the text below it, from the frame's
    /// top less its distance, reaches each line of the paragraph before it by the line's
    /// bottom. If not, the last line of that paragraph reaches into the band with the
    /// paragraph's space after (`FRAME_BAND_TOP_MODE`).
    #[must_use]
    pub fn frame_band_reaches_from_frame_top(self) -> bool {
        self.holds_from(params::FRAME_BAND_TOP_MODE)
    }

    /// Whether a frame at the text right after a paragraph, whose own band moves none of that
    /// paragraph's lines, counts from the lines as the later frames' bands leave them. If not,
    /// it keeps the place that the paragraph gave it before those bands
    /// (`FRAME_PLACE_FROM_LINES_MODE`).
    #[must_use]
    pub fn frame_without_band_follows_the_lines(self) -> bool {
        self.holds_from(params::FRAME_PLACE_FROM_LINES_MODE)
    }

    /// Whether a table in the flow whose rows all go leaves the space before of the paragraph
    /// after it, at the top of a page, as it would be without the table. If not, that paragraph
    /// has no space before there (`DELETED_TABLE_TOP_MODE`).
    #[must_use]
    pub fn deleted_table_keeps_top_space(self) -> bool {
        self.holds_from(params::DELETED_TABLE_TOP_MODE)
    }

    /// Whether the paragraph that a run of paragraphs that show nothing and whose marks are
    /// hidden joins draws the border lines of the run's first paragraph, when it has a border
    /// or a fill of its own (`HIDDEN_MARK_BORDERS_MODE`).
    #[must_use]
    pub fn joins_hidden_mark_borders(self) -> bool {
        self.holds_from(params::HIDDEN_MARK_BORDERS_MODE)
    }

    /// Whether a line above the footnotes of its page needs its whole height, and the last
    /// line of a paragraph the space after too (`NOTE_WHOLE_ROOM_MODE`).
    #[must_use]
    pub fn notes_need_whole_line_room(self) -> bool {
        self.holds_from(params::NOTE_WHOLE_ROOM_MODE)
    }

    /// Whether the rule above the rest of a footnote is as long as the separator's rule, and
    /// not as wide as the column (`NOTE_NARROW_CONTINUATION_MODE`).
    #[must_use]
    pub fn shortens_continuation_rule(self) -> bool {
        self.holds_from(params::NOTE_NARROW_CONTINUATION_MODE)
    }

    /// Whether a row with `w:cantSplit` splits in a table with `w:tblpPr`
    /// (`PLACED_ROW_SPLITS_MODE`).
    #[must_use]
    pub fn splits_placed_rows(self) -> bool {
        self.holds_from(params::PLACED_ROW_SPLITS_MODE)
    }

    /// Whether a row of a floating table that breaks across pages fits when its bottom lies
    /// above the body's end, rather than its top (`FLOATING_ROW_BOTTOM_MODE`).
    #[must_use]
    pub fn fits_floating_rows_by_bottom(self) -> bool {
        self.holds_from(params::FLOATING_ROW_BOTTOM_MODE)
    }

    /// Whether a header row is kept with the next row of its table (`HEADER_ROWS_KEPT_MODE`).
    #[must_use]
    pub fn keeps_header_rows(self) -> bool {
        self.holds_from(params::HEADER_ROWS_KEPT_MODE)
    }

    /// Whether the row below the header rows that repeat at the top of a page gets the room of
    /// the line drawn above it, which only the modes below `HEADER_LINE_ROOM_MODE` give it.
    #[must_use]
    pub fn gives_header_line_room(self) -> bool {
        !self.holds_from(params::HEADER_LINE_ROOM_MODE)
    }

    /// Whether a right-to-left table wider than the text starts at the left edge of the column
    /// of a right-to-left section (`WIDE_RIGHT_TO_LEFT_TABLE_MODE`). A nested table has this
    /// in every mode ([`Self::for_table`]).
    #[must_use]
    pub fn starts_wide_right_to_left_table_at_left(self) -> bool {
        self.level == TableLevel::Nested || self.holds_from(params::WIDE_RIGHT_TO_LEFT_TABLE_MODE)
    }

    /// Whether a paragraph that starts a page after a page break before it has no space above
    /// it (`PAGE_BREAK_SPACE_MODE`).
    #[must_use]
    pub fn drops_space_after_page_break(self) -> bool {
        self.holds_from(params::PAGE_BREAK_SPACE_MODE)
    }

    /// Whether each footnote of a section of more than one text column stands at the foot of
    /// the column that holds its reference, as wide as that column (before `NOTE_FLOW_MODE`).
    #[must_use]
    pub fn notes_in_reference_column(self) -> bool {
        !self.holds_from(params::NOTE_FLOW_MODE)
    }

    /// Whether a drop cap goes to the next page with the paragraph after it, and is no link of
    /// a chain of kept paragraphs (`DROP_CAP_WITH_TEXT_MODE`).
    #[must_use]
    pub fn moves_drop_cap_with_text(self) -> bool {
        self.holds_from(params::DROP_CAP_WITH_TEXT_MODE)
    }

    /// Whether a target that takes a later column's edge by its parts
    /// (`ColumnEdgeHalves::ColumnParts`) takes it so in this document: before
    /// `COLUMN_EDGE_WHOLE_MODE`.
    #[must_use]
    pub fn takes_column_edge_by_parts(self) -> bool {
        !self.holds_from(params::COLUMN_EDGE_WHOLE_MODE)
    }

    /// Whether a target whose kerned runs step at the nearest dot by the mode alone
    /// (`KernedDeviceWidths::NearestOrOwn`) steps so in this document: from `KERNED_STEP_MODE`.
    #[must_use]
    pub fn steps_kerned_runs_at_nearest_dot(self) -> bool {
        self.holds_from(params::KERNED_STEP_MODE)
    }

    /// Whether a target whose kerned runs step at the nearest dot by the mode and the OpenType
    /// features setting (`KernedDeviceWidths::Nearest`) steps so in this document: from
    /// `KERNED_STEP_MODE`, and in an earlier mode with the setting.
    #[must_use]
    pub fn steps_kerned_runs_at_nearest_dot_with_open_type_features(self) -> bool {
        self.steps_kerned_runs_at_nearest_dot() || self.open_type_features
    }

    /// Whether a target that reads the `kern` table before a mode
    /// (`KerningTableRead::KernTableBeforeMode`) gives a kerned pair the value of the face's
    /// kerning source in this document: from `KERNED_KERN_FEATURE_MODE`, and in an earlier mode
    /// when the OpenType features setting is on.
    #[must_use]
    pub fn kerns_by_the_kern_feature(self) -> bool {
        self.holds_from(params::KERNED_KERN_FEATURE_MODE) || self.open_type_features
    }

    /// Whether a paragraph with `w:keepNext` that may split splits at the page's foot, so that
    /// only its last lines go with the next paragraph (`KEPT_SPLIT_MODE`).
    #[must_use]
    pub fn splits_kept_paragraphs(self) -> bool {
        self.holds_from(params::KEPT_SPLIT_MODE)
    }

    /// Whether widow control applies in a cell of a row that splits (`CELL_WIDOW_MODE`).
    #[must_use]
    pub fn controls_widows_in_cells(self) -> bool {
        self.holds_from(params::CELL_WIDOW_MODE)
    }

    /// Whether the empty paragraph that ends a section right after a table keeps its line on a
    /// target whose continuous section ends keep their spaces
    /// (`SECTION_END_AFTER_TABLE_LINE_MODE`).
    #[must_use]
    pub fn keeps_section_end_line_after_table(self) -> bool {
        self.holds_from(params::SECTION_END_AFTER_TABLE_LINE_MODE)
    }

    /// How the edge check rounds the shares of a justified line's free space that it counts on
    /// a target that counts Word's shares (`EdgeShares::ExactFree`): their sum toward zero from
    /// `SUMMED_SHARE_MODE`, and before it equal shares of the line's whole parts, to the nearest,
    /// with the parts that they leave to the first spaces.
    #[must_use]
    pub fn share_rounding(self) -> ShareRounding {
        if self.holds_from(params::SUMMED_SHARE_MODE) {
            ShareRounding::SumTowardZero
        } else {
            ShareRounding::RemainderFirst
        }
    }
}

/// How the edge check of right-edge steps rounds the shares of a justified line's free space to
/// whole parts of `params::SHARE_PARTS_PER_POINT` ([`ModeRules::share_rounding`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShareRounding {
    /// The sum of the shares up to a glyph, toward zero.
    SumTowardZero,
    /// The line's free space in whole parts to the nearest, a half away from zero, an equal share
    /// of it toward zero for each stretched space, and one part more, away from zero, for each of
    /// the line's first stretched spaces, as many as the equal shares leave parts.
    RemainderFirst,
}

#[cfg(test)]
mod tests {
    use super::{CompatibilityMode, ModeRules};

    #[test]
    fn reads_a_missing_value_and_the_older_modes_as_one_class() {
        for value in [None, Some(11), Some(12), Some(13), Some(-1)] {
            assert_eq!(CompatibilityMode::read(value), CompatibilityMode::Below14);
        }
        assert_eq!(CompatibilityMode::read(Some(14)), CompatibilityMode::Mode14);
        for value in [15, 16] {
            assert_eq!(
                CompatibilityMode::read(Some(value)),
                CompatibilityMode::Mode15
            );
        }
    }

    #[test]
    fn cuts_pieces_of_256_characters_from_mode_15_and_of_512_before() {
        let length = |value| ModeRules::of(CompatibilityMode::read(value)).text_piece_length();
        assert_eq!(length(Some(15)), 256);
        assert_eq!(length(Some(16)), 256);
        assert_eq!(length(Some(14)), 512);
        assert_eq!(length(None), 512);
    }

    #[test]
    fn shares_stretched_spaces_by_the_quirk_from_mode_15() {
        let shares = |value| {
            ModeRules::of(CompatibilityMode::read(value)).shares_stretched_spaces_by_quirk()
        };
        assert!(shares(Some(15)) && shares(Some(16)));
        assert!(!shares(Some(14)) && !shares(Some(12)) && !shares(None));
    }

    #[test]
    fn limits_the_text_after_a_stop_past_the_line_end_from_mode_15() {
        let limits =
            |value| ModeRules::of(CompatibilityMode::read(value)).limits_text_past_the_line_end();
        assert!(limits(Some(15)) && limits(Some(16)));
        assert!(!limits(Some(14)) && !limits(Some(11)) && !limits(None));
    }

    #[test]
    fn moves_a_tab_to_a_left_stop_past_the_line_end_from_mode_15() {
        let moves =
            |value| ModeRules::of(CompatibilityMode::read(value)).moves_tab_to_left_stop_past_end();
        assert!(moves(Some(15)) && moves(Some(16)));
        assert!(!moves(Some(14)) && !moves(Some(11)) && !moves(None));
    }

    #[test]
    fn gives_mode_14_the_rules_of_word_2010_only() {
        let rules = ModeRules::of(CompatibilityMode::Mode14);
        assert!(rules.joins_ligatures() && rules.joins_space_runs());
        assert!(rules.breaks_words_when_word_wrap_off());
        assert!(rules.reads_numbering_indent_setting());
        assert!(!rules.aligns_table_edge() && !rules.drops_space_after_page_break());
        assert!(!ModeRules::default().joins_space_runs());
        assert!(!ModeRules::default().breaks_words_when_word_wrap_off());
        assert_eq!(
            ModeRules::default(),
            ModeRules::of(CompatibilityMode::read(None))
        );
    }

    #[test]
    fn kerns_by_the_kern_feature_from_mode_15() {
        let kerns =
            |value| ModeRules::of(CompatibilityMode::read(value)).kerns_by_the_kern_feature();
        assert!(kerns(Some(15)) && kerns(Some(16)));
        assert!(!kerns(Some(14)) && !kerns(Some(12)) && !kerns(None));
        // The OpenType features setting kerns by the `kern` feature in every mode.
        for value in [None, Some(12), Some(14), Some(15)] {
            let rules = ModeRules::of(CompatibilityMode::read(value));
            assert!(
                rules
                    .with_open_type_features(true)
                    .kerns_by_the_kern_feature()
            );
        }
    }

    #[test]
    fn scales_object_lines_by_object_runs_from_mode_15() {
        let scales = |value| {
            ModeRules::of(CompatibilityMode::read(value)).scales_object_lines_by_object_runs()
        };
        assert!(scales(Some(15)) && scales(Some(16)));
        assert!(!scales(Some(14)) && !scales(Some(12)) && !scales(None));
    }

    #[test]
    fn takes_a_column_edge_by_its_parts_before_mode_15() {
        let parts =
            |value| ModeRules::of(CompatibilityMode::read(value)).takes_column_edge_by_parts();
        assert!(parts(None) && parts(Some(11)) && parts(Some(12)) && parts(Some(14)));
        assert!(!parts(Some(15)) && !parts(Some(16)));
    }

    #[test]
    fn steps_kerned_runs_at_the_nearest_dot_from_mode_15() {
        let steps = |value| {
            ModeRules::of(CompatibilityMode::read(value)).steps_kerned_runs_at_nearest_dot()
        };
        assert!(steps(Some(15)) && steps(Some(16)));
        assert!(!steps(Some(14)) && !steps(Some(12)) && !steps(None));
        // With the OpenType features setting, in every mode, for the rule that asks it.
        for value in [None, Some(12), Some(14)] {
            let rules = ModeRules::of(CompatibilityMode::read(value));
            assert!(
                !rules
                    .with_open_type_features(true)
                    .steps_kerned_runs_at_nearest_dot()
            );
            let with_setting = rules.with_open_type_features(true);
            assert!(with_setting.steps_kerned_runs_at_nearest_dot_with_open_type_features());
            assert!(!rules.steps_kerned_runs_at_nearest_dot_with_open_type_features());
        }
    }

    #[test]
    fn gives_a_nested_table_the_edge_rules_in_every_mode() {
        let rules = ModeRules::default();
        assert_eq!(rules.for_table(false), rules);
        let nested = rules.for_table(true);
        assert!(nested.aligns_table_edge());
        assert!(nested.starts_wide_right_to_left_table_at_left());
        assert!(!rules.starts_wide_right_to_left_table_at_left());
        assert!(!nested.drops_space_after_page_break());
    }
}