Correct String::countHeight with breakEverywhere.

This commit is contained in:
John Preston 2019-09-20 19:30:21 +03:00
parent 6fb693f606
commit 00393b7b3b
3 changed files with 22 additions and 17 deletions

View file

@ -2890,13 +2890,13 @@ bool String::removeSkipBlock() {
return true; return true;
} }
int String::countWidth(int width) const { int String::countWidth(int width, bool breakEverywhere) const {
if (QFixed(width) >= _maxWidth) { if (QFixed(width) >= _maxWidth) {
return _maxWidth.ceil().toInt(); return _maxWidth.ceil().toInt();
} }
QFixed maxLineWidth = 0; QFixed maxLineWidth = 0;
enumerateLines(width, [&maxLineWidth](QFixed lineWidth, int lineHeight) { enumerateLines(width, breakEverywhere, [&](QFixed lineWidth, int lineHeight) {
if (lineWidth > maxLineWidth) { if (lineWidth > maxLineWidth) {
maxLineWidth = lineWidth; maxLineWidth = lineWidth;
} }
@ -2904,25 +2904,28 @@ int String::countWidth(int width) const {
return maxLineWidth.ceil().toInt(); return maxLineWidth.ceil().toInt();
} }
int String::countHeight(int width) const { int String::countHeight(int width, bool breakEverywhere) const {
if (QFixed(width) >= _maxWidth) { if (QFixed(width) >= _maxWidth) {
return _minHeight; return _minHeight;
} }
int result = 0; int result = 0;
enumerateLines(width, [&result](QFixed lineWidth, int lineHeight) { enumerateLines(width, breakEverywhere, [&](QFixed lineWidth, int lineHeight) {
result += lineHeight; result += lineHeight;
}); });
return result; return result;
} }
void String::countLineWidths(int width, QVector<int> *lineWidths) const { void String::countLineWidths(int width, QVector<int> *lineWidths, bool breakEverywhere) const {
enumerateLines(width, [lineWidths](QFixed lineWidth, int lineHeight) { enumerateLines(width, breakEverywhere, [&](QFixed lineWidth, int lineHeight) {
lineWidths->push_back(lineWidth.ceil().toInt()); lineWidths->push_back(lineWidth.ceil().toInt());
}); });
} }
template <typename Callback> template <typename Callback>
void String::enumerateLines(int w, Callback callback) const { void String::enumerateLines(
int w,
bool breakEverywhere,
Callback callback) const {
QFixed width = w; QFixed width = w;
if (width < _minResizeWidth) width = _minResizeWidth; if (width < _minResizeWidth) width = _minResizeWidth;
@ -2994,7 +2997,7 @@ void String::enumerateLines(int w, Callback callback) const {
continue; continue;
} }
if (f != j) { if (f != j && !breakEverywhere) {
j = f; j = f;
widthLeft = f_wLeft; widthLeft = f_wLeft;
lineHeight = f_lineHeight; lineHeight = f_lineHeight;

View file

@ -114,9 +114,9 @@ public:
String &operator=(const String &other); String &operator=(const String &other);
String &operator=(String &&other); String &operator=(String &&other);
int countWidth(int width) const; int countWidth(int width, bool breakEverywhere = false) const;
int countHeight(int width) const; int countHeight(int width, bool breakEverywhere = false) const;
void countLineWidths(int width, QVector<int> *lineWidths) const; void countLineWidths(int width, QVector<int> *lineWidths, bool breakEverywhere = false) const;
void setText(const style::TextStyle &st, const QString &text, const TextParseOptions &options = _defaultOptions); void setText(const style::TextStyle &st, const QString &text, const TextParseOptions &options = _defaultOptions);
void setRichText(const style::TextStyle &st, const QString &text, TextParseOptions options = _defaultOptions); void setRichText(const style::TextStyle &st, const QString &text, TextParseOptions options = _defaultOptions);
void setMarkedText(const style::TextStyle &st, const TextWithEntities &textWithEntities, const TextParseOptions &options = _defaultOptions); void setMarkedText(const style::TextStyle &st, const TextWithEntities &textWithEntities, const TextParseOptions &options = _defaultOptions);
@ -208,7 +208,7 @@ private:
// callback(lineWidth, lineHeight) will be called for all lines with: // callback(lineWidth, lineHeight) will be called for all lines with:
// QFixed lineWidth, int lineHeight // QFixed lineWidth, int lineHeight
template <typename Callback> template <typename Callback>
void enumerateLines(int w, Callback callback) const; void enumerateLines(int w, bool breakEverywhere, Callback callback) const;
void recountNaturalSize(bool initial, Qt::LayoutDirection optionsDir = Qt::LayoutDirectionAuto); void recountNaturalSize(bool initial, Qt::LayoutDirection optionsDir = Qt::LayoutDirectionAuto);

View file

@ -266,10 +266,10 @@ int FlatLabel::countTextWidth() const {
&& _tryMakeSimilarLines) { && _tryMakeSimilarLines) {
auto large = _allowedWidth; auto large = _allowedWidth;
auto small = _allowedWidth / 2; auto small = _allowedWidth / 2;
const auto largeHeight = _text.countHeight(large); const auto largeHeight = _text.countHeight(large, _breakEverywhere);
while (large - small > 1) { while (large - small > 1) {
const auto middle = (large + small) / 2; const auto middle = (large + small) / 2;
if (largeHeight == _text.countHeight(middle)) { if (largeHeight == _text.countHeight(middle, _breakEverywhere)) {
large = middle; large = middle;
} else { } else {
small = middle; small = middle;
@ -281,8 +281,10 @@ int FlatLabel::countTextWidth() const {
} }
int FlatLabel::countTextHeight(int textWidth) { int FlatLabel::countTextHeight(int textWidth) {
_fullTextHeight = _text.countHeight(textWidth); _fullTextHeight = _text.countHeight(textWidth, _breakEverywhere);
return _st.maxHeight ? qMin(_fullTextHeight, _st.maxHeight) : _fullTextHeight; return _st.maxHeight
? qMin(_fullTextHeight, _st.maxHeight)
: _fullTextHeight;
} }
void FlatLabel::refreshSize() { void FlatLabel::refreshSize() {
@ -693,7 +695,7 @@ std::unique_ptr<CrossFadeAnimation> FlatLabel::CrossFade(
auto result = Data(); auto result = Data();
result.full = GrabWidgetToImage(label, QRect(), bg->c); result.full = GrabWidgetToImage(label, QRect(), bg->c);
auto textWidth = label->width() - label->_st.margin.left() - label->_st.margin.right(); auto textWidth = label->width() - label->_st.margin.left() - label->_st.margin.right();
label->_text.countLineWidths(textWidth, &result.lineWidths); label->_text.countLineWidths(textWidth, &result.lineWidths, label->_breakEverywhere);
result.lineHeight = label->_st.style.font->height; result.lineHeight = label->_st.style.font->height;
auto addedHeight = (label->_st.style.lineHeight - result.lineHeight); auto addedHeight = (label->_st.style.lineHeight - result.lineHeight);
if (addedHeight > 0) { if (addedHeight > 0) {