jdk/src/java.desktop/share/classes/sun/font/BidiUtils.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright IBM Corp. 1999-2000 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The original version of this source code and documentation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * copyrighted and owned by IBM. These materials are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * under terms of a License Agreement between IBM and Sun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * patents. This notice and attribution to IBM may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.text.Bidi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public final class BidiUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Return the level of each character into the levels array starting at start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * This is a convenience method for clients who prefer to use an explicit levels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * array instead of iterating over the runs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param levels the array to receive the character levels
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    50
     * @param start the starting offset into the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @throws IndexOutOfBoundsException if <code>start</code> is less than 0 or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * <code>start + getLength()</code> is greater than <code>levels.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void getLevels(Bidi bidi, byte[] levels, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int limit = start + bidi.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (start < 0 || limit > levels.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new IndexOutOfBoundsException("levels.length = " + levels.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                " start: " + start + " limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int runCount = bidi.getRunCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int i = 0; i < runCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            int rlimit = start + bidi.getRunLimit(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            byte rlevel = (byte)bidi.getRunLevel(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            while (p < rlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                levels[p++] = rlevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Return an array containing the resolved bidi level of each character, in logical order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return an array containing the level of each character, in logical order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static byte[] getLevels(Bidi bidi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        byte[] levels = new byte[bidi.getLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        getLevels(bidi, levels, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return levels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static final char NUMLEVELS = 62;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Given level data, compute a a visual to logical mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The leftmost (or topmost) character is at visual index zero.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * logical index of the character is derived from the visual index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * by the expression <code>li = map[vi];</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param levels the levels array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return the mapping array from visual to logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static int[] createVisualToLogicalMap(byte[] levels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int len = levels.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int[] mapping = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        byte lowestOddLevel = (byte)(NUMLEVELS + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        byte highestLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // initialize mapping and levels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            mapping[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            byte level = levels[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (level > highestLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                highestLevel = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if ((level & 0x01) != 0 && level < lowestOddLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                lowestOddLevel = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        while (highestLevel >= lowestOddLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                while (i < len && levels[i] < highestLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                int begin = i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (begin == levels.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    break; // no more runs at this level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                while (i < len && levels[i] >= highestLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                int end = i - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                while (begin < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    int temp = mapping[begin];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    mapping[begin] = mapping[end];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    mapping[end] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    ++begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            --highestLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return mapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Return the inverse position map.  The source array must map one-to-one (each value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * is distinct and the values run from zero to the length of the array minus one).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * For example, if <code>values[i] = j</code>, then <code>inverse[j] = i</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param values the source ordering array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return the inverse array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static int[] createInverseMap(int[] values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int[] result = new int[values.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0; i < values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            result[values[i]] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Return an array containing contiguous values from 0 to length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * having the same ordering as the source array. If this would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * a canonical ltr ordering, return null.  The data in values[] is NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * required to be a permutation, but elements in values are required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * to be distinct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param values an array containing the discontiguous values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return the contiguous values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static int[] createContiguousOrder(int[] values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (values != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return computeContiguousOrder(values, 0, values.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Compute a contiguous order for the range start, limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static int[] computeContiguousOrder(int[] values, int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int[] result = new int[limit-start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (int i=0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            result[i] = i + start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // now we'll sort result[], with the following comparison:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // result[i] lessthan result[j] iff values[result[i]] < values[result[j]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // selection sort for now;  use more elaborate sorts if desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i=0; i < result.length-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int minIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            int currentValue = values[result[minIndex]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            for (int j=i; j < result.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (values[result[j]] < currentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    minIndex = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    currentValue = values[result[minIndex]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            int temp = result[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            result[i] = result[minIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            result[minIndex] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // shift result by start:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (start != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            for (int i=0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                result[i] -= start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // next, check for canonical order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (k=0; k < result.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (result[k] != k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (k == result.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // now return inverse of result:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return createInverseMap(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Return an array containing the data in the values array from start up to limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * normalized to fall within the range from 0 up to limit - start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * If this would be a canonical ltr ordering, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * NOTE: This method assumes that values[] is a logical to visual map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * generated from levels[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param values the source mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param levels the levels corresponding to the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param start the starting offset in the values and levels arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param limit the limiting offset in the values and levels arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return the normlized map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public static int[] createNormalizedMap(int[] values, byte[] levels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                           int start, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (values != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (start != 0 || limit != values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                // levels optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                boolean copyRange, canonical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                byte primaryLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (levels == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    primaryLevel = (byte) 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    copyRange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    canonical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    if (levels[start] == levels[limit-1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        primaryLevel = levels[start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        canonical = (primaryLevel & (byte)0x1) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        // scan for levels below primary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        for (i=start; i < limit; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            if (levels[i] < primaryLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            if (canonical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                canonical = levels[i] == primaryLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        copyRange = (i == limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        copyRange = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        // these don't matter;  but the compiler cares:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        primaryLevel = (byte) 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        canonical = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (copyRange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    if (canonical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    int[] result = new int[limit-start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    int baseValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    if ((primaryLevel & (byte)0x1) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        baseValue = values[limit-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        baseValue = values[start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    if (baseValue == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        System.arraycopy(values, start, result, 0, limit-start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        for (int j=0; j < result.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                            result[j] = values[j+start] - baseValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    return computeContiguousOrder(values, start, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Reorder the objects in the array into visual order based on their levels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * This is a utility function to use when you have a collection of objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * representing runs of text in logical order, each run containing text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * at a single level.  The elements in the objects array will be reordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * into visual order assuming each run of text has the level provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * by the corresponding element in the levels array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param levels an array representing the bidi level of each object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param objects the array of objects to be reordered into visual order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static void reorderVisually(byte[] levels, Object[] objects) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int len = levels.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        byte lowestOddLevel = (byte)(NUMLEVELS + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        byte highestLevel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // initialize mapping and levels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            byte level = levels[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (level > highestLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                highestLevel = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if ((level & 0x01) != 0 && level < lowestOddLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                lowestOddLevel = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        while (highestLevel >= lowestOddLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                while (i < len && levels[i] < highestLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                int begin = i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                if (begin == levels.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    break; // no more runs at this level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                while (i < len && levels[i] >= highestLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                int end = i - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                while (begin < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    Object temp = objects[begin];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    objects[begin] = objects[end];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    objects[end] = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    ++begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    --end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            --highestLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}