8#include <private/qsvgvisitor_p.h>
12#include <QTextDocument>
17#include <private/qquickshape_p.h>
18#include <private/qquicktext_p.h>
19#include <private/qquicktranslate_p.h>
20#include <private/qquickitem_p.h>
22#include <private/qquickimagebase_p_p.h>
23#include <private/qquickimage_p.h>
24#include <private/qsgcurveprocessor_p.h>
26#include <private/qquadpath_p.h>
28#include <QtCore/private/qstringiterator_p.h>
31#include <QtCore/qloggingcategory.h>
33#include <QtSvg/private/qsvgstyle_p.h>
34#include <QtSvg/private/qsvgfilter_p.h>
38Q_STATIC_LOGGING_CATEGORY(lcVectorImageAnimations,
"qt.quick.vectorimage.animations")
40using namespace Qt::StringLiterals;
47 m_dummyImage = QImage(1, 1, QImage::Format_RGB32);
48 m_dummyPainter.begin(&m_dummyImage);
49 QPen defaultPen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
50 defaultPen.setMiterLimit(4);
51 m_dummyPainter.setPen(defaultPen);
52 m_dummyPainter.setBrush(Qt::black);
65 if (m_dummyPainter.brush().style() == Qt::NoBrush ||
66 m_dummyPainter.brush().color() == QColorConstants::Transparent) {
67 return QColor(QColorConstants::Transparent);
71 fillColor = m_dummyPainter.brush().color();
72 fillColor.setAlphaF(m_svgState.fillOpacity);
79 return m_svgState.fillOpacity;
84 QBrush brush = m_dummyPainter.pen().brush();
85 if (brush.style() == Qt::LinearGradientPattern
86 || brush.style() == Qt::RadialGradientPattern
87 || brush.style() == Qt::ConicalGradientPattern) {
88 return brush.gradient();
95 if (m_dummyPainter.brush().style() == Qt::LinearGradientPattern || m_dummyPainter.brush().style() == Qt::RadialGradientPattern || m_dummyPainter.brush().style() == Qt::ConicalGradientPattern )
96 return m_dummyPainter.brush().gradient();
102 return m_dummyPainter.brush().transform();
107 if (m_dummyPainter.pen().brush().style() == Qt::NoBrush ||
108 m_dummyPainter.pen().brush().color() == QColorConstants::Transparent) {
109 return QColor(QColorConstants::Transparent);
113 strokeColor = m_dummyPainter.pen().brush().color();
114 strokeColor.setAlphaF(m_svgState.strokeOpacity);
121 QGradient grad = gradient;
122 QGradientStops stops;
123 for (
auto &stop : grad.stops()) {
124 stop.second.setAlphaF(stop.second.alphaF() * opacity);
128 grad.setStops(stops);
135 float penWidth = m_dummyPainter.pen().widthF();
136 return penWidth ? penWidth : 1;
141 return m_dummyPainter.pen();
151inline bool isPathContainer(
const QSvgStructureNode *node)
153 bool foundPath =
false;
154 for (
const auto &child : node->renderers()) {
155 switch (child->type()) {
157 case QSvgNode::Switch:
159 case QSvgNode::Group:
160 case QSvgNode::AnimateColor:
161 case QSvgNode::AnimateTransform:
163 case QSvgNode::Video:
164 case QSvgNode::Image:
165 case QSvgNode::Textarea:
167 case QSvgNode::Tspan:
169 case QSvgNode::Marker:
170 case QSvgNode::Pattern:
176 case QSvgNode::Symbol:
181 case QSvgNode::Circle:
182 case QSvgNode::Ellipse:
185 case QSvgNode::Polygon:
186 case QSvgNode::Polyline:
188 if (child->hasFilter())
191 if (child->hasAnyMarker())
194 if (!child->style().opacity.isDefault())
197 if (!child->style().transform.isDefault()) {
201 const auto animations = child->document()->animator()->animationsForNode(child.get());
202 if (!animations.isEmpty()) {
210 qCDebug(lcQuickVectorImage) <<
"Unhandled type in switch" << child->type();
218static QString capStyleName(Qt::PenCapStyle style)
224 styleName = QStringLiteral(
"squarecap");
227 styleName = QStringLiteral(
"flatcap");
230 styleName = QStringLiteral(
"roundcap");
239static QString joinStyleName(Qt::PenJoinStyle style)
245 styleName = QStringLiteral(
"miterjoin");
248 styleName = QStringLiteral(
"beveljoin");
251 styleName = QStringLiteral(
"roundjoin");
253 case Qt::SvgMiterJoin:
254 styleName = QStringLiteral(
"svgmiterjoin");
263static QString dashArrayString(QList<qreal> dashArray)
265 if (dashArray.isEmpty())
268 QString dashArrayString;
269 QTextStream stream(&dashArrayString);
271 for (
int i = 0; i < dashArray.length() - 1; i++) {
272 qreal value = dashArray[i];
273 stream << value <<
", ";
276 stream << dashArray.last();
278 return dashArrayString;
282static QString
scrub(
const QString &raw)
284 QString res(raw.left(80));
286 if (!res.isEmpty()) {
287 constexpr QLatin1StringView legalSymbols(
"_-.:");
290 if (res.at(i).isLetterOrNumber() || legalSymbols.contains(res.at(i)))
294 }
while (i < res.size());
301 QQuickGenerator *generator,
302 bool assumeTrustedSource)
305 , m_assumeTrustedSource(assumeTrustedSource)
315 fillCommonNodeInfo(node, info);
321 if (node->type() == QSvgNode::Pattern) {
322 info.transform = QQuickAnimatedProperty(QVariant::fromValue(QTransform{}));
323 info.isDefaultTransform =
true;
326 if (!m_generator->generateDefsNode(info))
335 fillCommonNodeInfo(node, info);
339 m_generator->generateDefsNode(info);
344 switch (node->type()) {
345 case QSvgNode::Switch:
348 case QSvgNode::Group:
350 case QSvgNode::Symbol:
351 case QSvgNode::Filter:
352 case QSvgNode::FeMerge:
353 case QSvgNode::FeMergenode:
354 case QSvgNode::FeColormatrix:
355 case QSvgNode::FeGaussianblur:
356 case QSvgNode::FeOffset:
357 case QSvgNode::FeComposite:
358 case QSvgNode::FeFlood:
359 case QSvgNode::FeBlend:
360 case QSvgNode::Marker:
361 case QSvgNode::Pattern:
368static void recurseSvgNodes(
const QSvgNode *root,
const std::function<
void(
const QSvgNode *)> &fnc)
372 if (isStructureNode(root)) {
373 const QSvgStructureNode *sn =
static_cast<
const QSvgStructureNode *>(root);
374 for (
const auto &child : sn->renderers())
375 recurseSvgNodes(child.get(), fnc);
381 Q_ASSERT(m_generator !=
nullptr);
385 QSet<QString> referencedIds;
386 auto findReferencedIds = [&referencedIds](
const QSvgNode *node) {
387 if (node->hasFilter())
388 referencedIds.insert(node->filterId());
390 referencedIds.insert(node->maskId());
391 if (node->hasMarkerStart())
392 referencedIds.insert(node->markerStartId());
393 if (node->hasMarkerMid())
394 referencedIds.insert(node->markerMidId());
395 if (node->hasMarkerEnd())
396 referencedIds.insert(node->markerEndId());
397 if (node->type() == QSvgNode::Pattern)
398 referencedIds.insert(node->nodeId());
400 recurseSvgNodes(doc, findReferencedIds);
402 m_pregeneratingReferencedNodes =
true;
403 for (
const QString &referencedId : referencedIds) {
404 const QSvgNode *referencedNode = doc->document()->namedNode(referencedId);
405 if (referencedNode ==
nullptr)
408 if (!startDefsBlock(referencedNode))
411 traverse(referencedNode);
413 endDefsBlock(referencedNode);
416 m_pregeneratingReferencedNodes =
false;
422 qCDebug(lcQuickVectorImage) <<
"No valid QQuickGenerator is set. Genration will stop";
426 QtSvg::Options options;
427 if (m_assumeTrustedSource)
428 options.setFlag(QtSvg::AssumeTrustedSource);
430 const auto doc = QSvgDocument::load(m_svgFileName, options);
432 qCDebug(lcQuickVectorImage) <<
"Not a valid Svg File : " << m_svgFileName;
436 QSvgVisitor::traverse(doc.get());
443 handleBaseNodeSetup(node);
446 fillCommonNodeInfo(node, info);
447 fillAnimationInfo(node, info);
449 m_generator->generateNode(info);
451 handleBaseNodeEnd(node);
457 handleBaseNodeSetup(node);
460 fillCommonNodeInfo(node, info);
461 fillAnimationInfo(node, info);
462 info.image = node->image();
463 info.rect = node->rect();
464 info.externalFileReference = node->filename();
466 m_generator->generateImageNode(info);
468 handleBaseNodeEnd(node);
473 QRectF rect = node->rect();
474 QPointF rads = node->radius();
476 qreal x1 = rect.left();
477 qreal x2 = rect.right();
478 qreal y1 = rect.top();
479 qreal y2 = rect.bottom();
481 qreal rx = rads.x() * rect.width() / 200;
482 qreal ry = rads.y() * rect.height() / 200;
485 p.moveTo(x1 + rx, y1);
486 p.lineTo(x2 - rx, y1);
488 p.arcTo(x2 - rx * 2, y1, rx * 2, ry * 2, 90, -90);
491 p.lineTo(x2, y2 - ry);
492 p.arcTo(x2 - rx * 2, y2 - ry * 2, rx * 2, ry * 2, 0, -90);
494 p.lineTo(x1 + rx, y2);
495 p.arcTo(x1, y2 - ry * 2, rx * 2, ry * 2, 270, -90);
497 p.lineTo(x1, y1 + ry);
498 p.arcTo(x1, y1, rx * 2, ry * 2, 180, -90);
500 handlePathNode(node, p);
505 QRectF rect = node->rect();
510 handlePathNode(node, p);
515 handlePathNode(node, node->path());
521 p.moveTo(node->line().p1());
522 p.lineTo(node->line().p2());
523 handlePathNode(node, p);
528 QPainterPath p = QQuickVectorImageGenerator::Utils::polygonToPath(node->polygon(),
true);
529 handlePathNode(node, p);
534 QPainterPath p = QQuickVectorImageGenerator::Utils::polygonToPath(node->polygon(),
false);
535 handlePathNode(node, p);
538QString
QSvgVisitorImpl::gradientCssDescription(
const QGradient *gradient)
540 QString cssDescription;
541 if (gradient->type() == QGradient::LinearGradient) {
542 const QLinearGradient *linearGradient =
static_cast<
const QLinearGradient *>(gradient);
544 cssDescription +=
" -qt-foreground: qlineargradient("_L1;
545 cssDescription +=
"x1:"_L1 + QString::number(linearGradient->start().x()) + u',';
546 cssDescription +=
"y1:"_L1 + QString::number(linearGradient->start().y()) + u',';
547 cssDescription +=
"x2:"_L1 + QString::number(linearGradient->finalStop().x()) + u',';
548 cssDescription +=
"y2:"_L1 + QString::number(linearGradient->finalStop().y()) + u',';
549 }
else if (gradient->type() == QGradient::RadialGradient) {
550 const QRadialGradient *radialGradient =
static_cast<
const QRadialGradient *>(gradient);
552 cssDescription +=
" -qt-foreground: qradialgradient("_L1;
553 cssDescription +=
"cx:"_L1 + QString::number(radialGradient->center().x()) + u',';
554 cssDescription +=
"cy:"_L1 + QString::number(radialGradient->center().y()) + u',';
555 cssDescription +=
"fx:"_L1 + QString::number(radialGradient->focalPoint().x()) + u',';
556 cssDescription +=
"fy:"_L1 + QString::number(radialGradient->focalPoint().y()) + u',';
557 cssDescription +=
"radius:"_L1 + QString::number(radialGradient->radius()) + u',';
559 const QConicalGradient *conicalGradient =
static_cast<
const QConicalGradient *>(gradient);
561 cssDescription +=
" -qt-foreground: qconicalgradient("_L1;
562 cssDescription +=
"cx:"_L1 + QString::number(conicalGradient->center().x()) + u',';
563 cssDescription +=
"cy:"_L1 + QString::number(conicalGradient->center().y()) + u',';
564 cssDescription +=
"angle:"_L1 + QString::number(conicalGradient->angle()) + u',';
567 const QStringList coordinateModes = {
"logical"_L1,
"stretchtodevice"_L1,
"objectbounding"_L1,
"object"_L1 };
568 cssDescription +=
"coordinatemode:"_L1;
569 cssDescription += coordinateModes.at(
int(gradient->coordinateMode()));
570 cssDescription += u',';
572 const QStringList spreads = {
"pad"_L1,
"reflect"_L1,
"repeat"_L1 };
573 cssDescription +=
"spread:"_L1;
574 cssDescription += spreads.at(
int(gradient->spread()));
576 for (
const QGradientStop &stop : gradient->stops()) {
577 cssDescription +=
",stop:"_L1;
578 cssDescription += QString::number(stop.first);
579 cssDescription += u' ';
580 cssDescription += stop.second.name(QColor::HexArgb);
583 cssDescription +=
");"_L1;
585 return cssDescription;
590 QString cssDescription;
591 cssDescription += QStringLiteral(
"rgba(");
592 cssDescription += QString::number(color.red()) + QStringLiteral(
",");
593 cssDescription += QString::number(color.green()) + QStringLiteral(
",");
594 cssDescription += QString::number(color.blue()) + QStringLiteral(
",");
595 cssDescription += QString::number(color.alphaF()) + QStringLiteral(
")");
597 return cssDescription;
606 class QSvgFontEngine :
public QFontEngine
609 QSvgFontEngine(
const QSvgFont *font, qreal size);
611 QFontEngine *cloneWithSize(qreal size)
const override;
613 glyph_t glyphIndex(uint ucs4)
const override;
614 int stringToCMap(
const QChar *str,
616 QGlyphLayout *glyphs,
618 ShaperFlags flags)
const override;
620 void addGlyphsToPath(glyph_t *glyphs,
621 QFixedPoint *positions,
624 QTextItem::RenderFlags flags)
override;
626 glyph_metrics_t boundingBox(glyph_t glyph)
override;
628 void recalcAdvances(QGlyphLayout *, ShaperFlags)
const override;
629 QFixed ascent()
const override;
630 QFixed capHeight()
const override;
631 QFixed descent()
const override;
632 QFixed leading()
const override;
633 qreal maxCharWidth()
const override;
634 qreal minLeftBearing()
const override;
635 qreal minRightBearing()
const override;
637 QFixed emSquareSize()
const override;
640 const QSvgFont *m_font;
643 QSvgFontEngine::QSvgFontEngine(
const QSvgFont *font, qreal size)
647 fontDef.pixelSize = size;
648 fontDef.families = QStringList(m_font->m_familyName);
651 QFixed QSvgFontEngine::emSquareSize()
const
653 return QFixed::fromReal(m_font->m_unitsPerEm);
656 glyph_t QSvgFontEngine::glyphIndex(uint ucs4)
const
658 const ushort c(ucs4);
659 if (ucs4 < USHRT_MAX && m_font->findFirstGlyphFor(QStringView(&c, 1)))
660 return glyph_t(ucs4);
665 int QSvgFontEngine::stringToCMap(
const QChar *str,
667 QGlyphLayout *glyphs,
669 ShaperFlags flags)
const
671 Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
672 if (*nglyphs < len) {
678 QStringIterator it(str, str + len);
679 while (it.hasNext()) {
680 char32_t ucs4 = it.next();
681 glyph_t index = glyphIndex(ucs4);
682 glyphs->glyphs[ucs4Length++] = index;
685 *nglyphs = ucs4Length;
686 glyphs->numGlyphs = ucs4Length;
688 if (!(flags & GlyphIndicesOnly))
689 recalcAdvances(glyphs, flags);
694 void QSvgFontEngine::addGlyphsToPath(glyph_t *glyphs,
695 QFixedPoint *positions,
698 QTextItem::RenderFlags flags)
701 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
702 for (
int i = 0; i < nGlyphs; ++i) {
703 glyph_t index = glyphs[i];
705 QPointF position = positions[i].toPointF();
706 const ushort c(index);
707 const QSvgGlyph *foundGlyph = m_font->findFirstGlyphFor(QStringView(&c, 1));
712 QPainterPath glyphPath = foundGlyph->m_path;
715 xform.translate(position.x(), position.y());
716 xform.scale(scale, -scale);
717 glyphPath = xform.map(glyphPath);
718 path->addPath(glyphPath);
723 glyph_metrics_t QSvgFontEngine::boundingBox(glyph_t glyph)
728 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
729 const ushort c(glyph);
730 const QSvgGlyph *svgGlyph = m_font->findFirstGlyphFor(QStringView(&c, 1));
731 ret.width = QFixed::fromReal(svgGlyph ? svgGlyph->m_horizAdvX * scale : 0.);
732 ret.height = ascent() + descent();
736 QFontEngine *QSvgFontEngine::cloneWithSize(qreal size)
const
738 QSvgFontEngine *otherEngine =
new QSvgFontEngine(m_font, size);
742 void QSvgFontEngine::recalcAdvances(QGlyphLayout *glyphLayout, ShaperFlags)
const
744 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
745 for (
int i = 0; i < glyphLayout->numGlyphs; i++) {
746 const ushort c(glyphLayout->glyphs[i]);
747 const QSvgGlyph *svgGl = m_font->findFirstGlyphFor(QStringView(&c, 1));
748 glyphLayout->advances[i] = QFixed::fromReal(svgGl ? svgGl->m_horizAdvX * scale : 0.);
752 QFixed QSvgFontEngine::ascent()
const
754 return QFixed::fromReal(fontDef.pixelSize);
757 QFixed QSvgFontEngine::capHeight()
const
761 QFixed QSvgFontEngine::descent()
const
766 QFixed QSvgFontEngine::leading()
const
771 qreal QSvgFontEngine::maxCharWidth()
const
773 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
774 return m_font->m_horizAdvX * scale;
777 qreal QSvgFontEngine::minLeftBearing()
const
782 qreal QSvgFontEngine::minRightBearing()
const
791 const_cast<QSvgAbstractAnimatedProperty *>(property)->interpolate(1, 0.0);
793 const_cast<QSvgAbstractAnimatedProperty *>(property)->interpolate(index, 1.0);
795 return property->interpolatedValue();
800 handleBaseNodeSetup(node);
801 const bool isTextArea = node->type() == QSvgNode::Textarea;
804 const QSvgFont *svgFont = m_styleResolver->states().svgFont;
805 bool needsRichText =
false;
806 bool preserveWhiteSpace = node->whitespaceMode() == QSvgText::Preserve;
807 const QGradient *mainGradient = m_styleResolver->currentFillGradient();
809 QFontEngine *fontEngine =
nullptr;
810 if (svgFont !=
nullptr) {
811 fontEngine =
new QSvgFontEngine(svgFont, m_styleResolver->painter().font().pointSize());
812 fontEngine->ref.ref();
815#if QT_CONFIG(texthtmlparser)
816 bool needsPathNode = mainGradient !=
nullptr
817 || svgFont !=
nullptr
818 || m_styleResolver->currentStrokeGradient() !=
nullptr;
820 for (
const auto *tspan : node->tspans()) {
822 text += QStringLiteral(
"<br>");
828 handleBaseNodeSetup(tspan);
829 QFont font = m_styleResolver->painter().font();
831 QString styleTagContent;
833 if ((font.resolveMask() & QFont::FamilyResolved)
834 || (font.resolveMask() & QFont::FamiliesResolved)) {
835 styleTagContent += QStringLiteral(
"font-family: %1;").arg(font.family());
838 if (font.resolveMask() & QFont::WeightResolved
839 && font.weight() != QFont::Normal
840 && font.weight() != QFont::Bold) {
841 styleTagContent += QStringLiteral(
"font-weight: %1;").arg(
int(font.weight()));
844 if (font.resolveMask() & QFont::SizeResolved) {
846 styleTagContent += QStringLiteral(
"font-size: %1px;").arg(
int(font.pointSizeF()));
849 if (font.resolveMask() & QFont::CapitalizationResolved
850 && font.capitalization() == QFont::SmallCaps) {
851 styleTagContent += QStringLiteral(
"font-variant: small-caps;");
854 if (m_styleResolver->currentFillGradient() !=
nullptr
855 && m_styleResolver->currentFillGradient() != mainGradient) {
856 const QGradient grad = m_styleResolver->applyOpacityToGradient(*m_styleResolver->currentFillGradient(), m_styleResolver->currentFillOpacity());
857 styleTagContent += gradientCssDescription(&grad) + u';';
858#if QT_CONFIG(texthtmlparser)
859 needsPathNode =
true;
863 const QColor currentStrokeColor = m_styleResolver->currentStrokeColor();
864 if (currentStrokeColor.alpha() > 0) {
865 QString strokeColor = colorCssDescription(currentStrokeColor);
866 styleTagContent += QStringLiteral(
"-qt-stroke-color:%1;").arg(strokeColor);
867 styleTagContent += QStringLiteral(
"-qt-stroke-width:%1px;").arg(m_styleResolver->currentStrokeWidth());
868 styleTagContent += QStringLiteral(
"-qt-stroke-dasharray:%1;").arg(dashArrayString(m_styleResolver->currentStroke().dashPattern()));
869 styleTagContent += QStringLiteral(
"-qt-stroke-dashoffset:%1;").arg(m_styleResolver->currentStroke().dashOffset());
870 styleTagContent += QStringLiteral(
"-qt-stroke-lineCap:%1;").arg(capStyleName(m_styleResolver->currentStroke().capStyle()));
871 styleTagContent += QStringLiteral(
"-qt-stroke-lineJoin:%1;").arg(joinStyleName(m_styleResolver->currentStroke().joinStyle()));
872 if (m_styleResolver->currentStroke().joinStyle() == Qt::MiterJoin || m_styleResolver->currentStroke().joinStyle() == Qt::SvgMiterJoin)
873 styleTagContent += QStringLiteral(
"-qt-stroke-miterlimit:%1;").arg(m_styleResolver->currentStroke().miterLimit());
874#if QT_CONFIG(texthtmlparser)
875 needsPathNode =
true;
879 if (tspan->whitespaceMode() == QSvgText::Preserve && !preserveWhiteSpace)
880 styleTagContent += QStringLiteral(
"white-space: pre-wrap;");
882 QString content = tspan->text().toHtmlEscaped();
883 content.replace(QLatin1Char(
'\t'), QLatin1Char(
' '));
884 content.replace(QLatin1Char(
'\n'), QLatin1Char(
' '));
886 bool fontTag =
false;
887 if (!tspan->style().fill.isDefault()) {
888 auto &b = tspan->style().fill->qbrush();
889 qCDebug(lcQuickVectorImage) <<
"tspan FILL:" << b;
890 if (b.style() != Qt::NoBrush)
892 if (qFuzzyCompare(b.color().alphaF() + 1.0, 2.0))
894 QString spanColor = b.color().name();
895 fontTag = !spanColor.isEmpty();
897 text += QStringLiteral(
"<font color=\"%1\">").arg(spanColor);
899 QString spanColor = colorCssDescription(b.color());
900 styleTagContent += QStringLiteral(
"color:%1").arg(spanColor);
905 needsRichText = needsRichText || !styleTagContent.isEmpty();
906 if (!styleTagContent.isEmpty())
907 text += QStringLiteral(
"<span style=\"%1\">").arg(styleTagContent.toHtmlEscaped());
909 if (font.resolveMask() & QFont::WeightResolved && font.bold())
910 text += QStringLiteral(
"<b>");
912 if (font.resolveMask() & QFont::StyleResolved && font.italic())
913 text += QStringLiteral(
"<i>");
915 if (font.resolveMask() & QFont::CapitalizationResolved) {
916 switch (font.capitalization()) {
917 case QFont::AllLowercase:
918 content = content.toLower();
920 case QFont::AllUppercase:
921 content = content.toUpper();
923 case QFont::Capitalize:
926 qCWarning(lcQuickVectorImage) <<
"Title case not implemented for tspan";
934 text += QStringLiteral(
"</font>");
936 if (font.resolveMask() & QFont::StyleResolved && font.italic())
937 text += QStringLiteral(
"</i>");
939 if (font.resolveMask() & QFont::WeightResolved && font.bold())
940 text += QStringLiteral(
"</b>");
942 if (!styleTagContent.isEmpty())
943 text += QStringLiteral(
"</span>");
945 handleBaseNodeEnd(tspan);
948 if (preserveWhiteSpace && (needsRichText || m_styleResolver->currentFillGradient() !=
nullptr))
949 text = QStringLiteral(
"<span style=\"white-space: pre-wrap\">") + text + QStringLiteral(
"</span>");
951 QFont font = m_styleResolver->painter().font();
952 if (font.pixelSize() <= 0 && font.pointSize() > 0)
953 font.setPixelSize(font.pointSize());
955 font.setHintingPreference(QFont::PreferNoHinting);
957#if QT_CONFIG(texthtmlparser)
959 QTextDocument document;
960 document.setHtml(text);
961 if (isTextArea && node->size().width() > 0)
962 document.setTextWidth(node->size().width());
963 document.setDefaultFont(font);
964 document.pageCount();
966 QTextBlock block = document.firstBlock();
967 while (block.isValid()) {
968 QTextLayout *lout = block.layout();
970 if (lout !=
nullptr) {
971 QRectF boundingRect = lout->boundingRect();
977 QFont blockFont = block.charFormat().font();
978 if (svgFont !=
nullptr
979 && blockFont.family() == svgFont->m_familyName) {
981 QRawFontPrivate *rawFontD = QRawFontPrivate::get(rawFont);
982 rawFontD->setFontEngine(fontEngine->cloneWithSize(blockFont.pixelSize()));
984 lout->setRawFont(rawFont);
987 auto addPathForFormat = [&](QPainterPath p, QTextCharFormat fmt,
int pathIndex) {
989 fillCommonNodeInfo(node, info, QStringLiteral(
"_path%1").arg(pathIndex));
990 fillPathAnimationInfo(node, info);
991 auto fillStyle = node->style().fill;
993 info.fillRule = fillStyle->fillRule();
995 if (fmt.hasProperty(QTextCharFormat::ForegroundBrush)) {
996 info.fillColor.setDefaultValue(fmt.foreground().color());
997 if (fmt.foreground().gradient() !=
nullptr && fmt.foreground().gradient()->type() != QGradient::NoGradient)
998 info.grad = *fmt.foreground().gradient();
1000 info.fillColor.setDefaultValue(m_styleResolver->currentFillColor());
1003 info.path.setDefaultValue(QVariant::fromValue(p));
1005 const QGradient *strokeGradient = m_styleResolver->currentStrokeGradient();
1007 if (fmt.hasProperty(QTextCharFormat::TextOutline)) {
1008 pen = fmt.textOutline();
1009 if (strokeGradient ==
nullptr) {
1010 info.strokeStyle = StrokeStyle::fromPen(pen);
1011 info.strokeStyle.color.setDefaultValue(pen.color());
1014 pen = m_styleResolver->currentStroke();
1015 if (strokeGradient ==
nullptr) {
1016 info.strokeStyle = StrokeStyle::fromPen(pen);
1017 info.strokeStyle.color.setDefaultValue(m_styleResolver->currentStrokeColor());
1021 if (info.grad.type() == QGradient::NoGradient && m_styleResolver->currentFillGradient() !=
nullptr)
1022 info.grad = m_styleResolver->applyOpacityToGradient(*m_styleResolver->currentFillGradient(), m_styleResolver->currentFillOpacity());
1024 info.fillTransform = m_styleResolver->currentFillTransform();
1026 m_generator->generatePath(info, boundingRect);
1028 if (strokeGradient !=
nullptr) {
1029 PathNodeInfo strokeInfo;
1030 fillCommonNodeInfo(node, strokeInfo, QStringLiteral(
"_stroke%1").arg(pathIndex));
1031 fillPathAnimationInfo(node, strokeInfo);
1033 strokeInfo.grad = *strokeGradient;
1035 QPainterPathStroker stroker(pen);
1036 strokeInfo.path.setDefaultValue(QVariant::fromValue(stroker.createStroke(p)));
1037 m_generator->generatePath(strokeInfo, boundingRect);
1041 qreal baselineOffset = -QFontMetricsF(font).ascent();
1042 if (lout->lineCount() > 0 && lout->lineAt(0).isValid())
1043 baselineOffset = -lout->lineAt(0).ascent();
1045 const QPointF baselineTranslation(0.0, baselineOffset);
1046 auto glyphsToPath = [&](QList<QGlyphRun> glyphRuns, qreal width) {
1047 QList<QPainterPath> paths;
1048 for (
const QGlyphRun &glyphRun : glyphRuns) {
1049 QRawFont font = glyphRun.rawFont();
1050 QList<quint32> glyphIndexes = glyphRun.glyphIndexes();
1051 QList<QPointF> positions = glyphRun.positions();
1053 for (qsizetype j = 0; j < glyphIndexes.size(); ++j) {
1054 quint32 glyphIndex = glyphIndexes.at(j);
1055 const QPointF &pos = positions.at(j);
1057 QPainterPath p = font.pathForGlyph(glyphIndex);
1058 p.translate(pos + node->position() + baselineTranslation);
1059 if (m_styleResolver->states().textAnchor == Qt::AlignHCenter)
1060 p.translate(QPointF(-0.5 * width, 0));
1061 else if (m_styleResolver->states().textAnchor == Qt::AlignRight)
1062 p.translate(QPointF(-width, 0));
1070 QList<QTextLayout::FormatRange> formats = block.textFormats();
1071 for (
int i = 0; i < formats.size(); ++i) {
1072 QTextLayout::FormatRange range = formats.at(i);
1074 QList<QGlyphRun> glyphRuns = lout->glyphRuns(range.start, range.length);
1075 QList<QPainterPath> paths = glyphsToPath(glyphRuns, lout->minimumWidth());
1076 for (
int j = 0; j < paths.size(); ++j) {
1077 const QPainterPath &path = paths.at(j);
1078 addPathForFormat(path, range.format, j);
1083 block = block.next();
1089 fillCommonNodeInfo(node, info);
1090 fillAnimationInfo(node, info);
1093 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"fill"));
1094 if (!animations.isEmpty())
1095 applyAnimationsToProperty(animations, &info.fillColor, calculateInterpolatedValue);
1099 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"fill-opacity"));
1100 if (!animations.isEmpty())
1101 applyAnimationsToProperty(animations, &info.fillOpacity, calculateInterpolatedValue);
1105 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"stroke"));
1106 if (!animations.isEmpty())
1107 applyAnimationsToProperty(animations, &info.strokeColor, calculateInterpolatedValue);
1111 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"stroke-opacity"));
1112 if (!animations.isEmpty())
1113 applyAnimationsToProperty(animations, &info.strokeOpacity, calculateInterpolatedValue);
1116 info.position = node->position();
1117 info.size = node->size();
1122 info.fillColor.setDefaultValue(m_styleResolver->currentFillColor());
1123 info.alignment = m_styleResolver->states().textAnchor;
1124 info.strokeColor.setDefaultValue(m_styleResolver->currentStrokeColor());
1126 m_generator->generateTextNode(info);
1129 handleBaseNodeEnd(node);
1131 if (fontEngine !=
nullptr) {
1132 fontEngine->ref.deref();
1133 Q_ASSERT(fontEngine->ref.loadRelaxed() == 0);
1140 QSvgNode *link = node->link();
1143 handleBaseNodeSetup(node);
1145 QPointF startPos = node->start();
1146 fillCommonNodeInfo(node, info);
1147 fillAnimationInfo(node, info);
1148 if (!info.bounds.isNull())
1149 info.bounds.translate(-startPos);
1151 if (!startPos.isNull()) {
1153 if (!info.isDefaultTransform)
1154 xform = info.transform.defaultValue().value<QTransform>();
1155 xform.translate(startPos.x(), startPos.y());
1156 info.transform.setDefaultValue(QVariant::fromValue(xform));
1157 info.isDefaultTransform =
false;
1159 m_generator->generateUseNode(info);
1160 QString oldLinkSuffix = m_linkSuffix;
1161 m_linkSuffix += QStringLiteral(
"_use") + info.id;
1163 QSvgVisitor::traverse(link);
1165 m_linkSuffix = oldLinkSuffix;
1167 m_generator->generateUseNode(info);
1168 handleBaseNodeEnd(node);
1173 QSvgNode *link = node->childToRender();
1177 QString oldLinkSuffix = m_linkSuffix;
1178 m_linkSuffix += QStringLiteral(
"_switch") + QString::number(quintptr(node), 16);
1179 QSvgVisitor::traverse(link);
1180 m_linkSuffix = oldLinkSuffix;
1193 return m_pregeneratingReferencedNodes;
1203 if (m_pregeneratingReferencedNodes) {
1204 handleBaseNodeSetup(node);
1207 fillCommonNodeInfo(node, info);
1208 fillAnimationInfo(node, info);
1212 QSvgRectF r = node->rect();
1213 info.isPatternRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1214 info.patternRect = r;
1216 if (node->contentUnits() == QtSvg::UnitTypes::objectBoundingBox)
1217 qCWarning(lcQuickVectorImage) <<
"Only user space content units supported for patterns";
1219 return m_generator->generatePatternNode(info);
1227 Q_ASSERT(m_pregeneratingReferencedNodes);
1229 handleBaseNodeSetup(node);
1232 fillCommonNodeInfo(node, info);
1233 fillAnimationInfo(node, info);
1235 QSvgRectF r = node->rect();
1236 info.isPatternRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1237 info.patternRect = r;
1241 m_generator->generatePatternNode(info);
1246 if (m_useLevel == 0)
1249 handleBaseNodeSetup(node);
1252 fillCommonNodeInfo(node, info);
1253 fillAnimationInfo(node, info);
1255 QTransform oldTransform = info.transform.defaultValue().value<QTransform>();
1256 info.clipBox = oldTransform.mapRect(node->clipRect());
1258 QTransform xform = node->aspectRatioTransform();
1259 if (!xform.isIdentity()) {
1260 info.isDefaultTransform =
false;
1261 xform = xform * oldTransform;
1262 info.transform.setDefaultValue(QVariant::fromValue(xform));
1266 return m_generator->generateStructureNode(info);
1271 handleBaseNodeSetup(node);
1274 fillCommonNodeInfo(node, info);
1275 fillAnimationInfo(node, info);
1277 info.clipBox = node->clipRect();
1280 m_generator->generateStructureNode(info);
1285 if (!m_pregeneratingReferencedNodes)
1288 handleBaseNodeSetup(node);
1292 QSvgRectF r = node->rect();
1293 info.isMaskRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1296 if (node->contentUnits() == QtSvg::UnitTypes::objectBoundingBox)
1297 qCWarning(lcQuickVectorImage) <<
"Only user space content units supported for masks";
1299 fillCommonNodeInfo(node, info);
1301 return m_generator->generateMaskNode(info);
1309 QSvgRectF r = node->rect();
1310 info.isMaskRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1312 fillCommonNodeInfo(node, info);
1314 m_generator->generateMaskNode(info);
1316 handleBaseNodeEnd(node);
1323 if (!m_pregeneratingReferencedNodes)
1326 if (!m_filterPrimitives.isEmpty()) {
1327 qCWarning(lcQuickVectorImage) <<
"Filter defined inside a filter";
1336 if (m_filterPrimitives.isEmpty())
1339 handleBaseNodeSetup(node);
1342 fillCommonNodeInfo(node, info);
1344 info.filterRect = node->rect();
1345 if (node->filterUnits() == QtSvg::UnitTypes::objectBoundingBox)
1348 bool generatedAlpha =
false;
1349 for (
const QSvgFeFilterPrimitive *filterPrimitive : std::as_const(m_filterPrimitives)) {
1350 if (filterPrimitive->requiresSourceAlpha() && !generatedAlpha) {
1351 FilterNodeInfo::FilterStep alphaStep;
1352 alphaStep.filterType = FilterNodeInfo::Type::ColorMatrix;
1353 alphaStep.csFilterParameter = FilterNodeInfo::CoordinateSystem::MatchFilterRect;
1356 qreal values[] = { 0.0, 0.0, 0.0, 0.0, 0.0,
1357 0.0, 0.0, 0.0, 0.0, 0.0,
1358 0.0, 0.0, 0.0, 0.0, 0.0,
1359 0.0, 0.0, 0.0, 1.0, 0.0,
1360 0.0, 0.0, 0.0, 0.0, 0.0 };
1361 QGenericMatrix<5, 5, qreal> matrix(values);
1362 alphaStep.filterParameter = QVariant::fromValue(matrix);
1363 alphaStep.outputName = info.id + QStringLiteral(
"_source_alpha");
1364 generatedAlpha =
true;
1366 info.steps.append(alphaStep);
1369 fillFilterPrimitiveInfo(node, filterPrimitive, info);
1372 m_generator->generateFilterNode(info);
1373 m_filterPrimitives.clear();
1376void QSvgVisitorImpl::fillFilterPrimitiveInfo(
const QSvgFilterContainer *node,
1377 const QSvgFeFilterPrimitive *filterPrimitive,
1381 step.filterPrimitiveRect = filterPrimitive->rect();
1383 step.outputName = info.id + QStringLiteral(
"_")
1384 + (filterPrimitive->result().isEmpty()
1385 ? QStringLiteral(
"output_") + QString::number(info.steps.size())
1386 : filterPrimitive->result());
1388 auto findInput = [&info, &filterPrimitive](
const QString &input, QString *outName) {
1389 const QString alphaSource = info.id + QStringLiteral(
"_source_alpha");
1390 if (input == QStringLiteral(
"SourceGraphic")) {
1392 }
else if (input == QStringLiteral(
"SourceAlpha")) {
1393 *outName = alphaSource;
1395 }
else if (info.steps.isEmpty()) {
1399 if (!input.isEmpty()) {
1400 *outName = info.id + QStringLiteral(
"_") + input;
1402 bool insideMergeNode = filterPrimitive->type() == QSvgNode::FeMergenode;
1403 for (
int i = info.steps.size() - 1; i >= 0; --i) {
1404 const auto &prevStep = info.steps.at(i);
1406 insideMergeNode =
false;
1410 if (!prevStep.outputName.isEmpty() && prevStep.outputName != alphaSource) {
1411 *outName = prevStep.outputName;
1420 step
.input1 = findInput(filterPrimitive->input(), &step.namedInput1);
1422 if (node->primitiveUnits() == QtSvg::UnitTypes::objectBoundingBox)
1429 if (node->primitiveUnits() == QtSvg::UnitTypes::userSpaceOnUse
1430 && filterPrimitive->rect().unitW() == QtSvg::UnitTypes::unknown) {
1434 switch (filterPrimitive->type()) {
1435 case QSvgNode::FeMerge:
1438 case QSvgNode::FeMergenode:
1441 case QSvgNode::FeBlend:
1443 const QSvgFeBlend *blend =
static_cast<
const QSvgFeBlend *>(filterPrimitive);
1444 switch (blend->mode()) {
1445 case QSvgFeBlend::Mode::Normal:
1448 case QSvgFeBlend::Mode::Multiply:
1451 case QSvgFeBlend::Mode::Screen:
1454 case QSvgFeBlend::Mode::Darken:
1457 case QSvgFeBlend::Mode::Lighten:
1462 step
.input2 = findInput(blend->input2(), &step.namedInput2);
1465 case QSvgNode::FeComposite:
1467 const QSvgFeComposite *composite =
static_cast<
const QSvgFeComposite *>(filterPrimitive);
1468 switch (composite->compositionOperator()) {
1469 case QSvgFeComposite::Operator::Over:
1472 case QSvgFeComposite::Operator::In:
1475 case QSvgFeComposite::Operator::Out:
1478 case QSvgFeComposite::Operator::Atop:
1481 case QSvgFeComposite::Operator::Xor:
1484 case QSvgFeComposite::Operator::Lighter:
1487 case QSvgFeComposite::Operator::Arithmetic:
1492 step
.input2 = findInput(composite->input2(), &step.namedInput2);
1493 step.filterParameter = composite->k();
1496 case QSvgNode::FeOffset:
1498 const QSvgFeOffset *offset =
static_cast<
const QSvgFeOffset *>(filterPrimitive);
1500 step.filterParameter = QVariant::fromValue(QVector2D(offset->dx(), offset->dy()));
1504 case QSvgNode::FeColormatrix:
1506 const QSvgFeColorMatrix *colorMatrix =
1507 static_cast<
const QSvgFeColorMatrix *>(filterPrimitive);
1509 step.filterParameter = QVariant::fromValue(colorMatrix->matrix());
1513 case QSvgNode::FeGaussianblur:
1515 const QSvgFeGaussianBlur *gaussianBlur =
1516 static_cast<
const QSvgFeGaussianBlur *>(filterPrimitive);
1518 if (gaussianBlur->edgeMode() == QSvgFeGaussianBlur::EdgeMode::Wrap)
1519 info.wrapMode = QSGTexture::Repeat;
1521 if (!qFuzzyCompare(gaussianBlur->stdDeviationX(), gaussianBlur->stdDeviationY()))
1522 qCWarning(lcQuickVectorImage) <<
"Separate X and Y deviations not supported for gaussian blur";
1523 step.filterParameter = std::max(gaussianBlur->stdDeviationX(),
1524 gaussianBlur->stdDeviationY());
1528 case QSvgNode::FeFlood:
1530 const QSvgFeFlood *flood =
1531 static_cast<
const QSvgFeFlood *>(filterPrimitive);
1534 step.filterParameter = flood->color();
1543 info.steps.append(step);
1548 m_filterPrimitives.append(node);
1559 constexpr bool forceSeparatePaths =
false;
1560 handleBaseNodeSetup(node);
1564 fillCommonNodeInfo(node, info);
1565 fillAnimationInfo(node, info);
1567 info.isPathContainer = isPathContainer(node);
1570 return m_generator->generateStructureNode(info);
1575 handleBaseNodeEnd(node);
1580 fillCommonNodeInfo(node, info);
1581 info.isPathContainer = isPathContainer(node);
1584 m_generator->generateStructureNode(info);
1589 return QStringLiteral(
"_qt_node%1").arg(m_nodeIdCounter++);
1594 handleBaseNodeSetup(node);
1597 fillCommonNodeInfo(node, info);
1598 fillAnimationInfo(node, info);
1600 const QSvgDocument *doc =
static_cast<
const QSvgDocument *>(node);
1601 info.size = doc->size();
1602 info.viewBox = doc->viewBox();
1603 info.isPathContainer = isPathContainer(node);
1607 if (m_generator->generateRootNode(info)) {
1608 pregenerateReferencedNodes(node);
1617 handleBaseNodeEnd(node);
1618 qCDebug(lcQuickVectorImage) <<
"REVERT" << node->nodeId() << node->type() << (m_styleResolver->painter().pen().style() != Qt::NoPen)
1619 << m_styleResolver->painter().pen().color().name() << (m_styleResolver->painter().pen().brush().style() != Qt::NoBrush)
1620 << m_styleResolver->painter().pen().brush().color().name();
1623 fillCommonNodeInfo(node, info);
1626 m_generator->generateRootNode(info);
1631 QString ret = m_idForNodeId.value(id);
1632 if (ret.isEmpty()) {
1634 m_idForNodeId.insert(id, ret);
1639QString
QSvgVisitorImpl::findOrCreateId(
const QSvgNode *node,
const QString &nodeId)
1641 QString key = nodeId;
1642 const QSvgNode *n = m_nodesForKeys.value(key);
1643 if (key.isEmpty() || (n !=
nullptr && n != node))
1644 key = QString::number(quintptr(node), 16);
1646 m_nodesForKeys.insert(key, node);
1647 return findOrCreateId(key);
1652 const QString nodeId = scrub(node->nodeId());
1653 info.id = findOrCreateId(node, nodeId);
1656 info.id += idSuffix;
1658 if (!m_linkSuffix.isEmpty())
1659 info.id += m_linkSuffix;
1661 info.nodeId = nodeId;
1662 info.typeName = node->typeName();
1665 QTransform xf = !info
.isDefaultTransform ? node->style().transform->qtransform() : QTransform();
1666 info.transform.setDefaultValue(QVariant::fromValue(xf));
1668 info.opacity.setDefaultValue(!info
.isDefaultOpacity ? node->style().opacity->opacity() : 1.0);
1670 info.isDisplayed = node->displayMode() != QSvgNode::DisplayMode::NoneMode;
1672 if (node->hasFilter()
1674 || node->type() == QSvgNode::Type::Mask
1675 || node->type() == QSvgNode::Type::Pattern) {
1676 QImage dummy(1, 1, QImage::Format_RGB32);
1678 QSvgExtraStates states;
1679 p.setPen(QPen(Qt::NoPen));
1680 info.bounds = node->internalBounds(&p, states);
1683 if (node->hasMask())
1684 info.maskId = findOrCreateId(node->maskId());
1686 if (node->hasFilter())
1687 info.filterId = findOrCreateId(node->filterId());
1690QList<QSvgVisitorImpl::AnimationPair>
QSvgVisitorImpl::collectAnimations(
const QSvgNode *node,
1691 const QString &propertyName)
1693 QList<AnimationPair> ret;
1694 const QList<QSvgAbstractAnimation *> animations = node->document()->animator()->animationsForNode(node);
1695 for (
const QSvgAbstractAnimation *animation : animations) {
1696 const QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
1697 for (
const QSvgAbstractAnimatedProperty *property : properties) {
1698 if (property->propertyName() == propertyName)
1699 ret.append(std::make_pair(animation, property));
1706void QSvgVisitorImpl::applyAnimationsToProperty(
const QList<AnimationPair> &animations,
1707 QQuickAnimatedProperty *outProperty,
1708 std::function<QVariant(
const QSvgAbstractAnimatedProperty *,
int index,
int animationIndex)> calculateValue)
1710 qCDebug(lcVectorImageAnimations) <<
"Applying animations to property with default value"
1711 << outProperty->defaultValue();
1712 for (
auto it = animations.constBegin(); it != animations.constEnd(); ++it) {
1713 qCDebug(lcVectorImageAnimations) <<
" -> Add animation";
1714 const QSvgAbstractAnimation *animation = it->first;
1715 const QSvgAbstractAnimatedProperty *property = it->second;
1717 const int start = animation->start();
1718 const int repeatCount = animation->iterationCount();
1719 const int duration = animation->duration();
1721 bool freeze =
false;
1722 bool replace =
true;
1723 if (animation->animationType() == QSvgAbstractAnimation::SMIL) {
1724 const QSvgAnimateNode *animateNode =
static_cast<
const QSvgAnimateNode *>(animation);
1725 freeze = animateNode->fill() == QSvgAnimateNode::Freeze;
1726 replace = animateNode->additiveType() == QSvgAnimateNode::Replace;
1729 qCDebug(lcVectorImageAnimations) <<
" -> Start:" << start
1730 <<
", repeatCount:" << repeatCount
1731 <<
", freeze:" << freeze
1732 <<
", replace:" << replace;
1734 QBezier easing = easingForAnimation(animation);
1735 QList<qreal> propertyKeyFrames = property->keyFrames();
1736 QList<QQuickAnimatedProperty::PropertyAnimation> outAnimations;
1740 if (property->type() == QSvgAbstractAnimatedProperty::Transform) {
1741 const auto *transformProperty =
static_cast<
const QSvgAnimatedPropertyTransform *>(property);
1742 const auto &components = transformProperty->components();
1743 Q_ASSERT(q20::cmp_greater_equal(components.size(),transformProperty->transformCount()));
1744 for (uint i = 0; i < transformProperty->transformCount(); ++i) {
1745 QQuickAnimatedProperty::PropertyAnimation outAnimation;
1746 outAnimation.repeatCount = repeatCount;
1747 outAnimation.startOffset = start;
1749 outAnimation.flags |= QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1751 outAnimation.flags |= QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
1752 switch (components.at(i).type) {
1753 case QSvgAnimatedPropertyTransform::TransformComponent::Translate:
1754 outAnimation.subtype = QTransform::TxTranslate;
1756 case QSvgAnimatedPropertyTransform::TransformComponent::Scale:
1757 outAnimation.subtype = QTransform::TxScale;
1759 case QSvgAnimatedPropertyTransform::TransformComponent::Rotate:
1760 outAnimation.subtype = QTransform::TxRotate;
1762 case QSvgAnimatedPropertyTransform::TransformComponent::Skew:
1763 outAnimation.subtype = QTransform::TxShear;
1766 qCWarning(lcQuickVectorImage()) <<
"Unhandled transform type:" << components.at(i).type;
1770 qDebug(lcVectorImageAnimations) <<
" -> Property type:"
1773 << property->propertyName()
1774 <<
" animation subtype:"
1775 << outAnimation.subtype;
1777 outAnimations.append(outAnimation);
1780 QQuickAnimatedProperty::PropertyAnimation outAnimation;
1781 outAnimation.repeatCount = repeatCount;
1782 outAnimation.startOffset = start;
1784 outAnimation.flags |= QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1786 qDebug(lcVectorImageAnimations) <<
" -> Property type:"
1789 << property->propertyName();
1791 outAnimations.append(outAnimation);
1794 outProperty->beginAnimationGroup();
1795 for (
int i = 0; i < outAnimations.size(); ++i) {
1796 QQuickAnimatedProperty::PropertyAnimation outAnimation = outAnimations.at(i);
1798 for (
int j = 0; j < propertyKeyFrames.size(); ++j) {
1799 const int time = qRound(propertyKeyFrames.at(j) * duration);
1801 const QVariant value = calculateValue(property, j, i);
1802 outAnimation.frames[time] = value;
1803 outAnimation.easingPerFrame[time] = easing;
1804 qCDebug(lcVectorImageAnimations) <<
" -> Frame " << time <<
" is " << value;
1807 outProperty->addAnimation(outAnimation);
1812QBezier
QSvgVisitorImpl::easingForAnimation(
const QSvgAbstractAnimation *animation)
1814 constexpr QPointF startControlPoint(0, 0);
1815 constexpr QPointF endControlPoint(1, 1);
1816 constexpr QPointF easeC1(0.25, 0.1);
1817 constexpr QPointF easeC2(0.25, 1);
1819 QBezier easing = QBezier::fromPoints(startControlPoint, startControlPoint, endControlPoint, endControlPoint);
1821#if QT_CONFIG(cssparser)
1822 if (animation->animationType() == QSvgAbstractAnimation::CSS) {
1823 QSvgEasingInterface *easingInterface = animation->easing();
1824 QSvgCssEasing *cssEasing =
static_cast<QSvgCssEasing *>(easingInterface);
1825 switch (cssEasing->easingFunction()) {
1826 case QSvgCssValues::EasingFunction::Ease:
1827 case QSvgCssValues::EasingFunction::EaseIn:
1828 case QSvgCssValues::EasingFunction::EaseOut:
1829 case QSvgCssValues::EasingFunction::EaseInOut:
1830 case QSvgCssValues::EasingFunction::CubicBezier:
1831 case QSvgCssValues::EasingFunction::Linear:
1833 const QSvgCssCubicBezierEasing *cssCubicEasing =
static_cast<
const QSvgCssCubicBezierEasing *>(cssEasing);
1834 QPointF c1 = cssCubicEasing->c1();
1835 QPointF c2 = cssCubicEasing->c2();
1836 easing = QBezier::fromPoints(startControlPoint, c1, c2, endControlPoint);
1839 case QSvgCssValues::EasingFunction::Steps:
1841 qCDebug(lcVectorImageAnimations) <<
"Step easing is not supported reverting to default.";
1842 easing = QBezier::fromPoints(startControlPoint, easeC1, easeC2, endControlPoint);
1856 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"fill"));
1857 if (!animations.isEmpty())
1858 applyAnimationsToProperty(animations, &info.fillColor, calculateInterpolatedValue);
1862 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"fill-opacity"));
1863 if (!animations.isEmpty())
1864 applyAnimationsToProperty(animations, &info.fillOpacity, calculateInterpolatedValue);
1868 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"stroke"));
1869 if (!animations.isEmpty())
1870 applyAnimationsToProperty(animations, &info.strokeStyle.color, calculateInterpolatedValue);
1874 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"stroke-opacity"));
1875 if (!animations.isEmpty())
1876 applyAnimationsToProperty(animations, &info.strokeStyle.opacity, calculateInterpolatedValue);
1882 qCDebug(lcVectorImageAnimations) <<
"Applying transform animations to property with default value"
1883 << info.transform.defaultValue();
1885 auto calculateValue = [](
const QSvgAbstractAnimatedProperty *property,
int index,
int animationIndex) {
1886 if (property->type() != QSvgAbstractAnimatedProperty::Transform)
1889 const auto *transformProperty =
static_cast<
const QSvgAnimatedPropertyTransform *>(property);
1890 const auto &components = transformProperty->components();
1892 const int componentIndex = index * transformProperty->transformCount() + animationIndex;
1894 QVariantList parameters;
1896 const QSvgAnimatedPropertyTransform::TransformComponent &component = components.at(componentIndex);
1897 switch (component.type) {
1898 case QSvgAnimatedPropertyTransform::TransformComponent::Translate:
1899 parameters.append(QVariant::fromValue(QPointF(component.values.value(0),
1900 component.values.value(1))));
1902 case QSvgAnimatedPropertyTransform::TransformComponent::Rotate:
1903 parameters.append(QVariant::fromValue(QPointF(component.values.value(1),
1904 component.values.value(2))));
1905 parameters.append(QVariant::fromValue(component.values.value(0)));
1907 case QSvgAnimatedPropertyTransform::TransformComponent::Scale:
1908 parameters.append(QVariant::fromValue(QPointF(component.values.value(0),
1909 component.values.value(1))));
1911 case QSvgAnimatedPropertyTransform::TransformComponent::Skew:
1912 parameters.append(QVariant::fromValue(QPointF(component.values.value(0),
1913 component.values.value(1))));
1916 qCWarning(lcVectorImageAnimations) <<
"Unhandled transform type:" << component.type;
1919 return QVariant::fromValue(parameters);
1924 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"transform"));
1925 if (!animations.isEmpty())
1926 applyAnimationsToProperty(animations, &info.transform, calculateValue);
1932 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"offset-distance"));
1933 if (animations.isEmpty())
1936 if (animations.size() > 1) {
1937 qCWarning(lcQuickVectorImage)
1938 <<
"Not supported: More than one offset path animation on same node";
1941 if (node->style().offset ==
nullptr) {
1942 qCWarning(lcQuickVectorImage) <<
"Motion path animation: No offset path";
1946 const AnimationPair &animationPair = animations.first();
1948 const QSvgAbstractAnimation *animation = animationPair.first;
1949 const QSvgAbstractAnimatedProperty *property = animationPair.second;
1951 const int start = animation->start();
1952 const int repeatCount = animation->iterationCount();
1953 const int duration = animation->duration();
1955 qCDebug(lcVectorImageAnimations) <<
"Motion path animation:"
1956 <<
"start == " << start
1957 <<
", repeatCount == " << repeatCount
1958 <<
"; duration == " << duration;
1961 QQuickAnimatedProperty::PropertyAnimation outAnimation;
1962 outAnimation.repeatCount = repeatCount;
1963 outAnimation.startOffset = start;
1965 QPainterPath originalPath = node->style().offset->path();
1969 switch (node->style().offset->rotateType()) {
1970 case QtSvg::OffsetRotateType::Auto:
1974 case QtSvg::OffsetRotateType::Angle:
1976 baseRotation = node->style().offset->rotateAngle();
1978 case QtSvg::OffsetRotateType::AutoAngle:
1980 baseRotation = node->style().offset->rotateAngle();
1982 case QtSvg::OffsetRotateType::Reverse:
1984 baseRotation = 180.0;
1986 case QtSvg::OffsetRotateType::ReverseAngle:
1988 baseRotation = node->style().offset->rotateAngle() + 180.0f;
1995 info.motionPath.setDefaultValue(QVariant::fromValue(QVariantPair(adaptAngle, baseRotation)));
1997 const QList<qreal> propertyKeyFrames = property->keyFrames();
1999 qreal previousT = 0.0;
2000 for (
int j = 0; j < propertyKeyFrames.size(); ++j) {
2001 const int time = qRound(propertyKeyFrames.at(j) * duration);
2003 qreal t = calculateInterpolatedValue(property, j, 0).toReal();
2006 QPainterPath path = originalPath.trimmed(previousT, t);
2008 outAnimation.frames[time] = QVariant::fromValue(path);
2009 qCDebug(lcVectorImageAnimations) <<
" -> Frame " << time <<
" is " << path;
2015 info.motionPath.addAnimation(outAnimation);
2020 fillColorAnimationInfo(node, info);
2021 fillAnimationInfo(node, info);
2027 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral(
"opacity"));
2028 if (!animations.isEmpty())
2029 applyAnimationsToProperty(animations, &info.opacity, calculateInterpolatedValue);
2032 fillTransformAnimationInfo(node, info);
2033 fillMotionPathAnimationInfo(node, info);
2038 qCDebug(lcQuickVectorImage) <<
"Before SETUP" << node <<
"fill" << m_styleResolver->currentFillColor()
2039 <<
"stroke" << m_styleResolver->currentStrokeColor() << m_styleResolver->currentStrokeWidth()
2040 << node->nodeId() <<
" type: " << node->typeName() <<
" " << node->type();
2042 node->applyStyle(&m_styleResolver->painter(), m_styleResolver->states());
2044 qCDebug(lcQuickVectorImage) <<
"After SETUP" << node <<
"fill" << m_styleResolver->currentFillColor()
2045 <<
"stroke" << m_styleResolver->currentStrokeColor()
2046 << m_styleResolver->currentStrokeWidth() << node->nodeId();
2052 fillCommonNodeInfo(node, info);
2054 m_generator->generateNodeBase(info);
2059 node->revertStyle(&m_styleResolver->painter(), m_styleResolver->states());
2061 qCDebug(lcQuickVectorImage) <<
"After END" << node <<
"fill" << m_styleResolver->currentFillColor()
2062 <<
"stroke" << m_styleResolver->currentStrokeColor() << m_styleResolver->currentStrokeWidth()
2066void QSvgVisitorImpl::handlePathNode(
const QSvgNode *node,
const QPainterPath &path)
2068 handleBaseNodeSetup(node);
2071 fillCommonNodeInfo(node, info);
2073 if (node->hasMarkerStart())
2074 info.markerStartId = findOrCreateId(node->markerStartId());
2076 if (node->hasMarkerMid())
2077 info.markerMidId = findOrCreateId(node->markerMidId());
2079 if (node->hasMarkerEnd())
2080 info.markerEndId = findOrCreateId(node->markerEndId());
2082 const QGradient *strokeGradient = m_styleResolver->currentStrokeGradient();
2083 auto strokeStyle = node->style().stroke;
2084 bool hasStrokePattern = strokeStyle
2085 && strokeStyle->style()
2086 && strokeStyle->style()->type() == QSvgStyleProperty::PATTERN;
2088 info.path.setDefaultValue(QVariant::fromValue(path));
2089 info.fillColor.setDefaultValue(m_styleResolver->currentFillColor());
2090 if (strokeGradient ==
nullptr && !hasStrokePattern) {
2091 info.strokeStyle = StrokeStyle::fromPen(m_styleResolver->currentStroke());
2092 info.strokeStyle.color.setDefaultValue(m_styleResolver->currentStrokeColor());
2094 if (m_styleResolver->currentFillGradient() !=
nullptr)
2095 info.grad = m_styleResolver->applyOpacityToGradient(*m_styleResolver->currentFillGradient(), m_styleResolver->currentFillOpacity());
2096 info.fillTransform = m_styleResolver->currentFillTransform();
2098 auto fillStyle = node->style().fill;
2100 info.fillRule = fillStyle->fillRule();
2102 if (fillStyle->style() && fillStyle->style()->type() == QSvgStyleProperty::PATTERN) {
2103 QSvgPatternStyle *patternStyle =
static_cast<QSvgPatternStyle *>(fillStyle->style());
2104 info.patternId = findOrCreateId(patternStyle->patternNode()->nodeId());
2110 info.fillTransform = patternStyle->patternNode()->transform();
2114 fillPathAnimationInfo(node, info);
2116 m_generator->generatePath(info);
2118 if (strokeGradient !=
nullptr || hasStrokePattern) {
2120 fillCommonNodeInfo(node, strokeInfo, QStringLiteral(
"_stroke"));
2122 if (strokeGradient !=
nullptr) {
2123 strokeInfo.grad = *strokeGradient;
2125 QSvgPatternStyle *patternStyle =
static_cast<QSvgPatternStyle *>(strokeStyle->style());
2126 strokeInfo.patternId = findOrCreateId(patternStyle->patternNode()->nodeId());
2127 strokeInfo.fillTransform = patternStyle->patternNode()->transform();
2130 QPainterPathStroker stroker(m_styleResolver->currentStroke());
2131 strokeInfo.path.setDefaultValue(QVariant::fromValue(stroker.createStroke(path)));
2132 m_generator->generatePath(strokeInfo);
2135 handleBaseNodeEnd(node);
2140 QTransform oldTransform = info.transform.defaultValue().value<QTransform>();
2142 info.markerSize = node->rect().size();
2143 info.anchorPoint = node->refP();
2144 info.clipBox = oldTransform.mapRect(node->clipRect());
2145 info.viewBox = node->viewBox();
2146 switch (node->orientation()) {
2147 case QSvgMarker::Orientation::Auto:
2150 case QSvgMarker::Orientation::AutoStartReverse:
2153 case QSvgMarker::Orientation::Value:
2158 switch (node->markerUnits()) {
2159 case QSvgMarker::MarkerUnits::UserSpaceOnUse:
2162 case QSvgMarker::MarkerUnits::StrokeWidth:
2167 info.angle = node->orientationAngle();
2169 QTransform xform = node->aspectRatioTransform();
2170 if (!xform.isIdentity()) {
2171 info.isDefaultTransform =
false;
2172 xform = xform * oldTransform;
2173 info.transform.setDefaultValue(QVariant::fromValue(xform));
2181 if (!m_pregeneratingReferencedNodes)
2184 handleBaseNodeSetup(node);
2188 fillCommonNodeInfo(node, info);
2189 fillAnimationInfo(node, info);
2190 fillMarkerInfo(node, info);
2193 return m_generator->generateMarkerNode(info);
2198 handleBaseNodeEnd(node);
2201 fillCommonNodeInfo(node, info);
2202 fillMarkerInfo(node, info);
2205 m_generator->generateMarkerNode(info);
const QGradient * currentFillGradient() const
const QGradient * currentStrokeGradient() const
QColor currentFillColor() const
QSvgExtraStates & states()
static QGradient applyOpacityToGradient(const QGradient &gradient, float opacity)
QTransform currentFillTransform() const
QPen currentStroke() const
qreal currentFillOpacity() const
float currentStrokeWidth() const
QSvgExtraStates m_svgState
QColor currentStrokeColor() const
bool visitFilterNodeStart(const QSvgFilterContainer *node) override
void visitPolygonNode(const QSvgPolygon *node) override
bool visitSwitchNodeStart(const QSvgSwitch *node) override
void visitMaskNodeEnd(const QSvgMask *node) override
void visitEllipseNode(const QSvgEllipse *node) override
void visitPathNode(const QSvgPath *node) override
void visitMarkerNodeEnd(const QSvgMarker *node) override
bool visitDefsNodeStart(const QSvgDefs *node) override
void visitDefsNodeEnd(const QSvgDefs *node) override
bool visitMaskNodeStart(const QSvgMask *node) override
void visitDocumentNodeEnd(const QSvgDocument *node) override
~QSvgVisitorImpl() override
void visitStructureNodeEnd(const QSvgStructureNode *node) override
void visitRectNode(const QSvgRect *node) override
void visitFeFilterPrimitiveNodeEnd(const QSvgFeFilterPrimitive *node) override
void visitLineNode(const QSvgLine *node) override
void visitNode(const QSvgNode *node) override
bool visitMarkerNodeStart(const QSvgMarker *node) override
void visitPolylineNode(const QSvgPolyline *node) override
QSvgVisitorImpl(const QString svgFileName, QQuickGenerator *generator, bool assumeTrustedSource)
void visitUseNode(const QSvgUse *node) override
bool visitSymbolNodeStart(const QSvgSymbol *node) override
void visitPatternNodeEnd(const QSvgPattern *) override
bool visitDocumentNodeStart(const QSvgDocument *node) override
void visitImageNode(const QSvgImage *node) override
void visitFilterNodeEnd(const QSvgFilterContainer *node) override
bool visitPatternNodeStart(const QSvgPattern *) override
void visitTextNode(const QSvgText *node) override
void visitSwitchNodeEnd(const QSvgSwitch *node) override
void visitSymbolNodeEnd(const QSvgSymbol *node) override
bool visitFeFilterPrimitiveNodeStart(const QSvgFeFilterPrimitive *node) override
bool visitStructureNodeStart(const QSvgStructureNode *node) override
Combined button and popup list for selecting options.
static QString scrub(const QString &raw)
static bool isStructureNode(const QSvgNode *node)
static void recurseSvgNodes(const QSvgNode *root, const std::function< void(const QSvgNode *)> &fnc)
static QVariant calculateInterpolatedValue(const QSvgAbstractAnimatedProperty *property, int index, int)
CoordinateSystem csFilterParameter
CoordinateSystem csFilterRect
PreserveAspectRatio preserveAspectRatio