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(QSvgDocument *doc, std::unique_ptr<QSvgNode> child,
76 doc->addNamedNode(id, child.get());
78 m_renderers.push_back(std::move(child));
81QSvgDefs::QSvgDefs(QSvgNode *parent)
82 : QSvgStructureNode(parent)
89bool QSvgDefs::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
94QSvgNode::Type QSvgDefs::type()
const
99QSvgSymbolLike::QSvgSymbolLike(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
100 QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow)
101 : QSvgStructureNode(parent)
105 , m_pAspectRatios(pAspectRatios)
106 , m_overflow(overflow)
111QSvgSymbolLike::~QSvgSymbolLike()
114QRectF QSvgSymbolLike::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
117 setPainterToRectAndAdjustment(p);
118 QRectF rect = internalBounds(p, states);
123bool QSvgSymbolLike::requiresGroupRendering()
const
125 return m_renderers.size() > 1;
128QRectF QSvgSymbolLike::clipRect()
const
130 if (m_overflow != Overflow::Hidden || !m_viewBox.isValid())
134 if (m_rect.width() > 0 && m_viewBox.width() > 0)
135 scaleX = m_rect.width() / m_viewBox.width();
137 if (m_rect.height() > 0 && m_viewBox.height() > 0)
138 scaleY = m_rect.height() / m_viewBox.height();
141 t.translate(- m_refP.x() * scaleX - m_rect.left() - m_viewBox.left() * scaleX,
142 - m_refP.y() * scaleY - m_rect.top() - m_viewBox.top() * scaleY);
143 t.scale(scaleX, scaleY);
145 return t.mapRect(m_viewBox);
148QTransform QSvgSymbolLike::aspectRatioTransform()
const
156 if (m_rect.width() > 0 && m_viewBox.width() > 0)
157 scaleX = m_rect.width() / m_viewBox.width();
159 if (m_rect.height() > 0 && m_viewBox.height() > 0)
160 scaleY = m_rect.height() / m_viewBox.height();
162 if (!qFuzzyCompare(scaleX, scaleY) &&
163 m_pAspectRatios.testAnyFlag(PreserveAspectRatio::xyMask)) {
165 if (m_pAspectRatios.testAnyFlag(PreserveAspectRatio::meet))
166 scaleX = scaleY = qMin(scaleX, scaleY);
168 scaleX = scaleY = qMax(scaleX, scaleY);
170 qreal xOverflow = scaleX * m_viewBox.width() - m_rect.width();
171 qreal yOverflow = scaleY * m_viewBox.height() - m_rect.height();
173 if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMid)
174 offsetX -= xOverflow / 2.;
175 else if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMax)
176 offsetX -= xOverflow;
178 if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMid)
179 offsetY -= yOverflow / 2.;
180 else if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMax)
181 offsetY -= yOverflow;
184 xform.translate(offsetX - m_refP.x() * scaleX, offsetY - m_refP.y() * scaleY);
185 xform.scale(scaleX, scaleY);
190void QSvgSymbolLike::setPainterToRectAndAdjustment(QPainter *p)
const
192 QRectF clip = clipRect();
194 p->setClipRect(clip);
196 p->setTransform(aspectRatioTransform(),
true);
199QSvgSymbol::QSvgSymbol(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
200 QSvgSymbol::PreserveAspectRatios pAspectRatios,
201 QSvgSymbol::Overflow overflow)
202 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
206QSvgSymbol::~QSvgSymbol()
209void QSvgSymbol::drawCommand(QPainter *p, QSvgExtraStates &states)
215 setPainterToRectAndAdjustment(p);
216 for (
const auto &node : renderers()) {
217 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
218 node->draw(p, states);
223QSvgNode::Type QSvgSymbol::type()
const
228QSvgMarker::QSvgMarker(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
229 QSvgSymbol::PreserveAspectRatios pAspectRatios, QSvgSymbol::Overflow overflow,
230 Orientation orientation, qreal orientationAngle, MarkerUnits markerUnits)
231 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
232 , m_orientation(orientation)
233 , m_orientationAngle(orientationAngle)
234 , m_markerUnits(markerUnits)
237 QSvgFillStylePtr fillProp = std::make_unique<QSvgFillStyle>();
238 fillProp->setBrush(Qt::black);
239 appendStyleProperty(std::move(fillProp));
241 QSvgStrokeStylePtr strokeProp = std::make_unique<QSvgStrokeStyle>();
242 strokeProp->setMiterLimit(4);
243 strokeProp->setWidth(1);
244 strokeProp->setLineCap(Qt::FlatCap);
245 strokeProp->setLineJoin(Qt::SvgMiterJoin);
246 strokeProp->setStroke(Qt::NoBrush);
247 appendStyleProperty(std::move(strokeProp));
250QSvgMarker::~QSvgMarker()
253QSvgFilterContainer::QSvgFilterContainer(QSvgNode *parent,
const QSvgRectF &bounds,
254 QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits)
255 : QSvgStructureNode(parent)
257 , m_filterUnits(filterUnits)
258 , m_primitiveUnits(primitiveUnits)
264QSvgFilterContainer::~QSvgFilterContainer()
267bool QSvgFilterContainer::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
272void QSvgMarker::drawCommand(QPainter *p, QSvgExtraStates &states)
277 if (Q_UNLIKELY(m_recursing))
279 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
282 setPainterToRectAndAdjustment(p);
284 for (
const auto &node : renderers()) {
285 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
286 node->draw(p, states);
293struct PositionMarkerPair {
298 bool isStartNode =
false;
301QList<PositionMarkerPair> markersForNode(
const QSvgNode *node)
303 if (!node->hasAnyMarker())
306 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
307 QPointF t1 = p1 - p0;
308 QPointF t2 = p2 - p1;
309 qreal hyp1 = hypot(t1.x(), t1.y());
314 qreal hyp2 = hypot(t2.x(), t2.y());
319 QPointF tangent = t1 + t2;
320 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
323 QList<PositionMarkerPair> markers;
325 switch (node->type()) {
326 case QSvgNode::Line: {
327 const QSvgLine *line =
static_cast<
const QSvgLine*>(node);
328 if (node->hasMarkerStart())
329 markers << PositionMarkerPair { line->line().p1().x(), line->line().p1().y(),
330 line->line().angle(), line->markerStartId(),
332 if (node->hasMarkerEnd())
333 markers << PositionMarkerPair { line->line().p2().x(), line->line().p2().y(),
334 line->line().angle(), line->markerEndId() };
337 case QSvgNode::Polyline:
338 case QSvgNode::Polygon: {
339 const QPolygonF &polyData = (node->type() == QSvgNode::Polyline)
340 ?
static_cast<
const QSvgPolyline*>(node)->polygon()
341 :
static_cast<
const QSvgPolygon*>(node)->polygon();
343 if (node->hasMarkerStart() && polyData.size() > 1) {
344 QLineF line(polyData.at(0), polyData.at(1));
345 markers << PositionMarkerPair { line.p1().x(),
348 node->markerStartId(),
351 if (node->hasMarkerMid()) {
352 for (
int i = 1; i < polyData.size() - 1; i++) {
353 QPointF p0 = polyData.at(i - 1);
354 QPointF p1 = polyData.at(i);
355 QPointF p2 = polyData.at(i + 1);
357 markers << PositionMarkerPair { p1.x(),
359 getMeanAngle(p0, p1, p2),
360 node->markerStartId() };
363 if (node->hasMarkerEnd() && polyData.size() > 1) {
364 QLineF line(polyData.at(polyData.size() - 1), polyData.last());
365 markers << PositionMarkerPair { line.p2().x(),
368 node->markerEndId() };
372 case QSvgNode::Path: {
373 const QSvgPath *path =
static_cast<
const QSvgPath*>(node);
374 if (node->hasMarkerStart())
375 markers << PositionMarkerPair { path->path().pointAtPercent(0.).x(),
376 path->path().pointAtPercent(0.).y(),
377 path->path().angleAtPercent(0.),
378 path->markerStartId(),
380 if (node->hasMarkerMid()) {
381 for (
int i = 1; i < path->path().elementCount() - 1; i++) {
382 if (path->path().elementAt(i).type == QPainterPath::MoveToElement)
384 if (path->path().elementAt(i).type == QPainterPath::CurveToElement)
386 if (( path->path().elementAt(i).type == QPainterPath::CurveToDataElement &&
387 path->path().elementAt(i + 1).type != QPainterPath::CurveToDataElement ) ||
388 path->path().elementAt(i).type == QPainterPath::LineToElement) {
390 QPointF p0(path->path().elementAt(i - 1).x, path->path().elementAt(i - 1).y);
391 QPointF p1(path->path().elementAt(i).x, path->path().elementAt(i).y);
392 QPointF p2(path->path().elementAt(i + 1).x, path->path().elementAt(i + 1).y);
394 markers << PositionMarkerPair { p1.x(),
396 getMeanAngle(p0, p1, p2),
397 path->markerMidId() };
401 if (node->hasMarkerEnd())
402 markers << PositionMarkerPair { path->path().pointAtPercent(1.).x(),
403 path->path().pointAtPercent(1.).y(),
404 path->path().angleAtPercent(1.),
405 path->markerEndId() };
419void QSvgMarker::drawMarkersForNode(QSvgNode *node, QPainter *p, QSvgExtraStates &states)
421 drawHelper(node, p, states);
424QRectF QSvgMarker::markersBoundsForNode(
const QSvgNode *node, QPainter *p, QSvgExtraStates &states)
427 drawHelper(node, p, states, &bounds);
431QSvgNode::Type QSvgMarker::type()
const
436void QSvgMarker::drawHelper(
const QSvgNode *node, QPainter *p,
437 QSvgExtraStates &states, QRectF *boundingRect)
439 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
441 const bool isPainting = (boundingRect ==
nullptr);
442 const auto markers = markersForNode(node);
443 for (
auto &i : markers) {
444 QSvgNode *referencedNode = states.doc()->namedNode(i.markerId);
445 if (!referencedNode || referencedNode->type() != QSvgNode::Marker)
447 QSvgMarker *markNode =
static_cast<QSvgMarker *>(referencedNode);
450 p->translate(i.x, i.y);
451 if (markNode->orientation() == QSvgMarker::Orientation::Value) {
452 p->rotate(markNode->orientationAngle());
455 if (i.isStartNode && markNode->orientation()
456 == QSvgMarker::Orientation::AutoStartReverse) {
460 QRectF oldRect = markNode->m_rect;
461 if (markNode->markerUnits() == QSvgMarker::MarkerUnits::StrokeWidth) {
462 markNode->m_rect.setWidth(markNode->m_rect.width() * p->pen().widthF());
463 markNode->m_rect.setHeight(markNode->m_rect.height() * p->pen().widthF());
466 markNode->draw(p, states);
469 QTransform xf = p->transform();
471 *boundingRect |= xf.mapRect(markNode->decoratedInternalBounds(p, states));
474 markNode->m_rect = oldRect;
479QImage QSvgFilterContainer::applyFilter(
const QImage &buffer, QPainter *p,
const QRectF &bounds)
const
481 QRectF localFilterRegion = m_rect.resolveRelativeLengths(bounds, m_filterUnits);
482 QRect globalFilterRegion = p->transform().mapRect(localFilterRegion).toRect();
483 QRect globalFilterRegionRel = globalFilterRegion.translated(-buffer.offset());
485 if (globalFilterRegionRel.isEmpty())
489 if (!QImageIOHandler::allocateImage(globalFilterRegionRel.size(), buffer.format(), &proxy)) {
490 qCWarning(lcSvgDraw) <<
"The requested filter is too big, ignoring";
493 proxy = buffer.copy(globalFilterRegionRel);
494 proxy.setOffset(globalFilterRegion.topLeft());
498 QMap<QString, QImage> buffers;
502 bool requiresSourceAlpha =
false;
504 for (
const auto &node : renderers()) {
505 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
506 if (filter && filter->requiresSourceAlpha()) {
507 requiresSourceAlpha =
true;
512 if (requiresSourceAlpha) {
513 QImage proxyAlpha = proxy.convertedTo(QImage::Format_Alpha8).convertedTo(proxy.format());
514 proxyAlpha.setOffset(proxy.offset());
515 if (proxyAlpha.isNull())
521 for (
const auto &node : renderers()) {
522 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
524 result = filter->apply(buffers, p, bounds, localFilterRegion, m_primitiveUnits, m_filterUnits);
525 if (!result.isNull()) {
527 buffers[filter->result()] = result;
534void QSvgFilterContainer::setSupported(
bool supported)
536 m_supported = supported;
539bool QSvgFilterContainer::supported()
const
544QRectF QSvgFilterContainer::filterRegion(
const QRectF &itemBounds)
const
546 return m_rect.resolveRelativeLengths(itemBounds, m_filterUnits);
549QSvgNode::Type QSvgFilterContainer::type()
const
557 constexpr auto prefix_1_2 =
"http://www.w3.org/Graphics/SVG/feature/1.2/#"_L1;
558 if (str.startsWith(prefix_1_2)) {
559 const auto suffix = str.sliced(prefix_1_2.size());
561 constexpr auto features = qOffsetStringArray(
575 "ConditionalProcessing",
580 "ConditionalProcessingAttribute",
581 "ExternalResourcesRequiredAttribute"
587 for (
int i = 0; i < features.count(); ++i)
588 if (suffix == QLatin1StringView{features.at(i)})
601QSvgSwitch::QSvgSwitch(QSvgNode *parent)
602 : QSvgStructureNode(parent)
607QSvgSwitch::~QSvgSwitch()
610QSvgNode *QSvgSwitch::childToRender()
const
612 for (
const auto &node : renderers()) {
613 if (node->isVisible() && (node->displayMode() != QSvgNode::NoneMode)) {
614 const QStringList &features = node->requiredFeatures();
615 const QStringList &extensions = node->requiredExtensions();
616 const QStringList &languages = node->requiredLanguages();
617 const QStringList &formats = node->requiredFormats();
618 const QStringList &fonts = node->requiredFonts();
620 bool okToRender =
true;
621 if (!features.isEmpty()) {
622 QStringList::const_iterator sitr = features.constBegin();
623 for (; sitr != features.constEnd(); ++sitr) {
624 if (!isSupportedSvgFeature(*sitr)) {
631 if (okToRender && !extensions.isEmpty()) {
632 QStringList::const_iterator sitr = extensions.constBegin();
633 for (; sitr != extensions.constEnd(); ++sitr) {
634 if (!isSupportedSvgExtension(*sitr)) {
641 if (okToRender && !languages.isEmpty()) {
642 QStringList::const_iterator sitr = languages.constBegin();
644 for (; sitr != languages.constEnd(); ++sitr) {
645 if ((*sitr).startsWith(m_systemLanguagePrefix)) {
652 if (okToRender && !formats.isEmpty())
655 if (okToRender && !fonts.isEmpty())
666void QSvgSwitch::drawCommand(QPainter *p, QSvgExtraStates &states)
668 QSvgNode *node = childToRender();
670 node->draw(p, states);
673QSvgNode::Type QSvgSwitch::type()
const
678void QSvgSwitch::init()
681 m_systemLanguage = locale.name().replace(QLatin1Char(
'_'), QLatin1Char(
'-'));
682 int idx = m_systemLanguage.indexOf(QLatin1Char(
'-'));
683 m_systemLanguagePrefix = m_systemLanguage.mid(0, idx);
686QRectF QSvgStructureNode::internalBounds(QPainter *p, QSvgExtraStates &states)
const
690 QScopedValueRollback<
bool> guard(m_recursing,
true);
691 for (
const auto &node : renderers())
692 bounds |= node->bounds(p, states);
697QRectF QSvgStructureNode::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
701 QScopedValueRollback<
bool> guard(m_recursing,
true);
702 for (
const auto &node : renderers())
703 bounds |= node->decoratedBounds(p, states);
708QSvgNode* QSvgStructureNode::previousSiblingNode(QSvgNode *n)
const
710 QSvgNode *prev =
nullptr;
711 for (
const auto &node : renderers()) {
719void QSvgStructureNode::releaseDescendants()
726 QSvgNode *currentParent =
this;
727 while (currentParent) {
728 switch (currentParent->type()) {
731 case QSvgNode::Group:
733 case QSvgNode::Pattern:
734 case QSvgNode::Symbol:
735 case QSvgNode::Switch:
736 case QSvgNode::Filter:
738 QSvgStructureNode *currentParentSN =
static_cast<QSvgStructureNode *>(currentParent);
739 if (currentParentSN->m_renderers.empty()) {
740 currentParent = currentParent->parent();
742 static_cast<QSvgStructureNode *>(currentParent)->m_renderers.pop_front();
744 Q_ASSERT(currentParentSN->m_renderers.front().get()->parent() == currentParent);
745 currentParent = currentParentSN->m_renderers.front().get();
750 currentParent = currentParent->parent();
752 static_cast<QSvgStructureNode *>(currentParent)->m_renderers.pop_front();
756 Q_ASSERT(
this->m_renderers.empty());
759QSvgMask::QSvgMask(QSvgNode *parent, QSvgRectF bounds,
760 QtSvg::UnitTypes contentUnits)
761 : QSvgStructureNode(parent)
763 , m_contentUnits(contentUnits)
770bool QSvgMask::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
775QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect)
const
777 QTransform t = p->transform();
779 QRectF basicRect = targetNode->internalBounds(p, states);
780 *globalRect = t.mapRect(basicRect);
782 return createMask(p, states, basicRect, globalRect);
785QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states,
const QRectF &localRect, QRectF *globalRect)
const
787 QRect imageBound = globalRect->toAlignedRect();
788 *globalRect = imageBound.toRectF();
791 if (!QImageIOHandler::allocateImage(imageBound.size(), QImage::Format_RGBA8888, &mask)) {
792 qCWarning(lcSvgDraw) <<
"The requested mask size is too big, ignoring";
796 if (Q_UNLIKELY(m_recursing))
798 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
801 if (
this->hasMask()) {
802 QSvgNode *referencedNode = states.doc()->namedNode(
this->maskId());
803 if (referencedNode && referencedNode->type() == QSvgNode::Mask) {
804 QSvgMask *maskNode =
static_cast<QSvgMask *>(referencedNode);
806 return maskNode->createMask(p, states, localRect, &boundsRect);
816 mask.fill(Qt::transparent);
817 QPainter painter(&mask);
818 initPainter(&painter);
820 QSvgExtraStates maskNodeStates(states.doc());
821 maskNodeStates.trustedSource = states.trustedSource;
822 maskNodeStates.remainingNestedNodes = states.remainingNestedNodes;
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 (states.doc()->isCalculatingImplicitViewBox())
950 return QImage(imageSize, QImage::Format_ARGB32);
952 return renderPattern(imageSize, std::make_pair(contentScaleFactorX, contentScaleFactorY), states);
955QSvgNode::Type QSvgPattern::type()
const
960QImage QSvgPattern::renderPattern(QSize size, std::pair<qreal, qreal> scale,
const QSvgExtraStates &states)
962 if (size.isEmpty() || !qIsFinite(scale.first) || !qIsFinite(scale.second))
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(states.doc());
983 patternStates.trustedSource = states.trustedSource;
984 patternStates.remainingNestedNodes = states.remainingNestedNodes;
985 initPainter(&patternPainter);
986 applyStyleRecursive(&patternPainter, patternStates);
987 patternPainter.resetTransform();
991 if (m_viewBox.isNull())
992 patternPainter.scale(scale.first, scale.second);
994 patternPainter.setWindow(m_viewBox.toRect());
997 for (
const auto &node : renderers())
998 node->draw(&patternPainter, patternStates);
1000 revertStyleRecursive(&patternPainter, patternStates);
1004void QSvgPattern::calculateAppliedTransform(QTransform &worldTransform, QRectF peLocalBB, QSize imageSize)
1019 m_appliedTransform.reset();
1020 qreal imageDownScaleFactorX = 1 / worldTransform.m11();
1021 qreal imageDownScaleFactorY = 1 / worldTransform.m22();
1023 m_appliedTransform.scale(qIsFinite(imageDownScaleFactorX) ? imageDownScaleFactorX : 1.0,
1024 qIsFinite(imageDownScaleFactorY) ? imageDownScaleFactorY : 1.0);
1026 QRectF p = m_rect.resolveRelativeLengths(peLocalBB);
1027 m_appliedTransform.scale((p.width() * worldTransform.m11() * m_transform.m11()) / imageSize.width(),
1028 (p.height() * worldTransform.m22() * m_transform.m22()) / imageSize.height());
1030 QPointF translation = m_rect.translationRelativeToBoundingBox(peLocalBB);
1031 m_appliedTransform.translate(translation.x() * worldTransform.m11(), translation.y() * worldTransform.m22());
1033 QTransform scalelessTransform = m_transform;
1034 scalelessTransform.scale(1 / m_transform.m11(), 1 / m_transform.m22());
1036 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)