字體 是數字文檔和網頁的組成部分,用於定義文本的外觀。字體文件用於存儲有關字體的信息,例如樣式、粗細、大小等。在某些情況下,您可能需要操作字體以提取其信息。對於此類場景,在本文中,您將學習如何使用 C++ 從 TrueType、CFF 和 Type1 字體中加載和讀取信息。

C++ 字體操作庫

Aspose.Font for C++ 是一個強大的字體操作 API,允許您使用 TrueTypeCFFOpenTypeType1EOT 以及來自您的 C++ 應用程序中的許多 其他字體。您可以從字體中加載、保存和提取編碼信息,也可以使用字形。 API 可以從 下載 部分下載或通過 NuGet 安裝。

使用 C++ 加載 CFF、TrueType 和 Type1 字體

Aspose.Font for C++ 允許您從存儲介質上的文件加載 CFF、TrueType 和 Type1 字體。以下是加載字體的步驟。

  • 創建 FontDefinition 類的對像以通過指定其類型(即 TrueType、Type1 等)來加載字體。
  • 使用 CffFontTtfFontType1Font 類分別從 FontDefinition 對像打開 CFF、TrueType 和 Type1 字體。

使用 C++ 加載 CFF 字體

以下代碼示例顯示瞭如何使用 C++ 加載 CFF 字體。

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));

使用 C++ 加載 TrueType 字體

以下代碼示例顯示如何使用 C++ 加載 TrueType 字體。

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));

使用 C++ 加載 Type1 字體

以下代碼示例顯示如何使用 C++ 加載 Type1 字體。

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));

使用 C++ 從 TrueType 或 Type1 中提取字體規格

Aspose.Font for C++ 還允許您提取包含 Ascender、Descender、TypoAscender、TypoDescender 和 UnitsPerEm 等信息的字體指標。以下是從 TrueType 或 Type1 字體檢索字體規格的步驟。

  • 創建一個 FontDefinition 類的對象來加載 TrueType 或 Type1 字體。
  • 根據字體類型使用 TtfFontType1Font 類打開字體。
  • 提取字體的規格信息。

使用 C++ 從 TrueType 字體獲取字體規格

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)));
        }
    }
}

使用 C++ 從 Type1 字體中提取字體規格

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);

結論

在本文中,您學習瞭如何使用 C++ 加載 CFF、TrueType 和 Type1 字體並提取它們的信息。您可以使用 文檔 探索有關 C++ 字體操作 API 的更多信息。