Java中文英文数字混合掩码_Java8 中文教程
默認(rèn)情況下,當(dāng)文本包含數(shù)字值時,這些值使用拉丁(歐洲)數(shù)字顯示。當(dāng)首選其他 Unicode 數(shù)字形狀時,請使用java.awt.font.NumericShaper類。使用NumericShaper API,您可以以任何 Unicode 數(shù)字形狀顯示內(nèi)部表示為 ASCII 值的數(shù)字值。
ArabicDigits示例中的以下代碼段顯示了如何使用NumericShaper實例將拉丁數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字。決定整形動作的線為粗體。
ArabicDigitsPanel(String fontname) {
HashMap map = new HashMap();
Font font = new Font(fontname, Font.PLAIN, 60);
map.put(TextAttribute.FONT, font);
map.put(TextAttribute.NUMERIC_SHAPING,
NumericShaper.getShaper(NumericShaper.ARABIC));
FontRenderContext frc = new FontRenderContext(null, false, false);
layout = new TextLayout(text, map, frc);
}
// ...
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
layout.draw(g2d, 10, 50);
}
獲取阿拉伯?dāng)?shù)字的NumericShaper實例,并將其放入TextLayout.NUMERIC_SHAPING屬性鍵的HashMap中。哈希 Map 被傳遞到TextLayout實例。用paint方法渲染文本后,數(shù)字將顯示在所需的腳本中。在此示例中,拉丁數(shù)字 0 到 9 被繪制為阿拉伯?dāng)?shù)字。
前面的示例使用NumericShaper.ARABIC常量檢索所需的整形器,但是NumericShaper類提供了許多語言的常量。這些常量定義為位掩碼,并稱為NumericShaper 基于位掩碼的常量。
基于枚舉的范圍常量
指定一組特定數(shù)字的另一種方法是使用NumericShaper.Range枚舉類型(枚舉)。 Java SE 7 發(fā)行版中引入的該枚舉還提供了一組constants。盡管使用不同的機(jī)制定義了這些常量,但是NumericShaper.ARABIC位掩碼在功能上等效于NumericShaper.Range.ARABIC枚舉,并且每種常量類型都有對應(yīng)的getShaper方法:
ArabicDigitsEnum示例與 ArabicDigits 示例相同,不同之處在于它使用NumericShaper.Range枚舉指定語言腳本:
ArabicDigitsEnumPanel(String fontname) {
HashMap map = new HashMap();
Font font = new Font(fontname, Font.PLAIN, 60);
map.put(TextAttribute.FONT, font);
map.put(TextAttribute.NUMERIC_SHAPING,
NumericShaper.getShaper(NumericShaper.Range.ARABIC));
FontRenderContext frc = new FontRenderContext(null, false, false);
layout = new TextLayout(text, map, frc);
}
兩種getShaper方法都接受singleRange參數(shù)。無論使用哪種常量類型,都可以指定腳本特定數(shù)字的范圍??梢允褂肙R操作數(shù)來組合基于位掩碼的常量,也可以創(chuàng)建一組NumericShaper.Range枚舉。下面顯示了如何使用每種常量類型定義范圍:
NumericShaper.MONGOLIAN | NumericShaper.THAI |
NumericShaper.TIBETAN
EnumSet.of(
NumericShaper.Range.MONGOLIAN,
NumericShaper.Range.THAI,
NumericShaper.Range.TIBETAN)
您可以使用_方法(針對基于位掩碼的成形器)或getRangeSet方法(針對基于枚舉的成形器)來查詢NumericShaper對象,以確定其支持的范圍。
Note:
您可以使用傳統(tǒng)的基于位掩碼的常量或基于Range枚舉的常量。在決定使用哪種方法時,有一些注意事項:
Range API 需要 JDK 7 或更高版本。
Range API 比位掩碼 API 涵蓋更多的 Unicode 范圍。
位掩碼 API 比Range API 快一點。
根據(jù)語言上下文渲染數(shù)字
ArabicDigits示例旨在將整形器用于特定的語言,但是有時必須根據(jù)語言上下文來呈現(xiàn)數(shù)字。例如,如果數(shù)字前面的文本使用泰語腳本,則首選泰文數(shù)字。如果 Literals 以藏文顯示,則以藏文數(shù)字為佳。
您可以使用getContextualShaper方法之一來完成此操作:
前兩種方法使用位掩碼常量,后兩種使用枚舉常量。接受defaultContext參數(shù)的方法使您可以指定在文本前顯示數(shù)值時使用的初始成形器。如果未定義默認(rèn)上下文,則使用拉丁形狀顯示任何前導(dǎo)數(shù)字。
ShapedDigits示例顯示了成型器的工作方式。顯示五個文本布局:
第一個布局不使用整形器;所有數(shù)字都顯示為拉丁文。
第二種布局將所有數(shù)字整形為阿拉伯?dāng)?shù)字,而與語言環(huán)境無關(guān)。
第三種布局使用使用阿拉伯?dāng)?shù)字的上下文整形器。默認(rèn)上下文定義為阿拉伯語。
第四個布局使用使用阿拉伯?dāng)?shù)字的上下文整形器,但是該整形器未指定默認(rèn)上下文。
第五個布局采用了使用ALL_RANGES位掩碼的上下文整形器,但是該整形器未指定默認(rèn)上下文。
以下代碼行顯示了成形器(如果使用的話)的定義方式:
不使用整形器。
NumericShaper arabic = NumericShaper.getShaper(NumericShaper.ARABIC);
NumericShaper contextualArabic = NumericShaper.getContextualShaper(NumericShaper.ARABIC, NumericShaper.ARABIC);
NumericShaper contextualArabicASCII = NumericShaper.getContextualShaper(NumericShaper.ARABIC);
NumericShaper contextualAll = NumericShaper.getContextualShaper(NumericShaper.ALL_RANGES);
總結(jié)
以上是生活随笔為你收集整理的Java中文英文数字混合掩码_Java8 中文教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 测试计算机的运行速度
- 下一篇: 操作rabbitMQ时,误删guest账