Home | History | Annotate | Download | only in rules
      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.
      5 #
      6 #  file:  line.txt
      7 #
      8 #         Line Breaking Rules
      9 #         Implement default line breaking as defined by
     10 #         Unicode Standard Annex #14 Revision 37 for Unicode 9.0
     11 #         http://www.unicode.org/reports/tr14/
     12 #
     13 #         Includes extensions to the handling of emoji ZWJ sequences from
     14 #         https://goo.gl/cluFCn
     15 #
     16 #         TODO:  Rule LB 8 remains as it was in Unicode 5.2
     17 #         This is only because of a limitation of ICU break engine implementation,
     18 #         not because the older behavior is desirable.
     19 #
     20 #         This corresponds to CSS line-break=strict (BCP47 -u-lb-strict).
     21 #         It sets characters of class CJ to behave like NS.
     22 
     23 #
     24 #  Character Classes defined by TR 14.
     25 #
     26 
     27 !!chain;
     28 !!quoted_literals_only;
     29 
     30 $AI = [:LineBreak =  Ambiguous:];
     31 $AL = [:LineBreak =  Alphabetic:];
     32 $BA = [:LineBreak =  Break_After:];
     33 $BB = [:LineBreak =  Break_Before:];
     34 $BK = [:LineBreak =  Mandatory_Break:];
     35 $B2 = [:LineBreak =  Break_Both:];
     36 $CB = [:LineBreak =  Contingent_Break:];
     37 $CJ = [:LineBreak =  Conditional_Japanese_Starter:];
     38 $CL = [:LineBreak =  Close_Punctuation:];
     39 # $CM = [:LineBreak =  Combining_Mark:];
     40 $CP = [:LineBreak =  Close_Parenthesis:];
     41 $CR = [:LineBreak =  Carriage_Return:];
     42 $EB = [:LineBreak =  EB:];
     43 $EM = [:LineBreak =  EM:];
     44 $EX = [:LineBreak =  Exclamation:];
     45 $GL = [:LineBreak =  Glue:];
     46 $HL = [:LineBreak =  Hebrew_Letter:];
     47 $HY = [:LineBreak =  Hyphen:];
     48 $H2 = [:LineBreak =  H2:];
     49 $H3 = [:LineBreak =  H3:];
     50 $ID = [:LineBreak =  Ideographic:];
     51 $IN = [:LineBreak =  Inseperable:];
     52 $IS = [:LineBreak =  Infix_Numeric:];
     53 $JL = [:LineBreak =  JL:];
     54 $JV = [:LineBreak =  JV:];
     55 $JT = [:LineBreak =  JT:];
     56 $LF = [:LineBreak =  Line_Feed:];
     57 $NL = [:LineBreak =  Next_Line:];
     58 # NS includes CJ for CSS strict line breaking.
     59 $NS = [[:LineBreak =  Nonstarter:] $CJ];
     60 $NU = [:LineBreak =  Numeric:];
     61 $OP = [:LineBreak =  Open_Punctuation:];
     62 $PO = [:LineBreak =  Postfix_Numeric:];
     63 $PR = [:LineBreak =  Prefix_Numeric:];
     64 $QU = [:LineBreak =  Quotation:];
     65 $RI = [:LineBreak =  Regional_Indicator:];
     66 $SA = [:LineBreak =  Complex_Context:];
     67 $SG = [:LineBreak =  Surrogate:];
     68 $SP = [:LineBreak =  Space:];
     69 $SY = [:LineBreak =  Break_Symbols:];
     70 $WJ = [:LineBreak =  Word_Joiner:];
     71 $XX = [:LineBreak =  Unknown:];
     72 $ZW = [:LineBreak =  ZWSpace:];
     73 $ZWJ = [:LineBreak = ZWJ:];
     74 
     75 $EmojiNRK = [[\p{Emoji}] - [$RI \u002a\u00230-9]];
     76 # Data for Extended Pictographic scraped from CLDR common/properties/ExtendedPictographic.txt, r13267
     77 $Extended_Pict = [\U0001F774-\U0001F77F\U00002700-\U00002701\U00002703-\U00002704\U0000270E\U00002710-\U00002711\U00002765-\U00002767\U0001F030-\U0001F093\U0001F094-\U0001F09F\U0001F10D-\U0001F10F\U0001F12F\U0001F16C-\U0001F16F\U0001F1AD-\U0001F1E5\U0001F260-\U0001F265\U0001F203-\U0001F20F\U0001F23C-\U0001F23F\U0001F249-\U0001F24F\U0001F252-\U0001F25F\U0001F266-\U0001F2FF\U0001F7D5-\U0001F7FF\U0001F000-\U0001F003\U0001F005-\U0001F02B\U0001F02C-\U0001F02F\U0001F322-\U0001F323\U0001F394-\U0001F395\U0001F398\U0001F39C-\U0001F39D\U0001F3F1-\U0001F3F2\U0001F3F6\U0001F4FE\U0001F53E-\U0001F548\U0001F54F\U0001F568-\U0001F56E\U0001F571-\U0001F572\U0001F57B-\U0001F586\U0001F588-\U0001F589\U0001F58E-\U0001F58F\U0001F591-\U0001F594\U0001F597-\U0001F5A3\U0001F5A6-\U0001F5A7\U0001F5A9-\U0001F5B0\U0001F5B3-\U0001F5BB\U0001F5BD-\U0001F5C1\U0001F5C5-\U0001F5D0\U0001F5D4-\U0001F5DB\U0001F5DF-\U0001F5E0\U0001F5E2\U0001F5E4-\U0001F5E7\U0001F5E9-\U0001F5EE\U0001F5F0-\U0001F5F2\U0001F5F4-\U0001F5F9\U00002605\U00002607-\U0000260D\U0000260F-\U00002610\U00002612\U00002616-\U00002617\U00002619-\U0000261C\U0000261E-\U0000261F\U00002621\U00002624-\U00002625\U00002627-\U00002629\U0000262B-\U0000262D\U00002630-\U00002637\U0000263B-\U00002647\U00002654-\U0000265F\U00002661-\U00002662\U00002664\U00002667\U00002669-\U0000267A\U0000267C-\U0000267E\U00002680-\U00002691\U00002695\U00002698\U0000269A\U0000269D-\U0000269F\U000026A2-\U000026A9\U000026AC-\U000026AF\U000026B2-\U000026BC\U000026BF-\U000026C3\U000026C6-\U000026C7\U000026C9-\U000026CD\U000026D0\U000026D2\U000026D5-\U000026E8\U000026EB-\U000026EF\U000026F6\U000026FB-\U000026FC\U000026FE-\U000026FF\U00002388\U0001FA00-\U0001FFFD\U0001F0A0-\U0001F0AE\U0001F0B1-\U0001F0BF\U0001F0C1-\U0001F0CF\U0001F0D1-\U0001F0F5\U0001F0AF-\U0001F0B0\U0001F0C0\U0001F0D0\U0001F0F6-\U0001F0FF\U0001F80C-\U0001F80F\U0001F848-\U0001F84F\U0001F85A-\U0001F85F\U0001F888-\U0001F88F\U0001F8AE-\U0001F8FF\U0001F900-\U0001F90B\U0001F91F\U0001F928-\U0001F92F\U0001F931-\U0001F932\U0001F94C\U0001F95F-\U0001F96B\U0001F992-\U0001F997\U0001F9D0-\U0001F9E6\U0001F90C-\U0001F90F\U0001F93F\U0001F94D-\U0001F94F\U0001F96C-\U0001F97F\U0001F998-\U0001F9BF\U0001F9C1-\U0001F9CF\U0001F9E7-\U0001F9FF\U0001F6C6-\U0001F6CA\U0001F6D3-\U0001F6D4\U0001F6E6-\U0001F6E8\U0001F6EA\U0001F6F1-\U0001F6F2\U0001F6F7-\U0001F6F8\U0001F6D5-\U0001F6DF\U0001F6ED-\U0001F6EF\U0001F6F9-\U0001F6FF];
     78 
     79 # By LB9, a ZWJ also behaves as a CM. Including it in the definition of CM avoids having to explicitly
     80 #         list it in the numerous rules that use CM.
     81 # By LB1, SA characters with general categor of Mn or Mc also resolve to CM.
     82 
     83 $CM = [[:LineBreak = Combining_Mark:] $ZWJ [$SA & [[:Mn:][:Mc:]]]];
     84 
     85 #   Dictionary character set, for triggering language-based break engines. Currently
     86 #   limited to LineBreak=Complex_Context (SA).
     87 
     88 $dictionary = [$SA];
     89 
     90 #
     91 #  Rule LB1.  By default, treat AI  (characters with ambiguous east Asian width),
     92 #                               SA  (Dictionary chars, excluding Mn and Mc)
     93 #                               SG  (Unpaired Surrogates)
     94 #                               XX  (Unknown, unassigned)
     95 #                         as $AL  (Alphabetic)
     96 #
     97 $ALPlus = [$AL $AI $SG $XX [$SA-[[:Mn:][:Mc:]]]];
     98 
     99 
    100 ## -------------------------------------------------
    101 
    102 !!forward;
    103 
    104 #
    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.
    108 #
    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.
    111 #
    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
    114 
    115 #
    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.
    118 #
    119 $AL_FOLLOW      = [$BK $CR $LF $NL $ZW $SP $CL $CP $EX $HL $IS $SY $WJ $GL $OP $QU $BA $HY $NS $IN $NU $PR $PO $ALPlus];
    120 
    121 
    122 #
    123 #  Rule LB 4, 5    Mandatory (Hard) breaks.
    124 #
    125 $LB4Breaks    = [$BK $CR $LF $NL];
    126 $LB4NonBreaks = [^$BK $CR $LF $NL $CM];
    127 $CR $LF {100};
    128 
    129 #
    130 #  LB 6    Do not break before hard line breaks.
    131 #
    132 $LB4NonBreaks?  $LB4Breaks {100};    # LB 5  do not break before hard breaks.
    133 $CAN_CM $CM*    $LB4Breaks {100};
    134 ^$CM+           $LB4Breaks {100};
    135 
    136 # LB 7         x SP
    137 #              x ZW
    138 $LB4NonBreaks [$SP $ZW];
    139 $CAN_CM $CM*  [$SP $ZW];
    140 ^$CM+         [$SP $ZW];
    141 
    142 #
    143 # LB 8         Break after zero width space
    144 #              TODO:  ZW SP* <break>
    145 #              An engine change is required to write the reverse rule for this.
    146 #              For now, leave the Unicode 5.2 rule, ZW <break>
    147 #
    148 $LB8Breaks    = [$LB4Breaks $ZW];
    149 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
    150 
    151 # LB 8a        ZWJ x (ID | Extended_Pict | EmojiNRK)   Emoji ZWJ sequences.
    152 #
    153 $ZWJ ($ID | $Extended_Pict | $EmojiNRK);
    154 
    155 # LB 9     Combining marks.      X   $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
    156 #                                $CM not covered by the above needs to behave like $AL
    157 #                                See definition of $CAN_CM.
    158 
    159 $CAN_CM $CM+;                   #  Stick together any combining sequences that don't match other rules.
    160 ^$CM+;
    161 
    162 #
    163 # LB 11  Do not break before or after WORD JOINER & related characters.
    164 #
    165 $CAN_CM $CM*  $WJ;
    166 $LB8NonBreaks $WJ;
    167 ^$CM+         $WJ;
    168 
    169 $WJ $CM* .;
    170 
    171 #
    172 # LB 12  Do not break after NBSP and related characters.
    173 #         GL  x
    174 #
    175 $GL $CM* .;
    176 
    177 #
    178 # LB 12a  Do not break before NBSP and related characters ...
    179 #            [^SP BA HY] x GL
    180 #
    181 [[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GL;
    182 ^$CM+ $GL;
    183 
    184 
    185 
    186 #
    187 # LB 13   Don't break before ']' or '!' or ';' or '/', even after spaces.
    188 #
    189 $LB8NonBreaks $CL;
    190 $CAN_CM $CM*  $CL;
    191 ^$CM+         $CL;              # by rule 10, stand-alone CM behaves as AL
    192 
    193 $LB8NonBreaks $CP;
    194 $CAN_CM $CM*  $CP;
    195 ^$CM+         $CP;              # by rule 10, stand-alone CM behaves as AL
    196 
    197 $LB8NonBreaks $EX;
    198 $CAN_CM $CM*  $EX;
    199 ^$CM+         $EX;              # by rule 10, stand-alone CM behaves as AL
    200 
    201 $LB8NonBreaks $IS;
    202 $CAN_CM $CM*  $IS;
    203 ^$CM+         $IS;              # by rule 10, stand-alone CM behaves as AL
    204 
    205 $LB8NonBreaks $SY;
    206 $CAN_CM $CM*  $SY;
    207 ^$CM+         $SY;              # by rule 10, stand-alone CM behaves as AL
    208 
    209 
    210 #
    211 # LB 14  Do not break after OP, even after spaces
    212 #
    213 $OP $CM* $SP* .;
    214 
    215 $OP $CM* $SP+ $CM+ $AL_FOLLOW?;    # by rule 10, stand-alone CM behaves as AL
    216                                    # by rule 8, CM following a SP is stand-alone.
    217 
    218 # LB 15
    219 $QU $CM* $SP* $OP;
    220 
    221 # LB 16
    222 ($CL | $CP) $CM* $SP* $NS;
    223 
    224 # LB 17
    225 $B2 $CM* $SP* $B2;
    226 
    227 #
    228 # LB 18  Break after spaces.
    229 #
    230 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
    231 $LB18Breaks    = [$LB8Breaks $SP];
    232 
    233 
    234 # LB 19
    235 #         x QU
    236 $LB18NonBreaks $CM* $QU;
    237 ^$CM+               $QU;
    238 
    239 #         QU  x
    240 $QU $CM* .;
    241 
    242 
    243 # LB 20
    244 #        <break>  $CB
    245 #        $CB   <break>
    246 
    247 $LB20NonBreaks = [$LB18NonBreaks - $CB];
    248 
    249 # LB 21        x   (BA | HY | NS)
    250 #           BB x
    251 #
    252 $LB20NonBreaks $CM* ($BA | $HY | $NS);
    253 ^$CM+ ($BA | $HY | $NS);
    254 
    255 $BB $CM* [^$CB];                                  #  $BB  x
    256 $BB $CM* $LB20NonBreaks;
    257 
    258 # LB 21a Don't break after Hebrew + Hyphen
    259 #   HL (HY | BA) x
    260 #
    261 $HL $CM* ($HY | $BA) $CM* [^$CB]?;
    262 
    263 # LB 21b (forward) Don't break between SY and HL
    264 # (break between HL and SY already disallowed by LB 13 above)
    265 $SY $CM* $HL;
    266 
    267 # LB 22
    268 ($ALPlus | $HL) $CM* $IN;
    269 ^$CM+    $IN;     #  by rule 10, any otherwise unattached CM behaves as AL
    270 $EX $CM*    $IN;
    271 ($ID | $EB | $EM) $CM*  $IN;
    272 $IN $CM*    $IN;
    273 $NU $CM*    $IN;
    274 
    275 
    276 # $LB 23
    277 #
    278 ($ALPlus | $HL) $CM* $NU;
    279 ^$CM+  $NU;       # Rule 10, any otherwise unattached CM behaves as AL
    280 $NU $CM* ($ALPlus | $HL);
    281 
    282 # LB 23a
    283 #
    284 $PR $CM* ($ID | $EB | $EM);
    285 ($ID | $EB | $EM) $CM*  $PO;
    286 
    287 
    288 #
    289 # LB 24
    290 #
    291 ($PR | $PO) $CM* ($ALPlus | $HL);
    292 ($ALPlus | $HL) $CM* ($PR | $PO);
    293 ^$CM+ ($PR | $PO);       # Rule 10, any otherwise unattached CM behaves as AL
    294 
    295 #
    296 # LB 25   Numbers.
    297 #
    298 (($PR | $PO) $CM*)? (($OP | $HY) $CM*)? $NU ($CM* ($NU | $SY | $IS))*
    299     ($CM* ($CL | $CP))? ($CM* ($PR | $PO))?;
    300 
    301 # LB 26  Do not break a Korean syllable
    302 #
    303 $JL $CM* ($JL | $JV | $H2 | $H3);
    304 ($JV | $H2) $CM* ($JV | $JT);
    305 ($JT | $H3) $CM* $JT;
    306 
    307 # LB 27  Treat korean Syllable Block the same as ID  (don't break it)
    308 ($JL | $JV | $JT | $H2 | $H3) $CM* $IN;
    309 ($JL | $JV | $JT | $H2 | $H3) $CM* $PO;
    310 $PR $CM* ($JL | $JV | $JT | $H2 | $H3);
    311 
    312 
    313 # LB 28   Do not break between alphabetics
    314 #
    315 ($ALPlus | $HL) $CM* ($ALPlus | $HL);
    316 ^$CM+ ($ALPlus | $HL);      # The $CM+ is from rule 10, an unattached CM is treated as AL
    317 
    318 # LB 29
    319 $IS $CM* ($ALPlus | $HL);
    320 
    321 # LB 30
    322 ($ALPlus | $HL | $NU) $CM* $OP;
    323 ^$CM+ $OP;         # The $CM+ is from rule 10, an unattached CM is treated as AL.
    324 $CP $CM* ($ALPlus | $HL | $NU);
    325 
    326 # LB 30a  Do not break between regional indicators. Break after pairs of them.
    327 #         Tricky interaction with LB8a: ZWJ x ID
    328 $RI $CM* $RI                / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM] {eof}];
    329 $RI $CM* $RI $CM*  $ZWJ     / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM $ID $Extended_Pict $EmojiNRK] {eof}];
    330 $RI $CM* $RI $CM* [$CM-$ZWJ] / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM] {eof}];
    331 
    332 $RI $CM* $RI $CM* [$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS {eof}];
    333 $RI $CM* $RI $CM* $ZWJ ($ID | $Extended_Pict | $EmojiNRK);
    334 
    335 # LB 30b Do not break between an Emoji Base and an Emoji Modifier
    336 $EB $CM* $EM;
    337 
    338 # LB 31 Break everywhere else.
    339 #       Match a single code point if no other rule applies.
    340 .;
    341 
    342 ## -------------------------------------------------
    343 
    344 !!safe_reverse;
    345 
    346 # LB 9
    347 ^$CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
    348 
    349 # LB 14
    350 $SP+ $CM* $OP;
    351 
    352 # LB 15
    353 $SP+ $CM* $QU;
    354 
    355 # LB 16
    356 $SP+ $CM* ($CL | $CP);
    357 
    358 # LB 17
    359 $SP+ $CM* $B2;
    360 
    361 # LB 21
    362 $CM* ($HY | $BA) $CM* $HL;
    363 
    364 # LB 25
    365 ($CM* ($IS | $SY))+ $CM* $NU;
    366 ($CL | $CP) $CM* ($NU | $IS | $SY);
    367 
    368 #  LB 30
    369 ($CM* $RI)+;
    370 
    371 # For dictionary-based break
    372 $dictionary $dictionary;
    373 
    374