java.awt.font
Class NumericShaper

java.lang.Object
  extended byjava.awt.font.NumericShaper
All Implemented Interfaces:
Serializable

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

Since:
1.4
See Also:
Serialized Form

Field Summary
static int ALL_RANGES
          Identifies all ranges, for full contextual shaping.
static int ARABIC
          Identifies the ARABIC range and decimal base.
private static int ARABIC_KEY
           
private static char[] bases
           
static int BENGALI
          Identifies the BENGALI range and decimal base.
private static int BENGALI_KEY
           
private static char[] contexts
           
private static int CONTEXTUAL_MASK
           
private static int ctCache
           
private static int ctCacheLimit
           
static int DEVANAGARI
          Identifies the DEVANAGARI range and decimal base.
private static int DEVANAGARI_KEY
           
static int EASTERN_ARABIC
          Identifies the ARABIC range and ARABIC_EXTENDED decimal base.
private static int EASTERN_ARABIC_KEY
           
static int ETHIOPIC
          Identifies the ETHIOPIC range and decimal base.
private static int ETHIOPIC_KEY
           
static int EUROPEAN
          Identifies the Latin-1 (European) and extended range, and Latin-1 (European) decimal base.
private static int EUROPEAN_KEY
           
static int GUJARATI
          Identifies the GUJARATI range and decimal base.
private static int GUJARATI_KEY
           
static int GURMUKHI
          Identifies the GURMUKHI range and decimal base.
private static int GURMUKHI_KEY
           
static int KANNADA
          Identifies the KANNADA range and decimal base.
private static int KANNADA_KEY
           
private  int key
          index of context for contextual shaping - values range from 0 to 18
private static String[] keyNames
           
static int KHMER
          Identifies the KHMER range and decimal base.
private static int KHMER_KEY
           
static int LAO
          Identifies the LAO range and decimal base.
private static int LAO_KEY
           
static int MALAYALAM
          Identifies the MALAYALAM range and decimal base.
private static int MALAYALAM_KEY
           
private  int mask
          flag indicating whether to shape contextually (high bit) and which digit ranges to shape (bits 0-18)
static int MONGOLIAN
          Identifies the MONGOLIAN range and decimal base.
private static int MONGOLIAN_KEY
           
static int MYANMAR
          Identifies the MYANMAR range and decimal base.
private static int MYANMAR_KEY
           
private static int NUM_KEYS
           
static int ORIYA
          Identifies the ORIYA range and decimal base.
private static int ORIYA_KEY
           
private static int stCache
           
private static char[] strongTable
           
static int TAMIL
          Identifies the TAMIL range and decimal base.
private static int TAMIL_KEY
           
static int TELUGU
          Identifies the TELUGU range and decimal base.
private static int TELUGU_KEY
           
static int THAI
          Identifies the THAI range and decimal base.
private static int THAI_KEY
           
static int TIBETAN
          Identifies the TIBETAN range and decimal base.
private static int TIBETAN_KEY
           
 
Constructor Summary
private NumericShaper(int key, int mask)
          Private constructor.
 
Method Summary
 boolean equals(Object o)
          Returns true if the specified object is an instance of NumericShaper and shapes identically to this one.
private static int getContextKey(char c)
           
static NumericShaper getContextualShaper(int ranges)
          Returns a contextual shaper for the provided unicode range(s).
static NumericShaper getContextualShaper(int ranges, int defaultContext)
          Returns a contextual shaper for the provided unicode range(s).
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.
private static int getKeyFromMask(int mask)
           
 int getRanges()
          Returns an int that ORs together the values for all the ranges that will be shaped.
static NumericShaper getShaper(int singleRange)
          Returns a shaper for the provided unicode range.
 int hashCode()
          Returns a hash code for this shaper.
 boolean isContextual()
          Returns a boolean indicating whether or not this shaper shapes contextually.
private static boolean isStrongDirectional(char c)
           
private static int search(char value, char[] array, int start, int length)
          fast binary search over subrange of array.
 void shape(char[] text, int start, int count)
          Converts the digits in the text that occur between start and start + count.
 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.
private  void shapeContextually(char[] text, int start, int count, int ctxKey)
          Perform contextual shaping.
private  void shapeNonContextually(char[] text, int start, int count)
          Perform non-contextual shaping.
 String toString()
          Returns a String that describes this shaper.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

key

private int key
index of context for contextual shaping - values range from 0 to 18


mask

private int mask
flag indicating whether to shape contextually (high bit) and which digit ranges to shape (bits 0-18)


EUROPEAN

public static final int EUROPEAN
Identifies the Latin-1 (European) and extended range, and Latin-1 (European) decimal base.

See Also:
Constant Field Values

ARABIC

public static final int ARABIC
Identifies the ARABIC range and decimal base.

See Also:
Constant Field Values

EASTERN_ARABIC

public static final int EASTERN_ARABIC
Identifies the ARABIC range and ARABIC_EXTENDED decimal base.

See Also:
Constant Field Values

DEVANAGARI

public static final int DEVANAGARI
Identifies the DEVANAGARI range and decimal base.

See Also:
Constant Field Values

BENGALI

public static final int BENGALI
Identifies the BENGALI range and decimal base.

See Also:
Constant Field Values

GURMUKHI

public static final int GURMUKHI
Identifies the GURMUKHI range and decimal base.

See Also:
Constant Field Values

GUJARATI

public static final int GUJARATI
Identifies the GUJARATI range and decimal base.

See Also:
Constant Field Values

ORIYA

public static final int ORIYA
Identifies the ORIYA range and decimal base.

See Also:
Constant Field Values

TAMIL

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.

See Also:
Constant Field Values

TELUGU

public static final int TELUGU
Identifies the TELUGU range and decimal base.

See Also:
Constant Field Values

KANNADA

public static final int KANNADA
Identifies the KANNADA range and decimal base.

See Also:
Constant Field Values

MALAYALAM

public static final int MALAYALAM
Identifies the MALAYALAM range and decimal base.

See Also:
Constant Field Values

THAI

public static final int THAI
Identifies the THAI range and decimal base.

See Also:
Constant Field Values

LAO

public static final int LAO
Identifies the LAO range and decimal base.

See Also:
Constant Field Values

TIBETAN

public static final int TIBETAN
Identifies the TIBETAN range and decimal base.

See Also:
Constant Field Values

MYANMAR

public static final int MYANMAR
Identifies the MYANMAR range and decimal base.

See Also:
Constant Field Values

ETHIOPIC

public static final int ETHIOPIC
Identifies the ETHIOPIC range and decimal base.

See Also:
Constant Field Values

KHMER

public static final int KHMER
Identifies the KHMER range and decimal base.

See Also:
Constant Field Values

MONGOLIAN

public static final int MONGOLIAN
Identifies the MONGOLIAN range and decimal base.

See Also:
Constant Field Values

ALL_RANGES

public static final int ALL_RANGES
Identifies all ranges, for full contextual shaping.

See Also:
Constant Field Values

EUROPEAN_KEY

private static final int EUROPEAN_KEY
See Also:
Constant Field Values

ARABIC_KEY

private static final int ARABIC_KEY
See Also:
Constant Field Values

EASTERN_ARABIC_KEY

private static final int EASTERN_ARABIC_KEY
See Also:
Constant Field Values

DEVANAGARI_KEY

private static final int DEVANAGARI_KEY
See Also:
Constant Field Values

BENGALI_KEY

private static final int BENGALI_KEY
See Also:
Constant Field Values

GURMUKHI_KEY

private static final int GURMUKHI_KEY
See Also:
Constant Field Values

GUJARATI_KEY

private static final int GUJARATI_KEY
See Also:
Constant Field Values

ORIYA_KEY

private static final int ORIYA_KEY
See Also:
Constant Field Values

TAMIL_KEY

private static final int TAMIL_KEY
See Also:
Constant Field Values

TELUGU_KEY

private static final int TELUGU_KEY
See Also:
Constant Field Values

KANNADA_KEY

private static final int KANNADA_KEY
See Also:
Constant Field Values

MALAYALAM_KEY

private static final int MALAYALAM_KEY
See Also:
Constant Field Values

THAI_KEY

private static final int THAI_KEY
See Also:
Constant Field Values

LAO_KEY

private static final int LAO_KEY
See Also:
Constant Field Values

TIBETAN_KEY

private static final int TIBETAN_KEY
See Also:
Constant Field Values

MYANMAR_KEY

private static final int MYANMAR_KEY
See Also:
Constant Field Values

ETHIOPIC_KEY

private static final int ETHIOPIC_KEY
See Also:
Constant Field Values

KHMER_KEY

private static final int KHMER_KEY
See Also:
Constant Field Values

MONGOLIAN_KEY

private static final int MONGOLIAN_KEY
See Also:
Constant Field Values

NUM_KEYS

private static final int NUM_KEYS
See Also:
Constant Field Values

keyNames

private static final String[] keyNames

CONTEXTUAL_MASK

private static final int CONTEXTUAL_MASK
See Also:
Constant Field Values

bases

private static final char[] bases

contexts

private static final char[] contexts

ctCache

private static int ctCache

ctCacheLimit

private static int ctCacheLimit

strongTable

private static char[] strongTable

stCache

private static int stCache
Constructor Detail

NumericShaper

private NumericShaper(int key,
                      int mask)
Private constructor.

Method Detail

getContextKey

private static int getContextKey(char c)

isStrongDirectional

private static boolean isStrongDirectional(char c)

getKeyFromMask

private static int getKeyFromMask(int mask)

getShaper

public static 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.

Parameters:
singleRange - the specified Unicode range
Returns:
a non-contextual numeric shaper
Throws:
IllegalArgumentException - if the range is not a single range

getContextualShaper

public static 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.

Parameters:
ranges - the specified Unicode ranges
Returns:
a shaper for the specified ranges

getContextualShaper

public static 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.

Parameters:
ranges - the specified Unicode ranges
defaultContext - the starting context, such as NumericShaper.EUROPEAN
Returns:
a shaper for the specified Unicode ranges.

shape

public void shape(char[] text,
                  int start,
                  int count)
Converts the digits in the text that occur between start and start + count.

Parameters:
text - an array of characters to convert
start - the index into text to start converting
count - the number of characters in text to convert

shape

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.

Parameters:
text - an array of characters
start - the index into text to start converting
count - the number of characters in text to convert
context - the context to which to convert the characters, such as NumericShaper.EUROPEAN

isContextual

public boolean isContextual()
Returns a boolean indicating whether or not this shaper shapes contextually.

Returns:
true if this shaper is contextual; false otherwise.

getRanges

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) { ...

Returns:
the values for all the ranges to be shaped.

shapeNonContextually

private void shapeNonContextually(char[] text,
                                  int start,
                                  int count)
Perform non-contextual shaping.


shapeContextually

private void shapeContextually(char[] text,
                               int start,
                               int count,
                               int ctxKey)
Perform contextual shaping. Synchronized to protect caches used in getContextKey and isStrongDirectional.


hashCode

public int hashCode()
Returns a hash code for this shaper.

Overrides:
hashCode in class Object
Returns:
this shaper's hash code.
See Also:
Object.hashCode()

equals

public boolean equals(Object o)
Returns true if the specified object is an instance of NumericShaper and shapes identically to this one.

Overrides:
equals in class Object
Parameters:
o - the specified object to compare to this NumericShaper
Returns:
true if o is an instance of NumericShaper and shapes in the same way; false otherwise.
See Also:
Object.equals(java.lang.Object)

toString

public String toString()
Returns a String that describes this shaper. This method is used for debugging purposes only.

Overrides:
toString in class Object
Returns:
a String describing this shaper.

getHighBit

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.


search

private static int search(char value,
                          char[] array,
                          int start,
                          int length)
fast binary search over subrange of array.