在 Java 中从文件、流和字节数组加载字体

使用 Aspose.Font for Java 以编程方式加载和初始化 TrueType (TTF)、OpenType (OTF)、WOFF/WOFF2、Embedded OpenType (EOT)、Type1 和 Compact Font Format (CFF) 字体。无需依赖原生 Java AWT 或操作系统相关的字体安装,即可从磁盘路径、系统文件、字节流或内存中的数组无缝加载字体。

 

要在 Aspose.Font for Java 中打开和处理任何字体,需要两个关键参数:

  • 字体格式 (FontType): 标识常规类别(例如 FontType.TTF、FontType.Type1、FontType.CFF)。
  • 字体二进制源 (StreamSource/FontFileDefinition): 封装底层二进制流,无论该流位于磁盘、数据库中,还是文档流(例如 PDF/DOCX)内。
  • 核心类 FontDefinition 将这些参数绑定在一起,使 Font.open(fontDefinition) 能够返回统一的 com.aspose.font.Font 对象。

如何使用 java.io.File 加载字体?

使用 FontFileDefinition(File file) 加载存储在本地磁盘驱动器上的字体,这是最直接的方法。

import com.aspose.font.*;
import java.io.File;
import java.nio.file.Paths;

public class LoadFontFromFile {
    public static void main(String[] args) {
        // Step 1: Define path to font file
        String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();

        // Step 2: Create FontFileDefinition from java.io.File
        FontFileDefinition fileDef = new FontFileDefinition(new File(fontPath));

        // Step 3: Create FontDefinition (extension is auto-detected from FontFileDefinition)
        FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);

        // Step 4: Open and load font instance
        Font font = Font.open(fontDef);

        System.out.println("Successfully loaded font: " + font.getFontName());
    }
}

如何使用 FileSystemStreamSource 加载字体?

在从磁盘路径加载字体时提供明确的文件扩展名处理,以便准确检测字体格式。

import com.aspose.font.*;
import java.nio.file.Paths;

public class LoadFontFromStreamSource {
    public static void main(String[] args) {
        String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();

        // Pass 'ttf' explicitly to ensure proper format mapping
        FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));
        FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);

        Font font = Font.open(fontDef);
        System.out.println("Loaded Font Name: " + font.getFontName());
    }
}

如何从直接流源加载字体(不使用 FontFileDefinition)?

将 FileSystemStreamSource 直接传递给 FontDefinition,以简化加载过程。

import com.aspose.font.*;
import java.nio.file.Paths;

public class LoadFontDirectStream {
    public static void main(String[] args) {
        String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();

        // Provide FontType, file extension, and StreamSource directly
        FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));

        Font font = Font.open(fontDef);
        System.out.println("Loaded Font Name: " + font.getFontName());
    }
}

如何从内存中的字节数组 (byte[]) 加载字体?

非常适合使用 ByteContentStreamSource 加载从数据库、网络请求、云存储或嵌入式 PDF/DOCX 资源中获取的字体。

import com.aspose.font.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class LoadFontFromByteArray {
    public static void main(String[] args) throws Exception {
        String fontPath = Paths.get("data", "Montserrat-Regular.ttf").toString();

        // Read font binary data into byte array
        byte[] fontBytes = Files.readAllBytes(Paths.get(fontPath));

        // Initialize FontDefinition with ByteContentStreamSource
        FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new ByteContentStreamSource(fontBytes));

        // Open font from memory
        Font font = Font.open(fontDef);

        System.out.println("Loaded Font from Memory: " + font.getFontName());
    }
}

安装和设置

通过 Maven 将 Aspose.Font for Java 添加到您的项目中。

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-font</artifactId>
    <version>Latest</version>
</dependency>

FAQ

1. 加载字体时,为什么必须同时传递 FontType 和 fileExtension?

FontType 表示总体格式系列(例如 FontType.TTF),但该系列包含多个不同的文件格式(例如 .ttf、.eot 和 .ttc)。指定 fileExtension 可确保库针对特定的容器格式使用正确的解析器。

2. 我可以直接从数据库 BLOB 字段加载字体吗?

可以。将 BLOB 数据作为 byte[] 数组获取,然后使用 ByteContentStreamSource 初始化 FontDefinition,无需将数据写入磁盘。

3. 如果二进制数据从文件中的特定偏移量开始,我该如何加载字体?

使用 FontFileDefinition(String fileExtension, StreamSource streamSource, long offset) constructor,并传入字体数据开始位置的字节偏移量。