Phông chữ là một phần không thể thiếu của tài liệu kỹ thuật số và trang web được sử dụng để xác định hình thức của văn bản. Các tệp phông chữ được sử dụng để lưu trữ thông tin về phông chữ chẳng hạn như kiểu dáng, trọng lượng, kích thước, v.v. Có thể xảy ra trường hợp bạn cần thao tác với phông chữ để trích xuất thông tin của chúng. Đối với những tình huống như vậy, trong bài viết này, bạn sẽ tìm hiểu cách tải và đọc thông tin từ các phông chữ TrueType, CFF và Type1 bằng C++.

Thư viện thao tác phông chữ C ++

Aspose.Font for C++ là API thao tác phông chữ mạnh mẽ cho phép bạn làm việc với TrueType, CFF, OpenTypeType1 , EOT và nhiều các phông chữ khác từ bên trong các ứng dụng C++ của bạn. Bạn có thể tải, lưu và trích xuất thông tin mã hóa từ các phông chữ cũng như làm việc với glyphs. Có thể tải xuống API từ phần tải xuống hoặc cài đặt qua NuGet.

Tải Phông chữ CFF, TrueType và Type1 bằng C++

Aspose.Font cho C++ cho phép bạn tải các phông chữ CFF, TrueType và Type1 từ các tệp nằm trên phương tiện lưu trữ của bạn. Sau đây là các bước để tải một phông chữ.

  • Tạo một đối tượng của lớp FontDefinition để tải phông chữ bằng cách chỉ định loại của nó, ví dụ TrueType, Type1, v.v.
  • Sử dụng lớp CffFont, TtfFont hoặc Type1Font để mở các phông chữ CFF, TrueType và Type1 tương ứng từ đối tượng FontDefinition.

Tải phông chữ CFF bằng C++

Mẫu mã sau đây cho biết cách tải phông chữ CFF bằng 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";
//Tên tệp phông chữ với đường dẫn đầy đủ
    
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));

Tải Phông chữ TrueType bằng C++

Mẫu mã sau đây cho biết cách tải phông chữ TrueType bằng 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";
//Tên tệp phông chữ với đường dẫn đầy đủ
    
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));

Tải phông chữ Type1 bằng C++

Mẫu mã sau đây cho biết cách tải phông chữ Type1 bằng 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";
//Tên tệp phông chữ với đường dẫn đầy đủ
    
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));

Trích xuất số liệu phông chữ từ TrueType hoặc Type1 bằng C++

Aspose.Font cho C++ cũng cho phép bạn trích xuất các chỉ số phông chữ có chứa thông tin như Ascender, Descender, TypoAscender, TypoDescender và UnitsPerEm. Sau đây là các bước để truy xuất số liệu phông chữ từ phông chữ TrueType hoặc Type1.

  • Tạo một đối tượng thuộc lớp FontDefinition để tải phông chữ TrueType hoặc Type1.
  • Mở phông chữ bằng lớp TtfFont hoặc Type1Font tùy theo loại phông chữ.
  • Trích xuất thông tin số liệu của phông chữ.

Nhận số liệu phông chữ từ phông chữ TrueType bằng 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";
//Tên tệp phông chữ với đường dẫn đầy đủ
    
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);
    
//Nhận bảng mã hóa cmap unicode từ phông chữ dưới dạng đối tượng TtfCMapFormatBaseTable để truy cập thông tin về glyph phông chữ cho ký hiệu 'A'.
//Ngoài ra, hãy kiểm tra xem phông chữ có đối tượng TtfGlyfTable (bảng 'glyf') để truy cập glyph hay không.
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());
    
    //Mã cho ký hiệu 'A'
    char16_t unicode = (char16_t)65;
    
    //Chỉ số Glyph cho 'A'
    uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
    
    if (glIndex != static_cast<uint32_t>(0))
    {
        //Glyph cho 'A'
        System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
        if (glyph != nullptr)
        {
            //In số liệu glyph
            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)));
        }
    }
}

Trích xuất số liệu phông chữ từ phông chữ Type1 bằng 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";
//Tên tệp phông chữ với đường dẫn đầy đủ
    
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);

Sự kết luận

Trong bài viết này, bạn đã học cách tải các phông chữ CFF, TrueType và Type1 và trích xuất thông tin của chúng bằng C++. Bạn có thể khám phá thêm về API thao tác phông chữ C++ bằng cách sử dụng tài liệu.