使用字形对象| C ++的API解决方案
在编码字体时,使用字形与其单元一样至关重要。当 是什么是字体段落的 Artract中解释有关字形的基本信息时,我们将在这里教您Aspose.font的功能。
字形对象
任何字体的字形以 glyph在aspose.font库中键入。 因此, glyph类型是字体字形的通用对象,独立于字体格式(trueType,compact字体格式等)。
字形对象提供的功能
这个对象为我们提供什么功能?
它的属性回答了这个问题。
- 首先,让我们看一下属性 状态。某些字体可能会损坏,并且它们的字形也可能被损坏。 财产 *状态 *告诉我们字形是否已损坏。如果它具有 parsedwitherrors的值,则该字形被错误解析,并且并非收到所有字形数据。
- 属性 widthVectorx和 左Sidebearingx告诉我们这样的 Glyph Metrics作为预先宽度和左侧轴承。
- 属性 widthVectory和 左Sidebearingy具有与widthVectorx和leftsidebearingx**的感觉,但是widthVectoryandleftsidebearingy*与垂直Y轴的坐标有关。
让我们使用下一个代码段的 示例,该代码段显示如何计算字体大小为10时在像素中的文本“ hello world”的宽度。
在文件的头部添加下一个名称空间:
1using namespace Aspose::Font::Glyphs;
2System::SharedPtr<Aspose::Font::Font> font;
然后,您需要采取下一步:
1 //Declare text and other constants
2 const System::String text = u"Hello world";
3 const int32_t fontSize = 10;
4
5 //Declare variable for string width
6 double width = 0;
7
8 //Get glyph for each letter in text and calculate width for whole text.
9 //The same result can be achieved using method font->get_Metrics()->MeasureString(text, fontSize).
10 for (char16_t symbol : text)
11 {
12 System::SharedPtr<GlyphId> gid = this->_font->get_Encoding()->DecodeToGid(symbol);
13 System::SharedPtr<Glyph> glyph = this->_font->GetGlyphById(gid);
14 width += (glyph->get_WidthVectorX() / this->_font->get_Metrics()->get_UnitsPerEM()) * fontSize;
15 }
要获得字形的边界框,请使用 glyphbbox glyph对象的属性。
要获得字形的视觉表示,您需要了解所有字形的坐标。
如何从字形对象中获取所有字形点的坐标?
属性 Isempty是辅助。它告诉我们,字形的路径是空的,换句话说,字形根本没有绘图说明。如果它具有值 false,则该使用非常有用的属性 路径构造整个字形的数字了。
在Aspose.font库的概念中,任何字形的表示形式都分为最简单的图形原始图,称为semgments,并由接口 ipathsegment表示。 接口 iPathseggent是一个基本的抽象图形原始。
混凝土图形原始词由 Moveto, lineto, curveto和 ClosePath等类型表示。
类型 ClosePath用于指示当前图形轮廓的末尾。
类型 moveto, *lineto *和 curveto根据其定义与相同的后记运算符相对应。
另外,类型 moveto and lineto按定义对应于函数moveToex()
andlineto()
从Windows gdi lib,type curveto用于描述 bézier曲线。
glyph属性路径为我们提供了该字形的所有图形原始图。 属性路径具有类型 segmentPath,并且此类型的每个对象都具有属性 sengments的类型 PathSegmentSegrection Collection。此属性 段返回所有object segmentpath包含的图形原始图。换句话说,我们可以使用条目Glyph.path.segments获取字形的所有图形原始图。
下一个 示例计算雕文所具有的所有点,并将它们存储在变量`‘‘中,该点代表具有 point类型的对象数组。
该样本使用的逻辑很简单,并且不会提取雕文轮廓。为了获得这些轮廓,使用类型 ClosePath必须添加到段处理中。
在文件的头部添加下一个名称空间:
1using System::Collections::Generic;
2using System::Drawing;
3using Aspose::Font::Glyphs;
4using Aspose::Font::RenderingPath;
然后,您需要采取下一步:
1 System::SharedPtr<Glyph> glyph;
2
3 //Declare resultant list with pints
4 System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points = System::MakeObject<System::Collections::Generic::List<System::Drawing::Point>>();
5
6 //Init service reference on IPathSegment
7 System::SharedPtr<IPathSegment> prevSegment;
8
9 //Iterate all glyph path segments and collect points
10 for (auto&& segment : glyph->get_Path()->get_Segments())
11 {
12 if ((System::ObjectExt::Is<LineTo>(segment)) || (System::ObjectExt::Is<CurveTo>(segment)))
13 {
14 if (System::ObjectExt::Is<MoveTo>(prevSegment))
15 {
16 System::SharedPtr<MoveTo> moveTo = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::MoveTo>(prevSegment);
17 AddPoint((int32_t)moveTo->get_X(), (int32_t)moveTo->get_Y(), points);
18 }
19 if (System::ObjectExt::Is<LineTo>(segment))
20 {
21 System::SharedPtr<LineTo> line = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::LineTo>(segment);
22 AddPoint((int32_t)line->get_X(), (int32_t)line->get_Y(), points);
23 }
24 else if (System::ObjectExt::Is<CurveTo>(segment))
25 {
26 System::SharedPtr<CurveTo> curve = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::CurveTo>(segment);
27 AddPoint((int32_t)curve->get_X1(), (int32_t)curve->get_Y1(), points);
28 AddPoint((int32_t)curve->get_X2(), (int32_t)curve->get_Y2(), points);
29 AddPoint((int32_t)curve->get_X3(), (int32_t)curve->get_Y3(), points);
30 }
31 }
32 prevSegment = segment;
33 }
34
35void GlyphMetrics::AddPoint(int32_t x, int32_t y, System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points)
36{
37 System::Drawing::Point p;
38 p.set_X(x);
39 p.set_Y(y);
40 points->Add(p);
41}
所有使用 Aspose.Font 的示例均存储在 Aspose.Font.Examples.CPP.sln 解决方案 中,以及 Aspose.Font 文档 的 cpp-examples 中。
如果您有任何问题,可以在 免费支持论坛 的 Aspose.Font.Product Family 版块提交,我们的支持团队将在几个小时内为您解决所有问题。