从TTF文件加载字体| C++
Contents
[
Hide
Show
]此处描述了四个不同满足的示例,介绍了如何从文件蒙特塞拉特regular.ttf中加载字体。
首先在 如何加载字体上学习加载基础知识?页。
在文件的头部添加下一个名称空间:
1using namespace Aspose::Font;
2using namespace Aspose::Font::Sources;
3using namespace System.IO;
使用filesystemstreamsource对象从文件加载
采取下一步完成操作:
- 构建文件的路径。
- 启动 fontdefiniton对象。
- 将
fileextension设置为
ttf
。 - 加载字体。
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对象从文件加载
要实现加载,请执行下一个:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
ttf
为 fonttype值。 - 获取自动计算的值 fileextension。
- 加载字体。
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对象
必须采取下一个操作以通过这种方式加载字体:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
ttf
作为 fonttypevalue,ttf
as fileextension值(9)值和 FilesystemStreamSource对象。参数 fileextension这不是参数 fonttype的重复值。 - 加载字体。
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);
从字节数组加载字体
从字节数组加载字体:
- 构建文件的路径。
- 将字体二进制数据加载到字节数组中。
- 初始化
fontdefiniton对象将
ttf
作为 fonttypevalue,ttf
as fileextension值(15)和 bytecontentstreamsource基于Fontbytes array的对象。 - 加载字体。
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 文档。