FileDocCategorySizeDatePackage
NumericShaper.javaAPI DocAndroid 1.5 API34407Wed May 06 22:41:02 BST 2009java.awt.font

NumericShaper

public final class NumericShaper extends Object implements Serializable
The Class NumericShaper provides methods to convert latin character codes to unicode character codes. For tables of the character codes used, see unicode.org.

Fields Summary
private static final long
serialVersionUID
The Constant serialVersionUID.
public static final int
EUROPEAN
The Constant EUROPEAN indicates the latin and extended range, and latin decimal base.
public static final int
ARABIC
The Constant ARABIC indicates the ARABIC range and decimal base.
public static final int
EASTERN_ARABIC
The Constant EASTERN_ARABIC indicates the ARABIC range and ARABIC_EXTENDED decimal base.
public static final int
DEVANAGARI
The Constant DEVANAGARI indicates the DEVANAGARI range and decimal base.
public static final int
BENGALI
The Constant BENGALI indicates the BENGALI range and decimal base.
public static final int
GURMUKHI
The Constant GURMUKHI indicates the GURMUKHI range and decimal base.
public static final int
GUJARATI
The Constant GUJARATI indicates the GUJARATI range and decimal base.
public static final int
ORIYA
The Constant ORIYA indicates the ORIYA range and decimal base.
public static final int
TAMIL
The Constant TAMIL indicates the TAMIL range and decimal base.
public static final int
TELUGU
The Constant TELUGU indicates the TELUGU range and decimal base.
public static final int
KANNADA
The Constant KANNADA indicates the KANNADA range and decimal base.
public static final int
MALAYALAM
The Constant MALAYALAM indicates the MALAYALAM range and decimal base.
public static final int
THAI
The Constant THAI indicates the THAI range and decimal base.
public static final int
LAO
The Constant LAO indicates the LAO range and decimal base.
public static final int
TIBETAN
The Constant TIBETAN indicates the TIBETAN range and decimal base.
public static final int
MYANMAR
The Constant MYANMAR indicates the MYANMAR range and decimal base.
public static final int
ETHIOPIC
The Constant ETHIOPIC indicates the ETHIOPIC range and decimal base.
public static final int
KHMER
The Constant KHMER indicates the KHMER range and decimal base.
public static final int
MONGOLIAN
The Constant MONGOLIAN indicates the MONGOLIAN range and decimal base.
public static final int
ALL_RANGES
The Constant ALL_RANGES indicates all ranges.
private static final int
INDEX_EUROPEAN
The Constant INDEX_EUROPEAN.
private static final int
INDEX_ARABIC
The Constant INDEX_ARABIC.
private static final int
INDEX_EASTERN_ARABIC
The Constant INDEX_EASTERN_ARABIC.
private static final int
INDEX_DEVANAGARI
The Constant INDEX_DEVANAGARI.
private static final int
INDEX_BENGALI
The Constant INDEX_BENGALI.
private static final int
INDEX_GURMUKHI
The Constant INDEX_GURMUKHI.
private static final int
INDEX_GUJARATI
The Constant INDEX_GUJARATI.
private static final int
INDEX_ORIYA
The Constant INDEX_ORIYA.
private static final int
INDEX_TAMIL
The Constant INDEX_TAMIL.
private static final int
INDEX_TELUGU
The Constant INDEX_TELUGU.
private static final int
INDEX_KANNADA
The Constant INDEX_KANNADA.
private static final int
INDEX_MALAYALAM
The Constant INDEX_MALAYALAM.
private static final int
INDEX_THAI
The Constant INDEX_THAI.
private static final int
INDEX_LAO
The Constant INDEX_LAO.
private static final int
INDEX_TIBETAN
The Constant INDEX_TIBETAN.
private static final int
INDEX_MYANMAR
The Constant INDEX_MYANMAR.
private static final int
INDEX_ETHIOPIC
The Constant INDEX_ETHIOPIC.
private static final int
INDEX_KHMER
The Constant INDEX_KHMER.
private static final int
INDEX_MONGOLIAN
The Constant INDEX_MONGOLIAN.
private static final int
MAX_INDEX
The Constant MAX_INDEX.
private final int[]
scriptsRanges
The scripts ranges.
private final int[]
digitsLowRanges
The digits low ranges.
private final String[]
contexts
The contexts.
private static final int[]
STRONG_TEXT_FLAGS
The Constant STRONG_TEXT_FLAGS.
private int
key
The key.
private int
mask
The mask.
private int
fRanges
The ranges.
private int
fDefaultContextIndex
The default context index.
private boolean
fContextual
The contextual.
private int
fSingleRangeIndex
The single range index.
Constructors Summary
private NumericShaper(int ranges, int defaultContext, boolean isContextual)
Creates NumericShaper with specified parameters.

param
ranges specified ranges to be shaped
param
defaultContext default context range
param
isContextual specifies if the instance is contextual


                                    
          
        this.fRanges = ranges;
        this.fDefaultContextIndex = getIndexFromRange(defaultContext);
        this.fContextual = isContextual;

        if (!fContextual){
            fSingleRangeIndex = getIndexFromRange(ranges);
        }
    
Methods Summary
private voidcontextualShape(char[] text, int start, int count, int contextIndex)
Converts count of digits of the given array of characters from the start index using specified context. This method is applied for the contextual shaping, if the shaper instance is not contextual use nonContextualShape method.

param
text an array of chars
param
start index of the first character to convert
param
count a number of characters to convert
param
contextIndex index of the script index to use in shaper

        char maxDigit = (char)0x0039;
        char minDigit = (char)0x0030;

        int currIndex;
        if (((1 << contextIndex) & fRanges) == 0 ){
            currIndex = INDEX_EUROPEAN;
        } else {
            currIndex = contextIndex;
        }

        for (int ind = start; ind < start + count; ind++){
            if (minDigit <= text[ind] && text[ind] <= maxDigit){
                if (currIndex != INDEX_ETHIOPIC || text[ind] != '0"){
                    text[ind] = (char)(digitsLowRanges[currIndex] + text[ind]);
                }
            } else {
                if(isCharStrong(text[ind])){
                    int index = getCharIndex(text[ind]);
                    if (currIndex != index){
                        if (((1 << index) & fRanges) != 0){
                            currIndex = index;
                        } else {
                            currIndex = INDEX_EUROPEAN;
                        }
                    }
                }
            }
        }

    
public booleanequals(java.lang.Object obj)
Compares this NumericShaper object with the specified Object.

param
obj the Object to be compared.
return
true, if this NumericShaper object is equal to the specified Object, false otherwise.

        if (obj == null) {
            return false;
        }

        if (obj == this) {
            return true;
        }

        try {
            NumericShaper ns = (NumericShaper)obj;
            return (fRanges == ns.fRanges &&
                    fDefaultContextIndex == ns.fDefaultContextIndex &&
                    fContextual == ns.fContextual);
        } catch (ClassCastException e){
        }

        return false;
    
private intgetCharIndex(char ch)
Returns the index of the script of the specified char.

param
ch specified unicode character
return
script index corresponding to the given char

        int index = INDEX_EUROPEAN;
        for (int i=0; i < MAX_INDEX; i++){
            int j = i * 2;
            if (scriptsRanges[j] <= ch && ch <= scriptsRanges[j+1]){
                return i;
            }
        }

        return index;
    
public static java.awt.font.NumericShapergetContextualShaper(int ranges, int defaultContext)
Gets the NumericShaper for the specified unicode ranges and default unicode range. The defaultContext parameter is used as the starting context (which indicates the language/script being used). The OR logical operation should be used for multiple ranges: NumericShaper.DEVANAGARI | NumericShaper.BENGALI. The NumericShaper returned by this method is contextual in that it supports multiple character ranges, depending on the context.

param
ranges the unicode ranges.
param
defaultContext the default, starting context.
return
the NumericShaper for the specified ranges.

        ranges &= ALL_RANGES;
        defaultContext &= ALL_RANGES;
        return new NumericShaper(ranges, defaultContext, true);
    
public static java.awt.font.NumericShapergetContextualShaper(int ranges)
Gets the NumericShaper for the specified unicode ranges. The OR logical operation should be used for multiple ranges: NumericShaper.DEVANAGARI | NumericShaper.BENGALI. The NumericShaper returned by this method is contextual in that it supports multiple character ranges, depending on the context.

param
ranges the unicode ranges.
return
the NumericShaper for the specified ranges.

        ranges &= ALL_RANGES;
        return new NumericShaper(ranges, EUROPEAN, true);
    
private intgetIndexFromRange(int range)
Returns script index of the specified context range.

param
range specified range
return
one of the script indices according to the specified range.

        if (range == 0){
            // BEGIN android-changed
            throwRange(range);
            // END android-changed
        }

        int index = 0;
        while (index < MAX_INDEX){
            if (range == (1 << index)){
                return index;
            }
            index++;
        }

        // BEGIN android-changed
        throwRange(range);
        return -1; // Never executed; quiets the compiler.
        // END android-changed
    
private intgetRangeFromIndex(int index)
Returns range corresponding to the specified script index.

param
index specified script index
return
one of the range constants according to the specified script index.

        if (index < 0 || index >= MAX_INDEX){
            // BEGIN android-changed
            throwRange(index);
            // END android-changed
        }

        return 1 << index;
    
public intgetRanges()
Gets the masks for all of the ranges supported by this NumericShaper, packed into an int value using the logical OR logical operation for multiple ranges: NumericShaper.DEVANAGARI | NumericShaper.BENGALI.

return
all ranges of this NumericShaper.

        return fRanges;
    
public static java.awt.font.NumericShapergetShaper(int singleRange)
Gets a NumericShaper for the specified unicode range. The NumericShaper supports only a single range and hence is not contextual.

param
singleRange the specified unicode single range.
return
the NumericShaper for the specified unicode range.

        singleRange &= ALL_RANGES;
        return new NumericShaper(singleRange, EUROPEAN, false);
    
public inthashCode()
Returns a hash code of this NumericShaper.

return
a hash code of this NumericShaper.

        HashCode hash = new HashCode();

        hash.append(fRanges);
        hash.append(fDefaultContextIndex);
        hash.append(fContextual);

        return hash.hashCode();

    
private booleanisCharStrong(int chr)
Returns true if the bidirectional category of the character is strong.

param
chr the chr
return
true, if the character is strong, false otherwise

        return (STRONG_TEXT_FLAGS[chr >> 5] & (1 << (chr % 32))) != 0;
    
public booleanisContextual()
Checks if this NumericShaper is contextual (supporting multiple script ranges) or not.

return
true, if this NumericShaper is contextual, false otherwise.

        return fContextual;
    
private voidnonContextualShape(char[] text, int start, int count)
Converts count of digits of the given array of characters from the start index. Method is applied for non-contextual shaper.

param
text an array of chars
param
start index of the first character to convert
param
count a number of characters to convert

        char maxDigit = (char)0x0039;
        char minDigit = (char)((fRanges == ETHIOPIC) ? 0x0031 : 0x0030);
        for (int ind = start; ind < start + count; ind++){
            if (minDigit <= text[ind] && text[ind] <= maxDigit){
                    text[ind] = (char)(digitsLowRanges[fSingleRangeIndex] + text[ind]);
            }
        }

    
private voidreadObject(java.io.ObjectInputStream in)
Read object.

param
in the in
throws
IOException Signals that an I/O exception has occurred.
throws
ClassNotFoundException the class not found exception

        in.defaultReadObject();
        updateRangesFields();
    
public voidshape(char[] text, int start, int count, int context)
Transforms the encoding of the text, starting from the character at index start and transforming count characters, using the specified context.

param
text the text to be shaped.
param
start the start offset of the text.
param
count the number of characters to be shaped.
param
context the context to be used for shaping.

        if (isContextual()){
            contextualShape(text, start, count, getIndexFromRange(context));
        } else {
            nonContextualShape(text, start, count);
        }
    
public voidshape(char[] text, int start, int count)
Transforms the encoding of the text, starting from the character at index start and transforming count characters.

param
text the text to be shaped.
param
start the start offset of the text.
param
count the number of characters to be shaped.

        if (isContextual()){
            contextualShape(text, start, count, fDefaultContextIndex);
        } else {
            nonContextualShape(text, start, count);
        }
    
private static voidthrowRange(int value)
Throws a standard "out of range" exception.

param
value the bogus value

        throw new IllegalArgumentException(
                "Illegal range argument value: " + value);
    
public java.lang.StringtoString()
Returns a string representation of this NumericShaper.

return
the string representation of this NumericShaper.

        /* !! There is no description in the documentation what this method must
         * return. Thus format of toString method is based on 1.5 release 
         * behavior and can be obtained using next test sample:
         * 
         * // Simple shapers toString format  
         * System.out.println(NumericShaper.getShaper(NumericShaper.EASTERN_ARABIC));
         * 
         * // Context shapers with default context toString format
         * System.out.println(NumericShaper.getContextualShaper(
         *      NumericShaper.ARABIC | NumericShaper.TAMIL));
         * 
         * // Context shapers with context 
         * System.out.println(NumericShaper.getContextualShaper(
         *      NumericShaper.ARABIC | NumericShaper.TAMIL, 
         *      NumericShaper.EASTERN_ARABIC));
         */
        StringBuffer sb = new StringBuffer(super.toString());

        sb.append("[contextual:"); //$NON-NLS-1$
        sb.append(fContextual);

        if (fContextual){
            sb.append(", context:"); //$NON-NLS-1$
            sb.append(contexts[fDefaultContextIndex]);
        }

        sb.append(", range(s): "); //$NON-NLS-1$
        if (fContextual) {
            int index = 0;
            boolean isFirst = true;
            while (index < MAX_INDEX){
                if ((fRanges & (1 << index)) != 0){
                    if (isFirst){
                        isFirst = false;
                    } else {
                        sb.append(", "); //$NON-NLS-1$
                    }
                    sb.append(contexts[index]);
                }
                index++;
            }
        } else {
            sb.append(contexts[fSingleRangeIndex]);
        }
        sb.append("]"); //$NON-NLS-1$

        return sb.toString();
    
private voidupdateKeyMaskFields()
Updates private fields for object after deserialization according to the serialized form of this class mentioned in the documentation.

        mask = fRanges;
        if (fContextual){
            mask |= (1 << 31);
            key = fDefaultContextIndex;
        } else{
            key = fSingleRangeIndex;
        }
    
private voidupdateRangesFields()
Updates all private serialized fields for object to be correctly serialized according to the serialized form of this class mentioned in the documentation.

        fRanges = (mask & ~(1 << 31));
        fContextual = ((mask &(1 << 31)) != 0);
        if (fContextual){
            fRanges = (mask & ~(1 << 31));
            fDefaultContextIndex = key;
        } else {
            fRanges = mask;
            fSingleRangeIndex = key;
        }
    
private voidwriteObject(java.io.ObjectOutputStream out)
Write object.

param
out the out
throws
IOException Signals that an I/O exception has occurred.

        updateKeyMaskFields();
        out.defaultWriteObject();