Làm việc với TrueType Fonts
Làm việc với TrueType Fonts
Hướng dẫn này cho bạn biết làm thế nào để tải các phông chữ TrueType, kiểm tra dữ liệu bảng nhị phân của họ, đọc byte bảng nguyên chất và tạo ra các ví dụ tĩnh từ các font biến bằng cách sử dụng. TtfInstancer.
TrueType (TTF) phông chữ là định dạng phon desktop và web phổ biến nhất. cho Python triển khai TtfFont để truy cập dữ liệu bảng cụ thể TTF, glyph blob retrieval, và kiểm soát axit font biến động.
Tải về TTF Font
FontLoader.open() tự phát hiện định dạng TTF và trả về một TtfFont-typed object.The return instance provides access to TTF-specific features such as: ttf_tables, get_table_bytes(), và dữ liệu ax font biến.
from aspose_font.loader import FontLoader
font = FontLoader.open("Roboto-Regular.ttf")
print(font.font_name, font.num_glyphs, "glyphs")Tính năng TTF Table
TtfFont.ttf_tables Hiển thị A TtfTableSet với các thuộc tính được đặt tên cho bảng chung.
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
tables = ttf_font.ttf_tables
print("kern table present:", tables.kern is not None)Bảng Raw
Sử dụng TtfFont.get_table_bytes(tag) để thu thập dữ liệu bảng binary nguyên và TtfFont.set_table_bytes(tag, data) - Tạo các byte bảng được sửa đổi.
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
raw_cmap = ttf_font.get_table_bytes("cmap")Variable Font Instances (Những điều kiện văn bản khác nhau)
TtfFont.get_axis() Trả lại thông tin axis. TtfInstancer.instantiate() sản xuất a ví dụ tĩnh từ một chữ số biến tại các định dạng ax cụ thể.
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
from aspose_font.ttf.instancer import TtfInstancer
ttf_font: TtfFont = FontLoader.open("Roboto-VariableFont_wdth,wght.ttf")
# Get a static instance at wght=700, wdth=75
instance = TtfInstancer.instantiate(ttf_font, {"wght": 700.0, "wdth": 75.0})Mẹo và Thực hành Tốt nhất
- Luôn sử dụng
isinstance(font, TtfFont)Trước khi truy cậpttf_tableshoặcget_table_bytes() - Sử dụng
TtfTableSet.kernKiểm tra khả năng sẵn có trước khi đọc cặp kern - Tính năng phông chữ biến tạo ra một đối tượng phon mới; nguyên bản không thay đổi
Tóm tắt Tham chiếu API
| Class / Phương pháp | Description |
|---|---|
TtfFont.ttf_tables | Truy cập vào TTF bảng set |
TtfFont.get_table_bytes(tag) | Bytes nguyên liệu cho một bảng được đặt tên |
TtfFont.set_table_bytes(tag, data) | Viết các byte bảng được sửa đổi |
TtfFont.get_axis() | Thông tin axis font variable |
TtfInstancer.instantiate(font, coords) | tạo ra một trường hợp static |
TtfTableSet.kern | Bảng thang (không có nếu không) |