Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight: Glyphs Width
    text
    copied!<h2>Scenario</h2> <p>I want to use <code>Glyphs</code> on WP7 to create a line of text that is justified, i.e. touches the left and right border of the surrounding rectangle.</p> <h2>My solution</h2> <pre><code>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(); </code></pre> <h2>Problem and Question</h2> <p>The right side of the glyphs is not exactly touching the <code>availableSize.Width</code>-Border but is some pixels off, and that looks weired when there are several lines of text stacked up.</p> <p>What is wrong with my calculation?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload