# [SlimDX] Rendering text with Direct2D

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

## Recommended Posts

I'm having problems rendering text to an absolute (x, y) pixel position using SlimDX's Direct2D API. It seems that both the DrawTextLayout and DrawText methods require a bounding rectangle for the string, basically its size measurement, but there seems to be no way to measure the size of a string using SlimDX. Does anybody know of a way to measure the size of string using SlimDX's DirectWrite implementation? Without this I can't see a way of rendering strings in SlimDX's Direct2D API. I understand that a possible solution to this would be the implementation of the GetMetrics() method. Does anybody know if this method has been implemented in the lastest (non-public) version of SlimDX? Is there any way I can help in getting it implemented if it isn't? Many thanks!

##### Share on other sites
You actually don't really have to set the bounding rect unless you want your text to be clipped.

It's basically meant to be used the other way round. You define an area for your text, and if the text is too long or high, it will be clipped if you want it to be clipped (have the Draw Text Options set to clip).

Check this out:
http://msdn.microsoft.com/en-us/library/dd371919(VS.85).aspx

##### Share on other sites
Michael,

Quote:
 Original post by michaweyelYou actually don't really have to set the bounding rect unless you want your text to be clipped.

If I don't set the bounding rect, the text draws vertically. If I set the bounding rect even a little bit bigger than the actual size of the text (having modified all alignment properties) the top-left corner of the text is not exactly at the specified (x, y) position.

This is a problem for me, as in my application it is very important that strings of different sizes and of different characteristics (font etc.) are very precisely placed. What is more, the position of several elements of my application depend on being able to calculate accurately the correct size of the string.

Thank you!

##### Share on other sites
The reason why you see it vertically is because WordWrap is still on its default (True). You can find it in your SlimDX.DirectWrite.TextFormat object.

 _drawTextOptions = Direct2D.DrawTextOptions.None _textFormat = New TextFormat(DevicePool.DWRFactory, _ familyName, _ FontWeight.Normal, _ FontStyle.Normal, _ FontStretch.Normal, _ fontSize, _ "en-us") With _textFormat .FlowDirection = FlowDirection.TopToBottom .IncrementalTabStop = .FontSize * 4 ' already set as default .ParagraphAlignment = ParagraphAlignment.Near .ReadingDirection = ReadingDirection.LeftToRight .TextAlignment = TextAlignment.Leading .WordWrapping = WordWrapping.NoWrap End With

This is how you want to declare it with all parameters visible to adjust. Use NoWrap to fix the vertical text...