162 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			162 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C++
		
	
	
	
| #include "..\enodes.h"
 | ||
| 
 | ||
| e2d::Text::Text()
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setFont(new Font());
 | ||
| }
 | ||
| 
 | ||
| e2d::Text::Text(const String & text)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setText(text);
 | ||
| 	this->setFont(new Font());
 | ||
| }
 | ||
| 
 | ||
| e2d::Text::Text(Font * font)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setFont(font);
 | ||
| }
 | ||
| 
 | ||
| e2d::Text::Text(const String & text, Font * font)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setText(text);
 | ||
| 	this->setFont(font);
 | ||
| }
 | ||
| 
 | ||
| e2d::Text::Text(const String & text, String fontFamily, double fontSize, UINT32 color, UINT32 fontWeight, bool italic)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setText(text);
 | ||
| 	this->setFont(new Font(fontFamily, fontSize, color, fontWeight, italic));
 | ||
| }
 | ||
| 
 | ||
| e2d::Text::~Text()
 | ||
| {
 | ||
| 	SafeRelease(&m_pFont);
 | ||
| }
 | ||
| 
 | ||
| e2d::String e2d::Text::getText() const
 | ||
| {
 | ||
| 	return m_sText;
 | ||
| }
 | ||
| 
 | ||
| double e2d::Text::getWidth() const
 | ||
| {
 | ||
| 	return m_fWordWrappingWidth * m_fScaleX;
 | ||
| }
 | ||
| 
 | ||
| double e2d::Text::getRealWidth() const
 | ||
| {
 | ||
| 	return m_fWordWrappingWidth;
 | ||
| }
 | ||
| 
 | ||
| e2d::Font * e2d::Text::getFont() const
 | ||
| {
 | ||
| 	return m_pFont;
 | ||
| }
 | ||
| 
 | ||
| void e2d::Text::setText(const String & text)
 | ||
| {
 | ||
| 	m_sText = text;
 | ||
| 	_initTextLayout();
 | ||
| }
 | ||
| 
 | ||
| void e2d::Text::setFont(Font * font)
 | ||
| {
 | ||
| 	if (font)
 | ||
| 	{
 | ||
| 		SafeRelease(&m_pFont);
 | ||
| 		m_pFont = font;
 | ||
| 		font->retain();
 | ||
| 
 | ||
| 		_initTextLayout();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::Text::setWordWrappingEnable(bool value)
 | ||
| {
 | ||
| 	m_bWordWrapping = value;
 | ||
| 	_initTextLayout();
 | ||
| }
 | ||
| 
 | ||
| void e2d::Text::setWordWrappingWidth(double wordWrapWidth)
 | ||
| {
 | ||
| 	m_fWordWrappingWidth = max(static_cast<float>(wordWrapWidth), 0);
 | ||
| 	_initTextLayout();
 | ||
| }
 | ||
| 
 | ||
| void e2d::Text::onRender()
 | ||
| {
 | ||
| 	Renderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_pFont->m_Color, m_fDisplayOpacity));
 | ||
| 	Renderer::getRenderTarget()->DrawTextW(
 | ||
| 		m_sText,
 | ||
| 		static_cast<UINT32>(m_sText.getLength()),
 | ||
| 		m_pFont->_getTextFormat(),
 | ||
| 		D2D1::RectF(
 | ||
| 			0,
 | ||
| 			0,
 | ||
| 			m_bWordWrapping ? m_fWordWrappingWidth : m_fWidth,
 | ||
| 			m_fHeight
 | ||
| 		),
 | ||
| 		Renderer::getSolidColorBrush()
 | ||
| 	);
 | ||
| }
 | ||
| 
 | ||
| void e2d::Text::_initTextLayout()
 | ||
| {
 | ||
| 	// δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ϊ 0
 | ||
| 	if (!m_pFont || m_sText.isEmpty())
 | ||
| 	{
 | ||
| 		this->setSize(0, 0);
 | ||
| 		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;
 | ||
| 
 | ||
| 	HRESULT hr = Renderer::getIDWriteFactory()->CreateTextLayout(
 | ||
| 		m_sText,
 | ||
| 		static_cast<UINT32>(m_sText.getLength()),
 | ||
| 		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);
 | ||
| }
 |