FormattedText Width 属性不考虑尾随空格

Tar*_*don 5 c# wpf formatting text formatted-text

我正在System.Windows.Media.FormattedText做一些低级渲染(特别是,尝试以排版上令人愉悦的方式渲染数学方程)。为此,我使用的文本块的精确指标至关重要。

我正在创建多个FormattedText对象并在最低级别的渲染中使用这些对象。问题是,如果其中任何一个包含尾随空格,则在计算FormattedText.Width属性时不会考虑该空格。例如,如果我写:

double w1 = new FormattedText ("Hello", ...).Width;
double w2 = new FormattedText ("Hello    ", ...).Width;
Run Code Online (Sandbox Code Playgroud)

w1 和 w2 原来是一样的。前导空格正确的测量。我如何强制FormattedText测量这些尾随空格?

Ada*_*itt 7

从使用Width属性变为使用WidthIncludingTrailingWhitespace属性。

double w1 = new FormattedText ("Hello", ...).WidthIncludingTrailingWhitespace;
double w2 = new FormattedText ("Hello    ", ...).WidthIncludingTrailingWhitespace;
Run Code Online (Sandbox Code Playgroud)

使用此代码,您应该看到两个不同的宽度值。