Documentation

The Java™ Tutorials
Hide TOC
Physical and Logical Fonts物理字体和逻辑字体
Trail: 2D Graphics
Lesson: Working with Text APIs

Physical and Logical Fonts物理和逻辑字体

There are two types of fonts: physical fonts and logical fonts. 字体有两种类型:物理字体和逻辑字体。Physical fonts are the actual font libraries consisting of, for example, TrueType or PostScript Type 1 fonts. 物理字体是由TrueType或PostScript Type 1字体等组成的实际字体库。The physical fonts may be Time, Helvetica, Courier, or any number of other fonts, including international fonts. 物理字体可以是Time、Helvetica、Courier或任何数量的其他字体,包括国际字体。Logical fonts are the following five font families: Serif, SansSerif, Monospaced, Dialog, and DialogInput. 逻辑字体有以下五种字体系列:衬线、衬线、等距、对话框和对话框输入。These logical fonts are not actual font libraries. 这些逻辑字体不是实际的字体库。Instead, the logical font names are mapped to physical fonts by the Java runtime environment.相反,Java运行时环境将逻辑字体名称映射到物理字体。

This section helps you determine which type of font to use in your application. 本节帮助您确定应用程序中使用的字体类型。It covers the following topics:它涵盖以下主题:

Physical Fonts物理字体

Physical fonts are the actual font libraries containing glyph data and tables to map from character sequences to glyph sequences, using a font technology such as TrueType or PostScript Type 1. 物理字体是实际的字体库,包含字形数据和表格,可使用字体技术(如TrueType或PostScript Type 1)从字符序列映射到字形序列。To obtain the names of all available font families installed in your system, call the following:要获取系统中安装的所有可用字体系列的名称,请调用以下命令:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();

The FontSelector sample program (available in FontSelector.java) illustrates how to locate and select these fonts.FontSelector示例程序(在FontSelector.java中提供)说明了如何定位和选择这些字体。


Note: Applications should not assume that any particular physical font is present. 应用程序不应假定存在任何特定的物理字体。However, the logical fonts are a safe choice because they are always present. 但是,逻辑字体是一个安全的选择,因为它们总是存在的。See Logical Fonts for more information. 有关详细信息,请参阅逻辑字体


Note:  If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.如果您没有看到小程序正在运行,则至少需要安装Java SE开发工具包(JDK)7版本。

Lucidia Fonts萤光虫字体

Oracle's JREs contain this family of physical fonts, which is also licensed for use in other implementations of the Java platform. Oracle的JRE包含这一系列物理字体,这些字体也被许可用于Java平台的其他实现。These fonts are physical fonts, but do not depend on the host operating system.这些字体是物理字体,但不依赖于主机操作系统。

Applications using these fonts can achieve the same look wherever these fonts are available. 使用这些字体的应用程序可以在任何有这些字体的地方获得相同的外观。Also, these fonts cover a large range of languages (especially European and Middle Eastern), so you can create fully multilingual applications for the supported languages. 此外,这些字体涵盖了多种语言(特别是欧洲和中东),因此您可以为支持的语言创建完全多语言的应用程序。However, these fonts may not be available in all JREs. 但是,这些字体可能并非在所有JRE中都可用。Also, they currently do not cover the complete Unicode character set; in particular, Chinese, Japanese, and Korean are not supported.此外,它们目前不包括完整的Unicode字符集;特别是,不支持中文、日文和韩文。

Bundling Physical Fonts with Your Application将物理字体与应用程序绑定

Sometimes, an application cannot depend on a font being installed on the system, usually because the font is a custom font that is not otherwise available. 有时,应用程序无法依赖于系统上安装的字体,这通常是因为该字体是一种自定义字体,在其他情况下不可用。In this case, you must bundle the font files with your application.在这种情况下,必须将字体文件与应用程序捆绑在一起。

Use one of these methods to create a Font object from an existing physical font:使用以下方法之一从现有物理字体创建Font对象:

Font java.awt.Font.createFont(int fontFormat, InputStream in);
Font java.awt.Font.createFont(int fontFormat, File fontFile);

To create a Font object from a TrueType font, the formal parameter fontFormat must be the constant Font.TRUETYPE_FONT. The following example creates a Font object from the TrueType font file A.ttf:

Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));

Accessing the font directly from a file is simpler and more convenient. 直接从文件访问字体更简单、更方便。However, you might require an InputStream object if your code is unable to access file system resources, or if the font is packaged in a Java Archive (JAR) file along with the rest of the application or applet.但是,如果代码无法访问文件系统资源,或者字体与应用程序或小程序的其余部分一起打包在Java归档(JAR)文件中,则可能需要InputStream对象。

The createFont method creates a new Font object with a point size of 1 and style PLAIN. This base font can then be used with the Font.deriveFont methods to derive new Font objects with varying sizes, styles, transforms and font features. For example:

try {
     //Returned font is of pt size 1
     Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));

     //Derive and return a 12 pt version:
     //Need to use float otherwise
     //it would be interpreted as style

     return font.deriveFont(12f);

} catch (IOException|FontFormatException e) {
     // Handle exception
}

It is important to use the deriveFont method because fonts that are created by an application are not part of the set of fonts known to the underlying font system. Because the deriveFont method works from the originally created font, it does not have this limitation.

The solution for this problem is to register the created font with the graphics environment. For example:

try {
     GraphicsEnvironment ge = 
         GraphicsEnvironment.getLocalGraphicsEnvironment();
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));
} catch (IOException|FontFormatException e) {
     //Handle exception
}

After registering the font with the graphics environment, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.

Logical Fonts

Java SE defines the following five logical font families:

These fonts are available on any Java platform and can be thought of as aliases for some underlying font that has the properties implied by its name. A Serif font is a font similar to Times New Roman, which is commonly used in print. A Sans Serif font is more typical for onscreen use.

These fonts can be customized for the locale of the user. In addition these fonts support the widest range of code points (Unicode characters).

Apart from the family, fonts have other attributes, the most important of which are style and size. Styles are Bold and Italic.

The default font used by the Java 2D API is 12 pt Dialog. This font is a typical point size for reading text on a normal 72–120 DPI display device. An application can create an instance of this font directly by specifying the following:

Font font = new Font("Dialog", Font.PLAIN, 12);

Advantages and Disadvantages of Using Physical and Logical Fonts

Physical fonts enable an application take full advantage of all available fonts, to accomplish both different text appearances and maximum language coverage. However, it is substantially more difficult to create applications that use physical fonts.

Bundling physical fonts with your application enables you to create applications that have the same look everywhere, and to have full control over which applications you want to support. However, bundled fonts may be quite big, especially if you want your applications to support Chinese, Japanese, and Korean. In addition, you may need to resolve licensing issues.

Logical font names are guaranteed to work anywhere, and they enable text rendering in at least the language that the host operating system is localized for (often a much larger range of languages). However, The physical fonts used for rendering the text vary between different implementations, host operating systems, and locales, so an application can not achieve the same look everywhere. Also, the mapping mechanisms occasionally limit the range of characters that can be rendered. The latter used to be a big problem on JRE versions before 5.0: for example, Japanese text could only be rendered on Japanese localized host operating systems, not on other localized systems even if Japanese fonts have been installed. For applications using 2D font rendering, this problem is much rarer with JRE version 5.0 and later, because the mapping mechanism now generally recognizes and uses fonts for all supported writing systems if they are installed.

Font Configuration Files

The Java SE runtime environment uses font configuration files to map logical font names to physical fonts. There are several files to support different mappings depending on the host operating system version. The files are located in the lib directory within the JRE installation. You can edit or create your own font configuration files to adjust the mappings to your particular system setup. See Font Configuration Files for more information.


Previous page: Text Layout Concepts
Next page: Measuring Text