从TTF文件加载字体| C++

此处描述了四个不同满足的示例,介绍了如何从文件蒙特塞拉特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对象传递ttffonttype值。
  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作为 fonttypevalue,ttf as fileextension值(9)值和 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作为 fonttypevalue,ttf as fileextension值(15)和 bytecontentstreamsource基于Fontbytes array的对象。
  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 解决方案Aspose.Font 文档

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.