The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Before a piece of text can be displayed, it must be properly shaped and positioned using the appropriate glyphs and ligatures. 在显示一段文本之前,必须使用适当的图示符和连字对其进行适当的形状和定位。This process is referred to as text layout. 此过程称为文本布局。The text layout process involves the following:文本布局过程涉及以下内容:
The information used to lay out text is also necessary for performing text operations such as caret positioning, hit detection, and highlighting. 用于布局文本的信息对于执行插入符号定位、命中检测和高亮显示等文本操作也是必需的。See Working with Bidirectional Text for more information about these text operations.有关这些文本操作的详细信息,请参阅使用双向文本。
To develop software that can be deployed in international markets, text must be laid out in different languages in a way that conforms to the rules of the appropriate writing system.为了开发能够在国际市场上部署的软件,文本必须以符合适当书写系统规则的方式以不同的语言排列。
This section covers the following topics:本节涵盖以下主题:
A glyph is the visual representation of one or more characters. 字形是一个或多个字符的视觉表示。The shape, size, and position of a glyph is dependent on its context. 字形的形状、大小和位置取决于其上下文。Many different glyphs can be used to represent a single character or combination of characters, depending on the font and style.根据字体和样式,可以使用许多不同的图示符来表示单个字符或字符组合。
For example, in handwritten cursive text, a particular character can take on different shapes depending on how it is connected to adjacent characters.例如,在手写草书文本中,特定字符可以根据其与相邻字符的连接方式呈现不同的形状。
In some writing systems, particularly Arabic, the context of a glyph must always be taken into account. 在某些书写系统中,尤其是阿拉伯语,必须始终考虑字形的上下文。Unlike in English, cursive forms are mandatory in Arabic; it is unacceptable to present text without using cursive forms.与英语不同,草书形式在阿拉伯语中是强制性的;不使用草书形式呈现文本是不可接受的。
Depending on the context, these cursive forms can differ radically in shape. 根据上下文的不同,这些草书的形式可能在形状上有根本的不同。For example, the Arabic letter heh has the four cursive forms shown in the following figure:例如,阿拉伯文字母heh有四种草书形式,如下图所示:
Although these four forms are quite different from one another, such cursive shape-changing is not fundamentally different from cursive writing in English.虽然这四种形式彼此有很大的不同,但这种草书的形状变化与英语草书并没有根本的区别。
In some contexts, two glyphs can change shape even more radically and merge to form a single glyph. 在某些情况下,两个图示符可以更彻底地改变形状,并合并形成一个图示符。This type of merged glyph is called a ligature. 这种类型的合并图示符称为连字。For example, most English fonts contain the ligature fi shown in the following figure:例如,大多数英文字体包含连字fi,如下图所示:
The merged glyph takes into account the overhang on the letter f and combines the characters in a natural-looking way, instead of simply letting the letters collide.合并的字形考虑了字母f上的突出部分,并以自然的方式组合字符,而不是简单地让字母碰撞。
Ligatures are also used in Arabic and the use of some ligatures is mandatory; it is unacceptable to present certain character combinations without using the appropriate ligature. 连字在阿拉伯语中也有使用,一些连字的使用是强制性的;不使用适当的连字就呈现某些字符组合是不可接受的。When ligatures are formed from Arabic characters, the shapes change even more radically than they do in English. 当连字由阿拉伯字符组成时,其形状的变化甚至比英语中的变化更为剧烈。For example, the following figure illustrates how two Arabic characters are combined into a single ligature when they appear together.例如,下图说明了两个阿拉伯文字符一起出现时如何组合成一个连字。
In the Java programming language, text is encoded using Unicode character encoding. 在Java编程语言中,使用Unicode字符编码对文本进行编码。Text that uses Unicode character encoding is stored in memory in logical order. 使用Unicode字符编码的文本按逻辑顺序存储在内存中。Logical order is the order in which characters and words are read and written. 逻辑顺序是字符和单词的读写顺序。The logical order is not necessarily the same as the visual order, the order in which the corresponding glyphs are displayed.逻辑顺序不一定与视觉顺序相同,即相应图示符的显示顺序。
The visual order for glyphs in a particular writing system (script) is called the script order. 特定书写系统(脚本)中字形的可视顺序称为脚本顺序。For example, the script order for Roman text is left-to-right and the script order for Arabic and Hebrew is right-to-left.例如,罗马文本的脚本顺序是从左到右,阿拉伯语和希伯来语的脚本顺序是从右到左。
Some writing systems have rules in addition to script order for arranging glyphs and words on lines of text. 除了脚本顺序外,一些书写系统还有规则,用于在文本行上排列字形和单词。For example, Arabic and Hebrew numbers run left to right, even though the letters run right to left. 例如,阿拉伯语和希伯来语数字从左到右,即使字母从右到左。This means that Arabic and Hebrew, even with no embedded English text, are truly bidirectional. 这意味着阿拉伯语和希伯来语,即使没有嵌入英语文本,也是真正双向的。See Working with Bidirectional Text for more information.有关详细信息,请参阅使用双向文本。
Unless you are working with a monospaced font, different characters in a font have different widths. 除非使用单间距字体,否则字体中的不同字符具有不同的宽度。This means that all positioning and measuring of text has to take into account exactly which characters are used, not just how many. 这意味着文本的所有定位和测量都必须考虑使用的字符,而不仅仅是数量。For example, to right-align a column of numbers displayed in a proportional font, you can't simply use extra spaces to position the text. 例如,要右对齐按比例字体显示的数字列,不能简单地使用额外的空格来定位文本。To properly align the column, you need to know the exact width of each number so that you can adjust accordingly.要正确对齐列,需要知道每个数字的确切宽度,以便进行相应调整。
Text is often displayed using multiple fonts and styles, such as bold or italic. 文本通常使用多种字体和样式显示,如粗体或斜体。In this case, even the same character can have different shapes and widths, depending on how it is styled. 在这种情况下,即使是同一个字符也可以有不同的形状和宽度,这取决于它的样式。To properly position, measure, and render text, you need to keep track of each individual character and the style applied to that character. 要正确定位、测量和渲染文本,需要跟踪每个单独的字符以及应用于该字符的样式。Fortunately, the 幸运的是,TextLayout
class does this for you.TextLayout
类为您完成了这项工作。
To properly display text in languages such as Hebrew and Arabic, each individual character needs to be measured and positioned within the context of neighboring characters. 要以希伯来语和阿拉伯语等语言正确显示文本,需要在相邻字符的上下文中测量和定位每个字符。Because the shapes and positions of the characters can change depending on the context, measuring and positioning such text without taking the context into account produces unacceptable results.由于字符的形状和位置可能根据上下文而改变,因此在不考虑上下文的情况下测量和定位此类文本会产生不可接受的结果。
In addition, Java SE provides you with the 此外,Java SE还提供了FontMetrics
class, which enables you to obtain measurements of text rendered by a Font
object such as the height of a line of text in the font. FontMetrics
类,该类使您能够获取Font
对象呈现的文本的度量值,例如字体中文本行的高度。You can use this information to precisely position text in Java graphical applications. 您可以使用此信息在Java图形应用程序中精确定位文本。See Measuring Text for more information.有关更多信息,请参阅测量文本。