summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2021-05-07 13:18:20 +0200
committerEirik Aavitsland <eirik.aavitsland@qt.io>2021-05-10 07:16:08 +0200
commit7063de4f39c2915a69ce4bfad992c2e44df9dccb (patch)
tree422325f485a55eb442ee02acc0377f3c459ce9bd /src
parentbec52435c63856d7ad87eaa273fe2f2b67372295 (diff)
downloadqtsvg-7063de4f39c2915a69ce4bfad992c2e44df9dccb.tar.gz
Implement QSvgRenderer::boundsOnElement() for text nodes
Fixes: QTBUG-32405 Change-Id: Ifb8c418546b378d9e3d6b272fc782775f6010faa Reviewed-by: Jonas Karlsson <jonas.karlsson@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/svg/qsvggraphics.cpp24
-rw-r--r--src/svg/qsvggraphics_p.h5
2 files changed, 26 insertions, 3 deletions
diff --git a/src/svg/qsvggraphics.cpp b/src/svg/qsvggraphics.cpp
index 65b9304..bc4f9d6 100644
--- a/src/svg/qsvggraphics.cpp
+++ b/src/svg/qsvggraphics.cpp
@@ -285,10 +285,21 @@ void QSvgText::setTextArea(const QSizeF &size)
m_type = TEXTAREA;
}
-//QRectF QSvgText::bounds(QPainter *p, QSvgExtraStates &) const {}
+QRectF QSvgText::bounds(QPainter *p, QSvgExtraStates &states) const
+{
+ QRectF boundingRect;
+ draw_helper(p, states, &boundingRect);
+ return boundingRect;
+}
void QSvgText::draw(QPainter *p, QSvgExtraStates &states)
{
+ draw_helper(p, states);
+}
+
+void QSvgText::draw_helper(QPainter *p, QSvgExtraStates &states, QRectF *boundingRect) const
+{
+ const bool isPainting = (boundingRect == nullptr);
applyStyle(p, states);
qreal oldOpacity = p->opacity();
p->setOpacity(oldOpacity * states.fillOpacity);
@@ -397,6 +408,7 @@ void QSvgText::draw(QPainter *p, QSvgExtraStates &states)
}
states.svgFont->draw(p, m_coord * scale, text, p->font().pointSizeF() * scale, states.textAnchor);
} else {
+ QRectF brect;
for (int i = 0; i < paragraphs.size(); ++i) {
QTextLayout tl(paragraphs[i]);
QTextOption op = tl.textOption();
@@ -429,6 +441,7 @@ void QSvgText::draw(QPainter *p, QSvgExtraStates &states)
initial = false;
line.setPosition(QPointF(x, y));
+ brect |= line.naturalTextRect();
// Check if the current line fits into the bounding rectangle.
if ((m_size.width() != 0 && line.naturalTextWidth() > scaledSize.width())
@@ -442,11 +455,18 @@ void QSvgText::draw(QPainter *p, QSvgExtraStates &states)
y += 1.1 * line.height();
}
- tl.draw(p, QPointF(px, py), QList<QTextLayout::FormatRange>(), bounds);
+ if (isPainting)
+ tl.draw(p, QPointF(px, py), QList<QTextLayout::FormatRange>(), bounds);
if (endOfBoundsReached)
break;
}
+ if (boundingRect) {
+ brect.translate(m_coord * scale);
+ if (bounds.height() > 0)
+ brect.setBottom(qMin(brect.bottom(), bounds.bottom()));
+ *boundingRect = p->transform().mapRect(brect);
+ }
}
p->setWorldTransform(oldTransform, false);
diff --git a/src/svg/qsvggraphics_p.h b/src/svg/qsvggraphics_p.h
index d2af6d6..9943185 100644
--- a/src/svg/qsvggraphics_p.h
+++ b/src/svg/qsvggraphics_p.h
@@ -196,8 +196,11 @@ public:
void addLineBreak() {m_tspans.append(LINEBREAK);}
void setWhitespaceMode(WhitespaceMode mode) {m_mode = mode;}
- //QRectF bounds(QPainter *p, QSvgExtraStates &states) const override;
+ QRectF bounds(QPainter *p, QSvgExtraStates &states) const override;
+
private:
+ void draw_helper(QPainter *p, QSvgExtraStates &states, QRectF *boundingRect = nullptr) const;
+
static QSvgTspan * const LINEBREAK;
QPointF m_coord;