NumericShaperpublic final class NumericShaper extends Object implements SerializableThe NumericShaper class is used to convert Latin-1 (European)
digits to other Unicode decimal digits. Users of this class will
primarily be people who wish to present data using
national digit shapes, but find it more convenient to represent the
data internally using Latin-1 (European) digits. This does not
interpret the deprecated numeric shape selector character (U+206E).
Instances of NumericShaper are typically applied
as attributes to text with the
{@link TextAttribute#NUMERIC_SHAPING NUMERIC_SHAPING} attribute
of the TextAttribute class.
For example, this code snippet causes a TextLayout to
shape European digits to Arabic in an Arabic context:
Map map = new HashMap();
map.put(TextAttribute.NUMERIC_SHAPING,
NumericShaper.getContextualShaper(NumericShaper.ARABIC));
FontRenderContext frc = ...;
TextLayout layout = new TextLayout(text, map, frc);
layout.draw(g2d, x, y);
It is also possible to perform numeric shaping explicitly using instances
of NumericShaper , as this code snippet demonstrates:
char[] text = ...;
// shape all EUROPEAN digits (except zero) to ARABIC digits
NumericShaper shaper = NumericShaper.getShaper(NumericShaper.ARABIC);
shaper.shape(text, start, count);
// shape European digits to ARABIC digits if preceeding text is Arabic, or
// shape European digits to TAMIL digits if preceeding text is Tamil, or
// leave European digits alone if there is no preceeding text, or
// preceeding text is neither Arabic nor Tamil
NumericShaper shaper =
NumericShaper.getContextualShaper(NumericShaper.ARABIC |
NumericShaper.TAMIL,
NumericShaper.EUROPEAN);
shaper.shape(text. start, count);
|
Fields Summary |
---|
private int | keyindex of context for contextual shaping - values range from 0 to 18 | private int | maskflag indicating whether to shape contextually (high bit) and which
digit ranges to shape (bits 0-18) | public static final int | EUROPEANIdentifies the Latin-1 (European) and extended range, and
Latin-1 (European) decimal base. | public static final int | ARABICIdentifies the ARABIC range and decimal base. | public static final int | EASTERN_ARABICIdentifies the ARABIC range and ARABIC_EXTENDED decimal base. | public static final int | DEVANAGARIIdentifies the DEVANAGARI range and decimal base. | public static final int | BENGALIIdentifies the BENGALI range and decimal base. | public static final int | GURMUKHIIdentifies the GURMUKHI range and decimal base. | public static final int | GUJARATIIdentifies the GUJARATI range and decimal base. | public static final int | ORIYAIdentifies the ORIYA range and decimal base. | public static final int | TAMILIdentifies the TAMIL range and decimal base. Tamil does not have a
decimal digit 0 so Latin-1 (European) 0 is used. | public static final int | TELUGUIdentifies the TELUGU range and decimal base. | public static final int | KANNADAIdentifies the KANNADA range and decimal base. | public static final int | MALAYALAMIdentifies the MALAYALAM range and decimal base. | public static final int | THAIIdentifies the THAI range and decimal base. | public static final int | LAOIdentifies the LAO range and decimal base. | public static final int | TIBETANIdentifies the TIBETAN range and decimal base. | public static final int | MYANMARIdentifies the MYANMAR range and decimal base. | public static final int | ETHIOPICIdentifies the ETHIOPIC range and decimal base. | public static final int | KHMERIdentifies the KHMER range and decimal base. | public static final int | MONGOLIANIdentifies the MONGOLIAN range and decimal base. | public static final int | ALL_RANGESIdentifies all ranges, for full contextual shaping. | private static final int | EUROPEAN_KEY | private static final int | ARABIC_KEY | private static final int | EASTERN_ARABIC_KEY | private static final int | DEVANAGARI_KEY | private static final int | BENGALI_KEY | private static final int | GURMUKHI_KEY | private static final int | GUJARATI_KEY | private static final int | ORIYA_KEY | private static final int | TAMIL_KEY | private static final int | TELUGU_KEY | private static final int | KANNADA_KEY | private static final int | MALAYALAM_KEY | private static final int | THAI_KEY | private static final int | LAO_KEY | private static final int | TIBETAN_KEY | private static final int | MYANMAR_KEY | private static final int | ETHIOPIC_KEY | private static final int | KHMER_KEY | private static final int | MONGOLIAN_KEY | private static final int | NUM_KEYS | private static final String[] | keyNames | private static final int | CONTEXTUAL_MASK | private static final char[] | bases | private static final char[] | contexts | private static int | ctCache | private static int | ctCacheLimit | private static char[] | strongTable | private static int | stCache |
Constructors Summary |
---|
private NumericShaper(int key, int mask)Private constructor.
this.key = key;
this.mask = mask;
|
Methods Summary |
---|
public boolean | equals(java.lang.Object o)Returns true if the specified object is an instance of
NumericShaper and shapes identically to this one.
if (o != null) {
try {
NumericShaper rhs = (NumericShaper)o;
return rhs.mask == mask && rhs.key == key;
}
catch (ClassCastException e) {
}
}
return false;
| private static int | getContextKey(char c)
// warning, synchronize access to this as it modifies state
if (c < contexts[ctCache]) {
while (ctCache > 0 && c < contexts[ctCache]) --ctCache;
} else if (c >= contexts[ctCache + 1]) {
while (ctCache < ctCacheLimit && c >= contexts[ctCache + 1]) ++ctCache;
}
// if we're not in a known range, then return EUROPEAN as the range key
return (ctCache & 0x1) == 0 ? (ctCache / 2) : EUROPEAN_KEY;
| public static java.awt.font.NumericShaper | getContextualShaper(int ranges)Returns a contextual shaper for the provided unicode range(s).
Latin-1 (EUROPEAN) digits are converted to the decimal digits
corresponding to the range of the preceeding text, if the
range is one of the provided ranges. Multiple ranges are
represented by or-ing the values together, such as,
NumericShaper.ARABIC | NumericShaper.THAI . The
shaper assumes EUROPEAN as the starting context, that is, if
EUROPEAN digits are encountered before any strong directional
text in the string, the context is presumed to be EUROPEAN, and
so the digits will not shape.
ranges |= CONTEXTUAL_MASK;
return new NumericShaper(EUROPEAN_KEY, ranges);
| public static java.awt.font.NumericShaper | getContextualShaper(int ranges, int defaultContext)Returns a contextual shaper for the provided unicode range(s).
Latin-1 (EUROPEAN) digits will be converted to the decimal digits
corresponding to the range of the preceeding text, if the
range is one of the provided ranges. Multiple ranges are
represented by or-ing the values together, for example,
NumericShaper.ARABIC | NumericShaper.THAI . The
shaper uses defaultContext as the starting context.
int key = getKeyFromMask(defaultContext);
ranges |= CONTEXTUAL_MASK;
return new NumericShaper(key, ranges);
| private static int | getHighBit(int value)Returns the index of the high bit in value (assuming le, actually
power of 2 >= value). value must be positive.
if (value <= 0) {
return -32;
}
int bit = 0;
if (value >= 1 << 16) {
value >>= 16;
bit += 16;
}
if (value >= 1 << 8) {
value >>= 8;
bit += 8;
}
if (value >= 1 << 4) {
value >>= 4;
bit += 4;
}
if (value >= 1 << 2) {
value >>= 2;
bit += 2;
}
if (value >= 1 << 1) {
value >>= 1;
bit += 1;
}
return bit;
| private static int | getKeyFromMask(int mask)
int key = 0;
while (key < NUM_KEYS && ((mask & (1<<key)) == 0)) {
++key;
}
if (key == NUM_KEYS || ((mask & ~(1<<key)) != 0)) {
throw new IllegalArgumentException("invalid shaper: " + Integer.toHexString(mask));
}
return key;
| public int | getRanges()Returns an int that ORs together the values for
all the ranges that will be shaped.
For example, to check if a shaper shapes to Arabic, you would use the
following:
if ((shaper.getRanges() & shaper.ARABIC) != 0) { ...
return mask & ~CONTEXTUAL_MASK;
| public static java.awt.font.NumericShaper | getShaper(int singleRange)Returns a shaper for the provided unicode range. All
Latin-1 (EUROPEAN) digits are converted
to the corresponding decimal unicode digits.
int key = getKeyFromMask(singleRange);
return new NumericShaper(key, singleRange);
| public int | hashCode()Returns a hash code for this shaper.
return mask;
| public boolean | isContextual()Returns a boolean indicating whether or not
this shaper shapes contextually.
return (mask & CONTEXTUAL_MASK) != 0;
| private static boolean | isStrongDirectional(char c)
// warning, synchronize access to this as it modifies state
if (c < strongTable[stCache]) {
stCache = search(c, strongTable, 0, stCache);
} else if (c >= strongTable[stCache + 1]) {
stCache = search(c, strongTable, stCache + 1, strongTable.length - stCache - 1);
}
return (stCache & 0x1) == 1;
| private static int | search(char value, char[] array, int start, int length)fast binary search over subrange of array.
int power = 1 << getHighBit(length);
int extra = length - power;
int probe = power;
int index = start;
if (value >= array[index + extra]) {
index += extra;
}
while (probe > 1) {
probe >>= 1;
if (value >= array[index + probe]) {
index += probe;
}
}
return index;
| public void | shape(char[] text, int start, int count)Converts the digits in the text that occur between start and
start + count.
if (isContextual()) {
shapeContextually(text, start, count, key);
} else {
shapeNonContextually(text, start, count);
}
| public void | shape(char[] text, int start, int count, int context)Converts the digits in the text that occur between start and
start + count, using the provided context.
Context is ignored if the shaper is not a contextual shaper.
if (isContextual()) {
int ctxKey = getKeyFromMask(context);
shapeContextually(text, start, count, ctxKey);
} else {
shapeNonContextually(text, start, count);
}
| private synchronized void | shapeContextually(char[] text, int start, int count, int ctxKey)Perform contextual shaping.
Synchronized to protect caches used in getContextKey and isStrongDirectional.
// if we don't support this context, then don't shape
if ((mask & (1<<ctxKey)) == 0) {
ctxKey = EUROPEAN_KEY;
}
int lastkey = ctxKey;
int base = bases[ctxKey];
char minDigit = ctxKey == TAMIL_KEY ? '\u0031" : '\u0030"; // Tamil doesn't use decimal zero
for (int i = start, e = start + count; i < e; ++i) {
char c = text[i];
if (c >= minDigit && c <= '\u0039") {
text[i] = (char)(c + base);
}
if (isStrongDirectional(c)) {
int newkey = getContextKey(c);
if (newkey != lastkey) {
lastkey = newkey;
ctxKey = newkey;
if (((mask & EASTERN_ARABIC) != 0) && (ctxKey == ARABIC_KEY || ctxKey == EASTERN_ARABIC_KEY)) {
ctxKey = EASTERN_ARABIC_KEY;
} else if ((mask & (1<<ctxKey)) == 0) {
ctxKey = EUROPEAN_KEY;
}
base = bases[ctxKey];
minDigit = ctxKey == TAMIL_KEY ? '\u0031" : '\u0030"; // Tamil doesn't use decimal zero
}
}
}
| private void | shapeNonContextually(char[] text, int start, int count)Perform non-contextual shaping.
int base = bases[key];
char minDigit = key == TAMIL_KEY ? '\u0031" : '\u0030"; // Tamil doesn't use decimal zero
for (int i = start, e = start + count; i < e; ++i) {
char c = text[i];
if (c >= minDigit && c <= '\u0039") {
text[i] = (char)(c + base);
}
}
| public java.lang.String | toString()Returns a String that describes this shaper. This method
is used for debugging purposes only.
StringBuffer buf = new StringBuffer(super.toString());
buf.append("[contextual:" + isContextual());
if (isContextual()) {
buf.append(", context:" + keyNames[key]);
}
buf.append(", range(s): ");
boolean first = true;
for (int i = 0; i < NUM_KEYS; ++i) {
if ((mask & (1 << i)) != 0) {
if (first) {
first = false;
} else {
buf.append(", ");
}
buf.append(keyNames[i]);
}
}
buf.append(']");
return buf.toString();
|
|