0003-QTextLayout-fix-maximumWidth-for-a-text-containing-s.patch 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. From ffa54deca4ee1e47c112b33ff37f296c4df6c559 Mon Sep 17 00:00:00 2001
  2. From: Vladimir Belyavsky <belyavskyv@gmail.com>
  3. Date: Mon, 26 Sep 2022 19:32:50 +0300
  4. Subject: [PATCH] QTextLayout: fix maximumWidth() for a text containing spaces
  5. When laying out a text and calculating maxWidth, we must _always_ take
  6. into account the accumulated width of spaces (lbh.spaceData.textWidth)
  7. regardless of wrapMode, other text content, spaces position, etc.
  8. Fixes: QTBUG-106947
  9. Change-Id: I2ac9af92ed7dd07c1e040bfcf83949a358d1c9c9
  10. Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
  11. Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
  12. Upstream: https://github.com/qt/qtbase/commit/4945fd93f13d2fc34adf260fd0e0325d0794f3f7
  13. [Thomas: Needed to backport fix for
  14. https://security-tracker.debian.org/tracker/CVE-2023-32763]
  15. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  16. ---
  17. src/gui/text/qtextlayout.cpp | 6 +-----
  18. .../gui/text/qtextlayout/tst_qtextlayout.cpp | 16 ++++++++++++++--
  19. 2 files changed, 15 insertions(+), 7 deletions(-)
  20. diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp
  21. index e3c69db7e57..9ae6bee2de3 100644
  22. --- a/src/gui/text/qtextlayout.cpp
  23. +++ b/src/gui/text/qtextlayout.cpp
  24. @@ -1919,7 +1919,6 @@ void QTextLine::layout_helper(int maxGlyphs)
  25. }
  26. if (!lbh.manualWrap && lbh.spaceData.textWidth > line.width) {
  27. - lbh.spaceData.textWidth = line.width; // ignore spaces that fall out of the line.
  28. goto found;
  29. }
  30. } else {
  31. @@ -2105,12 +2104,9 @@ found:
  32. eng->maxWidth = qMax(eng->maxWidth, line.textWidth);
  33. } else {
  34. eng->minWidth = qMax(eng->minWidth, lbh.minw);
  35. - eng->maxWidth += line.textWidth;
  36. + eng->maxWidth += line.textWidth + lbh.spaceData.textWidth;
  37. }
  38. - if (line.textWidth > 0 && item < eng->layoutData->items.size())
  39. - eng->maxWidth += lbh.spaceData.textWidth;
  40. -
  41. line.textWidth += trailingSpace;
  42. if (lbh.spaceData.length) {
  43. line.trailingSpaces = lbh.spaceData.length;
  44. diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
  45. index a8b42b88697..680c62e9825 100644
  46. --- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
  47. +++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
  48. @@ -124,6 +124,7 @@ private slots:
  49. void tooManyDirectionalCharctersCrash_qtbug77819();
  50. void softHyphens_data();
  51. void softHyphens();
  52. + void min_maximumWidth_data();
  53. void min_maximumWidth();
  54. private:
  55. @@ -2662,10 +2663,21 @@ void tst_QTextLayout::softHyphens()
  56. }
  57. }
  58. +void tst_QTextLayout::min_maximumWidth_data()
  59. +{
  60. + QTest::addColumn<QString>("text");
  61. +
  62. + QTest::newRow("long string") << QStringLiteral("lmong_long_crazy_87235982735_23857239682376923876923876-fuwhfhfw-names-AAAA-deeaois2019-03-03.and.more");
  63. + QTest::newRow("QTBUG-106947") << QStringLiteral("text text");
  64. + QTest::newRow("spaces") << QStringLiteral(" text text ");
  65. +}
  66. +
  67. void tst_QTextLayout::min_maximumWidth()
  68. {
  69. - QString longString("lmong_long_crazy_87235982735_23857239682376923876923876-fuwhfhfw-names-AAAA-deeaois2019-03-03.and.more");
  70. - QTextLayout layout(longString, testFont);
  71. + QFETCH(QString, text);
  72. +
  73. + QTextLayout layout(text, testFont);
  74. + layout.setCacheEnabled(true);
  75. for (int wrapMode = QTextOption::NoWrap; wrapMode <= QTextOption::WrapAtWordBoundaryOrAnywhere; ++wrapMode) {
  76. QTextOption opt;
  77. --
  78. 2.46.0