10#include <qabstracttextdocumentlayout.h>
12#include <qloggingcategory.h>
14#include <qscopedvaluerollback.h>
15#include <qtextcursor.h>
16#include <qtextdocument.h>
17#include <private/qfixed_p.h>
19#include <QElapsedTimer>
20#include <QLoggingCategory>
28#ifndef QT_SVG_MAX_LAYOUT_SIZE
29#define QT_SVG_MAX_LAYOUT_SIZE (qint64(QFIXED_MAX / 2
))
32QSvgDummyNode::~QSvgDummyNode()
35void QSvgDummyNode::drawCommand(QPainter *, QSvgExtraStates &)
37 qWarning(
"Dummy node not meant to be drawn");
40QSvgEllipse::QSvgEllipse(QSvgNode *parent,
const QRectF &rect)
41 : QSvgNode(parent), m_bounds(rect)
45QSvgEllipse::~QSvgEllipse()
48QRectF QSvgEllipse::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
50 return p->transform().mapRect(m_bounds);
53QRectF QSvgEllipse::internalBounds(QPainter *p, QSvgExtraStates &)
const
56 path.addEllipse(m_bounds);
57 qreal sw = strokeWidth(p);
58 return qFuzzyIsNull(sw) ? p->transform().map(path).boundingRect()
59 : boundsOnStroke(p, path, sw, BoundsMode::Simplistic);
62QRectF QSvgEllipse::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
65 path.addEllipse(m_bounds);
66 qreal sw = strokeWidth(p);
67 QRectF rect = qFuzzyIsNull(sw) ? p->transform().map(path).boundingRect()
68 : boundsOnStroke(p, path, sw, BoundsMode::IncludeMiterLimit);
69 return filterRegion(s.doc(), rect);
72void QSvgEllipse::drawCommand(QPainter *p, QSvgExtraStates &)
74 p->drawEllipse(m_bounds);
77bool QSvgEllipse::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
82QSvgImage::QSvgImage(QSvgNode *parent,
84 const QString &filename,
87 , m_filename(filename)
91 if (m_bounds.width() == 0.0)
92 m_bounds.setWidth(
static_cast<qreal>(m_image.width()));
93 if (m_bounds.height() == 0.0)
94 m_bounds.setHeight(
static_cast<qreal>(m_image.height()));
97QSvgImage::~QSvgImage()
100void QSvgImage::drawCommand(QPainter *p, QSvgExtraStates &)
102 p->drawImage(m_bounds, m_image);
105QSvgLine::QSvgLine(QSvgNode *parent,
const QLineF &line)
106 : QSvgNode(parent), m_line(line)
113void QSvgLine::drawCommand(QPainter *p, QSvgExtraStates &states)
115 if (p->pen().widthF() != 0) {
116 qreal oldOpacity = p->opacity();
117 p->setOpacity(oldOpacity * states.strokeOpacity);
118 if (m_line.isNull() && p->pen().capStyle() != Qt::FlatCap)
119 p->drawPoint(m_line.p1());
122 p->setOpacity(oldOpacity);
124 QSvgMarker::drawMarkersForNode(
this, p, states);
127QSvgPath::QSvgPath(QSvgNode *parent,
const QPainterPath &qpath)
128 : QSvgNode(parent), m_path(qpath)
135void QSvgPath::drawCommand(QPainter *p, QSvgExtraStates &states)
137 const qreal oldOpacity = p->opacity();
138 const bool drawingInOnePass = !separateFillStroke(p, states);
139 if (drawingInOnePass)
140 p->setOpacity(oldOpacity * states.fillOpacity);
141 m_path.setFillRule(states.fillRule);
142 if (m_path.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
143 p->drawPoint(m_path.boundingRect().topLeft());
146 if (!path().isEmpty())
147 QSvgMarker::drawMarkersForNode(
this, p, states);
148 if (drawingInOnePass)
149 p->setOpacity(oldOpacity);
152bool QSvgPath::separateFillStroke(
const QPainter *p,
const QSvgExtraStates &s)
const
154 return !qFuzzyCompare(s.fillOpacity, s.strokeOpacity)
155 || qFuzzyIsNull(p->pen().widthF());
158QRectF QSvgPath::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
160 return p->transform().mapRect(m_path.controlPointRect());
163QRectF QSvgPath::internalBounds(QPainter *p, QSvgExtraStates &)
const
165 qreal sw = strokeWidth(p);
166 return qFuzzyIsNull(sw) ? p->transform().map(m_path).boundingRect()
167 : boundsOnStroke(p, m_path, sw, BoundsMode::Simplistic);
170QRectF QSvgPath::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
172 qreal sw = strokeWidth(p);
173 QRectF rect = qFuzzyIsNull(sw) ? p->transform().map(m_path).boundingRect()
174 : boundsOnStroke(p, m_path, sw, BoundsMode::IncludeMiterLimit);
175 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
176 return filterRegion(s.doc(), rect);
179bool QSvgPath::requiresGroupRendering()
const
181 return hasAnyMarker();
184QSvgPolygon::QSvgPolygon(QSvgNode *parent,
const QPolygonF &poly)
185 : QSvgNode(parent), m_poly(poly)
189QSvgPolygon::~QSvgPolygon()
192QRectF QSvgPolygon::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
194 return p->transform().mapRect(m_poly.boundingRect());
197QRectF QSvgPolygon::internalBounds(QPainter *p, QSvgExtraStates &s)
const
199 return internalBounds(p, s, BoundsMode::Simplistic);
202QRectF QSvgPolygon::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
204 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
205 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
206 return filterRegion(s.doc(), rect);
209bool QSvgPolygon::requiresGroupRendering()
const
211 return hasAnyMarker();
214QRectF QSvgPolygon::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
216 qreal sw = strokeWidth(p);
217 if (qFuzzyIsNull(sw)) {
218 return p->transform().map(m_poly).boundingRect();
221 path.addPolygon(m_poly);
222 return boundsOnStroke(p, path, sw, mode);
226void QSvgPolygon::drawCommand(QPainter *p, QSvgExtraStates &states)
228 if (m_poly.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
229 p->drawPoint(m_poly.first());
231 p->drawPolygon(m_poly, states.fillRule);
232 QSvgMarker::drawMarkersForNode(
this, p, states);
235bool QSvgPolygon::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
240QSvgPolyline::QSvgPolyline(QSvgNode *parent,
const QPolygonF &poly)
241 : QSvgNode(parent), m_poly(poly)
246QSvgPolyline::~QSvgPolyline()
249void QSvgPolyline::drawCommand(QPainter *p, QSvgExtraStates &states)
251 if (p->brush().style() != Qt::NoBrush) {
252 p->drawPolygon(m_poly, states.fillRule);
254 if (m_poly.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
255 p->drawPoint(m_poly.first());
257 p->drawPolyline(m_poly);
258 QSvgMarker::drawMarkersForNode(
this, p, states);
262bool QSvgPolyline::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
267QSvgRect::QSvgRect(QSvgNode *node,
const QRectF &rect, qreal rx, qreal ry)
269 m_rect(rect), m_rx(rx), m_ry(ry)
276QRectF QSvgRect::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
278 return p->transform().mapRect(m_rect);
281QRectF QSvgRect::internalBounds(QPainter *p, QSvgExtraStates &s)
const
283 return internalBounds(p, s, BoundsMode::Simplistic);
286QRectF QSvgRect::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
288 return filterRegion(s.doc(), internalBounds(p, s, BoundsMode::IncludeMiterLimit));
291QRectF QSvgRect::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
293 qreal sw = strokeWidth(p);
294 if (qFuzzyIsNull(sw)) {
295 return p->transform().mapRect(m_rect);
298 path.addRect(m_rect);
299 return boundsOnStroke(p, path, sw, mode);
303void QSvgRect::drawCommand(QPainter *p, QSvgExtraStates &)
306 p->drawRoundedRect(m_rect, m_rx, m_ry, Qt::RelativeSize);
311bool QSvgRect::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
316QSvgTspan *
const QSvgText::LINEBREAK = 0;
318QSvgText::QSvgText(QSvgNode *parent,
const QPointF &coord)
329 for (
int i = 0; i < m_tspans.size(); ++i) {
330 if (m_tspans[i] != LINEBREAK)
335void QSvgText::setTextArea(
const QSizeF &size)
341QRectF QSvgText::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
343 QSvgFontStyle *fontProperty =
static_cast<QSvgFontStyle *>(m_style.property(QSvgStyleProperty::Font));
344 QFont font = fontProperty ? fontProperty->qfont() : p->font();
345 QFontMetricsF fm(font);
348 for (
int i = 0; i < m_tspans.size(); ++i) {
349 if (m_tspans.at(i) != LINEBREAK)
350 charCount += m_tspans.at(i)->text().size();
353 QRectF approxMaximumBrect(m_coord.x(),
355 charCount * fm.averageCharWidth(),
356 -m_tspans.size() * fm.height());
357 return p->transform().mapRect(approxMaximumBrect);
360QRectF QSvgText::internalBounds(QPainter *p, QSvgExtraStates &states)
const
363 if (shouldDrawNode(p, states))
364 draw_helper(p, states, &boundingRect);
365 return p->transform().mapRect(boundingRect);
368void QSvgText::drawCommand(QPainter *p, QSvgExtraStates &states)
370 draw_helper(p, states);
373bool QSvgText::shouldDrawNode(QPainter *p, QSvgExtraStates &)
const
375 qsizetype numChars = 0;
376 qreal originalFontSize = p->font().pointSizeF();
377 qreal maxFontSize = originalFontSize;
378 for (
const QSvgTspan *span : std::as_const(m_tspans)) {
379 if (span == LINEBREAK)
382 numChars += span->text().size();
384 QSvgFontStyle *style =
static_cast<QSvgFontStyle *>(span->styleProperty(QSvgStyleProperty::Font));
385 if (style !=
nullptr && style->qfont().pointSizeF() > maxFontSize)
386 maxFontSize = style->qfont().pointSizeF();
389 QFont font = p->font();
390 font.setPixelSize(maxFontSize);
391 QFontMetricsF fm(font);
393 qCWarning(lcSvgDraw) <<
"Text element too high to lay out, ignoring";
398 qCWarning(lcSvgDraw) <<
"Text element too wide to lay out, ignoring";
405bool QSvgText::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
410void QSvgText::draw_helper(QPainter *p, QSvgExtraStates &states, QRectF *boundingRect)
const
412 const bool isPainting = (boundingRect ==
nullptr);
413 if (!isPainting || shouldDrawNode(p, states)) {
414 QFont font = p->font();
415 Qt::Alignment alignment = states.textAnchor;
419 qreal px = m_coord.x();
420 qreal py = m_coord.y();
422 if (m_type == Textarea) {
423 if (alignment == Qt::AlignHCenter)
424 px += m_size.width() / 2;
425 else if (alignment == Qt::AlignRight)
426 px += m_size.width();
430 if (m_size.height() != 0)
431 bounds = QRectF(0, py, 1, m_size.height());
433 bool appendSpace =
false;
434 QStringList paragraphs;
435 QList<QList<QTextLayout::FormatRange> > formatRanges(1);
438 QList<std::optional<QPointF>> paragraphPos;
439 paragraphs.push_back(QString());
440 paragraphPos.push_back(std::nullopt);
442 for (
int i = 0; i < m_tspans.size(); ++i) {
443 if (m_tspans[i] == LINEBREAK) {
447 if (m_type == Textarea && paragraphs.back().isEmpty()) {
448 font.setPixelSize(font.pointSizeF());
449 font.setHintingPreference(QFont::PreferNoHinting);
451 QTextLayout::FormatRange range;
454 range.format.setFont(font);
455 formatRanges.back().append(range);
457 paragraphs.back().append(QLatin1Char(
' '));;
460 paragraphs.push_back(QString());
461 formatRanges.resize(formatRanges.size() + 1);
462 paragraphPos.push_back(std::nullopt);
467 if (m_tspans[i]->isTspan() && m_tspans[i]->hasPosition()) {
468 if (!paragraphs.back().isEmpty()) {
469 paragraphs.push_back(QString());
470 formatRanges.resize(formatRanges.size() + 1);
471 paragraphPos.push_back(std::nullopt);
474 paragraphPos.back() = QPointF(m_tspans[i]->hasX() ? m_tspans[i]->x() : m_coord.x(),
475 m_tspans[i]->hasY() ? m_tspans[i]->y() : m_coord.y());
477 WhitespaceMode mode = m_tspans[i]->whitespaceMode();
478 m_tspans[i]->applyStyle(p, states);
481 font.setPixelSize(font.pointSizeF());
482 font.setHintingPreference(QFont::PreferNoHinting);
484 QString newText(m_tspans[i]->text());
485 newText.replace(QLatin1Char(
'\t'), QLatin1Char(
' '));
486 newText.replace(QLatin1Char(
'\n'), QLatin1Char(
' '));
488 bool prependSpace = !appendSpace && !m_tspans[i]->isTspan() && (mode == Default) && !paragraphs.back().isEmpty() && newText.startsWith(QLatin1Char(
' '));
489 if (appendSpace || prependSpace)
490 paragraphs.back().append(QLatin1Char(
' '));
492 bool appendSpaceNext = (!m_tspans[i]->isTspan() && (mode == Default) && newText.endsWith(QLatin1Char(
' ')));
494 if (mode == Default) {
495 newText = newText.simplified();
496 if (newText.isEmpty())
497 appendSpaceNext =
false;
500 QTextLayout::FormatRange range;
501 range.start = paragraphs.back().size();
502 range.length = newText.size();
503 range.format.setFont(font);
504 if (p->pen().style() != Qt::NoPen && p->pen().brush() != Qt::NoBrush)
505 range.format.setTextOutline(p->pen());
507 range.format.setForeground(p->brush().style() == Qt::NoBrush
508 ? QColor(Qt::transparent) : p->brush());
511 Q_ASSERT(!formatRanges.back().isEmpty());
512 ++formatRanges.back().back().length;
513 }
else if (prependSpace) {
517 formatRanges.back().append(range);
519 appendSpace = appendSpaceNext;
520 paragraphs.back() += newText;
522 m_tspans[i]->revertStyle(p, states);
526 if (states.svgFont) {
529 m_glyphsToDraw = states.svgFont->toGlyphs(paragraphs.join(QLatin1Char(
'\n')));
531 states.svgFont->draw(p, m_coord, m_glyphsToDraw.value(),
532 p->font().pointSizeF(), states.textAnchor);
534 *boundingRect = states.svgFont->boundingRect(p, m_coord, m_glyphsToDraw.value(),
535 p->font().pointSizeF(), states.textAnchor);
539 for (
int i = 0; i < paragraphs.size(); ++i) {
540 QTextLayout tl(paragraphs[i]);
541 QTextOption op = tl.textOption();
542 op.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
543 tl.setTextOption(op);
544 tl.setFormats(formatRanges[i]);
548 QTextLine line = tl.createLine();
551 if (m_size.width() != 0)
552 line.setLineWidth(m_size.width());
558 const bool positioned = paragraphPos[i].has_value();
559 const qreal originX = positioned ? paragraphPos[i]->x() : px;
560 const qreal originY = positioned ? paragraphPos[i]->y() : py;
563 bool endOfBoundsReached =
false;
564 for (
int j = 0; j < tl.lineCount(); ++j) {
565 QTextLine line = tl.lineAt(j);
568 if (alignment == Qt::AlignHCenter)
569 x -= 0.5 * line.naturalTextWidth();
570 else if (alignment == Qt::AlignRight)
571 x -= line.naturalTextWidth();
577 localY -= line.ascent();
578 line.setPosition(QPointF(x, localY));
579 brect |= line.naturalTextRect().translated(originX, originY);
580 localY += 1.1 * line.height();
584 if (initial && m_type == Text)
588 line.setPosition(QPointF(x, y));
589 brect |= line.naturalTextRect().translated(originX, originY);
592 if ((m_size.width() != 0 && line.naturalTextWidth() > m_size.width())
593 || (m_size.height() != 0 && y + line.height() > m_size.height())) {
596 bounds.setHeight(y - 1);
597 endOfBoundsReached =
true;
601 y += 1.1 * line.height();
604 tl.draw(p, QPointF(originX, originY), QList<QTextLayout::FormatRange>(), bounds);
606 if (endOfBoundsReached)
610 if (bounds.height() > 0)
611 brect.setBottom(qMin(brect.bottom(), bounds.bottom()));
612 *boundingRect = brect;
618void QSvgText::addText(QStringView text)
620 m_tspans.append(
new QSvgTspan(
this,
false));
621 m_tspans.back()->setWhitespaceMode(m_mode);
622 m_tspans.back()->addText(text);
625QSvgTspan::~QSvgTspan()
639 if (Q_UNLIKELY(!m_link || isDescendantOf(m_link) || m_recursing))
642 Q_ASSERT(states.nestedUseCount == 0 || states.nestedUseLevel > 0);
643 if (states.nestedUseLevel > 3 && states.nestedUseCount > (256 + states.nestedUseLevel * 2)) {
648 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
650 if (!m_start.isNull()) {
651 p->translate(m_start);
653 if (states.nestedUseLevel > 0)
654 ++states.nestedUseCount;
656 QScopedValueRollback<
int> useLevelGuard(states.nestedUseLevel, states.nestedUseLevel + 1);
657 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
658 m_link->draw(p, states);
660 if (states.nestedUseLevel == 0)
661 states.nestedUseCount = 0;
663 if (!m_start.isNull()) {
664 p->translate(-m_start);
668QSvgNode::Type QSvgDummyNode::type()
const
670 return FeUnsupported;
674QSvgCircle::~QSvgCircle()
677QSvgNode::Type QSvgCircle::type()
const
682QSvgNode::Type QSvgEllipse::type()
const
687QSvgNode::Type QSvgImage::type()
const
692QSvgNode::Type QSvgLine::type()
const
697QSvgNode::Type QSvgPath::type()
const
702QSvgNode::Type QSvgPolygon::type()
const
707QSvgNode::Type QSvgPolyline::type()
const
712QSvgNode::Type QSvgRect::type()
const
717QSvgNode::Type QSvgText::type()
const
738 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
739 QScopedValueRollback<
bool> guard(m_recursing,
true);
740 p->translate(m_start);
741 bounds = m_link->bounds(p, states);
742 p->translate(-m_start);
750 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
751 QScopedValueRollback<
bool> guard(m_recursing,
true);
752 p->translate(m_start);
753 bounds = m_link->decoratedBounds(p, states);
754 p->translate(-m_start);
759QRectF QSvgPolyline::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
761 return p->transform().mapRect(m_poly.boundingRect());
764QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &s)
const
766 return internalBounds(p, s, BoundsMode::Simplistic);
769QRectF QSvgPolyline::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
771 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
772 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
773 return filterRegion(s.doc(), rect);
776bool QSvgPolyline::requiresGroupRendering()
const
778 return hasAnyMarker();
781QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
783 qreal sw = strokeWidth(p);
784 if (qFuzzyIsNull(sw)) {
785 return p->transform().map(m_poly).boundingRect();
788 path.addPolygon(m_poly);
789 return boundsOnStroke(p, path, sw, mode);
793QRectF QSvgImage::internalBounds(QPainter *p, QSvgExtraStates &)
const
795 return p->transform().mapRect(m_bounds);
798QRectF QSvgLine::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
800 QPointF p1 = p->transform().map(m_line.p1());
801 QPointF p2 = p->transform().map(m_line.p2());
802 qreal minX = qMin(p1.x(), p2.x());
803 qreal minY = qMin(p1.y(), p2.y());
804 qreal maxX = qMax(p1.x(), p2.x());
805 qreal maxY = qMax(p1.y(), p2.y());
806 return QRectF(minX, minY, maxX - minX, maxY - minY);
809QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s)
const
811 return internalBounds(p, s, BoundsMode::Simplistic);
814QRectF QSvgLine::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
816 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
817 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
818 return filterRegion(s.doc(), rect);
821bool QSvgLine::requiresGroupRendering()
const
823 return hasAnyMarker();
826QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s, BoundsMode mode)
const
828 qreal sw = strokeWidth(p);
829 if (qFuzzyIsNull(sw)) {
830 return internalFastBounds(p, s);
833 path.moveTo(m_line.p1());
834 path.lineTo(m_line.p2());
835 return boundsOnStroke(p, path, sw, mode);
QRectF decoratedInternalBounds(QPainter *p, QSvgExtraStates &states) const override
void drawCommand(QPainter *p, QSvgExtraStates &states) override
QRectF internalBounds(QPainter *p, QSvgExtraStates &states) const override
QSvgUse(const QPointF &start, QSvgNode *parent, QSvgNode *link)
Type type() const override
Type type() const override
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
#define qPrintable(string)
#define QT_SVG_MAX_LAYOUT_SIZE