บทนำ
Embedded OpenType (EOT) เป็นรูปแบบฟอนต์ขนาดกะทัดรัดที่พัฒนาขึ้นเพื่อฝังฟอนต์ TrueType ในหน้าเว็บ ไฟล์ EOT จะห่อหุ้มฟอนต์ TTF หรือ OTF ด้วยส่วนหัวขนาดคงที่ซึ่งมีฟิลด์เมตาดาต้าอธิบายมิติของฟอนต์, เวอร์ชัน, ชุดอักขระ, และสิทธิ์การฝัง แม้การพัฒนาเว็บสมัยใหม่จะย้ายไปใช้ WOFF/WOFF2 อย่างกว้างขวางแล้ว ไฟล์ EOT ยังพบใช้งานอยู่ในเอกสารเก่าและแอปพลิเคชันระดับองค์กร.
Aspose.Font FOSS สำหรับ Python ให้การเข้าถึงภายในฟอนต์ EOT โดยตรงผ่านคลาส EotFont และคู่หู EotHeader. คุณสามารถโหลดไฟล์ EOT ด้วย FontLoader.open, อ่านฟิลด์หัวเรื่อง, เดินทางผ่าน TtfFont ภายใน, และทำการซีเรียลไลซ์ผลลัพธ์กลับเป็นไบต์หรือเป็นไฟล์โดยใช้ Font.save. ไลบรารีนี้ถูกเผยแพร่บน PyPI ในชื่อ aspose-font และมีสัญญาอนุญาตภายใต้ 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 จะอ่านส่วนขยายไฟล์และค่ามาจิกส่วนหัวเพื่อระบุรูปแบบโดยอัตโนมัติ ไม่จำเป็นต้องระบุอาร์กิวเมนต์ 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}")
เริ่มต้นอย่างรวดเร็ว
ติดตั้งไลบรารี:
.venv/bin/pip install aspose-font
โหลดและตรวจสอบฟอนต์ 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 License. คุณสามารถใช้ในโครงการส่วนบุคคล เชิงพาณิชย์ และโอเพ่นซอร์สได้โดยไม่มีค่าใช้จ่าย. โค้ดต้นฉบับมีให้ที่ GitHub และแพ็กเกจถูกแจกจ่ายผ่าน PyPI ในรูปแบบ aspose-font.