| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | #include "..\enodes.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::Text() | 
					
						
							| 
									
										
										
										
											2017-10-26 17:17:30 +08:00
										 |  |  |  | 	: m_bWordWrapping(false) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	, m_pFont(nullptr) | 
					
						
							|  |  |  |  | 	, m_fWordWrappingWidth(0) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	this->setFont(new Font()); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::Text(const String & text) | 
					
						
							| 
									
										
										
										
											2017-12-15 21:51:07 +08:00
										 |  |  |  | 	: m_bWordWrapping(false) | 
					
						
							|  |  |  |  | 	, m_pFont(nullptr) | 
					
						
							|  |  |  |  | 	, m_fWordWrappingWidth(0) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	this->setText(text); | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	this->setFont(new Font()); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::Text(Font * font) | 
					
						
							| 
									
										
										
										
											2017-12-15 21:51:07 +08:00
										 |  |  |  | 	: m_bWordWrapping(false) | 
					
						
							|  |  |  |  | 	, m_pFont(nullptr) | 
					
						
							|  |  |  |  | 	, m_fWordWrappingWidth(0) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	this->setFont(font); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::Text(const String & text, Font * font) | 
					
						
							| 
									
										
										
										
											2017-12-15 21:51:07 +08:00
										 |  |  |  | 	: m_bWordWrapping(false) | 
					
						
							|  |  |  |  | 	, m_pFont(nullptr) | 
					
						
							|  |  |  |  | 	, m_fWordWrappingWidth(0) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	this->setText(text); | 
					
						
							|  |  |  |  | 	this->setFont(font); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::Text(const String & text, String fontFamily, float fontSize, UINT32 color, UINT32 fontWeight, bool italic) | 
					
						
							| 
									
										
										
										
											2017-12-16 15:49:48 +08:00
										 |  |  |  | 	: m_bWordWrapping(false) | 
					
						
							|  |  |  |  | 	, m_pFont(nullptr) | 
					
						
							|  |  |  |  | 	, m_fWordWrappingWidth(0) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	this->setText(text); | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	this->setFont(new Font(fontFamily, fontSize, color, fontWeight, italic)); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::~Text() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 	SafeRelease(&m_pFont); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::String e2d::Text::getText() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_sText; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | float e2d::Text::getWidth() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_fWordWrappingWidth * m_fScaleX; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | float e2d::Text::getRealWidth() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_fWordWrappingWidth; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Font * e2d::Text::getFont() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_pFont; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Text::setText(const String & text) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	m_sText = text; | 
					
						
							|  |  |  |  | 	_initTextLayout(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Text::setFont(Font * font) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	if (font) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-10-31 17:19:13 +08:00
										 |  |  |  | 		SafeRelease(&m_pFont); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		m_pFont = font; | 
					
						
							|  |  |  |  | 		font->retain(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		_initTextLayout(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 20:38:50 +08:00
										 |  |  |  | void e2d::Text::setWordWrappingEnable(bool value) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	m_bWordWrapping = value; | 
					
						
							|  |  |  |  | 	_initTextLayout(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Text::setWordWrappingWidth(float wordWrapWidth) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	m_fWordWrappingWidth = max(wordWrapWidth, 0); | 
					
						
							|  |  |  |  | 	_initTextLayout(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Text::onRender() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_pFont->m_Color, m_fDisplayOpacity)); | 
					
						
							|  |  |  |  | 	Renderer::getRenderTarget()->DrawTextW( | 
					
						
							| 
									
										
										
										
											2017-12-11 18:17:24 +08:00
										 |  |  |  | 		m_sText, | 
					
						
							| 
									
										
										
										
											2018-02-27 16:32:17 +08:00
										 |  |  |  | 		static_cast<UINT32>(m_sText.getLength()), | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		m_pFont->_getTextFormat(), | 
					
						
							|  |  |  |  | 		D2D1::RectF( | 
					
						
							|  |  |  |  | 			0, | 
					
						
							|  |  |  |  | 			0, | 
					
						
							|  |  |  |  | 			m_bWordWrapping ? m_fWordWrappingWidth : m_Size.width, | 
					
						
							|  |  |  |  | 			getRealHeight() | 
					
						
							|  |  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 		Renderer::getSolidColorBrush() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Text::_initTextLayout() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	// δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ϊ 0
 | 
					
						
							| 
									
										
										
										
											2017-12-11 18:17:24 +08:00
										 |  |  |  | 	if (!m_pFont || m_sText.isEmpty()) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 		this->_setSize(0, 0); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		m_fWordWrappingWidth = 0; | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// δ<><CEB4><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TextFormat <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (!m_bWordWrapping) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_pFont->_getTextFormat()->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_pFont->_getTextFormat()->SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// <20><>ȡ TextLayout
 | 
					
						
							|  |  |  |  | 	IDWriteTextLayout * pDWriteTextLayout = nullptr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	HRESULT hr = Renderer::getIDWriteFactory()->CreateTextLayout( | 
					
						
							| 
									
										
										
										
											2017-12-11 18:17:24 +08:00
										 |  |  |  | 		m_sText, | 
					
						
							| 
									
										
										
										
											2018-02-27 16:32:17 +08:00
										 |  |  |  | 		static_cast<UINT32>(m_sText.getLength()), | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		m_pFont->_getTextFormat(), | 
					
						
							|  |  |  |  | 		m_bWordWrapping ? m_fWordWrappingWidth : 0, | 
					
						
							|  |  |  |  | 		0, | 
					
						
							|  |  |  |  | 		&pDWriteTextLayout | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	ASSERT(SUCCEEDED(hr), "Create IDWriteTextFormat Failed!"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ֵĿ<D6B5><C4BF>Ⱥ߶<CDB8>
 | 
					
						
							|  |  |  |  | 	DWRITE_TEXT_METRICS metrics; | 
					
						
							|  |  |  |  | 	pDWriteTextLayout->GetMetrics(&metrics); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	this->_setSize(metrics.widthIncludingTrailingWhitespace, metrics.height); | 
					
						
							|  |  |  |  | 	m_fWordWrappingWidth = metrics.widthIncludingTrailingWhitespace; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							|  |  |  |  | 	SafeReleaseInterface(&pDWriteTextLayout); | 
					
						
							|  |  |  |  | } |