[graphics/drawy/gsoc2026] src/gui/item: fix: account for mixed fonts and intend width in minWrapWidth

Abdelhadi Wael <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit fcb128151583adc4f20dd7efd32b49e21b065fd5 by Abdelhadi Wael.
Committed on 26/07/2026 at 23:51.
Pushed by mlaurent into branch 'gsoc2026'.

fix: account for mixed fonts and intend width in minWrapWidth

M  +14   -2    src/gui/item/text.cpp
M  +1    -0    src/gui/item/text.hpp

https://invent.kde.org/graphics/drawy/-/commit/fcb128151583adc4f20dd7efd32b49e21b065fd5

diff --git a/src/gui/item/text.cpp b/src/gui/item/text.cpp
index 16d5839e..84c13d22 100644
--- a/src/gui/item/text.cpp
+++ b/src/gui/item/text.cpp
@@ -43,6 +43,7 @@ TextItem::TextItem()
     updateBoundingBox();
 
     QObject::connect(&m_document, &QTextDocument::contentsChanged, &m_document, [this] {
+        m_minWrapWidth = -1;
         updateBoundingBox();
     });
 }
@@ -194,6 +195,7 @@ void TextItem::scaleTextFragments(const qreal scaleY)
         block = block.next();
     }
     m_cursor.endEditBlock();
+    m_minWrapWidth = -1;
 }
 
 void TextItem::drawItem([[maybe_unused]] QPainter &painter, [[maybe_unused]] const QPointF &offset) const
@@ -676,6 +678,16 @@ void TextItem::setWrapWidth(const qreal wrapWidth)
 
 qreal TextItem::minWrapWidth() const
 {
-    const QFontMetricsF metrics{getFont()};
-    return metrics.maxWidth() + 2 * m_document.documentMargin();
+    if (m_minWrapWidth >= 0) {
+        return m_minWrapWidth;
+    }
+
+    const std::unique_ptr<QTextDocument> copy(m_document.clone());
+    QTextOption options = copy->defaultTextOption();
+    options.setWrapMode(QTextOption::WrapAnywhere);
+    copy->setDefaultTextOption(options);
+
+    copy->setTextWidth(0);
+    m_minWrapWidth = copy->size().width() + 2 * m_document.documentMargin();
+    return m_minWrapWidth;
 }
diff --git a/src/gui/item/text.hpp b/src/gui/item/text.hpp
index 1db03a6a..a2d48304 100644
--- a/src/gui/item/text.hpp
+++ b/src/gui/item/text.hpp
@@ -93,6 +93,7 @@ private:
     int m_preeditCursorPos{0};
 
     qreal m_wrapWidth{-1};
+    mutable qreal m_minWrapWidth{-1};
     bool m_isHorizontalResize{false};
 };
 LIBDRAWYGUI_EXPORT QDebug operator<<(QDebug d, const TextItem &t);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.