Завантажити шрифт із файлу TTF | C++

Тут описано чотири по-різному виконаних приклади того, як завантажити шрифт із файлу Montserrat-Regular.ttf.


Спочатку ознайомтеся з основами завантаження на сторінці Як завантажувати шрифти?.

Додайте наступні простори імен у заголовок файлу:

1using namespace Aspose::Font;
2using namespace Aspose::Font::Sources;
3using namespace System.IO;

Завантаження з файлу за допомогою об’єкта FileSystemStreamSource

Виконайте наступні кроки для виконання операції:

  1. Побудувати шлях до файлу.
  2. Ініціювати об’єкт FontDefiniton.
  3. Установіть fileExtension на ttf.
  4. Завантажте шрифт.
 1    // Construct path to the file
 2    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"Montserrat-Regular.ttf");
 3
 4    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5    // based on FileSystemStreamSource object, set fileExtension to "ttf"
 6    System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fontPath));
 7    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, fileDef);
 8    
 9    // Load font
10    System::SharedPtr<Font> font = Font::Open(fontDef);

Завантаження з файлу за допомогою об’єкта FileInfo

Щоб виконати завантаження, виконайте наступне:

  1. Побудувати шлях до файлу.
  2. Ініціювати об’єкт FontDefiniton, передаючи TTF як значення FontType.
  3. Отримати автоматично розраховане значення fileExtension.
  4. Завантажте шрифт.
 1     // Construct path to the file
 2    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"Montserrat-Regular.ttf");
 3
 4    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
 5    // based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
 6    System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(System::MakeObject<System::IO::FileInfo>(fontPath));
 7    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, fileDef);
 8
 9     // Load the font
10    System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);

Завантаження з файлу, виключаючи об’єкт FontFileDefinition з ланцюжка ініціалізації

Для завантаження шрифту таким чином потрібно виконати наступні дії:

  1. Побудувати шлях до файлу.
  2. Ініціювати об’єкт FontDefiniton, передаючи TTF як значення FontType, ttf як значення fileExtension і об’єкт FileSystemStreamSource . Параметр fileExtension тут не є повторюваним значенням для параметра FontType.
  3. Завантажте шрифт.
 1     // Construct path to the file
 2    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"Montserrat-Regular.ttf");
 3
 4    // Initialize FontDefinition object passing TTF as FontType value, "ttf" as fileExtension value, 
 5    // and FileSystemStreamSource object. Parameter 'fileExtension' here is not duplicate value 
 6    // for parameter 'FontType' and it's needed for correct font format detection
 7    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, u"ttf", System::MakeObject<FileSystemStreamSource>(fontPath));
 8
 9     // Load the font
10    System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);

Завантаження шрифту з байтового масиву

Щоб завантажити шрифт із масиву байтів:

  1. Побудувати шлях до файлу.
  2. Завантажте двійкові дані шрифту в масив байтів.
  3. Ініціалізуйте об’єкт FontDefiniton, передавши TTF як значення FontType, ttf як значення fileExtension і ByteContentStreamSource об’єкт на основі масиву fontBytes.
  4. Завантажте шрифт.
 1     // Construct path to the file
 2    System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"Montserrat-Regular.ttf");
 3
 4	// Load font binary data into byte array
 5    System::ArrayPtr<uint8_t> fontBytes = System::IO::File::ReadAllBytes(fontPath);
 6    
 7    // Initialize FontDefinition object  passing TTF as FontType value, "ttf" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, u"ttf", System::MakeObject<ByteContentStreamSource>(fontBytes));
10
11     // Load the font
12    System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);

Більше прикладів того, як використовувати Aspose.Font, можна знайти в [Aspose.Font.Examples.CPP.sln рішенні]( https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/cpp- приклади), у cpp-examples Aspose.Font Documentation.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.