19#include <QLoggingCategory>
20#include <qscopedvaluerollback.h>
21#include <QtGui/qimageiohandler.h>
25QSvgG::QSvgG(QSvgNode *parent)
26 : QSvgStructureNode(parent)
31QSvgStructureNode::~QSvgStructureNode()
35void QSvgG::drawCommand(QPainter *p, QSvgExtraStates &states)
37 for (
const auto &node : renderers()) {
38 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
39 node->draw(p, states);
43bool QSvgG::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
48QSvgNode::Type QSvgG::type()
const
53bool QSvgG::requiresGroupRendering()
const
55 return m_renderers.size() > 1;
58QSvgStructureNode::QSvgStructureNode(QSvgNode *parent)
64QSvgNode * QSvgStructureNode::scopeNode(
const QString &id)
const
66 QSvgDocument *doc = document();
67 return doc ? doc->namedNode(id) : 0;
70void QSvgStructureNode::addChild(std::unique_ptr<QSvgNode> child,
const QString &id)
73 QSvgDocument *doc = document();
75 doc->addNamedNode(id, child.get());
78 m_renderers.push_back(std::move(child));
81QSvgDefs::QSvgDefs(QSvgNode *parent)
82 : QSvgStructureNode(parent)
86bool QSvgDefs::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
91QSvgNode::Type QSvgDefs::type()
const
96QSvgSymbolLike::QSvgSymbolLike(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
97 QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow)
98 : QSvgStructureNode(parent)
102 , m_pAspectRatios(pAspectRatios)
103 , m_overflow(overflow)
108QRectF QSvgSymbolLike::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
111 setPainterToRectAndAdjustment(p);
112 QRectF rect = internalBounds(p, states);
117bool QSvgSymbolLike::requiresGroupRendering()
const
119 return m_renderers.size() > 1;
122QRectF QSvgSymbolLike::clipRect()
const
124 if (m_overflow != Overflow::Hidden || !m_viewBox.isValid())
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();
135 t.translate(- m_refP.x() * scaleX - m_rect.left() - m_viewBox.left() * scaleX,
136 - m_refP.y() * scaleY - m_rect.top() - m_viewBox.top() * scaleY);
137 t.scale(scaleX, scaleY);
139 return t.mapRect(m_viewBox);
142QTransform QSvgSymbolLike::aspectRatioTransform()
const
150 if (m_rect.width() > 0 && m_viewBox.width() > 0)
151 scaleX = m_rect.width() / m_viewBox.width();
153 if (m_rect.height() > 0 && m_viewBox.height() > 0)
154 scaleY = m_rect.height() / m_viewBox.height();
156 if (!qFuzzyCompare(scaleX, scaleY) &&
157 m_pAspectRatios.testAnyFlag(PreserveAspectRatio::xyMask)) {
159 if (m_pAspectRatios.testAnyFlag(PreserveAspectRatio::meet))
160 scaleX = scaleY = qMin(scaleX, scaleY);
162 scaleX = scaleY = qMax(scaleX, scaleY);
164 qreal xOverflow = scaleX * m_viewBox.width() - m_rect.width();
165 qreal yOverflow = scaleY * m_viewBox.height() - m_rect.height();
167 if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMid)
168 offsetX -= xOverflow / 2.;
169 else if ((m_pAspectRatios & PreserveAspectRatio::xMask) == PreserveAspectRatio::xMax)
170 offsetX -= xOverflow;
172 if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMid)
173 offsetY -= yOverflow / 2.;
174 else if ((m_pAspectRatios & PreserveAspectRatio::yMask) == PreserveAspectRatio::yMax)
175 offsetY -= yOverflow;
178 xform.translate(offsetX - m_refP.x() * scaleX, offsetY - m_refP.y() * scaleY);
179 xform.scale(scaleX, scaleY);
184void QSvgSymbolLike::setPainterToRectAndAdjustment(QPainter *p)
const
186 QRectF clip = clipRect();
188 p->setClipRect(clip);
190 p->setTransform(aspectRatioTransform(),
true);
193QSvgSymbol::QSvgSymbol(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
194 QSvgSymbol::PreserveAspectRatios pAspectRatios,
195 QSvgSymbol::Overflow overflow)
196 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
200void QSvgSymbol::drawCommand(QPainter *p, QSvgExtraStates &states)
206 setPainterToRectAndAdjustment(p);
207 for (
const auto &node : renderers()) {
208 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
209 node->draw(p, states);
214QSvgNode::Type QSvgSymbol::type()
const
219QSvgMarker::QSvgMarker(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
220 QSvgSymbol::PreserveAspectRatios pAspectRatios, QSvgSymbol::Overflow overflow,
221 Orientation orientation, qreal orientationAngle, MarkerUnits markerUnits)
222 : QSvgSymbolLike(parent, bounds, viewBox, refP, pAspectRatios, overflow)
223 , m_orientation(orientation)
224 , m_orientationAngle(orientationAngle)
225 , m_markerUnits(markerUnits)
228 QSvgFillStyle *fillProp =
new QSvgFillStyle();
229 fillProp->setBrush(Qt::black);
232 QSvgStrokeStyle *strokeProp =
new QSvgStrokeStyle();
233 strokeProp->setMiterLimit(4);
234 strokeProp->setWidth(1);
235 strokeProp->setLineCap(Qt::FlatCap);
236 strokeProp->setLineJoin(Qt::SvgMiterJoin);
237 strokeProp->setStroke(Qt::NoBrush);
241QSvgFilterContainer::QSvgFilterContainer(QSvgNode *parent,
const QSvgRectF &bounds,
242 QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits)
243 : QSvgStructureNode(parent)
245 , m_filterUnits(filterUnits)
246 , m_primitiveUnits(primitiveUnits)
252bool QSvgFilterContainer::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
257void QSvgMarker::drawCommand(QPainter *p, QSvgExtraStates &states)
262 if (Q_UNLIKELY(m_recursing))
264 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
267 setPainterToRectAndAdjustment(p);
269 for (
const auto &node : renderers()) {
270 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
271 node->draw(p, states);
278struct PositionMarkerPair {
283 bool isStartNode =
false;
286QList<PositionMarkerPair> markersForNode(
const QSvgNode *node)
288 if (!node->hasAnyMarker())
291 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
292 QPointF t1 = p1 - p0;
293 QPointF t2 = p2 - p1;
294 qreal hyp1 = hypot(t1.x(), t1.y());
299 qreal hyp2 = hypot(t2.x(), t2.y());
304 QPointF tangent = t1 + t2;
305 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
308 QList<PositionMarkerPair> markers;
310 switch (node->type()) {
311 case QSvgNode::Line: {
312 const QSvgLine *line =
static_cast<
const QSvgLine*>(node);
313 if (node->hasMarkerStart())
314 markers << PositionMarkerPair { line->line().p1().x(), line->line().p1().y(),
315 line->line().angle(), line->markerStartId(),
317 if (node->hasMarkerEnd())
318 markers << PositionMarkerPair { line->line().p2().x(), line->line().p2().y(),
319 line->line().angle(), line->markerEndId() };
322 case QSvgNode::Polyline:
323 case QSvgNode::Polygon: {
324 const QPolygonF &polyData = (node->type() == QSvgNode::Polyline)
325 ?
static_cast<
const QSvgPolyline*>(node)->polygon()
326 :
static_cast<
const QSvgPolygon*>(node)->polygon();
328 if (node->hasMarkerStart() && polyData.size() > 1) {
329 QLineF line(polyData.at(0), polyData.at(1));
330 markers << PositionMarkerPair { line.p1().x(),
333 node->markerStartId(),
336 if (node->hasMarkerMid()) {
337 for (
int i = 1; i < polyData.size() - 1; i++) {
338 QPointF p0 = polyData.at(i - 1);
339 QPointF p1 = polyData.at(i);
340 QPointF p2 = polyData.at(i + 1);
342 markers << PositionMarkerPair { p1.x(),
344 getMeanAngle(p0, p1, p2),
345 node->markerStartId() };
348 if (node->hasMarkerEnd() && polyData.size() > 1) {
349 QLineF line(polyData.at(polyData.size() - 1), polyData.last());
350 markers << PositionMarkerPair { line.p2().x(),
353 node->markerEndId() };
357 case QSvgNode::Path: {
358 const QSvgPath *path =
static_cast<
const QSvgPath*>(node);
359 if (node->hasMarkerStart())
360 markers << PositionMarkerPair { path->path().pointAtPercent(0.).x(),
361 path->path().pointAtPercent(0.).y(),
362 path->path().angleAtPercent(0.),
363 path->markerStartId(),
365 if (node->hasMarkerMid()) {
366 for (
int i = 1; i < path->path().elementCount() - 1; i++) {
367 if (path->path().elementAt(i).type == QPainterPath::MoveToElement)
369 if (path->path().elementAt(i).type == QPainterPath::CurveToElement)
371 if (( path->path().elementAt(i).type == QPainterPath::CurveToDataElement &&
372 path->path().elementAt(i + 1).type != QPainterPath::CurveToDataElement ) ||
373 path->path().elementAt(i).type == QPainterPath::LineToElement) {
375 QPointF p0(path->path().elementAt(i - 1).x, path->path().elementAt(i - 1).y);
376 QPointF p1(path->path().elementAt(i).x, path->path().elementAt(i).y);
377 QPointF p2(path->path().elementAt(i + 1).x, path->path().elementAt(i + 1).y);
379 markers << PositionMarkerPair { p1.x(),
381 getMeanAngle(p0, p1, p2),
382 path->markerMidId() };
386 if (node->hasMarkerEnd())
387 markers << PositionMarkerPair { path->path().pointAtPercent(1.).x(),
388 path->path().pointAtPercent(1.).y(),
389 path->path().angleAtPercent(1.),
390 path->markerEndId() };
404void QSvgMarker::drawMarkersForNode(QSvgNode *node, QPainter *p, QSvgExtraStates &states)
406 drawHelper(node, p, states);
409QRectF QSvgMarker::markersBoundsForNode(
const QSvgNode *node, QPainter *p, QSvgExtraStates &states)
412 drawHelper(node, p, states, &bounds);
416QSvgNode::Type QSvgMarker::type()
const
421void QSvgMarker::drawHelper(
const QSvgNode *node, QPainter *p,
422 QSvgExtraStates &states, QRectF *boundingRect)
424 QScopedValueRollback<
bool> inUseGuard(states.inUse,
true);
426 const bool isPainting = (boundingRect ==
nullptr);
427 const auto markers = markersForNode(node);
428 for (
auto &i : markers) {
429 QSvgMarker *markNode =
static_cast<QSvgMarker*>(node->document()->namedNode(i.markerId));
434 p->translate(i.x, i.y);
435 if (markNode->orientation() == QSvgMarker::Orientation::Value) {
436 p->rotate(markNode->orientationAngle());
439 if (i.isStartNode && markNode->orientation()
440 == QSvgMarker::Orientation::AutoStartReverse) {
444 QRectF oldRect = markNode->m_rect;
445 if (markNode->markerUnits() == QSvgMarker::MarkerUnits::StrokeWidth) {
446 markNode->m_rect.setWidth(markNode->m_rect.width() * p->pen().widthF());
447 markNode->m_rect.setHeight(markNode->m_rect.height() * p->pen().widthF());
450 markNode->draw(p, states);
453 QTransform xf = p->transform();
455 *boundingRect |= xf.mapRect(markNode->decoratedInternalBounds(p, states));
458 markNode->m_rect = oldRect;
463QImage QSvgFilterContainer::applyFilter(
const QImage &buffer, QPainter *p,
const QRectF &bounds)
const
465 QRectF localFilterRegion = m_rect.resolveRelativeLengths(bounds, m_filterUnits);
466 QRect globalFilterRegion = p->transform().mapRect(localFilterRegion).toRect();
467 QRect globalFilterRegionRel = globalFilterRegion.translated(-buffer.offset());
469 if (globalFilterRegionRel.isEmpty())
473 if (!QImageIOHandler::allocateImage(globalFilterRegionRel.size(), buffer.format(), &proxy)) {
474 qCWarning(lcSvgDraw) <<
"The requested filter is too big, ignoring";
477 proxy = buffer.copy(globalFilterRegionRel);
478 proxy.setOffset(globalFilterRegion.topLeft());
482 QMap<QString, QImage> buffers;
486 bool requiresSourceAlpha =
false;
488 for (
const auto &node : renderers()) {
489 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
490 if (filter && filter->requiresSourceAlpha()) {
491 requiresSourceAlpha =
true;
496 if (requiresSourceAlpha) {
497 QImage proxyAlpha = proxy.convertedTo(QImage::Format_Alpha8).convertedTo(proxy.format());
498 proxyAlpha.setOffset(proxy.offset());
499 if (proxyAlpha.isNull())
505 for (
const auto &node : renderers()) {
506 const QSvgFeFilterPrimitive *filter = QSvgFeFilterPrimitive::castToFilterPrimitive(node.get());
508 result = filter->apply(buffers, p, bounds, localFilterRegion, m_primitiveUnits, m_filterUnits);
509 if (!result.isNull()) {
511 buffers[filter->result()] = result;
518void QSvgFilterContainer::setSupported(
bool supported)
520 m_supported = supported;
523bool QSvgFilterContainer::supported()
const
528QRectF QSvgFilterContainer::filterRegion(
const QRectF &itemBounds)
const
530 return m_rect.resolveRelativeLengths(itemBounds, m_filterUnits);
533QSvgNode::Type QSvgFilterContainer::type()
const
541 static const QStringList wordList = {
542 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Text"),
543 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Shape"),
544 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#SVG"),
545 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Structure"),
546 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#SolidColor"),
547 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Hyperlinking"),
548 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#CoreAttribute"),
549 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#XlinkAttribute"),
550 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#SVG-static"),
551 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#OpacityAttribute"),
552 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Gradient"),
553 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Font"),
554 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Image"),
555 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#ConditionalProcessing"),
556 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Extensibility"),
557 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#GraphicsAttribute"),
558 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#Prefetch"),
559 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#PaintAttribute"),
560 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#ConditionalProcessingAttribute"),
561 QStringLiteral(
"http://www.w3.org/Graphics/SVG/feature/1.2/#ExternalResourcesRequiredAttribute")
564 return wordList.contains(str);
573QSvgSwitch::QSvgSwitch(QSvgNode *parent)
574 : QSvgStructureNode(parent)
579QSvgNode *QSvgSwitch::childToRender()
const
581 auto itr = m_renderers.begin();
583 for (
const auto &node : renderers()) {
584 if (node->isVisible() && (node->displayMode() != QSvgNode::NoneMode)) {
585 const QStringList &features = node->requiredFeatures();
586 const QStringList &extensions = node->requiredExtensions();
587 const QStringList &languages = node->requiredLanguages();
588 const QStringList &formats = node->requiredFormats();
589 const QStringList &fonts = node->requiredFonts();
591 bool okToRender =
true;
592 if (!features.isEmpty()) {
593 QStringList::const_iterator sitr = features.constBegin();
594 for (; sitr != features.constEnd(); ++sitr) {
595 if (!isSupportedSvgFeature(*sitr)) {
602 if (okToRender && !extensions.isEmpty()) {
603 QStringList::const_iterator sitr = extensions.constBegin();
604 for (; sitr != extensions.constEnd(); ++sitr) {
605 if (!isSupportedSvgExtension(*sitr)) {
612 if (okToRender && !languages.isEmpty()) {
613 QStringList::const_iterator sitr = languages.constBegin();
615 for (; sitr != languages.constEnd(); ++sitr) {
616 if ((*sitr).startsWith(m_systemLanguagePrefix)) {
623 if (okToRender && !formats.isEmpty())
626 if (okToRender && !fonts.isEmpty())
639void QSvgSwitch::drawCommand(QPainter *p, QSvgExtraStates &states)
641 QSvgNode *node = childToRender();
643 node->draw(p, states);
646QSvgNode::Type QSvgSwitch::type()
const
651void QSvgSwitch::init()
654 m_systemLanguage = locale.name().replace(QLatin1Char(
'_'), QLatin1Char(
'-'));
655 int idx = m_systemLanguage.indexOf(QLatin1Char(
'-'));
656 m_systemLanguagePrefix = m_systemLanguage.mid(0, idx);
659QRectF QSvgStructureNode::internalBounds(QPainter *p, QSvgExtraStates &states)
const
663 QScopedValueRollback<
bool> guard(m_recursing,
true);
664 for (
const auto &node : renderers())
665 bounds |= node->bounds(p, states);
670QRectF QSvgStructureNode::decoratedInternalBounds(QPainter *p, QSvgExtraStates &states)
const
674 QScopedValueRollback<
bool> guard(m_recursing,
true);
675 for (
const auto &node : renderers())
676 bounds |= node->decoratedBounds(p, states);
681QSvgNode* QSvgStructureNode::previousSiblingNode(QSvgNode *n)
const
683 QSvgNode *prev =
nullptr;
684 for (
const auto &node : renderers()) {
692QSvgMask::QSvgMask(QSvgNode *parent, QSvgRectF bounds,
693 QtSvg::UnitTypes contentUnits)
694 : QSvgStructureNode(parent)
696 , m_contentUnits(contentUnits)
700bool QSvgMask::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
705QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect)
const
707 QTransform t = p->transform();
709 QRectF basicRect = targetNode->internalBounds(p, states);
710 *globalRect = t.mapRect(basicRect);
712 return createMask(p, states, basicRect, globalRect);
715QImage QSvgMask::createMask(QPainter *p, QSvgExtraStates &states,
const QRectF &localRect, QRectF *globalRect)
const
717 QRect imageBound = globalRect->toAlignedRect();
718 *globalRect = imageBound.toRectF();
721 if (!QImageIOHandler::allocateImage(imageBound.size(), QImage::Format_RGBA8888, &mask)) {
722 qCWarning(lcSvgDraw) <<
"The requested mask size is too big, ignoring";
726 if (Q_UNLIKELY(m_recursing))
728 QScopedValueRollback<
bool> recursingGuard(m_recursing,
true);
731 if (
this->hasMask()) {
732 QSvgMask *maskNode =
static_cast<QSvgMask*>(document()->namedNode(
this->maskId()));
735 return maskNode->createMask(p, states, localRect, &boundsRect);
745 mask.fill(Qt::transparent);
746 QPainter painter(&mask);
747 initPainter(&painter);
749 QSvgExtraStates maskNodeStates;
750 applyStyleRecursive(&painter, maskNodeStates);
754 painter.resetTransform();
755 painter.translate(-imageBound.topLeft());
756 painter.setTransform(p->transform(),
true);
758 QTransform oldT = painter.transform();
759 if (m_contentUnits == QtSvg::UnitTypes::objectBoundingBox){
760 painter.translate(localRect.topLeft());
761 painter.scale(localRect.width(), localRect.height());
765 for (
const auto &node : renderers())
767 if ((node->isVisible()) && (node->displayMode() != QSvgNode::NoneMode))
768 node->draw(&painter, maskNodeStates);
771 for (
int i=0; i < mask.height(); i++) {
772 QRgb *line =
reinterpret_cast<QRgb *>(mask.scanLine(i));
773 for (
int j=0; j < mask.width(); j++) {
774 const qreal rC = 0.2125, gC = 0.7154, bC = 0.0721;
775 int alpha = 255 - (qRed(line[j]) * rC + qGreen(line[j]) * gC + qBlue(line[j]) * bC) * qAlpha(line[j])/255.;
776 line[j] = qRgba(0, 0, 0, alpha);
784 QRectF clipRect = m_rect.resolveRelativeLengths(localRect);
785 QPainterPath clipPath;
786 clipPath.setFillRule(Qt::OddEvenFill);
787 clipPath.addRect(mask.rect().adjusted(-10, -10, 20, 20));
788 clipPath.addPolygon(oldT.map(QPolygonF(clipRect)));
789 painter.resetTransform();
790 painter.fillPath(clipPath, Qt::black);
791 revertStyleRecursive(&painter, maskNodeStates);
795QSvgNode::Type QSvgMask::type()
const
800QSvgPattern::QSvgPattern(QSvgNode *parent, QSvgRectF bounds, QRectF viewBox,
801 QtSvg::UnitTypes contentUnits, QTransform transform)
802 : QSvgStructureNode(parent),
805 m_contentUnits(contentUnits),
806 m_isRendering(
false),
807 m_transform(transform)
813bool QSvgPattern::shouldDrawNode(QPainter *, QSvgExtraStates &)
const
820 static QImage checkerPattern;
822 if (checkerPattern.isNull()) {
823 checkerPattern = QImage(QSize(8, 8), QImage::Format_ARGB32);
825 p.fillRect(QRect(0, 0, 4, 4), QColorConstants::Svg::white);
826 p.fillRect(QRect(4, 0, 4, 4), QColorConstants::Svg::black);
827 p.fillRect(QRect(0, 4, 4, 4), QColorConstants::Svg::black);
828 p.fillRect(QRect(4, 4, 4, 4), QColorConstants::Svg::white);
831 return checkerPattern;
834QImage QSvgPattern::patternImage(QPainter *p, QSvgExtraStates &states,
const QSvgNode *patternElement)
837 QRectF peBoundingBox;
838 QRectF peWorldBoundingBox;
840 QTransform t = p->transform();
842 peBoundingBox = patternElement->internalBounds(p, states);
843 peWorldBoundingBox = t.mapRect(peBoundingBox);
853 qreal contentScaleFactorX = m_transform.m11();
854 qreal contentScaleFactorY = m_transform.m22();
855 if (m_contentUnits == QtSvg::UnitTypes::userSpaceOnUse) {
856 contentScaleFactorX *= t.m11();
857 contentScaleFactorY *= t.m22();
859 contentScaleFactorX *= peWorldBoundingBox.width();
860 contentScaleFactorY *= peWorldBoundingBox.height();
864 QRectF patternBoundingBox = m_rect.resolveRelativeLengths(peBoundingBox);
867 imageSize.setWidth(qCeil(patternBoundingBox.width() * t.m11() * m_transform.m11()));
868 imageSize.setHeight(qCeil(patternBoundingBox.height() * t.m22() * m_transform.m22()));
869 if (imageSize.isEmpty())
872 calculateAppliedTransform(t, peBoundingBox, imageSize);
873 if (document()->isCalculatingImplicitViewBox())
874 return QImage(imageSize, QImage::Format_ARGB32);
876 return renderPattern(imageSize, contentScaleFactorX, contentScaleFactorY);
879QSvgNode::Type QSvgPattern::type()
const
884QImage QSvgPattern::renderPattern(QSize size, qreal contentScaleX, qreal contentScaleY)
886 if (size.isEmpty() || !qIsFinite(contentScaleX) || !qIsFinite(contentScaleY))
887 return defaultPattern();
891 if (!QImageIOHandler::allocateImage(size, QImage::Format_ARGB32, &pattern)) {
892 qCWarning(lcSvgDraw) <<
"The requested pattern size is too big, ignoring";
893 return defaultPattern();
895 pattern.fill(Qt::transparent);
898 qCWarning(lcSvgDraw) <<
"The pattern is trying to render itself recursively. "
899 "Returning a transparent QImage of the right size.";
902 QScopedValueRollback<
bool> guard(m_isRendering,
true);
905 QPainter patternPainter(&pattern);
906 QSvgExtraStates patternStates;
907 initPainter(&patternPainter);
908 applyStyleRecursive(&patternPainter, patternStates);
909 patternPainter.resetTransform();
913 if (m_viewBox.isNull())
914 patternPainter.scale(contentScaleX, contentScaleY);
916 patternPainter.setWindow(m_viewBox.toRect());
920 for (
const auto &node : renderers())
921 node->draw(&patternPainter, patternStates);
923 revertStyleRecursive(&patternPainter, patternStates);
927void QSvgPattern::calculateAppliedTransform(QTransform &worldTransform, QRectF peLocalBB, QSize imageSize)
942 m_appliedTransform.reset();
943 qreal imageDownScaleFactorX = 1 / worldTransform.m11();
944 qreal imageDownScaleFactorY = 1 / worldTransform.m22();
946 m_appliedTransform.scale(qIsFinite(imageDownScaleFactorX) ? imageDownScaleFactorX : 1.0,
947 qIsFinite(imageDownScaleFactorY) ? imageDownScaleFactorY : 1.0);
949 QRectF p = m_rect.resolveRelativeLengths(peLocalBB);
950 m_appliedTransform.scale((p.width() * worldTransform.m11() * m_transform.m11()) / imageSize.width(),
951 (p.height() * worldTransform.m22() * m_transform.m22()) / imageSize.height());
953 QPointF translation = m_rect.translationRelativeToBoundingBox(peLocalBB);
954 m_appliedTransform.translate(translation.x() * worldTransform.m11(), translation.y() * worldTransform.m22());
956 QTransform scalelessTransform = m_transform;
957 scalelessTransform.scale(1 / m_transform.m11(), 1 / m_transform.m22());
959 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(const QString &str)