โหลดและบันทึกฟอนต์ CFF ใน Java

ประมวลผลไฟล์ Compact Font Format (.cff) ที่ใช้ PostScript ด้วยโปรแกรมโดยใช้ Aspose.Font for Java เปิดไบนารีฟอนต์ CFF จากพาธไฟล์ภายในเครื่องหรืออาร์เรย์ไบต์ในหน่วยความจำ ตรวจสอบโครงสร้างฟอนต์โดยใช้คลาส CffFont และบันทึกข้อมูลฟอนต์ที่แก้ไขแล้วกลับไปยังดิสก์หรือสตรีมโดยไม่ต้องพึ่งพาไลบรารีภายนอก

 

Compact Font Format (CFF) เป็นรูปแบบการแสดงเส้นโครงร่างของฟอนต์ PostScript Type 1 ที่มีขนาดกะทัดรัด ซึ่งใช้งานอย่างแพร่หลายในเอกสาร PDF ฟอนต์ OpenType CFF (.otf) และเวิร์กโฟลว์การเผยแพร่ดิจิทัล ด้วย Aspose.Font for Java นักพัฒนาสามารถ:

  • การประมวลผลฟอนต์ในหน่วยความจำ: โหลดไบต์ฟอนต์ CFF โดยตรงจาก BLOB ของฐานข้อมูล สตรีม PDF หรือพื้นที่จัดเก็บบนคลาวด์ โดยไม่ต้องเขียนไฟล์ชั่วคราวลงดิสก์
  • การจัดการพื้นที่จัดเก็บแบบกะทัดรัด: จัดการโครงสร้างฟอนต์เส้นโครงร่าง PostScript ที่ใช้พื้นที่น้อย เพื่อการฝังลงในเอกสารอย่างมีประสิทธิภาพ
  • การดำเนินการกับไฟล์ที่ยืดหยุ่น: อ่านไฟล์ฟอนต์ CFF โดยใช้ FileSystemStreamSource หรือ ByteContentStreamSource และส่งออกผ่าน CffFont.save()

เวิร์กโฟลว์การประมวลผลฟอนต์ CFF

โหลด CFF จากดิสก์ - ใช้ (พาธไฟล์ (.cff)) และ FontDefinition + FileSystemStreamSource

โหลด CFF จากหน่วยความจำ - ใช้อาร์เรย์ไบต์ไบนารี (byte[]) และ FontDefinition + ByteContentStreamSource

ตรวจสอบ/แก้ไขข้อมูล CFF - ใช้อ็อบเจ็กต์ CffFont และ CffFont.getFontName(), CffFont.getNumGlyphs() เป็นต้น

บันทึก CFF ลงดิสก์ - ใช้อ็อบเจ็กต์ CffFont และ CffFont.getFontName(), CffFont.getNumGlyphs() เป็นต้น

จะโหลด CFF จากฟอนต์บนดิสก์ใน Java ได้อย่างไร?

โหลดไฟล์ฟอนต์ .cff โดยกำหนด FontDefinition ด้วย FontType.CFF และ FileSystemStreamSource

import com.aspose.font.*;

public class LoadCffFontFromDisk {
    public static void main(String[] args) {
        // Step 1: Specify path to the .cff font file
        String fileName = "path/to/OpenSans-Regular.cff";

        // Step 2: Initialize FontDefinition with CFF format and FileSystemStreamSource
        FontDefinition fd = new FontDefinition(
            FontType.CFF, 
            new FontFileDefinition("cff", new FileSystemStreamSource(fileName))
        );

        // Step 3: Open the CFF font
        CffFont font = (CffFont) Font.open(fd);

        // Step 4: Verify font properties
        System.out.println("Font Name: " + font.getFontName());
        System.out.println("Glyph Count: " + font.getNumGlyphs());
        System.out.println("CFF Font successfully loaded from disk.");
    }
}

จะโหลดฟอนต์ CFF จากอาร์เรย์ไบต์ (`byte[]`) ได้อย่างไร?

หลังจากโหลดฟอนต์เข้าสู่หน่วยความจำและดำเนินการแยกหรือแก้ไขข้อมูลแล้ว คุณสามารถบันทึกฟอนต์กลับไปยังพื้นที่จัดเก็บบนดิสก์ภายในเครื่องหรือเขียนลงในสตรีมไบต์เอาต์พุตได้

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

public class LoadCffFontFromByteArray {
    public static void main(String[] args) throws Exception {
        String fileName = "path/to/OpenSans-Regular.cff";

        // Step 1: Read font file bytes into memory
        byte[] fontMemoryData = Files.readAllBytes(Paths.get(fileName));

        // Step 2: Create FontDefinition using ByteContentStreamSource
        FontDefinition fd = new FontDefinition(
            FontType.CFF, 
            new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData))
        );

        // Step 3: Open font instance from byte array
        CffFont font = (CffFont) Font.open(fd);

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

จะแก้ไขและบันทึกฟอนต์ CFF ลงดิสก์ได้อย่างไร?

หลังจากโหลดและทำงานกับอินสแตนซ์ CffFont แล้ว ให้เขียนไฟล์ฟอนต์ไปยังตำแหน่งใหม่โดยใช้เมธอด save()

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

public class SaveCffFontToDisk {
    public static void main(String[] args) throws Exception {
        String inputPath = "path/to/OpenSans-Regular.cff";
        String outputPath = "path/to/OpenSans-Regular_out.cff";

        // Step 1: Load font from byte array
        byte[] fontMemoryData = Files.readAllBytes(Paths.get(inputPath));
        FontDefinition fd = new FontDefinition(
            FontType.CFF, 
            new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData))
        );
        CffFont font = (CffFont) Font.open(fd);

        System.out.println("Loaded CFF Font: " + font.getFontName());

        // Step 2: Work with font data (inspect metrics, glyphs, or update properties)

        // Step 3: Save CFF font back to disk
        font.save(outputPath);

        System.out.println("CFF Font successfully saved to: " + outputPath);
    }
}

การติดตั้งและการตั้งค่า

เพิ่ม Aspose.Font for Java ลงในโปรเจกต์ของคุณผ่าน 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. OpenType CFF (.otf) และ CFF แบบสแตนด์อโลน (.cff) แตกต่างกันอย่างไร?

ไฟล์ .cff แบบสแตนด์อโลนประกอบด้วยข้อมูลโปรแกรมฟอนต์ PostScript ดิบโดยไม่มี wrapper ของตาราง OpenType ส่วนไฟล์ .otf ที่มีเส้นขอบ CFF จะห่อหุ้มสตรีม .cff ไว้ภายในโครงสร้างตาราง OpenType มาตรฐาน (CFF, cmap, head เป็นต้น) Aspose.Font for Java รองรับทั้งรูปแบบ CFF แบบสแตนด์อโลนและ OpenType CFF

2. สามารถแปลงฟอนต์ CFF เป็นรูปแบบฟอนต์อื่น เช่น TTF หรือ WOFF ใน Java ได้หรือไม่?

ได้ เมื่อโหลดเข้าสู่อินสแตนซ์ CffFont แล้ว Aspose.Font for Java มีความสามารถด้านการเรนเดอร์และการเข้ารหัสที่ช่วยให้สามารถแปลงข้อมูลฟอนต์เป็นรูปแบบเป้าหมายอื่น เช่น TTF, WOFF หรือ WOFF2

3. สามารถบันทึก CffFont ที่แก้ไขแล้วโดยตรงไปยัง OutputStream ได้หรือไม่?

ได้ นอกจากการส่งสตริงพาธไฟล์ไปยัง font.save(fileName) แล้ว คุณยังสามารถส่ง java.io.OutputStream เพื่อเขียนข้อมูลฟอนต์ไบนารีโดยตรงไปยัง network หรือ memory streams ได้