在SharpDX / DirectWrite中获取ABC字符宽度



在GDI中获取我称之为GetCharABCWidths的字符的ABC宽度(左方位角,右方位角等(。

如何使用 SharpDX 或 DirectWrite 实现相同的测量,我是否应该期望此值与相同字符的 GDI 之间的值匹配?

到目前为止,我做了什么?由于缺乏 Direct* 和 SharpDX 的文档而迷失方向。

/// <summary>
///  Takes a string, text format, and associated constraints, and produces an object that represents the fully analyzed and formatted result.
/// </summary>
/// <param name="factory">an instance of <see cref="T:SharpDX.DirectWrite.Factory" /></param>
/// <param name="text">An array of characters that contains the string to create a new <see cref="T:SharpDX.DirectWrite.TextLayout" /> object from. This array must be of length stringLength and can contain embedded NULL characters.</param>
/// <param name="textFormat">A pointer to an object that indicates the format to apply to the string.</param>
/// <param name="maxWidth">The width of the layout box.</param>
/// <param name="maxHeight">The height of the layout box.</param>
/// <unmanaged>HRESULT CreateTextLayout([In, Buffer] const wchar* string,[None] UINT32 stringLength,[None] IDWriteTextFormat* textFormat,[None] FLOAT maxWidth,[None] FLOAT maxHeight,[Out] IDWriteTextLayout** textLayout)</unmanaged>
public TextLayout(Factory factory, string text, TextFormat textFormat, float maxWidth, float maxHeight)

示例代码:

using (var dwFactory = new SharpDX.DirectWrite.Factory())
{
  var textLayout = new TextLayout(dwFactory, "ABC", textFormat, float.PositiveInfinity, float.PositiveInfinity);
  var width = textLayout.Metrics.Width
  ...
}

最新更新