1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
19import android.text.Layout.Directions;
20
21/**
22 * Access the ICU bidi implementation.
23 * @hide
24 */
25/* package */ class AndroidBidi {
26
27    public static int bidi(int dir, char[] chs, byte[] chInfo, int n, boolean haveInfo) {
28        if (chs == null || chInfo == null) {
29            throw new NullPointerException();
30        }
31
32        if (n < 0 || chs.length < n || chInfo.length < n) {
33            throw new IndexOutOfBoundsException();
34        }
35
36        switch(dir) {
37            case Layout.DIR_REQUEST_LTR: dir = 0; break;
38            case Layout.DIR_REQUEST_RTL: dir = 1; break;
39            case Layout.DIR_REQUEST_DEFAULT_LTR: dir = -2; break;
40            case Layout.DIR_REQUEST_DEFAULT_RTL: dir = -1; break;
41            default: dir = 0; break;
42        }
43
44        int result = runBidi(dir, chs, chInfo, n, haveInfo);
45        result = (result & 0x1) == 0 ? Layout.DIR_LEFT_TO_RIGHT : Layout.DIR_RIGHT_TO_LEFT;
46        return result;
47    }
48
49    /**
50     * Returns run direction information for a line within a paragraph.
51     *
52     * @param dir base line direction, either Layout.DIR_LEFT_TO_RIGHT or
53     *     Layout.DIR_RIGHT_TO_LEFT
54     * @param levels levels as returned from {@link #bidi}
55     * @param lstart start of the line in the levels array
56     * @param chars the character array (used to determine whitespace)
57     * @param cstart the start of the line in the chars array
58     * @param len the length of the line
59     * @return the directions
60     */
61    public static Directions directions(int dir, byte[] levels, int lstart,
62            char[] chars, int cstart, int len) {
63
64        int baseLevel = dir == Layout.DIR_LEFT_TO_RIGHT ? 0 : 1;
65        int curLevel = levels[lstart];
66        int minLevel = curLevel;
67        int runCount = 1;
68        for (int i = lstart + 1, e = lstart + len; i < e; ++i) {
69            int level = levels[i];
70            if (level != curLevel) {
71                curLevel = level;
72                ++runCount;
73            }
74        }
75
76        // add final run for trailing counter-directional whitespace
77        int visLen = len;
78        if ((curLevel & 1) != (baseLevel & 1)) {
79            // look for visible end
80            while (--visLen >= 0) {
81                char ch = chars[cstart + visLen];
82
83                if (ch == '\n') {
84                    --visLen;
85                    break;
86                }
87
88                if (ch != ' ' && ch != '\t') {
89                    break;
90                }
91            }
92            ++visLen;
93            if (visLen != len) {
94                ++runCount;
95            }
96        }
97
98        if (runCount == 1 && minLevel == baseLevel) {
99            // we're done, only one run on this line
100            if ((minLevel & 1) != 0) {
101                return Layout.DIRS_ALL_RIGHT_TO_LEFT;
102            }
103            return Layout.DIRS_ALL_LEFT_TO_RIGHT;
104        }
105
106        int[] ld = new int[runCount * 2];
107        int maxLevel = minLevel;
108        int levelBits = minLevel << Layout.RUN_LEVEL_SHIFT;
109        {
110            // Start of first pair is always 0, we write
111            // length then start at each new run, and the
112            // last run length after we're done.
113            int n = 1;
114            int prev = lstart;
115            curLevel = minLevel;
116            for (int i = lstart, e = lstart + visLen; i < e; ++i) {
117                int level = levels[i];
118                if (level != curLevel) {
119                    curLevel = level;
120                    if (level > maxLevel) {
121                        maxLevel = level;
122                    } else if (level < minLevel) {
123                        minLevel = level;
124                    }
125                    // XXX ignore run length limit of 2^RUN_LEVEL_SHIFT
126                    ld[n++] = (i - prev) | levelBits;
127                    ld[n++] = i - lstart;
128                    levelBits = curLevel << Layout.RUN_LEVEL_SHIFT;
129                    prev = i;
130                }
131            }
132            ld[n] = (lstart + visLen - prev) | levelBits;
133            if (visLen < len) {
134                ld[++n] = visLen;
135                ld[++n] = (len - visLen) | (baseLevel << Layout.RUN_LEVEL_SHIFT);
136            }
137        }
138
139        // See if we need to swap any runs.
140        // If the min level run direction doesn't match the base
141        // direction, we always need to swap (at this point
142        // we have more than one run).
143        // Otherwise, we don't need to swap the lowest level.
144        // Since there are no logically adjacent runs at the same
145        // level, if the max level is the same as the (new) min
146        // level, we have a series of alternating levels that
147        // is already in order, so there's no more to do.
148        //
149        boolean swap;
150        if ((minLevel & 1) == baseLevel) {
151            minLevel += 1;
152            swap = maxLevel > minLevel;
153        } else {
154            swap = runCount > 1;
155        }
156        if (swap) {
157            for (int level = maxLevel - 1; level >= minLevel; --level) {
158                for (int i = 0; i < ld.length; i += 2) {
159                    if (levels[ld[i]] >= level) {
160                        int e = i + 2;
161                        while (e < ld.length && levels[ld[e]] >= level) {
162                            e += 2;
163                        }
164                        for (int low = i, hi = e - 2; low < hi; low += 2, hi -= 2) {
165                            int x = ld[low]; ld[low] = ld[hi]; ld[hi] = x;
166                            x = ld[low+1]; ld[low+1] = ld[hi+1]; ld[hi+1] = x;
167                        }
168                        i = e + 2;
169                    }
170                }
171            }
172        }
173        return new Directions(ld);
174    }
175
176    private native static int runBidi(int dir, char[] chs, byte[] chInfo, int n, boolean haveInfo);
177}