/** * Gets column width. * The meaning of what a column is can be considered a fairly weak * notion for some fonts. This method is used to define the width * of a column. By default this is defined to be the width of the * character <em>m</em> for the font used. This method can be * redefined to be some alternative amount. * * @return the column width >= 1 */ /*protected*/ int JTextArea::getColumnWidth() { if (columnWidth == 0) { QFontMetrics metrics = QFontMetrics(font()); columnWidth = metrics.charWidth(QString("m"),0); } return columnWidth; }
int Font::width(QChar *chs, int, int pos, int len, int start, int end, int toAdd) const { const QConstString cstr(chs + pos, len); int w = 0; const QString qstr = cstr.string(); if(scFont) { const QString upper = qstr.upper(); const QChar *uc = qstr.unicode(); const QFontMetrics sc_fm(*scFont); for(int i = 0; i < len; ++i) { if((uc + i)->category() == QChar::Letter_Lowercase) w += sc_fm.charWidth(upper, i); else w += fm.charWidth(qstr, i); } } else { // ### might be a little inaccurate w = fm.width(qstr); } if(letterSpacing) w += len * letterSpacing; if(wordSpacing) // add amount for(int i = 0; i < len; ++i) { if(chs[i + pos].category() == QChar::Separator_Space) w += wordSpacing; } if(toAdd) { // first gather count of spaces int numSpaces = 0; for(int i = start; i != end; ++i) if(chs[i].category() == QChar::Separator_Space) ++numSpaces; // distribute pixels evenly among spaces, but count only those within // [pos, pos+len) for(int i = start; numSpaces && i != pos + len; i++) if(chs[i].category() == QChar::Separator_Space) { const int a = toAdd / numSpaces; if(i >= pos) w += a; toAdd -= a; --numSpaces; } } return w; }
QString KSqueezeLabel::squeezer(const QString &s, const QFontMetrics& fm, uint width) { if (s.isEmpty() || uint(fm.width(s)) <= width) { return s; } const unsigned int length = s.length(); if (length == 2) return s; const int maxWidth = width - fm.width('.') * 3; if (maxWidth <= 0) return "..."; unsigned int leftIdx = 0, rightIdx = length; unsigned int leftWidth = fm.charWidth(s, leftIdx++); unsigned int rightWidth = fm.charWidth(s, --rightIdx); while (leftWidth + rightWidth < uint(maxWidth)) { while (leftWidth <= rightWidth && leftWidth + rightWidth < uint( maxWidth)) { leftWidth += fm.charWidth(s, leftIdx++); } while (rightWidth <= leftWidth && leftWidth + rightWidth < uint( maxWidth)) { rightWidth += fm.charWidth(s, --rightIdx); } } if (leftWidth > rightWidth) { --leftIdx; } else { ++rightIdx; } rightIdx = length - rightIdx; if ((leftIdx == 0 && rightIdx == 1) || (leftIdx == 1 && rightIdx == 0)) return "..."; return s.left(leftIdx) + "..." + s.right(rightIdx); }