-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathunicode14_rules.go
More file actions
449 lines (411 loc) · 12.8 KB
/
unicode14_rules.go
File metadata and controls
449 lines (411 loc) · 12.8 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SPDX-License-Identifier: Unlicense OR BSD-3-Clause
package segmenter
import (
"unicode"
ucd "github.com/go-text/typesetting/internal/unicodedata"
)
// Apply the Line Breaking Rules and returns the computed break opportunity
// See https://unicode.org/reports/tr14/#BreakingRules
func (cr *cursor) applyLineBoundaryRules() breakOpportunity {
// start by attributing the break class for the current rune
cr.ruleLB1()
triggerNumSequence := cr.updateNumSequence()
// add the line break rules in reverse order to override
// the lower priority rules.
breakOp := breakEmpty
cr.ruleLB30(&breakOp)
cr.ruleLB30ab(&breakOp)
cr.ruleLB29To26(&breakOp)
cr.ruleLB25(&breakOp, triggerNumSequence)
cr.ruleLB24To22(&breakOp)
cr.ruleLB21To9(&breakOp)
cr.ruleLB8(&breakOp)
cr.ruleLB7To4(&breakOp)
return breakOp
}
// breakOpportunity is a convenient enum,
// mapped to the LineBreak and MandatoryBreak properties,
// avoiding too many bit operations
type breakOpportunity uint8
const (
breakEmpty breakOpportunity = iota // not specified
breakProhibited // no break
breakAllowed // direct break (can always break here)
breakMandatory // break is mandatory (implies breakAllowed)
)
func (cr *cursor) ruleLB30(breakOp *breakOpportunity) {
// (AL | HL | NU) × [OP-[\p{ea=F}\p{ea=W}\p{ea=H}]]
if (cr.prevLine == ucd.BreakAL || cr.prevLine == ucd.BreakHL || cr.prevLine == ucd.BreakNU) &&
cr.line == ucd.BreakOP && !unicode.Is(ucd.LargeEastAsian, cr.r) {
*breakOp = breakProhibited
}
// [CP-[\p{ea=F}\p{ea=W}\p{ea=H}]] × (AL | HL | NU)
if cr.prevLine == ucd.BreakCP && !unicode.Is(ucd.LargeEastAsian, cr.prev) &&
(cr.line == ucd.BreakAL || cr.line == ucd.BreakHL || cr.line == ucd.BreakNU) {
*breakOp = breakProhibited
}
}
func (cr *cursor) ruleLB30ab(breakOp *breakOpportunity) {
// (RI RI)* RI × RI
if cr.isPrevLinebreakRIOdd && cr.line == ucd.BreakRI { // LB30a
*breakOp = breakProhibited
}
// LB30b
// EB × EM
if cr.prevLine == ucd.BreakEB && cr.line == ucd.BreakEM {
*breakOp = breakProhibited
}
// [\p{Extended_Pictographic}&\p{Cn}] × EM
if unicode.Is(ucd.Extended_Pictographic, cr.prev) && ucd.LookupType(cr.prev) == nil &&
cr.line == ucd.BreakEM {
*breakOp = breakProhibited
}
}
func (cr *cursor) ruleLB29To26(breakOp *breakOpportunity) {
b0, b1 := cr.prevLine, cr.line
// LB29 : IS × (AL | HL)
if b0 == ucd.BreakIS && (b1 == ucd.BreakAL || b1 == ucd.BreakHL) {
*breakOp = breakProhibited
}
// LB28 : (AL | HL) × (AL | HL)
if (b0 == ucd.BreakAL || b0 == ucd.BreakHL) && (b1 == ucd.BreakAL || b1 == ucd.BreakHL) {
*breakOp = breakProhibited
}
// LB27
// (JL | JV | JT | H2 | H3) × PO
if (b0 == ucd.BreakJL || b0 == ucd.BreakJV || b0 == ucd.BreakJT || b0 == ucd.BreakH2 || b0 == ucd.BreakH3) &&
b1 == ucd.BreakPO {
*breakOp = breakProhibited
}
// PR × (JL | JV | JT | H2 | H3)
if b0 == ucd.BreakPR &&
(b1 == ucd.BreakJL || b1 == ucd.BreakJV || b1 == ucd.BreakJT || b1 == ucd.BreakH2 || b1 == ucd.BreakH3) {
*breakOp = breakProhibited
}
// LB26
// JL × (JL | JV | H2 | H3)
if b0 == ucd.BreakJL &&
(b1 == ucd.BreakJL || b1 == ucd.BreakJV || b1 == ucd.BreakH2 || b1 == ucd.BreakH3) {
*breakOp = breakProhibited
}
// (JV | H2) × (JV | JT)
if (b0 == ucd.BreakJV || b0 == ucd.BreakH2) && (b1 == ucd.BreakJV || b1 == ucd.BreakJT) {
*breakOp = breakProhibited
}
// (JT | H3) × JT
if (b0 == ucd.BreakJT || b0 == ucd.BreakH3) && b1 == ucd.BreakJT {
*breakOp = breakProhibited
}
}
// we follow other implementations by using the tailoring described
// in Example 7
func (cr *cursor) ruleLB25(breakOp *breakOpportunity, triggerNumSequence bool) {
br0, br1 := cr.prevLine, cr.line
// (PR | PO) × ( OP | HY )? NU
if (br0 == ucd.BreakPR || br0 == ucd.BreakPO) && br1 == ucd.BreakNU {
*breakOp = breakProhibited
}
if (br0 == ucd.BreakPR || br0 == ucd.BreakPO) &&
(br1 == ucd.BreakOP || br1 == ucd.BreakHY) &&
cr.nextLine == ucd.BreakNU {
*breakOp = breakProhibited
}
// ( OP | HY ) × NU
if (br0 == ucd.BreakOP || br0 == ucd.BreakHY) && br1 == ucd.BreakNU {
*breakOp = breakProhibited
}
// NU × (NU | SY | IS)
if br0 == ucd.BreakNU && (br1 == ucd.BreakNU || br1 == ucd.BreakSY || br1 == ucd.BreakIS) {
*breakOp = breakProhibited
}
// NU (NU | SY | IS)* × (NU | SY | IS | CL | CP )
if triggerNumSequence {
*breakOp = breakProhibited
}
}
func (cr *cursor) ruleLB24To22(breakOp *breakOpportunity) {
br0, br1 := cr.prevLine, cr.line
// LB24
// (PR | PO) × (AL | HL)
if (br0 == ucd.BreakPR || br0 == ucd.BreakPO) && (br1 == ucd.BreakAL || br1 == ucd.BreakHL) {
*breakOp = breakProhibited
}
// (AL | HL) × (PR | PO)
if (br0 == ucd.BreakAL || br0 == ucd.BreakHL) && (br1 == ucd.BreakPR || br1 == ucd.BreakPO) {
*breakOp = breakProhibited
}
// LB23
// (AL | HL) × NU
if (br0 == ucd.BreakAL || br0 == ucd.BreakHL) && br1 == ucd.BreakNU {
*breakOp = breakProhibited
}
// NU × (AL | HL)
if br0 == ucd.BreakNU && (br1 == ucd.BreakAL || br1 == ucd.BreakHL) {
*breakOp = breakProhibited
}
// LB23a
// PR × (ID | EB | EM)
if br0 == ucd.BreakPR && (br1 == ucd.BreakID || br1 == ucd.BreakEB || br1 == ucd.BreakEM) {
*breakOp = breakProhibited
}
// (ID | EB | EM) × PO
if (br0 == ucd.BreakID || br0 == ucd.BreakEB || br0 == ucd.BreakEM) && br1 == ucd.BreakPO {
*breakOp = breakProhibited
}
// LB22 : × IN
if br1 == ucd.BreakIN {
*breakOp = breakProhibited
}
}
func (cr *cursor) ruleLB21To9(breakOp *breakOpportunity) {
br0, br1 := cr.prevLine, cr.line
// LB21
// × BA
// × HY
// × NS
// BB ×
if br1 == ucd.BreakBA || br1 == ucd.BreakHY || br1 == ucd.BreakNS || br0 == ucd.BreakBB {
*breakOp = breakProhibited
}
// LB21a : HL (HY | BA) ×
if cr.prevPrevLine == ucd.BreakHL &&
(br0 == ucd.BreakHY || br0 == ucd.BreakBA) {
*breakOp = breakProhibited
}
// LB21b : SY × HL
if br0 == ucd.BreakSY && br1 == ucd.BreakHL {
*breakOp = breakProhibited
}
// LB20
// ÷ CB
// CB ÷
if br0 == ucd.BreakCB || br1 == ucd.BreakCB {
*breakOp = breakAllowed
}
// LB19
// × QU
// QU ×
if br0 == ucd.BreakQU || br1 == ucd.BreakQU {
*breakOp = breakProhibited
}
// LB18 : SP ÷
if br0 == ucd.BreakSP {
*breakOp = breakAllowed
}
// LB17 : B2 SP* × B2
if cr.beforeSpaces == ucd.BreakB2 && br1 == ucd.BreakB2 {
*breakOp = breakProhibited
}
// LB16 : (CL | CP) SP* × NS
if (cr.beforeSpaces == ucd.BreakCL || cr.beforeSpaces == ucd.BreakCP) && br1 == ucd.BreakNS {
*breakOp = breakProhibited
}
// LB15 : QU SP* × OP
if cr.beforeSpaces == ucd.BreakQU && br1 == ucd.BreakOP {
*breakOp = breakProhibited
}
// LB14 : OP SP* ×
if cr.beforeSpaces == ucd.BreakOP {
*breakOp = breakProhibited
}
// rule LB13, with the tailoring described in Example 7
// × EX
if br1 == ucd.BreakEX {
*breakOp = breakProhibited
}
// [^NU] × CL
// [^NU] × CP
// [^NU] × IS
// [^NU] × SY
if br0 != ucd.BreakNU &&
(br1 == ucd.BreakCL || br1 == ucd.BreakCP || br1 == ucd.BreakIS || br1 == ucd.BreakSY) {
*breakOp = breakProhibited
}
// LB12 : GL ×
if br0 == ucd.BreakGL {
*breakOp = breakProhibited
}
// LB12a : [^SP BA HY] × GL
if (br0 != ucd.BreakSP && br0 != ucd.BreakBA && br0 != ucd.BreakHY) &&
br1 == ucd.BreakGL {
*breakOp = breakProhibited
}
// LB11
// × WJ
// WJ ×
if br0 == ucd.BreakWJ || br1 == ucd.BreakWJ {
*breakOp = breakProhibited
}
// rule LB9 : "Do not break a combining character sequence"
// where X is any line break class except BK, CR, LF, NL, SP, or ZW.
// see also [endIteration]
if br1 == ucd.BreakCM || br1 == ucd.BreakZWJ {
if !(br0 == ucd.BreakBK || br0 == ucd.BreakCR || br0 == ucd.BreakLF ||
br0 == ucd.BreakNL || br0 == ucd.BreakSP || br0 == ucd.BreakZW) {
*breakOp = breakProhibited
}
}
}
func (cr *cursor) ruleLB8(breakOp *breakOpportunity) {
// rule LB8 : ZW SP* ÷
if cr.beforeSpaces == ucd.BreakZW {
*breakOp = breakAllowed
}
// rule LB8a : ZWJ ×
// there is a catch here : prevLine is not always exactly
// the class at index i-1, because of rules LB9 and LB10
// however, rule LB8a applies before LB9 and LB10, meaning
// we need to use the real class
if unicode.Is(ucd.BreakZWJ, cr.prev) {
*breakOp = breakProhibited
}
}
func (cr *cursor) ruleLB7To4(breakOp *breakOpportunity) {
// LB7
// × SP
// × ZW
if cr.line == ucd.BreakSP || cr.line == ucd.BreakZW {
*breakOp = breakProhibited
}
// LB6 : × ( BK | CR | LF | NL )
if cr.line == ucd.BreakBK || cr.line == ucd.BreakCR || cr.line == ucd.BreakLF || cr.line == ucd.BreakNL {
*breakOp = breakProhibited
}
// LB4 and LB5
// BK !
// CR !
// LF !
// NL !
// (CR × LF is actually handled in rule LB6)
if cr.prevLine == ucd.BreakBK || (cr.prevLine == ucd.BreakCR && cr.r != '\n') ||
cr.prevLine == ucd.BreakLF || cr.prevLine == ucd.BreakNL {
*breakOp = breakMandatory
}
}
// apply rule LB1 to resolve break classses AI, SG, XX, SA and CJ.
// We use the default values specified in https://unicode.org/reports/tr14/#BreakingRules.
func (cr *cursor) ruleLB1() {
switch cr.line {
case ucd.BreakAI, ucd.BreakSG, ucd.BreakXX:
cr.line = ucd.BreakAL
case ucd.BreakSA:
if unicode.Is(unicode.Mn, cr.r) || unicode.Is(unicode.Mc, cr.r) {
cr.line = ucd.BreakCM
} else {
cr.line = ucd.BreakAL
}
case ucd.BreakCJ:
cr.line = ucd.BreakNS
}
}
type numSequenceState uint8
const (
noNumSequence numSequenceState = iota // we are not in a sequence
inNumSequence // we are in NU (NU | SY | IS)*
seenCloseNum // we are at NU (NU | SY | IS)* (CL | CP)?
)
// update the `numSequence` state used for rule LB25
// and returns true if we matched one
func (cr *cursor) updateNumSequence() bool {
// note that rule LB9 also apply : (CM|ZWJ) do not change
// the flag
if cr.line == ucd.BreakCM || cr.line == ucd.BreakZWJ {
return false
}
switch cr.numSequence {
case noNumSequence:
if cr.line == ucd.BreakNU { // start a sequence
cr.numSequence = inNumSequence
}
return false
case inNumSequence:
switch cr.line {
case ucd.BreakNU, ucd.BreakSY, ucd.BreakIS:
// NU (NU | SY | IS)* × (NU | SY | IS) : the sequence continue
return true
case ucd.BreakCL, ucd.BreakCP:
// NU (NU | SY | IS)* × (CL | CP)
cr.numSequence = seenCloseNum
return true
case ucd.BreakPO, ucd.BreakPR:
// NU (NU | SY | IS)* × (PO | PR) : close the sequence
cr.numSequence = noNumSequence
return true
default:
cr.numSequence = noNumSequence
return false
}
case seenCloseNum:
cr.numSequence = noNumSequence // close the sequence anyway
if cr.line == ucd.BreakPO || cr.line == ucd.BreakPR {
// NU (NU | SY | IS)* (CL | CP) × (PO | PR)
return true
}
return false
default:
panic("exhaustive switch")
}
}
// startIteration updates the cursor properties, setting the current
// rune to text[i].
// Some properties depending on the context are rather
// updated in the previous `endIteration` call.
func (cr *cursor) startIteration(text []rune, i int) {
cr.prev = cr.r
if i < len(text) {
cr.r = text[i]
} else {
cr.r = paragraphSeparator
}
if i == len(text) {
cr.next = 0
} else if i == len(text)-1 {
// we fill in the last element of `attrs` by assuming
// there's a paragraph separators off the end of text
cr.next = paragraphSeparator
} else {
cr.next = text[i+1]
}
// query general unicode properties for the current rune
cr.isExtentedPic = unicode.Is(ucd.Extended_Pictographic, cr.r)
cr.prevGrapheme = cr.grapheme
cr.grapheme = ucd.LookupGraphemeBreakClass(cr.r)
if cr.word != ucd.WordBreakExtendFormat {
cr.prevPrevWord = cr.prevWord
cr.prevWord = cr.word
cr.prevWordNoExtend = i - 1
}
cr.word = ucd.LookupWordBreakClass(cr.r)
// prevPrevLine and prevLine are handled in endIteration
cr.line = cr.nextLine // avoid calling LookupLineBreakClass twice
cr.nextLine = ucd.LookupLineBreakClass(cr.next)
}
// end the current iteration, computing some of the properties
// required for the next rune and respecting rule LB9 and LB10
func (cr *cursor) endIteration(isStart bool) {
// start by handling rule LB9 and LB10
if cr.line == ucd.BreakCM || cr.line == ucd.BreakZWJ {
isLB10 := cr.prevLine == ucd.BreakBK ||
cr.prevLine == ucd.BreakCR ||
cr.prevLine == ucd.BreakLF ||
cr.prevLine == ucd.BreakNL ||
cr.prevLine == ucd.BreakSP ||
cr.prevLine == ucd.BreakZW
if isStart || isLB10 { // Rule LB10
cr.prevLine = ucd.BreakAL
} // else rule LB9 : ignore the rune for prevLine and prevPrevLine
} else { // regular update
cr.prevPrevLine = cr.prevLine
cr.prevLine = cr.line
}
// keep track of the rune before the spaces
if cr.prevLine != ucd.BreakSP {
cr.beforeSpaces = cr.prevLine
}
// update RegionalIndicator parity used for LB30a
if cr.line == ucd.BreakRI {
cr.isPrevLinebreakRIOdd = !cr.isPrevLinebreakRIOdd
} else if !(cr.line == ucd.BreakCM || cr.line == ucd.BreakZWJ) { // beware of the rule LB9: (CM|ZWJ) ignore the update
cr.isPrevLinebreakRIOdd = false
}
}