如何加载字体? | C ++的API解决方案
概述
为了回答这个问题,如何加载字体,首先我们需要学习任何字体在物理上都是一个字节数组,其中数据以某种格式表示。 因此,要正确的字体加载,您需要知道两个关键参数:
- 字体格式。字体可以有不同的格式,例如
TrueType
、Type1
等。 - 存储,其中代表字体的二进制数据。每个字体表示为二进制数据,但存储数据的方式可能有所不同。在大多数情况下,该字体被存储在硬盘驱动器上。但是有时可以将字体二进制数据放在不是字体本身的其他文件中。例如,由Microsoft Word或Adobe Acrobat创建的文件。这些是带有扩展名的文件
.docx
,.pdf
。它们可以包括不同的字体。
字体加载所需的对象和参数
Aspose.Font 适用于C++给出对象 fontdefiniton通知字体加载所需的两个关键参数。
在创建和启动 fontdefiniton对象之后,您只需要调用静态方法 aspose.font.font.open()(3),然后仅通过初始化对象 fontdefiniton作为参数即可加载所需的字体并在其上获取参考。
让我们用伪代码来描述它:
1using Aspose::Font;
2 ...
3
4 // Initialize object FontDefinition with appropriate properties
5 System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>();
6 System::SharedPtr<Font> desiredFont = Aspose::Font::Font::Open(fd);
字体加载的最终结果是获得 aspose.font.font类型的对象。 class aspose.font.font是C ++库的 Aspose.font的基本字体类。它代表使用字体工作的主要功能。
将字体加载到 aspose.font.font键入对象中,您将能够使用此字体呈现文本,获取编码数据和字体指标。您还可以修改或保存此字体等。
要启动 fontdefiniton对象,我们需要给出 fontdefinition()2 font参数, - 格式类型和描述字体二进制数据存储的对象。 fonttype枚举通知第一个参数。
为了简化第二个参数的工作, aspose.font提供了一系列对象,可以独立于放置字体的位置对字体二进制数据的通用访问。H
这种通用访问与字节流这样的东西链接。 因此,独立于将字体放置在磁盘上的文件中,在另一个文件中,内存中的字节数组 - 在任何情况下,特殊 aspose.font对象将提供与所需字体相关的字节流的访问。
让我们描述这些对象:
streamsource类。 该抽象基类定义了访问字体字节流的属性和方法。 getFontStream()此类方法返回与二进制字体数据有关的流。 属性 offset在此流处的位置上的点,从那里我们必须开始读取数据。
从基础 streamsource继承的两个最常用的类:
- FileSystemStreamSource - 旨在根据文件系统提供对流的访问。 属性 filename返回字体文件的名称。 getFontStream()方法返回与此文件相关的字节流。
- ByteContentStreamSource - 基于字节数组提供对流的访问。
如何开始课程?
以下是这些班级启动的示例。
- 假设字体在名为lora.ttf的文件中。在这种情况下,要访问此字体的字节流,我们需要使用此单个构造函数来创建类型 filesystemstreamsource的对象:
1 System::SharedPtr<FileSystemStreamSource> fontSource = System::MakeObject<FileSystemStreamSource>("Lora.ttf");
- 如果字体二进制数据位于由字节[] fontarray变量定义的二进制数组中,则类型为 bytecontentstreamsource的对象将根据Fontarray变量提供对字体数据流的访问。 要访问字体字节流,我们必须创建一个类型的对象 bytecontentstreamsource才能使用类似代码与字体字节数组相关的流访问流:
1 System::SharedPtr<ByteContentStreamSource> fontSource = System::MakeObject<ByteContentStreamSource>(fontArray);
因此,要提供对字体二进制数据的访问,请使用从基础 streamsource类继承的对象。此外,我们将称之为此类对象字体流源。
fontfiledefinition对象
大多数方面都位于常规文件中,但同时,一部分字体可能具有不同的数据源。要加入一个对象项文件和字体二进制流, aspose.font库提供特殊对象 fontfiledefinition。
该对象具有这样的属性:
- 文件名 - 字体文件的名称。
- fileextension - 字体文件的扩展。
- StreamSource - 字体流源。
- 偏移 - 字体字节中的偏移量。
该对象最简单且最常用的构造函数之一是具有以下签名的构造函数: FontFileDefinition (System::SharedPtr System::IO::FileInfo fontFile),当所需字体位于硬盘上的文件中时,使用此构造函数。
下面是为位于文件 C:\Windows\Fonts\Montserrat.ttf 中的字体Montserrat
启动此类对象的示例:
1 System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(System::MakeObject<System::IO::FileInfo>(u"C:\\Windows\\Fonts\\Montserrat.ttf"));
另外, fontfiledefinition可以使用以下构造函数来引用`蒙特塞拉特字体:
1 System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(System::MakeObject<FileSystemStreamSource>(u"C:\\Windows\\Fonts\\Montserrat.ttf"));
2
3 System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(u"C:\\Windows\\Fonts\\Montserrat.ttf"));
参数
fileextension应与标准相匹配,对于字体格式,扩展名。例如,如果字体格式为trueType
,则
fileextension参数的值可能为ttf
或ttc
(如果字体文件是trueType
fonts的集合)。
如果字体格式为“嵌入式打开类型”,则
fileExtension参数的值应为eot
。
下表显示了aspose.font中最常使用的C ++字体文件格式。在那里,它们具有 fileextension参数应采用的匹配值。
字体文件格式 | 文件扩展名 |
---|---|
TrueType , single font | ttf |
TrueType font collection | ttc |
Web Open Font Format | woff |
Web Open Font Format version 2.0 | woff2 |
Embedded OpenType | eot |
Adobe Type 1 font | pfa , pfb , afm , pfm |
Compact Font Format | cff , can be null |
如果字体数据不是从字节流的位置0启动,则将以下构造函数与 offset参数:
1 FontFileDefinition(System::String fileExtension, System::SharedPtr<Aspose::Font::Sources::StreamSource> streamSource, int64_t offset).
如何启动FontDefinition对象?
现在,当您了解从 streamsource类继承的对象并提供对字体字节流的访问权限以及 fontfiledefinition对象的访问,我们将向您解释如何启动 FontDefiniton对这些对象,以适当地适合这些对象。
fontdefiniton对象为您提供许多超载构造函数。 所有现有构造函数的共同特征是 fonttype参数。它描述了字体格式的类型。
如前所述,除了 fonttype值之外,应参考字体二进制数据初始化对象 fontdefiniton。
fontdefiniton构造函数的下一个参数可以在这种情况下使用:
- fontname;
- fileextension;
- streamsource类型的对象;
- fontfiledefinition类型的对象。
您可能有个疑问。我们总是传递参数 FontType,而它的值似乎与 fileExtension 一致,为什么还要将参数 fileExtension 传递给 FontDefiniton 或 FontFileDefinition 对象呢?
问题在于 FontType 的值并不总是与 fileExtension 的值一致。 FontType 的值一致。 FontType 定义了通用字体格式,但没有定义特定文件的字体格式。某些同一种格式的字体可能对应不同的文件格式。
例如,值 FontType.TTF 定义了 TrueType
格式。但同时,TrueType
包含几种字体格式,其中包含以 ttf
、eot
、ttc
等为扩展名的字体文件。例如,如果我们将 EOT
格式的字体仅传递给
FontDefiniton ,那么该对象如何理解该字体属于 EOT
格式而不是 TTF
格式?
同样,FontType.Type1 的值是此格式字体的通用定义。同时,Type1
格式的字体文件具有 .pfa
、.pfb
、.afm
和 .pfm
扩展名。因此,仅依靠 FonType.Type1 值无法正确定义字体文件格式。因此,为了正确定义字体格式,我们需要将
FontType 参数的值与
fileExtension 参数的值一起指定。
在下面,您可以学习 fontdefiniton对象初始化的示例和不同情况下的字体加载。
在所有情况下,最终的字体加载结果都放入 aspose.font.font类型的变量中。这种类型是 ASPOSE.FONT库的基本字体类,此类型的对象为使用字体提供了共同的基本功能。
将字体加载到此对象中,您将能够使用此字体呈现文本,获取编码信息,字体指标等。
如何加载字体的示例
让我们举一个例子,从文件C上加载字体montserrat
在文件的头部添加下一个名称空间:
1using Aspose::Font;
2using Aspose::Font::Sources;
3using System::IO;
您可以使用 fontdefiniton和 fontfiledefinition对象以几种不同的方式加载此字体:
借助 System::IO::FileInfo 对象加载
要实现加载,请执行下一个:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
ttf
为 fonttype值。 - 获取自动计算的值 fileextension。
- 加载字体。
1 // Construct path to the file
2 System::String fontPath = u"C:\\Windows\\Fonts\\Montserrat.ttf";
3
4 // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
5 System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(System::MakeObject<System::IO::FileInfo>(fontPath));
6
7 // Based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
8 System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, fileDef);
9
10 // Load font
11 System::SharedPtr<Font> font = Font::Open(fontDef);
借助filesystemstreamsource类型对象加载
采取下一步完成操作:
- 构建文件的路径。
- 启动 fontdefiniton对象。
- 将
*fileextension设置为
ttf
。 - 加载字体。
1 // Construct path to the file
2 System::String fontPath = u"C:\\Windows\\Fonts\\Montserrat.ttf";
3
4 // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
5 System::SharedPtr<FontFileDefinition> fileDef = System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fontPath));
6
7 // Based on FileSystemStreamSource object, set fileExtension to "ttf"
8 System::SharedPtr<FontDefinition> fontDef = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, fileDef);
9
10 // Load font
11 System::SharedPtr<Font> font = Font::Open(fontDef);
字体加载无FontfiledEfinition对象,将FifeSystreamSource直接传递给FontDefinition
必须采取下一个操作以使字体加载:
- 构建文件的路径。
- 启动
fontdefiniton对象传递
ttf
as fonttypevalue,ttf
as fileextension值和 FileSystemStreamSource对象。参数 fileextension这不是参数 fonttype的重复值。 - 加载字体。
1 // Construct path to the file
2 System::String fontPath = u"C:\\Windows\\Fonts\\Montserrat.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 font
10 System::SharedPtr<Font> font = Font::Open(fontDef);
字体加载字节[]类型变量以及使用ByteContentsTreamSource类型对象
要从字节数组加载字体,您需要:
- 构建文件的路径。
- 将字体二进制数据加载到字节数组中
- 初始化
fontdefiniton对象传递
ttf
为 fonttypevalue,ttf
as fileextensionvalue和 bytecontentstreamsource(81)基于fontbytes array上的对象。 - 加载字体。
1 // Construct path to the file
2 System::String fontPath = u"C:\\Windows\\Fonts\\Montserrat.ttf";
3
4 // Load font binary data into byte array
5 System::ArrayPtr<uint8_t> fontBytes;
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 font
12 System::SharedPtr<Font> font = Font::Open(fontDef);
要获取更多使用 Aspose.Font 的示例,请转到 Aspose.Font 文档 中的 Aspose.Font.Examples.CPP.sln 解决方案。
如果您剩下任何问题或问题,则可以将它们发布在 ASPOSE.FONT.PRODUCT.REMPLE 免费支持论坛的部分中,并且在几个小时内我们的支持团队将为您清除所有内容。