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

Bit mask- and enum-based Unicode ranges

This class supports two different programming interfaces to represent Unicode ranges for script-specific digits: bit mask-based ones, such as NumericShaper.ARABIC, and enum-based ones, such as ARABIC. Multiple ranges can be specified by ORing bit mask-based constants, such as:

 NumericShaper.ARABIC | NumericShaper.TAMIL
 
or creating a Set with the NumericShaper.Range constants, such as:
 EnumSet.of(NumericShaper.Scirpt.ARABIC, NumericShaper.Range.TAMIL)
 
The enum-based ranges are a super set of the bit mask-based ones.

If the two interfaces are mixed (including serialization), Unicode range values are mapped to their counterparts where such mapping is possible, such as NumericShaper.Range.ARABIC from/to NumericShaper.ARABIC. If any unmappable range values are specified, such as NumericShaper.Range.BALINESE, those ranges are ignored.

Decimal Digits Precedence

A Unicode range may have more than one set of decimal digits. If multiple decimal digits sets are specified for the same Unicode range, one of the sets will take precedence as follows.

Unicode Range NumericShaper Constants Precedence
Arabic NumericShaper.ARABIC
NumericShaper.EASTERN_ARABIC
NumericShaper.EASTERN_ARABIC
ARABIC
EASTERN_ARABIC
EASTERN_ARABIC
Tai Tham TAI_THAM_HORA
TAI_THAM_THAM
TAI_THAM_THAM

Nested Class Summary

enum NumericShaper.Range A NumericShaper.Range represents a Unicode range of a script having its own decimal digits. 

Constant Summary

int ALL_RANGES Identifies all ranges, for full contextual shaping.
int ARABIC Identifies the ARABIC range and decimal base.
int BENGALI Identifies the BENGALI range and decimal base.
int DEVANAGARI Identifies the DEVANAGARI range and decimal base.
int EASTERN_ARABIC Identifies the ARABIC range and ARABIC_EXTENDED decimal base.
int ETHIOPIC Identifies the ETHIOPIC range and decimal base.
int EUROPEAN Identifies the Latin-1 (European) and extended range, and Latin-1 (European) decimal base.
int GUJARATI Identifies the GUJARATI range and decimal base.
int GURMUKHI Identifies the GURMUKHI range and decimal base.
int KANNADA Identifies the KANNADA range and decimal base.
int KHMER Identifies the KHMER range and decimal base.
int LAO Identifies the LAO range and decimal base.
int MALAYALAM Identifies the MALAYALAM range and decimal base.
int MONGOLIAN Identifies the MONGOLIAN range and decimal base.
int MYANMAR Identifies the MYANMAR range and decimal base.
int ORIYA Identifies the ORIYA range and decimal base.
int TAMIL Identifies the TAMIL range and decimal base.
int TELUGU Identifies the TELUGU range and decimal base.
int THAI Identifies the THAI range and decimal base.
int TIBETAN Identifies the TIBETAN range and decimal base.

Public Method Summary

boolean
equals(Object o)
Returns true if the specified object is an instance of NumericShaper and shapes identically to this one, regardless of the range representations, the bit mask or the enum.
static NumericShaper
getContextualShaper(Set<NumericShaper.Range> 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).
static NumericShaper
getContextualShaper(Set<NumericShaper.Range> ranges, NumericShaper.Range defaultContext)
Returns a contextual shaper for the provided Unicode range(s).
static NumericShaper
getContextualShaper(int ranges)
Returns a contextual shaper for the provided unicode range(s).
Set<NumericShaper.Range>
getRangeSet()
Returns a Set representing all the Unicode ranges in this NumericShaper that will be shaped.
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.
static NumericShaper
getShaper(NumericShaper.Range 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.
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, NumericShaper.Range context)
Converts the digits in the text that occur between start and start + count, using the provided context.
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.
String
toString()
Returns a String that describes this shaper.

Inherited Method Summary