39void QSvgNode::draw(QPainter *p, QSvgExtraStates &states)
42 QElapsedTimer qtSvgTimer; qtSvgTimer.start();
45 if (shouldDrawNode(p, states)) {
46 applyStyle(p, states);
47 applyAnimatedStyle(p, states);
48 QSvgNode *maskNode =
this->hasMask() ? document()->namedNode(
this->maskId()) :
nullptr;
49 QSvgFilterContainer *filterNode =
this->hasFilter() ?
static_cast<QSvgFilterContainer*>(document()->namedNode(
this->filterId()))
51 if (filterNode && filterNode->type() == QSvgNode::Filter && filterNode->supported()) {
52 QTransform xf = p->transform();
54 QRectF localRect = internalBounds(p, states);
56 QRectF boundsRect = xf.mapRect(filterNode->filterRegion(localRect));
57 QImage proxy = drawIntoBuffer(p, states, boundsRect.toRect());
58 proxy = filterNode->applyFilter(proxy, p, localRect);
59 if (maskNode && maskNode->type() == QSvgNode::Mask) {
60 boundsRect = QRectF(proxy.offset(), proxy.size());
61 localRect = p->transform().inverted().mapRect(boundsRect);
62 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states, localRect, &boundsRect);
63 applyMaskToBuffer(&proxy, mask);
65 applyBufferToCanvas(p, proxy);
67 }
else if (maskNode && maskNode->type() == QSvgNode::Mask) {
69 QImage mask =
static_cast<QSvgMask*>(maskNode)->createMask(p, states,
this, &boundsRect);
70 drawWithMask(p, states, mask, boundsRect.toRect());
71 }
else if (!qFuzzyCompare(p->opacity(), qreal(1.0)) && requiresGroupRendering()) {
72 QTransform xf = p->transform();
75 QRectF localRect = decoratedInternalBounds(p, states);
77 QRectF boundsRect = xf.mapRect(localRect);
78 const int deltaX = boundsRect.width() * 0.1;
79 const int deltaY = boundsRect.height() * 0.1;
80 boundsRect = boundsRect.adjusted(-deltaX, -deltaY, deltaX, deltaY);
84 QImage proxy = drawIntoBuffer(p, states, boundsRect.toAlignedRect());
85 applyBufferToCanvas(p, proxy);
87 if (separateFillStroke(states))
88 fillThenStroke(p, states);
90 drawCommand(p, states);
93 revertAnimatedStyle(p ,states);
94 revertStyle(p, states);
98 if (Q_UNLIKELY(lcSvgTiming().isDebugEnabled()))
99 qCDebug(lcSvgTiming) <<
"Drawing" << typeName() <<
"took" << (qtSvgTimer.nsecsElapsed() / 1000000.0f) <<
"ms";
103void QSvgNode::fillThenStroke(QPainter *p, QSvgExtraStates &states)
105 qreal oldOpacity = p->opacity();
106 if (p->brush().style() != Qt::NoBrush) {
107 QPen oldPen = p->pen();
108 p->setPen(Qt::NoPen);
109 p->setOpacity(oldOpacity * states.fillOpacity);
111 drawCommand(p, states);
115 if (p->pen() != Qt::NoPen && p->pen().brush() != Qt::NoBrush && p->pen().widthF() != 0) {
116 QBrush oldBrush = p->brush();
117 p->setOpacity(oldOpacity * states.strokeOpacity);
118 p->setBrush(Qt::NoBrush);
120 drawCommand(p, states);
122 p->setBrush(oldBrush);
124 p->setOpacity(oldOpacity);
127void QSvgNode::drawWithMask(QPainter *p, QSvgExtraStates &states,
const QImage &mask,
const QRect &boundsRect)
129 QImage proxy = drawIntoBuffer(p, states, boundsRect);
132 applyMaskToBuffer(&proxy, mask);
136 p->drawImage(boundsRect, proxy);
140QImage QSvgNode::drawIntoBuffer(QPainter *p, QSvgExtraStates &states,
const QRect &boundsRect)
143 if (!QImageIOHandler::allocateImage(boundsRect.size(), QImage::Format_ARGB32_Premultiplied, &proxy)) {
144 qCWarning(lcSvgDraw) <<
"The requested buffer size is too big, ignoring";
147 proxy.setOffset(boundsRect.topLeft());
148 proxy.fill(Qt::transparent);
149 QPainter proxyPainter(&proxy);
150 proxyPainter.setPen(p->pen());
151 proxyPainter.setBrush(p->brush());
152 proxyPainter.setFont(p->font());
153 proxyPainter.translate(-boundsRect.topLeft());
154 proxyPainter.setTransform(p->transform(),
true);
155 proxyPainter.setRenderHints(p->renderHints());
156 if (separateFillStroke(states))
157 fillThenStroke(&proxyPainter, states);
159 drawCommand(&proxyPainter, states);
163void QSvgNode::applyMaskToBuffer(QImage *proxy, QImage mask)
const
165 QPainter proxyPainter(proxy);
166 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
167 proxyPainter.resetTransform();
168 proxyPainter.drawImage(QRect(0, 0, mask.width(), mask.height()), mask);
190void QSvgNode::appendStyleProperty(QSvgStyleProperty *prop,
const QString &id)
193 QSvgTinyDocument *doc;
194 switch (prop->type()) {
195 case QSvgStyleProperty::QUALITY:
196 m_style.quality =
static_cast<QSvgQualityStyle*>(prop);
198 case QSvgStyleProperty::FILL:
199 m_style.fill =
static_cast<QSvgFillStyle*>(prop);
201 case QSvgStyleProperty::VIEWPORT_FILL:
202 m_style.viewportFill =
static_cast<QSvgViewportFillStyle*>(prop);
204 case QSvgStyleProperty::FONT:
205 m_style.font =
static_cast<QSvgFontStyle*>(prop);
207 case QSvgStyleProperty::STROKE:
208 m_style.stroke =
static_cast<QSvgStrokeStyle*>(prop);
210 case QSvgStyleProperty::SOLID_COLOR:
211 m_style.solidColor =
static_cast<QSvgSolidColorStyle*>(prop);
213 if (doc && !id.isEmpty())
214 doc->addNamedStyle(id, m_style.solidColor);
216 case QSvgStyleProperty::GRADIENT:
217 m_style.gradient =
static_cast<QSvgGradientStyle*>(prop);
219 if (doc && !id.isEmpty())
220 doc->addNamedStyle(id, m_style.gradient);
222 case QSvgStyleProperty::PATTERN:
223 m_style.pattern =
static_cast<QSvgPatternStyle*>(prop);
225 if (doc && !id.isEmpty())
226 doc->addNamedStyle(id, m_style.pattern);
228 case QSvgStyleProperty::TRANSFORM:
229 m_style.transform =
static_cast<QSvgTransformStyle*>(prop);
231 case QSvgStyleProperty::OPACITY:
232 m_style.opacity =
static_cast<QSvgOpacityStyle*>(prop);
234 case QSvgStyleProperty::COMP_OP:
235 m_style.compop =
static_cast<QSvgCompOpStyle*>(prop);
238 qDebug(
"QSvgNode: Trying to append unknown property!");
288QSvgStyleProperty * QSvgNode::styleProperty(QSvgStyleProperty::Type type)
const
290 const QSvgNode *node =
this;
293 case QSvgStyleProperty::QUALITY:
294 if (node->m_style.quality)
295 return node->m_style.quality;
297 case QSvgStyleProperty::FILL:
298 if (node->m_style.fill)
299 return node->m_style.fill;
301 case QSvgStyleProperty::VIEWPORT_FILL:
302 if (m_style.viewportFill)
303 return node->m_style.viewportFill;
305 case QSvgStyleProperty::FONT:
306 if (node->m_style.font)
307 return node->m_style.font;
309 case QSvgStyleProperty::STROKE:
310 if (node->m_style.stroke)
311 return node->m_style.stroke;
313 case QSvgStyleProperty::SOLID_COLOR:
314 if (node->m_style.solidColor)
315 return node->m_style.solidColor;
317 case QSvgStyleProperty::GRADIENT:
318 if (node->m_style.gradient)
319 return node->m_style.gradient;
321 case QSvgStyleProperty::PATTERN:
322 if (node->m_style.pattern)
323 return node->m_style.pattern;
325 case QSvgStyleProperty::TRANSFORM:
326 if (node->m_style.transform)
327 return node->m_style.transform;
329 case QSvgStyleProperty::OPACITY:
330 if (node->m_style.opacity)
331 return node->m_style.opacity;
333 case QSvgStyleProperty::COMP_OP:
334 if (node->m_style.compop)
335 return node->m_style.compop;
340 node = node->parent();
364QRectF QSvgNode::bounds()
const
366 if (!m_cachedBounds.isEmpty())
367 return m_cachedBounds;
369 QImage dummy(1, 1, QImage::Format_RGB32);
372 QSvgExtraStates states;
375 parent()->applyStyleRecursive(&p, states);
376 p.setWorldTransform(QTransform());
377 m_cachedBounds = bounds(&p, states);
379 parent()->revertStyleRecursive(&p, states);
380 return m_cachedBounds;
643void QSvgNode::initPainter(QPainter *p)
645 QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
646 pen.setMiterLimit(4);
648 p->setBrush(Qt::black);
649 p->setRenderHint(QPainter::Antialiasing);
650 p->setRenderHint(QPainter::SmoothPixmapTransform);
651 QFont font(p->font());
652 if (font.pointSize() < 0 && font.pixelSize() > 0) {
653 font.setPointSizeF(font.pixelSize() * 72.0 / p->device()->logicalDpiY());
658QRectF QSvgNode::boundsOnStroke(QPainter *p,
const QPainterPath &path,
659 qreal width, BoundsMode mode)
661 QPainterPathStroker stroker;
662 stroker.setWidth(width);
663 if (mode == BoundsMode::IncludeMiterLimit) {
664 stroker.setJoinStyle(p->pen().joinStyle());
665 stroker.setMiterLimit(p->pen().miterLimit());
667 QPainterPath stroke = stroker.createStroke(path);
668 return p->transform().map(stroke).boundingRect();