Tải phông chữ từ tệp, luồng và mảng byte trong Java

Tải và khởi tạo phông chữ TrueType (TTF), OpenType (OTF), WOFF/WOFF2, Embedded OpenType (EOT), Type1 và Compact Font Format (CFF) theo chương trình bằng Aspose.Font for Java. Tải phông chữ liền mạch từ đường dẫn trên ổ đĩa, tệp hệ thống, luồng byte hoặc mảng trong bộ nhớ mà không phụ thuộc vào Java AWT gốc hoặc việc cài đặt phông chữ phụ thuộc vào hệ điều hành.

 

Để mở và làm việc với bất kỳ phông chữ nào trong Aspose.Font for Java, cần có hai tham số chính:

  • Định dạng phông chữ (FontType): Xác định danh mục chung (ví dụ: FontType.TTF, FontType.Type1, FontType.CFF).
  • Nguồn nhị phân của phông chữ (StreamSource/FontFileDefinition): Đóng gói luồng nhị phân cơ bản bất kể luồng đó nằm trên ổ đĩa, trong cơ sở dữ liệu hay bên trong các luồng tài liệu (ví dụ: PDF/DOCX).
  • Lớp trung tâm FontDefinition liên kết các tham số này với nhau, cho phép Font.open(fontDefinition) trả về một đối tượng com.aspose.font.Font thống nhất.

Cách tải phông chữ bằng java.io.File?

Cách đơn giản nhất để tải phông chữ được lưu trữ trên ổ đĩa cục bộ bằng 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());
    }
}

Cách tải phông chữ bằng FileSystemStreamSource?

Cung cấp khả năng xử lý phần mở rộng tệp rõ ràng để xác định chính xác định dạng khi tải phông chữ từ các đường dẫn trên ổ đĩa.

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());
    }
}

Cách tải phông chữ trực tiếp từ nguồn luồng (không có FontFileDefinition)?

Truyền FileSystemStreamSource trực tiếp vào FontDefinition để đơn giản hóa quá trình tải.

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());
    }
}

Cách tải phông chữ từ mảng byte trong bộ nhớ (byte[])?

Lý tưởng để tải phông chữ được lấy từ cơ sở dữ liệu, yêu cầu mạng, bộ lưu trữ đám mây hoặc tài nguyên PDF/DOCX được nhúng bằng ByteContentStreamSource.

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());
    }
}

Cài đặt và thiết lập

Thêm Aspose.Font for Java vào dự án của bạn thông qua Maven.

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. Tại sao tôi phải truyền cả FontType và fileExtension khi tải phông chữ?

FontType đại diện cho họ định dạng tổng thể (ví dụ: FontType.TTF), nhưng có nhiều định dạng tệp khác nhau thuộc họ này (chẳng hạn như .ttf, .eot và .ttc). Việc chỉ định fileExtension đảm bảo thư viện sử dụng trình phân tích cú pháp chính xác cho định dạng container cụ thể đó.

2. Tôi có thể tải phông chữ trực tiếp từ các trường BLOB của cơ sở dữ liệu không?

Có. Truy xuất dữ liệu BLOB dưới dạng mảng byte[] và sử dụng ByteContentStreamSource để khởi tạo FontDefinition mà không cần ghi dữ liệu vào ổ đĩa.

3. Làm cách nào để tải phông chữ khi dữ liệu nhị phân bắt đầu tại một offset cụ thể bên trong tệp?

Sử dụng FontFileDefinition(String fileExtension, StreamSource streamSource, long offset) constructor, truyền vào offset tính bằng byte nơi dữ liệu phông chữ bắt đầu.