previous post 中,您已经了解如何从内部加载和使用 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 的更多信息。

也可以看看