Fontes são parte integrante de documentos digitais e páginas da web que são usadas para definir a aparência do texto. Os arquivos de fontes são usados para armazenar informações sobre as fontes, como estilos, peso, tamanho e etc. Pode haver o caso em que você precise manipular as fontes para extrair suas informações. Para esses cenários, neste artigo, você aprenderá como carregar e ler informações de fontes TrueType, CFF e Type1 usando C++.

Biblioteca de manipulação de fontes C++

Aspose.Font for C++ é uma poderosa API de manipulação de fontes que permite trabalhar com TrueType, CFF, OpenType e Type1 , EOT e muitas outras fontes de seus aplicativos C++. Você pode carregar, salvar e extrair informações de codificação das fontes, bem como trabalhar com glifos. A API pode ser baixada na seção downloads ou instalada via NuGet.

Carregar fontes CFF, TrueType e Type1 usando C++

Aspose.Font para C++ permite carregar as fontes CFF, TrueType e Type1 dos arquivos localizados em sua mídia de armazenamento. A seguir estão as etapas para carregar uma fonte.

  • Crie um objeto da classe FontDefinition para carregar a fonte especificando seu tipo, ou seja, TrueType, Type1, etc.
  • Use a classe CffFont, TtfFont ou Type1Font para abrir fontes CFF, TrueType e Type1, respectivamente, do objeto FontDefinition.

Carregar fontes CFF usando C++

O exemplo de código a seguir mostra como carregar fontes CFF usando 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";
//Nome do arquivo de fonte com caminho completo
    
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));

Carregar fontes TrueType usando C++

O exemplo de código a seguir mostra como carregar fontes TrueType usando 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";
//Nome do arquivo de fonte com caminho completo
    
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));

Carregar fontes Type1 usando C++

O exemplo de código a seguir mostra como carregar fontes Type1 usando 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";
//Nome do arquivo de fonte com caminho completo
    
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));

Extrair métricas de fonte de TrueType ou Type1 usando C++

Aspose.Font para C++ também permite extrair as métricas de fonte que contêm informações como Ascender, Descender, TypoAscender, TypoDescender e UnitsPerEm. A seguir estão as etapas para recuperar métricas de fonte de uma fonte TrueType ou Type1.

  • Crie um objeto da classe FontDefinition para carregar a fonte TrueType ou Type1.
  • Abra a fonte usando a classe TtfFont ou Type1Font de acordo com o tipo da fonte.
  • Extraia as informações de métricas da fonte.

Obter métricas de fonte da fonte TrueType usando 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";
//Nome do arquivo de fonte com caminho completo
    
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);
    
//Obtenha a tabela de codificação unicode cmap da fonte como objeto TtfCMapFormatBaseTable para acessar informações sobre o glifo de fonte para o símbolo 'A'.
//Verifique também se a fonte possui o objeto TtfGlyfTable (tabela 'glyf') para acessar o glifo.
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());
    
    //Código para o símbolo 'A'
    char16_t unicode = (char16_t)65;
    
    //Índice de glifo para 'A'
    uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
    
    if (glIndex != static_cast<uint32_t>(0))
    {
        //Glifo para 'A'
        System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
        if (glyph != nullptr)
        {
            //Imprimir métricas de glifo
            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)));
        }
    }
}

Extrair métricas de fonte da fonte Type1 usando 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";
//Nome do arquivo de fonte com caminho completo
    
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);

Conclusão

Neste artigo, você aprendeu como carregar fontes CFF, TrueType e Type1 e extrair suas informações usando C++. Você pode explorar mais sobre a API de manipulação de fontes C++ usando a documentação.