Introduction
Compact Font Format (CFF) เป็นรูปแบบการอธิบายไบนารีของกลิฟที่ใช้ใน OpenType แฟนท์ที่มีเครื่องออกแบบที่เข้ากับ PostScript. Aspose. Font FOSS สําหรับ Python เผยแพร่ ชุดของชั้นเรียนที่ได้รับการจัดตั้งภายใต้ aspose_font.cff namespace สําหรับการอ่าน และ การตรวจสอบโครงสร้างข้อมูล CFF: CffFont, CffCharset, CffEncoding, CffDict, CffIndex,และ Type2Interpreter. บทความนี้จะนําไปใช้วิธีการอัด CFF แฟนท์, การเดินท่องตารางภายในของพวกเขา และตีความโปรแกรม charstring โดยใช้ห้องสมุด.
แพ็คเกจมีลิเซนซิส MIT. ติดตั้งมันด้วย pip install aspose-font>=1.0.0.
คุณสมบัติหลัก
การอัตราค่า CFF Font
FontLoader.open() ตรวจสอบรูปแบบ CFF โดยอัตโนมัติ การผ่านการออกเสียงเป็นตัวแทน .cff ไฟล์หรือไฟล์ OTF ที่รวมตารางกรอบ CFF. อวัตถุที่กลับคือ a CffFont (สามารถเข้าถึงได้ผ่านทาง cff_font อสังหาริมทรัพย์บน TtfFont สําหรับไฟล์ OTF).
from aspose_font.loader import FontLoader
# Load a standalone CFF file
font = FontLoader.open("OpenSans.cff")
# Load a standalone CFF file (auto-detected from format)
font = FontLoader.open("OpenSans.cff")
print(font.font_name, font.num_glyphs)
CFF Charset การนํารหัส Glyph มาใช้กับชื่อ
CffCharset แผนที่ ID ไกลิฟ (GIDs) เป็นชื่อไกลีฟ PostScript. ใช้ standard() ไปยัง รับ ISO เซ็นเตอร์ตัวอักษร Adobe หรือ from_reader() การพาร์สเซ็ตตัวอักษรจากไบนารี ข้อมูล CFF. name_for() วิธีการคืนชื่อ PostScript สําหรับ GID และ gid_for() ทําการค้นหากลับหลัง.
from aspose_font.cff.charset import CffCharset
charset = CffCharset.standard()
print(charset.name_for(1)) # ".notdef" or similar
print(charset.gid_for("A")) # GID for the PostScript name "A"
การรหัส CFF Unicode-to-GID แผนที่
CffEncoding จัดการตารางโค้ด CFF ซึ่งนํารหัสตัวอักษรมาใช้กับไอดีกลิฟฟ์. standard() กลับรหัสมาตรฐาน, expert() กลับรหัสเชี่ยวชาญ และ from_reader() การพารัสการรหัสที่กําหนดเองจากข้อมูลไบนารี CFF. ใช้ unicode_to_gid() ไปยัง ดู GID โดยจุดรหัส Unicode และ get_all_codepoints() เพื่อการนับจํานวนทั้งหมดที่ถูกแผนภาพ จุดรหัส.
from aspose_font.cff.encoding import CffEncoding
encoding = CffEncoding.standard()
gid = encoding.unicode_to_gid(0x41) # Unicode for 'A'
all_codepoints = encoding.get_all_codepoints()
print(f"Standard encoding covers {len(all_codepoints)} code points")
การพารัสคําศัพท์ CFF
แฟนต์ CFF สะกัดข้อมูลเมตา-เดทระดับสูงและส่วนตัวในวัสดุคําศัพท์. CffDict.from_bytes() พาราส์บล็อบ CFF DICT และให้ข้อมูลคุณ get() และ set() การเข้าถึงโดยคีย์ผู้ใช้งาน. TopDict.from_dict() เป็นพิเศษสําหรับวัสดุ CFF ระดับสูงสุด และ PrivateDict.from_dict() ทําเช่นเดียวกันกับ DICT ของเอกชน.
from aspose_font.cff.dict_ import CffDict
# Parse a raw CFF DICT from bytes
raw_dict_bytes = b"..." # CFF-encoded DICT bytes
d = CffDict.from_bytes(raw_dict_bytes)
vals = d.get(21) # retrieve values for operator 21 (PostScript CharstringType)
โครงสร้างอัตราสรรหา CFF
CffIndex เป็นโครงสร้างข้อมูล CFF INDEX แอเรรย์ที่มีค่ากําหนดความยาวของตัวแปร- ความยาว byte string ที่ใช้สําหรับ charstrings, subrs และ name table. from_reader() การพาร์ส INDEX จากตัวอ่านไบนารี และผลที่สามารถจดทะเบียนได้โดยอินเด็กซ์จํานวนเต็ม.
from aspose_font.cff.index import CffIndex
from aspose_font._io import BinaryReader # internal API — may change between releases
import io
raw_index = b"..." # raw CFF INDEX bytes
reader = BinaryReader(io.BytesIO(raw_index))
index = CffIndex.from_reader(reader)
print(f"INDEX entries: {len(index)}")
การตีความ Charstring ประเภท 2
Type2Interpreter ใช้โปรแกรม charstring แบบ 2 (แบบ PostScript ที่คอมพั๊ต) การใช้รูปแบบใน CFF แฟนท์) และผลิตการออกเสียงของอักษรที่มีความสามารถในการสร้างความรู้ GlyphPath การเรียกคําสั่งแบบกราฟ interpret() กับ charstring bytes และคําศัพท์ส่วนตัวที่เกี่ยวข้อง. ทําให้คุณสามารถสกัดลักษณะ glyph แร่จาก CFF charstrings โดยไม่ต้องเต็ม อธิบาย PostScript.
from aspose_font.cff.type2 import Type2Interpreter
interpreter = Type2Interpreter()
glyph_path = interpreter.interpret(charstring_bytes, private_dict)
for cmd in glyph_path.commands:
print(type(cmd).__name__, cmd)
เริ่มต้นอย่างรวดเร็ว
pip install aspose-font>=1.0.0
from aspose_font.loader import FontLoader
# Load an OTF with embedded CFF outlines
font = FontLoader.open("OpenSans-Regular.otf")
glyph_id = font.encoding.unicode_to_gid(0x41)
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
print(f"Path commands for 'A': {len(glyph.path.commands)}")
รูปแบบที่สนับสนุน
| Format | Extension | Read | Write |
|---|---|---|---|
| CFF | .cff | ✓ | ✓ |
| OTF | .otf | ✓ | ✓ |
| TTF | .ttf | ✓ | ✓ |
| WOFF | .- โว๊ฟ | ✓ | ✓ |
| WOFF2 | .woff2 | ✓ | ✓ |
| EOT | .eot | ✓ | ✓ |
| TYPE1 | .pfb / .pfa | ✓ | ✓ |
หลักสูตรและใบอนุญาต
Aspose.Font FOSS for Python is MIT licensed. Source code is on GitHub at aspose-font-foss/Aspose.Font-FOSS-for-Python แปลว่าการใช้งานที่มีความสะดวกสบาย และมีความสามารถในการเขียนแบบพีชั่น. ติดตั้งจาก PyPI เป็น aspose-font.