TTF 글꼴을 Python에서 사용하는 방법
이 가이드는 TTF 글꼴을 로드하고, TtfFont 속성을 검사하며, 테이블을 읽고, 가변 글꼴을 인스턴스화하고, Aspose.Font FOSS를 사용하여 Python용으로 다른 형식으로 변환하는 방법을 보여줍니다. TrueType 글꼴(.ttf)은 가장 일반적인 글꼴 형식입니다. Aspose.Font FOSS는 이를 TtfFont 인스턴스로 로드하여 테이블, 가변 글꼴 축, 글리프 데이터 및 변환 유틸리티에 접근할 수 있게 합니다.
단계별 가이드
1단계: 패키지 설치
아래 명령을 사용하여 Aspose.Font FOSS 패키지를 설치합니다:
git clone https://github.com/aspose-font-foss/Aspose.Font-FOSS-for-Python.git
cd Aspose.Font-FOSS-for-Python
pip install -e .2단계: 필요한 클래스 가져오기
FontLoader와 TtfFont을 가져와 TTF 글꼴을 로드하고 해당 타입된 속성에 접근하세요:
from aspose_font import FontLoader
from aspose_font import TtfFont3단계: TTF 글꼴 로드 및 검사
FontLoader.open()를 사용하여 TTF 파일을 로드하고 이름, 패밀리, 글리프 수와 같은 기본 속성을 읽으세요:
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
print(ttf_font.font_name)
print(ttf_font.font_family)
print(ttf_font.num_glyphs)4단계: kern 테이블 읽기
ttf_tables 속성에 접근하고 이름으로 kern 테이블이 존재하는지 확인하세요:
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
kern = ttf_font.ttf_tables.kern
print("kern present:", kern is not None)5단계: 가변 글꼴 인스턴스화
가변 글꼴 파일을 로드하고, is_variable를 확인한 다음, 축 딕셔너리를 사용하여 instantiate()을 호출해 정적 인스턴스를 생성하세요:
from aspose_font import FontLoader
from aspose_font import TtfFont
ttf_font: TtfFont = FontLoader.open("Roboto-VariableFont_wdth,wght.ttf")
if ttf_font.is_variable:
static = ttf_font.instantiate({"wght": 700.0})
print("Instantiated:", static.font_name)6단계: WOFF2로 변환
로드된 TTF 글꼴을 FontType.WOFF2을(를) FontConverter.convert()에 전달하여 WOFF2 형식으로 변환합니다:
from aspose_font import FontLoader
from aspose_font.converter import FontConverter # internal API — may change between releases
from aspose_font import FontType
font = FontLoader.open("Roboto-Regular.ttf")
woff2 = FontConverter.convert(font, FontType.WOFF2)일반적인 문제 및 해결책
AttributeError: Font has no attribute ttf_tables — 사용 ttf_font: TtfFont = FontLoader.open(...). 그 ttf_tables 속성은 TtfFont, 기본이 아니라 Font.
is_variable 가변 글꼴에서 False입니다 — 일부 가변 글꼴은 비표준 fvar 테이블 형식. 확인 ttf_font.fvar is not None 대안으로.
instantiate() raises KeyError — 축 태그는 등록된 4자리 태그여야 합니다 (예를 들어. "wght", "wdth"). 확인 ttf_font.axes 사용 가능한 태그를 확인하십시오.
자주 묻는 질문
TTF와 OTF의 차이점은 무엇인가요?
OTF와 TTF는 동일한 바이너리 컨테이너(SFNT)를 공유합니다. OTF 글꼴은 일반적으로 CFF 윤곽선을 사용하고 TTF 글꼴은 TrueType 2차 곡선을 사용합니다. 두 경우 모두 FontLoader.open()에 의해 로드됩니다.
TTF 글꼴에서 kern 페어를 가져올 수 있나요?
예. TtfFont.get_kern_pairs()을(를) 사용하여 KernPair 객체 목록을 가져옵니다.
라이브러리가 폰트 컬렉션(.ttc)을 지원하나요?
예. FontLoader.open(source, collection_index=N)을(를) 사용하여 TTC 컬렉션 파일에서 특정 폰트를 선택합니다.