Font adalah bagian integral dari dokumen digital dan halaman web yang digunakan untuk menentukan tampilan teks. File font digunakan untuk menyimpan informasi tentang font seperti gaya, berat, ukuran, dll. Mungkin ada kasus ketika Anda perlu memanipulasi font untuk mengekstrak informasinya. Untuk skenario seperti itu, dalam artikel ini, Anda akan mempelajari cara memuat dan membaca informasi dari font TrueType, CFF, dan Type1 menggunakan C++.

Pustaka Manipulasi Font C++

Aspose.Font for C++ adalah API manipulasi font canggih yang memungkinkan Anda bekerja dengan TrueType, CFF, OpenType, dan Type1 , EOT, dan banyak font lain dari dalam aplikasi C++ Anda. Anda dapat memuat, menyimpan, dan mengekstrak informasi penyandian dari font serta bekerja dengan mesin terbang. API dapat diunduh dari bagian unduhan atau diinstal melalui NuGet.

Muat Font CFF, TrueType, dan Type1 menggunakan C++

Aspose.Font for C++ memungkinkan Anda memuat font CFF, TrueType, dan Type1 dari file yang terletak di media penyimpanan Anda. Berikut ini adalah langkah-langkah untuk memuat font.

  • Buat objek kelas FontDefinition untuk memuat font dengan menentukan jenisnya yaitu TrueType, Type1, dll.
  • Gunakan kelas CffFont, TtfFont, atau Type1Font untuk membuka masing-masing font CFF, TrueType, dan Type1 dari objek FontDefinition.

Muat Font CFF menggunakan C++

Contoh kode berikut menunjukkan cara memuat font CFF menggunakan 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";
//Nama file font dengan path lengkap
    
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));

Muat TrueType Fonts menggunakan C++

Contoh kode berikut menunjukkan cara memuat font TrueType menggunakan 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";
//Nama file font dengan path lengkap
    
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));

Muat Font Type1 menggunakan C++

Contoh kode berikut menunjukkan cara memuat font Type1 menggunakan 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";
//Nama file font dengan path lengkap
    
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));

Ekstrak Metrik Font dari TrueType atau Type1 menggunakan C++

Aspose.Font for C++ juga memungkinkan Anda mengekstrak metrik font yang berisi informasi seperti Ascender, Descender, TypoAscender, TypoDescender, dan UnitsPerEm. Berikut adalah langkah-langkah untuk mengambil metrik font dari font TrueType atau Type1.

  • Buat objek kelas FontDefinition untuk memuat font TrueType atau Type1.
  • Buka font menggunakan kelas TtfFont atau Type1Font sesuai dengan jenis font.
  • Ekstrak informasi metrik font.

Dapatkan Metrik Font dari TrueType Font menggunakan 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";
//Nama file font dengan path lengkap
    
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);
    
//Dapatkan tabel encoding unicode cmap dari font sebagai objek TtfCMapFormatBaseTable untuk mengakses informasi tentang font glyph untuk simbol 'A'.
//Periksa juga apakah font memiliki objek TtfGlyfTable (tabel 'glyf') untuk mengakses mesin terbang.
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());
    
    //Kode untuk simbol 'A'
    char16_t unicode = (char16_t)65;
    
    //Indeks mesin terbang untuk 'A'
    uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
    
    if (glIndex != static_cast<uint32_t>(0))
    {
        //Mesin terbang untuk 'A'
        System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
        if (glyph != nullptr)
        {
            //Cetak metrik mesin terbang
            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)));
        }
    }
}

Ekstrak Metrik Font dari Font Type1 menggunakan 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";
//Nama file font dengan path lengkap
    
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);

Kesimpulan

Pada artikel ini, Anda telah mempelajari cara memuat font CFF, TrueType, dan Type1 dan mengekstrak informasinya menggunakan C++. Anda dapat mempelajari lebih lanjut tentang C++ Font Manipulation API menggunakan dokumentasi.