Is there any way to display text with multiple lines? I tried the typical \n for newline, but it doesn't work.
Thanks in advance.
Thanks in advance.
A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.
typedef std::string<_TCHAR> tstring;
void
drawText(
CGapiSurface& surface,
CGapiBitmapFont& font,
const RECT& kRect,
const tstring& ks)
{
if (ks.empty())
{
return;
}
const int knLineHeight = font.GetHeight();
int nBeginPos = 0;
int nEndPos = ks.find(_T(" "));
tstring sWord = ks.substr(0, nEndPos);
DWORD nWordWidth;
int nX = kRect.left;
int nY = kRect.top;
if (kRect.bottom < nY + knLineHeight)
{
// The first line doesn't fit, so abort.
return;
}
while (true)
{
// Get the width of the word.
font.GetStringWidth(sWord.c_str(), &nWordWidth);
if (nX + nWordWidth <= kRect.right)
{
// The word fits on this line so draw it.
surface.DrawText(
nX,
nY,
sWord.c_str(),
&font,
0,
NULL,
0,
NULL);
// Advance the position.
nX += nWordWidth;
if (nEndPos == tstring::npos)
{
// We have drawn all the words.
break;
}
else
{
// Advance to the next word.
nBeginPos = nEndPos;
nEndPos = ks.find(_T(" "), nEndPos + 1);
sWord = ks.substr(nBeginPos, nEndPos - nBeginPos);
}
}
else
{
// The word doesn't fit on this line.
if (nX == kRect.left)
{
// This is already a new line, so abort.
break;
}
else
{
// Make a new line.
nY += knLineHeight;
if (kRect.bottom < nY + knLineHeight)
{
// The new line doesn't fit, so abort.
break;
}
// Prepare the new line by removing the leading space.
nX = kRect.left;
sWord = sWord.erase(sWord.begin());
}
}
}
}
typedef std::basic_string<_TCHAR> tstring;
bool guiTextbox::drawText( CGapiSurface* pSurface ,
CGapiBitmapFont* pFont ,
long lx ,
long ly ,
TCHAR* pszString,
dword dwFlags,
bool bMultiline )
{
// Hardcoded for this example
long lFontHeight = 7;
long lLineSpacing = 4;
// Get the first line - reading up to the carrage return
TCHAR* pszTok = NULL;
// If the caller is requesting multiline functionality, then do so using strtok to
// separate the lines using the carrage return
if( bMultiline )
{
pszTok = _tcstok( pszString, _T("\n") );
// While there is a valid string....
while( pszTok )
{
// Draw the text unto the surface.
if( pSurface->DrawText( lx, ly, pszTok, pFont, dwFlags, NULL, 0UL, NULL ) != S_OK )
return false;
// Increment to the next line
ly += (lFontHeight + lLineSpacing);
// Use the strtok function to obtain the next line
pszTok = _tcstok( NULL, _T("\n") );
}
}
else
{
// Otherwise, just draw the string normally.
if( pSurface->DrawText( lx, ly, pszString, pFont, dwFlags, NULL, 0UL, NULL ) != S_OK )
return false;
}
return true;
}
Layre5150 wrote:I should also add that the code above is meant for a static window, one that is not dynamicially resizable. If that is your aim, then you would have to check word for word and handle all the re-formatting within the function.
egarayblas wrote:I noticed that both the methods above rely on word length, however my font is not a fixed-width font so sometimes sentences with more "W" and "M" get clipped on the screen. Is there a way we can bind the word lengths with the actual "letter" width?
void Text::Format()
{
// Assume the text has been formatted before,
// thus all line seperators need to be removed.
this->RemoveLineSeperators();
CGapiBitmapFont* pFont = this->GetDrawingFont();
tstring_t::size_type index = 0;
DWORD charSpacing = 0;
DWORD wordWidth = 0;
DWORD lineWidth = 0;
DWORD numOfLines = 0;
DWORD padding = m_padding << 1;
DWORD spaceWidth;
pFont->GetCharWidth(_T(" ")[0], &spaceWidth);
tstring_t strWord;
strWord.reserve(TEXT_WORD_LENGTH);
tstring_t::size_type lineBeginPos = 0, lineEndPos, wordBeginPos = 0, wordEndPos;
lineEndPos = m_text.find(_T("\n")); // First line.
// Go through all the lines in the text.
while (true)
{
// Move to new line.
numOfLines++;
lineWidth = padding;
// First word in line.
wordEndPos = m_text.find(_T(" "), lineBeginPos);
// Go through all the words in the line.
while (true)
{
// Get size of the entire word.
strWord = m_text.substr(wordBeginPos, wordEndPos - wordBeginPos);
pFont->GetStringWidth(strWord.c_str(), &wordWidth);
// Check for word spacing.
if (lineWidth > padding) {
lineWidth += spaceWidth;
}
// Check word wrap.
lineWidth += wordWidth;
if (lineWidth > (DWORD) m_region.cx) {
if (wordBeginPos != 0) {
// Mark line seperation.
m_text[wordBeginPos - 1] = TEXT_LINE_SEPERATOR[0];
}
// Move to new line and add the word to it.
numOfLines++;
lineWidth = padding + wordWidth;
}
if (wordEndPos == tstring_t::npos || wordEndPos > lineEndPos) {
break; // No more words on this line.
}
// Move to next word.
wordBeginPos = wordEndPos + 1;
wordEndPos = m_text.find(_T(" "), wordBeginPos);
}
if (lineEndPos == tstring_t::npos) {
break; // No more lines to be found.
}
// Move to next line.
lineBeginPos = lineEndPos + 1;
lineEndPos = m_text.find(_T("\n"), lineBeginPos);
}
// Properly align text to the text region.
this->AlignText(numOfLines, pFont->GetHeight());
}
void Text::RemoveLineSeperators()
{
tstring_t::size_type pos;
pos = m_text.find(TEXT_LINE_SEPERATOR); // First line seperator.
// Go until no line seperators are found.
while (pos != tstring_t::npos)
{
m_text[pos] = _T(" ")[0]; // Insert space where seperator is at.
// Get next seperator.
pos = m_text.find(TEXT_LINE_SEPERATOR, pos + 1);
}
}
void Text::AlignText(DWORD numOfLines, DWORD fontHeight)
{
switch(m_alignment)
{
case GDDRAWTEXT_LEFT:
{
m_alignPos.x = m_padding;
m_alignPos.y = m_padding;
break;
}
case GDDRAWTEXT_CENTER:
{
m_alignPos.x = m_region.cx >> 1;
DWORD textHeight = numOfLines * (fontHeight + m_lineSpacing) - m_lineSpacing;
if (textHeight > m_region.cy - (m_padding << 1)) {
// Text won't event fit, so don't align y axis.
m_alignPos.y = m_padding;
}
else {
m_alignPos.y = (m_region.cy >> 1) - (textHeight >> 1);
}
break;
}
case GDDRAWTEXT_RIGHT:
{
m_alignPos.x = m_region.cx - m_padding;
m_alignPos.y = m_padding;
break;
}
default: // Should never happen.
break;
}
}
void Text::Draw(CGapiSurface* pBuffer, LONG x, LONG y) const
{
if (m_text.empty()) {
return; // No reason to draw an empty string.
}
CGapiBitmapFont* pFont = this->GetDrawingFont();
DWORD fontHeight = pFont->GetHeight();
// Adjust for padding.
LONG xPos = x + m_alignPos.x;
LONG yPos = y + m_alignPos.y;
// Draw text line by line. It should already be formatted,
// so drawing should be as efficient as possible.
tstring_t strTok(m_text);
TCHAR* pLineTok = NULL;
const TCHAR pTokens[] = { _T("\n")[0], TEXT_LINE_SEPERATOR[0], NULL };
pLineTok = _tcstok(const_cast<TCHAR*>(strTok.c_str()), pTokens); // First line.
while (pLineTok != NULL && (yPos + (LONG) fontHeight) < (y + m_region.cy - (LONG) m_padding))
{
// Draw line.
if (pBuffer->DrawText(xPos, yPos, pLineTok, pFont, m_alignment, NULL, NULL, NULL) != S_OK) {
return;
};
// Move to next line.
pLineTok = _tcstok(NULL, pTokens);
yPos += fontHeight + m_lineSpacing;
}
}