Java에서 CFF 글꼴 로드 및 저장
Aspose.Font for Java를 사용하여 PostScript 기반 Compact Font Format (.cff) 파일을 프로그래밍 방식으로 처리합니다. 로컬 파일 경로 또는 메모리 내 바이트 배열에서 CFF 글꼴 바이너리를 열고, CffFont 클래스를 사용하여 글꼴 구조를 검사하며, 외부 종속성 없이 수정된 글꼴 데이터를 디스크 또는 스트림에 다시 저장합니다.
Compact Font Format (CFF)은 PostScript Type 1 글꼴 아웃라인을 압축하여 표현한 형식으로, PDF 문서, OpenType CFF (.otf) 글꼴 및 디지털 출판 워크플로에서 널리 사용됩니다. Aspose.Font for Java를 사용하면 개발자는 다음을 수행할 수 있습니다:
- 메모리 내 글꼴 처리: 임시 파일을 디스크에 기록하지 않고 데이터베이스 BLOB, PDF 스트림 또는 클라우드 스토리지에서 CFF 글꼴 바이트를 직접 로드합니다.
- 컴팩트한 스토리지 관리: 최적화된 문서 임베딩을 위해 적은 공간을 차지하는 PostScript 아웃라인 글꼴 구조를 조작합니다.
- 유연한 파일 작업:
FileSystemStreamSource또는ByteContentStreamSource를 사용하여 CFF 글꼴 파일을 읽고CffFont.save()를 통해 내보냅니다.
CFF 글꼴 처리 워크플로
디스크에서 CFF 로드 - (파일 경로 (.cff)) 및 FontDefinition + FileSystemStreamSource 사용
메모리에서 CFF 로드 - 바이너리 바이트 배열 (byte[]) 및 FontDefinition + ByteContentStreamSource 사용
CFF 데이터 검사/편집 - CffFont 객체 및 CffFont.getFontName(), CffFont.getNumGlyphs() 등을 사용
CFF를 디스크에 저장 - CffFont 객체 및 CffFont.getFontName(), CffFont.getNumGlyphs() 등을 사용
Java에서 디스크의 글꼴에서 CFF를 로드하는 방법은?
FontType.CFF 및 FileSystemStreamSource를 사용하여 FontDefinition을 정의함으로써 .cff 글꼴 파일을 로드합니다.
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.");
}
}바이트 배열 (`byte[]`)에서 CFF 글꼴을 로드하는 방법은?
글꼴을 메모리에 로드하고 데이터를 추출하거나 수정한 후, 글꼴을 로컬 디스크 스토리지에 다시 저장하거나 출력 바이트 스트림에 기록할 수 있습니다.
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);
}
}설치 및 설정
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. OpenType CFF (.otf)과 독립형 CFF (.cff)의 차이점은 무엇인가요?
독립형 .cff 파일에는 OpenType 테이블 래퍼 없이 원시 PostScript 글꼴 프로그램 데이터가 포함됩니다. CFF 아웃라인이 포함된 .otf 파일은 .cff 스트림을 표준 OpenType 테이블 구조(CFF, cmap, head 등)로 래핑합니다. Aspose.Font for Java는 독립형 CFF와 OpenType CFF 형식을 모두 지원합니다.
2. Java에서 CFF 글꼴을 TTF 또는 WOFF와 같은 다른 글꼴 형식으로 변환할 수 있나요?
예. CffFont 인스턴스로 로드한 후 Aspose.Font for Java는 글꼴 데이터를 TTF, WOFF 또는 WOFF2와 같은 다른 대상 형식으로 변환할 수 있는 렌더링 및 인코딩 기능을 제공합니다.
3. 수정된 CffFont를 OutputStream에 직접 저장할 수 있나요?
예. font.save(fileName)에 파일 경로 문자열을 전달하는 것 외에도 java.io.OutputStream을 전달하여 바이너리 글꼴 데이터를 네트워크 또는 메모리 스트림에 직접 기록할 수 있습니다.