From 00393b7b3ba113a216d3704d47e0ca1cb97d0e6e Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 20 Sep 2019 19:30:21 +0300 Subject: [PATCH] Correct String::countHeight with breakEverywhere. --- ui/text/text.cpp | 19 +++++++++++-------- ui/text/text.h | 8 ++++---- ui/widgets/labels.cpp | 12 +++++++----- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/ui/text/text.cpp b/ui/text/text.cpp index 1deadf8..3d061e8 100644 --- a/ui/text/text.cpp +++ b/ui/text/text.cpp @@ -2890,13 +2890,13 @@ bool String::removeSkipBlock() { return true; } -int String::countWidth(int width) const { +int String::countWidth(int width, bool breakEverywhere) const { if (QFixed(width) >= _maxWidth) { return _maxWidth.ceil().toInt(); } QFixed maxLineWidth = 0; - enumerateLines(width, [&maxLineWidth](QFixed lineWidth, int lineHeight) { + enumerateLines(width, breakEverywhere, [&](QFixed lineWidth, int lineHeight) { if (lineWidth > maxLineWidth) { maxLineWidth = lineWidth; } @@ -2904,25 +2904,28 @@ int String::countWidth(int width) const { return maxLineWidth.ceil().toInt(); } -int String::countHeight(int width) const { +int String::countHeight(int width, bool breakEverywhere) const { if (QFixed(width) >= _maxWidth) { return _minHeight; } int result = 0; - enumerateLines(width, [&result](QFixed lineWidth, int lineHeight) { + enumerateLines(width, breakEverywhere, [&](QFixed lineWidth, int lineHeight) { result += lineHeight; }); return result; } -void String::countLineWidths(int width, QVector *lineWidths) const { - enumerateLines(width, [lineWidths](QFixed lineWidth, int lineHeight) { +void String::countLineWidths(int width, QVector *lineWidths, bool breakEverywhere) const { + enumerateLines(width, breakEverywhere, [&](QFixed lineWidth, int lineHeight) { lineWidths->push_back(lineWidth.ceil().toInt()); }); } template -void String::enumerateLines(int w, Callback callback) const { +void String::enumerateLines( + int w, + bool breakEverywhere, + Callback callback) const { QFixed width = w; if (width < _minResizeWidth) width = _minResizeWidth; @@ -2994,7 +2997,7 @@ void String::enumerateLines(int w, Callback callback) const { continue; } - if (f != j) { + if (f != j && !breakEverywhere) { j = f; widthLeft = f_wLeft; lineHeight = f_lineHeight; diff --git a/ui/text/text.h b/ui/text/text.h index 343b640..dd7614e 100644 --- a/ui/text/text.h +++ b/ui/text/text.h @@ -114,9 +114,9 @@ public: String &operator=(const String &other); String &operator=(String &&other); - int countWidth(int width) const; - int countHeight(int width) const; - void countLineWidths(int width, QVector *lineWidths) const; + int countWidth(int width, bool breakEverywhere = false) const; + int countHeight(int width, bool breakEverywhere = false) const; + void countLineWidths(int width, QVector *lineWidths, bool breakEverywhere = false) const; 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 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: // QFixed lineWidth, int lineHeight template - 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); diff --git a/ui/widgets/labels.cpp b/ui/widgets/labels.cpp index a29544c..2f9c9c0 100644 --- a/ui/widgets/labels.cpp +++ b/ui/widgets/labels.cpp @@ -266,10 +266,10 @@ int FlatLabel::countTextWidth() const { && _tryMakeSimilarLines) { auto large = _allowedWidth; auto small = _allowedWidth / 2; - const auto largeHeight = _text.countHeight(large); + const auto largeHeight = _text.countHeight(large, _breakEverywhere); while (large - small > 1) { const auto middle = (large + small) / 2; - if (largeHeight == _text.countHeight(middle)) { + if (largeHeight == _text.countHeight(middle, _breakEverywhere)) { large = middle; } else { small = middle; @@ -281,8 +281,10 @@ int FlatLabel::countTextWidth() const { } int FlatLabel::countTextHeight(int textWidth) { - _fullTextHeight = _text.countHeight(textWidth); - return _st.maxHeight ? qMin(_fullTextHeight, _st.maxHeight) : _fullTextHeight; + _fullTextHeight = _text.countHeight(textWidth, _breakEverywhere); + return _st.maxHeight + ? qMin(_fullTextHeight, _st.maxHeight) + : _fullTextHeight; } void FlatLabel::refreshSize() { @@ -693,7 +695,7 @@ std::unique_ptr FlatLabel::CrossFade( auto result = Data(); result.full = GrabWidgetToImage(label, QRect(), bg->c); 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; auto addedHeight = (label->_st.style.lineHeight - result.lineHeight); if (addedHeight > 0) {