aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/complexscripts/bidi/UnicodeBidiAlgorithm.java
blob: 77cc6de134a71f4d3cb6566848ac4d4e4cb3b680 (plain)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.complexscripts.bidi;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.fop.traits.Direction;
import org.apache.fop.util.CharUtilities;

// CSOFF: AvoidNestedBlocksCheck
// CSOFF: EmptyForIteratorPadCheck
// CSOFF: InnerAssignmentCheck
// CSOFF: LineLengthCheck
// CSOFF: NoWhitespaceAfterCheck
// CSOFF: ParameterNumberCheck

/**
 * <p>The <code>UnicodeBidiAlgorithm</code> class implements functionality prescribed by
 * the Unicode Bidirectional Algorithm, Unicode Standard Annex #9.</p>
 *
 * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
 */
public final class UnicodeBidiAlgorithm implements BidiConstants {

    /**
     * logging instance
     */
    private static final Log log = LogFactory.getLog(BidiResolver.class);                                                   // CSOK: ConstantNameCheck

    private UnicodeBidiAlgorithm() {
    }

    /**
     * Resolve the directionality levels of each character in a character seqeunce.
     * If some character is encoded in the character sequence as a Unicode Surrogate Pair,
     * then the directionality level of each of the two members of the  pair will be identical.
     * @return null if bidirectional processing is not required; otherwise, returns an array
     * of integers, where each integer corresponds to exactly one UTF-16
     * encoding element present in the input character sequence, and where each integer denotes
     * the directionality level of the corresponding encoding element
     * @param cs input character sequence representing a UTF-16 encoded string
     * @param defaultLevel the default paragraph level, which must be zero (LR) or one (RL)
     */
    public static int[] resolveLevels(CharSequence cs, Direction defaultLevel) {
        int[] chars = new int [ cs.length() ];
        if (convertToScalar(cs, chars) || (defaultLevel == Direction.RL)) {
            return resolveLevels(chars, (defaultLevel == Direction.RL) ? 1 : 0, new int [ chars.length ]);
        } else {
            return null;
        }
    }

    /**
     * Resolve the directionality levels of each character in a character seqeunce.
     * @return null if bidirectional processing is not required; otherwise, returns an array
     * of integers, where each integer corresponds to exactly one UTF-16
     * encoding element present in the input character sequence, and where each integer denotes
     * the directionality level of the corresponding encoding element
     * @param chars array of input characters represented as unicode scalar values
     * @param defaultLevel the default paragraph level, which must be zero (LR) or one (RL)
     * @param levels array to receive levels, one for each character in chars array
     */
    public static int[] resolveLevels(int[] chars, int defaultLevel, int[] levels) {
        return resolveLevels(chars, getClasses(chars), defaultLevel, levels, false);
    }

    /**
     * Resolve the directionality levels of each character in a character seqeunce.
     * @return null if bidirectional processing is not required; otherwise, returns an array
     * of integers, where each integer corresponds to exactly one UTF-16
     * encoding element present in the input character sequence, and where each integer denotes
     * the directionality level of the corresponding encoding element
     * @param chars array of input characters represented as unicode scalar values
     * @param classes array containing one bidi class per character in chars array
     * @param defaultLevel the default paragraph level, which must be zero (LR) or one (RL)
     * @param levels array to receive levels, one for each character in chars array
     * @param useRuleL1 true if rule L1 should be used
     */
    public static int[] resolveLevels(int[] chars, int[] classes, int defaultLevel, int[] levels, boolean useRuleL1) {
        int[] ica = classes;
        int[] wca = copySequence(ica);
        int[] ea  = new int [ levels.length ];
        resolveExplicit(wca, defaultLevel, ea);
        resolveRuns(wca, defaultLevel, ea, levelsFromEmbeddings(ea, levels));
        if (useRuleL1) {
            resolveSeparators(ica, wca, defaultLevel, levels);
        }
        dump("RL: CC(" + ((chars != null) ? chars.length : -1) + ")", chars, classes, defaultLevel, levels);
        return levels;
    }

    private static int[] copySequence(int[] ta) {
        int[] na = new int [ ta.length ];
        System.arraycopy(ta, 0, na, 0, na.length);
        return na;
    }

    private static void resolveExplicit(int[] wca, int defaultLevel, int[] ea) {
        int[] es = new int [ MAX_LEVELS ];          /* embeddings stack */
        int ei = 0;                                 /* embeddings stack index */
        int ec = defaultLevel;                      /* current embedding level */
        for (int i = 0, n = wca.length; i < n; i++) {
            int bc = wca [ i ];                     /* bidi class of current char */
            int el;                                 /* embedding level to assign to current char */
            switch (bc) {
            case LRE:                               // start left-to-right embedding
            case RLE:                               // start right-to-left embedding
            case LRO:                               // start left-to-right override
            case RLO:                               // start right-to-left override
                {
                    int en;                         /* new embedding level */
                    if ((bc == RLE) || (bc == RLO)) {
                        en = ((ec & ~OVERRIDE) + 1) | 1;
                    } else {
                        en = ((ec & ~OVERRIDE) + 2) & ~1;
                    }
                    if (en < (MAX_LEVELS + 1)) {
                        es [ ei++ ] = ec;
                        if ((bc == LRO) || (bc == RLO)) {
                            ec = en | OVERRIDE;
                        } else {
                            ec = en & ~OVERRIDE;
                        }
                    } else {
                        // max levels exceeded, so don't change level or override
                    }
                    el = ec;
                    break;
                }
            case PDF:                               // pop directional formatting
                {
                    el = ec;
                    if (ei > 0) {
                        ec = es [ --ei ];
                    } else {
                        // ignore isolated PDF
                    }
                    break;
                }
            case B:                                 // paragraph separator
                {
                    el = ec = defaultLevel;
                    ei = 0;
                    break;
                }
            default:
                {
                    el = ec;
                    break;
                }
            }
            switch (bc) {
            case BN:
                break;
            case LRE: case RLE: case LRO: case RLO: case PDF:
                wca [ i ] = BN;
                break;
            default:
                if ((el & OVERRIDE) != 0) {
                    wca [ i ] = directionOfLevel(el);
                }
                break;
            }
            ea [ i ] = el;
        }
    }

    private static int directionOfLevel(int level) {
        return ((level & 1) != 0) ? R : L;
    }

    private static int levelOfEmbedding(int embedding) {
        return embedding & ~OVERRIDE;
    }

    private static int[] levelsFromEmbeddings(int[] ea, int[] la) {
        assert ea != null;
        assert la != null;
        assert la.length == ea.length;
        for (int i = 0, n = la.length; i < n; i++) {
            la [ i ] = levelOfEmbedding(ea [ i ]);
        }
        return la;
    }

    private static void resolveRuns(int[] wca, int defaultLevel, int[] ea, int[] la) {
        if (la.length != wca.length) {
            throw new IllegalArgumentException("levels sequence length must match classes sequence length");
        } else if (la.length != ea.length) {
            throw new IllegalArgumentException("levels sequence length must match embeddings sequence length");
        } else {
            for (int i = 0, n = ea.length, lPrev = defaultLevel; i < n; ) {
                int s = i;
                int e = s;
                int l = findNextNonRetainedFormattingLevel(wca, ea, s, lPrev);
                while (e < n) {
                    if (la [ e ] != l) {
                        if (startsWithRetainedFormattingRun(wca, ea, e)) {
                            e += getLevelRunLength(ea, e);
                        } else {
                            break;
                        }
                    } else {
                        e++;
                    }
                }
                lPrev = resolveRun(wca, defaultLevel, ea, la, s, e, l, lPrev);
                i = e;
            }
        }
    }

    private static int findNextNonRetainedFormattingLevel(int[] wca, int[] ea, int start, int lPrev) {
        int s = start;
        int e = wca.length;
        while (s < e) {
            if (startsWithRetainedFormattingRun(wca, ea, s)) {
                s += getLevelRunLength(ea, s);
            } else {
                break;
            }
        }
        if (s < e) {
            return levelOfEmbedding(ea [ s ]);
        } else {
            return lPrev;
        }
    }

    private static int getLevelRunLength(int[] ea, int start) {
        assert start < ea.length;
        int nl = 0;
        for (int s = start, e = ea.length, l0 = levelOfEmbedding(ea [ start ]); s < e; s++) {
            if (levelOfEmbedding(ea [ s ]) == l0) {
                nl++;
            } else {
                break;
            }
        }
        return nl;
    }

    private static boolean startsWithRetainedFormattingRun(int[] wca, int[] ea, int start) {
        int nl = getLevelRunLength(ea, start);
        if (nl > 0) {
            int nc = getRetainedFormattingRunLength(wca, start);
            return (nc >= nl);
        } else {
            return false;
        }
    }

    private static int getRetainedFormattingRunLength(int[] wca, int start) {
        assert start < wca.length;
        int nc = 0;
        for (int s = start, e = wca.length; s < e; s++) {
            if (wca [ s ] == BidiConstants.BN) {
                nc++;
            } else {
                break;
            }
        }
        return nc;
    }

    private static int resolveRun(int[] wca, int defaultLevel, int[] ea, int[] la, int start, int end, int level, int levelPrev) {

        // determine start of run direction
        int sor = directionOfLevel(max(levelPrev, level));

        // determine end of run direction
        int le = -1;
        if (end == wca.length) {
            le = max(level, defaultLevel);
        } else {
            for (int i = end; i < wca.length; i++) {
                if (wca [ i ] != BidiConstants.BN) {
                    le = max(level, la [ i ]);
                    break;
                }
            }
            if (le < 0) {
                le = max(level, defaultLevel);
            }
        }
        int eor = directionOfLevel(le);

        if (log.isDebugEnabled()) {
            log.debug("BR[" + padLeft(start, 3) + "," + padLeft(end, 3) + "] :" + padLeft(level, 2) + ": SOR(" + getClassName(sor) + "), EOR(" + getClassName(eor) + ")");
        }

        resolveWeak(wca, defaultLevel, ea, la, start, end, level, sor, eor);
        resolveNeutrals(wca, defaultLevel, ea, la, start, end, level, sor, eor);
        resolveImplicit(wca, defaultLevel, ea, la, start, end, level, sor, eor);

        // if this run is all retained formatting, then return prior level, otherwise this run's level
        return isRetainedFormatting(wca, start, end) ? levelPrev : level;
    }

    private static void resolveWeak(int[] wca, int defaultLevel, int[] ea, int[] la, int start, int end, int level, int sor, int eor) {

        // W1 - X BN* NSM -> X BN* X
        for (int i = start, n = end, bcPrev = sor; i < n; i++) {
            int bc = wca [ i ];
            if (bc == NSM) {
                wca [ i ] = bcPrev;
            } else if (bc != BN) {
                bcPrev = bc;
            }
        }

        // W2 - AL ... EN -> AL ... AN
        for (int i = start, n = end, bcPrev = sor; i < n; i++) {
            int bc = wca [ i ];
            if (bc == EN) {
                if (bcPrev == AL) {
                    wca [ i ] = AN;
                }
            } else if (isStrong(bc)) {
                bcPrev = bc;
            }
        }

        // W3 - AL -> R
        for (int i = start, n = end; i < n; i++) {
            int bc = wca [ i ];
            if (bc == AL) {
                wca [ i ] = R;
            }
        }

        // W4 - EN BN* ES BN* EN -> EN BN* EN BN* EN; XN BN* CS BN* XN -> XN BN* XN BN* XN
        for (int i = start, n = end, bcPrev = sor; i < n; i++) {
            int bc = wca [ i ];
            if (bc == ES) {
                int bcNext = eor;
                for (int j = i + 1; j < n; j++) {
                    if ((bc = wca [ j ]) != BN) {
                        bcNext = bc;
                        break;
                    }
                }
                if ((bcPrev == EN) && (bcNext == EN)) {
                    wca [ i ] = EN;
                }
            } else if (bc == CS) {
                int bcNext = eor;
                for (int j = i + 1; j < n; j++) {
                    if ((bc = wca [ j ]) != BN) {
                        bcNext = bc;
                        break;
                    }
                }
                if ((bcPrev == EN) && (bcNext == EN)) {
                    wca [ i ] = EN;
                } else if ((bcPrev == AN) && (bcNext == AN)) {
                    wca [ i ] = AN;
                }
            }
            if (bc != BN) {
                bcPrev = bc;
            }
        }

        // W5 - EN (ET|BN)* -> EN (EN|BN)*; (ET|BN)* EN -> (EN|BN)* EN
        for (int i = start, n = end, bcPrev = sor; i < n; i++) {
            int bc = wca [ i ];
            if (bc == ET) {
                int bcNext = eor;
                for (int j = i + 1; j < n; j++) {
                    bc = wca [ j ];
                    if ((bc != BN) && (bc != ET)) {
                        bcNext = bc;
                        break;
                    }
                }
                if ((bcPrev == EN) || (bcNext == EN)) {
                    wca [ i ] = EN;
                }
            } else if ((bc != BN) && (bc != ET)) {
                bcPrev = bc;
            }
        }

        // W6 - BN* (ET|ES|CS) BN* -> ON* ON ON*
        for (int i = start, n = end; i < n; i++) {
            int bc = wca [ i ];
            if ((bc == ET) || (bc == ES) || (bc == CS)) {
                wca [ i ] = ON;
                resolveAdjacentBoundaryNeutrals(wca, start, end, i, ON);
            }
        }

        // W7 - L ... EN -> L ... L
        for (int i = start, n = end, bcPrev = sor; i < n; i++) {
            int bc = wca [ i ];
            if (bc == EN) {
                if (bcPrev == L) {
                    wca [ i ] = L;
                }
            } else if ((bc == L) || (bc == R)) {
                bcPrev = bc;
            }
        }

    }

    private static void resolveNeutrals(int[] wca, int defaultLevel, int[] ea, int[] la, int start, int end, int level, int sor, int eor) {

        // N1 - (L|R) N+ (L|R) -> L L+ L | R R+ R; (AN|EN) N+ R -> (AN|EN) R+ R; R N+ (AN|EN) -> R R+ (AN|EN)
        for (int i = start, n = end, bcPrev = sor; i < n; i++) {
            int bc = wca [ i ];
            if (isNeutral(bc)) {
                int bcNext = eor;
                for (int j = i + 1; j < n; j++) {
                    bc = wca [ j ];
                    if ((bc == L) || (bc == R)) {
                        bcNext = bc;
                        break;
                    } else if ((bc == AN) || (bc == EN)) {
                        bcNext = R;
                        break;
                    } else if (isNeutral(bc)) {
                        continue;
                    } else if (isRetainedFormatting(bc)) {
                        continue;
                    } else {
                        break;
                    }
                }
                if (bcPrev == bcNext) {
                    wca [ i ] = bcPrev;
                    resolveAdjacentBoundaryNeutrals(wca, start, end, i, bcPrev);
                }
            } else if ((bc == L) || (bc == R)) {
                bcPrev = bc;
            } else if ((bc == AN) || (bc == EN)) {
                bcPrev = R;
            }
        }

        // N2 - N -> embedding level
        for (int i = start, n = end; i < n; i++) {
            int bc = wca [ i ];
            if (isNeutral(bc)) {
                int bcEmbedding = directionOfLevel(levelOfEmbedding(ea [ i ]));
                wca [ i ] = bcEmbedding;
                resolveAdjacentBoundaryNeutrals(wca, start, end, i, bcEmbedding);
            }
        }

    }

    private static void resolveAdjacentBoundaryNeutrals(int[] wca, int start, int end, int index, int bcNew) {
        if ((index < start) || (index >= end)) {
            throw new IllegalArgumentException();
        } else {
            for (int i = index - 1; i >= start; i--) {
                int bc = wca [ i ];
                if (bc == BN) {
                    wca [ i ] = bcNew;
                } else {
                    break;
                }
            }
            for (int i = index + 1; i < end; i++) {
                int bc = wca [ i ];
                if (bc == BN) {
                    wca [ i ] = bcNew;
                } else {
                    break;
                }
            }
        }
    }

    private static void resolveImplicit(int[] wca, int defaultLevel, int[] ea, int[] la, int start, int end, int level, int sor, int eor) {
        for (int i = start, n = end; i < n; i++) {
            int bc = wca [ i ];                     // bidi class
            int el = la [ i ];                      // embedding level
            int ed = 0;                             // embedding level delta
            if ((el & 1) == 0) {                // even
                if (bc == R) {
                    ed = 1;
                } else if (bc == AN) {
                    ed = 2;
                } else if (bc == EN) {
                    ed = 2;
                }
            } else {                                // odd
                if (bc == L) {
                    ed = 1;
                } else if (bc == EN) {
                    ed = 1;
                } else if (bc == AN) {
                    ed = 1;
                }
            }
            la [ i ] = el + ed;
        }
    }

    /**
     * Resolve separators and boundary neutral levels to account for UAX#9 3.4 L1 while taking into
     * account retention of formatting codes (5.2).
     * @param ica original input class array (sequence)
     * @param wca working copy of original intput class array (sequence), as modified by prior steps
     * @param dl default paragraph level
     * @param la array of output levels to be adjusted, as produced by bidi algorithm
     */
    private static void resolveSeparators(int[] ica, int[] wca, int dl, int[] la) {
        // steps (1) through (3)
        for (int i = 0, n = ica.length; i < n; i++) {
            int ic = ica[i];
            if ((ic == BidiConstants.S) || (ic == BidiConstants.B)) {
                la[i] = dl;
                for (int k = i - 1; k >= 0; k--) {
                    int pc = ica[k];
                    if (isRetainedFormatting(pc)) {
                        continue;
                    } else if (pc == BidiConstants.WS) {
                        la[k] = dl;
                    } else {
                        break;
                    }
                }
            }
        }
        // step (4) - consider end of input sequence to be end of line, but skip any trailing boundary neutrals and retained formatting codes
        for (int i = ica.length; i > 0; i--) {
            int k = i - 1;
            int ic = ica[k];
            if (isRetainedFormatting(ic)) {
                continue;
            } else if (ic == BidiConstants.WS) {
                la[k] = dl;
            } else {
                break;
            }
        }
        // step (5) - per section 5.2
        for (int i = 0, n = ica.length; i < n; i++) {
            int ic = ica[i];
            if (isRetainedFormatting(ic)) {
                if (i == 0) {
                    la[i] = dl;
                } else {
                    la[i] = la [ i - 1 ];
                }
            }
        }
    }

    private static boolean isStrong(int bc) {
        switch (bc) {
        case L:
        case R:
        case AL:
            return true;
        default:
            return false;
        }
    }

    private static boolean isNeutral(int bc) {
        switch (bc) {
        case WS:
        case ON:
        case S:
        case B:
            return true;
        default:
            return false;
        }
    }

    private static boolean isRetainedFormatting(int bc) {
        switch (bc) {
        case LRE:
        case LRO:
        case RLE:
        case RLO:
        case PDF:
        case BN:
            return true;
        default:
            return false;
        }
    }

    private static boolean isRetainedFormatting(int[] ca, int s, int e) {
        for (int i = s; i < e; i++) {
            if (! isRetainedFormatting(ca[i])) {
                return false;
            }
        }
        return true;
    }

    private static int max(int x, int y) {
        if (x > y) {
            return x;
        } else {
            return y;
        }
    }

    private static int[] getClasses(int[] chars) {
        int[] classes = new int [ chars.length ];
        int bc;
        for (int i = 0, n = chars.length; i < n; i++) {
            int ch = chars [ i ];
            if (ch >= 0) {
                bc = BidiClass.getBidiClass(chars [ i ]);
            } else {
                bc = SURROGATE;
            }
            classes [ i ] = bc;
        }
        return classes;
    }

    /**
     * Convert character sequence (a UTF-16 encoded string) to an array of unicode scalar values
     * expressed as integers. If a valid UTF-16 surrogate pair is encountered, it is converted to
     * two integers, the first being the equivalent unicode scalar  value, and the second being
     * negative one (-1). This special mechanism is used to track the use of surrogate pairs while
     * working with unicode scalar values, and permits maintaining indices that apply both to the
     * input UTF-16 and out scalar value sequences.
     * @return a boolean indicating that content is present that triggers bidirectional processing
     * @param cs a UTF-16 encoded character sequence
     * @param chars an integer array to accept the converted scalar values, where the length of the
     * array must be the same as the length of the input character sequence
     * @throws IllegalArgumentException if the input sequence is not a valid UTF-16 string, e.g.,
     * if it contains an isolated UTF-16 surrogate
     */
    private static boolean convertToScalar(CharSequence cs, int[] chars) throws IllegalArgumentException {
        boolean triggered = false;
        if (chars.length != cs.length()) {
            throw new IllegalArgumentException("characters array length must match input sequence length");
        }
        for (int i = 0, n = chars.length; i < n; ) {
            int chIn = cs.charAt(i);
            int chOut;
            if (chIn < 0xD800) {
                chOut = chIn;
            } else if (chIn < 0xDC00) {
                int chHi = chIn;
                int chLo;
                if ((i + 1) < n) {
                    chLo = cs.charAt(i + 1);
                    if ((chLo >= 0xDC00) && (chLo <= 0xDFFF)) {
                        chOut = convertToScalar(chHi, chLo);
                    } else {
                        throw new IllegalArgumentException("isolated high surrogate");
                    }
                } else {
                    throw new IllegalArgumentException("truncated surrogate pair");
                }
            } else if (chIn < 0xE000) {
                throw new IllegalArgumentException("isolated low surrogate");
            } else {
                chOut = chIn;
            }
            if (! triggered && triggersBidi(chOut)) {
                triggered = true;
            }
            if ((chOut & 0xFF0000) == 0) {
                chars [ i++ ] = chOut;
            } else {
                chars [ i++ ] = chOut;
                chars [ i++ ] = -1;
            }
        }
        return triggered;
    }

    /**
     * Convert UTF-16 surrogate pair to unicode scalar valuee.
     * @return a unicode scalar value
     * @param chHi high (most significant or first) surrogate
     * @param chLo low (least significant or second) surrogate
     * @throws IllegalArgumentException if one of the input surrogates is not valid
     */
    private static int convertToScalar(int chHi, int chLo) {
        if ((chHi < 0xD800) || (chHi > 0xDBFF)) {
            throw new IllegalArgumentException("bad high surrogate");
        } else if ((chLo < 0xDC00) || (chLo > 0xDFFF)) {
            throw new IllegalArgumentException("bad low surrogate");
        } else {
            return (((chHi & 0x03FF) << 10) | (chLo & 0x03FF)) + 0x10000;
        }
    }

    /**
     * Determine of character CH triggers bidirectional processing. Bidirectional
     * processing is deemed triggerable if CH is a strong right-to-left character,
     * an arabic letter or number, or is a right-to-left embedding or override
     * character.
     * @return true if character triggers bidirectional processing
     * @param ch a unicode scalar value
     */
    private static boolean triggersBidi(int ch) {
        switch (BidiClass.getBidiClass(ch)) {
        case R:
        case AL:
        case AN:
        case RLE:
        case RLO:
            return true;
        default:
            return false;
        }
    }

    private static void dump(String header, int[] chars, int[] classes, int defaultLevel, int[] levels) {
        log.debug(header);
        log.debug("BD: default level(" + defaultLevel + ")");
        StringBuffer sb = new StringBuffer();
        if (chars != null) {
            for (int i = 0, n = chars.length; i < n; i++) {
                int ch = chars [ i ];
                sb.setLength(0);
                if ((ch > 0x20) && (ch < 0x7F)) {
                    sb.append((char) ch);
                } else {
                    sb.append(CharUtilities.charToNCRef(ch));
                }
                for (int k = sb.length(); k < 12; k++) {
                    sb.append(' ');
                }
                sb.append(": " + padRight(getClassName(classes[i]), 4) + " " + levels[i]);
                log.debug(sb);
            }
        } else {
            for (int i = 0, n = classes.length; i < n; i++) {
                sb.setLength(0);
                for (int k = sb.length(); k < 12; k++) {
                    sb.append(' ');
                }
                sb.append(": " + padRight(getClassName(classes[i]), 4) + " " + levels[i]);
                log.debug(sb);
            }
        }
    }

    private static String getClassName(int bc) {
        switch (bc) {
        case L:                                     // left-to-right
            return "L";
        case LRE:                                   // left-to-right embedding
            return "LRE";
        case LRO:                                   // left-to-right override
            return "LRO";
        case R:                                     // right-to-left
            return "R";
        case AL:                                    // right-to-left arabic
            return "AL";
        case RLE:                                   // right-to-left embedding
            return "RLE";
        case RLO:                                   // right-to-left override
            return "RLO";
        case PDF:                                   // pop directional formatting
            return "PDF";
        case EN:                                    // european number
            return "EN";
        case ES:                                    // european number separator
            return "ES";
        case ET:                                    // european number terminator
            return "ET";
        case AN:                                    // arabic number
            return "AN";
        case CS:                                    // common number separator
            return "CS";
        case NSM:                                   // non-spacing mark
            return "NSM";
        case BN:                                    // boundary neutral
            return "BN";
        case B:                                     // paragraph separator
            return "B";
        case S:                                     // segment separator
            return "S";
        case WS:                                    // whitespace
            return "WS";
        case ON:                                    // other neutrals
            return "ON";
        case SURROGATE:                             // placeholder for low surrogate
            return "SUR";
        default:
            return "?";
        }
    }

    private static String padLeft(int n, int width) {
        return padLeft(Integer.toString(n), width);
    }

    private static String padLeft(String s, int width) {
        StringBuffer sb = new StringBuffer();
        for (int i = s.length(); i < width; i++) {
            sb.append(' ');
        }
        sb.append(s);
        return sb.toString();
    }

    /* not used yet
    private static String padRight ( int n, int width ) {
        return padRight ( Integer.toString ( n ), width );
    }
    */

    private static String padRight(String s, int width) {
        StringBuffer sb = new StringBuffer(s);
        for (int i = sb.length(); i < width; i++) {
            sb.append(' ');
        }
        return sb.toString();
    }

}