1 # Copyright (C) 2016 and later: Unicode, Inc. and others.
2 # License & terms of use: http://www.unicode.org/copyright.html
3 # Copyright (c) 2002-2016 International Business Machines Corporation and
4 # others. All Rights Reserved.
9 # Implement default line breaking as defined by
10 # Unicode Standard Annex #14 Revision 44 for Unicode 13.0
11 # http://www.unicode.org/reports/tr14/, with the following modification:
13 # Boundaries between hyphens and following letters are suppressed when
14 # there is a boundary preceding the hyphen. See rule 20.9
16 # This corresponds to CSS line-break=strict (BCP47 -u-lb-strict).
17 # It sets characters of class CJ to behave like NS.
18 # It allows breaking before 201C and after 201D, for zh_Hans, zh_Hant, and ja.
21 # Character Classes defined by TR 14.
25 !!quoted_literals_only;
27 $AI = [:LineBreak = Ambiguous:];
28 $AL = [:LineBreak = Alphabetic:];
29 $BA = [:LineBreak = Break_After:];
30 $HH = [\u2010]; # \u2010 is HYPHEN, default line break is BA.
31 $BB = [:LineBreak = Break_Before:];
32 $BK = [:LineBreak = Mandatory_Break:];
33 $B2 = [:LineBreak = Break_Both:];
34 $CB = [:LineBreak = Contingent_Break:];
35 $CJ = [:LineBreak = Conditional_Japanese_Starter:];
36 $CL = [[:LineBreak = Close_Punctuation:] \u201d];
37 # $CM = [:LineBreak = Combining_Mark:];
38 $CP = [:LineBreak = Close_Parenthesis:];
39 $CR = [:LineBreak = Carriage_Return:];
40 $EB = [:LineBreak = EB:];
41 $EM = [:LineBreak = EM:];
42 $EX = [:LineBreak = Exclamation:];
43 $GL = [:LineBreak = Glue:];
44 $HL = [:LineBreak = Hebrew_Letter:];
45 $HY = [:LineBreak = Hyphen:];
46 $H2 = [:LineBreak = H2:];
47 $H3 = [:LineBreak = H3:];
48 $ID = [:LineBreak = Ideographic:];
49 $IN = [:LineBreak = Inseperable:];
50 $IS = [:LineBreak = Infix_Numeric:];
51 $JL = [:LineBreak = JL:];
52 $JV = [:LineBreak = JV:];
53 $JT = [:LineBreak = JT:];
54 $LF = [:LineBreak = Line_Feed:];
55 $NL = [:LineBreak = Next_Line:];
56 # NS includes CJ for CSS strict line breaking.
57 $NS = [[:LineBreak = Nonstarter:] $CJ];
58 $NU = [:LineBreak = Numeric:];
59 $OP = [[:LineBreak = Open_Punctuation:] \u201c];
60 $PO = [:LineBreak = Postfix_Numeric:];
61 $PR = [:LineBreak = Prefix_Numeric:];
62 $QU = [[:LineBreak = Quotation:] - [\u201c\u201d]];
63 $RI = [:LineBreak = Regional_Indicator:];
64 $SA = [:LineBreak = Complex_Context:];
65 $SG = [:LineBreak = Surrogate:];
66 $SP = [:LineBreak = Space:];
67 $SY = [:LineBreak = Break_Symbols:];
68 $WJ = [:LineBreak = Word_Joiner:];
69 $XX = [:LineBreak = Unknown:];
70 $ZW = [:LineBreak = ZWSpace:];
71 $ZWJ = [:LineBreak = ZWJ:];
73 # OP30 and CP30 are variants of OP and CP that appear in-line in rule LB30 from UAX 14,
74 # without a formal name. Because ICU rules require multiple uses of the expressions,
75 # give them a single definition with a name
77 $OP30 = [$OP - [\p{ea=F}\p{ea=W}\p{ea=H}]];
78 $CP30 = [$CP - [\p{ea=F}\p{ea=W}\p{ea=H}]];
80 # By LB9, a ZWJ also behaves as a CM. Including it in the definition of CM avoids having to explicitly
81 # list it in the numerous rules that use CM.
82 # By LB1, SA characters with general categor of Mn or Mc also resolve to CM.
84 $CM = [[:LineBreak = Combining_Mark:] $ZWJ [$SA & [[:Mn:][:Mc:]]]];
85 $CMX = [[$CM] - [$ZWJ]];
87 # Dictionary character set, for triggering language-based break engines. Currently
88 # limited to LineBreak=Complex_Context (SA).
93 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
94 # SA (Dictionary chars, excluding Mn and Mc)
95 # SG (Unpaired Surrogates)
96 # XX (Unknown, unassigned)
99 $ALPlus = [$AL $AI $SG $XX [$SA-[[:Mn:][:Mc:]]]];
102 ## -------------------------------------------------
105 # CAN_CM is the set of characters that may combine with CM combining chars.
106 # Note that Linebreak UAX 14's concept of a combining char and the rules
107 # for what they can combine with are _very_ different from the rest of Unicode.
109 # Note that $CM itself is left out of this set. If CM is needed as a base
110 # it must be listed separately in the rule.
112 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
113 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
116 # AL_FOLLOW set of chars that can unconditionally follow an AL
117 # Needed in rules where stand-alone $CM s are treated as AL.
119 $AL_FOLLOW = [$BK $CR $LF $NL $ZW $SP $CL $CP $EX $HL $IS $SY $WJ $GL $OP30 $QU $BA $HY $NS $IN $NU $PR $PO $ALPlus];
123 # Rule LB 4, 5 Mandatory (Hard) breaks.
125 $LB4Breaks = [$BK $CR $LF $NL];
126 $LB4NonBreaks = [^$BK $CR $LF $NL $CM];
130 # LB 6 Do not break before hard line breaks.
132 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
133 $CAN_CM $CM* $LB4Breaks {100};
134 ^$CM+ $LB4Breaks {100};
138 $LB4NonBreaks [$SP $ZW];
139 $CAN_CM $CM* [$SP $ZW];
143 # LB 8 Break after zero width space
146 $LB8Breaks = [$LB4Breaks $ZW];
147 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
148 $ZW $SP* / [^$SP $ZW $LB4Breaks];
150 # LB 8a ZWJ x Do not break Emoji ZWJ sequences.
154 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
155 # $CM not covered by the above needs to behave like $AL
156 # See definition of $CAN_CM.
158 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
162 # LB 11 Do not break before or after WORD JOINER & related characters.
171 # LB 12 Do not break after NBSP and related characters.
177 # LB 12a Do not break before NBSP and related characters ...
180 [[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GL;
186 # LB 13 Don't break before ']' or '!' or or '/', even after spaces.
190 ^$CM+ $CL; # by rule 10, stand-alone CM behaves as AL
194 ^$CM+ $CP; # by rule 10, stand-alone CM behaves as AL
198 ^$CM+ $EX; # by rule 10, stand-alone CM behaves as AL
202 ^$CM+ $SY; # by rule 10, stand-alone CM behaves as AL
206 # LB 14 Do not break after OP, even after spaces
207 # Note subtle interaction with "SP IS /" rules in LB14a.
208 # This rule consumes the SP, chaining happens on the IS, effectivley overriding the SP IS rules,
209 # which is the desired behavior.
213 $OP $CM* $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
214 # by rule 8, CM following a SP is stand-alone.
217 # LB 14a Force a break before start of a number with a leading decimal pt, e.g. " .23"
218 # Note: would be simpler to express as "$SP / $IS $CM* $NU;", but ICU rules have limitations.
219 # See issue ICU-20303
222 $CanFollowIS = [$BK $CR $LF $NL $SP $ZW $WJ $GL $CL $CP $EX $IS $SY $QU $BA $HY $NS $ALPlus $HL $IN];
223 $SP $IS / [^ $CanFollowIS $NU $CM];
224 $SP $IS $CM* $CMX / [^ $CanFollowIS $NU $CM];
227 # LB 14b Do not break before numeric separators (IS), even after spaces.
229 [$LB8NonBreaks - $SP] $IS;
230 $SP $IS $CM* [$CanFollowIS {eof}];
231 $SP $IS $CM* $ZWJ [^$CM $NU];
234 ^$CM+ $IS; # by rule 10, stand-alone CM behaves as AL
241 ($CL | $CP) $CM* $SP* $NS;
247 # LB 18 Break after spaces.
249 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
250 $LB18Breaks = [$LB8Breaks $SP];
255 $LB18NonBreaks $CM* $QU;
265 $LB20NonBreaks = [$LB18NonBreaks - $CB];
267 # LB 20.09 Don't break between Hyphens and Letters when there is a break preceding the hyphen.
268 # Originally added as a Finnish tailoring, now promoted to default ICU behavior.
269 # Note: this is not default UAX-14 behaviour. See issue ICU-8151.
271 ^($HY | $HH) $CM* $ALPlus;
273 # LB 21 x (BA | HY | NS)
276 $LB20NonBreaks $CM* ($BA | $HY | $NS);
279 ^$CM+ ($BA | $HY | $NS);
281 $BB $CM* [^$CB]; # $BB x
282 $BB $CM* $LB20NonBreaks;
284 # LB 21a Don't break after Hebrew + Hyphen
287 $HL $CM* ($HY | $BA) $CM* [^$CB]?;
289 # LB 21b (forward) Don't break between SY and HL
290 # (break between HL and SY already disallowed by LB 13 above)
293 # LB 22 Do not break before ellipses
295 $LB20NonBreaks $CM* $IN;
301 ($ALPlus | $HL) $CM* $NU;
302 ^$CM+ $NU; # Rule 10, any otherwise unattached CM behaves as AL
303 $NU $CM* ($ALPlus | $HL);
307 $PR $CM* ($ID | $EB | $EM);
308 ($ID | $EB | $EM) $CM* $PO;
314 ($PR | $PO) $CM* ($ALPlus | $HL);
315 ($ALPlus | $HL) $CM* ($PR | $PO);
316 ^$CM+ ($PR | $PO); # Rule 10, any otherwise unattached CM behaves as AL
321 (($PR | $PO) $CM*)? (($OP | $HY) $CM*)? ($IS $CM*)? $NU ($CM* ($NU | $SY | $IS))*
322 ($CM* ($CL | $CP))? ($CM* ($PR | $PO))?;
324 # LB 26 Do not break a Korean syllable
326 $JL $CM* ($JL | $JV | $H2 | $H3);
327 ($JV | $H2) $CM* ($JV | $JT);
328 ($JT | $H3) $CM* $JT;
330 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
331 ($JL | $JV | $JT | $H2 | $H3) $CM* $IN;
332 ($JL | $JV | $JT | $H2 | $H3) $CM* $PO;
333 $PR $CM* ($JL | $JV | $JT | $H2 | $H3);
336 # LB 28 Do not break between alphabetics
338 ($ALPlus | $HL) $CM* ($ALPlus | $HL);
339 ^$CM+ ($ALPlus | $HL); # The $CM+ is from rule 10, an unattached CM is treated as AL
342 $IS $CM* ($ALPlus | $HL);
345 ($ALPlus | $HL | $NU) $CM* $OP30;
346 ^$CM+ $OP30; # The $CM+ is from rule 10, an unattached CM is treated as AL.
347 $CP30 $CM* ($ALPlus | $HL | $NU);
349 # LB 30a Do not break between regional indicators. Break after pairs of them.
350 # Tricky interaction with LB8a: ZWJ x . together with ZWJ acting like a CM.
351 $RI $CM* $RI / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $IN $CM]];
352 $RI $CM* $RI $CM* [$CM-$ZWJ] / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $IN $CM]];
353 $RI $CM* $RI $CM* [$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $IN $ZWJ {eof}];
354 # note: the preceding rule includes {eof} rather than having the last [set] term qualified with '?'
355 # because of the chain-out behavior difference. The rule must chain out only from the [set characters],
356 # not from the preceding $RI or $CM, which it would be able to do if the set were optional.
358 # LB 30b Do not break between an Emoji Base and an Emoji Modifier
361 # LB 31 Break everywhere else.
362 # Match a single code point if no other rule applies.