float ofxFTGLFont::stringWidth(const string& c) { if (c.compare(" ") == 0) { // FTGL won't measure a space width properly, so we // have to use this hack to get that value. return (stringWidth("A A") - stringWidth("AA")); } else { ofRectangle rect = getStringBoundingBox(c, 0,0); return rect.width; } }
void Screen::print(const Common::Point &pt, uint color, const char *formatStr, ...) { // Create the string to display va_list args; va_start(args, formatStr); Common::String str = Common::String::vformat(formatStr, args); va_end(args); // Figure out area to draw text in Common::Point pos = pt; int width_ = stringWidth(str); pos.y--; // Font is always drawing one line higher if (!pos.x) // Center text horizontally pos.x = (this->width() - width_) / 2; Common::Rect textBounds(pos.x, pos.y, pos.x + width_, pos.y + _fontHeight); if (textBounds.right > this->width()) textBounds.moveTo(this->width() - width_, textBounds.top); if (textBounds.bottom > this->height()) textBounds.moveTo(textBounds.left, this->height() - _fontHeight); // Write out the string at the given position writeString(str, Common::Point(textBounds.left, textBounds.top), color); // Copy the affected area to the screen slamRect(textBounds); }
void NameDisplay::draw() { float displayWidth = stringWidth(name)/2; int finalx = pos.x - displayWidth; if (finalx < 0) { pos.x -= (finalx - (5 * drawScale)); } else if ((pos.x + displayWidth) > ofGetWidth()) { pos.x -= displayWidth/2; } ofPushMatrix(); ofTranslate(pos.x, pos.y); ofScale(currentSize, currentSize, 1); ofFill(); ofSetHexColor(0xff000000); ofTrueTypeFont::drawStringAsShapes(name,0 - displayWidth+ (2 * drawScale), -8 * drawScale); ofSetHexColor(0xff8cfff0); ofTrueTypeFont::drawStringAsShapes(name, 0 - displayWidth, (-10 * drawScale)); ofPopMatrix(); }
void S_KR_Font::drawString_bold(CGraphics* g,char* argStr, int argX, int argY, int argAnchor, bool argIsNewLine) { //argStr = utf8ToUnicode(argStr); int strWidth = stringWidth(argStr); if ((argAnchor & Graphics::HCENTER) == Graphics::HCENTER) { argX -= (strWidth >> 1); }
void QPaintContext::drawString(int x, int y, const char *str, int len) { QString qStr = QString::fromUtf8(str, len); for (unsigned int i=0; i<qStr.length(); i++) { if (qStr[i] == QChar(0x14,0x20) || qStr[i] == QChar(0x13,0x20)) qStr[i] = QChar('-', 0); } // myPainter->drawText(x + leftMargin(), y + topMargin(),0,-1,-1, qStr); myPainter->drawText(x + leftMargin(), y + topMargin(),stringWidth(str,len),stringHeight()+3,0, qStr); // myPainter->drawText(x + leftMargin(), y + topMargin(), qStr); }
//-------------------------------------------------------------- void ofxNumberAnimation::startAnimation(int num) { // Change number to string string numStr = ofToString(num); if (!_isNDigitsSet) { _nDigits = numStr.size(); } else if (_nDigits < numStr.size()) { cout << "more digits than nDigits." << endl; return; } // insert zeros int nZeros = _nDigits - numStr.size(); for (int i=0; i<nZeros; i++) { numStr.insert(numStr.begin(), '0'); } _numModulo3 = _nDigits % 3; numStrVec.clear(); if (_thousandsSeparator) { for (int i=0; i<_nDigits; i++) { if (i%3 == _numModulo3 && i!= 0) { numStrVec.push_back(separator); } numStrVec.push_back(ofToString(numStr[i])); } } else { for (int i=0; i<_nDigits; i++) { numStrVec.push_back(ofToString(numStr[i])); } } //----------- // Calculate a position of each number int x = 0; xOffsets.clear(); for (int i=0; i<numStrVec.size(); i++) { xOffsets.push_back(x); x += stringWidth(numStrVec[i]) + _fontSize * 0.2 * _spacing; } //----------- // show random numbers for waitTime ms, then show the target numbers // for waitTimePerDigit ms for each number. waitTimeMs = _animationTimeMs * 0.5; waitTimePerDigitMs = (_animationTimeMs - waitTimeMs) / _nDigits; startTimeMs = ofGetElapsedTimeMillis(); status = animating; }
//----------------------------------------------------------------------- // r e n d e r //----------------------------------------------------------------------- void TGButton::render() { if(isRenderCached()) return; int x1, y1, x2, y2; TGFrameStyle fs = FS_FLAT; getBounds(x1, y1, x2, y2); if (getMouseOverControl()) fs = m_pushed?FS_LOWERED:FS_RAISED; else fs = FS_FLAT; drawFrame(x1, y1, x2, y2, fs); if (focused()) { drawOwnFocus(); } TGSBrush brush; if (m_pushed) brush = m_theme.getTextInvertedBrush(); else { if(getMouseOverControl()) brush = m_theme.getTextFocusedBrush(); else brush = m_theme.getTextBrush(); } x1 = (x2 - x1 + 1)/2 + x1; x1 -= stringWidth(m_caption)/2; x2 = 0; openClip(); drawString(x1 + x2, (y2-y1 + 1)/2 + y1 - stringHeight()/2, m_caption, brush); closeClip(); }
bool Line::addText(string* xiText) { Logger::Log("Entering Line.addText (string)...", LOG_DEBUG); if (stringWidth(xiText) > mWidth) { Logger::Log("String too long for line.", LOG_DEBUG); // calculating number of characters to include float lCurrentWidth = stringWidth(&mText); string lHyphenation("-"); string lLeftText; string lRightText; int lLeftChars; for (lLeftChars = 0; lCurrentWidth < mWidth; lLeftChars++) { // calculate width of current n characters + string already present + a hyphen lLeftText = xiText->substr(0, lLeftChars); lCurrentWidth = stringWidth(&mText) + stringWidth(&lLeftText) + stringWidth(&lHyphenation); } lLeftChars-=2; // add the portion of text that fits lLeftText = xiText->substr(0, lLeftChars); lLeftText.append(lHyphenation); // attempt to add chopped off string to line if (addText(&lLeftText)) { Logger::Log("Wrapping remainder of long string onto next line.", LOG_DEBUG); // alter string to remainder, and return failiure state lRightText = xiText->substr(lLeftChars, xiText->length() - lLeftChars); *xiText = lRightText; // trim chopped part of line if it begins with a space if ((*xiText)[0] == ' ') { Logger::Log("Trimming first character of string (space).", LOG_DEBUG); string lTrimText = xiText->substr(1, xiText->length()-1); xiText->swap(lTrimText); } // and force the second part onto the next line return false; } else { // induce addition to next line return false; } } // if adding the string will make the line too wide, return failiure state if ((stringWidth(&mText) + stringWidth(xiText)) > mWidth) { Logger::Log("Wrapping string onto next line.", LOG_DEBUG); // trim chopped part of line if it begins with a space if ((*xiText)[0] == ' ') { Logger::Log("Trimming first char of string (space).", LOG_DEBUG); string lTrimText = xiText->substr(1, xiText->length()-1); xiText->swap(lTrimText); } // return failiure state: force it on to the next line return false; } // success case: the string will fit, so concatenate the text // fix the previous characters // mFixedChars = (int)mText.length(); // add the new characters mText = mText + *xiText; // reset animation only if complete if (mVisibility == 1.0) mVisibility = 0.0; return true; }
float StringTruncator::width(const String& string, const Font& font, bool disableRoundingHacks) { return stringWidth(font, string.characters(), string.length(), disableRoundingHacks); }
static String truncateString(const String& string, float maxWidth, const Font& font, TruncationFunction truncateToBuffer, bool disableRoundingHacks) { if (string.isEmpty()) return string; ASSERT(maxWidth >= 0); float currentEllipsisWidth = stringWidth(font, &horizontalEllipsis, 1, disableRoundingHacks); UChar stringBuffer[STRING_BUFFER_SIZE]; unsigned truncatedLength; unsigned keepCount; unsigned length = string.length(); if (length > STRING_BUFFER_SIZE) { keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis truncatedLength = centerTruncateToBuffer(string, length, keepCount, stringBuffer); } else { keepCount = length; memcpy(stringBuffer, string.characters(), sizeof(UChar) * length); truncatedLength = length; } float width = stringWidth(font, stringBuffer, truncatedLength, disableRoundingHacks); if (width <= maxWidth) return string; unsigned keepCountForLargestKnownToFit = 0; float widthForLargestKnownToFit = currentEllipsisWidth; unsigned keepCountForSmallestKnownToNotFit = keepCount; float widthForSmallestKnownToNotFit = width; if (currentEllipsisWidth >= maxWidth) { keepCountForLargestKnownToFit = 1; keepCountForSmallestKnownToNotFit = 2; } while (keepCountForLargestKnownToFit + 1 < keepCountForSmallestKnownToNotFit) { ASSERT(widthForLargestKnownToFit <= maxWidth); ASSERT(widthForSmallestKnownToNotFit > maxWidth); float ratio = (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit) / (widthForSmallestKnownToNotFit - widthForLargestKnownToFit); keepCount = static_cast<unsigned>(maxWidth * ratio); if (keepCount <= keepCountForLargestKnownToFit) { keepCount = keepCountForLargestKnownToFit + 1; } else if (keepCount >= keepCountForSmallestKnownToNotFit) { keepCount = keepCountForSmallestKnownToNotFit - 1; } ASSERT(keepCount < length); ASSERT(keepCount > 0); ASSERT(keepCount < keepCountForSmallestKnownToNotFit); ASSERT(keepCount > keepCountForLargestKnownToFit); truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer); width = stringWidth(font, stringBuffer, truncatedLength, disableRoundingHacks); if (width <= maxWidth) { keepCountForLargestKnownToFit = keepCount; widthForLargestKnownToFit = width; } else { keepCountForSmallestKnownToNotFit = keepCount; widthForSmallestKnownToNotFit = width; } } if (keepCountForLargestKnownToFit == 0) { keepCountForLargestKnownToFit = 1; } if (keepCount != keepCountForLargestKnownToFit) { keepCount = keepCountForLargestKnownToFit; truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer); } return String(stringBuffer, truncatedLength); }
float StringTruncator::width(const String& string, const FontCascade& font, EnableRoundingHacksOrNot enableRoundingHacks) { return stringWidth(font, StringView(string).upconvertedCharacters(), string.length(), !enableRoundingHacks); }
float StringTruncator::width(const String& string, const Font& font, EnableRoundingHacksOrNot enableRoundingHacks) { return stringWidth(font, string.characters(), string.length(), !enableRoundingHacks); }
float ofxFTGLFont::getSpaceSize(){ return stringWidth(" "); }
static String truncateString(const String& string, float maxWidth, const FontCascade& font, TruncationFunction truncateToBuffer, bool disableRoundingHacks, float* resultWidth = nullptr, bool shouldInsertEllipsis = true, float customTruncationElementWidth = 0, bool alwaysTruncate = false) { if (string.isEmpty()) return string; if (resultWidth) *resultWidth = 0; ASSERT(maxWidth >= 0); float currentEllipsisWidth = shouldInsertEllipsis ? stringWidth(font, &horizontalEllipsis, 1, disableRoundingHacks) : customTruncationElementWidth; UChar stringBuffer[STRING_BUFFER_SIZE]; unsigned truncatedLength; unsigned keepCount; unsigned length = string.length(); if (length > STRING_BUFFER_SIZE) { if (shouldInsertEllipsis) keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis else keepCount = 0; truncatedLength = centerTruncateToBuffer(string, length, keepCount, stringBuffer, shouldInsertEllipsis); } else { keepCount = length; StringView(string).getCharactersWithUpconvert(stringBuffer); truncatedLength = length; } float width = stringWidth(font, stringBuffer, truncatedLength, disableRoundingHacks); if (!shouldInsertEllipsis && alwaysTruncate) width += customTruncationElementWidth; if ((width - maxWidth) < 0.0001) { // Ignore rounding errors. if (resultWidth) *resultWidth = width; return string; } unsigned keepCountForLargestKnownToFit = 0; float widthForLargestKnownToFit = currentEllipsisWidth; unsigned keepCountForSmallestKnownToNotFit = keepCount; float widthForSmallestKnownToNotFit = width; if (currentEllipsisWidth >= maxWidth) { keepCountForLargestKnownToFit = 1; keepCountForSmallestKnownToNotFit = 2; } while (keepCountForLargestKnownToFit + 1 < keepCountForSmallestKnownToNotFit) { ASSERT_WITH_SECURITY_IMPLICATION(widthForLargestKnownToFit <= maxWidth); ASSERT_WITH_SECURITY_IMPLICATION(widthForSmallestKnownToNotFit > maxWidth); float ratio = (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit) / (widthForSmallestKnownToNotFit - widthForLargestKnownToFit); keepCount = static_cast<unsigned>(maxWidth * ratio); if (keepCount <= keepCountForLargestKnownToFit) keepCount = keepCountForLargestKnownToFit + 1; else if (keepCount >= keepCountForSmallestKnownToNotFit) keepCount = keepCountForSmallestKnownToNotFit - 1; ASSERT_WITH_SECURITY_IMPLICATION(keepCount < length); ASSERT(keepCount > 0); ASSERT_WITH_SECURITY_IMPLICATION(keepCount < keepCountForSmallestKnownToNotFit); ASSERT_WITH_SECURITY_IMPLICATION(keepCount > keepCountForLargestKnownToFit); truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer, shouldInsertEllipsis); width = stringWidth(font, stringBuffer, truncatedLength, disableRoundingHacks); if (!shouldInsertEllipsis) width += customTruncationElementWidth; if (width <= maxWidth) { keepCountForLargestKnownToFit = keepCount; widthForLargestKnownToFit = width; if (resultWidth) *resultWidth = width; } else { keepCountForSmallestKnownToNotFit = keepCount; widthForSmallestKnownToNotFit = width; } } if (keepCountForLargestKnownToFit == 0) { keepCountForLargestKnownToFit = 1; } if (keepCount != keepCountForLargestKnownToFit) { keepCount = keepCountForLargestKnownToFit; truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer, shouldInsertEllipsis); } return String(stringBuffer, truncatedLength); }
float Line::stringWidth() { // Logger::Log("Calculating Line.stringWidth (member string)...", LOG_DEBUG); return stringWidth(&mText); }
float ofxFontStash::getSpaceSize(){ return stringWidth(" "); }
//----------------------------------------------------------------------- // s e t T e x t //----------------------------------------------------------------------- void TGListBoxItem::setText(TGString newText) { text = newText; minimumWidth = stringWidth(newText) + 16; }
int GtkPaintContext::spaceWidth() const { if (mySpaceWidth == -1) { mySpaceWidth = stringWidth(" ", 1); } return mySpaceWidth; }
float StringTruncator::width(const String& string, const Font& font) { return stringWidth(font, string); }