บทนำ
Embedded OpenType (EOT) คือรูปแบบฟอนต์ที่กะทัดรัดซึ่งพัฒนาขึ้นเพื่อฝังฟอนต์ TrueType ลงในหน้าเว็บ ไฟล์ EOT จะห่อหุ้มฟอนต์ TTF หรือ OTF ด้วยส่วนหัวขนาดคงที่ที่มีฟิลด์เมตาดาต้าอธิบายมิติของฟอนต์, เวอร์ชัน, ชุดอักขระ, และสิทธิ์การฝัง แม้ว่าการพัฒนาเว็บสมัยใหม่จะเปลี่ยนไปใช้ WOFF/WOFF2 อย่างกว้างขวางแล้ว แต่ไฟล์ EOT ยังคงมีอยู่ในเอกสารและแอปพลิเคชันองค์กรแบบเก่า.
Aspose.Font FOSS สำหรับ Python ให้การเข้าถึงภายในฟอนต์ EOT โดยตรงผ่านคลาส EotFont และคู่หู EotHeader. คุณสามารถโหลดไฟล์ EOT ด้วย FontLoader.open, อ่านฟิลด์ส่วนหัว, เดินทางผ่าน TtfFont ภายใน, และทำการแปลงผลลัพธ์กลับเป็นไบต์หรือเป็นไฟล์โดยใช้ Font.save. ไลบรารีนี้ถูกแจกจ่ายเป็น aspose-font-foss และได้รับใบอนุญาตภายใต้ MIT License. ไม่มีการพึ่งพาทางการค้าที่เป็นข้อบังคับและทำงานกับ runtime Python 3 มาตรฐาน.
คุณลักษณะหลัก
การโหลดฟอนต์ EOT
ใช้ FontLoader.open เพื่อโหลดไฟล์ EOT จากเส้นทางหรืออ็อบเจกต์ไบต์ วิธีนี้จะคืนค่าอินสแตนซ์ของ EotFont เมื่อพบลายเซ็น EOT:
from aspose_font import FontLoader, EotFont
font = FontLoader.open("fonts/sample.eot")
assert isinstance(font, EotFont)
print(font.font_name) # e.g. "Roboto"
print(font.font_family) # e.g. "Roboto"
print(font.num_glyphs) # number of glyphs in the inner font
FontLoader.open อ่านส่วนขยายของไฟล์และค่า magic ของส่วนหัวเพื่อระบุรูปแบบโดยอัตโนมัติ ไม่จำเป็นต้องระบุอาร์กิวเมนต์ font_type สำหรับไฟล์ฟอนต์ทั่วไป.
การอ่านเมตาดาต้า EotHeader
EotFont เปิดเผยฟิลด์ส่วนหัวโดยตรงเป็นคุณสมบัติบนคลาส เหล่านี้สอดคล้องกับฟิลด์ที่กำหนดในสเปคไบนารีของ EOT:
from aspose_font import FontLoader, EotFont, EotSerializer
# Load EOT — FontLoader returns an EotFont instance for .eot files
loaded = FontLoader.open("fonts/sample.eot")
# Serialize back to bytes to confirm the font loaded correctly
eot_bytes = EotSerializer.serialize(loaded)
print(f"EOT data loaded: {len(eot_bytes)} bytes")
# Base Font properties work on any loaded font
print("Font name:", loaded.font_name)
print("Glyphs :", loaded.num_glyphs)
คลาส EotFont เปิดเผยเมตาดาต้าส่วนหัวต่อไปนี้เป็นคุณสมบัติ:
eot_version(int) — เวอร์ชันรูปแบบ EOT (เช่น0x00020002)flags(int) — ธงการอนุญาตฝังตัวcharset(int) — ตัวระบุชุดอักขระ (0 = ANSI, 1 = DEFAULT)italic(int) — ธงเอียง (0 หรือ 1)weight(int) — น้ำหนักฟอนต์ (เช่น 400 สำหรับ Regular, 700 สำหรับ Bold)fs_type(int) — บิตการฝัง OS/2 fsType
ใช้ flags และ fs_type เพื่อกำหนดว่าฟอนต์อนุญาตให้ทำการย่อยหรือการฝังหรือไม่ ซึ่งเกี่ยวข้องเมื่อประมวลผลฟอนต์สำหรับสายงานการแจกจ่ายใหม่.
เข้าถึง TtfFont ภายใน
ทุก EotFont จะห่อหุ้มฟอนต์ TrueType มาตรฐาน คุณสมบัติ inner_font จะคืนค่าอินสแตนซ์ของ TtfFont ให้คุณเข้าถึงชุดตาราง TrueType ทั้งหมด:
from aspose_font import FontLoader
loaded = FontLoader.open("fonts/sample.eot")
# Metrics are accessible directly from the loaded font
metrics = loaded.metrics
print("Units per em :", metrics.units_per_em)
print("Ascender :", metrics.ascender)
print("Descender :", metrics.descender)
print("Line gap :", metrics.line_gap)
คุณสมบัติ inner_font บน EotFont จะคืนค่าอินสแตนซ์ของ TtfFont ที่ห่อหุ้ม ให้การเข้าถึงชุดตาราง TrueType ทั้งหมด, คู่ kern, และตารางการเข้ารหัส.
การแปลง EOT เป็นไบต์
EotSerializer.serialize จะแปลงวัตถุ Font ใด ๆ ให้เป็นสตริงไบต์ EOT ดิบ:
from aspose_font import FontLoader, EotFont, EotSerializer
font = FontLoader.open("fonts/sample.eot")
assert isinstance(font, EotFont)
eot_bytes = EotSerializer.serialize(font)
with open("output/sample_copy.eot", "wb") as fh:
fh.write(eot_bytes)
print(f"Serialized {len(eot_bytes)} bytes")
การแปลง TTF เป็น EOT
EotSerializer.serialize ยอมรับอินสแตนซ์ Font ใด ๆ, ดังนั้นคุณสามารถโหลด TTF และทำการซีเรียลไลซ์โดยตรงเป็นไบต์ EOT:
from aspose_font import FontLoader, EotFont, EotSerializer
ttf = FontLoader.open("fonts/Roboto-Regular.ttf")
eot_bytes = EotSerializer.serialize(ttf)
with open("output/Roboto-Regular.eot", "wb") as fh:
fh.write(eot_bytes)
reloaded = FontLoader.open("output/Roboto-Regular.eot")
assert isinstance(reloaded, EotFont)
print("Converted and verified:", reloaded.font_name)
เมตริกแบบอักษรและคู่เคิร์น
EotFont สืบทอดอินเทอร์เฟซ Font อย่างเต็มรูปแบบ, ดังนั้นเมตริกและการเข้ารหัสจึงพร้อมใช้งานโดยตรงโดยไม่ต้องผ่าน inner_font:
from aspose_font import FontLoader
loaded = FontLoader.open("fonts/sample.eot")
metrics = loaded.metrics
print(f"Ascender : {metrics.ascender}")
print(f"Descender: {metrics.descender}")
print(f"Line gap : {metrics.line_gap}")
print(f"Underline: {metrics.underline_position}")
for pair in loaded.get_kern_pairs():
print(f" Kern: {pair}")
เริ่มต้นอย่างรวดเร็ว
ติดตั้งไลบรารี:
pip install aspose-font-fossโหลดและตรวจสอบฟอนต์ EOT:
from aspose_font import FontLoader, EotFont, EotSerializer
loaded = FontLoader.open("sample.eot")
# Base font info
print(f"Font name : {loaded.font_name}")
print(f"Glyphs : {loaded.num_glyphs}")
# Metrics
metrics = loaded.metrics
print(f"UPM : {metrics.units_per_em}")
print(f"Ascender : {metrics.ascender}")
# Serialize the loaded font back to EOT bytes
eot_bytes = EotSerializer.serialize(loaded)
print(f"Serialized: {len(eot_bytes)} bytes")
รูปแบบที่รองรับ
| รูปแบบ | ส่วนขยาย | อ่าน | เขียน |
|---|---|---|---|
| TTF | .ttf | ✓ | — |
| OTF | .otf | ✓ | — |
| EOT | .eot | ✓ | — |
| CFF | .cff | ✓ | — |
| TYPE1 | .pfb/.pfa | ✓ | — |
| WOFF | .woff | ✓ | — |
| WOFF2 | .woff2 | ✓ | — |
รูปแบบทั้งหมดที่ระบุข้างต้นได้รับการสนับสนุนสำหรับการโหลดผ่าน FontLoader.open. การเขียนไปยังรูปแบบเฉพาะใช้ Font.save หรือ EotSerializer.serialize สำหรับ EOT.
ซอร์สเปิดและการให้สิทธิ์
Aspose.Font FOSS สำหรับ Python ถูกปล่อยภายใต้ใบอนุญาต MIT. คุณสามารถใช้ได้ในโครงการส่วนบุคคล, เชิงพาณิชย์, และโครงการโอเพนซอร์สโดยไม่มีค่าใช้จ่าย. โค้ดต้นฉบับพร้อมใช้งานบน GitHub, แพ็กเกจเป็น aspose-font-foss.