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 &)
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(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(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(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(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 QFont font = m_style.font ? m_style.font->qfont() : p->font();
344 QFontMetricsF fm(font);
347 for (
int i = 0; i < m_tspans.size(); ++i) {
348 if (m_tspans.at(i) != LINEBREAK)
349 charCount += m_tspans.at(i)->text().size();
352 QRectF approxMaximumBrect(m_coord.x(),
354 charCount * fm.averageCharWidth(),
355 -m_tspans.size() * fm.height());
356 return p->transform().mapRect(approxMaximumBrect);
359QRectF QSvgText::internalBounds(QPainter *p, QSvgExtraStates &states)
const
362 if (shouldDrawNode(p, states))
363 draw_helper(p, states, &boundingRect);
364 return p->transform().mapRect(boundingRect);
367void QSvgText::drawCommand(QPainter *p, QSvgExtraStates &states)
369 draw_helper(p, states);
372bool QSvgText::shouldDrawNode(QPainter *p, QSvgExtraStates &)
const
374 qsizetype numChars = 0;
375 qreal originalFontSize = p->font().pointSizeF();
376 qreal maxFontSize = originalFontSize;
377 for (
const QSvgTspan *span : std::as_const(m_tspans)) {
378 if (span == LINEBREAK)
381 numChars += span->text().size();
383 QSvgFontStyle *style =
static_cast<QSvgFontStyle *>(span->styleProperty(QSvgStyleProperty::FONT));
384 if (style !=
nullptr && style->qfont().pointSizeF() > maxFontSize)
385 maxFontSize = style->qfont().pointSizeF();
388 QFont font = p->font();
389 font.setPixelSize(maxFontSize);
390 QFontMetricsF fm(font);
392 qCWarning(lcSvgDraw) <<
"Text element too high to lay out, ignoring";
397 qCWarning(lcSvgDraw) <<
"Text element too wide to lay out, ignoring";
404bool QSvgText::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
409void QSvgText::draw_helper(QPainter *p, QSvgExtraStates &states, QRectF *boundingRect)
const
411 const bool isPainting = (boundingRect ==
nullptr);
412 if (!isPainting || shouldDrawNode(p, states)) {
413 QFont font = p->font();
414 Qt::Alignment alignment = states.textAnchor;
418 qreal px = m_coord.x();
419 qreal py = m_coord.y();
421 if (m_type == Textarea) {
422 if (alignment == Qt::AlignHCenter)
423 px += m_size.width() / 2;
424 else if (alignment == Qt::AlignRight)
425 px += m_size.width();
429 if (m_size.height() != 0)
430 bounds = QRectF(0, py, 1, m_size.height());
432 bool appendSpace =
false;
433 QStringList paragraphs;
434 QList<QList<QTextLayout::FormatRange> > formatRanges(1);
435 paragraphs.push_back(QString());
437 for (
int i = 0; i < m_tspans.size(); ++i) {
438 if (m_tspans[i] == LINEBREAK) {
439 if (m_type == Textarea) {
440 if (paragraphs.back().isEmpty()) {
441 font.setPixelSize(font.pointSizeF());
442 font.setHintingPreference(QFont::PreferNoHinting);
444 QTextLayout::FormatRange range;
447 range.format.setFont(font);
448 formatRanges.back().append(range);
450 paragraphs.back().append(QLatin1Char(
' '));;
453 paragraphs.push_back(QString());
454 formatRanges.resize(formatRanges.size() + 1);
457 WhitespaceMode mode = m_tspans[i]->whitespaceMode();
458 m_tspans[i]->applyStyle(p, states);
461 font.setPixelSize(font.pointSizeF());
462 font.setHintingPreference(QFont::PreferNoHinting);
464 QString newText(m_tspans[i]->text());
465 newText.replace(QLatin1Char(
'\t'), QLatin1Char(
' '));
466 newText.replace(QLatin1Char(
'\n'), QLatin1Char(
' '));
468 bool prependSpace = !appendSpace && !m_tspans[i]->isTspan() && (mode == Default) && !paragraphs.back().isEmpty() && newText.startsWith(QLatin1Char(
' '));
469 if (appendSpace || prependSpace)
470 paragraphs.back().append(QLatin1Char(
' '));
472 bool appendSpaceNext = (!m_tspans[i]->isTspan() && (mode == Default) && newText.endsWith(QLatin1Char(
' ')));
474 if (mode == Default) {
475 newText = newText.simplified();
476 if (newText.isEmpty())
477 appendSpaceNext =
false;
480 QTextLayout::FormatRange range;
481 range.start = paragraphs.back().size();
482 range.length = newText.size();
483 range.format.setFont(font);
484 if (p->pen().style() != Qt::NoPen && p->pen().brush() != Qt::NoBrush)
485 range.format.setTextOutline(p->pen());
487 range.format.setForeground(p->brush().style() == Qt::NoBrush
488 ? QColor(Qt::transparent) : p->brush());
491 Q_ASSERT(!formatRanges.back().isEmpty());
492 ++formatRanges.back().back().length;
493 }
else if (prependSpace) {
497 formatRanges.back().append(range);
499 appendSpace = appendSpaceNext;
500 paragraphs.back() += newText;
502 m_tspans[i]->revertStyle(p, states);
506 if (states.svgFont) {
509 m_glyphsToDraw = states.svgFont->toGlyphs(paragraphs.join(QLatin1Char(
'\n')));
511 states.svgFont->draw(p, m_coord, m_glyphsToDraw.value(),
512 p->font().pointSizeF(), states.textAnchor);
514 *boundingRect = states.svgFont->boundingRect(p, m_coord, m_glyphsToDraw.value(),
515 p->font().pointSizeF(), states.textAnchor);
519 for (
int i = 0; i < paragraphs.size(); ++i) {
520 QTextLayout tl(paragraphs[i]);
521 QTextOption op = tl.textOption();
522 op.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
523 tl.setTextOption(op);
524 tl.setFormats(formatRanges[i]);
528 QTextLine line = tl.createLine();
531 if (m_size.width() != 0)
532 line.setLineWidth(m_size.width());
536 bool endOfBoundsReached =
false;
537 for (
int i = 0; i < tl.lineCount(); ++i) {
538 QTextLine line = tl.lineAt(i);
541 if (alignment == Qt::AlignHCenter)
542 x -= 0.5 * line.naturalTextWidth();
543 else if (alignment == Qt::AlignRight)
544 x -= line.naturalTextWidth();
546 if (initial && m_type == Text)
550 line.setPosition(QPointF(x, y));
551 brect |= line.naturalTextRect();
554 if ((m_size.width() != 0 && line.naturalTextWidth() > m_size.width())
555 || (m_size.height() != 0 && y + line.height() > m_size.height())) {
558 bounds.setHeight(y - 1);
559 endOfBoundsReached =
true;
563 y += 1.1 * line.height();
566 tl.draw(p, QPointF(px, py), QList<QTextLayout::FormatRange>(), bounds);
568 if (endOfBoundsReached)
572 brect.translate(m_coord);
573 if (bounds.height() > 0)
574 brect.setBottom(qMin(brect.bottom(), bounds.bottom()));
575 *boundingRect = brect;
581void QSvgText::addText(QStringView text)
583 m_tspans.append(
new QSvgTspan(
this,
false));
584 m_tspans.back()->setWhitespaceMode(m_mode);
585 m_tspans.back()->addText(text);
588QSvgTspan::~QSvgTspan()
602 if (Q_UNLIKELY(!m_link || isDescendantOf(m_link) || m_recursing))
605 Q_ASSERT(states.nestedUseCount == 0 || states.nestedUseLevel > 0);
606 if (states.nestedUseLevel > 3 && states.nestedUseCount > (256 + states.nestedUseLevel * 2)) {
611 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
613 if (!m_start.isNull()) {
614 p->translate(m_start);
616 if (states.nestedUseLevel > 0)
617 ++states.nestedUseCount;
619 QScopedValueRollback<
int> useLevelGuard(states.nestedUseLevel, states.nestedUseLevel + 1);
620 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
621 m_link->draw(p, states);
623 if (states.nestedUseLevel == 0)
624 states.nestedUseCount = 0;
626 if (!m_start.isNull()) {
627 p->translate(-m_start);
631QSvgNode::Type QSvgDummyNode::type()
const
633 return FeUnsupported;
637QSvgCircle::~QSvgCircle()
640QSvgNode::Type QSvgCircle::type()
const
645QSvgNode::Type QSvgEllipse::type()
const
650QSvgNode::Type QSvgImage::type()
const
655QSvgNode::Type QSvgLine::type()
const
660QSvgNode::Type QSvgPath::type()
const
665QSvgNode::Type QSvgPolygon::type()
const
670QSvgNode::Type QSvgPolyline::type()
const
675QSvgNode::Type QSvgRect::type()
const
680QSvgNode::Type QSvgText::type()
const
701 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
702 QScopedValueRollback<
bool> guard(m_recursing,
true);
703 p->translate(m_start);
704 bounds = m_link->bounds(p, states);
705 p->translate(-m_start);
713 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
714 QScopedValueRollback<
bool> guard(m_recursing,
true);
715 p->translate(m_start);
716 bounds = m_link->decoratedBounds(p, states);
717 p->translate(-m_start);
722QRectF QSvgPolyline::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
724 return p->transform().mapRect(m_poly.boundingRect());
727QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &s)
const
729 return internalBounds(p, s, BoundsMode::Simplistic);
732QRectF QSvgPolyline::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
734 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
735 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
736 return filterRegion(rect);
739bool QSvgPolyline::requiresGroupRendering()
const
741 return hasAnyMarker();
744QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
746 qreal sw = strokeWidth(p);
747 if (qFuzzyIsNull(sw)) {
748 return p->transform().map(m_poly).boundingRect();
751 path.addPolygon(m_poly);
752 return boundsOnStroke(p, path, sw, mode);
756QRectF QSvgImage::internalBounds(QPainter *p, QSvgExtraStates &)
const
758 return p->transform().mapRect(m_bounds);
761QRectF QSvgLine::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
763 QPointF p1 = p->transform().map(m_line.p1());
764 QPointF p2 = p->transform().map(m_line.p2());
765 qreal minX = qMin(p1.x(), p2.x());
766 qreal minY = qMin(p1.y(), p2.y());
767 qreal maxX = qMax(p1.x(), p2.x());
768 qreal maxY = qMax(p1.y(), p2.y());
769 return QRectF(minX, minY, maxX - minX, maxY - minY);
772QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s)
const
774 return internalBounds(p, s, BoundsMode::Simplistic);
777QRectF QSvgLine::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
779 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
780 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
781 return filterRegion(rect);
784bool QSvgLine::requiresGroupRendering()
const
786 return hasAnyMarker();
789QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s, BoundsMode mode)
const
791 qreal sw = strokeWidth(p);
792 if (qFuzzyIsNull(sw)) {
793 return internalFastBounds(p, s);
796 path.moveTo(m_line.p1());
797 path.lineTo(m_line.p2());
798 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