31 * under terms of a License Agreement between IBM and Sun.
32 * This technology is protected by multiple US and International
33 * patents. This notice and attribution to IBM may not be removed.
34 */
35
36 package sun.font;
37
38 import java.text.Bidi;
39
40 public final class BidiUtils {
41
42
43
44 /**
45 * Return the level of each character into the levels array starting at start.
46 * This is a convenience method for clients who prefer to use an explicit levels
47 * array instead of iterating over the runs.
48 *
49 * @param levels the array to receive the character levels
50 * @param start the starting offset into the array
51 * @throws IndexOutOfBoundsException if <code>start</code> is less than 0 or
52 * <code>start + getLength()</code> is greater than <code>levels.length</code>.
53 */
54 public static void getLevels(Bidi bidi, byte[] levels, int start) {
55 int limit = start + bidi.getLength();
56
57 if (start < 0 || limit > levels.length) {
58 throw new IndexOutOfBoundsException("levels.length = " + levels.length +
59 " start: " + start + " limit: " + limit);
60 }
61
62 int runCount = bidi.getRunCount();
63 int p = start;
64 for (int i = 0; i < runCount; ++i) {
65 int rlimit = start + bidi.getRunLimit(i);
66 byte rlevel = (byte)bidi.getRunLevel(i);
67
68 while (p < rlimit) {
69 levels[p++] = rlevel;
70 }
71 }
72 }
73
74 /**
75 * Return an array containing the resolved bidi level of each character, in logical order.
76 * @return an array containing the level of each character, in logical order.
77 */
78 public static byte[] getLevels(Bidi bidi) {
79 byte[] levels = new byte[bidi.getLength()];
80 getLevels(bidi, levels, 0);
81 return levels;
82 }
83
84 static final char NUMLEVELS = 62;
85
86 /**
87 * Given level data, compute a a visual to logical mapping.
88 * The leftmost (or topmost) character is at visual index zero. The
89 * logical index of the character is derived from the visual index
90 * by the expression <code>li = map[vi];</code>.
91 * @param levels the levels array
92 * @return the mapping array from visual to logical
93 */
94 public static int[] createVisualToLogicalMap(byte[] levels) {
95 int len = levels.length;
96 int[] mapping = new int[len];
97
98 byte lowestOddLevel = (byte)(NUMLEVELS + 1);
99 byte highestLevel = 0;
100
101 // initialize mapping and levels
102
103 for (int i = 0; i < len; i++) {
104 mapping[i] = i;
105
106 byte level = levels[i];
107 if (level > highestLevel) {
108 highestLevel = level;
109 }
110
131 int end = i - 1;
132
133 while (begin < end) {
134 int temp = mapping[begin];
135 mapping[begin] = mapping[end];
136 mapping[end] = temp;
137 ++begin;
138 --end;
139 }
140 }
141
142 --highestLevel;
143 }
144
145 return mapping;
146 }
147
148 /**
149 * Return the inverse position map. The source array must map one-to-one (each value
150 * is distinct and the values run from zero to the length of the array minus one).
151 * For example, if <code>values[i] = j</code>, then <code>inverse[j] = i</code>.
152 * @param values the source ordering array
153 * @return the inverse array
154 */
155 public static int[] createInverseMap(int[] values) {
156 if (values == null) {
157 return null;
158 }
159
160 int[] result = new int[values.length];
161 for (int i = 0; i < values.length; i++) {
162 result[values[i]] = i;
163 }
164
165 return result;
166 }
167
168
169 /**
170 * Return an array containing contiguous values from 0 to length
171 * having the same ordering as the source array. If this would be
|
31 * under terms of a License Agreement between IBM and Sun.
32 * This technology is protected by multiple US and International
33 * patents. This notice and attribution to IBM may not be removed.
34 */
35
36 package sun.font;
37
38 import java.text.Bidi;
39
40 public final class BidiUtils {
41
42
43
44 /**
45 * Return the level of each character into the levels array starting at start.
46 * This is a convenience method for clients who prefer to use an explicit levels
47 * array instead of iterating over the runs.
48 *
49 * @param levels the array to receive the character levels
50 * @param start the starting offset into the array
51 * @throws IndexOutOfBoundsException if {@code start} is less than 0 or
52 * {@code start + getLength()} is greater than {@code levels.length}.
53 */
54 public static void getLevels(Bidi bidi, byte[] levels, int start) {
55 int limit = start + bidi.getLength();
56
57 if (start < 0 || limit > levels.length) {
58 throw new IndexOutOfBoundsException("levels.length = " + levels.length +
59 " start: " + start + " limit: " + limit);
60 }
61
62 int runCount = bidi.getRunCount();
63 int p = start;
64 for (int i = 0; i < runCount; ++i) {
65 int rlimit = start + bidi.getRunLimit(i);
66 byte rlevel = (byte)bidi.getRunLevel(i);
67
68 while (p < rlimit) {
69 levels[p++] = rlevel;
70 }
71 }
72 }
73
74 /**
75 * Return an array containing the resolved bidi level of each character, in logical order.
76 * @return an array containing the level of each character, in logical order.
77 */
78 public static byte[] getLevels(Bidi bidi) {
79 byte[] levels = new byte[bidi.getLength()];
80 getLevels(bidi, levels, 0);
81 return levels;
82 }
83
84 static final char NUMLEVELS = 62;
85
86 /**
87 * Given level data, compute a a visual to logical mapping.
88 * The leftmost (or topmost) character is at visual index zero. The
89 * logical index of the character is derived from the visual index
90 * by the expression {@code li = map[vi];}.
91 * @param levels the levels array
92 * @return the mapping array from visual to logical
93 */
94 public static int[] createVisualToLogicalMap(byte[] levels) {
95 int len = levels.length;
96 int[] mapping = new int[len];
97
98 byte lowestOddLevel = (byte)(NUMLEVELS + 1);
99 byte highestLevel = 0;
100
101 // initialize mapping and levels
102
103 for (int i = 0; i < len; i++) {
104 mapping[i] = i;
105
106 byte level = levels[i];
107 if (level > highestLevel) {
108 highestLevel = level;
109 }
110
131 int end = i - 1;
132
133 while (begin < end) {
134 int temp = mapping[begin];
135 mapping[begin] = mapping[end];
136 mapping[end] = temp;
137 ++begin;
138 --end;
139 }
140 }
141
142 --highestLevel;
143 }
144
145 return mapping;
146 }
147
148 /**
149 * Return the inverse position map. The source array must map one-to-one (each value
150 * is distinct and the values run from zero to the length of the array minus one).
151 * For example, if {@code values[i] = j}, then {@code inverse[j] = i}.
152 * @param values the source ordering array
153 * @return the inverse array
154 */
155 public static int[] createInverseMap(int[] values) {
156 if (values == null) {
157 return null;
158 }
159
160 int[] result = new int[values.length];
161 for (int i = 0; i < values.length; i++) {
162 result[values[i]] = i;
163 }
164
165 return result;
166 }
167
168
169 /**
170 * Return an array containing contiguous values from 0 to length
171 * having the same ordering as the source array. If this would be
|