文本——Java 2D Oracle官方教程翻译(双语对照)

本文翻译自Oracle官方教程
<<Previous • Trail • Next>>

文本


The Java 2D API has various text rendering capabilities including methods for rendering strings and entire classes for setting font attributes and performing text layout.

Java 2D API拥有渲染各式文本的能力,包括渲染字符串的方法以及整套用于设置字体属性和展示文本布局的类。


If you just want to draw a static text string, the most direct way to render it directly through the Graphics class by using the drawString method. To specify the font, you use the setFont method of the Graphics class.

如果你只是想画出一个静态文本串,最直接的办法就是用Graphics类的drawString方法渲染它。要指定字体就用Graphics类的setFont方法。


If you want to implement your own text-editing routines or need more control over the layout of the text than the text components provide, you can use the Java 2D text layout classes in the java.awt.font package.

如果你想要实现自己的文本编辑例程,或者文本组件在布局上所提供的控制无法满足你的需求,那么你可以使用java.awt.font包当中的Java 2D文本布局类。


字体


The shapes that a font uses to represent the characters in a string are called glyphs.

被字体用来表示字符的图形称为 字形

A particular character or combination of characters might be represented as one or more glyphs. For example, á might be represented by two glyphs, whereas the ligature fi might be represented by a single glyph.

一个特定的字符或字符组合可能由一个或多个字形来表示。比如"á"是一个字符,但可能是由两个字形来表示的。而连字”fi“是两个字符,却可能由一个单独的字形来表示。


A font can be thought of as a collection of glyphs.

字体可以看作字形的集合。

A single font might have many faces, such as italic and regular.

单独一套字体可能有许多面,比如斜体和正规体。

All of the faces in a font have similar typographic features and can be recognized as members of the same family.

一套字体的所有面都有相似的版式特征,可以认为是同一家族的成员。

In other words, a collection of glyphs with a particular style form a font face. A collection of font faces forms a font family. The collection of font families forms the set of fonts that are available on the system.

换句话说,带有某个特定样式的一组 字形 形成一套 字体面。一组 字体面 形成一个 字体家族。众多 字体家族 聚集起来形成可以用在系统上的 字体 集合。
(font family的家族成员不是font,而是font face,字面上容易混)


When you are using the Java 2D API, you specify fonts by using an instance of Font.

Java 2D API使用Font实例指定字体。

You can determine what fonts are available by calling the static method GraphicsEnvironment.getLocalGraphicsEnvironment and then querying the returned GraphicsEnvironment. The getAllFonts method returns an array that contains Font instances for all of the fonts available on the system. The getAvailableFontFamilyNames method returns a list of the available font families.

想确定有哪些字体可用的话,你可以访问静态方法GraphicsEnvironment.getLocalGraphicsEnvironment,然后在返回的GraphicsEnvironment中查询。getAllFonts方法返回一个数组,包含系统中所有可用字体对应的Font实例。getAvailableFontFamilyNames方法返回的数组包含可用的字体家族的名字。

{
	GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
	Font[] allFonts = localGraphicsEnvironment.getAllFonts();
	String[] availableFontFamilyNames = localGraphicsEnvironment.getAvailableFontFamilyNames();
}

文本布局


Before text can be displayed, it must be laid out so that the characters are represented by the appropriate glyphs in the proper positions. The following are two Java 2D mechanisms for managing text layout:

文本在显示之前需要先布局,这样字符才能被合适的字形在适当的位置表示出来。下面是Java 2D管理布局的两套机制:


  • The TextLayout class manages text layout, highlighting, and hit detection. The facilities provided by TextLayout handle the most common cases, including strings with mixed fonts, mixed languages, and bidirectional text.
  • TextLayout类管理着文本布局,高亮显示和碰撞检测。TextLayout所提供的设施负责处理最常见的情况,包括混合字体字符串,混合语言和双向文本。
  • You can create the own GlyphVector objects by using the Font class and then rendering each GlyphVector object through the Graphics2D class. Thus, you can completely control how text is shaped and positioned.
  • 你可以使用Font类创建自己的GlyphVector对象,然后通过Graphics2D类渲染它们。这样你就可以完全控制文本如何被塑造和放置了。

文本的渲染提示


The Java 2D API enables you to control the quality of shapes and text rendering by using rendering hints. Rendering hints are encapsulated by the java.awt.RenderingHints class.

Java 2D API使你能够用渲染提示控制图形和文本的渲染质量。渲染提示是由java.awt.RenderingHints类封装的。


As applied to text, this capability is used for antialiasing (which is also known as an smooth edges). For example, the KEY_TEXT_ANTIALIASING hint enables you to control the antialiasing of text separately from the antialiasing of other shapes.

具体到文本方面,该能力服务于抗锯齿。例如KEY_TEXT_ANTIALIASING这个渲染提示使你能够单独控制文本的抗锯齿而不影响其他图形。

To learn more about rendering hints see the Controlling Rendering Quality lesson.

想进一步了解渲染提示可以参考 渲染质量控制 课程。


<<Previous • Trail • Next>>

发布了9 篇原创文章 · 获赞 0 · 访问量 643

猜你喜欢

转载自blog.csdn.net/realcfuture/article/details/103975153
今日推荐