ในคำแนะนำทีละขั้นตอนนี้ เราจะอธิบายวิธีรับเมตริกแบบอักษรใน C# บทช่วยสอนนี้มีโค้ดสำหรับรับเมตริกแบบอักษร Type1 ใน C# แต่ Aspose.Font for .NET ไลบรารีแบบอักษร C# ช่วยให้คุณอ่านเมตริกแบบอักษรใน C# จากแบบอักษรประเภทอื่นๆ รวมถึงรูปแบบแบบอักษร TruType และ OpenType
ขั้นตอนในการรับ Font Metrics ใน C#
- ติดตั้งแพ็คเกจ Aspose.Font for .NET จาก NuGet.org
- รวมเนมสเปซ Aspose.Font, Aspose.Font.Sources และ Aspose.Font.Type1
- ใช้ใบอนุญาตกับ Aspose.Font สำหรับ .NET เพื่อหลีกเลี่ยงลายน้ำการประเมิน
- โหลดแบบอักษร Type1 อินพุตลงในวัตถุ FileSystemStreamSource
- สร้างวัตถุ FontFileDefinition จากแหล่งที่มาของสตรีม
- สร้างวัตถุ FontDefinition จากข้อกำหนดของไฟล์
- เปิดการกำหนดแบบอักษรเป็นวัตถุ Type1Font
- ตอนนี้ รับแอตทริบิวต์เมตริกทั้งหมดของแบบอักษร Type1
แบบอักษรประกอบด้วยข้อมูลเมตริกที่ช่วยอธิบายสัญลักษณ์ภายในแบบอักษร ข้อมูลเมตริกของฟอนต์นี้ช่วยให้คอมพิวเตอร์เรียนรู้วิธีวาดอักขระและประโยคบนหน้าจอ หากเราต้องการประมวลผลข้อมูลเมตริกนี้ผ่านโค้ดในแอปพลิเคชัน .NET ของเรา นั่นไม่ใช่เรื่องง่าย อย่างไรก็ตาม Aspose.Font สำหรับ .NET ช่วยให้กระบวนการทั้งหมดง่ายและสะดวกโดยใช้ขั้นตอนไม่กี่ขั้นตอนตามที่กล่าวไว้ข้างต้น
รหัสเพื่อรับเมตริกแบบอักษรใน C
using System; | |
//Add reference to Aspose.Font for .NET API | |
//Use following namespaces to get font metrics | |
using Aspose.Font; | |
using Aspose.Font.Sources; | |
using Aspose.Font.Type1; | |
namespace GetFontMetrics | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before getting font metrics | |
//using Aspose.Font for .NET | |
Aspose.Font.License AsposeFontLicense = new Aspose.Font.License(); | |
AsposeFontLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Load Type1 font into a file stream object & read font definition | |
FileSystemStreamSource Type1FontFileSource = new FileSystemStreamSource("SampleInputType1Font.pfb"); | |
FontFileDefinition Type1FontFileDefintion = new FontFileDefinition(Type1FontFileSource); | |
FontDefinition Type1FontDefinition = new FontDefinition(FontType.Type1, Type1FontFileDefintion); | |
//Open Type1 font | |
Type1Font InputType1Font = Font.Open(Type1FontDefinition) as Type1Font; | |
//Read font metrics information and use it for further processing | |
string FontName = InputType1Font.FontName; | |
int FontGlyphcount = InputType1Font.NumGlyphs; | |
double FontMetricsAscender = InputType1Font.Metrics.Ascender; | |
double FontMetricsDescender = InputType1Font.Metrics.Descender; | |
double FontMetricsTypoAscender = InputType1Font.Metrics.TypoAscender; | |
double FontMetricsTypoDescender = InputType1Font.Metrics.TypoDescender; | |
double FontMetricsUnitsPerEM = InputType1Font.Metrics.UnitsPerEM; | |
//display information to console in this case | |
Console.WriteLine( | |
"Font Name: {0}, " + | |
"Glyph Count: {1}, " + | |
"Asender: {2}, " + | |
"Descender: {3}, " + | |
"Typo Ascender: {4}, " + | |
"Typo Descender: {5}, " + | |
"Units Per EM: {6}", | |
FontName, | |
FontGlyphcount, | |
FontMetricsAscender, | |
FontMetricsDescender, | |
FontMetricsTypoAscender, | |
FontMetricsTypoDescender, | |
FontMetricsUnitsPerEM | |
); | |
} | |
} | |
} |
ในโค้ดด้านบน เรากำลังโหลดรูปแบบไฟล์ PFB ซึ่งมีฟอนต์ Type1 ของเราที่เราสนใจจะประมวลผลในโค้ด C# เมื่อเราโหลด font และแปลงเป็นวัตถุ Type1Font แล้ว เราก็สามารถดึงเมตริกแบบอักษรใน C# จากไฟล์นี้ได้
โค้ดนี้สามารถช่วยคุณสร้างตัวอ่านฟอนต์ C# ได้อย่างง่ายดายในแอปพลิเคชัน .NET ของคุณ รวมถึงเว็บ เดสก์ท็อป หน้าต่าง และแอปพลิเคชัน Microsoft Store