19#include <QLoggingCategory>
20#include <qscopedvaluerollback.h>
21#include <QtGui/qimageiohandler.h>
23#include <QtCore/qlatin1stringview.h>
24#include <QtCore/qstringview.h>
25#include <QtCore/private/qoffsetstringarray_p.h>
29using namespace Qt::StringLiterals;
31QSvgG::QSvgG(QSvgNode *parent)
32 : QSvgStructureNode(parent)
40void QSvgG::drawCommand(QPainter *p, QSvgExtraStates &states)
42 for (
const auto &node : renderers()) {
43 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
44 node->draw(p, states);
48bool QSvgG::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
53QSvgNode::Type QSvgG::type()
const
58bool QSvgG::requiresGroupRendering()
const
60 return m_renderers.size() > 1;
63QSvgStructureNode::QSvgStructureNode(QSvgNode *parent)
69QSvgStructureNode::~QSvgStructureNode()
72void QSvgStructureNode::addChild(std::unique_ptr<QSvgNode> child,
const QString &id)
75 QSvgDocument *doc = document();
77 doc->addNamedNode(id, child.get());
80 m_renderers.push_back(std::move(child));
83QSvgDefs::QSvgDefs(QSvgNode *parent)
84 : QSvgStructureNode(parent)
91bool QSvgDefs::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
96QSvgNode::Type QSvgDefs::type()
const
101QSvgSymbolLike::QSvgSymbolLike(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
102 QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow)
103 : QSvgStructureNode(parent)
107 , m_pAspectRatios(pAspectRatios)
108 , m_overflow(overflow)
113QSvgSymbolLike::~QSvgSymbolLike()
116QRectF QSvgSymbolLike::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
119 setPainterToRectAndAdjustment(p);
120 QRectF rect = internalBounds(p, states);
125bool QSvgSymbolLike::requiresGroupRendering()
const
127 return m_renderers.size() > 1;
130QRectF QSvgSymbolLike::clipRect()
const
132 if (m_overflow != Overflow::Hidden || !m_viewBox.isValid())
136 if (m_rect.width() > 0 && m_viewBox.width() > 0)
137 scaleX = m_rect.width() / m_viewBox.width();
139 if (m_rect.height() > 0 && m_viewBox.height() > 0)
140 scaleY = m_rect.height() / m_viewBox.height();
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);
147 return t.mapRect(m_viewBox);
150QTransform QSvgSymbolLike::aspectRatioTransform()
const
158 if (m_rect.width() > 0 && m_viewBox.width() > 0)
159 scaleX = m_rect.width() / m_viewBox.width();
161 if (m_rect.height() > 0 && m_viewBox.height() > 0)
162 scaleY = m_rect.height() / m_viewBox.height();
164 if (!qFuzzyCompare(scaleX, scaleY) &&
165 m_pAspectRatios.testAnyFlag(PreserveAspectRatio::xyMask)) {
167 if (m_pAspectRatios.testAnyFlag(PreserveAspectRatio::meet))
168 scaleX = scaleY = qMin(scaleX, scaleY);
170 scaleX = scaleY = qMax(scaleX, scaleY);
172 qreal xOverflow = scaleX * m_viewBox.width() - m_rect.width();
173 qreal yOverflow = scaleY * m_viewBox.height() - m_rect.height();
175 if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMid)
176 offsetX -= xOverflow / 2.;
177 else if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMax)
178 offsetX -= xOverflow;
180 if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMid)
181 offsetY -= yOverflow / 2.;
182 else if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMax)
183 offsetY -= yOverflow;
186 xform.translate(offsetX - m_refP.x() * scaleX, offsetY - m_refP.y() * scaleY);
187 xform.scale(scaleX, scaleY);
192void QSvgSymbolLike::setPainterToRectAndAdjustment(QPainter *p)
const
194 QRectF clip = clipRect();
196 p->setClipRect(clip);
198 p->setTransform(aspectRatioTransform(),
true);
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)
208QSvgSymbol::~QSvgSymbol()
211void QSvgSymbol::drawCommand(QPainter *p, QSvgExtraStates &states)
217 setPainterToRectAndAdjustment(p);
218 for (
const auto &node : renderers()) {
219 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
220 node->draw(p, states);
225QSvgNode::Type QSvgSymbol::type()
const
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)
239 QSvgFillStylePtr fillProp = std::make_unique<QSvgFillStyle>();
240 fillProp->setBrush(Qt::black);
241 appendStyleProperty(std::move(fillProp));
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));
252QSvgMarker::~QSvgMarker()
255QSvgFilterContainer::QSvgFilterContainer(QSvgNode *parent,
const QSvgRectF &bounds,
256 QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits)
257 : QSvgStructureNode(parent)
259 , m_filterUnits(filterUnits)
260 , m_primitiveUnits(primitiveUnits)
266QSvgFilterContainer::~QSvgFilterContainer()
269bool QSvgFilterContainer::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
274void QSvgMarker::drawCommand(QPainter *p, QSvgExtraStates &states)
279 if (Q_UNLIKELY(m_recursing))
281 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
284 setPainterToRectAndAdjustment(p);
286 for (
const auto &node : renderers()) {
287 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
288 node->draw(p, states);
295struct PositionMarkerPair {
300 bool isStartNode =
false;
303QList<PositionMarkerPair> markersForNode(
const QSvgNode *node)
305 if (!node->hasAnyMarker())
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());
316 qreal hyp2 = hypot(t2.x(), t2.y());
321 QPointF tangent = t1 + t2;
322 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
325 QList<PositionMarkerPair> markers;
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(),
334 if (node->hasMarkerEnd())
335 markers << PositionMarkerPair { line->line().p2().x(), line->line().p2().y(),
336 line->line().angle(), line->markerEndId() };
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();
345 if (node->hasMarkerStart() && polyData.size() > 1) {
346 QLineF line(polyData.at(0), polyData.at(1));
347 markers << PositionMarkerPair { line.p1().x(),
350 node->markerStartId(),
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);
359 markers << PositionMarkerPair { p1.x(),
361 getMeanAngle(p0, p1, p2),
362 node->markerStartId() };
365 if (node->hasMarkerEnd() && polyData.size() > 1) {
366 QLineF line(polyData.at(polyData.size() - 1), polyData.last());
367 markers << PositionMarkerPair { line.p2().x(),
370 node->markerEndId() };
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(),
382 if (node->hasMarkerMid()) {
383 for (
int i = 1; i < path->path().elementCount() - 1; i++) {
384 if (path->path().elementAt(i).type == QPainterPath::MoveToElement)
386 if (path->path().elementAt(i).type == QPainterPath::CurveToElement)
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) {
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);
396 markers << PositionMarkerPair { p1.x(),
398 getMeanAngle(p0, p1, p2),
399 path->markerMidId() };
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() };
421void QSvgMarker::drawMarkersForNode(QSvgNode *node, QPainter *p, QSvgExtraStates &states)
423 drawHelper(node, p, states);
426QRectF QSvgMarker::markersBoundsForNode(
const QSvgNode *node, QPainter *p, QSvgExtraStates &states)
429 drawHelper(node, p, states, &bounds);
433QSvgNode::Type QSvgMarker::type()
const
438void QSvgMarker::drawHelper(
const QSvgNode *node, QPainter *p,
439 QSvgExtraStates &states, QRectF *boundingRect)
441 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
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)
449 QSvgMarker *markNode =
static_cast<QSvgMarker *>(referencedNode);
452 p->translate(i.x, i.y);
453 if (markNode->orientation() == QSvgMarker::Orientation::Value) {
454 p->rotate(markNode->orientationAngle());
457 if (i.isStartNode && markNode->orientation()
458 == QSvgMarker::Orientation::AutoStartReverse) {
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());
468 markNode->draw(p, states);
471 QTransform xf = p->transform();
473 *boundingRect |= xf.mapRect(markNode->decoratedInternalBounds(p, states));
476 markNode->m_rect = oldRect;
481QImage QSvgFilterContainer::applyFilter(
const QImage &buffer, QPainter *p,
const QRectF &bounds)
const
483 QRectF localFilterRegion = m_rect.resolveRelativeLengths(bounds, m_filterUnits);
484 QRect globalFilterRegion = p->transform().mapRect(localFilterRegion).toRect();
485 QRect globalFilterRegionRel = globalFilterRegion.translated(-buffer.offset());
487 if (globalFilterRegionRel.isEmpty())
491 if (!QImageIOHandler::allocateImage(globalFilterRegionRel.size(), buffer.format(), &proxy)) {
492 qCWarning(lcSvgDraw) <<
"The requested filter is too big, ignoring";
495 proxy = buffer.copy(globalFilterRegionRel);
496 proxy.setOffset(globalFilterRegion.topLeft());
500 QMap<QString, QImage> buffers;
504 bool requiresSourceAlpha =
false;
506 for (
const auto &node : renderers()) {
507 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
508 if (filter && filter->requiresSourceAlpha()) {
509 requiresSourceAlpha =
true;
514 if (requiresSourceAlpha) {
515 QImage proxyAlpha = proxy.convertedTo(QImage::Format_Alpha8).convertedTo(proxy.format());
516 proxyAlpha.setOffset(proxy.offset());
517 if (proxyAlpha.isNull())
523 for (
const auto &node : renderers()) {
524 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
526 result = filter->apply(buffers, p, bounds, localFilterRegion, m_primitiveUnits, m_filterUnits);
527 if (!result.isNull()) {
529 buffers[filter->result()] = result;
536void QSvgFilterContainer::setSupported(
bool supported)
538 m_supported = supported;
541bool QSvgFilterContainer::supported()
const
546QRectF QSvgFilterContainer::filterRegion(
const QRectF &itemBounds)
const
548 return m_rect.resolveRelativeLengths(itemBounds, m_filterUnits);
551QSvgNode::Type QSvgFilterContainer::type()
const
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());
563 constexpr auto features = qOffsetStringArray(
577 "ConditionalProcessing",
582 "ConditionalProcessingAttribute",
583 "ExternalResourcesRequiredAttribute"
589 for (
int i = 0; i < features.count(); ++i)
590 if (suffix == QLatin1StringView{features.at(i)})
603QSvgSwitch::QSvgSwitch(QSvgNode *parent)
604 : QSvgStructureNode(parent)
609QSvgSwitch::~QSvgSwitch()
612QSvgNode *QSvgSwitch::childToRender()
const
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();
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)) {
633 if (okToRender && !extensions.isEmpty()) {
634 QStringList::const_iterator sitr = extensions.constBegin();
635 for (; sitr != extensions.constEnd(); ++sitr) {
636 if (!isSupportedSvgExtension(*sitr)) {
643 if (okToRender && !languages.isEmpty()) {
644 QStringList::const_iterator sitr = languages.constBegin();
646 for (; sitr != languages.constEnd(); ++sitr) {
647 if ((*sitr).startsWith(m_systemLanguagePrefix)) {
654 if (okToRender && !formats.isEmpty())
657 if (okToRender && !fonts.isEmpty())
668void QSvgSwitch::drawCommand(QPainter *p, QSvgExtraStates &states)
670 QSvgNode *node = childToRender();
672 node->draw(p, states);
675QSvgNode::Type QSvgSwitch::type()
const
680void QSvgSwitch::init()
683 m_systemLanguage = locale.name().replace(QLatin1Char(
'_'), QLatin1Char(
'-'));
684 int idx = m_systemLanguage.indexOf(QLatin1Char(
'-'));
685 m_systemLanguagePrefix = m_systemLanguage.mid(0, idx);
688QRectF QSvgStructureNode::internalBounds(QPainter *p, QSvgExtraStates &states)
const
692 QScopedValueRollback<
bool> guard(m_recursing,
true);
693 for (
const auto &node : renderers())
694 bounds |= node->bounds(p, states);
699QRectF QSvgStructureNode::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
703 QScopedValueRollback<
bool> guard(m_recursing,
true);
704 for (
const auto &node : renderers())
705 bounds |= node->decoratedBounds(p, states);
710QSvgNode* QSvgStructureNode::previousSiblingNode(QSvgNode *n)
const
712 QSvgNode *prev =
nullptr;
713 for (
const auto &node : renderers()) {
721void QSvgStructureNode::releaseDescendants()
728 QSvgNode *currentParent =
this;
729 while (currentParent) {
730 switch (currentParent->type()) {
733 case QSvgNode::Group:
735 case QSvgNode::Pattern:
736 case QSvgNode::Symbol:
737 case QSvgNode::Switch:
738 case QSvgNode::Filter:
740 QSvgStructureNode *currentParentSN =
static_cast<QSvgStructureNode *>(currentParent);
741 if (currentParentSN->m_renderers.empty()) {
742 currentParent = currentParent->parent();
744 static_cast<QSvgStructureNode *>(currentParent)->m_renderers.pop_front();
746 Q_ASSERT(currentParentSN->m_renderers.front().get()->parent() == currentParent);
747 currentParent = currentParentSN->m_renderers.front().get();
752 currentParent = currentParent->parent();
754 static_cast<QSvgStructureNode *>(currentParent)->m_renderers.pop_front();
758 Q_ASSERT(
this->m_renderers.empty());
761QSvgMask::QSvgMask(QSvgNode *parent, QSvgRectF bounds,
762 QtSvg::UnitTypes contentUnits)
763 : QSvgStructureNode(parent)
765 , m_contentUnits(contentUnits)
772bool QSvgMask::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
777QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect)
const
779 QTransform t = p->transform();
781 QRectF basicRect = targetNode->internalBounds(p, states);
782 *globalRect = t.mapRect(basicRect);
784 return createMask(p, states, basicRect, globalRect);
787QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states,
const QRectF &localRect, QRectF *globalRect)
const
789 QRect imageBound = globalRect->toAlignedRect();
790 *globalRect = imageBound.toRectF();
793 if (!QImageIOHandler::allocateImage(imageBound.size(), QImage::Format_RGBA8888, &mask)) {
794 qCWarning(lcSvgDraw) <<
"The requested mask size is too big, ignoring";
798 if (Q_UNLIKELY(m_recursing))
800 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
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);
808 return maskNode->createMask(p, states, localRect, &boundsRect);
818 mask.fill(Qt::transparent);
819 QPainter painter(&mask);
820 initPainter(&painter);
822 QSvgExtraStates maskNodeStates;
823 applyStyleRecursive(&painter, maskNodeStates);
827 painter.resetTransform();
828 painter.translate(-imageBound.topLeft());
829 painter.setTransform(p->transform(),
true);
831 QTransform oldT = painter.transform();
832 if (m_contentUnits == QtSvg::UnitTypes::objectBoundingBox){
833 painter.translate(localRect.topLeft());
834 painter.scale(localRect.width(), localRect.height());
838 for (
const auto &node : renderers())
840 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
841 node->draw(&painter, maskNodeStates);
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;
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);
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);
868QSvgNode::Type QSvgMask::type()
const
873QSvgPattern::QSvgPattern(QSvgNode *parent, QSvgRectF bounds, QRectF viewBox,
874 QtSvg::UnitTypes contentUnits, QTransform transform)
875 : QSvgStructureNode(parent),
878 m_contentUnits(contentUnits),
879 m_isRendering(
false),
880 m_transform(transform)
886QSvgPattern::~QSvgPattern()
889bool QSvgPattern::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
896 static QImage checkerPattern;
898 if (checkerPattern.isNull()) {
899 checkerPattern = QImage(QSize(8, 8), QImage::Format_ARGB32);
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);
907 return checkerPattern;
910QImage QSvgPattern::patternImage(QPainter *p, QSvgExtraStates &states,
const QSvgNode *patternElement)
913 QRectF peBoundingBox;
914 QRectF peWorldBoundingBox;
916 QTransform t = p->transform();
918 peBoundingBox = patternElement->internalBounds(p, states);
919 peWorldBoundingBox = t.mapRect(peBoundingBox);
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();
935 contentScaleFactorX *= peWorldBoundingBox.width();
936 contentScaleFactorY *= peWorldBoundingBox.height();
940 QRectF patternBoundingBox = m_rect.resolveRelativeLengths(peBoundingBox);
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())
948 calculateAppliedTransform(t, peBoundingBox, imageSize);
949 if (document()->isCalculatingImplicitViewBox())
950 return QImage(imageSize, QImage::Format_ARGB32);
952 return renderPattern(imageSize, contentScaleFactorX, contentScaleFactorY);
955QSvgNode::Type QSvgPattern::type()
const
960QImage QSvgPattern::renderPattern(QSize size, qreal contentScaleX, qreal contentScaleY)
962 if (size.isEmpty() || !qIsFinite(contentScaleX) || !qIsFinite(contentScaleY))
963 return defaultPattern();
967 if (!QImageIOHandler::allocateImage(size, QImage::Format_ARGB32, &pattern)) {
968 qCWarning(lcSvgDraw) <<
"The requested pattern size is too big, ignoring";
969 return defaultPattern();
971 pattern.fill(Qt::transparent);
974 qCWarning(lcSvgDraw) <<
"The pattern is trying to render itself recursively. "
975 "Returning a transparent QImage of the right size.";
978 QScopedValueRollback<
bool> guard(m_isRendering,
true);
981 QPainter patternPainter(&pattern);
982 QSvgExtraStates patternStates;
983 initPainter(&patternPainter);
984 applyStyleRecursive(&patternPainter, patternStates);
985 patternPainter.resetTransform();
989 if (m_viewBox.isNull())
990 patternPainter.scale(contentScaleX, contentScaleY);
992 patternPainter.setWindow(m_viewBox.toRect());
996 for (
const auto &node : renderers())
997 node->draw(&patternPainter, patternStates);
999 revertStyleRecursive(&patternPainter, patternStates);
1003void QSvgPattern::calculateAppliedTransform(QTransform &worldTransform, QRectF peLocalBB, QSize imageSize)
1018 m_appliedTransform.reset();
1019 qreal imageDownScaleFactorX = 1 / worldTransform.m11();
1020 qreal imageDownScaleFactorY = 1 / worldTransform.m22();
1022 m_appliedTransform.scale(qIsFinite(imageDownScaleFactorX) ? imageDownScaleFactorX : 1.0,
1023 qIsFinite(imageDownScaleFactorY) ? imageDownScaleFactorY : 1.0);
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());
1029 QPointF translation = m_rect.translationRelativeToBoundingBox(peLocalBB);
1030 m_appliedTransform.translate(translation.x() * worldTransform.m11(), translation.y() * worldTransform.m22());
1032 QTransform scalelessTransform = m_transform;
1033 scalelessTransform.scale(1 / m_transform.m11(), 1 / m_transform.m22());
1035 m_appliedTransform = m_appliedTransform * scalelessTransform;
The QPolygonF class provides a list of points using floating point precision.
Combined button and popup list for selecting options.
#define QStringLiteral(str)
static QImage & defaultPattern()
static bool isSupportedSvgExtension(const QString &)
static bool isSupportedSvgFeature(QStringView str)