Introduction

ไฟล์ Font มีโครงสร้างรอบตัวตาราง ตาระแต่ละตราเก็บข้อมูลชนิดเฉพาะ เช่น การออกแบบกลิฟ, แครกเมป, เมตาเดทการตั้งชื่อ หรือเมตรขอบเขต. ความเข้าใจและเข้าถึงตลาดเหล่านี้เป็นสิ่งจําเป็นสําหรับงานเช่น วิเคราะห์อักษร, แก้ไขเมตาเดีต, การันตีคุณภาพ และแปลงรูปแบบ.

Aspose.Font FOSS for Python (aspose-font) ให้คุณเข้าถึงตาราง OpenType และ TrueTyme ได้โดยตรงผ่าน API Python ที่สะอาด ห้องสมุดนี้ถูกปล่อยลงภายใต้ลิซเซนส์ MIT โดยไม่มีความพึ่งพาจากภายนอก ดังนั้น คุณสามารถรวมมันเข้าไปในโครงการใด ๆ โดยไม่ต้องกังวลเกี่ยวกับการอนุญาต.

บทความนี้เดินผ่านหลักตารางชั้นที่มีในห้องสมุด จากการอ่าน metadata หัวข้อเพื่อตรวจสอบข้อมูล glyph และปรับเปลี่ยนบันทึกชื่อ ?? ทั้งหมดรองรับโดย TtfTableSet ขวดใน TtfFont.


คุณสมบัติหลัก

การอัตราบรรจุ Font และการเข้าถึงตาราง

ทุกตัวอักษรที่บรรจุด้วย FontLoader เปิดตารางที่วิเคราะห์ผ่าน ttf_tables อสังหาริมทรัพย์บน TtfFont.- ครับ TtfTableSet object ให้การเข้าถึงแบบทับในตารางมาตรฐานแต่ละตัว, บวกกับการเข้าไปใช้ byte raw ผ่าน get_raw() สําหรับตารางที่ทําตามมาตรฐานหรือไม่รับรู้.

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
tables = font.ttf_tables

# Typed table access
head = tables.head
name = tables.name
cmap = tables.cmap

# Raw bytes for any table tag
raw_os2 = tables.get_raw("OS/2")

การอ่านโต๊ะหัวหน้า

การ HeadTable เก็บค่าพื้นฐานของตัวอักษรทั่วไป: หน่วยต่อ em, การแก้ไข, ตารางเขตจํากัด, วันที่สร้าง และ ฟันธงสไตล์. ค่าเหล่านี้เป็นสิ่งสําคัญสําหรับเครื่องยนต์การแสดงผลและผู้ยืนยันลักษณะอักขระ.

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
head = font.ttf_tables.head

print(f"Units per em: {head.units_per_em}")
print(f"Revision: {head.font_revision}")
print(f"Bounding box: ({head.x_min}, {head.y_min}) to ({head.x_max}, {head.y_max})")
print(f"Mac style flags: {head.mac_style}")
print(f"Glyph data format: {head.glyph_data_format}")

การทํางานกับตารางชื่อ

การ NameTable มีเมตาเดต้าที่สามารถอ่านได้ทั้งหมด: ชื่อครอบครัวตัวอักษร, สไตล์, หนังสืออนุญาตลิขสิทธิ์, ผู้ออกแบบและคําบรรยาย คุณสามารถสอบถามบันทึกโดย ID ชื่อ, ค้นหาชื่อท้องถิ่นหรือปรับเปลี่ยนรายการเพื่อทําแบรนด์ใหม่.

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
name_table = font.ttf_tables.name

# Read the font family name (name ID 1)
family = name_table.best_name(1, ["en"])
print(f"Family: {family}")

# List all localized versions of the full name (name ID 4)
localized = name_table.localized_names(4)
for lang, value in localized.items():
    print(f"  {lang}: {value}")

# Replace the font family name
name_table.replace_name(1, "MyCustomFamily")

การตรวจสอบตาราง CMap

การ CmapTable แผนที่ Unicode โค้ดปอยท์ เป็น glyph ID อาจมีหลายตารางย่อยสําหรับแพลตฟอร์มและการรวมโค๊ดต่าง ๆ ใช้ best_subtable() เพื่อให้ได้การทําแผนที่เหมาะสมที่สุดสําหรับกรณีใช้งานของคุณ.

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
cmap = font.ttf_tables.cmap

# Get the best Unicode subtable
subtable = cmap.best_subtable()
print(f"Platform: {subtable.platform_id}, Encoding: {subtable.encoding_id}")

# Look up the glyph ID for 'A'
gid = subtable.get_gid(ord("A"))
print(f"Glyph ID for 'A': {gid}")

การอ่าน OS/2 และเมทริกส์แนวขอบ

การ Os2Table ให้บริการ OS/2 และ Windows เฉพาะเมทริกส์รวมถึง typographic ascender/descender, การติดตั้งธงและ Panose ประเภทประจําตัว. HheaTable และ HmtxTable การจัดหาเมตรการวางแผนแนวขอบที่ใช้ในการออกแบบข้อความ.

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")
os2 = font.ttf_tables.os2
hhea = font.ttf_tables.hhea

print(f"Typo ascender: {os2.s_typo_ascender}")
print(f"Typo descender: {os2.s_typo_descender}")
print(f"Embedding flags (fsType): {os2.fs_type}")
print(f"Panose: {os2.panose.hex()}")

print(f"Hhea ascender: {hhea.ascender}")
print(f"Max advance width: {hhea.advance_width_max}")
print(f"Number of hmetrics: {hhea.number_of_hmetrics}")

ข้อมูลการคอและ Glyph

การ KernTable จัดเก็บคู่คอริ้งสําหรับการปรับระยะทางแนวขอบ GlyfTable มีข้อมูลแนวทางตัวอักษรจริงสําหรับแปล TrueType คุณยังสามารถใช้ PostTable เพื่อแก้ไขชื่อกลิฟ์.

import aspose_font

font = aspose_font.FontLoader.open("MyFont.ttf")

# Kerning lookup
kern = font.ttf_tables.kern
if kern:
    pairs = kern.pairs
    print(f"Total kern pairs: {len(pairs)}")
    # Look up a specific pair by glyph IDs
    adjustment = kern.get(36, 57)
    print(f"Kern adjustment: {adjustment}")

# Glyph name from post table
post = font.ttf_tables.post
if post:
    glyph_name = post.glyph_name(0)
    print(f"Glyph 0 name: {glyph_name}")

เริ่มต้นอย่างรวดเร็ว

pip install aspose-font>=1.0.0
import aspose_font

# Load any TTF or OTF font
font = aspose_font.FontLoader.open("MyFont.ttf")
tables = font.ttf_tables

# Print basic table info
print(f"Font: {font.font_name}")
print(f"Family: {font.font_family}")
print(f"Glyphs: {font.num_glyphs}")
print(f"Units/em: {tables.head.units_per_em}")

# Read name table entries
name_table = tables.name
for name_id in [0, 1, 2, 4, 5, 6]:
    value = name_table.best_name(name_id, ["en"])
    if value:
        print(f"  Name ID {name_id}: {value}")

# Check character coverage
cmap = tables.cmap
subtable = cmap.best_subtable()
for char in "ABCabc012":
    gid = subtable.get_gid(ord(char))
    print(f"  '{char}' -> glyph {gid}")

รูปแบบที่สนับสนุน

FormatExtensionReadWrite
TTF.ttf
OTF.otf
CFF.cff
WOFF.- โว๊ฟ
WOFF2.woff2
EOT.eot
TYPE1.pfb/.pfa

หลักสูตรและใบอนุญาต

Aspose.Font FOSS for Python is released under the MIT license. The source code is available on GitHub and the package is published on PyPI as aspose-font.คุณมีอิสระในการใช้งาน ปรับปรุงและกระจายมันในทั้งโครงการส่วนตัว และพาณิชย์ โดยไม่มีข้อบังคับใดๆ.


เริ่มต้น

แหล่งข้อมูลที่เกี่ยวข้อง