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        if (len == 0) {
64            return Layout.DIRS_ALL_LEFT_TO_RIGHT;
65        }
66
67        int baseLevel = dir == Layout.DIR_LEFT_TO_RIGHT ? 0 : 1;
68        int curLevel = levels[lstart];
69        int minLevel = curLevel;
70        int runCount = 1;
71        for (int i = lstart + 1, e = lstart + len; i < e; ++i) {
72            int level = levels[i];
73            if (level != curLevel) {
74                curLevel = level;
75                ++runCount;
76            }
77        }
78
79        // add final run for trailing counter-directional whitespace
80        int visLen = len;
81        if ((curLevel & 1) != (baseLevel & 1)) {
82            // look for visible end
83            while (--visLen >= 0) {
84                char ch = chars[cstart + visLen];
85
86                if (ch == '\n') {
87                    --visLen;
88                    break;
89                }
90
91                if (ch != ' ' && ch != '\t') {
92                    break;
93                }
94            }
95            ++visLen;
96            if (visLen != len) {
97                ++runCount;
98            }
99        }
100
101        if (runCount == 1 && minLevel == baseLevel) {
102            // we're done, only one run on this line
103            if ((minLevel & 1) != 0) {
104                return Layout.DIRS_ALL_RIGHT_TO_LEFT;
105            }
106            return Layout.DIRS_ALL_LEFT_TO_RIGHT;
107        }
108
109        int[] ld = new int[runCount * 2];
110        int maxLevel = minLevel;
111        int levelBits = minLevel << Layout.RUN_LEVEL_SHIFT;
112        {
113            // Start of first pair is always 0, we write
114            // length then start at each new run, and the
115            // last run length after we're done.
116            int n = 1;
117            int prev = lstart;
118            curLevel = minLevel;
119            for (int i = lstart, e = lstart + visLen; i < e; ++i) {
120                int level = levels[i];
121                if (level != curLevel) {
122                    curLevel = level;
123                    if (level > maxLevel) {
124                        maxLevel = level;
125                    } else if (level < minLevel) {
126                        minLevel = level;
127                    }
128                    // XXX ignore run length limit of 2^RUN_LEVEL_SHIFT
129                    ld[n++] = (i - prev) | levelBits;
130                    ld[n++] = i - lstart;
131                    levelBits = curLevel << Layout.RUN_LEVEL_SHIFT;
132                    prev = i;
133                }
134            }
135            ld[n] = (lstart + visLen - prev) | levelBits;
136            if (visLen < len) {
137                ld[++n] = visLen;
138                ld[++n] = (len - visLen) | (baseLevel << Layout.RUN_LEVEL_SHIFT);
139            }
140        }
141
142        // See if we need to swap any runs.
143        // If the min level run direction doesn't match the base
144        // direction, we always need to swap (at this point
145        // we have more than one run).
146        // Otherwise, we don't need to swap the lowest level.
147        // Since there are no logically adjacent runs at the same
148        // level, if the max level is the same as the (new) min
149        // level, we have a series of alternating levels that
150        // is already in order, so there's no more to do.
151        //
152        boolean swap;
153        if ((minLevel & 1) == baseLevel) {
154            minLevel += 1;
155            swap = maxLevel > minLevel;
156        } else {
157            swap = runCount > 1;
158        }
159        if (swap) {
160            for (int level = maxLevel - 1; level >= minLevel; --level) {
161                for (int i = 0; i < ld.length; i += 2) {
162                    if (levels[ld[i]] >= level) {
163                        int e = i + 2;
164                        while (e < ld.length && levels[ld[e]] >= level) {
165                            e += 2;
166                        }
167                        for (int low = i, hi = e - 2; low < hi; low += 2, hi -= 2) {
168                            int x = ld[low]; ld[low] = ld[hi]; ld[hi] = x;
169                            x = ld[low+1]; ld[low+1] = ld[hi+1]; ld[hi+1] = x;
170                        }
171                        i = e + 2;
172                    }
173                }
174            }
175        }
176        return new Directions(ld);
177    }
178
179    private native static int runBidi(int dir, char[] chs, byte[] chInfo, int n, boolean haveInfo);
180}