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
//! East Asian line breaking (kinsoku): a line may break between two characters when one of
//! them is full-width, except before a character that cannot start a line or after one that
//! cannot end a line. The sets of those characters are the defaults of each language in
//! ECMA-376 Part 1, 17.3.1.16. Each language also has the marks that Word lets stand past the
//! end of a line where they do not fit (hanging punctuation, `w:overflowPunct`), which F-Office
//! does not do; the layout only warns where Word would hang one.
use crate::character_class::CharacterClass;
use crate::props::EastAsianScript;
/// The characters of one language that cannot start a line and those that cannot end one,
/// and the marks that Word lets stand past the end of a line.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Kinsoku {
no_start: &'static str,
no_end: &'static str,
/// The marks that Word lets stand past the end of a line where they do not fit, with
/// `w:overflowPunct` on: the lists of each language in [MS-OI29500] 2.1.52, each a part of
/// `no_start`, so that F-Office takes such a mark to the next line with the character
/// before it.
hanging: &'static str,
}
impl Kinsoku {
/// No restriction: a paragraph that turns kinsoku off (`w:kinsoku w:val="0"`) may break
/// between any two characters when one of them is full-width.
/// No mark makes a word go to the next line, so none hangs.
pub const FREE: Self = Self {
no_start: "",
no_end: "",
hanging: "",
};
/// The default sets of a script's language.
#[must_use]
pub const fn of(script: EastAsianScript) -> Self {
match script {
EastAsianScript::Simplified => Self {
no_start: "!%),.:;>?]}¢¨°·ˇˉ―‖’”…‰′″›℃∶、。〃〉》」』】〕〗〞︶︺︾﹀﹄﹚﹜﹞!"%'),.:;?]`|}~¢",
no_end: "$([{£¥·‘“〈《「『【〔〖〝﹙﹛﹝$(.[{£¥",
hanging: "!%),.:;>?]}¢°·ˇ’”‰′″℃∶、。〃〉》」』】〕〗〞﹚﹜﹞!"%'),.:;?]}¢",
},
EastAsianScript::Traditional => Self {
no_start: "!),.:;?]}¢·–—’”•‥…‧′╴、。〉》」』】〕〞︰︱︳︴︶︸︺︼︾﹀﹂﹄﹏﹐﹑﹒﹔﹕﹖﹗﹚﹜﹞!),.:;?]|}、",
no_end: "([{£¥‘“‵〈《「『【〔〝︵︷︹︻︽︿﹁﹃﹙﹛﹝({",
hanging: "!),.:;?]}’”′、。〉》」』】〕〞﹚﹜﹞!),.:;?]}",
},
EastAsianScript::Japanese => Self {
no_start: "!%),.:;?]}¢°’”‰′″℃、。々〉》」』】〕゛゜ゝゞ・ヽヾ!%),.:;?]}。」、・゙゚¢",
no_end: "$([\\{£¥‘“〈《「『【〔$([{「£¥",
hanging: ",.’”、。」』】),.]}。、",
},
EastAsianScript::Korean => Self {
no_start: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
no_end: "$([\\{£¥‘“〈《「『【〔$([{£¥₩",
hanging: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
},
}
}
/// The same sets with no mark that hangs: a paragraph that turns `w:overflowPunct` off.
#[must_use]
pub const fn without_hanging(self) -> Self {
Self {
hanging: "",
..self
}
}
/// Whether Word lets `ch` stand past the end of a line where it does not fit.
#[must_use]
pub fn hangs(self, ch: char) -> bool {
self.hanging.contains(ch)
}
}
/// Whether a line may break between two characters that no space separates. Text without an
/// East Asian language (`kinsoku` is `None`) breaks only where Latin text does.
#[must_use]
pub fn breaks_between(before: char, after: char, kinsoku: Option<Kinsoku>) -> bool {
let Some(kinsoku) = kinsoku else {
return false;
};
let full_width = |ch| CharacterClass::FullWidthCell.contains(ch);
(full_width(before) || full_width(after))
&& !kinsoku.no_end.contains(before)
&& !kinsoku.no_start.contains(after)
}
#[cfg(test)]
mod tests {
use super::{Kinsoku, breaks_between};
use crate::props::EastAsianScript;
#[test]
fn breaks_between_east_asian_characters_but_not_inside_latin_words() {
let japanese = Some(Kinsoku::of(EastAsianScript::Japanese));
assert!(breaks_between('日', '本', japanese));
assert!(breaks_between('本', 'W', japanese));
assert!(breaks_between('d', '日', japanese));
assert!(!breaks_between('W', 'o', japanese));
}
#[test]
fn keeps_closing_marks_off_the_start_and_opening_marks_off_the_end() {
let japanese = Some(Kinsoku::of(EastAsianScript::Japanese));
assert!(!breaks_between('書', '」', japanese));
assert!(!breaks_between('本', '、', japanese));
assert!(!breaks_between('「', '日', japanese));
// Japanese keeps small kana free to start a line unless strict rules apply.
assert!(breaks_between('ン', 'ッ', japanese));
}
#[test]
fn follows_the_language_and_the_kinsoku_switch() {
// With kinsoku off, any break between full-width characters is allowed, and text
// without an East Asian language breaks only where Latin text does.
assert!(breaks_between('書', '」', Some(Kinsoku::FREE)));
assert!(!breaks_between('日', '本', None));
// The sets differ by language: Simplified Chinese keeps "〃" off the start, Korean
// does not.
let simplified = Some(Kinsoku::of(EastAsianScript::Simplified));
let korean = Some(Kinsoku::of(EastAsianScript::Korean));
assert!(!breaks_between('字', '〃', simplified));
assert!(breaks_between('字', '〃', korean));
}
#[test]
fn hangs_only_marks_that_cannot_start_a_line() {
// [MS-OI29500] 2.1.52: the marks that hang are marks that cannot start a line, so a
// word that ends with one goes to the next line whole where Word hangs the mark.
for script in [
EastAsianScript::Simplified,
EastAsianScript::Traditional,
EastAsianScript::Japanese,
EastAsianScript::Korean,
] {
let sets = Kinsoku::of(script);
assert!(
sets.hanging.chars().all(|ch| sets.no_start.contains(ch)),
"{script:?}"
);
assert!(sets.hangs(',') && !sets.hangs('字'), "{script:?}");
assert!(!sets.without_hanging().hangs(','), "{script:?}");
}
// The lists differ by language: Korean hangs "!", Japanese does not.
assert!(Kinsoku::of(EastAsianScript::Korean).hangs('!'));
assert!(!Kinsoku::of(EastAsianScript::Japanese).hangs('!'));
assert!(!Kinsoku::FREE.hangs(','));
}
}