读取字形和度量信息

C++ API 解决方案用于计算字符串宽度、计算字形坐标,以及其他字形操作。

 

Aspose.Font API 提供了全面的字体相关功能,包括转换、字形操作、拉丁符号检测等。其中一些功能涉及字形的操作,字形是字体的单个字符或符号/字符的图形表示。要了解更多关于这些字体单元,请参阅 字形简介 文章。

本页概述了读取字形和度量信息的选项,但要全面了解功能,请参考文章 使用字形对象 。在此您将找到大量 C++ 代码示例,并了解 Aspose.Font 在处理字形和字形对象方面的能力。更多展示 Aspose.Font 用于操作字形的代码示例可在 Aspose Github 项目 中找到。

字形度量,此处使用的术语,指的是字体中特定字形的信息和测量值。它们可能包括字形的高度、宽度、前进宽度以及其他尺寸,这对于字符在字体中的正确定位和间距至关重要。这些度量被文本布局和渲染系统使用,以确保文本显示的一致性和准确性。

要使用字形我们需要:

  • 功能丰富、强大且易于使用的文档操作和转换 API——Aspose.Font for C++ API。

  • 打开 NuGet 包管理器,搜索 Aspose.Font 并安装。您也可以使用以下命令从包管理器控制台进行安装。

Package Manager Console Command


    PM> Install-Package Aspose.Font

使用 C++ 计算字符串宽度的步骤:

要使用字形计算字符串宽度,首先需要确定字符串中每个字形的宽度,然后将这些宽度相加得到总宽度。如果按步骤解释,顺序如下:

  1. 确定用于计算字符串宽度的字体和大小。
  2. 对字符串中的每个字符,确定字体中对应的字形。使用字符映射,可获得相应字形的索引。
  3. 使用字形索引,在字体的度量表中查找该字形的度量信息。字形的度量通常包括宽度、高度和前进宽度。
  4. 将字符串中所有字形的前进宽度相加,得到字符串的总宽度。注意关注字距信息。

如果使用 C++ 代码描述类似的过程,它将如下所示。

用于获取字符串宽度的 C++ 代码

    using System;
    using System::IO;
    using System::Drawing;
    using System::Collections::Generic;
    using Aspose::Font::Sources;
    using Aspose::Font::Glyphs;
    using Aspose::Font::RenderingPath;
        //Declare the text and other constants
        const System::String text = u"Hello world";
        const int32_t fontSize = 10;
    
        //Declare the variable for string width
        double width = 0;
    
        //Get glyph for each letter in text and calculate width for whole text.
        //The same result can be achieved using method font.Metrics.MeasureString(text, fontSize).
        for (char16_t symbol : text)
        {
            System::SharedPtr<GlyphId> gid = this->_font->get_Encoding()->DecodeToGid(symbol);
            System::SharedPtr<Glyph> glyph = this->_font->GetGlyphById(gid);
            width += (glyph->get_WidthVectorX() / this->_font->get_Metrics()->get_UnitsPerEM()) * fontSize;
        }
	    font->get_Metrics()->MeasureString(text, width);

        //Print the output results
        System::Console::WriteLine(System::String::Format(u"Width for text \"{0}\" with font size {2} is equal {3}.", text, FontName, fontSize, width));

使用 C++ 计算所有字形点坐标的步骤:

  1. 声明类型为 System::Drawing::Point 的列表 points :此列表将存储字形路径段的点。
  2. IPathSegment 接口上指定 Init 服务引用。
  3. 遍历所有字形路径段并累计这些点。

用于获取字形点的 C++ 代码

    //Declare the resultant list with points
    System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points =
    System::MakeObject<System::Collections::Generic::List<System::Drawing::Point>>();
    
    //Init the service reference on IPathSegment
    System::SharedPtr<IPathSegment> prevSegment;
    
    //Iterate all the glyph path segments and collect points
    for (auto&& segment : glyph->get_Path()->get_Segments())
    {
        if ((System::ObjectExt::Is<LineTo>(segment)) || (System::ObjectExt::Is<CurveTo>(segment)))
        {
            if (System::ObjectExt::Is<MoveTo>(prevSegment))
            {
                System::SharedPtr<MoveTo> moveTo = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::MoveTo>(prevSegment);
                AddPoint((int32_t)moveTo->get_X(), (int32_t)moveTo->get_Y(), points);
            }
            if (System::ObjectExt::Is<LineTo>(segment))
            {
                System::SharedPtr<LineTo> line = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::LineTo>(segment);
                AddPoint((int32_t)line->get_X(), (int32_t)line->get_Y(), points);
            }
            else if (System::ObjectExt::Is<CurveTo>(segment))
            {
                System::SharedPtr<CurveTo> curve = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::CurveTo>(segment);
                AddPoint((int32_t)curve->get_X1(), (int32_t)curve->get_Y1(), points);
                AddPoint((int32_t)curve->get_X2(), (int32_t)curve->get_Y2(), points);
                AddPoint((int32_t)curve->get_X3(), (int32_t)curve->get_Y3(), points);
            }
        }
        prevSegment = segment;
    }



常问问题

1. 什么是字形度量?

字形度量 是在创建文本布局时影响字形定位方式的参数。

2. 什么是字形指标?

最常用的字形度量是前进宽度、原点、侧方位、基线、上升、上升、下降、下降、边界框、高度、宽度和字偶距。

3. 如何使用此 API 解决方案管理字形?

要在 C# 中编码字形,请使用 Aspose.Font.Gly​​phs 命名空间的实体。

4. 字形指标的重要性是什么?

通过理解并正确调整字形指标,印刷师和设计师可以确保印刷材料、数字界面或网页中文本的最佳间距、对齐和易读性。