Jump to content
  • Advertisement
Sign in to follow this  

Managed DX9 MeasureString

This topic is 3801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently developing a UI and have run into troubles while creating a multiline texbox or RichTexBox if you will. The problem it self is that the Font.MeasureString returns a Rectangle who's dimensions are not directly applicable to the desired target surface dimension, primarily the width. Those that know the API call may recall that you cannot supply a Rectangle, in which the text is to be formatted, as function parameter. So what you get is a rectangle that measures the total length of the string (as width) and line height (as height). A little concept background: A multiline textbox can contain a very large amount of text, 2^32 characters in my case. It would therefore be reasonable to divide the text into "buckets" (I chose separated by newline which is suitable for the currently intended use) and only render visible text. So the multiline textbox can be described as a rectangle with a number of whole or partial "buckets" inside it. So due to MeasureString returning a rectangle representing a string as a single line I had to come up with a workaround that works for most cases. And that is to recalculate the dimensions as: Say: target rectangle have dimsnions W x H and, "bucket" returned by MeasureString has dimensions w x h The the new dimensions for the "bucket" are: h0 = Math.Ceil( w / W ) * h w0 = W This ensures, according to my understanding, that the "bucket" fits into the textbox rectangle possibly clipped topwise, bottomwise or both but not right or left which is the desired result. The w / W returns the number of lines the string can be split into, and Math.Ceil ensures that partial lines are included as a whole line. Thus you say number of line times line height and you have the height dimension. The width is given. However ... I have experienced cases where my calculations give say what corresponds to a 3 line bucket is actually drawn as a 4 line bucket. This results in a ackward rendering of text since the recalculated rectangle is provided to the Fond.DrawText call. The cause is that the DrawTextFormat parameter that I provide has the DrawTextFormat.WordBreak flag set. My math does not include hyphenation whereas line break tries to fit them in wherever possible. Therefore I will continue the see cases where my calculation will return lines equal or less of that wich is being rendered. If I do not include WordBreak the, it will be drawn as a single clipped line and the whole idea falls apart. The basic reason for the measuring of buckets is to determine what (how much) text to draw. I am having problems figuring out what to do to overcome this. Any ideas, comments?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!