blob: 000be7fc7a342ea326cd43022bae4a771baa248a [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package bidi
6
7import (
8 "fmt"
9 "log"
10)
11
12// This implementation is a port based on the reference implementation found at:
13// https://www.unicode.org/Public/PROGRAMS/BidiReferenceJava/
14//
15// described in Unicode Bidirectional Algorithm (UAX #9).
16//
17// Input:
18// There are two levels of input to the algorithm, since clients may prefer to
19// supply some information from out-of-band sources rather than relying on the
20// default behavior.
21//
22// - Bidi class array
23// - Bidi class array, with externally supplied base line direction
24//
25// Output:
26// Output is separated into several stages:
27//
28// - levels array over entire paragraph
29// - reordering array over entire paragraph
30// - levels array over line
31// - reordering array over line
32//
33// Note that for conformance to the Unicode Bidirectional Algorithm,
34// implementations are only required to generate correct reordering and
35// character directionality (odd or even levels) over a line. Generating
36// identical level arrays over a line is not required. Bidi explicit format
37// codes (LRE, RLE, LRO, RLO, PDF) and BN can be assigned arbitrary levels and
38// positions as long as the rest of the input is properly reordered.
39//
40// As the algorithm is defined to operate on a single paragraph at a time, this
41// implementation is written to handle single paragraphs. Thus rule P1 is
42// presumed by this implementation-- the data provided to the implementation is
43// assumed to be a single paragraph, and either contains no 'B' codes, or a
44// single 'B' code at the end of the input. 'B' is allowed as input to
45// illustrate how the algorithm assigns it a level.
46//
47// Also note that rules L3 and L4 depend on the rendering engine that uses the
48// result of the bidi algorithm. This implementation assumes that the rendering
49// engine expects combining marks in visual order (e.g. to the left of their
50// base character in RTL runs) and that it adjusts the glyphs used to render
51// mirrored characters that are in RTL runs so that they render appropriately.
52
53// level is the embedding level of a character. Even embedding levels indicate
54// left-to-right order and odd levels indicate right-to-left order. The special
55// level of -1 is reserved for undefined order.
56type level int8
57
58const implicitLevel level = -1
59
60// in returns if x is equal to any of the values in set.
61func (c Class) in(set ...Class) bool {
62 for _, s := range set {
63 if c == s {
64 return true
65 }
66 }
67 return false
68}
69
70// A paragraph contains the state of a paragraph.
71type paragraph struct {
72 initialTypes []Class
73
74 // Arrays of properties needed for paired bracket evaluation in N0
75 pairTypes []bracketType // paired Bracket types for paragraph
76 pairValues []rune // rune for opening bracket or pbOpen and pbClose; 0 for pbNone
77
78 embeddingLevel level // default: = implicitLevel;
79
80 // at the paragraph levels
81 resultTypes []Class
82 resultLevels []level
83
84 // Index of matching PDI for isolate initiator characters. For other
85 // characters, the value of matchingPDI will be set to -1. For isolate
86 // initiators with no matching PDI, matchingPDI will be set to the length of
87 // the input string.
88 matchingPDI []int
89
90 // Index of matching isolate initiator for PDI characters. For other
91 // characters, and for PDIs with no matching isolate initiator, the value of
92 // matchingIsolateInitiator will be set to -1.
93 matchingIsolateInitiator []int
94}
95
96// newParagraph initializes a paragraph. The user needs to supply a few arrays
97// corresponding to the preprocessed text input. The types correspond to the
98// Unicode BiDi classes for each rune. pairTypes indicates the bracket type for
99// each rune. pairValues provides a unique bracket class identifier for each
100// rune (suggested is the rune of the open bracket for opening and matching
101// close brackets, after normalization). The embedding levels are optional, but
102// may be supplied to encode embedding levels of styled text.
103func newParagraph(types []Class, pairTypes []bracketType, pairValues []rune, levels level) (*paragraph, error) {
104 var err error
105 if err = validateTypes(types); err != nil {
106 return nil, err
107 }
108 if err = validatePbTypes(pairTypes); err != nil {
109 return nil, err
110 }
111 if err = validatePbValues(pairValues, pairTypes); err != nil {
112 return nil, err
113 }
114 if err = validateParagraphEmbeddingLevel(levels); err != nil {
115 return nil, err
116 }
117
118 p := &paragraph{
119 initialTypes: append([]Class(nil), types...),
120 embeddingLevel: levels,
121
122 pairTypes: pairTypes,
123 pairValues: pairValues,
124
125 resultTypes: append([]Class(nil), types...),
126 }
127 p.run()
128 return p, nil
129}
130
131func (p *paragraph) Len() int { return len(p.initialTypes) }
132
133// The algorithm. Does not include line-based processing (Rules L1, L2).
134// These are applied later in the line-based phase of the algorithm.
135func (p *paragraph) run() {
136 p.determineMatchingIsolates()
137
138 // 1) determining the paragraph level
139 // Rule P1 is the requirement for entering this algorithm.
140 // Rules P2, P3.
141 // If no externally supplied paragraph embedding level, use default.
142 if p.embeddingLevel == implicitLevel {
143 p.embeddingLevel = p.determineParagraphEmbeddingLevel(0, p.Len())
144 }
145
146 // Initialize result levels to paragraph embedding level.
147 p.resultLevels = make([]level, p.Len())
148 setLevels(p.resultLevels, p.embeddingLevel)
149
150 // 2) Explicit levels and directions
151 // Rules X1-X8.
152 p.determineExplicitEmbeddingLevels()
153
154 // Rule X9.
155 // We do not remove the embeddings, the overrides, the PDFs, and the BNs
156 // from the string explicitly. But they are not copied into isolating run
157 // sequences when they are created, so they are removed for all
158 // practical purposes.
159
160 // Rule X10.
161 // Run remainder of algorithm one isolating run sequence at a time
162 for _, seq := range p.determineIsolatingRunSequences() {
163 // 3) resolving weak types
164 // Rules W1-W7.
165 seq.resolveWeakTypes()
166
167 // 4a) resolving paired brackets
168 // Rule N0
169 resolvePairedBrackets(seq)
170
171 // 4b) resolving neutral types
172 // Rules N1-N3.
173 seq.resolveNeutralTypes()
174
175 // 5) resolving implicit embedding levels
176 // Rules I1, I2.
177 seq.resolveImplicitLevels()
178
179 // Apply the computed levels and types
180 seq.applyLevelsAndTypes()
181 }
182
183 // Assign appropriate levels to 'hide' LREs, RLEs, LROs, RLOs, PDFs, and
184 // BNs. This is for convenience, so the resulting level array will have
185 // a value for every character.
186 p.assignLevelsToCharactersRemovedByX9()
187}
188
189// determineMatchingIsolates determines the matching PDI for each isolate
190// initiator and vice versa.
191//
192// Definition BD9.
193//
194// At the end of this function:
195//
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500196// - The member variable matchingPDI is set to point to the index of the
197// matching PDI character for each isolate initiator character. If there is
198// no matching PDI, it is set to the length of the input text. For other
199// characters, it is set to -1.
200// - The member variable matchingIsolateInitiator is set to point to the
201// index of the matching isolate initiator character for each PDI character.
202// If there is no matching isolate initiator, or the character is not a PDI,
203// it is set to -1.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400204func (p *paragraph) determineMatchingIsolates() {
205 p.matchingPDI = make([]int, p.Len())
206 p.matchingIsolateInitiator = make([]int, p.Len())
207
208 for i := range p.matchingIsolateInitiator {
209 p.matchingIsolateInitiator[i] = -1
210 }
211
212 for i := range p.matchingPDI {
213 p.matchingPDI[i] = -1
214
215 if t := p.resultTypes[i]; t.in(LRI, RLI, FSI) {
216 depthCounter := 1
217 for j := i + 1; j < p.Len(); j++ {
218 if u := p.resultTypes[j]; u.in(LRI, RLI, FSI) {
219 depthCounter++
220 } else if u == PDI {
221 if depthCounter--; depthCounter == 0 {
222 p.matchingPDI[i] = j
223 p.matchingIsolateInitiator[j] = i
224 break
225 }
226 }
227 }
228 if p.matchingPDI[i] == -1 {
229 p.matchingPDI[i] = p.Len()
230 }
231 }
232 }
233}
234
235// determineParagraphEmbeddingLevel reports the resolved paragraph direction of
236// the substring limited by the given range [start, end).
237//
238// Determines the paragraph level based on rules P2, P3. This is also used
239// in rule X5c to find if an FSI should resolve to LRI or RLI.
240func (p *paragraph) determineParagraphEmbeddingLevel(start, end int) level {
241 var strongType Class = unknownClass
242
243 // Rule P2.
244 for i := start; i < end; i++ {
245 if t := p.resultTypes[i]; t.in(L, AL, R) {
246 strongType = t
247 break
248 } else if t.in(FSI, LRI, RLI) {
249 i = p.matchingPDI[i] // skip over to the matching PDI
250 if i > end {
251 log.Panic("assert (i <= end)")
252 }
253 }
254 }
255 // Rule P3.
256 switch strongType {
257 case unknownClass: // none found
258 // default embedding level when no strong types found is 0.
259 return 0
260 case L:
261 return 0
262 default: // AL, R
263 return 1
264 }
265}
266
267const maxDepth = 125
268
269// This stack will store the embedding levels and override and isolated
270// statuses
271type directionalStatusStack struct {
272 stackCounter int
273 embeddingLevelStack [maxDepth + 1]level
274 overrideStatusStack [maxDepth + 1]Class
275 isolateStatusStack [maxDepth + 1]bool
276}
277
278func (s *directionalStatusStack) empty() { s.stackCounter = 0 }
279func (s *directionalStatusStack) pop() { s.stackCounter-- }
280func (s *directionalStatusStack) depth() int { return s.stackCounter }
281
282func (s *directionalStatusStack) push(level level, overrideStatus Class, isolateStatus bool) {
283 s.embeddingLevelStack[s.stackCounter] = level
284 s.overrideStatusStack[s.stackCounter] = overrideStatus
285 s.isolateStatusStack[s.stackCounter] = isolateStatus
286 s.stackCounter++
287}
288
289func (s *directionalStatusStack) lastEmbeddingLevel() level {
290 return s.embeddingLevelStack[s.stackCounter-1]
291}
292
293func (s *directionalStatusStack) lastDirectionalOverrideStatus() Class {
294 return s.overrideStatusStack[s.stackCounter-1]
295}
296
297func (s *directionalStatusStack) lastDirectionalIsolateStatus() bool {
298 return s.isolateStatusStack[s.stackCounter-1]
299}
300
301// Determine explicit levels using rules X1 - X8
302func (p *paragraph) determineExplicitEmbeddingLevels() {
303 var stack directionalStatusStack
304 var overflowIsolateCount, overflowEmbeddingCount, validIsolateCount int
305
306 // Rule X1.
307 stack.push(p.embeddingLevel, ON, false)
308
309 for i, t := range p.resultTypes {
310 // Rules X2, X3, X4, X5, X5a, X5b, X5c
311 switch t {
312 case RLE, LRE, RLO, LRO, RLI, LRI, FSI:
313 isIsolate := t.in(RLI, LRI, FSI)
314 isRTL := t.in(RLE, RLO, RLI)
315
316 // override if this is an FSI that resolves to RLI
317 if t == FSI {
318 isRTL = (p.determineParagraphEmbeddingLevel(i+1, p.matchingPDI[i]) == 1)
319 }
320 if isIsolate {
321 p.resultLevels[i] = stack.lastEmbeddingLevel()
322 if stack.lastDirectionalOverrideStatus() != ON {
323 p.resultTypes[i] = stack.lastDirectionalOverrideStatus()
324 }
325 }
326
327 var newLevel level
328 if isRTL {
329 // least greater odd
330 newLevel = (stack.lastEmbeddingLevel() + 1) | 1
331 } else {
332 // least greater even
333 newLevel = (stack.lastEmbeddingLevel() + 2) &^ 1
334 }
335
336 if newLevel <= maxDepth && overflowIsolateCount == 0 && overflowEmbeddingCount == 0 {
337 if isIsolate {
338 validIsolateCount++
339 }
340 // Push new embedding level, override status, and isolated
341 // status.
342 // No check for valid stack counter, since the level check
343 // suffices.
344 switch t {
345 case LRO:
346 stack.push(newLevel, L, isIsolate)
347 case RLO:
348 stack.push(newLevel, R, isIsolate)
349 default:
350 stack.push(newLevel, ON, isIsolate)
351 }
352 // Not really part of the spec
353 if !isIsolate {
354 p.resultLevels[i] = newLevel
355 }
356 } else {
357 // This is an invalid explicit formatting character,
358 // so apply the "Otherwise" part of rules X2-X5b.
359 if isIsolate {
360 overflowIsolateCount++
361 } else { // !isIsolate
362 if overflowIsolateCount == 0 {
363 overflowEmbeddingCount++
364 }
365 }
366 }
367
368 // Rule X6a
369 case PDI:
370 if overflowIsolateCount > 0 {
371 overflowIsolateCount--
372 } else if validIsolateCount == 0 {
373 // do nothing
374 } else {
375 overflowEmbeddingCount = 0
376 for !stack.lastDirectionalIsolateStatus() {
377 stack.pop()
378 }
379 stack.pop()
380 validIsolateCount--
381 }
382 p.resultLevels[i] = stack.lastEmbeddingLevel()
383
384 // Rule X7
385 case PDF:
386 // Not really part of the spec
387 p.resultLevels[i] = stack.lastEmbeddingLevel()
388
389 if overflowIsolateCount > 0 {
390 // do nothing
391 } else if overflowEmbeddingCount > 0 {
392 overflowEmbeddingCount--
393 } else if !stack.lastDirectionalIsolateStatus() && stack.depth() >= 2 {
394 stack.pop()
395 }
396
397 case B: // paragraph separator.
398 // Rule X8.
399
400 // These values are reset for clarity, in this implementation B
401 // can only occur as the last code in the array.
402 stack.empty()
403 overflowIsolateCount = 0
404 overflowEmbeddingCount = 0
405 validIsolateCount = 0
406 p.resultLevels[i] = p.embeddingLevel
407
408 default:
409 p.resultLevels[i] = stack.lastEmbeddingLevel()
410 if stack.lastDirectionalOverrideStatus() != ON {
411 p.resultTypes[i] = stack.lastDirectionalOverrideStatus()
412 }
413 }
414 }
415}
416
417type isolatingRunSequence struct {
418 p *paragraph
419
420 indexes []int // indexes to the original string
421
422 types []Class // type of each character using the index
423 resolvedLevels []level // resolved levels after application of rules
424 level level
425 sos, eos Class
426}
427
428func (i *isolatingRunSequence) Len() int { return len(i.indexes) }
429
430func maxLevel(a, b level) level {
431 if a > b {
432 return a
433 }
434 return b
435}
436
437// Rule X10, second bullet: Determine the start-of-sequence (sos) and end-of-sequence (eos) types,
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500438//
439// either L or R, for each isolating run sequence.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400440func (p *paragraph) isolatingRunSequence(indexes []int) *isolatingRunSequence {
441 length := len(indexes)
442 types := make([]Class, length)
443 for i, x := range indexes {
444 types[i] = p.resultTypes[x]
445 }
446
447 // assign level, sos and eos
448 prevChar := indexes[0] - 1
449 for prevChar >= 0 && isRemovedByX9(p.initialTypes[prevChar]) {
450 prevChar--
451 }
452 prevLevel := p.embeddingLevel
453 if prevChar >= 0 {
454 prevLevel = p.resultLevels[prevChar]
455 }
456
457 var succLevel level
458 lastType := types[length-1]
459 if lastType.in(LRI, RLI, FSI) {
460 succLevel = p.embeddingLevel
461 } else {
462 // the first character after the end of run sequence
463 limit := indexes[length-1] + 1
464 for ; limit < p.Len() && isRemovedByX9(p.initialTypes[limit]); limit++ {
465
466 }
467 succLevel = p.embeddingLevel
468 if limit < p.Len() {
469 succLevel = p.resultLevels[limit]
470 }
471 }
472 level := p.resultLevels[indexes[0]]
473 return &isolatingRunSequence{
474 p: p,
475 indexes: indexes,
476 types: types,
477 level: level,
478 sos: typeForLevel(maxLevel(prevLevel, level)),
479 eos: typeForLevel(maxLevel(succLevel, level)),
480 }
481}
482
483// Resolving weak types Rules W1-W7.
484//
485// Note that some weak types (EN, AN) remain after this processing is
486// complete.
487func (s *isolatingRunSequence) resolveWeakTypes() {
488
489 // on entry, only these types remain
490 s.assertOnly(L, R, AL, EN, ES, ET, AN, CS, B, S, WS, ON, NSM, LRI, RLI, FSI, PDI)
491
492 // Rule W1.
493 // Changes all NSMs.
494 precedingCharacterType := s.sos
495 for i, t := range s.types {
496 if t == NSM {
497 s.types[i] = precedingCharacterType
498 } else {
499 if t.in(LRI, RLI, FSI, PDI) {
500 precedingCharacterType = ON
501 }
502 precedingCharacterType = t
503 }
504 }
505
506 // Rule W2.
507 // EN does not change at the start of the run, because sos != AL.
508 for i, t := range s.types {
509 if t == EN {
510 for j := i - 1; j >= 0; j-- {
511 if t := s.types[j]; t.in(L, R, AL) {
512 if t == AL {
513 s.types[i] = AN
514 }
515 break
516 }
517 }
518 }
519 }
520
521 // Rule W3.
522 for i, t := range s.types {
523 if t == AL {
524 s.types[i] = R
525 }
526 }
527
528 // Rule W4.
529 // Since there must be values on both sides for this rule to have an
530 // effect, the scan skips the first and last value.
531 //
532 // Although the scan proceeds left to right, and changes the type
533 // values in a way that would appear to affect the computations
534 // later in the scan, there is actually no problem. A change in the
535 // current value can only affect the value to its immediate right,
536 // and only affect it if it is ES or CS. But the current value can
537 // only change if the value to its right is not ES or CS. Thus
538 // either the current value will not change, or its change will have
539 // no effect on the remainder of the analysis.
540
541 for i := 1; i < s.Len()-1; i++ {
542 t := s.types[i]
543 if t == ES || t == CS {
544 prevSepType := s.types[i-1]
545 succSepType := s.types[i+1]
546 if prevSepType == EN && succSepType == EN {
547 s.types[i] = EN
548 } else if s.types[i] == CS && prevSepType == AN && succSepType == AN {
549 s.types[i] = AN
550 }
551 }
552 }
553
554 // Rule W5.
555 for i, t := range s.types {
556 if t == ET {
557 // locate end of sequence
558 runStart := i
559 runEnd := s.findRunLimit(runStart, ET)
560
561 // check values at ends of sequence
562 t := s.sos
563 if runStart > 0 {
564 t = s.types[runStart-1]
565 }
566 if t != EN {
567 t = s.eos
568 if runEnd < len(s.types) {
569 t = s.types[runEnd]
570 }
571 }
572 if t == EN {
573 setTypes(s.types[runStart:runEnd], EN)
574 }
575 // continue at end of sequence
576 i = runEnd
577 }
578 }
579
580 // Rule W6.
581 for i, t := range s.types {
582 if t.in(ES, ET, CS) {
583 s.types[i] = ON
584 }
585 }
586
587 // Rule W7.
588 for i, t := range s.types {
589 if t == EN {
590 // set default if we reach start of run
591 prevStrongType := s.sos
592 for j := i - 1; j >= 0; j-- {
593 t = s.types[j]
594 if t == L || t == R { // AL's have been changed to R
595 prevStrongType = t
596 break
597 }
598 }
599 if prevStrongType == L {
600 s.types[i] = L
601 }
602 }
603 }
604}
605
606// 6) resolving neutral types Rules N1-N2.
607func (s *isolatingRunSequence) resolveNeutralTypes() {
608
609 // on entry, only these types can be in resultTypes
610 s.assertOnly(L, R, EN, AN, B, S, WS, ON, RLI, LRI, FSI, PDI)
611
612 for i, t := range s.types {
613 switch t {
614 case WS, ON, B, S, RLI, LRI, FSI, PDI:
615 // find bounds of run of neutrals
616 runStart := i
617 runEnd := s.findRunLimit(runStart, B, S, WS, ON, RLI, LRI, FSI, PDI)
618
619 // determine effective types at ends of run
620 var leadType, trailType Class
621
622 // Note that the character found can only be L, R, AN, or
623 // EN.
624 if runStart == 0 {
625 leadType = s.sos
626 } else {
627 leadType = s.types[runStart-1]
628 if leadType.in(AN, EN) {
629 leadType = R
630 }
631 }
632 if runEnd == len(s.types) {
633 trailType = s.eos
634 } else {
635 trailType = s.types[runEnd]
636 if trailType.in(AN, EN) {
637 trailType = R
638 }
639 }
640
641 var resolvedType Class
642 if leadType == trailType {
643 // Rule N1.
644 resolvedType = leadType
645 } else {
646 // Rule N2.
647 // Notice the embedding level of the run is used, not
648 // the paragraph embedding level.
649 resolvedType = typeForLevel(s.level)
650 }
651
652 setTypes(s.types[runStart:runEnd], resolvedType)
653
654 // skip over run of (former) neutrals
655 i = runEnd
656 }
657 }
658}
659
660func setLevels(levels []level, newLevel level) {
661 for i := range levels {
662 levels[i] = newLevel
663 }
664}
665
666func setTypes(types []Class, newType Class) {
667 for i := range types {
668 types[i] = newType
669 }
670}
671
672// 7) resolving implicit embedding levels Rules I1, I2.
673func (s *isolatingRunSequence) resolveImplicitLevels() {
674
675 // on entry, only these types can be in resultTypes
676 s.assertOnly(L, R, EN, AN)
677
678 s.resolvedLevels = make([]level, len(s.types))
679 setLevels(s.resolvedLevels, s.level)
680
681 if (s.level & 1) == 0 { // even level
682 for i, t := range s.types {
683 // Rule I1.
684 if t == L {
685 // no change
686 } else if t == R {
687 s.resolvedLevels[i] += 1
688 } else { // t == AN || t == EN
689 s.resolvedLevels[i] += 2
690 }
691 }
692 } else { // odd level
693 for i, t := range s.types {
694 // Rule I2.
695 if t == R {
696 // no change
697 } else { // t == L || t == AN || t == EN
698 s.resolvedLevels[i] += 1
699 }
700 }
701 }
702}
703
704// Applies the levels and types resolved in rules W1-I2 to the
705// resultLevels array.
706func (s *isolatingRunSequence) applyLevelsAndTypes() {
707 for i, x := range s.indexes {
708 s.p.resultTypes[x] = s.types[i]
709 s.p.resultLevels[x] = s.resolvedLevels[i]
710 }
711}
712
713// Return the limit of the run consisting only of the types in validSet
714// starting at index. This checks the value at index, and will return
715// index if that value is not in validSet.
716func (s *isolatingRunSequence) findRunLimit(index int, validSet ...Class) int {
717loop:
718 for ; index < len(s.types); index++ {
719 t := s.types[index]
720 for _, valid := range validSet {
721 if t == valid {
722 continue loop
723 }
724 }
725 return index // didn't find a match in validSet
726 }
727 return len(s.types)
728}
729
730// Algorithm validation. Assert that all values in types are in the
731// provided set.
732func (s *isolatingRunSequence) assertOnly(codes ...Class) {
733loop:
734 for i, t := range s.types {
735 for _, c := range codes {
736 if t == c {
737 continue loop
738 }
739 }
740 log.Panicf("invalid bidi code %v present in assertOnly at position %d", t, s.indexes[i])
741 }
742}
743
744// determineLevelRuns returns an array of level runs. Each level run is
745// described as an array of indexes into the input string.
746//
747// Determines the level runs. Rule X9 will be applied in determining the
748// runs, in the way that makes sure the characters that are supposed to be
749// removed are not included in the runs.
750func (p *paragraph) determineLevelRuns() [][]int {
751 run := []int{}
752 allRuns := [][]int{}
753 currentLevel := implicitLevel
754
755 for i := range p.initialTypes {
756 if !isRemovedByX9(p.initialTypes[i]) {
757 if p.resultLevels[i] != currentLevel {
758 // we just encountered a new run; wrap up last run
759 if currentLevel >= 0 { // only wrap it up if there was a run
760 allRuns = append(allRuns, run)
761 run = nil
762 }
763 // Start new run
764 currentLevel = p.resultLevels[i]
765 }
766 run = append(run, i)
767 }
768 }
769 // Wrap up the final run, if any
770 if len(run) > 0 {
771 allRuns = append(allRuns, run)
772 }
773 return allRuns
774}
775
776// Definition BD13. Determine isolating run sequences.
777func (p *paragraph) determineIsolatingRunSequences() []*isolatingRunSequence {
778 levelRuns := p.determineLevelRuns()
779
780 // Compute the run that each character belongs to
781 runForCharacter := make([]int, p.Len())
782 for i, run := range levelRuns {
783 for _, index := range run {
784 runForCharacter[index] = i
785 }
786 }
787
788 sequences := []*isolatingRunSequence{}
789
790 var currentRunSequence []int
791
792 for _, run := range levelRuns {
793 first := run[0]
794 if p.initialTypes[first] != PDI || p.matchingIsolateInitiator[first] == -1 {
795 currentRunSequence = nil
796 // int run = i;
797 for {
798 // Copy this level run into currentRunSequence
799 currentRunSequence = append(currentRunSequence, run...)
800
801 last := currentRunSequence[len(currentRunSequence)-1]
802 lastT := p.initialTypes[last]
803 if lastT.in(LRI, RLI, FSI) && p.matchingPDI[last] != p.Len() {
804 run = levelRuns[runForCharacter[p.matchingPDI[last]]]
805 } else {
806 break
807 }
808 }
809 sequences = append(sequences, p.isolatingRunSequence(currentRunSequence))
810 }
811 }
812 return sequences
813}
814
815// Assign level information to characters removed by rule X9. This is for
816// ease of relating the level information to the original input data. Note
817// that the levels assigned to these codes are arbitrary, they're chosen so
818// as to avoid breaking level runs.
819func (p *paragraph) assignLevelsToCharactersRemovedByX9() {
820 for i, t := range p.initialTypes {
821 if t.in(LRE, RLE, LRO, RLO, PDF, BN) {
822 p.resultTypes[i] = t
823 p.resultLevels[i] = -1
824 }
825 }
826 // now propagate forward the levels information (could have
827 // propagated backward, the main thing is not to introduce a level
828 // break where one doesn't already exist).
829
830 if p.resultLevels[0] == -1 {
831 p.resultLevels[0] = p.embeddingLevel
832 }
833 for i := 1; i < len(p.initialTypes); i++ {
834 if p.resultLevels[i] == -1 {
835 p.resultLevels[i] = p.resultLevels[i-1]
836 }
837 }
838 // Embedding information is for informational purposes only so need not be
839 // adjusted.
840}
841
842//
843// Output
844//
845
846// getLevels computes levels array breaking lines at offsets in linebreaks.
847// Rule L1.
848//
849// The linebreaks array must include at least one value. The values must be
850// in strictly increasing order (no duplicates) between 1 and the length of
851// the text, inclusive. The last value must be the length of the text.
852func (p *paragraph) getLevels(linebreaks []int) []level {
853 // Note that since the previous processing has removed all
854 // P, S, and WS values from resultTypes, the values referred to
855 // in these rules are the initial types, before any processing
856 // has been applied (including processing of overrides).
857 //
858 // This example implementation has reinserted explicit format codes
859 // and BN, in order that the levels array correspond to the
860 // initial text. Their final placement is not normative.
861 // These codes are treated like WS in this implementation,
862 // so they don't interrupt sequences of WS.
863
864 validateLineBreaks(linebreaks, p.Len())
865
866 result := append([]level(nil), p.resultLevels...)
867
868 // don't worry about linebreaks since if there is a break within
869 // a series of WS values preceding S, the linebreak itself
870 // causes the reset.
871 for i, t := range p.initialTypes {
872 if t.in(B, S) {
873 // Rule L1, clauses one and two.
874 result[i] = p.embeddingLevel
875
876 // Rule L1, clause three.
877 for j := i - 1; j >= 0; j-- {
878 if isWhitespace(p.initialTypes[j]) { // including format codes
879 result[j] = p.embeddingLevel
880 } else {
881 break
882 }
883 }
884 }
885 }
886
887 // Rule L1, clause four.
888 start := 0
889 for _, limit := range linebreaks {
890 for j := limit - 1; j >= start; j-- {
891 if isWhitespace(p.initialTypes[j]) { // including format codes
892 result[j] = p.embeddingLevel
893 } else {
894 break
895 }
896 }
897 start = limit
898 }
899
900 return result
901}
902
903// getReordering returns the reordering of lines from a visual index to a
904// logical index for line breaks at the given offsets.
905//
906// Lines are concatenated from left to right. So for example, the fifth
907// character from the left on the third line is
908//
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500909// getReordering(linebreaks)[linebreaks[1] + 4]
khenaidoo5fc5cea2021-08-11 17:39:16 -0400910//
911// (linebreaks[1] is the position after the last character of the second
912// line, which is also the index of the first character on the third line,
913// and adding four gets the fifth character from the left).
914//
915// The linebreaks array must include at least one value. The values must be
916// in strictly increasing order (no duplicates) between 1 and the length of
917// the text, inclusive. The last value must be the length of the text.
918func (p *paragraph) getReordering(linebreaks []int) []int {
919 validateLineBreaks(linebreaks, p.Len())
920
921 return computeMultilineReordering(p.getLevels(linebreaks), linebreaks)
922}
923
924// Return multiline reordering array for a given level array. Reordering
925// does not occur across a line break.
926func computeMultilineReordering(levels []level, linebreaks []int) []int {
927 result := make([]int, len(levels))
928
929 start := 0
930 for _, limit := range linebreaks {
931 tempLevels := make([]level, limit-start)
932 copy(tempLevels, levels[start:])
933
934 for j, order := range computeReordering(tempLevels) {
935 result[start+j] = order + start
936 }
937 start = limit
938 }
939 return result
940}
941
942// Return reordering array for a given level array. This reorders a single
943// line. The reordering is a visual to logical map. For example, the
944// leftmost char is string.charAt(order[0]). Rule L2.
945func computeReordering(levels []level) []int {
946 result := make([]int, len(levels))
947 // initialize order
948 for i := range result {
949 result[i] = i
950 }
951
952 // locate highest level found on line.
953 // Note the rules say text, but no reordering across line bounds is
954 // performed, so this is sufficient.
955 highestLevel := level(0)
956 lowestOddLevel := level(maxDepth + 2)
957 for _, level := range levels {
958 if level > highestLevel {
959 highestLevel = level
960 }
961 if level&1 != 0 && level < lowestOddLevel {
962 lowestOddLevel = level
963 }
964 }
965
966 for level := highestLevel; level >= lowestOddLevel; level-- {
967 for i := 0; i < len(levels); i++ {
968 if levels[i] >= level {
969 // find range of text at or above this level
970 start := i
971 limit := i + 1
972 for limit < len(levels) && levels[limit] >= level {
973 limit++
974 }
975
976 for j, k := start, limit-1; j < k; j, k = j+1, k-1 {
977 result[j], result[k] = result[k], result[j]
978 }
979 // skip to end of level run
980 i = limit
981 }
982 }
983 }
984
985 return result
986}
987
988// isWhitespace reports whether the type is considered a whitespace type for the
989// line break rules.
990func isWhitespace(c Class) bool {
991 switch c {
992 case LRE, RLE, LRO, RLO, PDF, LRI, RLI, FSI, PDI, BN, WS:
993 return true
994 }
995 return false
996}
997
998// isRemovedByX9 reports whether the type is one of the types removed in X9.
999func isRemovedByX9(c Class) bool {
1000 switch c {
1001 case LRE, RLE, LRO, RLO, PDF, BN:
1002 return true
1003 }
1004 return false
1005}
1006
1007// typeForLevel reports the strong type (L or R) corresponding to the level.
1008func typeForLevel(level level) Class {
1009 if (level & 0x1) == 0 {
1010 return L
1011 }
1012 return R
1013}
1014
1015func validateTypes(types []Class) error {
1016 if len(types) == 0 {
1017 return fmt.Errorf("types is null")
1018 }
1019 for i, t := range types[:len(types)-1] {
1020 if t == B {
1021 return fmt.Errorf("B type before end of paragraph at index: %d", i)
1022 }
1023 }
1024 return nil
1025}
1026
1027func validateParagraphEmbeddingLevel(embeddingLevel level) error {
1028 if embeddingLevel != implicitLevel &&
1029 embeddingLevel != 0 &&
1030 embeddingLevel != 1 {
1031 return fmt.Errorf("illegal paragraph embedding level: %d", embeddingLevel)
1032 }
1033 return nil
1034}
1035
1036func validateLineBreaks(linebreaks []int, textLength int) error {
1037 prev := 0
1038 for i, next := range linebreaks {
1039 if next <= prev {
1040 return fmt.Errorf("bad linebreak: %d at index: %d", next, i)
1041 }
1042 prev = next
1043 }
1044 if prev != textLength {
1045 return fmt.Errorf("last linebreak was %d, want %d", prev, textLength)
1046 }
1047 return nil
1048}
1049
1050func validatePbTypes(pairTypes []bracketType) error {
1051 if len(pairTypes) == 0 {
1052 return fmt.Errorf("pairTypes is null")
1053 }
1054 for i, pt := range pairTypes {
1055 switch pt {
1056 case bpNone, bpOpen, bpClose:
1057 default:
1058 return fmt.Errorf("illegal pairType value at %d: %v", i, pairTypes[i])
1059 }
1060 }
1061 return nil
1062}
1063
1064func validatePbValues(pairValues []rune, pairTypes []bracketType) error {
1065 if pairValues == nil {
1066 return fmt.Errorf("pairValues is null")
1067 }
1068 if len(pairTypes) != len(pairValues) {
1069 return fmt.Errorf("pairTypes is different length from pairValues")
1070 }
1071 return nil
1072}