Java中文英文数字混合掩码_Java8 中文教程
默認情況下,當文本包含數字值時,這些值使用拉丁(歐洲)數字顯示。當首選其他 Unicode 數字形狀時,請使用java.awt.font.NumericShaper類。使用NumericShaper API,您可以以任何 Unicode 數字形狀顯示內部表示為 ASCII 值的數字值。
ArabicDigits示例中的以下代碼段顯示了如何使用NumericShaper實例將拉丁數字轉換為阿拉伯數字。決定整形動作的線為粗體。
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);
}
獲取阿拉伯數字的NumericShaper實例,并將其放入TextLayout.NUMERIC_SHAPING屬性鍵的HashMap中。哈希 Map 被傳遞到TextLayout實例。用paint方法渲染文本后,數字將顯示在所需的腳本中。在此示例中,拉丁數字 0 到 9 被繪制為阿拉伯數字。
前面的示例使用NumericShaper.ARABIC常量檢索所需的整形器,但是NumericShaper類提供了許多語言的常量。這些常量定義為位掩碼,并稱為NumericShaper 基于位掩碼的常量。
基于枚舉的范圍常量
指定一組特定數字的另一種方法是使用NumericShaper.Range枚舉類型(枚舉)。 Java SE 7 發行版中引入的該枚舉還提供了一組constants。盡管使用不同的機制定義了這些常量,但是NumericShaper.ARABIC位掩碼在功能上等效于NumericShaper.Range.ARABIC枚舉,并且每種常量類型都有對應的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參數。無論使用哪種常量類型,都可以指定腳本特定數字的范圍。可以使用OR操作數來組合基于位掩碼的常量,也可以創建一組NumericShaper.Range枚舉。下面顯示了如何使用每種常量類型定義范圍:
NumericShaper.MONGOLIAN | NumericShaper.THAI |
NumericShaper.TIBETAN
EnumSet.of(
NumericShaper.Range.MONGOLIAN,
NumericShaper.Range.THAI,
NumericShaper.Range.TIBETAN)
您可以使用_方法(針對基于位掩碼的成形器)或getRangeSet方法(針對基于枚舉的成形器)來查詢NumericShaper對象,以確定其支持的范圍。
Note:
您可以使用傳統的基于位掩碼的常量或基于Range枚舉的常量。在決定使用哪種方法時,有一些注意事項:
Range API 需要 JDK 7 或更高版本。
Range API 比位掩碼 API 涵蓋更多的 Unicode 范圍。
位掩碼 API 比Range API 快一點。
根據語言上下文渲染數字
ArabicDigits示例旨在將整形器用于特定的語言,但是有時必須根據語言上下文來呈現數字。例如,如果數字前面的文本使用泰語腳本,則首選泰文數字。如果 Literals 以藏文顯示,則以藏文數字為佳。
您可以使用getContextualShaper方法之一來完成此操作:
前兩種方法使用位掩碼常量,后兩種使用枚舉常量。接受defaultContext參數的方法使您可以指定在文本前顯示數值時使用的初始成形器。如果未定義默認上下文,則使用拉丁形狀顯示任何前導數字。
ShapedDigits示例顯示了成型器的工作方式。顯示五個文本布局:
第一個布局不使用整形器;所有數字都顯示為拉丁文。
第二種布局將所有數字整形為阿拉伯數字,而與語言環境無關。
第三種布局使用使用阿拉伯數字的上下文整形器。默認上下文定義為阿拉伯語。
第四個布局使用使用阿拉伯數字的上下文整形器,但是該整形器未指定默認上下文。
第五個布局采用了使用ALL_RANGES位掩碼的上下文整形器,但是該整形器未指定默認上下文。
以下代碼行顯示了成形器(如果使用的話)的定義方式:
不使用整形器。
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);
總結
以上是生活随笔為你收集整理的Java中文英文数字混合掩码_Java8 中文教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 测试计算机的运行速度
- 下一篇: 详细分析Java8中default关键字