This project has moved and is read-only. For the latest updates, please go here.


MeasureString ignoring trailing spaces.


Hey. First I'd like to thank you a lot for the great work on this tool kit. Porting my code from XNA to SharpDX was only made possible because of this tool kit (I started porting with SharpDX 2.3 - before they introduced their tool kit).

I realize this issue was already raised here, where you stated that you really don't see this as an issue. Well, it might be an issue for some, like me, who are implementing any sort of text editor with a caret. My implementation uses SpriteFont::MeasureString to determine the position of the caret and requires trailing spaces accounted for. For a [quite ugly] work around, I intend to
  1. create string1, a substring of the trailing spaces.
  2. create string2, containing just one arbitrary letter character (ie: 'W').
  3. measure string2.
  4. create string3 by appending string2 to string1.
  5. measure string 3.
  6. calculate the length of trailing spaces as length-of-string3 minus length-of-string2.
Right now, I see no other approach to move the caret in my textbox/editor.
Closed Feb 6, 2013 at 12:26 AM by ShawnHargreaves
I think that's a reasonable approach for an editor app.

The problem with text layout is that different use cases want different behaviors. For an interactive editor, counting trailing spaces is good, but if you are implementing a wordwrap function, or want to fit a border around a block of formatted text, you want those extra spaces ignored!

There's no single solution that works for everyone, but if you add options to make this infinitely configurable, then you end up with a really complex yet powerful API such as DWrite. But we already have DWrite, so there is no point extending SpriteFont to make it just as complicated :-)

I think more people will use SpriteFont for game HUD overlays, which benefit from things like centering in a box or calculating tight fit size of border, and less for interactive editing, so it's better to optimize the design for the former use case.