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
))
33
34
35
36
37
38
39
40
41
44 const qreal size = font.pointSizeF();
46 font.setPixelSize(QSvgFontStyle::pixelSize(size));
49QSvgDummyNode::~QSvgDummyNode()
52void QSvgDummyNode::drawCommand(QPainter *, QSvgExtraStates &)
54 qWarning(
"Dummy node not meant to be drawn");
57QSvgEllipse::QSvgEllipse(QSvgNode *parent,
const QRectF &rect)
58 : QSvgNode(parent), m_bounds(rect)
62QSvgEllipse::~QSvgEllipse()
65QRectF QSvgEllipse::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
67 return p->transform().mapRect(m_bounds);
70QRectF QSvgEllipse::internalBounds(QPainter *p, QSvgExtraStates &)
const
73 path.addEllipse(m_bounds);
74 qreal sw = strokeWidth(p);
75 return qFuzzyIsNull(sw) ? p->transform().map(path).boundingRect()
76 : boundsOnStroke(p, path, sw, BoundsMode::Simplistic);
79QRectF QSvgEllipse::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
82 path.addEllipse(m_bounds);
83 qreal sw = strokeWidth(p);
84 QRectF rect = qFuzzyIsNull(sw) ? p->transform().map(path).boundingRect()
85 : boundsOnStroke(p, path, sw, BoundsMode::IncludeMiterLimit);
86 return filterRegion(s.doc(), rect);
89void QSvgEllipse::drawCommand(QPainter *p, QSvgExtraStates &)
91 p->drawEllipse(m_bounds);
94bool QSvgEllipse::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
99QSvgImage::QSvgImage(QSvgNode *parent,
101 const QString &filename,
102 const QRectF &bounds)
104 , m_filename(filename)
108 if (m_bounds.width() == 0.0)
109 m_bounds.setWidth(
static_cast<qreal>(m_image.width()));
110 if (m_bounds.height() == 0.0)
111 m_bounds.setHeight(
static_cast<qreal>(m_image.height()));
114QSvgImage::~QSvgImage()
117void QSvgImage::drawCommand(QPainter *p, QSvgExtraStates &)
119 p->drawImage(m_bounds, m_image);
122QSvgLine::QSvgLine(QSvgNode *parent,
const QLineF &line)
123 : QSvgNode(parent), m_line(line)
130void QSvgLine::drawCommand(QPainter *p, QSvgExtraStates &states)
132 if (p->pen().widthF() != 0) {
133 qreal oldOpacity = p->opacity();
134 p->setOpacity(oldOpacity * states.strokeOpacity);
135 if (m_line.isNull() && p->pen().capStyle() != Qt::FlatCap)
136 p->drawPoint(m_line.p1());
139 p->setOpacity(oldOpacity);
141 QSvgMarker::drawMarkersForNode(
this, p, states);
144QSvgPath::QSvgPath(QSvgNode *parent,
const QPainterPath &qpath)
145 : QSvgNode(parent), m_path(qpath)
152void QSvgPath::drawCommand(QPainter *p, QSvgExtraStates &states)
154 const qreal oldOpacity = p->opacity();
155 const bool drawingInOnePass = !separateFillStroke(p, states);
156 if (drawingInOnePass)
157 p->setOpacity(oldOpacity * states.fillOpacity);
158 m_path.setFillRule(states.fillRule);
159 if (m_path.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
160 p->drawPoint(m_path.boundingRect().topLeft());
163 if (!path().isEmpty())
164 QSvgMarker::drawMarkersForNode(
this, p, states);
165 if (drawingInOnePass)
166 p->setOpacity(oldOpacity);
169bool QSvgPath::separateFillStroke(
const QPainter *p,
const QSvgExtraStates &s)
const
171 return !qFuzzyCompare(s.fillOpacity, s.strokeOpacity)
172 || qFuzzyIsNull(p->pen().widthF());
175QRectF QSvgPath::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
177 return p->transform().mapRect(m_path.controlPointRect());
180QRectF QSvgPath::internalBounds(QPainter *p, QSvgExtraStates &)
const
182 qreal sw = strokeWidth(p);
183 return qFuzzyIsNull(sw) ? p->transform().map(m_path).boundingRect()
184 : boundsOnStroke(p, m_path, sw, BoundsMode::Simplistic);
187QRectF QSvgPath::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
189 qreal sw = strokeWidth(p);
190 QRectF rect = qFuzzyIsNull(sw) ? p->transform().map(m_path).boundingRect()
191 : boundsOnStroke(p, m_path, sw, BoundsMode::IncludeMiterLimit);
192 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
193 return filterRegion(s.doc(), rect);
196bool QSvgPath::requiresGroupRendering()
const
198 return hasAnyMarker();
201QSvgPolygon::QSvgPolygon(QSvgNode *parent,
const QPolygonF &poly)
202 : QSvgNode(parent), m_poly(poly)
206QSvgPolygon::~QSvgPolygon()
209QRectF QSvgPolygon::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
211 return p->transform().mapRect(m_poly.boundingRect());
214QRectF QSvgPolygon::internalBounds(QPainter *p, QSvgExtraStates &s)
const
216 return internalBounds(p, s, BoundsMode::Simplistic);
219QRectF QSvgPolygon::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
221 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
222 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
223 return filterRegion(s.doc(), rect);
226bool QSvgPolygon::requiresGroupRendering()
const
228 return hasAnyMarker();
231QRectF QSvgPolygon::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
233 qreal sw = strokeWidth(p);
234 if (qFuzzyIsNull(sw)) {
235 return p->transform().map(m_poly).boundingRect();
238 path.addPolygon(m_poly);
239 return boundsOnStroke(p, path, sw, mode);
243void QSvgPolygon::drawCommand(QPainter *p, QSvgExtraStates &states)
245 if (m_poly.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
246 p->drawPoint(m_poly.first());
248 p->drawPolygon(m_poly, states.fillRule);
249 QSvgMarker::drawMarkersForNode(
this, p, states);
252bool QSvgPolygon::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
257QSvgPolyline::QSvgPolyline(QSvgNode *parent,
const QPolygonF &poly)
258 : QSvgNode(parent), m_poly(poly)
263QSvgPolyline::~QSvgPolyline()
266void QSvgPolyline::drawCommand(QPainter *p, QSvgExtraStates &states)
268 if (p->brush().style() != Qt::NoBrush) {
269 p->drawPolygon(m_poly, states.fillRule);
271 if (m_poly.boundingRect().isNull() && p->pen().capStyle() != Qt::FlatCap)
272 p->drawPoint(m_poly.first());
274 p->drawPolyline(m_poly);
275 QSvgMarker::drawMarkersForNode(
this, p, states);
279bool QSvgPolyline::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
284QSvgRect::QSvgRect(QSvgNode *node,
const QRectF &rect, qreal rx, qreal ry)
286 m_rect(rect), m_rx(rx), m_ry(ry)
293QRectF QSvgRect::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
295 return p->transform().mapRect(m_rect);
298QRectF QSvgRect::internalBounds(QPainter *p, QSvgExtraStates &s)
const
300 return internalBounds(p, s, BoundsMode::Simplistic);
303QRectF QSvgRect::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
305 return filterRegion(s.doc(), internalBounds(p, s, BoundsMode::IncludeMiterLimit));
308QRectF QSvgRect::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
310 qreal sw = strokeWidth(p);
311 if (qFuzzyIsNull(sw)) {
312 return p->transform().mapRect(m_rect);
315 path.addRect(m_rect);
316 return boundsOnStroke(p, path, sw, mode);
320void QSvgRect::drawCommand(QPainter *p, QSvgExtraStates &)
323 p->drawRoundedRect(m_rect, m_rx, m_ry, Qt::RelativeSize);
328bool QSvgRect::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
333QSvgTspan *
const QSvgText::LINEBREAK = 0;
335QSvgText::QSvgText(QSvgNode *parent,
const QPointF &coord)
346 for (
int i = 0; i < m_tspans.size(); ++i) {
347 if (m_tspans[i] != LINEBREAK)
352void QSvgText::setTextArea(
const QSizeF &size)
358QRectF QSvgText::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
360 QSvgFontStyle *fontProperty =
static_cast<QSvgFontStyle *>(m_style.property(QSvgStyleProperty::Font));
361 QFont font = fontProperty ? fontProperty->qfont() : p->font();
362 QFontMetricsF fm(font);
365 for (
int i = 0; i < m_tspans.size(); ++i) {
366 if (m_tspans.at(i) != LINEBREAK)
367 charCount += m_tspans.at(i)->text().size();
370 QRectF approxMaximumBrect(m_coord.x(),
372 charCount * fm.averageCharWidth(),
373 -m_tspans.size() * fm.height());
374 return p->transform().mapRect(approxMaximumBrect);
377QRectF QSvgText::internalBounds(QPainter *p, QSvgExtraStates &states)
const
380 if (shouldDrawNode(p, states))
381 draw_helper(p, states, &boundingRect);
382 return p->transform().mapRect(boundingRect);
385void QSvgText::drawCommand(QPainter *p, QSvgExtraStates &states)
387 draw_helper(p, states);
390bool QSvgText::shouldDrawNode(QPainter *p, QSvgExtraStates &)
const
392 qsizetype numChars = 0;
393 qreal originalFontSize = p->font().pointSizeF();
394 qreal maxFontSize = originalFontSize;
395 for (
const QSvgTspan *span : std::as_const(m_tspans)) {
396 if (span == LINEBREAK)
399 numChars += span->text().size();
401 QSvgFontStyle *style =
static_cast<QSvgFontStyle *>(span->styleProperty(QSvgStyleProperty::Font));
402 if (style !=
nullptr && style->qfont().pointSizeF() > maxFontSize)
403 maxFontSize = style->qfont().pointSizeF();
406 QFont font = p->font();
407 font.setPixelSize(QSvgFontStyle::pixelSize(maxFontSize));
408 QFontMetricsF fm(font);
410 qCWarning(lcSvgDraw) <<
"Text element too high to lay out, ignoring";
415 qCWarning(lcSvgDraw) <<
"Text element too wide to lay out, ignoring";
422bool QSvgText::separateFillStroke(
const QPainter *,
const QSvgExtraStates &)
const
427void QSvgText::draw_helper(QPainter *p, QSvgExtraStates &states, QRectF *boundingRect)
const
429 const bool isPainting = (boundingRect ==
nullptr);
430 if (!isPainting || shouldDrawNode(p, states)) {
431 QFont font = p->font();
432 Qt::Alignment alignment = states.textAnchor;
436 qreal px = m_coord.x();
437 qreal py = m_coord.y();
439 if (m_type == Textarea) {
440 if (alignment == Qt::AlignHCenter)
441 px += m_size.width() / 2;
442 else if (alignment == Qt::AlignRight)
443 px += m_size.width();
447 if (m_size.height() != 0)
448 bounds = QRectF(0, py, 1, m_size.height());
450 bool appendSpace =
false;
451 QStringList paragraphs;
452 QList<QList<QTextLayout::FormatRange> > formatRanges(1);
455 QList<std::optional<QPointF>> paragraphPos;
456 paragraphs.push_back(QString());
457 paragraphPos.push_back(std::nullopt);
459 for (
int i = 0; i < m_tspans.size(); ++i) {
460 if (m_tspans[i] == LINEBREAK) {
464 if (m_type == Textarea && paragraphs.back().isEmpty()) {
465 applySvgFontPixelSize(font);
466 font.setHintingPreference(QFont::PreferNoHinting);
468 QTextLayout::FormatRange range;
471 range.format.setFont(font);
472 formatRanges.back().append(range);
474 paragraphs.back().append(QLatin1Char(
' '));;
477 paragraphs.push_back(QString());
478 formatRanges.resize(formatRanges.size() + 1);
479 paragraphPos.push_back(std::nullopt);
484 if (m_tspans[i]->isTspan() && m_tspans[i]->hasPosition()) {
485 if (!paragraphs.back().isEmpty()) {
486 paragraphs.push_back(QString());
487 formatRanges.resize(formatRanges.size() + 1);
488 paragraphPos.push_back(std::nullopt);
491 paragraphPos.back() = QPointF(m_tspans[i]->hasX() ? m_tspans[i]->x() : m_coord.x(),
492 m_tspans[i]->hasY() ? m_tspans[i]->y() : m_coord.y());
494 WhitespaceMode mode = m_tspans[i]->whitespaceMode();
495 m_tspans[i]->applyStyle(p, states);
498 applySvgFontPixelSize(font);
499 font.setHintingPreference(QFont::PreferNoHinting);
501 QString newText(m_tspans[i]->text());
502 newText.replace(QLatin1Char(
'\t'), QLatin1Char(
' '));
503 newText.replace(QLatin1Char(
'\n'), QLatin1Char(
' '));
505 bool prependSpace = !appendSpace && !m_tspans[i]->isTspan() && (mode == Default) && !paragraphs.back().isEmpty() && newText.startsWith(QLatin1Char(
' '));
506 if (appendSpace || prependSpace)
507 paragraphs.back().append(QLatin1Char(
' '));
509 bool appendSpaceNext = (!m_tspans[i]->isTspan() && (mode == Default) && newText.endsWith(QLatin1Char(
' ')));
511 if (mode == Default) {
512 newText = newText.simplified();
513 if (newText.isEmpty())
514 appendSpaceNext =
false;
517 QTextLayout::FormatRange range;
518 range.start = paragraphs.back().size();
519 range.length = newText.size();
520 range.format.setFont(font);
521 if (p->pen().style() != Qt::NoPen && p->pen().brush() != Qt::NoBrush)
522 range.format.setTextOutline(p->pen());
524 range.format.setForeground(p->brush().style() == Qt::NoBrush
525 ? QColor(Qt::transparent) : p->brush());
528 Q_ASSERT(!formatRanges.back().isEmpty());
529 ++formatRanges.back().back().length;
530 }
else if (prependSpace) {
534 formatRanges.back().append(range);
536 appendSpace = appendSpaceNext;
537 paragraphs.back() += newText;
539 m_tspans[i]->revertStyle(p, states);
543 if (states.svgFont) {
546 m_glyphsToDraw = states.svgFont->toGlyphs(paragraphs.join(QLatin1Char(
'\n')));
548 states.svgFont->draw(p, m_coord, m_glyphsToDraw.value(),
549 p->font().pointSizeF(), states.textAnchor);
551 *boundingRect = states.svgFont->boundingRect(p, m_coord, m_glyphsToDraw.value(),
552 p->font().pointSizeF(), states.textAnchor);
556 for (
int i = 0; i < paragraphs.size(); ++i) {
557 QTextLayout tl(paragraphs[i]);
558 QTextOption op = tl.textOption();
559 op.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
560 tl.setTextOption(op);
561 tl.setFormats(formatRanges[i]);
565 QTextLine line = tl.createLine();
568 if (m_size.width() != 0)
569 line.setLineWidth(m_size.width());
575 const bool positioned = paragraphPos[i].has_value();
576 const qreal originX = positioned ? paragraphPos[i]->x() : px;
577 const qreal originY = positioned ? paragraphPos[i]->y() : py;
580 bool endOfBoundsReached =
false;
581 for (
int j = 0; j < tl.lineCount(); ++j) {
582 QTextLine line = tl.lineAt(j);
585 if (alignment == Qt::AlignHCenter)
586 x -= 0.5 * line.naturalTextWidth();
587 else if (alignment == Qt::AlignRight)
588 x -= line.naturalTextWidth();
594 localY -= line.ascent();
595 line.setPosition(QPointF(x, localY));
596 brect |= line.naturalTextRect().translated(originX, originY);
597 localY += 1.1 * line.height();
601 if (initial && m_type == Text)
605 line.setPosition(QPointF(x, y));
606 brect |= line.naturalTextRect().translated(originX, originY);
609 if ((m_size.width() != 0 && line.naturalTextWidth() > m_size.width())
610 || (m_size.height() != 0 && y + line.height() > m_size.height())) {
613 bounds.setHeight(y - 1);
614 endOfBoundsReached =
true;
618 y += 1.1 * line.height();
621 tl.draw(p, QPointF(originX, originY), QList<QTextLayout::FormatRange>(), bounds);
623 if (endOfBoundsReached)
627 if (bounds.height() > 0)
628 brect.setBottom(qMin(brect.bottom(), bounds.bottom()));
629 *boundingRect = brect;
635void QSvgText::addText(QStringView text)
637 m_tspans.append(
new QSvgTspan(
this,
false));
638 m_tspans.back()->setWhitespaceMode(m_mode);
639 m_tspans.back()->addText(text);
642QSvgTspan::~QSvgTspan()
656 if (Q_UNLIKELY(!m_link || isDescendantOf(m_link) || m_recursing))
659 Q_ASSERT(states.nestedUseCount == 0 || states.nestedUseLevel > 0);
660 if (states.nestedUseLevel > 3 && states.nestedUseCount > (256 + states.nestedUseLevel * 2)) {
665 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
667 if (!m_start.isNull()) {
668 p->translate(m_start);
670 if (states.nestedUseLevel > 0)
671 ++states.nestedUseCount;
673 QScopedValueRollback<
int> useLevelGuard(states.nestedUseLevel, states.nestedUseLevel + 1);
674 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
675 m_link->draw(p, states);
677 if (states.nestedUseLevel == 0)
678 states.nestedUseCount = 0;
680 if (!m_start.isNull()) {
681 p->translate(-m_start);
685QSvgNode::Type QSvgDummyNode::type()
const
687 return FeUnsupported;
691QSvgCircle::~QSvgCircle()
694QSvgNode::Type QSvgCircle::type()
const
699QSvgNode::Type QSvgEllipse::type()
const
704QSvgNode::Type QSvgImage::type()
const
709QSvgNode::Type QSvgLine::type()
const
714QSvgNode::Type QSvgPath::type()
const
719QSvgNode::Type QSvgPolygon::type()
const
724QSvgNode::Type QSvgPolyline::type()
const
729QSvgNode::Type QSvgRect::type()
const
734QSvgNode::Type QSvgText::type()
const
755 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
756 QScopedValueRollback<
bool> guard(m_recursing,
true);
757 p->translate(m_start);
758 bounds = m_link->bounds(p, states);
759 p->translate(-m_start);
767 if (Q_LIKELY(m_link && !isDescendantOf(m_link) && !m_recursing)) {
768 QScopedValueRollback<
bool> guard(m_recursing,
true);
769 p->translate(m_start);
770 bounds = m_link->decoratedBounds(p, states);
771 p->translate(-m_start);
776QRectF QSvgPolyline::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
778 return p->transform().mapRect(m_poly.boundingRect());
781QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &s)
const
783 return internalBounds(p, s, BoundsMode::Simplistic);
786QRectF QSvgPolyline::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
788 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
789 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
790 return filterRegion(s.doc(), rect);
793bool QSvgPolyline::requiresGroupRendering()
const
795 return hasAnyMarker();
798QRectF QSvgPolyline::internalBounds(QPainter *p, QSvgExtraStates &, BoundsMode mode)
const
800 qreal sw = strokeWidth(p);
801 if (qFuzzyIsNull(sw)) {
802 return p->transform().map(m_poly).boundingRect();
805 path.addPolygon(m_poly);
806 return boundsOnStroke(p, path, sw, mode);
810QRectF QSvgImage::internalBounds(QPainter *p, QSvgExtraStates &)
const
812 return p->transform().mapRect(m_bounds);
815QRectF QSvgLine::internalFastBounds(QPainter *p, QSvgExtraStates &)
const
817 QPointF p1 = p->transform().map(m_line.p1());
818 QPointF p2 = p->transform().map(m_line.p2());
819 qreal minX = qMin(p1.x(), p2.x());
820 qreal minY = qMin(p1.y(), p2.y());
821 qreal maxX = qMax(p1.x(), p2.x());
822 qreal maxY = qMax(p1.y(), p2.y());
823 return QRectF(minX, minY, maxX - minX, maxY - minY);
826QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s)
const
828 return internalBounds(p, s, BoundsMode::Simplistic);
831QRectF QSvgLine::decoratedInternalBounds(QPainter *p, QSvgExtraStates &s)
const
833 QRectF rect = internalBounds(p, s, BoundsMode::IncludeMiterLimit);
834 rect |= QSvgMarker::markersBoundsForNode(
this, p, s);
835 return filterRegion(s.doc(), rect);
838bool QSvgLine::requiresGroupRendering()
const
840 return hasAnyMarker();
843QRectF QSvgLine::internalBounds(QPainter *p, QSvgExtraStates &s, BoundsMode mode)
const
845 qreal sw = strokeWidth(p);
846 if (qFuzzyIsNull(sw)) {
847 return internalFastBounds(p, s);
850 path.moveTo(m_line.p1());
851 path.lineTo(m_line.p2());
852 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)
static void applySvgFontPixelSize(QFont &font)
#define QT_SVG_MAX_LAYOUT_SIZE