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
))
32void QSvgDummyNode::drawCommand(QPainter *, QSvgExtraStates &)
34 qWarning(
"Dummy node not meant to be drawn");
37QSvgEllipse::QSvgEllipse(QSvgNode *parent,
const QRectF &rect)
38 : QSvgNode(parent), m_bounds(rect)
42QRectF QSvgEllipse::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
44 return p->transform().mapRect(m_bounds);
47QRectF QSvgEllipse::internalBounds(QPainter *p, QSvgExtraStates &)
const
50 path.addEllipse(m_bounds);
51 qreal sw = strokeWidth(p);
52 return qFuzzyIsNull(sw) ? p->transform().map(path).boundingRect()
53 : boundsOnStroke(p, path, sw, BoundsMode::Simplistic);
56QRectF QSvgEllipse::decoratedInternalBounds(QPainter *p, QSvgExtraStates &)
const
59 path.addEllipse(m_bounds);
60 qreal sw = strokeWidth(p);
61 QRectF rect = qFuzzyIsNull(sw) ? p->transform().map(path).boundingRect()
62 : boundsOnStroke(p, path, sw, BoundsMode::IncludeMiterLimit);
63 return filterRegion(rect);
66void QSvgEllipse::drawCommand(QPainter *p, QSvgExtraStates &)
68 p->drawEllipse(m_bounds);
71bool QSvgEllipse::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
76QSvgImage::QSvgImage(QSvgNode *parent,
78 const QString &filename,
81 , m_filename(filename)
85 if (m_bounds.width() == 0.0)
86 m_bounds.setWidth(
static_cast<qreal>(m_image.width()));
87 if (m_bounds.height() == 0.0)
88 m_bounds.setHeight(
static_cast<qreal>(m_image.height()));
91void QSvgImage::drawCommand(QPainter *p, QSvgExtraStates &)
93 p->drawImage(m_bounds, m_image);
96QSvgLine::QSvgLine(QSvgNode *parent,
const QLineF &line)
97 : QSvgNode(parent), m_line(line)
101void QSvgLine::drawCommand(QPainter *p, QSvgExtraStates &states)
103 if (p->pen().widthF() != 0) {
104 qreal oldOpacity = p->opacity();
105 p->setOpacity(oldOpacity * states.strokeOpacity);
106 if (m_line.isNull() && p->pen().capStyle() != Qt::FlatCap)
107 p->drawPoint(m_line.p1());
110 p->setOpacity(oldOpacity);
112 QSvgMarker::drawMarkersForNode(
this, p, states);
115QSvgPath::QSvgPath(QSvgNode *parent,
const QPainterPath &qpath)
116 : QSvgNode(parent), m_path(qpath)
120void QSvgPath::drawCommand(QPainter *p, QSvgExtraStates &states)
122 const qreal oldOpacity = p->opacity();
123 const bool drawingInOnePass = !separateFillStroke(p, states);
124 if (drawingInOnePass)
125 p->setOpacity(oldOpacity * states.fillOpacity);
126 m_path.setFillRule(states.fillRule);
127 if (m_path.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
128 p->drawPoint(m_path.boundingRect().topLeft());
131 if (!path().isEmpty())
132 QSvgMarker::drawMarkersForNode(
this, p, states);
133 if (drawingInOnePass)
134 p->setOpacity(oldOpacity);
137bool QSvgPath::separateFillStroke(
const QPainter *p,
const QSvgExtraStates &s)
const
139 return !qFuzzyCompare(s.fillOpacity, s.strokeOpacity)
140 || qFuzzyIsNull(p->pen().widthF());
143QRectF QSvgPath::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
145 return p->transform().mapRect(m_path.controlPointRect());
148QRectF QSvgPath::internalBounds(QPainter *p, QSvgExtraStates &)
const
150 qreal sw = strokeWidth(p);
151 return qFuzzyIsNull(sw) ? p->transform().map(m_path).boundingRect()
152 : boundsOnStroke(p, m_path, sw, BoundsMode::Simplistic);
155QRectF QSvgPath::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
157 qreal sw = strokeWidth(p);
158 QRectF rect = qFuzzyIsNull(sw) ? p->transform().map(m_path).boundingRect()
159 : boundsOnStroke(p, m_path, sw, BoundsMode::IncludeMiterLimit);
160 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
161 return filterRegion(rect);
164bool QSvgPath::requiresGroupRendering()
const
166 return hasAnyMarker();
169QSvgPolygon::QSvgPolygon(QSvgNode *parent,
const QPolygonF &poly)
170 : QSvgNode(parent), m_poly(poly)
174QRectF QSvgPolygon::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
176 return p->transform().mapRect(m_poly.boundingRect());
179QRectF QSvgPolygon::internalBounds(QPainter *p, QSvgExtraStates &s)
const
181 return internalBounds(p, s, BoundsMode::Simplistic);
184QRectF QSvgPolygon::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
186 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
187 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
188 return filterRegion(rect);
191bool QSvgPolygon::requiresGroupRendering()
const
193 return hasAnyMarker();
196QRectF QSvgPolygon::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
198 qreal sw = strokeWidth(p);
199 if (qFuzzyIsNull(sw)) {
200 return p->transform().map(m_poly).boundingRect();
203 path.addPolygon(m_poly);
204 return boundsOnStroke(p, path, sw, mode);
208void QSvgPolygon::drawCommand(QPainter *p, QSvgExtraStates &states)
210 if (m_poly.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
211 p->drawPoint(m_poly.first());
213 p->drawPolygon(m_poly, states.fillRule);
214 QSvgMarker::drawMarkersForNode(
this, p, states);
217bool QSvgPolygon::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
222QSvgPolyline::QSvgPolyline(QSvgNode *parent,
const QPolygonF &poly)
223 : QSvgNode(parent), m_poly(poly)
228void QSvgPolyline::drawCommand(QPainter *p, QSvgExtraStates &states)
230 if (p->brush().style() != Qt::NoBrush) {
231 p->drawPolygon(m_poly, states.fillRule);
233 if (m_poly.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
234 p->drawPoint(m_poly.first());
236 p->drawPolyline(m_poly);
237 QSvgMarker::drawMarkersForNode(
this, p, states);
241bool QSvgPolyline::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
246QSvgRect::QSvgRect(QSvgNode *node,
const QRectF &rect, qreal rx, qreal ry)
248 m_rect(rect), m_rx(rx), m_ry(ry)
252QRectF QSvgRect::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
254 return p->transform().mapRect(m_rect);
257QRectF QSvgRect::internalBounds(QPainter *p, QSvgExtraStates &s)
const
259 return internalBounds(p, s, BoundsMode::Simplistic);
262QRectF QSvgRect::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
264 return filterRegion(internalBounds(p, s, BoundsMode::IncludeMiterLimit));
267QRectF QSvgRect::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
269 qreal sw = strokeWidth(p);
270 if (qFuzzyIsNull(sw)) {
271 return p->transform().mapRect(m_rect);
274 path.addRect(m_rect);
275 return boundsOnStroke(p, path, sw, mode);
279void QSvgRect::drawCommand(QPainter *p, QSvgExtraStates &)
282 p->drawRoundedRect(m_rect, m_rx, m_ry, Qt::RelativeSize);
287bool QSvgRect::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
292QSvgTspan *
const QSvgText::LINEBREAK = 0;
294QSvgText::QSvgText(QSvgNode *parent,
const QPointF &coord)
305 for (
int i = 0; i < m_tspans.size(); ++i) {
306 if (m_tspans[i] != LINEBREAK)
311void QSvgText::setTextArea(
const QSizeF &size)
317QRectF QSvgText::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
319 QFont font = m_style.font ? m_style.font->qfont() : p->font();
320 QFontMetricsF fm(font);
323 for (
int i = 0; i < m_tspans.size(); ++i) {
324 if (m_tspans.at(i) != LINEBREAK)
325 charCount += m_tspans.at(i)->text().size();
328 QRectF approxMaximumBrect(m_coord.x(),
330 charCount * fm.averageCharWidth(),
331 -m_tspans.size() * fm.height());
332 return p->transform().mapRect(approxMaximumBrect);
335QRectF QSvgText::internalBounds(QPainter *p, QSvgExtraStates &states)
const
338 if (shouldDrawNode(p, states))
339 draw_helper(p, states, &boundingRect);
340 return p->transform().mapRect(boundingRect);
343void QSvgText::drawCommand(QPainter *p, QSvgExtraStates &states)
345 draw_helper(p, states);
348bool QSvgText::shouldDrawNode(QPainter *p, QSvgExtraStates &)
const
350 qsizetype numChars = 0;
351 qreal originalFontSize = p->font().pointSizeF();
352 qreal maxFontSize = originalFontSize;
353 for (
const QSvgTspan *span : std::as_const(m_tspans)) {
354 if (span == LINEBREAK)
357 numChars += span->text().size();
359 QSvgFontStyle *style =
static_cast<QSvgFontStyle *>(span->styleProperty(QSvgStyleProperty::FONT));
360 if (style !=
nullptr && style->qfont().pointSizeF() > maxFontSize)
361 maxFontSize = style->qfont().pointSizeF();
364 QFont font = p->font();
365 font.setPixelSize(maxFontSize);
366 QFontMetricsF fm(font);
368 qCWarning(lcSvgDraw) <<
"Text element too high to lay out, ignoring";
373 qCWarning(lcSvgDraw) <<
"Text element too wide to lay out, ignoring";
380bool QSvgText::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
385void QSvgText::draw_helper(QPainter *p, QSvgExtraStates &states, QRectF *boundingRect)
const
387 const bool isPainting = (boundingRect ==
nullptr);
388 if (!isPainting || shouldDrawNode(p, states)) {
389 QFont font = p->font();
390 Qt::Alignment alignment = states.textAnchor;
394 qreal px = m_coord.x();
395 qreal py = m_coord.y();
397 if (m_type == Textarea) {
398 if (alignment == Qt::AlignHCenter)
399 px += m_size.width() / 2;
400 else if (alignment == Qt::AlignRight)
401 px += m_size.width();
405 if (m_size.height() != 0)
406 bounds = QRectF(0, py, 1, m_size.height());
408 bool appendSpace =
false;
409 QStringList paragraphs;
410 QList<QList<QTextLayout::FormatRange> > formatRanges(1);
411 paragraphs.push_back(QString());
413 for (
int i = 0; i < m_tspans.size(); ++i) {
414 if (m_tspans[i] == LINEBREAK) {
415 if (m_type == Textarea) {
416 if (paragraphs.back().isEmpty()) {
417 font.setPixelSize(font.pointSizeF());
418 font.setHintingPreference(QFont::PreferNoHinting);
420 QTextLayout::FormatRange range;
423 range.format.setFont(font);
424 formatRanges.back().append(range);
426 paragraphs.back().append(QLatin1Char(
' '));;
429 paragraphs.push_back(QString());
430 formatRanges.resize(formatRanges.size() + 1);
433 WhitespaceMode mode = m_tspans[i]->whitespaceMode();
434 m_tspans[i]->applyStyle(p, states);
437 font.setPixelSize(font.pointSizeF());
438 font.setHintingPreference(QFont::PreferNoHinting);
440 QString newText(m_tspans[i]->text());
441 newText.replace(QLatin1Char(
'\t'), QLatin1Char(
' '));
442 newText.replace(QLatin1Char(
'\n'), QLatin1Char(
' '));
444 bool prependSpace = !appendSpace && !m_tspans[i]->isTspan() && (mode == Default) && !paragraphs.back().isEmpty() && newText.startsWith(QLatin1Char(
' '));
445 if (appendSpace || prependSpace)
446 paragraphs.back().append(QLatin1Char(
' '));
448 bool appendSpaceNext = (!m_tspans[i]->isTspan() && (mode == Default) && newText.endsWith(QLatin1Char(
' ')));
450 if (mode == Default) {
451 newText = newText.simplified();
452 if (newText.isEmpty())
453 appendSpaceNext =
false;
456 QTextLayout::FormatRange range;
457 range.start = paragraphs.back().size();
458 range.length = newText.size();
459 range.format.setFont(font);
460 if (p->pen().style() != Qt::NoPen && p->pen().brush() != Qt::NoBrush)
461 range.format.setTextOutline(p->pen());
463 range.format.setForeground(p->brush().style() == Qt::NoBrush
464 ? QColor(Qt::transparent) : p->brush());
467 Q_ASSERT(!formatRanges.back().isEmpty());
468 ++formatRanges.back().back().length;
469 }
else if (prependSpace) {
473 formatRanges.back().append(range);
475 appendSpace = appendSpaceNext;
476 paragraphs.back() += newText;
478 m_tspans[i]->revertStyle(p, states);
482 if (states.svgFont) {
485 m_glyphsToDraw = states.svgFont->toGlyphs(paragraphs.join(QLatin1Char(
'\n')));
487 states.svgFont->draw(p, m_coord, m_glyphsToDraw.value(),
488 p->font().pointSizeF(), states.textAnchor);
490 *boundingRect = states.svgFont->boundingRect(p, m_coord, m_glyphsToDraw.value(),
491 p->font().pointSizeF(), states.textAnchor);
495 for (
int i = 0; i < paragraphs.size(); ++i) {
496 QTextLayout tl(paragraphs[i]);
497 QTextOption op = tl.textOption();
498 op.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
499 tl.setTextOption(op);
500 tl.setFormats(formatRanges[i]);
504 QTextLine line = tl.createLine();
507 if (m_size.width() != 0)
508 line.setLineWidth(m_size.width());
512 bool endOfBoundsReached =
false;
513 for (
int i = 0; i < tl.lineCount(); ++i) {
514 QTextLine line = tl.lineAt(i);
517 if (alignment == Qt::AlignHCenter)
518 x -= 0.5 * line.naturalTextWidth();
519 else if (alignment == Qt::AlignRight)
520 x -= line.naturalTextWidth();
522 if (initial && m_type == Text)
526 line.setPosition(QPointF(x, y));
527 brect |= line.naturalTextRect();
530 if ((m_size.width() != 0 && line.naturalTextWidth() > m_size.width())
531 || (m_size.height() != 0 && y + line.height() > m_size.height())) {
534 bounds.setHeight(y - 1);
535 endOfBoundsReached =
true;
539 y += 1.1 * line.height();
542 tl.draw(p, QPointF(px, py), QList<QTextLayout::FormatRange>(), bounds);
544 if (endOfBoundsReached)
548 brect.translate(m_coord);
549 if (bounds.height() > 0)
550 brect.setBottom(qMin(brect.bottom(), bounds.bottom()));
551 *boundingRect = brect;
557void QSvgText::addText(QStringView text)
559 m_tspans.append(
new QSvgTspan(
this,
false));
560 m_tspans.back()->setWhitespaceMode(m_mode);
561 m_tspans.back()->addText(text);
572 if (Q_UNLIKELY(!m_link || isDescendantOf(m_link) || m_recursing))
575 Q_ASSERT(states.nestedUseCount == 0 || states.nestedUseLevel > 0);
576 if (states.nestedUseLevel > 3 && states.nestedUseCount > (256 + states.nestedUseLevel * 2)) {
581 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
583 if (!m_start.isNull()) {
584 p->translate(m_start);
586 if (states.nestedUseLevel > 0)
587 ++states.nestedUseCount;
589 QScopedValueRollback<
int> useLevelGuard(states.nestedUseLevel, states.nestedUseLevel + 1);
590 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
591 m_link->draw(p, states);
593 if (states.nestedUseLevel == 0)
594 states.nestedUseCount = 0;
596 if (!m_start.isNull()) {
597 p->translate(-m_start);
601QSvgNode::Type QSvgDummyNode::type()
const
603 return FeUnsupported;
606QSvgNode::Type QSvgCircle::type()
const
611QSvgNode::Type QSvgEllipse::type()
const
616QSvgNode::Type QSvgImage::type()
const
621QSvgNode::Type QSvgLine::type()
const
626QSvgNode::Type QSvgPath::type()
const
631QSvgNode::Type QSvgPolygon::type()
const
636QSvgNode::Type QSvgPolyline::type()
const
641QSvgNode::Type QSvgRect::type()
const
646QSvgNode::Type QSvgText::type()
const
664 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
665 QScopedValueRollback<
bool> guard(m_recursing,
true);
666 p->translate(m_start);
667 bounds = m_link->bounds(p, states);
668 p->translate(-m_start);
676 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
677 QScopedValueRollback<
bool> guard(m_recursing,
true);
678 p->translate(m_start);
679 bounds = m_link->decoratedBounds(p, states);
680 p->translate(-m_start);
685QRectF QSvgPolyline::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
687 return p->transform().mapRect(m_poly.boundingRect());
690QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &s)
const
692 return internalBounds(p, s, BoundsMode::Simplistic);
695QRectF QSvgPolyline::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
697 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
698 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
699 return filterRegion(rect);
702bool QSvgPolyline::requiresGroupRendering()
const
704 return hasAnyMarker();
707QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
709 qreal sw = strokeWidth(p);
710 if (qFuzzyIsNull(sw)) {
711 return p->transform().map(m_poly).boundingRect();
714 path.addPolygon(m_poly);
715 return boundsOnStroke(p, path, sw, mode);
719QRectF QSvgImage::internalBounds(QPainter *p, QSvgExtraStates &)
const
721 return p->transform().mapRect(m_bounds);
724QRectF QSvgLine::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
726 QPointF p1 = p->transform().map(m_line.p1());
727 QPointF p2 = p->transform().map(m_line.p2());
728 qreal minX = qMin(p1.x(), p2.x());
729 qreal minY = qMin(p1.y(), p2.y());
730 qreal maxX = qMax(p1.x(), p2.x());
731 qreal maxY = qMax(p1.y(), p2.y());
732 return QRectF(minX, minY, maxX - minX, maxY - minY);
735QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s)
const
737 return internalBounds(p, s, BoundsMode::Simplistic);
740QRectF QSvgLine::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
742 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
743 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
744 return filterRegion(rect);
747bool QSvgLine::requiresGroupRendering()
const
749 return hasAnyMarker();
752QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s, BoundsMode mode)
const
754 qreal sw = strokeWidth(p);
755 if (qFuzzyIsNull(sw)) {
756 return internalFastBounds(p, s);
759 path.moveTo(m_line.p1());
760 path.lineTo(m_line.p2());
761 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