上一篇文章 中,您了解瞭如何從內部加載和使用 CFFTrueTypeOpenTypeType1 字體你的 C++ 應用程序。本文將進一步介紹如何使用 C++ 以編程方式檢測字體中的拉丁符號。通過檢測,您可以判斷字體是否支持拉丁符號。

用於檢測字體中拉丁符號的 C++ API

Aspose.Font for C++ 是一種字體操作和管理 API,可讓您輕鬆檢測字體中是否支持拉丁符號。您可以 下載 API 或通過 NuGet 安裝它。

PM> Install-Package Aspose.Font.Cpp

在 C++ 中檢測 TrueType 字體中的拉丁符號

使用Aspose.Font for C++檢測對拉丁符號的支持就像做餅一樣簡單。以下是檢查特定 TrueType 字體是否支持拉丁符號的步驟。

  • 創建一個 FontDefinition 類的對象,通過將其類型指定為 TrueType 來加載字體。
  • 創建一個 TtfFont 類的對象來訪問字體的信息。
  • 遍歷可能的代碼並將它們解碼為字形 ID。
  • 匹配字形 ID 以檢查源 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));
    
bool latinText = true;
    
    
for (uint32_t code = 65; code < static_cast<uint32_t>(123); code++)
{
    System::SharedPtr<GlyphId> gid = ttfFont->get_Encoding()->DecodeToGid(code);
    if (gid == nullptr || gid == System::StaticCast<System::Object>(GlyphUInt32Id::get_NotDefId()))
    {
        latinText = false;
    }
}
    
if (latinText)
{
    System::Console::WriteLine(System::String::Format(u"Font {0} supports latin symbols.", ttfFont->get_FontName()));
}
else
{
    System::Console::WriteLine(System::String::Format(u"Latin symbols are not supported by font {0}.", ttfFont->get_FontName()));
}

在 C++ 中檢測 Type1 字體中的拉丁符號

檢測 Type1 字體中的拉丁符號的過程與檢測 TrueType 字體的過程相同。唯一的區別是 Type1Font 類的使用。以下步驟演示了 Type1 字體中拉丁符號檢測的完整過程。

  • 使用 FontDefinition 類通過將其類型指定為 Type1 來加載字體。
  • 使用 Type1Font 類訪問字體信息。
  • 遍歷可能的代碼並將它們解碼為字形 ID。
  • 匹配字形 ID 以檢查提供的 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));
    
bool latinText = true;
    
    
for (uint32_t code = 65; code < static_cast<uint32_t>(123); code++)
{
    System::SharedPtr<GlyphId> gid = font->get_Encoding()->DecodeToGid(code);
    if (gid == nullptr || gid == System::StaticCast<System::Object>(GlyphUInt32Id::get_NotDefId()))
    {
        latinText = false;
    }
}
    
if (latinText)
{
    System::Console::WriteLine(System::String::Format(u"Font {0} supports latin symbols.", font->get_FontName()));
}
else
{
    System::Console::WriteLine(System::String::Format(u"Latin symbols are not supported by font {0}.", font->get_FontName()));
}

結論

在本文中,您學習瞭如何使用 C++ 檢測 TrueType 和 Type1 字體中的拉丁符號。本文中的代碼示例可讓您確定特定字體是否支持拉丁符號。您可以使用 文檔 了解有關 C++ 字體操作 API 的更多信息。

也可以看看