การทํางานกับ TrueType Font

การทํางานกับ TrueType Font

การทํางานกับ TrueType Font

คู่มือนี้แสดงวิธีการอัตราบรรจุ TrueType fonts, ตรวจสอบข้อมูลตารางไบนารีของพวกเขา, อ่านบายต์ตัวก่อนและสร้างตัวอย่างสติกจาก font แปร โดยใช้ TtfInstancer.

TrueType (TTF) เป็นรูปแบบตัวอักษรที่ใช้กันทั่วไปที่สุดในระบบ desktop และเว็บไซต์. Aspose.Font FOSS สําหรับ Python exposes TtfFont สําหรับการเข้าถึงข้อมูลตารางเฉพาะ TTF การค้นหา glyph blob, และการควบคุมแกนตัวอักษรที่เปลี่ยนไป.


การอัด Font TTF

FontLoader.open() auto-detects รูปแบบ TTF และคืน a TtfFont- type object ตัวอย่างที่กลับมาให้การเข้าถึง TTF-specific features เช่น ttf_tables, get_table_bytes(), และข้อมูลแกนตัวอักษรที่เปลี่ยนไป.

from aspose_font.loader import FontLoader

font = FontLoader.open("Roboto-Regular.ttf")
print(font.font_name, font.num_glyphs, "glyphs")

การเข้าถึงตาราง TTF

TtfFont.ttf_tables เปิดเผย a TtfTableSet ที่มี Properties ชื่อสําหรับตารางทั่วไป.

from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont

ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
tables = ttf_font.ttf_tables
print("kern table present:", tables.kern is not None)

ภาพบายตาราง Raw

การใช้งาน TtfFont.get_table_bytes(tag) เพื่อหาข้อมูลตารางไบนารีสด และ TtfFont.set_table_bytes(tag, data) เพื่อเขียนบายต์ตารางที่เปลี่ยนแปลง.

from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont

ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
raw_cmap = ttf_font.get_table_bytes("cmap")

ตัวอย่าง Font ที่เปลี่ยนได้

TtfFont.get_axis() กลับข้อมูลแกน. TtfInstancer.instantiate() ผลิตการ ตัวอย่างที่ยังคงจากตัวอักษรเปลี่ยน ที่จุดประสานแกนที่ถูกกําหนดไว้.

from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
from aspose_font.ttf.instancer import TtfInstancer

ttf_font: TtfFont = FontLoader.open("Roboto-VariableFont_wdth,wght.ttf")
# Get a static instance at wght=700, wdth=75
instance = TtfInstancer.instantiate(ttf_font, {"wght": 700.0, "wdth": 75.0})

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

  • ใช้เสมอ ๆ isinstance(font, TtfFont) ก่อนเข้าถึง ttf_tables หรือ get_table_bytes()
  • การใช้งาน TtfTableSet.kern เพื่อตรวจสอบการมีของคาร์นก่อนอ่านคู่คอร์น
  • การใช้ตัวแปร font จะสร้างวัตถุ fonts ใหม่; ตัวเดิมไม่เปลี่ยนแปลง

คําอธิบายการพบบ่อย API

ประเภท / กลไกDescription
TtfFont.ttf_tablesการเข้าถึงตาราง TTF
TtfFont.get_table_bytes(tag)ไบต์ Raw สําหรับตารางที่มีชื่อ
TtfFont.set_table_bytes(tag, data)เขียนไบต์ตารางที่เปลี่ยน
TtfFont.get_axis()ข้อมูลแกนตัวอักษรที่เปลี่ยนแปลงได้
TtfInstancer.instantiate(font, coords)สร้างตัวอย่างที่ไม่เป็นของตัวเอง
TtfTableSet.kernโต๊ะคาร์น (ไม่มีถ้าไม่ปรากฏ)

See Also

 ภาษาไทย