소개
폰트 파일은 테이블을 중심으로 구성됩니다 — 각 테이블은 글리프 윤곽, 문자 매핑, 이름 메타데이터, 혹은 가로 메트릭과 같은 특정 데이터 범주를 저장합니다. 이러한 테이블을 이해하고 접근하는 것은 폰트 분석, 메타데이터 편집, 품질 보증, 포맷 변환과 같은 작업에 필수적입니다.
Aspose.Font FOSS for Python (aspose-font)는 깔끔한 Python API을 통해 OpenType 및 TrueType 테이블 전체에 직접 접근할 수 있게 합니다. 이 라이브러리는 MIT 라이선스로 배포되며 외부 종속성이 전혀 없어 라이선스 문제 없이 어떤 프로젝트에도 통합할 수 있습니다.
이 글에서는 라이브러리에서 제공되는 핵심 테이블 클래스들을 살펴봅니다 — 헤더 메타데이터 읽기부터 글리프 데이터 검사 및 이름 레코드 수정까지 — 모두 TtfFont의 TtfTableSet 컨테이너가 지원합니다.
주요 기능
폰트 로드 및 테이블 접근
FontLoader로 로드된 모든 폰트는 TtfFont의 ttf_tables 속성을 통해 파싱된 테이블을 노출합니다. TtfTableSet 객체는 각 표준 테이블에 대한 타입화된 접근을 제공하며, 맞춤형 또는 인식되지 않은 테이블의 경우 get_raw()를 통해 원시 바이트 접근도 가능합니다.
import aspose_font
font = aspose_font.FontLoader.open("MyFont.ttf")
tables = font.ttf_tables
# Typed table access
head = tables.head
name = tables.name
cmap = tables.cmap
# Raw bytes for any table tag
raw_os2 = tables.get_raw("OS/2")
Head 테이블 읽기
HeadTable은(는) 기본적인 전체 폰트 매개변수(단위당 em, 리비전, 경계 상자, 생성 날짜, 스타일 플래그)를 저장합니다. 이러한 값은 렌더링 엔진과 폰트 검증기에 중요합니다.
import aspose_font
font = aspose_font.FontLoader.open("MyFont.ttf")
head = font.ttf_tables.head
print(f"Units per em: {head.units_per_em}")
print(f"Revision: {head.font_revision}")
print(f"Bounding box: ({head.x_min}, {head.y_min}) to ({head.x_max}, {head.y_max})")
print(f"Mac style flags: {head.mac_style}")
print(f"Glyph data format: {head.glyph_data_format}")
Name 테이블 다루기
NameTable은(는) 인간이 읽을 수 있는 모든 메타데이터(폰트 패밀리 이름, 스타일, 저작권, 라이선스, 디자이너, 설명)를 보유합니다. 이름 ID로 레코드를 조회하거나, 현지화된 이름을 가져오거나, 리브랜딩을 위해 항목을 수정할 수 있습니다.
import aspose_font
font = aspose_font.FontLoader.open("MyFont.ttf")
name_table = font.ttf_tables.name
# Read the font family name (name ID 1)
family = name_table.best_name(1, ["en"])
print(f"Family: {family}")
# List all localized versions of the full name (name ID 4)
localized = name_table.localized_names(4)
for lang, value in localized.items():
print(f" {lang}: {value}")
# Replace the font family name
name_table.replace_name(1, "MyCustomFamily")
CMap 테이블 검사
CmapTable은(는) 유니코드 코드포인트를 글리프 ID에 매핑합니다. 다양한 플랫폼 및 인코딩 조합에 대한 여러 서브테이블을 포함할 수 있습니다. best_subtable()을(를) 사용하여 사용 사례에 가장 적합한 매핑을 얻으세요.
import aspose_font
font = aspose_font.FontLoader.open("MyFont.ttf")
cmap = font.ttf_tables.cmap
# Get the best Unicode subtable
subtable = cmap.best_subtable()
print(f"Platform: {subtable.platform_id}, Encoding: {subtable.encoding_id}")
# Look up the glyph ID for 'A'
gid = subtable.get_gid(ord("A"))
print(f"Glyph ID for 'A': {gid}")
OS/2 및 가로 메트릭 읽기
Os2Table은(는) OS/2 및 Windows 전용 메트릭(타이포그래픽 상승/하강, 임베딩 플래그, Panose 분류)을 제공합니다. HheaTable과 HmtxTable는 텍스트 셰이핑에 사용되는 가로 레이아웃 메트릭을 제공한다.
import aspose_font
font = aspose_font.FontLoader.open("MyFont.ttf")
os2 = font.ttf_tables.os2
hhea = font.ttf_tables.hhea
print(f"Typo ascender: {os2.s_typo_ascender}")
print(f"Typo descender: {os2.s_typo_descender}")
print(f"Embedding flags (fsType): {os2.fs_type}")
print(f"Panose: {os2.panose.hex()}")
print(f"Hhea ascender: {hhea.ascender}")
print(f"Max advance width: {hhea.advance_width_max}")
print(f"Number of hmetrics: {hhea.number_of_hmetrics}")
커닝 및 글리프 데이터
KernTable은(는) 수평 간격 조정을 위한 커닝 페어를 저장합니다. GlyfTable은(는) TrueType 글꼴에 대한 실제 글리프 외곽선 데이터를 포함합니다. 또한 PostTable를 사용하여 글리프 이름을 해석할 수 있습니다.
import aspose_font
font = aspose_font.FontLoader.open("MyFont.ttf")
# Kerning lookup
kern = font.ttf_tables.kern
if kern:
pairs = kern.pairs
print(f"Total kern pairs: {len(pairs)}")
# Look up a specific pair by glyph IDs
adjustment = kern.get(36, 57)
print(f"Kern adjustment: {adjustment}")
# Glyph name from post table
post = font.ttf_tables.post
if post:
glyph_name = post.glyph_name(0)
print(f"Glyph 0 name: {glyph_name}")
빠른 시작
git clone https://github.com/aspose-font-foss/Aspose.Font-FOSS-for-Python.git
cd Aspose.Font-FOSS-for-Python
pip install -e .import aspose_font
# Load any TTF or OTF font
font = aspose_font.FontLoader.open("MyFont.ttf")
tables = font.ttf_tables
# Print basic table info
print(f"Font: {font.font_name}")
print(f"Family: {font.font_family}")
print(f"Glyphs: {font.num_glyphs}")
print(f"Units/em: {tables.head.units_per_em}")
# Read name table entries
name_table = tables.name
for name_id in [0, 1, 2, 4, 5, 6]:
value = name_table.best_name(name_id, ["en"])
if value:
print(f" Name ID {name_id}: {value}")
# Check character coverage
cmap = tables.cmap
subtable = cmap.best_subtable()
for char in "ABCabc012":
gid = subtable.get_gid(ord(char))
print(f" '{char}' -> glyph {gid}")
지원되는 포맷
| 포맷 | 확장자 | 읽기 | 쓰기 |
|---|---|---|---|
| TTF | .ttf | ✓ | ✓ |
| OTF | .otf | ✓ | ✓ |
| CFF | .cff | ✓ | ✓ |
| WOFF | .woff | ✓ | ✓ |
| WOFF2 | .woff2 | ✓ | ✓ |
| EOT | .eot | ✓ | — |
| TYPE1 | .pfb/.pfa | ✓ | — |
오픈 소스 및 라이선스
Aspose.Font FOSS은(는) Python용 MIT 라이선스로 배포됩니다. 소스 코드는 GitHub에서 확인할 수 있으며, 패키지는 PyPI에 aspose-font 이름으로 게시됩니다. 개인 및 상업 프로젝트 모두에서 제한 없이 자유롭게 사용, 수정 및 배포할 수 있습니다.