NumericShaper (Java SE 10 & JDK 10 )
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 NumericShaper.Range.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.Range.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
|
NumericShaper.Range.ARABIC
NumericShaper.Range.EASTERN_ARABIC
| NumericShaper.Range.EASTERN_ARABIC
| |
| Tai Tham | NumericShaper.Range.TAI_THAM_HORA
NumericShaper.Range.TAI_THAM_THAM
| NumericShaper.Range.TAI_THAM_THAM
|