TTFファイルからフォントをロードします| C ++

ここでは、ファイルMontserrat-Regular.ttfからフォントをロードする方法に関する4つの異なる充実した例について説明します。


最初に フォントのロード方法?ページで読み込みの基礎を学びます。

ファイルの頭に次の名前空間を追加します。

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

ファイルSystemStreamSourceオブジェクトを使用してファイルからロードします

次の手順を実行して、手術を実現します。

  1. ファイルへのパスを構築します。
  2. fontdefinitonオブジェクトを開始します。
  3. fileExtensionTTFに設定します。
  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. fontdefinitonas fonttypevalueを渡すオブジェクトを開始します。
  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を開始するオブジェクト fonttypevalue、 ttf as fileextensionvalue and filesystemstreamSourceオブジェクトとしてas fonttypevalue、 ttfを渡します。パラメーター 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を初期化するオブジェクトは fonttype値として ttfを渡します。
  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 solutioncpp-examplesaspose.font documentation

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.