Las fuentes son una parte integral de los documentos digitales y las páginas web que se utilizan para definir la apariencia del texto. Los archivos de fuentes se utilizan para almacenar información sobre las fuentes, como estilos, peso, tamaño, etc. Podría darse el caso de que necesite manipular las fuentes para extraer su información. Para tales escenarios, en este artículo, aprenderá cómo cargar y leer información de fuentes TrueType, CFF y Type1 usando C++.

Biblioteca de manipulación de fuentes C++

Aspose.Font for C++ es una potente API de manipulación de fuentes que le permite trabajar con TrueType, CFF, OpenType y Type1 , EOT y muchas otras fuentes desde sus aplicaciones C++. Puede cargar, guardar y extraer información de codificación de las fuentes, así como trabajar con glifos. La API puede descargarse desde la sección descargas o instalarse a través de NuGet.

Cargue fuentes CFF, TrueType y Type1 usando C++

Aspose.Font for C++ le permite cargar las fuentes CFF, TrueType y Type1 desde los archivos ubicados en su medio de almacenamiento. Los siguientes son los pasos para cargar una fuente.

  • Cree un objeto de la clase FontDefinition para cargar la fuente especificando su tipo, es decir, TrueType, Type1, etc.
  • Utilice la clase CffFont, TtfFont o Type1Font para abrir fuentes CFF, TrueType y Type1 respectivamente desde el objeto FontDefinition.

Cargue fuentes CFF usando C++

El siguiente ejemplo de código muestra cómo cargar fuentes 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";
//Nombre de archivo de fuente con ruta completa
    
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));

Cargue fuentes TrueType usando C++

El siguiente ejemplo de código muestra cómo cargar fuentes TrueType mediante 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";
//Nombre de archivo de fuente con ruta completa
    
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));

Cargue fuentes Type1 usando C++

El siguiente ejemplo de código muestra cómo cargar fuentes 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";
//Nombre de archivo de fuente con ruta completa
    
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));

Extraiga métricas de fuentes de TrueType o Type1 usando C++

Aspose.Font for C++ también le permite extraer las métricas de fuente que contienen información como Ascender, Descender, TypoAscender, TypoDescender y UnitsPerEm. Los siguientes son los pasos para recuperar métricas de fuente de una fuente TrueType o Type1.

  • Cree un objeto de la clase FontDefinition para cargar la fuente TrueType o Type1.
  • Abra la fuente usando la clase TtfFont o Type1Font según el tipo de fuente.
  • Extrae la información de las métricas de la fuente.

Obtener métricas de fuentes de fuentes 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";
//Nombre de archivo de fuente con ruta completa
    
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);
    
//Obtenga la tabla de codificación cmap Unicode de la fuente como objeto TtfCMapFormatBaseTable para acceder a información sobre el glifo de fuente para el símbolo 'A'.
//También verifique que la fuente tenga el objeto TtfGlyfTable (tabla 'glyf') para acceder al 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 el símbolo 'A'
    char16_t unicode = (char16_t)65;
    
    //Índice de glifos para 'A'
    uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
    
    if (glIndex != static_cast<uint32_t>(0))
    {
        //Glifo de '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)));
        }
    }
}

Extraiga las métricas de fuente de la fuente 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";
//Nombre de archivo de fuente con ruta completa
    
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);

Conclusión

En este artículo, ha aprendido cómo cargar fuentes CFF, TrueType y Type1 y extraer su información usando C++. Puede explorar más sobre la API de manipulación de fuentes de C++ utilizando la documentación.