static bool getComboBoxSize( MyGUI::Widget* pw,MyGUI::IntSize& size ){ MyGUI::ComboBox* p = pw->castType<MyGUI::ComboBox>(false); if( p ){ MyGUI::IntSize s,ss; for( size_t i=0;i<p->getItemCount();++i ){ s = CalcTextWidth(p->getItemNameAt(i),p->getFontName()); ss.width = max(s.width,ss.width); } s = CalcTextWidth(p->getCaption(),p->getFontName()); size.width = max(s.width,ss.width) + 26; size.height = max(s.height,ss.height)+4; return true; } return false; }
bool CalcTextWidth(CHTString sFontPath, int iTextSize, CHTString sText, OUT int& riWidth, OUT CHTString& rsError) { TTF_Font* pFont = GetOrLoadFont(sFontPath, iTextSize, rsError); if (!pFont) return false; riWidth = CalcTextWidth(pFont, sText); return true; }
bool getWidgetSize( MyGUI::Widget* pw,MyGUI::IntSize& size ){ T* p = pw->castType<T>(false); if( p ){ size = CalcTextWidth(p->getCaption(),p->getFontName()); return true; } return false; }
CHTString CalcTextLine(CHTString sEntireText, TTF_Font* pTextFont, int iMaxWidth) { // TODO: Remove spaces when wrapping int iLastWrapPos = -1; for (int iCharPos = 0; iCharPos < sEntireText.Length(); iCharPos++) { // always break on line-feed if (sEntireText[iCharPos] == '\n' || sEntireText[iCharPos] == '\r') { iLastWrapPos = iCharPos; break; } // can wrap at this location? if (CanWrapText(sEntireText, iCharPos)) { // wrap pos is end of line; get current line CHTString sCurAttempt = sEntireText.Left(iCharPos+1); // fits within max width? if (CalcTextWidth(pTextFont, sCurAttempt) < iMaxWidth) iLastWrapPos = iCharPos; else break; } } if (iLastWrapPos == -1) { // no place to wrap; give whole line return sEntireText; } else { // wrap position is end of line return sEntireText.Left(iLastWrapPos+1); } }
void Canvas::DrawFormattedText(PixelRect *rc, const TCHAR *text, unsigned format) { assert(text != nullptr); #ifndef UNICODE assert(ValidateUTF8(text)); #endif if (font == nullptr) return; unsigned skip = font->GetLineSpacing(); unsigned max_lines = (format & DT_CALCRECT) ? -1 : (rc->bottom - rc->top + skip - 1) / skip; size_t len = _tcslen(text); TCHAR *duplicated = new TCHAR[len + 1], *p = duplicated; unsigned lines = 1; for (const TCHAR *i = text; *i != _T('\0'); ++i) { TCHAR ch = *i; if (ch == _T('\n')) { /* explicit line break */ if (++lines >= max_lines) break; ch = _T('\0'); } else if (ch == _T('\r')) /* skip */ continue; else if ((unsigned)ch < 0x20) /* replace non-printable characters */ ch = _T(' '); *p++ = ch; } *p = _T('\0'); len = p - duplicated; // simple wordbreak algorithm. looks for single spaces only, no tabs, // no grouping of multiple spaces if (format & DT_WORDBREAK) { for (size_t i = 0; i < len; i += _tcslen(duplicated + i) + 1) { PixelSize sz = CalcTextSize(duplicated + i); TCHAR *prev_p = nullptr; // remove words from behind till line fits or no more space is found while (sz.cx > rc->right - rc->left && (p = StringFindLast(duplicated + i, _T(' '))) != nullptr) { if (prev_p) *prev_p = _T(' '); *p = _T('\0'); prev_p = p; sz = CalcTextSize(duplicated + i); } if (prev_p) { lines++; if (lines >= max_lines) break; } } } if (format & DT_CALCRECT) { rc->bottom = rc->top + lines * skip; delete[] duplicated; return; } int y = (format & DT_VCENTER) && lines < max_lines ? (rc->top + rc->bottom - lines * skip) / 2 : rc->top; for (size_t i = 0; i < len; i += _tcslen(duplicated + i) + 1) { if (duplicated[i] != _T('\0')) { int x; if (format & (DT_RIGHT | DT_CENTER)) { PixelSize sz = CalcTextSize(duplicated + i); x = (format & DT_CENTER) ? (rc->left + rc->right - sz.cx)/2 : rc->right - sz.cx; // DT_RIGHT } else { // default is DT_LEFT x = rc->left; } TextAutoClipped(x, y, duplicated + i); if (format & DT_UNDERLINE) DrawHLine(x, x + CalcTextWidth(duplicated + i), y + font->GetAscentHeight() + 1, text_color); } y += skip; if (y >= rc->bottom) break; } delete[] duplicated; }