Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsvgstructure.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
8
9#include "qsvgstyle_p.h"
10#include "qsvgdocument_p.h"
11#include "qsvggraphics_p.h"
12#include "qsvgstyle_p.h"
13#include "qsvgfilter_p.h"
14
15#include "qpainter.h"
16#include "qlocale.h"
17#include "qdebug.h"
18
19#include <QLoggingCategory>
20#include <qscopedvaluerollback.h>
21#include <QtGui/qimageiohandler.h>
22
23#include <QtCore/qlatin1stringview.h>
24#include <QtCore/qstringview.h>
25#include <QtCore/private/qoffsetstringarray_p.h>
26
28
29using namespace Qt::StringLiterals;
30
31QSvgG::QSvgG(QSvgNode *parent)
32 : QSvgStructureNode(parent)
33{
34
35}
36
37QSvgG::~QSvgG()
38 = default;
39
40void QSvgG::drawCommand(QPainter *p, QSvgExtraStates &states)
41{
42 for (const auto &node : renderers()) {
43 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
44 node->draw(p, states);
45 }
46}
47
48bool QSvgG::shouldDrawNode(QPainter *, QSvgExtraStates &) const
49{
50 return true;
51}
52
53QSvgNode::Type QSvgG::type() const
54{
55 return Group;
56}
57
58bool QSvgG::requiresGroupRendering() const
59{
60 return m_renderers.size() > 1;
61}
62
63QSvgStructureNode::QSvgStructureNode(QSvgNode *parent)
64 :QSvgNode(parent)
65{
66
67}
68
69QSvgStructureNode::~QSvgStructureNode()
70 = default;
71
72void QSvgStructureNode::addChild(std::unique_ptr<QSvgNode> child, const QString &id)
73{
74 if (!id.isEmpty()) {
75 QSvgDocument *doc = document();
76 if (doc)
77 doc->addNamedNode(id, child.get());
78 }
79
80 m_renderers.push_back(std::move(child));
81}
82
83QSvgDefs::QSvgDefs(QSvgNode *parent)
84 : QSvgStructureNode(parent)
85{
86}
87
88QSvgDefs::~QSvgDefs()
89 = default;
90
91bool QSvgDefs::shouldDrawNode(QPainter *, QSvgExtraStates &) const
92{
93 return false;
94}
95
96QSvgNode::Type QSvgDefs::type() const
97{
98 return Defs;
99}
100
101QSvgSymbolLike::QSvgSymbolLike(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
102 QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow)
103 : QSvgStructureNode(parent)
104 , m_rect(bounds)
105 , m_viewBox(viewBox)
106 , m_refP(refP)
107 , m_pAspectRatios(pAspectRatios)
108 , m_overflow(overflow)
109{
110
111}
112
113QSvgSymbolLike::~QSvgSymbolLike()
114 = default;
115
116QRectF QSvgSymbolLike::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states) const
117{
118 p->save();
119 setPainterToRectAndAdjustment(p);
120 QRectF rect = internalBounds(p, states);
121 p->restore();
122 return rect;
123}
124
125bool QSvgSymbolLike::requiresGroupRendering() const
126{
127 return m_renderers.size() > 1;
128}
129
130QRectF QSvgSymbolLike::clipRect() const
131{
132 if (m_overflow != Overflow::Hidden || !m_viewBox.isValid())
133 return QRectF{};
134
135 qreal scaleX = 1.0;
136 if (m_rect.width() > 0 && m_viewBox.width() > 0)
137 scaleX = m_rect.width() / m_viewBox.width();
138 qreal scaleY = 1.0;
139 if (m_rect.height() > 0 && m_viewBox.height() > 0)
140 scaleY = m_rect.height() / m_viewBox.height();
141
142 QTransform t;
143 t.translate(- m_refP.x() * scaleX - m_rect.left() - m_viewBox.left() * scaleX,
144 - m_refP.y() * scaleY - m_rect.top() - m_viewBox.top() * scaleY);
145 t.scale(scaleX, scaleY);
146
147 return t.mapRect(m_viewBox);
148}
149
150QTransform QSvgSymbolLike::aspectRatioTransform() const
151{
152 QTransform xform;
153
154 qreal offsetX = 0.0;
155 qreal offsetY = 0.0;
156
157 qreal scaleX = 1.0;
158 if (m_rect.width() > 0 && m_viewBox.width() > 0)
159 scaleX = m_rect.width() / m_viewBox.width();
160 qreal scaleY = 1.0;
161 if (m_rect.height() > 0 && m_viewBox.height() > 0)
162 scaleY = m_rect.height() / m_viewBox.height();
163
164 if (!qFuzzyCompare(scaleX, scaleY) &&
165 m_pAspectRatios.testAnyFlag(PreserveAspectRatio::xyMask)) {
166
167 if (m_pAspectRatios.testAnyFlag(PreserveAspectRatio::meet))
168 scaleX = scaleY = qMin(scaleX, scaleY);
169 else
170 scaleX = scaleY = qMax(scaleX, scaleY);
171
172 qreal xOverflow = scaleX * m_viewBox.width() - m_rect.width();
173 qreal yOverflow = scaleY * m_viewBox.height() - m_rect.height();
174
175 if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMid)
176 offsetX -= xOverflow / 2.;
177 else if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMax)
178 offsetX -= xOverflow;
179
180 if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMid)
181 offsetY -= yOverflow / 2.;
182 else if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMax)
183 offsetY -= yOverflow;
184 }
185
186 xform.translate(offsetX - m_refP.x() * scaleX, offsetY - m_refP.y() * scaleY);
187 xform.scale(scaleX, scaleY);
188
189 return xform;
190}
191
192void QSvgSymbolLike::setPainterToRectAndAdjustment(QPainter *p) const
193{
194 QRectF clip = clipRect();
195 if (!clip.isEmpty())
196 p->setClipRect(clip);
197
198 p->setTransform(aspectRatioTransform(), true);
199}
200
201QSvgSymbol::QSvgSymbol(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
202 QSvgSymbol::PreserveAspectRatios pAspectRatios,
203 QSvgSymbol::Overflow overflow)
204 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
205{
206}
207
208QSvgSymbol::~QSvgSymbol()
209 = default;
210
211void QSvgSymbol::drawCommand(QPainter *p, QSvgExtraStates &states)
212{
213 if (!states.inUse) //Symbol is only drawn when within a use node.
214 return;
215
216 p->save();
217 setPainterToRectAndAdjustment(p);
218 for (const auto &node : renderers()) {
219 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
220 node->draw(p, states);
221 }
222 p->restore();
223}
224
225QSvgNode::Type QSvgSymbol::type() const
226{
227 return Symbol;
228}
229
230QSvgMarker::QSvgMarker(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
231 QSvgSymbol::PreserveAspectRatios pAspectRatios, QSvgSymbol::Overflow overflow,
232 Orientation orientation, qreal orientationAngle, MarkerUnits markerUnits)
233 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
234 , m_orientation(orientation)
235 , m_orientationAngle(orientationAngle)
236 , m_markerUnits(markerUnits)
237{
238 // apply the svg standard style
239 QSvgFillStylePtr fillProp = std::make_unique<QSvgFillStyle>();
240 fillProp->setBrush(Qt::black);
241 appendStyleProperty(std::move(fillProp));
242
243 QSvgStrokeStylePtr strokeProp = std::make_unique<QSvgStrokeStyle>();
244 strokeProp->setMiterLimit(4);
245 strokeProp->setWidth(1);
246 strokeProp->setLineCap(Qt::FlatCap);
247 strokeProp->setLineJoin(Qt::SvgMiterJoin);
248 strokeProp->setStroke(Qt::NoBrush);
249 appendStyleProperty(std::move(strokeProp));
250}
251
252QSvgMarker::~QSvgMarker()
253 = default;
254
255QSvgFilterContainer::QSvgFilterContainer(QSvgNode *parent, const QSvgRectF &bounds,
256 QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits)
257 : QSvgStructureNode(parent)
258 , m_rect(bounds)
259 , m_filterUnits(filterUnits)
260 , m_primitiveUnits(primitiveUnits)
261 , m_supported(true)
262{
263
264}
265
266QSvgFilterContainer::~QSvgFilterContainer()
267 = default;
268
269bool QSvgFilterContainer::shouldDrawNode(QPainter *, QSvgExtraStates &) const
270{
271 return false;
272}
273
274void QSvgMarker::drawCommand(QPainter *p, QSvgExtraStates &states)
275{
276 if (!states.inUse) //Symbol is only drawn in combination with another node.
277 return;
278
279 if (Q_UNLIKELY(m_recursing))
280 return;
281 QScopedValueRollback<bool> recursingGuard(m_recursing, true);
282
283 p->save();
284 setPainterToRectAndAdjustment(p);
285
286 for (const auto &node : renderers()) {
287 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
288 node->draw(p, states);
289 }
290 p->restore();
291}
292
293namespace {
294
295struct PositionMarkerPair {
296 qreal x;
297 qreal y;
298 qreal angle;
299 QString markerId;
300 bool isStartNode = false;
301};
302
303QList<PositionMarkerPair> markersForNode(const QSvgNode *node)
304{
305 if (!node->hasAnyMarker())
306 return {};
307
308 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
309 QPointF t1 = p1 - p0;
310 QPointF t2 = p2 - p1;
311 qreal hyp1 = hypot(t1.x(), t1.y());
312 if (hyp1 > 0)
313 t1 /= hyp1;
314 else
315 return 0.;
316 qreal hyp2 = hypot(t2.x(), t2.y());
317 if (hyp2 > 0)
318 t2 /= hyp2;
319 else
320 return 0.;
321 QPointF tangent = t1 + t2;
322 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
323 };
324
325 QList<PositionMarkerPair> markers;
326
327 switch (node->type()) {
328 case QSvgNode::Line: {
329 const QSvgLine *line = static_cast<const QSvgLine*>(node);
330 if (node->hasMarkerStart())
331 markers << PositionMarkerPair { line->line().p1().x(), line->line().p1().y(),
332 line->line().angle(), line->markerStartId(),
333 true};
334 if (node->hasMarkerEnd())
335 markers << PositionMarkerPair { line->line().p2().x(), line->line().p2().y(),
336 line->line().angle(), line->markerEndId() };
337 break;
338 }
339 case QSvgNode::Polyline:
340 case QSvgNode::Polygon: {
341 const QPolygonF &polyData = (node->type() == QSvgNode::Polyline)
342 ? static_cast<const QSvgPolyline*>(node)->polygon()
343 : static_cast<const QSvgPolygon*>(node)->polygon();
344
345 if (node->hasMarkerStart() && polyData.size() > 1) {
346 QLineF line(polyData.at(0), polyData.at(1));
347 markers << PositionMarkerPair { line.p1().x(),
348 line.p1().y(),
349 line.angle(),
350 node->markerStartId(),
351 true };
352 }
353 if (node->hasMarkerMid()) {
354 for (int i = 1; i < polyData.size() - 1; i++) {
355 QPointF p0 = polyData.at(i - 1);
356 QPointF p1 = polyData.at(i);
357 QPointF p2 = polyData.at(i + 1);
358
359 markers << PositionMarkerPair { p1.x(),
360 p1.y(),
361 getMeanAngle(p0, p1, p2),
362 node->markerStartId() };
363 }
364 }
365 if (node->hasMarkerEnd() && polyData.size() > 1) {
366 QLineF line(polyData.at(polyData.size() - 1), polyData.last());
367 markers << PositionMarkerPair { line.p2().x(),
368 line.p2().y(),
369 line.angle(),
370 node->markerEndId() };
371 }
372 break;
373 }
374 case QSvgNode::Path: {
375 const QSvgPath *path = static_cast<const QSvgPath*>(node);
376 if (node->hasMarkerStart())
377 markers << PositionMarkerPair { path->path().pointAtPercent(0.).x(),
378 path->path().pointAtPercent(0.).y(),
379 path->path().angleAtPercent(0.),
380 path->markerStartId(),
381 true };
382 if (node->hasMarkerMid()) {
383 for (int i = 1; i < path->path().elementCount() - 1; i++) {
384 if (path->path().elementAt(i).type == QPainterPath::MoveToElement)
385 continue;
386 if (path->path().elementAt(i).type == QPainterPath::CurveToElement)
387 continue;
388 if (( path->path().elementAt(i).type == QPainterPath::CurveToDataElement &&
389 path->path().elementAt(i + 1).type != QPainterPath::CurveToDataElement ) ||
390 path->path().elementAt(i).type == QPainterPath::LineToElement) {
391
392 QPointF p0(path->path().elementAt(i - 1).x, path->path().elementAt(i - 1).y);
393 QPointF p1(path->path().elementAt(i).x, path->path().elementAt(i).y);
394 QPointF p2(path->path().elementAt(i + 1).x, path->path().elementAt(i + 1).y);
395
396 markers << PositionMarkerPair { p1.x(),
397 p1.y(),
398 getMeanAngle(p0, p1, p2),
399 path->markerMidId() };
400 }
401 }
402 }
403 if (node->hasMarkerEnd())
404 markers << PositionMarkerPair { path->path().pointAtPercent(1.).x(),
405 path->path().pointAtPercent(1.).y(),
406 path->path().angleAtPercent(1.),
407 path->markerEndId() };
408 break;
409 }
410 default:
411 Q_UNREACHABLE();
412 break;
413 }
414
415 return markers;
416}
417
418} // anonymous namespace
419
420
421void QSvgMarker::drawMarkersForNode(QSvgNode *node, QPainter *p, QSvgExtraStates &states)
422{
423 drawHelper(node, p, states);
424}
425
426QRectF QSvgMarker::markersBoundsForNode(const QSvgNode *node, QPainter *p, QSvgExtraStates &states)
427{
428 QRectF bounds;
429 drawHelper(node, p, states, &bounds);
430 return bounds;
431}
432
433QSvgNode::Type QSvgMarker::type() const
434{
435 return Marker;
436}
437
438void QSvgMarker::drawHelper(const QSvgNode *node, QPainter *p,
439 QSvgExtraStates &states, QRectF *boundingRect)
440{
441 QScopedValueRollback<bool> inUseGuard(states.inUse, true);
442
443 const bool isPainting = (boundingRect == nullptr);
444 const auto markers = markersForNode(node);
445 for (auto &i : markers) {
446 QSvgNode *referencedNode = node->document()->namedNode(i.markerId);
447 if (!referencedNode || referencedNode->type() != QSvgNode::Marker)
448 continue;
449 QSvgMarker *markNode = static_cast<QSvgMarker *>(referencedNode);
450
451 p->save();
452 p->translate(i.x, i.y);
453 if (markNode->orientation() == QSvgMarker::Orientation::Value) {
454 p->rotate(markNode->orientationAngle());
455 } else {
456 p->rotate(-i.angle);
457 if (i.isStartNode && markNode->orientation()
458 == QSvgMarker::Orientation::AutoStartReverse) {
459 p->scale(-1, -1);
460 }
461 }
462 QRectF oldRect = markNode->m_rect;
463 if (markNode->markerUnits() == QSvgMarker::MarkerUnits::StrokeWidth) {
464 markNode->m_rect.setWidth(markNode->m_rect.width() * p->pen().widthF());
465 markNode->m_rect.setHeight(markNode->m_rect.height() * p->pen().widthF());
466 }
467 if (isPainting)
468 markNode->draw(p, states);
469
470 if (boundingRect) {
471 QTransform xf = p->transform();
472 p->resetTransform();
473 *boundingRect |= xf.mapRect(markNode->decoratedInternalBounds(p, states));
474 }
475
476 markNode->m_rect = oldRect;
477 p->restore();
478 }
479}
480
481QImage QSvgFilterContainer::applyFilter(const QImage &buffer, QPainter *p, const QRectF &bounds) const
482{
483 QRectF localFilterRegion = m_rect.resolveRelativeLengths(bounds, m_filterUnits);
484 QRect globalFilterRegion = p->transform().mapRect(localFilterRegion).toRect();
485 QRect globalFilterRegionRel = globalFilterRegion.translated(-buffer.offset());
486
487 if (globalFilterRegionRel.isEmpty())
488 return buffer;
489
490 QImage proxy;
491 if (!QImageIOHandler::allocateImage(globalFilterRegionRel.size(), buffer.format(), &proxy)) {
492 qCWarning(lcSvgDraw) << "The requested filter is too big, ignoring";
493 return buffer;
494 }
495 proxy = buffer.copy(globalFilterRegionRel);
496 proxy.setOffset(globalFilterRegion.topLeft());
497 if (proxy.isNull())
498 return buffer;
499
500 QMap<QString, QImage> buffers;
501 buffers[QStringLiteral("")] = proxy;
502 buffers[QStringLiteral("SourceGraphic")] = proxy;
503
504 bool requiresSourceAlpha = false;
505
506 for (const auto &node : renderers()) {
507 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
508 if (filter && filter->requiresSourceAlpha()) {
509 requiresSourceAlpha = true;
510 break;
511 }
512 }
513
514 if (requiresSourceAlpha) {
515 QImage proxyAlpha = proxy.convertedTo(QImage::Format_Alpha8).convertedTo(proxy.format());
516 proxyAlpha.setOffset(proxy.offset());
517 if (proxyAlpha.isNull())
518 return buffer;
519 buffers[QStringLiteral("SourceAlpha")] = std::move(proxyAlpha);
520 }
521
522 QImage result;
523 for (const auto &node : renderers()) {
524 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
525 if (filter) {
526 result = filter->apply(buffers, p, bounds, localFilterRegion, m_primitiveUnits, m_filterUnits);
527 if (!result.isNull()) {
528 buffers[QStringLiteral("")] = result;
529 buffers[filter->result()] = result;
530 }
531 }
532 }
533 return result;
534}
535
536void QSvgFilterContainer::setSupported(bool supported)
537{
538 m_supported = supported;
539}
540
541bool QSvgFilterContainer::supported() const
542{
543 return m_supported;
544}
545
546QRectF QSvgFilterContainer::filterRegion(const QRectF &itemBounds) const
547{
548 return m_rect.resolveRelativeLengths(itemBounds, m_filterUnits);
549}
550
551QSvgNode::Type QSvgFilterContainer::type() const
552{
553 return Filter;
554}
555
556
557inline static bool isSupportedSvgFeature(QStringView str)
558{
559 constexpr auto prefix_1_2 = "http://www.w3.org/Graphics/SVG/feature/1.2/#"_L1;
560 if (str.startsWith(prefix_1_2)) {
561 const auto suffix = str.sliced(prefix_1_2.size());
562
563 constexpr auto features = qOffsetStringArray(
564 "Text",
565 "Shape",
566 "SVG",
567 "Structure",
568 "SolidColor",
569 "Hyperlinking",
570 "CoreAttribute",
571 "XlinkAttribute",
572 "SVG-static",
573 "OpacityAttribute",
574 "Gradient",
575 "Font",
576 "Image",
577 "ConditionalProcessing",
578 "Extensibility",
579 "GraphicsAttribute",
580 "Prefetch",
581 "PaintAttribute",
582 "ConditionalProcessingAttribute",
583 "ExternalResourcesRequiredAttribute"
584 );
585
586 // This is
587 // return features.contains(suffix);
588 // but QOffsetStringArray does't support heterogeneous contains()
589 for (int i = 0; i < features.count(); ++i)
590 if (suffix == QLatin1StringView{features.at(i)})
591 return true;
592 } // 1.2
593
594 return false;
595}
596
597static inline bool isSupportedSvgExtension(const QString &)
598{
599 return false;
600}
601
602
603QSvgSwitch::QSvgSwitch(QSvgNode *parent)
604 : QSvgStructureNode(parent)
605{
606 init();
607}
608
609QSvgSwitch::~QSvgSwitch()
610 = default;
611
612QSvgNode *QSvgSwitch::childToRender() const
613{
614 for (const auto &node : renderers()) {
615 if (node->isVisible() && (node->displayMode() != QSvgNode::NoneMode)) {
616 const QStringList &features = node->requiredFeatures();
617 const QStringList &extensions = node->requiredExtensions();
618 const QStringList &languages = node->requiredLanguages();
619 const QStringList &formats = node->requiredFormats();
620 const QStringList &fonts = node->requiredFonts();
621
622 bool okToRender = true;
623 if (!features.isEmpty()) {
624 QStringList::const_iterator sitr = features.constBegin();
625 for (; sitr != features.constEnd(); ++sitr) {
626 if (!isSupportedSvgFeature(*sitr)) {
627 okToRender = false;
628 break;
629 }
630 }
631 }
632
633 if (okToRender && !extensions.isEmpty()) {
634 QStringList::const_iterator sitr = extensions.constBegin();
635 for (; sitr != extensions.constEnd(); ++sitr) {
636 if (!isSupportedSvgExtension(*sitr)) {
637 okToRender = false;
638 break;
639 }
640 }
641 }
642
643 if (okToRender && !languages.isEmpty()) {
644 QStringList::const_iterator sitr = languages.constBegin();
645 okToRender = false;
646 for (; sitr != languages.constEnd(); ++sitr) {
647 if ((*sitr).startsWith(m_systemLanguagePrefix)) {
648 okToRender = true;
649 break;
650 }
651 }
652 }
653
654 if (okToRender && !formats.isEmpty())
655 okToRender = false;
656
657 if (okToRender && !fonts.isEmpty())
658 okToRender = false;
659
660 if (okToRender)
661 return node.get();
662 }
663 }
664
665 return nullptr;
666}
667
668void QSvgSwitch::drawCommand(QPainter *p, QSvgExtraStates &states)
669{
670 QSvgNode *node = childToRender();
671 if (node != nullptr)
672 node->draw(p, states);
673}
674
675QSvgNode::Type QSvgSwitch::type() const
676{
677 return Switch;
678}
679
680void QSvgSwitch::init()
681{
682 QLocale locale;
683 m_systemLanguage = locale.name().replace(QLatin1Char('_'), QLatin1Char('-'));
684 int idx = m_systemLanguage.indexOf(QLatin1Char('-'));
685 m_systemLanguagePrefix = m_systemLanguage.mid(0, idx);
686}
687
688QRectF QSvgStructureNode::internalBounds(QPainter *p, QSvgExtraStates &states) const
689{
690 QRectF bounds;
691 if (!m_recursing) {
692 QScopedValueRollback<bool> guard(m_recursing, true);
693 for (const auto &node : renderers())
694 bounds |= node->bounds(p, states);
695 }
696 return bounds;
697}
698
699QRectF QSvgStructureNode::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states) const
700{
701 QRectF bounds;
702 if (!m_recursing) {
703 QScopedValueRollback<bool> guard(m_recursing, true);
704 for (const auto &node : renderers())
705 bounds |= node->decoratedBounds(p, states);
706 }
707 return bounds;
708}
709
710QSvgNode* QSvgStructureNode::previousSiblingNode(QSvgNode *n) const
711{
712 QSvgNode *prev = nullptr;
713 for (const auto &node : renderers()) {
714 if (node.get() == n)
715 return prev;
716 prev = node.get();
717 }
718 return prev;
719}
720
721void QSvgStructureNode::releaseDescendants()
722{
723 // This function will release the descendants of a QSvgStructureNode from bottom to top.
724 // Destructors are never called recursively in this case and stack overflow will not
725 // happen in deeply nested trees.
726 // This function does not allocate any memory at the cost of sacrificing some performance to
727 // make it safe to be called from a destructor.
728 QSvgNode *currentParent = this;
729 while (currentParent) {
730 switch (currentParent->type()) {
731 case QSvgNode::Doc:
732 case QSvgNode::Defs:
733 case QSvgNode::Group:
734 case QSvgNode::Mask:
735 case QSvgNode::Pattern:
736 case QSvgNode::Symbol:
737 case QSvgNode::Switch:
738 case QSvgNode::Filter:
739 {
740 QSvgStructureNode *currentParentSN = static_cast<QSvgStructureNode *>(currentParent);
741 if (currentParentSN->m_renderers.empty()) {
742 currentParent = currentParent->parent();
743 if (currentParent)
744 static_cast<QSvgStructureNode *>(currentParent)->m_renderers.pop_front();
745 } else {
746 Q_ASSERT(currentParentSN->m_renderers.front().get()->parent() == currentParent);
747 currentParent = currentParentSN->m_renderers.front().get();
748 }
749 }
750 break;
751 default:
752 currentParent = currentParent->parent();
753 if (currentParent)
754 static_cast<QSvgStructureNode *>(currentParent)->m_renderers.pop_front();
755 break;
756 }
757 }
758 Q_ASSERT(this->m_renderers.empty());
759}
760
761QSvgMask::QSvgMask(QSvgNode *parent, QSvgRectF bounds,
762 QtSvg::UnitTypes contentUnits)
763 : QSvgStructureNode(parent)
764 , m_rect(bounds)
765 , m_contentUnits(contentUnits)
766{
767}
768
769QSvgMask::~QSvgMask()
770 = default;
771
772bool QSvgMask::shouldDrawNode(QPainter *, QSvgExtraStates &) const
773{
774 return false;
775}
776
777QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect) const
778{
779 QTransform t = p->transform();
780 p->resetTransform();
781 QRectF basicRect = targetNode->internalBounds(p, states);
782 *globalRect = t.mapRect(basicRect);
783 p->setTransform(t);
784 return createMask(p, states, basicRect, globalRect);
785}
786
787QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states, const QRectF &localRect, QRectF *globalRect) const
788{
789 QRect imageBound = globalRect->toAlignedRect();
790 *globalRect = imageBound.toRectF();
791
792 QImage mask;
793 if (!QImageIOHandler::allocateImage(imageBound.size(), QImage::Format_RGBA8888, &mask)) {
794 qCWarning(lcSvgDraw) << "The requested mask size is too big, ignoring";
795 return mask;
796 }
797
798 if (Q_UNLIKELY(m_recursing))
799 return mask;
800 QScopedValueRollback<bool> recursingGuard(m_recursing, true);
801
802 // Chrome seems to return the mask of the mask if a mask is set on the mask
803 if (this->hasMask()) {
804 QSvgNode *referencedNode = document()->namedNode(this->maskId());
805 if (referencedNode && referencedNode->type() == QSvgNode::Mask) {
806 QSvgMask *maskNode = static_cast<QSvgMask *>(referencedNode);
807 QRectF boundsRect;
808 return maskNode->createMask(p, states, localRect, &boundsRect);
809 }
810 }
811
812 // The mask is created with other elements during rendering.
813 // Black pixels are masked out, white pixels are not masked.
814 // The strategy is to draw the elements in a buffer (QImage) and to map
815 // the white-black image into a transparent-white image that can be used
816 // with QPainters composition mode to set the mask.
817
818 mask.fill(Qt::transparent);
819 QPainter painter(&mask);
820 initPainter(&painter);
821
822 QSvgExtraStates maskNodeStates;
823 applyStyleRecursive(&painter, maskNodeStates);
824
825 // The transformation of the mask node is not relevant. What matters are the contentUnits
826 // and the position/scale of the node that the mask is applied to.
827 painter.resetTransform();
828 painter.translate(-imageBound.topLeft());
829 painter.setTransform(p->transform(), true);
830
831 QTransform oldT = painter.transform();
832 if (m_contentUnits == QtSvg::UnitTypes::objectBoundingBox){
833 painter.translate(localRect.topLeft());
834 painter.scale(localRect.width(), localRect.height());
835 }
836
837 // Draw all content items of the mask to generate the mask
838 for (const auto &node : renderers())
839 {
840 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
841 node->draw(&painter, maskNodeStates);
842 }
843
844 for (int i=0; i < mask.height(); i++) {
845 QRgb *line = reinterpret_cast<QRgb *>(mask.scanLine(i));
846 for (int j=0; j < mask.width(); j++) {
847 const qreal rC = 0.2125, gC = 0.7154, bC = 0.0721; //luminanceToAlpha times alpha following SVG 1.1
848 int alpha = 255 - (qRed(line[j]) * rC + qGreen(line[j]) * gC + qBlue(line[j]) * bC) * qAlpha(line[j])/255.;
849 line[j] = qRgba(0, 0, 0, alpha);
850 }
851 }
852
853 // Make a path out of the clipRectangle and draw it inverted - black over all content items.
854 // This is required to apply a clip rectangle with transformations.
855 // painter.setClipRect(clipRect) sounds like the obvious thing to do but
856 // created artifacts due to antialiasing.
857 QRectF clipRect = m_rect.resolveRelativeLengths(localRect);
858 QPainterPath clipPath;
859 clipPath.setFillRule(Qt::OddEvenFill);
860 clipPath.addRect(mask.rect().adjusted(-10, -10, 20, 20));
861 clipPath.addPolygon(oldT.map(QPolygonF(clipRect)));
862 painter.resetTransform();
863 painter.fillPath(clipPath, Qt::black);
864 revertStyleRecursive(&painter, maskNodeStates);
865 return mask;
866}
867
868QSvgNode::Type QSvgMask::type() const
869{
870 return Mask;
871}
872
873QSvgPattern::QSvgPattern(QSvgNode *parent, QSvgRectF bounds, QRectF viewBox,
874 QtSvg::UnitTypes contentUnits, QTransform transform)
875 : QSvgStructureNode(parent),
876 m_rect(bounds),
877 m_viewBox(viewBox),
878 m_contentUnits(contentUnits),
879 m_isRendering(false),
880 m_transform(transform)
881
882{
883
884}
885
886QSvgPattern::~QSvgPattern()
887 = default;
888
889bool QSvgPattern::shouldDrawNode(QPainter *, QSvgExtraStates &) const
890{
891 return false;
892}
893
895{
896 static QImage checkerPattern;
897
898 if (checkerPattern.isNull()) {
899 checkerPattern = QImage(QSize(8, 8), QImage::Format_ARGB32);
900 QPainter p(&checkerPattern);
901 p.fillRect(QRect(0, 0, 4, 4), QColorConstants::Svg::white);
902 p.fillRect(QRect(4, 0, 4, 4), QColorConstants::Svg::black);
903 p.fillRect(QRect(0, 4, 4, 4), QColorConstants::Svg::black);
904 p.fillRect(QRect(4, 4, 4, 4), QColorConstants::Svg::white);
905 }
906
907 return checkerPattern;
908}
909
910QImage QSvgPattern::patternImage(QPainter *p, QSvgExtraStates &states, const QSvgNode *patternElement)
911{
912 // pe stands for Pattern Element
913 QRectF peBoundingBox;
914 QRectF peWorldBoundingBox;
915
916 QTransform t = p->transform();
917 p->resetTransform();
918 peBoundingBox = patternElement->internalBounds(p, states);
919 peWorldBoundingBox = t.mapRect(peBoundingBox);
920 p->setTransform(t);
921
922 // This function renders the pattern into an Image, so we need to apply the correct
923 // scaling values when we draw the pattern. The scaling is affected by two factors :
924 // - The "patternTransform" attribute which itself might contain a scaling
925 // - The scaling applied globally.
926 // The first is obtained from m11 and m22 matrix elements,
927 // while the second is calculated by dividing the patternElement global size
928 // by its local size.
929 qreal contentScaleFactorX = m_transform.m11();
930 qreal contentScaleFactorY = m_transform.m22();
931 if (m_contentUnits == QtSvg::UnitTypes::userSpaceOnUse) {
932 contentScaleFactorX *= t.m11();
933 contentScaleFactorY *= t.m22();
934 } else {
935 contentScaleFactorX *= peWorldBoundingBox.width();
936 contentScaleFactorY *= peWorldBoundingBox.height();
937 }
938
939 // Calculate the pattern bounding box depending on the used UnitTypes
940 QRectF patternBoundingBox = m_rect.resolveRelativeLengths(peBoundingBox);
941
942 QSize imageSize;
943 imageSize.setWidth(qCeil(patternBoundingBox.width() * t.m11() * m_transform.m11()));
944 imageSize.setHeight(qCeil(patternBoundingBox.height() * t.m22() * m_transform.m22()));
945 if (imageSize.isEmpty())
946 return QImage(); // Avoid division by zero in calculateAppliedTransform()
947
948 calculateAppliedTransform(t, peBoundingBox, imageSize);
949 if (document()->isCalculatingImplicitViewBox())
950 return QImage(imageSize, QImage::Format_ARGB32); // dummy image to avoid endless recursion
951 else
952 return renderPattern(imageSize, contentScaleFactorX, contentScaleFactorY);
953}
954
955QSvgNode::Type QSvgPattern::type() const
956{
957 return Pattern;
958}
959
960QImage QSvgPattern::renderPattern(QSize size, qreal contentScaleX, qreal contentScaleY)
961{
962 if (size.isEmpty() || !qIsFinite(contentScaleX) || !qIsFinite(contentScaleY))
963 return defaultPattern();
964
965 // Allocate a QImage to draw the pattern in with the calculated size.
966 QImage pattern;
967 if (!QImageIOHandler::allocateImage(size, QImage::Format_ARGB32, &pattern)) {
968 qCWarning(lcSvgDraw) << "The requested pattern size is too big, ignoring";
969 return defaultPattern();
970 }
971 pattern.fill(Qt::transparent);
972
973 if (m_isRendering) {
974 qCWarning(lcSvgDraw) << "The pattern is trying to render itself recursively. "
975 "Returning a transparent QImage of the right size.";
976 return pattern;
977 }
978 QScopedValueRollback<bool> guard(m_isRendering, true);
979
980 // Draw the pattern using our QPainter.
981 QPainter patternPainter(&pattern);
982 QSvgExtraStates patternStates;
983 initPainter(&patternPainter);
984 applyStyleRecursive(&patternPainter, patternStates);
985 patternPainter.resetTransform();
986
987 // According to the <pattern> definition, if viewBox exists then patternContentUnits
988 // is ignored
989 if (m_viewBox.isNull())
990 patternPainter.scale(contentScaleX, contentScaleY);
991 else
992 patternPainter.setWindow(m_viewBox.toRect());
993
994 // Draw all this Pattern children nodes with our QPainter,
995 // no need to use any Extra States
996 for (const auto &node : renderers())
997 node->draw(&patternPainter, patternStates);
998
999 revertStyleRecursive(&patternPainter, patternStates);
1000 return pattern;
1001}
1002
1003void QSvgPattern::calculateAppliedTransform(QTransform &worldTransform, QRectF peLocalBB, QSize imageSize)
1004{
1005 // Calculate the required transform to be applied to the QBrush used for correct
1006 // pattern drawing with the object being rendered.
1007 // Scale : Apply inverse the scale used above because QBrush uses the transform used
1008 // by the QPainter and this function has already rendered the QImage with the
1009 // correct size. Moreover, take into account the difference between the required
1010 // ideal image size in float and the QSize given to image as an integer value.
1011 //
1012 // Translate : Apply translation depending on the calculated x and y values so that the
1013 // drawn pattern can be shifted inside the object.
1014 // Pattern Transform : Apply the transform in the "patternTransform" attribute. This
1015 // transform contains everything except scaling, because it is
1016 // already applied above on the QImage and the QPainter while
1017 // drawing the pattern tile.
1018 m_appliedTransform.reset();
1019 qreal imageDownScaleFactorX = 1 / worldTransform.m11();
1020 qreal imageDownScaleFactorY = 1 / worldTransform.m22();
1021
1022 m_appliedTransform.scale(qIsFinite(imageDownScaleFactorX) ? imageDownScaleFactorX : 1.0,
1023 qIsFinite(imageDownScaleFactorY) ? imageDownScaleFactorY : 1.0);
1024
1025 QRectF p = m_rect.resolveRelativeLengths(peLocalBB);
1026 m_appliedTransform.scale((p.width() * worldTransform.m11() * m_transform.m11()) / imageSize.width(),
1027 (p.height() * worldTransform.m22() * m_transform.m22()) / imageSize.height());
1028
1029 QPointF translation = m_rect.translationRelativeToBoundingBox(peLocalBB);
1030 m_appliedTransform.translate(translation.x() * worldTransform.m11(), translation.y() * worldTransform.m22());
1031
1032 QTransform scalelessTransform = m_transform;
1033 scalelessTransform.scale(1 / m_transform.m11(), 1 / m_transform.m22());
1034
1035 m_appliedTransform = m_appliedTransform * scalelessTransform;
1036}
1037
1038QT_END_NAMESPACE
Definition qlist.h:81
friend class QPainter
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:97
Combined button and popup list for selecting options.
#define QStringLiteral(str)
Definition qstring.h:1825
static QImage & defaultPattern()
static bool isSupportedSvgExtension(const QString &)
static bool isSupportedSvgFeature(QStringView str)