45void QSvgNode::draw(QPainter *p, QSvgExtraStates &states)
48 QElapsedTimer qtSvgTimer; qtSvgTimer.start();
51 if (shouldDrawNode(p, states)) {
52 quint8 remainingDepth = states.trustedSource ? states.remainingNestedNodes
53 : states.remainingNestedNodes - 1;
54 QScopedValueRollback<quint8> nestedNodesGuard(states.remainingNestedNodes, remainingDepth);
55 if (states.remainingNestedNodes == 0) {
56 qCWarning(lcSvgDraw).noquote() <<
"Too many nested nodes at" << typeName()
57 <<
"exceeding max nested limit of" << QtSvg::renderingMaxNestedNodes <<
"."
58 <<
"Enable AssumeTrustedSource in QSvgHandler or set QT_SVG_DEFAULT_OPTIONS=2 to disable this check.";
62 applyStyle(p, states);
63 applyAnimatedStyle(p, states);
64 QSvgNode *maskNode =
this->hasMask() ? document()->namedNode(
this->maskId()) :
nullptr;
65 QSvgFilterContainer *filterNode =
this->hasFilter() ?
static_cast<QSvgFilterContainer*>(document()->namedNode(
this->filterId()))
67 if (filterNode && filterNode->type() == QSvgNode::Filter && filterNode->supported()) {
68 QTransform xf = p->transform();
70 QRectF localRect = internalBounds(p, states);
72 QRectF boundsRect = xf.mapRect(filterNode->filterRegion(localRect));
73 QImage proxy = drawIntoBuffer(p, states, boundsRect.toRect());
74 proxy = filterNode->applyFilter(proxy, p, localRect);
75 if (maskNode && maskNode->type() == QSvgNode::Mask) {
76 boundsRect = QRectF(proxy.offset(), proxy.size());
77 localRect = p->transform().inverted().mapRect(boundsRect);
78 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states, localRect, &boundsRect);
79 applyMaskToBuffer(&proxy, mask);
81 applyBufferToCanvas(p, proxy);
83 }
else if (maskNode && maskNode->type() == QSvgNode::Mask) {
85 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states,
this, &boundsRect);
86 drawWithMask(p, states, mask, boundsRect.toRect());
87 }
else if (!qFuzzyCompare(p->opacity(), qreal(1.0)) && requiresGroupRendering()) {
88 QTransform xf = p->transform();
91 QRectF localRect = decoratedInternalBounds(p, states);
93 QRectF boundsRect = xf.mapRect(localRect);
94 const int deltaX = boundsRect.width() * 0.1;
95 const int deltaY = boundsRect.height() * 0.1;
96 boundsRect = boundsRect.adjusted(-deltaX, -deltaY, deltaX, deltaY);
100 QImage proxy = drawIntoBuffer(p, states, boundsRect.toAlignedRect());
101 applyBufferToCanvas(p, proxy);
103 if (separateFillStroke(p, states))
104 fillThenStroke(p, states);
106 drawCommand(p, states);
109 revertAnimatedStyle(p ,states);
110 revertStyle(p, states);
114 if (Q_UNLIKELY(lcSvgTiming().isDebugEnabled()))
115 qCDebug(lcSvgTiming) <<
"Drawing" << typeName() <<
"took" << (qtSvgTimer.nsecsElapsed() / 1000000.0f) <<
"ms";
119void QSvgNode::fillThenStroke(QPainter *p, QSvgExtraStates &states)
121 qreal oldOpacity = p->opacity();
122 if (p->brush().style() != Qt::NoBrush) {
123 QPen oldPen = p->pen();
124 p->setPen(Qt::NoPen);
125 p->setOpacity(oldOpacity * states.fillOpacity);
127 drawCommand(p, states);
131 if (p->pen() != Qt::NoPen && p->pen().brush() != Qt::NoBrush && p->pen().widthF() != 0) {
132 QBrush oldBrush = p->brush();
133 p->setOpacity(oldOpacity * states.strokeOpacity);
134 p->setBrush(Qt::NoBrush);
136 drawCommand(p, states);
138 p->setBrush(oldBrush);
140 p->setOpacity(oldOpacity);
156QImage QSvgNode::drawIntoBuffer(QPainter *p, QSvgExtraStates &states,
const QRect &boundsRect)
159 if (!QImageIOHandler::allocateImage(boundsRect.size(), QImage::Format_ARGB32_Premultiplied, &proxy)) {
160 qCWarning(lcSvgDraw) <<
"The requested buffer size is too big, ignoring";
163 proxy.setOffset(boundsRect.topLeft());
164 proxy.fill(Qt::transparent);
165 QPainter proxyPainter(&proxy);
166 proxyPainter.setPen(p->pen());
167 proxyPainter.setBrush(p->brush());
168 proxyPainter.setFont(p->font());
169 proxyPainter.translate(-boundsRect.topLeft());
170 proxyPainter.setTransform(p->transform(),
true);
171 proxyPainter.setRenderHints(p->renderHints());
172 if (separateFillStroke(p, states))
173 fillThenStroke(&proxyPainter, states);
175 drawCommand(&proxyPainter, states);
288QRectF QSvgNode::bounds()
const
290 if (!m_cachedBounds.isEmpty())
291 return m_cachedBounds;
293 QImage dummy(1, 1, QImage::Format_RGB32);
296 QSvgExtraStates states;
299 parent()->applyStyleRecursive(&p, states);
300 p.setWorldTransform(QTransform());
301 m_cachedBounds = bounds(&p, states);
303 parent()->revertStyleRecursive(&p, states);
304 return m_cachedBounds;
555void QSvgNode::initPainter(QPainter *p)
557 QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
558 pen.setMiterLimit(4);
560 p->setBrush(Qt::black);
561 p->setRenderHint(QPainter::Antialiasing);
562 p->setRenderHint(QPainter::SmoothPixmapTransform);
563 QFont font(p->font());
564 if (font.pointSize() < 0 && font.pixelSize() > 0) {
565 font.setPointSizeF(font.pixelSize() * 72.0 / p->device()->logicalDpiY());
570QRectF QSvgNode::boundsOnStroke(QPainter *p,
const QPainterPath &path,
571 qreal width, BoundsMode mode)
573 QPainterPathStroker stroker;
574 stroker.setWidth(width);
575 if (mode == BoundsMode::IncludeMiterLimit) {
576 stroker.setJoinStyle(p->pen().joinStyle());
577 stroker.setMiterLimit(p->pen().miterLimit());
579 QPainterPath stroke = stroker.createStroke(path);
580 return p->transform().map(stroke).boundingRect();