17#include <QLoggingCategory>
18#include <qscopedvaluerollback.h>
19#include <QtGui/qimageiohandler.h>
23QSvgG::QSvgG(QSvgNode *parent)
24 : QSvgStructureNode(parent)
29QSvgStructureNode::~QSvgStructureNode()
31 qDeleteAll(m_renderers);
34void QSvgG::drawCommand(QPainter *p, QSvgExtraStates &states)
36 QList<QSvgNode*>::iterator itr = m_renderers.begin();
37 while (itr != m_renderers.end()) {
38 QSvgNode *node = *itr;
39 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
40 node->draw(p, states);
45bool QSvgG::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
50QSvgNode::Type QSvgG::type()
const
55bool QSvgG::requiresGroupRendering()
const
57 return m_renderers.count() > 1;
60QSvgStructureNode::QSvgStructureNode(QSvgNode *parent)
66QSvgNode * QSvgStructureNode::scopeNode(
const QString &id)
const
68 QSvgTinyDocument *doc = document();
69 return doc ? doc->namedNode(id) : 0;
72void QSvgStructureNode::addChild(QSvgNode *child,
const QString &id)
74 m_renderers.append(child);
79 QSvgTinyDocument *doc = document();
81 doc->addNamedNode(id, child);
84QSvgDefs::QSvgDefs(QSvgNode *parent)
85 : 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)
111QRectF QSvgSymbolLike::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
114 setPainterToRectAndAdjustment(p);
115 QRectF rect = internalBounds(p, states);
120bool QSvgSymbolLike::requiresGroupRendering()
const
122 return m_renderers.count() > 1;
125void QSvgSymbolLike::setPainterToRectAndAdjustment(QPainter *p)
const
128 if (m_rect.width() > 0 && m_viewBox.width() > 0)
129 scaleX = m_rect.width()/m_viewBox.width();
131 if (m_rect.height() > 0 && m_viewBox.height() > 0)
132 scaleY = m_rect.height()/m_viewBox.height();
134 if (m_overflow == Overflow::Hidden) {
136 t.translate(- m_refP.x() * scaleX - m_rect.left() - m_viewBox.left() * scaleX,
137 - m_refP.y() * scaleY - m_rect.top() - m_viewBox.top() * scaleY);
138 t.scale(scaleX, scaleY);
140 if (m_viewBox.isValid())
141 p->setClipRect(t.mapRect(m_viewBox));
147 if (!qFuzzyCompare(scaleX, scaleY) &&
148 m_pAspectRatios.testAnyFlag(PreserveAspectRatio::xyMask)) {
150 if (m_pAspectRatios.testAnyFlag(PreserveAspectRatio::meet))
151 scaleX = scaleY = qMin(scaleX, scaleY);
153 scaleX = scaleY = qMax(scaleX, scaleY);
155 qreal xOverflow = scaleX * m_viewBox.width() - m_rect.width();
156 qreal yOverflow = scaleY * m_viewBox.height() - m_rect.height();
158 if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMid)
159 offsetX -= xOverflow / 2.;
160 else if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMax)
161 offsetX -= xOverflow;
163 if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMid)
164 offsetY -= yOverflow / 2.;
165 else if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMax)
166 offsetY -= yOverflow;
169 p->translate(offsetX - m_refP.x() * scaleX, offsetY - m_refP.y() * scaleY);
170 p->scale(scaleX, scaleY);
173QSvgSymbol::QSvgSymbol(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
174 QSvgSymbol::PreserveAspectRatios pAspectRatios,
175 QSvgSymbol::Overflow overflow)
176 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
180void QSvgSymbol::drawCommand(QPainter *p, QSvgExtraStates &states)
185 QList<QSvgNode*>::iterator itr = m_renderers.begin();
188 setPainterToRectAndAdjustment(p);
189 while (itr != m_renderers.end()) {
190 QSvgNode *node = *itr;
191 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
192 node->draw(p, states);
198QSvgNode::Type QSvgSymbol::type()
const
203QSvgMarker::QSvgMarker(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
204 QSvgSymbol::PreserveAspectRatios pAspectRatios, QSvgSymbol::Overflow overflow,
205 Orientation orientation, qreal orientationAngle, MarkerUnits markerUnits)
206 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
207 , m_orientation(orientation)
208 , m_orientationAngle(orientationAngle)
209 , m_markerUnits(markerUnits)
212 QSvgFillStyle *fillProp =
new QSvgFillStyle();
213 fillProp->setBrush(Qt::black);
216 QSvgStrokeStyle *strokeProp =
new QSvgStrokeStyle();
217 strokeProp->setMiterLimit(4);
218 strokeProp->setWidth(1);
219 strokeProp->setLineCap(Qt::FlatCap);
220 strokeProp->setLineJoin(Qt::SvgMiterJoin);
221 strokeProp->setStroke(Qt::NoBrush);
225QSvgFilterContainer::QSvgFilterContainer(QSvgNode *parent,
const QSvgRectF &bounds,
226 QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits)
227 : QSvgStructureNode(parent)
229 , m_filterUnits(filterUnits)
230 , m_primitiveUnits(primitiveUnits)
236bool QSvgFilterContainer::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
241void QSvgMarker::drawCommand(QPainter *p, QSvgExtraStates &states)
246 if (Q_UNLIKELY(m_recursing))
248 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
250 QList<QSvgNode*>::iterator itr = m_renderers.begin();
253 setPainterToRectAndAdjustment(p);
255 while (itr != m_renderers.end()) {
256 QSvgNode *node = *itr;
257 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
258 node->draw(p, states);
266struct PositionMarkerPair {
271 bool isStartNode =
false;
274QList<PositionMarkerPair> markersForNode(
const QSvgNode *node)
276 if (!node->hasAnyMarker())
279 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
280 QPointF t1 = p1 - p0;
281 QPointF t2 = p2 - p1;
282 qreal hyp1 = hypot(t1.x(), t1.y());
287 qreal hyp2 = hypot(t2.x(), t2.y());
292 QPointF tangent = t1 + t2;
293 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
296 QList<PositionMarkerPair> markers;
298 switch (node->type()) {
299 case QSvgNode::Line: {
300 const QSvgLine *line =
static_cast<
const QSvgLine*>(node);
301 if (node->hasMarkerStart())
302 markers << PositionMarkerPair { line->line().p1().x(), line->line().p1().y(),
303 line->line().angle(), line->markerStartId(),
305 if (node->hasMarkerEnd())
306 markers << PositionMarkerPair { line->line().p2().x(), line->line().p2().y(),
307 line->line().angle(), line->markerEndId() };
310 case QSvgNode::Polyline:
311 case QSvgNode::Polygon: {
312 const QPolygonF &polyData = (node->type() == QSvgNode::Polyline)
313 ?
static_cast<
const QSvgPolyline*>(node)->polygon()
314 :
static_cast<
const QSvgPolygon*>(node)->polygon();
316 if (node->hasMarkerStart() && polyData.size() > 1) {
317 QLineF line(polyData.at(0), polyData.at(1));
318 markers << PositionMarkerPair { line.p1().x(),
321 node->markerStartId(),
324 if (node->hasMarkerMid()) {
325 for (
int i = 1; i < polyData.size() - 1; i++) {
326 QPointF p0 = polyData.at(i - 1);
327 QPointF p1 = polyData.at(i);
328 QPointF p2 = polyData.at(i + 1);
330 markers << PositionMarkerPair { p1.x(),
332 getMeanAngle(p0, p1, p2),
333 node->markerStartId() };
336 if (node->hasMarkerEnd() && polyData.size() > 1) {
337 QLineF line(polyData.at(polyData.size() - 1), polyData.last());
338 markers << PositionMarkerPair { line.p2().x(),
341 node->markerEndId() };
345 case QSvgNode::Path: {
346 const QSvgPath *path =
static_cast<
const QSvgPath*>(node);
347 if (node->hasMarkerStart())
348 markers << PositionMarkerPair { path->path().pointAtPercent(0.).x(),
349 path->path().pointAtPercent(0.).y(),
350 path->path().angleAtPercent(0.),
351 path->markerStartId(),
353 if (node->hasMarkerMid()) {
354 for (
int i = 1; i < path->path().elementCount() - 1; i++) {
355 if (path->path().elementAt(i).type == QPainterPath::MoveToElement)
357 if (path->path().elementAt(i).type == QPainterPath::CurveToElement)
359 if (( path->path().elementAt(i).type == QPainterPath::CurveToDataElement &&
360 path->path().elementAt(i + 1).type != QPainterPath::CurveToDataElement ) ||
361 path->path().elementAt(i).type == QPainterPath::LineToElement) {
363 QPointF p0(path->path().elementAt(i - 1).x, path->path().elementAt(i - 1).y);
364 QPointF p1(path->path().elementAt(i).x, path->path().elementAt(i).y);
365 QPointF p2(path->path().elementAt(i + 1).x, path->path().elementAt(i + 1).y);
367 markers << PositionMarkerPair { p1.x(),
369 getMeanAngle(p0, p1, p2),
370 path->markerMidId() };
374 if (node->hasMarkerEnd())
375 markers << PositionMarkerPair { path->path().pointAtPercent(1.).x(),
376 path->path().pointAtPercent(1.).y(),
377 path->path().angleAtPercent(1.),
378 path->markerEndId() };
392void QSvgMarker::drawMarkersForNode(QSvgNode *node, QPainter *p, QSvgExtraStates &states)
394 drawHelper(node, p, states);
397QRectF QSvgMarker::markersBoundsForNode(
const QSvgNode *node, QPainter *p, QSvgExtraStates &states)
400 drawHelper(node, p, states, &bounds);
404QSvgNode::Type QSvgMarker::type()
const
409void QSvgMarker::drawHelper(
const QSvgNode *node, QPainter *p,
410 QSvgExtraStates &states, QRectF *boundingRect)
412 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
414 const bool isPainting = (boundingRect ==
nullptr);
415 const auto markers = markersForNode(node);
416 for (
auto &i : markers) {
417 QSvgMarker *markNode =
static_cast<QSvgMarker*>(node->document()->namedNode(i.markerId));
422 p->translate(i.x, i.y);
423 if (markNode->orientation() == QSvgMarker::Orientation::Value) {
424 p->rotate(markNode->orientationAngle());
427 if (i.isStartNode && markNode->orientation()
428 == QSvgMarker::Orientation::AutoStartReverse) {
432 QRectF oldRect = markNode->m_rect;
433 if (markNode->markerUnits() == QSvgMarker::MarkerUnits::StrokeWidth) {
434 markNode->m_rect.setWidth(markNode->m_rect.width() * p->pen().widthF());
435 markNode->m_rect.setHeight(markNode->m_rect.height() * p->pen().widthF());
438 markNode->draw(p, states);
441 QTransform xf = p->transform();
443 *boundingRect |= xf.mapRect(markNode->decoratedInternalBounds(p, states));
446 markNode->m_rect = oldRect;
451QImage QSvgFilterContainer::applyFilter(
const QImage &buffer, QPainter *p,
const QRectF &bounds)
const
453 QRectF localFilterRegion = m_rect.resolveRelativeLengths(bounds, m_filterUnits);
454 QRect globalFilterRegion = p->transform().mapRect(localFilterRegion).toRect();
455 QRect globalFilterRegionRel = globalFilterRegion.translated(-buffer.offset());
457 if (globalFilterRegionRel.isEmpty())
461 if (!QImageIOHandler::allocateImage(globalFilterRegionRel.size(), buffer.format(), &proxy)) {
462 qCWarning(lcSvgDraw) <<
"The requested filter is too big, ignoring";
465 proxy = buffer.copy(globalFilterRegionRel);
466 proxy.setOffset(globalFilterRegion.topLeft());
470 QMap<QString, QImage> buffers;
474 bool requiresSourceAlpha =
false;
476 const QList<QSvgNode *> children = renderers();
477 for (
const QSvgNode *renderer : children) {
478 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(renderer);
479 if (filter && filter->requiresSourceAlpha()) {
480 requiresSourceAlpha =
true;
485 if (requiresSourceAlpha) {
486 QImage proxyAlpha = proxy.convertedTo(QImage::Format_Alpha8).convertedTo(proxy.format());
487 proxyAlpha.setOffset(proxy.offset());
488 if (proxyAlpha.isNull())
494 for (
const QSvgNode *renderer : children) {
495 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(renderer);
497 result = filter->apply(buffers, p, bounds, localFilterRegion, m_primitiveUnits, m_filterUnits);
498 if (!result.isNull()) {
500 buffers[filter->result()] = result;
507void QSvgFilterContainer::setSupported(
bool supported)
509 m_supported = supported;
512bool QSvgFilterContainer::supported()
const
517QRectF QSvgFilterContainer::filterRegion(
const QRectF &itemBounds)
const
519 return m_rect.resolveRelativeLengths(itemBounds, m_filterUnits);
522QSvgNode::Type QSvgFilterContainer::type()
const
530 static const QStringList wordList = {
531 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Text"),
532 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Shape"),
533 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#SVG"),
534 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Structure"),
535 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#SolidColor"),
536 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Hyperlinking"),
537 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#CoreAttribute"),
538 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#XlinkAttribute"),
539 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#SVG-static"),
540 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#OpacityAttribute"),
541 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Gradient"),
542 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Font"),
543 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Image"),
544 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#ConditionalProcessing"),
545 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Extensibility"),
546 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#GraphicsAttribute"),
547 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Prefetch"),
548 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#PaintAttribute"),
549 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#ConditionalProcessingAttribute"),
550 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#ExternalResourcesRequiredAttribute")
553 return wordList.contains(str);
562QSvgSwitch::QSvgSwitch(QSvgNode *parent)
563 : QSvgStructureNode(parent)
568QSvgNode *QSvgSwitch::childToRender()
const
570 auto itr = m_renderers.begin();
572 while (itr != m_renderers.end()) {
573 QSvgNode *node = *itr;
574 if (node->isVisible() && (node->displayMode() != QSvgNode::NoneMode)) {
575 const QStringList &features = node->requiredFeatures();
576 const QStringList &extensions = node->requiredExtensions();
577 const QStringList &languages = node->requiredLanguages();
578 const QStringList &formats = node->requiredFormats();
579 const QStringList &fonts = node->requiredFonts();
581 bool okToRender =
true;
582 if (!features.isEmpty()) {
583 QStringList::const_iterator sitr = features.constBegin();
584 for (; sitr != features.constEnd(); ++sitr) {
585 if (!isSupportedSvgFeature(*sitr)) {
592 if (okToRender && !extensions.isEmpty()) {
593 QStringList::const_iterator sitr = extensions.constBegin();
594 for (; sitr != extensions.constEnd(); ++sitr) {
595 if (!isSupportedSvgExtension(*sitr)) {
602 if (okToRender && !languages.isEmpty()) {
603 QStringList::const_iterator sitr = languages.constBegin();
605 for (; sitr != languages.constEnd(); ++sitr) {
606 if ((*sitr).startsWith(m_systemLanguagePrefix)) {
613 if (okToRender && !formats.isEmpty())
616 if (okToRender && !fonts.isEmpty())
629void QSvgSwitch::drawCommand(QPainter *p, QSvgExtraStates &states)
631 QSvgNode *node = childToRender();
633 node->draw(p, states);
636QSvgNode::Type QSvgSwitch::type()
const
641void QSvgSwitch::init()
644 m_systemLanguage = locale.name().replace(QLatin1Char(
'_'), QLatin1Char(
'-'));
645 int idx = m_systemLanguage.indexOf(QLatin1Char(
'-'));
646 m_systemLanguagePrefix = m_systemLanguage.mid(0, idx);
649QRectF QSvgStructureNode::internalBounds(QPainter *p, QSvgExtraStates &states)
const
653 QScopedValueRollback<
bool> guard(m_recursing,
true);
654 for (QSvgNode *node : std::as_const(m_renderers))
655 bounds |= node->bounds(p, states);
660QRectF QSvgStructureNode::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
664 QScopedValueRollback<
bool> guard(m_recursing,
true);
665 for (QSvgNode *node : std::as_const(m_renderers))
666 bounds |= node->decoratedBounds(p, states);
671QSvgNode* QSvgStructureNode::previousSiblingNode(QSvgNode *n)
const
673 QSvgNode *prev =
nullptr;
674 QList<QSvgNode*>::const_iterator itr = m_renderers.constBegin();
675 for (; itr != m_renderers.constEnd(); ++itr) {
676 QSvgNode *node = *itr;
684QSvgMask::QSvgMask(QSvgNode *parent, QSvgRectF bounds,
685 QtSvg::UnitTypes contentUnits)
686 : QSvgStructureNode(parent)
688 , m_contentUnits(contentUnits)
692bool QSvgMask::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
697QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect)
const
699 QTransform t = p->transform();
701 QRectF basicRect = targetNode->internalBounds(p, states);
702 *globalRect = t.mapRect(basicRect);
704 return createMask(p, states, basicRect, globalRect);
707QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states,
const QRectF &localRect, QRectF *globalRect)
const
709 QRect imageBound = globalRect->toAlignedRect();
710 *globalRect = imageBound.toRectF();
713 if (!QImageIOHandler::allocateImage(imageBound.size(), QImage::Format_RGBA8888, &mask)) {
714 qCWarning(lcSvgDraw) <<
"The requested mask size is too big, ignoring";
718 if (Q_UNLIKELY(m_recursing))
720 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
723 if (
this->hasMask()) {
724 QSvgMask *maskNode =
static_cast<QSvgMask*>(document()->namedNode(
this->maskId()));
727 return maskNode->createMask(p, states, localRect, &boundsRect);
737 mask.fill(Qt::transparent);
738 QPainter painter(&mask);
739 initPainter(&painter);
741 QSvgExtraStates maskNodeStates;
742 applyStyleRecursive(&painter, maskNodeStates);
746 painter.resetTransform();
747 painter.translate(-imageBound.topLeft());
748 painter.setTransform(p->transform(),
true);
750 QTransform oldT = painter.transform();
751 if (m_contentUnits == QtSvg::UnitTypes::objectBoundingBox){
752 painter.translate(localRect.topLeft());
753 painter.scale(localRect.width(), localRect.height());
757 QList<QSvgNode*>::const_iterator itr = m_renderers.begin();
758 while (itr != m_renderers.end()) {
759 QSvgNode *node = *itr;
760 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
761 node->draw(&painter, maskNodeStates);
765 for (
int i=0; i < mask.height(); i++) {
766 QRgb *line =
reinterpret_cast<QRgb *>(mask.scanLine(i));
767 for (
int j=0; j < mask.width(); j++) {
768 const qreal rC = 0.2125, gC = 0.7154, bC = 0.0721;
769 int alpha = 255 - (qRed(line[j]) * rC + qGreen(line[j]) * gC + qBlue(line[j]) * bC) * qAlpha(line[j])/255.;
770 line[j] = qRgba(0, 0, 0, alpha);
778 QRectF clipRect = m_rect.resolveRelativeLengths(localRect);
779 QPainterPath clipPath;
780 clipPath.setFillRule(Qt::OddEvenFill);
781 clipPath.addRect(mask.rect().adjusted(-10, -10, 20, 20));
782 clipPath.addPolygon(oldT.map(QPolygonF(clipRect)));
783 painter.resetTransform();
784 painter.fillPath(clipPath, Qt::black);
785 revertStyleRecursive(&painter, maskNodeStates);
789QSvgNode::Type QSvgMask::type()
const
794QSvgPattern::QSvgPattern(QSvgNode *parent, QSvgRectF bounds, QRectF viewBox,
795 QtSvg::UnitTypes contentUnits, QTransform transform)
796 : QSvgStructureNode(parent),
799 m_contentUnits(contentUnits),
800 m_isRendering(
false),
801 m_transform(transform)
807bool QSvgPattern::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
814 static QImage checkerPattern;
816 if (checkerPattern.isNull()) {
817 checkerPattern = QImage(QSize(8, 8), QImage::Format_ARGB32);
819 p.fillRect(QRect(0, 0, 4, 4), QColorConstants::Svg::white);
820 p.fillRect(QRect(4, 0, 4, 4), QColorConstants::Svg::black);
821 p.fillRect(QRect(0, 4, 4, 4), QColorConstants::Svg::black);
822 p.fillRect(QRect(4, 4, 4, 4), QColorConstants::Svg::white);
825 return checkerPattern;
828QImage QSvgPattern::patternImage(QPainter *p, QSvgExtraStates &states,
const QSvgNode *patternElement)
831 QRectF peBoundingBox;
832 QRectF peWorldBoundingBox;
834 QTransform t = p->transform();
836 peBoundingBox = patternElement->internalBounds(p, states);
837 peWorldBoundingBox = t.mapRect(peBoundingBox);
847 qreal contentScaleFactorX = m_transform.m11();
848 qreal contentScaleFactorY = m_transform.m22();
849 if (m_contentUnits == QtSvg::UnitTypes::userSpaceOnUse) {
850 contentScaleFactorX *= t.m11();
851 contentScaleFactorY *= t.m22();
853 contentScaleFactorX *= peWorldBoundingBox.width();
854 contentScaleFactorY *= peWorldBoundingBox.height();
858 QRectF patternBoundingBox = m_rect.resolveRelativeLengths(peBoundingBox);
861 imageSize.setWidth(qCeil(patternBoundingBox.width() * t.m11() * m_transform.m11()));
862 imageSize.setHeight(qCeil(patternBoundingBox.height() * t.m22() * m_transform.m22()));
864 calculateAppliedTransform(t, peBoundingBox, imageSize);
865 if (document()->isCalculatingImplicitViewBox())
866 return QImage(imageSize, QImage::Format_ARGB32);
868 return renderPattern(imageSize, contentScaleFactorX, contentScaleFactorY);
871QSvgNode::Type QSvgPattern::type()
const
876QImage QSvgPattern::renderPattern(QSize size, qreal contentScaleX, qreal contentScaleY)
878 if (size.isEmpty() || !qIsFinite(contentScaleX) || !qIsFinite(contentScaleY))
879 return defaultPattern();
883 if (!QImageIOHandler::allocateImage(size, QImage::Format_ARGB32, &pattern)) {
884 qCWarning(lcSvgDraw) <<
"The requested pattern size is too big, ignoring";
885 return defaultPattern();
887 pattern.fill(Qt::transparent);
890 qCWarning(lcSvgDraw) <<
"The pattern is trying to render itself recursively. "
891 "Returning a transparent QImage of the right size.";
894 QScopedValueRollback<
bool> guard(m_isRendering,
true);
897 QPainter patternPainter(&pattern);
898 QSvgExtraStates patternStates;
899 initPainter(&patternPainter);
900 applyStyleRecursive(&patternPainter, patternStates);
901 patternPainter.resetTransform();
905 if (m_viewBox.isNull())
906 patternPainter.scale(contentScaleX, contentScaleY);
908 patternPainter.setWindow(m_viewBox.toRect());
912 for (QSvgNode *node : m_renderers)
913 node->draw(&patternPainter, patternStates);
915 revertStyleRecursive(&patternPainter, patternStates);
919void QSvgPattern::calculateAppliedTransform(QTransform &worldTransform, QRectF peLocalBB, QSize imageSize)
934 m_appliedTransform.reset();
935 qreal imageDownScaleFactorX = 1 / worldTransform.m11();
936 qreal imageDownScaleFactorY = 1 / worldTransform.m22();
938 m_appliedTransform.scale(qIsFinite(imageDownScaleFactorX) ? imageDownScaleFactorX : 1.0,
939 qIsFinite(imageDownScaleFactorY) ? imageDownScaleFactorY : 1.0);
941 QRectF p = m_rect.resolveRelativeLengths(peLocalBB);
942 m_appliedTransform.scale((p.width() * worldTransform.m11() * m_transform.m11()) / imageSize.width(),
943 (p.height() * worldTransform.m22() * m_transform.m22()) / imageSize.height());
945 QPointF translation = m_rect.translationRelativeToBoundingBox(peLocalBB);
946 m_appliedTransform.translate(translation.x() * worldTransform.m11(), translation.y() * worldTransform.m22());
948 QTransform scalelessTransform = m_transform;
949 scalelessTransform.scale(1 / m_transform.m11(), 1 / m_transform.m22());
951 m_appliedTransform = m_appliedTransform * scalelessTransform;
The QPolygonF class provides a list of points using floating point precision.
#define QStringLiteral(str)
static QImage & defaultPattern()
static bool isSupportedSvgExtension(const QString &)
static bool isSupportedSvgFeature(const QString &str)