6941948: NumaricShaper.shape() doesn't work with NumericShaper.Range.EASTERN_ARABIC

Reviewed-by: peytoia
This commit is contained in:
Masayoshi Okutsu 2010-04-14 13:53:17 +09:00
parent 2790d3b818
commit 6cfd12db85
2 changed files with 184 additions and 45 deletions

View File

@ -129,6 +129,8 @@ public final class NumericShaper implements java.io.Serializable {
* @since 1.7 * @since 1.7
*/ */
public static enum Range { public static enum Range {
// The order of EUROPEAN to MOGOLIAN must be consistent
// with the bitmask-based constants.
/** /**
* The Latin (European) range with the Latin (ASCII) digits. * The Latin (European) range with the Latin (ASCII) digits.
*/ */
@ -210,6 +212,9 @@ public final class NumericShaper implements java.io.Serializable {
* The Mongolian range with the Mongolian digits. * The Mongolian range with the Mongolian digits.
*/ */
MONGOLIAN ('\u1810', '\u1800', '\u1900'), MONGOLIAN ('\u1810', '\u1800', '\u1900'),
// The order of EUROPEAN to MOGOLIAN must be consistent
// with the bitmask-based constants.
/** /**
* The N'Ko range with the N'Ko digits. * The N'Ko range with the N'Ko digits.
*/ */
@ -259,17 +264,6 @@ public final class NumericShaper implements java.io.Serializable {
*/ */
CHAM ('\uaa50', '\uaa00', '\uaa60'); CHAM ('\uaa50', '\uaa00', '\uaa60');
private static final Range[] ranges = Range.class.getEnumConstants();
static {
// sort ranges[] by base for binary search
Arrays.sort(ranges,
new Comparator<Range>() {
public int compare(Range s1, Range s2) {
return s1.base > s2.base ? 1 : s1.base == s2.base ? 0 : -1;
}
});
}
private static int toRangeIndex(Range script) { private static int toRangeIndex(Range script) {
int index = script.ordinal(); int index = script.ordinal();
return index < NUM_KEYS ? index : -1; return index < NUM_KEYS ? index : -1;
@ -346,11 +340,20 @@ public final class NumericShaper implements java.io.Serializable {
/** /**
* {@code Set<Range>} indicating which Unicode ranges to * {@code Set<Range>} indicating which Unicode ranges to
* shape. {@code null} for the bit mask-based API. * shape. {@code null} for the bit mask-based API.
*
* @since 1.7
*/ */
private transient Set<Range> rangeSet; private transient Set<Range> rangeSet;
/**
* rangeSet.toArray() value. Sorted by Range.base when the number
* of elements is greater then BSEARCH_THRESHOLD.
*/
private transient Range[] rangeArray;
/**
* If more than BSEARCH_THRESHOLD ranges are specified, binary search is used.
*/
private static final int BSEARCH_THRESHOLD = 3;
private static final long serialVersionUID = -8022764705923730308L; private static final long serialVersionUID = -8022764705923730308L;
/** Identifies the Latin-1 (European) and extended range, and /** Identifies the Latin-1 (European) and extended range, and
@ -513,25 +516,32 @@ public final class NumericShaper implements java.io.Serializable {
// cache for the NumericShaper.Range version // cache for the NumericShaper.Range version
private transient volatile Range currentRange = Range.EUROPEAN; private transient volatile Range currentRange = Range.EUROPEAN;
private Range rangeForCodePoint(int codepoint) { private Range rangeForCodePoint(final int codepoint) {
Range range = currentRange; if (currentRange.inRange(codepoint)) {
if (range.inRange(codepoint)) { return currentRange;
return range;
} }
final Range[] ranges = Range.ranges; final Range[] ranges = rangeArray;
int lo = 0; if (ranges.length > BSEARCH_THRESHOLD) {
int hi = ranges.length - 1; int lo = 0;
while (lo <= hi) { int hi = ranges.length - 1;
int mid = (lo + hi) / 2; while (lo <= hi) {
range = ranges[mid]; int mid = (lo + hi) / 2;
if (codepoint < range.start) { Range range = ranges[mid];
hi = mid - 1; if (codepoint < range.start) {
} else if (codepoint >= range.end) { hi = mid - 1;
lo = mid + 1; } else if (codepoint >= range.end) {
} else { lo = mid + 1;
currentRange = range; } else {
return range; currentRange = range;
return range;
}
}
} else {
for (int i = 0; i < ranges.length; i++) {
if (ranges[i].inRange(codepoint)) {
return ranges[i];
}
} }
} }
return Range.EUROPEAN; return Range.EUROPEAN;
@ -928,8 +938,25 @@ public final class NumericShaper implements java.io.Serializable {
} }
private NumericShaper(Range defaultContext, Set<Range> ranges) { private NumericShaper(Range defaultContext, Set<Range> ranges) {
this.shapingRange = defaultContext; shapingRange = defaultContext;
this.rangeSet = EnumSet.copyOf(ranges); // throws NPE if ranges is null. rangeSet = EnumSet.copyOf(ranges); // throws NPE if ranges is null.
// Give precedance to EASTERN_ARABIC if both ARABIC and
// EASTERN_ARABIC are specified.
if (rangeSet.contains(Range.EASTERN_ARABIC)
&& rangeSet.contains(Range.ARABIC)) {
rangeSet.remove(Range.ARABIC);
}
rangeArray = rangeSet.toArray(new Range[rangeSet.size()]);
if (rangeArray.length > BSEARCH_THRESHOLD) {
// sort rangeArray for binary search
Arrays.sort(rangeArray,
new Comparator<Range>() {
public int compare(Range s1, Range s2) {
return s1.base > s2.base ? 1 : s1.base == s2.base ? 0 : -1;
}
});
}
} }
/** /**
@ -1152,31 +1179,25 @@ public final class NumericShaper implements java.io.Serializable {
} }
private void shapeContextually(char[] text, int start, int count, Range ctxKey) { private void shapeContextually(char[] text, int start, int count, Range ctxKey) {
if (ctxKey == null) { // if we don't support the specified context, then don't shape.
if (ctxKey == null || !rangeSet.contains(ctxKey)) {
ctxKey = Range.EUROPEAN; ctxKey = Range.EUROPEAN;
} }
Range lastKey = ctxKey; Range lastKey = ctxKey;
int base = ctxKey.getDigitBase(); int base = ctxKey.getDigitBase();
char minDigit = (char)('0' + ctxKey.getNumericBase()); char minDigit = (char)('0' + ctxKey.getNumericBase());
for (int i = start, end = start + count; i < end; ++i) { final int end = start + count;
for (int i = start; i < end; ++i) {
char c = text[i]; char c = text[i];
if (c >= minDigit && c <= '9') { if (c >= minDigit && c <= '9') {
text[i] = (char)(c + base); text[i] = (char)(c + base);
continue; continue;
} }
if (isStrongDirectional(c)) { if (isStrongDirectional(c)) {
Range newKey = rangeForCodePoint(c); ctxKey = rangeForCodePoint(c);
if (newKey != lastKey) { if (ctxKey != lastKey) {
lastKey = newKey; lastKey = ctxKey;
ctxKey = newKey;
if (rangeSet.contains(Range.EUROPEAN)
&& (ctxKey == Range.ARABIC || ctxKey == Range.EASTERN_ARABIC)) {
ctxKey = Range.EASTERN_ARABIC;
} else if (!rangeSet.contains(ctxKey)) {
ctxKey = Range.EUROPEAN;
}
base = ctxKey.getDigitBase(); base = ctxKey.getDigitBase();
minDigit = (char)('0' + ctxKey.getNumericBase()); minDigit = (char)('0' + ctxKey.getNumericBase());
} }

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6941948
* @summary Make sure that EASTERN_ARABIC works with the enum interface.
*/
import java.awt.font.NumericShaper;
import java.util.EnumSet;
import static java.awt.font.NumericShaper.*;
public class EasternArabicTest {
static NumericShaper ns_old, ns_new;
static boolean err = false;
static String[][] testData = {
// Arabic "October 10"
{"\u0623\u0643\u062a\u0648\u0628\u0631 10",
"\u0623\u0643\u062a\u0648\u0628\u0631 \u06f1\u06f0"}, // EASTERN_ARABIC digits
// Tamil "Year 2009"
{"\u0b86\u0ba3\u0bcd\u0b9f\u0bc1 2009",
"\u0b86\u0ba3\u0bcd\u0b9f\u0bc1 \u0be8\u0be6\u0be6\u0bef"},
// "\u0be800\u0bef is returned by pre-JDK7 because Tamil zero was not
// included in Unicode 4.0.0.
// Ethiopic "Syllable<HA> 2009"
{"\u1200 2009",
"\u1200 \u136a00\u1371"},
// Ethiopic zero doesn't exist even in Unicode 5.1.0.
};
public static void main(String[] args) {
ns_old = getContextualShaper(TAMIL|ETHIOPIC|EASTERN_ARABIC|ARABIC|THAI|LAO,
EUROPEAN);
ns_new = getContextualShaper(EnumSet.of(Range.THAI,
Range.TAMIL,
Range.ETHIOPIC,
Range.EASTERN_ARABIC,
Range.ARABIC,
Range.LAO),
Range.EUROPEAN);
StringBuilder cData = new StringBuilder();
StringBuilder cExpected = new StringBuilder();
for (int i = 0; i < testData.length; i++) {
String data = testData[i][0];
String expected = testData[i][1];
test(data, expected);
cData.append(data).append(' ');
cExpected.append(expected).append(' ');
}
test(cData.toString(), cExpected.toString());
if (err) {
throw new RuntimeException("shape() returned unexpected value.");
}
}
private static void test(String data, String expected) {
char[] text = data.toCharArray();
ns_old.shape(text, 0, text.length);
String got = new String(text);
if (!expected.equals(got)) {
err = true;
System.err.println("Error with traditional range.");
System.err.println(" text = " + data);
System.err.println(" got = " + got);
System.err.println(" expected = " + expected);
} else {
System.err.println("OK with traditional range.");
System.err.println(" text = " + data);
System.err.println(" got = " + got);
System.err.println(" expected = " + expected);
}
text = data.toCharArray();
ns_new.shape(text, 0, text.length);
got = new String(text);
if (!expected.equals(got)) {
err = true;
System.err.println("Error with new Enum range.");
System.err.println(" text = " + data);
System.err.println(" got = " + got);
System.err.println(" expected = " + expected);
} else {
System.err.println("OK with new Enum range.");
System.err.println(" text = " + data);
System.err.println(" got = " + got);
System.err.println(" expected = " + expected);
}
}
}