Ejemplo n.º 1
0
//------------------------------------------------------------------------
void CTextLabel::calculateTruncatedText ()
{
	truncatedText = "";
	if (textRotation != 0.) // currently truncation is only supported when not rotated
		return;
	std::string _truncatedText;
	if (!(textTruncateMode == kTruncateNone || text.getByteCount () == 0 || fontID == 0 || fontID->getPlatformFont () == 0 || fontID->getPlatformFont ()->getPainter () == 0))
	{
		IFontPainter* painter = fontID->getPlatformFont ()->getPainter ();
		CCoord width = painter->getStringWidth (0, text.getPlatformString (), true);
		width += textInset.x * 2;
		if (width > getWidth ())
		{
			if (textTruncateMode == kTruncateTail)
			{
				_truncatedText = text;
				_truncatedText += "..";
				while (width > getWidth () && _truncatedText.size () > 2)
				{
					UTF8CharacterIterator it (_truncatedText);
					it.end ();
					for (int32_t i = 0; i < 3; i++, --it)
					{
						if (it == it.front ())
						{
							break;
						}
					}
					_truncatedText.erase (_truncatedText.size () - (2 + it.getByteLength ()), it.getByteLength ());
					width = painter->getStringWidth (0, CString (_truncatedText.c_str ()).getPlatformString (), true);
					width += textInset.x * 2;
				}
			}
			else if (textTruncateMode == kTruncateHead)
			{
				_truncatedText = "..";
				_truncatedText += text;
				while (width > getWidth () && _truncatedText.size () > 2)
				{
					UTF8CharacterIterator it (_truncatedText);
					for (int32_t i = 0; i < 2; i++, ++it)
					{
						if (it == it.back ())
						{
							break;
						}
					}
					_truncatedText.erase (2, it.getByteLength ());
					width = painter->getStringWidth (0, CString (_truncatedText.c_str ()).getPlatformString (), true);
					width += textInset.x * 2;
				}
			}
		}
	}
	if (truncatedText != _truncatedText)
	{
		truncatedText = _truncatedText.c_str ();
		changed (kMsgTruncatedTextChanged);
	}
}
Ejemplo n.º 2
0
//------------------------------------------------------------------------
void CTextLabel::calculateTruncatedText ()
{
	std::string tmp (truncatedText);
	truncatedText.clear ();
	if (!(textTruncateMode == kTruncateNone || text == 0 || text[0] == 0 || fontID == 0 || fontID->getPlatformFont () == 0 || fontID->getPlatformFont ()->getPainter () == 0))
	{
		IFontPainter* painter = fontID->getPlatformFont ()->getPainter ();
		CCoord width = painter->getStringWidth (0, text, true);
		width += textInset.x * 2;
		if (width > getWidth ())
		{
			if (textTruncateMode == kTruncateTail)
			{
				truncatedText = text;
				truncatedText += "..";
				while (width > getWidth () && truncatedText.size () > 2)
				{
					UTF8CharacterIterator it (truncatedText);
					it.end ();
					for (int32_t i = 0; i < 3; i++, --it)
					{
						if (it == it.front ())
						{
							break;
						}
					}
					truncatedText.erase (truncatedText.size () - (2 + it.getByteLength ()), it.getByteLength ());
					width = painter->getStringWidth (0, truncatedText.c_str (), true);
					width += textInset.x * 2;
				}
			}
			else if (textTruncateMode == kTruncateHead)
			{
				truncatedText = "..";
				truncatedText += text;
				while (width > getWidth () && truncatedText.size () > 2)
				{
					UTF8CharacterIterator it (truncatedText);
					for (int32_t i = 0; i < 2; i++, ++it)
					{
						if (it == it.back ())
						{
							break;
						}
					}
					truncatedText.erase (2, it.getByteLength ());
					width = painter->getStringWidth (0, truncatedText.c_str (), true);
					width += textInset.x * 2;
				}
			}
		}
	}
	if (tmp != truncatedText)
		changed (kMsgTruncatedTextChanged);
}