字体渲染与字形信息

jac*_*ack 6 c# c++ algorithm fonts rendering

我调用"GetCharABCWidthsFloatW"来获取角色的宽度信息.有了这个,我将获得左侧轴承,右侧轴承和先进的宽度.

为了定位每个字符,我将从一个从零开始的"xPlacement"变量开始.我将首先通过减去"左侧轴承"来调整xPlacement变量.绘制完角色后,我会按角色的宽度前进(我稍后会显示计算结果).然后,我将通过添加当前"xPlacement"中的"右侧方位"信息来移动xPlacement变量.

在我看来,这应该是字符放置的代码,对吗?

重要的是要纠正字符的宽度.宽度将通过采用advancedWidth,左侧轴承的POSITIVE版本和右侧轴承的POSITIVE版本来计算.我会将这些值转换为正值,如果它们是负数,那么我可以得到字符的总宽度.

这是一些关于如何生成的伪代码.

float xPlacement = 0.0f;
for(int i = 0; i < strlen(text); ++i)
{
 char charValue = text[i];
 GetCharWidthABC(.., .., charInfo);

 float posLeft = charInfo.leftSideBearing;
 if(charInfo.leftSideBearing < 0)
  posLeft = -charInfo.leftSideBearing;

 float posRight = charInfo.rightSideBearing;
 if(posRight < 0)
  posRight = -charInfo.rightSideBearing;

 float posWidth = posRight + posRight + charInfo.advancedWidth;

 float letterWidth = posWidth;

 xPlacement  -= charInfo.leftSideBearing;

 /* generated some vertex coordinates, using the xPlacement variable and letterWidth */

 xPlacement += letterWidth;
 xPlacement += charInfo.rightSideBearing
}
Run Code Online (Sandbox Code Playgroud)

这似乎是正确的方法吗?

Sim*_*ier 3

由于问题标签中提到了 C#,因此您可能会对查看 .NET Framework 附带的一些非常强大的类(从版本 3.0 开始,实际上随 WPF 核心程序集附带)感兴趣。他们是:

  • GlyphTypeFace:指定与磁盘上的字体文件相对应的物理字体。对于解析 .TTF 文件并从中构建字形很有用。
  • GlyphRun:表示来自单一字体、单一尺寸、单一渲染风格的一系列字形,具有许多有用的属性和方法(例如,您可以从中构建几何图形)
  • GlyphRunDrawing:表示渲染 GlyphRun 的 Drawing 对象。也许对您来说不太有趣,因为它与 WPF 渲染/合成系统更相关。

前两个类在某种程度上与技术/设备无关,除了 .NET Framework 本身之外。