.NET Font Management API

ใน [การพิมพ์] แบบดิจิทัล 1 ฟอนต์จะกำหนดรูปแบบเฉพาะที่จะใช้สำหรับรูปลักษณ์ของตัวละคร ส่วนใหญ่มักจะใช้แบบอักษรในเอกสารและหน้าเว็บเพื่อจัดรูปแบบข้อความ ฟอนต์แต่ละตัวจะอธิบายไว้ในไฟล์ที่มีข้อมูลเกี่ยวกับขนาด น้ำหนัก สไตล์ และการเข้ารหัสของอักขระ เนื่องจากฟอนต์เป็นส่วนสำคัญของรูปแบบไฟล์ต่างๆ Aspose จึงนำเสนอ API เฉพาะสำหรับจัดการและแสดงผลประเภทฟอนต์ยอดนิยม รวมถึง TrueType, CFF, OpenType, และ Type1 ในบทความนี้ คุณจะได้เรียนรู้วิธีโหลด บันทึก และแยกข้อมูลจากฟอนต์โดยใช้ C# ด้วย Aspose.Font for .NET

API การจัดการฟอนต์ .NET - ดาวน์โหลดฟรี

Aspose.Font for .NET คือ API ภายในองค์กรที่ให้คุณใช้งานฟีเจอร์การจัดการฟอนต์โดยใช้ C# ภายในแอปพลิเคชัน .NET ของคุณ นอกจากการโหลด การบันทึก และการดึงข้อมูลจากฟอนต์แล้ว API ยังให้คุณแสดงสัญลักษณ์หรือข้อความได้หากต้องการ คุณสามารถ ดาวน์โหลด API หรือติดตั้งภายในแอปพลิเคชัน .NET ของคุณโดยใช้ NuGet

PM> Install-Package Aspose.Font

โหลดหรือบันทึกฟอนต์จากไฟล์โดยใช้ C#

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

  • โหลดฟอนต์จากไฟล์ (โดยใช้พาธของไฟล์หรืออาร์เรย์ไบต์) โดยใช้คลาส FontDefinition
  • เข้าถึงข้อมูลแบบอักษรโดยใช้คลาส CffFont, TtfFont หรือ Type1Font
  • บันทึกไฟล์ (ถ้าจำเป็น)

โหลดและบันทึกฟอนต์ CFF

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดและบันทึกฟอนต์ CFF โดยใช้ C#

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
//อาร์เรย์ไบต์เพื่อโหลดแบบอักษรจาก
string dataDir = RunExamples.GetDataDir_Data();

byte[] fontMemoryData = File.ReadAllBytes(dataDir + "OpenSans-Regular.cff");
FontDefinition fd = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
CffFont cffFont = Aspose.Font.Font.Open(fd) as CffFont;

//ทำงานกับข้อมูลจากวัตถุ CffFont ที่เพิ่งโหลด

//บันทึก CffFont ลงดิสก์
//ชื่อไฟล์เอาต์พุตแบบอักษรพร้อมเส้นทางแบบเต็ม
string outputFile = RunExamples.GetDataDir_Data() + "OpenSans-Regular_out.cff";

cffFont.Save(outputFile);

โหลดและบันทึกแบบอักษร TrueType

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดและบันทึกฟอนต์ TrueType โดยใช้ C#

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
//อาร์เรย์ไบต์เพื่อโหลดแบบอักษรจาก
string dataDir = RunExamples.GetDataDir_Data();
            
byte[] fontMemoryData = File.ReadAllBytes(dataDir + "Montserrat-Regular.ttf");
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new ByteContentStreamSource(fontMemoryData)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;

//ทำงานกับข้อมูลจากวัตถุ TtfFont ที่เพิ่งโหลด

//บันทึก TtfFont ลงดิสก์
//ชื่อไฟล์เอาต์พุตแบบอักษรพร้อมเส้นทางแบบเต็ม
string outputFile = RunExamples.GetDataDir_Data() + "Montserrat-Regular_out.ttf";

ttfFont.Save(outputFile);

โหลดและบันทึกฟอนต์ Type1

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดและบันทึกฟอนต์ Type1 โดยใช้ C#

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;

แยกเมตริกแบบอักษรโดยใช้ C

Aspose.Font for .NET ยังช่วยให้คุณสามารถดึงข้อมูลเมตริกแบบอักษร เช่น Ascender, Descender, TypoAscender, TypoDescender และ UnitsPerEm ต่อไปนี้เป็นขั้นตอนในการทำเช่นนั้น

  • ใช้คลาส FontDefinition เพื่อโหลดฟอนต์ TrueType หรือ Type1 จากไฟล์
  • เปิดฟอนต์โดยใช้คลาสประเภทฟอนต์ที่เกี่ยวข้อง เช่น Type1Font, TtfFont เป็นต้น
  • เข้าถึงข้อมูลตัวชี้วัดของแบบอักษร

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแยกเมตริกแบบอักษรจากแบบอักษร TrueType และ Type1 โดยใช้ C#

แยกเมตริกจาก TrueType Font

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = Aspose.Font.Font.Open(fd) as TtfFont;

string name = font.FontName;
Console.WriteLine("Font name: " + name);
Console.WriteLine("Glyph count: " + font.NumGlyphs);
string metrics = string.Format(
    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
    font.Metrics.Ascender, font.Metrics.Descender,
    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);

Console.WriteLine(metrics);

//รับตารางการเข้ารหัส cmap unicode จากฟอนต์เป็นวัตถุ TtfCMapFormatBaseTable เพื่อเข้าถึงข้อมูลเกี่ยวกับฟอนต์ glyph สำหรับสัญลักษณ์ 'A'
//ตรวจสอบด้วยว่าแบบอักษรมีวัตถุ TtfGlyfTable (ตาราง 'glyf') เพื่อเข้าถึงสัญลักษณ์
Aspose.Font.TtfCMapFormats.TtfCMapFormatBaseTable cmapTable = null;
if (font.TtfTables.CMapTable != null)
{
    cmapTable = font.TtfTables.CMapTable.FindUnicodeTable();
}
if (cmapTable != null && font.TtfTables.GlyfTable != null)
{
    Console.WriteLine("Font cmap unicode table: PlatformID = " + cmapTable.PlatformId + ", PlatformSpecificID = " + cmapTable.PlatformSpecificId);

    //รหัสสำหรับสัญลักษณ์ 'A'
    char unicode = (char)65;

    //ดัชนีสัญลักษณ์สำหรับ 'A'
    uint glIndex = cmapTable.GetGlyphIndex(unicode);

    if (glIndex != 0)
    {
        //สัญลักษณ์สำหรับ 'A'
        Glyph glyph = font.GetGlyphById(glIndex);
        if (glyph != null)
        {
            //พิมพ์สัญลักษณ์เมตริก
            Console.WriteLine("Glyph metrics for 'A' symbol:");
            string bbox = string.Format(
                "Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
                glyph.GlyphBBox.XMin, glyph.GlyphBBox.XMax,
                glyph.GlyphBBox.YMin, glyph.GlyphBBox.YMax);
            Console.WriteLine(bbox);
            Console.WriteLine("Width:" + font.Metrics.GetGlyphWidth(new GlyphUInt32Id(glIndex)));
        }
    }
}

แยกเมตริกจากแบบอักษร Type1

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;

string name = font.FontName;
Console.WriteLine("Font name: " + name);
Console.WriteLine("Glyph count: " + font.NumGlyphs);
string metrics = string.Format(
    "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
    font.Metrics.Ascender, font.Metrics.Descender,
    font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);

Console.WriteLine(metrics);

ตรวจหาสัญลักษณ์ละตินในแบบอักษรโดยใช้ C#

คุณยังสามารถตรวจจับข้อความละตินในแบบอักษรได้ด้วยการถอดรหัสรหัสสัญลักษณ์โดยใช้ Aspose.Font for .NET ต่อไปนี้เป็นขั้นตอนในการดำเนินการนี้

  • โหลดฟอนต์จากไฟล์โดยใช้คลาส FontDefinition
  • ถอดรหัส GlyphId โดยใช้เมธอด DecodeToGid() ของคลาสฟอนต์ที่เกี่ยวข้อง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีถอดรหัสสัญลักษณ์ละตินในฟอนต์ TrueType และ Type1 โดยใช้ C#

ตรวจหาสัญลักษณ์ละตินในแบบอักษร TrueType

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;

bool latinText = true;


for (uint code = 65; code < 123; code++)
{
    GlyphId gid = ttfFont.Encoding.DecodeToGid(code);
    if (gid == null || gid == GlyphUInt32Id.NotDefId)
    {
        latinText = false;
    }
}

if (latinText)
{
    Console.WriteLine(string.Format("Font {0} supports latin symbols.", ttfFont.FontName));
}
else
{
    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", ttfFont.FontName));
}

ตรวจหาสัญลักษณ์ละตินในแบบอักษร Type1

// สำหรับตัวอย่างและไฟล์ข้อมูลทั้งหมด โปรดไปที่ https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;

bool latinText = true;


for (uint code = 65; code < 123; code++)
{
    GlyphId gid = font.Encoding.DecodeToGid(code);
    if (gid == null || gid == GlyphUInt32Id.NotDefId)
    {
        latinText = false;
    }
}

if (latinText)
{
    Console.WriteLine(string.Format("Font {0} supports latin symbols.", font.FontName));
}
else
{
    Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", font.FontName));
}

บทสรุป

ในบทความนี้ คุณได้เห็นวิธีโหลดและบันทึกฟอนต์ CFF, TrueType และ Type1 โดยทางโปรแกรมโดยใช้ C# นอกจากนี้ คุณได้เรียนรู้วิธีแยกข้อมูลเมตริกแบบอักษรจากแบบอักษร TrueType และ Type1 ภายในแอปพลิเคชัน .NET ของคุณ คุณสามารถสำรวจคุณสมบัติที่น่าสนใจอื่นๆ ที่นำเสนอโดย Aspose.Font for .NET ได้ใน เอกสารประกอบ สำหรับการอัปเดตเพิ่มเติม โปรดไปที่ Aspose.Font blog