Silverlight:字形宽度

Til*_*ilo 29 .net c# silverlight windows-phone-7

脚本

我想Glyphs在WP7 上使用来创建一个合理的文本行,即触摸周围矩形的左右边框.

我的解决方案

var glyphs = new Glyphs();
glyphs.FontUri = new Uri("/MyAssembly;component/MyPath/MyFont.ttf", UriKind.Relative);
glyphs.FontRenderingEmSize = 20;
glyphs.Fill = new SolidColorBrush(Colors.Red);

// measue width of space
glyphs.UnicodeString = " ";
glyphs.Measure(availableSize);
double spaceWidth = glyphs.DesiredSize.Width;
glyphs.InvalidateMeasure();

// setup justified text
string text = "Lorem Ipsum is dummy text of the printing and typesetting industry.";
int spaceCount = 10; // number of spaces in above text

glyphs.UnicodeString = text;
glyphs.Measure(availableSize); // now DesiredSize.Width = width of left aligned text

// I suspect my error to be in this formula:
double spaceAdvance = ((availableSize.Width - glyphs.DesiredSize.Width) 
                       / spaceCount + spaceWidth) / glyphs.FontRenderingEmSize * 100;
string spaceAdvanceString = String.Format(",{0};", spaceAdvance);

var indices = new StringBuilder();
foreach (char c in text)
{
    if (c == ' ')   indices.Append(spaceAdvanceString);
    else            indices.Append(';');
}
glyphs.Indices = indices.ToString();
Run Code Online (Sandbox Code Playgroud)

问题和疑问

字形的右侧并没有完全触及availableSize.Width-Border,而是关闭了一些像素,当有多行文本堆叠起来时,看起来很像.

我的计算有什么问题?

Jas*_*per 1

替代建议:您可以使用支持“justify”的RichTextBox控件。