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) <<
"Too many nested nodes at" <<
qPrintable(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() ? document()->namedNode(
this->maskId()) :
nullptr;
63 QSvgFilterContainer *filterNode =
this->hasFilter() ?
static_cast<QSvgFilterContainer*>(document()->namedNode(
this->filterId()))
65 if (filterNode && filterNode->type() == QSvgNode::Filter && filterNode->supported()) {
66 QTransform xf = p->transform();
68 QRectF localRect = internalBounds(p, states);
70 QRectF boundsRect = xf.mapRect(filterNode->filterRegion(localRect));
71 QImage proxy = drawIntoBuffer(p, states, boundsRect.toRect());
72 proxy = filterNode->applyFilter(proxy, p, localRect);
73 if (maskNode && maskNode->type() == QSvgNode::Mask) {
74 boundsRect = QRectF(proxy.offset(), proxy.size());
75 localRect = p->transform().inverted().mapRect(boundsRect);
76 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states, localRect, &boundsRect);
77 applyMaskToBuffer(&proxy, mask);
79 applyBufferToCanvas(p, proxy);
81 }
else if (maskNode && maskNode->type() == QSvgNode::Mask) {
83 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states,
this, &boundsRect);
84 drawWithMask(p, states, mask, boundsRect.toRect());
85 }
else if (!qFuzzyCompare(p->opacity(), qreal(1.0)) && requiresGroupRendering()) {
86 QTransform xf = p->transform();
89 QRectF localRect = decoratedInternalBounds(p, states);
91 QRectF boundsRect = xf.mapRect(localRect);
92 const int deltaX = boundsRect.width() * 0.1;
93 const int deltaY = boundsRect.height() * 0.1;
94 boundsRect = boundsRect.adjusted(-deltaX, -deltaY, deltaX, deltaY);
98 QImage proxy = drawIntoBuffer(p, states, boundsRect.toAlignedRect());
99 applyBufferToCanvas(p, proxy);
101 if (separateFillStroke(p, states))
102 fillThenStroke(p, states);
104 drawCommand(p, states);
107 revertAnimatedStyle(p ,states);
108 revertStyle(p, states);
112 if (Q_UNLIKELY(lcSvgTiming().isDebugEnabled()))
113 qCDebug(lcSvgTiming) <<
"Drawing" << typeName() <<
"took" << (qtSvgTimer.nsecsElapsed() / 1000000.0f) <<
"ms";
117void QSvgNode::fillThenStroke(QPainter *p, QSvgExtraStates &states)
119 qreal oldOpacity = p->opacity();
120 if (p->brush().style() != Qt::NoBrush) {
121 QPen oldPen = p->pen();
122 p->setPen(Qt::NoPen);
123 p->setOpacity(oldOpacity * states.fillOpacity);
125 drawCommand(p, states);
129 if (p->pen() != Qt::NoPen && p->pen().brush() != Qt::NoBrush && p->pen().widthF() != 0) {
130 QBrush oldBrush = p->brush();
131 p->setOpacity(oldOpacity * states.strokeOpacity);
132 p->setBrush(Qt::NoBrush);
134 drawCommand(p, states);
136 p->setBrush(oldBrush);
138 p->setOpacity(oldOpacity);
154QImage QSvgNode::drawIntoBuffer(QPainter *p, QSvgExtraStates &states,
const QRect &boundsRect)
157 if (!QImageIOHandler::allocateImage(boundsRect.size(), QImage::Format_ARGB32_Premultiplied, &proxy)) {
158 qCWarning(lcSvgDraw) <<
"The requested buffer size is too big, ignoring";
161 proxy.setOffset(boundsRect.topLeft());
162 proxy.fill(Qt::transparent);
163 QPainter proxyPainter(&proxy);
164 proxyPainter.setPen(p->pen());
165 proxyPainter.setBrush(p->brush());
166 proxyPainter.setFont(p->font());
167 proxyPainter.translate(-boundsRect.topLeft());
168 proxyPainter.setTransform(p->transform(),
true);
169 proxyPainter.setRenderHints(p->renderHints());
170 if (separateFillStroke(p, states))
171 fillThenStroke(&proxyPainter, states);
173 drawCommand(&proxyPainter, states);
286QRectF QSvgNode::bounds()
const
288 if (!m_cachedBounds.isEmpty())
289 return m_cachedBounds;
291 QImage dummy(1, 1, QImage::Format_RGB32);
294 QSvgExtraStates states;
297 parent()->applyStyleRecursive(&p, states);
298 p.setWorldTransform(QTransform());
299 m_cachedBounds = bounds(&p, states);
301 parent()->revertStyleRecursive(&p, states);
302 return m_cachedBounds;
553void QSvgNode::initPainter(QPainter *p)
555 QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
556 pen.setMiterLimit(4);
558 p->setBrush(Qt::black);
559 p->setRenderHint(QPainter::Antialiasing);
560 p->setRenderHint(QPainter::SmoothPixmapTransform);
561 QFont font(p->font());
562 if (font.pointSize() < 0 && font.pixelSize() > 0) {
563 font.setPointSizeF(font.pixelSize() * 72.0 / p->device()->logicalDpiY());
568QRectF QSvgNode::boundsOnStroke(QPainter *p,
const QPainterPath &path,
569 qreal width, BoundsMode mode)
571 QPainterPathStroker stroker;
572 stroker.setWidth(width);
573 if (mode == BoundsMode::IncludeMiterLimit) {
574 stroker.setJoinStyle(p->pen().joinStyle());
575 stroker.setMiterLimit(p->pen().miterLimit());
577 QPainterPath stroke = stroker.createStroke(path);
578 return p->transform().map(stroke).boundingRect();