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
//! 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, is_hangul};
use crate::props::EastAsianScript;
/// Which adjacent non-space characters may break a line without a space between them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CharBreak {
/// Where either side is a non-Hangul full-width CJK character (ideograph, kana, or CJK
/// punctuation). Hangul wraps as words.
CjkNonHangul,
/// Where either side is any full-width East Asian character, including Hangul (Korean with
/// `w:wordWrap` off).
FullWidth,
/// Between any two characters, including Latin and Hangul (non-Korean with `w:wordWrap` off).
Any,
}
/// 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,
char_break: CharBreak,
}
impl Kinsoku {
/// No restriction: a paragraph that turns kinsoku off (`w:kinsoku w:val="0"`) may break
/// between any two characters that its character-break mode allows.
/// No mark makes a word go to the next line, so none hangs.
pub const FREE: Self = Self {
no_start: "",
no_end: "",
hanging: "",
char_break: CharBreak::CjkNonHangul,
};
/// 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: "!%),.:;>?]}¢°·ˇ’”‰′″℃∶、。〃〉》」』】〕〗〞﹚﹜﹞!"%'),.:;?]}¢",
char_break: CharBreak::CjkNonHangul,
},
EastAsianScript::Traditional => Self {
no_start: "!),.:;?]}¢·–—’”•‥…‧′╴、。〉》」』】〕〞︰︱︳︴︶︸︺︼︾﹀﹂﹄﹏﹐﹑﹒﹔﹕﹖﹗﹚﹜﹞!),.:;?]|}、",
no_end: "([{£¥‘“‵〈《「『【〔〝︵︷︹︻︽︿﹁﹃﹙﹛﹝({",
hanging: "!),.:;?]}’”′、。〉》」』】〕〞﹚﹜﹞!),.:;?]}",
char_break: CharBreak::CjkNonHangul,
},
EastAsianScript::Japanese => Self {
no_start: "!%),.:;?]}¢°’”‰′″℃、。々〉》」』】〕゛゜ゝゞ・ヽヾ!%),.:;?]}。」、・゙゚¢",
no_end: "$([\\{£¥‘“〈《「『【〔$([{「£¥",
hanging: ",.’”、。」』】),.]}。、",
char_break: CharBreak::CjkNonHangul,
},
EastAsianScript::Korean => Self {
no_start: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
no_end: "$([\\{£¥‘“〈《「『【〔$([{£¥₩",
hanging: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
char_break: CharBreak::CjkNonHangul,
},
}
}
/// 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
}
}
/// The same sets with the character-break mode `char_break`.
#[must_use]
pub const fn with_char_break(self, char_break: CharBreak) -> Self {
Self { char_break, ..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 `ch` is a full-width East Asian character other than Hangul.
fn is_cjk_non_hangul(ch: char) -> bool {
CharacterClass::FullWidthCell.contains(ch) && !is_hangul(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 allowed = match kinsoku.char_break {
CharBreak::CjkNonHangul => is_cjk_non_hangul(before) || is_cjk_non_hangul(after),
CharBreak::FullWidth => {
CharacterClass::FullWidthCell.contains(before)
|| CharacterClass::FullWidthCell.contains(after)
}
CharBreak::Any => true,
};
allowed && !kinsoku.no_end.contains(before) && !kinsoku.no_start.contains(after)
}
#[cfg(test)]
mod tests {
use super::{CharBreak, Kinsoku, breaks_between};
use crate::props::EastAsianScript;
#[test]
fn breaks_between_east_asian_characters_but_not_inside_latin_or_hangul_words() {
let japanese = Some(Kinsoku::of(EastAsianScript::Japanese));
for (before, after, expected) in [
('日', '本', true),
('本', 'W', true),
('d', '日', true),
('W', 'o', false),
('가', '나', false),
('ㄱ', 'ㄴ', false),
('가', 'A', false),
('가', '日', true),
] {
assert_eq!(breaks_between(before, after, japanese), expected);
}
}
#[test]
fn word_wrap_off_breaks_full_width_in_korean_and_any_char_otherwise() {
let korean_off =
Some(Kinsoku::of(EastAsianScript::Korean).with_char_break(CharBreak::FullWidth));
let zh_off = Some(Kinsoku::of(EastAsianScript::Simplified).with_char_break(CharBreak::Any));
let free_off = Some(Kinsoku::FREE.with_char_break(CharBreak::Any));
for (before, after, kinsoku, expected) in [
('가', '나', korean_off, true),
('日', '本', korean_off, true),
('타', '.', korean_off, false),
('a', 'b', korean_off, false),
('a', 'b', zh_off, true),
('가', '나', zh_off, true),
('o', '.', zh_off, false),
('타', '.', zh_off, false),
('o', '.', free_off, true),
('타', '.', free_off, true),
] {
assert_eq!(breaks_between(before, after, kinsoku), expected);
}
}
#[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(','));
}
}