WIN32 D2D TextLayout and Update Text

Hello, my program uses TextLayout to draw text. My problem is TextLayout can not be changed but in my program user type text so I use it to make a textbox. On Microsoft source that says I should destroy and re-create it. I did not see any performance issue or I did not observe increase of memory usage within 15 minutes. That looks like stable. Still, thats re-drawed even I do not type text.

My Question, Is there a better option to update text without recreating TextLaout ?

AS SOMETHING LIKE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*RenderText() called at rendering loop
IDWriteTextLayout textLayout_ is at Initialize. That does not go into loop.
*/

RenderText(/*...PARAMETERS...*/){

...

 m_writeFactory->CreateTextLayout(
		 text,           // The string to be laid out and formatted.
		 wcslen(text),   // The length of the string.
		 textFormat,     // The text format to apply to the string (contains font information, etc).
		 layoutSize.x,   // The width of the layout box.
		 layoutSize.y,   // The height of the layout box.
		 &textLayout_    // The IDWriteTextLayout interface pointer.
	);
	 DWRITE_TEXT_RANGE textRange; textRange.length = wcslen(text); textRange.startPosition = 0;
	 textLayout_->SetFontFamilyName(fontFamily, textRange);
	 m_d2dRenderTarget->DrawTextLayout(D2D1_POINT_2F{ (float)posX, (float)posY }, textLayout_, m_whiteBrush, D2D1_DRAW_TEXT_OPTIONS::D2D1_DRAW_TEXT_OPTIONS_CLIP); //OVERFLOW HIDDEN

	//m_d2dRenderTarget->DrawTextW(wcharText, wcslen(wcharText), textFormat, RectF(posX, posY, m_screenSize.x, m_screenSize.y), m_whiteBrush);
	m_d2dRenderTarget->PopLayer();
	m_d2dRenderTarget->EndDraw();

	textLayout_->Release();
	textLayout_ = 0;

...

}
Last edited on
My Question, Is there a better option to update text without recreating TextLaout ?

No.

TextFormat and TextLayout must be recreated each time the size of a text changes, ie. when the window resizes.

I'm not 100% sure about TextLayout but it's true for TextFormat, you can't update existing TextFormat, it must be recreated.

Since your TextLayout takes TextFormat as parameter this means you need to recreate both of them not just TextLayout.
Alright, got that well. Thanks. I will recreate it only when user types.
Btw, if you care about performance you have option to use only TextFormat for drawing text without textlayout.
In which case you use DrawText(...) instead of DrawTextLayout(...)

you can also scrap both and use richedit if it's only purpose is to input text.

Also do not forget to release textlayout and textformat before recreating, othewise you leak memory. ;)
Last edited on
Btw, if you care about performance you have option to use only TextFormat for drawing text without textlayout.
In which case you use DrawText(...) instead of DrawTextLayout(...)


Yes but If I am not wrong I will not have opportunity to wrap text to bottom line when that's bigger than specified client area. If you say "DrawText" has option to wrap it automatically when that's bigger than specified area alright I use it. In the end I will make a new function/bool in d2d class which draws a scrollbar for textbox so textbox stays at same size but I can scroll the text.

Last, for performance, that works at 900-1000FPS with Layout.
Yes but If I am not wrong I will not have opportunity to wrap text to bottom line when that's bigger than specified client area.

I guess not, since it's only purpose is to set text properties.

I was suggesting according to my experience, and asked my self the same question you were asking and figured out there is no way, but your problem is different obviously.
Registered users can post here. Sign in or register to post.