从CFF文件加载字体| C++
Contents
[
Hide
Show
]在此页面上,我们将查看file centurygothic.cff中加载字体centurygothic
的示例。
如果您没有阅读Aspose.font加载基本面,请转到 如何加载字体?页。
首先,您需要通知文件头部的下一个名称空间:
1using namespace Aspose::Font;
2using namespace Aspose::Font::Sources;
3using namespace System.IO;
使用FileInfo对象从文件加载
遵循该算法来实现字体加载:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
cff
为 fonttype值。 - 获取自动计算的值 fileextension。
- 加载字体。
1 // Construct path to the file.
2 System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"CenturyGothic.cff");
3
4 // Initialize FontDefinition object passing CFF 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::CFF, fileDef);
8
9 // Load the font
10 System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);
字体加载字节[]类型变量以及使用ByteContentsTreamSource类型对象
要通过这种方式加载字体,您需要采取以下步骤:
- 构建文件的路径。
- 初始化
fontdefiniton对象将
cff
作为 fonttype值,cff
as fileextension值(7)和 bytecontentstreamsource基于fontbytes array array array array上。 - 加载字体。
1 // Construct path to the file
2 System::String fontPath = System::IO::Path::Combine(get_DataDir(), u"CenturyGothic.cff");
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 CFF as FontType value, "cff" as fileExtension value,
8 // and ByteContentStreamSource object based on fontBytes array
9 System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, u"ttf", System::MakeObject<ByteContentStreamSource>(fontBytes));
10
11 // Load font and print results
12 System::SharedPtr<Aspose::Font::Font> font = Aspose::Font::Font::Open(fontDef);
有关如何使用 Aspose.Font 的更多示例,请参阅 Aspose.Font.Examples.CPP.sln 解决方案 和 Aspose.Font 文档。