43void QSvgNode::draw(QPainter *p, QSvgExtraStates &states)
46 QElapsedTimer qtSvgTimer; qtSvgTimer.start();
49 if (shouldDrawNode(p, states)) {
50 quint8 remainingDepth = states.trustedSource ? states.remainingNestedNodes
51 : states.remainingNestedNodes - 1;
52 QScopedValueRollback<quint8> nestedNodesGuard(states.remainingNestedNodes, remainingDepth);
53 if (states.remainingNestedNodes == 0) {
54 qCWarning(lcSvgDraw).noquote() <<
"Too many nested nodes at" << typeName()
55 <<
"exceeding max nested limit of" << QtSvg::renderingMaxNestedNodes <<
"."
56 <<
"Enable AssumeTrustedSource in QSvgHandler or set QT_SVG_DEFAULT_OPTIONS=2 to disable this check.";
60 applyStyle(p, states);
61 applyAnimatedStyle(p, states);
62 QSvgNode *maskNode =
this->hasMask() ? states.doc()->namedNode(
this->maskId()) :
nullptr;
63 QSvgFilterContainer *filterNode =
this->hasFilter()
64 ?
static_cast<QSvgFilterContainer *>(states.doc()->namedNode(
this->filterId()))
66 if (filterNode && filterNode->type() == QSvgNode::Filter && filterNode->supported()) {
67 QTransform xf = p->transform();
69 QRectF localRect = internalBounds(p, states);
71 QRectF boundsRect = xf.mapRect(filterNode->filterRegion(localRect));
72 QImage proxy = drawIntoBuffer(p, states, boundsRect.toRect());
73 proxy = filterNode->applyFilter(proxy, p, localRect);
74 if (maskNode && maskNode->type() == QSvgNode::Mask) {
75 boundsRect = QRectF(proxy.offset(), proxy.size());
76 localRect = p->transform().inverted().mapRect(boundsRect);
77 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states, localRect, &boundsRect);
78 applyMaskToBuffer(&proxy, mask);
80 applyBufferToCanvas(p, proxy);
82 }
else if (maskNode && maskNode->type() == QSvgNode::Mask) {
84 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states,
this, &boundsRect);
85 drawWithMask(p, states, mask, boundsRect.toRect());
86 }
else if (!qFuzzyCompare(p->opacity(), qreal(1.0)) && requiresGroupRendering()) {
87 QTransform xf = p->transform();
90 QRectF localRect = decoratedInternalBounds(p, states);
92 QRectF boundsRect = xf.mapRect(localRect);
93 const int deltaX = boundsRect.width() * 0.1;
94 const int deltaY = boundsRect.height() * 0.1;
95 boundsRect = boundsRect.adjusted(-deltaX, -deltaY, deltaX, deltaY);
99 QImage proxy = drawIntoBuffer(p, states, boundsRect.toAlignedRect());
100 applyBufferToCanvas(p, proxy);
102 if (separateFillStroke(p, states))
103 fillThenStroke(p, states);
105 drawCommand(p, states);
108 revertAnimatedStyle(p ,states);
109 revertStyle(p, states);
113 if (Q_UNLIKELY(lcSvgTiming().isDebugEnabled()))
114 qCDebug(lcSvgTiming) <<
"Drawing" << typeName() <<
"took" << (qtSvgTimer.nsecsElapsed() / 1000000.0f) <<
"ms";
118void QSvgNode::fillThenStroke(QPainter *p, QSvgExtraStates &states)
120 qreal oldOpacity = p->opacity();
121 if (p->brush().style() != Qt::NoBrush) {
122 QPen oldPen = p->pen();
123 p->setPen(Qt::NoPen);
124 p->setOpacity(oldOpacity * states.fillOpacity);
126 drawCommand(p, states);
130 if (p->pen() != Qt::NoPen && p->pen().brush() != Qt::NoBrush && p->pen().widthF() != 0) {
131 QBrush oldBrush = p->brush();
132 p->setOpacity(oldOpacity * states.strokeOpacity);
133 p->setBrush(Qt::NoBrush);
135 drawCommand(p, states);
137 p->setBrush(oldBrush);
139 p->setOpacity(oldOpacity);
155QImage QSvgNode::drawIntoBuffer(QPainter *p, QSvgExtraStates &states,
const QRect &boundsRect)
158 if (!QImageIOHandler::allocateImage(boundsRect.size(), QImage::Format_ARGB32_Premultiplied, &proxy)) {
159 qCWarning(lcSvgDraw) <<
"The requested buffer size is too big, ignoring";
162 proxy.setOffset(boundsRect.topLeft());
163 proxy.fill(Qt::transparent);
164 QPainter proxyPainter(&proxy);
165 proxyPainter.setPen(p->pen());
166 proxyPainter.setBrush(p->brush());
167 proxyPainter.setFont(p->font());
168 proxyPainter.translate(-boundsRect.topLeft());
169 proxyPainter.setTransform(p->transform(),
true);
170 proxyPainter.setRenderHints(p->renderHints());
171 if (separateFillStroke(p, states))
172 fillThenStroke(&proxyPainter, states);
174 drawCommand(&proxyPainter, states);
287QRectF QSvgNode::bounds(
const QSvgDocument *doc)
const
289 if (!m_cachedBounds.isEmpty())
290 return m_cachedBounds;
292 QImage dummy(1, 1, QImage::Format_RGB32);
295 QSvgExtraStates states(doc);
298 parent()->applyStyleRecursive(&p, states);
299 p.setWorldTransform(QTransform());
300 m_cachedBounds = bounds(&p, states);
302 parent()->revertStyleRecursive(&p, states);
303 return m_cachedBounds;
543void QSvgNode::initPainter(QPainter *p)
545 QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
546 pen.setMiterLimit(4);
548 p->setBrush(Qt::black);
549 p->setRenderHint(QPainter::Antialiasing);
550 p->setRenderHint(QPainter::SmoothPixmapTransform);
551 QFont font(p->font());
552 if (font.pointSize() < 0 && font.pixelSize() > 0) {
553 font.setPointSizeF(font.pixelSize() * 72.0 / p->device()->logicalDpiY());
558QRectF QSvgNode::boundsOnStroke(QPainter *p,
const QPainterPath &path,
559 qreal width, BoundsMode mode)
561 QPainterPathStroker stroker;
562 stroker.setWidth(width);
563 if (mode == BoundsMode::IncludeMiterLimit) {
564 stroker.setJoinStyle(p->pen().joinStyle());
565 stroker.setMiterLimit(p->pen().miterLimit());
567 QPainterPath stroke = stroker.createStroke(path);
568 return p->transform().map(stroke).boundingRect();