| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | #include "..\enodes.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Text::Text() | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	: m_bWrappingEnable(false) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	, m_pFont(nullptr) | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	, m_fWrappingWidth(0) | 
					
						
							|  |  |  |  | 	, m_bHasUnderline(false) | 
					
						
							|  |  |  |  | 	, m_bHasStrikethrough(false) | 
					
						
							|  |  |  |  | 	, m_pDWriteTextLayout(nullptr) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	this->setFont(new Font()); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | e2d::Text::Text(String text) | 
					
						
							|  |  |  |  | 	: m_bWrappingEnable(false) | 
					
						
							| 
									
										
										
										
											2017-12-15 21:51:07 +08:00
										 |  |  |  | 	, m_pFont(nullptr) | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	, m_fWrappingWidth(0) | 
					
						
							|  |  |  |  | 	, m_bHasUnderline(false) | 
					
						
							|  |  |  |  | 	, m_bHasStrikethrough(false) | 
					
						
							|  |  |  |  | 	, m_pDWriteTextLayout(nullptr) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	: m_bWrappingEnable(false) | 
					
						
							| 
									
										
										
										
											2017-12-15 21:51:07 +08:00
										 |  |  |  | 	, m_pFont(nullptr) | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	, m_fWrappingWidth(0) | 
					
						
							|  |  |  |  | 	, m_bHasUnderline(false) | 
					
						
							|  |  |  |  | 	, m_bHasStrikethrough(false) | 
					
						
							|  |  |  |  | 	, m_pDWriteTextLayout(nullptr) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	this->setFont(font); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | e2d::Text::Text(String text, Font * font) | 
					
						
							|  |  |  |  | 	: m_bWrappingEnable(false) | 
					
						
							| 
									
										
										
										
											2017-12-15 21:51:07 +08:00
										 |  |  |  | 	, m_pFont(nullptr) | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	, m_fWrappingWidth(0) | 
					
						
							|  |  |  |  | 	, m_bHasUnderline(false) | 
					
						
							|  |  |  |  | 	, m_bHasStrikethrough(false) | 
					
						
							|  |  |  |  | 	, m_pDWriteTextLayout(nullptr) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	this->setText(text); | 
					
						
							|  |  |  |  | 	this->setFont(font); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | e2d::Text::Text(String text, String fontFamily, double fontSize, UINT32 color, UINT32 fontWeight, bool italic) | 
					
						
							|  |  |  |  | 	: m_bWrappingEnable(false) | 
					
						
							| 
									
										
										
										
											2017-12-16 15:49:48 +08:00
										 |  |  |  | 	, m_pFont(nullptr) | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	, m_fWrappingWidth(0) | 
					
						
							|  |  |  |  | 	, m_bHasUnderline(false) | 
					
						
							|  |  |  |  | 	, m_bHasStrikethrough(false) | 
					
						
							|  |  |  |  | 	, m_pDWriteTextLayout(nullptr) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | e2d::Font * e2d::Text::getFont() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	return m_pFont; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | void e2d::Text::setText(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-03-30 01:41:29 +08:00
										 |  |  |  | void e2d::Text::setWrappingWidth(double wordWrapWidth) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	m_fWrappingWidth = max(static_cast<float>(wordWrapWidth), 0); | 
					
						
							|  |  |  |  | 	m_bWrappingEnable = (abs(m_fWrappingWidth) >= 1e-7); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	_initTextLayout(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | void e2d::Text::setLineSpacing(double fLineSpacing) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	if (m_pFont) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (fLineSpacing == 0.0f) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			m_pFont->getDWriteTextFormat()->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			m_pFont->getDWriteTextFormat()->SetLineSpacing( | 
					
						
							|  |  |  |  | 				DWRITE_LINE_SPACING_METHOD_UNIFORM, | 
					
						
							|  |  |  |  | 				static_cast<float>(fLineSpacing), | 
					
						
							|  |  |  |  | 				static_cast<float>(fLineSpacing) * 0.8f | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		 | 
					
						
							|  |  |  |  | 		_initTextLayout(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Text::setAlignment(UINT32 nAlign) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (m_pFont) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_pFont->getDWriteTextFormat()->SetTextAlignment(DWRITE_TEXT_ALIGNMENT(nAlign)); | 
					
						
							|  |  |  |  | 		_initTextLayout(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Text::setUnderline(bool hasUnderline) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (m_bHasUnderline != hasUnderline) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_bHasUnderline = hasUnderline; | 
					
						
							|  |  |  |  | 		_initTextLayout(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Text::setStrikethrough(bool hasStrikethrough) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (m_bHasStrikethrough != hasStrikethrough) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_bHasStrikethrough = hasStrikethrough; | 
					
						
							|  |  |  |  | 		_initTextLayout(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Text::onRender() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	D2D1_RECT_F textLayoutRect = D2D1::RectF(0, 0, m_bWrappingEnable ? m_fWrappingWidth : m_fWidth, m_fHeight); | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD>û<EFBFBD>ˢ<EFBFBD><CBA2>ɫ<EFBFBD><C9AB><EFBFBD><CDB8><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_pFont->m_Color, m_fDisplayOpacity)); | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (m_pDWriteTextLayout) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		Renderer::getRenderTarget()->DrawTextLayout( | 
					
						
							|  |  |  |  | 			{ 0, 0 }, | 
					
						
							|  |  |  |  | 			m_pDWriteTextLayout, | 
					
						
							|  |  |  |  | 			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-03-01 19:28:22 +08:00
										 |  |  |  | 		this->setSize(0, 0); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		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>
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	if (!m_bWrappingEnable) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 		m_pFont->getDWriteTextFormat()->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 		m_pFont->getDWriteTextFormat()->SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// <20><>ȡ TextLayout
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	SafeReleaseInterface(&m_pDWriteTextLayout); | 
					
						
							|  |  |  |  | 	UINT32 length = static_cast<UINT32>(m_sText.getLength()); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 	HRESULT hr = Renderer::getIDWriteFactory()->CreateTextLayout( | 
					
						
							| 
									
										
										
										
											2017-12-11 18:17:24 +08:00
										 |  |  |  | 		m_sText, | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 		length, | 
					
						
							|  |  |  |  | 		m_pFont->getDWriteTextFormat(), | 
					
						
							|  |  |  |  | 		m_bWrappingEnable ? m_fWrappingWidth : 0, | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		0, | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 		&m_pDWriteTextLayout | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	ASSERT(SUCCEEDED(hr), "Create IDWriteTextFormat Failed!"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>»<EFBFBD><C2BB>ߺ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	if (m_bHasUnderline) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_pDWriteTextLayout->SetUnderline(true, { 0, length }); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if (m_bHasStrikethrough) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		m_pDWriteTextLayout->SetStrikethrough(true, { 0, length }); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	// <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ֵĿ<D6B5><C4BF>Ⱥ߶<CDB8>
 | 
					
						
							|  |  |  |  | 	DWRITE_TEXT_METRICS metrics; | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	m_pDWriteTextLayout->GetMetrics(&metrics); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 19:28:22 +08:00
										 |  |  |  | 	this->setSize(metrics.widthIncludingTrailingWhitespace, metrics.height); | 
					
						
							| 
									
										
										
										
											2018-03-30 01:41:29 +08:00
										 |  |  |  | 	m_fWrappingWidth = metrics.widthIncludingTrailingWhitespace; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } |