การจัดการฟอนต์หลัก

การจัดการฟอนต์หลัก

การจัดการฟอนต์หลัก

คู่มือนี้แสดงวิธีโหลดฟอนต์จากเส้นทางไฟล์หรืออาเรย์ไบต์, อ่านคุณสมบัติมีตาดาต้า, แมปจุดรหัส Unicode ไปยัง ID ของ glyph, ดึง glyph, และทำงานกับแกนฟอนต์แบบแปรผันและอินสแตนซ์ที่ตั้งชื่อไว้.

FontLoader เป็นจุดเข้าหลักสำหรับการดำเนินการโหลดฟอนต์ทั้งหมด หลังจากฟอนต์ถูกโหลดแล้ว, คลาสฐาน Font จะให้การเข้าถึงเมตาดาต้า, การเข้ารหัส, การเข้าถึง glyph, และเมตริกโดยไม่ต้องทำการวิเคราะห์ตารางไบนารี.


การโหลดฟอนต์

FontLoader.open(source, font_type, collection_index) โหลดฟอนต์จากเส้นทางไฟล์พร้อมการตรวจจับรูปแบบอัตโนมัติ FontLoader.load(source, font_type, collection_index) รับไบต์ดิบสำหรับกระบวนการทำงานในหน่วยความจำหรือเครือข่าย.

from aspose_font import FontLoader

# From a file path — format auto-detected
font = FontLoader.open("Roboto-Regular.ttf")

# From raw bytes — useful for in-memory or network workflows
with open("OpenSans-Regular.otf", "rb") as f:
    font = FontLoader.load(f.read())

การอ่านเมตาดาต้าฟอนต์

หลังจากโหลด, วัตถุ Font จะเปิดเผยคุณสมบัติ font_name, font_family, font_style และ num_glyphs เพื่อการตรวจสอบอย่างรวดเร็วโดยไม่ต้องวิเคราะห์ตารางด้วยตนเอง.

from aspose_font import FontLoader

font = FontLoader.open("MyFont.ttf")
print(font.font_name)    # full PostScript name
print(font.font_family)  # family name
print(font.font_style)   # style name e.g. "Regular", "Bold"
print(font.num_glyphs)   # total glyph count

การเข้ารหัส Glyph

FontEncoding จัดแผนที่จุดโค้ด Unicode ไปยัง glyph ID. ใช้ unicode_to_gid(cp) เพื่อแก้ไขจุดโค้ดเดียว, หรือ get_all_codepoints() เพื่อแสดงรายการทุกจุดโค้ดที่แมปในฟอนต์.

from aspose_font import FontLoader

font = FontLoader.open("OpenSans-Regular.otf")

# Map a single code point
gid = font.encoding.unicode_to_gid(0x41)  # GID for 'A'

# Enumerate all mapped code points
all_cps = font.encoding.get_all_codepoints()
print(f"Font covers {len(all_cps)} Unicode code points")

การเข้าถึง Glyph

GlyphAccessor ดึงวัตถุ glyph ตาม ID, Unicode หรือสตริงข้อความ.

from aspose_font import FontLoader

font = FontLoader.open("OpenSans-Regular.otf")

glyph = font.glyph_accessor.get_glyph_by_id(gid)
glyph_a = font.glyph_accessor.get_glyph_by_unicode(0x41)
glyphs = font.glyph_accessor.get_glyphs_for_text("Hello")
all_ids = font.glyph_accessor.get_all_glyph_ids()

ฟอนต์แบบแปรผัน

สำหรับ ฟอนต์แบบแปรผัน (ฟอนต์ที่มีตาราง fvar), TtfFont แสดงคำนิยามแกนและอินสแตนซ์ที่ตั้งชื่อโดยตรง.

from aspose_font import FontLoader
from aspose_font import TtfFont

loaded = FontLoader.load("Roboto-VariableFont_wdth,wght.ttf")
ttf: TtfFont = loaded.font

# Check if the font is variable
if ttf.is_variable:
    # List all variation axes
    for axis in ttf.variable_axes:
        print(f"{axis.tag}  range: {axis.min_value}..{axis.max_value}  default: {axis.default_value}")

    # Look up a single axis by tag
    wght = ttf.get_axis("wght")
    if wght:
        print(f"Weight axis: {wght.min_value}..{wght.max_value}")

    # Enumerate named instances (e.g. Bold, Condensed)
    for inst in ttf.named_instances:
        print(f"Instance: name_id={inst.name_id}  coordinates={inst.coordinates}")

สำหรับกระบวนการทำอินสแตนซ์ระดับสูง, SmartInstancer ห่อหุ้มการนับจำนวนแกนและการแก้ไขอินสแตนซ์ที่ตั้งชื่อไว้ในอ็อบเจ็กต์เดียว:

from aspose_font import FontLoader
from aspose_font import SmartInstancer

loaded = FontLoader.load("Roboto-VariableFont_wdth,wght.ttf")
instancer = SmartInstancer(loaded.font)

print(f"Axes: {[a.tag for a in instancer.axes]}")
print(f"Named instances: {len(instancer.named_instances)}")

เคล็ดลับและแนวทางปฏิบัติที่ดีที่สุด

  • แคชอ็อบเจกต์ Font ที่โหลดแล้วเมื่อประมวลผลไฟล์ฟอนต์เดียวกันหลายครั้ง
  • ใช้ FontLoader.load(bytes) สำหรับกระบวนการทำงานเครือข่ายหรือฐานข้อมูลเพื่อหลีกเลี่ยงไฟล์ชั่วคราว
  • ตรวจสอบ num_glyphs ก่อนทำการ subset เพื่อประเมินการครอบคลุมหลังการ subset
  • get_all_glyph_ids() มีประโยชน์สำหรับการวนรอบชุด glyph ทั้งหมด

ปัญหาที่พบบ่อย

ปัญหาสาเหตุวิธีแก้
UnsupportedFontFormatExceptionไม่สามารถจดจำรูปแบบไฟล์ตรวจสอบว่าไฟล์เป็น TTF/OTF/CFF/WOFF/WOFF2/EOT/Type1 ที่ถูกต้อง
FontParseExceptionข้อมูลไบนารีที่ผิดรูปแบบตรวจสอบไฟล์ฟอนท์ด้วย linter ก่อนทำการโหลด
GlyphNotFoundExceptionค่า GID อยู่นอกช่วงตรวจสอบ num_glyphs ก่อนเข้าถึงโดยใช้ ID

FAQ

FontLoader รองรับ WOFF2 หรือไม่?

ใช่. FontLoader.open() และ FontLoader.load() รองรับไฟล์ WOFF2 ทั้งสองและตรวจจับรูปแบบโดยอัตโนมัติ.

ฉันสามารถโหลดฟอนต์จาก URL ได้หรือไม่?

ไม่สามารถทำได้โดยตรง ดาวน์โหลดไบต์ของฟอนต์ด้วย urllib หรือ requests แล้วส่งไปยัง FontLoader.load(bytes).

การโหลดเป็น thread-safe หรือไม่?

แต่ละครั้งที่เรียก FontLoader.open() จะคืนค่าอ็อบเจกต์ Font ที่เป็นอิสระ การเรียกพร้อมกันบนอ็อบเจกต์ที่แยกกันนั้นปลอดภัย.


API Reference สรุป

คลาส / เมธอดคำอธิบาย
FontLoader.open(path)โหลดฟอนท์จากเส้นทางไฟล์
FontLoader.load(data)โหลดฟอนต์จากไบต์ดิบ
Font.font_nameชื่อฟอนต์ PostScript แบบเต็ม
Font.font_familyชื่อกลุ่มฟอนต์
Font.font_styleชื่อสไตล์ฟอนต์
Font.num_glyphsจำนวน glyph ทั้งหมด
FontEncoding.unicode_to_gid(cp)แมปจุดรหัส Unicode ไปยัง ID ของ glyph
FontEncoding.get_all_codepoints()จุดรหัส Unicode ทั้งหมดพร้อมการแมป glyph
GlyphAccessor.get_glyph_by_id(gid)ดึง glyph ตาม ID
GlyphAccessor.get_glyph_by_unicode(cp)ดึง glyph ตาม Unicode code point
GlyphAccessor.get_glyphs_for_text(text)ดึง glyph สำหรับสตริงข้อความ
GlyphAccessor.get_all_glyph_ids()แสดงรายการ ID ของ glyph ทั้งหมดในฟอนต์
TtfFont.is_variableว่าฟอนต์มีข้อมูลฟอนต์แบบแปรผันหรือไม่
TtfFont.variable_axesรายการของอ็อบเจกต์ VariableAxis สำหรับแต่ละแกนการแปรผัน
TtfFont.get_axis(tag)ค้นหาแกนเดียวโดยใช้แท็กสี่ตัวอักษรของมัน
TtfFont.named_instancesอินสแตนซ์ที่ตั้งชื่อไว้ซึ่งกำหนดในตาราง fvar ของฟอนต์
SmartInstancer(font)ตัวช่วยระดับสูงที่ห่อหุ้มการสืบค้นแกนและอินสแตนซ์

ดูเพิ่มเติม

 ภาษาไทย