גופנים הם חלק בלתי נפרד ממסמכים דיגיטליים ודפי אינטרנט המשמשים להגדרת מראה הטקסט. קובצי הגופן משמשים לאחסון מידע על הגופנים כגון סגנונות, משקל, גודל וכו’. יכול להיות מקרה שבו תצטרך לבצע מניפולציות על גופנים כדי לחלץ את המידע שלהם. עבור תרחישים כאלה, במאמר זה, תלמד כיצד לטעון ולקרוא מידע מגופני TrueType, CFF ו-Type1 באמצעות C++.

ספריית מניפולציה של גופנים C++

Aspose.Font for C++ הוא ממשק API רב עוצמה למניפולציה של גופנים המאפשר לך לעבוד עם TrueType, CFF, OpenType ו-Type1 , EOT, והרבה גופנים אחרים מתוך יישומי C++ שלך. ניתן לטעון, לשמור ולחלץ מידע קידוד מהגופנים וכן לעבוד עם גליפים. ניתן להוריד את ה-API מהקטע הורדות או להתקין דרך NuGet.

טען גופני CFF, TrueType ו-Type1 באמצעות C++

Aspose.Font for C++ מאפשר לך לטעון את הגופנים CFF, TrueType ו-Type1 מהקבצים שנמצאים במדיית האחסון שלך. להלן השלבים לטעינת גופן.

  • צור אובייקט במחלקה FontDefinition כדי לטעון את הגופן על ידי ציון הסוג שלו, כלומר TrueType, Type1 וכו'.
  • השתמש במחלקה CffFont, TtfFont, או Type1Font כדי לפתוח גופנים CFF, TrueType ו-Type1 בהתאמה מאובייקט FontDefinition.

טען גופני CFF באמצעות C++

דוגמת הקוד הבאה מראה כיצד לטעון גופני CFF באמצעות C++.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"OpenSans-Regular.cff";
//שם קובץ גופן עם נתיב מלא
    
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, System::MakeObject<FontFileDefinition>(u"cff", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<CffFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Cff::CffFont>(Aspose::Font::Font::Open(fd));

טען גופני TrueType באמצעות C++

דוגמת הקוד הבאה מראה כיצד לטעון גופני TrueType באמצעות C++.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//שם קובץ גופן עם נתיב מלא
    
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));

טען גופנים מסוג Type1 באמצעות C++

דוגמת הקוד הבאה מראה כיצד לטעון גופנים מסוג Type1 באמצעות C++.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//שם קובץ גופן עם נתיב מלא
    
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));

חלץ מדדי גופן מ-TrueType או Type1 באמצעות C++

Aspose.Font עבור C++ מאפשר לך גם לחלץ את מדדי הגופן המכילים מידע כגון Ascender, Descender, TypoAscender, TypoDescender ו-UnitsPerEm. להלן השלבים לאחזור מדדי גופן מגופן TrueType או Type1.

  • צור אובייקט במחלקה FontDefinition כדי לטעון גופן TrueType או Type1.
  • פתח גופן באמצעות מחלקה TtfFont או Type1Font לפי סוג הגופן.
  • חלץ את פרטי המדדים של הגופן.

קבל מדדי גופנים מגופן TrueType באמצעות C++

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//שם קובץ גופן עם נתיב מלא
    
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> font = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
    
System::String name = font->get_FontName();
System::Console::WriteLine(System::String(u"Font name: ") + name);
System::Console::WriteLine(System::String(u"Glyph count: ") + font->get_NumGlyphs());
System::String metrics = System::String::Format(u"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}", font->get_Metrics()->get_Ascender(), font->get_Metrics()->get_Descender(), font->get_Metrics()->get_TypoAscender(), font->get_Metrics()->get_TypoDescender(), font->get_Metrics()->get_UnitsPerEM());
    
System::Console::WriteLine(metrics);
    
//קבל את טבלת הקידוד cmap unicode מגופן כאובייקט TtfCMapFormatBaseTable כדי לגשת למידע על גליף גופן עבור סמל 'A'.
//בדוק גם שלגופן יש אובייקט TtfGlyfTable (טבלה 'glyf') כדי לגשת לגליף.
System::SharedPtr<Aspose::Font::TtfCMapFormats::TtfCMapFormatBaseTable> cmapTable;
if (font->get_TtfTables()->get_CMapTable() != nullptr)
{
    cmapTable = font->get_TtfTables()->get_CMapTable()->FindUnicodeTable();
}
if (cmapTable != nullptr && font->get_TtfTables()->get_GlyfTable() != nullptr)
{
    System::Console::WriteLine(System::String(u"Font cmap unicode table: PlatformID = ") + cmapTable->get_PlatformId() + u", PlatformSpecificID = " + cmapTable->get_PlatformSpecificId());
    
    //קוד לסמל 'A'
    char16_t unicode = (char16_t)65;
    
    //אינדקס גליף עבור 'A'
    uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
    
    if (glIndex != static_cast<uint32_t>(0))
    {
        //גליף עבור 'A'
        System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
        if (glyph != nullptr)
        {
            //הדפס מדדי גליפים
            System::Console::WriteLine(u"Glyph metrics for 'A' symbol:");
            System::String bbox = System::String::Format(System::String(u"Glyph BBox: Xmin = {0}, Xmax = {1}") + u", Ymin = {2}, Ymax = {3}", glyph->get_GlyphBBox()->get_XMin(), glyph->get_GlyphBBox()->get_XMax(), glyph->get_GlyphBBox()->get_YMin(), glyph->get_GlyphBBox()->get_YMax());
            System::Console::WriteLine(bbox);
            System::Console::WriteLine(System::String(u"Width:") + font->get_Metrics()->GetGlyphWidth(System::MakeObject<GlyphUInt32Id>(glIndex)));
        }
    }
}

חלץ מדדי גופן מגופן Type1 באמצעות C++

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//שם קובץ גופן עם נתיב מלא
    
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));
    
System::String name = font->get_FontName();
System::Console::WriteLine(System::String(u"Font name: ") + name);
System::Console::WriteLine(System::String(u"Glyph count: ") + font->get_NumGlyphs());
System::String metrics = System::String::Format(u"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}", font->get_Metrics()->get_Ascender(), font->get_Metrics()->get_Descender(), font->get_Metrics()->get_TypoAscender(), font->get_Metrics()->get_TypoDescender(), font->get_Metrics()->get_UnitsPerEM());
    
System::Console::WriteLine(metrics);

סיכום

במאמר זה, למדת כיצד לטעון גופנים CFF, TrueType ו-Type1 ולחלץ את המידע שלהם באמצעות C++. תוכל לחקור עוד על ממשק ה-API של C++ Font Manipulation באמצעות תיעוד.