הכניסה
קבצי טפטים מבוססים סביב טבלאות – כל טבלה מאוחסנת קטגוריה מסוימת של נתונים כגון תווית גליף, מפות אופי, מטא-דאו שמות או מטריקה אורזונטית.הבנה והגישה לטבלים אלה חיונית למשימות כמו ניתוח טקסט, עריכה מטאת-דיונים, הבטחת איכות ופיצול פורמט.
Aspose.Font FOSS for Python (aspose-font) נותן לך גישה ישירה למערכת מלאה של טבלאות OpenType ו- TrueTYpe באמצעות API Python נקי. הספרייה שוחררה תחת רישיון MIT עם אפס תלויות חיצוניות, כך שתוכל לשלב אותה בכל פרויקט ללא דאגות רישום.
הפוסט הזה עובר את שיעורי השולחן המפתח שניתן למצוא בספריה – מהקריאה של נתוני כותרת ועד לבדוק נתונים גליף ושינוי רשומות שמות – כולם תומכים ב- TtfTableSet המכולת על TtfFont.
תכונות מרכזיות
להעלות טופס ולטבעות גישה
כל מכתב מחובר עם FontLoader הוא מפרט את השולחנות שלו דרך ttf_tables נכסים על TtfFont.• The TtfTableSet אובייקט מספק גישה טופס לכל לוח סטנדרטי, בתוספת גישת באטים ירוקים באמצעות 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")
קראו את שולחן הראשי
The 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}")
עבודה עם השם שולחן
The 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
The CmapTable מפות Unicode נקודות קוד ל- glyph IDs. זה עשוי להכיל מספר תת-טבלאות עבור פלטפורמות שונות ושל שילובים של קוד. 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 ו-Metry Horizontal
The Os2Table מספקת מטרקים ספציפיים עבור OS/2 ו- Windows, כולל אסינדר/הנחיתה טיפוסית, דגלים מעורבים ומסווג 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}")
נתונים Kerning ו-Glyph Data
The 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}")
פורמט תומך
| פורמט | הרחבה | קראו | כתוב |
|---|---|---|---|
| TTF | .טטפ | ✓ | ✓ |
| OTF | .אוטו | ✓ | ✓ |
| CFF | .קפ | ✓ | ✓ |
| WOFF | .ווק | ✓ | ✓ |
| WOFF2 | .WOW2 | ✓ | ✓ |
| EOT | .EOT | ✓ | — |
| TYPE1 | .pf / 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.אתה חופשי להשתמש בו, לשנות אותו ולהפיץ אותו בפרויקטים אישיים ומסחריים ללא הגבלה.