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
//! 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::{EastAsianLanguage, 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,
/// Whether `w:kinsoku` is enabled so per-run East Asian languages enforce their `no_start` and
/// `no_end` sets.
restricted: bool,
}
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,
restricted: false,
};
/// 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,
restricted: true,
},
EastAsianScript::Traditional => Self {
no_start: "!),.:;?]}¢·–—’”•‥…‧′╴、。〉》」』】〕〞︰︱︳︴︶︸︺︼︾﹀﹂﹄﹏﹐﹑﹒﹔﹕﹖﹗﹚﹜﹞!),.:;?]|}、",
no_end: "([{£¥‘“‵〈《「『【〔〝︵︷︹︻︽︿﹁﹃﹙﹛﹝({",
hanging: "!),.:;?]}’”′、。〉》」』】〕〞﹚﹜﹞!),.:;?]}",
char_break: CharBreak::CjkNonHangul,
restricted: true,
},
EastAsianScript::Japanese => Self {
no_start: "!%),.:;?]}¢°’”‰′″℃、。々〉》」』】〕゛゜ゝゞ・ヽヾ!%),.:;?]}。」、・゙゚¢",
no_end: "$([\\{£¥‘“〈《「『【〔$([{「£¥",
hanging: ",.’”、。」』】),.]}。、",
char_break: CharBreak::CjkNonHangul,
restricted: true,
},
EastAsianScript::Korean => Self {
no_start: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
no_end: "$([\\{£¥‘“〈《「『【〔$([{£¥₩",
hanging: "!%),.:;?]}¢°’”′″℃〉》」』】〕!%),.:;?]}¢",
char_break: CharBreak::CjkNonHangul,
restricted: true,
},
}
}
/// 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 hanging punctuation set of `source` (used when `w:kinsoku` is off
/// while `w:overflowPunct` stays on).
#[must_use]
pub const fn with_hanging_of(self, source: Self) -> Self {
Self {
hanging: source.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 per-run East Asian languages enforce their `no_start`/`no_end` sets (`w:kinsoku`).
#[must_use]
pub const fn with_restricted(self, restricted: bool) -> Self {
Self { restricted, ..self }
}
/// Resolves `no_end` for the preceding glyph and `no_start` for the following glyph using each
/// glyph's own run `w:lang w:eastAsia` setting when `w:kinsoku` is enabled.
#[must_use]
pub const fn for_pair(
self,
before: Option<EastAsianLanguage>,
after: Option<EastAsianLanguage>,
) -> Self {
if !self.restricted {
return self;
}
let no_end = match before {
Some(EastAsianLanguage::Script(script)) => Self::of(script).no_end,
Some(EastAsianLanguage::NonEastAsian) => "",
None => self.no_end,
};
let no_start = match after {
Some(EastAsianLanguage::Script(script)) => Self::of(script).no_start,
Some(EastAsianLanguage::NonEastAsian) => "",
None => self.no_start,
};
Self {
no_start,
no_end,
..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(','));
}
#[test]
fn for_pair_respects_per_run_east_asian_language() {
use crate::props::EastAsianLanguage;
let cn = Kinsoku::of(EastAsianScript::Simplified);
let non_ea = Some(EastAsianLanguage::NonEastAsian);
let ea_cn = Some(EastAsianLanguage::Script(EastAsianScript::Simplified));
// Explicit non-East-Asian run language clears kinsoku no_start on the mark.
assert!(breaks_between('癸', '、', Some(cn.for_pair(ea_cn, non_ea))));
// Paragraph without EA script still applies a run's EA no_start when w:kinsoku is on.
let free_restricted = Kinsoku::FREE.with_restricted(true);
assert!(!breaks_between(
'癸',
'、',
Some(free_restricted.for_pair(ea_cn, ea_cn))
));
// With w:kinsoku off (restricted == false), a run's EA script does not re-enable no_start.
assert!(breaks_between(
'癸',
'、',
Some(Kinsoku::FREE.for_pair(ea_cn, ea_cn))
));
}
}