FileDocCategorySizeDatePackage
NumericShaper.javaAPI DocJava SE 6 API26854Tue Jun 10 00:25:24 BST 2008java.awt.font

NumericShaper

public final class NumericShaper extends Object implements Serializable
The 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 preceding text is Arabic, or
// shape European digits to TAMIL digits if preceding text is Tamil, or
// leave European digits alone if there is no preceding text, or
// preceding text is neither Arabic nor Tamil
NumericShaper shaper =
NumericShaper.getContextualShaper(NumericShaper.ARABIC |
NumericShaper.TAMIL,
NumericShaper.EUROPEAN);
shaper.shape(text. start, count);
since
1.4

Fields Summary
private int
key
index of context for contextual shaping - values range from 0 to 18
private int
mask
flag indicating whether to shape contextually (high bit) and which digit ranges to shape (bits 0-18)
public static final int
EUROPEAN
Identifies the Latin-1 (European) and extended range, and Latin-1 (European) decimal base.
public static final int
ARABIC
Identifies the ARABIC range and decimal base.
public static final int
EASTERN_ARABIC
Identifies the ARABIC range and ARABIC_EXTENDED decimal base.
public static final int
DEVANAGARI
Identifies the DEVANAGARI range and decimal base.
public static final int
BENGALI
Identifies the BENGALI range and decimal base.
public static final int
GURMUKHI
Identifies the GURMUKHI range and decimal base.
public static final int
GUJARATI
Identifies the GUJARATI range and decimal base.
public static final int
ORIYA
Identifies the ORIYA range and decimal base.
public static final int
TAMIL
Identifies 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
TELUGU
Identifies the TELUGU range and decimal base.
public static final int
KANNADA
Identifies the KANNADA range and decimal base.
public static final int
MALAYALAM
Identifies the MALAYALAM range and decimal base.
public static final int
THAI
Identifies the THAI range and decimal base.
public static final int
LAO
Identifies the LAO range and decimal base.
public static final int
TIBETAN
Identifies the TIBETAN range and decimal base.
public static final int
MYANMAR
Identifies the MYANMAR range and decimal base.
public static final int
ETHIOPIC
Identifies the ETHIOPIC range and decimal base.
public static final int
KHMER
Identifies the KHMER range and decimal base.
public static final int
MONGOLIAN
Identifies the MONGOLIAN range and decimal base.
public static final int
ALL_RANGES
Identifies 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 booleanequals(java.lang.Object o)
Returns true if the specified object is an instance of NumericShaper and shapes identically to this one.

param
o the specified object to compare to this NumericShaper
return
true if o is an instance of NumericShaper and shapes in the same way; false otherwise.
see
java.lang.Object#equals(java.lang.Object)

	if (o != null) {
	    try {
		NumericShaper rhs = (NumericShaper)o;
		return rhs.mask == mask && rhs.key == key;
	    }
	    catch (ClassCastException e) {
	    }
	}
	return false;
    
private static intgetContextKey(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.NumericShapergetContextualShaper(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 preceding 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.

param
ranges the specified Unicode ranges
return
a shaper for the specified ranges

	ranges |= CONTEXTUAL_MASK;
	return new NumericShaper(EUROPEAN_KEY, ranges);
    
public static java.awt.font.NumericShapergetContextualShaper(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 preceding 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.

param
ranges the specified Unicode ranges
param
defaultContext the starting context, such as NumericShaper.EUROPEAN
return
a shaper for the specified Unicode ranges.
throws
IllegalArgumentException if the specified defaultContext is not a single valid range.

	int key = getKeyFromMask(defaultContext);
	ranges |= CONTEXTUAL_MASK;
	return new NumericShaper(key, ranges);
    
private static intgetHighBit(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 intgetKeyFromMask(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 intgetRanges()
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
the values for all the ranges to be shaped.

	return mask & ~CONTEXTUAL_MASK;
    
public static java.awt.font.NumericShapergetShaper(int singleRange)
Returns a shaper for the provided unicode range. All Latin-1 (EUROPEAN) digits are converted to the corresponding decimal unicode digits.

param
singleRange the specified Unicode range
return
a non-contextual numeric shaper
throws
IllegalArgumentException if the range is not a single range

	int key = getKeyFromMask(singleRange);
	return new NumericShaper(key, singleRange);
    
public inthashCode()
Returns a hash code for this shaper.

return
this shaper's hash code.
see
java.lang.Object#hashCode

	return mask;
    
public booleanisContextual()
Returns a boolean indicating whether or not this shaper shapes contextually.

return
true if this shaper is contextual; false otherwise.

	return (mask & CONTEXTUAL_MASK) != 0;
    
private static booleanisStrongDirectional(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 intsearch(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 voidshape(char[] text, int start, int count)
Converts the digits in the text that occur between start and start + count.

param
text an array of characters to convert
param
start the index into text to start converting
param
count the number of characters in text to convert
throws
IndexOutOfBoundsException if start or start + count is out of bounds
throws
NullPointerException if text is null

        if (text == null) {
            throw new NullPointerException("text is null");
        }
        if ((start < 0) 
            || (start > text.length) 
            || ((start + count) < 0)
            || ((start + count) > text.length)) {
            throw new IndexOutOfBoundsException(
                "bad start or count for text of length " + text.length);
        }

	if (isContextual()) {
	    shapeContextually(text, start, count, key);
	} else {
	    shapeNonContextually(text, start, count);
	}
    
public voidshape(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.

param
text an array of characters
param
start the index into text to start converting
param
count the number of characters in text to convert
param
context the context to which to convert the characters, such as NumericShaper.EUROPEAN
throws
IndexOutOfBoundsException if start or start + count is out of bounds
throws
NullPointerException if text is null
throws
IllegalArgumentException if this is a contextual shaper and the specified context is not a single valid range.

        if (text == null) {
            throw new NullPointerException("text is null");
        }
        if ((start < 0) 
            || (start > text.length) 
            || ((start + count) < 0)
            || ((start + count) > text.length)) {
            throw new IndexOutOfBoundsException(
                "bad start or count for text of length " + text.length);
        }

	if (isContextual()) {
	    int ctxKey = getKeyFromMask(context);
	    shapeContextually(text, start, count, ctxKey);
	} else {
	    shapeNonContextually(text, start, count);
	}
    
private synchronized voidshapeContextually(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 voidshapeNonContextually(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.StringtoString()
Returns a String that describes this shaper. This method is used for debugging purposes only.

return
a String describing this shaper.

	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();